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