]> rtime.felk.cvut.cz Git - jailhouse.git/blob - hypervisor/arch/arm/gic-v3.c
arm: irqchip: add SPI configuration in cell_init and cell_exit
[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 int gic_cpu_reset(struct per_cpu *cpu_data)
56 {
57         unsigned int i;
58         void *gicr = cpu_data->gicr_base;
59         unsigned long active;
60
61         if (gicr == 0)
62                 return -ENODEV;
63
64         /* Clear list registers */
65         for (i = 0; i < gic_num_lr; i++)
66                 gic_write_lr(i, 0);
67
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);
74         }
75
76         /* Disable all PPIs, ensure IPIs are enabled */
77         mmio_write32(gicr + GICR_ICENABLER, 0xffff0000);
78         mmio_write32(gicr + GICR_ISENABLER, 0x0000ffff);
79
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);
88         }
89
90         arm_write_sysreg(ICH_VMCR_EL2, 0);
91         arm_write_sysreg(ICH_HCR_EL2, ICH_HCR_EN);
92
93         return 0;
94 }
95
96 static int gic_cpu_init(struct per_cpu *cpu_data)
97 {
98         u64 typer;
99         u32 pidr;
100         u32 cell_icc_ctlr, cell_icc_pmr, cell_icc_igrpen1;
101         u32 ich_vtr;
102         u32 ich_vmcr;
103         void *redist_base = gicr_base;
104
105         /* Find redistributor */
106         do {
107                 pidr = mmio_read32(redist_base + GICR_PIDR2);
108                 gic_version = GICR_PIDR2_ARCH(pidr);
109                 if (gic_version != 3 && gic_version != 4)
110                         break;
111
112                 typer = mmio_read64(redist_base + GICR_TYPER);
113                 if ((typer >> 32) == cpu_data->cpu_id) {
114                         cpu_data->gicr_base = redist_base;
115                         break;
116                 }
117
118                 redist_base += 0x20000;
119                 if (gic_version == 4)
120                         redist_base += 0x20000;
121         } while (!(typer & GICR_TYPER_Last));
122
123         if (cpu_data->gicr_base == 0) {
124                 printk("GIC: No redist found for CPU%d\n", cpu_data->cpu_id);
125                 return -ENODEV;
126         }
127
128         /* Ensure all IPIs are enabled */
129         mmio_write32(redist_base + GICR_SGI_BASE + GICR_ISENABLER, 0x0000ffff);
130
131         /*
132          * Set EOIMode to 1
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
138          * interrupt source.
139          */
140         arm_read_sysreg(ICC_CTLR_EL1, cell_icc_ctlr);
141         arm_write_sysreg(ICC_CTLR_EL1, ICC_CTLR_EOImode);
142
143         arm_read_sysreg(ICC_PMR_EL1, cell_icc_pmr);
144         arm_write_sysreg(ICC_PMR_EL1, ICC_PMR_DEFAULT);
145
146         arm_read_sysreg(ICC_IGRPEN1_EL1, cell_icc_igrpen1);
147         arm_write_sysreg(ICC_IGRPEN1_EL1, ICC_IGRPEN1_EN);
148
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;
152
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);
159
160         /* After this, the cells access the virtual interface of the GIC. */
161         arm_write_sysreg(ICH_HCR_EL2, ICH_HCR_EN);
162
163         return 0;
164 }
165
166 static void gic_route_spis(struct cell *config_cell, struct cell *dest_cell)
167 {
168         int i;
169         u64 spis = config_cell->arch.spis;
170         void *irouter = gicd_base + GICD_IROUTER;
171         unsigned int first_cpu;
172
173         /* Use the core functions to retrieve the first physical id */
174         for_each_cpu(first_cpu, dest_cell->cpu_set)
175                 break;
176
177         for (i = 0; i < 64; i++, irouter += 8) {
178                 if (test_bit(i, (unsigned long *)&spis))
179                         mmio_write64(irouter, first_cpu);
180         }
181 }
182
183 static void gic_cell_init(struct cell *cell)
184 {
185         gic_route_spis(cell, cell);
186 }
187
188 static void gic_cell_exit(struct cell *cell)
189 {
190         /* Reset interrupt routing of the cell's spis*/
191         gic_route_spis(cell, &root_cell);
192 }
193
194 static int gic_send_sgi(struct sgi *sgi)
195 {
196         u64 val;
197         u16 targets = sgi->targets;
198
199         if (!is_sgi(sgi->id))
200                 return -EINVAL;
201
202         if (sgi->routing_mode == 2)
203                 targets = 1 << phys_processor_id();
204
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;
210
211         if (sgi->routing_mode == 1)
212                 val |= ICC_SGIR_ROUTING_BIT;
213
214         /*
215          * Ensure the targets see our modifications to their per-cpu
216          * structures.
217          */
218         dsb(ish);
219
220         arm_write_sysreg(ICC_SGI1R_EL1, val);
221         isb();
222
223         return 0;
224 }
225
226 int gicv3_handle_sgir_write(struct per_cpu *cpu_data, u64 sgir)
227 {
228         struct sgi sgi;
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;
235
236         /* FIXME: clusters are not supported yet. */
237         sgi.targets = 0;
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;
242         sgi.id = SGI_INJECT;
243
244         for_each_cpu_except(cpu, cell->cpu_set, this_cpu) {
245                 virt_id = arm_cpu_phys2virt(cpu);
246
247                 if (routing_mode == 0 && !test_bit(virt_id, &targets))
248                         continue;
249                 else if (routing_mode == 1 && cpu == this_cpu)
250                         continue;
251
252                 irqchip_set_pending(per_cpu(cpu), irq, false);
253                 sgi.targets |= (1 << cpu);
254         }
255
256         /* Let the other CPUS inject their SGIs */
257         gic_send_sgi(&sgi);
258
259         return TRAP_HANDLED;
260 }
261
262 /*
263  * Handle the maintenance interrupt, the rest is injected into the cell.
264  * Return true when the IRQ has been handled by the hyp.
265  */
266 static bool arch_handle_phys_irq(struct per_cpu *cpu_data, u32 irqn)
267 {
268         if (irqn == MAINTENANCE_IRQ) {
269                 irqchip_inject_pending(cpu_data);
270                 return true;
271         }
272
273         irqchip_set_pending(cpu_data, irqn, true);
274
275         return false;
276 }
277
278 static void gic_eoi_irq(u32 irq_id, bool deactivate)
279 {
280         arm_write_sysreg(ICC_EOIR1_EL1, irq_id);
281         if (deactivate)
282                 arm_write_sysreg(ICC_DIR_EL1, irq_id);
283 }
284
285 static void gic_handle_irq(struct per_cpu *cpu_data)
286 {
287         bool handled = false;
288         u32 irq_id;
289
290         while (1) {
291                 /* Read ICC_IAR1: set 'active' state */
292                 arm_read_sysreg(ICC_IAR1_EL1, irq_id);
293
294                 if (irq_id == 0x3ff) /* Spurious IRQ */
295                         break;
296
297                 /* Handle IRQ */
298                 if (is_sgi(irq_id)) {
299                         arch_handle_sgi(cpu_data, irq_id);
300                         handled = true;
301                 } else {
302                         handled = arch_handle_phys_irq(cpu_data, irq_id);
303                 }
304
305                 /*
306                  * Write ICC_EOIR1: drop priority, but stay active if handled is
307                  * false.
308                  * This allows to not be re-interrupted by a level-triggered
309                  * interrupt that needs handling in the guest (e.g. timer)
310                  */
311                 gic_eoi_irq(irq_id, handled);
312         }
313 }
314
315 static int gic_inject_irq(struct per_cpu *cpu_data, struct pending_irq *irq)
316 {
317         int i;
318         int free_lr = -1;
319         u32 elsr;
320         u64 lr;
321
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 */
326                         if (free_lr == -1)
327                                 free_lr = i;
328                         continue;
329                 }
330
331                 /*
332                  * Entry is in use, check that it doesn't match the one we want
333                  * to inject.
334                  */
335                 lr = gic_read_lr(i);
336
337                 /*
338                  * A strict phys->virt id mapping is used for SPIs, so this test
339                  * should be sufficient.
340                  */
341                 if ((u32)lr == irq->virt_id)
342                         return -EINVAL;
343         }
344
345         if (free_lr == -1) {
346                 u32 hcr;
347                 /*
348                  * All list registers are in use, trigger a maintenance
349                  * interrupt once they are available again.
350                  */
351                 arm_read_sysreg(ICH_HCR_EL2, hcr);
352                 hcr |= ICH_HCR_UIE;
353                 arm_write_sysreg(ICH_HCR_EL2, hcr);
354
355                 return -EBUSY;
356         }
357
358         lr = irq->virt_id;
359         /* Only group 1 interrupts */
360         lr |= ICH_LR_GROUP_BIT;
361         lr |= ICH_LR_PENDING;
362         if (irq->hw) {
363                 lr |= ICH_LR_HW_BIT;
364                 lr |= (u64)irq->type.irq << ICH_LR_PHYS_ID_SHIFT;
365         } else if (irq->type.sgi.maintenance) {
366                 lr |= ICH_LR_SGI_EOI;
367         }
368
369         gic_write_lr(free_lr, lr);
370
371         return 0;
372 }
373
374 static int gic_handle_redist_access(struct per_cpu *cpu_data,
375                                     struct mmio_access *access)
376 {
377         unsigned int cpu;
378         unsigned int reg;
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;
385
386         /*
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.
390          */
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
395                                 + redist_size) {
396                         phys_redist = per_cpu(cpu)->gicr_base;
397                         break;
398                 }
399         }
400
401         if (phys_redist == NULL)
402                 return TRAP_FORBIDDEN;
403
404         reg = address - virt_redist;
405         access->addr = (unsigned long)phys_redist + reg;
406
407         /* Change the ID register, all other accesses are allowed. */
408         if (!access->is_write) {
409                 switch (reg) {
410                 case GICR_TYPER:
411                         if (virt_id == cpu_data->cell->arch.last_virt_id)
412                                 access->val = GICR_TYPER_Last;
413                         else
414                                 access->val = 0;
415                         /* AArch64 can use a writeq for this register */
416                         if (access->size == 8)
417                                 access->val |= (u64)virt_id << 32;
418
419                         ret = TRAP_HANDLED;
420                         break;
421                 case GICR_TYPER + 4:
422                         /* Upper bits contain the affinity */
423                         access->val = virt_id;
424                         ret = TRAP_HANDLED;
425                         break;
426                 }
427         }
428         if (ret == TRAP_HANDLED)
429                 return ret;
430
431         arch_mmio_access(access);
432         return TRAP_HANDLED;
433 }
434
435 static int gic_mmio_access(struct per_cpu *cpu_data,
436                            struct mmio_access *access)
437 {
438         void *address = (void *)access->addr;
439
440         if (address >= gicr_base && address < gicr_base + gicr_size)
441                 return gic_handle_redist_access(cpu_data, access);
442
443         return TRAP_UNHANDLED;
444 }
445
446 struct irqchip_ops gic_irqchip = {
447         .init = gic_init,
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,
457 };