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