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> |
Paul Gortmaker | 07c9273 | 2011-07-10 12:57:12 -0400 | [diff] [blame] | 17 | #include <linux/export.h> |
Paul Mundt | 2be6bb0 | 2010-10-05 22:10:30 +0900 | [diff] [blame] | 18 | #include "internals.h" |
| 19 | |
Rob Herring | 0f55239 | 2012-01-17 13:10:25 -0600 | [diff] [blame] | 20 | static struct intc_map_entry intc_irq_xlate[INTC_NR_IRQS]; |
Paul Mundt | 2be6bb0 | 2010-10-05 22:10:30 +0900 | [diff] [blame] | 21 | |
| 22 | struct intc_virq_list { |
| 23 | unsigned int irq; |
| 24 | struct intc_virq_list *next; |
| 25 | }; |
| 26 | |
| 27 | #define for_each_virq(entry, head) \ |
| 28 | for (entry = head; entry; entry = entry->next) |
| 29 | |
| 30 | /* |
| 31 | * Tags for the radix tree |
| 32 | */ |
| 33 | #define INTC_TAG_VIRQ_NEEDS_ALLOC 0 |
| 34 | |
| 35 | void intc_irq_xlate_set(unsigned int irq, intc_enum id, struct intc_desc_int *d) |
| 36 | { |
| 37 | unsigned long flags; |
| 38 | |
| 39 | raw_spin_lock_irqsave(&intc_big_lock, flags); |
| 40 | intc_irq_xlate[irq].enum_id = id; |
| 41 | intc_irq_xlate[irq].desc = d; |
| 42 | raw_spin_unlock_irqrestore(&intc_big_lock, flags); |
| 43 | } |
| 44 | |
| 45 | struct intc_map_entry *intc_irq_xlate_get(unsigned int irq) |
| 46 | { |
| 47 | return intc_irq_xlate + irq; |
| 48 | } |
| 49 | |
| 50 | int intc_irq_lookup(const char *chipname, intc_enum enum_id) |
| 51 | { |
| 52 | struct intc_map_entry *ptr; |
| 53 | struct intc_desc_int *d; |
| 54 | int irq = -1; |
| 55 | |
| 56 | list_for_each_entry(d, &intc_list, list) { |
| 57 | int tagged; |
| 58 | |
| 59 | if (strcmp(d->chip.name, chipname) != 0) |
| 60 | continue; |
| 61 | |
| 62 | /* |
| 63 | * Catch early lookups for subgroup VIRQs that have not |
| 64 | * yet been allocated an IRQ. This already includes a |
| 65 | * fast-path out if the tree is untagged, so there is no |
| 66 | * need to explicitly test the root tree. |
| 67 | */ |
| 68 | tagged = radix_tree_tag_get(&d->tree, enum_id, |
| 69 | INTC_TAG_VIRQ_NEEDS_ALLOC); |
| 70 | if (unlikely(tagged)) |
| 71 | break; |
| 72 | |
| 73 | ptr = radix_tree_lookup(&d->tree, enum_id); |
| 74 | if (ptr) { |
| 75 | irq = ptr - intc_irq_xlate; |
| 76 | break; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | return irq; |
| 81 | } |
| 82 | EXPORT_SYMBOL_GPL(intc_irq_lookup); |
| 83 | |
| 84 | static int add_virq_to_pirq(unsigned int irq, unsigned int virq) |
| 85 | { |
Jiang Liu | d0abe2f | 2015-07-13 20:51:22 +0000 | [diff] [blame] | 86 | struct intc_virq_list *entry; |
| 87 | struct intc_virq_list **last = NULL; |
Paul Mundt | 2be6bb0 | 2010-10-05 22:10:30 +0900 | [diff] [blame] | 88 | |
| 89 | /* scan for duplicates */ |
Jiang Liu | d0abe2f | 2015-07-13 20:51:22 +0000 | [diff] [blame] | 90 | for_each_virq(entry, irq_get_handler_data(irq)) { |
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 | |
Jiang Liu | d0abe2f | 2015-07-13 20:51:22 +0000 | [diff] [blame] | 104 | if (last) |
| 105 | *last = entry; |
| 106 | else |
| 107 | irq_set_handler_data(irq, entry); |
Paul Mundt | 2be6bb0 | 2010-10-05 22:10:30 +0900 | [diff] [blame] | 108 | |
| 109 | return 0; |
| 110 | } |
| 111 | |
Thomas Gleixner | bd0b9ac | 2015-09-14 10:42:37 +0200 | [diff] [blame] | 112 | static void intc_virq_handler(struct irq_desc *desc) |
Paul Mundt | 2be6bb0 | 2010-10-05 22:10:30 +0900 | [diff] [blame] | 113 | { |
Thomas Gleixner | c497615 | 2015-07-13 20:51:26 +0000 | [diff] [blame] | 114 | unsigned int irq = irq_desc_get_irq(desc); |
Jiang Liu | 8228a04 | 2015-07-13 20:51:25 +0000 | [diff] [blame] | 115 | struct irq_data *data = irq_desc_get_irq_data(desc); |
Paul Mundt | 26599a9 | 2010-10-27 15:42:10 +0900 | [diff] [blame] | 116 | struct irq_chip *chip = irq_data_get_irq_chip(data); |
Thomas Gleixner | fcb8918 | 2011-03-24 16:31:17 +0100 | [diff] [blame] | 117 | 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] | 118 | struct intc_desc_int *d = get_intc_desc(irq); |
| 119 | |
Paul Mundt | 26599a9 | 2010-10-27 15:42:10 +0900 | [diff] [blame] | 120 | chip->irq_mask_ack(data); |
Paul Mundt | 2be6bb0 | 2010-10-05 22:10:30 +0900 | [diff] [blame] | 121 | |
| 122 | for_each_virq(entry, vlist) { |
| 123 | unsigned long addr, handle; |
Jiang Liu | 8228a04 | 2015-07-13 20:51:25 +0000 | [diff] [blame] | 124 | struct irq_desc *vdesc = irq_to_desc(entry->irq); |
Paul Mundt | 2be6bb0 | 2010-10-05 22:10:30 +0900 | [diff] [blame] | 125 | |
Jiang Liu | 8228a04 | 2015-07-13 20:51:25 +0000 | [diff] [blame] | 126 | if (vdesc) { |
| 127 | handle = (unsigned long)irq_desc_get_handler_data(vdesc); |
| 128 | addr = INTC_REG(d, _INTC_ADDR_E(handle), 0); |
| 129 | if (intc_reg_fns[_INTC_FN(handle)](addr, handle, 0)) |
Thomas Gleixner | bd0b9ac | 2015-09-14 10:42:37 +0200 | [diff] [blame] | 130 | generic_handle_irq_desc(vdesc); |
Jiang Liu | 8228a04 | 2015-07-13 20:51:25 +0000 | [diff] [blame] | 131 | } |
Paul Mundt | 2be6bb0 | 2010-10-05 22:10:30 +0900 | [diff] [blame] | 132 | } |
| 133 | |
Paul Mundt | 26599a9 | 2010-10-27 15:42:10 +0900 | [diff] [blame] | 134 | chip->irq_unmask(data); |
Paul Mundt | 2be6bb0 | 2010-10-05 22:10:30 +0900 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | static unsigned long __init intc_subgroup_data(struct intc_subgroup *subgroup, |
| 138 | struct intc_desc_int *d, |
| 139 | unsigned int index) |
| 140 | { |
| 141 | unsigned int fn = REG_FN_TEST_BASE + (subgroup->reg_width >> 3) - 1; |
| 142 | |
| 143 | return _INTC_MK(fn, MODE_ENABLE_REG, intc_get_reg(d, subgroup->reg), |
| 144 | 0, 1, (subgroup->reg_width - 1) - index); |
| 145 | } |
| 146 | |
| 147 | static void __init intc_subgroup_init_one(struct intc_desc *desc, |
| 148 | struct intc_desc_int *d, |
| 149 | struct intc_subgroup *subgroup) |
| 150 | { |
| 151 | struct intc_map_entry *mapped; |
| 152 | unsigned int pirq; |
| 153 | unsigned long flags; |
| 154 | int i; |
| 155 | |
| 156 | mapped = radix_tree_lookup(&d->tree, subgroup->parent_id); |
| 157 | if (!mapped) { |
| 158 | WARN_ON(1); |
| 159 | return; |
| 160 | } |
| 161 | |
| 162 | pirq = mapped - intc_irq_xlate; |
| 163 | |
| 164 | raw_spin_lock_irqsave(&d->lock, flags); |
| 165 | |
| 166 | for (i = 0; i < ARRAY_SIZE(subgroup->enum_ids); i++) { |
| 167 | struct intc_subgroup_entry *entry; |
| 168 | int err; |
| 169 | |
| 170 | if (!subgroup->enum_ids[i]) |
| 171 | continue; |
| 172 | |
| 173 | entry = kmalloc(sizeof(*entry), GFP_NOWAIT); |
| 174 | if (!entry) |
| 175 | break; |
| 176 | |
| 177 | entry->pirq = pirq; |
| 178 | entry->enum_id = subgroup->enum_ids[i]; |
| 179 | entry->handle = intc_subgroup_data(subgroup, d, i); |
| 180 | |
| 181 | err = radix_tree_insert(&d->tree, entry->enum_id, entry); |
| 182 | if (unlikely(err < 0)) |
| 183 | break; |
| 184 | |
| 185 | radix_tree_tag_set(&d->tree, entry->enum_id, |
| 186 | INTC_TAG_VIRQ_NEEDS_ALLOC); |
| 187 | } |
| 188 | |
| 189 | raw_spin_unlock_irqrestore(&d->lock, flags); |
| 190 | } |
| 191 | |
| 192 | void __init intc_subgroup_init(struct intc_desc *desc, struct intc_desc_int *d) |
| 193 | { |
| 194 | int i; |
| 195 | |
| 196 | if (!desc->hw.subgroups) |
| 197 | return; |
| 198 | |
| 199 | for (i = 0; i < desc->hw.nr_subgroups; i++) |
| 200 | intc_subgroup_init_one(desc, d, desc->hw.subgroups + i); |
| 201 | } |
| 202 | |
| 203 | static void __init intc_subgroup_map(struct intc_desc_int *d) |
| 204 | { |
| 205 | struct intc_subgroup_entry *entries[32]; |
| 206 | unsigned long flags; |
| 207 | unsigned int nr_found; |
| 208 | int i; |
| 209 | |
| 210 | raw_spin_lock_irqsave(&d->lock, flags); |
| 211 | |
| 212 | restart: |
| 213 | nr_found = radix_tree_gang_lookup_tag_slot(&d->tree, |
| 214 | (void ***)entries, 0, ARRAY_SIZE(entries), |
| 215 | INTC_TAG_VIRQ_NEEDS_ALLOC); |
| 216 | |
| 217 | for (i = 0; i < nr_found; i++) { |
| 218 | struct intc_subgroup_entry *entry; |
| 219 | int irq; |
| 220 | |
| 221 | entry = radix_tree_deref_slot((void **)entries[i]); |
| 222 | if (unlikely(!entry)) |
| 223 | continue; |
Paul Mundt | 6318af9 | 2010-11-15 14:30:30 +0900 | [diff] [blame] | 224 | if (radix_tree_deref_retry(entry)) |
Paul Mundt | 2be6bb0 | 2010-10-05 22:10:30 +0900 | [diff] [blame] | 225 | goto restart; |
| 226 | |
Paul Mundt | 123df01 | 2012-05-24 19:24:18 +0900 | [diff] [blame] | 227 | irq = irq_alloc_desc(numa_node_id()); |
Paul Mundt | 2be6bb0 | 2010-10-05 22:10:30 +0900 | [diff] [blame] | 228 | if (unlikely(irq < 0)) { |
| 229 | pr_err("no more free IRQs, bailing..\n"); |
| 230 | break; |
| 231 | } |
| 232 | |
Paul Mundt | 123df01 | 2012-05-24 19:24:18 +0900 | [diff] [blame] | 233 | activate_irq(irq); |
| 234 | |
Paul Mundt | 2be6bb0 | 2010-10-05 22:10:30 +0900 | [diff] [blame] | 235 | pr_info("Setting up a chained VIRQ from %d -> %d\n", |
| 236 | irq, entry->pirq); |
| 237 | |
| 238 | intc_irq_xlate_set(irq, entry->enum_id, d); |
| 239 | |
Thomas Gleixner | fcb8918 | 2011-03-24 16:31:17 +0100 | [diff] [blame] | 240 | irq_set_chip_and_handler_name(irq, irq_get_chip(entry->pirq), |
Paul Mundt | 2be6bb0 | 2010-10-05 22:10:30 +0900 | [diff] [blame] | 241 | handle_simple_irq, "virq"); |
Thomas Gleixner | fcb8918 | 2011-03-24 16:31:17 +0100 | [diff] [blame] | 242 | irq_set_chip_data(irq, irq_get_chip_data(entry->pirq)); |
Paul Mundt | 2be6bb0 | 2010-10-05 22:10:30 +0900 | [diff] [blame] | 243 | |
Thomas Gleixner | fcb8918 | 2011-03-24 16:31:17 +0100 | [diff] [blame] | 244 | irq_set_handler_data(irq, (void *)entry->handle); |
Paul Mundt | 2be6bb0 | 2010-10-05 22:10:30 +0900 | [diff] [blame] | 245 | |
Paul Mundt | 442f56d | 2011-04-18 11:45:08 +0900 | [diff] [blame] | 246 | /* |
| 247 | * Set the virtual IRQ as non-threadable. |
| 248 | */ |
| 249 | irq_set_nothread(irq); |
| 250 | |
Thomas Gleixner | beab99f | 2015-06-22 11:31:34 +0200 | [diff] [blame] | 251 | /* Set handler data before installing the handler */ |
Paul Mundt | 2be6bb0 | 2010-10-05 22:10:30 +0900 | [diff] [blame] | 252 | add_virq_to_pirq(entry->pirq, irq); |
Thomas Gleixner | beab99f | 2015-06-22 11:31:34 +0200 | [diff] [blame] | 253 | irq_set_chained_handler(entry->pirq, intc_virq_handler); |
Paul Mundt | 2be6bb0 | 2010-10-05 22:10:30 +0900 | [diff] [blame] | 254 | |
| 255 | radix_tree_tag_clear(&d->tree, entry->enum_id, |
| 256 | INTC_TAG_VIRQ_NEEDS_ALLOC); |
| 257 | radix_tree_replace_slot((void **)entries[i], |
| 258 | &intc_irq_xlate[irq]); |
| 259 | } |
| 260 | |
| 261 | raw_spin_unlock_irqrestore(&d->lock, flags); |
| 262 | } |
| 263 | |
| 264 | void __init intc_finalize(void) |
| 265 | { |
| 266 | struct intc_desc_int *d; |
| 267 | |
| 268 | list_for_each_entry(d, &intc_list, list) |
| 269 | if (radix_tree_tagged(&d->tree, INTC_TAG_VIRQ_NEEDS_ALLOC)) |
| 270 | intc_subgroup_map(d); |
| 271 | } |