2 * Jailhouse, a Linux-based partitioning hypervisor
4 * Copyright (c) ARM Limited, 2014
7 * Jean-Philippe Brucker <jean-philippe.brucker@arm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
13 #include <jailhouse/control.h>
14 #include <jailhouse/mmio.h>
15 #include <jailhouse/printk.h>
16 #include <jailhouse/processor.h>
17 #include <jailhouse/types.h>
18 #include <asm/control.h>
19 #include <asm/gic_common.h>
20 #include <asm/irqchip.h>
21 #include <asm/platform.h>
22 #include <asm/setup.h>
23 #include <asm/traps.h>
26 * This implementation assumes that the kernel driver already initialised most
28 * There is almost no instruction barrier, since IRQs are always disabled in the
29 * hyp, and ERET serves as the context synchronization event.
32 static unsigned int gic_num_lr;
33 static unsigned int gic_num_priority_bits;
34 static u32 gic_version;
36 extern void *gicd_base;
37 extern unsigned int gicd_size;
38 static void *gicr_base;
39 static unsigned int gicr_size;
41 static int gic_init(void)
45 /* FIXME: parse a dt */
46 gicr_base = GICR_BASE;
47 gicr_size = GICR_SIZE;
49 /* Let the per-cpu code access the redistributors */
50 err = arch_map_device(gicr_base, gicr_base, gicr_size);
55 static int gic_cpu_reset(struct per_cpu *cpu_data)
58 void *gicr = cpu_data->gicr_base;
64 /* Clear list registers */
65 for (i = 0; i < gic_num_lr; i++)
68 gicr += GICR_SGI_BASE;
69 active = mmio_read32(gicr + GICR_ICACTIVER);
70 /* Deactivate all active PPIs */
71 for (i = 16; i < 32; i++) {
72 if (test_bit(i, &active))
73 arm_write_sysreg(ICC_DIR_EL1, i);
76 /* Disable all PPIs, ensure IPIs are enabled */
77 mmio_write32(gicr + GICR_ICENABLER, 0xffff0000);
78 mmio_write32(gicr + GICR_ISENABLER, 0x0000ffff);
80 /* Clear active priority bits */
81 if (gic_num_priority_bits >= 5)
82 arm_write_sysreg(ICH_AP1R0_EL2, 0);
83 if (gic_num_priority_bits >= 6)
84 arm_write_sysreg(ICH_AP1R1_EL2, 0);
85 if (gic_num_priority_bits > 6) {
86 arm_write_sysreg(ICH_AP1R2_EL2, 0);
87 arm_write_sysreg(ICH_AP1R3_EL2, 0);
90 arm_write_sysreg(ICH_VMCR_EL2, 0);
91 arm_write_sysreg(ICH_HCR_EL2, ICH_HCR_EN);
96 static int gic_cpu_init(struct per_cpu *cpu_data)
100 u32 cell_icc_ctlr, cell_icc_pmr, cell_icc_igrpen1;
103 void *redist_base = gicr_base;
105 /* Find redistributor */
107 pidr = mmio_read32(redist_base + GICR_PIDR2);
108 gic_version = GICR_PIDR2_ARCH(pidr);
109 if (gic_version != 3 && gic_version != 4)
112 typer = mmio_read64(redist_base + GICR_TYPER);
113 if ((typer >> 32) == cpu_data->cpu_id) {
114 cpu_data->gicr_base = redist_base;
118 redist_base += 0x20000;
119 if (gic_version == 4)
120 redist_base += 0x20000;
121 } while (!(typer & GICR_TYPER_Last));
123 if (cpu_data->gicr_base == 0) {
124 printk("GIC: No redist found for CPU%d\n", cpu_data->cpu_id);
128 /* Ensure all IPIs are enabled */
129 mmio_write32(redist_base + GICR_SGI_BASE + GICR_ISENABLER, 0x0000ffff);
133 * This allow to drop the priority of level-triggered interrupts without
134 * deactivating them, and thus ensure that they won't be immediately
135 * re-triggered. (e.g. timer)
136 * They can then be injected into the guest using the LR.HW bit, and
137 * will be deactivated once the guest does an EOI after handling the
140 arm_read_sysreg(ICC_CTLR_EL1, cell_icc_ctlr);
141 arm_write_sysreg(ICC_CTLR_EL1, ICC_CTLR_EOImode);
143 arm_read_sysreg(ICC_PMR_EL1, cell_icc_pmr);
144 arm_write_sysreg(ICC_PMR_EL1, ICC_PMR_DEFAULT);
146 arm_read_sysreg(ICC_IGRPEN1_EL1, cell_icc_igrpen1);
147 arm_write_sysreg(ICC_IGRPEN1_EL1, ICC_IGRPEN1_EN);
149 arm_read_sysreg(ICH_VTR_EL2, ich_vtr);
150 gic_num_lr = (ich_vtr & 0xf) + 1;
151 gic_num_priority_bits = (ich_vtr >> 29) + 1;
153 ich_vmcr = (cell_icc_pmr & ICC_PMR_MASK) << ICH_VMCR_VPMR_SHIFT;
154 if (cell_icc_igrpen1 & ICC_IGRPEN1_EN)
155 ich_vmcr |= ICH_VMCR_VENG1;
156 if (cell_icc_ctlr & ICC_CTLR_EOImode)
157 ich_vmcr |= ICH_VMCR_VEOIM;
158 arm_write_sysreg(ICH_VMCR_EL2, ich_vmcr);
160 /* After this, the cells access the virtual interface of the GIC. */
161 arm_write_sysreg(ICH_HCR_EL2, ICH_HCR_EN);
166 static void gic_route_spis(struct cell *config_cell, struct cell *dest_cell)
169 u64 spis = config_cell->arch.spis;
170 void *irouter = gicd_base + GICD_IROUTER;
171 unsigned int first_cpu;
173 /* Use the core functions to retrieve the first physical id */
174 for_each_cpu(first_cpu, dest_cell->cpu_set)
177 for (i = 0; i < 64; i++, irouter += 8) {
178 if (test_bit(i, (unsigned long *)&spis))
179 mmio_write64(irouter, first_cpu);
183 static void gic_cell_init(struct cell *cell)
185 gic_route_spis(cell, cell);
188 static void gic_cell_exit(struct cell *cell)
190 /* Reset interrupt routing of the cell's spis*/
191 gic_route_spis(cell, &root_cell);
194 static int gic_send_sgi(struct sgi *sgi)
197 u16 targets = sgi->targets;
199 if (!is_sgi(sgi->id))
202 if (sgi->routing_mode == 2)
203 targets = 1 << phys_processor_id();
205 val = (u64)sgi->aff3 << ICC_SGIR_AFF3_SHIFT
206 | (u64)sgi->aff2 << ICC_SGIR_AFF2_SHIFT
207 | sgi->aff1 << ICC_SGIR_AFF1_SHIFT
208 | (targets & ICC_SGIR_TARGET_MASK)
209 | (sgi->id & 0xf) << ICC_SGIR_IRQN_SHIFT;
211 if (sgi->routing_mode == 1)
212 val |= ICC_SGIR_ROUTING_BIT;
215 * Ensure the targets see our modifications to their per-cpu
220 arm_write_sysreg(ICC_SGI1R_EL1, val);
226 int gicv3_handle_sgir_write(struct per_cpu *cpu_data, u64 sgir)
229 struct cell *cell = cpu_data->cell;
230 unsigned int cpu, virt_id;
231 unsigned long this_cpu = cpu_data->cpu_id;
232 unsigned long routing_mode = !!(sgir & ICC_SGIR_ROUTING_BIT);
233 unsigned long targets = sgir & ICC_SGIR_TARGET_MASK;
234 u32 irq = sgir >> ICC_SGIR_IRQN_SHIFT & 0xf;
236 /* FIXME: clusters are not supported yet. */
238 sgi.routing_mode = routing_mode;
239 sgi.aff1 = sgir >> ICC_SGIR_AFF1_SHIFT & 0xff;
240 sgi.aff2 = sgir >> ICC_SGIR_AFF2_SHIFT & 0xff;
241 sgi.aff3 = sgir >> ICC_SGIR_AFF3_SHIFT & 0xff;
244 for_each_cpu_except(cpu, cell->cpu_set, this_cpu) {
245 virt_id = arm_cpu_phys2virt(cpu);
247 if (routing_mode == 0 && !test_bit(virt_id, &targets))
249 else if (routing_mode == 1 && cpu == this_cpu)
252 irqchip_set_pending(per_cpu(cpu), irq, false);
253 sgi.targets |= (1 << cpu);
256 /* Let the other CPUS inject their SGIs */
263 * Handle the maintenance interrupt, the rest is injected into the cell.
264 * Return true when the IRQ has been handled by the hyp.
266 static bool arch_handle_phys_irq(struct per_cpu *cpu_data, u32 irqn)
268 if (irqn == MAINTENANCE_IRQ) {
269 irqchip_inject_pending(cpu_data);
273 irqchip_set_pending(cpu_data, irqn, true);
278 static void gic_eoi_irq(u32 irq_id, bool deactivate)
280 arm_write_sysreg(ICC_EOIR1_EL1, irq_id);
282 arm_write_sysreg(ICC_DIR_EL1, irq_id);
285 static void gic_handle_irq(struct per_cpu *cpu_data)
287 bool handled = false;
291 /* Read ICC_IAR1: set 'active' state */
292 arm_read_sysreg(ICC_IAR1_EL1, irq_id);
294 if (irq_id == 0x3ff) /* Spurious IRQ */
298 if (is_sgi(irq_id)) {
299 arch_handle_sgi(cpu_data, irq_id);
302 handled = arch_handle_phys_irq(cpu_data, irq_id);
306 * Write ICC_EOIR1: drop priority, but stay active if handled is
308 * This allows to not be re-interrupted by a level-triggered
309 * interrupt that needs handling in the guest (e.g. timer)
311 gic_eoi_irq(irq_id, handled);
315 static int gic_inject_irq(struct per_cpu *cpu_data, struct pending_irq *irq)
322 arm_read_sysreg(ICH_ELSR_EL2, elsr);
323 for (i = 0; i < gic_num_lr; i++) {
324 if ((elsr >> i) & 1) {
325 /* Entry is invalid, candidate for injection */
332 * Entry is in use, check that it doesn't match the one we want
338 * A strict phys->virt id mapping is used for SPIs, so this test
339 * should be sufficient.
341 if ((u32)lr == irq->virt_id)
348 * All list registers are in use, trigger a maintenance
349 * interrupt once they are available again.
351 arm_read_sysreg(ICH_HCR_EL2, hcr);
353 arm_write_sysreg(ICH_HCR_EL2, hcr);
359 /* Only group 1 interrupts */
360 lr |= ICH_LR_GROUP_BIT;
361 lr |= ICH_LR_PENDING;
364 lr |= (u64)irq->type.irq << ICH_LR_PHYS_ID_SHIFT;
365 } else if (irq->type.sgi.maintenance) {
366 lr |= ICH_LR_SGI_EOI;
369 gic_write_lr(free_lr, lr);
374 static int gic_handle_redist_access(struct per_cpu *cpu_data,
375 struct mmio_access *access)
379 int ret = TRAP_UNHANDLED;
380 unsigned int virt_id;
381 void *virt_redist = 0;
382 void *phys_redist = 0;
383 unsigned int redist_size = (gic_version == 4) ? 0x40000 : 0x20000;
384 void *address = (void *)access->addr;
387 * The redistributor accessed by the cell is not the one stored in these
388 * cpu_datas, but the one associated to its virtual id. So we first
389 * need to translate the redistributor address.
391 for_each_cpu(cpu, cpu_data->cell->cpu_set) {
392 virt_id = arm_cpu_phys2virt(cpu);
393 virt_redist = per_cpu(virt_id)->gicr_base;
394 if (address >= virt_redist && address < virt_redist
396 phys_redist = per_cpu(cpu)->gicr_base;
401 if (phys_redist == NULL)
402 return TRAP_FORBIDDEN;
404 reg = address - virt_redist;
405 access->addr = (unsigned long)phys_redist + reg;
407 /* Change the ID register, all other accesses are allowed. */
408 if (!access->is_write) {
411 if (virt_id == cpu_data->cell->arch.last_virt_id)
412 access->val = GICR_TYPER_Last;
415 /* AArch64 can use a writeq for this register */
416 if (access->size == 8)
417 access->val |= (u64)virt_id << 32;
422 /* Upper bits contain the affinity */
423 access->val = virt_id;
428 if (ret == TRAP_HANDLED)
431 arch_mmio_access(access);
435 static int gic_mmio_access(struct per_cpu *cpu_data,
436 struct mmio_access *access)
438 void *address = (void *)access->addr;
440 if (address >= gicr_base && address < gicr_base + gicr_size)
441 return gic_handle_redist_access(cpu_data, access);
443 return TRAP_UNHANDLED;
446 struct irqchip_ops gic_irqchip = {
448 .cpu_init = gic_cpu_init,
449 .cpu_reset = gic_cpu_reset,
450 .cell_init = gic_cell_init,
451 .cell_exit = gic_cell_exit,
452 .send_sgi = gic_send_sgi,
453 .handle_irq = gic_handle_irq,
454 .inject_irq = gic_inject_irq,
455 .eoi_irq = gic_eoi_irq,
456 .mmio_access = gic_mmio_access,