blob: 04bc12d2a4552e97935835886c8e619f826e752b [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2 * Copyright (C) 2013-2017 ARM Limited, All Rights Reserved.
3 * Author: Marc Zyngier <marc.zyngier@arm.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#define pr_fmt(fmt) "GICv3: " fmt
19
20#include <linux/acpi.h>
21#include <linux/cpu.h>
22#include <linux/cpu_pm.h>
23#include <linux/delay.h>
24#include <linux/interrupt.h>
25#include <linux/irqdomain.h>
26#include <linux/of.h>
27#include <linux/of_address.h>
28#include <linux/of_irq.h>
29#include <linux/percpu.h>
30#include <linux/slab.h>
31
32#include <linux/irqchip.h>
33#include <linux/irqchip/arm-gic-common.h>
34#include <linux/irqchip/arm-gic-v3.h>
35#include <linux/irqchip/irq-partition-percpu.h>
36
37#include <asm/cputype.h>
38#include <asm/exception.h>
39#include <asm/smp_plat.h>
40#include <asm/virt.h>
41
42#include "irq-gic-common.h"
43
44struct redist_region {
45 void __iomem *redist_base;
46 phys_addr_t phys_base;
47 bool single_redist;
48};
49
50struct gic_chip_data {
51 struct fwnode_handle *fwnode;
52 void __iomem *dist_base;
53 struct redist_region *redist_regions;
54 struct rdists rdists;
55 struct irq_domain *domain;
56 u64 redist_stride;
57 u32 nr_redist_regions;
58 unsigned int irq_nr;
59 struct partition_desc *ppi_descs[16];
60};
61
62static struct gic_chip_data gic_data __read_mostly;
63static struct static_key supports_deactivate = STATIC_KEY_INIT_TRUE;
64
65static struct gic_kvm_info gic_v3_kvm_info;
66
67#define gic_data_rdist() (this_cpu_ptr(gic_data.rdists.rdist))
68#define gic_data_rdist_rd_base() (gic_data_rdist()->rd_base)
69#define gic_data_rdist_sgi_base() (gic_data_rdist_rd_base() + SZ_64K)
70
71/* Our default, arbitrary priority value. Linux only uses one anyway. */
72#define DEFAULT_PMR_VALUE 0xf0
73
74static inline unsigned int gic_irq(struct irq_data *d)
75{
76 return d->hwirq;
77}
78
79static inline int gic_irq_in_rdist(struct irq_data *d)
80{
81 return gic_irq(d) < 32;
82}
83
84static inline void __iomem *gic_dist_base(struct irq_data *d)
85{
86 if (gic_irq_in_rdist(d)) /* SGI+PPI -> SGI_base for this CPU */
87 return gic_data_rdist_sgi_base();
88
89 if (d->hwirq <= 1023) /* SPI -> dist_base */
90 return gic_data.dist_base;
91
92 return NULL;
93}
94
95static void gic_do_wait_for_rwp(void __iomem *base)
96{
97 u32 count = 1000000; /* 1s! */
98
99 while (readl_relaxed(base + GICD_CTLR) & GICD_CTLR_RWP) {
100 count--;
101 if (!count) {
102 pr_err_ratelimited("RWP timeout, gone fishing\n");
103 return;
104 }
105 cpu_relax();
106 udelay(1);
107 };
108}
109
110/* Wait for completion of a distributor change */
111static void gic_dist_wait_for_rwp(void)
112{
113 gic_do_wait_for_rwp(gic_data.dist_base);
114}
115
116/* Wait for completion of a redistributor change */
117static void gic_redist_wait_for_rwp(void)
118{
119 gic_do_wait_for_rwp(gic_data_rdist_rd_base());
120}
121
122#ifdef CONFIG_ARM64
123
124static u64 __maybe_unused gic_read_iar(void)
125{
126 if (cpus_have_const_cap(ARM64_WORKAROUND_CAVIUM_23154))
127 return gic_read_iar_cavium_thunderx();
128 else
129 return gic_read_iar_common();
130}
131#endif
132
133static void gic_enable_redist(bool enable)
134{
135 void __iomem *rbase;
136 u32 count = 1000000; /* 1s! */
137 u32 val;
138
139 rbase = gic_data_rdist_rd_base();
140
141 val = readl_relaxed(rbase + GICR_WAKER);
142 if (enable)
143 /* Wake up this CPU redistributor */
144 val &= ~GICR_WAKER_ProcessorSleep;
145 else
146 val |= GICR_WAKER_ProcessorSleep;
147 writel_relaxed(val, rbase + GICR_WAKER);
148
149 if (!enable) { /* Check that GICR_WAKER is writeable */
150 val = readl_relaxed(rbase + GICR_WAKER);
151 if (!(val & GICR_WAKER_ProcessorSleep))
152 return; /* No PM support in this redistributor */
153 }
154
155 while (--count) {
156 val = readl_relaxed(rbase + GICR_WAKER);
157 if (enable ^ (bool)(val & GICR_WAKER_ChildrenAsleep))
158 break;
159 cpu_relax();
160 udelay(1);
161 };
162 if (!count)
163 pr_err_ratelimited("redistributor failed to %s...\n",
164 enable ? "wakeup" : "sleep");
165}
166
167/*
168 * Routines to disable, enable, EOI and route interrupts
169 */
170static int gic_peek_irq(struct irq_data *d, u32 offset)
171{
172 u32 mask = 1 << (gic_irq(d) % 32);
173 void __iomem *base;
174
175 if (gic_irq_in_rdist(d))
176 base = gic_data_rdist_sgi_base();
177 else
178 base = gic_data.dist_base;
179
180 return !!(readl_relaxed(base + offset + (gic_irq(d) / 32) * 4) & mask);
181}
182
183static void gic_poke_irq(struct irq_data *d, u32 offset)
184{
185 u32 mask = 1 << (gic_irq(d) % 32);
186 void (*rwp_wait)(void);
187 void __iomem *base;
188
189 if (gic_irq_in_rdist(d)) {
190 base = gic_data_rdist_sgi_base();
191 rwp_wait = gic_redist_wait_for_rwp;
192 } else {
193 base = gic_data.dist_base;
194 rwp_wait = gic_dist_wait_for_rwp;
195 }
196
197 writel_relaxed(mask, base + offset + (gic_irq(d) / 32) * 4);
198 rwp_wait();
199}
200
201static void gic_mask_irq(struct irq_data *d)
202{
203 gic_poke_irq(d, GICD_ICENABLER);
204}
205
206static void gic_eoimode1_mask_irq(struct irq_data *d)
207{
208 gic_mask_irq(d);
209 /*
210 * When masking a forwarded interrupt, make sure it is
211 * deactivated as well.
212 *
213 * This ensures that an interrupt that is getting
214 * disabled/masked will not get "stuck", because there is
215 * noone to deactivate it (guest is being terminated).
216 */
217 if (irqd_is_forwarded_to_vcpu(d))
218 gic_poke_irq(d, GICD_ICACTIVER);
219}
220
221static void gic_unmask_irq(struct irq_data *d)
222{
223 gic_poke_irq(d, GICD_ISENABLER);
224}
225
226static int gic_irq_set_irqchip_state(struct irq_data *d,
227 enum irqchip_irq_state which, bool val)
228{
229 u32 reg;
230
231 if (d->hwirq >= gic_data.irq_nr) /* PPI/SPI only */
232 return -EINVAL;
233
234 switch (which) {
235 case IRQCHIP_STATE_PENDING:
236 reg = val ? GICD_ISPENDR : GICD_ICPENDR;
237 break;
238
239 case IRQCHIP_STATE_ACTIVE:
240 reg = val ? GICD_ISACTIVER : GICD_ICACTIVER;
241 break;
242
243 case IRQCHIP_STATE_MASKED:
244 reg = val ? GICD_ICENABLER : GICD_ISENABLER;
245 break;
246
247 default:
248 return -EINVAL;
249 }
250
251 gic_poke_irq(d, reg);
252 return 0;
253}
254
255static int gic_irq_get_irqchip_state(struct irq_data *d,
256 enum irqchip_irq_state which, bool *val)
257{
258 if (d->hwirq >= gic_data.irq_nr) /* PPI/SPI only */
259 return -EINVAL;
260
261 switch (which) {
262 case IRQCHIP_STATE_PENDING:
263 *val = gic_peek_irq(d, GICD_ISPENDR);
264 break;
265
266 case IRQCHIP_STATE_ACTIVE:
267 *val = gic_peek_irq(d, GICD_ISACTIVER);
268 break;
269
270 case IRQCHIP_STATE_MASKED:
271 *val = !gic_peek_irq(d, GICD_ISENABLER);
272 break;
273
274 default:
275 return -EINVAL;
276 }
277
278 return 0;
279}
280
281static void gic_eoi_irq(struct irq_data *d)
282{
283 gic_write_eoir(gic_irq(d));
284}
285
286static void gic_eoimode1_eoi_irq(struct irq_data *d)
287{
288 /*
289 * No need to deactivate an LPI, or an interrupt that
290 * is is getting forwarded to a vcpu.
291 */
292 if (gic_irq(d) >= 8192 || irqd_is_forwarded_to_vcpu(d))
293 return;
294 gic_write_dir(gic_irq(d));
295}
296
297static int gic_set_type(struct irq_data *d, unsigned int type)
298{
299 unsigned int irq = gic_irq(d);
300 void (*rwp_wait)(void);
301 void __iomem *base;
302
303 /* Interrupt configuration for SGIs can't be changed */
304 if (irq < 16)
305 return -EINVAL;
306
307 /* SPIs have restrictions on the supported types */
308 if (irq >= 32 && type != IRQ_TYPE_LEVEL_HIGH &&
309 type != IRQ_TYPE_EDGE_RISING)
310 return -EINVAL;
311
312 if (gic_irq_in_rdist(d)) {
313 base = gic_data_rdist_sgi_base();
314 rwp_wait = gic_redist_wait_for_rwp;
315 } else {
316 base = gic_data.dist_base;
317 rwp_wait = gic_dist_wait_for_rwp;
318 }
319
320 return gic_configure_irq(irq, type, base, rwp_wait);
321}
322
323static int gic_irq_set_vcpu_affinity(struct irq_data *d, void *vcpu)
324{
325 if (vcpu)
326 irqd_set_forwarded_to_vcpu(d);
327 else
328 irqd_clr_forwarded_to_vcpu(d);
329 return 0;
330}
331
332static u64 gic_mpidr_to_affinity(unsigned long mpidr)
333{
334 u64 aff;
335
336 aff = ((u64)MPIDR_AFFINITY_LEVEL(mpidr, 3) << 32 |
337 MPIDR_AFFINITY_LEVEL(mpidr, 2) << 16 |
338 MPIDR_AFFINITY_LEVEL(mpidr, 1) << 8 |
339 MPIDR_AFFINITY_LEVEL(mpidr, 0));
340
341 return aff;
342}
343
344static asmlinkage void __exception_irq_entry gic_handle_irq(struct pt_regs *regs)
345{
346 u32 irqnr;
347
348 do {
349 irqnr = gic_read_iar();
350
351 if (likely(irqnr > 15 && irqnr < 1020) || irqnr >= 8192) {
352 int err;
353
354 if (static_key_true(&supports_deactivate))
355 gic_write_eoir(irqnr);
356 else
357 isb();
358
359 err = handle_domain_irq(gic_data.domain, irqnr, regs);
360 if (err) {
361 WARN_ONCE(true, "Unexpected interrupt received!\n");
362 if (static_key_true(&supports_deactivate)) {
363 if (irqnr < 8192)
364 gic_write_dir(irqnr);
365 } else {
366 gic_write_eoir(irqnr);
367 }
368 }
369 continue;
370 }
371 if (irqnr < 16) {
372 gic_write_eoir(irqnr);
373 if (static_key_true(&supports_deactivate))
374 gic_write_dir(irqnr);
375#ifdef CONFIG_SMP
376 /*
377 * Unlike GICv2, we don't need an smp_rmb() here.
378 * The control dependency from gic_read_iar to
379 * the ISB in gic_write_eoir is enough to ensure
380 * that any shared data read by handle_IPI will
381 * be read after the ACK.
382 */
383 handle_IPI(irqnr, regs);
384#else
385 WARN_ONCE(true, "Unexpected SGI received!\n");
386#endif
387 continue;
388 }
389 } while (irqnr != ICC_IAR1_EL1_SPURIOUS);
390}
391
392static void __init gic_dist_init(void)
393{
394 unsigned int i;
395#ifndef CONFIG_MTK_GIC_TARGET_ALL
396 u64 affinity;
397#endif
398 void __iomem *base = gic_data.dist_base;
399
400 /* Disable the distributor */
401 writel_relaxed(0, base + GICD_CTLR);
402 gic_dist_wait_for_rwp();
403
404 /*
405 * Configure SPIs as non-secure Group-1. This will only matter
406 * if the GIC only has a single security state. This will not
407 * do the right thing if the kernel is running in secure mode,
408 * but that's not the intended use case anyway.
409 */
410 for (i = 32; i < gic_data.irq_nr; i += 32)
411 writel_relaxed(~0, base + GICD_IGROUPR + i / 8);
412
413 gic_dist_config(base, gic_data.irq_nr, gic_dist_wait_for_rwp);
414
415 /* Enable distributor with ARE, Group1 */
416 writel_relaxed(GICD_CTLR_ARE_NS | GICD_CTLR_ENABLE_G1A | GICD_CTLR_ENABLE_G1,
417 base + GICD_CTLR);
418
419#ifndef CONFIG_MTK_GIC_TARGET_ALL
420 /*
421 * Set all global interrupts to the boot CPU only. ARE must be
422 * enabled.
423 */
424 affinity = gic_mpidr_to_affinity(cpu_logical_map(smp_processor_id()));
425 for (i = 32; i < gic_data.irq_nr; i++)
426 gic_write_irouter(affinity, base + GICD_IROUTER + i * 8);
427#else
428 /* default set target all for all SPI */
429 for (i = 32; i < gic_data.irq_nr; i++)
430 gic_write_irouter(GICD_IROUTER_SPI_MODE_ANY,
431 base + GICD_IROUTER + i * 8);
432#endif
433}
434
435static int gic_iterate_rdists(int (*fn)(struct redist_region *, void __iomem *))
436{
437 int ret = -ENODEV;
438 int i;
439
440 for (i = 0; i < gic_data.nr_redist_regions; i++) {
441 void __iomem *ptr = gic_data.redist_regions[i].redist_base;
442 u64 typer;
443 u32 reg;
444
445 reg = readl_relaxed(ptr + GICR_PIDR2) & GIC_PIDR2_ARCH_MASK;
446 if (reg != GIC_PIDR2_ARCH_GICv3 &&
447 reg != GIC_PIDR2_ARCH_GICv4) { /* We're in trouble... */
448 pr_warn("No redistributor present @%p\n", ptr);
449 break;
450 }
451
452 do {
453 typer = gic_read_typer(ptr + GICR_TYPER);
454 ret = fn(gic_data.redist_regions + i, ptr);
455 if (!ret)
456 return 0;
457
458 if (gic_data.redist_regions[i].single_redist)
459 break;
460
461 if (gic_data.redist_stride) {
462 ptr += gic_data.redist_stride;
463 } else {
464 ptr += SZ_64K * 2; /* Skip RD_base + SGI_base */
465 if (typer & GICR_TYPER_VLPIS)
466 ptr += SZ_64K * 2; /* Skip VLPI_base + reserved page */
467 }
468 } while (!(typer & GICR_TYPER_LAST));
469 }
470
471 return ret ? -ENODEV : 0;
472}
473
474static int __gic_populate_rdist(struct redist_region *region, void __iomem *ptr)
475{
476 unsigned long mpidr = cpu_logical_map(smp_processor_id());
477 u64 typer;
478 u32 aff;
479
480 /*
481 * Convert affinity to a 32bit value that can be matched to
482 * GICR_TYPER bits [63:32].
483 */
484 aff = (MPIDR_AFFINITY_LEVEL(mpidr, 3) << 24 |
485 MPIDR_AFFINITY_LEVEL(mpidr, 2) << 16 |
486 MPIDR_AFFINITY_LEVEL(mpidr, 1) << 8 |
487 MPIDR_AFFINITY_LEVEL(mpidr, 0));
488
489 typer = gic_read_typer(ptr + GICR_TYPER);
490 if ((typer >> 32) == aff) {
491 u64 offset = ptr - region->redist_base;
492 gic_data_rdist_rd_base() = ptr;
493 gic_data_rdist()->phys_base = region->phys_base + offset;
494
495 pr_info("CPU%d: found redistributor %lx region %d:%pa\n",
496 smp_processor_id(), mpidr,
497 (int)(region - gic_data.redist_regions),
498 &gic_data_rdist()->phys_base);
499 return 0;
500 }
501
502 /* Try next one */
503 return 1;
504}
505
506static int gic_populate_rdist(void)
507{
508 if (gic_iterate_rdists(__gic_populate_rdist) == 0)
509 return 0;
510
511 /* We couldn't even deal with ourselves... */
512 WARN(true, "CPU%d: mpidr %lx has no re-distributor!\n",
513 smp_processor_id(),
514 (unsigned long)cpu_logical_map(smp_processor_id()));
515 return -ENODEV;
516}
517
518static int __gic_update_vlpi_properties(struct redist_region *region,
519 void __iomem *ptr)
520{
521 u64 typer = gic_read_typer(ptr + GICR_TYPER);
522 gic_data.rdists.has_vlpis &= !!(typer & GICR_TYPER_VLPIS);
523 gic_data.rdists.has_direct_lpi &= !!(typer & GICR_TYPER_DirectLPIS);
524
525 return 1;
526}
527
528static void gic_update_vlpi_properties(void)
529{
530 gic_iterate_rdists(__gic_update_vlpi_properties);
531 pr_info("%sVLPI support, %sdirect LPI support\n",
532 !gic_data.rdists.has_vlpis ? "no " : "",
533 !gic_data.rdists.has_direct_lpi ? "no " : "");
534}
535
536static void gic_cpu_sys_reg_init(void)
537{
538 /*
539 * Need to check that the SRE bit has actually been set. If
540 * not, it means that SRE is disabled at EL2. We're going to
541 * die painfully, and there is nothing we can do about it.
542 *
543 * Kindly inform the luser.
544 */
545 if (!gic_enable_sre())
546 pr_err("GIC: unable to set SRE (disabled at EL2), panic ahead\n");
547
548 /* Set priority mask register */
549 gic_write_pmr(DEFAULT_PMR_VALUE);
550
551 /*
552 * Some firmwares hand over to the kernel with the BPR changed from
553 * its reset value (and with a value large enough to prevent
554 * any pre-emptive interrupts from working at all). Writing a zero
555 * to BPR restores is reset value.
556 */
557 gic_write_bpr1(0);
558
559 if (static_key_true(&supports_deactivate)) {
560 /* EOI drops priority only (mode 1) */
561 gic_write_ctlr(ICC_CTLR_EL1_EOImode_drop);
562 } else {
563 /* EOI deactivates interrupt too (mode 0) */
564 gic_write_ctlr(ICC_CTLR_EL1_EOImode_drop_dir);
565 }
566
567 /* ... and let's hit the road... */
568 gic_write_grpen1(1);
569}
570
571static int gic_dist_supports_lpis(void)
572{
573 return !!(readl_relaxed(gic_data.dist_base + GICD_TYPER) & GICD_TYPER_LPIS);
574}
575
576static void gic_cpu_init(void)
577{
578 void __iomem *rbase;
579
580 /* Register ourselves with the rest of the world */
581 if (gic_populate_rdist())
582 return;
583
584 gic_enable_redist(true);
585
586 rbase = gic_data_rdist_sgi_base();
587
588 /* Configure SGIs/PPIs as non-secure Group-1 */
589 writel_relaxed(~0, rbase + GICR_IGROUPR0);
590
591 gic_cpu_config(rbase, gic_redist_wait_for_rwp);
592
593 /* Give LPIs a spin */
594 if (IS_ENABLED(CONFIG_ARM_GIC_V3_ITS) && gic_dist_supports_lpis())
595 its_cpu_init();
596
597 /* initialise system registers */
598 gic_cpu_sys_reg_init();
599}
600
601#ifdef CONFIG_SMP
602
603static int gic_starting_cpu(unsigned int cpu)
604{
605 gic_cpu_init();
606 return 0;
607}
608
609static u16 gic_compute_target_list(int *base_cpu, const struct cpumask *mask,
610 unsigned long cluster_id)
611{
612 int next_cpu, cpu = *base_cpu;
613 unsigned long mpidr = cpu_logical_map(cpu);
614 u16 tlist = 0;
615
616 while (cpu < nr_cpu_ids) {
617 /*
618 * If we ever get a cluster of more than 16 CPUs, just
619 * scream and skip that CPU.
620 */
621 if (WARN_ON((mpidr & 0xff) >= 16))
622 goto out;
623
624 tlist |= 1 << (mpidr & 0xf);
625
626 next_cpu = cpumask_next(cpu, mask);
627 if (next_cpu >= nr_cpu_ids)
628 goto out;
629 cpu = next_cpu;
630
631 mpidr = cpu_logical_map(cpu);
632
633 if (cluster_id != (mpidr & ~0xffUL)) {
634 cpu--;
635 goto out;
636 }
637 }
638out:
639 *base_cpu = cpu;
640 return tlist;
641}
642
643#define MPIDR_TO_SGI_AFFINITY(cluster_id, level) \
644 (MPIDR_AFFINITY_LEVEL(cluster_id, level) \
645 << ICC_SGI1R_AFFINITY_## level ##_SHIFT)
646
647static void gic_send_sgi(u64 cluster_id, u16 tlist, unsigned int irq)
648{
649 u64 val;
650
651 val = (MPIDR_TO_SGI_AFFINITY(cluster_id, 3) |
652 MPIDR_TO_SGI_AFFINITY(cluster_id, 2) |
653 irq << ICC_SGI1R_SGI_ID_SHIFT |
654 MPIDR_TO_SGI_AFFINITY(cluster_id, 1) |
655 tlist << ICC_SGI1R_TARGET_LIST_SHIFT);
656
657 pr_devel("CPU%d: ICC_SGI1R_EL1 %llx\n", smp_processor_id(), val);
658 gic_write_sgi1r(val);
659}
660
661static void gic_raise_softirq(const struct cpumask *mask, unsigned int irq)
662{
663 int cpu;
664
665 if (WARN_ON(irq >= 16))
666 return;
667
668 /*
669 * Ensure that stores to Normal memory are visible to the
670 * other CPUs before issuing the IPI.
671 */
672 wmb();
673
674 for_each_cpu(cpu, mask) {
675 unsigned long cluster_id = cpu_logical_map(cpu) & ~0xffUL;
676 u16 tlist;
677
678 tlist = gic_compute_target_list(&cpu, mask, cluster_id);
679 gic_send_sgi(cluster_id, tlist, irq);
680 }
681
682 /* Force the above writes to ICC_SGI1R_EL1 to be executed */
683 isb();
684}
685
686static void gic_smp_init(void)
687{
688 set_smp_cross_call(gic_raise_softirq);
689 cpuhp_setup_state_nocalls(CPUHP_AP_IRQ_GIC_STARTING,
690 "irqchip/arm/gicv3:starting",
691 gic_starting_cpu, NULL);
692}
693
694static int gic_set_affinity(struct irq_data *d, const struct cpumask *mask_val,
695 bool force)
696{
697 unsigned int cpu;
698 void __iomem *reg;
699 int enabled;
700 u64 val;
701
702 if (force)
703 cpu = cpumask_first(mask_val);
704 else
705 cpu = cpumask_any_and(mask_val, cpu_online_mask);
706
707 if (cpu >= nr_cpu_ids)
708 return -EINVAL;
709
710 if (gic_irq_in_rdist(d))
711 return -EINVAL;
712
713#ifndef CONFIG_MTK_GIC_TARGET_ALL
714 /* If interrupt was enabled, disable it first */
715 enabled = gic_peek_irq(d, GICD_ISENABLER);
716 if (enabled)
717 gic_mask_irq(d);
718
719 reg = gic_dist_base(d) + GICD_IROUTER + (gic_irq(d) * 8);
720 val = gic_mpidr_to_affinity(cpu_logical_map(cpu));
721
722 gic_write_irouter(val, reg);
723
724 /*
725 * If the interrupt was enabled, enabled it again. Otherwise,
726 * just wait for the distributor to have digested our changes.
727 */
728 if (enabled)
729 gic_unmask_irq(d);
730 else
731 gic_dist_wait_for_rwp();
732
733 irq_data_update_effective_affinity(d, cpumask_of(cpu));
734
735 return IRQ_SET_MASK_OK_DONE;
736#else
737 /*
738 * no need to update when:
739 * input mask is equal to the current setting
740 */
741 if (cpumask_equal(d->common->affinity, mask_val))
742 return IRQ_SET_MASK_OK_NOCOPY;
743
744 /*
745 * cpumask_first_and() returns >= nr_cpu_ids
746 * when the intersection
747 * of inputs is an empty set -> return error
748 * when this is not a "forced" update
749 */
750 if (!force &&
751 (cpumask_first_and(mask_val, cpu_online_mask) >= nr_cpu_ids))
752 return -EINVAL;
753
754 if (gic_irq_in_rdist(d))
755 return -EINVAL;
756
757 /* If interrupt was enabled, disable it first */
758 enabled = gic_peek_irq(d, GICD_ISENABLER);
759 if (enabled)
760 gic_mask_irq(d);
761
762 reg = gic_dist_base(d) + GICD_IROUTER + (gic_irq(d) * 8);
763
764 /* GICv3 supports target is 1 or all */
765 if (cpumask_weight(mask_val) > 1)
766 val = GICD_IROUTER_SPI_MODE_ANY;
767 else
768 val = gic_mpidr_to_affinity(cpu_logical_map(cpu));
769
770 gic_write_irouter(val, reg);
771
772 /*
773 * If the interrupt was enabled, enabled it again. Otherwise,
774 * just wait for the distributor to have digested our changes.
775 */
776 if (enabled)
777 gic_unmask_irq(d);
778 else
779 gic_dist_wait_for_rwp();
780
781 return IRQ_SET_MASK_OK;
782#endif
783}
784#else
785#define gic_set_affinity NULL
786#define gic_smp_init() do { } while(0)
787#endif
788
789#ifdef CONFIG_CPU_PM
790/* Check whether it's single security state view */
791static bool gic_dist_security_disabled(void)
792{
793 return readl_relaxed(gic_data.dist_base + GICD_CTLR) & GICD_CTLR_DS;
794}
795
796static int gic_cpu_pm_notifier(struct notifier_block *self,
797 unsigned long cmd, void *v)
798{
799 if (cmd == CPU_PM_EXIT) {
800 if (gic_dist_security_disabled())
801 gic_enable_redist(true);
802 gic_cpu_sys_reg_init();
803 } else if (cmd == CPU_PM_ENTER && gic_dist_security_disabled()) {
804 gic_write_grpen1(0);
805 gic_enable_redist(false);
806 }
807 return NOTIFY_OK;
808}
809
810static struct notifier_block gic_cpu_pm_notifier_block = {
811 .notifier_call = gic_cpu_pm_notifier,
812};
813
814static void gic_cpu_pm_init(void)
815{
816 cpu_pm_register_notifier(&gic_cpu_pm_notifier_block);
817}
818
819#else
820static inline void gic_cpu_pm_init(void) { }
821#endif /* CONFIG_CPU_PM */
822
823static struct irq_chip gic_chip = {
824 .name = "GICv3",
825 .irq_mask = gic_mask_irq,
826 .irq_unmask = gic_unmask_irq,
827 .irq_eoi = gic_eoi_irq,
828 .irq_set_type = gic_set_type,
829 .irq_set_affinity = gic_set_affinity,
830 .irq_get_irqchip_state = gic_irq_get_irqchip_state,
831 .irq_set_irqchip_state = gic_irq_set_irqchip_state,
832 .flags = IRQCHIP_SET_TYPE_MASKED,
833};
834
835static struct irq_chip gic_eoimode1_chip = {
836 .name = "GICv3",
837 .irq_mask = gic_eoimode1_mask_irq,
838 .irq_unmask = gic_unmask_irq,
839 .irq_eoi = gic_eoimode1_eoi_irq,
840 .irq_set_type = gic_set_type,
841 .irq_set_affinity = gic_set_affinity,
842 .irq_get_irqchip_state = gic_irq_get_irqchip_state,
843 .irq_set_irqchip_state = gic_irq_set_irqchip_state,
844 .irq_set_vcpu_affinity = gic_irq_set_vcpu_affinity,
845 .flags = IRQCHIP_SET_TYPE_MASKED,
846};
847
848#define GIC_ID_NR (1U << gic_data.rdists.id_bits)
849
850static int gic_irq_domain_map(struct irq_domain *d, unsigned int irq,
851 irq_hw_number_t hw)
852{
853 struct irq_chip *chip = &gic_chip;
854
855 if (static_key_true(&supports_deactivate))
856 chip = &gic_eoimode1_chip;
857
858 /* SGIs are private to the core kernel */
859 if (hw < 16)
860 return -EPERM;
861 /* Nothing here */
862 if (hw >= gic_data.irq_nr && hw < 8192)
863 return -EPERM;
864 /* Off limits */
865 if (hw >= GIC_ID_NR)
866 return -EPERM;
867
868 /* PPIs */
869 if (hw < 32) {
870 irq_set_percpu_devid(irq);
871 irq_domain_set_info(d, irq, hw, chip, d->host_data,
872 handle_percpu_devid_irq, NULL, NULL);
873 irq_set_status_flags(irq, IRQ_NOAUTOEN);
874 }
875 /* SPIs */
876 if (hw >= 32 && hw < gic_data.irq_nr) {
877 irq_domain_set_info(d, irq, hw, chip, d->host_data,
878 handle_fasteoi_irq, NULL, NULL);
879 irq_set_probe(irq);
880 irqd_set_single_target(irq_desc_get_irq_data(irq_to_desc(irq)));
881 }
882 /* LPIs */
883 if (hw >= 8192 && hw < GIC_ID_NR) {
884 if (!gic_dist_supports_lpis())
885 return -EPERM;
886 irq_domain_set_info(d, irq, hw, chip, d->host_data,
887 handle_fasteoi_irq, NULL, NULL);
888 }
889
890 return 0;
891}
892
893static int gic_irq_domain_translate(struct irq_domain *d,
894 struct irq_fwspec *fwspec,
895 unsigned long *hwirq,
896 unsigned int *type)
897{
898 if (is_of_node(fwspec->fwnode)) {
899 if (fwspec->param_count < 3)
900 return -EINVAL;
901
902 switch (fwspec->param[0]) {
903 case 0: /* SPI */
904 *hwirq = fwspec->param[1] + 32;
905 break;
906 case 1: /* PPI */
907 *hwirq = fwspec->param[1] + 16;
908 break;
909 case GIC_IRQ_TYPE_LPI: /* LPI */
910 *hwirq = fwspec->param[1];
911 break;
912 default:
913 return -EINVAL;
914 }
915
916 *type = fwspec->param[2] & IRQ_TYPE_SENSE_MASK;
917 return 0;
918 }
919
920 if (is_fwnode_irqchip(fwspec->fwnode)) {
921 if(fwspec->param_count != 2)
922 return -EINVAL;
923
924 *hwirq = fwspec->param[0];
925 *type = fwspec->param[1];
926 return 0;
927 }
928
929 return -EINVAL;
930}
931
932static int gic_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
933 unsigned int nr_irqs, void *arg)
934{
935 int i, ret;
936 irq_hw_number_t hwirq;
937 unsigned int type = IRQ_TYPE_NONE;
938 struct irq_fwspec *fwspec = arg;
939
940 ret = gic_irq_domain_translate(domain, fwspec, &hwirq, &type);
941 if (ret)
942 return ret;
943
944 for (i = 0; i < nr_irqs; i++) {
945 ret = gic_irq_domain_map(domain, virq + i, hwirq + i);
946 if (ret)
947 return ret;
948 }
949
950 return 0;
951}
952
953static void gic_irq_domain_free(struct irq_domain *domain, unsigned int virq,
954 unsigned int nr_irqs)
955{
956 int i;
957
958 for (i = 0; i < nr_irqs; i++) {
959 struct irq_data *d = irq_domain_get_irq_data(domain, virq + i);
960 irq_set_handler(virq + i, NULL);
961 irq_domain_reset_irq_data(d);
962 }
963}
964
965static int gic_irq_domain_select(struct irq_domain *d,
966 struct irq_fwspec *fwspec,
967 enum irq_domain_bus_token bus_token)
968{
969 /* Not for us */
970 if (fwspec->fwnode != d->fwnode)
971 return 0;
972
973 /* If this is not DT, then we have a single domain */
974 if (!is_of_node(fwspec->fwnode))
975 return 1;
976
977 /*
978 * If this is a PPI and we have a 4th (non-null) parameter,
979 * then we need to match the partition domain.
980 */
981 if (fwspec->param_count >= 4 &&
982 fwspec->param[0] == 1 && fwspec->param[3] != 0)
983 return d == partition_get_domain(gic_data.ppi_descs[fwspec->param[1]]);
984
985 return d == gic_data.domain;
986}
987
988static const struct irq_domain_ops gic_irq_domain_ops = {
989 .translate = gic_irq_domain_translate,
990 .alloc = gic_irq_domain_alloc,
991 .free = gic_irq_domain_free,
992 .select = gic_irq_domain_select,
993};
994
995static int partition_domain_translate(struct irq_domain *d,
996 struct irq_fwspec *fwspec,
997 unsigned long *hwirq,
998 unsigned int *type)
999{
1000 struct device_node *np;
1001 int ret;
1002
1003 np = of_find_node_by_phandle(fwspec->param[3]);
1004 if (WARN_ON(!np))
1005 return -EINVAL;
1006
1007 ret = partition_translate_id(gic_data.ppi_descs[fwspec->param[1]],
1008 of_node_to_fwnode(np));
1009 if (ret < 0)
1010 return ret;
1011
1012 *hwirq = ret;
1013 *type = fwspec->param[2] & IRQ_TYPE_SENSE_MASK;
1014
1015 return 0;
1016}
1017
1018static const struct irq_domain_ops partition_domain_ops = {
1019 .translate = partition_domain_translate,
1020 .select = gic_irq_domain_select,
1021};
1022
1023static int __init gic_init_bases(void __iomem *dist_base,
1024 struct redist_region *rdist_regs,
1025 u32 nr_redist_regions,
1026 u64 redist_stride,
1027 struct fwnode_handle *handle)
1028{
1029 u32 typer;
1030 int gic_irqs;
1031 int err;
1032
1033 if (!is_hyp_mode_available())
1034 static_key_slow_dec(&supports_deactivate);
1035
1036 if (static_key_true(&supports_deactivate))
1037 pr_info("GIC: Using split EOI/Deactivate mode\n");
1038
1039 gic_data.fwnode = handle;
1040 gic_data.dist_base = dist_base;
1041 gic_data.redist_regions = rdist_regs;
1042 gic_data.nr_redist_regions = nr_redist_regions;
1043 gic_data.redist_stride = redist_stride;
1044
1045 /*
1046 * Find out how many interrupts are supported.
1047 * The GIC only supports up to 1020 interrupt sources (SGI+PPI+SPI)
1048 */
1049 typer = readl_relaxed(gic_data.dist_base + GICD_TYPER);
1050 gic_data.rdists.id_bits = GICD_TYPER_ID_BITS(typer);
1051 gic_irqs = GICD_TYPER_IRQS(typer);
1052 if (gic_irqs > 1020)
1053 gic_irqs = 1020;
1054 gic_data.irq_nr = gic_irqs;
1055
1056 gic_data.domain = irq_domain_create_tree(handle, &gic_irq_domain_ops,
1057 &gic_data);
1058 gic_data.rdists.rdist = alloc_percpu(typeof(*gic_data.rdists.rdist));
1059 gic_data.rdists.has_vlpis = true;
1060 gic_data.rdists.has_direct_lpi = true;
1061
1062 if (WARN_ON(!gic_data.domain) || WARN_ON(!gic_data.rdists.rdist)) {
1063 err = -ENOMEM;
1064 goto out_free;
1065 }
1066
1067 set_handle_irq(gic_handle_irq);
1068
1069 gic_update_vlpi_properties();
1070
1071 if (IS_ENABLED(CONFIG_ARM_GIC_V3_ITS) && gic_dist_supports_lpis())
1072 its_init(handle, &gic_data.rdists, gic_data.domain);
1073
1074 gic_smp_init();
1075 gic_dist_init();
1076 gic_cpu_init();
1077 gic_cpu_pm_init();
1078
1079 return 0;
1080
1081out_free:
1082 if (gic_data.domain)
1083 irq_domain_remove(gic_data.domain);
1084 free_percpu(gic_data.rdists.rdist);
1085 return err;
1086}
1087
1088static int __init gic_validate_dist_version(void __iomem *dist_base)
1089{
1090 u32 reg = readl_relaxed(dist_base + GICD_PIDR2) & GIC_PIDR2_ARCH_MASK;
1091
1092 if (reg != GIC_PIDR2_ARCH_GICv3 && reg != GIC_PIDR2_ARCH_GICv4)
1093 return -ENODEV;
1094
1095 return 0;
1096}
1097
1098static int get_cpu_number(struct device_node *dn)
1099{
1100 const __be32 *cell;
1101 u64 hwid;
1102 int cpu;
1103
1104 cell = of_get_property(dn, "reg", NULL);
1105 if (!cell)
1106 return -1;
1107
1108 hwid = of_read_number(cell, of_n_addr_cells(dn));
1109
1110 /*
1111 * Non affinity bits must be set to 0 in the DT
1112 */
1113 if (hwid & ~MPIDR_HWID_BITMASK)
1114 return -1;
1115
1116 for_each_possible_cpu(cpu)
1117 if (cpu_logical_map(cpu) == hwid)
1118 return cpu;
1119
1120 return -1;
1121}
1122
1123/* Create all possible partitions at boot time */
1124static void __init gic_populate_ppi_partitions(struct device_node *gic_node)
1125{
1126 struct device_node *parts_node, *child_part;
1127 int part_idx = 0, i;
1128 int nr_parts;
1129 struct partition_affinity *parts;
1130
1131 parts_node = of_get_child_by_name(gic_node, "ppi-partitions");
1132 if (!parts_node)
1133 return;
1134
1135 nr_parts = of_get_child_count(parts_node);
1136
1137 if (!nr_parts)
1138 goto out_put_node;
1139
1140 parts = kzalloc(sizeof(*parts) * nr_parts, GFP_KERNEL);
1141 if (WARN_ON(!parts))
1142 goto out_put_node;
1143
1144 for_each_child_of_node(parts_node, child_part) {
1145 struct partition_affinity *part;
1146 int n;
1147
1148 part = &parts[part_idx];
1149
1150 part->partition_id = of_node_to_fwnode(child_part);
1151
1152 pr_info("GIC: PPI partition %s[%d] { ",
1153 child_part->name, part_idx);
1154
1155 n = of_property_count_elems_of_size(child_part, "affinity",
1156 sizeof(u32));
1157 WARN_ON(n <= 0);
1158
1159 for (i = 0; i < n; i++) {
1160 int err, cpu;
1161 u32 cpu_phandle;
1162 struct device_node *cpu_node;
1163
1164 err = of_property_read_u32_index(child_part, "affinity",
1165 i, &cpu_phandle);
1166 if (WARN_ON(err))
1167 continue;
1168
1169 cpu_node = of_find_node_by_phandle(cpu_phandle);
1170 if (WARN_ON(!cpu_node))
1171 continue;
1172
1173 cpu = get_cpu_number(cpu_node);
1174 if (WARN_ON(cpu == -1))
1175 continue;
1176
1177 pr_cont("%pOF[%d] ", cpu_node, cpu);
1178
1179 cpumask_set_cpu(cpu, &part->mask);
1180 }
1181
1182 pr_cont("}\n");
1183 part_idx++;
1184 }
1185
1186 for (i = 0; i < 16; i++) {
1187 unsigned int irq;
1188 struct partition_desc *desc;
1189 struct irq_fwspec ppi_fwspec = {
1190 .fwnode = gic_data.fwnode,
1191 .param_count = 3,
1192 .param = {
1193 [0] = 1,
1194 [1] = i,
1195 [2] = IRQ_TYPE_NONE,
1196 },
1197 };
1198
1199 irq = irq_create_fwspec_mapping(&ppi_fwspec);
1200 if (WARN_ON(!irq))
1201 continue;
1202 desc = partition_create_desc(gic_data.fwnode, parts, nr_parts,
1203 irq, &partition_domain_ops);
1204 if (WARN_ON(!desc))
1205 continue;
1206
1207 gic_data.ppi_descs[i] = desc;
1208 }
1209
1210out_put_node:
1211 of_node_put(parts_node);
1212}
1213
1214static void __init gic_of_setup_kvm_info(struct device_node *node)
1215{
1216 int ret;
1217 struct resource r;
1218 u32 gicv_idx;
1219
1220 gic_v3_kvm_info.type = GIC_V3;
1221
1222 gic_v3_kvm_info.maint_irq = irq_of_parse_and_map(node, 0);
1223 if (!gic_v3_kvm_info.maint_irq)
1224 return;
1225
1226 if (of_property_read_u32(node, "#redistributor-regions",
1227 &gicv_idx))
1228 gicv_idx = 1;
1229
1230 gicv_idx += 3; /* Also skip GICD, GICC, GICH */
1231 ret = of_address_to_resource(node, gicv_idx, &r);
1232 if (!ret)
1233 gic_v3_kvm_info.vcpu = r;
1234
1235 gic_v3_kvm_info.has_v4 = gic_data.rdists.has_vlpis;
1236 gic_set_kvm_info(&gic_v3_kvm_info);
1237}
1238
1239static int __init gic_of_init(struct device_node *node, struct device_node *parent)
1240{
1241 void __iomem *dist_base;
1242 struct redist_region *rdist_regs;
1243 u64 redist_stride;
1244 u32 nr_redist_regions;
1245 int err, i;
1246
1247 dist_base = of_iomap(node, 0);
1248 if (!dist_base) {
1249 pr_err("%pOF: unable to map gic dist registers\n", node);
1250 return -ENXIO;
1251 }
1252
1253 err = gic_validate_dist_version(dist_base);
1254 if (err) {
1255 pr_err("%pOF: no distributor detected, giving up\n", node);
1256 goto out_unmap_dist;
1257 }
1258
1259 if (of_property_read_u32(node, "#redistributor-regions", &nr_redist_regions))
1260 nr_redist_regions = 1;
1261
1262 rdist_regs = kzalloc(sizeof(*rdist_regs) * nr_redist_regions, GFP_KERNEL);
1263 if (!rdist_regs) {
1264 err = -ENOMEM;
1265 goto out_unmap_dist;
1266 }
1267
1268 for (i = 0; i < nr_redist_regions; i++) {
1269 struct resource res;
1270 int ret;
1271
1272 ret = of_address_to_resource(node, 1 + i, &res);
1273 rdist_regs[i].redist_base = of_iomap(node, 1 + i);
1274 if (ret || !rdist_regs[i].redist_base) {
1275 pr_err("%pOF: couldn't map region %d\n", node, i);
1276 err = -ENODEV;
1277 goto out_unmap_rdist;
1278 }
1279 rdist_regs[i].phys_base = res.start;
1280 }
1281
1282 if (of_property_read_u64(node, "redistributor-stride", &redist_stride))
1283 redist_stride = 0;
1284
1285 err = gic_init_bases(dist_base, rdist_regs, nr_redist_regions,
1286 redist_stride, &node->fwnode);
1287 if (err)
1288 goto out_unmap_rdist;
1289
1290 gic_populate_ppi_partitions(node);
1291 gic_of_setup_kvm_info(node);
1292 return 0;
1293
1294out_unmap_rdist:
1295 for (i = 0; i < nr_redist_regions; i++)
1296 if (rdist_regs[i].redist_base)
1297 iounmap(rdist_regs[i].redist_base);
1298 kfree(rdist_regs);
1299out_unmap_dist:
1300 iounmap(dist_base);
1301 return err;
1302}
1303
1304IRQCHIP_DECLARE(gic_v3, "arm,gic-v3", gic_of_init);
1305
1306#ifdef CONFIG_ACPI
1307static struct
1308{
1309 void __iomem *dist_base;
1310 struct redist_region *redist_regs;
1311 u32 nr_redist_regions;
1312 bool single_redist;
1313 int enabled_rdists;
1314 u32 maint_irq;
1315 int maint_irq_mode;
1316 phys_addr_t vcpu_base;
1317} acpi_data __initdata;
1318
1319static void __init
1320gic_acpi_register_redist(phys_addr_t phys_base, void __iomem *redist_base)
1321{
1322 static int count = 0;
1323
1324 acpi_data.redist_regs[count].phys_base = phys_base;
1325 acpi_data.redist_regs[count].redist_base = redist_base;
1326 acpi_data.redist_regs[count].single_redist = acpi_data.single_redist;
1327 count++;
1328}
1329
1330static int __init
1331gic_acpi_parse_madt_redist(struct acpi_subtable_header *header,
1332 const unsigned long end)
1333{
1334 struct acpi_madt_generic_redistributor *redist =
1335 (struct acpi_madt_generic_redistributor *)header;
1336 void __iomem *redist_base;
1337
1338 redist_base = ioremap(redist->base_address, redist->length);
1339 if (!redist_base) {
1340 pr_err("Couldn't map GICR region @%llx\n", redist->base_address);
1341 return -ENOMEM;
1342 }
1343
1344 gic_acpi_register_redist(redist->base_address, redist_base);
1345 return 0;
1346}
1347
1348static int __init
1349gic_acpi_parse_madt_gicc(struct acpi_subtable_header *header,
1350 const unsigned long end)
1351{
1352 struct acpi_madt_generic_interrupt *gicc =
1353 (struct acpi_madt_generic_interrupt *)header;
1354 u32 reg = readl_relaxed(acpi_data.dist_base + GICD_PIDR2) & GIC_PIDR2_ARCH_MASK;
1355 u32 size = reg == GIC_PIDR2_ARCH_GICv4 ? SZ_64K * 4 : SZ_64K * 2;
1356 void __iomem *redist_base;
1357
1358 /* GICC entry which has !ACPI_MADT_ENABLED is not unusable so skip */
1359 if (!(gicc->flags & ACPI_MADT_ENABLED))
1360 return 0;
1361
1362 redist_base = ioremap(gicc->gicr_base_address, size);
1363 if (!redist_base)
1364 return -ENOMEM;
1365
1366 gic_acpi_register_redist(gicc->gicr_base_address, redist_base);
1367 return 0;
1368}
1369
1370static int __init gic_acpi_collect_gicr_base(void)
1371{
1372 acpi_tbl_entry_handler redist_parser;
1373 enum acpi_madt_type type;
1374
1375 if (acpi_data.single_redist) {
1376 type = ACPI_MADT_TYPE_GENERIC_INTERRUPT;
1377 redist_parser = gic_acpi_parse_madt_gicc;
1378 } else {
1379 type = ACPI_MADT_TYPE_GENERIC_REDISTRIBUTOR;
1380 redist_parser = gic_acpi_parse_madt_redist;
1381 }
1382
1383 /* Collect redistributor base addresses in GICR entries */
1384 if (acpi_table_parse_madt(type, redist_parser, 0) > 0)
1385 return 0;
1386
1387 pr_info("No valid GICR entries exist\n");
1388 return -ENODEV;
1389}
1390
1391static int __init gic_acpi_match_gicr(struct acpi_subtable_header *header,
1392 const unsigned long end)
1393{
1394 /* Subtable presence means that redist exists, that's it */
1395 return 0;
1396}
1397
1398static int __init gic_acpi_match_gicc(struct acpi_subtable_header *header,
1399 const unsigned long end)
1400{
1401 struct acpi_madt_generic_interrupt *gicc =
1402 (struct acpi_madt_generic_interrupt *)header;
1403
1404 /*
1405 * If GICC is enabled and has valid gicr base address, then it means
1406 * GICR base is presented via GICC
1407 */
1408 if ((gicc->flags & ACPI_MADT_ENABLED) && gicc->gicr_base_address) {
1409 acpi_data.enabled_rdists++;
1410 return 0;
1411 }
1412
1413 /*
1414 * It's perfectly valid firmware can pass disabled GICC entry, driver
1415 * should not treat as errors, skip the entry instead of probe fail.
1416 */
1417 if (!(gicc->flags & ACPI_MADT_ENABLED))
1418 return 0;
1419
1420 return -ENODEV;
1421}
1422
1423static int __init gic_acpi_count_gicr_regions(void)
1424{
1425 int count;
1426
1427 /*
1428 * Count how many redistributor regions we have. It is not allowed
1429 * to mix redistributor description, GICR and GICC subtables have to be
1430 * mutually exclusive.
1431 */
1432 count = acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_REDISTRIBUTOR,
1433 gic_acpi_match_gicr, 0);
1434 if (count > 0) {
1435 acpi_data.single_redist = false;
1436 return count;
1437 }
1438
1439 count = acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_INTERRUPT,
1440 gic_acpi_match_gicc, 0);
1441 if (count > 0) {
1442 acpi_data.single_redist = true;
1443 count = acpi_data.enabled_rdists;
1444 }
1445
1446 return count;
1447}
1448
1449static bool __init acpi_validate_gic_table(struct acpi_subtable_header *header,
1450 struct acpi_probe_entry *ape)
1451{
1452 struct acpi_madt_generic_distributor *dist;
1453 int count;
1454
1455 dist = (struct acpi_madt_generic_distributor *)header;
1456 if (dist->version != ape->driver_data)
1457 return false;
1458
1459 /* We need to do that exercise anyway, the sooner the better */
1460 count = gic_acpi_count_gicr_regions();
1461 if (count <= 0)
1462 return false;
1463
1464 acpi_data.nr_redist_regions = count;
1465 return true;
1466}
1467
1468static int __init gic_acpi_parse_virt_madt_gicc(struct acpi_subtable_header *header,
1469 const unsigned long end)
1470{
1471 struct acpi_madt_generic_interrupt *gicc =
1472 (struct acpi_madt_generic_interrupt *)header;
1473 int maint_irq_mode;
1474 static int first_madt = true;
1475
1476 /* Skip unusable CPUs */
1477 if (!(gicc->flags & ACPI_MADT_ENABLED))
1478 return 0;
1479
1480 maint_irq_mode = (gicc->flags & ACPI_MADT_VGIC_IRQ_MODE) ?
1481 ACPI_EDGE_SENSITIVE : ACPI_LEVEL_SENSITIVE;
1482
1483 if (first_madt) {
1484 first_madt = false;
1485
1486 acpi_data.maint_irq = gicc->vgic_interrupt;
1487 acpi_data.maint_irq_mode = maint_irq_mode;
1488 acpi_data.vcpu_base = gicc->gicv_base_address;
1489
1490 return 0;
1491 }
1492
1493 /*
1494 * The maintenance interrupt and GICV should be the same for every CPU
1495 */
1496 if ((acpi_data.maint_irq != gicc->vgic_interrupt) ||
1497 (acpi_data.maint_irq_mode != maint_irq_mode) ||
1498 (acpi_data.vcpu_base != gicc->gicv_base_address))
1499 return -EINVAL;
1500
1501 return 0;
1502}
1503
1504static bool __init gic_acpi_collect_virt_info(void)
1505{
1506 int count;
1507
1508 count = acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_INTERRUPT,
1509 gic_acpi_parse_virt_madt_gicc, 0);
1510
1511 return (count > 0);
1512}
1513
1514#define ACPI_GICV3_DIST_MEM_SIZE (SZ_64K)
1515#define ACPI_GICV2_VCTRL_MEM_SIZE (SZ_4K)
1516#define ACPI_GICV2_VCPU_MEM_SIZE (SZ_8K)
1517
1518static void __init gic_acpi_setup_kvm_info(void)
1519{
1520 int irq;
1521
1522 if (!gic_acpi_collect_virt_info()) {
1523 pr_warn("Unable to get hardware information used for virtualization\n");
1524 return;
1525 }
1526
1527 gic_v3_kvm_info.type = GIC_V3;
1528
1529 irq = acpi_register_gsi(NULL, acpi_data.maint_irq,
1530 acpi_data.maint_irq_mode,
1531 ACPI_ACTIVE_HIGH);
1532 if (irq <= 0)
1533 return;
1534
1535 gic_v3_kvm_info.maint_irq = irq;
1536
1537 if (acpi_data.vcpu_base) {
1538 struct resource *vcpu = &gic_v3_kvm_info.vcpu;
1539
1540 vcpu->flags = IORESOURCE_MEM;
1541 vcpu->start = acpi_data.vcpu_base;
1542 vcpu->end = vcpu->start + ACPI_GICV2_VCPU_MEM_SIZE - 1;
1543 }
1544
1545 gic_v3_kvm_info.has_v4 = gic_data.rdists.has_vlpis;
1546 gic_set_kvm_info(&gic_v3_kvm_info);
1547}
1548
1549static int __init
1550gic_acpi_init(struct acpi_subtable_header *header, const unsigned long end)
1551{
1552 struct acpi_madt_generic_distributor *dist;
1553 struct fwnode_handle *domain_handle;
1554 size_t size;
1555 int i, err;
1556
1557 /* Get distributor base address */
1558 dist = (struct acpi_madt_generic_distributor *)header;
1559 acpi_data.dist_base = ioremap(dist->base_address,
1560 ACPI_GICV3_DIST_MEM_SIZE);
1561 if (!acpi_data.dist_base) {
1562 pr_err("Unable to map GICD registers\n");
1563 return -ENOMEM;
1564 }
1565
1566 err = gic_validate_dist_version(acpi_data.dist_base);
1567 if (err) {
1568 pr_err("No distributor detected at @%p, giving up",
1569 acpi_data.dist_base);
1570 goto out_dist_unmap;
1571 }
1572
1573 size = sizeof(*acpi_data.redist_regs) * acpi_data.nr_redist_regions;
1574 acpi_data.redist_regs = kzalloc(size, GFP_KERNEL);
1575 if (!acpi_data.redist_regs) {
1576 err = -ENOMEM;
1577 goto out_dist_unmap;
1578 }
1579
1580 err = gic_acpi_collect_gicr_base();
1581 if (err)
1582 goto out_redist_unmap;
1583
1584 domain_handle = irq_domain_alloc_fwnode(acpi_data.dist_base);
1585 if (!domain_handle) {
1586 err = -ENOMEM;
1587 goto out_redist_unmap;
1588 }
1589
1590 err = gic_init_bases(acpi_data.dist_base, acpi_data.redist_regs,
1591 acpi_data.nr_redist_regions, 0, domain_handle);
1592 if (err)
1593 goto out_fwhandle_free;
1594
1595 acpi_set_irq_model(ACPI_IRQ_MODEL_GIC, domain_handle);
1596 gic_acpi_setup_kvm_info();
1597
1598 return 0;
1599
1600out_fwhandle_free:
1601 irq_domain_free_fwnode(domain_handle);
1602out_redist_unmap:
1603 for (i = 0; i < acpi_data.nr_redist_regions; i++)
1604 if (acpi_data.redist_regs[i].redist_base)
1605 iounmap(acpi_data.redist_regs[i].redist_base);
1606 kfree(acpi_data.redist_regs);
1607out_dist_unmap:
1608 iounmap(acpi_data.dist_base);
1609 return err;
1610}
1611IRQCHIP_ACPI_DECLARE(gic_v3, ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR,
1612 acpi_validate_gic_table, ACPI_MADT_GIC_VERSION_V3,
1613 gic_acpi_init);
1614IRQCHIP_ACPI_DECLARE(gic_v4, ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR,
1615 acpi_validate_gic_table, ACPI_MADT_GIC_VERSION_V4,
1616 gic_acpi_init);
1617IRQCHIP_ACPI_DECLARE(gic_v3_or_v4, ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR,
1618 acpi_validate_gic_table, ACPI_MADT_GIC_VERSION_NONE,
1619 gic_acpi_init);
1620#endif