blob: 1ac8cf41b9a53ee611024bb63801be744dacd365 [file] [log] [blame]
Paul Mundt54a90582012-05-19 15:11:47 +09001#define pr_fmt(fmt) "irq: " fmt
2
Grant Likelycc79ca62012-02-16 01:37:49 -07003#include <linux/debugfs.h>
4#include <linux/hardirq.h>
5#include <linux/interrupt.h>
Grant Likely08a543a2011-07-26 03:19:06 -06006#include <linux/irq.h>
Grant Likelycc79ca62012-02-16 01:37:49 -07007#include <linux/irqdesc.h>
Grant Likely08a543a2011-07-26 03:19:06 -06008#include <linux/irqdomain.h>
9#include <linux/module.h>
10#include <linux/mutex.h>
11#include <linux/of.h>
Grant Likely7e713302011-07-26 03:19:06 -060012#include <linux/of_address.h>
Paul Mundt5ca4db62012-06-03 22:04:34 -070013#include <linux/topology.h>
Grant Likelycc79ca62012-02-16 01:37:49 -070014#include <linux/seq_file.h>
Grant Likely7e713302011-07-26 03:19:06 -060015#include <linux/slab.h>
Grant Likelycc79ca62012-02-16 01:37:49 -070016#include <linux/smp.h>
17#include <linux/fs.h>
Grant Likely08a543a2011-07-26 03:19:06 -060018
19static LIST_HEAD(irq_domain_list);
20static DEFINE_MUTEX(irq_domain_mutex);
21
Grant Likelycc79ca62012-02-16 01:37:49 -070022static DEFINE_MUTEX(revmap_trees_mutex);
Grant Likely68700652012-02-14 14:06:53 -070023static struct irq_domain *irq_default_domain;
Grant Likelycc79ca62012-02-16 01:37:49 -070024
Grant Likelycc79ca62012-02-16 01:37:49 -070025/**
Grant Likelya8db8cf2012-02-14 14:06:54 -070026 * irq_domain_alloc() - Allocate a new irq_domain data structure
Grant Likelycc79ca62012-02-16 01:37:49 -070027 * @of_node: optional device-tree node of the interrupt controller
28 * @revmap_type: type of reverse mapping to use
Grant Likely68700652012-02-14 14:06:53 -070029 * @ops: map/unmap domain callbacks
Grant Likelya8db8cf2012-02-14 14:06:54 -070030 * @host_data: Controller private data pointer
Grant Likelycc79ca62012-02-16 01:37:49 -070031 *
Grant Likelya8db8cf2012-02-14 14:06:54 -070032 * 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.
Grant Likelycc79ca62012-02-16 01:37:49 -070035 */
Grant Likelya8db8cf2012-02-14 14:06:54 -070036static struct irq_domain *irq_domain_alloc(struct device_node *of_node,
37 unsigned int revmap_type,
Grant Likelya18dc812012-01-26 12:12:14 -070038 const struct irq_domain_ops *ops,
Grant Likelya8db8cf2012-02-14 14:06:54 -070039 void *host_data)
Grant Likelycc79ca62012-02-16 01:37:49 -070040{
Grant Likelya8db8cf2012-02-14 14:06:54 -070041 struct irq_domain *domain;
Grant Likelycc79ca62012-02-16 01:37:49 -070042
Paul Mundt5ca4db62012-06-03 22:04:34 -070043 domain = kzalloc_node(sizeof(*domain), GFP_KERNEL,
44 of_node_to_nid(of_node));
Grant Likelya8db8cf2012-02-14 14:06:54 -070045 if (WARN_ON(!domain))
Grant Likelycc79ca62012-02-16 01:37:49 -070046 return NULL;
47
48 /* Fill structure */
Grant Likely68700652012-02-14 14:06:53 -070049 domain->revmap_type = revmap_type;
Grant Likely68700652012-02-14 14:06:53 -070050 domain->ops = ops;
Grant Likelya8db8cf2012-02-14 14:06:54 -070051 domain->host_data = host_data;
Grant Likely68700652012-02-14 14:06:53 -070052 domain->of_node = of_node_get(of_node);
Grant Likelycc79ca62012-02-16 01:37:49 -070053
Grant Likelya8db8cf2012-02-14 14:06:54 -070054 return domain;
55}
56
Paul Mundt58ee99a2012-05-19 15:11:41 +090057static void irq_domain_free(struct irq_domain *domain)
58{
59 of_node_put(domain->of_node);
60 kfree(domain);
61}
62
Grant Likelya8db8cf2012-02-14 14:06:54 -070063static 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);
Paul Mundt54a90582012-05-19 15:11:47 +090068 pr_debug("Allocated domain of type %d @0x%p\n",
Grant Likelya8db8cf2012-02-14 14:06:54 -070069 domain->revmap_type, domain);
70}
71
Paul Mundt58ee99a2012-05-19 15:11:41 +090072/**
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 */
80void irq_domain_remove(struct irq_domain *domain)
81{
82 mutex_lock(&irq_domain_mutex);
83
84 switch (domain->revmap_type) {
Paul Mundt58ee99a2012-05-19 15:11:41 +090085 case IRQ_DOMAIN_MAP_TREE:
86 /*
87 * radix_tree_delete() takes care of destroying the root
88 * node when all entries are removed. Shout if there are
89 * any mappings left.
90 */
91 WARN_ON(domain->revmap_data.tree.height);
92 break;
93 case IRQ_DOMAIN_MAP_LINEAR:
94 kfree(domain->revmap_data.linear.revmap);
95 domain->revmap_data.linear.size = 0;
96 break;
97 case IRQ_DOMAIN_MAP_NOMAP:
98 break;
99 }
100
101 list_del(&domain->link);
102
103 /*
104 * If the going away domain is the default one, reset it.
105 */
106 if (unlikely(irq_default_domain == domain))
107 irq_set_default_host(NULL);
108
109 mutex_unlock(&irq_domain_mutex);
110
Paul Mundt54a90582012-05-19 15:11:47 +0900111 pr_debug("Removed domain of type %d @0x%p\n",
Paul Mundt58ee99a2012-05-19 15:11:41 +0900112 domain->revmap_type, domain);
113
114 irq_domain_free(domain);
115}
Paul Mundtecd84eb2012-05-19 15:11:42 +0900116EXPORT_SYMBOL_GPL(irq_domain_remove);
Paul Mundt58ee99a2012-05-19 15:11:41 +0900117
Grant Likelya8db8cf2012-02-14 14:06:54 -0700118/**
Mark Brown781d0f42012-07-05 12:19:19 +0100119 * irq_domain_add_simple() - Allocate and register a simple irq_domain.
120 * @of_node: pointer to interrupt controller's device tree node.
121 * @size: total number of irqs in mapping
Linus Walleij94a63da2013-06-06 12:10:23 +0100122 * @first_irq: first number of irq block assigned to the domain,
123 * pass zero to assign irqs on-the-fly. This will result in a
124 * linear IRQ domain so it is important to use irq_create_mapping()
125 * for each used IRQ, especially when SPARSE_IRQ is enabled.
Mark Brown781d0f42012-07-05 12:19:19 +0100126 * @ops: map/unmap domain callbacks
127 * @host_data: Controller private data pointer
128 *
129 * Allocates a legacy irq_domain if irq_base is positive or a linear
Linus Walleij2854d162012-09-27 14:59:39 +0200130 * domain otherwise. For the legacy domain, IRQ descriptors will also
131 * be allocated.
Mark Brown781d0f42012-07-05 12:19:19 +0100132 *
133 * This is intended to implement the expected behaviour for most
134 * interrupt controllers which is that a linear mapping should
135 * normally be used unless the system requires a legacy mapping in
136 * order to support supplying interrupt numbers during non-DT
137 * registration of devices.
138 */
139struct irq_domain *irq_domain_add_simple(struct device_node *of_node,
140 unsigned int size,
141 unsigned int first_irq,
142 const struct irq_domain_ops *ops,
143 void *host_data)
144{
Linus Walleij2854d162012-09-27 14:59:39 +0200145 if (first_irq > 0) {
146 int irq_base;
147
148 if (IS_ENABLED(CONFIG_SPARSE_IRQ)) {
149 /*
150 * Set the descriptor allocator to search for a
151 * 1-to-1 mapping, such as irq_alloc_desc_at().
152 * Use of_node_to_nid() which is defined to
153 * numa_node_id() on platforms that have no custom
154 * implementation.
155 */
156 irq_base = irq_alloc_descs(first_irq, first_irq, size,
157 of_node_to_nid(of_node));
158 if (irq_base < 0) {
Linus Walleijd202b7b2012-11-27 01:20:32 +0100159 pr_info("Cannot allocate irq_descs @ IRQ%d, assuming pre-allocated\n",
160 first_irq);
Linus Walleij2854d162012-09-27 14:59:39 +0200161 irq_base = first_irq;
162 }
163 } else
164 irq_base = first_irq;
165
166 return irq_domain_add_legacy(of_node, size, irq_base, 0,
Mark Brown781d0f42012-07-05 12:19:19 +0100167 ops, host_data);
Linus Walleij2854d162012-09-27 14:59:39 +0200168 }
169
170 /* A linear domain is the default */
171 return irq_domain_add_linear(of_node, size, ops, host_data);
Mark Brown781d0f42012-07-05 12:19:19 +0100172}
Arnd Bergmann346dbb72013-04-25 19:28:54 +0200173EXPORT_SYMBOL_GPL(irq_domain_add_simple);
Mark Brown781d0f42012-07-05 12:19:19 +0100174
175/**
Grant Likelya8db8cf2012-02-14 14:06:54 -0700176 * irq_domain_add_legacy() - Allocate and register a legacy revmap irq_domain.
177 * @of_node: pointer to interrupt controller's device tree node.
Grant Likely1bc04f22012-02-14 14:06:55 -0700178 * @size: total number of irqs in legacy mapping
179 * @first_irq: first number of irq block assigned to the domain
180 * @first_hwirq: first hwirq number to use for the translation. Should normally
181 * be '0', but a positive integer can be used if the effective
182 * hwirqs numbering does not begin at zero.
Grant Likelya8db8cf2012-02-14 14:06:54 -0700183 * @ops: map/unmap domain callbacks
184 * @host_data: Controller private data pointer
185 *
186 * Note: the map() callback will be called before this function returns
187 * for all legacy interrupts except 0 (which is always the invalid irq for
188 * a legacy controller).
189 */
190struct irq_domain *irq_domain_add_legacy(struct device_node *of_node,
Grant Likely1bc04f22012-02-14 14:06:55 -0700191 unsigned int size,
192 unsigned int first_irq,
193 irq_hw_number_t first_hwirq,
Grant Likelya18dc812012-01-26 12:12:14 -0700194 const struct irq_domain_ops *ops,
Grant Likelya8db8cf2012-02-14 14:06:54 -0700195 void *host_data)
196{
Grant Likely1bc04f22012-02-14 14:06:55 -0700197 struct irq_domain *domain;
Grant Likelya8db8cf2012-02-14 14:06:54 -0700198
Grant Likely9bbf8772013-06-06 12:10:24 +0100199 pr_debug("Setting up legacy domain virq[%i:%i] ==> hwirq[%i:%i]\n",
200 first_irq, first_irq + size - 1,
201 (int)first_hwirq, (int)first_hwirq + size -1);
202
203 domain = irq_domain_add_linear(of_node, first_hwirq + size, ops, host_data);
Grant Likelya8db8cf2012-02-14 14:06:54 -0700204 if (!domain)
205 return NULL;
206
Grant Likely9bbf8772013-06-06 12:10:24 +0100207 WARN_ON(irq_domain_associate_many(domain, first_irq, first_hwirq, size));
Grant Likely1bc04f22012-02-14 14:06:55 -0700208
Grant Likelya8db8cf2012-02-14 14:06:54 -0700209 return domain;
210}
Paul Mundtecd84eb2012-05-19 15:11:42 +0900211EXPORT_SYMBOL_GPL(irq_domain_add_legacy);
Grant Likelycc79ca62012-02-16 01:37:49 -0700212
Grant Likelya8db8cf2012-02-14 14:06:54 -0700213/**
Dong Aisheng22076c72012-06-20 17:00:30 +0800214 * irq_domain_add_linear() - Allocate and register a linear revmap irq_domain.
Grant Likelya8db8cf2012-02-14 14:06:54 -0700215 * @of_node: pointer to interrupt controller's device tree node.
Mark Browna87487e2012-05-19 12:15:35 +0100216 * @size: Number of interrupts in the domain.
Grant Likelya8db8cf2012-02-14 14:06:54 -0700217 * @ops: map/unmap domain callbacks
218 * @host_data: Controller private data pointer
219 */
220struct irq_domain *irq_domain_add_linear(struct device_node *of_node,
221 unsigned int size,
Grant Likelya18dc812012-01-26 12:12:14 -0700222 const struct irq_domain_ops *ops,
Grant Likelya8db8cf2012-02-14 14:06:54 -0700223 void *host_data)
224{
225 struct irq_domain *domain;
226 unsigned int *revmap;
Grant Likelycc79ca62012-02-16 01:37:49 -0700227
Paul Mundt5ca4db62012-06-03 22:04:34 -0700228 revmap = kzalloc_node(sizeof(*revmap) * size, GFP_KERNEL,
229 of_node_to_nid(of_node));
Grant Likelya8db8cf2012-02-14 14:06:54 -0700230 if (WARN_ON(!revmap))
231 return NULL;
232
233 domain = irq_domain_alloc(of_node, IRQ_DOMAIN_MAP_LINEAR, ops, host_data);
234 if (!domain) {
235 kfree(revmap);
236 return NULL;
237 }
238 domain->revmap_data.linear.size = size;
239 domain->revmap_data.linear.revmap = revmap;
240 irq_domain_add(domain);
241 return domain;
242}
Paul Mundtecd84eb2012-05-19 15:11:42 +0900243EXPORT_SYMBOL_GPL(irq_domain_add_linear);
Grant Likelya8db8cf2012-02-14 14:06:54 -0700244
245struct irq_domain *irq_domain_add_nomap(struct device_node *of_node,
Grant Likely6fa6c8e22012-02-15 15:06:08 -0700246 unsigned int max_irq,
Grant Likelya18dc812012-01-26 12:12:14 -0700247 const struct irq_domain_ops *ops,
Grant Likelya8db8cf2012-02-14 14:06:54 -0700248 void *host_data)
249{
250 struct irq_domain *domain = irq_domain_alloc(of_node,
251 IRQ_DOMAIN_MAP_NOMAP, ops, host_data);
Grant Likely6fa6c8e22012-02-15 15:06:08 -0700252 if (domain) {
253 domain->revmap_data.nomap.max_irq = max_irq ? max_irq : ~0;
Grant Likelya8db8cf2012-02-14 14:06:54 -0700254 irq_domain_add(domain);
Grant Likely6fa6c8e22012-02-15 15:06:08 -0700255 }
Grant Likelya8db8cf2012-02-14 14:06:54 -0700256 return domain;
257}
Paul Mundtecd84eb2012-05-19 15:11:42 +0900258EXPORT_SYMBOL_GPL(irq_domain_add_nomap);
Grant Likelya8db8cf2012-02-14 14:06:54 -0700259
260/**
261 * irq_domain_add_tree()
262 * @of_node: pointer to interrupt controller's device tree node.
263 * @ops: map/unmap domain callbacks
264 *
265 * Note: The radix tree will be allocated later during boot automatically
266 * (the reverse mapping will use the slow path until that happens).
267 */
268struct irq_domain *irq_domain_add_tree(struct device_node *of_node,
Grant Likelya18dc812012-01-26 12:12:14 -0700269 const struct irq_domain_ops *ops,
Grant Likelya8db8cf2012-02-14 14:06:54 -0700270 void *host_data)
271{
272 struct irq_domain *domain = irq_domain_alloc(of_node,
273 IRQ_DOMAIN_MAP_TREE, ops, host_data);
274 if (domain) {
275 INIT_RADIX_TREE(&domain->revmap_data.tree, GFP_KERNEL);
276 irq_domain_add(domain);
277 }
Grant Likely68700652012-02-14 14:06:53 -0700278 return domain;
Grant Likelycc79ca62012-02-16 01:37:49 -0700279}
Paul Mundtecd84eb2012-05-19 15:11:42 +0900280EXPORT_SYMBOL_GPL(irq_domain_add_tree);
Grant Likelycc79ca62012-02-16 01:37:49 -0700281
282/**
283 * irq_find_host() - Locates a domain for a given device node
284 * @node: device-tree node of the interrupt controller
285 */
286struct irq_domain *irq_find_host(struct device_node *node)
287{
288 struct irq_domain *h, *found = NULL;
Grant Likelya18dc812012-01-26 12:12:14 -0700289 int rc;
Grant Likelycc79ca62012-02-16 01:37:49 -0700290
291 /* We might want to match the legacy controller last since
292 * it might potentially be set to match all interrupts in
293 * the absence of a device node. This isn't a problem so far
294 * yet though...
295 */
296 mutex_lock(&irq_domain_mutex);
Grant Likelya18dc812012-01-26 12:12:14 -0700297 list_for_each_entry(h, &irq_domain_list, link) {
298 if (h->ops->match)
299 rc = h->ops->match(h, node);
300 else
301 rc = (h->of_node != NULL) && (h->of_node == node);
302
303 if (rc) {
Grant Likelycc79ca62012-02-16 01:37:49 -0700304 found = h;
305 break;
306 }
Grant Likelya18dc812012-01-26 12:12:14 -0700307 }
Grant Likelycc79ca62012-02-16 01:37:49 -0700308 mutex_unlock(&irq_domain_mutex);
309 return found;
310}
311EXPORT_SYMBOL_GPL(irq_find_host);
312
313/**
314 * irq_set_default_host() - Set a "default" irq domain
Grant Likely68700652012-02-14 14:06:53 -0700315 * @domain: default domain pointer
Grant Likelycc79ca62012-02-16 01:37:49 -0700316 *
317 * For convenience, it's possible to set a "default" domain that will be used
318 * whenever NULL is passed to irq_create_mapping(). It makes life easier for
319 * platforms that want to manipulate a few hard coded interrupt numbers that
320 * aren't properly represented in the device-tree.
321 */
Grant Likely68700652012-02-14 14:06:53 -0700322void irq_set_default_host(struct irq_domain *domain)
Grant Likelycc79ca62012-02-16 01:37:49 -0700323{
Paul Mundt54a90582012-05-19 15:11:47 +0900324 pr_debug("Default domain set to @0x%p\n", domain);
Grant Likelycc79ca62012-02-16 01:37:49 -0700325
Grant Likely68700652012-02-14 14:06:53 -0700326 irq_default_domain = domain;
Grant Likelycc79ca62012-02-16 01:37:49 -0700327}
Paul Mundtecd84eb2012-05-19 15:11:42 +0900328EXPORT_SYMBOL_GPL(irq_set_default_host);
Grant Likelycc79ca62012-02-16 01:37:49 -0700329
Grant Likely913af202012-06-03 22:04:35 -0700330static void irq_domain_disassociate_many(struct irq_domain *domain,
331 unsigned int irq_base, int count)
332{
333 /*
334 * disassociate in reverse order;
335 * not strictly necessary, but nice for unwinding
336 */
337 while (count--) {
338 int irq = irq_base + count;
339 struct irq_data *irq_data = irq_get_irq_data(irq);
Chen Gang275e31b2013-05-14 19:02:45 +0800340 irq_hw_number_t hwirq;
Grant Likely913af202012-06-03 22:04:35 -0700341
342 if (WARN_ON(!irq_data || irq_data->domain != domain))
343 continue;
344
Chen Gang275e31b2013-05-14 19:02:45 +0800345 hwirq = irq_data->hwirq;
Grant Likely913af202012-06-03 22:04:35 -0700346 irq_set_status_flags(irq, IRQ_NOREQUEST);
347
348 /* remove chip and handler */
349 irq_set_chip_and_handler(irq, NULL, NULL);
350
351 /* Make sure it's completed */
352 synchronize_irq(irq);
353
354 /* Tell the PIC about it */
355 if (domain->ops->unmap)
356 domain->ops->unmap(domain, irq);
357 smp_mb();
358
359 irq_data->domain = NULL;
360 irq_data->hwirq = 0;
361
362 /* Clear reverse map */
363 switch(domain->revmap_type) {
364 case IRQ_DOMAIN_MAP_LINEAR:
365 if (hwirq < domain->revmap_data.linear.size)
366 domain->revmap_data.linear.revmap[hwirq] = 0;
367 break;
368 case IRQ_DOMAIN_MAP_TREE:
369 mutex_lock(&revmap_trees_mutex);
370 radix_tree_delete(&domain->revmap_data.tree, hwirq);
371 mutex_unlock(&revmap_trees_mutex);
372 break;
373 }
374 }
375}
376
Grant Likely98aa4682012-06-17 16:17:04 -0600377int irq_domain_associate_many(struct irq_domain *domain, unsigned int irq_base,
378 irq_hw_number_t hwirq_base, int count)
Grant Likelycc79ca62012-02-16 01:37:49 -0700379{
Grant Likely98aa4682012-06-17 16:17:04 -0600380 unsigned int virq = irq_base;
381 irq_hw_number_t hwirq = hwirq_base;
Mark Brownf5a1ad02012-07-20 10:33:19 +0100382 int i, ret;
Grant Likelycc79ca62012-02-16 01:37:49 -0700383
Grant Likely98aa4682012-06-17 16:17:04 -0600384 pr_debug("%s(%s, irqbase=%i, hwbase=%i, count=%i)\n", __func__,
385 of_node_full_name(domain->of_node), irq_base, (int)hwirq_base, count);
386
387 for (i = 0; i < count; i++) {
388 struct irq_data *irq_data = irq_get_irq_data(virq + i);
389
390 if (WARN(!irq_data, "error: irq_desc not allocated; "
391 "irq=%i hwirq=0x%x\n", virq + i, (int)hwirq + i))
392 return -EINVAL;
393 if (WARN(irq_data->domain, "error: irq_desc already associated; "
394 "irq=%i hwirq=0x%x\n", virq + i, (int)hwirq + i))
395 return -EINVAL;
396 };
397
398 for (i = 0; i < count; i++, virq++, hwirq++) {
399 struct irq_data *irq_data = irq_get_irq_data(virq);
400
401 irq_data->hwirq = hwirq;
402 irq_data->domain = domain;
Mark Brownf5a1ad02012-07-20 10:33:19 +0100403 if (domain->ops->map) {
404 ret = domain->ops->map(domain, virq, hwirq);
405 if (ret != 0) {
Benjamin Herrenschmidt5fe0c1f2013-05-06 11:37:43 +1000406 /*
407 * If map() returns -EPERM, this interrupt is protected
408 * by the firmware or some other service and shall not
Grant Likely5e1cda52013-05-29 03:10:53 +0100409 * be mapped. Don't bother telling the user about it.
Benjamin Herrenschmidt5fe0c1f2013-05-06 11:37:43 +1000410 */
411 if (ret != -EPERM) {
Grant Likely5e1cda52013-05-29 03:10:53 +0100412 pr_info("%s didn't like hwirq-0x%lx to VIRQ%i mapping (rc=%d)\n",
413 of_node_full_name(domain->of_node), hwirq, virq, ret);
Benjamin Herrenschmidt5fe0c1f2013-05-06 11:37:43 +1000414 }
Mark Brownf5a1ad02012-07-20 10:33:19 +0100415 irq_data->domain = NULL;
416 irq_data->hwirq = 0;
Grant Likely5e1cda52013-05-29 03:10:53 +0100417 continue;
Mark Brownf5a1ad02012-07-20 10:33:19 +0100418 }
Grant Likely98aa4682012-06-17 16:17:04 -0600419 }
420
421 switch (domain->revmap_type) {
422 case IRQ_DOMAIN_MAP_LINEAR:
423 if (hwirq < domain->revmap_data.linear.size)
424 domain->revmap_data.linear.revmap[hwirq] = virq;
425 break;
426 case IRQ_DOMAIN_MAP_TREE:
427 mutex_lock(&revmap_trees_mutex);
Grant Likelyd6b0d1f2012-06-03 22:04:37 -0700428 radix_tree_insert(&domain->revmap_data.tree, hwirq, irq_data);
Grant Likely98aa4682012-06-17 16:17:04 -0600429 mutex_unlock(&revmap_trees_mutex);
430 break;
431 }
432
433 irq_clear_status_flags(virq, IRQ_NOREQUEST);
Grant Likelycc79ca62012-02-16 01:37:49 -0700434 }
435
Grant Likelycc79ca62012-02-16 01:37:49 -0700436 return 0;
437}
Grant Likely98aa4682012-06-17 16:17:04 -0600438EXPORT_SYMBOL_GPL(irq_domain_associate_many);
Grant Likelycc79ca62012-02-16 01:37:49 -0700439
440/**
441 * irq_create_direct_mapping() - Allocate an irq for direct mapping
Grant Likely68700652012-02-14 14:06:53 -0700442 * @domain: domain to allocate the irq for or NULL for default domain
Grant Likelycc79ca62012-02-16 01:37:49 -0700443 *
444 * This routine is used for irq controllers which can choose the hardware
445 * interrupt numbers they generate. In such a case it's simplest to use
446 * the linux irq as the hardware interrupt number.
447 */
Grant Likely68700652012-02-14 14:06:53 -0700448unsigned int irq_create_direct_mapping(struct irq_domain *domain)
Grant Likelycc79ca62012-02-16 01:37:49 -0700449{
450 unsigned int virq;
451
Grant Likely68700652012-02-14 14:06:53 -0700452 if (domain == NULL)
453 domain = irq_default_domain;
Grant Likelycc79ca62012-02-16 01:37:49 -0700454
Grant Likely9844a552012-06-03 22:04:38 -0700455 if (WARN_ON(!domain || domain->revmap_type != IRQ_DOMAIN_MAP_NOMAP))
456 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700457
Paul Mundt5ca4db62012-06-03 22:04:34 -0700458 virq = irq_alloc_desc_from(1, of_node_to_nid(domain->of_node));
Grant Likely03848372012-02-14 14:06:52 -0700459 if (!virq) {
Paul Mundt54a90582012-05-19 15:11:47 +0900460 pr_debug("create_direct virq allocation failed\n");
Grant Likely03848372012-02-14 14:06:52 -0700461 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700462 }
Grant Likely6fa6c8e22012-02-15 15:06:08 -0700463 if (virq >= domain->revmap_data.nomap.max_irq) {
Grant Likelycc79ca62012-02-16 01:37:49 -0700464 pr_err("ERROR: no free irqs available below %i maximum\n",
Grant Likely6fa6c8e22012-02-15 15:06:08 -0700465 domain->revmap_data.nomap.max_irq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700466 irq_free_desc(virq);
467 return 0;
468 }
Paul Mundt54a90582012-05-19 15:11:47 +0900469 pr_debug("create_direct obtained virq %d\n", virq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700470
Grant Likely98aa4682012-06-17 16:17:04 -0600471 if (irq_domain_associate(domain, virq, virq)) {
Grant Likelycc79ca62012-02-16 01:37:49 -0700472 irq_free_desc(virq);
Grant Likely03848372012-02-14 14:06:52 -0700473 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700474 }
475
476 return virq;
477}
Paul Mundtecd84eb2012-05-19 15:11:42 +0900478EXPORT_SYMBOL_GPL(irq_create_direct_mapping);
Grant Likelycc79ca62012-02-16 01:37:49 -0700479
480/**
481 * irq_create_mapping() - Map a hardware interrupt into linux irq space
Grant Likely68700652012-02-14 14:06:53 -0700482 * @domain: domain owning this hardware interrupt or NULL for default domain
483 * @hwirq: hardware irq number in that domain space
Grant Likelycc79ca62012-02-16 01:37:49 -0700484 *
485 * Only one mapping per hardware interrupt is permitted. Returns a linux
486 * irq number.
487 * If the sense/trigger is to be specified, set_irq_type() should be called
488 * on the number returned from that call.
489 */
Grant Likely68700652012-02-14 14:06:53 -0700490unsigned int irq_create_mapping(struct irq_domain *domain,
Grant Likelycc79ca62012-02-16 01:37:49 -0700491 irq_hw_number_t hwirq)
492{
David Daney5b7526e2012-04-05 16:52:13 -0700493 unsigned int hint;
494 int virq;
Grant Likelycc79ca62012-02-16 01:37:49 -0700495
Paul Mundt54a90582012-05-19 15:11:47 +0900496 pr_debug("irq_create_mapping(0x%p, 0x%lx)\n", domain, hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700497
Grant Likely68700652012-02-14 14:06:53 -0700498 /* Look for default domain if nececssary */
499 if (domain == NULL)
500 domain = irq_default_domain;
501 if (domain == NULL) {
Paul Mundt54a90582012-05-19 15:11:47 +0900502 pr_warning("irq_create_mapping called for"
503 " NULL domain, hwirq=%lx\n", hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700504 WARN_ON(1);
Grant Likely03848372012-02-14 14:06:52 -0700505 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700506 }
Paul Mundt54a90582012-05-19 15:11:47 +0900507 pr_debug("-> using domain @%p\n", domain);
Grant Likelycc79ca62012-02-16 01:37:49 -0700508
509 /* Check if mapping already exists */
Grant Likely68700652012-02-14 14:06:53 -0700510 virq = irq_find_mapping(domain, hwirq);
Grant Likely03848372012-02-14 14:06:52 -0700511 if (virq) {
Paul Mundt54a90582012-05-19 15:11:47 +0900512 pr_debug("-> existing mapping on virq %d\n", virq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700513 return virq;
514 }
515
Grant Likely1bc04f22012-02-14 14:06:55 -0700516 /* Allocate a virtual interrupt number */
Grant Likely6fa6c8e22012-02-15 15:06:08 -0700517 hint = hwirq % nr_irqs;
Grant Likely1bc04f22012-02-14 14:06:55 -0700518 if (hint == 0)
519 hint++;
Paul Mundt5ca4db62012-06-03 22:04:34 -0700520 virq = irq_alloc_desc_from(hint, of_node_to_nid(domain->of_node));
David Daney5b7526e2012-04-05 16:52:13 -0700521 if (virq <= 0)
Paul Mundt5ca4db62012-06-03 22:04:34 -0700522 virq = irq_alloc_desc_from(1, of_node_to_nid(domain->of_node));
David Daney5b7526e2012-04-05 16:52:13 -0700523 if (virq <= 0) {
Paul Mundt54a90582012-05-19 15:11:47 +0900524 pr_debug("-> virq allocation failed\n");
Grant Likely1bc04f22012-02-14 14:06:55 -0700525 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700526 }
527
Grant Likely98aa4682012-06-17 16:17:04 -0600528 if (irq_domain_associate(domain, virq, hwirq)) {
Grant Likely73255702012-06-03 22:04:35 -0700529 irq_free_desc(virq);
Grant Likely03848372012-02-14 14:06:52 -0700530 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700531 }
532
Paul Mundt54a90582012-05-19 15:11:47 +0900533 pr_debug("irq %lu on domain %s mapped to virtual irq %u\n",
Grant Likelyefd68e72012-06-03 22:04:33 -0700534 hwirq, of_node_full_name(domain->of_node), virq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700535
536 return virq;
537}
538EXPORT_SYMBOL_GPL(irq_create_mapping);
539
Grant Likely98aa4682012-06-17 16:17:04 -0600540/**
541 * irq_create_strict_mappings() - Map a range of hw irqs to fixed linux irqs
542 * @domain: domain owning the interrupt range
543 * @irq_base: beginning of linux IRQ range
544 * @hwirq_base: beginning of hardware IRQ range
545 * @count: Number of interrupts to map
546 *
547 * This routine is used for allocating and mapping a range of hardware
548 * irqs to linux irqs where the linux irq numbers are at pre-defined
549 * locations. For use by controllers that already have static mappings
550 * to insert in to the domain.
551 *
552 * Non-linear users can use irq_create_identity_mapping() for IRQ-at-a-time
553 * domain insertion.
554 *
555 * 0 is returned upon success, while any failure to establish a static
556 * mapping is treated as an error.
557 */
558int irq_create_strict_mappings(struct irq_domain *domain, unsigned int irq_base,
559 irq_hw_number_t hwirq_base, int count)
560{
561 int ret;
562
563 ret = irq_alloc_descs(irq_base, irq_base, count,
564 of_node_to_nid(domain->of_node));
565 if (unlikely(ret < 0))
566 return ret;
567
568 ret = irq_domain_associate_many(domain, irq_base, hwirq_base, count);
569 if (unlikely(ret < 0)) {
570 irq_free_descs(irq_base, count);
571 return ret;
572 }
573
574 return 0;
575}
576EXPORT_SYMBOL_GPL(irq_create_strict_mappings);
577
Grant Likelycc79ca62012-02-16 01:37:49 -0700578unsigned int irq_create_of_mapping(struct device_node *controller,
579 const u32 *intspec, unsigned int intsize)
580{
Grant Likely68700652012-02-14 14:06:53 -0700581 struct irq_domain *domain;
Grant Likelycc79ca62012-02-16 01:37:49 -0700582 irq_hw_number_t hwirq;
583 unsigned int type = IRQ_TYPE_NONE;
584 unsigned int virq;
585
Grant Likely68700652012-02-14 14:06:53 -0700586 domain = controller ? irq_find_host(controller) : irq_default_domain;
587 if (!domain) {
Grant Likelyabd23632012-02-24 08:07:06 -0700588#ifdef CONFIG_MIPS
589 /*
590 * Workaround to avoid breaking interrupt controller drivers
591 * that don't yet register an irq_domain. This is temporary
592 * code. ~~~gcl, Feb 24, 2012
593 *
594 * Scheduled for removal in Linux v3.6. That should be enough
595 * time.
596 */
597 if (intsize > 0)
598 return intspec[0];
599#endif
Paul Mundt54a90582012-05-19 15:11:47 +0900600 pr_warning("no irq domain found for %s !\n",
Grant Likelyefd68e72012-06-03 22:04:33 -0700601 of_node_full_name(controller));
Grant Likely03848372012-02-14 14:06:52 -0700602 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700603 }
604
Grant Likely68700652012-02-14 14:06:53 -0700605 /* If domain has no translation, then we assume interrupt line */
606 if (domain->ops->xlate == NULL)
Grant Likelycc79ca62012-02-16 01:37:49 -0700607 hwirq = intspec[0];
608 else {
Grant Likely68700652012-02-14 14:06:53 -0700609 if (domain->ops->xlate(domain, controller, intspec, intsize,
Grant Likelycc79ca62012-02-16 01:37:49 -0700610 &hwirq, &type))
Grant Likely03848372012-02-14 14:06:52 -0700611 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700612 }
613
614 /* Create mapping */
Grant Likely68700652012-02-14 14:06:53 -0700615 virq = irq_create_mapping(domain, hwirq);
Grant Likely03848372012-02-14 14:06:52 -0700616 if (!virq)
Grant Likelycc79ca62012-02-16 01:37:49 -0700617 return virq;
618
619 /* Set type if specified and different than the current one */
620 if (type != IRQ_TYPE_NONE &&
621 type != (irqd_get_trigger_type(irq_get_irq_data(virq))))
622 irq_set_irq_type(virq, type);
623 return virq;
624}
625EXPORT_SYMBOL_GPL(irq_create_of_mapping);
626
627/**
628 * irq_dispose_mapping() - Unmap an interrupt
629 * @virq: linux irq number of the interrupt to unmap
630 */
631void irq_dispose_mapping(unsigned int virq)
632{
633 struct irq_data *irq_data = irq_get_irq_data(virq);
Grant Likely68700652012-02-14 14:06:53 -0700634 struct irq_domain *domain;
Grant Likelycc79ca62012-02-16 01:37:49 -0700635
Grant Likely03848372012-02-14 14:06:52 -0700636 if (!virq || !irq_data)
Grant Likelycc79ca62012-02-16 01:37:49 -0700637 return;
638
Grant Likely68700652012-02-14 14:06:53 -0700639 domain = irq_data->domain;
640 if (WARN_ON(domain == NULL))
Grant Likelycc79ca62012-02-16 01:37:49 -0700641 return;
642
Grant Likely913af202012-06-03 22:04:35 -0700643 irq_domain_disassociate_many(domain, virq, 1);
Grant Likelycc79ca62012-02-16 01:37:49 -0700644 irq_free_desc(virq);
645}
646EXPORT_SYMBOL_GPL(irq_dispose_mapping);
647
648/**
649 * irq_find_mapping() - Find a linux irq from an hw irq number.
Grant Likely68700652012-02-14 14:06:53 -0700650 * @domain: domain owning this hardware interrupt
651 * @hwirq: hardware irq number in that domain space
Grant Likelycc79ca62012-02-16 01:37:49 -0700652 */
Grant Likely68700652012-02-14 14:06:53 -0700653unsigned int irq_find_mapping(struct irq_domain *domain,
Grant Likelycc79ca62012-02-16 01:37:49 -0700654 irq_hw_number_t hwirq)
655{
Grant Likely4c0946c2012-06-03 22:04:39 -0700656 struct irq_data *data;
Grant Likelycc79ca62012-02-16 01:37:49 -0700657
Grant Likely68700652012-02-14 14:06:53 -0700658 /* Look for default domain if nececssary */
659 if (domain == NULL)
660 domain = irq_default_domain;
661 if (domain == NULL)
Grant Likely03848372012-02-14 14:06:52 -0700662 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700663
Grant Likely4c0946c2012-06-03 22:04:39 -0700664 switch (domain->revmap_type) {
Grant Likely4c0946c2012-06-03 22:04:39 -0700665 case IRQ_DOMAIN_MAP_LINEAR:
666 return irq_linear_revmap(domain, hwirq);
667 case IRQ_DOMAIN_MAP_TREE:
668 rcu_read_lock();
669 data = radix_tree_lookup(&domain->revmap_data.tree, hwirq);
670 rcu_read_unlock();
671 if (data)
672 return data->irq;
673 break;
674 case IRQ_DOMAIN_MAP_NOMAP:
675 data = irq_get_irq_data(hwirq);
Grant Likely68700652012-02-14 14:06:53 -0700676 if (data && (data->domain == domain) && (data->hwirq == hwirq))
Grant Likely4c0946c2012-06-03 22:04:39 -0700677 return hwirq;
678 break;
679 }
680
Grant Likely03848372012-02-14 14:06:52 -0700681 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700682}
683EXPORT_SYMBOL_GPL(irq_find_mapping);
684
685/**
Grant Likelycc79ca62012-02-16 01:37:49 -0700686 * irq_linear_revmap() - Find a linux irq from a hw irq number.
Grant Likely68700652012-02-14 14:06:53 -0700687 * @domain: domain owning this hardware interrupt
688 * @hwirq: hardware irq number in that domain space
Grant Likelycc79ca62012-02-16 01:37:49 -0700689 *
Grant Likely4c0946c2012-06-03 22:04:39 -0700690 * This is a fast path that can be called directly by irq controller code to
691 * save a handful of instructions.
Grant Likelycc79ca62012-02-16 01:37:49 -0700692 */
Grant Likely68700652012-02-14 14:06:53 -0700693unsigned int irq_linear_revmap(struct irq_domain *domain,
Grant Likelycc79ca62012-02-16 01:37:49 -0700694 irq_hw_number_t hwirq)
695{
Grant Likely4c0946c2012-06-03 22:04:39 -0700696 BUG_ON(domain->revmap_type != IRQ_DOMAIN_MAP_LINEAR);
Grant Likelycc79ca62012-02-16 01:37:49 -0700697
Grant Likely4c0946c2012-06-03 22:04:39 -0700698 /* Check revmap bounds; complain if exceeded */
699 if (WARN_ON(hwirq >= domain->revmap_data.linear.size))
700 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700701
Grant Likely4c0946c2012-06-03 22:04:39 -0700702 return domain->revmap_data.linear.revmap[hwirq];
Grant Likelycc79ca62012-02-16 01:37:49 -0700703}
Paul Mundtecd84eb2012-05-19 15:11:42 +0900704EXPORT_SYMBOL_GPL(irq_linear_revmap);
Grant Likelycc79ca62012-02-16 01:37:49 -0700705
Grant Likely092b2fb2012-03-29 14:10:30 -0600706#ifdef CONFIG_IRQ_DOMAIN_DEBUG
Grant Likelycc79ca62012-02-16 01:37:49 -0700707static int virq_debug_show(struct seq_file *m, void *private)
708{
709 unsigned long flags;
710 struct irq_desc *desc;
711 const char *p;
712 static const char none[] = "none";
713 void *data;
714 int i;
715
Grant Likely15e06bf2012-04-11 00:26:25 -0600716 seq_printf(m, "%-5s %-7s %-15s %-*s %s\n", "irq", "hwirq",
Grant Likely5269a9a2012-04-12 14:42:15 -0600717 "chip name", (int)(2 * sizeof(void *) + 2), "chip data",
718 "domain name");
Grant Likelycc79ca62012-02-16 01:37:49 -0700719
720 for (i = 1; i < nr_irqs; i++) {
721 desc = irq_to_desc(i);
722 if (!desc)
723 continue;
724
725 raw_spin_lock_irqsave(&desc->lock, flags);
726
727 if (desc->action && desc->action->handler) {
728 struct irq_chip *chip;
729
730 seq_printf(m, "%5d ", i);
731 seq_printf(m, "0x%05lx ", desc->irq_data.hwirq);
732
733 chip = irq_desc_get_chip(desc);
734 if (chip && chip->name)
735 p = chip->name;
736 else
737 p = none;
738 seq_printf(m, "%-15s ", p);
739
740 data = irq_desc_get_chip_data(desc);
Grant Likely15e06bf2012-04-11 00:26:25 -0600741 seq_printf(m, data ? "0x%p " : " %p ", data);
Grant Likelycc79ca62012-02-16 01:37:49 -0700742
Grant Likelyefd68e72012-06-03 22:04:33 -0700743 if (desc->irq_data.domain)
744 p = of_node_full_name(desc->irq_data.domain->of_node);
Grant Likelycc79ca62012-02-16 01:37:49 -0700745 else
746 p = none;
747 seq_printf(m, "%s\n", p);
748 }
749
750 raw_spin_unlock_irqrestore(&desc->lock, flags);
751 }
752
753 return 0;
754}
755
756static int virq_debug_open(struct inode *inode, struct file *file)
757{
758 return single_open(file, virq_debug_show, inode->i_private);
759}
760
761static const struct file_operations virq_debug_fops = {
762 .open = virq_debug_open,
763 .read = seq_read,
764 .llseek = seq_lseek,
765 .release = single_release,
766};
767
768static int __init irq_debugfs_init(void)
769{
Grant Likely092b2fb2012-03-29 14:10:30 -0600770 if (debugfs_create_file("irq_domain_mapping", S_IRUGO, NULL,
Grant Likelycc79ca62012-02-16 01:37:49 -0700771 NULL, &virq_debug_fops) == NULL)
772 return -ENOMEM;
773
774 return 0;
775}
776__initcall(irq_debugfs_init);
Grant Likely092b2fb2012-03-29 14:10:30 -0600777#endif /* CONFIG_IRQ_DOMAIN_DEBUG */
Grant Likelycc79ca62012-02-16 01:37:49 -0700778
Grant Likely16b2e6e2012-01-26 11:26:52 -0700779/**
780 * irq_domain_xlate_onecell() - Generic xlate for direct one cell bindings
781 *
782 * Device Tree IRQ specifier translation function which works with one cell
783 * bindings where the cell value maps directly to the hwirq number.
784 */
785int irq_domain_xlate_onecell(struct irq_domain *d, struct device_node *ctrlr,
786 const u32 *intspec, unsigned int intsize,
787 unsigned long *out_hwirq, unsigned int *out_type)
Grant Likely7e713302011-07-26 03:19:06 -0600788{
Grant Likely16b2e6e2012-01-26 11:26:52 -0700789 if (WARN_ON(intsize < 1))
Grant Likely7e713302011-07-26 03:19:06 -0600790 return -EINVAL;
Grant Likely7e713302011-07-26 03:19:06 -0600791 *out_hwirq = intspec[0];
792 *out_type = IRQ_TYPE_NONE;
Grant Likely7e713302011-07-26 03:19:06 -0600793 return 0;
794}
Grant Likely16b2e6e2012-01-26 11:26:52 -0700795EXPORT_SYMBOL_GPL(irq_domain_xlate_onecell);
796
797/**
798 * irq_domain_xlate_twocell() - Generic xlate for direct two cell bindings
799 *
800 * Device Tree IRQ specifier translation function which works with two cell
801 * bindings where the cell values map directly to the hwirq number
802 * and linux irq flags.
803 */
804int irq_domain_xlate_twocell(struct irq_domain *d, struct device_node *ctrlr,
805 const u32 *intspec, unsigned int intsize,
806 irq_hw_number_t *out_hwirq, unsigned int *out_type)
807{
808 if (WARN_ON(intsize < 2))
809 return -EINVAL;
810 *out_hwirq = intspec[0];
811 *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK;
812 return 0;
813}
814EXPORT_SYMBOL_GPL(irq_domain_xlate_twocell);
815
816/**
817 * irq_domain_xlate_onetwocell() - Generic xlate for one or two cell bindings
818 *
819 * Device Tree IRQ specifier translation function which works with either one
820 * or two cell bindings where the cell values map directly to the hwirq number
821 * and linux irq flags.
822 *
823 * Note: don't use this function unless your interrupt controller explicitly
824 * supports both one and two cell bindings. For the majority of controllers
825 * the _onecell() or _twocell() variants above should be used.
826 */
827int irq_domain_xlate_onetwocell(struct irq_domain *d,
828 struct device_node *ctrlr,
829 const u32 *intspec, unsigned int intsize,
830 unsigned long *out_hwirq, unsigned int *out_type)
831{
832 if (WARN_ON(intsize < 1))
833 return -EINVAL;
834 *out_hwirq = intspec[0];
835 *out_type = (intsize > 1) ? intspec[1] : IRQ_TYPE_NONE;
836 return 0;
837}
838EXPORT_SYMBOL_GPL(irq_domain_xlate_onetwocell);
Grant Likely7e713302011-07-26 03:19:06 -0600839
Grant Likelya18dc812012-01-26 12:12:14 -0700840const struct irq_domain_ops irq_domain_simple_ops = {
Grant Likely16b2e6e2012-01-26 11:26:52 -0700841 .xlate = irq_domain_xlate_onetwocell,
Grant Likely75294952012-02-14 14:06:57 -0700842};
843EXPORT_SYMBOL_GPL(irq_domain_simple_ops);
844
845#ifdef CONFIG_OF_IRQ
Grant Likely7e713302011-07-26 03:19:06 -0600846void irq_domain_generate_simple(const struct of_device_id *match,
847 u64 phys_base, unsigned int irq_start)
848{
849 struct device_node *node;
Grant Likelye1964c52012-02-14 14:06:48 -0700850 pr_debug("looking for phys_base=%llx, irq_start=%i\n",
Grant Likely7e713302011-07-26 03:19:06 -0600851 (unsigned long long) phys_base, (int) irq_start);
852 node = of_find_matching_node_by_address(NULL, match, phys_base);
853 if (node)
Grant Likely6b783f72012-01-10 17:09:30 -0700854 irq_domain_add_legacy(node, 32, irq_start, 0,
855 &irq_domain_simple_ops, NULL);
Grant Likely7e713302011-07-26 03:19:06 -0600856}
857EXPORT_SYMBOL_GPL(irq_domain_generate_simple);
Grant Likely75294952012-02-14 14:06:57 -0700858#endif