blob: 3b5340b16472ccdba8e35d6c8e40718f055d102c [file] [log] [blame]
Grant Likely6469dfb2011-07-26 03:19:06 -06001#include <linux/irq.h>
2#include <linux/irqdomain.h>
3#include <linux/module.h>
4#include <linux/mutex.h>
5#include <linux/of.h>
Grant Likely971a0ee2011-07-26 03:19:06 -06006#include <linux/of_address.h>
7#include <linux/slab.h>
Grant Likely6469dfb2011-07-26 03:19:06 -06008
9static LIST_HEAD(irq_domain_list);
10static DEFINE_MUTEX(irq_domain_mutex);
11
12/**
13 * irq_domain_add() - Register an irq_domain
14 * @domain: ptr to initialized irq_domain structure
15 *
Michael Bohane6717fd2012-01-03 16:37:43 -080016 * Adds a irq_domain structure. The irq_domain must at a minimum be
Grant Likely6469dfb2011-07-26 03:19:06 -060017 * initialized with an ops structure pointer, and either a ->to_irq hook or
Michael Bohan6f4df412012-01-11 11:45:16 -080018 * a valid irq_base value. The irq range must be mutually exclusive with
19 * domains already registered. Everything else is optional.
Grant Likely6469dfb2011-07-26 03:19:06 -060020 */
Michael Bohan6f4df412012-01-11 11:45:16 -080021int irq_domain_add(struct irq_domain *domain)
Grant Likely6469dfb2011-07-26 03:19:06 -060022{
Michael Bohan6f4df412012-01-11 11:45:16 -080023 struct irq_domain *curr;
24 uint32_t d_highirq = domain->irq_base + domain->nr_irq - 1;
25
26 if (!domain->nr_irq)
27 return -EINVAL;
28
Michael Bohane6717fd2012-01-03 16:37:43 -080029 mutex_lock(&irq_domain_mutex);
Michael Bohan6f4df412012-01-11 11:45:16 -080030 /* insert in ascending order of domain->irq_base */
31 list_for_each_entry(curr, &irq_domain_list, list) {
32 uint32_t c_highirq = curr->irq_base + curr->nr_irq - 1;
33 if (domain->irq_base < curr->irq_base &&
34 d_highirq < curr->irq_base) {
35 break;
36 }
37 if (d_highirq <= c_highirq) {
38 mutex_unlock(&irq_domain_mutex);
39 return -EINVAL;
40 }
41 }
42 list_add_tail(&domain->list, &curr->list);
Michael Bohane6717fd2012-01-03 16:37:43 -080043 mutex_unlock(&irq_domain_mutex);
Michael Bohan6f4df412012-01-11 11:45:16 -080044
45 return 0;
Michael Bohane6717fd2012-01-03 16:37:43 -080046}
47
48/**
49 * irq_domain_register() - Register an entire irq_domain
50 * @domain: ptr to initialized irq_domain structure
51 *
52 * Registers the entire irq_domain. The irq_domain must at a minimum be
53 * initialized with an ops structure pointer, and either a ->to_irq hook or
54 * a valid irq_base value. Everything else is optional.
55 */
56void irq_domain_register(struct irq_domain *domain)
57{
Grant Likely6469dfb2011-07-26 03:19:06 -060058 struct irq_data *d;
Rob Herring60fe4602011-09-30 10:48:38 -050059 int hwirq, irq;
Grant Likely6469dfb2011-07-26 03:19:06 -060060
Rob Herring60fe4602011-09-30 10:48:38 -050061 irq_domain_for_each_irq(domain, hwirq, irq) {
62 d = irq_get_irq_data(irq);
Rob Herring36b2c362011-09-14 11:31:37 -050063 if (!d) {
64 WARN(1, "error: assigning domain to non existant irq_desc");
65 return;
66 }
67 if (d->domain) {
Grant Likely6469dfb2011-07-26 03:19:06 -060068 /* things are broken; just report, don't clean up */
69 WARN(1, "error: irq_desc already assigned to a domain");
70 return;
71 }
72 d->domain = domain;
73 d->hwirq = hwirq;
74 }
Grant Likely6469dfb2011-07-26 03:19:06 -060075}
76
77/**
Michael Bohane6717fd2012-01-03 16:37:43 -080078 * irq_domain_register_irq() - Register an irq_domain
79 * @domain: ptr to initialized irq_domain structure
80 * @hwirq: irq_domain hwirq to register
81 *
82 * Registers a specific hwirq within the irq_domain. The irq_domain
83 * must at a minimum be initialized with an ops structure pointer, and
84 * either a ->to_irq hook or a valid irq_base value. Everything else is
85 * optional.
86 */
87void irq_domain_register_irq(struct irq_domain *domain, int hwirq)
88{
89 struct irq_data *d;
90
91 d = irq_get_irq_data(irq_domain_to_irq(domain, hwirq));
92 if (!d) {
93 WARN(1, "error: assigning domain to non existant irq_desc");
94 return;
95 }
96 if (d->domain) {
97 /* things are broken; just report, don't clean up */
98 WARN(1, "error: irq_desc already assigned to a domain");
99 return;
100 }
101 d->domain = domain;
102 d->hwirq = hwirq;
103}
104
105/**
106 * irq_domain_del() - Removes a irq_domain from the system
Grant Likely6469dfb2011-07-26 03:19:06 -0600107 * @domain: ptr to registered irq_domain.
108 */
109void irq_domain_del(struct irq_domain *domain)
110{
Grant Likely6469dfb2011-07-26 03:19:06 -0600111 mutex_lock(&irq_domain_mutex);
112 list_del(&domain->list);
113 mutex_unlock(&irq_domain_mutex);
Michael Bohane6717fd2012-01-03 16:37:43 -0800114}
115
116/**
117 * irq_domain_unregister() - Unregister an irq_domain
118 * @domain: ptr to registered irq_domain.
119 */
120void irq_domain_unregister(struct irq_domain *domain)
121{
122 struct irq_data *d;
123 int hwirq, irq;
Grant Likely6469dfb2011-07-26 03:19:06 -0600124
125 /* Clear the irq_domain assignments */
Rob Herring60fe4602011-09-30 10:48:38 -0500126 irq_domain_for_each_irq(domain, hwirq, irq) {
127 d = irq_get_irq_data(irq);
Grant Likely6469dfb2011-07-26 03:19:06 -0600128 d->domain = NULL;
129 }
130}
131
Michael Bohane6717fd2012-01-03 16:37:43 -0800132/**
133 * irq_domain_unregister_irq() - Unregister a hwirq within a irq_domain
134 * @domain: ptr to registered irq_domain.
135 * @hwirq: irq_domain hwirq to unregister.
136 */
137void irq_domain_unregister_irq(struct irq_domain *domain, int hwirq)
138{
139 struct irq_data *d;
140
141 /* Clear the irq_domain assignment */
142 d = irq_get_irq_data(irq_domain_to_irq(domain, hwirq));
143 d->domain = NULL;
144}
145
Michael Bohancf68d7a2012-01-11 17:53:05 -0800146/**
147 * irq_domain_find_free_range() - Find an available irq range
148 * @from: lowest logical irq number to request from
149 * @cnt: number of interrupts to search for
150 *
151 * Finds an available logical irq range from the domains specified
152 * on the system. The from parameter can be used to allocate a range
153 * at least as great as the specified irq number.
154 */
155int irq_domain_find_free_range(unsigned int from, unsigned int cnt)
156{
157 struct irq_domain *curr, *prev = NULL;
158
159 if (list_empty(&irq_domain_list))
160 return from;
161
162 list_for_each_entry(curr, &irq_domain_list, list) {
163 if (prev == NULL) {
164 if ((from + cnt - 1) < curr->irq_base)
165 return from;
166 } else {
167 uint32_t p_next_irq = prev->irq_base + prev->nr_irq;
168 uint32_t start_irq;
169 if (from >= curr->irq_base)
170 continue;
171 if (from < p_next_irq)
172 start_irq = p_next_irq;
173 else
174 start_irq = from;
175 if ((curr->irq_base - start_irq) >= cnt)
176 return p_next_irq;
177 }
178 prev = curr;
179 }
180 curr = list_entry(curr->list.prev, struct irq_domain, list);
181
182 return from > curr->irq_base + curr->nr_irq ?
183 from : curr->irq_base + curr->nr_irq;
184}
185
Grant Likely6469dfb2011-07-26 03:19:06 -0600186#if defined(CONFIG_OF_IRQ)
187/**
188 * irq_create_of_mapping() - Map a linux irq number from a DT interrupt spec
189 *
190 * Used by the device tree interrupt mapping code to translate a device tree
191 * interrupt specifier to a valid linux irq number. Returns either a valid
192 * linux IRQ number or 0.
193 *
194 * When the caller no longer need the irq number returned by this function it
195 * should arrange to call irq_dispose_mapping().
196 */
197unsigned int irq_create_of_mapping(struct device_node *controller,
198 const u32 *intspec, unsigned int intsize)
199{
200 struct irq_domain *domain;
201 unsigned long hwirq;
202 unsigned int irq, type;
203 int rc = -EINVAL;
204
205 /* Find a domain which can translate the irq spec */
206 mutex_lock(&irq_domain_mutex);
207 list_for_each_entry(domain, &irq_domain_list, list) {
208 if (!domain->ops->dt_translate)
209 continue;
Michael Bohan6f4df412012-01-11 11:45:16 -0800210
Grant Likely6469dfb2011-07-26 03:19:06 -0600211 rc = domain->ops->dt_translate(domain, controller,
212 intspec, intsize, &hwirq, &type);
213 if (rc == 0)
214 break;
215 }
216 mutex_unlock(&irq_domain_mutex);
217
218 if (rc != 0)
219 return 0;
220
221 irq = irq_domain_to_irq(domain, hwirq);
222 if (type != IRQ_TYPE_NONE)
223 irq_set_irq_type(irq, type);
224 pr_debug("%s: mapped hwirq=%i to irq=%i, flags=%x\n",
225 controller->full_name, (int)hwirq, irq, type);
226 return irq;
227}
228EXPORT_SYMBOL_GPL(irq_create_of_mapping);
229
230/**
231 * irq_dispose_mapping() - Discard a mapping created by irq_create_of_mapping()
232 * @irq: linux irq number to be discarded
233 *
234 * Calling this function indicates the caller no longer needs a reference to
235 * the linux irq number returned by a prior call to irq_create_of_mapping().
236 */
237void irq_dispose_mapping(unsigned int irq)
238{
239 /*
240 * nothing yet; will be filled when support for dynamic allocation of
241 * irq_descs is added to irq_domain
242 */
243}
244EXPORT_SYMBOL_GPL(irq_dispose_mapping);
Grant Likely971a0ee2011-07-26 03:19:06 -0600245
246int irq_domain_simple_dt_translate(struct irq_domain *d,
247 struct device_node *controller,
248 const u32 *intspec, unsigned int intsize,
249 unsigned long *out_hwirq, unsigned int *out_type)
250{
251 if (d->of_node != controller)
252 return -EINVAL;
253 if (intsize < 1)
254 return -EINVAL;
255
256 *out_hwirq = intspec[0];
257 *out_type = IRQ_TYPE_NONE;
258 if (intsize > 1)
259 *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK;
260 return 0;
261}
262
263struct irq_domain_ops irq_domain_simple_ops = {
264 .dt_translate = irq_domain_simple_dt_translate,
265};
266EXPORT_SYMBOL_GPL(irq_domain_simple_ops);
267
268/**
269 * irq_domain_create_simple() - Set up a 'simple' translation range
270 */
271void irq_domain_add_simple(struct device_node *controller, int irq_base)
272{
273 struct irq_domain *domain;
Michael Bohan6f4df412012-01-11 11:45:16 -0800274 int rc;
Grant Likely971a0ee2011-07-26 03:19:06 -0600275
276 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
277 if (!domain) {
278 WARN_ON(1);
279 return;
280 }
281
282 domain->irq_base = irq_base;
283 domain->of_node = of_node_get(controller);
284 domain->ops = &irq_domain_simple_ops;
Michael Bohan6f4df412012-01-11 11:45:16 -0800285 rc = irq_domain_add(domain);
286 if (rc) {
287 WARN(1, "Unable to create irq domain\n");
288 return;
289 }
Michael Bohane6717fd2012-01-03 16:37:43 -0800290 irq_domain_register(domain);
Grant Likely971a0ee2011-07-26 03:19:06 -0600291}
292EXPORT_SYMBOL_GPL(irq_domain_add_simple);
293
294void irq_domain_generate_simple(const struct of_device_id *match,
295 u64 phys_base, unsigned int irq_start)
296{
297 struct device_node *node;
298 pr_info("looking for phys_base=%llx, irq_start=%i\n",
299 (unsigned long long) phys_base, (int) irq_start);
300 node = of_find_matching_node_by_address(NULL, match, phys_base);
301 if (node)
302 irq_domain_add_simple(node, irq_start);
303 else
304 pr_info("no node found\n");
305}
306EXPORT_SYMBOL_GPL(irq_domain_generate_simple);
Grant Likely6469dfb2011-07-26 03:19:06 -0600307#endif /* CONFIG_OF_IRQ */