KVM: arm64: GICv3 ITS emulation
Subsystems (6)
v5 cover KVM: arm64: GICv3 ITS emulation
Hi,
please welcome a reworked version of the ITS emulation support. It allows those KVM guests that use an emulated GICv3 to use LPIs as well, though in the moment this is limited to emulated PCI devices. This is based on v4.7-rc1 and only works with the new VGIC implementation. There have been some modifications due to changes in the underlying VGIC code (changed MMIO handlers, etc.).
Some other changes relate to the fact that any redistributor related variables are now held in the vgic_cpu structure, so they are naturally per VCPU.
Also this series supports multiple ITSes and makes them independent from the distributor. There is a new KVM device, which can be created multiple times.
You can find all of this code (and the prerequisites) in the its-emul/v5 branch of my repository [1].
This has been briefly tested on the model and on GICv3 hardware. Since there are quite some fundamental changes in this version, I expect some issues in the code, so if you have GICv3 capable hardware, please test it on your setup.
Also of course any review comments are very welcome!
Cheers,
Andre.
Changelog v4..v5:
- adapting to final new VGIC (MMIO handlers, etc.)
- new KVM device to model an ITS, multiple instances allowed
- move redistributor data into struct vgic_cpu
- separate distributor and ITS(es)
- various bug fixes and amended comments after review comments
Changelog v3..v4:
- adapting to new VGIC (changes in IRQ injection mechanism)
Changelog v2..v3:
- adapt to 4.3-rc and Christoffer's timer rework
- adapt spin locks on handling PROPBASER/PENDBASER registers
- rework locking in ITS command handling (dropping dist where needed)
- only clear LPI pending bit if LPI could actually be queued
- simplify GICR_CTLR handling
- properly free ITTEs (including our pending bitmap)
- fix corner cases with unmapped collections
- keep retire_lr() around
- rename vgic_handle_base_register to vgic_reg64_access()
- use kcalloc instead of kmalloc
- minor fixes, renames and added comments
Changelog v1..v2
- fix issues when using non-ITS GICv3 emulation
- streamline frame address initialization (new patch 05/15)
- preallocate buffer memory for reading from guest's memory
- move locking into the actual command handlers
- preallocate memory for new structures if needed
- use non-atomic __set_bit() and __clear_bit() when under the lock
- add INT command handler to allow LPI injection from the guest
- rewrite CWRITER handler to align with new locking scheme
- remove unneeded CONFIG_HAVE_KVM_MSI #ifdefs
- check memory table size against our LPI limit (65536 interrupts)
- observe initial gap of 1024 interrupts in pending table
- use term "configuration table" to be in line with the spec
- clarify and extend documentation on API extensions
- introduce new KVM_CAP_MSI_DEVID capability to advertise device ID requirement
- update, fix and add many comments
- minor style changes as requested by reviewers
---------------
The GICv3 ITS (Interrupt Translation Service) is a part of the ARM GICv3 interrupt controller [3] used for implementing MSIs. It specifies a new kind of interrupts (LPIs), which are mapped to establish a connection between a device, its MSI payload value and the target processor the IRQ is eventually delivered to. In order to allow using MSIs in an ARM64 KVM guest, we emulate this ITS widget in the kernel.
The ITS works by reading commands written by software (from the guest in our case) into a (guest allocated) memory region and establishing the mapping between a device, the MSI payload and the target CPU. We parse these commands and update our internal data structures to reflect those changes. On an MSI injection we iterate those structures to learn the LPI number we have to inject.
For the time being we use simple lists to hold the data, this is good enough for the small number of entries each of the components currently have. Should this become a performance bottleneck in the future, those can be extended to arrays or trees if needed.
Most of the code lives in a separate source file (vgic-its.c), though there are some changes necessary in the existing VGIC files.
For the time being this series gives us the ability to use emulated PCI devices that can use MSIs in the guest. Those have to be triggered by letting the userland device emulation simulate the MSI write with the KVM_SIGNAL_MSI ioctl. This will be translated into the proper LPI by the ITS emulation and injected into the guest in the usual way (just with a higher IRQ number).
This series is based on 4.7-rc1 and can be found at the its-emul/v5 branch of this repository [1].
For this to be used you need a GICv3 host machine (a fast model would do), though it does not rely on any host ITS bits (neither in hardware or software).
To test this you can use the kvmtool patches available in the "its-v5" branch here [2].
Start a guest with: "$ lkvm run --irqchip=gicv3-its --force-pci" and see the ITS being used for instance by the virtio devices.
[1]: git://linux-arm.org/linux-ap.git
http://www.linux-arm.org/git?p=linux-ap.git;a=log;h=refs/heads/its-emul/v5 [2]: git://linux-arm.org/kvmtool.git http://www.linux-arm.org/git?p=kvmtool.git;a=log;h=refs/heads/its-v5
Andre Przywara (13):
KVM: arm/arm64: move redistributor kvm_io_devices
KVM: arm/arm64: check return value for kvm_register_vgic_device
KVM: extend struct kvm_msi to hold a 32-bit device ID
KVM: arm/arm64: extend arch CAP checks to allow per-VM capabilities
KVM: arm64: handle ITS related GICv3 redistributor registers
KVM: arm64: introduce ITS emulation file with stub functions
KVM: arm64: introduce new KVM ITS device
KVM: arm64: implement basic ITS register handlers
KVM: arm64: connect LPIs to the VGIC emulation
KVM: arm64: sync LPI configuration and pending tables
KVM: arm64: implement ITS command queue command handlers
KVM: arm64: implement MSI injection in ITS emulation
KVM: arm64: enable ITS emulation as a virtual MSI controller
Documentation/virtual/kvm/api.txt | 14 +- Documentation/virtual/kvm/devices/arm-vgic.txt | 23 +- arch/arm/include/asm/kvm_host.h | 5 +- arch/arm/kvm/arm.c | 5 +- arch/arm64/include/asm/kvm_host.h | 4 +- arch/arm64/include/uapi/asm/kvm.h | 2 + arch/arm64/kvm/Kconfig | 1 + arch/arm64/kvm/Makefile | 1 + arch/arm64/kvm/reset.c | 12 +- include/kvm/vgic/vgic.h | 45 +- include/linux/irqchip/arm-gic-v3.h | 30 +- include/uapi/linux/kvm.h | 7 +- virt/kvm/arm/vgic.c | 5 + virt/kvm/arm/vgic/vgic-init.c | 17 +- virt/kvm/arm/vgic/vgic-its.c | 1275 ++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic-kvm-device.c | 22 +- virt/kvm/arm/vgic/vgic-mmio-v3.c | 130 ++- virt/kvm/arm/vgic/vgic-mmio.h | 6 + virt/kvm/arm/vgic/vgic-v2.c | 7 +- virt/kvm/arm/vgic/vgic-v3.c | 15 +- virt/kvm/arm/vgic/vgic.c | 10 +- virt/kvm/arm/vgic/vgic.h | 46 +- 22 files changed, 1634 insertions(+), 48 deletions(-) create mode 100644 virt/kvm/arm/vgic/vgic-its.c
Signature · 1 line
2.8.2
PATCH v5 4/13 KVM: arm/arm64: extend arch CAP checks to allow per-VM capabilities
KVM capabilities can be a per-VM property, though ARM/ARM64 currently does not pass on the VM pointer to the architecture specific capability handlers.
Add a "struct kvm*" parameter to those function to later allow proper per-VM capability reporting.
--- arch/arm/include/asm/kvm_host.h | 2 +- arch/arm/kvm/arm.c | 2 +- arch/arm64/include/asm/kvm_host.h | 2 +- arch/arm64/kvm/reset.c | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-)
arch/arm/include/asm/kvm_host.h +1 -1
@@ -281,7 +281,7 @@ static inline void __cpu_reset_hyp_mode(phys_addr_t boot_pgd_ptr,*/ } -static inline int kvm_arch_dev_ioctl_check_extension (long ext) +static inline int kvm_arch_dev_ioctl_check_extension (struct kvm *kvm,long ext) {return 0 ; }
arch/arm/kvm/arm.c +1 -1
@@ -201,7 +201,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) r =KVM_MAX_VCPUS ;break ;default : - r =kvm_arch_dev_ioctl_check_extension (ext); + r =kvm_arch_dev_ioctl_check_extension (kvm, ext);break ; }return r;
arch/arm64/include/asm/kvm_host.h +1 -1
@@ -47,7 +47,7 @@ int __attribute_const__ kvm_target_cpu(void);int kvm_reset_vcpu (struct kvm_vcpu *vcpu); -int kvm_arch_dev_ioctl_check_extension (long ext); +int kvm_arch_dev_ioctl_check_extension (struct kvm *kvm,long ext);unsigned long kvm_hyp_reset_entry (void );void __extended_idmap_trampoline (phys_addr_t boot_pgd,phys_addr_t idmap_start);
arch/arm64/kvm/reset.c +1 -1
@@ -65,7 +65,7 @@ static bool cpu_has_32bit_el1(void)* We currently assume that the number of HW registers is uniform * across all CPUs (see cpuinfo_sanity_check). */ -int kvm_arch_dev_ioctl_check_extension (long ext) +int kvm_arch_dev_ioctl_check_extension (struct kvm *kvm,long ext) {int r;
Signature · 1 line
2.8.2
PATCH v5 7/13 KVM: arm64: introduce new KVM ITS device
In reply to Andre PrzywaraIntroduce a new KVM device that represents an ARM Interrupt Translation Service (ITS) controller. Since there can be multiple of this per guest, we can't piggy back on the existing GICv3 distributor device, but create a new type of KVM device.
On the KVM_CREATE_DEVICE ioctl we initialize the ITS data structure and add this ITS to a linked list. Userland is expected to set the frame address using an ioctl during the VM initialization process, this will be used to pick the right ITS for a particular device or MMIO access.
--- Documentation/virtual/kvm/devices/arm-vgic.txt | 23 ++++- arch/arm/include/asm/kvm_host.h | 3 + arch/arm/kvm/arm.c | 3 + arch/arm64/include/asm/kvm_host.h | 2 + arch/arm64/include/uapi/asm/kvm.h | 2 + include/kvm/vgic/vgic.h | 2 + include/uapi/linux/kvm.h | 2 + virt/kvm/arm/vgic/vgic-init.c | 13 +++ virt/kvm/arm/vgic/vgic-its.c | 121 +++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic-kvm-device.c | 7 +- virt/kvm/arm/vgic/vgic-mmio-v3.c | 2 +- virt/kvm/arm/vgic/vgic.h | 9 ++ 12 files changed, 182 insertions(+), 7 deletions(-)
Documentation/virtual/kvm/devices/arm-vgic.txt +19 -4
@@ -4,16 +4,22 @@ ARM Virtual Generic Interrupt Controller (VGIC) Device types supported: KVM_DEV_TYPE_ARM_VGIC_V2 ARM Generic Interrupt Controller v2.0 KVM_DEV_TYPE_ARM_VGIC_V3 ARM Generic Interrupt Controller v3.0 + KVM_DEV_TYPE_ARM_VGIC_ITS ARM Interrupt Translation Service Controller -Only one VGIC instance may be instantiated through either this API or the -legacy KVM_CREATE_IRQCHIP api. The created VGIC will act as the VM interrupt -controller, requiring emulated user-space devices to inject interrupts to the -VGIC instead of directly to CPUs. +Only one VGIC instance of the V2/V3 types above may be instantiated through +either this API or the legacy KVM_CREATE_IRQCHIP api. The created VGIC will +act as the VM interrupt controller, requiring emulated user-space devices to +inject interrupts to the VGIC instead of directly to CPUs. Creating a guest GICv3 device requires a host GICv3 as well. GICv3 implementations with hardware compatibility support allow a guest GICv2 as well. +Creating a virtual ITS controller requires a host GICv3 (but does not depend +on having physical ITS controllers). +There can be multiple ITS controllers per guest, each of them has to have +a separate, non-overlapping MMIO region. + Groups: KVM_DEV_ARM_VGIC_GRP_ADDR Attributes:
@@ -39,6 +45,15 @@ Groups: Only valid for KVM_DEV_TYPE_ARM_VGIC_V3. This address needs to be 64K aligned. + KVM_VGIC_V3_ADDR_TYPE_ITS (rw, 64-bit) + Base address in the guest physical address space of the GICv3 ITS + control register frame. The ITS allows MSI(-X) interrupts to be + injected into guests. This extension is optional. If the kernel + does not support the ITS, the call returns -ENODEV. + This address region is solely for the guest to access the ITS control + registers and does not cover the ITS translation register. + Only valid for KVM_DEV_TYPE_ARM_VGIC_ITS. + This address needs to be 64K aligned and the region covers 64K. KVM_DEV_ARM_VGIC_GRP_DIST_REGS Attributes:
arch/arm/include/asm/kvm_host.h +3 -0
@@ -69,6 +69,9 @@ struct kvm_arch {/* Interrupt controller */ struct vgic_dist vgic; +/* List of virtual ITS MSI controllers */ +struct list_head vits_list; +int max_vcpus; };
arch/arm/kvm/arm.c +3 -0
@@ -20,6 +20,7 @@#include <linux/errno.h> #include <linux/err.h> #include <linux/kvm_host.h> +#include <linux/list.h> #include <linux/module.h> #include <linux/vmalloc.h> #include <linux/fs.h>
@@ -126,7 +127,9 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)if (ret) goto out_free_stage2_pgd; +INIT_LIST_HEAD (&kvm->arch.vits_list);kvm_vgic_early_init (kvm); +kvm_timer_init (kvm);/* Mark the initial VMID generation invalid */
arch/arm64/include/asm/kvm_host.h +2 -0
@@ -68,6 +68,8 @@ struct kvm_arch {/* Interrupt controller */ struct vgic_dist vgic; +/* List of virtual ITS MSI controllers */ +struct list_head vits_list;/* Timer */ struct arch_timer_kvm timer;
arch/arm64/include/uapi/asm/kvm.h +2 -0
@@ -87,9 +87,11 @@ struct kvm_regs {/* Supported VGICv3 address types */ #define KVM_VGIC_V3_ADDR_TYPE_DIST 2#define KVM_VGIC_V3_ADDR_TYPE_REDIST 3 +#define KVM_VGIC_ITS_ADDR_TYPE 4#define KVM_VGIC_V3_DIST_SIZE SZ_64K#define KVM_VGIC_V3_REDIST_SIZE (2 * SZ_64K) +#define KVM_VGIC_V3_ITS_SIZE SZ_64K#define KVM_ARM_VCPU_POWER_OFF 0/* CPU is started in OFF state */ #define KVM_ARM_VCPU_EL1_32BIT 1/* CPU running a 32bit VM */
include/kvm/vgic/vgic.h +2 -0
@@ -117,6 +117,8 @@ struct vgic_io_device { };struct vgic_its { +struct list_head its_list; +/* The base address of the ITS control register frame */ gpa_t vgic_its_base;
include/uapi/linux/kvm.h +2 -0
@@ -1077,6 +1077,8 @@ enum kvm_device_type {#define KVM_DEV_TYPE_FLIC KVM_DEV_TYPE_FLIC KVM_DEV_TYPE_ARM_VGIC_V3,#define KVM_DEV_TYPE_ARM_VGIC_V3 KVM_DEV_TYPE_ARM_VGIC_V3 + KVM_DEV_TYPE_ARM_VGIC_ITS, +#define KVM_DEV_TYPE_ARM_VGIC_ITS KVM_DEV_TYPE_ARM_VGIC_ITS KVM_DEV_TYPE_MAX, };
virt/kvm/arm/vgic/vgic-init.c +13 -0
@@ -239,6 +239,7 @@ static void kvm_vgic_vcpu_init(struct kvm_vcpu *vcpu)int vgic_init (struct kvm *kvm) {struct vgic_dist *dist = &kvm->arch.vgic; +struct vgic_its *its;struct kvm_vcpu *vcpu;int ret =0 , i;
@@ -253,6 +254,12 @@ int vgic_init(struct kvm *kvm)if (ret) goto out; +list_for_each_entry (its, &kvm->arch.vits_list, its_list) { + ret =vits_init (kvm, its); +if (ret) + goto out; + } +kvm_for_each_vcpu (i, vcpu, kvm)kvm_vgic_vcpu_init (vcpu);
@@ -286,10 +293,16 @@ void kvm_vgic_vcpu_destroy(struct kvm_vcpu *vcpu)void kvm_vgic_destroy (struct kvm *kvm) {struct kvm_vcpu *vcpu; +struct vgic_its *its, *next;int i;kvm_vgic_dist_destroy (kvm); +list_for_each_entry_safe (its, next, &kvm->arch.vits_list, its_list) { +vits_destroy (kvm, its); +kfree (its); + } +kvm_for_each_vcpu (i, vcpu, kvm)kvm_vgic_vcpu_destroy (vcpu); }
virt/kvm/arm/vgic/vgic-its.c +121 -0
130 lines not on this page · raw message
virt/kvm/arm/vgic/vgic-kvm-device.c +5 -2
@@ -21,8 +21,8 @@/* common helpers */ -static int vgic_check_ioaddr (struct kvm *kvm,phys_addr_t *ioaddr, -phys_addr_t addr,phys_addr_t alignment) +int vgic_check_ioaddr (struct kvm *kvm,phys_addr_t *ioaddr, +phys_addr_t addr,phys_addr_t alignment) {if (addr & ~KVM_PHYS_MASK )return -E2BIG ;
@@ -223,6 +223,9 @@ int kvm_register_vgic_device(unsigned long type)case KVM_DEV_TYPE_ARM_VGIC_V3 : ret =kvm_register_device_ops (&kvm_arm_vgic_v3_ops,KVM_DEV_TYPE_ARM_VGIC_V3 ); +if (ret) +break ; + ret =kvm_vgic_register_its_device ();break ;#endif }
virt/kvm/arm/vgic/vgic-mmio-v3.c +1 -1
@@ -46,7 +46,7 @@ bool vgic_has_its(struct kvm *kvm)if (dist->vgic_model !=KVM_DEV_TYPE_ARM_VGIC_V3 )return false; -return false; +return !list_empty (&kvm->arch.vits_list); } static unsigned long vgic_mmio_read_v3_misc(struct kvm_vcpu *vcpu,
virt/kvm/arm/vgic/vgic.h +9 -0
@@ -41,6 +41,9 @@ struct vgic_irq *vgic_get_irq(struct kvm *kvm, struct kvm_vcpu *vcpu,bool vgic_queue_irq_unlock (struct kvm *kvm,struct vgic_irq *irq);void vgic_kick_vcpus (struct kvm *kvm); +int vgic_check_ioaddr (struct kvm *kvm,phys_addr_t *ioaddr, +phys_addr_t addr,phys_addr_t alignment); +void vgic_v2_process_maintenance (struct kvm_vcpu *vcpu);void vgic_v2_fold_lr_state (struct kvm_vcpu *vcpu);void vgic_v2_populate_lr (struct kvm_vcpu *vcpu,struct vgic_irq *irq,int lr);
@@ -74,6 +77,7 @@ int vgic_register_redist_iodevs(struct kvm *kvm, gpa_t dist_base_address);bool vgic_has_its (struct kvm *kvm);int vits_init (struct kvm *kvm,struct vgic_its *its);void vits_destroy (struct kvm *kvm,struct vgic_its *its); +int kvm_vgic_register_its_device (void );#else static inline void vgic_v3_process_maintenance (struct kvm_vcpu *vcpu) {
@@ -139,6 +143,11 @@ static int vits_init(struct kvm *kvm, struct vgic_its *its)static inline void vits_destroy (struct kvm *kvm,struct vgic_its *its) { } + +static int kvm_vgic_register_its_device (void ) +{ +return -ENODEV ; +}#endif int kvm_register_vgic_device (unsigned long type);
Signature · 1 line
2.8.2
PATCH v5 8/13 KVM: arm64: implement basic ITS register handlers
In reply to Andre PrzywaraAdd emulation for some basic MMIO registers used in the ITS emulation. This includes:
- GITS_{CTLR,TYPER,IIDR}
- ID registers
- GITS_{CBASER,CREADR,CWRITER}
(which implement the ITS command buffer handling)
Most of the handlers are pretty straight forward, but CWRITER goes some extra miles to allow fine grained locking. The idea here is to let only the first instance iterate through the command ring buffer, CWRITER accesses on other VCPUs meanwhile will be picked up by that first instance and handled as well. The ITS lock is thus only held for very small periods of time and is dropped before the actual command handler is called.
--- include/kvm/vgic/vgic.h | 6 + include/linux/irqchip/arm-gic-v3.h | 10 ++ virt/kvm/arm/vgic/vgic-its.c | 307 ++++++++++++++++++++++++++++++++++++- virt/kvm/arm/vgic/vgic-mmio-v3.c | 8 +- virt/kvm/arm/vgic/vgic-mmio.h | 6 + 5 files changed, 326 insertions(+), 11 deletions(-)
include/kvm/vgic/vgic.h +6 -0
@@ -22,6 +22,7 @@#include <linux/spinlock.h> #include <linux/types.h> #include <kvm/iodev.h> +#include <linux/list.h> #define VGIC_V3_MAX_CPUS 255#define VGIC_V2_MAX_CPUS 8
@@ -125,6 +126,11 @@ struct vgic_its {bool enabled;struct vgic_io_device iodev;spinlock_t lock; +u64 cbaser; +u32 creadr; +u32 cwriter; +struct list_head device_list; +struct list_head collection_list; };struct vgic_dist {
include/linux/irqchip/arm-gic-v3.h +10 -0
@@ -176,16 +176,26 @@#define GITS_CREADR 0x0090#define GITS_BASER 0x0100#define GITS_IDREGS_BASE 0xffd0 +#define GITS_PIDR0 0xffe0 +#define GITS_PIDR1 0xffe4#define GITS_PIDR2 GICR_PIDR2 +#define GITS_PIDR4 0xffd0 +#define GITS_CIDR0 0xfff0 +#define GITS_CIDR1 0xfff4 +#define GITS_CIDR2 0xfff8 +#define GITS_CIDR3 0xfffc#define GITS_TRANSLATER 0x10040#define GITS_CTLR_ENABLE (1U << 0)#define GITS_CTLR_QUIESCENT (1U << 31) +#define GITS_TYPER_PLPIS (1UL << 0) +#define GITS_TYPER_IDBITS_SHIFT 8#define GITS_TYPER_DEVBITS_SHIFT 13#define GITS_TYPER_DEVBITS (r) ((((r) >> GITS_TYPER_DEVBITS_SHIFT) & 0x1f) + 1)#define GITS_TYPER_PTA (1UL << 19) +#define GITS_TYPER_HWCOLLCNT_SHIFT 24#define GITS_CBASER_VALID (1UL << 63)#define GITS_CBASER_nCnB (0UL << 59)
virt/kvm/arm/vgic/vgic-its.c +300 -7
348 lines not on this page · raw message
virt/kvm/arm/vgic/vgic-mmio-v3.c +4 -4
@@ -23,15 +23,15 @@#include "vgic-mmio.h" /* extract @num bytes at @offset bytes offset in data */ -static unsigned long extract_bytes (unsigned long data,unsigned int offset, -unsigned int num) +unsigned long extract_bytes (unsigned long data,unsigned int offset, +unsigned int num) {return (data >> (offset *8 )) &GENMASK_ULL (num *8 -1 ,0 ); }/* allows updates of any half of a 64-bit register (or the whole thing) */ -static u64 update_64bit_reg (u64 reg,unsigned int offset,unsigned int len, -unsigned long val) +u64 update_64bit_reg (u64 reg,unsigned int offset,unsigned int len, +unsigned long val) { offset &=4 ; reg &=GENMASK_ULL (len *8 -1 ,0 ) << ((len - offset) *8 );
virt/kvm/arm/vgic/vgic-mmio.h +6 -0
@@ -87,6 +87,12 @@ unsigned long vgic_data_mmio_bus_to_host(const void *val, unsigned int len);void vgic_data_host_to_mmio_bus (void *buf,unsigned int len,unsigned long data); +unsigned long extract_bytes (unsigned long data,unsigned int offset, +unsigned int num); + +u64 update_64bit_reg (u64 reg,unsigned int offset,unsigned int len, +unsigned long val); +unsigned long vgic_mmio_read_raz (struct kvm_vcpu *vcpu,gpa_t addr,unsigned int len);
Signature · 1 line
2.8.2
PATCH v5 9/13 KVM: arm64: connect LPIs to the VGIC emulation
In reply to Andre PrzywaraLPIs are dynamically created (mapped) at guest runtime and their actual numbers can be quite high, but is mostly assigned using a very sparse allocation scheme. So arrays are not an ideal data structure to hold the information. We use our equivalent of the "Interrupt Translation Table Entry" (ITTE) to hold the vgic_irq struct for a virtual LPI embedded in in the ITTE.
Connect the VGIC core code via an accessor function to help it get the struct vgic_irq for a certain LPI.
--- virt/kvm/arm/vgic/vgic-its.c | 34 ++++++++++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic.c | 2 +- virt/kvm/arm/vgic/vgic.h | 6 ++++++ 3 files changed, 41 insertions(+), 1 deletion(-)
virt/kvm/arm/vgic/vgic-its.c +34 -0
@@ -68,11 +68,29 @@ struct its_collection {struct its_itte {struct list_head itte_list; +struct vgic_irq irq;struct its_collection *collection;u32 lpi;u32 event_id; }; +/* To be used as an iterator this macro misses the enclosing parentheses */ +#define for_each_lpi (dev, itte, its) \ + list_for_each_entry(dev, &(its)->device_list, dev_list) \ + list_for_each_entry(itte, &(dev)->itt, itte_list) + +static struct its_itte *find_itte_by_lpi (struct vgic_its *its,int lpi) +{ +struct its_device *device; +struct its_itte *itte; + +for_each_lpi (device, itte, its) { +if (itte->lpi == lpi) +return itte; + } +return NULL ; +} +#define BASER_BASE_ADDRESS (x) ((x) & 0xfffffffff000ULL)#define ITS_FRAME (addr) ((addr) & ~(SZ_64K - 1))
@@ -158,6 +176,22 @@ static unsigned long vgic_mmio_read_its_idregs(struct kvm_vcpu *vcpu,return 0 ; } +struct vgic_irq *vgic_its_get_lpi (struct kvm *kvm,u32 intid) +{ +struct vgic_its *its; +struct its_itte *itte; + +list_for_each_entry (its, &kvm->arch.vits_list, its_list) { + itte =find_itte_by_lpi (its, intid); +if (!itte) +continue ; + +return &itte->irq; + } + +return NULL ; +} +static void its_free_itte (struct its_itte *itte) {list_del (&itte->itte_list);
virt/kvm/arm/vgic/vgic.c +1 -1
@@ -58,7 +58,7 @@ struct vgic_irq *vgic_get_irq(struct kvm *kvm, struct kvm_vcpu *vcpu,/* LPIs are not yet covered */ if (intid >=VGIC_MIN_LPI ) -return NULL ; +return vgic_its_get_lpi (kvm, intid);WARN (1 ,"Looking up struct vgic_irq for reserved INTID" );return NULL ;
virt/kvm/arm/vgic/vgic.h +6 -0
@@ -78,6 +78,7 @@ bool vgic_has_its(struct kvm *kvm);int vits_init (struct kvm *kvm,struct vgic_its *its);void vits_destroy (struct kvm *kvm,struct vgic_its *its);int kvm_vgic_register_its_device (void ); +struct vgic_irq *vgic_its_get_lpi (struct kvm *kvm,u32 intid);#else static inline void vgic_v3_process_maintenance (struct kvm_vcpu *vcpu) {
@@ -148,6 +149,11 @@ static int kvm_vgic_register_its_device(void) {return -ENODEV ; } + +static inline struct vgic_irq *vgic_its_get_lpi (struct kvm *kvm,u32 intid) +{ +return NULL ; +}#endif int kvm_register_vgic_device (unsigned long type);
Signature · 1 line
2.8.2
PATCH v5 11/13 KVM: arm64: implement ITS command queue command handlers
In reply to Andre PrzywaraThe connection between a device, an event ID, the LPI number and the allocated CPU is stored in in-memory tables in a GICv3, but their format is not specified by the spec. Instead software uses a command queue in a ring buffer to let the ITS implementation use their own format.
Implement handlers for the various ITS commands and let them store the requested relation into our own data structures.
To avoid kmallocs inside the ITS spinlock, we preallocate possibly needed memory outside of the lock and free that if it turns out to be not needed (mostly error handling).
Error handling is very basic at this point, as we don't have a good way of communicating errors to the guest (usually a SError). The INT command handler is missing at this point, as we gain the capability of actually injecting MSIs into the guest only later on.
--- include/linux/irqchip/arm-gic-v3.h | 19 +- virt/kvm/arm/vgic/vgic-its.c | 513 ++++++++++++++++++++++++++++++++++++- 2 files changed, 530 insertions(+), 2 deletions(-)
include/linux/irqchip/arm-gic-v3.h +18 -1
@@ -254,7 +254,10 @@*/ #define GITS_CMD_MAPD 0x08#define GITS_CMD_MAPC 0x09 -#define GITS_CMD_MAPVI 0x0a +#define GITS_CMD_MAPTI 0x0a +/* older GIC documentation used MAPVI for this command */ +#define GITS_CMD_MAPVI GITS_CMD_MAPTI +#define GITS_CMD_MAPI 0x0b#define GITS_CMD_MOVI 0x01#define GITS_CMD_DISCARD 0x0f#define GITS_CMD_INV 0x0c
@@ -265,6 +268,20 @@#define GITS_CMD_SYNC 0x05/* +* ITS error numbers +*/ +#define E_ITS_MOVI_UNMAPPED_INTERRUPT 0x010107 +#define E_ITS_MOVI_UNMAPPED_COLLECTION 0x010109 +#define E_ITS_CLEAR_UNMAPPED_INTERRUPT 0x010507 +#define E_ITS_MAPC_PROCNUM_OOR 0x010902 +#define E_ITS_MAPTI_UNMAPPED_DEVICE 0x010a04 +#define E_ITS_MAPTI_PHYSICALID_OOR 0x010a06 +#define E_ITS_INV_UNMAPPED_INTERRUPT 0x010c07 +#define E_ITS_INVALL_UNMAPPED_COLLECTION 0x010d09 +#define E_ITS_MOVALL_PROCNUM_OOR 0x010e01 +#define E_ITS_DISCARD_UNMAPPED_INTERRUPT 0x010f07 + +/* * CPU interface registers */ #define ICC_CTLR_EL1_EOImode_drop_dir (0U << 1)
virt/kvm/arm/vgic/vgic-its.c +512 -1
549 lines not on this page · raw message
Signature · 1 line
2.8.2
PATCH v5 1/13 KVM: arm/arm64: move redistributor kvm_io_devices
In reply to Andre PrzywaraLogically a GICv3 redistributor is assigned to a (v)CPU, so we should aim to keep redistributor related variables out of our struct vgic_dist.
Let's start by replacing the redistributor related kvm_io_device array with two members in our existing struct vgic_cpu, which are naturally per-VCPU and thus don't require any allocation / freeing. So apart from the better fit with the redistributor design this saves some code as well.
--- include/kvm/vgic/vgic.h | 8 +++++++- virt/kvm/arm/vgic/vgic-init.c | 1 - virt/kvm/arm/vgic/vgic-mmio-v3.c | 22 ++++++++-------------- 3 files changed, 15 insertions(+), 16 deletions(-)
include/kvm/vgic/vgic.h +7 -1
@@ -145,7 +145,6 @@ struct vgic_dist {struct vgic_irq *spis;struct vgic_io_device dist_iodev; -struct vgic_io_device *redist_iodevs; };struct vgic_v2_cpu_if {
@@ -193,6 +192,13 @@ struct vgic_cpu {struct list_head ap_list_head;u64 live_lrs; + +/* +* Members below are used with GICv3 emulation only and represent +* parts of the redistributor. +*/ +struct vgic_io_device rd_iodev; +struct vgic_io_device sgi_iodev; };int kvm_vgic_addr (struct kvm *kvm,unsigned long type,u64 *addr,bool write);
virt/kvm/arm/vgic/vgic-init.c +0 -1
@@ -271,7 +271,6 @@ static void kvm_vgic_dist_destroy(struct kvm *kvm) dist->initialized = false;kfree (dist->spis); -kfree (dist->redist_iodevs); dist->nr_spis =0 ;mutex_unlock (&kvm->lock);
virt/kvm/arm/vgic/vgic-mmio-v3.c +8 -14
@@ -285,21 +285,14 @@ unsigned int vgic_v3_init_dist_iodev(struct vgic_io_device *dev)int vgic_register_redist_iodevs (struct kvm *kvm,gpa_t redist_base_address) { -int nr_vcpus =atomic_read (&kvm->online_vcpus);struct kvm_vcpu *vcpu; -struct vgic_io_device *devices;int c, ret =0 ; - devices =kmalloc (sizeof (struct vgic_io_device ) * nr_vcpus *2 , -GFP_KERNEL ); -if (!devices) -return -ENOMEM ; -kvm_for_each_vcpu (c, vcpu, kvm) {gpa_t rd_base = redist_base_address + c *SZ_64K *2 ;gpa_t sgi_base = rd_base +SZ_64K ; -struct vgic_io_device *rd_dev = &devices[c *2 ]; -struct vgic_io_device *sgi_dev = &devices[c *2 +1 ]; +struct vgic_io_device *rd_dev = &vcpu->arch.vgic_cpu.rd_iodev; +struct vgic_io_device *sgi_dev = &vcpu->arch.vgic_cpu.sgi_iodev;kvm_iodevice_init (&rd_dev->dev, &kvm_io_gic_ops); rd_dev->base_addr = rd_base;
@@ -335,14 +328,15 @@ int vgic_register_redist_iodevs(struct kvm *kvm, gpa_t redist_base_address)if (ret) {/* The current c failed, so we start with the previous one. */ for (c--; c >=0 ; c--) { +struct vgic_cpu *vgic_cpu; + + vcpu =kvm_get_vcpu (kvm, c); + vgic_cpu = &vcpu->arch.vgic_cpu;kvm_io_bus_unregister_dev (kvm,KVM_MMIO_BUS , - &devices[c *2 ].dev); + &vgic_cpu->rd_iodev.dev);kvm_io_bus_unregister_dev (kvm,KVM_MMIO_BUS , - &devices[c *2 +1 ].dev); + &vgic_cpu->sgi_iodev.dev); } -kfree (devices); - }else { - kvm->arch.vgic.redist_iodevs = devices; }return ret;
Signature · 1 line
2.8.2
PATCH v5 2/13 KVM: arm/arm64: check return value for kvm_register_vgic_device
In reply to Andre Przywarakvm_register_device_ops() can return an error, so lets check its return value and propagate this up the call chain.
--- virt/kvm/arm/vgic/vgic-kvm-device.c | 15 +++++++++------ virt/kvm/arm/vgic/vgic-v2.c | 7 ++++++- virt/kvm/arm/vgic/vgic-v3.c | 15 +++++++++++++-- virt/kvm/arm/vgic/vgic.h | 2 +- 4 files changed, 29 insertions(+), 10 deletions(-)
virt/kvm/arm/vgic/vgic-kvm-device.c +9 -6
@@ -210,20 +210,24 @@ static void vgic_destroy(struct kvm_device *dev)kfree (dev); } -void kvm_register_vgic_device (unsigned long type) +int kvm_register_vgic_device (unsigned long type) { +int ret = -ENODEV ; +switch (type) {case KVM_DEV_TYPE_ARM_VGIC_V2 : -kvm_register_device_ops (&kvm_arm_vgic_v2_ops, -KVM_DEV_TYPE_ARM_VGIC_V2 ); + ret =kvm_register_device_ops (&kvm_arm_vgic_v2_ops, +KVM_DEV_TYPE_ARM_VGIC_V2 );break ;#ifdef CONFIG_KVM_ARM_VGIC_V3 case KVM_DEV_TYPE_ARM_VGIC_V3 : -kvm_register_device_ops (&kvm_arm_vgic_v3_ops, -KVM_DEV_TYPE_ARM_VGIC_V3 ); + ret =kvm_register_device_ops (&kvm_arm_vgic_v3_ops, +KVM_DEV_TYPE_ARM_VGIC_V3 );break ;#endif } + +return ret; }/** vgic_attr_regs_access: allows user space to read/write VGIC registers
@@ -428,4 +432,3 @@ struct kvm_device_ops kvm_arm_vgic_v3_ops = { };#endif /* CONFIG_KVM_ARM_VGIC_V3 */ -
virt/kvm/arm/vgic/vgic-v2.c +6 -1
@@ -340,7 +340,12 @@ int vgic_v2_probe(const struct gic_kvm_info *info) } kvm_vgic_global_state.can_emulate_gicv2 = true; -kvm_register_vgic_device (KVM_DEV_TYPE_ARM_VGIC_V2 ); + ret =kvm_register_vgic_device (KVM_DEV_TYPE_ARM_VGIC_V2 ); +if (ret) { +kvm_err ("Cannot register GICv2 KVM device\n" ); +iounmap (kvm_vgic_global_state.vctrl_base); +return ret; + } kvm_vgic_global_state.vcpu_base = info->vcpu.start; kvm_vgic_global_state.type =VGIC_V2 ;
virt/kvm/arm/vgic/vgic-v3.c +13 -2
@@ -292,6 +292,7 @@ out:int vgic_v3_probe (const struct gic_kvm_info *info) {u32 ich_vtr_el2 =kvm_call_hyp (__vgic_v3_get_ich_vtr_el2); +int ret;/* * The ListRegs field is 5 bits, but there is a architectural
@@ -315,12 +316,22 @@ int vgic_v3_probe(const struct gic_kvm_info *info) }else { kvm_vgic_global_state.vcpu_base = info->vcpu.start; kvm_vgic_global_state.can_emulate_gicv2 = true; -kvm_register_vgic_device (KVM_DEV_TYPE_ARM_VGIC_V2 ); + ret =kvm_register_vgic_device (KVM_DEV_TYPE_ARM_VGIC_V2 ); +if (ret) { +kvm_err ("Cannot register GICv2 KVM device.\n" ); +return ret; + }kvm_info ("vgic-v2@%llx\n" , info->vcpu.start); } + ret =kvm_register_vgic_device (KVM_DEV_TYPE_ARM_VGIC_V3 ); +if (ret) { +kvm_err ("Cannot register GICv3 KVM device.\n" ); +kvm_unregister_device_ops (KVM_DEV_TYPE_ARM_VGIC_V2 ); +return ret; + } +if (kvm_vgic_global_state.vcpu_base ==0 )kvm_info ("disabling GICv2 emulation\n" ); -kvm_register_vgic_device (KVM_DEV_TYPE_ARM_VGIC_V3 ); kvm_vgic_global_state.vctrl_base =NULL ; kvm_vgic_global_state.type =VGIC_V3 ;
virt/kvm/arm/vgic/vgic.h +1 -1
@@ -124,7 +124,7 @@ static inline int vgic_register_redist_iodevs(struct kvm *kvm, }#endif -void kvm_register_vgic_device (unsigned long type); +int kvm_register_vgic_device (unsigned long type);int vgic_lazy_init (struct kvm *kvm);int vgic_init (struct kvm *kvm);
Signature · 1 line
2.8.2
PATCH v5 5/13 KVM: arm64: handle ITS related GICv3 redistributor registers
In reply to Andre PrzywaraIn the GICv3 redistributor there are the PENDBASER and PROPBASER registers which we did not emulate so far, as they only make sense when having an ITS. In preparation for that emulate those MMIO accesses by storing the 64-bit data written into it into a variable which we later read in the ITS emulation.
--- include/kvm/vgic/vgic.h | 13 +++++++++ virt/kvm/arm/vgic/vgic-mmio-v3.c | 58 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 69 insertions(+), 2 deletions(-)
include/kvm/vgic/vgic.h +13 -0
@@ -145,6 +145,14 @@ struct vgic_dist {struct vgic_irq *spis;struct vgic_io_device dist_iodev; + +/* +* Contains the address of the LPI configuration table. +* Since we report GICR_TYPER.CommonLPIAff as 0b00, we can share +* one address across all redistributors. +* GICv3 spec: 6.1.2 "LPI Configuration tables" +*/ +u64 propbaser; };struct vgic_v2_cpu_if {
@@ -199,6 +207,11 @@ struct vgic_cpu {*/ struct vgic_io_device rd_iodev;struct vgic_io_device sgi_iodev; + +/* Points to the LPI pending tables for the redistributor */ +u64 pendbaser; + +bool lpis_enabled; };int kvm_vgic_addr (struct kvm *kvm,unsigned long type,u64 *addr,bool write);
virt/kvm/arm/vgic/vgic-mmio-v3.c +56 -2
78 lines not on this page · raw message
Signature · 1 line
2.8.2
PATCH v5 6/13 KVM: arm64: introduce ITS emulation file with stub functions
In reply to Andre PrzywaraThe ARM GICv3 ITS emulation code goes into a separate file, but needs to be connected to the GICv3 emulation, of which it is an option.
Introduce the skeleton with function stubs to be filled later. Introduce the basic ITS data structure and initialize it, but don't return any success yet, as we are not yet ready for the show.
--- arch/arm64/kvm/Makefile | 1 + include/kvm/vgic/vgic.h | 9 ++++ include/linux/irqchip/arm-gic-v3.h | 1 + virt/kvm/arm/vgic/vgic-its.c | 84 ++++++++++++++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic-mmio-v3.c | 38 ++++++++++++++++- virt/kvm/arm/vgic/vgic.h | 17 ++++++++ 6 files changed, 149 insertions(+), 1 deletion(-) create mode 100644 virt/kvm/arm/vgic/vgic-its.c
arch/arm64/kvm/Makefile +1 -0
@@ -30,6 +30,7 @@ kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/vgic/vgic-mmio.o kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/vgic/vgic-mmio-v2.o kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/vgic/vgic-mmio-v3.o kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/vgic/vgic-kvm-device.o +kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/vgic/vgic-its.o else kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/vgic.o kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/vgic-v2.o
include/kvm/vgic/vgic.h +9 -0
@@ -116,6 +116,15 @@ struct vgic_io_device {struct kvm_io_device dev; }; +struct vgic_its { +/* The base address of the ITS control register frame */ +gpa_t vgic_its_base; + +bool enabled; +struct vgic_io_device iodev; +spinlock_t lock; +}; +struct vgic_dist {bool in_kernel;bool ready;
include/linux/irqchip/arm-gic-v3.h +1 -0
@@ -175,6 +175,7 @@#define GITS_CWRITER 0x0088#define GITS_CREADR 0x0090#define GITS_BASER 0x0100 +#define GITS_IDREGS_BASE 0xffd0#define GITS_PIDR2 GICR_PIDR2#define GITS_TRANSLATER 0x10040
virt/kvm/arm/vgic/vgic-its.c new file +84 -0
84 lines not on this page · raw message
virt/kvm/arm/vgic/vgic-mmio-v3.c +37 -1
@@ -39,6 +39,16 @@ static u64 update_64bit_reg(u64 reg, unsigned int offset, unsigned int len,return reg | ((u64 )val << (offset *8 )); } +bool vgic_has_its (struct kvm *kvm) +{ +struct vgic_dist *dist = &kvm->arch.vgic; + +if (dist->vgic_model !=KVM_DEV_TYPE_ARM_VGIC_V3 ) +return false; + +return false; +} +static unsigned long vgic_mmio_read_v3_misc (struct kvm_vcpu *vcpu,gpa_t addr,unsigned int len) {
@@ -124,6 +134,32 @@ static void vgic_mmio_write_irouter(struct kvm_vcpu *vcpu,spin_unlock (&irq->irq_lock); } +static unsigned long vgic_mmio_read_v3r_ctlr (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len) +{ +struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; + +return vgic_cpu->lpis_enabled ?GICR_CTLR_ENABLE_LPIS :0 ; +} + + +static void vgic_mmio_write_v3r_ctlr (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len, +unsigned long val) +{ +struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; +bool was_enabled = vgic_cpu->lpis_enabled; + +if (!vgic_has_its (vcpu->kvm)) +return ; + + vgic_cpu->lpis_enabled = val &GICR_CTLR_ENABLE_LPIS ; + +if (!was_enabled && vgic_cpu->lpis_enabled) { +/* Eventually do something */ + } +} +static unsigned long vgic_mmio_read_v3r_typer (struct kvm_vcpu *vcpu,gpa_t addr,unsigned int len) {
@@ -272,7 +308,7 @@ static const struct vgic_register_region vgic_v3_dist_registers[] = {static const struct vgic_register_region vgic_v3_rdbase_registers[] = {REGISTER_DESC_WITH_LENGTH (GICR_CTLR , - vgic_mmio_read_raz, vgic_mmio_write_wi,4 , + vgic_mmio_read_v3r_ctlr, vgic_mmio_write_v3r_ctlr,4 , VGIC_ACCESS_32bit), REGISTER_DESC_WITH_LENGTH(GICR_IIDR, vgic_mmio_read_v3r_iidr, vgic_mmio_write_wi, 4,
virt/kvm/arm/vgic/vgic.h +17 -0
@@ -71,6 +71,9 @@ void vgic_v3_enable(struct kvm_vcpu *vcpu);int vgic_v3_probe (const struct gic_kvm_info *info);int vgic_v3_map_resources (struct kvm *kvm);int vgic_register_redist_iodevs (struct kvm *kvm,gpa_t dist_base_address); +bool vgic_has_its (struct kvm *kvm); +int vits_init (struct kvm *kvm,struct vgic_its *its); +void vits_destroy (struct kvm *kvm,struct vgic_its *its);#else static inline void vgic_v3_process_maintenance (struct kvm_vcpu *vcpu) {
@@ -122,6 +125,20 @@ static inline int vgic_register_redist_iodevs(struct kvm *kvm, {return -ENODEV ; } + +static bool vgic_has_its (struct kvm *kvm) +{ +return false; +} + +static int vits_init (struct kvm *kvm,struct vgic_its *its) +{ +return -ENODEV ; +} + +static inline void vits_destroy (struct kvm *kvm,struct vgic_its *its) +{ +}#endif int kvm_register_vgic_device (unsigned long type);
Signature · 1 line
2.8.2
PATCH v5 3/13 KVM: extend struct kvm_msi to hold a 32-bit device ID
In reply to Andre PrzywaraThe ARM GICv3 ITS MSI controller requires a device ID to be able to assign the proper interrupt vector. On real hardware, this ID is sampled from the bus. To be able to emulate an ITS controller, extend the KVM MSI interface to let userspace provide such a device ID. For PCI devices, the device ID is simply the 16-bit bus-device-function triplet, which should be easily available to the userland tool.
Also there is a new KVM capability which advertises whether the current VM requires a device ID to be set along with the MSI data. This flag is still reported as not available everywhere, later we will enable it when ITS emulation is used.
--- Documentation/virtual/kvm/api.txt | 12 ++++++++++-- include/uapi/linux/kvm.h | 5 ++++- 2 files changed, 14 insertions(+), 3 deletions(-)
Documentation/virtual/kvm/api.txt +10 -2
@@ -2169,10 +2169,18 @@ struct kvm_msi { __u32 address_hi; __u32 data; __u32 flags; - __u8 pad[16]; + __u32 devid; + __u8 pad[12]; }; -No flags are defined so far. The corresponding field must be 0. +flags: KVM_MSI_VALID_DEVID: devid contains a valid value +devid: If KVM_MSI_VALID_DEVID is set, contains a unique device identifier + for the device that wrote the MSI message. + For PCI, this is usually a BFD identifier in the lower 16 bits. + +The per-VM KVM_CAP_MSI_DEVID capability advertises the need to provide +the device ID. If this capability is not set, userland cannot rely on +the kernel to allow the KVM_MSI_VALID_DEVID flag being set. 4.71 KVM_CREATE_PIT2
include/uapi/linux/kvm.h +4 -1
@@ -866,6 +866,7 @@ struct kvm_ppc_smmu_info {#define KVM_CAP_ARM_PMU_V3 126#define KVM_CAP_VCPU_ATTRIBUTES 127#define KVM_CAP_MAX_VCPU_ID 128 +#define KVM_CAP_MSI_DEVID 129#ifdef KVM_CAP_IRQ_ROUTING
@@ -1024,12 +1025,14 @@ struct kvm_one_reg {__u64 addr; }; +#define KVM_MSI_VALID_DEVID (1U << 0)struct kvm_msi {__u32 address_lo;__u32 address_hi;__u32 data;__u32 flags; -__u8 pad[16 ]; +__u32 devid; +__u8 pad[12 ]; };struct kvm_arm_device_addr {
Signature · 1 line
2.8.2
PATCH v5 10/13 KVM: arm64: sync LPI configuration and pending tables
In reply to Andre PrzywaraThe LPI configuration and pending tables of the GICv3 LPIs are held in tables in (guest) memory. To achieve reasonable performance, we cache this data in our own data structures, so we need to sync those two views from time to time. This behaviour is well described in the GICv3 spec and is also exercised by hardware, so the sync points are well known.
Provide functions that read the guest memory and store the information from the configuration and pending tables in the kernel.
--- include/kvm/vgic/vgic.h | 2 + virt/kvm/arm/vgic/vgic-its.c | 145 +++++++++++++++++++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic.h | 6 ++ 3 files changed, 153 insertions(+)
include/kvm/vgic/vgic.h +2 -0
@@ -131,6 +131,8 @@ struct vgic_its {u32 cwriter;struct list_head device_list;struct list_head collection_list; +/* memory used for buffering guest's memory */ +void *buffer_page; };struct vgic_dist {
virt/kvm/arm/vgic/vgic-its.c +145 -0
169 lines not on this page · raw message
virt/kvm/arm/vgic/vgic.h +6 -0
@@ -25,6 +25,7 @@#define IS_VGIC_ADDR_UNDEF (_x) ((_x) == VGIC_ADDR_UNDEF)#define INTERRUPT_ID_BITS_SPIS 10 +#define INTERRUPT_ID_BITS_ITS 16#define VGIC_PRI_BITS 5#define vgic_irq_is_sgi (intid) ((intid) < VGIC_NR_SGIS)
@@ -79,6 +80,7 @@ int vits_init(struct kvm *kvm, struct vgic_its *its);void vits_destroy (struct kvm *kvm,struct vgic_its *its);int kvm_vgic_register_its_device (void );struct vgic_irq *vgic_its_get_lpi (struct kvm *kvm,u32 intid); +void vgic_enable_lpis (struct kvm_vcpu *vcpu);#else static inline void vgic_v3_process_maintenance (struct kvm_vcpu *vcpu) {
@@ -154,6 +156,10 @@ static inline struct vgic_irq *vgic_its_get_lpi(struct kvm *kvm, u32 intid) {return NULL ; } + +static inline void vgic_enable_lpis (struct kvm_vcpu *vcpu) +{ +}#endif int kvm_register_vgic_device (unsigned long type);
Signature · 1 line
2.8.2
PATCH v5 12/13 KVM: arm64: implement MSI injection in ITS emulation
In reply to Andre PrzywaraWhen userland wants to inject a MSI into the guest, we have to use our data structures to find the LPI number and the VCPU to receive the interrupt.
Use the wrapper functions to iterate the linked lists and find the proper Interrupt Translation Table Entry. Then set the pending bit in this ITTE to be later picked up by the LR handling code. Kick the VCPU which is meant to handle this interrupt.
We provide a VGIC emulation model specific routine for the actual MSI injection. The wrapper functions return an error for models not (yet) implementing MSIs (like the GICv2 emulation).
We also provide the handler for the ITS "INT" command, which allows a guest to trigger an MSI via the ITS command queue.
--- virt/kvm/arm/vgic/vgic-its.c | 85 ++++++++++++++++++++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic.h | 6 ++++ 2 files changed, 91 insertions(+)
virt/kvm/arm/vgic/vgic-its.c +85 -0
@@ -46,6 +46,16 @@ static struct vgic_its *find_its(struct kvm *kvm, gpa_t base_address)return NULL ; } +#define ITS_DOORBELL_OFFSET (SZ_64K + 0x40) +static struct vgic_its *find_its_doorbell (struct kvm *kvm,gpa_t doorbell) +{ +/* +* The actual doorbell address is in the second page of the ITS +* frame, at offset 0x40. +*/ +return find_its (kvm, doorbell -ITS_DOORBELL_OFFSET ); +} +struct its_device {struct list_head dev_list;
@@ -363,6 +373,61 @@ static unsigned long vgic_mmio_read_its_idregs(struct kvm_vcpu *vcpu,return 0 ; } +/* +* Translates an incoming MSI request into the redistributor (=VCPU) and +* the associated LPI number. Sets the LPI pending bit and also marks the +* VCPU as having a pending interrupt. +*/ +int vits_inject_msi (struct kvm *kvm,struct kvm_msi *msi) +{ +struct vgic_its *its; +struct its_itte *itte; +struct kvm_vcpu *vcpu; +bool inject = false; +u64 doorbell; +int ret =0 ; + +if (!vgic_has_its (kvm)) +return -ENODEV ; + +if (!(msi->flags &KVM_MSI_VALID_DEVID )) +return -EINVAL ; + + doorbell = (u64 )msi->address_hi <<32 | msi->address_lo; + its =find_its_doorbell (kvm, doorbell); +if (!its) +return -EINVAL ; + +spin_lock (&its->lock); + +if (!its->enabled) { + ret = -EAGAIN ; + goto out_unlock; + } + + itte =find_itte (its, msi->devid, msi->data); +/* Triggering an unmapped IRQ gets silently dropped. */ +if (!itte || !its_is_collection_mapped (itte->collection)) + goto out_unlock; + + vcpu =kvm_get_vcpu (kvm, itte->collection->target_addr); +if (!vcpu || !vcpu->arch.vgic_cpu.lpis_enabled) + goto out_unlock; + + inject = true; + +out_unlock: +spin_unlock (&its->lock); + +if (inject) { +spin_lock (&itte->irq.irq_lock); + itte->irq.pending = true; +vgic_queue_irq_unlock (kvm, &itte->irq); + } + +return ret; +} +struct vgic_irq *vgic_its_get_lpi (struct kvm *kvm,u32 intid) {struct vgic_its *its;
@@ -791,6 +856,23 @@ static int vits_cmd_handle_movall(struct kvm *kvm, struct vgic_its *its,return 0 ; } +/* The INT command injects the LPI associated with that DevID/EvID pair. */ +static int vits_cmd_handle_int (struct kvm *kvm,struct vgic_its *its, +u64 *its_cmd) +{ +u64 doorbell = its->vgic_its_base +ITS_DOORBELL_OFFSET ; +struct kvm_msi msi = { + .address_lo = doorbell &0xffffffff , + .address_hi = doorbell >>32 , + .data =its_cmd_get_id (its_cmd), + .devid =its_cmd_get_deviceid (its_cmd), + .flags =KVM_MSI_VALID_DEVID , + }; + +vits_inject_msi (kvm, &msi); +return 0 ; +} +/* * This function expects the ITS lock to be dropped, so the actual command * handlers must take care of proper locking when needed.
@@ -826,6 +908,9 @@ static int vits_handle_command(struct kvm *kvm, struct vgic_its *its,case GITS_CMD_MOVALL : ret =vits_cmd_handle_movall (kvm, its, its_cmd);break ; +case GITS_CMD_INT : + ret =vits_cmd_handle_int (kvm, its, its_cmd); +break ;case GITS_CMD_INV : ret =vits_cmd_handle_inv (kvm, its, its_cmd);break ;
virt/kvm/arm/vgic/vgic.h +6 -0
@@ -81,6 +81,7 @@ void vits_destroy(struct kvm *kvm, struct vgic_its *its);int kvm_vgic_register_its_device (void );struct vgic_irq *vgic_its_get_lpi (struct kvm *kvm,u32 intid);void vgic_enable_lpis (struct kvm_vcpu *vcpu); +int vits_inject_msi (struct kvm *kvm,struct kvm_msi *msi);#else static inline void vgic_v3_process_maintenance (struct kvm_vcpu *vcpu) {
@@ -160,6 +161,11 @@ static inline struct vgic_irq *vgic_its_get_lpi(struct kvm *kvm, u32 intid)static inline void vgic_enable_lpis (struct kvm_vcpu *vcpu) { } + +static inline int vits_inject_msi (struct kvm *kvm,struct kvm_msi *msi) +{ +return -ENODEV ; +}#endif int kvm_register_vgic_device (unsigned long type);
Signature · 1 line
2.8.2
PATCH v5 13/13 KVM: arm64: enable ITS emulation as a virtual MSI controller
In reply to Andre PrzywaraIf userspace has provided a base address for the ITS register frame, we enable the bits that advertise LPIs in the GICv3.
When the guest has enabled LPIs and the ITS, we enable the emulation part by initializing the ITS data structures and trapping on ITS register frame accesses by the guest.
Also we enable the KVM_SIGNAL_MSI feature to allow userland to inject MSIs into the guest. Not having enabled the ITS emulation will lead to a -ENODEV when trying to inject a MSI.
--- Documentation/virtual/kvm/api.txt | 2 +- arch/arm64/kvm/Kconfig | 1 + arch/arm64/kvm/reset.c | 10 ++++++++++ include/kvm/vgic/vgic.h | 5 +++++ virt/kvm/arm/vgic.c | 5 +++++ virt/kvm/arm/vgic/vgic-init.c | 3 +++ virt/kvm/arm/vgic/vgic-its.c | 2 ++ virt/kvm/arm/vgic/vgic-mmio-v3.c | 14 ++++++++++---- virt/kvm/arm/vgic/vgic.c | 8 ++++++++ 9 files changed, 45 insertions(+), 5 deletions(-)
Documentation/virtual/kvm/api.txt +1 -1
@@ -2156,7 +2156,7 @@ after pausing the vcpu, but before it is resumed. 4.71 KVM_SIGNAL_MSI Capability: KVM_CAP_SIGNAL_MSI -Architectures: x86 +Architectures: x86 arm64 Type: vm ioctl Parameters: struct kvm_msi (in) Returns: >0 on delivery, 0 if guest blocked the MSI, and -1 on error
arch/arm64/kvm/Kconfig +1 -0
@@ -36,6 +36,7 @@ config KVM select HAVE_KVM_IRQFD select KVM_ARM_VGIC_V3 select KVM_ARM_PMU if HW_PERF_EVENTS + select HAVE_KVM_MSI ---help--- Support hosting virtualized guest machines. We don't support KVM with 16K page tables yet, due to the multiple
arch/arm64/kvm/reset.c +10 -0
@@ -86,6 +86,16 @@ int kvm_arch_dev_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_VCPU_ATTRIBUTES : r =1 ;break ; +case KVM_CAP_MSI_DEVID : +#ifdef CONFIG_KVM_NEW_VGIC +if (!kvm) + r = -EINVAL ; +else + r = kvm->arch.vgic.msis_require_devid; +#else + r = -EINVAL ; +#endif +break ;default : r =0 ; }
include/kvm/vgic/vgic.h +5 -0
@@ -143,6 +143,9 @@ struct vgic_dist {/* vGIC model the kernel emulates for the guest (GICv2 or GICv3) */ u32 vgic_model; +/* Do injected MSIs require an additional device ID? */ +bool msis_require_devid; +int nr_spis;/* TODO: Consider moving to global state */
@@ -281,4 +284,6 @@ static inline int kvm_vgic_get_max_vcpus(void) return kvm_vgic_global_state.max_gic_vcpus; } +int kvm_send_userspace_msi (struct kvm *kvm,struct kvm_msi *msi); +#endif /* __ASM_ARM_KVM_VGIC_VGIC_H */
virt/kvm/arm/vgic.c +5 -0
@@ -2438,3 +2438,8 @@ int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e, {return 0 ; } + +int kvm_send_userspace_msi (struct kvm *kvm,struct kvm_msi *msi) +{ +return -ENODEV ; +}
virt/kvm/arm/vgic/vgic-init.c +3 -0
@@ -254,6 +254,9 @@ int vgic_init(struct kvm *kvm)if (ret) goto out; +if (vgic_has_its (kvm)) + dist->msis_require_devid = true; +list_for_each_entry (its, &kvm->arch.vits_list, its_list) { ret =vits_init (kvm, its);if (ret)
virt/kvm/arm/vgic/vgic-its.c +2 -0
@@ -1104,6 +1104,8 @@ int vits_init(struct kvm *kvm, struct vgic_its *its) SZ_64K, &iodev->dev);mutex_unlock (&kvm->slots_lock); + its->enabled = false; +return ret; }
virt/kvm/arm/vgic/vgic-mmio-v3.c +10 -4
@@ -63,7 +63,12 @@ static unsigned long vgic_mmio_read_v3_misc(struct kvm_vcpu *vcpu,case GICD_TYPER : value = vcpu->kvm->arch.vgic.nr_spis +VGIC_NR_PRIVATE_IRQS ; value = (value >>5 ) -1 ; - value |= (INTERRUPT_ID_BITS_SPIS -1 ) <<19 ; +if (vgic_has_its (vcpu->kvm)) { + value |= (INTERRUPT_ID_BITS_ITS -1 ) <<19 ; + value |=GICD_TYPER_LPIS ; + }else { + value |= (INTERRUPT_ID_BITS_SPIS -1 ) <<19 ; + }break ;case GICD_IIDR : value = (PRODUCT_ID_KVM <<24 ) | (IMPLEMENTER_ARM <<0 );
@@ -155,9 +160,8 @@ static void vgic_mmio_write_v3r_ctlr(struct kvm_vcpu *vcpu, vgic_cpu->lpis_enabled = val &GICR_CTLR_ENABLE_LPIS ; -if (!was_enabled && vgic_cpu->lpis_enabled) { -/* Eventually do something */ - } +if (!was_enabled && vgic_cpu->lpis_enabled) +vgic_enable_lpis (vcpu); } static unsigned long vgic_mmio_read_v3r_typer(struct kvm_vcpu *vcpu,
@@ -171,6 +175,8 @@ static unsigned long vgic_mmio_read_v3r_typer(struct kvm_vcpu *vcpu, value |= ((target_vcpu_id &0xffff ) <<8 );if (target_vcpu_id ==atomic_read (&vcpu->kvm->online_vcpus) -1 ) value |=GICR_TYPER_LAST ; +if (vgic_has_its (vcpu->kvm)) + value |=GICR_TYPER_PLPIS ;return extract_bytes (value, addr &7 , len); }
virt/kvm/arm/vgic/vgic.c +8 -0
@@ -617,3 +617,11 @@ bool kvm_vgic_map_is_active(struct kvm_vcpu *vcpu, unsigned int virt_irq)return map_is_active; } + +int kvm_send_userspace_msi (struct kvm *kvm,struct kvm_msi *msi) +{ +if (vgic_has_its (kvm)) +return vits_inject_msi (kvm, msi); +else +return -ENODEV ; +}
Signature · 1 line
2.8.2
Re: [PATCH v5 08/13] KVM: arm64: implement basic ITS register handlers
In reply to Andre PrzywaraQuoted 265 lines · Andre Przywara
Add emulation for some basic MMIO registers used in the ITS emulation. This includes:
- GITS_{CTLR,TYPER,IIDR}
- ID registers
- GITS_{CBASER,CREADR,CWRITER}
(which implement the ITS command buffer handling)Most of the handlers are pretty straight forward, but CWRITER goes some extra miles to allow fine grained locking. The idea here is to let only the first instance iterate through the command ring buffer, CWRITER accesses on other VCPUs meanwhile will be picked up by that first instance and handled as well. The ITS lock is thus only held for very small periods of time and is dropped before the actual command handler is called.
Signed-off-by Andre Przywara <[email protected]>--- include/kvm/vgic/vgic.h | 6 + include/linux/irqchip/arm-gic-v3.h | 10 ++ virt/kvm/arm/vgic/vgic-its.c | 307 ++++++++++++++++++++++++++++++++++++- virt/kvm/arm/vgic/vgic-mmio-v3.c | 8 +- virt/kvm/arm/vgic/vgic-mmio.h | 6 + 5 files changed, 326 insertions(+), 11 deletions(-)include/kvm/vgic/vgic.h +6
-0@@ -22,6 +22,7 @@#include <linux/spinlock.h> #include <linux/types.h> #include <kvm/iodev.h> +#include <linux/list.h> #define VGIC_V3_MAX_CPUS 255#define VGIC_V2_MAX_CPUS 8@@ -125,6 +126,11 @@ struct vgic_its {bool enabled;struct vgic_io_device iodev;spinlock_t lock; +u64 cbaser; +u32 creadr; +u32 cwriter; +struct list_head device_list; +struct list_head collection_list; };struct vgic_dist {include/linux/irqchip/arm-gic-v3.h +10
-0@@ -176,16 +176,26 @@#define GITS_CREADR 0x0090#define GITS_BASER 0x0100#define GITS_IDREGS_BASE 0xffd0 +#define GITS_PIDR0 0xffe0 +#define GITS_PIDR1 0xffe4#define GITS_PIDR2 GICR_PIDR2 +#define GITS_PIDR4 0xffd0 +#define GITS_CIDR0 0xfff0 +#define GITS_CIDR1 0xfff4 +#define GITS_CIDR2 0xfff8 +#define GITS_CIDR3 0xfffc#define GITS_TRANSLATER 0x10040#define GITS_CTLR_ENABLE (1U << 0)#define GITS_CTLR_QUIESCENT (1U << 31) +#define GITS_TYPER_PLPIS (1UL << 0) +#define GITS_TYPER_IDBITS_SHIFT 8#define GITS_TYPER_DEVBITS_SHIFT 13#define GITS_TYPER_DEVBITS (r) ((((r) >> GITS_TYPER_DEVBITS_SHIFT) & 0x1f) + 1)#define GITS_TYPER_PTA (1UL << 19) +#define GITS_TYPER_HWCOLLCNT_SHIFT 24#define GITS_CBASER_VALID (1UL << 63)#define GITS_CBASER_nCnB (0UL << 59)virt/kvm/arm/vgic/vgic-its.c +171
-0@@ -21,6 +21,7 @@#include <linux/kvm.h> #include <linux/kvm_host.h> #include <linux/interrupt.h> +#include <linux/list.h> #include <linux/uaccess.h> #include <linux/irqchip/arm-gic-v3.h> @@ -32,30 +33,291 @@#include "vgic.h" #include "vgic-mmio.h" +static struct vgic_its *find_its (struct kvm *kvm,gpa_t base_address) +{ +struct vgic_its *its; + +list_for_each_entry (its, &kvm->arch.vits_list, its_list) { +if (its->vgic_its_base == base_address) +return its; + } + +return NULL ; +} + +struct its_device { +struct list_head dev_list; + +/* the head for the list of ITTEs */ +struct list_head itt; +u32 device_id; +}; + +#define COLLECTION_NOT_MAPPED ((u32)-1) + +struct its_collection { +struct list_head coll_list; + +u32 collection_id; +u32 target_addr; +}; + +#define its_is_collection_mapped (coll) ((coll) && \ + ((coll)->target_addr != COLLECTION_NOT_MAPPED)) + +struct its_itte { +struct list_head itte_list; + +struct its_collection *collection; +u32 lpi; +u32 event_id; +}; + +#define BASER_BASE_ADDRESS (x) ((x) & 0xfffffffff000ULL) + +#define ITS_FRAME (addr) ((addr) & ~(SZ_64K - 1)) + +static unsigned long vgic_mmio_read_its_ctlr (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len) +{ +struct vgic_its *its =find_its (vcpu->kvm,ITS_FRAME (addr)); +u32 reg =0 ; + +spin_lock (&its->lock); +if (its->creadr == its->cwriter) + reg |=GITS_CTLR_QUIESCENT ; +if (its->enabled) + reg |=GITS_CTLR_ENABLE ; +spin_unlock (&its->lock); + +return reg; +} + +static void vgic_mmio_write_its_ctlr (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len, +unsigned long val) +{ +struct vgic_its *its =find_its (vcpu->kvm,ITS_FRAME (addr)); + + its->enabled = !!(val &GITS_CTLR_ENABLE ); +} + +static unsigned long vgic_mmio_read_its_typer (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len) +{ +u64 reg =GITS_TYPER_PLPIS ; + +/* +* We use linear CPU numbers for redistributor addressing, +* so GITS_TYPER.PTA is 0. +* Also we force all PROPBASER registers to be the same, so +* CommonLPIAff is 0 as well. +* As we hold all LPI mapping related data structures in the kernel +* (mimicing what the spec describes as "held in hardware"), we can +* claim to support a high number of "hardware" mapped collections +* (since we use linked lists to store them). +* However to avoid memory waste, we keep the number of IDBits and +* DevBits low - as least for the time being. +*/ + reg |=0xff <<GITS_TYPER_HWCOLLCNT_SHIFT ; + reg |=0x0f <<GITS_TYPER_DEVBITS_SHIFT ; + reg |=0x0f <<GITS_TYPER_IDBITS_SHIFT ; + +return extract_bytes (reg, addr &7 , len); +} + +static unsigned long vgic_mmio_read_its_iidr (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len) +{ +return (PRODUCT_ID_KVM <<24 ) | (IMPLEMENTER_ARM <<0 ); +} + +static unsigned long vgic_mmio_read_its_idregs (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len) +{ +switch (addr &0xffff ) { +case GITS_PIDR0 : +return 0x92 ;/* part number, bits[7:0] */ +case GITS_PIDR1 : +return 0xb4 ;/* part number, bits[11:8] */ +case GITS_PIDR2 : +return GIC_PIDR2_ARCH_GICv3 |0x0b ; +case GITS_PIDR4 : +return 0x40 ;/* This is a 64K software visible page */ +/* The following are the ID registers for (any) GIC. */ +case GITS_CIDR0 : +return 0x0d ; +case GITS_CIDR1 : +return 0xf0 ; +case GITS_CIDR2 : +return 0x05 ; +case GITS_CIDR3 : +return 0xb1 ; + } + +return 0 ; +} + +static void its_free_itte (struct its_itte *itte) +{ +list_del (&itte->itte_list); +kfree (itte); +} + +/* +* This function expects the ITS lock to be dropped, so the actual command +* handlers must take care of proper locking when needed. +*/ +static int vits_handle_command (struct kvm *kvm,struct vgic_its *its, +u64 *its_cmd) +{ +return -ENODEV ; +} + +static unsigned long vgic_mmio_read_its_cbaser (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len) +{ +struct vgic_its *its =find_its (vcpu->kvm,ITS_FRAME (addr)); + +return extract_bytes (its->cbaser, addr &7 , len); +} + +static void vgic_mmio_write_its_cbaser (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len, +unsigned long val) +{ +struct vgic_its *its =find_its (vcpu->kvm,ITS_FRAME (addr)); + +/* When GITS_CTLR.Enable is 1, this register is RO. */ +if (its->enabled) +return ; + +spin_lock (&its->lock); + its->cbaser =update_64bit_reg (its->cbaser, addr &7 , len, val); + its->creadr =0 ; +/* +* CWRITER is architecturally UNKNOWN on reset, but we need to reset +* it to CREADR to make sure we start with an empty command buffer. +*/ + its->cwriter = its->creadr; +spin_unlock (&its->lock); +} +
virt/kvm/arm/vgic/vgic-its.c line 215
Quoted 10 lines
virt/kvm/arm/vgic/vgic-its.c +10
-0+#define ITS_CMD_BUFFER_SIZE (baser) ((((baser) & 0xff) + 1) << 12) + +/* +* By writing to CWRITER the guest announces new commands to be processed. +* Since we cannot read from guest memory inside the ITS spinlock, we +* iterate over the command buffer (with the lock dropped) until the read +* pointer matches the write pointer. Other VCPUs writing this register in the +* meantime will just update the write pointer, leaving the command +* processing to the first instance of the function. +*/
[...]
I already expressed my concerns about how fragile this code is, and offered an alternative. I'm not going to copy-paste them here, but my comments still stand.
Thanks,
M.
Signature · 1 line
Jazz is not dead. It just smells funny...
Re: [PATCH v5 01/13] KVM: arm/arm64: move redistributor kvm_io_devices
In reply to Andre PrzywaraQuoted 29 lines · Andre Przywara
Logically a GICv3 redistributor is assigned to a (v)CPU, so we should aim to keep redistributor related variables out of our struct
vgic_dist.Let's start by replacing the redistributor related
kvm_io_devicearray with two members in our existing structvgic_cpu, which are naturally per-VCPU and thus don't require any allocation / freeing. So apart from the better fit with the redistributor design this saves some code as well.Signed-off-by Andre Przywara <[email protected]>--- include/kvm/vgic/vgic.h | 8 +++++++- virt/kvm/arm/vgic/vgic-init.c | 1 - virt/kvm/arm/vgic/vgic-mmio-v3.c | 22 ++++++++-------------- 3 files changed, 15 insertions(+), 16 deletions(-)include/kvm/vgic/vgic.h +0
-1@@ -145,7 +145,6 @@ struct vgic_dist {struct vgic_irq *spis;struct vgic_io_device dist_iodev; -struct vgic_io_device *redist_iodevs; };struct vgic_v2_cpu_if {
include/kvm/vgic/vgic.h struct vgic_cpu { · line 201
Quoted 10 lines
include/kvm/vgic/vgic.h +7
-0@@ -193,6 +192,13 @@ struct vgic_cpu {struct list_head ap_list_head;u64 live_lrs; + +/* +* Members below are used with GICv3 emulation only and represent +* parts of the redistributor. +*/ +struct vgic_io_device rd_iodev; +struct vgic_io_device sgi_iodev;
Since we don't have GICv3 support on 32bit (yet), it'd make sense to make this conditional on CONFIG_KVM_ARM_VGIC_V3.
Quoted 55 lines
include/kvm/vgic/vgic.h +0
-0};int kvm_vgic_addr (struct kvm *kvm,unsigned long type,u64 *addr,bool write);virt/kvm/arm/vgic/vgic-init.c +0
-1@@ -271,7 +271,6 @@ static void kvm_vgic_dist_destroy(struct kvm *kvm) dist->initialized = false;kfree (dist->spis); -kfree (dist->redist_iodevs); dist->nr_spis =0 ;mutex_unlock (&kvm->lock);virt/kvm/arm/vgic/vgic-mmio-v3.c +7
-10@@ -285,21 +285,14 @@ unsigned int vgic_v3_init_dist_iodev(struct vgic_io_device *dev)int vgic_register_redist_iodevs (struct kvm *kvm,gpa_t redist_base_address) { -int nr_vcpus =atomic_read (&kvm->online_vcpus);struct kvm_vcpu *vcpu; -struct vgic_io_device *devices;int c, ret =0 ; - devices =kmalloc (sizeof (struct vgic_io_device ) * nr_vcpus *2 , -GFP_KERNEL ); -if (!devices) -return -ENOMEM ; -kvm_for_each_vcpu (c, vcpu, kvm) {gpa_t rd_base = redist_base_address + c *SZ_64K *2 ;gpa_t sgi_base = rd_base +SZ_64K ; -struct vgic_io_device *rd_dev = &devices[c *2 ]; -struct vgic_io_device *sgi_dev = &devices[c *2 +1 ]; +struct vgic_io_device *rd_dev = &vcpu->arch.vgic_cpu.rd_iodev; +struct vgic_io_device *sgi_dev = &vcpu->arch.vgic_cpu.sgi_iodev;kvm_iodevice_init (&rd_dev->dev, &kvm_io_gic_ops); rd_dev->base_addr = rd_base;@@ -335,14 +328,15 @@ int vgic_register_redist_iodevs(struct kvm *kvm, gpa_t redist_base_address)if (ret) {/* The current c failed, so we start with the previous one. */ for (c--; c >=0 ; c--) { +struct vgic_cpu *vgic_cpu; + + vcpu =kvm_get_vcpu (kvm, c); + vgic_cpu = &vcpu->arch.vgic_cpu;kvm_io_bus_unregister_dev (kvm,KVM_MMIO_BUS , - &devices[c *2 ].dev); + &vgic_cpu->rd_iodev.dev);
virt/kvm/arm/vgic/vgic-mmio-v3.c int vgic_register_redist_iodevs(struct kvm *kvm, gpa_t redist_base_address) · line 342
Quoted 10 lines
virt/kvm/arm/vgic/vgic-mmio-v3.c +1
-4kvm_io_bus_unregister_dev (kvm,KVM_MMIO_BUS , - &devices[c *2 +1 ].dev); + &vgic_cpu->sgi_iodev.dev); } -kfree (devices); - }else { - kvm->arch.vgic.redist_iodevs = devices; }return ret;
Thanks,
M.
Signature · 1 line
Jazz is not dead. It just smells funny...
Re: [PATCH v5 05/13] KVM: arm64: handle ITS related GICv3 redistributor registers
In reply to Andre PrzywaraQuoted 49 lines · Andre Przywara
In the GICv3 redistributor there are the PENDBASER and PROPBASER registers which we did not emulate so far, as they only make sense when having an ITS. In preparation for that emulate those MMIO accesses by storing the 64-bit data written into it into a variable which we later read in the ITS emulation.
Signed-off-by Andre Przywara <[email protected]>--- include/kvm/vgic/vgic.h | 13 +++++++++ virt/kvm/arm/vgic/vgic-mmio-v3.c | 58 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 69 insertions(+), 2 deletions(-)include/kvm/vgic/vgic.h +13
-0@@ -145,6 +145,14 @@ struct vgic_dist {struct vgic_irq *spis;struct vgic_io_device dist_iodev; + +/* +* Contains the address of the LPI configuration table. +* Since we report GICR_TYPER.CommonLPIAff as 0b00, we can share +* one address across all redistributors. +* GICv3 spec: 6.1.2 "LPI Configuration tables" +*/ +u64 propbaser; };struct vgic_v2_cpu_if {@@ -199,6 +207,11 @@ struct vgic_cpu {*/ struct vgic_io_device rd_iodev;struct vgic_io_device sgi_iodev; + +/* Points to the LPI pending tables for the redistributor */ +u64 pendbaser; + +bool lpis_enabled; };int kvm_vgic_addr (struct kvm *kvm,unsigned long type,u64 *addr,bool write);virt/kvm/arm/vgic/vgic-mmio-v3.c +0
-0@@ -29,6 +29,16 @@ static unsigned long extract_bytes(unsigned long data, unsigned int offset,return (data >> (offset *8 )) &GENMASK_ULL (num *8 -1 ,0 );
virt/kvm/arm/vgic/vgic-mmio-v3.c static unsigned long extract_bytes(unsigned long data, unsigned int offset, · line 39
Quoted 10 lines
virt/kvm/arm/vgic/vgic-mmio-v3.c +8
-0} +/* allows updates of any half of a 64-bit register (or the whole thing) */ +static u64 update_64bit_reg (u64 reg,unsigned int offset,unsigned int len, +unsigned long val) +{ + offset &=4 ; + reg &=GENMASK_ULL (len *8 -1 ,0 ) << ((len - offset) *8 ); + +return reg | ((u64 )val << (offset *8 ));
Feels like an OCCC entry. It is also undefined because in the 64bit write case, you're shifting a 64bit value by 64 places.
Please rewrite this in a readable way, such as:
int low, high; if (!offset) low = 0; else low = 32; if (len == 4) high = low + 31; else high = 63; reg &= ~GENMASK_ULL(high, low); return reg | ((u64)val << low);
Easily understood, no undefined behaviour.
Quoted 60 lines
virt/kvm/arm/vgic/vgic-mmio-v3.c +46
-0+} +static unsigned long vgic_mmio_read_v3_misc (struct kvm_vcpu *vcpu,gpa_t addr,unsigned int len) {@@ -147,6 +157,50 @@ static unsigned long vgic_mmio_read_v3_idregs(struct kvm_vcpu *vcpu,return 0 ; } +static unsigned long vgic_mmio_read_propbase (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len) +{ +struct vgic_dist *dist = &vcpu->kvm->arch.vgic; + +return extract_bytes (dist->propbaser, addr &7 , len); +} + +static void vgic_mmio_write_propbase (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len, +unsigned long val) +{ +struct vgic_dist *dist = &vcpu->kvm->arch.vgic; +struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; + +/* Storing a value with LPIs already enabled is undefined */ +if (vgic_cpu->lpis_enabled) +return ; + + dist->propbaser =update_64bit_reg (dist->propbaser, addr &4 , len, val); +} + +static unsigned long vgic_mmio_read_pendbase (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len) +{ +struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; + +return extract_bytes (vgic_cpu->pendbaser, addr &7 , len); +} + +static void vgic_mmio_write_pendbase (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len, +unsigned long val) +{ +struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; + +/* Storing a value with LPIs already enabled is undefined */ +if (vgic_cpu->lpis_enabled) +return ; + + vgic_cpu->pendbaser =update_64bit_reg (vgic_cpu->pendbaser, + addr &4 , len, val); +} +/* * The GICv3 per-IRQ registers are split to control PPIs and SGIs in the * redistributors, while SPIs are covered by registers in the distributor @@ -227,10 +281,10 @@ static const struct vgic_register_region vgic_v3_rdbase_registers[] = { vgic_mmio_read_v3r_typer, vgic_mmio_write_wi,8 , VGIC_ACCESS_64bit | VGIC_ACCESS_32bit),
virt/kvm/arm/vgic/vgic-mmio-v3.c static const struct vgic_register_region vgic_v3_rdbase_registers[] = { · line 290
Quoted 10 lines
virt/kvm/arm/vgic/vgic-mmio-v3.c +2
-2REGISTER_DESC_WITH_LENGTH (GICR_PROPBASER , - vgic_mmio_read_raz, vgic_mmio_write_wi,8 , + vgic_mmio_read_propbase, vgic_mmio_write_propbase,8 , VGIC_ACCESS_64bit | VGIC_ACCESS_32bit),REGISTER_DESC_WITH_LENGTH (GICR_PENDBASER , - vgic_mmio_read_raz, vgic_mmio_write_wi,8 , + vgic_mmio_read_pendbase, vgic_mmio_write_pendbase,8 , VGIC_ACCESS_64bit | VGIC_ACCESS_32bit), REGISTER_DESC_WITH_LENGTH(GICR_IDREGS, vgic_mmio_read_v3_idregs, vgic_mmio_write_wi, 48,
Thanks,
M./
Signature · 1 line
Jazz is not dead. It just smells funny...
Re: [PATCH v5 07/13] KVM: arm64: introduce new KVM ITS device
In reply to Andre PrzywaraQuoted 77 lines · Andre Przywara
Introduce a new KVM device that represents an ARM Interrupt Translation Service (ITS) controller. Since there can be multiple of this per guest, we can't piggy back on the existing GICv3 distributor device, but create a new type of KVM device.
On theKVM_CREATE_DEVICEioctl we initialize the ITS data structure and add this ITS to a linked list. Userland is expected to set the frame address using an ioctl during the VM initialization process, this will be used to pick the right ITS for a particular device or MMIO access.Signed-off-by Andre Przywara <[email protected]>--- Documentation/virtual/kvm/devices/arm-vgic.txt | 23 ++++- arch/arm/include/asm/kvm_host.h | 3 + arch/arm/kvm/arm.c | 3 + arch/arm64/include/asm/kvm_host.h | 2 + arch/arm64/include/uapi/asm/kvm.h | 2 + include/kvm/vgic/vgic.h | 2 + include/uapi/linux/kvm.h | 2 + virt/kvm/arm/vgic/vgic-init.c | 13 +++ virt/kvm/arm/vgic/vgic-its.c | 121 +++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic-kvm-device.c | 7 +- virt/kvm/arm/vgic/vgic-mmio-v3.c | 2 +- virt/kvm/arm/vgic/vgic.h | 9 ++ 12 files changed, 182 insertions(+), 7 deletions(-)Documentation/virtual/kvm/devices/arm-vgic.txt +19
-4@@ -4,16 +4,22 @@ ARM Virtual Generic Interrupt Controller (VGIC) Device types supported: KVM_DEV_TYPE_ARM_VGIC_V2 ARM Generic Interrupt Controller v2.0 KVM_DEV_TYPE_ARM_VGIC_V3 ARM Generic Interrupt Controller v3.0 + KVM_DEV_TYPE_ARM_VGIC_ITS ARM Interrupt Translation Service Controller -Only one VGIC instance may be instantiated through either this API or the -legacy KVM_CREATE_IRQCHIP api. The created VGIC will act as the VM interrupt -controller, requiring emulated user-space devices to inject interrupts to the -VGIC instead of directly to CPUs. +Only one VGIC instance of the V2/V3 types above may be instantiated through +either this API or the legacy KVM_CREATE_IRQCHIP api. The created VGIC will +act as the VM interrupt controller, requiring emulated user-space devices to +inject interrupts to the VGIC instead of directly to CPUs. Creating a guest GICv3 device requires a host GICv3 as well. GICv3 implementations with hardware compatibility support allow a guest GICv2 as well. +Creating a virtual ITS controller requires a host GICv3 (but does not depend +on having physical ITS controllers). +There can be multiple ITS controllers per guest, each of them has to have +a separate, non-overlapping MMIO region. + Groups: KVM_DEV_ARM_VGIC_GRP_ADDR Attributes:@@ -39,6 +45,15 @@ Groups: Only valid for KVM_DEV_TYPE_ARM_VGIC_V3. This address needs to be 64K aligned. + KVM_VGIC_V3_ADDR_TYPE_ITS (rw, 64-bit) + Base address in the guest physical address space of the GICv3 ITS + control register frame. The ITS allows MSI(-X) interrupts to be + injected into guests. This extension is optional. If the kernel + does not support the ITS, the call returns -ENODEV. + This address region is solely for the guest to access the ITS control + registers and does not cover the ITS translation register. + Only valid for KVM_DEV_TYPE_ARM_VGIC_ITS. + This address needs to be 64K aligned and the region covers 64K. KVM_DEV_ARM_VGIC_GRP_DIST_REGS Attributes:arch/arm/include/asm/kvm_host.h +0
-0
arch/arm/include/asm/kvm_host.h struct kvm_arch { · line 73
Quoted 5 lines
arch/arm/include/asm/kvm_host.h +2
-0@@ -69,6 +69,9 @@ struct kvm_arch {/* Interrupt controller */ struct vgic_dist vgic; +/* List of virtual ITS MSI controllers */ +struct list_head vits_list;
32bit ARM does not have an ITS, as it doesn't have GICv3 either. Why do we have to have this? Also, you could perfectly move the list into the vgic_dist structure (and rename vgic_dist to something else if that's too ugly).
Quoted 17 lines
arch/arm/include/asm/kvm_host.h +1
-0+int max_vcpus; };arch/arm/kvm/arm.c +1
-0@@ -20,6 +20,7 @@#include <linux/errno.h> #include <linux/err.h> #include <linux/kvm_host.h> +#include <linux/list.h> #include <linux/module.h> #include <linux/vmalloc.h> #include <linux/fs.h>
arch/arm/kvm/arm.c int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) · line 130
Quoted 4 lines
arch/arm/kvm/arm.c +1
-0@@ -126,7 +127,9 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)if (ret) goto out_free_stage2_pgd; +INIT_LIST_HEAD (&kvm->arch.vits_list);
Why can't that be moved to kvm_vgic_early_init? And does it actually need to be done that early?
arch/arm/kvm/arm.c int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) · line 132
Quoted 2 lines
arch/arm/kvm/arm.c +1
-0kvm_vgic_early_init (kvm); +
Spurious whitespace.
Quoted 37 lines
arch/arm/kvm/arm.c +0
-0kvm_timer_init (kvm);/* Mark the initial VMID generation invalid */ arch/arm64/include/asm/kvm_host.h +2
-0@@ -68,6 +68,8 @@ struct kvm_arch {/* Interrupt controller */ struct vgic_dist vgic; +/* List of virtual ITS MSI controllers */ +struct list_head vits_list;/* Timer */ struct arch_timer_kvm timer;arch/arm64/include/uapi/asm/kvm.h +2
-0@@ -87,9 +87,11 @@ struct kvm_regs {/* Supported VGICv3 address types */ #define KVM_VGIC_V3_ADDR_TYPE_DIST 2#define KVM_VGIC_V3_ADDR_TYPE_REDIST 3 +#define KVM_VGIC_ITS_ADDR_TYPE 4#define KVM_VGIC_V3_DIST_SIZE SZ_64K#define KVM_VGIC_V3_REDIST_SIZE (2 * SZ_64K) +#define KVM_VGIC_V3_ITS_SIZE SZ_64K#define KVM_ARM_VCPU_POWER_OFF 0/* CPU is started in OFF state */ #define KVM_ARM_VCPU_EL1_32BIT 1/* CPU running a 32bit VM */ include/kvm/vgic/vgic.h +0
-0
include/kvm/vgic/vgic.h struct vgic_io_device { · line 120
Quoted 4 lines
include/kvm/vgic/vgic.h +1
-0@@ -117,6 +117,8 @@ struct vgic_io_device { };struct vgic_its { +struct list_head its_list;
Consider renaming this to "entry", or something else that doesn't feel like it is a list on its own.
Quoted 86 lines
include/kvm/vgic/vgic.h +1
-0+/* The base address of the ITS control register frame */ gpa_t vgic_its_base;include/uapi/linux/kvm.h +2
-0@@ -1077,6 +1077,8 @@ enum kvm_device_type {#define KVM_DEV_TYPE_FLIC KVM_DEV_TYPE_FLIC KVM_DEV_TYPE_ARM_VGIC_V3,#define KVM_DEV_TYPE_ARM_VGIC_V3 KVM_DEV_TYPE_ARM_VGIC_V3 + KVM_DEV_TYPE_ARM_VGIC_ITS, +#define KVM_DEV_TYPE_ARM_VGIC_ITS KVM_DEV_TYPE_ARM_VGIC_ITS KVM_DEV_TYPE_MAX, };virt/kvm/arm/vgic/vgic-init.c +13
-0@@ -239,6 +239,7 @@ static void kvm_vgic_vcpu_init(struct kvm_vcpu *vcpu)int vgic_init (struct kvm *kvm) {struct vgic_dist *dist = &kvm->arch.vgic; +struct vgic_its *its;struct kvm_vcpu *vcpu;int ret =0 , i;@@ -253,6 +254,12 @@ int vgic_init(struct kvm *kvm)if (ret) goto out; +list_for_each_entry (its, &kvm->arch.vits_list, its_list) { + ret =vits_init (kvm, its); +if (ret) + goto out; + } +kvm_for_each_vcpu (i, vcpu, kvm)kvm_vgic_vcpu_init (vcpu);@@ -286,10 +293,16 @@ void kvm_vgic_vcpu_destroy(struct kvm_vcpu *vcpu)void kvm_vgic_destroy (struct kvm *kvm) {struct kvm_vcpu *vcpu; +struct vgic_its *its, *next;int i;kvm_vgic_dist_destroy (kvm); +list_for_each_entry_safe (its, next, &kvm->arch.vits_list, its_list) { +vits_destroy (kvm, its); +kfree (its); + } +kvm_for_each_vcpu (i, vcpu, kvm)kvm_vgic_vcpu_destroy (vcpu); }virt/kvm/arm/vgic/vgic-its.c +12
-0@@ -21,6 +21,7 @@#include <linux/kvm.h> #include <linux/kvm_host.h> #include <linux/interrupt.h> +#include <linux/uaccess.h> #include <linux/irqchip/arm-gic-v3.h> @@ -82,3 +83,123 @@ void vits_destroy(struct kvm *kvm, struct vgic_its *its) its->enabled = false; } + +static int vgic_its_create (struct kvm_device *dev,u32 type) +{ +struct vgic_its *its; + +if (type !=KVM_DEV_TYPE_ARM_VGIC_ITS ) +return -ENODEV ; + + its =kzalloc (sizeof (struct vgic_its ),GFP_KERNEL ); +if (!its) +return -ENOMEM ;
virt/kvm/arm/vgic/vgic-its.c void vits_destroy(struct kvm *kvm, struct vgic_its *its) · line 106
Quoted 10 lines
virt/kvm/arm/vgic/vgic-its.c +10
-0+ +spin_lock_init (&its->lock); + + its->vgic_its_base =VGIC_ADDR_UNDEF ; + + its->enabled = false; + + dev->private = its; + +list_add_tail (&its->its_list, &dev->kvm->arch.vits_list);
Where is the lock that protects the list when you perform the insertion?
Quoted 76 lines
virt/kvm/arm/vgic/vgic-its.c +76
-0+ +return 0 ; +} + +static void vgic_its_destroy (struct kvm_device *dev) +{ +struct vgic_its *its = dev->private; + +list_del (&its->its_list); + +vits_destroy (dev->kvm, its); + +kfree (its); +} + +static int vgic_its_has_attr (struct kvm_device *dev, +struct kvm_device_attr *attr) +{ +switch (attr->group) { +case KVM_DEV_ARM_VGIC_GRP_ADDR : +switch (attr->attr) { +case KVM_VGIC_ITS_ADDR_TYPE : +return 0 ; + } +break ; + } +return -ENXIO ; +} + +static int vgic_its_set_attr (struct kvm_device *dev, +struct kvm_device_attr *attr) +{ +int ret; + +switch (attr->group) { +case KVM_DEV_ARM_VGIC_GRP_ADDR : { +struct vgic_its *its = dev->private; +u64 __user *uaddr = (u64 __user *)(long )attr->addr; +unsigned long type = (unsigned long )attr->attr; +u64 addr; + +if (type !=KVM_VGIC_ITS_ADDR_TYPE ) +return -ENODEV ; + +if (copy_from_user (&addr, uaddr,sizeof (addr))) +return -EFAULT ; + + ret =vgic_check_ioaddr (dev->kvm, &its->vgic_its_base, + addr,SZ_64K ); +if (ret) +return ret; + + its->vgic_its_base = addr; + +return 0 ; + } + } + +return -ENXIO ; +} + +static int vgic_its_get_attr (struct kvm_device *dev, +struct kvm_device_attr *attr) +{ +switch (attr->group) { +case KVM_DEV_ARM_VGIC_GRP_ADDR : { +struct vgic_its *its = dev->private; +u64 addr = its->vgic_its_base; +u64 __user *uaddr = (u64 __user *)(long )attr->addr; +unsigned long type = (unsigned long )attr->attr; + +if (type !=KVM_VGIC_ITS_ADDR_TYPE ) +return -ENODEV ; + +if (copy_to_user (uaddr, &addr,sizeof (addr))) +return -EFAULT ;
virt/kvm/arm/vgic/vgic-its.c void vits_destroy(struct kvm *kvm, struct vgic_its *its) · line 192
Quoted 10 lines
virt/kvm/arm/vgic/vgic-its.c +10
-0+break ; +default : +return -ENXIO ; + } + } + +return 0 ; +} + +struct kvm_device_ops kvm_arm_vgic_its_ops = {
Why isn't this static?
Quoted 29 lines
virt/kvm/arm/vgic/vgic-its.c +13
-0+ .name = "kvm-arm-vgic-its", + .create = vgic_its_create, + .destroy = vgic_its_destroy, + .set_attr = vgic_its_set_attr, + .get_attr = vgic_its_get_attr, + .has_attr = vgic_its_has_attr, +}; + +int kvm_vgic_register_its_device (void ) +{ +return kvm_register_device_ops (&kvm_arm_vgic_its_ops, +KVM_DEV_TYPE_ARM_VGIC_ITS ); +}virt/kvm/arm/vgic/vgic-kvm-device.c +2
-2@@ -21,8 +21,8 @@/* common helpers */ -static int vgic_check_ioaddr (struct kvm *kvm,phys_addr_t *ioaddr, -phys_addr_t addr,phys_addr_t alignment) +int vgic_check_ioaddr (struct kvm *kvm,phys_addr_t *ioaddr, +phys_addr_t addr,phys_addr_t alignment) {if (addr & ~KVM_PHYS_MASK )return -E2BIG ;
virt/kvm/arm/vgic/vgic-kvm-device.c int kvm_register_vgic_device(unsigned long type) · line 228
Quoted 6 lines
virt/kvm/arm/vgic/vgic-kvm-device.c +3
-0@@ -223,6 +223,9 @@ int kvm_register_vgic_device(unsigned long type)case KVM_DEV_TYPE_ARM_VGIC_V3 : ret =kvm_register_device_ops (&kvm_arm_vgic_v3_ops,KVM_DEV_TYPE_ARM_VGIC_V3 ); +if (ret) +break ; + ret =kvm_vgic_register_its_device ();
You're mixing two init style here. One that uses kvm_register_device_ops directly, and one that uses a helper. Please choose one way or the other, but keep the init sequence uniform.
Quoted 41 lines
virt/kvm/arm/vgic/vgic-kvm-device.c +0
-0break ;#endif }virt/kvm/arm/vgic/vgic-mmio-v3.c +1
-1@@ -46,7 +46,7 @@ bool vgic_has_its(struct kvm *kvm)if (dist->vgic_model !=KVM_DEV_TYPE_ARM_VGIC_V3 )return false; -return false; +return !list_empty (&kvm->arch.vits_list); } static unsigned long vgic_mmio_read_v3_misc(struct kvm_vcpu *vcpu,virt/kvm/arm/vgic/vgic.h +4
-0@@ -41,6 +41,9 @@ struct vgic_irq *vgic_get_irq(struct kvm *kvm, struct kvm_vcpu *vcpu,bool vgic_queue_irq_unlock (struct kvm *kvm,struct vgic_irq *irq);void vgic_kick_vcpus (struct kvm *kvm); +int vgic_check_ioaddr (struct kvm *kvm,phys_addr_t *ioaddr, +phys_addr_t addr,phys_addr_t alignment); +void vgic_v2_process_maintenance (struct kvm_vcpu *vcpu);void vgic_v2_fold_lr_state (struct kvm_vcpu *vcpu);void vgic_v2_populate_lr (struct kvm_vcpu *vcpu,struct vgic_irq *irq,int lr);@@ -74,6 +77,7 @@ int vgic_register_redist_iodevs(struct kvm *kvm, gpa_t dist_base_address);bool vgic_has_its (struct kvm *kvm);int vits_init (struct kvm *kvm,struct vgic_its *its);void vits_destroy (struct kvm *kvm,struct vgic_its *its); +int kvm_vgic_register_its_device (void );#else static inline void vgic_v3_process_maintenance (struct kvm_vcpu *vcpu) {@@ -139,6 +143,11 @@ static int vits_init(struct kvm *kvm, struct vgic_its *its)static inline void vits_destroy (struct kvm *kvm,struct vgic_its *its)
virt/kvm/arm/vgic/vgic.h static int vits_init(struct kvm *kvm, struct vgic_its *its) · line 153
Quoted 10 lines
virt/kvm/arm/vgic/vgic.h +5
-0{ } + +static int kvm_vgic_register_its_device (void ) +{ +return -ENODEV ; +}#endif int kvm_register_vgic_device (unsigned long type);
Thanks,
M.
Signature · 1 line
Jazz is not dead. It just smells funny...
Re: [PATCH v5 08/13] KVM: arm64: implement basic ITS register handlers
In reply to Andre PrzywaraQuoted 7 lines · Andre Przywara
Add emulation for some basic MMIO registers used in the ITS emulation. This includes:
- GITS_{CTLR,TYPER,IIDR}
- ID registers
- GITS_{CBASER,CREADR,CWRITER}
(which implement the ITS command buffer handling)
Quoted 7 lines
Most of the handlers are pretty straight forward, but CWRITER goes some extra miles to allow fine grained locking. The idea here is to let only the first instance iterate through the command ring buffer, CWRITER accesses on other VCPUs meanwhile will be picked up by that first instance and handled as well. The ITS lock is thus only held for very small periods of time and is dropped before the actual command handler is called.
On top of the comment I've already given on this patch:
You've completely ignored the GITS_BASERn registers. While they are not strictly necessary for your emulation, we need them for save/restore. I expect the next version of this patch to at least expose:
- Device table
- Collection table
Also, consider advertising the Indirect bit in the GITS_BASERn describing the Device table, the guest will thank you for it (if the support it).
Thanks,
M.
Signature · 1 line
Jazz is not dead. It just smells funny...
Re: [PATCH v5 09/13] KVM: arm64: connect LPIs to the VGIC emulation
In reply to Andre PrzywaraQuoted 24 lines · Andre Przywara
LPIs are dynamically created (mapped) at guest runtime and their actual numbers can be quite high, but is mostly assigned using a very sparse allocation scheme. So arrays are not an ideal data structure to hold the information. We use our equivalent of the "Interrupt Translation Table Entry" (ITTE) to hold the
vgic_irqstruct for a virtual LPI embedded in in the ITTE.
Connect the VGIC core code via an accessor function to help it get the structvgic_irqfor a certain LPI.Signed-off-by Andre Przywara <[email protected]>--- virt/kvm/arm/vgic/vgic-its.c | 34 ++++++++++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic.c | 2 +- virt/kvm/arm/vgic/vgic.h | 6 ++++++ 3 files changed, 41 insertions(+), 1 deletion(-)virt/kvm/arm/vgic/vgic-its.c +0
-0@@ -68,11 +68,29 @@ struct its_collection {struct its_itte {struct list_head itte_list;
virt/kvm/arm/vgic/vgic-its.c struct its_collection { · line 80
Quoted 10 lines
virt/kvm/arm/vgic/vgic-its.c +5
-0+struct vgic_irq irq;struct its_collection *collection;u32 lpi;u32 event_id; }; +/* To be used as an iterator this macro misses the enclosing parentheses */ +#define for_each_lpi (dev, itte, its) \ + list_for_each_entry(dev, &(its)->device_list, dev_list) \ + list_for_each_entry(itte, &(dev)->itt, itte_list)
Well, this is not really "for each LPI". This is "for each LPI that can be generated by this ITS". Are you sure that you can always do this on a per-ITS basis? In other words, while this work for a direct translation, it doesn't work for a reverse one. Do we have any such case?
Quoted 21 lines
virt/kvm/arm/vgic/vgic-its.c +14
-0+ +static struct its_itte *find_itte_by_lpi (struct vgic_its *its,int lpi) +{ +struct its_device *device; +struct its_itte *itte; + +for_each_lpi (device, itte, its) { +if (itte->lpi == lpi) +return itte; + } +return NULL ; +} +#define BASER_BASE_ADDRESS (x) ((x) & 0xfffffffff000ULL)#define ITS_FRAME (addr) ((addr) & ~(SZ_64K - 1))@@ -158,6 +176,22 @@ static unsigned long vgic_mmio_read_its_idregs(struct kvm_vcpu *vcpu,return 0 ; } +struct vgic_irq *vgic_its_get_lpi (struct kvm *kvm,u32 intid)
virt/kvm/arm/vgic/vgic-its.c static unsigned long vgic_mmio_read_its_idregs(struct kvm_vcpu *vcpu, · line 189
Quoted 10 lines
virt/kvm/arm/vgic/vgic-its.c +10
-0+{ +struct vgic_its *its; +struct its_itte *itte; + +list_for_each_entry (its, &kvm->arch.vits_list, its_list) { + itte =find_itte_by_lpi (its, intid); +if (!itte) +continue ; + +return &itte->irq;
Or rather
if (itte) return &itte->irq;
This function implements the case I was worried about above. It would be worth mentioning that this *only* works because of 6.1.1 in the architecture spec (an LPI can only be generated by a single EID/DID pair).
What doesn't really work here is that you are allowed to program this EID/DID->LPI translation on several ITSs (think of a device moving its doorbell from one ITS to another), which means that you cannot store the vgic_irq in the ITE. Instead, this must be a pointer to IRQ, and the interrupt as part of a separate list.
Quoted 36 lines
virt/kvm/arm/vgic/vgic-its.c +5
-0+ } + +return NULL ; +} +static void its_free_itte (struct its_itte *itte) {list_del (&itte->itte_list);virt/kvm/arm/vgic/vgic.c +1
-1@@ -58,7 +58,7 @@ struct vgic_irq *vgic_get_irq(struct kvm *kvm, struct kvm_vcpu *vcpu,/* LPIs are not yet covered */ if (intid >=VGIC_MIN_LPI ) -return NULL ; +return vgic_its_get_lpi (kvm, intid);WARN (1 ,"Looking up struct vgic_irq for reserved INTID" );return NULL ;virt/kvm/arm/vgic/vgic.h +1
-0@@ -78,6 +78,7 @@ bool vgic_has_its(struct kvm *kvm);int vits_init (struct kvm *kvm,struct vgic_its *its);void vits_destroy (struct kvm *kvm,struct vgic_its *its);int kvm_vgic_register_its_device (void ); +struct vgic_irq *vgic_its_get_lpi (struct kvm *kvm,u32 intid);#else static inline void vgic_v3_process_maintenance (struct kvm_vcpu *vcpu) {@@ -148,6 +149,11 @@ static int kvm_vgic_register_its_device(void) {
virt/kvm/arm/vgic/vgic.h static int kvm_vgic_register_its_device(void) · line 159
Quoted 10 lines
virt/kvm/arm/vgic/vgic.h +5
-0return -ENODEV; } + +static inline struct vgic_irq *vgic_its_get_lpi (struct kvm *kvm,u32 intid) +{ +return NULL ; +}#endif int kvm_register_vgic_device (unsigned long type);
Thanks,
M.
Signature · 1 line
Jazz is not dead. It just smells funny...
Re: [PATCH v5 05/13] KVM: arm64: handle ITS related GICv3 redistributor registers
In reply to Andre PrzywaraOn top of the issues I already outlined:
Quoted 78 lines · Andre Przywara
In the GICv3 redistributor there are the PENDBASER and PROPBASER registers which we did not emulate so far, as they only make sense when having an ITS. In preparation for that emulate those MMIO accesses by storing the 64-bit data written into it into a variable which we later read in the ITS emulation.
Signed-off-by Andre Przywara <[email protected]>--- include/kvm/vgic/vgic.h | 13 +++++++++ virt/kvm/arm/vgic/vgic-mmio-v3.c | 58 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 69 insertions(+), 2 deletions(-)include/kvm/vgic/vgic.h +13
-0@@ -145,6 +145,14 @@ struct vgic_dist {struct vgic_irq *spis;struct vgic_io_device dist_iodev; + +/* +* Contains the address of the LPI configuration table. +* Since we report GICR_TYPER.CommonLPIAff as 0b00, we can share +* one address across all redistributors. +* GICv3 spec: 6.1.2 "LPI Configuration tables" +*/ +u64 propbaser; };struct vgic_v2_cpu_if {@@ -199,6 +207,11 @@ struct vgic_cpu {*/ struct vgic_io_device rd_iodev;struct vgic_io_device sgi_iodev; + +/* Points to the LPI pending tables for the redistributor */ +u64 pendbaser; + +bool lpis_enabled; };int kvm_vgic_addr (struct kvm *kvm,unsigned long type,u64 *addr,bool write);virt/kvm/arm/vgic/vgic-mmio-v3.c +20
-0@@ -29,6 +29,16 @@ static unsigned long extract_bytes(unsigned long data, unsigned int offset,return (data >> (offset *8 )) &GENMASK_ULL (num *8 -1 ,0 ); } +/* allows updates of any half of a 64-bit register (or the whole thing) */ +static u64 update_64bit_reg (u64 reg,unsigned int offset,unsigned int len, +unsigned long val) +{ + offset &=4 ; + reg &=GENMASK_ULL (len *8 -1 ,0 ) << ((len - offset) *8 ); + +return reg | ((u64 )val << (offset *8 )); +} +static unsigned long vgic_mmio_read_v3_misc (struct kvm_vcpu *vcpu,gpa_t addr,unsigned int len) {@@ -147,6 +157,50 @@ static unsigned long vgic_mmio_read_v3_idregs(struct kvm_vcpu *vcpu,return 0 ; } +static unsigned long vgic_mmio_read_propbase (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len) +{ +struct vgic_dist *dist = &vcpu->kvm->arch.vgic; + +return extract_bytes (dist->propbaser, addr &7 , len); +} + +static void vgic_mmio_write_propbase(struct kvm_vcpu *vcpu, + gpa_t addr, unsigned int len,
virt/kvm/arm/vgic/vgic-mmio-v3.c static unsigned long vgic_mmio_read_v3_idregs(struct kvm_vcpu *vcpu, · line 179
Quoted 10 lines
virt/kvm/arm/vgic/vgic-mmio-v3.c +10
-0+ unsigned long val) +{ +struct vgic_dist *dist = &vcpu->kvm->arch.vgic; +struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; + +/* Storing a value with LPIs already enabled is undefined */ +if (vgic_cpu->lpis_enabled) +return ; + + dist->propbaser =update_64bit_reg (dist->propbaser, addr &4 , len, val);
How about sanitizing the register? RES0 bits and co? What do you do for cacheability, shareabitily?
Quoted 12 lines
virt/kvm/arm/vgic/vgic-mmio-v3.c +12
-0+} + +static unsigned long vgic_mmio_read_pendbase (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len) +{ +struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; + +return extract_bytes (vgic_cpu->pendbaser, addr &7 , len); +} + +static void vgic_mmio_write_pendbase(struct kvm_vcpu *vcpu, + gpa_t addr, unsigned int len,
virt/kvm/arm/vgic/vgic-mmio-v3.c static unsigned long vgic_mmio_read_v3_idregs(struct kvm_vcpu *vcpu, · line 201
Quoted 10 lines
virt/kvm/arm/vgic/vgic-mmio-v3.c +10
-0+ unsigned long val) +{ +struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; + +/* Storing a value with LPIs already enabled is undefined */ +if (vgic_cpu->lpis_enabled) +return ; + + vgic_cpu->pendbaser =update_64bit_reg (vgic_cpu->pendbaser, + addr &4 , len, val);
Same issue about sanitizing the register.
Quoted 9 lines
virt/kvm/arm/vgic/vgic-mmio-v3.c +2
-0+} +/* * The GICv3 per-IRQ registers are split to control PPIs and SGIs in the * redistributors, while SPIs are covered by registers in the distributor @@ -227,10 +281,10 @@ static const struct vgic_register_region vgic_v3_rdbase_registers[] = { vgic_mmio_read_v3r_typer, vgic_mmio_write_wi,8 , VGIC_ACCESS_64bit | VGIC_ACCESS_32bit),
virt/kvm/arm/vgic/vgic-mmio-v3.c static const struct vgic_register_region vgic_v3_rdbase_registers[] = { · line 290
Quoted 10 lines
virt/kvm/arm/vgic/vgic-mmio-v3.c +2
-2REGISTER_DESC_WITH_LENGTH (GICR_PROPBASER , - vgic_mmio_read_raz, vgic_mmio_write_wi,8 , + vgic_mmio_read_propbase, vgic_mmio_write_propbase,8 , VGIC_ACCESS_64bit | VGIC_ACCESS_32bit),REGISTER_DESC_WITH_LENGTH (GICR_PENDBASER , - vgic_mmio_read_raz, vgic_mmio_write_wi,8 , + vgic_mmio_read_pendbase, vgic_mmio_write_pendbase,8 , VGIC_ACCESS_64bit | VGIC_ACCESS_32bit), REGISTER_DESC_WITH_LENGTH(GICR_IDREGS, vgic_mmio_read_v3_idregs, vgic_mmio_write_wi, 48,
Thanks,
M.
Signature · 1 line
Jazz is not dead. It just smells funny...
Re: [PATCH v5 10/13] KVM: arm64: sync LPI configuration and pending tables
In reply to Andre PrzywaraQuoted 6 lines · Andre Przywara
The LPI configuration and pending tables of the GICv3 LPIs are held in tables in (guest) memory. To achieve reasonable performance, we cache this data in our own data structures, so we need to sync those two views from time to time. This behaviour is well described in the GICv3 spec and is also exercised by hardware, so the sync points are well known.
Care to describe them?
Quoted 39 lines
Provide functions that read the guest memory and store the information from the configuration and pending tables in the kernel.
Signed-off-by Andre Przywara <[email protected]>--- include/kvm/vgic/vgic.h | 2 + virt/kvm/arm/vgic/vgic-its.c | 145 +++++++++++++++++++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic.h | 6 ++ 3 files changed, 153 insertions(+)include/kvm/vgic/vgic.h +2
-0@@ -131,6 +131,8 @@ struct vgic_its {u32 cwriter;struct list_head device_list;struct list_head collection_list; +/* memory used for buffering guest's memory */ +void *buffer_page; };struct vgic_dist {virt/kvm/arm/vgic/vgic-its.c +7
-0@@ -93,6 +93,128 @@ static struct its_itte *find_itte_by_lpi(struct vgic_its *its, int lpi)#define BASER_BASE_ADDRESS (x) ((x) & 0xfffffffff000ULL) +#define LPI_PROP_ENABLE_BIT (p) ((p) & LPI_PROP_ENABLED) +#define LPI_PROP_PRIORITY (p) ((p) & 0xfc) + +/* stores the priority and enable bit for a given LPI */ +static void update_lpi_config (struct kvm *kvm,struct its_itte *itte,u8 prop) +{ +spin_lock (&itte->irq.irq_lock);
virt/kvm/arm/vgic/vgic-its.c static struct its_itte *find_itte_by_lpi(struct vgic_its *its, int lpi) · line 112
Quoted 10 lines
virt/kvm/arm/vgic/vgic-its.c +10
-0+ itte->irq.priority =LPI_PROP_PRIORITY (prop); + itte->irq.enabled =LPI_PROP_ENABLE_BIT (prop); + +vgic_queue_irq_unlock (kvm, &itte->irq); +} + +#define GIC_LPI_OFFSET 8192 + +/* We scan the table in chunks the size of the smallest page size */ +#define CHUNK_SIZE 4096U
SZ_4K. And why 4K?
Quoted 5 lines
virt/kvm/arm/vgic/vgic-its.c +5
-0+ +static u32 max_lpis_propbaser (u64 propbaser) +{ +int nr_idbits = (propbaser &0x1f ) +1 ; +
virt/kvm/arm/vgic/vgic-its.c static struct its_itte *find_itte_by_lpi(struct vgic_its *its, int lpi) · line 127
Quoted 10 lines
virt/kvm/arm/vgic/vgic-its.c +10
-0+return 1U <<min (nr_idbits,INTERRUPT_ID_BITS_ITS ); +} + +/* +* Scan the whole LPI configuration table and put the LPI configuration +* data in our own data structures. This relies on the LPI being +* mapped before. +*/ +static bool its_update_lpis_configuration (struct kvm *kvm,struct vgic_its *its, +u64 prop_base_reg)
Why do you have to pass prop_base_reg here? You already have struct kvm that provides it. Also, the fact that you pass an ITS here shows how wrong the current design is (LPIs are at the redistributor level, and completely independent of the ITS implementation).
virt/kvm/arm/vgic/vgic-its.c static struct its_itte *find_itte_by_lpi(struct vgic_its *its, int lpi) · line 137
Quoted 10 lines
virt/kvm/arm/vgic/vgic-its.c +10
-0+{ +u8 *prop = its->buffer_page; +u32 tsize; +gpa_t propbase; +int lpi =GIC_LPI_OFFSET ; +struct its_itte *itte; +struct its_device *device; +int ret; + + propbase =BASER_BASE_ADDRESS (prop_base_reg);
So you're extracting bits [47:12] and use that as an address. No matter how you look at it, this is wrong. You either use [39:12] (because that's what KVM implements so far as the IPA range), or you use the architecturally defined range [51:12]. I strongly suggest you use the latter (and use a specific macro for that if you really have to use one).
Quoted 6 lines
virt/kvm/arm/vgic/vgic-its.c +6
-0+ tsize =max_lpis_propbaser (prop_base_reg); + +while (tsize >0 ) { +int chunksize =min (tsize,CHUNK_SIZE ); + + ret =kvm_read_guest (kvm, propbase, prop, chunksize);
virt/kvm/arm/vgic/vgic-its.c static struct its_itte *find_itte_by_lpi(struct vgic_its *its, int lpi) · line 153
Quoted 10 lines
virt/kvm/arm/vgic/vgic-its.c +10
-0+if (ret) +return false; + +spin_lock (&its->lock); +/* +* Updating the status for all allocated LPIs. We catch +* those LPIs that get disabled. We really don't care +* about unmapped LPIs, as they need to be updated +* later manually anyway once they get mapped. +*/
So why do you have to read the whole of the property table? Just iterate over the existing mappings (the LPIs that are allocated in the system) and read the single byte that actually matters (instead of reading several kb of useless data).
Quoted 12 lines
virt/kvm/arm/vgic/vgic-its.c +12
-0+for_each_lpi (device, itte, its) { +if (itte->lpi < lpi || itte->lpi >= lpi + chunksize) +continue ; + +update_lpi_config (kvm, itte, prop[itte->lpi - lpi]); + } +spin_unlock (&its->lock); + tsize -= chunksize; + lpi += chunksize; + propbase += chunksize; + } +
virt/kvm/arm/vgic/vgic-its.c static struct its_itte *find_itte_by_lpi(struct vgic_its *its, int lpi) · line 175
Quoted 10 lines
virt/kvm/arm/vgic/vgic-its.c +10
-0+return true; +} + +/* +* Scan the whole LPI pending table and sync the pending bit in there +* with our own data structures. This relies on the LPI being +* mapped before. +*/ +static bool its_sync_lpi_pending_table (struct kvm_vcpu *vcpu, +struct vgic_its *its,u64 base_addr_reg)
Same thing about the pending table address.
virt/kvm/arm/vgic/vgic-its.c static struct its_itte *find_itte_by_lpi(struct vgic_its *its, int lpi) · line 186
Quoted 11 lines
virt/kvm/arm/vgic/vgic-its.c +11
-0+{ +unsigned long *pendmask = its->buffer_page; +u32 nr_lpis =1U <<INTERRUPT_ID_BITS_ITS ; +gpa_t pendbase; +int lpi =0 ; +struct its_itte *itte; +struct its_device *device; +int ret; +int lpi_bit, nr_bits; + + pendbase =BASER_BASE_ADDRESS (base_addr_reg);
And now you're getting 4 bits of junk that the guest may have written. Great!
Quoted 11 lines
virt/kvm/arm/vgic/vgic-its.c +11
-0+ +while (nr_lpis >0 ) { + nr_bits =min (nr_lpis,CHUNK_SIZE *BITS_PER_BYTE ); + + ret =kvm_read_guest (vcpu->kvm, pendbase, pendmask, + nr_bits /BITS_PER_BYTE ); +if (ret) +return false; + +spin_lock (&its->lock); +for_each_lpi (device, itte, its) {
virt/kvm/arm/vgic/vgic-its.c static struct its_itte *find_itte_by_lpi(struct vgic_its *its, int lpi) · line 207
Quoted 10 lines
virt/kvm/arm/vgic/vgic-its.c +10
-0+ lpi_bit = itte->lpi - lpi; +if (lpi_bit <0 || lpi_bit >= nr_bits) +continue ; + +if (!test_bit (lpi_bit, pendmask)) +continue ; + +spin_lock (&itte->irq.irq_lock); + itte->irq.pending = true; +vgic_queue_irq_unlock (vcpu->kvm, &itte->irq);
Same comment about reading way too much data. You have the list of LPIs, use that to find out the right *bit* that actually matters. You'll need that functionality when being issued a MAPI/MAPVI anyway.
Quoted 45 lines
virt/kvm/arm/vgic/vgic-its.c +27
-0+ } +spin_unlock (&its->lock); + nr_lpis -= nr_bits; + lpi += nr_bits; + pendbase += nr_bits /BITS_PER_BYTE ; + } + +return true; +} +#define ITS_FRAME (addr) ((addr) & ~(SZ_64K - 1)) static unsigned long vgic_mmio_read_its_ctlr(struct kvm_vcpu *vcpu,@@ -355,6 +477,21 @@ struct vgic_register_region its_registers[] = { VGIC_ACCESS_32bit), }; +/* This is called on setting the LPI enable bit in the redistributor. */ +void vgic_enable_lpis (struct kvm_vcpu *vcpu) +{ +u64 prop_base_reg, pend_base_reg; +struct vgic_its *its; + + pend_base_reg = vcpu->arch.vgic_cpu.pendbaser; + prop_base_reg = vcpu->kvm->arch.vgic.propbaser; + +list_for_each_entry (its, &vcpu->kvm->arch.vits_list, its_list) { +its_update_lpis_configuration (vcpu->kvm, its, prop_base_reg); +its_sync_lpi_pending_table (vcpu, its, pend_base_reg); + } +} +int vits_init (struct kvm *kvm,struct vgic_its *its) {struct vgic_io_device *iodev = &its->iodev;@@ -381,6 +518,8 @@ void vits_destroy(struct kvm *kvm, struct vgic_its *its)struct list_head *dev_cur, *dev_temp;struct list_head *cur, *temp; +kfree (its->buffer_page); +/* * We may end up here without the lists ever having been initialized. * Check this and bail out early to avoid dereferencing a NULL pointer.
virt/kvm/arm/vgic/vgic-its.c static int vgic_its_create(struct kvm_device *dev, u32 type) · line 561
Quoted 4 lines
virt/kvm/arm/vgic/vgic-its.c +1
-0@@ -419,6 +558,12 @@ static int vgic_its_create(struct kvm_device *dev, u32 type)if (!its)return -ENOMEM ; + its->buffer_page =kmalloc (CHUNK_SIZE ,GFP_KERNEL );
kzalloc.
Quoted 13 lines
virt/kvm/arm/vgic/vgic-its.c +5
-0+if (!its->buffer_page) { +kfree (its); +return -ENOMEM ; + } +spin_lock_init (&its->lock); its->vgic_its_base =VGIC_ADDR_UNDEF ;virt/kvm/arm/vgic/vgic.h +0
-0
virt/kvm/arm/vgic/vgic.h line 28
Quoted 4 lines
virt/kvm/arm/vgic/vgic.h +1
-0@@ -25,6 +25,7 @@#define IS_VGIC_ADDR_UNDEF (_x) ((_x) == VGIC_ADDR_UNDEF)#define INTERRUPT_ID_BITS_SPIS 10 +#define INTERRUPT_ID_BITS_ITS 16
What provision do we have to make this configurable? I'd rather tackle this now than having to rev it later.
Quoted 13 lines
virt/kvm/arm/vgic/vgic.h +1
-0#define VGIC_PRI_BITS 5#define vgic_irq_is_sgi (intid) ((intid) < VGIC_NR_SGIS)@@ -79,6 +80,7 @@ int vits_init(struct kvm *kvm, struct vgic_its *its);void vits_destroy (struct kvm *kvm,struct vgic_its *its);int kvm_vgic_register_its_device (void );struct vgic_irq *vgic_its_get_lpi (struct kvm *kvm,u32 intid); +void vgic_enable_lpis (struct kvm_vcpu *vcpu);#else static inline void vgic_v3_process_maintenance (struct kvm_vcpu *vcpu) {
virt/kvm/arm/vgic/vgic.h static inline struct vgic_irq *vgic_its_get_lpi(struct kvm *kvm, u32 intid) · line 165
Quoted 10 lines
virt/kvm/arm/vgic/vgic.h +4
-0@@ -154,6 +156,10 @@ static inline struct vgic_irq *vgic_its_get_lpi(struct kvm *kvm, u32 intid) {return NULL ; } + +static inline void vgic_enable_lpis (struct kvm_vcpu *vcpu) +{ +}#endif int kvm_register_vgic_device (unsigned long type);
Thanks,
M.
Signature · 1 line
Jazz is not dead. It just smells funny...
Re: [PATCH v5 11/13] KVM: arm64: implement ITS command queue command handlers
In reply to Andre PrzywaraQuoted 10 lines · Andre Przywara
The connection between a device, an event ID, the LPI number and the allocated CPU is stored in in-memory tables in a GICv3, but their format is not specified by the spec. Instead software uses a command queue in a ring buffer to let the ITS implementation use their own format.
Implement handlers for the various ITS commands and let them store the requested relation into our own data structures.
To avoid kmallocs inside the ITS spinlock, we preallocate possibly needed memory outside of the lock and free that if it turns out to be not needed (mostly error handling).
What is the reason for using a spinlock the first place? Why can't a mutex be good enough? I've asked this before, and never had an answer.
As far as I can tell, we never take that lock in an interrupt context (or any other context that would prevent us from sleeping), so a spin_lock seems like the wrong solution.
Care to comment this time around?
Quoted 115 lines
Error handling is very basic at this point, as we don't have a good way of communicating errors to the guest (usually a SError). The INT command handler is missing at this point, as we gain the capability of actually injecting MSIs into the guest only later on.
Signed-off-by Andre Przywara <[email protected]>--- include/linux/irqchip/arm-gic-v3.h | 19 +- virt/kvm/arm/vgic/vgic-its.c | 513 ++++++++++++++++++++++++++++++++++++- 2 files changed, 530 insertions(+), 2 deletions(-)include/linux/irqchip/arm-gic-v3.h +18
-1@@ -254,7 +254,10 @@*/ #define GITS_CMD_MAPD 0x08#define GITS_CMD_MAPC 0x09 -#define GITS_CMD_MAPVI 0x0a +#define GITS_CMD_MAPTI 0x0a +/* older GIC documentation used MAPVI for this command */ +#define GITS_CMD_MAPVI GITS_CMD_MAPTI +#define GITS_CMD_MAPI 0x0b#define GITS_CMD_MOVI 0x01#define GITS_CMD_DISCARD 0x0f#define GITS_CMD_INV 0x0c@@ -265,6 +268,20 @@#define GITS_CMD_SYNC 0x05/* +* ITS error numbers +*/ +#define E_ITS_MOVI_UNMAPPED_INTERRUPT 0x010107 +#define E_ITS_MOVI_UNMAPPED_COLLECTION 0x010109 +#define E_ITS_CLEAR_UNMAPPED_INTERRUPT 0x010507 +#define E_ITS_MAPC_PROCNUM_OOR 0x010902 +#define E_ITS_MAPTI_UNMAPPED_DEVICE 0x010a04 +#define E_ITS_MAPTI_PHYSICALID_OOR 0x010a06 +#define E_ITS_INV_UNMAPPED_INTERRUPT 0x010c07 +#define E_ITS_INVALL_UNMAPPED_COLLECTION 0x010d09 +#define E_ITS_MOVALL_PROCNUM_OOR 0x010e01 +#define E_ITS_DISCARD_UNMAPPED_INTERRUPT 0x010f07 + +/* * CPU interface registers */ #define ICC_CTLR_EL1_EOImode_drop_dir (0U << 1)virt/kvm/arm/vgic/vgic-its.c +41
-0@@ -22,6 +22,7 @@#include <linux/kvm_host.h> #include <linux/interrupt.h> #include <linux/list.h> +#include <linux/slab.h> #include <linux/uaccess.h> #include <linux/irqchip/arm-gic-v3.h> @@ -74,6 +75,34 @@ struct its_itte {u32 event_id; }; +static struct its_device *find_its_device (struct vgic_its *its,u32 device_id) +{ +struct its_device *device; + +list_for_each_entry (device, &its->device_list, dev_list) +if (device_id == device->device_id) +return device; + +return NULL ; +} + +static struct its_itte *find_itte (struct vgic_its *its,u32 device_id, +u32 event_id) +{ +struct its_device *device; +struct its_itte *itte; + + device =find_its_device (its, device_id); +if (device ==NULL ) +return NULL ; + +list_for_each_entry (itte, &device->itt, itte_list) +if (itte->event_id == event_id) +return itte; + +return NULL ; +} +/* To be used as an iterator this macro misses the enclosing parentheses */ #define for_each_lpi (dev, itte, its) \ list_for_each_entry(dev, &(its)->device_list, dev_list) \@@ -93,6 +122,18 @@ static struct its_itte *find_itte_by_lpi(struct vgic_its *its, int lpi)#define BASER_BASE_ADDRESS (x) ((x) & 0xfffffffff000ULL) +static struct its_collection *find_collection (struct vgic_its *its,int coll_id) +{ +struct its_collection *collection; + +list_for_each_entry (collection, &its->collection_list, coll_list) { +if (coll_id == collection->collection_id) +return collection; + } + +return NULL ; +} +#define LPI_PROP_ENABLE_BIT (p) ((p) & LPI_PROP_ENABLED)#define LPI_PROP_PRIORITY (p) ((p) & 0xfc)
virt/kvm/arm/vgic/vgic-its.c static void update_lpi_config(struct kvm *kvm, struct its_itte *itte, u8 prop) · line 154
Quoted 8 lines
virt/kvm/arm/vgic/vgic-its.c +5
-0@@ -106,6 +147,30 @@ static void update_lpi_config(struct kvm *kvm, struct its_itte *itte, u8 prop)vgic_queue_irq_unlock (kvm, &itte->irq); } +/* +* Finds all LPIs which are mapped to this collection and updates the +* struct irq's target_vcpu field accordingly. +* Needs to be called whenever either the collection for a LPIs has +* changed or the collection itself got retargetted.
Why do you need to update all the interrupts that belong to a single collections if you change the affinity of a single interrupt? Or am I misunderstanding the comment?
virt/kvm/arm/vgic/vgic-its.c static void update_lpi_config(struct kvm *kvm, struct its_itte *itte, u8 prop) · line 165
Quoted 11 lines
virt/kvm/arm/vgic/vgic-its.c +11
-0+*/ +static void update_affinity (struct kvm *kvm,struct vgic_its *its, +struct its_collection *coll) +{ +struct its_device *device; +struct its_itte *itte; +struct kvm_vcpu *vcpu =kvm_get_vcpu (kvm, coll->target_addr); + +for_each_lpi (device, itte, its) { +if (!itte->collection || + coll->collection_id != itte->collection->collection_id)
If you can ensure that you cannot have two collections with the same ID (and you really should), why can't you just compare the pointers?
Quoted 76 lines
virt/kvm/arm/vgic/vgic-its.c +69
-0+continue ; + +spin_lock (&itte->irq.irq_lock); + itte->irq.target_vcpu = vcpu; +spin_unlock (&itte->irq.irq_lock); + } +} +#define GIC_LPI_OFFSET 8192/* We scan the table in chunks the size of the smallest page size */ @@ -320,6 +385,412 @@ static void its_free_itte(struct its_itte *itte)kfree (itte); } +static u64 its_cmd_mask_field (u64 *its_cmd,int word,int shift,int size) +{ +return (le64_to_cpu (its_cmd[word]) >> shift) & (BIT_ULL (size) -1 ); +} + +#define its_cmd_get_command (cmd) its_cmd_mask_field(cmd, 0, 0, 8) +#define its_cmd_get_deviceid (cmd) its_cmd_mask_field(cmd, 0, 32, 32) +#define its_cmd_get_id (cmd) its_cmd_mask_field(cmd, 1, 0, 32) +#define its_cmd_get_physical_id (cmd) its_cmd_mask_field(cmd, 1, 32, 32) +#define its_cmd_get_collection (cmd) its_cmd_mask_field(cmd, 2, 0, 16) +#define its_cmd_get_target_addr (cmd) its_cmd_mask_field(cmd, 2, 16, 32) +#define its_cmd_get_validbit (cmd) its_cmd_mask_field(cmd, 2, 63, 1) + +/* The DISCARD command frees an Interrupt Translation Table Entry (ITTE). */ +static int vits_cmd_handle_discard (struct kvm *kvm,struct vgic_its *its, +u64 *its_cmd) +{ +u32 device_id; +u32 event_id; +struct its_itte *itte; +int ret =E_ITS_DISCARD_UNMAPPED_INTERRUPT ; + + device_id =its_cmd_get_deviceid (its_cmd); + event_id =its_cmd_get_id (its_cmd); + +spin_lock (&its->lock); + itte =find_itte (its, device_id, event_id); +if (itte && itte->collection) { +/* +* Though the spec talks about removing the pending state, we +* don't bother here since we clear the ITTE anyway and the +* pending state is a property of the ITTE struct. +*/ +its_free_itte (itte); + ret =0 ; + } + +spin_unlock (&its->lock); +return ret; +} + +/* The MOVI command moves an ITTE to a different collection. */ +static int vits_cmd_handle_movi (struct kvm *kvm,struct vgic_its *its, +u64 *its_cmd) +{ +u32 device_id =its_cmd_get_deviceid (its_cmd); +u32 event_id =its_cmd_get_id (its_cmd); +u32 coll_id =its_cmd_get_collection (its_cmd); +struct its_itte *itte; +struct its_collection *collection; +int ret =0 ; + +spin_lock (&its->lock); + itte =find_itte (its, device_id, event_id); +if (!itte) { + ret =E_ITS_MOVI_UNMAPPED_INTERRUPT ; + goto out_unlock; + } +if (!its_is_collection_mapped (itte->collection)) { + ret =E_ITS_MOVI_UNMAPPED_COLLECTION ; + goto out_unlock;
virt/kvm/arm/vgic/vgic-its.c static void its_free_itte(struct its_itte *itte) · line 458
Quoted 10 lines
virt/kvm/arm/vgic/vgic-its.c +10
-0+ } + + collection =find_collection (its, coll_id); +if (!its_is_collection_mapped (collection)) { + ret =E_ITS_MOVI_UNMAPPED_COLLECTION ; + goto out_unlock; + } + + itte->collection = collection; +update_affinity (kvm, its, collection);
I really don't get it. Only this particular interrupt has moved. Why are you repainting all of the interrupts mapped to that collection?
Quoted 4 lines
virt/kvm/arm/vgic/vgic-its.c +4
-0+ +out_unlock: +spin_unlock (&its->lock); +return ret;
virt/kvm/arm/vgic/vgic-its.c static void its_free_itte(struct its_itte *itte) · line 472
Quoted 10 lines
virt/kvm/arm/vgic/vgic-its.c +10
-0+} + +static void vits_init_collection (struct vgic_its *its, +struct its_collection *collection, +u32 coll_id) +{ + collection->collection_id = coll_id; + collection->target_addr =COLLECTION_NOT_MAPPED ; + +list_add_tail (&collection->coll_list, &its->collection_list);
What are the locking requirements?
Quoted 7 lines
virt/kvm/arm/vgic/vgic-its.c +7
-0+} + +/* The MAPTI and MAPI commands map LPIs to ITTEs. */ +static int vits_cmd_handle_mapi (struct kvm *kvm,struct vgic_its *its, +u64 *its_cmd,u8 subcmd) +{ +u32 device_id =its_cmd_get_deviceid (its_cmd);
virt/kvm/arm/vgic/vgic-its.c static void its_free_itte(struct its_itte *itte) · line 489
Quoted 10 lines
virt/kvm/arm/vgic/vgic-its.c +10
-0+u32 event_id =its_cmd_get_id (its_cmd); +u32 coll_id =its_cmd_get_collection (its_cmd); +struct its_itte *itte, *new_itte; +struct its_device *device; +struct its_collection *collection, *new_coll; +int lpi_nr; +int ret =0 ; + +/* Preallocate possibly needed memory here outside of the lock */ + new_coll =kmalloc (sizeof (struct its_collection ),GFP_KERNEL );
kzalloc.
Quoted 44 lines
virt/kvm/arm/vgic/vgic-its.c +44
-0+ new_itte =kzalloc (sizeof (struct its_itte ),GFP_KERNEL ); + +spin_lock (&its->lock); + + device =find_its_device (its, device_id); +if (!device) { + ret =E_ITS_MAPTI_UNMAPPED_DEVICE ; + goto out_unlock; + } + + collection =find_collection (its, coll_id); +if (!collection && !new_coll) { + ret = -ENOMEM ; + goto out_unlock; + } + +if (subcmd ==GITS_CMD_MAPTI ) + lpi_nr =its_cmd_get_physical_id (its_cmd); +else + lpi_nr = event_id; +if (lpi_nr <GIC_LPI_OFFSET || + lpi_nr >=max_lpis_propbaser (kvm->arch.vgic.propbaser)) { + ret =E_ITS_MAPTI_PHYSICALID_OOR ; + goto out_unlock; + } + + itte =find_itte (its, device_id, event_id); +if (!itte) { +if (!new_itte) { + ret = -ENOMEM ; + goto out_unlock; + } + itte = new_itte; + + itte->event_id = event_id; +list_add_tail (&itte->itte_list, &device->itt); + }else { +kfree (new_itte); + } + +if (!collection) { + collection = new_coll; +vits_init_collection (its, collection, coll_id); + }else {
virt/kvm/arm/vgic/vgic-its.c static void its_free_itte(struct its_itte *itte) · line 543
Quoted 10 lines
virt/kvm/arm/vgic/vgic-its.c +10
-0+kfree (new_coll); + } + + itte->collection = collection; + itte->lpi = lpi_nr; + itte->irq.intid = lpi_nr; +INIT_LIST_HEAD (&itte->irq.ap_list); +spin_lock_init (&itte->irq.irq_lock); + itte->irq.vcpu =NULL ; +update_affinity (kvm, its, collection);
Same question about the update. Also, I think we may have to read the pending bit from memory (think save/restore).
Quoted 171 lines
virt/kvm/arm/vgic/vgic-its.c +171
-0+ +out_unlock: +spin_unlock (&its->lock); +if (ret) { +kfree (new_coll); +kfree (new_itte); + } +return ret; +} + +static void vits_unmap_device (struct kvm *kvm,struct its_device *device) +{ +struct its_itte *itte, *temp; + +/* +* The spec says that unmapping a device with still valid +* ITTEs associated is UNPREDICTABLE. We remove all ITTEs, +* since we cannot leave the memory unreferenced. +*/ +list_for_each_entry_safe (itte, temp, &device->itt, itte_list) +its_free_itte (itte); + +list_del (&device->dev_list); +kfree (device); +} + +/* MAPD maps or unmaps a device ID to Interrupt Translation Tables (ITTs). */ +static int vits_cmd_handle_mapd (struct kvm *kvm,struct vgic_its *its, +u64 *its_cmd) +{ +bool valid =its_cmd_get_validbit (its_cmd); +u32 device_id =its_cmd_get_deviceid (its_cmd); +struct its_device *device, *new_device =NULL ; + +/* We preallocate memory outside of the lock here */ +if (valid) { + new_device =kzalloc (sizeof (struct its_device ),GFP_KERNEL ); +if (!new_device) +return -ENOMEM ; + } + +spin_lock (&its->lock); + + device =find_its_device (its, device_id); +if (device) +vits_unmap_device (kvm, device); + +/* +* The spec does not say whether unmapping a not-mapped device +* is an error, so we are done in any case. +*/ +if (!valid) + goto out_unlock; + + device = new_device; + + device->device_id = device_id; +INIT_LIST_HEAD (&device->itt); + +list_add_tail (&device->dev_list, &its->device_list); + +out_unlock: +spin_unlock (&its->lock); +return 0 ; +} + +/* The MAPC command maps collection IDs to redistributors. */ +static int vits_cmd_handle_mapc (struct kvm *kvm,struct vgic_its *its, +u64 *its_cmd) +{ +u16 coll_id; +u32 target_addr; +struct its_collection *collection, *new_coll =NULL ; +bool valid; + + valid =its_cmd_get_validbit (its_cmd); + coll_id =its_cmd_get_collection (its_cmd); + target_addr =its_cmd_get_target_addr (its_cmd); + +if (target_addr >=atomic_read (&kvm->online_vcpus)) +return E_ITS_MAPC_PROCNUM_OOR ; + +/* We preallocate memory outside of the lock here */ +if (valid) { + new_coll =kmalloc (sizeof (struct its_collection ),GFP_KERNEL ); +if (!new_coll) +return -ENOMEM ; + } + +spin_lock (&its->lock); + collection =find_collection (its, coll_id); + +if (!valid) { +struct its_device *device; +struct its_itte *itte; +/* +* Clearing the mapping for that collection ID removes the +* entry from the list. If there wasn't any before, we can +* go home early. +*/ +if (!collection) + goto out_unlock; + +for_each_lpi (device, itte, its) +if (itte->collection && + itte->collection->collection_id == coll_id) + itte->collection =NULL ; + +list_del (&collection->coll_list); +kfree (collection); + }else { +if (!collection) + collection = new_coll; +else +kfree (new_coll); + +vits_init_collection (its, collection, coll_id); + collection->target_addr = target_addr; +update_affinity (kvm, its, collection); + } + +out_unlock: +spin_unlock (&its->lock); +return 0 ; +} + +/* The CLEAR command removes the pending state for a particular LPI. */ +static int vits_cmd_handle_clear (struct kvm *kvm,struct vgic_its *its, +u64 *its_cmd) +{ +u32 device_id; +u32 event_id; +struct its_itte *itte; +int ret =0 ; + + device_id =its_cmd_get_deviceid (its_cmd); + event_id =its_cmd_get_id (its_cmd); + +spin_lock (&its->lock); + + itte =find_itte (its, device_id, event_id); +if (!itte) { + ret =E_ITS_CLEAR_UNMAPPED_INTERRUPT ; + goto out_unlock; + } + + itte->irq.pending = false; + +out_unlock: +spin_unlock (&its->lock); +return ret; +} + +/* The INV command syncs the configuration bits from the memory tables. */ +static int vits_cmd_handle_inv (struct kvm *kvm,struct vgic_its *its, +u64 *its_cmd) +{ +struct vgic_dist *dist = &kvm->arch.vgic; +u32 device_id; +u32 event_id; +struct its_itte *itte, *new_itte; +gpa_t propbase; +int ret; +u8 prop; + + device_id =its_cmd_get_deviceid (its_cmd); + event_id =its_cmd_get_id (its_cmd); + +spin_lock (&its->lock); + itte =find_itte (its, device_id, event_id); +spin_unlock (&its->lock);
virt/kvm/arm/vgic/vgic-its.c static void its_free_itte(struct its_itte *itte) · line 724
Quoted 10 lines
virt/kvm/arm/vgic/vgic-its.c +10
-0+if (!itte) +return E_ITS_INV_UNMAPPED_INTERRUPT ; + +/* +* We cannot read from guest memory inside the spinlock, so we +* need to re-read our tables to learn whether the LPI number we are +* using is still valid. +*/ + do { + propbase = BASER_BASE_ADDRESS(dist->propbaser);
Same remarks about the usage of this macro.
Quoted 52 lines
virt/kvm/arm/vgic/vgic-its.c +52
-0+ ret =kvm_read_guest (kvm, propbase + itte->lpi -GIC_LPI_OFFSET , + &prop,1 ); +if (ret) +return ret; + +spin_lock (&its->lock); + new_itte =find_itte (its, device_id, event_id); +if (new_itte->lpi != itte->lpi) { + itte = new_itte; +spin_unlock (&its->lock); +continue ; + } +update_lpi_config (kvm, itte, prop); +spin_unlock (&its->lock); + }while (0 ); +return 0 ; +} + +/* The INVALL command requests flushing of all IRQ data in this collection. */ +static int vits_cmd_handle_invall (struct kvm *kvm,struct vgic_its *its, +u64 *its_cmd) +{ +u64 prop_base_reg, pend_base_reg; +u32 coll_id =its_cmd_get_collection (its_cmd); +struct its_collection *collection; +struct kvm_vcpu *vcpu; + + collection =find_collection (its, coll_id); +if (!its_is_collection_mapped (collection)) +return E_ITS_INVALL_UNMAPPED_COLLECTION ; + + vcpu =kvm_get_vcpu (kvm, collection->target_addr); + + pend_base_reg = vcpu->arch.vgic_cpu.pendbaser; + prop_base_reg = kvm->arch.vgic.propbaser; + +its_update_lpis_configuration (kvm, its, prop_base_reg); +its_sync_lpi_pending_table (vcpu, its, pend_base_reg); + +return 0 ; +} + +/* The MOVALL command moves all IRQs from one redistributor to another. */ +static int vits_cmd_handle_movall (struct kvm *kvm,struct vgic_its *its, +u64 *its_cmd) +{ +u32 target1_addr =its_cmd_get_target_addr (its_cmd); +u32 target2_addr =its_cmd_mask_field (its_cmd,3 ,16 ,32 ); +struct its_collection *collection; + +if (target1_addr >=atomic_read (&kvm->online_vcpus) || + target2_addr >=atomic_read (&kvm->online_vcpus))
virt/kvm/arm/vgic/vgic-its.c static void its_free_itte(struct its_itte *itte) · line 786
Quoted 10 lines
virt/kvm/arm/vgic/vgic-its.c +10
-0+return E_ITS_MOVALL_PROCNUM_OOR ; + +if (target1_addr == target2_addr) +return 0 ; + +spin_lock (&its->lock); +list_for_each_entry (collection, &its->collection_list, + coll_list) { +if (collection && collection->target_addr == target1_addr) + collection->target_addr = target2_addr;
FTFM: "Both the mapping of interrupts to collections and the mapping of collections to Redistributors are normally unaffected by this command."
Look at what the pseudocode does:
if rd1 != rd2 then MoveAllPendingState(rd1, rd2);
And that's it. MOVEALL only moves the pending state. So I have no idea what you're doing here, but it looks as far as possible from what the architecture requires.
Quoted 50 lines
virt/kvm/arm/vgic/vgic-its.c +41
-1+update_affinity (kvm, its, collection); + } + +spin_unlock (&its->lock); +return 0 ; +} +/* * This function expects the ITS lock to be dropped, so the actual command * handlers must take care of proper locking when needed. @@ -327,7 +798,47 @@ static void its_free_itte(struct its_itte *itte)static int vits_handle_command (struct kvm *kvm,struct vgic_its *its,u64 *its_cmd) { -return -ENODEV ; +u8 cmd =its_cmd_get_command (its_cmd); +int ret = -ENODEV ; + +switch (cmd) { +case GITS_CMD_MAPD : + ret =vits_cmd_handle_mapd (kvm, its, its_cmd); +break ; +case GITS_CMD_MAPC : + ret =vits_cmd_handle_mapc (kvm, its, its_cmd); +break ; +case GITS_CMD_MAPI : + ret =vits_cmd_handle_mapi (kvm, its, its_cmd, cmd); +break ; +case GITS_CMD_MAPTI : + ret =vits_cmd_handle_mapi (kvm, its, its_cmd, cmd); +break ; +case GITS_CMD_MOVI : + ret =vits_cmd_handle_movi (kvm, its, its_cmd); +break ; +case GITS_CMD_DISCARD : + ret =vits_cmd_handle_discard (kvm, its, its_cmd); +break ; +case GITS_CMD_CLEAR : + ret =vits_cmd_handle_clear (kvm, its, its_cmd); +break ; +case GITS_CMD_MOVALL : + ret =vits_cmd_handle_movall (kvm, its, its_cmd); +break ; +case GITS_CMD_INV : + ret =vits_cmd_handle_inv (kvm, its, its_cmd); +break ; +case GITS_CMD_INVALL : + ret =vits_cmd_handle_invall (kvm, its, its_cmd); +break ;
virt/kvm/arm/vgic/vgic-its.c static void its_free_itte(struct its_itte *itte) · line 844
Quoted 10 lines
virt/kvm/arm/vgic/vgic-its.c +7
-0+case GITS_CMD_SYNC : +/* we ignore this command: we are in sync all of the time */ + ret =0 ; +break ; + } + +return ret; } static unsigned long vgic_mmio_read_its_cbaser(struct kvm_vcpu *vcpu,
I think you need to go back to the drawing board on this. Your handling of collection is not aligned to the architecture, and the whole locking vs allocation is at least questionnable.
M.
Signature · 1 line
Jazz is not dead. It just smells funny...
Re: [PATCH v5 12/13] KVM: arm64: implement MSI injection in ITS emulation
In reply to Andre PrzywaraQuoted 25 lines · Andre Przywara
When userland wants to inject a MSI into the guest, we have to use our data structures to find the LPI number and the VCPU to receive the interrupt.
Use the wrapper functions to iterate the linked lists and find the proper Interrupt Translation Table Entry. Then set the pending bit in this ITTE to be later picked up by the LR handling code. Kick the VCPU which is meant to handle this interrupt.
We provide a VGIC emulation model specific routine for the actual MSI injection. The wrapper functions return an error for models not (yet) implementing MSIs (like the GICv2 emulation).
We also provide the handler for the ITS "INT" command, which allows a guest to trigger an MSI via the ITS command queue.Signed-off-by Andre Przywara <[email protected]>--- virt/kvm/arm/vgic/vgic-its.c | 85 ++++++++++++++++++++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic.h | 6 ++++ 2 files changed, 91 insertions(+)virt/kvm/arm/vgic/vgic-its.c +0
-0@@ -46,6 +46,16 @@ static struct vgic_its *find_its(struct kvm *kvm, gpa_t base_address)return NULL ;
virt/kvm/arm/vgic/vgic-its.c static struct vgic_its *find_its(struct kvm *kvm, gpa_t base_address) · line 56
Quoted 10 lines
virt/kvm/arm/vgic/vgic-its.c +8
-0} +#define ITS_DOORBELL_OFFSET (SZ_64K + 0x40) +static struct vgic_its *find_its_doorbell (struct kvm *kvm,gpa_t doorbell) +{ +/* +* The actual doorbell address is in the second page of the ITS +* frame, at offset 0x40. +*/ +return find_its (kvm, doorbell -ITS_DOORBELL_OFFSET );
Hold on. This is an MMIO write (coming from a device, but hey). Why can't you generate that write directly? And why can't we get the ITS address directly from the region that we hit? We cache the target vcpu for redistributors. We could perfectly cache the ITS there instead of setting it to NULL (creative use of a union will get you a long way).
Quoted 28 lines
virt/kvm/arm/vgic/vgic-its.c +21
-0+} +struct its_device {struct list_head dev_list;@@ -363,6 +373,61 @@ static unsigned long vgic_mmio_read_its_idregs(struct kvm_vcpu *vcpu,return 0 ; } +/* +* Translates an incoming MSI request into the redistributor (=VCPU) and +* the associated LPI number. Sets the LPI pending bit and also marks the +* VCPU as having a pending interrupt. +*/ +int vits_inject_msi (struct kvm *kvm,struct kvm_msi *msi) +{ +struct vgic_its *its; +struct its_itte *itte; +struct kvm_vcpu *vcpu; +bool inject = false; +u64 doorbell; +int ret =0 ; + +if (!vgic_has_its (kvm)) +return -ENODEV ; + +if (!(msi->flags &KVM_MSI_VALID_DEVID )) +return -EINVAL ;
virt/kvm/arm/vgic/vgic-its.c static unsigned long vgic_mmio_read_its_idregs(struct kvm_vcpu *vcpu, · line 404
Quoted 10 lines
virt/kvm/arm/vgic/vgic-its.c +10
-0+ + doorbell = (u64 )msi->address_hi <<32 | msi->address_lo; + its =find_its_doorbell (kvm, doorbell); +if (!its) +return -EINVAL ; + +spin_lock (&its->lock); + +if (!its->enabled) { + ret = -EAGAIN ;
Why -EAGAIN? Is retrying going to help? What would happen on the actual HW?
Quoted 6 lines
virt/kvm/arm/vgic/vgic-its.c +6
-0+ goto out_unlock; + } + + itte =find_itte (its, msi->devid, msi->data); +/* Triggering an unmapped IRQ gets silently dropped. */ +if (!itte || !its_is_collection_mapped (itte->collection))
virt/kvm/arm/vgic/vgic-its.c static unsigned long vgic_mmio_read_its_idregs(struct kvm_vcpu *vcpu, · line 420
Quoted 10 lines
virt/kvm/arm/vgic/vgic-its.c +10
-0+ goto out_unlock; + + vcpu =kvm_get_vcpu (kvm, itte->collection->target_addr); +if (!vcpu || !vcpu->arch.vgic_cpu.lpis_enabled) + goto out_unlock; + + inject = true; + +out_unlock: +spin_unlock (&its->lock);
So you release the ITS lock here....
virt/kvm/arm/vgic/vgic-its.c static unsigned long vgic_mmio_read_its_idregs(struct kvm_vcpu *vcpu, · line 423
Quoted 3 lines
virt/kvm/arm/vgic/vgic-its.c +3
-0+ +if (inject) { +spin_lock (&itte->irq.irq_lock);
and take the IRQ lock here.
In the meantime, the guest has injected a DISCARD, which has freed the itte structure. Bingo. I've been saying that the locking is silly, and I'll say it again.
Quoted 16 lines
virt/kvm/arm/vgic/vgic-its.c +9
-0+ itte->irq.pending = true; +vgic_queue_irq_unlock (kvm, &itte->irq); + } + +return ret; +} +struct vgic_irq *vgic_its_get_lpi (struct kvm *kvm,u32 intid) {struct vgic_its *its;@@ -791,6 +856,23 @@ static int vits_cmd_handle_movall(struct kvm *kvm, struct vgic_its *its,return 0 ; } +/* The INT command injects the LPI associated with that DevID/EvID pair. */ +static int vits_cmd_handle_int(struct kvm *kvm, struct vgic_its *its,
virt/kvm/arm/vgic/vgic-its.c static int vits_cmd_handle_movall(struct kvm *kvm, struct vgic_its *its, · line 870
Quoted 10 lines
virt/kvm/arm/vgic/vgic-its.c +10
-0+ u64 *its_cmd) +{ +u64 doorbell = its->vgic_its_base +ITS_DOORBELL_OFFSET ; +struct kvm_msi msi = { + .address_lo = doorbell &0xffffffff , + .address_hi = doorbell >>32 , + .data =its_cmd_get_id (its_cmd), + .devid =its_cmd_get_deviceid (its_cmd), + .flags =KVM_MSI_VALID_DEVID , + };
Really???
Quoted 33 lines
virt/kvm/arm/vgic/vgic-its.c +8
-0+ +vits_inject_msi (kvm, &msi); +return 0 ; +} +/* * This function expects the ITS lock to be dropped, so the actual command * handlers must take care of proper locking when needed. @@ -826,6 +908,9 @@ static int vits_handle_command(struct kvm *kvm, struct vgic_its *its,case GITS_CMD_MOVALL : ret =vits_cmd_handle_movall (kvm, its, its_cmd);break ; +case GITS_CMD_INT : + ret =vits_cmd_handle_int (kvm, its, its_cmd); +break ;case GITS_CMD_INV : ret =vits_cmd_handle_inv (kvm, its, its_cmd);break ;virt/kvm/arm/vgic/vgic.h +1
-0@@ -81,6 +81,7 @@ void vits_destroy(struct kvm *kvm, struct vgic_its *its);int kvm_vgic_register_its_device (void );struct vgic_irq *vgic_its_get_lpi (struct kvm *kvm,u32 intid);void vgic_enable_lpis (struct kvm_vcpu *vcpu); +int vits_inject_msi (struct kvm *kvm,struct kvm_msi *msi);#else static inline void vgic_v3_process_maintenance (struct kvm_vcpu *vcpu) {@@ -160,6 +161,11 @@ static inline struct vgic_irq *vgic_its_get_lpi(struct kvm *kvm, u32 intid)static inline void vgic_enable_lpis (struct kvm_vcpu *vcpu)
virt/kvm/arm/vgic/vgic.h static inline struct vgic_irq *vgic_its_get_lpi(struct kvm *kvm, u32 intid) · line 171
Quoted 10 lines
virt/kvm/arm/vgic/vgic.h +5
-0{ } + +static inline int vits_inject_msi (struct kvm *kvm,struct kvm_msi *msi) +{ +return -ENODEV ; +}#endif int kvm_register_vgic_device (unsigned long type);
M.
Signature · 1 line
Jazz is not dead. It just smells funny...
Andre,
Quoted 15 lines · Andre Przywara
Hi,
please welcome a reworked version of the ITS emulation support. It allows those KVM guests that use an emulated GICv3 to use LPIs as well, though in the moment this is limited to emulated PCI devices. This is based on v4.7-rc1 and only works with the new VGIC implementation. There have been some modifications due to changes in the underlying VGIC code (changed MMIO handlers, etc.).
Some other changes relate to the fact that any redistributor related variables are now held in thevgic_cpustructure, so they are naturally per VCPU.
Also this series supports multiple ITSes and makes them independent from the distributor. There is a new KVM device, which can be created multiple times.
Quoted 7 lines
You can find all of this code (and the prerequisites) in the its-emul/v5 branch of my repository [1].
This has been briefly tested on the model and on GICv3 hardware. Since there are quite some fundamental changes in this version, I expect some issues in the code, so if you have GICv3 capable hardware, please test it on your setup.
Also of course any review comments are very welcome!
I've stopped reviewing after patch 12, as there is way too many design issues. What I really want to see in the next drop:
1) Locking. The current implementation is too complicated, has holes, and this leads to interesting bugs.
2) Split between vgic_irq and ITEs. They are not the same, and your design explodes in the face of multiple ITSs.
3) Handling of collections is extremely creative, and doesn't remotely match my reading of the architecture.
There is also all kind of buglets all over the shop, which can be swiftly addressed, but the above three points are real blockers for me.
Thanks,
M.
Signature · 1 line
Jazz is not dead. It just smells funny...