blob: 01d4a0de061f5a5c1b1aaf7c3ce3e8274ba5a24c [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}
Paul Mundtecd84eb2012-05-19 15:11:42 +0900125EXPORT_SYMBOL_GPL(irq_domain_remove);
Paul Mundt58ee99a2012-05-19 15:11:41 +0900126
Grant Likely1bc04f22012-02-14 14:06:55 -0700127static unsigned int irq_domain_legacy_revmap(struct irq_domain *domain,
128 irq_hw_number_t hwirq)
129{
130 irq_hw_number_t first_hwirq = domain->revmap_data.legacy.first_hwirq;
131 int size = domain->revmap_data.legacy.size;
132
133 if (WARN_ON(hwirq < first_hwirq || hwirq >= first_hwirq + size))
134 return 0;
135 return hwirq - first_hwirq + domain->revmap_data.legacy.first_irq;
136}
137
Grant Likelya8db8cf2012-02-14 14:06:54 -0700138/**
139 * irq_domain_add_legacy() - Allocate and register a legacy revmap irq_domain.
140 * @of_node: pointer to interrupt controller's device tree node.
Grant Likely1bc04f22012-02-14 14:06:55 -0700141 * @size: total number of irqs in legacy mapping
142 * @first_irq: first number of irq block assigned to the domain
143 * @first_hwirq: first hwirq number to use for the translation. Should normally
144 * be '0', but a positive integer can be used if the effective
145 * hwirqs numbering does not begin at zero.
Grant Likelya8db8cf2012-02-14 14:06:54 -0700146 * @ops: map/unmap domain callbacks
147 * @host_data: Controller private data pointer
148 *
149 * Note: the map() callback will be called before this function returns
150 * for all legacy interrupts except 0 (which is always the invalid irq for
151 * a legacy controller).
152 */
153struct irq_domain *irq_domain_add_legacy(struct device_node *of_node,
Grant Likely1bc04f22012-02-14 14:06:55 -0700154 unsigned int size,
155 unsigned int first_irq,
156 irq_hw_number_t first_hwirq,
Grant Likelya18dc812012-01-26 12:12:14 -0700157 const struct irq_domain_ops *ops,
Grant Likelya8db8cf2012-02-14 14:06:54 -0700158 void *host_data)
159{
Grant Likely1bc04f22012-02-14 14:06:55 -0700160 struct irq_domain *domain;
Grant Likelya8db8cf2012-02-14 14:06:54 -0700161 unsigned int i;
162
163 domain = irq_domain_alloc(of_node, IRQ_DOMAIN_MAP_LEGACY, ops, host_data);
164 if (!domain)
165 return NULL;
166
Grant Likely1bc04f22012-02-14 14:06:55 -0700167 domain->revmap_data.legacy.first_irq = first_irq;
168 domain->revmap_data.legacy.first_hwirq = first_hwirq;
169 domain->revmap_data.legacy.size = size;
170
Grant Likelycc79ca62012-02-16 01:37:49 -0700171 mutex_lock(&irq_domain_mutex);
Grant Likely1bc04f22012-02-14 14:06:55 -0700172 /* Verify that all the irqs are available */
173 for (i = 0; i < size; i++) {
174 int irq = first_irq + i;
175 struct irq_data *irq_data = irq_get_irq_data(irq);
176
177 if (WARN_ON(!irq_data || irq_data->domain)) {
Grant Likelya8db8cf2012-02-14 14:06:54 -0700178 mutex_unlock(&irq_domain_mutex);
Paul Mundt58ee99a2012-05-19 15:11:41 +0900179 irq_domain_free(domain);
Grant Likelya8db8cf2012-02-14 14:06:54 -0700180 return NULL;
Grant Likelycc79ca62012-02-16 01:37:49 -0700181 }
182 }
Grant Likely1bc04f22012-02-14 14:06:55 -0700183
184 /* Claim all of the irqs before registering a legacy domain */
185 for (i = 0; i < size; i++) {
186 struct irq_data *irq_data = irq_get_irq_data(first_irq + i);
187 irq_data->hwirq = first_hwirq + i;
188 irq_data->domain = domain;
189 }
Grant Likelycc79ca62012-02-16 01:37:49 -0700190 mutex_unlock(&irq_domain_mutex);
191
Grant Likely1bc04f22012-02-14 14:06:55 -0700192 for (i = 0; i < size; i++) {
193 int irq = first_irq + i;
194 int hwirq = first_hwirq + i;
195
196 /* IRQ0 gets ignored */
197 if (!irq)
198 continue;
Grant Likelycc79ca62012-02-16 01:37:49 -0700199
Grant Likelya8db8cf2012-02-14 14:06:54 -0700200 /* Legacy flags are left to default at this point,
201 * one can then use irq_create_mapping() to
202 * explicitly change them
203 */
Grant Likely1bc04f22012-02-14 14:06:55 -0700204 ops->map(domain, irq, hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700205
Grant Likelya8db8cf2012-02-14 14:06:54 -0700206 /* Clear norequest flags */
Grant Likely1bc04f22012-02-14 14:06:55 -0700207 irq_clear_status_flags(irq, IRQ_NOREQUEST);
Grant Likelycc79ca62012-02-16 01:37:49 -0700208 }
Grant Likely1bc04f22012-02-14 14:06:55 -0700209
210 irq_domain_add(domain);
Grant Likelya8db8cf2012-02-14 14:06:54 -0700211 return domain;
212}
Paul Mundtecd84eb2012-05-19 15:11:42 +0900213EXPORT_SYMBOL_GPL(irq_domain_add_legacy);
Grant Likelycc79ca62012-02-16 01:37:49 -0700214
Grant Likelya8db8cf2012-02-14 14:06:54 -0700215/**
216 * irq_domain_add_linear() - Allocate and register a legacy revmap irq_domain.
217 * @of_node: pointer to interrupt controller's device tree node.
218 * @ops: map/unmap domain callbacks
219 * @host_data: Controller private data pointer
220 */
221struct irq_domain *irq_domain_add_linear(struct device_node *of_node,
222 unsigned int size,
Grant Likelya18dc812012-01-26 12:12:14 -0700223 const struct irq_domain_ops *ops,
Grant Likelya8db8cf2012-02-14 14:06:54 -0700224 void *host_data)
225{
226 struct irq_domain *domain;
227 unsigned int *revmap;
Grant Likelycc79ca62012-02-16 01:37:49 -0700228
Grant Likelya8db8cf2012-02-14 14:06:54 -0700229 revmap = kzalloc(sizeof(*revmap) * size, GFP_KERNEL);
230 if (WARN_ON(!revmap))
231 return NULL;
232
233 domain = irq_domain_alloc(of_node, IRQ_DOMAIN_MAP_LINEAR, ops, host_data);
234 if (!domain) {
235 kfree(revmap);
236 return NULL;
237 }
238 domain->revmap_data.linear.size = size;
239 domain->revmap_data.linear.revmap = revmap;
240 irq_domain_add(domain);
241 return domain;
242}
Paul Mundtecd84eb2012-05-19 15:11:42 +0900243EXPORT_SYMBOL_GPL(irq_domain_add_linear);
Grant Likelya8db8cf2012-02-14 14:06:54 -0700244
245struct irq_domain *irq_domain_add_nomap(struct device_node *of_node,
Grant Likely6fa6c8e22012-02-15 15:06:08 -0700246 unsigned int max_irq,
Grant Likelya18dc812012-01-26 12:12:14 -0700247 const struct irq_domain_ops *ops,
Grant Likelya8db8cf2012-02-14 14:06:54 -0700248 void *host_data)
249{
250 struct irq_domain *domain = irq_domain_alloc(of_node,
251 IRQ_DOMAIN_MAP_NOMAP, ops, host_data);
Grant Likely6fa6c8e22012-02-15 15:06:08 -0700252 if (domain) {
253 domain->revmap_data.nomap.max_irq = max_irq ? max_irq : ~0;
Grant Likelya8db8cf2012-02-14 14:06:54 -0700254 irq_domain_add(domain);
Grant Likely6fa6c8e22012-02-15 15:06:08 -0700255 }
Grant Likelya8db8cf2012-02-14 14:06:54 -0700256 return domain;
257}
Paul Mundtecd84eb2012-05-19 15:11:42 +0900258EXPORT_SYMBOL_GPL(irq_domain_add_nomap);
Grant Likelya8db8cf2012-02-14 14:06:54 -0700259
260/**
261 * irq_domain_add_tree()
262 * @of_node: pointer to interrupt controller's device tree node.
263 * @ops: map/unmap domain callbacks
264 *
265 * Note: The radix tree will be allocated later during boot automatically
266 * (the reverse mapping will use the slow path until that happens).
267 */
268struct irq_domain *irq_domain_add_tree(struct device_node *of_node,
Grant Likelya18dc812012-01-26 12:12:14 -0700269 const struct irq_domain_ops *ops,
Grant Likelya8db8cf2012-02-14 14:06:54 -0700270 void *host_data)
271{
272 struct irq_domain *domain = irq_domain_alloc(of_node,
273 IRQ_DOMAIN_MAP_TREE, ops, host_data);
274 if (domain) {
275 INIT_RADIX_TREE(&domain->revmap_data.tree, GFP_KERNEL);
276 irq_domain_add(domain);
277 }
Grant Likely68700652012-02-14 14:06:53 -0700278 return domain;
Grant Likelycc79ca62012-02-16 01:37:49 -0700279}
Paul Mundtecd84eb2012-05-19 15:11:42 +0900280EXPORT_SYMBOL_GPL(irq_domain_add_tree);
Grant Likelycc79ca62012-02-16 01:37:49 -0700281
282/**
283 * irq_find_host() - Locates a domain for a given device node
284 * @node: device-tree node of the interrupt controller
285 */
286struct irq_domain *irq_find_host(struct device_node *node)
287{
288 struct irq_domain *h, *found = NULL;
Grant Likelya18dc812012-01-26 12:12:14 -0700289 int rc;
Grant Likelycc79ca62012-02-16 01:37:49 -0700290
291 /* We might want to match the legacy controller last since
292 * it might potentially be set to match all interrupts in
293 * the absence of a device node. This isn't a problem so far
294 * yet though...
295 */
296 mutex_lock(&irq_domain_mutex);
Grant Likelya18dc812012-01-26 12:12:14 -0700297 list_for_each_entry(h, &irq_domain_list, link) {
298 if (h->ops->match)
299 rc = h->ops->match(h, node);
300 else
301 rc = (h->of_node != NULL) && (h->of_node == node);
302
303 if (rc) {
Grant Likelycc79ca62012-02-16 01:37:49 -0700304 found = h;
305 break;
306 }
Grant Likelya18dc812012-01-26 12:12:14 -0700307 }
Grant Likelycc79ca62012-02-16 01:37:49 -0700308 mutex_unlock(&irq_domain_mutex);
309 return found;
310}
311EXPORT_SYMBOL_GPL(irq_find_host);
312
313/**
314 * irq_set_default_host() - Set a "default" irq domain
Grant Likely68700652012-02-14 14:06:53 -0700315 * @domain: default domain pointer
Grant Likelycc79ca62012-02-16 01:37:49 -0700316 *
317 * For convenience, it's possible to set a "default" domain that will be used
318 * whenever NULL is passed to irq_create_mapping(). It makes life easier for
319 * platforms that want to manipulate a few hard coded interrupt numbers that
320 * aren't properly represented in the device-tree.
321 */
Grant Likely68700652012-02-14 14:06:53 -0700322void irq_set_default_host(struct irq_domain *domain)
Grant Likelycc79ca62012-02-16 01:37:49 -0700323{
Grant Likely68700652012-02-14 14:06:53 -0700324 pr_debug("irq: Default domain set to @0x%p\n", domain);
Grant Likelycc79ca62012-02-16 01:37:49 -0700325
Grant Likely68700652012-02-14 14:06:53 -0700326 irq_default_domain = domain;
Grant Likelycc79ca62012-02-16 01:37:49 -0700327}
Paul Mundtecd84eb2012-05-19 15:11:42 +0900328EXPORT_SYMBOL_GPL(irq_set_default_host);
Grant Likelycc79ca62012-02-16 01:37:49 -0700329
Grant Likely68700652012-02-14 14:06:53 -0700330static int irq_setup_virq(struct irq_domain *domain, unsigned int virq,
Grant Likelycc79ca62012-02-16 01:37:49 -0700331 irq_hw_number_t hwirq)
332{
333 struct irq_data *irq_data = irq_get_irq_data(virq);
334
335 irq_data->hwirq = hwirq;
Grant Likely68700652012-02-14 14:06:53 -0700336 irq_data->domain = domain;
337 if (domain->ops->map(domain, virq, hwirq)) {
Grant Likelycc79ca62012-02-16 01:37:49 -0700338 pr_debug("irq: -> mapping failed, freeing\n");
339 irq_data->domain = NULL;
340 irq_data->hwirq = 0;
341 return -1;
342 }
343
344 irq_clear_status_flags(virq, IRQ_NOREQUEST);
345
346 return 0;
347}
348
349/**
350 * irq_create_direct_mapping() - Allocate an irq for direct mapping
Grant Likely68700652012-02-14 14:06:53 -0700351 * @domain: domain to allocate the irq for or NULL for default domain
Grant Likelycc79ca62012-02-16 01:37:49 -0700352 *
353 * This routine is used for irq controllers which can choose the hardware
354 * interrupt numbers they generate. In such a case it's simplest to use
355 * the linux irq as the hardware interrupt number.
356 */
Grant Likely68700652012-02-14 14:06:53 -0700357unsigned int irq_create_direct_mapping(struct irq_domain *domain)
Grant Likelycc79ca62012-02-16 01:37:49 -0700358{
359 unsigned int virq;
360
Grant Likely68700652012-02-14 14:06:53 -0700361 if (domain == NULL)
362 domain = irq_default_domain;
Grant Likelycc79ca62012-02-16 01:37:49 -0700363
Grant Likely68700652012-02-14 14:06:53 -0700364 BUG_ON(domain == NULL);
365 WARN_ON(domain->revmap_type != IRQ_DOMAIN_MAP_NOMAP);
Grant Likelycc79ca62012-02-16 01:37:49 -0700366
367 virq = irq_alloc_desc_from(1, 0);
Grant Likely03848372012-02-14 14:06:52 -0700368 if (!virq) {
Grant Likelycc79ca62012-02-16 01:37:49 -0700369 pr_debug("irq: create_direct virq allocation failed\n");
Grant Likely03848372012-02-14 14:06:52 -0700370 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700371 }
Grant Likely6fa6c8e22012-02-15 15:06:08 -0700372 if (virq >= domain->revmap_data.nomap.max_irq) {
Grant Likelycc79ca62012-02-16 01:37:49 -0700373 pr_err("ERROR: no free irqs available below %i maximum\n",
Grant Likely6fa6c8e22012-02-15 15:06:08 -0700374 domain->revmap_data.nomap.max_irq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700375 irq_free_desc(virq);
376 return 0;
377 }
Grant Likelycc79ca62012-02-16 01:37:49 -0700378 pr_debug("irq: create_direct obtained virq %d\n", virq);
379
Grant Likely68700652012-02-14 14:06:53 -0700380 if (irq_setup_virq(domain, virq, virq)) {
Grant Likelycc79ca62012-02-16 01:37:49 -0700381 irq_free_desc(virq);
Grant Likely03848372012-02-14 14:06:52 -0700382 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700383 }
384
385 return virq;
386}
Paul Mundtecd84eb2012-05-19 15:11:42 +0900387EXPORT_SYMBOL_GPL(irq_create_direct_mapping);
Grant Likelycc79ca62012-02-16 01:37:49 -0700388
389/**
390 * irq_create_mapping() - Map a hardware interrupt into linux irq space
Grant Likely68700652012-02-14 14:06:53 -0700391 * @domain: domain owning this hardware interrupt or NULL for default domain
392 * @hwirq: hardware irq number in that domain space
Grant Likelycc79ca62012-02-16 01:37:49 -0700393 *
394 * Only one mapping per hardware interrupt is permitted. Returns a linux
395 * irq number.
396 * If the sense/trigger is to be specified, set_irq_type() should be called
397 * on the number returned from that call.
398 */
Grant Likely68700652012-02-14 14:06:53 -0700399unsigned int irq_create_mapping(struct irq_domain *domain,
Grant Likelycc79ca62012-02-16 01:37:49 -0700400 irq_hw_number_t hwirq)
401{
David Daney5b7526e2012-04-05 16:52:13 -0700402 unsigned int hint;
403 int virq;
Grant Likelycc79ca62012-02-16 01:37:49 -0700404
Grant Likely68700652012-02-14 14:06:53 -0700405 pr_debug("irq: irq_create_mapping(0x%p, 0x%lx)\n", domain, hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700406
Grant Likely68700652012-02-14 14:06:53 -0700407 /* Look for default domain if nececssary */
408 if (domain == NULL)
409 domain = irq_default_domain;
410 if (domain == NULL) {
Grant Likelycc79ca62012-02-16 01:37:49 -0700411 printk(KERN_WARNING "irq_create_mapping called for"
Grant Likely68700652012-02-14 14:06:53 -0700412 " NULL domain, hwirq=%lx\n", hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700413 WARN_ON(1);
Grant Likely03848372012-02-14 14:06:52 -0700414 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700415 }
Grant Likely68700652012-02-14 14:06:53 -0700416 pr_debug("irq: -> using domain @%p\n", domain);
Grant Likelycc79ca62012-02-16 01:37:49 -0700417
418 /* Check if mapping already exists */
Grant Likely68700652012-02-14 14:06:53 -0700419 virq = irq_find_mapping(domain, hwirq);
Grant Likely03848372012-02-14 14:06:52 -0700420 if (virq) {
Grant Likelycc79ca62012-02-16 01:37:49 -0700421 pr_debug("irq: -> existing mapping on virq %d\n", virq);
422 return virq;
423 }
424
425 /* Get a virtual interrupt number */
Grant Likely1bc04f22012-02-14 14:06:55 -0700426 if (domain->revmap_type == IRQ_DOMAIN_MAP_LEGACY)
427 return irq_domain_legacy_revmap(domain, hwirq);
428
429 /* Allocate a virtual interrupt number */
Grant Likely6fa6c8e22012-02-15 15:06:08 -0700430 hint = hwirq % nr_irqs;
Grant Likely1bc04f22012-02-14 14:06:55 -0700431 if (hint == 0)
432 hint++;
433 virq = irq_alloc_desc_from(hint, 0);
David Daney5b7526e2012-04-05 16:52:13 -0700434 if (virq <= 0)
Grant Likely1bc04f22012-02-14 14:06:55 -0700435 virq = irq_alloc_desc_from(1, 0);
David Daney5b7526e2012-04-05 16:52:13 -0700436 if (virq <= 0) {
Grant Likely1bc04f22012-02-14 14:06:55 -0700437 pr_debug("irq: -> virq allocation failed\n");
438 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700439 }
440
Grant Likely68700652012-02-14 14:06:53 -0700441 if (irq_setup_virq(domain, virq, hwirq)) {
442 if (domain->revmap_type != IRQ_DOMAIN_MAP_LEGACY)
Grant Likelycc79ca62012-02-16 01:37:49 -0700443 irq_free_desc(virq);
Grant Likely03848372012-02-14 14:06:52 -0700444 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700445 }
446
Grant Likely68700652012-02-14 14:06:53 -0700447 pr_debug("irq: irq %lu on domain %s mapped to virtual irq %u\n",
448 hwirq, domain->of_node ? domain->of_node->full_name : "null", virq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700449
450 return virq;
451}
452EXPORT_SYMBOL_GPL(irq_create_mapping);
453
454unsigned int irq_create_of_mapping(struct device_node *controller,
455 const u32 *intspec, unsigned int intsize)
456{
Grant Likely68700652012-02-14 14:06:53 -0700457 struct irq_domain *domain;
Grant Likelycc79ca62012-02-16 01:37:49 -0700458 irq_hw_number_t hwirq;
459 unsigned int type = IRQ_TYPE_NONE;
460 unsigned int virq;
461
Grant Likely68700652012-02-14 14:06:53 -0700462 domain = controller ? irq_find_host(controller) : irq_default_domain;
463 if (!domain) {
Grant Likelyabd23632012-02-24 08:07:06 -0700464#ifdef CONFIG_MIPS
465 /*
466 * Workaround to avoid breaking interrupt controller drivers
467 * that don't yet register an irq_domain. This is temporary
468 * code. ~~~gcl, Feb 24, 2012
469 *
470 * Scheduled for removal in Linux v3.6. That should be enough
471 * time.
472 */
473 if (intsize > 0)
474 return intspec[0];
475#endif
Grant Likely68700652012-02-14 14:06:53 -0700476 printk(KERN_WARNING "irq: no irq domain found for %s !\n",
Grant Likelycc79ca62012-02-16 01:37:49 -0700477 controller->full_name);
Grant Likely03848372012-02-14 14:06:52 -0700478 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700479 }
480
Grant Likely68700652012-02-14 14:06:53 -0700481 /* If domain has no translation, then we assume interrupt line */
482 if (domain->ops->xlate == NULL)
Grant Likelycc79ca62012-02-16 01:37:49 -0700483 hwirq = intspec[0];
484 else {
Grant Likely68700652012-02-14 14:06:53 -0700485 if (domain->ops->xlate(domain, controller, intspec, intsize,
Grant Likelycc79ca62012-02-16 01:37:49 -0700486 &hwirq, &type))
Grant Likely03848372012-02-14 14:06:52 -0700487 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700488 }
489
490 /* Create mapping */
Grant Likely68700652012-02-14 14:06:53 -0700491 virq = irq_create_mapping(domain, hwirq);
Grant Likely03848372012-02-14 14:06:52 -0700492 if (!virq)
Grant Likelycc79ca62012-02-16 01:37:49 -0700493 return virq;
494
495 /* Set type if specified and different than the current one */
496 if (type != IRQ_TYPE_NONE &&
497 type != (irqd_get_trigger_type(irq_get_irq_data(virq))))
498 irq_set_irq_type(virq, type);
499 return virq;
500}
501EXPORT_SYMBOL_GPL(irq_create_of_mapping);
502
503/**
504 * irq_dispose_mapping() - Unmap an interrupt
505 * @virq: linux irq number of the interrupt to unmap
506 */
507void irq_dispose_mapping(unsigned int virq)
508{
509 struct irq_data *irq_data = irq_get_irq_data(virq);
Grant Likely68700652012-02-14 14:06:53 -0700510 struct irq_domain *domain;
Grant Likelycc79ca62012-02-16 01:37:49 -0700511 irq_hw_number_t hwirq;
512
Grant Likely03848372012-02-14 14:06:52 -0700513 if (!virq || !irq_data)
Grant Likelycc79ca62012-02-16 01:37:49 -0700514 return;
515
Grant Likely68700652012-02-14 14:06:53 -0700516 domain = irq_data->domain;
517 if (WARN_ON(domain == NULL))
Grant Likelycc79ca62012-02-16 01:37:49 -0700518 return;
519
520 /* Never unmap legacy interrupts */
Grant Likely68700652012-02-14 14:06:53 -0700521 if (domain->revmap_type == IRQ_DOMAIN_MAP_LEGACY)
Grant Likelycc79ca62012-02-16 01:37:49 -0700522 return;
523
524 irq_set_status_flags(virq, IRQ_NOREQUEST);
525
526 /* remove chip and handler */
527 irq_set_chip_and_handler(virq, NULL, NULL);
528
529 /* Make sure it's completed */
530 synchronize_irq(virq);
531
532 /* Tell the PIC about it */
Grant Likely68700652012-02-14 14:06:53 -0700533 if (domain->ops->unmap)
534 domain->ops->unmap(domain, virq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700535 smp_mb();
536
537 /* Clear reverse map */
538 hwirq = irq_data->hwirq;
Grant Likely68700652012-02-14 14:06:53 -0700539 switch(domain->revmap_type) {
Grant Likelycc79ca62012-02-16 01:37:49 -0700540 case IRQ_DOMAIN_MAP_LINEAR:
Grant Likely68700652012-02-14 14:06:53 -0700541 if (hwirq < domain->revmap_data.linear.size)
542 domain->revmap_data.linear.revmap[hwirq] = 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700543 break;
544 case IRQ_DOMAIN_MAP_TREE:
545 mutex_lock(&revmap_trees_mutex);
Grant Likely68700652012-02-14 14:06:53 -0700546 radix_tree_delete(&domain->revmap_data.tree, hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700547 mutex_unlock(&revmap_trees_mutex);
548 break;
549 }
550
Grant Likelycc79ca62012-02-16 01:37:49 -0700551 irq_free_desc(virq);
552}
553EXPORT_SYMBOL_GPL(irq_dispose_mapping);
554
555/**
556 * irq_find_mapping() - Find a linux irq from an hw irq number.
Grant Likely68700652012-02-14 14:06:53 -0700557 * @domain: domain owning this hardware interrupt
558 * @hwirq: hardware irq number in that domain space
Grant Likelycc79ca62012-02-16 01:37:49 -0700559 *
560 * This is a slow path, for use by generic code. It's expected that an
561 * irq controller implementation directly calls the appropriate low level
562 * mapping function.
563 */
Grant Likely68700652012-02-14 14:06:53 -0700564unsigned int irq_find_mapping(struct irq_domain *domain,
Grant Likelycc79ca62012-02-16 01:37:49 -0700565 irq_hw_number_t hwirq)
566{
567 unsigned int i;
Grant Likely6fa6c8e22012-02-15 15:06:08 -0700568 unsigned int hint = hwirq % nr_irqs;
Grant Likelycc79ca62012-02-16 01:37:49 -0700569
Grant Likely68700652012-02-14 14:06:53 -0700570 /* Look for default domain if nececssary */
571 if (domain == NULL)
572 domain = irq_default_domain;
573 if (domain == NULL)
Grant Likely03848372012-02-14 14:06:52 -0700574 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700575
576 /* legacy -> bail early */
Grant Likely68700652012-02-14 14:06:53 -0700577 if (domain->revmap_type == IRQ_DOMAIN_MAP_LEGACY)
Grant Likely1bc04f22012-02-14 14:06:55 -0700578 return irq_domain_legacy_revmap(domain, hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700579
580 /* Slow path does a linear search of the map */
581 if (hint == 0)
582 hint = 1;
583 i = hint;
584 do {
585 struct irq_data *data = irq_get_irq_data(i);
Grant Likely68700652012-02-14 14:06:53 -0700586 if (data && (data->domain == domain) && (data->hwirq == hwirq))
Grant Likelycc79ca62012-02-16 01:37:49 -0700587 return i;
588 i++;
Grant Likely6fa6c8e22012-02-15 15:06:08 -0700589 if (i >= nr_irqs)
Grant Likelycc79ca62012-02-16 01:37:49 -0700590 i = 1;
591 } while(i != hint);
Grant Likely03848372012-02-14 14:06:52 -0700592 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700593}
594EXPORT_SYMBOL_GPL(irq_find_mapping);
595
596/**
597 * irq_radix_revmap_lookup() - Find a linux irq from a hw irq number.
Grant Likely68700652012-02-14 14:06:53 -0700598 * @domain: domain owning this hardware interrupt
599 * @hwirq: hardware irq number in that domain space
Grant Likelycc79ca62012-02-16 01:37:49 -0700600 *
601 * This is a fast path, for use by irq controller code that uses radix tree
602 * revmaps
603 */
Grant Likely68700652012-02-14 14:06:53 -0700604unsigned int irq_radix_revmap_lookup(struct irq_domain *domain,
Grant Likelycc79ca62012-02-16 01:37:49 -0700605 irq_hw_number_t hwirq)
606{
607 struct irq_data *irq_data;
608
Grant Likely68700652012-02-14 14:06:53 -0700609 if (WARN_ON_ONCE(domain->revmap_type != IRQ_DOMAIN_MAP_TREE))
610 return irq_find_mapping(domain, hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700611
612 /*
613 * Freeing an irq can delete nodes along the path to
614 * do the lookup via call_rcu.
615 */
616 rcu_read_lock();
Grant Likely68700652012-02-14 14:06:53 -0700617 irq_data = radix_tree_lookup(&domain->revmap_data.tree, hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700618 rcu_read_unlock();
619
620 /*
621 * If found in radix tree, then fine.
622 * Else fallback to linear lookup - this should not happen in practice
623 * as it means that we failed to insert the node in the radix tree.
624 */
Grant Likely68700652012-02-14 14:06:53 -0700625 return irq_data ? irq_data->irq : irq_find_mapping(domain, hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700626}
Paul Mundtecd84eb2012-05-19 15:11:42 +0900627EXPORT_SYMBOL_GPL(irq_radix_revmap_lookup);
Grant Likelycc79ca62012-02-16 01:37:49 -0700628
629/**
630 * irq_radix_revmap_insert() - Insert a hw irq to linux irq number mapping.
Grant Likely68700652012-02-14 14:06:53 -0700631 * @domain: domain owning this hardware interrupt
Grant Likelycc79ca62012-02-16 01:37:49 -0700632 * @virq: linux irq number
Grant Likely68700652012-02-14 14:06:53 -0700633 * @hwirq: hardware irq number in that domain space
Grant Likelycc79ca62012-02-16 01:37:49 -0700634 *
635 * This is for use by irq controllers that use a radix tree reverse
636 * mapping for fast lookup.
637 */
Grant Likely68700652012-02-14 14:06:53 -0700638void irq_radix_revmap_insert(struct irq_domain *domain, unsigned int virq,
Grant Likelycc79ca62012-02-16 01:37:49 -0700639 irq_hw_number_t hwirq)
640{
641 struct irq_data *irq_data = irq_get_irq_data(virq);
642
Grant Likely68700652012-02-14 14:06:53 -0700643 if (WARN_ON(domain->revmap_type != IRQ_DOMAIN_MAP_TREE))
Grant Likelycc79ca62012-02-16 01:37:49 -0700644 return;
645
Grant Likely03848372012-02-14 14:06:52 -0700646 if (virq) {
Grant Likelycc79ca62012-02-16 01:37:49 -0700647 mutex_lock(&revmap_trees_mutex);
Grant Likely68700652012-02-14 14:06:53 -0700648 radix_tree_insert(&domain->revmap_data.tree, hwirq, irq_data);
Grant Likelycc79ca62012-02-16 01:37:49 -0700649 mutex_unlock(&revmap_trees_mutex);
650 }
651}
Paul Mundtecd84eb2012-05-19 15:11:42 +0900652EXPORT_SYMBOL_GPL(irq_radix_revmap_insert);
Grant Likelycc79ca62012-02-16 01:37:49 -0700653
654/**
655 * irq_linear_revmap() - Find a linux irq from a hw irq number.
Grant Likely68700652012-02-14 14:06:53 -0700656 * @domain: domain owning this hardware interrupt
657 * @hwirq: hardware irq number in that domain space
Grant Likelycc79ca62012-02-16 01:37:49 -0700658 *
659 * This is a fast path, for use by irq controller code that uses linear
660 * revmaps. It does fallback to the slow path if the revmap doesn't exist
661 * yet and will create the revmap entry with appropriate locking
662 */
Grant Likely68700652012-02-14 14:06:53 -0700663unsigned int irq_linear_revmap(struct irq_domain *domain,
Grant Likelycc79ca62012-02-16 01:37:49 -0700664 irq_hw_number_t hwirq)
665{
666 unsigned int *revmap;
667
Grant Likely68700652012-02-14 14:06:53 -0700668 if (WARN_ON_ONCE(domain->revmap_type != IRQ_DOMAIN_MAP_LINEAR))
669 return irq_find_mapping(domain, hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700670
671 /* Check revmap bounds */
Grant Likely68700652012-02-14 14:06:53 -0700672 if (unlikely(hwirq >= domain->revmap_data.linear.size))
673 return irq_find_mapping(domain, hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700674
675 /* Check if revmap was allocated */
Grant Likely68700652012-02-14 14:06:53 -0700676 revmap = domain->revmap_data.linear.revmap;
Grant Likelycc79ca62012-02-16 01:37:49 -0700677 if (unlikely(revmap == NULL))
Grant Likely68700652012-02-14 14:06:53 -0700678 return irq_find_mapping(domain, hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700679
680 /* Fill up revmap with slow path if no mapping found */
Grant Likely03848372012-02-14 14:06:52 -0700681 if (unlikely(!revmap[hwirq]))
Grant Likely68700652012-02-14 14:06:53 -0700682 revmap[hwirq] = irq_find_mapping(domain, hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700683
684 return revmap[hwirq];
685}
Paul Mundtecd84eb2012-05-19 15:11:42 +0900686EXPORT_SYMBOL_GPL(irq_linear_revmap);
Grant Likelycc79ca62012-02-16 01:37:49 -0700687
Grant Likely092b2fb2012-03-29 14:10:30 -0600688#ifdef CONFIG_IRQ_DOMAIN_DEBUG
Grant Likelycc79ca62012-02-16 01:37:49 -0700689static int virq_debug_show(struct seq_file *m, void *private)
690{
691 unsigned long flags;
692 struct irq_desc *desc;
693 const char *p;
694 static const char none[] = "none";
695 void *data;
696 int i;
697
Grant Likely15e06bf2012-04-11 00:26:25 -0600698 seq_printf(m, "%-5s %-7s %-15s %-*s %s\n", "irq", "hwirq",
Grant Likely5269a9a2012-04-12 14:42:15 -0600699 "chip name", (int)(2 * sizeof(void *) + 2), "chip data",
700 "domain name");
Grant Likelycc79ca62012-02-16 01:37:49 -0700701
702 for (i = 1; i < nr_irqs; i++) {
703 desc = irq_to_desc(i);
704 if (!desc)
705 continue;
706
707 raw_spin_lock_irqsave(&desc->lock, flags);
708
709 if (desc->action && desc->action->handler) {
710 struct irq_chip *chip;
711
712 seq_printf(m, "%5d ", i);
713 seq_printf(m, "0x%05lx ", desc->irq_data.hwirq);
714
715 chip = irq_desc_get_chip(desc);
716 if (chip && chip->name)
717 p = chip->name;
718 else
719 p = none;
720 seq_printf(m, "%-15s ", p);
721
722 data = irq_desc_get_chip_data(desc);
Grant Likely15e06bf2012-04-11 00:26:25 -0600723 seq_printf(m, data ? "0x%p " : " %p ", data);
Grant Likelycc79ca62012-02-16 01:37:49 -0700724
Grant Likely092b2fb2012-03-29 14:10:30 -0600725 if (desc->irq_data.domain && desc->irq_data.domain->of_node)
Grant Likelycc79ca62012-02-16 01:37:49 -0700726 p = desc->irq_data.domain->of_node->full_name;
727 else
728 p = none;
729 seq_printf(m, "%s\n", p);
730 }
731
732 raw_spin_unlock_irqrestore(&desc->lock, flags);
733 }
734
735 return 0;
736}
737
738static int virq_debug_open(struct inode *inode, struct file *file)
739{
740 return single_open(file, virq_debug_show, inode->i_private);
741}
742
743static const struct file_operations virq_debug_fops = {
744 .open = virq_debug_open,
745 .read = seq_read,
746 .llseek = seq_lseek,
747 .release = single_release,
748};
749
750static int __init irq_debugfs_init(void)
751{
Grant Likely092b2fb2012-03-29 14:10:30 -0600752 if (debugfs_create_file("irq_domain_mapping", S_IRUGO, NULL,
Grant Likelycc79ca62012-02-16 01:37:49 -0700753 NULL, &virq_debug_fops) == NULL)
754 return -ENOMEM;
755
756 return 0;
757}
758__initcall(irq_debugfs_init);
Grant Likely092b2fb2012-03-29 14:10:30 -0600759#endif /* CONFIG_IRQ_DOMAIN_DEBUG */
Grant Likelycc79ca62012-02-16 01:37:49 -0700760
Grant Likely75294952012-02-14 14:06:57 -0700761int irq_domain_simple_map(struct irq_domain *d, unsigned int irq,
762 irq_hw_number_t hwirq)
Grant Likely08a543a2011-07-26 03:19:06 -0600763{
Grant Likely75294952012-02-14 14:06:57 -0700764 return 0;
Grant Likely08a543a2011-07-26 03:19:06 -0600765}
766
Grant Likely16b2e6e2012-01-26 11:26:52 -0700767/**
768 * irq_domain_xlate_onecell() - Generic xlate for direct one cell bindings
769 *
770 * Device Tree IRQ specifier translation function which works with one cell
771 * bindings where the cell value maps directly to the hwirq number.
772 */
773int irq_domain_xlate_onecell(struct irq_domain *d, struct device_node *ctrlr,
774 const u32 *intspec, unsigned int intsize,
775 unsigned long *out_hwirq, unsigned int *out_type)
Grant Likely7e713302011-07-26 03:19:06 -0600776{
Grant Likely16b2e6e2012-01-26 11:26:52 -0700777 if (WARN_ON(intsize < 1))
Grant Likely7e713302011-07-26 03:19:06 -0600778 return -EINVAL;
Grant Likely7e713302011-07-26 03:19:06 -0600779 *out_hwirq = intspec[0];
780 *out_type = IRQ_TYPE_NONE;
Grant Likely7e713302011-07-26 03:19:06 -0600781 return 0;
782}
Grant Likely16b2e6e2012-01-26 11:26:52 -0700783EXPORT_SYMBOL_GPL(irq_domain_xlate_onecell);
784
785/**
786 * irq_domain_xlate_twocell() - Generic xlate for direct two cell bindings
787 *
788 * Device Tree IRQ specifier translation function which works with two cell
789 * bindings where the cell values map directly to the hwirq number
790 * and linux irq flags.
791 */
792int irq_domain_xlate_twocell(struct irq_domain *d, struct device_node *ctrlr,
793 const u32 *intspec, unsigned int intsize,
794 irq_hw_number_t *out_hwirq, unsigned int *out_type)
795{
796 if (WARN_ON(intsize < 2))
797 return -EINVAL;
798 *out_hwirq = intspec[0];
799 *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK;
800 return 0;
801}
802EXPORT_SYMBOL_GPL(irq_domain_xlate_twocell);
803
804/**
805 * irq_domain_xlate_onetwocell() - Generic xlate for one or two cell bindings
806 *
807 * Device Tree IRQ specifier translation function which works with either one
808 * or two cell bindings where the cell values map directly to the hwirq number
809 * and linux irq flags.
810 *
811 * Note: don't use this function unless your interrupt controller explicitly
812 * supports both one and two cell bindings. For the majority of controllers
813 * the _onecell() or _twocell() variants above should be used.
814 */
815int irq_domain_xlate_onetwocell(struct irq_domain *d,
816 struct device_node *ctrlr,
817 const u32 *intspec, unsigned int intsize,
818 unsigned long *out_hwirq, unsigned int *out_type)
819{
820 if (WARN_ON(intsize < 1))
821 return -EINVAL;
822 *out_hwirq = intspec[0];
823 *out_type = (intsize > 1) ? intspec[1] : IRQ_TYPE_NONE;
824 return 0;
825}
826EXPORT_SYMBOL_GPL(irq_domain_xlate_onetwocell);
Grant Likely7e713302011-07-26 03:19:06 -0600827
Grant Likelya18dc812012-01-26 12:12:14 -0700828const struct irq_domain_ops irq_domain_simple_ops = {
Grant Likely75294952012-02-14 14:06:57 -0700829 .map = irq_domain_simple_map,
Grant Likely16b2e6e2012-01-26 11:26:52 -0700830 .xlate = irq_domain_xlate_onetwocell,
Grant Likely75294952012-02-14 14:06:57 -0700831};
832EXPORT_SYMBOL_GPL(irq_domain_simple_ops);
833
834#ifdef CONFIG_OF_IRQ
Grant Likely7e713302011-07-26 03:19:06 -0600835void irq_domain_generate_simple(const struct of_device_id *match,
836 u64 phys_base, unsigned int irq_start)
837{
838 struct device_node *node;
Grant Likelye1964c52012-02-14 14:06:48 -0700839 pr_debug("looking for phys_base=%llx, irq_start=%i\n",
Grant Likely7e713302011-07-26 03:19:06 -0600840 (unsigned long long) phys_base, (int) irq_start);
841 node = of_find_matching_node_by_address(NULL, match, phys_base);
842 if (node)
Grant Likely6b783f72012-01-10 17:09:30 -0700843 irq_domain_add_legacy(node, 32, irq_start, 0,
844 &irq_domain_simple_ops, NULL);
Grant Likely7e713302011-07-26 03:19:06 -0600845}
846EXPORT_SYMBOL_GPL(irq_domain_generate_simple);
Grant Likely75294952012-02-14 14:06:57 -0700847#endif