blob: 05a856073714aa2060922e13577c307337214962 [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>
31
Joel Porquet41a83e062015-07-07 17:11:46 -040032#include <linux/irqchip.h>
Julien Grall1839e572016-04-11 16:32:57 +010033#include <linux/irqchip/arm-gic-common.h>
Marc Zyngier021f6532014-06-30 16:01:31 +010034#include <linux/irqchip/arm-gic-v3.h>
35
36#include <asm/cputype.h>
37#include <asm/exception.h>
38#include <asm/smp_plat.h>
Marc Zyngier0b6a3da2015-08-26 17:00:42 +010039#include <asm/virt.h>
Marc Zyngier021f6532014-06-30 16:01:31 +010040
41#include "irq-gic-common.h"
Marc Zyngier021f6532014-06-30 16:01:31 +010042
Marc Zyngierf5c14342014-11-24 14:35:10 +000043struct redist_region {
44 void __iomem *redist_base;
45 phys_addr_t phys_base;
Tomasz Nowickib70fb7a2016-01-19 14:11:16 +010046 bool single_redist;
Marc Zyngierf5c14342014-11-24 14:35:10 +000047};
48
Marc Zyngier021f6532014-06-30 16:01:31 +010049struct gic_chip_data {
50 void __iomem *dist_base;
Marc Zyngierf5c14342014-11-24 14:35:10 +000051 struct redist_region *redist_regions;
52 struct rdists rdists;
Marc Zyngier021f6532014-06-30 16:01:31 +010053 struct irq_domain *domain;
54 u64 redist_stride;
Marc Zyngierf5c14342014-11-24 14:35:10 +000055 u32 nr_redist_regions;
Marc Zyngier021f6532014-06-30 16:01:31 +010056 unsigned int irq_nr;
57};
58
59static struct gic_chip_data gic_data __read_mostly;
Marc Zyngier0b6a3da2015-08-26 17:00:42 +010060static struct static_key supports_deactivate = STATIC_KEY_INIT_TRUE;
Marc Zyngier021f6532014-06-30 16:01:31 +010061
Julien Grall1839e572016-04-11 16:32:57 +010062static struct gic_kvm_info gic_v3_kvm_info;
63
Marc Zyngierf5c14342014-11-24 14:35:10 +000064#define gic_data_rdist() (this_cpu_ptr(gic_data.rdists.rdist))
65#define gic_data_rdist_rd_base() (gic_data_rdist()->rd_base)
Marc Zyngier021f6532014-06-30 16:01:31 +010066#define gic_data_rdist_sgi_base() (gic_data_rdist_rd_base() + SZ_64K)
67
68/* Our default, arbitrary priority value. Linux only uses one anyway. */
69#define DEFAULT_PMR_VALUE 0xf0
70
71static inline unsigned int gic_irq(struct irq_data *d)
72{
73 return d->hwirq;
74}
75
76static inline int gic_irq_in_rdist(struct irq_data *d)
77{
78 return gic_irq(d) < 32;
79}
80
81static inline void __iomem *gic_dist_base(struct irq_data *d)
82{
83 if (gic_irq_in_rdist(d)) /* SGI+PPI -> SGI_base for this CPU */
84 return gic_data_rdist_sgi_base();
85
86 if (d->hwirq <= 1023) /* SPI -> dist_base */
87 return gic_data.dist_base;
88
Marc Zyngier021f6532014-06-30 16:01:31 +010089 return NULL;
90}
91
92static void gic_do_wait_for_rwp(void __iomem *base)
93{
94 u32 count = 1000000; /* 1s! */
95
96 while (readl_relaxed(base + GICD_CTLR) & GICD_CTLR_RWP) {
97 count--;
98 if (!count) {
99 pr_err_ratelimited("RWP timeout, gone fishing\n");
100 return;
101 }
102 cpu_relax();
103 udelay(1);
104 };
105}
106
107/* Wait for completion of a distributor change */
108static void gic_dist_wait_for_rwp(void)
109{
110 gic_do_wait_for_rwp(gic_data.dist_base);
111}
112
113/* Wait for completion of a redistributor change */
114static void gic_redist_wait_for_rwp(void)
115{
116 gic_do_wait_for_rwp(gic_data_rdist_rd_base());
117}
118
Jean-Philippe Brucker7936e912015-10-01 13:47:14 +0100119#ifdef CONFIG_ARM64
Robert Richter8ac2a172015-09-21 22:58:39 +0200120static DEFINE_STATIC_KEY_FALSE(is_cavium_thunderx);
Robert Richter6d4e11c2015-09-21 22:58:35 +0200121
122static u64 __maybe_unused gic_read_iar(void)
123{
Robert Richter8ac2a172015-09-21 22:58:39 +0200124 if (static_branch_unlikely(&is_cavium_thunderx))
Robert Richter6d4e11c2015-09-21 22:58:35 +0200125 return gic_read_iar_cavium_thunderx();
126 else
127 return gic_read_iar_common();
128}
Jean-Philippe Brucker7936e912015-10-01 13:47:14 +0100129#endif
Marc Zyngier021f6532014-06-30 16:01:31 +0100130
Sudeep Hollaa2c22512014-08-26 16:03:34 +0100131static void gic_enable_redist(bool enable)
Marc Zyngier021f6532014-06-30 16:01:31 +0100132{
133 void __iomem *rbase;
134 u32 count = 1000000; /* 1s! */
135 u32 val;
136
137 rbase = gic_data_rdist_rd_base();
138
Marc Zyngier021f6532014-06-30 16:01:31 +0100139 val = readl_relaxed(rbase + GICR_WAKER);
Sudeep Hollaa2c22512014-08-26 16:03:34 +0100140 if (enable)
141 /* Wake up this CPU redistributor */
142 val &= ~GICR_WAKER_ProcessorSleep;
143 else
144 val |= GICR_WAKER_ProcessorSleep;
Marc Zyngier021f6532014-06-30 16:01:31 +0100145 writel_relaxed(val, rbase + GICR_WAKER);
146
Sudeep Hollaa2c22512014-08-26 16:03:34 +0100147 if (!enable) { /* Check that GICR_WAKER is writeable */
148 val = readl_relaxed(rbase + GICR_WAKER);
149 if (!(val & GICR_WAKER_ProcessorSleep))
150 return; /* No PM support in this redistributor */
151 }
152
153 while (count--) {
154 val = readl_relaxed(rbase + GICR_WAKER);
155 if (enable ^ (val & GICR_WAKER_ChildrenAsleep))
156 break;
Marc Zyngier021f6532014-06-30 16:01:31 +0100157 cpu_relax();
158 udelay(1);
159 };
Sudeep Hollaa2c22512014-08-26 16:03:34 +0100160 if (!count)
161 pr_err_ratelimited("redistributor failed to %s...\n",
162 enable ? "wakeup" : "sleep");
Marc Zyngier021f6532014-06-30 16:01:31 +0100163}
164
165/*
166 * Routines to disable, enable, EOI and route interrupts
167 */
Marc Zyngierb594c6e2015-03-18 11:01:24 +0000168static int gic_peek_irq(struct irq_data *d, u32 offset)
169{
170 u32 mask = 1 << (gic_irq(d) % 32);
171 void __iomem *base;
172
173 if (gic_irq_in_rdist(d))
174 base = gic_data_rdist_sgi_base();
175 else
176 base = gic_data.dist_base;
177
178 return !!(readl_relaxed(base + offset + (gic_irq(d) / 32) * 4) & mask);
179}
180
Marc Zyngier021f6532014-06-30 16:01:31 +0100181static void gic_poke_irq(struct irq_data *d, u32 offset)
182{
183 u32 mask = 1 << (gic_irq(d) % 32);
184 void (*rwp_wait)(void);
185 void __iomem *base;
186
187 if (gic_irq_in_rdist(d)) {
188 base = gic_data_rdist_sgi_base();
189 rwp_wait = gic_redist_wait_for_rwp;
190 } else {
191 base = gic_data.dist_base;
192 rwp_wait = gic_dist_wait_for_rwp;
193 }
194
195 writel_relaxed(mask, base + offset + (gic_irq(d) / 32) * 4);
196 rwp_wait();
197}
198
Marc Zyngier021f6532014-06-30 16:01:31 +0100199static void gic_mask_irq(struct irq_data *d)
200{
201 gic_poke_irq(d, GICD_ICENABLER);
202}
203
Marc Zyngier0b6a3da2015-08-26 17:00:42 +0100204static void gic_eoimode1_mask_irq(struct irq_data *d)
205{
206 gic_mask_irq(d);
Marc Zyngier530bf352015-08-26 17:00:43 +0100207 /*
208 * When masking a forwarded interrupt, make sure it is
209 * deactivated as well.
210 *
211 * This ensures that an interrupt that is getting
212 * disabled/masked will not get "stuck", because there is
213 * noone to deactivate it (guest is being terminated).
214 */
Thomas Gleixner4df7f542015-09-15 13:19:16 +0200215 if (irqd_is_forwarded_to_vcpu(d))
Marc Zyngier530bf352015-08-26 17:00:43 +0100216 gic_poke_irq(d, GICD_ICACTIVER);
Marc Zyngier0b6a3da2015-08-26 17:00:42 +0100217}
218
Marc Zyngier021f6532014-06-30 16:01:31 +0100219static void gic_unmask_irq(struct irq_data *d)
220{
221 gic_poke_irq(d, GICD_ISENABLER);
222}
223
Marc Zyngierb594c6e2015-03-18 11:01:24 +0000224static int gic_irq_set_irqchip_state(struct irq_data *d,
225 enum irqchip_irq_state which, bool val)
226{
227 u32 reg;
228
229 if (d->hwirq >= gic_data.irq_nr) /* PPI/SPI only */
230 return -EINVAL;
231
232 switch (which) {
233 case IRQCHIP_STATE_PENDING:
234 reg = val ? GICD_ISPENDR : GICD_ICPENDR;
235 break;
236
237 case IRQCHIP_STATE_ACTIVE:
238 reg = val ? GICD_ISACTIVER : GICD_ICACTIVER;
239 break;
240
241 case IRQCHIP_STATE_MASKED:
242 reg = val ? GICD_ICENABLER : GICD_ISENABLER;
243 break;
244
245 default:
246 return -EINVAL;
247 }
248
249 gic_poke_irq(d, reg);
250 return 0;
251}
252
253static int gic_irq_get_irqchip_state(struct irq_data *d,
254 enum irqchip_irq_state which, bool *val)
255{
256 if (d->hwirq >= gic_data.irq_nr) /* PPI/SPI only */
257 return -EINVAL;
258
259 switch (which) {
260 case IRQCHIP_STATE_PENDING:
261 *val = gic_peek_irq(d, GICD_ISPENDR);
262 break;
263
264 case IRQCHIP_STATE_ACTIVE:
265 *val = gic_peek_irq(d, GICD_ISACTIVER);
266 break;
267
268 case IRQCHIP_STATE_MASKED:
269 *val = !gic_peek_irq(d, GICD_ISENABLER);
270 break;
271
272 default:
273 return -EINVAL;
274 }
275
276 return 0;
277}
278
Marc Zyngier021f6532014-06-30 16:01:31 +0100279static void gic_eoi_irq(struct irq_data *d)
280{
281 gic_write_eoir(gic_irq(d));
282}
283
Marc Zyngier0b6a3da2015-08-26 17:00:42 +0100284static void gic_eoimode1_eoi_irq(struct irq_data *d)
285{
286 /*
Marc Zyngier530bf352015-08-26 17:00:43 +0100287 * No need to deactivate an LPI, or an interrupt that
288 * is is getting forwarded to a vcpu.
Marc Zyngier0b6a3da2015-08-26 17:00:42 +0100289 */
Thomas Gleixner4df7f542015-09-15 13:19:16 +0200290 if (gic_irq(d) >= 8192 || irqd_is_forwarded_to_vcpu(d))
Marc Zyngier0b6a3da2015-08-26 17:00:42 +0100291 return;
292 gic_write_dir(gic_irq(d));
293}
294
Marc Zyngier021f6532014-06-30 16:01:31 +0100295static int gic_set_type(struct irq_data *d, unsigned int type)
296{
297 unsigned int irq = gic_irq(d);
298 void (*rwp_wait)(void);
299 void __iomem *base;
300
301 /* Interrupt configuration for SGIs can't be changed */
302 if (irq < 16)
303 return -EINVAL;
304
Liviu Dudaufb7e7de2015-01-20 16:52:59 +0000305 /* SPIs have restrictions on the supported types */
306 if (irq >= 32 && type != IRQ_TYPE_LEVEL_HIGH &&
307 type != IRQ_TYPE_EDGE_RISING)
Marc Zyngier021f6532014-06-30 16:01:31 +0100308 return -EINVAL;
309
310 if (gic_irq_in_rdist(d)) {
311 base = gic_data_rdist_sgi_base();
312 rwp_wait = gic_redist_wait_for_rwp;
313 } else {
314 base = gic_data.dist_base;
315 rwp_wait = gic_dist_wait_for_rwp;
316 }
317
Liviu Dudaufb7e7de2015-01-20 16:52:59 +0000318 return gic_configure_irq(irq, type, base, rwp_wait);
Marc Zyngier021f6532014-06-30 16:01:31 +0100319}
320
Marc Zyngier530bf352015-08-26 17:00:43 +0100321static int gic_irq_set_vcpu_affinity(struct irq_data *d, void *vcpu)
322{
Thomas Gleixner4df7f542015-09-15 13:19:16 +0200323 if (vcpu)
324 irqd_set_forwarded_to_vcpu(d);
325 else
326 irqd_clr_forwarded_to_vcpu(d);
Marc Zyngier530bf352015-08-26 17:00:43 +0100327 return 0;
328}
329
Jean-Philippe Bruckerf6c86a42015-10-01 13:47:15 +0100330static u64 gic_mpidr_to_affinity(unsigned long mpidr)
Marc Zyngier021f6532014-06-30 16:01:31 +0100331{
332 u64 aff;
333
Jean-Philippe Bruckerf6c86a42015-10-01 13:47:15 +0100334 aff = ((u64)MPIDR_AFFINITY_LEVEL(mpidr, 3) << 32 |
Marc Zyngier021f6532014-06-30 16:01:31 +0100335 MPIDR_AFFINITY_LEVEL(mpidr, 2) << 16 |
336 MPIDR_AFFINITY_LEVEL(mpidr, 1) << 8 |
337 MPIDR_AFFINITY_LEVEL(mpidr, 0));
338
339 return aff;
340}
341
342static asmlinkage void __exception_irq_entry gic_handle_irq(struct pt_regs *regs)
343{
Jean-Philippe Bruckerf6c86a42015-10-01 13:47:15 +0100344 u32 irqnr;
Marc Zyngier021f6532014-06-30 16:01:31 +0100345
346 do {
347 irqnr = gic_read_iar();
348
Marc Zyngierda33f312014-11-24 14:35:18 +0000349 if (likely(irqnr > 15 && irqnr < 1020) || irqnr >= 8192) {
Marc Zyngierebc6de02014-08-26 11:03:33 +0100350 int err;
Marc Zyngier0b6a3da2015-08-26 17:00:42 +0100351
352 if (static_key_true(&supports_deactivate))
353 gic_write_eoir(irqnr);
354
Marc Zyngierebc6de02014-08-26 11:03:33 +0100355 err = handle_domain_irq(gic_data.domain, irqnr, regs);
356 if (err) {
Marc Zyngierda33f312014-11-24 14:35:18 +0000357 WARN_ONCE(true, "Unexpected interrupt received!\n");
Marc Zyngier0b6a3da2015-08-26 17:00:42 +0100358 if (static_key_true(&supports_deactivate)) {
359 if (irqnr < 8192)
360 gic_write_dir(irqnr);
361 } else {
362 gic_write_eoir(irqnr);
363 }
Marc Zyngier021f6532014-06-30 16:01:31 +0100364 }
Marc Zyngierebc6de02014-08-26 11:03:33 +0100365 continue;
Marc Zyngier021f6532014-06-30 16:01:31 +0100366 }
367 if (irqnr < 16) {
368 gic_write_eoir(irqnr);
Marc Zyngier0b6a3da2015-08-26 17:00:42 +0100369 if (static_key_true(&supports_deactivate))
370 gic_write_dir(irqnr);
Marc Zyngier021f6532014-06-30 16:01:31 +0100371#ifdef CONFIG_SMP
372 handle_IPI(irqnr, regs);
373#else
374 WARN_ONCE(true, "Unexpected SGI received!\n");
375#endif
376 continue;
377 }
378 } while (irqnr != ICC_IAR1_EL1_SPURIOUS);
379}
380
381static void __init gic_dist_init(void)
382{
383 unsigned int i;
384 u64 affinity;
385 void __iomem *base = gic_data.dist_base;
386
387 /* Disable the distributor */
388 writel_relaxed(0, base + GICD_CTLR);
389 gic_dist_wait_for_rwp();
390
391 gic_dist_config(base, gic_data.irq_nr, gic_dist_wait_for_rwp);
392
393 /* Enable distributor with ARE, Group1 */
394 writel_relaxed(GICD_CTLR_ARE_NS | GICD_CTLR_ENABLE_G1A | GICD_CTLR_ENABLE_G1,
395 base + GICD_CTLR);
396
397 /*
398 * Set all global interrupts to the boot CPU only. ARE must be
399 * enabled.
400 */
401 affinity = gic_mpidr_to_affinity(cpu_logical_map(smp_processor_id()));
402 for (i = 32; i < gic_data.irq_nr; i++)
Jean-Philippe Brucker72c97122015-10-01 13:47:16 +0100403 gic_write_irouter(affinity, base + GICD_IROUTER + i * 8);
Marc Zyngier021f6532014-06-30 16:01:31 +0100404}
405
406static int gic_populate_rdist(void)
407{
Jean-Philippe Bruckerf6c86a42015-10-01 13:47:15 +0100408 unsigned long mpidr = cpu_logical_map(smp_processor_id());
Marc Zyngier021f6532014-06-30 16:01:31 +0100409 u64 typer;
410 u32 aff;
411 int i;
412
413 /*
414 * Convert affinity to a 32bit value that can be matched to
415 * GICR_TYPER bits [63:32].
416 */
417 aff = (MPIDR_AFFINITY_LEVEL(mpidr, 3) << 24 |
418 MPIDR_AFFINITY_LEVEL(mpidr, 2) << 16 |
419 MPIDR_AFFINITY_LEVEL(mpidr, 1) << 8 |
420 MPIDR_AFFINITY_LEVEL(mpidr, 0));
421
Marc Zyngierf5c14342014-11-24 14:35:10 +0000422 for (i = 0; i < gic_data.nr_redist_regions; i++) {
423 void __iomem *ptr = gic_data.redist_regions[i].redist_base;
Marc Zyngier021f6532014-06-30 16:01:31 +0100424 u32 reg;
425
426 reg = readl_relaxed(ptr + GICR_PIDR2) & GIC_PIDR2_ARCH_MASK;
427 if (reg != GIC_PIDR2_ARCH_GICv3 &&
428 reg != GIC_PIDR2_ARCH_GICv4) { /* We're in trouble... */
429 pr_warn("No redistributor present @%p\n", ptr);
430 break;
431 }
432
433 do {
Jean-Philippe Brucker72c97122015-10-01 13:47:16 +0100434 typer = gic_read_typer(ptr + GICR_TYPER);
Marc Zyngier021f6532014-06-30 16:01:31 +0100435 if ((typer >> 32) == aff) {
Marc Zyngierf5c14342014-11-24 14:35:10 +0000436 u64 offset = ptr - gic_data.redist_regions[i].redist_base;
Marc Zyngier021f6532014-06-30 16:01:31 +0100437 gic_data_rdist_rd_base() = ptr;
Marc Zyngierf5c14342014-11-24 14:35:10 +0000438 gic_data_rdist()->phys_base = gic_data.redist_regions[i].phys_base + offset;
Jean-Philippe Bruckerf6c86a42015-10-01 13:47:15 +0100439 pr_info("CPU%d: found redistributor %lx region %d:%pa\n",
440 smp_processor_id(), mpidr, i,
441 &gic_data_rdist()->phys_base);
Marc Zyngier021f6532014-06-30 16:01:31 +0100442 return 0;
443 }
444
Tomasz Nowickib70fb7a2016-01-19 14:11:16 +0100445 if (gic_data.redist_regions[i].single_redist)
446 break;
447
Marc Zyngier021f6532014-06-30 16:01:31 +0100448 if (gic_data.redist_stride) {
449 ptr += gic_data.redist_stride;
450 } else {
451 ptr += SZ_64K * 2; /* Skip RD_base + SGI_base */
452 if (typer & GICR_TYPER_VLPIS)
453 ptr += SZ_64K * 2; /* Skip VLPI_base + reserved page */
454 }
455 } while (!(typer & GICR_TYPER_LAST));
456 }
457
458 /* We couldn't even deal with ourselves... */
Jean-Philippe Bruckerf6c86a42015-10-01 13:47:15 +0100459 WARN(true, "CPU%d: mpidr %lx has no re-distributor!\n",
460 smp_processor_id(), mpidr);
Marc Zyngier021f6532014-06-30 16:01:31 +0100461 return -ENODEV;
462}
463
Sudeep Holla3708d522014-08-26 16:03:35 +0100464static void gic_cpu_sys_reg_init(void)
Marc Zyngier021f6532014-06-30 16:01:31 +0100465{
Marc Zyngier7cabd002015-09-30 11:48:01 +0100466 /*
467 * Need to check that the SRE bit has actually been set. If
468 * not, it means that SRE is disabled at EL2. We're going to
469 * die painfully, and there is nothing we can do about it.
470 *
471 * Kindly inform the luser.
472 */
473 if (!gic_enable_sre())
474 pr_err("GIC: unable to set SRE (disabled at EL2), panic ahead\n");
Marc Zyngier021f6532014-06-30 16:01:31 +0100475
476 /* Set priority mask register */
477 gic_write_pmr(DEFAULT_PMR_VALUE);
478
Marc Zyngier0b6a3da2015-08-26 17:00:42 +0100479 if (static_key_true(&supports_deactivate)) {
480 /* EOI drops priority only (mode 1) */
481 gic_write_ctlr(ICC_CTLR_EL1_EOImode_drop);
482 } else {
483 /* EOI deactivates interrupt too (mode 0) */
484 gic_write_ctlr(ICC_CTLR_EL1_EOImode_drop_dir);
485 }
Marc Zyngier021f6532014-06-30 16:01:31 +0100486
487 /* ... and let's hit the road... */
488 gic_write_grpen1(1);
489}
490
Marc Zyngierda33f312014-11-24 14:35:18 +0000491static int gic_dist_supports_lpis(void)
492{
493 return !!(readl_relaxed(gic_data.dist_base + GICD_TYPER) & GICD_TYPER_LPIS);
494}
495
Marc Zyngier021f6532014-06-30 16:01:31 +0100496static void gic_cpu_init(void)
497{
498 void __iomem *rbase;
499
500 /* Register ourselves with the rest of the world */
501 if (gic_populate_rdist())
502 return;
503
Sudeep Hollaa2c22512014-08-26 16:03:34 +0100504 gic_enable_redist(true);
Marc Zyngier021f6532014-06-30 16:01:31 +0100505
506 rbase = gic_data_rdist_sgi_base();
507
508 gic_cpu_config(rbase, gic_redist_wait_for_rwp);
509
Marc Zyngierda33f312014-11-24 14:35:18 +0000510 /* Give LPIs a spin */
511 if (IS_ENABLED(CONFIG_ARM_GIC_V3_ITS) && gic_dist_supports_lpis())
512 its_cpu_init();
513
Sudeep Holla3708d522014-08-26 16:03:35 +0100514 /* initialise system registers */
515 gic_cpu_sys_reg_init();
Marc Zyngier021f6532014-06-30 16:01:31 +0100516}
517
518#ifdef CONFIG_SMP
Marc Zyngier021f6532014-06-30 16:01:31 +0100519static int gic_secondary_init(struct notifier_block *nfb,
520 unsigned long action, void *hcpu)
521{
522 if (action == CPU_STARTING || action == CPU_STARTING_FROZEN)
523 gic_cpu_init();
524 return NOTIFY_OK;
525}
526
527/*
528 * Notifier for enabling the GIC CPU interface. Set an arbitrarily high
529 * priority because the GIC needs to be up before the ARM generic timers.
530 */
531static struct notifier_block gic_cpu_notifier = {
532 .notifier_call = gic_secondary_init,
533 .priority = 100,
534};
535
536static u16 gic_compute_target_list(int *base_cpu, const struct cpumask *mask,
Jean-Philippe Bruckerf6c86a42015-10-01 13:47:15 +0100537 unsigned long cluster_id)
Marc Zyngier021f6532014-06-30 16:01:31 +0100538{
539 int cpu = *base_cpu;
Jean-Philippe Bruckerf6c86a42015-10-01 13:47:15 +0100540 unsigned long mpidr = cpu_logical_map(cpu);
Marc Zyngier021f6532014-06-30 16:01:31 +0100541 u16 tlist = 0;
542
543 while (cpu < nr_cpu_ids) {
544 /*
545 * If we ever get a cluster of more than 16 CPUs, just
546 * scream and skip that CPU.
547 */
548 if (WARN_ON((mpidr & 0xff) >= 16))
549 goto out;
550
551 tlist |= 1 << (mpidr & 0xf);
552
553 cpu = cpumask_next(cpu, mask);
Vladimir Murzin614be382015-03-06 16:37:45 +0000554 if (cpu >= nr_cpu_ids)
Marc Zyngier021f6532014-06-30 16:01:31 +0100555 goto out;
556
557 mpidr = cpu_logical_map(cpu);
558
559 if (cluster_id != (mpidr & ~0xffUL)) {
560 cpu--;
561 goto out;
562 }
563 }
564out:
565 *base_cpu = cpu;
566 return tlist;
567}
568
Andre Przywara7e580272014-11-12 13:46:06 +0000569#define MPIDR_TO_SGI_AFFINITY(cluster_id, level) \
570 (MPIDR_AFFINITY_LEVEL(cluster_id, level) \
571 << ICC_SGI1R_AFFINITY_## level ##_SHIFT)
572
Marc Zyngier021f6532014-06-30 16:01:31 +0100573static void gic_send_sgi(u64 cluster_id, u16 tlist, unsigned int irq)
574{
575 u64 val;
576
Andre Przywara7e580272014-11-12 13:46:06 +0000577 val = (MPIDR_TO_SGI_AFFINITY(cluster_id, 3) |
578 MPIDR_TO_SGI_AFFINITY(cluster_id, 2) |
579 irq << ICC_SGI1R_SGI_ID_SHIFT |
580 MPIDR_TO_SGI_AFFINITY(cluster_id, 1) |
581 tlist << ICC_SGI1R_TARGET_LIST_SHIFT);
Marc Zyngier021f6532014-06-30 16:01:31 +0100582
583 pr_debug("CPU%d: ICC_SGI1R_EL1 %llx\n", smp_processor_id(), val);
584 gic_write_sgi1r(val);
585}
586
587static void gic_raise_softirq(const struct cpumask *mask, unsigned int irq)
588{
589 int cpu;
590
591 if (WARN_ON(irq >= 16))
592 return;
593
594 /*
595 * Ensure that stores to Normal memory are visible to the
596 * other CPUs before issuing the IPI.
597 */
598 smp_wmb();
599
Rusty Russellf9b531f2015-03-05 10:49:16 +1030600 for_each_cpu(cpu, mask) {
Jean-Philippe Bruckerf6c86a42015-10-01 13:47:15 +0100601 unsigned long cluster_id = cpu_logical_map(cpu) & ~0xffUL;
Marc Zyngier021f6532014-06-30 16:01:31 +0100602 u16 tlist;
603
604 tlist = gic_compute_target_list(&cpu, mask, cluster_id);
605 gic_send_sgi(cluster_id, tlist, irq);
606 }
607
608 /* Force the above writes to ICC_SGI1R_EL1 to be executed */
609 isb();
610}
611
612static void gic_smp_init(void)
613{
614 set_smp_cross_call(gic_raise_softirq);
615 register_cpu_notifier(&gic_cpu_notifier);
616}
617
618static int gic_set_affinity(struct irq_data *d, const struct cpumask *mask_val,
619 bool force)
620{
621 unsigned int cpu = cpumask_any_and(mask_val, cpu_online_mask);
622 void __iomem *reg;
623 int enabled;
624 u64 val;
625
626 if (gic_irq_in_rdist(d))
627 return -EINVAL;
628
629 /* If interrupt was enabled, disable it first */
630 enabled = gic_peek_irq(d, GICD_ISENABLER);
631 if (enabled)
632 gic_mask_irq(d);
633
634 reg = gic_dist_base(d) + GICD_IROUTER + (gic_irq(d) * 8);
635 val = gic_mpidr_to_affinity(cpu_logical_map(cpu));
636
Jean-Philippe Brucker72c97122015-10-01 13:47:16 +0100637 gic_write_irouter(val, reg);
Marc Zyngier021f6532014-06-30 16:01:31 +0100638
639 /*
640 * If the interrupt was enabled, enabled it again. Otherwise,
641 * just wait for the distributor to have digested our changes.
642 */
643 if (enabled)
644 gic_unmask_irq(d);
645 else
646 gic_dist_wait_for_rwp();
647
Antoine Tenart0fc6fa22016-02-19 16:22:43 +0100648 return IRQ_SET_MASK_OK_DONE;
Marc Zyngier021f6532014-06-30 16:01:31 +0100649}
650#else
651#define gic_set_affinity NULL
652#define gic_smp_init() do { } while(0)
653#endif
654
Sudeep Holla3708d522014-08-26 16:03:35 +0100655#ifdef CONFIG_CPU_PM
656static int gic_cpu_pm_notifier(struct notifier_block *self,
657 unsigned long cmd, void *v)
658{
659 if (cmd == CPU_PM_EXIT) {
660 gic_enable_redist(true);
661 gic_cpu_sys_reg_init();
662 } else if (cmd == CPU_PM_ENTER) {
663 gic_write_grpen1(0);
664 gic_enable_redist(false);
665 }
666 return NOTIFY_OK;
667}
668
669static struct notifier_block gic_cpu_pm_notifier_block = {
670 .notifier_call = gic_cpu_pm_notifier,
671};
672
673static void gic_cpu_pm_init(void)
674{
675 cpu_pm_register_notifier(&gic_cpu_pm_notifier_block);
676}
677
678#else
679static inline void gic_cpu_pm_init(void) { }
680#endif /* CONFIG_CPU_PM */
681
Marc Zyngier021f6532014-06-30 16:01:31 +0100682static struct irq_chip gic_chip = {
683 .name = "GICv3",
684 .irq_mask = gic_mask_irq,
685 .irq_unmask = gic_unmask_irq,
686 .irq_eoi = gic_eoi_irq,
687 .irq_set_type = gic_set_type,
688 .irq_set_affinity = gic_set_affinity,
Marc Zyngierb594c6e2015-03-18 11:01:24 +0000689 .irq_get_irqchip_state = gic_irq_get_irqchip_state,
690 .irq_set_irqchip_state = gic_irq_set_irqchip_state,
Sudeep Holla55963c92015-06-05 11:59:57 +0100691 .flags = IRQCHIP_SET_TYPE_MASKED,
Marc Zyngier021f6532014-06-30 16:01:31 +0100692};
693
Marc Zyngier0b6a3da2015-08-26 17:00:42 +0100694static struct irq_chip gic_eoimode1_chip = {
695 .name = "GICv3",
696 .irq_mask = gic_eoimode1_mask_irq,
697 .irq_unmask = gic_unmask_irq,
698 .irq_eoi = gic_eoimode1_eoi_irq,
699 .irq_set_type = gic_set_type,
700 .irq_set_affinity = gic_set_affinity,
701 .irq_get_irqchip_state = gic_irq_get_irqchip_state,
702 .irq_set_irqchip_state = gic_irq_set_irqchip_state,
Marc Zyngier530bf352015-08-26 17:00:43 +0100703 .irq_set_vcpu_affinity = gic_irq_set_vcpu_affinity,
Marc Zyngier0b6a3da2015-08-26 17:00:42 +0100704 .flags = IRQCHIP_SET_TYPE_MASKED,
705};
706
Marc Zyngierda33f312014-11-24 14:35:18 +0000707#define GIC_ID_NR (1U << gic_data.rdists.id_bits)
708
Marc Zyngier021f6532014-06-30 16:01:31 +0100709static int gic_irq_domain_map(struct irq_domain *d, unsigned int irq,
710 irq_hw_number_t hw)
711{
Marc Zyngier0b6a3da2015-08-26 17:00:42 +0100712 struct irq_chip *chip = &gic_chip;
713
714 if (static_key_true(&supports_deactivate))
715 chip = &gic_eoimode1_chip;
716
Marc Zyngier021f6532014-06-30 16:01:31 +0100717 /* SGIs are private to the core kernel */
718 if (hw < 16)
719 return -EPERM;
Marc Zyngierda33f312014-11-24 14:35:18 +0000720 /* Nothing here */
721 if (hw >= gic_data.irq_nr && hw < 8192)
722 return -EPERM;
723 /* Off limits */
724 if (hw >= GIC_ID_NR)
725 return -EPERM;
726
Marc Zyngier021f6532014-06-30 16:01:31 +0100727 /* PPIs */
728 if (hw < 32) {
729 irq_set_percpu_devid(irq);
Marc Zyngier0b6a3da2015-08-26 17:00:42 +0100730 irq_domain_set_info(d, irq, hw, chip, d->host_data,
Marc Zyngier443acc42014-11-24 14:35:09 +0000731 handle_percpu_devid_irq, NULL, NULL);
Rob Herringd17cab42015-08-29 18:01:22 -0500732 irq_set_status_flags(irq, IRQ_NOAUTOEN);
Marc Zyngier021f6532014-06-30 16:01:31 +0100733 }
734 /* SPIs */
735 if (hw >= 32 && hw < gic_data.irq_nr) {
Marc Zyngier0b6a3da2015-08-26 17:00:42 +0100736 irq_domain_set_info(d, irq, hw, chip, d->host_data,
Marc Zyngier443acc42014-11-24 14:35:09 +0000737 handle_fasteoi_irq, NULL, NULL);
Rob Herringd17cab42015-08-29 18:01:22 -0500738 irq_set_probe(irq);
Marc Zyngier021f6532014-06-30 16:01:31 +0100739 }
Marc Zyngierda33f312014-11-24 14:35:18 +0000740 /* LPIs */
741 if (hw >= 8192 && hw < GIC_ID_NR) {
742 if (!gic_dist_supports_lpis())
743 return -EPERM;
Marc Zyngier0b6a3da2015-08-26 17:00:42 +0100744 irq_domain_set_info(d, irq, hw, chip, d->host_data,
Marc Zyngierda33f312014-11-24 14:35:18 +0000745 handle_fasteoi_irq, NULL, NULL);
Marc Zyngierda33f312014-11-24 14:35:18 +0000746 }
747
Marc Zyngier021f6532014-06-30 16:01:31 +0100748 return 0;
749}
750
Marc Zyngierf833f572015-10-13 12:51:33 +0100751static int gic_irq_domain_translate(struct irq_domain *d,
752 struct irq_fwspec *fwspec,
753 unsigned long *hwirq,
754 unsigned int *type)
Marc Zyngier021f6532014-06-30 16:01:31 +0100755{
Marc Zyngierf833f572015-10-13 12:51:33 +0100756 if (is_of_node(fwspec->fwnode)) {
757 if (fwspec->param_count < 3)
758 return -EINVAL;
Marc Zyngier021f6532014-06-30 16:01:31 +0100759
Marc Zyngierdb8c70e2015-10-14 12:27:16 +0100760 switch (fwspec->param[0]) {
761 case 0: /* SPI */
762 *hwirq = fwspec->param[1] + 32;
763 break;
764 case 1: /* PPI */
765 *hwirq = fwspec->param[1] + 16;
766 break;
767 case GIC_IRQ_TYPE_LPI: /* LPI */
768 *hwirq = fwspec->param[1];
769 break;
770 default:
771 return -EINVAL;
772 }
Marc Zyngierf833f572015-10-13 12:51:33 +0100773
774 *type = fwspec->param[2] & IRQ_TYPE_SENSE_MASK;
775 return 0;
Marc Zyngier021f6532014-06-30 16:01:31 +0100776 }
777
Tomasz Nowickiffa7d612016-01-19 14:11:15 +0100778 if (is_fwnode_irqchip(fwspec->fwnode)) {
779 if(fwspec->param_count != 2)
780 return -EINVAL;
781
782 *hwirq = fwspec->param[0];
783 *type = fwspec->param[1];
784 return 0;
785 }
786
Marc Zyngierf833f572015-10-13 12:51:33 +0100787 return -EINVAL;
Marc Zyngier021f6532014-06-30 16:01:31 +0100788}
789
Marc Zyngier443acc42014-11-24 14:35:09 +0000790static int gic_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
791 unsigned int nr_irqs, void *arg)
792{
793 int i, ret;
794 irq_hw_number_t hwirq;
795 unsigned int type = IRQ_TYPE_NONE;
Marc Zyngierf833f572015-10-13 12:51:33 +0100796 struct irq_fwspec *fwspec = arg;
Marc Zyngier443acc42014-11-24 14:35:09 +0000797
Marc Zyngierf833f572015-10-13 12:51:33 +0100798 ret = gic_irq_domain_translate(domain, fwspec, &hwirq, &type);
Marc Zyngier443acc42014-11-24 14:35:09 +0000799 if (ret)
800 return ret;
801
802 for (i = 0; i < nr_irqs; i++)
803 gic_irq_domain_map(domain, virq + i, hwirq + i);
804
805 return 0;
806}
807
808static void gic_irq_domain_free(struct irq_domain *domain, unsigned int virq,
809 unsigned int nr_irqs)
810{
811 int i;
812
813 for (i = 0; i < nr_irqs; i++) {
814 struct irq_data *d = irq_domain_get_irq_data(domain, virq + i);
815 irq_set_handler(virq + i, NULL);
816 irq_domain_reset_irq_data(d);
817 }
818}
819
Marc Zyngier021f6532014-06-30 16:01:31 +0100820static const struct irq_domain_ops gic_irq_domain_ops = {
Marc Zyngierf833f572015-10-13 12:51:33 +0100821 .translate = gic_irq_domain_translate,
Marc Zyngier443acc42014-11-24 14:35:09 +0000822 .alloc = gic_irq_domain_alloc,
823 .free = gic_irq_domain_free,
Marc Zyngier021f6532014-06-30 16:01:31 +0100824};
825
Robert Richter6d4e11c2015-09-21 22:58:35 +0200826static void gicv3_enable_quirks(void)
827{
Jean-Philippe Brucker7936e912015-10-01 13:47:14 +0100828#ifdef CONFIG_ARM64
Robert Richter6d4e11c2015-09-21 22:58:35 +0200829 if (cpus_have_cap(ARM64_WORKAROUND_CAVIUM_23154))
Robert Richter8ac2a172015-09-21 22:58:39 +0200830 static_branch_enable(&is_cavium_thunderx);
Jean-Philippe Brucker7936e912015-10-01 13:47:14 +0100831#endif
Robert Richter6d4e11c2015-09-21 22:58:35 +0200832}
833
Tomasz Nowickidb57d742016-01-19 14:11:14 +0100834static int __init gic_init_bases(void __iomem *dist_base,
835 struct redist_region *rdist_regs,
836 u32 nr_redist_regions,
837 u64 redist_stride,
838 struct fwnode_handle *handle)
839{
840 struct device_node *node;
841 u32 typer;
842 int gic_irqs;
843 int err;
844
845 if (!is_hyp_mode_available())
846 static_key_slow_dec(&supports_deactivate);
847
848 if (static_key_true(&supports_deactivate))
849 pr_info("GIC: Using split EOI/Deactivate mode\n");
850
851 gic_data.dist_base = dist_base;
852 gic_data.redist_regions = rdist_regs;
853 gic_data.nr_redist_regions = nr_redist_regions;
854 gic_data.redist_stride = redist_stride;
855
856 gicv3_enable_quirks();
857
858 /*
859 * Find out how many interrupts are supported.
860 * The GIC only supports up to 1020 interrupt sources (SGI+PPI+SPI)
861 */
862 typer = readl_relaxed(gic_data.dist_base + GICD_TYPER);
863 gic_data.rdists.id_bits = GICD_TYPER_ID_BITS(typer);
864 gic_irqs = GICD_TYPER_IRQS(typer);
865 if (gic_irqs > 1020)
866 gic_irqs = 1020;
867 gic_data.irq_nr = gic_irqs;
868
869 gic_data.domain = irq_domain_create_tree(handle, &gic_irq_domain_ops,
870 &gic_data);
871 gic_data.rdists.rdist = alloc_percpu(typeof(*gic_data.rdists.rdist));
872
873 if (WARN_ON(!gic_data.domain) || WARN_ON(!gic_data.rdists.rdist)) {
874 err = -ENOMEM;
875 goto out_free;
876 }
877
878 set_handle_irq(gic_handle_irq);
879
880 node = to_of_node(handle);
881 if (IS_ENABLED(CONFIG_ARM_GIC_V3_ITS) && gic_dist_supports_lpis() &&
882 node) /* Temp hack to prevent ITS init for ACPI */
883 its_init(node, &gic_data.rdists, gic_data.domain);
884
885 gic_smp_init();
886 gic_dist_init();
887 gic_cpu_init();
888 gic_cpu_pm_init();
889
890 return 0;
891
892out_free:
893 if (gic_data.domain)
894 irq_domain_remove(gic_data.domain);
895 free_percpu(gic_data.rdists.rdist);
896 return err;
897}
898
899static int __init gic_validate_dist_version(void __iomem *dist_base)
900{
901 u32 reg = readl_relaxed(dist_base + GICD_PIDR2) & GIC_PIDR2_ARCH_MASK;
902
903 if (reg != GIC_PIDR2_ARCH_GICv3 && reg != GIC_PIDR2_ARCH_GICv4)
904 return -ENODEV;
905
906 return 0;
907}
908
Julien Grall1839e572016-04-11 16:32:57 +0100909static void __init gic_of_setup_kvm_info(struct device_node *node)
910{
911 int ret;
912 struct resource r;
913 u32 gicv_idx;
914
915 gic_v3_kvm_info.type = GIC_V3;
916
917 gic_v3_kvm_info.maint_irq = irq_of_parse_and_map(node, 0);
918 if (!gic_v3_kvm_info.maint_irq)
919 return;
920
921 if (of_property_read_u32(node, "#redistributor-regions",
922 &gicv_idx))
923 gicv_idx = 1;
924
925 gicv_idx += 3; /* Also skip GICD, GICC, GICH */
926 ret = of_address_to_resource(node, gicv_idx, &r);
927 if (!ret)
928 gic_v3_kvm_info.vcpu = r;
929
930 gic_set_kvm_info(&gic_v3_kvm_info);
931}
932
Marc Zyngier021f6532014-06-30 16:01:31 +0100933static int __init gic_of_init(struct device_node *node, struct device_node *parent)
934{
935 void __iomem *dist_base;
Marc Zyngierf5c14342014-11-24 14:35:10 +0000936 struct redist_region *rdist_regs;
Marc Zyngier021f6532014-06-30 16:01:31 +0100937 u64 redist_stride;
Marc Zyngierf5c14342014-11-24 14:35:10 +0000938 u32 nr_redist_regions;
Tomasz Nowickidb57d742016-01-19 14:11:14 +0100939 int err, i;
Marc Zyngier021f6532014-06-30 16:01:31 +0100940
941 dist_base = of_iomap(node, 0);
942 if (!dist_base) {
943 pr_err("%s: unable to map gic dist registers\n",
944 node->full_name);
945 return -ENXIO;
946 }
947
Tomasz Nowickidb57d742016-01-19 14:11:14 +0100948 err = gic_validate_dist_version(dist_base);
949 if (err) {
Marc Zyngier021f6532014-06-30 16:01:31 +0100950 pr_err("%s: no distributor detected, giving up\n",
951 node->full_name);
Marc Zyngier021f6532014-06-30 16:01:31 +0100952 goto out_unmap_dist;
953 }
954
Marc Zyngierf5c14342014-11-24 14:35:10 +0000955 if (of_property_read_u32(node, "#redistributor-regions", &nr_redist_regions))
956 nr_redist_regions = 1;
Marc Zyngier021f6532014-06-30 16:01:31 +0100957
Marc Zyngierf5c14342014-11-24 14:35:10 +0000958 rdist_regs = kzalloc(sizeof(*rdist_regs) * nr_redist_regions, GFP_KERNEL);
959 if (!rdist_regs) {
Marc Zyngier021f6532014-06-30 16:01:31 +0100960 err = -ENOMEM;
961 goto out_unmap_dist;
962 }
963
Marc Zyngierf5c14342014-11-24 14:35:10 +0000964 for (i = 0; i < nr_redist_regions; i++) {
965 struct resource res;
966 int ret;
967
968 ret = of_address_to_resource(node, 1 + i, &res);
969 rdist_regs[i].redist_base = of_iomap(node, 1 + i);
970 if (ret || !rdist_regs[i].redist_base) {
Marc Zyngier021f6532014-06-30 16:01:31 +0100971 pr_err("%s: couldn't map region %d\n",
972 node->full_name, i);
973 err = -ENODEV;
974 goto out_unmap_rdist;
975 }
Marc Zyngierf5c14342014-11-24 14:35:10 +0000976 rdist_regs[i].phys_base = res.start;
Marc Zyngier021f6532014-06-30 16:01:31 +0100977 }
978
979 if (of_property_read_u64(node, "redistributor-stride", &redist_stride))
980 redist_stride = 0;
981
Tomasz Nowickidb57d742016-01-19 14:11:14 +0100982 err = gic_init_bases(dist_base, rdist_regs, nr_redist_regions,
983 redist_stride, &node->fwnode);
Julien Grall1839e572016-04-11 16:32:57 +0100984 if (!err) {
985 gic_of_setup_kvm_info(node);
Tomasz Nowickidb57d742016-01-19 14:11:14 +0100986 return 0;
Julien Grall1839e572016-04-11 16:32:57 +0100987 }
Marc Zyngier0b6a3da2015-08-26 17:00:42 +0100988
Marc Zyngier021f6532014-06-30 16:01:31 +0100989out_unmap_rdist:
Marc Zyngierf5c14342014-11-24 14:35:10 +0000990 for (i = 0; i < nr_redist_regions; i++)
991 if (rdist_regs[i].redist_base)
992 iounmap(rdist_regs[i].redist_base);
993 kfree(rdist_regs);
Marc Zyngier021f6532014-06-30 16:01:31 +0100994out_unmap_dist:
995 iounmap(dist_base);
996 return err;
997}
998
999IRQCHIP_DECLARE(gic_v3, "arm,gic-v3", gic_of_init);
Tomasz Nowickiffa7d612016-01-19 14:11:15 +01001000
1001#ifdef CONFIG_ACPI
Julien Grall611f0392016-04-11 16:32:56 +01001002static struct
1003{
1004 void __iomem *dist_base;
1005 struct redist_region *redist_regs;
1006 u32 nr_redist_regions;
1007 bool single_redist;
Julien Grall1839e572016-04-11 16:32:57 +01001008 u32 maint_irq;
1009 int maint_irq_mode;
1010 phys_addr_t vcpu_base;
Julien Grall611f0392016-04-11 16:32:56 +01001011} acpi_data __initdata;
Tomasz Nowickib70fb7a2016-01-19 14:11:16 +01001012
1013static void __init
1014gic_acpi_register_redist(phys_addr_t phys_base, void __iomem *redist_base)
1015{
1016 static int count = 0;
1017
Julien Grall611f0392016-04-11 16:32:56 +01001018 acpi_data.redist_regs[count].phys_base = phys_base;
1019 acpi_data.redist_regs[count].redist_base = redist_base;
1020 acpi_data.redist_regs[count].single_redist = acpi_data.single_redist;
Tomasz Nowickib70fb7a2016-01-19 14:11:16 +01001021 count++;
1022}
Tomasz Nowickiffa7d612016-01-19 14:11:15 +01001023
1024static int __init
1025gic_acpi_parse_madt_redist(struct acpi_subtable_header *header,
1026 const unsigned long end)
1027{
1028 struct acpi_madt_generic_redistributor *redist =
1029 (struct acpi_madt_generic_redistributor *)header;
1030 void __iomem *redist_base;
Tomasz Nowickiffa7d612016-01-19 14:11:15 +01001031
1032 redist_base = ioremap(redist->base_address, redist->length);
1033 if (!redist_base) {
1034 pr_err("Couldn't map GICR region @%llx\n", redist->base_address);
1035 return -ENOMEM;
1036 }
1037
Tomasz Nowickib70fb7a2016-01-19 14:11:16 +01001038 gic_acpi_register_redist(redist->base_address, redist_base);
Tomasz Nowickiffa7d612016-01-19 14:11:15 +01001039 return 0;
1040}
1041
Tomasz Nowickib70fb7a2016-01-19 14:11:16 +01001042static int __init
1043gic_acpi_parse_madt_gicc(struct acpi_subtable_header *header,
1044 const unsigned long end)
1045{
1046 struct acpi_madt_generic_interrupt *gicc =
1047 (struct acpi_madt_generic_interrupt *)header;
Julien Grall611f0392016-04-11 16:32:56 +01001048 u32 reg = readl_relaxed(acpi_data.dist_base + GICD_PIDR2) & GIC_PIDR2_ARCH_MASK;
Tomasz Nowickib70fb7a2016-01-19 14:11:16 +01001049 u32 size = reg == GIC_PIDR2_ARCH_GICv4 ? SZ_64K * 4 : SZ_64K * 2;
1050 void __iomem *redist_base;
1051
1052 redist_base = ioremap(gicc->gicr_base_address, size);
1053 if (!redist_base)
1054 return -ENOMEM;
1055
1056 gic_acpi_register_redist(gicc->gicr_base_address, redist_base);
1057 return 0;
1058}
1059
1060static int __init gic_acpi_collect_gicr_base(void)
1061{
1062 acpi_tbl_entry_handler redist_parser;
1063 enum acpi_madt_type type;
1064
Julien Grall611f0392016-04-11 16:32:56 +01001065 if (acpi_data.single_redist) {
Tomasz Nowickib70fb7a2016-01-19 14:11:16 +01001066 type = ACPI_MADT_TYPE_GENERIC_INTERRUPT;
1067 redist_parser = gic_acpi_parse_madt_gicc;
1068 } else {
1069 type = ACPI_MADT_TYPE_GENERIC_REDISTRIBUTOR;
1070 redist_parser = gic_acpi_parse_madt_redist;
1071 }
1072
1073 /* Collect redistributor base addresses in GICR entries */
1074 if (acpi_table_parse_madt(type, redist_parser, 0) > 0)
1075 return 0;
1076
1077 pr_info("No valid GICR entries exist\n");
1078 return -ENODEV;
1079}
1080
Tomasz Nowickiffa7d612016-01-19 14:11:15 +01001081static int __init gic_acpi_match_gicr(struct acpi_subtable_header *header,
1082 const unsigned long end)
1083{
1084 /* Subtable presence means that redist exists, that's it */
1085 return 0;
1086}
1087
Tomasz Nowickib70fb7a2016-01-19 14:11:16 +01001088static int __init gic_acpi_match_gicc(struct acpi_subtable_header *header,
1089 const unsigned long end)
1090{
1091 struct acpi_madt_generic_interrupt *gicc =
1092 (struct acpi_madt_generic_interrupt *)header;
1093
1094 /*
1095 * If GICC is enabled and has valid gicr base address, then it means
1096 * GICR base is presented via GICC
1097 */
1098 if ((gicc->flags & ACPI_MADT_ENABLED) && gicc->gicr_base_address)
1099 return 0;
1100
1101 return -ENODEV;
1102}
1103
1104static int __init gic_acpi_count_gicr_regions(void)
1105{
1106 int count;
1107
1108 /*
1109 * Count how many redistributor regions we have. It is not allowed
1110 * to mix redistributor description, GICR and GICC subtables have to be
1111 * mutually exclusive.
1112 */
1113 count = acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_REDISTRIBUTOR,
1114 gic_acpi_match_gicr, 0);
1115 if (count > 0) {
Julien Grall611f0392016-04-11 16:32:56 +01001116 acpi_data.single_redist = false;
Tomasz Nowickib70fb7a2016-01-19 14:11:16 +01001117 return count;
1118 }
1119
1120 count = acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_INTERRUPT,
1121 gic_acpi_match_gicc, 0);
1122 if (count > 0)
Julien Grall611f0392016-04-11 16:32:56 +01001123 acpi_data.single_redist = true;
Tomasz Nowickib70fb7a2016-01-19 14:11:16 +01001124
1125 return count;
1126}
1127
Tomasz Nowickiffa7d612016-01-19 14:11:15 +01001128static bool __init acpi_validate_gic_table(struct acpi_subtable_header *header,
1129 struct acpi_probe_entry *ape)
1130{
1131 struct acpi_madt_generic_distributor *dist;
1132 int count;
1133
1134 dist = (struct acpi_madt_generic_distributor *)header;
1135 if (dist->version != ape->driver_data)
1136 return false;
1137
1138 /* We need to do that exercise anyway, the sooner the better */
Tomasz Nowickib70fb7a2016-01-19 14:11:16 +01001139 count = gic_acpi_count_gicr_regions();
Tomasz Nowickiffa7d612016-01-19 14:11:15 +01001140 if (count <= 0)
1141 return false;
1142
Julien Grall611f0392016-04-11 16:32:56 +01001143 acpi_data.nr_redist_regions = count;
Tomasz Nowickiffa7d612016-01-19 14:11:15 +01001144 return true;
1145}
1146
Julien Grall1839e572016-04-11 16:32:57 +01001147static int __init gic_acpi_parse_virt_madt_gicc(struct acpi_subtable_header *header,
1148 const unsigned long end)
1149{
1150 struct acpi_madt_generic_interrupt *gicc =
1151 (struct acpi_madt_generic_interrupt *)header;
1152 int maint_irq_mode;
1153 static int first_madt = true;
1154
1155 /* Skip unusable CPUs */
1156 if (!(gicc->flags & ACPI_MADT_ENABLED))
1157 return 0;
1158
1159 maint_irq_mode = (gicc->flags & ACPI_MADT_VGIC_IRQ_MODE) ?
1160 ACPI_EDGE_SENSITIVE : ACPI_LEVEL_SENSITIVE;
1161
1162 if (first_madt) {
1163 first_madt = false;
1164
1165 acpi_data.maint_irq = gicc->vgic_interrupt;
1166 acpi_data.maint_irq_mode = maint_irq_mode;
1167 acpi_data.vcpu_base = gicc->gicv_base_address;
1168
1169 return 0;
1170 }
1171
1172 /*
1173 * The maintenance interrupt and GICV should be the same for every CPU
1174 */
1175 if ((acpi_data.maint_irq != gicc->vgic_interrupt) ||
1176 (acpi_data.maint_irq_mode != maint_irq_mode) ||
1177 (acpi_data.vcpu_base != gicc->gicv_base_address))
1178 return -EINVAL;
1179
1180 return 0;
1181}
1182
1183static bool __init gic_acpi_collect_virt_info(void)
1184{
1185 int count;
1186
1187 count = acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_INTERRUPT,
1188 gic_acpi_parse_virt_madt_gicc, 0);
1189
1190 return (count > 0);
1191}
1192
Tomasz Nowickiffa7d612016-01-19 14:11:15 +01001193#define ACPI_GICV3_DIST_MEM_SIZE (SZ_64K)
Julien Grall1839e572016-04-11 16:32:57 +01001194#define ACPI_GICV2_VCTRL_MEM_SIZE (SZ_4K)
1195#define ACPI_GICV2_VCPU_MEM_SIZE (SZ_8K)
1196
1197static void __init gic_acpi_setup_kvm_info(void)
1198{
1199 int irq;
1200
1201 if (!gic_acpi_collect_virt_info()) {
1202 pr_warn("Unable to get hardware information used for virtualization\n");
1203 return;
1204 }
1205
1206 gic_v3_kvm_info.type = GIC_V3;
1207
1208 irq = acpi_register_gsi(NULL, acpi_data.maint_irq,
1209 acpi_data.maint_irq_mode,
1210 ACPI_ACTIVE_HIGH);
1211 if (irq <= 0)
1212 return;
1213
1214 gic_v3_kvm_info.maint_irq = irq;
1215
1216 if (acpi_data.vcpu_base) {
1217 struct resource *vcpu = &gic_v3_kvm_info.vcpu;
1218
1219 vcpu->flags = IORESOURCE_MEM;
1220 vcpu->start = acpi_data.vcpu_base;
1221 vcpu->end = vcpu->start + ACPI_GICV2_VCPU_MEM_SIZE - 1;
1222 }
1223
1224 gic_set_kvm_info(&gic_v3_kvm_info);
1225}
Tomasz Nowickiffa7d612016-01-19 14:11:15 +01001226
1227static int __init
1228gic_acpi_init(struct acpi_subtable_header *header, const unsigned long end)
1229{
1230 struct acpi_madt_generic_distributor *dist;
1231 struct fwnode_handle *domain_handle;
Julien Grall611f0392016-04-11 16:32:56 +01001232 size_t size;
Tomasz Nowickib70fb7a2016-01-19 14:11:16 +01001233 int i, err;
Tomasz Nowickiffa7d612016-01-19 14:11:15 +01001234
1235 /* Get distributor base address */
1236 dist = (struct acpi_madt_generic_distributor *)header;
Julien Grall611f0392016-04-11 16:32:56 +01001237 acpi_data.dist_base = ioremap(dist->base_address,
1238 ACPI_GICV3_DIST_MEM_SIZE);
1239 if (!acpi_data.dist_base) {
Tomasz Nowickiffa7d612016-01-19 14:11:15 +01001240 pr_err("Unable to map GICD registers\n");
1241 return -ENOMEM;
1242 }
1243
Julien Grall611f0392016-04-11 16:32:56 +01001244 err = gic_validate_dist_version(acpi_data.dist_base);
Tomasz Nowickiffa7d612016-01-19 14:11:15 +01001245 if (err) {
Julien Grall611f0392016-04-11 16:32:56 +01001246 pr_err("No distributor detected at @%p, giving up",
1247 acpi_data.dist_base);
Tomasz Nowickiffa7d612016-01-19 14:11:15 +01001248 goto out_dist_unmap;
1249 }
1250
Julien Grall611f0392016-04-11 16:32:56 +01001251 size = sizeof(*acpi_data.redist_regs) * acpi_data.nr_redist_regions;
1252 acpi_data.redist_regs = kzalloc(size, GFP_KERNEL);
1253 if (!acpi_data.redist_regs) {
Tomasz Nowickiffa7d612016-01-19 14:11:15 +01001254 err = -ENOMEM;
1255 goto out_dist_unmap;
1256 }
1257
Tomasz Nowickib70fb7a2016-01-19 14:11:16 +01001258 err = gic_acpi_collect_gicr_base();
1259 if (err)
Tomasz Nowickiffa7d612016-01-19 14:11:15 +01001260 goto out_redist_unmap;
Tomasz Nowickiffa7d612016-01-19 14:11:15 +01001261
Julien Grall611f0392016-04-11 16:32:56 +01001262 domain_handle = irq_domain_alloc_fwnode(acpi_data.dist_base);
Tomasz Nowickiffa7d612016-01-19 14:11:15 +01001263 if (!domain_handle) {
1264 err = -ENOMEM;
1265 goto out_redist_unmap;
1266 }
1267
Julien Grall611f0392016-04-11 16:32:56 +01001268 err = gic_init_bases(acpi_data.dist_base, acpi_data.redist_regs,
1269 acpi_data.nr_redist_regions, 0, domain_handle);
Tomasz Nowickiffa7d612016-01-19 14:11:15 +01001270 if (err)
1271 goto out_fwhandle_free;
1272
1273 acpi_set_irq_model(ACPI_IRQ_MODEL_GIC, domain_handle);
Julien Grall1839e572016-04-11 16:32:57 +01001274 gic_acpi_setup_kvm_info();
1275
Tomasz Nowickiffa7d612016-01-19 14:11:15 +01001276 return 0;
1277
1278out_fwhandle_free:
1279 irq_domain_free_fwnode(domain_handle);
1280out_redist_unmap:
Julien Grall611f0392016-04-11 16:32:56 +01001281 for (i = 0; i < acpi_data.nr_redist_regions; i++)
1282 if (acpi_data.redist_regs[i].redist_base)
1283 iounmap(acpi_data.redist_regs[i].redist_base);
1284 kfree(acpi_data.redist_regs);
Tomasz Nowickiffa7d612016-01-19 14:11:15 +01001285out_dist_unmap:
Julien Grall611f0392016-04-11 16:32:56 +01001286 iounmap(acpi_data.dist_base);
Tomasz Nowickiffa7d612016-01-19 14:11:15 +01001287 return err;
1288}
1289IRQCHIP_ACPI_DECLARE(gic_v3, ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR,
1290 acpi_validate_gic_table, ACPI_MADT_GIC_VERSION_V3,
1291 gic_acpi_init);
1292IRQCHIP_ACPI_DECLARE(gic_v4, ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR,
1293 acpi_validate_gic_table, ACPI_MADT_GIC_VERSION_V4,
1294 gic_acpi_init);
1295IRQCHIP_ACPI_DECLARE(gic_v3_or_v4, ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR,
1296 acpi_validate_gic_table, ACPI_MADT_GIC_VERSION_NONE,
1297 gic_acpi_init);
1298#endif