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