blob: 5a1d8ec8509e176d1eb9f7f0c0ab9c3d161f0200 [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,
Grant Likelycef50752012-07-11 17:24:31 +010037 unsigned int revmap_type, int size,
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
Grant Likelycef50752012-07-11 17:24:31 +010043 domain = kzalloc_node(sizeof(*domain) + (sizeof(unsigned int) * size),
44 GFP_KERNEL, 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 Likelycef50752012-07-11 17:24:31 +010049 INIT_RADIX_TREE(&domain->revmap_data.tree, GFP_KERNEL);
Grant Likely68700652012-02-14 14:06:53 -070050 domain->revmap_type = revmap_type;
Grant Likely68700652012-02-14 14:06:53 -070051 domain->ops = ops;
Grant Likelya8db8cf2012-02-14 14:06:54 -070052 domain->host_data = host_data;
Grant Likely68700652012-02-14 14:06:53 -070053 domain->of_node = of_node_get(of_node);
Grant Likelycef50752012-07-11 17:24:31 +010054 domain->revmap_data.linear.size = size;
Grant Likelycc79ca62012-02-16 01:37:49 -070055
Grant Likelya8db8cf2012-02-14 14:06:54 -070056 return domain;
57}
58
Paul Mundt58ee99a2012-05-19 15:11:41 +090059static void irq_domain_free(struct irq_domain *domain)
60{
61 of_node_put(domain->of_node);
62 kfree(domain);
63}
64
Grant Likelya8db8cf2012-02-14 14:06:54 -070065static void irq_domain_add(struct irq_domain *domain)
66{
67 mutex_lock(&irq_domain_mutex);
68 list_add(&domain->link, &irq_domain_list);
69 mutex_unlock(&irq_domain_mutex);
Paul Mundt54a90582012-05-19 15:11:47 +090070 pr_debug("Allocated domain of type %d @0x%p\n",
Grant Likelya8db8cf2012-02-14 14:06:54 -070071 domain->revmap_type, domain);
72}
73
Paul Mundt58ee99a2012-05-19 15:11:41 +090074/**
75 * irq_domain_remove() - Remove an irq domain.
76 * @domain: domain to remove
77 *
78 * This routine is used to remove an irq domain. The caller must ensure
79 * that all mappings within the domain have been disposed of prior to
80 * use, depending on the revmap type.
81 */
82void irq_domain_remove(struct irq_domain *domain)
83{
84 mutex_lock(&irq_domain_mutex);
85
Grant Likelycef50752012-07-11 17:24:31 +010086 /*
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);
Paul Mundt58ee99a2012-05-19 15:11:41 +090092
93 list_del(&domain->link);
94
95 /*
96 * If the going away domain is the default one, reset it.
97 */
98 if (unlikely(irq_default_domain == domain))
99 irq_set_default_host(NULL);
100
101 mutex_unlock(&irq_domain_mutex);
102
Paul Mundt54a90582012-05-19 15:11:47 +0900103 pr_debug("Removed domain of type %d @0x%p\n",
Paul Mundt58ee99a2012-05-19 15:11:41 +0900104 domain->revmap_type, domain);
105
106 irq_domain_free(domain);
107}
Paul Mundtecd84eb2012-05-19 15:11:42 +0900108EXPORT_SYMBOL_GPL(irq_domain_remove);
Paul Mundt58ee99a2012-05-19 15:11:41 +0900109
Grant Likelya8db8cf2012-02-14 14:06:54 -0700110/**
Mark Brown781d0f42012-07-05 12:19:19 +0100111 * irq_domain_add_simple() - Allocate and register a simple irq_domain.
112 * @of_node: pointer to interrupt controller's device tree node.
113 * @size: total number of irqs in mapping
Linus Walleij94a63da2013-06-06 12:10:23 +0100114 * @first_irq: first number of irq block assigned to the domain,
115 * pass zero to assign irqs on-the-fly. This will result in a
116 * linear IRQ domain so it is important to use irq_create_mapping()
117 * for each used IRQ, especially when SPARSE_IRQ is enabled.
Mark Brown781d0f42012-07-05 12:19:19 +0100118 * @ops: map/unmap domain callbacks
119 * @host_data: Controller private data pointer
120 *
121 * Allocates a legacy irq_domain if irq_base is positive or a linear
Linus Walleij2854d162012-09-27 14:59:39 +0200122 * domain otherwise. For the legacy domain, IRQ descriptors will also
123 * be allocated.
Mark Brown781d0f42012-07-05 12:19:19 +0100124 *
125 * This is intended to implement the expected behaviour for most
126 * interrupt controllers which is that a linear mapping should
127 * normally be used unless the system requires a legacy mapping in
128 * order to support supplying interrupt numbers during non-DT
129 * registration of devices.
130 */
131struct irq_domain *irq_domain_add_simple(struct device_node *of_node,
132 unsigned int size,
133 unsigned int first_irq,
134 const struct irq_domain_ops *ops,
135 void *host_data)
136{
Linus Walleij2854d162012-09-27 14:59:39 +0200137 if (first_irq > 0) {
138 int irq_base;
139
140 if (IS_ENABLED(CONFIG_SPARSE_IRQ)) {
141 /*
142 * Set the descriptor allocator to search for a
143 * 1-to-1 mapping, such as irq_alloc_desc_at().
144 * Use of_node_to_nid() which is defined to
145 * numa_node_id() on platforms that have no custom
146 * implementation.
147 */
148 irq_base = irq_alloc_descs(first_irq, first_irq, size,
149 of_node_to_nid(of_node));
150 if (irq_base < 0) {
Linus Walleijd202b7b2012-11-27 01:20:32 +0100151 pr_info("Cannot allocate irq_descs @ IRQ%d, assuming pre-allocated\n",
152 first_irq);
Linus Walleij2854d162012-09-27 14:59:39 +0200153 irq_base = first_irq;
154 }
155 } else
156 irq_base = first_irq;
157
158 return irq_domain_add_legacy(of_node, size, irq_base, 0,
Mark Brown781d0f42012-07-05 12:19:19 +0100159 ops, host_data);
Linus Walleij2854d162012-09-27 14:59:39 +0200160 }
161
162 /* A linear domain is the default */
163 return irq_domain_add_linear(of_node, size, ops, host_data);
Mark Brown781d0f42012-07-05 12:19:19 +0100164}
Arnd Bergmann346dbb72013-04-25 19:28:54 +0200165EXPORT_SYMBOL_GPL(irq_domain_add_simple);
Mark Brown781d0f42012-07-05 12:19:19 +0100166
167/**
Grant Likelya8db8cf2012-02-14 14:06:54 -0700168 * irq_domain_add_legacy() - Allocate and register a legacy revmap irq_domain.
169 * @of_node: pointer to interrupt controller's device tree node.
Grant Likely1bc04f22012-02-14 14:06:55 -0700170 * @size: total number of irqs in legacy mapping
171 * @first_irq: first number of irq block assigned to the domain
172 * @first_hwirq: first hwirq number to use for the translation. Should normally
173 * be '0', but a positive integer can be used if the effective
174 * hwirqs numbering does not begin at zero.
Grant Likelya8db8cf2012-02-14 14:06:54 -0700175 * @ops: map/unmap domain callbacks
176 * @host_data: Controller private data pointer
177 *
178 * Note: the map() callback will be called before this function returns
179 * for all legacy interrupts except 0 (which is always the invalid irq for
180 * a legacy controller).
181 */
182struct irq_domain *irq_domain_add_legacy(struct device_node *of_node,
Grant Likely1bc04f22012-02-14 14:06:55 -0700183 unsigned int size,
184 unsigned int first_irq,
185 irq_hw_number_t first_hwirq,
Grant Likelya18dc812012-01-26 12:12:14 -0700186 const struct irq_domain_ops *ops,
Grant Likelya8db8cf2012-02-14 14:06:54 -0700187 void *host_data)
188{
Grant Likely1bc04f22012-02-14 14:06:55 -0700189 struct irq_domain *domain;
Grant Likelya8db8cf2012-02-14 14:06:54 -0700190
Grant Likely9bbf8772013-06-06 12:10:24 +0100191 pr_debug("Setting up legacy domain virq[%i:%i] ==> hwirq[%i:%i]\n",
192 first_irq, first_irq + size - 1,
193 (int)first_hwirq, (int)first_hwirq + size -1);
194
195 domain = irq_domain_add_linear(of_node, first_hwirq + size, ops, host_data);
Grant Likelya8db8cf2012-02-14 14:06:54 -0700196 if (!domain)
197 return NULL;
198
Grant Likely9bbf8772013-06-06 12:10:24 +0100199 WARN_ON(irq_domain_associate_many(domain, first_irq, first_hwirq, size));
Grant Likely1bc04f22012-02-14 14:06:55 -0700200
Grant Likelya8db8cf2012-02-14 14:06:54 -0700201 return domain;
202}
Paul Mundtecd84eb2012-05-19 15:11:42 +0900203EXPORT_SYMBOL_GPL(irq_domain_add_legacy);
Grant Likelycc79ca62012-02-16 01:37:49 -0700204
Grant Likelya8db8cf2012-02-14 14:06:54 -0700205/**
Dong Aisheng22076c72012-06-20 17:00:30 +0800206 * irq_domain_add_linear() - Allocate and register a linear revmap irq_domain.
Grant Likelya8db8cf2012-02-14 14:06:54 -0700207 * @of_node: pointer to interrupt controller's device tree node.
Mark Browna87487e2012-05-19 12:15:35 +0100208 * @size: Number of interrupts in the domain.
Grant Likelya8db8cf2012-02-14 14:06:54 -0700209 * @ops: map/unmap domain callbacks
210 * @host_data: Controller private data pointer
211 */
212struct irq_domain *irq_domain_add_linear(struct device_node *of_node,
213 unsigned int size,
Grant Likelya18dc812012-01-26 12:12:14 -0700214 const struct irq_domain_ops *ops,
Grant Likelya8db8cf2012-02-14 14:06:54 -0700215 void *host_data)
216{
217 struct irq_domain *domain;
Grant Likelycc79ca62012-02-16 01:37:49 -0700218
Grant Likelycef50752012-07-11 17:24:31 +0100219 domain = irq_domain_alloc(of_node, IRQ_DOMAIN_MAP_LINEAR, size, ops, host_data);
220 if (!domain)
Grant Likelya8db8cf2012-02-14 14:06:54 -0700221 return NULL;
222
Grant Likelya8db8cf2012-02-14 14:06:54 -0700223 irq_domain_add(domain);
224 return domain;
225}
Paul Mundtecd84eb2012-05-19 15:11:42 +0900226EXPORT_SYMBOL_GPL(irq_domain_add_linear);
Grant Likelya8db8cf2012-02-14 14:06:54 -0700227
228struct irq_domain *irq_domain_add_nomap(struct device_node *of_node,
Grant Likely6fa6c8e22012-02-15 15:06:08 -0700229 unsigned int max_irq,
Grant Likelya18dc812012-01-26 12:12:14 -0700230 const struct irq_domain_ops *ops,
Grant Likelya8db8cf2012-02-14 14:06:54 -0700231 void *host_data)
232{
233 struct irq_domain *domain = irq_domain_alloc(of_node,
Grant Likelycef50752012-07-11 17:24:31 +0100234 IRQ_DOMAIN_MAP_NOMAP, 0, ops, host_data);
Grant Likely6fa6c8e22012-02-15 15:06:08 -0700235 if (domain) {
236 domain->revmap_data.nomap.max_irq = max_irq ? max_irq : ~0;
Grant Likelya8db8cf2012-02-14 14:06:54 -0700237 irq_domain_add(domain);
Grant Likely6fa6c8e22012-02-15 15:06:08 -0700238 }
Grant Likelya8db8cf2012-02-14 14:06:54 -0700239 return domain;
240}
Paul Mundtecd84eb2012-05-19 15:11:42 +0900241EXPORT_SYMBOL_GPL(irq_domain_add_nomap);
Grant Likelya8db8cf2012-02-14 14:06:54 -0700242
243/**
Grant Likelycc79ca62012-02-16 01:37:49 -0700244 * irq_find_host() - Locates a domain for a given device node
245 * @node: device-tree node of the interrupt controller
246 */
247struct irq_domain *irq_find_host(struct device_node *node)
248{
249 struct irq_domain *h, *found = NULL;
Grant Likelya18dc812012-01-26 12:12:14 -0700250 int rc;
Grant Likelycc79ca62012-02-16 01:37:49 -0700251
252 /* We might want to match the legacy controller last since
253 * it might potentially be set to match all interrupts in
254 * the absence of a device node. This isn't a problem so far
255 * yet though...
256 */
257 mutex_lock(&irq_domain_mutex);
Grant Likelya18dc812012-01-26 12:12:14 -0700258 list_for_each_entry(h, &irq_domain_list, link) {
259 if (h->ops->match)
260 rc = h->ops->match(h, node);
261 else
262 rc = (h->of_node != NULL) && (h->of_node == node);
263
264 if (rc) {
Grant Likelycc79ca62012-02-16 01:37:49 -0700265 found = h;
266 break;
267 }
Grant Likelya18dc812012-01-26 12:12:14 -0700268 }
Grant Likelycc79ca62012-02-16 01:37:49 -0700269 mutex_unlock(&irq_domain_mutex);
270 return found;
271}
272EXPORT_SYMBOL_GPL(irq_find_host);
273
274/**
275 * irq_set_default_host() - Set a "default" irq domain
Grant Likely68700652012-02-14 14:06:53 -0700276 * @domain: default domain pointer
Grant Likelycc79ca62012-02-16 01:37:49 -0700277 *
278 * For convenience, it's possible to set a "default" domain that will be used
279 * whenever NULL is passed to irq_create_mapping(). It makes life easier for
280 * platforms that want to manipulate a few hard coded interrupt numbers that
281 * aren't properly represented in the device-tree.
282 */
Grant Likely68700652012-02-14 14:06:53 -0700283void irq_set_default_host(struct irq_domain *domain)
Grant Likelycc79ca62012-02-16 01:37:49 -0700284{
Paul Mundt54a90582012-05-19 15:11:47 +0900285 pr_debug("Default domain set to @0x%p\n", domain);
Grant Likelycc79ca62012-02-16 01:37:49 -0700286
Grant Likely68700652012-02-14 14:06:53 -0700287 irq_default_domain = domain;
Grant Likelycc79ca62012-02-16 01:37:49 -0700288}
Paul Mundtecd84eb2012-05-19 15:11:42 +0900289EXPORT_SYMBOL_GPL(irq_set_default_host);
Grant Likelycc79ca62012-02-16 01:37:49 -0700290
Grant Likely913af202012-06-03 22:04:35 -0700291static void irq_domain_disassociate_many(struct irq_domain *domain,
292 unsigned int irq_base, int count)
293{
294 /*
295 * disassociate in reverse order;
296 * not strictly necessary, but nice for unwinding
297 */
298 while (count--) {
299 int irq = irq_base + count;
300 struct irq_data *irq_data = irq_get_irq_data(irq);
Chen Gang275e31b2013-05-14 19:02:45 +0800301 irq_hw_number_t hwirq;
Grant Likely913af202012-06-03 22:04:35 -0700302
303 if (WARN_ON(!irq_data || irq_data->domain != domain))
304 continue;
305
Chen Gang275e31b2013-05-14 19:02:45 +0800306 hwirq = irq_data->hwirq;
Grant Likely913af202012-06-03 22:04:35 -0700307 irq_set_status_flags(irq, IRQ_NOREQUEST);
308
309 /* remove chip and handler */
310 irq_set_chip_and_handler(irq, NULL, NULL);
311
312 /* Make sure it's completed */
313 synchronize_irq(irq);
314
315 /* Tell the PIC about it */
316 if (domain->ops->unmap)
317 domain->ops->unmap(domain, irq);
318 smp_mb();
319
320 irq_data->domain = NULL;
321 irq_data->hwirq = 0;
322
Grant Likelycef50752012-07-11 17:24:31 +0100323 /* Clear reverse map for this hwirq */
324 if (hwirq < domain->revmap_data.linear.size) {
325 domain->linear_revmap[hwirq] = 0;
326 } else {
Grant Likely913af202012-06-03 22:04:35 -0700327 mutex_lock(&revmap_trees_mutex);
328 radix_tree_delete(&domain->revmap_data.tree, hwirq);
329 mutex_unlock(&revmap_trees_mutex);
Grant Likely913af202012-06-03 22:04:35 -0700330 }
331 }
332}
333
Grant Likely98aa4682012-06-17 16:17:04 -0600334int irq_domain_associate_many(struct irq_domain *domain, unsigned int irq_base,
335 irq_hw_number_t hwirq_base, int count)
Grant Likelycc79ca62012-02-16 01:37:49 -0700336{
Grant Likely98aa4682012-06-17 16:17:04 -0600337 unsigned int virq = irq_base;
338 irq_hw_number_t hwirq = hwirq_base;
Mark Brownf5a1ad02012-07-20 10:33:19 +0100339 int i, ret;
Grant Likelycc79ca62012-02-16 01:37:49 -0700340
Grant Likely98aa4682012-06-17 16:17:04 -0600341 pr_debug("%s(%s, irqbase=%i, hwbase=%i, count=%i)\n", __func__,
342 of_node_full_name(domain->of_node), irq_base, (int)hwirq_base, count);
343
344 for (i = 0; i < count; i++) {
345 struct irq_data *irq_data = irq_get_irq_data(virq + i);
346
347 if (WARN(!irq_data, "error: irq_desc not allocated; "
348 "irq=%i hwirq=0x%x\n", virq + i, (int)hwirq + i))
349 return -EINVAL;
350 if (WARN(irq_data->domain, "error: irq_desc already associated; "
351 "irq=%i hwirq=0x%x\n", virq + i, (int)hwirq + i))
352 return -EINVAL;
353 };
354
355 for (i = 0; i < count; i++, virq++, hwirq++) {
356 struct irq_data *irq_data = irq_get_irq_data(virq);
357
358 irq_data->hwirq = hwirq;
359 irq_data->domain = domain;
Mark Brownf5a1ad02012-07-20 10:33:19 +0100360 if (domain->ops->map) {
361 ret = domain->ops->map(domain, virq, hwirq);
362 if (ret != 0) {
Benjamin Herrenschmidt5fe0c1f2013-05-06 11:37:43 +1000363 /*
364 * If map() returns -EPERM, this interrupt is protected
365 * by the firmware or some other service and shall not
Grant Likely5e1cda52013-05-29 03:10:53 +0100366 * be mapped. Don't bother telling the user about it.
Benjamin Herrenschmidt5fe0c1f2013-05-06 11:37:43 +1000367 */
368 if (ret != -EPERM) {
Grant Likely5e1cda52013-05-29 03:10:53 +0100369 pr_info("%s didn't like hwirq-0x%lx to VIRQ%i mapping (rc=%d)\n",
Grant Likely0bb4afb2013-06-06 14:23:30 +0100370 domain->name, hwirq, virq, ret);
Benjamin Herrenschmidt5fe0c1f2013-05-06 11:37:43 +1000371 }
Mark Brownf5a1ad02012-07-20 10:33:19 +0100372 irq_data->domain = NULL;
373 irq_data->hwirq = 0;
Grant Likely5e1cda52013-05-29 03:10:53 +0100374 continue;
Mark Brownf5a1ad02012-07-20 10:33:19 +0100375 }
Grant Likely0bb4afb2013-06-06 14:23:30 +0100376 /* If not already assigned, give the domain the chip's name */
377 if (!domain->name && irq_data->chip)
378 domain->name = irq_data->chip->name;
Grant Likely98aa4682012-06-17 16:17:04 -0600379 }
380
Grant Likelycef50752012-07-11 17:24:31 +0100381 if (hwirq < domain->revmap_data.linear.size) {
382 domain->linear_revmap[hwirq] = virq;
383 } else {
Grant Likely98aa4682012-06-17 16:17:04 -0600384 mutex_lock(&revmap_trees_mutex);
Grant Likelyd6b0d1f2012-06-03 22:04:37 -0700385 radix_tree_insert(&domain->revmap_data.tree, hwirq, irq_data);
Grant Likely98aa4682012-06-17 16:17:04 -0600386 mutex_unlock(&revmap_trees_mutex);
Grant Likely98aa4682012-06-17 16:17:04 -0600387 }
388
389 irq_clear_status_flags(virq, IRQ_NOREQUEST);
Grant Likelycc79ca62012-02-16 01:37:49 -0700390 }
391
Grant Likelycc79ca62012-02-16 01:37:49 -0700392 return 0;
393}
Grant Likely98aa4682012-06-17 16:17:04 -0600394EXPORT_SYMBOL_GPL(irq_domain_associate_many);
Grant Likelycc79ca62012-02-16 01:37:49 -0700395
396/**
397 * irq_create_direct_mapping() - Allocate an irq for direct mapping
Grant Likely68700652012-02-14 14:06:53 -0700398 * @domain: domain to allocate the irq for or NULL for default domain
Grant Likelycc79ca62012-02-16 01:37:49 -0700399 *
400 * This routine is used for irq controllers which can choose the hardware
401 * interrupt numbers they generate. In such a case it's simplest to use
402 * the linux irq as the hardware interrupt number.
403 */
Grant Likely68700652012-02-14 14:06:53 -0700404unsigned int irq_create_direct_mapping(struct irq_domain *domain)
Grant Likelycc79ca62012-02-16 01:37:49 -0700405{
406 unsigned int virq;
407
Grant Likely68700652012-02-14 14:06:53 -0700408 if (domain == NULL)
409 domain = irq_default_domain;
Grant Likelycc79ca62012-02-16 01:37:49 -0700410
Grant Likely9844a552012-06-03 22:04:38 -0700411 if (WARN_ON(!domain || domain->revmap_type != IRQ_DOMAIN_MAP_NOMAP))
412 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700413
Paul Mundt5ca4db62012-06-03 22:04:34 -0700414 virq = irq_alloc_desc_from(1, of_node_to_nid(domain->of_node));
Grant Likely03848372012-02-14 14:06:52 -0700415 if (!virq) {
Paul Mundt54a90582012-05-19 15:11:47 +0900416 pr_debug("create_direct virq allocation failed\n");
Grant Likely03848372012-02-14 14:06:52 -0700417 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700418 }
Grant Likely6fa6c8e22012-02-15 15:06:08 -0700419 if (virq >= domain->revmap_data.nomap.max_irq) {
Grant Likelycc79ca62012-02-16 01:37:49 -0700420 pr_err("ERROR: no free irqs available below %i maximum\n",
Grant Likely6fa6c8e22012-02-15 15:06:08 -0700421 domain->revmap_data.nomap.max_irq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700422 irq_free_desc(virq);
423 return 0;
424 }
Paul Mundt54a90582012-05-19 15:11:47 +0900425 pr_debug("create_direct obtained virq %d\n", virq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700426
Grant Likely98aa4682012-06-17 16:17:04 -0600427 if (irq_domain_associate(domain, virq, virq)) {
Grant Likelycc79ca62012-02-16 01:37:49 -0700428 irq_free_desc(virq);
Grant Likely03848372012-02-14 14:06:52 -0700429 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700430 }
431
432 return virq;
433}
Paul Mundtecd84eb2012-05-19 15:11:42 +0900434EXPORT_SYMBOL_GPL(irq_create_direct_mapping);
Grant Likelycc79ca62012-02-16 01:37:49 -0700435
436/**
437 * irq_create_mapping() - Map a hardware interrupt into linux irq space
Grant Likely68700652012-02-14 14:06:53 -0700438 * @domain: domain owning this hardware interrupt or NULL for default domain
439 * @hwirq: hardware irq number in that domain space
Grant Likelycc79ca62012-02-16 01:37:49 -0700440 *
441 * Only one mapping per hardware interrupt is permitted. Returns a linux
442 * irq number.
443 * If the sense/trigger is to be specified, set_irq_type() should be called
444 * on the number returned from that call.
445 */
Grant Likely68700652012-02-14 14:06:53 -0700446unsigned int irq_create_mapping(struct irq_domain *domain,
Grant Likelycc79ca62012-02-16 01:37:49 -0700447 irq_hw_number_t hwirq)
448{
David Daney5b7526e2012-04-05 16:52:13 -0700449 unsigned int hint;
450 int virq;
Grant Likelycc79ca62012-02-16 01:37:49 -0700451
Paul Mundt54a90582012-05-19 15:11:47 +0900452 pr_debug("irq_create_mapping(0x%p, 0x%lx)\n", domain, hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700453
Grant Likely68700652012-02-14 14:06:53 -0700454 /* Look for default domain if nececssary */
455 if (domain == NULL)
456 domain = irq_default_domain;
457 if (domain == NULL) {
Paul Mundt54a90582012-05-19 15:11:47 +0900458 pr_warning("irq_create_mapping called for"
459 " NULL domain, hwirq=%lx\n", hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700460 WARN_ON(1);
Grant Likely03848372012-02-14 14:06:52 -0700461 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700462 }
Paul Mundt54a90582012-05-19 15:11:47 +0900463 pr_debug("-> using domain @%p\n", domain);
Grant Likelycc79ca62012-02-16 01:37:49 -0700464
465 /* Check if mapping already exists */
Grant Likely68700652012-02-14 14:06:53 -0700466 virq = irq_find_mapping(domain, hwirq);
Grant Likely03848372012-02-14 14:06:52 -0700467 if (virq) {
Paul Mundt54a90582012-05-19 15:11:47 +0900468 pr_debug("-> existing mapping on virq %d\n", virq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700469 return virq;
470 }
471
Grant Likely1bc04f22012-02-14 14:06:55 -0700472 /* Allocate a virtual interrupt number */
Grant Likely6fa6c8e22012-02-15 15:06:08 -0700473 hint = hwirq % nr_irqs;
Grant Likely1bc04f22012-02-14 14:06:55 -0700474 if (hint == 0)
475 hint++;
Paul Mundt5ca4db62012-06-03 22:04:34 -0700476 virq = irq_alloc_desc_from(hint, of_node_to_nid(domain->of_node));
David Daney5b7526e2012-04-05 16:52:13 -0700477 if (virq <= 0)
Paul Mundt5ca4db62012-06-03 22:04:34 -0700478 virq = irq_alloc_desc_from(1, of_node_to_nid(domain->of_node));
David Daney5b7526e2012-04-05 16:52:13 -0700479 if (virq <= 0) {
Paul Mundt54a90582012-05-19 15:11:47 +0900480 pr_debug("-> virq allocation failed\n");
Grant Likely1bc04f22012-02-14 14:06:55 -0700481 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700482 }
483
Grant Likely98aa4682012-06-17 16:17:04 -0600484 if (irq_domain_associate(domain, virq, hwirq)) {
Grant Likely73255702012-06-03 22:04:35 -0700485 irq_free_desc(virq);
Grant Likely03848372012-02-14 14:06:52 -0700486 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700487 }
488
Paul Mundt54a90582012-05-19 15:11:47 +0900489 pr_debug("irq %lu on domain %s mapped to virtual irq %u\n",
Grant Likelyefd68e72012-06-03 22:04:33 -0700490 hwirq, of_node_full_name(domain->of_node), virq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700491
492 return virq;
493}
494EXPORT_SYMBOL_GPL(irq_create_mapping);
495
Grant Likely98aa4682012-06-17 16:17:04 -0600496/**
497 * irq_create_strict_mappings() - Map a range of hw irqs to fixed linux irqs
498 * @domain: domain owning the interrupt range
499 * @irq_base: beginning of linux IRQ range
500 * @hwirq_base: beginning of hardware IRQ range
501 * @count: Number of interrupts to map
502 *
503 * This routine is used for allocating and mapping a range of hardware
504 * irqs to linux irqs where the linux irq numbers are at pre-defined
505 * locations. For use by controllers that already have static mappings
506 * to insert in to the domain.
507 *
508 * Non-linear users can use irq_create_identity_mapping() for IRQ-at-a-time
509 * domain insertion.
510 *
511 * 0 is returned upon success, while any failure to establish a static
512 * mapping is treated as an error.
513 */
514int irq_create_strict_mappings(struct irq_domain *domain, unsigned int irq_base,
515 irq_hw_number_t hwirq_base, int count)
516{
517 int ret;
518
519 ret = irq_alloc_descs(irq_base, irq_base, count,
520 of_node_to_nid(domain->of_node));
521 if (unlikely(ret < 0))
522 return ret;
523
524 ret = irq_domain_associate_many(domain, irq_base, hwirq_base, count);
525 if (unlikely(ret < 0)) {
526 irq_free_descs(irq_base, count);
527 return ret;
528 }
529
530 return 0;
531}
532EXPORT_SYMBOL_GPL(irq_create_strict_mappings);
533
Grant Likelycc79ca62012-02-16 01:37:49 -0700534unsigned int irq_create_of_mapping(struct device_node *controller,
535 const u32 *intspec, unsigned int intsize)
536{
Grant Likely68700652012-02-14 14:06:53 -0700537 struct irq_domain *domain;
Grant Likelycc79ca62012-02-16 01:37:49 -0700538 irq_hw_number_t hwirq;
539 unsigned int type = IRQ_TYPE_NONE;
540 unsigned int virq;
541
Grant Likely68700652012-02-14 14:06:53 -0700542 domain = controller ? irq_find_host(controller) : irq_default_domain;
543 if (!domain) {
Grant Likelyabd23632012-02-24 08:07:06 -0700544#ifdef CONFIG_MIPS
545 /*
546 * Workaround to avoid breaking interrupt controller drivers
547 * that don't yet register an irq_domain. This is temporary
548 * code. ~~~gcl, Feb 24, 2012
549 *
550 * Scheduled for removal in Linux v3.6. That should be enough
551 * time.
552 */
553 if (intsize > 0)
554 return intspec[0];
555#endif
Paul Mundt54a90582012-05-19 15:11:47 +0900556 pr_warning("no irq domain found for %s !\n",
Grant Likelyefd68e72012-06-03 22:04:33 -0700557 of_node_full_name(controller));
Grant Likely03848372012-02-14 14:06:52 -0700558 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700559 }
560
Grant Likely68700652012-02-14 14:06:53 -0700561 /* If domain has no translation, then we assume interrupt line */
562 if (domain->ops->xlate == NULL)
Grant Likelycc79ca62012-02-16 01:37:49 -0700563 hwirq = intspec[0];
564 else {
Grant Likely68700652012-02-14 14:06:53 -0700565 if (domain->ops->xlate(domain, controller, intspec, intsize,
Grant Likelycc79ca62012-02-16 01:37:49 -0700566 &hwirq, &type))
Grant Likely03848372012-02-14 14:06:52 -0700567 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700568 }
569
570 /* Create mapping */
Grant Likely68700652012-02-14 14:06:53 -0700571 virq = irq_create_mapping(domain, hwirq);
Grant Likely03848372012-02-14 14:06:52 -0700572 if (!virq)
Grant Likelycc79ca62012-02-16 01:37:49 -0700573 return virq;
574
575 /* Set type if specified and different than the current one */
576 if (type != IRQ_TYPE_NONE &&
577 type != (irqd_get_trigger_type(irq_get_irq_data(virq))))
578 irq_set_irq_type(virq, type);
579 return virq;
580}
581EXPORT_SYMBOL_GPL(irq_create_of_mapping);
582
583/**
584 * irq_dispose_mapping() - Unmap an interrupt
585 * @virq: linux irq number of the interrupt to unmap
586 */
587void irq_dispose_mapping(unsigned int virq)
588{
589 struct irq_data *irq_data = irq_get_irq_data(virq);
Grant Likely68700652012-02-14 14:06:53 -0700590 struct irq_domain *domain;
Grant Likelycc79ca62012-02-16 01:37:49 -0700591
Grant Likely03848372012-02-14 14:06:52 -0700592 if (!virq || !irq_data)
Grant Likelycc79ca62012-02-16 01:37:49 -0700593 return;
594
Grant Likely68700652012-02-14 14:06:53 -0700595 domain = irq_data->domain;
596 if (WARN_ON(domain == NULL))
Grant Likelycc79ca62012-02-16 01:37:49 -0700597 return;
598
Grant Likely913af202012-06-03 22:04:35 -0700599 irq_domain_disassociate_many(domain, virq, 1);
Grant Likelycc79ca62012-02-16 01:37:49 -0700600 irq_free_desc(virq);
601}
602EXPORT_SYMBOL_GPL(irq_dispose_mapping);
603
604/**
605 * irq_find_mapping() - Find a linux irq from an hw irq number.
Grant Likely68700652012-02-14 14:06:53 -0700606 * @domain: domain owning this hardware interrupt
607 * @hwirq: hardware irq number in that domain space
Grant Likelycc79ca62012-02-16 01:37:49 -0700608 */
Grant Likely68700652012-02-14 14:06:53 -0700609unsigned int irq_find_mapping(struct irq_domain *domain,
Grant Likelycc79ca62012-02-16 01:37:49 -0700610 irq_hw_number_t hwirq)
611{
Grant Likely4c0946c2012-06-03 22:04:39 -0700612 struct irq_data *data;
Grant Likelycc79ca62012-02-16 01:37:49 -0700613
Grant Likely68700652012-02-14 14:06:53 -0700614 /* Look for default domain if nececssary */
615 if (domain == NULL)
616 domain = irq_default_domain;
617 if (domain == NULL)
Grant Likely03848372012-02-14 14:06:52 -0700618 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700619
Grant Likely4c0946c2012-06-03 22:04:39 -0700620 switch (domain->revmap_type) {
Grant Likely4c0946c2012-06-03 22:04:39 -0700621 case IRQ_DOMAIN_MAP_LINEAR:
622 return irq_linear_revmap(domain, hwirq);
Grant Likely4c0946c2012-06-03 22:04:39 -0700623 case IRQ_DOMAIN_MAP_NOMAP:
624 data = irq_get_irq_data(hwirq);
Grant Likely68700652012-02-14 14:06:53 -0700625 if (data && (data->domain == domain) && (data->hwirq == hwirq))
Grant Likely4c0946c2012-06-03 22:04:39 -0700626 return hwirq;
627 break;
628 }
629
Grant Likely03848372012-02-14 14:06:52 -0700630 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700631}
632EXPORT_SYMBOL_GPL(irq_find_mapping);
633
634/**
Grant Likelycc79ca62012-02-16 01:37:49 -0700635 * irq_linear_revmap() - Find a linux irq from a hw irq number.
Grant Likely68700652012-02-14 14:06:53 -0700636 * @domain: domain owning this hardware interrupt
637 * @hwirq: hardware irq number in that domain space
Grant Likelycc79ca62012-02-16 01:37:49 -0700638 *
Grant Likely4c0946c2012-06-03 22:04:39 -0700639 * This is a fast path that can be called directly by irq controller code to
640 * save a handful of instructions.
Grant Likelycc79ca62012-02-16 01:37:49 -0700641 */
Grant Likely68700652012-02-14 14:06:53 -0700642unsigned int irq_linear_revmap(struct irq_domain *domain,
Grant Likelycc79ca62012-02-16 01:37:49 -0700643 irq_hw_number_t hwirq)
644{
Grant Likelycef50752012-07-11 17:24:31 +0100645 struct irq_data *data;
Grant Likely4c0946c2012-06-03 22:04:39 -0700646 BUG_ON(domain->revmap_type != IRQ_DOMAIN_MAP_LINEAR);
Grant Likelycc79ca62012-02-16 01:37:49 -0700647
Grant Likely4c0946c2012-06-03 22:04:39 -0700648 /* Check revmap bounds; complain if exceeded */
Grant Likelycef50752012-07-11 17:24:31 +0100649 if (hwirq >= domain->revmap_data.linear.size) {
650 rcu_read_lock();
651 data = radix_tree_lookup(&domain->revmap_data.tree, hwirq);
652 rcu_read_unlock();
653 return data ? data->irq : 0;
654 }
Grant Likelycc79ca62012-02-16 01:37:49 -0700655
Grant Likelycef50752012-07-11 17:24:31 +0100656 return domain->linear_revmap[hwirq];
Grant Likelycc79ca62012-02-16 01:37:49 -0700657}
Paul Mundtecd84eb2012-05-19 15:11:42 +0900658EXPORT_SYMBOL_GPL(irq_linear_revmap);
Grant Likelycc79ca62012-02-16 01:37:49 -0700659
Grant Likely092b2fb2012-03-29 14:10:30 -0600660#ifdef CONFIG_IRQ_DOMAIN_DEBUG
Grant Likelycc79ca62012-02-16 01:37:49 -0700661static int virq_debug_show(struct seq_file *m, void *private)
662{
663 unsigned long flags;
664 struct irq_desc *desc;
Grant Likelycc79ca62012-02-16 01:37:49 -0700665 void *data;
666 int i;
667
Grant Likely15e06bf2012-04-11 00:26:25 -0600668 seq_printf(m, "%-5s %-7s %-15s %-*s %s\n", "irq", "hwirq",
Grant Likely5269a9a2012-04-12 14:42:15 -0600669 "chip name", (int)(2 * sizeof(void *) + 2), "chip data",
670 "domain name");
Grant Likelycc79ca62012-02-16 01:37:49 -0700671
672 for (i = 1; i < nr_irqs; i++) {
673 desc = irq_to_desc(i);
674 if (!desc)
675 continue;
676
677 raw_spin_lock_irqsave(&desc->lock, flags);
678
679 if (desc->action && desc->action->handler) {
680 struct irq_chip *chip;
681
682 seq_printf(m, "%5d ", i);
683 seq_printf(m, "0x%05lx ", desc->irq_data.hwirq);
684
685 chip = irq_desc_get_chip(desc);
Grant Likely0bb4afb2013-06-06 14:23:30 +0100686 seq_printf(m, "%-15s ", (chip && chip->name) ? chip->name : "none");
Grant Likelycc79ca62012-02-16 01:37:49 -0700687
688 data = irq_desc_get_chip_data(desc);
Grant Likely15e06bf2012-04-11 00:26:25 -0600689 seq_printf(m, data ? "0x%p " : " %p ", data);
Grant Likelycc79ca62012-02-16 01:37:49 -0700690
Grant Likely0bb4afb2013-06-06 14:23:30 +0100691 seq_printf(m, "%s\n", desc->irq_data.domain->name);
Grant Likelycc79ca62012-02-16 01:37:49 -0700692 }
693
694 raw_spin_unlock_irqrestore(&desc->lock, flags);
695 }
696
697 return 0;
698}
699
700static int virq_debug_open(struct inode *inode, struct file *file)
701{
702 return single_open(file, virq_debug_show, inode->i_private);
703}
704
705static const struct file_operations virq_debug_fops = {
706 .open = virq_debug_open,
707 .read = seq_read,
708 .llseek = seq_lseek,
709 .release = single_release,
710};
711
712static int __init irq_debugfs_init(void)
713{
Grant Likely092b2fb2012-03-29 14:10:30 -0600714 if (debugfs_create_file("irq_domain_mapping", S_IRUGO, NULL,
Grant Likelycc79ca62012-02-16 01:37:49 -0700715 NULL, &virq_debug_fops) == NULL)
716 return -ENOMEM;
717
718 return 0;
719}
720__initcall(irq_debugfs_init);
Grant Likely092b2fb2012-03-29 14:10:30 -0600721#endif /* CONFIG_IRQ_DOMAIN_DEBUG */
Grant Likelycc79ca62012-02-16 01:37:49 -0700722
Grant Likely16b2e6e2012-01-26 11:26:52 -0700723/**
724 * irq_domain_xlate_onecell() - Generic xlate for direct one cell bindings
725 *
726 * Device Tree IRQ specifier translation function which works with one cell
727 * bindings where the cell value maps directly to the hwirq number.
728 */
729int irq_domain_xlate_onecell(struct irq_domain *d, struct device_node *ctrlr,
730 const u32 *intspec, unsigned int intsize,
731 unsigned long *out_hwirq, unsigned int *out_type)
Grant Likely7e713302011-07-26 03:19:06 -0600732{
Grant Likely16b2e6e2012-01-26 11:26:52 -0700733 if (WARN_ON(intsize < 1))
Grant Likely7e713302011-07-26 03:19:06 -0600734 return -EINVAL;
Grant Likely7e713302011-07-26 03:19:06 -0600735 *out_hwirq = intspec[0];
736 *out_type = IRQ_TYPE_NONE;
Grant Likely7e713302011-07-26 03:19:06 -0600737 return 0;
738}
Grant Likely16b2e6e2012-01-26 11:26:52 -0700739EXPORT_SYMBOL_GPL(irq_domain_xlate_onecell);
740
741/**
742 * irq_domain_xlate_twocell() - Generic xlate for direct two cell bindings
743 *
744 * Device Tree IRQ specifier translation function which works with two cell
745 * bindings where the cell values map directly to the hwirq number
746 * and linux irq flags.
747 */
748int irq_domain_xlate_twocell(struct irq_domain *d, struct device_node *ctrlr,
749 const u32 *intspec, unsigned int intsize,
750 irq_hw_number_t *out_hwirq, unsigned int *out_type)
751{
752 if (WARN_ON(intsize < 2))
753 return -EINVAL;
754 *out_hwirq = intspec[0];
755 *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK;
756 return 0;
757}
758EXPORT_SYMBOL_GPL(irq_domain_xlate_twocell);
759
760/**
761 * irq_domain_xlate_onetwocell() - Generic xlate for one or two cell bindings
762 *
763 * Device Tree IRQ specifier translation function which works with either one
764 * or two cell bindings where the cell values map directly to the hwirq number
765 * and linux irq flags.
766 *
767 * Note: don't use this function unless your interrupt controller explicitly
768 * supports both one and two cell bindings. For the majority of controllers
769 * the _onecell() or _twocell() variants above should be used.
770 */
771int irq_domain_xlate_onetwocell(struct irq_domain *d,
772 struct device_node *ctrlr,
773 const u32 *intspec, unsigned int intsize,
774 unsigned long *out_hwirq, unsigned int *out_type)
775{
776 if (WARN_ON(intsize < 1))
777 return -EINVAL;
778 *out_hwirq = intspec[0];
779 *out_type = (intsize > 1) ? intspec[1] : IRQ_TYPE_NONE;
780 return 0;
781}
782EXPORT_SYMBOL_GPL(irq_domain_xlate_onetwocell);
Grant Likely7e713302011-07-26 03:19:06 -0600783
Grant Likelya18dc812012-01-26 12:12:14 -0700784const struct irq_domain_ops irq_domain_simple_ops = {
Grant Likely16b2e6e2012-01-26 11:26:52 -0700785 .xlate = irq_domain_xlate_onetwocell,
Grant Likely75294952012-02-14 14:06:57 -0700786};
787EXPORT_SYMBOL_GPL(irq_domain_simple_ops);
788
789#ifdef CONFIG_OF_IRQ
Grant Likely7e713302011-07-26 03:19:06 -0600790void irq_domain_generate_simple(const struct of_device_id *match,
791 u64 phys_base, unsigned int irq_start)
792{
793 struct device_node *node;
Grant Likelye1964c52012-02-14 14:06:48 -0700794 pr_debug("looking for phys_base=%llx, irq_start=%i\n",
Grant Likely7e713302011-07-26 03:19:06 -0600795 (unsigned long long) phys_base, (int) irq_start);
796 node = of_find_matching_node_by_address(NULL, match, phys_base);
797 if (node)
Grant Likely6b783f72012-01-10 17:09:30 -0700798 irq_domain_add_legacy(node, 32, irq_start, 0,
799 &irq_domain_simple_ops, NULL);
Grant Likely7e713302011-07-26 03:19:06 -0600800}
801EXPORT_SYMBOL_GPL(irq_domain_generate_simple);
Grant Likely75294952012-02-14 14:06:57 -0700802#endif