blob: 9cae0b2f509f217db3ac2e877de032c07d513e11 [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 -070025static DEFINE_MUTEX(revmap_trees_mutex);
Grant Likely68700652012-02-14 14:06:53 -070026static struct irq_domain *irq_default_domain;
Grant Likelycc79ca62012-02-16 01:37:49 -070027
Grant Likelycc79ca62012-02-16 01:37:49 -070028/**
Grant Likelya8db8cf2012-02-14 14:06:54 -070029 * irq_domain_alloc() - Allocate a new irq_domain data structure
Grant Likelycc79ca62012-02-16 01:37:49 -070030 * @of_node: optional device-tree node of the interrupt controller
31 * @revmap_type: type of reverse mapping to use
Grant Likely68700652012-02-14 14:06:53 -070032 * @ops: map/unmap domain callbacks
Grant Likelya8db8cf2012-02-14 14:06:54 -070033 * @host_data: Controller private data pointer
Grant Likelycc79ca62012-02-16 01:37:49 -070034 *
Grant Likelya8db8cf2012-02-14 14:06:54 -070035 * Allocates and initialize and irq_domain structure. Caller is expected to
36 * register allocated irq_domain with irq_domain_register(). Returns pointer
37 * to IRQ domain, or NULL on failure.
Grant Likelycc79ca62012-02-16 01:37:49 -070038 */
Grant Likelya8db8cf2012-02-14 14:06:54 -070039static struct irq_domain *irq_domain_alloc(struct device_node *of_node,
40 unsigned int revmap_type,
Grant Likelya18dc812012-01-26 12:12:14 -070041 const struct irq_domain_ops *ops,
Grant Likelya8db8cf2012-02-14 14:06:54 -070042 void *host_data)
Grant Likelycc79ca62012-02-16 01:37:49 -070043{
Grant Likelya8db8cf2012-02-14 14:06:54 -070044 struct irq_domain *domain;
Grant Likelycc79ca62012-02-16 01:37:49 -070045
Grant Likelya8db8cf2012-02-14 14:06:54 -070046 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
47 if (WARN_ON(!domain))
Grant Likelycc79ca62012-02-16 01:37:49 -070048 return NULL;
49
50 /* Fill structure */
Grant Likely68700652012-02-14 14:06:53 -070051 domain->revmap_type = revmap_type;
Grant Likely68700652012-02-14 14:06:53 -070052 domain->ops = ops;
Grant Likelya8db8cf2012-02-14 14:06:54 -070053 domain->host_data = host_data;
Grant Likely68700652012-02-14 14:06:53 -070054 domain->of_node = of_node_get(of_node);
Grant Likelycc79ca62012-02-16 01:37:49 -070055
Grant Likelya8db8cf2012-02-14 14:06:54 -070056 return domain;
57}
58
Paul Mundt58ee99a2012-05-19 15:11:41 +090059static void irq_domain_free(struct irq_domain *domain)
60{
61 of_node_put(domain->of_node);
62 kfree(domain);
63}
64
Grant Likelya8db8cf2012-02-14 14:06:54 -070065static void irq_domain_add(struct irq_domain *domain)
66{
67 mutex_lock(&irq_domain_mutex);
68 list_add(&domain->link, &irq_domain_list);
69 mutex_unlock(&irq_domain_mutex);
70 pr_debug("irq: Allocated domain of type %d @0x%p\n",
71 domain->revmap_type, domain);
72}
73
Paul Mundt58ee99a2012-05-19 15:11:41 +090074/**
75 * irq_domain_remove() - Remove an irq domain.
76 * @domain: domain to remove
77 *
78 * This routine is used to remove an irq domain. The caller must ensure
79 * that all mappings within the domain have been disposed of prior to
80 * use, depending on the revmap type.
81 */
82void irq_domain_remove(struct irq_domain *domain)
83{
84 mutex_lock(&irq_domain_mutex);
85
86 switch (domain->revmap_type) {
87 case IRQ_DOMAIN_MAP_LEGACY:
88 /*
89 * Legacy domains don't manage their own irq_desc
90 * allocations, we expect the caller to handle irq_desc
91 * freeing on their own.
92 */
93 break;
94 case IRQ_DOMAIN_MAP_TREE:
95 /*
96 * radix_tree_delete() takes care of destroying the root
97 * node when all entries are removed. Shout if there are
98 * any mappings left.
99 */
100 WARN_ON(domain->revmap_data.tree.height);
101 break;
102 case IRQ_DOMAIN_MAP_LINEAR:
103 kfree(domain->revmap_data.linear.revmap);
104 domain->revmap_data.linear.size = 0;
105 break;
106 case IRQ_DOMAIN_MAP_NOMAP:
107 break;
108 }
109
110 list_del(&domain->link);
111
112 /*
113 * If the going away domain is the default one, reset it.
114 */
115 if (unlikely(irq_default_domain == domain))
116 irq_set_default_host(NULL);
117
118 mutex_unlock(&irq_domain_mutex);
119
120 pr_debug("irq: Removed domain of type %d @0x%p\n",
121 domain->revmap_type, domain);
122
123 irq_domain_free(domain);
124}
125
Grant Likely1bc04f22012-02-14 14:06:55 -0700126static unsigned int irq_domain_legacy_revmap(struct irq_domain *domain,
127 irq_hw_number_t hwirq)
128{
129 irq_hw_number_t first_hwirq = domain->revmap_data.legacy.first_hwirq;
130 int size = domain->revmap_data.legacy.size;
131
132 if (WARN_ON(hwirq < first_hwirq || hwirq >= first_hwirq + size))
133 return 0;
134 return hwirq - first_hwirq + domain->revmap_data.legacy.first_irq;
135}
136
Grant Likelya8db8cf2012-02-14 14:06:54 -0700137/**
138 * irq_domain_add_legacy() - Allocate and register a legacy revmap irq_domain.
139 * @of_node: pointer to interrupt controller's device tree node.
Grant Likely1bc04f22012-02-14 14:06:55 -0700140 * @size: total number of irqs in legacy mapping
141 * @first_irq: first number of irq block assigned to the domain
142 * @first_hwirq: first hwirq number to use for the translation. Should normally
143 * be '0', but a positive integer can be used if the effective
144 * hwirqs numbering does not begin at zero.
Grant Likelya8db8cf2012-02-14 14:06:54 -0700145 * @ops: map/unmap domain callbacks
146 * @host_data: Controller private data pointer
147 *
148 * Note: the map() callback will be called before this function returns
149 * for all legacy interrupts except 0 (which is always the invalid irq for
150 * a legacy controller).
151 */
152struct irq_domain *irq_domain_add_legacy(struct device_node *of_node,
Grant Likely1bc04f22012-02-14 14:06:55 -0700153 unsigned int size,
154 unsigned int first_irq,
155 irq_hw_number_t first_hwirq,
Grant Likelya18dc812012-01-26 12:12:14 -0700156 const struct irq_domain_ops *ops,
Grant Likelya8db8cf2012-02-14 14:06:54 -0700157 void *host_data)
158{
Grant Likely1bc04f22012-02-14 14:06:55 -0700159 struct irq_domain *domain;
Grant Likelya8db8cf2012-02-14 14:06:54 -0700160 unsigned int i;
161
162 domain = irq_domain_alloc(of_node, IRQ_DOMAIN_MAP_LEGACY, ops, host_data);
163 if (!domain)
164 return NULL;
165
Grant Likely1bc04f22012-02-14 14:06:55 -0700166 domain->revmap_data.legacy.first_irq = first_irq;
167 domain->revmap_data.legacy.first_hwirq = first_hwirq;
168 domain->revmap_data.legacy.size = size;
169
Grant Likelycc79ca62012-02-16 01:37:49 -0700170 mutex_lock(&irq_domain_mutex);
Grant Likely1bc04f22012-02-14 14:06:55 -0700171 /* Verify that all the irqs are available */
172 for (i = 0; i < size; i++) {
173 int irq = first_irq + i;
174 struct irq_data *irq_data = irq_get_irq_data(irq);
175
176 if (WARN_ON(!irq_data || irq_data->domain)) {
Grant Likelya8db8cf2012-02-14 14:06:54 -0700177 mutex_unlock(&irq_domain_mutex);
Paul Mundt58ee99a2012-05-19 15:11:41 +0900178 irq_domain_free(domain);
Grant Likelya8db8cf2012-02-14 14:06:54 -0700179 return NULL;
Grant Likelycc79ca62012-02-16 01:37:49 -0700180 }
181 }
Grant Likely1bc04f22012-02-14 14:06:55 -0700182
183 /* Claim all of the irqs before registering a legacy domain */
184 for (i = 0; i < size; i++) {
185 struct irq_data *irq_data = irq_get_irq_data(first_irq + i);
186 irq_data->hwirq = first_hwirq + i;
187 irq_data->domain = domain;
188 }
Grant Likelycc79ca62012-02-16 01:37:49 -0700189 mutex_unlock(&irq_domain_mutex);
190
Grant Likely1bc04f22012-02-14 14:06:55 -0700191 for (i = 0; i < size; i++) {
192 int irq = first_irq + i;
193 int hwirq = first_hwirq + i;
194
195 /* IRQ0 gets ignored */
196 if (!irq)
197 continue;
Grant Likelycc79ca62012-02-16 01:37:49 -0700198
Grant Likelya8db8cf2012-02-14 14:06:54 -0700199 /* Legacy flags are left to default at this point,
200 * one can then use irq_create_mapping() to
201 * explicitly change them
202 */
Grant Likely1bc04f22012-02-14 14:06:55 -0700203 ops->map(domain, irq, hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700204
Grant Likelya8db8cf2012-02-14 14:06:54 -0700205 /* Clear norequest flags */
Grant Likely1bc04f22012-02-14 14:06:55 -0700206 irq_clear_status_flags(irq, IRQ_NOREQUEST);
Grant Likelycc79ca62012-02-16 01:37:49 -0700207 }
Grant Likely1bc04f22012-02-14 14:06:55 -0700208
209 irq_domain_add(domain);
Grant Likelya8db8cf2012-02-14 14:06:54 -0700210 return domain;
211}
Grant Likelycc79ca62012-02-16 01:37:49 -0700212
Grant Likelya8db8cf2012-02-14 14:06:54 -0700213/**
214 * irq_domain_add_linear() - Allocate and register a legacy revmap irq_domain.
215 * @of_node: pointer to interrupt controller's device tree node.
216 * @ops: map/unmap domain callbacks
217 * @host_data: Controller private data pointer
218 */
219struct irq_domain *irq_domain_add_linear(struct device_node *of_node,
220 unsigned int size,
Grant Likelya18dc812012-01-26 12:12:14 -0700221 const struct irq_domain_ops *ops,
Grant Likelya8db8cf2012-02-14 14:06:54 -0700222 void *host_data)
223{
224 struct irq_domain *domain;
225 unsigned int *revmap;
Grant Likelycc79ca62012-02-16 01:37:49 -0700226
Grant Likelya8db8cf2012-02-14 14:06:54 -0700227 revmap = kzalloc(sizeof(*revmap) * size, GFP_KERNEL);
228 if (WARN_ON(!revmap))
229 return NULL;
230
231 domain = irq_domain_alloc(of_node, IRQ_DOMAIN_MAP_LINEAR, ops, host_data);
232 if (!domain) {
233 kfree(revmap);
234 return NULL;
235 }
236 domain->revmap_data.linear.size = size;
237 domain->revmap_data.linear.revmap = revmap;
238 irq_domain_add(domain);
239 return domain;
240}
241
242struct irq_domain *irq_domain_add_nomap(struct device_node *of_node,
Grant Likely6fa6c8e22012-02-15 15:06:08 -0700243 unsigned int max_irq,
Grant Likelya18dc812012-01-26 12:12:14 -0700244 const struct irq_domain_ops *ops,
Grant Likelya8db8cf2012-02-14 14:06:54 -0700245 void *host_data)
246{
247 struct irq_domain *domain = irq_domain_alloc(of_node,
248 IRQ_DOMAIN_MAP_NOMAP, ops, host_data);
Grant Likely6fa6c8e22012-02-15 15:06:08 -0700249 if (domain) {
250 domain->revmap_data.nomap.max_irq = max_irq ? max_irq : ~0;
Grant Likelya8db8cf2012-02-14 14:06:54 -0700251 irq_domain_add(domain);
Grant Likely6fa6c8e22012-02-15 15:06:08 -0700252 }
Grant Likelya8db8cf2012-02-14 14:06:54 -0700253 return domain;
254}
255
256/**
257 * irq_domain_add_tree()
258 * @of_node: pointer to interrupt controller's device tree node.
259 * @ops: map/unmap domain callbacks
260 *
261 * Note: The radix tree will be allocated later during boot automatically
262 * (the reverse mapping will use the slow path until that happens).
263 */
264struct irq_domain *irq_domain_add_tree(struct device_node *of_node,
Grant Likelya18dc812012-01-26 12:12:14 -0700265 const struct irq_domain_ops *ops,
Grant Likelya8db8cf2012-02-14 14:06:54 -0700266 void *host_data)
267{
268 struct irq_domain *domain = irq_domain_alloc(of_node,
269 IRQ_DOMAIN_MAP_TREE, ops, host_data);
270 if (domain) {
271 INIT_RADIX_TREE(&domain->revmap_data.tree, GFP_KERNEL);
272 irq_domain_add(domain);
273 }
Grant Likely68700652012-02-14 14:06:53 -0700274 return domain;
Grant Likelycc79ca62012-02-16 01:37:49 -0700275}
276
277/**
278 * irq_find_host() - Locates a domain for a given device node
279 * @node: device-tree node of the interrupt controller
280 */
281struct irq_domain *irq_find_host(struct device_node *node)
282{
283 struct irq_domain *h, *found = NULL;
Grant Likelya18dc812012-01-26 12:12:14 -0700284 int rc;
Grant Likelycc79ca62012-02-16 01:37:49 -0700285
286 /* We might want to match the legacy controller last since
287 * it might potentially be set to match all interrupts in
288 * the absence of a device node. This isn't a problem so far
289 * yet though...
290 */
291 mutex_lock(&irq_domain_mutex);
Grant Likelya18dc812012-01-26 12:12:14 -0700292 list_for_each_entry(h, &irq_domain_list, link) {
293 if (h->ops->match)
294 rc = h->ops->match(h, node);
295 else
296 rc = (h->of_node != NULL) && (h->of_node == node);
297
298 if (rc) {
Grant Likelycc79ca62012-02-16 01:37:49 -0700299 found = h;
300 break;
301 }
Grant Likelya18dc812012-01-26 12:12:14 -0700302 }
Grant Likelycc79ca62012-02-16 01:37:49 -0700303 mutex_unlock(&irq_domain_mutex);
304 return found;
305}
306EXPORT_SYMBOL_GPL(irq_find_host);
307
308/**
309 * irq_set_default_host() - Set a "default" irq domain
Grant Likely68700652012-02-14 14:06:53 -0700310 * @domain: default domain pointer
Grant Likelycc79ca62012-02-16 01:37:49 -0700311 *
312 * For convenience, it's possible to set a "default" domain that will be used
313 * whenever NULL is passed to irq_create_mapping(). It makes life easier for
314 * platforms that want to manipulate a few hard coded interrupt numbers that
315 * aren't properly represented in the device-tree.
316 */
Grant Likely68700652012-02-14 14:06:53 -0700317void irq_set_default_host(struct irq_domain *domain)
Grant Likelycc79ca62012-02-16 01:37:49 -0700318{
Grant Likely68700652012-02-14 14:06:53 -0700319 pr_debug("irq: Default domain set to @0x%p\n", domain);
Grant Likelycc79ca62012-02-16 01:37:49 -0700320
Grant Likely68700652012-02-14 14:06:53 -0700321 irq_default_domain = domain;
Grant Likelycc79ca62012-02-16 01:37:49 -0700322}
323
Grant Likely68700652012-02-14 14:06:53 -0700324static int irq_setup_virq(struct irq_domain *domain, unsigned int virq,
Grant Likelycc79ca62012-02-16 01:37:49 -0700325 irq_hw_number_t hwirq)
326{
327 struct irq_data *irq_data = irq_get_irq_data(virq);
328
329 irq_data->hwirq = hwirq;
Grant Likely68700652012-02-14 14:06:53 -0700330 irq_data->domain = domain;
331 if (domain->ops->map(domain, virq, hwirq)) {
Grant Likelycc79ca62012-02-16 01:37:49 -0700332 pr_debug("irq: -> mapping failed, freeing\n");
333 irq_data->domain = NULL;
334 irq_data->hwirq = 0;
335 return -1;
336 }
337
338 irq_clear_status_flags(virq, IRQ_NOREQUEST);
339
340 return 0;
341}
342
343/**
344 * irq_create_direct_mapping() - Allocate an irq for direct mapping
Grant Likely68700652012-02-14 14:06:53 -0700345 * @domain: domain to allocate the irq for or NULL for default domain
Grant Likelycc79ca62012-02-16 01:37:49 -0700346 *
347 * This routine is used for irq controllers which can choose the hardware
348 * interrupt numbers they generate. In such a case it's simplest to use
349 * the linux irq as the hardware interrupt number.
350 */
Grant Likely68700652012-02-14 14:06:53 -0700351unsigned int irq_create_direct_mapping(struct irq_domain *domain)
Grant Likelycc79ca62012-02-16 01:37:49 -0700352{
353 unsigned int virq;
354
Grant Likely68700652012-02-14 14:06:53 -0700355 if (domain == NULL)
356 domain = irq_default_domain;
Grant Likelycc79ca62012-02-16 01:37:49 -0700357
Grant Likely68700652012-02-14 14:06:53 -0700358 BUG_ON(domain == NULL);
359 WARN_ON(domain->revmap_type != IRQ_DOMAIN_MAP_NOMAP);
Grant Likelycc79ca62012-02-16 01:37:49 -0700360
361 virq = irq_alloc_desc_from(1, 0);
Grant Likely03848372012-02-14 14:06:52 -0700362 if (!virq) {
Grant Likelycc79ca62012-02-16 01:37:49 -0700363 pr_debug("irq: create_direct virq allocation failed\n");
Grant Likely03848372012-02-14 14:06:52 -0700364 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700365 }
Grant Likely6fa6c8e22012-02-15 15:06:08 -0700366 if (virq >= domain->revmap_data.nomap.max_irq) {
Grant Likelycc79ca62012-02-16 01:37:49 -0700367 pr_err("ERROR: no free irqs available below %i maximum\n",
Grant Likely6fa6c8e22012-02-15 15:06:08 -0700368 domain->revmap_data.nomap.max_irq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700369 irq_free_desc(virq);
370 return 0;
371 }
Grant Likelycc79ca62012-02-16 01:37:49 -0700372 pr_debug("irq: create_direct obtained virq %d\n", virq);
373
Grant Likely68700652012-02-14 14:06:53 -0700374 if (irq_setup_virq(domain, virq, virq)) {
Grant Likelycc79ca62012-02-16 01:37:49 -0700375 irq_free_desc(virq);
Grant Likely03848372012-02-14 14:06:52 -0700376 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700377 }
378
379 return virq;
380}
381
382/**
383 * irq_create_mapping() - Map a hardware interrupt into linux irq space
Grant Likely68700652012-02-14 14:06:53 -0700384 * @domain: domain owning this hardware interrupt or NULL for default domain
385 * @hwirq: hardware irq number in that domain space
Grant Likelycc79ca62012-02-16 01:37:49 -0700386 *
387 * Only one mapping per hardware interrupt is permitted. Returns a linux
388 * irq number.
389 * If the sense/trigger is to be specified, set_irq_type() should be called
390 * on the number returned from that call.
391 */
Grant Likely68700652012-02-14 14:06:53 -0700392unsigned int irq_create_mapping(struct irq_domain *domain,
Grant Likelycc79ca62012-02-16 01:37:49 -0700393 irq_hw_number_t hwirq)
394{
David Daney5b7526e2012-04-05 16:52:13 -0700395 unsigned int hint;
396 int virq;
Grant Likelycc79ca62012-02-16 01:37:49 -0700397
Grant Likely68700652012-02-14 14:06:53 -0700398 pr_debug("irq: irq_create_mapping(0x%p, 0x%lx)\n", domain, hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700399
Grant Likely68700652012-02-14 14:06:53 -0700400 /* Look for default domain if nececssary */
401 if (domain == NULL)
402 domain = irq_default_domain;
403 if (domain == NULL) {
Grant Likelycc79ca62012-02-16 01:37:49 -0700404 printk(KERN_WARNING "irq_create_mapping called for"
Grant Likely68700652012-02-14 14:06:53 -0700405 " NULL domain, hwirq=%lx\n", hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700406 WARN_ON(1);
Grant Likely03848372012-02-14 14:06:52 -0700407 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700408 }
Grant Likely68700652012-02-14 14:06:53 -0700409 pr_debug("irq: -> using domain @%p\n", domain);
Grant Likelycc79ca62012-02-16 01:37:49 -0700410
411 /* Check if mapping already exists */
Grant Likely68700652012-02-14 14:06:53 -0700412 virq = irq_find_mapping(domain, hwirq);
Grant Likely03848372012-02-14 14:06:52 -0700413 if (virq) {
Grant Likelycc79ca62012-02-16 01:37:49 -0700414 pr_debug("irq: -> existing mapping on virq %d\n", virq);
415 return virq;
416 }
417
418 /* Get a virtual interrupt number */
Grant Likely1bc04f22012-02-14 14:06:55 -0700419 if (domain->revmap_type == IRQ_DOMAIN_MAP_LEGACY)
420 return irq_domain_legacy_revmap(domain, hwirq);
421
422 /* Allocate a virtual interrupt number */
Grant Likely6fa6c8e22012-02-15 15:06:08 -0700423 hint = hwirq % nr_irqs;
Grant Likely1bc04f22012-02-14 14:06:55 -0700424 if (hint == 0)
425 hint++;
426 virq = irq_alloc_desc_from(hint, 0);
David Daney5b7526e2012-04-05 16:52:13 -0700427 if (virq <= 0)
Grant Likely1bc04f22012-02-14 14:06:55 -0700428 virq = irq_alloc_desc_from(1, 0);
David Daney5b7526e2012-04-05 16:52:13 -0700429 if (virq <= 0) {
Grant Likely1bc04f22012-02-14 14:06:55 -0700430 pr_debug("irq: -> virq allocation failed\n");
431 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700432 }
433
Grant Likely68700652012-02-14 14:06:53 -0700434 if (irq_setup_virq(domain, virq, hwirq)) {
435 if (domain->revmap_type != IRQ_DOMAIN_MAP_LEGACY)
Grant Likelycc79ca62012-02-16 01:37:49 -0700436 irq_free_desc(virq);
Grant Likely03848372012-02-14 14:06:52 -0700437 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700438 }
439
Grant Likely68700652012-02-14 14:06:53 -0700440 pr_debug("irq: irq %lu on domain %s mapped to virtual irq %u\n",
441 hwirq, domain->of_node ? domain->of_node->full_name : "null", virq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700442
443 return virq;
444}
445EXPORT_SYMBOL_GPL(irq_create_mapping);
446
447unsigned int irq_create_of_mapping(struct device_node *controller,
448 const u32 *intspec, unsigned int intsize)
449{
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 unsigned int type = IRQ_TYPE_NONE;
453 unsigned int virq;
454
Grant Likely68700652012-02-14 14:06:53 -0700455 domain = controller ? irq_find_host(controller) : irq_default_domain;
456 if (!domain) {
Grant Likelyabd23632012-02-24 08:07:06 -0700457#ifdef CONFIG_MIPS
458 /*
459 * Workaround to avoid breaking interrupt controller drivers
460 * that don't yet register an irq_domain. This is temporary
461 * code. ~~~gcl, Feb 24, 2012
462 *
463 * Scheduled for removal in Linux v3.6. That should be enough
464 * time.
465 */
466 if (intsize > 0)
467 return intspec[0];
468#endif
Grant Likely68700652012-02-14 14:06:53 -0700469 printk(KERN_WARNING "irq: no irq domain found for %s !\n",
Grant Likelycc79ca62012-02-16 01:37:49 -0700470 controller->full_name);
Grant Likely03848372012-02-14 14:06:52 -0700471 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700472 }
473
Grant Likely68700652012-02-14 14:06:53 -0700474 /* If domain has no translation, then we assume interrupt line */
475 if (domain->ops->xlate == NULL)
Grant Likelycc79ca62012-02-16 01:37:49 -0700476 hwirq = intspec[0];
477 else {
Grant Likely68700652012-02-14 14:06:53 -0700478 if (domain->ops->xlate(domain, controller, intspec, intsize,
Grant Likelycc79ca62012-02-16 01:37:49 -0700479 &hwirq, &type))
Grant Likely03848372012-02-14 14:06:52 -0700480 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700481 }
482
483 /* Create mapping */
Grant Likely68700652012-02-14 14:06:53 -0700484 virq = irq_create_mapping(domain, hwirq);
Grant Likely03848372012-02-14 14:06:52 -0700485 if (!virq)
Grant Likelycc79ca62012-02-16 01:37:49 -0700486 return virq;
487
488 /* Set type if specified and different than the current one */
489 if (type != IRQ_TYPE_NONE &&
490 type != (irqd_get_trigger_type(irq_get_irq_data(virq))))
491 irq_set_irq_type(virq, type);
492 return virq;
493}
494EXPORT_SYMBOL_GPL(irq_create_of_mapping);
495
496/**
497 * irq_dispose_mapping() - Unmap an interrupt
498 * @virq: linux irq number of the interrupt to unmap
499 */
500void irq_dispose_mapping(unsigned int virq)
501{
502 struct irq_data *irq_data = irq_get_irq_data(virq);
Grant Likely68700652012-02-14 14:06:53 -0700503 struct irq_domain *domain;
Grant Likelycc79ca62012-02-16 01:37:49 -0700504 irq_hw_number_t hwirq;
505
Grant Likely03848372012-02-14 14:06:52 -0700506 if (!virq || !irq_data)
Grant Likelycc79ca62012-02-16 01:37:49 -0700507 return;
508
Grant Likely68700652012-02-14 14:06:53 -0700509 domain = irq_data->domain;
510 if (WARN_ON(domain == NULL))
Grant Likelycc79ca62012-02-16 01:37:49 -0700511 return;
512
513 /* Never unmap legacy interrupts */
Grant Likely68700652012-02-14 14:06:53 -0700514 if (domain->revmap_type == IRQ_DOMAIN_MAP_LEGACY)
Grant Likelycc79ca62012-02-16 01:37:49 -0700515 return;
516
517 irq_set_status_flags(virq, IRQ_NOREQUEST);
518
519 /* remove chip and handler */
520 irq_set_chip_and_handler(virq, NULL, NULL);
521
522 /* Make sure it's completed */
523 synchronize_irq(virq);
524
525 /* Tell the PIC about it */
Grant Likely68700652012-02-14 14:06:53 -0700526 if (domain->ops->unmap)
527 domain->ops->unmap(domain, virq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700528 smp_mb();
529
530 /* Clear reverse map */
531 hwirq = irq_data->hwirq;
Grant Likely68700652012-02-14 14:06:53 -0700532 switch(domain->revmap_type) {
Grant Likelycc79ca62012-02-16 01:37:49 -0700533 case IRQ_DOMAIN_MAP_LINEAR:
Grant Likely68700652012-02-14 14:06:53 -0700534 if (hwirq < domain->revmap_data.linear.size)
535 domain->revmap_data.linear.revmap[hwirq] = 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700536 break;
537 case IRQ_DOMAIN_MAP_TREE:
538 mutex_lock(&revmap_trees_mutex);
Grant Likely68700652012-02-14 14:06:53 -0700539 radix_tree_delete(&domain->revmap_data.tree, hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700540 mutex_unlock(&revmap_trees_mutex);
541 break;
542 }
543
Grant Likelycc79ca62012-02-16 01:37:49 -0700544 irq_free_desc(virq);
545}
546EXPORT_SYMBOL_GPL(irq_dispose_mapping);
547
548/**
549 * irq_find_mapping() - Find a linux irq from an hw irq number.
Grant Likely68700652012-02-14 14:06:53 -0700550 * @domain: domain owning this hardware interrupt
551 * @hwirq: hardware irq number in that domain space
Grant Likelycc79ca62012-02-16 01:37:49 -0700552 *
553 * This is a slow path, for use by generic code. It's expected that an
554 * irq controller implementation directly calls the appropriate low level
555 * mapping function.
556 */
Grant Likely68700652012-02-14 14:06:53 -0700557unsigned int irq_find_mapping(struct irq_domain *domain,
Grant Likelycc79ca62012-02-16 01:37:49 -0700558 irq_hw_number_t hwirq)
559{
560 unsigned int i;
Grant Likely6fa6c8e22012-02-15 15:06:08 -0700561 unsigned int hint = hwirq % nr_irqs;
Grant Likelycc79ca62012-02-16 01:37:49 -0700562
Grant Likely68700652012-02-14 14:06:53 -0700563 /* Look for default domain if nececssary */
564 if (domain == NULL)
565 domain = irq_default_domain;
566 if (domain == NULL)
Grant Likely03848372012-02-14 14:06:52 -0700567 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700568
569 /* legacy -> bail early */
Grant Likely68700652012-02-14 14:06:53 -0700570 if (domain->revmap_type == IRQ_DOMAIN_MAP_LEGACY)
Grant Likely1bc04f22012-02-14 14:06:55 -0700571 return irq_domain_legacy_revmap(domain, hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700572
573 /* Slow path does a linear search of the map */
574 if (hint == 0)
575 hint = 1;
576 i = hint;
577 do {
578 struct irq_data *data = irq_get_irq_data(i);
Grant Likely68700652012-02-14 14:06:53 -0700579 if (data && (data->domain == domain) && (data->hwirq == hwirq))
Grant Likelycc79ca62012-02-16 01:37:49 -0700580 return i;
581 i++;
Grant Likely6fa6c8e22012-02-15 15:06:08 -0700582 if (i >= nr_irqs)
Grant Likelycc79ca62012-02-16 01:37:49 -0700583 i = 1;
584 } while(i != hint);
Grant Likely03848372012-02-14 14:06:52 -0700585 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700586}
587EXPORT_SYMBOL_GPL(irq_find_mapping);
588
589/**
590 * irq_radix_revmap_lookup() - Find a linux irq from a hw irq number.
Grant Likely68700652012-02-14 14:06:53 -0700591 * @domain: domain owning this hardware interrupt
592 * @hwirq: hardware irq number in that domain space
Grant Likelycc79ca62012-02-16 01:37:49 -0700593 *
594 * This is a fast path, for use by irq controller code that uses radix tree
595 * revmaps
596 */
Grant Likely68700652012-02-14 14:06:53 -0700597unsigned int irq_radix_revmap_lookup(struct irq_domain *domain,
Grant Likelycc79ca62012-02-16 01:37:49 -0700598 irq_hw_number_t hwirq)
599{
600 struct irq_data *irq_data;
601
Grant Likely68700652012-02-14 14:06:53 -0700602 if (WARN_ON_ONCE(domain->revmap_type != IRQ_DOMAIN_MAP_TREE))
603 return irq_find_mapping(domain, hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700604
605 /*
606 * Freeing an irq can delete nodes along the path to
607 * do the lookup via call_rcu.
608 */
609 rcu_read_lock();
Grant Likely68700652012-02-14 14:06:53 -0700610 irq_data = radix_tree_lookup(&domain->revmap_data.tree, hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700611 rcu_read_unlock();
612
613 /*
614 * If found in radix tree, then fine.
615 * Else fallback to linear lookup - this should not happen in practice
616 * as it means that we failed to insert the node in the radix tree.
617 */
Grant Likely68700652012-02-14 14:06:53 -0700618 return irq_data ? irq_data->irq : irq_find_mapping(domain, hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700619}
620
621/**
622 * irq_radix_revmap_insert() - Insert a hw irq to linux irq number mapping.
Grant Likely68700652012-02-14 14:06:53 -0700623 * @domain: domain owning this hardware interrupt
Grant Likelycc79ca62012-02-16 01:37:49 -0700624 * @virq: linux irq number
Grant Likely68700652012-02-14 14:06:53 -0700625 * @hwirq: hardware irq number in that domain space
Grant Likelycc79ca62012-02-16 01:37:49 -0700626 *
627 * This is for use by irq controllers that use a radix tree reverse
628 * mapping for fast lookup.
629 */
Grant Likely68700652012-02-14 14:06:53 -0700630void irq_radix_revmap_insert(struct irq_domain *domain, unsigned int virq,
Grant Likelycc79ca62012-02-16 01:37:49 -0700631 irq_hw_number_t hwirq)
632{
633 struct irq_data *irq_data = irq_get_irq_data(virq);
634
Grant Likely68700652012-02-14 14:06:53 -0700635 if (WARN_ON(domain->revmap_type != IRQ_DOMAIN_MAP_TREE))
Grant Likelycc79ca62012-02-16 01:37:49 -0700636 return;
637
Grant Likely03848372012-02-14 14:06:52 -0700638 if (virq) {
Grant Likelycc79ca62012-02-16 01:37:49 -0700639 mutex_lock(&revmap_trees_mutex);
Grant Likely68700652012-02-14 14:06:53 -0700640 radix_tree_insert(&domain->revmap_data.tree, hwirq, irq_data);
Grant Likelycc79ca62012-02-16 01:37:49 -0700641 mutex_unlock(&revmap_trees_mutex);
642 }
643}
644
645/**
646 * irq_linear_revmap() - Find a linux irq from a hw irq number.
Grant Likely68700652012-02-14 14:06:53 -0700647 * @domain: domain owning this hardware interrupt
648 * @hwirq: hardware irq number in that domain space
Grant Likelycc79ca62012-02-16 01:37:49 -0700649 *
650 * This is a fast path, for use by irq controller code that uses linear
651 * revmaps. It does fallback to the slow path if the revmap doesn't exist
652 * yet and will create the revmap entry with appropriate locking
653 */
Grant Likely68700652012-02-14 14:06:53 -0700654unsigned int irq_linear_revmap(struct irq_domain *domain,
Grant Likelycc79ca62012-02-16 01:37:49 -0700655 irq_hw_number_t hwirq)
656{
657 unsigned int *revmap;
658
Grant Likely68700652012-02-14 14:06:53 -0700659 if (WARN_ON_ONCE(domain->revmap_type != IRQ_DOMAIN_MAP_LINEAR))
660 return irq_find_mapping(domain, hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700661
662 /* Check revmap bounds */
Grant Likely68700652012-02-14 14:06:53 -0700663 if (unlikely(hwirq >= domain->revmap_data.linear.size))
664 return irq_find_mapping(domain, hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700665
666 /* Check if revmap was allocated */
Grant Likely68700652012-02-14 14:06:53 -0700667 revmap = domain->revmap_data.linear.revmap;
Grant Likelycc79ca62012-02-16 01:37:49 -0700668 if (unlikely(revmap == NULL))
Grant Likely68700652012-02-14 14:06:53 -0700669 return irq_find_mapping(domain, hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700670
671 /* Fill up revmap with slow path if no mapping found */
Grant Likely03848372012-02-14 14:06:52 -0700672 if (unlikely(!revmap[hwirq]))
Grant Likely68700652012-02-14 14:06:53 -0700673 revmap[hwirq] = irq_find_mapping(domain, hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700674
675 return revmap[hwirq];
676}
677
Grant Likely092b2fb2012-03-29 14:10:30 -0600678#ifdef CONFIG_IRQ_DOMAIN_DEBUG
Grant Likelycc79ca62012-02-16 01:37:49 -0700679static int virq_debug_show(struct seq_file *m, void *private)
680{
681 unsigned long flags;
682 struct irq_desc *desc;
683 const char *p;
684 static const char none[] = "none";
685 void *data;
686 int i;
687
Grant Likely15e06bf2012-04-11 00:26:25 -0600688 seq_printf(m, "%-5s %-7s %-15s %-*s %s\n", "irq", "hwirq",
Grant Likely5269a9a2012-04-12 14:42:15 -0600689 "chip name", (int)(2 * sizeof(void *) + 2), "chip data",
690 "domain name");
Grant Likelycc79ca62012-02-16 01:37:49 -0700691
692 for (i = 1; i < nr_irqs; i++) {
693 desc = irq_to_desc(i);
694 if (!desc)
695 continue;
696
697 raw_spin_lock_irqsave(&desc->lock, flags);
698
699 if (desc->action && desc->action->handler) {
700 struct irq_chip *chip;
701
702 seq_printf(m, "%5d ", i);
703 seq_printf(m, "0x%05lx ", desc->irq_data.hwirq);
704
705 chip = irq_desc_get_chip(desc);
706 if (chip && chip->name)
707 p = chip->name;
708 else
709 p = none;
710 seq_printf(m, "%-15s ", p);
711
712 data = irq_desc_get_chip_data(desc);
Grant Likely15e06bf2012-04-11 00:26:25 -0600713 seq_printf(m, data ? "0x%p " : " %p ", data);
Grant Likelycc79ca62012-02-16 01:37:49 -0700714
Grant Likely092b2fb2012-03-29 14:10:30 -0600715 if (desc->irq_data.domain && desc->irq_data.domain->of_node)
Grant Likelycc79ca62012-02-16 01:37:49 -0700716 p = desc->irq_data.domain->of_node->full_name;
717 else
718 p = none;
719 seq_printf(m, "%s\n", p);
720 }
721
722 raw_spin_unlock_irqrestore(&desc->lock, flags);
723 }
724
725 return 0;
726}
727
728static int virq_debug_open(struct inode *inode, struct file *file)
729{
730 return single_open(file, virq_debug_show, inode->i_private);
731}
732
733static const struct file_operations virq_debug_fops = {
734 .open = virq_debug_open,
735 .read = seq_read,
736 .llseek = seq_lseek,
737 .release = single_release,
738};
739
740static int __init irq_debugfs_init(void)
741{
Grant Likely092b2fb2012-03-29 14:10:30 -0600742 if (debugfs_create_file("irq_domain_mapping", S_IRUGO, NULL,
Grant Likelycc79ca62012-02-16 01:37:49 -0700743 NULL, &virq_debug_fops) == NULL)
744 return -ENOMEM;
745
746 return 0;
747}
748__initcall(irq_debugfs_init);
Grant Likely092b2fb2012-03-29 14:10:30 -0600749#endif /* CONFIG_IRQ_DOMAIN_DEBUG */
Grant Likelycc79ca62012-02-16 01:37:49 -0700750
Grant Likely75294952012-02-14 14:06:57 -0700751int irq_domain_simple_map(struct irq_domain *d, unsigned int irq,
752 irq_hw_number_t hwirq)
Grant Likely08a543a2011-07-26 03:19:06 -0600753{
Grant Likely75294952012-02-14 14:06:57 -0700754 return 0;
Grant Likely08a543a2011-07-26 03:19:06 -0600755}
756
Grant Likely16b2e6e2012-01-26 11:26:52 -0700757/**
758 * irq_domain_xlate_onecell() - Generic xlate for direct one cell bindings
759 *
760 * Device Tree IRQ specifier translation function which works with one cell
761 * bindings where the cell value maps directly to the hwirq number.
762 */
763int irq_domain_xlate_onecell(struct irq_domain *d, struct device_node *ctrlr,
764 const u32 *intspec, unsigned int intsize,
765 unsigned long *out_hwirq, unsigned int *out_type)
Grant Likely7e713302011-07-26 03:19:06 -0600766{
Grant Likely16b2e6e2012-01-26 11:26:52 -0700767 if (WARN_ON(intsize < 1))
Grant Likely7e713302011-07-26 03:19:06 -0600768 return -EINVAL;
Grant Likely7e713302011-07-26 03:19:06 -0600769 *out_hwirq = intspec[0];
770 *out_type = IRQ_TYPE_NONE;
Grant Likely7e713302011-07-26 03:19:06 -0600771 return 0;
772}
Grant Likely16b2e6e2012-01-26 11:26:52 -0700773EXPORT_SYMBOL_GPL(irq_domain_xlate_onecell);
774
775/**
776 * irq_domain_xlate_twocell() - Generic xlate for direct two cell bindings
777 *
778 * Device Tree IRQ specifier translation function which works with two cell
779 * bindings where the cell values map directly to the hwirq number
780 * and linux irq flags.
781 */
782int irq_domain_xlate_twocell(struct irq_domain *d, struct device_node *ctrlr,
783 const u32 *intspec, unsigned int intsize,
784 irq_hw_number_t *out_hwirq, unsigned int *out_type)
785{
786 if (WARN_ON(intsize < 2))
787 return -EINVAL;
788 *out_hwirq = intspec[0];
789 *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK;
790 return 0;
791}
792EXPORT_SYMBOL_GPL(irq_domain_xlate_twocell);
793
794/**
795 * irq_domain_xlate_onetwocell() - Generic xlate for one or two cell bindings
796 *
797 * Device Tree IRQ specifier translation function which works with either one
798 * or two cell bindings where the cell values map directly to the hwirq number
799 * and linux irq flags.
800 *
801 * Note: don't use this function unless your interrupt controller explicitly
802 * supports both one and two cell bindings. For the majority of controllers
803 * the _onecell() or _twocell() variants above should be used.
804 */
805int irq_domain_xlate_onetwocell(struct irq_domain *d,
806 struct device_node *ctrlr,
807 const u32 *intspec, unsigned int intsize,
808 unsigned long *out_hwirq, unsigned int *out_type)
809{
810 if (WARN_ON(intsize < 1))
811 return -EINVAL;
812 *out_hwirq = intspec[0];
813 *out_type = (intsize > 1) ? intspec[1] : IRQ_TYPE_NONE;
814 return 0;
815}
816EXPORT_SYMBOL_GPL(irq_domain_xlate_onetwocell);
Grant Likely7e713302011-07-26 03:19:06 -0600817
Grant Likelya18dc812012-01-26 12:12:14 -0700818const struct irq_domain_ops irq_domain_simple_ops = {
Grant Likely75294952012-02-14 14:06:57 -0700819 .map = irq_domain_simple_map,
Grant Likely16b2e6e2012-01-26 11:26:52 -0700820 .xlate = irq_domain_xlate_onetwocell,
Grant Likely75294952012-02-14 14:06:57 -0700821};
822EXPORT_SYMBOL_GPL(irq_domain_simple_ops);
823
824#ifdef CONFIG_OF_IRQ
Grant Likely7e713302011-07-26 03:19:06 -0600825void irq_domain_generate_simple(const struct of_device_id *match,
826 u64 phys_base, unsigned int irq_start)
827{
828 struct device_node *node;
Grant Likelye1964c52012-02-14 14:06:48 -0700829 pr_debug("looking for phys_base=%llx, irq_start=%i\n",
Grant Likely7e713302011-07-26 03:19:06 -0600830 (unsigned long long) phys_base, (int) irq_start);
831 node = of_find_matching_node_by_address(NULL, match, phys_base);
832 if (node)
Grant Likely6b783f72012-01-10 17:09:30 -0700833 irq_domain_add_legacy(node, 32, irq_start, 0,
834 &irq_domain_simple_ops, NULL);
Grant Likely7e713302011-07-26 03:19:06 -0600835}
836EXPORT_SYMBOL_GPL(irq_domain_generate_simple);
Grant Likely75294952012-02-14 14:06:57 -0700837#endif