xref: /linux/drivers/irqchip/irq-gic-v3.c (revision e04e2b760ddbe3d7b283a05898c3a029085cd8cd)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2013-2017 ARM Limited, All Rights Reserved.
4  * Author: Marc Zyngier <marc.zyngier@arm.com>
5  */
6 
7 #define pr_fmt(fmt)	"GICv3: " fmt
8 
9 #include <linux/acpi.h>
10 #include <linux/cpu.h>
11 #include <linux/cpu_pm.h>
12 #include <linux/delay.h>
13 #include <linux/interrupt.h>
14 #include <linux/irqdomain.h>
15 #include <linux/kernel.h>
16 #include <linux/kstrtox.h>
17 #include <linux/of.h>
18 #include <linux/of_address.h>
19 #include <linux/of_irq.h>
20 #include <linux/percpu.h>
21 #include <linux/refcount.h>
22 #include <linux/slab.h>
23 #include <linux/iopoll.h>
24 
25 #include <linux/irqchip.h>
26 #include <linux/irqchip/arm-gic-common.h>
27 #include <linux/irqchip/arm-gic-v3.h>
28 #include <linux/irqchip/arm-gic-v3-prio.h>
29 #include <linux/irqchip/irq-partition-percpu.h>
30 #include <linux/bitfield.h>
31 #include <linux/bits.h>
32 #include <linux/arm-smccc.h>
33 
34 #include <asm/cputype.h>
35 #include <asm/exception.h>
36 #include <asm/smp_plat.h>
37 #include <asm/virt.h>
38 
39 #include "irq-gic-common.h"
40 
41 static u8 dist_prio_irq __ro_after_init = GICV3_PRIO_IRQ;
42 static u8 dist_prio_nmi __ro_after_init = GICV3_PRIO_NMI;
43 
44 #define FLAGS_WORKAROUND_GICR_WAKER_MSM8996	(1ULL << 0)
45 #define FLAGS_WORKAROUND_CAVIUM_ERRATUM_38539	(1ULL << 1)
46 #define FLAGS_WORKAROUND_ASR_ERRATUM_8601001	(1ULL << 2)
47 
48 #define GIC_IRQ_TYPE_PARTITION	(GIC_IRQ_TYPE_LPI + 1)
49 
50 static struct cpumask broken_rdists __read_mostly __maybe_unused;
51 
52 struct redist_region {
53 	void __iomem		*redist_base;
54 	phys_addr_t		phys_base;
55 	bool			single_redist;
56 };
57 
58 struct gic_chip_data {
59 	struct fwnode_handle	*fwnode;
60 	phys_addr_t		dist_phys_base;
61 	void __iomem		*dist_base;
62 	struct redist_region	*redist_regions;
63 	struct rdists		rdists;
64 	struct irq_domain	*domain;
65 	u64			redist_stride;
66 	u32			nr_redist_regions;
67 	u64			flags;
68 	bool			has_rss;
69 	unsigned int		ppi_nr;
70 	struct partition_desc	**ppi_descs;
71 };
72 
73 #define T241_CHIPS_MAX		4
74 static void __iomem *t241_dist_base_alias[T241_CHIPS_MAX] __read_mostly;
75 static DEFINE_STATIC_KEY_FALSE(gic_nvidia_t241_erratum);
76 
77 static DEFINE_STATIC_KEY_FALSE(gic_arm64_2941627_erratum);
78 
79 static struct gic_chip_data gic_data __read_mostly;
80 static DEFINE_STATIC_KEY_TRUE(supports_deactivate_key);
81 
82 #define GIC_ID_NR	(1U << GICD_TYPER_ID_BITS(gic_data.rdists.gicd_typer))
83 #define GIC_LINE_NR	min(GICD_TYPER_SPIS(gic_data.rdists.gicd_typer), 1020U)
84 #define GIC_ESPI_NR	GICD_TYPER_ESPIS(gic_data.rdists.gicd_typer)
85 
86 /*
87  * There are 16 SGIs, though we only actually use 8 in Linux. The other 8 SGIs
88  * are potentially stolen by the secure side. Some code, especially code dealing
89  * with hwirq IDs, is simplified by accounting for all 16.
90  */
91 #define SGI_NR		16
92 
93 /*
94  * The behaviours of RPR and PMR registers differ depending on the value of
95  * SCR_EL3.FIQ, and the behaviour of non-secure priority registers of the
96  * distributor and redistributors depends on whether security is enabled in the
97  * GIC.
98  *
99  * When security is enabled, non-secure priority values from the (re)distributor
100  * are presented to the GIC CPUIF as follow:
101  *     (GIC_(R)DIST_PRI[irq] >> 1) | 0x80;
102  *
103  * If SCR_EL3.FIQ == 1, the values written to/read from PMR and RPR at non-secure
104  * EL1 are subject to a similar operation thus matching the priorities presented
105  * from the (re)distributor when security is enabled. When SCR_EL3.FIQ == 0,
106  * these values are unchanged by the GIC.
107  *
108  * see GICv3/GICv4 Architecture Specification (IHI0069D):
109  * - section 4.8.1 Non-secure accesses to register fields for Secure interrupt
110  *   priorities.
111  * - Figure 4-7 Secure read of the priority field for a Non-secure Group 1
112  *   interrupt.
113  */
114 static DEFINE_STATIC_KEY_FALSE(supports_pseudo_nmis);
115 
116 static u32 gic_get_pribits(void)
117 {
118 	u32 pribits;
119 
120 	pribits = gic_read_ctlr();
121 	pribits &= ICC_CTLR_EL1_PRI_BITS_MASK;
122 	pribits >>= ICC_CTLR_EL1_PRI_BITS_SHIFT;
123 	pribits++;
124 
125 	return pribits;
126 }
127 
128 static bool gic_has_group0(void)
129 {
130 	u32 val;
131 	u32 old_pmr;
132 
133 	old_pmr = gic_read_pmr();
134 
135 	/*
136 	 * Let's find out if Group0 is under control of EL3 or not by
137 	 * setting the highest possible, non-zero priority in PMR.
138 	 *
139 	 * If SCR_EL3.FIQ is set, the priority gets shifted down in
140 	 * order for the CPU interface to set bit 7, and keep the
141 	 * actual priority in the non-secure range. In the process, it
142 	 * looses the least significant bit and the actual priority
143 	 * becomes 0x80. Reading it back returns 0, indicating that
144 	 * we're don't have access to Group0.
145 	 */
146 	gic_write_pmr(BIT(8 - gic_get_pribits()));
147 	val = gic_read_pmr();
148 
149 	gic_write_pmr(old_pmr);
150 
151 	return val != 0;
152 }
153 
154 static inline bool gic_dist_security_disabled(void)
155 {
156 	return readl_relaxed(gic_data.dist_base + GICD_CTLR) & GICD_CTLR_DS;
157 }
158 
159 static bool cpus_have_security_disabled __ro_after_init;
160 static bool cpus_have_group0 __ro_after_init;
161 
162 static void __init gic_prio_init(void)
163 {
164 	cpus_have_security_disabled = gic_dist_security_disabled();
165 	cpus_have_group0 = gic_has_group0();
166 
167 	/*
168 	 * How priority values are used by the GIC depends on two things:
169 	 * the security state of the GIC (controlled by the GICD_CTRL.DS bit)
170 	 * and if Group 0 interrupts can be delivered to Linux in the non-secure
171 	 * world as FIQs (controlled by the SCR_EL3.FIQ bit). These affect the
172 	 * way priorities are presented in ICC_PMR_EL1 and in the distributor:
173 	 *
174 	 * GICD_CTRL.DS | SCR_EL3.FIQ | ICC_PMR_EL1 | Distributor
175 	 * -------------------------------------------------------
176 	 *      1       |      -      |  unchanged  |  unchanged
177 	 * -------------------------------------------------------
178 	 *      0       |      1      |  non-secure |  non-secure
179 	 * -------------------------------------------------------
180 	 *      0       |      0      |  unchanged  |  non-secure
181 	 *
182 	 * In the non-secure view reads and writes are modified:
183 	 *
184 	 * - A value written is right-shifted by one and the MSB is set,
185 	 *   forcing the priority into the non-secure range.
186 	 *
187 	 * - A value read is left-shifted by one.
188 	 *
189 	 * In the first two cases, where ICC_PMR_EL1 and the interrupt priority
190 	 * are both either modified or unchanged, we can use the same set of
191 	 * priorities.
192 	 *
193 	 * In the last case, where only the interrupt priorities are modified to
194 	 * be in the non-secure range, we program the non-secure values into
195 	 * the distributor to match the PMR values we want.
196 	 */
197 	if (cpus_have_group0 & !cpus_have_security_disabled) {
198 		dist_prio_irq = __gicv3_prio_to_ns(dist_prio_irq);
199 		dist_prio_nmi = __gicv3_prio_to_ns(dist_prio_nmi);
200 	}
201 
202 	pr_info("GICD_CTRL.DS=%d, SCR_EL3.FIQ=%d\n",
203 		cpus_have_security_disabled,
204 		!cpus_have_group0);
205 }
206 
207 /* rdist_nmi_refs[n] == number of cpus having the rdist interrupt n set as NMI */
208 static refcount_t *rdist_nmi_refs;
209 
210 static struct gic_kvm_info gic_v3_kvm_info __initdata;
211 static DEFINE_PER_CPU(bool, has_rss);
212 
213 #define MPIDR_RS(mpidr)			(((mpidr) & 0xF0UL) >> 4)
214 #define gic_data_rdist()		(this_cpu_ptr(gic_data.rdists.rdist))
215 #define gic_data_rdist_rd_base()	(gic_data_rdist()->rd_base)
216 #define gic_data_rdist_sgi_base()	(gic_data_rdist_rd_base() + SZ_64K)
217 
218 /* Our default, arbitrary priority value. Linux only uses one anyway. */
219 #define DEFAULT_PMR_VALUE	0xf0
220 
221 enum gic_intid_range {
222 	SGI_RANGE,
223 	PPI_RANGE,
224 	SPI_RANGE,
225 	EPPI_RANGE,
226 	ESPI_RANGE,
227 	LPI_RANGE,
228 	__INVALID_RANGE__
229 };
230 
231 static enum gic_intid_range __get_intid_range(irq_hw_number_t hwirq)
232 {
233 	switch (hwirq) {
234 	case 0 ... 15:
235 		return SGI_RANGE;
236 	case 16 ... 31:
237 		return PPI_RANGE;
238 	case 32 ... 1019:
239 		return SPI_RANGE;
240 	case EPPI_BASE_INTID ... (EPPI_BASE_INTID + 63):
241 		return EPPI_RANGE;
242 	case ESPI_BASE_INTID ... (ESPI_BASE_INTID + 1023):
243 		return ESPI_RANGE;
244 	case 8192 ... GENMASK(23, 0):
245 		return LPI_RANGE;
246 	default:
247 		return __INVALID_RANGE__;
248 	}
249 }
250 
251 static enum gic_intid_range get_intid_range(struct irq_data *d)
252 {
253 	return __get_intid_range(d->hwirq);
254 }
255 
256 static inline bool gic_irq_in_rdist(struct irq_data *d)
257 {
258 	switch (get_intid_range(d)) {
259 	case SGI_RANGE:
260 	case PPI_RANGE:
261 	case EPPI_RANGE:
262 		return true;
263 	default:
264 		return false;
265 	}
266 }
267 
268 static inline void __iomem *gic_dist_base_alias(struct irq_data *d)
269 {
270 	if (static_branch_unlikely(&gic_nvidia_t241_erratum)) {
271 		irq_hw_number_t hwirq = irqd_to_hwirq(d);
272 		u32 chip;
273 
274 		/*
275 		 * For the erratum T241-FABRIC-4, read accesses to GICD_In{E}
276 		 * registers are directed to the chip that owns the SPI. The
277 		 * the alias region can also be used for writes to the
278 		 * GICD_In{E} except GICD_ICENABLERn. Each chip has support
279 		 * for 320 {E}SPIs. Mappings for all 4 chips:
280 		 *    Chip0 = 32-351
281 		 *    Chip1 = 352-671
282 		 *    Chip2 = 672-991
283 		 *    Chip3 = 4096-4415
284 		 */
285 		switch (__get_intid_range(hwirq)) {
286 		case SPI_RANGE:
287 			chip = (hwirq - 32) / 320;
288 			break;
289 		case ESPI_RANGE:
290 			chip = 3;
291 			break;
292 		default:
293 			unreachable();
294 		}
295 		return t241_dist_base_alias[chip];
296 	}
297 
298 	return gic_data.dist_base;
299 }
300 
301 static inline void __iomem *gic_dist_base(struct irq_data *d)
302 {
303 	switch (get_intid_range(d)) {
304 	case SGI_RANGE:
305 	case PPI_RANGE:
306 	case EPPI_RANGE:
307 		/* SGI+PPI -> SGI_base for this CPU */
308 		return gic_data_rdist_sgi_base();
309 
310 	case SPI_RANGE:
311 	case ESPI_RANGE:
312 		/* SPI -> dist_base */
313 		return gic_data.dist_base;
314 
315 	default:
316 		return NULL;
317 	}
318 }
319 
320 static void gic_do_wait_for_rwp(void __iomem *base, u32 bit)
321 {
322 	u32 val;
323 	int ret;
324 
325 	ret = readl_relaxed_poll_timeout_atomic(base + GICD_CTLR, val, !(val & bit),
326 						1, USEC_PER_SEC);
327 	if (ret == -ETIMEDOUT)
328 		pr_err_ratelimited("RWP timeout, gone fishing\n");
329 }
330 
331 /* Wait for completion of a distributor change */
332 static void gic_dist_wait_for_rwp(void)
333 {
334 	gic_do_wait_for_rwp(gic_data.dist_base, GICD_CTLR_RWP);
335 }
336 
337 /* Wait for completion of a redistributor change */
338 static void gic_redist_wait_for_rwp(void)
339 {
340 	gic_do_wait_for_rwp(gic_data_rdist_rd_base(), GICR_CTLR_RWP);
341 }
342 
343 static void gic_enable_redist(bool enable)
344 {
345 	void __iomem *rbase;
346 	u32 val;
347 	int ret;
348 
349 	if (gic_data.flags & FLAGS_WORKAROUND_GICR_WAKER_MSM8996)
350 		return;
351 
352 	rbase = gic_data_rdist_rd_base();
353 
354 	val = readl_relaxed(rbase + GICR_WAKER);
355 	if (enable)
356 		/* Wake up this CPU redistributor */
357 		val &= ~GICR_WAKER_ProcessorSleep;
358 	else
359 		val |= GICR_WAKER_ProcessorSleep;
360 	writel_relaxed(val, rbase + GICR_WAKER);
361 
362 	if (!enable) {		/* Check that GICR_WAKER is writeable */
363 		val = readl_relaxed(rbase + GICR_WAKER);
364 		if (!(val & GICR_WAKER_ProcessorSleep))
365 			return;	/* No PM support in this redistributor */
366 	}
367 
368 	ret = readl_relaxed_poll_timeout_atomic(rbase + GICR_WAKER, val,
369 						enable ^ (bool)(val & GICR_WAKER_ChildrenAsleep),
370 						1, USEC_PER_SEC);
371 	if (ret == -ETIMEDOUT) {
372 		pr_err_ratelimited("redistributor failed to %s...\n",
373 				   enable ? "wakeup" : "sleep");
374 	}
375 }
376 
377 /*
378  * Routines to disable, enable, EOI and route interrupts
379  */
380 static u32 convert_offset_index(struct irq_data *d, u32 offset, u32 *index)
381 {
382 	switch (get_intid_range(d)) {
383 	case SGI_RANGE:
384 	case PPI_RANGE:
385 	case SPI_RANGE:
386 		*index = d->hwirq;
387 		return offset;
388 	case EPPI_RANGE:
389 		/*
390 		 * Contrary to the ESPI range, the EPPI range is contiguous
391 		 * to the PPI range in the registers, so let's adjust the
392 		 * displacement accordingly. Consistency is overrated.
393 		 */
394 		*index = d->hwirq - EPPI_BASE_INTID + 32;
395 		return offset;
396 	case ESPI_RANGE:
397 		*index = d->hwirq - ESPI_BASE_INTID;
398 		switch (offset) {
399 		case GICD_ISENABLER:
400 			return GICD_ISENABLERnE;
401 		case GICD_ICENABLER:
402 			return GICD_ICENABLERnE;
403 		case GICD_ISPENDR:
404 			return GICD_ISPENDRnE;
405 		case GICD_ICPENDR:
406 			return GICD_ICPENDRnE;
407 		case GICD_ISACTIVER:
408 			return GICD_ISACTIVERnE;
409 		case GICD_ICACTIVER:
410 			return GICD_ICACTIVERnE;
411 		case GICD_IPRIORITYR:
412 			return GICD_IPRIORITYRnE;
413 		case GICD_ICFGR:
414 			return GICD_ICFGRnE;
415 		case GICD_IROUTER:
416 			return GICD_IROUTERnE;
417 		default:
418 			break;
419 		}
420 		break;
421 	default:
422 		break;
423 	}
424 
425 	WARN_ON(1);
426 	*index = d->hwirq;
427 	return offset;
428 }
429 
430 static int gic_peek_irq(struct irq_data *d, u32 offset)
431 {
432 	void __iomem *base;
433 	u32 index, mask;
434 
435 	offset = convert_offset_index(d, offset, &index);
436 	mask = 1 << (index % 32);
437 
438 	if (gic_irq_in_rdist(d))
439 		base = gic_data_rdist_sgi_base();
440 	else
441 		base = gic_dist_base_alias(d);
442 
443 	return !!(readl_relaxed(base + offset + (index / 32) * 4) & mask);
444 }
445 
446 static void gic_poke_irq(struct irq_data *d, u32 offset)
447 {
448 	void __iomem *base;
449 	u32 index, mask;
450 
451 	offset = convert_offset_index(d, offset, &index);
452 	mask = 1 << (index % 32);
453 
454 	if (gic_irq_in_rdist(d))
455 		base = gic_data_rdist_sgi_base();
456 	else
457 		base = gic_data.dist_base;
458 
459 	writel_relaxed(mask, base + offset + (index / 32) * 4);
460 }
461 
462 static void gic_mask_irq(struct irq_data *d)
463 {
464 	gic_poke_irq(d, GICD_ICENABLER);
465 	if (gic_irq_in_rdist(d))
466 		gic_redist_wait_for_rwp();
467 	else
468 		gic_dist_wait_for_rwp();
469 }
470 
471 static void gic_eoimode1_mask_irq(struct irq_data *d)
472 {
473 	gic_mask_irq(d);
474 	/*
475 	 * When masking a forwarded interrupt, make sure it is
476 	 * deactivated as well.
477 	 *
478 	 * This ensures that an interrupt that is getting
479 	 * disabled/masked will not get "stuck", because there is
480 	 * noone to deactivate it (guest is being terminated).
481 	 */
482 	if (irqd_is_forwarded_to_vcpu(d))
483 		gic_poke_irq(d, GICD_ICACTIVER);
484 }
485 
486 static void gic_unmask_irq(struct irq_data *d)
487 {
488 	gic_poke_irq(d, GICD_ISENABLER);
489 }
490 
491 static inline bool gic_supports_nmi(void)
492 {
493 	return IS_ENABLED(CONFIG_ARM64_PSEUDO_NMI) &&
494 	       static_branch_likely(&supports_pseudo_nmis);
495 }
496 
497 static int gic_irq_set_irqchip_state(struct irq_data *d,
498 				     enum irqchip_irq_state which, bool val)
499 {
500 	u32 reg;
501 
502 	if (d->hwirq >= 8192) /* SGI/PPI/SPI only */
503 		return -EINVAL;
504 
505 	switch (which) {
506 	case IRQCHIP_STATE_PENDING:
507 		reg = val ? GICD_ISPENDR : GICD_ICPENDR;
508 		break;
509 
510 	case IRQCHIP_STATE_ACTIVE:
511 		reg = val ? GICD_ISACTIVER : GICD_ICACTIVER;
512 		break;
513 
514 	case IRQCHIP_STATE_MASKED:
515 		if (val) {
516 			gic_mask_irq(d);
517 			return 0;
518 		}
519 		reg = GICD_ISENABLER;
520 		break;
521 
522 	default:
523 		return -EINVAL;
524 	}
525 
526 	gic_poke_irq(d, reg);
527 	return 0;
528 }
529 
530 static int gic_irq_get_irqchip_state(struct irq_data *d,
531 				     enum irqchip_irq_state which, bool *val)
532 {
533 	if (d->hwirq >= 8192) /* PPI/SPI only */
534 		return -EINVAL;
535 
536 	switch (which) {
537 	case IRQCHIP_STATE_PENDING:
538 		*val = gic_peek_irq(d, GICD_ISPENDR);
539 		break;
540 
541 	case IRQCHIP_STATE_ACTIVE:
542 		*val = gic_peek_irq(d, GICD_ISACTIVER);
543 		break;
544 
545 	case IRQCHIP_STATE_MASKED:
546 		*val = !gic_peek_irq(d, GICD_ISENABLER);
547 		break;
548 
549 	default:
550 		return -EINVAL;
551 	}
552 
553 	return 0;
554 }
555 
556 static void gic_irq_set_prio(struct irq_data *d, u8 prio)
557 {
558 	void __iomem *base = gic_dist_base(d);
559 	u32 offset, index;
560 
561 	offset = convert_offset_index(d, GICD_IPRIORITYR, &index);
562 
563 	writeb_relaxed(prio, base + offset + index);
564 }
565 
566 static u32 __gic_get_ppi_index(irq_hw_number_t hwirq)
567 {
568 	switch (__get_intid_range(hwirq)) {
569 	case PPI_RANGE:
570 		return hwirq - 16;
571 	case EPPI_RANGE:
572 		return hwirq - EPPI_BASE_INTID + 16;
573 	default:
574 		unreachable();
575 	}
576 }
577 
578 static u32 __gic_get_rdist_index(irq_hw_number_t hwirq)
579 {
580 	switch (__get_intid_range(hwirq)) {
581 	case SGI_RANGE:
582 	case PPI_RANGE:
583 		return hwirq;
584 	case EPPI_RANGE:
585 		return hwirq - EPPI_BASE_INTID + 32;
586 	default:
587 		unreachable();
588 	}
589 }
590 
591 static u32 gic_get_rdist_index(struct irq_data *d)
592 {
593 	return __gic_get_rdist_index(d->hwirq);
594 }
595 
596 static int gic_irq_nmi_setup(struct irq_data *d)
597 {
598 	struct irq_desc *desc = irq_to_desc(d->irq);
599 
600 	if (!gic_supports_nmi())
601 		return -EINVAL;
602 
603 	if (gic_peek_irq(d, GICD_ISENABLER)) {
604 		pr_err("Cannot set NMI property of enabled IRQ %u\n", d->irq);
605 		return -EINVAL;
606 	}
607 
608 	/*
609 	 * A secondary irq_chip should be in charge of LPI request,
610 	 * it should not be possible to get there
611 	 */
612 	if (WARN_ON(irqd_to_hwirq(d) >= 8192))
613 		return -EINVAL;
614 
615 	/* desc lock should already be held */
616 	if (gic_irq_in_rdist(d)) {
617 		u32 idx = gic_get_rdist_index(d);
618 
619 		/*
620 		 * Setting up a percpu interrupt as NMI, only switch handler
621 		 * for first NMI
622 		 */
623 		if (!refcount_inc_not_zero(&rdist_nmi_refs[idx])) {
624 			refcount_set(&rdist_nmi_refs[idx], 1);
625 			desc->handle_irq = handle_percpu_devid_fasteoi_nmi;
626 		}
627 	} else {
628 		desc->handle_irq = handle_fasteoi_nmi;
629 	}
630 
631 	gic_irq_set_prio(d, dist_prio_nmi);
632 
633 	return 0;
634 }
635 
636 static void gic_irq_nmi_teardown(struct irq_data *d)
637 {
638 	struct irq_desc *desc = irq_to_desc(d->irq);
639 
640 	if (WARN_ON(!gic_supports_nmi()))
641 		return;
642 
643 	if (gic_peek_irq(d, GICD_ISENABLER)) {
644 		pr_err("Cannot set NMI property of enabled IRQ %u\n", d->irq);
645 		return;
646 	}
647 
648 	/*
649 	 * A secondary irq_chip should be in charge of LPI request,
650 	 * it should not be possible to get there
651 	 */
652 	if (WARN_ON(irqd_to_hwirq(d) >= 8192))
653 		return;
654 
655 	/* desc lock should already be held */
656 	if (gic_irq_in_rdist(d)) {
657 		u32 idx = gic_get_rdist_index(d);
658 
659 		/* Tearing down NMI, only switch handler for last NMI */
660 		if (refcount_dec_and_test(&rdist_nmi_refs[idx]))
661 			desc->handle_irq = handle_percpu_devid_irq;
662 	} else {
663 		desc->handle_irq = handle_fasteoi_irq;
664 	}
665 
666 	gic_irq_set_prio(d, dist_prio_irq);
667 }
668 
669 static bool gic_arm64_erratum_2941627_needed(struct irq_data *d)
670 {
671 	enum gic_intid_range range;
672 
673 	if (!static_branch_unlikely(&gic_arm64_2941627_erratum))
674 		return false;
675 
676 	range = get_intid_range(d);
677 
678 	/*
679 	 * The workaround is needed if the IRQ is an SPI and
680 	 * the target cpu is different from the one we are
681 	 * executing on.
682 	 */
683 	return (range == SPI_RANGE || range == ESPI_RANGE) &&
684 		!cpumask_test_cpu(raw_smp_processor_id(),
685 				  irq_data_get_effective_affinity_mask(d));
686 }
687 
688 static void gic_eoi_irq(struct irq_data *d)
689 {
690 	write_gicreg(irqd_to_hwirq(d), ICC_EOIR1_EL1);
691 	isb();
692 
693 	if (gic_arm64_erratum_2941627_needed(d)) {
694 		/*
695 		 * Make sure the GIC stream deactivate packet
696 		 * issued by ICC_EOIR1_EL1 has completed before
697 		 * deactivating through GICD_IACTIVER.
698 		 */
699 		dsb(sy);
700 		gic_poke_irq(d, GICD_ICACTIVER);
701 	}
702 }
703 
704 static void gic_eoimode1_eoi_irq(struct irq_data *d)
705 {
706 	/*
707 	 * No need to deactivate an LPI, or an interrupt that
708 	 * is is getting forwarded to a vcpu.
709 	 */
710 	if (irqd_to_hwirq(d) >= 8192 || irqd_is_forwarded_to_vcpu(d))
711 		return;
712 
713 	if (!gic_arm64_erratum_2941627_needed(d))
714 		gic_write_dir(irqd_to_hwirq(d));
715 	else
716 		gic_poke_irq(d, GICD_ICACTIVER);
717 }
718 
719 static int gic_set_type(struct irq_data *d, unsigned int type)
720 {
721 	irq_hw_number_t irq = irqd_to_hwirq(d);
722 	enum gic_intid_range range;
723 	void __iomem *base;
724 	u32 offset, index;
725 	int ret;
726 
727 	range = get_intid_range(d);
728 
729 	/* Interrupt configuration for SGIs can't be changed */
730 	if (range == SGI_RANGE)
731 		return type != IRQ_TYPE_EDGE_RISING ? -EINVAL : 0;
732 
733 	/* SPIs have restrictions on the supported types */
734 	if ((range == SPI_RANGE || range == ESPI_RANGE) &&
735 	    type != IRQ_TYPE_LEVEL_HIGH && type != IRQ_TYPE_EDGE_RISING)
736 		return -EINVAL;
737 
738 	if (gic_irq_in_rdist(d))
739 		base = gic_data_rdist_sgi_base();
740 	else
741 		base = gic_dist_base_alias(d);
742 
743 	offset = convert_offset_index(d, GICD_ICFGR, &index);
744 
745 	ret = gic_configure_irq(index, type, base + offset);
746 	if (ret && (range == PPI_RANGE || range == EPPI_RANGE)) {
747 		/* Misconfigured PPIs are usually not fatal */
748 		pr_warn("GIC: PPI INTID%ld is secure or misconfigured\n", irq);
749 		ret = 0;
750 	}
751 
752 	return ret;
753 }
754 
755 static int gic_irq_set_vcpu_affinity(struct irq_data *d, void *vcpu)
756 {
757 	if (get_intid_range(d) == SGI_RANGE)
758 		return -EINVAL;
759 
760 	if (vcpu)
761 		irqd_set_forwarded_to_vcpu(d);
762 	else
763 		irqd_clr_forwarded_to_vcpu(d);
764 	return 0;
765 }
766 
767 static u64 gic_cpu_to_affinity(int cpu)
768 {
769 	u64 mpidr = cpu_logical_map(cpu);
770 	u64 aff;
771 
772 	/* ASR8601 needs to have its affinities shifted down... */
773 	if (unlikely(gic_data.flags & FLAGS_WORKAROUND_ASR_ERRATUM_8601001))
774 		mpidr = (MPIDR_AFFINITY_LEVEL(mpidr, 1)	|
775 			 (MPIDR_AFFINITY_LEVEL(mpidr, 2) << 8));
776 
777 	aff = ((u64)MPIDR_AFFINITY_LEVEL(mpidr, 3) << 32 |
778 	       MPIDR_AFFINITY_LEVEL(mpidr, 2) << 16 |
779 	       MPIDR_AFFINITY_LEVEL(mpidr, 1) << 8  |
780 	       MPIDR_AFFINITY_LEVEL(mpidr, 0));
781 
782 	return aff;
783 }
784 
785 static void gic_deactivate_unhandled(u32 irqnr)
786 {
787 	if (static_branch_likely(&supports_deactivate_key)) {
788 		if (irqnr < 8192)
789 			gic_write_dir(irqnr);
790 	} else {
791 		write_gicreg(irqnr, ICC_EOIR1_EL1);
792 		isb();
793 	}
794 }
795 
796 /*
797  * Follow a read of the IAR with any HW maintenance that needs to happen prior
798  * to invoking the relevant IRQ handler. We must do two things:
799  *
800  * (1) Ensure instruction ordering between a read of IAR and subsequent
801  *     instructions in the IRQ handler using an ISB.
802  *
803  *     It is possible for the IAR to report an IRQ which was signalled *after*
804  *     the CPU took an IRQ exception as multiple interrupts can race to be
805  *     recognized by the GIC, earlier interrupts could be withdrawn, and/or
806  *     later interrupts could be prioritized by the GIC.
807  *
808  *     For devices which are tightly coupled to the CPU, such as PMUs, a
809  *     context synchronization event is necessary to ensure that system
810  *     register state is not stale, as these may have been indirectly written
811  *     *after* exception entry.
812  *
813  * (2) Deactivate the interrupt when EOI mode 1 is in use.
814  */
815 static inline void gic_complete_ack(u32 irqnr)
816 {
817 	if (static_branch_likely(&supports_deactivate_key))
818 		write_gicreg(irqnr, ICC_EOIR1_EL1);
819 
820 	isb();
821 }
822 
823 static bool gic_rpr_is_nmi_prio(void)
824 {
825 	if (!gic_supports_nmi())
826 		return false;
827 
828 	return unlikely(gic_read_rpr() == GICV3_PRIO_NMI);
829 }
830 
831 static bool gic_irqnr_is_special(u32 irqnr)
832 {
833 	return irqnr >= 1020 && irqnr <= 1023;
834 }
835 
836 static void __gic_handle_irq(u32 irqnr, struct pt_regs *regs)
837 {
838 	if (gic_irqnr_is_special(irqnr))
839 		return;
840 
841 	gic_complete_ack(irqnr);
842 
843 	if (generic_handle_domain_irq(gic_data.domain, irqnr)) {
844 		WARN_ONCE(true, "Unexpected interrupt (irqnr %u)\n", irqnr);
845 		gic_deactivate_unhandled(irqnr);
846 	}
847 }
848 
849 static void __gic_handle_nmi(u32 irqnr, struct pt_regs *regs)
850 {
851 	if (gic_irqnr_is_special(irqnr))
852 		return;
853 
854 	gic_complete_ack(irqnr);
855 
856 	if (generic_handle_domain_nmi(gic_data.domain, irqnr)) {
857 		WARN_ONCE(true, "Unexpected pseudo-NMI (irqnr %u)\n", irqnr);
858 		gic_deactivate_unhandled(irqnr);
859 	}
860 }
861 
862 /*
863  * An exception has been taken from a context with IRQs enabled, and this could
864  * be an IRQ or an NMI.
865  *
866  * The entry code called us with DAIF.IF set to keep NMIs masked. We must clear
867  * DAIF.IF (and update ICC_PMR_EL1 to mask regular IRQs) prior to returning,
868  * after handling any NMI but before handling any IRQ.
869  *
870  * The entry code has performed IRQ entry, and if an NMI is detected we must
871  * perform NMI entry/exit around invoking the handler.
872  */
873 static void __gic_handle_irq_from_irqson(struct pt_regs *regs)
874 {
875 	bool is_nmi;
876 	u32 irqnr;
877 
878 	irqnr = gic_read_iar();
879 
880 	is_nmi = gic_rpr_is_nmi_prio();
881 
882 	if (is_nmi) {
883 		nmi_enter();
884 		__gic_handle_nmi(irqnr, regs);
885 		nmi_exit();
886 	}
887 
888 	if (gic_prio_masking_enabled()) {
889 		gic_pmr_mask_irqs();
890 		gic_arch_enable_irqs();
891 	}
892 
893 	if (!is_nmi)
894 		__gic_handle_irq(irqnr, regs);
895 }
896 
897 /*
898  * An exception has been taken from a context with IRQs disabled, which can only
899  * be an NMI.
900  *
901  * The entry code called us with DAIF.IF set to keep NMIs masked. We must leave
902  * DAIF.IF (and ICC_PMR_EL1) unchanged.
903  *
904  * The entry code has performed NMI entry.
905  */
906 static void __gic_handle_irq_from_irqsoff(struct pt_regs *regs)
907 {
908 	u64 pmr;
909 	u32 irqnr;
910 
911 	/*
912 	 * We were in a context with IRQs disabled. However, the
913 	 * entry code has set PMR to a value that allows any
914 	 * interrupt to be acknowledged, and not just NMIs. This can
915 	 * lead to surprising effects if the NMI has been retired in
916 	 * the meantime, and that there is an IRQ pending. The IRQ
917 	 * would then be taken in NMI context, something that nobody
918 	 * wants to debug twice.
919 	 *
920 	 * Until we sort this, drop PMR again to a level that will
921 	 * actually only allow NMIs before reading IAR, and then
922 	 * restore it to what it was.
923 	 */
924 	pmr = gic_read_pmr();
925 	gic_pmr_mask_irqs();
926 	isb();
927 	irqnr = gic_read_iar();
928 	gic_write_pmr(pmr);
929 
930 	__gic_handle_nmi(irqnr, regs);
931 }
932 
933 static asmlinkage void __exception_irq_entry gic_handle_irq(struct pt_regs *regs)
934 {
935 	if (unlikely(gic_supports_nmi() && !interrupts_enabled(regs)))
936 		__gic_handle_irq_from_irqsoff(regs);
937 	else
938 		__gic_handle_irq_from_irqson(regs);
939 }
940 
941 static void __init gic_dist_init(void)
942 {
943 	unsigned int i;
944 	u64 affinity;
945 	void __iomem *base = gic_data.dist_base;
946 	u32 val;
947 
948 	/* Disable the distributor */
949 	writel_relaxed(0, base + GICD_CTLR);
950 	gic_dist_wait_for_rwp();
951 
952 	/*
953 	 * Configure SPIs as non-secure Group-1. This will only matter
954 	 * if the GIC only has a single security state. This will not
955 	 * do the right thing if the kernel is running in secure mode,
956 	 * but that's not the intended use case anyway.
957 	 */
958 	for (i = 32; i < GIC_LINE_NR; i += 32)
959 		writel_relaxed(~0, base + GICD_IGROUPR + i / 8);
960 
961 	/* Extended SPI range, not handled by the GICv2/GICv3 common code */
962 	for (i = 0; i < GIC_ESPI_NR; i += 32) {
963 		writel_relaxed(~0U, base + GICD_ICENABLERnE + i / 8);
964 		writel_relaxed(~0U, base + GICD_ICACTIVERnE + i / 8);
965 	}
966 
967 	for (i = 0; i < GIC_ESPI_NR; i += 32)
968 		writel_relaxed(~0U, base + GICD_IGROUPRnE + i / 8);
969 
970 	for (i = 0; i < GIC_ESPI_NR; i += 16)
971 		writel_relaxed(0, base + GICD_ICFGRnE + i / 4);
972 
973 	for (i = 0; i < GIC_ESPI_NR; i += 4)
974 		writel_relaxed(REPEAT_BYTE_U32(dist_prio_irq),
975 			       base + GICD_IPRIORITYRnE + i);
976 
977 	/* Now do the common stuff */
978 	gic_dist_config(base, GIC_LINE_NR, dist_prio_irq);
979 
980 	val = GICD_CTLR_ARE_NS | GICD_CTLR_ENABLE_G1A | GICD_CTLR_ENABLE_G1;
981 	if (gic_data.rdists.gicd_typer2 & GICD_TYPER2_nASSGIcap) {
982 		pr_info("Enabling SGIs without active state\n");
983 		val |= GICD_CTLR_nASSGIreq;
984 	}
985 
986 	/* Enable distributor with ARE, Group1, and wait for it to drain */
987 	writel_relaxed(val, base + GICD_CTLR);
988 	gic_dist_wait_for_rwp();
989 
990 	/*
991 	 * Set all global interrupts to the boot CPU only. ARE must be
992 	 * enabled.
993 	 */
994 	affinity = gic_cpu_to_affinity(smp_processor_id());
995 	for (i = 32; i < GIC_LINE_NR; i++)
996 		gic_write_irouter(affinity, base + GICD_IROUTER + i * 8);
997 
998 	for (i = 0; i < GIC_ESPI_NR; i++)
999 		gic_write_irouter(affinity, base + GICD_IROUTERnE + i * 8);
1000 }
1001 
1002 static int gic_iterate_rdists(int (*fn)(struct redist_region *, void __iomem *))
1003 {
1004 	int ret = -ENODEV;
1005 	int i;
1006 
1007 	for (i = 0; i < gic_data.nr_redist_regions; i++) {
1008 		void __iomem *ptr = gic_data.redist_regions[i].redist_base;
1009 		u64 typer;
1010 		u32 reg;
1011 
1012 		reg = readl_relaxed(ptr + GICR_PIDR2) & GIC_PIDR2_ARCH_MASK;
1013 		if (reg != GIC_PIDR2_ARCH_GICv3 &&
1014 		    reg != GIC_PIDR2_ARCH_GICv4) { /* We're in trouble... */
1015 			pr_warn("No redistributor present @%p\n", ptr);
1016 			break;
1017 		}
1018 
1019 		do {
1020 			typer = gic_read_typer(ptr + GICR_TYPER);
1021 			ret = fn(gic_data.redist_regions + i, ptr);
1022 			if (!ret)
1023 				return 0;
1024 
1025 			if (gic_data.redist_regions[i].single_redist)
1026 				break;
1027 
1028 			if (gic_data.redist_stride) {
1029 				ptr += gic_data.redist_stride;
1030 			} else {
1031 				ptr += SZ_64K * 2; /* Skip RD_base + SGI_base */
1032 				if (typer & GICR_TYPER_VLPIS)
1033 					ptr += SZ_64K * 2; /* Skip VLPI_base + reserved page */
1034 			}
1035 		} while (!(typer & GICR_TYPER_LAST));
1036 	}
1037 
1038 	return ret ? -ENODEV : 0;
1039 }
1040 
1041 static int __gic_populate_rdist(struct redist_region *region, void __iomem *ptr)
1042 {
1043 	unsigned long mpidr;
1044 	u64 typer;
1045 	u32 aff;
1046 
1047 	/*
1048 	 * Convert affinity to a 32bit value that can be matched to
1049 	 * GICR_TYPER bits [63:32].
1050 	 */
1051 	mpidr = gic_cpu_to_affinity(smp_processor_id());
1052 
1053 	aff = (MPIDR_AFFINITY_LEVEL(mpidr, 3) << 24 |
1054 	       MPIDR_AFFINITY_LEVEL(mpidr, 2) << 16 |
1055 	       MPIDR_AFFINITY_LEVEL(mpidr, 1) << 8 |
1056 	       MPIDR_AFFINITY_LEVEL(mpidr, 0));
1057 
1058 	typer = gic_read_typer(ptr + GICR_TYPER);
1059 	if ((typer >> 32) == aff) {
1060 		u64 offset = ptr - region->redist_base;
1061 		raw_spin_lock_init(&gic_data_rdist()->rd_lock);
1062 		gic_data_rdist_rd_base() = ptr;
1063 		gic_data_rdist()->phys_base = region->phys_base + offset;
1064 
1065 		pr_info("CPU%d: found redistributor %lx region %d:%pa\n",
1066 			smp_processor_id(), mpidr,
1067 			(int)(region - gic_data.redist_regions),
1068 			&gic_data_rdist()->phys_base);
1069 		return 0;
1070 	}
1071 
1072 	/* Try next one */
1073 	return 1;
1074 }
1075 
1076 static int gic_populate_rdist(void)
1077 {
1078 	if (gic_iterate_rdists(__gic_populate_rdist) == 0)
1079 		return 0;
1080 
1081 	/* We couldn't even deal with ourselves... */
1082 	WARN(true, "CPU%d: mpidr %lx has no re-distributor!\n",
1083 	     smp_processor_id(),
1084 	     (unsigned long)cpu_logical_map(smp_processor_id()));
1085 	return -ENODEV;
1086 }
1087 
1088 static int __gic_update_rdist_properties(struct redist_region *region,
1089 					 void __iomem *ptr)
1090 {
1091 	u64 typer = gic_read_typer(ptr + GICR_TYPER);
1092 	u32 ctlr = readl_relaxed(ptr + GICR_CTLR);
1093 
1094 	/* Boot-time cleanup */
1095 	if ((typer & GICR_TYPER_VLPIS) && (typer & GICR_TYPER_RVPEID)) {
1096 		u64 val;
1097 
1098 		/* Deactivate any present vPE */
1099 		val = gicr_read_vpendbaser(ptr + SZ_128K + GICR_VPENDBASER);
1100 		if (val & GICR_VPENDBASER_Valid)
1101 			gicr_write_vpendbaser(GICR_VPENDBASER_PendingLast,
1102 					      ptr + SZ_128K + GICR_VPENDBASER);
1103 
1104 		/* Mark the VPE table as invalid */
1105 		val = gicr_read_vpropbaser(ptr + SZ_128K + GICR_VPROPBASER);
1106 		val &= ~GICR_VPROPBASER_4_1_VALID;
1107 		gicr_write_vpropbaser(val, ptr + SZ_128K + GICR_VPROPBASER);
1108 	}
1109 
1110 	gic_data.rdists.has_vlpis &= !!(typer & GICR_TYPER_VLPIS);
1111 
1112 	/*
1113 	 * TYPER.RVPEID implies some form of DirectLPI, no matter what the
1114 	 * doc says... :-/ And CTLR.IR implies another subset of DirectLPI
1115 	 * that the ITS driver can make use of for LPIs (and not VLPIs).
1116 	 *
1117 	 * These are 3 different ways to express the same thing, depending
1118 	 * on the revision of the architecture and its relaxations over
1119 	 * time. Just group them under the 'direct_lpi' banner.
1120 	 */
1121 	gic_data.rdists.has_rvpeid &= !!(typer & GICR_TYPER_RVPEID);
1122 	gic_data.rdists.has_direct_lpi &= (!!(typer & GICR_TYPER_DirectLPIS) |
1123 					   !!(ctlr & GICR_CTLR_IR) |
1124 					   gic_data.rdists.has_rvpeid);
1125 	gic_data.rdists.has_vpend_valid_dirty &= !!(typer & GICR_TYPER_DIRTY);
1126 
1127 	/* Detect non-sensical configurations */
1128 	if (WARN_ON_ONCE(gic_data.rdists.has_rvpeid && !gic_data.rdists.has_vlpis)) {
1129 		gic_data.rdists.has_direct_lpi = false;
1130 		gic_data.rdists.has_vlpis = false;
1131 		gic_data.rdists.has_rvpeid = false;
1132 	}
1133 
1134 	gic_data.ppi_nr = min(GICR_TYPER_NR_PPIS(typer), gic_data.ppi_nr);
1135 
1136 	return 1;
1137 }
1138 
1139 static void gic_update_rdist_properties(void)
1140 {
1141 	gic_data.ppi_nr = UINT_MAX;
1142 	gic_iterate_rdists(__gic_update_rdist_properties);
1143 	if (WARN_ON(gic_data.ppi_nr == UINT_MAX))
1144 		gic_data.ppi_nr = 0;
1145 	pr_info("GICv3 features: %d PPIs%s%s\n",
1146 		gic_data.ppi_nr,
1147 		gic_data.has_rss ? ", RSS" : "",
1148 		gic_data.rdists.has_direct_lpi ? ", DirectLPI" : "");
1149 
1150 	if (gic_data.rdists.has_vlpis)
1151 		pr_info("GICv4 features: %s%s%s\n",
1152 			gic_data.rdists.has_direct_lpi ? "DirectLPI " : "",
1153 			gic_data.rdists.has_rvpeid ? "RVPEID " : "",
1154 			gic_data.rdists.has_vpend_valid_dirty ? "Valid+Dirty " : "");
1155 }
1156 
1157 static void gic_cpu_sys_reg_init(void)
1158 {
1159 	int i, cpu = smp_processor_id();
1160 	u64 mpidr = gic_cpu_to_affinity(cpu);
1161 	u64 need_rss = MPIDR_RS(mpidr);
1162 	bool group0;
1163 	u32 pribits;
1164 
1165 	/*
1166 	 * Need to check that the SRE bit has actually been set. If
1167 	 * not, it means that SRE is disabled at EL2. We're going to
1168 	 * die painfully, and there is nothing we can do about it.
1169 	 *
1170 	 * Kindly inform the luser.
1171 	 */
1172 	if (!gic_enable_sre())
1173 		pr_err("GIC: unable to set SRE (disabled at EL2), panic ahead\n");
1174 
1175 	pribits = gic_get_pribits();
1176 
1177 	group0 = gic_has_group0();
1178 
1179 	/* Set priority mask register */
1180 	if (!gic_prio_masking_enabled()) {
1181 		write_gicreg(DEFAULT_PMR_VALUE, ICC_PMR_EL1);
1182 	} else if (gic_supports_nmi()) {
1183 		/*
1184 		 * Check that all CPUs use the same priority space.
1185 		 *
1186 		 * If there's a mismatch with the boot CPU, the system is
1187 		 * likely to die as interrupt masking will not work properly on
1188 		 * all CPUs.
1189 		 */
1190 		WARN_ON(group0 != cpus_have_group0);
1191 		WARN_ON(gic_dist_security_disabled() != cpus_have_security_disabled);
1192 	}
1193 
1194 	/*
1195 	 * Some firmwares hand over to the kernel with the BPR changed from
1196 	 * its reset value (and with a value large enough to prevent
1197 	 * any pre-emptive interrupts from working at all). Writing a zero
1198 	 * to BPR restores is reset value.
1199 	 */
1200 	gic_write_bpr1(0);
1201 
1202 	if (static_branch_likely(&supports_deactivate_key)) {
1203 		/* EOI drops priority only (mode 1) */
1204 		gic_write_ctlr(ICC_CTLR_EL1_EOImode_drop);
1205 	} else {
1206 		/* EOI deactivates interrupt too (mode 0) */
1207 		gic_write_ctlr(ICC_CTLR_EL1_EOImode_drop_dir);
1208 	}
1209 
1210 	/* Always whack Group0 before Group1 */
1211 	if (group0) {
1212 		switch(pribits) {
1213 		case 8:
1214 		case 7:
1215 			write_gicreg(0, ICC_AP0R3_EL1);
1216 			write_gicreg(0, ICC_AP0R2_EL1);
1217 			fallthrough;
1218 		case 6:
1219 			write_gicreg(0, ICC_AP0R1_EL1);
1220 			fallthrough;
1221 		case 5:
1222 		case 4:
1223 			write_gicreg(0, ICC_AP0R0_EL1);
1224 		}
1225 
1226 		isb();
1227 	}
1228 
1229 	switch(pribits) {
1230 	case 8:
1231 	case 7:
1232 		write_gicreg(0, ICC_AP1R3_EL1);
1233 		write_gicreg(0, ICC_AP1R2_EL1);
1234 		fallthrough;
1235 	case 6:
1236 		write_gicreg(0, ICC_AP1R1_EL1);
1237 		fallthrough;
1238 	case 5:
1239 	case 4:
1240 		write_gicreg(0, ICC_AP1R0_EL1);
1241 	}
1242 
1243 	isb();
1244 
1245 	/* ... and let's hit the road... */
1246 	gic_write_grpen1(1);
1247 
1248 	/* Keep the RSS capability status in per_cpu variable */
1249 	per_cpu(has_rss, cpu) = !!(gic_read_ctlr() & ICC_CTLR_EL1_RSS);
1250 
1251 	/* Check all the CPUs have capable of sending SGIs to other CPUs */
1252 	for_each_online_cpu(i) {
1253 		bool have_rss = per_cpu(has_rss, i) && per_cpu(has_rss, cpu);
1254 
1255 		need_rss |= MPIDR_RS(gic_cpu_to_affinity(i));
1256 		if (need_rss && (!have_rss))
1257 			pr_crit("CPU%d (%lx) can't SGI CPU%d (%lx), no RSS\n",
1258 				cpu, (unsigned long)mpidr,
1259 				i, (unsigned long)gic_cpu_to_affinity(i));
1260 	}
1261 
1262 	/**
1263 	 * GIC spec says, when ICC_CTLR_EL1.RSS==1 and GICD_TYPER.RSS==0,
1264 	 * writing ICC_ASGI1R_EL1 register with RS != 0 is a CONSTRAINED
1265 	 * UNPREDICTABLE choice of :
1266 	 *   - The write is ignored.
1267 	 *   - The RS field is treated as 0.
1268 	 */
1269 	if (need_rss && (!gic_data.has_rss))
1270 		pr_crit_once("RSS is required but GICD doesn't support it\n");
1271 }
1272 
1273 static bool gicv3_nolpi;
1274 
1275 static int __init gicv3_nolpi_cfg(char *buf)
1276 {
1277 	return kstrtobool(buf, &gicv3_nolpi);
1278 }
1279 early_param("irqchip.gicv3_nolpi", gicv3_nolpi_cfg);
1280 
1281 static int gic_dist_supports_lpis(void)
1282 {
1283 	return (IS_ENABLED(CONFIG_ARM_GIC_V3_ITS) &&
1284 		!!(readl_relaxed(gic_data.dist_base + GICD_TYPER) & GICD_TYPER_LPIS) &&
1285 		!gicv3_nolpi);
1286 }
1287 
1288 static void gic_cpu_init(void)
1289 {
1290 	void __iomem *rbase;
1291 	int i;
1292 
1293 	/* Register ourselves with the rest of the world */
1294 	if (gic_populate_rdist())
1295 		return;
1296 
1297 	gic_enable_redist(true);
1298 
1299 	WARN((gic_data.ppi_nr > 16 || GIC_ESPI_NR != 0) &&
1300 	     !(gic_read_ctlr() & ICC_CTLR_EL1_ExtRange),
1301 	     "Distributor has extended ranges, but CPU%d doesn't\n",
1302 	     smp_processor_id());
1303 
1304 	rbase = gic_data_rdist_sgi_base();
1305 
1306 	/* Configure SGIs/PPIs as non-secure Group-1 */
1307 	for (i = 0; i < gic_data.ppi_nr + SGI_NR; i += 32)
1308 		writel_relaxed(~0, rbase + GICR_IGROUPR0 + i / 8);
1309 
1310 	gic_cpu_config(rbase, gic_data.ppi_nr + SGI_NR, dist_prio_irq);
1311 	gic_redist_wait_for_rwp();
1312 
1313 	/* initialise system registers */
1314 	gic_cpu_sys_reg_init();
1315 }
1316 
1317 #ifdef CONFIG_SMP
1318 
1319 #define MPIDR_TO_SGI_RS(mpidr)	(MPIDR_RS(mpidr) << ICC_SGI1R_RS_SHIFT)
1320 #define MPIDR_TO_SGI_CLUSTER_ID(mpidr)	((mpidr) & ~0xFUL)
1321 
1322 /*
1323  * gic_starting_cpu() is called after the last point where cpuhp is allowed
1324  * to fail. So pre check for problems earlier.
1325  */
1326 static int gic_check_rdist(unsigned int cpu)
1327 {
1328 	if (cpumask_test_cpu(cpu, &broken_rdists))
1329 		return -EINVAL;
1330 
1331 	return 0;
1332 }
1333 
1334 static int gic_starting_cpu(unsigned int cpu)
1335 {
1336 	gic_cpu_init();
1337 
1338 	if (gic_dist_supports_lpis())
1339 		its_cpu_init();
1340 
1341 	return 0;
1342 }
1343 
1344 static u16 gic_compute_target_list(int *base_cpu, const struct cpumask *mask,
1345 				   unsigned long cluster_id)
1346 {
1347 	int next_cpu, cpu = *base_cpu;
1348 	unsigned long mpidr;
1349 	u16 tlist = 0;
1350 
1351 	mpidr = gic_cpu_to_affinity(cpu);
1352 
1353 	while (cpu < nr_cpu_ids) {
1354 		tlist |= 1 << (mpidr & 0xf);
1355 
1356 		next_cpu = cpumask_next(cpu, mask);
1357 		if (next_cpu >= nr_cpu_ids)
1358 			goto out;
1359 		cpu = next_cpu;
1360 
1361 		mpidr = gic_cpu_to_affinity(cpu);
1362 
1363 		if (cluster_id != MPIDR_TO_SGI_CLUSTER_ID(mpidr)) {
1364 			cpu--;
1365 			goto out;
1366 		}
1367 	}
1368 out:
1369 	*base_cpu = cpu;
1370 	return tlist;
1371 }
1372 
1373 #define MPIDR_TO_SGI_AFFINITY(cluster_id, level) \
1374 	(MPIDR_AFFINITY_LEVEL(cluster_id, level) \
1375 		<< ICC_SGI1R_AFFINITY_## level ##_SHIFT)
1376 
1377 static void gic_send_sgi(u64 cluster_id, u16 tlist, unsigned int irq)
1378 {
1379 	u64 val;
1380 
1381 	val = (MPIDR_TO_SGI_AFFINITY(cluster_id, 3)	|
1382 	       MPIDR_TO_SGI_AFFINITY(cluster_id, 2)	|
1383 	       irq << ICC_SGI1R_SGI_ID_SHIFT		|
1384 	       MPIDR_TO_SGI_AFFINITY(cluster_id, 1)	|
1385 	       MPIDR_TO_SGI_RS(cluster_id)		|
1386 	       tlist << ICC_SGI1R_TARGET_LIST_SHIFT);
1387 
1388 	pr_devel("CPU%d: ICC_SGI1R_EL1 %llx\n", smp_processor_id(), val);
1389 	gic_write_sgi1r(val);
1390 }
1391 
1392 static void gic_ipi_send_mask(struct irq_data *d, const struct cpumask *mask)
1393 {
1394 	int cpu;
1395 
1396 	if (WARN_ON(d->hwirq >= 16))
1397 		return;
1398 
1399 	/*
1400 	 * Ensure that stores to Normal memory are visible to the
1401 	 * other CPUs before issuing the IPI.
1402 	 */
1403 	dsb(ishst);
1404 
1405 	for_each_cpu(cpu, mask) {
1406 		u64 cluster_id = MPIDR_TO_SGI_CLUSTER_ID(gic_cpu_to_affinity(cpu));
1407 		u16 tlist;
1408 
1409 		tlist = gic_compute_target_list(&cpu, mask, cluster_id);
1410 		gic_send_sgi(cluster_id, tlist, d->hwirq);
1411 	}
1412 
1413 	/* Force the above writes to ICC_SGI1R_EL1 to be executed */
1414 	isb();
1415 }
1416 
1417 static void __init gic_smp_init(void)
1418 {
1419 	struct irq_fwspec sgi_fwspec = {
1420 		.fwnode		= gic_data.fwnode,
1421 		.param_count	= 1,
1422 	};
1423 	int base_sgi;
1424 
1425 	cpuhp_setup_state_nocalls(CPUHP_BP_PREPARE_DYN,
1426 				  "irqchip/arm/gicv3:checkrdist",
1427 				  gic_check_rdist, NULL);
1428 
1429 	cpuhp_setup_state_nocalls(CPUHP_AP_IRQ_GIC_STARTING,
1430 				  "irqchip/arm/gicv3:starting",
1431 				  gic_starting_cpu, NULL);
1432 
1433 	/* Register all 8 non-secure SGIs */
1434 	base_sgi = irq_domain_alloc_irqs(gic_data.domain, 8, NUMA_NO_NODE, &sgi_fwspec);
1435 	if (WARN_ON(base_sgi <= 0))
1436 		return;
1437 
1438 	set_smp_ipi_range(base_sgi, 8);
1439 }
1440 
1441 static int gic_set_affinity(struct irq_data *d, const struct cpumask *mask_val,
1442 			    bool force)
1443 {
1444 	unsigned int cpu;
1445 	u32 offset, index;
1446 	void __iomem *reg;
1447 	int enabled;
1448 	u64 val;
1449 
1450 	if (force)
1451 		cpu = cpumask_first(mask_val);
1452 	else
1453 		cpu = cpumask_any_and(mask_val, cpu_online_mask);
1454 
1455 	if (cpu >= nr_cpu_ids)
1456 		return -EINVAL;
1457 
1458 	if (gic_irq_in_rdist(d))
1459 		return -EINVAL;
1460 
1461 	/* If interrupt was enabled, disable it first */
1462 	enabled = gic_peek_irq(d, GICD_ISENABLER);
1463 	if (enabled)
1464 		gic_mask_irq(d);
1465 
1466 	offset = convert_offset_index(d, GICD_IROUTER, &index);
1467 	reg = gic_dist_base(d) + offset + (index * 8);
1468 	val = gic_cpu_to_affinity(cpu);
1469 
1470 	gic_write_irouter(val, reg);
1471 
1472 	/*
1473 	 * If the interrupt was enabled, enabled it again. Otherwise,
1474 	 * just wait for the distributor to have digested our changes.
1475 	 */
1476 	if (enabled)
1477 		gic_unmask_irq(d);
1478 
1479 	irq_data_update_effective_affinity(d, cpumask_of(cpu));
1480 
1481 	return IRQ_SET_MASK_OK_DONE;
1482 }
1483 #else
1484 #define gic_set_affinity	NULL
1485 #define gic_ipi_send_mask	NULL
1486 #define gic_smp_init()		do { } while(0)
1487 #endif
1488 
1489 static int gic_retrigger(struct irq_data *data)
1490 {
1491 	return !gic_irq_set_irqchip_state(data, IRQCHIP_STATE_PENDING, true);
1492 }
1493 
1494 #ifdef CONFIG_CPU_PM
1495 static int gic_cpu_pm_notifier(struct notifier_block *self,
1496 			       unsigned long cmd, void *v)
1497 {
1498 	if (cmd == CPU_PM_EXIT) {
1499 		if (gic_dist_security_disabled())
1500 			gic_enable_redist(true);
1501 		gic_cpu_sys_reg_init();
1502 	} else if (cmd == CPU_PM_ENTER && gic_dist_security_disabled()) {
1503 		gic_write_grpen1(0);
1504 		gic_enable_redist(false);
1505 	}
1506 	return NOTIFY_OK;
1507 }
1508 
1509 static struct notifier_block gic_cpu_pm_notifier_block = {
1510 	.notifier_call = gic_cpu_pm_notifier,
1511 };
1512 
1513 static void gic_cpu_pm_init(void)
1514 {
1515 	cpu_pm_register_notifier(&gic_cpu_pm_notifier_block);
1516 }
1517 
1518 #else
1519 static inline void gic_cpu_pm_init(void) { }
1520 #endif /* CONFIG_CPU_PM */
1521 
1522 static struct irq_chip gic_chip = {
1523 	.name			= "GICv3",
1524 	.irq_mask		= gic_mask_irq,
1525 	.irq_unmask		= gic_unmask_irq,
1526 	.irq_eoi		= gic_eoi_irq,
1527 	.irq_set_type		= gic_set_type,
1528 	.irq_set_affinity	= gic_set_affinity,
1529 	.irq_retrigger          = gic_retrigger,
1530 	.irq_get_irqchip_state	= gic_irq_get_irqchip_state,
1531 	.irq_set_irqchip_state	= gic_irq_set_irqchip_state,
1532 	.irq_nmi_setup		= gic_irq_nmi_setup,
1533 	.irq_nmi_teardown	= gic_irq_nmi_teardown,
1534 	.ipi_send_mask		= gic_ipi_send_mask,
1535 	.flags			= IRQCHIP_SET_TYPE_MASKED |
1536 				  IRQCHIP_SKIP_SET_WAKE |
1537 				  IRQCHIP_MASK_ON_SUSPEND,
1538 };
1539 
1540 static struct irq_chip gic_eoimode1_chip = {
1541 	.name			= "GICv3",
1542 	.irq_mask		= gic_eoimode1_mask_irq,
1543 	.irq_unmask		= gic_unmask_irq,
1544 	.irq_eoi		= gic_eoimode1_eoi_irq,
1545 	.irq_set_type		= gic_set_type,
1546 	.irq_set_affinity	= gic_set_affinity,
1547 	.irq_retrigger          = gic_retrigger,
1548 	.irq_get_irqchip_state	= gic_irq_get_irqchip_state,
1549 	.irq_set_irqchip_state	= gic_irq_set_irqchip_state,
1550 	.irq_set_vcpu_affinity	= gic_irq_set_vcpu_affinity,
1551 	.irq_nmi_setup		= gic_irq_nmi_setup,
1552 	.irq_nmi_teardown	= gic_irq_nmi_teardown,
1553 	.ipi_send_mask		= gic_ipi_send_mask,
1554 	.flags			= IRQCHIP_SET_TYPE_MASKED |
1555 				  IRQCHIP_SKIP_SET_WAKE |
1556 				  IRQCHIP_MASK_ON_SUSPEND,
1557 };
1558 
1559 static int gic_irq_domain_map(struct irq_domain *d, unsigned int irq,
1560 			      irq_hw_number_t hw)
1561 {
1562 	struct irq_chip *chip = &gic_chip;
1563 	struct irq_data *irqd = irq_desc_get_irq_data(irq_to_desc(irq));
1564 
1565 	if (static_branch_likely(&supports_deactivate_key))
1566 		chip = &gic_eoimode1_chip;
1567 
1568 	switch (__get_intid_range(hw)) {
1569 	case SGI_RANGE:
1570 	case PPI_RANGE:
1571 	case EPPI_RANGE:
1572 		irq_set_percpu_devid(irq);
1573 		irq_domain_set_info(d, irq, hw, chip, d->host_data,
1574 				    handle_percpu_devid_irq, NULL, NULL);
1575 		break;
1576 
1577 	case SPI_RANGE:
1578 	case ESPI_RANGE:
1579 		irq_domain_set_info(d, irq, hw, chip, d->host_data,
1580 				    handle_fasteoi_irq, NULL, NULL);
1581 		irq_set_probe(irq);
1582 		irqd_set_single_target(irqd);
1583 		break;
1584 
1585 	case LPI_RANGE:
1586 		if (!gic_dist_supports_lpis())
1587 			return -EPERM;
1588 		irq_domain_set_info(d, irq, hw, chip, d->host_data,
1589 				    handle_fasteoi_irq, NULL, NULL);
1590 		break;
1591 
1592 	default:
1593 		return -EPERM;
1594 	}
1595 
1596 	/* Prevents SW retriggers which mess up the ACK/EOI ordering */
1597 	irqd_set_handle_enforce_irqctx(irqd);
1598 	return 0;
1599 }
1600 
1601 static int gic_irq_domain_translate(struct irq_domain *d,
1602 				    struct irq_fwspec *fwspec,
1603 				    unsigned long *hwirq,
1604 				    unsigned int *type)
1605 {
1606 	if (fwspec->param_count == 1 && fwspec->param[0] < 16) {
1607 		*hwirq = fwspec->param[0];
1608 		*type = IRQ_TYPE_EDGE_RISING;
1609 		return 0;
1610 	}
1611 
1612 	if (is_of_node(fwspec->fwnode)) {
1613 		if (fwspec->param_count < 3)
1614 			return -EINVAL;
1615 
1616 		switch (fwspec->param[0]) {
1617 		case 0:			/* SPI */
1618 			*hwirq = fwspec->param[1] + 32;
1619 			break;
1620 		case 1:			/* PPI */
1621 			*hwirq = fwspec->param[1] + 16;
1622 			break;
1623 		case 2:			/* ESPI */
1624 			*hwirq = fwspec->param[1] + ESPI_BASE_INTID;
1625 			break;
1626 		case 3:			/* EPPI */
1627 			*hwirq = fwspec->param[1] + EPPI_BASE_INTID;
1628 			break;
1629 		case GIC_IRQ_TYPE_LPI:	/* LPI */
1630 			*hwirq = fwspec->param[1];
1631 			break;
1632 		case GIC_IRQ_TYPE_PARTITION:
1633 			*hwirq = fwspec->param[1];
1634 			if (fwspec->param[1] >= 16)
1635 				*hwirq += EPPI_BASE_INTID - 16;
1636 			else
1637 				*hwirq += 16;
1638 			break;
1639 		default:
1640 			return -EINVAL;
1641 		}
1642 
1643 		*type = fwspec->param[2] & IRQ_TYPE_SENSE_MASK;
1644 
1645 		/*
1646 		 * Make it clear that broken DTs are... broken.
1647 		 * Partitioned PPIs are an unfortunate exception.
1648 		 */
1649 		WARN_ON(*type == IRQ_TYPE_NONE &&
1650 			fwspec->param[0] != GIC_IRQ_TYPE_PARTITION);
1651 		return 0;
1652 	}
1653 
1654 	if (is_fwnode_irqchip(fwspec->fwnode)) {
1655 		if(fwspec->param_count != 2)
1656 			return -EINVAL;
1657 
1658 		if (fwspec->param[0] < 16) {
1659 			pr_err(FW_BUG "Illegal GSI%d translation request\n",
1660 			       fwspec->param[0]);
1661 			return -EINVAL;
1662 		}
1663 
1664 		*hwirq = fwspec->param[0];
1665 		*type = fwspec->param[1];
1666 
1667 		WARN_ON(*type == IRQ_TYPE_NONE);
1668 		return 0;
1669 	}
1670 
1671 	return -EINVAL;
1672 }
1673 
1674 static int gic_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
1675 				unsigned int nr_irqs, void *arg)
1676 {
1677 	int i, ret;
1678 	irq_hw_number_t hwirq;
1679 	unsigned int type = IRQ_TYPE_NONE;
1680 	struct irq_fwspec *fwspec = arg;
1681 
1682 	ret = gic_irq_domain_translate(domain, fwspec, &hwirq, &type);
1683 	if (ret)
1684 		return ret;
1685 
1686 	for (i = 0; i < nr_irqs; i++) {
1687 		ret = gic_irq_domain_map(domain, virq + i, hwirq + i);
1688 		if (ret)
1689 			return ret;
1690 	}
1691 
1692 	return 0;
1693 }
1694 
1695 static void gic_irq_domain_free(struct irq_domain *domain, unsigned int virq,
1696 				unsigned int nr_irqs)
1697 {
1698 	int i;
1699 
1700 	for (i = 0; i < nr_irqs; i++) {
1701 		struct irq_data *d = irq_domain_get_irq_data(domain, virq + i);
1702 		irq_set_handler(virq + i, NULL);
1703 		irq_domain_reset_irq_data(d);
1704 	}
1705 }
1706 
1707 static bool fwspec_is_partitioned_ppi(struct irq_fwspec *fwspec,
1708 				      irq_hw_number_t hwirq)
1709 {
1710 	enum gic_intid_range range;
1711 
1712 	if (!gic_data.ppi_descs)
1713 		return false;
1714 
1715 	if (!is_of_node(fwspec->fwnode))
1716 		return false;
1717 
1718 	if (fwspec->param_count < 4 || !fwspec->param[3])
1719 		return false;
1720 
1721 	range = __get_intid_range(hwirq);
1722 	if (range != PPI_RANGE && range != EPPI_RANGE)
1723 		return false;
1724 
1725 	return true;
1726 }
1727 
1728 static int gic_irq_domain_select(struct irq_domain *d,
1729 				 struct irq_fwspec *fwspec,
1730 				 enum irq_domain_bus_token bus_token)
1731 {
1732 	unsigned int type, ret, ppi_idx;
1733 	irq_hw_number_t hwirq;
1734 
1735 	/* Not for us */
1736 	if (fwspec->fwnode != d->fwnode)
1737 		return 0;
1738 
1739 	/* Handle pure domain searches */
1740 	if (!fwspec->param_count)
1741 		return d->bus_token == bus_token;
1742 
1743 	/* If this is not DT, then we have a single domain */
1744 	if (!is_of_node(fwspec->fwnode))
1745 		return 1;
1746 
1747 	ret = gic_irq_domain_translate(d, fwspec, &hwirq, &type);
1748 	if (WARN_ON_ONCE(ret))
1749 		return 0;
1750 
1751 	if (!fwspec_is_partitioned_ppi(fwspec, hwirq))
1752 		return d == gic_data.domain;
1753 
1754 	/*
1755 	 * If this is a PPI and we have a 4th (non-null) parameter,
1756 	 * then we need to match the partition domain.
1757 	 */
1758 	ppi_idx = __gic_get_ppi_index(hwirq);
1759 	return d == partition_get_domain(gic_data.ppi_descs[ppi_idx]);
1760 }
1761 
1762 static const struct irq_domain_ops gic_irq_domain_ops = {
1763 	.translate = gic_irq_domain_translate,
1764 	.alloc = gic_irq_domain_alloc,
1765 	.free = gic_irq_domain_free,
1766 	.select = gic_irq_domain_select,
1767 };
1768 
1769 static int partition_domain_translate(struct irq_domain *d,
1770 				      struct irq_fwspec *fwspec,
1771 				      unsigned long *hwirq,
1772 				      unsigned int *type)
1773 {
1774 	unsigned long ppi_intid;
1775 	struct device_node *np;
1776 	unsigned int ppi_idx;
1777 	int ret;
1778 
1779 	if (!gic_data.ppi_descs)
1780 		return -ENOMEM;
1781 
1782 	np = of_find_node_by_phandle(fwspec->param[3]);
1783 	if (WARN_ON(!np))
1784 		return -EINVAL;
1785 
1786 	ret = gic_irq_domain_translate(d, fwspec, &ppi_intid, type);
1787 	if (WARN_ON_ONCE(ret))
1788 		return 0;
1789 
1790 	ppi_idx = __gic_get_ppi_index(ppi_intid);
1791 	ret = partition_translate_id(gic_data.ppi_descs[ppi_idx],
1792 				     of_node_to_fwnode(np));
1793 	if (ret < 0)
1794 		return ret;
1795 
1796 	*hwirq = ret;
1797 	*type = fwspec->param[2] & IRQ_TYPE_SENSE_MASK;
1798 
1799 	return 0;
1800 }
1801 
1802 static const struct irq_domain_ops partition_domain_ops = {
1803 	.translate = partition_domain_translate,
1804 	.select = gic_irq_domain_select,
1805 };
1806 
1807 static bool gic_enable_quirk_msm8996(void *data)
1808 {
1809 	struct gic_chip_data *d = data;
1810 
1811 	d->flags |= FLAGS_WORKAROUND_GICR_WAKER_MSM8996;
1812 
1813 	return true;
1814 }
1815 
1816 static bool gic_enable_quirk_cavium_38539(void *data)
1817 {
1818 	struct gic_chip_data *d = data;
1819 
1820 	d->flags |= FLAGS_WORKAROUND_CAVIUM_ERRATUM_38539;
1821 
1822 	return true;
1823 }
1824 
1825 static bool gic_enable_quirk_hip06_07(void *data)
1826 {
1827 	struct gic_chip_data *d = data;
1828 
1829 	/*
1830 	 * HIP06 GICD_IIDR clashes with GIC-600 product number (despite
1831 	 * not being an actual ARM implementation). The saving grace is
1832 	 * that GIC-600 doesn't have ESPI, so nothing to do in that case.
1833 	 * HIP07 doesn't even have a proper IIDR, and still pretends to
1834 	 * have ESPI. In both cases, put them right.
1835 	 */
1836 	if (d->rdists.gicd_typer & GICD_TYPER_ESPI) {
1837 		/* Zero both ESPI and the RES0 field next to it... */
1838 		d->rdists.gicd_typer &= ~GENMASK(9, 8);
1839 		return true;
1840 	}
1841 
1842 	return false;
1843 }
1844 
1845 #define T241_CHIPN_MASK		GENMASK_ULL(45, 44)
1846 #define T241_CHIP_GICDA_OFFSET	0x1580000
1847 #define SMCCC_SOC_ID_T241	0x036b0241
1848 
1849 static bool gic_enable_quirk_nvidia_t241(void *data)
1850 {
1851 	s32 soc_id = arm_smccc_get_soc_id_version();
1852 	unsigned long chip_bmask = 0;
1853 	phys_addr_t phys;
1854 	u32 i;
1855 
1856 	/* Check JEP106 code for NVIDIA T241 chip (036b:0241) */
1857 	if ((soc_id < 0) || (soc_id != SMCCC_SOC_ID_T241))
1858 		return false;
1859 
1860 	/* Find the chips based on GICR regions PHYS addr */
1861 	for (i = 0; i < gic_data.nr_redist_regions; i++) {
1862 		chip_bmask |= BIT(FIELD_GET(T241_CHIPN_MASK,
1863 				  (u64)gic_data.redist_regions[i].phys_base));
1864 	}
1865 
1866 	if (hweight32(chip_bmask) < 3)
1867 		return false;
1868 
1869 	/* Setup GICD alias regions */
1870 	for (i = 0; i < ARRAY_SIZE(t241_dist_base_alias); i++) {
1871 		if (chip_bmask & BIT(i)) {
1872 			phys = gic_data.dist_phys_base + T241_CHIP_GICDA_OFFSET;
1873 			phys |= FIELD_PREP(T241_CHIPN_MASK, i);
1874 			t241_dist_base_alias[i] = ioremap(phys, SZ_64K);
1875 			WARN_ON_ONCE(!t241_dist_base_alias[i]);
1876 		}
1877 	}
1878 	static_branch_enable(&gic_nvidia_t241_erratum);
1879 	return true;
1880 }
1881 
1882 static bool gic_enable_quirk_asr8601(void *data)
1883 {
1884 	struct gic_chip_data *d = data;
1885 
1886 	d->flags |= FLAGS_WORKAROUND_ASR_ERRATUM_8601001;
1887 
1888 	return true;
1889 }
1890 
1891 static bool gic_enable_quirk_arm64_2941627(void *data)
1892 {
1893 	static_branch_enable(&gic_arm64_2941627_erratum);
1894 	return true;
1895 }
1896 
1897 static bool rd_set_non_coherent(void *data)
1898 {
1899 	struct gic_chip_data *d = data;
1900 
1901 	d->rdists.flags |= RDIST_FLAGS_FORCE_NON_SHAREABLE;
1902 	return true;
1903 }
1904 
1905 static const struct gic_quirk gic_quirks[] = {
1906 	{
1907 		.desc	= "GICv3: Qualcomm MSM8996 broken firmware",
1908 		.compatible = "qcom,msm8996-gic-v3",
1909 		.init	= gic_enable_quirk_msm8996,
1910 	},
1911 	{
1912 		.desc	= "GICv3: ASR erratum 8601001",
1913 		.compatible = "asr,asr8601-gic-v3",
1914 		.init	= gic_enable_quirk_asr8601,
1915 	},
1916 	{
1917 		.desc	= "GICv3: HIP06 erratum 161010803",
1918 		.iidr	= 0x0204043b,
1919 		.mask	= 0xffffffff,
1920 		.init	= gic_enable_quirk_hip06_07,
1921 	},
1922 	{
1923 		.desc	= "GICv3: HIP07 erratum 161010803",
1924 		.iidr	= 0x00000000,
1925 		.mask	= 0xffffffff,
1926 		.init	= gic_enable_quirk_hip06_07,
1927 	},
1928 	{
1929 		/*
1930 		 * Reserved register accesses generate a Synchronous
1931 		 * External Abort. This erratum applies to:
1932 		 * - ThunderX: CN88xx
1933 		 * - OCTEON TX: CN83xx, CN81xx
1934 		 * - OCTEON TX2: CN93xx, CN96xx, CN98xx, CNF95xx*
1935 		 */
1936 		.desc	= "GICv3: Cavium erratum 38539",
1937 		.iidr	= 0xa000034c,
1938 		.mask	= 0xe8f00fff,
1939 		.init	= gic_enable_quirk_cavium_38539,
1940 	},
1941 	{
1942 		.desc	= "GICv3: NVIDIA erratum T241-FABRIC-4",
1943 		.iidr	= 0x0402043b,
1944 		.mask	= 0xffffffff,
1945 		.init	= gic_enable_quirk_nvidia_t241,
1946 	},
1947 	{
1948 		/*
1949 		 * GIC-700: 2941627 workaround - IP variant [0,1]
1950 		 *
1951 		 */
1952 		.desc	= "GICv3: ARM64 erratum 2941627",
1953 		.iidr	= 0x0400043b,
1954 		.mask	= 0xff0e0fff,
1955 		.init	= gic_enable_quirk_arm64_2941627,
1956 	},
1957 	{
1958 		/*
1959 		 * GIC-700: 2941627 workaround - IP variant [2]
1960 		 */
1961 		.desc	= "GICv3: ARM64 erratum 2941627",
1962 		.iidr	= 0x0402043b,
1963 		.mask	= 0xff0f0fff,
1964 		.init	= gic_enable_quirk_arm64_2941627,
1965 	},
1966 	{
1967 		.desc   = "GICv3: non-coherent attribute",
1968 		.property = "dma-noncoherent",
1969 		.init   = rd_set_non_coherent,
1970 	},
1971 	{
1972 	}
1973 };
1974 
1975 static void gic_enable_nmi_support(void)
1976 {
1977 	int i;
1978 
1979 	if (!gic_prio_masking_enabled())
1980 		return;
1981 
1982 	rdist_nmi_refs = kcalloc(gic_data.ppi_nr + SGI_NR,
1983 				 sizeof(*rdist_nmi_refs), GFP_KERNEL);
1984 	if (!rdist_nmi_refs)
1985 		return;
1986 
1987 	for (i = 0; i < gic_data.ppi_nr + SGI_NR; i++)
1988 		refcount_set(&rdist_nmi_refs[i], 0);
1989 
1990 	pr_info("Pseudo-NMIs enabled using %s ICC_PMR_EL1 synchronisation\n",
1991 		gic_has_relaxed_pmr_sync() ? "relaxed" : "forced");
1992 
1993 	static_branch_enable(&supports_pseudo_nmis);
1994 
1995 	if (static_branch_likely(&supports_deactivate_key))
1996 		gic_eoimode1_chip.flags |= IRQCHIP_SUPPORTS_NMI;
1997 	else
1998 		gic_chip.flags |= IRQCHIP_SUPPORTS_NMI;
1999 }
2000 
2001 static int __init gic_init_bases(phys_addr_t dist_phys_base,
2002 				 void __iomem *dist_base,
2003 				 struct redist_region *rdist_regs,
2004 				 u32 nr_redist_regions,
2005 				 u64 redist_stride,
2006 				 struct fwnode_handle *handle)
2007 {
2008 	u32 typer;
2009 	int err;
2010 
2011 	if (!is_hyp_mode_available())
2012 		static_branch_disable(&supports_deactivate_key);
2013 
2014 	if (static_branch_likely(&supports_deactivate_key))
2015 		pr_info("GIC: Using split EOI/Deactivate mode\n");
2016 
2017 	gic_data.fwnode = handle;
2018 	gic_data.dist_phys_base = dist_phys_base;
2019 	gic_data.dist_base = dist_base;
2020 	gic_data.redist_regions = rdist_regs;
2021 	gic_data.nr_redist_regions = nr_redist_regions;
2022 	gic_data.redist_stride = redist_stride;
2023 
2024 	/*
2025 	 * Find out how many interrupts are supported.
2026 	 */
2027 	typer = readl_relaxed(gic_data.dist_base + GICD_TYPER);
2028 	gic_data.rdists.gicd_typer = typer;
2029 
2030 	gic_enable_quirks(readl_relaxed(gic_data.dist_base + GICD_IIDR),
2031 			  gic_quirks, &gic_data);
2032 
2033 	pr_info("%d SPIs implemented\n", GIC_LINE_NR - 32);
2034 	pr_info("%d Extended SPIs implemented\n", GIC_ESPI_NR);
2035 
2036 	/*
2037 	 * ThunderX1 explodes on reading GICD_TYPER2, in violation of the
2038 	 * architecture spec (which says that reserved registers are RES0).
2039 	 */
2040 	if (!(gic_data.flags & FLAGS_WORKAROUND_CAVIUM_ERRATUM_38539))
2041 		gic_data.rdists.gicd_typer2 = readl_relaxed(gic_data.dist_base + GICD_TYPER2);
2042 
2043 	gic_data.domain = irq_domain_create_tree(handle, &gic_irq_domain_ops,
2044 						 &gic_data);
2045 	gic_data.rdists.rdist = alloc_percpu(typeof(*gic_data.rdists.rdist));
2046 	if (!static_branch_unlikely(&gic_nvidia_t241_erratum)) {
2047 		/* Disable GICv4.x features for the erratum T241-FABRIC-4 */
2048 		gic_data.rdists.has_rvpeid = true;
2049 		gic_data.rdists.has_vlpis = true;
2050 		gic_data.rdists.has_direct_lpi = true;
2051 		gic_data.rdists.has_vpend_valid_dirty = true;
2052 	}
2053 
2054 	if (WARN_ON(!gic_data.domain) || WARN_ON(!gic_data.rdists.rdist)) {
2055 		err = -ENOMEM;
2056 		goto out_free;
2057 	}
2058 
2059 	irq_domain_update_bus_token(gic_data.domain, DOMAIN_BUS_WIRED);
2060 
2061 	gic_data.has_rss = !!(typer & GICD_TYPER_RSS);
2062 
2063 	if (typer & GICD_TYPER_MBIS) {
2064 		err = mbi_init(handle, gic_data.domain);
2065 		if (err)
2066 			pr_err("Failed to initialize MBIs\n");
2067 	}
2068 
2069 	set_handle_irq(gic_handle_irq);
2070 
2071 	gic_update_rdist_properties();
2072 
2073 	gic_prio_init();
2074 	gic_dist_init();
2075 	gic_cpu_init();
2076 	gic_enable_nmi_support();
2077 	gic_smp_init();
2078 	gic_cpu_pm_init();
2079 
2080 	if (gic_dist_supports_lpis()) {
2081 		its_init(handle, &gic_data.rdists, gic_data.domain, dist_prio_irq);
2082 		its_cpu_init();
2083 		its_lpi_memreserve_init();
2084 	} else {
2085 		if (IS_ENABLED(CONFIG_ARM_GIC_V2M))
2086 			gicv2m_init(handle, gic_data.domain);
2087 	}
2088 
2089 	return 0;
2090 
2091 out_free:
2092 	if (gic_data.domain)
2093 		irq_domain_remove(gic_data.domain);
2094 	free_percpu(gic_data.rdists.rdist);
2095 	return err;
2096 }
2097 
2098 static int __init gic_validate_dist_version(void __iomem *dist_base)
2099 {
2100 	u32 reg = readl_relaxed(dist_base + GICD_PIDR2) & GIC_PIDR2_ARCH_MASK;
2101 
2102 	if (reg != GIC_PIDR2_ARCH_GICv3 && reg != GIC_PIDR2_ARCH_GICv4)
2103 		return -ENODEV;
2104 
2105 	return 0;
2106 }
2107 
2108 /* Create all possible partitions at boot time */
2109 static void __init gic_populate_ppi_partitions(struct device_node *gic_node)
2110 {
2111 	struct device_node *parts_node, *child_part;
2112 	int part_idx = 0, i;
2113 	int nr_parts;
2114 	struct partition_affinity *parts;
2115 
2116 	parts_node = of_get_child_by_name(gic_node, "ppi-partitions");
2117 	if (!parts_node)
2118 		return;
2119 
2120 	gic_data.ppi_descs = kcalloc(gic_data.ppi_nr, sizeof(*gic_data.ppi_descs), GFP_KERNEL);
2121 	if (!gic_data.ppi_descs)
2122 		goto out_put_node;
2123 
2124 	nr_parts = of_get_child_count(parts_node);
2125 
2126 	if (!nr_parts)
2127 		goto out_put_node;
2128 
2129 	parts = kcalloc(nr_parts, sizeof(*parts), GFP_KERNEL);
2130 	if (WARN_ON(!parts))
2131 		goto out_put_node;
2132 
2133 	for_each_child_of_node(parts_node, child_part) {
2134 		struct partition_affinity *part;
2135 		int n;
2136 
2137 		part = &parts[part_idx];
2138 
2139 		part->partition_id = of_node_to_fwnode(child_part);
2140 
2141 		pr_info("GIC: PPI partition %pOFn[%d] { ",
2142 			child_part, part_idx);
2143 
2144 		n = of_property_count_elems_of_size(child_part, "affinity",
2145 						    sizeof(u32));
2146 		WARN_ON(n <= 0);
2147 
2148 		for (i = 0; i < n; i++) {
2149 			int err, cpu;
2150 			u32 cpu_phandle;
2151 			struct device_node *cpu_node;
2152 
2153 			err = of_property_read_u32_index(child_part, "affinity",
2154 							 i, &cpu_phandle);
2155 			if (WARN_ON(err))
2156 				continue;
2157 
2158 			cpu_node = of_find_node_by_phandle(cpu_phandle);
2159 			if (WARN_ON(!cpu_node))
2160 				continue;
2161 
2162 			cpu = of_cpu_node_to_id(cpu_node);
2163 			if (WARN_ON(cpu < 0)) {
2164 				of_node_put(cpu_node);
2165 				continue;
2166 			}
2167 
2168 			pr_cont("%pOF[%d] ", cpu_node, cpu);
2169 
2170 			cpumask_set_cpu(cpu, &part->mask);
2171 			of_node_put(cpu_node);
2172 		}
2173 
2174 		pr_cont("}\n");
2175 		part_idx++;
2176 	}
2177 
2178 	for (i = 0; i < gic_data.ppi_nr; i++) {
2179 		unsigned int irq;
2180 		struct partition_desc *desc;
2181 		struct irq_fwspec ppi_fwspec = {
2182 			.fwnode		= gic_data.fwnode,
2183 			.param_count	= 3,
2184 			.param		= {
2185 				[0]	= GIC_IRQ_TYPE_PARTITION,
2186 				[1]	= i,
2187 				[2]	= IRQ_TYPE_NONE,
2188 			},
2189 		};
2190 
2191 		irq = irq_create_fwspec_mapping(&ppi_fwspec);
2192 		if (WARN_ON(!irq))
2193 			continue;
2194 		desc = partition_create_desc(gic_data.fwnode, parts, nr_parts,
2195 					     irq, &partition_domain_ops);
2196 		if (WARN_ON(!desc))
2197 			continue;
2198 
2199 		gic_data.ppi_descs[i] = desc;
2200 	}
2201 
2202 out_put_node:
2203 	of_node_put(parts_node);
2204 }
2205 
2206 static void __init gic_of_setup_kvm_info(struct device_node *node, u32 nr_redist_regions)
2207 {
2208 	int ret;
2209 	struct resource r;
2210 
2211 	gic_v3_kvm_info.type = GIC_V3;
2212 
2213 	gic_v3_kvm_info.maint_irq = irq_of_parse_and_map(node, 0);
2214 	if (!gic_v3_kvm_info.maint_irq)
2215 		return;
2216 
2217 	/* Also skip GICD, GICC, GICH */
2218 	ret = of_address_to_resource(node, nr_redist_regions + 3, &r);
2219 	if (!ret)
2220 		gic_v3_kvm_info.vcpu = r;
2221 
2222 	gic_v3_kvm_info.has_v4 = gic_data.rdists.has_vlpis;
2223 	gic_v3_kvm_info.has_v4_1 = gic_data.rdists.has_rvpeid;
2224 	vgic_set_kvm_info(&gic_v3_kvm_info);
2225 }
2226 
2227 static void gic_request_region(resource_size_t base, resource_size_t size,
2228 			       const char *name)
2229 {
2230 	if (!request_mem_region(base, size, name))
2231 		pr_warn_once(FW_BUG "%s region %pa has overlapping address\n",
2232 			     name, &base);
2233 }
2234 
2235 static void __iomem *gic_of_iomap(struct device_node *node, int idx,
2236 				  const char *name, struct resource *res)
2237 {
2238 	void __iomem *base;
2239 	int ret;
2240 
2241 	ret = of_address_to_resource(node, idx, res);
2242 	if (ret)
2243 		return IOMEM_ERR_PTR(ret);
2244 
2245 	gic_request_region(res->start, resource_size(res), name);
2246 	base = of_iomap(node, idx);
2247 
2248 	return base ?: IOMEM_ERR_PTR(-ENOMEM);
2249 }
2250 
2251 static int __init gic_of_init(struct device_node *node, struct device_node *parent)
2252 {
2253 	phys_addr_t dist_phys_base;
2254 	void __iomem *dist_base;
2255 	struct redist_region *rdist_regs;
2256 	struct resource res;
2257 	u64 redist_stride;
2258 	u32 nr_redist_regions;
2259 	int err, i;
2260 
2261 	dist_base = gic_of_iomap(node, 0, "GICD", &res);
2262 	if (IS_ERR(dist_base)) {
2263 		pr_err("%pOF: unable to map gic dist registers\n", node);
2264 		return PTR_ERR(dist_base);
2265 	}
2266 
2267 	dist_phys_base = res.start;
2268 
2269 	err = gic_validate_dist_version(dist_base);
2270 	if (err) {
2271 		pr_err("%pOF: no distributor detected, giving up\n", node);
2272 		goto out_unmap_dist;
2273 	}
2274 
2275 	if (of_property_read_u32(node, "#redistributor-regions", &nr_redist_regions))
2276 		nr_redist_regions = 1;
2277 
2278 	rdist_regs = kcalloc(nr_redist_regions, sizeof(*rdist_regs),
2279 			     GFP_KERNEL);
2280 	if (!rdist_regs) {
2281 		err = -ENOMEM;
2282 		goto out_unmap_dist;
2283 	}
2284 
2285 	for (i = 0; i < nr_redist_regions; i++) {
2286 		rdist_regs[i].redist_base = gic_of_iomap(node, 1 + i, "GICR", &res);
2287 		if (IS_ERR(rdist_regs[i].redist_base)) {
2288 			pr_err("%pOF: couldn't map region %d\n", node, i);
2289 			err = -ENODEV;
2290 			goto out_unmap_rdist;
2291 		}
2292 		rdist_regs[i].phys_base = res.start;
2293 	}
2294 
2295 	if (of_property_read_u64(node, "redistributor-stride", &redist_stride))
2296 		redist_stride = 0;
2297 
2298 	gic_enable_of_quirks(node, gic_quirks, &gic_data);
2299 
2300 	err = gic_init_bases(dist_phys_base, dist_base, rdist_regs,
2301 			     nr_redist_regions, redist_stride, &node->fwnode);
2302 	if (err)
2303 		goto out_unmap_rdist;
2304 
2305 	gic_populate_ppi_partitions(node);
2306 
2307 	if (static_branch_likely(&supports_deactivate_key))
2308 		gic_of_setup_kvm_info(node, nr_redist_regions);
2309 	return 0;
2310 
2311 out_unmap_rdist:
2312 	for (i = 0; i < nr_redist_regions; i++)
2313 		if (rdist_regs[i].redist_base && !IS_ERR(rdist_regs[i].redist_base))
2314 			iounmap(rdist_regs[i].redist_base);
2315 	kfree(rdist_regs);
2316 out_unmap_dist:
2317 	iounmap(dist_base);
2318 	return err;
2319 }
2320 
2321 IRQCHIP_DECLARE(gic_v3, "arm,gic-v3", gic_of_init);
2322 
2323 #ifdef CONFIG_ACPI
2324 static struct
2325 {
2326 	void __iomem *dist_base;
2327 	struct redist_region *redist_regs;
2328 	u32 nr_redist_regions;
2329 	bool single_redist;
2330 	int enabled_rdists;
2331 	u32 maint_irq;
2332 	int maint_irq_mode;
2333 	phys_addr_t vcpu_base;
2334 } acpi_data __initdata;
2335 
2336 static void __init
2337 gic_acpi_register_redist(phys_addr_t phys_base, void __iomem *redist_base)
2338 {
2339 	static int count = 0;
2340 
2341 	acpi_data.redist_regs[count].phys_base = phys_base;
2342 	acpi_data.redist_regs[count].redist_base = redist_base;
2343 	acpi_data.redist_regs[count].single_redist = acpi_data.single_redist;
2344 	count++;
2345 }
2346 
2347 static int __init
2348 gic_acpi_parse_madt_redist(union acpi_subtable_headers *header,
2349 			   const unsigned long end)
2350 {
2351 	struct acpi_madt_generic_redistributor *redist =
2352 			(struct acpi_madt_generic_redistributor *)header;
2353 	void __iomem *redist_base;
2354 
2355 	redist_base = ioremap(redist->base_address, redist->length);
2356 	if (!redist_base) {
2357 		pr_err("Couldn't map GICR region @%llx\n", redist->base_address);
2358 		return -ENOMEM;
2359 	}
2360 
2361 	if (acpi_get_madt_revision() >= 7 &&
2362 	    (redist->flags & ACPI_MADT_GICR_NON_COHERENT))
2363 		gic_data.rdists.flags |= RDIST_FLAGS_FORCE_NON_SHAREABLE;
2364 
2365 	gic_request_region(redist->base_address, redist->length, "GICR");
2366 
2367 	gic_acpi_register_redist(redist->base_address, redist_base);
2368 	return 0;
2369 }
2370 
2371 static int __init
2372 gic_acpi_parse_madt_gicc(union acpi_subtable_headers *header,
2373 			 const unsigned long end)
2374 {
2375 	struct acpi_madt_generic_interrupt *gicc =
2376 				(struct acpi_madt_generic_interrupt *)header;
2377 	u32 reg = readl_relaxed(acpi_data.dist_base + GICD_PIDR2) & GIC_PIDR2_ARCH_MASK;
2378 	u32 size = reg == GIC_PIDR2_ARCH_GICv4 ? SZ_64K * 4 : SZ_64K * 2;
2379 	void __iomem *redist_base;
2380 
2381 	/* Neither enabled or online capable means it doesn't exist, skip it */
2382 	if (!(gicc->flags & (ACPI_MADT_ENABLED | ACPI_MADT_GICC_ONLINE_CAPABLE)))
2383 		return 0;
2384 
2385 	/*
2386 	 * Capable but disabled CPUs can be brought online later. What about
2387 	 * the redistributor? ACPI doesn't want to say!
2388 	 * Virtual hotplug systems can use the MADT's "always-on" GICR entries.
2389 	 * Otherwise, prevent such CPUs from being brought online.
2390 	 */
2391 	if (!(gicc->flags & ACPI_MADT_ENABLED)) {
2392 		int cpu = get_cpu_for_acpi_id(gicc->uid);
2393 
2394 		pr_warn("CPU %u's redistributor is inaccessible: this CPU can't be brought online\n", cpu);
2395 		if (cpu >= 0)
2396 			cpumask_set_cpu(cpu, &broken_rdists);
2397 		return 0;
2398 	}
2399 
2400 	redist_base = ioremap(gicc->gicr_base_address, size);
2401 	if (!redist_base)
2402 		return -ENOMEM;
2403 	gic_request_region(gicc->gicr_base_address, size, "GICR");
2404 
2405 	if (acpi_get_madt_revision() >= 7 &&
2406 	    (gicc->flags & ACPI_MADT_GICC_NON_COHERENT))
2407 		gic_data.rdists.flags |= RDIST_FLAGS_FORCE_NON_SHAREABLE;
2408 
2409 	gic_acpi_register_redist(gicc->gicr_base_address, redist_base);
2410 	return 0;
2411 }
2412 
2413 static int __init gic_acpi_collect_gicr_base(void)
2414 {
2415 	acpi_tbl_entry_handler redist_parser;
2416 	enum acpi_madt_type type;
2417 
2418 	if (acpi_data.single_redist) {
2419 		type = ACPI_MADT_TYPE_GENERIC_INTERRUPT;
2420 		redist_parser = gic_acpi_parse_madt_gicc;
2421 	} else {
2422 		type = ACPI_MADT_TYPE_GENERIC_REDISTRIBUTOR;
2423 		redist_parser = gic_acpi_parse_madt_redist;
2424 	}
2425 
2426 	/* Collect redistributor base addresses in GICR entries */
2427 	if (acpi_table_parse_madt(type, redist_parser, 0) > 0)
2428 		return 0;
2429 
2430 	pr_info("No valid GICR entries exist\n");
2431 	return -ENODEV;
2432 }
2433 
2434 static int __init gic_acpi_match_gicr(union acpi_subtable_headers *header,
2435 				  const unsigned long end)
2436 {
2437 	/* Subtable presence means that redist exists, that's it */
2438 	return 0;
2439 }
2440 
2441 static int __init gic_acpi_match_gicc(union acpi_subtable_headers *header,
2442 				      const unsigned long end)
2443 {
2444 	struct acpi_madt_generic_interrupt *gicc =
2445 				(struct acpi_madt_generic_interrupt *)header;
2446 
2447 	/*
2448 	 * If GICC is enabled and has valid gicr base address, then it means
2449 	 * GICR base is presented via GICC. The redistributor is only known to
2450 	 * be accessible if the GICC is marked as enabled. If this bit is not
2451 	 * set, we'd need to add the redistributor at runtime, which isn't
2452 	 * supported.
2453 	 */
2454 	if (gicc->flags & ACPI_MADT_ENABLED && gicc->gicr_base_address)
2455 		acpi_data.enabled_rdists++;
2456 
2457 	return 0;
2458 }
2459 
2460 static int __init gic_acpi_count_gicr_regions(void)
2461 {
2462 	int count;
2463 
2464 	/*
2465 	 * Count how many redistributor regions we have. It is not allowed
2466 	 * to mix redistributor description, GICR and GICC subtables have to be
2467 	 * mutually exclusive.
2468 	 */
2469 	count = acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_REDISTRIBUTOR,
2470 				      gic_acpi_match_gicr, 0);
2471 	if (count > 0) {
2472 		acpi_data.single_redist = false;
2473 		return count;
2474 	}
2475 
2476 	count = acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_INTERRUPT,
2477 				      gic_acpi_match_gicc, 0);
2478 	if (count > 0) {
2479 		acpi_data.single_redist = true;
2480 		count = acpi_data.enabled_rdists;
2481 	}
2482 
2483 	return count;
2484 }
2485 
2486 static bool __init acpi_validate_gic_table(struct acpi_subtable_header *header,
2487 					   struct acpi_probe_entry *ape)
2488 {
2489 	struct acpi_madt_generic_distributor *dist;
2490 	int count;
2491 
2492 	dist = (struct acpi_madt_generic_distributor *)header;
2493 	if (dist->version != ape->driver_data)
2494 		return false;
2495 
2496 	/* We need to do that exercise anyway, the sooner the better */
2497 	count = gic_acpi_count_gicr_regions();
2498 	if (count <= 0)
2499 		return false;
2500 
2501 	acpi_data.nr_redist_regions = count;
2502 	return true;
2503 }
2504 
2505 static int __init gic_acpi_parse_virt_madt_gicc(union acpi_subtable_headers *header,
2506 						const unsigned long end)
2507 {
2508 	struct acpi_madt_generic_interrupt *gicc =
2509 		(struct acpi_madt_generic_interrupt *)header;
2510 	int maint_irq_mode;
2511 	static int first_madt = true;
2512 
2513 	if (!(gicc->flags &
2514 	      (ACPI_MADT_ENABLED | ACPI_MADT_GICC_ONLINE_CAPABLE)))
2515 		return 0;
2516 
2517 	maint_irq_mode = (gicc->flags & ACPI_MADT_VGIC_IRQ_MODE) ?
2518 		ACPI_EDGE_SENSITIVE : ACPI_LEVEL_SENSITIVE;
2519 
2520 	if (first_madt) {
2521 		first_madt = false;
2522 
2523 		acpi_data.maint_irq = gicc->vgic_interrupt;
2524 		acpi_data.maint_irq_mode = maint_irq_mode;
2525 		acpi_data.vcpu_base = gicc->gicv_base_address;
2526 
2527 		return 0;
2528 	}
2529 
2530 	/*
2531 	 * The maintenance interrupt and GICV should be the same for every CPU
2532 	 */
2533 	if ((acpi_data.maint_irq != gicc->vgic_interrupt) ||
2534 	    (acpi_data.maint_irq_mode != maint_irq_mode) ||
2535 	    (acpi_data.vcpu_base != gicc->gicv_base_address))
2536 		return -EINVAL;
2537 
2538 	return 0;
2539 }
2540 
2541 static bool __init gic_acpi_collect_virt_info(void)
2542 {
2543 	int count;
2544 
2545 	count = acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_INTERRUPT,
2546 				      gic_acpi_parse_virt_madt_gicc, 0);
2547 
2548 	return (count > 0);
2549 }
2550 
2551 #define ACPI_GICV3_DIST_MEM_SIZE (SZ_64K)
2552 #define ACPI_GICV2_VCTRL_MEM_SIZE	(SZ_4K)
2553 #define ACPI_GICV2_VCPU_MEM_SIZE	(SZ_8K)
2554 
2555 static void __init gic_acpi_setup_kvm_info(void)
2556 {
2557 	int irq;
2558 
2559 	if (!gic_acpi_collect_virt_info()) {
2560 		pr_warn("Unable to get hardware information used for virtualization\n");
2561 		return;
2562 	}
2563 
2564 	gic_v3_kvm_info.type = GIC_V3;
2565 
2566 	irq = acpi_register_gsi(NULL, acpi_data.maint_irq,
2567 				acpi_data.maint_irq_mode,
2568 				ACPI_ACTIVE_HIGH);
2569 	if (irq <= 0)
2570 		return;
2571 
2572 	gic_v3_kvm_info.maint_irq = irq;
2573 
2574 	if (acpi_data.vcpu_base) {
2575 		struct resource *vcpu = &gic_v3_kvm_info.vcpu;
2576 
2577 		vcpu->flags = IORESOURCE_MEM;
2578 		vcpu->start = acpi_data.vcpu_base;
2579 		vcpu->end = vcpu->start + ACPI_GICV2_VCPU_MEM_SIZE - 1;
2580 	}
2581 
2582 	gic_v3_kvm_info.has_v4 = gic_data.rdists.has_vlpis;
2583 	gic_v3_kvm_info.has_v4_1 = gic_data.rdists.has_rvpeid;
2584 	vgic_set_kvm_info(&gic_v3_kvm_info);
2585 }
2586 
2587 static struct fwnode_handle *gsi_domain_handle;
2588 
2589 static struct fwnode_handle *gic_v3_get_gsi_domain_id(u32 gsi)
2590 {
2591 	return gsi_domain_handle;
2592 }
2593 
2594 static int __init
2595 gic_acpi_init(union acpi_subtable_headers *header, const unsigned long end)
2596 {
2597 	struct acpi_madt_generic_distributor *dist;
2598 	size_t size;
2599 	int i, err;
2600 
2601 	/* Get distributor base address */
2602 	dist = (struct acpi_madt_generic_distributor *)header;
2603 	acpi_data.dist_base = ioremap(dist->base_address,
2604 				      ACPI_GICV3_DIST_MEM_SIZE);
2605 	if (!acpi_data.dist_base) {
2606 		pr_err("Unable to map GICD registers\n");
2607 		return -ENOMEM;
2608 	}
2609 	gic_request_region(dist->base_address, ACPI_GICV3_DIST_MEM_SIZE, "GICD");
2610 
2611 	err = gic_validate_dist_version(acpi_data.dist_base);
2612 	if (err) {
2613 		pr_err("No distributor detected at @%p, giving up\n",
2614 		       acpi_data.dist_base);
2615 		goto out_dist_unmap;
2616 	}
2617 
2618 	size = sizeof(*acpi_data.redist_regs) * acpi_data.nr_redist_regions;
2619 	acpi_data.redist_regs = kzalloc(size, GFP_KERNEL);
2620 	if (!acpi_data.redist_regs) {
2621 		err = -ENOMEM;
2622 		goto out_dist_unmap;
2623 	}
2624 
2625 	err = gic_acpi_collect_gicr_base();
2626 	if (err)
2627 		goto out_redist_unmap;
2628 
2629 	gsi_domain_handle = irq_domain_alloc_fwnode(&dist->base_address);
2630 	if (!gsi_domain_handle) {
2631 		err = -ENOMEM;
2632 		goto out_redist_unmap;
2633 	}
2634 
2635 	err = gic_init_bases(dist->base_address, acpi_data.dist_base,
2636 			     acpi_data.redist_regs, acpi_data.nr_redist_regions,
2637 			     0, gsi_domain_handle);
2638 	if (err)
2639 		goto out_fwhandle_free;
2640 
2641 	acpi_set_irq_model(ACPI_IRQ_MODEL_GIC, gic_v3_get_gsi_domain_id);
2642 
2643 	if (static_branch_likely(&supports_deactivate_key))
2644 		gic_acpi_setup_kvm_info();
2645 
2646 	return 0;
2647 
2648 out_fwhandle_free:
2649 	irq_domain_free_fwnode(gsi_domain_handle);
2650 out_redist_unmap:
2651 	for (i = 0; i < acpi_data.nr_redist_regions; i++)
2652 		if (acpi_data.redist_regs[i].redist_base)
2653 			iounmap(acpi_data.redist_regs[i].redist_base);
2654 	kfree(acpi_data.redist_regs);
2655 out_dist_unmap:
2656 	iounmap(acpi_data.dist_base);
2657 	return err;
2658 }
2659 IRQCHIP_ACPI_DECLARE(gic_v3, ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR,
2660 		     acpi_validate_gic_table, ACPI_MADT_GIC_VERSION_V3,
2661 		     gic_acpi_init);
2662 IRQCHIP_ACPI_DECLARE(gic_v4, ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR,
2663 		     acpi_validate_gic_table, ACPI_MADT_GIC_VERSION_V4,
2664 		     gic_acpi_init);
2665 IRQCHIP_ACPI_DECLARE(gic_v3_or_v4, ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR,
2666 		     acpi_validate_gic_table, ACPI_MADT_GIC_VERSION_NONE,
2667 		     gic_acpi_init);
2668 #endif
2669