blob: 432d292b33f8c273a9b125567c0c708db09f062e [file] [log] [blame]
Grant Likelycc79ca62012-02-16 01:37:49 -07001#include <linux/debugfs.h>
2#include <linux/hardirq.h>
3#include <linux/interrupt.h>
Grant Likely08a543a2011-07-26 03:19:06 -06004#include <linux/irq.h>
Grant Likelycc79ca62012-02-16 01:37:49 -07005#include <linux/irqdesc.h>
Grant Likely08a543a2011-07-26 03:19:06 -06006#include <linux/irqdomain.h>
7#include <linux/module.h>
8#include <linux/mutex.h>
9#include <linux/of.h>
Grant Likely7e713302011-07-26 03:19:06 -060010#include <linux/of_address.h>
Grant Likelycc79ca62012-02-16 01:37:49 -070011#include <linux/seq_file.h>
Grant Likely7e713302011-07-26 03:19:06 -060012#include <linux/slab.h>
Grant Likelycc79ca62012-02-16 01:37:49 -070013#include <linux/smp.h>
14#include <linux/fs.h>
Grant Likely08a543a2011-07-26 03:19:06 -060015
16static LIST_HEAD(irq_domain_list);
17static DEFINE_MUTEX(irq_domain_mutex);
18
Grant Likelycc79ca62012-02-16 01:37:49 -070019#ifdef CONFIG_PPC
20static DEFINE_MUTEX(revmap_trees_mutex);
21static unsigned int irq_virq_count = NR_IRQS;
Grant Likely68700652012-02-14 14:06:53 -070022static struct irq_domain *irq_default_domain;
Grant Likelycc79ca62012-02-16 01:37:49 -070023
Grant Likely68700652012-02-14 14:06:53 -070024static int default_irq_domain_match(struct irq_domain *d, struct device_node *np)
Grant Likelycc79ca62012-02-16 01:37:49 -070025{
Grant Likely68700652012-02-14 14:06:53 -070026 return d->of_node != NULL && d->of_node == np;
Grant Likelycc79ca62012-02-16 01:37:49 -070027}
28
29/**
30 * irq_alloc_host() - Allocate a new irq_domain data structure
31 * @of_node: optional device-tree node of the interrupt controller
32 * @revmap_type: type of reverse mapping to use
33 * @revmap_arg: for IRQ_DOMAIN_MAP_LINEAR linear only: size of the map
Grant Likely68700652012-02-14 14:06:53 -070034 * @ops: map/unmap domain callbacks
35 * @inval_irq: provide a hw number in that domain space that is always invalid
Grant Likelycc79ca62012-02-16 01:37:49 -070036 *
37 * Allocates and initialize and irq_domain structure. Note that in the case of
38 * IRQ_DOMAIN_MAP_LEGACY, the map() callback will be called before this returns
39 * for all legacy interrupts except 0 (which is always the invalid irq for
40 * a legacy controller). For a IRQ_DOMAIN_MAP_LINEAR, the map is allocated by
41 * this call as well. For a IRQ_DOMAIN_MAP_TREE, the radix tree will be
42 * allocated later during boot automatically (the reverse mapping will use the
43 * slow path until that happens).
44 */
45struct irq_domain *irq_alloc_host(struct device_node *of_node,
46 unsigned int revmap_type,
47 unsigned int revmap_arg,
48 struct irq_domain_ops *ops,
49 irq_hw_number_t inval_irq)
50{
Grant Likely68700652012-02-14 14:06:53 -070051 struct irq_domain *domain, *h;
Grant Likelycc79ca62012-02-16 01:37:49 -070052 unsigned int size = sizeof(struct irq_domain);
53 unsigned int i;
54 unsigned int *rmap;
55
56 /* Allocate structure and revmap table if using linear mapping */
57 if (revmap_type == IRQ_DOMAIN_MAP_LINEAR)
58 size += revmap_arg * sizeof(unsigned int);
Grant Likely68700652012-02-14 14:06:53 -070059 domain = kzalloc(size, GFP_KERNEL);
60 if (domain == NULL)
Grant Likelycc79ca62012-02-16 01:37:49 -070061 return NULL;
62
63 /* Fill structure */
Grant Likely68700652012-02-14 14:06:53 -070064 domain->revmap_type = revmap_type;
65 domain->inval_irq = inval_irq;
66 domain->ops = ops;
67 domain->of_node = of_node_get(of_node);
Grant Likelycc79ca62012-02-16 01:37:49 -070068
Grant Likely68700652012-02-14 14:06:53 -070069 if (domain->ops->match == NULL)
70 domain->ops->match = default_irq_domain_match;
Grant Likelycc79ca62012-02-16 01:37:49 -070071
72 mutex_lock(&irq_domain_mutex);
73 /* Make sure only one legacy controller can be created */
74 if (revmap_type == IRQ_DOMAIN_MAP_LEGACY) {
75 list_for_each_entry(h, &irq_domain_list, link) {
76 if (WARN_ON(h->revmap_type == IRQ_DOMAIN_MAP_LEGACY)) {
77 mutex_unlock(&irq_domain_mutex);
Grant Likely68700652012-02-14 14:06:53 -070078 of_node_put(domain->of_node);
79 kfree(domain);
Grant Likelycc79ca62012-02-16 01:37:49 -070080 return NULL;
81 }
82 }
83 }
Grant Likely68700652012-02-14 14:06:53 -070084 list_add(&domain->link, &irq_domain_list);
Grant Likelycc79ca62012-02-16 01:37:49 -070085 mutex_unlock(&irq_domain_mutex);
86
87 /* Additional setups per revmap type */
88 switch(revmap_type) {
89 case IRQ_DOMAIN_MAP_LEGACY:
90 /* 0 is always the invalid number for legacy */
Grant Likely68700652012-02-14 14:06:53 -070091 domain->inval_irq = 0;
92 /* setup us as the domain for all legacy interrupts */
Grant Likelycc79ca62012-02-16 01:37:49 -070093 for (i = 1; i < NUM_ISA_INTERRUPTS; i++) {
94 struct irq_data *irq_data = irq_get_irq_data(i);
95 irq_data->hwirq = i;
Grant Likely68700652012-02-14 14:06:53 -070096 irq_data->domain = domain;
Grant Likelycc79ca62012-02-16 01:37:49 -070097
98 /* Legacy flags are left to default at this point,
99 * one can then use irq_create_mapping() to
100 * explicitly change them
101 */
Grant Likely68700652012-02-14 14:06:53 -0700102 ops->map(domain, i, i);
Grant Likelycc79ca62012-02-16 01:37:49 -0700103
104 /* Clear norequest flags */
105 irq_clear_status_flags(i, IRQ_NOREQUEST);
106 }
107 break;
108 case IRQ_DOMAIN_MAP_LINEAR:
Grant Likely68700652012-02-14 14:06:53 -0700109 rmap = (unsigned int *)(domain + 1);
Grant Likelycc79ca62012-02-16 01:37:49 -0700110 for (i = 0; i < revmap_arg; i++)
Grant Likely03848372012-02-14 14:06:52 -0700111 rmap[i] = 0;
Grant Likely68700652012-02-14 14:06:53 -0700112 domain->revmap_data.linear.size = revmap_arg;
113 domain->revmap_data.linear.revmap = rmap;
Grant Likelycc79ca62012-02-16 01:37:49 -0700114 break;
115 case IRQ_DOMAIN_MAP_TREE:
Grant Likely68700652012-02-14 14:06:53 -0700116 INIT_RADIX_TREE(&domain->revmap_data.tree, GFP_KERNEL);
Grant Likelycc79ca62012-02-16 01:37:49 -0700117 break;
118 default:
119 break;
120 }
121
Grant Likely68700652012-02-14 14:06:53 -0700122 pr_debug("irq: Allocated domain of type %d @0x%p\n", revmap_type, domain);
Grant Likelycc79ca62012-02-16 01:37:49 -0700123
Grant Likely68700652012-02-14 14:06:53 -0700124 return domain;
Grant Likelycc79ca62012-02-16 01:37:49 -0700125}
126
127/**
128 * irq_find_host() - Locates a domain for a given device node
129 * @node: device-tree node of the interrupt controller
130 */
131struct irq_domain *irq_find_host(struct device_node *node)
132{
133 struct irq_domain *h, *found = NULL;
134
135 /* We might want to match the legacy controller last since
136 * it might potentially be set to match all interrupts in
137 * the absence of a device node. This isn't a problem so far
138 * yet though...
139 */
140 mutex_lock(&irq_domain_mutex);
141 list_for_each_entry(h, &irq_domain_list, link)
142 if (h->ops->match(h, node)) {
143 found = h;
144 break;
145 }
146 mutex_unlock(&irq_domain_mutex);
147 return found;
148}
149EXPORT_SYMBOL_GPL(irq_find_host);
150
151/**
152 * irq_set_default_host() - Set a "default" irq domain
Grant Likely68700652012-02-14 14:06:53 -0700153 * @domain: default domain pointer
Grant Likelycc79ca62012-02-16 01:37:49 -0700154 *
155 * For convenience, it's possible to set a "default" domain that will be used
156 * whenever NULL is passed to irq_create_mapping(). It makes life easier for
157 * platforms that want to manipulate a few hard coded interrupt numbers that
158 * aren't properly represented in the device-tree.
159 */
Grant Likely68700652012-02-14 14:06:53 -0700160void irq_set_default_host(struct irq_domain *domain)
Grant Likelycc79ca62012-02-16 01:37:49 -0700161{
Grant Likely68700652012-02-14 14:06:53 -0700162 pr_debug("irq: Default domain set to @0x%p\n", domain);
Grant Likelycc79ca62012-02-16 01:37:49 -0700163
Grant Likely68700652012-02-14 14:06:53 -0700164 irq_default_domain = domain;
Grant Likelycc79ca62012-02-16 01:37:49 -0700165}
166
167/**
168 * irq_set_virq_count() - Set the maximum number of linux irqs
169 * @count: number of linux irqs, capped with NR_IRQS
170 *
171 * This is mainly for use by platforms like iSeries who want to program
172 * the virtual irq number in the controller to avoid the reverse mapping
173 */
174void irq_set_virq_count(unsigned int count)
175{
176 pr_debug("irq: Trying to set virq count to %d\n", count);
177
178 BUG_ON(count < NUM_ISA_INTERRUPTS);
179 if (count < NR_IRQS)
180 irq_virq_count = count;
181}
182
Grant Likely68700652012-02-14 14:06:53 -0700183static int irq_setup_virq(struct irq_domain *domain, unsigned int virq,
Grant Likelycc79ca62012-02-16 01:37:49 -0700184 irq_hw_number_t hwirq)
185{
186 struct irq_data *irq_data = irq_get_irq_data(virq);
187
188 irq_data->hwirq = hwirq;
Grant Likely68700652012-02-14 14:06:53 -0700189 irq_data->domain = domain;
190 if (domain->ops->map(domain, virq, hwirq)) {
Grant Likelycc79ca62012-02-16 01:37:49 -0700191 pr_debug("irq: -> mapping failed, freeing\n");
192 irq_data->domain = NULL;
193 irq_data->hwirq = 0;
194 return -1;
195 }
196
197 irq_clear_status_flags(virq, IRQ_NOREQUEST);
198
199 return 0;
200}
201
202/**
203 * irq_create_direct_mapping() - Allocate an irq for direct mapping
Grant Likely68700652012-02-14 14:06:53 -0700204 * @domain: domain to allocate the irq for or NULL for default domain
Grant Likelycc79ca62012-02-16 01:37:49 -0700205 *
206 * This routine is used for irq controllers which can choose the hardware
207 * interrupt numbers they generate. In such a case it's simplest to use
208 * the linux irq as the hardware interrupt number.
209 */
Grant Likely68700652012-02-14 14:06:53 -0700210unsigned int irq_create_direct_mapping(struct irq_domain *domain)
Grant Likelycc79ca62012-02-16 01:37:49 -0700211{
212 unsigned int virq;
213
Grant Likely68700652012-02-14 14:06:53 -0700214 if (domain == NULL)
215 domain = irq_default_domain;
Grant Likelycc79ca62012-02-16 01:37:49 -0700216
Grant Likely68700652012-02-14 14:06:53 -0700217 BUG_ON(domain == NULL);
218 WARN_ON(domain->revmap_type != IRQ_DOMAIN_MAP_NOMAP);
Grant Likelycc79ca62012-02-16 01:37:49 -0700219
220 virq = irq_alloc_desc_from(1, 0);
Grant Likely03848372012-02-14 14:06:52 -0700221 if (!virq) {
Grant Likelycc79ca62012-02-16 01:37:49 -0700222 pr_debug("irq: create_direct virq allocation failed\n");
Grant Likely03848372012-02-14 14:06:52 -0700223 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700224 }
225 if (virq >= irq_virq_count) {
226 pr_err("ERROR: no free irqs available below %i maximum\n",
227 irq_virq_count);
228 irq_free_desc(virq);
229 return 0;
230 }
231
232 pr_debug("irq: create_direct obtained virq %d\n", virq);
233
Grant Likely68700652012-02-14 14:06:53 -0700234 if (irq_setup_virq(domain, virq, virq)) {
Grant Likelycc79ca62012-02-16 01:37:49 -0700235 irq_free_desc(virq);
Grant Likely03848372012-02-14 14:06:52 -0700236 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700237 }
238
239 return virq;
240}
241
242/**
243 * irq_create_mapping() - Map a hardware interrupt into linux irq space
Grant Likely68700652012-02-14 14:06:53 -0700244 * @domain: domain owning this hardware interrupt or NULL for default domain
245 * @hwirq: hardware irq number in that domain space
Grant Likelycc79ca62012-02-16 01:37:49 -0700246 *
247 * Only one mapping per hardware interrupt is permitted. Returns a linux
248 * irq number.
249 * If the sense/trigger is to be specified, set_irq_type() should be called
250 * on the number returned from that call.
251 */
Grant Likely68700652012-02-14 14:06:53 -0700252unsigned int irq_create_mapping(struct irq_domain *domain,
Grant Likelycc79ca62012-02-16 01:37:49 -0700253 irq_hw_number_t hwirq)
254{
255 unsigned int virq, hint;
256
Grant Likely68700652012-02-14 14:06:53 -0700257 pr_debug("irq: irq_create_mapping(0x%p, 0x%lx)\n", domain, hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700258
Grant Likely68700652012-02-14 14:06:53 -0700259 /* Look for default domain if nececssary */
260 if (domain == NULL)
261 domain = irq_default_domain;
262 if (domain == NULL) {
Grant Likelycc79ca62012-02-16 01:37:49 -0700263 printk(KERN_WARNING "irq_create_mapping called for"
Grant Likely68700652012-02-14 14:06:53 -0700264 " NULL domain, hwirq=%lx\n", hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700265 WARN_ON(1);
Grant Likely03848372012-02-14 14:06:52 -0700266 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700267 }
Grant Likely68700652012-02-14 14:06:53 -0700268 pr_debug("irq: -> using domain @%p\n", domain);
Grant Likelycc79ca62012-02-16 01:37:49 -0700269
270 /* Check if mapping already exists */
Grant Likely68700652012-02-14 14:06:53 -0700271 virq = irq_find_mapping(domain, hwirq);
Grant Likely03848372012-02-14 14:06:52 -0700272 if (virq) {
Grant Likelycc79ca62012-02-16 01:37:49 -0700273 pr_debug("irq: -> existing mapping on virq %d\n", virq);
274 return virq;
275 }
276
277 /* Get a virtual interrupt number */
Grant Likely68700652012-02-14 14:06:53 -0700278 if (domain->revmap_type == IRQ_DOMAIN_MAP_LEGACY) {
Grant Likelycc79ca62012-02-16 01:37:49 -0700279 /* Handle legacy */
280 virq = (unsigned int)hwirq;
281 if (virq == 0 || virq >= NUM_ISA_INTERRUPTS)
Grant Likely03848372012-02-14 14:06:52 -0700282 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700283 return virq;
284 } else {
285 /* Allocate a virtual interrupt number */
286 hint = hwirq % irq_virq_count;
287 if (hint == 0)
288 hint++;
289 virq = irq_alloc_desc_from(hint, 0);
290 if (!virq)
291 virq = irq_alloc_desc_from(1, 0);
Grant Likely03848372012-02-14 14:06:52 -0700292 if (!virq) {
Grant Likelycc79ca62012-02-16 01:37:49 -0700293 pr_debug("irq: -> virq allocation failed\n");
Grant Likely03848372012-02-14 14:06:52 -0700294 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700295 }
296 }
297
Grant Likely68700652012-02-14 14:06:53 -0700298 if (irq_setup_virq(domain, virq, hwirq)) {
299 if (domain->revmap_type != IRQ_DOMAIN_MAP_LEGACY)
Grant Likelycc79ca62012-02-16 01:37:49 -0700300 irq_free_desc(virq);
Grant Likely03848372012-02-14 14:06:52 -0700301 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700302 }
303
Grant Likely68700652012-02-14 14:06:53 -0700304 pr_debug("irq: irq %lu on domain %s mapped to virtual irq %u\n",
305 hwirq, domain->of_node ? domain->of_node->full_name : "null", virq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700306
307 return virq;
308}
309EXPORT_SYMBOL_GPL(irq_create_mapping);
310
311unsigned int irq_create_of_mapping(struct device_node *controller,
312 const u32 *intspec, unsigned int intsize)
313{
Grant Likely68700652012-02-14 14:06:53 -0700314 struct irq_domain *domain;
Grant Likelycc79ca62012-02-16 01:37:49 -0700315 irq_hw_number_t hwirq;
316 unsigned int type = IRQ_TYPE_NONE;
317 unsigned int virq;
318
Grant Likely68700652012-02-14 14:06:53 -0700319 domain = controller ? irq_find_host(controller) : irq_default_domain;
320 if (!domain) {
321 printk(KERN_WARNING "irq: no irq domain found for %s !\n",
Grant Likelycc79ca62012-02-16 01:37:49 -0700322 controller->full_name);
Grant Likely03848372012-02-14 14:06:52 -0700323 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700324 }
325
Grant Likely68700652012-02-14 14:06:53 -0700326 /* If domain has no translation, then we assume interrupt line */
327 if (domain->ops->xlate == NULL)
Grant Likelycc79ca62012-02-16 01:37:49 -0700328 hwirq = intspec[0];
329 else {
Grant Likely68700652012-02-14 14:06:53 -0700330 if (domain->ops->xlate(domain, controller, intspec, intsize,
Grant Likelycc79ca62012-02-16 01:37:49 -0700331 &hwirq, &type))
Grant Likely03848372012-02-14 14:06:52 -0700332 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700333 }
334
335 /* Create mapping */
Grant Likely68700652012-02-14 14:06:53 -0700336 virq = irq_create_mapping(domain, hwirq);
Grant Likely03848372012-02-14 14:06:52 -0700337 if (!virq)
Grant Likelycc79ca62012-02-16 01:37:49 -0700338 return virq;
339
340 /* Set type if specified and different than the current one */
341 if (type != IRQ_TYPE_NONE &&
342 type != (irqd_get_trigger_type(irq_get_irq_data(virq))))
343 irq_set_irq_type(virq, type);
344 return virq;
345}
346EXPORT_SYMBOL_GPL(irq_create_of_mapping);
347
348/**
349 * irq_dispose_mapping() - Unmap an interrupt
350 * @virq: linux irq number of the interrupt to unmap
351 */
352void irq_dispose_mapping(unsigned int virq)
353{
354 struct irq_data *irq_data = irq_get_irq_data(virq);
Grant Likely68700652012-02-14 14:06:53 -0700355 struct irq_domain *domain;
Grant Likelycc79ca62012-02-16 01:37:49 -0700356 irq_hw_number_t hwirq;
357
Grant Likely03848372012-02-14 14:06:52 -0700358 if (!virq || !irq_data)
Grant Likelycc79ca62012-02-16 01:37:49 -0700359 return;
360
Grant Likely68700652012-02-14 14:06:53 -0700361 domain = irq_data->domain;
362 if (WARN_ON(domain == NULL))
Grant Likelycc79ca62012-02-16 01:37:49 -0700363 return;
364
365 /* Never unmap legacy interrupts */
Grant Likely68700652012-02-14 14:06:53 -0700366 if (domain->revmap_type == IRQ_DOMAIN_MAP_LEGACY)
Grant Likelycc79ca62012-02-16 01:37:49 -0700367 return;
368
369 irq_set_status_flags(virq, IRQ_NOREQUEST);
370
371 /* remove chip and handler */
372 irq_set_chip_and_handler(virq, NULL, NULL);
373
374 /* Make sure it's completed */
375 synchronize_irq(virq);
376
377 /* Tell the PIC about it */
Grant Likely68700652012-02-14 14:06:53 -0700378 if (domain->ops->unmap)
379 domain->ops->unmap(domain, virq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700380 smp_mb();
381
382 /* Clear reverse map */
383 hwirq = irq_data->hwirq;
Grant Likely68700652012-02-14 14:06:53 -0700384 switch(domain->revmap_type) {
Grant Likelycc79ca62012-02-16 01:37:49 -0700385 case IRQ_DOMAIN_MAP_LINEAR:
Grant Likely68700652012-02-14 14:06:53 -0700386 if (hwirq < domain->revmap_data.linear.size)
387 domain->revmap_data.linear.revmap[hwirq] = 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700388 break;
389 case IRQ_DOMAIN_MAP_TREE:
390 mutex_lock(&revmap_trees_mutex);
Grant Likely68700652012-02-14 14:06:53 -0700391 radix_tree_delete(&domain->revmap_data.tree, hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700392 mutex_unlock(&revmap_trees_mutex);
393 break;
394 }
395
396 /* Destroy map */
Grant Likely68700652012-02-14 14:06:53 -0700397 irq_data->hwirq = domain->inval_irq;
Grant Likelycc79ca62012-02-16 01:37:49 -0700398
399 irq_free_desc(virq);
400}
401EXPORT_SYMBOL_GPL(irq_dispose_mapping);
402
403/**
404 * irq_find_mapping() - Find a linux irq from an hw irq number.
Grant Likely68700652012-02-14 14:06:53 -0700405 * @domain: domain owning this hardware interrupt
406 * @hwirq: hardware irq number in that domain space
Grant Likelycc79ca62012-02-16 01:37:49 -0700407 *
408 * This is a slow path, for use by generic code. It's expected that an
409 * irq controller implementation directly calls the appropriate low level
410 * mapping function.
411 */
Grant Likely68700652012-02-14 14:06:53 -0700412unsigned int irq_find_mapping(struct irq_domain *domain,
Grant Likelycc79ca62012-02-16 01:37:49 -0700413 irq_hw_number_t hwirq)
414{
415 unsigned int i;
416 unsigned int hint = hwirq % irq_virq_count;
417
Grant Likely68700652012-02-14 14:06:53 -0700418 /* Look for default domain if nececssary */
419 if (domain == NULL)
420 domain = irq_default_domain;
421 if (domain == NULL)
Grant Likely03848372012-02-14 14:06:52 -0700422 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700423
424 /* legacy -> bail early */
Grant Likely68700652012-02-14 14:06:53 -0700425 if (domain->revmap_type == IRQ_DOMAIN_MAP_LEGACY)
Grant Likelycc79ca62012-02-16 01:37:49 -0700426 return hwirq;
427
428 /* Slow path does a linear search of the map */
429 if (hint == 0)
430 hint = 1;
431 i = hint;
432 do {
433 struct irq_data *data = irq_get_irq_data(i);
Grant Likely68700652012-02-14 14:06:53 -0700434 if (data && (data->domain == domain) && (data->hwirq == hwirq))
Grant Likelycc79ca62012-02-16 01:37:49 -0700435 return i;
436 i++;
437 if (i >= irq_virq_count)
438 i = 1;
439 } while(i != hint);
Grant Likely03848372012-02-14 14:06:52 -0700440 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700441}
442EXPORT_SYMBOL_GPL(irq_find_mapping);
443
444/**
445 * irq_radix_revmap_lookup() - Find a linux irq from a hw irq number.
Grant Likely68700652012-02-14 14:06:53 -0700446 * @domain: domain owning this hardware interrupt
447 * @hwirq: hardware irq number in that domain space
Grant Likelycc79ca62012-02-16 01:37:49 -0700448 *
449 * This is a fast path, for use by irq controller code that uses radix tree
450 * revmaps
451 */
Grant Likely68700652012-02-14 14:06:53 -0700452unsigned int irq_radix_revmap_lookup(struct irq_domain *domain,
Grant Likelycc79ca62012-02-16 01:37:49 -0700453 irq_hw_number_t hwirq)
454{
455 struct irq_data *irq_data;
456
Grant Likely68700652012-02-14 14:06:53 -0700457 if (WARN_ON_ONCE(domain->revmap_type != IRQ_DOMAIN_MAP_TREE))
458 return irq_find_mapping(domain, hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700459
460 /*
461 * Freeing an irq can delete nodes along the path to
462 * do the lookup via call_rcu.
463 */
464 rcu_read_lock();
Grant Likely68700652012-02-14 14:06:53 -0700465 irq_data = radix_tree_lookup(&domain->revmap_data.tree, hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700466 rcu_read_unlock();
467
468 /*
469 * If found in radix tree, then fine.
470 * Else fallback to linear lookup - this should not happen in practice
471 * as it means that we failed to insert the node in the radix tree.
472 */
Grant Likely68700652012-02-14 14:06:53 -0700473 return irq_data ? irq_data->irq : irq_find_mapping(domain, hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700474}
475
476/**
477 * irq_radix_revmap_insert() - Insert a hw irq to linux irq number mapping.
Grant Likely68700652012-02-14 14:06:53 -0700478 * @domain: domain owning this hardware interrupt
Grant Likelycc79ca62012-02-16 01:37:49 -0700479 * @virq: linux irq number
Grant Likely68700652012-02-14 14:06:53 -0700480 * @hwirq: hardware irq number in that domain space
Grant Likelycc79ca62012-02-16 01:37:49 -0700481 *
482 * This is for use by irq controllers that use a radix tree reverse
483 * mapping for fast lookup.
484 */
Grant Likely68700652012-02-14 14:06:53 -0700485void irq_radix_revmap_insert(struct irq_domain *domain, unsigned int virq,
Grant Likelycc79ca62012-02-16 01:37:49 -0700486 irq_hw_number_t hwirq)
487{
488 struct irq_data *irq_data = irq_get_irq_data(virq);
489
Grant Likely68700652012-02-14 14:06:53 -0700490 if (WARN_ON(domain->revmap_type != IRQ_DOMAIN_MAP_TREE))
Grant Likelycc79ca62012-02-16 01:37:49 -0700491 return;
492
Grant Likely03848372012-02-14 14:06:52 -0700493 if (virq) {
Grant Likelycc79ca62012-02-16 01:37:49 -0700494 mutex_lock(&revmap_trees_mutex);
Grant Likely68700652012-02-14 14:06:53 -0700495 radix_tree_insert(&domain->revmap_data.tree, hwirq, irq_data);
Grant Likelycc79ca62012-02-16 01:37:49 -0700496 mutex_unlock(&revmap_trees_mutex);
497 }
498}
499
500/**
501 * irq_linear_revmap() - Find a linux irq from a hw irq number.
Grant Likely68700652012-02-14 14:06:53 -0700502 * @domain: domain owning this hardware interrupt
503 * @hwirq: hardware irq number in that domain space
Grant Likelycc79ca62012-02-16 01:37:49 -0700504 *
505 * This is a fast path, for use by irq controller code that uses linear
506 * revmaps. It does fallback to the slow path if the revmap doesn't exist
507 * yet and will create the revmap entry with appropriate locking
508 */
Grant Likely68700652012-02-14 14:06:53 -0700509unsigned int irq_linear_revmap(struct irq_domain *domain,
Grant Likelycc79ca62012-02-16 01:37:49 -0700510 irq_hw_number_t hwirq)
511{
512 unsigned int *revmap;
513
Grant Likely68700652012-02-14 14:06:53 -0700514 if (WARN_ON_ONCE(domain->revmap_type != IRQ_DOMAIN_MAP_LINEAR))
515 return irq_find_mapping(domain, hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700516
517 /* Check revmap bounds */
Grant Likely68700652012-02-14 14:06:53 -0700518 if (unlikely(hwirq >= domain->revmap_data.linear.size))
519 return irq_find_mapping(domain, hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700520
521 /* Check if revmap was allocated */
Grant Likely68700652012-02-14 14:06:53 -0700522 revmap = domain->revmap_data.linear.revmap;
Grant Likelycc79ca62012-02-16 01:37:49 -0700523 if (unlikely(revmap == NULL))
Grant Likely68700652012-02-14 14:06:53 -0700524 return irq_find_mapping(domain, hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700525
526 /* Fill up revmap with slow path if no mapping found */
Grant Likely03848372012-02-14 14:06:52 -0700527 if (unlikely(!revmap[hwirq]))
Grant Likely68700652012-02-14 14:06:53 -0700528 revmap[hwirq] = irq_find_mapping(domain, hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700529
530 return revmap[hwirq];
531}
532
533#ifdef CONFIG_VIRQ_DEBUG
534static int virq_debug_show(struct seq_file *m, void *private)
535{
536 unsigned long flags;
537 struct irq_desc *desc;
538 const char *p;
539 static const char none[] = "none";
540 void *data;
541 int i;
542
543 seq_printf(m, "%-5s %-7s %-15s %-18s %s\n", "virq", "hwirq",
Grant Likely68700652012-02-14 14:06:53 -0700544 "chip name", "chip data", "domain name");
Grant Likelycc79ca62012-02-16 01:37:49 -0700545
546 for (i = 1; i < nr_irqs; i++) {
547 desc = irq_to_desc(i);
548 if (!desc)
549 continue;
550
551 raw_spin_lock_irqsave(&desc->lock, flags);
552
553 if (desc->action && desc->action->handler) {
554 struct irq_chip *chip;
555
556 seq_printf(m, "%5d ", i);
557 seq_printf(m, "0x%05lx ", desc->irq_data.hwirq);
558
559 chip = irq_desc_get_chip(desc);
560 if (chip && chip->name)
561 p = chip->name;
562 else
563 p = none;
564 seq_printf(m, "%-15s ", p);
565
566 data = irq_desc_get_chip_data(desc);
567 seq_printf(m, "0x%16p ", data);
568
569 if (desc->irq_data.domain->of_node)
570 p = desc->irq_data.domain->of_node->full_name;
571 else
572 p = none;
573 seq_printf(m, "%s\n", p);
574 }
575
576 raw_spin_unlock_irqrestore(&desc->lock, flags);
577 }
578
579 return 0;
580}
581
582static int virq_debug_open(struct inode *inode, struct file *file)
583{
584 return single_open(file, virq_debug_show, inode->i_private);
585}
586
587static const struct file_operations virq_debug_fops = {
588 .open = virq_debug_open,
589 .read = seq_read,
590 .llseek = seq_lseek,
591 .release = single_release,
592};
593
594static int __init irq_debugfs_init(void)
595{
596 if (debugfs_create_file("virq_mapping", S_IRUGO, powerpc_debugfs_root,
597 NULL, &virq_debug_fops) == NULL)
598 return -ENOMEM;
599
600 return 0;
601}
602__initcall(irq_debugfs_init);
603#endif /* CONFIG_VIRQ_DEBUG */
604
605#else /* CONFIG_PPC */
606
Grant Likely08a543a2011-07-26 03:19:06 -0600607/**
608 * irq_domain_add() - Register an irq_domain
609 * @domain: ptr to initialized irq_domain structure
610 *
611 * Registers an irq_domain structure. The irq_domain must at a minimum be
612 * initialized with an ops structure pointer, and either a ->to_irq hook or
613 * a valid irq_base value. Everything else is optional.
614 */
615void irq_domain_add(struct irq_domain *domain)
616{
617 struct irq_data *d;
Rob Herring6d274302011-09-30 10:48:38 -0500618 int hwirq, irq;
Grant Likely08a543a2011-07-26 03:19:06 -0600619
620 /*
621 * This assumes that the irq_domain owner has already allocated
622 * the irq_descs. This block will be removed when support for dynamic
623 * allocation of irq_descs is added to irq_domain.
624 */
Rob Herring6d274302011-09-30 10:48:38 -0500625 irq_domain_for_each_irq(domain, hwirq, irq) {
626 d = irq_get_irq_data(irq);
Rob Herringeef24af2011-09-14 11:31:37 -0500627 if (!d) {
628 WARN(1, "error: assigning domain to non existant irq_desc");
629 return;
630 }
631 if (d->domain) {
Grant Likely08a543a2011-07-26 03:19:06 -0600632 /* things are broken; just report, don't clean up */
633 WARN(1, "error: irq_desc already assigned to a domain");
634 return;
635 }
636 d->domain = domain;
637 d->hwirq = hwirq;
638 }
639
640 mutex_lock(&irq_domain_mutex);
Grant Likely7bb69ba2012-02-14 14:06:48 -0700641 list_add(&domain->link, &irq_domain_list);
Grant Likely08a543a2011-07-26 03:19:06 -0600642 mutex_unlock(&irq_domain_mutex);
643}
644
645/**
646 * irq_domain_del() - Unregister an irq_domain
647 * @domain: ptr to registered irq_domain.
648 */
649void irq_domain_del(struct irq_domain *domain)
650{
651 struct irq_data *d;
Rob Herring6d274302011-09-30 10:48:38 -0500652 int hwirq, irq;
Grant Likely08a543a2011-07-26 03:19:06 -0600653
654 mutex_lock(&irq_domain_mutex);
Grant Likely7bb69ba2012-02-14 14:06:48 -0700655 list_del(&domain->link);
Grant Likely08a543a2011-07-26 03:19:06 -0600656 mutex_unlock(&irq_domain_mutex);
657
658 /* Clear the irq_domain assignments */
Rob Herring6d274302011-09-30 10:48:38 -0500659 irq_domain_for_each_irq(domain, hwirq, irq) {
660 d = irq_get_irq_data(irq);
Grant Likely08a543a2011-07-26 03:19:06 -0600661 d->domain = NULL;
662 }
663}
664
665#if defined(CONFIG_OF_IRQ)
666/**
667 * irq_create_of_mapping() - Map a linux irq number from a DT interrupt spec
668 *
669 * Used by the device tree interrupt mapping code to translate a device tree
670 * interrupt specifier to a valid linux irq number. Returns either a valid
671 * linux IRQ number or 0.
672 *
673 * When the caller no longer need the irq number returned by this function it
674 * should arrange to call irq_dispose_mapping().
675 */
676unsigned int irq_create_of_mapping(struct device_node *controller,
677 const u32 *intspec, unsigned int intsize)
678{
679 struct irq_domain *domain;
680 unsigned long hwirq;
681 unsigned int irq, type;
682 int rc = -EINVAL;
683
684 /* Find a domain which can translate the irq spec */
685 mutex_lock(&irq_domain_mutex);
Grant Likely7bb69ba2012-02-14 14:06:48 -0700686 list_for_each_entry(domain, &irq_domain_list, link) {
687 if (!domain->ops->xlate)
Grant Likely08a543a2011-07-26 03:19:06 -0600688 continue;
Grant Likely7bb69ba2012-02-14 14:06:48 -0700689 rc = domain->ops->xlate(domain, controller,
Grant Likely08a543a2011-07-26 03:19:06 -0600690 intspec, intsize, &hwirq, &type);
691 if (rc == 0)
692 break;
693 }
694 mutex_unlock(&irq_domain_mutex);
695
696 if (rc != 0)
697 return 0;
698
699 irq = irq_domain_to_irq(domain, hwirq);
700 if (type != IRQ_TYPE_NONE)
701 irq_set_irq_type(irq, type);
702 pr_debug("%s: mapped hwirq=%i to irq=%i, flags=%x\n",
703 controller->full_name, (int)hwirq, irq, type);
704 return irq;
705}
706EXPORT_SYMBOL_GPL(irq_create_of_mapping);
707
708/**
709 * irq_dispose_mapping() - Discard a mapping created by irq_create_of_mapping()
710 * @irq: linux irq number to be discarded
711 *
712 * Calling this function indicates the caller no longer needs a reference to
713 * the linux irq number returned by a prior call to irq_create_of_mapping().
714 */
715void irq_dispose_mapping(unsigned int irq)
716{
717 /*
718 * nothing yet; will be filled when support for dynamic allocation of
719 * irq_descs is added to irq_domain
720 */
721}
722EXPORT_SYMBOL_GPL(irq_dispose_mapping);
Grant Likely7e713302011-07-26 03:19:06 -0600723
Grant Likely7bb69ba2012-02-14 14:06:48 -0700724int irq_domain_simple_xlate(struct irq_domain *d,
Grant Likely7e713302011-07-26 03:19:06 -0600725 struct device_node *controller,
726 const u32 *intspec, unsigned int intsize,
727 unsigned long *out_hwirq, unsigned int *out_type)
728{
729 if (d->of_node != controller)
730 return -EINVAL;
731 if (intsize < 1)
732 return -EINVAL;
Rob Herring93797d82011-12-12 09:59:14 -0600733 if (d->nr_irq && ((intspec[0] < d->hwirq_base) ||
734 (intspec[0] >= d->hwirq_base + d->nr_irq)))
735 return -EINVAL;
Grant Likely7e713302011-07-26 03:19:06 -0600736
737 *out_hwirq = intspec[0];
738 *out_type = IRQ_TYPE_NONE;
739 if (intsize > 1)
740 *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK;
741 return 0;
742}
743
Grant Likely7e713302011-07-26 03:19:06 -0600744/**
745 * irq_domain_create_simple() - Set up a 'simple' translation range
746 */
747void irq_domain_add_simple(struct device_node *controller, int irq_base)
748{
749 struct irq_domain *domain;
750
751 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
752 if (!domain) {
753 WARN_ON(1);
754 return;
755 }
756
757 domain->irq_base = irq_base;
758 domain->of_node = of_node_get(controller);
759 domain->ops = &irq_domain_simple_ops;
760 irq_domain_add(domain);
761}
762EXPORT_SYMBOL_GPL(irq_domain_add_simple);
763
764void irq_domain_generate_simple(const struct of_device_id *match,
765 u64 phys_base, unsigned int irq_start)
766{
767 struct device_node *node;
Grant Likelye1964c52012-02-14 14:06:48 -0700768 pr_debug("looking for phys_base=%llx, irq_start=%i\n",
Grant Likely7e713302011-07-26 03:19:06 -0600769 (unsigned long long) phys_base, (int) irq_start);
770 node = of_find_matching_node_by_address(NULL, match, phys_base);
771 if (node)
772 irq_domain_add_simple(node, irq_start);
Grant Likely7e713302011-07-26 03:19:06 -0600773}
774EXPORT_SYMBOL_GPL(irq_domain_generate_simple);
Grant Likely08a543a2011-07-26 03:19:06 -0600775#endif /* CONFIG_OF_IRQ */
Jamie Ilesc87fb572011-12-14 23:43:16 +0100776
777struct irq_domain_ops irq_domain_simple_ops = {
778#ifdef CONFIG_OF_IRQ
Grant Likely7bb69ba2012-02-14 14:06:48 -0700779 .xlate = irq_domain_simple_xlate,
Jamie Ilesc87fb572011-12-14 23:43:16 +0100780#endif /* CONFIG_OF_IRQ */
781};
782EXPORT_SYMBOL_GPL(irq_domain_simple_ops);
Grant Likelycc79ca62012-02-16 01:37:49 -0700783
784#endif /* !CONFIG_PPC */