]> rtime.felk.cvut.cz Git - jailhouse.git/blob - hypervisor/arch/arm/gic-v2.c
arm: Stop relying on little endian ordering for GICH_ELSRn scan
[jailhouse.git] / hypervisor / arch / arm / gic-v2.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 <asm/gic_common.h>
16 #include <asm/irqchip.h>
17 #include <asm/platform.h>
18 #include <asm/setup.h>
19
20 static unsigned int gic_num_lr;
21
22 extern void *gicd_base;
23 extern unsigned int gicd_size;
24 void *gicc_base;
25 unsigned int gicc_size;
26 void *gicv_base;
27 void *gich_base;
28 unsigned int gich_size;
29
30 static int gic_init(void)
31 {
32         int err;
33
34         /* FIXME: parse device tree */
35         gicc_base = GICC_BASE;
36         gicc_size = GICC_SIZE;
37         gich_base = GICH_BASE;
38         gich_size = GICH_SIZE;
39         gicv_base = GICV_BASE;
40
41         err = arch_map_device(gicc_base, gicc_base, gicc_size);
42         if (err)
43                 return err;
44
45         err = arch_map_device(gich_base, gich_base, gich_size);
46
47         return err;
48 }
49
50 static void gic_clear_pending_irqs(void)
51 {
52         unsigned int n;
53
54         /* Clear list registers. */
55         for (n = 0; n < gic_num_lr; n++)
56                 gic_write_lr(n, 0);
57
58         /* Clear active priority bits. */
59         mmio_write32(gich_base + GICH_APR, 0);
60 }
61
62 static int gic_cpu_reset(struct per_cpu *cpu_data, bool is_shutdown)
63 {
64         unsigned int i;
65         bool root_shutdown = is_shutdown && (cpu_data->cell == &root_cell);
66         u32 active;
67         u32 gich_vmcr = 0;
68         u32 gicc_ctlr, gicc_pmr;
69
70         gic_clear_pending_irqs();
71
72         /* Deactivate all PPIs */
73         active = mmio_read32(gicd_base + GICD_ISACTIVER);
74         for (i = 16; i < 32; i++) {
75                 if (test_bit(i, (unsigned long *)&active))
76                         mmio_write32(gicc_base + GICC_DIR, i);
77         }
78
79         /* Disable PPIs if necessary */
80         if (!root_shutdown)
81                 mmio_write32(gicd_base + GICD_ICENABLER, 0xffff0000);
82         /* Ensure IPIs are enabled */
83         mmio_write32(gicd_base + GICD_ISENABLER, 0x0000ffff);
84
85         if (is_shutdown)
86                 mmio_write32(gich_base + GICH_HCR, 0);
87
88         if (root_shutdown) {
89                 gich_vmcr = mmio_read32(gich_base + GICH_VMCR);
90                 gicc_ctlr = 0;
91                 gicc_pmr = (gich_vmcr >> GICH_VMCR_PMR_SHIFT) << GICV_PMR_SHIFT;
92
93                 if (gich_vmcr & GICH_VMCR_EN0)
94                         gicc_ctlr |= GICC_CTLR_GRPEN1;
95                 if (gich_vmcr & GICH_VMCR_EOImode)
96                         gicc_ctlr |= GICC_CTLR_EOImode;
97
98                 mmio_write32(gicc_base + GICC_CTLR, gicc_ctlr);
99                 mmio_write32(gicc_base + GICC_PMR, gicc_pmr);
100
101                 gich_vmcr = 0;
102         }
103         mmio_write32(gich_base + GICH_VMCR, gich_vmcr);
104
105         return 0;
106 }
107
108 static int gic_cpu_init(struct per_cpu *cpu_data)
109 {
110         u32 vtr, vmcr;
111         u32 cell_gicc_ctlr, cell_gicc_pmr;
112
113         /* Ensure all IPIs are enabled */
114         mmio_write32(gicd_base + GICD_ISENABLER, 0x0000ffff);
115
116         cell_gicc_ctlr = mmio_read32(gicc_base + GICC_CTLR);
117         cell_gicc_pmr = mmio_read32(gicc_base + GICC_PMR);
118
119         mmio_write32(gicc_base + GICC_CTLR,
120                      GICC_CTLR_GRPEN1 | GICC_CTLR_EOImode);
121         mmio_write32(gicc_base + GICC_PMR, GICC_PMR_DEFAULT);
122
123         vtr = mmio_read32(gich_base + GICH_VTR);
124         gic_num_lr = (vtr & 0x3f) + 1;
125
126         /* VMCR only contains 5 bits of priority */
127         vmcr = (cell_gicc_pmr >> GICV_PMR_SHIFT) << GICH_VMCR_PMR_SHIFT;
128         /*
129          * All virtual interrupts are group 0 in this driver since the GICV
130          * layout seen by the guest corresponds to GICC without security
131          * extensions:
132          * - A read from GICV_IAR doesn't acknowledge group 1 interrupts
133          *   (GICV_AIAR does it, but the guest never attempts to accesses it)
134          * - A write to GICV_CTLR.GRP0EN corresponds to the GICC_CTLR.GRP1EN bit
135          *   Since the guest's driver thinks that it is accessing a GIC with
136          *   security extensions, a write to GPR1EN will enable group 0
137          *   interrups.
138          * - Group 0 interrupts are presented as virtual IRQs (FIQEn = 0)
139          */
140         if (cell_gicc_ctlr & GICC_CTLR_GRPEN1)
141                 vmcr |= GICH_VMCR_EN0;
142         if (cell_gicc_ctlr & GICC_CTLR_EOImode)
143                 vmcr |= GICH_VMCR_EOImode;
144
145         mmio_write32(gich_base + GICH_VMCR, vmcr);
146         mmio_write32(gich_base + GICH_HCR, GICH_HCR_EN);
147
148         /*
149          * Clear pending virtual IRQs in case anything is left from previous
150          * use. Physically pending IRQs will be forwarded to Linux once we
151          * enable interrupts for the hypervisor.
152          */
153         gic_clear_pending_irqs();
154
155         /* Register ourselves into the CPU itf map */
156         gic_probe_cpu_id(cpu_data->cpu_id);
157
158         return 0;
159 }
160
161 static void gic_eoi_irq(u32 irq_id, bool deactivate)
162 {
163         /*
164          * The GIC doesn't seem to care about the CPUID value written to EOIR,
165          * which is rather convenient...
166          */
167         mmio_write32(gicc_base + GICC_EOIR, irq_id);
168         if (deactivate)
169                 mmio_write32(gicc_base + GICC_DIR, irq_id);
170 }
171
172 static void gic_cell_init(struct cell *cell)
173 {
174         struct jailhouse_memory gicv_region;
175
176         /*
177          * target_cpu_map has not been populated by all available CPUs when the
178          * setup code initialises the root cell. It is assumed that the kernel
179          * already has configured all its SPIs anyway, and that it will redirect
180          * them when unplugging a CPU.
181          */
182         if (cell != &root_cell)
183                 gic_target_spis(cell, cell);
184
185         gicv_region.phys_start = (unsigned long)gicv_base;
186         /*
187          * WARN: some SoCs (EXYNOS4) use a modified GIC which doesn't have any
188          * banked CPU interface, so we should map per-CPU physical addresses
189          * here.
190          * As for now, none of them seem to have virtualization extensions.
191          */
192         gicv_region.virt_start = (unsigned long)gicc_base;
193         gicv_region.size = gicc_size;
194         gicv_region.flags = JAILHOUSE_MEM_IO | JAILHOUSE_MEM_READ
195                           | JAILHOUSE_MEM_WRITE;
196
197         /*
198          * Let the guest access the virtual CPU interface instead of the
199          * physical one
200          */
201         arch_map_memory_region(cell, &gicv_region);
202 }
203
204 static void gic_cell_exit(struct cell *cell)
205 {
206         /* Reset interrupt routing of the cell's spis */
207         gic_target_spis(cell, &root_cell);
208 }
209
210 static int gic_send_sgi(struct sgi *sgi)
211 {
212         u32 val;
213
214         if (!is_sgi(sgi->id))
215                 return -EINVAL;
216
217         val = (sgi->routing_mode & 0x3) << 24
218                 | (sgi->targets & 0xff) << 16
219                 | (sgi->id & 0xf);
220
221         mmio_write32(gicd_base + GICD_SGIR, val);
222
223         return 0;
224 }
225
226 static int gic_inject_irq(struct per_cpu *cpu_data, struct pending_irq *irq)
227 {
228         int i;
229         int first_free = -1;
230         u32 lr;
231         unsigned long elsr[2];
232
233         elsr[0] = mmio_read32(gich_base + GICH_ELSR0);
234         elsr[1] = mmio_read32(gich_base + GICH_ELSR1);
235         for (i = 0; i < gic_num_lr; i++) {
236                 if (test_bit(i, elsr)) {
237                         /* Entry is available */
238                         if (first_free == -1)
239                                 first_free = i;
240                         continue;
241                 }
242
243                 /* Check that there is no overlapping */
244                 lr = gic_read_lr(i);
245                 if ((lr & GICH_LR_VIRT_ID_MASK) == irq->virt_id)
246                         return -EINVAL;
247         }
248
249         if (first_free == -1) {
250                 /* Enable maintenance IRQ */
251                 u32 hcr;
252                 hcr = mmio_read32(gich_base + GICH_HCR);
253                 hcr |= GICH_HCR_UIE;
254                 mmio_write32(gich_base + GICH_HCR, hcr);
255
256                 return -EBUSY;
257         }
258
259         /* Inject group 0 interrupt (seen as IRQ by the guest) */
260         lr = irq->virt_id;
261         lr |= GICH_LR_PENDING_BIT;
262
263         if (irq->hw) {
264                 lr |= GICH_LR_HW_BIT;
265                 lr |= irq->type.irq << GICH_LR_PHYS_ID_SHIFT;
266         } else {
267                 lr |= irq->type.sgi.cpuid << GICH_LR_CPUID_SHIFT;
268                 if (irq->type.sgi.maintenance)
269                         lr |= GICH_LR_SGI_EOI_BIT;
270         }
271
272         gic_write_lr(first_free, lr);
273
274         return 0;
275 }
276
277 static int gic_mmio_access(struct per_cpu *cpu_data,
278                            struct mmio_access *access)
279 {
280         void *address = (void *)access->addr;
281
282         if (address >= gicd_base && address < gicd_base + gicd_size)
283                 return gic_handle_dist_access(cpu_data, access);
284
285         return TRAP_UNHANDLED;
286 }
287
288 struct irqchip_ops gic_irqchip = {
289         .init = gic_init,
290         .cpu_init = gic_cpu_init,
291         .cpu_reset = gic_cpu_reset,
292         .cell_init = gic_cell_init,
293         .cell_exit = gic_cell_exit,
294
295         .send_sgi = gic_send_sgi,
296         .handle_irq = gic_handle_irq,
297         .inject_irq = gic_inject_irq,
298         .eoi_irq = gic_eoi_irq,
299         .mmio_access = gic_mmio_access,
300 };