blob: 25a498eb98a3ef21e303f43149de10a8bf97c176 [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);
26static unsigned int irq_virq_count = NR_IRQS;
Grant Likely68700652012-02-14 14:06:53 -070027static struct irq_domain *irq_default_domain;
Grant Likelycc79ca62012-02-16 01:37:49 -070028
Grant Likelycc79ca62012-02-16 01:37:49 -070029/**
Grant Likelya8db8cf2012-02-14 14:06:54 -070030 * irq_domain_alloc() - Allocate a new irq_domain data structure
Grant Likelycc79ca62012-02-16 01:37:49 -070031 * @of_node: optional device-tree node of the interrupt controller
32 * @revmap_type: type of reverse mapping to use
Grant Likely68700652012-02-14 14:06:53 -070033 * @ops: map/unmap domain callbacks
Grant Likelya8db8cf2012-02-14 14:06:54 -070034 * @host_data: Controller private data pointer
Grant Likelycc79ca62012-02-16 01:37:49 -070035 *
Grant Likelya8db8cf2012-02-14 14:06:54 -070036 * Allocates and initialize and irq_domain structure. Caller is expected to
37 * register allocated irq_domain with irq_domain_register(). Returns pointer
38 * to IRQ domain, or NULL on failure.
Grant Likelycc79ca62012-02-16 01:37:49 -070039 */
Grant Likelya8db8cf2012-02-14 14:06:54 -070040static struct irq_domain *irq_domain_alloc(struct device_node *of_node,
41 unsigned int revmap_type,
Grant Likelya18dc812012-01-26 12:12:14 -070042 const struct irq_domain_ops *ops,
Grant Likelya8db8cf2012-02-14 14:06:54 -070043 void *host_data)
Grant Likelycc79ca62012-02-16 01:37:49 -070044{
Grant Likelya8db8cf2012-02-14 14:06:54 -070045 struct irq_domain *domain;
Grant Likelycc79ca62012-02-16 01:37:49 -070046
Grant Likelya8db8cf2012-02-14 14:06:54 -070047 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
48 if (WARN_ON(!domain))
Grant Likelycc79ca62012-02-16 01:37:49 -070049 return NULL;
50
51 /* Fill structure */
Grant Likely68700652012-02-14 14:06:53 -070052 domain->revmap_type = revmap_type;
Grant Likely68700652012-02-14 14:06:53 -070053 domain->ops = ops;
Grant Likelya8db8cf2012-02-14 14:06:54 -070054 domain->host_data = host_data;
Grant Likely68700652012-02-14 14:06:53 -070055 domain->of_node = of_node_get(of_node);
Grant Likelycc79ca62012-02-16 01:37:49 -070056
Grant Likelya8db8cf2012-02-14 14:06:54 -070057 return domain;
58}
59
60static void irq_domain_add(struct irq_domain *domain)
61{
62 mutex_lock(&irq_domain_mutex);
63 list_add(&domain->link, &irq_domain_list);
64 mutex_unlock(&irq_domain_mutex);
65 pr_debug("irq: Allocated domain of type %d @0x%p\n",
66 domain->revmap_type, domain);
67}
68
Grant Likely1bc04f22012-02-14 14:06:55 -070069static unsigned int irq_domain_legacy_revmap(struct irq_domain *domain,
70 irq_hw_number_t hwirq)
71{
72 irq_hw_number_t first_hwirq = domain->revmap_data.legacy.first_hwirq;
73 int size = domain->revmap_data.legacy.size;
74
75 if (WARN_ON(hwirq < first_hwirq || hwirq >= first_hwirq + size))
76 return 0;
77 return hwirq - first_hwirq + domain->revmap_data.legacy.first_irq;
78}
79
Grant Likelya8db8cf2012-02-14 14:06:54 -070080/**
81 * irq_domain_add_legacy() - Allocate and register a legacy revmap irq_domain.
82 * @of_node: pointer to interrupt controller's device tree node.
Grant Likely1bc04f22012-02-14 14:06:55 -070083 * @size: total number of irqs in legacy mapping
84 * @first_irq: first number of irq block assigned to the domain
85 * @first_hwirq: first hwirq number to use for the translation. Should normally
86 * be '0', but a positive integer can be used if the effective
87 * hwirqs numbering does not begin at zero.
Grant Likelya8db8cf2012-02-14 14:06:54 -070088 * @ops: map/unmap domain callbacks
89 * @host_data: Controller private data pointer
90 *
91 * Note: the map() callback will be called before this function returns
92 * for all legacy interrupts except 0 (which is always the invalid irq for
93 * a legacy controller).
94 */
95struct irq_domain *irq_domain_add_legacy(struct device_node *of_node,
Grant Likely1bc04f22012-02-14 14:06:55 -070096 unsigned int size,
97 unsigned int first_irq,
98 irq_hw_number_t first_hwirq,
Grant Likelya18dc812012-01-26 12:12:14 -070099 const struct irq_domain_ops *ops,
Grant Likelya8db8cf2012-02-14 14:06:54 -0700100 void *host_data)
101{
Grant Likely1bc04f22012-02-14 14:06:55 -0700102 struct irq_domain *domain;
Grant Likelya8db8cf2012-02-14 14:06:54 -0700103 unsigned int i;
104
105 domain = irq_domain_alloc(of_node, IRQ_DOMAIN_MAP_LEGACY, ops, host_data);
106 if (!domain)
107 return NULL;
108
Grant Likely1bc04f22012-02-14 14:06:55 -0700109 domain->revmap_data.legacy.first_irq = first_irq;
110 domain->revmap_data.legacy.first_hwirq = first_hwirq;
111 domain->revmap_data.legacy.size = size;
112
Grant Likelycc79ca62012-02-16 01:37:49 -0700113 mutex_lock(&irq_domain_mutex);
Grant Likely1bc04f22012-02-14 14:06:55 -0700114 /* Verify that all the irqs are available */
115 for (i = 0; i < size; i++) {
116 int irq = first_irq + i;
117 struct irq_data *irq_data = irq_get_irq_data(irq);
118
119 if (WARN_ON(!irq_data || irq_data->domain)) {
Grant Likelya8db8cf2012-02-14 14:06:54 -0700120 mutex_unlock(&irq_domain_mutex);
121 of_node_put(domain->of_node);
122 kfree(domain);
123 return NULL;
Grant Likelycc79ca62012-02-16 01:37:49 -0700124 }
125 }
Grant Likely1bc04f22012-02-14 14:06:55 -0700126
127 /* Claim all of the irqs before registering a legacy domain */
128 for (i = 0; i < size; i++) {
129 struct irq_data *irq_data = irq_get_irq_data(first_irq + i);
130 irq_data->hwirq = first_hwirq + i;
131 irq_data->domain = domain;
132 }
Grant Likelycc79ca62012-02-16 01:37:49 -0700133 mutex_unlock(&irq_domain_mutex);
134
Grant Likely1bc04f22012-02-14 14:06:55 -0700135 for (i = 0; i < size; i++) {
136 int irq = first_irq + i;
137 int hwirq = first_hwirq + i;
138
139 /* IRQ0 gets ignored */
140 if (!irq)
141 continue;
Grant Likelycc79ca62012-02-16 01:37:49 -0700142
Grant Likelya8db8cf2012-02-14 14:06:54 -0700143 /* Legacy flags are left to default at this point,
144 * one can then use irq_create_mapping() to
145 * explicitly change them
146 */
Grant Likely1bc04f22012-02-14 14:06:55 -0700147 ops->map(domain, irq, hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700148
Grant Likelya8db8cf2012-02-14 14:06:54 -0700149 /* Clear norequest flags */
Grant Likely1bc04f22012-02-14 14:06:55 -0700150 irq_clear_status_flags(irq, IRQ_NOREQUEST);
Grant Likelycc79ca62012-02-16 01:37:49 -0700151 }
Grant Likely1bc04f22012-02-14 14:06:55 -0700152
153 irq_domain_add(domain);
Grant Likelya8db8cf2012-02-14 14:06:54 -0700154 return domain;
155}
Grant Likelycc79ca62012-02-16 01:37:49 -0700156
Grant Likelya8db8cf2012-02-14 14:06:54 -0700157/**
158 * irq_domain_add_linear() - Allocate and register a legacy revmap irq_domain.
159 * @of_node: pointer to interrupt controller's device tree node.
160 * @ops: map/unmap domain callbacks
161 * @host_data: Controller private data pointer
162 */
163struct irq_domain *irq_domain_add_linear(struct device_node *of_node,
164 unsigned int size,
Grant Likelya18dc812012-01-26 12:12:14 -0700165 const struct irq_domain_ops *ops,
Grant Likelya8db8cf2012-02-14 14:06:54 -0700166 void *host_data)
167{
168 struct irq_domain *domain;
169 unsigned int *revmap;
Grant Likelycc79ca62012-02-16 01:37:49 -0700170
Grant Likelya8db8cf2012-02-14 14:06:54 -0700171 revmap = kzalloc(sizeof(*revmap) * size, GFP_KERNEL);
172 if (WARN_ON(!revmap))
173 return NULL;
174
175 domain = irq_domain_alloc(of_node, IRQ_DOMAIN_MAP_LINEAR, ops, host_data);
176 if (!domain) {
177 kfree(revmap);
178 return NULL;
179 }
180 domain->revmap_data.linear.size = size;
181 domain->revmap_data.linear.revmap = revmap;
182 irq_domain_add(domain);
183 return domain;
184}
185
186struct irq_domain *irq_domain_add_nomap(struct device_node *of_node,
Grant Likelya18dc812012-01-26 12:12:14 -0700187 const struct irq_domain_ops *ops,
Grant Likelya8db8cf2012-02-14 14:06:54 -0700188 void *host_data)
189{
190 struct irq_domain *domain = irq_domain_alloc(of_node,
191 IRQ_DOMAIN_MAP_NOMAP, ops, host_data);
192 if (domain)
193 irq_domain_add(domain);
194 return domain;
195}
196
197/**
198 * irq_domain_add_tree()
199 * @of_node: pointer to interrupt controller's device tree node.
200 * @ops: map/unmap domain callbacks
201 *
202 * Note: The radix tree will be allocated later during boot automatically
203 * (the reverse mapping will use the slow path until that happens).
204 */
205struct irq_domain *irq_domain_add_tree(struct device_node *of_node,
Grant Likelya18dc812012-01-26 12:12:14 -0700206 const struct irq_domain_ops *ops,
Grant Likelya8db8cf2012-02-14 14:06:54 -0700207 void *host_data)
208{
209 struct irq_domain *domain = irq_domain_alloc(of_node,
210 IRQ_DOMAIN_MAP_TREE, ops, host_data);
211 if (domain) {
212 INIT_RADIX_TREE(&domain->revmap_data.tree, GFP_KERNEL);
213 irq_domain_add(domain);
214 }
Grant Likely68700652012-02-14 14:06:53 -0700215 return domain;
Grant Likelycc79ca62012-02-16 01:37:49 -0700216}
217
218/**
219 * irq_find_host() - Locates a domain for a given device node
220 * @node: device-tree node of the interrupt controller
221 */
222struct irq_domain *irq_find_host(struct device_node *node)
223{
224 struct irq_domain *h, *found = NULL;
Grant Likelya18dc812012-01-26 12:12:14 -0700225 int rc;
Grant Likelycc79ca62012-02-16 01:37:49 -0700226
227 /* We might want to match the legacy controller last since
228 * it might potentially be set to match all interrupts in
229 * the absence of a device node. This isn't a problem so far
230 * yet though...
231 */
232 mutex_lock(&irq_domain_mutex);
Grant Likelya18dc812012-01-26 12:12:14 -0700233 list_for_each_entry(h, &irq_domain_list, link) {
234 if (h->ops->match)
235 rc = h->ops->match(h, node);
236 else
237 rc = (h->of_node != NULL) && (h->of_node == node);
238
239 if (rc) {
Grant Likelycc79ca62012-02-16 01:37:49 -0700240 found = h;
241 break;
242 }
Grant Likelya18dc812012-01-26 12:12:14 -0700243 }
Grant Likelycc79ca62012-02-16 01:37:49 -0700244 mutex_unlock(&irq_domain_mutex);
245 return found;
246}
247EXPORT_SYMBOL_GPL(irq_find_host);
248
249/**
250 * irq_set_default_host() - Set a "default" irq domain
Grant Likely68700652012-02-14 14:06:53 -0700251 * @domain: default domain pointer
Grant Likelycc79ca62012-02-16 01:37:49 -0700252 *
253 * For convenience, it's possible to set a "default" domain that will be used
254 * whenever NULL is passed to irq_create_mapping(). It makes life easier for
255 * platforms that want to manipulate a few hard coded interrupt numbers that
256 * aren't properly represented in the device-tree.
257 */
Grant Likely68700652012-02-14 14:06:53 -0700258void irq_set_default_host(struct irq_domain *domain)
Grant Likelycc79ca62012-02-16 01:37:49 -0700259{
Grant Likely68700652012-02-14 14:06:53 -0700260 pr_debug("irq: Default domain set to @0x%p\n", domain);
Grant Likelycc79ca62012-02-16 01:37:49 -0700261
Grant Likely68700652012-02-14 14:06:53 -0700262 irq_default_domain = domain;
Grant Likelycc79ca62012-02-16 01:37:49 -0700263}
264
265/**
266 * irq_set_virq_count() - Set the maximum number of linux irqs
267 * @count: number of linux irqs, capped with NR_IRQS
268 *
269 * This is mainly for use by platforms like iSeries who want to program
270 * the virtual irq number in the controller to avoid the reverse mapping
271 */
272void irq_set_virq_count(unsigned int count)
273{
274 pr_debug("irq: Trying to set virq count to %d\n", count);
275
276 BUG_ON(count < NUM_ISA_INTERRUPTS);
277 if (count < NR_IRQS)
278 irq_virq_count = count;
279}
280
Grant Likely68700652012-02-14 14:06:53 -0700281static int irq_setup_virq(struct irq_domain *domain, unsigned int virq,
Grant Likelycc79ca62012-02-16 01:37:49 -0700282 irq_hw_number_t hwirq)
283{
284 struct irq_data *irq_data = irq_get_irq_data(virq);
285
286 irq_data->hwirq = hwirq;
Grant Likely68700652012-02-14 14:06:53 -0700287 irq_data->domain = domain;
288 if (domain->ops->map(domain, virq, hwirq)) {
Grant Likelycc79ca62012-02-16 01:37:49 -0700289 pr_debug("irq: -> mapping failed, freeing\n");
290 irq_data->domain = NULL;
291 irq_data->hwirq = 0;
292 return -1;
293 }
294
295 irq_clear_status_flags(virq, IRQ_NOREQUEST);
296
297 return 0;
298}
299
300/**
301 * irq_create_direct_mapping() - Allocate an irq for direct mapping
Grant Likely68700652012-02-14 14:06:53 -0700302 * @domain: domain to allocate the irq for or NULL for default domain
Grant Likelycc79ca62012-02-16 01:37:49 -0700303 *
304 * This routine is used for irq controllers which can choose the hardware
305 * interrupt numbers they generate. In such a case it's simplest to use
306 * the linux irq as the hardware interrupt number.
307 */
Grant Likely68700652012-02-14 14:06:53 -0700308unsigned int irq_create_direct_mapping(struct irq_domain *domain)
Grant Likelycc79ca62012-02-16 01:37:49 -0700309{
310 unsigned int virq;
311
Grant Likely68700652012-02-14 14:06:53 -0700312 if (domain == NULL)
313 domain = irq_default_domain;
Grant Likelycc79ca62012-02-16 01:37:49 -0700314
Grant Likely68700652012-02-14 14:06:53 -0700315 BUG_ON(domain == NULL);
316 WARN_ON(domain->revmap_type != IRQ_DOMAIN_MAP_NOMAP);
Grant Likelycc79ca62012-02-16 01:37:49 -0700317
318 virq = irq_alloc_desc_from(1, 0);
Grant Likely03848372012-02-14 14:06:52 -0700319 if (!virq) {
Grant Likelycc79ca62012-02-16 01:37:49 -0700320 pr_debug("irq: create_direct virq allocation failed\n");
Grant Likely03848372012-02-14 14:06:52 -0700321 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700322 }
323 if (virq >= irq_virq_count) {
324 pr_err("ERROR: no free irqs available below %i maximum\n",
325 irq_virq_count);
326 irq_free_desc(virq);
327 return 0;
328 }
329
330 pr_debug("irq: create_direct obtained virq %d\n", virq);
331
Grant Likely68700652012-02-14 14:06:53 -0700332 if (irq_setup_virq(domain, virq, virq)) {
Grant Likelycc79ca62012-02-16 01:37:49 -0700333 irq_free_desc(virq);
Grant Likely03848372012-02-14 14:06:52 -0700334 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700335 }
336
337 return virq;
338}
339
340/**
341 * irq_create_mapping() - Map a hardware interrupt into linux irq space
Grant Likely68700652012-02-14 14:06:53 -0700342 * @domain: domain owning this hardware interrupt or NULL for default domain
343 * @hwirq: hardware irq number in that domain space
Grant Likelycc79ca62012-02-16 01:37:49 -0700344 *
345 * Only one mapping per hardware interrupt is permitted. Returns a linux
346 * irq number.
347 * If the sense/trigger is to be specified, set_irq_type() should be called
348 * on the number returned from that call.
349 */
Grant Likely68700652012-02-14 14:06:53 -0700350unsigned int irq_create_mapping(struct irq_domain *domain,
Grant Likelycc79ca62012-02-16 01:37:49 -0700351 irq_hw_number_t hwirq)
352{
353 unsigned int virq, hint;
354
Grant Likely68700652012-02-14 14:06:53 -0700355 pr_debug("irq: irq_create_mapping(0x%p, 0x%lx)\n", domain, hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700356
Grant Likely68700652012-02-14 14:06:53 -0700357 /* Look for default domain if nececssary */
358 if (domain == NULL)
359 domain = irq_default_domain;
360 if (domain == NULL) {
Grant Likelycc79ca62012-02-16 01:37:49 -0700361 printk(KERN_WARNING "irq_create_mapping called for"
Grant Likely68700652012-02-14 14:06:53 -0700362 " NULL domain, hwirq=%lx\n", hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700363 WARN_ON(1);
Grant Likely03848372012-02-14 14:06:52 -0700364 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700365 }
Grant Likely68700652012-02-14 14:06:53 -0700366 pr_debug("irq: -> using domain @%p\n", domain);
Grant Likelycc79ca62012-02-16 01:37:49 -0700367
368 /* Check if mapping already exists */
Grant Likely68700652012-02-14 14:06:53 -0700369 virq = irq_find_mapping(domain, hwirq);
Grant Likely03848372012-02-14 14:06:52 -0700370 if (virq) {
Grant Likelycc79ca62012-02-16 01:37:49 -0700371 pr_debug("irq: -> existing mapping on virq %d\n", virq);
372 return virq;
373 }
374
375 /* Get a virtual interrupt number */
Grant Likely1bc04f22012-02-14 14:06:55 -0700376 if (domain->revmap_type == IRQ_DOMAIN_MAP_LEGACY)
377 return irq_domain_legacy_revmap(domain, hwirq);
378
379 /* Allocate a virtual interrupt number */
380 hint = hwirq % irq_virq_count;
381 if (hint == 0)
382 hint++;
383 virq = irq_alloc_desc_from(hint, 0);
384 if (!virq)
385 virq = irq_alloc_desc_from(1, 0);
386 if (!virq) {
387 pr_debug("irq: -> virq allocation failed\n");
388 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700389 }
390
Grant Likely68700652012-02-14 14:06:53 -0700391 if (irq_setup_virq(domain, virq, hwirq)) {
392 if (domain->revmap_type != IRQ_DOMAIN_MAP_LEGACY)
Grant Likelycc79ca62012-02-16 01:37:49 -0700393 irq_free_desc(virq);
Grant Likely03848372012-02-14 14:06:52 -0700394 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700395 }
396
Grant Likely68700652012-02-14 14:06:53 -0700397 pr_debug("irq: irq %lu on domain %s mapped to virtual irq %u\n",
398 hwirq, domain->of_node ? domain->of_node->full_name : "null", virq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700399
400 return virq;
401}
402EXPORT_SYMBOL_GPL(irq_create_mapping);
403
404unsigned int irq_create_of_mapping(struct device_node *controller,
405 const u32 *intspec, unsigned int intsize)
406{
Grant Likely68700652012-02-14 14:06:53 -0700407 struct irq_domain *domain;
Grant Likelycc79ca62012-02-16 01:37:49 -0700408 irq_hw_number_t hwirq;
409 unsigned int type = IRQ_TYPE_NONE;
410 unsigned int virq;
411
Grant Likely68700652012-02-14 14:06:53 -0700412 domain = controller ? irq_find_host(controller) : irq_default_domain;
413 if (!domain) {
414 printk(KERN_WARNING "irq: no irq domain found for %s !\n",
Grant Likelycc79ca62012-02-16 01:37:49 -0700415 controller->full_name);
Grant Likely03848372012-02-14 14:06:52 -0700416 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700417 }
418
Grant Likely68700652012-02-14 14:06:53 -0700419 /* If domain has no translation, then we assume interrupt line */
420 if (domain->ops->xlate == NULL)
Grant Likelycc79ca62012-02-16 01:37:49 -0700421 hwirq = intspec[0];
422 else {
Grant Likely68700652012-02-14 14:06:53 -0700423 if (domain->ops->xlate(domain, controller, intspec, intsize,
Grant Likelycc79ca62012-02-16 01:37:49 -0700424 &hwirq, &type))
Grant Likely03848372012-02-14 14:06:52 -0700425 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700426 }
427
428 /* Create mapping */
Grant Likely68700652012-02-14 14:06:53 -0700429 virq = irq_create_mapping(domain, hwirq);
Grant Likely03848372012-02-14 14:06:52 -0700430 if (!virq)
Grant Likelycc79ca62012-02-16 01:37:49 -0700431 return virq;
432
433 /* Set type if specified and different than the current one */
434 if (type != IRQ_TYPE_NONE &&
435 type != (irqd_get_trigger_type(irq_get_irq_data(virq))))
436 irq_set_irq_type(virq, type);
437 return virq;
438}
439EXPORT_SYMBOL_GPL(irq_create_of_mapping);
440
441/**
442 * irq_dispose_mapping() - Unmap an interrupt
443 * @virq: linux irq number of the interrupt to unmap
444 */
445void irq_dispose_mapping(unsigned int virq)
446{
447 struct irq_data *irq_data = irq_get_irq_data(virq);
Grant Likely68700652012-02-14 14:06:53 -0700448 struct irq_domain *domain;
Grant Likelycc79ca62012-02-16 01:37:49 -0700449 irq_hw_number_t hwirq;
450
Grant Likely03848372012-02-14 14:06:52 -0700451 if (!virq || !irq_data)
Grant Likelycc79ca62012-02-16 01:37:49 -0700452 return;
453
Grant Likely68700652012-02-14 14:06:53 -0700454 domain = irq_data->domain;
455 if (WARN_ON(domain == NULL))
Grant Likelycc79ca62012-02-16 01:37:49 -0700456 return;
457
458 /* Never unmap legacy interrupts */
Grant Likely68700652012-02-14 14:06:53 -0700459 if (domain->revmap_type == IRQ_DOMAIN_MAP_LEGACY)
Grant Likelycc79ca62012-02-16 01:37:49 -0700460 return;
461
462 irq_set_status_flags(virq, IRQ_NOREQUEST);
463
464 /* remove chip and handler */
465 irq_set_chip_and_handler(virq, NULL, NULL);
466
467 /* Make sure it's completed */
468 synchronize_irq(virq);
469
470 /* Tell the PIC about it */
Grant Likely68700652012-02-14 14:06:53 -0700471 if (domain->ops->unmap)
472 domain->ops->unmap(domain, virq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700473 smp_mb();
474
475 /* Clear reverse map */
476 hwirq = irq_data->hwirq;
Grant Likely68700652012-02-14 14:06:53 -0700477 switch(domain->revmap_type) {
Grant Likelycc79ca62012-02-16 01:37:49 -0700478 case IRQ_DOMAIN_MAP_LINEAR:
Grant Likely68700652012-02-14 14:06:53 -0700479 if (hwirq < domain->revmap_data.linear.size)
480 domain->revmap_data.linear.revmap[hwirq] = 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700481 break;
482 case IRQ_DOMAIN_MAP_TREE:
483 mutex_lock(&revmap_trees_mutex);
Grant Likely68700652012-02-14 14:06:53 -0700484 radix_tree_delete(&domain->revmap_data.tree, hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700485 mutex_unlock(&revmap_trees_mutex);
486 break;
487 }
488
Grant Likelycc79ca62012-02-16 01:37:49 -0700489 irq_free_desc(virq);
490}
491EXPORT_SYMBOL_GPL(irq_dispose_mapping);
492
493/**
494 * irq_find_mapping() - Find a linux irq from an hw irq number.
Grant Likely68700652012-02-14 14:06:53 -0700495 * @domain: domain owning this hardware interrupt
496 * @hwirq: hardware irq number in that domain space
Grant Likelycc79ca62012-02-16 01:37:49 -0700497 *
498 * This is a slow path, for use by generic code. It's expected that an
499 * irq controller implementation directly calls the appropriate low level
500 * mapping function.
501 */
Grant Likely68700652012-02-14 14:06:53 -0700502unsigned int irq_find_mapping(struct irq_domain *domain,
Grant Likelycc79ca62012-02-16 01:37:49 -0700503 irq_hw_number_t hwirq)
504{
505 unsigned int i;
506 unsigned int hint = hwirq % irq_virq_count;
507
Grant Likely68700652012-02-14 14:06:53 -0700508 /* Look for default domain if nececssary */
509 if (domain == NULL)
510 domain = irq_default_domain;
511 if (domain == NULL)
Grant Likely03848372012-02-14 14:06:52 -0700512 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700513
514 /* legacy -> bail early */
Grant Likely68700652012-02-14 14:06:53 -0700515 if (domain->revmap_type == IRQ_DOMAIN_MAP_LEGACY)
Grant Likely1bc04f22012-02-14 14:06:55 -0700516 return irq_domain_legacy_revmap(domain, hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700517
518 /* Slow path does a linear search of the map */
519 if (hint == 0)
520 hint = 1;
521 i = hint;
522 do {
523 struct irq_data *data = irq_get_irq_data(i);
Grant Likely68700652012-02-14 14:06:53 -0700524 if (data && (data->domain == domain) && (data->hwirq == hwirq))
Grant Likelycc79ca62012-02-16 01:37:49 -0700525 return i;
526 i++;
527 if (i >= irq_virq_count)
528 i = 1;
529 } while(i != hint);
Grant Likely03848372012-02-14 14:06:52 -0700530 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700531}
532EXPORT_SYMBOL_GPL(irq_find_mapping);
533
534/**
535 * irq_radix_revmap_lookup() - Find a linux irq from a hw irq number.
Grant Likely68700652012-02-14 14:06:53 -0700536 * @domain: domain owning this hardware interrupt
537 * @hwirq: hardware irq number in that domain space
Grant Likelycc79ca62012-02-16 01:37:49 -0700538 *
539 * This is a fast path, for use by irq controller code that uses radix tree
540 * revmaps
541 */
Grant Likely68700652012-02-14 14:06:53 -0700542unsigned int irq_radix_revmap_lookup(struct irq_domain *domain,
Grant Likelycc79ca62012-02-16 01:37:49 -0700543 irq_hw_number_t hwirq)
544{
545 struct irq_data *irq_data;
546
Grant Likely68700652012-02-14 14:06:53 -0700547 if (WARN_ON_ONCE(domain->revmap_type != IRQ_DOMAIN_MAP_TREE))
548 return irq_find_mapping(domain, hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700549
550 /*
551 * Freeing an irq can delete nodes along the path to
552 * do the lookup via call_rcu.
553 */
554 rcu_read_lock();
Grant Likely68700652012-02-14 14:06:53 -0700555 irq_data = radix_tree_lookup(&domain->revmap_data.tree, hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700556 rcu_read_unlock();
557
558 /*
559 * If found in radix tree, then fine.
560 * Else fallback to linear lookup - this should not happen in practice
561 * as it means that we failed to insert the node in the radix tree.
562 */
Grant Likely68700652012-02-14 14:06:53 -0700563 return irq_data ? irq_data->irq : irq_find_mapping(domain, hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700564}
565
566/**
567 * irq_radix_revmap_insert() - Insert a hw irq to linux irq number mapping.
Grant Likely68700652012-02-14 14:06:53 -0700568 * @domain: domain owning this hardware interrupt
Grant Likelycc79ca62012-02-16 01:37:49 -0700569 * @virq: linux irq number
Grant Likely68700652012-02-14 14:06:53 -0700570 * @hwirq: hardware irq number in that domain space
Grant Likelycc79ca62012-02-16 01:37:49 -0700571 *
572 * This is for use by irq controllers that use a radix tree reverse
573 * mapping for fast lookup.
574 */
Grant Likely68700652012-02-14 14:06:53 -0700575void irq_radix_revmap_insert(struct irq_domain *domain, unsigned int virq,
Grant Likelycc79ca62012-02-16 01:37:49 -0700576 irq_hw_number_t hwirq)
577{
578 struct irq_data *irq_data = irq_get_irq_data(virq);
579
Grant Likely68700652012-02-14 14:06:53 -0700580 if (WARN_ON(domain->revmap_type != IRQ_DOMAIN_MAP_TREE))
Grant Likelycc79ca62012-02-16 01:37:49 -0700581 return;
582
Grant Likely03848372012-02-14 14:06:52 -0700583 if (virq) {
Grant Likelycc79ca62012-02-16 01:37:49 -0700584 mutex_lock(&revmap_trees_mutex);
Grant Likely68700652012-02-14 14:06:53 -0700585 radix_tree_insert(&domain->revmap_data.tree, hwirq, irq_data);
Grant Likelycc79ca62012-02-16 01:37:49 -0700586 mutex_unlock(&revmap_trees_mutex);
587 }
588}
589
590/**
591 * irq_linear_revmap() - Find a linux irq from a hw irq number.
Grant Likely68700652012-02-14 14:06:53 -0700592 * @domain: domain owning this hardware interrupt
593 * @hwirq: hardware irq number in that domain space
Grant Likelycc79ca62012-02-16 01:37:49 -0700594 *
595 * This is a fast path, for use by irq controller code that uses linear
596 * revmaps. It does fallback to the slow path if the revmap doesn't exist
597 * yet and will create the revmap entry with appropriate locking
598 */
Grant Likely68700652012-02-14 14:06:53 -0700599unsigned int irq_linear_revmap(struct irq_domain *domain,
Grant Likelycc79ca62012-02-16 01:37:49 -0700600 irq_hw_number_t hwirq)
601{
602 unsigned int *revmap;
603
Grant Likely68700652012-02-14 14:06:53 -0700604 if (WARN_ON_ONCE(domain->revmap_type != IRQ_DOMAIN_MAP_LINEAR))
605 return irq_find_mapping(domain, hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700606
607 /* Check revmap bounds */
Grant Likely68700652012-02-14 14:06:53 -0700608 if (unlikely(hwirq >= domain->revmap_data.linear.size))
609 return irq_find_mapping(domain, hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700610
611 /* Check if revmap was allocated */
Grant Likely68700652012-02-14 14:06:53 -0700612 revmap = domain->revmap_data.linear.revmap;
Grant Likelycc79ca62012-02-16 01:37:49 -0700613 if (unlikely(revmap == NULL))
Grant Likely68700652012-02-14 14:06:53 -0700614 return irq_find_mapping(domain, hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700615
616 /* Fill up revmap with slow path if no mapping found */
Grant Likely03848372012-02-14 14:06:52 -0700617 if (unlikely(!revmap[hwirq]))
Grant Likely68700652012-02-14 14:06:53 -0700618 revmap[hwirq] = irq_find_mapping(domain, hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700619
620 return revmap[hwirq];
621}
622
623#ifdef CONFIG_VIRQ_DEBUG
624static int virq_debug_show(struct seq_file *m, void *private)
625{
626 unsigned long flags;
627 struct irq_desc *desc;
628 const char *p;
629 static const char none[] = "none";
630 void *data;
631 int i;
632
633 seq_printf(m, "%-5s %-7s %-15s %-18s %s\n", "virq", "hwirq",
Grant Likely68700652012-02-14 14:06:53 -0700634 "chip name", "chip data", "domain name");
Grant Likelycc79ca62012-02-16 01:37:49 -0700635
636 for (i = 1; i < nr_irqs; i++) {
637 desc = irq_to_desc(i);
638 if (!desc)
639 continue;
640
641 raw_spin_lock_irqsave(&desc->lock, flags);
642
643 if (desc->action && desc->action->handler) {
644 struct irq_chip *chip;
645
646 seq_printf(m, "%5d ", i);
647 seq_printf(m, "0x%05lx ", desc->irq_data.hwirq);
648
649 chip = irq_desc_get_chip(desc);
650 if (chip && chip->name)
651 p = chip->name;
652 else
653 p = none;
654 seq_printf(m, "%-15s ", p);
655
656 data = irq_desc_get_chip_data(desc);
657 seq_printf(m, "0x%16p ", data);
658
659 if (desc->irq_data.domain->of_node)
660 p = desc->irq_data.domain->of_node->full_name;
661 else
662 p = none;
663 seq_printf(m, "%s\n", p);
664 }
665
666 raw_spin_unlock_irqrestore(&desc->lock, flags);
667 }
668
669 return 0;
670}
671
672static int virq_debug_open(struct inode *inode, struct file *file)
673{
674 return single_open(file, virq_debug_show, inode->i_private);
675}
676
677static const struct file_operations virq_debug_fops = {
678 .open = virq_debug_open,
679 .read = seq_read,
680 .llseek = seq_lseek,
681 .release = single_release,
682};
683
684static int __init irq_debugfs_init(void)
685{
686 if (debugfs_create_file("virq_mapping", S_IRUGO, powerpc_debugfs_root,
687 NULL, &virq_debug_fops) == NULL)
688 return -ENOMEM;
689
690 return 0;
691}
692__initcall(irq_debugfs_init);
693#endif /* CONFIG_VIRQ_DEBUG */
694
Grant Likely75294952012-02-14 14:06:57 -0700695int irq_domain_simple_map(struct irq_domain *d, unsigned int irq,
696 irq_hw_number_t hwirq)
Grant Likely08a543a2011-07-26 03:19:06 -0600697{
Grant Likely75294952012-02-14 14:06:57 -0700698 return 0;
Grant Likely08a543a2011-07-26 03:19:06 -0600699}
700
Grant Likely16b2e6e2012-01-26 11:26:52 -0700701/**
702 * irq_domain_xlate_onecell() - Generic xlate for direct one cell bindings
703 *
704 * Device Tree IRQ specifier translation function which works with one cell
705 * bindings where the cell value maps directly to the hwirq number.
706 */
707int irq_domain_xlate_onecell(struct irq_domain *d, struct device_node *ctrlr,
708 const u32 *intspec, unsigned int intsize,
709 unsigned long *out_hwirq, unsigned int *out_type)
Grant Likely7e713302011-07-26 03:19:06 -0600710{
Grant Likely16b2e6e2012-01-26 11:26:52 -0700711 if (WARN_ON(intsize < 1))
Grant Likely7e713302011-07-26 03:19:06 -0600712 return -EINVAL;
Grant Likely7e713302011-07-26 03:19:06 -0600713 *out_hwirq = intspec[0];
714 *out_type = IRQ_TYPE_NONE;
Grant Likely7e713302011-07-26 03:19:06 -0600715 return 0;
716}
Grant Likely16b2e6e2012-01-26 11:26:52 -0700717EXPORT_SYMBOL_GPL(irq_domain_xlate_onecell);
718
719/**
720 * irq_domain_xlate_twocell() - Generic xlate for direct two cell bindings
721 *
722 * Device Tree IRQ specifier translation function which works with two cell
723 * bindings where the cell values map directly to the hwirq number
724 * and linux irq flags.
725 */
726int irq_domain_xlate_twocell(struct irq_domain *d, struct device_node *ctrlr,
727 const u32 *intspec, unsigned int intsize,
728 irq_hw_number_t *out_hwirq, unsigned int *out_type)
729{
730 if (WARN_ON(intsize < 2))
731 return -EINVAL;
732 *out_hwirq = intspec[0];
733 *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK;
734 return 0;
735}
736EXPORT_SYMBOL_GPL(irq_domain_xlate_twocell);
737
738/**
739 * irq_domain_xlate_onetwocell() - Generic xlate for one or two cell bindings
740 *
741 * Device Tree IRQ specifier translation function which works with either one
742 * or two cell bindings where the cell values map directly to the hwirq number
743 * and linux irq flags.
744 *
745 * Note: don't use this function unless your interrupt controller explicitly
746 * supports both one and two cell bindings. For the majority of controllers
747 * the _onecell() or _twocell() variants above should be used.
748 */
749int irq_domain_xlate_onetwocell(struct irq_domain *d,
750 struct device_node *ctrlr,
751 const u32 *intspec, unsigned int intsize,
752 unsigned long *out_hwirq, unsigned int *out_type)
753{
754 if (WARN_ON(intsize < 1))
755 return -EINVAL;
756 *out_hwirq = intspec[0];
757 *out_type = (intsize > 1) ? intspec[1] : IRQ_TYPE_NONE;
758 return 0;
759}
760EXPORT_SYMBOL_GPL(irq_domain_xlate_onetwocell);
Grant Likely7e713302011-07-26 03:19:06 -0600761
Grant Likelya18dc812012-01-26 12:12:14 -0700762const struct irq_domain_ops irq_domain_simple_ops = {
Grant Likely75294952012-02-14 14:06:57 -0700763 .map = irq_domain_simple_map,
Grant Likely16b2e6e2012-01-26 11:26:52 -0700764 .xlate = irq_domain_xlate_onetwocell,
Grant Likely75294952012-02-14 14:06:57 -0700765};
766EXPORT_SYMBOL_GPL(irq_domain_simple_ops);
767
768#ifdef CONFIG_OF_IRQ
Grant Likely7e713302011-07-26 03:19:06 -0600769void irq_domain_generate_simple(const struct of_device_id *match,
770 u64 phys_base, unsigned int irq_start)
771{
772 struct device_node *node;
Grant Likelye1964c52012-02-14 14:06:48 -0700773 pr_debug("looking for phys_base=%llx, irq_start=%i\n",
Grant Likely7e713302011-07-26 03:19:06 -0600774 (unsigned long long) phys_base, (int) irq_start);
775 node = of_find_matching_node_by_address(NULL, match, phys_base);
776 if (node)
Grant Likely6b783f72012-01-10 17:09:30 -0700777 irq_domain_add_legacy(node, 32, irq_start, 0,
778 &irq_domain_simple_ops, NULL);
Grant Likely7e713302011-07-26 03:19:06 -0600779}
780EXPORT_SYMBOL_GPL(irq_domain_generate_simple);
Grant Likely75294952012-02-14 14:06:57 -0700781#endif