]> rtime.felk.cvut.cz Git - jailhouse.git/blob - hypervisor/arch/arm/gic-v3.c
arm: Rework interrupt affinity management on cell creation
[jailhouse.git] / hypervisor / arch / arm / gic-v3.c
1 /*
2  * Jailhouse, a Linux-based partitioning hypervisor
3  *
4  * Copyright (c) ARM Limited, 2014
5  *
6  * Authors:
7  *  Jean-Philippe Brucker <jean-philippe.brucker@arm.com>
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2.  See
10  * the COPYING file in the top-level directory.
11  */
12
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>
24
25 /*
26  * This implementation assumes that the kernel driver already initialised most
27  * of the GIC.
28  * There is almost no instruction barrier, since IRQs are always disabled in the
29  * hyp, and ERET serves as the context synchronization event.
30  */
31
32 static unsigned int gic_num_lr;
33 static unsigned int gic_num_priority_bits;
34 static u32 gic_version;
35
36 extern void *gicd_base;
37 extern unsigned int gicd_size;
38 static void *gicr_base;
39 static unsigned int gicr_size;
40
41 static int gic_init(void)
42 {
43         /* TODO: need to validate more? */
44         if (!(mmio_read32(gicd_base + GICD_CTLR) & GICD_CTLR_ARE_NS))
45                 return trace_error(-EIO);
46
47         /* FIXME: parse a dt */
48         gicr_base = GICR_BASE;
49         gicr_size = GICR_SIZE;
50
51         /* Let the per-cpu code access the redistributors */
52         return arch_map_device(gicr_base, gicr_base, gicr_size);
53 }
54
55 static void gic_clear_pending_irqs(void)
56 {
57         unsigned int n;
58
59         /* Clear list registers. */
60         for (n = 0; n < gic_num_lr; n++)
61                 gic_write_lr(n, 0);
62
63         /* Clear active priority bits */
64         if (gic_num_priority_bits >= 5)
65                 arm_write_sysreg(ICH_AP1R0_EL2, 0);
66         if (gic_num_priority_bits >= 6)
67                 arm_write_sysreg(ICH_AP1R1_EL2, 0);
68         if (gic_num_priority_bits > 6) {
69                 arm_write_sysreg(ICH_AP1R2_EL2, 0);
70                 arm_write_sysreg(ICH_AP1R3_EL2, 0);
71         }
72 }
73
74 static int gic_cpu_reset(struct per_cpu *cpu_data, bool is_shutdown)
75 {
76         unsigned int i;
77         void *gicr = cpu_data->gicr_base;
78         unsigned long active;
79         bool root_shutdown = is_shutdown && (cpu_data->cell == &root_cell);
80         u32 ich_vmcr;
81
82         if (gicr == 0)
83                 return -ENODEV;
84
85         gic_clear_pending_irqs();
86
87         gicr += GICR_SGI_BASE;
88         active = mmio_read32(gicr + GICR_ICACTIVER);
89         /* Deactivate all active PPIs */
90         for (i = 16; i < 32; i++) {
91                 if (test_bit(i, &active))
92                         arm_write_sysreg(ICC_DIR_EL1, i);
93         }
94
95         /* Ensure all IPIs and the maintenance PPI are enabled. */
96         mmio_write32(gicr + GICR_ISENABLER,
97                      0x0000ffff | (1 << MAINTENANCE_IRQ));
98
99         /*
100          * Disable PPIs, except for the maintenance interrupt.
101          * On shutdown, the root cell expects to find all its PPIs still
102          * enabled - except for the maintenance interrupt we used.
103          */
104         mmio_write32(gicr + GICR_ICENABLER,
105                      root_shutdown ? 1 << MAINTENANCE_IRQ :
106                                      0xffff0000 & ~(1 << MAINTENANCE_IRQ));
107
108         if (root_shutdown) {
109                 /* Restore the root config */
110                 arm_read_sysreg(ICH_VMCR_EL2, ich_vmcr);
111
112                 if (!(ich_vmcr & ICH_VMCR_VEOIM)) {
113                         u32 icc_ctlr;
114                         arm_read_sysreg(ICC_CTLR_EL1, icc_ctlr);
115                         icc_ctlr &= ~ICC_CTLR_EOImode;
116                         arm_write_sysreg(ICC_CTLR_EL1, icc_ctlr);
117                 }
118
119                 arm_write_sysreg(ICH_HCR_EL2, 0);
120         }
121
122         arm_write_sysreg(ICH_VMCR_EL2, 0);
123
124         return 0;
125 }
126
127 static int gic_cpu_init(struct per_cpu *cpu_data)
128 {
129         u64 typer;
130         u32 pidr;
131         u32 cell_icc_ctlr, cell_icc_pmr, cell_icc_igrpen1;
132         u32 ich_vtr;
133         u32 ich_vmcr;
134         void *redist_base = gicr_base;
135
136         /* Find redistributor */
137         do {
138                 pidr = mmio_read32(redist_base + GICR_PIDR2);
139                 gic_version = GICR_PIDR2_ARCH(pidr);
140                 if (gic_version != 3 && gic_version != 4)
141                         break;
142
143                 typer = mmio_read64(redist_base + GICR_TYPER);
144                 if ((typer >> 32) == cpu_data->cpu_id) {
145                         cpu_data->gicr_base = redist_base;
146                         break;
147                 }
148
149                 redist_base += 0x20000;
150                 if (gic_version == 4)
151                         redist_base += 0x20000;
152         } while (!(typer & GICR_TYPER_Last));
153
154         if (cpu_data->gicr_base == 0) {
155                 printk("GIC: No redist found for CPU%d\n", cpu_data->cpu_id);
156                 return -ENODEV;
157         }
158
159         /* Ensure all IPIs and the maintenance PPI are enabled. */
160         mmio_write32(redist_base + GICR_SGI_BASE + GICR_ISENABLER,
161                      0x0000ffff | (1 << MAINTENANCE_IRQ));
162
163         /*
164          * Set EOIMode to 1
165          * This allow to drop the priority of level-triggered interrupts without
166          * deactivating them, and thus ensure that they won't be immediately
167          * re-triggered. (e.g. timer)
168          * They can then be injected into the guest using the LR.HW bit, and
169          * will be deactivated once the guest does an EOI after handling the
170          * interrupt source.
171          */
172         arm_read_sysreg(ICC_CTLR_EL1, cell_icc_ctlr);
173         arm_write_sysreg(ICC_CTLR_EL1, ICC_CTLR_EOImode);
174
175         arm_read_sysreg(ICC_PMR_EL1, cell_icc_pmr);
176         arm_write_sysreg(ICC_PMR_EL1, ICC_PMR_DEFAULT);
177
178         arm_read_sysreg(ICC_IGRPEN1_EL1, cell_icc_igrpen1);
179         arm_write_sysreg(ICC_IGRPEN1_EL1, ICC_IGRPEN1_EN);
180
181         arm_read_sysreg(ICH_VTR_EL2, ich_vtr);
182         gic_num_lr = (ich_vtr & 0xf) + 1;
183         gic_num_priority_bits = (ich_vtr >> 29) + 1;
184
185         /*
186          * Clear pending virtual IRQs in case anything is left from previous
187          * use. Physically pending IRQs will be forwarded to Linux once we
188          * enable interrupts for the hypervisor.
189          */
190         gic_clear_pending_irqs();
191
192         ich_vmcr = (cell_icc_pmr & ICC_PMR_MASK) << ICH_VMCR_VPMR_SHIFT;
193         if (cell_icc_igrpen1 & ICC_IGRPEN1_EN)
194                 ich_vmcr |= ICH_VMCR_VENG1;
195         if (cell_icc_ctlr & ICC_CTLR_EOImode)
196                 ich_vmcr |= ICH_VMCR_VEOIM;
197         arm_write_sysreg(ICH_VMCR_EL2, ich_vmcr);
198
199         /* After this, the cells access the virtual interface of the GIC. */
200         arm_write_sysreg(ICH_HCR_EL2, ICH_HCR_EN);
201
202         return 0;
203 }
204
205 static void gic_adjust_irq_target(struct cell *cell, u16 irq_id)
206 {
207         void *irouter = gicd_base + GICD_IROUTER + irq_id;
208         u32 route = mmio_read32(irouter);
209
210         if (!cell_owns_cpu(cell, route))
211                 mmio_write32(irouter, first_cpu(cell->cpu_set));
212 }
213
214 static enum mmio_result gic_handle_redist_access(void *arg,
215                                                  struct mmio_access *mmio)
216 {
217         struct cell *cell = this_cell();
218         unsigned int cpu;
219         unsigned int virt_id;
220         void *virt_redist = 0;
221         void *phys_redist = 0;
222         unsigned int redist_size = (gic_version == 4) ? 0x40000 : 0x20000;
223         void *address = (void *)(mmio->address + (unsigned long)gicr_base);
224
225         /*
226          * The redistributor accessed by the cell is not the one stored in these
227          * cpu_datas, but the one associated to its virtual id. So we first
228          * need to translate the redistributor address.
229          */
230         for_each_cpu(cpu, cell->cpu_set) {
231                 virt_id = arm_cpu_phys2virt(cpu);
232                 virt_redist = per_cpu(virt_id)->gicr_base;
233                 if (address >= virt_redist && address < virt_redist
234                                 + redist_size) {
235                         phys_redist = per_cpu(cpu)->gicr_base;
236                         break;
237                 }
238         }
239
240         if (phys_redist == NULL)
241                 return MMIO_ERROR;
242
243         mmio->address = address - virt_redist;
244
245         /* Change the ID register, all other accesses are allowed. */
246         if (!mmio->is_write) {
247                 switch (mmio->address) {
248                 case GICR_TYPER:
249                         if (virt_id == cell->arch.last_virt_id)
250                                 mmio->value = GICR_TYPER_Last;
251                         else
252                                 mmio->value = 0;
253                         /* AArch64 can use a writeq for this register */
254                         if (mmio->size == 8)
255                                 mmio->value |= (u64)virt_id << 32;
256
257                         return MMIO_HANDLED;
258                 case GICR_TYPER + 4:
259                         /* Upper bits contain the affinity */
260                         mmio->value = virt_id;
261                         return MMIO_HANDLED;
262                 }
263         }
264         mmio_perform_access(phys_redist, mmio);
265         return MMIO_HANDLED;
266 }
267
268 static int gic_cell_init(struct cell *cell)
269 {
270         mmio_region_register(cell, (unsigned long)gicd_base, gicd_size,
271                              gic_handle_dist_access, NULL);
272         mmio_region_register(cell, (unsigned long)gicr_base, gicr_size,
273                              gic_handle_redist_access, NULL);
274
275         return 0;
276 }
277
278 static int gic_send_sgi(struct sgi *sgi)
279 {
280         u64 val;
281         u16 targets = sgi->targets;
282
283         if (!is_sgi(sgi->id))
284                 return -EINVAL;
285
286         if (sgi->routing_mode == 2)
287                 targets = 1 << phys_processor_id();
288
289         val = (u64)sgi->aff3 << ICC_SGIR_AFF3_SHIFT
290             | (u64)sgi->aff2 << ICC_SGIR_AFF2_SHIFT
291             | sgi->aff1 << ICC_SGIR_AFF1_SHIFT
292             | (targets & ICC_SGIR_TARGET_MASK)
293             | (sgi->id & 0xf) << ICC_SGIR_IRQN_SHIFT;
294
295         if (sgi->routing_mode == 1)
296                 val |= ICC_SGIR_ROUTING_BIT;
297
298         /*
299          * Ensure the targets see our modifications to their per-cpu
300          * structures.
301          */
302         dsb(ish);
303
304         arm_write_sysreg(ICC_SGI1R_EL1, val);
305         isb();
306
307         return 0;
308 }
309
310 void gicv3_handle_sgir_write(u64 sgir)
311 {
312         struct sgi sgi;
313         unsigned long routing_mode = !!(sgir & ICC_SGIR_ROUTING_BIT);
314
315         /* FIXME: clusters are not supported yet. */
316         sgi.targets = sgir & ICC_SGIR_TARGET_MASK;
317         sgi.routing_mode = routing_mode;
318         sgi.aff1 = sgir >> ICC_SGIR_AFF1_SHIFT & 0xff;
319         sgi.aff2 = sgir >> ICC_SGIR_AFF2_SHIFT & 0xff;
320         sgi.aff3 = sgir >> ICC_SGIR_AFF3_SHIFT & 0xff;
321         sgi.id = sgir >> ICC_SGIR_IRQN_SHIFT & 0xf;
322
323         gic_handle_sgir_write(&sgi, true);
324 }
325
326 /*
327  * GICv3 uses a 64bit register IROUTER for each IRQ
328  */
329 enum mmio_result gic_handle_irq_route(struct mmio_access *mmio,
330                                       unsigned int irq)
331 {
332         struct cell *cell = this_cell();
333         unsigned int cpu;
334
335         /* Ignore aff3 on AArch32 (return 0) */
336         if (mmio->size == 4 && (mmio->address % 8))
337                 return MMIO_HANDLED;
338
339         /* SGIs and PPIs are res0 */
340         if (!is_spi(irq))
341                 return MMIO_HANDLED;
342
343         /*
344          * Ignore accesses to SPIs that do not belong to the cell. This isn't
345          * forbidden, because the guest driver may simply iterate over all
346          * registers at initialisation
347          */
348         if (!irqchip_irq_in_cell(cell, irq))
349                 return MMIO_HANDLED;
350
351         /* Translate the virtual cpu id into the physical one */
352         if (mmio->is_write) {
353                 mmio->value = arm_cpu_virt2phys(cell, mmio->value);
354                 if (mmio->value == -1) {
355                         printk("Attempt to route IRQ%d outside of cell\n", irq);
356                         return MMIO_ERROR;
357                 }
358                 mmio_perform_access(gicd_base, mmio);
359         } else {
360                 cpu = mmio_read32(gicd_base + GICD_IROUTER + 8 * irq);
361                 mmio->value = arm_cpu_phys2virt(cpu);
362         }
363         return MMIO_HANDLED;
364 }
365
366 static void gic_eoi_irq(u32 irq_id, bool deactivate)
367 {
368         arm_write_sysreg(ICC_EOIR1_EL1, irq_id);
369         if (deactivate)
370                 arm_write_sysreg(ICC_DIR_EL1, irq_id);
371 }
372
373 static int gic_inject_irq(struct per_cpu *cpu_data, u16 irq_id)
374 {
375         int i;
376         int free_lr = -1;
377         u32 elsr;
378         u64 lr;
379
380         arm_read_sysreg(ICH_ELSR_EL2, elsr);
381         for (i = 0; i < gic_num_lr; i++) {
382                 if ((elsr >> i) & 1) {
383                         /* Entry is invalid, candidate for injection */
384                         if (free_lr == -1)
385                                 free_lr = i;
386                         continue;
387                 }
388
389                 /*
390                  * Entry is in use, check that it doesn't match the one we want
391                  * to inject.
392                  */
393                 lr = gic_read_lr(i);
394
395                 /*
396                  * A strict phys->virt id mapping is used for SPIs, so this test
397                  * should be sufficient.
398                  */
399                 if ((u32)lr == irq_id)
400                         return -EEXIST;
401         }
402
403         if (free_lr == -1)
404                 /* All list registers are in use */
405                 return -EBUSY;
406
407         lr = irq_id;
408         /* Only group 1 interrupts */
409         lr |= ICH_LR_GROUP_BIT;
410         lr |= ICH_LR_PENDING;
411         if (!is_sgi(irq_id)) {
412                 lr |= ICH_LR_HW_BIT;
413                 lr |= (u64)irq_id << ICH_LR_PHYS_ID_SHIFT;
414         }
415
416         gic_write_lr(free_lr, lr);
417
418         return 0;
419 }
420
421 static void gicv3_enable_maint_irq(bool enable)
422 {
423         u32 hcr;
424
425         arm_read_sysreg(ICH_HCR_EL2, hcr);
426         if (enable)
427                 hcr |= ICH_HCR_UIE;
428         else
429                 hcr &= ~ICH_HCR_UIE;
430         arm_write_sysreg(ICH_HCR_EL2, hcr);
431 }
432
433 unsigned int irqchip_mmio_count_regions(struct cell *cell)
434 {
435         return 2;
436 }
437
438 struct irqchip_ops irqchip = {
439         .init = gic_init,
440         .cpu_init = gic_cpu_init,
441         .cpu_reset = gic_cpu_reset,
442         .cell_init = gic_cell_init,
443         .adjust_irq_target = gic_adjust_irq_target,
444         .send_sgi = gic_send_sgi,
445         .handle_irq = gic_handle_irq,
446         .inject_irq = gic_inject_irq,
447         .enable_maint_irq = gicv3_enable_maint_irq,
448         .eoi_irq = gic_eoi_irq,
449 };