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