blob: e21c1f4218d35fcb09164829ab0ff629adaadc73 [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 */
Russell Kingbef8f9e2010-12-04 16:50:58 +000039void __iomem *gic_cpu_base_addr __read_mostly;
Russell Kingff2e27a2010-12-04 16:13:29 +000040
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
Russell Kingbef8f9e2010-12-04 16:50:58 +000051static struct gic_chip_data gic_data[MAX_GIC_NR] __read_mostly;
Catalin Marinasb3a1bde2007-02-14 19:14:56 +010052
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +010053static inline void __iomem *gic_dist_base(struct irq_data *d)
Catalin Marinasb3a1bde2007-02-14 19:14:56 +010054{
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +010055 struct gic_chip_data *gic_data = irq_data_get_irq_chip_data(d);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +010056 return gic_data->dist_base;
57}
58
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +010059static inline void __iomem *gic_cpu_base(struct irq_data *d)
Catalin Marinasb3a1bde2007-02-14 19:14:56 +010060{
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +010061 struct gic_chip_data *gic_data = irq_data_get_irq_chip_data(d);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +010062 return gic_data->cpu_base;
63}
64
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +010065static inline unsigned int gic_irq(struct irq_data *d)
Catalin Marinasb3a1bde2007-02-14 19:14:56 +010066{
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +010067 struct gic_chip_data *gic_data = irq_data_get_irq_chip_data(d);
68 return d->irq - gic_data->irq_offset;
Catalin Marinasb3a1bde2007-02-14 19:14:56 +010069}
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 */
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +010074static void gic_ack_irq(struct irq_data *d)
Russell Kingf27ecac2005-08-18 21:31:00 +010075{
Thomas Gleixnerc4bfa282006-07-01 22:32:14 +010076 spin_lock(&irq_controller_lock);
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +010077 writel(gic_irq(d), gic_cpu_base(d) + GIC_CPU_EOI);
Thomas Gleixnerc4bfa282006-07-01 22:32:14 +010078 spin_unlock(&irq_controller_lock);
Russell Kingf27ecac2005-08-18 21:31:00 +010079}
80
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +010081static void gic_mask_irq(struct irq_data *d)
Russell Kingf27ecac2005-08-18 21:31:00 +010082{
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +010083 u32 mask = 1 << (d->irq % 32);
Thomas Gleixnerc4bfa282006-07-01 22:32:14 +010084
85 spin_lock(&irq_controller_lock);
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +010086 writel(mask, gic_dist_base(d) + GIC_DIST_ENABLE_CLEAR + (gic_irq(d) / 32) * 4);
Thomas Gleixnerc4bfa282006-07-01 22:32:14 +010087 spin_unlock(&irq_controller_lock);
Russell Kingf27ecac2005-08-18 21:31:00 +010088}
89
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +010090static void gic_unmask_irq(struct irq_data *d)
Russell Kingf27ecac2005-08-18 21:31:00 +010091{
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +010092 u32 mask = 1 << (d->irq % 32);
Thomas Gleixnerc4bfa282006-07-01 22:32:14 +010093
94 spin_lock(&irq_controller_lock);
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +010095 writel(mask, gic_dist_base(d) + GIC_DIST_ENABLE_SET + (gic_irq(d) / 32) * 4);
Thomas Gleixnerc4bfa282006-07-01 22:32:14 +010096 spin_unlock(&irq_controller_lock);
Russell Kingf27ecac2005-08-18 21:31:00 +010097}
98
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +010099static int gic_set_type(struct irq_data *d, unsigned int type)
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100100{
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100101 void __iomem *base = gic_dist_base(d);
102 unsigned int gicirq = gic_irq(d);
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100103 u32 enablemask = 1 << (gicirq % 32);
104 u32 enableoff = (gicirq / 32) * 4;
105 u32 confmask = 0x2 << ((gicirq % 16) * 2);
106 u32 confoff = (gicirq / 16) * 4;
107 bool enabled = false;
108 u32 val;
109
110 /* Interrupt configuration for SGIs can't be changed */
111 if (gicirq < 16)
112 return -EINVAL;
113
114 if (type != IRQ_TYPE_LEVEL_HIGH && type != IRQ_TYPE_EDGE_RISING)
115 return -EINVAL;
116
117 spin_lock(&irq_controller_lock);
118
119 val = readl(base + GIC_DIST_CONFIG + confoff);
120 if (type == IRQ_TYPE_LEVEL_HIGH)
121 val &= ~confmask;
122 else if (type == IRQ_TYPE_EDGE_RISING)
123 val |= confmask;
124
125 /*
126 * As recommended by the spec, disable the interrupt before changing
127 * the configuration
128 */
129 if (readl(base + GIC_DIST_ENABLE_SET + enableoff) & enablemask) {
130 writel(enablemask, base + GIC_DIST_ENABLE_CLEAR + enableoff);
131 enabled = true;
132 }
133
134 writel(val, base + GIC_DIST_CONFIG + confoff);
135
136 if (enabled)
137 writel(enablemask, base + GIC_DIST_ENABLE_SET + enableoff);
138
139 spin_unlock(&irq_controller_lock);
140
141 return 0;
142}
143
Catalin Marinasa06f5462005-09-30 16:07:05 +0100144#ifdef CONFIG_SMP
Russell Kingc1917892011-01-23 12:12:01 +0000145static int gic_set_affinity(struct irq_data *d, const struct cpumask *mask_val,
146 bool force)
Russell Kingf27ecac2005-08-18 21:31:00 +0100147{
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100148 void __iomem *reg = gic_dist_base(d) + GIC_DIST_TARGET + (gic_irq(d) & ~3);
149 unsigned int shift = (d->irq % 4) * 8;
Rusty Russell0de26522008-12-13 21:20:26 +1030150 unsigned int cpu = cpumask_first(mask_val);
Russell Kingc1917892011-01-23 12:12:01 +0000151 u32 val, mask, bit;
152
153 if (cpu >= 8)
154 return -EINVAL;
155
156 mask = 0xff << shift;
157 bit = 1 << (cpu + shift);
Russell Kingf27ecac2005-08-18 21:31:00 +0100158
Thomas Gleixnerc4bfa282006-07-01 22:32:14 +0100159 spin_lock(&irq_controller_lock);
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100160 d->node = cpu;
Russell Kingc1917892011-01-23 12:12:01 +0000161 val = readl(reg) & ~mask;
162 writel(val | bit, reg);
Thomas Gleixnerc4bfa282006-07-01 22:32:14 +0100163 spin_unlock(&irq_controller_lock);
Yinghai Lud5dedd42009-04-27 17:59:21 -0700164
165 return 0;
Russell Kingf27ecac2005-08-18 21:31:00 +0100166}
Catalin Marinasa06f5462005-09-30 16:07:05 +0100167#endif
Russell Kingf27ecac2005-08-18 21:31:00 +0100168
Russell King0f347bb2007-05-17 10:11:34 +0100169static void gic_handle_cascade_irq(unsigned int irq, struct irq_desc *desc)
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100170{
171 struct gic_chip_data *chip_data = get_irq_data(irq);
172 struct irq_chip *chip = get_irq_chip(irq);
Russell King0f347bb2007-05-17 10:11:34 +0100173 unsigned int cascade_irq, gic_irq;
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100174 unsigned long status;
175
176 /* primary controller ack'ing */
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100177 chip->irq_ack(&desc->irq_data);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100178
179 spin_lock(&irq_controller_lock);
180 status = readl(chip_data->cpu_base + GIC_CPU_INTACK);
181 spin_unlock(&irq_controller_lock);
182
Russell King0f347bb2007-05-17 10:11:34 +0100183 gic_irq = (status & 0x3ff);
184 if (gic_irq == 1023)
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100185 goto out;
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100186
Russell King0f347bb2007-05-17 10:11:34 +0100187 cascade_irq = gic_irq + chip_data->irq_offset;
188 if (unlikely(gic_irq < 32 || gic_irq > 1020 || cascade_irq >= NR_IRQS))
189 do_bad_IRQ(cascade_irq, desc);
190 else
191 generic_handle_irq(cascade_irq);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100192
193 out:
194 /* primary controller unmasking */
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100195 chip->irq_unmask(&desc->irq_data);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100196}
197
David Brownell38c677c2006-08-01 22:26:25 +0100198static struct irq_chip gic_chip = {
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100199 .name = "GIC",
200 .irq_ack = gic_ack_irq,
201 .irq_mask = gic_mask_irq,
202 .irq_unmask = gic_unmask_irq,
203 .irq_set_type = gic_set_type,
Russell Kingf27ecac2005-08-18 21:31:00 +0100204#ifdef CONFIG_SMP
Russell Kingc1917892011-01-23 12:12:01 +0000205 .irq_set_affinity = gic_set_affinity,
Russell Kingf27ecac2005-08-18 21:31:00 +0100206#endif
207};
208
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100209void __init gic_cascade_irq(unsigned int gic_nr, unsigned int irq)
210{
211 if (gic_nr >= MAX_GIC_NR)
212 BUG();
213 if (set_irq_data(irq, &gic_data[gic_nr]) != 0)
214 BUG();
215 set_irq_chained_handler(irq, gic_handle_cascade_irq);
216}
217
Russell Kingbef8f9e2010-12-04 16:50:58 +0000218static void __init gic_dist_init(struct gic_chip_data *gic,
Russell Kingb580b892010-12-04 15:55:14 +0000219 unsigned int irq_start)
Russell Kingf27ecac2005-08-18 21:31:00 +0100220{
Pawel Molle6afec92010-11-26 13:45:43 +0100221 unsigned int gic_irqs, irq_limit, i;
Russell Kingbef8f9e2010-12-04 16:50:58 +0000222 void __iomem *base = gic->dist_base;
Russell Kingf27ecac2005-08-18 21:31:00 +0100223 u32 cpumask = 1 << smp_processor_id();
224
225 cpumask |= cpumask << 8;
226 cpumask |= cpumask << 16;
227
Russell Kingf27ecac2005-08-18 21:31:00 +0100228 writel(0, base + GIC_DIST_CTRL);
229
230 /*
231 * Find out how many interrupts are supported.
Russell Kingf27ecac2005-08-18 21:31:00 +0100232 * The GIC only supports up to 1020 interrupt sources.
Russell Kingf27ecac2005-08-18 21:31:00 +0100233 */
Pawel Molle6afec92010-11-26 13:45:43 +0100234 gic_irqs = readl(base + GIC_DIST_CTR) & 0x1f;
235 gic_irqs = (gic_irqs + 1) * 32;
236 if (gic_irqs > 1020)
237 gic_irqs = 1020;
Russell Kingf27ecac2005-08-18 21:31:00 +0100238
239 /*
240 * Set all global interrupts to be level triggered, active low.
241 */
Pawel Molle6afec92010-11-26 13:45:43 +0100242 for (i = 32; i < gic_irqs; i += 16)
Russell Kingf27ecac2005-08-18 21:31:00 +0100243 writel(0, base + GIC_DIST_CONFIG + i * 4 / 16);
244
245 /*
246 * Set all global interrupts to this CPU only.
247 */
Pawel Molle6afec92010-11-26 13:45:43 +0100248 for (i = 32; i < gic_irqs; i += 4)
Russell Kingf27ecac2005-08-18 21:31:00 +0100249 writel(cpumask, base + GIC_DIST_TARGET + i * 4 / 4);
250
251 /*
Russell King9395f6e2010-11-11 23:10:30 +0000252 * Set priority on all global interrupts.
Russell Kingf27ecac2005-08-18 21:31:00 +0100253 */
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(0xa0a0a0a0, base + GIC_DIST_PRI + i * 4 / 4);
256
257 /*
Russell King9395f6e2010-11-11 23:10:30 +0000258 * Disable all interrupts. Leave the PPI and SGIs alone
259 * as these enables are banked registers.
Russell Kingf27ecac2005-08-18 21:31:00 +0100260 */
Pawel Molle6afec92010-11-26 13:45:43 +0100261 for (i = 32; i < gic_irqs; i += 32)
Russell Kingf27ecac2005-08-18 21:31:00 +0100262 writel(0xffffffff, base + GIC_DIST_ENABLE_CLEAR + i * 4 / 32);
263
264 /*
Pawel Molle6afec92010-11-26 13:45:43 +0100265 * Limit number of interrupts registered to the platform maximum
266 */
Russell Kingbef8f9e2010-12-04 16:50:58 +0000267 irq_limit = gic->irq_offset + gic_irqs;
Pawel Molle6afec92010-11-26 13:45:43 +0100268 if (WARN_ON(irq_limit > NR_IRQS))
269 irq_limit = NR_IRQS;
270
271 /*
Russell Kingf27ecac2005-08-18 21:31:00 +0100272 * Setup the Linux IRQ subsystem.
273 */
Pawel Molle6afec92010-11-26 13:45:43 +0100274 for (i = irq_start; i < irq_limit; i++) {
Russell Kingf27ecac2005-08-18 21:31:00 +0100275 set_irq_chip(i, &gic_chip);
Russell Kingbef8f9e2010-12-04 16:50:58 +0000276 set_irq_chip_data(i, gic);
Russell King10dd5ce2006-11-23 11:41:32 +0000277 set_irq_handler(i, handle_level_irq);
Russell Kingf27ecac2005-08-18 21:31:00 +0100278 set_irq_flags(i, IRQF_VALID | IRQF_PROBE);
279 }
280
281 writel(1, base + GIC_DIST_CTRL);
282}
283
Russell Kingbef8f9e2010-12-04 16:50:58 +0000284static void __cpuinit gic_cpu_init(struct gic_chip_data *gic)
Russell Kingf27ecac2005-08-18 21:31:00 +0100285{
Russell Kingbef8f9e2010-12-04 16:50:58 +0000286 void __iomem *dist_base = gic->dist_base;
287 void __iomem *base = gic->cpu_base;
Russell King9395f6e2010-11-11 23:10:30 +0000288 int i;
289
Russell King9395f6e2010-11-11 23:10:30 +0000290 /*
291 * Deal with the banked PPI and SGI interrupts - disable all
292 * PPI interrupts, ensure all SGI interrupts are enabled.
293 */
294 writel(0xffff0000, dist_base + GIC_DIST_ENABLE_CLEAR);
295 writel(0x0000ffff, dist_base + GIC_DIST_ENABLE_SET);
296
297 /*
298 * Set priority on PPI and SGI interrupts
299 */
300 for (i = 0; i < 32; i += 4)
301 writel(0xa0a0a0a0, dist_base + GIC_DIST_PRI + i * 4 / 4);
302
Russell Kingf27ecac2005-08-18 21:31:00 +0100303 writel(0xf0, base + GIC_CPU_PRIMASK);
304 writel(1, base + GIC_CPU_CTRL);
305}
306
Russell Kingb580b892010-12-04 15:55:14 +0000307void __init gic_init(unsigned int gic_nr, unsigned int irq_start,
308 void __iomem *dist_base, void __iomem *cpu_base)
309{
Russell Kingbef8f9e2010-12-04 16:50:58 +0000310 struct gic_chip_data *gic;
311
312 BUG_ON(gic_nr >= MAX_GIC_NR);
313
314 gic = &gic_data[gic_nr];
315 gic->dist_base = dist_base;
316 gic->cpu_base = cpu_base;
317 gic->irq_offset = (irq_start - 1) & ~31;
318
Russell Kingff2e27a2010-12-04 16:13:29 +0000319 if (gic_nr == 0)
320 gic_cpu_base_addr = cpu_base;
Russell Kingbef8f9e2010-12-04 16:50:58 +0000321
322 gic_dist_init(gic, irq_start);
323 gic_cpu_init(gic);
Russell Kingb580b892010-12-04 15:55:14 +0000324}
325
Russell King38489532010-12-04 16:01:03 +0000326void __cpuinit gic_secondary_init(unsigned int gic_nr)
327{
Russell Kingbef8f9e2010-12-04 16:50:58 +0000328 BUG_ON(gic_nr >= MAX_GIC_NR);
329
330 gic_cpu_init(&gic_data[gic_nr]);
Russell King38489532010-12-04 16:01:03 +0000331}
332
Russell Kingac61d142010-12-06 10:38:14 +0000333void __cpuinit gic_enable_ppi(unsigned int irq)
334{
335 unsigned long flags;
336
337 local_irq_save(flags);
338 irq_to_desc(irq)->status |= IRQ_NOPROBE;
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100339 gic_unmask_irq(irq_get_irq_data(irq));
Russell Kingac61d142010-12-06 10:38:14 +0000340 local_irq_restore(flags);
341}
342
Russell Kingf27ecac2005-08-18 21:31:00 +0100343#ifdef CONFIG_SMP
Russell King82668102009-05-17 16:20:18 +0100344void gic_raise_softirq(const struct cpumask *mask, unsigned int irq)
Russell Kingf27ecac2005-08-18 21:31:00 +0100345{
Russell King82668102009-05-17 16:20:18 +0100346 unsigned long map = *cpus_addr(*mask);
Russell Kingf27ecac2005-08-18 21:31:00 +0100347
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100348 /* this always happens on GIC0 */
349 writel(map << 16 | irq, gic_data[0].dist_base + GIC_DIST_SOFTINT);
Russell Kingf27ecac2005-08-18 21:31:00 +0100350}
351#endif