blob: 016c1aeb847c5c5aa2cfd092ced53e2c8e017c39 [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>
27#include <linux/list.h>
28#include <linux/smp.h>
Colin Cross254056f2011-02-10 12:54:10 -080029#include <linux/cpu_pm.h>
Catalin Marinasdcb86e82005-08-31 21:45:14 +010030#include <linux/cpumask.h>
Russell Kingfced80c2008-09-06 12:10:45 +010031#include <linux/io.h>
Marc Zyngier292b2932011-07-20 16:24:14 +010032#include <linux/interrupt.h>
33#include <linux/percpu.h>
34#include <linux/slab.h>
Russell Kingf27ecac2005-08-18 21:31:00 +010035
36#include <asm/irq.h>
Russell Kingf27ecac2005-08-18 21:31:00 +010037#include <asm/mach/irq.h>
38#include <asm/hardware/gic.h>
39
Thomas Gleixnerc4bfa282006-07-01 22:32:14 +010040static DEFINE_SPINLOCK(irq_controller_lock);
Russell Kingf27ecac2005-08-18 21:31:00 +010041
Russell Kingff2e27a2010-12-04 16:13:29 +000042/* Address of GIC 0 CPU interface */
Russell Kingbef8f9e2010-12-04 16:50:58 +000043void __iomem *gic_cpu_base_addr __read_mostly;
Russell Kingff2e27a2010-12-04 16:13:29 +000044
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +010045/*
46 * Supported arch specific GIC irq extension.
47 * Default make them NULL.
48 */
49struct irq_chip gic_arch_extn = {
Will Deacon1a017532011-02-09 12:01:12 +000050 .irq_eoi = NULL,
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +010051 .irq_mask = NULL,
52 .irq_unmask = NULL,
53 .irq_retrigger = NULL,
54 .irq_set_type = NULL,
55 .irq_set_wake = NULL,
56};
57
Catalin Marinasb3a1bde2007-02-14 19:14:56 +010058#ifndef MAX_GIC_NR
59#define MAX_GIC_NR 1
60#endif
61
Russell Kingbef8f9e2010-12-04 16:50:58 +000062static struct gic_chip_data gic_data[MAX_GIC_NR] __read_mostly;
Catalin Marinasb3a1bde2007-02-14 19:14:56 +010063
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +010064static inline void __iomem *gic_dist_base(struct irq_data *d)
Catalin Marinasb3a1bde2007-02-14 19:14:56 +010065{
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +010066 struct gic_chip_data *gic_data = irq_data_get_irq_chip_data(d);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +010067 return gic_data->dist_base;
68}
69
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +010070static inline void __iomem *gic_cpu_base(struct irq_data *d)
Catalin Marinasb3a1bde2007-02-14 19:14:56 +010071{
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +010072 struct gic_chip_data *gic_data = irq_data_get_irq_chip_data(d);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +010073 return gic_data->cpu_base;
74}
75
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +010076static inline unsigned int gic_irq(struct irq_data *d)
Catalin Marinasb3a1bde2007-02-14 19:14:56 +010077{
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +010078 struct gic_chip_data *gic_data = irq_data_get_irq_chip_data(d);
79 return d->irq - gic_data->irq_offset;
Catalin Marinasb3a1bde2007-02-14 19:14:56 +010080}
81
Russell Kingf27ecac2005-08-18 21:31:00 +010082/*
83 * Routines to acknowledge, disable and enable interrupts
Russell Kingf27ecac2005-08-18 21:31:00 +010084 */
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +010085static void gic_mask_irq(struct irq_data *d)
Russell Kingf27ecac2005-08-18 21:31:00 +010086{
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +010087 u32 mask = 1 << (d->irq % 32);
Thomas Gleixnerc4bfa282006-07-01 22:32:14 +010088
89 spin_lock(&irq_controller_lock);
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +053090 writel_relaxed(mask, gic_dist_base(d) + GIC_DIST_ENABLE_CLEAR + (gic_irq(d) / 32) * 4);
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +010091 if (gic_arch_extn.irq_mask)
92 gic_arch_extn.irq_mask(d);
Thomas Gleixnerc4bfa282006-07-01 22:32:14 +010093 spin_unlock(&irq_controller_lock);
Russell Kingf27ecac2005-08-18 21:31:00 +010094}
95
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +010096static void gic_unmask_irq(struct irq_data *d)
Russell Kingf27ecac2005-08-18 21:31:00 +010097{
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +010098 u32 mask = 1 << (d->irq % 32);
Thomas Gleixnerc4bfa282006-07-01 22:32:14 +010099
100 spin_lock(&irq_controller_lock);
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100101 if (gic_arch_extn.irq_unmask)
102 gic_arch_extn.irq_unmask(d);
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530103 writel_relaxed(mask, gic_dist_base(d) + GIC_DIST_ENABLE_SET + (gic_irq(d) / 32) * 4);
Thomas Gleixnerc4bfa282006-07-01 22:32:14 +0100104 spin_unlock(&irq_controller_lock);
Russell Kingf27ecac2005-08-18 21:31:00 +0100105}
106
Will Deacon1a017532011-02-09 12:01:12 +0000107static void gic_eoi_irq(struct irq_data *d)
108{
109 if (gic_arch_extn.irq_eoi) {
110 spin_lock(&irq_controller_lock);
111 gic_arch_extn.irq_eoi(d);
112 spin_unlock(&irq_controller_lock);
113 }
114
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530115 writel_relaxed(gic_irq(d), gic_cpu_base(d) + GIC_CPU_EOI);
Will Deacon1a017532011-02-09 12:01:12 +0000116}
117
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100118static int gic_set_type(struct irq_data *d, unsigned int type)
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100119{
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100120 void __iomem *base = gic_dist_base(d);
121 unsigned int gicirq = gic_irq(d);
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100122 u32 enablemask = 1 << (gicirq % 32);
123 u32 enableoff = (gicirq / 32) * 4;
124 u32 confmask = 0x2 << ((gicirq % 16) * 2);
125 u32 confoff = (gicirq / 16) * 4;
126 bool enabled = false;
127 u32 val;
128
129 /* Interrupt configuration for SGIs can't be changed */
130 if (gicirq < 16)
131 return -EINVAL;
132
133 if (type != IRQ_TYPE_LEVEL_HIGH && type != IRQ_TYPE_EDGE_RISING)
134 return -EINVAL;
135
136 spin_lock(&irq_controller_lock);
137
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100138 if (gic_arch_extn.irq_set_type)
139 gic_arch_extn.irq_set_type(d, type);
140
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530141 val = readl_relaxed(base + GIC_DIST_CONFIG + confoff);
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100142 if (type == IRQ_TYPE_LEVEL_HIGH)
143 val &= ~confmask;
144 else if (type == IRQ_TYPE_EDGE_RISING)
145 val |= confmask;
146
147 /*
148 * As recommended by the spec, disable the interrupt before changing
149 * the configuration
150 */
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530151 if (readl_relaxed(base + GIC_DIST_ENABLE_SET + enableoff) & enablemask) {
152 writel_relaxed(enablemask, base + GIC_DIST_ENABLE_CLEAR + enableoff);
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100153 enabled = true;
154 }
155
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530156 writel_relaxed(val, base + GIC_DIST_CONFIG + confoff);
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100157
158 if (enabled)
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530159 writel_relaxed(enablemask, base + GIC_DIST_ENABLE_SET + enableoff);
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100160
161 spin_unlock(&irq_controller_lock);
162
163 return 0;
164}
165
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100166static int gic_retrigger(struct irq_data *d)
167{
168 if (gic_arch_extn.irq_retrigger)
169 return gic_arch_extn.irq_retrigger(d);
170
171 return -ENXIO;
172}
173
Catalin Marinasa06f5462005-09-30 16:07:05 +0100174#ifdef CONFIG_SMP
Russell Kingc1917892011-01-23 12:12:01 +0000175static int gic_set_affinity(struct irq_data *d, const struct cpumask *mask_val,
176 bool force)
Russell Kingf27ecac2005-08-18 21:31:00 +0100177{
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100178 void __iomem *reg = gic_dist_base(d) + GIC_DIST_TARGET + (gic_irq(d) & ~3);
179 unsigned int shift = (d->irq % 4) * 8;
Russell King5dfc54e2011-07-21 15:00:57 +0100180 unsigned int cpu = cpumask_any_and(mask_val, cpu_online_mask);
Russell Kingc1917892011-01-23 12:12:01 +0000181 u32 val, mask, bit;
182
Russell King5dfc54e2011-07-21 15:00:57 +0100183 if (cpu >= 8 || cpu >= nr_cpu_ids)
Russell Kingc1917892011-01-23 12:12:01 +0000184 return -EINVAL;
185
186 mask = 0xff << shift;
Will Deacon267840f2011-08-23 22:20:03 +0100187 bit = 1 << (cpu_logical_map(cpu) + shift);
Russell Kingf27ecac2005-08-18 21:31:00 +0100188
Thomas Gleixnerc4bfa282006-07-01 22:32:14 +0100189 spin_lock(&irq_controller_lock);
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530190 val = readl_relaxed(reg) & ~mask;
191 writel_relaxed(val | bit, reg);
Thomas Gleixnerc4bfa282006-07-01 22:32:14 +0100192 spin_unlock(&irq_controller_lock);
Yinghai Lud5dedd42009-04-27 17:59:21 -0700193
Russell King5dfc54e2011-07-21 15:00:57 +0100194 return IRQ_SET_MASK_OK;
Russell Kingf27ecac2005-08-18 21:31:00 +0100195}
Catalin Marinasa06f5462005-09-30 16:07:05 +0100196#endif
Russell Kingf27ecac2005-08-18 21:31:00 +0100197
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100198#ifdef CONFIG_PM
199static int gic_set_wake(struct irq_data *d, unsigned int on)
200{
201 int ret = -ENXIO;
202
203 if (gic_arch_extn.irq_set_wake)
204 ret = gic_arch_extn.irq_set_wake(d, on);
205
206 return ret;
207}
208
209#else
210#define gic_set_wake NULL
211#endif
212
Russell King0f347bb2007-05-17 10:11:34 +0100213static void gic_handle_cascade_irq(unsigned int irq, struct irq_desc *desc)
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100214{
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100215 struct gic_chip_data *chip_data = irq_get_handler_data(irq);
216 struct irq_chip *chip = irq_get_chip(irq);
Russell King0f347bb2007-05-17 10:11:34 +0100217 unsigned int cascade_irq, gic_irq;
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100218 unsigned long status;
219
Will Deacon1a017532011-02-09 12:01:12 +0000220 chained_irq_enter(chip, desc);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100221
222 spin_lock(&irq_controller_lock);
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530223 status = readl_relaxed(chip_data->cpu_base + GIC_CPU_INTACK);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100224 spin_unlock(&irq_controller_lock);
225
Russell King0f347bb2007-05-17 10:11:34 +0100226 gic_irq = (status & 0x3ff);
227 if (gic_irq == 1023)
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100228 goto out;
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100229
Russell King0f347bb2007-05-17 10:11:34 +0100230 cascade_irq = gic_irq + chip_data->irq_offset;
231 if (unlikely(gic_irq < 32 || gic_irq > 1020 || cascade_irq >= NR_IRQS))
232 do_bad_IRQ(cascade_irq, desc);
233 else
234 generic_handle_irq(cascade_irq);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100235
236 out:
Will Deacon1a017532011-02-09 12:01:12 +0000237 chained_irq_exit(chip, desc);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100238}
239
David Brownell38c677c2006-08-01 22:26:25 +0100240static struct irq_chip gic_chip = {
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100241 .name = "GIC",
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100242 .irq_mask = gic_mask_irq,
243 .irq_unmask = gic_unmask_irq,
Will Deacon1a017532011-02-09 12:01:12 +0000244 .irq_eoi = gic_eoi_irq,
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100245 .irq_set_type = gic_set_type,
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100246 .irq_retrigger = gic_retrigger,
Russell Kingf27ecac2005-08-18 21:31:00 +0100247#ifdef CONFIG_SMP
Russell Kingc1917892011-01-23 12:12:01 +0000248 .irq_set_affinity = gic_set_affinity,
Russell Kingf27ecac2005-08-18 21:31:00 +0100249#endif
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100250 .irq_set_wake = gic_set_wake,
Russell Kingf27ecac2005-08-18 21:31:00 +0100251};
252
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100253void __init gic_cascade_irq(unsigned int gic_nr, unsigned int irq)
254{
255 if (gic_nr >= MAX_GIC_NR)
256 BUG();
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100257 if (irq_set_handler_data(irq, &gic_data[gic_nr]) != 0)
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100258 BUG();
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100259 irq_set_chained_handler(irq, gic_handle_cascade_irq);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100260}
261
Russell Kingbef8f9e2010-12-04 16:50:58 +0000262static void __init gic_dist_init(struct gic_chip_data *gic,
Russell Kingb580b892010-12-04 15:55:14 +0000263 unsigned int irq_start)
Russell Kingf27ecac2005-08-18 21:31:00 +0100264{
Pawel Molle6afec92010-11-26 13:45:43 +0100265 unsigned int gic_irqs, irq_limit, i;
Will Deacon267840f2011-08-23 22:20:03 +0100266 u32 cpumask;
Russell Kingbef8f9e2010-12-04 16:50:58 +0000267 void __iomem *base = gic->dist_base;
Will Deacon267840f2011-08-23 22:20:03 +0100268 u32 cpu = 0;
Marc Zyngier292b2932011-07-20 16:24:14 +0100269 u32 nrppis = 0, ppi_base = 0;
Russell Kingf27ecac2005-08-18 21:31:00 +0100270
Will Deacon267840f2011-08-23 22:20:03 +0100271#ifdef CONFIG_SMP
272 cpu = cpu_logical_map(smp_processor_id());
273#endif
274
275 cpumask = 1 << cpu;
Russell Kingf27ecac2005-08-18 21:31:00 +0100276 cpumask |= cpumask << 8;
277 cpumask |= cpumask << 16;
278
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530279 writel_relaxed(0, base + GIC_DIST_CTRL);
Russell Kingf27ecac2005-08-18 21:31:00 +0100280
281 /*
282 * Find out how many interrupts are supported.
Russell Kingf27ecac2005-08-18 21:31:00 +0100283 * The GIC only supports up to 1020 interrupt sources.
Russell Kingf27ecac2005-08-18 21:31:00 +0100284 */
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530285 gic_irqs = readl_relaxed(base + GIC_DIST_CTR) & 0x1f;
Pawel Molle6afec92010-11-26 13:45:43 +0100286 gic_irqs = (gic_irqs + 1) * 32;
287 if (gic_irqs > 1020)
288 gic_irqs = 1020;
Russell Kingf27ecac2005-08-18 21:31:00 +0100289
Colin Cross254056f2011-02-10 12:54:10 -0800290 gic->gic_irqs = gic_irqs;
291
Russell Kingf27ecac2005-08-18 21:31:00 +0100292 /*
Marc Zyngier292b2932011-07-20 16:24:14 +0100293 * Nobody would be insane enough to use PPIs on a secondary
294 * GIC, right?
295 */
296 if (gic == &gic_data[0]) {
297 nrppis = (32 - irq_start) & 31;
298
299 /* The GIC only supports up to 16 PPIs. */
300 if (nrppis > 16)
301 BUG();
302
303 ppi_base = gic->irq_offset + 32 - nrppis;
Marc Zyngier292b2932011-07-20 16:24:14 +0100304 }
305
306 pr_info("Configuring GIC with %d sources (%d PPIs)\n",
307 gic_irqs, (gic == &gic_data[0]) ? nrppis : 0);
308
309 /*
Russell Kingf27ecac2005-08-18 21:31:00 +0100310 * Set all global interrupts to be level triggered, active low.
311 */
Pawel Molle6afec92010-11-26 13:45:43 +0100312 for (i = 32; i < gic_irqs; i += 16)
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530313 writel_relaxed(0, base + GIC_DIST_CONFIG + i * 4 / 16);
Russell Kingf27ecac2005-08-18 21:31:00 +0100314
315 /*
316 * Set all global interrupts to this CPU only.
317 */
Pawel Molle6afec92010-11-26 13:45:43 +0100318 for (i = 32; i < gic_irqs; i += 4)
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530319 writel_relaxed(cpumask, base + GIC_DIST_TARGET + i * 4 / 4);
Russell Kingf27ecac2005-08-18 21:31:00 +0100320
321 /*
Russell King9395f6e2010-11-11 23:10:30 +0000322 * Set priority on all global interrupts.
Russell Kingf27ecac2005-08-18 21:31:00 +0100323 */
Pawel Molle6afec92010-11-26 13:45:43 +0100324 for (i = 32; i < gic_irqs; i += 4)
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530325 writel_relaxed(0xa0a0a0a0, base + GIC_DIST_PRI + i * 4 / 4);
Russell Kingf27ecac2005-08-18 21:31:00 +0100326
327 /*
Russell King9395f6e2010-11-11 23:10:30 +0000328 * Disable all interrupts. Leave the PPI and SGIs alone
329 * as these enables are banked registers.
Russell Kingf27ecac2005-08-18 21:31:00 +0100330 */
Pawel Molle6afec92010-11-26 13:45:43 +0100331 for (i = 32; i < gic_irqs; i += 32)
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530332 writel_relaxed(0xffffffff, base + GIC_DIST_ENABLE_CLEAR + i * 4 / 32);
Russell Kingf27ecac2005-08-18 21:31:00 +0100333
334 /*
Pawel Molle6afec92010-11-26 13:45:43 +0100335 * Limit number of interrupts registered to the platform maximum
336 */
Russell Kingbef8f9e2010-12-04 16:50:58 +0000337 irq_limit = gic->irq_offset + gic_irqs;
Pawel Molle6afec92010-11-26 13:45:43 +0100338 if (WARN_ON(irq_limit > NR_IRQS))
339 irq_limit = NR_IRQS;
340
341 /*
Russell Kingf27ecac2005-08-18 21:31:00 +0100342 * Setup the Linux IRQ subsystem.
343 */
Marc Zyngier292b2932011-07-20 16:24:14 +0100344 for (i = 0; i < nrppis; i++) {
345 int ppi = i + ppi_base;
Marc Zyngier292b2932011-07-20 16:24:14 +0100346
347 irq_set_percpu_devid(ppi);
348 irq_set_chip_and_handler(ppi, &gic_chip,
349 handle_percpu_devid_irq);
350 irq_set_chip_data(ppi, gic);
351 set_irq_flags(ppi, IRQF_VALID | IRQF_NOAUTOEN);
Marc Zyngier292b2932011-07-20 16:24:14 +0100352 }
353
354 for (i = irq_start + nrppis; i < irq_limit; i++) {
Will Deacon1a017532011-02-09 12:01:12 +0000355 irq_set_chip_and_handler(i, &gic_chip, handle_fasteoi_irq);
Thomas Gleixner9323f2612011-03-24 13:29:39 +0100356 irq_set_chip_data(i, gic);
Russell Kingf27ecac2005-08-18 21:31:00 +0100357 set_irq_flags(i, IRQF_VALID | IRQF_PROBE);
358 }
359
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530360 writel_relaxed(1, base + GIC_DIST_CTRL);
Russell Kingf27ecac2005-08-18 21:31:00 +0100361}
362
Russell Kingbef8f9e2010-12-04 16:50:58 +0000363static void __cpuinit gic_cpu_init(struct gic_chip_data *gic)
Russell Kingf27ecac2005-08-18 21:31:00 +0100364{
Russell Kingbef8f9e2010-12-04 16:50:58 +0000365 void __iomem *dist_base = gic->dist_base;
366 void __iomem *base = gic->cpu_base;
Russell King9395f6e2010-11-11 23:10:30 +0000367 int i;
368
Russell King9395f6e2010-11-11 23:10:30 +0000369 /*
370 * Deal with the banked PPI and SGI interrupts - disable all
371 * PPI interrupts, ensure all SGI interrupts are enabled.
372 */
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530373 writel_relaxed(0xffff0000, dist_base + GIC_DIST_ENABLE_CLEAR);
374 writel_relaxed(0x0000ffff, dist_base + GIC_DIST_ENABLE_SET);
Russell King9395f6e2010-11-11 23:10:30 +0000375
376 /*
377 * Set priority on PPI and SGI interrupts
378 */
379 for (i = 0; i < 32; i += 4)
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530380 writel_relaxed(0xa0a0a0a0, dist_base + GIC_DIST_PRI + i * 4 / 4);
Russell King9395f6e2010-11-11 23:10:30 +0000381
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530382 writel_relaxed(0xf0, base + GIC_CPU_PRIMASK);
383 writel_relaxed(1, base + GIC_CPU_CTRL);
Russell Kingf27ecac2005-08-18 21:31:00 +0100384}
385
Colin Cross254056f2011-02-10 12:54:10 -0800386#ifdef CONFIG_CPU_PM
387/*
388 * Saves the GIC distributor registers during suspend or idle. Must be called
389 * with interrupts disabled but before powering down the GIC. After calling
390 * this function, no interrupts will be delivered by the GIC, and another
391 * platform-specific wakeup source must be enabled.
392 */
393static void gic_dist_save(unsigned int gic_nr)
394{
395 unsigned int gic_irqs;
396 void __iomem *dist_base;
397 int i;
398
399 if (gic_nr >= MAX_GIC_NR)
400 BUG();
401
402 gic_irqs = gic_data[gic_nr].gic_irqs;
403 dist_base = gic_data[gic_nr].dist_base;
404
405 if (!dist_base)
406 return;
407
408 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 16); i++)
409 gic_data[gic_nr].saved_spi_conf[i] =
410 readl_relaxed(dist_base + GIC_DIST_CONFIG + i * 4);
411
412 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 4); i++)
413 gic_data[gic_nr].saved_spi_target[i] =
414 readl_relaxed(dist_base + GIC_DIST_TARGET + i * 4);
415
416 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 32); i++)
417 gic_data[gic_nr].saved_spi_enable[i] =
418 readl_relaxed(dist_base + GIC_DIST_ENABLE_SET + i * 4);
419}
420
421/*
422 * Restores the GIC distributor registers during resume or when coming out of
423 * idle. Must be called before enabling interrupts. If a level interrupt
424 * that occured while the GIC was suspended is still present, it will be
425 * handled normally, but any edge interrupts that occured will not be seen by
426 * the GIC and need to be handled by the platform-specific wakeup source.
427 */
428static void gic_dist_restore(unsigned int gic_nr)
429{
430 unsigned int gic_irqs;
431 unsigned int i;
432 void __iomem *dist_base;
433
434 if (gic_nr >= MAX_GIC_NR)
435 BUG();
436
437 gic_irqs = gic_data[gic_nr].gic_irqs;
438 dist_base = gic_data[gic_nr].dist_base;
439
440 if (!dist_base)
441 return;
442
443 writel_relaxed(0, dist_base + GIC_DIST_CTRL);
444
445 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 16); i++)
446 writel_relaxed(gic_data[gic_nr].saved_spi_conf[i],
447 dist_base + GIC_DIST_CONFIG + i * 4);
448
449 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 4); i++)
450 writel_relaxed(0xa0a0a0a0,
451 dist_base + GIC_DIST_PRI + i * 4);
452
453 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 4); i++)
454 writel_relaxed(gic_data[gic_nr].saved_spi_target[i],
455 dist_base + GIC_DIST_TARGET + i * 4);
456
457 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 32); i++)
458 writel_relaxed(gic_data[gic_nr].saved_spi_enable[i],
459 dist_base + GIC_DIST_ENABLE_SET + i * 4);
460
461 writel_relaxed(1, dist_base + GIC_DIST_CTRL);
462}
463
464static void gic_cpu_save(unsigned int gic_nr)
465{
466 int i;
467 u32 *ptr;
468 void __iomem *dist_base;
469 void __iomem *cpu_base;
470
471 if (gic_nr >= MAX_GIC_NR)
472 BUG();
473
474 dist_base = gic_data[gic_nr].dist_base;
475 cpu_base = gic_data[gic_nr].cpu_base;
476
477 if (!dist_base || !cpu_base)
478 return;
479
480 ptr = __this_cpu_ptr(gic_data[gic_nr].saved_ppi_enable);
481 for (i = 0; i < DIV_ROUND_UP(32, 32); i++)
482 ptr[i] = readl_relaxed(dist_base + GIC_DIST_ENABLE_SET + i * 4);
483
484 ptr = __this_cpu_ptr(gic_data[gic_nr].saved_ppi_conf);
485 for (i = 0; i < DIV_ROUND_UP(32, 16); i++)
486 ptr[i] = readl_relaxed(dist_base + GIC_DIST_CONFIG + i * 4);
487
488}
489
490static void gic_cpu_restore(unsigned int gic_nr)
491{
492 int i;
493 u32 *ptr;
494 void __iomem *dist_base;
495 void __iomem *cpu_base;
496
497 if (gic_nr >= MAX_GIC_NR)
498 BUG();
499
500 dist_base = gic_data[gic_nr].dist_base;
501 cpu_base = gic_data[gic_nr].cpu_base;
502
503 if (!dist_base || !cpu_base)
504 return;
505
506 ptr = __this_cpu_ptr(gic_data[gic_nr].saved_ppi_enable);
507 for (i = 0; i < DIV_ROUND_UP(32, 32); i++)
508 writel_relaxed(ptr[i], dist_base + GIC_DIST_ENABLE_SET + i * 4);
509
510 ptr = __this_cpu_ptr(gic_data[gic_nr].saved_ppi_conf);
511 for (i = 0; i < DIV_ROUND_UP(32, 16); i++)
512 writel_relaxed(ptr[i], dist_base + GIC_DIST_CONFIG + i * 4);
513
514 for (i = 0; i < DIV_ROUND_UP(32, 4); i++)
515 writel_relaxed(0xa0a0a0a0, dist_base + GIC_DIST_PRI + i * 4);
516
517 writel_relaxed(0xf0, cpu_base + GIC_CPU_PRIMASK);
518 writel_relaxed(1, cpu_base + GIC_CPU_CTRL);
519}
520
521static int gic_notifier(struct notifier_block *self, unsigned long cmd, void *v)
522{
523 int i;
524
525 for (i = 0; i < MAX_GIC_NR; i++) {
526 switch (cmd) {
527 case CPU_PM_ENTER:
528 gic_cpu_save(i);
529 break;
530 case CPU_PM_ENTER_FAILED:
531 case CPU_PM_EXIT:
532 gic_cpu_restore(i);
533 break;
534 case CPU_CLUSTER_PM_ENTER:
535 gic_dist_save(i);
536 break;
537 case CPU_CLUSTER_PM_ENTER_FAILED:
538 case CPU_CLUSTER_PM_EXIT:
539 gic_dist_restore(i);
540 break;
541 }
542 }
543
544 return NOTIFY_OK;
545}
546
547static struct notifier_block gic_notifier_block = {
548 .notifier_call = gic_notifier,
549};
550
551static void __init gic_pm_init(struct gic_chip_data *gic)
552{
553 gic->saved_ppi_enable = __alloc_percpu(DIV_ROUND_UP(32, 32) * 4,
554 sizeof(u32));
555 BUG_ON(!gic->saved_ppi_enable);
556
557 gic->saved_ppi_conf = __alloc_percpu(DIV_ROUND_UP(32, 16) * 4,
558 sizeof(u32));
559 BUG_ON(!gic->saved_ppi_conf);
560
561 cpu_pm_register_notifier(&gic_notifier_block);
562}
563#else
564static void __init gic_pm_init(struct gic_chip_data *gic)
565{
566}
567#endif
568
Russell Kingb580b892010-12-04 15:55:14 +0000569void __init gic_init(unsigned int gic_nr, unsigned int irq_start,
570 void __iomem *dist_base, void __iomem *cpu_base)
571{
Russell Kingbef8f9e2010-12-04 16:50:58 +0000572 struct gic_chip_data *gic;
573
574 BUG_ON(gic_nr >= MAX_GIC_NR);
575
576 gic = &gic_data[gic_nr];
577 gic->dist_base = dist_base;
578 gic->cpu_base = cpu_base;
579 gic->irq_offset = (irq_start - 1) & ~31;
580
Russell Kingff2e27a2010-12-04 16:13:29 +0000581 if (gic_nr == 0)
582 gic_cpu_base_addr = cpu_base;
Russell Kingbef8f9e2010-12-04 16:50:58 +0000583
Colin Cross9c128452011-06-13 00:45:59 +0000584 gic_chip.flags |= gic_arch_extn.flags;
Russell Kingbef8f9e2010-12-04 16:50:58 +0000585 gic_dist_init(gic, irq_start);
586 gic_cpu_init(gic);
Colin Cross254056f2011-02-10 12:54:10 -0800587 gic_pm_init(gic);
Russell Kingb580b892010-12-04 15:55:14 +0000588}
589
Russell King38489532010-12-04 16:01:03 +0000590void __cpuinit gic_secondary_init(unsigned int gic_nr)
591{
Russell Kingbef8f9e2010-12-04 16:50:58 +0000592 BUG_ON(gic_nr >= MAX_GIC_NR);
593
594 gic_cpu_init(&gic_data[gic_nr]);
Russell King38489532010-12-04 16:01:03 +0000595}
596
Russell Kingf27ecac2005-08-18 21:31:00 +0100597#ifdef CONFIG_SMP
Russell King82668102009-05-17 16:20:18 +0100598void gic_raise_softirq(const struct cpumask *mask, unsigned int irq)
Russell Kingf27ecac2005-08-18 21:31:00 +0100599{
Will Deacon267840f2011-08-23 22:20:03 +0100600 int cpu;
601 unsigned long map = 0;
602
603 /* Convert our logical CPU mask into a physical one. */
604 for_each_cpu(cpu, mask)
605 map |= 1 << cpu_logical_map(cpu);
Russell Kingf27ecac2005-08-18 21:31:00 +0100606
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530607 /*
608 * Ensure that stores to Normal memory are visible to the
609 * other CPUs before issuing the IPI.
610 */
611 dsb();
612
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100613 /* this always happens on GIC0 */
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530614 writel_relaxed(map << 16 | irq, gic_data[0].dist_base + GIC_DIST_SOFTINT);
Russell Kingf27ecac2005-08-18 21:31:00 +0100615}
616#endif