Paul Mundt | 2be6bb0 | 2010-10-05 22:10:30 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Support for virtual IRQ subgroups. |
| 3 | * |
| 4 | * Copyright (C) 2010 Paul Mundt |
| 5 | * |
| 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 |
| 11 | |
| 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" |
| 18 | |
| 19 | static struct intc_map_entry intc_irq_xlate[NR_IRQS]; |
| 20 | |
| 21 | struct intc_virq_list { |
| 22 | unsigned int irq; |
| 23 | struct intc_virq_list *next; |
| 24 | }; |
| 25 | |
| 26 | #define for_each_virq(entry, head) \ |
| 27 | for (entry = head; entry; entry = entry->next) |
| 28 | |
| 29 | /* |
| 30 | * Tags for the radix tree |
| 31 | */ |
| 32 | #define INTC_TAG_VIRQ_NEEDS_ALLOC 0 |
| 33 | |
| 34 | void intc_irq_xlate_set(unsigned int irq, intc_enum id, struct intc_desc_int *d) |
| 35 | { |
| 36 | unsigned long flags; |
| 37 | |
| 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); |
| 42 | } |
| 43 | |
| 44 | struct intc_map_entry *intc_irq_xlate_get(unsigned int irq) |
| 45 | { |
| 46 | return intc_irq_xlate + irq; |
| 47 | } |
| 48 | |
| 49 | int intc_irq_lookup(const char *chipname, intc_enum enum_id) |
| 50 | { |
| 51 | struct intc_map_entry *ptr; |
| 52 | struct intc_desc_int *d; |
| 53 | int irq = -1; |
| 54 | |
| 55 | list_for_each_entry(d, &intc_list, list) { |
| 56 | int tagged; |
| 57 | |
| 58 | if (strcmp(d->chip.name, chipname) != 0) |
| 59 | continue; |
| 60 | |
| 61 | /* |
| 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. |
| 66 | */ |
| 67 | tagged = radix_tree_tag_get(&d->tree, enum_id, |
| 68 | INTC_TAG_VIRQ_NEEDS_ALLOC); |
| 69 | if (unlikely(tagged)) |
| 70 | break; |
| 71 | |
| 72 | ptr = radix_tree_lookup(&d->tree, enum_id); |
| 73 | if (ptr) { |
| 74 | irq = ptr - intc_irq_xlate; |
| 75 | break; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | return irq; |
| 80 | } |
| 81 | EXPORT_SYMBOL_GPL(intc_irq_lookup); |
| 82 | |
| 83 | static int add_virq_to_pirq(unsigned int irq, unsigned int virq) |
| 84 | { |
| 85 | struct intc_virq_list **last, *entry; |
Paul Mundt | 26599a9 | 2010-10-27 15:42:10 +0900 | [diff] [blame] | 86 | struct irq_data *data = irq_get_irq_data(irq); |
Paul Mundt | 2be6bb0 | 2010-10-05 22:10:30 +0900 | [diff] [blame] | 87 | |
| 88 | /* scan for duplicates */ |
Paul Mundt | 26599a9 | 2010-10-27 15:42:10 +0900 | [diff] [blame] | 89 | last = (struct intc_virq_list **)&data->handler_data; |
| 90 | for_each_virq(entry, data->handler_data) { |
Paul Mundt | 2be6bb0 | 2010-10-05 22:10:30 +0900 | [diff] [blame] | 91 | if (entry->irq == virq) |
| 92 | return 0; |
| 93 | last = &entry->next; |
| 94 | } |
| 95 | |
| 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; |
| 100 | } |
| 101 | |
| 102 | entry->irq = virq; |
| 103 | |
| 104 | *last = entry; |
| 105 | |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | static void intc_virq_handler(unsigned int irq, struct irq_desc *desc) |
| 110 | { |
Paul Mundt | 26599a9 | 2010-10-27 15:42:10 +0900 | [diff] [blame] | 111 | struct irq_data *data = irq_get_irq_data(irq); |
| 112 | struct irq_chip *chip = irq_data_get_irq_chip(data); |
Thomas Gleixner | fcb8918 | 2011-03-24 16:31:17 +0100 | [diff] [blame] | 113 | struct intc_virq_list *entry, *vlist = irq_data_get_irq_handler_data(data); |
Paul Mundt | 2be6bb0 | 2010-10-05 22:10:30 +0900 | [diff] [blame] | 114 | struct intc_desc_int *d = get_intc_desc(irq); |
| 115 | |
Paul Mundt | 26599a9 | 2010-10-27 15:42:10 +0900 | [diff] [blame] | 116 | chip->irq_mask_ack(data); |
Paul Mundt | 2be6bb0 | 2010-10-05 22:10:30 +0900 | [diff] [blame] | 117 | |
| 118 | for_each_virq(entry, vlist) { |
| 119 | unsigned long addr, handle; |
| 120 | |
Thomas Gleixner | fcb8918 | 2011-03-24 16:31:17 +0100 | [diff] [blame] | 121 | handle = (unsigned long)irq_get_handler_data(entry->irq); |
Paul Mundt | 2be6bb0 | 2010-10-05 22:10:30 +0900 | [diff] [blame] | 122 | addr = INTC_REG(d, _INTC_ADDR_E(handle), 0); |
| 123 | |
| 124 | if (intc_reg_fns[_INTC_FN(handle)](addr, handle, 0)) |
| 125 | generic_handle_irq(entry->irq); |
| 126 | } |
| 127 | |
Paul Mundt | 26599a9 | 2010-10-27 15:42:10 +0900 | [diff] [blame] | 128 | chip->irq_unmask(data); |
Paul Mundt | 2be6bb0 | 2010-10-05 22:10:30 +0900 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | static unsigned long __init intc_subgroup_data(struct intc_subgroup *subgroup, |
| 132 | struct intc_desc_int *d, |
| 133 | unsigned int index) |
| 134 | { |
| 135 | unsigned int fn = REG_FN_TEST_BASE + (subgroup->reg_width >> 3) - 1; |
| 136 | |
| 137 | return _INTC_MK(fn, MODE_ENABLE_REG, intc_get_reg(d, subgroup->reg), |
| 138 | 0, 1, (subgroup->reg_width - 1) - index); |
| 139 | } |
| 140 | |
| 141 | static void __init intc_subgroup_init_one(struct intc_desc *desc, |
| 142 | struct intc_desc_int *d, |
| 143 | struct intc_subgroup *subgroup) |
| 144 | { |
| 145 | struct intc_map_entry *mapped; |
| 146 | unsigned int pirq; |
| 147 | unsigned long flags; |
| 148 | int i; |
| 149 | |
| 150 | mapped = radix_tree_lookup(&d->tree, subgroup->parent_id); |
| 151 | if (!mapped) { |
| 152 | WARN_ON(1); |
| 153 | return; |
| 154 | } |
| 155 | |
| 156 | pirq = mapped - intc_irq_xlate; |
| 157 | |
| 158 | raw_spin_lock_irqsave(&d->lock, flags); |
| 159 | |
| 160 | for (i = 0; i < ARRAY_SIZE(subgroup->enum_ids); i++) { |
| 161 | struct intc_subgroup_entry *entry; |
| 162 | int err; |
| 163 | |
| 164 | if (!subgroup->enum_ids[i]) |
| 165 | continue; |
| 166 | |
| 167 | entry = kmalloc(sizeof(*entry), GFP_NOWAIT); |
| 168 | if (!entry) |
| 169 | break; |
| 170 | |
| 171 | entry->pirq = pirq; |
| 172 | entry->enum_id = subgroup->enum_ids[i]; |
| 173 | entry->handle = intc_subgroup_data(subgroup, d, i); |
| 174 | |
| 175 | err = radix_tree_insert(&d->tree, entry->enum_id, entry); |
| 176 | if (unlikely(err < 0)) |
| 177 | break; |
| 178 | |
| 179 | radix_tree_tag_set(&d->tree, entry->enum_id, |
| 180 | INTC_TAG_VIRQ_NEEDS_ALLOC); |
| 181 | } |
| 182 | |
| 183 | raw_spin_unlock_irqrestore(&d->lock, flags); |
| 184 | } |
| 185 | |
| 186 | void __init intc_subgroup_init(struct intc_desc *desc, struct intc_desc_int *d) |
| 187 | { |
| 188 | int i; |
| 189 | |
| 190 | if (!desc->hw.subgroups) |
| 191 | return; |
| 192 | |
| 193 | for (i = 0; i < desc->hw.nr_subgroups; i++) |
| 194 | intc_subgroup_init_one(desc, d, desc->hw.subgroups + i); |
| 195 | } |
| 196 | |
| 197 | static void __init intc_subgroup_map(struct intc_desc_int *d) |
| 198 | { |
| 199 | struct intc_subgroup_entry *entries[32]; |
| 200 | unsigned long flags; |
| 201 | unsigned int nr_found; |
| 202 | int i; |
| 203 | |
| 204 | raw_spin_lock_irqsave(&d->lock, flags); |
| 205 | |
| 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); |
| 210 | |
| 211 | for (i = 0; i < nr_found; i++) { |
| 212 | struct intc_subgroup_entry *entry; |
| 213 | int irq; |
| 214 | |
| 215 | entry = radix_tree_deref_slot((void **)entries[i]); |
| 216 | if (unlikely(!entry)) |
| 217 | continue; |
Paul Mundt | 6318af9 | 2010-11-15 14:30:30 +0900 | [diff] [blame] | 218 | if (radix_tree_deref_retry(entry)) |
Paul Mundt | 2be6bb0 | 2010-10-05 22:10:30 +0900 | [diff] [blame] | 219 | goto restart; |
| 220 | |
| 221 | irq = create_irq(); |
| 222 | if (unlikely(irq < 0)) { |
| 223 | pr_err("no more free IRQs, bailing..\n"); |
| 224 | break; |
| 225 | } |
| 226 | |
| 227 | pr_info("Setting up a chained VIRQ from %d -> %d\n", |
| 228 | irq, entry->pirq); |
| 229 | |
| 230 | intc_irq_xlate_set(irq, entry->enum_id, d); |
| 231 | |
Thomas Gleixner | fcb8918 | 2011-03-24 16:31:17 +0100 | [diff] [blame] | 232 | irq_set_chip_and_handler_name(irq, irq_get_chip(entry->pirq), |
Paul Mundt | 2be6bb0 | 2010-10-05 22:10:30 +0900 | [diff] [blame] | 233 | handle_simple_irq, "virq"); |
Thomas Gleixner | fcb8918 | 2011-03-24 16:31:17 +0100 | [diff] [blame] | 234 | irq_set_chip_data(irq, irq_get_chip_data(entry->pirq)); |
Paul Mundt | 2be6bb0 | 2010-10-05 22:10:30 +0900 | [diff] [blame] | 235 | |
Thomas Gleixner | fcb8918 | 2011-03-24 16:31:17 +0100 | [diff] [blame] | 236 | irq_set_handler_data(irq, (void *)entry->handle); |
Paul Mundt | 2be6bb0 | 2010-10-05 22:10:30 +0900 | [diff] [blame] | 237 | |
Thomas Gleixner | fcb8918 | 2011-03-24 16:31:17 +0100 | [diff] [blame] | 238 | irq_set_chained_handler(entry->pirq, intc_virq_handler); |
Paul Mundt | 2be6bb0 | 2010-10-05 22:10:30 +0900 | [diff] [blame] | 239 | add_virq_to_pirq(entry->pirq, irq); |
| 240 | |
| 241 | radix_tree_tag_clear(&d->tree, entry->enum_id, |
| 242 | INTC_TAG_VIRQ_NEEDS_ALLOC); |
| 243 | radix_tree_replace_slot((void **)entries[i], |
| 244 | &intc_irq_xlate[irq]); |
| 245 | } |
| 246 | |
| 247 | raw_spin_unlock_irqrestore(&d->lock, flags); |
| 248 | } |
| 249 | |
| 250 | void __init intc_finalize(void) |
| 251 | { |
| 252 | struct intc_desc_int *d; |
| 253 | |
| 254 | list_for_each_entry(d, &intc_list, list) |
| 255 | if (radix_tree_tagged(&d->tree, INTC_TAG_VIRQ_NEEDS_ALLOC)) |
| 256 | intc_subgroup_map(d); |
| 257 | } |