blob: 8f809b80fb7cb99d765e09ec679577e291801713 [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>
Colin Cross692c3e252011-02-10 12:54:10 -080029#include <linux/cpu_pm.h>
Catalin Marinasdcb86e82005-08-31 21:45:14 +010030#include <linux/cpumask.h>
Russell Kingfced80c2008-09-06 12:10:45 +010031#include <linux/io.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070032#include <linux/syscore_ops.h>
Trilok Sonieecb28c2011-07-20 16:24:14 +010033#include <linux/interrupt.h>
34#include <linux/percpu.h>
35#include <linux/slab.h>
Russell Kingf27ecac2005-08-18 21:31:00 +010036
37#include <asm/irq.h>
Russell Kingf27ecac2005-08-18 21:31:00 +010038#include <asm/mach/irq.h>
39#include <asm/hardware/gic.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070040#include <asm/system.h>
Trilok Sonieecb28c2011-07-20 16:24:14 +010041#include <asm/localtimer.h>
Russell Kingf27ecac2005-08-18 21:31:00 +010042
Thomas Gleixner450ea482009-07-03 08:44:46 -050043static DEFINE_RAW_SPINLOCK(irq_controller_lock);
Russell Kingf27ecac2005-08-18 21:31:00 +010044
Russell Kingff2e27a2010-12-04 16:13:29 +000045/* Address of GIC 0 CPU interface */
Russell Kingbef8f9e2010-12-04 16:50:58 +000046void __iomem *gic_cpu_base_addr __read_mostly;
Russell Kingff2e27a2010-12-04 16:13:29 +000047
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +010048/*
49 * Supported arch specific GIC irq extension.
50 * Default make them NULL.
51 */
52struct irq_chip gic_arch_extn = {
Will Deacon1a017532011-02-09 12:01:12 +000053 .irq_eoi = NULL,
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +010054 .irq_mask = NULL,
55 .irq_unmask = NULL,
56 .irq_retrigger = NULL,
57 .irq_set_type = NULL,
58 .irq_set_wake = NULL,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070059 .irq_disable = NULL,
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +010060};
61
Catalin Marinasb3a1bde2007-02-14 19:14:56 +010062#ifndef MAX_GIC_NR
63#define MAX_GIC_NR 1
64#endif
65
Russell Kingbef8f9e2010-12-04 16:50:58 +000066static struct gic_chip_data gic_data[MAX_GIC_NR] __read_mostly;
Catalin Marinasb3a1bde2007-02-14 19:14:56 +010067
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +010068static inline void __iomem *gic_dist_base(struct irq_data *d)
Catalin Marinasb3a1bde2007-02-14 19:14:56 +010069{
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +010070 struct gic_chip_data *gic_data = irq_data_get_irq_chip_data(d);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +010071 return gic_data->dist_base;
72}
73
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +010074static inline void __iomem *gic_cpu_base(struct irq_data *d)
Catalin Marinasb3a1bde2007-02-14 19:14:56 +010075{
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +010076 struct gic_chip_data *gic_data = irq_data_get_irq_chip_data(d);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +010077 return gic_data->cpu_base;
78}
79
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +010080static inline unsigned int gic_irq(struct irq_data *d)
Catalin Marinasb3a1bde2007-02-14 19:14:56 +010081{
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +010082 struct gic_chip_data *gic_data = irq_data_get_irq_chip_data(d);
83 return d->irq - gic_data->irq_offset;
Catalin Marinasb3a1bde2007-02-14 19:14:56 +010084}
85
Russell Kingf27ecac2005-08-18 21:31:00 +010086/*
87 * Routines to acknowledge, disable and enable interrupts
Russell Kingf27ecac2005-08-18 21:31:00 +010088 */
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +010089static void gic_mask_irq(struct irq_data *d)
Russell Kingf27ecac2005-08-18 21:31:00 +010090{
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +010091 u32 mask = 1 << (d->irq % 32);
Thomas Gleixnerc4bfa282006-07-01 22:32:14 +010092
Thomas Gleixner450ea482009-07-03 08:44:46 -050093 raw_spin_lock(&irq_controller_lock);
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +053094 writel_relaxed(mask, gic_dist_base(d) + GIC_DIST_ENABLE_CLEAR + (gic_irq(d) / 32) * 4);
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +010095 if (gic_arch_extn.irq_mask)
96 gic_arch_extn.irq_mask(d);
Thomas Gleixner450ea482009-07-03 08:44:46 -050097 raw_spin_unlock(&irq_controller_lock);
Russell Kingf27ecac2005-08-18 21:31:00 +010098}
99
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100100static void gic_unmask_irq(struct irq_data *d)
Russell Kingf27ecac2005-08-18 21:31:00 +0100101{
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100102 u32 mask = 1 << (d->irq % 32);
Thomas Gleixnerc4bfa282006-07-01 22:32:14 +0100103
Thomas Gleixner450ea482009-07-03 08:44:46 -0500104 raw_spin_lock(&irq_controller_lock);
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100105 if (gic_arch_extn.irq_unmask)
106 gic_arch_extn.irq_unmask(d);
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530107 writel_relaxed(mask, gic_dist_base(d) + GIC_DIST_ENABLE_SET + (gic_irq(d) / 32) * 4);
Thomas Gleixner450ea482009-07-03 08:44:46 -0500108 raw_spin_unlock(&irq_controller_lock);
Russell Kingf27ecac2005-08-18 21:31:00 +0100109}
110
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700111static void gic_disable_irq(struct irq_data *d)
112{
113 if (gic_arch_extn.irq_disable)
114 gic_arch_extn.irq_disable(d);
115}
116
117#ifdef CONFIG_PM
118static int gic_suspend_one(struct gic_chip_data *gic)
119{
120 unsigned int i;
121 void __iomem *base = gic->dist_base;
122
123 for (i = 0; i * 32 < gic->max_irq; i++) {
124 gic->enabled_irqs[i]
125 = readl_relaxed(base + GIC_DIST_ENABLE_SET + i * 4);
126 /* disable all of them */
127 writel_relaxed(0xffffffff, base + GIC_DIST_ENABLE_CLEAR + i * 4);
128 /* enable the wakeup set */
129 writel_relaxed(gic->wakeup_irqs[i],
130 base + GIC_DIST_ENABLE_SET + i * 4);
131 }
132 mb();
133 return 0;
134}
135
136static int gic_suspend(void)
137{
138 int i;
139 for (i = 0; i < MAX_GIC_NR; i++)
140 gic_suspend_one(&gic_data[i]);
141 return 0;
142}
143
144extern int msm_show_resume_irq_mask;
145
146static void gic_show_resume_irq(struct gic_chip_data *gic)
147{
148 unsigned int i;
149 u32 enabled;
150 unsigned long pending[32];
151 void __iomem *base = gic->dist_base;
152
153 if (!msm_show_resume_irq_mask)
154 return;
155
Thomas Gleixner450ea482009-07-03 08:44:46 -0500156 raw_spin_lock(&irq_controller_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700157 for (i = 0; i * 32 < gic->max_irq; i++) {
158 enabled = readl_relaxed(base + GIC_DIST_ENABLE_CLEAR + i * 4);
159 pending[i] = readl_relaxed(base + GIC_DIST_PENDING_SET + i * 4);
160 pending[i] &= enabled;
161 }
Thomas Gleixner450ea482009-07-03 08:44:46 -0500162 raw_spin_unlock(&irq_controller_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700163
164 for (i = find_first_bit(pending, gic->max_irq);
165 i < gic->max_irq;
166 i = find_next_bit(pending, gic->max_irq, i+1)) {
167 pr_warning("%s: %d triggered", __func__,
168 i + gic->irq_offset);
169 }
170}
171
172static void gic_resume_one(struct gic_chip_data *gic)
173{
174 unsigned int i;
175 void __iomem *base = gic->dist_base;
176
177 gic_show_resume_irq(gic);
178 for (i = 0; i * 32 < gic->max_irq; i++) {
179 /* disable all of them */
180 writel_relaxed(0xffffffff, base + GIC_DIST_ENABLE_CLEAR + i * 4);
181 /* enable the enabled set */
182 writel_relaxed(gic->enabled_irqs[i],
183 base + GIC_DIST_ENABLE_SET + i * 4);
184 }
185 mb();
186}
187
188static void gic_resume(void)
189{
190 int i;
191 for (i = 0; i < MAX_GIC_NR; i++)
192 gic_resume_one(&gic_data[i]);
193}
194
195static struct syscore_ops gic_syscore_ops = {
196 .suspend = gic_suspend,
197 .resume = gic_resume,
198};
199
200static int __init gic_init_sys(void)
201{
202 register_syscore_ops(&gic_syscore_ops);
203 return 0;
204}
205arch_initcall(gic_init_sys);
206
207#endif
208
Will Deacon1a017532011-02-09 12:01:12 +0000209static void gic_eoi_irq(struct irq_data *d)
210{
211 if (gic_arch_extn.irq_eoi) {
Thomas Gleixner450ea482009-07-03 08:44:46 -0500212 raw_spin_lock(&irq_controller_lock);
Will Deacon1a017532011-02-09 12:01:12 +0000213 gic_arch_extn.irq_eoi(d);
Thomas Gleixner450ea482009-07-03 08:44:46 -0500214 raw_spin_unlock(&irq_controller_lock);
Will Deacon1a017532011-02-09 12:01:12 +0000215 }
216
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530217 writel_relaxed(gic_irq(d), gic_cpu_base(d) + GIC_CPU_EOI);
Will Deacon1a017532011-02-09 12:01:12 +0000218}
219
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100220static int gic_set_type(struct irq_data *d, unsigned int type)
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100221{
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100222 void __iomem *base = gic_dist_base(d);
223 unsigned int gicirq = gic_irq(d);
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100224 u32 enablemask = 1 << (gicirq % 32);
225 u32 enableoff = (gicirq / 32) * 4;
226 u32 confmask = 0x2 << ((gicirq % 16) * 2);
227 u32 confoff = (gicirq / 16) * 4;
228 bool enabled = false;
229 u32 val;
230
231 /* Interrupt configuration for SGIs can't be changed */
232 if (gicirq < 16)
233 return -EINVAL;
234
235 if (type != IRQ_TYPE_LEVEL_HIGH && type != IRQ_TYPE_EDGE_RISING)
236 return -EINVAL;
237
Thomas Gleixner450ea482009-07-03 08:44:46 -0500238 raw_spin_lock(&irq_controller_lock);
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100239
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100240 if (gic_arch_extn.irq_set_type)
241 gic_arch_extn.irq_set_type(d, type);
242
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530243 val = readl_relaxed(base + GIC_DIST_CONFIG + confoff);
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100244 if (type == IRQ_TYPE_LEVEL_HIGH)
245 val &= ~confmask;
246 else if (type == IRQ_TYPE_EDGE_RISING)
247 val |= confmask;
248
249 /*
250 * As recommended by the spec, disable the interrupt before changing
251 * the configuration
252 */
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530253 if (readl_relaxed(base + GIC_DIST_ENABLE_SET + enableoff) & enablemask) {
254 writel_relaxed(enablemask, base + GIC_DIST_ENABLE_CLEAR + enableoff);
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100255 enabled = true;
256 }
257
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530258 writel_relaxed(val, base + GIC_DIST_CONFIG + confoff);
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100259
260 if (enabled)
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530261 writel_relaxed(enablemask, base + GIC_DIST_ENABLE_SET + enableoff);
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100262
Thomas Gleixner450ea482009-07-03 08:44:46 -0500263 raw_spin_unlock(&irq_controller_lock);
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100264
265 return 0;
266}
267
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100268static int gic_retrigger(struct irq_data *d)
269{
270 if (gic_arch_extn.irq_retrigger)
271 return gic_arch_extn.irq_retrigger(d);
272
Abhijeet Dharmapurikar9d44ea02011-10-30 16:47:19 -0700273 /* the retrigger expects 0 for failure */
274 return 0;
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100275}
276
Catalin Marinasa06f5462005-09-30 16:07:05 +0100277#ifdef CONFIG_SMP
Russell Kingc1917892011-01-23 12:12:01 +0000278static int gic_set_affinity(struct irq_data *d, const struct cpumask *mask_val,
279 bool force)
Russell Kingf27ecac2005-08-18 21:31:00 +0100280{
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100281 void __iomem *reg = gic_dist_base(d) + GIC_DIST_TARGET + (gic_irq(d) & ~3);
282 unsigned int shift = (d->irq % 4) * 8;
Russell Kingf3c52e22011-07-21 15:00:57 +0100283 unsigned int cpu = cpumask_any_and(mask_val, cpu_online_mask);
Russell Kingc1917892011-01-23 12:12:01 +0000284 u32 val, mask, bit;
285
Russell Kingf3c52e22011-07-21 15:00:57 +0100286 if (cpu >= 8 || cpu >= nr_cpu_ids)
Russell Kingc1917892011-01-23 12:12:01 +0000287 return -EINVAL;
288
289 mask = 0xff << shift;
Will Deacona803a8d2011-08-23 22:20:03 +0100290 bit = 1 << (cpu_logical_map(cpu) + shift);
Russell Kingf27ecac2005-08-18 21:31:00 +0100291
Thomas Gleixner450ea482009-07-03 08:44:46 -0500292 raw_spin_lock(&irq_controller_lock);
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530293 val = readl_relaxed(reg) & ~mask;
294 writel_relaxed(val | bit, reg);
Thomas Gleixner450ea482009-07-03 08:44:46 -0500295 raw_spin_unlock(&irq_controller_lock);
Yinghai Lud5dedd42009-04-27 17:59:21 -0700296
Russell Kingf3c52e22011-07-21 15:00:57 +0100297 return IRQ_SET_MASK_OK;
Russell Kingf27ecac2005-08-18 21:31:00 +0100298}
Catalin Marinasa06f5462005-09-30 16:07:05 +0100299#endif
Russell Kingf27ecac2005-08-18 21:31:00 +0100300
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100301#ifdef CONFIG_PM
302static int gic_set_wake(struct irq_data *d, unsigned int on)
303{
304 int ret = -ENXIO;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700305 unsigned int reg_offset, bit_offset;
306 unsigned int gicirq = gic_irq(d);
307 struct gic_chip_data *gic_data = irq_data_get_irq_chip_data(d);
308
309 /* per-cpu interrupts cannot be wakeup interrupts */
310 WARN_ON(gicirq < 32);
311
312 reg_offset = gicirq / 32;
313 bit_offset = gicirq % 32;
314
315 if (on)
316 gic_data->wakeup_irqs[reg_offset] |= 1 << bit_offset;
317 else
318 gic_data->wakeup_irqs[reg_offset] &= ~(1 << bit_offset);
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100319
320 if (gic_arch_extn.irq_set_wake)
321 ret = gic_arch_extn.irq_set_wake(d, on);
322
323 return ret;
324}
325
326#else
Rohit Vaswani550aa1a2011-10-06 21:15:37 -0700327static int gic_set_wake(struct irq_data *d, unsigned int on)
328{
329 return 0;
330}
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100331#endif
332
Russell King0f347bb2007-05-17 10:11:34 +0100333static void gic_handle_cascade_irq(unsigned int irq, struct irq_desc *desc)
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100334{
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100335 struct gic_chip_data *chip_data = irq_get_handler_data(irq);
336 struct irq_chip *chip = irq_get_chip(irq);
Russell King0f347bb2007-05-17 10:11:34 +0100337 unsigned int cascade_irq, gic_irq;
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100338 unsigned long status;
339
Will Deacon1a017532011-02-09 12:01:12 +0000340 chained_irq_enter(chip, desc);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100341
Thomas Gleixner450ea482009-07-03 08:44:46 -0500342 raw_spin_lock(&irq_controller_lock);
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530343 status = readl_relaxed(chip_data->cpu_base + GIC_CPU_INTACK);
Thomas Gleixner450ea482009-07-03 08:44:46 -0500344 raw_spin_unlock(&irq_controller_lock);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100345
Russell King0f347bb2007-05-17 10:11:34 +0100346 gic_irq = (status & 0x3ff);
347 if (gic_irq == 1023)
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100348 goto out;
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100349
Russell King0f347bb2007-05-17 10:11:34 +0100350 cascade_irq = gic_irq + chip_data->irq_offset;
351 if (unlikely(gic_irq < 32 || gic_irq > 1020 || cascade_irq >= NR_IRQS))
352 do_bad_IRQ(cascade_irq, desc);
353 else
354 generic_handle_irq(cascade_irq);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100355
356 out:
Will Deacon1a017532011-02-09 12:01:12 +0000357 chained_irq_exit(chip, desc);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100358}
359
David Brownell38c677c2006-08-01 22:26:25 +0100360static struct irq_chip gic_chip = {
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100361 .name = "GIC",
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100362 .irq_mask = gic_mask_irq,
363 .irq_unmask = gic_unmask_irq,
Will Deacon1a017532011-02-09 12:01:12 +0000364 .irq_eoi = gic_eoi_irq,
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100365 .irq_set_type = gic_set_type,
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100366 .irq_retrigger = gic_retrigger,
Russell Kingf27ecac2005-08-18 21:31:00 +0100367#ifdef CONFIG_SMP
Russell Kingc1917892011-01-23 12:12:01 +0000368 .irq_set_affinity = gic_set_affinity,
Russell Kingf27ecac2005-08-18 21:31:00 +0100369#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700370 .irq_disable = gic_disable_irq,
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100371 .irq_set_wake = gic_set_wake,
Russell Kingf27ecac2005-08-18 21:31:00 +0100372};
373
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100374void __init gic_cascade_irq(unsigned int gic_nr, unsigned int irq)
375{
376 if (gic_nr >= MAX_GIC_NR)
377 BUG();
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100378 if (irq_set_handler_data(irq, &gic_data[gic_nr]) != 0)
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100379 BUG();
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100380 irq_set_chained_handler(irq, gic_handle_cascade_irq);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100381}
382
Russell Kingbef8f9e2010-12-04 16:50:58 +0000383static void __init gic_dist_init(struct gic_chip_data *gic,
Russell Kingb580b892010-12-04 15:55:14 +0000384 unsigned int irq_start)
Russell Kingf27ecac2005-08-18 21:31:00 +0100385{
Pawel Molle6afec92010-11-26 13:45:43 +0100386 unsigned int gic_irqs, irq_limit, i;
Will Deacona803a8d2011-08-23 22:20:03 +0100387 u32 cpumask;
Russell Kingbef8f9e2010-12-04 16:50:58 +0000388 void __iomem *base = gic->dist_base;
Will Deacona803a8d2011-08-23 22:20:03 +0100389 u32 cpu = 0;
Trilok Sonieecb28c2011-07-20 16:24:14 +0100390 u32 nrppis = 0, ppi_base = 0;
Russell Kingf27ecac2005-08-18 21:31:00 +0100391
Will Deacona803a8d2011-08-23 22:20:03 +0100392#ifdef CONFIG_SMP
393 cpu = cpu_logical_map(smp_processor_id());
394#endif
395
396 cpumask = 1 << cpu;
Russell Kingf27ecac2005-08-18 21:31:00 +0100397 cpumask |= cpumask << 8;
398 cpumask |= cpumask << 16;
399
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530400 writel_relaxed(0, base + GIC_DIST_CTRL);
Russell Kingf27ecac2005-08-18 21:31:00 +0100401
402 /*
403 * Find out how many interrupts are supported.
Russell Kingf27ecac2005-08-18 21:31:00 +0100404 * The GIC only supports up to 1020 interrupt sources.
Russell Kingf27ecac2005-08-18 21:31:00 +0100405 */
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530406 gic_irqs = readl_relaxed(base + GIC_DIST_CTR) & 0x1f;
Pawel Molle6afec92010-11-26 13:45:43 +0100407 gic_irqs = (gic_irqs + 1) * 32;
408 if (gic_irqs > 1020)
409 gic_irqs = 1020;
Russell Kingf27ecac2005-08-18 21:31:00 +0100410
Colin Cross692c3e252011-02-10 12:54:10 -0800411 gic->gic_irqs = gic_irqs;
412
Russell Kingf27ecac2005-08-18 21:31:00 +0100413 /*
Trilok Sonieecb28c2011-07-20 16:24:14 +0100414 * Nobody would be insane enough to use PPIs on a secondary
415 * GIC, right?
416 */
417 if (gic == &gic_data[0]) {
418 nrppis = (32 - irq_start) & 31;
419
420 /* The GIC only supports up to 16 PPIs. */
421 if (nrppis > 16)
422 BUG();
423
424 ppi_base = gic->irq_offset + 32 - nrppis;
425 }
426
427 pr_info("Configuring GIC with %d sources (%d PPIs)\n",
428 gic_irqs, (gic == &gic_data[0]) ? nrppis : 0);
429
430 /*
Russell Kingf27ecac2005-08-18 21:31:00 +0100431 * Set all global interrupts to be level triggered, active low.
432 */
Pawel Molle6afec92010-11-26 13:45:43 +0100433 for (i = 32; i < gic_irqs; i += 16)
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530434 writel_relaxed(0, base + GIC_DIST_CONFIG + i * 4 / 16);
Russell Kingf27ecac2005-08-18 21:31:00 +0100435
436 /*
437 * Set all global interrupts to this CPU only.
438 */
Pawel Molle6afec92010-11-26 13:45:43 +0100439 for (i = 32; i < gic_irqs; i += 4)
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530440 writel_relaxed(cpumask, base + GIC_DIST_TARGET + i * 4 / 4);
Russell Kingf27ecac2005-08-18 21:31:00 +0100441
442 /*
Russell King9395f6e2010-11-11 23:10:30 +0000443 * Set priority on all global interrupts.
Russell Kingf27ecac2005-08-18 21:31:00 +0100444 */
Pawel Molle6afec92010-11-26 13:45:43 +0100445 for (i = 32; i < gic_irqs; i += 4)
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530446 writel_relaxed(0xa0a0a0a0, base + GIC_DIST_PRI + i * 4 / 4);
Russell Kingf27ecac2005-08-18 21:31:00 +0100447
448 /*
Russell King9395f6e2010-11-11 23:10:30 +0000449 * Disable all interrupts. Leave the PPI and SGIs alone
450 * as these enables are banked registers.
Russell Kingf27ecac2005-08-18 21:31:00 +0100451 */
Pawel Molle6afec92010-11-26 13:45:43 +0100452 for (i = 32; i < gic_irqs; i += 32)
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530453 writel_relaxed(0xffffffff, base + GIC_DIST_ENABLE_CLEAR + i * 4 / 32);
Russell Kingf27ecac2005-08-18 21:31:00 +0100454
455 /*
Pawel Molle6afec92010-11-26 13:45:43 +0100456 * Limit number of interrupts registered to the platform maximum
457 */
Russell Kingbef8f9e2010-12-04 16:50:58 +0000458 irq_limit = gic->irq_offset + gic_irqs;
Pawel Molle6afec92010-11-26 13:45:43 +0100459 if (WARN_ON(irq_limit > NR_IRQS))
460 irq_limit = NR_IRQS;
461
462 /*
Russell Kingf27ecac2005-08-18 21:31:00 +0100463 * Setup the Linux IRQ subsystem.
464 */
Trilok Sonieecb28c2011-07-20 16:24:14 +0100465 for (i = 0; i < nrppis; i++) {
466 int ppi = i + ppi_base;
467
468 irq_set_percpu_devid(ppi);
469 irq_set_chip_and_handler(ppi, &gic_chip,
470 handle_percpu_devid_irq);
471 irq_set_chip_data(ppi, gic);
472 set_irq_flags(ppi, IRQF_VALID | IRQF_NOAUTOEN);
473 }
474
475 for (i = irq_start + nrppis; i < irq_limit; i++) {
Will Deacon1a017532011-02-09 12:01:12 +0000476 irq_set_chip_and_handler(i, &gic_chip, handle_fasteoi_irq);
Thomas Gleixner9323f2612011-03-24 13:29:39 +0100477 irq_set_chip_data(i, gic);
Russell Kingf27ecac2005-08-18 21:31:00 +0100478 set_irq_flags(i, IRQF_VALID | IRQF_PROBE);
479 }
480
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700481 gic->max_irq = gic_irqs;
482
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530483 writel_relaxed(1, base + GIC_DIST_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700484 mb();
Russell Kingf27ecac2005-08-18 21:31:00 +0100485}
486
Russell Kingbef8f9e2010-12-04 16:50:58 +0000487static void __cpuinit gic_cpu_init(struct gic_chip_data *gic)
Russell Kingf27ecac2005-08-18 21:31:00 +0100488{
Russell Kingbef8f9e2010-12-04 16:50:58 +0000489 void __iomem *dist_base = gic->dist_base;
490 void __iomem *base = gic->cpu_base;
Russell King9395f6e2010-11-11 23:10:30 +0000491 int i;
492
Russell King9395f6e2010-11-11 23:10:30 +0000493 /*
494 * Deal with the banked PPI and SGI interrupts - disable all
495 * PPI interrupts, ensure all SGI interrupts are enabled.
496 */
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530497 writel_relaxed(0xffff0000, dist_base + GIC_DIST_ENABLE_CLEAR);
498 writel_relaxed(0x0000ffff, dist_base + GIC_DIST_ENABLE_SET);
Russell King9395f6e2010-11-11 23:10:30 +0000499
500 /*
501 * Set priority on PPI and SGI interrupts
502 */
503 for (i = 0; i < 32; i += 4)
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530504 writel_relaxed(0xa0a0a0a0, dist_base + GIC_DIST_PRI + i * 4 / 4);
Russell King9395f6e2010-11-11 23:10:30 +0000505
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530506 writel_relaxed(0xf0, base + GIC_CPU_PRIMASK);
507 writel_relaxed(1, base + GIC_CPU_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700508 mb();
Russell Kingf27ecac2005-08-18 21:31:00 +0100509}
510
Colin Cross692c3e252011-02-10 12:54:10 -0800511#ifdef CONFIG_CPU_PM
512/*
513 * Saves the GIC distributor registers during suspend or idle. Must be called
514 * with interrupts disabled but before powering down the GIC. After calling
515 * this function, no interrupts will be delivered by the GIC, and another
516 * platform-specific wakeup source must be enabled.
517 */
518static void gic_dist_save(unsigned int gic_nr)
519{
520 unsigned int gic_irqs;
521 void __iomem *dist_base;
522 int i;
523
524 if (gic_nr >= MAX_GIC_NR)
525 BUG();
526
527 gic_irqs = gic_data[gic_nr].gic_irqs;
528 dist_base = gic_data[gic_nr].dist_base;
529
530 if (!dist_base)
531 return;
532
533 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 16); i++)
534 gic_data[gic_nr].saved_spi_conf[i] =
535 readl_relaxed(dist_base + GIC_DIST_CONFIG + i * 4);
536
537 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 4); i++)
538 gic_data[gic_nr].saved_spi_target[i] =
539 readl_relaxed(dist_base + GIC_DIST_TARGET + i * 4);
540
541 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 32); i++)
542 gic_data[gic_nr].saved_spi_enable[i] =
543 readl_relaxed(dist_base + GIC_DIST_ENABLE_SET + i * 4);
544}
545
546/*
547 * Restores the GIC distributor registers during resume or when coming out of
548 * idle. Must be called before enabling interrupts. If a level interrupt
549 * that occured while the GIC was suspended is still present, it will be
550 * handled normally, but any edge interrupts that occured will not be seen by
551 * the GIC and need to be handled by the platform-specific wakeup source.
552 */
553static void gic_dist_restore(unsigned int gic_nr)
554{
555 unsigned int gic_irqs;
556 unsigned int i;
557 void __iomem *dist_base;
558
559 if (gic_nr >= MAX_GIC_NR)
560 BUG();
561
562 gic_irqs = gic_data[gic_nr].gic_irqs;
563 dist_base = gic_data[gic_nr].dist_base;
564
565 if (!dist_base)
566 return;
567
568 writel_relaxed(0, dist_base + GIC_DIST_CTRL);
569
570 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 16); i++)
571 writel_relaxed(gic_data[gic_nr].saved_spi_conf[i],
572 dist_base + GIC_DIST_CONFIG + i * 4);
573
574 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 4); i++)
575 writel_relaxed(0xa0a0a0a0,
576 dist_base + GIC_DIST_PRI + i * 4);
577
578 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 4); i++)
579 writel_relaxed(gic_data[gic_nr].saved_spi_target[i],
580 dist_base + GIC_DIST_TARGET + i * 4);
581
582 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 32); i++)
583 writel_relaxed(gic_data[gic_nr].saved_spi_enable[i],
584 dist_base + GIC_DIST_ENABLE_SET + i * 4);
585
586 writel_relaxed(1, dist_base + GIC_DIST_CTRL);
587}
588
589static void gic_cpu_save(unsigned int gic_nr)
590{
591 int i;
592 u32 *ptr;
593 void __iomem *dist_base;
594 void __iomem *cpu_base;
595
596 if (gic_nr >= MAX_GIC_NR)
597 BUG();
598
599 dist_base = gic_data[gic_nr].dist_base;
600 cpu_base = gic_data[gic_nr].cpu_base;
601
602 if (!dist_base || !cpu_base)
603 return;
604
605 ptr = __this_cpu_ptr(gic_data[gic_nr].saved_ppi_enable);
606 for (i = 0; i < DIV_ROUND_UP(32, 32); i++)
607 ptr[i] = readl_relaxed(dist_base + GIC_DIST_ENABLE_SET + i * 4);
608
609 ptr = __this_cpu_ptr(gic_data[gic_nr].saved_ppi_conf);
610 for (i = 0; i < DIV_ROUND_UP(32, 16); i++)
611 ptr[i] = readl_relaxed(dist_base + GIC_DIST_CONFIG + i * 4);
612
613}
614
615static void gic_cpu_restore(unsigned int gic_nr)
616{
617 int i;
618 u32 *ptr;
619 void __iomem *dist_base;
620 void __iomem *cpu_base;
621
622 if (gic_nr >= MAX_GIC_NR)
623 BUG();
624
625 dist_base = gic_data[gic_nr].dist_base;
626 cpu_base = gic_data[gic_nr].cpu_base;
627
628 if (!dist_base || !cpu_base)
629 return;
630
631 ptr = __this_cpu_ptr(gic_data[gic_nr].saved_ppi_enable);
632 for (i = 0; i < DIV_ROUND_UP(32, 32); i++)
633 writel_relaxed(ptr[i], dist_base + GIC_DIST_ENABLE_SET + i * 4);
634
635 ptr = __this_cpu_ptr(gic_data[gic_nr].saved_ppi_conf);
636 for (i = 0; i < DIV_ROUND_UP(32, 16); i++)
637 writel_relaxed(ptr[i], dist_base + GIC_DIST_CONFIG + i * 4);
638
639 for (i = 0; i < DIV_ROUND_UP(32, 4); i++)
640 writel_relaxed(0xa0a0a0a0, dist_base + GIC_DIST_PRI + i * 4);
641
642 writel_relaxed(0xf0, cpu_base + GIC_CPU_PRIMASK);
643 writel_relaxed(1, cpu_base + GIC_CPU_CTRL);
644}
645
646static int gic_notifier(struct notifier_block *self, unsigned long cmd, void *v)
647{
648 int i;
649
650 for (i = 0; i < MAX_GIC_NR; i++) {
651 switch (cmd) {
652 case CPU_PM_ENTER:
653 gic_cpu_save(i);
654 break;
655 case CPU_PM_ENTER_FAILED:
656 case CPU_PM_EXIT:
657 gic_cpu_restore(i);
658 break;
659 case CPU_CLUSTER_PM_ENTER:
660 gic_dist_save(i);
661 break;
662 case CPU_CLUSTER_PM_ENTER_FAILED:
663 case CPU_CLUSTER_PM_EXIT:
664 gic_dist_restore(i);
665 break;
666 }
667 }
668
669 return NOTIFY_OK;
670}
671
672static struct notifier_block gic_notifier_block = {
673 .notifier_call = gic_notifier,
674};
675
676static void __init gic_pm_init(struct gic_chip_data *gic)
677{
678 gic->saved_ppi_enable = __alloc_percpu(DIV_ROUND_UP(32, 32) * 4,
679 sizeof(u32));
680 BUG_ON(!gic->saved_ppi_enable);
681
682 gic->saved_ppi_conf = __alloc_percpu(DIV_ROUND_UP(32, 16) * 4,
683 sizeof(u32));
684 BUG_ON(!gic->saved_ppi_conf);
685
686 cpu_pm_register_notifier(&gic_notifier_block);
687}
688#else
689static void __init gic_pm_init(struct gic_chip_data *gic)
690{
691}
692#endif
693
Russell Kingb580b892010-12-04 15:55:14 +0000694void __init gic_init(unsigned int gic_nr, unsigned int irq_start,
695 void __iomem *dist_base, void __iomem *cpu_base)
696{
Russell Kingbef8f9e2010-12-04 16:50:58 +0000697 struct gic_chip_data *gic;
698
699 BUG_ON(gic_nr >= MAX_GIC_NR);
700
701 gic = &gic_data[gic_nr];
702 gic->dist_base = dist_base;
703 gic->cpu_base = cpu_base;
704 gic->irq_offset = (irq_start - 1) & ~31;
705
Russell Kingff2e27a2010-12-04 16:13:29 +0000706 if (gic_nr == 0)
707 gic_cpu_base_addr = cpu_base;
Russell Kingbef8f9e2010-12-04 16:50:58 +0000708
Colin Cross692c3e252011-02-10 12:54:10 -0800709 gic_chip.flags |= gic_arch_extn.flags;
Russell Kingbef8f9e2010-12-04 16:50:58 +0000710 gic_dist_init(gic, irq_start);
711 gic_cpu_init(gic);
Colin Cross692c3e252011-02-10 12:54:10 -0800712 gic_pm_init(gic);
Russell Kingb580b892010-12-04 15:55:14 +0000713}
714
Russell King38489532010-12-04 16:01:03 +0000715void __cpuinit gic_secondary_init(unsigned int gic_nr)
716{
Russell Kingbef8f9e2010-12-04 16:50:58 +0000717 BUG_ON(gic_nr >= MAX_GIC_NR);
718
719 gic_cpu_init(&gic_data[gic_nr]);
Russell King38489532010-12-04 16:01:03 +0000720}
721
Russell Kingf27ecac2005-08-18 21:31:00 +0100722#ifdef CONFIG_SMP
Russell King82668102009-05-17 16:20:18 +0100723void gic_raise_softirq(const struct cpumask *mask, unsigned int irq)
Russell Kingf27ecac2005-08-18 21:31:00 +0100724{
Will Deacona803a8d2011-08-23 22:20:03 +0100725 int cpu;
726 unsigned long map = 0;
727
728 /* Convert our logical CPU mask into a physical one. */
729 for_each_cpu(cpu, mask)
730 map |= 1 << cpu_logical_map(cpu);
Russell Kingf27ecac2005-08-18 21:31:00 +0100731
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530732 /*
733 * Ensure that stores to Normal memory are visible to the
734 * other CPUs before issuing the IPI.
735 */
736 dsb();
737
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100738 /* this always happens on GIC0 */
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530739 writel_relaxed(map << 16 | irq, gic_data[0].dist_base + GIC_DIST_SOFTINT);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700740 mb();
Russell Kingf27ecac2005-08-18 21:31:00 +0100741}
742#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700743
744/* before calling this function the interrupts should be disabled
745 * and the irq must be disabled at gic to avoid spurious interrupts */
746bool gic_is_spi_pending(unsigned int irq)
747{
748 struct irq_data *d = irq_get_irq_data(irq);
749 struct gic_chip_data *gic_data = &gic_data[0];
750 u32 mask, val;
751
752 WARN_ON(!irqs_disabled());
Thomas Gleixner450ea482009-07-03 08:44:46 -0500753 raw_spin_lock(&irq_controller_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700754 mask = 1 << (gic_irq(d) % 32);
755 val = readl(gic_dist_base(d) +
756 GIC_DIST_ENABLE_SET + (gic_irq(d) / 32) * 4);
757 /* warn if the interrupt is enabled */
758 WARN_ON(val & mask);
759 val = readl(gic_dist_base(d) +
760 GIC_DIST_PENDING_SET + (gic_irq(d) / 32) * 4);
Thomas Gleixner450ea482009-07-03 08:44:46 -0500761 raw_spin_unlock(&irq_controller_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700762 return (bool) (val & mask);
763}
764
765/* before calling this function the interrupts should be disabled
766 * and the irq must be disabled at gic to avoid spurious interrupts */
767void gic_clear_spi_pending(unsigned int irq)
768{
769 struct gic_chip_data *gic_data = &gic_data[0];
770 struct irq_data *d = irq_get_irq_data(irq);
771
772 u32 mask, val;
773 WARN_ON(!irqs_disabled());
Thomas Gleixner450ea482009-07-03 08:44:46 -0500774 raw_spin_lock(&irq_controller_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700775 mask = 1 << (gic_irq(d) % 32);
776 val = readl(gic_dist_base(d) +
777 GIC_DIST_ENABLE_SET + (gic_irq(d) / 32) * 4);
778 /* warn if the interrupt is enabled */
779 WARN_ON(val & mask);
780 writel(mask, gic_dist_base(d) +
781 GIC_DIST_PENDING_CLEAR + (gic_irq(d) / 32) * 4);
Thomas Gleixner450ea482009-07-03 08:44:46 -0500782 raw_spin_unlock(&irq_controller_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700783}