Commit | Line | Data |
---|---|---|
59c5ab40 AP |
1 | /* |
2 | * GICv3 ITS emulation | |
3 | * | |
4 | * Copyright (C) 2015,2016 ARM Ltd. | |
5 | * Author: Andre Przywara <andre.przywara@arm.com> | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License version 2 as | |
9 | * published by the Free Software Foundation. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
20 | #include <linux/cpu.h> | |
21 | #include <linux/kvm.h> | |
22 | #include <linux/kvm_host.h> | |
23 | #include <linux/interrupt.h> | |
424c3383 | 24 | #include <linux/list.h> |
1085fdc6 | 25 | #include <linux/uaccess.h> |
59c5ab40 AP |
26 | |
27 | #include <linux/irqchip/arm-gic-v3.h> | |
28 | ||
29 | #include <asm/kvm_emulate.h> | |
30 | #include <asm/kvm_arm.h> | |
31 | #include <asm/kvm_mmu.h> | |
32 | ||
33 | #include "vgic.h" | |
34 | #include "vgic-mmio.h" | |
35 | ||
df9f58fb AP |
36 | /* |
37 | * Creates a new (reference to a) struct vgic_irq for a given LPI. | |
38 | * If this LPI is already mapped on another ITS, we increase its refcount | |
39 | * and return a pointer to the existing structure. | |
40 | * If this is a "new" LPI, we allocate and initialize a new struct vgic_irq. | |
41 | * This function returns a pointer to the _unlocked_ structure. | |
42 | */ | |
43 | static struct vgic_irq *vgic_add_lpi(struct kvm *kvm, u32 intid) | |
44 | { | |
45 | struct vgic_dist *dist = &kvm->arch.vgic; | |
46 | struct vgic_irq *irq = vgic_get_irq(kvm, NULL, intid), *oldirq; | |
47 | ||
48 | /* In this case there is no put, since we keep the reference. */ | |
49 | if (irq) | |
50 | return irq; | |
51 | ||
52 | irq = kzalloc(sizeof(struct vgic_irq), GFP_KERNEL); | |
53 | if (!irq) | |
54 | return NULL; | |
55 | ||
56 | INIT_LIST_HEAD(&irq->lpi_list); | |
57 | INIT_LIST_HEAD(&irq->ap_list); | |
58 | spin_lock_init(&irq->irq_lock); | |
59 | ||
60 | irq->config = VGIC_CONFIG_EDGE; | |
61 | kref_init(&irq->refcount); | |
62 | irq->intid = intid; | |
63 | ||
64 | spin_lock(&dist->lpi_list_lock); | |
65 | ||
66 | /* | |
67 | * There could be a race with another vgic_add_lpi(), so we need to | |
68 | * check that we don't add a second list entry with the same LPI. | |
69 | */ | |
70 | list_for_each_entry(oldirq, &dist->lpi_list_head, lpi_list) { | |
71 | if (oldirq->intid != intid) | |
72 | continue; | |
73 | ||
74 | /* Someone was faster with adding this LPI, lets use that. */ | |
75 | kfree(irq); | |
76 | irq = oldirq; | |
77 | ||
78 | /* | |
79 | * This increases the refcount, the caller is expected to | |
80 | * call vgic_put_irq() on the returned pointer once it's | |
81 | * finished with the IRQ. | |
82 | */ | |
d97594e6 | 83 | vgic_get_irq_kref(irq); |
df9f58fb AP |
84 | |
85 | goto out_unlock; | |
86 | } | |
87 | ||
88 | list_add_tail(&irq->lpi_list, &dist->lpi_list_head); | |
89 | dist->lpi_list_count++; | |
90 | ||
91 | out_unlock: | |
92 | spin_unlock(&dist->lpi_list_lock); | |
93 | ||
94 | return irq; | |
95 | } | |
96 | ||
424c3383 AP |
97 | struct its_device { |
98 | struct list_head dev_list; | |
99 | ||
100 | /* the head for the list of ITTEs */ | |
101 | struct list_head itt_head; | |
102 | u32 device_id; | |
103 | }; | |
104 | ||
105 | #define COLLECTION_NOT_MAPPED ((u32)~0) | |
106 | ||
107 | struct its_collection { | |
108 | struct list_head coll_list; | |
109 | ||
110 | u32 collection_id; | |
111 | u32 target_addr; | |
112 | }; | |
113 | ||
114 | #define its_is_collection_mapped(coll) ((coll) && \ | |
115 | ((coll)->target_addr != COLLECTION_NOT_MAPPED)) | |
116 | ||
117 | struct its_itte { | |
118 | struct list_head itte_list; | |
119 | ||
3802411d | 120 | struct vgic_irq *irq; |
424c3383 AP |
121 | struct its_collection *collection; |
122 | u32 lpi; | |
123 | u32 event_id; | |
124 | }; | |
125 | ||
df9f58fb AP |
126 | /* |
127 | * Find and returns a device in the device table for an ITS. | |
128 | * Must be called with the its_lock mutex held. | |
129 | */ | |
130 | static struct its_device *find_its_device(struct vgic_its *its, u32 device_id) | |
131 | { | |
132 | struct its_device *device; | |
133 | ||
134 | list_for_each_entry(device, &its->device_list, dev_list) | |
135 | if (device_id == device->device_id) | |
136 | return device; | |
137 | ||
138 | return NULL; | |
139 | } | |
140 | ||
141 | /* | |
142 | * Find and returns an interrupt translation table entry (ITTE) for a given | |
143 | * Device ID/Event ID pair on an ITS. | |
144 | * Must be called with the its_lock mutex held. | |
145 | */ | |
146 | static struct its_itte *find_itte(struct vgic_its *its, u32 device_id, | |
147 | u32 event_id) | |
148 | { | |
149 | struct its_device *device; | |
150 | struct its_itte *itte; | |
151 | ||
152 | device = find_its_device(its, device_id); | |
153 | if (device == NULL) | |
154 | return NULL; | |
155 | ||
156 | list_for_each_entry(itte, &device->itt_head, itte_list) | |
157 | if (itte->event_id == event_id) | |
158 | return itte; | |
159 | ||
160 | return NULL; | |
161 | } | |
162 | ||
163 | /* To be used as an iterator this macro misses the enclosing parentheses */ | |
164 | #define for_each_lpi_its(dev, itte, its) \ | |
165 | list_for_each_entry(dev, &(its)->device_list, dev_list) \ | |
166 | list_for_each_entry(itte, &(dev)->itt_head, itte_list) | |
167 | ||
424c3383 AP |
168 | /* |
169 | * We only implement 48 bits of PA at the moment, although the ITS | |
170 | * supports more. Let's be restrictive here. | |
171 | */ | |
df9f58fb | 172 | #define BASER_ADDRESS(x) ((x) & GENMASK_ULL(47, 16)) |
424c3383 | 173 | #define CBASER_ADDRESS(x) ((x) & GENMASK_ULL(47, 12)) |
33d3bc95 | 174 | #define PENDBASER_ADDRESS(x) ((x) & GENMASK_ULL(47, 16)) |
f9f77af9 AP |
175 | #define PROPBASER_ADDRESS(x) ((x) & GENMASK_ULL(47, 12)) |
176 | ||
177 | #define GIC_LPI_OFFSET 8192 | |
178 | ||
df9f58fb AP |
179 | /* |
180 | * Finds and returns a collection in the ITS collection table. | |
181 | * Must be called with the its_lock mutex held. | |
182 | */ | |
183 | static struct its_collection *find_collection(struct vgic_its *its, int coll_id) | |
184 | { | |
185 | struct its_collection *collection; | |
186 | ||
187 | list_for_each_entry(collection, &its->collection_list, coll_list) { | |
188 | if (coll_id == collection->collection_id) | |
189 | return collection; | |
190 | } | |
191 | ||
192 | return NULL; | |
193 | } | |
194 | ||
f9f77af9 AP |
195 | #define LPI_PROP_ENABLE_BIT(p) ((p) & LPI_PROP_ENABLED) |
196 | #define LPI_PROP_PRIORITY(p) ((p) & 0xfc) | |
197 | ||
198 | /* | |
199 | * Reads the configuration data for a given LPI from guest memory and | |
200 | * updates the fields in struct vgic_irq. | |
201 | * If filter_vcpu is not NULL, applies only if the IRQ is targeting this | |
202 | * VCPU. Unconditionally applies if filter_vcpu is NULL. | |
203 | */ | |
204 | static int update_lpi_config(struct kvm *kvm, struct vgic_irq *irq, | |
205 | struct kvm_vcpu *filter_vcpu) | |
206 | { | |
207 | u64 propbase = PROPBASER_ADDRESS(kvm->arch.vgic.propbaser); | |
208 | u8 prop; | |
209 | int ret; | |
210 | ||
211 | ret = kvm_read_guest(kvm, propbase + irq->intid - GIC_LPI_OFFSET, | |
212 | &prop, 1); | |
213 | ||
214 | if (ret) | |
215 | return ret; | |
216 | ||
217 | spin_lock(&irq->irq_lock); | |
218 | ||
219 | if (!filter_vcpu || filter_vcpu == irq->target_vcpu) { | |
220 | irq->priority = LPI_PROP_PRIORITY(prop); | |
221 | irq->enabled = LPI_PROP_ENABLE_BIT(prop); | |
222 | ||
223 | vgic_queue_irq_unlock(kvm, irq); | |
224 | } else { | |
225 | spin_unlock(&irq->irq_lock); | |
226 | } | |
227 | ||
228 | return 0; | |
229 | } | |
33d3bc95 AP |
230 | |
231 | /* | |
232 | * Create a snapshot of the current LPI list, so that we can enumerate all | |
233 | * LPIs without holding any lock. | |
234 | * Returns the array length and puts the kmalloc'ed array into intid_ptr. | |
235 | */ | |
236 | static int vgic_copy_lpi_list(struct kvm *kvm, u32 **intid_ptr) | |
237 | { | |
238 | struct vgic_dist *dist = &kvm->arch.vgic; | |
239 | struct vgic_irq *irq; | |
240 | u32 *intids; | |
241 | int irq_count = dist->lpi_list_count, i = 0; | |
242 | ||
243 | /* | |
244 | * We use the current value of the list length, which may change | |
245 | * after the kmalloc. We don't care, because the guest shouldn't | |
246 | * change anything while the command handling is still running, | |
247 | * and in the worst case we would miss a new IRQ, which one wouldn't | |
248 | * expect to be covered by this command anyway. | |
249 | */ | |
250 | intids = kmalloc_array(irq_count, sizeof(intids[0]), GFP_KERNEL); | |
251 | if (!intids) | |
252 | return -ENOMEM; | |
253 | ||
254 | spin_lock(&dist->lpi_list_lock); | |
255 | list_for_each_entry(irq, &dist->lpi_list_head, lpi_list) { | |
256 | /* We don't need to "get" the IRQ, as we hold the list lock. */ | |
257 | intids[i] = irq->intid; | |
258 | if (++i == irq_count) | |
259 | break; | |
260 | } | |
261 | spin_unlock(&dist->lpi_list_lock); | |
262 | ||
263 | *intid_ptr = intids; | |
264 | return irq_count; | |
265 | } | |
266 | ||
df9f58fb AP |
267 | /* |
268 | * Promotes the ITS view of affinity of an ITTE (which redistributor this LPI | |
269 | * is targeting) to the VGIC's view, which deals with target VCPUs. | |
270 | * Needs to be called whenever either the collection for a LPIs has | |
271 | * changed or the collection itself got retargeted. | |
272 | */ | |
273 | static void update_affinity_itte(struct kvm *kvm, struct its_itte *itte) | |
274 | { | |
275 | struct kvm_vcpu *vcpu; | |
276 | ||
277 | if (!its_is_collection_mapped(itte->collection)) | |
278 | return; | |
279 | ||
280 | vcpu = kvm_get_vcpu(kvm, itte->collection->target_addr); | |
281 | ||
282 | spin_lock(&itte->irq->irq_lock); | |
283 | itte->irq->target_vcpu = vcpu; | |
284 | spin_unlock(&itte->irq->irq_lock); | |
285 | } | |
286 | ||
287 | /* | |
288 | * Updates the target VCPU for every LPI targeting this collection. | |
289 | * Must be called with the its_lock mutex held. | |
290 | */ | |
291 | static void update_affinity_collection(struct kvm *kvm, struct vgic_its *its, | |
292 | struct its_collection *coll) | |
293 | { | |
294 | struct its_device *device; | |
295 | struct its_itte *itte; | |
296 | ||
297 | for_each_lpi_its(device, itte, its) { | |
298 | if (!itte->collection || coll != itte->collection) | |
299 | continue; | |
300 | ||
301 | update_affinity_itte(kvm, itte); | |
302 | } | |
303 | } | |
304 | ||
305 | static u32 max_lpis_propbaser(u64 propbaser) | |
306 | { | |
307 | int nr_idbits = (propbaser & 0x1f) + 1; | |
308 | ||
309 | return 1U << min(nr_idbits, INTERRUPT_ID_BITS_ITS); | |
310 | } | |
311 | ||
33d3bc95 AP |
312 | /* |
313 | * Scan the whole LPI pending table and sync the pending bit in there | |
314 | * with our own data structures. This relies on the LPI being | |
315 | * mapped before. | |
316 | */ | |
317 | static int its_sync_lpi_pending_table(struct kvm_vcpu *vcpu) | |
318 | { | |
319 | gpa_t pendbase = PENDBASER_ADDRESS(vcpu->arch.vgic_cpu.pendbaser); | |
320 | struct vgic_irq *irq; | |
321 | int last_byte_offset = -1; | |
322 | int ret = 0; | |
323 | u32 *intids; | |
324 | int nr_irqs, i; | |
325 | ||
326 | nr_irqs = vgic_copy_lpi_list(vcpu->kvm, &intids); | |
327 | if (nr_irqs < 0) | |
328 | return nr_irqs; | |
329 | ||
330 | for (i = 0; i < nr_irqs; i++) { | |
331 | int byte_offset, bit_nr; | |
332 | u8 pendmask; | |
333 | ||
334 | byte_offset = intids[i] / BITS_PER_BYTE; | |
335 | bit_nr = intids[i] % BITS_PER_BYTE; | |
336 | ||
337 | /* | |
338 | * For contiguously allocated LPIs chances are we just read | |
339 | * this very same byte in the last iteration. Reuse that. | |
340 | */ | |
341 | if (byte_offset != last_byte_offset) { | |
342 | ret = kvm_read_guest(vcpu->kvm, pendbase + byte_offset, | |
343 | &pendmask, 1); | |
344 | if (ret) { | |
345 | kfree(intids); | |
346 | return ret; | |
347 | } | |
348 | last_byte_offset = byte_offset; | |
349 | } | |
350 | ||
351 | irq = vgic_get_irq(vcpu->kvm, NULL, intids[i]); | |
352 | spin_lock(&irq->irq_lock); | |
353 | irq->pending = pendmask & (1U << bit_nr); | |
354 | vgic_queue_irq_unlock(vcpu->kvm, irq); | |
355 | vgic_put_irq(vcpu->kvm, irq); | |
356 | } | |
357 | ||
358 | kfree(intids); | |
359 | ||
360 | return ret; | |
361 | } | |
424c3383 AP |
362 | |
363 | static unsigned long vgic_mmio_read_its_ctlr(struct kvm *vcpu, | |
364 | struct vgic_its *its, | |
365 | gpa_t addr, unsigned int len) | |
366 | { | |
367 | u32 reg = 0; | |
368 | ||
369 | mutex_lock(&its->cmd_lock); | |
370 | if (its->creadr == its->cwriter) | |
371 | reg |= GITS_CTLR_QUIESCENT; | |
372 | if (its->enabled) | |
373 | reg |= GITS_CTLR_ENABLE; | |
374 | mutex_unlock(&its->cmd_lock); | |
375 | ||
376 | return reg; | |
377 | } | |
378 | ||
379 | static void vgic_mmio_write_its_ctlr(struct kvm *kvm, struct vgic_its *its, | |
380 | gpa_t addr, unsigned int len, | |
381 | unsigned long val) | |
382 | { | |
383 | its->enabled = !!(val & GITS_CTLR_ENABLE); | |
384 | } | |
385 | ||
386 | static unsigned long vgic_mmio_read_its_typer(struct kvm *kvm, | |
387 | struct vgic_its *its, | |
388 | gpa_t addr, unsigned int len) | |
389 | { | |
390 | u64 reg = GITS_TYPER_PLPIS; | |
391 | ||
392 | /* | |
393 | * We use linear CPU numbers for redistributor addressing, | |
394 | * so GITS_TYPER.PTA is 0. | |
395 | * Also we force all PROPBASER registers to be the same, so | |
396 | * CommonLPIAff is 0 as well. | |
397 | * To avoid memory waste in the guest, we keep the number of IDBits and | |
398 | * DevBits low - as least for the time being. | |
399 | */ | |
400 | reg |= 0x0f << GITS_TYPER_DEVBITS_SHIFT; | |
401 | reg |= 0x0f << GITS_TYPER_IDBITS_SHIFT; | |
402 | ||
403 | return extract_bytes(reg, addr & 7, len); | |
404 | } | |
405 | ||
406 | static unsigned long vgic_mmio_read_its_iidr(struct kvm *kvm, | |
407 | struct vgic_its *its, | |
408 | gpa_t addr, unsigned int len) | |
409 | { | |
410 | return (PRODUCT_ID_KVM << 24) | (IMPLEMENTER_ARM << 0); | |
411 | } | |
412 | ||
413 | static unsigned long vgic_mmio_read_its_idregs(struct kvm *kvm, | |
414 | struct vgic_its *its, | |
415 | gpa_t addr, unsigned int len) | |
416 | { | |
417 | switch (addr & 0xffff) { | |
418 | case GITS_PIDR0: | |
419 | return 0x92; /* part number, bits[7:0] */ | |
420 | case GITS_PIDR1: | |
421 | return 0xb4; /* part number, bits[11:8] */ | |
422 | case GITS_PIDR2: | |
423 | return GIC_PIDR2_ARCH_GICv3 | 0x0b; | |
424 | case GITS_PIDR4: | |
425 | return 0x40; /* This is a 64K software visible page */ | |
426 | /* The following are the ID registers for (any) GIC. */ | |
427 | case GITS_CIDR0: | |
428 | return 0x0d; | |
429 | case GITS_CIDR1: | |
430 | return 0xf0; | |
431 | case GITS_CIDR2: | |
432 | return 0x05; | |
433 | case GITS_CIDR3: | |
434 | return 0xb1; | |
435 | } | |
436 | ||
437 | return 0; | |
438 | } | |
439 | ||
2891a7df AP |
440 | /* |
441 | * Find the target VCPU and the LPI number for a given devid/eventid pair | |
442 | * and make this IRQ pending, possibly injecting it. | |
443 | * Must be called with the its_lock mutex held. | |
444 | */ | |
445 | static void vgic_its_trigger_msi(struct kvm *kvm, struct vgic_its *its, | |
446 | u32 devid, u32 eventid) | |
447 | { | |
448 | struct its_itte *itte; | |
449 | ||
450 | if (!its->enabled) | |
451 | return; | |
452 | ||
453 | itte = find_itte(its, devid, eventid); | |
454 | /* Triggering an unmapped IRQ gets silently dropped. */ | |
455 | if (itte && its_is_collection_mapped(itte->collection)) { | |
456 | struct kvm_vcpu *vcpu; | |
457 | ||
458 | vcpu = kvm_get_vcpu(kvm, itte->collection->target_addr); | |
459 | if (vcpu && vcpu->arch.vgic_cpu.lpis_enabled) { | |
460 | spin_lock(&itte->irq->irq_lock); | |
461 | itte->irq->pending = true; | |
462 | vgic_queue_irq_unlock(kvm, itte->irq); | |
463 | } | |
464 | } | |
465 | } | |
466 | ||
467 | /* | |
468 | * Queries the KVM IO bus framework to get the ITS pointer from the given | |
469 | * doorbell address. | |
470 | * We then call vgic_its_trigger_msi() with the decoded data. | |
471 | */ | |
472 | int vgic_its_inject_msi(struct kvm *kvm, struct kvm_msi *msi) | |
473 | { | |
474 | u64 address; | |
475 | struct kvm_io_device *kvm_io_dev; | |
476 | struct vgic_io_device *iodev; | |
477 | ||
478 | if (!vgic_has_its(kvm)) | |
479 | return -ENODEV; | |
480 | ||
481 | if (!(msi->flags & KVM_MSI_VALID_DEVID)) | |
482 | return -EINVAL; | |
483 | ||
484 | address = (u64)msi->address_hi << 32 | msi->address_lo; | |
485 | ||
486 | kvm_io_dev = kvm_io_bus_get_dev(kvm, KVM_MMIO_BUS, address); | |
487 | if (!kvm_io_dev) | |
488 | return -ENODEV; | |
489 | ||
490 | iodev = container_of(kvm_io_dev, struct vgic_io_device, dev); | |
491 | ||
492 | mutex_lock(&iodev->its->its_lock); | |
493 | vgic_its_trigger_msi(kvm, iodev->its, msi->devid, msi->data); | |
494 | mutex_unlock(&iodev->its->its_lock); | |
495 | ||
496 | return 0; | |
497 | } | |
498 | ||
424c3383 AP |
499 | /* Requires the its_lock to be held. */ |
500 | static void its_free_itte(struct kvm *kvm, struct its_itte *itte) | |
501 | { | |
502 | list_del(&itte->itte_list); | |
3802411d AP |
503 | |
504 | /* This put matches the get in vgic_add_lpi. */ | |
505 | vgic_put_irq(kvm, itte->irq); | |
506 | ||
424c3383 AP |
507 | kfree(itte); |
508 | } | |
509 | ||
df9f58fb AP |
510 | static u64 its_cmd_mask_field(u64 *its_cmd, int word, int shift, int size) |
511 | { | |
512 | return (le64_to_cpu(its_cmd[word]) >> shift) & (BIT_ULL(size) - 1); | |
513 | } | |
514 | ||
515 | #define its_cmd_get_command(cmd) its_cmd_mask_field(cmd, 0, 0, 8) | |
516 | #define its_cmd_get_deviceid(cmd) its_cmd_mask_field(cmd, 0, 32, 32) | |
517 | #define its_cmd_get_id(cmd) its_cmd_mask_field(cmd, 1, 0, 32) | |
518 | #define its_cmd_get_physical_id(cmd) its_cmd_mask_field(cmd, 1, 32, 32) | |
519 | #define its_cmd_get_collection(cmd) its_cmd_mask_field(cmd, 2, 0, 16) | |
520 | #define its_cmd_get_target_addr(cmd) its_cmd_mask_field(cmd, 2, 16, 32) | |
521 | #define its_cmd_get_validbit(cmd) its_cmd_mask_field(cmd, 2, 63, 1) | |
522 | ||
523 | /* | |
524 | * The DISCARD command frees an Interrupt Translation Table Entry (ITTE). | |
525 | * Must be called with the its_lock mutex held. | |
526 | */ | |
527 | static int vgic_its_cmd_handle_discard(struct kvm *kvm, struct vgic_its *its, | |
528 | u64 *its_cmd) | |
529 | { | |
530 | u32 device_id = its_cmd_get_deviceid(its_cmd); | |
531 | u32 event_id = its_cmd_get_id(its_cmd); | |
532 | struct its_itte *itte; | |
533 | ||
534 | ||
535 | itte = find_itte(its, device_id, event_id); | |
536 | if (itte && itte->collection) { | |
537 | /* | |
538 | * Though the spec talks about removing the pending state, we | |
539 | * don't bother here since we clear the ITTE anyway and the | |
540 | * pending state is a property of the ITTE struct. | |
541 | */ | |
542 | its_free_itte(kvm, itte); | |
543 | return 0; | |
544 | } | |
545 | ||
546 | return E_ITS_DISCARD_UNMAPPED_INTERRUPT; | |
547 | } | |
548 | ||
549 | /* | |
550 | * The MOVI command moves an ITTE to a different collection. | |
551 | * Must be called with the its_lock mutex held. | |
552 | */ | |
553 | static int vgic_its_cmd_handle_movi(struct kvm *kvm, struct vgic_its *its, | |
554 | u64 *its_cmd) | |
555 | { | |
556 | u32 device_id = its_cmd_get_deviceid(its_cmd); | |
557 | u32 event_id = its_cmd_get_id(its_cmd); | |
558 | u32 coll_id = its_cmd_get_collection(its_cmd); | |
559 | struct kvm_vcpu *vcpu; | |
560 | struct its_itte *itte; | |
561 | struct its_collection *collection; | |
562 | ||
563 | itte = find_itte(its, device_id, event_id); | |
564 | if (!itte) | |
565 | return E_ITS_MOVI_UNMAPPED_INTERRUPT; | |
566 | ||
567 | if (!its_is_collection_mapped(itte->collection)) | |
568 | return E_ITS_MOVI_UNMAPPED_COLLECTION; | |
569 | ||
570 | collection = find_collection(its, coll_id); | |
571 | if (!its_is_collection_mapped(collection)) | |
572 | return E_ITS_MOVI_UNMAPPED_COLLECTION; | |
573 | ||
574 | itte->collection = collection; | |
575 | vcpu = kvm_get_vcpu(kvm, collection->target_addr); | |
576 | ||
577 | spin_lock(&itte->irq->irq_lock); | |
578 | itte->irq->target_vcpu = vcpu; | |
579 | spin_unlock(&itte->irq->irq_lock); | |
580 | ||
581 | return 0; | |
582 | } | |
583 | ||
17a21f58 MZ |
584 | static int vgic_its_alloc_collection(struct vgic_its *its, |
585 | struct its_collection **colp, | |
df9f58fb AP |
586 | u32 coll_id) |
587 | { | |
17a21f58 MZ |
588 | struct its_collection *collection; |
589 | ||
590 | collection = kzalloc(sizeof(*collection), GFP_KERNEL); | |
591 | ||
df9f58fb AP |
592 | collection->collection_id = coll_id; |
593 | collection->target_addr = COLLECTION_NOT_MAPPED; | |
594 | ||
595 | list_add_tail(&collection->coll_list, &its->collection_list); | |
17a21f58 MZ |
596 | *colp = collection; |
597 | ||
598 | return 0; | |
599 | } | |
600 | ||
601 | static void vgic_its_free_collection(struct vgic_its *its, u32 coll_id) | |
602 | { | |
603 | struct its_collection *collection; | |
604 | struct its_device *device; | |
605 | struct its_itte *itte; | |
606 | ||
607 | /* | |
608 | * Clearing the mapping for that collection ID removes the | |
609 | * entry from the list. If there wasn't any before, we can | |
610 | * go home early. | |
611 | */ | |
612 | collection = find_collection(its, coll_id); | |
613 | if (!collection) | |
614 | return; | |
615 | ||
616 | for_each_lpi_its(device, itte, its) | |
617 | if (itte->collection && | |
618 | itte->collection->collection_id == coll_id) | |
619 | itte->collection = NULL; | |
620 | ||
621 | list_del(&collection->coll_list); | |
622 | kfree(collection); | |
df9f58fb AP |
623 | } |
624 | ||
625 | /* | |
626 | * The MAPTI and MAPI commands map LPIs to ITTEs. | |
627 | * Must be called with its_lock mutex held. | |
628 | */ | |
629 | static int vgic_its_cmd_handle_mapi(struct kvm *kvm, struct vgic_its *its, | |
630 | u64 *its_cmd, u8 subcmd) | |
631 | { | |
632 | u32 device_id = its_cmd_get_deviceid(its_cmd); | |
633 | u32 event_id = its_cmd_get_id(its_cmd); | |
634 | u32 coll_id = its_cmd_get_collection(its_cmd); | |
635 | struct its_itte *itte; | |
636 | struct its_device *device; | |
637 | struct its_collection *collection, *new_coll = NULL; | |
638 | int lpi_nr; | |
17a21f58 | 639 | int ret; |
df9f58fb AP |
640 | |
641 | device = find_its_device(its, device_id); | |
642 | if (!device) | |
643 | return E_ITS_MAPTI_UNMAPPED_DEVICE; | |
644 | ||
645 | collection = find_collection(its, coll_id); | |
646 | if (!collection) { | |
17a21f58 MZ |
647 | ret = vgic_its_alloc_collection(its, &collection, coll_id); |
648 | if (ret) | |
649 | return ret; | |
650 | new_coll = collection; | |
df9f58fb AP |
651 | } |
652 | ||
653 | if (subcmd == GITS_CMD_MAPTI) | |
654 | lpi_nr = its_cmd_get_physical_id(its_cmd); | |
655 | else | |
656 | lpi_nr = event_id; | |
657 | if (lpi_nr < GIC_LPI_OFFSET || | |
658 | lpi_nr >= max_lpis_propbaser(kvm->arch.vgic.propbaser)) { | |
17a21f58 MZ |
659 | ret = E_ITS_MAPTI_PHYSICALID_OOR; |
660 | goto err; | |
df9f58fb AP |
661 | } |
662 | ||
663 | itte = find_itte(its, device_id, event_id); | |
664 | if (!itte) { | |
665 | itte = kzalloc(sizeof(struct its_itte), GFP_KERNEL); | |
666 | if (!itte) { | |
17a21f58 MZ |
667 | ret = -ENOMEM; |
668 | goto err; | |
df9f58fb AP |
669 | } |
670 | ||
671 | itte->event_id = event_id; | |
672 | list_add_tail(&itte->itte_list, &device->itt_head); | |
673 | } | |
674 | ||
df9f58fb AP |
675 | itte->collection = collection; |
676 | itte->lpi = lpi_nr; | |
677 | itte->irq = vgic_add_lpi(kvm, lpi_nr); | |
678 | update_affinity_itte(kvm, itte); | |
679 | ||
680 | /* | |
681 | * We "cache" the configuration table entries in out struct vgic_irq's. | |
682 | * However we only have those structs for mapped IRQs, so we read in | |
683 | * the respective config data from memory here upon mapping the LPI. | |
684 | */ | |
685 | update_lpi_config(kvm, itte->irq, NULL); | |
686 | ||
687 | return 0; | |
17a21f58 MZ |
688 | err: |
689 | if (new_coll) | |
690 | vgic_its_free_collection(its, coll_id); | |
691 | return ret; | |
df9f58fb AP |
692 | } |
693 | ||
694 | /* Requires the its_lock to be held. */ | |
695 | static void vgic_its_unmap_device(struct kvm *kvm, struct its_device *device) | |
696 | { | |
697 | struct its_itte *itte, *temp; | |
698 | ||
699 | /* | |
700 | * The spec says that unmapping a device with still valid | |
701 | * ITTEs associated is UNPREDICTABLE. We remove all ITTEs, | |
702 | * since we cannot leave the memory unreferenced. | |
703 | */ | |
704 | list_for_each_entry_safe(itte, temp, &device->itt_head, itte_list) | |
705 | its_free_itte(kvm, itte); | |
706 | ||
707 | list_del(&device->dev_list); | |
708 | kfree(device); | |
709 | } | |
710 | ||
711 | /* | |
712 | * Check whether a device ID can be stored into the guest device tables. | |
713 | * For a direct table this is pretty easy, but gets a bit nasty for | |
714 | * indirect tables. We check whether the resulting guest physical address | |
715 | * is actually valid (covered by a memslot and guest accessbible). | |
716 | * For this we have to read the respective first level entry. | |
717 | */ | |
718 | static bool vgic_its_check_device_id(struct kvm *kvm, struct vgic_its *its, | |
719 | int device_id) | |
720 | { | |
721 | u64 r = its->baser_device_table; | |
b90338b7 | 722 | int l1_tbl_size = GITS_BASER_NR_PAGES(r) * SZ_64K; |
df9f58fb AP |
723 | int index; |
724 | u64 indirect_ptr; | |
725 | gfn_t gfn; | |
726 | ||
727 | ||
333a53ff MZ |
728 | if (!(r & GITS_BASER_INDIRECT)) { |
729 | phys_addr_t addr; | |
730 | ||
731 | if (device_id >= (l1_tbl_size / GITS_BASER_ENTRY_SIZE(r))) | |
732 | return false; | |
733 | ||
734 | addr = BASER_ADDRESS(r) + device_id * GITS_BASER_ENTRY_SIZE(r); | |
735 | gfn = addr >> PAGE_SHIFT; | |
736 | ||
737 | return kvm_is_visible_gfn(kvm, gfn); | |
738 | } | |
df9f58fb AP |
739 | |
740 | /* calculate and check the index into the 1st level */ | |
741 | index = device_id / (SZ_64K / GITS_BASER_ENTRY_SIZE(r)); | |
b90338b7 | 742 | if (index >= (l1_tbl_size / sizeof(u64))) |
df9f58fb AP |
743 | return false; |
744 | ||
745 | /* Each 1st level entry is represented by a 64-bit value. */ | |
c0091073 MZ |
746 | if (kvm_read_guest(kvm, |
747 | BASER_ADDRESS(r) + index * sizeof(indirect_ptr), | |
748 | &indirect_ptr, sizeof(indirect_ptr))) | |
df9f58fb AP |
749 | return false; |
750 | ||
7e3963a5 MZ |
751 | indirect_ptr = le64_to_cpu(indirect_ptr); |
752 | ||
df9f58fb AP |
753 | /* check the valid bit of the first level entry */ |
754 | if (!(indirect_ptr & BIT_ULL(63))) | |
755 | return false; | |
756 | ||
757 | /* | |
758 | * Mask the guest physical address and calculate the frame number. | |
759 | * Any address beyond our supported 48 bits of PA will be caught | |
760 | * by the actual check in the final step. | |
761 | */ | |
d6c7f865 MZ |
762 | indirect_ptr &= GENMASK_ULL(51, 16); |
763 | ||
764 | /* Find the address of the actual entry */ | |
765 | index = device_id % (SZ_64K / GITS_BASER_ENTRY_SIZE(r)); | |
766 | indirect_ptr += index * GITS_BASER_ENTRY_SIZE(r); | |
767 | gfn = indirect_ptr >> PAGE_SHIFT; | |
df9f58fb AP |
768 | |
769 | return kvm_is_visible_gfn(kvm, gfn); | |
770 | } | |
771 | ||
772 | /* | |
773 | * MAPD maps or unmaps a device ID to Interrupt Translation Tables (ITTs). | |
774 | * Must be called with the its_lock mutex held. | |
775 | */ | |
776 | static int vgic_its_cmd_handle_mapd(struct kvm *kvm, struct vgic_its *its, | |
777 | u64 *its_cmd) | |
778 | { | |
779 | u32 device_id = its_cmd_get_deviceid(its_cmd); | |
780 | bool valid = its_cmd_get_validbit(its_cmd); | |
781 | struct its_device *device; | |
782 | ||
783 | if (!vgic_its_check_device_id(kvm, its, device_id)) | |
784 | return E_ITS_MAPD_DEVICE_OOR; | |
785 | ||
786 | device = find_its_device(its, device_id); | |
787 | ||
788 | /* | |
789 | * The spec says that calling MAPD on an already mapped device | |
790 | * invalidates all cached data for this device. We implement this | |
791 | * by removing the mapping and re-establishing it. | |
792 | */ | |
793 | if (device) | |
794 | vgic_its_unmap_device(kvm, device); | |
795 | ||
796 | /* | |
797 | * The spec does not say whether unmapping a not-mapped device | |
798 | * is an error, so we are done in any case. | |
799 | */ | |
800 | if (!valid) | |
801 | return 0; | |
802 | ||
803 | device = kzalloc(sizeof(struct its_device), GFP_KERNEL); | |
804 | if (!device) | |
805 | return -ENOMEM; | |
806 | ||
807 | device->device_id = device_id; | |
808 | INIT_LIST_HEAD(&device->itt_head); | |
809 | ||
810 | list_add_tail(&device->dev_list, &its->device_list); | |
811 | ||
812 | return 0; | |
813 | } | |
814 | ||
815 | static int vgic_its_nr_collection_ids(struct vgic_its *its) | |
816 | { | |
817 | u64 r = its->baser_coll_table; | |
818 | ||
819 | return (GITS_BASER_NR_PAGES(r) * SZ_64K) / GITS_BASER_ENTRY_SIZE(r); | |
820 | } | |
821 | ||
822 | /* | |
823 | * The MAPC command maps collection IDs to redistributors. | |
824 | * Must be called with the its_lock mutex held. | |
825 | */ | |
826 | static int vgic_its_cmd_handle_mapc(struct kvm *kvm, struct vgic_its *its, | |
827 | u64 *its_cmd) | |
828 | { | |
829 | u16 coll_id; | |
830 | u32 target_addr; | |
831 | struct its_collection *collection; | |
832 | bool valid; | |
833 | ||
834 | valid = its_cmd_get_validbit(its_cmd); | |
835 | coll_id = its_cmd_get_collection(its_cmd); | |
836 | target_addr = its_cmd_get_target_addr(its_cmd); | |
837 | ||
838 | if (target_addr >= atomic_read(&kvm->online_vcpus)) | |
839 | return E_ITS_MAPC_PROCNUM_OOR; | |
840 | ||
841 | if (coll_id >= vgic_its_nr_collection_ids(its)) | |
842 | return E_ITS_MAPC_COLLECTION_OOR; | |
843 | ||
df9f58fb | 844 | if (!valid) { |
17a21f58 | 845 | vgic_its_free_collection(its, coll_id); |
df9f58fb | 846 | } else { |
17a21f58 MZ |
847 | collection = find_collection(its, coll_id); |
848 | ||
df9f58fb | 849 | if (!collection) { |
17a21f58 | 850 | int ret; |
df9f58fb | 851 | |
17a21f58 MZ |
852 | ret = vgic_its_alloc_collection(its, &collection, |
853 | coll_id); | |
854 | if (ret) | |
855 | return ret; | |
df9f58fb AP |
856 | collection->target_addr = target_addr; |
857 | } else { | |
858 | collection->target_addr = target_addr; | |
859 | update_affinity_collection(kvm, its, collection); | |
860 | } | |
861 | } | |
862 | ||
863 | return 0; | |
864 | } | |
865 | ||
866 | /* | |
867 | * The CLEAR command removes the pending state for a particular LPI. | |
868 | * Must be called with the its_lock mutex held. | |
869 | */ | |
870 | static int vgic_its_cmd_handle_clear(struct kvm *kvm, struct vgic_its *its, | |
871 | u64 *its_cmd) | |
872 | { | |
873 | u32 device_id = its_cmd_get_deviceid(its_cmd); | |
874 | u32 event_id = its_cmd_get_id(its_cmd); | |
875 | struct its_itte *itte; | |
876 | ||
877 | ||
878 | itte = find_itte(its, device_id, event_id); | |
879 | if (!itte) | |
880 | return E_ITS_CLEAR_UNMAPPED_INTERRUPT; | |
881 | ||
882 | itte->irq->pending = false; | |
883 | ||
884 | return 0; | |
885 | } | |
886 | ||
887 | /* | |
888 | * The INV command syncs the configuration bits from the memory table. | |
889 | * Must be called with the its_lock mutex held. | |
890 | */ | |
891 | static int vgic_its_cmd_handle_inv(struct kvm *kvm, struct vgic_its *its, | |
892 | u64 *its_cmd) | |
893 | { | |
894 | u32 device_id = its_cmd_get_deviceid(its_cmd); | |
895 | u32 event_id = its_cmd_get_id(its_cmd); | |
896 | struct its_itte *itte; | |
897 | ||
898 | ||
899 | itte = find_itte(its, device_id, event_id); | |
900 | if (!itte) | |
901 | return E_ITS_INV_UNMAPPED_INTERRUPT; | |
902 | ||
903 | return update_lpi_config(kvm, itte->irq, NULL); | |
904 | } | |
905 | ||
906 | /* | |
907 | * The INVALL command requests flushing of all IRQ data in this collection. | |
908 | * Find the VCPU mapped to that collection, then iterate over the VM's list | |
909 | * of mapped LPIs and update the configuration for each IRQ which targets | |
910 | * the specified vcpu. The configuration will be read from the in-memory | |
911 | * configuration table. | |
912 | * Must be called with the its_lock mutex held. | |
913 | */ | |
914 | static int vgic_its_cmd_handle_invall(struct kvm *kvm, struct vgic_its *its, | |
915 | u64 *its_cmd) | |
916 | { | |
917 | u32 coll_id = its_cmd_get_collection(its_cmd); | |
918 | struct its_collection *collection; | |
919 | struct kvm_vcpu *vcpu; | |
920 | struct vgic_irq *irq; | |
921 | u32 *intids; | |
922 | int irq_count, i; | |
923 | ||
924 | collection = find_collection(its, coll_id); | |
925 | if (!its_is_collection_mapped(collection)) | |
926 | return E_ITS_INVALL_UNMAPPED_COLLECTION; | |
927 | ||
928 | vcpu = kvm_get_vcpu(kvm, collection->target_addr); | |
929 | ||
930 | irq_count = vgic_copy_lpi_list(kvm, &intids); | |
931 | if (irq_count < 0) | |
932 | return irq_count; | |
933 | ||
934 | for (i = 0; i < irq_count; i++) { | |
935 | irq = vgic_get_irq(kvm, NULL, intids[i]); | |
936 | if (!irq) | |
937 | continue; | |
938 | update_lpi_config(kvm, irq, vcpu); | |
939 | vgic_put_irq(kvm, irq); | |
940 | } | |
941 | ||
942 | kfree(intids); | |
943 | ||
944 | return 0; | |
945 | } | |
946 | ||
947 | /* | |
948 | * The MOVALL command moves the pending state of all IRQs targeting one | |
949 | * redistributor to another. We don't hold the pending state in the VCPUs, | |
950 | * but in the IRQs instead, so there is really not much to do for us here. | |
951 | * However the spec says that no IRQ must target the old redistributor | |
952 | * afterwards, so we make sure that no LPI is using the associated target_vcpu. | |
953 | * This command affects all LPIs in the system that target that redistributor. | |
954 | */ | |
955 | static int vgic_its_cmd_handle_movall(struct kvm *kvm, struct vgic_its *its, | |
956 | u64 *its_cmd) | |
957 | { | |
958 | struct vgic_dist *dist = &kvm->arch.vgic; | |
959 | u32 target1_addr = its_cmd_get_target_addr(its_cmd); | |
960 | u32 target2_addr = its_cmd_mask_field(its_cmd, 3, 16, 32); | |
961 | struct kvm_vcpu *vcpu1, *vcpu2; | |
962 | struct vgic_irq *irq; | |
963 | ||
964 | if (target1_addr >= atomic_read(&kvm->online_vcpus) || | |
965 | target2_addr >= atomic_read(&kvm->online_vcpus)) | |
966 | return E_ITS_MOVALL_PROCNUM_OOR; | |
967 | ||
968 | if (target1_addr == target2_addr) | |
969 | return 0; | |
970 | ||
971 | vcpu1 = kvm_get_vcpu(kvm, target1_addr); | |
972 | vcpu2 = kvm_get_vcpu(kvm, target2_addr); | |
973 | ||
974 | spin_lock(&dist->lpi_list_lock); | |
975 | ||
976 | list_for_each_entry(irq, &dist->lpi_list_head, lpi_list) { | |
977 | spin_lock(&irq->irq_lock); | |
978 | ||
979 | if (irq->target_vcpu == vcpu1) | |
980 | irq->target_vcpu = vcpu2; | |
981 | ||
982 | spin_unlock(&irq->irq_lock); | |
983 | } | |
984 | ||
985 | spin_unlock(&dist->lpi_list_lock); | |
986 | ||
987 | return 0; | |
988 | } | |
989 | ||
2891a7df AP |
990 | /* |
991 | * The INT command injects the LPI associated with that DevID/EvID pair. | |
992 | * Must be called with the its_lock mutex held. | |
993 | */ | |
994 | static int vgic_its_cmd_handle_int(struct kvm *kvm, struct vgic_its *its, | |
995 | u64 *its_cmd) | |
996 | { | |
997 | u32 msi_data = its_cmd_get_id(its_cmd); | |
998 | u64 msi_devid = its_cmd_get_deviceid(its_cmd); | |
999 | ||
1000 | vgic_its_trigger_msi(kvm, its, msi_devid, msi_data); | |
1001 | ||
1002 | return 0; | |
1003 | } | |
1004 | ||
df9f58fb AP |
1005 | /* |
1006 | * This function is called with the its_cmd lock held, but the ITS data | |
1007 | * structure lock dropped. | |
1008 | */ | |
424c3383 AP |
1009 | static int vgic_its_handle_command(struct kvm *kvm, struct vgic_its *its, |
1010 | u64 *its_cmd) | |
1011 | { | |
df9f58fb AP |
1012 | u8 cmd = its_cmd_get_command(its_cmd); |
1013 | int ret = -ENODEV; | |
1014 | ||
1015 | mutex_lock(&its->its_lock); | |
1016 | switch (cmd) { | |
1017 | case GITS_CMD_MAPD: | |
1018 | ret = vgic_its_cmd_handle_mapd(kvm, its, its_cmd); | |
1019 | break; | |
1020 | case GITS_CMD_MAPC: | |
1021 | ret = vgic_its_cmd_handle_mapc(kvm, its, its_cmd); | |
1022 | break; | |
1023 | case GITS_CMD_MAPI: | |
1024 | ret = vgic_its_cmd_handle_mapi(kvm, its, its_cmd, cmd); | |
1025 | break; | |
1026 | case GITS_CMD_MAPTI: | |
1027 | ret = vgic_its_cmd_handle_mapi(kvm, its, its_cmd, cmd); | |
1028 | break; | |
1029 | case GITS_CMD_MOVI: | |
1030 | ret = vgic_its_cmd_handle_movi(kvm, its, its_cmd); | |
1031 | break; | |
1032 | case GITS_CMD_DISCARD: | |
1033 | ret = vgic_its_cmd_handle_discard(kvm, its, its_cmd); | |
1034 | break; | |
1035 | case GITS_CMD_CLEAR: | |
1036 | ret = vgic_its_cmd_handle_clear(kvm, its, its_cmd); | |
1037 | break; | |
1038 | case GITS_CMD_MOVALL: | |
1039 | ret = vgic_its_cmd_handle_movall(kvm, its, its_cmd); | |
1040 | break; | |
2891a7df AP |
1041 | case GITS_CMD_INT: |
1042 | ret = vgic_its_cmd_handle_int(kvm, its, its_cmd); | |
1043 | break; | |
df9f58fb AP |
1044 | case GITS_CMD_INV: |
1045 | ret = vgic_its_cmd_handle_inv(kvm, its, its_cmd); | |
1046 | break; | |
1047 | case GITS_CMD_INVALL: | |
1048 | ret = vgic_its_cmd_handle_invall(kvm, its, its_cmd); | |
1049 | break; | |
1050 | case GITS_CMD_SYNC: | |
1051 | /* we ignore this command: we are in sync all of the time */ | |
1052 | ret = 0; | |
1053 | break; | |
1054 | } | |
1055 | mutex_unlock(&its->its_lock); | |
1056 | ||
1057 | return ret; | |
424c3383 AP |
1058 | } |
1059 | ||
1060 | static u64 vgic_sanitise_its_baser(u64 reg) | |
1061 | { | |
1062 | reg = vgic_sanitise_field(reg, GITS_BASER_SHAREABILITY_MASK, | |
1063 | GITS_BASER_SHAREABILITY_SHIFT, | |
1064 | vgic_sanitise_shareability); | |
1065 | reg = vgic_sanitise_field(reg, GITS_BASER_INNER_CACHEABILITY_MASK, | |
1066 | GITS_BASER_INNER_CACHEABILITY_SHIFT, | |
1067 | vgic_sanitise_inner_cacheability); | |
1068 | reg = vgic_sanitise_field(reg, GITS_BASER_OUTER_CACHEABILITY_MASK, | |
1069 | GITS_BASER_OUTER_CACHEABILITY_SHIFT, | |
1070 | vgic_sanitise_outer_cacheability); | |
1071 | ||
1072 | /* Bits 15:12 contain bits 51:48 of the PA, which we don't support. */ | |
1073 | reg &= ~GENMASK_ULL(15, 12); | |
1074 | ||
1075 | /* We support only one (ITS) page size: 64K */ | |
1076 | reg = (reg & ~GITS_BASER_PAGE_SIZE_MASK) | GITS_BASER_PAGE_SIZE_64K; | |
1077 | ||
1078 | return reg; | |
1079 | } | |
1080 | ||
1081 | static u64 vgic_sanitise_its_cbaser(u64 reg) | |
1082 | { | |
1083 | reg = vgic_sanitise_field(reg, GITS_CBASER_SHAREABILITY_MASK, | |
1084 | GITS_CBASER_SHAREABILITY_SHIFT, | |
1085 | vgic_sanitise_shareability); | |
1086 | reg = vgic_sanitise_field(reg, GITS_CBASER_INNER_CACHEABILITY_MASK, | |
1087 | GITS_CBASER_INNER_CACHEABILITY_SHIFT, | |
1088 | vgic_sanitise_inner_cacheability); | |
1089 | reg = vgic_sanitise_field(reg, GITS_CBASER_OUTER_CACHEABILITY_MASK, | |
1090 | GITS_CBASER_OUTER_CACHEABILITY_SHIFT, | |
1091 | vgic_sanitise_outer_cacheability); | |
1092 | ||
1093 | /* | |
1094 | * Sanitise the physical address to be 64k aligned. | |
1095 | * Also limit the physical addresses to 48 bits. | |
1096 | */ | |
1097 | reg &= ~(GENMASK_ULL(51, 48) | GENMASK_ULL(15, 12)); | |
1098 | ||
1099 | return reg; | |
1100 | } | |
1101 | ||
1102 | static unsigned long vgic_mmio_read_its_cbaser(struct kvm *kvm, | |
1103 | struct vgic_its *its, | |
1104 | gpa_t addr, unsigned int len) | |
1105 | { | |
1106 | return extract_bytes(its->cbaser, addr & 7, len); | |
1107 | } | |
1108 | ||
1109 | static void vgic_mmio_write_its_cbaser(struct kvm *kvm, struct vgic_its *its, | |
1110 | gpa_t addr, unsigned int len, | |
1111 | unsigned long val) | |
1112 | { | |
1113 | /* When GITS_CTLR.Enable is 1, this register is RO. */ | |
1114 | if (its->enabled) | |
1115 | return; | |
1116 | ||
1117 | mutex_lock(&its->cmd_lock); | |
1118 | its->cbaser = update_64bit_reg(its->cbaser, addr & 7, len, val); | |
1119 | its->cbaser = vgic_sanitise_its_cbaser(its->cbaser); | |
1120 | its->creadr = 0; | |
1121 | /* | |
1122 | * CWRITER is architecturally UNKNOWN on reset, but we need to reset | |
1123 | * it to CREADR to make sure we start with an empty command buffer. | |
1124 | */ | |
1125 | its->cwriter = its->creadr; | |
1126 | mutex_unlock(&its->cmd_lock); | |
1127 | } | |
1128 | ||
1129 | #define ITS_CMD_BUFFER_SIZE(baser) ((((baser) & 0xff) + 1) << 12) | |
1130 | #define ITS_CMD_SIZE 32 | |
1131 | #define ITS_CMD_OFFSET(reg) ((reg) & GENMASK(19, 5)) | |
1132 | ||
1133 | /* | |
1134 | * By writing to CWRITER the guest announces new commands to be processed. | |
1135 | * To avoid any races in the first place, we take the its_cmd lock, which | |
1136 | * protects our ring buffer variables, so that there is only one user | |
1137 | * per ITS handling commands at a given time. | |
1138 | */ | |
1139 | static void vgic_mmio_write_its_cwriter(struct kvm *kvm, struct vgic_its *its, | |
1140 | gpa_t addr, unsigned int len, | |
1141 | unsigned long val) | |
1142 | { | |
1143 | gpa_t cbaser; | |
1144 | u64 cmd_buf[4]; | |
1145 | u32 reg; | |
1146 | ||
1147 | if (!its) | |
1148 | return; | |
1149 | ||
1150 | mutex_lock(&its->cmd_lock); | |
1151 | ||
1152 | reg = update_64bit_reg(its->cwriter, addr & 7, len, val); | |
1153 | reg = ITS_CMD_OFFSET(reg); | |
1154 | if (reg >= ITS_CMD_BUFFER_SIZE(its->cbaser)) { | |
1155 | mutex_unlock(&its->cmd_lock); | |
1156 | return; | |
1157 | } | |
1158 | ||
1159 | its->cwriter = reg; | |
1160 | cbaser = CBASER_ADDRESS(its->cbaser); | |
1161 | ||
1162 | while (its->cwriter != its->creadr) { | |
1163 | int ret = kvm_read_guest(kvm, cbaser + its->creadr, | |
1164 | cmd_buf, ITS_CMD_SIZE); | |
1165 | /* | |
1166 | * If kvm_read_guest() fails, this could be due to the guest | |
1167 | * programming a bogus value in CBASER or something else going | |
1168 | * wrong from which we cannot easily recover. | |
1169 | * According to section 6.3.2 in the GICv3 spec we can just | |
1170 | * ignore that command then. | |
1171 | */ | |
1172 | if (!ret) | |
1173 | vgic_its_handle_command(kvm, its, cmd_buf); | |
1174 | ||
1175 | its->creadr += ITS_CMD_SIZE; | |
1176 | if (its->creadr == ITS_CMD_BUFFER_SIZE(its->cbaser)) | |
1177 | its->creadr = 0; | |
1178 | } | |
1179 | ||
1180 | mutex_unlock(&its->cmd_lock); | |
1181 | } | |
1182 | ||
1183 | static unsigned long vgic_mmio_read_its_cwriter(struct kvm *kvm, | |
1184 | struct vgic_its *its, | |
1185 | gpa_t addr, unsigned int len) | |
1186 | { | |
1187 | return extract_bytes(its->cwriter, addr & 0x7, len); | |
1188 | } | |
1189 | ||
1190 | static unsigned long vgic_mmio_read_its_creadr(struct kvm *kvm, | |
1191 | struct vgic_its *its, | |
1192 | gpa_t addr, unsigned int len) | |
1193 | { | |
1194 | return extract_bytes(its->creadr, addr & 0x7, len); | |
1195 | } | |
1196 | ||
1197 | #define BASER_INDEX(addr) (((addr) / sizeof(u64)) & 0x7) | |
1198 | static unsigned long vgic_mmio_read_its_baser(struct kvm *kvm, | |
1199 | struct vgic_its *its, | |
1200 | gpa_t addr, unsigned int len) | |
1201 | { | |
1202 | u64 reg; | |
1203 | ||
1204 | switch (BASER_INDEX(addr)) { | |
1205 | case 0: | |
1206 | reg = its->baser_device_table; | |
1207 | break; | |
1208 | case 1: | |
1209 | reg = its->baser_coll_table; | |
1210 | break; | |
1211 | default: | |
1212 | reg = 0; | |
1213 | break; | |
1214 | } | |
1215 | ||
1216 | return extract_bytes(reg, addr & 7, len); | |
1217 | } | |
1218 | ||
1219 | #define GITS_BASER_RO_MASK (GENMASK_ULL(52, 48) | GENMASK_ULL(58, 56)) | |
1220 | static void vgic_mmio_write_its_baser(struct kvm *kvm, | |
1221 | struct vgic_its *its, | |
1222 | gpa_t addr, unsigned int len, | |
1223 | unsigned long val) | |
1224 | { | |
1225 | u64 entry_size, device_type; | |
1226 | u64 reg, *regptr, clearbits = 0; | |
1227 | ||
1228 | /* When GITS_CTLR.Enable is 1, we ignore write accesses. */ | |
1229 | if (its->enabled) | |
1230 | return; | |
1231 | ||
1232 | switch (BASER_INDEX(addr)) { | |
1233 | case 0: | |
1234 | regptr = &its->baser_device_table; | |
1235 | entry_size = 8; | |
1236 | device_type = GITS_BASER_TYPE_DEVICE; | |
1237 | break; | |
1238 | case 1: | |
1239 | regptr = &its->baser_coll_table; | |
1240 | entry_size = 8; | |
1241 | device_type = GITS_BASER_TYPE_COLLECTION; | |
1242 | clearbits = GITS_BASER_INDIRECT; | |
1243 | break; | |
1244 | default: | |
1245 | return; | |
1246 | } | |
1247 | ||
1248 | reg = update_64bit_reg(*regptr, addr & 7, len, val); | |
1249 | reg &= ~GITS_BASER_RO_MASK; | |
1250 | reg &= ~clearbits; | |
1251 | ||
1252 | reg |= (entry_size - 1) << GITS_BASER_ENTRY_SIZE_SHIFT; | |
1253 | reg |= device_type << GITS_BASER_TYPE_SHIFT; | |
1254 | reg = vgic_sanitise_its_baser(reg); | |
1255 | ||
1256 | *regptr = reg; | |
1257 | } | |
1258 | ||
59c5ab40 AP |
1259 | #define REGISTER_ITS_DESC(off, rd, wr, length, acc) \ |
1260 | { \ | |
1261 | .reg_offset = off, \ | |
1262 | .len = length, \ | |
1263 | .access_flags = acc, \ | |
1264 | .its_read = rd, \ | |
1265 | .its_write = wr, \ | |
1266 | } | |
1267 | ||
59c5ab40 AP |
1268 | static void its_mmio_write_wi(struct kvm *kvm, struct vgic_its *its, |
1269 | gpa_t addr, unsigned int len, unsigned long val) | |
1270 | { | |
1271 | /* Ignore */ | |
1272 | } | |
1273 | ||
1274 | static struct vgic_register_region its_registers[] = { | |
1275 | REGISTER_ITS_DESC(GITS_CTLR, | |
424c3383 | 1276 | vgic_mmio_read_its_ctlr, vgic_mmio_write_its_ctlr, 4, |
59c5ab40 AP |
1277 | VGIC_ACCESS_32bit), |
1278 | REGISTER_ITS_DESC(GITS_IIDR, | |
424c3383 | 1279 | vgic_mmio_read_its_iidr, its_mmio_write_wi, 4, |
59c5ab40 AP |
1280 | VGIC_ACCESS_32bit), |
1281 | REGISTER_ITS_DESC(GITS_TYPER, | |
424c3383 | 1282 | vgic_mmio_read_its_typer, its_mmio_write_wi, 8, |
59c5ab40 AP |
1283 | VGIC_ACCESS_64bit | VGIC_ACCESS_32bit), |
1284 | REGISTER_ITS_DESC(GITS_CBASER, | |
424c3383 | 1285 | vgic_mmio_read_its_cbaser, vgic_mmio_write_its_cbaser, 8, |
59c5ab40 AP |
1286 | VGIC_ACCESS_64bit | VGIC_ACCESS_32bit), |
1287 | REGISTER_ITS_DESC(GITS_CWRITER, | |
424c3383 | 1288 | vgic_mmio_read_its_cwriter, vgic_mmio_write_its_cwriter, 8, |
59c5ab40 AP |
1289 | VGIC_ACCESS_64bit | VGIC_ACCESS_32bit), |
1290 | REGISTER_ITS_DESC(GITS_CREADR, | |
424c3383 | 1291 | vgic_mmio_read_its_creadr, its_mmio_write_wi, 8, |
59c5ab40 AP |
1292 | VGIC_ACCESS_64bit | VGIC_ACCESS_32bit), |
1293 | REGISTER_ITS_DESC(GITS_BASER, | |
424c3383 | 1294 | vgic_mmio_read_its_baser, vgic_mmio_write_its_baser, 0x40, |
59c5ab40 AP |
1295 | VGIC_ACCESS_64bit | VGIC_ACCESS_32bit), |
1296 | REGISTER_ITS_DESC(GITS_IDREGS_BASE, | |
424c3383 | 1297 | vgic_mmio_read_its_idregs, its_mmio_write_wi, 0x30, |
59c5ab40 AP |
1298 | VGIC_ACCESS_32bit), |
1299 | }; | |
1300 | ||
33d3bc95 AP |
1301 | /* This is called on setting the LPI enable bit in the redistributor. */ |
1302 | void vgic_enable_lpis(struct kvm_vcpu *vcpu) | |
1303 | { | |
1304 | if (!(vcpu->arch.vgic_cpu.pendbaser & GICR_PENDBASER_PTZ)) | |
1305 | its_sync_lpi_pending_table(vcpu); | |
1306 | } | |
1307 | ||
59c5ab40 AP |
1308 | static int vgic_its_init_its(struct kvm *kvm, struct vgic_its *its) |
1309 | { | |
1310 | struct vgic_io_device *iodev = &its->iodev; | |
1311 | int ret; | |
1312 | ||
1085fdc6 AP |
1313 | if (its->initialized) |
1314 | return 0; | |
1315 | ||
59c5ab40 AP |
1316 | if (IS_VGIC_ADDR_UNDEF(its->vgic_its_base)) |
1317 | return -ENXIO; | |
1318 | ||
1319 | iodev->regions = its_registers; | |
1320 | iodev->nr_regions = ARRAY_SIZE(its_registers); | |
1321 | kvm_iodevice_init(&iodev->dev, &kvm_io_gic_ops); | |
1322 | ||
1323 | iodev->base_addr = its->vgic_its_base; | |
1324 | iodev->iodev_type = IODEV_ITS; | |
1325 | iodev->its = its; | |
1326 | mutex_lock(&kvm->slots_lock); | |
1327 | ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, iodev->base_addr, | |
1328 | KVM_VGIC_V3_ITS_SIZE, &iodev->dev); | |
1329 | mutex_unlock(&kvm->slots_lock); | |
1330 | ||
1085fdc6 AP |
1331 | if (!ret) |
1332 | its->initialized = true; | |
1333 | ||
59c5ab40 AP |
1334 | return ret; |
1335 | } | |
1085fdc6 | 1336 | |
424c3383 AP |
1337 | #define INITIAL_BASER_VALUE \ |
1338 | (GIC_BASER_CACHEABILITY(GITS_BASER, INNER, RaWb) | \ | |
1339 | GIC_BASER_CACHEABILITY(GITS_BASER, OUTER, SameAsInner) | \ | |
1340 | GIC_BASER_SHAREABILITY(GITS_BASER, InnerShareable) | \ | |
1341 | ((8ULL - 1) << GITS_BASER_ENTRY_SIZE_SHIFT) | \ | |
1342 | GITS_BASER_PAGE_SIZE_64K) | |
1343 | ||
1344 | #define INITIAL_PROPBASER_VALUE \ | |
1345 | (GIC_BASER_CACHEABILITY(GICR_PROPBASER, INNER, RaWb) | \ | |
1346 | GIC_BASER_CACHEABILITY(GICR_PROPBASER, OUTER, SameAsInner) | \ | |
1347 | GIC_BASER_SHAREABILITY(GICR_PROPBASER, InnerShareable)) | |
1348 | ||
1085fdc6 AP |
1349 | static int vgic_its_create(struct kvm_device *dev, u32 type) |
1350 | { | |
1351 | struct vgic_its *its; | |
1352 | ||
1353 | if (type != KVM_DEV_TYPE_ARM_VGIC_ITS) | |
1354 | return -ENODEV; | |
1355 | ||
1356 | its = kzalloc(sizeof(struct vgic_its), GFP_KERNEL); | |
1357 | if (!its) | |
1358 | return -ENOMEM; | |
1359 | ||
424c3383 AP |
1360 | mutex_init(&its->its_lock); |
1361 | mutex_init(&its->cmd_lock); | |
1362 | ||
1085fdc6 AP |
1363 | its->vgic_its_base = VGIC_ADDR_UNDEF; |
1364 | ||
424c3383 AP |
1365 | INIT_LIST_HEAD(&its->device_list); |
1366 | INIT_LIST_HEAD(&its->collection_list); | |
1367 | ||
1085fdc6 AP |
1368 | dev->kvm->arch.vgic.has_its = true; |
1369 | its->initialized = false; | |
1370 | its->enabled = false; | |
1371 | ||
424c3383 AP |
1372 | its->baser_device_table = INITIAL_BASER_VALUE | |
1373 | ((u64)GITS_BASER_TYPE_DEVICE << GITS_BASER_TYPE_SHIFT); | |
1374 | its->baser_coll_table = INITIAL_BASER_VALUE | | |
1375 | ((u64)GITS_BASER_TYPE_COLLECTION << GITS_BASER_TYPE_SHIFT); | |
1376 | dev->kvm->arch.vgic.propbaser = INITIAL_PROPBASER_VALUE; | |
1377 | ||
1085fdc6 AP |
1378 | dev->private = its; |
1379 | ||
1380 | return 0; | |
1381 | } | |
1382 | ||
1383 | static void vgic_its_destroy(struct kvm_device *kvm_dev) | |
1384 | { | |
424c3383 | 1385 | struct kvm *kvm = kvm_dev->kvm; |
1085fdc6 | 1386 | struct vgic_its *its = kvm_dev->private; |
424c3383 AP |
1387 | struct its_device *dev; |
1388 | struct its_itte *itte; | |
1389 | struct list_head *dev_cur, *dev_temp; | |
1390 | struct list_head *cur, *temp; | |
1391 | ||
1392 | /* | |
1393 | * We may end up here without the lists ever having been initialized. | |
1394 | * Check this and bail out early to avoid dereferencing a NULL pointer. | |
1395 | */ | |
1396 | if (!its->device_list.next) | |
1397 | return; | |
1398 | ||
1399 | mutex_lock(&its->its_lock); | |
1400 | list_for_each_safe(dev_cur, dev_temp, &its->device_list) { | |
1401 | dev = container_of(dev_cur, struct its_device, dev_list); | |
1402 | list_for_each_safe(cur, temp, &dev->itt_head) { | |
1403 | itte = (container_of(cur, struct its_itte, itte_list)); | |
1404 | its_free_itte(kvm, itte); | |
1405 | } | |
1406 | list_del(dev_cur); | |
1407 | kfree(dev); | |
1408 | } | |
1409 | ||
1410 | list_for_each_safe(cur, temp, &its->collection_list) { | |
1411 | list_del(cur); | |
1412 | kfree(container_of(cur, struct its_collection, coll_list)); | |
1413 | } | |
1414 | mutex_unlock(&its->its_lock); | |
1085fdc6 AP |
1415 | |
1416 | kfree(its); | |
1417 | } | |
1418 | ||
1419 | static int vgic_its_has_attr(struct kvm_device *dev, | |
1420 | struct kvm_device_attr *attr) | |
1421 | { | |
1422 | switch (attr->group) { | |
1423 | case KVM_DEV_ARM_VGIC_GRP_ADDR: | |
1424 | switch (attr->attr) { | |
1425 | case KVM_VGIC_ITS_ADDR_TYPE: | |
1426 | return 0; | |
1427 | } | |
1428 | break; | |
1429 | case KVM_DEV_ARM_VGIC_GRP_CTRL: | |
1430 | switch (attr->attr) { | |
1431 | case KVM_DEV_ARM_VGIC_CTRL_INIT: | |
1432 | return 0; | |
1433 | } | |
1434 | break; | |
1435 | } | |
1436 | return -ENXIO; | |
1437 | } | |
1438 | ||
1439 | static int vgic_its_set_attr(struct kvm_device *dev, | |
1440 | struct kvm_device_attr *attr) | |
1441 | { | |
1442 | struct vgic_its *its = dev->private; | |
1443 | int ret; | |
1444 | ||
1445 | switch (attr->group) { | |
1446 | case KVM_DEV_ARM_VGIC_GRP_ADDR: { | |
1447 | u64 __user *uaddr = (u64 __user *)(long)attr->addr; | |
1448 | unsigned long type = (unsigned long)attr->attr; | |
1449 | u64 addr; | |
1450 | ||
1451 | if (type != KVM_VGIC_ITS_ADDR_TYPE) | |
1452 | return -ENODEV; | |
1453 | ||
1454 | if (its->initialized) | |
1455 | return -EBUSY; | |
1456 | ||
1457 | if (copy_from_user(&addr, uaddr, sizeof(addr))) | |
1458 | return -EFAULT; | |
1459 | ||
1460 | ret = vgic_check_ioaddr(dev->kvm, &its->vgic_its_base, | |
1461 | addr, SZ_64K); | |
1462 | if (ret) | |
1463 | return ret; | |
1464 | ||
1465 | its->vgic_its_base = addr; | |
1466 | ||
1467 | return 0; | |
1468 | } | |
1469 | case KVM_DEV_ARM_VGIC_GRP_CTRL: | |
1470 | switch (attr->attr) { | |
1471 | case KVM_DEV_ARM_VGIC_CTRL_INIT: | |
1472 | return vgic_its_init_its(dev->kvm, its); | |
1473 | } | |
1474 | break; | |
1475 | } | |
1476 | return -ENXIO; | |
1477 | } | |
1478 | ||
1479 | static int vgic_its_get_attr(struct kvm_device *dev, | |
1480 | struct kvm_device_attr *attr) | |
1481 | { | |
1482 | switch (attr->group) { | |
1483 | case KVM_DEV_ARM_VGIC_GRP_ADDR: { | |
1484 | struct vgic_its *its = dev->private; | |
1485 | u64 addr = its->vgic_its_base; | |
1486 | u64 __user *uaddr = (u64 __user *)(long)attr->addr; | |
1487 | unsigned long type = (unsigned long)attr->attr; | |
1488 | ||
1489 | if (type != KVM_VGIC_ITS_ADDR_TYPE) | |
1490 | return -ENODEV; | |
1491 | ||
1492 | if (copy_to_user(uaddr, &addr, sizeof(addr))) | |
1493 | return -EFAULT; | |
1494 | break; | |
1495 | default: | |
1496 | return -ENXIO; | |
1497 | } | |
1498 | } | |
1499 | ||
1500 | return 0; | |
1501 | } | |
1502 | ||
1503 | static struct kvm_device_ops kvm_arm_vgic_its_ops = { | |
1504 | .name = "kvm-arm-vgic-its", | |
1505 | .create = vgic_its_create, | |
1506 | .destroy = vgic_its_destroy, | |
1507 | .set_attr = vgic_its_set_attr, | |
1508 | .get_attr = vgic_its_get_attr, | |
1509 | .has_attr = vgic_its_has_attr, | |
1510 | }; | |
1511 | ||
1512 | int kvm_vgic_register_its_device(void) | |
1513 | { | |
1514 | return kvm_register_device_ops(&kvm_arm_vgic_its_ops, | |
1515 | KVM_DEV_TYPE_ARM_VGIC_ITS); | |
1516 | } |