blob: c6740d72073ea4ffa4c97edb7b54d6ef45e2d46b [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 Likely1bc04f22012-02-14 14:06:55 -070016#define IRQ_DOMAIN_MAP_LEGACY 0 /* driver allocated fixed range of irqs.
17 * ie. legacy 8259, gets irqs 1..15 */
Grant Likelya8db8cf2012-02-14 14:06:54 -070018#define IRQ_DOMAIN_MAP_NOMAP 1 /* no fast reverse mapping */
19#define IRQ_DOMAIN_MAP_LINEAR 2 /* linear map of interrupts */
20#define IRQ_DOMAIN_MAP_TREE 3 /* radix tree */
21
Grant Likely08a543a2011-07-26 03:19:06 -060022static LIST_HEAD(irq_domain_list);
23static DEFINE_MUTEX(irq_domain_mutex);
24
Grant Likelycc79ca62012-02-16 01:37:49 -070025#ifdef CONFIG_PPC
26static DEFINE_MUTEX(revmap_trees_mutex);
27static unsigned int irq_virq_count = NR_IRQS;
Grant Likely68700652012-02-14 14:06:53 -070028static struct irq_domain *irq_default_domain;
Grant Likelycc79ca62012-02-16 01:37:49 -070029
Grant Likely68700652012-02-14 14:06:53 -070030static int default_irq_domain_match(struct irq_domain *d, struct device_node *np)
Grant Likelycc79ca62012-02-16 01:37:49 -070031{
Grant Likely68700652012-02-14 14:06:53 -070032 return d->of_node != NULL && d->of_node == np;
Grant Likelycc79ca62012-02-16 01:37:49 -070033}
34
35/**
Grant Likelya8db8cf2012-02-14 14:06:54 -070036 * irq_domain_alloc() - Allocate a new irq_domain data structure
Grant Likelycc79ca62012-02-16 01:37:49 -070037 * @of_node: optional device-tree node of the interrupt controller
38 * @revmap_type: type of reverse mapping to use
Grant Likely68700652012-02-14 14:06:53 -070039 * @ops: map/unmap domain callbacks
Grant Likelya8db8cf2012-02-14 14:06:54 -070040 * @host_data: Controller private data pointer
Grant Likelycc79ca62012-02-16 01:37:49 -070041 *
Grant Likelya8db8cf2012-02-14 14:06:54 -070042 * Allocates and initialize and irq_domain structure. Caller is expected to
43 * register allocated irq_domain with irq_domain_register(). Returns pointer
44 * to IRQ domain, or NULL on failure.
Grant Likelycc79ca62012-02-16 01:37:49 -070045 */
Grant Likelya8db8cf2012-02-14 14:06:54 -070046static struct irq_domain *irq_domain_alloc(struct device_node *of_node,
47 unsigned int revmap_type,
48 struct irq_domain_ops *ops,
49 void *host_data)
Grant Likelycc79ca62012-02-16 01:37:49 -070050{
Grant Likelya8db8cf2012-02-14 14:06:54 -070051 struct irq_domain *domain;
Grant Likelycc79ca62012-02-16 01:37:49 -070052
Grant Likelya8db8cf2012-02-14 14:06:54 -070053 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
54 if (WARN_ON(!domain))
Grant Likelycc79ca62012-02-16 01:37:49 -070055 return NULL;
56
57 /* Fill structure */
Grant Likely68700652012-02-14 14:06:53 -070058 domain->revmap_type = revmap_type;
Grant Likely68700652012-02-14 14:06:53 -070059 domain->ops = ops;
Grant Likelya8db8cf2012-02-14 14:06:54 -070060 domain->host_data = host_data;
Grant Likely68700652012-02-14 14:06:53 -070061 domain->of_node = of_node_get(of_node);
Grant Likelycc79ca62012-02-16 01:37:49 -070062
Grant Likely68700652012-02-14 14:06:53 -070063 if (domain->ops->match == NULL)
64 domain->ops->match = default_irq_domain_match;
Grant Likelycc79ca62012-02-16 01:37:49 -070065
Grant Likelya8db8cf2012-02-14 14:06:54 -070066 return domain;
67}
68
69static void irq_domain_add(struct irq_domain *domain)
70{
71 mutex_lock(&irq_domain_mutex);
72 list_add(&domain->link, &irq_domain_list);
73 mutex_unlock(&irq_domain_mutex);
74 pr_debug("irq: Allocated domain of type %d @0x%p\n",
75 domain->revmap_type, domain);
76}
77
Grant Likely1bc04f22012-02-14 14:06:55 -070078static unsigned int irq_domain_legacy_revmap(struct irq_domain *domain,
79 irq_hw_number_t hwirq)
80{
81 irq_hw_number_t first_hwirq = domain->revmap_data.legacy.first_hwirq;
82 int size = domain->revmap_data.legacy.size;
83
84 if (WARN_ON(hwirq < first_hwirq || hwirq >= first_hwirq + size))
85 return 0;
86 return hwirq - first_hwirq + domain->revmap_data.legacy.first_irq;
87}
88
Grant Likelya8db8cf2012-02-14 14:06:54 -070089/**
90 * irq_domain_add_legacy() - Allocate and register a legacy revmap irq_domain.
91 * @of_node: pointer to interrupt controller's device tree node.
Grant Likely1bc04f22012-02-14 14:06:55 -070092 * @size: total number of irqs in legacy mapping
93 * @first_irq: first number of irq block assigned to the domain
94 * @first_hwirq: first hwirq number to use for the translation. Should normally
95 * be '0', but a positive integer can be used if the effective
96 * hwirqs numbering does not begin at zero.
Grant Likelya8db8cf2012-02-14 14:06:54 -070097 * @ops: map/unmap domain callbacks
98 * @host_data: Controller private data pointer
99 *
100 * Note: the map() callback will be called before this function returns
101 * for all legacy interrupts except 0 (which is always the invalid irq for
102 * a legacy controller).
103 */
104struct irq_domain *irq_domain_add_legacy(struct device_node *of_node,
Grant Likely1bc04f22012-02-14 14:06:55 -0700105 unsigned int size,
106 unsigned int first_irq,
107 irq_hw_number_t first_hwirq,
Grant Likelya8db8cf2012-02-14 14:06:54 -0700108 struct irq_domain_ops *ops,
109 void *host_data)
110{
Grant Likely1bc04f22012-02-14 14:06:55 -0700111 struct irq_domain *domain;
Grant Likelya8db8cf2012-02-14 14:06:54 -0700112 unsigned int i;
113
114 domain = irq_domain_alloc(of_node, IRQ_DOMAIN_MAP_LEGACY, ops, host_data);
115 if (!domain)
116 return NULL;
117
Grant Likely1bc04f22012-02-14 14:06:55 -0700118 domain->revmap_data.legacy.first_irq = first_irq;
119 domain->revmap_data.legacy.first_hwirq = first_hwirq;
120 domain->revmap_data.legacy.size = size;
121
Grant Likelycc79ca62012-02-16 01:37:49 -0700122 mutex_lock(&irq_domain_mutex);
Grant Likely1bc04f22012-02-14 14:06:55 -0700123 /* Verify that all the irqs are available */
124 for (i = 0; i < size; i++) {
125 int irq = first_irq + i;
126 struct irq_data *irq_data = irq_get_irq_data(irq);
127
128 if (WARN_ON(!irq_data || irq_data->domain)) {
Grant Likelya8db8cf2012-02-14 14:06:54 -0700129 mutex_unlock(&irq_domain_mutex);
130 of_node_put(domain->of_node);
131 kfree(domain);
132 return NULL;
Grant Likelycc79ca62012-02-16 01:37:49 -0700133 }
134 }
Grant Likely1bc04f22012-02-14 14:06:55 -0700135
136 /* Claim all of the irqs before registering a legacy domain */
137 for (i = 0; i < size; i++) {
138 struct irq_data *irq_data = irq_get_irq_data(first_irq + i);
139 irq_data->hwirq = first_hwirq + i;
140 irq_data->domain = domain;
141 }
Grant Likelycc79ca62012-02-16 01:37:49 -0700142 mutex_unlock(&irq_domain_mutex);
143
Grant Likely1bc04f22012-02-14 14:06:55 -0700144 for (i = 0; i < size; i++) {
145 int irq = first_irq + i;
146 int hwirq = first_hwirq + i;
147
148 /* IRQ0 gets ignored */
149 if (!irq)
150 continue;
Grant Likelycc79ca62012-02-16 01:37:49 -0700151
Grant Likelya8db8cf2012-02-14 14:06:54 -0700152 /* Legacy flags are left to default at this point,
153 * one can then use irq_create_mapping() to
154 * explicitly change them
155 */
Grant Likely1bc04f22012-02-14 14:06:55 -0700156 ops->map(domain, irq, hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700157
Grant Likelya8db8cf2012-02-14 14:06:54 -0700158 /* Clear norequest flags */
Grant Likely1bc04f22012-02-14 14:06:55 -0700159 irq_clear_status_flags(irq, IRQ_NOREQUEST);
Grant Likelycc79ca62012-02-16 01:37:49 -0700160 }
Grant Likely1bc04f22012-02-14 14:06:55 -0700161
162 irq_domain_add(domain);
Grant Likelya8db8cf2012-02-14 14:06:54 -0700163 return domain;
164}
Grant Likelycc79ca62012-02-16 01:37:49 -0700165
Grant Likelya8db8cf2012-02-14 14:06:54 -0700166/**
167 * irq_domain_add_linear() - Allocate and register a legacy revmap irq_domain.
168 * @of_node: pointer to interrupt controller's device tree node.
169 * @ops: map/unmap domain callbacks
170 * @host_data: Controller private data pointer
171 */
172struct irq_domain *irq_domain_add_linear(struct device_node *of_node,
173 unsigned int size,
174 struct irq_domain_ops *ops,
175 void *host_data)
176{
177 struct irq_domain *domain;
178 unsigned int *revmap;
Grant Likelycc79ca62012-02-16 01:37:49 -0700179
Grant Likelya8db8cf2012-02-14 14:06:54 -0700180 revmap = kzalloc(sizeof(*revmap) * size, GFP_KERNEL);
181 if (WARN_ON(!revmap))
182 return NULL;
183
184 domain = irq_domain_alloc(of_node, IRQ_DOMAIN_MAP_LINEAR, ops, host_data);
185 if (!domain) {
186 kfree(revmap);
187 return NULL;
188 }
189 domain->revmap_data.linear.size = size;
190 domain->revmap_data.linear.revmap = revmap;
191 irq_domain_add(domain);
192 return domain;
193}
194
195struct irq_domain *irq_domain_add_nomap(struct device_node *of_node,
196 struct irq_domain_ops *ops,
197 void *host_data)
198{
199 struct irq_domain *domain = irq_domain_alloc(of_node,
200 IRQ_DOMAIN_MAP_NOMAP, ops, host_data);
201 if (domain)
202 irq_domain_add(domain);
203 return domain;
204}
205
206/**
207 * irq_domain_add_tree()
208 * @of_node: pointer to interrupt controller's device tree node.
209 * @ops: map/unmap domain callbacks
210 *
211 * Note: The radix tree will be allocated later during boot automatically
212 * (the reverse mapping will use the slow path until that happens).
213 */
214struct irq_domain *irq_domain_add_tree(struct device_node *of_node,
215 struct irq_domain_ops *ops,
216 void *host_data)
217{
218 struct irq_domain *domain = irq_domain_alloc(of_node,
219 IRQ_DOMAIN_MAP_TREE, ops, host_data);
220 if (domain) {
221 INIT_RADIX_TREE(&domain->revmap_data.tree, GFP_KERNEL);
222 irq_domain_add(domain);
223 }
Grant Likely68700652012-02-14 14:06:53 -0700224 return domain;
Grant Likelycc79ca62012-02-16 01:37:49 -0700225}
226
227/**
228 * irq_find_host() - Locates a domain for a given device node
229 * @node: device-tree node of the interrupt controller
230 */
231struct irq_domain *irq_find_host(struct device_node *node)
232{
233 struct irq_domain *h, *found = NULL;
234
235 /* We might want to match the legacy controller last since
236 * it might potentially be set to match all interrupts in
237 * the absence of a device node. This isn't a problem so far
238 * yet though...
239 */
240 mutex_lock(&irq_domain_mutex);
241 list_for_each_entry(h, &irq_domain_list, link)
242 if (h->ops->match(h, node)) {
243 found = h;
244 break;
245 }
246 mutex_unlock(&irq_domain_mutex);
247 return found;
248}
249EXPORT_SYMBOL_GPL(irq_find_host);
250
251/**
252 * irq_set_default_host() - Set a "default" irq domain
Grant Likely68700652012-02-14 14:06:53 -0700253 * @domain: default domain pointer
Grant Likelycc79ca62012-02-16 01:37:49 -0700254 *
255 * For convenience, it's possible to set a "default" domain that will be used
256 * whenever NULL is passed to irq_create_mapping(). It makes life easier for
257 * platforms that want to manipulate a few hard coded interrupt numbers that
258 * aren't properly represented in the device-tree.
259 */
Grant Likely68700652012-02-14 14:06:53 -0700260void irq_set_default_host(struct irq_domain *domain)
Grant Likelycc79ca62012-02-16 01:37:49 -0700261{
Grant Likely68700652012-02-14 14:06:53 -0700262 pr_debug("irq: Default domain set to @0x%p\n", domain);
Grant Likelycc79ca62012-02-16 01:37:49 -0700263
Grant Likely68700652012-02-14 14:06:53 -0700264 irq_default_domain = domain;
Grant Likelycc79ca62012-02-16 01:37:49 -0700265}
266
267/**
268 * irq_set_virq_count() - Set the maximum number of linux irqs
269 * @count: number of linux irqs, capped with NR_IRQS
270 *
271 * This is mainly for use by platforms like iSeries who want to program
272 * the virtual irq number in the controller to avoid the reverse mapping
273 */
274void irq_set_virq_count(unsigned int count)
275{
276 pr_debug("irq: Trying to set virq count to %d\n", count);
277
278 BUG_ON(count < NUM_ISA_INTERRUPTS);
279 if (count < NR_IRQS)
280 irq_virq_count = count;
281}
282
Grant Likely68700652012-02-14 14:06:53 -0700283static int irq_setup_virq(struct irq_domain *domain, unsigned int virq,
Grant Likelycc79ca62012-02-16 01:37:49 -0700284 irq_hw_number_t hwirq)
285{
286 struct irq_data *irq_data = irq_get_irq_data(virq);
287
288 irq_data->hwirq = hwirq;
Grant Likely68700652012-02-14 14:06:53 -0700289 irq_data->domain = domain;
290 if (domain->ops->map(domain, virq, hwirq)) {
Grant Likelycc79ca62012-02-16 01:37:49 -0700291 pr_debug("irq: -> mapping failed, freeing\n");
292 irq_data->domain = NULL;
293 irq_data->hwirq = 0;
294 return -1;
295 }
296
297 irq_clear_status_flags(virq, IRQ_NOREQUEST);
298
299 return 0;
300}
301
302/**
303 * irq_create_direct_mapping() - Allocate an irq for direct mapping
Grant Likely68700652012-02-14 14:06:53 -0700304 * @domain: domain to allocate the irq for or NULL for default domain
Grant Likelycc79ca62012-02-16 01:37:49 -0700305 *
306 * This routine is used for irq controllers which can choose the hardware
307 * interrupt numbers they generate. In such a case it's simplest to use
308 * the linux irq as the hardware interrupt number.
309 */
Grant Likely68700652012-02-14 14:06:53 -0700310unsigned int irq_create_direct_mapping(struct irq_domain *domain)
Grant Likelycc79ca62012-02-16 01:37:49 -0700311{
312 unsigned int virq;
313
Grant Likely68700652012-02-14 14:06:53 -0700314 if (domain == NULL)
315 domain = irq_default_domain;
Grant Likelycc79ca62012-02-16 01:37:49 -0700316
Grant Likely68700652012-02-14 14:06:53 -0700317 BUG_ON(domain == NULL);
318 WARN_ON(domain->revmap_type != IRQ_DOMAIN_MAP_NOMAP);
Grant Likelycc79ca62012-02-16 01:37:49 -0700319
320 virq = irq_alloc_desc_from(1, 0);
Grant Likely03848372012-02-14 14:06:52 -0700321 if (!virq) {
Grant Likelycc79ca62012-02-16 01:37:49 -0700322 pr_debug("irq: create_direct virq allocation failed\n");
Grant Likely03848372012-02-14 14:06:52 -0700323 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700324 }
325 if (virq >= irq_virq_count) {
326 pr_err("ERROR: no free irqs available below %i maximum\n",
327 irq_virq_count);
328 irq_free_desc(virq);
329 return 0;
330 }
331
332 pr_debug("irq: create_direct obtained virq %d\n", virq);
333
Grant Likely68700652012-02-14 14:06:53 -0700334 if (irq_setup_virq(domain, virq, virq)) {
Grant Likelycc79ca62012-02-16 01:37:49 -0700335 irq_free_desc(virq);
Grant Likely03848372012-02-14 14:06:52 -0700336 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700337 }
338
339 return virq;
340}
341
342/**
343 * irq_create_mapping() - Map a hardware interrupt into linux irq space
Grant Likely68700652012-02-14 14:06:53 -0700344 * @domain: domain owning this hardware interrupt or NULL for default domain
345 * @hwirq: hardware irq number in that domain space
Grant Likelycc79ca62012-02-16 01:37:49 -0700346 *
347 * Only one mapping per hardware interrupt is permitted. Returns a linux
348 * irq number.
349 * If the sense/trigger is to be specified, set_irq_type() should be called
350 * on the number returned from that call.
351 */
Grant Likely68700652012-02-14 14:06:53 -0700352unsigned int irq_create_mapping(struct irq_domain *domain,
Grant Likelycc79ca62012-02-16 01:37:49 -0700353 irq_hw_number_t hwirq)
354{
355 unsigned int virq, hint;
356
Grant Likely68700652012-02-14 14:06:53 -0700357 pr_debug("irq: irq_create_mapping(0x%p, 0x%lx)\n", domain, hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700358
Grant Likely68700652012-02-14 14:06:53 -0700359 /* Look for default domain if nececssary */
360 if (domain == NULL)
361 domain = irq_default_domain;
362 if (domain == NULL) {
Grant Likelycc79ca62012-02-16 01:37:49 -0700363 printk(KERN_WARNING "irq_create_mapping called for"
Grant Likely68700652012-02-14 14:06:53 -0700364 " NULL domain, hwirq=%lx\n", hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700365 WARN_ON(1);
Grant Likely03848372012-02-14 14:06:52 -0700366 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700367 }
Grant Likely68700652012-02-14 14:06:53 -0700368 pr_debug("irq: -> using domain @%p\n", domain);
Grant Likelycc79ca62012-02-16 01:37:49 -0700369
370 /* Check if mapping already exists */
Grant Likely68700652012-02-14 14:06:53 -0700371 virq = irq_find_mapping(domain, hwirq);
Grant Likely03848372012-02-14 14:06:52 -0700372 if (virq) {
Grant Likelycc79ca62012-02-16 01:37:49 -0700373 pr_debug("irq: -> existing mapping on virq %d\n", virq);
374 return virq;
375 }
376
377 /* Get a virtual interrupt number */
Grant Likely1bc04f22012-02-14 14:06:55 -0700378 if (domain->revmap_type == IRQ_DOMAIN_MAP_LEGACY)
379 return irq_domain_legacy_revmap(domain, hwirq);
380
381 /* Allocate a virtual interrupt number */
382 hint = hwirq % irq_virq_count;
383 if (hint == 0)
384 hint++;
385 virq = irq_alloc_desc_from(hint, 0);
386 if (!virq)
387 virq = irq_alloc_desc_from(1, 0);
388 if (!virq) {
389 pr_debug("irq: -> virq allocation failed\n");
390 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700391 }
392
Grant Likely68700652012-02-14 14:06:53 -0700393 if (irq_setup_virq(domain, virq, hwirq)) {
394 if (domain->revmap_type != IRQ_DOMAIN_MAP_LEGACY)
Grant Likelycc79ca62012-02-16 01:37:49 -0700395 irq_free_desc(virq);
Grant Likely03848372012-02-14 14:06:52 -0700396 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700397 }
398
Grant Likely68700652012-02-14 14:06:53 -0700399 pr_debug("irq: irq %lu on domain %s mapped to virtual irq %u\n",
400 hwirq, domain->of_node ? domain->of_node->full_name : "null", virq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700401
402 return virq;
403}
404EXPORT_SYMBOL_GPL(irq_create_mapping);
405
406unsigned int irq_create_of_mapping(struct device_node *controller,
407 const u32 *intspec, unsigned int intsize)
408{
Grant Likely68700652012-02-14 14:06:53 -0700409 struct irq_domain *domain;
Grant Likelycc79ca62012-02-16 01:37:49 -0700410 irq_hw_number_t hwirq;
411 unsigned int type = IRQ_TYPE_NONE;
412 unsigned int virq;
413
Grant Likely68700652012-02-14 14:06:53 -0700414 domain = controller ? irq_find_host(controller) : irq_default_domain;
415 if (!domain) {
416 printk(KERN_WARNING "irq: no irq domain found for %s !\n",
Grant Likelycc79ca62012-02-16 01:37:49 -0700417 controller->full_name);
Grant Likely03848372012-02-14 14:06:52 -0700418 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700419 }
420
Grant Likely68700652012-02-14 14:06:53 -0700421 /* If domain has no translation, then we assume interrupt line */
422 if (domain->ops->xlate == NULL)
Grant Likelycc79ca62012-02-16 01:37:49 -0700423 hwirq = intspec[0];
424 else {
Grant Likely68700652012-02-14 14:06:53 -0700425 if (domain->ops->xlate(domain, controller, intspec, intsize,
Grant Likelycc79ca62012-02-16 01:37:49 -0700426 &hwirq, &type))
Grant Likely03848372012-02-14 14:06:52 -0700427 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700428 }
429
430 /* Create mapping */
Grant Likely68700652012-02-14 14:06:53 -0700431 virq = irq_create_mapping(domain, hwirq);
Grant Likely03848372012-02-14 14:06:52 -0700432 if (!virq)
Grant Likelycc79ca62012-02-16 01:37:49 -0700433 return virq;
434
435 /* Set type if specified and different than the current one */
436 if (type != IRQ_TYPE_NONE &&
437 type != (irqd_get_trigger_type(irq_get_irq_data(virq))))
438 irq_set_irq_type(virq, type);
439 return virq;
440}
441EXPORT_SYMBOL_GPL(irq_create_of_mapping);
442
443/**
444 * irq_dispose_mapping() - Unmap an interrupt
445 * @virq: linux irq number of the interrupt to unmap
446 */
447void irq_dispose_mapping(unsigned int virq)
448{
449 struct irq_data *irq_data = irq_get_irq_data(virq);
Grant Likely68700652012-02-14 14:06:53 -0700450 struct irq_domain *domain;
Grant Likelycc79ca62012-02-16 01:37:49 -0700451 irq_hw_number_t hwirq;
452
Grant Likely03848372012-02-14 14:06:52 -0700453 if (!virq || !irq_data)
Grant Likelycc79ca62012-02-16 01:37:49 -0700454 return;
455
Grant Likely68700652012-02-14 14:06:53 -0700456 domain = irq_data->domain;
457 if (WARN_ON(domain == NULL))
Grant Likelycc79ca62012-02-16 01:37:49 -0700458 return;
459
460 /* Never unmap legacy interrupts */
Grant Likely68700652012-02-14 14:06:53 -0700461 if (domain->revmap_type == IRQ_DOMAIN_MAP_LEGACY)
Grant Likelycc79ca62012-02-16 01:37:49 -0700462 return;
463
464 irq_set_status_flags(virq, IRQ_NOREQUEST);
465
466 /* remove chip and handler */
467 irq_set_chip_and_handler(virq, NULL, NULL);
468
469 /* Make sure it's completed */
470 synchronize_irq(virq);
471
472 /* Tell the PIC about it */
Grant Likely68700652012-02-14 14:06:53 -0700473 if (domain->ops->unmap)
474 domain->ops->unmap(domain, virq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700475 smp_mb();
476
477 /* Clear reverse map */
478 hwirq = irq_data->hwirq;
Grant Likely68700652012-02-14 14:06:53 -0700479 switch(domain->revmap_type) {
Grant Likelycc79ca62012-02-16 01:37:49 -0700480 case IRQ_DOMAIN_MAP_LINEAR:
Grant Likely68700652012-02-14 14:06:53 -0700481 if (hwirq < domain->revmap_data.linear.size)
482 domain->revmap_data.linear.revmap[hwirq] = 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700483 break;
484 case IRQ_DOMAIN_MAP_TREE:
485 mutex_lock(&revmap_trees_mutex);
Grant Likely68700652012-02-14 14:06:53 -0700486 radix_tree_delete(&domain->revmap_data.tree, hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700487 mutex_unlock(&revmap_trees_mutex);
488 break;
489 }
490
Grant Likelycc79ca62012-02-16 01:37:49 -0700491 irq_free_desc(virq);
492}
493EXPORT_SYMBOL_GPL(irq_dispose_mapping);
494
495/**
496 * irq_find_mapping() - Find a linux irq from an hw irq number.
Grant Likely68700652012-02-14 14:06:53 -0700497 * @domain: domain owning this hardware interrupt
498 * @hwirq: hardware irq number in that domain space
Grant Likelycc79ca62012-02-16 01:37:49 -0700499 *
500 * This is a slow path, for use by generic code. It's expected that an
501 * irq controller implementation directly calls the appropriate low level
502 * mapping function.
503 */
Grant Likely68700652012-02-14 14:06:53 -0700504unsigned int irq_find_mapping(struct irq_domain *domain,
Grant Likelycc79ca62012-02-16 01:37:49 -0700505 irq_hw_number_t hwirq)
506{
507 unsigned int i;
508 unsigned int hint = hwirq % irq_virq_count;
509
Grant Likely68700652012-02-14 14:06:53 -0700510 /* Look for default domain if nececssary */
511 if (domain == NULL)
512 domain = irq_default_domain;
513 if (domain == NULL)
Grant Likely03848372012-02-14 14:06:52 -0700514 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700515
516 /* legacy -> bail early */
Grant Likely68700652012-02-14 14:06:53 -0700517 if (domain->revmap_type == IRQ_DOMAIN_MAP_LEGACY)
Grant Likely1bc04f22012-02-14 14:06:55 -0700518 return irq_domain_legacy_revmap(domain, hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700519
520 /* Slow path does a linear search of the map */
521 if (hint == 0)
522 hint = 1;
523 i = hint;
524 do {
525 struct irq_data *data = irq_get_irq_data(i);
Grant Likely68700652012-02-14 14:06:53 -0700526 if (data && (data->domain == domain) && (data->hwirq == hwirq))
Grant Likelycc79ca62012-02-16 01:37:49 -0700527 return i;
528 i++;
529 if (i >= irq_virq_count)
530 i = 1;
531 } while(i != hint);
Grant Likely03848372012-02-14 14:06:52 -0700532 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700533}
534EXPORT_SYMBOL_GPL(irq_find_mapping);
535
536/**
537 * irq_radix_revmap_lookup() - Find a linux irq from a hw irq number.
Grant Likely68700652012-02-14 14:06:53 -0700538 * @domain: domain owning this hardware interrupt
539 * @hwirq: hardware irq number in that domain space
Grant Likelycc79ca62012-02-16 01:37:49 -0700540 *
541 * This is a fast path, for use by irq controller code that uses radix tree
542 * revmaps
543 */
Grant Likely68700652012-02-14 14:06:53 -0700544unsigned int irq_radix_revmap_lookup(struct irq_domain *domain,
Grant Likelycc79ca62012-02-16 01:37:49 -0700545 irq_hw_number_t hwirq)
546{
547 struct irq_data *irq_data;
548
Grant Likely68700652012-02-14 14:06:53 -0700549 if (WARN_ON_ONCE(domain->revmap_type != IRQ_DOMAIN_MAP_TREE))
550 return irq_find_mapping(domain, hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700551
552 /*
553 * Freeing an irq can delete nodes along the path to
554 * do the lookup via call_rcu.
555 */
556 rcu_read_lock();
Grant Likely68700652012-02-14 14:06:53 -0700557 irq_data = radix_tree_lookup(&domain->revmap_data.tree, hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700558 rcu_read_unlock();
559
560 /*
561 * If found in radix tree, then fine.
562 * Else fallback to linear lookup - this should not happen in practice
563 * as it means that we failed to insert the node in the radix tree.
564 */
Grant Likely68700652012-02-14 14:06:53 -0700565 return irq_data ? irq_data->irq : irq_find_mapping(domain, hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700566}
567
568/**
569 * irq_radix_revmap_insert() - Insert a hw irq to linux irq number mapping.
Grant Likely68700652012-02-14 14:06:53 -0700570 * @domain: domain owning this hardware interrupt
Grant Likelycc79ca62012-02-16 01:37:49 -0700571 * @virq: linux irq number
Grant Likely68700652012-02-14 14:06:53 -0700572 * @hwirq: hardware irq number in that domain space
Grant Likelycc79ca62012-02-16 01:37:49 -0700573 *
574 * This is for use by irq controllers that use a radix tree reverse
575 * mapping for fast lookup.
576 */
Grant Likely68700652012-02-14 14:06:53 -0700577void irq_radix_revmap_insert(struct irq_domain *domain, unsigned int virq,
Grant Likelycc79ca62012-02-16 01:37:49 -0700578 irq_hw_number_t hwirq)
579{
580 struct irq_data *irq_data = irq_get_irq_data(virq);
581
Grant Likely68700652012-02-14 14:06:53 -0700582 if (WARN_ON(domain->revmap_type != IRQ_DOMAIN_MAP_TREE))
Grant Likelycc79ca62012-02-16 01:37:49 -0700583 return;
584
Grant Likely03848372012-02-14 14:06:52 -0700585 if (virq) {
Grant Likelycc79ca62012-02-16 01:37:49 -0700586 mutex_lock(&revmap_trees_mutex);
Grant Likely68700652012-02-14 14:06:53 -0700587 radix_tree_insert(&domain->revmap_data.tree, hwirq, irq_data);
Grant Likelycc79ca62012-02-16 01:37:49 -0700588 mutex_unlock(&revmap_trees_mutex);
589 }
590}
591
592/**
593 * irq_linear_revmap() - Find a linux irq from a hw irq number.
Grant Likely68700652012-02-14 14:06:53 -0700594 * @domain: domain owning this hardware interrupt
595 * @hwirq: hardware irq number in that domain space
Grant Likelycc79ca62012-02-16 01:37:49 -0700596 *
597 * This is a fast path, for use by irq controller code that uses linear
598 * revmaps. It does fallback to the slow path if the revmap doesn't exist
599 * yet and will create the revmap entry with appropriate locking
600 */
Grant Likely68700652012-02-14 14:06:53 -0700601unsigned int irq_linear_revmap(struct irq_domain *domain,
Grant Likelycc79ca62012-02-16 01:37:49 -0700602 irq_hw_number_t hwirq)
603{
604 unsigned int *revmap;
605
Grant Likely68700652012-02-14 14:06:53 -0700606 if (WARN_ON_ONCE(domain->revmap_type != IRQ_DOMAIN_MAP_LINEAR))
607 return irq_find_mapping(domain, hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700608
609 /* Check revmap bounds */
Grant Likely68700652012-02-14 14:06:53 -0700610 if (unlikely(hwirq >= domain->revmap_data.linear.size))
611 return irq_find_mapping(domain, hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700612
613 /* Check if revmap was allocated */
Grant Likely68700652012-02-14 14:06:53 -0700614 revmap = domain->revmap_data.linear.revmap;
Grant Likelycc79ca62012-02-16 01:37:49 -0700615 if (unlikely(revmap == NULL))
Grant Likely68700652012-02-14 14:06:53 -0700616 return irq_find_mapping(domain, hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700617
618 /* Fill up revmap with slow path if no mapping found */
Grant Likely03848372012-02-14 14:06:52 -0700619 if (unlikely(!revmap[hwirq]))
Grant Likely68700652012-02-14 14:06:53 -0700620 revmap[hwirq] = irq_find_mapping(domain, hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700621
622 return revmap[hwirq];
623}
624
625#ifdef CONFIG_VIRQ_DEBUG
626static int virq_debug_show(struct seq_file *m, void *private)
627{
628 unsigned long flags;
629 struct irq_desc *desc;
630 const char *p;
631 static const char none[] = "none";
632 void *data;
633 int i;
634
635 seq_printf(m, "%-5s %-7s %-15s %-18s %s\n", "virq", "hwirq",
Grant Likely68700652012-02-14 14:06:53 -0700636 "chip name", "chip data", "domain name");
Grant Likelycc79ca62012-02-16 01:37:49 -0700637
638 for (i = 1; i < nr_irqs; i++) {
639 desc = irq_to_desc(i);
640 if (!desc)
641 continue;
642
643 raw_spin_lock_irqsave(&desc->lock, flags);
644
645 if (desc->action && desc->action->handler) {
646 struct irq_chip *chip;
647
648 seq_printf(m, "%5d ", i);
649 seq_printf(m, "0x%05lx ", desc->irq_data.hwirq);
650
651 chip = irq_desc_get_chip(desc);
652 if (chip && chip->name)
653 p = chip->name;
654 else
655 p = none;
656 seq_printf(m, "%-15s ", p);
657
658 data = irq_desc_get_chip_data(desc);
659 seq_printf(m, "0x%16p ", data);
660
661 if (desc->irq_data.domain->of_node)
662 p = desc->irq_data.domain->of_node->full_name;
663 else
664 p = none;
665 seq_printf(m, "%s\n", p);
666 }
667
668 raw_spin_unlock_irqrestore(&desc->lock, flags);
669 }
670
671 return 0;
672}
673
674static int virq_debug_open(struct inode *inode, struct file *file)
675{
676 return single_open(file, virq_debug_show, inode->i_private);
677}
678
679static const struct file_operations virq_debug_fops = {
680 .open = virq_debug_open,
681 .read = seq_read,
682 .llseek = seq_lseek,
683 .release = single_release,
684};
685
686static int __init irq_debugfs_init(void)
687{
688 if (debugfs_create_file("virq_mapping", S_IRUGO, powerpc_debugfs_root,
689 NULL, &virq_debug_fops) == NULL)
690 return -ENOMEM;
691
692 return 0;
693}
694__initcall(irq_debugfs_init);
695#endif /* CONFIG_VIRQ_DEBUG */
696
697#else /* CONFIG_PPC */
698
Grant Likely08a543a2011-07-26 03:19:06 -0600699/**
700 * irq_domain_add() - Register an irq_domain
701 * @domain: ptr to initialized irq_domain structure
702 *
703 * Registers an irq_domain structure. The irq_domain must at a minimum be
704 * initialized with an ops structure pointer, and either a ->to_irq hook or
705 * a valid irq_base value. Everything else is optional.
706 */
707void irq_domain_add(struct irq_domain *domain)
708{
709 struct irq_data *d;
Rob Herring6d274302011-09-30 10:48:38 -0500710 int hwirq, irq;
Grant Likely08a543a2011-07-26 03:19:06 -0600711
712 /*
713 * This assumes that the irq_domain owner has already allocated
714 * the irq_descs. This block will be removed when support for dynamic
715 * allocation of irq_descs is added to irq_domain.
716 */
Rob Herring6d274302011-09-30 10:48:38 -0500717 irq_domain_for_each_irq(domain, hwirq, irq) {
718 d = irq_get_irq_data(irq);
Rob Herringeef24af2011-09-14 11:31:37 -0500719 if (!d) {
720 WARN(1, "error: assigning domain to non existant irq_desc");
721 return;
722 }
723 if (d->domain) {
Grant Likely08a543a2011-07-26 03:19:06 -0600724 /* things are broken; just report, don't clean up */
725 WARN(1, "error: irq_desc already assigned to a domain");
726 return;
727 }
728 d->domain = domain;
729 d->hwirq = hwirq;
730 }
731
732 mutex_lock(&irq_domain_mutex);
Grant Likely7bb69ba2012-02-14 14:06:48 -0700733 list_add(&domain->link, &irq_domain_list);
Grant Likely08a543a2011-07-26 03:19:06 -0600734 mutex_unlock(&irq_domain_mutex);
735}
736
737/**
738 * irq_domain_del() - Unregister an irq_domain
739 * @domain: ptr to registered irq_domain.
740 */
741void irq_domain_del(struct irq_domain *domain)
742{
743 struct irq_data *d;
Rob Herring6d274302011-09-30 10:48:38 -0500744 int hwirq, irq;
Grant Likely08a543a2011-07-26 03:19:06 -0600745
746 mutex_lock(&irq_domain_mutex);
Grant Likely7bb69ba2012-02-14 14:06:48 -0700747 list_del(&domain->link);
Grant Likely08a543a2011-07-26 03:19:06 -0600748 mutex_unlock(&irq_domain_mutex);
749
750 /* Clear the irq_domain assignments */
Rob Herring6d274302011-09-30 10:48:38 -0500751 irq_domain_for_each_irq(domain, hwirq, irq) {
752 d = irq_get_irq_data(irq);
Grant Likely08a543a2011-07-26 03:19:06 -0600753 d->domain = NULL;
754 }
755}
756
757#if defined(CONFIG_OF_IRQ)
758/**
759 * irq_create_of_mapping() - Map a linux irq number from a DT interrupt spec
760 *
761 * Used by the device tree interrupt mapping code to translate a device tree
762 * interrupt specifier to a valid linux irq number. Returns either a valid
763 * linux IRQ number or 0.
764 *
765 * When the caller no longer need the irq number returned by this function it
766 * should arrange to call irq_dispose_mapping().
767 */
768unsigned int irq_create_of_mapping(struct device_node *controller,
769 const u32 *intspec, unsigned int intsize)
770{
771 struct irq_domain *domain;
772 unsigned long hwirq;
773 unsigned int irq, type;
774 int rc = -EINVAL;
775
776 /* Find a domain which can translate the irq spec */
777 mutex_lock(&irq_domain_mutex);
Grant Likely7bb69ba2012-02-14 14:06:48 -0700778 list_for_each_entry(domain, &irq_domain_list, link) {
779 if (!domain->ops->xlate)
Grant Likely08a543a2011-07-26 03:19:06 -0600780 continue;
Grant Likely7bb69ba2012-02-14 14:06:48 -0700781 rc = domain->ops->xlate(domain, controller,
Grant Likely08a543a2011-07-26 03:19:06 -0600782 intspec, intsize, &hwirq, &type);
783 if (rc == 0)
784 break;
785 }
786 mutex_unlock(&irq_domain_mutex);
787
788 if (rc != 0)
789 return 0;
790
791 irq = irq_domain_to_irq(domain, hwirq);
792 if (type != IRQ_TYPE_NONE)
793 irq_set_irq_type(irq, type);
794 pr_debug("%s: mapped hwirq=%i to irq=%i, flags=%x\n",
795 controller->full_name, (int)hwirq, irq, type);
796 return irq;
797}
798EXPORT_SYMBOL_GPL(irq_create_of_mapping);
799
800/**
801 * irq_dispose_mapping() - Discard a mapping created by irq_create_of_mapping()
802 * @irq: linux irq number to be discarded
803 *
804 * Calling this function indicates the caller no longer needs a reference to
805 * the linux irq number returned by a prior call to irq_create_of_mapping().
806 */
807void irq_dispose_mapping(unsigned int irq)
808{
809 /*
810 * nothing yet; will be filled when support for dynamic allocation of
811 * irq_descs is added to irq_domain
812 */
813}
814EXPORT_SYMBOL_GPL(irq_dispose_mapping);
Grant Likely7e713302011-07-26 03:19:06 -0600815
Grant Likely7bb69ba2012-02-14 14:06:48 -0700816int irq_domain_simple_xlate(struct irq_domain *d,
Grant Likely7e713302011-07-26 03:19:06 -0600817 struct device_node *controller,
818 const u32 *intspec, unsigned int intsize,
819 unsigned long *out_hwirq, unsigned int *out_type)
820{
821 if (d->of_node != controller)
822 return -EINVAL;
823 if (intsize < 1)
824 return -EINVAL;
Rob Herring93797d82011-12-12 09:59:14 -0600825 if (d->nr_irq && ((intspec[0] < d->hwirq_base) ||
826 (intspec[0] >= d->hwirq_base + d->nr_irq)))
827 return -EINVAL;
Grant Likely7e713302011-07-26 03:19:06 -0600828
829 *out_hwirq = intspec[0];
830 *out_type = IRQ_TYPE_NONE;
831 if (intsize > 1)
832 *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK;
833 return 0;
834}
835
Grant Likely7e713302011-07-26 03:19:06 -0600836/**
837 * irq_domain_create_simple() - Set up a 'simple' translation range
838 */
839void irq_domain_add_simple(struct device_node *controller, int irq_base)
840{
841 struct irq_domain *domain;
842
843 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
844 if (!domain) {
845 WARN_ON(1);
846 return;
847 }
848
849 domain->irq_base = irq_base;
850 domain->of_node = of_node_get(controller);
851 domain->ops = &irq_domain_simple_ops;
852 irq_domain_add(domain);
853}
854EXPORT_SYMBOL_GPL(irq_domain_add_simple);
855
856void irq_domain_generate_simple(const struct of_device_id *match,
857 u64 phys_base, unsigned int irq_start)
858{
859 struct device_node *node;
Grant Likelye1964c52012-02-14 14:06:48 -0700860 pr_debug("looking for phys_base=%llx, irq_start=%i\n",
Grant Likely7e713302011-07-26 03:19:06 -0600861 (unsigned long long) phys_base, (int) irq_start);
862 node = of_find_matching_node_by_address(NULL, match, phys_base);
863 if (node)
864 irq_domain_add_simple(node, irq_start);
Grant Likely7e713302011-07-26 03:19:06 -0600865}
866EXPORT_SYMBOL_GPL(irq_domain_generate_simple);
Grant Likely08a543a2011-07-26 03:19:06 -0600867#endif /* CONFIG_OF_IRQ */
Jamie Ilesc87fb572011-12-14 23:43:16 +0100868
869struct irq_domain_ops irq_domain_simple_ops = {
870#ifdef CONFIG_OF_IRQ
Grant Likely7bb69ba2012-02-14 14:06:48 -0700871 .xlate = irq_domain_simple_xlate,
Jamie Ilesc87fb572011-12-14 23:43:16 +0100872#endif /* CONFIG_OF_IRQ */
873};
874EXPORT_SYMBOL_GPL(irq_domain_simple_ops);
Grant Likelycc79ca62012-02-16 01:37:49 -0700875
876#endif /* !CONFIG_PPC */