blob: 871973a81234fd012fb98d93605bd2491fe47e50 [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 Herringc383e042011-09-28 21:25:31 -050027#include <linux/export.h>
Russell Kingf27ecac2005-08-18 21:31:00 +010028#include <linux/list.h>
29#include <linux/smp.h>
Colin Cross692c3e252011-02-10 12:54:10 -080030#include <linux/cpu_pm.h>
Catalin Marinasdcb86e82005-08-31 21:45:14 +010031#include <linux/cpumask.h>
Russell Kingfced80c2008-09-06 12:10:45 +010032#include <linux/io.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070033#include <linux/syscore_ops.h>
Rob Herring0fc0d942011-09-28 21:27:52 -050034#include <linux/of.h>
35#include <linux/of_address.h>
36#include <linux/of_irq.h>
Rob Herringc383e042011-09-28 21:25:31 -050037#include <linux/irqdomain.h>
Trilok Sonieecb28c2011-07-20 16:24:14 +010038#include <linux/interrupt.h>
39#include <linux/percpu.h>
40#include <linux/slab.h>
Russell Kingf27ecac2005-08-18 21:31:00 +010041
42#include <asm/irq.h>
Russell Kingf27ecac2005-08-18 21:31:00 +010043#include <asm/mach/irq.h>
44#include <asm/hardware/gic.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070045#include <asm/system.h>
Trilok Sonieecb28c2011-07-20 16:24:14 +010046#include <asm/localtimer.h>
Russell Kingf27ecac2005-08-18 21:31:00 +010047
Thomas Gleixner450ea482009-07-03 08:44:46 -050048static DEFINE_RAW_SPINLOCK(irq_controller_lock);
Russell Kingf27ecac2005-08-18 21:31:00 +010049
Russell Kingff2e27a2010-12-04 16:13:29 +000050/* Address of GIC 0 CPU interface */
Russell Kingbef8f9e2010-12-04 16:50:58 +000051void __iomem *gic_cpu_base_addr __read_mostly;
Russell Kingff2e27a2010-12-04 16:13:29 +000052
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +010053/*
54 * Supported arch specific GIC irq extension.
55 * Default make them NULL.
56 */
57struct irq_chip gic_arch_extn = {
Will Deacon1a017532011-02-09 12:01:12 +000058 .irq_eoi = NULL,
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +010059 .irq_mask = NULL,
60 .irq_unmask = NULL,
61 .irq_retrigger = NULL,
62 .irq_set_type = NULL,
63 .irq_set_wake = NULL,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070064 .irq_disable = NULL,
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +010065};
66
Catalin Marinasb3a1bde2007-02-14 19:14:56 +010067#ifndef MAX_GIC_NR
68#define MAX_GIC_NR 1
69#endif
70
Russell Kingbef8f9e2010-12-04 16:50:58 +000071static struct gic_chip_data gic_data[MAX_GIC_NR] __read_mostly;
Catalin Marinasb3a1bde2007-02-14 19:14:56 +010072
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +010073static inline void __iomem *gic_dist_base(struct irq_data *d)
Catalin Marinasb3a1bde2007-02-14 19:14:56 +010074{
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +010075 struct gic_chip_data *gic_data = irq_data_get_irq_chip_data(d);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +010076 return gic_data->dist_base;
77}
78
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +010079static inline void __iomem *gic_cpu_base(struct irq_data *d)
Catalin Marinasb3a1bde2007-02-14 19:14:56 +010080{
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +010081 struct gic_chip_data *gic_data = irq_data_get_irq_chip_data(d);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +010082 return gic_data->cpu_base;
83}
84
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +010085static inline unsigned int gic_irq(struct irq_data *d)
Catalin Marinasb3a1bde2007-02-14 19:14:56 +010086{
Rob Herringc383e042011-09-28 21:25:31 -050087 return d->hwirq;
Catalin Marinasb3a1bde2007-02-14 19:14:56 +010088}
89
Russell Kingf27ecac2005-08-18 21:31:00 +010090/*
91 * Routines to acknowledge, disable and enable interrupts
Russell Kingf27ecac2005-08-18 21:31:00 +010092 */
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +010093static void gic_mask_irq(struct irq_data *d)
Russell Kingf27ecac2005-08-18 21:31:00 +010094{
Rob Herringc383e042011-09-28 21:25:31 -050095 u32 mask = 1 << (gic_irq(d) % 32);
Thomas Gleixnerc4bfa282006-07-01 22:32:14 +010096
Thomas Gleixner450ea482009-07-03 08:44:46 -050097 raw_spin_lock(&irq_controller_lock);
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +053098 writel_relaxed(mask, gic_dist_base(d) + GIC_DIST_ENABLE_CLEAR + (gic_irq(d) / 32) * 4);
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +010099 if (gic_arch_extn.irq_mask)
100 gic_arch_extn.irq_mask(d);
Thomas Gleixner450ea482009-07-03 08:44:46 -0500101 raw_spin_unlock(&irq_controller_lock);
Russell Kingf27ecac2005-08-18 21:31:00 +0100102}
103
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100104static void gic_unmask_irq(struct irq_data *d)
Russell Kingf27ecac2005-08-18 21:31:00 +0100105{
Rob Herringc383e042011-09-28 21:25:31 -0500106 u32 mask = 1 << (gic_irq(d) % 32);
Thomas Gleixnerc4bfa282006-07-01 22:32:14 +0100107
Thomas Gleixner450ea482009-07-03 08:44:46 -0500108 raw_spin_lock(&irq_controller_lock);
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100109 if (gic_arch_extn.irq_unmask)
110 gic_arch_extn.irq_unmask(d);
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530111 writel_relaxed(mask, gic_dist_base(d) + GIC_DIST_ENABLE_SET + (gic_irq(d) / 32) * 4);
Thomas Gleixner450ea482009-07-03 08:44:46 -0500112 raw_spin_unlock(&irq_controller_lock);
Russell Kingf27ecac2005-08-18 21:31:00 +0100113}
114
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700115static void gic_disable_irq(struct irq_data *d)
116{
117 if (gic_arch_extn.irq_disable)
118 gic_arch_extn.irq_disable(d);
119}
120
121#ifdef CONFIG_PM
122static int gic_suspend_one(struct gic_chip_data *gic)
123{
124 unsigned int i;
125 void __iomem *base = gic->dist_base;
126
127 for (i = 0; i * 32 < gic->max_irq; i++) {
128 gic->enabled_irqs[i]
129 = readl_relaxed(base + GIC_DIST_ENABLE_SET + i * 4);
130 /* disable all of them */
131 writel_relaxed(0xffffffff, base + GIC_DIST_ENABLE_CLEAR + i * 4);
132 /* enable the wakeup set */
133 writel_relaxed(gic->wakeup_irqs[i],
134 base + GIC_DIST_ENABLE_SET + i * 4);
135 }
136 mb();
137 return 0;
138}
139
140static int gic_suspend(void)
141{
142 int i;
143 for (i = 0; i < MAX_GIC_NR; i++)
144 gic_suspend_one(&gic_data[i]);
145 return 0;
146}
147
148extern int msm_show_resume_irq_mask;
149
150static void gic_show_resume_irq(struct gic_chip_data *gic)
151{
152 unsigned int i;
153 u32 enabled;
154 unsigned long pending[32];
155 void __iomem *base = gic->dist_base;
156
157 if (!msm_show_resume_irq_mask)
158 return;
159
Thomas Gleixner450ea482009-07-03 08:44:46 -0500160 raw_spin_lock(&irq_controller_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700161 for (i = 0; i * 32 < gic->max_irq; i++) {
162 enabled = readl_relaxed(base + GIC_DIST_ENABLE_CLEAR + i * 4);
163 pending[i] = readl_relaxed(base + GIC_DIST_PENDING_SET + i * 4);
164 pending[i] &= enabled;
165 }
Thomas Gleixner450ea482009-07-03 08:44:46 -0500166 raw_spin_unlock(&irq_controller_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700167
168 for (i = find_first_bit(pending, gic->max_irq);
169 i < gic->max_irq;
170 i = find_next_bit(pending, gic->max_irq, i+1)) {
171 pr_warning("%s: %d triggered", __func__,
172 i + gic->irq_offset);
173 }
174}
175
176static void gic_resume_one(struct gic_chip_data *gic)
177{
178 unsigned int i;
179 void __iomem *base = gic->dist_base;
180
181 gic_show_resume_irq(gic);
182 for (i = 0; i * 32 < gic->max_irq; i++) {
183 /* disable all of them */
184 writel_relaxed(0xffffffff, base + GIC_DIST_ENABLE_CLEAR + i * 4);
185 /* enable the enabled set */
186 writel_relaxed(gic->enabled_irqs[i],
187 base + GIC_DIST_ENABLE_SET + i * 4);
188 }
189 mb();
190}
191
192static void gic_resume(void)
193{
194 int i;
195 for (i = 0; i < MAX_GIC_NR; i++)
196 gic_resume_one(&gic_data[i]);
197}
198
199static struct syscore_ops gic_syscore_ops = {
200 .suspend = gic_suspend,
201 .resume = gic_resume,
202};
203
204static int __init gic_init_sys(void)
205{
206 register_syscore_ops(&gic_syscore_ops);
207 return 0;
208}
209arch_initcall(gic_init_sys);
210
211#endif
212
Will Deacon1a017532011-02-09 12:01:12 +0000213static void gic_eoi_irq(struct irq_data *d)
214{
215 if (gic_arch_extn.irq_eoi) {
Thomas Gleixner450ea482009-07-03 08:44:46 -0500216 raw_spin_lock(&irq_controller_lock);
Will Deacon1a017532011-02-09 12:01:12 +0000217 gic_arch_extn.irq_eoi(d);
Thomas Gleixner450ea482009-07-03 08:44:46 -0500218 raw_spin_unlock(&irq_controller_lock);
Will Deacon1a017532011-02-09 12:01:12 +0000219 }
220
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530221 writel_relaxed(gic_irq(d), gic_cpu_base(d) + GIC_CPU_EOI);
Will Deacon1a017532011-02-09 12:01:12 +0000222}
223
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100224static int gic_set_type(struct irq_data *d, unsigned int type)
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100225{
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100226 void __iomem *base = gic_dist_base(d);
227 unsigned int gicirq = gic_irq(d);
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100228 u32 enablemask = 1 << (gicirq % 32);
229 u32 enableoff = (gicirq / 32) * 4;
230 u32 confmask = 0x2 << ((gicirq % 16) * 2);
231 u32 confoff = (gicirq / 16) * 4;
232 bool enabled = false;
233 u32 val;
234
235 /* Interrupt configuration for SGIs can't be changed */
236 if (gicirq < 16)
237 return -EINVAL;
238
239 if (type != IRQ_TYPE_LEVEL_HIGH && type != IRQ_TYPE_EDGE_RISING)
240 return -EINVAL;
241
Thomas Gleixner450ea482009-07-03 08:44:46 -0500242 raw_spin_lock(&irq_controller_lock);
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100243
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100244 if (gic_arch_extn.irq_set_type)
245 gic_arch_extn.irq_set_type(d, type);
246
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530247 val = readl_relaxed(base + GIC_DIST_CONFIG + confoff);
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100248 if (type == IRQ_TYPE_LEVEL_HIGH)
249 val &= ~confmask;
250 else if (type == IRQ_TYPE_EDGE_RISING)
251 val |= confmask;
252
253 /*
254 * As recommended by the spec, disable the interrupt before changing
255 * the configuration
256 */
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530257 if (readl_relaxed(base + GIC_DIST_ENABLE_SET + enableoff) & enablemask) {
258 writel_relaxed(enablemask, base + GIC_DIST_ENABLE_CLEAR + enableoff);
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100259 enabled = true;
260 }
261
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530262 writel_relaxed(val, base + GIC_DIST_CONFIG + confoff);
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100263
264 if (enabled)
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530265 writel_relaxed(enablemask, base + GIC_DIST_ENABLE_SET + enableoff);
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100266
Thomas Gleixner450ea482009-07-03 08:44:46 -0500267 raw_spin_unlock(&irq_controller_lock);
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100268
269 return 0;
270}
271
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100272static int gic_retrigger(struct irq_data *d)
273{
274 if (gic_arch_extn.irq_retrigger)
275 return gic_arch_extn.irq_retrigger(d);
276
Abhijeet Dharmapurikar9d44ea02011-10-30 16:47:19 -0700277 /* the retrigger expects 0 for failure */
278 return 0;
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100279}
280
Catalin Marinasa06f5462005-09-30 16:07:05 +0100281#ifdef CONFIG_SMP
Russell Kingc1917892011-01-23 12:12:01 +0000282static int gic_set_affinity(struct irq_data *d, const struct cpumask *mask_val,
283 bool force)
Russell Kingf27ecac2005-08-18 21:31:00 +0100284{
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100285 void __iomem *reg = gic_dist_base(d) + GIC_DIST_TARGET + (gic_irq(d) & ~3);
Rob Herringc383e042011-09-28 21:25:31 -0500286 unsigned int shift = (gic_irq(d) % 4) * 8;
Russell Kingf3c52e22011-07-21 15:00:57 +0100287 unsigned int cpu = cpumask_any_and(mask_val, cpu_online_mask);
Russell Kingc1917892011-01-23 12:12:01 +0000288 u32 val, mask, bit;
289
Russell Kingf3c52e22011-07-21 15:00:57 +0100290 if (cpu >= 8 || cpu >= nr_cpu_ids)
Russell Kingc1917892011-01-23 12:12:01 +0000291 return -EINVAL;
292
293 mask = 0xff << shift;
Will Deacona803a8d2011-08-23 22:20:03 +0100294 bit = 1 << (cpu_logical_map(cpu) + shift);
Russell Kingf27ecac2005-08-18 21:31:00 +0100295
Thomas Gleixner450ea482009-07-03 08:44:46 -0500296 raw_spin_lock(&irq_controller_lock);
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530297 val = readl_relaxed(reg) & ~mask;
298 writel_relaxed(val | bit, reg);
Thomas Gleixner450ea482009-07-03 08:44:46 -0500299 raw_spin_unlock(&irq_controller_lock);
Yinghai Lud5dedd42009-04-27 17:59:21 -0700300
Russell Kingf3c52e22011-07-21 15:00:57 +0100301 return IRQ_SET_MASK_OK;
Russell Kingf27ecac2005-08-18 21:31:00 +0100302}
Catalin Marinasa06f5462005-09-30 16:07:05 +0100303#endif
Russell Kingf27ecac2005-08-18 21:31:00 +0100304
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100305#ifdef CONFIG_PM
306static int gic_set_wake(struct irq_data *d, unsigned int on)
307{
308 int ret = -ENXIO;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700309 unsigned int reg_offset, bit_offset;
310 unsigned int gicirq = gic_irq(d);
311 struct gic_chip_data *gic_data = irq_data_get_irq_chip_data(d);
312
313 /* per-cpu interrupts cannot be wakeup interrupts */
314 WARN_ON(gicirq < 32);
315
316 reg_offset = gicirq / 32;
317 bit_offset = gicirq % 32;
318
319 if (on)
320 gic_data->wakeup_irqs[reg_offset] |= 1 << bit_offset;
321 else
322 gic_data->wakeup_irqs[reg_offset] &= ~(1 << bit_offset);
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100323
324 if (gic_arch_extn.irq_set_wake)
325 ret = gic_arch_extn.irq_set_wake(d, on);
326
327 return ret;
328}
329
330#else
Rohit Vaswani550aa1a2011-10-06 21:15:37 -0700331static int gic_set_wake(struct irq_data *d, unsigned int on)
332{
333 return 0;
334}
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100335#endif
336
Russell King0f347bb2007-05-17 10:11:34 +0100337static void gic_handle_cascade_irq(unsigned int irq, struct irq_desc *desc)
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100338{
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100339 struct gic_chip_data *chip_data = irq_get_handler_data(irq);
340 struct irq_chip *chip = irq_get_chip(irq);
Russell King0f347bb2007-05-17 10:11:34 +0100341 unsigned int cascade_irq, gic_irq;
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100342 unsigned long status;
343
Will Deacon1a017532011-02-09 12:01:12 +0000344 chained_irq_enter(chip, desc);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100345
Thomas Gleixner450ea482009-07-03 08:44:46 -0500346 raw_spin_lock(&irq_controller_lock);
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530347 status = readl_relaxed(chip_data->cpu_base + GIC_CPU_INTACK);
Thomas Gleixner450ea482009-07-03 08:44:46 -0500348 raw_spin_unlock(&irq_controller_lock);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100349
Russell King0f347bb2007-05-17 10:11:34 +0100350 gic_irq = (status & 0x3ff);
351 if (gic_irq == 1023)
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100352 goto out;
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100353
Rob Herringc383e042011-09-28 21:25:31 -0500354 cascade_irq = irq_domain_to_irq(&chip_data->domain, gic_irq);
Russell King0f347bb2007-05-17 10:11:34 +0100355 if (unlikely(gic_irq < 32 || gic_irq > 1020 || cascade_irq >= NR_IRQS))
356 do_bad_IRQ(cascade_irq, desc);
357 else
358 generic_handle_irq(cascade_irq);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100359
360 out:
Will Deacon1a017532011-02-09 12:01:12 +0000361 chained_irq_exit(chip, desc);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100362}
363
David Brownell38c677c2006-08-01 22:26:25 +0100364static struct irq_chip gic_chip = {
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100365 .name = "GIC",
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100366 .irq_mask = gic_mask_irq,
367 .irq_unmask = gic_unmask_irq,
Will Deacon1a017532011-02-09 12:01:12 +0000368 .irq_eoi = gic_eoi_irq,
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100369 .irq_set_type = gic_set_type,
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100370 .irq_retrigger = gic_retrigger,
Russell Kingf27ecac2005-08-18 21:31:00 +0100371#ifdef CONFIG_SMP
Russell Kingc1917892011-01-23 12:12:01 +0000372 .irq_set_affinity = gic_set_affinity,
Russell Kingf27ecac2005-08-18 21:31:00 +0100373#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700374 .irq_disable = gic_disable_irq,
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100375 .irq_set_wake = gic_set_wake,
Russell Kingf27ecac2005-08-18 21:31:00 +0100376};
377
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100378void __init gic_cascade_irq(unsigned int gic_nr, unsigned int irq)
379{
380 if (gic_nr >= MAX_GIC_NR)
381 BUG();
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100382 if (irq_set_handler_data(irq, &gic_data[gic_nr]) != 0)
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100383 BUG();
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100384 irq_set_chained_handler(irq, gic_handle_cascade_irq);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100385}
386
Rob Herringc383e042011-09-28 21:25:31 -0500387static void __init gic_dist_init(struct gic_chip_data *gic)
Russell Kingf27ecac2005-08-18 21:31:00 +0100388{
Rob Herringc383e042011-09-28 21:25:31 -0500389 unsigned int i, irq;
Will Deacona803a8d2011-08-23 22:20:03 +0100390 u32 cpumask;
Rob Herringc383e042011-09-28 21:25:31 -0500391 unsigned int gic_irqs = gic->gic_irqs;
392 struct irq_domain *domain = &gic->domain;
Russell Kingbef8f9e2010-12-04 16:50:58 +0000393 void __iomem *base = gic->dist_base;
Will Deacona803a8d2011-08-23 22:20:03 +0100394 u32 cpu = 0;
Russell Kingf27ecac2005-08-18 21:31:00 +0100395
Will Deacona803a8d2011-08-23 22:20:03 +0100396#ifdef CONFIG_SMP
397 cpu = cpu_logical_map(smp_processor_id());
398#endif
399
400 cpumask = 1 << cpu;
Russell Kingf27ecac2005-08-18 21:31:00 +0100401 cpumask |= cpumask << 8;
402 cpumask |= cpumask << 16;
403
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530404 writel_relaxed(0, base + GIC_DIST_CTRL);
Russell Kingf27ecac2005-08-18 21:31:00 +0100405
406 /*
Russell Kingf27ecac2005-08-18 21:31:00 +0100407 * Set all global interrupts to be level triggered, active low.
408 */
Pawel Molle6afec92010-11-26 13:45:43 +0100409 for (i = 32; i < gic_irqs; i += 16)
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530410 writel_relaxed(0, base + GIC_DIST_CONFIG + i * 4 / 16);
Russell Kingf27ecac2005-08-18 21:31:00 +0100411
412 /*
413 * Set all global interrupts to this CPU only.
414 */
Pawel Molle6afec92010-11-26 13:45:43 +0100415 for (i = 32; i < gic_irqs; i += 4)
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530416 writel_relaxed(cpumask, base + GIC_DIST_TARGET + i * 4 / 4);
Russell Kingf27ecac2005-08-18 21:31:00 +0100417
418 /*
Russell King9395f6e2010-11-11 23:10:30 +0000419 * Set priority on all global interrupts.
Russell Kingf27ecac2005-08-18 21:31:00 +0100420 */
Pawel Molle6afec92010-11-26 13:45:43 +0100421 for (i = 32; i < gic_irqs; i += 4)
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530422 writel_relaxed(0xa0a0a0a0, base + GIC_DIST_PRI + i * 4 / 4);
Russell Kingf27ecac2005-08-18 21:31:00 +0100423
424 /*
Russell King9395f6e2010-11-11 23:10:30 +0000425 * Disable all interrupts. Leave the PPI and SGIs alone
426 * as these enables are banked registers.
Russell Kingf27ecac2005-08-18 21:31:00 +0100427 */
Pawel Molle6afec92010-11-26 13:45:43 +0100428 for (i = 32; i < gic_irqs; i += 32)
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530429 writel_relaxed(0xffffffff, base + GIC_DIST_ENABLE_CLEAR + i * 4 / 32);
Russell Kingf27ecac2005-08-18 21:31:00 +0100430
431 /*
432 * Setup the Linux IRQ subsystem.
433 */
Rob Herringc383e042011-09-28 21:25:31 -0500434 irq_domain_for_each_irq(domain, i, irq) {
435 if (i < 32) {
436 irq_set_percpu_devid(irq);
437 irq_set_chip_and_handler(irq, &gic_chip,
438 handle_percpu_devid_irq);
439 set_irq_flags(irq, IRQF_VALID | IRQF_NOAUTOEN);
440 } else {
441 irq_set_chip_and_handler(irq, &gic_chip,
442 handle_fasteoi_irq);
443 set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
444 }
445 irq_set_chip_data(irq, gic);
Russell Kingf27ecac2005-08-18 21:31:00 +0100446 }
447
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700448 gic->max_irq = gic_irqs;
449
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530450 writel_relaxed(1, base + GIC_DIST_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700451 mb();
Russell Kingf27ecac2005-08-18 21:31:00 +0100452}
453
Russell Kingbef8f9e2010-12-04 16:50:58 +0000454static void __cpuinit gic_cpu_init(struct gic_chip_data *gic)
Russell Kingf27ecac2005-08-18 21:31:00 +0100455{
Russell Kingbef8f9e2010-12-04 16:50:58 +0000456 void __iomem *dist_base = gic->dist_base;
457 void __iomem *base = gic->cpu_base;
Russell King9395f6e2010-11-11 23:10:30 +0000458 int i;
459
Russell King9395f6e2010-11-11 23:10:30 +0000460 /*
461 * Deal with the banked PPI and SGI interrupts - disable all
462 * PPI interrupts, ensure all SGI interrupts are enabled.
463 */
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530464 writel_relaxed(0xffff0000, dist_base + GIC_DIST_ENABLE_CLEAR);
465 writel_relaxed(0x0000ffff, dist_base + GIC_DIST_ENABLE_SET);
Russell King9395f6e2010-11-11 23:10:30 +0000466
467 /*
468 * Set priority on PPI and SGI interrupts
469 */
470 for (i = 0; i < 32; i += 4)
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530471 writel_relaxed(0xa0a0a0a0, dist_base + GIC_DIST_PRI + i * 4 / 4);
Russell King9395f6e2010-11-11 23:10:30 +0000472
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530473 writel_relaxed(0xf0, base + GIC_CPU_PRIMASK);
474 writel_relaxed(1, base + GIC_CPU_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700475 mb();
Russell Kingf27ecac2005-08-18 21:31:00 +0100476}
477
Colin Cross692c3e252011-02-10 12:54:10 -0800478#ifdef CONFIG_CPU_PM
479/*
480 * Saves the GIC distributor registers during suspend or idle. Must be called
481 * with interrupts disabled but before powering down the GIC. After calling
482 * this function, no interrupts will be delivered by the GIC, and another
483 * platform-specific wakeup source must be enabled.
484 */
485static void gic_dist_save(unsigned int gic_nr)
486{
487 unsigned int gic_irqs;
488 void __iomem *dist_base;
489 int i;
490
491 if (gic_nr >= MAX_GIC_NR)
492 BUG();
493
494 gic_irqs = gic_data[gic_nr].gic_irqs;
495 dist_base = gic_data[gic_nr].dist_base;
496
497 if (!dist_base)
498 return;
499
500 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 16); i++)
501 gic_data[gic_nr].saved_spi_conf[i] =
502 readl_relaxed(dist_base + GIC_DIST_CONFIG + i * 4);
503
504 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 4); i++)
505 gic_data[gic_nr].saved_spi_target[i] =
506 readl_relaxed(dist_base + GIC_DIST_TARGET + i * 4);
507
508 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 32); i++)
509 gic_data[gic_nr].saved_spi_enable[i] =
510 readl_relaxed(dist_base + GIC_DIST_ENABLE_SET + i * 4);
511}
512
513/*
514 * Restores the GIC distributor registers during resume or when coming out of
515 * idle. Must be called before enabling interrupts. If a level interrupt
516 * that occured while the GIC was suspended is still present, it will be
517 * handled normally, but any edge interrupts that occured will not be seen by
518 * the GIC and need to be handled by the platform-specific wakeup source.
519 */
520static void gic_dist_restore(unsigned int gic_nr)
521{
522 unsigned int gic_irqs;
523 unsigned int i;
524 void __iomem *dist_base;
525
526 if (gic_nr >= MAX_GIC_NR)
527 BUG();
528
529 gic_irqs = gic_data[gic_nr].gic_irqs;
530 dist_base = gic_data[gic_nr].dist_base;
531
532 if (!dist_base)
533 return;
534
535 writel_relaxed(0, dist_base + GIC_DIST_CTRL);
536
537 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 16); i++)
538 writel_relaxed(gic_data[gic_nr].saved_spi_conf[i],
539 dist_base + GIC_DIST_CONFIG + i * 4);
540
541 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 4); i++)
542 writel_relaxed(0xa0a0a0a0,
543 dist_base + GIC_DIST_PRI + i * 4);
544
545 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 4); i++)
546 writel_relaxed(gic_data[gic_nr].saved_spi_target[i],
547 dist_base + GIC_DIST_TARGET + i * 4);
548
549 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 32); i++)
550 writel_relaxed(gic_data[gic_nr].saved_spi_enable[i],
551 dist_base + GIC_DIST_ENABLE_SET + i * 4);
552
553 writel_relaxed(1, dist_base + GIC_DIST_CTRL);
554}
555
556static void gic_cpu_save(unsigned int gic_nr)
557{
558 int i;
559 u32 *ptr;
560 void __iomem *dist_base;
561 void __iomem *cpu_base;
562
563 if (gic_nr >= MAX_GIC_NR)
564 BUG();
565
566 dist_base = gic_data[gic_nr].dist_base;
567 cpu_base = gic_data[gic_nr].cpu_base;
568
569 if (!dist_base || !cpu_base)
570 return;
571
572 ptr = __this_cpu_ptr(gic_data[gic_nr].saved_ppi_enable);
573 for (i = 0; i < DIV_ROUND_UP(32, 32); i++)
574 ptr[i] = readl_relaxed(dist_base + GIC_DIST_ENABLE_SET + i * 4);
575
576 ptr = __this_cpu_ptr(gic_data[gic_nr].saved_ppi_conf);
577 for (i = 0; i < DIV_ROUND_UP(32, 16); i++)
578 ptr[i] = readl_relaxed(dist_base + GIC_DIST_CONFIG + i * 4);
579
580}
581
582static void gic_cpu_restore(unsigned int gic_nr)
583{
584 int i;
585 u32 *ptr;
586 void __iomem *dist_base;
587 void __iomem *cpu_base;
588
589 if (gic_nr >= MAX_GIC_NR)
590 BUG();
591
592 dist_base = gic_data[gic_nr].dist_base;
593 cpu_base = gic_data[gic_nr].cpu_base;
594
595 if (!dist_base || !cpu_base)
596 return;
597
598 ptr = __this_cpu_ptr(gic_data[gic_nr].saved_ppi_enable);
599 for (i = 0; i < DIV_ROUND_UP(32, 32); i++)
600 writel_relaxed(ptr[i], dist_base + GIC_DIST_ENABLE_SET + i * 4);
601
602 ptr = __this_cpu_ptr(gic_data[gic_nr].saved_ppi_conf);
603 for (i = 0; i < DIV_ROUND_UP(32, 16); i++)
604 writel_relaxed(ptr[i], dist_base + GIC_DIST_CONFIG + i * 4);
605
606 for (i = 0; i < DIV_ROUND_UP(32, 4); i++)
607 writel_relaxed(0xa0a0a0a0, dist_base + GIC_DIST_PRI + i * 4);
608
609 writel_relaxed(0xf0, cpu_base + GIC_CPU_PRIMASK);
610 writel_relaxed(1, cpu_base + GIC_CPU_CTRL);
611}
612
613static int gic_notifier(struct notifier_block *self, unsigned long cmd, void *v)
614{
615 int i;
616
617 for (i = 0; i < MAX_GIC_NR; i++) {
618 switch (cmd) {
619 case CPU_PM_ENTER:
620 gic_cpu_save(i);
621 break;
622 case CPU_PM_ENTER_FAILED:
623 case CPU_PM_EXIT:
624 gic_cpu_restore(i);
625 break;
626 case CPU_CLUSTER_PM_ENTER:
627 gic_dist_save(i);
628 break;
629 case CPU_CLUSTER_PM_ENTER_FAILED:
630 case CPU_CLUSTER_PM_EXIT:
631 gic_dist_restore(i);
632 break;
633 }
634 }
635
636 return NOTIFY_OK;
637}
638
639static struct notifier_block gic_notifier_block = {
640 .notifier_call = gic_notifier,
641};
642
643static void __init gic_pm_init(struct gic_chip_data *gic)
644{
645 gic->saved_ppi_enable = __alloc_percpu(DIV_ROUND_UP(32, 32) * 4,
646 sizeof(u32));
647 BUG_ON(!gic->saved_ppi_enable);
648
649 gic->saved_ppi_conf = __alloc_percpu(DIV_ROUND_UP(32, 16) * 4,
650 sizeof(u32));
651 BUG_ON(!gic->saved_ppi_conf);
652
653 cpu_pm_register_notifier(&gic_notifier_block);
654}
655#else
656static void __init gic_pm_init(struct gic_chip_data *gic)
657{
658}
659#endif
660
Rob Herring0fc0d942011-09-28 21:27:52 -0500661#ifdef CONFIG_OF
662static int gic_irq_domain_dt_translate(struct irq_domain *d,
663 struct device_node *controller,
664 const u32 *intspec, unsigned int intsize,
665 unsigned long *out_hwirq, unsigned int *out_type)
666{
667 if (d->of_node != controller)
668 return -EINVAL;
669 if (intsize < 3)
670 return -EINVAL;
671
672 /* Get the interrupt number and add 16 to skip over SGIs */
673 *out_hwirq = intspec[1] + 16;
674
675 /* For SPIs, we need to add 16 more to get the GIC irq ID number */
676 if (!intspec[0])
677 *out_hwirq += 16;
678
679 *out_type = intspec[2] & IRQ_TYPE_SENSE_MASK;
680 return 0;
681}
682#endif
683
Rob Herringc383e042011-09-28 21:25:31 -0500684const struct irq_domain_ops gic_irq_domain_ops = {
Rob Herring0fc0d942011-09-28 21:27:52 -0500685#ifdef CONFIG_OF
686 .dt_translate = gic_irq_domain_dt_translate,
687#endif
Rob Herringc383e042011-09-28 21:25:31 -0500688};
689
Russell Kingb580b892010-12-04 15:55:14 +0000690void __init gic_init(unsigned int gic_nr, unsigned int irq_start,
691 void __iomem *dist_base, void __iomem *cpu_base)
692{
Russell Kingbef8f9e2010-12-04 16:50:58 +0000693 struct gic_chip_data *gic;
Rob Herringc383e042011-09-28 21:25:31 -0500694 struct irq_domain *domain;
695 int gic_irqs;
Russell Kingbef8f9e2010-12-04 16:50:58 +0000696
697 BUG_ON(gic_nr >= MAX_GIC_NR);
698
699 gic = &gic_data[gic_nr];
Rob Herringc383e042011-09-28 21:25:31 -0500700 domain = &gic->domain;
Russell Kingbef8f9e2010-12-04 16:50:58 +0000701 gic->dist_base = dist_base;
702 gic->cpu_base = cpu_base;
Russell Kingbef8f9e2010-12-04 16:50:58 +0000703
Rob Herringc383e042011-09-28 21:25:31 -0500704 /*
705 * For primary GICs, skip over SGIs.
706 * For secondary GICs, skip over PPIs, too.
707 */
708 if (gic_nr == 0) {
Russell Kingff2e27a2010-12-04 16:13:29 +0000709 gic_cpu_base_addr = cpu_base;
Rob Herringc383e042011-09-28 21:25:31 -0500710 domain->hwirq_base = 16;
711 irq_start = (irq_start & ~31) + 16;
712 } else
713 domain->hwirq_base = 32;
714
715 /*
716 * Find out how many interrupts are supported.
717 * The GIC only supports up to 1020 interrupt sources.
718 */
719 gic_irqs = readl_relaxed(dist_base + GIC_DIST_CTR) & 0x1f;
720 gic_irqs = (gic_irqs + 1) * 32;
721 if (gic_irqs > 1020)
722 gic_irqs = 1020;
723 gic->gic_irqs = gic_irqs;
724
725 domain->nr_irq = gic_irqs - domain->hwirq_base;
726 domain->irq_base = irq_alloc_descs(-1, irq_start, domain->nr_irq,
727 numa_node_id());
728 domain->priv = gic;
729 domain->ops = &gic_irq_domain_ops;
730 irq_domain_add(domain);
Russell Kingbef8f9e2010-12-04 16:50:58 +0000731
Colin Cross692c3e252011-02-10 12:54:10 -0800732 gic_chip.flags |= gic_arch_extn.flags;
Rob Herringc383e042011-09-28 21:25:31 -0500733 gic_dist_init(gic);
Russell Kingbef8f9e2010-12-04 16:50:58 +0000734 gic_cpu_init(gic);
Colin Cross692c3e252011-02-10 12:54:10 -0800735 gic_pm_init(gic);
Russell Kingb580b892010-12-04 15:55:14 +0000736}
737
Russell King38489532010-12-04 16:01:03 +0000738void __cpuinit gic_secondary_init(unsigned int gic_nr)
739{
Russell Kingbef8f9e2010-12-04 16:50:58 +0000740 BUG_ON(gic_nr >= MAX_GIC_NR);
741
742 gic_cpu_init(&gic_data[gic_nr]);
Russell King38489532010-12-04 16:01:03 +0000743}
744
Russell Kingf27ecac2005-08-18 21:31:00 +0100745#ifdef CONFIG_SMP
Russell King82668102009-05-17 16:20:18 +0100746void gic_raise_softirq(const struct cpumask *mask, unsigned int irq)
Russell Kingf27ecac2005-08-18 21:31:00 +0100747{
Will Deacona803a8d2011-08-23 22:20:03 +0100748 int cpu;
749 unsigned long map = 0;
750
751 /* Convert our logical CPU mask into a physical one. */
752 for_each_cpu(cpu, mask)
753 map |= 1 << cpu_logical_map(cpu);
Russell Kingf27ecac2005-08-18 21:31:00 +0100754
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530755 /*
756 * Ensure that stores to Normal memory are visible to the
757 * other CPUs before issuing the IPI.
758 */
759 dsb();
760
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100761 /* this always happens on GIC0 */
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530762 writel_relaxed(map << 16 | irq, gic_data[0].dist_base + GIC_DIST_SOFTINT);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700763 mb();
Russell Kingf27ecac2005-08-18 21:31:00 +0100764}
765#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700766
767/* before calling this function the interrupts should be disabled
768 * and the irq must be disabled at gic to avoid spurious interrupts */
769bool gic_is_spi_pending(unsigned int irq)
770{
771 struct irq_data *d = irq_get_irq_data(irq);
772 struct gic_chip_data *gic_data = &gic_data[0];
773 u32 mask, val;
774
775 WARN_ON(!irqs_disabled());
Thomas Gleixner450ea482009-07-03 08:44:46 -0500776 raw_spin_lock(&irq_controller_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700777 mask = 1 << (gic_irq(d) % 32);
778 val = readl(gic_dist_base(d) +
779 GIC_DIST_ENABLE_SET + (gic_irq(d) / 32) * 4);
780 /* warn if the interrupt is enabled */
781 WARN_ON(val & mask);
782 val = readl(gic_dist_base(d) +
783 GIC_DIST_PENDING_SET + (gic_irq(d) / 32) * 4);
Thomas Gleixner450ea482009-07-03 08:44:46 -0500784 raw_spin_unlock(&irq_controller_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700785 return (bool) (val & mask);
786}
787
788/* before calling this function the interrupts should be disabled
789 * and the irq must be disabled at gic to avoid spurious interrupts */
790void gic_clear_spi_pending(unsigned int irq)
791{
792 struct gic_chip_data *gic_data = &gic_data[0];
793 struct irq_data *d = irq_get_irq_data(irq);
794
795 u32 mask, val;
796 WARN_ON(!irqs_disabled());
Thomas Gleixner450ea482009-07-03 08:44:46 -0500797 raw_spin_lock(&irq_controller_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700798 mask = 1 << (gic_irq(d) % 32);
799 val = readl(gic_dist_base(d) +
800 GIC_DIST_ENABLE_SET + (gic_irq(d) / 32) * 4);
801 /* warn if the interrupt is enabled */
802 WARN_ON(val & mask);
803 writel(mask, gic_dist_base(d) +
804 GIC_DIST_PENDING_CLEAR + (gic_irq(d) / 32) * 4);
Thomas Gleixner450ea482009-07-03 08:44:46 -0500805 raw_spin_unlock(&irq_controller_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700806}
Rob Herring0fc0d942011-09-28 21:27:52 -0500807#ifdef CONFIG_OF
808static int gic_cnt __initdata = 0;
809
810int __init gic_of_init(struct device_node *node, struct device_node *parent)
811{
812 void __iomem *cpu_base;
813 void __iomem *dist_base;
814 int irq;
815 struct irq_domain *domain = &gic_data[gic_cnt].domain;
816
817 if (WARN_ON(!node))
818 return -ENODEV;
819
820 dist_base = of_iomap(node, 0);
821 WARN(!dist_base, "unable to map gic dist registers\n");
822
823 cpu_base = of_iomap(node, 1);
824 WARN(!cpu_base, "unable to map gic cpu registers\n");
825
826 domain->of_node = of_node_get(node);
827
828 gic_init(gic_cnt, 16, dist_base, cpu_base);
829
830 if (parent) {
831 irq = irq_of_parse_and_map(node, 0);
832 gic_cascade_irq(gic_cnt, irq);
833 }
834 gic_cnt++;
835 return 0;
836}
837#endif