blob: e9c2ff83909b6ae00f60d176e3d816b841fb8248 [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
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +010047/*
48 * Supported arch specific GIC irq extension.
49 * Default make them NULL.
50 */
51struct irq_chip gic_arch_extn = {
Will Deacon1a017532011-02-09 12:01:12 +000052 .irq_eoi = NULL,
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +010053 .irq_mask = NULL,
54 .irq_unmask = NULL,
55 .irq_retrigger = NULL,
56 .irq_set_type = NULL,
57 .irq_set_wake = NULL,
58};
59
Catalin Marinasb3a1bde2007-02-14 19:14:56 +010060#ifndef MAX_GIC_NR
61#define MAX_GIC_NR 1
62#endif
63
Russell Kingbef8f9e2010-12-04 16:50:58 +000064static struct gic_chip_data gic_data[MAX_GIC_NR] __read_mostly;
Catalin Marinasb3a1bde2007-02-14 19:14:56 +010065
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +010066static inline void __iomem *gic_dist_base(struct irq_data *d)
Catalin Marinasb3a1bde2007-02-14 19:14:56 +010067{
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +010068 struct gic_chip_data *gic_data = irq_data_get_irq_chip_data(d);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +010069 return gic_data->dist_base;
70}
71
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +010072static inline void __iomem *gic_cpu_base(struct irq_data *d)
Catalin Marinasb3a1bde2007-02-14 19:14:56 +010073{
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +010074 struct gic_chip_data *gic_data = irq_data_get_irq_chip_data(d);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +010075 return gic_data->cpu_base;
76}
77
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +010078static inline unsigned int gic_irq(struct irq_data *d)
Catalin Marinasb3a1bde2007-02-14 19:14:56 +010079{
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +010080 struct gic_chip_data *gic_data = irq_data_get_irq_chip_data(d);
81 return d->irq - gic_data->irq_offset;
Catalin Marinasb3a1bde2007-02-14 19:14:56 +010082}
83
Russell Kingf27ecac2005-08-18 21:31:00 +010084/*
85 * Routines to acknowledge, disable and enable interrupts
Russell Kingf27ecac2005-08-18 21:31:00 +010086 */
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +010087static void gic_mask_irq(struct irq_data *d)
Russell Kingf27ecac2005-08-18 21:31:00 +010088{
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +010089 u32 mask = 1 << (d->irq % 32);
Thomas Gleixnerc4bfa282006-07-01 22:32:14 +010090
91 spin_lock(&irq_controller_lock);
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +010092 writel(mask, gic_dist_base(d) + GIC_DIST_ENABLE_CLEAR + (gic_irq(d) / 32) * 4);
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +010093 if (gic_arch_extn.irq_mask)
94 gic_arch_extn.irq_mask(d);
Thomas Gleixnerc4bfa282006-07-01 22:32:14 +010095 spin_unlock(&irq_controller_lock);
Russell Kingf27ecac2005-08-18 21:31:00 +010096}
97
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +010098static void gic_unmask_irq(struct irq_data *d)
Russell Kingf27ecac2005-08-18 21:31:00 +010099{
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100100 u32 mask = 1 << (d->irq % 32);
Thomas Gleixnerc4bfa282006-07-01 22:32:14 +0100101
102 spin_lock(&irq_controller_lock);
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100103 if (gic_arch_extn.irq_unmask)
104 gic_arch_extn.irq_unmask(d);
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100105 writel(mask, gic_dist_base(d) + GIC_DIST_ENABLE_SET + (gic_irq(d) / 32) * 4);
Thomas Gleixnerc4bfa282006-07-01 22:32:14 +0100106 spin_unlock(&irq_controller_lock);
Russell Kingf27ecac2005-08-18 21:31:00 +0100107}
108
Will Deacon1a017532011-02-09 12:01:12 +0000109static void gic_eoi_irq(struct irq_data *d)
110{
111 if (gic_arch_extn.irq_eoi) {
112 spin_lock(&irq_controller_lock);
113 gic_arch_extn.irq_eoi(d);
114 spin_unlock(&irq_controller_lock);
115 }
116
117 writel(gic_irq(d), gic_cpu_base(d) + GIC_CPU_EOI);
118}
119
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100120static int gic_set_type(struct irq_data *d, unsigned int type)
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100121{
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100122 void __iomem *base = gic_dist_base(d);
123 unsigned int gicirq = gic_irq(d);
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100124 u32 enablemask = 1 << (gicirq % 32);
125 u32 enableoff = (gicirq / 32) * 4;
126 u32 confmask = 0x2 << ((gicirq % 16) * 2);
127 u32 confoff = (gicirq / 16) * 4;
128 bool enabled = false;
129 u32 val;
130
131 /* Interrupt configuration for SGIs can't be changed */
132 if (gicirq < 16)
133 return -EINVAL;
134
135 if (type != IRQ_TYPE_LEVEL_HIGH && type != IRQ_TYPE_EDGE_RISING)
136 return -EINVAL;
137
138 spin_lock(&irq_controller_lock);
139
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100140 if (gic_arch_extn.irq_set_type)
141 gic_arch_extn.irq_set_type(d, type);
142
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100143 val = readl(base + GIC_DIST_CONFIG + confoff);
144 if (type == IRQ_TYPE_LEVEL_HIGH)
145 val &= ~confmask;
146 else if (type == IRQ_TYPE_EDGE_RISING)
147 val |= confmask;
148
149 /*
150 * As recommended by the spec, disable the interrupt before changing
151 * the configuration
152 */
153 if (readl(base + GIC_DIST_ENABLE_SET + enableoff) & enablemask) {
154 writel(enablemask, base + GIC_DIST_ENABLE_CLEAR + enableoff);
155 enabled = true;
156 }
157
158 writel(val, base + GIC_DIST_CONFIG + confoff);
159
160 if (enabled)
161 writel(enablemask, base + GIC_DIST_ENABLE_SET + enableoff);
162
163 spin_unlock(&irq_controller_lock);
164
165 return 0;
166}
167
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100168static int gic_retrigger(struct irq_data *d)
169{
170 if (gic_arch_extn.irq_retrigger)
171 return gic_arch_extn.irq_retrigger(d);
172
173 return -ENXIO;
174}
175
Catalin Marinasa06f5462005-09-30 16:07:05 +0100176#ifdef CONFIG_SMP
Russell Kingc1917892011-01-23 12:12:01 +0000177static int gic_set_affinity(struct irq_data *d, const struct cpumask *mask_val,
178 bool force)
Russell Kingf27ecac2005-08-18 21:31:00 +0100179{
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100180 void __iomem *reg = gic_dist_base(d) + GIC_DIST_TARGET + (gic_irq(d) & ~3);
181 unsigned int shift = (d->irq % 4) * 8;
Rusty Russell0de26522008-12-13 21:20:26 +1030182 unsigned int cpu = cpumask_first(mask_val);
Russell Kingc1917892011-01-23 12:12:01 +0000183 u32 val, mask, bit;
184
185 if (cpu >= 8)
186 return -EINVAL;
187
188 mask = 0xff << shift;
189 bit = 1 << (cpu + shift);
Russell Kingf27ecac2005-08-18 21:31:00 +0100190
Thomas Gleixnerc4bfa282006-07-01 22:32:14 +0100191 spin_lock(&irq_controller_lock);
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100192 d->node = cpu;
Russell Kingc1917892011-01-23 12:12:01 +0000193 val = readl(reg) & ~mask;
194 writel(val | bit, reg);
Thomas Gleixnerc4bfa282006-07-01 22:32:14 +0100195 spin_unlock(&irq_controller_lock);
Yinghai Lud5dedd42009-04-27 17:59:21 -0700196
197 return 0;
Russell Kingf27ecac2005-08-18 21:31:00 +0100198}
Catalin Marinasa06f5462005-09-30 16:07:05 +0100199#endif
Russell Kingf27ecac2005-08-18 21:31:00 +0100200
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100201#ifdef CONFIG_PM
202static int gic_set_wake(struct irq_data *d, unsigned int on)
203{
204 int ret = -ENXIO;
205
206 if (gic_arch_extn.irq_set_wake)
207 ret = gic_arch_extn.irq_set_wake(d, on);
208
209 return ret;
210}
211
212#else
213#define gic_set_wake NULL
214#endif
215
Russell King0f347bb2007-05-17 10:11:34 +0100216static void gic_handle_cascade_irq(unsigned int irq, struct irq_desc *desc)
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100217{
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100218 struct gic_chip_data *chip_data = irq_get_handler_data(irq);
219 struct irq_chip *chip = irq_get_chip(irq);
Russell King0f347bb2007-05-17 10:11:34 +0100220 unsigned int cascade_irq, gic_irq;
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100221 unsigned long status;
222
Will Deacon1a017532011-02-09 12:01:12 +0000223 chained_irq_enter(chip, desc);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100224
225 spin_lock(&irq_controller_lock);
226 status = readl(chip_data->cpu_base + GIC_CPU_INTACK);
227 spin_unlock(&irq_controller_lock);
228
Russell King0f347bb2007-05-17 10:11:34 +0100229 gic_irq = (status & 0x3ff);
230 if (gic_irq == 1023)
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100231 goto out;
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100232
Russell King0f347bb2007-05-17 10:11:34 +0100233 cascade_irq = gic_irq + chip_data->irq_offset;
234 if (unlikely(gic_irq < 32 || gic_irq > 1020 || cascade_irq >= NR_IRQS))
235 do_bad_IRQ(cascade_irq, desc);
236 else
237 generic_handle_irq(cascade_irq);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100238
239 out:
Will Deacon1a017532011-02-09 12:01:12 +0000240 chained_irq_exit(chip, desc);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100241}
242
David Brownell38c677c2006-08-01 22:26:25 +0100243static struct irq_chip gic_chip = {
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100244 .name = "GIC",
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100245 .irq_mask = gic_mask_irq,
246 .irq_unmask = gic_unmask_irq,
Will Deacon1a017532011-02-09 12:01:12 +0000247 .irq_eoi = gic_eoi_irq,
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100248 .irq_set_type = gic_set_type,
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100249 .irq_retrigger = gic_retrigger,
Russell Kingf27ecac2005-08-18 21:31:00 +0100250#ifdef CONFIG_SMP
Russell Kingc1917892011-01-23 12:12:01 +0000251 .irq_set_affinity = gic_set_affinity,
Russell Kingf27ecac2005-08-18 21:31:00 +0100252#endif
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100253 .irq_set_wake = gic_set_wake,
Russell Kingf27ecac2005-08-18 21:31:00 +0100254};
255
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100256void __init gic_cascade_irq(unsigned int gic_nr, unsigned int irq)
257{
258 if (gic_nr >= MAX_GIC_NR)
259 BUG();
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100260 if (irq_set_handler_data(irq, &gic_data[gic_nr]) != 0)
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100261 BUG();
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100262 irq_set_chained_handler(irq, gic_handle_cascade_irq);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100263}
264
Russell Kingbef8f9e2010-12-04 16:50:58 +0000265static void __init gic_dist_init(struct gic_chip_data *gic,
Russell Kingb580b892010-12-04 15:55:14 +0000266 unsigned int irq_start)
Russell Kingf27ecac2005-08-18 21:31:00 +0100267{
Pawel Molle6afec92010-11-26 13:45:43 +0100268 unsigned int gic_irqs, irq_limit, i;
Russell Kingbef8f9e2010-12-04 16:50:58 +0000269 void __iomem *base = gic->dist_base;
Russell Kingf27ecac2005-08-18 21:31:00 +0100270 u32 cpumask = 1 << smp_processor_id();
271
272 cpumask |= cpumask << 8;
273 cpumask |= cpumask << 16;
274
Russell Kingf27ecac2005-08-18 21:31:00 +0100275 writel(0, base + GIC_DIST_CTRL);
276
277 /*
278 * Find out how many interrupts are supported.
Russell Kingf27ecac2005-08-18 21:31:00 +0100279 * The GIC only supports up to 1020 interrupt sources.
Russell Kingf27ecac2005-08-18 21:31:00 +0100280 */
Pawel Molle6afec92010-11-26 13:45:43 +0100281 gic_irqs = readl(base + GIC_DIST_CTR) & 0x1f;
282 gic_irqs = (gic_irqs + 1) * 32;
283 if (gic_irqs > 1020)
284 gic_irqs = 1020;
Russell Kingf27ecac2005-08-18 21:31:00 +0100285
286 /*
287 * Set all global interrupts to be level triggered, active low.
288 */
Pawel Molle6afec92010-11-26 13:45:43 +0100289 for (i = 32; i < gic_irqs; i += 16)
Russell Kingf27ecac2005-08-18 21:31:00 +0100290 writel(0, base + GIC_DIST_CONFIG + i * 4 / 16);
291
292 /*
293 * Set all global interrupts to this CPU only.
294 */
Pawel Molle6afec92010-11-26 13:45:43 +0100295 for (i = 32; i < gic_irqs; i += 4)
Russell Kingf27ecac2005-08-18 21:31:00 +0100296 writel(cpumask, base + GIC_DIST_TARGET + i * 4 / 4);
297
298 /*
Russell King9395f6e2010-11-11 23:10:30 +0000299 * Set priority on all global interrupts.
Russell Kingf27ecac2005-08-18 21:31:00 +0100300 */
Pawel Molle6afec92010-11-26 13:45:43 +0100301 for (i = 32; i < gic_irqs; i += 4)
Russell Kingf27ecac2005-08-18 21:31:00 +0100302 writel(0xa0a0a0a0, base + GIC_DIST_PRI + i * 4 / 4);
303
304 /*
Russell King9395f6e2010-11-11 23:10:30 +0000305 * Disable all interrupts. Leave the PPI and SGIs alone
306 * as these enables are banked registers.
Russell Kingf27ecac2005-08-18 21:31:00 +0100307 */
Pawel Molle6afec92010-11-26 13:45:43 +0100308 for (i = 32; i < gic_irqs; i += 32)
Russell Kingf27ecac2005-08-18 21:31:00 +0100309 writel(0xffffffff, base + GIC_DIST_ENABLE_CLEAR + i * 4 / 32);
310
311 /*
Pawel Molle6afec92010-11-26 13:45:43 +0100312 * Limit number of interrupts registered to the platform maximum
313 */
Russell Kingbef8f9e2010-12-04 16:50:58 +0000314 irq_limit = gic->irq_offset + gic_irqs;
Pawel Molle6afec92010-11-26 13:45:43 +0100315 if (WARN_ON(irq_limit > NR_IRQS))
316 irq_limit = NR_IRQS;
317
318 /*
Russell Kingf27ecac2005-08-18 21:31:00 +0100319 * Setup the Linux IRQ subsystem.
320 */
Pawel Molle6afec92010-11-26 13:45:43 +0100321 for (i = irq_start; i < irq_limit; i++) {
Will Deacon1a017532011-02-09 12:01:12 +0000322 irq_set_chip_and_handler(i, &gic_chip, handle_fasteoi_irq);
Thomas Gleixner9323f2612011-03-24 13:29:39 +0100323 irq_set_chip_data(i, gic);
Russell Kingf27ecac2005-08-18 21:31:00 +0100324 set_irq_flags(i, IRQF_VALID | IRQF_PROBE);
325 }
326
327 writel(1, base + GIC_DIST_CTRL);
328}
329
Russell Kingbef8f9e2010-12-04 16:50:58 +0000330static void __cpuinit gic_cpu_init(struct gic_chip_data *gic)
Russell Kingf27ecac2005-08-18 21:31:00 +0100331{
Russell Kingbef8f9e2010-12-04 16:50:58 +0000332 void __iomem *dist_base = gic->dist_base;
333 void __iomem *base = gic->cpu_base;
Russell King9395f6e2010-11-11 23:10:30 +0000334 int i;
335
Russell King9395f6e2010-11-11 23:10:30 +0000336 /*
337 * Deal with the banked PPI and SGI interrupts - disable all
338 * PPI interrupts, ensure all SGI interrupts are enabled.
339 */
340 writel(0xffff0000, dist_base + GIC_DIST_ENABLE_CLEAR);
341 writel(0x0000ffff, dist_base + GIC_DIST_ENABLE_SET);
342
343 /*
344 * Set priority on PPI and SGI interrupts
345 */
346 for (i = 0; i < 32; i += 4)
347 writel(0xa0a0a0a0, dist_base + GIC_DIST_PRI + i * 4 / 4);
348
Russell Kingf27ecac2005-08-18 21:31:00 +0100349 writel(0xf0, base + GIC_CPU_PRIMASK);
350 writel(1, base + GIC_CPU_CTRL);
351}
352
Russell Kingb580b892010-12-04 15:55:14 +0000353void __init gic_init(unsigned int gic_nr, unsigned int irq_start,
354 void __iomem *dist_base, void __iomem *cpu_base)
355{
Russell Kingbef8f9e2010-12-04 16:50:58 +0000356 struct gic_chip_data *gic;
357
358 BUG_ON(gic_nr >= MAX_GIC_NR);
359
360 gic = &gic_data[gic_nr];
361 gic->dist_base = dist_base;
362 gic->cpu_base = cpu_base;
363 gic->irq_offset = (irq_start - 1) & ~31;
364
Russell Kingff2e27a2010-12-04 16:13:29 +0000365 if (gic_nr == 0)
366 gic_cpu_base_addr = cpu_base;
Russell Kingbef8f9e2010-12-04 16:50:58 +0000367
368 gic_dist_init(gic, irq_start);
369 gic_cpu_init(gic);
Russell Kingb580b892010-12-04 15:55:14 +0000370}
371
Russell King38489532010-12-04 16:01:03 +0000372void __cpuinit gic_secondary_init(unsigned int gic_nr)
373{
Russell Kingbef8f9e2010-12-04 16:50:58 +0000374 BUG_ON(gic_nr >= MAX_GIC_NR);
375
376 gic_cpu_init(&gic_data[gic_nr]);
Russell King38489532010-12-04 16:01:03 +0000377}
378
Russell Kingac61d142010-12-06 10:38:14 +0000379void __cpuinit gic_enable_ppi(unsigned int irq)
380{
381 unsigned long flags;
382
383 local_irq_save(flags);
Thomas Gleixnerfdea77b2011-03-24 12:48:54 +0100384 irq_set_status_flags(irq, IRQ_NOPROBE);
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100385 gic_unmask_irq(irq_get_irq_data(irq));
Russell Kingac61d142010-12-06 10:38:14 +0000386 local_irq_restore(flags);
387}
388
Russell Kingf27ecac2005-08-18 21:31:00 +0100389#ifdef CONFIG_SMP
Russell King82668102009-05-17 16:20:18 +0100390void gic_raise_softirq(const struct cpumask *mask, unsigned int irq)
Russell Kingf27ecac2005-08-18 21:31:00 +0100391{
Russell King82668102009-05-17 16:20:18 +0100392 unsigned long map = *cpus_addr(*mask);
Russell Kingf27ecac2005-08-18 21:31:00 +0100393
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100394 /* this always happens on GIC0 */
395 writel(map << 16 | irq, gic_data[0].dist_base + GIC_DIST_SOFTINT);
Russell Kingf27ecac2005-08-18 21:31:00 +0100396}
397#endif