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