1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * GICv3 ITS emulation 4 * 5 * Copyright (C) 2015,2016 ARM Ltd. 6 * Author: Andre Przywara <andre.przywara@arm.com> 7 */ 8 9 #include <linux/cpu.h> 10 #include <linux/kvm.h> 11 #include <linux/kvm_host.h> 12 #include <linux/interrupt.h> 13 #include <linux/list.h> 14 #include <linux/uaccess.h> 15 #include <linux/list_sort.h> 16 17 #include <linux/irqchip/arm-gic-v3.h> 18 19 #include <asm/kvm_emulate.h> 20 #include <asm/kvm_arm.h> 21 #include <asm/kvm_mmu.h> 22 23 #include "vgic.h" 24 #include "vgic-mmio.h" 25 26 static struct kvm_device_ops kvm_arm_vgic_its_ops; 27 28 static int vgic_its_save_tables_v0(struct vgic_its *its); 29 static int vgic_its_restore_tables_v0(struct vgic_its *its); 30 static void vgic_its_commit_v0(struct vgic_its *its); 31 static int update_lpi_config(struct kvm *kvm, struct vgic_irq *irq, 32 struct kvm_vcpu *filter_vcpu, bool needs_inv); 33 34 #define vgic_its_read_entry_lock(i, g, valp, t) \ 35 ({ \ 36 int __sz = vgic_its_get_abi(i)->t##_esz; \ 37 struct kvm *__k = (i)->dev->kvm; \ 38 int __ret; \ 39 \ 40 BUILD_BUG_ON(NR_ITS_ABIS == 1 && \ 41 sizeof(*(valp)) != ABI_0_ESZ); \ 42 if (NR_ITS_ABIS > 1 && \ 43 KVM_BUG_ON(__sz != sizeof(*(valp)), __k)) \ 44 __ret = -EINVAL; \ 45 else \ 46 __ret = kvm_read_guest_lock(__k, (g), \ 47 valp, __sz); \ 48 __ret; \ 49 }) 50 51 #define vgic_its_write_entry_lock(i, g, val, t) \ 52 ({ \ 53 int __sz = vgic_its_get_abi(i)->t##_esz; \ 54 struct kvm *__k = (i)->dev->kvm; \ 55 typeof(val) __v = (val); \ 56 int __ret; \ 57 \ 58 BUILD_BUG_ON(NR_ITS_ABIS == 1 && \ 59 sizeof(__v) != ABI_0_ESZ); \ 60 if (NR_ITS_ABIS > 1 && \ 61 KVM_BUG_ON(__sz != sizeof(__v), __k)) \ 62 __ret = -EINVAL; \ 63 else \ 64 __ret = vgic_write_guest_lock(__k, (g), \ 65 &__v, __sz); \ 66 __ret; \ 67 }) 68 69 /* 70 * Creates a new (reference to a) struct vgic_irq for a given LPI. 71 * If this LPI is already mapped on another ITS, we increase its refcount 72 * and return a pointer to the existing structure. 73 * If this is a "new" LPI, we allocate and initialize a new struct vgic_irq. 74 * This function returns a pointer to the _unlocked_ structure. 75 */ 76 static struct vgic_irq *vgic_add_lpi(struct kvm *kvm, u32 intid, 77 struct kvm_vcpu *vcpu) 78 { 79 struct vgic_dist *dist = &kvm->arch.vgic; 80 struct vgic_irq *irq = vgic_get_irq(kvm, intid), *oldirq; 81 unsigned long flags; 82 int ret; 83 84 /* In this case there is no put, since we keep the reference. */ 85 if (irq) 86 return irq; 87 88 irq = kzalloc_obj(struct vgic_irq, GFP_KERNEL_ACCOUNT); 89 if (!irq) 90 return ERR_PTR(-ENOMEM); 91 92 ret = xa_reserve_irq(&dist->lpi_xa, intid, GFP_KERNEL_ACCOUNT); 93 if (ret) { 94 kfree(irq); 95 return ERR_PTR(ret); 96 } 97 98 INIT_LIST_HEAD(&irq->ap_list); 99 raw_spin_lock_init(&irq->irq_lock); 100 101 irq->config = VGIC_CONFIG_EDGE; 102 refcount_set(&irq->refcount, 1); 103 irq->intid = intid; 104 irq->target_vcpu = vcpu; 105 irq->group = 1; 106 107 xa_lock_irqsave(&dist->lpi_xa, flags); 108 109 /* 110 * There could be a race with another vgic_add_lpi(), so we need to 111 * check that we don't add a second list entry with the same LPI. 112 */ 113 oldirq = xa_load(&dist->lpi_xa, intid); 114 if (vgic_try_get_irq_ref(oldirq)) { 115 /* Someone was faster with adding this LPI, lets use that. */ 116 kfree(irq); 117 irq = oldirq; 118 } else { 119 ret = xa_err(__xa_store(&dist->lpi_xa, intid, irq, 0)); 120 } 121 122 xa_unlock_irqrestore(&dist->lpi_xa, flags); 123 124 if (ret) { 125 xa_release(&dist->lpi_xa, intid); 126 kfree(irq); 127 128 return ERR_PTR(ret); 129 } 130 131 /* 132 * We "cache" the configuration table entries in our struct vgic_irq's. 133 * However we only have those structs for mapped IRQs, so we read in 134 * the respective config data from memory here upon mapping the LPI. 135 * 136 * Should any of these fail, behave as if we couldn't create the LPI 137 * by dropping the refcount and returning the error. 138 */ 139 ret = update_lpi_config(kvm, irq, NULL, false); 140 if (ret) { 141 vgic_put_irq(kvm, irq); 142 return ERR_PTR(ret); 143 } 144 145 ret = vgic_v3_lpi_sync_pending_status(kvm, irq); 146 if (ret) { 147 vgic_put_irq(kvm, irq); 148 return ERR_PTR(ret); 149 } 150 151 return irq; 152 } 153 154 /** 155 * struct vgic_its_abi - ITS abi ops and settings 156 * @cte_esz: collection table entry size 157 * @dte_esz: device table entry size 158 * @ite_esz: interrupt translation table entry size 159 * @save_tables: save the ITS tables into guest RAM 160 * @restore_tables: restore the ITS internal structs from tables 161 * stored in guest RAM 162 * @commit: initialize the registers which expose the ABI settings, 163 * especially the entry sizes 164 */ 165 struct vgic_its_abi { 166 int cte_esz; 167 int dte_esz; 168 int ite_esz; 169 int (*save_tables)(struct vgic_its *its); 170 int (*restore_tables)(struct vgic_its *its); 171 void (*commit)(struct vgic_its *its); 172 }; 173 174 #define ABI_0_ESZ 8 175 #define ESZ_MAX ABI_0_ESZ 176 177 static const struct vgic_its_abi its_table_abi_versions[] = { 178 [0] = { 179 .cte_esz = ABI_0_ESZ, 180 .dte_esz = ABI_0_ESZ, 181 .ite_esz = ABI_0_ESZ, 182 .save_tables = vgic_its_save_tables_v0, 183 .restore_tables = vgic_its_restore_tables_v0, 184 .commit = vgic_its_commit_v0, 185 }, 186 }; 187 188 #define NR_ITS_ABIS ARRAY_SIZE(its_table_abi_versions) 189 190 inline const struct vgic_its_abi *vgic_its_get_abi(struct vgic_its *its) 191 { 192 return &its_table_abi_versions[its->abi_rev]; 193 } 194 195 static void vgic_its_set_abi(struct vgic_its *its, u32 rev) 196 { 197 const struct vgic_its_abi *abi; 198 199 its->abi_rev = rev; 200 abi = vgic_its_get_abi(its); 201 abi->commit(its); 202 } 203 204 /* 205 * Find and returns a device in the device table for an ITS. 206 * Must be called with the its_lock mutex held. 207 */ 208 static struct its_device *find_its_device(struct vgic_its *its, u32 device_id) 209 { 210 struct its_device *device; 211 212 list_for_each_entry(device, &its->device_list, dev_list) 213 if (device_id == device->device_id) 214 return device; 215 216 return NULL; 217 } 218 219 /* 220 * Find and returns an interrupt translation table entry (ITTE) for a given 221 * Device ID/Event ID pair on an ITS. 222 * Must be called with the its_lock mutex held. 223 */ 224 static struct its_ite *find_ite(struct vgic_its *its, u32 device_id, 225 u32 event_id) 226 { 227 struct its_device *device; 228 struct its_ite *ite; 229 230 device = find_its_device(its, device_id); 231 if (device == NULL) 232 return NULL; 233 234 list_for_each_entry(ite, &device->itt_head, ite_list) 235 if (ite->event_id == event_id) 236 return ite; 237 238 return NULL; 239 } 240 241 /* To be used as an iterator this macro misses the enclosing parentheses */ 242 #define for_each_lpi_its(dev, ite, its) \ 243 list_for_each_entry(dev, &(its)->device_list, dev_list) \ 244 list_for_each_entry(ite, &(dev)->itt_head, ite_list) 245 246 #define GIC_LPI_OFFSET 8192 247 248 #define VITS_TYPER_IDBITS 16 249 #define VITS_MAX_EVENTID (BIT(VITS_TYPER_IDBITS) - 1) 250 #define VITS_TYPER_DEVBITS 16 251 #define VITS_MAX_DEVID (BIT(VITS_TYPER_DEVBITS) - 1) 252 #define VITS_DTE_MAX_DEVID_OFFSET (BIT(14) - 1) 253 #define VITS_ITE_MAX_EVENTID_OFFSET (BIT(16) - 1) 254 255 /* 256 * Finds and returns a collection in the ITS collection table. 257 * Must be called with the its_lock mutex held. 258 */ 259 static struct its_collection *find_collection(struct vgic_its *its, int coll_id) 260 { 261 struct its_collection *collection; 262 263 list_for_each_entry(collection, &its->collection_list, coll_list) { 264 if (coll_id == collection->collection_id) 265 return collection; 266 } 267 268 return NULL; 269 } 270 271 #define LPI_PROP_ENABLE_BIT(p) ((p) & LPI_PROP_ENABLED) 272 #define LPI_PROP_PRIORITY(p) ((p) & 0xfc) 273 274 /* 275 * Reads the configuration data for a given LPI from guest memory and 276 * updates the fields in struct vgic_irq. 277 * If filter_vcpu is not NULL, applies only if the IRQ is targeting this 278 * VCPU. Unconditionally applies if filter_vcpu is NULL. 279 */ 280 static int update_lpi_config(struct kvm *kvm, struct vgic_irq *irq, 281 struct kvm_vcpu *filter_vcpu, bool needs_inv) 282 { 283 u64 propbase = GICR_PROPBASER_ADDRESS(kvm->arch.vgic.propbaser); 284 u8 prop; 285 int ret; 286 unsigned long flags; 287 288 ret = kvm_read_guest_lock(kvm, propbase + irq->intid - GIC_LPI_OFFSET, 289 &prop, 1); 290 291 if (ret) 292 return ret; 293 294 raw_spin_lock_irqsave(&irq->irq_lock, flags); 295 296 if (!filter_vcpu || filter_vcpu == irq->target_vcpu) { 297 irq->priority = LPI_PROP_PRIORITY(prop); 298 irq->enabled = LPI_PROP_ENABLE_BIT(prop); 299 300 if (!irq->hw) { 301 vgic_queue_irq_unlock(kvm, irq, flags); 302 return 0; 303 } 304 } 305 306 if (irq->hw) 307 ret = its_prop_update_vlpi(irq->host_irq, prop, needs_inv); 308 309 raw_spin_unlock_irqrestore(&irq->irq_lock, flags); 310 return ret; 311 } 312 313 static int update_affinity(struct vgic_irq *irq, struct kvm_vcpu *vcpu) 314 { 315 struct its_vlpi_map map; 316 int ret; 317 318 guard(raw_spinlock_irqsave)(&irq->irq_lock); 319 irq->target_vcpu = vcpu; 320 321 if (!irq->hw) 322 return 0; 323 324 ret = its_get_vlpi(irq->host_irq, &map); 325 if (ret) 326 return ret; 327 328 if (map.vpe) 329 atomic_dec(&map.vpe->vlpi_count); 330 331 map.vpe = &vcpu->arch.vgic_cpu.vgic_v3.its_vpe; 332 atomic_inc(&map.vpe->vlpi_count); 333 return its_map_vlpi(irq->host_irq, &map); 334 } 335 336 static struct kvm_vcpu *collection_to_vcpu(struct kvm *kvm, 337 struct its_collection *col) 338 { 339 return kvm_get_vcpu_by_id(kvm, col->target_addr); 340 } 341 342 /* 343 * Promotes the ITS view of affinity of an ITTE (which redistributor this LPI 344 * is targeting) to the VGIC's view, which deals with target VCPUs. 345 * Needs to be called whenever either the collection for a LPIs has 346 * changed or the collection itself got retargeted. 347 */ 348 static void update_affinity_ite(struct kvm *kvm, struct its_ite *ite) 349 { 350 struct kvm_vcpu *vcpu; 351 352 if (!its_is_collection_mapped(ite->collection)) 353 return; 354 355 vcpu = collection_to_vcpu(kvm, ite->collection); 356 update_affinity(ite->irq, vcpu); 357 } 358 359 /* 360 * Updates the target VCPU for every LPI targeting this collection. 361 * Must be called with the its_lock mutex held. 362 */ 363 static void update_affinity_collection(struct kvm *kvm, struct vgic_its *its, 364 struct its_collection *coll) 365 { 366 struct its_device *device; 367 struct its_ite *ite; 368 369 for_each_lpi_its(device, ite, its) { 370 if (ite->collection != coll) 371 continue; 372 373 update_affinity_ite(kvm, ite); 374 } 375 } 376 377 static u32 max_lpis_propbaser(u64 propbaser) 378 { 379 int nr_idbits = (propbaser & 0x1f) + 1; 380 381 return 1U << min(nr_idbits, INTERRUPT_ID_BITS_ITS); 382 } 383 384 /* 385 * Sync the pending table pending bit of LPIs targeting @vcpu 386 * with our own data structures. This relies on the LPI being 387 * mapped before. 388 */ 389 static int its_sync_lpi_pending_table(struct kvm_vcpu *vcpu) 390 { 391 gpa_t pendbase = GICR_PENDBASER_ADDRESS(vcpu->arch.vgic_cpu.pendbaser); 392 struct vgic_dist *dist = &vcpu->kvm->arch.vgic; 393 unsigned long intid, flags; 394 struct vgic_irq *irq; 395 int last_byte_offset = -1; 396 int ret = 0; 397 u8 pendmask; 398 399 xa_for_each(&dist->lpi_xa, intid, irq) { 400 int byte_offset, bit_nr; 401 402 byte_offset = intid / BITS_PER_BYTE; 403 bit_nr = intid % BITS_PER_BYTE; 404 405 /* 406 * For contiguously allocated LPIs chances are we just read 407 * this very same byte in the last iteration. Reuse that. 408 */ 409 if (byte_offset != last_byte_offset) { 410 ret = kvm_read_guest_lock(vcpu->kvm, 411 pendbase + byte_offset, 412 &pendmask, 1); 413 if (ret) 414 return ret; 415 416 last_byte_offset = byte_offset; 417 } 418 419 irq = vgic_get_irq(vcpu->kvm, intid); 420 if (!irq) 421 continue; 422 423 raw_spin_lock_irqsave(&irq->irq_lock, flags); 424 if (irq->target_vcpu == vcpu) 425 irq->pending_latch = pendmask & (1U << bit_nr); 426 vgic_queue_irq_unlock(vcpu->kvm, irq, flags); 427 vgic_put_irq(vcpu->kvm, irq); 428 } 429 430 return ret; 431 } 432 433 static unsigned long vgic_mmio_read_its_typer(struct kvm *kvm, 434 struct vgic_its *its, 435 gpa_t addr, unsigned int len) 436 { 437 const struct vgic_its_abi *abi = vgic_its_get_abi(its); 438 u64 reg = GITS_TYPER_PLPIS; 439 440 /* 441 * We use linear CPU numbers for redistributor addressing, 442 * so GITS_TYPER.PTA is 0. 443 * Also we force all PROPBASER registers to be the same, so 444 * CommonLPIAff is 0 as well. 445 * To avoid memory waste in the guest, we keep the number of IDBits and 446 * DevBits low - as least for the time being. 447 */ 448 reg |= GIC_ENCODE_SZ(VITS_TYPER_DEVBITS, 5) << GITS_TYPER_DEVBITS_SHIFT; 449 reg |= GIC_ENCODE_SZ(VITS_TYPER_IDBITS, 5) << GITS_TYPER_IDBITS_SHIFT; 450 reg |= GIC_ENCODE_SZ(abi->ite_esz, 4) << GITS_TYPER_ITT_ENTRY_SIZE_SHIFT; 451 452 return extract_bytes(reg, addr & 7, len); 453 } 454 455 static unsigned long vgic_mmio_read_its_iidr(struct kvm *kvm, 456 struct vgic_its *its, 457 gpa_t addr, unsigned int len) 458 { 459 u32 val; 460 461 val = (its->abi_rev << GITS_IIDR_REV_SHIFT) & GITS_IIDR_REV_MASK; 462 val |= (PRODUCT_ID_KVM << GITS_IIDR_PRODUCTID_SHIFT) | IMPLEMENTER_ARM; 463 return val; 464 } 465 466 static int vgic_mmio_uaccess_write_its_iidr(struct kvm *kvm, 467 struct vgic_its *its, 468 gpa_t addr, unsigned int len, 469 unsigned long val) 470 { 471 u32 rev = GITS_IIDR_REV(val); 472 473 if (rev >= NR_ITS_ABIS) 474 return -EINVAL; 475 vgic_its_set_abi(its, rev); 476 return 0; 477 } 478 479 static unsigned long vgic_mmio_read_its_idregs(struct kvm *kvm, 480 struct vgic_its *its, 481 gpa_t addr, unsigned int len) 482 { 483 switch (addr & 0xffff) { 484 case GITS_PIDR0: 485 return 0x92; /* part number, bits[7:0] */ 486 case GITS_PIDR1: 487 return 0xb4; /* part number, bits[11:8] */ 488 case GITS_PIDR2: 489 return GIC_PIDR2_ARCH_GICv3 | 0x0b; 490 case GITS_PIDR4: 491 return 0x40; /* This is a 64K software visible page */ 492 /* The following are the ID registers for (any) GIC. */ 493 case GITS_CIDR0: 494 return 0x0d; 495 case GITS_CIDR1: 496 return 0xf0; 497 case GITS_CIDR2: 498 return 0x05; 499 case GITS_CIDR3: 500 return 0xb1; 501 } 502 503 return 0; 504 } 505 506 static struct vgic_its *__vgic_doorbell_to_its(struct kvm *kvm, gpa_t db) 507 { 508 struct kvm_io_device *kvm_io_dev; 509 struct vgic_io_device *iodev; 510 511 kvm_io_dev = kvm_io_bus_get_dev(kvm, KVM_MMIO_BUS, db); 512 if (!kvm_io_dev) 513 return ERR_PTR(-EINVAL); 514 515 if (kvm_io_dev->ops != &kvm_io_gic_ops) 516 return ERR_PTR(-EINVAL); 517 518 iodev = container_of(kvm_io_dev, struct vgic_io_device, dev); 519 if (iodev->iodev_type != IODEV_ITS) 520 return ERR_PTR(-EINVAL); 521 522 return iodev->its; 523 } 524 525 static unsigned long vgic_its_cache_key(u32 devid, u32 eventid) 526 { 527 return (((unsigned long)devid) << VITS_TYPER_IDBITS) | eventid; 528 529 } 530 531 static struct vgic_irq *vgic_its_check_cache(struct kvm *kvm, phys_addr_t db, 532 u32 devid, u32 eventid) 533 { 534 unsigned long cache_key = vgic_its_cache_key(devid, eventid); 535 struct vgic_its *its; 536 struct vgic_irq *irq; 537 538 if (devid > VITS_MAX_DEVID || eventid > VITS_MAX_EVENTID) 539 return NULL; 540 541 its = __vgic_doorbell_to_its(kvm, db); 542 if (IS_ERR(its)) 543 return NULL; 544 545 rcu_read_lock(); 546 547 irq = xa_load(&its->translation_cache, cache_key); 548 if (!vgic_try_get_irq_ref(irq)) 549 irq = NULL; 550 551 rcu_read_unlock(); 552 553 return irq; 554 } 555 556 static void vgic_its_cache_translation(struct kvm *kvm, struct vgic_its *its, 557 u32 devid, u32 eventid, 558 struct vgic_irq *irq) 559 { 560 unsigned long cache_key = vgic_its_cache_key(devid, eventid); 561 struct vgic_irq *old; 562 563 /* Do not cache a directly injected interrupt */ 564 if (irq->hw) 565 return; 566 567 /* 568 * The irq refcount is guaranteed to be nonzero while holding the 569 * its_lock, as the ITE (and the reference it holds) cannot be freed. 570 */ 571 lockdep_assert_held(&its->its_lock); 572 vgic_get_irq_ref(irq); 573 574 old = xa_store(&its->translation_cache, cache_key, irq, GFP_KERNEL_ACCOUNT); 575 576 /* 577 * Put the reference taken on @irq if the store fails. Intentionally do 578 * not return the error as the translation cache is best effort. 579 */ 580 if (xa_is_err(old)) { 581 vgic_put_irq(kvm, irq); 582 return; 583 } 584 585 /* 586 * We could have raced with another CPU caching the same 587 * translation behind our back, ensure we don't leak a 588 * reference if that is the case. 589 */ 590 if (old) 591 vgic_put_irq(kvm, old); 592 } 593 594 static void vgic_its_invalidate_cache(struct vgic_its *its) 595 { 596 struct kvm *kvm = its->dev->kvm; 597 struct vgic_irq *irq; 598 unsigned long idx; 599 600 xa_for_each(&its->translation_cache, idx, irq) { 601 /* Only the context that erases the entry drops its cache ref. */ 602 irq = xa_erase(&its->translation_cache, idx); 603 if (irq) 604 vgic_put_irq(kvm, irq); 605 } 606 } 607 608 void vgic_its_invalidate_all_caches(struct kvm *kvm) 609 { 610 struct kvm_device *dev; 611 struct vgic_its *its; 612 613 rcu_read_lock(); 614 615 list_for_each_entry_rcu(dev, &kvm->devices, vm_node) { 616 if (dev->ops != &kvm_arm_vgic_its_ops) 617 continue; 618 619 its = dev->private; 620 vgic_its_invalidate_cache(its); 621 } 622 623 rcu_read_unlock(); 624 } 625 626 int vgic_its_resolve_lpi(struct kvm *kvm, struct vgic_its *its, 627 u32 devid, u32 eventid, struct vgic_irq **irq) 628 { 629 struct kvm_vcpu *vcpu; 630 struct its_ite *ite; 631 632 if (!its->enabled) 633 return -EBUSY; 634 635 ite = find_ite(its, devid, eventid); 636 if (!ite || !its_is_collection_mapped(ite->collection)) 637 return E_ITS_INT_UNMAPPED_INTERRUPT; 638 639 vcpu = collection_to_vcpu(kvm, ite->collection); 640 if (!vcpu) 641 return E_ITS_INT_UNMAPPED_INTERRUPT; 642 643 if (!vgic_lpis_enabled(vcpu)) 644 return -EBUSY; 645 646 vgic_its_cache_translation(kvm, its, devid, eventid, ite->irq); 647 648 *irq = ite->irq; 649 return 0; 650 } 651 652 struct vgic_its *vgic_msi_to_its(struct kvm *kvm, struct kvm_msi *msi) 653 { 654 u64 address; 655 656 if (!vgic_has_its(kvm)) 657 return ERR_PTR(-ENODEV); 658 659 if (!(msi->flags & KVM_MSI_VALID_DEVID)) 660 return ERR_PTR(-EINVAL); 661 662 address = (u64)msi->address_hi << 32 | msi->address_lo; 663 664 return __vgic_doorbell_to_its(kvm, address); 665 } 666 667 /* 668 * Find the target VCPU and the LPI number for a given devid/eventid pair 669 * and make this IRQ pending, possibly injecting it. 670 * Must be called with the its_lock mutex held. 671 * Returns 0 on success, a positive error value for any ITS mapping 672 * related errors and negative error values for generic errors. 673 */ 674 static int vgic_its_trigger_msi(struct kvm *kvm, struct vgic_its *its, 675 u32 devid, u32 eventid) 676 { 677 struct vgic_irq *irq = NULL; 678 unsigned long flags; 679 int err; 680 681 err = vgic_its_resolve_lpi(kvm, its, devid, eventid, &irq); 682 if (err) 683 return err; 684 685 if (irq->hw) 686 return irq_set_irqchip_state(irq->host_irq, 687 IRQCHIP_STATE_PENDING, true); 688 689 raw_spin_lock_irqsave(&irq->irq_lock, flags); 690 irq->pending_latch = true; 691 vgic_queue_irq_unlock(kvm, irq, flags); 692 693 return 0; 694 } 695 696 int vgic_its_inject_cached_translation(struct kvm *kvm, struct kvm_msi *msi) 697 { 698 struct vgic_irq *irq; 699 unsigned long flags; 700 phys_addr_t db; 701 702 db = (u64)msi->address_hi << 32 | msi->address_lo; 703 irq = vgic_its_check_cache(kvm, db, msi->devid, msi->data); 704 if (!irq) 705 return -EWOULDBLOCK; 706 707 raw_spin_lock_irqsave(&irq->irq_lock, flags); 708 irq->pending_latch = true; 709 vgic_queue_irq_unlock(kvm, irq, flags); 710 vgic_put_irq(kvm, irq); 711 712 return 0; 713 } 714 715 /* 716 * Queries the KVM IO bus framework to get the ITS pointer from the given 717 * doorbell address. 718 * We then call vgic_its_trigger_msi() with the decoded data. 719 * According to the KVM_SIGNAL_MSI API description returns 1 on success. 720 */ 721 int vgic_its_inject_msi(struct kvm *kvm, struct kvm_msi *msi) 722 { 723 struct vgic_its *its; 724 int ret; 725 726 if (!vgic_its_inject_cached_translation(kvm, msi)) 727 return 1; 728 729 its = vgic_msi_to_its(kvm, msi); 730 if (IS_ERR(its)) 731 return PTR_ERR(its); 732 733 mutex_lock(&its->its_lock); 734 ret = vgic_its_trigger_msi(kvm, its, msi->devid, msi->data); 735 mutex_unlock(&its->its_lock); 736 737 if (ret < 0) 738 return ret; 739 740 /* 741 * KVM_SIGNAL_MSI demands a return value > 0 for success and 0 742 * if the guest has blocked the MSI. So we map any LPI mapping 743 * related error to that. 744 */ 745 if (ret) 746 return 0; 747 else 748 return 1; 749 } 750 751 /* Requires the its_lock to be held. */ 752 static void its_free_ite(struct kvm *kvm, struct its_ite *ite) 753 { 754 struct vgic_irq *irq = ite->irq; 755 list_del(&ite->ite_list); 756 757 /* This put matches the get in vgic_add_lpi. */ 758 if (irq) { 759 scoped_guard(raw_spinlock_irqsave, &irq->irq_lock) { 760 if (irq->hw) 761 its_unmap_vlpi(ite->irq->host_irq); 762 763 irq->hw = false; 764 } 765 766 vgic_put_irq(kvm, ite->irq); 767 } 768 769 kfree(ite); 770 } 771 772 static u64 its_cmd_mask_field(u64 *its_cmd, int word, int shift, int size) 773 { 774 return (le64_to_cpu(its_cmd[word]) >> shift) & (BIT_ULL(size) - 1); 775 } 776 777 #define its_cmd_get_command(cmd) its_cmd_mask_field(cmd, 0, 0, 8) 778 #define its_cmd_get_deviceid(cmd) its_cmd_mask_field(cmd, 0, 32, 32) 779 #define its_cmd_get_size(cmd) (its_cmd_mask_field(cmd, 1, 0, 5) + 1) 780 #define its_cmd_get_id(cmd) its_cmd_mask_field(cmd, 1, 0, 32) 781 #define its_cmd_get_physical_id(cmd) its_cmd_mask_field(cmd, 1, 32, 32) 782 #define its_cmd_get_collection(cmd) its_cmd_mask_field(cmd, 2, 0, 16) 783 #define its_cmd_get_ittaddr(cmd) (its_cmd_mask_field(cmd, 2, 8, 44) << 8) 784 #define its_cmd_get_target_addr(cmd) its_cmd_mask_field(cmd, 2, 16, 32) 785 #define its_cmd_get_validbit(cmd) its_cmd_mask_field(cmd, 2, 63, 1) 786 787 /* 788 * The DISCARD command frees an Interrupt Translation Table Entry (ITTE). 789 * Must be called with the its_lock mutex held. 790 */ 791 static int vgic_its_cmd_handle_discard(struct kvm *kvm, struct vgic_its *its, 792 u64 *its_cmd) 793 { 794 u32 device_id = its_cmd_get_deviceid(its_cmd); 795 u32 event_id = its_cmd_get_id(its_cmd); 796 struct its_ite *ite; 797 798 ite = find_ite(its, device_id, event_id); 799 if (ite && its_is_collection_mapped(ite->collection)) { 800 struct its_device *device = find_its_device(its, device_id); 801 int ite_esz = vgic_its_get_abi(its)->ite_esz; 802 gpa_t gpa = device->itt_addr + ite->event_id * ite_esz; 803 /* 804 * Though the spec talks about removing the pending state, we 805 * don't bother here since we clear the ITTE anyway and the 806 * pending state is a property of the ITTE struct. 807 */ 808 vgic_its_invalidate_cache(its); 809 810 its_free_ite(kvm, ite); 811 812 return vgic_its_write_entry_lock(its, gpa, 0ULL, ite); 813 } 814 815 return E_ITS_DISCARD_UNMAPPED_INTERRUPT; 816 } 817 818 /* 819 * The MOVI command moves an ITTE to a different collection. 820 * Must be called with the its_lock mutex held. 821 */ 822 static int vgic_its_cmd_handle_movi(struct kvm *kvm, struct vgic_its *its, 823 u64 *its_cmd) 824 { 825 u32 device_id = its_cmd_get_deviceid(its_cmd); 826 u32 event_id = its_cmd_get_id(its_cmd); 827 u32 coll_id = its_cmd_get_collection(its_cmd); 828 struct kvm_vcpu *vcpu; 829 struct its_ite *ite; 830 struct its_collection *collection; 831 832 ite = find_ite(its, device_id, event_id); 833 if (!ite) 834 return E_ITS_MOVI_UNMAPPED_INTERRUPT; 835 836 if (!its_is_collection_mapped(ite->collection)) 837 return E_ITS_MOVI_UNMAPPED_COLLECTION; 838 839 collection = find_collection(its, coll_id); 840 if (!its_is_collection_mapped(collection)) 841 return E_ITS_MOVI_UNMAPPED_COLLECTION; 842 843 ite->collection = collection; 844 vcpu = collection_to_vcpu(kvm, collection); 845 846 vgic_its_invalidate_cache(its); 847 848 return update_affinity(ite->irq, vcpu); 849 } 850 851 static bool __is_visible_gfn_locked(struct vgic_its *its, gpa_t gpa) 852 { 853 gfn_t gfn = gpa >> PAGE_SHIFT; 854 int idx; 855 bool ret; 856 857 idx = srcu_read_lock(&its->dev->kvm->srcu); 858 ret = kvm_is_visible_gfn(its->dev->kvm, gfn); 859 srcu_read_unlock(&its->dev->kvm->srcu, idx); 860 return ret; 861 } 862 863 /* 864 * Check whether an ID can be stored into the corresponding guest table. 865 * For a direct table this is pretty easy, but gets a bit nasty for 866 * indirect tables. We check whether the resulting guest physical address 867 * is actually valid (covered by a memslot and guest accessible). 868 * For this we have to read the respective first level entry. 869 */ 870 static bool vgic_its_check_id(struct vgic_its *its, u64 baser, u32 id, 871 gpa_t *eaddr) 872 { 873 int l1_tbl_size = GITS_BASER_NR_PAGES(baser) * SZ_64K; 874 u64 indirect_ptr, type = GITS_BASER_TYPE(baser); 875 phys_addr_t base = GITS_BASER_ADDR_48_to_52(baser); 876 int esz = GITS_BASER_ENTRY_SIZE(baser); 877 int index; 878 879 switch (type) { 880 case GITS_BASER_TYPE_DEVICE: 881 if (id > VITS_MAX_DEVID) 882 return false; 883 break; 884 case GITS_BASER_TYPE_COLLECTION: 885 /* as GITS_TYPER.CIL == 0, ITS supports 16-bit collection ID */ 886 if (id >= BIT_ULL(16)) 887 return false; 888 break; 889 default: 890 return false; 891 } 892 893 if (!(baser & GITS_BASER_INDIRECT)) { 894 phys_addr_t addr; 895 896 if (id >= (l1_tbl_size / esz)) 897 return false; 898 899 addr = base + id * esz; 900 901 if (eaddr) 902 *eaddr = addr; 903 904 return __is_visible_gfn_locked(its, addr); 905 } 906 907 /* calculate and check the index into the 1st level */ 908 index = id / (SZ_64K / esz); 909 if (index >= (l1_tbl_size / sizeof(u64))) 910 return false; 911 912 /* Each 1st level entry is represented by a 64-bit value. */ 913 if (kvm_read_guest_lock(its->dev->kvm, 914 base + index * sizeof(indirect_ptr), 915 &indirect_ptr, sizeof(indirect_ptr))) 916 return false; 917 918 indirect_ptr = le64_to_cpu(indirect_ptr); 919 920 /* check the valid bit of the first level entry */ 921 if (!(indirect_ptr & BIT_ULL(63))) 922 return false; 923 924 /* Mask the guest physical address and calculate the frame number. */ 925 indirect_ptr &= GENMASK_ULL(51, 16); 926 927 /* Find the address of the actual entry */ 928 index = id % (SZ_64K / esz); 929 indirect_ptr += index * esz; 930 931 if (eaddr) 932 *eaddr = indirect_ptr; 933 934 return __is_visible_gfn_locked(its, indirect_ptr); 935 } 936 937 /* 938 * Check whether an event ID can be stored in the corresponding Interrupt 939 * Translation Table, which starts at device->itt_addr. 940 */ 941 static bool vgic_its_check_event_id(struct vgic_its *its, struct its_device *device, 942 u32 event_id) 943 { 944 const struct vgic_its_abi *abi = vgic_its_get_abi(its); 945 int ite_esz = abi->ite_esz; 946 gpa_t gpa; 947 948 /* max table size is: BIT_ULL(device->num_eventid_bits) * ite_esz */ 949 if (event_id >= BIT_ULL(device->num_eventid_bits)) 950 return false; 951 952 gpa = device->itt_addr + event_id * ite_esz; 953 return __is_visible_gfn_locked(its, gpa); 954 } 955 956 /* 957 * Add a new collection into the ITS collection table. 958 * Returns 0 on success, and a negative error value for generic errors. 959 */ 960 static int vgic_its_alloc_collection(struct vgic_its *its, 961 struct its_collection **colp, 962 u32 coll_id) 963 { 964 struct its_collection *collection; 965 966 collection = kzalloc_obj(*collection, GFP_KERNEL_ACCOUNT); 967 if (!collection) 968 return -ENOMEM; 969 970 collection->collection_id = coll_id; 971 collection->target_addr = COLLECTION_NOT_MAPPED; 972 973 list_add_tail(&collection->coll_list, &its->collection_list); 974 *colp = collection; 975 976 return 0; 977 } 978 979 static void vgic_its_free_collection(struct vgic_its *its, u32 coll_id) 980 { 981 struct its_collection *collection; 982 struct its_device *device; 983 struct its_ite *ite; 984 985 /* 986 * Clearing the mapping for that collection ID removes the 987 * entry from the list. If there wasn't any before, we can 988 * go home early. 989 */ 990 collection = find_collection(its, coll_id); 991 if (!collection) 992 return; 993 994 for_each_lpi_its(device, ite, its) 995 if (ite->collection && 996 ite->collection->collection_id == coll_id) 997 ite->collection = NULL; 998 999 list_del(&collection->coll_list); 1000 kfree(collection); 1001 } 1002 1003 /* Must be called with its_lock mutex held */ 1004 static struct its_ite *vgic_its_alloc_ite(struct its_device *device, 1005 struct its_collection *collection, 1006 u32 event_id) 1007 { 1008 struct its_ite *ite; 1009 1010 ite = kzalloc_obj(*ite, GFP_KERNEL_ACCOUNT); 1011 if (!ite) 1012 return ERR_PTR(-ENOMEM); 1013 1014 ite->event_id = event_id; 1015 ite->collection = collection; 1016 1017 list_add_tail(&ite->ite_list, &device->itt_head); 1018 return ite; 1019 } 1020 1021 /* 1022 * The MAPTI and MAPI commands map LPIs to ITTEs. 1023 * Must be called with its_lock mutex held. 1024 */ 1025 static int vgic_its_cmd_handle_mapi(struct kvm *kvm, struct vgic_its *its, 1026 u64 *its_cmd) 1027 { 1028 u32 device_id = its_cmd_get_deviceid(its_cmd); 1029 u32 event_id = its_cmd_get_id(its_cmd); 1030 u32 coll_id = its_cmd_get_collection(its_cmd); 1031 struct its_ite *ite; 1032 struct kvm_vcpu *vcpu = NULL; 1033 struct its_device *device; 1034 struct its_collection *collection, *new_coll = NULL; 1035 struct vgic_irq *irq; 1036 int lpi_nr; 1037 1038 device = find_its_device(its, device_id); 1039 if (!device) 1040 return E_ITS_MAPTI_UNMAPPED_DEVICE; 1041 1042 if (!vgic_its_check_event_id(its, device, event_id)) 1043 return E_ITS_MAPTI_ID_OOR; 1044 1045 if (its_cmd_get_command(its_cmd) == GITS_CMD_MAPTI) 1046 lpi_nr = its_cmd_get_physical_id(its_cmd); 1047 else 1048 lpi_nr = event_id; 1049 if (lpi_nr < GIC_LPI_OFFSET || 1050 lpi_nr >= max_lpis_propbaser(kvm->arch.vgic.propbaser)) 1051 return E_ITS_MAPTI_PHYSICALID_OOR; 1052 1053 /* If there is an existing mapping, behavior is UNPREDICTABLE. */ 1054 if (find_ite(its, device_id, event_id)) 1055 return 0; 1056 1057 collection = find_collection(its, coll_id); 1058 if (!collection) { 1059 int ret; 1060 1061 if (!vgic_its_check_id(its, its->baser_coll_table, coll_id, NULL)) 1062 return E_ITS_MAPC_COLLECTION_OOR; 1063 1064 ret = vgic_its_alloc_collection(its, &collection, coll_id); 1065 if (ret) 1066 return ret; 1067 new_coll = collection; 1068 } 1069 1070 ite = vgic_its_alloc_ite(device, collection, event_id); 1071 if (IS_ERR(ite)) { 1072 if (new_coll) 1073 vgic_its_free_collection(its, coll_id); 1074 return PTR_ERR(ite); 1075 } 1076 1077 if (its_is_collection_mapped(collection)) 1078 vcpu = collection_to_vcpu(kvm, collection); 1079 1080 irq = vgic_add_lpi(kvm, lpi_nr, vcpu); 1081 if (IS_ERR(irq)) { 1082 if (new_coll) 1083 vgic_its_free_collection(its, coll_id); 1084 its_free_ite(kvm, ite); 1085 return PTR_ERR(irq); 1086 } 1087 ite->irq = irq; 1088 1089 return 0; 1090 } 1091 1092 /* Requires the its_lock to be held. */ 1093 static void vgic_its_free_device(struct kvm *kvm, struct vgic_its *its, 1094 struct its_device *device) 1095 { 1096 struct its_ite *ite, *temp; 1097 1098 /* 1099 * The spec says that unmapping a device with still valid 1100 * ITTEs associated is UNPREDICTABLE. We remove all ITTEs, 1101 * since we cannot leave the memory unreferenced. 1102 */ 1103 list_for_each_entry_safe(ite, temp, &device->itt_head, ite_list) 1104 its_free_ite(kvm, ite); 1105 1106 vgic_its_invalidate_cache(its); 1107 1108 list_del(&device->dev_list); 1109 kfree(device); 1110 } 1111 1112 /* its lock must be held */ 1113 static void vgic_its_free_device_list(struct kvm *kvm, struct vgic_its *its) 1114 { 1115 struct its_device *cur, *temp; 1116 1117 list_for_each_entry_safe(cur, temp, &its->device_list, dev_list) 1118 vgic_its_free_device(kvm, its, cur); 1119 } 1120 1121 /* its lock must be held */ 1122 static void vgic_its_free_collection_list(struct kvm *kvm, struct vgic_its *its) 1123 { 1124 struct its_collection *cur, *temp; 1125 1126 list_for_each_entry_safe(cur, temp, &its->collection_list, coll_list) 1127 vgic_its_free_collection(its, cur->collection_id); 1128 } 1129 1130 /* Must be called with its_lock mutex held */ 1131 static struct its_device *vgic_its_alloc_device(struct vgic_its *its, 1132 u32 device_id, gpa_t itt_addr, 1133 u8 num_eventid_bits) 1134 { 1135 struct its_device *device; 1136 1137 device = kzalloc_obj(*device, GFP_KERNEL_ACCOUNT); 1138 if (!device) 1139 return ERR_PTR(-ENOMEM); 1140 1141 device->device_id = device_id; 1142 device->itt_addr = itt_addr; 1143 device->num_eventid_bits = num_eventid_bits; 1144 INIT_LIST_HEAD(&device->itt_head); 1145 1146 list_add_tail(&device->dev_list, &its->device_list); 1147 return device; 1148 } 1149 1150 /* 1151 * MAPD maps or unmaps a device ID to Interrupt Translation Tables (ITTs). 1152 * Must be called with the its_lock mutex held. 1153 */ 1154 static int vgic_its_cmd_handle_mapd(struct kvm *kvm, struct vgic_its *its, 1155 u64 *its_cmd) 1156 { 1157 u32 device_id = its_cmd_get_deviceid(its_cmd); 1158 bool valid = its_cmd_get_validbit(its_cmd); 1159 u8 num_eventid_bits = its_cmd_get_size(its_cmd); 1160 gpa_t itt_addr = its_cmd_get_ittaddr(its_cmd); 1161 struct its_device *device; 1162 gpa_t gpa; 1163 1164 if (!vgic_its_check_id(its, its->baser_device_table, device_id, &gpa)) 1165 return E_ITS_MAPD_DEVICE_OOR; 1166 1167 if (valid && num_eventid_bits > VITS_TYPER_IDBITS) 1168 return E_ITS_MAPD_ITTSIZE_OOR; 1169 1170 device = find_its_device(its, device_id); 1171 1172 /* 1173 * The spec says that calling MAPD on an already mapped device 1174 * invalidates all cached data for this device. We implement this 1175 * by removing the mapping and re-establishing it. 1176 */ 1177 if (device) 1178 vgic_its_free_device(kvm, its, device); 1179 1180 /* 1181 * The spec does not say whether unmapping a not-mapped device 1182 * is an error, so we are done in any case. 1183 */ 1184 if (!valid) 1185 return vgic_its_write_entry_lock(its, gpa, 0ULL, dte); 1186 1187 device = vgic_its_alloc_device(its, device_id, itt_addr, 1188 num_eventid_bits); 1189 1190 return PTR_ERR_OR_ZERO(device); 1191 } 1192 1193 /* 1194 * The MAPC command maps collection IDs to redistributors. 1195 * Must be called with the its_lock mutex held. 1196 */ 1197 static int vgic_its_cmd_handle_mapc(struct kvm *kvm, struct vgic_its *its, 1198 u64 *its_cmd) 1199 { 1200 u16 coll_id; 1201 struct its_collection *collection; 1202 bool valid; 1203 1204 valid = its_cmd_get_validbit(its_cmd); 1205 coll_id = its_cmd_get_collection(its_cmd); 1206 1207 if (!valid) { 1208 vgic_its_free_collection(its, coll_id); 1209 vgic_its_invalidate_cache(its); 1210 } else { 1211 struct kvm_vcpu *vcpu; 1212 1213 vcpu = kvm_get_vcpu_by_id(kvm, its_cmd_get_target_addr(its_cmd)); 1214 if (!vcpu) 1215 return E_ITS_MAPC_PROCNUM_OOR; 1216 1217 collection = find_collection(its, coll_id); 1218 1219 if (!collection) { 1220 int ret; 1221 1222 if (!vgic_its_check_id(its, its->baser_coll_table, 1223 coll_id, NULL)) 1224 return E_ITS_MAPC_COLLECTION_OOR; 1225 1226 ret = vgic_its_alloc_collection(its, &collection, 1227 coll_id); 1228 if (ret) 1229 return ret; 1230 collection->target_addr = vcpu->vcpu_id; 1231 } else { 1232 collection->target_addr = vcpu->vcpu_id; 1233 update_affinity_collection(kvm, its, collection); 1234 } 1235 } 1236 1237 return 0; 1238 } 1239 1240 /* 1241 * The CLEAR command removes the pending state for a particular LPI. 1242 * Must be called with the its_lock mutex held. 1243 */ 1244 static int vgic_its_cmd_handle_clear(struct kvm *kvm, struct vgic_its *its, 1245 u64 *its_cmd) 1246 { 1247 u32 device_id = its_cmd_get_deviceid(its_cmd); 1248 u32 event_id = its_cmd_get_id(its_cmd); 1249 struct its_ite *ite; 1250 1251 1252 ite = find_ite(its, device_id, event_id); 1253 if (!ite) 1254 return E_ITS_CLEAR_UNMAPPED_INTERRUPT; 1255 1256 ite->irq->pending_latch = false; 1257 1258 if (ite->irq->hw) 1259 return irq_set_irqchip_state(ite->irq->host_irq, 1260 IRQCHIP_STATE_PENDING, false); 1261 1262 return 0; 1263 } 1264 1265 int vgic_its_inv_lpi(struct kvm *kvm, struct vgic_irq *irq) 1266 { 1267 return update_lpi_config(kvm, irq, NULL, true); 1268 } 1269 1270 /* 1271 * The INV command syncs the configuration bits from the memory table. 1272 * Must be called with the its_lock mutex held. 1273 */ 1274 static int vgic_its_cmd_handle_inv(struct kvm *kvm, struct vgic_its *its, 1275 u64 *its_cmd) 1276 { 1277 u32 device_id = its_cmd_get_deviceid(its_cmd); 1278 u32 event_id = its_cmd_get_id(its_cmd); 1279 struct its_ite *ite; 1280 1281 1282 ite = find_ite(its, device_id, event_id); 1283 if (!ite) 1284 return E_ITS_INV_UNMAPPED_INTERRUPT; 1285 1286 return vgic_its_inv_lpi(kvm, ite->irq); 1287 } 1288 1289 /** 1290 * vgic_its_invall - invalidate all LPIs targeting a given vcpu 1291 * @vcpu: the vcpu for which the RD is targeted by an invalidation 1292 * 1293 * Contrary to the INVALL command, this targets a RD instead of a 1294 * collection, and we don't need to hold the its_lock, since no ITS is 1295 * involved here. 1296 */ 1297 int vgic_its_invall(struct kvm_vcpu *vcpu) 1298 { 1299 struct kvm *kvm = vcpu->kvm; 1300 struct vgic_dist *dist = &kvm->arch.vgic; 1301 struct vgic_irq *irq; 1302 unsigned long intid; 1303 1304 xa_for_each(&dist->lpi_xa, intid, irq) { 1305 irq = vgic_get_irq(kvm, intid); 1306 if (!irq) 1307 continue; 1308 1309 update_lpi_config(kvm, irq, vcpu, false); 1310 vgic_put_irq(kvm, irq); 1311 } 1312 1313 if (vcpu->arch.vgic_cpu.vgic_v3.its_vpe.its_vm) 1314 its_invall_vpe(&vcpu->arch.vgic_cpu.vgic_v3.its_vpe); 1315 1316 return 0; 1317 } 1318 1319 /* 1320 * The INVALL command requests flushing of all IRQ data in this collection. 1321 * Find the VCPU mapped to that collection, then iterate over the VM's list 1322 * of mapped LPIs and update the configuration for each IRQ which targets 1323 * the specified vcpu. The configuration will be read from the in-memory 1324 * configuration table. 1325 * Must be called with the its_lock mutex held. 1326 */ 1327 static int vgic_its_cmd_handle_invall(struct kvm *kvm, struct vgic_its *its, 1328 u64 *its_cmd) 1329 { 1330 u32 coll_id = its_cmd_get_collection(its_cmd); 1331 struct its_collection *collection; 1332 struct kvm_vcpu *vcpu; 1333 1334 collection = find_collection(its, coll_id); 1335 if (!its_is_collection_mapped(collection)) 1336 return E_ITS_INVALL_UNMAPPED_COLLECTION; 1337 1338 vcpu = collection_to_vcpu(kvm, collection); 1339 vgic_its_invall(vcpu); 1340 1341 return 0; 1342 } 1343 1344 /* 1345 * The MOVALL command moves the pending state of all IRQs targeting one 1346 * redistributor to another. We don't hold the pending state in the VCPUs, 1347 * but in the IRQs instead, so there is really not much to do for us here. 1348 * However the spec says that no IRQ must target the old redistributor 1349 * afterwards, so we make sure that no LPI is using the associated target_vcpu. 1350 * This command affects all LPIs in the system that target that redistributor. 1351 */ 1352 static int vgic_its_cmd_handle_movall(struct kvm *kvm, struct vgic_its *its, 1353 u64 *its_cmd) 1354 { 1355 struct vgic_dist *dist = &kvm->arch.vgic; 1356 struct kvm_vcpu *vcpu1, *vcpu2; 1357 struct vgic_irq *irq; 1358 unsigned long intid; 1359 1360 /* We advertise GITS_TYPER.PTA==0, making the address the vcpu ID */ 1361 vcpu1 = kvm_get_vcpu_by_id(kvm, its_cmd_get_target_addr(its_cmd)); 1362 vcpu2 = kvm_get_vcpu_by_id(kvm, its_cmd_mask_field(its_cmd, 3, 16, 32)); 1363 1364 if (!vcpu1 || !vcpu2) 1365 return E_ITS_MOVALL_PROCNUM_OOR; 1366 1367 if (vcpu1 == vcpu2) 1368 return 0; 1369 1370 xa_for_each(&dist->lpi_xa, intid, irq) { 1371 irq = vgic_get_irq(kvm, intid); 1372 if (!irq) 1373 continue; 1374 1375 update_affinity(irq, vcpu2); 1376 1377 vgic_put_irq(kvm, irq); 1378 } 1379 1380 vgic_its_invalidate_cache(its); 1381 1382 return 0; 1383 } 1384 1385 /* 1386 * The INT command injects the LPI associated with that DevID/EvID pair. 1387 * Must be called with the its_lock mutex held. 1388 */ 1389 static int vgic_its_cmd_handle_int(struct kvm *kvm, struct vgic_its *its, 1390 u64 *its_cmd) 1391 { 1392 u32 msi_data = its_cmd_get_id(its_cmd); 1393 u64 msi_devid = its_cmd_get_deviceid(its_cmd); 1394 1395 return vgic_its_trigger_msi(kvm, its, msi_devid, msi_data); 1396 } 1397 1398 /* 1399 * This function is called with the its_cmd lock held, but the ITS data 1400 * structure lock dropped. 1401 */ 1402 static int vgic_its_handle_command(struct kvm *kvm, struct vgic_its *its, 1403 u64 *its_cmd) 1404 { 1405 int ret = -ENODEV; 1406 1407 mutex_lock(&its->its_lock); 1408 switch (its_cmd_get_command(its_cmd)) { 1409 case GITS_CMD_MAPD: 1410 ret = vgic_its_cmd_handle_mapd(kvm, its, its_cmd); 1411 break; 1412 case GITS_CMD_MAPC: 1413 ret = vgic_its_cmd_handle_mapc(kvm, its, its_cmd); 1414 break; 1415 case GITS_CMD_MAPI: 1416 ret = vgic_its_cmd_handle_mapi(kvm, its, its_cmd); 1417 break; 1418 case GITS_CMD_MAPTI: 1419 ret = vgic_its_cmd_handle_mapi(kvm, its, its_cmd); 1420 break; 1421 case GITS_CMD_MOVI: 1422 ret = vgic_its_cmd_handle_movi(kvm, its, its_cmd); 1423 break; 1424 case GITS_CMD_DISCARD: 1425 ret = vgic_its_cmd_handle_discard(kvm, its, its_cmd); 1426 break; 1427 case GITS_CMD_CLEAR: 1428 ret = vgic_its_cmd_handle_clear(kvm, its, its_cmd); 1429 break; 1430 case GITS_CMD_MOVALL: 1431 ret = vgic_its_cmd_handle_movall(kvm, its, its_cmd); 1432 break; 1433 case GITS_CMD_INT: 1434 ret = vgic_its_cmd_handle_int(kvm, its, its_cmd); 1435 break; 1436 case GITS_CMD_INV: 1437 ret = vgic_its_cmd_handle_inv(kvm, its, its_cmd); 1438 break; 1439 case GITS_CMD_INVALL: 1440 ret = vgic_its_cmd_handle_invall(kvm, its, its_cmd); 1441 break; 1442 case GITS_CMD_SYNC: 1443 /* we ignore this command: we are in sync all of the time */ 1444 ret = 0; 1445 break; 1446 } 1447 mutex_unlock(&its->its_lock); 1448 1449 return ret; 1450 } 1451 1452 static u64 vgic_sanitise_its_baser(u64 reg) 1453 { 1454 reg = vgic_sanitise_field(reg, GITS_BASER_SHAREABILITY_MASK, 1455 GITS_BASER_SHAREABILITY_SHIFT, 1456 vgic_sanitise_shareability); 1457 reg = vgic_sanitise_field(reg, GITS_BASER_INNER_CACHEABILITY_MASK, 1458 GITS_BASER_INNER_CACHEABILITY_SHIFT, 1459 vgic_sanitise_inner_cacheability); 1460 reg = vgic_sanitise_field(reg, GITS_BASER_OUTER_CACHEABILITY_MASK, 1461 GITS_BASER_OUTER_CACHEABILITY_SHIFT, 1462 vgic_sanitise_outer_cacheability); 1463 1464 /* We support only one (ITS) page size: 64K */ 1465 reg = (reg & ~GITS_BASER_PAGE_SIZE_MASK) | GITS_BASER_PAGE_SIZE_64K; 1466 1467 return reg; 1468 } 1469 1470 static u64 vgic_sanitise_its_cbaser(u64 reg) 1471 { 1472 reg = vgic_sanitise_field(reg, GITS_CBASER_SHAREABILITY_MASK, 1473 GITS_CBASER_SHAREABILITY_SHIFT, 1474 vgic_sanitise_shareability); 1475 reg = vgic_sanitise_field(reg, GITS_CBASER_INNER_CACHEABILITY_MASK, 1476 GITS_CBASER_INNER_CACHEABILITY_SHIFT, 1477 vgic_sanitise_inner_cacheability); 1478 reg = vgic_sanitise_field(reg, GITS_CBASER_OUTER_CACHEABILITY_MASK, 1479 GITS_CBASER_OUTER_CACHEABILITY_SHIFT, 1480 vgic_sanitise_outer_cacheability); 1481 1482 /* Sanitise the physical address to be 64k aligned. */ 1483 reg &= ~GENMASK_ULL(15, 12); 1484 1485 return reg; 1486 } 1487 1488 static unsigned long vgic_mmio_read_its_cbaser(struct kvm *kvm, 1489 struct vgic_its *its, 1490 gpa_t addr, unsigned int len) 1491 { 1492 return extract_bytes(its->cbaser, addr & 7, len); 1493 } 1494 1495 static void vgic_mmio_write_its_cbaser(struct kvm *kvm, struct vgic_its *its, 1496 gpa_t addr, unsigned int len, 1497 unsigned long val) 1498 { 1499 /* When GITS_CTLR.Enable is 1, this register is RO. */ 1500 if (its->enabled) 1501 return; 1502 1503 mutex_lock(&its->cmd_lock); 1504 its->cbaser = update_64bit_reg(its->cbaser, addr & 7, len, val); 1505 its->cbaser = vgic_sanitise_its_cbaser(its->cbaser); 1506 its->creadr = 0; 1507 /* 1508 * CWRITER is architecturally UNKNOWN on reset, but we need to reset 1509 * it to CREADR to make sure we start with an empty command buffer. 1510 */ 1511 its->cwriter = its->creadr; 1512 mutex_unlock(&its->cmd_lock); 1513 } 1514 1515 #define ITS_CMD_BUFFER_SIZE(baser) ((((baser) & 0xff) + 1) << 12) 1516 #define ITS_CMD_SIZE 32 1517 #define ITS_CMD_OFFSET(reg) ((reg) & GENMASK(19, 5)) 1518 1519 /* Must be called with the cmd_lock held. */ 1520 static void vgic_its_process_commands(struct kvm *kvm, struct vgic_its *its) 1521 { 1522 gpa_t cbaser; 1523 u64 cmd_buf[4]; 1524 1525 /* Commands are only processed when the ITS is enabled. */ 1526 if (!its->enabled) 1527 return; 1528 1529 cbaser = GITS_CBASER_ADDRESS(its->cbaser); 1530 1531 while (its->cwriter != its->creadr) { 1532 int ret = kvm_read_guest_lock(kvm, cbaser + its->creadr, 1533 cmd_buf, ITS_CMD_SIZE); 1534 /* 1535 * If kvm_read_guest() fails, this could be due to the guest 1536 * programming a bogus value in CBASER or something else going 1537 * wrong from which we cannot easily recover. 1538 * According to section 6.3.2 in the GICv3 spec we can just 1539 * ignore that command then. 1540 */ 1541 if (!ret) 1542 vgic_its_handle_command(kvm, its, cmd_buf); 1543 1544 its->creadr += ITS_CMD_SIZE; 1545 if (its->creadr == ITS_CMD_BUFFER_SIZE(its->cbaser)) 1546 its->creadr = 0; 1547 } 1548 } 1549 1550 /* 1551 * By writing to CWRITER the guest announces new commands to be processed. 1552 * To avoid any races in the first place, we take the its_cmd lock, which 1553 * protects our ring buffer variables, so that there is only one user 1554 * per ITS handling commands at a given time. 1555 */ 1556 static void vgic_mmio_write_its_cwriter(struct kvm *kvm, struct vgic_its *its, 1557 gpa_t addr, unsigned int len, 1558 unsigned long val) 1559 { 1560 u64 reg; 1561 1562 if (!its) 1563 return; 1564 1565 mutex_lock(&its->cmd_lock); 1566 1567 reg = update_64bit_reg(its->cwriter, addr & 7, len, val); 1568 reg = ITS_CMD_OFFSET(reg); 1569 if (reg >= ITS_CMD_BUFFER_SIZE(its->cbaser)) { 1570 mutex_unlock(&its->cmd_lock); 1571 return; 1572 } 1573 its->cwriter = reg; 1574 1575 vgic_its_process_commands(kvm, its); 1576 1577 mutex_unlock(&its->cmd_lock); 1578 } 1579 1580 static unsigned long vgic_mmio_read_its_cwriter(struct kvm *kvm, 1581 struct vgic_its *its, 1582 gpa_t addr, unsigned int len) 1583 { 1584 return extract_bytes(its->cwriter, addr & 0x7, len); 1585 } 1586 1587 static unsigned long vgic_mmio_read_its_creadr(struct kvm *kvm, 1588 struct vgic_its *its, 1589 gpa_t addr, unsigned int len) 1590 { 1591 return extract_bytes(its->creadr, addr & 0x7, len); 1592 } 1593 1594 static int vgic_mmio_uaccess_write_its_creadr(struct kvm *kvm, 1595 struct vgic_its *its, 1596 gpa_t addr, unsigned int len, 1597 unsigned long val) 1598 { 1599 u32 cmd_offset; 1600 int ret = 0; 1601 1602 mutex_lock(&its->cmd_lock); 1603 1604 if (its->enabled) { 1605 ret = -EBUSY; 1606 goto out; 1607 } 1608 1609 cmd_offset = ITS_CMD_OFFSET(val); 1610 if (cmd_offset >= ITS_CMD_BUFFER_SIZE(its->cbaser)) { 1611 ret = -EINVAL; 1612 goto out; 1613 } 1614 1615 its->creadr = cmd_offset; 1616 out: 1617 mutex_unlock(&its->cmd_lock); 1618 return ret; 1619 } 1620 1621 #define BASER_INDEX(addr) (((addr) / sizeof(u64)) & 0x7) 1622 static unsigned long vgic_mmio_read_its_baser(struct kvm *kvm, 1623 struct vgic_its *its, 1624 gpa_t addr, unsigned int len) 1625 { 1626 u64 reg; 1627 1628 switch (BASER_INDEX(addr)) { 1629 case 0: 1630 reg = its->baser_device_table; 1631 break; 1632 case 1: 1633 reg = its->baser_coll_table; 1634 break; 1635 default: 1636 reg = 0; 1637 break; 1638 } 1639 1640 return extract_bytes(reg, addr & 7, len); 1641 } 1642 1643 #define GITS_BASER_RO_MASK (GENMASK_ULL(52, 48) | GENMASK_ULL(58, 56)) 1644 static void vgic_mmio_write_its_baser(struct kvm *kvm, 1645 struct vgic_its *its, 1646 gpa_t addr, unsigned int len, 1647 unsigned long val) 1648 { 1649 const struct vgic_its_abi *abi = vgic_its_get_abi(its); 1650 u64 entry_size, table_type; 1651 u64 reg, *regptr, clearbits = 0; 1652 1653 /* When GITS_CTLR.Enable is 1, we ignore write accesses. */ 1654 if (its->enabled) 1655 return; 1656 1657 switch (BASER_INDEX(addr)) { 1658 case 0: 1659 regptr = &its->baser_device_table; 1660 entry_size = abi->dte_esz; 1661 table_type = GITS_BASER_TYPE_DEVICE; 1662 break; 1663 case 1: 1664 regptr = &its->baser_coll_table; 1665 entry_size = abi->cte_esz; 1666 table_type = GITS_BASER_TYPE_COLLECTION; 1667 clearbits = GITS_BASER_INDIRECT; 1668 break; 1669 default: 1670 return; 1671 } 1672 1673 reg = update_64bit_reg(*regptr, addr & 7, len, val); 1674 reg &= ~GITS_BASER_RO_MASK; 1675 reg &= ~clearbits; 1676 1677 reg |= (entry_size - 1) << GITS_BASER_ENTRY_SIZE_SHIFT; 1678 reg |= table_type << GITS_BASER_TYPE_SHIFT; 1679 reg = vgic_sanitise_its_baser(reg); 1680 1681 *regptr = reg; 1682 1683 if (!(reg & GITS_BASER_VALID)) { 1684 /* Take the its_lock to prevent a race with a save/restore */ 1685 mutex_lock(&its->its_lock); 1686 switch (table_type) { 1687 case GITS_BASER_TYPE_DEVICE: 1688 vgic_its_free_device_list(kvm, its); 1689 break; 1690 case GITS_BASER_TYPE_COLLECTION: 1691 vgic_its_free_collection_list(kvm, its); 1692 break; 1693 } 1694 mutex_unlock(&its->its_lock); 1695 } 1696 } 1697 1698 static unsigned long vgic_mmio_read_its_ctlr(struct kvm *vcpu, 1699 struct vgic_its *its, 1700 gpa_t addr, unsigned int len) 1701 { 1702 u32 reg = 0; 1703 1704 mutex_lock(&its->cmd_lock); 1705 if (its->creadr == its->cwriter) 1706 reg |= GITS_CTLR_QUIESCENT; 1707 if (its->enabled) 1708 reg |= GITS_CTLR_ENABLE; 1709 mutex_unlock(&its->cmd_lock); 1710 1711 return reg; 1712 } 1713 1714 static void vgic_mmio_write_its_ctlr(struct kvm *kvm, struct vgic_its *its, 1715 gpa_t addr, unsigned int len, 1716 unsigned long val) 1717 { 1718 mutex_lock(&its->cmd_lock); 1719 1720 /* 1721 * It is UNPREDICTABLE to enable the ITS if any of the CBASER or 1722 * device/collection BASER are invalid 1723 */ 1724 if (!its->enabled && (val & GITS_CTLR_ENABLE) && 1725 (!(its->baser_device_table & GITS_BASER_VALID) || 1726 !(its->baser_coll_table & GITS_BASER_VALID) || 1727 !(its->cbaser & GITS_CBASER_VALID))) 1728 goto out; 1729 1730 its->enabled = !!(val & GITS_CTLR_ENABLE); 1731 if (!its->enabled) 1732 vgic_its_invalidate_cache(its); 1733 1734 /* 1735 * Try to process any pending commands. This function bails out early 1736 * if the ITS is disabled or no commands have been queued. 1737 */ 1738 vgic_its_process_commands(kvm, its); 1739 1740 out: 1741 mutex_unlock(&its->cmd_lock); 1742 } 1743 1744 #define REGISTER_ITS_DESC(off, rd, wr, length, acc) \ 1745 { \ 1746 .reg_offset = off, \ 1747 .len = length, \ 1748 .access_flags = acc, \ 1749 .its_read = rd, \ 1750 .its_write = wr, \ 1751 } 1752 1753 #define REGISTER_ITS_DESC_UACCESS(off, rd, wr, uwr, length, acc)\ 1754 { \ 1755 .reg_offset = off, \ 1756 .len = length, \ 1757 .access_flags = acc, \ 1758 .its_read = rd, \ 1759 .its_write = wr, \ 1760 .uaccess_its_write = uwr, \ 1761 } 1762 1763 static void its_mmio_write_wi(struct kvm *kvm, struct vgic_its *its, 1764 gpa_t addr, unsigned int len, unsigned long val) 1765 { 1766 /* Ignore */ 1767 } 1768 1769 static struct vgic_register_region its_registers[] = { 1770 REGISTER_ITS_DESC(GITS_CTLR, 1771 vgic_mmio_read_its_ctlr, vgic_mmio_write_its_ctlr, 4, 1772 VGIC_ACCESS_32bit), 1773 REGISTER_ITS_DESC_UACCESS(GITS_IIDR, 1774 vgic_mmio_read_its_iidr, its_mmio_write_wi, 1775 vgic_mmio_uaccess_write_its_iidr, 4, 1776 VGIC_ACCESS_32bit), 1777 REGISTER_ITS_DESC(GITS_TYPER, 1778 vgic_mmio_read_its_typer, its_mmio_write_wi, 8, 1779 VGIC_ACCESS_64bit | VGIC_ACCESS_32bit), 1780 REGISTER_ITS_DESC(GITS_CBASER, 1781 vgic_mmio_read_its_cbaser, vgic_mmio_write_its_cbaser, 8, 1782 VGIC_ACCESS_64bit | VGIC_ACCESS_32bit), 1783 REGISTER_ITS_DESC(GITS_CWRITER, 1784 vgic_mmio_read_its_cwriter, vgic_mmio_write_its_cwriter, 8, 1785 VGIC_ACCESS_64bit | VGIC_ACCESS_32bit), 1786 REGISTER_ITS_DESC_UACCESS(GITS_CREADR, 1787 vgic_mmio_read_its_creadr, its_mmio_write_wi, 1788 vgic_mmio_uaccess_write_its_creadr, 8, 1789 VGIC_ACCESS_64bit | VGIC_ACCESS_32bit), 1790 REGISTER_ITS_DESC(GITS_BASER, 1791 vgic_mmio_read_its_baser, vgic_mmio_write_its_baser, 0x40, 1792 VGIC_ACCESS_64bit | VGIC_ACCESS_32bit), 1793 REGISTER_ITS_DESC(GITS_IDREGS_BASE, 1794 vgic_mmio_read_its_idregs, its_mmio_write_wi, 0x30, 1795 VGIC_ACCESS_32bit), 1796 }; 1797 1798 /* This is called on setting the LPI enable bit in the redistributor. */ 1799 void vgic_enable_lpis(struct kvm_vcpu *vcpu) 1800 { 1801 if (!(vcpu->arch.vgic_cpu.pendbaser & GICR_PENDBASER_PTZ)) 1802 its_sync_lpi_pending_table(vcpu); 1803 } 1804 1805 static int vgic_register_its_iodev(struct kvm *kvm, struct vgic_its *its, 1806 u64 addr) 1807 { 1808 struct vgic_io_device *iodev = &its->iodev; 1809 int ret; 1810 1811 mutex_lock(&kvm->slots_lock); 1812 if (!IS_VGIC_ADDR_UNDEF(its->vgic_its_base)) { 1813 ret = -EBUSY; 1814 goto out; 1815 } 1816 1817 its->vgic_its_base = addr; 1818 iodev->regions = its_registers; 1819 iodev->nr_regions = ARRAY_SIZE(its_registers); 1820 kvm_iodevice_init(&iodev->dev, &kvm_io_gic_ops); 1821 1822 iodev->base_addr = its->vgic_its_base; 1823 iodev->iodev_type = IODEV_ITS; 1824 iodev->its = its; 1825 ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, iodev->base_addr, 1826 KVM_VGIC_V3_ITS_SIZE, &iodev->dev); 1827 out: 1828 mutex_unlock(&kvm->slots_lock); 1829 1830 return ret; 1831 } 1832 1833 #define INITIAL_BASER_VALUE \ 1834 (GIC_BASER_CACHEABILITY(GITS_BASER, INNER, RaWb) | \ 1835 GIC_BASER_CACHEABILITY(GITS_BASER, OUTER, SameAsInner) | \ 1836 GIC_BASER_SHAREABILITY(GITS_BASER, InnerShareable) | \ 1837 GITS_BASER_PAGE_SIZE_64K) 1838 1839 #define INITIAL_PROPBASER_VALUE \ 1840 (GIC_BASER_CACHEABILITY(GICR_PROPBASER, INNER, RaWb) | \ 1841 GIC_BASER_CACHEABILITY(GICR_PROPBASER, OUTER, SameAsInner) | \ 1842 GIC_BASER_SHAREABILITY(GICR_PROPBASER, InnerShareable)) 1843 1844 static int vgic_its_create(struct kvm_device *dev, u32 type) 1845 { 1846 int ret; 1847 struct vgic_its *its; 1848 1849 if (type != KVM_DEV_TYPE_ARM_VGIC_ITS) 1850 return -ENODEV; 1851 1852 its = kzalloc_obj(struct vgic_its, GFP_KERNEL_ACCOUNT); 1853 if (!its) 1854 return -ENOMEM; 1855 1856 mutex_lock(&dev->kvm->arch.config_lock); 1857 1858 if (vgic_initialized(dev->kvm)) { 1859 ret = vgic_v4_init(dev->kvm); 1860 if (ret < 0) { 1861 mutex_unlock(&dev->kvm->arch.config_lock); 1862 kfree(its); 1863 return ret; 1864 } 1865 } 1866 1867 mutex_init(&its->its_lock); 1868 mutex_init(&its->cmd_lock); 1869 1870 /* Yep, even more trickery for lock ordering... */ 1871 #ifdef CONFIG_LOCKDEP 1872 mutex_lock(&its->cmd_lock); 1873 mutex_lock(&its->its_lock); 1874 mutex_unlock(&its->its_lock); 1875 mutex_unlock(&its->cmd_lock); 1876 #endif 1877 1878 its->vgic_its_base = VGIC_ADDR_UNDEF; 1879 1880 INIT_LIST_HEAD(&its->device_list); 1881 INIT_LIST_HEAD(&its->collection_list); 1882 xa_init(&its->translation_cache); 1883 1884 dev->kvm->arch.vgic.msis_require_devid = true; 1885 dev->kvm->arch.vgic.has_its = true; 1886 its->enabled = false; 1887 its->dev = dev; 1888 1889 its->baser_device_table = INITIAL_BASER_VALUE | 1890 ((u64)GITS_BASER_TYPE_DEVICE << GITS_BASER_TYPE_SHIFT); 1891 its->baser_coll_table = INITIAL_BASER_VALUE | 1892 ((u64)GITS_BASER_TYPE_COLLECTION << GITS_BASER_TYPE_SHIFT); 1893 dev->kvm->arch.vgic.propbaser = INITIAL_PROPBASER_VALUE; 1894 dev->private = its; 1895 1896 vgic_its_set_abi(its, NR_ITS_ABIS - 1); 1897 mutex_unlock(&dev->kvm->arch.config_lock); 1898 return 0; 1899 } 1900 1901 static void vgic_its_destroy(struct kvm_device *kvm_dev) 1902 { 1903 struct kvm *kvm = kvm_dev->kvm; 1904 struct vgic_its *its = kvm_dev->private; 1905 1906 mutex_lock(&its->its_lock); 1907 1908 vgic_its_debug_destroy(kvm_dev); 1909 1910 vgic_its_free_device_list(kvm, its); 1911 vgic_its_free_collection_list(kvm, its); 1912 vgic_its_invalidate_cache(its); 1913 xa_destroy(&its->translation_cache); 1914 1915 mutex_unlock(&its->its_lock); 1916 kfree(its); 1917 kfree(kvm_dev);/* alloc by kvm_ioctl_create_device, free by .destroy */ 1918 } 1919 1920 static int vgic_its_has_attr_regs(struct kvm_device *dev, 1921 struct kvm_device_attr *attr) 1922 { 1923 const struct vgic_register_region *region; 1924 gpa_t offset = attr->attr; 1925 int align; 1926 1927 align = (offset < GITS_TYPER) || (offset >= GITS_PIDR4) ? 0x3 : 0x7; 1928 1929 if (offset & align) 1930 return -EINVAL; 1931 1932 region = vgic_find_mmio_region(its_registers, 1933 ARRAY_SIZE(its_registers), 1934 offset); 1935 if (!region) 1936 return -ENXIO; 1937 1938 return 0; 1939 } 1940 1941 static int vgic_its_attr_regs_access(struct kvm_device *dev, 1942 struct kvm_device_attr *attr, 1943 u64 *reg, bool is_write) 1944 { 1945 const struct vgic_register_region *region; 1946 struct vgic_its *its; 1947 gpa_t addr, offset; 1948 unsigned int len; 1949 int align, ret = 0; 1950 1951 its = dev->private; 1952 offset = attr->attr; 1953 1954 /* 1955 * Although the spec supports upper/lower 32-bit accesses to 1956 * 64-bit ITS registers, the userspace ABI requires 64-bit 1957 * accesses to all 64-bit wide registers. We therefore only 1958 * support 32-bit accesses to GITS_CTLR, GITS_IIDR and GITS ID 1959 * registers 1960 */ 1961 if ((offset < GITS_TYPER) || (offset >= GITS_PIDR4)) 1962 align = 0x3; 1963 else 1964 align = 0x7; 1965 1966 if (offset & align) 1967 return -EINVAL; 1968 1969 mutex_lock(&dev->kvm->lock); 1970 1971 if (kvm_trylock_all_vcpus(dev->kvm)) { 1972 mutex_unlock(&dev->kvm->lock); 1973 return -EBUSY; 1974 } 1975 1976 mutex_lock(&dev->kvm->arch.config_lock); 1977 1978 if (IS_VGIC_ADDR_UNDEF(its->vgic_its_base)) { 1979 ret = -ENXIO; 1980 goto out; 1981 } 1982 1983 region = vgic_find_mmio_region(its_registers, 1984 ARRAY_SIZE(its_registers), 1985 offset); 1986 if (!region) { 1987 ret = -ENXIO; 1988 goto out; 1989 } 1990 1991 addr = its->vgic_its_base + offset; 1992 1993 len = region->access_flags & VGIC_ACCESS_64bit ? 8 : 4; 1994 1995 if (is_write) { 1996 if (region->uaccess_its_write) 1997 ret = region->uaccess_its_write(dev->kvm, its, addr, 1998 len, *reg); 1999 else 2000 region->its_write(dev->kvm, its, addr, len, *reg); 2001 } else { 2002 *reg = region->its_read(dev->kvm, its, addr, len); 2003 } 2004 out: 2005 mutex_unlock(&dev->kvm->arch.config_lock); 2006 kvm_unlock_all_vcpus(dev->kvm); 2007 mutex_unlock(&dev->kvm->lock); 2008 return ret; 2009 } 2010 2011 static u32 compute_next_devid_offset(struct list_head *h, 2012 struct its_device *dev) 2013 { 2014 struct its_device *next; 2015 u32 next_offset; 2016 2017 if (list_is_last(&dev->dev_list, h)) 2018 return 0; 2019 next = list_next_entry(dev, dev_list); 2020 next_offset = next->device_id - dev->device_id; 2021 2022 return min_t(u32, next_offset, VITS_DTE_MAX_DEVID_OFFSET); 2023 } 2024 2025 static u32 compute_next_eventid_offset(struct list_head *h, struct its_ite *ite) 2026 { 2027 struct its_ite *next; 2028 u32 next_offset; 2029 2030 if (list_is_last(&ite->ite_list, h)) 2031 return 0; 2032 next = list_next_entry(ite, ite_list); 2033 next_offset = next->event_id - ite->event_id; 2034 2035 return min_t(u32, next_offset, VITS_ITE_MAX_EVENTID_OFFSET); 2036 } 2037 2038 /** 2039 * typedef entry_fn_t - Callback called on a table entry restore path 2040 * @its: its handle 2041 * @id: id of the entry 2042 * @entry: pointer to the entry 2043 * @opaque: pointer to an opaque data 2044 * 2045 * Return: < 0 on error, 0 if last element was identified, id offset to next 2046 * element otherwise 2047 */ 2048 typedef int (*entry_fn_t)(struct vgic_its *its, u32 id, void *entry, 2049 void *opaque); 2050 2051 /** 2052 * scan_its_table - Scan a contiguous table in guest RAM and applies a function 2053 * to each entry 2054 * 2055 * @its: its handle 2056 * @base: base gpa of the table 2057 * @size: size of the table in bytes 2058 * @esz: entry size in bytes 2059 * @start_id: the ID of the first entry in the table 2060 * (non zero for 2d level tables) 2061 * @fn: function to apply on each entry 2062 * @opaque: pointer to opaque data 2063 * 2064 * Return: < 0 on error, 0 if last element was identified, 1 otherwise 2065 * (the last element may not be found on second level tables) 2066 */ 2067 static int scan_its_table(struct vgic_its *its, gpa_t base, int size, u32 esz, 2068 int start_id, entry_fn_t fn, void *opaque) 2069 { 2070 struct kvm *kvm = its->dev->kvm; 2071 unsigned long len = size; 2072 int id = start_id; 2073 gpa_t gpa = base; 2074 char entry[ESZ_MAX]; 2075 int ret; 2076 2077 memset(entry, 0, esz); 2078 2079 while (true) { 2080 int next_offset; 2081 size_t byte_offset; 2082 2083 ret = kvm_read_guest_lock(kvm, gpa, entry, esz); 2084 if (ret) 2085 return ret; 2086 2087 next_offset = fn(its, id, entry, opaque); 2088 if (next_offset <= 0) 2089 return next_offset; 2090 2091 byte_offset = next_offset * esz; 2092 if (byte_offset >= len) 2093 break; 2094 2095 id += next_offset; 2096 gpa += byte_offset; 2097 len -= byte_offset; 2098 } 2099 return 1; 2100 } 2101 2102 /* 2103 * vgic_its_save_ite - Save an interrupt translation entry at @gpa 2104 */ 2105 static int vgic_its_save_ite(struct vgic_its *its, struct its_device *dev, 2106 struct its_ite *ite, gpa_t gpa) 2107 { 2108 u32 next_offset; 2109 u64 val; 2110 2111 next_offset = compute_next_eventid_offset(&dev->itt_head, ite); 2112 val = ((u64)next_offset << KVM_ITS_ITE_NEXT_SHIFT) | 2113 ((u64)ite->irq->intid << KVM_ITS_ITE_PINTID_SHIFT) | 2114 ite->collection->collection_id; 2115 val = cpu_to_le64(val); 2116 2117 return vgic_its_write_entry_lock(its, gpa, val, ite); 2118 } 2119 2120 /** 2121 * vgic_its_restore_ite - restore an interrupt translation entry 2122 * 2123 * @its: its handle 2124 * @event_id: id used for indexing 2125 * @ptr: pointer to the ITE entry 2126 * @opaque: pointer to the its_device 2127 */ 2128 static int vgic_its_restore_ite(struct vgic_its *its, u32 event_id, 2129 void *ptr, void *opaque) 2130 { 2131 struct its_device *dev = opaque; 2132 struct its_collection *collection; 2133 struct kvm *kvm = its->dev->kvm; 2134 struct kvm_vcpu *vcpu = NULL; 2135 u64 val; 2136 u64 *p = (u64 *)ptr; 2137 struct vgic_irq *irq; 2138 u32 coll_id, lpi_id; 2139 struct its_ite *ite; 2140 u32 offset; 2141 2142 val = *p; 2143 2144 val = le64_to_cpu(val); 2145 2146 coll_id = val & KVM_ITS_ITE_ICID_MASK; 2147 lpi_id = (val & KVM_ITS_ITE_PINTID_MASK) >> KVM_ITS_ITE_PINTID_SHIFT; 2148 2149 if (!lpi_id) 2150 return 1; /* invalid entry, no choice but to scan next entry */ 2151 2152 if (lpi_id < VGIC_MIN_LPI) 2153 return -EINVAL; 2154 2155 offset = val >> KVM_ITS_ITE_NEXT_SHIFT; 2156 if (event_id + offset >= BIT_ULL(dev->num_eventid_bits)) 2157 return -EINVAL; 2158 2159 collection = find_collection(its, coll_id); 2160 if (!collection) 2161 return -EINVAL; 2162 2163 if (!vgic_its_check_event_id(its, dev, event_id)) 2164 return -EINVAL; 2165 2166 ite = vgic_its_alloc_ite(dev, collection, event_id); 2167 if (IS_ERR(ite)) 2168 return PTR_ERR(ite); 2169 2170 if (its_is_collection_mapped(collection)) 2171 vcpu = kvm_get_vcpu_by_id(kvm, collection->target_addr); 2172 2173 irq = vgic_add_lpi(kvm, lpi_id, vcpu); 2174 if (IS_ERR(irq)) { 2175 its_free_ite(kvm, ite); 2176 return PTR_ERR(irq); 2177 } 2178 ite->irq = irq; 2179 2180 return offset; 2181 } 2182 2183 static int vgic_its_ite_cmp(void *priv, const struct list_head *a, 2184 const struct list_head *b) 2185 { 2186 struct its_ite *itea = container_of(a, struct its_ite, ite_list); 2187 struct its_ite *iteb = container_of(b, struct its_ite, ite_list); 2188 2189 if (itea->event_id < iteb->event_id) 2190 return -1; 2191 else 2192 return 1; 2193 } 2194 2195 static int vgic_its_save_itt(struct vgic_its *its, struct its_device *device) 2196 { 2197 const struct vgic_its_abi *abi = vgic_its_get_abi(its); 2198 gpa_t base = device->itt_addr; 2199 struct its_ite *ite; 2200 int ret; 2201 int ite_esz = abi->ite_esz; 2202 2203 list_sort(NULL, &device->itt_head, vgic_its_ite_cmp); 2204 2205 list_for_each_entry(ite, &device->itt_head, ite_list) { 2206 gpa_t gpa = base + ite->event_id * ite_esz; 2207 2208 /* 2209 * If an LPI carries the HW bit, this means that this 2210 * interrupt is controlled by GICv4, and we do not 2211 * have direct access to that state without GICv4.1. 2212 * Let's simply fail the save operation... 2213 */ 2214 if (ite->irq->hw && !kvm_vgic_global_state.has_gicv4_1) 2215 return -EACCES; 2216 2217 ret = vgic_its_save_ite(its, device, ite, gpa); 2218 if (ret) 2219 return ret; 2220 } 2221 return 0; 2222 } 2223 2224 /** 2225 * vgic_its_restore_itt - restore the ITT of a device 2226 * 2227 * @its: its handle 2228 * @dev: device handle 2229 * 2230 * Return 0 on success, < 0 on error 2231 */ 2232 static int vgic_its_restore_itt(struct vgic_its *its, struct its_device *dev) 2233 { 2234 const struct vgic_its_abi *abi = vgic_its_get_abi(its); 2235 gpa_t base = dev->itt_addr; 2236 int ret; 2237 int ite_esz = abi->ite_esz; 2238 size_t max_size = BIT_ULL(dev->num_eventid_bits) * ite_esz; 2239 2240 ret = scan_its_table(its, base, max_size, ite_esz, 0, 2241 vgic_its_restore_ite, dev); 2242 2243 /* scan_its_table returns +1 if all ITEs are invalid */ 2244 if (ret > 0) 2245 ret = 0; 2246 2247 return ret; 2248 } 2249 2250 /** 2251 * vgic_its_save_dte - Save a device table entry at a given GPA 2252 * 2253 * @its: ITS handle 2254 * @dev: ITS device 2255 * @ptr: GPA 2256 */ 2257 static int vgic_its_save_dte(struct vgic_its *its, struct its_device *dev, 2258 gpa_t ptr) 2259 { 2260 u64 val, itt_addr_field; 2261 u32 next_offset; 2262 2263 itt_addr_field = dev->itt_addr >> 8; 2264 next_offset = compute_next_devid_offset(&its->device_list, dev); 2265 val = (1ULL << KVM_ITS_DTE_VALID_SHIFT | 2266 ((u64)next_offset << KVM_ITS_DTE_NEXT_SHIFT) | 2267 (itt_addr_field << KVM_ITS_DTE_ITTADDR_SHIFT) | 2268 (dev->num_eventid_bits - 1)); 2269 val = cpu_to_le64(val); 2270 2271 return vgic_its_write_entry_lock(its, ptr, val, dte); 2272 } 2273 2274 /** 2275 * vgic_its_restore_dte - restore a device table entry 2276 * 2277 * @its: its handle 2278 * @id: device id the DTE corresponds to 2279 * @ptr: kernel VA where the 8 byte DTE is located 2280 * @opaque: unused 2281 * 2282 * Return: < 0 on error, 0 if the dte is the last one, id offset to the 2283 * next dte otherwise 2284 */ 2285 static int vgic_its_restore_dte(struct vgic_its *its, u32 id, 2286 void *ptr, void *opaque) 2287 { 2288 struct its_device *dev; 2289 u64 baser = its->baser_device_table; 2290 gpa_t itt_addr; 2291 u8 num_eventid_bits; 2292 u64 entry = *(u64 *)ptr; 2293 bool valid; 2294 u32 offset; 2295 int ret; 2296 2297 entry = le64_to_cpu(entry); 2298 2299 valid = entry >> KVM_ITS_DTE_VALID_SHIFT; 2300 num_eventid_bits = (entry & KVM_ITS_DTE_SIZE_MASK) + 1; 2301 itt_addr = ((entry & KVM_ITS_DTE_ITTADDR_MASK) 2302 >> KVM_ITS_DTE_ITTADDR_SHIFT) << 8; 2303 2304 if (!valid) 2305 return 1; 2306 2307 /* dte entry is valid */ 2308 offset = (entry & KVM_ITS_DTE_NEXT_MASK) >> KVM_ITS_DTE_NEXT_SHIFT; 2309 2310 /* Mimic the MAPD behaviour and reject invalid EID bits. */ 2311 if (num_eventid_bits > VITS_TYPER_IDBITS) 2312 return -EINVAL; 2313 2314 if (!vgic_its_check_id(its, baser, id, NULL)) 2315 return -EINVAL; 2316 2317 dev = vgic_its_alloc_device(its, id, itt_addr, num_eventid_bits); 2318 if (IS_ERR(dev)) 2319 return PTR_ERR(dev); 2320 2321 ret = vgic_its_restore_itt(its, dev); 2322 if (ret) { 2323 vgic_its_free_device(its->dev->kvm, its, dev); 2324 return ret; 2325 } 2326 2327 return offset; 2328 } 2329 2330 static int vgic_its_device_cmp(void *priv, const struct list_head *a, 2331 const struct list_head *b) 2332 { 2333 struct its_device *deva = container_of(a, struct its_device, dev_list); 2334 struct its_device *devb = container_of(b, struct its_device, dev_list); 2335 2336 if (deva->device_id < devb->device_id) 2337 return -1; 2338 else 2339 return 1; 2340 } 2341 2342 /* 2343 * vgic_its_save_device_tables - Save the device table and all ITT 2344 * into guest RAM 2345 * 2346 * L1/L2 handling is hidden by vgic_its_check_id() helper which directly 2347 * returns the GPA of the device entry 2348 */ 2349 static int vgic_its_save_device_tables(struct vgic_its *its) 2350 { 2351 u64 baser = its->baser_device_table; 2352 struct its_device *dev; 2353 2354 if (!(baser & GITS_BASER_VALID)) 2355 return 0; 2356 2357 list_sort(NULL, &its->device_list, vgic_its_device_cmp); 2358 2359 list_for_each_entry(dev, &its->device_list, dev_list) { 2360 int ret; 2361 gpa_t eaddr; 2362 2363 if (!vgic_its_check_id(its, baser, 2364 dev->device_id, &eaddr)) 2365 return -EINVAL; 2366 2367 ret = vgic_its_save_itt(its, dev); 2368 if (ret) 2369 return ret; 2370 2371 ret = vgic_its_save_dte(its, dev, eaddr); 2372 if (ret) 2373 return ret; 2374 } 2375 return 0; 2376 } 2377 2378 /** 2379 * handle_l1_dte - callback used for L1 device table entries (2 stage case) 2380 * 2381 * @its: its handle 2382 * @id: index of the entry in the L1 table 2383 * @addr: kernel VA 2384 * @opaque: unused 2385 * 2386 * L1 table entries are scanned by steps of 1 entry 2387 * Return < 0 if error, 0 if last dte was found when scanning the L2 2388 * table, +1 otherwise (meaning next L1 entry must be scanned) 2389 */ 2390 static int handle_l1_dte(struct vgic_its *its, u32 id, void *addr, 2391 void *opaque) 2392 { 2393 const struct vgic_its_abi *abi = vgic_its_get_abi(its); 2394 int l2_start_id = id * (SZ_64K / abi->dte_esz); 2395 u64 entry = *(u64 *)addr; 2396 int dte_esz = abi->dte_esz; 2397 gpa_t gpa; 2398 int ret; 2399 2400 entry = le64_to_cpu(entry); 2401 2402 if (!(entry & KVM_ITS_L1E_VALID_MASK)) 2403 return 1; 2404 2405 gpa = entry & KVM_ITS_L1E_ADDR_MASK; 2406 2407 ret = scan_its_table(its, gpa, SZ_64K, dte_esz, 2408 l2_start_id, vgic_its_restore_dte, NULL); 2409 2410 return ret; 2411 } 2412 2413 /* 2414 * vgic_its_restore_device_tables - Restore the device table and all ITT 2415 * from guest RAM to internal data structs 2416 */ 2417 static int vgic_its_restore_device_tables(struct vgic_its *its) 2418 { 2419 const struct vgic_its_abi *abi = vgic_its_get_abi(its); 2420 u64 baser = its->baser_device_table; 2421 int l1_esz, ret; 2422 int l1_tbl_size = GITS_BASER_NR_PAGES(baser) * SZ_64K; 2423 gpa_t l1_gpa; 2424 2425 if (!(baser & GITS_BASER_VALID)) 2426 return 0; 2427 2428 l1_gpa = GITS_BASER_ADDR_48_to_52(baser); 2429 2430 if (baser & GITS_BASER_INDIRECT) { 2431 l1_esz = GITS_LVL1_ENTRY_SIZE; 2432 ret = scan_its_table(its, l1_gpa, l1_tbl_size, l1_esz, 0, 2433 handle_l1_dte, NULL); 2434 } else { 2435 l1_esz = abi->dte_esz; 2436 ret = scan_its_table(its, l1_gpa, l1_tbl_size, l1_esz, 0, 2437 vgic_its_restore_dte, NULL); 2438 } 2439 2440 /* scan_its_table returns +1 if all entries are invalid */ 2441 if (ret > 0) 2442 ret = 0; 2443 2444 if (ret < 0) 2445 vgic_its_free_device_list(its->dev->kvm, its); 2446 2447 return ret; 2448 } 2449 2450 static int vgic_its_save_cte(struct vgic_its *its, 2451 struct its_collection *collection, 2452 gpa_t gpa) 2453 { 2454 u64 val; 2455 2456 val = (1ULL << KVM_ITS_CTE_VALID_SHIFT | 2457 ((u64)collection->target_addr << KVM_ITS_CTE_RDBASE_SHIFT) | 2458 collection->collection_id); 2459 val = cpu_to_le64(val); 2460 2461 return vgic_its_write_entry_lock(its, gpa, val, cte); 2462 } 2463 2464 /* 2465 * Restore a collection entry into the ITS collection table. 2466 * Return +1 on success, 0 if the entry was invalid (which should be 2467 * interpreted as end-of-table), and a negative error value for generic errors. 2468 */ 2469 static int vgic_its_restore_cte(struct vgic_its *its, gpa_t gpa) 2470 { 2471 struct its_collection *collection; 2472 struct kvm *kvm = its->dev->kvm; 2473 u32 target_addr, coll_id; 2474 u64 val; 2475 int ret; 2476 2477 ret = vgic_its_read_entry_lock(its, gpa, &val, cte); 2478 if (ret) 2479 return ret; 2480 val = le64_to_cpu(val); 2481 if (!(val & KVM_ITS_CTE_VALID_MASK)) 2482 return 0; 2483 2484 target_addr = (u32)(val >> KVM_ITS_CTE_RDBASE_SHIFT); 2485 coll_id = val & KVM_ITS_CTE_ICID_MASK; 2486 2487 if (target_addr != COLLECTION_NOT_MAPPED && 2488 !kvm_get_vcpu_by_id(kvm, target_addr)) 2489 return -EINVAL; 2490 2491 collection = find_collection(its, coll_id); 2492 if (collection) 2493 return -EEXIST; 2494 2495 if (!vgic_its_check_id(its, its->baser_coll_table, coll_id, NULL)) 2496 return -EINVAL; 2497 2498 ret = vgic_its_alloc_collection(its, &collection, coll_id); 2499 if (ret) 2500 return ret; 2501 collection->target_addr = target_addr; 2502 return 1; 2503 } 2504 2505 /* 2506 * vgic_its_save_collection_table - Save the collection table into 2507 * guest RAM 2508 */ 2509 static int vgic_its_save_collection_table(struct vgic_its *its) 2510 { 2511 const struct vgic_its_abi *abi = vgic_its_get_abi(its); 2512 u64 baser = its->baser_coll_table; 2513 gpa_t gpa = GITS_BASER_ADDR_48_to_52(baser); 2514 struct its_collection *collection; 2515 size_t max_size, filled = 0; 2516 int ret, cte_esz = abi->cte_esz; 2517 2518 if (!(baser & GITS_BASER_VALID)) 2519 return 0; 2520 2521 max_size = GITS_BASER_NR_PAGES(baser) * SZ_64K; 2522 2523 list_for_each_entry(collection, &its->collection_list, coll_list) { 2524 ret = vgic_its_save_cte(its, collection, gpa); 2525 if (ret) 2526 return ret; 2527 gpa += cte_esz; 2528 filled += cte_esz; 2529 } 2530 2531 if (filled == max_size) 2532 return 0; 2533 2534 /* 2535 * table is not fully filled, add a last dummy element 2536 * with valid bit unset 2537 */ 2538 return vgic_its_write_entry_lock(its, gpa, 0ULL, cte); 2539 } 2540 2541 /* 2542 * vgic_its_restore_collection_table - reads the collection table 2543 * in guest memory and restores the ITS internal state. Requires the 2544 * BASER registers to be restored before. 2545 */ 2546 static int vgic_its_restore_collection_table(struct vgic_its *its) 2547 { 2548 const struct vgic_its_abi *abi = vgic_its_get_abi(its); 2549 u64 baser = its->baser_coll_table; 2550 int cte_esz = abi->cte_esz; 2551 size_t max_size, read = 0; 2552 gpa_t gpa; 2553 int ret; 2554 2555 if (!(baser & GITS_BASER_VALID)) 2556 return 0; 2557 2558 gpa = GITS_BASER_ADDR_48_to_52(baser); 2559 2560 max_size = GITS_BASER_NR_PAGES(baser) * SZ_64K; 2561 2562 while (read < max_size) { 2563 ret = vgic_its_restore_cte(its, gpa); 2564 if (ret <= 0) 2565 break; 2566 gpa += cte_esz; 2567 read += cte_esz; 2568 } 2569 2570 if (ret > 0) 2571 return 0; 2572 2573 if (ret < 0) 2574 vgic_its_free_collection_list(its->dev->kvm, its); 2575 2576 return ret; 2577 } 2578 2579 /* 2580 * vgic_its_save_tables_v0 - Save the ITS tables into guest ARM 2581 * according to v0 ABI 2582 */ 2583 static int vgic_its_save_tables_v0(struct vgic_its *its) 2584 { 2585 int ret; 2586 2587 ret = vgic_its_save_device_tables(its); 2588 if (ret) 2589 return ret; 2590 2591 return vgic_its_save_collection_table(its); 2592 } 2593 2594 /* 2595 * vgic_its_restore_tables_v0 - Restore the ITS tables from guest RAM 2596 * to internal data structs according to V0 ABI 2597 * 2598 */ 2599 static int vgic_its_restore_tables_v0(struct vgic_its *its) 2600 { 2601 int ret; 2602 2603 ret = vgic_its_restore_collection_table(its); 2604 if (ret) 2605 return ret; 2606 2607 ret = vgic_its_restore_device_tables(its); 2608 if (ret) 2609 vgic_its_free_collection_list(its->dev->kvm, its); 2610 return ret; 2611 } 2612 2613 static void vgic_its_commit_v0(struct vgic_its *its) 2614 { 2615 const struct vgic_its_abi *abi; 2616 2617 abi = vgic_its_get_abi(its); 2618 its->baser_coll_table &= ~GITS_BASER_ENTRY_SIZE_MASK; 2619 its->baser_device_table &= ~GITS_BASER_ENTRY_SIZE_MASK; 2620 2621 its->baser_coll_table |= (GIC_ENCODE_SZ(abi->cte_esz, 5) 2622 << GITS_BASER_ENTRY_SIZE_SHIFT); 2623 2624 its->baser_device_table |= (GIC_ENCODE_SZ(abi->dte_esz, 5) 2625 << GITS_BASER_ENTRY_SIZE_SHIFT); 2626 } 2627 2628 static void vgic_its_reset(struct kvm *kvm, struct vgic_its *its) 2629 { 2630 /* We need to keep the ABI specific field values */ 2631 its->baser_coll_table &= ~GITS_BASER_VALID; 2632 its->baser_device_table &= ~GITS_BASER_VALID; 2633 its->cbaser = 0; 2634 its->creadr = 0; 2635 its->cwriter = 0; 2636 its->enabled = 0; 2637 vgic_its_free_device_list(kvm, its); 2638 vgic_its_free_collection_list(kvm, its); 2639 } 2640 2641 static int vgic_its_has_attr(struct kvm_device *dev, 2642 struct kvm_device_attr *attr) 2643 { 2644 switch (attr->group) { 2645 case KVM_DEV_ARM_VGIC_GRP_ADDR: 2646 switch (attr->attr) { 2647 case KVM_VGIC_ITS_ADDR_TYPE: 2648 return 0; 2649 } 2650 break; 2651 case KVM_DEV_ARM_VGIC_GRP_CTRL: 2652 switch (attr->attr) { 2653 case KVM_DEV_ARM_VGIC_CTRL_INIT: 2654 return 0; 2655 case KVM_DEV_ARM_ITS_CTRL_RESET: 2656 return 0; 2657 case KVM_DEV_ARM_ITS_SAVE_TABLES: 2658 return 0; 2659 case KVM_DEV_ARM_ITS_RESTORE_TABLES: 2660 return 0; 2661 } 2662 break; 2663 case KVM_DEV_ARM_VGIC_GRP_ITS_REGS: 2664 return vgic_its_has_attr_regs(dev, attr); 2665 } 2666 return -ENXIO; 2667 } 2668 2669 static int vgic_its_ctrl(struct kvm *kvm, struct vgic_its *its, u64 attr) 2670 { 2671 const struct vgic_its_abi *abi = vgic_its_get_abi(its); 2672 int ret = 0; 2673 2674 if (attr == KVM_DEV_ARM_VGIC_CTRL_INIT) /* Nothing to do */ 2675 return 0; 2676 2677 mutex_lock(&kvm->lock); 2678 2679 if (kvm_trylock_all_vcpus(kvm)) { 2680 mutex_unlock(&kvm->lock); 2681 return -EBUSY; 2682 } 2683 2684 mutex_lock(&kvm->arch.config_lock); 2685 mutex_lock(&its->its_lock); 2686 2687 switch (attr) { 2688 case KVM_DEV_ARM_ITS_CTRL_RESET: 2689 vgic_its_reset(kvm, its); 2690 break; 2691 case KVM_DEV_ARM_ITS_SAVE_TABLES: 2692 ret = abi->save_tables(its); 2693 break; 2694 case KVM_DEV_ARM_ITS_RESTORE_TABLES: 2695 ret = abi->restore_tables(its); 2696 break; 2697 default: 2698 ret = -ENXIO; 2699 break; 2700 } 2701 2702 mutex_unlock(&its->its_lock); 2703 mutex_unlock(&kvm->arch.config_lock); 2704 kvm_unlock_all_vcpus(kvm); 2705 mutex_unlock(&kvm->lock); 2706 return ret; 2707 } 2708 2709 /* 2710 * kvm_arch_allow_write_without_running_vcpu - allow writing guest memory 2711 * without the running VCPU when dirty ring is enabled. 2712 * 2713 * The running VCPU is required to track dirty guest pages when dirty ring 2714 * is enabled. Otherwise, the backup bitmap should be used to track the 2715 * dirty guest pages. When vgic/its tables are being saved, the backup 2716 * bitmap is used to track the dirty guest pages due to the missed running 2717 * VCPU in the period. 2718 */ 2719 bool kvm_arch_allow_write_without_running_vcpu(struct kvm *kvm) 2720 { 2721 struct vgic_dist *dist = &kvm->arch.vgic; 2722 2723 return dist->table_write_in_progress; 2724 } 2725 2726 static int vgic_its_set_attr(struct kvm_device *dev, 2727 struct kvm_device_attr *attr) 2728 { 2729 struct vgic_its *its = dev->private; 2730 int ret; 2731 2732 switch (attr->group) { 2733 case KVM_DEV_ARM_VGIC_GRP_ADDR: { 2734 u64 __user *uaddr = (u64 __user *)(long)attr->addr; 2735 unsigned long type = (unsigned long)attr->attr; 2736 u64 addr; 2737 2738 if (type != KVM_VGIC_ITS_ADDR_TYPE) 2739 return -ENODEV; 2740 2741 if (copy_from_user(&addr, uaddr, sizeof(addr))) 2742 return -EFAULT; 2743 2744 ret = vgic_check_iorange(dev->kvm, its->vgic_its_base, 2745 addr, SZ_64K, KVM_VGIC_V3_ITS_SIZE); 2746 if (ret) 2747 return ret; 2748 2749 ret = vgic_register_its_iodev(dev->kvm, its, addr); 2750 if (ret) 2751 return ret; 2752 2753 return vgic_its_debug_init(dev); 2754 2755 } 2756 case KVM_DEV_ARM_VGIC_GRP_CTRL: 2757 return vgic_its_ctrl(dev->kvm, its, attr->attr); 2758 case KVM_DEV_ARM_VGIC_GRP_ITS_REGS: { 2759 u64 __user *uaddr = (u64 __user *)(long)attr->addr; 2760 u64 reg; 2761 2762 if (get_user(reg, uaddr)) 2763 return -EFAULT; 2764 2765 return vgic_its_attr_regs_access(dev, attr, ®, true); 2766 } 2767 } 2768 return -ENXIO; 2769 } 2770 2771 static int vgic_its_get_attr(struct kvm_device *dev, 2772 struct kvm_device_attr *attr) 2773 { 2774 switch (attr->group) { 2775 case KVM_DEV_ARM_VGIC_GRP_ADDR: { 2776 struct vgic_its *its = dev->private; 2777 u64 addr = its->vgic_its_base; 2778 u64 __user *uaddr = (u64 __user *)(long)attr->addr; 2779 unsigned long type = (unsigned long)attr->attr; 2780 2781 if (type != KVM_VGIC_ITS_ADDR_TYPE) 2782 return -ENODEV; 2783 2784 if (copy_to_user(uaddr, &addr, sizeof(addr))) 2785 return -EFAULT; 2786 break; 2787 } 2788 case KVM_DEV_ARM_VGIC_GRP_ITS_REGS: { 2789 u64 __user *uaddr = (u64 __user *)(long)attr->addr; 2790 u64 reg; 2791 int ret; 2792 2793 ret = vgic_its_attr_regs_access(dev, attr, ®, false); 2794 if (ret) 2795 return ret; 2796 return put_user(reg, uaddr); 2797 } 2798 default: 2799 return -ENXIO; 2800 } 2801 2802 return 0; 2803 } 2804 2805 static struct kvm_device_ops kvm_arm_vgic_its_ops = { 2806 .name = "kvm-arm-vgic-its", 2807 .create = vgic_its_create, 2808 .destroy = vgic_its_destroy, 2809 .set_attr = vgic_its_set_attr, 2810 .get_attr = vgic_its_get_attr, 2811 .has_attr = vgic_its_has_attr, 2812 }; 2813 2814 int kvm_vgic_register_its_device(void) 2815 { 2816 return kvm_register_device_ops(&kvm_arm_vgic_its_ops, 2817 KVM_DEV_TYPE_ARM_VGIC_ITS); 2818 } 2819