blob: 0455878272ae9711758e438cba221cb4ec602857 [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>
Rob Herring050113e2011-10-21 17:14:27 -050027#include <linux/err.h>
Arnd Bergmann4f874102011-11-01 00:28:37 +010028#include <linux/module.h>
Russell Kingf27ecac2005-08-18 21:31:00 +010029#include <linux/list.h>
30#include <linux/smp.h>
Colin Cross692c3e252011-02-10 12:54:10 -080031#include <linux/cpu_pm.h>
Catalin Marinasdcb86e82005-08-31 21:45:14 +010032#include <linux/cpumask.h>
Russell Kingfced80c2008-09-06 12:10:45 +010033#include <linux/io.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070034#include <linux/syscore_ops.h>
Rob Herring0fc0d942011-09-28 21:27:52 -050035#include <linux/of.h>
36#include <linux/of_address.h>
37#include <linux/of_irq.h>
Rob Herringc383e042011-09-28 21:25:31 -050038#include <linux/irqdomain.h>
Trilok Sonieecb28c2011-07-20 16:24:14 +010039#include <linux/interrupt.h>
40#include <linux/percpu.h>
41#include <linux/slab.h>
Russell Kingf27ecac2005-08-18 21:31:00 +010042
43#include <asm/irq.h>
Marc Zyngier181621e2011-09-06 09:56:17 +010044#include <asm/exception.h>
Russell Kingf27ecac2005-08-18 21:31:00 +010045#include <asm/mach/irq.h>
46#include <asm/hardware/gic.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070047#include <asm/system.h>
Trilok Sonieecb28c2011-07-20 16:24:14 +010048#include <asm/localtimer.h>
Russell Kingf27ecac2005-08-18 21:31:00 +010049
Marc Zyngier680392b2011-11-12 16:09:49 +000050union gic_base {
51 void __iomem *common_base;
52 void __percpu __iomem **percpu_base;
53};
54
55struct gic_chip_data {
56 unsigned int irq_offset;
57 union gic_base dist_base;
58 union gic_base cpu_base;
59 unsigned int max_irq;
60#ifdef CONFIG_PM
61 unsigned int wakeup_irqs[32];
62 unsigned int enabled_irqs[32];
63#endif
64#ifdef CONFIG_CPU_PM
65 u32 saved_spi_enable[DIV_ROUND_UP(1020, 32)];
66 u32 saved_spi_conf[DIV_ROUND_UP(1020, 16)];
67 u32 saved_spi_target[DIV_ROUND_UP(1020, 4)];
68 u32 __percpu *saved_ppi_enable;
69 u32 __percpu *saved_ppi_conf;
70#endif
71#ifdef CONFIG_IRQ_DOMAIN
72 struct irq_domain domain;
73#endif
74 unsigned int gic_irqs;
75#ifdef CONFIG_GIC_NON_BANKED
76 void __iomem *(*get_base)(union gic_base *);
77#endif
78};
79
Thomas Gleixner450ea482009-07-03 08:44:46 -050080static DEFINE_RAW_SPINLOCK(irq_controller_lock);
Russell Kingf27ecac2005-08-18 21:31:00 +010081
Russell Kingff2e27a2010-12-04 16:13:29 +000082/* Address of GIC 0 CPU interface */
Russell Kingbef8f9e2010-12-04 16:50:58 +000083void __iomem *gic_cpu_base_addr __read_mostly;
Russell Kingff2e27a2010-12-04 16:13:29 +000084
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +010085/*
86 * Supported arch specific GIC irq extension.
87 * Default make them NULL.
88 */
89struct irq_chip gic_arch_extn = {
Will Deacon1a017532011-02-09 12:01:12 +000090 .irq_eoi = NULL,
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +010091 .irq_mask = NULL,
92 .irq_unmask = NULL,
93 .irq_retrigger = NULL,
94 .irq_set_type = NULL,
95 .irq_set_wake = NULL,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070096 .irq_disable = NULL,
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +010097};
98
Catalin Marinasb3a1bde2007-02-14 19:14:56 +010099#ifndef MAX_GIC_NR
100#define MAX_GIC_NR 1
101#endif
102
Russell Kingbef8f9e2010-12-04 16:50:58 +0000103static struct gic_chip_data gic_data[MAX_GIC_NR] __read_mostly;
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100104
Marc Zyngier680392b2011-11-12 16:09:49 +0000105#ifdef CONFIG_GIC_NON_BANKED
106static void __iomem *gic_get_percpu_base(union gic_base *base)
107{
108 return *__this_cpu_ptr(base->percpu_base);
109}
110
111static void __iomem *gic_get_common_base(union gic_base *base)
112{
113 return base->common_base;
114}
115
116static inline void __iomem *gic_data_dist_base(struct gic_chip_data *data)
117{
118 return data->get_base(&data->dist_base);
119}
120
121static inline void __iomem *gic_data_cpu_base(struct gic_chip_data *data)
122{
123 return data->get_base(&data->cpu_base);
124}
125
126static inline void gic_set_base_accessor(struct gic_chip_data *data,
127 void __iomem *(*f)(union gic_base *))
128{
129 data->get_base = f;
130}
131#else
132#define gic_data_dist_base(d) ((d)->dist_base.common_base)
133#define gic_data_cpu_base(d) ((d)->cpu_base.common_base)
134#define gic_set_base_accessor(d,f)
135#endif
136
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100137static inline void __iomem *gic_dist_base(struct irq_data *d)
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100138{
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100139 struct gic_chip_data *gic_data = irq_data_get_irq_chip_data(d);
Marc Zyngier680392b2011-11-12 16:09:49 +0000140 return gic_data_dist_base(gic_data);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100141}
142
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100143static inline void __iomem *gic_cpu_base(struct irq_data *d)
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100144{
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100145 struct gic_chip_data *gic_data = irq_data_get_irq_chip_data(d);
Marc Zyngier680392b2011-11-12 16:09:49 +0000146 return gic_data_cpu_base(gic_data);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100147}
148
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100149static inline unsigned int gic_irq(struct irq_data *d)
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100150{
Rob Herringc383e042011-09-28 21:25:31 -0500151 return d->hwirq;
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100152}
153
Russell Kingf27ecac2005-08-18 21:31:00 +0100154/*
155 * Routines to acknowledge, disable and enable interrupts
Russell Kingf27ecac2005-08-18 21:31:00 +0100156 */
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100157static void gic_mask_irq(struct irq_data *d)
Russell Kingf27ecac2005-08-18 21:31:00 +0100158{
Rob Herringc383e042011-09-28 21:25:31 -0500159 u32 mask = 1 << (gic_irq(d) % 32);
Thomas Gleixnerc4bfa282006-07-01 22:32:14 +0100160
Thomas Gleixner450ea482009-07-03 08:44:46 -0500161 raw_spin_lock(&irq_controller_lock);
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530162 writel_relaxed(mask, gic_dist_base(d) + GIC_DIST_ENABLE_CLEAR + (gic_irq(d) / 32) * 4);
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100163 if (gic_arch_extn.irq_mask)
164 gic_arch_extn.irq_mask(d);
Thomas Gleixner450ea482009-07-03 08:44:46 -0500165 raw_spin_unlock(&irq_controller_lock);
Russell Kingf27ecac2005-08-18 21:31:00 +0100166}
167
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100168static void gic_unmask_irq(struct irq_data *d)
Russell Kingf27ecac2005-08-18 21:31:00 +0100169{
Rob Herringc383e042011-09-28 21:25:31 -0500170 u32 mask = 1 << (gic_irq(d) % 32);
Thomas Gleixnerc4bfa282006-07-01 22:32:14 +0100171
Thomas Gleixner450ea482009-07-03 08:44:46 -0500172 raw_spin_lock(&irq_controller_lock);
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100173 if (gic_arch_extn.irq_unmask)
174 gic_arch_extn.irq_unmask(d);
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530175 writel_relaxed(mask, gic_dist_base(d) + GIC_DIST_ENABLE_SET + (gic_irq(d) / 32) * 4);
Thomas Gleixner450ea482009-07-03 08:44:46 -0500176 raw_spin_unlock(&irq_controller_lock);
Russell Kingf27ecac2005-08-18 21:31:00 +0100177}
178
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700179static void gic_disable_irq(struct irq_data *d)
180{
181 if (gic_arch_extn.irq_disable)
182 gic_arch_extn.irq_disable(d);
183}
184
185#ifdef CONFIG_PM
186static int gic_suspend_one(struct gic_chip_data *gic)
187{
188 unsigned int i;
Marc Zyngier680392b2011-11-12 16:09:49 +0000189 void __iomem *base = gic_data_dist_base(gic);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700190
191 for (i = 0; i * 32 < gic->max_irq; i++) {
192 gic->enabled_irqs[i]
193 = readl_relaxed(base + GIC_DIST_ENABLE_SET + i * 4);
194 /* disable all of them */
195 writel_relaxed(0xffffffff, base + GIC_DIST_ENABLE_CLEAR + i * 4);
196 /* enable the wakeup set */
197 writel_relaxed(gic->wakeup_irqs[i],
198 base + GIC_DIST_ENABLE_SET + i * 4);
199 }
200 mb();
201 return 0;
202}
203
204static int gic_suspend(void)
205{
206 int i;
207 for (i = 0; i < MAX_GIC_NR; i++)
208 gic_suspend_one(&gic_data[i]);
209 return 0;
210}
211
212extern int msm_show_resume_irq_mask;
213
214static void gic_show_resume_irq(struct gic_chip_data *gic)
215{
216 unsigned int i;
217 u32 enabled;
218 unsigned long pending[32];
Marc Zyngier680392b2011-11-12 16:09:49 +0000219 void __iomem *base = gic_data_dist_base(gic);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700220
221 if (!msm_show_resume_irq_mask)
222 return;
223
Thomas Gleixner450ea482009-07-03 08:44:46 -0500224 raw_spin_lock(&irq_controller_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700225 for (i = 0; i * 32 < gic->max_irq; i++) {
226 enabled = readl_relaxed(base + GIC_DIST_ENABLE_CLEAR + i * 4);
227 pending[i] = readl_relaxed(base + GIC_DIST_PENDING_SET + i * 4);
228 pending[i] &= enabled;
229 }
Thomas Gleixner450ea482009-07-03 08:44:46 -0500230 raw_spin_unlock(&irq_controller_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700231
232 for (i = find_first_bit(pending, gic->max_irq);
233 i < gic->max_irq;
234 i = find_next_bit(pending, gic->max_irq, i+1)) {
235 pr_warning("%s: %d triggered", __func__,
236 i + gic->irq_offset);
237 }
238}
239
240static void gic_resume_one(struct gic_chip_data *gic)
241{
242 unsigned int i;
Marc Zyngier680392b2011-11-12 16:09:49 +0000243 void __iomem *base = gic_data_dist_base(gic);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700244
245 gic_show_resume_irq(gic);
246 for (i = 0; i * 32 < gic->max_irq; i++) {
247 /* disable all of them */
248 writel_relaxed(0xffffffff, base + GIC_DIST_ENABLE_CLEAR + i * 4);
249 /* enable the enabled set */
250 writel_relaxed(gic->enabled_irqs[i],
251 base + GIC_DIST_ENABLE_SET + i * 4);
252 }
253 mb();
254}
255
256static void gic_resume(void)
257{
258 int i;
259 for (i = 0; i < MAX_GIC_NR; i++)
260 gic_resume_one(&gic_data[i]);
261}
262
263static struct syscore_ops gic_syscore_ops = {
264 .suspend = gic_suspend,
265 .resume = gic_resume,
266};
267
268static int __init gic_init_sys(void)
269{
270 register_syscore_ops(&gic_syscore_ops);
271 return 0;
272}
273arch_initcall(gic_init_sys);
274
275#endif
276
Will Deacon1a017532011-02-09 12:01:12 +0000277static void gic_eoi_irq(struct irq_data *d)
278{
279 if (gic_arch_extn.irq_eoi) {
Thomas Gleixner450ea482009-07-03 08:44:46 -0500280 raw_spin_lock(&irq_controller_lock);
Will Deacon1a017532011-02-09 12:01:12 +0000281 gic_arch_extn.irq_eoi(d);
Thomas Gleixner450ea482009-07-03 08:44:46 -0500282 raw_spin_unlock(&irq_controller_lock);
Will Deacon1a017532011-02-09 12:01:12 +0000283 }
284
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530285 writel_relaxed(gic_irq(d), gic_cpu_base(d) + GIC_CPU_EOI);
Will Deacon1a017532011-02-09 12:01:12 +0000286}
287
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100288static int gic_set_type(struct irq_data *d, unsigned int type)
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100289{
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100290 void __iomem *base = gic_dist_base(d);
291 unsigned int gicirq = gic_irq(d);
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100292 u32 enablemask = 1 << (gicirq % 32);
293 u32 enableoff = (gicirq / 32) * 4;
294 u32 confmask = 0x2 << ((gicirq % 16) * 2);
295 u32 confoff = (gicirq / 16) * 4;
296 bool enabled = false;
297 u32 val;
298
299 /* Interrupt configuration for SGIs can't be changed */
300 if (gicirq < 16)
301 return -EINVAL;
302
303 if (type != IRQ_TYPE_LEVEL_HIGH && type != IRQ_TYPE_EDGE_RISING)
304 return -EINVAL;
305
Thomas Gleixner450ea482009-07-03 08:44:46 -0500306 raw_spin_lock(&irq_controller_lock);
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100307
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100308 if (gic_arch_extn.irq_set_type)
309 gic_arch_extn.irq_set_type(d, type);
310
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530311 val = readl_relaxed(base + GIC_DIST_CONFIG + confoff);
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100312 if (type == IRQ_TYPE_LEVEL_HIGH)
313 val &= ~confmask;
314 else if (type == IRQ_TYPE_EDGE_RISING)
315 val |= confmask;
316
317 /*
318 * As recommended by the spec, disable the interrupt before changing
319 * the configuration
320 */
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530321 if (readl_relaxed(base + GIC_DIST_ENABLE_SET + enableoff) & enablemask) {
322 writel_relaxed(enablemask, base + GIC_DIST_ENABLE_CLEAR + enableoff);
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100323 enabled = true;
324 }
325
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530326 writel_relaxed(val, base + GIC_DIST_CONFIG + confoff);
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100327
328 if (enabled)
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530329 writel_relaxed(enablemask, base + GIC_DIST_ENABLE_SET + enableoff);
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100330
Thomas Gleixner450ea482009-07-03 08:44:46 -0500331 raw_spin_unlock(&irq_controller_lock);
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100332
333 return 0;
334}
335
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100336static int gic_retrigger(struct irq_data *d)
337{
338 if (gic_arch_extn.irq_retrigger)
339 return gic_arch_extn.irq_retrigger(d);
340
Abhijeet Dharmapurikar9d44ea02011-10-30 16:47:19 -0700341 /* the retrigger expects 0 for failure */
342 return 0;
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100343}
344
Catalin Marinasa06f5462005-09-30 16:07:05 +0100345#ifdef CONFIG_SMP
Russell Kingc1917892011-01-23 12:12:01 +0000346static int gic_set_affinity(struct irq_data *d, const struct cpumask *mask_val,
347 bool force)
Russell Kingf27ecac2005-08-18 21:31:00 +0100348{
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100349 void __iomem *reg = gic_dist_base(d) + GIC_DIST_TARGET + (gic_irq(d) & ~3);
Rob Herringc383e042011-09-28 21:25:31 -0500350 unsigned int shift = (gic_irq(d) % 4) * 8;
Russell Kingf3c52e22011-07-21 15:00:57 +0100351 unsigned int cpu = cpumask_any_and(mask_val, cpu_online_mask);
Russell Kingc1917892011-01-23 12:12:01 +0000352 u32 val, mask, bit;
353
Russell Kingf3c52e22011-07-21 15:00:57 +0100354 if (cpu >= 8 || cpu >= nr_cpu_ids)
Russell Kingc1917892011-01-23 12:12:01 +0000355 return -EINVAL;
356
357 mask = 0xff << shift;
Will Deacona803a8d2011-08-23 22:20:03 +0100358 bit = 1 << (cpu_logical_map(cpu) + shift);
Russell Kingf27ecac2005-08-18 21:31:00 +0100359
Thomas Gleixner450ea482009-07-03 08:44:46 -0500360 raw_spin_lock(&irq_controller_lock);
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530361 val = readl_relaxed(reg) & ~mask;
362 writel_relaxed(val | bit, reg);
Thomas Gleixner450ea482009-07-03 08:44:46 -0500363 raw_spin_unlock(&irq_controller_lock);
Yinghai Lud5dedd42009-04-27 17:59:21 -0700364
Russell Kingf3c52e22011-07-21 15:00:57 +0100365 return IRQ_SET_MASK_OK;
Russell Kingf27ecac2005-08-18 21:31:00 +0100366}
Catalin Marinasa06f5462005-09-30 16:07:05 +0100367#endif
Russell Kingf27ecac2005-08-18 21:31:00 +0100368
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100369#ifdef CONFIG_PM
370static int gic_set_wake(struct irq_data *d, unsigned int on)
371{
372 int ret = -ENXIO;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700373 unsigned int reg_offset, bit_offset;
374 unsigned int gicirq = gic_irq(d);
375 struct gic_chip_data *gic_data = irq_data_get_irq_chip_data(d);
376
377 /* per-cpu interrupts cannot be wakeup interrupts */
378 WARN_ON(gicirq < 32);
379
380 reg_offset = gicirq / 32;
381 bit_offset = gicirq % 32;
382
383 if (on)
384 gic_data->wakeup_irqs[reg_offset] |= 1 << bit_offset;
385 else
386 gic_data->wakeup_irqs[reg_offset] &= ~(1 << bit_offset);
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100387
388 if (gic_arch_extn.irq_set_wake)
389 ret = gic_arch_extn.irq_set_wake(d, on);
390
391 return ret;
392}
393
394#else
Rohit Vaswani550aa1a2011-10-06 21:15:37 -0700395static int gic_set_wake(struct irq_data *d, unsigned int on)
396{
397 return 0;
398}
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100399#endif
400
Marc Zyngier181621e2011-09-06 09:56:17 +0100401asmlinkage void __exception_irq_entry gic_handle_irq(struct pt_regs *regs)
402{
403 u32 irqstat, irqnr;
404 struct gic_chip_data *gic = &gic_data[0];
405 void __iomem *cpu_base = gic_data_cpu_base(gic);
406
407 do {
408 irqstat = readl_relaxed(cpu_base + GIC_CPU_INTACK);
409 irqnr = irqstat & ~0x1c00;
410
411 if (likely(irqnr > 15 && irqnr < 1021)) {
412 irqnr = irq_domain_to_irq(&gic->domain, irqnr);
413 handle_IRQ(irqnr, regs);
414 continue;
415 }
416 if (irqnr < 16) {
417 writel_relaxed(irqstat, cpu_base + GIC_CPU_EOI);
418#ifdef CONFIG_SMP
419 handle_IPI(irqnr, regs);
420#endif
421 continue;
422 }
423 break;
424 } while (1);
425}
426
Russell King0f347bb2007-05-17 10:11:34 +0100427static void gic_handle_cascade_irq(unsigned int irq, struct irq_desc *desc)
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100428{
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100429 struct gic_chip_data *chip_data = irq_get_handler_data(irq);
430 struct irq_chip *chip = irq_get_chip(irq);
Russell King0f347bb2007-05-17 10:11:34 +0100431 unsigned int cascade_irq, gic_irq;
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100432 unsigned long status;
433
Will Deacon1a017532011-02-09 12:01:12 +0000434 chained_irq_enter(chip, desc);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100435
Thomas Gleixner450ea482009-07-03 08:44:46 -0500436 raw_spin_lock(&irq_controller_lock);
Marc Zyngier680392b2011-11-12 16:09:49 +0000437 status = readl_relaxed(gic_data_cpu_base(chip_data) + GIC_CPU_INTACK);
Thomas Gleixner450ea482009-07-03 08:44:46 -0500438 raw_spin_unlock(&irq_controller_lock);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100439
Russell King0f347bb2007-05-17 10:11:34 +0100440 gic_irq = (status & 0x3ff);
441 if (gic_irq == 1023)
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100442 goto out;
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100443
Rob Herringc383e042011-09-28 21:25:31 -0500444 cascade_irq = irq_domain_to_irq(&chip_data->domain, gic_irq);
Russell King0f347bb2007-05-17 10:11:34 +0100445 if (unlikely(gic_irq < 32 || gic_irq > 1020 || cascade_irq >= NR_IRQS))
446 do_bad_IRQ(cascade_irq, desc);
447 else
448 generic_handle_irq(cascade_irq);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100449
450 out:
Will Deacon1a017532011-02-09 12:01:12 +0000451 chained_irq_exit(chip, desc);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100452}
453
David Brownell38c677c2006-08-01 22:26:25 +0100454static struct irq_chip gic_chip = {
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100455 .name = "GIC",
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100456 .irq_mask = gic_mask_irq,
457 .irq_unmask = gic_unmask_irq,
Will Deacon1a017532011-02-09 12:01:12 +0000458 .irq_eoi = gic_eoi_irq,
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100459 .irq_set_type = gic_set_type,
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100460 .irq_retrigger = gic_retrigger,
Russell Kingf27ecac2005-08-18 21:31:00 +0100461#ifdef CONFIG_SMP
Russell Kingc1917892011-01-23 12:12:01 +0000462 .irq_set_affinity = gic_set_affinity,
Russell Kingf27ecac2005-08-18 21:31:00 +0100463#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700464 .irq_disable = gic_disable_irq,
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100465 .irq_set_wake = gic_set_wake,
Russell Kingf27ecac2005-08-18 21:31:00 +0100466};
467
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100468void __init gic_cascade_irq(unsigned int gic_nr, unsigned int irq)
469{
470 if (gic_nr >= MAX_GIC_NR)
471 BUG();
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100472 if (irq_set_handler_data(irq, &gic_data[gic_nr]) != 0)
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100473 BUG();
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100474 irq_set_chained_handler(irq, gic_handle_cascade_irq);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100475}
476
Rob Herringc383e042011-09-28 21:25:31 -0500477static void __init gic_dist_init(struct gic_chip_data *gic)
Russell Kingf27ecac2005-08-18 21:31:00 +0100478{
Rob Herringc383e042011-09-28 21:25:31 -0500479 unsigned int i, irq;
Will Deacona803a8d2011-08-23 22:20:03 +0100480 u32 cpumask;
Rob Herringc383e042011-09-28 21:25:31 -0500481 unsigned int gic_irqs = gic->gic_irqs;
482 struct irq_domain *domain = &gic->domain;
Marc Zyngier680392b2011-11-12 16:09:49 +0000483 void __iomem *base = gic_data_dist_base(gic);
Will Deacona803a8d2011-08-23 22:20:03 +0100484 u32 cpu = 0;
Russell Kingf27ecac2005-08-18 21:31:00 +0100485
Will Deacona803a8d2011-08-23 22:20:03 +0100486#ifdef CONFIG_SMP
487 cpu = cpu_logical_map(smp_processor_id());
488#endif
489
490 cpumask = 1 << cpu;
Russell Kingf27ecac2005-08-18 21:31:00 +0100491 cpumask |= cpumask << 8;
492 cpumask |= cpumask << 16;
493
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530494 writel_relaxed(0, base + GIC_DIST_CTRL);
Russell Kingf27ecac2005-08-18 21:31:00 +0100495
496 /*
Russell Kingf27ecac2005-08-18 21:31:00 +0100497 * Set all global interrupts to be level triggered, active low.
498 */
Pawel Molle6afec92010-11-26 13:45:43 +0100499 for (i = 32; i < gic_irqs; i += 16)
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530500 writel_relaxed(0, base + GIC_DIST_CONFIG + i * 4 / 16);
Russell Kingf27ecac2005-08-18 21:31:00 +0100501
502 /*
503 * Set all global interrupts to this CPU only.
504 */
Pawel Molle6afec92010-11-26 13:45:43 +0100505 for (i = 32; i < gic_irqs; i += 4)
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530506 writel_relaxed(cpumask, base + GIC_DIST_TARGET + i * 4 / 4);
Russell Kingf27ecac2005-08-18 21:31:00 +0100507
508 /*
Russell King9395f6e2010-11-11 23:10:30 +0000509 * Set priority on all global interrupts.
Russell Kingf27ecac2005-08-18 21:31:00 +0100510 */
Pawel Molle6afec92010-11-26 13:45:43 +0100511 for (i = 32; i < gic_irqs; i += 4)
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530512 writel_relaxed(0xa0a0a0a0, base + GIC_DIST_PRI + i * 4 / 4);
Russell Kingf27ecac2005-08-18 21:31:00 +0100513
514 /*
Russell King9395f6e2010-11-11 23:10:30 +0000515 * Disable all interrupts. Leave the PPI and SGIs alone
516 * as these enables are banked registers.
Russell Kingf27ecac2005-08-18 21:31:00 +0100517 */
Pawel Molle6afec92010-11-26 13:45:43 +0100518 for (i = 32; i < gic_irqs; i += 32)
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530519 writel_relaxed(0xffffffff, base + GIC_DIST_ENABLE_CLEAR + i * 4 / 32);
Russell Kingf27ecac2005-08-18 21:31:00 +0100520
521 /*
522 * Setup the Linux IRQ subsystem.
523 */
Rob Herringc383e042011-09-28 21:25:31 -0500524 irq_domain_for_each_irq(domain, i, irq) {
525 if (i < 32) {
526 irq_set_percpu_devid(irq);
527 irq_set_chip_and_handler(irq, &gic_chip,
528 handle_percpu_devid_irq);
529 set_irq_flags(irq, IRQF_VALID | IRQF_NOAUTOEN);
530 } else {
531 irq_set_chip_and_handler(irq, &gic_chip,
532 handle_fasteoi_irq);
533 set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
534 }
535 irq_set_chip_data(irq, gic);
Russell Kingf27ecac2005-08-18 21:31:00 +0100536 }
537
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700538 gic->max_irq = gic_irqs;
539
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530540 writel_relaxed(1, base + GIC_DIST_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700541 mb();
Russell Kingf27ecac2005-08-18 21:31:00 +0100542}
543
Russell Kingbef8f9e2010-12-04 16:50:58 +0000544static void __cpuinit gic_cpu_init(struct gic_chip_data *gic)
Russell Kingf27ecac2005-08-18 21:31:00 +0100545{
Marc Zyngier680392b2011-11-12 16:09:49 +0000546 void __iomem *dist_base = gic_data_dist_base(gic);
547 void __iomem *base = gic_data_cpu_base(gic);
Russell King9395f6e2010-11-11 23:10:30 +0000548 int i;
549
Russell King9395f6e2010-11-11 23:10:30 +0000550 /*
551 * Deal with the banked PPI and SGI interrupts - disable all
552 * PPI interrupts, ensure all SGI interrupts are enabled.
553 */
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530554 writel_relaxed(0xffff0000, dist_base + GIC_DIST_ENABLE_CLEAR);
555 writel_relaxed(0x0000ffff, dist_base + GIC_DIST_ENABLE_SET);
Russell King9395f6e2010-11-11 23:10:30 +0000556
557 /*
558 * Set priority on PPI and SGI interrupts
559 */
560 for (i = 0; i < 32; i += 4)
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530561 writel_relaxed(0xa0a0a0a0, dist_base + GIC_DIST_PRI + i * 4 / 4);
Russell King9395f6e2010-11-11 23:10:30 +0000562
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530563 writel_relaxed(0xf0, base + GIC_CPU_PRIMASK);
564 writel_relaxed(1, base + GIC_CPU_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700565 mb();
Russell Kingf27ecac2005-08-18 21:31:00 +0100566}
567
Colin Cross692c3e252011-02-10 12:54:10 -0800568#ifdef CONFIG_CPU_PM
569/*
570 * Saves the GIC distributor registers during suspend or idle. Must be called
571 * with interrupts disabled but before powering down the GIC. After calling
572 * this function, no interrupts will be delivered by the GIC, and another
573 * platform-specific wakeup source must be enabled.
574 */
575static void gic_dist_save(unsigned int gic_nr)
576{
577 unsigned int gic_irqs;
578 void __iomem *dist_base;
579 int i;
580
581 if (gic_nr >= MAX_GIC_NR)
582 BUG();
583
584 gic_irqs = gic_data[gic_nr].gic_irqs;
Marc Zyngier680392b2011-11-12 16:09:49 +0000585 dist_base = gic_data_dist_base(&gic_data[gic_nr]);
Colin Cross692c3e252011-02-10 12:54:10 -0800586
587 if (!dist_base)
588 return;
589
590 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 16); i++)
591 gic_data[gic_nr].saved_spi_conf[i] =
592 readl_relaxed(dist_base + GIC_DIST_CONFIG + i * 4);
593
594 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 4); i++)
595 gic_data[gic_nr].saved_spi_target[i] =
596 readl_relaxed(dist_base + GIC_DIST_TARGET + i * 4);
597
598 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 32); i++)
599 gic_data[gic_nr].saved_spi_enable[i] =
600 readl_relaxed(dist_base + GIC_DIST_ENABLE_SET + i * 4);
601}
602
603/*
604 * Restores the GIC distributor registers during resume or when coming out of
605 * idle. Must be called before enabling interrupts. If a level interrupt
606 * that occured while the GIC was suspended is still present, it will be
607 * handled normally, but any edge interrupts that occured will not be seen by
608 * the GIC and need to be handled by the platform-specific wakeup source.
609 */
610static void gic_dist_restore(unsigned int gic_nr)
611{
612 unsigned int gic_irqs;
613 unsigned int i;
614 void __iomem *dist_base;
615
616 if (gic_nr >= MAX_GIC_NR)
617 BUG();
618
619 gic_irqs = gic_data[gic_nr].gic_irqs;
Marc Zyngier680392b2011-11-12 16:09:49 +0000620 dist_base = gic_data_dist_base(&gic_data[gic_nr]);
Colin Cross692c3e252011-02-10 12:54:10 -0800621
622 if (!dist_base)
623 return;
624
625 writel_relaxed(0, dist_base + GIC_DIST_CTRL);
626
627 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 16); i++)
628 writel_relaxed(gic_data[gic_nr].saved_spi_conf[i],
629 dist_base + GIC_DIST_CONFIG + i * 4);
630
631 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 4); i++)
632 writel_relaxed(0xa0a0a0a0,
633 dist_base + GIC_DIST_PRI + i * 4);
634
635 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 4); i++)
636 writel_relaxed(gic_data[gic_nr].saved_spi_target[i],
637 dist_base + GIC_DIST_TARGET + i * 4);
638
639 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 32); i++)
640 writel_relaxed(gic_data[gic_nr].saved_spi_enable[i],
641 dist_base + GIC_DIST_ENABLE_SET + i * 4);
642
643 writel_relaxed(1, dist_base + GIC_DIST_CTRL);
644}
645
646static void gic_cpu_save(unsigned int gic_nr)
647{
648 int i;
649 u32 *ptr;
650 void __iomem *dist_base;
651 void __iomem *cpu_base;
652
653 if (gic_nr >= MAX_GIC_NR)
654 BUG();
655
Marc Zyngier680392b2011-11-12 16:09:49 +0000656 dist_base = gic_data_dist_base(&gic_data[gic_nr]);
657 cpu_base = gic_data_cpu_base(&gic_data[gic_nr]);
Colin Cross692c3e252011-02-10 12:54:10 -0800658
659 if (!dist_base || !cpu_base)
660 return;
661
662 ptr = __this_cpu_ptr(gic_data[gic_nr].saved_ppi_enable);
663 for (i = 0; i < DIV_ROUND_UP(32, 32); i++)
664 ptr[i] = readl_relaxed(dist_base + GIC_DIST_ENABLE_SET + i * 4);
665
666 ptr = __this_cpu_ptr(gic_data[gic_nr].saved_ppi_conf);
667 for (i = 0; i < DIV_ROUND_UP(32, 16); i++)
668 ptr[i] = readl_relaxed(dist_base + GIC_DIST_CONFIG + i * 4);
669
670}
671
672static void gic_cpu_restore(unsigned int gic_nr)
673{
674 int i;
675 u32 *ptr;
676 void __iomem *dist_base;
677 void __iomem *cpu_base;
678
679 if (gic_nr >= MAX_GIC_NR)
680 BUG();
681
Marc Zyngier680392b2011-11-12 16:09:49 +0000682 dist_base = gic_data_dist_base(&gic_data[gic_nr]);
683 cpu_base = gic_data_cpu_base(&gic_data[gic_nr]);
Colin Cross692c3e252011-02-10 12:54:10 -0800684
685 if (!dist_base || !cpu_base)
686 return;
687
688 ptr = __this_cpu_ptr(gic_data[gic_nr].saved_ppi_enable);
689 for (i = 0; i < DIV_ROUND_UP(32, 32); i++)
690 writel_relaxed(ptr[i], dist_base + GIC_DIST_ENABLE_SET + i * 4);
691
692 ptr = __this_cpu_ptr(gic_data[gic_nr].saved_ppi_conf);
693 for (i = 0; i < DIV_ROUND_UP(32, 16); i++)
694 writel_relaxed(ptr[i], dist_base + GIC_DIST_CONFIG + i * 4);
695
696 for (i = 0; i < DIV_ROUND_UP(32, 4); i++)
697 writel_relaxed(0xa0a0a0a0, dist_base + GIC_DIST_PRI + i * 4);
698
699 writel_relaxed(0xf0, cpu_base + GIC_CPU_PRIMASK);
700 writel_relaxed(1, cpu_base + GIC_CPU_CTRL);
701}
702
703static int gic_notifier(struct notifier_block *self, unsigned long cmd, void *v)
704{
705 int i;
706
707 for (i = 0; i < MAX_GIC_NR; i++) {
Marc Zyngier680392b2011-11-12 16:09:49 +0000708#ifdef CONFIG_GIC_NON_BANKED
709 /* Skip over unused GICs */
710 if (!gic_data[i].get_base)
711 continue;
712#endif
Colin Cross692c3e252011-02-10 12:54:10 -0800713 switch (cmd) {
714 case CPU_PM_ENTER:
715 gic_cpu_save(i);
716 break;
717 case CPU_PM_ENTER_FAILED:
718 case CPU_PM_EXIT:
719 gic_cpu_restore(i);
720 break;
721 case CPU_CLUSTER_PM_ENTER:
722 gic_dist_save(i);
723 break;
724 case CPU_CLUSTER_PM_ENTER_FAILED:
725 case CPU_CLUSTER_PM_EXIT:
726 gic_dist_restore(i);
727 break;
728 }
729 }
730
731 return NOTIFY_OK;
732}
733
734static struct notifier_block gic_notifier_block = {
735 .notifier_call = gic_notifier,
736};
737
738static void __init gic_pm_init(struct gic_chip_data *gic)
739{
740 gic->saved_ppi_enable = __alloc_percpu(DIV_ROUND_UP(32, 32) * 4,
741 sizeof(u32));
742 BUG_ON(!gic->saved_ppi_enable);
743
744 gic->saved_ppi_conf = __alloc_percpu(DIV_ROUND_UP(32, 16) * 4,
745 sizeof(u32));
746 BUG_ON(!gic->saved_ppi_conf);
747
748 cpu_pm_register_notifier(&gic_notifier_block);
749}
750#else
751static void __init gic_pm_init(struct gic_chip_data *gic)
752{
753}
754#endif
755
Rob Herring0fc0d942011-09-28 21:27:52 -0500756#ifdef CONFIG_OF
757static int gic_irq_domain_dt_translate(struct irq_domain *d,
758 struct device_node *controller,
759 const u32 *intspec, unsigned int intsize,
760 unsigned long *out_hwirq, unsigned int *out_type)
761{
762 if (d->of_node != controller)
763 return -EINVAL;
764 if (intsize < 3)
765 return -EINVAL;
766
767 /* Get the interrupt number and add 16 to skip over SGIs */
768 *out_hwirq = intspec[1] + 16;
769
770 /* For SPIs, we need to add 16 more to get the GIC irq ID number */
771 if (!intspec[0])
772 *out_hwirq += 16;
773
774 *out_type = intspec[2] & IRQ_TYPE_SENSE_MASK;
775 return 0;
776}
777#endif
778
Rob Herringc383e042011-09-28 21:25:31 -0500779const struct irq_domain_ops gic_irq_domain_ops = {
Rob Herring0fc0d942011-09-28 21:27:52 -0500780#ifdef CONFIG_OF
781 .dt_translate = gic_irq_domain_dt_translate,
782#endif
Rob Herringc383e042011-09-28 21:25:31 -0500783};
784
Marc Zyngier680392b2011-11-12 16:09:49 +0000785void __init gic_init_bases(unsigned int gic_nr, int irq_start,
786 void __iomem *dist_base, void __iomem *cpu_base,
787 u32 percpu_offset)
Russell Kingb580b892010-12-04 15:55:14 +0000788{
Russell Kingbef8f9e2010-12-04 16:50:58 +0000789 struct gic_chip_data *gic;
Rob Herringc383e042011-09-28 21:25:31 -0500790 struct irq_domain *domain;
Michael Bohan33efecf2012-01-12 15:32:21 -0800791 int gic_irqs, rc;
Russell Kingbef8f9e2010-12-04 16:50:58 +0000792
793 BUG_ON(gic_nr >= MAX_GIC_NR);
794
795 gic = &gic_data[gic_nr];
Rob Herringc383e042011-09-28 21:25:31 -0500796 domain = &gic->domain;
Marc Zyngier680392b2011-11-12 16:09:49 +0000797#ifdef CONFIG_GIC_NON_BANKED
798 if (percpu_offset) { /* Frankein-GIC without banked registers... */
799 unsigned int cpu;
800
801 gic->dist_base.percpu_base = alloc_percpu(void __iomem *);
802 gic->cpu_base.percpu_base = alloc_percpu(void __iomem *);
803 if (WARN_ON(!gic->dist_base.percpu_base ||
Michael Bohan33efecf2012-01-12 15:32:21 -0800804 !gic->cpu_base.percpu_base))
805 goto init_bases_err;
Marc Zyngier680392b2011-11-12 16:09:49 +0000806
807 for_each_possible_cpu(cpu) {
808 unsigned long offset = percpu_offset * cpu_logical_map(cpu);
809 *per_cpu_ptr(gic->dist_base.percpu_base, cpu) = dist_base + offset;
810 *per_cpu_ptr(gic->cpu_base.percpu_base, cpu) = cpu_base + offset;
811 }
812
813 gic_set_base_accessor(gic, gic_get_percpu_base);
814 } else
815#endif
816 { /* Normal, sane GIC... */
817 WARN(percpu_offset,
818 "GIC_NON_BANKED not enabled, ignoring %08x offset!",
819 percpu_offset);
820 gic->dist_base.common_base = dist_base;
821 gic->cpu_base.common_base = cpu_base;
822 gic_set_base_accessor(gic, gic_get_common_base);
823 }
Russell Kingbef8f9e2010-12-04 16:50:58 +0000824
Rob Herringc383e042011-09-28 21:25:31 -0500825 /*
826 * For primary GICs, skip over SGIs.
827 * For secondary GICs, skip over PPIs, too.
828 */
829 if (gic_nr == 0) {
Russell Kingff2e27a2010-12-04 16:13:29 +0000830 gic_cpu_base_addr = cpu_base;
Rob Herringc383e042011-09-28 21:25:31 -0500831 domain->hwirq_base = 16;
Rob Herring050113e2011-10-21 17:14:27 -0500832 if (irq_start > 0)
833 irq_start = (irq_start & ~31) + 16;
Rob Herringc383e042011-09-28 21:25:31 -0500834 } else
835 domain->hwirq_base = 32;
836
837 /*
838 * Find out how many interrupts are supported.
839 * The GIC only supports up to 1020 interrupt sources.
840 */
Marc Zyngier680392b2011-11-12 16:09:49 +0000841 gic_irqs = readl_relaxed(gic_data_dist_base(gic) + GIC_DIST_CTR) & 0x1f;
Rob Herringc383e042011-09-28 21:25:31 -0500842 gic_irqs = (gic_irqs + 1) * 32;
843 if (gic_irqs > 1020)
844 gic_irqs = 1020;
845 gic->gic_irqs = gic_irqs;
846
847 domain->nr_irq = gic_irqs - domain->hwirq_base;
Rob Herring050113e2011-10-21 17:14:27 -0500848 domain->irq_base = irq_alloc_descs(irq_start, 16, domain->nr_irq,
Rob Herringc383e042011-09-28 21:25:31 -0500849 numa_node_id());
Rob Herring050113e2011-10-21 17:14:27 -0500850 if (IS_ERR_VALUE(domain->irq_base)) {
851 WARN(1, "Cannot allocate irq_descs @ IRQ%d, assuming pre-allocated\n",
852 irq_start);
853 domain->irq_base = irq_start;
854 }
Rob Herringc383e042011-09-28 21:25:31 -0500855 domain->priv = gic;
856 domain->ops = &gic_irq_domain_ops;
Michael Bohan33efecf2012-01-12 15:32:21 -0800857 rc = irq_domain_add(domain);
858 if (rc) {
859 WARN(1, "Unable to create irq_domain\n");
860 goto init_bases_err;
861 }
Michael Bohanb8635c32012-01-05 18:32:10 -0800862 irq_domain_register(domain);
Russell Kingbef8f9e2010-12-04 16:50:58 +0000863
Colin Cross692c3e252011-02-10 12:54:10 -0800864 gic_chip.flags |= gic_arch_extn.flags;
Rob Herringc383e042011-09-28 21:25:31 -0500865 gic_dist_init(gic);
Russell Kingbef8f9e2010-12-04 16:50:58 +0000866 gic_cpu_init(gic);
Colin Cross692c3e252011-02-10 12:54:10 -0800867 gic_pm_init(gic);
Michael Bohan33efecf2012-01-12 15:32:21 -0800868
869 return;
870
871init_bases_err:
872 free_percpu(gic->dist_base.percpu_base);
873 free_percpu(gic->cpu_base.percpu_base);
Russell Kingb580b892010-12-04 15:55:14 +0000874}
875
Russell King38489532010-12-04 16:01:03 +0000876void __cpuinit gic_secondary_init(unsigned int gic_nr)
877{
Russell Kingbef8f9e2010-12-04 16:50:58 +0000878 BUG_ON(gic_nr >= MAX_GIC_NR);
879
880 gic_cpu_init(&gic_data[gic_nr]);
Russell King38489532010-12-04 16:01:03 +0000881}
882
Russell Kingf27ecac2005-08-18 21:31:00 +0100883#ifdef CONFIG_SMP
Russell King82668102009-05-17 16:20:18 +0100884void gic_raise_softirq(const struct cpumask *mask, unsigned int irq)
Russell Kingf27ecac2005-08-18 21:31:00 +0100885{
Will Deacona803a8d2011-08-23 22:20:03 +0100886 int cpu;
887 unsigned long map = 0;
888
889 /* Convert our logical CPU mask into a physical one. */
890 for_each_cpu(cpu, mask)
891 map |= 1 << cpu_logical_map(cpu);
Russell Kingf27ecac2005-08-18 21:31:00 +0100892
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530893 /*
894 * Ensure that stores to Normal memory are visible to the
895 * other CPUs before issuing the IPI.
896 */
897 dsb();
898
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100899 /* this always happens on GIC0 */
Marc Zyngier680392b2011-11-12 16:09:49 +0000900 writel_relaxed(map << 16 | irq, gic_data_dist_base(&gic_data[0]) + GIC_DIST_SOFTINT);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700901 mb();
Russell Kingf27ecac2005-08-18 21:31:00 +0100902}
903#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700904
905/* before calling this function the interrupts should be disabled
906 * and the irq must be disabled at gic to avoid spurious interrupts */
907bool gic_is_spi_pending(unsigned int irq)
908{
909 struct irq_data *d = irq_get_irq_data(irq);
910 struct gic_chip_data *gic_data = &gic_data[0];
911 u32 mask, val;
912
913 WARN_ON(!irqs_disabled());
Thomas Gleixner450ea482009-07-03 08:44:46 -0500914 raw_spin_lock(&irq_controller_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700915 mask = 1 << (gic_irq(d) % 32);
916 val = readl(gic_dist_base(d) +
917 GIC_DIST_ENABLE_SET + (gic_irq(d) / 32) * 4);
918 /* warn if the interrupt is enabled */
919 WARN_ON(val & mask);
920 val = readl(gic_dist_base(d) +
921 GIC_DIST_PENDING_SET + (gic_irq(d) / 32) * 4);
Thomas Gleixner450ea482009-07-03 08:44:46 -0500922 raw_spin_unlock(&irq_controller_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700923 return (bool) (val & mask);
924}
925
926/* before calling this function the interrupts should be disabled
927 * and the irq must be disabled at gic to avoid spurious interrupts */
928void gic_clear_spi_pending(unsigned int irq)
929{
930 struct gic_chip_data *gic_data = &gic_data[0];
931 struct irq_data *d = irq_get_irq_data(irq);
932
933 u32 mask, val;
934 WARN_ON(!irqs_disabled());
Thomas Gleixner450ea482009-07-03 08:44:46 -0500935 raw_spin_lock(&irq_controller_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700936 mask = 1 << (gic_irq(d) % 32);
937 val = readl(gic_dist_base(d) +
938 GIC_DIST_ENABLE_SET + (gic_irq(d) / 32) * 4);
939 /* warn if the interrupt is enabled */
940 WARN_ON(val & mask);
941 writel(mask, gic_dist_base(d) +
942 GIC_DIST_PENDING_CLEAR + (gic_irq(d) / 32) * 4);
Thomas Gleixner450ea482009-07-03 08:44:46 -0500943 raw_spin_unlock(&irq_controller_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700944}
Rob Herring0fc0d942011-09-28 21:27:52 -0500945#ifdef CONFIG_OF
946static int gic_cnt __initdata = 0;
947
948int __init gic_of_init(struct device_node *node, struct device_node *parent)
949{
950 void __iomem *cpu_base;
951 void __iomem *dist_base;
Marc Zyngier680392b2011-11-12 16:09:49 +0000952 u32 percpu_offset;
Rob Herring0fc0d942011-09-28 21:27:52 -0500953 int irq;
954 struct irq_domain *domain = &gic_data[gic_cnt].domain;
955
956 if (WARN_ON(!node))
957 return -ENODEV;
958
959 dist_base = of_iomap(node, 0);
960 WARN(!dist_base, "unable to map gic dist registers\n");
961
962 cpu_base = of_iomap(node, 1);
963 WARN(!cpu_base, "unable to map gic cpu registers\n");
964
Marc Zyngier680392b2011-11-12 16:09:49 +0000965 if (of_property_read_u32(node, "cpu-offset", &percpu_offset))
966 percpu_offset = 0;
967
Rob Herring0fc0d942011-09-28 21:27:52 -0500968 domain->of_node = of_node_get(node);
969
Marc Zyngier680392b2011-11-12 16:09:49 +0000970 gic_init_bases(gic_cnt, -1, dist_base, cpu_base, percpu_offset);
Rob Herring0fc0d942011-09-28 21:27:52 -0500971
972 if (parent) {
973 irq = irq_of_parse_and_map(node, 0);
974 gic_cascade_irq(gic_cnt, irq);
975 }
976 gic_cnt++;
977 return 0;
978}
979#endif