blob: ccaa1ab18de747311783734eec222a3001a877c8 [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 Herring4294f8ba2011-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 Cross254056f2011-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>
Rob Herring4294f8ba2011-09-28 21:25:31 -050033#include <linux/irqdomain.h>
Marc Zyngier292b2932011-07-20 16:24:14 +010034#include <linux/interrupt.h>
35#include <linux/percpu.h>
36#include <linux/slab.h>
Russell Kingf27ecac2005-08-18 21:31:00 +010037
38#include <asm/irq.h>
Russell Kingf27ecac2005-08-18 21:31:00 +010039#include <asm/mach/irq.h>
40#include <asm/hardware/gic.h>
41
Thomas Gleixnerc4bfa282006-07-01 22:32:14 +010042static DEFINE_SPINLOCK(irq_controller_lock);
Russell Kingf27ecac2005-08-18 21:31:00 +010043
Russell Kingff2e27a2010-12-04 16:13:29 +000044/* Address of GIC 0 CPU interface */
Russell Kingbef8f9e2010-12-04 16:50:58 +000045void __iomem *gic_cpu_base_addr __read_mostly;
Russell Kingff2e27a2010-12-04 16:13:29 +000046
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +010047/*
48 * Supported arch specific GIC irq extension.
49 * Default make them NULL.
50 */
51struct irq_chip gic_arch_extn = {
Will Deacon1a017532011-02-09 12:01:12 +000052 .irq_eoi = NULL,
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +010053 .irq_mask = NULL,
54 .irq_unmask = NULL,
55 .irq_retrigger = NULL,
56 .irq_set_type = NULL,
57 .irq_set_wake = NULL,
58};
59
Catalin Marinasb3a1bde2007-02-14 19:14:56 +010060#ifndef MAX_GIC_NR
61#define MAX_GIC_NR 1
62#endif
63
Russell Kingbef8f9e2010-12-04 16:50:58 +000064static struct gic_chip_data gic_data[MAX_GIC_NR] __read_mostly;
Catalin Marinasb3a1bde2007-02-14 19:14:56 +010065
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +010066static inline void __iomem *gic_dist_base(struct irq_data *d)
Catalin Marinasb3a1bde2007-02-14 19:14:56 +010067{
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +010068 struct gic_chip_data *gic_data = irq_data_get_irq_chip_data(d);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +010069 return gic_data->dist_base;
70}
71
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +010072static inline void __iomem *gic_cpu_base(struct irq_data *d)
Catalin Marinasb3a1bde2007-02-14 19:14:56 +010073{
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +010074 struct gic_chip_data *gic_data = irq_data_get_irq_chip_data(d);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +010075 return gic_data->cpu_base;
76}
77
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +010078static inline unsigned int gic_irq(struct irq_data *d)
Catalin Marinasb3a1bde2007-02-14 19:14:56 +010079{
Rob Herring4294f8ba2011-09-28 21:25:31 -050080 return d->hwirq;
Catalin Marinasb3a1bde2007-02-14 19:14:56 +010081}
82
Russell Kingf27ecac2005-08-18 21:31:00 +010083/*
84 * Routines to acknowledge, disable and enable interrupts
Russell Kingf27ecac2005-08-18 21:31:00 +010085 */
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +010086static void gic_mask_irq(struct irq_data *d)
Russell Kingf27ecac2005-08-18 21:31:00 +010087{
Rob Herring4294f8ba2011-09-28 21:25:31 -050088 u32 mask = 1 << (gic_irq(d) % 32);
Thomas Gleixnerc4bfa282006-07-01 22:32:14 +010089
90 spin_lock(&irq_controller_lock);
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +053091 writel_relaxed(mask, gic_dist_base(d) + GIC_DIST_ENABLE_CLEAR + (gic_irq(d) / 32) * 4);
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +010092 if (gic_arch_extn.irq_mask)
93 gic_arch_extn.irq_mask(d);
Thomas Gleixnerc4bfa282006-07-01 22:32:14 +010094 spin_unlock(&irq_controller_lock);
Russell Kingf27ecac2005-08-18 21:31:00 +010095}
96
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +010097static void gic_unmask_irq(struct irq_data *d)
Russell Kingf27ecac2005-08-18 21:31:00 +010098{
Rob Herring4294f8ba2011-09-28 21:25:31 -050099 u32 mask = 1 << (gic_irq(d) % 32);
Thomas Gleixnerc4bfa282006-07-01 22:32:14 +0100100
101 spin_lock(&irq_controller_lock);
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100102 if (gic_arch_extn.irq_unmask)
103 gic_arch_extn.irq_unmask(d);
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530104 writel_relaxed(mask, gic_dist_base(d) + GIC_DIST_ENABLE_SET + (gic_irq(d) / 32) * 4);
Thomas Gleixnerc4bfa282006-07-01 22:32:14 +0100105 spin_unlock(&irq_controller_lock);
Russell Kingf27ecac2005-08-18 21:31:00 +0100106}
107
Will Deacon1a017532011-02-09 12:01:12 +0000108static void gic_eoi_irq(struct irq_data *d)
109{
110 if (gic_arch_extn.irq_eoi) {
111 spin_lock(&irq_controller_lock);
112 gic_arch_extn.irq_eoi(d);
113 spin_unlock(&irq_controller_lock);
114 }
115
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530116 writel_relaxed(gic_irq(d), gic_cpu_base(d) + GIC_CPU_EOI);
Will Deacon1a017532011-02-09 12:01:12 +0000117}
118
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100119static int gic_set_type(struct irq_data *d, unsigned int type)
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100120{
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100121 void __iomem *base = gic_dist_base(d);
122 unsigned int gicirq = gic_irq(d);
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100123 u32 enablemask = 1 << (gicirq % 32);
124 u32 enableoff = (gicirq / 32) * 4;
125 u32 confmask = 0x2 << ((gicirq % 16) * 2);
126 u32 confoff = (gicirq / 16) * 4;
127 bool enabled = false;
128 u32 val;
129
130 /* Interrupt configuration for SGIs can't be changed */
131 if (gicirq < 16)
132 return -EINVAL;
133
134 if (type != IRQ_TYPE_LEVEL_HIGH && type != IRQ_TYPE_EDGE_RISING)
135 return -EINVAL;
136
137 spin_lock(&irq_controller_lock);
138
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100139 if (gic_arch_extn.irq_set_type)
140 gic_arch_extn.irq_set_type(d, type);
141
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530142 val = readl_relaxed(base + GIC_DIST_CONFIG + confoff);
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100143 if (type == IRQ_TYPE_LEVEL_HIGH)
144 val &= ~confmask;
145 else if (type == IRQ_TYPE_EDGE_RISING)
146 val |= confmask;
147
148 /*
149 * As recommended by the spec, disable the interrupt before changing
150 * the configuration
151 */
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530152 if (readl_relaxed(base + GIC_DIST_ENABLE_SET + enableoff) & enablemask) {
153 writel_relaxed(enablemask, base + GIC_DIST_ENABLE_CLEAR + enableoff);
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100154 enabled = true;
155 }
156
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530157 writel_relaxed(val, base + GIC_DIST_CONFIG + confoff);
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100158
159 if (enabled)
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530160 writel_relaxed(enablemask, base + GIC_DIST_ENABLE_SET + enableoff);
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100161
162 spin_unlock(&irq_controller_lock);
163
164 return 0;
165}
166
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100167static int gic_retrigger(struct irq_data *d)
168{
169 if (gic_arch_extn.irq_retrigger)
170 return gic_arch_extn.irq_retrigger(d);
171
172 return -ENXIO;
173}
174
Catalin Marinasa06f5462005-09-30 16:07:05 +0100175#ifdef CONFIG_SMP
Russell Kingc1917892011-01-23 12:12:01 +0000176static int gic_set_affinity(struct irq_data *d, const struct cpumask *mask_val,
177 bool force)
Russell Kingf27ecac2005-08-18 21:31:00 +0100178{
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100179 void __iomem *reg = gic_dist_base(d) + GIC_DIST_TARGET + (gic_irq(d) & ~3);
Rob Herring4294f8ba2011-09-28 21:25:31 -0500180 unsigned int shift = (gic_irq(d) % 4) * 8;
Russell King5dfc54e2011-07-21 15:00:57 +0100181 unsigned int cpu = cpumask_any_and(mask_val, cpu_online_mask);
Russell Kingc1917892011-01-23 12:12:01 +0000182 u32 val, mask, bit;
183
Russell King5dfc54e2011-07-21 15:00:57 +0100184 if (cpu >= 8 || cpu >= nr_cpu_ids)
Russell Kingc1917892011-01-23 12:12:01 +0000185 return -EINVAL;
186
187 mask = 0xff << shift;
Will Deacon267840f2011-08-23 22:20:03 +0100188 bit = 1 << (cpu_logical_map(cpu) + shift);
Russell Kingf27ecac2005-08-18 21:31:00 +0100189
Thomas Gleixnerc4bfa282006-07-01 22:32:14 +0100190 spin_lock(&irq_controller_lock);
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530191 val = readl_relaxed(reg) & ~mask;
192 writel_relaxed(val | bit, reg);
Thomas Gleixnerc4bfa282006-07-01 22:32:14 +0100193 spin_unlock(&irq_controller_lock);
Yinghai Lud5dedd42009-04-27 17:59:21 -0700194
Russell King5dfc54e2011-07-21 15:00:57 +0100195 return IRQ_SET_MASK_OK;
Russell Kingf27ecac2005-08-18 21:31:00 +0100196}
Catalin Marinasa06f5462005-09-30 16:07:05 +0100197#endif
Russell Kingf27ecac2005-08-18 21:31:00 +0100198
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100199#ifdef CONFIG_PM
200static int gic_set_wake(struct irq_data *d, unsigned int on)
201{
202 int ret = -ENXIO;
203
204 if (gic_arch_extn.irq_set_wake)
205 ret = gic_arch_extn.irq_set_wake(d, on);
206
207 return ret;
208}
209
210#else
211#define gic_set_wake NULL
212#endif
213
Russell King0f347bb2007-05-17 10:11:34 +0100214static void gic_handle_cascade_irq(unsigned int irq, struct irq_desc *desc)
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100215{
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100216 struct gic_chip_data *chip_data = irq_get_handler_data(irq);
217 struct irq_chip *chip = irq_get_chip(irq);
Russell King0f347bb2007-05-17 10:11:34 +0100218 unsigned int cascade_irq, gic_irq;
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100219 unsigned long status;
220
Will Deacon1a017532011-02-09 12:01:12 +0000221 chained_irq_enter(chip, desc);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100222
223 spin_lock(&irq_controller_lock);
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530224 status = readl_relaxed(chip_data->cpu_base + GIC_CPU_INTACK);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100225 spin_unlock(&irq_controller_lock);
226
Russell King0f347bb2007-05-17 10:11:34 +0100227 gic_irq = (status & 0x3ff);
228 if (gic_irq == 1023)
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100229 goto out;
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100230
Rob Herring4294f8ba2011-09-28 21:25:31 -0500231 cascade_irq = irq_domain_to_irq(&chip_data->domain, gic_irq);
Russell King0f347bb2007-05-17 10:11:34 +0100232 if (unlikely(gic_irq < 32 || gic_irq > 1020 || cascade_irq >= NR_IRQS))
233 do_bad_IRQ(cascade_irq, desc);
234 else
235 generic_handle_irq(cascade_irq);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100236
237 out:
Will Deacon1a017532011-02-09 12:01:12 +0000238 chained_irq_exit(chip, desc);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100239}
240
David Brownell38c677c2006-08-01 22:26:25 +0100241static struct irq_chip gic_chip = {
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100242 .name = "GIC",
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100243 .irq_mask = gic_mask_irq,
244 .irq_unmask = gic_unmask_irq,
Will Deacon1a017532011-02-09 12:01:12 +0000245 .irq_eoi = gic_eoi_irq,
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100246 .irq_set_type = gic_set_type,
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100247 .irq_retrigger = gic_retrigger,
Russell Kingf27ecac2005-08-18 21:31:00 +0100248#ifdef CONFIG_SMP
Russell Kingc1917892011-01-23 12:12:01 +0000249 .irq_set_affinity = gic_set_affinity,
Russell Kingf27ecac2005-08-18 21:31:00 +0100250#endif
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100251 .irq_set_wake = gic_set_wake,
Russell Kingf27ecac2005-08-18 21:31:00 +0100252};
253
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100254void __init gic_cascade_irq(unsigned int gic_nr, unsigned int irq)
255{
256 if (gic_nr >= MAX_GIC_NR)
257 BUG();
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100258 if (irq_set_handler_data(irq, &gic_data[gic_nr]) != 0)
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100259 BUG();
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100260 irq_set_chained_handler(irq, gic_handle_cascade_irq);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100261}
262
Rob Herring4294f8ba2011-09-28 21:25:31 -0500263static void __init gic_dist_init(struct gic_chip_data *gic)
Russell Kingf27ecac2005-08-18 21:31:00 +0100264{
Rob Herring4294f8ba2011-09-28 21:25:31 -0500265 unsigned int i, irq;
Will Deacon267840f2011-08-23 22:20:03 +0100266 u32 cpumask;
Rob Herring4294f8ba2011-09-28 21:25:31 -0500267 unsigned int gic_irqs = gic->gic_irqs;
268 struct irq_domain *domain = &gic->domain;
Russell Kingbef8f9e2010-12-04 16:50:58 +0000269 void __iomem *base = gic->dist_base;
Will Deacon267840f2011-08-23 22:20:03 +0100270 u32 cpu = 0;
Russell Kingf27ecac2005-08-18 21:31:00 +0100271
Will Deacon267840f2011-08-23 22:20:03 +0100272#ifdef CONFIG_SMP
273 cpu = cpu_logical_map(smp_processor_id());
274#endif
275
276 cpumask = 1 << cpu;
Russell Kingf27ecac2005-08-18 21:31:00 +0100277 cpumask |= cpumask << 8;
278 cpumask |= cpumask << 16;
279
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530280 writel_relaxed(0, base + GIC_DIST_CTRL);
Russell Kingf27ecac2005-08-18 21:31:00 +0100281
282 /*
Russell Kingf27ecac2005-08-18 21:31:00 +0100283 * Set all global interrupts to be level triggered, active low.
284 */
Pawel Molle6afec92010-11-26 13:45:43 +0100285 for (i = 32; i < gic_irqs; i += 16)
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530286 writel_relaxed(0, base + GIC_DIST_CONFIG + i * 4 / 16);
Russell Kingf27ecac2005-08-18 21:31:00 +0100287
288 /*
289 * Set all global interrupts to this CPU only.
290 */
Pawel Molle6afec92010-11-26 13:45:43 +0100291 for (i = 32; i < gic_irqs; i += 4)
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530292 writel_relaxed(cpumask, base + GIC_DIST_TARGET + i * 4 / 4);
Russell Kingf27ecac2005-08-18 21:31:00 +0100293
294 /*
Russell King9395f6e2010-11-11 23:10:30 +0000295 * Set priority on all global interrupts.
Russell Kingf27ecac2005-08-18 21:31:00 +0100296 */
Pawel Molle6afec92010-11-26 13:45:43 +0100297 for (i = 32; i < gic_irqs; i += 4)
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530298 writel_relaxed(0xa0a0a0a0, base + GIC_DIST_PRI + i * 4 / 4);
Russell Kingf27ecac2005-08-18 21:31:00 +0100299
300 /*
Russell King9395f6e2010-11-11 23:10:30 +0000301 * Disable all interrupts. Leave the PPI and SGIs alone
302 * as these enables are banked registers.
Russell Kingf27ecac2005-08-18 21:31:00 +0100303 */
Pawel Molle6afec92010-11-26 13:45:43 +0100304 for (i = 32; i < gic_irqs; i += 32)
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530305 writel_relaxed(0xffffffff, base + GIC_DIST_ENABLE_CLEAR + i * 4 / 32);
Russell Kingf27ecac2005-08-18 21:31:00 +0100306
307 /*
308 * Setup the Linux IRQ subsystem.
309 */
Rob Herring4294f8ba2011-09-28 21:25:31 -0500310 irq_domain_for_each_irq(domain, i, irq) {
311 if (i < 32) {
312 irq_set_percpu_devid(irq);
313 irq_set_chip_and_handler(irq, &gic_chip,
314 handle_percpu_devid_irq);
315 set_irq_flags(irq, IRQF_VALID | IRQF_NOAUTOEN);
316 } else {
317 irq_set_chip_and_handler(irq, &gic_chip,
318 handle_fasteoi_irq);
319 set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
320 }
321 irq_set_chip_data(irq, gic);
Russell Kingf27ecac2005-08-18 21:31:00 +0100322 }
323
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530324 writel_relaxed(1, base + GIC_DIST_CTRL);
Russell Kingf27ecac2005-08-18 21:31:00 +0100325}
326
Russell Kingbef8f9e2010-12-04 16:50:58 +0000327static void __cpuinit gic_cpu_init(struct gic_chip_data *gic)
Russell Kingf27ecac2005-08-18 21:31:00 +0100328{
Russell Kingbef8f9e2010-12-04 16:50:58 +0000329 void __iomem *dist_base = gic->dist_base;
330 void __iomem *base = gic->cpu_base;
Russell King9395f6e2010-11-11 23:10:30 +0000331 int i;
332
Russell King9395f6e2010-11-11 23:10:30 +0000333 /*
334 * Deal with the banked PPI and SGI interrupts - disable all
335 * PPI interrupts, ensure all SGI interrupts are enabled.
336 */
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530337 writel_relaxed(0xffff0000, dist_base + GIC_DIST_ENABLE_CLEAR);
338 writel_relaxed(0x0000ffff, dist_base + GIC_DIST_ENABLE_SET);
Russell King9395f6e2010-11-11 23:10:30 +0000339
340 /*
341 * Set priority on PPI and SGI interrupts
342 */
343 for (i = 0; i < 32; i += 4)
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530344 writel_relaxed(0xa0a0a0a0, dist_base + GIC_DIST_PRI + i * 4 / 4);
Russell King9395f6e2010-11-11 23:10:30 +0000345
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530346 writel_relaxed(0xf0, base + GIC_CPU_PRIMASK);
347 writel_relaxed(1, base + GIC_CPU_CTRL);
Russell Kingf27ecac2005-08-18 21:31:00 +0100348}
349
Colin Cross254056f2011-02-10 12:54:10 -0800350#ifdef CONFIG_CPU_PM
351/*
352 * Saves the GIC distributor registers during suspend or idle. Must be called
353 * with interrupts disabled but before powering down the GIC. After calling
354 * this function, no interrupts will be delivered by the GIC, and another
355 * platform-specific wakeup source must be enabled.
356 */
357static void gic_dist_save(unsigned int gic_nr)
358{
359 unsigned int gic_irqs;
360 void __iomem *dist_base;
361 int i;
362
363 if (gic_nr >= MAX_GIC_NR)
364 BUG();
365
366 gic_irqs = gic_data[gic_nr].gic_irqs;
367 dist_base = gic_data[gic_nr].dist_base;
368
369 if (!dist_base)
370 return;
371
372 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 16); i++)
373 gic_data[gic_nr].saved_spi_conf[i] =
374 readl_relaxed(dist_base + GIC_DIST_CONFIG + i * 4);
375
376 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 4); i++)
377 gic_data[gic_nr].saved_spi_target[i] =
378 readl_relaxed(dist_base + GIC_DIST_TARGET + i * 4);
379
380 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 32); i++)
381 gic_data[gic_nr].saved_spi_enable[i] =
382 readl_relaxed(dist_base + GIC_DIST_ENABLE_SET + i * 4);
383}
384
385/*
386 * Restores the GIC distributor registers during resume or when coming out of
387 * idle. Must be called before enabling interrupts. If a level interrupt
388 * that occured while the GIC was suspended is still present, it will be
389 * handled normally, but any edge interrupts that occured will not be seen by
390 * the GIC and need to be handled by the platform-specific wakeup source.
391 */
392static void gic_dist_restore(unsigned int gic_nr)
393{
394 unsigned int gic_irqs;
395 unsigned int i;
396 void __iomem *dist_base;
397
398 if (gic_nr >= MAX_GIC_NR)
399 BUG();
400
401 gic_irqs = gic_data[gic_nr].gic_irqs;
402 dist_base = gic_data[gic_nr].dist_base;
403
404 if (!dist_base)
405 return;
406
407 writel_relaxed(0, dist_base + GIC_DIST_CTRL);
408
409 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 16); i++)
410 writel_relaxed(gic_data[gic_nr].saved_spi_conf[i],
411 dist_base + GIC_DIST_CONFIG + i * 4);
412
413 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 4); i++)
414 writel_relaxed(0xa0a0a0a0,
415 dist_base + GIC_DIST_PRI + i * 4);
416
417 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 4); i++)
418 writel_relaxed(gic_data[gic_nr].saved_spi_target[i],
419 dist_base + GIC_DIST_TARGET + i * 4);
420
421 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 32); i++)
422 writel_relaxed(gic_data[gic_nr].saved_spi_enable[i],
423 dist_base + GIC_DIST_ENABLE_SET + i * 4);
424
425 writel_relaxed(1, dist_base + GIC_DIST_CTRL);
426}
427
428static void gic_cpu_save(unsigned int gic_nr)
429{
430 int i;
431 u32 *ptr;
432 void __iomem *dist_base;
433 void __iomem *cpu_base;
434
435 if (gic_nr >= MAX_GIC_NR)
436 BUG();
437
438 dist_base = gic_data[gic_nr].dist_base;
439 cpu_base = gic_data[gic_nr].cpu_base;
440
441 if (!dist_base || !cpu_base)
442 return;
443
444 ptr = __this_cpu_ptr(gic_data[gic_nr].saved_ppi_enable);
445 for (i = 0; i < DIV_ROUND_UP(32, 32); i++)
446 ptr[i] = readl_relaxed(dist_base + GIC_DIST_ENABLE_SET + i * 4);
447
448 ptr = __this_cpu_ptr(gic_data[gic_nr].saved_ppi_conf);
449 for (i = 0; i < DIV_ROUND_UP(32, 16); i++)
450 ptr[i] = readl_relaxed(dist_base + GIC_DIST_CONFIG + i * 4);
451
452}
453
454static void gic_cpu_restore(unsigned int gic_nr)
455{
456 int i;
457 u32 *ptr;
458 void __iomem *dist_base;
459 void __iomem *cpu_base;
460
461 if (gic_nr >= MAX_GIC_NR)
462 BUG();
463
464 dist_base = gic_data[gic_nr].dist_base;
465 cpu_base = gic_data[gic_nr].cpu_base;
466
467 if (!dist_base || !cpu_base)
468 return;
469
470 ptr = __this_cpu_ptr(gic_data[gic_nr].saved_ppi_enable);
471 for (i = 0; i < DIV_ROUND_UP(32, 32); i++)
472 writel_relaxed(ptr[i], dist_base + GIC_DIST_ENABLE_SET + i * 4);
473
474 ptr = __this_cpu_ptr(gic_data[gic_nr].saved_ppi_conf);
475 for (i = 0; i < DIV_ROUND_UP(32, 16); i++)
476 writel_relaxed(ptr[i], dist_base + GIC_DIST_CONFIG + i * 4);
477
478 for (i = 0; i < DIV_ROUND_UP(32, 4); i++)
479 writel_relaxed(0xa0a0a0a0, dist_base + GIC_DIST_PRI + i * 4);
480
481 writel_relaxed(0xf0, cpu_base + GIC_CPU_PRIMASK);
482 writel_relaxed(1, cpu_base + GIC_CPU_CTRL);
483}
484
485static int gic_notifier(struct notifier_block *self, unsigned long cmd, void *v)
486{
487 int i;
488
489 for (i = 0; i < MAX_GIC_NR; i++) {
490 switch (cmd) {
491 case CPU_PM_ENTER:
492 gic_cpu_save(i);
493 break;
494 case CPU_PM_ENTER_FAILED:
495 case CPU_PM_EXIT:
496 gic_cpu_restore(i);
497 break;
498 case CPU_CLUSTER_PM_ENTER:
499 gic_dist_save(i);
500 break;
501 case CPU_CLUSTER_PM_ENTER_FAILED:
502 case CPU_CLUSTER_PM_EXIT:
503 gic_dist_restore(i);
504 break;
505 }
506 }
507
508 return NOTIFY_OK;
509}
510
511static struct notifier_block gic_notifier_block = {
512 .notifier_call = gic_notifier,
513};
514
515static void __init gic_pm_init(struct gic_chip_data *gic)
516{
517 gic->saved_ppi_enable = __alloc_percpu(DIV_ROUND_UP(32, 32) * 4,
518 sizeof(u32));
519 BUG_ON(!gic->saved_ppi_enable);
520
521 gic->saved_ppi_conf = __alloc_percpu(DIV_ROUND_UP(32, 16) * 4,
522 sizeof(u32));
523 BUG_ON(!gic->saved_ppi_conf);
524
525 cpu_pm_register_notifier(&gic_notifier_block);
526}
527#else
528static void __init gic_pm_init(struct gic_chip_data *gic)
529{
530}
531#endif
532
Rob Herring4294f8ba2011-09-28 21:25:31 -0500533const struct irq_domain_ops gic_irq_domain_ops = {
534};
535
Russell Kingb580b892010-12-04 15:55:14 +0000536void __init gic_init(unsigned int gic_nr, unsigned int irq_start,
537 void __iomem *dist_base, void __iomem *cpu_base)
538{
Russell Kingbef8f9e2010-12-04 16:50:58 +0000539 struct gic_chip_data *gic;
Rob Herring4294f8ba2011-09-28 21:25:31 -0500540 struct irq_domain *domain;
541 int gic_irqs;
Russell Kingbef8f9e2010-12-04 16:50:58 +0000542
543 BUG_ON(gic_nr >= MAX_GIC_NR);
544
545 gic = &gic_data[gic_nr];
Rob Herring4294f8ba2011-09-28 21:25:31 -0500546 domain = &gic->domain;
Russell Kingbef8f9e2010-12-04 16:50:58 +0000547 gic->dist_base = dist_base;
548 gic->cpu_base = cpu_base;
Russell Kingbef8f9e2010-12-04 16:50:58 +0000549
Rob Herring4294f8ba2011-09-28 21:25:31 -0500550 /*
551 * For primary GICs, skip over SGIs.
552 * For secondary GICs, skip over PPIs, too.
553 */
554 if (gic_nr == 0) {
Russell Kingff2e27a2010-12-04 16:13:29 +0000555 gic_cpu_base_addr = cpu_base;
Rob Herring4294f8ba2011-09-28 21:25:31 -0500556 domain->hwirq_base = 16;
557 irq_start = (irq_start & ~31) + 16;
558 } else
559 domain->hwirq_base = 32;
560
561 /*
562 * Find out how many interrupts are supported.
563 * The GIC only supports up to 1020 interrupt sources.
564 */
565 gic_irqs = readl_relaxed(dist_base + GIC_DIST_CTR) & 0x1f;
566 gic_irqs = (gic_irqs + 1) * 32;
567 if (gic_irqs > 1020)
568 gic_irqs = 1020;
569 gic->gic_irqs = gic_irqs;
570
571 domain->nr_irq = gic_irqs - domain->hwirq_base;
572 domain->irq_base = irq_alloc_descs(-1, irq_start, domain->nr_irq,
573 numa_node_id());
574 domain->priv = gic;
575 domain->ops = &gic_irq_domain_ops;
576 irq_domain_add(domain);
Russell Kingbef8f9e2010-12-04 16:50:58 +0000577
Colin Cross9c128452011-06-13 00:45:59 +0000578 gic_chip.flags |= gic_arch_extn.flags;
Rob Herring4294f8ba2011-09-28 21:25:31 -0500579 gic_dist_init(gic);
Russell Kingbef8f9e2010-12-04 16:50:58 +0000580 gic_cpu_init(gic);
Colin Cross254056f2011-02-10 12:54:10 -0800581 gic_pm_init(gic);
Russell Kingb580b892010-12-04 15:55:14 +0000582}
583
Russell King38489532010-12-04 16:01:03 +0000584void __cpuinit gic_secondary_init(unsigned int gic_nr)
585{
Russell Kingbef8f9e2010-12-04 16:50:58 +0000586 BUG_ON(gic_nr >= MAX_GIC_NR);
587
588 gic_cpu_init(&gic_data[gic_nr]);
Russell King38489532010-12-04 16:01:03 +0000589}
590
Russell Kingf27ecac2005-08-18 21:31:00 +0100591#ifdef CONFIG_SMP
Russell King82668102009-05-17 16:20:18 +0100592void gic_raise_softirq(const struct cpumask *mask, unsigned int irq)
Russell Kingf27ecac2005-08-18 21:31:00 +0100593{
Will Deacon267840f2011-08-23 22:20:03 +0100594 int cpu;
595 unsigned long map = 0;
596
597 /* Convert our logical CPU mask into a physical one. */
598 for_each_cpu(cpu, mask)
599 map |= 1 << cpu_logical_map(cpu);
Russell Kingf27ecac2005-08-18 21:31:00 +0100600
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530601 /*
602 * Ensure that stores to Normal memory are visible to the
603 * other CPUs before issuing the IPI.
604 */
605 dsb();
606
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100607 /* this always happens on GIC0 */
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530608 writel_relaxed(map << 16 | irq, gic_data[0].dist_base + GIC_DIST_SOFTINT);
Russell Kingf27ecac2005-08-18 21:31:00 +0100609}
610#endif