]> rtime.felk.cvut.cz Git - jailhouse.git/blob - hypervisor/arch/arm/gic-common.c
00da4cb2dda592b4a2a97e5b6ae8e8d8f8a783f5
[jailhouse.git] / hypervisor / arch / arm / gic-common.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/cell.h>
14 #include <jailhouse/control.h>
15 #include <jailhouse/mmio.h>
16 #include <jailhouse/printk.h>
17 #include <asm/control.h>
18 #include <asm/gic_common.h>
19 #include <asm/irqchip.h>
20 #include <asm/percpu.h>
21 #include <asm/platform.h>
22 #include <asm/spinlock.h>
23 #include <asm/traps.h>
24
25 #define REG_RANGE(base, n, size)                \
26                 (base) ... ((base) + (n - 1) * (size))
27
28 extern void *gicd_base;
29 extern unsigned int gicd_size;
30
31 static DEFINE_SPINLOCK(dist_lock);
32
33 /* The GIC interface numbering does not necessarily match the logical map */
34 static u8 target_cpu_map[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
35
36 /*
37  * Most of the GIC distributor writes only reconfigure the IRQs corresponding to
38  * the bits of the written value, by using separate `set' and `clear' registers.
39  * Such registers can be handled by setting the `is_poke' boolean, which allows
40  * to simply restrict the mmio->value with the cell configuration mask.
41  * Others, such as the priority registers, will need to be read and written back
42  * with a restricted value, by using the distributor lock.
43  */
44 static enum mmio_result
45 restrict_bitmask_access(struct mmio_access *mmio, unsigned int reg_index,
46                         unsigned int bits_per_irq, bool is_poke)
47 {
48         struct cell *cell = this_cell();
49         unsigned int spi;
50         unsigned long access_mask = 0;
51         /*
52          * In order to avoid division, the number of bits per irq is limited
53          * to powers of 2 for the moment.
54          */
55         unsigned long irqs_per_reg = 32 >> ffsl(bits_per_irq);
56         unsigned long spi_bits = (1 << bits_per_irq) - 1;
57         /* First, extract the first interrupt affected by this access */
58         unsigned int first_irq = reg_index * irqs_per_reg;
59
60         /* For SGIs or PPIs, let the caller do the mmio access */
61         if (!is_spi(first_irq)) {
62                 arm_mmio_perform_access((unsigned long)gicd_base, mmio);
63                 return MMIO_HANDLED;
64         }
65
66         /* For SPIs, compare against the cell config mask */
67         first_irq -= 32;
68         for (spi = first_irq; spi < first_irq + irqs_per_reg; spi++) {
69                 unsigned int bit_nr = (spi - first_irq) * bits_per_irq;
70                 if (spi_in_cell(cell, spi))
71                         access_mask |= spi_bits << bit_nr;
72         }
73
74         if (!mmio->is_write) {
75                 /* Restrict the read value */
76                 arm_mmio_perform_access((unsigned long)gicd_base, mmio);
77                 mmio->value &= access_mask;
78                 return MMIO_HANDLED;
79         }
80
81         if (!is_poke) {
82                 /*
83                  * Modify the existing value of this register by first reading
84                  * it into mmio->value
85                  * Relies on a spinlock since we need two mmio accesses.
86                  */
87                 unsigned long access_val = mmio->value;
88
89                 spin_lock(&dist_lock);
90
91                 mmio->is_write = false;
92                 arm_mmio_perform_access((unsigned long)gicd_base, mmio);
93                 mmio->is_write = true;
94
95                 /* Clear 0 bits */
96                 mmio->value &= ~(access_mask & ~access_val);
97                 mmio->value |= access_val;
98                 arm_mmio_perform_access((unsigned long)gicd_base, mmio);
99
100                 spin_unlock(&dist_lock);
101         } else {
102                 mmio->value &= access_mask;
103                 arm_mmio_perform_access((unsigned long)gicd_base, mmio);
104         }
105         return MMIO_HANDLED;
106 }
107
108 /*
109  * GICv3 uses a 64bit register IROUTER for each IRQ
110  */
111 static enum mmio_result handle_irq_route(struct mmio_access *mmio,
112                                          unsigned int irq)
113 {
114         struct cell *cell = this_cell();
115         unsigned int cpu;
116
117         /* Ignore aff3 on AArch32 (return 0) */
118         if (mmio->size == 4 && (mmio->address % 8))
119                 return MMIO_HANDLED;
120
121         /* SGIs and PPIs are res0 */
122         if (!is_spi(irq))
123                 return MMIO_HANDLED;
124
125         /*
126          * Ignore accesses to SPIs that do not belong to the cell. This isn't
127          * forbidden, because the guest driver may simply iterate over all
128          * registers at initialisation
129          */
130         if (!spi_in_cell(cell, irq - 32))
131                 return MMIO_HANDLED;
132
133         /* Translate the virtual cpu id into the physical one */
134         if (mmio->is_write) {
135                 mmio->value = arm_cpu_virt2phys(cell, mmio->value);
136                 if (mmio->value == -1) {
137                         printk("Attempt to route IRQ%d outside of cell\n", irq);
138                         return MMIO_ERROR;
139                 }
140                 arm_mmio_perform_access((unsigned long)gicd_base, mmio);
141         } else {
142                 cpu = mmio_read32(gicd_base + GICD_IROUTER + 8 * irq);
143                 mmio->value = arm_cpu_phys2virt(cpu);
144         }
145         return MMIO_HANDLED;
146 }
147
148 /*
149  * GICv2 uses 8bit values for each IRQ in the ITARGETRs registers
150  */
151 static enum mmio_result handle_irq_target(struct mmio_access *mmio,
152                                           unsigned int reg)
153 {
154         /*
155          * ITARGETSR contain one byte per IRQ, so the first one affected by this
156          * access corresponds to the reg index
157          */
158         struct cell *cell = this_cell();
159         unsigned int i, cpu;
160         unsigned int spi = reg - 32;
161         unsigned int offset;
162         u32 access_mask = 0;
163         u8 targets;
164
165         /*
166          * Let the guest freely access its SGIs and PPIs, which may be used to
167          * fill its CPU interface map.
168          */
169         if (!is_spi(reg)) {
170                 arm_mmio_perform_access((unsigned long)gicd_base, mmio);
171                 return MMIO_HANDLED;
172         }
173
174         /*
175          * The registers are byte-accessible, extend the access to a word if
176          * necessary.
177          */
178         offset = spi % 4;
179         mmio->value <<= 8 * offset;
180         mmio->size = 4;
181         spi -= offset;
182
183         for (i = 0; i < 4; i++, spi++) {
184                 if (spi_in_cell(cell, spi))
185                         access_mask |= 0xff << (8 * i);
186                 else
187                         continue;
188
189                 if (!mmio->is_write)
190                         continue;
191
192                 targets = (mmio->value >> (8 * i)) & 0xff;
193
194                 /* Check that the targeted interface belongs to the cell */
195                 for (cpu = 0; cpu < 8; cpu++) {
196                         if (!(targets & target_cpu_map[cpu]))
197                                 continue;
198
199                         if (per_cpu(cpu)->cell == cell)
200                                 continue;
201
202                         printk("Attempt to route SPI%d outside of cell\n", spi);
203                         return MMIO_ERROR;
204                 }
205         }
206
207         if (mmio->is_write) {
208                 spin_lock(&dist_lock);
209                 u32 itargetsr =
210                         mmio_read32(gicd_base + GICD_ITARGETSR + reg + offset);
211                 mmio->value &= access_mask;
212                 /* Combine with external SPIs */
213                 mmio->value |= (itargetsr & ~access_mask);
214                 /* And do the access */
215                 arm_mmio_perform_access((unsigned long)gicd_base, mmio);
216                 spin_unlock(&dist_lock);
217         } else {
218                 arm_mmio_perform_access((unsigned long)gicd_base, mmio);
219                 mmio->value &= access_mask;
220         }
221
222         return MMIO_HANDLED;
223 }
224
225 static enum mmio_result handle_sgir_access(struct mmio_access *mmio)
226 {
227         struct sgi sgi;
228         unsigned long val = mmio->value;
229
230         if (!mmio->is_write)
231                 return MMIO_HANDLED;
232
233         sgi.targets = (val >> 16) & 0xff;
234         sgi.routing_mode = (val >> 24) & 0x3;
235         sgi.aff1 = 0;
236         sgi.aff2 = 0;
237         sgi.aff3 = 0;
238         sgi.id = val & 0xf;
239
240         gic_handle_sgir_write(&sgi, false);
241         return MMIO_HANDLED;
242 }
243
244 /*
245  * Get the CPU interface ID for this cpu. It can be discovered by reading
246  * the banked value of the PPI and IPI TARGET registers
247  * Patch 2bb3135 in Linux explains why the probe may need to scans the first 8
248  * registers: some early implementation returned 0 for the first ITARGETSR
249  * registers.
250  * Since those didn't have virtualization extensions, we can safely ignore that
251  * case.
252  */
253 int gic_probe_cpu_id(unsigned int cpu)
254 {
255         if (cpu >= ARRAY_SIZE(target_cpu_map))
256                 return -EINVAL;
257
258         target_cpu_map[cpu] = mmio_read32(gicd_base + GICD_ITARGETSR);
259
260         if (target_cpu_map[cpu] == 0)
261                 return -ENODEV;
262
263         return 0;
264 }
265
266 void gic_handle_sgir_write(struct sgi *sgi, bool virt_input)
267 {
268         struct per_cpu *cpu_data = this_cpu_data();
269         unsigned int cpu;
270         unsigned long targets;
271         unsigned int this_cpu = cpu_data->cpu_id;
272         struct cell *cell = cpu_data->cell;
273         bool is_target = false;
274
275         cpu_data->stats[JAILHOUSE_CPU_STAT_VMEXITS_VSGI]++;
276
277         targets = sgi->targets;
278         sgi->targets = 0;
279
280         /* Filter the targets */
281         for_each_cpu_except(cpu, cell->cpu_set, this_cpu) {
282                 /*
283                  * When using a cpu map to target the different CPUs (GICv2),
284                  * they are independent from the physical CPU IDs, so there is
285                  * no need to translate them to the hypervisor's virtual IDs.
286                  */
287                 if (virt_input)
288                         is_target = !!test_bit(arm_cpu_phys2virt(cpu),
289                                                &targets);
290                 else
291                         is_target = !!(targets & target_cpu_map[cpu]);
292
293                 if (sgi->routing_mode == 0 && !is_target)
294                         continue;
295
296                 irqchip_set_pending(per_cpu(cpu), sgi->id, false);
297                 sgi->targets |= (1 << cpu);
298         }
299
300         /* Let the other CPUS inject their SGIs */
301         sgi->id = SGI_INJECT;
302         irqchip_send_sgi(sgi);
303 }
304
305 enum mmio_result gic_handle_dist_access(void *arg, struct mmio_access *mmio)
306 {
307         unsigned long reg = mmio->address;
308         enum mmio_result ret;
309
310         switch (reg) {
311         case REG_RANGE(GICD_IROUTER, 1024, 8):
312                 ret = handle_irq_route(mmio, (reg - GICD_IROUTER) / 8);
313                 break;
314
315         case REG_RANGE(GICD_ITARGETSR, 1024, 1):
316                 ret = handle_irq_target(mmio, reg - GICD_ITARGETSR);
317                 break;
318
319         case REG_RANGE(GICD_ICENABLER, 32, 4):
320         case REG_RANGE(GICD_ISENABLER, 32, 4):
321         case REG_RANGE(GICD_ICPENDR, 32, 4):
322         case REG_RANGE(GICD_ISPENDR, 32, 4):
323         case REG_RANGE(GICD_ICACTIVER, 32, 4):
324         case REG_RANGE(GICD_ISACTIVER, 32, 4):
325                 ret = restrict_bitmask_access(mmio, (reg & 0x7f) / 4, 1, true);
326                 break;
327
328         case REG_RANGE(GICD_IGROUPR, 32, 4):
329                 ret = restrict_bitmask_access(mmio, (reg & 0x7f) / 4, 1, false);
330                 break;
331
332         case REG_RANGE(GICD_ICFGR, 64, 4):
333                 ret = restrict_bitmask_access(mmio, (reg & 0xff) / 4, 2, false);
334                 break;
335
336         case REG_RANGE(GICD_IPRIORITYR, 255, 4):
337                 ret = restrict_bitmask_access(mmio, (reg & 0x3ff) / 4, 8,
338                                               false);
339                 break;
340
341         case GICD_SGIR:
342                 ret = handle_sgir_access(mmio);
343                 break;
344
345         case GICD_CTLR:
346         case GICD_TYPER:
347         case GICD_IIDR:
348         case REG_RANGE(GICD_PIDR0, 4, 4):
349         case REG_RANGE(GICD_PIDR4, 4, 4):
350         case REG_RANGE(GICD_CIDR0, 4, 4):
351                 /* Allow read access, ignore write */
352                 if (!mmio->is_write)
353                         arm_mmio_perform_access((unsigned long)gicd_base, mmio);
354                 /* fall through */
355         default:
356                 /* Ignore access. */
357                 ret = MMIO_HANDLED;
358         }
359
360         return ret;
361 }
362
363 void gic_handle_irq(struct per_cpu *cpu_data)
364 {
365         bool handled = false;
366         u32 irq_id;
367
368         while (1) {
369                 /* Read IAR1: set 'active' state */
370                 irq_id = gic_read_iar();
371
372                 if (irq_id == 0x3ff) /* Spurious IRQ */
373                         break;
374
375                 /* Handle IRQ */
376                 if (is_sgi(irq_id)) {
377                         arch_handle_sgi(cpu_data, irq_id);
378                         handled = true;
379                 } else {
380                         handled = arch_handle_phys_irq(cpu_data, irq_id);
381                 }
382
383                 /*
384                  * Write EOIR1: drop priority, but stay active if handled is
385                  * false.
386                  * This allows to not be re-interrupted by a level-triggered
387                  * interrupt that needs handling in the guest (e.g. timer)
388                  */
389                 irqchip_eoi_irq(irq_id, handled);
390         }
391 }
392
393 void gic_target_spis(struct cell *config_cell, struct cell *dest_cell)
394 {
395         unsigned int i, first_cpu, cpu_itf;
396         unsigned int shift = 0;
397         void *itargetsr = gicd_base + GICD_ITARGETSR;
398         u32 targets;
399         u32 mask = 0;
400         u32 bits = 0;
401
402         /* Always route to the first logical CPU on reset */
403         for_each_cpu(first_cpu, dest_cell->cpu_set)
404                 break;
405
406         cpu_itf = target_cpu_map[first_cpu];
407
408         /* ITARGETSR0-7 contain the PPIs and SGIs, and are read-only. */
409         itargetsr += 4 * 8;
410
411         for (i = 0; i < 64; i++, shift = (shift + 8) % 32) {
412                 if (spi_in_cell(config_cell, i)) {
413                         mask |= (0xff << shift);
414                         bits |= (cpu_itf << shift);
415                 }
416
417                 /* ITARGETRs have 4 IRQ per register */
418                 if ((i + 1) % 4 == 0) {
419                         targets = mmio_read32(itargetsr);
420                         targets &= ~mask;
421                         targets |= bits;
422                         mmio_write32(itargetsr, targets);
423                         itargetsr += 4;
424                         mask = 0;
425                         bits = 0;
426                 }
427         }
428 }