Debian Codegraph @

drivers/irqchip/irq-gic-v5.c

raw
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2024-2025 ARM Limited, All Rights Reserved.
4 */
5
6 #define pr_fmt(fmt) "GICv5: " fmt
7
8 #include <linux/acpi_iort.h>
9 #include <linux/cpuhotplug.h>
10 #include <linux/idr.h>
11 #include <linux/irqdomain.h>
12 #include <linux/slab.h>
13 #include <linux/wordpart.h>
14
15 #include <linux/irqchip.h>
16 #include <linux/irqchip/arm-gic-v5.h>
17 #include <linux/irqchip/arm-vgic-info.h>
18
19 #include <asm/cpufeature.h>
20 #include <asm/exception.h>
21
22 static u8 pri_bits __ro_after_init = 5;
23
24 #define GICV5_IRQ_PRI_MASK 0x1f
25 #define GICV5_IRQ_PRI_MI (GICV5_IRQ_PRI_MASK & GENMASK(4, 5 - pri_bits))
26
27 #define PPI_NR 128
28
29 static bool gicv5_cpuif_has_gcie(void)
30 {
31 return this_cpu_has_cap(ARM64_HAS_GICV5_CPUIF);
32 }
33
34 struct gicv5_chip_data gicv5_global_data __read_mostly;
35
36 static DEFINE_IDA(lpi_ida);
37 static u32 num_lpis __ro_after_init;
38
39 void __init gicv5_init_lpis(u32 lpis)
40 {
41 num_lpis = lpis;
42 }
43
44 void __init gicv5_deinit_lpis(void)
45 {
46 num_lpis = 0;
47 }
48
49 static int alloc_lpi(void)
50 {
51 if (!num_lpis)
52 return -ENOSPC;
53
54 return ida_alloc_max(&lpi_ida, num_lpis - 1, GFP_KERNEL);
55 }
56
57 static void release_lpi(u32 lpi)
58 {
59 ida_free(&lpi_ida, lpi);
60 }
61
62 static void gicv5_ppi_priority_init(void)
63 {
64 write_sysreg_s(REPEAT_BYTE(GICV5_IRQ_PRI_MI), SYS_ICC_PPI_PRIORITYR0_EL1);
65 write_sysreg_s(REPEAT_BYTE(GICV5_IRQ_PRI_MI), SYS_ICC_PPI_PRIORITYR1_EL1);
66 write_sysreg_s(REPEAT_BYTE(GICV5_IRQ_PRI_MI), SYS_ICC_PPI_PRIORITYR2_EL1);
67 write_sysreg_s(REPEAT_BYTE(GICV5_IRQ_PRI_MI), SYS_ICC_PPI_PRIORITYR3_EL1);
68 write_sysreg_s(REPEAT_BYTE(GICV5_IRQ_PRI_MI), SYS_ICC_PPI_PRIORITYR4_EL1);
69 write_sysreg_s(REPEAT_BYTE(GICV5_IRQ_PRI_MI), SYS_ICC_PPI_PRIORITYR5_EL1);
70 write_sysreg_s(REPEAT_BYTE(GICV5_IRQ_PRI_MI), SYS_ICC_PPI_PRIORITYR6_EL1);
71 write_sysreg_s(REPEAT_BYTE(GICV5_IRQ_PRI_MI), SYS_ICC_PPI_PRIORITYR7_EL1);
72 write_sysreg_s(REPEAT_BYTE(GICV5_IRQ_PRI_MI), SYS_ICC_PPI_PRIORITYR8_EL1);
73 write_sysreg_s(REPEAT_BYTE(GICV5_IRQ_PRI_MI), SYS_ICC_PPI_PRIORITYR9_EL1);
74 write_sysreg_s(REPEAT_BYTE(GICV5_IRQ_PRI_MI), SYS_ICC_PPI_PRIORITYR10_EL1);
75 write_sysreg_s(REPEAT_BYTE(GICV5_IRQ_PRI_MI), SYS_ICC_PPI_PRIORITYR11_EL1);
76 write_sysreg_s(REPEAT_BYTE(GICV5_IRQ_PRI_MI), SYS_ICC_PPI_PRIORITYR12_EL1);
77 write_sysreg_s(REPEAT_BYTE(GICV5_IRQ_PRI_MI), SYS_ICC_PPI_PRIORITYR13_EL1);
78 write_sysreg_s(REPEAT_BYTE(GICV5_IRQ_PRI_MI), SYS_ICC_PPI_PRIORITYR14_EL1);
79 write_sysreg_s(REPEAT_BYTE(GICV5_IRQ_PRI_MI), SYS_ICC_PPI_PRIORITYR15_EL1);
80
81 /*
82 * Context syncronization required to make sure system register writes
83 * effects are synchronised.
84 */
85 isb();
86 }
87
88 static void gicv5_hwirq_init(irq_hw_number_t hwirq, u8 priority, u8 hwirq_type)
89 {
90 u64 cdpri, cdaff;
91 u16 iaffid;
92 int ret;
93
94 if (hwirq_type == GICV5_HWIRQ_TYPE_LPI || hwirq_type == GICV5_HWIRQ_TYPE_SPI) {
95 cdpri = FIELD_PREP(GICV5_GIC_CDPRI_PRIORITY_MASK, priority) |
96 FIELD_PREP(GICV5_GIC_CDPRI_TYPE_MASK, hwirq_type) |
97 FIELD_PREP(GICV5_GIC_CDPRI_ID_MASK, hwirq);
98 gic_insn(cdpri, CDPRI);
99
100 ret = gicv5_irs_cpu_to_iaffid(smp_processor_id(), &iaffid);
101
102 if (WARN_ON_ONCE(ret))
103 return;
104
105 cdaff = FIELD_PREP(GICV5_GIC_CDAFF_IAFFID_MASK, iaffid) |
106 FIELD_PREP(GICV5_GIC_CDAFF_TYPE_MASK, hwirq_type) |
107 FIELD_PREP(GICV5_GIC_CDAFF_ID_MASK, hwirq);
108 gic_insn(cdaff, CDAFF);
109 }
110 }
111
112 static void gicv5_ppi_irq_mask(struct irq_data *d)
113 {
114 u64 hwirq_id_bit = BIT_ULL(d->hwirq % 64);
115
116 if (d->hwirq < 64)
117 sysreg_clear_set_s(SYS_ICC_PPI_ENABLER0_EL1, hwirq_id_bit, 0);
118 else
119 sysreg_clear_set_s(SYS_ICC_PPI_ENABLER1_EL1, hwirq_id_bit, 0);
120
121 /*
122 * We must ensure that the disable takes effect immediately to
123 * guarantee that the lazy-disabled IRQ mechanism works.
124 * A context synchronization event is required to guarantee it.
125 * Reference: I_ZLTKB/R_YRGMH GICv5 specification - section 2.9.1.
126 */
127 isb();
128 }
129
130 static void gicv5_iri_irq_mask(struct irq_data *d, u8 hwirq_type)
131 {
132 u64 cddis;
133
134 cddis = FIELD_PREP(GICV5_GIC_CDDIS_ID_MASK, d->hwirq) |
135 FIELD_PREP(GICV5_GIC_CDDIS_TYPE_MASK, hwirq_type);
136
137 gic_insn(cddis, CDDIS);
138 /*
139 * We must make sure that GIC CDDIS write effects are propagated
140 * immediately to make sure the disable takes effect to guarantee
141 * that the lazy-disabled IRQ mechanism works.
142 * Rule R_XCLJC states that the effects of a GIC system instruction
143 * complete in finite time.
144 * The GSB ensures completion of the GIC instruction and prevents
145 * loads, stores and GIC instructions from executing part of their
146 * functionality before the GSB SYS.
147 */
148 gsb_sys();
149 }
150
151 static void gicv5_spi_irq_mask(struct irq_data *d)
152 {
153 gicv5_iri_irq_mask(d, GICV5_HWIRQ_TYPE_SPI);
154 }
155
156 static void gicv5_lpi_irq_mask(struct irq_data *d)
157 {
158 gicv5_iri_irq_mask(d, GICV5_HWIRQ_TYPE_LPI);
159 }
160
161 static void gicv5_ppi_irq_unmask(struct irq_data *d)
162 {
163 u64 hwirq_id_bit = BIT_ULL(d->hwirq % 64);
164
165 if (d->hwirq < 64)
166 sysreg_clear_set_s(SYS_ICC_PPI_ENABLER0_EL1, 0, hwirq_id_bit);
167 else
168 sysreg_clear_set_s(SYS_ICC_PPI_ENABLER1_EL1, 0, hwirq_id_bit);
169 /*
170 * We must ensure that the enable takes effect in finite time - a
171 * context synchronization event is required to guarantee it, we
172 * can not take for granted that would happen (eg a core going straight
173 * into idle after enabling a PPI).
174 * Reference: I_ZLTKB/R_YRGMH GICv5 specification - section 2.9.1.
175 */
176 isb();
177 }
178
179 static void gicv5_iri_irq_unmask(struct irq_data *d, u8 hwirq_type)
180 {
181 u64 cden;
182
183 cden = FIELD_PREP(GICV5_GIC_CDEN_ID_MASK, d->hwirq) |
184 FIELD_PREP(GICV5_GIC_CDEN_TYPE_MASK, hwirq_type);
185 /*
186 * Rule R_XCLJC states that the effects of a GIC system instruction
187 * complete in finite time and that's the only requirement when
188 * unmasking an SPI/LPI IRQ.
189 */
190 gic_insn(cden, CDEN);
191 }
192
193 static void gicv5_spi_irq_unmask(struct irq_data *d)
194 {
195 gicv5_iri_irq_unmask(d, GICV5_HWIRQ_TYPE_SPI);
196 }
197
198 static void gicv5_lpi_irq_unmask(struct irq_data *d)
199 {
200 gicv5_iri_irq_unmask(d, GICV5_HWIRQ_TYPE_LPI);
201 }
202
203 static void gicv5_hwirq_eoi(u32 hwirq_id, u8 hwirq_type)
204 {
205 u64 cddi;
206
207 cddi = FIELD_PREP(GICV5_GIC_CDDI_ID_MASK, hwirq_id) |
208 FIELD_PREP(GICV5_GIC_CDDI_TYPE_MASK, hwirq_type);
209
210 gic_insn(cddi, CDDI);
211
212 gic_insn(0, CDEOI);
213 }
214
215 static void gicv5_ppi_irq_eoi(struct irq_data *d)
216 {
217 /* Skip deactivate for forwarded PPI interrupts */
218 if (irqd_is_forwarded_to_vcpu(d)) {
219 gic_insn(0, CDEOI);
220 return;
221 }
222
223 gicv5_hwirq_eoi(d->hwirq, GICV5_HWIRQ_TYPE_PPI);
224 }
225
226 static void gicv5_spi_irq_eoi(struct irq_data *d)
227 {
228 gicv5_hwirq_eoi(d->hwirq, GICV5_HWIRQ_TYPE_SPI);
229 }
230
231 static void gicv5_lpi_irq_eoi(struct irq_data *d)
232 {
233 gicv5_hwirq_eoi(d->hwirq, GICV5_HWIRQ_TYPE_LPI);
234 }
235
236 static int gicv5_iri_irq_set_affinity(struct irq_data *d,
237 const struct cpumask *mask_val,
238 bool force, u8 hwirq_type)
239 {
240 int ret, cpuid;
241 u16 iaffid;
242 u64 cdaff;
243
244 if (force)
245 cpuid = cpumask_first(mask_val);
246 else
247 cpuid = cpumask_any_and(mask_val, cpu_online_mask);
248
249 ret = gicv5_irs_cpu_to_iaffid(cpuid, &iaffid);
250 if (ret)
251 return ret;
252
253 cdaff = FIELD_PREP(GICV5_GIC_CDAFF_IAFFID_MASK, iaffid) |
254 FIELD_PREP(GICV5_GIC_CDAFF_TYPE_MASK, hwirq_type) |
255 FIELD_PREP(GICV5_GIC_CDAFF_ID_MASK, d->hwirq);
256 gic_insn(cdaff, CDAFF);
257
258 irq_data_update_effective_affinity(d, cpumask_of(cpuid));
259
260 return IRQ_SET_MASK_OK_DONE;
261 }
262
263 static int gicv5_spi_irq_set_affinity(struct irq_data *d,
264 const struct cpumask *mask_val,
265 bool force)
266 {
267 return gicv5_iri_irq_set_affinity(d, mask_val, force,
268 GICV5_HWIRQ_TYPE_SPI);
269 }
270
271 static int gicv5_lpi_irq_set_affinity(struct irq_data *d,
272 const struct cpumask *mask_val,
273 bool force)
274 {
275 return gicv5_iri_irq_set_affinity(d, mask_val, force,
276 GICV5_HWIRQ_TYPE_LPI);
277 }
278
279 enum ppi_reg {
280 PPI_PENDING,
281 PPI_ACTIVE,
282 PPI_HM
283 };
284
285 static __always_inline u64 read_ppi_sysreg_s(unsigned int irq,
286 const enum ppi_reg which)
287 {
288 switch (which) {
289 case PPI_PENDING:
290 return irq < 64 ? read_sysreg_s(SYS_ICC_PPI_SPENDR0_EL1) :
291 read_sysreg_s(SYS_ICC_PPI_SPENDR1_EL1);
292 case PPI_ACTIVE:
293 return irq < 64 ? read_sysreg_s(SYS_ICC_PPI_SACTIVER0_EL1) :
294 read_sysreg_s(SYS_ICC_PPI_SACTIVER1_EL1);
295 case PPI_HM:
296 return irq < 64 ? read_sysreg_s(SYS_ICC_PPI_HMR0_EL1) :
297 read_sysreg_s(SYS_ICC_PPI_HMR1_EL1);
298 default:
299 BUILD_BUG_ON(1);
300 }
301 }
302
303 static __always_inline void write_ppi_sysreg_s(unsigned int irq, bool set,
304 const enum ppi_reg which)
305 {
306 u64 bit = BIT_ULL(irq % 64);
307
308 switch (which) {
309 case PPI_PENDING:
310 if (set) {
311 if (irq < 64)
312 write_sysreg_s(bit, SYS_ICC_PPI_SPENDR0_EL1);
313 else
314 write_sysreg_s(bit, SYS_ICC_PPI_SPENDR1_EL1);
315 } else {
316 if (irq < 64)
317 write_sysreg_s(bit, SYS_ICC_PPI_CPENDR0_EL1);
318 else
319 write_sysreg_s(bit, SYS_ICC_PPI_CPENDR1_EL1);
320 }
321 return;
322 case PPI_ACTIVE:
323 if (set) {
324 if (irq < 64)
325 write_sysreg_s(bit, SYS_ICC_PPI_SACTIVER0_EL1);
326 else
327 write_sysreg_s(bit, SYS_ICC_PPI_SACTIVER1_EL1);
328 } else {
329 if (irq < 64)
330 write_sysreg_s(bit, SYS_ICC_PPI_CACTIVER0_EL1);
331 else
332 write_sysreg_s(bit, SYS_ICC_PPI_CACTIVER1_EL1);
333 }
334 return;
335 default:
336 BUILD_BUG_ON(1);
337 }
338 }
339
340 static int gicv5_ppi_irq_get_irqchip_state(struct irq_data *d,
341 enum irqchip_irq_state which,
342 bool *state)
343 {
344 u64 hwirq_id_bit = BIT_ULL(d->hwirq % 64);
345
346 switch (which) {
347 case IRQCHIP_STATE_PENDING:
348 *state = !!(read_ppi_sysreg_s(d->hwirq, PPI_PENDING) & hwirq_id_bit);
349 return 0;
350 case IRQCHIP_STATE_ACTIVE:
351 *state = !!(read_ppi_sysreg_s(d->hwirq, PPI_ACTIVE) & hwirq_id_bit);
352 return 0;
353 default:
354 pr_debug("Unexpected PPI irqchip state\n");
355 return -EINVAL;
356 }
357 }
358
359 static int gicv5_iri_irq_get_irqchip_state(struct irq_data *d,
360 enum irqchip_irq_state which,
361 bool *state, u8 hwirq_type)
362 {
363 u64 icsr, cdrcfg;
364
365 cdrcfg = d->hwirq | FIELD_PREP(GICV5_GIC_CDRCFG_TYPE_MASK, hwirq_type);
366
367 gic_insn(cdrcfg, CDRCFG);
368 isb();
369 icsr = read_sysreg_s(SYS_ICC_ICSR_EL1);
370
371 if (FIELD_GET(ICC_ICSR_EL1_F, icsr)) {
372 pr_err("ICSR_EL1 is invalid\n");
373 return -EINVAL;
374 }
375
376 switch (which) {
377 case IRQCHIP_STATE_PENDING:
378 *state = !!(FIELD_GET(ICC_ICSR_EL1_Pending, icsr));
379 return 0;
380
381 case IRQCHIP_STATE_ACTIVE:
382 *state = !!(FIELD_GET(ICC_ICSR_EL1_Active, icsr));
383 return 0;
384
385 default:
386 pr_debug("Unexpected irqchip_irq_state\n");
387 return -EINVAL;
388 }
389 }
390
391 static int gicv5_spi_irq_get_irqchip_state(struct irq_data *d,
392 enum irqchip_irq_state which,
393 bool *state)
394 {
395 return gicv5_iri_irq_get_irqchip_state(d, which, state,
396 GICV5_HWIRQ_TYPE_SPI);
397 }
398
399 static int gicv5_lpi_irq_get_irqchip_state(struct irq_data *d,
400 enum irqchip_irq_state which,
401 bool *state)
402 {
403 return gicv5_iri_irq_get_irqchip_state(d, which, state,
404 GICV5_HWIRQ_TYPE_LPI);
405 }
406
407 static int gicv5_ppi_irq_set_irqchip_state(struct irq_data *d,
408 enum irqchip_irq_state which,
409 bool state)
410 {
411 switch (which) {
412 case IRQCHIP_STATE_PENDING:
413 write_ppi_sysreg_s(d->hwirq, state, PPI_PENDING);
414 return 0;
415 case IRQCHIP_STATE_ACTIVE:
416 write_ppi_sysreg_s(d->hwirq, state, PPI_ACTIVE);
417 return 0;
418 default:
419 pr_debug("Unexpected PPI irqchip state\n");
420 return -EINVAL;
421 }
422 }
423
424 static void gicv5_iri_irq_write_pending_state(struct irq_data *d, bool state,
425 u8 hwirq_type)
426 {
427 u64 cdpend;
428
429 cdpend = FIELD_PREP(GICV5_GIC_CDPEND_TYPE_MASK, hwirq_type) |
430 FIELD_PREP(GICV5_GIC_CDPEND_ID_MASK, d->hwirq) |
431 FIELD_PREP(GICV5_GIC_CDPEND_PENDING_MASK, state);
432
433 gic_insn(cdpend, CDPEND);
434 }
435
436 static void gicv5_spi_irq_write_pending_state(struct irq_data *d, bool state)
437 {
438 gicv5_iri_irq_write_pending_state(d, state, GICV5_HWIRQ_TYPE_SPI);
439 }
440
441 static void gicv5_lpi_irq_write_pending_state(struct irq_data *d, bool state)
442 {
443 gicv5_iri_irq_write_pending_state(d, state, GICV5_HWIRQ_TYPE_LPI);
444 }
445
446 static int gicv5_spi_irq_set_irqchip_state(struct irq_data *d,
447 enum irqchip_irq_state which,
448 bool state)
449 {
450 switch (which) {
451 case IRQCHIP_STATE_PENDING:
452 gicv5_spi_irq_write_pending_state(d, state);
453 break;
454 default:
455 pr_debug("Unexpected irqchip_irq_state\n");
456 return -EINVAL;
457 }
458
459 return 0;
460 }
461
462 static int gicv5_lpi_irq_set_irqchip_state(struct irq_data *d,
463 enum irqchip_irq_state which,
464 bool state)
465 {
466 switch (which) {
467 case IRQCHIP_STATE_PENDING:
468 gicv5_lpi_irq_write_pending_state(d, state);
469 break;
470
471 default:
472 pr_debug("Unexpected irqchip_irq_state\n");
473 return -EINVAL;
474 }
475
476 return 0;
477 }
478
479 static int gicv5_spi_irq_retrigger(struct irq_data *data)
480 {
481 return !gicv5_spi_irq_set_irqchip_state(data, IRQCHIP_STATE_PENDING,
482 true);
483 }
484
485 static int gicv5_lpi_irq_retrigger(struct irq_data *data)
486 {
487 return !gicv5_lpi_irq_set_irqchip_state(data, IRQCHIP_STATE_PENDING,
488 true);
489 }
490
491 static void gicv5_ipi_send_single(struct irq_data *d, unsigned int cpu)
492 {
493 /* Mark the LPI pending */
494 irq_chip_retrigger_hierarchy(d);
495 }
496
497 static bool gicv5_ppi_irq_is_level(irq_hw_number_t hwirq)
498 {
499 u64 bit = BIT_ULL(hwirq % 64);
500
501 return !!(read_ppi_sysreg_s(hwirq, PPI_HM) & bit);
502 }
503
504 static int gicv5_ppi_irq_set_vcpu_affinity(struct irq_data *d, void *vcpu)
505 {
506 if (vcpu)
507 irqd_set_forwarded_to_vcpu(d);
508 else
509 irqd_clr_forwarded_to_vcpu(d);
510
511 return 0;
512 }
513
514 static const struct irq_chip gicv5_ppi_irq_chip = {
515 .name = "GICv5-PPI",
516 .irq_mask = gicv5_ppi_irq_mask,
517 .irq_unmask = gicv5_ppi_irq_unmask,
518 .irq_eoi = gicv5_ppi_irq_eoi,
519 .irq_get_irqchip_state = gicv5_ppi_irq_get_irqchip_state,
520 .irq_set_irqchip_state = gicv5_ppi_irq_set_irqchip_state,
521 .irq_set_vcpu_affinity = gicv5_ppi_irq_set_vcpu_affinity,
522 .flags = IRQCHIP_SKIP_SET_WAKE |
523 IRQCHIP_MASK_ON_SUSPEND,
524 };
525
526 static const struct irq_chip gicv5_spi_irq_chip = {
527 .name = "GICv5-SPI",
528 .irq_mask = gicv5_spi_irq_mask,
529 .irq_unmask = gicv5_spi_irq_unmask,
530 .irq_eoi = gicv5_spi_irq_eoi,
531 .irq_set_type = gicv5_spi_irq_set_type,
532 .irq_set_affinity = gicv5_spi_irq_set_affinity,
533 .irq_retrigger = gicv5_spi_irq_retrigger,
534 .irq_get_irqchip_state = gicv5_spi_irq_get_irqchip_state,
535 .irq_set_irqchip_state = gicv5_spi_irq_set_irqchip_state,
536 .flags = IRQCHIP_SET_TYPE_MASKED |
537 IRQCHIP_SKIP_SET_WAKE |
538 IRQCHIP_MASK_ON_SUSPEND,
539 };
540
541 static const struct irq_chip gicv5_lpi_irq_chip = {
542 .name = "GICv5-LPI",
543 .irq_mask = gicv5_lpi_irq_mask,
544 .irq_unmask = gicv5_lpi_irq_unmask,
545 .irq_eoi = gicv5_lpi_irq_eoi,
546 .irq_set_affinity = gicv5_lpi_irq_set_affinity,
547 .irq_retrigger = gicv5_lpi_irq_retrigger,
548 .irq_get_irqchip_state = gicv5_lpi_irq_get_irqchip_state,
549 .irq_set_irqchip_state = gicv5_lpi_irq_set_irqchip_state,
550 .flags = IRQCHIP_SKIP_SET_WAKE |
551 IRQCHIP_MASK_ON_SUSPEND,
552 };
553
554 static const struct irq_chip gicv5_ipi_irq_chip = {
555 .name = "GICv5-IPI",
556 .irq_mask = irq_chip_mask_parent,
557 .irq_unmask = irq_chip_unmask_parent,
558 .irq_eoi = irq_chip_eoi_parent,
559 .irq_set_affinity = irq_chip_set_affinity_parent,
560 .irq_get_irqchip_state = irq_chip_get_parent_state,
561 .irq_set_irqchip_state = irq_chip_set_parent_state,
562 .ipi_send_single = gicv5_ipi_send_single,
563 .flags = IRQCHIP_SKIP_SET_WAKE |
564 IRQCHIP_MASK_ON_SUSPEND,
565 };
566
567 static __always_inline int gicv5_irq_domain_translate(struct irq_domain *d,
568 struct irq_fwspec *fwspec,
569 irq_hw_number_t *hwirq,
570 unsigned int *type,
571 const u8 hwirq_type)
572 {
573 unsigned int hwirq_trigger;
574 u8 fwspec_irq_type;
575
576 if (is_of_node(fwspec->fwnode)) {
577
578 if (fwspec->param_count < 3)
579 return -EINVAL;
580
581 fwspec_irq_type = fwspec->param[0];
582
583 if (fwspec->param[0] != hwirq_type)
584 return -EINVAL;
585
586 *hwirq = fwspec->param[1];
587 hwirq_trigger = fwspec->param[2];
588 }
589
590 if (is_fwnode_irqchip(fwspec->fwnode)) {
591
592 if (fwspec->param_count != 2)
593 return -EINVAL;
594
595 fwspec_irq_type = FIELD_GET(GICV5_HWIRQ_TYPE, fwspec->param[0]);
596
597 if (fwspec_irq_type != hwirq_type)
598 return -EINVAL;
599
600 *hwirq = FIELD_GET(GICV5_HWIRQ_ID, fwspec->param[0]);
601 hwirq_trigger = fwspec->param[1];
602 }
603
604 switch (hwirq_type) {
605 case GICV5_HWIRQ_TYPE_PPI:
606 /*
607 * Handling mode is hardcoded for PPIs, set the type using
608 * HW reported value.
609 */
610 *type = gicv5_ppi_irq_is_level(*hwirq) ? IRQ_TYPE_LEVEL_LOW :
611 IRQ_TYPE_EDGE_RISING;
612 break;
613 case GICV5_HWIRQ_TYPE_SPI:
614 *type = hwirq_trigger & IRQ_TYPE_SENSE_MASK;
615 break;
616 default:
617 BUILD_BUG_ON(1);
618 }
619
620 return 0;
621 }
622
623 static int gicv5_irq_ppi_domain_translate(struct irq_domain *d,
624 struct irq_fwspec *fwspec,
625 irq_hw_number_t *hwirq,
626 unsigned int *type)
627 {
628 return gicv5_irq_domain_translate(d, fwspec, hwirq, type,
629 GICV5_HWIRQ_TYPE_PPI);
630 }
631
632 static int gicv5_irq_ppi_domain_alloc(struct irq_domain *domain, unsigned int virq,
633 unsigned int nr_irqs, void *arg)
634 {
635 unsigned int type = IRQ_TYPE_NONE;
636 struct irq_fwspec *fwspec = arg;
637 irq_hw_number_t hwirq;
638 int ret;
639
640 if (WARN_ON_ONCE(nr_irqs != 1))
641 return -EINVAL;
642
643 ret = gicv5_irq_ppi_domain_translate(domain, fwspec, &hwirq, &type);
644 if (ret)
645 return ret;
646
647 if (type & IRQ_TYPE_LEVEL_MASK)
648 irq_set_status_flags(virq, IRQ_LEVEL);
649
650 irq_set_percpu_devid(virq);
651 irq_domain_set_info(domain, virq, hwirq, &gicv5_ppi_irq_chip, NULL,
652 handle_percpu_devid_irq, NULL, NULL);
653
654 return 0;
655 }
656
657 static void gicv5_irq_domain_free(struct irq_domain *domain, unsigned int virq,
658 unsigned int nr_irqs)
659 {
660 struct irq_data *d;
661
662 if (WARN_ON_ONCE(nr_irqs != 1))
663 return;
664
665 d = irq_domain_get_irq_data(domain, virq);
666
667 irq_set_handler(virq, NULL);
668 irq_domain_reset_irq_data(d);
669 }
670
671 static int gicv5_irq_ppi_domain_select(struct irq_domain *d, struct irq_fwspec *fwspec,
672 enum irq_domain_bus_token bus_token)
673 {
674 u32 hwirq_type;
675
676 if (fwspec->fwnode != d->fwnode)
677 return 0;
678
679 if (is_of_node(fwspec->fwnode))
680 hwirq_type = fwspec->param[0];
681
682 if (is_fwnode_irqchip(fwspec->fwnode))
683 hwirq_type = FIELD_GET(GICV5_HWIRQ_TYPE, fwspec->param[0]);
684
685 if (hwirq_type != GICV5_HWIRQ_TYPE_PPI)
686 return 0;
687
688 return (d == gicv5_global_data.ppi_domain);
689 }
690
691 static const struct irq_domain_ops gicv5_irq_ppi_domain_ops = {
692 .translate = gicv5_irq_ppi_domain_translate,
693 .alloc = gicv5_irq_ppi_domain_alloc,
694 .free = gicv5_irq_domain_free,
695 .select = gicv5_irq_ppi_domain_select
696 };
697
698 static int gicv5_irq_spi_domain_translate(struct irq_domain *d,
699 struct irq_fwspec *fwspec,
700 irq_hw_number_t *hwirq,
701 unsigned int *type)
702 {
703 return gicv5_irq_domain_translate(d, fwspec, hwirq, type,
704 GICV5_HWIRQ_TYPE_SPI);
705 }
706
707 static int gicv5_irq_spi_domain_alloc(struct irq_domain *domain, unsigned int virq,
708 unsigned int nr_irqs, void *arg)
709 {
710 struct gicv5_irs_chip_data *chip_data;
711 unsigned int type = IRQ_TYPE_NONE;
712 struct irq_fwspec *fwspec = arg;
713 struct irq_data *irqd;
714 irq_hw_number_t hwirq;
715 int ret;
716
717 if (WARN_ON_ONCE(nr_irqs != 1))
718 return -EINVAL;
719
720 ret = gicv5_irq_spi_domain_translate(domain, fwspec, &hwirq, &type);
721 if (ret)
722 return ret;
723
724 irqd = irq_desc_get_irq_data(irq_to_desc(virq));
725 chip_data = gicv5_irs_lookup_by_spi_id(hwirq);
726
727 irq_domain_set_info(domain, virq, hwirq, &gicv5_spi_irq_chip, chip_data,
728 handle_fasteoi_irq, NULL, NULL);
729 irq_set_probe(virq);
730 irqd_set_single_target(irqd);
731
732 gicv5_hwirq_init(hwirq, GICV5_IRQ_PRI_MI, GICV5_HWIRQ_TYPE_SPI);
733
734 return 0;
735 }
736
737 static int gicv5_irq_spi_domain_select(struct irq_domain *d, struct irq_fwspec *fwspec,
738 enum irq_domain_bus_token bus_token)
739 {
740 u32 hwirq_type;
741
742 if (fwspec->fwnode != d->fwnode)
743 return 0;
744
745 if (is_of_node(fwspec->fwnode))
746 hwirq_type = fwspec->param[0];
747
748 if (is_fwnode_irqchip(fwspec->fwnode))
749 hwirq_type = FIELD_GET(GICV5_HWIRQ_TYPE, fwspec->param[0]);
750
751 if (hwirq_type != GICV5_HWIRQ_TYPE_SPI)
752 return 0;
753
754 return (d == gicv5_global_data.spi_domain);
755 }
756
757 static const struct irq_domain_ops gicv5_irq_spi_domain_ops = {
758 .translate = gicv5_irq_spi_domain_translate,
759 .alloc = gicv5_irq_spi_domain_alloc,
760 .free = gicv5_irq_domain_free,
761 .select = gicv5_irq_spi_domain_select
762 };
763
764 static void gicv5_lpi_config_reset(struct irq_data *d)
765 {
766 u64 cdhm;
767
768 /*
769 * Reset LPIs handling mode to edge by default and clear pending
770 * state to make sure we start the LPI with a clean state from
771 * previous incarnations.
772 */
773 cdhm = FIELD_PREP(GICV5_GIC_CDHM_HM_MASK, 0) |
774 FIELD_PREP(GICV5_GIC_CDHM_TYPE_MASK, GICV5_HWIRQ_TYPE_LPI) |
775 FIELD_PREP(GICV5_GIC_CDHM_ID_MASK, d->hwirq);
776 gic_insn(cdhm, CDHM);
777
778 gicv5_lpi_irq_write_pending_state(d, false);
779 }
780
781 static void gicv5_irq_lpi_domain_free(struct irq_domain *domain, unsigned int virq,
782 unsigned int nr_irqs)
783 {
784 struct irq_data *d;
785
786 for (unsigned int i = 0; i < nr_irqs; i++, virq++) {
787 d = irq_domain_get_irq_data(domain, virq);
788
789 release_lpi(d->hwirq);
790
791 irq_set_handler(virq, NULL);
792 irq_domain_reset_irq_data(d);
793 }
794 }
795
796 static int gicv5_irq_lpi_domain_alloc(struct irq_domain *domain, unsigned int virq,
797 unsigned int nr_irqs, void *arg)
798 {
799 irq_hw_number_t hwirq;
800 struct irq_data *irqd;
801 unsigned int i;
802 int ret;
803
804 for (i = 0; i < nr_irqs; i++) {
805 ret = alloc_lpi();
806 if (ret < 0)
807 goto out_free_lpis;
808 hwirq = ret;
809
810 ret = gicv5_irs_iste_alloc(hwirq);
811 if (ret < 0) {
812 /* Undo partial state first, then clean up the rest */
813 release_lpi(hwirq);
814 goto out_free_lpis;
815 }
816
817 irqd = irq_domain_get_irq_data(domain, virq + i);
818
819 irq_domain_set_info(domain, virq + i, hwirq, &gicv5_lpi_irq_chip,
820 NULL, handle_fasteoi_irq, NULL, NULL);
821 irqd_set_single_target(irqd);
822
823 gicv5_hwirq_init(hwirq, GICV5_IRQ_PRI_MI, GICV5_HWIRQ_TYPE_LPI);
824 gicv5_lpi_config_reset(irqd);
825 }
826
827 return 0;
828
829 out_free_lpis:
830 if (i)
831 gicv5_irq_lpi_domain_free(domain, virq, i);
832
833 return ret;
834 }
835
836 static const struct irq_domain_ops gicv5_irq_lpi_domain_ops = {
837 .alloc = gicv5_irq_lpi_domain_alloc,
838 .free = gicv5_irq_lpi_domain_free,
839 };
840
841 void __init gicv5_init_lpi_domain(void)
842 {
843 struct irq_domain *d;
844
845 d = irq_domain_create_tree(NULL, &gicv5_irq_lpi_domain_ops, NULL);
846 gicv5_global_data.lpi_domain = d;
847 }
848
849 void __init gicv5_free_lpi_domain(void)
850 {
851 irq_domain_remove(gicv5_global_data.lpi_domain);
852 gicv5_global_data.lpi_domain = NULL;
853 }
854
855 static int gicv5_irq_ipi_domain_alloc(struct irq_domain *domain, unsigned int virq,
856 unsigned int nr_irqs, void *arg)
857 {
858 struct irq_data *irqd;
859 int ret;
860
861 ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, arg);
862 if (ret)
863 return ret;
864
865 for (unsigned int i = 0; i < nr_irqs; i++, virq++) {
866 irqd = irq_domain_get_irq_data(domain, virq);
867
868 irq_domain_set_hwirq_and_chip(domain, virq, i,
869 &gicv5_ipi_irq_chip, NULL);
870
871 irqd_set_single_target(irqd);
872
873 irq_set_handler(virq, handle_percpu_irq);
874 }
875
876 return 0;
877 }
878
879 static void gicv5_irq_ipi_domain_free(struct irq_domain *domain, unsigned int virq,
880 unsigned int nr_irqs)
881 {
882 struct irq_data *d;
883 unsigned int i;
884
885 for (i = 0; i < nr_irqs; i++) {
886 d = irq_domain_get_irq_data(domain, virq + i);
887
888 if (!d)
889 return;
890
891 irq_set_handler(virq + i, NULL);
892 irq_domain_reset_irq_data(d);
893 }
894
895 irq_domain_free_irqs_parent(domain, virq, nr_irqs);
896 }
897
898 static const struct irq_domain_ops gicv5_irq_ipi_domain_ops = {
899 .alloc = gicv5_irq_ipi_domain_alloc,
900 .free = gicv5_irq_ipi_domain_free,
901 };
902
903 static void handle_irq_per_domain(u32 hwirq)
904 {
905 u8 hwirq_type = FIELD_GET(GICV5_HWIRQ_TYPE, hwirq);
906 u32 hwirq_id = FIELD_GET(GICV5_HWIRQ_ID, hwirq);
907 struct irq_domain *domain;
908
909 switch (hwirq_type) {
910 case GICV5_HWIRQ_TYPE_PPI:
911 domain = gicv5_global_data.ppi_domain;
912 break;
913 case GICV5_HWIRQ_TYPE_SPI:
914 domain = gicv5_global_data.spi_domain;
915 break;
916 case GICV5_HWIRQ_TYPE_LPI:
917 domain = gicv5_global_data.lpi_domain;
918 break;
919 default:
920 pr_err_once("Unknown IRQ type, bail out\n");
921 return;
922 }
923
924 if (generic_handle_domain_irq(domain, hwirq_id)) {
925 pr_err_once("Could not handle, hwirq = 0x%x", hwirq_id);
926 gicv5_hwirq_eoi(hwirq_id, hwirq_type);
927 }
928 }
929
930 static void __exception_irq_entry gicv5_handle_irq(struct pt_regs *regs)
931 {
932 bool valid;
933 u32 hwirq;
934 u64 ia;
935
936 ia = gicr_insn(CDIA);
937 valid = GICV5_GICR_CDIA_VALID(ia);
938
939 if (!valid)
940 return;
941
942 /*
943 * Ensure that the CDIA instruction effects (ie IRQ activation) are
944 * completed before handling the interrupt.
945 */
946 gsb_ack();
947
948 /*
949 * Ensure instruction ordering between an acknowledgment and subsequent
950 * instructions in the IRQ handler using an ISB.
951 */
952 isb();
953
954 hwirq = FIELD_GET(GICV5_HWIRQ_INTID, ia);
955
956 handle_irq_per_domain(hwirq);
957 }
958
959 static void gicv5_cpu_disable_interrupts(void)
960 {
961 u64 cr0;
962
963 cr0 = FIELD_PREP(ICC_CR0_EL1_EN, 0);
964 write_sysreg_s(cr0, SYS_ICC_CR0_EL1);
965 }
966
967 static void gicv5_cpu_enable_interrupts(void)
968 {
969 u64 cr0, pcr;
970
971 write_sysreg_s(0, SYS_ICC_PPI_ENABLER0_EL1);
972 write_sysreg_s(0, SYS_ICC_PPI_ENABLER1_EL1);
973
974 gicv5_ppi_priority_init();
975
976 pcr = FIELD_PREP(ICC_PCR_EL1_PRIORITY, GICV5_IRQ_PRI_MI);
977 write_sysreg_s(pcr, SYS_ICC_PCR_EL1);
978
979 cr0 = FIELD_PREP(ICC_CR0_EL1_EN, 1);
980 write_sysreg_s(cr0, SYS_ICC_CR0_EL1);
981 }
982
983 static int base_ipi_virq;
984
985 static int gicv5_starting_cpu(unsigned int cpu)
986 {
987 if (WARN(!gicv5_cpuif_has_gcie(),
988 "GICv5 system components present but CPU does not have FEAT_GCIE"))
989 return -ENODEV;
990
991 gicv5_cpu_enable_interrupts();
992
993 return gicv5_irs_register_cpu(cpu);
994 }
995
996 static void __init gicv5_smp_init(void)
997 {
998 unsigned int num_ipis = GICV5_IPIS_PER_CPU * nr_cpu_ids;
999
1000 cpuhp_setup_state_nocalls(CPUHP_AP_IRQ_GIC_STARTING,
1001 "irqchip/arm/gicv5:starting",
1002 gicv5_starting_cpu, NULL);
1003
1004 base_ipi_virq = irq_domain_alloc_irqs(gicv5_global_data.ipi_domain,
1005 num_ipis, NUMA_NO_NODE, NULL);
1006 if (WARN(base_ipi_virq <= 0, "IPI IRQ allocation was not successful"))
1007 return;
1008
1009 set_smp_ipi_range_percpu(base_ipi_virq, GICV5_IPIS_PER_CPU, nr_cpu_ids);
1010 }
1011
1012 static void __init gicv5_free_domains(void)
1013 {
1014 if (gicv5_global_data.ppi_domain)
1015 irq_domain_remove(gicv5_global_data.ppi_domain);
1016 if (gicv5_global_data.spi_domain)
1017 irq_domain_remove(gicv5_global_data.spi_domain);
1018 if (gicv5_global_data.ipi_domain)
1019 irq_domain_remove(gicv5_global_data.ipi_domain);
1020
1021 gicv5_global_data.ppi_domain = NULL;
1022 gicv5_global_data.spi_domain = NULL;
1023 gicv5_global_data.ipi_domain = NULL;
1024 }
1025
1026 static int __init gicv5_init_domains(struct fwnode_handle *handle)
1027 {
1028 u32 spi_count = gicv5_global_data.global_spi_count;
1029 struct irq_domain *d;
1030
1031 d = irq_domain_create_linear(handle, PPI_NR, &gicv5_irq_ppi_domain_ops, NULL);
1032 if (!d)
1033 return -ENOMEM;
1034
1035 irq_domain_update_bus_token(d, DOMAIN_BUS_WIRED);
1036 gicv5_global_data.ppi_domain = d;
1037
1038 if (spi_count) {
1039 d = irq_domain_create_linear(handle, spi_count,
1040 &gicv5_irq_spi_domain_ops, NULL);
1041
1042 if (!d) {
1043 gicv5_free_domains();
1044 return -ENOMEM;
1045 }
1046
1047 gicv5_global_data.spi_domain = d;
1048 irq_domain_update_bus_token(d, DOMAIN_BUS_WIRED);
1049 }
1050
1051 if (!WARN(!gicv5_global_data.lpi_domain,
1052 "LPI domain uninitialized, can't set up IPIs")) {
1053 d = irq_domain_create_hierarchy(gicv5_global_data.lpi_domain,
1054 0, GICV5_IPIS_PER_CPU * nr_cpu_ids,
1055 NULL, &gicv5_irq_ipi_domain_ops,
1056 NULL);
1057
1058 if (!d) {
1059 gicv5_free_domains();
1060 return -ENOMEM;
1061 }
1062 gicv5_global_data.ipi_domain = d;
1063 }
1064 gicv5_global_data.fwnode = handle;
1065
1066 return 0;
1067 }
1068
1069 static void gicv5_set_cpuif_pribits(void)
1070 {
1071 u64 icc_idr0 = read_sysreg_s(SYS_ICC_IDR0_EL1);
1072
1073 switch (FIELD_GET(ICC_IDR0_EL1_PRI_BITS, icc_idr0)) {
1074 case ICC_IDR0_EL1_PRI_BITS_4BITS:
1075 gicv5_global_data.cpuif_pri_bits = 4;
1076 break;
1077 case ICC_IDR0_EL1_PRI_BITS_5BITS:
1078 gicv5_global_data.cpuif_pri_bits = 5;
1079 break;
1080 default:
1081 pr_err("Unexpected ICC_IDR0_EL1_PRI_BITS value, default to 4");
1082 gicv5_global_data.cpuif_pri_bits = 4;
1083 break;
1084 }
1085 }
1086
1087 static void gicv5_set_cpuif_idbits(void)
1088 {
1089 u32 icc_idr0 = read_sysreg_s(SYS_ICC_IDR0_EL1);
1090
1091 switch (FIELD_GET(ICC_IDR0_EL1_ID_BITS, icc_idr0)) {
1092 case ICC_IDR0_EL1_ID_BITS_16BITS:
1093 gicv5_global_data.cpuif_id_bits = 16;
1094 break;
1095 case ICC_IDR0_EL1_ID_BITS_24BITS:
1096 gicv5_global_data.cpuif_id_bits = 24;
1097 break;
1098 default:
1099 pr_err("Unexpected ICC_IDR0_EL1_ID_BITS value, default to 16");
1100 gicv5_global_data.cpuif_id_bits = 16;
1101 break;
1102 }
1103 }
1104
1105 #ifdef CONFIG_KVM
1106 static struct gic_kvm_info gic_v5_kvm_info __initdata;
1107
1108 static void __init gic_of_setup_kvm_info(struct device_node *node)
1109 {
1110 /*
1111 * If we don't have native GICv5 virtualisation support, then
1112 * we also don't have FEAT_GCIE_LEGACY - the architecture
1113 * forbids this combination.
1114 */
1115 if (!gicv5_global_data.virt_capable) {
1116 pr_info("GIC implementation is not virtualization capable\n");
1117 return;
1118 }
1119
1120 gic_v5_kvm_info.type = GIC_V5;
1121
1122 /* GIC Virtual CPU interface maintenance interrupt */
1123 gic_v5_kvm_info.no_maint_irq_mask = false;
1124 gic_v5_kvm_info.maint_irq = irq_of_parse_and_map(node, 0);
1125 if (!gic_v5_kvm_info.maint_irq) {
1126 pr_warn("cannot find GICv5 virtual CPU interface maintenance interrupt\n");
1127 return;
1128 }
1129
1130 vgic_set_kvm_info(&gic_v5_kvm_info);
1131 }
1132 #else
1133 static inline void __init gic_of_setup_kvm_info(struct device_node *node)
1134 {
1135 }
1136 #endif // CONFIG_KVM
1137
1138 static int __init gicv5_init_common(struct fwnode_handle *parent_domain)
1139 {
1140 int ret = gicv5_init_domains(parent_domain);
1141 if (ret)
1142 return ret;
1143
1144 gicv5_set_cpuif_pribits();
1145 gicv5_set_cpuif_idbits();
1146
1147 pri_bits = min_not_zero(gicv5_global_data.cpuif_pri_bits,
1148 gicv5_global_data.irs_pri_bits);
1149
1150 ret = gicv5_starting_cpu(smp_processor_id());
1151 if (ret)
1152 goto out_dom;
1153
1154 ret = set_handle_irq(gicv5_handle_irq);
1155 if (ret)
1156 goto out_int;
1157
1158 ret = gicv5_irs_enable();
1159 if (ret)
1160 goto out_int;
1161
1162 gicv5_smp_init();
1163
1164 gicv5_irs_its_probe();
1165 return 0;
1166
1167 out_int:
1168 gicv5_cpu_disable_interrupts();
1169 out_dom:
1170 gicv5_free_domains();
1171 return ret;
1172 }
1173
1174 static int __init gicv5_of_init(struct device_node *node, struct device_node *parent)
1175 {
1176 int ret = gicv5_irs_of_probe(node);
1177 if (ret)
1178 return ret;
1179
1180 ret = gicv5_init_common(of_fwnode_handle(node));
1181 if (ret)
1182 goto out_irs;
1183
1184 gic_of_setup_kvm_info(node);
1185
1186 return 0;
1187 out_irs:
1188 gicv5_irs_remove();
1189
1190 return ret;
1191 }
1192 IRQCHIP_DECLARE(gic_v5, "arm,gic-v5", gicv5_of_init);
1193
1194 #ifdef CONFIG_ACPI
1195 static bool __init acpi_validate_gic_table(struct acpi_subtable_header *header,
1196 struct acpi_probe_entry *ape)
1197 {
1198 struct acpi_madt_gicv5_irs *irs = (struct acpi_madt_gicv5_irs *)header;
1199
1200 return (irs->version == ape->driver_data);
1201 }
1202
1203 static struct fwnode_handle *gsi_domain_handle;
1204
1205 static struct fwnode_handle *gic_v5_get_gsi_domain_id(u32 gsi)
1206 {
1207 if (FIELD_GET(GICV5_GSI_IC_TYPE, gsi) == GICV5_GSI_IWB_TYPE)
1208 return iort_iwb_handle(FIELD_GET(GICV5_GSI_IWB_FRAME_ID, gsi));
1209
1210 return gsi_domain_handle;
1211 }
1212
1213 static int __init gic_acpi_init(union acpi_subtable_headers *header, const unsigned long end)
1214 {
1215 struct acpi_madt_gicv5_irs *irs = (struct acpi_madt_gicv5_irs *)header;
1216 int ret;
1217
1218 if (gsi_domain_handle)
1219 return 0;
1220
1221 gsi_domain_handle = irq_domain_alloc_fwnode(&irs->config_base_address);
1222 if (!gsi_domain_handle)
1223 return -ENOMEM;
1224
1225 ret = gicv5_irs_acpi_probe();
1226 if (ret)
1227 goto out_fwnode;
1228
1229 ret = gicv5_init_common(gsi_domain_handle);
1230 if (ret)
1231 goto out_irs;
1232
1233 acpi_set_irq_model(ACPI_IRQ_MODEL_GIC_V5, gic_v5_get_gsi_domain_id);
1234
1235 return 0;
1236
1237 out_irs:
1238 gicv5_irs_remove();
1239 out_fwnode:
1240 irq_domain_free_fwnode(gsi_domain_handle);
1241 return ret;
1242 }
1243 IRQCHIP_ACPI_DECLARE(gic_v5, ACPI_MADT_TYPE_GICV5_IRS,
1244 acpi_validate_gic_table, ACPI_MADT_GIC_VERSION_V5,
1245 gic_acpi_init);
1246 #endif