blob: 911c9555ef9ed1a37a28e56da73334fa3ceda754 [file] [log] [blame]
Marc Zyngier021f6532014-06-30 16:01:31 +01001/*
2 * Copyright (C) 2013, 2014 ARM Limited, All Rights Reserved.
3 * Author: Marc Zyngier <marc.zyngier@arm.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
Julien Grall68628bb2016-04-11 16:32:55 +010018#define pr_fmt(fmt) "GICv3: " fmt
19
Tomasz Nowickiffa7d612016-01-19 14:11:15 +010020#include <linux/acpi.h>
Marc Zyngier021f6532014-06-30 16:01:31 +010021#include <linux/cpu.h>
Sudeep Holla3708d522014-08-26 16:03:35 +010022#include <linux/cpu_pm.h>
Marc Zyngier021f6532014-06-30 16:01:31 +010023#include <linux/delay.h>
24#include <linux/interrupt.h>
Tomasz Nowickiffa7d612016-01-19 14:11:15 +010025#include <linux/irqdomain.h>
Marc Zyngier021f6532014-06-30 16:01:31 +010026#include <linux/of.h>
27#include <linux/of_address.h>
28#include <linux/of_irq.h>
29#include <linux/percpu.h>
30#include <linux/slab.h>
Channagoud Kadabidf164542016-09-19 20:24:21 -070031#include <linux/msm_rtb.h>
Marc Zyngier021f6532014-06-30 16:01:31 +010032
Joel Porquet41a83e02015-07-07 17:11:46 -040033#include <linux/irqchip.h>
Julien Grall1839e572016-04-11 16:32:57 +010034#include <linux/irqchip/arm-gic-common.h>
Marc Zyngier021f6532014-06-30 16:01:31 +010035#include <linux/irqchip/arm-gic-v3.h>
Marc Zyngiere3825ba2016-04-11 09:57:54 +010036#include <linux/irqchip/irq-partition-percpu.h>
Marc Zyngier021f6532014-06-30 16:01:31 +010037
38#include <asm/cputype.h>
39#include <asm/exception.h>
40#include <asm/smp_plat.h>
Marc Zyngier0b6a3da2015-08-26 17:00:42 +010041#include <asm/virt.h>
Marc Zyngier021f6532014-06-30 16:01:31 +010042
43#include "irq-gic-common.h"
Marc Zyngier021f6532014-06-30 16:01:31 +010044
Marc Zyngierf5c14342014-11-24 14:35:10 +000045struct redist_region {
46 void __iomem *redist_base;
47 phys_addr_t phys_base;
Tomasz Nowickib70fb7a2016-01-19 14:11:16 +010048 bool single_redist;
Marc Zyngierf5c14342014-11-24 14:35:10 +000049};
50
Marc Zyngier021f6532014-06-30 16:01:31 +010051struct gic_chip_data {
Marc Zyngiere3825ba2016-04-11 09:57:54 +010052 struct fwnode_handle *fwnode;
Marc Zyngier021f6532014-06-30 16:01:31 +010053 void __iomem *dist_base;
Marc Zyngierf5c14342014-11-24 14:35:10 +000054 struct redist_region *redist_regions;
55 struct rdists rdists;
Marc Zyngier021f6532014-06-30 16:01:31 +010056 struct irq_domain *domain;
57 u64 redist_stride;
Marc Zyngierf5c14342014-11-24 14:35:10 +000058 u32 nr_redist_regions;
Marc Zyngier021f6532014-06-30 16:01:31 +010059 unsigned int irq_nr;
Marc Zyngiere3825ba2016-04-11 09:57:54 +010060 struct partition_desc *ppi_descs[16];
Marc Zyngier021f6532014-06-30 16:01:31 +010061};
62
63static struct gic_chip_data gic_data __read_mostly;
Marc Zyngier0b6a3da2015-08-26 17:00:42 +010064static struct static_key supports_deactivate = STATIC_KEY_INIT_TRUE;
Marc Zyngier021f6532014-06-30 16:01:31 +010065
Julien Grall1839e572016-04-11 16:32:57 +010066static struct gic_kvm_info gic_v3_kvm_info;
67
Marc Zyngierf5c14342014-11-24 14:35:10 +000068#define gic_data_rdist() (this_cpu_ptr(gic_data.rdists.rdist))
69#define gic_data_rdist_rd_base() (gic_data_rdist()->rd_base)
Marc Zyngier021f6532014-06-30 16:01:31 +010070#define gic_data_rdist_sgi_base() (gic_data_rdist_rd_base() + SZ_64K)
71
72/* Our default, arbitrary priority value. Linux only uses one anyway. */
73#define DEFAULT_PMR_VALUE 0xf0
74
75static inline unsigned int gic_irq(struct irq_data *d)
76{
77 return d->hwirq;
78}
79
80static inline int gic_irq_in_rdist(struct irq_data *d)
81{
82 return gic_irq(d) < 32;
83}
84
85static inline void __iomem *gic_dist_base(struct irq_data *d)
86{
87 if (gic_irq_in_rdist(d)) /* SGI+PPI -> SGI_base for this CPU */
88 return gic_data_rdist_sgi_base();
89
90 if (d->hwirq <= 1023) /* SPI -> dist_base */
91 return gic_data.dist_base;
92
Marc Zyngier021f6532014-06-30 16:01:31 +010093 return NULL;
94}
95
96static void gic_do_wait_for_rwp(void __iomem *base)
97{
98 u32 count = 1000000; /* 1s! */
99
100 while (readl_relaxed(base + GICD_CTLR) & GICD_CTLR_RWP) {
101 count--;
102 if (!count) {
103 pr_err_ratelimited("RWP timeout, gone fishing\n");
104 return;
105 }
106 cpu_relax();
107 udelay(1);
108 };
109}
110
111/* Wait for completion of a distributor change */
112static void gic_dist_wait_for_rwp(void)
113{
114 gic_do_wait_for_rwp(gic_data.dist_base);
115}
116
117/* Wait for completion of a redistributor change */
118static void gic_redist_wait_for_rwp(void)
119{
120 gic_do_wait_for_rwp(gic_data_rdist_rd_base());
121}
122
Jean-Philippe Brucker7936e912015-10-01 13:47:14 +0100123#ifdef CONFIG_ARM64
Robert Richter8ac2a172015-09-21 22:58:39 +0200124static DEFINE_STATIC_KEY_FALSE(is_cavium_thunderx);
Robert Richter6d4e11c2015-09-21 22:58:35 +0200125
126static u64 __maybe_unused gic_read_iar(void)
127{
Robert Richter8ac2a172015-09-21 22:58:39 +0200128 if (static_branch_unlikely(&is_cavium_thunderx))
Robert Richter6d4e11c2015-09-21 22:58:35 +0200129 return gic_read_iar_cavium_thunderx();
130 else
131 return gic_read_iar_common();
132}
Jean-Philippe Brucker7936e912015-10-01 13:47:14 +0100133#endif
Marc Zyngier021f6532014-06-30 16:01:31 +0100134
Sudeep Hollaa2c22512014-08-26 16:03:34 +0100135static void gic_enable_redist(bool enable)
Marc Zyngier021f6532014-06-30 16:01:31 +0100136{
137 void __iomem *rbase;
138 u32 count = 1000000; /* 1s! */
139 u32 val;
140
141 rbase = gic_data_rdist_rd_base();
142
Marc Zyngier021f6532014-06-30 16:01:31 +0100143 val = readl_relaxed(rbase + GICR_WAKER);
Sudeep Hollaa2c22512014-08-26 16:03:34 +0100144 if (enable)
145 /* Wake up this CPU redistributor */
146 val &= ~GICR_WAKER_ProcessorSleep;
147 else
148 val |= GICR_WAKER_ProcessorSleep;
Marc Zyngier021f6532014-06-30 16:01:31 +0100149 writel_relaxed(val, rbase + GICR_WAKER);
150
Sudeep Hollaa2c22512014-08-26 16:03:34 +0100151 if (!enable) { /* Check that GICR_WAKER is writeable */
152 val = readl_relaxed(rbase + GICR_WAKER);
153 if (!(val & GICR_WAKER_ProcessorSleep))
154 return; /* No PM support in this redistributor */
155 }
156
Dan Carpenterd102eb52016-10-14 10:26:21 +0300157 while (--count) {
Sudeep Hollaa2c22512014-08-26 16:03:34 +0100158 val = readl_relaxed(rbase + GICR_WAKER);
Andrew Jonescf1d9d12016-05-11 21:23:17 +0200159 if (enable ^ (bool)(val & GICR_WAKER_ChildrenAsleep))
Sudeep Hollaa2c22512014-08-26 16:03:34 +0100160 break;
Marc Zyngier021f6532014-06-30 16:01:31 +0100161 cpu_relax();
162 udelay(1);
163 };
Sudeep Hollaa2c22512014-08-26 16:03:34 +0100164 if (!count)
165 pr_err_ratelimited("redistributor failed to %s...\n",
166 enable ? "wakeup" : "sleep");
Marc Zyngier021f6532014-06-30 16:01:31 +0100167}
168
169/*
170 * Routines to disable, enable, EOI and route interrupts
171 */
Marc Zyngierb594c6e2015-03-18 11:01:24 +0000172static int gic_peek_irq(struct irq_data *d, u32 offset)
173{
174 u32 mask = 1 << (gic_irq(d) % 32);
175 void __iomem *base;
176
177 if (gic_irq_in_rdist(d))
178 base = gic_data_rdist_sgi_base();
179 else
180 base = gic_data.dist_base;
181
182 return !!(readl_relaxed(base + offset + (gic_irq(d) / 32) * 4) & mask);
183}
184
Marc Zyngier021f6532014-06-30 16:01:31 +0100185static void gic_poke_irq(struct irq_data *d, u32 offset)
186{
187 u32 mask = 1 << (gic_irq(d) % 32);
188 void (*rwp_wait)(void);
189 void __iomem *base;
190
191 if (gic_irq_in_rdist(d)) {
192 base = gic_data_rdist_sgi_base();
193 rwp_wait = gic_redist_wait_for_rwp;
194 } else {
195 base = gic_data.dist_base;
196 rwp_wait = gic_dist_wait_for_rwp;
197 }
198
199 writel_relaxed(mask, base + offset + (gic_irq(d) / 32) * 4);
200 rwp_wait();
201}
202
Marc Zyngier021f6532014-06-30 16:01:31 +0100203static void gic_mask_irq(struct irq_data *d)
204{
205 gic_poke_irq(d, GICD_ICENABLER);
206}
207
Marc Zyngier0b6a3da2015-08-26 17:00:42 +0100208static void gic_eoimode1_mask_irq(struct irq_data *d)
209{
210 gic_mask_irq(d);
Marc Zyngier530bf352015-08-26 17:00:43 +0100211 /*
212 * When masking a forwarded interrupt, make sure it is
213 * deactivated as well.
214 *
215 * This ensures that an interrupt that is getting
216 * disabled/masked will not get "stuck", because there is
217 * noone to deactivate it (guest is being terminated).
218 */
Thomas Gleixner4df7f542015-09-15 13:19:16 +0200219 if (irqd_is_forwarded_to_vcpu(d))
Marc Zyngier530bf352015-08-26 17:00:43 +0100220 gic_poke_irq(d, GICD_ICACTIVER);
Marc Zyngier0b6a3da2015-08-26 17:00:42 +0100221}
222
Marc Zyngier021f6532014-06-30 16:01:31 +0100223static void gic_unmask_irq(struct irq_data *d)
224{
225 gic_poke_irq(d, GICD_ISENABLER);
226}
227
Marc Zyngierb594c6e2015-03-18 11:01:24 +0000228static int gic_irq_set_irqchip_state(struct irq_data *d,
229 enum irqchip_irq_state which, bool val)
230{
231 u32 reg;
232
233 if (d->hwirq >= gic_data.irq_nr) /* PPI/SPI only */
234 return -EINVAL;
235
236 switch (which) {
237 case IRQCHIP_STATE_PENDING:
238 reg = val ? GICD_ISPENDR : GICD_ICPENDR;
239 break;
240
241 case IRQCHIP_STATE_ACTIVE:
242 reg = val ? GICD_ISACTIVER : GICD_ICACTIVER;
243 break;
244
245 case IRQCHIP_STATE_MASKED:
246 reg = val ? GICD_ICENABLER : GICD_ISENABLER;
247 break;
248
249 default:
250 return -EINVAL;
251 }
252
253 gic_poke_irq(d, reg);
254 return 0;
255}
256
257static int gic_irq_get_irqchip_state(struct irq_data *d,
258 enum irqchip_irq_state which, bool *val)
259{
260 if (d->hwirq >= gic_data.irq_nr) /* PPI/SPI only */
261 return -EINVAL;
262
263 switch (which) {
264 case IRQCHIP_STATE_PENDING:
265 *val = gic_peek_irq(d, GICD_ISPENDR);
266 break;
267
268 case IRQCHIP_STATE_ACTIVE:
269 *val = gic_peek_irq(d, GICD_ISACTIVER);
270 break;
271
272 case IRQCHIP_STATE_MASKED:
273 *val = !gic_peek_irq(d, GICD_ISENABLER);
274 break;
275
276 default:
277 return -EINVAL;
278 }
279
280 return 0;
281}
282
Marc Zyngier021f6532014-06-30 16:01:31 +0100283static void gic_eoi_irq(struct irq_data *d)
284{
285 gic_write_eoir(gic_irq(d));
286}
287
Marc Zyngier0b6a3da2015-08-26 17:00:42 +0100288static void gic_eoimode1_eoi_irq(struct irq_data *d)
289{
290 /*
Marc Zyngier530bf352015-08-26 17:00:43 +0100291 * No need to deactivate an LPI, or an interrupt that
292 * is is getting forwarded to a vcpu.
Marc Zyngier0b6a3da2015-08-26 17:00:42 +0100293 */
Thomas Gleixner4df7f542015-09-15 13:19:16 +0200294 if (gic_irq(d) >= 8192 || irqd_is_forwarded_to_vcpu(d))
Marc Zyngier0b6a3da2015-08-26 17:00:42 +0100295 return;
296 gic_write_dir(gic_irq(d));
297}
298
Marc Zyngier021f6532014-06-30 16:01:31 +0100299static int gic_set_type(struct irq_data *d, unsigned int type)
300{
301 unsigned int irq = gic_irq(d);
302 void (*rwp_wait)(void);
303 void __iomem *base;
304
305 /* Interrupt configuration for SGIs can't be changed */
306 if (irq < 16)
307 return -EINVAL;
308
Liviu Dudaufb7e7de2015-01-20 16:52:59 +0000309 /* SPIs have restrictions on the supported types */
310 if (irq >= 32 && type != IRQ_TYPE_LEVEL_HIGH &&
311 type != IRQ_TYPE_EDGE_RISING)
Marc Zyngier021f6532014-06-30 16:01:31 +0100312 return -EINVAL;
313
314 if (gic_irq_in_rdist(d)) {
315 base = gic_data_rdist_sgi_base();
316 rwp_wait = gic_redist_wait_for_rwp;
317 } else {
318 base = gic_data.dist_base;
319 rwp_wait = gic_dist_wait_for_rwp;
320 }
321
Liviu Dudaufb7e7de2015-01-20 16:52:59 +0000322 return gic_configure_irq(irq, type, base, rwp_wait);
Marc Zyngier021f6532014-06-30 16:01:31 +0100323}
324
Marc Zyngier530bf352015-08-26 17:00:43 +0100325static int gic_irq_set_vcpu_affinity(struct irq_data *d, void *vcpu)
326{
Thomas Gleixner4df7f542015-09-15 13:19:16 +0200327 if (vcpu)
328 irqd_set_forwarded_to_vcpu(d);
329 else
330 irqd_clr_forwarded_to_vcpu(d);
Marc Zyngier530bf352015-08-26 17:00:43 +0100331 return 0;
332}
333
Jean-Philippe Bruckerf6c86a42015-10-01 13:47:15 +0100334static u64 gic_mpidr_to_affinity(unsigned long mpidr)
Marc Zyngier021f6532014-06-30 16:01:31 +0100335{
336 u64 aff;
337
Jean-Philippe Bruckerf6c86a42015-10-01 13:47:15 +0100338 aff = ((u64)MPIDR_AFFINITY_LEVEL(mpidr, 3) << 32 |
Marc Zyngier021f6532014-06-30 16:01:31 +0100339 MPIDR_AFFINITY_LEVEL(mpidr, 2) << 16 |
340 MPIDR_AFFINITY_LEVEL(mpidr, 1) << 8 |
341 MPIDR_AFFINITY_LEVEL(mpidr, 0));
342
343 return aff;
344}
345
346static asmlinkage void __exception_irq_entry gic_handle_irq(struct pt_regs *regs)
347{
Jean-Philippe Bruckerf6c86a42015-10-01 13:47:15 +0100348 u32 irqnr;
Marc Zyngier021f6532014-06-30 16:01:31 +0100349
350 do {
351 irqnr = gic_read_iar();
352
Marc Zyngierda33f312014-11-24 14:35:18 +0000353 if (likely(irqnr > 15 && irqnr < 1020) || irqnr >= 8192) {
Marc Zyngierebc6de02014-08-26 11:03:33 +0100354 int err;
Marc Zyngier0b6a3da2015-08-26 17:00:42 +0100355
Channagoud Kadabidf164542016-09-19 20:24:21 -0700356 uncached_logk(LOGK_IRQ, (void *)(uintptr_t)irqnr);
Marc Zyngier0b6a3da2015-08-26 17:00:42 +0100357 if (static_key_true(&supports_deactivate))
358 gic_write_eoir(irqnr);
359
Marc Zyngierebc6de02014-08-26 11:03:33 +0100360 err = handle_domain_irq(gic_data.domain, irqnr, regs);
361 if (err) {
Marc Zyngierda33f312014-11-24 14:35:18 +0000362 WARN_ONCE(true, "Unexpected interrupt received!\n");
Marc Zyngier0b6a3da2015-08-26 17:00:42 +0100363 if (static_key_true(&supports_deactivate)) {
364 if (irqnr < 8192)
365 gic_write_dir(irqnr);
366 } else {
367 gic_write_eoir(irqnr);
368 }
Marc Zyngier021f6532014-06-30 16:01:31 +0100369 }
Marc Zyngierebc6de02014-08-26 11:03:33 +0100370 continue;
Marc Zyngier021f6532014-06-30 16:01:31 +0100371 }
372 if (irqnr < 16) {
Channagoud Kadabidf164542016-09-19 20:24:21 -0700373 uncached_logk(LOGK_IRQ, (void *)(uintptr_t)irqnr);
Marc Zyngier021f6532014-06-30 16:01:31 +0100374 gic_write_eoir(irqnr);
Marc Zyngier0b6a3da2015-08-26 17:00:42 +0100375 if (static_key_true(&supports_deactivate))
376 gic_write_dir(irqnr);
Marc Zyngier021f6532014-06-30 16:01:31 +0100377#ifdef CONFIG_SMP
Will Deaconf86c4fb2016-04-26 12:00:00 +0100378 /*
379 * Unlike GICv2, we don't need an smp_rmb() here.
380 * The control dependency from gic_read_iar to
381 * the ISB in gic_write_eoir is enough to ensure
382 * that any shared data read by handle_IPI will
383 * be read after the ACK.
384 */
Marc Zyngier021f6532014-06-30 16:01:31 +0100385 handle_IPI(irqnr, regs);
386#else
387 WARN_ONCE(true, "Unexpected SGI received!\n");
388#endif
389 continue;
390 }
391 } while (irqnr != ICC_IAR1_EL1_SPURIOUS);
392}
393
394static void __init gic_dist_init(void)
395{
396 unsigned int i;
397 u64 affinity;
398 void __iomem *base = gic_data.dist_base;
399
400 /* Disable the distributor */
401 writel_relaxed(0, base + GICD_CTLR);
402 gic_dist_wait_for_rwp();
403
Marc Zyngier7c9b9732016-05-06 19:41:56 +0100404 /*
405 * Configure SPIs as non-secure Group-1. This will only matter
406 * if the GIC only has a single security state. This will not
407 * do the right thing if the kernel is running in secure mode,
408 * but that's not the intended use case anyway.
409 */
410 for (i = 32; i < gic_data.irq_nr; i += 32)
411 writel_relaxed(~0, base + GICD_IGROUPR + i / 8);
412
Marc Zyngier021f6532014-06-30 16:01:31 +0100413 gic_dist_config(base, gic_data.irq_nr, gic_dist_wait_for_rwp);
414
415 /* Enable distributor with ARE, Group1 */
416 writel_relaxed(GICD_CTLR_ARE_NS | GICD_CTLR_ENABLE_G1A | GICD_CTLR_ENABLE_G1,
417 base + GICD_CTLR);
418
419 /*
420 * Set all global interrupts to the boot CPU only. ARE must be
421 * enabled.
422 */
423 affinity = gic_mpidr_to_affinity(cpu_logical_map(smp_processor_id()));
424 for (i = 32; i < gic_data.irq_nr; i++)
Jean-Philippe Brucker72c97122015-10-01 13:47:16 +0100425 gic_write_irouter(affinity, base + GICD_IROUTER + i * 8);
Marc Zyngier021f6532014-06-30 16:01:31 +0100426}
427
428static int gic_populate_rdist(void)
429{
Jean-Philippe Bruckerf6c86a42015-10-01 13:47:15 +0100430 unsigned long mpidr = cpu_logical_map(smp_processor_id());
Marc Zyngier021f6532014-06-30 16:01:31 +0100431 u64 typer;
432 u32 aff;
433 int i;
434
435 /*
436 * Convert affinity to a 32bit value that can be matched to
437 * GICR_TYPER bits [63:32].
438 */
439 aff = (MPIDR_AFFINITY_LEVEL(mpidr, 3) << 24 |
440 MPIDR_AFFINITY_LEVEL(mpidr, 2) << 16 |
441 MPIDR_AFFINITY_LEVEL(mpidr, 1) << 8 |
442 MPIDR_AFFINITY_LEVEL(mpidr, 0));
443
Marc Zyngierf5c14342014-11-24 14:35:10 +0000444 for (i = 0; i < gic_data.nr_redist_regions; i++) {
445 void __iomem *ptr = gic_data.redist_regions[i].redist_base;
Marc Zyngier021f6532014-06-30 16:01:31 +0100446 u32 reg;
447
448 reg = readl_relaxed(ptr + GICR_PIDR2) & GIC_PIDR2_ARCH_MASK;
449 if (reg != GIC_PIDR2_ARCH_GICv3 &&
450 reg != GIC_PIDR2_ARCH_GICv4) { /* We're in trouble... */
451 pr_warn("No redistributor present @%p\n", ptr);
452 break;
453 }
454
455 do {
Jean-Philippe Brucker72c97122015-10-01 13:47:16 +0100456 typer = gic_read_typer(ptr + GICR_TYPER);
Marc Zyngier021f6532014-06-30 16:01:31 +0100457 if ((typer >> 32) == aff) {
Marc Zyngierf5c14342014-11-24 14:35:10 +0000458 u64 offset = ptr - gic_data.redist_regions[i].redist_base;
Marc Zyngier021f6532014-06-30 16:01:31 +0100459 gic_data_rdist_rd_base() = ptr;
Marc Zyngierf5c14342014-11-24 14:35:10 +0000460 gic_data_rdist()->phys_base = gic_data.redist_regions[i].phys_base + offset;
Jean-Philippe Bruckerf6c86a42015-10-01 13:47:15 +0100461 pr_info("CPU%d: found redistributor %lx region %d:%pa\n",
462 smp_processor_id(), mpidr, i,
463 &gic_data_rdist()->phys_base);
Marc Zyngier021f6532014-06-30 16:01:31 +0100464 return 0;
465 }
466
Tomasz Nowickib70fb7a2016-01-19 14:11:16 +0100467 if (gic_data.redist_regions[i].single_redist)
468 break;
469
Marc Zyngier021f6532014-06-30 16:01:31 +0100470 if (gic_data.redist_stride) {
471 ptr += gic_data.redist_stride;
472 } else {
473 ptr += SZ_64K * 2; /* Skip RD_base + SGI_base */
474 if (typer & GICR_TYPER_VLPIS)
475 ptr += SZ_64K * 2; /* Skip VLPI_base + reserved page */
476 }
477 } while (!(typer & GICR_TYPER_LAST));
478 }
479
480 /* We couldn't even deal with ourselves... */
Jean-Philippe Bruckerf6c86a42015-10-01 13:47:15 +0100481 WARN(true, "CPU%d: mpidr %lx has no re-distributor!\n",
482 smp_processor_id(), mpidr);
Marc Zyngier021f6532014-06-30 16:01:31 +0100483 return -ENODEV;
484}
485
Sudeep Holla3708d522014-08-26 16:03:35 +0100486static void gic_cpu_sys_reg_init(void)
Marc Zyngier021f6532014-06-30 16:01:31 +0100487{
Marc Zyngier7cabd002015-09-30 11:48:01 +0100488 /*
489 * Need to check that the SRE bit has actually been set. If
490 * not, it means that SRE is disabled at EL2. We're going to
491 * die painfully, and there is nothing we can do about it.
492 *
493 * Kindly inform the luser.
494 */
495 if (!gic_enable_sre())
496 pr_err("GIC: unable to set SRE (disabled at EL2), panic ahead\n");
Marc Zyngier021f6532014-06-30 16:01:31 +0100497
498 /* Set priority mask register */
499 gic_write_pmr(DEFAULT_PMR_VALUE);
500
Daniel Thompson91ef8442016-08-19 17:13:09 +0100501 /*
502 * Some firmwares hand over to the kernel with the BPR changed from
503 * its reset value (and with a value large enough to prevent
504 * any pre-emptive interrupts from working at all). Writing a zero
505 * to BPR restores is reset value.
506 */
507 gic_write_bpr1(0);
508
Marc Zyngier0b6a3da2015-08-26 17:00:42 +0100509 if (static_key_true(&supports_deactivate)) {
510 /* EOI drops priority only (mode 1) */
511 gic_write_ctlr(ICC_CTLR_EL1_EOImode_drop);
512 } else {
513 /* EOI deactivates interrupt too (mode 0) */
514 gic_write_ctlr(ICC_CTLR_EL1_EOImode_drop_dir);
515 }
Marc Zyngier021f6532014-06-30 16:01:31 +0100516
517 /* ... and let's hit the road... */
518 gic_write_grpen1(1);
519}
520
Marc Zyngierda33f312014-11-24 14:35:18 +0000521static int gic_dist_supports_lpis(void)
522{
523 return !!(readl_relaxed(gic_data.dist_base + GICD_TYPER) & GICD_TYPER_LPIS);
524}
525
Marc Zyngier021f6532014-06-30 16:01:31 +0100526static void gic_cpu_init(void)
527{
528 void __iomem *rbase;
529
530 /* Register ourselves with the rest of the world */
531 if (gic_populate_rdist())
532 return;
533
Sudeep Hollaa2c22512014-08-26 16:03:34 +0100534 gic_enable_redist(true);
Marc Zyngier021f6532014-06-30 16:01:31 +0100535
536 rbase = gic_data_rdist_sgi_base();
537
Marc Zyngier7c9b9732016-05-06 19:41:56 +0100538 /* Configure SGIs/PPIs as non-secure Group-1 */
539 writel_relaxed(~0, rbase + GICR_IGROUPR0);
540
Marc Zyngier021f6532014-06-30 16:01:31 +0100541 gic_cpu_config(rbase, gic_redist_wait_for_rwp);
542
Marc Zyngierda33f312014-11-24 14:35:18 +0000543 /* Give LPIs a spin */
Channagoud Kadabifdb06642016-09-19 20:55:36 -0700544 if (IS_ENABLED(CONFIG_ARM_GIC_V3_ITS) && gic_dist_supports_lpis() &&
545 !IS_ENABLED(CONFIG_ARM_GIC_V3_ACL))
Marc Zyngierda33f312014-11-24 14:35:18 +0000546 its_cpu_init();
547
Sudeep Holla3708d522014-08-26 16:03:35 +0100548 /* initialise system registers */
549 gic_cpu_sys_reg_init();
Marc Zyngier021f6532014-06-30 16:01:31 +0100550}
551
552#ifdef CONFIG_SMP
Marc Zyngier021f6532014-06-30 16:01:31 +0100553
Richard Cochran6670a6d2016-07-13 17:16:05 +0000554static int gic_starting_cpu(unsigned int cpu)
555{
556 gic_cpu_init();
557 return 0;
558}
Marc Zyngier021f6532014-06-30 16:01:31 +0100559
560static u16 gic_compute_target_list(int *base_cpu, const struct cpumask *mask,
Jean-Philippe Bruckerf6c86a42015-10-01 13:47:15 +0100561 unsigned long cluster_id)
Marc Zyngier021f6532014-06-30 16:01:31 +0100562{
James Morse727653d2016-09-19 18:29:15 +0100563 int next_cpu, cpu = *base_cpu;
Jean-Philippe Bruckerf6c86a42015-10-01 13:47:15 +0100564 unsigned long mpidr = cpu_logical_map(cpu);
Marc Zyngier021f6532014-06-30 16:01:31 +0100565 u16 tlist = 0;
566
567 while (cpu < nr_cpu_ids) {
568 /*
569 * If we ever get a cluster of more than 16 CPUs, just
570 * scream and skip that CPU.
571 */
572 if (WARN_ON((mpidr & 0xff) >= 16))
573 goto out;
574
575 tlist |= 1 << (mpidr & 0xf);
576
James Morse727653d2016-09-19 18:29:15 +0100577 next_cpu = cpumask_next(cpu, mask);
578 if (next_cpu >= nr_cpu_ids)
Marc Zyngier021f6532014-06-30 16:01:31 +0100579 goto out;
James Morse727653d2016-09-19 18:29:15 +0100580 cpu = next_cpu;
Marc Zyngier021f6532014-06-30 16:01:31 +0100581
582 mpidr = cpu_logical_map(cpu);
583
584 if (cluster_id != (mpidr & ~0xffUL)) {
585 cpu--;
586 goto out;
587 }
588 }
589out:
590 *base_cpu = cpu;
591 return tlist;
592}
593
Andre Przywara7e580272014-11-12 13:46:06 +0000594#define MPIDR_TO_SGI_AFFINITY(cluster_id, level) \
595 (MPIDR_AFFINITY_LEVEL(cluster_id, level) \
596 << ICC_SGI1R_AFFINITY_## level ##_SHIFT)
597
Marc Zyngier021f6532014-06-30 16:01:31 +0100598static void gic_send_sgi(u64 cluster_id, u16 tlist, unsigned int irq)
599{
600 u64 val;
601
Andre Przywara7e580272014-11-12 13:46:06 +0000602 val = (MPIDR_TO_SGI_AFFINITY(cluster_id, 3) |
603 MPIDR_TO_SGI_AFFINITY(cluster_id, 2) |
604 irq << ICC_SGI1R_SGI_ID_SHIFT |
605 MPIDR_TO_SGI_AFFINITY(cluster_id, 1) |
606 tlist << ICC_SGI1R_TARGET_LIST_SHIFT);
Marc Zyngier021f6532014-06-30 16:01:31 +0100607
608 pr_debug("CPU%d: ICC_SGI1R_EL1 %llx\n", smp_processor_id(), val);
609 gic_write_sgi1r(val);
610}
611
612static void gic_raise_softirq(const struct cpumask *mask, unsigned int irq)
613{
614 int cpu;
615
616 if (WARN_ON(irq >= 16))
617 return;
618
619 /*
620 * Ensure that stores to Normal memory are visible to the
621 * other CPUs before issuing the IPI.
622 */
623 smp_wmb();
624
Rusty Russellf9b531f2015-03-05 10:49:16 +1030625 for_each_cpu(cpu, mask) {
Jean-Philippe Bruckerf6c86a42015-10-01 13:47:15 +0100626 unsigned long cluster_id = cpu_logical_map(cpu) & ~0xffUL;
Marc Zyngier021f6532014-06-30 16:01:31 +0100627 u16 tlist;
628
629 tlist = gic_compute_target_list(&cpu, mask, cluster_id);
630 gic_send_sgi(cluster_id, tlist, irq);
631 }
632
633 /* Force the above writes to ICC_SGI1R_EL1 to be executed */
634 isb();
635}
636
637static void gic_smp_init(void)
638{
639 set_smp_cross_call(gic_raise_softirq);
Richard Cochran6670a6d2016-07-13 17:16:05 +0000640 cpuhp_setup_state_nocalls(CPUHP_AP_IRQ_GICV3_STARTING,
641 "AP_IRQ_GICV3_STARTING", gic_starting_cpu,
642 NULL);
Marc Zyngier021f6532014-06-30 16:01:31 +0100643}
644
645static int gic_set_affinity(struct irq_data *d, const struct cpumask *mask_val,
646 bool force)
647{
648 unsigned int cpu = cpumask_any_and(mask_val, cpu_online_mask);
649 void __iomem *reg;
650 int enabled;
651 u64 val;
652
653 if (gic_irq_in_rdist(d))
654 return -EINVAL;
655
656 /* If interrupt was enabled, disable it first */
657 enabled = gic_peek_irq(d, GICD_ISENABLER);
658 if (enabled)
659 gic_mask_irq(d);
660
661 reg = gic_dist_base(d) + GICD_IROUTER + (gic_irq(d) * 8);
662 val = gic_mpidr_to_affinity(cpu_logical_map(cpu));
663
Jean-Philippe Brucker72c97122015-10-01 13:47:16 +0100664 gic_write_irouter(val, reg);
Marc Zyngier021f6532014-06-30 16:01:31 +0100665
666 /*
667 * If the interrupt was enabled, enabled it again. Otherwise,
668 * just wait for the distributor to have digested our changes.
669 */
670 if (enabled)
671 gic_unmask_irq(d);
672 else
673 gic_dist_wait_for_rwp();
674
Antoine Tenart0fc6fa22016-02-19 16:22:43 +0100675 return IRQ_SET_MASK_OK_DONE;
Marc Zyngier021f6532014-06-30 16:01:31 +0100676}
677#else
678#define gic_set_affinity NULL
679#define gic_smp_init() do { } while(0)
680#endif
681
Sudeep Holla3708d522014-08-26 16:03:35 +0100682#ifdef CONFIG_CPU_PM
Sudeep Hollaccd94322016-08-17 13:49:19 +0100683/* Check whether it's single security state view */
684static bool gic_dist_security_disabled(void)
685{
686 return readl_relaxed(gic_data.dist_base + GICD_CTLR) & GICD_CTLR_DS;
687}
688
Sudeep Holla3708d522014-08-26 16:03:35 +0100689static int gic_cpu_pm_notifier(struct notifier_block *self,
690 unsigned long cmd, void *v)
691{
692 if (cmd == CPU_PM_EXIT) {
Sudeep Hollaccd94322016-08-17 13:49:19 +0100693 if (gic_dist_security_disabled())
694 gic_enable_redist(true);
Sudeep Holla3708d522014-08-26 16:03:35 +0100695 gic_cpu_sys_reg_init();
Sudeep Hollaccd94322016-08-17 13:49:19 +0100696 } else if (cmd == CPU_PM_ENTER && gic_dist_security_disabled()) {
Sudeep Holla3708d522014-08-26 16:03:35 +0100697 gic_write_grpen1(0);
698 gic_enable_redist(false);
699 }
700 return NOTIFY_OK;
701}
702
703static struct notifier_block gic_cpu_pm_notifier_block = {
704 .notifier_call = gic_cpu_pm_notifier,
705};
706
707static void gic_cpu_pm_init(void)
708{
709 cpu_pm_register_notifier(&gic_cpu_pm_notifier_block);
710}
711
712#else
713static inline void gic_cpu_pm_init(void) { }
714#endif /* CONFIG_CPU_PM */
715
Marc Zyngier021f6532014-06-30 16:01:31 +0100716static struct irq_chip gic_chip = {
717 .name = "GICv3",
718 .irq_mask = gic_mask_irq,
719 .irq_unmask = gic_unmask_irq,
720 .irq_eoi = gic_eoi_irq,
721 .irq_set_type = gic_set_type,
722 .irq_set_affinity = gic_set_affinity,
Marc Zyngierb594c6e2015-03-18 11:01:24 +0000723 .irq_get_irqchip_state = gic_irq_get_irqchip_state,
724 .irq_set_irqchip_state = gic_irq_set_irqchip_state,
Sudeep Holla55963c92015-06-05 11:59:57 +0100725 .flags = IRQCHIP_SET_TYPE_MASKED,
Marc Zyngier021f6532014-06-30 16:01:31 +0100726};
727
Marc Zyngier0b6a3da2015-08-26 17:00:42 +0100728static struct irq_chip gic_eoimode1_chip = {
729 .name = "GICv3",
730 .irq_mask = gic_eoimode1_mask_irq,
731 .irq_unmask = gic_unmask_irq,
732 .irq_eoi = gic_eoimode1_eoi_irq,
733 .irq_set_type = gic_set_type,
734 .irq_set_affinity = gic_set_affinity,
735 .irq_get_irqchip_state = gic_irq_get_irqchip_state,
736 .irq_set_irqchip_state = gic_irq_set_irqchip_state,
Marc Zyngier530bf352015-08-26 17:00:43 +0100737 .irq_set_vcpu_affinity = gic_irq_set_vcpu_affinity,
Marc Zyngier0b6a3da2015-08-26 17:00:42 +0100738 .flags = IRQCHIP_SET_TYPE_MASKED,
739};
740
Marc Zyngierda33f312014-11-24 14:35:18 +0000741#define GIC_ID_NR (1U << gic_data.rdists.id_bits)
742
Marc Zyngier021f6532014-06-30 16:01:31 +0100743static int gic_irq_domain_map(struct irq_domain *d, unsigned int irq,
744 irq_hw_number_t hw)
745{
Marc Zyngier0b6a3da2015-08-26 17:00:42 +0100746 struct irq_chip *chip = &gic_chip;
747
748 if (static_key_true(&supports_deactivate))
749 chip = &gic_eoimode1_chip;
750
Marc Zyngier021f6532014-06-30 16:01:31 +0100751 /* SGIs are private to the core kernel */
752 if (hw < 16)
753 return -EPERM;
Marc Zyngierda33f312014-11-24 14:35:18 +0000754 /* Nothing here */
755 if (hw >= gic_data.irq_nr && hw < 8192)
756 return -EPERM;
757 /* Off limits */
758 if (hw >= GIC_ID_NR)
759 return -EPERM;
760
Marc Zyngier021f6532014-06-30 16:01:31 +0100761 /* PPIs */
762 if (hw < 32) {
763 irq_set_percpu_devid(irq);
Marc Zyngier0b6a3da2015-08-26 17:00:42 +0100764 irq_domain_set_info(d, irq, hw, chip, d->host_data,
Marc Zyngier443acc42014-11-24 14:35:09 +0000765 handle_percpu_devid_irq, NULL, NULL);
Rob Herringd17cab42015-08-29 18:01:22 -0500766 irq_set_status_flags(irq, IRQ_NOAUTOEN);
Marc Zyngier021f6532014-06-30 16:01:31 +0100767 }
768 /* SPIs */
769 if (hw >= 32 && hw < gic_data.irq_nr) {
Marc Zyngier0b6a3da2015-08-26 17:00:42 +0100770 irq_domain_set_info(d, irq, hw, chip, d->host_data,
Marc Zyngier443acc42014-11-24 14:35:09 +0000771 handle_fasteoi_irq, NULL, NULL);
Rob Herringd17cab42015-08-29 18:01:22 -0500772 irq_set_probe(irq);
Marc Zyngier021f6532014-06-30 16:01:31 +0100773 }
Marc Zyngierda33f312014-11-24 14:35:18 +0000774 /* LPIs */
775 if (hw >= 8192 && hw < GIC_ID_NR) {
776 if (!gic_dist_supports_lpis())
777 return -EPERM;
Marc Zyngier0b6a3da2015-08-26 17:00:42 +0100778 irq_domain_set_info(d, irq, hw, chip, d->host_data,
Marc Zyngierda33f312014-11-24 14:35:18 +0000779 handle_fasteoi_irq, NULL, NULL);
Marc Zyngierda33f312014-11-24 14:35:18 +0000780 }
781
Marc Zyngier021f6532014-06-30 16:01:31 +0100782 return 0;
783}
784
Marc Zyngierf833f572015-10-13 12:51:33 +0100785static int gic_irq_domain_translate(struct irq_domain *d,
786 struct irq_fwspec *fwspec,
787 unsigned long *hwirq,
788 unsigned int *type)
Marc Zyngier021f6532014-06-30 16:01:31 +0100789{
Marc Zyngierf833f572015-10-13 12:51:33 +0100790 if (is_of_node(fwspec->fwnode)) {
791 if (fwspec->param_count < 3)
792 return -EINVAL;
Marc Zyngier021f6532014-06-30 16:01:31 +0100793
Marc Zyngierdb8c70e2015-10-14 12:27:16 +0100794 switch (fwspec->param[0]) {
795 case 0: /* SPI */
796 *hwirq = fwspec->param[1] + 32;
797 break;
798 case 1: /* PPI */
799 *hwirq = fwspec->param[1] + 16;
800 break;
801 case GIC_IRQ_TYPE_LPI: /* LPI */
802 *hwirq = fwspec->param[1];
803 break;
804 default:
805 return -EINVAL;
806 }
Marc Zyngierf833f572015-10-13 12:51:33 +0100807
808 *type = fwspec->param[2] & IRQ_TYPE_SENSE_MASK;
809 return 0;
Marc Zyngier021f6532014-06-30 16:01:31 +0100810 }
811
Tomasz Nowickiffa7d612016-01-19 14:11:15 +0100812 if (is_fwnode_irqchip(fwspec->fwnode)) {
813 if(fwspec->param_count != 2)
814 return -EINVAL;
815
816 *hwirq = fwspec->param[0];
817 *type = fwspec->param[1];
818 return 0;
819 }
820
Marc Zyngierf833f572015-10-13 12:51:33 +0100821 return -EINVAL;
Marc Zyngier021f6532014-06-30 16:01:31 +0100822}
823
Marc Zyngier443acc42014-11-24 14:35:09 +0000824static int gic_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
825 unsigned int nr_irqs, void *arg)
826{
827 int i, ret;
828 irq_hw_number_t hwirq;
829 unsigned int type = IRQ_TYPE_NONE;
Marc Zyngierf833f572015-10-13 12:51:33 +0100830 struct irq_fwspec *fwspec = arg;
Marc Zyngier443acc42014-11-24 14:35:09 +0000831
Marc Zyngierf833f572015-10-13 12:51:33 +0100832 ret = gic_irq_domain_translate(domain, fwspec, &hwirq, &type);
Marc Zyngier443acc42014-11-24 14:35:09 +0000833 if (ret)
834 return ret;
835
836 for (i = 0; i < nr_irqs; i++)
837 gic_irq_domain_map(domain, virq + i, hwirq + i);
838
839 return 0;
840}
841
842static void gic_irq_domain_free(struct irq_domain *domain, unsigned int virq,
843 unsigned int nr_irqs)
844{
845 int i;
846
847 for (i = 0; i < nr_irqs; i++) {
848 struct irq_data *d = irq_domain_get_irq_data(domain, virq + i);
849 irq_set_handler(virq + i, NULL);
850 irq_domain_reset_irq_data(d);
851 }
852}
853
Marc Zyngiere3825ba2016-04-11 09:57:54 +0100854static int gic_irq_domain_select(struct irq_domain *d,
855 struct irq_fwspec *fwspec,
856 enum irq_domain_bus_token bus_token)
857{
858 /* Not for us */
859 if (fwspec->fwnode != d->fwnode)
860 return 0;
861
862 /* If this is not DT, then we have a single domain */
863 if (!is_of_node(fwspec->fwnode))
864 return 1;
865
866 /*
867 * If this is a PPI and we have a 4th (non-null) parameter,
868 * then we need to match the partition domain.
869 */
870 if (fwspec->param_count >= 4 &&
871 fwspec->param[0] == 1 && fwspec->param[3] != 0)
872 return d == partition_get_domain(gic_data.ppi_descs[fwspec->param[1]]);
873
874 return d == gic_data.domain;
875}
876
Marc Zyngier021f6532014-06-30 16:01:31 +0100877static const struct irq_domain_ops gic_irq_domain_ops = {
Marc Zyngierf833f572015-10-13 12:51:33 +0100878 .translate = gic_irq_domain_translate,
Marc Zyngier443acc42014-11-24 14:35:09 +0000879 .alloc = gic_irq_domain_alloc,
880 .free = gic_irq_domain_free,
Marc Zyngiere3825ba2016-04-11 09:57:54 +0100881 .select = gic_irq_domain_select,
882};
883
884static int partition_domain_translate(struct irq_domain *d,
885 struct irq_fwspec *fwspec,
886 unsigned long *hwirq,
887 unsigned int *type)
888{
889 struct device_node *np;
890 int ret;
891
892 np = of_find_node_by_phandle(fwspec->param[3]);
893 if (WARN_ON(!np))
894 return -EINVAL;
895
896 ret = partition_translate_id(gic_data.ppi_descs[fwspec->param[1]],
897 of_node_to_fwnode(np));
898 if (ret < 0)
899 return ret;
900
901 *hwirq = ret;
902 *type = fwspec->param[2] & IRQ_TYPE_SENSE_MASK;
903
904 return 0;
905}
906
907static const struct irq_domain_ops partition_domain_ops = {
908 .translate = partition_domain_translate,
909 .select = gic_irq_domain_select,
Marc Zyngier021f6532014-06-30 16:01:31 +0100910};
911
Robert Richter6d4e11c2015-09-21 22:58:35 +0200912static void gicv3_enable_quirks(void)
913{
Jean-Philippe Brucker7936e912015-10-01 13:47:14 +0100914#ifdef CONFIG_ARM64
Robert Richter6d4e11c2015-09-21 22:58:35 +0200915 if (cpus_have_cap(ARM64_WORKAROUND_CAVIUM_23154))
Robert Richter8ac2a172015-09-21 22:58:39 +0200916 static_branch_enable(&is_cavium_thunderx);
Jean-Philippe Brucker7936e912015-10-01 13:47:14 +0100917#endif
Robert Richter6d4e11c2015-09-21 22:58:35 +0200918}
919
Tomasz Nowickidb57d742016-01-19 14:11:14 +0100920static int __init gic_init_bases(void __iomem *dist_base,
921 struct redist_region *rdist_regs,
922 u32 nr_redist_regions,
923 u64 redist_stride,
924 struct fwnode_handle *handle)
925{
Tomasz Nowickidb57d742016-01-19 14:11:14 +0100926 u32 typer;
927 int gic_irqs;
928 int err;
929
930 if (!is_hyp_mode_available())
931 static_key_slow_dec(&supports_deactivate);
932
933 if (static_key_true(&supports_deactivate))
934 pr_info("GIC: Using split EOI/Deactivate mode\n");
935
Marc Zyngiere3825ba2016-04-11 09:57:54 +0100936 gic_data.fwnode = handle;
Tomasz Nowickidb57d742016-01-19 14:11:14 +0100937 gic_data.dist_base = dist_base;
938 gic_data.redist_regions = rdist_regs;
939 gic_data.nr_redist_regions = nr_redist_regions;
940 gic_data.redist_stride = redist_stride;
941
942 gicv3_enable_quirks();
943
944 /*
945 * Find out how many interrupts are supported.
946 * The GIC only supports up to 1020 interrupt sources (SGI+PPI+SPI)
947 */
948 typer = readl_relaxed(gic_data.dist_base + GICD_TYPER);
949 gic_data.rdists.id_bits = GICD_TYPER_ID_BITS(typer);
950 gic_irqs = GICD_TYPER_IRQS(typer);
951 if (gic_irqs > 1020)
952 gic_irqs = 1020;
953 gic_data.irq_nr = gic_irqs;
954
955 gic_data.domain = irq_domain_create_tree(handle, &gic_irq_domain_ops,
956 &gic_data);
957 gic_data.rdists.rdist = alloc_percpu(typeof(*gic_data.rdists.rdist));
958
959 if (WARN_ON(!gic_data.domain) || WARN_ON(!gic_data.rdists.rdist)) {
960 err = -ENOMEM;
961 goto out_free;
962 }
963
964 set_handle_irq(gic_handle_irq);
965
Tomasz Nowickidb57d742016-01-19 14:11:14 +0100966 if (IS_ENABLED(CONFIG_ARM_GIC_V3_ITS) && gic_dist_supports_lpis() &&
Kyle Yan65be4a52016-10-31 15:05:00 -0700967 !IS_ENABLED(CONFIG_ARM_GIC_V3_ACL))
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +0200968 its_init(handle, &gic_data.rdists, gic_data.domain);
Tomasz Nowickidb57d742016-01-19 14:11:14 +0100969
970 gic_smp_init();
971 gic_dist_init();
972 gic_cpu_init();
973 gic_cpu_pm_init();
974
975 return 0;
976
977out_free:
978 if (gic_data.domain)
979 irq_domain_remove(gic_data.domain);
980 free_percpu(gic_data.rdists.rdist);
981 return err;
982}
983
984static int __init gic_validate_dist_version(void __iomem *dist_base)
985{
986 u32 reg = readl_relaxed(dist_base + GICD_PIDR2) & GIC_PIDR2_ARCH_MASK;
987
988 if (reg != GIC_PIDR2_ARCH_GICv3 && reg != GIC_PIDR2_ARCH_GICv4)
989 return -ENODEV;
990
991 return 0;
992}
993
Marc Zyngiere3825ba2016-04-11 09:57:54 +0100994static int get_cpu_number(struct device_node *dn)
995{
996 const __be32 *cell;
997 u64 hwid;
998 int i;
999
1000 cell = of_get_property(dn, "reg", NULL);
1001 if (!cell)
1002 return -1;
1003
1004 hwid = of_read_number(cell, of_n_addr_cells(dn));
1005
1006 /*
1007 * Non affinity bits must be set to 0 in the DT
1008 */
1009 if (hwid & ~MPIDR_HWID_BITMASK)
1010 return -1;
1011
1012 for (i = 0; i < num_possible_cpus(); i++)
1013 if (cpu_logical_map(i) == hwid)
1014 return i;
1015
1016 return -1;
1017}
1018
1019/* Create all possible partitions at boot time */
Linus Torvalds7beaa242016-05-19 11:27:09 -07001020static void __init gic_populate_ppi_partitions(struct device_node *gic_node)
Marc Zyngiere3825ba2016-04-11 09:57:54 +01001021{
1022 struct device_node *parts_node, *child_part;
1023 int part_idx = 0, i;
1024 int nr_parts;
1025 struct partition_affinity *parts;
1026
1027 parts_node = of_find_node_by_name(gic_node, "ppi-partitions");
1028 if (!parts_node)
1029 return;
1030
1031 nr_parts = of_get_child_count(parts_node);
1032
1033 if (!nr_parts)
1034 return;
1035
1036 parts = kzalloc(sizeof(*parts) * nr_parts, GFP_KERNEL);
1037 if (WARN_ON(!parts))
1038 return;
1039
1040 for_each_child_of_node(parts_node, child_part) {
1041 struct partition_affinity *part;
1042 int n;
1043
1044 part = &parts[part_idx];
1045
1046 part->partition_id = of_node_to_fwnode(child_part);
1047
1048 pr_info("GIC: PPI partition %s[%d] { ",
1049 child_part->name, part_idx);
1050
1051 n = of_property_count_elems_of_size(child_part, "affinity",
1052 sizeof(u32));
1053 WARN_ON(n <= 0);
1054
1055 for (i = 0; i < n; i++) {
1056 int err, cpu;
1057 u32 cpu_phandle;
1058 struct device_node *cpu_node;
1059
1060 err = of_property_read_u32_index(child_part, "affinity",
1061 i, &cpu_phandle);
1062 if (WARN_ON(err))
1063 continue;
1064
1065 cpu_node = of_find_node_by_phandle(cpu_phandle);
1066 if (WARN_ON(!cpu_node))
1067 continue;
1068
1069 cpu = get_cpu_number(cpu_node);
1070 if (WARN_ON(cpu == -1))
1071 continue;
1072
1073 pr_cont("%s[%d] ", cpu_node->full_name, cpu);
1074
1075 cpumask_set_cpu(cpu, &part->mask);
1076 }
1077
1078 pr_cont("}\n");
1079 part_idx++;
1080 }
1081
1082 for (i = 0; i < 16; i++) {
1083 unsigned int irq;
1084 struct partition_desc *desc;
1085 struct irq_fwspec ppi_fwspec = {
1086 .fwnode = gic_data.fwnode,
1087 .param_count = 3,
1088 .param = {
1089 [0] = 1,
1090 [1] = i,
1091 [2] = IRQ_TYPE_NONE,
1092 },
1093 };
1094
1095 irq = irq_create_fwspec_mapping(&ppi_fwspec);
1096 if (WARN_ON(!irq))
1097 continue;
1098 desc = partition_create_desc(gic_data.fwnode, parts, nr_parts,
1099 irq, &partition_domain_ops);
1100 if (WARN_ON(!desc))
1101 continue;
1102
1103 gic_data.ppi_descs[i] = desc;
1104 }
1105}
1106
Julien Grall1839e572016-04-11 16:32:57 +01001107static void __init gic_of_setup_kvm_info(struct device_node *node)
1108{
1109 int ret;
1110 struct resource r;
1111 u32 gicv_idx;
1112
1113 gic_v3_kvm_info.type = GIC_V3;
1114
1115 gic_v3_kvm_info.maint_irq = irq_of_parse_and_map(node, 0);
1116 if (!gic_v3_kvm_info.maint_irq)
1117 return;
1118
1119 if (of_property_read_u32(node, "#redistributor-regions",
1120 &gicv_idx))
1121 gicv_idx = 1;
1122
1123 gicv_idx += 3; /* Also skip GICD, GICC, GICH */
1124 ret = of_address_to_resource(node, gicv_idx, &r);
1125 if (!ret)
1126 gic_v3_kvm_info.vcpu = r;
1127
1128 gic_set_kvm_info(&gic_v3_kvm_info);
1129}
1130
Marc Zyngier021f6532014-06-30 16:01:31 +01001131static int __init gic_of_init(struct device_node *node, struct device_node *parent)
1132{
1133 void __iomem *dist_base;
Marc Zyngierf5c14342014-11-24 14:35:10 +00001134 struct redist_region *rdist_regs;
Marc Zyngier021f6532014-06-30 16:01:31 +01001135 u64 redist_stride;
Marc Zyngierf5c14342014-11-24 14:35:10 +00001136 u32 nr_redist_regions;
Tomasz Nowickidb57d742016-01-19 14:11:14 +01001137 int err, i;
Marc Zyngier021f6532014-06-30 16:01:31 +01001138
1139 dist_base = of_iomap(node, 0);
1140 if (!dist_base) {
1141 pr_err("%s: unable to map gic dist registers\n",
1142 node->full_name);
1143 return -ENXIO;
1144 }
1145
Tomasz Nowickidb57d742016-01-19 14:11:14 +01001146 err = gic_validate_dist_version(dist_base);
1147 if (err) {
Marc Zyngier021f6532014-06-30 16:01:31 +01001148 pr_err("%s: no distributor detected, giving up\n",
1149 node->full_name);
Marc Zyngier021f6532014-06-30 16:01:31 +01001150 goto out_unmap_dist;
1151 }
1152
Marc Zyngierf5c14342014-11-24 14:35:10 +00001153 if (of_property_read_u32(node, "#redistributor-regions", &nr_redist_regions))
1154 nr_redist_regions = 1;
Marc Zyngier021f6532014-06-30 16:01:31 +01001155
Marc Zyngierf5c14342014-11-24 14:35:10 +00001156 rdist_regs = kzalloc(sizeof(*rdist_regs) * nr_redist_regions, GFP_KERNEL);
1157 if (!rdist_regs) {
Marc Zyngier021f6532014-06-30 16:01:31 +01001158 err = -ENOMEM;
1159 goto out_unmap_dist;
1160 }
1161
Marc Zyngierf5c14342014-11-24 14:35:10 +00001162 for (i = 0; i < nr_redist_regions; i++) {
1163 struct resource res;
1164 int ret;
1165
1166 ret = of_address_to_resource(node, 1 + i, &res);
1167 rdist_regs[i].redist_base = of_iomap(node, 1 + i);
1168 if (ret || !rdist_regs[i].redist_base) {
Marc Zyngier021f6532014-06-30 16:01:31 +01001169 pr_err("%s: couldn't map region %d\n",
1170 node->full_name, i);
1171 err = -ENODEV;
1172 goto out_unmap_rdist;
1173 }
Marc Zyngierf5c14342014-11-24 14:35:10 +00001174 rdist_regs[i].phys_base = res.start;
Marc Zyngier021f6532014-06-30 16:01:31 +01001175 }
1176
1177 if (of_property_read_u64(node, "redistributor-stride", &redist_stride))
1178 redist_stride = 0;
1179
Tomasz Nowickidb57d742016-01-19 14:11:14 +01001180 err = gic_init_bases(dist_base, rdist_regs, nr_redist_regions,
1181 redist_stride, &node->fwnode);
Marc Zyngiere3825ba2016-04-11 09:57:54 +01001182 if (err)
1183 goto out_unmap_rdist;
1184
1185 gic_populate_ppi_partitions(node);
Linus Torvalds7beaa242016-05-19 11:27:09 -07001186 gic_of_setup_kvm_info(node);
Marc Zyngiere3825ba2016-04-11 09:57:54 +01001187 return 0;
Marc Zyngier0b6a3da2015-08-26 17:00:42 +01001188
Marc Zyngier021f6532014-06-30 16:01:31 +01001189out_unmap_rdist:
Marc Zyngierf5c14342014-11-24 14:35:10 +00001190 for (i = 0; i < nr_redist_regions; i++)
1191 if (rdist_regs[i].redist_base)
1192 iounmap(rdist_regs[i].redist_base);
1193 kfree(rdist_regs);
Marc Zyngier021f6532014-06-30 16:01:31 +01001194out_unmap_dist:
1195 iounmap(dist_base);
1196 return err;
1197}
1198
1199IRQCHIP_DECLARE(gic_v3, "arm,gic-v3", gic_of_init);
Tomasz Nowickiffa7d612016-01-19 14:11:15 +01001200
1201#ifdef CONFIG_ACPI
Julien Grall611f0392016-04-11 16:32:56 +01001202static struct
1203{
1204 void __iomem *dist_base;
1205 struct redist_region *redist_regs;
1206 u32 nr_redist_regions;
1207 bool single_redist;
Julien Grall1839e572016-04-11 16:32:57 +01001208 u32 maint_irq;
1209 int maint_irq_mode;
1210 phys_addr_t vcpu_base;
Julien Grall611f0392016-04-11 16:32:56 +01001211} acpi_data __initdata;
Tomasz Nowickib70fb7a2016-01-19 14:11:16 +01001212
1213static void __init
1214gic_acpi_register_redist(phys_addr_t phys_base, void __iomem *redist_base)
1215{
1216 static int count = 0;
1217
Julien Grall611f0392016-04-11 16:32:56 +01001218 acpi_data.redist_regs[count].phys_base = phys_base;
1219 acpi_data.redist_regs[count].redist_base = redist_base;
1220 acpi_data.redist_regs[count].single_redist = acpi_data.single_redist;
Tomasz Nowickib70fb7a2016-01-19 14:11:16 +01001221 count++;
1222}
Tomasz Nowickiffa7d612016-01-19 14:11:15 +01001223
1224static int __init
1225gic_acpi_parse_madt_redist(struct acpi_subtable_header *header,
1226 const unsigned long end)
1227{
1228 struct acpi_madt_generic_redistributor *redist =
1229 (struct acpi_madt_generic_redistributor *)header;
1230 void __iomem *redist_base;
Tomasz Nowickiffa7d612016-01-19 14:11:15 +01001231
1232 redist_base = ioremap(redist->base_address, redist->length);
1233 if (!redist_base) {
1234 pr_err("Couldn't map GICR region @%llx\n", redist->base_address);
1235 return -ENOMEM;
1236 }
1237
Tomasz Nowickib70fb7a2016-01-19 14:11:16 +01001238 gic_acpi_register_redist(redist->base_address, redist_base);
Tomasz Nowickiffa7d612016-01-19 14:11:15 +01001239 return 0;
1240}
1241
Tomasz Nowickib70fb7a2016-01-19 14:11:16 +01001242static int __init
1243gic_acpi_parse_madt_gicc(struct acpi_subtable_header *header,
1244 const unsigned long end)
1245{
1246 struct acpi_madt_generic_interrupt *gicc =
1247 (struct acpi_madt_generic_interrupt *)header;
Julien Grall611f0392016-04-11 16:32:56 +01001248 u32 reg = readl_relaxed(acpi_data.dist_base + GICD_PIDR2) & GIC_PIDR2_ARCH_MASK;
Tomasz Nowickib70fb7a2016-01-19 14:11:16 +01001249 u32 size = reg == GIC_PIDR2_ARCH_GICv4 ? SZ_64K * 4 : SZ_64K * 2;
1250 void __iomem *redist_base;
1251
1252 redist_base = ioremap(gicc->gicr_base_address, size);
1253 if (!redist_base)
1254 return -ENOMEM;
1255
1256 gic_acpi_register_redist(gicc->gicr_base_address, redist_base);
1257 return 0;
1258}
1259
1260static int __init gic_acpi_collect_gicr_base(void)
1261{
1262 acpi_tbl_entry_handler redist_parser;
1263 enum acpi_madt_type type;
1264
Julien Grall611f0392016-04-11 16:32:56 +01001265 if (acpi_data.single_redist) {
Tomasz Nowickib70fb7a2016-01-19 14:11:16 +01001266 type = ACPI_MADT_TYPE_GENERIC_INTERRUPT;
1267 redist_parser = gic_acpi_parse_madt_gicc;
1268 } else {
1269 type = ACPI_MADT_TYPE_GENERIC_REDISTRIBUTOR;
1270 redist_parser = gic_acpi_parse_madt_redist;
1271 }
1272
1273 /* Collect redistributor base addresses in GICR entries */
1274 if (acpi_table_parse_madt(type, redist_parser, 0) > 0)
1275 return 0;
1276
1277 pr_info("No valid GICR entries exist\n");
1278 return -ENODEV;
1279}
1280
Tomasz Nowickiffa7d612016-01-19 14:11:15 +01001281static int __init gic_acpi_match_gicr(struct acpi_subtable_header *header,
1282 const unsigned long end)
1283{
1284 /* Subtable presence means that redist exists, that's it */
1285 return 0;
1286}
1287
Tomasz Nowickib70fb7a2016-01-19 14:11:16 +01001288static int __init gic_acpi_match_gicc(struct acpi_subtable_header *header,
1289 const unsigned long end)
1290{
1291 struct acpi_madt_generic_interrupt *gicc =
1292 (struct acpi_madt_generic_interrupt *)header;
1293
1294 /*
1295 * If GICC is enabled and has valid gicr base address, then it means
1296 * GICR base is presented via GICC
1297 */
1298 if ((gicc->flags & ACPI_MADT_ENABLED) && gicc->gicr_base_address)
1299 return 0;
1300
1301 return -ENODEV;
1302}
1303
1304static int __init gic_acpi_count_gicr_regions(void)
1305{
1306 int count;
1307
1308 /*
1309 * Count how many redistributor regions we have. It is not allowed
1310 * to mix redistributor description, GICR and GICC subtables have to be
1311 * mutually exclusive.
1312 */
1313 count = acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_REDISTRIBUTOR,
1314 gic_acpi_match_gicr, 0);
1315 if (count > 0) {
Julien Grall611f0392016-04-11 16:32:56 +01001316 acpi_data.single_redist = false;
Tomasz Nowickib70fb7a2016-01-19 14:11:16 +01001317 return count;
1318 }
1319
1320 count = acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_INTERRUPT,
1321 gic_acpi_match_gicc, 0);
1322 if (count > 0)
Julien Grall611f0392016-04-11 16:32:56 +01001323 acpi_data.single_redist = true;
Tomasz Nowickib70fb7a2016-01-19 14:11:16 +01001324
1325 return count;
1326}
1327
Tomasz Nowickiffa7d612016-01-19 14:11:15 +01001328static bool __init acpi_validate_gic_table(struct acpi_subtable_header *header,
1329 struct acpi_probe_entry *ape)
1330{
1331 struct acpi_madt_generic_distributor *dist;
1332 int count;
1333
1334 dist = (struct acpi_madt_generic_distributor *)header;
1335 if (dist->version != ape->driver_data)
1336 return false;
1337
1338 /* We need to do that exercise anyway, the sooner the better */
Tomasz Nowickib70fb7a2016-01-19 14:11:16 +01001339 count = gic_acpi_count_gicr_regions();
Tomasz Nowickiffa7d612016-01-19 14:11:15 +01001340 if (count <= 0)
1341 return false;
1342
Julien Grall611f0392016-04-11 16:32:56 +01001343 acpi_data.nr_redist_regions = count;
Tomasz Nowickiffa7d612016-01-19 14:11:15 +01001344 return true;
1345}
1346
Julien Grall1839e572016-04-11 16:32:57 +01001347static int __init gic_acpi_parse_virt_madt_gicc(struct acpi_subtable_header *header,
1348 const unsigned long end)
1349{
1350 struct acpi_madt_generic_interrupt *gicc =
1351 (struct acpi_madt_generic_interrupt *)header;
1352 int maint_irq_mode;
1353 static int first_madt = true;
1354
1355 /* Skip unusable CPUs */
1356 if (!(gicc->flags & ACPI_MADT_ENABLED))
1357 return 0;
1358
1359 maint_irq_mode = (gicc->flags & ACPI_MADT_VGIC_IRQ_MODE) ?
1360 ACPI_EDGE_SENSITIVE : ACPI_LEVEL_SENSITIVE;
1361
1362 if (first_madt) {
1363 first_madt = false;
1364
1365 acpi_data.maint_irq = gicc->vgic_interrupt;
1366 acpi_data.maint_irq_mode = maint_irq_mode;
1367 acpi_data.vcpu_base = gicc->gicv_base_address;
1368
1369 return 0;
1370 }
1371
1372 /*
1373 * The maintenance interrupt and GICV should be the same for every CPU
1374 */
1375 if ((acpi_data.maint_irq != gicc->vgic_interrupt) ||
1376 (acpi_data.maint_irq_mode != maint_irq_mode) ||
1377 (acpi_data.vcpu_base != gicc->gicv_base_address))
1378 return -EINVAL;
1379
1380 return 0;
1381}
1382
1383static bool __init gic_acpi_collect_virt_info(void)
1384{
1385 int count;
1386
1387 count = acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_INTERRUPT,
1388 gic_acpi_parse_virt_madt_gicc, 0);
1389
1390 return (count > 0);
1391}
1392
Tomasz Nowickiffa7d612016-01-19 14:11:15 +01001393#define ACPI_GICV3_DIST_MEM_SIZE (SZ_64K)
Julien Grall1839e572016-04-11 16:32:57 +01001394#define ACPI_GICV2_VCTRL_MEM_SIZE (SZ_4K)
1395#define ACPI_GICV2_VCPU_MEM_SIZE (SZ_8K)
1396
1397static void __init gic_acpi_setup_kvm_info(void)
1398{
1399 int irq;
1400
1401 if (!gic_acpi_collect_virt_info()) {
1402 pr_warn("Unable to get hardware information used for virtualization\n");
1403 return;
1404 }
1405
1406 gic_v3_kvm_info.type = GIC_V3;
1407
1408 irq = acpi_register_gsi(NULL, acpi_data.maint_irq,
1409 acpi_data.maint_irq_mode,
1410 ACPI_ACTIVE_HIGH);
1411 if (irq <= 0)
1412 return;
1413
1414 gic_v3_kvm_info.maint_irq = irq;
1415
1416 if (acpi_data.vcpu_base) {
1417 struct resource *vcpu = &gic_v3_kvm_info.vcpu;
1418
1419 vcpu->flags = IORESOURCE_MEM;
1420 vcpu->start = acpi_data.vcpu_base;
1421 vcpu->end = vcpu->start + ACPI_GICV2_VCPU_MEM_SIZE - 1;
1422 }
1423
1424 gic_set_kvm_info(&gic_v3_kvm_info);
1425}
Tomasz Nowickiffa7d612016-01-19 14:11:15 +01001426
1427static int __init
1428gic_acpi_init(struct acpi_subtable_header *header, const unsigned long end)
1429{
1430 struct acpi_madt_generic_distributor *dist;
1431 struct fwnode_handle *domain_handle;
Julien Grall611f0392016-04-11 16:32:56 +01001432 size_t size;
Tomasz Nowickib70fb7a2016-01-19 14:11:16 +01001433 int i, err;
Tomasz Nowickiffa7d612016-01-19 14:11:15 +01001434
1435 /* Get distributor base address */
1436 dist = (struct acpi_madt_generic_distributor *)header;
Julien Grall611f0392016-04-11 16:32:56 +01001437 acpi_data.dist_base = ioremap(dist->base_address,
1438 ACPI_GICV3_DIST_MEM_SIZE);
1439 if (!acpi_data.dist_base) {
Tomasz Nowickiffa7d612016-01-19 14:11:15 +01001440 pr_err("Unable to map GICD registers\n");
1441 return -ENOMEM;
1442 }
1443
Julien Grall611f0392016-04-11 16:32:56 +01001444 err = gic_validate_dist_version(acpi_data.dist_base);
Tomasz Nowickiffa7d612016-01-19 14:11:15 +01001445 if (err) {
Julien Grall611f0392016-04-11 16:32:56 +01001446 pr_err("No distributor detected at @%p, giving up",
1447 acpi_data.dist_base);
Tomasz Nowickiffa7d612016-01-19 14:11:15 +01001448 goto out_dist_unmap;
1449 }
1450
Julien Grall611f0392016-04-11 16:32:56 +01001451 size = sizeof(*acpi_data.redist_regs) * acpi_data.nr_redist_regions;
1452 acpi_data.redist_regs = kzalloc(size, GFP_KERNEL);
1453 if (!acpi_data.redist_regs) {
Tomasz Nowickiffa7d612016-01-19 14:11:15 +01001454 err = -ENOMEM;
1455 goto out_dist_unmap;
1456 }
1457
Tomasz Nowickib70fb7a2016-01-19 14:11:16 +01001458 err = gic_acpi_collect_gicr_base();
1459 if (err)
Tomasz Nowickiffa7d612016-01-19 14:11:15 +01001460 goto out_redist_unmap;
Tomasz Nowickiffa7d612016-01-19 14:11:15 +01001461
Julien Grall611f0392016-04-11 16:32:56 +01001462 domain_handle = irq_domain_alloc_fwnode(acpi_data.dist_base);
Tomasz Nowickiffa7d612016-01-19 14:11:15 +01001463 if (!domain_handle) {
1464 err = -ENOMEM;
1465 goto out_redist_unmap;
1466 }
1467
Julien Grall611f0392016-04-11 16:32:56 +01001468 err = gic_init_bases(acpi_data.dist_base, acpi_data.redist_regs,
1469 acpi_data.nr_redist_regions, 0, domain_handle);
Tomasz Nowickiffa7d612016-01-19 14:11:15 +01001470 if (err)
1471 goto out_fwhandle_free;
1472
1473 acpi_set_irq_model(ACPI_IRQ_MODEL_GIC, domain_handle);
Julien Grall1839e572016-04-11 16:32:57 +01001474 gic_acpi_setup_kvm_info();
1475
Tomasz Nowickiffa7d612016-01-19 14:11:15 +01001476 return 0;
1477
1478out_fwhandle_free:
1479 irq_domain_free_fwnode(domain_handle);
1480out_redist_unmap:
Julien Grall611f0392016-04-11 16:32:56 +01001481 for (i = 0; i < acpi_data.nr_redist_regions; i++)
1482 if (acpi_data.redist_regs[i].redist_base)
1483 iounmap(acpi_data.redist_regs[i].redist_base);
1484 kfree(acpi_data.redist_regs);
Tomasz Nowickiffa7d612016-01-19 14:11:15 +01001485out_dist_unmap:
Julien Grall611f0392016-04-11 16:32:56 +01001486 iounmap(acpi_data.dist_base);
Tomasz Nowickiffa7d612016-01-19 14:11:15 +01001487 return err;
1488}
1489IRQCHIP_ACPI_DECLARE(gic_v3, ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR,
1490 acpi_validate_gic_table, ACPI_MADT_GIC_VERSION_V3,
1491 gic_acpi_init);
1492IRQCHIP_ACPI_DECLARE(gic_v4, ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR,
1493 acpi_validate_gic_table, ACPI_MADT_GIC_VERSION_V4,
1494 gic_acpi_init);
1495IRQCHIP_ACPI_DECLARE(gic_v3_or_v4, ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR,
1496 acpi_validate_gic_table, ACPI_MADT_GIC_VERSION_NONE,
1497 gic_acpi_init);
1498#endif