2 * linux/arch/arm/common/gic.c
4 * Copyright (C) 2002 ARM Limited, All Rights Reserved.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * Interrupt architecture for the GIC:
12 * o There is one Interrupt Distributor, which receives interrupts
13 * from system devices and sends them to the Interrupt Controllers.
15 * o There is one CPU Interface per CPU, which sends interrupts sent
16 * by the Distributor, and interrupts generated locally, to the
17 * associated CPU. The base address of the CPU interface is usually
18 * aliased so that the same address points to different chips depending
19 * on the CPU it is accessed from.
21 * Note that IRQs 0-31 are special - they are local to each CPU.
22 * As such, the enable set/clear, pending set/clear and active bit
23 * registers are banked per-cpu for these sources.
25 #include <linux/init.h>
26 #include <linux/kernel.h>
27 #include <linux/list.h>
28 #include <linux/smp.h>
29 #include <linux/cpu_pm.h>
30 #include <linux/cpumask.h>
34 #include <asm/mach/irq.h>
35 #include <asm/hardware/gic.h>
37 static DEFINE_SPINLOCK(irq_controller_lock);
39 /* Address of GIC 0 CPU interface */
40 void __iomem *gic_cpu_base_addr __read_mostly;
43 * Supported arch specific GIC irq extension.
44 * Default make them NULL.
46 struct irq_chip gic_arch_extn = {
50 .irq_retrigger = NULL,
59 static struct gic_chip_data gic_data[MAX_GIC_NR] __read_mostly;
61 static inline void __iomem *gic_dist_base(struct irq_data *d)
63 struct gic_chip_data *gic_data = irq_data_get_irq_chip_data(d);
64 return gic_data->dist_base;
67 static inline void __iomem *gic_cpu_base(struct irq_data *d)
69 struct gic_chip_data *gic_data = irq_data_get_irq_chip_data(d);
70 return gic_data->cpu_base;
73 static inline unsigned int gic_irq(struct irq_data *d)
75 struct gic_chip_data *gic_data = irq_data_get_irq_chip_data(d);
76 return d->irq - gic_data->irq_offset;
80 * Routines to acknowledge, disable and enable interrupts
82 static void gic_mask_irq(struct irq_data *d)
84 u32 mask = 1 << (d->irq % 32);
86 spin_lock(&irq_controller_lock);
87 writel_relaxed(mask, gic_dist_base(d) + GIC_DIST_ENABLE_CLEAR + (gic_irq(d) / 32) * 4);
88 if (gic_arch_extn.irq_mask)
89 gic_arch_extn.irq_mask(d);
90 spin_unlock(&irq_controller_lock);
93 static void gic_unmask_irq(struct irq_data *d)
95 u32 mask = 1 << (d->irq % 32);
97 spin_lock(&irq_controller_lock);
98 if (gic_arch_extn.irq_unmask)
99 gic_arch_extn.irq_unmask(d);
100 writel_relaxed(mask, gic_dist_base(d) + GIC_DIST_ENABLE_SET + (gic_irq(d) / 32) * 4);
101 spin_unlock(&irq_controller_lock);
104 static void gic_eoi_irq(struct irq_data *d)
106 if (gic_arch_extn.irq_eoi) {
107 spin_lock(&irq_controller_lock);
108 gic_arch_extn.irq_eoi(d);
109 spin_unlock(&irq_controller_lock);
112 writel_relaxed(gic_irq(d), gic_cpu_base(d) + GIC_CPU_EOI);
115 static int gic_set_type(struct irq_data *d, unsigned int type)
117 void __iomem *base = gic_dist_base(d);
118 unsigned int gicirq = gic_irq(d);
119 u32 enablemask = 1 << (gicirq % 32);
120 u32 enableoff = (gicirq / 32) * 4;
121 u32 confmask = 0x2 << ((gicirq % 16) * 2);
122 u32 confoff = (gicirq / 16) * 4;
123 bool enabled = false;
126 /* Interrupt configuration for SGIs can't be changed */
130 if (type != IRQ_TYPE_LEVEL_HIGH && type != IRQ_TYPE_EDGE_RISING)
133 spin_lock(&irq_controller_lock);
135 if (gic_arch_extn.irq_set_type)
136 gic_arch_extn.irq_set_type(d, type);
138 val = readl_relaxed(base + GIC_DIST_CONFIG + confoff);
139 if (type == IRQ_TYPE_LEVEL_HIGH)
141 else if (type == IRQ_TYPE_EDGE_RISING)
145 * As recommended by the spec, disable the interrupt before changing
148 if (readl_relaxed(base + GIC_DIST_ENABLE_SET + enableoff) & enablemask) {
149 writel_relaxed(enablemask, base + GIC_DIST_ENABLE_CLEAR + enableoff);
153 writel_relaxed(val, base + GIC_DIST_CONFIG + confoff);
156 writel_relaxed(enablemask, base + GIC_DIST_ENABLE_SET + enableoff);
158 spin_unlock(&irq_controller_lock);
163 static int gic_retrigger(struct irq_data *d)
165 if (gic_arch_extn.irq_retrigger)
166 return gic_arch_extn.irq_retrigger(d);
172 static int gic_set_affinity(struct irq_data *d, const struct cpumask *mask_val,
175 void __iomem *reg = gic_dist_base(d) + GIC_DIST_TARGET + (gic_irq(d) & ~3);
176 unsigned int shift = (d->irq % 4) * 8;
177 unsigned int cpu = cpumask_any_and(mask_val, cpu_online_mask);
180 if (cpu >= 8 || cpu >= nr_cpu_ids)
183 mask = 0xff << shift;
184 bit = 1 << (cpu + shift);
186 spin_lock(&irq_controller_lock);
187 val = readl_relaxed(reg) & ~mask;
188 writel_relaxed(val | bit, reg);
189 spin_unlock(&irq_controller_lock);
191 return IRQ_SET_MASK_OK;
196 static int gic_set_wake(struct irq_data *d, unsigned int on)
200 if (gic_arch_extn.irq_set_wake)
201 ret = gic_arch_extn.irq_set_wake(d, on);
207 #define gic_set_wake NULL
210 static void gic_handle_cascade_irq(unsigned int irq, struct irq_desc *desc)
212 struct gic_chip_data *chip_data = irq_get_handler_data(irq);
213 struct irq_chip *chip = irq_get_chip(irq);
214 unsigned int cascade_irq, gic_irq;
215 unsigned long status;
217 chained_irq_enter(chip, desc);
219 spin_lock(&irq_controller_lock);
220 status = readl_relaxed(chip_data->cpu_base + GIC_CPU_INTACK);
221 spin_unlock(&irq_controller_lock);
223 gic_irq = (status & 0x3ff);
227 cascade_irq = gic_irq + chip_data->irq_offset;
228 if (unlikely(gic_irq < 32 || gic_irq > 1020 || cascade_irq >= NR_IRQS))
229 do_bad_IRQ(cascade_irq, desc);
231 generic_handle_irq(cascade_irq);
234 chained_irq_exit(chip, desc);
237 static struct irq_chip gic_chip = {
239 .irq_mask = gic_mask_irq,
240 .irq_unmask = gic_unmask_irq,
241 .irq_eoi = gic_eoi_irq,
242 .irq_set_type = gic_set_type,
243 .irq_retrigger = gic_retrigger,
245 .irq_set_affinity = gic_set_affinity,
247 .irq_set_wake = gic_set_wake,
250 void __init gic_cascade_irq(unsigned int gic_nr, unsigned int irq)
252 if (gic_nr >= MAX_GIC_NR)
254 if (irq_set_handler_data(irq, &gic_data[gic_nr]) != 0)
256 irq_set_chained_handler(irq, gic_handle_cascade_irq);
259 static void __init gic_dist_init(struct gic_chip_data *gic,
260 unsigned int irq_start)
262 unsigned int gic_irqs, irq_limit, i;
263 void __iomem *base = gic->dist_base;
264 u32 cpumask = 1 << smp_processor_id();
266 cpumask |= cpumask << 8;
267 cpumask |= cpumask << 16;
269 writel_relaxed(0, base + GIC_DIST_CTRL);
272 * Find out how many interrupts are supported.
273 * The GIC only supports up to 1020 interrupt sources.
275 gic_irqs = readl_relaxed(base + GIC_DIST_CTR) & 0x1f;
276 gic_irqs = (gic_irqs + 1) * 32;
280 gic->gic_irqs = gic_irqs;
283 * Set all global interrupts to be level triggered, active low.
285 for (i = 32; i < gic_irqs; i += 16)
286 writel_relaxed(0, base + GIC_DIST_CONFIG + i * 4 / 16);
289 * Set all global interrupts to this CPU only.
291 for (i = 32; i < gic_irqs; i += 4)
292 writel_relaxed(cpumask, base + GIC_DIST_TARGET + i * 4 / 4);
295 * Set priority on all global interrupts.
297 for (i = 32; i < gic_irqs; i += 4)
298 writel_relaxed(0xa0a0a0a0, base + GIC_DIST_PRI + i * 4 / 4);
301 * Disable all interrupts. Leave the PPI and SGIs alone
302 * as these enables are banked registers.
304 for (i = 32; i < gic_irqs; i += 32)
305 writel_relaxed(0xffffffff, base + GIC_DIST_ENABLE_CLEAR + i * 4 / 32);
308 * Limit number of interrupts registered to the platform maximum
310 irq_limit = gic->irq_offset + gic_irqs;
311 if (WARN_ON(irq_limit > NR_IRQS))
315 * Setup the Linux IRQ subsystem.
317 for (i = irq_start; i < irq_limit; i++) {
318 irq_set_chip_and_handler(i, &gic_chip, handle_fasteoi_irq);
319 irq_set_chip_data(i, gic);
320 set_irq_flags(i, IRQF_VALID | IRQF_PROBE);
323 writel_relaxed(1, base + GIC_DIST_CTRL);
326 static void __cpuinit gic_cpu_init(struct gic_chip_data *gic)
328 void __iomem *dist_base = gic->dist_base;
329 void __iomem *base = gic->cpu_base;
333 * Deal with the banked PPI and SGI interrupts - disable all
334 * PPI interrupts, ensure all SGI interrupts are enabled.
336 writel_relaxed(0xffff0000, dist_base + GIC_DIST_ENABLE_CLEAR);
337 writel_relaxed(0x0000ffff, dist_base + GIC_DIST_ENABLE_SET);
340 * Set priority on PPI and SGI interrupts
342 for (i = 0; i < 32; i += 4)
343 writel_relaxed(0xa0a0a0a0, dist_base + GIC_DIST_PRI + i * 4 / 4);
345 writel_relaxed(0xf0, base + GIC_CPU_PRIMASK);
346 writel_relaxed(1, base + GIC_CPU_CTRL);
351 * Saves the GIC distributor registers during suspend or idle. Must be called
352 * with interrupts disabled but before powering down the GIC. After calling
353 * this function, no interrupts will be delivered by the GIC, and another
354 * platform-specific wakeup source must be enabled.
356 static void gic_dist_save(unsigned int gic_nr)
358 unsigned int gic_irqs;
359 void __iomem *dist_base;
362 if (gic_nr >= MAX_GIC_NR)
365 gic_irqs = gic_data[gic_nr].gic_irqs;
366 dist_base = gic_data[gic_nr].dist_base;
371 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 16); i++)
372 gic_data[gic_nr].saved_spi_conf[i] =
373 readl_relaxed(dist_base + GIC_DIST_CONFIG + i * 4);
375 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 4); i++)
376 gic_data[gic_nr].saved_spi_target[i] =
377 readl_relaxed(dist_base + GIC_DIST_TARGET + i * 4);
379 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 32); i++)
380 gic_data[gic_nr].saved_spi_enable[i] =
381 readl_relaxed(dist_base + GIC_DIST_ENABLE_SET + i * 4);
385 * Restores the GIC distributor registers during resume or when coming out of
386 * idle. Must be called before enabling interrupts. If a level interrupt
387 * that occured while the GIC was suspended is still present, it will be
388 * handled normally, but any edge interrupts that occured will not be seen by
389 * the GIC and need to be handled by the platform-specific wakeup source.
391 static void gic_dist_restore(unsigned int gic_nr)
393 unsigned int gic_irqs;
395 void __iomem *dist_base;
397 if (gic_nr >= MAX_GIC_NR)
400 gic_irqs = gic_data[gic_nr].gic_irqs;
401 dist_base = gic_data[gic_nr].dist_base;
406 writel_relaxed(0, dist_base + GIC_DIST_CTRL);
408 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 16); i++)
409 writel_relaxed(gic_data[gic_nr].saved_spi_conf[i],
410 dist_base + GIC_DIST_CONFIG + i * 4);
412 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 4); i++)
413 writel_relaxed(0xa0a0a0a0,
414 dist_base + GIC_DIST_PRI + i * 4);
416 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 4); i++)
417 writel_relaxed(gic_data[gic_nr].saved_spi_target[i],
418 dist_base + GIC_DIST_TARGET + i * 4);
420 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 32); i++)
421 writel_relaxed(gic_data[gic_nr].saved_spi_enable[i],
422 dist_base + GIC_DIST_ENABLE_SET + i * 4);
424 writel_relaxed(1, dist_base + GIC_DIST_CTRL);
427 static void gic_cpu_save(unsigned int gic_nr)
431 void __iomem *dist_base;
432 void __iomem *cpu_base;
434 if (gic_nr >= MAX_GIC_NR)
437 dist_base = gic_data[gic_nr].dist_base;
438 cpu_base = gic_data[gic_nr].cpu_base;
440 if (!dist_base || !cpu_base)
443 ptr = __this_cpu_ptr(gic_data[gic_nr].saved_ppi_enable);
444 for (i = 0; i < DIV_ROUND_UP(32, 32); i++)
445 ptr[i] = readl_relaxed(dist_base + GIC_DIST_ENABLE_SET + i * 4);
447 ptr = __this_cpu_ptr(gic_data[gic_nr].saved_ppi_conf);
448 for (i = 0; i < DIV_ROUND_UP(32, 16); i++)
449 ptr[i] = readl_relaxed(dist_base + GIC_DIST_CONFIG + i * 4);
453 static void gic_cpu_restore(unsigned int gic_nr)
457 void __iomem *dist_base;
458 void __iomem *cpu_base;
460 if (gic_nr >= MAX_GIC_NR)
463 dist_base = gic_data[gic_nr].dist_base;
464 cpu_base = gic_data[gic_nr].cpu_base;
466 if (!dist_base || !cpu_base)
469 ptr = __this_cpu_ptr(gic_data[gic_nr].saved_ppi_enable);
470 for (i = 0; i < DIV_ROUND_UP(32, 32); i++)
471 writel_relaxed(ptr[i], dist_base + GIC_DIST_ENABLE_SET + i * 4);
473 ptr = __this_cpu_ptr(gic_data[gic_nr].saved_ppi_conf);
474 for (i = 0; i < DIV_ROUND_UP(32, 16); i++)
475 writel_relaxed(ptr[i], dist_base + GIC_DIST_CONFIG + i * 4);
477 for (i = 0; i < DIV_ROUND_UP(32, 4); i++)
478 writel_relaxed(0xa0a0a0a0, dist_base + GIC_DIST_PRI + i * 4);
480 writel_relaxed(0xf0, cpu_base + GIC_CPU_PRIMASK);
481 writel_relaxed(1, cpu_base + GIC_CPU_CTRL);
484 static int gic_notifier(struct notifier_block *self, unsigned long cmd, void *v)
488 for (i = 0; i < MAX_GIC_NR; i++) {
493 case CPU_PM_ENTER_FAILED:
497 case CPU_CLUSTER_PM_ENTER:
500 case CPU_CLUSTER_PM_ENTER_FAILED:
501 case CPU_CLUSTER_PM_EXIT:
510 static struct notifier_block gic_notifier_block = {
511 .notifier_call = gic_notifier,
514 static void __init gic_pm_init(struct gic_chip_data *gic)
516 gic->saved_ppi_enable = __alloc_percpu(DIV_ROUND_UP(32, 32) * 4,
518 BUG_ON(!gic->saved_ppi_enable);
520 gic->saved_ppi_conf = __alloc_percpu(DIV_ROUND_UP(32, 16) * 4,
522 BUG_ON(!gic->saved_ppi_conf);
524 cpu_pm_register_notifier(&gic_notifier_block);
527 static void __init gic_pm_init(struct gic_chip_data *gic)
532 void __init gic_init(unsigned int gic_nr, unsigned int irq_start,
533 void __iomem *dist_base, void __iomem *cpu_base)
535 struct gic_chip_data *gic;
537 BUG_ON(gic_nr >= MAX_GIC_NR);
539 gic = &gic_data[gic_nr];
540 gic->dist_base = dist_base;
541 gic->cpu_base = cpu_base;
542 gic->irq_offset = (irq_start - 1) & ~31;
545 gic_cpu_base_addr = cpu_base;
547 gic_chip.flags |= gic_arch_extn.flags;
548 gic_dist_init(gic, irq_start);
553 void __cpuinit gic_secondary_init(unsigned int gic_nr)
555 BUG_ON(gic_nr >= MAX_GIC_NR);
557 gic_cpu_init(&gic_data[gic_nr]);
560 void __cpuinit gic_enable_ppi(unsigned int irq)
564 local_irq_save(flags);
565 irq_set_status_flags(irq, IRQ_NOPROBE);
566 gic_unmask_irq(irq_get_irq_data(irq));
567 local_irq_restore(flags);
571 void gic_raise_softirq(const struct cpumask *mask, unsigned int irq)
573 unsigned long map = *cpus_addr(*mask);
576 * Ensure that stores to Normal memory are visible to the
577 * other CPUs before issuing the IPI.
581 /* this always happens on GIC0 */
582 writel_relaxed(map << 16 | irq, gic_data[0].dist_base + GIC_DIST_SOFTINT);