1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2012 Regents of the University of California
4 * Copyright (C) 2017-2018 SiFive
5 * Copyright (C) 2020 Western Digital Corporation or its affiliates.
6 */
7
8 #define pr_fmt(fmt) "riscv-intc: " fmt
9 #include <linux/acpi.h>
10 #include <linux/atomic.h>
11 #include <linux/bits.h>
12 #include <linux/cpu.h>
13 #include <linux/irq.h>
14 #include <linux/irqchip.h>
15 #include <linux/irqdomain.h>
16 #include <linux/interrupt.h>
17 #include <linux/module.h>
18 #include <linux/of.h>
19 #include <linux/smp.h>
20 #include <linux/soc/andes/irq.h>
21
22 #include <asm/hwcap.h>
23
24 static struct irq_domain *intc_domain;
25 static unsigned int riscv_intc_nr_irqs __ro_after_init = BITS_PER_LONG;
26 static unsigned int riscv_intc_custom_base __ro_after_init = BITS_PER_LONG;
27 static unsigned int riscv_intc_custom_nr_irqs __ro_after_init;
28
riscv_intc_irq(struct pt_regs * regs)29 static void riscv_intc_irq(struct pt_regs *regs)
30 {
31 unsigned long cause = regs->cause & ~CAUSE_IRQ_FLAG;
32
33 if (generic_handle_domain_irq(intc_domain, cause))
34 pr_warn_ratelimited("Failed to handle interrupt (cause: %ld)\n", cause);
35 }
36
riscv_intc_aia_irq(struct pt_regs * regs)37 static void riscv_intc_aia_irq(struct pt_regs *regs)
38 {
39 unsigned long topi;
40
41 while ((topi = csr_read(CSR_TOPI)))
42 generic_handle_domain_irq(intc_domain, topi >> TOPI_IID_SHIFT);
43 }
44
45 /*
46 * On RISC-V systems local interrupts are masked or unmasked by writing
47 * the SIE (Supervisor Interrupt Enable) CSR. As CSRs can only be written
48 * on the local hart, these functions can only be called on the hart that
49 * corresponds to the IRQ chip.
50 */
51
riscv_intc_irq_mask(struct irq_data * d)52 static void riscv_intc_irq_mask(struct irq_data *d)
53 {
54 if (IS_ENABLED(CONFIG_32BIT) && d->hwirq >= BITS_PER_LONG)
55 csr_clear(CSR_IEH, BIT(d->hwirq - BITS_PER_LONG));
56 else
57 csr_clear(CSR_IE, BIT(d->hwirq));
58 }
59
riscv_intc_irq_unmask(struct irq_data * d)60 static void riscv_intc_irq_unmask(struct irq_data *d)
61 {
62 if (IS_ENABLED(CONFIG_32BIT) && d->hwirq >= BITS_PER_LONG)
63 csr_set(CSR_IEH, BIT(d->hwirq - BITS_PER_LONG));
64 else
65 csr_set(CSR_IE, BIT(d->hwirq));
66 }
67
andes_intc_irq_mask(struct irq_data * d)68 static void andes_intc_irq_mask(struct irq_data *d)
69 {
70 /*
71 * Andes specific S-mode local interrupt causes (hwirq)
72 * are defined as (256 + n) and controlled by n-th bit
73 * of SLIE.
74 */
75 unsigned int mask = BIT(d->hwirq % BITS_PER_LONG);
76
77 if (d->hwirq < ANDES_SLI_CAUSE_BASE)
78 csr_clear(CSR_IE, mask);
79 else
80 csr_clear(ANDES_CSR_SLIE, mask);
81 }
82
andes_intc_irq_unmask(struct irq_data * d)83 static void andes_intc_irq_unmask(struct irq_data *d)
84 {
85 unsigned int mask = BIT(d->hwirq % BITS_PER_LONG);
86
87 if (d->hwirq < ANDES_SLI_CAUSE_BASE)
88 csr_set(CSR_IE, mask);
89 else
90 csr_set(ANDES_CSR_SLIE, mask);
91 }
92
riscv_intc_irq_eoi(struct irq_data * d)93 static void riscv_intc_irq_eoi(struct irq_data *d)
94 {
95 /*
96 * The RISC-V INTC driver uses handle_percpu_devid_irq() flow
97 * for the per-HART local interrupts and child irqchip drivers
98 * (such as PLIC, SBI IPI, CLINT, APLIC, IMSIC, etc) implement
99 * chained handlers for the per-HART local interrupts.
100 *
101 * In the absence of irq_eoi(), the chained_irq_enter() and
102 * chained_irq_exit() functions (used by child irqchip drivers)
103 * will do unnecessary mask/unmask of per-HART local interrupts
104 * at the time of handling interrupts. To avoid this, we provide
105 * an empty irq_eoi() callback for RISC-V INTC irqchip.
106 */
107 }
108
109 static struct irq_chip riscv_intc_chip = {
110 .name = "RISC-V INTC",
111 .irq_mask = riscv_intc_irq_mask,
112 .irq_unmask = riscv_intc_irq_unmask,
113 .irq_eoi = riscv_intc_irq_eoi,
114 };
115
116 static struct irq_chip andes_intc_chip = {
117 .name = "RISC-V INTC",
118 .irq_mask = andes_intc_irq_mask,
119 .irq_unmask = andes_intc_irq_unmask,
120 .irq_eoi = riscv_intc_irq_eoi,
121 };
122
riscv_intc_domain_map(struct irq_domain * d,unsigned int irq,irq_hw_number_t hwirq)123 static int riscv_intc_domain_map(struct irq_domain *d, unsigned int irq,
124 irq_hw_number_t hwirq)
125 {
126 struct irq_chip *chip = d->host_data;
127
128 irq_set_percpu_devid(irq);
129 irq_domain_set_info(d, irq, hwirq, chip, NULL, handle_percpu_devid_irq,
130 NULL, NULL);
131
132 return 0;
133 }
134
riscv_intc_domain_alloc(struct irq_domain * domain,unsigned int virq,unsigned int nr_irqs,void * arg)135 static int riscv_intc_domain_alloc(struct irq_domain *domain,
136 unsigned int virq, unsigned int nr_irqs,
137 void *arg)
138 {
139 int i, ret;
140 irq_hw_number_t hwirq;
141 unsigned int type = IRQ_TYPE_NONE;
142 struct irq_fwspec *fwspec = arg;
143
144 ret = irq_domain_translate_onecell(domain, fwspec, &hwirq, &type);
145 if (ret)
146 return ret;
147
148 /*
149 * Only allow hwirq for which we have corresponding standard or
150 * custom interrupt enable register.
151 */
152 if (hwirq >= riscv_intc_nr_irqs &&
153 (hwirq < riscv_intc_custom_base ||
154 hwirq >= riscv_intc_custom_base + riscv_intc_custom_nr_irqs))
155 return -EINVAL;
156
157 for (i = 0; i < nr_irqs; i++) {
158 ret = riscv_intc_domain_map(domain, virq + i, hwirq + i);
159 if (ret)
160 return ret;
161 }
162
163 return 0;
164 }
165
166 static const struct irq_domain_ops riscv_intc_domain_ops = {
167 .map = riscv_intc_domain_map,
168 .xlate = irq_domain_xlate_onecell,
169 .alloc = riscv_intc_domain_alloc,
170 .free = irq_domain_free_irqs_top,
171 };
172
riscv_intc_hwnode(void)173 static struct fwnode_handle *riscv_intc_hwnode(void)
174 {
175 return intc_domain->fwnode;
176 }
177
riscv_intc_init_common(struct fwnode_handle * fn,struct irq_chip * chip)178 static int __init riscv_intc_init_common(struct fwnode_handle *fn, struct irq_chip *chip)
179 {
180 int rc;
181
182 intc_domain = irq_domain_create_tree(fn, &riscv_intc_domain_ops, chip);
183 if (!intc_domain) {
184 pr_err("unable to add IRQ domain\n");
185 return -ENXIO;
186 }
187
188 if (riscv_isa_extension_available(NULL, SxAIA)) {
189 riscv_intc_nr_irqs = 64;
190 rc = set_handle_irq(&riscv_intc_aia_irq);
191 } else {
192 rc = set_handle_irq(&riscv_intc_irq);
193 }
194 if (rc) {
195 pr_err("failed to set irq handler\n");
196 return rc;
197 }
198
199 riscv_set_intc_hwnode_fn(riscv_intc_hwnode);
200
201 pr_info("%d local interrupts mapped%s\n",
202 riscv_intc_nr_irqs,
203 riscv_isa_extension_available(NULL, SxAIA) ? " using AIA" : "");
204 if (riscv_intc_custom_nr_irqs)
205 pr_info("%d custom local interrupts mapped\n", riscv_intc_custom_nr_irqs);
206
207 return 0;
208 }
209
riscv_intc_init(struct device_node * node,struct device_node * parent)210 static int __init riscv_intc_init(struct device_node *node,
211 struct device_node *parent)
212 {
213 struct irq_chip *chip = &riscv_intc_chip;
214 unsigned long hartid;
215 int rc;
216
217 rc = riscv_of_parent_hartid(node, &hartid);
218 if (rc < 0) {
219 pr_warn("unable to find hart id for %pOF\n", node);
220 return 0;
221 }
222
223 /*
224 * The DT will have one INTC DT node under each CPU (or HART)
225 * DT node so riscv_intc_init() function will be called once
226 * for each INTC DT node. We only need to do INTC initialization
227 * for the INTC DT node belonging to boot CPU (or boot HART).
228 */
229 if (riscv_hartid_to_cpuid(hartid) != smp_processor_id()) {
230 /*
231 * The INTC nodes of each CPU are suppliers for downstream
232 * interrupt controllers (such as PLIC, IMSIC and APLIC
233 * direct-mode) so we should mark an INTC node as initialized
234 * if we are not creating IRQ domain for it.
235 */
236 fwnode_dev_initialized(of_fwnode_handle(node), true);
237 return 0;
238 }
239
240 if (of_device_is_compatible(node, "andestech,cpu-intc")) {
241 riscv_intc_custom_base = ANDES_SLI_CAUSE_BASE;
242 riscv_intc_custom_nr_irqs = ANDES_RV_IRQ_LAST;
243 chip = &andes_intc_chip;
244 }
245
246 return riscv_intc_init_common(of_fwnode_handle(node), chip);
247 }
248
249 IRQCHIP_DECLARE(riscv, "riscv,cpu-intc", riscv_intc_init);
250 IRQCHIP_DECLARE(andes, "andestech,cpu-intc", riscv_intc_init);
251
252 #ifdef CONFIG_ACPI
253
254 struct rintc_data {
255 union {
256 u32 ext_intc_id;
257 struct {
258 u32 context_id : 16,
259 reserved : 8,
260 aplic_plic_id : 8;
261 };
262 };
263 unsigned long hart_id;
264 u64 imsic_addr;
265 u32 imsic_size;
266 };
267
268 static u32 nr_rintc;
269 static struct rintc_data **rintc_acpi_data;
270
271 #define for_each_matching_plic(_plic_id) \
272 unsigned int _plic; \
273 \
274 for (_plic = 0; _plic < nr_rintc; _plic++) \
275 if (rintc_acpi_data[_plic]->aplic_plic_id != _plic_id) \
276 continue; \
277 else
278
acpi_rintc_get_plic_nr_contexts(unsigned int plic_id)279 unsigned int acpi_rintc_get_plic_nr_contexts(unsigned int plic_id)
280 {
281 unsigned int nctx = 0;
282
283 for_each_matching_plic(plic_id)
284 nctx++;
285
286 return nctx;
287 }
288
get_plic_context(unsigned int plic_id,unsigned int ctxt_idx)289 static struct rintc_data *get_plic_context(unsigned int plic_id, unsigned int ctxt_idx)
290 {
291 unsigned int ctxt = 0;
292
293 for_each_matching_plic(plic_id) {
294 if (ctxt == ctxt_idx)
295 return rintc_acpi_data[_plic];
296
297 ctxt++;
298 }
299
300 return NULL;
301 }
302
acpi_rintc_ext_parent_to_hartid(unsigned int plic_id,unsigned int ctxt_idx)303 unsigned long acpi_rintc_ext_parent_to_hartid(unsigned int plic_id, unsigned int ctxt_idx)
304 {
305 struct rintc_data *data = get_plic_context(plic_id, ctxt_idx);
306
307 return data ? data->hart_id : INVALID_HARTID;
308 }
309
acpi_rintc_get_plic_context(unsigned int plic_id,unsigned int ctxt_idx)310 unsigned int acpi_rintc_get_plic_context(unsigned int plic_id, unsigned int ctxt_idx)
311 {
312 struct rintc_data *data = get_plic_context(plic_id, ctxt_idx);
313
314 return data ? data->context_id : INVALID_CONTEXT;
315 }
316
acpi_rintc_index_to_hartid(u32 index)317 unsigned long acpi_rintc_index_to_hartid(u32 index)
318 {
319 return index >= nr_rintc ? INVALID_HARTID : rintc_acpi_data[index]->hart_id;
320 }
321
acpi_rintc_get_imsic_mmio_info(u32 index,struct resource * res)322 int acpi_rintc_get_imsic_mmio_info(u32 index, struct resource *res)
323 {
324 if (index >= nr_rintc)
325 return -1;
326
327 res->start = rintc_acpi_data[index]->imsic_addr;
328 res->end = res->start + rintc_acpi_data[index]->imsic_size - 1;
329 res->flags = IORESOURCE_MEM;
330 return 0;
331 }
332
riscv_intc_acpi_match(union acpi_subtable_headers * header,const unsigned long end)333 static int __init riscv_intc_acpi_match(union acpi_subtable_headers *header,
334 const unsigned long end)
335 {
336 return 0;
337 }
338
riscv_intc_acpi_init(union acpi_subtable_headers * header,const unsigned long end)339 static int __init riscv_intc_acpi_init(union acpi_subtable_headers *header,
340 const unsigned long end)
341 {
342 struct acpi_madt_rintc *rintc;
343 struct fwnode_handle *fn;
344 int count;
345 int rc;
346
347 if (!rintc_acpi_data) {
348 count = acpi_table_parse_madt(ACPI_MADT_TYPE_RINTC, riscv_intc_acpi_match, 0);
349 if (count <= 0)
350 return -EINVAL;
351
352 rintc_acpi_data = kcalloc(count, sizeof(*rintc_acpi_data), GFP_KERNEL);
353 if (!rintc_acpi_data)
354 return -ENOMEM;
355 }
356
357 rintc = (struct acpi_madt_rintc *)header;
358 rintc_acpi_data[nr_rintc] = kzalloc(sizeof(*rintc_acpi_data[0]), GFP_KERNEL);
359 if (!rintc_acpi_data[nr_rintc])
360 return -ENOMEM;
361
362 rintc_acpi_data[nr_rintc]->ext_intc_id = rintc->ext_intc_id;
363 rintc_acpi_data[nr_rintc]->hart_id = rintc->hart_id;
364 rintc_acpi_data[nr_rintc]->imsic_addr = rintc->imsic_addr;
365 rintc_acpi_data[nr_rintc]->imsic_size = rintc->imsic_size;
366 nr_rintc++;
367
368 /*
369 * The ACPI MADT will have one INTC for each CPU (or HART)
370 * so riscv_intc_acpi_init() function will be called once
371 * for each INTC. We only do INTC initialization
372 * for the INTC belonging to the boot CPU (or boot HART).
373 */
374 if (riscv_hartid_to_cpuid(rintc->hart_id) != smp_processor_id())
375 return 0;
376
377 fn = irq_domain_alloc_named_fwnode("RISCV-INTC");
378 if (!fn) {
379 pr_err("unable to allocate INTC FW node\n");
380 return -ENOMEM;
381 }
382
383 rc = riscv_intc_init_common(fn, &riscv_intc_chip);
384 if (rc)
385 irq_domain_free_fwnode(fn);
386 else
387 acpi_set_irq_model(ACPI_IRQ_MODEL_RINTC, riscv_acpi_get_gsi_domain_id);
388
389 return rc;
390 }
391
392 IRQCHIP_ACPI_DECLARE(riscv_intc, ACPI_MADT_TYPE_RINTC, NULL,
393 ACPI_MADT_RINTC_VERSION_V1, riscv_intc_acpi_init);
394 #endif
395