blob: acedba1a2651b03fae055090afb22deccd2e8738 [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
Grant Likelya8db8cf2012-02-14 14:06:54 -070016#define IRQ_DOMAIN_MAP_LEGACY 0 /* legacy 8259, gets irqs 1..15 */
17#define IRQ_DOMAIN_MAP_NOMAP 1 /* no fast reverse mapping */
18#define IRQ_DOMAIN_MAP_LINEAR 2 /* linear map of interrupts */
19#define IRQ_DOMAIN_MAP_TREE 3 /* radix tree */
20
Grant Likely08a543a2011-07-26 03:19:06 -060021static LIST_HEAD(irq_domain_list);
22static DEFINE_MUTEX(irq_domain_mutex);
23
Grant Likelycc79ca62012-02-16 01:37:49 -070024#ifdef CONFIG_PPC
25static DEFINE_MUTEX(revmap_trees_mutex);
26static unsigned int irq_virq_count = NR_IRQS;
Grant Likely68700652012-02-14 14:06:53 -070027static struct irq_domain *irq_default_domain;
Grant Likelycc79ca62012-02-16 01:37:49 -070028
Grant Likely68700652012-02-14 14:06:53 -070029static int default_irq_domain_match(struct irq_domain *d, struct device_node *np)
Grant Likelycc79ca62012-02-16 01:37:49 -070030{
Grant Likely68700652012-02-14 14:06:53 -070031 return d->of_node != NULL && d->of_node == np;
Grant Likelycc79ca62012-02-16 01:37:49 -070032}
33
34/**
Grant Likelya8db8cf2012-02-14 14:06:54 -070035 * irq_domain_alloc() - Allocate a new irq_domain data structure
Grant Likelycc79ca62012-02-16 01:37:49 -070036 * @of_node: optional device-tree node of the interrupt controller
37 * @revmap_type: type of reverse mapping to use
Grant Likely68700652012-02-14 14:06:53 -070038 * @ops: map/unmap domain callbacks
Grant Likelya8db8cf2012-02-14 14:06:54 -070039 * @host_data: Controller private data pointer
Grant Likelycc79ca62012-02-16 01:37:49 -070040 *
Grant Likelya8db8cf2012-02-14 14:06:54 -070041 * Allocates and initialize and irq_domain structure. Caller is expected to
42 * register allocated irq_domain with irq_domain_register(). Returns pointer
43 * to IRQ domain, or NULL on failure.
Grant Likelycc79ca62012-02-16 01:37:49 -070044 */
Grant Likelya8db8cf2012-02-14 14:06:54 -070045static struct irq_domain *irq_domain_alloc(struct device_node *of_node,
46 unsigned int revmap_type,
47 struct irq_domain_ops *ops,
48 void *host_data)
Grant Likelycc79ca62012-02-16 01:37:49 -070049{
Grant Likelya8db8cf2012-02-14 14:06:54 -070050 struct irq_domain *domain;
Grant Likelycc79ca62012-02-16 01:37:49 -070051
Grant Likelya8db8cf2012-02-14 14:06:54 -070052 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
53 if (WARN_ON(!domain))
Grant Likelycc79ca62012-02-16 01:37:49 -070054 return NULL;
55
56 /* Fill structure */
Grant Likely68700652012-02-14 14:06:53 -070057 domain->revmap_type = revmap_type;
Grant Likely68700652012-02-14 14:06:53 -070058 domain->ops = ops;
Grant Likelya8db8cf2012-02-14 14:06:54 -070059 domain->host_data = host_data;
Grant Likely68700652012-02-14 14:06:53 -070060 domain->of_node = of_node_get(of_node);
Grant Likelycc79ca62012-02-16 01:37:49 -070061
Grant Likely68700652012-02-14 14:06:53 -070062 if (domain->ops->match == NULL)
63 domain->ops->match = default_irq_domain_match;
Grant Likelycc79ca62012-02-16 01:37:49 -070064
Grant Likelya8db8cf2012-02-14 14:06:54 -070065 return domain;
66}
67
68static void irq_domain_add(struct irq_domain *domain)
69{
70 mutex_lock(&irq_domain_mutex);
71 list_add(&domain->link, &irq_domain_list);
72 mutex_unlock(&irq_domain_mutex);
73 pr_debug("irq: Allocated domain of type %d @0x%p\n",
74 domain->revmap_type, domain);
75}
76
77/**
78 * irq_domain_add_legacy() - Allocate and register a legacy revmap irq_domain.
79 * @of_node: pointer to interrupt controller's device tree node.
80 * @ops: map/unmap domain callbacks
81 * @host_data: Controller private data pointer
82 *
83 * Note: the map() callback will be called before this function returns
84 * for all legacy interrupts except 0 (which is always the invalid irq for
85 * a legacy controller).
86 */
87struct irq_domain *irq_domain_add_legacy(struct device_node *of_node,
88 struct irq_domain_ops *ops,
89 void *host_data)
90{
91 struct irq_domain *domain, *h;
92 unsigned int i;
93
94 domain = irq_domain_alloc(of_node, IRQ_DOMAIN_MAP_LEGACY, ops, host_data);
95 if (!domain)
96 return NULL;
97
Grant Likelycc79ca62012-02-16 01:37:49 -070098 mutex_lock(&irq_domain_mutex);
99 /* Make sure only one legacy controller can be created */
Grant Likelya8db8cf2012-02-14 14:06:54 -0700100 list_for_each_entry(h, &irq_domain_list, link) {
101 if (WARN_ON(h->revmap_type == IRQ_DOMAIN_MAP_LEGACY)) {
102 mutex_unlock(&irq_domain_mutex);
103 of_node_put(domain->of_node);
104 kfree(domain);
105 return NULL;
Grant Likelycc79ca62012-02-16 01:37:49 -0700106 }
107 }
Grant Likely68700652012-02-14 14:06:53 -0700108 list_add(&domain->link, &irq_domain_list);
Grant Likelycc79ca62012-02-16 01:37:49 -0700109 mutex_unlock(&irq_domain_mutex);
110
Grant Likelya8db8cf2012-02-14 14:06:54 -0700111 /* setup us as the domain for all legacy interrupts */
112 for (i = 1; i < NUM_ISA_INTERRUPTS; i++) {
113 struct irq_data *irq_data = irq_get_irq_data(i);
114 irq_data->hwirq = i;
115 irq_data->domain = domain;
Grant Likelycc79ca62012-02-16 01:37:49 -0700116
Grant Likelya8db8cf2012-02-14 14:06:54 -0700117 /* Legacy flags are left to default at this point,
118 * one can then use irq_create_mapping() to
119 * explicitly change them
120 */
121 ops->map(domain, i, i);
Grant Likelycc79ca62012-02-16 01:37:49 -0700122
Grant Likelya8db8cf2012-02-14 14:06:54 -0700123 /* Clear norequest flags */
124 irq_clear_status_flags(i, IRQ_NOREQUEST);
Grant Likelycc79ca62012-02-16 01:37:49 -0700125 }
Grant Likelya8db8cf2012-02-14 14:06:54 -0700126 return domain;
127}
Grant Likelycc79ca62012-02-16 01:37:49 -0700128
Grant Likelya8db8cf2012-02-14 14:06:54 -0700129/**
130 * irq_domain_add_linear() - Allocate and register a legacy revmap irq_domain.
131 * @of_node: pointer to interrupt controller's device tree node.
132 * @ops: map/unmap domain callbacks
133 * @host_data: Controller private data pointer
134 */
135struct irq_domain *irq_domain_add_linear(struct device_node *of_node,
136 unsigned int size,
137 struct irq_domain_ops *ops,
138 void *host_data)
139{
140 struct irq_domain *domain;
141 unsigned int *revmap;
Grant Likelycc79ca62012-02-16 01:37:49 -0700142
Grant Likelya8db8cf2012-02-14 14:06:54 -0700143 revmap = kzalloc(sizeof(*revmap) * size, GFP_KERNEL);
144 if (WARN_ON(!revmap))
145 return NULL;
146
147 domain = irq_domain_alloc(of_node, IRQ_DOMAIN_MAP_LINEAR, ops, host_data);
148 if (!domain) {
149 kfree(revmap);
150 return NULL;
151 }
152 domain->revmap_data.linear.size = size;
153 domain->revmap_data.linear.revmap = revmap;
154 irq_domain_add(domain);
155 return domain;
156}
157
158struct irq_domain *irq_domain_add_nomap(struct device_node *of_node,
159 struct irq_domain_ops *ops,
160 void *host_data)
161{
162 struct irq_domain *domain = irq_domain_alloc(of_node,
163 IRQ_DOMAIN_MAP_NOMAP, ops, host_data);
164 if (domain)
165 irq_domain_add(domain);
166 return domain;
167}
168
169/**
170 * irq_domain_add_tree()
171 * @of_node: pointer to interrupt controller's device tree node.
172 * @ops: map/unmap domain callbacks
173 *
174 * Note: The radix tree will be allocated later during boot automatically
175 * (the reverse mapping will use the slow path until that happens).
176 */
177struct irq_domain *irq_domain_add_tree(struct device_node *of_node,
178 struct irq_domain_ops *ops,
179 void *host_data)
180{
181 struct irq_domain *domain = irq_domain_alloc(of_node,
182 IRQ_DOMAIN_MAP_TREE, ops, host_data);
183 if (domain) {
184 INIT_RADIX_TREE(&domain->revmap_data.tree, GFP_KERNEL);
185 irq_domain_add(domain);
186 }
Grant Likely68700652012-02-14 14:06:53 -0700187 return domain;
Grant Likelycc79ca62012-02-16 01:37:49 -0700188}
189
190/**
191 * irq_find_host() - Locates a domain for a given device node
192 * @node: device-tree node of the interrupt controller
193 */
194struct irq_domain *irq_find_host(struct device_node *node)
195{
196 struct irq_domain *h, *found = NULL;
197
198 /* We might want to match the legacy controller last since
199 * it might potentially be set to match all interrupts in
200 * the absence of a device node. This isn't a problem so far
201 * yet though...
202 */
203 mutex_lock(&irq_domain_mutex);
204 list_for_each_entry(h, &irq_domain_list, link)
205 if (h->ops->match(h, node)) {
206 found = h;
207 break;
208 }
209 mutex_unlock(&irq_domain_mutex);
210 return found;
211}
212EXPORT_SYMBOL_GPL(irq_find_host);
213
214/**
215 * irq_set_default_host() - Set a "default" irq domain
Grant Likely68700652012-02-14 14:06:53 -0700216 * @domain: default domain pointer
Grant Likelycc79ca62012-02-16 01:37:49 -0700217 *
218 * For convenience, it's possible to set a "default" domain that will be used
219 * whenever NULL is passed to irq_create_mapping(). It makes life easier for
220 * platforms that want to manipulate a few hard coded interrupt numbers that
221 * aren't properly represented in the device-tree.
222 */
Grant Likely68700652012-02-14 14:06:53 -0700223void irq_set_default_host(struct irq_domain *domain)
Grant Likelycc79ca62012-02-16 01:37:49 -0700224{
Grant Likely68700652012-02-14 14:06:53 -0700225 pr_debug("irq: Default domain set to @0x%p\n", domain);
Grant Likelycc79ca62012-02-16 01:37:49 -0700226
Grant Likely68700652012-02-14 14:06:53 -0700227 irq_default_domain = domain;
Grant Likelycc79ca62012-02-16 01:37:49 -0700228}
229
230/**
231 * irq_set_virq_count() - Set the maximum number of linux irqs
232 * @count: number of linux irqs, capped with NR_IRQS
233 *
234 * This is mainly for use by platforms like iSeries who want to program
235 * the virtual irq number in the controller to avoid the reverse mapping
236 */
237void irq_set_virq_count(unsigned int count)
238{
239 pr_debug("irq: Trying to set virq count to %d\n", count);
240
241 BUG_ON(count < NUM_ISA_INTERRUPTS);
242 if (count < NR_IRQS)
243 irq_virq_count = count;
244}
245
Grant Likely68700652012-02-14 14:06:53 -0700246static int irq_setup_virq(struct irq_domain *domain, unsigned int virq,
Grant Likelycc79ca62012-02-16 01:37:49 -0700247 irq_hw_number_t hwirq)
248{
249 struct irq_data *irq_data = irq_get_irq_data(virq);
250
251 irq_data->hwirq = hwirq;
Grant Likely68700652012-02-14 14:06:53 -0700252 irq_data->domain = domain;
253 if (domain->ops->map(domain, virq, hwirq)) {
Grant Likelycc79ca62012-02-16 01:37:49 -0700254 pr_debug("irq: -> mapping failed, freeing\n");
255 irq_data->domain = NULL;
256 irq_data->hwirq = 0;
257 return -1;
258 }
259
260 irq_clear_status_flags(virq, IRQ_NOREQUEST);
261
262 return 0;
263}
264
265/**
266 * irq_create_direct_mapping() - Allocate an irq for direct mapping
Grant Likely68700652012-02-14 14:06:53 -0700267 * @domain: domain to allocate the irq for or NULL for default domain
Grant Likelycc79ca62012-02-16 01:37:49 -0700268 *
269 * This routine is used for irq controllers which can choose the hardware
270 * interrupt numbers they generate. In such a case it's simplest to use
271 * the linux irq as the hardware interrupt number.
272 */
Grant Likely68700652012-02-14 14:06:53 -0700273unsigned int irq_create_direct_mapping(struct irq_domain *domain)
Grant Likelycc79ca62012-02-16 01:37:49 -0700274{
275 unsigned int virq;
276
Grant Likely68700652012-02-14 14:06:53 -0700277 if (domain == NULL)
278 domain = irq_default_domain;
Grant Likelycc79ca62012-02-16 01:37:49 -0700279
Grant Likely68700652012-02-14 14:06:53 -0700280 BUG_ON(domain == NULL);
281 WARN_ON(domain->revmap_type != IRQ_DOMAIN_MAP_NOMAP);
Grant Likelycc79ca62012-02-16 01:37:49 -0700282
283 virq = irq_alloc_desc_from(1, 0);
Grant Likely03848372012-02-14 14:06:52 -0700284 if (!virq) {
Grant Likelycc79ca62012-02-16 01:37:49 -0700285 pr_debug("irq: create_direct virq allocation failed\n");
Grant Likely03848372012-02-14 14:06:52 -0700286 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700287 }
288 if (virq >= irq_virq_count) {
289 pr_err("ERROR: no free irqs available below %i maximum\n",
290 irq_virq_count);
291 irq_free_desc(virq);
292 return 0;
293 }
294
295 pr_debug("irq: create_direct obtained virq %d\n", virq);
296
Grant Likely68700652012-02-14 14:06:53 -0700297 if (irq_setup_virq(domain, virq, virq)) {
Grant Likelycc79ca62012-02-16 01:37:49 -0700298 irq_free_desc(virq);
Grant Likely03848372012-02-14 14:06:52 -0700299 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700300 }
301
302 return virq;
303}
304
305/**
306 * irq_create_mapping() - Map a hardware interrupt into linux irq space
Grant Likely68700652012-02-14 14:06:53 -0700307 * @domain: domain owning this hardware interrupt or NULL for default domain
308 * @hwirq: hardware irq number in that domain space
Grant Likelycc79ca62012-02-16 01:37:49 -0700309 *
310 * Only one mapping per hardware interrupt is permitted. Returns a linux
311 * irq number.
312 * If the sense/trigger is to be specified, set_irq_type() should be called
313 * on the number returned from that call.
314 */
Grant Likely68700652012-02-14 14:06:53 -0700315unsigned int irq_create_mapping(struct irq_domain *domain,
Grant Likelycc79ca62012-02-16 01:37:49 -0700316 irq_hw_number_t hwirq)
317{
318 unsigned int virq, hint;
319
Grant Likely68700652012-02-14 14:06:53 -0700320 pr_debug("irq: irq_create_mapping(0x%p, 0x%lx)\n", domain, hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700321
Grant Likely68700652012-02-14 14:06:53 -0700322 /* Look for default domain if nececssary */
323 if (domain == NULL)
324 domain = irq_default_domain;
325 if (domain == NULL) {
Grant Likelycc79ca62012-02-16 01:37:49 -0700326 printk(KERN_WARNING "irq_create_mapping called for"
Grant Likely68700652012-02-14 14:06:53 -0700327 " NULL domain, hwirq=%lx\n", hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700328 WARN_ON(1);
Grant Likely03848372012-02-14 14:06:52 -0700329 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700330 }
Grant Likely68700652012-02-14 14:06:53 -0700331 pr_debug("irq: -> using domain @%p\n", domain);
Grant Likelycc79ca62012-02-16 01:37:49 -0700332
333 /* Check if mapping already exists */
Grant Likely68700652012-02-14 14:06:53 -0700334 virq = irq_find_mapping(domain, hwirq);
Grant Likely03848372012-02-14 14:06:52 -0700335 if (virq) {
Grant Likelycc79ca62012-02-16 01:37:49 -0700336 pr_debug("irq: -> existing mapping on virq %d\n", virq);
337 return virq;
338 }
339
340 /* Get a virtual interrupt number */
Grant Likely68700652012-02-14 14:06:53 -0700341 if (domain->revmap_type == IRQ_DOMAIN_MAP_LEGACY) {
Grant Likelycc79ca62012-02-16 01:37:49 -0700342 /* Handle legacy */
343 virq = (unsigned int)hwirq;
344 if (virq == 0 || virq >= NUM_ISA_INTERRUPTS)
Grant Likely03848372012-02-14 14:06:52 -0700345 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700346 return virq;
347 } else {
348 /* Allocate a virtual interrupt number */
349 hint = hwirq % irq_virq_count;
350 if (hint == 0)
351 hint++;
352 virq = irq_alloc_desc_from(hint, 0);
353 if (!virq)
354 virq = irq_alloc_desc_from(1, 0);
Grant Likely03848372012-02-14 14:06:52 -0700355 if (!virq) {
Grant Likelycc79ca62012-02-16 01:37:49 -0700356 pr_debug("irq: -> virq allocation failed\n");
Grant Likely03848372012-02-14 14:06:52 -0700357 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700358 }
359 }
360
Grant Likely68700652012-02-14 14:06:53 -0700361 if (irq_setup_virq(domain, virq, hwirq)) {
362 if (domain->revmap_type != IRQ_DOMAIN_MAP_LEGACY)
Grant Likelycc79ca62012-02-16 01:37:49 -0700363 irq_free_desc(virq);
Grant Likely03848372012-02-14 14:06:52 -0700364 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700365 }
366
Grant Likely68700652012-02-14 14:06:53 -0700367 pr_debug("irq: irq %lu on domain %s mapped to virtual irq %u\n",
368 hwirq, domain->of_node ? domain->of_node->full_name : "null", virq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700369
370 return virq;
371}
372EXPORT_SYMBOL_GPL(irq_create_mapping);
373
374unsigned int irq_create_of_mapping(struct device_node *controller,
375 const u32 *intspec, unsigned int intsize)
376{
Grant Likely68700652012-02-14 14:06:53 -0700377 struct irq_domain *domain;
Grant Likelycc79ca62012-02-16 01:37:49 -0700378 irq_hw_number_t hwirq;
379 unsigned int type = IRQ_TYPE_NONE;
380 unsigned int virq;
381
Grant Likely68700652012-02-14 14:06:53 -0700382 domain = controller ? irq_find_host(controller) : irq_default_domain;
383 if (!domain) {
384 printk(KERN_WARNING "irq: no irq domain found for %s !\n",
Grant Likelycc79ca62012-02-16 01:37:49 -0700385 controller->full_name);
Grant Likely03848372012-02-14 14:06:52 -0700386 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700387 }
388
Grant Likely68700652012-02-14 14:06:53 -0700389 /* If domain has no translation, then we assume interrupt line */
390 if (domain->ops->xlate == NULL)
Grant Likelycc79ca62012-02-16 01:37:49 -0700391 hwirq = intspec[0];
392 else {
Grant Likely68700652012-02-14 14:06:53 -0700393 if (domain->ops->xlate(domain, controller, intspec, intsize,
Grant Likelycc79ca62012-02-16 01:37:49 -0700394 &hwirq, &type))
Grant Likely03848372012-02-14 14:06:52 -0700395 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700396 }
397
398 /* Create mapping */
Grant Likely68700652012-02-14 14:06:53 -0700399 virq = irq_create_mapping(domain, hwirq);
Grant Likely03848372012-02-14 14:06:52 -0700400 if (!virq)
Grant Likelycc79ca62012-02-16 01:37:49 -0700401 return virq;
402
403 /* Set type if specified and different than the current one */
404 if (type != IRQ_TYPE_NONE &&
405 type != (irqd_get_trigger_type(irq_get_irq_data(virq))))
406 irq_set_irq_type(virq, type);
407 return virq;
408}
409EXPORT_SYMBOL_GPL(irq_create_of_mapping);
410
411/**
412 * irq_dispose_mapping() - Unmap an interrupt
413 * @virq: linux irq number of the interrupt to unmap
414 */
415void irq_dispose_mapping(unsigned int virq)
416{
417 struct irq_data *irq_data = irq_get_irq_data(virq);
Grant Likely68700652012-02-14 14:06:53 -0700418 struct irq_domain *domain;
Grant Likelycc79ca62012-02-16 01:37:49 -0700419 irq_hw_number_t hwirq;
420
Grant Likely03848372012-02-14 14:06:52 -0700421 if (!virq || !irq_data)
Grant Likelycc79ca62012-02-16 01:37:49 -0700422 return;
423
Grant Likely68700652012-02-14 14:06:53 -0700424 domain = irq_data->domain;
425 if (WARN_ON(domain == NULL))
Grant Likelycc79ca62012-02-16 01:37:49 -0700426 return;
427
428 /* Never unmap legacy interrupts */
Grant Likely68700652012-02-14 14:06:53 -0700429 if (domain->revmap_type == IRQ_DOMAIN_MAP_LEGACY)
Grant Likelycc79ca62012-02-16 01:37:49 -0700430 return;
431
432 irq_set_status_flags(virq, IRQ_NOREQUEST);
433
434 /* remove chip and handler */
435 irq_set_chip_and_handler(virq, NULL, NULL);
436
437 /* Make sure it's completed */
438 synchronize_irq(virq);
439
440 /* Tell the PIC about it */
Grant Likely68700652012-02-14 14:06:53 -0700441 if (domain->ops->unmap)
442 domain->ops->unmap(domain, virq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700443 smp_mb();
444
445 /* Clear reverse map */
446 hwirq = irq_data->hwirq;
Grant Likely68700652012-02-14 14:06:53 -0700447 switch(domain->revmap_type) {
Grant Likelycc79ca62012-02-16 01:37:49 -0700448 case IRQ_DOMAIN_MAP_LINEAR:
Grant Likely68700652012-02-14 14:06:53 -0700449 if (hwirq < domain->revmap_data.linear.size)
450 domain->revmap_data.linear.revmap[hwirq] = 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700451 break;
452 case IRQ_DOMAIN_MAP_TREE:
453 mutex_lock(&revmap_trees_mutex);
Grant Likely68700652012-02-14 14:06:53 -0700454 radix_tree_delete(&domain->revmap_data.tree, hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700455 mutex_unlock(&revmap_trees_mutex);
456 break;
457 }
458
Grant Likelycc79ca62012-02-16 01:37:49 -0700459 irq_free_desc(virq);
460}
461EXPORT_SYMBOL_GPL(irq_dispose_mapping);
462
463/**
464 * irq_find_mapping() - Find a linux irq from an hw irq number.
Grant Likely68700652012-02-14 14:06:53 -0700465 * @domain: domain owning this hardware interrupt
466 * @hwirq: hardware irq number in that domain space
Grant Likelycc79ca62012-02-16 01:37:49 -0700467 *
468 * This is a slow path, for use by generic code. It's expected that an
469 * irq controller implementation directly calls the appropriate low level
470 * mapping function.
471 */
Grant Likely68700652012-02-14 14:06:53 -0700472unsigned int irq_find_mapping(struct irq_domain *domain,
Grant Likelycc79ca62012-02-16 01:37:49 -0700473 irq_hw_number_t hwirq)
474{
475 unsigned int i;
476 unsigned int hint = hwirq % irq_virq_count;
477
Grant Likely68700652012-02-14 14:06:53 -0700478 /* Look for default domain if nececssary */
479 if (domain == NULL)
480 domain = irq_default_domain;
481 if (domain == NULL)
Grant Likely03848372012-02-14 14:06:52 -0700482 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700483
484 /* legacy -> bail early */
Grant Likely68700652012-02-14 14:06:53 -0700485 if (domain->revmap_type == IRQ_DOMAIN_MAP_LEGACY)
Grant Likelycc79ca62012-02-16 01:37:49 -0700486 return hwirq;
487
488 /* Slow path does a linear search of the map */
489 if (hint == 0)
490 hint = 1;
491 i = hint;
492 do {
493 struct irq_data *data = irq_get_irq_data(i);
Grant Likely68700652012-02-14 14:06:53 -0700494 if (data && (data->domain == domain) && (data->hwirq == hwirq))
Grant Likelycc79ca62012-02-16 01:37:49 -0700495 return i;
496 i++;
497 if (i >= irq_virq_count)
498 i = 1;
499 } while(i != hint);
Grant Likely03848372012-02-14 14:06:52 -0700500 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700501}
502EXPORT_SYMBOL_GPL(irq_find_mapping);
503
504/**
505 * irq_radix_revmap_lookup() - Find a linux irq from a hw irq number.
Grant Likely68700652012-02-14 14:06:53 -0700506 * @domain: domain owning this hardware interrupt
507 * @hwirq: hardware irq number in that domain space
Grant Likelycc79ca62012-02-16 01:37:49 -0700508 *
509 * This is a fast path, for use by irq controller code that uses radix tree
510 * revmaps
511 */
Grant Likely68700652012-02-14 14:06:53 -0700512unsigned int irq_radix_revmap_lookup(struct irq_domain *domain,
Grant Likelycc79ca62012-02-16 01:37:49 -0700513 irq_hw_number_t hwirq)
514{
515 struct irq_data *irq_data;
516
Grant Likely68700652012-02-14 14:06:53 -0700517 if (WARN_ON_ONCE(domain->revmap_type != IRQ_DOMAIN_MAP_TREE))
518 return irq_find_mapping(domain, hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700519
520 /*
521 * Freeing an irq can delete nodes along the path to
522 * do the lookup via call_rcu.
523 */
524 rcu_read_lock();
Grant Likely68700652012-02-14 14:06:53 -0700525 irq_data = radix_tree_lookup(&domain->revmap_data.tree, hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700526 rcu_read_unlock();
527
528 /*
529 * If found in radix tree, then fine.
530 * Else fallback to linear lookup - this should not happen in practice
531 * as it means that we failed to insert the node in the radix tree.
532 */
Grant Likely68700652012-02-14 14:06:53 -0700533 return irq_data ? irq_data->irq : irq_find_mapping(domain, hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700534}
535
536/**
537 * irq_radix_revmap_insert() - Insert a hw irq to linux irq number mapping.
Grant Likely68700652012-02-14 14:06:53 -0700538 * @domain: domain owning this hardware interrupt
Grant Likelycc79ca62012-02-16 01:37:49 -0700539 * @virq: linux irq number
Grant Likely68700652012-02-14 14:06:53 -0700540 * @hwirq: hardware irq number in that domain space
Grant Likelycc79ca62012-02-16 01:37:49 -0700541 *
542 * This is for use by irq controllers that use a radix tree reverse
543 * mapping for fast lookup.
544 */
Grant Likely68700652012-02-14 14:06:53 -0700545void irq_radix_revmap_insert(struct irq_domain *domain, unsigned int virq,
Grant Likelycc79ca62012-02-16 01:37:49 -0700546 irq_hw_number_t hwirq)
547{
548 struct irq_data *irq_data = irq_get_irq_data(virq);
549
Grant Likely68700652012-02-14 14:06:53 -0700550 if (WARN_ON(domain->revmap_type != IRQ_DOMAIN_MAP_TREE))
Grant Likelycc79ca62012-02-16 01:37:49 -0700551 return;
552
Grant Likely03848372012-02-14 14:06:52 -0700553 if (virq) {
Grant Likelycc79ca62012-02-16 01:37:49 -0700554 mutex_lock(&revmap_trees_mutex);
Grant Likely68700652012-02-14 14:06:53 -0700555 radix_tree_insert(&domain->revmap_data.tree, hwirq, irq_data);
Grant Likelycc79ca62012-02-16 01:37:49 -0700556 mutex_unlock(&revmap_trees_mutex);
557 }
558}
559
560/**
561 * irq_linear_revmap() - Find a linux irq from a hw irq number.
Grant Likely68700652012-02-14 14:06:53 -0700562 * @domain: domain owning this hardware interrupt
563 * @hwirq: hardware irq number in that domain space
Grant Likelycc79ca62012-02-16 01:37:49 -0700564 *
565 * This is a fast path, for use by irq controller code that uses linear
566 * revmaps. It does fallback to the slow path if the revmap doesn't exist
567 * yet and will create the revmap entry with appropriate locking
568 */
Grant Likely68700652012-02-14 14:06:53 -0700569unsigned int irq_linear_revmap(struct irq_domain *domain,
Grant Likelycc79ca62012-02-16 01:37:49 -0700570 irq_hw_number_t hwirq)
571{
572 unsigned int *revmap;
573
Grant Likely68700652012-02-14 14:06:53 -0700574 if (WARN_ON_ONCE(domain->revmap_type != IRQ_DOMAIN_MAP_LINEAR))
575 return irq_find_mapping(domain, hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700576
577 /* Check revmap bounds */
Grant Likely68700652012-02-14 14:06:53 -0700578 if (unlikely(hwirq >= domain->revmap_data.linear.size))
579 return irq_find_mapping(domain, hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700580
581 /* Check if revmap was allocated */
Grant Likely68700652012-02-14 14:06:53 -0700582 revmap = domain->revmap_data.linear.revmap;
Grant Likelycc79ca62012-02-16 01:37:49 -0700583 if (unlikely(revmap == NULL))
Grant Likely68700652012-02-14 14:06:53 -0700584 return irq_find_mapping(domain, hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700585
586 /* Fill up revmap with slow path if no mapping found */
Grant Likely03848372012-02-14 14:06:52 -0700587 if (unlikely(!revmap[hwirq]))
Grant Likely68700652012-02-14 14:06:53 -0700588 revmap[hwirq] = irq_find_mapping(domain, hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700589
590 return revmap[hwirq];
591}
592
593#ifdef CONFIG_VIRQ_DEBUG
594static int virq_debug_show(struct seq_file *m, void *private)
595{
596 unsigned long flags;
597 struct irq_desc *desc;
598 const char *p;
599 static const char none[] = "none";
600 void *data;
601 int i;
602
603 seq_printf(m, "%-5s %-7s %-15s %-18s %s\n", "virq", "hwirq",
Grant Likely68700652012-02-14 14:06:53 -0700604 "chip name", "chip data", "domain name");
Grant Likelycc79ca62012-02-16 01:37:49 -0700605
606 for (i = 1; i < nr_irqs; i++) {
607 desc = irq_to_desc(i);
608 if (!desc)
609 continue;
610
611 raw_spin_lock_irqsave(&desc->lock, flags);
612
613 if (desc->action && desc->action->handler) {
614 struct irq_chip *chip;
615
616 seq_printf(m, "%5d ", i);
617 seq_printf(m, "0x%05lx ", desc->irq_data.hwirq);
618
619 chip = irq_desc_get_chip(desc);
620 if (chip && chip->name)
621 p = chip->name;
622 else
623 p = none;
624 seq_printf(m, "%-15s ", p);
625
626 data = irq_desc_get_chip_data(desc);
627 seq_printf(m, "0x%16p ", data);
628
629 if (desc->irq_data.domain->of_node)
630 p = desc->irq_data.domain->of_node->full_name;
631 else
632 p = none;
633 seq_printf(m, "%s\n", p);
634 }
635
636 raw_spin_unlock_irqrestore(&desc->lock, flags);
637 }
638
639 return 0;
640}
641
642static int virq_debug_open(struct inode *inode, struct file *file)
643{
644 return single_open(file, virq_debug_show, inode->i_private);
645}
646
647static const struct file_operations virq_debug_fops = {
648 .open = virq_debug_open,
649 .read = seq_read,
650 .llseek = seq_lseek,
651 .release = single_release,
652};
653
654static int __init irq_debugfs_init(void)
655{
656 if (debugfs_create_file("virq_mapping", S_IRUGO, powerpc_debugfs_root,
657 NULL, &virq_debug_fops) == NULL)
658 return -ENOMEM;
659
660 return 0;
661}
662__initcall(irq_debugfs_init);
663#endif /* CONFIG_VIRQ_DEBUG */
664
665#else /* CONFIG_PPC */
666
Grant Likely08a543a2011-07-26 03:19:06 -0600667/**
668 * irq_domain_add() - Register an irq_domain
669 * @domain: ptr to initialized irq_domain structure
670 *
671 * Registers an irq_domain structure. The irq_domain must at a minimum be
672 * initialized with an ops structure pointer, and either a ->to_irq hook or
673 * a valid irq_base value. Everything else is optional.
674 */
675void irq_domain_add(struct irq_domain *domain)
676{
677 struct irq_data *d;
Rob Herring6d274302011-09-30 10:48:38 -0500678 int hwirq, irq;
Grant Likely08a543a2011-07-26 03:19:06 -0600679
680 /*
681 * This assumes that the irq_domain owner has already allocated
682 * the irq_descs. This block will be removed when support for dynamic
683 * allocation of irq_descs is added to irq_domain.
684 */
Rob Herring6d274302011-09-30 10:48:38 -0500685 irq_domain_for_each_irq(domain, hwirq, irq) {
686 d = irq_get_irq_data(irq);
Rob Herringeef24af2011-09-14 11:31:37 -0500687 if (!d) {
688 WARN(1, "error: assigning domain to non existant irq_desc");
689 return;
690 }
691 if (d->domain) {
Grant Likely08a543a2011-07-26 03:19:06 -0600692 /* things are broken; just report, don't clean up */
693 WARN(1, "error: irq_desc already assigned to a domain");
694 return;
695 }
696 d->domain = domain;
697 d->hwirq = hwirq;
698 }
699
700 mutex_lock(&irq_domain_mutex);
Grant Likely7bb69ba2012-02-14 14:06:48 -0700701 list_add(&domain->link, &irq_domain_list);
Grant Likely08a543a2011-07-26 03:19:06 -0600702 mutex_unlock(&irq_domain_mutex);
703}
704
705/**
706 * irq_domain_del() - Unregister an irq_domain
707 * @domain: ptr to registered irq_domain.
708 */
709void irq_domain_del(struct irq_domain *domain)
710{
711 struct irq_data *d;
Rob Herring6d274302011-09-30 10:48:38 -0500712 int hwirq, irq;
Grant Likely08a543a2011-07-26 03:19:06 -0600713
714 mutex_lock(&irq_domain_mutex);
Grant Likely7bb69ba2012-02-14 14:06:48 -0700715 list_del(&domain->link);
Grant Likely08a543a2011-07-26 03:19:06 -0600716 mutex_unlock(&irq_domain_mutex);
717
718 /* Clear the irq_domain assignments */
Rob Herring6d274302011-09-30 10:48:38 -0500719 irq_domain_for_each_irq(domain, hwirq, irq) {
720 d = irq_get_irq_data(irq);
Grant Likely08a543a2011-07-26 03:19:06 -0600721 d->domain = NULL;
722 }
723}
724
725#if defined(CONFIG_OF_IRQ)
726/**
727 * irq_create_of_mapping() - Map a linux irq number from a DT interrupt spec
728 *
729 * Used by the device tree interrupt mapping code to translate a device tree
730 * interrupt specifier to a valid linux irq number. Returns either a valid
731 * linux IRQ number or 0.
732 *
733 * When the caller no longer need the irq number returned by this function it
734 * should arrange to call irq_dispose_mapping().
735 */
736unsigned int irq_create_of_mapping(struct device_node *controller,
737 const u32 *intspec, unsigned int intsize)
738{
739 struct irq_domain *domain;
740 unsigned long hwirq;
741 unsigned int irq, type;
742 int rc = -EINVAL;
743
744 /* Find a domain which can translate the irq spec */
745 mutex_lock(&irq_domain_mutex);
Grant Likely7bb69ba2012-02-14 14:06:48 -0700746 list_for_each_entry(domain, &irq_domain_list, link) {
747 if (!domain->ops->xlate)
Grant Likely08a543a2011-07-26 03:19:06 -0600748 continue;
Grant Likely7bb69ba2012-02-14 14:06:48 -0700749 rc = domain->ops->xlate(domain, controller,
Grant Likely08a543a2011-07-26 03:19:06 -0600750 intspec, intsize, &hwirq, &type);
751 if (rc == 0)
752 break;
753 }
754 mutex_unlock(&irq_domain_mutex);
755
756 if (rc != 0)
757 return 0;
758
759 irq = irq_domain_to_irq(domain, hwirq);
760 if (type != IRQ_TYPE_NONE)
761 irq_set_irq_type(irq, type);
762 pr_debug("%s: mapped hwirq=%i to irq=%i, flags=%x\n",
763 controller->full_name, (int)hwirq, irq, type);
764 return irq;
765}
766EXPORT_SYMBOL_GPL(irq_create_of_mapping);
767
768/**
769 * irq_dispose_mapping() - Discard a mapping created by irq_create_of_mapping()
770 * @irq: linux irq number to be discarded
771 *
772 * Calling this function indicates the caller no longer needs a reference to
773 * the linux irq number returned by a prior call to irq_create_of_mapping().
774 */
775void irq_dispose_mapping(unsigned int irq)
776{
777 /*
778 * nothing yet; will be filled when support for dynamic allocation of
779 * irq_descs is added to irq_domain
780 */
781}
782EXPORT_SYMBOL_GPL(irq_dispose_mapping);
Grant Likely7e713302011-07-26 03:19:06 -0600783
Grant Likely7bb69ba2012-02-14 14:06:48 -0700784int irq_domain_simple_xlate(struct irq_domain *d,
Grant Likely7e713302011-07-26 03:19:06 -0600785 struct device_node *controller,
786 const u32 *intspec, unsigned int intsize,
787 unsigned long *out_hwirq, unsigned int *out_type)
788{
789 if (d->of_node != controller)
790 return -EINVAL;
791 if (intsize < 1)
792 return -EINVAL;
Rob Herring93797d82011-12-12 09:59:14 -0600793 if (d->nr_irq && ((intspec[0] < d->hwirq_base) ||
794 (intspec[0] >= d->hwirq_base + d->nr_irq)))
795 return -EINVAL;
Grant Likely7e713302011-07-26 03:19:06 -0600796
797 *out_hwirq = intspec[0];
798 *out_type = IRQ_TYPE_NONE;
799 if (intsize > 1)
800 *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK;
801 return 0;
802}
803
Grant Likely7e713302011-07-26 03:19:06 -0600804/**
805 * irq_domain_create_simple() - Set up a 'simple' translation range
806 */
807void irq_domain_add_simple(struct device_node *controller, int irq_base)
808{
809 struct irq_domain *domain;
810
811 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
812 if (!domain) {
813 WARN_ON(1);
814 return;
815 }
816
817 domain->irq_base = irq_base;
818 domain->of_node = of_node_get(controller);
819 domain->ops = &irq_domain_simple_ops;
820 irq_domain_add(domain);
821}
822EXPORT_SYMBOL_GPL(irq_domain_add_simple);
823
824void irq_domain_generate_simple(const struct of_device_id *match,
825 u64 phys_base, unsigned int irq_start)
826{
827 struct device_node *node;
Grant Likelye1964c52012-02-14 14:06:48 -0700828 pr_debug("looking for phys_base=%llx, irq_start=%i\n",
Grant Likely7e713302011-07-26 03:19:06 -0600829 (unsigned long long) phys_base, (int) irq_start);
830 node = of_find_matching_node_by_address(NULL, match, phys_base);
831 if (node)
832 irq_domain_add_simple(node, irq_start);
Grant Likely7e713302011-07-26 03:19:06 -0600833}
834EXPORT_SYMBOL_GPL(irq_domain_generate_simple);
Grant Likely08a543a2011-07-26 03:19:06 -0600835#endif /* CONFIG_OF_IRQ */
Jamie Ilesc87fb572011-12-14 23:43:16 +0100836
837struct irq_domain_ops irq_domain_simple_ops = {
838#ifdef CONFIG_OF_IRQ
Grant Likely7bb69ba2012-02-14 14:06:48 -0700839 .xlate = irq_domain_simple_xlate,
Jamie Ilesc87fb572011-12-14 23:43:16 +0100840#endif /* CONFIG_OF_IRQ */
841};
842EXPORT_SYMBOL_GPL(irq_domain_simple_ops);
Grant Likelycc79ca62012-02-16 01:37:49 -0700843
844#endif /* !CONFIG_PPC */