Merge branch 'sched/core'
[linux-2.6/x86.git] / drivers / sh / intc / virq.c
blob1e6e2d0353ea52987e6ef25a5baf42173c0cea03
1 /*
2 * Support for virtual IRQ subgroups.
4 * Copyright (C) 2010 Paul Mundt
6 * This file is subject to the terms and conditions of the GNU General Public
7 * License. See the file "COPYING" in the main directory of this archive
8 * for more details.
9 */
10 #define pr_fmt(fmt) "intc: " fmt
12 #include <linux/slab.h>
13 #include <linux/irq.h>
14 #include <linux/list.h>
15 #include <linux/radix-tree.h>
16 #include <linux/spinlock.h>
17 #include "internals.h"
19 static struct intc_map_entry intc_irq_xlate[NR_IRQS];
21 struct intc_virq_list {
22 unsigned int irq;
23 struct intc_virq_list *next;
26 #define for_each_virq(entry, head) \
27 for (entry = head; entry; entry = entry->next)
30 * Tags for the radix tree
32 #define INTC_TAG_VIRQ_NEEDS_ALLOC 0
34 void intc_irq_xlate_set(unsigned int irq, intc_enum id, struct intc_desc_int *d)
36 unsigned long flags;
38 raw_spin_lock_irqsave(&intc_big_lock, flags);
39 intc_irq_xlate[irq].enum_id = id;
40 intc_irq_xlate[irq].desc = d;
41 raw_spin_unlock_irqrestore(&intc_big_lock, flags);
44 struct intc_map_entry *intc_irq_xlate_get(unsigned int irq)
46 return intc_irq_xlate + irq;
49 int intc_irq_lookup(const char *chipname, intc_enum enum_id)
51 struct intc_map_entry *ptr;
52 struct intc_desc_int *d;
53 int irq = -1;
55 list_for_each_entry(d, &intc_list, list) {
56 int tagged;
58 if (strcmp(d->chip.name, chipname) != 0)
59 continue;
62 * Catch early lookups for subgroup VIRQs that have not
63 * yet been allocated an IRQ. This already includes a
64 * fast-path out if the tree is untagged, so there is no
65 * need to explicitly test the root tree.
67 tagged = radix_tree_tag_get(&d->tree, enum_id,
68 INTC_TAG_VIRQ_NEEDS_ALLOC);
69 if (unlikely(tagged))
70 break;
72 ptr = radix_tree_lookup(&d->tree, enum_id);
73 if (ptr) {
74 irq = ptr - intc_irq_xlate;
75 break;
79 return irq;
81 EXPORT_SYMBOL_GPL(intc_irq_lookup);
83 static int add_virq_to_pirq(unsigned int irq, unsigned int virq)
85 struct intc_virq_list **last, *entry;
86 struct irq_data *data = irq_get_irq_data(irq);
88 /* scan for duplicates */
89 last = (struct intc_virq_list **)&data->handler_data;
90 for_each_virq(entry, data->handler_data) {
91 if (entry->irq == virq)
92 return 0;
93 last = &entry->next;
96 entry = kzalloc(sizeof(struct intc_virq_list), GFP_ATOMIC);
97 if (!entry) {
98 pr_err("can't allocate VIRQ mapping for %d\n", virq);
99 return -ENOMEM;
102 entry->irq = virq;
104 *last = entry;
106 return 0;
109 static void intc_virq_handler(unsigned int irq, struct irq_desc *desc)
111 struct irq_data *data = irq_get_irq_data(irq);
112 struct irq_chip *chip = irq_data_get_irq_chip(data);
113 struct intc_virq_list *entry, *vlist = irq_data_get_irq_handler_data(data);
114 struct intc_desc_int *d = get_intc_desc(irq);
116 chip->irq_mask_ack(data);
118 for_each_virq(entry, vlist) {
119 unsigned long addr, handle;
121 handle = (unsigned long)irq_get_handler_data(entry->irq);
122 addr = INTC_REG(d, _INTC_ADDR_E(handle), 0);
124 if (intc_reg_fns[_INTC_FN(handle)](addr, handle, 0))
125 generic_handle_irq(entry->irq);
128 chip->irq_unmask(data);
131 static unsigned long __init intc_subgroup_data(struct intc_subgroup *subgroup,
132 struct intc_desc_int *d,
133 unsigned int index)
135 unsigned int fn = REG_FN_TEST_BASE + (subgroup->reg_width >> 3) - 1;
137 return _INTC_MK(fn, MODE_ENABLE_REG, intc_get_reg(d, subgroup->reg),
138 0, 1, (subgroup->reg_width - 1) - index);
141 static void __init intc_subgroup_init_one(struct intc_desc *desc,
142 struct intc_desc_int *d,
143 struct intc_subgroup *subgroup)
145 struct intc_map_entry *mapped;
146 unsigned int pirq;
147 unsigned long flags;
148 int i;
150 mapped = radix_tree_lookup(&d->tree, subgroup->parent_id);
151 if (!mapped) {
152 WARN_ON(1);
153 return;
156 pirq = mapped - intc_irq_xlate;
158 raw_spin_lock_irqsave(&d->lock, flags);
160 for (i = 0; i < ARRAY_SIZE(subgroup->enum_ids); i++) {
161 struct intc_subgroup_entry *entry;
162 int err;
164 if (!subgroup->enum_ids[i])
165 continue;
167 entry = kmalloc(sizeof(*entry), GFP_NOWAIT);
168 if (!entry)
169 break;
171 entry->pirq = pirq;
172 entry->enum_id = subgroup->enum_ids[i];
173 entry->handle = intc_subgroup_data(subgroup, d, i);
175 err = radix_tree_insert(&d->tree, entry->enum_id, entry);
176 if (unlikely(err < 0))
177 break;
179 radix_tree_tag_set(&d->tree, entry->enum_id,
180 INTC_TAG_VIRQ_NEEDS_ALLOC);
183 raw_spin_unlock_irqrestore(&d->lock, flags);
186 void __init intc_subgroup_init(struct intc_desc *desc, struct intc_desc_int *d)
188 int i;
190 if (!desc->hw.subgroups)
191 return;
193 for (i = 0; i < desc->hw.nr_subgroups; i++)
194 intc_subgroup_init_one(desc, d, desc->hw.subgroups + i);
197 static void __init intc_subgroup_map(struct intc_desc_int *d)
199 struct intc_subgroup_entry *entries[32];
200 unsigned long flags;
201 unsigned int nr_found;
202 int i;
204 raw_spin_lock_irqsave(&d->lock, flags);
206 restart:
207 nr_found = radix_tree_gang_lookup_tag_slot(&d->tree,
208 (void ***)entries, 0, ARRAY_SIZE(entries),
209 INTC_TAG_VIRQ_NEEDS_ALLOC);
211 for (i = 0; i < nr_found; i++) {
212 struct intc_subgroup_entry *entry;
213 int irq;
215 entry = radix_tree_deref_slot((void **)entries[i]);
216 if (unlikely(!entry))
217 continue;
218 if (radix_tree_deref_retry(entry))
219 goto restart;
221 irq = create_irq();
222 if (unlikely(irq < 0)) {
223 pr_err("no more free IRQs, bailing..\n");
224 break;
227 pr_info("Setting up a chained VIRQ from %d -> %d\n",
228 irq, entry->pirq);
230 intc_irq_xlate_set(irq, entry->enum_id, d);
232 irq_set_chip_and_handler_name(irq, irq_get_chip(entry->pirq),
233 handle_simple_irq, "virq");
234 irq_set_chip_data(irq, irq_get_chip_data(entry->pirq));
236 irq_set_handler_data(irq, (void *)entry->handle);
239 * Set the virtual IRQ as non-threadable.
241 irq_set_nothread(irq);
243 irq_set_chained_handler(entry->pirq, intc_virq_handler);
244 add_virq_to_pirq(entry->pirq, irq);
246 radix_tree_tag_clear(&d->tree, entry->enum_id,
247 INTC_TAG_VIRQ_NEEDS_ALLOC);
248 radix_tree_replace_slot((void **)entries[i],
249 &intc_irq_xlate[irq]);
252 raw_spin_unlock_irqrestore(&d->lock, flags);
255 void __init intc_finalize(void)
257 struct intc_desc_int *d;
259 list_for_each_entry(d, &intc_list, list)
260 if (radix_tree_tagged(&d->tree, INTC_TAG_VIRQ_NEEDS_ALLOC))
261 intc_subgroup_map(d);