blob: ca7874579df86de57661ee76b027db6040de8b0a [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright (C) 2002 ARM Limited, All Rights Reserved.
4 *
5 * Interrupt architecture for the GIC:
6 *
7 * o There is one Interrupt Distributor, which receives interrupts
8 * from system devices and sends them to the Interrupt Controllers.
9 *
10 * o There is one CPU Interface per CPU, which sends interrupts sent
11 * by the Distributor, and interrupts generated locally, to the
12 * associated CPU. The base address of the CPU interface is usually
13 * aliased so that the same address points to different chips depending
14 * on the CPU it is accessed from.
15 *
16 * Note that IRQs 0-31 are special - they are local to each CPU.
17 * As such, the enable set/clear, pending set/clear and active bit
18 * registers are banked per-cpu for these sources.
19 */
20#include <linux/init.h>
21#include <linux/kernel.h>
22#include <linux/err.h>
23#include <linux/module.h>
24#include <linux/list.h>
25#include <linux/smp.h>
26#include <linux/cpu.h>
27#include <linux/cpu_pm.h>
28#include <linux/cpumask.h>
29#include <linux/io.h>
30#include <linux/of.h>
31#include <linux/of_address.h>
32#include <linux/of_irq.h>
33#include <linux/acpi.h>
34#include <linux/irqdomain.h>
35#include <linux/interrupt.h>
36#include <linux/percpu.h>
37#include <linux/slab.h>
38#include <linux/irqchip.h>
39#include <linux/irqchip/chained_irq.h>
40#include <linux/irqchip/arm-gic.h>
41
42#include <asm/cputype.h>
43#include <asm/irq.h>
44#include <asm/exception.h>
45#include <asm/smp_plat.h>
46#include <asm/virt.h>
47
48#include "irq-gic-common.h"
49
50#ifdef CONFIG_ARM64
51#include <asm/cpufeature.h>
52
53static void gic_check_cpu_features(void)
54{
55 WARN_TAINT_ONCE(this_cpu_has_cap(ARM64_HAS_SYSREG_GIC_CPUIF),
56 TAINT_CPU_OUT_OF_SPEC,
57 "GICv3 system registers enabled, broken firmware!\n");
58}
59#else
60#define gic_check_cpu_features() do { } while(0)
61#endif
62
63union gic_base {
64 void __iomem *common_base;
65 void __iomem * __percpu *percpu_base;
66};
67
68struct gic_chip_data {
69 struct irq_chip chip;
70 union gic_base dist_base;
71 union gic_base cpu_base;
72 void __iomem *raw_dist_base;
73 void __iomem *raw_cpu_base;
74 u32 percpu_offset;
75#if defined(CONFIG_CPU_PM) || defined(CONFIG_ARM_GIC_PM)
76 u32 saved_spi_enable[DIV_ROUND_UP(1020, 32)];
77 u32 saved_spi_active[DIV_ROUND_UP(1020, 32)];
78 u32 saved_spi_conf[DIV_ROUND_UP(1020, 16)];
79 u32 saved_spi_target[DIV_ROUND_UP(1020, 4)];
80 u32 __percpu *saved_ppi_enable;
81 u32 __percpu *saved_ppi_active;
82 u32 __percpu *saved_ppi_conf;
83#endif
84 struct irq_domain *domain;
85 unsigned int gic_irqs;
86};
87
88#ifdef CONFIG_BL_SWITCHER
89
90static DEFINE_RAW_SPINLOCK(cpu_map_lock);
91
92#define gic_lock_irqsave(f) \
93 raw_spin_lock_irqsave(&cpu_map_lock, (f))
94#define gic_unlock_irqrestore(f) \
95 raw_spin_unlock_irqrestore(&cpu_map_lock, (f))
96
97#define gic_lock() raw_spin_lock(&cpu_map_lock)
98#define gic_unlock() raw_spin_unlock(&cpu_map_lock)
99
100#else
101
102#define gic_lock_irqsave(f) do { (void)(f); } while(0)
103#define gic_unlock_irqrestore(f) do { (void)(f); } while(0)
104
105#define gic_lock() do { } while(0)
106#define gic_unlock() do { } while(0)
107
108#endif
109
110/*
111 * The GIC mapping of CPU interfaces does not necessarily match
112 * the logical CPU numbering. Let's use a mapping as returned
113 * by the GIC itself.
114 */
115#define NR_GIC_CPU_IF 8
116static u8 gic_cpu_map[NR_GIC_CPU_IF] __read_mostly;
117
118static DEFINE_STATIC_KEY_TRUE(supports_deactivate_key);
119
120static struct gic_chip_data gic_data[CONFIG_ARM_GIC_MAX_NR] __read_mostly;
121
122static struct gic_kvm_info gic_v2_kvm_info;
123
124static DEFINE_PER_CPU(u32, sgi_intid);
125
126/*
127 * Supported arch specific GIC irq extension.
128 * Default make them NULL.
129 */
130struct irq_chip gic_arch_extn = {
131 .irq_eoi = NULL,
132 .irq_mask = NULL,
133 .irq_unmask = NULL,
134 .irq_retrigger = NULL,
135 .irq_set_type = NULL,
136 .irq_set_wake = NULL,
137};
138
139static void __iomem *gic_dist_base_addr;
140
141#ifdef CONFIG_GIC_NON_BANKED
142static DEFINE_STATIC_KEY_FALSE(frankengic_key);
143
144static void enable_frankengic(void)
145{
146 static_branch_enable(&frankengic_key);
147}
148
149static inline void __iomem *__get_base(union gic_base *base)
150{
151 if (static_branch_unlikely(&frankengic_key))
152 return raw_cpu_read(*base->percpu_base);
153
154 return base->common_base;
155}
156
157#define gic_data_dist_base(d) __get_base(&(d)->dist_base)
158#define gic_data_cpu_base(d) __get_base(&(d)->cpu_base)
159#else
160#define gic_data_dist_base(d) ((d)->dist_base.common_base)
161#define gic_data_cpu_base(d) ((d)->cpu_base.common_base)
162#define enable_frankengic() do { } while (0)
163#endif
164
165
166static int gic_set_wake(struct irq_data *d, unsigned int on)
167{
168 int ret = -ENXIO;
169
170 if (gic_arch_extn.irq_set_wake)
171 ret = gic_arch_extn.irq_set_wake(d, on);
172
173 return ret;
174}
175
176static inline void __iomem *gic_dist_base(struct irq_data *d)
177{
178 struct gic_chip_data *gic_data = irq_data_get_irq_chip_data(d);
179 return gic_data_dist_base(gic_data);
180}
181
182static inline void __iomem *gic_cpu_base(struct irq_data *d)
183{
184 struct gic_chip_data *gic_data = irq_data_get_irq_chip_data(d);
185 return gic_data_cpu_base(gic_data);
186}
187
188static inline unsigned int gic_irq(struct irq_data *d)
189{
190 return d->hwirq;
191}
192
193static inline bool cascading_gic_irq(struct irq_data *d)
194{
195 void *data = irq_data_get_irq_handler_data(d);
196
197 /*
198 * If handler_data is set, this is a cascading interrupt, and
199 * it cannot possibly be forwarded.
200 */
201 return data != NULL;
202}
203
204/*
205 * Routines to acknowledge, disable and enable interrupts
206 */
207static void gic_poke_irq(struct irq_data *d, u32 offset)
208{
209 u32 mask = 1 << (gic_irq(d) % 32);
210 writel_relaxed(mask, gic_dist_base(d) + offset + (gic_irq(d) / 32) * 4);
211}
212
213static int gic_peek_irq(struct irq_data *d, u32 offset)
214{
215 u32 mask = 1 << (gic_irq(d) % 32);
216 return !!(readl_relaxed(gic_dist_base(d) + offset + (gic_irq(d) / 32) * 4) & mask);
217}
218
219static void gic_mask_irq(struct irq_data *d)
220{
221 gic_poke_irq(d, GIC_DIST_ENABLE_CLEAR);
222}
223
224static void gic_eoimode1_mask_irq(struct irq_data *d)
225{
226 gic_mask_irq(d);
227 /*
228 * When masking a forwarded interrupt, make sure it is
229 * deactivated as well.
230 *
231 * This ensures that an interrupt that is getting
232 * disabled/masked will not get "stuck", because there is
233 * noone to deactivate it (guest is being terminated).
234 */
235 if (irqd_is_forwarded_to_vcpu(d))
236 gic_poke_irq(d, GIC_DIST_ACTIVE_CLEAR);
237}
238
239static void gic_unmask_irq(struct irq_data *d)
240{
241 gic_poke_irq(d, GIC_DIST_ENABLE_SET);
242}
243
244static void gic_eoi_irq(struct irq_data *d)
245{
246 u32 hwirq = gic_irq(d);
247
248 if (hwirq < 16)
249 hwirq = this_cpu_read(sgi_intid);
250
251 writel_relaxed(hwirq, gic_cpu_base(d) + GIC_CPU_EOI);
252}
253
254static void gic_eoimode1_eoi_irq(struct irq_data *d)
255{
256 u32 hwirq = gic_irq(d);
257
258 /* Do not deactivate an IRQ forwarded to a vcpu. */
259 if (irqd_is_forwarded_to_vcpu(d))
260 return;
261
262 if (hwirq < 16)
263 hwirq = this_cpu_read(sgi_intid);
264
265 writel_relaxed(hwirq, gic_cpu_base(d) + GIC_CPU_DEACTIVATE);
266}
267
268static int gic_irq_set_irqchip_state(struct irq_data *d,
269 enum irqchip_irq_state which, bool val)
270{
271 u32 reg;
272
273 switch (which) {
274 case IRQCHIP_STATE_PENDING:
275 reg = val ? GIC_DIST_PENDING_SET : GIC_DIST_PENDING_CLEAR;
276 break;
277
278 case IRQCHIP_STATE_ACTIVE:
279 reg = val ? GIC_DIST_ACTIVE_SET : GIC_DIST_ACTIVE_CLEAR;
280 break;
281
282 case IRQCHIP_STATE_MASKED:
283 reg = val ? GIC_DIST_ENABLE_CLEAR : GIC_DIST_ENABLE_SET;
284 break;
285
286 default:
287 return -EINVAL;
288 }
289
290 gic_poke_irq(d, reg);
291 return 0;
292}
293
294static int gic_irq_get_irqchip_state(struct irq_data *d,
295 enum irqchip_irq_state which, bool *val)
296{
297 switch (which) {
298 case IRQCHIP_STATE_PENDING:
299 *val = gic_peek_irq(d, GIC_DIST_PENDING_SET);
300 break;
301
302 case IRQCHIP_STATE_ACTIVE:
303 *val = gic_peek_irq(d, GIC_DIST_ACTIVE_SET);
304 break;
305
306 case IRQCHIP_STATE_MASKED:
307 *val = !gic_peek_irq(d, GIC_DIST_ENABLE_SET);
308 break;
309
310 default:
311 return -EINVAL;
312 }
313
314 return 0;
315}
316
317static int gic_set_type(struct irq_data *d, unsigned int type)
318{
319 void __iomem *base = gic_dist_base(d);
320 unsigned int gicirq = gic_irq(d);
321 int ret;
322
323 /* Interrupt configuration for SGIs can't be changed */
324 if (gicirq < 16)
325 return type != IRQ_TYPE_EDGE_RISING ? -EINVAL : 0;
326
327 /* SPIs have restrictions on the supported types */
328 if (gicirq >= 32 && type != IRQ_TYPE_LEVEL_HIGH &&
329 type != IRQ_TYPE_EDGE_RISING)
330 return -EINVAL;
331
332 ret = gic_configure_irq(gicirq, type, base + GIC_DIST_CONFIG, NULL);
333 if (ret && gicirq < 32) {
334 /* Misconfigured PPIs are usually not fatal */
335 pr_warn("GIC: PPI%d is secure or misconfigured\n", gicirq - 16);
336 ret = 0;
337 }
338
339 return ret;
340}
341
342static int gic_irq_set_vcpu_affinity(struct irq_data *d, void *vcpu)
343{
344 /* Only interrupts on the primary GIC can be forwarded to a vcpu. */
345 if (cascading_gic_irq(d) || gic_irq(d) < 16)
346 return -EINVAL;
347
348 if (vcpu)
349 irqd_set_forwarded_to_vcpu(d);
350 else
351 irqd_clr_forwarded_to_vcpu(d);
352 return 0;
353}
354
355static void __exception_irq_entry gic_handle_irq(struct pt_regs *regs)
356{
357 u32 irqstat, irqnr;
358 struct gic_chip_data *gic = &gic_data[0];
359 void __iomem *cpu_base = gic_data_cpu_base(gic);
360
361 do {
362 irqstat = readl_relaxed(cpu_base + GIC_CPU_INTACK);
363 irqnr = irqstat & GICC_IAR_INT_ID_MASK;
364
365 if (unlikely(irqnr >= 1020))
366 break;
367
368 if (static_branch_likely(&supports_deactivate_key))
369 writel_relaxed(irqstat, cpu_base + GIC_CPU_EOI);
370 isb();
371
372 /*
373 * Ensure any shared data written by the CPU sending the IPI
374 * is read after we've read the ACK register on the GIC.
375 *
376 * Pairs with the write barrier in gic_ipi_send_mask
377 */
378 if (irqnr <= 15) {
379 smp_rmb();
380
381 /*
382 * The GIC encodes the source CPU in GICC_IAR,
383 * leading to the deactivation to fail if not
384 * written back as is to GICC_EOI. Stash the INTID
385 * away for gic_eoi_irq() to write back. This only
386 * works because we don't nest SGIs...
387 */
388 this_cpu_write(sgi_intid, irqstat);
389 }
390
391 handle_domain_irq(gic->domain, irqnr, regs);
392 } while (1);
393}
394
395static void gic_handle_cascade_irq(struct irq_desc *desc)
396{
397 struct gic_chip_data *chip_data = irq_desc_get_handler_data(desc);
398 struct irq_chip *chip = irq_desc_get_chip(desc);
399 unsigned int cascade_irq, gic_irq;
400 unsigned long status;
401
402 chained_irq_enter(chip, desc);
403
404 status = readl_relaxed(gic_data_cpu_base(chip_data) + GIC_CPU_INTACK);
405
406 gic_irq = (status & GICC_IAR_INT_ID_MASK);
407 if (gic_irq == GICC_INT_SPURIOUS)
408 goto out;
409
410 cascade_irq = irq_find_mapping(chip_data->domain, gic_irq);
411 if (unlikely(gic_irq < 32 || gic_irq > 1020)) {
412 handle_bad_irq(desc);
413 } else {
414 isb();
415 generic_handle_irq(cascade_irq);
416 }
417
418 out:
419 chained_irq_exit(chip, desc);
420}
421
422static const struct irq_chip gic_chip = {
423 .irq_mask = gic_mask_irq,
424 .irq_unmask = gic_unmask_irq,
425 .irq_eoi = gic_eoi_irq,
426 .irq_set_type = gic_set_type,
427 .irq_get_irqchip_state = gic_irq_get_irqchip_state,
428 .irq_set_irqchip_state = gic_irq_set_irqchip_state,
429 .irq_set_wake = gic_set_wake,
430 .flags = IRQCHIP_SET_TYPE_MASKED |
431 /* IRQCHIP_SKIP_SET_WAKE | */
432 IRQCHIP_MASK_ON_SUSPEND,
433};
434
435void __init gic_cascade_irq(unsigned int gic_nr, unsigned int irq)
436{
437 BUG_ON(gic_nr >= CONFIG_ARM_GIC_MAX_NR);
438 irq_set_chained_handler_and_data(irq, gic_handle_cascade_irq,
439 &gic_data[gic_nr]);
440}
441
442static u8 gic_get_cpumask(struct gic_chip_data *gic)
443{
444 void __iomem *base = gic_data_dist_base(gic);
445 u32 mask, i;
446
447 for (i = mask = 0; i < 32; i += 4) {
448 mask = readl_relaxed(base + GIC_DIST_TARGET + i);
449 mask |= mask >> 16;
450 mask |= mask >> 8;
451 if (mask)
452 break;
453 }
454
455 if (!mask && num_possible_cpus() > 1)
456 pr_crit("GIC CPU mask not found - kernel will fail to boot.\n");
457
458 return mask;
459}
460
461static bool gic_check_gicv2(void __iomem *base)
462{
463 u32 val = readl_relaxed(base + GIC_CPU_IDENT);
464 return (val & 0xff0fff) == 0x02043B;
465}
466
467static void gic_cpu_if_up(struct gic_chip_data *gic)
468{
469 void __iomem *cpu_base = gic_data_cpu_base(gic);
470 u32 bypass = 0;
471 u32 mode = 0;
472 int i;
473
474 if (gic == &gic_data[0] && static_branch_likely(&supports_deactivate_key))
475 mode = GIC_CPU_CTRL_EOImodeNS;
476
477 if (gic_check_gicv2(cpu_base))
478 for (i = 0; i < 4; i++)
479 writel_relaxed(0, cpu_base + GIC_CPU_ACTIVEPRIO + i * 4);
480
481 /*
482 * Preserve bypass disable bits to be written back later
483 */
484 bypass = readl(cpu_base + GIC_CPU_CTRL);
485 bypass &= GICC_DIS_BYPASS_MASK;
486
487 writel_relaxed(bypass | mode | GICC_ENABLE, cpu_base + GIC_CPU_CTRL);
488}
489
490
491static void gic_dist_init(struct gic_chip_data *gic)
492{
493 unsigned int i;
494 u32 cpumask;
495 unsigned int gic_irqs = gic->gic_irqs;
496 void __iomem *base = gic_data_dist_base(gic);
497
498 gic_dist_base_addr = base;
499 writel_relaxed(GICD_DISABLE, base + GIC_DIST_CTRL);
500
501 /*
502 * Set all global interrupts to this CPU only.
503 */
504 cpumask = gic_get_cpumask(gic);
505 cpumask |= cpumask << 8;
506 cpumask |= cpumask << 16;
507 for (i = 32; i < gic_irqs; i += 4)
508 writel_relaxed(cpumask, base + GIC_DIST_TARGET + i * 4 / 4);
509
510 gic_dist_config(base, gic_irqs, NULL);
511
512 writel_relaxed(GICD_ENABLE, base + GIC_DIST_CTRL);
513}
514
515static int gic_cpu_init(struct gic_chip_data *gic)
516{
517 void __iomem *dist_base = gic_data_dist_base(gic);
518 void __iomem *base = gic_data_cpu_base(gic);
519 unsigned int cpu_mask, cpu = smp_processor_id();
520 int i;
521
522 /*
523 * Setting up the CPU map is only relevant for the primary GIC
524 * because any nested/secondary GICs do not directly interface
525 * with the CPU(s).
526 */
527 if (gic == &gic_data[0]) {
528 /*
529 * Get what the GIC says our CPU mask is.
530 */
531 if (WARN_ON(cpu >= NR_GIC_CPU_IF))
532 return -EINVAL;
533
534 gic_check_cpu_features();
535 cpu_mask = gic_get_cpumask(gic);
536 gic_cpu_map[cpu] = cpu_mask;
537
538 /*
539 * Clear our mask from the other map entries in case they're
540 * still undefined.
541 */
542 for (i = 0; i < NR_GIC_CPU_IF; i++)
543 if (i != cpu)
544 gic_cpu_map[i] &= ~cpu_mask;
545 }
546
547 gic_cpu_config(dist_base, 32, NULL);
548
549 writel_relaxed(GICC_INT_PRI_THRESHOLD, base + GIC_CPU_PRIMASK);
550 gic_cpu_if_up(gic);
551
552 return 0;
553}
554
555int gic_cpu_if_down(unsigned int gic_nr)
556{
557 void __iomem *cpu_base;
558 u32 val = 0;
559
560 if (gic_nr >= CONFIG_ARM_GIC_MAX_NR)
561 return -EINVAL;
562
563 cpu_base = gic_data_cpu_base(&gic_data[gic_nr]);
564 val = readl(cpu_base + GIC_CPU_CTRL);
565 val &= ~GICC_ENABLE;
566 writel_relaxed(val, cpu_base + GIC_CPU_CTRL);
567
568 return 0;
569}
570
571#if defined(CONFIG_CPU_PM) || defined(CONFIG_ARM_GIC_PM)
572/*
573 * Saves the GIC distributor registers during suspend or idle. Must be called
574 * with interrupts disabled but before powering down the GIC. After calling
575 * this function, no interrupts will be delivered by the GIC, and another
576 * platform-specific wakeup source must be enabled.
577 */
578void gic_dist_save(struct gic_chip_data *gic)
579{
580 unsigned int gic_irqs;
581 void __iomem *dist_base;
582 int i;
583
584 if (WARN_ON(!gic))
585 return;
586
587 gic_irqs = gic->gic_irqs;
588 dist_base = gic_data_dist_base(gic);
589
590 if (!dist_base)
591 return;
592
593 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 16); i++)
594 gic->saved_spi_conf[i] =
595 readl_relaxed(dist_base + GIC_DIST_CONFIG + i * 4);
596
597 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 4); i++)
598 gic->saved_spi_target[i] =
599 readl_relaxed(dist_base + GIC_DIST_TARGET + i * 4);
600
601 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 32); i++)
602 gic->saved_spi_enable[i] =
603 readl_relaxed(dist_base + GIC_DIST_ENABLE_SET + i * 4);
604
605 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 32); i++)
606 gic->saved_spi_active[i] =
607 readl_relaxed(dist_base + GIC_DIST_ACTIVE_SET + i * 4);
608}
609
610/*
611 * Restores the GIC distributor registers during resume or when coming out of
612 * idle. Must be called before enabling interrupts. If a level interrupt
613 * that occurred while the GIC was suspended is still present, it will be
614 * handled normally, but any edge interrupts that occurred will not be seen by
615 * the GIC and need to be handled by the platform-specific wakeup source.
616 */
617void gic_dist_restore(struct gic_chip_data *gic)
618{
619 unsigned int gic_irqs;
620 unsigned int i;
621 void __iomem *dist_base;
622
623 if (WARN_ON(!gic))
624 return;
625
626 gic_irqs = gic->gic_irqs;
627 dist_base = gic_data_dist_base(gic);
628
629 if (!dist_base)
630 return;
631
632 writel_relaxed(GICD_DISABLE, dist_base + GIC_DIST_CTRL);
633
634 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 16); i++)
635 writel_relaxed(gic->saved_spi_conf[i],
636 dist_base + GIC_DIST_CONFIG + i * 4);
637
638 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 4); i++)
639 writel_relaxed(GICD_INT_DEF_PRI_X4,
640 dist_base + GIC_DIST_PRI + i * 4);
641
642 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 4); i++)
643 writel_relaxed(gic->saved_spi_target[i],
644 dist_base + GIC_DIST_TARGET + i * 4);
645
646 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 32); i++) {
647 writel_relaxed(GICD_INT_EN_CLR_X32,
648 dist_base + GIC_DIST_ENABLE_CLEAR + i * 4);
649 writel_relaxed(gic->saved_spi_enable[i],
650 dist_base + GIC_DIST_ENABLE_SET + i * 4);
651 }
652
653 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 32); i++) {
654 writel_relaxed(GICD_INT_EN_CLR_X32,
655 dist_base + GIC_DIST_ACTIVE_CLEAR + i * 4);
656 writel_relaxed(gic->saved_spi_active[i],
657 dist_base + GIC_DIST_ACTIVE_SET + i * 4);
658 }
659
660 writel_relaxed(GICD_ENABLE, dist_base + GIC_DIST_CTRL);
661}
662
663void gic_cpu_save(struct gic_chip_data *gic)
664{
665 int i;
666 u32 *ptr;
667 void __iomem *dist_base;
668 void __iomem *cpu_base;
669
670 if (WARN_ON(!gic))
671 return;
672
673 dist_base = gic_data_dist_base(gic);
674 cpu_base = gic_data_cpu_base(gic);
675
676 if (!dist_base || !cpu_base)
677 return;
678
679 ptr = raw_cpu_ptr(gic->saved_ppi_enable);
680 for (i = 0; i < DIV_ROUND_UP(32, 32); i++)
681 ptr[i] = readl_relaxed(dist_base + GIC_DIST_ENABLE_SET + i * 4);
682
683 ptr = raw_cpu_ptr(gic->saved_ppi_active);
684 for (i = 0; i < DIV_ROUND_UP(32, 32); i++)
685 ptr[i] = readl_relaxed(dist_base + GIC_DIST_ACTIVE_SET + i * 4);
686
687 ptr = raw_cpu_ptr(gic->saved_ppi_conf);
688 for (i = 0; i < DIV_ROUND_UP(32, 16); i++)
689 ptr[i] = readl_relaxed(dist_base + GIC_DIST_CONFIG + i * 4);
690
691}
692
693void gic_cpu_restore(struct gic_chip_data *gic)
694{
695 int i;
696 u32 *ptr;
697 void __iomem *dist_base;
698 void __iomem *cpu_base;
699
700 if (WARN_ON(!gic))
701 return;
702
703 dist_base = gic_data_dist_base(gic);
704 cpu_base = gic_data_cpu_base(gic);
705
706 if (!dist_base || !cpu_base)
707 return;
708
709 ptr = raw_cpu_ptr(gic->saved_ppi_enable);
710 for (i = 0; i < DIV_ROUND_UP(32, 32); i++) {
711 writel_relaxed(GICD_INT_EN_CLR_X32,
712 dist_base + GIC_DIST_ENABLE_CLEAR + i * 4);
713 writel_relaxed(ptr[i], dist_base + GIC_DIST_ENABLE_SET + i * 4);
714 }
715
716 ptr = raw_cpu_ptr(gic->saved_ppi_active);
717 for (i = 0; i < DIV_ROUND_UP(32, 32); i++) {
718 writel_relaxed(GICD_INT_EN_CLR_X32,
719 dist_base + GIC_DIST_ACTIVE_CLEAR + i * 4);
720 writel_relaxed(ptr[i], dist_base + GIC_DIST_ACTIVE_SET + i * 4);
721 }
722
723 ptr = raw_cpu_ptr(gic->saved_ppi_conf);
724 for (i = 0; i < DIV_ROUND_UP(32, 16); i++)
725 writel_relaxed(ptr[i], dist_base + GIC_DIST_CONFIG + i * 4);
726
727 for (i = 0; i < DIV_ROUND_UP(32, 4); i++)
728 writel_relaxed(GICD_INT_DEF_PRI_X4,
729 dist_base + GIC_DIST_PRI + i * 4);
730
731 writel_relaxed(GICC_INT_PRI_THRESHOLD, cpu_base + GIC_CPU_PRIMASK);
732 gic_cpu_if_up(gic);
733}
734
735#ifdef CONFIG_CPU_ASR1901
736void extern_gic_mask_unmask_irq(int irq_num, bool mask)
737{
738 u32 mask_val;
739 unsigned long flags;
740
741 if (!gic_get_dist_base()) {
742 pr_err("GIC not initialized\n");
743 return;
744 }
745
746 irq_num += 32;
747 mask_val = 1 << (irq_num % 32);
748
749 gic_lock_irqsave(flags);
750 if (mask)
751 writel_relaxed(mask_val, gic_get_dist_base() + GIC_DIST_ENABLE_CLEAR + (irq_num / 32) * 4);
752 else
753 writel_relaxed(mask_val, gic_get_dist_base() + GIC_DIST_ENABLE_SET + (irq_num / 32) * 4);
754 gic_unlock_irqrestore(flags);
755}
756
757void extern_gic_ack_irq(int irq_num)
758{
759 u32 irqstat, irqnr, mask_val;
760 unsigned long flags;
761
762 irq_num += 32;
763 mask_val = 1 << (irq_num % 32);
764
765 gic_lock_irqsave(flags);
766 writel_relaxed(mask_val, gic_get_dist_base() + GIC_DIST_PENDING_CLEAR + (irq_num / 32) * 4);
767 irqstat = readl_relaxed(gic_data_cpu_base(&gic_data[0]) + GIC_CPU_INTACK);
768 irqnr = irqstat & ~0x1c00;
769
770 if (irqnr == irq_num) {
771 writel_relaxed(irqstat, gic_data_cpu_base(&gic_data[0]) + GIC_CPU_EOI);
772 }
773 gic_unlock_irqrestore(flags);
774}
775#endif
776
777static int gic_notifier(struct notifier_block *self, unsigned long cmd, void *v)
778{
779 int i;
780
781 for (i = 0; i < CONFIG_ARM_GIC_MAX_NR; i++) {
782 switch (cmd) {
783 case CPU_PM_ENTER:
784 gic_cpu_save(&gic_data[i]);
785 break;
786 case CPU_PM_ENTER_FAILED:
787 case CPU_PM_EXIT:
788 gic_cpu_restore(&gic_data[i]);
789 break;
790 case CPU_CLUSTER_PM_ENTER:
791 gic_dist_save(&gic_data[i]);
792 break;
793 case CPU_CLUSTER_PM_ENTER_FAILED:
794 case CPU_CLUSTER_PM_EXIT:
795 gic_dist_restore(&gic_data[i]);
796 break;
797 }
798 }
799
800 return NOTIFY_OK;
801}
802
803static struct notifier_block gic_notifier_block = {
804 .notifier_call = gic_notifier,
805};
806
807static int gic_pm_init(struct gic_chip_data *gic)
808{
809 gic->saved_ppi_enable = __alloc_percpu(DIV_ROUND_UP(32, 32) * 4,
810 sizeof(u32));
811 if (WARN_ON(!gic->saved_ppi_enable))
812 return -ENOMEM;
813
814 gic->saved_ppi_active = __alloc_percpu(DIV_ROUND_UP(32, 32) * 4,
815 sizeof(u32));
816 if (WARN_ON(!gic->saved_ppi_active))
817 goto free_ppi_enable;
818
819 gic->saved_ppi_conf = __alloc_percpu(DIV_ROUND_UP(32, 16) * 4,
820 sizeof(u32));
821 if (WARN_ON(!gic->saved_ppi_conf))
822 goto free_ppi_active;
823
824 if (gic == &gic_data[0])
825 cpu_pm_register_notifier(&gic_notifier_block);
826
827 return 0;
828
829free_ppi_active:
830 free_percpu(gic->saved_ppi_active);
831free_ppi_enable:
832 free_percpu(gic->saved_ppi_enable);
833
834 return -ENOMEM;
835}
836#else
837static int gic_pm_init(struct gic_chip_data *gic)
838{
839 return 0;
840}
841#endif
842
843#ifdef CONFIG_SMP
844static int gic_set_affinity(struct irq_data *d, const struct cpumask *mask_val,
845 bool force)
846{
847 void __iomem *reg = gic_dist_base(d) + GIC_DIST_TARGET + gic_irq(d);
848 unsigned int cpu;
849
850 if (!force)
851 cpu = cpumask_any_and(mask_val, cpu_online_mask);
852 else
853 cpu = cpumask_first(mask_val);
854
855 if (cpu >= NR_GIC_CPU_IF || cpu >= nr_cpu_ids)
856 return -EINVAL;
857
858 writeb_relaxed(gic_cpu_map[cpu], reg);
859 irq_data_update_effective_affinity(d, cpumask_of(cpu));
860
861 return IRQ_SET_MASK_OK_DONE;
862}
863
864#if defined(CONFIG_CPU_ASR1901) && defined(CONFIG_SMP)
865extern void asr1901_gic_raise_softirq(const struct cpumask *mask, unsigned int irq);
866#endif
867static void gic_ipi_send_mask(struct irq_data *d, const struct cpumask *mask)
868{
869 int cpu;
870 unsigned long flags, map = 0;
871
872 if (unlikely(nr_cpu_ids == 1)) {
873 /* Only one CPU? let's do a self-IPI... */
874 writel_relaxed(2 << 24 | d->hwirq,
875 gic_data_dist_base(&gic_data[0]) + GIC_DIST_SOFTINT);
876 return;
877 }
878
879 gic_lock_irqsave(flags);
880
881 /* Convert our logical CPU mask into a physical one. */
882 for_each_cpu(cpu, mask)
883 map |= gic_cpu_map[cpu];
884
885 /*
886 * Ensure that stores to Normal memory are visible to the
887 * other CPUs before they observe us issuing the IPI.
888 */
889 dmb(ishst);
890
891 /* this always happens on GIC0 */
892 writel_relaxed(map << 16 | d->hwirq, gic_data_dist_base(&gic_data[0]) + GIC_DIST_SOFTINT);
893
894 gic_unlock_irqrestore(flags);
895
896#if defined(CONFIG_CPU_ASR1901) && defined(CONFIG_SMP)
897 asr1901_gic_raise_softirq(mask, d->hwirq);
898#endif
899
900}
901
902static int gic_starting_cpu(unsigned int cpu)
903{
904 gic_cpu_init(&gic_data[0]);
905 return 0;
906}
907
908static __init void gic_smp_init(void)
909{
910 struct irq_fwspec sgi_fwspec = {
911 .fwnode = gic_data[0].domain->fwnode,
912 .param_count = 1,
913 };
914 int base_sgi;
915
916 cpuhp_setup_state_nocalls(CPUHP_AP_IRQ_GIC_STARTING,
917 "irqchip/arm/gic:starting",
918 gic_starting_cpu, NULL);
919
920 base_sgi = __irq_domain_alloc_irqs(gic_data[0].domain, -1, 8,
921 NUMA_NO_NODE, &sgi_fwspec,
922 false, NULL);
923 if (WARN_ON(base_sgi <= 0))
924 return;
925
926 set_smp_ipi_range(base_sgi, 8);
927}
928#else
929#define gic_smp_init() do { } while (0)
930#define gic_set_affinity NULL
931#define gic_ipi_send_mask NULL
932#endif
933
934#ifdef CONFIG_BL_SWITCHER
935/*
936 * gic_send_sgi - send a SGI directly to given CPU interface number
937 *
938 * cpu_id: the ID for the destination CPU interface
939 * irq: the IPI number to send a SGI for
940 */
941void gic_send_sgi(unsigned int cpu_id, unsigned int irq)
942{
943 BUG_ON(cpu_id >= NR_GIC_CPU_IF);
944 cpu_id = 1 << cpu_id;
945 /* this always happens on GIC0 */
946 writel_relaxed((cpu_id << 16) | irq, gic_data_dist_base(&gic_data[0]) + GIC_DIST_SOFTINT);
947}
948
949/*
950 * gic_get_cpu_id - get the CPU interface ID for the specified CPU
951 *
952 * @cpu: the logical CPU number to get the GIC ID for.
953 *
954 * Return the CPU interface ID for the given logical CPU number,
955 * or -1 if the CPU number is too large or the interface ID is
956 * unknown (more than one bit set).
957 */
958int gic_get_cpu_id(unsigned int cpu)
959{
960 unsigned int cpu_bit;
961
962 if (cpu >= NR_GIC_CPU_IF)
963 return -1;
964 cpu_bit = gic_cpu_map[cpu];
965 if (cpu_bit & (cpu_bit - 1))
966 return -1;
967 return __ffs(cpu_bit);
968}
969
970/*
971 * gic_migrate_target - migrate IRQs to another CPU interface
972 *
973 * @new_cpu_id: the CPU target ID to migrate IRQs to
974 *
975 * Migrate all peripheral interrupts with a target matching the current CPU
976 * to the interface corresponding to @new_cpu_id. The CPU interface mapping
977 * is also updated. Targets to other CPU interfaces are unchanged.
978 * This must be called with IRQs locally disabled.
979 */
980void gic_migrate_target(unsigned int new_cpu_id)
981{
982 unsigned int cur_cpu_id, gic_irqs, gic_nr = 0;
983 void __iomem *dist_base;
984 int i, ror_val, cpu = smp_processor_id();
985 u32 val, cur_target_mask, active_mask;
986
987 BUG_ON(gic_nr >= CONFIG_ARM_GIC_MAX_NR);
988
989 dist_base = gic_data_dist_base(&gic_data[gic_nr]);
990 if (!dist_base)
991 return;
992 gic_irqs = gic_data[gic_nr].gic_irqs;
993
994 cur_cpu_id = __ffs(gic_cpu_map[cpu]);
995 cur_target_mask = 0x01010101 << cur_cpu_id;
996 ror_val = (cur_cpu_id - new_cpu_id) & 31;
997
998 gic_lock();
999
1000 /* Update the target interface for this logical CPU */
1001 gic_cpu_map[cpu] = 1 << new_cpu_id;
1002
1003 /*
1004 * Find all the peripheral interrupts targeting the current
1005 * CPU interface and migrate them to the new CPU interface.
1006 * We skip DIST_TARGET 0 to 7 as they are read-only.
1007 */
1008 for (i = 8; i < DIV_ROUND_UP(gic_irqs, 4); i++) {
1009 val = readl_relaxed(dist_base + GIC_DIST_TARGET + i * 4);
1010 active_mask = val & cur_target_mask;
1011 if (active_mask) {
1012 val &= ~active_mask;
1013 val |= ror32(active_mask, ror_val);
1014 writel_relaxed(val, dist_base + GIC_DIST_TARGET + i*4);
1015 }
1016 }
1017
1018 gic_unlock();
1019
1020 /*
1021 * Now let's migrate and clear any potential SGIs that might be
1022 * pending for us (cur_cpu_id). Since GIC_DIST_SGI_PENDING_SET
1023 * is a banked register, we can only forward the SGI using
1024 * GIC_DIST_SOFTINT. The original SGI source is lost but Linux
1025 * doesn't use that information anyway.
1026 *
1027 * For the same reason we do not adjust SGI source information
1028 * for previously sent SGIs by us to other CPUs either.
1029 */
1030 for (i = 0; i < 16; i += 4) {
1031 int j;
1032 val = readl_relaxed(dist_base + GIC_DIST_SGI_PENDING_SET + i);
1033 if (!val)
1034 continue;
1035 writel_relaxed(val, dist_base + GIC_DIST_SGI_PENDING_CLEAR + i);
1036 for (j = i; j < i + 4; j++) {
1037 if (val & 0xff)
1038 writel_relaxed((1 << (new_cpu_id + 16)) | j,
1039 dist_base + GIC_DIST_SOFTINT);
1040 val >>= 8;
1041 }
1042 }
1043}
1044
1045/*
1046 * gic_get_sgir_physaddr - get the physical address for the SGI register
1047 *
1048 * REturn the physical address of the SGI register to be used
1049 * by some early assembly code when the kernel is not yet available.
1050 */
1051static unsigned long gic_dist_physaddr;
1052
1053unsigned long gic_get_sgir_physaddr(void)
1054{
1055 if (!gic_dist_physaddr)
1056 return 0;
1057 return gic_dist_physaddr + GIC_DIST_SOFTINT;
1058}
1059
1060static void __init gic_init_physaddr(struct device_node *node)
1061{
1062 struct resource res;
1063 if (of_address_to_resource(node, 0, &res) == 0) {
1064 gic_dist_physaddr = res.start;
1065 pr_info("GIC physical location is %#lx\n", gic_dist_physaddr);
1066 }
1067}
1068
1069#else
1070/*
1071 * gic_get_sgir_physaddr - get the physical address for the SGI register
1072 *
1073 * REturn the physical address of the SGI register to be used
1074 * by some early assembly code when the kernel is not yet available.
1075 */
1076static unsigned long gic_dist_physaddr;
1077
1078static void __init gic_init_physaddr(struct device_node *node)
1079{
1080 struct resource res;
1081
1082 if (of_address_to_resource(node, 0, &res) == 0) {
1083 gic_dist_physaddr = res.start;
1084 pr_info("GIC physical location is %#lx\n", gic_dist_physaddr);
1085 }
1086}
1087#endif
1088
1089static int gic_irq_domain_map(struct irq_domain *d, unsigned int irq,
1090 irq_hw_number_t hw)
1091{
1092 struct gic_chip_data *gic = d->host_data;
1093
1094 switch (hw) {
1095 case 0 ... 15:
1096 irq_set_percpu_devid(irq);
1097 irq_domain_set_info(d, irq, hw, &gic->chip, d->host_data,
1098 handle_percpu_devid_fasteoi_ipi,
1099 NULL, NULL);
1100 break;
1101 case 16 ... 31:
1102 irq_set_percpu_devid(irq);
1103 irq_domain_set_info(d, irq, hw, &gic->chip, d->host_data,
1104 handle_percpu_devid_irq, NULL, NULL);
1105 break;
1106 default:
1107 irq_domain_set_info(d, irq, hw, &gic->chip, d->host_data,
1108 handle_fasteoi_irq, NULL, NULL);
1109 irq_set_probe(irq);
1110 irqd_set_single_target(irq_desc_get_irq_data(irq_to_desc(irq)));
1111 break;
1112 }
1113 return 0;
1114}
1115
1116static void gic_irq_domain_unmap(struct irq_domain *d, unsigned int irq)
1117{
1118}
1119
1120static int gic_irq_domain_translate(struct irq_domain *d,
1121 struct irq_fwspec *fwspec,
1122 unsigned long *hwirq,
1123 unsigned int *type)
1124{
1125 if (fwspec->param_count == 1 && fwspec->param[0] < 16) {
1126 *hwirq = fwspec->param[0];
1127 *type = IRQ_TYPE_EDGE_RISING;
1128 return 0;
1129 }
1130
1131 if (is_of_node(fwspec->fwnode)) {
1132 if (fwspec->param_count < 3)
1133 return -EINVAL;
1134
1135 switch (fwspec->param[0]) {
1136 case 0: /* SPI */
1137 *hwirq = fwspec->param[1] + 32;
1138 break;
1139 case 1: /* PPI */
1140 *hwirq = fwspec->param[1] + 16;
1141 break;
1142 default:
1143 return -EINVAL;
1144 }
1145
1146 *type = fwspec->param[2] & IRQ_TYPE_SENSE_MASK;
1147
1148 /* Make it clear that broken DTs are... broken */
1149 WARN_ON(*type == IRQ_TYPE_NONE);
1150 return 0;
1151 }
1152
1153 if (is_fwnode_irqchip(fwspec->fwnode)) {
1154 if(fwspec->param_count != 2)
1155 return -EINVAL;
1156
1157 *hwirq = fwspec->param[0];
1158 *type = fwspec->param[1];
1159
1160 WARN_ON(*type == IRQ_TYPE_NONE);
1161 return 0;
1162 }
1163
1164 return -EINVAL;
1165}
1166
1167static int gic_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
1168 unsigned int nr_irqs, void *arg)
1169{
1170 int i, ret;
1171 irq_hw_number_t hwirq;
1172 unsigned int type = IRQ_TYPE_NONE;
1173 struct irq_fwspec *fwspec = arg;
1174
1175 ret = gic_irq_domain_translate(domain, fwspec, &hwirq, &type);
1176 if (ret)
1177 return ret;
1178
1179 for (i = 0; i < nr_irqs; i++) {
1180 ret = gic_irq_domain_map(domain, virq + i, hwirq + i);
1181 if (ret)
1182 return ret;
1183 }
1184
1185 return 0;
1186}
1187
1188static const struct irq_domain_ops gic_irq_domain_hierarchy_ops = {
1189 .translate = gic_irq_domain_translate,
1190 .alloc = gic_irq_domain_alloc,
1191 .free = irq_domain_free_irqs_top,
1192};
1193
1194static const struct irq_domain_ops gic_irq_domain_ops = {
1195 .map = gic_irq_domain_map,
1196 .unmap = gic_irq_domain_unmap,
1197};
1198
1199static void gic_init_chip(struct gic_chip_data *gic, struct device *dev,
1200 const char *name, bool use_eoimode1)
1201{
1202 /* Initialize irq_chip */
1203 gic->chip = gic_chip;
1204 gic->chip.name = name;
1205 gic->chip.parent_device = dev;
1206
1207 if (use_eoimode1) {
1208 gic->chip.irq_mask = gic_eoimode1_mask_irq;
1209 gic->chip.irq_eoi = gic_eoimode1_eoi_irq;
1210 gic->chip.irq_set_vcpu_affinity = gic_irq_set_vcpu_affinity;
1211 }
1212
1213 if (gic == &gic_data[0]) {
1214 gic->chip.irq_set_affinity = gic_set_affinity;
1215 gic->chip.ipi_send_mask = gic_ipi_send_mask;
1216 }
1217}
1218
1219static int gic_init_bases(struct gic_chip_data *gic,
1220 struct fwnode_handle *handle)
1221{
1222 int gic_irqs, ret;
1223
1224 if (IS_ENABLED(CONFIG_GIC_NON_BANKED) && gic->percpu_offset) {
1225 /* Frankein-GIC without banked registers... */
1226 unsigned int cpu;
1227
1228 gic->dist_base.percpu_base = alloc_percpu(void __iomem *);
1229 gic->cpu_base.percpu_base = alloc_percpu(void __iomem *);
1230 if (WARN_ON(!gic->dist_base.percpu_base ||
1231 !gic->cpu_base.percpu_base)) {
1232 ret = -ENOMEM;
1233 goto error;
1234 }
1235
1236 for_each_possible_cpu(cpu) {
1237 u32 mpidr = cpu_logical_map(cpu);
1238 u32 core_id = MPIDR_AFFINITY_LEVEL(mpidr, 0);
1239 unsigned long offset = gic->percpu_offset * core_id;
1240 *per_cpu_ptr(gic->dist_base.percpu_base, cpu) =
1241 gic->raw_dist_base + offset;
1242 *per_cpu_ptr(gic->cpu_base.percpu_base, cpu) =
1243 gic->raw_cpu_base + offset;
1244 }
1245
1246 enable_frankengic();
1247 } else {
1248 /* Normal, sane GIC... */
1249 WARN(gic->percpu_offset,
1250 "GIC_NON_BANKED not enabled, ignoring %08x offset!",
1251 gic->percpu_offset);
1252 gic->dist_base.common_base = gic->raw_dist_base;
1253 gic->cpu_base.common_base = gic->raw_cpu_base;
1254 }
1255
1256 /*
1257 * Find out how many interrupts are supported.
1258 * The GIC only supports up to 1020 interrupt sources.
1259 */
1260 gic_irqs = readl_relaxed(gic_data_dist_base(gic) + GIC_DIST_CTR) & 0x1f;
1261 gic_irqs = (gic_irqs + 1) * 32;
1262 if (gic_irqs > 1020)
1263 gic_irqs = 1020;
1264 gic->gic_irqs = gic_irqs;
1265
1266 if (handle) { /* DT/ACPI */
1267 gic->domain = irq_domain_create_linear(handle, gic_irqs,
1268 &gic_irq_domain_hierarchy_ops,
1269 gic);
1270 } else { /* Legacy support */
1271 /*
1272 * For primary GICs, skip over SGIs.
1273 * No secondary GIC support whatsoever.
1274 */
1275 int irq_base;
1276
1277 gic_irqs -= 16; /* calculate # of irqs to allocate */
1278
1279 irq_base = irq_alloc_descs(16, 16, gic_irqs,
1280 numa_node_id());
1281 if (irq_base < 0) {
1282 WARN(1, "Cannot allocate irq_descs @ IRQ16, assuming pre-allocated\n");
1283 irq_base = 16;
1284 }
1285
1286 gic->domain = irq_domain_add_legacy(NULL, gic_irqs, irq_base,
1287 16, &gic_irq_domain_ops, gic);
1288 }
1289
1290 if (WARN_ON(!gic->domain)) {
1291 ret = -ENODEV;
1292 goto error;
1293 }
1294
1295 gic_dist_init(gic);
1296 ret = gic_cpu_init(gic);
1297 if (ret)
1298 goto error;
1299
1300 ret = gic_pm_init(gic);
1301 if (ret)
1302 goto error;
1303
1304 return 0;
1305
1306error:
1307 if (IS_ENABLED(CONFIG_GIC_NON_BANKED) && gic->percpu_offset) {
1308 free_percpu(gic->dist_base.percpu_base);
1309 free_percpu(gic->cpu_base.percpu_base);
1310 }
1311
1312 return ret;
1313}
1314
1315static int __init __gic_init_bases(struct gic_chip_data *gic,
1316 struct fwnode_handle *handle)
1317{
1318 char *name;
1319 int i, ret;
1320
1321 if (WARN_ON(!gic || gic->domain))
1322 return -EINVAL;
1323
1324 if (gic == &gic_data[0]) {
1325 /*
1326 * Initialize the CPU interface map to all CPUs.
1327 * It will be refined as each CPU probes its ID.
1328 * This is only necessary for the primary GIC.
1329 */
1330 for (i = 0; i < NR_GIC_CPU_IF; i++)
1331 gic_cpu_map[i] = 0xff;
1332
1333 set_handle_irq(gic_handle_irq);
1334 if (static_branch_likely(&supports_deactivate_key))
1335 pr_info("GIC: Using split EOI/Deactivate mode\n");
1336 }
1337
1338 if (static_branch_likely(&supports_deactivate_key) && gic == &gic_data[0]) {
1339 name = kasprintf(GFP_KERNEL, "GICv2");
1340 gic_init_chip(gic, NULL, name, true);
1341 } else {
1342 name = kasprintf(GFP_KERNEL, "GIC-%d", (int)(gic-&gic_data[0]));
1343 gic_init_chip(gic, NULL, name, false);
1344 }
1345
1346 ret = gic_init_bases(gic, handle);
1347 if (ret)
1348 kfree(name);
1349 else if (gic == &gic_data[0])
1350 gic_smp_init();
1351
1352 return ret;
1353}
1354
1355void __init gic_init(void __iomem *dist_base, void __iomem *cpu_base)
1356{
1357 struct gic_chip_data *gic;
1358
1359 /*
1360 * Non-DT/ACPI systems won't run a hypervisor, so let's not
1361 * bother with these...
1362 */
1363 static_branch_disable(&supports_deactivate_key);
1364
1365 gic = &gic_data[0];
1366 gic->raw_dist_base = dist_base;
1367 gic->raw_cpu_base = cpu_base;
1368
1369 __gic_init_bases(gic, NULL);
1370}
1371
1372static void gic_teardown(struct gic_chip_data *gic)
1373{
1374 if (WARN_ON(!gic))
1375 return;
1376
1377 if (gic->raw_dist_base)
1378 iounmap(gic->raw_dist_base);
1379 if (gic->raw_cpu_base)
1380 iounmap(gic->raw_cpu_base);
1381}
1382
1383unsigned long gic_dist_base_phys(void)
1384{
1385 return gic_dist_physaddr;
1386}
1387void __iomem *gic_get_dist_base(void)
1388{
1389 return gic_dist_base_addr;
1390}
1391
1392#ifdef CONFIG_OF
1393static int gic_cnt __initdata;
1394static bool gicv2_force_probe;
1395
1396static int __init gicv2_force_probe_cfg(char *buf)
1397{
1398 return strtobool(buf, &gicv2_force_probe);
1399}
1400early_param("irqchip.gicv2_force_probe", gicv2_force_probe_cfg);
1401
1402static bool gic_check_eoimode(struct device_node *node, void __iomem **base)
1403{
1404 struct resource cpuif_res;
1405
1406 of_address_to_resource(node, 1, &cpuif_res);
1407
1408 if (!is_hyp_mode_available())
1409 return false;
1410 if (resource_size(&cpuif_res) < SZ_8K) {
1411 void __iomem *alt;
1412 /*
1413 * Check for a stupid firmware that only exposes the
1414 * first page of a GICv2.
1415 */
1416 if (!gic_check_gicv2(*base))
1417 return false;
1418
1419 if (!gicv2_force_probe) {
1420 pr_warn("GIC: GICv2 detected, but range too small and irqchip.gicv2_force_probe not set\n");
1421 return false;
1422 }
1423
1424 alt = ioremap(cpuif_res.start, SZ_8K);
1425 if (!alt)
1426 return false;
1427 if (!gic_check_gicv2(alt + SZ_4K)) {
1428 /*
1429 * The first page was that of a GICv2, and
1430 * the second was *something*. Let's trust it
1431 * to be a GICv2, and update the mapping.
1432 */
1433 pr_warn("GIC: GICv2 at %pa, but range is too small (broken DT?), assuming 8kB\n",
1434 &cpuif_res.start);
1435 iounmap(*base);
1436 *base = alt;
1437 return true;
1438 }
1439
1440 /*
1441 * We detected *two* initial GICv2 pages in a
1442 * row. Could be a GICv2 aliased over two 64kB
1443 * pages. Update the resource, map the iospace, and
1444 * pray.
1445 */
1446 iounmap(alt);
1447 alt = ioremap(cpuif_res.start, SZ_128K);
1448 if (!alt)
1449 return false;
1450 pr_warn("GIC: Aliased GICv2 at %pa, trying to find the canonical range over 128kB\n",
1451 &cpuif_res.start);
1452 cpuif_res.end = cpuif_res.start + SZ_128K -1;
1453 iounmap(*base);
1454 *base = alt;
1455 }
1456 if (resource_size(&cpuif_res) == SZ_128K) {
1457 /*
1458 * Verify that we have the first 4kB of a GICv2
1459 * aliased over the first 64kB by checking the
1460 * GICC_IIDR register on both ends.
1461 */
1462 if (!gic_check_gicv2(*base) ||
1463 !gic_check_gicv2(*base + 0xf000))
1464 return false;
1465
1466 /*
1467 * Move the base up by 60kB, so that we have a 8kB
1468 * contiguous region, which allows us to use GICC_DIR
1469 * at its normal offset. Please pass me that bucket.
1470 */
1471 *base += 0xf000;
1472 cpuif_res.start += 0xf000;
1473 pr_warn("GIC: Adjusting CPU interface base to %pa\n",
1474 &cpuif_res.start);
1475 }
1476
1477 return true;
1478}
1479
1480static int gic_of_setup(struct gic_chip_data *gic, struct device_node *node)
1481{
1482 if (!gic || !node)
1483 return -EINVAL;
1484
1485 gic->raw_dist_base = of_iomap(node, 0);
1486 if (WARN(!gic->raw_dist_base, "unable to map gic dist registers\n"))
1487 goto error;
1488
1489 gic->raw_cpu_base = of_iomap(node, 1);
1490 if (WARN(!gic->raw_cpu_base, "unable to map gic cpu registers\n"))
1491 goto error;
1492
1493 if (of_property_read_u32(node, "cpu-offset", &gic->percpu_offset))
1494 gic->percpu_offset = 0;
1495
1496 return 0;
1497
1498error:
1499 gic_teardown(gic);
1500
1501 return -ENOMEM;
1502}
1503
1504int gic_of_init_child(struct device *dev, struct gic_chip_data **gic, int irq)
1505{
1506 int ret;
1507
1508 if (!dev || !dev->of_node || !gic || !irq)
1509 return -EINVAL;
1510
1511 *gic = devm_kzalloc(dev, sizeof(**gic), GFP_KERNEL);
1512 if (!*gic)
1513 return -ENOMEM;
1514
1515 gic_init_chip(*gic, dev, dev->of_node->name, false);
1516
1517 ret = gic_of_setup(*gic, dev->of_node);
1518 if (ret)
1519 return ret;
1520
1521 ret = gic_init_bases(*gic, &dev->of_node->fwnode);
1522 if (ret) {
1523 gic_teardown(*gic);
1524 return ret;
1525 }
1526
1527 irq_set_chained_handler_and_data(irq, gic_handle_cascade_irq, *gic);
1528
1529 return 0;
1530}
1531
1532static void __init gic_of_setup_kvm_info(struct device_node *node)
1533{
1534 int ret;
1535 struct resource *vctrl_res = &gic_v2_kvm_info.vctrl;
1536 struct resource *vcpu_res = &gic_v2_kvm_info.vcpu;
1537
1538 gic_v2_kvm_info.type = GIC_V2;
1539
1540 gic_v2_kvm_info.maint_irq = irq_of_parse_and_map(node, 0);
1541 if (!gic_v2_kvm_info.maint_irq)
1542 return;
1543
1544 ret = of_address_to_resource(node, 2, vctrl_res);
1545 if (ret)
1546 return;
1547
1548 ret = of_address_to_resource(node, 3, vcpu_res);
1549 if (ret)
1550 return;
1551
1552 if (static_branch_likely(&supports_deactivate_key))
1553 gic_set_kvm_info(&gic_v2_kvm_info);
1554}
1555
1556int __init
1557gic_of_init(struct device_node *node, struct device_node *parent)
1558{
1559 struct gic_chip_data *gic;
1560 int irq, ret;
1561
1562 if (WARN_ON(!node))
1563 return -ENODEV;
1564
1565 if (WARN_ON(gic_cnt >= CONFIG_ARM_GIC_MAX_NR))
1566 return -EINVAL;
1567
1568 gic = &gic_data[gic_cnt];
1569
1570 ret = gic_of_setup(gic, node);
1571 if (ret)
1572 return ret;
1573
1574 /*
1575 * Disable split EOI/Deactivate if either HYP is not available
1576 * or the CPU interface is too small.
1577 */
1578 if (gic_cnt == 0 && !gic_check_eoimode(node, &gic->raw_cpu_base))
1579 static_branch_disable(&supports_deactivate_key);
1580
1581 ret = __gic_init_bases(gic, &node->fwnode);
1582 if (ret) {
1583 gic_teardown(gic);
1584 return ret;
1585 }
1586
1587 if (!gic_cnt) {
1588 gic_init_physaddr(node);
1589 gic_of_setup_kvm_info(node);
1590 }
1591
1592 if (parent) {
1593 irq = irq_of_parse_and_map(node, 0);
1594 gic_cascade_irq(gic_cnt, irq);
1595 }
1596
1597 if (IS_ENABLED(CONFIG_ARM_GIC_V2M))
1598 gicv2m_init(&node->fwnode, gic_data[gic_cnt].domain);
1599
1600 gic_cnt++;
1601 return 0;
1602}
1603IRQCHIP_DECLARE(gic_400, "arm,gic-400", gic_of_init);
1604IRQCHIP_DECLARE(arm11mp_gic, "arm,arm11mp-gic", gic_of_init);
1605IRQCHIP_DECLARE(arm1176jzf_dc_gic, "arm,arm1176jzf-devchip-gic", gic_of_init);
1606IRQCHIP_DECLARE(cortex_a15_gic, "arm,cortex-a15-gic", gic_of_init);
1607IRQCHIP_DECLARE(cortex_a9_gic, "arm,cortex-a9-gic", gic_of_init);
1608IRQCHIP_DECLARE(cortex_a7_gic, "arm,cortex-a7-gic", gic_of_init);
1609IRQCHIP_DECLARE(msm_8660_qgic, "qcom,msm-8660-qgic", gic_of_init);
1610IRQCHIP_DECLARE(msm_qgic2, "qcom,msm-qgic2", gic_of_init);
1611IRQCHIP_DECLARE(pl390, "arm,pl390", gic_of_init);
1612#else
1613int gic_of_init_child(struct device *dev, struct gic_chip_data **gic, int irq)
1614{
1615 return -ENOTSUPP;
1616}
1617#endif
1618
1619#ifdef CONFIG_ACPI
1620static struct
1621{
1622 phys_addr_t cpu_phys_base;
1623 u32 maint_irq;
1624 int maint_irq_mode;
1625 phys_addr_t vctrl_base;
1626 phys_addr_t vcpu_base;
1627} acpi_data __initdata;
1628
1629static int __init
1630gic_acpi_parse_madt_cpu(union acpi_subtable_headers *header,
1631 const unsigned long end)
1632{
1633 struct acpi_madt_generic_interrupt *processor;
1634 phys_addr_t gic_cpu_base;
1635 static int cpu_base_assigned;
1636
1637 processor = (struct acpi_madt_generic_interrupt *)header;
1638
1639 if (BAD_MADT_GICC_ENTRY(processor, end))
1640 return -EINVAL;
1641
1642 /*
1643 * There is no support for non-banked GICv1/2 register in ACPI spec.
1644 * All CPU interface addresses have to be the same.
1645 */
1646 gic_cpu_base = processor->base_address;
1647 if (cpu_base_assigned && gic_cpu_base != acpi_data.cpu_phys_base)
1648 return -EINVAL;
1649
1650 acpi_data.cpu_phys_base = gic_cpu_base;
1651 acpi_data.maint_irq = processor->vgic_interrupt;
1652 acpi_data.maint_irq_mode = (processor->flags & ACPI_MADT_VGIC_IRQ_MODE) ?
1653 ACPI_EDGE_SENSITIVE : ACPI_LEVEL_SENSITIVE;
1654 acpi_data.vctrl_base = processor->gich_base_address;
1655 acpi_data.vcpu_base = processor->gicv_base_address;
1656
1657 cpu_base_assigned = 1;
1658 return 0;
1659}
1660
1661/* The things you have to do to just *count* something... */
1662static int __init acpi_dummy_func(union acpi_subtable_headers *header,
1663 const unsigned long end)
1664{
1665 return 0;
1666}
1667
1668static bool __init acpi_gic_redist_is_present(void)
1669{
1670 return acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_REDISTRIBUTOR,
1671 acpi_dummy_func, 0) > 0;
1672}
1673
1674static bool __init gic_validate_dist(struct acpi_subtable_header *header,
1675 struct acpi_probe_entry *ape)
1676{
1677 struct acpi_madt_generic_distributor *dist;
1678 dist = (struct acpi_madt_generic_distributor *)header;
1679
1680 return (dist->version == ape->driver_data &&
1681 (dist->version != ACPI_MADT_GIC_VERSION_NONE ||
1682 !acpi_gic_redist_is_present()));
1683}
1684
1685#define ACPI_GICV2_DIST_MEM_SIZE (SZ_4K)
1686#define ACPI_GIC_CPU_IF_MEM_SIZE (SZ_8K)
1687#define ACPI_GICV2_VCTRL_MEM_SIZE (SZ_4K)
1688#define ACPI_GICV2_VCPU_MEM_SIZE (SZ_8K)
1689
1690static void __init gic_acpi_setup_kvm_info(void)
1691{
1692 int irq;
1693 struct resource *vctrl_res = &gic_v2_kvm_info.vctrl;
1694 struct resource *vcpu_res = &gic_v2_kvm_info.vcpu;
1695
1696 gic_v2_kvm_info.type = GIC_V2;
1697
1698 if (!acpi_data.vctrl_base)
1699 return;
1700
1701 vctrl_res->flags = IORESOURCE_MEM;
1702 vctrl_res->start = acpi_data.vctrl_base;
1703 vctrl_res->end = vctrl_res->start + ACPI_GICV2_VCTRL_MEM_SIZE - 1;
1704
1705 if (!acpi_data.vcpu_base)
1706 return;
1707
1708 vcpu_res->flags = IORESOURCE_MEM;
1709 vcpu_res->start = acpi_data.vcpu_base;
1710 vcpu_res->end = vcpu_res->start + ACPI_GICV2_VCPU_MEM_SIZE - 1;
1711
1712 irq = acpi_register_gsi(NULL, acpi_data.maint_irq,
1713 acpi_data.maint_irq_mode,
1714 ACPI_ACTIVE_HIGH);
1715 if (irq <= 0)
1716 return;
1717
1718 gic_v2_kvm_info.maint_irq = irq;
1719
1720 gic_set_kvm_info(&gic_v2_kvm_info);
1721}
1722
1723static int __init gic_v2_acpi_init(struct acpi_subtable_header *header,
1724 const unsigned long end)
1725{
1726 struct acpi_madt_generic_distributor *dist;
1727 struct fwnode_handle *domain_handle;
1728 struct gic_chip_data *gic = &gic_data[0];
1729 int count, ret;
1730
1731 /* Collect CPU base addresses */
1732 count = acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_INTERRUPT,
1733 gic_acpi_parse_madt_cpu, 0);
1734 if (count <= 0) {
1735 pr_err("No valid GICC entries exist\n");
1736 return -EINVAL;
1737 }
1738
1739 gic->raw_cpu_base = ioremap(acpi_data.cpu_phys_base, ACPI_GIC_CPU_IF_MEM_SIZE);
1740 if (!gic->raw_cpu_base) {
1741 pr_err("Unable to map GICC registers\n");
1742 return -ENOMEM;
1743 }
1744
1745 dist = (struct acpi_madt_generic_distributor *)header;
1746 gic->raw_dist_base = ioremap(dist->base_address,
1747 ACPI_GICV2_DIST_MEM_SIZE);
1748 if (!gic->raw_dist_base) {
1749 pr_err("Unable to map GICD registers\n");
1750 gic_teardown(gic);
1751 return -ENOMEM;
1752 }
1753
1754 /*
1755 * Disable split EOI/Deactivate if HYP is not available. ACPI
1756 * guarantees that we'll always have a GICv2, so the CPU
1757 * interface will always be the right size.
1758 */
1759 if (!is_hyp_mode_available())
1760 static_branch_disable(&supports_deactivate_key);
1761
1762 /*
1763 * Initialize GIC instance zero (no multi-GIC support).
1764 */
1765 domain_handle = irq_domain_alloc_fwnode(&dist->base_address);
1766 if (!domain_handle) {
1767 pr_err("Unable to allocate domain handle\n");
1768 gic_teardown(gic);
1769 return -ENOMEM;
1770 }
1771
1772 ret = __gic_init_bases(gic, domain_handle);
1773 if (ret) {
1774 pr_err("Failed to initialise GIC\n");
1775 irq_domain_free_fwnode(domain_handle);
1776 gic_teardown(gic);
1777 return ret;
1778 }
1779
1780 acpi_set_irq_model(ACPI_IRQ_MODEL_GIC, domain_handle);
1781
1782 if (IS_ENABLED(CONFIG_ARM_GIC_V2M))
1783 gicv2m_init(NULL, gic_data[0].domain);
1784
1785 if (static_branch_likely(&supports_deactivate_key))
1786 gic_acpi_setup_kvm_info();
1787
1788 return 0;
1789}
1790IRQCHIP_ACPI_DECLARE(gic_v2, ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR,
1791 gic_validate_dist, ACPI_MADT_GIC_VERSION_V2,
1792 gic_v2_acpi_init);
1793IRQCHIP_ACPI_DECLARE(gic_v2_maybe, ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR,
1794 gic_validate_dist, ACPI_MADT_GIC_VERSION_NONE,
1795 gic_v2_acpi_init);
1796#endif