blob: d91d80176c980c78ff7bff46eecf3a3658122f3d [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 Herringf37a53c2011-10-21 17:14:27 -050027#include <linux/err.h>
Arnd Bergmann7e1efcf2011-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 Cross254056f2011-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>
Rob Herringb3f7ed02011-09-28 21:27:52 -050034#include <linux/of.h>
35#include <linux/of_address.h>
36#include <linux/of_irq.h>
Rob Herring4294f8b2011-09-28 21:25:31 -050037#include <linux/irqdomain.h>
Marc Zyngier292b2932011-07-20 16:24:14 +010038#include <linux/interrupt.h>
39#include <linux/percpu.h>
40#include <linux/slab.h>
Steve Mucklef132c6c2012-06-06 18:30:57 -070041#include <linux/syscore_ops.h>
Russell Kingf27ecac2005-08-18 21:31:00 +010042
43#include <asm/irq.h>
Marc Zyngier562e0022011-09-06 09:56:17 +010044#include <asm/exception.h>
Will Deaconeb504392012-01-20 12:01:12 +010045#include <asm/smp_plat.h>
Russell Kingf27ecac2005-08-18 21:31:00 +010046#include <asm/mach/irq.h>
47#include <asm/hardware/gic.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070048#include <asm/system.h>
Russell Kingf27ecac2005-08-18 21:31:00 +010049
Trilok Soniaf1709a2012-06-06 19:00:25 +053050#include <mach/socinfo.h>
51
Marc Zyngierdb0d4db2011-11-12 16:09:49 +000052union gic_base {
53 void __iomem *common_base;
54 void __percpu __iomem **percpu_base;
55};
56
57struct gic_chip_data {
Marc Zyngier680392b2011-11-12 16:09:49 +000058 unsigned int irq_offset;
Marc Zyngierdb0d4db2011-11-12 16:09:49 +000059 union gic_base dist_base;
60 union gic_base cpu_base;
Trilok Soniaf1709a2012-06-06 19:00:25 +053061 bool need_access_lock;
Marc Zyngierdb0d4db2011-11-12 16:09:49 +000062#ifdef CONFIG_CPU_PM
63 u32 saved_spi_enable[DIV_ROUND_UP(1020, 32)];
64 u32 saved_spi_conf[DIV_ROUND_UP(1020, 16)];
65 u32 saved_spi_target[DIV_ROUND_UP(1020, 4)];
Rohit Vaswani26e44862012-01-05 20:26:40 -080066 u32 saved_dist_pri[DIV_ROUND_UP(1020, 4)];
Marc Zyngierdb0d4db2011-11-12 16:09:49 +000067 u32 __percpu *saved_ppi_enable;
68 u32 __percpu *saved_ppi_conf;
69#endif
Michael Bohanbb6b30f2012-06-01 13:33:51 -070070 struct irq_domain *domain;
Marc Zyngierdb0d4db2011-11-12 16:09:49 +000071 unsigned int gic_irqs;
72#ifdef CONFIG_GIC_NON_BANKED
73 void __iomem *(*get_base)(union gic_base *);
74#endif
Steve Mucklef132c6c2012-06-06 18:30:57 -070075 unsigned int max_irq;
76#ifdef CONFIG_PM
77 unsigned int wakeup_irqs[32];
78 unsigned int enabled_irqs[32];
79#endif
Marc Zyngierdb0d4db2011-11-12 16:09:49 +000080};
81
Thomas Gleixnerbd31b852009-07-03 08:44:46 -050082static DEFINE_RAW_SPINLOCK(irq_controller_lock);
Russell Kingf27ecac2005-08-18 21:31:00 +010083
Rohit Vaswani26e44862012-01-05 20:26:40 -080084#ifdef CONFIG_CPU_PM
85static unsigned int saved_dist_ctrl, saved_cpu_ctrl;
86#endif
87
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +010088/*
89 * Supported arch specific GIC irq extension.
90 * Default make them NULL.
91 */
92struct irq_chip gic_arch_extn = {
Will Deacon1a017532011-02-09 12:01:12 +000093 .irq_eoi = NULL,
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +010094 .irq_mask = NULL,
95 .irq_unmask = NULL,
96 .irq_retrigger = NULL,
97 .irq_set_type = NULL,
98 .irq_set_wake = NULL,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070099 .irq_disable = NULL,
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100100};
101
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100102#ifndef MAX_GIC_NR
103#define MAX_GIC_NR 1
104#endif
105
Russell Kingbef8f9e2010-12-04 16:50:58 +0000106static struct gic_chip_data gic_data[MAX_GIC_NR] __read_mostly;
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100107
Marc Zyngierdb0d4db2011-11-12 16:09:49 +0000108#ifdef CONFIG_GIC_NON_BANKED
109static void __iomem *gic_get_percpu_base(union gic_base *base)
110{
111 return *__this_cpu_ptr(base->percpu_base);
112}
113
114static void __iomem *gic_get_common_base(union gic_base *base)
115{
116 return base->common_base;
117}
118
119static inline void __iomem *gic_data_dist_base(struct gic_chip_data *data)
120{
121 return data->get_base(&data->dist_base);
122}
123
124static inline void __iomem *gic_data_cpu_base(struct gic_chip_data *data)
125{
126 return data->get_base(&data->cpu_base);
127}
128
129static inline void gic_set_base_accessor(struct gic_chip_data *data,
130 void __iomem *(*f)(union gic_base *))
131{
132 data->get_base = f;
133}
134#else
135#define gic_data_dist_base(d) ((d)->dist_base.common_base)
136#define gic_data_cpu_base(d) ((d)->cpu_base.common_base)
137#define gic_set_base_accessor(d,f)
138#endif
139
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100140static inline void __iomem *gic_dist_base(struct irq_data *d)
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100141{
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100142 struct gic_chip_data *gic_data = irq_data_get_irq_chip_data(d);
Marc Zyngierdb0d4db2011-11-12 16:09:49 +0000143 return gic_data_dist_base(gic_data);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100144}
145
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100146static inline void __iomem *gic_cpu_base(struct irq_data *d)
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100147{
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100148 struct gic_chip_data *gic_data = irq_data_get_irq_chip_data(d);
Marc Zyngierdb0d4db2011-11-12 16:09:49 +0000149 return gic_data_cpu_base(gic_data);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100150}
151
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100152static inline unsigned int gic_irq(struct irq_data *d)
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100153{
Rob Herring4294f8b2011-09-28 21:25:31 -0500154 return d->hwirq;
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100155}
156
Taniya Dasb241bd82012-03-19 17:58:06 +0530157#if defined(CONFIG_CPU_V7) && defined(CONFIG_GIC_SECURE)
Rohit Vaswani26e44862012-01-05 20:26:40 -0800158static const inline bool is_cpu_secure(void)
159{
160 unsigned int dscr;
161
162 asm volatile ("mrc p14, 0, %0, c0, c1, 0" : "=r" (dscr));
163
164 /* BIT(18) - NS bit; 1 = NS; 0 = S */
165 if (BIT(18) & dscr)
166 return false;
167 else
168 return true;
169}
170#else
171static const inline bool is_cpu_secure(void)
172{
173 return false;
174}
175#endif
176
Russell Kingf27ecac2005-08-18 21:31:00 +0100177/*
178 * Routines to acknowledge, disable and enable interrupts
Russell Kingf27ecac2005-08-18 21:31:00 +0100179 */
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100180static void gic_mask_irq(struct irq_data *d)
Russell Kingf27ecac2005-08-18 21:31:00 +0100181{
Rob Herring4294f8b2011-09-28 21:25:31 -0500182 u32 mask = 1 << (gic_irq(d) % 32);
Thomas Gleixnerc4bfa282006-07-01 22:32:14 +0100183
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500184 raw_spin_lock(&irq_controller_lock);
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530185 writel_relaxed(mask, gic_dist_base(d) + GIC_DIST_ENABLE_CLEAR + (gic_irq(d) / 32) * 4);
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100186 if (gic_arch_extn.irq_mask)
187 gic_arch_extn.irq_mask(d);
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500188 raw_spin_unlock(&irq_controller_lock);
Russell Kingf27ecac2005-08-18 21:31:00 +0100189}
190
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100191static void gic_unmask_irq(struct irq_data *d)
Russell Kingf27ecac2005-08-18 21:31:00 +0100192{
Rob Herring4294f8b2011-09-28 21:25:31 -0500193 u32 mask = 1 << (gic_irq(d) % 32);
Thomas Gleixnerc4bfa282006-07-01 22:32:14 +0100194
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500195 raw_spin_lock(&irq_controller_lock);
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100196 if (gic_arch_extn.irq_unmask)
197 gic_arch_extn.irq_unmask(d);
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530198 writel_relaxed(mask, gic_dist_base(d) + GIC_DIST_ENABLE_SET + (gic_irq(d) / 32) * 4);
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500199 raw_spin_unlock(&irq_controller_lock);
Russell Kingf27ecac2005-08-18 21:31:00 +0100200}
201
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700202static void gic_disable_irq(struct irq_data *d)
203{
204 if (gic_arch_extn.irq_disable)
205 gic_arch_extn.irq_disable(d);
206}
207
208#ifdef CONFIG_PM
209static int gic_suspend_one(struct gic_chip_data *gic)
210{
211 unsigned int i;
Marc Zyngier680392b2011-11-12 16:09:49 +0000212 void __iomem *base = gic_data_dist_base(gic);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700213
214 for (i = 0; i * 32 < gic->max_irq; i++) {
215 gic->enabled_irqs[i]
216 = readl_relaxed(base + GIC_DIST_ENABLE_SET + i * 4);
217 /* disable all of them */
218 writel_relaxed(0xffffffff, base + GIC_DIST_ENABLE_CLEAR + i * 4);
219 /* enable the wakeup set */
220 writel_relaxed(gic->wakeup_irqs[i],
221 base + GIC_DIST_ENABLE_SET + i * 4);
222 }
223 mb();
224 return 0;
225}
226
227static int gic_suspend(void)
228{
229 int i;
230 for (i = 0; i < MAX_GIC_NR; i++)
231 gic_suspend_one(&gic_data[i]);
232 return 0;
233}
234
235extern int msm_show_resume_irq_mask;
236
237static void gic_show_resume_irq(struct gic_chip_data *gic)
238{
239 unsigned int i;
240 u32 enabled;
241 unsigned long pending[32];
Marc Zyngier680392b2011-11-12 16:09:49 +0000242 void __iomem *base = gic_data_dist_base(gic);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700243
244 if (!msm_show_resume_irq_mask)
245 return;
246
Thomas Gleixner450ea482009-07-03 08:44:46 -0500247 raw_spin_lock(&irq_controller_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700248 for (i = 0; i * 32 < gic->max_irq; i++) {
249 enabled = readl_relaxed(base + GIC_DIST_ENABLE_CLEAR + i * 4);
250 pending[i] = readl_relaxed(base + GIC_DIST_PENDING_SET + i * 4);
251 pending[i] &= enabled;
252 }
Trilok Soni1bf3f2d2012-05-26 11:58:59 +0530253 raw_spin_unlock(&irq_controller_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700254
255 for (i = find_first_bit(pending, gic->max_irq);
256 i < gic->max_irq;
257 i = find_next_bit(pending, gic->max_irq, i+1)) {
258 pr_warning("%s: %d triggered", __func__,
259 i + gic->irq_offset);
260 }
261}
262
263static void gic_resume_one(struct gic_chip_data *gic)
264{
265 unsigned int i;
Marc Zyngier680392b2011-11-12 16:09:49 +0000266 void __iomem *base = gic_data_dist_base(gic);
Trilok Soni1bf3f2d2012-05-26 11:58:59 +0530267
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700268 gic_show_resume_irq(gic);
269 for (i = 0; i * 32 < gic->max_irq; i++) {
270 /* disable all of them */
271 writel_relaxed(0xffffffff, base + GIC_DIST_ENABLE_CLEAR + i * 4);
272 /* enable the enabled set */
273 writel_relaxed(gic->enabled_irqs[i],
274 base + GIC_DIST_ENABLE_SET + i * 4);
275 }
276 mb();
277}
278
279static void gic_resume(void)
280{
281 int i;
282 for (i = 0; i < MAX_GIC_NR; i++)
283 gic_resume_one(&gic_data[i]);
284}
285
286static struct syscore_ops gic_syscore_ops = {
287 .suspend = gic_suspend,
288 .resume = gic_resume,
289};
290
291static int __init gic_init_sys(void)
292{
293 register_syscore_ops(&gic_syscore_ops);
294 return 0;
295}
296arch_initcall(gic_init_sys);
297
298#endif
299
Will Deacon1a017532011-02-09 12:01:12 +0000300static void gic_eoi_irq(struct irq_data *d)
301{
Trilok Soniaf1709a2012-06-06 19:00:25 +0530302 struct gic_chip_data *gic = irq_data_get_irq_chip_data(d);
303
Will Deacon1a017532011-02-09 12:01:12 +0000304 if (gic_arch_extn.irq_eoi) {
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500305 raw_spin_lock(&irq_controller_lock);
Will Deacon1a017532011-02-09 12:01:12 +0000306 gic_arch_extn.irq_eoi(d);
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500307 raw_spin_unlock(&irq_controller_lock);
Will Deacon1a017532011-02-09 12:01:12 +0000308 }
Trilok Soniaf1709a2012-06-06 19:00:25 +0530309
310 if (gic->need_access_lock)
311 raw_spin_lock(&irq_controller_lock);
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530312 writel_relaxed(gic_irq(d), gic_cpu_base(d) + GIC_CPU_EOI);
Trilok Soniaf1709a2012-06-06 19:00:25 +0530313 if (gic->need_access_lock)
314 raw_spin_unlock(&irq_controller_lock);
Will Deacon1a017532011-02-09 12:01:12 +0000315}
316
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100317static int gic_set_type(struct irq_data *d, unsigned int type)
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100318{
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100319 void __iomem *base = gic_dist_base(d);
320 unsigned int gicirq = gic_irq(d);
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100321 u32 enablemask = 1 << (gicirq % 32);
322 u32 enableoff = (gicirq / 32) * 4;
323 u32 confmask = 0x2 << ((gicirq % 16) * 2);
324 u32 confoff = (gicirq / 16) * 4;
325 bool enabled = false;
326 u32 val;
327
328 /* Interrupt configuration for SGIs can't be changed */
329 if (gicirq < 16)
330 return -EINVAL;
331
332 if (type != IRQ_TYPE_LEVEL_HIGH && type != IRQ_TYPE_EDGE_RISING)
333 return -EINVAL;
334
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500335 raw_spin_lock(&irq_controller_lock);
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100336
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100337 if (gic_arch_extn.irq_set_type)
338 gic_arch_extn.irq_set_type(d, type);
339
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530340 val = readl_relaxed(base + GIC_DIST_CONFIG + confoff);
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100341 if (type == IRQ_TYPE_LEVEL_HIGH)
342 val &= ~confmask;
343 else if (type == IRQ_TYPE_EDGE_RISING)
344 val |= confmask;
345
346 /*
347 * As recommended by the spec, disable the interrupt before changing
348 * the configuration
349 */
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530350 if (readl_relaxed(base + GIC_DIST_ENABLE_SET + enableoff) & enablemask) {
351 writel_relaxed(enablemask, base + GIC_DIST_ENABLE_CLEAR + enableoff);
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100352 enabled = true;
353 }
354
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530355 writel_relaxed(val, base + GIC_DIST_CONFIG + confoff);
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100356
357 if (enabled)
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530358 writel_relaxed(enablemask, base + GIC_DIST_ENABLE_SET + enableoff);
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100359
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500360 raw_spin_unlock(&irq_controller_lock);
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100361
362 return 0;
363}
364
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100365static int gic_retrigger(struct irq_data *d)
366{
367 if (gic_arch_extn.irq_retrigger)
368 return gic_arch_extn.irq_retrigger(d);
369
370 return -ENXIO;
371}
372
Catalin Marinasa06f5462005-09-30 16:07:05 +0100373#ifdef CONFIG_SMP
Russell Kingc1917892011-01-23 12:12:01 +0000374static int gic_set_affinity(struct irq_data *d, const struct cpumask *mask_val,
375 bool force)
Russell Kingf27ecac2005-08-18 21:31:00 +0100376{
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100377 void __iomem *reg = gic_dist_base(d) + GIC_DIST_TARGET + (gic_irq(d) & ~3);
Rob Herring4294f8b2011-09-28 21:25:31 -0500378 unsigned int shift = (gic_irq(d) % 4) * 8;
Russell King5dfc54e2011-07-21 15:00:57 +0100379 unsigned int cpu = cpumask_any_and(mask_val, cpu_online_mask);
Russell Kingc1917892011-01-23 12:12:01 +0000380 u32 val, mask, bit;
381
Russell King5dfc54e2011-07-21 15:00:57 +0100382 if (cpu >= 8 || cpu >= nr_cpu_ids)
Russell Kingc1917892011-01-23 12:12:01 +0000383 return -EINVAL;
384
385 mask = 0xff << shift;
Will Deacon267840f2011-08-23 22:20:03 +0100386 bit = 1 << (cpu_logical_map(cpu) + shift);
Russell Kingf27ecac2005-08-18 21:31:00 +0100387
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500388 raw_spin_lock(&irq_controller_lock);
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530389 val = readl_relaxed(reg) & ~mask;
390 writel_relaxed(val | bit, reg);
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500391 raw_spin_unlock(&irq_controller_lock);
Yinghai Lud5dedd42009-04-27 17:59:21 -0700392
Russell King5dfc54e2011-07-21 15:00:57 +0100393 return IRQ_SET_MASK_OK;
Russell Kingf27ecac2005-08-18 21:31:00 +0100394}
Catalin Marinasa06f5462005-09-30 16:07:05 +0100395#endif
Russell Kingf27ecac2005-08-18 21:31:00 +0100396
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100397#ifdef CONFIG_PM
398static int gic_set_wake(struct irq_data *d, unsigned int on)
399{
400 int ret = -ENXIO;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700401 unsigned int reg_offset, bit_offset;
402 unsigned int gicirq = gic_irq(d);
403 struct gic_chip_data *gic_data = irq_data_get_irq_chip_data(d);
404
405 /* per-cpu interrupts cannot be wakeup interrupts */
406 WARN_ON(gicirq < 32);
407
408 reg_offset = gicirq / 32;
409 bit_offset = gicirq % 32;
410
411 if (on)
412 gic_data->wakeup_irqs[reg_offset] |= 1 << bit_offset;
413 else
414 gic_data->wakeup_irqs[reg_offset] &= ~(1 << bit_offset);
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100415
416 if (gic_arch_extn.irq_set_wake)
417 ret = gic_arch_extn.irq_set_wake(d, on);
418
419 return ret;
420}
421
422#else
423#define gic_set_wake NULL
424#endif
425
Marc Zyngier562e0022011-09-06 09:56:17 +0100426asmlinkage void __exception_irq_entry gic_handle_irq(struct pt_regs *regs)
427{
428 u32 irqstat, irqnr;
429 struct gic_chip_data *gic = &gic_data[0];
430 void __iomem *cpu_base = gic_data_cpu_base(gic);
431
432 do {
Trilok Soniaf1709a2012-06-06 19:00:25 +0530433 if (gic->need_access_lock)
434 raw_spin_lock(&irq_controller_lock);
Marc Zyngier562e0022011-09-06 09:56:17 +0100435 irqstat = readl_relaxed(cpu_base + GIC_CPU_INTACK);
Trilok Soniaf1709a2012-06-06 19:00:25 +0530436 if (gic->need_access_lock)
437 raw_spin_unlock(&irq_controller_lock);
Marc Zyngier562e0022011-09-06 09:56:17 +0100438 irqnr = irqstat & ~0x1c00;
439
440 if (likely(irqnr > 15 && irqnr < 1021)) {
Michael Bohanbb6b30f2012-06-01 13:33:51 -0700441 irqnr = irq_find_mapping(gic->domain, irqnr);
Marc Zyngier562e0022011-09-06 09:56:17 +0100442 handle_IRQ(irqnr, regs);
443 continue;
444 }
445 if (irqnr < 16) {
Trilok Soniaf1709a2012-06-06 19:00:25 +0530446 if (gic->need_access_lock)
447 raw_spin_lock(&irq_controller_lock);
Marc Zyngier562e0022011-09-06 09:56:17 +0100448 writel_relaxed(irqstat, cpu_base + GIC_CPU_EOI);
Trilok Soniaf1709a2012-06-06 19:00:25 +0530449 if (gic->need_access_lock)
450 raw_spin_unlock(&irq_controller_lock);
Marc Zyngier562e0022011-09-06 09:56:17 +0100451#ifdef CONFIG_SMP
452 handle_IPI(irqnr, regs);
453#endif
454 continue;
455 }
456 break;
457 } while (1);
458}
459
Russell King0f347bb2007-05-17 10:11:34 +0100460static void gic_handle_cascade_irq(unsigned int irq, struct irq_desc *desc)
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100461{
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100462 struct gic_chip_data *chip_data = irq_get_handler_data(irq);
463 struct irq_chip *chip = irq_get_chip(irq);
Russell King0f347bb2007-05-17 10:11:34 +0100464 unsigned int cascade_irq, gic_irq;
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100465 unsigned long status;
466
Will Deacon1a017532011-02-09 12:01:12 +0000467 chained_irq_enter(chip, desc);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100468
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500469 raw_spin_lock(&irq_controller_lock);
Marc Zyngierdb0d4db2011-11-12 16:09:49 +0000470 status = readl_relaxed(gic_data_cpu_base(chip_data) + GIC_CPU_INTACK);
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500471 raw_spin_unlock(&irq_controller_lock);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100472
Russell King0f347bb2007-05-17 10:11:34 +0100473 gic_irq = (status & 0x3ff);
474 if (gic_irq == 1023)
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100475 goto out;
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100476
Michael Bohanbb6b30f2012-06-01 13:33:51 -0700477 cascade_irq = irq_find_mapping(chip_data->domain, gic_irq);
478 if (unlikely(gic_irq < 32 || gic_irq > 1020))
Russell King0f347bb2007-05-17 10:11:34 +0100479 do_bad_IRQ(cascade_irq, desc);
480 else
481 generic_handle_irq(cascade_irq);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100482
483 out:
Will Deacon1a017532011-02-09 12:01:12 +0000484 chained_irq_exit(chip, desc);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100485}
486
David Brownell38c677c2006-08-01 22:26:25 +0100487static struct irq_chip gic_chip = {
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100488 .name = "GIC",
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100489 .irq_mask = gic_mask_irq,
490 .irq_unmask = gic_unmask_irq,
Will Deacon1a017532011-02-09 12:01:12 +0000491 .irq_eoi = gic_eoi_irq,
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100492 .irq_set_type = gic_set_type,
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100493 .irq_retrigger = gic_retrigger,
Russell Kingf27ecac2005-08-18 21:31:00 +0100494#ifdef CONFIG_SMP
Russell Kingc1917892011-01-23 12:12:01 +0000495 .irq_set_affinity = gic_set_affinity,
Russell Kingf27ecac2005-08-18 21:31:00 +0100496#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700497 .irq_disable = gic_disable_irq,
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100498 .irq_set_wake = gic_set_wake,
Russell Kingf27ecac2005-08-18 21:31:00 +0100499};
500
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100501void __init gic_cascade_irq(unsigned int gic_nr, unsigned int irq)
502{
503 if (gic_nr >= MAX_GIC_NR)
504 BUG();
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100505 if (irq_set_handler_data(irq, &gic_data[gic_nr]) != 0)
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100506 BUG();
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100507 irq_set_chained_handler(irq, gic_handle_cascade_irq);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100508}
509
Rob Herring4294f8b2011-09-28 21:25:31 -0500510static void __init gic_dist_init(struct gic_chip_data *gic)
Russell Kingf27ecac2005-08-18 21:31:00 +0100511{
Michael Bohanbb6b30f2012-06-01 13:33:51 -0700512 unsigned int i;
Will Deacon267840f2011-08-23 22:20:03 +0100513 u32 cpumask;
Rob Herring4294f8b2011-09-28 21:25:31 -0500514 unsigned int gic_irqs = gic->gic_irqs;
Marc Zyngierdb0d4db2011-11-12 16:09:49 +0000515 void __iomem *base = gic_data_dist_base(gic);
Will Deaconeb504392012-01-20 12:01:12 +0100516 u32 cpu = cpu_logical_map(smp_processor_id());
Will Deacon267840f2011-08-23 22:20:03 +0100517
518 cpumask = 1 << cpu;
Russell Kingf27ecac2005-08-18 21:31:00 +0100519 cpumask |= cpumask << 8;
520 cpumask |= cpumask << 16;
521
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530522 writel_relaxed(0, base + GIC_DIST_CTRL);
Russell Kingf27ecac2005-08-18 21:31:00 +0100523
524 /*
Russell Kingf27ecac2005-08-18 21:31:00 +0100525 * Set all global interrupts to be level triggered, active low.
526 */
Pawel Molle6afec92010-11-26 13:45:43 +0100527 for (i = 32; i < gic_irqs; i += 16)
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530528 writel_relaxed(0, base + GIC_DIST_CONFIG + i * 4 / 16);
Russell Kingf27ecac2005-08-18 21:31:00 +0100529
530 /*
531 * Set all global interrupts to this CPU only.
532 */
Pawel Molle6afec92010-11-26 13:45:43 +0100533 for (i = 32; i < gic_irqs; i += 4)
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530534 writel_relaxed(cpumask, base + GIC_DIST_TARGET + i * 4 / 4);
Russell Kingf27ecac2005-08-18 21:31:00 +0100535
536 /*
Rohit Vaswani26e44862012-01-05 20:26:40 -0800537 * Set NS/S.
538 */
539 if (is_cpu_secure())
540 for (i = 32; i < gic_irqs; i += 32)
541 writel_relaxed(0xFFFFFFFF,
542 base + GIC_DIST_ISR + i * 4 / 32);
543
544 /*
Russell King9395f6e2010-11-11 23:10:30 +0000545 * Set priority on all global interrupts.
Russell Kingf27ecac2005-08-18 21:31:00 +0100546 */
Pawel Molle6afec92010-11-26 13:45:43 +0100547 for (i = 32; i < gic_irqs; i += 4)
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530548 writel_relaxed(0xa0a0a0a0, base + GIC_DIST_PRI + i * 4 / 4);
Russell Kingf27ecac2005-08-18 21:31:00 +0100549
550 /*
Russell King9395f6e2010-11-11 23:10:30 +0000551 * Disable all interrupts. Leave the PPI and SGIs alone
552 * as these enables are banked registers.
Russell Kingf27ecac2005-08-18 21:31:00 +0100553 */
Pawel Molle6afec92010-11-26 13:45:43 +0100554 for (i = 32; i < gic_irqs; i += 32)
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530555 writel_relaxed(0xffffffff, base + GIC_DIST_ENABLE_CLEAR + i * 4 / 32);
Russell Kingf27ecac2005-08-18 21:31:00 +0100556
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700557 gic->max_irq = gic_irqs;
558
Rohit Vaswani26e44862012-01-05 20:26:40 -0800559 if (is_cpu_secure())
560 writel_relaxed(3, base + GIC_DIST_CTRL);
561 else
562 writel_relaxed(1, base + GIC_DIST_CTRL);
563
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700564 mb();
Russell Kingf27ecac2005-08-18 21:31:00 +0100565}
566
Russell Kingbef8f9e2010-12-04 16:50:58 +0000567static void __cpuinit gic_cpu_init(struct gic_chip_data *gic)
Russell Kingf27ecac2005-08-18 21:31:00 +0100568{
Marc Zyngierdb0d4db2011-11-12 16:09:49 +0000569 void __iomem *dist_base = gic_data_dist_base(gic);
570 void __iomem *base = gic_data_cpu_base(gic);
Russell King9395f6e2010-11-11 23:10:30 +0000571 int i;
572
Russell King9395f6e2010-11-11 23:10:30 +0000573 /*
574 * Deal with the banked PPI and SGI interrupts - disable all
575 * PPI interrupts, ensure all SGI interrupts are enabled.
576 */
Trilok Soniaf1709a2012-06-06 19:00:25 +0530577 if (gic->need_access_lock)
578 raw_spin_lock(&irq_controller_lock);
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530579 writel_relaxed(0xffff0000, dist_base + GIC_DIST_ENABLE_CLEAR);
580 writel_relaxed(0x0000ffff, dist_base + GIC_DIST_ENABLE_SET);
Russell King9395f6e2010-11-11 23:10:30 +0000581
Rohit Vaswani26e44862012-01-05 20:26:40 -0800582 /* Set NS/S */
583 if (is_cpu_secure())
584 writel_relaxed(0xFFFFFFFF, dist_base + GIC_DIST_ISR);
585
Russell King9395f6e2010-11-11 23:10:30 +0000586 /*
587 * Set priority on PPI and SGI interrupts
588 */
589 for (i = 0; i < 32; i += 4)
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530590 writel_relaxed(0xa0a0a0a0, dist_base + GIC_DIST_PRI + i * 4 / 4);
Russell King9395f6e2010-11-11 23:10:30 +0000591
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530592 writel_relaxed(0xf0, base + GIC_CPU_PRIMASK);
Rohit Vaswani26e44862012-01-05 20:26:40 -0800593
594 if (is_cpu_secure())
595 writel_relaxed(0xF, base + GIC_CPU_CTRL);
596 else
597 writel_relaxed(1, base + GIC_CPU_CTRL);
Trilok Soniaf1709a2012-06-06 19:00:25 +0530598 if (gic->need_access_lock)
599 raw_spin_unlock(&irq_controller_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700600 mb();
Russell Kingf27ecac2005-08-18 21:31:00 +0100601}
602
Colin Cross254056f2011-02-10 12:54:10 -0800603#ifdef CONFIG_CPU_PM
604/*
605 * Saves the GIC distributor registers during suspend or idle. Must be called
606 * with interrupts disabled but before powering down the GIC. After calling
607 * this function, no interrupts will be delivered by the GIC, and another
608 * platform-specific wakeup source must be enabled.
609 */
610static void gic_dist_save(unsigned int gic_nr)
611{
612 unsigned int gic_irqs;
613 void __iomem *dist_base;
614 int i;
615
616 if (gic_nr >= MAX_GIC_NR)
617 BUG();
618
619 gic_irqs = gic_data[gic_nr].gic_irqs;
Marc Zyngierdb0d4db2011-11-12 16:09:49 +0000620 dist_base = gic_data_dist_base(&gic_data[gic_nr]);
Colin Cross254056f2011-02-10 12:54:10 -0800621
622 if (!dist_base)
623 return;
624
Rohit Vaswani26e44862012-01-05 20:26:40 -0800625 saved_dist_ctrl = readl_relaxed(dist_base + GIC_DIST_CTRL);
626
Colin Cross254056f2011-02-10 12:54:10 -0800627 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 16); i++)
628 gic_data[gic_nr].saved_spi_conf[i] =
629 readl_relaxed(dist_base + GIC_DIST_CONFIG + i * 4);
630
631 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 4); i++)
632 gic_data[gic_nr].saved_spi_target[i] =
633 readl_relaxed(dist_base + GIC_DIST_TARGET + i * 4);
634
Rohit Vaswani26e44862012-01-05 20:26:40 -0800635 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 4); i++)
636 gic_data[gic_nr].saved_dist_pri[i] =
637 readl_relaxed(dist_base + GIC_DIST_PRI + i * 4);
638
Colin Cross254056f2011-02-10 12:54:10 -0800639 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 32); i++)
640 gic_data[gic_nr].saved_spi_enable[i] =
641 readl_relaxed(dist_base + GIC_DIST_ENABLE_SET + i * 4);
642}
643
644/*
645 * Restores the GIC distributor registers during resume or when coming out of
646 * idle. Must be called before enabling interrupts. If a level interrupt
647 * that occured while the GIC was suspended is still present, it will be
648 * handled normally, but any edge interrupts that occured will not be seen by
649 * the GIC and need to be handled by the platform-specific wakeup source.
650 */
651static void gic_dist_restore(unsigned int gic_nr)
652{
653 unsigned int gic_irqs;
654 unsigned int i;
655 void __iomem *dist_base;
656
657 if (gic_nr >= MAX_GIC_NR)
658 BUG();
659
660 gic_irqs = gic_data[gic_nr].gic_irqs;
Marc Zyngierdb0d4db2011-11-12 16:09:49 +0000661 dist_base = gic_data_dist_base(&gic_data[gic_nr]);
Colin Cross254056f2011-02-10 12:54:10 -0800662
663 if (!dist_base)
664 return;
665
666 writel_relaxed(0, dist_base + GIC_DIST_CTRL);
667
668 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 16); i++)
669 writel_relaxed(gic_data[gic_nr].saved_spi_conf[i],
670 dist_base + GIC_DIST_CONFIG + i * 4);
671
672 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 4); i++)
Rohit Vaswani26e44862012-01-05 20:26:40 -0800673 writel_relaxed(gic_data[gic_nr].saved_dist_pri[i],
Colin Cross254056f2011-02-10 12:54:10 -0800674 dist_base + GIC_DIST_PRI + i * 4);
675
676 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 4); i++)
677 writel_relaxed(gic_data[gic_nr].saved_spi_target[i],
678 dist_base + GIC_DIST_TARGET + i * 4);
679
680 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 32); i++)
681 writel_relaxed(gic_data[gic_nr].saved_spi_enable[i],
682 dist_base + GIC_DIST_ENABLE_SET + i * 4);
683
Rohit Vaswani26e44862012-01-05 20:26:40 -0800684 writel_relaxed(saved_dist_ctrl, dist_base + GIC_DIST_CTRL);
Colin Cross254056f2011-02-10 12:54:10 -0800685}
686
687static void gic_cpu_save(unsigned int gic_nr)
688{
689 int i;
690 u32 *ptr;
691 void __iomem *dist_base;
692 void __iomem *cpu_base;
693
694 if (gic_nr >= MAX_GIC_NR)
695 BUG();
696
Marc Zyngierdb0d4db2011-11-12 16:09:49 +0000697 dist_base = gic_data_dist_base(&gic_data[gic_nr]);
698 cpu_base = gic_data_cpu_base(&gic_data[gic_nr]);
Colin Cross254056f2011-02-10 12:54:10 -0800699
700 if (!dist_base || !cpu_base)
701 return;
702
Rohit Vaswani26e44862012-01-05 20:26:40 -0800703 saved_cpu_ctrl = readl_relaxed(cpu_base + GIC_CPU_CTRL);
704
705 for (i = 0; i < DIV_ROUND_UP(32, 4); i++)
706 gic_data[gic_nr].saved_dist_pri[i] = readl_relaxed(dist_base +
707 GIC_DIST_PRI + i * 4);
708
Colin Cross254056f2011-02-10 12:54:10 -0800709 ptr = __this_cpu_ptr(gic_data[gic_nr].saved_ppi_enable);
710 for (i = 0; i < DIV_ROUND_UP(32, 32); i++)
711 ptr[i] = readl_relaxed(dist_base + GIC_DIST_ENABLE_SET + i * 4);
712
713 ptr = __this_cpu_ptr(gic_data[gic_nr].saved_ppi_conf);
714 for (i = 0; i < DIV_ROUND_UP(32, 16); i++)
715 ptr[i] = readl_relaxed(dist_base + GIC_DIST_CONFIG + i * 4);
716
717}
718
719static void gic_cpu_restore(unsigned int gic_nr)
720{
721 int i;
722 u32 *ptr;
723 void __iomem *dist_base;
724 void __iomem *cpu_base;
725
726 if (gic_nr >= MAX_GIC_NR)
727 BUG();
728
Marc Zyngierdb0d4db2011-11-12 16:09:49 +0000729 dist_base = gic_data_dist_base(&gic_data[gic_nr]);
730 cpu_base = gic_data_cpu_base(&gic_data[gic_nr]);
Colin Cross254056f2011-02-10 12:54:10 -0800731
732 if (!dist_base || !cpu_base)
733 return;
734
735 ptr = __this_cpu_ptr(gic_data[gic_nr].saved_ppi_enable);
736 for (i = 0; i < DIV_ROUND_UP(32, 32); i++)
737 writel_relaxed(ptr[i], dist_base + GIC_DIST_ENABLE_SET + i * 4);
738
739 ptr = __this_cpu_ptr(gic_data[gic_nr].saved_ppi_conf);
740 for (i = 0; i < DIV_ROUND_UP(32, 16); i++)
741 writel_relaxed(ptr[i], dist_base + GIC_DIST_CONFIG + i * 4);
742
743 for (i = 0; i < DIV_ROUND_UP(32, 4); i++)
Rohit Vaswani26e44862012-01-05 20:26:40 -0800744 writel_relaxed(gic_data[gic_nr].saved_dist_pri[i],
745 dist_base + GIC_DIST_PRI + i * 4);
Colin Cross254056f2011-02-10 12:54:10 -0800746
747 writel_relaxed(0xf0, cpu_base + GIC_CPU_PRIMASK);
Rohit Vaswani26e44862012-01-05 20:26:40 -0800748 writel_relaxed(saved_cpu_ctrl, cpu_base + GIC_CPU_CTRL);
Colin Cross254056f2011-02-10 12:54:10 -0800749}
750
751static int gic_notifier(struct notifier_block *self, unsigned long cmd, void *v)
752{
753 int i;
754
755 for (i = 0; i < MAX_GIC_NR; i++) {
Marc Zyngierdb0d4db2011-11-12 16:09:49 +0000756#ifdef CONFIG_GIC_NON_BANKED
757 /* Skip over unused GICs */
758 if (!gic_data[i].get_base)
759 continue;
760#endif
Colin Cross254056f2011-02-10 12:54:10 -0800761 switch (cmd) {
762 case CPU_PM_ENTER:
763 gic_cpu_save(i);
764 break;
765 case CPU_PM_ENTER_FAILED:
766 case CPU_PM_EXIT:
767 gic_cpu_restore(i);
768 break;
769 case CPU_CLUSTER_PM_ENTER:
770 gic_dist_save(i);
771 break;
772 case CPU_CLUSTER_PM_ENTER_FAILED:
773 case CPU_CLUSTER_PM_EXIT:
774 gic_dist_restore(i);
775 break;
776 }
777 }
778
779 return NOTIFY_OK;
780}
781
782static struct notifier_block gic_notifier_block = {
783 .notifier_call = gic_notifier,
784};
785
786static void __init gic_pm_init(struct gic_chip_data *gic)
787{
788 gic->saved_ppi_enable = __alloc_percpu(DIV_ROUND_UP(32, 32) * 4,
789 sizeof(u32));
790 BUG_ON(!gic->saved_ppi_enable);
791
792 gic->saved_ppi_conf = __alloc_percpu(DIV_ROUND_UP(32, 16) * 4,
793 sizeof(u32));
794 BUG_ON(!gic->saved_ppi_conf);
795
Marc Zyngierabdd7b92011-11-25 17:58:19 +0100796 if (gic == &gic_data[0])
797 cpu_pm_register_notifier(&gic_notifier_block);
Colin Cross254056f2011-02-10 12:54:10 -0800798}
799#else
800static void __init gic_pm_init(struct gic_chip_data *gic)
801{
802}
Trilok Soni38501052012-06-07 18:55:37 +0530803
804static void gic_cpu_restore(unsigned int gic_nr)
805{
806}
807
808static void gic_cpu_save(unsigned int gic_nr)
809{
810}
811
812static void gic_dist_restore(unsigned int gic_nr)
813{
814}
815
816static void gic_dist_save(unsigned int gic_nr)
817{
818}
Colin Cross254056f2011-02-10 12:54:10 -0800819#endif
820
Michael Bohanbb6b30f2012-06-01 13:33:51 -0700821static int gic_irq_domain_map(struct irq_domain *d, unsigned int irq,
822 irq_hw_number_t hw)
823{
824 if (hw < 32) {
825 irq_set_percpu_devid(irq);
826 irq_set_chip_and_handler(irq, &gic_chip,
827 handle_percpu_devid_irq);
828 set_irq_flags(irq, IRQF_VALID | IRQF_NOAUTOEN);
829 } else {
830 irq_set_chip_and_handler(irq, &gic_chip,
831 handle_fasteoi_irq);
832 set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
833 }
834 irq_set_chip_data(irq, d->host_data);
835 return 0;
836}
837
838static int gic_irq_domain_xlate(struct irq_domain *d,
839 struct device_node *controller,
840 const u32 *intspec, unsigned int intsize,
841 unsigned long *out_hwirq, unsigned int *out_type)
Rob Herringb3f7ed02011-09-28 21:27:52 -0500842{
843 if (d->of_node != controller)
844 return -EINVAL;
845 if (intsize < 3)
846 return -EINVAL;
847
848 /* Get the interrupt number and add 16 to skip over SGIs */
849 *out_hwirq = intspec[1] + 16;
850
851 /* For SPIs, we need to add 16 more to get the GIC irq ID number */
852 if (!intspec[0])
853 *out_hwirq += 16;
854
855 *out_type = intspec[2] & IRQ_TYPE_SENSE_MASK;
856 return 0;
857}
Rob Herringb3f7ed02011-09-28 21:27:52 -0500858
Grant Likely15a25982012-01-26 12:25:18 -0700859const struct irq_domain_ops gic_irq_domain_ops = {
Michael Bohanbb6b30f2012-06-01 13:33:51 -0700860 .map = gic_irq_domain_map,
861 .xlate = gic_irq_domain_xlate,
Rob Herring4294f8b2011-09-28 21:25:31 -0500862};
863
Marc Zyngierdb0d4db2011-11-12 16:09:49 +0000864void __init gic_init_bases(unsigned int gic_nr, int irq_start,
865 void __iomem *dist_base, void __iomem *cpu_base,
Michael Bohanbb6b30f2012-06-01 13:33:51 -0700866 u32 percpu_offset, struct device_node *node)
Russell Kingb580b892010-12-04 15:55:14 +0000867{
Michael Bohanbb6b30f2012-06-01 13:33:51 -0700868 irq_hw_number_t hwirq_base;
Russell Kingbef8f9e2010-12-04 16:50:58 +0000869 struct gic_chip_data *gic;
Michael Bohanbb6b30f2012-06-01 13:33:51 -0700870 int gic_irqs, irq_base;
Russell Kingbef8f9e2010-12-04 16:50:58 +0000871
872 BUG_ON(gic_nr >= MAX_GIC_NR);
873
874 gic = &gic_data[gic_nr];
Trilok Soniaf1709a2012-06-06 19:00:25 +0530875 if (cpu_is_msm8625() &&
876 (SOCINFO_VERSION_MAJOR(socinfo_get_version()) <= 1))
877 gic->need_access_lock = true;
878
Marc Zyngierdb0d4db2011-11-12 16:09:49 +0000879#ifdef CONFIG_GIC_NON_BANKED
880 if (percpu_offset) { /* Frankein-GIC without banked registers... */
881 unsigned int cpu;
882
883 gic->dist_base.percpu_base = alloc_percpu(void __iomem *);
884 gic->cpu_base.percpu_base = alloc_percpu(void __iomem *);
885 if (WARN_ON(!gic->dist_base.percpu_base ||
Michael Bohanbb6b30f2012-06-01 13:33:51 -0700886 !gic->cpu_base.percpu_base)) {
887 free_percpu(gic->dist_base.percpu_base);
888 free_percpu(gic->cpu_base.percpu_base);
889 return;
890 }
Marc Zyngierdb0d4db2011-11-12 16:09:49 +0000891
892 for_each_possible_cpu(cpu) {
893 unsigned long offset = percpu_offset * cpu_logical_map(cpu);
894 *per_cpu_ptr(gic->dist_base.percpu_base, cpu) = dist_base + offset;
895 *per_cpu_ptr(gic->cpu_base.percpu_base, cpu) = cpu_base + offset;
896 }
897
898 gic_set_base_accessor(gic, gic_get_percpu_base);
899 } else
900#endif
901 { /* Normal, sane GIC... */
902 WARN(percpu_offset,
903 "GIC_NON_BANKED not enabled, ignoring %08x offset!",
904 percpu_offset);
905 gic->dist_base.common_base = dist_base;
906 gic->cpu_base.common_base = cpu_base;
907 gic_set_base_accessor(gic, gic_get_common_base);
908 }
Russell Kingbef8f9e2010-12-04 16:50:58 +0000909
Rob Herring4294f8b2011-09-28 21:25:31 -0500910 /*
911 * For primary GICs, skip over SGIs.
912 * For secondary GICs, skip over PPIs, too.
913 */
Michael Bohanbb6b30f2012-06-01 13:33:51 -0700914 if (gic_nr == 0 && (irq_start & 31) > 0) {
915 hwirq_base = 16;
916 if (irq_start != -1)
917 irq_start = (irq_start & ~31) + 16;
918 } else {
919 hwirq_base = 32;
Will Deaconfe41db72011-11-25 19:23:36 +0100920 }
Rob Herring4294f8b2011-09-28 21:25:31 -0500921
922 /*
923 * Find out how many interrupts are supported.
924 * The GIC only supports up to 1020 interrupt sources.
925 */
Marc Zyngierdb0d4db2011-11-12 16:09:49 +0000926 gic_irqs = readl_relaxed(gic_data_dist_base(gic) + GIC_DIST_CTR) & 0x1f;
Rob Herring4294f8b2011-09-28 21:25:31 -0500927 gic_irqs = (gic_irqs + 1) * 32;
928 if (gic_irqs > 1020)
929 gic_irqs = 1020;
930 gic->gic_irqs = gic_irqs;
931
Michael Bohanbb6b30f2012-06-01 13:33:51 -0700932 gic_irqs -= hwirq_base; /* calculate # of irqs to allocate */
933 irq_base = irq_alloc_descs(irq_start, 16, gic_irqs, numa_node_id());
934 if (IS_ERR_VALUE(irq_base)) {
Rob Herringf37a53c2011-10-21 17:14:27 -0500935 WARN(1, "Cannot allocate irq_descs @ IRQ%d, assuming pre-allocated\n",
936 irq_start);
Michael Bohanbb6b30f2012-06-01 13:33:51 -0700937 irq_base = irq_start;
Rob Herringf37a53c2011-10-21 17:14:27 -0500938 }
Michael Bohanbb6b30f2012-06-01 13:33:51 -0700939 gic->domain = irq_domain_add_legacy(node, gic_irqs, irq_base,
940 hwirq_base, &gic_irq_domain_ops, gic);
941 if (WARN_ON(!gic->domain))
942 return;
Russell Kingbef8f9e2010-12-04 16:50:58 +0000943
Colin Cross9c128452011-06-13 00:45:59 +0000944 gic_chip.flags |= gic_arch_extn.flags;
Rob Herring4294f8b2011-09-28 21:25:31 -0500945 gic_dist_init(gic);
Russell Kingbef8f9e2010-12-04 16:50:58 +0000946 gic_cpu_init(gic);
Colin Cross254056f2011-02-10 12:54:10 -0800947 gic_pm_init(gic);
Russell Kingb580b892010-12-04 15:55:14 +0000948}
949
Russell King38489532010-12-04 16:01:03 +0000950void __cpuinit gic_secondary_init(unsigned int gic_nr)
951{
Russell Kingbef8f9e2010-12-04 16:50:58 +0000952 BUG_ON(gic_nr >= MAX_GIC_NR);
953
954 gic_cpu_init(&gic_data[gic_nr]);
Russell King38489532010-12-04 16:01:03 +0000955}
956
Russell Kingf27ecac2005-08-18 21:31:00 +0100957#ifdef CONFIG_SMP
Russell King82668102009-05-17 16:20:18 +0100958void gic_raise_softirq(const struct cpumask *mask, unsigned int irq)
Russell Kingf27ecac2005-08-18 21:31:00 +0100959{
Will Deacon267840f2011-08-23 22:20:03 +0100960 int cpu;
Rohit Vaswani26e44862012-01-05 20:26:40 -0800961 unsigned long sgir;
Will Deacon267840f2011-08-23 22:20:03 +0100962 unsigned long map = 0;
Trilok Soniaf1709a2012-06-06 19:00:25 +0530963 unsigned long flags = 0;
964 struct gic_chip_data *gic = &gic_data[0];
Will Deacon267840f2011-08-23 22:20:03 +0100965
966 /* Convert our logical CPU mask into a physical one. */
967 for_each_cpu(cpu, mask)
968 map |= 1 << cpu_logical_map(cpu);
Russell Kingf27ecac2005-08-18 21:31:00 +0100969
Rohit Vaswani26e44862012-01-05 20:26:40 -0800970 sgir = (map << 16) | irq;
971 if (is_cpu_secure())
972 sgir |= (1 << 15);
973
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530974 /*
975 * Ensure that stores to Normal memory are visible to the
976 * other CPUs before issuing the IPI.
977 */
978 dsb();
979
Trilok Soniaf1709a2012-06-06 19:00:25 +0530980 if (gic->need_access_lock)
981 raw_spin_lock_irqsave(&irq_controller_lock, flags);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100982 /* this always happens on GIC0 */
Trilok Soniaf1709a2012-06-06 19:00:25 +0530983 writel_relaxed(sgir, gic_data_dist_base(gic) + GIC_DIST_SOFTINT);
984 if (gic->need_access_lock)
985 raw_spin_unlock_irqrestore(&irq_controller_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700986 mb();
Russell Kingf27ecac2005-08-18 21:31:00 +0100987}
988#endif
Rob Herringb3f7ed02011-09-28 21:27:52 -0500989
Rohit Vaswani26e44862012-01-05 20:26:40 -0800990void gic_set_irq_secure(unsigned int irq)
991{
992 unsigned int gicd_isr_reg, gicd_pri_reg;
993 unsigned int mask = 0xFFFFFF00;
994 struct gic_chip_data *gic_data = &gic_data[0];
995 struct irq_data *d = irq_get_irq_data(irq);
996
997 if (is_cpu_secure()) {
998 raw_spin_lock(&irq_controller_lock);
999 gicd_isr_reg = readl_relaxed(gic_dist_base(d) +
1000 GIC_DIST_ISR + gic_irq(d) / 32 * 4);
1001 gicd_isr_reg &= ~BIT(gic_irq(d) % 32);
1002 writel_relaxed(gicd_isr_reg, gic_dist_base(d) +
1003 GIC_DIST_ISR + gic_irq(d) / 32 * 4);
1004 /* Also increase the priority of that irq */
1005 gicd_pri_reg = readl_relaxed(gic_dist_base(d) +
1006 GIC_DIST_PRI + (gic_irq(d) * 4 / 4));
1007 gicd_pri_reg &= mask;
1008 gicd_pri_reg |= 0x80; /* Priority of 0x80 > 0xA0 */
1009 writel_relaxed(gicd_pri_reg, gic_dist_base(d) + GIC_DIST_PRI +
1010 gic_irq(d) * 4 / 4);
1011 mb();
1012 raw_spin_unlock(&irq_controller_lock);
1013 } else {
1014 WARN(1, "Trying to run secure operation from Non-secure mode");
1015 }
1016}
1017
Rob Herringb3f7ed02011-09-28 21:27:52 -05001018#ifdef CONFIG_OF
1019static int gic_cnt __initdata = 0;
1020
1021int __init gic_of_init(struct device_node *node, struct device_node *parent)
1022{
1023 void __iomem *cpu_base;
1024 void __iomem *dist_base;
Marc Zyngierdb0d4db2011-11-12 16:09:49 +00001025 u32 percpu_offset;
Rob Herringb3f7ed02011-09-28 21:27:52 -05001026 int irq;
Rob Herringb3f7ed02011-09-28 21:27:52 -05001027
1028 if (WARN_ON(!node))
1029 return -ENODEV;
1030
1031 dist_base = of_iomap(node, 0);
1032 WARN(!dist_base, "unable to map gic dist registers\n");
1033
1034 cpu_base = of_iomap(node, 1);
1035 WARN(!cpu_base, "unable to map gic cpu registers\n");
1036
Marc Zyngierdb0d4db2011-11-12 16:09:49 +00001037 if (of_property_read_u32(node, "cpu-offset", &percpu_offset))
1038 percpu_offset = 0;
1039
Michael Bohanbb6b30f2012-06-01 13:33:51 -07001040 gic_init_bases(gic_cnt, -1, dist_base, cpu_base, percpu_offset, node);
Rob Herringb3f7ed02011-09-28 21:27:52 -05001041
1042 if (parent) {
1043 irq = irq_of_parse_and_map(node, 0);
1044 gic_cascade_irq(gic_cnt, irq);
1045 }
1046 gic_cnt++;
1047 return 0;
1048}
1049#endif
Trilok Soni01dbb612012-05-28 19:23:53 +05301050/*
1051 * Before calling this function the interrupts should be disabled
1052 * and the irq must be disabled at gic to avoid spurious interrupts
1053 */
1054bool gic_is_irq_pending(unsigned int irq)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001055{
1056 struct irq_data *d = irq_get_irq_data(irq);
1057 struct gic_chip_data *gic_data = &gic_data[0];
1058 u32 mask, val;
1059
1060 WARN_ON(!irqs_disabled());
Thomas Gleixner450ea482009-07-03 08:44:46 -05001061 raw_spin_lock(&irq_controller_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001062 mask = 1 << (gic_irq(d) % 32);
1063 val = readl(gic_dist_base(d) +
1064 GIC_DIST_ENABLE_SET + (gic_irq(d) / 32) * 4);
1065 /* warn if the interrupt is enabled */
1066 WARN_ON(val & mask);
1067 val = readl(gic_dist_base(d) +
1068 GIC_DIST_PENDING_SET + (gic_irq(d) / 32) * 4);
Thomas Gleixner450ea482009-07-03 08:44:46 -05001069 raw_spin_unlock(&irq_controller_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001070 return (bool) (val & mask);
1071}
1072
Trilok Soni01dbb612012-05-28 19:23:53 +05301073/*
1074 * Before calling this function the interrupts should be disabled
1075 * and the irq must be disabled at gic to avoid spurious interrupts
1076 */
1077void gic_clear_irq_pending(unsigned int irq)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001078{
1079 struct gic_chip_data *gic_data = &gic_data[0];
1080 struct irq_data *d = irq_get_irq_data(irq);
1081
1082 u32 mask, val;
1083 WARN_ON(!irqs_disabled());
Thomas Gleixner450ea482009-07-03 08:44:46 -05001084 raw_spin_lock(&irq_controller_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001085 mask = 1 << (gic_irq(d) % 32);
1086 val = readl(gic_dist_base(d) +
1087 GIC_DIST_ENABLE_SET + (gic_irq(d) / 32) * 4);
1088 /* warn if the interrupt is enabled */
1089 WARN_ON(val & mask);
1090 writel(mask, gic_dist_base(d) +
1091 GIC_DIST_PENDING_CLEAR + (gic_irq(d) / 32) * 4);
Thomas Gleixner450ea482009-07-03 08:44:46 -05001092 raw_spin_unlock(&irq_controller_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001093}
Rob Herring0fc0d942011-09-28 21:27:52 -05001094
Taniya Dasbc9248a2012-04-30 19:59:11 +05301095#ifdef CONFIG_ARCH_MSM8625
1096 /*
1097 * Check for any interrupts which are enabled are pending
1098 * in the pending set or not.
1099 * Return :
1100 * 0 : No pending interrupts
1101 * 1 : Pending interrupts other than A9_M2A_5
1102 */
1103unsigned int msm_gic_spi_ppi_pending(void)
1104{
1105 unsigned int i, bit = 0;
1106 unsigned int pending_enb = 0, pending = 0;
1107 unsigned long value = 0;
1108 struct gic_chip_data *gic = &gic_data[0];
1109 void __iomem *base = gic_data_dist_base(gic);
Trilok Soni6278db02012-05-20 01:29:52 +05301110 unsigned long flags;
Taniya Dasbc9248a2012-04-30 19:59:11 +05301111
Trilok Soni6278db02012-05-20 01:29:52 +05301112 raw_spin_lock_irqsave(&irq_controller_lock, flags);
Taniya Dasbc9248a2012-04-30 19:59:11 +05301113 /*
1114 * PPI and SGI to be included.
1115 * MSM8625_INT_A9_M2A_5 needs to be ignored, as A9_M2A_5
1116 * requesting sleep triggers it
1117 */
1118 for (i = 0; (i * 32) < gic->max_irq; i++) {
1119 pending = readl_relaxed(base +
1120 GIC_DIST_PENDING_SET + i * 4);
1121 pending_enb = readl_relaxed(base +
1122 GIC_DIST_ENABLE_SET + i * 4);
1123 value = pending & pending_enb;
1124
1125 if (value) {
1126 for (bit = 0; bit < 32; bit++) {
1127 bit = find_next_bit(&value, 32, bit);
1128 if ((bit + 32 * i) != MSM8625_INT_A9_M2A_5) {
Trilok Soni6278db02012-05-20 01:29:52 +05301129 raw_spin_unlock_irqrestore(
1130 &irq_controller_lock, flags);
Taniya Dasbc9248a2012-04-30 19:59:11 +05301131 return 1;
1132 }
1133 }
1134 }
1135 }
Trilok Soni6278db02012-05-20 01:29:52 +05301136 raw_spin_unlock_irqrestore(&irq_controller_lock, flags);
Taniya Dasbc9248a2012-04-30 19:59:11 +05301137
1138 return 0;
1139}
Trilok Soni38501052012-06-07 18:55:37 +05301140#endif
Taniya Dasbc9248a2012-04-30 19:59:11 +05301141
1142void msm_gic_save(bool modem_wake, int from_idle)
1143{
1144 unsigned int i;
1145 struct gic_chip_data *gic = &gic_data[0];
1146 void __iomem *base = gic_data_dist_base(gic);
1147
1148 gic_cpu_save(0);
1149 gic_dist_save(0);
Taniya Das8862d7d2012-05-21 20:11:37 +05301150
1151 /* Disable all the Interrupts, before we enter pc */
1152 for (i = 0; (i * 32) < gic->max_irq; i++) {
1153 raw_spin_lock(&irq_controller_lock);
1154 writel_relaxed(0xffffffff, base
1155 + GIC_DIST_ENABLE_CLEAR + i * 4);
1156 raw_spin_unlock(&irq_controller_lock);
Taniya Dasbc9248a2012-04-30 19:59:11 +05301157 }
1158}
1159
1160void msm_gic_restore(void)
1161{
1162 gic_dist_restore(0);
1163 gic_cpu_restore(0);
1164}
1165
1166/*
1167 * Configure the GIC after we come out of power collapse.
1168 * This function will configure some of the GIC registers so as to prepare the
1169 * core1 to receive an SPI(ACSR_MP_CORE_IPC1, (32 + 8)), which will bring
1170 * core1 out of GDFS.
1171 */
1172void core1_gic_configure_and_raise(void)
1173{
1174 struct gic_chip_data *gic = &gic_data[0];
1175 void __iomem *base = gic_data_dist_base(gic);
1176 unsigned int value = 0;
Trilok Soni6278db02012-05-20 01:29:52 +05301177 unsigned long flags;
Taniya Dasbc9248a2012-04-30 19:59:11 +05301178
Trilok Soni6278db02012-05-20 01:29:52 +05301179 raw_spin_lock_irqsave(&irq_controller_lock, flags);
Taniya Dasbc9248a2012-04-30 19:59:11 +05301180
1181 value = __raw_readl(base + GIC_DIST_ACTIVE_BIT + 0x4);
1182 value |= BIT(8);
1183 __raw_writel(value, base + GIC_DIST_ACTIVE_BIT + 0x4);
1184 mb();
1185
1186 value = __raw_readl(base + GIC_DIST_TARGET + 0x24);
1187 value |= BIT(13);
1188 __raw_writel(value, base + GIC_DIST_TARGET + 0x24);
1189 mb();
1190
1191 value = __raw_readl(base + GIC_DIST_TARGET + 0x28);
1192 value |= BIT(1);
1193 __raw_writel(value, base + GIC_DIST_TARGET + 0x28);
1194 mb();
1195
1196 value = __raw_readl(base + GIC_DIST_ENABLE_SET + 0x4);
1197 value |= BIT(8);
1198 __raw_writel(value, base + GIC_DIST_ENABLE_SET + 0x4);
1199 mb();
1200
1201 value = __raw_readl(base + GIC_DIST_PENDING_SET + 0x4);
1202 value |= BIT(8);
1203 __raw_writel(value, base + GIC_DIST_PENDING_SET + 0x4);
1204 mb();
Trilok Soni6278db02012-05-20 01:29:52 +05301205 raw_spin_unlock_irqrestore(&irq_controller_lock, flags);
Taniya Dasbc9248a2012-04-30 19:59:11 +05301206}