blob: 7b315e385ba3a0bba8de91fa851ab03c3ef653e8 [file] [log] [blame]
Russell Kingf27ecac2005-08-18 21:31:00 +01001/*
Russell Kingf27ecac2005-08-18 21:31:00 +01002 * Copyright (C) 2002 ARM Limited, All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * Interrupt architecture for the GIC:
9 *
10 * o There is one Interrupt Distributor, which receives interrupts
11 * from system devices and sends them to the Interrupt Controllers.
12 *
13 * o There is one CPU Interface per CPU, which sends interrupts sent
14 * by the Distributor, and interrupts generated locally, to the
Catalin Marinasb3a1bde2007-02-14 19:14:56 +010015 * associated CPU. The base address of the CPU interface is usually
16 * aliased so that the same address points to different chips depending
17 * on the CPU it is accessed from.
Russell Kingf27ecac2005-08-18 21:31:00 +010018 *
19 * Note that IRQs 0-31 are special - they are local to each CPU.
20 * As such, the enable set/clear, pending set/clear and active bit
21 * registers are banked per-cpu for these sources.
22 */
23#include <linux/init.h>
24#include <linux/kernel.h>
Rob Herringf37a53c2011-10-21 17:14:27 -050025#include <linux/err.h>
Arnd Bergmann7e1efcf2011-11-01 00:28:37 +010026#include <linux/module.h>
Russell Kingf27ecac2005-08-18 21:31:00 +010027#include <linux/list.h>
28#include <linux/smp.h>
Catalin Marinasc0114702013-01-14 18:05:37 +000029#include <linux/cpu.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 Herringb3f7ed02011-09-28 21:27:52 -050033#include <linux/of.h>
34#include <linux/of_address.h>
35#include <linux/of_irq.h>
Tomasz Nowickid60fc382015-03-24 14:02:49 +000036#include <linux/acpi.h>
Rob Herring4294f8ba2011-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>
Catalin Marinasde88cbb2013-01-18 15:31:37 +000041#include <linux/irqchip/chained_irq.h>
Rob Herring520f7bd2012-12-27 13:10:24 -060042#include <linux/irqchip/arm-gic.h>
Tomasz Nowickid60fc382015-03-24 14:02:49 +000043#include <linux/irqchip/arm-gic-acpi.h>
Russell Kingf27ecac2005-08-18 21:31:00 +010044
Tomasz Figa29e697b2014-07-17 17:23:44 +020045#include <asm/cputype.h>
Russell Kingf27ecac2005-08-18 21:31:00 +010046#include <asm/irq.h>
Marc Zyngier562e0022011-09-06 09:56:17 +010047#include <asm/exception.h>
Will Deaconeb504392012-01-20 12:01:12 +010048#include <asm/smp_plat.h>
Russell Kingf27ecac2005-08-18 21:31:00 +010049
Marc Zyngierd51d0af2014-06-30 16:01:30 +010050#include "irq-gic-common.h"
Rob Herring81243e42012-11-20 21:21:40 -060051#include "irqchip.h"
Russell Kingf27ecac2005-08-18 21:31:00 +010052
Marc Zyngierdb0d4db2011-11-12 16:09:49 +000053union gic_base {
54 void __iomem *common_base;
Stephen Boyd68593582014-03-04 17:02:01 -080055 void __percpu * __iomem *percpu_base;
Marc Zyngierdb0d4db2011-11-12 16:09:49 +000056};
57
58struct gic_chip_data {
Marc Zyngierdb0d4db2011-11-12 16:09:49 +000059 union gic_base dist_base;
60 union gic_base cpu_base;
61#ifdef CONFIG_CPU_PM
62 u32 saved_spi_enable[DIV_ROUND_UP(1020, 32)];
63 u32 saved_spi_conf[DIV_ROUND_UP(1020, 16)];
64 u32 saved_spi_target[DIV_ROUND_UP(1020, 4)];
65 u32 __percpu *saved_ppi_enable;
66 u32 __percpu *saved_ppi_conf;
67#endif
Grant Likely75294952012-02-14 14:06:57 -070068 struct irq_domain *domain;
Marc Zyngierdb0d4db2011-11-12 16:09:49 +000069 unsigned int gic_irqs;
70#ifdef CONFIG_GIC_NON_BANKED
71 void __iomem *(*get_base)(union gic_base *);
72#endif
73};
74
Thomas Gleixnerbd31b852009-07-03 08:44:46 -050075static DEFINE_RAW_SPINLOCK(irq_controller_lock);
Russell Kingf27ecac2005-08-18 21:31:00 +010076
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +010077/*
Nicolas Pitre384a2902012-04-11 18:55:48 -040078 * The GIC mapping of CPU interfaces does not necessarily match
79 * the logical CPU numbering. Let's use a mapping as returned
80 * by the GIC itself.
81 */
82#define NR_GIC_CPU_IF 8
83static u8 gic_cpu_map[NR_GIC_CPU_IF] __read_mostly;
84
85/*
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +010086 * Supported arch specific GIC irq extension.
87 * Default make them NULL.
88 */
89struct irq_chip gic_arch_extn = {
Will Deacon1a017532011-02-09 12:01:12 +000090 .irq_eoi = NULL,
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +010091 .irq_mask = NULL,
92 .irq_unmask = NULL,
93 .irq_retrigger = NULL,
94 .irq_set_type = NULL,
95 .irq_set_wake = NULL,
96};
97
Catalin Marinasb3a1bde2007-02-14 19:14:56 +010098#ifndef MAX_GIC_NR
99#define MAX_GIC_NR 1
100#endif
101
Russell Kingbef8f9e2010-12-04 16:50:58 +0000102static struct gic_chip_data gic_data[MAX_GIC_NR] __read_mostly;
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100103
Marc Zyngierdb0d4db2011-11-12 16:09:49 +0000104#ifdef CONFIG_GIC_NON_BANKED
105static void __iomem *gic_get_percpu_base(union gic_base *base)
106{
Christoph Lameter513d1a22014-09-02 10:00:07 -0500107 return raw_cpu_read(*base->percpu_base);
Marc Zyngierdb0d4db2011-11-12 16:09:49 +0000108}
109
110static void __iomem *gic_get_common_base(union gic_base *base)
111{
112 return base->common_base;
113}
114
115static inline void __iomem *gic_data_dist_base(struct gic_chip_data *data)
116{
117 return data->get_base(&data->dist_base);
118}
119
120static inline void __iomem *gic_data_cpu_base(struct gic_chip_data *data)
121{
122 return data->get_base(&data->cpu_base);
123}
124
125static inline void gic_set_base_accessor(struct gic_chip_data *data,
126 void __iomem *(*f)(union gic_base *))
127{
128 data->get_base = f;
129}
130#else
131#define gic_data_dist_base(d) ((d)->dist_base.common_base)
132#define gic_data_cpu_base(d) ((d)->cpu_base.common_base)
Sachin Kamat46f101d2013-03-13 15:05:15 +0530133#define gic_set_base_accessor(d, f)
Marc Zyngierdb0d4db2011-11-12 16:09:49 +0000134#endif
135
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100136static inline void __iomem *gic_dist_base(struct irq_data *d)
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100137{
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100138 struct gic_chip_data *gic_data = irq_data_get_irq_chip_data(d);
Marc Zyngierdb0d4db2011-11-12 16:09:49 +0000139 return gic_data_dist_base(gic_data);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100140}
141
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100142static inline void __iomem *gic_cpu_base(struct irq_data *d)
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100143{
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100144 struct gic_chip_data *gic_data = irq_data_get_irq_chip_data(d);
Marc Zyngierdb0d4db2011-11-12 16:09:49 +0000145 return gic_data_cpu_base(gic_data);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100146}
147
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100148static inline unsigned int gic_irq(struct irq_data *d)
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100149{
Rob Herring4294f8ba2011-09-28 21:25:31 -0500150 return d->hwirq;
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100151}
152
Russell Kingf27ecac2005-08-18 21:31:00 +0100153/*
154 * Routines to acknowledge, disable and enable interrupts
Russell Kingf27ecac2005-08-18 21:31:00 +0100155 */
Marc Zyngier56717802015-03-18 11:01:23 +0000156static void gic_poke_irq(struct irq_data *d, u32 offset)
Russell Kingf27ecac2005-08-18 21:31:00 +0100157{
Rob Herring4294f8ba2011-09-28 21:25:31 -0500158 u32 mask = 1 << (gic_irq(d) % 32);
Marc Zyngier56717802015-03-18 11:01:23 +0000159 writel_relaxed(mask, gic_dist_base(d) + offset + (gic_irq(d) / 32) * 4);
160}
161
162static int gic_peek_irq(struct irq_data *d, u32 offset)
163{
164 u32 mask = 1 << (gic_irq(d) % 32);
165 return !!(readl_relaxed(gic_dist_base(d) + offset + (gic_irq(d) / 32) * 4) & mask);
166}
167
168static void gic_mask_irq(struct irq_data *d)
169{
Marc Zyngiercf613872015-03-06 16:37:44 +0000170 unsigned long flags;
Thomas Gleixnerc4bfa282006-07-01 22:32:14 +0100171
Marc Zyngiercf613872015-03-06 16:37:44 +0000172 raw_spin_lock_irqsave(&irq_controller_lock, flags);
Marc Zyngier56717802015-03-18 11:01:23 +0000173 gic_poke_irq(d, GIC_DIST_ENABLE_CLEAR);
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100174 if (gic_arch_extn.irq_mask)
175 gic_arch_extn.irq_mask(d);
Marc Zyngiercf613872015-03-06 16:37:44 +0000176 raw_spin_unlock_irqrestore(&irq_controller_lock, flags);
Russell Kingf27ecac2005-08-18 21:31:00 +0100177}
178
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100179static void gic_unmask_irq(struct irq_data *d)
Russell Kingf27ecac2005-08-18 21:31:00 +0100180{
Marc Zyngiercf613872015-03-06 16:37:44 +0000181 unsigned long flags;
Thomas Gleixnerc4bfa282006-07-01 22:32:14 +0100182
Marc Zyngiercf613872015-03-06 16:37:44 +0000183 raw_spin_lock_irqsave(&irq_controller_lock, flags);
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100184 if (gic_arch_extn.irq_unmask)
185 gic_arch_extn.irq_unmask(d);
Marc Zyngier56717802015-03-18 11:01:23 +0000186 gic_poke_irq(d, GIC_DIST_ENABLE_SET);
Marc Zyngiercf613872015-03-06 16:37:44 +0000187 raw_spin_unlock_irqrestore(&irq_controller_lock, flags);
Russell Kingf27ecac2005-08-18 21:31:00 +0100188}
189
Will Deacon1a017532011-02-09 12:01:12 +0000190static void gic_eoi_irq(struct irq_data *d)
191{
192 if (gic_arch_extn.irq_eoi) {
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500193 raw_spin_lock(&irq_controller_lock);
Will Deacon1a017532011-02-09 12:01:12 +0000194 gic_arch_extn.irq_eoi(d);
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500195 raw_spin_unlock(&irq_controller_lock);
Will Deacon1a017532011-02-09 12:01:12 +0000196 }
197
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530198 writel_relaxed(gic_irq(d), gic_cpu_base(d) + GIC_CPU_EOI);
Will Deacon1a017532011-02-09 12:01:12 +0000199}
200
Marc Zyngier56717802015-03-18 11:01:23 +0000201static int gic_irq_set_irqchip_state(struct irq_data *d,
202 enum irqchip_irq_state which, bool val)
203{
204 u32 reg;
205
206 switch (which) {
207 case IRQCHIP_STATE_PENDING:
208 reg = val ? GIC_DIST_PENDING_SET : GIC_DIST_PENDING_CLEAR;
209 break;
210
211 case IRQCHIP_STATE_ACTIVE:
212 reg = val ? GIC_DIST_ACTIVE_SET : GIC_DIST_ACTIVE_CLEAR;
213 break;
214
215 case IRQCHIP_STATE_MASKED:
216 reg = val ? GIC_DIST_ENABLE_CLEAR : GIC_DIST_ENABLE_SET;
217 break;
218
219 default:
220 return -EINVAL;
221 }
222
223 gic_poke_irq(d, reg);
224 return 0;
225}
226
227static int gic_irq_get_irqchip_state(struct irq_data *d,
228 enum irqchip_irq_state which, bool *val)
229{
230 switch (which) {
231 case IRQCHIP_STATE_PENDING:
232 *val = gic_peek_irq(d, GIC_DIST_PENDING_SET);
233 break;
234
235 case IRQCHIP_STATE_ACTIVE:
236 *val = gic_peek_irq(d, GIC_DIST_ACTIVE_SET);
237 break;
238
239 case IRQCHIP_STATE_MASKED:
240 *val = !gic_peek_irq(d, GIC_DIST_ENABLE_SET);
241 break;
242
243 default:
244 return -EINVAL;
245 }
246
247 return 0;
248}
249
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100250static int gic_set_type(struct irq_data *d, unsigned int type)
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100251{
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100252 void __iomem *base = gic_dist_base(d);
253 unsigned int gicirq = gic_irq(d);
Marc Zyngiercf613872015-03-06 16:37:44 +0000254 unsigned long flags;
Liviu Dudaufb7e7de2015-01-20 16:52:59 +0000255 int ret;
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100256
257 /* Interrupt configuration for SGIs can't be changed */
258 if (gicirq < 16)
259 return -EINVAL;
260
Liviu Dudaufb7e7de2015-01-20 16:52:59 +0000261 /* SPIs have restrictions on the supported types */
262 if (gicirq >= 32 && type != IRQ_TYPE_LEVEL_HIGH &&
263 type != IRQ_TYPE_EDGE_RISING)
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100264 return -EINVAL;
265
Marc Zyngiercf613872015-03-06 16:37:44 +0000266 raw_spin_lock_irqsave(&irq_controller_lock, flags);
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100267
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100268 if (gic_arch_extn.irq_set_type)
269 gic_arch_extn.irq_set_type(d, type);
270
Liviu Dudaufb7e7de2015-01-20 16:52:59 +0000271 ret = gic_configure_irq(gicirq, type, base, NULL);
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100272
Marc Zyngiercf613872015-03-06 16:37:44 +0000273 raw_spin_unlock_irqrestore(&irq_controller_lock, flags);
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100274
Liviu Dudaufb7e7de2015-01-20 16:52:59 +0000275 return ret;
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100276}
277
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100278static int gic_retrigger(struct irq_data *d)
279{
280 if (gic_arch_extn.irq_retrigger)
281 return gic_arch_extn.irq_retrigger(d);
282
Abhijeet Dharmapurikarbad9a432013-03-19 16:05:49 -0700283 /* the genirq layer expects 0 if we can't retrigger in hardware */
284 return 0;
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100285}
286
Catalin Marinasa06f5462005-09-30 16:07:05 +0100287#ifdef CONFIG_SMP
Russell Kingc1917892011-01-23 12:12:01 +0000288static int gic_set_affinity(struct irq_data *d, const struct cpumask *mask_val,
289 bool force)
Russell Kingf27ecac2005-08-18 21:31:00 +0100290{
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100291 void __iomem *reg = gic_dist_base(d) + GIC_DIST_TARGET + (gic_irq(d) & ~3);
Thomas Gleixnerffde1de2014-04-16 14:36:44 +0000292 unsigned int cpu, shift = (gic_irq(d) % 4) * 8;
Russell Kingc1917892011-01-23 12:12:01 +0000293 u32 val, mask, bit;
Marc Zyngiercf613872015-03-06 16:37:44 +0000294 unsigned long flags;
Russell Kingc1917892011-01-23 12:12:01 +0000295
Thomas Gleixnerffde1de2014-04-16 14:36:44 +0000296 if (!force)
297 cpu = cpumask_any_and(mask_val, cpu_online_mask);
298 else
299 cpu = cpumask_first(mask_val);
300
Nicolas Pitre384a2902012-04-11 18:55:48 -0400301 if (cpu >= NR_GIC_CPU_IF || cpu >= nr_cpu_ids)
Russell Kingc1917892011-01-23 12:12:01 +0000302 return -EINVAL;
303
Marc Zyngiercf613872015-03-06 16:37:44 +0000304 raw_spin_lock_irqsave(&irq_controller_lock, flags);
Russell Kingc1917892011-01-23 12:12:01 +0000305 mask = 0xff << shift;
Nicolas Pitre384a2902012-04-11 18:55:48 -0400306 bit = gic_cpu_map[cpu] << shift;
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530307 val = readl_relaxed(reg) & ~mask;
308 writel_relaxed(val | bit, reg);
Marc Zyngiercf613872015-03-06 16:37:44 +0000309 raw_spin_unlock_irqrestore(&irq_controller_lock, flags);
Yinghai Lud5dedd42009-04-27 17:59:21 -0700310
Russell King5dfc54e2011-07-21 15:00:57 +0100311 return IRQ_SET_MASK_OK;
Russell Kingf27ecac2005-08-18 21:31:00 +0100312}
Catalin Marinasa06f5462005-09-30 16:07:05 +0100313#endif
Russell Kingf27ecac2005-08-18 21:31:00 +0100314
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100315#ifdef CONFIG_PM
316static int gic_set_wake(struct irq_data *d, unsigned int on)
317{
318 int ret = -ENXIO;
319
320 if (gic_arch_extn.irq_set_wake)
321 ret = gic_arch_extn.irq_set_wake(d, on);
322
323 return ret;
324}
325
326#else
327#define gic_set_wake NULL
328#endif
329
Stephen Boyd8783dd32014-03-04 16:40:30 -0800330static void __exception_irq_entry gic_handle_irq(struct pt_regs *regs)
Marc Zyngier562e0022011-09-06 09:56:17 +0100331{
332 u32 irqstat, irqnr;
333 struct gic_chip_data *gic = &gic_data[0];
334 void __iomem *cpu_base = gic_data_cpu_base(gic);
335
336 do {
337 irqstat = readl_relaxed(cpu_base + GIC_CPU_INTACK);
Haojian Zhuangb8802f72014-05-11 16:05:58 +0800338 irqnr = irqstat & GICC_IAR_INT_ID_MASK;
Marc Zyngier562e0022011-09-06 09:56:17 +0100339
340 if (likely(irqnr > 15 && irqnr < 1021)) {
Marc Zyngier60031b42014-08-26 11:03:20 +0100341 handle_domain_irq(gic->domain, irqnr, regs);
Marc Zyngier562e0022011-09-06 09:56:17 +0100342 continue;
343 }
344 if (irqnr < 16) {
345 writel_relaxed(irqstat, cpu_base + GIC_CPU_EOI);
346#ifdef CONFIG_SMP
347 handle_IPI(irqnr, regs);
348#endif
349 continue;
350 }
351 break;
352 } while (1);
353}
354
Russell King0f347bb2007-05-17 10:11:34 +0100355static void gic_handle_cascade_irq(unsigned int irq, struct irq_desc *desc)
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100356{
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100357 struct gic_chip_data *chip_data = irq_get_handler_data(irq);
358 struct irq_chip *chip = irq_get_chip(irq);
Russell King0f347bb2007-05-17 10:11:34 +0100359 unsigned int cascade_irq, gic_irq;
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100360 unsigned long status;
361
Will Deacon1a017532011-02-09 12:01:12 +0000362 chained_irq_enter(chip, desc);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100363
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500364 raw_spin_lock(&irq_controller_lock);
Marc Zyngierdb0d4db2011-11-12 16:09:49 +0000365 status = readl_relaxed(gic_data_cpu_base(chip_data) + GIC_CPU_INTACK);
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500366 raw_spin_unlock(&irq_controller_lock);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100367
Feng Kane5f81532014-07-30 14:56:58 -0700368 gic_irq = (status & GICC_IAR_INT_ID_MASK);
369 if (gic_irq == GICC_INT_SPURIOUS)
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100370 goto out;
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100371
Grant Likely75294952012-02-14 14:06:57 -0700372 cascade_irq = irq_find_mapping(chip_data->domain, gic_irq);
373 if (unlikely(gic_irq < 32 || gic_irq > 1020))
Catalin Marinasaec00952013-01-14 17:53:39 +0000374 handle_bad_irq(cascade_irq, desc);
Russell King0f347bb2007-05-17 10:11:34 +0100375 else
376 generic_handle_irq(cascade_irq);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100377
378 out:
Will Deacon1a017532011-02-09 12:01:12 +0000379 chained_irq_exit(chip, desc);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100380}
381
David Brownell38c677c2006-08-01 22:26:25 +0100382static struct irq_chip gic_chip = {
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100383 .name = "GIC",
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100384 .irq_mask = gic_mask_irq,
385 .irq_unmask = gic_unmask_irq,
Will Deacon1a017532011-02-09 12:01:12 +0000386 .irq_eoi = gic_eoi_irq,
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100387 .irq_set_type = gic_set_type,
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100388 .irq_retrigger = gic_retrigger,
Russell Kingf27ecac2005-08-18 21:31:00 +0100389#ifdef CONFIG_SMP
Russell Kingc1917892011-01-23 12:12:01 +0000390 .irq_set_affinity = gic_set_affinity,
Russell Kingf27ecac2005-08-18 21:31:00 +0100391#endif
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100392 .irq_set_wake = gic_set_wake,
Marc Zyngier56717802015-03-18 11:01:23 +0000393 .irq_get_irqchip_state = gic_irq_get_irqchip_state,
394 .irq_set_irqchip_state = gic_irq_set_irqchip_state,
Russell Kingf27ecac2005-08-18 21:31:00 +0100395};
396
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100397void __init gic_cascade_irq(unsigned int gic_nr, unsigned int irq)
398{
399 if (gic_nr >= MAX_GIC_NR)
400 BUG();
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100401 if (irq_set_handler_data(irq, &gic_data[gic_nr]) != 0)
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100402 BUG();
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100403 irq_set_chained_handler(irq, gic_handle_cascade_irq);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100404}
405
Russell King2bb31352013-01-30 23:49:57 +0000406static u8 gic_get_cpumask(struct gic_chip_data *gic)
407{
408 void __iomem *base = gic_data_dist_base(gic);
409 u32 mask, i;
410
411 for (i = mask = 0; i < 32; i += 4) {
412 mask = readl_relaxed(base + GIC_DIST_TARGET + i);
413 mask |= mask >> 16;
414 mask |= mask >> 8;
415 if (mask)
416 break;
417 }
418
Stephen Boyd6e3aca42015-03-11 23:21:31 -0700419 if (!mask && num_possible_cpus() > 1)
Russell King2bb31352013-01-30 23:49:57 +0000420 pr_crit("GIC CPU mask not found - kernel will fail to boot.\n");
421
422 return mask;
423}
424
Feng Kan32289502014-07-30 14:56:59 -0700425static void gic_cpu_if_up(void)
426{
427 void __iomem *cpu_base = gic_data_cpu_base(&gic_data[0]);
428 u32 bypass = 0;
429
430 /*
431 * Preserve bypass disable bits to be written back later
432 */
433 bypass = readl(cpu_base + GIC_CPU_CTRL);
434 bypass &= GICC_DIS_BYPASS_MASK;
435
436 writel_relaxed(bypass | GICC_ENABLE, cpu_base + GIC_CPU_CTRL);
437}
438
439
Rob Herring4294f8ba2011-09-28 21:25:31 -0500440static void __init gic_dist_init(struct gic_chip_data *gic)
Russell Kingf27ecac2005-08-18 21:31:00 +0100441{
Grant Likely75294952012-02-14 14:06:57 -0700442 unsigned int i;
Will Deacon267840f2011-08-23 22:20:03 +0100443 u32 cpumask;
Rob Herring4294f8ba2011-09-28 21:25:31 -0500444 unsigned int gic_irqs = gic->gic_irqs;
Marc Zyngierdb0d4db2011-11-12 16:09:49 +0000445 void __iomem *base = gic_data_dist_base(gic);
Russell Kingf27ecac2005-08-18 21:31:00 +0100446
Feng Kane5f81532014-07-30 14:56:58 -0700447 writel_relaxed(GICD_DISABLE, base + GIC_DIST_CTRL);
Russell Kingf27ecac2005-08-18 21:31:00 +0100448
449 /*
Russell Kingf27ecac2005-08-18 21:31:00 +0100450 * Set all global interrupts to this CPU only.
451 */
Russell King2bb31352013-01-30 23:49:57 +0000452 cpumask = gic_get_cpumask(gic);
453 cpumask |= cpumask << 8;
454 cpumask |= cpumask << 16;
Pawel Molle6afec92010-11-26 13:45:43 +0100455 for (i = 32; i < gic_irqs; i += 4)
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530456 writel_relaxed(cpumask, base + GIC_DIST_TARGET + i * 4 / 4);
Russell Kingf27ecac2005-08-18 21:31:00 +0100457
Marc Zyngierd51d0af2014-06-30 16:01:30 +0100458 gic_dist_config(base, gic_irqs, NULL);
Russell Kingf27ecac2005-08-18 21:31:00 +0100459
Feng Kane5f81532014-07-30 14:56:58 -0700460 writel_relaxed(GICD_ENABLE, base + GIC_DIST_CTRL);
Russell Kingf27ecac2005-08-18 21:31:00 +0100461}
462
Paul Gortmaker8c37bb32013-06-19 11:32:08 -0400463static void gic_cpu_init(struct gic_chip_data *gic)
Russell Kingf27ecac2005-08-18 21:31:00 +0100464{
Marc Zyngierdb0d4db2011-11-12 16:09:49 +0000465 void __iomem *dist_base = gic_data_dist_base(gic);
466 void __iomem *base = gic_data_cpu_base(gic);
Nicolas Pitre384a2902012-04-11 18:55:48 -0400467 unsigned int cpu_mask, cpu = smp_processor_id();
Russell King9395f6e2010-11-11 23:10:30 +0000468 int i;
469
Russell King9395f6e2010-11-11 23:10:30 +0000470 /*
Nicolas Pitre384a2902012-04-11 18:55:48 -0400471 * Get what the GIC says our CPU mask is.
472 */
473 BUG_ON(cpu >= NR_GIC_CPU_IF);
Russell King2bb31352013-01-30 23:49:57 +0000474 cpu_mask = gic_get_cpumask(gic);
Nicolas Pitre384a2902012-04-11 18:55:48 -0400475 gic_cpu_map[cpu] = cpu_mask;
476
477 /*
478 * Clear our mask from the other map entries in case they're
479 * still undefined.
480 */
481 for (i = 0; i < NR_GIC_CPU_IF; i++)
482 if (i != cpu)
483 gic_cpu_map[i] &= ~cpu_mask;
484
Marc Zyngierd51d0af2014-06-30 16:01:30 +0100485 gic_cpu_config(dist_base, NULL);
Russell King9395f6e2010-11-11 23:10:30 +0000486
Feng Kane5f81532014-07-30 14:56:58 -0700487 writel_relaxed(GICC_INT_PRI_THRESHOLD, base + GIC_CPU_PRIMASK);
Feng Kan32289502014-07-30 14:56:59 -0700488 gic_cpu_if_up();
Russell Kingf27ecac2005-08-18 21:31:00 +0100489}
490
Nicolas Pitre10d9eb82013-03-19 23:59:04 -0400491void gic_cpu_if_down(void)
492{
493 void __iomem *cpu_base = gic_data_cpu_base(&gic_data[0]);
Feng Kan32289502014-07-30 14:56:59 -0700494 u32 val = 0;
495
496 val = readl(cpu_base + GIC_CPU_CTRL);
497 val &= ~GICC_ENABLE;
498 writel_relaxed(val, cpu_base + GIC_CPU_CTRL);
Nicolas Pitre10d9eb82013-03-19 23:59:04 -0400499}
500
Colin Cross254056f2011-02-10 12:54:10 -0800501#ifdef CONFIG_CPU_PM
502/*
503 * Saves the GIC distributor registers during suspend or idle. Must be called
504 * with interrupts disabled but before powering down the GIC. After calling
505 * this function, no interrupts will be delivered by the GIC, and another
506 * platform-specific wakeup source must be enabled.
507 */
508static void gic_dist_save(unsigned int gic_nr)
509{
510 unsigned int gic_irqs;
511 void __iomem *dist_base;
512 int i;
513
514 if (gic_nr >= MAX_GIC_NR)
515 BUG();
516
517 gic_irqs = gic_data[gic_nr].gic_irqs;
Marc Zyngierdb0d4db2011-11-12 16:09:49 +0000518 dist_base = gic_data_dist_base(&gic_data[gic_nr]);
Colin Cross254056f2011-02-10 12:54:10 -0800519
520 if (!dist_base)
521 return;
522
523 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 16); i++)
524 gic_data[gic_nr].saved_spi_conf[i] =
525 readl_relaxed(dist_base + GIC_DIST_CONFIG + i * 4);
526
527 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 4); i++)
528 gic_data[gic_nr].saved_spi_target[i] =
529 readl_relaxed(dist_base + GIC_DIST_TARGET + i * 4);
530
531 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 32); i++)
532 gic_data[gic_nr].saved_spi_enable[i] =
533 readl_relaxed(dist_base + GIC_DIST_ENABLE_SET + i * 4);
534}
535
536/*
537 * Restores the GIC distributor registers during resume or when coming out of
538 * idle. Must be called before enabling interrupts. If a level interrupt
539 * that occured while the GIC was suspended is still present, it will be
540 * handled normally, but any edge interrupts that occured will not be seen by
541 * the GIC and need to be handled by the platform-specific wakeup source.
542 */
543static void gic_dist_restore(unsigned int gic_nr)
544{
545 unsigned int gic_irqs;
546 unsigned int i;
547 void __iomem *dist_base;
548
549 if (gic_nr >= MAX_GIC_NR)
550 BUG();
551
552 gic_irqs = gic_data[gic_nr].gic_irqs;
Marc Zyngierdb0d4db2011-11-12 16:09:49 +0000553 dist_base = gic_data_dist_base(&gic_data[gic_nr]);
Colin Cross254056f2011-02-10 12:54:10 -0800554
555 if (!dist_base)
556 return;
557
Feng Kane5f81532014-07-30 14:56:58 -0700558 writel_relaxed(GICD_DISABLE, dist_base + GIC_DIST_CTRL);
Colin Cross254056f2011-02-10 12:54:10 -0800559
560 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 16); i++)
561 writel_relaxed(gic_data[gic_nr].saved_spi_conf[i],
562 dist_base + GIC_DIST_CONFIG + i * 4);
563
564 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 4); i++)
Feng Kane5f81532014-07-30 14:56:58 -0700565 writel_relaxed(GICD_INT_DEF_PRI_X4,
Colin Cross254056f2011-02-10 12:54:10 -0800566 dist_base + GIC_DIST_PRI + i * 4);
567
568 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 4); i++)
569 writel_relaxed(gic_data[gic_nr].saved_spi_target[i],
570 dist_base + GIC_DIST_TARGET + i * 4);
571
572 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 32); i++)
573 writel_relaxed(gic_data[gic_nr].saved_spi_enable[i],
574 dist_base + GIC_DIST_ENABLE_SET + i * 4);
575
Feng Kane5f81532014-07-30 14:56:58 -0700576 writel_relaxed(GICD_ENABLE, dist_base + GIC_DIST_CTRL);
Colin Cross254056f2011-02-10 12:54:10 -0800577}
578
579static void gic_cpu_save(unsigned int gic_nr)
580{
581 int i;
582 u32 *ptr;
583 void __iomem *dist_base;
584 void __iomem *cpu_base;
585
586 if (gic_nr >= MAX_GIC_NR)
587 BUG();
588
Marc Zyngierdb0d4db2011-11-12 16:09:49 +0000589 dist_base = gic_data_dist_base(&gic_data[gic_nr]);
590 cpu_base = gic_data_cpu_base(&gic_data[gic_nr]);
Colin Cross254056f2011-02-10 12:54:10 -0800591
592 if (!dist_base || !cpu_base)
593 return;
594
Christoph Lameter532d0d02014-08-17 12:30:39 -0500595 ptr = raw_cpu_ptr(gic_data[gic_nr].saved_ppi_enable);
Colin Cross254056f2011-02-10 12:54:10 -0800596 for (i = 0; i < DIV_ROUND_UP(32, 32); i++)
597 ptr[i] = readl_relaxed(dist_base + GIC_DIST_ENABLE_SET + i * 4);
598
Christoph Lameter532d0d02014-08-17 12:30:39 -0500599 ptr = raw_cpu_ptr(gic_data[gic_nr].saved_ppi_conf);
Colin Cross254056f2011-02-10 12:54:10 -0800600 for (i = 0; i < DIV_ROUND_UP(32, 16); i++)
601 ptr[i] = readl_relaxed(dist_base + GIC_DIST_CONFIG + i * 4);
602
603}
604
605static void gic_cpu_restore(unsigned int gic_nr)
606{
607 int i;
608 u32 *ptr;
609 void __iomem *dist_base;
610 void __iomem *cpu_base;
611
612 if (gic_nr >= MAX_GIC_NR)
613 BUG();
614
Marc Zyngierdb0d4db2011-11-12 16:09:49 +0000615 dist_base = gic_data_dist_base(&gic_data[gic_nr]);
616 cpu_base = gic_data_cpu_base(&gic_data[gic_nr]);
Colin Cross254056f2011-02-10 12:54:10 -0800617
618 if (!dist_base || !cpu_base)
619 return;
620
Christoph Lameter532d0d02014-08-17 12:30:39 -0500621 ptr = raw_cpu_ptr(gic_data[gic_nr].saved_ppi_enable);
Colin Cross254056f2011-02-10 12:54:10 -0800622 for (i = 0; i < DIV_ROUND_UP(32, 32); i++)
623 writel_relaxed(ptr[i], dist_base + GIC_DIST_ENABLE_SET + i * 4);
624
Christoph Lameter532d0d02014-08-17 12:30:39 -0500625 ptr = raw_cpu_ptr(gic_data[gic_nr].saved_ppi_conf);
Colin Cross254056f2011-02-10 12:54:10 -0800626 for (i = 0; i < DIV_ROUND_UP(32, 16); i++)
627 writel_relaxed(ptr[i], dist_base + GIC_DIST_CONFIG + i * 4);
628
629 for (i = 0; i < DIV_ROUND_UP(32, 4); i++)
Feng Kane5f81532014-07-30 14:56:58 -0700630 writel_relaxed(GICD_INT_DEF_PRI_X4,
631 dist_base + GIC_DIST_PRI + i * 4);
Colin Cross254056f2011-02-10 12:54:10 -0800632
Feng Kane5f81532014-07-30 14:56:58 -0700633 writel_relaxed(GICC_INT_PRI_THRESHOLD, cpu_base + GIC_CPU_PRIMASK);
Feng Kan32289502014-07-30 14:56:59 -0700634 gic_cpu_if_up();
Colin Cross254056f2011-02-10 12:54:10 -0800635}
636
637static int gic_notifier(struct notifier_block *self, unsigned long cmd, void *v)
638{
639 int i;
640
641 for (i = 0; i < MAX_GIC_NR; i++) {
Marc Zyngierdb0d4db2011-11-12 16:09:49 +0000642#ifdef CONFIG_GIC_NON_BANKED
643 /* Skip over unused GICs */
644 if (!gic_data[i].get_base)
645 continue;
646#endif
Colin Cross254056f2011-02-10 12:54:10 -0800647 switch (cmd) {
648 case CPU_PM_ENTER:
649 gic_cpu_save(i);
650 break;
651 case CPU_PM_ENTER_FAILED:
652 case CPU_PM_EXIT:
653 gic_cpu_restore(i);
654 break;
655 case CPU_CLUSTER_PM_ENTER:
656 gic_dist_save(i);
657 break;
658 case CPU_CLUSTER_PM_ENTER_FAILED:
659 case CPU_CLUSTER_PM_EXIT:
660 gic_dist_restore(i);
661 break;
662 }
663 }
664
665 return NOTIFY_OK;
666}
667
668static struct notifier_block gic_notifier_block = {
669 .notifier_call = gic_notifier,
670};
671
672static void __init gic_pm_init(struct gic_chip_data *gic)
673{
674 gic->saved_ppi_enable = __alloc_percpu(DIV_ROUND_UP(32, 32) * 4,
675 sizeof(u32));
676 BUG_ON(!gic->saved_ppi_enable);
677
678 gic->saved_ppi_conf = __alloc_percpu(DIV_ROUND_UP(32, 16) * 4,
679 sizeof(u32));
680 BUG_ON(!gic->saved_ppi_conf);
681
Marc Zyngierabdd7b92011-11-25 17:58:19 +0100682 if (gic == &gic_data[0])
683 cpu_pm_register_notifier(&gic_notifier_block);
Colin Cross254056f2011-02-10 12:54:10 -0800684}
685#else
686static void __init gic_pm_init(struct gic_chip_data *gic)
687{
688}
689#endif
690
Rob Herringb1cffeb2012-11-26 15:05:48 -0600691#ifdef CONFIG_SMP
Stephen Boyd68593582014-03-04 17:02:01 -0800692static void gic_raise_softirq(const struct cpumask *mask, unsigned int irq)
Rob Herringb1cffeb2012-11-26 15:05:48 -0600693{
694 int cpu;
Nicolas Pitre1a6b69b2012-04-12 01:40:31 -0400695 unsigned long flags, map = 0;
696
697 raw_spin_lock_irqsave(&irq_controller_lock, flags);
Rob Herringb1cffeb2012-11-26 15:05:48 -0600698
699 /* Convert our logical CPU mask into a physical one. */
700 for_each_cpu(cpu, mask)
Javi Merino91bdf0d2013-02-19 13:52:22 +0000701 map |= gic_cpu_map[cpu];
Rob Herringb1cffeb2012-11-26 15:05:48 -0600702
703 /*
704 * Ensure that stores to Normal memory are visible to the
Will Deacon8adbf572014-02-20 17:42:07 +0000705 * other CPUs before they observe us issuing the IPI.
Rob Herringb1cffeb2012-11-26 15:05:48 -0600706 */
Will Deacon8adbf572014-02-20 17:42:07 +0000707 dmb(ishst);
Rob Herringb1cffeb2012-11-26 15:05:48 -0600708
709 /* this always happens on GIC0 */
710 writel_relaxed(map << 16 | irq, gic_data_dist_base(&gic_data[0]) + GIC_DIST_SOFTINT);
Nicolas Pitre1a6b69b2012-04-12 01:40:31 -0400711
712 raw_spin_unlock_irqrestore(&irq_controller_lock, flags);
713}
714#endif
715
716#ifdef CONFIG_BL_SWITCHER
717/*
Nicolas Pitre14d2ca62012-11-28 18:48:19 -0500718 * gic_send_sgi - send a SGI directly to given CPU interface number
719 *
720 * cpu_id: the ID for the destination CPU interface
721 * irq: the IPI number to send a SGI for
722 */
723void gic_send_sgi(unsigned int cpu_id, unsigned int irq)
724{
725 BUG_ON(cpu_id >= NR_GIC_CPU_IF);
726 cpu_id = 1 << cpu_id;
727 /* this always happens on GIC0 */
728 writel_relaxed((cpu_id << 16) | irq, gic_data_dist_base(&gic_data[0]) + GIC_DIST_SOFTINT);
729}
730
731/*
Nicolas Pitreed967622012-07-05 21:33:26 -0400732 * gic_get_cpu_id - get the CPU interface ID for the specified CPU
733 *
734 * @cpu: the logical CPU number to get the GIC ID for.
735 *
736 * Return the CPU interface ID for the given logical CPU number,
737 * or -1 if the CPU number is too large or the interface ID is
738 * unknown (more than one bit set).
739 */
740int gic_get_cpu_id(unsigned int cpu)
741{
742 unsigned int cpu_bit;
743
744 if (cpu >= NR_GIC_CPU_IF)
745 return -1;
746 cpu_bit = gic_cpu_map[cpu];
747 if (cpu_bit & (cpu_bit - 1))
748 return -1;
749 return __ffs(cpu_bit);
750}
751
752/*
Nicolas Pitre1a6b69b2012-04-12 01:40:31 -0400753 * gic_migrate_target - migrate IRQs to another CPU interface
754 *
755 * @new_cpu_id: the CPU target ID to migrate IRQs to
756 *
757 * Migrate all peripheral interrupts with a target matching the current CPU
758 * to the interface corresponding to @new_cpu_id. The CPU interface mapping
759 * is also updated. Targets to other CPU interfaces are unchanged.
760 * This must be called with IRQs locally disabled.
761 */
762void gic_migrate_target(unsigned int new_cpu_id)
763{
764 unsigned int cur_cpu_id, gic_irqs, gic_nr = 0;
765 void __iomem *dist_base;
766 int i, ror_val, cpu = smp_processor_id();
767 u32 val, cur_target_mask, active_mask;
768
769 if (gic_nr >= MAX_GIC_NR)
770 BUG();
771
772 dist_base = gic_data_dist_base(&gic_data[gic_nr]);
773 if (!dist_base)
774 return;
775 gic_irqs = gic_data[gic_nr].gic_irqs;
776
777 cur_cpu_id = __ffs(gic_cpu_map[cpu]);
778 cur_target_mask = 0x01010101 << cur_cpu_id;
779 ror_val = (cur_cpu_id - new_cpu_id) & 31;
780
781 raw_spin_lock(&irq_controller_lock);
782
783 /* Update the target interface for this logical CPU */
784 gic_cpu_map[cpu] = 1 << new_cpu_id;
785
786 /*
787 * Find all the peripheral interrupts targetting the current
788 * CPU interface and migrate them to the new CPU interface.
789 * We skip DIST_TARGET 0 to 7 as they are read-only.
790 */
791 for (i = 8; i < DIV_ROUND_UP(gic_irqs, 4); i++) {
792 val = readl_relaxed(dist_base + GIC_DIST_TARGET + i * 4);
793 active_mask = val & cur_target_mask;
794 if (active_mask) {
795 val &= ~active_mask;
796 val |= ror32(active_mask, ror_val);
797 writel_relaxed(val, dist_base + GIC_DIST_TARGET + i*4);
798 }
799 }
800
801 raw_spin_unlock(&irq_controller_lock);
802
803 /*
804 * Now let's migrate and clear any potential SGIs that might be
805 * pending for us (cur_cpu_id). Since GIC_DIST_SGI_PENDING_SET
806 * is a banked register, we can only forward the SGI using
807 * GIC_DIST_SOFTINT. The original SGI source is lost but Linux
808 * doesn't use that information anyway.
809 *
810 * For the same reason we do not adjust SGI source information
811 * for previously sent SGIs by us to other CPUs either.
812 */
813 for (i = 0; i < 16; i += 4) {
814 int j;
815 val = readl_relaxed(dist_base + GIC_DIST_SGI_PENDING_SET + i);
816 if (!val)
817 continue;
818 writel_relaxed(val, dist_base + GIC_DIST_SGI_PENDING_CLEAR + i);
819 for (j = i; j < i + 4; j++) {
820 if (val & 0xff)
821 writel_relaxed((1 << (new_cpu_id + 16)) | j,
822 dist_base + GIC_DIST_SOFTINT);
823 val >>= 8;
824 }
825 }
Rob Herringb1cffeb2012-11-26 15:05:48 -0600826}
Nicolas Pitreeeb44652012-11-28 18:17:25 -0500827
828/*
829 * gic_get_sgir_physaddr - get the physical address for the SGI register
830 *
831 * REturn the physical address of the SGI register to be used
832 * by some early assembly code when the kernel is not yet available.
833 */
834static unsigned long gic_dist_physaddr;
835
836unsigned long gic_get_sgir_physaddr(void)
837{
838 if (!gic_dist_physaddr)
839 return 0;
840 return gic_dist_physaddr + GIC_DIST_SOFTINT;
841}
842
843void __init gic_init_physaddr(struct device_node *node)
844{
845 struct resource res;
846 if (of_address_to_resource(node, 0, &res) == 0) {
847 gic_dist_physaddr = res.start;
848 pr_info("GIC physical location is %#lx\n", gic_dist_physaddr);
849 }
850}
851
852#else
853#define gic_init_physaddr(node) do { } while (0)
Rob Herringb1cffeb2012-11-26 15:05:48 -0600854#endif
855
Grant Likely75294952012-02-14 14:06:57 -0700856static int gic_irq_domain_map(struct irq_domain *d, unsigned int irq,
857 irq_hw_number_t hw)
858{
859 if (hw < 32) {
860 irq_set_percpu_devid(irq);
Yingjoe Chen9a1091e2014-11-25 16:04:19 +0800861 irq_domain_set_info(d, irq, hw, &gic_chip, d->host_data,
862 handle_percpu_devid_irq, NULL, NULL);
Grant Likely75294952012-02-14 14:06:57 -0700863 set_irq_flags(irq, IRQF_VALID | IRQF_NOAUTOEN);
864 } else {
Yingjoe Chen9a1091e2014-11-25 16:04:19 +0800865 irq_domain_set_info(d, irq, hw, &gic_chip, d->host_data,
866 handle_fasteoi_irq, NULL, NULL);
Grant Likely75294952012-02-14 14:06:57 -0700867 set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
868 }
Grant Likely75294952012-02-14 14:06:57 -0700869 return 0;
870}
871
Sricharan R006e9832013-12-03 15:57:22 +0530872static void gic_irq_domain_unmap(struct irq_domain *d, unsigned int irq)
873{
Sricharan R006e9832013-12-03 15:57:22 +0530874}
875
Grant Likely7bb69ba2012-02-14 14:06:48 -0700876static int gic_irq_domain_xlate(struct irq_domain *d,
877 struct device_node *controller,
878 const u32 *intspec, unsigned int intsize,
879 unsigned long *out_hwirq, unsigned int *out_type)
Rob Herringb3f7ed02011-09-28 21:27:52 -0500880{
Sricharan R006e9832013-12-03 15:57:22 +0530881 unsigned long ret = 0;
882
Rob Herringb3f7ed02011-09-28 21:27:52 -0500883 if (d->of_node != controller)
884 return -EINVAL;
885 if (intsize < 3)
886 return -EINVAL;
887
888 /* Get the interrupt number and add 16 to skip over SGIs */
889 *out_hwirq = intspec[1] + 16;
890
891 /* For SPIs, we need to add 16 more to get the GIC irq ID number */
Marc Zyngiera5561c32015-03-11 15:43:46 +0000892 if (!intspec[0])
893 *out_hwirq += 16;
Rob Herringb3f7ed02011-09-28 21:27:52 -0500894
895 *out_type = intspec[2] & IRQ_TYPE_SENSE_MASK;
Sricharan R006e9832013-12-03 15:57:22 +0530896
897 return ret;
Rob Herringb3f7ed02011-09-28 21:27:52 -0500898}
Rob Herringb3f7ed02011-09-28 21:27:52 -0500899
Catalin Marinasc0114702013-01-14 18:05:37 +0000900#ifdef CONFIG_SMP
Paul Gortmaker8c37bb32013-06-19 11:32:08 -0400901static int gic_secondary_init(struct notifier_block *nfb, unsigned long action,
902 void *hcpu)
Catalin Marinasc0114702013-01-14 18:05:37 +0000903{
Shawn Guo8b6fd652013-06-12 19:30:27 +0800904 if (action == CPU_STARTING || action == CPU_STARTING_FROZEN)
Catalin Marinasc0114702013-01-14 18:05:37 +0000905 gic_cpu_init(&gic_data[0]);
906 return NOTIFY_OK;
907}
908
909/*
910 * Notifier for enabling the GIC CPU interface. Set an arbitrarily high
911 * priority because the GIC needs to be up before the ARM generic timers.
912 */
Paul Gortmaker8c37bb32013-06-19 11:32:08 -0400913static struct notifier_block gic_cpu_notifier = {
Catalin Marinasc0114702013-01-14 18:05:37 +0000914 .notifier_call = gic_secondary_init,
915 .priority = 100,
916};
917#endif
918
Yingjoe Chen9a1091e2014-11-25 16:04:19 +0800919static int gic_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
920 unsigned int nr_irqs, void *arg)
921{
922 int i, ret;
923 irq_hw_number_t hwirq;
924 unsigned int type = IRQ_TYPE_NONE;
925 struct of_phandle_args *irq_data = arg;
926
927 ret = gic_irq_domain_xlate(domain, irq_data->np, irq_data->args,
928 irq_data->args_count, &hwirq, &type);
929 if (ret)
930 return ret;
931
932 for (i = 0; i < nr_irqs; i++)
933 gic_irq_domain_map(domain, virq + i, hwirq + i);
934
935 return 0;
936}
937
938static const struct irq_domain_ops gic_irq_domain_hierarchy_ops = {
939 .xlate = gic_irq_domain_xlate,
940 .alloc = gic_irq_domain_alloc,
941 .free = irq_domain_free_irqs_top,
942};
943
Stephen Boyd68593582014-03-04 17:02:01 -0800944static const struct irq_domain_ops gic_irq_domain_ops = {
Grant Likely75294952012-02-14 14:06:57 -0700945 .map = gic_irq_domain_map,
Sricharan R006e9832013-12-03 15:57:22 +0530946 .unmap = gic_irq_domain_unmap,
Grant Likely7bb69ba2012-02-14 14:06:48 -0700947 .xlate = gic_irq_domain_xlate,
Rob Herring4294f8ba2011-09-28 21:25:31 -0500948};
949
Marc Zyngier49869be2015-03-11 15:45:34 +0000950void gic_set_irqchip_flags(unsigned long flags)
Sricharan R006e9832013-12-03 15:57:22 +0530951{
Marc Zyngier49869be2015-03-11 15:45:34 +0000952 gic_chip.flags |= flags;
Sricharan R006e9832013-12-03 15:57:22 +0530953}
954
Marc Zyngierdb0d4db2011-11-12 16:09:49 +0000955void __init gic_init_bases(unsigned int gic_nr, int irq_start,
956 void __iomem *dist_base, void __iomem *cpu_base,
Grant Likely75294952012-02-14 14:06:57 -0700957 u32 percpu_offset, struct device_node *node)
Russell Kingb580b892010-12-04 15:55:14 +0000958{
Grant Likely75294952012-02-14 14:06:57 -0700959 irq_hw_number_t hwirq_base;
Russell Kingbef8f9e2010-12-04 16:50:58 +0000960 struct gic_chip_data *gic;
Nicolas Pitre384a2902012-04-11 18:55:48 -0400961 int gic_irqs, irq_base, i;
Russell Kingbef8f9e2010-12-04 16:50:58 +0000962
963 BUG_ON(gic_nr >= MAX_GIC_NR);
964
965 gic = &gic_data[gic_nr];
Marc Zyngierdb0d4db2011-11-12 16:09:49 +0000966#ifdef CONFIG_GIC_NON_BANKED
967 if (percpu_offset) { /* Frankein-GIC without banked registers... */
968 unsigned int cpu;
969
970 gic->dist_base.percpu_base = alloc_percpu(void __iomem *);
971 gic->cpu_base.percpu_base = alloc_percpu(void __iomem *);
972 if (WARN_ON(!gic->dist_base.percpu_base ||
973 !gic->cpu_base.percpu_base)) {
974 free_percpu(gic->dist_base.percpu_base);
975 free_percpu(gic->cpu_base.percpu_base);
976 return;
977 }
978
979 for_each_possible_cpu(cpu) {
Tomasz Figa29e697b2014-07-17 17:23:44 +0200980 u32 mpidr = cpu_logical_map(cpu);
981 u32 core_id = MPIDR_AFFINITY_LEVEL(mpidr, 0);
982 unsigned long offset = percpu_offset * core_id;
Marc Zyngierdb0d4db2011-11-12 16:09:49 +0000983 *per_cpu_ptr(gic->dist_base.percpu_base, cpu) = dist_base + offset;
984 *per_cpu_ptr(gic->cpu_base.percpu_base, cpu) = cpu_base + offset;
985 }
986
987 gic_set_base_accessor(gic, gic_get_percpu_base);
988 } else
989#endif
990 { /* Normal, sane GIC... */
991 WARN(percpu_offset,
992 "GIC_NON_BANKED not enabled, ignoring %08x offset!",
993 percpu_offset);
994 gic->dist_base.common_base = dist_base;
995 gic->cpu_base.common_base = cpu_base;
996 gic_set_base_accessor(gic, gic_get_common_base);
997 }
Russell Kingbef8f9e2010-12-04 16:50:58 +0000998
Rob Herring4294f8ba2011-09-28 21:25:31 -0500999 /*
Nicolas Pitre384a2902012-04-11 18:55:48 -04001000 * Initialize the CPU interface map to all CPUs.
1001 * It will be refined as each CPU probes its ID.
1002 */
1003 for (i = 0; i < NR_GIC_CPU_IF; i++)
1004 gic_cpu_map[i] = 0xff;
1005
1006 /*
Rob Herring4294f8ba2011-09-28 21:25:31 -05001007 * Find out how many interrupts are supported.
1008 * The GIC only supports up to 1020 interrupt sources.
1009 */
Marc Zyngierdb0d4db2011-11-12 16:09:49 +00001010 gic_irqs = readl_relaxed(gic_data_dist_base(gic) + GIC_DIST_CTR) & 0x1f;
Rob Herring4294f8ba2011-09-28 21:25:31 -05001011 gic_irqs = (gic_irqs + 1) * 32;
1012 if (gic_irqs > 1020)
1013 gic_irqs = 1020;
1014 gic->gic_irqs = gic_irqs;
1015
Yingjoe Chen9a1091e2014-11-25 16:04:19 +08001016 if (node) { /* DT case */
Marc Zyngiera5561c32015-03-11 15:43:46 +00001017 gic->domain = irq_domain_add_linear(node, gic_irqs,
1018 &gic_irq_domain_hierarchy_ops,
1019 gic);
Yingjoe Chen9a1091e2014-11-25 16:04:19 +08001020 } else { /* Non-DT case */
1021 /*
1022 * For primary GICs, skip over SGIs.
1023 * For secondary GICs, skip over PPIs, too.
1024 */
1025 if (gic_nr == 0 && (irq_start & 31) > 0) {
1026 hwirq_base = 16;
1027 if (irq_start != -1)
1028 irq_start = (irq_start & ~31) + 16;
1029 } else {
1030 hwirq_base = 32;
1031 }
1032
1033 gic_irqs -= hwirq_base; /* calculate # of irqs to allocate */
1034
Sricharan R006e9832013-12-03 15:57:22 +05301035 irq_base = irq_alloc_descs(irq_start, 16, gic_irqs,
1036 numa_node_id());
1037 if (IS_ERR_VALUE(irq_base)) {
1038 WARN(1, "Cannot allocate irq_descs @ IRQ%d, assuming pre-allocated\n",
1039 irq_start);
1040 irq_base = irq_start;
1041 }
1042
1043 gic->domain = irq_domain_add_legacy(node, gic_irqs, irq_base,
1044 hwirq_base, &gic_irq_domain_ops, gic);
Rob Herringf37a53c2011-10-21 17:14:27 -05001045 }
Sricharan R006e9832013-12-03 15:57:22 +05301046
Grant Likely75294952012-02-14 14:06:57 -07001047 if (WARN_ON(!gic->domain))
1048 return;
Russell Kingbef8f9e2010-12-04 16:50:58 +00001049
Mark Rutland08332df2013-11-28 14:21:40 +00001050 if (gic_nr == 0) {
Rob Herringb1cffeb2012-11-26 15:05:48 -06001051#ifdef CONFIG_SMP
Mark Rutland08332df2013-11-28 14:21:40 +00001052 set_smp_cross_call(gic_raise_softirq);
1053 register_cpu_notifier(&gic_cpu_notifier);
Rob Herringb1cffeb2012-11-26 15:05:48 -06001054#endif
Mark Rutland08332df2013-11-28 14:21:40 +00001055 set_handle_irq(gic_handle_irq);
1056 }
Rob Herringcfed7d62012-11-03 12:59:51 -05001057
Colin Cross9c128452011-06-13 00:45:59 +00001058 gic_chip.flags |= gic_arch_extn.flags;
Rob Herring4294f8ba2011-09-28 21:25:31 -05001059 gic_dist_init(gic);
Russell Kingbef8f9e2010-12-04 16:50:58 +00001060 gic_cpu_init(gic);
Colin Cross254056f2011-02-10 12:54:10 -08001061 gic_pm_init(gic);
Russell Kingb580b892010-12-04 15:55:14 +00001062}
1063
Rob Herringb3f7ed02011-09-28 21:27:52 -05001064#ifdef CONFIG_OF
Sachin Kamat46f101d2013-03-13 15:05:15 +05301065static int gic_cnt __initdata;
Rob Herringb3f7ed02011-09-28 21:27:52 -05001066
Stephen Boyd68593582014-03-04 17:02:01 -08001067static int __init
1068gic_of_init(struct device_node *node, struct device_node *parent)
Rob Herringb3f7ed02011-09-28 21:27:52 -05001069{
1070 void __iomem *cpu_base;
1071 void __iomem *dist_base;
Marc Zyngierdb0d4db2011-11-12 16:09:49 +00001072 u32 percpu_offset;
Rob Herringb3f7ed02011-09-28 21:27:52 -05001073 int irq;
Rob Herringb3f7ed02011-09-28 21:27:52 -05001074
1075 if (WARN_ON(!node))
1076 return -ENODEV;
1077
1078 dist_base = of_iomap(node, 0);
1079 WARN(!dist_base, "unable to map gic dist registers\n");
1080
1081 cpu_base = of_iomap(node, 1);
1082 WARN(!cpu_base, "unable to map gic cpu registers\n");
1083
Marc Zyngierdb0d4db2011-11-12 16:09:49 +00001084 if (of_property_read_u32(node, "cpu-offset", &percpu_offset))
1085 percpu_offset = 0;
1086
Grant Likely75294952012-02-14 14:06:57 -07001087 gic_init_bases(gic_cnt, -1, dist_base, cpu_base, percpu_offset, node);
Nicolas Pitreeeb44652012-11-28 18:17:25 -05001088 if (!gic_cnt)
1089 gic_init_physaddr(node);
Rob Herringb3f7ed02011-09-28 21:27:52 -05001090
1091 if (parent) {
1092 irq = irq_of_parse_and_map(node, 0);
1093 gic_cascade_irq(gic_cnt, irq);
1094 }
Suravee Suthikulpanit853a33c2014-11-25 18:47:22 +00001095
1096 if (IS_ENABLED(CONFIG_ARM_GIC_V2M))
1097 gicv2m_of_init(node, gic_data[gic_cnt].domain);
1098
Rob Herringb3f7ed02011-09-28 21:27:52 -05001099 gic_cnt++;
1100 return 0;
1101}
Suravee Suthikulpanit144cb082014-07-15 00:03:03 +02001102IRQCHIP_DECLARE(gic_400, "arm,gic-400", gic_of_init);
Linus Walleijfa6e2ee2014-10-01 09:29:22 +02001103IRQCHIP_DECLARE(arm11mp_gic, "arm,arm11mp-gic", gic_of_init);
1104IRQCHIP_DECLARE(arm1176jzf_dc_gic, "arm,arm1176jzf-devchip-gic", gic_of_init);
Rob Herring81243e42012-11-20 21:21:40 -06001105IRQCHIP_DECLARE(cortex_a15_gic, "arm,cortex-a15-gic", gic_of_init);
1106IRQCHIP_DECLARE(cortex_a9_gic, "arm,cortex-a9-gic", gic_of_init);
Matthias Bruggera97e80272014-07-03 13:58:52 +02001107IRQCHIP_DECLARE(cortex_a7_gic, "arm,cortex-a7-gic", gic_of_init);
Rob Herring81243e42012-11-20 21:21:40 -06001108IRQCHIP_DECLARE(msm_8660_qgic, "qcom,msm-8660-qgic", gic_of_init);
1109IRQCHIP_DECLARE(msm_qgic2, "qcom,msm-qgic2", gic_of_init);
1110
Rob Herringb3f7ed02011-09-28 21:27:52 -05001111#endif
Tomasz Nowickid60fc382015-03-24 14:02:49 +00001112
1113#ifdef CONFIG_ACPI
1114static phys_addr_t dist_phy_base, cpu_phy_base __initdata;
1115
1116static int __init
1117gic_acpi_parse_madt_cpu(struct acpi_subtable_header *header,
1118 const unsigned long end)
1119{
1120 struct acpi_madt_generic_interrupt *processor;
1121 phys_addr_t gic_cpu_base;
1122 static int cpu_base_assigned;
1123
1124 processor = (struct acpi_madt_generic_interrupt *)header;
1125
1126 if (BAD_MADT_ENTRY(processor, end))
1127 return -EINVAL;
1128
1129 /*
1130 * There is no support for non-banked GICv1/2 register in ACPI spec.
1131 * All CPU interface addresses have to be the same.
1132 */
1133 gic_cpu_base = processor->base_address;
1134 if (cpu_base_assigned && gic_cpu_base != cpu_phy_base)
1135 return -EINVAL;
1136
1137 cpu_phy_base = gic_cpu_base;
1138 cpu_base_assigned = 1;
1139 return 0;
1140}
1141
1142static int __init
1143gic_acpi_parse_madt_distributor(struct acpi_subtable_header *header,
1144 const unsigned long end)
1145{
1146 struct acpi_madt_generic_distributor *dist;
1147
1148 dist = (struct acpi_madt_generic_distributor *)header;
1149
1150 if (BAD_MADT_ENTRY(dist, end))
1151 return -EINVAL;
1152
1153 dist_phy_base = dist->base_address;
1154 return 0;
1155}
1156
1157int __init
1158gic_v2_acpi_init(struct acpi_table_header *table)
1159{
1160 void __iomem *cpu_base, *dist_base;
1161 int count;
1162
1163 /* Collect CPU base addresses */
1164 count = acpi_parse_entries(ACPI_SIG_MADT,
1165 sizeof(struct acpi_table_madt),
1166 gic_acpi_parse_madt_cpu, table,
1167 ACPI_MADT_TYPE_GENERIC_INTERRUPT, 0);
1168 if (count <= 0) {
1169 pr_err("No valid GICC entries exist\n");
1170 return -EINVAL;
1171 }
1172
1173 /*
1174 * Find distributor base address. We expect one distributor entry since
1175 * ACPI 5.1 spec neither support multi-GIC instances nor GIC cascade.
1176 */
1177 count = acpi_parse_entries(ACPI_SIG_MADT,
1178 sizeof(struct acpi_table_madt),
1179 gic_acpi_parse_madt_distributor, table,
1180 ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR, 0);
1181 if (count <= 0) {
1182 pr_err("No valid GICD entries exist\n");
1183 return -EINVAL;
1184 } else if (count > 1) {
1185 pr_err("More than one GICD entry detected\n");
1186 return -EINVAL;
1187 }
1188
1189 cpu_base = ioremap(cpu_phy_base, ACPI_GIC_CPU_IF_MEM_SIZE);
1190 if (!cpu_base) {
1191 pr_err("Unable to map GICC registers\n");
1192 return -ENOMEM;
1193 }
1194
1195 dist_base = ioremap(dist_phy_base, ACPI_GICV2_DIST_MEM_SIZE);
1196 if (!dist_base) {
1197 pr_err("Unable to map GICD registers\n");
1198 iounmap(cpu_base);
1199 return -ENOMEM;
1200 }
1201
1202 /*
1203 * Initialize zero GIC instance (no multi-GIC support). Also, set GIC
1204 * as default IRQ domain to allow for GSI registration and GSI to IRQ
1205 * number translation (see acpi_register_gsi() and acpi_gsi_to_irq()).
1206 */
1207 gic_init_bases(0, -1, dist_base, cpu_base, 0, NULL);
1208 irq_set_default_host(gic_data[0].domain);
Lorenzo Pieralisid8f4f162015-03-24 17:58:51 +00001209
1210 acpi_irq_model = ACPI_IRQ_MODEL_GIC;
Tomasz Nowickid60fc382015-03-24 14:02:49 +00001211 return 0;
1212}
1213#endif