]> rtime.felk.cvut.cz Git - jailhouse.git/blob - hypervisor/arch/arm/gic-v3.c
arm: Account for errors during irqchip cell_init
[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         int err;
44
45         /* FIXME: parse a dt */
46         gicr_base = GICR_BASE;
47         gicr_size = GICR_SIZE;
48
49         /* Let the per-cpu code access the redistributors */
50         err = arch_map_device(gicr_base, gicr_base, gicr_size);
51
52         return err;
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         /*
96          * Disable all PPIs, ensure IPIs are enabled.
97          * On shutdown, the root cell expects to find all its PPIs still enabled
98          * when returning to the driver.
99          */
100         if (!root_shutdown)
101                 mmio_write32(gicr + GICR_ICENABLER, 0xffff0000);
102         mmio_write32(gicr + GICR_ISENABLER, 0x0000ffff);
103
104         if (root_shutdown) {
105                 /* Restore the root config */
106                 arm_read_sysreg(ICH_VMCR_EL2, ich_vmcr);
107
108                 if (!(ich_vmcr & ICH_VMCR_VEOIM)) {
109                         u32 icc_ctlr;
110                         arm_read_sysreg(ICC_CTLR_EL1, icc_ctlr);
111                         icc_ctlr &= ~ICC_CTLR_EOImode;
112                         arm_write_sysreg(ICC_CTLR_EL1, icc_ctlr);
113                 }
114
115                 arm_write_sysreg(ICH_HCR_EL2, 0);
116         }
117
118         arm_write_sysreg(ICH_VMCR_EL2, 0);
119
120         return 0;
121 }
122
123 static int gic_cpu_init(struct per_cpu *cpu_data)
124 {
125         u64 typer;
126         u32 pidr;
127         u32 cell_icc_ctlr, cell_icc_pmr, cell_icc_igrpen1;
128         u32 ich_vtr;
129         u32 ich_vmcr;
130         void *redist_base = gicr_base;
131
132         /* Find redistributor */
133         do {
134                 pidr = mmio_read32(redist_base + GICR_PIDR2);
135                 gic_version = GICR_PIDR2_ARCH(pidr);
136                 if (gic_version != 3 && gic_version != 4)
137                         break;
138
139                 typer = mmio_read64(redist_base + GICR_TYPER);
140                 if ((typer >> 32) == cpu_data->cpu_id) {
141                         cpu_data->gicr_base = redist_base;
142                         break;
143                 }
144
145                 redist_base += 0x20000;
146                 if (gic_version == 4)
147                         redist_base += 0x20000;
148         } while (!(typer & GICR_TYPER_Last));
149
150         if (cpu_data->gicr_base == 0) {
151                 printk("GIC: No redist found for CPU%d\n", cpu_data->cpu_id);
152                 return -ENODEV;
153         }
154
155         /* Ensure all IPIs are enabled */
156         mmio_write32(redist_base + GICR_SGI_BASE + GICR_ISENABLER, 0x0000ffff);
157
158         /*
159          * Set EOIMode to 1
160          * This allow to drop the priority of level-triggered interrupts without
161          * deactivating them, and thus ensure that they won't be immediately
162          * re-triggered. (e.g. timer)
163          * They can then be injected into the guest using the LR.HW bit, and
164          * will be deactivated once the guest does an EOI after handling the
165          * interrupt source.
166          */
167         arm_read_sysreg(ICC_CTLR_EL1, cell_icc_ctlr);
168         arm_write_sysreg(ICC_CTLR_EL1, ICC_CTLR_EOImode);
169
170         arm_read_sysreg(ICC_PMR_EL1, cell_icc_pmr);
171         arm_write_sysreg(ICC_PMR_EL1, ICC_PMR_DEFAULT);
172
173         arm_read_sysreg(ICC_IGRPEN1_EL1, cell_icc_igrpen1);
174         arm_write_sysreg(ICC_IGRPEN1_EL1, ICC_IGRPEN1_EN);
175
176         arm_read_sysreg(ICH_VTR_EL2, ich_vtr);
177         gic_num_lr = (ich_vtr & 0xf) + 1;
178         gic_num_priority_bits = (ich_vtr >> 29) + 1;
179
180         /*
181          * Clear pending virtual IRQs in case anything is left from previous
182          * use. Physically pending IRQs will be forwarded to Linux once we
183          * enable interrupts for the hypervisor.
184          */
185         gic_clear_pending_irqs();
186
187         ich_vmcr = (cell_icc_pmr & ICC_PMR_MASK) << ICH_VMCR_VPMR_SHIFT;
188         if (cell_icc_igrpen1 & ICC_IGRPEN1_EN)
189                 ich_vmcr |= ICH_VMCR_VENG1;
190         if (cell_icc_ctlr & ICC_CTLR_EOImode)
191                 ich_vmcr |= ICH_VMCR_VEOIM;
192         arm_write_sysreg(ICH_VMCR_EL2, ich_vmcr);
193
194         /* After this, the cells access the virtual interface of the GIC. */
195         arm_write_sysreg(ICH_HCR_EL2, ICH_HCR_EN);
196
197         return 0;
198 }
199
200 static void gic_route_spis(struct cell *config_cell, struct cell *dest_cell)
201 {
202         int i;
203         void *irouter = gicd_base + GICD_IROUTER;
204         unsigned int first_cpu;
205
206         /* Use the core functions to retrieve the first physical id */
207         for_each_cpu(first_cpu, dest_cell->cpu_set)
208                 break;
209
210         for (i = 0; i < 64; i++, irouter += 8) {
211                 if (spi_in_cell(config_cell, i))
212                         mmio_write64(irouter, first_cpu);
213         }
214 }
215
216 static int gic_cell_init(struct cell *cell)
217 {
218         gic_route_spis(cell, cell);
219         return 0;
220 }
221
222 static void gic_cell_exit(struct cell *cell)
223 {
224         /* Reset interrupt routing of the cell's spis*/
225         gic_route_spis(cell, &root_cell);
226 }
227
228 static int gic_send_sgi(struct sgi *sgi)
229 {
230         u64 val;
231         u16 targets = sgi->targets;
232
233         if (!is_sgi(sgi->id))
234                 return -EINVAL;
235
236         if (sgi->routing_mode == 2)
237                 targets = 1 << phys_processor_id();
238
239         val = (u64)sgi->aff3 << ICC_SGIR_AFF3_SHIFT
240             | (u64)sgi->aff2 << ICC_SGIR_AFF2_SHIFT
241             | sgi->aff1 << ICC_SGIR_AFF1_SHIFT
242             | (targets & ICC_SGIR_TARGET_MASK)
243             | (sgi->id & 0xf) << ICC_SGIR_IRQN_SHIFT;
244
245         if (sgi->routing_mode == 1)
246                 val |= ICC_SGIR_ROUTING_BIT;
247
248         /*
249          * Ensure the targets see our modifications to their per-cpu
250          * structures.
251          */
252         dsb(ish);
253
254         arm_write_sysreg(ICC_SGI1R_EL1, val);
255         isb();
256
257         return 0;
258 }
259
260 int gicv3_handle_sgir_write(struct per_cpu *cpu_data, u64 sgir)
261 {
262         struct sgi sgi;
263         unsigned long routing_mode = !!(sgir & ICC_SGIR_ROUTING_BIT);
264
265         /* FIXME: clusters are not supported yet. */
266         sgi.targets = sgir & ICC_SGIR_TARGET_MASK;
267         sgi.routing_mode = routing_mode;
268         sgi.aff1 = sgir >> ICC_SGIR_AFF1_SHIFT & 0xff;
269         sgi.aff2 = sgir >> ICC_SGIR_AFF2_SHIFT & 0xff;
270         sgi.aff3 = sgir >> ICC_SGIR_AFF3_SHIFT & 0xff;
271         sgi.id = sgir >> ICC_SGIR_IRQN_SHIFT & 0xf;
272
273         return gic_handle_sgir_write(cpu_data, &sgi, true);
274 }
275
276 static void gic_eoi_irq(u32 irq_id, bool deactivate)
277 {
278         arm_write_sysreg(ICC_EOIR1_EL1, irq_id);
279         if (deactivate)
280                 arm_write_sysreg(ICC_DIR_EL1, irq_id);
281 }
282
283 static int gic_inject_irq(struct per_cpu *cpu_data, struct pending_irq *irq)
284 {
285         int i;
286         int free_lr = -1;
287         u32 elsr;
288         u64 lr;
289
290         arm_read_sysreg(ICH_ELSR_EL2, elsr);
291         for (i = 0; i < gic_num_lr; i++) {
292                 if ((elsr >> i) & 1) {
293                         /* Entry is invalid, candidate for injection */
294                         if (free_lr == -1)
295                                 free_lr = i;
296                         continue;
297                 }
298
299                 /*
300                  * Entry is in use, check that it doesn't match the one we want
301                  * to inject.
302                  */
303                 lr = gic_read_lr(i);
304
305                 /*
306                  * A strict phys->virt id mapping is used for SPIs, so this test
307                  * should be sufficient.
308                  */
309                 if ((u32)lr == irq->virt_id)
310                         return -EINVAL;
311         }
312
313         if (free_lr == -1) {
314                 u32 hcr;
315                 /*
316                  * All list registers are in use, trigger a maintenance
317                  * interrupt once they are available again.
318                  */
319                 arm_read_sysreg(ICH_HCR_EL2, hcr);
320                 hcr |= ICH_HCR_UIE;
321                 arm_write_sysreg(ICH_HCR_EL2, hcr);
322
323                 return -EBUSY;
324         }
325
326         lr = irq->virt_id;
327         /* Only group 1 interrupts */
328         lr |= ICH_LR_GROUP_BIT;
329         lr |= ICH_LR_PENDING;
330         if (irq->hw) {
331                 lr |= ICH_LR_HW_BIT;
332                 lr |= (u64)irq->type.irq << ICH_LR_PHYS_ID_SHIFT;
333         } else if (irq->type.sgi.maintenance) {
334                 lr |= ICH_LR_SGI_EOI;
335         }
336
337         gic_write_lr(free_lr, lr);
338
339         return 0;
340 }
341
342 static int gic_handle_redist_access(struct per_cpu *cpu_data,
343                                     struct mmio_access *access)
344 {
345         unsigned int cpu;
346         unsigned int reg;
347         int ret = TRAP_UNHANDLED;
348         unsigned int virt_id;
349         void *virt_redist = 0;
350         void *phys_redist = 0;
351         unsigned int redist_size = (gic_version == 4) ? 0x40000 : 0x20000;
352         void *address = (void *)access->addr;
353
354         /*
355          * The redistributor accessed by the cell is not the one stored in these
356          * cpu_datas, but the one associated to its virtual id. So we first
357          * need to translate the redistributor address.
358          */
359         for_each_cpu(cpu, cpu_data->cell->cpu_set) {
360                 virt_id = arm_cpu_phys2virt(cpu);
361                 virt_redist = per_cpu(virt_id)->gicr_base;
362                 if (address >= virt_redist && address < virt_redist
363                                 + redist_size) {
364                         phys_redist = per_cpu(cpu)->gicr_base;
365                         break;
366                 }
367         }
368
369         if (phys_redist == NULL)
370                 return TRAP_FORBIDDEN;
371
372         reg = address - virt_redist;
373         access->addr = (unsigned long)phys_redist + reg;
374
375         /* Change the ID register, all other accesses are allowed. */
376         if (!access->is_write) {
377                 switch (reg) {
378                 case GICR_TYPER:
379                         if (virt_id == cpu_data->cell->arch.last_virt_id)
380                                 access->val = GICR_TYPER_Last;
381                         else
382                                 access->val = 0;
383                         /* AArch64 can use a writeq for this register */
384                         if (access->size == 8)
385                                 access->val |= (u64)virt_id << 32;
386
387                         ret = TRAP_HANDLED;
388                         break;
389                 case GICR_TYPER + 4:
390                         /* Upper bits contain the affinity */
391                         access->val = virt_id;
392                         ret = TRAP_HANDLED;
393                         break;
394                 }
395         }
396         if (ret == TRAP_HANDLED)
397                 return ret;
398
399         arch_mmio_access(access);
400         return TRAP_HANDLED;
401 }
402
403 static int gic_mmio_access(struct per_cpu *cpu_data,
404                            struct mmio_access *access)
405 {
406         void *address = (void *)access->addr;
407
408         if (address >= gicd_base && address < gicd_base + gicd_size)
409                 return gic_handle_dist_access(cpu_data, access);
410
411         if (address >= gicr_base && address < gicr_base + gicr_size)
412                 return gic_handle_redist_access(cpu_data, access);
413
414         return TRAP_UNHANDLED;
415 }
416
417 struct irqchip_ops gic_irqchip = {
418         .init = gic_init,
419         .cpu_init = gic_cpu_init,
420         .cpu_reset = gic_cpu_reset,
421         .cell_init = gic_cell_init,
422         .cell_exit = gic_cell_exit,
423         .send_sgi = gic_send_sgi,
424         .handle_irq = gic_handle_irq,
425         .inject_irq = gic_inject_irq,
426         .eoi_irq = gic_eoi_irq,
427         .mmio_access = gic_mmio_access,
428 };