blob: 0f735301d3ece0746e4d6b1f1ce0c2326d2076ed [file] [log] [blame]
Paul Mundt2be6bb02010-10-05 22:10:30 +09001/*
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 Gortmaker07c92732011-07-10 12:57:12 -040017#include <linux/export.h>
Paul Mundt2be6bb02010-10-05 22:10:30 +090018#include "internals.h"
19
Rob Herring0f552392012-01-17 13:10:25 -060020static struct intc_map_entry intc_irq_xlate[INTC_NR_IRQS];
Paul Mundt2be6bb02010-10-05 22:10:30 +090021
22struct 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
35void 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
45struct intc_map_entry *intc_irq_xlate_get(unsigned int irq)
46{
47 return intc_irq_xlate + irq;
48}
49
50int 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}
82EXPORT_SYMBOL_GPL(intc_irq_lookup);
83
84static int add_virq_to_pirq(unsigned int irq, unsigned int virq)
85{
Jiang Liud0abe2f2015-07-13 20:51:22 +000086 struct intc_virq_list *entry;
87 struct intc_virq_list **last = NULL;
Paul Mundt2be6bb02010-10-05 22:10:30 +090088
89 /* scan for duplicates */
Jiang Liud0abe2f2015-07-13 20:51:22 +000090 for_each_virq(entry, irq_get_handler_data(irq)) {
Paul Mundt2be6bb02010-10-05 22:10:30 +090091 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 Liud0abe2f2015-07-13 20:51:22 +0000104 if (last)
105 *last = entry;
106 else
107 irq_set_handler_data(irq, entry);
Paul Mundt2be6bb02010-10-05 22:10:30 +0900108
109 return 0;
110}
111
112static void intc_virq_handler(unsigned int irq, struct irq_desc *desc)
113{
Paul Mundt26599a92010-10-27 15:42:10 +0900114 struct irq_data *data = irq_get_irq_data(irq);
115 struct irq_chip *chip = irq_data_get_irq_chip(data);
Thomas Gleixnerfcb89182011-03-24 16:31:17 +0100116 struct intc_virq_list *entry, *vlist = irq_data_get_irq_handler_data(data);
Paul Mundt2be6bb02010-10-05 22:10:30 +0900117 struct intc_desc_int *d = get_intc_desc(irq);
118
Paul Mundt26599a92010-10-27 15:42:10 +0900119 chip->irq_mask_ack(data);
Paul Mundt2be6bb02010-10-05 22:10:30 +0900120
121 for_each_virq(entry, vlist) {
122 unsigned long addr, handle;
123
Thomas Gleixnerfcb89182011-03-24 16:31:17 +0100124 handle = (unsigned long)irq_get_handler_data(entry->irq);
Paul Mundt2be6bb02010-10-05 22:10:30 +0900125 addr = INTC_REG(d, _INTC_ADDR_E(handle), 0);
126
127 if (intc_reg_fns[_INTC_FN(handle)](addr, handle, 0))
128 generic_handle_irq(entry->irq);
129 }
130
Paul Mundt26599a92010-10-27 15:42:10 +0900131 chip->irq_unmask(data);
Paul Mundt2be6bb02010-10-05 22:10:30 +0900132}
133
134static unsigned long __init intc_subgroup_data(struct intc_subgroup *subgroup,
135 struct intc_desc_int *d,
136 unsigned int index)
137{
138 unsigned int fn = REG_FN_TEST_BASE + (subgroup->reg_width >> 3) - 1;
139
140 return _INTC_MK(fn, MODE_ENABLE_REG, intc_get_reg(d, subgroup->reg),
141 0, 1, (subgroup->reg_width - 1) - index);
142}
143
144static void __init intc_subgroup_init_one(struct intc_desc *desc,
145 struct intc_desc_int *d,
146 struct intc_subgroup *subgroup)
147{
148 struct intc_map_entry *mapped;
149 unsigned int pirq;
150 unsigned long flags;
151 int i;
152
153 mapped = radix_tree_lookup(&d->tree, subgroup->parent_id);
154 if (!mapped) {
155 WARN_ON(1);
156 return;
157 }
158
159 pirq = mapped - intc_irq_xlate;
160
161 raw_spin_lock_irqsave(&d->lock, flags);
162
163 for (i = 0; i < ARRAY_SIZE(subgroup->enum_ids); i++) {
164 struct intc_subgroup_entry *entry;
165 int err;
166
167 if (!subgroup->enum_ids[i])
168 continue;
169
170 entry = kmalloc(sizeof(*entry), GFP_NOWAIT);
171 if (!entry)
172 break;
173
174 entry->pirq = pirq;
175 entry->enum_id = subgroup->enum_ids[i];
176 entry->handle = intc_subgroup_data(subgroup, d, i);
177
178 err = radix_tree_insert(&d->tree, entry->enum_id, entry);
179 if (unlikely(err < 0))
180 break;
181
182 radix_tree_tag_set(&d->tree, entry->enum_id,
183 INTC_TAG_VIRQ_NEEDS_ALLOC);
184 }
185
186 raw_spin_unlock_irqrestore(&d->lock, flags);
187}
188
189void __init intc_subgroup_init(struct intc_desc *desc, struct intc_desc_int *d)
190{
191 int i;
192
193 if (!desc->hw.subgroups)
194 return;
195
196 for (i = 0; i < desc->hw.nr_subgroups; i++)
197 intc_subgroup_init_one(desc, d, desc->hw.subgroups + i);
198}
199
200static void __init intc_subgroup_map(struct intc_desc_int *d)
201{
202 struct intc_subgroup_entry *entries[32];
203 unsigned long flags;
204 unsigned int nr_found;
205 int i;
206
207 raw_spin_lock_irqsave(&d->lock, flags);
208
209restart:
210 nr_found = radix_tree_gang_lookup_tag_slot(&d->tree,
211 (void ***)entries, 0, ARRAY_SIZE(entries),
212 INTC_TAG_VIRQ_NEEDS_ALLOC);
213
214 for (i = 0; i < nr_found; i++) {
215 struct intc_subgroup_entry *entry;
216 int irq;
217
218 entry = radix_tree_deref_slot((void **)entries[i]);
219 if (unlikely(!entry))
220 continue;
Paul Mundt6318af92010-11-15 14:30:30 +0900221 if (radix_tree_deref_retry(entry))
Paul Mundt2be6bb02010-10-05 22:10:30 +0900222 goto restart;
223
Paul Mundt123df012012-05-24 19:24:18 +0900224 irq = irq_alloc_desc(numa_node_id());
Paul Mundt2be6bb02010-10-05 22:10:30 +0900225 if (unlikely(irq < 0)) {
226 pr_err("no more free IRQs, bailing..\n");
227 break;
228 }
229
Paul Mundt123df012012-05-24 19:24:18 +0900230 activate_irq(irq);
231
Paul Mundt2be6bb02010-10-05 22:10:30 +0900232 pr_info("Setting up a chained VIRQ from %d -> %d\n",
233 irq, entry->pirq);
234
235 intc_irq_xlate_set(irq, entry->enum_id, d);
236
Thomas Gleixnerfcb89182011-03-24 16:31:17 +0100237 irq_set_chip_and_handler_name(irq, irq_get_chip(entry->pirq),
Paul Mundt2be6bb02010-10-05 22:10:30 +0900238 handle_simple_irq, "virq");
Thomas Gleixnerfcb89182011-03-24 16:31:17 +0100239 irq_set_chip_data(irq, irq_get_chip_data(entry->pirq));
Paul Mundt2be6bb02010-10-05 22:10:30 +0900240
Thomas Gleixnerfcb89182011-03-24 16:31:17 +0100241 irq_set_handler_data(irq, (void *)entry->handle);
Paul Mundt2be6bb02010-10-05 22:10:30 +0900242
Paul Mundt442f56d2011-04-18 11:45:08 +0900243 /*
244 * Set the virtual IRQ as non-threadable.
245 */
246 irq_set_nothread(irq);
247
Thomas Gleixnerbeab99f2015-06-22 11:31:34 +0200248 /* Set handler data before installing the handler */
Paul Mundt2be6bb02010-10-05 22:10:30 +0900249 add_virq_to_pirq(entry->pirq, irq);
Thomas Gleixnerbeab99f2015-06-22 11:31:34 +0200250 irq_set_chained_handler(entry->pirq, intc_virq_handler);
Paul Mundt2be6bb02010-10-05 22:10:30 +0900251
252 radix_tree_tag_clear(&d->tree, entry->enum_id,
253 INTC_TAG_VIRQ_NEEDS_ALLOC);
254 radix_tree_replace_slot((void **)entries[i],
255 &intc_irq_xlate[irq]);
256 }
257
258 raw_spin_unlock_irqrestore(&d->lock, flags);
259}
260
261void __init intc_finalize(void)
262{
263 struct intc_desc_int *d;
264
265 list_for_each_entry(d, &intc_list, list)
266 if (radix_tree_tagged(&d->tree, INTC_TAG_VIRQ_NEEDS_ALLOC))
267 intc_subgroup_map(d);
268}