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