blob: d7af88534971ece3c7bb28f9fbe46db3ba174f51 [file] [log] [blame]
Kyle Swenson8d8f6542021-03-15 11:02:55 -06001/*
2 * Broadcom BCM7038 style Level 1 interrupt controller driver
3 *
4 * Copyright (C) 2014 Broadcom Corporation
5 * Author: Kevin Cernekee
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
12#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
14#include <linux/bitops.h>
15#include <linux/kconfig.h>
16#include <linux/kernel.h>
17#include <linux/init.h>
18#include <linux/interrupt.h>
19#include <linux/io.h>
20#include <linux/ioport.h>
21#include <linux/irq.h>
22#include <linux/irqdomain.h>
23#include <linux/module.h>
24#include <linux/of.h>
25#include <linux/of_irq.h>
26#include <linux/of_address.h>
27#include <linux/of_platform.h>
28#include <linux/platform_device.h>
29#include <linux/slab.h>
30#include <linux/smp.h>
31#include <linux/types.h>
32#include <linux/irqchip.h>
33#include <linux/irqchip/chained_irq.h>
34
35#define IRQS_PER_WORD 32
36#define REG_BYTES_PER_IRQ_WORD (sizeof(u32) * 4)
37#define MAX_WORDS 8
38
39struct bcm7038_l1_cpu;
40
41struct bcm7038_l1_chip {
42 raw_spinlock_t lock;
43 unsigned int n_words;
44 struct irq_domain *domain;
45 struct bcm7038_l1_cpu *cpus[NR_CPUS];
46 u8 affinity[MAX_WORDS * IRQS_PER_WORD];
47};
48
49struct bcm7038_l1_cpu {
50 void __iomem *map_base;
51 u32 mask_cache[0];
52};
53
54/*
55 * STATUS/MASK_STATUS/MASK_SET/MASK_CLEAR are packed one right after another:
56 *
57 * 7038:
58 * 0x1000_1400: W0_STATUS
59 * 0x1000_1404: W1_STATUS
60 * 0x1000_1408: W0_MASK_STATUS
61 * 0x1000_140c: W1_MASK_STATUS
62 * 0x1000_1410: W0_MASK_SET
63 * 0x1000_1414: W1_MASK_SET
64 * 0x1000_1418: W0_MASK_CLEAR
65 * 0x1000_141c: W1_MASK_CLEAR
66 *
67 * 7445:
68 * 0xf03e_1500: W0_STATUS
69 * 0xf03e_1504: W1_STATUS
70 * 0xf03e_1508: W2_STATUS
71 * 0xf03e_150c: W3_STATUS
72 * 0xf03e_1510: W4_STATUS
73 * 0xf03e_1514: W0_MASK_STATUS
74 * 0xf03e_1518: W1_MASK_STATUS
75 * [...]
76 */
77
78static inline unsigned int reg_status(struct bcm7038_l1_chip *intc,
79 unsigned int word)
80{
81 return (0 * intc->n_words + word) * sizeof(u32);
82}
83
84static inline unsigned int reg_mask_status(struct bcm7038_l1_chip *intc,
85 unsigned int word)
86{
87 return (1 * intc->n_words + word) * sizeof(u32);
88}
89
90static inline unsigned int reg_mask_set(struct bcm7038_l1_chip *intc,
91 unsigned int word)
92{
93 return (2 * intc->n_words + word) * sizeof(u32);
94}
95
96static inline unsigned int reg_mask_clr(struct bcm7038_l1_chip *intc,
97 unsigned int word)
98{
99 return (3 * intc->n_words + word) * sizeof(u32);
100}
101
102static inline u32 l1_readl(void __iomem *reg)
103{
104 if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
105 return ioread32be(reg);
106 else
107 return readl(reg);
108}
109
110static inline void l1_writel(u32 val, void __iomem *reg)
111{
112 if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
113 iowrite32be(val, reg);
114 else
115 writel(val, reg);
116}
117
118static void bcm7038_l1_irq_handle(struct irq_desc *desc)
119{
120 struct bcm7038_l1_chip *intc = irq_desc_get_handler_data(desc);
121 struct bcm7038_l1_cpu *cpu;
122 struct irq_chip *chip = irq_desc_get_chip(desc);
123 unsigned int idx;
124
125#ifdef CONFIG_SMP
126 cpu = intc->cpus[cpu_logical_map(smp_processor_id())];
127#else
128 cpu = intc->cpus[0];
129#endif
130
131 chained_irq_enter(chip, desc);
132
133 for (idx = 0; idx < intc->n_words; idx++) {
134 int base = idx * IRQS_PER_WORD;
135 unsigned long pending, flags;
136 int hwirq;
137
138 raw_spin_lock_irqsave(&intc->lock, flags);
139 pending = l1_readl(cpu->map_base + reg_status(intc, idx)) &
140 ~cpu->mask_cache[idx];
141 raw_spin_unlock_irqrestore(&intc->lock, flags);
142
143 for_each_set_bit(hwirq, &pending, IRQS_PER_WORD) {
144 generic_handle_irq(irq_find_mapping(intc->domain,
145 base + hwirq));
146 }
147 }
148
149 chained_irq_exit(chip, desc);
150}
151
152static void __bcm7038_l1_unmask(struct irq_data *d, unsigned int cpu_idx)
153{
154 struct bcm7038_l1_chip *intc = irq_data_get_irq_chip_data(d);
155 u32 word = d->hwirq / IRQS_PER_WORD;
156 u32 mask = BIT(d->hwirq % IRQS_PER_WORD);
157
158 intc->cpus[cpu_idx]->mask_cache[word] &= ~mask;
159 l1_writel(mask, intc->cpus[cpu_idx]->map_base +
160 reg_mask_clr(intc, word));
161}
162
163static void __bcm7038_l1_mask(struct irq_data *d, unsigned int cpu_idx)
164{
165 struct bcm7038_l1_chip *intc = irq_data_get_irq_chip_data(d);
166 u32 word = d->hwirq / IRQS_PER_WORD;
167 u32 mask = BIT(d->hwirq % IRQS_PER_WORD);
168
169 intc->cpus[cpu_idx]->mask_cache[word] |= mask;
170 l1_writel(mask, intc->cpus[cpu_idx]->map_base +
171 reg_mask_set(intc, word));
172}
173
174static void bcm7038_l1_unmask(struct irq_data *d)
175{
176 struct bcm7038_l1_chip *intc = irq_data_get_irq_chip_data(d);
177 unsigned long flags;
178
179 raw_spin_lock_irqsave(&intc->lock, flags);
180 __bcm7038_l1_unmask(d, intc->affinity[d->hwirq]);
181 raw_spin_unlock_irqrestore(&intc->lock, flags);
182}
183
184static void bcm7038_l1_mask(struct irq_data *d)
185{
186 struct bcm7038_l1_chip *intc = irq_data_get_irq_chip_data(d);
187 unsigned long flags;
188
189 raw_spin_lock_irqsave(&intc->lock, flags);
190 __bcm7038_l1_mask(d, intc->affinity[d->hwirq]);
191 raw_spin_unlock_irqrestore(&intc->lock, flags);
192}
193
194static int bcm7038_l1_set_affinity(struct irq_data *d,
195 const struct cpumask *dest,
196 bool force)
197{
198 struct bcm7038_l1_chip *intc = irq_data_get_irq_chip_data(d);
199 unsigned long flags;
200 irq_hw_number_t hw = d->hwirq;
201 u32 word = hw / IRQS_PER_WORD;
202 u32 mask = BIT(hw % IRQS_PER_WORD);
203 unsigned int first_cpu = cpumask_any_and(dest, cpu_online_mask);
204 bool was_disabled;
205
206 raw_spin_lock_irqsave(&intc->lock, flags);
207
208 was_disabled = !!(intc->cpus[intc->affinity[hw]]->mask_cache[word] &
209 mask);
210 __bcm7038_l1_mask(d, intc->affinity[hw]);
211 intc->affinity[hw] = first_cpu;
212 if (!was_disabled)
213 __bcm7038_l1_unmask(d, first_cpu);
214
215 raw_spin_unlock_irqrestore(&intc->lock, flags);
216 return 0;
217}
218
219static void bcm7038_l1_cpu_offline(struct irq_data *d)
220{
221 struct cpumask *mask = irq_data_get_affinity_mask(d);
222 int cpu = smp_processor_id();
223 cpumask_t new_affinity;
224
225 /* This CPU was not on the affinity mask */
226 if (!cpumask_test_cpu(cpu, mask))
227 return;
228
229 if (cpumask_weight(mask) > 1) {
230 /*
231 * Multiple CPU affinity, remove this CPU from the affinity
232 * mask
233 */
234 cpumask_copy(&new_affinity, mask);
235 cpumask_clear_cpu(cpu, &new_affinity);
236 } else {
237 /* Only CPU, put on the lowest online CPU */
238 cpumask_clear(&new_affinity);
239 cpumask_set_cpu(cpumask_first(cpu_online_mask), &new_affinity);
240 }
241 irq_set_affinity_locked(d, &new_affinity, false);
242}
243
244static int __init bcm7038_l1_init_one(struct device_node *dn,
245 unsigned int idx,
246 struct bcm7038_l1_chip *intc)
247{
248 struct resource res;
249 resource_size_t sz;
250 struct bcm7038_l1_cpu *cpu;
251 unsigned int i, n_words, parent_irq;
252
253 if (of_address_to_resource(dn, idx, &res))
254 return -EINVAL;
255 sz = resource_size(&res);
256 n_words = sz / REG_BYTES_PER_IRQ_WORD;
257
258 if (n_words > MAX_WORDS)
259 return -EINVAL;
260 else if (!intc->n_words)
261 intc->n_words = n_words;
262 else if (intc->n_words != n_words)
263 return -EINVAL;
264
265 cpu = intc->cpus[idx] = kzalloc(sizeof(*cpu) + n_words * sizeof(u32),
266 GFP_KERNEL);
267 if (!cpu)
268 return -ENOMEM;
269
270 cpu->map_base = ioremap(res.start, sz);
271 if (!cpu->map_base)
272 return -ENOMEM;
273
274 for (i = 0; i < n_words; i++) {
275 l1_writel(0xffffffff, cpu->map_base + reg_mask_set(intc, i));
276 cpu->mask_cache[i] = 0xffffffff;
277 }
278
279 parent_irq = irq_of_parse_and_map(dn, idx);
280 if (!parent_irq) {
281 pr_err("failed to map parent interrupt %d\n", parent_irq);
282 return -EINVAL;
283 }
284 irq_set_chained_handler_and_data(parent_irq, bcm7038_l1_irq_handle,
285 intc);
286
287 return 0;
288}
289
290static struct irq_chip bcm7038_l1_irq_chip = {
291 .name = "bcm7038-l1",
292 .irq_mask = bcm7038_l1_mask,
293 .irq_unmask = bcm7038_l1_unmask,
294 .irq_set_affinity = bcm7038_l1_set_affinity,
295 .irq_cpu_offline = bcm7038_l1_cpu_offline,
296};
297
298static int bcm7038_l1_map(struct irq_domain *d, unsigned int virq,
299 irq_hw_number_t hw_irq)
300{
301 irq_set_chip_and_handler(virq, &bcm7038_l1_irq_chip, handle_level_irq);
302 irq_set_chip_data(virq, d->host_data);
303 return 0;
304}
305
306static const struct irq_domain_ops bcm7038_l1_domain_ops = {
307 .xlate = irq_domain_xlate_onecell,
308 .map = bcm7038_l1_map,
309};
310
311int __init bcm7038_l1_of_init(struct device_node *dn,
312 struct device_node *parent)
313{
314 struct bcm7038_l1_chip *intc;
315 int idx, ret;
316
317 intc = kzalloc(sizeof(*intc), GFP_KERNEL);
318 if (!intc)
319 return -ENOMEM;
320
321 raw_spin_lock_init(&intc->lock);
322 for_each_possible_cpu(idx) {
323 ret = bcm7038_l1_init_one(dn, idx, intc);
324 if (ret < 0) {
325 if (idx)
326 break;
327 pr_err("failed to remap intc L1 registers\n");
328 goto out_free;
329 }
330 }
331
332 intc->domain = irq_domain_add_linear(dn, IRQS_PER_WORD * intc->n_words,
333 &bcm7038_l1_domain_ops,
334 intc);
335 if (!intc->domain) {
336 ret = -ENOMEM;
337 goto out_unmap;
338 }
339
340 pr_info("registered BCM7038 L1 intc (mem: 0x%p, IRQs: %d)\n",
341 intc->cpus[0]->map_base, IRQS_PER_WORD * intc->n_words);
342
343 return 0;
344
345out_unmap:
346 for_each_possible_cpu(idx) {
347 struct bcm7038_l1_cpu *cpu = intc->cpus[idx];
348
349 if (cpu) {
350 if (cpu->map_base)
351 iounmap(cpu->map_base);
352 kfree(cpu);
353 }
354 }
355out_free:
356 kfree(intc);
357 return ret;
358}
359
360IRQCHIP_DECLARE(bcm7038_l1, "brcm,bcm7038-l1-intc", bcm7038_l1_of_init);