blob: 73b97f2633da68a3d63af8a0b5de07200e89a061 [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>
Rob Herringc383e042011-09-28 21:25:31 -050028#include <linux/export.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>
Russell Kingf27ecac2005-08-18 21:31:00 +010044#include <asm/mach/irq.h>
45#include <asm/hardware/gic.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070046#include <asm/system.h>
Trilok Sonieecb28c2011-07-20 16:24:14 +010047#include <asm/localtimer.h>
Russell Kingf27ecac2005-08-18 21:31:00 +010048
Thomas Gleixner450ea482009-07-03 08:44:46 -050049static DEFINE_RAW_SPINLOCK(irq_controller_lock);
Russell Kingf27ecac2005-08-18 21:31:00 +010050
Russell Kingff2e27a2010-12-04 16:13:29 +000051/* Address of GIC 0 CPU interface */
Russell Kingbef8f9e2010-12-04 16:50:58 +000052void __iomem *gic_cpu_base_addr __read_mostly;
Russell Kingff2e27a2010-12-04 16:13:29 +000053
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +010054/*
55 * Supported arch specific GIC irq extension.
56 * Default make them NULL.
57 */
58struct irq_chip gic_arch_extn = {
Will Deacon1a017532011-02-09 12:01:12 +000059 .irq_eoi = NULL,
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +010060 .irq_mask = NULL,
61 .irq_unmask = NULL,
62 .irq_retrigger = NULL,
63 .irq_set_type = NULL,
64 .irq_set_wake = NULL,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070065 .irq_disable = NULL,
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +010066};
67
Catalin Marinasb3a1bde2007-02-14 19:14:56 +010068#ifndef MAX_GIC_NR
69#define MAX_GIC_NR 1
70#endif
71
Russell Kingbef8f9e2010-12-04 16:50:58 +000072static struct gic_chip_data gic_data[MAX_GIC_NR] __read_mostly;
Catalin Marinasb3a1bde2007-02-14 19:14:56 +010073
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +010074static inline void __iomem *gic_dist_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->dist_base;
78}
79
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +010080static inline void __iomem *gic_cpu_base(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);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +010083 return gic_data->cpu_base;
84}
85
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +010086static inline unsigned int gic_irq(struct irq_data *d)
Catalin Marinasb3a1bde2007-02-14 19:14:56 +010087{
Rob Herringc383e042011-09-28 21:25:31 -050088 return d->hwirq;
Catalin Marinasb3a1bde2007-02-14 19:14:56 +010089}
90
Russell Kingf27ecac2005-08-18 21:31:00 +010091/*
92 * Routines to acknowledge, disable and enable interrupts
Russell Kingf27ecac2005-08-18 21:31:00 +010093 */
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +010094static void gic_mask_irq(struct irq_data *d)
Russell Kingf27ecac2005-08-18 21:31:00 +010095{
Rob Herringc383e042011-09-28 21:25:31 -050096 u32 mask = 1 << (gic_irq(d) % 32);
Thomas Gleixnerc4bfa282006-07-01 22:32:14 +010097
Thomas Gleixner450ea482009-07-03 08:44:46 -050098 raw_spin_lock(&irq_controller_lock);
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +053099 writel_relaxed(mask, gic_dist_base(d) + GIC_DIST_ENABLE_CLEAR + (gic_irq(d) / 32) * 4);
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100100 if (gic_arch_extn.irq_mask)
101 gic_arch_extn.irq_mask(d);
Thomas Gleixner450ea482009-07-03 08:44:46 -0500102 raw_spin_unlock(&irq_controller_lock);
Russell Kingf27ecac2005-08-18 21:31:00 +0100103}
104
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100105static void gic_unmask_irq(struct irq_data *d)
Russell Kingf27ecac2005-08-18 21:31:00 +0100106{
Rob Herringc383e042011-09-28 21:25:31 -0500107 u32 mask = 1 << (gic_irq(d) % 32);
Thomas Gleixnerc4bfa282006-07-01 22:32:14 +0100108
Thomas Gleixner450ea482009-07-03 08:44:46 -0500109 raw_spin_lock(&irq_controller_lock);
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100110 if (gic_arch_extn.irq_unmask)
111 gic_arch_extn.irq_unmask(d);
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530112 writel_relaxed(mask, gic_dist_base(d) + GIC_DIST_ENABLE_SET + (gic_irq(d) / 32) * 4);
Thomas Gleixner450ea482009-07-03 08:44:46 -0500113 raw_spin_unlock(&irq_controller_lock);
Russell Kingf27ecac2005-08-18 21:31:00 +0100114}
115
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700116static void gic_disable_irq(struct irq_data *d)
117{
118 if (gic_arch_extn.irq_disable)
119 gic_arch_extn.irq_disable(d);
120}
121
122#ifdef CONFIG_PM
123static int gic_suspend_one(struct gic_chip_data *gic)
124{
125 unsigned int i;
126 void __iomem *base = gic->dist_base;
127
128 for (i = 0; i * 32 < gic->max_irq; i++) {
129 gic->enabled_irqs[i]
130 = readl_relaxed(base + GIC_DIST_ENABLE_SET + i * 4);
131 /* disable all of them */
132 writel_relaxed(0xffffffff, base + GIC_DIST_ENABLE_CLEAR + i * 4);
133 /* enable the wakeup set */
134 writel_relaxed(gic->wakeup_irqs[i],
135 base + GIC_DIST_ENABLE_SET + i * 4);
136 }
137 mb();
138 return 0;
139}
140
141static int gic_suspend(void)
142{
143 int i;
144 for (i = 0; i < MAX_GIC_NR; i++)
145 gic_suspend_one(&gic_data[i]);
146 return 0;
147}
148
149extern int msm_show_resume_irq_mask;
150
151static void gic_show_resume_irq(struct gic_chip_data *gic)
152{
153 unsigned int i;
154 u32 enabled;
155 unsigned long pending[32];
156 void __iomem *base = gic->dist_base;
157
158 if (!msm_show_resume_irq_mask)
159 return;
160
Thomas Gleixner450ea482009-07-03 08:44:46 -0500161 raw_spin_lock(&irq_controller_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700162 for (i = 0; i * 32 < gic->max_irq; i++) {
163 enabled = readl_relaxed(base + GIC_DIST_ENABLE_CLEAR + i * 4);
164 pending[i] = readl_relaxed(base + GIC_DIST_PENDING_SET + i * 4);
165 pending[i] &= enabled;
166 }
Thomas Gleixner450ea482009-07-03 08:44:46 -0500167 raw_spin_unlock(&irq_controller_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700168
169 for (i = find_first_bit(pending, gic->max_irq);
170 i < gic->max_irq;
171 i = find_next_bit(pending, gic->max_irq, i+1)) {
172 pr_warning("%s: %d triggered", __func__,
173 i + gic->irq_offset);
174 }
175}
176
177static void gic_resume_one(struct gic_chip_data *gic)
178{
179 unsigned int i;
180 void __iomem *base = gic->dist_base;
181
182 gic_show_resume_irq(gic);
183 for (i = 0; i * 32 < gic->max_irq; i++) {
184 /* disable all of them */
185 writel_relaxed(0xffffffff, base + GIC_DIST_ENABLE_CLEAR + i * 4);
186 /* enable the enabled set */
187 writel_relaxed(gic->enabled_irqs[i],
188 base + GIC_DIST_ENABLE_SET + i * 4);
189 }
190 mb();
191}
192
193static void gic_resume(void)
194{
195 int i;
196 for (i = 0; i < MAX_GIC_NR; i++)
197 gic_resume_one(&gic_data[i]);
198}
199
200static struct syscore_ops gic_syscore_ops = {
201 .suspend = gic_suspend,
202 .resume = gic_resume,
203};
204
205static int __init gic_init_sys(void)
206{
207 register_syscore_ops(&gic_syscore_ops);
208 return 0;
209}
210arch_initcall(gic_init_sys);
211
212#endif
213
Will Deacon1a017532011-02-09 12:01:12 +0000214static void gic_eoi_irq(struct irq_data *d)
215{
216 if (gic_arch_extn.irq_eoi) {
Thomas Gleixner450ea482009-07-03 08:44:46 -0500217 raw_spin_lock(&irq_controller_lock);
Will Deacon1a017532011-02-09 12:01:12 +0000218 gic_arch_extn.irq_eoi(d);
Thomas Gleixner450ea482009-07-03 08:44:46 -0500219 raw_spin_unlock(&irq_controller_lock);
Will Deacon1a017532011-02-09 12:01:12 +0000220 }
221
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530222 writel_relaxed(gic_irq(d), gic_cpu_base(d) + GIC_CPU_EOI);
Will Deacon1a017532011-02-09 12:01:12 +0000223}
224
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100225static int gic_set_type(struct irq_data *d, unsigned int type)
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100226{
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100227 void __iomem *base = gic_dist_base(d);
228 unsigned int gicirq = gic_irq(d);
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100229 u32 enablemask = 1 << (gicirq % 32);
230 u32 enableoff = (gicirq / 32) * 4;
231 u32 confmask = 0x2 << ((gicirq % 16) * 2);
232 u32 confoff = (gicirq / 16) * 4;
233 bool enabled = false;
234 u32 val;
235
236 /* Interrupt configuration for SGIs can't be changed */
237 if (gicirq < 16)
238 return -EINVAL;
239
240 if (type != IRQ_TYPE_LEVEL_HIGH && type != IRQ_TYPE_EDGE_RISING)
241 return -EINVAL;
242
Thomas Gleixner450ea482009-07-03 08:44:46 -0500243 raw_spin_lock(&irq_controller_lock);
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100244
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100245 if (gic_arch_extn.irq_set_type)
246 gic_arch_extn.irq_set_type(d, type);
247
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530248 val = readl_relaxed(base + GIC_DIST_CONFIG + confoff);
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100249 if (type == IRQ_TYPE_LEVEL_HIGH)
250 val &= ~confmask;
251 else if (type == IRQ_TYPE_EDGE_RISING)
252 val |= confmask;
253
254 /*
255 * As recommended by the spec, disable the interrupt before changing
256 * the configuration
257 */
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530258 if (readl_relaxed(base + GIC_DIST_ENABLE_SET + enableoff) & enablemask) {
259 writel_relaxed(enablemask, base + GIC_DIST_ENABLE_CLEAR + enableoff);
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100260 enabled = true;
261 }
262
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530263 writel_relaxed(val, base + GIC_DIST_CONFIG + confoff);
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100264
265 if (enabled)
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530266 writel_relaxed(enablemask, base + GIC_DIST_ENABLE_SET + enableoff);
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100267
Thomas Gleixner450ea482009-07-03 08:44:46 -0500268 raw_spin_unlock(&irq_controller_lock);
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100269
270 return 0;
271}
272
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100273static int gic_retrigger(struct irq_data *d)
274{
275 if (gic_arch_extn.irq_retrigger)
276 return gic_arch_extn.irq_retrigger(d);
277
Abhijeet Dharmapurikar9d44ea02011-10-30 16:47:19 -0700278 /* the retrigger expects 0 for failure */
279 return 0;
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100280}
281
Catalin Marinasa06f5462005-09-30 16:07:05 +0100282#ifdef CONFIG_SMP
Russell Kingc1917892011-01-23 12:12:01 +0000283static int gic_set_affinity(struct irq_data *d, const struct cpumask *mask_val,
284 bool force)
Russell Kingf27ecac2005-08-18 21:31:00 +0100285{
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100286 void __iomem *reg = gic_dist_base(d) + GIC_DIST_TARGET + (gic_irq(d) & ~3);
Rob Herringc383e042011-09-28 21:25:31 -0500287 unsigned int shift = (gic_irq(d) % 4) * 8;
Russell Kingf3c52e22011-07-21 15:00:57 +0100288 unsigned int cpu = cpumask_any_and(mask_val, cpu_online_mask);
Russell Kingc1917892011-01-23 12:12:01 +0000289 u32 val, mask, bit;
290
Russell Kingf3c52e22011-07-21 15:00:57 +0100291 if (cpu >= 8 || cpu >= nr_cpu_ids)
Russell Kingc1917892011-01-23 12:12:01 +0000292 return -EINVAL;
293
294 mask = 0xff << shift;
Will Deacona803a8d2011-08-23 22:20:03 +0100295 bit = 1 << (cpu_logical_map(cpu) + shift);
Russell Kingf27ecac2005-08-18 21:31:00 +0100296
Thomas Gleixner450ea482009-07-03 08:44:46 -0500297 raw_spin_lock(&irq_controller_lock);
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530298 val = readl_relaxed(reg) & ~mask;
299 writel_relaxed(val | bit, reg);
Thomas Gleixner450ea482009-07-03 08:44:46 -0500300 raw_spin_unlock(&irq_controller_lock);
Yinghai Lud5dedd42009-04-27 17:59:21 -0700301
Russell Kingf3c52e22011-07-21 15:00:57 +0100302 return IRQ_SET_MASK_OK;
Russell Kingf27ecac2005-08-18 21:31:00 +0100303}
Catalin Marinasa06f5462005-09-30 16:07:05 +0100304#endif
Russell Kingf27ecac2005-08-18 21:31:00 +0100305
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100306#ifdef CONFIG_PM
307static int gic_set_wake(struct irq_data *d, unsigned int on)
308{
309 int ret = -ENXIO;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700310 unsigned int reg_offset, bit_offset;
311 unsigned int gicirq = gic_irq(d);
312 struct gic_chip_data *gic_data = irq_data_get_irq_chip_data(d);
313
314 /* per-cpu interrupts cannot be wakeup interrupts */
315 WARN_ON(gicirq < 32);
316
317 reg_offset = gicirq / 32;
318 bit_offset = gicirq % 32;
319
320 if (on)
321 gic_data->wakeup_irqs[reg_offset] |= 1 << bit_offset;
322 else
323 gic_data->wakeup_irqs[reg_offset] &= ~(1 << bit_offset);
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100324
325 if (gic_arch_extn.irq_set_wake)
326 ret = gic_arch_extn.irq_set_wake(d, on);
327
328 return ret;
329}
330
331#else
Rohit Vaswani550aa1a2011-10-06 21:15:37 -0700332static int gic_set_wake(struct irq_data *d, unsigned int on)
333{
334 return 0;
335}
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100336#endif
337
Russell King0f347bb2007-05-17 10:11:34 +0100338static void gic_handle_cascade_irq(unsigned int irq, struct irq_desc *desc)
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100339{
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100340 struct gic_chip_data *chip_data = irq_get_handler_data(irq);
341 struct irq_chip *chip = irq_get_chip(irq);
Russell King0f347bb2007-05-17 10:11:34 +0100342 unsigned int cascade_irq, gic_irq;
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100343 unsigned long status;
344
Will Deacon1a017532011-02-09 12:01:12 +0000345 chained_irq_enter(chip, desc);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100346
Thomas Gleixner450ea482009-07-03 08:44:46 -0500347 raw_spin_lock(&irq_controller_lock);
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530348 status = readl_relaxed(chip_data->cpu_base + GIC_CPU_INTACK);
Thomas Gleixner450ea482009-07-03 08:44:46 -0500349 raw_spin_unlock(&irq_controller_lock);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100350
Russell King0f347bb2007-05-17 10:11:34 +0100351 gic_irq = (status & 0x3ff);
352 if (gic_irq == 1023)
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100353 goto out;
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100354
Rob Herringc383e042011-09-28 21:25:31 -0500355 cascade_irq = irq_domain_to_irq(&chip_data->domain, gic_irq);
Russell King0f347bb2007-05-17 10:11:34 +0100356 if (unlikely(gic_irq < 32 || gic_irq > 1020 || cascade_irq >= NR_IRQS))
357 do_bad_IRQ(cascade_irq, desc);
358 else
359 generic_handle_irq(cascade_irq);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100360
361 out:
Will Deacon1a017532011-02-09 12:01:12 +0000362 chained_irq_exit(chip, desc);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100363}
364
David Brownell38c677c2006-08-01 22:26:25 +0100365static struct irq_chip gic_chip = {
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100366 .name = "GIC",
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100367 .irq_mask = gic_mask_irq,
368 .irq_unmask = gic_unmask_irq,
Will Deacon1a017532011-02-09 12:01:12 +0000369 .irq_eoi = gic_eoi_irq,
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100370 .irq_set_type = gic_set_type,
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100371 .irq_retrigger = gic_retrigger,
Russell Kingf27ecac2005-08-18 21:31:00 +0100372#ifdef CONFIG_SMP
Russell Kingc1917892011-01-23 12:12:01 +0000373 .irq_set_affinity = gic_set_affinity,
Russell Kingf27ecac2005-08-18 21:31:00 +0100374#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700375 .irq_disable = gic_disable_irq,
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100376 .irq_set_wake = gic_set_wake,
Russell Kingf27ecac2005-08-18 21:31:00 +0100377};
378
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100379void __init gic_cascade_irq(unsigned int gic_nr, unsigned int irq)
380{
381 if (gic_nr >= MAX_GIC_NR)
382 BUG();
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100383 if (irq_set_handler_data(irq, &gic_data[gic_nr]) != 0)
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100384 BUG();
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100385 irq_set_chained_handler(irq, gic_handle_cascade_irq);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100386}
387
Rob Herringc383e042011-09-28 21:25:31 -0500388static void __init gic_dist_init(struct gic_chip_data *gic)
Russell Kingf27ecac2005-08-18 21:31:00 +0100389{
Rob Herringc383e042011-09-28 21:25:31 -0500390 unsigned int i, irq;
Will Deacona803a8d2011-08-23 22:20:03 +0100391 u32 cpumask;
Rob Herringc383e042011-09-28 21:25:31 -0500392 unsigned int gic_irqs = gic->gic_irqs;
393 struct irq_domain *domain = &gic->domain;
Russell Kingbef8f9e2010-12-04 16:50:58 +0000394 void __iomem *base = gic->dist_base;
Will Deacona803a8d2011-08-23 22:20:03 +0100395 u32 cpu = 0;
Russell Kingf27ecac2005-08-18 21:31:00 +0100396
Will Deacona803a8d2011-08-23 22:20:03 +0100397#ifdef CONFIG_SMP
398 cpu = cpu_logical_map(smp_processor_id());
399#endif
400
401 cpumask = 1 << cpu;
Russell Kingf27ecac2005-08-18 21:31:00 +0100402 cpumask |= cpumask << 8;
403 cpumask |= cpumask << 16;
404
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530405 writel_relaxed(0, base + GIC_DIST_CTRL);
Russell Kingf27ecac2005-08-18 21:31:00 +0100406
407 /*
Russell Kingf27ecac2005-08-18 21:31:00 +0100408 * Set all global interrupts to be level triggered, active low.
409 */
Pawel Molle6afec92010-11-26 13:45:43 +0100410 for (i = 32; i < gic_irqs; i += 16)
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530411 writel_relaxed(0, base + GIC_DIST_CONFIG + i * 4 / 16);
Russell Kingf27ecac2005-08-18 21:31:00 +0100412
413 /*
414 * Set all global interrupts to this CPU only.
415 */
Pawel Molle6afec92010-11-26 13:45:43 +0100416 for (i = 32; i < gic_irqs; i += 4)
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530417 writel_relaxed(cpumask, base + GIC_DIST_TARGET + i * 4 / 4);
Russell Kingf27ecac2005-08-18 21:31:00 +0100418
419 /*
Russell King9395f6e2010-11-11 23:10:30 +0000420 * Set priority on all global interrupts.
Russell Kingf27ecac2005-08-18 21:31:00 +0100421 */
Pawel Molle6afec92010-11-26 13:45:43 +0100422 for (i = 32; i < gic_irqs; i += 4)
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530423 writel_relaxed(0xa0a0a0a0, base + GIC_DIST_PRI + i * 4 / 4);
Russell Kingf27ecac2005-08-18 21:31:00 +0100424
425 /*
Russell King9395f6e2010-11-11 23:10:30 +0000426 * Disable all interrupts. Leave the PPI and SGIs alone
427 * as these enables are banked registers.
Russell Kingf27ecac2005-08-18 21:31:00 +0100428 */
Pawel Molle6afec92010-11-26 13:45:43 +0100429 for (i = 32; i < gic_irqs; i += 32)
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530430 writel_relaxed(0xffffffff, base + GIC_DIST_ENABLE_CLEAR + i * 4 / 32);
Russell Kingf27ecac2005-08-18 21:31:00 +0100431
432 /*
433 * Setup the Linux IRQ subsystem.
434 */
Rob Herringc383e042011-09-28 21:25:31 -0500435 irq_domain_for_each_irq(domain, i, irq) {
436 if (i < 32) {
437 irq_set_percpu_devid(irq);
438 irq_set_chip_and_handler(irq, &gic_chip,
439 handle_percpu_devid_irq);
440 set_irq_flags(irq, IRQF_VALID | IRQF_NOAUTOEN);
441 } else {
442 irq_set_chip_and_handler(irq, &gic_chip,
443 handle_fasteoi_irq);
444 set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
445 }
446 irq_set_chip_data(irq, gic);
Russell Kingf27ecac2005-08-18 21:31:00 +0100447 }
448
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700449 gic->max_irq = gic_irqs;
450
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530451 writel_relaxed(1, base + GIC_DIST_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700452 mb();
Russell Kingf27ecac2005-08-18 21:31:00 +0100453}
454
Russell Kingbef8f9e2010-12-04 16:50:58 +0000455static void __cpuinit gic_cpu_init(struct gic_chip_data *gic)
Russell Kingf27ecac2005-08-18 21:31:00 +0100456{
Russell Kingbef8f9e2010-12-04 16:50:58 +0000457 void __iomem *dist_base = gic->dist_base;
458 void __iomem *base = gic->cpu_base;
Russell King9395f6e2010-11-11 23:10:30 +0000459 int i;
460
Russell King9395f6e2010-11-11 23:10:30 +0000461 /*
462 * Deal with the banked PPI and SGI interrupts - disable all
463 * PPI interrupts, ensure all SGI interrupts are enabled.
464 */
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530465 writel_relaxed(0xffff0000, dist_base + GIC_DIST_ENABLE_CLEAR);
466 writel_relaxed(0x0000ffff, dist_base + GIC_DIST_ENABLE_SET);
Russell King9395f6e2010-11-11 23:10:30 +0000467
468 /*
469 * Set priority on PPI and SGI interrupts
470 */
471 for (i = 0; i < 32; i += 4)
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530472 writel_relaxed(0xa0a0a0a0, dist_base + GIC_DIST_PRI + i * 4 / 4);
Russell King9395f6e2010-11-11 23:10:30 +0000473
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530474 writel_relaxed(0xf0, base + GIC_CPU_PRIMASK);
475 writel_relaxed(1, base + GIC_CPU_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700476 mb();
Russell Kingf27ecac2005-08-18 21:31:00 +0100477}
478
Colin Cross692c3e252011-02-10 12:54:10 -0800479#ifdef CONFIG_CPU_PM
480/*
481 * Saves the GIC distributor registers during suspend or idle. Must be called
482 * with interrupts disabled but before powering down the GIC. After calling
483 * this function, no interrupts will be delivered by the GIC, and another
484 * platform-specific wakeup source must be enabled.
485 */
486static void gic_dist_save(unsigned int gic_nr)
487{
488 unsigned int gic_irqs;
489 void __iomem *dist_base;
490 int i;
491
492 if (gic_nr >= MAX_GIC_NR)
493 BUG();
494
495 gic_irqs = gic_data[gic_nr].gic_irqs;
496 dist_base = gic_data[gic_nr].dist_base;
497
498 if (!dist_base)
499 return;
500
501 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 16); i++)
502 gic_data[gic_nr].saved_spi_conf[i] =
503 readl_relaxed(dist_base + GIC_DIST_CONFIG + i * 4);
504
505 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 4); i++)
506 gic_data[gic_nr].saved_spi_target[i] =
507 readl_relaxed(dist_base + GIC_DIST_TARGET + i * 4);
508
509 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 32); i++)
510 gic_data[gic_nr].saved_spi_enable[i] =
511 readl_relaxed(dist_base + GIC_DIST_ENABLE_SET + i * 4);
512}
513
514/*
515 * Restores the GIC distributor registers during resume or when coming out of
516 * idle. Must be called before enabling interrupts. If a level interrupt
517 * that occured while the GIC was suspended is still present, it will be
518 * handled normally, but any edge interrupts that occured will not be seen by
519 * the GIC and need to be handled by the platform-specific wakeup source.
520 */
521static void gic_dist_restore(unsigned int gic_nr)
522{
523 unsigned int gic_irqs;
524 unsigned int i;
525 void __iomem *dist_base;
526
527 if (gic_nr >= MAX_GIC_NR)
528 BUG();
529
530 gic_irqs = gic_data[gic_nr].gic_irqs;
531 dist_base = gic_data[gic_nr].dist_base;
532
533 if (!dist_base)
534 return;
535
536 writel_relaxed(0, dist_base + GIC_DIST_CTRL);
537
538 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 16); i++)
539 writel_relaxed(gic_data[gic_nr].saved_spi_conf[i],
540 dist_base + GIC_DIST_CONFIG + i * 4);
541
542 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 4); i++)
543 writel_relaxed(0xa0a0a0a0,
544 dist_base + GIC_DIST_PRI + i * 4);
545
546 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 4); i++)
547 writel_relaxed(gic_data[gic_nr].saved_spi_target[i],
548 dist_base + GIC_DIST_TARGET + i * 4);
549
550 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 32); i++)
551 writel_relaxed(gic_data[gic_nr].saved_spi_enable[i],
552 dist_base + GIC_DIST_ENABLE_SET + i * 4);
553
554 writel_relaxed(1, dist_base + GIC_DIST_CTRL);
555}
556
557static void gic_cpu_save(unsigned int gic_nr)
558{
559 int i;
560 u32 *ptr;
561 void __iomem *dist_base;
562 void __iomem *cpu_base;
563
564 if (gic_nr >= MAX_GIC_NR)
565 BUG();
566
567 dist_base = gic_data[gic_nr].dist_base;
568 cpu_base = gic_data[gic_nr].cpu_base;
569
570 if (!dist_base || !cpu_base)
571 return;
572
573 ptr = __this_cpu_ptr(gic_data[gic_nr].saved_ppi_enable);
574 for (i = 0; i < DIV_ROUND_UP(32, 32); i++)
575 ptr[i] = readl_relaxed(dist_base + GIC_DIST_ENABLE_SET + i * 4);
576
577 ptr = __this_cpu_ptr(gic_data[gic_nr].saved_ppi_conf);
578 for (i = 0; i < DIV_ROUND_UP(32, 16); i++)
579 ptr[i] = readl_relaxed(dist_base + GIC_DIST_CONFIG + i * 4);
580
581}
582
583static void gic_cpu_restore(unsigned int gic_nr)
584{
585 int i;
586 u32 *ptr;
587 void __iomem *dist_base;
588 void __iomem *cpu_base;
589
590 if (gic_nr >= MAX_GIC_NR)
591 BUG();
592
593 dist_base = gic_data[gic_nr].dist_base;
594 cpu_base = gic_data[gic_nr].cpu_base;
595
596 if (!dist_base || !cpu_base)
597 return;
598
599 ptr = __this_cpu_ptr(gic_data[gic_nr].saved_ppi_enable);
600 for (i = 0; i < DIV_ROUND_UP(32, 32); i++)
601 writel_relaxed(ptr[i], dist_base + GIC_DIST_ENABLE_SET + i * 4);
602
603 ptr = __this_cpu_ptr(gic_data[gic_nr].saved_ppi_conf);
604 for (i = 0; i < DIV_ROUND_UP(32, 16); i++)
605 writel_relaxed(ptr[i], dist_base + GIC_DIST_CONFIG + i * 4);
606
607 for (i = 0; i < DIV_ROUND_UP(32, 4); i++)
608 writel_relaxed(0xa0a0a0a0, dist_base + GIC_DIST_PRI + i * 4);
609
610 writel_relaxed(0xf0, cpu_base + GIC_CPU_PRIMASK);
611 writel_relaxed(1, cpu_base + GIC_CPU_CTRL);
612}
613
614static int gic_notifier(struct notifier_block *self, unsigned long cmd, void *v)
615{
616 int i;
617
618 for (i = 0; i < MAX_GIC_NR; i++) {
619 switch (cmd) {
620 case CPU_PM_ENTER:
621 gic_cpu_save(i);
622 break;
623 case CPU_PM_ENTER_FAILED:
624 case CPU_PM_EXIT:
625 gic_cpu_restore(i);
626 break;
627 case CPU_CLUSTER_PM_ENTER:
628 gic_dist_save(i);
629 break;
630 case CPU_CLUSTER_PM_ENTER_FAILED:
631 case CPU_CLUSTER_PM_EXIT:
632 gic_dist_restore(i);
633 break;
634 }
635 }
636
637 return NOTIFY_OK;
638}
639
640static struct notifier_block gic_notifier_block = {
641 .notifier_call = gic_notifier,
642};
643
644static void __init gic_pm_init(struct gic_chip_data *gic)
645{
646 gic->saved_ppi_enable = __alloc_percpu(DIV_ROUND_UP(32, 32) * 4,
647 sizeof(u32));
648 BUG_ON(!gic->saved_ppi_enable);
649
650 gic->saved_ppi_conf = __alloc_percpu(DIV_ROUND_UP(32, 16) * 4,
651 sizeof(u32));
652 BUG_ON(!gic->saved_ppi_conf);
653
654 cpu_pm_register_notifier(&gic_notifier_block);
655}
656#else
657static void __init gic_pm_init(struct gic_chip_data *gic)
658{
659}
660#endif
661
Rob Herring0fc0d942011-09-28 21:27:52 -0500662#ifdef CONFIG_OF
663static int gic_irq_domain_dt_translate(struct irq_domain *d,
664 struct device_node *controller,
665 const u32 *intspec, unsigned int intsize,
666 unsigned long *out_hwirq, unsigned int *out_type)
667{
668 if (d->of_node != controller)
669 return -EINVAL;
670 if (intsize < 3)
671 return -EINVAL;
672
673 /* Get the interrupt number and add 16 to skip over SGIs */
674 *out_hwirq = intspec[1] + 16;
675
676 /* For SPIs, we need to add 16 more to get the GIC irq ID number */
677 if (!intspec[0])
678 *out_hwirq += 16;
679
680 *out_type = intspec[2] & IRQ_TYPE_SENSE_MASK;
681 return 0;
682}
683#endif
684
Rob Herringc383e042011-09-28 21:25:31 -0500685const struct irq_domain_ops gic_irq_domain_ops = {
Rob Herring0fc0d942011-09-28 21:27:52 -0500686#ifdef CONFIG_OF
687 .dt_translate = gic_irq_domain_dt_translate,
688#endif
Rob Herringc383e042011-09-28 21:25:31 -0500689};
690
Rob Herring050113e2011-10-21 17:14:27 -0500691void __init gic_init(unsigned int gic_nr, int irq_start,
Russell Kingb580b892010-12-04 15:55:14 +0000692 void __iomem *dist_base, void __iomem *cpu_base)
693{
Russell Kingbef8f9e2010-12-04 16:50:58 +0000694 struct gic_chip_data *gic;
Rob Herringc383e042011-09-28 21:25:31 -0500695 struct irq_domain *domain;
696 int gic_irqs;
Russell Kingbef8f9e2010-12-04 16:50:58 +0000697
698 BUG_ON(gic_nr >= MAX_GIC_NR);
699
700 gic = &gic_data[gic_nr];
Rob Herringc383e042011-09-28 21:25:31 -0500701 domain = &gic->domain;
Russell Kingbef8f9e2010-12-04 16:50:58 +0000702 gic->dist_base = dist_base;
703 gic->cpu_base = cpu_base;
Russell Kingbef8f9e2010-12-04 16:50:58 +0000704
Rob Herringc383e042011-09-28 21:25:31 -0500705 /*
706 * For primary GICs, skip over SGIs.
707 * For secondary GICs, skip over PPIs, too.
708 */
709 if (gic_nr == 0) {
Russell Kingff2e27a2010-12-04 16:13:29 +0000710 gic_cpu_base_addr = cpu_base;
Rob Herringc383e042011-09-28 21:25:31 -0500711 domain->hwirq_base = 16;
Rob Herring050113e2011-10-21 17:14:27 -0500712 if (irq_start > 0)
713 irq_start = (irq_start & ~31) + 16;
Rob Herringc383e042011-09-28 21:25:31 -0500714 } else
715 domain->hwirq_base = 32;
716
717 /*
718 * Find out how many interrupts are supported.
719 * The GIC only supports up to 1020 interrupt sources.
720 */
721 gic_irqs = readl_relaxed(dist_base + GIC_DIST_CTR) & 0x1f;
722 gic_irqs = (gic_irqs + 1) * 32;
723 if (gic_irqs > 1020)
724 gic_irqs = 1020;
725 gic->gic_irqs = gic_irqs;
726
727 domain->nr_irq = gic_irqs - domain->hwirq_base;
Rob Herring050113e2011-10-21 17:14:27 -0500728 domain->irq_base = irq_alloc_descs(irq_start, 16, domain->nr_irq,
Rob Herringc383e042011-09-28 21:25:31 -0500729 numa_node_id());
Rob Herring050113e2011-10-21 17:14:27 -0500730 if (IS_ERR_VALUE(domain->irq_base)) {
731 WARN(1, "Cannot allocate irq_descs @ IRQ%d, assuming pre-allocated\n",
732 irq_start);
733 domain->irq_base = irq_start;
734 }
Rob Herringc383e042011-09-28 21:25:31 -0500735 domain->priv = gic;
736 domain->ops = &gic_irq_domain_ops;
737 irq_domain_add(domain);
Russell Kingbef8f9e2010-12-04 16:50:58 +0000738
Colin Cross692c3e252011-02-10 12:54:10 -0800739 gic_chip.flags |= gic_arch_extn.flags;
Rob Herringc383e042011-09-28 21:25:31 -0500740 gic_dist_init(gic);
Russell Kingbef8f9e2010-12-04 16:50:58 +0000741 gic_cpu_init(gic);
Colin Cross692c3e252011-02-10 12:54:10 -0800742 gic_pm_init(gic);
Russell Kingb580b892010-12-04 15:55:14 +0000743}
744
Russell King38489532010-12-04 16:01:03 +0000745void __cpuinit gic_secondary_init(unsigned int gic_nr)
746{
Russell Kingbef8f9e2010-12-04 16:50:58 +0000747 BUG_ON(gic_nr >= MAX_GIC_NR);
748
749 gic_cpu_init(&gic_data[gic_nr]);
Russell King38489532010-12-04 16:01:03 +0000750}
751
Russell Kingf27ecac2005-08-18 21:31:00 +0100752#ifdef CONFIG_SMP
Russell King82668102009-05-17 16:20:18 +0100753void gic_raise_softirq(const struct cpumask *mask, unsigned int irq)
Russell Kingf27ecac2005-08-18 21:31:00 +0100754{
Will Deacona803a8d2011-08-23 22:20:03 +0100755 int cpu;
756 unsigned long map = 0;
757
758 /* Convert our logical CPU mask into a physical one. */
759 for_each_cpu(cpu, mask)
760 map |= 1 << cpu_logical_map(cpu);
Russell Kingf27ecac2005-08-18 21:31:00 +0100761
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530762 /*
763 * Ensure that stores to Normal memory are visible to the
764 * other CPUs before issuing the IPI.
765 */
766 dsb();
767
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100768 /* this always happens on GIC0 */
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530769 writel_relaxed(map << 16 | irq, gic_data[0].dist_base + GIC_DIST_SOFTINT);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700770 mb();
Russell Kingf27ecac2005-08-18 21:31:00 +0100771}
772#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700773
774/* before calling this function the interrupts should be disabled
775 * and the irq must be disabled at gic to avoid spurious interrupts */
776bool gic_is_spi_pending(unsigned int irq)
777{
778 struct irq_data *d = irq_get_irq_data(irq);
779 struct gic_chip_data *gic_data = &gic_data[0];
780 u32 mask, val;
781
782 WARN_ON(!irqs_disabled());
Thomas Gleixner450ea482009-07-03 08:44:46 -0500783 raw_spin_lock(&irq_controller_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700784 mask = 1 << (gic_irq(d) % 32);
785 val = readl(gic_dist_base(d) +
786 GIC_DIST_ENABLE_SET + (gic_irq(d) / 32) * 4);
787 /* warn if the interrupt is enabled */
788 WARN_ON(val & mask);
789 val = readl(gic_dist_base(d) +
790 GIC_DIST_PENDING_SET + (gic_irq(d) / 32) * 4);
Thomas Gleixner450ea482009-07-03 08:44:46 -0500791 raw_spin_unlock(&irq_controller_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700792 return (bool) (val & mask);
793}
794
795/* before calling this function the interrupts should be disabled
796 * and the irq must be disabled at gic to avoid spurious interrupts */
797void gic_clear_spi_pending(unsigned int irq)
798{
799 struct gic_chip_data *gic_data = &gic_data[0];
800 struct irq_data *d = irq_get_irq_data(irq);
801
802 u32 mask, val;
803 WARN_ON(!irqs_disabled());
Thomas Gleixner450ea482009-07-03 08:44:46 -0500804 raw_spin_lock(&irq_controller_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700805 mask = 1 << (gic_irq(d) % 32);
806 val = readl(gic_dist_base(d) +
807 GIC_DIST_ENABLE_SET + (gic_irq(d) / 32) * 4);
808 /* warn if the interrupt is enabled */
809 WARN_ON(val & mask);
810 writel(mask, gic_dist_base(d) +
811 GIC_DIST_PENDING_CLEAR + (gic_irq(d) / 32) * 4);
Thomas Gleixner450ea482009-07-03 08:44:46 -0500812 raw_spin_unlock(&irq_controller_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700813}
Rob Herring0fc0d942011-09-28 21:27:52 -0500814#ifdef CONFIG_OF
815static int gic_cnt __initdata = 0;
816
817int __init gic_of_init(struct device_node *node, struct device_node *parent)
818{
819 void __iomem *cpu_base;
820 void __iomem *dist_base;
821 int irq;
822 struct irq_domain *domain = &gic_data[gic_cnt].domain;
823
824 if (WARN_ON(!node))
825 return -ENODEV;
826
827 dist_base = of_iomap(node, 0);
828 WARN(!dist_base, "unable to map gic dist registers\n");
829
830 cpu_base = of_iomap(node, 1);
831 WARN(!cpu_base, "unable to map gic cpu registers\n");
832
833 domain->of_node = of_node_get(node);
834
Rob Herring050113e2011-10-21 17:14:27 -0500835 gic_init(gic_cnt, -1, dist_base, cpu_base);
Rob Herring0fc0d942011-09-28 21:27:52 -0500836
837 if (parent) {
838 irq = irq_of_parse_and_map(node, 0);
839 gic_cascade_irq(gic_cnt, irq);
840 }
841 gic_cnt++;
842 return 0;
843}
844#endif