blob: 7a5504a1ed5d091a4f2042db0ecb0692d0b907dc [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
157 while (count--) {
158 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
Marc Zyngier0b6a3da2015-08-26 17:00:42 +0100501 if (static_key_true(&supports_deactivate)) {
502 /* EOI drops priority only (mode 1) */
503 gic_write_ctlr(ICC_CTLR_EL1_EOImode_drop);
504 } else {
505 /* EOI deactivates interrupt too (mode 0) */
506 gic_write_ctlr(ICC_CTLR_EL1_EOImode_drop_dir);
507 }
Marc Zyngier021f6532014-06-30 16:01:31 +0100508
509 /* ... and let's hit the road... */
510 gic_write_grpen1(1);
511}
512
Marc Zyngierda33f312014-11-24 14:35:18 +0000513static int gic_dist_supports_lpis(void)
514{
515 return !!(readl_relaxed(gic_data.dist_base + GICD_TYPER) & GICD_TYPER_LPIS);
516}
517
Marc Zyngier021f6532014-06-30 16:01:31 +0100518static void gic_cpu_init(void)
519{
520 void __iomem *rbase;
521
522 /* Register ourselves with the rest of the world */
523 if (gic_populate_rdist())
524 return;
525
Sudeep Hollaa2c22512014-08-26 16:03:34 +0100526 gic_enable_redist(true);
Marc Zyngier021f6532014-06-30 16:01:31 +0100527
528 rbase = gic_data_rdist_sgi_base();
529
Marc Zyngier7c9b9732016-05-06 19:41:56 +0100530 /* Configure SGIs/PPIs as non-secure Group-1 */
531 writel_relaxed(~0, rbase + GICR_IGROUPR0);
532
Marc Zyngier021f6532014-06-30 16:01:31 +0100533 gic_cpu_config(rbase, gic_redist_wait_for_rwp);
534
Marc Zyngierda33f312014-11-24 14:35:18 +0000535 /* Give LPIs a spin */
Channagoud Kadabifdb06642016-09-19 20:55:36 -0700536 if (IS_ENABLED(CONFIG_ARM_GIC_V3_ITS) && gic_dist_supports_lpis() &&
537 !IS_ENABLED(CONFIG_ARM_GIC_V3_ACL))
Marc Zyngierda33f312014-11-24 14:35:18 +0000538 its_cpu_init();
539
Sudeep Holla3708d522014-08-26 16:03:35 +0100540 /* initialise system registers */
541 gic_cpu_sys_reg_init();
Marc Zyngier021f6532014-06-30 16:01:31 +0100542}
543
544#ifdef CONFIG_SMP
Marc Zyngier021f6532014-06-30 16:01:31 +0100545
Richard Cochran6670a6d2016-07-13 17:16:05 +0000546static int gic_starting_cpu(unsigned int cpu)
547{
548 gic_cpu_init();
549 return 0;
550}
Marc Zyngier021f6532014-06-30 16:01:31 +0100551
552static u16 gic_compute_target_list(int *base_cpu, const struct cpumask *mask,
Jean-Philippe Bruckerf6c86a42015-10-01 13:47:15 +0100553 unsigned long cluster_id)
Marc Zyngier021f6532014-06-30 16:01:31 +0100554{
555 int cpu = *base_cpu;
Jean-Philippe Bruckerf6c86a42015-10-01 13:47:15 +0100556 unsigned long mpidr = cpu_logical_map(cpu);
Marc Zyngier021f6532014-06-30 16:01:31 +0100557 u16 tlist = 0;
558
559 while (cpu < nr_cpu_ids) {
560 /*
561 * If we ever get a cluster of more than 16 CPUs, just
562 * scream and skip that CPU.
563 */
564 if (WARN_ON((mpidr & 0xff) >= 16))
565 goto out;
566
567 tlist |= 1 << (mpidr & 0xf);
568
569 cpu = cpumask_next(cpu, mask);
Vladimir Murzin614be382015-03-06 16:37:45 +0000570 if (cpu >= nr_cpu_ids)
Marc Zyngier021f6532014-06-30 16:01:31 +0100571 goto out;
572
573 mpidr = cpu_logical_map(cpu);
574
575 if (cluster_id != (mpidr & ~0xffUL)) {
576 cpu--;
577 goto out;
578 }
579 }
580out:
581 *base_cpu = cpu;
582 return tlist;
583}
584
Andre Przywara7e580272014-11-12 13:46:06 +0000585#define MPIDR_TO_SGI_AFFINITY(cluster_id, level) \
586 (MPIDR_AFFINITY_LEVEL(cluster_id, level) \
587 << ICC_SGI1R_AFFINITY_## level ##_SHIFT)
588
Marc Zyngier021f6532014-06-30 16:01:31 +0100589static void gic_send_sgi(u64 cluster_id, u16 tlist, unsigned int irq)
590{
591 u64 val;
592
Andre Przywara7e580272014-11-12 13:46:06 +0000593 val = (MPIDR_TO_SGI_AFFINITY(cluster_id, 3) |
594 MPIDR_TO_SGI_AFFINITY(cluster_id, 2) |
595 irq << ICC_SGI1R_SGI_ID_SHIFT |
596 MPIDR_TO_SGI_AFFINITY(cluster_id, 1) |
597 tlist << ICC_SGI1R_TARGET_LIST_SHIFT);
Marc Zyngier021f6532014-06-30 16:01:31 +0100598
599 pr_debug("CPU%d: ICC_SGI1R_EL1 %llx\n", smp_processor_id(), val);
600 gic_write_sgi1r(val);
601}
602
603static void gic_raise_softirq(const struct cpumask *mask, unsigned int irq)
604{
605 int cpu;
606
607 if (WARN_ON(irq >= 16))
608 return;
609
610 /*
611 * Ensure that stores to Normal memory are visible to the
612 * other CPUs before issuing the IPI.
613 */
614 smp_wmb();
615
Rusty Russellf9b531f2015-03-05 10:49:16 +1030616 for_each_cpu(cpu, mask) {
Jean-Philippe Bruckerf6c86a42015-10-01 13:47:15 +0100617 unsigned long cluster_id = cpu_logical_map(cpu) & ~0xffUL;
Marc Zyngier021f6532014-06-30 16:01:31 +0100618 u16 tlist;
619
620 tlist = gic_compute_target_list(&cpu, mask, cluster_id);
621 gic_send_sgi(cluster_id, tlist, irq);
622 }
623
624 /* Force the above writes to ICC_SGI1R_EL1 to be executed */
625 isb();
626}
627
628static void gic_smp_init(void)
629{
630 set_smp_cross_call(gic_raise_softirq);
Richard Cochran6670a6d2016-07-13 17:16:05 +0000631 cpuhp_setup_state_nocalls(CPUHP_AP_IRQ_GICV3_STARTING,
632 "AP_IRQ_GICV3_STARTING", gic_starting_cpu,
633 NULL);
Marc Zyngier021f6532014-06-30 16:01:31 +0100634}
635
636static int gic_set_affinity(struct irq_data *d, const struct cpumask *mask_val,
637 bool force)
638{
639 unsigned int cpu = cpumask_any_and(mask_val, cpu_online_mask);
640 void __iomem *reg;
641 int enabled;
642 u64 val;
643
644 if (gic_irq_in_rdist(d))
645 return -EINVAL;
646
647 /* If interrupt was enabled, disable it first */
648 enabled = gic_peek_irq(d, GICD_ISENABLER);
649 if (enabled)
650 gic_mask_irq(d);
651
652 reg = gic_dist_base(d) + GICD_IROUTER + (gic_irq(d) * 8);
653 val = gic_mpidr_to_affinity(cpu_logical_map(cpu));
654
Jean-Philippe Brucker72c97122015-10-01 13:47:16 +0100655 gic_write_irouter(val, reg);
Marc Zyngier021f6532014-06-30 16:01:31 +0100656
657 /*
658 * If the interrupt was enabled, enabled it again. Otherwise,
659 * just wait for the distributor to have digested our changes.
660 */
661 if (enabled)
662 gic_unmask_irq(d);
663 else
664 gic_dist_wait_for_rwp();
665
Antoine Tenart0fc6fa22016-02-19 16:22:43 +0100666 return IRQ_SET_MASK_OK_DONE;
Marc Zyngier021f6532014-06-30 16:01:31 +0100667}
668#else
669#define gic_set_affinity NULL
670#define gic_smp_init() do { } while(0)
671#endif
672
Sudeep Holla3708d522014-08-26 16:03:35 +0100673#ifdef CONFIG_CPU_PM
Sudeep Hollaccd94322016-08-17 13:49:19 +0100674/* Check whether it's single security state view */
675static bool gic_dist_security_disabled(void)
676{
677 return readl_relaxed(gic_data.dist_base + GICD_CTLR) & GICD_CTLR_DS;
678}
679
Sudeep Holla3708d522014-08-26 16:03:35 +0100680static int gic_cpu_pm_notifier(struct notifier_block *self,
681 unsigned long cmd, void *v)
682{
683 if (cmd == CPU_PM_EXIT) {
Sudeep Hollaccd94322016-08-17 13:49:19 +0100684 if (gic_dist_security_disabled())
685 gic_enable_redist(true);
Sudeep Holla3708d522014-08-26 16:03:35 +0100686 gic_cpu_sys_reg_init();
Sudeep Hollaccd94322016-08-17 13:49:19 +0100687 } else if (cmd == CPU_PM_ENTER && gic_dist_security_disabled()) {
Sudeep Holla3708d522014-08-26 16:03:35 +0100688 gic_write_grpen1(0);
689 gic_enable_redist(false);
690 }
691 return NOTIFY_OK;
692}
693
694static struct notifier_block gic_cpu_pm_notifier_block = {
695 .notifier_call = gic_cpu_pm_notifier,
696};
697
698static void gic_cpu_pm_init(void)
699{
700 cpu_pm_register_notifier(&gic_cpu_pm_notifier_block);
701}
702
703#else
704static inline void gic_cpu_pm_init(void) { }
705#endif /* CONFIG_CPU_PM */
706
Marc Zyngier021f6532014-06-30 16:01:31 +0100707static struct irq_chip gic_chip = {
708 .name = "GICv3",
709 .irq_mask = gic_mask_irq,
710 .irq_unmask = gic_unmask_irq,
711 .irq_eoi = gic_eoi_irq,
712 .irq_set_type = gic_set_type,
713 .irq_set_affinity = gic_set_affinity,
Marc Zyngierb594c6e2015-03-18 11:01:24 +0000714 .irq_get_irqchip_state = gic_irq_get_irqchip_state,
715 .irq_set_irqchip_state = gic_irq_set_irqchip_state,
Sudeep Holla55963c92015-06-05 11:59:57 +0100716 .flags = IRQCHIP_SET_TYPE_MASKED,
Marc Zyngier021f6532014-06-30 16:01:31 +0100717};
718
Marc Zyngier0b6a3da2015-08-26 17:00:42 +0100719static struct irq_chip gic_eoimode1_chip = {
720 .name = "GICv3",
721 .irq_mask = gic_eoimode1_mask_irq,
722 .irq_unmask = gic_unmask_irq,
723 .irq_eoi = gic_eoimode1_eoi_irq,
724 .irq_set_type = gic_set_type,
725 .irq_set_affinity = gic_set_affinity,
726 .irq_get_irqchip_state = gic_irq_get_irqchip_state,
727 .irq_set_irqchip_state = gic_irq_set_irqchip_state,
Marc Zyngier530bf352015-08-26 17:00:43 +0100728 .irq_set_vcpu_affinity = gic_irq_set_vcpu_affinity,
Marc Zyngier0b6a3da2015-08-26 17:00:42 +0100729 .flags = IRQCHIP_SET_TYPE_MASKED,
730};
731
Marc Zyngierda33f312014-11-24 14:35:18 +0000732#define GIC_ID_NR (1U << gic_data.rdists.id_bits)
733
Marc Zyngier021f6532014-06-30 16:01:31 +0100734static int gic_irq_domain_map(struct irq_domain *d, unsigned int irq,
735 irq_hw_number_t hw)
736{
Marc Zyngier0b6a3da2015-08-26 17:00:42 +0100737 struct irq_chip *chip = &gic_chip;
738
739 if (static_key_true(&supports_deactivate))
740 chip = &gic_eoimode1_chip;
741
Marc Zyngier021f6532014-06-30 16:01:31 +0100742 /* SGIs are private to the core kernel */
743 if (hw < 16)
744 return -EPERM;
Marc Zyngierda33f312014-11-24 14:35:18 +0000745 /* Nothing here */
746 if (hw >= gic_data.irq_nr && hw < 8192)
747 return -EPERM;
748 /* Off limits */
749 if (hw >= GIC_ID_NR)
750 return -EPERM;
751
Marc Zyngier021f6532014-06-30 16:01:31 +0100752 /* PPIs */
753 if (hw < 32) {
754 irq_set_percpu_devid(irq);
Marc Zyngier0b6a3da2015-08-26 17:00:42 +0100755 irq_domain_set_info(d, irq, hw, chip, d->host_data,
Marc Zyngier443acc42014-11-24 14:35:09 +0000756 handle_percpu_devid_irq, NULL, NULL);
Rob Herringd17cab42015-08-29 18:01:22 -0500757 irq_set_status_flags(irq, IRQ_NOAUTOEN);
Marc Zyngier021f6532014-06-30 16:01:31 +0100758 }
759 /* SPIs */
760 if (hw >= 32 && hw < gic_data.irq_nr) {
Marc Zyngier0b6a3da2015-08-26 17:00:42 +0100761 irq_domain_set_info(d, irq, hw, chip, d->host_data,
Marc Zyngier443acc42014-11-24 14:35:09 +0000762 handle_fasteoi_irq, NULL, NULL);
Rob Herringd17cab42015-08-29 18:01:22 -0500763 irq_set_probe(irq);
Marc Zyngier021f6532014-06-30 16:01:31 +0100764 }
Marc Zyngierda33f312014-11-24 14:35:18 +0000765 /* LPIs */
766 if (hw >= 8192 && hw < GIC_ID_NR) {
767 if (!gic_dist_supports_lpis())
768 return -EPERM;
Marc Zyngier0b6a3da2015-08-26 17:00:42 +0100769 irq_domain_set_info(d, irq, hw, chip, d->host_data,
Marc Zyngierda33f312014-11-24 14:35:18 +0000770 handle_fasteoi_irq, NULL, NULL);
Marc Zyngierda33f312014-11-24 14:35:18 +0000771 }
772
Marc Zyngier021f6532014-06-30 16:01:31 +0100773 return 0;
774}
775
Marc Zyngierf833f572015-10-13 12:51:33 +0100776static int gic_irq_domain_translate(struct irq_domain *d,
777 struct irq_fwspec *fwspec,
778 unsigned long *hwirq,
779 unsigned int *type)
Marc Zyngier021f6532014-06-30 16:01:31 +0100780{
Marc Zyngierf833f572015-10-13 12:51:33 +0100781 if (is_of_node(fwspec->fwnode)) {
782 if (fwspec->param_count < 3)
783 return -EINVAL;
Marc Zyngier021f6532014-06-30 16:01:31 +0100784
Marc Zyngierdb8c70e2015-10-14 12:27:16 +0100785 switch (fwspec->param[0]) {
786 case 0: /* SPI */
787 *hwirq = fwspec->param[1] + 32;
788 break;
789 case 1: /* PPI */
790 *hwirq = fwspec->param[1] + 16;
791 break;
792 case GIC_IRQ_TYPE_LPI: /* LPI */
793 *hwirq = fwspec->param[1];
794 break;
795 default:
796 return -EINVAL;
797 }
Marc Zyngierf833f572015-10-13 12:51:33 +0100798
799 *type = fwspec->param[2] & IRQ_TYPE_SENSE_MASK;
800 return 0;
Marc Zyngier021f6532014-06-30 16:01:31 +0100801 }
802
Tomasz Nowickiffa7d612016-01-19 14:11:15 +0100803 if (is_fwnode_irqchip(fwspec->fwnode)) {
804 if(fwspec->param_count != 2)
805 return -EINVAL;
806
807 *hwirq = fwspec->param[0];
808 *type = fwspec->param[1];
809 return 0;
810 }
811
Marc Zyngierf833f572015-10-13 12:51:33 +0100812 return -EINVAL;
Marc Zyngier021f6532014-06-30 16:01:31 +0100813}
814
Marc Zyngier443acc42014-11-24 14:35:09 +0000815static int gic_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
816 unsigned int nr_irqs, void *arg)
817{
818 int i, ret;
819 irq_hw_number_t hwirq;
820 unsigned int type = IRQ_TYPE_NONE;
Marc Zyngierf833f572015-10-13 12:51:33 +0100821 struct irq_fwspec *fwspec = arg;
Marc Zyngier443acc42014-11-24 14:35:09 +0000822
Marc Zyngierf833f572015-10-13 12:51:33 +0100823 ret = gic_irq_domain_translate(domain, fwspec, &hwirq, &type);
Marc Zyngier443acc42014-11-24 14:35:09 +0000824 if (ret)
825 return ret;
826
827 for (i = 0; i < nr_irqs; i++)
828 gic_irq_domain_map(domain, virq + i, hwirq + i);
829
830 return 0;
831}
832
833static void gic_irq_domain_free(struct irq_domain *domain, unsigned int virq,
834 unsigned int nr_irqs)
835{
836 int i;
837
838 for (i = 0; i < nr_irqs; i++) {
839 struct irq_data *d = irq_domain_get_irq_data(domain, virq + i);
840 irq_set_handler(virq + i, NULL);
841 irq_domain_reset_irq_data(d);
842 }
843}
844
Marc Zyngiere3825ba2016-04-11 09:57:54 +0100845static int gic_irq_domain_select(struct irq_domain *d,
846 struct irq_fwspec *fwspec,
847 enum irq_domain_bus_token bus_token)
848{
849 /* Not for us */
850 if (fwspec->fwnode != d->fwnode)
851 return 0;
852
853 /* If this is not DT, then we have a single domain */
854 if (!is_of_node(fwspec->fwnode))
855 return 1;
856
857 /*
858 * If this is a PPI and we have a 4th (non-null) parameter,
859 * then we need to match the partition domain.
860 */
861 if (fwspec->param_count >= 4 &&
862 fwspec->param[0] == 1 && fwspec->param[3] != 0)
863 return d == partition_get_domain(gic_data.ppi_descs[fwspec->param[1]]);
864
865 return d == gic_data.domain;
866}
867
Marc Zyngier021f6532014-06-30 16:01:31 +0100868static const struct irq_domain_ops gic_irq_domain_ops = {
Marc Zyngierf833f572015-10-13 12:51:33 +0100869 .translate = gic_irq_domain_translate,
Marc Zyngier443acc42014-11-24 14:35:09 +0000870 .alloc = gic_irq_domain_alloc,
871 .free = gic_irq_domain_free,
Marc Zyngiere3825ba2016-04-11 09:57:54 +0100872 .select = gic_irq_domain_select,
873};
874
875static int partition_domain_translate(struct irq_domain *d,
876 struct irq_fwspec *fwspec,
877 unsigned long *hwirq,
878 unsigned int *type)
879{
880 struct device_node *np;
881 int ret;
882
883 np = of_find_node_by_phandle(fwspec->param[3]);
884 if (WARN_ON(!np))
885 return -EINVAL;
886
887 ret = partition_translate_id(gic_data.ppi_descs[fwspec->param[1]],
888 of_node_to_fwnode(np));
889 if (ret < 0)
890 return ret;
891
892 *hwirq = ret;
893 *type = fwspec->param[2] & IRQ_TYPE_SENSE_MASK;
894
895 return 0;
896}
897
898static const struct irq_domain_ops partition_domain_ops = {
899 .translate = partition_domain_translate,
900 .select = gic_irq_domain_select,
Marc Zyngier021f6532014-06-30 16:01:31 +0100901};
902
Robert Richter6d4e11c2015-09-21 22:58:35 +0200903static void gicv3_enable_quirks(void)
904{
Jean-Philippe Brucker7936e912015-10-01 13:47:14 +0100905#ifdef CONFIG_ARM64
Robert Richter6d4e11c2015-09-21 22:58:35 +0200906 if (cpus_have_cap(ARM64_WORKAROUND_CAVIUM_23154))
Robert Richter8ac2a172015-09-21 22:58:39 +0200907 static_branch_enable(&is_cavium_thunderx);
Jean-Philippe Brucker7936e912015-10-01 13:47:14 +0100908#endif
Robert Richter6d4e11c2015-09-21 22:58:35 +0200909}
910
Tomasz Nowickidb57d742016-01-19 14:11:14 +0100911static int __init gic_init_bases(void __iomem *dist_base,
912 struct redist_region *rdist_regs,
913 u32 nr_redist_regions,
914 u64 redist_stride,
915 struct fwnode_handle *handle)
916{
917 struct device_node *node;
918 u32 typer;
919 int gic_irqs;
920 int err;
921
922 if (!is_hyp_mode_available())
923 static_key_slow_dec(&supports_deactivate);
924
925 if (static_key_true(&supports_deactivate))
926 pr_info("GIC: Using split EOI/Deactivate mode\n");
927
Marc Zyngiere3825ba2016-04-11 09:57:54 +0100928 gic_data.fwnode = handle;
Tomasz Nowickidb57d742016-01-19 14:11:14 +0100929 gic_data.dist_base = dist_base;
930 gic_data.redist_regions = rdist_regs;
931 gic_data.nr_redist_regions = nr_redist_regions;
932 gic_data.redist_stride = redist_stride;
933
934 gicv3_enable_quirks();
935
936 /*
937 * Find out how many interrupts are supported.
938 * The GIC only supports up to 1020 interrupt sources (SGI+PPI+SPI)
939 */
940 typer = readl_relaxed(gic_data.dist_base + GICD_TYPER);
941 gic_data.rdists.id_bits = GICD_TYPER_ID_BITS(typer);
942 gic_irqs = GICD_TYPER_IRQS(typer);
943 if (gic_irqs > 1020)
944 gic_irqs = 1020;
945 gic_data.irq_nr = gic_irqs;
946
947 gic_data.domain = irq_domain_create_tree(handle, &gic_irq_domain_ops,
948 &gic_data);
949 gic_data.rdists.rdist = alloc_percpu(typeof(*gic_data.rdists.rdist));
950
951 if (WARN_ON(!gic_data.domain) || WARN_ON(!gic_data.rdists.rdist)) {
952 err = -ENOMEM;
953 goto out_free;
954 }
955
956 set_handle_irq(gic_handle_irq);
957
958 node = to_of_node(handle);
959 if (IS_ENABLED(CONFIG_ARM_GIC_V3_ITS) && gic_dist_supports_lpis() &&
Channagoud Kadabifdb06642016-09-19 20:55:36 -0700960 !IS_ENABLED(CONFIG_ARM_GIC_V3_ACL) &&
Tomasz Nowickidb57d742016-01-19 14:11:14 +0100961 node) /* Temp hack to prevent ITS init for ACPI */
962 its_init(node, &gic_data.rdists, gic_data.domain);
963
964 gic_smp_init();
965 gic_dist_init();
966 gic_cpu_init();
967 gic_cpu_pm_init();
968
969 return 0;
970
971out_free:
972 if (gic_data.domain)
973 irq_domain_remove(gic_data.domain);
974 free_percpu(gic_data.rdists.rdist);
975 return err;
976}
977
978static int __init gic_validate_dist_version(void __iomem *dist_base)
979{
980 u32 reg = readl_relaxed(dist_base + GICD_PIDR2) & GIC_PIDR2_ARCH_MASK;
981
982 if (reg != GIC_PIDR2_ARCH_GICv3 && reg != GIC_PIDR2_ARCH_GICv4)
983 return -ENODEV;
984
985 return 0;
986}
987
Marc Zyngiere3825ba2016-04-11 09:57:54 +0100988static int get_cpu_number(struct device_node *dn)
989{
990 const __be32 *cell;
991 u64 hwid;
992 int i;
993
994 cell = of_get_property(dn, "reg", NULL);
995 if (!cell)
996 return -1;
997
998 hwid = of_read_number(cell, of_n_addr_cells(dn));
999
1000 /*
1001 * Non affinity bits must be set to 0 in the DT
1002 */
1003 if (hwid & ~MPIDR_HWID_BITMASK)
1004 return -1;
1005
1006 for (i = 0; i < num_possible_cpus(); i++)
1007 if (cpu_logical_map(i) == hwid)
1008 return i;
1009
1010 return -1;
1011}
1012
1013/* Create all possible partitions at boot time */
Linus Torvalds7beaa242016-05-19 11:27:09 -07001014static void __init gic_populate_ppi_partitions(struct device_node *gic_node)
Marc Zyngiere3825ba2016-04-11 09:57:54 +01001015{
1016 struct device_node *parts_node, *child_part;
1017 int part_idx = 0, i;
1018 int nr_parts;
1019 struct partition_affinity *parts;
1020
1021 parts_node = of_find_node_by_name(gic_node, "ppi-partitions");
1022 if (!parts_node)
1023 return;
1024
1025 nr_parts = of_get_child_count(parts_node);
1026
1027 if (!nr_parts)
1028 return;
1029
1030 parts = kzalloc(sizeof(*parts) * nr_parts, GFP_KERNEL);
1031 if (WARN_ON(!parts))
1032 return;
1033
1034 for_each_child_of_node(parts_node, child_part) {
1035 struct partition_affinity *part;
1036 int n;
1037
1038 part = &parts[part_idx];
1039
1040 part->partition_id = of_node_to_fwnode(child_part);
1041
1042 pr_info("GIC: PPI partition %s[%d] { ",
1043 child_part->name, part_idx);
1044
1045 n = of_property_count_elems_of_size(child_part, "affinity",
1046 sizeof(u32));
1047 WARN_ON(n <= 0);
1048
1049 for (i = 0; i < n; i++) {
1050 int err, cpu;
1051 u32 cpu_phandle;
1052 struct device_node *cpu_node;
1053
1054 err = of_property_read_u32_index(child_part, "affinity",
1055 i, &cpu_phandle);
1056 if (WARN_ON(err))
1057 continue;
1058
1059 cpu_node = of_find_node_by_phandle(cpu_phandle);
1060 if (WARN_ON(!cpu_node))
1061 continue;
1062
1063 cpu = get_cpu_number(cpu_node);
1064 if (WARN_ON(cpu == -1))
1065 continue;
1066
1067 pr_cont("%s[%d] ", cpu_node->full_name, cpu);
1068
1069 cpumask_set_cpu(cpu, &part->mask);
1070 }
1071
1072 pr_cont("}\n");
1073 part_idx++;
1074 }
1075
1076 for (i = 0; i < 16; i++) {
1077 unsigned int irq;
1078 struct partition_desc *desc;
1079 struct irq_fwspec ppi_fwspec = {
1080 .fwnode = gic_data.fwnode,
1081 .param_count = 3,
1082 .param = {
1083 [0] = 1,
1084 [1] = i,
1085 [2] = IRQ_TYPE_NONE,
1086 },
1087 };
1088
1089 irq = irq_create_fwspec_mapping(&ppi_fwspec);
1090 if (WARN_ON(!irq))
1091 continue;
1092 desc = partition_create_desc(gic_data.fwnode, parts, nr_parts,
1093 irq, &partition_domain_ops);
1094 if (WARN_ON(!desc))
1095 continue;
1096
1097 gic_data.ppi_descs[i] = desc;
1098 }
1099}
1100
Julien Grall1839e572016-04-11 16:32:57 +01001101static void __init gic_of_setup_kvm_info(struct device_node *node)
1102{
1103 int ret;
1104 struct resource r;
1105 u32 gicv_idx;
1106
1107 gic_v3_kvm_info.type = GIC_V3;
1108
1109 gic_v3_kvm_info.maint_irq = irq_of_parse_and_map(node, 0);
1110 if (!gic_v3_kvm_info.maint_irq)
1111 return;
1112
1113 if (of_property_read_u32(node, "#redistributor-regions",
1114 &gicv_idx))
1115 gicv_idx = 1;
1116
1117 gicv_idx += 3; /* Also skip GICD, GICC, GICH */
1118 ret = of_address_to_resource(node, gicv_idx, &r);
1119 if (!ret)
1120 gic_v3_kvm_info.vcpu = r;
1121
1122 gic_set_kvm_info(&gic_v3_kvm_info);
1123}
1124
Marc Zyngier021f6532014-06-30 16:01:31 +01001125static int __init gic_of_init(struct device_node *node, struct device_node *parent)
1126{
1127 void __iomem *dist_base;
Marc Zyngierf5c14342014-11-24 14:35:10 +00001128 struct redist_region *rdist_regs;
Marc Zyngier021f6532014-06-30 16:01:31 +01001129 u64 redist_stride;
Marc Zyngierf5c14342014-11-24 14:35:10 +00001130 u32 nr_redist_regions;
Tomasz Nowickidb57d742016-01-19 14:11:14 +01001131 int err, i;
Marc Zyngier021f6532014-06-30 16:01:31 +01001132
1133 dist_base = of_iomap(node, 0);
1134 if (!dist_base) {
1135 pr_err("%s: unable to map gic dist registers\n",
1136 node->full_name);
1137 return -ENXIO;
1138 }
1139
Tomasz Nowickidb57d742016-01-19 14:11:14 +01001140 err = gic_validate_dist_version(dist_base);
1141 if (err) {
Marc Zyngier021f6532014-06-30 16:01:31 +01001142 pr_err("%s: no distributor detected, giving up\n",
1143 node->full_name);
Marc Zyngier021f6532014-06-30 16:01:31 +01001144 goto out_unmap_dist;
1145 }
1146
Marc Zyngierf5c14342014-11-24 14:35:10 +00001147 if (of_property_read_u32(node, "#redistributor-regions", &nr_redist_regions))
1148 nr_redist_regions = 1;
Marc Zyngier021f6532014-06-30 16:01:31 +01001149
Marc Zyngierf5c14342014-11-24 14:35:10 +00001150 rdist_regs = kzalloc(sizeof(*rdist_regs) * nr_redist_regions, GFP_KERNEL);
1151 if (!rdist_regs) {
Marc Zyngier021f6532014-06-30 16:01:31 +01001152 err = -ENOMEM;
1153 goto out_unmap_dist;
1154 }
1155
Marc Zyngierf5c14342014-11-24 14:35:10 +00001156 for (i = 0; i < nr_redist_regions; i++) {
1157 struct resource res;
1158 int ret;
1159
1160 ret = of_address_to_resource(node, 1 + i, &res);
1161 rdist_regs[i].redist_base = of_iomap(node, 1 + i);
1162 if (ret || !rdist_regs[i].redist_base) {
Marc Zyngier021f6532014-06-30 16:01:31 +01001163 pr_err("%s: couldn't map region %d\n",
1164 node->full_name, i);
1165 err = -ENODEV;
1166 goto out_unmap_rdist;
1167 }
Marc Zyngierf5c14342014-11-24 14:35:10 +00001168 rdist_regs[i].phys_base = res.start;
Marc Zyngier021f6532014-06-30 16:01:31 +01001169 }
1170
1171 if (of_property_read_u64(node, "redistributor-stride", &redist_stride))
1172 redist_stride = 0;
1173
Tomasz Nowickidb57d742016-01-19 14:11:14 +01001174 err = gic_init_bases(dist_base, rdist_regs, nr_redist_regions,
1175 redist_stride, &node->fwnode);
Marc Zyngiere3825ba2016-04-11 09:57:54 +01001176 if (err)
1177 goto out_unmap_rdist;
1178
1179 gic_populate_ppi_partitions(node);
Linus Torvalds7beaa242016-05-19 11:27:09 -07001180 gic_of_setup_kvm_info(node);
Marc Zyngiere3825ba2016-04-11 09:57:54 +01001181 return 0;
Marc Zyngier0b6a3da2015-08-26 17:00:42 +01001182
Marc Zyngier021f6532014-06-30 16:01:31 +01001183out_unmap_rdist:
Marc Zyngierf5c14342014-11-24 14:35:10 +00001184 for (i = 0; i < nr_redist_regions; i++)
1185 if (rdist_regs[i].redist_base)
1186 iounmap(rdist_regs[i].redist_base);
1187 kfree(rdist_regs);
Marc Zyngier021f6532014-06-30 16:01:31 +01001188out_unmap_dist:
1189 iounmap(dist_base);
1190 return err;
1191}
1192
1193IRQCHIP_DECLARE(gic_v3, "arm,gic-v3", gic_of_init);
Tomasz Nowickiffa7d612016-01-19 14:11:15 +01001194
1195#ifdef CONFIG_ACPI
Julien Grall611f0392016-04-11 16:32:56 +01001196static struct
1197{
1198 void __iomem *dist_base;
1199 struct redist_region *redist_regs;
1200 u32 nr_redist_regions;
1201 bool single_redist;
Julien Grall1839e572016-04-11 16:32:57 +01001202 u32 maint_irq;
1203 int maint_irq_mode;
1204 phys_addr_t vcpu_base;
Julien Grall611f0392016-04-11 16:32:56 +01001205} acpi_data __initdata;
Tomasz Nowickib70fb7a2016-01-19 14:11:16 +01001206
1207static void __init
1208gic_acpi_register_redist(phys_addr_t phys_base, void __iomem *redist_base)
1209{
1210 static int count = 0;
1211
Julien Grall611f0392016-04-11 16:32:56 +01001212 acpi_data.redist_regs[count].phys_base = phys_base;
1213 acpi_data.redist_regs[count].redist_base = redist_base;
1214 acpi_data.redist_regs[count].single_redist = acpi_data.single_redist;
Tomasz Nowickib70fb7a2016-01-19 14:11:16 +01001215 count++;
1216}
Tomasz Nowickiffa7d612016-01-19 14:11:15 +01001217
1218static int __init
1219gic_acpi_parse_madt_redist(struct acpi_subtable_header *header,
1220 const unsigned long end)
1221{
1222 struct acpi_madt_generic_redistributor *redist =
1223 (struct acpi_madt_generic_redistributor *)header;
1224 void __iomem *redist_base;
Tomasz Nowickiffa7d612016-01-19 14:11:15 +01001225
1226 redist_base = ioremap(redist->base_address, redist->length);
1227 if (!redist_base) {
1228 pr_err("Couldn't map GICR region @%llx\n", redist->base_address);
1229 return -ENOMEM;
1230 }
1231
Tomasz Nowickib70fb7a2016-01-19 14:11:16 +01001232 gic_acpi_register_redist(redist->base_address, redist_base);
Tomasz Nowickiffa7d612016-01-19 14:11:15 +01001233 return 0;
1234}
1235
Tomasz Nowickib70fb7a2016-01-19 14:11:16 +01001236static int __init
1237gic_acpi_parse_madt_gicc(struct acpi_subtable_header *header,
1238 const unsigned long end)
1239{
1240 struct acpi_madt_generic_interrupt *gicc =
1241 (struct acpi_madt_generic_interrupt *)header;
Julien Grall611f0392016-04-11 16:32:56 +01001242 u32 reg = readl_relaxed(acpi_data.dist_base + GICD_PIDR2) & GIC_PIDR2_ARCH_MASK;
Tomasz Nowickib70fb7a2016-01-19 14:11:16 +01001243 u32 size = reg == GIC_PIDR2_ARCH_GICv4 ? SZ_64K * 4 : SZ_64K * 2;
1244 void __iomem *redist_base;
1245
1246 redist_base = ioremap(gicc->gicr_base_address, size);
1247 if (!redist_base)
1248 return -ENOMEM;
1249
1250 gic_acpi_register_redist(gicc->gicr_base_address, redist_base);
1251 return 0;
1252}
1253
1254static int __init gic_acpi_collect_gicr_base(void)
1255{
1256 acpi_tbl_entry_handler redist_parser;
1257 enum acpi_madt_type type;
1258
Julien Grall611f0392016-04-11 16:32:56 +01001259 if (acpi_data.single_redist) {
Tomasz Nowickib70fb7a2016-01-19 14:11:16 +01001260 type = ACPI_MADT_TYPE_GENERIC_INTERRUPT;
1261 redist_parser = gic_acpi_parse_madt_gicc;
1262 } else {
1263 type = ACPI_MADT_TYPE_GENERIC_REDISTRIBUTOR;
1264 redist_parser = gic_acpi_parse_madt_redist;
1265 }
1266
1267 /* Collect redistributor base addresses in GICR entries */
1268 if (acpi_table_parse_madt(type, redist_parser, 0) > 0)
1269 return 0;
1270
1271 pr_info("No valid GICR entries exist\n");
1272 return -ENODEV;
1273}
1274
Tomasz Nowickiffa7d612016-01-19 14:11:15 +01001275static int __init gic_acpi_match_gicr(struct acpi_subtable_header *header,
1276 const unsigned long end)
1277{
1278 /* Subtable presence means that redist exists, that's it */
1279 return 0;
1280}
1281
Tomasz Nowickib70fb7a2016-01-19 14:11:16 +01001282static int __init gic_acpi_match_gicc(struct acpi_subtable_header *header,
1283 const unsigned long end)
1284{
1285 struct acpi_madt_generic_interrupt *gicc =
1286 (struct acpi_madt_generic_interrupt *)header;
1287
1288 /*
1289 * If GICC is enabled and has valid gicr base address, then it means
1290 * GICR base is presented via GICC
1291 */
1292 if ((gicc->flags & ACPI_MADT_ENABLED) && gicc->gicr_base_address)
1293 return 0;
1294
1295 return -ENODEV;
1296}
1297
1298static int __init gic_acpi_count_gicr_regions(void)
1299{
1300 int count;
1301
1302 /*
1303 * Count how many redistributor regions we have. It is not allowed
1304 * to mix redistributor description, GICR and GICC subtables have to be
1305 * mutually exclusive.
1306 */
1307 count = acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_REDISTRIBUTOR,
1308 gic_acpi_match_gicr, 0);
1309 if (count > 0) {
Julien Grall611f0392016-04-11 16:32:56 +01001310 acpi_data.single_redist = false;
Tomasz Nowickib70fb7a2016-01-19 14:11:16 +01001311 return count;
1312 }
1313
1314 count = acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_INTERRUPT,
1315 gic_acpi_match_gicc, 0);
1316 if (count > 0)
Julien Grall611f0392016-04-11 16:32:56 +01001317 acpi_data.single_redist = true;
Tomasz Nowickib70fb7a2016-01-19 14:11:16 +01001318
1319 return count;
1320}
1321
Tomasz Nowickiffa7d612016-01-19 14:11:15 +01001322static bool __init acpi_validate_gic_table(struct acpi_subtable_header *header,
1323 struct acpi_probe_entry *ape)
1324{
1325 struct acpi_madt_generic_distributor *dist;
1326 int count;
1327
1328 dist = (struct acpi_madt_generic_distributor *)header;
1329 if (dist->version != ape->driver_data)
1330 return false;
1331
1332 /* We need to do that exercise anyway, the sooner the better */
Tomasz Nowickib70fb7a2016-01-19 14:11:16 +01001333 count = gic_acpi_count_gicr_regions();
Tomasz Nowickiffa7d612016-01-19 14:11:15 +01001334 if (count <= 0)
1335 return false;
1336
Julien Grall611f0392016-04-11 16:32:56 +01001337 acpi_data.nr_redist_regions = count;
Tomasz Nowickiffa7d612016-01-19 14:11:15 +01001338 return true;
1339}
1340
Julien Grall1839e572016-04-11 16:32:57 +01001341static int __init gic_acpi_parse_virt_madt_gicc(struct acpi_subtable_header *header,
1342 const unsigned long end)
1343{
1344 struct acpi_madt_generic_interrupt *gicc =
1345 (struct acpi_madt_generic_interrupt *)header;
1346 int maint_irq_mode;
1347 static int first_madt = true;
1348
1349 /* Skip unusable CPUs */
1350 if (!(gicc->flags & ACPI_MADT_ENABLED))
1351 return 0;
1352
1353 maint_irq_mode = (gicc->flags & ACPI_MADT_VGIC_IRQ_MODE) ?
1354 ACPI_EDGE_SENSITIVE : ACPI_LEVEL_SENSITIVE;
1355
1356 if (first_madt) {
1357 first_madt = false;
1358
1359 acpi_data.maint_irq = gicc->vgic_interrupt;
1360 acpi_data.maint_irq_mode = maint_irq_mode;
1361 acpi_data.vcpu_base = gicc->gicv_base_address;
1362
1363 return 0;
1364 }
1365
1366 /*
1367 * The maintenance interrupt and GICV should be the same for every CPU
1368 */
1369 if ((acpi_data.maint_irq != gicc->vgic_interrupt) ||
1370 (acpi_data.maint_irq_mode != maint_irq_mode) ||
1371 (acpi_data.vcpu_base != gicc->gicv_base_address))
1372 return -EINVAL;
1373
1374 return 0;
1375}
1376
1377static bool __init gic_acpi_collect_virt_info(void)
1378{
1379 int count;
1380
1381 count = acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_INTERRUPT,
1382 gic_acpi_parse_virt_madt_gicc, 0);
1383
1384 return (count > 0);
1385}
1386
Tomasz Nowickiffa7d612016-01-19 14:11:15 +01001387#define ACPI_GICV3_DIST_MEM_SIZE (SZ_64K)
Julien Grall1839e572016-04-11 16:32:57 +01001388#define ACPI_GICV2_VCTRL_MEM_SIZE (SZ_4K)
1389#define ACPI_GICV2_VCPU_MEM_SIZE (SZ_8K)
1390
1391static void __init gic_acpi_setup_kvm_info(void)
1392{
1393 int irq;
1394
1395 if (!gic_acpi_collect_virt_info()) {
1396 pr_warn("Unable to get hardware information used for virtualization\n");
1397 return;
1398 }
1399
1400 gic_v3_kvm_info.type = GIC_V3;
1401
1402 irq = acpi_register_gsi(NULL, acpi_data.maint_irq,
1403 acpi_data.maint_irq_mode,
1404 ACPI_ACTIVE_HIGH);
1405 if (irq <= 0)
1406 return;
1407
1408 gic_v3_kvm_info.maint_irq = irq;
1409
1410 if (acpi_data.vcpu_base) {
1411 struct resource *vcpu = &gic_v3_kvm_info.vcpu;
1412
1413 vcpu->flags = IORESOURCE_MEM;
1414 vcpu->start = acpi_data.vcpu_base;
1415 vcpu->end = vcpu->start + ACPI_GICV2_VCPU_MEM_SIZE - 1;
1416 }
1417
1418 gic_set_kvm_info(&gic_v3_kvm_info);
1419}
Tomasz Nowickiffa7d612016-01-19 14:11:15 +01001420
1421static int __init
1422gic_acpi_init(struct acpi_subtable_header *header, const unsigned long end)
1423{
1424 struct acpi_madt_generic_distributor *dist;
1425 struct fwnode_handle *domain_handle;
Julien Grall611f0392016-04-11 16:32:56 +01001426 size_t size;
Tomasz Nowickib70fb7a2016-01-19 14:11:16 +01001427 int i, err;
Tomasz Nowickiffa7d612016-01-19 14:11:15 +01001428
1429 /* Get distributor base address */
1430 dist = (struct acpi_madt_generic_distributor *)header;
Julien Grall611f0392016-04-11 16:32:56 +01001431 acpi_data.dist_base = ioremap(dist->base_address,
1432 ACPI_GICV3_DIST_MEM_SIZE);
1433 if (!acpi_data.dist_base) {
Tomasz Nowickiffa7d612016-01-19 14:11:15 +01001434 pr_err("Unable to map GICD registers\n");
1435 return -ENOMEM;
1436 }
1437
Julien Grall611f0392016-04-11 16:32:56 +01001438 err = gic_validate_dist_version(acpi_data.dist_base);
Tomasz Nowickiffa7d612016-01-19 14:11:15 +01001439 if (err) {
Julien Grall611f0392016-04-11 16:32:56 +01001440 pr_err("No distributor detected at @%p, giving up",
1441 acpi_data.dist_base);
Tomasz Nowickiffa7d612016-01-19 14:11:15 +01001442 goto out_dist_unmap;
1443 }
1444
Julien Grall611f0392016-04-11 16:32:56 +01001445 size = sizeof(*acpi_data.redist_regs) * acpi_data.nr_redist_regions;
1446 acpi_data.redist_regs = kzalloc(size, GFP_KERNEL);
1447 if (!acpi_data.redist_regs) {
Tomasz Nowickiffa7d612016-01-19 14:11:15 +01001448 err = -ENOMEM;
1449 goto out_dist_unmap;
1450 }
1451
Tomasz Nowickib70fb7a2016-01-19 14:11:16 +01001452 err = gic_acpi_collect_gicr_base();
1453 if (err)
Tomasz Nowickiffa7d612016-01-19 14:11:15 +01001454 goto out_redist_unmap;
Tomasz Nowickiffa7d612016-01-19 14:11:15 +01001455
Julien Grall611f0392016-04-11 16:32:56 +01001456 domain_handle = irq_domain_alloc_fwnode(acpi_data.dist_base);
Tomasz Nowickiffa7d612016-01-19 14:11:15 +01001457 if (!domain_handle) {
1458 err = -ENOMEM;
1459 goto out_redist_unmap;
1460 }
1461
Julien Grall611f0392016-04-11 16:32:56 +01001462 err = gic_init_bases(acpi_data.dist_base, acpi_data.redist_regs,
1463 acpi_data.nr_redist_regions, 0, domain_handle);
Tomasz Nowickiffa7d612016-01-19 14:11:15 +01001464 if (err)
1465 goto out_fwhandle_free;
1466
1467 acpi_set_irq_model(ACPI_IRQ_MODEL_GIC, domain_handle);
Julien Grall1839e572016-04-11 16:32:57 +01001468 gic_acpi_setup_kvm_info();
1469
Tomasz Nowickiffa7d612016-01-19 14:11:15 +01001470 return 0;
1471
1472out_fwhandle_free:
1473 irq_domain_free_fwnode(domain_handle);
1474out_redist_unmap:
Julien Grall611f0392016-04-11 16:32:56 +01001475 for (i = 0; i < acpi_data.nr_redist_regions; i++)
1476 if (acpi_data.redist_regs[i].redist_base)
1477 iounmap(acpi_data.redist_regs[i].redist_base);
1478 kfree(acpi_data.redist_regs);
Tomasz Nowickiffa7d612016-01-19 14:11:15 +01001479out_dist_unmap:
Julien Grall611f0392016-04-11 16:32:56 +01001480 iounmap(acpi_data.dist_base);
Tomasz Nowickiffa7d612016-01-19 14:11:15 +01001481 return err;
1482}
1483IRQCHIP_ACPI_DECLARE(gic_v3, ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR,
1484 acpi_validate_gic_table, ACPI_MADT_GIC_VERSION_V3,
1485 gic_acpi_init);
1486IRQCHIP_ACPI_DECLARE(gic_v4, ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR,
1487 acpi_validate_gic_table, ACPI_MADT_GIC_VERSION_V4,
1488 gic_acpi_init);
1489IRQCHIP_ACPI_DECLARE(gic_v3_or_v4, ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR,
1490 acpi_validate_gic_table, ACPI_MADT_GIC_VERSION_NONE,
1491 gic_acpi_init);
1492#endif