blob: 9105d48c02de16172e6b5ca4aa785d2a4c8d0f26 [file] [log] [blame]
Russell Kingf27ecac2005-08-18 21:31:00 +01001/*
2 * linux/arch/arm/common/gic.c
3 *
4 * Copyright (C) 2002 ARM Limited, All Rights Reserved.
5 *
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.
9 *
10 * Interrupt architecture for the GIC:
11 *
12 * o There is one Interrupt Distributor, which receives interrupts
13 * from system devices and sends them to the Interrupt Controllers.
14 *
15 * o There is one CPU Interface per CPU, which sends interrupts sent
16 * by the Distributor, and interrupts generated locally, to the
Catalin Marinasb3a1bde2007-02-14 19:14:56 +010017 * 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.
Russell Kingf27ecac2005-08-18 21:31:00 +010020 *
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.
24 */
25#include <linux/init.h>
26#include <linux/kernel.h>
27#include <linux/list.h>
28#include <linux/smp.h>
Catalin Marinasdcb86e82005-08-31 21:45:14 +010029#include <linux/cpumask.h>
Russell Kingfced80c2008-09-06 12:10:45 +010030#include <linux/io.h>
Russell Kingf27ecac2005-08-18 21:31:00 +010031
32#include <asm/irq.h>
Russell Kingf27ecac2005-08-18 21:31:00 +010033#include <asm/mach/irq.h>
34#include <asm/hardware/gic.h>
35
Thomas Gleixnerc4bfa282006-07-01 22:32:14 +010036static DEFINE_SPINLOCK(irq_controller_lock);
Russell Kingf27ecac2005-08-18 21:31:00 +010037
Russell Kingff2e27a2010-12-04 16:13:29 +000038/* Address of GIC 0 CPU interface */
39void __iomem *gic_cpu_base_addr;
40
Catalin Marinasb3a1bde2007-02-14 19:14:56 +010041struct gic_chip_data {
42 unsigned int irq_offset;
43 void __iomem *dist_base;
44 void __iomem *cpu_base;
45};
46
47#ifndef MAX_GIC_NR
48#define MAX_GIC_NR 1
49#endif
50
51static struct gic_chip_data gic_data[MAX_GIC_NR];
52
53static inline void __iomem *gic_dist_base(unsigned int irq)
54{
55 struct gic_chip_data *gic_data = get_irq_chip_data(irq);
56 return gic_data->dist_base;
57}
58
59static inline void __iomem *gic_cpu_base(unsigned int irq)
60{
61 struct gic_chip_data *gic_data = get_irq_chip_data(irq);
62 return gic_data->cpu_base;
63}
64
65static inline unsigned int gic_irq(unsigned int irq)
66{
67 struct gic_chip_data *gic_data = get_irq_chip_data(irq);
68 return irq - gic_data->irq_offset;
69}
70
Russell Kingf27ecac2005-08-18 21:31:00 +010071/*
72 * Routines to acknowledge, disable and enable interrupts
Russell Kingf27ecac2005-08-18 21:31:00 +010073 */
74static void gic_ack_irq(unsigned int irq)
75{
Thomas Gleixnerc4bfa282006-07-01 22:32:14 +010076
77 spin_lock(&irq_controller_lock);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +010078 writel(gic_irq(irq), gic_cpu_base(irq) + GIC_CPU_EOI);
Thomas Gleixnerc4bfa282006-07-01 22:32:14 +010079 spin_unlock(&irq_controller_lock);
Russell Kingf27ecac2005-08-18 21:31:00 +010080}
81
82static void gic_mask_irq(unsigned int irq)
83{
84 u32 mask = 1 << (irq % 32);
Thomas Gleixnerc4bfa282006-07-01 22:32:14 +010085
86 spin_lock(&irq_controller_lock);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +010087 writel(mask, gic_dist_base(irq) + GIC_DIST_ENABLE_CLEAR + (gic_irq(irq) / 32) * 4);
Thomas Gleixnerc4bfa282006-07-01 22:32:14 +010088 spin_unlock(&irq_controller_lock);
Russell Kingf27ecac2005-08-18 21:31:00 +010089}
90
91static void gic_unmask_irq(unsigned int irq)
92{
93 u32 mask = 1 << (irq % 32);
Thomas Gleixnerc4bfa282006-07-01 22:32:14 +010094
95 spin_lock(&irq_controller_lock);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +010096 writel(mask, gic_dist_base(irq) + GIC_DIST_ENABLE_SET + (gic_irq(irq) / 32) * 4);
Thomas Gleixnerc4bfa282006-07-01 22:32:14 +010097 spin_unlock(&irq_controller_lock);
Russell Kingf27ecac2005-08-18 21:31:00 +010098}
99
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100100static int gic_set_type(unsigned int irq, unsigned int type)
101{
102 void __iomem *base = gic_dist_base(irq);
103 unsigned int gicirq = gic_irq(irq);
104 u32 enablemask = 1 << (gicirq % 32);
105 u32 enableoff = (gicirq / 32) * 4;
106 u32 confmask = 0x2 << ((gicirq % 16) * 2);
107 u32 confoff = (gicirq / 16) * 4;
108 bool enabled = false;
109 u32 val;
110
111 /* Interrupt configuration for SGIs can't be changed */
112 if (gicirq < 16)
113 return -EINVAL;
114
115 if (type != IRQ_TYPE_LEVEL_HIGH && type != IRQ_TYPE_EDGE_RISING)
116 return -EINVAL;
117
118 spin_lock(&irq_controller_lock);
119
120 val = readl(base + GIC_DIST_CONFIG + confoff);
121 if (type == IRQ_TYPE_LEVEL_HIGH)
122 val &= ~confmask;
123 else if (type == IRQ_TYPE_EDGE_RISING)
124 val |= confmask;
125
126 /*
127 * As recommended by the spec, disable the interrupt before changing
128 * the configuration
129 */
130 if (readl(base + GIC_DIST_ENABLE_SET + enableoff) & enablemask) {
131 writel(enablemask, base + GIC_DIST_ENABLE_CLEAR + enableoff);
132 enabled = true;
133 }
134
135 writel(val, base + GIC_DIST_CONFIG + confoff);
136
137 if (enabled)
138 writel(enablemask, base + GIC_DIST_ENABLE_SET + enableoff);
139
140 spin_unlock(&irq_controller_lock);
141
142 return 0;
143}
144
Catalin Marinasa06f5462005-09-30 16:07:05 +0100145#ifdef CONFIG_SMP
Yinghai Lud5dedd42009-04-27 17:59:21 -0700146static int gic_set_cpu(unsigned int irq, const struct cpumask *mask_val)
Russell Kingf27ecac2005-08-18 21:31:00 +0100147{
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100148 void __iomem *reg = gic_dist_base(irq) + GIC_DIST_TARGET + (gic_irq(irq) & ~3);
Russell Kingf27ecac2005-08-18 21:31:00 +0100149 unsigned int shift = (irq % 4) * 8;
Rusty Russell0de26522008-12-13 21:20:26 +1030150 unsigned int cpu = cpumask_first(mask_val);
Russell Kingf27ecac2005-08-18 21:31:00 +0100151 u32 val;
Chao Xie87507502010-12-06 07:01:10 +0100152 struct irq_desc *desc;
Russell Kingf27ecac2005-08-18 21:31:00 +0100153
Thomas Gleixnerc4bfa282006-07-01 22:32:14 +0100154 spin_lock(&irq_controller_lock);
Chao Xie87507502010-12-06 07:01:10 +0100155 desc = irq_to_desc(irq);
156 if (desc == NULL) {
157 spin_unlock(&irq_controller_lock);
158 return -EINVAL;
159 }
160 desc->node = cpu;
Russell Kingf27ecac2005-08-18 21:31:00 +0100161 val = readl(reg) & ~(0xff << shift);
162 val |= 1 << (cpu + shift);
163 writel(val, reg);
Thomas Gleixnerc4bfa282006-07-01 22:32:14 +0100164 spin_unlock(&irq_controller_lock);
Yinghai Lud5dedd42009-04-27 17:59:21 -0700165
166 return 0;
Russell Kingf27ecac2005-08-18 21:31:00 +0100167}
Catalin Marinasa06f5462005-09-30 16:07:05 +0100168#endif
Russell Kingf27ecac2005-08-18 21:31:00 +0100169
Russell King0f347bb2007-05-17 10:11:34 +0100170static void gic_handle_cascade_irq(unsigned int irq, struct irq_desc *desc)
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100171{
172 struct gic_chip_data *chip_data = get_irq_data(irq);
173 struct irq_chip *chip = get_irq_chip(irq);
Russell King0f347bb2007-05-17 10:11:34 +0100174 unsigned int cascade_irq, gic_irq;
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100175 unsigned long status;
176
177 /* primary controller ack'ing */
178 chip->ack(irq);
179
180 spin_lock(&irq_controller_lock);
181 status = readl(chip_data->cpu_base + GIC_CPU_INTACK);
182 spin_unlock(&irq_controller_lock);
183
Russell King0f347bb2007-05-17 10:11:34 +0100184 gic_irq = (status & 0x3ff);
185 if (gic_irq == 1023)
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100186 goto out;
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100187
Russell King0f347bb2007-05-17 10:11:34 +0100188 cascade_irq = gic_irq + chip_data->irq_offset;
189 if (unlikely(gic_irq < 32 || gic_irq > 1020 || cascade_irq >= NR_IRQS))
190 do_bad_IRQ(cascade_irq, desc);
191 else
192 generic_handle_irq(cascade_irq);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100193
194 out:
195 /* primary controller unmasking */
196 chip->unmask(irq);
197}
198
David Brownell38c677c2006-08-01 22:26:25 +0100199static struct irq_chip gic_chip = {
200 .name = "GIC",
Russell Kingf27ecac2005-08-18 21:31:00 +0100201 .ack = gic_ack_irq,
202 .mask = gic_mask_irq,
203 .unmask = gic_unmask_irq,
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100204 .set_type = gic_set_type,
Russell Kingf27ecac2005-08-18 21:31:00 +0100205#ifdef CONFIG_SMP
Thomas Gleixnerc4bfa282006-07-01 22:32:14 +0100206 .set_affinity = gic_set_cpu,
Russell Kingf27ecac2005-08-18 21:31:00 +0100207#endif
208};
209
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100210void __init gic_cascade_irq(unsigned int gic_nr, unsigned int irq)
211{
212 if (gic_nr >= MAX_GIC_NR)
213 BUG();
214 if (set_irq_data(irq, &gic_data[gic_nr]) != 0)
215 BUG();
216 set_irq_chained_handler(irq, gic_handle_cascade_irq);
217}
218
Russell Kingb580b892010-12-04 15:55:14 +0000219static void __init gic_dist_init(unsigned int gic_nr, void __iomem *base,
220 unsigned int irq_start)
Russell Kingf27ecac2005-08-18 21:31:00 +0100221{
Pawel Molle6afec92010-11-26 13:45:43 +0100222 unsigned int gic_irqs, irq_limit, i;
Russell Kingf27ecac2005-08-18 21:31:00 +0100223 u32 cpumask = 1 << smp_processor_id();
224
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100225 if (gic_nr >= MAX_GIC_NR)
226 BUG();
227
Russell Kingf27ecac2005-08-18 21:31:00 +0100228 cpumask |= cpumask << 8;
229 cpumask |= cpumask << 16;
230
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100231 gic_data[gic_nr].dist_base = base;
232 gic_data[gic_nr].irq_offset = (irq_start - 1) & ~31;
Russell Kingf27ecac2005-08-18 21:31:00 +0100233
234 writel(0, base + GIC_DIST_CTRL);
235
236 /*
237 * Find out how many interrupts are supported.
Russell Kingf27ecac2005-08-18 21:31:00 +0100238 * The GIC only supports up to 1020 interrupt sources.
Russell Kingf27ecac2005-08-18 21:31:00 +0100239 */
Pawel Molle6afec92010-11-26 13:45:43 +0100240 gic_irqs = readl(base + GIC_DIST_CTR) & 0x1f;
241 gic_irqs = (gic_irqs + 1) * 32;
242 if (gic_irqs > 1020)
243 gic_irqs = 1020;
Russell Kingf27ecac2005-08-18 21:31:00 +0100244
245 /*
246 * Set all global interrupts to be level triggered, active low.
247 */
Pawel Molle6afec92010-11-26 13:45:43 +0100248 for (i = 32; i < gic_irqs; i += 16)
Russell Kingf27ecac2005-08-18 21:31:00 +0100249 writel(0, base + GIC_DIST_CONFIG + i * 4 / 16);
250
251 /*
252 * Set all global interrupts to this CPU only.
253 */
Pawel Molle6afec92010-11-26 13:45:43 +0100254 for (i = 32; i < gic_irqs; i += 4)
Russell Kingf27ecac2005-08-18 21:31:00 +0100255 writel(cpumask, base + GIC_DIST_TARGET + i * 4 / 4);
256
257 /*
Russell King9395f6e2010-11-11 23:10:30 +0000258 * Set priority on all global interrupts.
Russell Kingf27ecac2005-08-18 21:31:00 +0100259 */
Pawel Molle6afec92010-11-26 13:45:43 +0100260 for (i = 32; i < gic_irqs; i += 4)
Russell Kingf27ecac2005-08-18 21:31:00 +0100261 writel(0xa0a0a0a0, base + GIC_DIST_PRI + i * 4 / 4);
262
263 /*
Russell King9395f6e2010-11-11 23:10:30 +0000264 * Disable all interrupts. Leave the PPI and SGIs alone
265 * as these enables are banked registers.
Russell Kingf27ecac2005-08-18 21:31:00 +0100266 */
Pawel Molle6afec92010-11-26 13:45:43 +0100267 for (i = 32; i < gic_irqs; i += 32)
Russell Kingf27ecac2005-08-18 21:31:00 +0100268 writel(0xffffffff, base + GIC_DIST_ENABLE_CLEAR + i * 4 / 32);
269
270 /*
Pawel Molle6afec92010-11-26 13:45:43 +0100271 * Limit number of interrupts registered to the platform maximum
272 */
273 irq_limit = gic_data[gic_nr].irq_offset + gic_irqs;
274 if (WARN_ON(irq_limit > NR_IRQS))
275 irq_limit = NR_IRQS;
276
277 /*
Russell Kingf27ecac2005-08-18 21:31:00 +0100278 * Setup the Linux IRQ subsystem.
279 */
Pawel Molle6afec92010-11-26 13:45:43 +0100280 for (i = irq_start; i < irq_limit; i++) {
Russell Kingf27ecac2005-08-18 21:31:00 +0100281 set_irq_chip(i, &gic_chip);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100282 set_irq_chip_data(i, &gic_data[gic_nr]);
Russell King10dd5ce2006-11-23 11:41:32 +0000283 set_irq_handler(i, handle_level_irq);
Russell Kingf27ecac2005-08-18 21:31:00 +0100284 set_irq_flags(i, IRQF_VALID | IRQF_PROBE);
285 }
286
287 writel(1, base + GIC_DIST_CTRL);
288}
289
Russell King38489532010-12-04 16:01:03 +0000290static void __cpuinit gic_cpu_init(unsigned int gic_nr, void __iomem *base)
Russell Kingf27ecac2005-08-18 21:31:00 +0100291{
Russell King9395f6e2010-11-11 23:10:30 +0000292 void __iomem *dist_base;
293 int i;
294
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100295 if (gic_nr >= MAX_GIC_NR)
296 BUG();
297
Russell King9395f6e2010-11-11 23:10:30 +0000298 dist_base = gic_data[gic_nr].dist_base;
299 BUG_ON(!dist_base);
300
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100301 gic_data[gic_nr].cpu_base = base;
302
Russell King9395f6e2010-11-11 23:10:30 +0000303 /*
304 * Deal with the banked PPI and SGI interrupts - disable all
305 * PPI interrupts, ensure all SGI interrupts are enabled.
306 */
307 writel(0xffff0000, dist_base + GIC_DIST_ENABLE_CLEAR);
308 writel(0x0000ffff, dist_base + GIC_DIST_ENABLE_SET);
309
310 /*
311 * Set priority on PPI and SGI interrupts
312 */
313 for (i = 0; i < 32; i += 4)
314 writel(0xa0a0a0a0, dist_base + GIC_DIST_PRI + i * 4 / 4);
315
Russell Kingf27ecac2005-08-18 21:31:00 +0100316 writel(0xf0, base + GIC_CPU_PRIMASK);
317 writel(1, base + GIC_CPU_CTRL);
318}
319
Russell Kingb580b892010-12-04 15:55:14 +0000320void __init gic_init(unsigned int gic_nr, unsigned int irq_start,
321 void __iomem *dist_base, void __iomem *cpu_base)
322{
Russell Kingff2e27a2010-12-04 16:13:29 +0000323 if (gic_nr == 0)
324 gic_cpu_base_addr = cpu_base;
Russell Kingb580b892010-12-04 15:55:14 +0000325 gic_dist_init(gic_nr, dist_base, irq_start);
326 gic_cpu_init(gic_nr, cpu_base);
327}
328
Russell King38489532010-12-04 16:01:03 +0000329void __cpuinit gic_secondary_init(unsigned int gic_nr)
330{
331 gic_cpu_init(gic_nr, gic_data[gic_nr].cpu_base);
332}
333
Russell Kingf27ecac2005-08-18 21:31:00 +0100334#ifdef CONFIG_SMP
Russell King82668102009-05-17 16:20:18 +0100335void gic_raise_softirq(const struct cpumask *mask, unsigned int irq)
Russell Kingf27ecac2005-08-18 21:31:00 +0100336{
Russell King82668102009-05-17 16:20:18 +0100337 unsigned long map = *cpus_addr(*mask);
Russell Kingf27ecac2005-08-18 21:31:00 +0100338
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100339 /* this always happens on GIC0 */
340 writel(map << 16 | irq, gic_data[0].dist_base + GIC_DIST_SOFTINT);
Russell Kingf27ecac2005-08-18 21:31:00 +0100341}
342#endif