Commit | Line | Data |
---|---|---|
54a90588 PM |
1 | #define pr_fmt(fmt) "irq: " fmt |
2 | ||
cc79ca69 GL |
3 | #include <linux/debugfs.h> |
4 | #include <linux/hardirq.h> | |
5 | #include <linux/interrupt.h> | |
08a543ad | 6 | #include <linux/irq.h> |
cc79ca69 | 7 | #include <linux/irqdesc.h> |
08a543ad GL |
8 | #include <linux/irqdomain.h> |
9 | #include <linux/module.h> | |
10 | #include <linux/mutex.h> | |
11 | #include <linux/of.h> | |
7e713301 | 12 | #include <linux/of_address.h> |
5ca4db61 | 13 | #include <linux/topology.h> |
cc79ca69 | 14 | #include <linux/seq_file.h> |
7e713301 | 15 | #include <linux/slab.h> |
cc79ca69 GL |
16 | #include <linux/smp.h> |
17 | #include <linux/fs.h> | |
08a543ad GL |
18 | |
19 | static LIST_HEAD(irq_domain_list); | |
20 | static DEFINE_MUTEX(irq_domain_mutex); | |
21 | ||
cc79ca69 | 22 | static DEFINE_MUTEX(revmap_trees_mutex); |
68700650 | 23 | static struct irq_domain *irq_default_domain; |
cc79ca69 | 24 | |
cc79ca69 | 25 | /** |
a8db8cf0 | 26 | * irq_domain_alloc() - Allocate a new irq_domain data structure |
cc79ca69 GL |
27 | * @of_node: optional device-tree node of the interrupt controller |
28 | * @revmap_type: type of reverse mapping to use | |
68700650 | 29 | * @ops: map/unmap domain callbacks |
a8db8cf0 | 30 | * @host_data: Controller private data pointer |
cc79ca69 | 31 | * |
a8db8cf0 GL |
32 | * Allocates and initialize and irq_domain structure. Caller is expected to |
33 | * register allocated irq_domain with irq_domain_register(). Returns pointer | |
34 | * to IRQ domain, or NULL on failure. | |
cc79ca69 | 35 | */ |
a8db8cf0 GL |
36 | static struct irq_domain *irq_domain_alloc(struct device_node *of_node, |
37 | unsigned int revmap_type, | |
a18dc81b | 38 | const struct irq_domain_ops *ops, |
a8db8cf0 | 39 | void *host_data) |
cc79ca69 | 40 | { |
a8db8cf0 | 41 | struct irq_domain *domain; |
cc79ca69 | 42 | |
5ca4db61 PM |
43 | domain = kzalloc_node(sizeof(*domain), GFP_KERNEL, |
44 | of_node_to_nid(of_node)); | |
a8db8cf0 | 45 | if (WARN_ON(!domain)) |
cc79ca69 GL |
46 | return NULL; |
47 | ||
48 | /* Fill structure */ | |
68700650 | 49 | domain->revmap_type = revmap_type; |
68700650 | 50 | domain->ops = ops; |
a8db8cf0 | 51 | domain->host_data = host_data; |
68700650 | 52 | domain->of_node = of_node_get(of_node); |
cc79ca69 | 53 | |
a8db8cf0 GL |
54 | return domain; |
55 | } | |
56 | ||
58ee99ad PM |
57 | static void irq_domain_free(struct irq_domain *domain) |
58 | { | |
59 | of_node_put(domain->of_node); | |
60 | kfree(domain); | |
61 | } | |
62 | ||
a8db8cf0 GL |
63 | static void irq_domain_add(struct irq_domain *domain) |
64 | { | |
65 | mutex_lock(&irq_domain_mutex); | |
66 | list_add(&domain->link, &irq_domain_list); | |
67 | mutex_unlock(&irq_domain_mutex); | |
54a90588 | 68 | pr_debug("Allocated domain of type %d @0x%p\n", |
a8db8cf0 GL |
69 | domain->revmap_type, domain); |
70 | } | |
71 | ||
58ee99ad PM |
72 | /** |
73 | * irq_domain_remove() - Remove an irq domain. | |
74 | * @domain: domain to remove | |
75 | * | |
76 | * This routine is used to remove an irq domain. The caller must ensure | |
77 | * that all mappings within the domain have been disposed of prior to | |
78 | * use, depending on the revmap type. | |
79 | */ | |
80 | void irq_domain_remove(struct irq_domain *domain) | |
81 | { | |
82 | mutex_lock(&irq_domain_mutex); | |
83 | ||
84 | switch (domain->revmap_type) { | |
85 | case IRQ_DOMAIN_MAP_LEGACY: | |
86 | /* | |
87 | * Legacy domains don't manage their own irq_desc | |
88 | * allocations, we expect the caller to handle irq_desc | |
89 | * freeing on their own. | |
90 | */ | |
91 | break; | |
92 | case IRQ_DOMAIN_MAP_TREE: | |
93 | /* | |
94 | * radix_tree_delete() takes care of destroying the root | |
95 | * node when all entries are removed. Shout if there are | |
96 | * any mappings left. | |
97 | */ | |
98 | WARN_ON(domain->revmap_data.tree.height); | |
99 | break; | |
100 | case IRQ_DOMAIN_MAP_LINEAR: | |
101 | kfree(domain->revmap_data.linear.revmap); | |
102 | domain->revmap_data.linear.size = 0; | |
103 | break; | |
104 | case IRQ_DOMAIN_MAP_NOMAP: | |
105 | break; | |
106 | } | |
107 | ||
108 | list_del(&domain->link); | |
109 | ||
110 | /* | |
111 | * If the going away domain is the default one, reset it. | |
112 | */ | |
113 | if (unlikely(irq_default_domain == domain)) | |
114 | irq_set_default_host(NULL); | |
115 | ||
116 | mutex_unlock(&irq_domain_mutex); | |
117 | ||
54a90588 | 118 | pr_debug("Removed domain of type %d @0x%p\n", |
58ee99ad PM |
119 | domain->revmap_type, domain); |
120 | ||
121 | irq_domain_free(domain); | |
122 | } | |
ecd84eb2 | 123 | EXPORT_SYMBOL_GPL(irq_domain_remove); |
58ee99ad | 124 | |
1bc04f2c GL |
125 | static unsigned int irq_domain_legacy_revmap(struct irq_domain *domain, |
126 | irq_hw_number_t hwirq) | |
127 | { | |
128 | irq_hw_number_t first_hwirq = domain->revmap_data.legacy.first_hwirq; | |
129 | int size = domain->revmap_data.legacy.size; | |
130 | ||
131 | if (WARN_ON(hwirq < first_hwirq || hwirq >= first_hwirq + size)) | |
132 | return 0; | |
133 | return hwirq - first_hwirq + domain->revmap_data.legacy.first_irq; | |
134 | } | |
135 | ||
781d0f46 MB |
136 | /** |
137 | * irq_domain_add_simple() - Allocate and register a simple irq_domain. | |
138 | * @of_node: pointer to interrupt controller's device tree node. | |
139 | * @size: total number of irqs in mapping | |
140 | * @first_irq: first number of irq block assigned to the domain | |
141 | * @ops: map/unmap domain callbacks | |
142 | * @host_data: Controller private data pointer | |
143 | * | |
144 | * Allocates a legacy irq_domain if irq_base is positive or a linear | |
2854d167 LW |
145 | * domain otherwise. For the legacy domain, IRQ descriptors will also |
146 | * be allocated. | |
781d0f46 MB |
147 | * |
148 | * This is intended to implement the expected behaviour for most | |
149 | * interrupt controllers which is that a linear mapping should | |
150 | * normally be used unless the system requires a legacy mapping in | |
151 | * order to support supplying interrupt numbers during non-DT | |
152 | * registration of devices. | |
153 | */ | |
154 | struct irq_domain *irq_domain_add_simple(struct device_node *of_node, | |
155 | unsigned int size, | |
156 | unsigned int first_irq, | |
157 | const struct irq_domain_ops *ops, | |
158 | void *host_data) | |
159 | { | |
2854d167 LW |
160 | if (first_irq > 0) { |
161 | int irq_base; | |
162 | ||
163 | if (IS_ENABLED(CONFIG_SPARSE_IRQ)) { | |
164 | /* | |
165 | * Set the descriptor allocator to search for a | |
166 | * 1-to-1 mapping, such as irq_alloc_desc_at(). | |
167 | * Use of_node_to_nid() which is defined to | |
168 | * numa_node_id() on platforms that have no custom | |
169 | * implementation. | |
170 | */ | |
171 | irq_base = irq_alloc_descs(first_irq, first_irq, size, | |
172 | of_node_to_nid(of_node)); | |
173 | if (irq_base < 0) { | |
d202b7b9 LW |
174 | pr_info("Cannot allocate irq_descs @ IRQ%d, assuming pre-allocated\n", |
175 | first_irq); | |
2854d167 LW |
176 | irq_base = first_irq; |
177 | } | |
178 | } else | |
179 | irq_base = first_irq; | |
180 | ||
181 | return irq_domain_add_legacy(of_node, size, irq_base, 0, | |
781d0f46 | 182 | ops, host_data); |
2854d167 LW |
183 | } |
184 | ||
185 | /* A linear domain is the default */ | |
186 | return irq_domain_add_linear(of_node, size, ops, host_data); | |
781d0f46 MB |
187 | } |
188 | ||
a8db8cf0 GL |
189 | /** |
190 | * irq_domain_add_legacy() - Allocate and register a legacy revmap irq_domain. | |
191 | * @of_node: pointer to interrupt controller's device tree node. | |
1bc04f2c GL |
192 | * @size: total number of irqs in legacy mapping |
193 | * @first_irq: first number of irq block assigned to the domain | |
194 | * @first_hwirq: first hwirq number to use for the translation. Should normally | |
195 | * be '0', but a positive integer can be used if the effective | |
196 | * hwirqs numbering does not begin at zero. | |
a8db8cf0 GL |
197 | * @ops: map/unmap domain callbacks |
198 | * @host_data: Controller private data pointer | |
199 | * | |
200 | * Note: the map() callback will be called before this function returns | |
201 | * for all legacy interrupts except 0 (which is always the invalid irq for | |
202 | * a legacy controller). | |
203 | */ | |
204 | struct irq_domain *irq_domain_add_legacy(struct device_node *of_node, | |
1bc04f2c GL |
205 | unsigned int size, |
206 | unsigned int first_irq, | |
207 | irq_hw_number_t first_hwirq, | |
a18dc81b | 208 | const struct irq_domain_ops *ops, |
a8db8cf0 GL |
209 | void *host_data) |
210 | { | |
1bc04f2c | 211 | struct irq_domain *domain; |
a8db8cf0 GL |
212 | unsigned int i; |
213 | ||
214 | domain = irq_domain_alloc(of_node, IRQ_DOMAIN_MAP_LEGACY, ops, host_data); | |
215 | if (!domain) | |
216 | return NULL; | |
217 | ||
1bc04f2c GL |
218 | domain->revmap_data.legacy.first_irq = first_irq; |
219 | domain->revmap_data.legacy.first_hwirq = first_hwirq; | |
220 | domain->revmap_data.legacy.size = size; | |
221 | ||
cc79ca69 | 222 | mutex_lock(&irq_domain_mutex); |
1bc04f2c GL |
223 | /* Verify that all the irqs are available */ |
224 | for (i = 0; i < size; i++) { | |
225 | int irq = first_irq + i; | |
226 | struct irq_data *irq_data = irq_get_irq_data(irq); | |
227 | ||
228 | if (WARN_ON(!irq_data || irq_data->domain)) { | |
a8db8cf0 | 229 | mutex_unlock(&irq_domain_mutex); |
58ee99ad | 230 | irq_domain_free(domain); |
a8db8cf0 | 231 | return NULL; |
cc79ca69 GL |
232 | } |
233 | } | |
cc79ca69 | 234 | |
1bc04f2c GL |
235 | /* Claim all of the irqs before registering a legacy domain */ |
236 | for (i = 0; i < size; i++) { | |
237 | struct irq_data *irq_data = irq_get_irq_data(first_irq + i); | |
238 | irq_data->hwirq = first_hwirq + i; | |
a8db8cf0 | 239 | irq_data->domain = domain; |
1bc04f2c GL |
240 | } |
241 | mutex_unlock(&irq_domain_mutex); | |
242 | ||
243 | for (i = 0; i < size; i++) { | |
244 | int irq = first_irq + i; | |
245 | int hwirq = first_hwirq + i; | |
246 | ||
247 | /* IRQ0 gets ignored */ | |
248 | if (!irq) | |
249 | continue; | |
a8db8cf0 GL |
250 | |
251 | /* Legacy flags are left to default at this point, | |
252 | * one can then use irq_create_mapping() to | |
253 | * explicitly change them | |
254 | */ | |
aed98048 GL |
255 | if (ops->map) |
256 | ops->map(domain, irq, hwirq); | |
a8db8cf0 GL |
257 | |
258 | /* Clear norequest flags */ | |
1bc04f2c | 259 | irq_clear_status_flags(irq, IRQ_NOREQUEST); |
cc79ca69 | 260 | } |
1bc04f2c GL |
261 | |
262 | irq_domain_add(domain); | |
a8db8cf0 GL |
263 | return domain; |
264 | } | |
ecd84eb2 | 265 | EXPORT_SYMBOL_GPL(irq_domain_add_legacy); |
a8db8cf0 GL |
266 | |
267 | /** | |
22076c77 | 268 | * irq_domain_add_linear() - Allocate and register a linear revmap irq_domain. |
a8db8cf0 | 269 | * @of_node: pointer to interrupt controller's device tree node. |
a87487e6 | 270 | * @size: Number of interrupts in the domain. |
a8db8cf0 GL |
271 | * @ops: map/unmap domain callbacks |
272 | * @host_data: Controller private data pointer | |
273 | */ | |
274 | struct irq_domain *irq_domain_add_linear(struct device_node *of_node, | |
275 | unsigned int size, | |
a18dc81b | 276 | const struct irq_domain_ops *ops, |
a8db8cf0 GL |
277 | void *host_data) |
278 | { | |
279 | struct irq_domain *domain; | |
280 | unsigned int *revmap; | |
cc79ca69 | 281 | |
5ca4db61 PM |
282 | revmap = kzalloc_node(sizeof(*revmap) * size, GFP_KERNEL, |
283 | of_node_to_nid(of_node)); | |
a8db8cf0 GL |
284 | if (WARN_ON(!revmap)) |
285 | return NULL; | |
cc79ca69 | 286 | |
a8db8cf0 GL |
287 | domain = irq_domain_alloc(of_node, IRQ_DOMAIN_MAP_LINEAR, ops, host_data); |
288 | if (!domain) { | |
289 | kfree(revmap); | |
290 | return NULL; | |
291 | } | |
292 | domain->revmap_data.linear.size = size; | |
293 | domain->revmap_data.linear.revmap = revmap; | |
294 | irq_domain_add(domain); | |
295 | return domain; | |
296 | } | |
ecd84eb2 | 297 | EXPORT_SYMBOL_GPL(irq_domain_add_linear); |
a8db8cf0 GL |
298 | |
299 | struct irq_domain *irq_domain_add_nomap(struct device_node *of_node, | |
6fa6c8e2 | 300 | unsigned int max_irq, |
a18dc81b | 301 | const struct irq_domain_ops *ops, |
a8db8cf0 GL |
302 | void *host_data) |
303 | { | |
304 | struct irq_domain *domain = irq_domain_alloc(of_node, | |
305 | IRQ_DOMAIN_MAP_NOMAP, ops, host_data); | |
6fa6c8e2 GL |
306 | if (domain) { |
307 | domain->revmap_data.nomap.max_irq = max_irq ? max_irq : ~0; | |
a8db8cf0 | 308 | irq_domain_add(domain); |
6fa6c8e2 | 309 | } |
a8db8cf0 GL |
310 | return domain; |
311 | } | |
ecd84eb2 | 312 | EXPORT_SYMBOL_GPL(irq_domain_add_nomap); |
a8db8cf0 GL |
313 | |
314 | /** | |
315 | * irq_domain_add_tree() | |
316 | * @of_node: pointer to interrupt controller's device tree node. | |
317 | * @ops: map/unmap domain callbacks | |
318 | * | |
319 | * Note: The radix tree will be allocated later during boot automatically | |
320 | * (the reverse mapping will use the slow path until that happens). | |
321 | */ | |
322 | struct irq_domain *irq_domain_add_tree(struct device_node *of_node, | |
a18dc81b | 323 | const struct irq_domain_ops *ops, |
a8db8cf0 GL |
324 | void *host_data) |
325 | { | |
326 | struct irq_domain *domain = irq_domain_alloc(of_node, | |
327 | IRQ_DOMAIN_MAP_TREE, ops, host_data); | |
328 | if (domain) { | |
329 | INIT_RADIX_TREE(&domain->revmap_data.tree, GFP_KERNEL); | |
330 | irq_domain_add(domain); | |
331 | } | |
68700650 | 332 | return domain; |
cc79ca69 | 333 | } |
ecd84eb2 | 334 | EXPORT_SYMBOL_GPL(irq_domain_add_tree); |
cc79ca69 GL |
335 | |
336 | /** | |
337 | * irq_find_host() - Locates a domain for a given device node | |
338 | * @node: device-tree node of the interrupt controller | |
339 | */ | |
340 | struct irq_domain *irq_find_host(struct device_node *node) | |
341 | { | |
342 | struct irq_domain *h, *found = NULL; | |
a18dc81b | 343 | int rc; |
cc79ca69 GL |
344 | |
345 | /* We might want to match the legacy controller last since | |
346 | * it might potentially be set to match all interrupts in | |
347 | * the absence of a device node. This isn't a problem so far | |
348 | * yet though... | |
349 | */ | |
350 | mutex_lock(&irq_domain_mutex); | |
a18dc81b GL |
351 | list_for_each_entry(h, &irq_domain_list, link) { |
352 | if (h->ops->match) | |
353 | rc = h->ops->match(h, node); | |
354 | else | |
355 | rc = (h->of_node != NULL) && (h->of_node == node); | |
356 | ||
357 | if (rc) { | |
cc79ca69 GL |
358 | found = h; |
359 | break; | |
360 | } | |
a18dc81b | 361 | } |
cc79ca69 GL |
362 | mutex_unlock(&irq_domain_mutex); |
363 | return found; | |
364 | } | |
365 | EXPORT_SYMBOL_GPL(irq_find_host); | |
366 | ||
367 | /** | |
368 | * irq_set_default_host() - Set a "default" irq domain | |
68700650 | 369 | * @domain: default domain pointer |
cc79ca69 GL |
370 | * |
371 | * For convenience, it's possible to set a "default" domain that will be used | |
372 | * whenever NULL is passed to irq_create_mapping(). It makes life easier for | |
373 | * platforms that want to manipulate a few hard coded interrupt numbers that | |
374 | * aren't properly represented in the device-tree. | |
375 | */ | |
68700650 | 376 | void irq_set_default_host(struct irq_domain *domain) |
cc79ca69 | 377 | { |
54a90588 | 378 | pr_debug("Default domain set to @0x%p\n", domain); |
cc79ca69 | 379 | |
68700650 | 380 | irq_default_domain = domain; |
cc79ca69 | 381 | } |
ecd84eb2 | 382 | EXPORT_SYMBOL_GPL(irq_set_default_host); |
cc79ca69 | 383 | |
913af207 GL |
384 | static void irq_domain_disassociate_many(struct irq_domain *domain, |
385 | unsigned int irq_base, int count) | |
386 | { | |
387 | /* | |
388 | * disassociate in reverse order; | |
389 | * not strictly necessary, but nice for unwinding | |
390 | */ | |
391 | while (count--) { | |
392 | int irq = irq_base + count; | |
393 | struct irq_data *irq_data = irq_get_irq_data(irq); | |
394 | irq_hw_number_t hwirq = irq_data->hwirq; | |
395 | ||
396 | if (WARN_ON(!irq_data || irq_data->domain != domain)) | |
397 | continue; | |
398 | ||
399 | irq_set_status_flags(irq, IRQ_NOREQUEST); | |
400 | ||
401 | /* remove chip and handler */ | |
402 | irq_set_chip_and_handler(irq, NULL, NULL); | |
403 | ||
404 | /* Make sure it's completed */ | |
405 | synchronize_irq(irq); | |
406 | ||
407 | /* Tell the PIC about it */ | |
408 | if (domain->ops->unmap) | |
409 | domain->ops->unmap(domain, irq); | |
410 | smp_mb(); | |
411 | ||
412 | irq_data->domain = NULL; | |
413 | irq_data->hwirq = 0; | |
414 | ||
415 | /* Clear reverse map */ | |
416 | switch(domain->revmap_type) { | |
417 | case IRQ_DOMAIN_MAP_LINEAR: | |
418 | if (hwirq < domain->revmap_data.linear.size) | |
419 | domain->revmap_data.linear.revmap[hwirq] = 0; | |
420 | break; | |
421 | case IRQ_DOMAIN_MAP_TREE: | |
422 | mutex_lock(&revmap_trees_mutex); | |
423 | radix_tree_delete(&domain->revmap_data.tree, hwirq); | |
424 | mutex_unlock(&revmap_trees_mutex); | |
425 | break; | |
426 | } | |
427 | } | |
428 | } | |
429 | ||
98aa468e GL |
430 | int irq_domain_associate_many(struct irq_domain *domain, unsigned int irq_base, |
431 | irq_hw_number_t hwirq_base, int count) | |
cc79ca69 | 432 | { |
98aa468e GL |
433 | unsigned int virq = irq_base; |
434 | irq_hw_number_t hwirq = hwirq_base; | |
f5a1ad05 | 435 | int i, ret; |
cc79ca69 | 436 | |
98aa468e GL |
437 | pr_debug("%s(%s, irqbase=%i, hwbase=%i, count=%i)\n", __func__, |
438 | of_node_full_name(domain->of_node), irq_base, (int)hwirq_base, count); | |
cc79ca69 | 439 | |
98aa468e GL |
440 | for (i = 0; i < count; i++) { |
441 | struct irq_data *irq_data = irq_get_irq_data(virq + i); | |
442 | ||
443 | if (WARN(!irq_data, "error: irq_desc not allocated; " | |
444 | "irq=%i hwirq=0x%x\n", virq + i, (int)hwirq + i)) | |
445 | return -EINVAL; | |
446 | if (WARN(irq_data->domain, "error: irq_desc already associated; " | |
447 | "irq=%i hwirq=0x%x\n", virq + i, (int)hwirq + i)) | |
448 | return -EINVAL; | |
449 | }; | |
450 | ||
451 | for (i = 0; i < count; i++, virq++, hwirq++) { | |
452 | struct irq_data *irq_data = irq_get_irq_data(virq); | |
453 | ||
454 | irq_data->hwirq = hwirq; | |
455 | irq_data->domain = domain; | |
f5a1ad05 MB |
456 | if (domain->ops->map) { |
457 | ret = domain->ops->map(domain, virq, hwirq); | |
458 | if (ret != 0) { | |
5fe0c1f2 BH |
459 | /* |
460 | * If map() returns -EPERM, this interrupt is protected | |
461 | * by the firmware or some other service and shall not | |
462 | * be mapped. | |
463 | * | |
464 | * Since on some platforms we blindly try to map everything | |
465 | * we end up with a log full of backtraces. | |
466 | * | |
467 | * So instead, we silently fail on -EPERM, it is the | |
468 | * responsibility of the PIC driver to display a relevant | |
469 | * message if needed. | |
470 | */ | |
471 | if (ret != -EPERM) { | |
472 | pr_err("irq-%i==>hwirq-0x%lx mapping failed: %d\n", | |
473 | virq, hwirq, ret); | |
474 | WARN_ON(1); | |
475 | } | |
f5a1ad05 MB |
476 | irq_data->domain = NULL; |
477 | irq_data->hwirq = 0; | |
478 | goto err_unmap; | |
479 | } | |
98aa468e GL |
480 | } |
481 | ||
482 | switch (domain->revmap_type) { | |
483 | case IRQ_DOMAIN_MAP_LINEAR: | |
484 | if (hwirq < domain->revmap_data.linear.size) | |
485 | domain->revmap_data.linear.revmap[hwirq] = virq; | |
486 | break; | |
487 | case IRQ_DOMAIN_MAP_TREE: | |
488 | mutex_lock(&revmap_trees_mutex); | |
d6b0d1f7 | 489 | radix_tree_insert(&domain->revmap_data.tree, hwirq, irq_data); |
98aa468e GL |
490 | mutex_unlock(&revmap_trees_mutex); |
491 | break; | |
492 | } | |
2a71a1a9 | 493 | |
98aa468e GL |
494 | irq_clear_status_flags(virq, IRQ_NOREQUEST); |
495 | } | |
cc79ca69 GL |
496 | |
497 | return 0; | |
98aa468e GL |
498 | |
499 | err_unmap: | |
500 | irq_domain_disassociate_many(domain, irq_base, i); | |
501 | return -EINVAL; | |
cc79ca69 | 502 | } |
98aa468e | 503 | EXPORT_SYMBOL_GPL(irq_domain_associate_many); |
cc79ca69 GL |
504 | |
505 | /** | |
506 | * irq_create_direct_mapping() - Allocate an irq for direct mapping | |
68700650 | 507 | * @domain: domain to allocate the irq for or NULL for default domain |
cc79ca69 GL |
508 | * |
509 | * This routine is used for irq controllers which can choose the hardware | |
510 | * interrupt numbers they generate. In such a case it's simplest to use | |
511 | * the linux irq as the hardware interrupt number. | |
512 | */ | |
68700650 | 513 | unsigned int irq_create_direct_mapping(struct irq_domain *domain) |
cc79ca69 GL |
514 | { |
515 | unsigned int virq; | |
516 | ||
68700650 GL |
517 | if (domain == NULL) |
518 | domain = irq_default_domain; | |
cc79ca69 | 519 | |
9844a552 GL |
520 | if (WARN_ON(!domain || domain->revmap_type != IRQ_DOMAIN_MAP_NOMAP)) |
521 | return 0; | |
cc79ca69 | 522 | |
5ca4db61 | 523 | virq = irq_alloc_desc_from(1, of_node_to_nid(domain->of_node)); |
03848373 | 524 | if (!virq) { |
54a90588 | 525 | pr_debug("create_direct virq allocation failed\n"); |
03848373 | 526 | return 0; |
cc79ca69 | 527 | } |
6fa6c8e2 | 528 | if (virq >= domain->revmap_data.nomap.max_irq) { |
cc79ca69 | 529 | pr_err("ERROR: no free irqs available below %i maximum\n", |
6fa6c8e2 | 530 | domain->revmap_data.nomap.max_irq); |
cc79ca69 GL |
531 | irq_free_desc(virq); |
532 | return 0; | |
533 | } | |
54a90588 | 534 | pr_debug("create_direct obtained virq %d\n", virq); |
cc79ca69 | 535 | |
98aa468e | 536 | if (irq_domain_associate(domain, virq, virq)) { |
cc79ca69 | 537 | irq_free_desc(virq); |
03848373 | 538 | return 0; |
cc79ca69 GL |
539 | } |
540 | ||
541 | return virq; | |
542 | } | |
ecd84eb2 | 543 | EXPORT_SYMBOL_GPL(irq_create_direct_mapping); |
cc79ca69 GL |
544 | |
545 | /** | |
546 | * irq_create_mapping() - Map a hardware interrupt into linux irq space | |
68700650 GL |
547 | * @domain: domain owning this hardware interrupt or NULL for default domain |
548 | * @hwirq: hardware irq number in that domain space | |
cc79ca69 GL |
549 | * |
550 | * Only one mapping per hardware interrupt is permitted. Returns a linux | |
551 | * irq number. | |
552 | * If the sense/trigger is to be specified, set_irq_type() should be called | |
553 | * on the number returned from that call. | |
554 | */ | |
68700650 | 555 | unsigned int irq_create_mapping(struct irq_domain *domain, |
cc79ca69 GL |
556 | irq_hw_number_t hwirq) |
557 | { | |
5b7526e3 DD |
558 | unsigned int hint; |
559 | int virq; | |
cc79ca69 | 560 | |
54a90588 | 561 | pr_debug("irq_create_mapping(0x%p, 0x%lx)\n", domain, hwirq); |
cc79ca69 | 562 | |
68700650 GL |
563 | /* Look for default domain if nececssary */ |
564 | if (domain == NULL) | |
565 | domain = irq_default_domain; | |
566 | if (domain == NULL) { | |
54a90588 PM |
567 | pr_warning("irq_create_mapping called for" |
568 | " NULL domain, hwirq=%lx\n", hwirq); | |
cc79ca69 | 569 | WARN_ON(1); |
03848373 | 570 | return 0; |
cc79ca69 | 571 | } |
54a90588 | 572 | pr_debug("-> using domain @%p\n", domain); |
cc79ca69 GL |
573 | |
574 | /* Check if mapping already exists */ | |
68700650 | 575 | virq = irq_find_mapping(domain, hwirq); |
03848373 | 576 | if (virq) { |
54a90588 | 577 | pr_debug("-> existing mapping on virq %d\n", virq); |
cc79ca69 GL |
578 | return virq; |
579 | } | |
580 | ||
581 | /* Get a virtual interrupt number */ | |
1bc04f2c GL |
582 | if (domain->revmap_type == IRQ_DOMAIN_MAP_LEGACY) |
583 | return irq_domain_legacy_revmap(domain, hwirq); | |
584 | ||
585 | /* Allocate a virtual interrupt number */ | |
6fa6c8e2 | 586 | hint = hwirq % nr_irqs; |
1bc04f2c GL |
587 | if (hint == 0) |
588 | hint++; | |
5ca4db61 | 589 | virq = irq_alloc_desc_from(hint, of_node_to_nid(domain->of_node)); |
5b7526e3 | 590 | if (virq <= 0) |
5ca4db61 | 591 | virq = irq_alloc_desc_from(1, of_node_to_nid(domain->of_node)); |
5b7526e3 | 592 | if (virq <= 0) { |
54a90588 | 593 | pr_debug("-> virq allocation failed\n"); |
1bc04f2c | 594 | return 0; |
cc79ca69 GL |
595 | } |
596 | ||
98aa468e | 597 | if (irq_domain_associate(domain, virq, hwirq)) { |
73255704 | 598 | irq_free_desc(virq); |
03848373 | 599 | return 0; |
cc79ca69 GL |
600 | } |
601 | ||
54a90588 | 602 | pr_debug("irq %lu on domain %s mapped to virtual irq %u\n", |
efd68e72 | 603 | hwirq, of_node_full_name(domain->of_node), virq); |
cc79ca69 GL |
604 | |
605 | return virq; | |
606 | } | |
607 | EXPORT_SYMBOL_GPL(irq_create_mapping); | |
608 | ||
98aa468e GL |
609 | /** |
610 | * irq_create_strict_mappings() - Map a range of hw irqs to fixed linux irqs | |
611 | * @domain: domain owning the interrupt range | |
612 | * @irq_base: beginning of linux IRQ range | |
613 | * @hwirq_base: beginning of hardware IRQ range | |
614 | * @count: Number of interrupts to map | |
615 | * | |
616 | * This routine is used for allocating and mapping a range of hardware | |
617 | * irqs to linux irqs where the linux irq numbers are at pre-defined | |
618 | * locations. For use by controllers that already have static mappings | |
619 | * to insert in to the domain. | |
620 | * | |
621 | * Non-linear users can use irq_create_identity_mapping() for IRQ-at-a-time | |
622 | * domain insertion. | |
623 | * | |
624 | * 0 is returned upon success, while any failure to establish a static | |
625 | * mapping is treated as an error. | |
626 | */ | |
627 | int irq_create_strict_mappings(struct irq_domain *domain, unsigned int irq_base, | |
628 | irq_hw_number_t hwirq_base, int count) | |
629 | { | |
630 | int ret; | |
631 | ||
632 | ret = irq_alloc_descs(irq_base, irq_base, count, | |
633 | of_node_to_nid(domain->of_node)); | |
634 | if (unlikely(ret < 0)) | |
635 | return ret; | |
636 | ||
637 | ret = irq_domain_associate_many(domain, irq_base, hwirq_base, count); | |
638 | if (unlikely(ret < 0)) { | |
639 | irq_free_descs(irq_base, count); | |
640 | return ret; | |
641 | } | |
642 | ||
643 | return 0; | |
644 | } | |
645 | EXPORT_SYMBOL_GPL(irq_create_strict_mappings); | |
646 | ||
cc79ca69 GL |
647 | unsigned int irq_create_of_mapping(struct device_node *controller, |
648 | const u32 *intspec, unsigned int intsize) | |
649 | { | |
68700650 | 650 | struct irq_domain *domain; |
cc79ca69 GL |
651 | irq_hw_number_t hwirq; |
652 | unsigned int type = IRQ_TYPE_NONE; | |
653 | unsigned int virq; | |
654 | ||
68700650 GL |
655 | domain = controller ? irq_find_host(controller) : irq_default_domain; |
656 | if (!domain) { | |
abd2363f GL |
657 | #ifdef CONFIG_MIPS |
658 | /* | |
659 | * Workaround to avoid breaking interrupt controller drivers | |
660 | * that don't yet register an irq_domain. This is temporary | |
661 | * code. ~~~gcl, Feb 24, 2012 | |
662 | * | |
663 | * Scheduled for removal in Linux v3.6. That should be enough | |
664 | * time. | |
665 | */ | |
666 | if (intsize > 0) | |
667 | return intspec[0]; | |
668 | #endif | |
54a90588 | 669 | pr_warning("no irq domain found for %s !\n", |
efd68e72 | 670 | of_node_full_name(controller)); |
03848373 | 671 | return 0; |
cc79ca69 GL |
672 | } |
673 | ||
68700650 GL |
674 | /* If domain has no translation, then we assume interrupt line */ |
675 | if (domain->ops->xlate == NULL) | |
cc79ca69 GL |
676 | hwirq = intspec[0]; |
677 | else { | |
68700650 | 678 | if (domain->ops->xlate(domain, controller, intspec, intsize, |
cc79ca69 | 679 | &hwirq, &type)) |
03848373 | 680 | return 0; |
cc79ca69 GL |
681 | } |
682 | ||
683 | /* Create mapping */ | |
68700650 | 684 | virq = irq_create_mapping(domain, hwirq); |
03848373 | 685 | if (!virq) |
cc79ca69 GL |
686 | return virq; |
687 | ||
688 | /* Set type if specified and different than the current one */ | |
689 | if (type != IRQ_TYPE_NONE && | |
690 | type != (irqd_get_trigger_type(irq_get_irq_data(virq)))) | |
691 | irq_set_irq_type(virq, type); | |
692 | return virq; | |
693 | } | |
694 | EXPORT_SYMBOL_GPL(irq_create_of_mapping); | |
695 | ||
696 | /** | |
697 | * irq_dispose_mapping() - Unmap an interrupt | |
698 | * @virq: linux irq number of the interrupt to unmap | |
699 | */ | |
700 | void irq_dispose_mapping(unsigned int virq) | |
701 | { | |
702 | struct irq_data *irq_data = irq_get_irq_data(virq); | |
68700650 | 703 | struct irq_domain *domain; |
cc79ca69 | 704 | |
03848373 | 705 | if (!virq || !irq_data) |
cc79ca69 GL |
706 | return; |
707 | ||
68700650 GL |
708 | domain = irq_data->domain; |
709 | if (WARN_ON(domain == NULL)) | |
cc79ca69 GL |
710 | return; |
711 | ||
712 | /* Never unmap legacy interrupts */ | |
68700650 | 713 | if (domain->revmap_type == IRQ_DOMAIN_MAP_LEGACY) |
cc79ca69 GL |
714 | return; |
715 | ||
913af207 | 716 | irq_domain_disassociate_many(domain, virq, 1); |
cc79ca69 GL |
717 | irq_free_desc(virq); |
718 | } | |
719 | EXPORT_SYMBOL_GPL(irq_dispose_mapping); | |
720 | ||
721 | /** | |
722 | * irq_find_mapping() - Find a linux irq from an hw irq number. | |
68700650 GL |
723 | * @domain: domain owning this hardware interrupt |
724 | * @hwirq: hardware irq number in that domain space | |
cc79ca69 | 725 | */ |
68700650 | 726 | unsigned int irq_find_mapping(struct irq_domain *domain, |
cc79ca69 GL |
727 | irq_hw_number_t hwirq) |
728 | { | |
4c0946c4 | 729 | struct irq_data *data; |
cc79ca69 | 730 | |
68700650 GL |
731 | /* Look for default domain if nececssary */ |
732 | if (domain == NULL) | |
733 | domain = irq_default_domain; | |
734 | if (domain == NULL) | |
03848373 | 735 | return 0; |
cc79ca69 | 736 | |
4c0946c4 GL |
737 | switch (domain->revmap_type) { |
738 | case IRQ_DOMAIN_MAP_LEGACY: | |
1bc04f2c | 739 | return irq_domain_legacy_revmap(domain, hwirq); |
4c0946c4 GL |
740 | case IRQ_DOMAIN_MAP_LINEAR: |
741 | return irq_linear_revmap(domain, hwirq); | |
742 | case IRQ_DOMAIN_MAP_TREE: | |
743 | rcu_read_lock(); | |
744 | data = radix_tree_lookup(&domain->revmap_data.tree, hwirq); | |
745 | rcu_read_unlock(); | |
746 | if (data) | |
747 | return data->irq; | |
748 | break; | |
749 | case IRQ_DOMAIN_MAP_NOMAP: | |
750 | data = irq_get_irq_data(hwirq); | |
68700650 | 751 | if (data && (data->domain == domain) && (data->hwirq == hwirq)) |
4c0946c4 GL |
752 | return hwirq; |
753 | break; | |
754 | } | |
755 | ||
03848373 | 756 | return 0; |
cc79ca69 GL |
757 | } |
758 | EXPORT_SYMBOL_GPL(irq_find_mapping); | |
759 | ||
cc79ca69 GL |
760 | /** |
761 | * irq_linear_revmap() - Find a linux irq from a hw irq number. | |
68700650 GL |
762 | * @domain: domain owning this hardware interrupt |
763 | * @hwirq: hardware irq number in that domain space | |
cc79ca69 | 764 | * |
4c0946c4 GL |
765 | * This is a fast path that can be called directly by irq controller code to |
766 | * save a handful of instructions. | |
cc79ca69 | 767 | */ |
68700650 | 768 | unsigned int irq_linear_revmap(struct irq_domain *domain, |
cc79ca69 GL |
769 | irq_hw_number_t hwirq) |
770 | { | |
4c0946c4 | 771 | BUG_ON(domain->revmap_type != IRQ_DOMAIN_MAP_LINEAR); |
cc79ca69 | 772 | |
4c0946c4 GL |
773 | /* Check revmap bounds; complain if exceeded */ |
774 | if (WARN_ON(hwirq >= domain->revmap_data.linear.size)) | |
775 | return 0; | |
cc79ca69 | 776 | |
4c0946c4 | 777 | return domain->revmap_data.linear.revmap[hwirq]; |
cc79ca69 | 778 | } |
ecd84eb2 | 779 | EXPORT_SYMBOL_GPL(irq_linear_revmap); |
cc79ca69 | 780 | |
092b2fb0 | 781 | #ifdef CONFIG_IRQ_DOMAIN_DEBUG |
cc79ca69 GL |
782 | static int virq_debug_show(struct seq_file *m, void *private) |
783 | { | |
784 | unsigned long flags; | |
785 | struct irq_desc *desc; | |
786 | const char *p; | |
787 | static const char none[] = "none"; | |
788 | void *data; | |
789 | int i; | |
790 | ||
15e06bf6 | 791 | seq_printf(m, "%-5s %-7s %-15s %-*s %s\n", "irq", "hwirq", |
5269a9ab GL |
792 | "chip name", (int)(2 * sizeof(void *) + 2), "chip data", |
793 | "domain name"); | |
cc79ca69 GL |
794 | |
795 | for (i = 1; i < nr_irqs; i++) { | |
796 | desc = irq_to_desc(i); | |
797 | if (!desc) | |
798 | continue; | |
799 | ||
800 | raw_spin_lock_irqsave(&desc->lock, flags); | |
801 | ||
802 | if (desc->action && desc->action->handler) { | |
803 | struct irq_chip *chip; | |
804 | ||
805 | seq_printf(m, "%5d ", i); | |
806 | seq_printf(m, "0x%05lx ", desc->irq_data.hwirq); | |
807 | ||
808 | chip = irq_desc_get_chip(desc); | |
809 | if (chip && chip->name) | |
810 | p = chip->name; | |
811 | else | |
812 | p = none; | |
813 | seq_printf(m, "%-15s ", p); | |
814 | ||
815 | data = irq_desc_get_chip_data(desc); | |
15e06bf6 | 816 | seq_printf(m, data ? "0x%p " : " %p ", data); |
cc79ca69 | 817 | |
efd68e72 GL |
818 | if (desc->irq_data.domain) |
819 | p = of_node_full_name(desc->irq_data.domain->of_node); | |
cc79ca69 GL |
820 | else |
821 | p = none; | |
822 | seq_printf(m, "%s\n", p); | |
823 | } | |
824 | ||
825 | raw_spin_unlock_irqrestore(&desc->lock, flags); | |
826 | } | |
827 | ||
828 | return 0; | |
829 | } | |
830 | ||
831 | static int virq_debug_open(struct inode *inode, struct file *file) | |
832 | { | |
833 | return single_open(file, virq_debug_show, inode->i_private); | |
834 | } | |
835 | ||
836 | static const struct file_operations virq_debug_fops = { | |
837 | .open = virq_debug_open, | |
838 | .read = seq_read, | |
839 | .llseek = seq_lseek, | |
840 | .release = single_release, | |
841 | }; | |
842 | ||
843 | static int __init irq_debugfs_init(void) | |
844 | { | |
092b2fb0 | 845 | if (debugfs_create_file("irq_domain_mapping", S_IRUGO, NULL, |
cc79ca69 GL |
846 | NULL, &virq_debug_fops) == NULL) |
847 | return -ENOMEM; | |
848 | ||
849 | return 0; | |
850 | } | |
851 | __initcall(irq_debugfs_init); | |
092b2fb0 | 852 | #endif /* CONFIG_IRQ_DOMAIN_DEBUG */ |
cc79ca69 | 853 | |
16b2e6e2 GL |
854 | /** |
855 | * irq_domain_xlate_onecell() - Generic xlate for direct one cell bindings | |
856 | * | |
857 | * Device Tree IRQ specifier translation function which works with one cell | |
858 | * bindings where the cell value maps directly to the hwirq number. | |
859 | */ | |
860 | int irq_domain_xlate_onecell(struct irq_domain *d, struct device_node *ctrlr, | |
861 | const u32 *intspec, unsigned int intsize, | |
862 | unsigned long *out_hwirq, unsigned int *out_type) | |
7e713301 | 863 | { |
16b2e6e2 | 864 | if (WARN_ON(intsize < 1)) |
7e713301 | 865 | return -EINVAL; |
7e713301 GL |
866 | *out_hwirq = intspec[0]; |
867 | *out_type = IRQ_TYPE_NONE; | |
7e713301 GL |
868 | return 0; |
869 | } | |
16b2e6e2 GL |
870 | EXPORT_SYMBOL_GPL(irq_domain_xlate_onecell); |
871 | ||
872 | /** | |
873 | * irq_domain_xlate_twocell() - Generic xlate for direct two cell bindings | |
874 | * | |
875 | * Device Tree IRQ specifier translation function which works with two cell | |
876 | * bindings where the cell values map directly to the hwirq number | |
877 | * and linux irq flags. | |
878 | */ | |
879 | int irq_domain_xlate_twocell(struct irq_domain *d, struct device_node *ctrlr, | |
880 | const u32 *intspec, unsigned int intsize, | |
881 | irq_hw_number_t *out_hwirq, unsigned int *out_type) | |
882 | { | |
883 | if (WARN_ON(intsize < 2)) | |
884 | return -EINVAL; | |
885 | *out_hwirq = intspec[0]; | |
886 | *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK; | |
887 | return 0; | |
888 | } | |
889 | EXPORT_SYMBOL_GPL(irq_domain_xlate_twocell); | |
890 | ||
891 | /** | |
892 | * irq_domain_xlate_onetwocell() - Generic xlate for one or two cell bindings | |
893 | * | |
894 | * Device Tree IRQ specifier translation function which works with either one | |
895 | * or two cell bindings where the cell values map directly to the hwirq number | |
896 | * and linux irq flags. | |
897 | * | |
898 | * Note: don't use this function unless your interrupt controller explicitly | |
899 | * supports both one and two cell bindings. For the majority of controllers | |
900 | * the _onecell() or _twocell() variants above should be used. | |
901 | */ | |
902 | int irq_domain_xlate_onetwocell(struct irq_domain *d, | |
903 | struct device_node *ctrlr, | |
904 | const u32 *intspec, unsigned int intsize, | |
905 | unsigned long *out_hwirq, unsigned int *out_type) | |
906 | { | |
907 | if (WARN_ON(intsize < 1)) | |
908 | return -EINVAL; | |
909 | *out_hwirq = intspec[0]; | |
910 | *out_type = (intsize > 1) ? intspec[1] : IRQ_TYPE_NONE; | |
911 | return 0; | |
912 | } | |
913 | EXPORT_SYMBOL_GPL(irq_domain_xlate_onetwocell); | |
7e713301 | 914 | |
a18dc81b | 915 | const struct irq_domain_ops irq_domain_simple_ops = { |
16b2e6e2 | 916 | .xlate = irq_domain_xlate_onetwocell, |
75294957 GL |
917 | }; |
918 | EXPORT_SYMBOL_GPL(irq_domain_simple_ops); | |
919 | ||
920 | #ifdef CONFIG_OF_IRQ | |
7e713301 GL |
921 | void irq_domain_generate_simple(const struct of_device_id *match, |
922 | u64 phys_base, unsigned int irq_start) | |
923 | { | |
924 | struct device_node *node; | |
e1964c50 | 925 | pr_debug("looking for phys_base=%llx, irq_start=%i\n", |
7e713301 GL |
926 | (unsigned long long) phys_base, (int) irq_start); |
927 | node = of_find_matching_node_by_address(NULL, match, phys_base); | |
928 | if (node) | |
6b783f7c GL |
929 | irq_domain_add_legacy(node, 32, irq_start, 0, |
930 | &irq_domain_simple_ops, NULL); | |
7e713301 GL |
931 | } |
932 | EXPORT_SYMBOL_GPL(irq_domain_generate_simple); | |
75294957 | 933 | #endif |