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