blob: f1efb53faebc624e920eeac80702512b7f0a1ff1 [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 Herring4294f8b2011-09-28 21:25:31 -050037#include <linux/irqdomain.h>
Marc Zyngier292b2932011-07-20 16:24:14 +010038#include <linux/interrupt.h>
39#include <linux/percpu.h>
40#include <linux/slab.h>
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 Herring4294f8b2011-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 */
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100156static void gic_mask_irq(struct irq_data *d)
Russell Kingf27ecac2005-08-18 21:31:00 +0100157{
Rob Herring4294f8b2011-09-28 21:25:31 -0500158 u32 mask = 1 << (gic_irq(d) % 32);
Marc Zyngiercf613872015-03-06 16:37:44 +0000159 unsigned long flags;
Thomas Gleixnerc4bfa282006-07-01 22:32:14 +0100160
Marc Zyngiercf613872015-03-06 16:37:44 +0000161 raw_spin_lock_irqsave(&irq_controller_lock, flags);
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530162 writel_relaxed(mask, gic_dist_base(d) + GIC_DIST_ENABLE_CLEAR + (gic_irq(d) / 32) * 4);
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100163 if (gic_arch_extn.irq_mask)
164 gic_arch_extn.irq_mask(d);
Marc Zyngiercf613872015-03-06 16:37:44 +0000165 raw_spin_unlock_irqrestore(&irq_controller_lock, flags);
Russell Kingf27ecac2005-08-18 21:31:00 +0100166}
167
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100168static void gic_unmask_irq(struct irq_data *d)
Russell Kingf27ecac2005-08-18 21:31:00 +0100169{
Rob Herring4294f8b2011-09-28 21:25:31 -0500170 u32 mask = 1 << (gic_irq(d) % 32);
Marc Zyngiercf613872015-03-06 16:37:44 +0000171 unsigned long flags;
Thomas Gleixnerc4bfa282006-07-01 22:32:14 +0100172
Marc Zyngiercf613872015-03-06 16:37:44 +0000173 raw_spin_lock_irqsave(&irq_controller_lock, flags);
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100174 if (gic_arch_extn.irq_unmask)
175 gic_arch_extn.irq_unmask(d);
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530176 writel_relaxed(mask, gic_dist_base(d) + GIC_DIST_ENABLE_SET + (gic_irq(d) / 32) * 4);
Marc Zyngiercf613872015-03-06 16:37:44 +0000177 raw_spin_unlock_irqrestore(&irq_controller_lock, flags);
Russell Kingf27ecac2005-08-18 21:31:00 +0100178}
179
Will Deacon1a017532011-02-09 12:01:12 +0000180static void gic_eoi_irq(struct irq_data *d)
181{
182 if (gic_arch_extn.irq_eoi) {
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500183 raw_spin_lock(&irq_controller_lock);
Will Deacon1a017532011-02-09 12:01:12 +0000184 gic_arch_extn.irq_eoi(d);
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500185 raw_spin_unlock(&irq_controller_lock);
Will Deacon1a017532011-02-09 12:01:12 +0000186 }
187
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530188 writel_relaxed(gic_irq(d), gic_cpu_base(d) + GIC_CPU_EOI);
Will Deacon1a017532011-02-09 12:01:12 +0000189}
190
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100191static int gic_set_type(struct irq_data *d, unsigned int type)
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100192{
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100193 void __iomem *base = gic_dist_base(d);
194 unsigned int gicirq = gic_irq(d);
Marc Zyngiercf613872015-03-06 16:37:44 +0000195 unsigned long flags;
Liviu Dudaufb7e7de2015-01-20 16:52:59 +0000196 int ret;
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100197
198 /* Interrupt configuration for SGIs can't be changed */
199 if (gicirq < 16)
200 return -EINVAL;
201
Liviu Dudaufb7e7de2015-01-20 16:52:59 +0000202 /* SPIs have restrictions on the supported types */
203 if (gicirq >= 32 && type != IRQ_TYPE_LEVEL_HIGH &&
204 type != IRQ_TYPE_EDGE_RISING)
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100205 return -EINVAL;
206
Marc Zyngiercf613872015-03-06 16:37:44 +0000207 raw_spin_lock_irqsave(&irq_controller_lock, flags);
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100208
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100209 if (gic_arch_extn.irq_set_type)
210 gic_arch_extn.irq_set_type(d, type);
211
Liviu Dudaufb7e7de2015-01-20 16:52:59 +0000212 ret = gic_configure_irq(gicirq, type, base, NULL);
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100213
Marc Zyngiercf613872015-03-06 16:37:44 +0000214 raw_spin_unlock_irqrestore(&irq_controller_lock, flags);
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100215
Liviu Dudaufb7e7de2015-01-20 16:52:59 +0000216 return ret;
Rabin Vincent5c0c1f02010-05-28 04:37:38 +0100217}
218
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100219static int gic_retrigger(struct irq_data *d)
220{
221 if (gic_arch_extn.irq_retrigger)
222 return gic_arch_extn.irq_retrigger(d);
223
Abhijeet Dharmapurikarbad9a432013-03-19 16:05:49 -0700224 /* the genirq layer expects 0 if we can't retrigger in hardware */
225 return 0;
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100226}
227
Catalin Marinasa06f5462005-09-30 16:07:05 +0100228#ifdef CONFIG_SMP
Russell Kingc1917892011-01-23 12:12:01 +0000229static int gic_set_affinity(struct irq_data *d, const struct cpumask *mask_val,
230 bool force)
Russell Kingf27ecac2005-08-18 21:31:00 +0100231{
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100232 void __iomem *reg = gic_dist_base(d) + GIC_DIST_TARGET + (gic_irq(d) & ~3);
Thomas Gleixnerffde1de2014-04-16 14:36:44 +0000233 unsigned int cpu, shift = (gic_irq(d) % 4) * 8;
Russell Kingc1917892011-01-23 12:12:01 +0000234 u32 val, mask, bit;
Marc Zyngiercf613872015-03-06 16:37:44 +0000235 unsigned long flags;
Russell Kingc1917892011-01-23 12:12:01 +0000236
Thomas Gleixnerffde1de2014-04-16 14:36:44 +0000237 if (!force)
238 cpu = cpumask_any_and(mask_val, cpu_online_mask);
239 else
240 cpu = cpumask_first(mask_val);
241
Nicolas Pitre384a2902012-04-11 18:55:48 -0400242 if (cpu >= NR_GIC_CPU_IF || cpu >= nr_cpu_ids)
Russell Kingc1917892011-01-23 12:12:01 +0000243 return -EINVAL;
244
Marc Zyngiercf613872015-03-06 16:37:44 +0000245 raw_spin_lock_irqsave(&irq_controller_lock, flags);
Russell Kingc1917892011-01-23 12:12:01 +0000246 mask = 0xff << shift;
Nicolas Pitre384a2902012-04-11 18:55:48 -0400247 bit = gic_cpu_map[cpu] << shift;
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530248 val = readl_relaxed(reg) & ~mask;
249 writel_relaxed(val | bit, reg);
Marc Zyngiercf613872015-03-06 16:37:44 +0000250 raw_spin_unlock_irqrestore(&irq_controller_lock, flags);
Yinghai Lud5dedd42009-04-27 17:59:21 -0700251
Russell King5dfc54e2011-07-21 15:00:57 +0100252 return IRQ_SET_MASK_OK;
Russell Kingf27ecac2005-08-18 21:31:00 +0100253}
Catalin Marinasa06f5462005-09-30 16:07:05 +0100254#endif
Russell Kingf27ecac2005-08-18 21:31:00 +0100255
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100256#ifdef CONFIG_PM
257static int gic_set_wake(struct irq_data *d, unsigned int on)
258{
259 int ret = -ENXIO;
260
261 if (gic_arch_extn.irq_set_wake)
262 ret = gic_arch_extn.irq_set_wake(d, on);
263
264 return ret;
265}
266
267#else
268#define gic_set_wake NULL
269#endif
270
Stephen Boyd8783dd32014-03-04 16:40:30 -0800271static void __exception_irq_entry gic_handle_irq(struct pt_regs *regs)
Marc Zyngier562e0022011-09-06 09:56:17 +0100272{
273 u32 irqstat, irqnr;
274 struct gic_chip_data *gic = &gic_data[0];
275 void __iomem *cpu_base = gic_data_cpu_base(gic);
276
277 do {
278 irqstat = readl_relaxed(cpu_base + GIC_CPU_INTACK);
Haojian Zhuangb8802f72014-05-11 16:05:58 +0800279 irqnr = irqstat & GICC_IAR_INT_ID_MASK;
Marc Zyngier562e0022011-09-06 09:56:17 +0100280
281 if (likely(irqnr > 15 && irqnr < 1021)) {
Marc Zyngier60031b42014-08-26 11:03:20 +0100282 handle_domain_irq(gic->domain, irqnr, regs);
Marc Zyngier562e0022011-09-06 09:56:17 +0100283 continue;
284 }
285 if (irqnr < 16) {
286 writel_relaxed(irqstat, cpu_base + GIC_CPU_EOI);
287#ifdef CONFIG_SMP
288 handle_IPI(irqnr, regs);
289#endif
290 continue;
291 }
292 break;
293 } while (1);
294}
295
Russell King0f347bb2007-05-17 10:11:34 +0100296static void gic_handle_cascade_irq(unsigned int irq, struct irq_desc *desc)
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100297{
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100298 struct gic_chip_data *chip_data = irq_get_handler_data(irq);
299 struct irq_chip *chip = irq_get_chip(irq);
Russell King0f347bb2007-05-17 10:11:34 +0100300 unsigned int cascade_irq, gic_irq;
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100301 unsigned long status;
302
Will Deacon1a017532011-02-09 12:01:12 +0000303 chained_irq_enter(chip, desc);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100304
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500305 raw_spin_lock(&irq_controller_lock);
Marc Zyngierdb0d4db2011-11-12 16:09:49 +0000306 status = readl_relaxed(gic_data_cpu_base(chip_data) + GIC_CPU_INTACK);
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500307 raw_spin_unlock(&irq_controller_lock);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100308
Feng Kane5f81532014-07-30 14:56:58 -0700309 gic_irq = (status & GICC_IAR_INT_ID_MASK);
310 if (gic_irq == GICC_INT_SPURIOUS)
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100311 goto out;
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100312
Grant Likely75294952012-02-14 14:06:57 -0700313 cascade_irq = irq_find_mapping(chip_data->domain, gic_irq);
314 if (unlikely(gic_irq < 32 || gic_irq > 1020))
Catalin Marinasaec00952013-01-14 17:53:39 +0000315 handle_bad_irq(cascade_irq, desc);
Russell King0f347bb2007-05-17 10:11:34 +0100316 else
317 generic_handle_irq(cascade_irq);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100318
319 out:
Will Deacon1a017532011-02-09 12:01:12 +0000320 chained_irq_exit(chip, desc);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100321}
322
David Brownell38c677c2006-08-01 22:26:25 +0100323static struct irq_chip gic_chip = {
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100324 .name = "GIC",
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100325 .irq_mask = gic_mask_irq,
326 .irq_unmask = gic_unmask_irq,
Will Deacon1a017532011-02-09 12:01:12 +0000327 .irq_eoi = gic_eoi_irq,
Lennert Buytenhek7d1f4282010-11-29 10:18:20 +0100328 .irq_set_type = gic_set_type,
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100329 .irq_retrigger = gic_retrigger,
Russell Kingf27ecac2005-08-18 21:31:00 +0100330#ifdef CONFIG_SMP
Russell Kingc1917892011-01-23 12:12:01 +0000331 .irq_set_affinity = gic_set_affinity,
Russell Kingf27ecac2005-08-18 21:31:00 +0100332#endif
Santosh Shilimkard7ed36a2011-03-02 08:03:22 +0100333 .irq_set_wake = gic_set_wake,
Russell Kingf27ecac2005-08-18 21:31:00 +0100334};
335
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100336void __init gic_cascade_irq(unsigned int gic_nr, unsigned int irq)
337{
338 if (gic_nr >= MAX_GIC_NR)
339 BUG();
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100340 if (irq_set_handler_data(irq, &gic_data[gic_nr]) != 0)
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100341 BUG();
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100342 irq_set_chained_handler(irq, gic_handle_cascade_irq);
Catalin Marinasb3a1bde2007-02-14 19:14:56 +0100343}
344
Russell King2bb31352013-01-30 23:49:57 +0000345static u8 gic_get_cpumask(struct gic_chip_data *gic)
346{
347 void __iomem *base = gic_data_dist_base(gic);
348 u32 mask, i;
349
350 for (i = mask = 0; i < 32; i += 4) {
351 mask = readl_relaxed(base + GIC_DIST_TARGET + i);
352 mask |= mask >> 16;
353 mask |= mask >> 8;
354 if (mask)
355 break;
356 }
357
358 if (!mask)
359 pr_crit("GIC CPU mask not found - kernel will fail to boot.\n");
360
361 return mask;
362}
363
Feng Kan32289502014-07-30 14:56:59 -0700364static void gic_cpu_if_up(void)
365{
366 void __iomem *cpu_base = gic_data_cpu_base(&gic_data[0]);
367 u32 bypass = 0;
368
369 /*
370 * Preserve bypass disable bits to be written back later
371 */
372 bypass = readl(cpu_base + GIC_CPU_CTRL);
373 bypass &= GICC_DIS_BYPASS_MASK;
374
375 writel_relaxed(bypass | GICC_ENABLE, cpu_base + GIC_CPU_CTRL);
376}
377
378
Rob Herring4294f8b2011-09-28 21:25:31 -0500379static void __init gic_dist_init(struct gic_chip_data *gic)
Russell Kingf27ecac2005-08-18 21:31:00 +0100380{
Grant Likely75294952012-02-14 14:06:57 -0700381 unsigned int i;
Will Deacon267840f2011-08-23 22:20:03 +0100382 u32 cpumask;
Rob Herring4294f8b2011-09-28 21:25:31 -0500383 unsigned int gic_irqs = gic->gic_irqs;
Marc Zyngierdb0d4db2011-11-12 16:09:49 +0000384 void __iomem *base = gic_data_dist_base(gic);
Russell Kingf27ecac2005-08-18 21:31:00 +0100385
Feng Kane5f81532014-07-30 14:56:58 -0700386 writel_relaxed(GICD_DISABLE, base + GIC_DIST_CTRL);
Russell Kingf27ecac2005-08-18 21:31:00 +0100387
388 /*
Russell Kingf27ecac2005-08-18 21:31:00 +0100389 * Set all global interrupts to this CPU only.
390 */
Russell King2bb31352013-01-30 23:49:57 +0000391 cpumask = gic_get_cpumask(gic);
392 cpumask |= cpumask << 8;
393 cpumask |= cpumask << 16;
Pawel Molle6afec92010-11-26 13:45:43 +0100394 for (i = 32; i < gic_irqs; i += 4)
Santosh Shilimkar6ac77e42011-03-28 19:27:46 +0530395 writel_relaxed(cpumask, base + GIC_DIST_TARGET + i * 4 / 4);
Russell Kingf27ecac2005-08-18 21:31:00 +0100396
Marc Zyngierd51d0af2014-06-30 16:01:30 +0100397 gic_dist_config(base, gic_irqs, NULL);
Russell Kingf27ecac2005-08-18 21:31:00 +0100398
Feng Kane5f81532014-07-30 14:56:58 -0700399 writel_relaxed(GICD_ENABLE, base + GIC_DIST_CTRL);
Russell Kingf27ecac2005-08-18 21:31:00 +0100400}
401
Paul Gortmaker8c37bb32013-06-19 11:32:08 -0400402static void gic_cpu_init(struct gic_chip_data *gic)
Russell Kingf27ecac2005-08-18 21:31:00 +0100403{
Marc Zyngierdb0d4db2011-11-12 16:09:49 +0000404 void __iomem *dist_base = gic_data_dist_base(gic);
405 void __iomem *base = gic_data_cpu_base(gic);
Nicolas Pitre384a2902012-04-11 18:55:48 -0400406 unsigned int cpu_mask, cpu = smp_processor_id();
Russell King9395f6e2010-11-11 23:10:30 +0000407 int i;
408
Russell King9395f6e2010-11-11 23:10:30 +0000409 /*
Nicolas Pitre384a2902012-04-11 18:55:48 -0400410 * Get what the GIC says our CPU mask is.
411 */
412 BUG_ON(cpu >= NR_GIC_CPU_IF);
Russell King2bb31352013-01-30 23:49:57 +0000413 cpu_mask = gic_get_cpumask(gic);
Nicolas Pitre384a2902012-04-11 18:55:48 -0400414 gic_cpu_map[cpu] = cpu_mask;
415
416 /*
417 * Clear our mask from the other map entries in case they're
418 * still undefined.
419 */
420 for (i = 0; i < NR_GIC_CPU_IF; i++)
421 if (i != cpu)
422 gic_cpu_map[i] &= ~cpu_mask;
423
Marc Zyngierd51d0af2014-06-30 16:01:30 +0100424 gic_cpu_config(dist_base, NULL);
Russell King9395f6e2010-11-11 23:10:30 +0000425
Feng Kane5f81532014-07-30 14:56:58 -0700426 writel_relaxed(GICC_INT_PRI_THRESHOLD, base + GIC_CPU_PRIMASK);
Feng Kan32289502014-07-30 14:56:59 -0700427 gic_cpu_if_up();
Russell Kingf27ecac2005-08-18 21:31:00 +0100428}
429
Nicolas Pitre10d9eb82013-03-19 23:59:04 -0400430void gic_cpu_if_down(void)
431{
432 void __iomem *cpu_base = gic_data_cpu_base(&gic_data[0]);
Feng Kan32289502014-07-30 14:56:59 -0700433 u32 val = 0;
434
435 val = readl(cpu_base + GIC_CPU_CTRL);
436 val &= ~GICC_ENABLE;
437 writel_relaxed(val, cpu_base + GIC_CPU_CTRL);
Nicolas Pitre10d9eb82013-03-19 23:59:04 -0400438}
439
Colin Cross254056f2011-02-10 12:54:10 -0800440#ifdef CONFIG_CPU_PM
441/*
442 * Saves the GIC distributor registers during suspend or idle. Must be called
443 * with interrupts disabled but before powering down the GIC. After calling
444 * this function, no interrupts will be delivered by the GIC, and another
445 * platform-specific wakeup source must be enabled.
446 */
447static void gic_dist_save(unsigned int gic_nr)
448{
449 unsigned int gic_irqs;
450 void __iomem *dist_base;
451 int i;
452
453 if (gic_nr >= MAX_GIC_NR)
454 BUG();
455
456 gic_irqs = gic_data[gic_nr].gic_irqs;
Marc Zyngierdb0d4db2011-11-12 16:09:49 +0000457 dist_base = gic_data_dist_base(&gic_data[gic_nr]);
Colin Cross254056f2011-02-10 12:54:10 -0800458
459 if (!dist_base)
460 return;
461
462 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 16); i++)
463 gic_data[gic_nr].saved_spi_conf[i] =
464 readl_relaxed(dist_base + GIC_DIST_CONFIG + i * 4);
465
466 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 4); i++)
467 gic_data[gic_nr].saved_spi_target[i] =
468 readl_relaxed(dist_base + GIC_DIST_TARGET + i * 4);
469
470 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 32); i++)
471 gic_data[gic_nr].saved_spi_enable[i] =
472 readl_relaxed(dist_base + GIC_DIST_ENABLE_SET + i * 4);
473}
474
475/*
476 * Restores the GIC distributor registers during resume or when coming out of
477 * idle. Must be called before enabling interrupts. If a level interrupt
478 * that occured while the GIC was suspended is still present, it will be
479 * handled normally, but any edge interrupts that occured will not be seen by
480 * the GIC and need to be handled by the platform-specific wakeup source.
481 */
482static void gic_dist_restore(unsigned int gic_nr)
483{
484 unsigned int gic_irqs;
485 unsigned int i;
486 void __iomem *dist_base;
487
488 if (gic_nr >= MAX_GIC_NR)
489 BUG();
490
491 gic_irqs = gic_data[gic_nr].gic_irqs;
Marc Zyngierdb0d4db2011-11-12 16:09:49 +0000492 dist_base = gic_data_dist_base(&gic_data[gic_nr]);
Colin Cross254056f2011-02-10 12:54:10 -0800493
494 if (!dist_base)
495 return;
496
Feng Kane5f81532014-07-30 14:56:58 -0700497 writel_relaxed(GICD_DISABLE, dist_base + GIC_DIST_CTRL);
Colin Cross254056f2011-02-10 12:54:10 -0800498
499 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 16); i++)
500 writel_relaxed(gic_data[gic_nr].saved_spi_conf[i],
501 dist_base + GIC_DIST_CONFIG + i * 4);
502
503 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 4); i++)
Feng Kane5f81532014-07-30 14:56:58 -0700504 writel_relaxed(GICD_INT_DEF_PRI_X4,
Colin Cross254056f2011-02-10 12:54:10 -0800505 dist_base + GIC_DIST_PRI + i * 4);
506
507 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 4); i++)
508 writel_relaxed(gic_data[gic_nr].saved_spi_target[i],
509 dist_base + GIC_DIST_TARGET + i * 4);
510
511 for (i = 0; i < DIV_ROUND_UP(gic_irqs, 32); i++)
512 writel_relaxed(gic_data[gic_nr].saved_spi_enable[i],
513 dist_base + GIC_DIST_ENABLE_SET + i * 4);
514
Feng Kane5f81532014-07-30 14:56:58 -0700515 writel_relaxed(GICD_ENABLE, dist_base + GIC_DIST_CTRL);
Colin Cross254056f2011-02-10 12:54:10 -0800516}
517
518static void gic_cpu_save(unsigned int gic_nr)
519{
520 int i;
521 u32 *ptr;
522 void __iomem *dist_base;
523 void __iomem *cpu_base;
524
525 if (gic_nr >= MAX_GIC_NR)
526 BUG();
527
Marc Zyngierdb0d4db2011-11-12 16:09:49 +0000528 dist_base = gic_data_dist_base(&gic_data[gic_nr]);
529 cpu_base = gic_data_cpu_base(&gic_data[gic_nr]);
Colin Cross254056f2011-02-10 12:54:10 -0800530
531 if (!dist_base || !cpu_base)
532 return;
533
Christoph Lameter532d0d02014-08-17 12:30:39 -0500534 ptr = raw_cpu_ptr(gic_data[gic_nr].saved_ppi_enable);
Colin Cross254056f2011-02-10 12:54:10 -0800535 for (i = 0; i < DIV_ROUND_UP(32, 32); i++)
536 ptr[i] = readl_relaxed(dist_base + GIC_DIST_ENABLE_SET + i * 4);
537
Christoph Lameter532d0d02014-08-17 12:30:39 -0500538 ptr = raw_cpu_ptr(gic_data[gic_nr].saved_ppi_conf);
Colin Cross254056f2011-02-10 12:54:10 -0800539 for (i = 0; i < DIV_ROUND_UP(32, 16); i++)
540 ptr[i] = readl_relaxed(dist_base + GIC_DIST_CONFIG + i * 4);
541
542}
543
544static void gic_cpu_restore(unsigned int gic_nr)
545{
546 int i;
547 u32 *ptr;
548 void __iomem *dist_base;
549 void __iomem *cpu_base;
550
551 if (gic_nr >= MAX_GIC_NR)
552 BUG();
553
Marc Zyngierdb0d4db2011-11-12 16:09:49 +0000554 dist_base = gic_data_dist_base(&gic_data[gic_nr]);
555 cpu_base = gic_data_cpu_base(&gic_data[gic_nr]);
Colin Cross254056f2011-02-10 12:54:10 -0800556
557 if (!dist_base || !cpu_base)
558 return;
559
Christoph Lameter532d0d02014-08-17 12:30:39 -0500560 ptr = raw_cpu_ptr(gic_data[gic_nr].saved_ppi_enable);
Colin Cross254056f2011-02-10 12:54:10 -0800561 for (i = 0; i < DIV_ROUND_UP(32, 32); i++)
562 writel_relaxed(ptr[i], dist_base + GIC_DIST_ENABLE_SET + i * 4);
563
Christoph Lameter532d0d02014-08-17 12:30:39 -0500564 ptr = raw_cpu_ptr(gic_data[gic_nr].saved_ppi_conf);
Colin Cross254056f2011-02-10 12:54:10 -0800565 for (i = 0; i < DIV_ROUND_UP(32, 16); i++)
566 writel_relaxed(ptr[i], dist_base + GIC_DIST_CONFIG + i * 4);
567
568 for (i = 0; i < DIV_ROUND_UP(32, 4); i++)
Feng Kane5f81532014-07-30 14:56:58 -0700569 writel_relaxed(GICD_INT_DEF_PRI_X4,
570 dist_base + GIC_DIST_PRI + i * 4);
Colin Cross254056f2011-02-10 12:54:10 -0800571
Feng Kane5f81532014-07-30 14:56:58 -0700572 writel_relaxed(GICC_INT_PRI_THRESHOLD, cpu_base + GIC_CPU_PRIMASK);
Feng Kan32289502014-07-30 14:56:59 -0700573 gic_cpu_if_up();
Colin Cross254056f2011-02-10 12:54:10 -0800574}
575
576static int gic_notifier(struct notifier_block *self, unsigned long cmd, void *v)
577{
578 int i;
579
580 for (i = 0; i < MAX_GIC_NR; i++) {
Marc Zyngierdb0d4db2011-11-12 16:09:49 +0000581#ifdef CONFIG_GIC_NON_BANKED
582 /* Skip over unused GICs */
583 if (!gic_data[i].get_base)
584 continue;
585#endif
Colin Cross254056f2011-02-10 12:54:10 -0800586 switch (cmd) {
587 case CPU_PM_ENTER:
588 gic_cpu_save(i);
589 break;
590 case CPU_PM_ENTER_FAILED:
591 case CPU_PM_EXIT:
592 gic_cpu_restore(i);
593 break;
594 case CPU_CLUSTER_PM_ENTER:
595 gic_dist_save(i);
596 break;
597 case CPU_CLUSTER_PM_ENTER_FAILED:
598 case CPU_CLUSTER_PM_EXIT:
599 gic_dist_restore(i);
600 break;
601 }
602 }
603
604 return NOTIFY_OK;
605}
606
607static struct notifier_block gic_notifier_block = {
608 .notifier_call = gic_notifier,
609};
610
611static void __init gic_pm_init(struct gic_chip_data *gic)
612{
613 gic->saved_ppi_enable = __alloc_percpu(DIV_ROUND_UP(32, 32) * 4,
614 sizeof(u32));
615 BUG_ON(!gic->saved_ppi_enable);
616
617 gic->saved_ppi_conf = __alloc_percpu(DIV_ROUND_UP(32, 16) * 4,
618 sizeof(u32));
619 BUG_ON(!gic->saved_ppi_conf);
620
Marc Zyngierabdd7b92011-11-25 17:58:19 +0100621 if (gic == &gic_data[0])
622 cpu_pm_register_notifier(&gic_notifier_block);
Colin Cross254056f2011-02-10 12:54:10 -0800623}
624#else
625static void __init gic_pm_init(struct gic_chip_data *gic)
626{
627}
628#endif
629
Rob Herringb1cffeb2012-11-26 15:05:48 -0600630#ifdef CONFIG_SMP
Stephen Boyd68593582014-03-04 17:02:01 -0800631static void gic_raise_softirq(const struct cpumask *mask, unsigned int irq)
Rob Herringb1cffeb2012-11-26 15:05:48 -0600632{
633 int cpu;
Nicolas Pitre1a6b69b2012-04-12 01:40:31 -0400634 unsigned long flags, map = 0;
635
636 raw_spin_lock_irqsave(&irq_controller_lock, flags);
Rob Herringb1cffeb2012-11-26 15:05:48 -0600637
638 /* Convert our logical CPU mask into a physical one. */
639 for_each_cpu(cpu, mask)
Javi Merino91bdf0d2013-02-19 13:52:22 +0000640 map |= gic_cpu_map[cpu];
Rob Herringb1cffeb2012-11-26 15:05:48 -0600641
642 /*
643 * Ensure that stores to Normal memory are visible to the
Will Deacon8adbf572014-02-20 17:42:07 +0000644 * other CPUs before they observe us issuing the IPI.
Rob Herringb1cffeb2012-11-26 15:05:48 -0600645 */
Will Deacon8adbf572014-02-20 17:42:07 +0000646 dmb(ishst);
Rob Herringb1cffeb2012-11-26 15:05:48 -0600647
648 /* this always happens on GIC0 */
649 writel_relaxed(map << 16 | irq, gic_data_dist_base(&gic_data[0]) + GIC_DIST_SOFTINT);
Nicolas Pitre1a6b69b2012-04-12 01:40:31 -0400650
651 raw_spin_unlock_irqrestore(&irq_controller_lock, flags);
652}
653#endif
654
655#ifdef CONFIG_BL_SWITCHER
656/*
Nicolas Pitre14d2ca62012-11-28 18:48:19 -0500657 * gic_send_sgi - send a SGI directly to given CPU interface number
658 *
659 * cpu_id: the ID for the destination CPU interface
660 * irq: the IPI number to send a SGI for
661 */
662void gic_send_sgi(unsigned int cpu_id, unsigned int irq)
663{
664 BUG_ON(cpu_id >= NR_GIC_CPU_IF);
665 cpu_id = 1 << cpu_id;
666 /* this always happens on GIC0 */
667 writel_relaxed((cpu_id << 16) | irq, gic_data_dist_base(&gic_data[0]) + GIC_DIST_SOFTINT);
668}
669
670/*
Nicolas Pitreed967622012-07-05 21:33:26 -0400671 * gic_get_cpu_id - get the CPU interface ID for the specified CPU
672 *
673 * @cpu: the logical CPU number to get the GIC ID for.
674 *
675 * Return the CPU interface ID for the given logical CPU number,
676 * or -1 if the CPU number is too large or the interface ID is
677 * unknown (more than one bit set).
678 */
679int gic_get_cpu_id(unsigned int cpu)
680{
681 unsigned int cpu_bit;
682
683 if (cpu >= NR_GIC_CPU_IF)
684 return -1;
685 cpu_bit = gic_cpu_map[cpu];
686 if (cpu_bit & (cpu_bit - 1))
687 return -1;
688 return __ffs(cpu_bit);
689}
690
691/*
Nicolas Pitre1a6b69b2012-04-12 01:40:31 -0400692 * gic_migrate_target - migrate IRQs to another CPU interface
693 *
694 * @new_cpu_id: the CPU target ID to migrate IRQs to
695 *
696 * Migrate all peripheral interrupts with a target matching the current CPU
697 * to the interface corresponding to @new_cpu_id. The CPU interface mapping
698 * is also updated. Targets to other CPU interfaces are unchanged.
699 * This must be called with IRQs locally disabled.
700 */
701void gic_migrate_target(unsigned int new_cpu_id)
702{
703 unsigned int cur_cpu_id, gic_irqs, gic_nr = 0;
704 void __iomem *dist_base;
705 int i, ror_val, cpu = smp_processor_id();
706 u32 val, cur_target_mask, active_mask;
707
708 if (gic_nr >= MAX_GIC_NR)
709 BUG();
710
711 dist_base = gic_data_dist_base(&gic_data[gic_nr]);
712 if (!dist_base)
713 return;
714 gic_irqs = gic_data[gic_nr].gic_irqs;
715
716 cur_cpu_id = __ffs(gic_cpu_map[cpu]);
717 cur_target_mask = 0x01010101 << cur_cpu_id;
718 ror_val = (cur_cpu_id - new_cpu_id) & 31;
719
720 raw_spin_lock(&irq_controller_lock);
721
722 /* Update the target interface for this logical CPU */
723 gic_cpu_map[cpu] = 1 << new_cpu_id;
724
725 /*
726 * Find all the peripheral interrupts targetting the current
727 * CPU interface and migrate them to the new CPU interface.
728 * We skip DIST_TARGET 0 to 7 as they are read-only.
729 */
730 for (i = 8; i < DIV_ROUND_UP(gic_irqs, 4); i++) {
731 val = readl_relaxed(dist_base + GIC_DIST_TARGET + i * 4);
732 active_mask = val & cur_target_mask;
733 if (active_mask) {
734 val &= ~active_mask;
735 val |= ror32(active_mask, ror_val);
736 writel_relaxed(val, dist_base + GIC_DIST_TARGET + i*4);
737 }
738 }
739
740 raw_spin_unlock(&irq_controller_lock);
741
742 /*
743 * Now let's migrate and clear any potential SGIs that might be
744 * pending for us (cur_cpu_id). Since GIC_DIST_SGI_PENDING_SET
745 * is a banked register, we can only forward the SGI using
746 * GIC_DIST_SOFTINT. The original SGI source is lost but Linux
747 * doesn't use that information anyway.
748 *
749 * For the same reason we do not adjust SGI source information
750 * for previously sent SGIs by us to other CPUs either.
751 */
752 for (i = 0; i < 16; i += 4) {
753 int j;
754 val = readl_relaxed(dist_base + GIC_DIST_SGI_PENDING_SET + i);
755 if (!val)
756 continue;
757 writel_relaxed(val, dist_base + GIC_DIST_SGI_PENDING_CLEAR + i);
758 for (j = i; j < i + 4; j++) {
759 if (val & 0xff)
760 writel_relaxed((1 << (new_cpu_id + 16)) | j,
761 dist_base + GIC_DIST_SOFTINT);
762 val >>= 8;
763 }
764 }
Rob Herringb1cffeb2012-11-26 15:05:48 -0600765}
Nicolas Pitreeeb44652012-11-28 18:17:25 -0500766
767/*
768 * gic_get_sgir_physaddr - get the physical address for the SGI register
769 *
770 * REturn the physical address of the SGI register to be used
771 * by some early assembly code when the kernel is not yet available.
772 */
773static unsigned long gic_dist_physaddr;
774
775unsigned long gic_get_sgir_physaddr(void)
776{
777 if (!gic_dist_physaddr)
778 return 0;
779 return gic_dist_physaddr + GIC_DIST_SOFTINT;
780}
781
782void __init gic_init_physaddr(struct device_node *node)
783{
784 struct resource res;
785 if (of_address_to_resource(node, 0, &res) == 0) {
786 gic_dist_physaddr = res.start;
787 pr_info("GIC physical location is %#lx\n", gic_dist_physaddr);
788 }
789}
790
791#else
792#define gic_init_physaddr(node) do { } while (0)
Rob Herringb1cffeb2012-11-26 15:05:48 -0600793#endif
794
Grant Likely75294952012-02-14 14:06:57 -0700795static int gic_irq_domain_map(struct irq_domain *d, unsigned int irq,
796 irq_hw_number_t hw)
797{
798 if (hw < 32) {
799 irq_set_percpu_devid(irq);
Yingjoe Chen9a1091e2014-11-25 16:04:19 +0800800 irq_domain_set_info(d, irq, hw, &gic_chip, d->host_data,
801 handle_percpu_devid_irq, NULL, NULL);
Grant Likely75294952012-02-14 14:06:57 -0700802 set_irq_flags(irq, IRQF_VALID | IRQF_NOAUTOEN);
803 } else {
Yingjoe Chen9a1091e2014-11-25 16:04:19 +0800804 irq_domain_set_info(d, irq, hw, &gic_chip, d->host_data,
805 handle_fasteoi_irq, NULL, NULL);
Grant Likely75294952012-02-14 14:06:57 -0700806 set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
Sricharan R006e9832013-12-03 15:57:22 +0530807
808 gic_routable_irq_domain_ops->map(d, irq, hw);
Grant Likely75294952012-02-14 14:06:57 -0700809 }
Grant Likely75294952012-02-14 14:06:57 -0700810 return 0;
811}
812
Sricharan R006e9832013-12-03 15:57:22 +0530813static void gic_irq_domain_unmap(struct irq_domain *d, unsigned int irq)
814{
815 gic_routable_irq_domain_ops->unmap(d, irq);
816}
817
Grant Likely7bb69ba2012-02-14 14:06:48 -0700818static int gic_irq_domain_xlate(struct irq_domain *d,
819 struct device_node *controller,
820 const u32 *intspec, unsigned int intsize,
821 unsigned long *out_hwirq, unsigned int *out_type)
Rob Herringb3f7ed02011-09-28 21:27:52 -0500822{
Sricharan R006e9832013-12-03 15:57:22 +0530823 unsigned long ret = 0;
824
Rob Herringb3f7ed02011-09-28 21:27:52 -0500825 if (d->of_node != controller)
826 return -EINVAL;
827 if (intsize < 3)
828 return -EINVAL;
829
830 /* Get the interrupt number and add 16 to skip over SGIs */
831 *out_hwirq = intspec[1] + 16;
832
833 /* For SPIs, we need to add 16 more to get the GIC irq ID number */
Sricharan R006e9832013-12-03 15:57:22 +0530834 if (!intspec[0]) {
835 ret = gic_routable_irq_domain_ops->xlate(d, controller,
836 intspec,
837 intsize,
838 out_hwirq,
839 out_type);
840
841 if (IS_ERR_VALUE(ret))
842 return ret;
843 }
Rob Herringb3f7ed02011-09-28 21:27:52 -0500844
845 *out_type = intspec[2] & IRQ_TYPE_SENSE_MASK;
Sricharan R006e9832013-12-03 15:57:22 +0530846
847 return ret;
Rob Herringb3f7ed02011-09-28 21:27:52 -0500848}
Rob Herringb3f7ed02011-09-28 21:27:52 -0500849
Catalin Marinasc0114702013-01-14 18:05:37 +0000850#ifdef CONFIG_SMP
Paul Gortmaker8c37bb32013-06-19 11:32:08 -0400851static int gic_secondary_init(struct notifier_block *nfb, unsigned long action,
852 void *hcpu)
Catalin Marinasc0114702013-01-14 18:05:37 +0000853{
Shawn Guo8b6fd652013-06-12 19:30:27 +0800854 if (action == CPU_STARTING || action == CPU_STARTING_FROZEN)
Catalin Marinasc0114702013-01-14 18:05:37 +0000855 gic_cpu_init(&gic_data[0]);
856 return NOTIFY_OK;
857}
858
859/*
860 * Notifier for enabling the GIC CPU interface. Set an arbitrarily high
861 * priority because the GIC needs to be up before the ARM generic timers.
862 */
Paul Gortmaker8c37bb32013-06-19 11:32:08 -0400863static struct notifier_block gic_cpu_notifier = {
Catalin Marinasc0114702013-01-14 18:05:37 +0000864 .notifier_call = gic_secondary_init,
865 .priority = 100,
866};
867#endif
868
Yingjoe Chen9a1091e2014-11-25 16:04:19 +0800869static int gic_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
870 unsigned int nr_irqs, void *arg)
871{
872 int i, ret;
873 irq_hw_number_t hwirq;
874 unsigned int type = IRQ_TYPE_NONE;
875 struct of_phandle_args *irq_data = arg;
876
877 ret = gic_irq_domain_xlate(domain, irq_data->np, irq_data->args,
878 irq_data->args_count, &hwirq, &type);
879 if (ret)
880 return ret;
881
882 for (i = 0; i < nr_irqs; i++)
883 gic_irq_domain_map(domain, virq + i, hwirq + i);
884
885 return 0;
886}
887
888static const struct irq_domain_ops gic_irq_domain_hierarchy_ops = {
889 .xlate = gic_irq_domain_xlate,
890 .alloc = gic_irq_domain_alloc,
891 .free = irq_domain_free_irqs_top,
892};
893
Stephen Boyd68593582014-03-04 17:02:01 -0800894static const struct irq_domain_ops gic_irq_domain_ops = {
Grant Likely75294952012-02-14 14:06:57 -0700895 .map = gic_irq_domain_map,
Sricharan R006e9832013-12-03 15:57:22 +0530896 .unmap = gic_irq_domain_unmap,
Grant Likely7bb69ba2012-02-14 14:06:48 -0700897 .xlate = gic_irq_domain_xlate,
Rob Herring4294f8b2011-09-28 21:25:31 -0500898};
899
Sricharan R006e9832013-12-03 15:57:22 +0530900/* Default functions for routable irq domain */
901static int gic_routable_irq_domain_map(struct irq_domain *d, unsigned int irq,
902 irq_hw_number_t hw)
903{
904 return 0;
905}
906
907static void gic_routable_irq_domain_unmap(struct irq_domain *d,
908 unsigned int irq)
909{
910}
911
912static int gic_routable_irq_domain_xlate(struct irq_domain *d,
913 struct device_node *controller,
914 const u32 *intspec, unsigned int intsize,
915 unsigned long *out_hwirq,
916 unsigned int *out_type)
917{
918 *out_hwirq += 16;
919 return 0;
920}
921
Will Deaconf3d147b2014-08-26 15:13:26 +0100922static const struct irq_domain_ops gic_default_routable_irq_domain_ops = {
Sricharan R006e9832013-12-03 15:57:22 +0530923 .map = gic_routable_irq_domain_map,
924 .unmap = gic_routable_irq_domain_unmap,
925 .xlate = gic_routable_irq_domain_xlate,
926};
927
928const struct irq_domain_ops *gic_routable_irq_domain_ops =
929 &gic_default_routable_irq_domain_ops;
930
Marc Zyngierdb0d4db2011-11-12 16:09:49 +0000931void __init gic_init_bases(unsigned int gic_nr, int irq_start,
932 void __iomem *dist_base, void __iomem *cpu_base,
Grant Likely75294952012-02-14 14:06:57 -0700933 u32 percpu_offset, struct device_node *node)
Russell Kingb580b892010-12-04 15:55:14 +0000934{
Grant Likely75294952012-02-14 14:06:57 -0700935 irq_hw_number_t hwirq_base;
Russell Kingbef8f9e2010-12-04 16:50:58 +0000936 struct gic_chip_data *gic;
Nicolas Pitre384a2902012-04-11 18:55:48 -0400937 int gic_irqs, irq_base, i;
Sricharan R006e9832013-12-03 15:57:22 +0530938 int nr_routable_irqs;
Russell Kingbef8f9e2010-12-04 16:50:58 +0000939
940 BUG_ON(gic_nr >= MAX_GIC_NR);
941
942 gic = &gic_data[gic_nr];
Marc Zyngierdb0d4db2011-11-12 16:09:49 +0000943#ifdef CONFIG_GIC_NON_BANKED
944 if (percpu_offset) { /* Frankein-GIC without banked registers... */
945 unsigned int cpu;
946
947 gic->dist_base.percpu_base = alloc_percpu(void __iomem *);
948 gic->cpu_base.percpu_base = alloc_percpu(void __iomem *);
949 if (WARN_ON(!gic->dist_base.percpu_base ||
950 !gic->cpu_base.percpu_base)) {
951 free_percpu(gic->dist_base.percpu_base);
952 free_percpu(gic->cpu_base.percpu_base);
953 return;
954 }
955
956 for_each_possible_cpu(cpu) {
Tomasz Figa29e697b2014-07-17 17:23:44 +0200957 u32 mpidr = cpu_logical_map(cpu);
958 u32 core_id = MPIDR_AFFINITY_LEVEL(mpidr, 0);
959 unsigned long offset = percpu_offset * core_id;
Marc Zyngierdb0d4db2011-11-12 16:09:49 +0000960 *per_cpu_ptr(gic->dist_base.percpu_base, cpu) = dist_base + offset;
961 *per_cpu_ptr(gic->cpu_base.percpu_base, cpu) = cpu_base + offset;
962 }
963
964 gic_set_base_accessor(gic, gic_get_percpu_base);
965 } else
966#endif
967 { /* Normal, sane GIC... */
968 WARN(percpu_offset,
969 "GIC_NON_BANKED not enabled, ignoring %08x offset!",
970 percpu_offset);
971 gic->dist_base.common_base = dist_base;
972 gic->cpu_base.common_base = cpu_base;
973 gic_set_base_accessor(gic, gic_get_common_base);
974 }
Russell Kingbef8f9e2010-12-04 16:50:58 +0000975
Rob Herring4294f8b2011-09-28 21:25:31 -0500976 /*
Nicolas Pitre384a2902012-04-11 18:55:48 -0400977 * Initialize the CPU interface map to all CPUs.
978 * It will be refined as each CPU probes its ID.
979 */
980 for (i = 0; i < NR_GIC_CPU_IF; i++)
981 gic_cpu_map[i] = 0xff;
982
983 /*
Rob Herring4294f8b2011-09-28 21:25:31 -0500984 * Find out how many interrupts are supported.
985 * The GIC only supports up to 1020 interrupt sources.
986 */
Marc Zyngierdb0d4db2011-11-12 16:09:49 +0000987 gic_irqs = readl_relaxed(gic_data_dist_base(gic) + GIC_DIST_CTR) & 0x1f;
Rob Herring4294f8b2011-09-28 21:25:31 -0500988 gic_irqs = (gic_irqs + 1) * 32;
989 if (gic_irqs > 1020)
990 gic_irqs = 1020;
991 gic->gic_irqs = gic_irqs;
992
Yingjoe Chen9a1091e2014-11-25 16:04:19 +0800993 if (node) { /* DT case */
994 const struct irq_domain_ops *ops = &gic_irq_domain_hierarchy_ops;
Sricharan R006e9832013-12-03 15:57:22 +0530995
Yingjoe Chen9a1091e2014-11-25 16:04:19 +0800996 if (!of_property_read_u32(node, "arm,routable-irqs",
997 &nr_routable_irqs)) {
998 ops = &gic_irq_domain_ops;
999 gic_irqs = nr_routable_irqs;
1000 }
1001
1002 gic->domain = irq_domain_add_linear(node, gic_irqs, ops, gic);
1003 } else { /* Non-DT case */
1004 /*
1005 * For primary GICs, skip over SGIs.
1006 * For secondary GICs, skip over PPIs, too.
1007 */
1008 if (gic_nr == 0 && (irq_start & 31) > 0) {
1009 hwirq_base = 16;
1010 if (irq_start != -1)
1011 irq_start = (irq_start & ~31) + 16;
1012 } else {
1013 hwirq_base = 32;
1014 }
1015
1016 gic_irqs -= hwirq_base; /* calculate # of irqs to allocate */
1017
Sricharan R006e9832013-12-03 15:57:22 +05301018 irq_base = irq_alloc_descs(irq_start, 16, gic_irqs,
1019 numa_node_id());
1020 if (IS_ERR_VALUE(irq_base)) {
1021 WARN(1, "Cannot allocate irq_descs @ IRQ%d, assuming pre-allocated\n",
1022 irq_start);
1023 irq_base = irq_start;
1024 }
1025
1026 gic->domain = irq_domain_add_legacy(node, gic_irqs, irq_base,
1027 hwirq_base, &gic_irq_domain_ops, gic);
Rob Herringf37a53c2011-10-21 17:14:27 -05001028 }
Sricharan R006e9832013-12-03 15:57:22 +05301029
Grant Likely75294952012-02-14 14:06:57 -07001030 if (WARN_ON(!gic->domain))
1031 return;
Russell Kingbef8f9e2010-12-04 16:50:58 +00001032
Mark Rutland08332df2013-11-28 14:21:40 +00001033 if (gic_nr == 0) {
Rob Herringb1cffeb2012-11-26 15:05:48 -06001034#ifdef CONFIG_SMP
Mark Rutland08332df2013-11-28 14:21:40 +00001035 set_smp_cross_call(gic_raise_softirq);
1036 register_cpu_notifier(&gic_cpu_notifier);
Rob Herringb1cffeb2012-11-26 15:05:48 -06001037#endif
Mark Rutland08332df2013-11-28 14:21:40 +00001038 set_handle_irq(gic_handle_irq);
1039 }
Rob Herringcfed7d62012-11-03 12:59:51 -05001040
Colin Cross9c128452011-06-13 00:45:59 +00001041 gic_chip.flags |= gic_arch_extn.flags;
Rob Herring4294f8b2011-09-28 21:25:31 -05001042 gic_dist_init(gic);
Russell Kingbef8f9e2010-12-04 16:50:58 +00001043 gic_cpu_init(gic);
Colin Cross254056f2011-02-10 12:54:10 -08001044 gic_pm_init(gic);
Russell Kingb580b892010-12-04 15:55:14 +00001045}
1046
Rob Herringb3f7ed02011-09-28 21:27:52 -05001047#ifdef CONFIG_OF
Sachin Kamat46f101d2013-03-13 15:05:15 +05301048static int gic_cnt __initdata;
Rob Herringb3f7ed02011-09-28 21:27:52 -05001049
Stephen Boyd68593582014-03-04 17:02:01 -08001050static int __init
1051gic_of_init(struct device_node *node, struct device_node *parent)
Rob Herringb3f7ed02011-09-28 21:27:52 -05001052{
1053 void __iomem *cpu_base;
1054 void __iomem *dist_base;
Marc Zyngierdb0d4db2011-11-12 16:09:49 +00001055 u32 percpu_offset;
Rob Herringb3f7ed02011-09-28 21:27:52 -05001056 int irq;
Rob Herringb3f7ed02011-09-28 21:27:52 -05001057
1058 if (WARN_ON(!node))
1059 return -ENODEV;
1060
1061 dist_base = of_iomap(node, 0);
1062 WARN(!dist_base, "unable to map gic dist registers\n");
1063
1064 cpu_base = of_iomap(node, 1);
1065 WARN(!cpu_base, "unable to map gic cpu registers\n");
1066
Marc Zyngierdb0d4db2011-11-12 16:09:49 +00001067 if (of_property_read_u32(node, "cpu-offset", &percpu_offset))
1068 percpu_offset = 0;
1069
Grant Likely75294952012-02-14 14:06:57 -07001070 gic_init_bases(gic_cnt, -1, dist_base, cpu_base, percpu_offset, node);
Nicolas Pitreeeb44652012-11-28 18:17:25 -05001071 if (!gic_cnt)
1072 gic_init_physaddr(node);
Rob Herringb3f7ed02011-09-28 21:27:52 -05001073
1074 if (parent) {
1075 irq = irq_of_parse_and_map(node, 0);
1076 gic_cascade_irq(gic_cnt, irq);
1077 }
Suravee Suthikulpanit853a33c2014-11-25 18:47:22 +00001078
1079 if (IS_ENABLED(CONFIG_ARM_GIC_V2M))
1080 gicv2m_of_init(node, gic_data[gic_cnt].domain);
1081
Rob Herringb3f7ed02011-09-28 21:27:52 -05001082 gic_cnt++;
1083 return 0;
1084}
Suravee Suthikulpanit144cb082014-07-15 00:03:03 +02001085IRQCHIP_DECLARE(gic_400, "arm,gic-400", gic_of_init);
Linus Walleijfa6e2ee2014-10-01 09:29:22 +02001086IRQCHIP_DECLARE(arm11mp_gic, "arm,arm11mp-gic", gic_of_init);
1087IRQCHIP_DECLARE(arm1176jzf_dc_gic, "arm,arm1176jzf-devchip-gic", gic_of_init);
Rob Herring81243e42012-11-20 21:21:40 -06001088IRQCHIP_DECLARE(cortex_a15_gic, "arm,cortex-a15-gic", gic_of_init);
1089IRQCHIP_DECLARE(cortex_a9_gic, "arm,cortex-a9-gic", gic_of_init);
Matthias Bruggera97e80272014-07-03 13:58:52 +02001090IRQCHIP_DECLARE(cortex_a7_gic, "arm,cortex-a7-gic", gic_of_init);
Rob Herring81243e42012-11-20 21:21:40 -06001091IRQCHIP_DECLARE(msm_8660_qgic, "qcom,msm-8660-qgic", gic_of_init);
1092IRQCHIP_DECLARE(msm_qgic2, "qcom,msm-qgic2", gic_of_init);
1093
Rob Herringb3f7ed02011-09-28 21:27:52 -05001094#endif
Tomasz Nowickid60fc382015-03-24 14:02:49 +00001095
1096#ifdef CONFIG_ACPI
1097static phys_addr_t dist_phy_base, cpu_phy_base __initdata;
1098
1099static int __init
1100gic_acpi_parse_madt_cpu(struct acpi_subtable_header *header,
1101 const unsigned long end)
1102{
1103 struct acpi_madt_generic_interrupt *processor;
1104 phys_addr_t gic_cpu_base;
1105 static int cpu_base_assigned;
1106
1107 processor = (struct acpi_madt_generic_interrupt *)header;
1108
1109 if (BAD_MADT_ENTRY(processor, end))
1110 return -EINVAL;
1111
1112 /*
1113 * There is no support for non-banked GICv1/2 register in ACPI spec.
1114 * All CPU interface addresses have to be the same.
1115 */
1116 gic_cpu_base = processor->base_address;
1117 if (cpu_base_assigned && gic_cpu_base != cpu_phy_base)
1118 return -EINVAL;
1119
1120 cpu_phy_base = gic_cpu_base;
1121 cpu_base_assigned = 1;
1122 return 0;
1123}
1124
1125static int __init
1126gic_acpi_parse_madt_distributor(struct acpi_subtable_header *header,
1127 const unsigned long end)
1128{
1129 struct acpi_madt_generic_distributor *dist;
1130
1131 dist = (struct acpi_madt_generic_distributor *)header;
1132
1133 if (BAD_MADT_ENTRY(dist, end))
1134 return -EINVAL;
1135
1136 dist_phy_base = dist->base_address;
1137 return 0;
1138}
1139
1140int __init
1141gic_v2_acpi_init(struct acpi_table_header *table)
1142{
1143 void __iomem *cpu_base, *dist_base;
1144 int count;
1145
1146 /* Collect CPU base addresses */
1147 count = acpi_parse_entries(ACPI_SIG_MADT,
1148 sizeof(struct acpi_table_madt),
1149 gic_acpi_parse_madt_cpu, table,
1150 ACPI_MADT_TYPE_GENERIC_INTERRUPT, 0);
1151 if (count <= 0) {
1152 pr_err("No valid GICC entries exist\n");
1153 return -EINVAL;
1154 }
1155
1156 /*
1157 * Find distributor base address. We expect one distributor entry since
1158 * ACPI 5.1 spec neither support multi-GIC instances nor GIC cascade.
1159 */
1160 count = acpi_parse_entries(ACPI_SIG_MADT,
1161 sizeof(struct acpi_table_madt),
1162 gic_acpi_parse_madt_distributor, table,
1163 ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR, 0);
1164 if (count <= 0) {
1165 pr_err("No valid GICD entries exist\n");
1166 return -EINVAL;
1167 } else if (count > 1) {
1168 pr_err("More than one GICD entry detected\n");
1169 return -EINVAL;
1170 }
1171
1172 cpu_base = ioremap(cpu_phy_base, ACPI_GIC_CPU_IF_MEM_SIZE);
1173 if (!cpu_base) {
1174 pr_err("Unable to map GICC registers\n");
1175 return -ENOMEM;
1176 }
1177
1178 dist_base = ioremap(dist_phy_base, ACPI_GICV2_DIST_MEM_SIZE);
1179 if (!dist_base) {
1180 pr_err("Unable to map GICD registers\n");
1181 iounmap(cpu_base);
1182 return -ENOMEM;
1183 }
1184
1185 /*
1186 * Initialize zero GIC instance (no multi-GIC support). Also, set GIC
1187 * as default IRQ domain to allow for GSI registration and GSI to IRQ
1188 * number translation (see acpi_register_gsi() and acpi_gsi_to_irq()).
1189 */
1190 gic_init_bases(0, -1, dist_base, cpu_base, 0, NULL);
1191 irq_set_default_host(gic_data[0].domain);
Lorenzo Pieralisid8f4f162015-03-24 17:58:51 +00001192
1193 acpi_irq_model = ACPI_IRQ_MODEL_GIC;
Tomasz Nowickid60fc382015-03-24 14:02:49 +00001194 return 0;
1195}
1196#endif