KVM: arm/arm64: Rework virtual GIC emulation
Subsystems (5)
v4 cover KVM: arm/arm64: Rework virtual GIC emulation
Hi,
a new update for the new VGIC series.
A lot of fixes this time: comments fixed, clarified and updated, some no longer needed code pieces from the old VGIC have been removed. The MMIO dispatching has been improved to allow only the access types that are mandatory in the spec, so 32-bit accesses only for most registers with some additionally accessible by byte or 64-bit accesses as well. This allows for some simplification in many handler functions. The vgic_queue_irq_unlock() function was reworked a bit and is now used by the set-active handler as well. The clear-active handler has been extended to basically halt the guest and make sure we clear the respective active bit in the LR (and not only in our structure). There is a new patch introducing the KVM core bits of this feature.
Also we now detect when the redistributor and distributor regions overlap and complain about it.
As this series is now based on kvmarm/next, we also gained the firmware independent probing feature, which is in turn also used in this series. This should enable ACPI machines.
A more detailed changelog can be found in the respective patches.
As this new VGIC emulation will probably become the default very shortly, please test it full steam on whatever setup you have access to. Any reports are warmly welcome.
This series is now based on kvmarm/next, which will be merged shortly into Linus' kernel.
A git tree containing this series can be found on linux-arm.org:
git://linux-arm.org/linux-ap.git branch: vgic-new/v4
http://www.linux-arm.org/git?p=linux-ap.git;a=log;h=refs/heads/vgic-new/v4
Cheers,
Andre.
===========================
This series is a joint effort to re-implement KVM's GIC emulation. Many patches as in here have been effectively authored by multiple people and authorship isn't clear in every case, especially since many of the patches have been stashed together or splitted apart in the process. So take the Author: tag with a grain of salt.
While the current VGIC implementation is centered around providing efficient MMIO emulation, the hot path for most guests is actually the guest entry and exit, which currently is rather costly. Also the existing emulation has a global distributor lock, which quickly becomes a bottleneck once the number of VCPUs increases. Additionally the emulation was originally designed for GICv2, adding GICv3 ITS emulation support to this proved to be rather painful. Last, but not least the existing code became less and less maintainable, with many special cases handled explicitly.
The new implementation is build around a struct vgic_irq data structure, which holds all information about a virtual interrupt. Interruts which should be injected are held in a per-VCPU list, this make the entry/exit path much more efficient. Also the new structure allows to have more fine grained locking - per IRQ and per VCPU - getting rid of the global distributor lock.
As a result of the new design ITS emulation fits in more nicely, the respective code will be provided as a follow-up series.
This series implements the same feature set as the existing emulation, as a goodie we now implement priorities correctly.
To allow an easy transition with good test coverage, but still maintain stability, both implementations live side by side, selectable via a Kconfig option. The default is the new implementation.
If this code proves to be reliable, we will later remove the current implementation with an extra patch set.
Please have a look at the series, review it and give the code some serious testing (and possibly debugging). All feedback is appreciated.
Cheers,
Andre.
Andre Przywara (29):
KVM: arm/arm64: vgic: streamline vgic_update_irq_pending() interface
KVM: arm/arm64: vgic: avoid map in kvm_vgic_inject_mapped_irq()
KVM: arm/arm64: vgic: avoid map in kvm_vgic_map_is_active()
KVM: arm/arm64: vgic: avoid map in kvm_vgic_unmap_phys_irq()
KVM: arm/arm64: arch_timer: Remove irq_phys_map
KVM: arm/arm64: vgic: Remove irq_phys_map from interface
KVM: arm/arm64: pmu: abstract access to number of SPIs
KVM: arm/arm64: move GICv2 emulation defines into arm-gic-v3.h
KVM: arm/arm64: vgic-new: Add GICv2 MMIO handling framework
KVM: arm/arm64: vgic-new: Add ENABLE registers handlers
KVM: arm/arm64: vgic-new: Add PENDING registers handlers
KVM: arm/arm64: vgic-new: Add ACTIVE registers handlers
KVM: arm/arm64: vgic-new: Add PRIORITY registers handlers
KVM: arm/arm64: vgic-new: Add CONFIG registers handlers
KVM: arm/arm64: vgic-new: Add TARGET registers handlers
KVM: arm/arm64: vgic-new: Add SGIR register handler
KVM: arm/arm64: vgic-new: Add SGIPENDR register handlers
KVM: arm/arm64: vgic-new: Add GICv3 MMIO handling framework
KVM: arm/arm64: vgic-new: Add GICv3 CTLR, IIDR, TYPER handlers
KVM: arm/arm64: vgic-new: Add GICv3 redistributor IIDR and TYPER
handler
KVM: arm/arm64: vgic-new: Add GICv3 IDREGS register handler
KVM: arm/arm64: vgic-new: Add GICv3 IROUTER register handlers
KVM: arm/arm64: vgic-new: Add GICv3 SGI system register trap handler
KVM: arm/arm64: vgic-new: Add userland access to VGIC dist registers
KVM: arm/arm64: vgic-new: Add GICH_VMCR accessors
KVM: arm/arm64: vgic-new: Add userland GIC CPU interface access
KVM: arm/arm64: vgic-new: Wire up irqfd injection
KVM: arm/arm64: vgic-new: implement mapped IRQ handling
KVM: arm/arm64: vgic-new: enable build
Christoffer Dall (10):
KVM: arm/arm64: Remove the IRQ field from struct irq_phys_map
KVM: arm/arm64: Get rid of vgic_cpu->nr_lr
KVM: arm/arm64: Fix MMIO emulation data handling
KVM: arm/arm64: Export mmio_read/write_bus
KVM: arm/arm64: Provide functionality to pause and resume a guest
KVM: arm/arm64: vgic-new: Add data structure definitions
KVM: arm/arm64: vgic-new: Add acccessor to new struct vgic_irq
instance
KVM: arm/arm64: vgic-new: Implement virtual IRQ injection
KVM: arm/arm64: vgic-new: Add IRQ sorting
KVM: arm/arm64: vgic-new: Export register access interface
Eric Auger (12):
KVM: arm/arm64: vgic-new: Implement kvm_vgic_vcpu_pending_irq
KVM: arm/arm64: vgic-new: vgic_kvm_device: KVM device ops registration
KVM: arm/arm64: vgic-new: vgic_kvm_device:
KVM_DEV_ARM_VGIC_GRP_NR_IRQS
KVM: arm/arm64: vgic-new: vgic_kvm_device: KVM_DEV_ARM_VGIC_GRP_CTRL
KVM: arm/arm64: vgic-new: vgic_kvm_device: implement kvm_vgic_addr
KVM: arm/arm64: vgic-new: vgic_kvm_device: KVM_DEV_ARM_VGIC_GRP_ADDR
KVM: arm/arm64: vgic-new: vgic_kvm_device: access to VGIC registers
KVM: arm/arm64: vgic-new: vgic_init: implement kvm_vgic_hyp_init
KVM: arm/arm64: vgic-new: vgic_init: implement vgic_create
KVM: arm/arm64: vgic-new: vgic_init: implement vgic_init
KVM: arm/arm64: vgic-new: vgic_init: implement map_resources
KVM: arm/arm64: vgic-new: Add vgic_v2/v3_enable
Marc Zyngier (5):
KVM: arm/arm64: vgic-new: Add IRQ sync/flush framework
KVM: arm/arm64: vgic-new: Add GICv2 world switch backend
KVM: arm/arm64: vgic-new: Add GICv3 world switch backend
KVM: arm/arm64: vgic-new: Add MMIO handling framework
KVM: arm/arm64: vgic-new: Add CTLR, TYPER and IIDR handlers
arch/arm/include/asm/kvm_host.h | 4 + arch/arm/include/asm/kvm_mmio.h | 3 + arch/arm/kvm/Kconfig | 7 + arch/arm/kvm/Makefile | 11 + arch/arm/kvm/arm.c | 25 +- arch/arm/kvm/mmio.c | 24 +- arch/arm64/include/asm/kvm_host.h | 4 + arch/arm64/include/asm/kvm_mmio.h | 3 + arch/arm64/kvm/Kconfig | 7 + arch/arm64/kvm/Makefile | 12 + include/kvm/arm_arch_timer.h | 3 - include/kvm/arm_vgic.h | 20 +- include/kvm/vgic/vgic.h | 246 +++++++++++++++ include/linux/irqchip/arm-gic-v3.h | 6 + include/linux/irqchip/arm-gic.h | 2 + virt/kvm/arm/arch_timer.c | 42 ++- virt/kvm/arm/hyp/vgic-v2-sr.c | 17 +- virt/kvm/arm/pmu.c | 25 +- virt/kvm/arm/vgic-v2.c | 4 +- virt/kvm/arm/vgic-v3.c | 8 +- virt/kvm/arm/vgic.c | 86 ++--- virt/kvm/arm/vgic/vgic-init.c | 452 ++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic-irqfd.c | 52 +++ virt/kvm/arm/vgic/vgic-kvm-device.c | 426 +++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic-mmio-v2.c | 446 ++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic-mmio-v3.c | 451 ++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic-mmio.c | 499 +++++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic-mmio.h | 138 ++++++++ virt/kvm/arm/vgic/vgic-v2.c | 352 +++++++++++++++++++++ virt/kvm/arm/vgic/vgic-v3.c | 330 +++++++++++++++++++ virt/kvm/arm/vgic/vgic.c | 615 ++++++++++++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic.h | 131 ++++++++ 32 files changed, 4314 insertions(+), 137 deletions(-) create mode 100644 include/kvm/vgic/vgic.h create mode 100644 virt/kvm/arm/vgic/vgic-init.c create mode 100644 virt/kvm/arm/vgic/vgic-irqfd.c create mode 100644 virt/kvm/arm/vgic/vgic-kvm-device.c create mode 100644 virt/kvm/arm/vgic/vgic-mmio-v2.c create mode 100644 virt/kvm/arm/vgic/vgic-mmio-v3.c create mode 100644 virt/kvm/arm/vgic/vgic-mmio.c create mode 100644 virt/kvm/arm/vgic/vgic-mmio.h create mode 100644 virt/kvm/arm/vgic/vgic-v2.c create mode 100644 virt/kvm/arm/vgic/vgic-v3.c create mode 100644 virt/kvm/arm/vgic/vgic.c create mode 100644 virt/kvm/arm/vgic/vgic.h
Signature · 1 line
2.8.2
PATCH v4 3/56 KVM: arm/arm64: vgic: avoid map in kvm_vgic_map_is_active()
For getting the active state of a mapped IRQ, we actually only need the virtual IRQ number, not the pointer to the mapping entry. Pass the virtual IRQ number from the arch timer to the VGIC directly.
---
Changelog v1 .. v2:
- rewrap phys_active determination to fit in 80 characters
include/kvm/arm_vgic.h | 2 +- virt/kvm/arm/arch_timer.c | 6 ++---- virt/kvm/arm/vgic.c | 6 +++--- 3 files changed, 6 insertions(+), 8 deletions(-)
include/kvm/arm_vgic.h +1 -1
@@ -348,7 +348,7 @@ int kvm_vgic_vcpu_pending_irq(struct kvm_vcpu *vcpu);struct irq_phys_map *kvm_vgic_map_phys_irq (struct kvm_vcpu *vcpu,int virt_irq,int irq);int kvm_vgic_unmap_phys_irq (struct kvm_vcpu *vcpu,struct irq_phys_map *map); -bool kvm_vgic_map_is_active (struct kvm_vcpu *vcpu,struct irq_phys_map *map); +bool kvm_vgic_map_is_active (struct kvm_vcpu *vcpu,unsigned int virt_irq);#define irqchip_in_kernel (k) (!!((k)->arch.vgic.in_kernel))#define vgic_initialized (k) (!!((k)->arch.vgic.nr_cpus))
virt/kvm/arm/arch_timer.c +2 -4
@@ -274,10 +274,8 @@ void kvm_timer_flush_hwstate(struct kvm_vcpu *vcpu)* to ensure that hardware interrupts from the timer triggers a guest * exit. */ -if (timer->irq.level ||kvm_vgic_map_is_active (vcpu, timer->map)) - phys_active = true; -else - phys_active = false; + phys_active = timer->irq.level || +kvm_vgic_map_is_active (vcpu, timer->map->virt_irq);/* * We want to avoid hitting the (re)distributor as much as
virt/kvm/arm/vgic.c +3 -3
@@ -1102,18 +1102,18 @@ static bool dist_active_irq(struct kvm_vcpu *vcpu)return test_bit (vcpu->vcpu_id, dist->irq_active_on_cpu); } -bool kvm_vgic_map_is_active (struct kvm_vcpu *vcpu,struct irq_phys_map *map) +bool kvm_vgic_map_is_active (struct kvm_vcpu *vcpu,unsigned int virt_irq) {int i;for (i =0 ; i < vcpu->arch.vgic_cpu.nr_lr; i++) {struct vgic_lr vlr =vgic_get_lr (vcpu, i); -if (vlr.irq == map->virt_irq && vlr.state &LR_STATE_ACTIVE ) +if (vlr.irq == virt_irq && vlr.state &LR_STATE_ACTIVE )return true; } -return vgic_irq_is_active (vcpu, map->virt_irq); +return vgic_irq_is_active (vcpu, virt_irq); }/*
Signature · 1 line
2.8.2
PATCH v4 6/56 KVM: arm/arm64: arch_timer: Remove irq_phys_map
In reply to Andre PrzywaraNow that the interface between the arch timer and the VGIC does not require passing the irq_phys_map entry pointer anymore, let's remove it from the virtual arch timer and use the virtual IRQ number instead directly.
The remaining pointer returned by kvm_vgic_map_phys_irq() will be removed in the following patch.
---
Changelog v1 .. v2:
- remove extra virt_irq member from struct, instead use irq.irq directly
Changelog v2 .. v3:
- remove bogus validity check from kvm_vgic_unmap_phys_irq()
include/kvm/arm_arch_timer.h | 3 --- virt/kvm/arm/arch_timer.c | 10 ++++------ 2 files changed, 4 insertions(+), 9 deletions(-)
include/kvm/arm_arch_timer.h +0 -3
@@ -53,9 +53,6 @@ struct arch_timer_cpu {/* Timer IRQ */ struct kvm_irq_level irq; -/* VGIC mapping */ -struct irq_phys_map *map; -/* Active IRQ state caching */ bool active_cleared_last; };
virt/kvm/arm/arch_timer.c +4 -6
@@ -175,10 +175,10 @@ static void kvm_timer_update_irq(struct kvm_vcpu *vcpu, bool new_level) timer->active_cleared_last = false; timer->irq.level = new_level; -trace_kvm_timer_update_irq (vcpu->vcpu_id, timer->map->virt_irq, +trace_kvm_timer_update_irq (vcpu->vcpu_id, timer->irq.irq, timer->irq.level); ret =kvm_vgic_inject_mapped_irq (vcpu->kvm, vcpu->vcpu_id, - timer->map->virt_irq, + timer->irq.irq, timer->irq.level);WARN_ON (ret); }
@@ -276,7 +276,7 @@ void kvm_timer_flush_hwstate(struct kvm_vcpu *vcpu)* exit. */ phys_active = timer->irq.level || -kvm_vgic_map_is_active (vcpu, timer->map->virt_irq); +kvm_vgic_map_is_active (vcpu, timer->irq.irq);/* * We want to avoid hitting the (re)distributor as much as
@@ -378,7 +378,6 @@ int kvm_timer_vcpu_reset(struct kvm_vcpu *vcpu,if (WARN_ON (IS_ERR (map)))return PTR_ERR (map); - timer->map = map;return 0 ; }
@@ -504,8 +503,7 @@ void kvm_timer_vcpu_terminate(struct kvm_vcpu *vcpu)struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;timer_disarm (timer); -if (timer->map) -kvm_vgic_unmap_phys_irq (vcpu, timer->map->virt_irq); +kvm_vgic_unmap_phys_irq (vcpu, timer->irq.irq); }void kvm_timer_enable (struct kvm *kvm)
Signature · 1 line
2.8.2
PATCH v4 7/56 KVM: arm/arm64: vgic: Remove irq_phys_map from interface
In reply to Andre PrzywaraNow that the virtual arch timer does not care about the irq_phys_map anymore, let's rework kvm_vgic_map_phys_irq() to return an error value instead. Any reference to that mapping can later be done by passing the correct combination of VCPU and virtual IRQ number. This makes the irq_phys_map handling completely private to the VGIC code.
---
Changelog v1 .. v2:
- adaptations to changes in previous patches (removal of virt_irq)
Changelog v2 .. v3:
- simplify return in kvm_timer_vcpu_reset()
include/kvm/arm_vgic.h | 3 +-- virt/kvm/arm/arch_timer.c | 7 +------ virt/kvm/arm/vgic.c | 15 +++++++-------- 3 files changed, 9 insertions(+), 16 deletions(-)
include/kvm/arm_vgic.h +1 -2
@@ -344,8 +344,7 @@ int kvm_vgic_inject_mapped_irq(struct kvm *kvm, int cpuid, unsigned int virt_irq, bool level);void vgic_v3_dispatch_sgi (struct kvm_vcpu *vcpu,u64 reg);int kvm_vgic_vcpu_pending_irq (struct kvm_vcpu *vcpu); -struct irq_phys_map *kvm_vgic_map_phys_irq (struct kvm_vcpu *vcpu, -int virt_irq,int phys_irq); +int kvm_vgic_map_phys_irq (struct kvm_vcpu *vcpu,int virt_irq,int phys_irq);int kvm_vgic_unmap_phys_irq (struct kvm_vcpu *vcpu,unsigned int virt_irq);bool kvm_vgic_map_is_active (struct kvm_vcpu *vcpu,unsigned int virt_irq);
virt/kvm/arm/arch_timer.c +1 -6
@@ -333,7 +333,6 @@ int kvm_timer_vcpu_reset(struct kvm_vcpu *vcpu, const struct kvm_irq_level *irq) {struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; -struct irq_phys_map *map;struct irq_desc *desc;struct irq_data *data;int phys_irq;
@@ -374,11 +373,7 @@ int kvm_timer_vcpu_reset(struct kvm_vcpu *vcpu,* Tell the VGIC that the virtual interrupt is tied to a * physical interrupt. We do that once per VCPU. */ - map =kvm_vgic_map_phys_irq (vcpu, irq->irq, phys_irq); -if (WARN_ON (IS_ERR (map))) -return PTR_ERR (map); - -return 0 ; +return kvm_vgic_map_phys_irq (vcpu, irq->irq, phys_irq); }void kvm_timer_vcpu_init (struct kvm_vcpu *vcpu)
virt/kvm/arm/vgic.c +7 -8
@@ -1719,21 +1719,20 @@ static struct list_head *vgic_get_irq_phys_map_list(struct kvm_vcpu *vcpu,* the physical interrupt represented by @phys_irq. This mapping can be * established multiple times as long as the parameters are the same. * -* Returns a valid pointer on success, and an error pointer otherwise +* Returns 0 on success or an error value otherwise. */ -struct irq_phys_map *kvm_vgic_map_phys_irq (struct kvm_vcpu *vcpu, -int virt_irq,int phys_irq) +int kvm_vgic_map_phys_irq (struct kvm_vcpu *vcpu,int virt_irq,int phys_irq) {struct vgic_dist *dist = &vcpu->kvm->arch.vgic;struct list_head *root =vgic_get_irq_phys_map_list (vcpu, virt_irq);struct irq_phys_map *map;struct irq_phys_map_entry *entry; - +int ret =0 ;/* Create a new mapping */ entry =kzalloc (sizeof (*entry),GFP_KERNEL );if (!entry) -return ERR_PTR (-ENOMEM ); +return -ENOMEM ;spin_lock (&dist->irq_phys_map_lock);
@@ -1742,7 +1741,7 @@ struct irq_phys_map *kvm_vgic_map_phys_irq(struct kvm_vcpu *vcpu,if (map) {/* Make sure this mapping matches */ if (map->phys_irq != phys_irq) - map =ERR_PTR (-EINVAL ); + ret = -EINVAL ;/* Found an existing, valid mapping */ goto out;
@@ -1758,9 +1757,9 @@ out:spin_unlock (&dist->irq_phys_map_lock);/* If we've found a hit in the existing list, free the useless * entry */ -if (IS_ERR (map) || map != &entry->map) +if (ret || map != &entry->map)kfree (entry); -return map; +return ret; } static struct irq_phys_map *vgic_irq_map_search(struct kvm_vcpu *vcpu,
Signature · 1 line
2.8.2
PATCH v4 9/56 KVM: arm/arm64: Fix MMIO emulation data handling
In reply to Andre PrzywaraFrom: Christoffer Dall <[email protected]>
When the kernel was handling a guest MMIO read access internally, we need to copy the emulation result into the run->mmio structure in order for the kvm_handle_mmio_return() function to pick it up and inject the
result back into the guest.
Currently the only user of kvm_io_bus for ARM is the VGIC, which did this copying itself, so this was not causing issues so far.
But with the upcoming new vgic implementation we need this done properly.
Update the kvm_handle_mmio_return description and cleanup the code to only perform a single copying when needed.
Code and commit message inspired by Andre Przywara.
--- arch/arm/kvm/mmio.c | 14 +++++++------- virt/kvm/arm/vgic.c | 7 ------- 2 files changed, 7 insertions(+), 14 deletions(-)
arch/arm/kvm/mmio.c +7 -7
@@ -87,11 +87,10 @@ static unsigned long mmio_read_buf(char *buf, unsigned int len)/** * kvm_handle_mmio_return -- Handle MMIO loads after user space emulation +* or in-kernel IO emulation +* * @vcpu: The VCPU pointer * @run: The VCPU run struct containing the mmio data -* -* This should only be called after returning from userspace for MMIO load -* emulation. */ int kvm_handle_mmio_return (struct kvm_vcpu *vcpu,struct kvm_run *run) {
@@ -206,18 +205,19 @@ int io_mem_abort(struct kvm_vcpu *vcpu, struct kvm_run *run, run->mmio.is_write = is_write; run->mmio.phys_addr = fault_ipa; run->mmio.len = len; -if (is_write) -memcpy (run->mmio.data, data_buf, len);if (!ret) {/* We handled the access successfully in the kernel. */ +if (!is_write) +memcpy (run->mmio.data, data_buf, len); vcpu->stat.mmio_exit_kernel++;kvm_handle_mmio_return (vcpu, run);return 1 ; - }else { - vcpu->stat.mmio_exit_user++; } +if (is_write) +memcpy (run->mmio.data, data_buf, len); + vcpu->stat.mmio_exit_user++; run->exit_reason =KVM_EXIT_MMIO ;return 0 ; }
virt/kvm/arm/vgic.c +0 -7
@@ -819,7 +819,6 @@ static int vgic_handle_mmio_access(struct kvm_vcpu *vcpu,struct vgic_dist *dist = &vcpu->kvm->arch.vgic;struct vgic_io_device *iodev =container_of (this, struct vgic_io_device, dev); -struct kvm_run *run = vcpu->run;const struct vgic_io_range *range;struct kvm_exit_mmio mmio;bool updated_state;
@@ -848,12 +847,6 @@ static int vgic_handle_mmio_access(struct kvm_vcpu *vcpu, updated_state = false; }spin_unlock (&dist->lock); - run->mmio.is_write = is_write; - run->mmio.len = len; - run->mmio.phys_addr = addr; -memcpy (run->mmio.data, val, len); - -kvm_handle_mmio_return (vcpu, run);if (updated_state)vgic_kick_vcpus (vcpu->kvm);
Signature · 1 line
2.8.2
PATCH v4 11/56 KVM: arm/arm64: pmu: abstract access to number of SPIs
In reply to Andre PrzywaraCurrently the PMU uses a member of the struct vgic_dist directly, which not only breaks abstraction, but will fail with the new VGIC. Abstract this access in the VGIC header file and refactor the validity check in the PMU code.
---
Changelog v3 .. v4:
- fix logic to allow PPIs again
- refactor IRQ validity check
include/kvm/arm_vgic.h | 2 ++ virt/kvm/arm/pmu.c | 25 ++++++++++++++----------- 2 files changed, 16 insertions(+), 11 deletions(-)
include/kvm/arm_vgic.h +2 -0
@@ -348,6 +348,8 @@ bool kvm_vgic_map_is_active(struct kvm_vcpu *vcpu, unsigned int virt_irq);#define irqchip_in_kernel (k) (!!((k)->arch.vgic.in_kernel))#define vgic_initialized (k) (!!((k)->arch.vgic.nr_cpus))#define vgic_ready (k) ((k)->arch.vgic.ready) +#define vgic_valid_spi (k, i) (((i) >= VGIC_NR_PRIVATE_IRQS) && \ + ((i) < (k)->arch.vgic.nr_irqs)) int vgic_v2_probe(const struct gic_kvm_info *gic_kvm_info, const struct vgic_ops **ops,
virt/kvm/arm/pmu.c +14 -11
@@ -436,7 +436,14 @@ static int kvm_arm_pmu_v3_init(struct kvm_vcpu *vcpu)return 0 ; } -static bool irq_is_valid (struct kvm *kvm,int irq,bool is_ppi) +#define irq_is_ppi (irq) ((irq) >= VGIC_NR_SGIS && (irq) < VGIC_NR_PRIVATE_IRQS) + +/* +* For one VM the interrupt type must be same for each vcpu. +* As a PPI, the interrupt number is the same for all vcpus, +* while as an SPI it must be a separate number per vcpu. +*/ +static bool pmu_irq_is_valid (struct kvm *kvm,int irq) {int i;struct kvm_vcpu *vcpu;
@@ -445,7 +452,7 @@ static bool irq_is_valid(struct kvm *kvm, int irq, bool is_ppi)if (!kvm_arm_pmu_irq_initialized (vcpu))continue ; -if (is_ppi) { +if (irq_is_ppi (irq)) {if (vcpu->arch.pmu.irq_num != irq)return false; }else {
@@ -457,7 +464,6 @@ static bool irq_is_valid(struct kvm *kvm, int irq, bool is_ppi)return true; } -int kvm_arm_pmu_v3_set_attr (struct kvm_vcpu *vcpu,struct kvm_device_attr *attr) {switch (attr->attr) {
@@ -471,14 +477,11 @@ int kvm_arm_pmu_v3_set_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)if (get_user (irq, uaddr))return -EFAULT ; -/* -* The PMU overflow interrupt could be a PPI or SPI, but for one -* VM the interrupt type must be same for each vcpu. As a PPI, -* the interrupt number is the same for all vcpus, while as an -* SPI it must be a separate number per vcpu. -*/ -if (irq <VGIC_NR_SGIS || irq >= vcpu->kvm->arch.vgic.nr_irqs || - !irq_is_valid (vcpu->kvm, irq, irq <VGIC_NR_PRIVATE_IRQS )) +/* The PMU overflow interrupt can be a PPI or a valid SPI. */ +if (!(irq_is_ppi (irq) ||vgic_valid_spi (vcpu->kvm, irq))) +return -EINVAL ; + +if (!pmu_irq_is_valid (vcpu->kvm, irq))return -EINVAL ;if (kvm_arm_pmu_irq_initialized (vcpu))
Signature · 1 line
2.8.2
PATCH v4 12/56 KVM: arm/arm64: Provide functionality to pause and resume a guest
In reply to Andre PrzywaraFrom: Christoffer Dall <[email protected]>
For some rare corner cases in our VGIC emulation later we have to stop the guest to make sure the VGIC state is consistent. Provide the necessary framework to pause and resume a guest.
--- arch/arm/include/asm/kvm_host.h | 4 ++++ arch/arm/kvm/arm.c | 25 +++++++++++++------------ arch/arm64/include/asm/kvm_host.h | 4 ++++ 3 files changed, 21 insertions(+), 12 deletions(-)
arch/arm/include/asm/kvm_host.h +4 -0
@@ -41,6 +41,8 @@#define KVM_MAX_VCPUS VGIC_V2_MAX_CPUS +#define KVM_REQ_VCPU_EXIT 8 +u32 *kvm_vcpu_reg (struct kvm_vcpu *vcpu,u8 reg_num,u32 mode); int __attribute_const__ kvm_target_cpu(void);int kvm_reset_vcpu (struct kvm_vcpu *vcpu);
@@ -225,6 +227,8 @@ static inline void kvm_arch_mmu_notifier_invalidate_page(struct kvm *kvm,struct kvm_vcpu *kvm_arm_get_running_vcpu (void );struct kvm_vcpu __percpu **kvm_get_running_vcpus (void ); +void kvm_arm_halt_guest (struct kvm *kvm); +void kvm_arm_resume_guest (struct kvm *kvm);int kvm_arm_copy_coproc_indices (struct kvm_vcpu *vcpu,u64 __user *uindices);unsigned long kvm_arm_num_coproc_regs (struct kvm_vcpu *vcpu);
arch/arm/kvm/arm.c +13 -12
@@ -492,30 +492,31 @@ bool kvm_arch_intc_initialized(struct kvm *kvm)return vgic_initialized (kvm); } -static void kvm_arm_halt_guest (struct kvm *kvm)__maybe_unused ; -static void kvm_arm_resume_guest (struct kvm *kvm)__maybe_unused ; - -static void kvm_arm_halt_guest (struct kvm *kvm) +void kvm_arm_halt_guest (struct kvm *kvm) {int i;struct kvm_vcpu *vcpu;kvm_for_each_vcpu (i, vcpu, kvm) vcpu->arch.pause = true; -force_vm_exit (cpu_all_mask); +kvm_make_all_cpus_request (kvm,KVM_REQ_VCPU_EXIT ); +} + +static void kvm_arm_resume_vcpu (struct kvm_vcpu *vcpu) +{ +struct swait_queue_head *wq =kvm_arch_vcpu_wq (vcpu); + + vcpu->arch.pause = false; +swake_up (wq); } -static void kvm_arm_resume_guest (struct kvm *kvm) +void kvm_arm_resume_guest (struct kvm *kvm) {int i;struct kvm_vcpu *vcpu; -kvm_for_each_vcpu (i, vcpu, kvm) { -struct swait_queue_head *wq =kvm_arch_vcpu_wq (vcpu); - - vcpu->arch.pause = false; -swake_up (wq); - } +kvm_for_each_vcpu (i, vcpu, kvm) +kvm_arm_resume_vcpu (vcpu); }static void vcpu_sleep (struct kvm_vcpu *vcpu)
arch/arm64/include/asm/kvm_host.h +4 -0
@@ -44,6 +44,8 @@#define KVM_VCPU_MAX_FEATURES 4 +#define KVM_REQ_VCPU_EXIT 8 + int __attribute_const__ kvm_target_cpu(void);int kvm_reset_vcpu (struct kvm_vcpu *vcpu);int kvm_arch_dev_ioctl_check_extension (long ext);
@@ -325,6 +327,8 @@ static inline void kvm_arch_mmu_notifier_invalidate_page(struct kvm *kvm,struct kvm_vcpu *kvm_arm_get_running_vcpu (void );struct kvm_vcpu *__percpu *kvm_get_running_vcpus (void ); +void kvm_arm_halt_guest (struct kvm *kvm); +void kvm_arm_resume_guest (struct kvm *kvm);u64 __kvm_call_hyp (void *hypfn, ...);#define kvm_call_hyp (f, ...) __kvm_call_hyp(kvm_ksym_ref(f), ##__VA_ARGS__)
Signature · 1 line
2.8.2
PATCH v4 16/56 KVM: arm/arm64: vgic-new: Implement virtual IRQ injection
In reply to Andre PrzywaraFrom: Christoffer Dall <[email protected]>
Provide a vgic_queue_irq_unlock() function which decides whether a given IRQ needs to be queued to a VCPU's ap_list.
This should be called whenever an IRQ becomes pending or enabled, either as a result of userspace injection, from in-kernel emulated devices like the architected timer or from MMIO accesses to the distributor emulation.
Also provides the necessary functions to allow userland to inject an IRQ to a guest.
Since this is the first code that starts using our locking mechanism, we add some (hopefully) clear documentation of our locking strategy and requirements along with this patch.
[Andre: refactor out vgic_queue_irq_unlock()]
---
Changelog RFC..v1:
- add spinlock checks protected by CONFIG_DEBUG_SPINLOCK
- add more comments to vgic_target_oracle
- remove BUG_ON() if IRQ is neither edge or level
- rename vgic_queue_irq() to vgic_queue_irq_unlock()
- simplify initial check in vgic_queue_irq_unlock()
- extend retry loop to ask the oracle again
- minor comment fixes
Changelog v1 .. v2:
- move most IRQ injection code into vgic_update_irq_pending()
Changelog v3 .. v4:
- simplify vgic_queue_irq_unlock() to allow reusage later
include/kvm/vgic/vgic.h | 3 + virt/kvm/arm/vgic/vgic.c | 211 +++++++++++++++++++++++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic.h | 1 + 3 files changed, 215 insertions(+)
include/kvm/vgic/vgic.h +3 -0
@@ -181,6 +181,9 @@ struct vgic_cpu {u64 live_lrs; }; +int kvm_vgic_inject_irq (struct kvm *kvm,int cpuid,unsigned int intid, +bool level); +#define irqchip_in_kernel (k) (!!((k)->arch.vgic.in_kernel))#define vgic_initialized (k) (false)#define vgic_ready (k) ((k)->arch.vgic.ready)
virt/kvm/arm/vgic/vgic.c +211 -0
222 lines not on this page · raw message
virt/kvm/arm/vgic/vgic.h +1 -0
@@ -18,5 +18,6 @@struct vgic_irq *vgic_get_irq (struct kvm *kvm,struct kvm_vcpu *vcpu,u32 intid); +bool vgic_queue_irq_unlock (struct kvm *kvm,struct vgic_irq *irq);#endif
Signature · 1 line
2.8.2
PATCH v4 18/56 KVM: arm/arm64: vgic-new: Add IRQ sync/flush framework
In reply to Andre PrzywaraFrom: Marc Zyngier <[email protected]>
Implement the framework for syncing IRQs between our emulation and the list registers, which represent the guest's view of IRQs. This is done in kvm_vgic_flush_hwstate and kvm_vgic_sync_hwstate, which gets called on guest entry and exit.
The code talking to the actual GICv2/v3 hardware is added in the following patches.
---
Changelog RFC..v1:
- split out vgic_clear_lr() from vgic_populate_lr()
- rename vgic_populate_lrs() to vgic_flush_lr_state()
- clean all LRs when the distributor is disabled
- use list_del() instead of list_del_init()
- add comments to explain the direction of sync/flush_hwstate
- remove unneeded BUG_ON(in_interrupt()
Changelog v2 .. v3:
- remove bogus v2 specific rebase leftovers
Changelog v3 .. v4:
- amend locks requirements
include/kvm/vgic/vgic.h | 4 + virt/kvm/arm/vgic/vgic.c | 191 +++++++++++++++++++++++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic.h | 2 + 3 files changed, 197 insertions(+)
include/kvm/vgic/vgic.h +4 -0
@@ -190,6 +190,10 @@ int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int intid,#define vgic_valid_spi (k, i) (((i) >= VGIC_NR_PRIVATE_IRQS) && \ ((i) < (k)->arch.vgic.nr_spis + VGIC_NR_PRIVATE_IRQS)) +bool kvm_vcpu_has_pending_irqs (struct kvm_vcpu *vcpu); +void kvm_vgic_sync_hwstate (struct kvm_vcpu *vcpu); +void kvm_vgic_flush_hwstate (struct kvm_vcpu *vcpu); +/** * kvm_vgic_get_max_vcpus - Get the maximum number of VCPUs allowed by HW *
virt/kvm/arm/vgic/vgic.c +191 -0
194 lines not on this page · raw message
virt/kvm/arm/vgic/vgic.h +2 -0
@@ -16,6 +16,8 @@#ifndef __KVM_ARM_VGIC_NEW_H__#define __KVM_ARM_VGIC_NEW_H__ +#define vgic_irq_is_sgi (intid) ((intid) < VGIC_NR_SGIS) +struct vgic_irq *vgic_get_irq (struct kvm *kvm,struct kvm_vcpu *vcpu,u32 intid);bool vgic_queue_irq_unlock (struct kvm *kvm,struct vgic_irq *irq);
Signature · 1 line
2.8.2
PATCH v4 20/56 KVM: arm/arm64: vgic-new: Add GICv3 world switch backend
In reply to Andre PrzywaraFrom: Marc Zyngier <[email protected]>
As the GICv3 virtual interface registers differ from their GICv2 siblings, we need different handlers for processing maintenance interrupts and reading/writing to the LRs.
Implement the respective handler functions and connect them to existing code to be called if the host is using a GICv3.
---
Changelog RFC..v1:
- remove outdated comment about the dist_lock
- add WARN_ON about LR_STATE not being 0 in maintenance interrupts
Changelog v1 .. v2:
- inject the IRQ priority into the list register
Changelog v2 .. v3:
- remove no longer needed irqchip/arm-gic.h inclusion
Changelog v3 .. v4:
- remove ELRSR bit set during process_maintenance()
- refine comment about Group1 interrupts
- remove wrong comment about ap_list_lock in vgic_v3_fold_lr_state()
include/linux/irqchip/arm-gic-v3.h | 1 + virt/kvm/arm/vgic/vgic-v3.c | 162 +++++++++++++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic.c | 25 ++++-- virt/kvm/arm/vgic/vgic.h | 29 +++++++ 4 files changed, 212 insertions(+), 5 deletions(-) create mode 100644 virt/kvm/arm/vgic/vgic-v3.c
include/linux/irqchip/arm-gic-v3.h +1 -0
@@ -275,6 +275,7 @@#define ICH_LR_ACTIVE_BIT (1ULL << 63)#define ICH_LR_PHYS_ID_SHIFT 32#define ICH_LR_PHYS_ID_MASK (0x3ffULL << ICH_LR_PHYS_ID_SHIFT) +#define ICH_LR_PRIORITY_SHIFT 48/* These are for GICv2 emulation only */ #define GICH_LR_VIRTUALID (0x3ffUL << 0)
virt/kvm/arm/vgic/vgic-v3.c new file +162 -0
162 lines not on this page · raw message
virt/kvm/arm/vgic/vgic.c +20 -5
@@ -399,12 +399,18 @@ retry:static inline void vgic_process_maintenance_interrupt (struct kvm_vcpu *vcpu) { -vgic_v2_process_maintenance (vcpu); +if (kvm_vgic_global_state.type ==VGIC_V2 ) +vgic_v2_process_maintenance (vcpu); +else +vgic_v3_process_maintenance (vcpu); }static inline void vgic_fold_lr_state (struct kvm_vcpu *vcpu) { -vgic_v2_fold_lr_state (vcpu); +if (kvm_vgic_global_state.type ==VGIC_V2 ) +vgic_v2_fold_lr_state (vcpu); +else +vgic_v3_fold_lr_state (vcpu); }/* Requires the irq_lock to be held. */
@@ -413,17 +419,26 @@ static inline void vgic_populate_lr(struct kvm_vcpu *vcpu, {DEBUG_SPINLOCK_BUG_ON (!spin_is_locked (&irq->irq_lock)); -vgic_v2_populate_lr (vcpu, irq, lr); +if (kvm_vgic_global_state.type ==VGIC_V2 ) +vgic_v2_populate_lr (vcpu, irq, lr); +else +vgic_v3_populate_lr (vcpu, irq, lr); }static inline void vgic_clear_lr (struct kvm_vcpu *vcpu,int lr) { -vgic_v2_clear_lr (vcpu, lr); +if (kvm_vgic_global_state.type ==VGIC_V2 ) +vgic_v2_clear_lr (vcpu, lr); +else +vgic_v3_clear_lr (vcpu, lr); }static inline void vgic_set_underflow (struct kvm_vcpu *vcpu) { -vgic_v2_set_underflow (vcpu); +if (kvm_vgic_global_state.type ==VGIC_V2 ) +vgic_v2_set_underflow (vcpu); +else +vgic_v3_set_underflow (vcpu); }/* Requires the ap_list_lock to be held. */
virt/kvm/arm/vgic/vgic.h +29 -0
@@ -28,4 +28,33 @@ void vgic_v2_populate_lr(struct kvm_vcpu *vcpu, struct vgic_irq *irq, int lr);void vgic_v2_clear_lr (struct kvm_vcpu *vcpu,int lr);void vgic_v2_set_underflow (struct kvm_vcpu *vcpu); +#ifdef CONFIG_KVM_ARM_VGIC_V3 +void vgic_v3_process_maintenance (struct kvm_vcpu *vcpu); +void vgic_v3_fold_lr_state (struct kvm_vcpu *vcpu); +void vgic_v3_populate_lr (struct kvm_vcpu *vcpu,struct vgic_irq *irq,int lr); +void vgic_v3_clear_lr (struct kvm_vcpu *vcpu,int lr); +void vgic_v3_set_underflow (struct kvm_vcpu *vcpu); +#else +static inline void vgic_v3_process_maintenance (struct kvm_vcpu *vcpu) +{ +} + +static inline void vgic_v3_fold_lr_state (struct kvm_vcpu *vcpu) +{ +} + +static inline void vgic_v3_populate_lr (struct kvm_vcpu *vcpu, +struct vgic_irq *irq,int lr) +{ +} + +static inline void vgic_v3_clear_lr (struct kvm_vcpu *vcpu,int lr) +{ +} + +static inline void vgic_v3_set_underflow (struct kvm_vcpu *vcpu) +{ +} +#endif +#endif
Signature · 1 line
2.8.2
PATCH v4 24/56 KVM: arm/arm64: vgic-new: Add CTLR, TYPER and IIDR handlers
In reply to Andre PrzywaraFrom: Marc Zyngier <[email protected]>
Those three registers are v2 emulation specific, so their implementation lives entirely in vgic-mmio-v2.c. Also they are handled in one function, as their implementation is pretty simple.
When the guest enables the distributor, we kick all VCPUs to get potentially pending interrupts serviced.
---
Changelog RFC..v1:
- kick VCPUs is the distributor gets enabled
- improve comment
Changelog v1 .. v2:
- adapt to new MMIO framework
- use switch() statements to improve readability
Changelog v2 .. v3:
- add vgic_kick_vcpus() implementation
Changelog v3 .. v4;
- specify accessor width
- simplify CTLR write access
- replace extract_bytes() with simple return
include/linux/irqchip/arm-gic.h | 1 + virt/kvm/arm/vgic/vgic-mmio-v2.c | 46 +++++++++++++++++++++++++++++++++++++++- virt/kvm/arm/vgic/vgic.c | 15 +++++++++++++ virt/kvm/arm/vgic/vgic.h | 4 ++++ 4 files changed, 65 insertions(+), 1 deletion(-)
include/linux/irqchip/arm-gic.h +1 -0
@@ -33,6 +33,7 @@#define GIC_DIST_CTRL 0x000#define GIC_DIST_CTR 0x004 +#define GIC_DIST_IIDR 0x008#define GIC_DIST_IGROUP 0x080#define GIC_DIST_ENABLE_SET 0x100#define GIC_DIST_ENABLE_CLEAR 0x180
virt/kvm/arm/vgic/vgic-mmio-v2.c +45 -1
54 lines not on this page · raw message
virt/kvm/arm/vgic/vgic.c +15 -0
@@ -544,3 +544,18 @@ int kvm_vgic_vcpu_pending_irq(struct kvm_vcpu *vcpu)return pending; } + +void vgic_kick_vcpus (struct kvm *kvm) +{ +struct kvm_vcpu *vcpu; +int c; + +/* +* We've injected an interrupt, time to find out who deserves +* a good kick... +*/ +kvm_for_each_vcpu (c, vcpu, kvm) { +if (kvm_vgic_vcpu_pending_irq (vcpu)) +kvm_vcpu_kick (vcpu); + } +}
virt/kvm/arm/vgic/vgic.h +4 -0
@@ -16,11 +16,15 @@#ifndef __KVM_ARM_VGIC_NEW_H__#define __KVM_ARM_VGIC_NEW_H__ +#define PRODUCT_ID_KVM 0x4b/* ASCII code K */ +#define IMPLEMENTER_ARM 0x43b +#define vgic_irq_is_sgi (intid) ((intid) < VGIC_NR_SGIS)struct vgic_irq *vgic_get_irq (struct kvm *kvm,struct kvm_vcpu *vcpu,u32 intid);bool vgic_queue_irq_unlock (struct kvm *kvm,struct vgic_irq *irq); +void vgic_kick_vcpus (struct kvm *kvm);void vgic_v2_process_maintenance (struct kvm_vcpu *vcpu);void vgic_v2_fold_lr_state (struct kvm_vcpu *vcpu);
Signature · 1 line
2.8.2
PATCH v4 25/56 KVM: arm/arm64: vgic-new: Add ENABLE registers handlers
In reply to Andre PrzywaraAs the enable register handlers are shared between the v2 and v3 emulation, their implementation goes into vgic-mmio.c, to be easily referenced from the v3 emulation as well later.
---
Changelog RFC..v1:
- use lower bits of address to determine IRQ number
- remove TODO, confirmed to be fine
Changelog v1 .. v2:
- adapt to new MMIO framework
Changelog v3 .. v4:
- specify accessor width
- use IRQ number accessor macro
- replace extract_bytes() with simple return
virt/kvm/arm/vgic/vgic-mmio-v2.c | 4 +-- virt/kvm/arm/vgic/vgic-mmio.c | 56 ++++++++++++++++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic-mmio.h | 11 ++++++++ 3 files changed, 69 insertions(+), 2 deletions(-)
virt/kvm/arm/vgic/vgic-mmio-v2.c +2 -2
@@ -72,10 +72,10 @@ static const struct vgic_register_region vgic_v2_dist_registers[] = { vgic_mmio_read_rao, vgic_mmio_write_wi, 1, VGIC_ACCESS_32bit),REGISTER_DESC_WITH_BITS_PER_IRQ (GIC_DIST_ENABLE_SET , - vgic_mmio_read_raz, vgic_mmio_write_wi,1 , + vgic_mmio_read_enable, vgic_mmio_write_senable,1 , VGIC_ACCESS_32bit),REGISTER_DESC_WITH_BITS_PER_IRQ (GIC_DIST_ENABLE_CLEAR , - vgic_mmio_read_raz, vgic_mmio_write_wi,1 , + vgic_mmio_read_enable, vgic_mmio_write_cenable,1 , VGIC_ACCESS_32bit), REGISTER_DESC_WITH_BITS_PER_IRQ(GIC_DIST_PENDING_SET, vgic_mmio_read_raz, vgic_mmio_write_wi, 1,
virt/kvm/arm/vgic/vgic-mmio.c +56 -0
62 lines not on this page · raw message
virt/kvm/arm/vgic/vgic-mmio.h +11 -0
@@ -84,6 +84,17 @@ unsigned long vgic_mmio_read_rao(struct kvm_vcpu *vcpu,void vgic_mmio_write_wi (struct kvm_vcpu *vcpu,gpa_t addr,unsigned int len,unsigned long val); +unsigned long vgic_mmio_read_enable (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len); + +void vgic_mmio_write_senable (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len, +unsigned long val); + +void vgic_mmio_write_cenable (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len, +unsigned long val); +unsigned int vgic_v2_init_dist_iodev (struct vgic_io_device *dev);#endif
Signature · 1 line
2.8.2
PATCH v4 27/56 KVM: arm/arm64: vgic-new: Add ACTIVE registers handlers
In reply to Andre PrzywaraThe active register handlers are shared between the v2 and v3 emulation, so their implementation goes into vgic-mmio.c, to be easily referenced from the v3 emulation as well later. Since activation/deactivation of an interrupt may happen entirely in the guest without it ever exiting, we need some extra logic to properly track the active state.
Putting it on an ap_list on activation is similar to the normal case handled by vgic_queue_irq_unlock(), but differs in some details that make a separate implementation worthwhile.
---
Changelog RFC..v1:
- handling queueing in write handler
- remove IRQ lock from read handler
Changelog v1 .. v2:
- adapt to new MMIO framework
Changelog v3 .. v4:
- specify accessor width
- use IRQ number accessor macro
- drop elaborate write_sactive handler and use new vgic_queue_irq_unlock()
- properly emulate clear active by halting the guest (Christoffer)
- replace extract_bytes() with simple return
virt/kvm/arm/vgic/vgic-mmio-v2.c | 4 +- virt/kvm/arm/vgic/vgic-mmio.c | 82 ++++++++++++++++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic-mmio.h | 10 +++++ 3 files changed, 94 insertions(+), 2 deletions(-)
virt/kvm/arm/vgic/vgic-mmio-v2.c +2 -2
@@ -84,10 +84,10 @@ static const struct vgic_register_region vgic_v2_dist_registers[] = { vgic_mmio_read_pending, vgic_mmio_write_cpending, 1, VGIC_ACCESS_32bit),REGISTER_DESC_WITH_BITS_PER_IRQ (GIC_DIST_ACTIVE_SET , - vgic_mmio_read_raz, vgic_mmio_write_wi,1 , + vgic_mmio_read_active, vgic_mmio_write_sactive,1 , VGIC_ACCESS_32bit),REGISTER_DESC_WITH_BITS_PER_IRQ (GIC_DIST_ACTIVE_CLEAR , - vgic_mmio_read_raz, vgic_mmio_write_wi,1 , + vgic_mmio_read_active, vgic_mmio_write_cactive,1 , VGIC_ACCESS_32bit), REGISTER_DESC_WITH_BITS_PER_IRQ(GIC_DIST_PRI, vgic_mmio_read_raz, vgic_mmio_write_wi, 8,
virt/kvm/arm/vgic/vgic-mmio.c +82 -0
88 lines not on this page · raw message
virt/kvm/arm/vgic/vgic-mmio.h +10 -0
@@ -106,6 +106,16 @@ void vgic_mmio_write_cpending(struct kvm_vcpu *vcpu, gpa_t addr, unsigned int len, unsigned long val); +unsigned long vgic_mmio_read_active (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len); + +void vgic_mmio_write_cactive (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len, +unsigned long val); + +void vgic_mmio_write_sactive (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len, +unsigned long val);unsigned int vgic_v2_init_dist_iodev (struct vgic_io_device *dev);
Signature · 1 line
2.8.2
PATCH v4 28/56 KVM: arm/arm64: vgic-new: Add PRIORITY registers handlers
In reply to Andre PrzywaraThe priority register handlers are shared between the v2 and v3 emulation, so their implementation goes into vgic-mmio.c, to be easily referenced from the v3 emulation as well later. There is a corner case when we change the priority of a pending interrupt which we don't handle at the moment.
---
Changelog v1 .. v2:
- adapt to new MMIO framework
Changelog v3 .. v4:
- specify accessor width
- use IRQ number accessor macro
virt/kvm/arm/vgic/vgic-mmio-v2.c | 2 +- virt/kvm/arm/vgic/vgic-mmio.c | 40 ++++++++++++++++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic-mmio.h | 7 +++++++ virt/kvm/arm/vgic/vgic.h | 2 ++ 4 files changed, 50 insertions(+), 1 deletion(-)
virt/kvm/arm/vgic/vgic-mmio-v2.c +1 -1
@@ -90,7 +90,7 @@ static const struct vgic_register_region vgic_v2_dist_registers[] = { vgic_mmio_read_active, vgic_mmio_write_cactive, 1, VGIC_ACCESS_32bit),REGISTER_DESC_WITH_BITS_PER_IRQ (GIC_DIST_PRI , - vgic_mmio_read_raz, vgic_mmio_write_wi,8 , + vgic_mmio_read_priority, vgic_mmio_write_priority,8 , VGIC_ACCESS_32bit | VGIC_ACCESS_8bit), REGISTER_DESC_WITH_BITS_PER_IRQ(GIC_DIST_TARGET, vgic_mmio_read_raz, vgic_mmio_write_wi, 8,
virt/kvm/arm/vgic/vgic-mmio.c +40 -0
@@ -237,6 +237,46 @@ void vgic_mmio_write_sactive(struct kvm_vcpu *vcpu, } } +unsigned long vgic_mmio_read_priority (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len) +{ +u32 intid =VGIC_ADDR_TO_INTID (addr,8 ); +int i; +u64 val =0 ; + +for (i =0 ; i < len; i++) { +struct vgic_irq *irq =vgic_get_irq (vcpu->kvm, vcpu, intid + i); + + val |= (u64 )irq->priority << (i *8 ); + } + +return val; +} + +/* +* We currently don't handle changing the priority of an interrupt that +* is already pending on a VCPU. If there is a need for this, we would +* need to make this VCPU exit and re-evaluate the priorities, potentially +* leading to this interrupt getting presented now to the guest (if it has +* been masked by the priority mask before). +*/ +void vgic_mmio_write_priority (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len, +unsigned long val) +{ +u32 intid =VGIC_ADDR_TO_INTID (addr,8 ); +int i; + +for (i =0 ; i < len; i++) { +struct vgic_irq *irq =vgic_get_irq (vcpu->kvm, vcpu, intid + i); + +spin_lock (&irq->irq_lock); +/* Narrow the priority range to what we actually support */ + irq->priority = (val >> (i *8 )) &GENMASK (7 ,8 -VGIC_PRI_BITS ); +spin_unlock (&irq->irq_lock); + } +} +static int match_region (const void *key,const void *elt) {const unsigned int offset = (unsigned long )key;
virt/kvm/arm/vgic/vgic-mmio.h +7 -0
@@ -117,6 +117,13 @@ void vgic_mmio_write_sactive(struct kvm_vcpu *vcpu, gpa_t addr, unsigned int len, unsigned long val); +unsigned long vgic_mmio_read_priority (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len); + +void vgic_mmio_write_priority (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len, +unsigned long val); +unsigned int vgic_v2_init_dist_iodev (struct vgic_io_device *dev);#endif
virt/kvm/arm/vgic/vgic.h +2 -0
@@ -19,6 +19,8 @@#define PRODUCT_ID_KVM 0x4b/* ASCII code K */ #define IMPLEMENTER_ARM 0x43b +#define VGIC_PRI_BITS 5 +#define vgic_irq_is_sgi (intid) ((intid) < VGIC_NR_SGIS) struct vgic_irq *vgic_get_irq(struct kvm *kvm, struct kvm_vcpu *vcpu,
Signature · 1 line
2.8.2
PATCH v4 29/56 KVM: arm/arm64: vgic-new: Add CONFIG registers handlers
In reply to Andre PrzywaraThe config register handlers are shared between the v2 and v3 emulation, so their implementation goes into vgic-mmio.c, to be easily referenced from the v3 emulation as well later.
---
Changelog v1 .. v2:
- adapt to new MMIO framework
Changelog v3 .. v4:
- specify accessor width
- use IRQ number accessor macro
- replace extract_bytes() with simple return
virt/kvm/arm/vgic/vgic-mmio-v2.c | 2 +- virt/kvm/arm/vgic/vgic-mmio.c | 46 ++++++++++++++++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic-mmio.h | 7 ++++++ 3 files changed, 54 insertions(+), 1 deletion(-)
virt/kvm/arm/vgic/vgic-mmio-v2.c +1 -1
@@ -96,7 +96,7 @@ static const struct vgic_register_region vgic_v2_dist_registers[] = { vgic_mmio_read_raz, vgic_mmio_write_wi, 8, VGIC_ACCESS_32bit | VGIC_ACCESS_8bit),REGISTER_DESC_WITH_BITS_PER_IRQ (GIC_DIST_CONFIG , - vgic_mmio_read_raz, vgic_mmio_write_wi,2 , + vgic_mmio_read_config, vgic_mmio_write_config,2 , VGIC_ACCESS_32bit), REGISTER_DESC_WITH_LENGTH(GIC_DIST_SOFTINT, vgic_mmio_read_raz, vgic_mmio_write_wi, 4,
virt/kvm/arm/vgic/vgic-mmio.c +46 -0
52 lines not on this page · raw message
virt/kvm/arm/vgic/vgic-mmio.h +7 -0
@@ -124,6 +124,13 @@ void vgic_mmio_write_priority(struct kvm_vcpu *vcpu, gpa_t addr, unsigned int len, unsigned long val); +unsigned long vgic_mmio_read_config (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len); + +void vgic_mmio_write_config (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len, +unsigned long val); +unsigned int vgic_v2_init_dist_iodev (struct vgic_io_device *dev);#endif
Signature · 1 line
2.8.2
PATCH v4 30/56 KVM: arm/arm64: vgic-new: Add TARGET registers handlers
In reply to Andre PrzywaraThe target register handlers are v2 emulation specific, so their implementation lives entirely in vgic-mmio-v2.c.
We copy the old VGIC behaviour of assigning an IRQ to the first VCPU set in the target mask instead of making it possibly pending on multiple VCPUs.
---
Changelog RFC..v1:
- remove runtime VCPU determination from this v2-only register
- fold in implementation of vgic_v2_irq_change_affinity()
- replace ffs() with __ffs()
Changelog v1 .. v2:
- adapt to new MMIO framework
Changelog v3 .. v4:
- specify accessor width
- use IRQ number accessor macro
virt/kvm/arm/vgic/vgic-mmio-v2.c | 43 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-)
virt/kvm/arm/vgic/vgic-mmio-v2.c +42 -1
@@ -64,6 +64,47 @@ static void vgic_mmio_write_v2_misc(struct kvm_vcpu *vcpu, } } +static unsigned long vgic_mmio_read_target (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len) +{ +u32 intid =VGIC_ADDR_TO_INTID (addr,8 ); +int i; +u64 val =0 ; + +for (i =0 ; i < len; i++) { +struct vgic_irq *irq =vgic_get_irq (vcpu->kvm, vcpu, intid + i); + + val |= (u64 )irq->targets << (i *8 ); + } + +return val; +} + +static void vgic_mmio_write_target (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len, +unsigned long val) +{ +u32 intid =VGIC_ADDR_TO_INTID (addr,8 ); +int i; + +/* GICD_ITARGETSR[0-7] are read-only */ +if (intid <VGIC_NR_PRIVATE_IRQS ) +return ; + +for (i =0 ; i < len; i++) { +struct vgic_irq *irq =vgic_get_irq (vcpu->kvm,NULL , intid + i); +int target; + +spin_lock (&irq->irq_lock); + + irq->targets = (val >> (i *8 )) &0xff ; + target = irq->targets ?__ffs (irq->targets) :0 ; + irq->target_vcpu =kvm_get_vcpu (vcpu->kvm, target); + +spin_unlock (&irq->irq_lock); + } +} +static const struct vgic_register_region vgic_v2_dist_registers[] = { REGISTER_DESC_WITH_LENGTH(GIC_DIST_CTRL, vgic_mmio_read_v2_misc, vgic_mmio_write_v2_misc, 12,
@@ -93,7 +134,7 @@ static const struct vgic_register_region vgic_v2_dist_registers[] = { vgic_mmio_read_priority, vgic_mmio_write_priority, 8, VGIC_ACCESS_32bit | VGIC_ACCESS_8bit),REGISTER_DESC_WITH_BITS_PER_IRQ (GIC_DIST_TARGET , - vgic_mmio_read_raz, vgic_mmio_write_wi,8 , + vgic_mmio_read_target, vgic_mmio_write_target,8 , VGIC_ACCESS_32bit | VGIC_ACCESS_8bit), REGISTER_DESC_WITH_BITS_PER_IRQ(GIC_DIST_CONFIG, vgic_mmio_read_config, vgic_mmio_write_config, 2,
Signature · 1 line
2.8.2
PATCH v4 31/56 KVM: arm/arm64: vgic-new: Add SGIR register handler
In reply to Andre PrzywaraTriggering an IPI via this register is v2 specific, so the implementation lives entirely in vgic-mmio-v2.c.
---
Changelog RFC..v1:
- silently return on illegal TargetListFilter value (=3)
Changelog v1 .. v2:
- adapt to new MMIO framework
Changelog v3 .. v4:
- specify accessor width
virt/kvm/arm/vgic/vgic-mmio-v2.c | 43 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-)
virt/kvm/arm/vgic/vgic-mmio-v2.c +42 -1
@@ -64,6 +64,47 @@ static void vgic_mmio_write_v2_misc(struct kvm_vcpu *vcpu, } } +static void vgic_mmio_write_sgir (struct kvm_vcpu *source_vcpu, +gpa_t addr,unsigned int len, +unsigned long val) +{ +int nr_vcpus =atomic_read (&source_vcpu->kvm->online_vcpus); +int intid = val &0xf ; +int targets = (val >>16 ) &0xff ; +int mode = (val >>24 ) &0x03 ; +int c; +struct kvm_vcpu *vcpu; + +switch (mode) { +case 0x0 :/* as specified by targets */ +break ; +case 0x1 : + targets = (1U << nr_vcpus) -1 ;/* all, ... */ + targets &= ~(1U << source_vcpu->vcpu_id);/* but self */ +break ; +case 0x2 :/* this very vCPU only */ + targets = (1U << source_vcpu->vcpu_id); +break ; +case 0x3 :/* reserved */ +return ; + } + +kvm_for_each_vcpu (c, vcpu, source_vcpu->kvm) { +struct vgic_irq *irq; + +if (!(targets & (1U << c))) +continue ; + + irq =vgic_get_irq (source_vcpu->kvm, vcpu, intid); + +spin_lock (&irq->irq_lock); + irq->pending = true; + irq->source |=1U << source_vcpu->vcpu_id; + +vgic_queue_irq_unlock (source_vcpu->kvm, irq); + } +} +static unsigned long vgic_mmio_read_target (struct kvm_vcpu *vcpu,gpa_t addr,unsigned int len) {
@@ -140,7 +181,7 @@ static const struct vgic_register_region vgic_v2_dist_registers[] = { vgic_mmio_read_config, vgic_mmio_write_config, 2, VGIC_ACCESS_32bit),REGISTER_DESC_WITH_LENGTH (GIC_DIST_SOFTINT , - vgic_mmio_read_raz, vgic_mmio_write_wi,4 , + vgic_mmio_read_raz, vgic_mmio_write_sgir,4 , VGIC_ACCESS_32bit), REGISTER_DESC_WITH_LENGTH(GIC_DIST_SGI_PENDING_CLEAR, vgic_mmio_read_raz, vgic_mmio_write_wi, 16,
Signature · 1 line
2.8.2
PATCH v4 32/56 KVM: arm/arm64: vgic-new: Add SGIPENDR register handlers
In reply to Andre PrzywaraAs this register is v2 specific, its implementation lives entirely in vgic-mmio-v2.c.
This register allows setting the source mask of an IPI.
---
Changelog RFC..v1:
- remove IRQ lock from read handler
- update pending bit on setting the first / clearing the last bit
- queue virtual IRQ if necessary
Changelog v1 .. v2:
- adapt to new MMIO framework
Changelog v3 .. v4:
- specify accessor width
virt/kvm/arm/vgic/vgic-mmio-v2.c | 62 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 60 insertions(+), 2 deletions(-)
virt/kvm/arm/vgic/vgic-mmio-v2.c +60 -2
@@ -146,6 +146,64 @@ static void vgic_mmio_write_target(struct kvm_vcpu *vcpu, } } +static unsigned long vgic_mmio_read_sgipend (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len) +{ +u32 intid = addr &0x0f ; +int i; +u64 val =0 ; + +for (i =0 ; i < len; i++) { +struct vgic_irq *irq =vgic_get_irq (vcpu->kvm, vcpu, intid + i); + + val |= (u64 )irq->source << (i *8 ); + } +return val; +} + +static void vgic_mmio_write_sgipendc (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len, +unsigned long val) +{ +u32 intid = addr &0x0f ; +int i; + +for (i =0 ; i < len; i++) { +struct vgic_irq *irq =vgic_get_irq (vcpu->kvm, vcpu, intid + i); + +spin_lock (&irq->irq_lock); + + irq->source &= ~((val >> (i *8 )) &0xff ); +if (!irq->source) + irq->pending = false; + +spin_unlock (&irq->irq_lock); + } +} + +static void vgic_mmio_write_sgipends (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len, +unsigned long val) +{ +u32 intid = addr &0x0f ; +int i; + +for (i =0 ; i < len; i++) { +struct vgic_irq *irq =vgic_get_irq (vcpu->kvm, vcpu, intid + i); + +spin_lock (&irq->irq_lock); + + irq->source |= (val >> (i *8 )) &0xff ; + +if (irq->source) { + irq->pending = true; +vgic_queue_irq_unlock (vcpu->kvm, irq); + }else { +spin_unlock (&irq->irq_lock); + } + } +} +static const struct vgic_register_region vgic_v2_dist_registers[] = { REGISTER_DESC_WITH_LENGTH(GIC_DIST_CTRL, vgic_mmio_read_v2_misc, vgic_mmio_write_v2_misc, 12,
@@ -184,10 +242,10 @@ static const struct vgic_register_region vgic_v2_dist_registers[] = { vgic_mmio_read_raz, vgic_mmio_write_sgir, 4, VGIC_ACCESS_32bit),REGISTER_DESC_WITH_LENGTH (GIC_DIST_SGI_PENDING_CLEAR , - vgic_mmio_read_raz, vgic_mmio_write_wi, 16, + vgic_mmio_read_sgipend, vgic_mmio_write_sgipendc, 16, VGIC_ACCESS_32bit | VGIC_ACCESS_8bit),REGISTER_DESC_WITH_LENGTH (GIC_DIST_SGI_PENDING_SET , - vgic_mmio_read_raz, vgic_mmio_write_wi, 16, + vgic_mmio_read_sgipend, vgic_mmio_write_sgipends, 16, VGIC_ACCESS_32bit | VGIC_ACCESS_8bit), };
Signature · 1 line
2.8.2
PATCH v4 33/56 KVM: arm/arm64: vgic-new: Add GICv3 MMIO handling framework
In reply to Andre PrzywaraCreate a new file called vgic-mmio-v3.c and describe the GICv3 distributor and redistributor registers there.
This adds a special macro to deal with the split of SGI/PPI in the redistributor and SPIs in the distributor, which allows us to reuse the existing GICv2 handlers for those registers which are compatible. Also we provide a function to deal with the registration of the two separate redistributor frames per VCPU.
---
Changelog RFC..v1:
- adapt to new MMIO registration approach:
register one device for the distributor and two for each VCPU
- implement special handling for private interrupts
- remove empty stub functions
- make IGROUPR return RAO
Changelog v1 .. v2:
- adapt to new framework, introduce vgic-mmio-v3.c
- remove userland register access functions (for now)
- precompute .len when describing a VGIC register
- add missed pointer incrementation on registering redist regions
- replace _nyi stub functions with raz/wi versions
Changelog v2 .. v3:
- replace inclusion of kvm/vgic/vgic.h with kvm/arm_vgic.h
- add prototype and stub code for vgic_register_redist_iodevs
- rename register struct variables _rdbase_ and _sgibase_
Changelog v3 .. v4:
- specify accessor width
virt/kvm/arm/vgic/vgic-mmio-v3.c | 224 +++++++++++++++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic-mmio.c | 5 + virt/kvm/arm/vgic/vgic-mmio.h | 2 + virt/kvm/arm/vgic/vgic.h | 7 ++ 4 files changed, 238 insertions(+) create mode 100644 virt/kvm/arm/vgic/vgic-mmio-v3.c
virt/kvm/arm/vgic/vgic-mmio-v3.c new file +224 -0
@@ -0,0 +1,224 @@ +/* +* VGICv3 MMIO handling functions +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +*/ + +#include <linux/irqchip/arm-gic-v3.h> +#include <linux/kvm.h> +#include <linux/kvm_host.h> +#include <kvm/iodev.h> +#include <kvm/arm_vgic.h> + +#include <asm/kvm_emulate.h> + +#include "vgic.h" +#include "vgic-mmio.h" + +/* +* The GICv3 per-IRQ registers are split to control PPIs and SGIs in the +* redistributors, while SPIs are covered by registers in the distributor +* block. Trying to set private IRQs in this block gets ignored. +* We take some special care here to fix the calculation of the register +* offset. +*/ +#define REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED (off, rd, wr, bpi, acc) \ + { \ + .reg_offset = off, \ + .bits_per_irq = bpi, \ + .len = (bpi * VGIC_NR_PRIVATE_IRQS) / 8, \ + .access_flags = acc, \ + .read = vgic_mmio_read_raz, \ + .write = vgic_mmio_write_wi, \ + }, { \ + .reg_offset = off + (bpi * VGIC_NR_PRIVATE_IRQS) / 8, \ + .bits_per_irq = bpi, \ + .len = (bpi * (1024 - VGIC_NR_PRIVATE_IRQS)) / 8, \ + .access_flags = acc, \ + .read = rd, \ + .write = wr, \ + } + +static const struct vgic_register_region vgic_v3_dist_registers[] = { +REGISTER_DESC_WITH_LENGTH (GICD_CTLR , + vgic_mmio_read_raz, vgic_mmio_write_wi,16 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED (GICD_IGROUPR , + vgic_mmio_read_rao, vgic_mmio_write_wi,1 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED (GICD_ISENABLER , + vgic_mmio_read_enable, vgic_mmio_write_senable,1 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED (GICD_ICENABLER , + vgic_mmio_read_enable, vgic_mmio_write_cenable,1 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED (GICD_ISPENDR , + vgic_mmio_read_pending, vgic_mmio_write_spending,1 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED (GICD_ICPENDR , + vgic_mmio_read_pending, vgic_mmio_write_cpending,1 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED (GICD_ISACTIVER , + vgic_mmio_read_active, vgic_mmio_write_sactive,1 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED (GICD_ICACTIVER , + vgic_mmio_read_active, vgic_mmio_write_cactive,1 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED (GICD_IPRIORITYR , + vgic_mmio_read_priority, vgic_mmio_write_priority,8 , + VGIC_ACCESS_32bit | VGIC_ACCESS_8bit), +REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED (GICD_ITARGETSR , + vgic_mmio_read_raz, vgic_mmio_write_wi,8 , + VGIC_ACCESS_32bit | VGIC_ACCESS_8bit), +REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED (GICD_ICFGR , + vgic_mmio_read_config, vgic_mmio_write_config,2 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED (GICD_IGRPMODR , + vgic_mmio_read_raz, vgic_mmio_write_wi,1 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED (GICD_IROUTER , + vgic_mmio_read_raz, vgic_mmio_write_wi,64 , + VGIC_ACCESS_64bit | VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_LENGTH (GICD_IDREGS , + vgic_mmio_read_raz, vgic_mmio_write_wi,48 , + VGIC_ACCESS_32bit), +}; + +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_ACCESS_32bit), +REGISTER_DESC_WITH_LENGTH (GICR_IIDR , + vgic_mmio_read_raz, vgic_mmio_write_wi,4 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_LENGTH (GICR_TYPER , + vgic_mmio_read_raz, vgic_mmio_write_wi,8 , + VGIC_ACCESS_64bit | VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_LENGTH (GICR_PROPBASER , + vgic_mmio_read_raz, vgic_mmio_write_wi,8 , + VGIC_ACCESS_64bit | VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_LENGTH (GICR_PENDBASER , + vgic_mmio_read_raz, vgic_mmio_write_wi,8 , + VGIC_ACCESS_64bit | VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_LENGTH (GICR_IDREGS , + vgic_mmio_read_raz, vgic_mmio_write_wi,48 , + VGIC_ACCESS_32bit), +}; + +static const struct vgic_register_region vgic_v3_sgibase_registers[] = { +REGISTER_DESC_WITH_LENGTH (GICR_IGROUPR0 , + vgic_mmio_read_rao, vgic_mmio_write_wi,4 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_LENGTH (GICR_ISENABLER0 , + vgic_mmio_read_enable, vgic_mmio_write_senable,4 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_LENGTH (GICR_ICENABLER0 , + vgic_mmio_read_enable, vgic_mmio_write_cenable,4 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_LENGTH (GICR_ISPENDR0 , + vgic_mmio_read_pending, vgic_mmio_write_spending,4 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_LENGTH (GICR_ICPENDR0 , + vgic_mmio_read_pending, vgic_mmio_write_cpending,4 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_LENGTH (GICR_ISACTIVER0 , + vgic_mmio_read_active, vgic_mmio_write_sactive,4 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_LENGTH (GICR_ICACTIVER0 , + vgic_mmio_read_active, vgic_mmio_write_cactive,4 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_LENGTH (GICR_IPRIORITYR0 , + vgic_mmio_read_priority, vgic_mmio_write_priority,32 , + VGIC_ACCESS_32bit | VGIC_ACCESS_8bit), +REGISTER_DESC_WITH_LENGTH (GICR_ICFGR0 , + vgic_mmio_read_config, vgic_mmio_write_config,8 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_LENGTH (GICR_IGRPMODR0 , + vgic_mmio_read_raz, vgic_mmio_write_wi,4 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_LENGTH (GICR_NSACR , + vgic_mmio_read_raz, vgic_mmio_write_wi,4 , + VGIC_ACCESS_32bit), +}; + +unsigned int vgic_v3_init_dist_iodev (struct vgic_io_device *dev) +{ + dev->regions = vgic_v3_dist_registers; + dev->nr_regions =ARRAY_SIZE (vgic_v3_dist_registers); + +kvm_iodevice_init (&dev->dev, &kvm_io_gic_ops); + +return SZ_64K ; +} + +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 ]; + +kvm_iodevice_init (&rd_dev->dev, &kvm_io_gic_ops); + rd_dev->base_addr = rd_base; + rd_dev->regions = vgic_v3_rdbase_registers; + rd_dev->nr_regions =ARRAY_SIZE (vgic_v3_rdbase_registers); + rd_dev->redist_vcpu = vcpu; + +mutex_lock (&kvm->slots_lock); + ret =kvm_io_bus_register_dev (kvm,KVM_MMIO_BUS , rd_base, +SZ_64K , &rd_dev->dev); +mutex_unlock (&kvm->slots_lock); + +if (ret) +break ; + +kvm_iodevice_init (&sgi_dev->dev, &kvm_io_gic_ops); + sgi_dev->base_addr = sgi_base; + sgi_dev->regions = vgic_v3_sgibase_registers; + sgi_dev->nr_regions =ARRAY_SIZE (vgic_v3_sgibase_registers); + sgi_dev->redist_vcpu = vcpu; + +mutex_lock (&kvm->slots_lock); + ret =kvm_io_bus_register_dev (kvm,KVM_MMIO_BUS , sgi_base, +SZ_64K , &sgi_dev->dev); +mutex_unlock (&kvm->slots_lock); +if (ret) { +kvm_io_bus_unregister_dev (kvm,KVM_MMIO_BUS , + &devices[c *2 ].dev); +break ; + } + } + +if (ret) { +/* The current c failed, so we start with the previous one. */ +for (c--; c >=0 ; c--) { +kvm_io_bus_unregister_dev (kvm,KVM_MMIO_BUS , + &devices[c *2 ].dev); +kvm_io_bus_unregister_dev (kvm,KVM_MMIO_BUS , + &devices[c *2 +1 ].dev); + } +kfree (devices); + }else { + kvm->arch.vgic.redist_iodevs = devices; + } + +return ret; +}
virt/kvm/arm/vgic/vgic-mmio.c +5 -0
@@ -478,6 +478,11 @@ int vgic_register_dist_iodev(struct kvm *kvm, gpa_t dist_base_address,case VGIC_V2 : len =vgic_v2_init_dist_iodev (io_device);break ; +#ifdef CONFIG_KVM_ARM_VGIC_V3 +case VGIC_V3 : + len =vgic_v3_init_dist_iodev (io_device); +break ; +#endif default :BUG_ON (1 ); }
virt/kvm/arm/vgic/vgic-mmio.h +2 -0
@@ -133,4 +133,6 @@ void vgic_mmio_write_config(struct kvm_vcpu *vcpu,unsigned int vgic_v2_init_dist_iodev (struct vgic_io_device *dev); +unsigned int vgic_v3_init_dist_iodev (struct vgic_io_device *dev); +#endif
virt/kvm/arm/vgic/vgic.h +7 -0
@@ -42,6 +42,7 @@ void vgic_v3_fold_lr_state(struct kvm_vcpu *vcpu);void vgic_v3_populate_lr (struct kvm_vcpu *vcpu,struct vgic_irq *irq,int lr);void vgic_v3_clear_lr (struct kvm_vcpu *vcpu,int lr);void vgic_v3_set_underflow (struct kvm_vcpu *vcpu); +int vgic_register_redist_iodevs (struct kvm *kvm,gpa_t dist_base_address);#else static inline void vgic_v3_process_maintenance (struct kvm_vcpu *vcpu) {
@@ -63,6 +64,12 @@ static inline void vgic_v3_clear_lr(struct kvm_vcpu *vcpu, int lr)static inline void vgic_v3_set_underflow (struct kvm_vcpu *vcpu) { } + +static inline int vgic_register_redist_iodevs (struct kvm *kvm, +gpa_t dist_base_address) +{ +return -ENODEV ; +}#endif #endif
Signature · 1 line
2.8.2
PATCH v4 34/56 KVM: arm/arm64: vgic-new: Add GICv3 CTLR, IIDR, TYPER handlers
In reply to Andre PrzywaraAs in the GICv2 emulation we handle those three registers in one function.
---
Changelog RFC..v1:
- kick VCPUs if distributor gets enabled
Changelog v1 .. v2:
- rewrite write handler to use switch statement
Changelog v3 .. v4:
- specify accessor width
virt/kvm/arm/vgic/vgic-mmio-v3.c | 48 +++++++++++++++++++++++++++++++++++++++- virt/kvm/arm/vgic/vgic.h | 1 + 2 files changed, 48 insertions(+), 1 deletion(-)
virt/kvm/arm/vgic/vgic-mmio-v3.c +47 -1
@@ -22,6 +22,52 @@#include "vgic.h" #include "vgic-mmio.h" +static unsigned long vgic_mmio_read_v3_misc (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len) +{ +u32 value =0 ; + +switch (addr &0x0c ) { +case GICD_CTLR : +if (vcpu->kvm->arch.vgic.enabled) + value |=GICD_CTLR_ENABLE_SS_G1 ; + value |=GICD_CTLR_ARE_NS |GICD_CTLR_DS ; +break ; +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 ; +break ; +case GICD_IIDR : + value = (PRODUCT_ID_KVM <<24 ) | (IMPLEMENTER_ARM <<0 ); +break ; +default : +return 0 ; + } + +return value; +} + +static void vgic_mmio_write_v3_misc (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len, +unsigned long val) +{ +struct vgic_dist *dist = &vcpu->kvm->arch.vgic; +bool was_enabled = dist->enabled; + +switch (addr &0x0c ) { +case GICD_CTLR : + dist->enabled = val &GICD_CTLR_ENABLE_SS_G1 ; + +if (!was_enabled && dist->enabled) +vgic_kick_vcpus (vcpu->kvm); +break ; +case GICD_TYPER : +case GICD_IIDR : +return ; + } +} +/* * The GICv3 per-IRQ registers are split to control PPIs and SGIs in the * redistributors, while SPIs are covered by registers in the distributor
@@ -48,7 +94,7 @@static const struct vgic_register_region vgic_v3_dist_registers[] = {REGISTER_DESC_WITH_LENGTH (GICD_CTLR , - vgic_mmio_read_raz, vgic_mmio_write_wi,16 , + vgic_mmio_read_v3_misc, vgic_mmio_write_v3_misc,16 , VGIC_ACCESS_32bit), REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED(GICD_IGROUPR, vgic_mmio_read_rao, vgic_mmio_write_wi, 1,
virt/kvm/arm/vgic/vgic.h +1 -0
@@ -19,6 +19,7 @@#define PRODUCT_ID_KVM 0x4b/* ASCII code K */ #define IMPLEMENTER_ARM 0x43b +#define INTERRUPT_ID_BITS_SPIS 10#define VGIC_PRI_BITS 5#define vgic_irq_is_sgi (intid) ((intid) < VGIC_NR_SGIS)
Signature · 1 line
2.8.2
PATCH v4 35/56 KVM: arm/arm64: vgic-new: Add GICv3 redistributor IIDR and TYPER handler
In reply to Andre PrzywaraThe redistributor TYPER tells the OS about the associated MPIDR, also the LAST bit is crucial to determine the number of redistributors.
---
Changelog v1 .. v2:
- adapt to new MMIO framework
Changelog v3 .. v4:
- specify accessor width
- add extract_bytes() as static (moved from an earlier patch)
- replace one extract_bytes() access with a simple return
virt/kvm/arm/vgic/vgic-mmio-v3.c | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-)
virt/kvm/arm/vgic/vgic-mmio-v3.c +30 -2
@@ -22,6 +22,13 @@#include "vgic.h" #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) +{ +return (data >> (offset *8 )) &GENMASK_ULL (num *8 -1 ,0 ); +} +static unsigned long vgic_mmio_read_v3_misc (struct kvm_vcpu *vcpu,gpa_t addr,unsigned int len) {
@@ -68,6 +75,27 @@ static void vgic_mmio_write_v3_misc(struct kvm_vcpu *vcpu, } } +static unsigned long vgic_mmio_read_v3r_typer (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len) +{ +unsigned long mpidr =kvm_vcpu_get_mpidr_aff (vcpu); +int target_vcpu_id = vcpu->vcpu_id; +u64 value; + + value = (mpidr &GENMASK (23 ,0 )) <<32 ; + value |= ((target_vcpu_id &0xffff ) <<8 ); +if (target_vcpu_id ==atomic_read (&vcpu->kvm->online_vcpus) -1 ) + value |=GICR_TYPER_LAST ; + +return extract_bytes (value, addr &7 , len); +} + +static unsigned long vgic_mmio_read_v3r_iidr (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len) +{ +return (PRODUCT_ID_KVM <<24 ) | (IMPLEMENTER_ARM <<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
@@ -142,10 +170,10 @@ static const struct vgic_register_region vgic_v3_rdbase_registers[] = { vgic_mmio_read_raz, vgic_mmio_write_wi, 4, VGIC_ACCESS_32bit),REGISTER_DESC_WITH_LENGTH (GICR_IIDR , - vgic_mmio_read_raz, vgic_mmio_write_wi,4 , + vgic_mmio_read_v3r_iidr, vgic_mmio_write_wi,4 , VGIC_ACCESS_32bit),REGISTER_DESC_WITH_LENGTH (GICR_TYPER , - vgic_mmio_read_raz, vgic_mmio_write_wi,8 , + vgic_mmio_read_v3r_typer, vgic_mmio_write_wi,8 , VGIC_ACCESS_64bit | VGIC_ACCESS_32bit), REGISTER_DESC_WITH_LENGTH(GICR_PROPBASER, vgic_mmio_read_raz, vgic_mmio_write_wi, 8,
Signature · 1 line
2.8.2
PATCH v4 36/56 KVM: arm/arm64: vgic-new: Add GICv3 IDREGS register handler
In reply to Andre PrzywaraWe implement the only one ID register that is required by the architecture, also this is the one that Linux actually checks.
---
Changelog v1 .. v2:
- adapt to new MMIO framework
Changelog v3 .. v4:
- specify accessor width
virt/kvm/arm/vgic/vgic-mmio-v3.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-)
virt/kvm/arm/vgic/vgic-mmio-v3.c +14 -2
@@ -96,6 +96,18 @@ static unsigned long vgic_mmio_read_v3r_iidr(struct kvm_vcpu *vcpu,return (PRODUCT_ID_KVM <<24 ) | (IMPLEMENTER_ARM <<0 ); } +static unsigned long vgic_mmio_read_v3_idregs (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len) +{ +switch (addr &0xffff ) { +case GICD_PIDR2 : +/* report a GICv3 compliant implementation */ +return 0x3b ; + } + +return 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
@@ -161,7 +173,7 @@ static const struct vgic_register_region vgic_v3_dist_registers[] = { vgic_mmio_read_raz, vgic_mmio_write_wi, 64, VGIC_ACCESS_64bit | VGIC_ACCESS_32bit), REGISTER_DESC_WITH_LENGTH(GICD_IDREGS, - vgic_mmio_read_raz, vgic_mmio_write_wi, 48, + vgic_mmio_read_v3_idregs, vgic_mmio_write_wi, 48, VGIC_ACCESS_32bit), };
@@ -182,7 +194,7 @@ static const struct vgic_register_region vgic_v3_rdbase_registers[] = { vgic_mmio_read_raz, vgic_mmio_write_wi, 8, VGIC_ACCESS_64bit | VGIC_ACCESS_32bit), REGISTER_DESC_WITH_LENGTH(GICR_IDREGS, - vgic_mmio_read_raz, vgic_mmio_write_wi, 48, + vgic_mmio_read_v3_idregs, vgic_mmio_write_wi, 48, VGIC_ACCESS_32bit), };
Signature · 1 line
2.8.2
PATCH v4 37/56 KVM: arm/arm64: vgic-new: Add GICv3 IROUTER register handlers
In reply to Andre PrzywaraSince GICv3 supports much more than the 8 CPUs the GICv2 ITARGETSR register can handle, the new IROUTER register covers the whole range of possible target (V)CPUs by using the same MPIDR that the cores report themselves.
In addition to translating this MPIDR into a vcpu pointer we store the originally written value as well. The architecture allows to write any values into the register, which must be read back as written.
Since we don't support affinity level 3, we don't need to take care about the upper word of this 64-bit register, which simplifies the handling a bit.
---
Changelog RFC..v1:
- fold in and simplify vgic_v3_irq_change_affinity
Changelog v1 .. v2:
- adapt to new MMIO framework
Changelog v3 .. v4:
- move accessor width check into dispatcher
- use IRQ number accessor macro
virt/kvm/arm/vgic/vgic-mmio-v3.c | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-)
virt/kvm/arm/vgic/vgic-mmio-v3.c +36 -1
@@ -75,6 +75,41 @@ static void vgic_mmio_write_v3_misc(struct kvm_vcpu *vcpu, } } +static unsigned long vgic_mmio_read_irouter (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len) +{ +int intid =VGIC_ADDR_TO_INTID (addr,64 ); +struct vgic_irq *irq =vgic_get_irq (vcpu->kvm,NULL , intid); + +if (!irq) +return 0 ; + +return extract_bytes (READ_ONCE (irq->mpidr), addr &7 , len); +} + +static void vgic_mmio_write_irouter (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len, +unsigned long val) +{ +int intid =VGIC_ADDR_TO_INTID (addr,64 ); +struct vgic_irq *irq =vgic_get_irq (vcpu->kvm,NULL , intid); + +if (!irq) +return ; + +/* The upper word is WI for us since we don't implement Aff3. */ +if (addr &4 ) +return ; + +spin_lock (&irq->irq_lock); + +/* We only care about and preserve Aff0, Aff1 and Aff2. */ + irq->mpidr = val &GENMASK (23 ,0 ); + irq->target_vcpu =kvm_mpidr_to_vcpu (vcpu->kvm, irq->mpidr); + +spin_unlock (&irq->irq_lock); +} +static unsigned long vgic_mmio_read_v3r_typer (struct kvm_vcpu *vcpu,gpa_t addr,unsigned int len) {
@@ -170,7 +205,7 @@ static const struct vgic_register_region vgic_v3_dist_registers[] = { vgic_mmio_read_raz, vgic_mmio_write_wi, 1, VGIC_ACCESS_32bit),REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED (GICD_IROUTER , - vgic_mmio_read_raz, vgic_mmio_write_wi,64 , + vgic_mmio_read_irouter, vgic_mmio_write_irouter,64 , VGIC_ACCESS_64bit | VGIC_ACCESS_32bit), REGISTER_DESC_WITH_LENGTH(GICD_IDREGS, vgic_mmio_read_v3_idregs, vgic_mmio_write_wi, 48,
Signature · 1 line
2.8.2
PATCH v4 38/56 KVM: arm/arm64: vgic-new: Add GICv3 SGI system register trap handler
In reply to Andre PrzywaraIn contrast to GICv2 SGIs in a GICv3 implementation are not triggered by a MMIO write, but with a system register write. KVM knows about that register already, we just need to implement the handler and wire it up to the core KVM/ARM code.
---
Changelog RFC..v1:
- add comment about SGI_AFFINITY_LEVEL macro
include/kvm/vgic/vgic.h | 8 +++ virt/kvm/arm/vgic/vgic-mmio-v3.c | 106 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+)
include/kvm/vgic/vgic.h +8 -0
@@ -209,6 +209,14 @@ bool kvm_vcpu_has_pending_irqs(struct kvm_vcpu *vcpu);void kvm_vgic_sync_hwstate (struct kvm_vcpu *vcpu);void kvm_vgic_flush_hwstate (struct kvm_vcpu *vcpu); +#ifdef CONFIG_KVM_ARM_VGIC_V3 +void vgic_v3_dispatch_sgi (struct kvm_vcpu *vcpu,u64 reg); +#else +static inline void vgic_v3_dispatch_sgi (struct kvm_vcpu *vcpu,u64 reg) +{ +} +#endif +/** * kvm_vgic_get_max_vcpus - Get the maximum number of VCPUs allowed by HW *
virt/kvm/arm/vgic/vgic-mmio-v3.c +106 -0
109 lines not on this page · raw message
Signature · 1 line
2.8.2
PATCH v4 39/56 KVM: arm/arm64: vgic-new: vgic_kvm_device: KVM device ops registration
In reply to Andre PrzywaraFrom: Eric Auger <[email protected]>
This patch introduces the skeleton for the KVM device operations associated to KVM_DEV_TYPE_ARM_VGIC_V2 and KVM_DEV_TYPE_ARM_VGIC_V3.
At that stage kvm_vgic_create is stubbed.
---
Changelog v1 .. v2:
- rename vgic_kvm_device.c to vgic-kvm-device.c
Changelog v2 .. v3:
- replace inclusion of vgic/vgic.h with arm_vgic.h
- add kvm_register_vgic_device() prototype to this patch
virt/kvm/arm/vgic/vgic-kvm-device.c | 108 ++++++++++++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic.h | 2 + 2 files changed, 110 insertions(+) create mode 100644 virt/kvm/arm/vgic/vgic-kvm-device.c
virt/kvm/arm/vgic/vgic-kvm-device.c new file +108 -0
@@ -0,0 +1,108 @@ +/* +* VGIC: KVM DEVICE API +* +* Copyright (C) 2015 ARM Ltd. +* Author: Marc Zyngier <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +*/ +#include <linux/kvm_host.h> +#include <kvm/arm_vgic.h> + +/* common helpers */ + +static int vgic_create (struct kvm_device *dev,u32 type) +{ +return kvm_vgic_create (dev->kvm, type); +} + +static void vgic_destroy (struct kvm_device *dev) +{ +kfree (dev); +} + +void kvm_register_vgic_device (unsigned long type) +{ +switch (type) { +case KVM_DEV_TYPE_ARM_VGIC_V2 : +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 ); +break ; +#endif + } +} + +/* V2 ops */ + +static int vgic_v2_set_attr (struct kvm_device *dev, +struct kvm_device_attr *attr) +{ +return -ENXIO ; +} + +static int vgic_v2_get_attr (struct kvm_device *dev, +struct kvm_device_attr *attr) +{ +return -ENXIO ; +} + +static int vgic_v2_has_attr (struct kvm_device *dev, +struct kvm_device_attr *attr) +{ +return -ENXIO ; +} + +struct kvm_device_ops kvm_arm_vgic_v2_ops = { + .name ="kvm-arm-vgic-v2" , + .create = vgic_create, + .destroy = vgic_destroy, + .set_attr = vgic_v2_set_attr, + .get_attr = vgic_v2_get_attr, + .has_attr = vgic_v2_has_attr, +}; + +/* V3 ops */ + +#ifdef CONFIG_KVM_ARM_VGIC_V3 + +static int vgic_v3_set_attr (struct kvm_device *dev, +struct kvm_device_attr *attr) +{ +return -ENXIO ; +} + +static int vgic_v3_get_attr (struct kvm_device *dev, +struct kvm_device_attr *attr) +{ +return -ENXIO ; +} + +static int vgic_v3_has_attr (struct kvm_device *dev, +struct kvm_device_attr *attr) +{ +return -ENXIO ; +} + +struct kvm_device_ops kvm_arm_vgic_v3_ops = { + .name ="kvm-arm-vgic-v3" , + .create = vgic_create, + .destroy = vgic_destroy, + .set_attr = vgic_v3_set_attr, + .get_attr = vgic_v3_get_attr, + .has_attr = vgic_v3_has_attr, +}; + +#endif /* CONFIG_KVM_ARM_VGIC_V3 */ +
virt/kvm/arm/vgic/vgic.h +2 -0
@@ -73,4 +73,6 @@ static inline int vgic_register_redist_iodevs(struct kvm *kvm, }#endif +void kvm_register_vgic_device (unsigned long type); +#endif
Signature · 1 line
2.8.2
PATCH v4 40/56 KVM: arm/arm64: vgic-new: vgic_kvm_device: KVM_DEV_ARM_VGIC_GRP_NR_IRQS
In reply to Andre PrzywaraFrom: Eric Auger <[email protected]>
This patch implements the KVM_DEV_ARM_VGIC_GRP_NR_IRQS group. This modality is supported by both VGIC V2 and V3 KVM device as will be other groups, hence the introduction of common helpers.
--- virt/kvm/arm/vgic/vgic-kvm-device.c | 83 +++++++++++++++++++++++++++++++++++-- 1 file changed, 79 insertions(+), 4 deletions(-)
virt/kvm/arm/vgic/vgic-kvm-device.c +79 -4
@@ -15,9 +15,69 @@*/ #include <linux/kvm_host.h> #include <kvm/arm_vgic.h> +#include <linux/uaccess.h> +#include "vgic.h" /* common helpers */ +static int vgic_set_common_attr (struct kvm_device *dev, +struct kvm_device_attr *attr) +{ +switch (attr->group) { +case KVM_DEV_ARM_VGIC_GRP_NR_IRQS : { +u32 __user *uaddr = (u32 __user *)(long )attr->addr; +u32 val; +int ret =0 ; + +if (get_user (val, uaddr)) +return -EFAULT ; + +/* +* We require: +* - at least 32 SPIs on top of the 16 SGIs and 16 PPIs +* - at most 1024 interrupts +* - a multiple of 32 interrupts +*/ +if (val < (VGIC_NR_PRIVATE_IRQS +32 ) || + val >VGIC_MAX_RESERVED || + (val &31 )) +return -EINVAL ; + +mutex_lock (&dev->kvm->lock); + +if (vgic_ready (dev->kvm) || dev->kvm->arch.vgic.nr_spis) + ret = -EBUSY ; +else + dev->kvm->arch.vgic.nr_spis = + val -VGIC_NR_PRIVATE_IRQS ; + +mutex_unlock (&dev->kvm->lock); + +return ret; + } + } + +return -ENXIO ; +} + +static int vgic_get_common_attr (struct kvm_device *dev, +struct kvm_device_attr *attr) +{ +int r = -ENXIO ; + +switch (attr->group) { +case KVM_DEV_ARM_VGIC_GRP_NR_IRQS : { +u32 __user *uaddr = (u32 __user *)(long )attr->addr; + + r =put_user (dev->kvm->arch.vgic.nr_spis + +VGIC_NR_PRIVATE_IRQS , uaddr); +break ; + } + } + +return r; +} +static int vgic_create (struct kvm_device *dev,u32 type) {return kvm_vgic_create (dev->kvm, type);
@@ -49,18 +109,29 @@ void kvm_register_vgic_device(unsigned long type)static int vgic_v2_set_attr (struct kvm_device *dev,struct kvm_device_attr *attr) { -return -ENXIO ; +int ret; + + ret =vgic_set_common_attr (dev, attr); +return ret; + }static int vgic_v2_get_attr (struct kvm_device *dev,struct kvm_device_attr *attr) { -return -ENXIO ; +int ret; + + ret =vgic_get_common_attr (dev, attr); +return ret; }static int vgic_v2_has_attr (struct kvm_device *dev,struct kvm_device_attr *attr) { +switch (attr->group) { +case KVM_DEV_ARM_VGIC_GRP_NR_IRQS : +return 0 ; + }return -ENXIO ; }
@@ -80,18 +151,22 @@ struct kvm_device_ops kvm_arm_vgic_v2_ops = {static int vgic_v3_set_attr (struct kvm_device *dev,struct kvm_device_attr *attr) { -return -ENXIO ; +return vgic_set_common_attr (dev, attr); }static int vgic_v3_get_attr (struct kvm_device *dev,struct kvm_device_attr *attr) { -return -ENXIO ; +return vgic_get_common_attr (dev, attr); }static int vgic_v3_has_attr (struct kvm_device *dev,struct kvm_device_attr *attr) { +switch (attr->group) { +case KVM_DEV_ARM_VGIC_GRP_NR_IRQS : +return 0 ; + }return -ENXIO ; }
Signature · 1 line
2.8.2
PATCH v4 41/56 KVM: arm/arm64: vgic-new: vgic_kvm_device: KVM_DEV_ARM_VGIC_GRP_CTRL
In reply to Andre PrzywaraFrom: Eric Auger <[email protected]>
This patch implements the KVM_DEV_ARM_VGIC_GRP_CTRL group API featuring KVM_DEV_ARM_VGIC_CTRL_INIT attribute. The vgic_init function is not yet implemented though.
--- virt/kvm/arm/vgic/vgic-kvm-device.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+)
virt/kvm/arm/vgic/vgic-kvm-device.c +22 -0
@@ -23,6 +23,8 @@static int vgic_set_common_attr (struct kvm_device *dev,struct kvm_device_attr *attr) { +int r; +switch (attr->group) {case KVM_DEV_ARM_VGIC_GRP_NR_IRQS : {u32 __user *uaddr = (u32 __user *)(long )attr->addr;
@@ -55,6 +57,16 @@ static int vgic_set_common_attr(struct kvm_device *dev,return ret; } +case KVM_DEV_ARM_VGIC_GRP_CTRL : { +switch (attr->attr) { +case KVM_DEV_ARM_VGIC_CTRL_INIT : +mutex_lock (&dev->kvm->lock); + r =vgic_init (dev->kvm); +mutex_unlock (&dev->kvm->lock); +return r; + } +break ; + } }return -ENXIO ;
@@ -131,6 +143,11 @@ static int vgic_v2_has_attr(struct kvm_device *dev,switch (attr->group) {case KVM_DEV_ARM_VGIC_GRP_NR_IRQS :return 0 ; +case KVM_DEV_ARM_VGIC_GRP_CTRL : +switch (attr->attr) { +case KVM_DEV_ARM_VGIC_CTRL_INIT : +return 0 ; + } }return -ENXIO ; }
@@ -166,6 +183,11 @@ static int vgic_v3_has_attr(struct kvm_device *dev,switch (attr->group) {case KVM_DEV_ARM_VGIC_GRP_NR_IRQS :return 0 ; +case KVM_DEV_ARM_VGIC_GRP_CTRL : +switch (attr->attr) { +case KVM_DEV_ARM_VGIC_CTRL_INIT : +return 0 ; + } }return -ENXIO ; }
Signature · 1 line
2.8.2
PATCH v4 42/56 KVM: arm/arm64: vgic-new: vgic_kvm_device: implement kvm_vgic_addr
In reply to Andre PrzywaraFrom: Eric Auger <[email protected]>
kvm_vgic_addr is used by the userspace to set the base address of the following register regions, as seen by the guest:
- distributor(v2 and v3),
- re-distributors (v3),
- CPU interface (v2).
---
Changelog v3 .. v4:
- move overlap check to init time (cannot work with GICv3 here)
- refactor remaining address sanity checks
include/kvm/vgic/vgic.h | 2 + virt/kvm/arm/vgic/vgic-kvm-device.c | 86 +++++++++++++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic.h | 3 ++ 3 files changed, 91 insertions(+)
include/kvm/vgic/vgic.h +2 -0
@@ -194,6 +194,8 @@ struct vgic_cpu {u64 live_lrs; }; +int kvm_vgic_addr (struct kvm *kvm,unsigned long type,u64 *addr,bool write); +int kvm_vgic_inject_irq (struct kvm *kvm,int cpuid,unsigned int intid,bool level);
virt/kvm/arm/vgic/vgic-kvm-device.c +86 -0
96 lines not on this page · raw message
virt/kvm/arm/vgic/vgic.h +3 -0
@@ -19,6 +19,9 @@#define PRODUCT_ID_KVM 0x4b/* ASCII code K */ #define IMPLEMENTER_ARM 0x43b +#define VGIC_ADDR_UNDEF (-1) +#define IS_VGIC_ADDR_UNDEF (_x) ((_x) == VGIC_ADDR_UNDEF) +#define INTERRUPT_ID_BITS_SPIS 10#define VGIC_PRI_BITS 5
Signature · 1 line
2.8.2
PATCH v4 43/56 KVM: arm/arm64: vgic-new: vgic_kvm_device: KVM_DEV_ARM_VGIC_GRP_ADDR
In reply to Andre PrzywaraFrom: Eric Auger <[email protected]>
This patch implements the KVM_DEV_ARM_VGIC_GRP_ADDR group which enables to set the base address of GIC regions as seen by the guest.
--- virt/kvm/arm/vgic/vgic-kvm-device.c | 38 +++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+)
virt/kvm/arm/vgic/vgic-kvm-device.c +38 -0
@@ -112,6 +112,17 @@ static int vgic_set_common_attr(struct kvm_device *dev,int r;switch (attr->group) { +case KVM_DEV_ARM_VGIC_GRP_ADDR : { +u64 __user *uaddr = (u64 __user *)(long )attr->addr; +u64 addr; +unsigned long type = (unsigned long )attr->attr; + +if (copy_from_user (&addr, uaddr,sizeof (addr))) +return -EFAULT ; + + r =kvm_vgic_addr (dev->kvm, type, &addr, true); +return (r == -ENODEV ) ? -ENXIO : r; + }case KVM_DEV_ARM_VGIC_GRP_NR_IRQS : {u32 __user *uaddr = (u32 __user *)(long )attr->addr;u32 val;
@@ -164,6 +175,19 @@ static int vgic_get_common_attr(struct kvm_device *dev,int r = -ENXIO ;switch (attr->group) { +case KVM_DEV_ARM_VGIC_GRP_ADDR : { +u64 __user *uaddr = (u64 __user *)(long )attr->addr; +u64 addr; +unsigned long type = (unsigned long )attr->attr; + + r =kvm_vgic_addr (dev->kvm, type, &addr, false); +if (r) +return (r == -ENODEV ) ? -ENXIO : r; + +if (copy_to_user (uaddr, &addr,sizeof (addr))) +return -EFAULT ; +break ; + }case KVM_DEV_ARM_VGIC_GRP_NR_IRQS : {u32 __user *uaddr = (u32 __user *)(long )attr->addr;
@@ -227,6 +251,13 @@ static int vgic_v2_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_V2_ADDR_TYPE_DIST : +case KVM_VGIC_V2_ADDR_TYPE_CPU : +return 0 ; + } +break ;case KVM_DEV_ARM_VGIC_GRP_NR_IRQS :return 0 ;case KVM_DEV_ARM_VGIC_GRP_CTRL :
@@ -267,6 +298,13 @@ static int vgic_v3_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_V3_ADDR_TYPE_DIST : +case KVM_VGIC_V3_ADDR_TYPE_REDIST : +return 0 ; + } +break ;case KVM_DEV_ARM_VGIC_GRP_NR_IRQS :return 0 ;case KVM_DEV_ARM_VGIC_GRP_CTRL :
Signature · 1 line
2.8.2
PATCH v4 44/56 KVM: arm/arm64: vgic-new: vgic_kvm_device: access to VGIC registers
In reply to Andre PrzywaraFrom: Eric Auger <[email protected]>
This patch implements the switches for KVM_DEV_ARM_VGIC_GRP_DIST_REGS and KVM_DEV_ARM_VGIC_GRP_CPU_REGS API which allows the userspace to access VGIC registers.
---
Changelog v3 .. v4:
- check for proper alignment on userland GIC accesses
virt/kvm/arm/vgic/vgic-kvm-device.c | 53 +++++++++++++++++++++++++++++++++++-- virt/kvm/arm/vgic/vgic-mmio-v2.c | 38 ++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic.h | 1 + 3 files changed, 90 insertions(+), 2 deletions(-)
virt/kvm/arm/vgic/vgic-kvm-device.c +51 -2
@@ -226,6 +226,21 @@ void kvm_register_vgic_device(unsigned long type) } } +/** vgic_attr_regs_access: allows user space to read/write VGIC registers +* +* @dev: kvm device handle +* @attr: kvm device attribute +* @reg: address the value is read or written +* @is_write: write flag +* +*/ +static int vgic_attr_regs_access (struct kvm_device *dev, +struct kvm_device_attr *attr, +u32 *reg,bool is_write) +{ +return -ENXIO ; +} +/* V2 ops */ static int vgic_v2_set_attr(struct kvm_device *dev,
@@ -234,8 +249,23 @@ static int vgic_v2_set_attr(struct kvm_device *dev,int ret; ret =vgic_set_common_attr (dev, attr); -return ret; +if (ret != -ENXIO ) +return ret; + +switch (attr->group) { +case KVM_DEV_ARM_VGIC_GRP_DIST_REGS : +case KVM_DEV_ARM_VGIC_GRP_CPU_REGS : { +u32 __user *uaddr = (u32 __user *)(long )attr->addr; +u32 reg; + +if (get_user (reg, uaddr)) +return -EFAULT ; +return vgic_attr_regs_access (dev, attr, ®, true); + } + } + +return -ENXIO ; } static int vgic_v2_get_attr(struct kvm_device *dev,
@@ -244,7 +274,23 @@ static int vgic_v2_get_attr(struct kvm_device *dev,int ret; ret =vgic_get_common_attr (dev, attr); -return ret; +if (ret != -ENXIO ) +return ret; + +switch (attr->group) { +case KVM_DEV_ARM_VGIC_GRP_DIST_REGS : +case KVM_DEV_ARM_VGIC_GRP_CPU_REGS : { +u32 __user *uaddr = (u32 __user *)(long )attr->addr; +u32 reg =0 ; + + ret =vgic_attr_regs_access (dev, attr, ®, false); +if (ret) +return ret; +return put_user (reg, uaddr); + } + } + +return -ENXIO ; } static int vgic_v2_has_attr(struct kvm_device *dev,
@@ -258,6 +304,9 @@ static int vgic_v2_has_attr(struct kvm_device *dev,return 0 ; }break ; +case KVM_DEV_ARM_VGIC_GRP_DIST_REGS : +case KVM_DEV_ARM_VGIC_GRP_CPU_REGS : +return vgic_v2_has_attr_regs (dev, attr);case KVM_DEV_ARM_VGIC_GRP_NR_IRQS :return 0 ;case KVM_DEV_ARM_VGIC_GRP_CTRL :
virt/kvm/arm/vgic/vgic-mmio-v2.c +38 -0
@@ -258,3 +258,41 @@ unsigned int vgic_v2_init_dist_iodev(struct vgic_io_device *dev)return SZ_4K ; } + +int vgic_v2_has_attr_regs (struct kvm_device *dev,struct kvm_device_attr *attr) +{ +int nr_irqs = dev->kvm->arch.vgic.nr_spis +VGIC_NR_PRIVATE_IRQS ; +const struct vgic_register_region *regions; +gpa_t addr; +int nr_regions, i, len; + + addr = attr->attr &KVM_DEV_ARM_VGIC_OFFSET_MASK ; + +switch (attr->group) { +case KVM_DEV_ARM_VGIC_GRP_DIST_REGS : + regions = vgic_v2_dist_registers; + nr_regions =ARRAY_SIZE (vgic_v2_dist_registers); +break ; +case KVM_DEV_ARM_VGIC_GRP_CPU_REGS : +return -ENXIO ;/* TODO: describe CPU i/f regs also */ +default : +return -ENXIO ; + } + +/* We only support aligned 32-bit accesses. */ +if (addr &3 ) +return -ENXIO ; + +for (i =0 ; i < nr_regions; i++) { +if (regions[i].bits_per_irq) + len = (regions[i].bits_per_irq * nr_irqs) /8 ; +else + len = regions[i].len; + +if (regions[i].reg_offset <= addr && + regions[i].reg_offset + len > addr) +return 0 ; + } + +return -ENXIO ; +}
virt/kvm/arm/vgic/vgic.h +1 -0
@@ -37,6 +37,7 @@ 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);void vgic_v2_clear_lr (struct kvm_vcpu *vcpu,int lr);void vgic_v2_set_underflow (struct kvm_vcpu *vcpu); +int vgic_v2_has_attr_regs (struct kvm_device *dev,struct kvm_device_attr *attr);int vgic_register_dist_iodev (struct kvm *kvm,gpa_t dist_base_address,enum vgic_type );
Signature · 1 line
2.8.2
PATCH v4 45/56 KVM: arm/arm64: vgic-new: Export register access interface
In reply to Andre PrzywaraFrom: Christoffer Dall <[email protected]>
Userland can access the emulated GIC to save and restore its state for initialization or migration purposes.
The kvm_io_bus API requires an absolute gpa, which does not fit the KVM_DEV_ARM_VGIC_GRP_DIST_REGS user API, that only provides relative offsets. So we provide a wrapper to plug into our MMIO framework and find the respective register handler.
---
Changelog v1 .. v2:
- adapt to new MMIO framework
- handle endianess explicitly
Changelog v2 .. v3:
- wrap accessor function for easier reusage later
- moved from earlier in the series
virt/kvm/arm/vgic/vgic-mmio-v2.c | 36 ++++++++++++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic.h | 2 ++ 2 files changed, 38 insertions(+)
virt/kvm/arm/vgic/vgic-mmio-v2.c +36 -0
@@ -296,3 +296,39 @@ int vgic_v2_has_attr_regs(struct kvm_device *dev, struct kvm_device_attr *attr)return -ENXIO ; } + +/* +* When userland tries to access the VGIC register handlers, we need to +* create a usable struct vgic_io_device to be passed to the handlers and we +* have to set up a buffer similar to what would have happened if a guest MMIO +* access occurred, including doing endian conversions on BE systems. +*/ +static int vgic_uaccess (struct kvm_vcpu *vcpu,struct vgic_io_device *dev, +bool is_write,int offset,u32 *val) +{ +unsigned int len =4 ; +u8 buf[4 ]; +int ret; + +if (is_write) { +vgic_data_host_to_mmio_bus (buf, len, *val); + ret = kvm_io_gic_ops.write (vcpu, &dev->dev, offset, len, buf); + }else { + ret = kvm_io_gic_ops.read (vcpu, &dev->dev, offset, len, buf); +if (!ret) + *val =vgic_data_mmio_bus_to_host (buf, len); + } + +return ret; +} + +int vgic_v2_dist_uaccess (struct kvm_vcpu *vcpu,bool is_write, +int offset,u32 *val) +{ +struct vgic_io_device dev = { + .regions = vgic_v2_dist_registers, + .nr_regions =ARRAY_SIZE (vgic_v2_dist_registers), + }; + +return vgic_uaccess (vcpu, &dev, is_write, offset, val); +}
virt/kvm/arm/vgic/vgic.h +2 -0
@@ -38,6 +38,8 @@ void vgic_v2_populate_lr(struct kvm_vcpu *vcpu, struct vgic_irq *irq, int lr);void vgic_v2_clear_lr (struct kvm_vcpu *vcpu,int lr);void vgic_v2_set_underflow (struct kvm_vcpu *vcpu);int vgic_v2_has_attr_regs (struct kvm_device *dev,struct kvm_device_attr *attr); +int vgic_v2_dist_uaccess (struct kvm_vcpu *vcpu,bool is_write, +int offset,u32 *val);int vgic_register_dist_iodev (struct kvm *kvm,gpa_t dist_base_address,enum vgic_type );
Signature · 1 line
2.8.2
PATCH v4 46/56 KVM: arm/arm64: vgic-new: Add userland access to VGIC dist registers
In reply to Andre PrzywaraUserland may want to save and restore the state of the in-kernel VGIC, so we provide the code which takes a userland request and translate that into calls to our MMIO framework.
--- virt/kvm/arm/vgic/vgic-kvm-device.c | 50 ++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-)
virt/kvm/arm/vgic/vgic-kvm-device.c +49 -1
@@ -238,7 +238,55 @@ static int vgic_attr_regs_access(struct kvm_device *dev,struct kvm_device_attr *attr, u32 *reg, bool is_write) { - return -ENXIO; + gpa_t addr; +int cpuid, ret, c; +struct kvm_vcpu *vcpu, *tmp_vcpu; + + cpuid = (attr->attr &KVM_DEV_ARM_VGIC_CPUID_MASK ) >> +KVM_DEV_ARM_VGIC_CPUID_SHIFT ; + vcpu =kvm_get_vcpu (dev->kvm, cpuid); + addr = attr->attr &KVM_DEV_ARM_VGIC_OFFSET_MASK ; + +mutex_lock (&dev->kvm->lock); + + ret =vgic_init (dev->kvm); +if (ret) + goto out; + +if (cpuid >=atomic_read (&dev->kvm->online_vcpus)) { + ret = -EINVAL ; + goto out; + } + +/* +* Ensure that no other VCPU is running by checking the vcpu->cpu +* field. If no other VPCUs are running we can safely access the VGIC +* state, because even if another VPU is run after this point, that +* VCPU will not touch the vgic state, because it will block on +* getting the vgic->lock in kvm_vgic_sync_hwstate(). +*/ +kvm_for_each_vcpu (c, tmp_vcpu, dev->kvm) { +if (unlikely (tmp_vcpu->cpu !=-1 )) { + ret = -EBUSY ; + goto out; + } + } + +switch (attr->group) { +case KVM_DEV_ARM_VGIC_GRP_CPU_REGS : + ret = -EINVAL ; +break ; +case KVM_DEV_ARM_VGIC_GRP_DIST_REGS : + ret =vgic_v2_dist_uaccess (vcpu, is_write, addr, reg); +break ; +default : + ret = -EINVAL ; +break ; + } + +out: +mutex_unlock (&dev->kvm->lock); +return ret; }/* V2 ops */
Signature · 1 line
2.8.2
PATCH v4 47/56 KVM: arm/arm64: vgic-new: Add GICH_VMCR accessors
In reply to Andre PrzywaraSince the GIC CPU interface is always virtualized by the hardware, we don't have CPU interface state information readily available in our emulation if userland wants to save or restore it. Fortunately the GIC hypervisor interface provides the VMCR register to access the required virtual CPU interface bits.
Provide wrappers for GICv2 and GICv3 hosts to have access to this register.
---
Changelog v2 .. v3:
- remove exported v2/v3 selector functions (as per Marc's patch)
Changelog v3 .. v4:
- move struct vgic_vmcr into the VGIC's private header file
virt/kvm/arm/vgic/vgic-v2.c | 29 +++++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic-v3.c | 22 ++++++++++++++++++++++ virt/kvm/arm/vgic/vgic.h | 21 +++++++++++++++++++++ 3 files changed, 72 insertions(+)
virt/kvm/arm/vgic/vgic-v2.c +29 -0
@@ -174,3 +174,32 @@ void vgic_v2_clear_lr(struct kvm_vcpu *vcpu, int lr) { vcpu->arch.vgic_cpu.vgic_v2.vgic_lr[lr] =0 ; } + +void vgic_v2_set_vmcr (struct kvm_vcpu *vcpu,struct vgic_vmcr *vmcrp) +{ +u32 vmcr; + + vmcr = (vmcrp->ctlr <<GICH_VMCR_CTRL_SHIFT ) &GICH_VMCR_CTRL_MASK ; + vmcr |= (vmcrp->abpr <<GICH_VMCR_ALIAS_BINPOINT_SHIFT ) & +GICH_VMCR_ALIAS_BINPOINT_MASK ; + vmcr |= (vmcrp->bpr <<GICH_VMCR_BINPOINT_SHIFT ) & +GICH_VMCR_BINPOINT_MASK ; + vmcr |= (vmcrp->pmr <<GICH_VMCR_PRIMASK_SHIFT ) & +GICH_VMCR_PRIMASK_MASK ; + + vcpu->arch.vgic_cpu.vgic_v2.vgic_vmcr = vmcr; +} + +void vgic_v2_get_vmcr (struct kvm_vcpu *vcpu,struct vgic_vmcr *vmcrp) +{ +u32 vmcr = vcpu->arch.vgic_cpu.vgic_v2.vgic_vmcr; + + vmcrp->ctlr = (vmcr &GICH_VMCR_CTRL_MASK ) >> +GICH_VMCR_CTRL_SHIFT ; + vmcrp->abpr = (vmcr &GICH_VMCR_ALIAS_BINPOINT_MASK ) >> +GICH_VMCR_ALIAS_BINPOINT_SHIFT ; + vmcrp->bpr = (vmcr &GICH_VMCR_BINPOINT_MASK ) >> +GICH_VMCR_BINPOINT_SHIFT ; + vmcrp->pmr = (vmcr &GICH_VMCR_PRIMASK_MASK ) >> +GICH_VMCR_PRIMASK_SHIFT ; +}
virt/kvm/arm/vgic/vgic-v3.c +22 -0
@@ -160,3 +160,25 @@ void vgic_v3_clear_lr(struct kvm_vcpu *vcpu, int lr) { vcpu->arch.vgic_cpu.vgic_v3.vgic_lr[lr] =0 ; } + +void vgic_v3_set_vmcr (struct kvm_vcpu *vcpu,struct vgic_vmcr *vmcrp) +{ +u32 vmcr; + + vmcr = (vmcrp->ctlr <<ICH_VMCR_CTLR_SHIFT ) &ICH_VMCR_CTLR_MASK ; + vmcr |= (vmcrp->abpr <<ICH_VMCR_BPR1_SHIFT ) &ICH_VMCR_BPR1_MASK ; + vmcr |= (vmcrp->bpr <<ICH_VMCR_BPR0_SHIFT ) &ICH_VMCR_BPR0_MASK ; + vmcr |= (vmcrp->pmr <<ICH_VMCR_PMR_SHIFT ) &ICH_VMCR_PMR_MASK ; + + vcpu->arch.vgic_cpu.vgic_v3.vgic_vmcr = vmcr; +} + +void vgic_v3_get_vmcr (struct kvm_vcpu *vcpu,struct vgic_vmcr *vmcrp) +{ +u32 vmcr = vcpu->arch.vgic_cpu.vgic_v3.vgic_vmcr; + + vmcrp->ctlr = (vmcr &ICH_VMCR_CTLR_MASK ) >>ICH_VMCR_CTLR_SHIFT ; + vmcrp->abpr = (vmcr &ICH_VMCR_BPR1_MASK ) >>ICH_VMCR_BPR1_SHIFT ; + vmcrp->bpr = (vmcr &ICH_VMCR_BPR0_MASK ) >>ICH_VMCR_BPR0_SHIFT ; + vmcrp->pmr = (vmcr &ICH_VMCR_PMR_MASK ) >>ICH_VMCR_PMR_SHIFT ; +}
virt/kvm/arm/vgic/vgic.h +21 -0
@@ -27,6 +27,13 @@#define vgic_irq_is_sgi (intid) ((intid) < VGIC_NR_SGIS) +struct vgic_vmcr { +u32 ctlr; +u32 abpr; +u32 bpr; +u32 pmr; +}; +struct vgic_irq *vgic_get_irq (struct kvm *kvm,struct kvm_vcpu *vcpu,u32 intid);bool vgic_queue_irq_unlock (struct kvm *kvm,struct vgic_irq *irq);
@@ -40,6 +47,8 @@ void vgic_v2_set_underflow(struct kvm_vcpu *vcpu);int vgic_v2_has_attr_regs (struct kvm_device *dev,struct kvm_device_attr *attr);int vgic_v2_dist_uaccess (struct kvm_vcpu *vcpu,bool is_write,int offset,u32 *val); +void vgic_v2_set_vmcr (struct kvm_vcpu *vcpu,struct vgic_vmcr *vmcr); +void vgic_v2_get_vmcr (struct kvm_vcpu *vcpu,struct vgic_vmcr *vmcr);int vgic_register_dist_iodev (struct kvm *kvm,gpa_t dist_base_address,enum vgic_type );
@@ -49,6 +58,8 @@ void vgic_v3_fold_lr_state(struct kvm_vcpu *vcpu);void vgic_v3_populate_lr (struct kvm_vcpu *vcpu,struct vgic_irq *irq,int lr);void vgic_v3_clear_lr (struct kvm_vcpu *vcpu,int lr);void vgic_v3_set_underflow (struct kvm_vcpu *vcpu); +void vgic_v3_set_vmcr (struct kvm_vcpu *vcpu,struct vgic_vmcr *vmcr); +void vgic_v3_get_vmcr (struct kvm_vcpu *vcpu,struct vgic_vmcr *vmcr);int vgic_register_redist_iodevs (struct kvm *kvm,gpa_t dist_base_address);#else static inline void vgic_v3_process_maintenance (struct kvm_vcpu *vcpu)
@@ -72,6 +83,16 @@ static inline void vgic_v3_set_underflow(struct kvm_vcpu *vcpu) { } +static inline +void vgic_v3_set_vmcr (struct kvm_vcpu *vcpu,struct vgic_vmcr *vmcr) +{ +} + +static inline +void vgic_v3_get_vmcr (struct kvm_vcpu *vcpu,struct vgic_vmcr *vmcr) +{ +} +static inline int vgic_register_redist_iodevs (struct kvm *kvm,gpa_t dist_base_address) {
Signature · 1 line
2.8.2
PATCH v4 48/56 KVM: arm/arm64: vgic-new: Add userland GIC CPU interface access
In reply to Andre PrzywaraUsing the VMCR accessors we provide access to GIC CPU interface state to userland by wiring it up to the existing userland interface. [Marc: move and make VMCR accessors static, streamline MMIO handlers]
---
Changelog v2 .. v3:
- total rework, moving into vgic-mmio-v2.c
- move vmcr accessor wrapper functions into this file
- use the register description table for CPU i/f registers as well
- add RAZ/WI handling for the active priority registers
- streamline MMIO handler functions
Changelog v3 .. v4:
- advertise CPU interface registers to userland
- specify accessor width
- replace call to extract_bytes() with a simple return
virt/kvm/arm/vgic/vgic-kvm-device.c | 2 +- virt/kvm/arm/vgic/vgic-mmio-v2.c | 114 +++++++++++++++++++++++++++++++++++- virt/kvm/arm/vgic/vgic.h | 2 + 3 files changed, 116 insertions(+), 2 deletions(-)
virt/kvm/arm/vgic/vgic-kvm-device.c +1 -1
@@ -274,7 +274,7 @@ static int vgic_attr_regs_access(struct kvm_device *dev,switch (attr->group) {case KVM_DEV_ARM_VGIC_GRP_CPU_REGS : - ret = -EINVAL ; + ret =vgic_v2_cpuif_uaccess (vcpu, is_write, addr, reg);break ;case KVM_DEV_ARM_VGIC_GRP_DIST_REGS : ret =vgic_v2_dist_uaccess (vcpu, is_write, addr, reg);
virt/kvm/arm/vgic/vgic-mmio-v2.c +113 -1
138 lines not on this page · raw message
virt/kvm/arm/vgic/vgic.h +2 -0
@@ -47,6 +47,8 @@ void vgic_v2_set_underflow(struct kvm_vcpu *vcpu);int vgic_v2_has_attr_regs (struct kvm_device *dev,struct kvm_device_attr *attr);int vgic_v2_dist_uaccess (struct kvm_vcpu *vcpu,bool is_write,int offset,u32 *val); +int vgic_v2_cpuif_uaccess (struct kvm_vcpu *vcpu,bool is_write, +int offset,u32 *val);void vgic_v2_set_vmcr (struct kvm_vcpu *vcpu,struct vgic_vmcr *vmcr);void vgic_v2_get_vmcr (struct kvm_vcpu *vcpu,struct vgic_vmcr *vmcr); int vgic_register_dist_iodev(struct kvm *kvm, gpa_t dist_base_address,
Signature · 1 line
2.8.2
PATCH v4 49/56 KVM: arm/arm64: vgic-new: vgic_init: implement kvm_vgic_hyp_init
In reply to Andre PrzywaraFrom: Eric Auger <[email protected]>
Implements kvm_vgic_hyp_init and vgic_probe function.
This uses the new firmware independent VGIC probing to support both ACPI and DT based systems (code from Marc Zyngier).
The vgic_global struct is enriched with new fields populated by those functions.
---
Changelog v1 .. v2:
- rename vgic_init.c to vgic-init.c
Changelog v2 .. v3:
- include kvm/arm_vgic.h instead of kvm/vgic/vgic.h
- move ich_vtr_el2 variable into probe function
Changelog v3 .. v4:
- improve nr_lr determination in GICv2 probe
- switching to firmware-independent probing
include/kvm/vgic/vgic.h | 1 + virt/kvm/arm/vgic/vgic-init.c | 123 ++++++++++++++++++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic-v2.c | 64 ++++++++++++++++++++++ virt/kvm/arm/vgic/vgic-v3.c | 49 +++++++++++++++++ virt/kvm/arm/vgic/vgic.h | 9 ++++ 5 files changed, 246 insertions(+) create mode 100644 virt/kvm/arm/vgic/vgic-init.c
include/kvm/vgic/vgic.h +1 -0
@@ -195,6 +195,7 @@ struct vgic_cpu { };int kvm_vgic_addr (struct kvm *kvm,unsigned long type,u64 *addr,bool write); +int kvm_vgic_hyp_init (void );int kvm_vgic_inject_irq (struct kvm *kvm,int cpuid,unsigned int intid,bool level);
virt/kvm/arm/vgic/vgic-init.c new file +123 -0
123 lines not on this page · raw message
virt/kvm/arm/vgic/vgic-v2.c +64 -0
73 lines not on this page · raw message
virt/kvm/arm/vgic/vgic-v3.c +49 -0
58 lines not on this page · raw message
virt/kvm/arm/vgic/vgic.h +9 -0
@@ -16,6 +16,8 @@#ifndef __KVM_ARM_VGIC_NEW_H__#define __KVM_ARM_VGIC_NEW_H__ +#include <linux/irqchip/arm-gic-common.h> +#define PRODUCT_ID_KVM 0x4b/* ASCII code K */ #define IMPLEMENTER_ARM 0x43b
@@ -51,6 +53,7 @@ int vgic_v2_cpuif_uaccess(struct kvm_vcpu *vcpu, bool is_write, int offset, u32 *val);void vgic_v2_set_vmcr (struct kvm_vcpu *vcpu,struct vgic_vmcr *vmcr);void vgic_v2_get_vmcr (struct kvm_vcpu *vcpu,struct vgic_vmcr *vmcr); +int vgic_v2_probe (const struct gic_kvm_info *info);int vgic_register_dist_iodev (struct kvm *kvm,gpa_t dist_base_address,enum vgic_type );
@@ -62,6 +65,7 @@ void vgic_v3_clear_lr(struct kvm_vcpu *vcpu, int lr);void vgic_v3_set_underflow (struct kvm_vcpu *vcpu);void vgic_v3_set_vmcr (struct kvm_vcpu *vcpu,struct vgic_vmcr *vmcr);void vgic_v3_get_vmcr (struct kvm_vcpu *vcpu,struct vgic_vmcr *vmcr); +int vgic_v3_probe (const struct gic_kvm_info *info);int vgic_register_redist_iodevs (struct kvm *kvm,gpa_t dist_base_address);#else static inline void vgic_v3_process_maintenance (struct kvm_vcpu *vcpu)
@@ -95,6 +99,11 @@ void vgic_v3_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr) { } +static inline int vgic_v3_probe (const struct gic_kvm_info *info) +{ +return -ENODEV ; +} +static inline int vgic_register_redist_iodevs (struct kvm *kvm,gpa_t dist_base_address) {
Signature · 1 line
2.8.2
PATCH v4 50/56 KVM: arm/arm64: vgic-new: vgic_init: implement vgic_create
In reply to Andre PrzywaraFrom: Eric Auger <[email protected]>
This patch implements the vgic_creation function which is called on CREATE_IRQCHIP VM IOCTL (v2 only) or KVM_CREATE_DEVICE
---
Changelog v3 .. v4:
- fix comment
include/kvm/vgic/vgic.h | 1 + virt/kvm/arm/vgic/vgic-init.c | 84 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+)
include/kvm/vgic/vgic.h +1 -0
@@ -195,6 +195,7 @@ struct vgic_cpu { };int kvm_vgic_addr (struct kvm *kvm,unsigned long type,u64 *addr,bool write); +int kvm_vgic_create (struct kvm *kvm,u32 type);int kvm_vgic_hyp_init (void ); int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int intid,
virt/kvm/arm/vgic/vgic-init.c +84 -0
90 lines not on this page · raw message
Signature · 1 line
2.8.2
PATCH v4 51/56 KVM: arm/arm64: vgic-new: vgic_init: implement vgic_init
In reply to Andre PrzywaraFrom: Eric Auger <[email protected]>
This patch allocates and initializes the data structures used to model the vgic distributor and virtual cpu interfaces. At that stage the number of IRQs and number of virtual CPUs is frozen.
---
Changelog RFC..v1:
- initialize v2/v3 default CPU affinities explicitly
Changelog v1 .. v2:
- move lazy_init() into vgic_update_irq_pending()
Changelog v3 .. v4:
- fix comments
- make local functions static
- adapt to new firmware independent probing
include/kvm/vgic/vgic.h | 7 +- virt/kvm/arm/vgic/vgic-init.c | 217 ++++++++++++++++++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic-v2.c | 5 + virt/kvm/arm/vgic/vgic-v3.c | 5 + virt/kvm/arm/vgic/vgic.c | 4 + virt/kvm/arm/vgic/vgic.h | 8 ++ 6 files changed, 245 insertions(+), 1 deletion(-)
include/kvm/vgic/vgic.h +6 -1
@@ -119,6 +119,7 @@ struct vgic_io_device {struct vgic_dist {bool in_kernel;bool ready; +bool initialized;/* vGIC model the kernel emulates for the guest (GICv2 or GICv3) */ u32 vgic_model;
@@ -195,7 +196,11 @@ struct vgic_cpu { };int kvm_vgic_addr (struct kvm *kvm,unsigned long type,u64 *addr,bool write); +void kvm_vgic_early_init (struct kvm *kvm);int kvm_vgic_create (struct kvm *kvm,u32 type); +void kvm_vgic_destroy (struct kvm *kvm); +void kvm_vgic_vcpu_early_init (struct kvm_vcpu *vcpu); +void kvm_vgic_vcpu_destroy (struct kvm_vcpu *vcpu);int kvm_vgic_hyp_init (void ); int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int intid,
@@ -204,7 +209,7 @@ int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int intid,int kvm_vgic_vcpu_pending_irq (struct kvm_vcpu *vcpu);#define irqchip_in_kernel (k) (!!((k)->arch.vgic.in_kernel)) -#define vgic_initialized (k) (false) +#define vgic_initialized (k) ((k)->arch.vgic.initialized)#define vgic_ready (k) ((k)->arch.vgic.ready)#define vgic_valid_spi (k, i) (((i) >= VGIC_NR_PRIVATE_IRQS) && \ ((i) < (k)->arch.vgic.nr_spis + VGIC_NR_PRIVATE_IRQS))
virt/kvm/arm/vgic/vgic-init.c +217 -0
235 lines not on this page · raw message
virt/kvm/arm/vgic/vgic-v2.c +5 -0
@@ -206,6 +206,11 @@ void vgic_v2_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcrp)GICH_VMCR_PRIMASK_SHIFT ; } +/* not yet implemented */ +void vgic_v2_enable (struct kvm_vcpu *vcpu) +{ +} +/** * vgic_v2_probe - probe for a GICv2 compatible interrupt controller in DT * @node: pointer to the DT node
virt/kvm/arm/vgic/vgic-v3.c +5 -0
@@ -186,6 +186,11 @@ void vgic_v3_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcrp) vmcrp->pmr = (vmcr &ICH_VMCR_PMR_MASK ) >>ICH_VMCR_PMR_SHIFT ; } +/* not yet implemented */ +void vgic_v3_enable (struct kvm_vcpu *vcpu) +{ +} +/** * vgic_v3_probe - probe for a GICv3 compatible interrupt controller in DT * @node: pointer to the DT node
virt/kvm/arm/vgic/vgic.c +4 -0
@@ -257,6 +257,10 @@ static int vgic_update_irq_pending(struct kvm *kvm, int cpuid,trace_vgic_update_irq_pending (cpuid, intid, level); + ret =vgic_lazy_init (kvm); +if (ret) +return ret; + vcpu =kvm_get_vcpu (kvm, cpuid);if (!vcpu && intid <VGIC_NR_PRIVATE_IRQS )return -EINVAL ;
virt/kvm/arm/vgic/vgic.h +8 -0
@@ -53,6 +53,7 @@ int vgic_v2_cpuif_uaccess(struct kvm_vcpu *vcpu, bool is_write, int offset, u32 *val);void vgic_v2_set_vmcr (struct kvm_vcpu *vcpu,struct vgic_vmcr *vmcr);void vgic_v2_get_vmcr (struct kvm_vcpu *vcpu,struct vgic_vmcr *vmcr); +void vgic_v2_enable (struct kvm_vcpu *vcpu);int vgic_v2_probe (const struct gic_kvm_info *info);int vgic_register_dist_iodev (struct kvm *kvm,gpa_t dist_base_address,enum vgic_type );
@@ -65,6 +66,7 @@ void vgic_v3_clear_lr(struct kvm_vcpu *vcpu, int lr);void vgic_v3_set_underflow (struct kvm_vcpu *vcpu);void vgic_v3_set_vmcr (struct kvm_vcpu *vcpu,struct vgic_vmcr *vmcr);void vgic_v3_get_vmcr (struct kvm_vcpu *vcpu,struct vgic_vmcr *vmcr); +void vgic_v3_enable (struct kvm_vcpu *vcpu);int vgic_v3_probe (const struct gic_kvm_info *info);int vgic_register_redist_iodevs (struct kvm *kvm,gpa_t dist_base_address);#else
@@ -99,6 +101,10 @@ void vgic_v3_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr) { } +static inline void vgic_v3_enable (struct kvm_vcpu *vcpu) +{ +} +static inline int vgic_v3_probe (const struct gic_kvm_info *info) {return -ENODEV ;
@@ -112,5 +118,7 @@ static inline int vgic_register_redist_iodevs(struct kvm *kvm,#endif void kvm_register_vgic_device (unsigned long type); +int vgic_lazy_init (struct kvm *kvm); +int vgic_init (struct kvm *kvm);#endif
Signature · 1 line
2.8.2
PATCH v4 52/56 KVM: arm/arm64: vgic-new: vgic_init: implement map_resources
In reply to Andre PrzywaraFrom: Eric Auger <[email protected]>
map_resources is the last initialization step. It is executed on first VCPU run. At that stage the code checks that userspace has provided the base addresses for the relevant VGIC regions, which depend on the type of VGIC that is exposed to the guest. Also we check if the two regions overlap.
If the checks succeeded, we register the respective register frames with the kvm_io_bus framework.
If we emulate a GICv2, the function also forces vgic_init execution if it has not been executed yet. Also we map the virtual GIC CPU interface onto the guest's CPU interface.
---
Changelog v3 .. v4:
- add overlap check (moved from kvm_vgic_addr())
- reword commit message
- improve comment
include/kvm/vgic/vgic.h | 1 + virt/kvm/arm/vgic/vgic-init.c | 28 +++++++++++++++++ virt/kvm/arm/vgic/vgic-v2.c | 69 +++++++++++++++++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic-v3.c | 71 +++++++++++++++++++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic.h | 7 +++++ 5 files changed, 176 insertions(+)
include/kvm/vgic/vgic.h +1 -0
@@ -201,6 +201,7 @@ int kvm_vgic_create(struct kvm *kvm, u32 type);void kvm_vgic_destroy (struct kvm *kvm);void kvm_vgic_vcpu_early_init (struct kvm_vcpu *vcpu);void kvm_vgic_vcpu_destroy (struct kvm_vcpu *vcpu); +int kvm_vgic_map_resources (struct kvm *kvm);int kvm_vgic_hyp_init (void ); int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int intid,
virt/kvm/arm/vgic/vgic-init.c +28 -0
@@ -323,6 +323,34 @@ int vgic_lazy_init(struct kvm *kvm)return ret; } +/* RESOURCE MAPPING */ + +/** +* Map the MMIO regions depending on the VGIC model exposed to the guest +* called on the first VCPU run. +* Also map the virtual CPU interface into the VM. +* v2/v3 derivatives call vgic_init if not already done. +* vgic_ready() returns true if this function has succeeded. +* @kvm: kvm struct pointer +*/ +int kvm_vgic_map_resources (struct kvm *kvm) +{ +struct vgic_dist *dist = &kvm->arch.vgic; +int ret =0 ; + +mutex_lock (&kvm->lock); +if (!irqchip_in_kernel (kvm)) + goto out; + +if (dist->vgic_model ==KVM_DEV_TYPE_ARM_VGIC_V2 ) + ret =vgic_v2_map_resources (kvm); +else + ret =vgic_v3_map_resources (kvm); +out: +mutex_unlock (&kvm->lock); +return ret; +} +/* GENERIC PROBE */ static void vgic_init_maintenance_interrupt (void *info)
virt/kvm/arm/vgic/vgic-v2.c +69 -0
75 lines not on this page · raw message
virt/kvm/arm/vgic/vgic-v3.c +71 -0
77 lines not on this page · raw message
virt/kvm/arm/vgic/vgic.h +7 -0
@@ -55,6 +55,7 @@ void vgic_v2_set_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr);void vgic_v2_get_vmcr (struct kvm_vcpu *vcpu,struct vgic_vmcr *vmcr);void vgic_v2_enable (struct kvm_vcpu *vcpu);int vgic_v2_probe (const struct gic_kvm_info *info); +int vgic_v2_map_resources (struct kvm *kvm);int vgic_register_dist_iodev (struct kvm *kvm,gpa_t dist_base_address,enum vgic_type );
@@ -68,6 +69,7 @@ void vgic_v3_set_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr);void vgic_v3_get_vmcr (struct kvm_vcpu *vcpu,struct vgic_vmcr *vmcr);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);#else static inline void vgic_v3_process_maintenance (struct kvm_vcpu *vcpu)
@@ -110,6 +112,11 @@ static inline int vgic_v3_probe(const struct gic_kvm_info *info)return -ENODEV ; } +static inline int vgic_v3_map_resources (struct kvm *kvm) +{ +return -ENODEV ; +} +static inline int vgic_register_redist_iodevs (struct kvm *kvm,gpa_t dist_base_address) {
Signature · 1 line
2.8.2
PATCH v4 53/56 KVM: arm/arm64: vgic-new: Add vgic_v2/v3_enable
In reply to Andre PrzywaraFrom: Eric Auger <[email protected]>
Enable the VGIC operation by properly initialising the registers in the hypervisor GIC interface.
--- virt/kvm/arm/vgic/vgic-v2.c | 11 ++++++++++- virt/kvm/arm/vgic/vgic-v3.c | 23 ++++++++++++++++++++++- 2 files changed, 32 insertions(+), 2 deletions(-)
virt/kvm/arm/vgic/vgic-v2.c +10 -1
@@ -206,9 +206,18 @@ void vgic_v2_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcrp)GICH_VMCR_PRIMASK_SHIFT ; } -/* not yet implemented */ void vgic_v2_enable (struct kvm_vcpu *vcpu) { +/* +* By forcing VMCR to zero, the GIC will restore the binary +* points to their reset values. Anything else resets to zero +* anyway. +*/ + vcpu->arch.vgic_cpu.vgic_v2.vgic_vmcr =0 ; + vcpu->arch.vgic_cpu.vgic_v2.vgic_elrsr = ~0 ; + +/* Get the show on the road... */ + vcpu->arch.vgic_cpu.vgic_v2.vgic_hcr =GICH_HCR_EN ; }/* check for overlapping regions and for regions crossing the end of memory */
virt/kvm/arm/vgic/vgic-v3.c +22 -1
@@ -186,9 +186,30 @@ void vgic_v3_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcrp) vmcrp->pmr = (vmcr &ICH_VMCR_PMR_MASK ) >>ICH_VMCR_PMR_SHIFT ; } -/* not yet implemented */ void vgic_v3_enable (struct kvm_vcpu *vcpu) { +struct vgic_v3_cpu_if *vgic_v3 = &vcpu->arch.vgic_cpu.vgic_v3; + +/* +* By forcing VMCR to zero, the GIC will restore the binary +* points to their reset values. Anything else resets to zero +* anyway. +*/ + vgic_v3->vgic_vmcr =0 ; + vgic_v3->vgic_elrsr = ~0 ; + +/* +* If we are emulating a GICv3, we do it in an non-GICv2-compatible +* way, so we force SRE to 1 to demonstrate this to the guest. +* This goes with the spec allowing the value to be RAO/WI. +*/ +if (vcpu->kvm->arch.vgic.vgic_model ==KVM_DEV_TYPE_ARM_VGIC_V3 ) + vgic_v3->vgic_sre =ICC_SRE_EL1_SRE ; +else + vgic_v3->vgic_sre =0 ; + +/* Get the show on the road... */ + vgic_v3->vgic_hcr =ICH_HCR_EN ; }/* check for overlapping regions and for regions crossing the end of memory */
Signature · 1 line
2.8.2
PATCH v4 54/56 KVM: arm/arm64: vgic-new: Wire up irqfd injection
In reply to Andre PrzywaraConnect to the new VGIC to the irqfd framework, so that we can inject IRQs.
GSI routing and MSI routing is not yet implemented.
--- virt/kvm/arm/vgic/vgic-irqfd.c | 52 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 virt/kvm/arm/vgic/vgic-irqfd.c
virt/kvm/arm/vgic/vgic-irqfd.c new file +52 -0
@@ -0,0 +1,52 @@ +/* +* Copyright (C) 2015, 2016 ARM Ltd. +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include <linux/kvm.h> +#include <linux/kvm_host.h> +#include <trace/events/kvm.h> + +int kvm_irq_map_gsi (struct kvm *kvm, +struct kvm_kernel_irq_routing_entry *entries, +int gsi) +{ +return 0 ; +} + +int kvm_irq_map_chip_pin (struct kvm *kvm,unsigned int irqchip, +unsigned int pin) +{ +return pin; +} + +int kvm_set_irq (struct kvm *kvm,int irq_source_id, +u32 irq,int level,bool line_status) +{ +unsigned int spi = irq +VGIC_NR_PRIVATE_IRQS ; + +trace_kvm_set_irq (irq, level, irq_source_id); + +BUG_ON (!vgic_initialized (kvm)); + +return kvm_vgic_inject_irq (kvm,0 , spi, level); +} + +/* MSI not implemented yet */ +int kvm_set_msi (struct kvm_kernel_irq_routing_entry *e, +struct kvm *kvm,int irq_source_id, +int level,bool line_status) +{ +return 0 ; +}
Signature · 1 line
2.8.2
PATCH v4 55/56 KVM: arm/arm64: vgic-new: implement mapped IRQ handling
In reply to Andre PrzywaraWe now store the mapped hardware IRQ number in our struct, so we don't need the irq_phys_map for the new VGIC. Implement the hardware IRQ mapping on top of the reworked arch timer interface.
---
Changelog RFC..v1:
- adapt to new arch_timer mapped IRQ interface
- implement inject_mapped_irq() as a macro (since it is identical to the "un-mapped" IRQ implementation)
Changelog v1 .. v2:
- replace inject_mapped_irq() macro with a separate implementation, which only allows mapped IRQs to be injected via this interface
include/kvm/vgic/vgic.h | 5 +++++ virt/kvm/arm/vgic/vgic.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+)
include/kvm/vgic/vgic.h +5 -0
@@ -206,6 +206,11 @@ int kvm_vgic_hyp_init(void);int kvm_vgic_inject_irq (struct kvm *kvm,int cpuid,unsigned int intid,bool level); +int kvm_vgic_inject_mapped_irq (struct kvm *kvm,int cpuid,unsigned int intid, +bool level); +int kvm_vgic_map_phys_irq (struct kvm_vcpu *vcpu,u32 virt_irq,u32 phys_irq); +int kvm_vgic_unmap_phys_irq (struct kvm_vcpu *vcpu,unsigned int virt_irq); +bool kvm_vgic_map_is_active (struct kvm_vcpu *vcpu,unsigned int virt_irq);int kvm_vgic_vcpu_pending_irq (struct kvm_vcpu *vcpu);
virt/kvm/arm/vgic/vgic.c +50 -0
59 lines not on this page · raw message
Signature · 1 line
2.8.2
v4 cover KVM: arm/arm64: Rework virtual GIC emulation
In reply to Andre PrzywaraHi,
a new update for the new VGIC series.
A lot of fixes this time: comments fixed, clarified and updated, some no longer needed code pieces from the old VGIC have been removed. The MMIO dispatching has been improved to allow only the access types that are mandatory in the spec, so 32-bit accesses only for most registers with some additionally accessible by byte or 64-bit accesses as well. This allows for some simplification in many handler functions. The vgic_queue_irq_unlock() function was reworked a bit and is now used by the set-active handler as well. The clear-active handler has been extended to basically halt the guest and make sure we clear the respective active bit in the LR (and not only in our structure). There is a new patch introducing the KVM core bits of this feature.
Also we now detect when the redistributor and distributor regions overlap and complain about it.
As this series is now based on kvmarm/next, we also gained the firmware independent probing feature, which is in turn also used in this series. This should enable ACPI machines.
A more detailed changelog can be found in the respective patches.
As this new VGIC emulation will probably become the default very shortly, please test it full steam on whatever setup you have access to. Any reports are warmly welcome.
This series is now based on kvmarm/next, which will be merged shortly into Linus' kernel.
A git tree containing this series can be found on linux-arm.org:
git://linux-arm.org/linux-ap.git branch: vgic-new/v4
http://www.linux-arm.org/git?p=linux-ap.git;a=log;h=refs/heads/vgic-new/v4
Cheers,
Andre.
===========================
This series is a joint effort to re-implement KVM's GIC emulation. Many patches as in here have been effectively authored by multiple people and authorship isn't clear in every case, especially since many of the patches have been stashed together or splitted apart in the process. So take the Author: tag with a grain of salt.
While the current VGIC implementation is centered around providing efficient MMIO emulation, the hot path for most guests is actually the guest entry and exit, which currently is rather costly. Also the existing emulation has a global distributor lock, which quickly becomes a bottleneck once the number of VCPUs increases. Additionally the emulation was originally designed for GICv2, adding GICv3 ITS emulation support to this proved to be rather painful. Last, but not least the existing code became less and less maintainable, with many special cases handled explicitly.
The new implementation is build around a struct vgic_irq data structure, which holds all information about a virtual interrupt. Interruts which should be injected are held in a per-VCPU list, this make the entry/exit path much more efficient. Also the new structure allows to have more fine grained locking - per IRQ and per VCPU - getting rid of the global distributor lock.
As a result of the new design ITS emulation fits in more nicely, the respective code will be provided as a follow-up series.
This series implements the same feature set as the existing emulation, as a goodie we now implement priorities correctly.
To allow an easy transition with good test coverage, but still maintain stability, both implementations live side by side, selectable via a Kconfig option. The default is the new implementation.
If this code proves to be reliable, we will later remove the current implementation with an extra patch set.
Please have a look at the series, review it and give the code some serious testing (and possibly debugging). All feedback is appreciated.
Cheers,
Andre.
Andre Przywara (29):
KVM: arm/arm64: vgic: streamline vgic_update_irq_pending() interface
KVM: arm/arm64: vgic: avoid map in kvm_vgic_inject_mapped_irq()
KVM: arm/arm64: vgic: avoid map in kvm_vgic_map_is_active()
KVM: arm/arm64: vgic: avoid map in kvm_vgic_unmap_phys_irq()
KVM: arm/arm64: arch_timer: Remove irq_phys_map
KVM: arm/arm64: vgic: Remove irq_phys_map from interface
KVM: arm/arm64: pmu: abstract access to number of SPIs
KVM: arm/arm64: move GICv2 emulation defines into arm-gic-v3.h
KVM: arm/arm64: vgic-new: Add GICv2 MMIO handling framework
KVM: arm/arm64: vgic-new: Add ENABLE registers handlers
KVM: arm/arm64: vgic-new: Add PENDING registers handlers
KVM: arm/arm64: vgic-new: Add ACTIVE registers handlers
KVM: arm/arm64: vgic-new: Add PRIORITY registers handlers
KVM: arm/arm64: vgic-new: Add CONFIG registers handlers
KVM: arm/arm64: vgic-new: Add TARGET registers handlers
KVM: arm/arm64: vgic-new: Add SGIR register handler
KVM: arm/arm64: vgic-new: Add SGIPENDR register handlers
KVM: arm/arm64: vgic-new: Add GICv3 MMIO handling framework
KVM: arm/arm64: vgic-new: Add GICv3 CTLR, IIDR, TYPER handlers
KVM: arm/arm64: vgic-new: Add GICv3 redistributor IIDR and TYPER
handler
KVM: arm/arm64: vgic-new: Add GICv3 IDREGS register handler
KVM: arm/arm64: vgic-new: Add GICv3 IROUTER register handlers
KVM: arm/arm64: vgic-new: Add GICv3 SGI system register trap handler
KVM: arm/arm64: vgic-new: Add userland access to VGIC dist registers
KVM: arm/arm64: vgic-new: Add GICH_VMCR accessors
KVM: arm/arm64: vgic-new: Add userland GIC CPU interface access
KVM: arm/arm64: vgic-new: Wire up irqfd injection
KVM: arm/arm64: vgic-new: implement mapped IRQ handling
KVM: arm/arm64: vgic-new: enable build
Christoffer Dall (10):
KVM: arm/arm64: Remove the IRQ field from struct irq_phys_map
KVM: arm/arm64: Get rid of vgic_cpu->nr_lr
KVM: arm/arm64: Fix MMIO emulation data handling
KVM: arm/arm64: Export mmio_read/write_bus
KVM: arm/arm64: Provide functionality to pause and resume a guest
KVM: arm/arm64: vgic-new: Add data structure definitions
KVM: arm/arm64: vgic-new: Add acccessor to new struct vgic_irq
instance
KVM: arm/arm64: vgic-new: Implement virtual IRQ injection
KVM: arm/arm64: vgic-new: Add IRQ sorting
KVM: arm/arm64: vgic-new: Export register access interface
Eric Auger (12):
KVM: arm/arm64: vgic-new: Implement kvm_vgic_vcpu_pending_irq
KVM: arm/arm64: vgic-new: vgic_kvm_device: KVM device ops registration
KVM: arm/arm64: vgic-new: vgic_kvm_device:
KVM_DEV_ARM_VGIC_GRP_NR_IRQS
KVM: arm/arm64: vgic-new: vgic_kvm_device: KVM_DEV_ARM_VGIC_GRP_CTRL
KVM: arm/arm64: vgic-new: vgic_kvm_device: implement kvm_vgic_addr
KVM: arm/arm64: vgic-new: vgic_kvm_device: KVM_DEV_ARM_VGIC_GRP_ADDR
KVM: arm/arm64: vgic-new: vgic_kvm_device: access to VGIC registers
KVM: arm/arm64: vgic-new: vgic_init: implement kvm_vgic_hyp_init
KVM: arm/arm64: vgic-new: vgic_init: implement vgic_create
KVM: arm/arm64: vgic-new: vgic_init: implement vgic_init
KVM: arm/arm64: vgic-new: vgic_init: implement map_resources
KVM: arm/arm64: vgic-new: Add vgic_v2/v3_enable
Marc Zyngier (5):
KVM: arm/arm64: vgic-new: Add IRQ sync/flush framework
KVM: arm/arm64: vgic-new: Add GICv2 world switch backend
KVM: arm/arm64: vgic-new: Add GICv3 world switch backend
KVM: arm/arm64: vgic-new: Add MMIO handling framework
KVM: arm/arm64: vgic-new: Add CTLR, TYPER and IIDR handlers
arch/arm/include/asm/kvm_host.h | 4 + arch/arm/include/asm/kvm_mmio.h | 3 + arch/arm/kvm/Kconfig | 7 + arch/arm/kvm/Makefile | 11 + arch/arm/kvm/arm.c | 25 +- arch/arm/kvm/mmio.c | 24 +- arch/arm64/include/asm/kvm_host.h | 4 + arch/arm64/include/asm/kvm_mmio.h | 3 + arch/arm64/kvm/Kconfig | 7 + arch/arm64/kvm/Makefile | 12 + include/kvm/arm_arch_timer.h | 3 - include/kvm/arm_vgic.h | 20 +- include/kvm/vgic/vgic.h | 246 +++++++++++++++ include/linux/irqchip/arm-gic-v3.h | 6 + include/linux/irqchip/arm-gic.h | 2 + virt/kvm/arm/arch_timer.c | 42 ++- virt/kvm/arm/hyp/vgic-v2-sr.c | 17 +- virt/kvm/arm/pmu.c | 25 +- virt/kvm/arm/vgic-v2.c | 4 +- virt/kvm/arm/vgic-v3.c | 8 +- virt/kvm/arm/vgic.c | 86 ++--- virt/kvm/arm/vgic/vgic-init.c | 452 ++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic-irqfd.c | 52 +++ virt/kvm/arm/vgic/vgic-kvm-device.c | 426 +++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic-mmio-v2.c | 446 ++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic-mmio-v3.c | 451 ++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic-mmio.c | 499 +++++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic-mmio.h | 138 ++++++++ virt/kvm/arm/vgic/vgic-v2.c | 352 +++++++++++++++++++++ virt/kvm/arm/vgic/vgic-v3.c | 330 +++++++++++++++++++ virt/kvm/arm/vgic/vgic.c | 615 ++++++++++++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic.h | 131 ++++++++ 32 files changed, 4314 insertions(+), 137 deletions(-) create mode 100644 include/kvm/vgic/vgic.h create mode 100644 virt/kvm/arm/vgic/vgic-init.c create mode 100644 virt/kvm/arm/vgic/vgic-irqfd.c create mode 100644 virt/kvm/arm/vgic/vgic-kvm-device.c create mode 100644 virt/kvm/arm/vgic/vgic-mmio-v2.c create mode 100644 virt/kvm/arm/vgic/vgic-mmio-v3.c create mode 100644 virt/kvm/arm/vgic/vgic-mmio.c create mode 100644 virt/kvm/arm/vgic/vgic-mmio.h create mode 100644 virt/kvm/arm/vgic/vgic-v2.c create mode 100644 virt/kvm/arm/vgic/vgic-v3.c create mode 100644 virt/kvm/arm/vgic/vgic.c create mode 100644 virt/kvm/arm/vgic/vgic.h
Signature · 1 line
2.8.2
PATCH v4 1/56 KVM: arm/arm64: vgic: streamline vgic_update_irq_pending() interface
In reply to Andre PrzywaraWe actually don't use the irq_phys_map parameter in vgic_update_irq_pending(), so let's just remove it.
--- virt/kvm/arm/vgic.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
virt/kvm/arm/vgic.c +2 -3
@@ -1521,7 +1521,6 @@ static int vgic_validate_injection(struct kvm_vcpu *vcpu, int irq, int level) }static int vgic_update_irq_pending (struct kvm *kvm,int cpuid, -struct irq_phys_map *map,unsigned int irq_num,bool level) {struct vgic_dist *dist = &kvm->arch.vgic;
@@ -1660,7 +1659,7 @@ int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int irq_num,if (map)return -EINVAL ; -return vgic_update_irq_pending (kvm, cpuid,NULL , irq_num, level); +return vgic_update_irq_pending (kvm, cpuid, irq_num, level); }/**
@@ -1686,7 +1685,7 @@ int kvm_vgic_inject_mapped_irq(struct kvm *kvm, int cpuid,if (ret)return ret; -return vgic_update_irq_pending (kvm, cpuid, map, map->virt_irq, level); +return vgic_update_irq_pending (kvm, cpuid, map->virt_irq, level); }static irqreturn_t vgic_maintenance_handler (int irq,void *data)
Signature · 1 line
2.8.2
PATCH v4 56/56 KVM: arm/arm64: vgic-new: enable build
In reply to Andre PrzywaraNow that the new VGIC implementation has reached feature parity with the old one, add the new files to the build system and add a Kconfig option to switch between the two versions.
We set the default to the new version to get maximum test coverage, in case people experience problems they can switch back to the old behaviour if needed.
---
Changelog v1 .. v2:
- adjust the changed filenames in the Makefiles
- add vgic-mmio-v2.c and vgic-mmio-v3.c to the Makefiles
arch/arm/kvm/Kconfig | 7 +++++++ arch/arm/kvm/Makefile | 11 +++++++++++ arch/arm64/kvm/Kconfig | 7 +++++++ arch/arm64/kvm/Makefile | 12 ++++++++++++ virt/kvm/arm/hyp/vgic-v2-sr.c | 5 +++++ 5 files changed, 42 insertions(+)
arch/arm/kvm/Kconfig +7 -0
@@ -46,6 +46,13 @@ config KVM_ARM_HOST ---help--- Provides host support for ARM processors. +config KVM_NEW_VGIC + bool "New VGIC implementation" + depends on KVM + default y + ---help--- + uses the new VGIC implementation + source drivers/vhost/Kconfig endif # VIRTUALIZATION
arch/arm/kvm/Makefile +11 -0
@@ -21,7 +21,18 @@ obj-$(CONFIG_KVM_ARM_HOST) += hyp/ obj-y += kvm-arm.o init.o interrupts.o obj-y += arm.o handle_exit.o guest.o mmu.o emulate.o reset.o obj-y += coproc.o coproc_a15.o coproc_a7.o mmio.o psci.o perf.o + +ifeq ($(CONFIG_KVM_NEW_VGIC),y) +obj-y += $(KVM)/arm/vgic/vgic.o +obj-y += $(KVM)/arm/vgic/vgic-init.o +obj-y += $(KVM)/arm/vgic/vgic-irqfd.o +obj-y += $(KVM)/arm/vgic/vgic-v2.o +obj-y += $(KVM)/arm/vgic/vgic-mmio.o +obj-y += $(KVM)/arm/vgic/vgic-mmio-v2.o +obj-y += $(KVM)/arm/vgic/vgic-kvm-device.o +else obj-y += $(KVM)/arm/vgic.o obj-y += $(KVM)/arm/vgic-v2.o obj-y += $(KVM)/arm/vgic-v2-emul.o +endif obj-y += $(KVM)/arm/arch_timer.o
arch/arm64/kvm/Kconfig +7 -0
@@ -54,6 +54,13 @@ config KVM_ARM_PMU Adds support for a virtual Performance Monitoring Unit (PMU) in virtual machines. +config KVM_NEW_VGIC + bool "New VGIC implementation" + depends on KVM + default y + ---help--- + uses the new VGIC implementation + source drivers/vhost/Kconfig endif # VIRTUALIZATION
arch/arm64/kvm/Makefile +12 -0
@@ -20,10 +20,22 @@ kvm-$(CONFIG_KVM_ARM_HOST) += emulate.o inject_fault.o regmap.o kvm-$(CONFIG_KVM_ARM_HOST) += hyp.o hyp-init.o handle_exit.o kvm-$(CONFIG_KVM_ARM_HOST) += guest.o debug.o reset.o sys_regs.o sys_regs_generic_v8.o +ifeq ($(CONFIG_KVM_NEW_VGIC),y) +kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/vgic/vgic.o +kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/vgic/vgic-init.o +kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/vgic/vgic-irqfd.o +kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/vgic/vgic-v2.o +kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/vgic/vgic-v3.o +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 +else kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/vgic.o kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/vgic-v2.o kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/vgic-v2-emul.o kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/vgic-v3.o kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/vgic-v3-emul.o +endif kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/arch_timer.o kvm-$(CONFIG_KVM_ARM_PMU) += $(KVM)/arm/pmu.o
virt/kvm/arm/hyp/vgic-v2-sr.c +5 -0
@@ -21,7 +21,12 @@#include <asm/kvm_hyp.h> +#ifdef CONFIG_KVM_NEW_VGIC +extern struct vgic_global kvm_vgic_global_state; +#define vgic_v2_params kvm_vgic_global_state +#else extern struct vgic_params vgic_v2_params; +#endif static void __hyp_text save_maint_int_state(struct kvm_vcpu *vcpu,void __iomem *base)
Signature · 1 line
2.8.2
PATCH v4 3/56 KVM: arm/arm64: vgic: avoid map in kvm_vgic_map_is_active()
In reply to Andre PrzywaraFor getting the active state of a mapped IRQ, we actually only need the virtual IRQ number, not the pointer to the mapping entry. Pass the virtual IRQ number from the arch timer to the VGIC directly.
---
Changelog v1 .. v2:
- rewrap phys_active determination to fit in 80 characters
include/kvm/arm_vgic.h | 2 +- virt/kvm/arm/arch_timer.c | 6 ++---- virt/kvm/arm/vgic.c | 6 +++--- 3 files changed, 6 insertions(+), 8 deletions(-)
include/kvm/arm_vgic.h +1 -1
@@ -348,7 +348,7 @@ int kvm_vgic_vcpu_pending_irq(struct kvm_vcpu *vcpu);struct irq_phys_map *kvm_vgic_map_phys_irq (struct kvm_vcpu *vcpu,int virt_irq,int irq);int kvm_vgic_unmap_phys_irq (struct kvm_vcpu *vcpu,struct irq_phys_map *map); -bool kvm_vgic_map_is_active (struct kvm_vcpu *vcpu,struct irq_phys_map *map); +bool kvm_vgic_map_is_active (struct kvm_vcpu *vcpu,unsigned int virt_irq);#define irqchip_in_kernel (k) (!!((k)->arch.vgic.in_kernel))#define vgic_initialized (k) (!!((k)->arch.vgic.nr_cpus))
virt/kvm/arm/arch_timer.c +2 -4
@@ -274,10 +274,8 @@ void kvm_timer_flush_hwstate(struct kvm_vcpu *vcpu)* to ensure that hardware interrupts from the timer triggers a guest * exit. */ -if (timer->irq.level ||kvm_vgic_map_is_active (vcpu, timer->map)) - phys_active = true; -else - phys_active = false; + phys_active = timer->irq.level || +kvm_vgic_map_is_active (vcpu, timer->map->virt_irq);/* * We want to avoid hitting the (re)distributor as much as
virt/kvm/arm/vgic.c +3 -3
@@ -1102,18 +1102,18 @@ static bool dist_active_irq(struct kvm_vcpu *vcpu)return test_bit (vcpu->vcpu_id, dist->irq_active_on_cpu); } -bool kvm_vgic_map_is_active (struct kvm_vcpu *vcpu,struct irq_phys_map *map) +bool kvm_vgic_map_is_active (struct kvm_vcpu *vcpu,unsigned int virt_irq) {int i;for (i =0 ; i < vcpu->arch.vgic_cpu.nr_lr; i++) {struct vgic_lr vlr =vgic_get_lr (vcpu, i); -if (vlr.irq == map->virt_irq && vlr.state &LR_STATE_ACTIVE ) +if (vlr.irq == virt_irq && vlr.state &LR_STATE_ACTIVE )return true; } -return vgic_irq_is_active (vcpu, map->virt_irq); +return vgic_irq_is_active (vcpu, virt_irq); }/*
Signature · 1 line
2.8.2
PATCH v4 4/56 KVM: arm/arm64: vgic: avoid map in kvm_vgic_unmap_phys_irq()
In reply to Andre Przywarakvm_vgic_unmap_phys_irq() only needs the virtual IRQ number, so let's just pass that between the arch timer and the VGIC to get rid of the irq_phys_map pointer.
--- include/kvm/arm_vgic.h | 2 +- virt/kvm/arm/arch_timer.c | 2 +- virt/kvm/arm/vgic.c | 11 ++++------- 3 files changed, 6 insertions(+), 9 deletions(-)
include/kvm/arm_vgic.h +1 -1
@@ -347,7 +347,7 @@ void vgic_v3_dispatch_sgi(struct kvm_vcpu *vcpu, u64 reg);int kvm_vgic_vcpu_pending_irq (struct kvm_vcpu *vcpu);struct irq_phys_map *kvm_vgic_map_phys_irq (struct kvm_vcpu *vcpu,int virt_irq,int irq); -int kvm_vgic_unmap_phys_irq (struct kvm_vcpu *vcpu,struct irq_phys_map *map); +int kvm_vgic_unmap_phys_irq (struct kvm_vcpu *vcpu,unsigned int virt_irq);bool kvm_vgic_map_is_active (struct kvm_vcpu *vcpu,unsigned int virt_irq);#define irqchip_in_kernel (k) (!!((k)->arch.vgic.in_kernel))
virt/kvm/arm/arch_timer.c +1 -1
@@ -486,7 +486,7 @@ void kvm_timer_vcpu_terminate(struct kvm_vcpu *vcpu)timer_disarm (timer);if (timer->map) -kvm_vgic_unmap_phys_irq (vcpu, timer->map); +kvm_vgic_unmap_phys_irq (vcpu, timer->map->virt_irq); }void kvm_timer_enable (struct kvm *kvm)
virt/kvm/arm/vgic.c +4 -7
@@ -1812,25 +1812,22 @@ static void vgic_free_phys_irq_map_rcu(struct rcu_head *rcu)/** * kvm_vgic_unmap_phys_irq - Remove a virtual to physical IRQ mapping * @vcpu: The VCPU pointer -* @map: The pointer to a mapping obtained through kvm_vgic_map_phys_irq +* @virt_irq: The virtual IRQ number to be unmapped * * Remove an existing mapping between virtual and physical interrupts. */ -int kvm_vgic_unmap_phys_irq (struct kvm_vcpu *vcpu,struct irq_phys_map *map) +int kvm_vgic_unmap_phys_irq (struct kvm_vcpu *vcpu,unsigned int virt_irq) {struct vgic_dist *dist = &vcpu->kvm->arch.vgic;struct irq_phys_map_entry *entry;struct list_head *root; -if (!map) -return -EINVAL ; - - root =vgic_get_irq_phys_map_list (vcpu, map->virt_irq); + root =vgic_get_irq_phys_map_list (vcpu, virt_irq);spin_lock (&dist->irq_phys_map_lock);list_for_each_entry (entry, root, entry) { -if (&entry->map == map) { +if (entry->map.virt_irq == virt_irq) {list_del_rcu (&entry->entry);call_rcu (&entry->rcu, vgic_free_phys_irq_map_rcu);break ;
Signature · 1 line
2.8.2
PATCH v4 6/56 KVM: arm/arm64: arch_timer: Remove irq_phys_map
In reply to Andre PrzywaraNow that the interface between the arch timer and the VGIC does not require passing the irq_phys_map entry pointer anymore, let's remove it from the virtual arch timer and use the virtual IRQ number instead directly.
The remaining pointer returned by kvm_vgic_map_phys_irq() will be removed in the following patch.
---
Changelog v1 .. v2:
- remove extra virt_irq member from struct, instead use irq.irq directly
Changelog v2 .. v3:
- remove bogus validity check from kvm_vgic_unmap_phys_irq()
include/kvm/arm_arch_timer.h | 3 --- virt/kvm/arm/arch_timer.c | 10 ++++------ 2 files changed, 4 insertions(+), 9 deletions(-)
include/kvm/arm_arch_timer.h +0 -3
@@ -53,9 +53,6 @@ struct arch_timer_cpu {/* Timer IRQ */ struct kvm_irq_level irq; -/* VGIC mapping */ -struct irq_phys_map *map; -/* Active IRQ state caching */ bool active_cleared_last; };
virt/kvm/arm/arch_timer.c +4 -6
@@ -175,10 +175,10 @@ static void kvm_timer_update_irq(struct kvm_vcpu *vcpu, bool new_level) timer->active_cleared_last = false; timer->irq.level = new_level; -trace_kvm_timer_update_irq (vcpu->vcpu_id, timer->map->virt_irq, +trace_kvm_timer_update_irq (vcpu->vcpu_id, timer->irq.irq, timer->irq.level); ret =kvm_vgic_inject_mapped_irq (vcpu->kvm, vcpu->vcpu_id, - timer->map->virt_irq, + timer->irq.irq, timer->irq.level);WARN_ON (ret); }
@@ -276,7 +276,7 @@ void kvm_timer_flush_hwstate(struct kvm_vcpu *vcpu)* exit. */ phys_active = timer->irq.level || -kvm_vgic_map_is_active (vcpu, timer->map->virt_irq); +kvm_vgic_map_is_active (vcpu, timer->irq.irq);/* * We want to avoid hitting the (re)distributor as much as
@@ -378,7 +378,6 @@ int kvm_timer_vcpu_reset(struct kvm_vcpu *vcpu,if (WARN_ON (IS_ERR (map)))return PTR_ERR (map); - timer->map = map;return 0 ; }
@@ -504,8 +503,7 @@ void kvm_timer_vcpu_terminate(struct kvm_vcpu *vcpu)struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;timer_disarm (timer); -if (timer->map) -kvm_vgic_unmap_phys_irq (vcpu, timer->map->virt_irq); +kvm_vgic_unmap_phys_irq (vcpu, timer->irq.irq); }void kvm_timer_enable (struct kvm *kvm)
Signature · 1 line
2.8.2
PATCH v4 7/56 KVM: arm/arm64: vgic: Remove irq_phys_map from interface
In reply to Andre PrzywaraNow that the virtual arch timer does not care about the irq_phys_map anymore, let's rework kvm_vgic_map_phys_irq() to return an error value instead. Any reference to that mapping can later be done by passing the correct combination of VCPU and virtual IRQ number. This makes the irq_phys_map handling completely private to the VGIC code.
---
Changelog v1 .. v2:
- adaptations to changes in previous patches (removal of virt_irq)
Changelog v2 .. v3:
- simplify return in kvm_timer_vcpu_reset()
include/kvm/arm_vgic.h | 3 +-- virt/kvm/arm/arch_timer.c | 7 +------ virt/kvm/arm/vgic.c | 15 +++++++-------- 3 files changed, 9 insertions(+), 16 deletions(-)
include/kvm/arm_vgic.h +1 -2
@@ -344,8 +344,7 @@ int kvm_vgic_inject_mapped_irq(struct kvm *kvm, int cpuid, unsigned int virt_irq, bool level);void vgic_v3_dispatch_sgi (struct kvm_vcpu *vcpu,u64 reg);int kvm_vgic_vcpu_pending_irq (struct kvm_vcpu *vcpu); -struct irq_phys_map *kvm_vgic_map_phys_irq (struct kvm_vcpu *vcpu, -int virt_irq,int phys_irq); +int kvm_vgic_map_phys_irq (struct kvm_vcpu *vcpu,int virt_irq,int phys_irq);int kvm_vgic_unmap_phys_irq (struct kvm_vcpu *vcpu,unsigned int virt_irq);bool kvm_vgic_map_is_active (struct kvm_vcpu *vcpu,unsigned int virt_irq);
virt/kvm/arm/arch_timer.c +1 -6
@@ -333,7 +333,6 @@ int kvm_timer_vcpu_reset(struct kvm_vcpu *vcpu, const struct kvm_irq_level *irq) {struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; -struct irq_phys_map *map;struct irq_desc *desc;struct irq_data *data;int phys_irq;
@@ -374,11 +373,7 @@ int kvm_timer_vcpu_reset(struct kvm_vcpu *vcpu,* Tell the VGIC that the virtual interrupt is tied to a * physical interrupt. We do that once per VCPU. */ - map =kvm_vgic_map_phys_irq (vcpu, irq->irq, phys_irq); -if (WARN_ON (IS_ERR (map))) -return PTR_ERR (map); - -return 0 ; +return kvm_vgic_map_phys_irq (vcpu, irq->irq, phys_irq); }void kvm_timer_vcpu_init (struct kvm_vcpu *vcpu)
virt/kvm/arm/vgic.c +7 -8
@@ -1719,21 +1719,20 @@ static struct list_head *vgic_get_irq_phys_map_list(struct kvm_vcpu *vcpu,* the physical interrupt represented by @phys_irq. This mapping can be * established multiple times as long as the parameters are the same. * -* Returns a valid pointer on success, and an error pointer otherwise +* Returns 0 on success or an error value otherwise. */ -struct irq_phys_map *kvm_vgic_map_phys_irq (struct kvm_vcpu *vcpu, -int virt_irq,int phys_irq) +int kvm_vgic_map_phys_irq (struct kvm_vcpu *vcpu,int virt_irq,int phys_irq) {struct vgic_dist *dist = &vcpu->kvm->arch.vgic;struct list_head *root =vgic_get_irq_phys_map_list (vcpu, virt_irq);struct irq_phys_map *map;struct irq_phys_map_entry *entry; - +int ret =0 ;/* Create a new mapping */ entry =kzalloc (sizeof (*entry),GFP_KERNEL );if (!entry) -return ERR_PTR (-ENOMEM ); +return -ENOMEM ;spin_lock (&dist->irq_phys_map_lock);
@@ -1742,7 +1741,7 @@ struct irq_phys_map *kvm_vgic_map_phys_irq(struct kvm_vcpu *vcpu,if (map) {/* Make sure this mapping matches */ if (map->phys_irq != phys_irq) - map =ERR_PTR (-EINVAL ); + ret = -EINVAL ;/* Found an existing, valid mapping */ goto out;
@@ -1758,9 +1757,9 @@ out:spin_unlock (&dist->irq_phys_map_lock);/* If we've found a hit in the existing list, free the useless * entry */ -if (IS_ERR (map) || map != &entry->map) +if (ret || map != &entry->map)kfree (entry); -return map; +return ret; } static struct irq_phys_map *vgic_irq_map_search(struct kvm_vcpu *vcpu,
Signature · 1 line
2.8.2
PATCH v4 8/56 KVM: arm/arm64: Get rid of vgic_cpu->nr_lr
In reply to Andre PrzywaraFrom: Christoffer Dall <[email protected]>
The number of list registers is a property of the underlying system, not of emulated VGIC CPU interface.
As we are about to move this variable to global state in the new vgic for clarity, move it from the legacy implementation as well to make the merge of the new code easier.
--- include/kvm/arm_vgic.h | 3 --- virt/kvm/arm/hyp/vgic-v2-sr.c | 12 +++++++----- virt/kvm/arm/vgic-v2.c | 4 +++- virt/kvm/arm/vgic.c | 12 ++---------- 4 files changed, 12 insertions(+), 19 deletions(-)
include/kvm/arm_vgic.h +0 -3
@@ -304,9 +304,6 @@ struct vgic_cpu {unsigned long *active_shared;unsigned long *pend_act_shared; -/* Number of list registers on this CPU */ -int nr_lr; -/* CPU vif control registers for world switch */ union {struct vgic_v2_cpu_if vgic_v2;
virt/kvm/arm/hyp/vgic-v2-sr.c +7 -5
@@ -21,11 +21,13 @@#include <asm/kvm_hyp.h> +extern struct vgic_params vgic_v2_params; + static void __hyp_text save_maint_int_state(struct kvm_vcpu *vcpu,void __iomem *base) {struct vgic_v2_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v2; -int nr_lr = vcpu->arch.vgic_cpu.nr_lr; +int nr_lr = (kern_hyp_va (&vgic_v2_params))->nr_lr;u32 eisr0, eisr1;int i;bool expect_mi;
@@ -67,7 +69,7 @@ static void __hyp_text save_maint_int_state(struct kvm_vcpu *vcpu, static void __hyp_text save_elrsr(struct kvm_vcpu *vcpu, void __iomem *base) {struct vgic_v2_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v2; -int nr_lr = vcpu->arch.vgic_cpu.nr_lr; +int nr_lr = (kern_hyp_va (&vgic_v2_params))->nr_lr;u32 elrsr0, elrsr1; elrsr0 =readl_relaxed (base +GICH_ELRSR0 );
@@ -86,7 +88,7 @@ static void __hyp_text save_elrsr(struct kvm_vcpu *vcpu, void __iomem *base) static void __hyp_text save_lrs(struct kvm_vcpu *vcpu, void __iomem *base) {struct vgic_v2_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v2; -int nr_lr = vcpu->arch.vgic_cpu.nr_lr; +int nr_lr = (kern_hyp_va (&vgic_v2_params))->nr_lr;int i;for (i =0 ; i < nr_lr; i++) {
@@ -141,13 +143,13 @@ void __hyp_text __vgic_v2_restore_state(struct kvm_vcpu *vcpu)struct vgic_v2_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v2;struct vgic_dist *vgic = &kvm->arch.vgic;void __iomem *base =kern_hyp_va (vgic->vctrl_base); -int i, nr_lr; +int nr_lr = (kern_hyp_va (&vgic_v2_params))->nr_lr; +int i;u64 live_lrs =0 ;if (!base)return ; - nr_lr = vcpu->arch.vgic_cpu.nr_lr;for (i =0 ; i < nr_lr; i++)if (cpu_if->vgic_lr[i] &GICH_LR_STATE )
virt/kvm/arm/vgic-v2.c +3 -1
@@ -171,7 +171,7 @@ static const struct vgic_ops vgic_v2_ops = { .enable = vgic_v2_enable, }; -static struct vgic_params vgic_v2_params; +struct vgic_params __section(.hyp.text) vgic_v2_params;static void vgic_cpu_init_lrs (void *params) {
@@ -201,6 +201,8 @@ int vgic_v2_probe(const struct gic_kvm_info *gic_kvm_info,const struct resource *vctrl_res = &gic_kvm_info->vctrl;const struct resource *vcpu_res = &gic_kvm_info->vcpu; +memset (vgic,0 ,sizeof (*vgic)); +if (!gic_kvm_info->maint_irq) {kvm_err ("error getting vgic maintenance irq\n" ); ret = -ENXIO ;
virt/kvm/arm/vgic.c +2 -10
@@ -690,12 +690,11 @@ bool vgic_handle_cfg_reg(u32 *reg, struct kvm_exit_mmio *mmio,*/ void vgic_unqueue_irqs (struct kvm_vcpu *vcpu) { -struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;u64 elrsr =vgic_get_elrsr (vcpu);unsigned long *elrsr_ptr =u64_to_bitmask (&elrsr);int i; -for_each_clear_bit (i, elrsr_ptr, vgic_cpu->nr_lr) { +for_each_clear_bit (i, elrsr_ptr, vgic->nr_lr) {struct vgic_lr lr =vgic_get_lr (vcpu, i);/*
@@ -1106,7 +1105,7 @@ bool kvm_vgic_map_is_active(struct kvm_vcpu *vcpu, unsigned int virt_irq) {int i; -for (i =0 ; i < vcpu->arch.vgic_cpu.nr_lr; i++) { +for (i =0 ; i < vgic->nr_lr; i++) {struct vgic_lr vlr =vgic_get_lr (vcpu, i);if (vlr.irq == virt_irq && vlr.state &LR_STATE_ACTIVE )
@@ -1866,13 +1865,6 @@ static int vgic_vcpu_init_maps(struct kvm_vcpu *vcpu, int nr_irqs)return -ENOMEM ; } -/* -* Store the number of LRs per vcpu, so we don't have to go -* all the way to the distributor structure to find out. Only -* assembly code should use this one. -*/ - vgic_cpu->nr_lr = vgic->nr_lr; -return 0 ; }
Signature · 1 line
2.8.2
PATCH v4 9/56 KVM: arm/arm64: Fix MMIO emulation data handling
In reply to Andre PrzywaraFrom: Christoffer Dall <[email protected]>
When the kernel was handling a guest MMIO read access internally, we need to copy the emulation result into the run->mmio structure in order for the kvm_handle_mmio_return() function to pick it up and inject the
result back into the guest.
Currently the only user of kvm_io_bus for ARM is the VGIC, which did this copying itself, so this was not causing issues so far.
But with the upcoming new vgic implementation we need this done properly.
Update the kvm_handle_mmio_return description and cleanup the code to only perform a single copying when needed.
Code and commit message inspired by Andre Przywara.
--- arch/arm/kvm/mmio.c | 14 +++++++------- virt/kvm/arm/vgic.c | 7 ------- 2 files changed, 7 insertions(+), 14 deletions(-)
arch/arm/kvm/mmio.c +7 -7
@@ -87,11 +87,10 @@ static unsigned long mmio_read_buf(char *buf, unsigned int len)/** * kvm_handle_mmio_return -- Handle MMIO loads after user space emulation +* or in-kernel IO emulation +* * @vcpu: The VCPU pointer * @run: The VCPU run struct containing the mmio data -* -* This should only be called after returning from userspace for MMIO load -* emulation. */ int kvm_handle_mmio_return (struct kvm_vcpu *vcpu,struct kvm_run *run) {
@@ -206,18 +205,19 @@ int io_mem_abort(struct kvm_vcpu *vcpu, struct kvm_run *run, run->mmio.is_write = is_write; run->mmio.phys_addr = fault_ipa; run->mmio.len = len; -if (is_write) -memcpy (run->mmio.data, data_buf, len);if (!ret) {/* We handled the access successfully in the kernel. */ +if (!is_write) +memcpy (run->mmio.data, data_buf, len); vcpu->stat.mmio_exit_kernel++;kvm_handle_mmio_return (vcpu, run);return 1 ; - }else { - vcpu->stat.mmio_exit_user++; } +if (is_write) +memcpy (run->mmio.data, data_buf, len); + vcpu->stat.mmio_exit_user++; run->exit_reason =KVM_EXIT_MMIO ;return 0 ; }
virt/kvm/arm/vgic.c +0 -7
@@ -819,7 +819,6 @@ static int vgic_handle_mmio_access(struct kvm_vcpu *vcpu,struct vgic_dist *dist = &vcpu->kvm->arch.vgic;struct vgic_io_device *iodev =container_of (this, struct vgic_io_device, dev); -struct kvm_run *run = vcpu->run;const struct vgic_io_range *range;struct kvm_exit_mmio mmio;bool updated_state;
@@ -848,12 +847,6 @@ static int vgic_handle_mmio_access(struct kvm_vcpu *vcpu, updated_state = false; }spin_unlock (&dist->lock); - run->mmio.is_write = is_write; - run->mmio.len = len; - run->mmio.phys_addr = addr; -memcpy (run->mmio.data, val, len); - -kvm_handle_mmio_return (vcpu, run);if (updated_state)vgic_kick_vcpus (vcpu->kvm);
Signature · 1 line
2.8.2
PATCH v4 10/56 KVM: arm/arm64: Export mmio_read/write_bus
In reply to Andre PrzywaraFrom: Christoffer Dall <[email protected]>
Rename mmio_{read,write}_bus to kvm_mmio_{read,write}_bus and export them out of mmio.c.
This will be needed later for the new VGIC implementation.
--- arch/arm/include/asm/kvm_mmio.h | 3 +++ arch/arm/kvm/mmio.c | 10 +++++----- arch/arm64/include/asm/kvm_mmio.h | 3 +++ 3 files changed, 11 insertions(+), 5 deletions(-)
arch/arm/include/asm/kvm_mmio.h +3 -0
@@ -28,6 +28,9 @@ struct kvm_decode {bool sign_extend; }; +void kvm_mmio_write_buf (void *buf,unsigned int len,unsigned long data); +unsigned long kvm_mmio_read_buf (const void *buf,unsigned int len); +int kvm_handle_mmio_return (struct kvm_vcpu *vcpu,struct kvm_run *run);int io_mem_abort (struct kvm_vcpu *vcpu,struct kvm_run *run,phys_addr_t fault_ipa);
arch/arm/kvm/mmio.c +5 -5
@@ -23,7 +23,7 @@#include "trace.h" -static void mmio_write_buf (char *buf,unsigned int len,unsigned long data) +void kvm_mmio_write_buf (void *buf,unsigned int len,unsigned long data) {void *datap =NULL ;union {
@@ -55,7 +55,7 @@ static void mmio_write_buf(char *buf, unsigned int len, unsigned long data)memcpy (buf, datap, len); } -static unsigned long mmio_read_buf (char *buf,unsigned int len) +unsigned long kvm_mmio_read_buf (const void *buf,unsigned int len) {unsigned long data =0 ;union {
@@ -66,7 +66,7 @@ static unsigned long mmio_read_buf(char *buf, unsigned int len)switch (len) {case 1 : - data = buf[0 ]; + data = *(u8 *)buf;break ;case 2 :memcpy (&tmp.hword, buf, len);
@@ -103,7 +103,7 @@ int kvm_handle_mmio_return(struct kvm_vcpu *vcpu, struct kvm_run *run)if (len >sizeof (unsigned long ))return -EINVAL ; - data =mmio_read_buf (run->mmio.data, len); + data =kvm_mmio_read_buf (run->mmio.data, len);if (vcpu->arch.mmio_decode.sign_extend && len <sizeof (unsigned long )) {
@@ -189,7 +189,7 @@ int io_mem_abort(struct kvm_vcpu *vcpu, struct kvm_run *run, len);trace_kvm_mmio (KVM_TRACE_MMIO_WRITE , len, fault_ipa, data); -mmio_write_buf (data_buf, len, data); +kvm_mmio_write_buf (data_buf, len, data); ret =kvm_io_bus_write (vcpu,KVM_MMIO_BUS , fault_ipa, len, data_buf);
arch/arm64/include/asm/kvm_mmio.h +3 -0
@@ -30,6 +30,9 @@ struct kvm_decode {bool sign_extend; }; +void kvm_mmio_write_buf (void *buf,unsigned int len,unsigned long data); +unsigned long kvm_mmio_read_buf (const void *buf,unsigned int len); +int kvm_handle_mmio_return (struct kvm_vcpu *vcpu,struct kvm_run *run);int io_mem_abort (struct kvm_vcpu *vcpu,struct kvm_run *run,phys_addr_t fault_ipa);
Signature · 1 line
2.8.2
PATCH v4 1/56 KVM: arm/arm64: vgic: streamline vgic_update_irq_pending() interface
In reply to Andre PrzywaraWe actually don't use the irq_phys_map parameter in vgic_update_irq_pending(), so let's just remove it.
--- virt/kvm/arm/vgic.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
virt/kvm/arm/vgic.c +2 -3
@@ -1521,7 +1521,6 @@ static int vgic_validate_injection(struct kvm_vcpu *vcpu, int irq, int level) }static int vgic_update_irq_pending (struct kvm *kvm,int cpuid, -struct irq_phys_map *map,unsigned int irq_num,bool level) {struct vgic_dist *dist = &kvm->arch.vgic;
@@ -1660,7 +1659,7 @@ int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int irq_num,if (map)return -EINVAL ; -return vgic_update_irq_pending (kvm, cpuid,NULL , irq_num, level); +return vgic_update_irq_pending (kvm, cpuid, irq_num, level); }/**
@@ -1686,7 +1685,7 @@ int kvm_vgic_inject_mapped_irq(struct kvm *kvm, int cpuid,if (ret)return ret; -return vgic_update_irq_pending (kvm, cpuid, map, map->virt_irq, level); +return vgic_update_irq_pending (kvm, cpuid, map->virt_irq, level); }static irqreturn_t vgic_maintenance_handler (int irq,void *data)
Signature · 1 line
2.8.2
PATCH v4 2/56 KVM: arm/arm64: vgic: avoid map in kvm_vgic_inject_mapped_irq()
In reply to Andre PrzywaraWhen we want to inject a hardware mapped IRQ into a guest, we actually only need the virtual IRQ number from the irq_phys_map.
So let's pass this number directly from the arch timer to the VGIC to avoid using the map as a parameter.
--- include/kvm/arm_vgic.h | 2 +- virt/kvm/arm/arch_timer.c | 2 +- virt/kvm/arm/vgic.c | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-)
include/kvm/arm_vgic.h +1 -1
@@ -342,7 +342,7 @@ void kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu);int kvm_vgic_inject_irq (struct kvm *kvm,int cpuid,unsigned int irq_num,bool level);int kvm_vgic_inject_mapped_irq (struct kvm *kvm,int cpuid, -struct irq_phys_map *map,bool level); +unsigned int virt_irq,bool level);void vgic_v3_dispatch_sgi (struct kvm_vcpu *vcpu,u64 reg);int kvm_vgic_vcpu_pending_irq (struct kvm_vcpu *vcpu); struct irq_phys_map *kvm_vgic_map_phys_irq(struct kvm_vcpu *vcpu,
virt/kvm/arm/arch_timer.c +1 -1
@@ -177,7 +177,7 @@ static void kvm_timer_update_irq(struct kvm_vcpu *vcpu, bool new_level)trace_kvm_timer_update_irq (vcpu->vcpu_id, timer->map->virt_irq, timer->irq.level); ret =kvm_vgic_inject_mapped_irq (vcpu->kvm, vcpu->vcpu_id, - timer->map, + timer->map->virt_irq, timer->irq.level);WARN_ON (ret); }
virt/kvm/arm/vgic.c +3 -3
@@ -1666,7 +1666,7 @@ int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int irq_num,* kvm_vgic_inject_mapped_irq - Inject a physically mapped IRQ to the vgic * @kvm: The VM structure pointer * @cpuid: The CPU for PPIs -* @map: Pointer to a irq_phys_map structure describing the mapping +* @virt_irq: The virtual IRQ to be injected * @level: Edge-triggered: true: to trigger the interrupt * false: to ignore the call * Level-sensitive true: raise the input signal
@@ -1677,7 +1677,7 @@ int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int irq_num,* being HIGH and 0 being LOW and all devices being active-HIGH. */ int kvm_vgic_inject_mapped_irq (struct kvm *kvm,int cpuid, -struct irq_phys_map *map,bool level) +unsigned int virt_irq,bool level) {int ret;
@@ -1685,7 +1685,7 @@ int kvm_vgic_inject_mapped_irq(struct kvm *kvm, int cpuid,if (ret)return ret; -return vgic_update_irq_pending (kvm, cpuid, map->virt_irq, level); +return vgic_update_irq_pending (kvm, cpuid, virt_irq, level); }static irqreturn_t vgic_maintenance_handler (int irq,void *data)
Signature · 1 line
2.8.2
PATCH v4 4/56 KVM: arm/arm64: vgic: avoid map in kvm_vgic_unmap_phys_irq()
In reply to Andre Przywarakvm_vgic_unmap_phys_irq() only needs the virtual IRQ number, so let's just pass that between the arch timer and the VGIC to get rid of the irq_phys_map pointer.
--- include/kvm/arm_vgic.h | 2 +- virt/kvm/arm/arch_timer.c | 2 +- virt/kvm/arm/vgic.c | 11 ++++------- 3 files changed, 6 insertions(+), 9 deletions(-)
include/kvm/arm_vgic.h +1 -1
@@ -347,7 +347,7 @@ void vgic_v3_dispatch_sgi(struct kvm_vcpu *vcpu, u64 reg);int kvm_vgic_vcpu_pending_irq (struct kvm_vcpu *vcpu);struct irq_phys_map *kvm_vgic_map_phys_irq (struct kvm_vcpu *vcpu,int virt_irq,int irq); -int kvm_vgic_unmap_phys_irq (struct kvm_vcpu *vcpu,struct irq_phys_map *map); +int kvm_vgic_unmap_phys_irq (struct kvm_vcpu *vcpu,unsigned int virt_irq);bool kvm_vgic_map_is_active (struct kvm_vcpu *vcpu,unsigned int virt_irq);#define irqchip_in_kernel (k) (!!((k)->arch.vgic.in_kernel))
virt/kvm/arm/arch_timer.c +1 -1
@@ -486,7 +486,7 @@ void kvm_timer_vcpu_terminate(struct kvm_vcpu *vcpu)timer_disarm (timer);if (timer->map) -kvm_vgic_unmap_phys_irq (vcpu, timer->map); +kvm_vgic_unmap_phys_irq (vcpu, timer->map->virt_irq); }void kvm_timer_enable (struct kvm *kvm)
virt/kvm/arm/vgic.c +4 -7
@@ -1812,25 +1812,22 @@ static void vgic_free_phys_irq_map_rcu(struct rcu_head *rcu)/** * kvm_vgic_unmap_phys_irq - Remove a virtual to physical IRQ mapping * @vcpu: The VCPU pointer -* @map: The pointer to a mapping obtained through kvm_vgic_map_phys_irq +* @virt_irq: The virtual IRQ number to be unmapped * * Remove an existing mapping between virtual and physical interrupts. */ -int kvm_vgic_unmap_phys_irq (struct kvm_vcpu *vcpu,struct irq_phys_map *map) +int kvm_vgic_unmap_phys_irq (struct kvm_vcpu *vcpu,unsigned int virt_irq) {struct vgic_dist *dist = &vcpu->kvm->arch.vgic;struct irq_phys_map_entry *entry;struct list_head *root; -if (!map) -return -EINVAL ; - - root =vgic_get_irq_phys_map_list (vcpu, map->virt_irq); + root =vgic_get_irq_phys_map_list (vcpu, virt_irq);spin_lock (&dist->irq_phys_map_lock);list_for_each_entry (entry, root, entry) { -if (&entry->map == map) { +if (entry->map.virt_irq == virt_irq) {list_del_rcu (&entry->entry);call_rcu (&entry->rcu, vgic_free_phys_irq_map_rcu);break ;
Signature · 1 line
2.8.2
PATCH v4 5/56 KVM: arm/arm64: Remove the IRQ field from struct irq_phys_map
In reply to Andre PrzywaraFrom: Christoffer Dall <[email protected]>
The communication of a Linux IRQ number from outside the VGIC to the vgic was a leftover from the day when the vgic code cared about how a particular device injects virtual interrupts mapped to a physical interrupt.
We can safely remove this notion, leaving all physical IRQ handling to be done in the device driver (the arch timer in this case), which makes room for a saner API for the new VGIC.
---
Changelog v1 .. v2:
- update kerneldoc comments on kvm_vgic_map_phys_irq()
Changelog v2 .. v3:
- include <linux/irq.h>
include/kvm/arm_vgic.h | 3 +-- virt/kvm/arm/arch_timer.c | 23 +++++++++++++++++++++-- virt/kvm/arm/vgic.c | 28 ++++++---------------------- 3 files changed, 28 insertions(+), 26 deletions(-)
include/kvm/arm_vgic.h +1 -2
@@ -158,7 +158,6 @@ struct vgic_io_device {struct irq_phys_map {u32 virt_irq;u32 phys_irq; -u32 irq; };struct irq_phys_map_entry {
@@ -346,7 +345,7 @@ int kvm_vgic_inject_mapped_irq(struct kvm *kvm, int cpuid,void vgic_v3_dispatch_sgi (struct kvm_vcpu *vcpu,u64 reg);int kvm_vgic_vcpu_pending_irq (struct kvm_vcpu *vcpu);struct irq_phys_map *kvm_vgic_map_phys_irq (struct kvm_vcpu *vcpu, -int virt_irq,int irq); +int virt_irq,int phys_irq);int kvm_vgic_unmap_phys_irq (struct kvm_vcpu *vcpu,unsigned int virt_irq);bool kvm_vgic_map_is_active (struct kvm_vcpu *vcpu,unsigned int virt_irq);
virt/kvm/arm/arch_timer.c +21 -2
@@ -20,6 +20,7 @@#include <linux/kvm.h> #include <linux/kvm_host.h> #include <linux/interrupt.h> +#include <linux/irq.h> #include <clocksource/arm_arch_timer.h> #include <asm/arch_timer.h>
@@ -300,7 +301,7 @@ void kvm_timer_flush_hwstate(struct kvm_vcpu *vcpu)if (timer->active_cleared_last && !phys_active)return ; - ret =irq_set_irqchip_state (timer->map->irq, + ret =irq_set_irqchip_state (host_vtimer_irq,IRQCHIP_STATE_ACTIVE , phys_active);WARN_ON (ret);
@@ -333,6 +334,9 @@ int kvm_timer_vcpu_reset(struct kvm_vcpu *vcpu, {struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;struct irq_phys_map *map; +struct irq_desc *desc; +struct irq_data *data; +int phys_irq;/* * The vcpu timer irq number cannot be determined in
@@ -352,10 +356,25 @@ int kvm_timer_vcpu_reset(struct kvm_vcpu *vcpu,kvm_timer_update_state (vcpu);/* +* Find the physical IRQ number corresponding to the host_vtimer_irq +*/ + desc =irq_to_desc (host_vtimer_irq); +if (!desc) { +kvm_err ("%s: no interrupt descriptor\n" , __func__); +return -EINVAL ; + } + + data =irq_desc_get_irq_data (desc); +while (data->parent_data) + data = data->parent_data; + + phys_irq = data->hwirq; + +/* * Tell the VGIC that the virtual interrupt is tied to a * physical interrupt. We do that once per VCPU. */ - map =kvm_vgic_map_phys_irq (vcpu, irq->irq, host_vtimer_irq); + map =kvm_vgic_map_phys_irq (vcpu, irq->irq, phys_irq);if (WARN_ON (IS_ERR (map)))return PTR_ERR (map);
virt/kvm/arm/vgic.c +6 -22
@@ -1711,38 +1711,24 @@ static struct list_head *vgic_get_irq_phys_map_list(struct kvm_vcpu *vcpu,/** * kvm_vgic_map_phys_irq - map a virtual IRQ to a physical IRQ * @vcpu: The VCPU pointer -* @virt_irq: The virtual irq number -* @irq: The Linux IRQ number +* @virt_irq: The virtual IRQ number for the guest +* @phys_irq: The hardware IRQ number of the host * * Establish a mapping between a guest visible irq (@virt_irq) and a -* Linux irq (@irq). On injection, @virt_irq will be associated with -* the physical interrupt represented by @irq. This mapping can be +* hardware irq (@phys_irq). On injection, @virt_irq will be associated with +* the physical interrupt represented by @phys_irq. This mapping can be * established multiple times as long as the parameters are the same. * * Returns a valid pointer on success, and an error pointer otherwise */ struct irq_phys_map *kvm_vgic_map_phys_irq (struct kvm_vcpu *vcpu, -int virt_irq,int irq) +int virt_irq,int phys_irq) {struct vgic_dist *dist = &vcpu->kvm->arch.vgic;struct list_head *root =vgic_get_irq_phys_map_list (vcpu, virt_irq);struct irq_phys_map *map;struct irq_phys_map_entry *entry; -struct irq_desc *desc; -struct irq_data *data; -int phys_irq; - desc =irq_to_desc (irq); -if (!desc) { -kvm_err ("%s: no interrupt descriptor\n" , __func__); -return ERR_PTR (-EINVAL ); - } - - data =irq_desc_get_irq_data (desc); -while (data->parent_data) - data = data->parent_data; - - phys_irq = data->hwirq;/* Create a new mapping */ entry =kzalloc (sizeof (*entry),GFP_KERNEL );
@@ -1755,8 +1741,7 @@ struct irq_phys_map *kvm_vgic_map_phys_irq(struct kvm_vcpu *vcpu, map =vgic_irq_map_search (vcpu, virt_irq);if (map) {/* Make sure this mapping matches */ -if (map->phys_irq != phys_irq || - map->irq != irq) +if (map->phys_irq != phys_irq) map =ERR_PTR (-EINVAL );/* Found an existing, valid mapping */
@@ -1766,7 +1751,6 @@ struct irq_phys_map *kvm_vgic_map_phys_irq(struct kvm_vcpu *vcpu, map = &entry->map; map->virt_irq = virt_irq; map->phys_irq = phys_irq; - map->irq = irq;list_add_tail_rcu (&entry->entry, root);
Signature · 1 line
2.8.2
PATCH v4 14/56 KVM: arm/arm64: vgic-new: Add data structure definitions
In reply to Andre PrzywaraFrom: Christoffer Dall <[email protected]>
Add a new header file for the new and improved GIC implementation. The big change is that we now have a struct vgic_irq per IRQ instead of spreading all the information over various bitmaps.
We include this new header conditionally from within the old header file for the time being to avoid touching all the users.
---
Changes RFC .. v1:
- adapt to 4.6-rc (adding live_lrs member)
- elaborate on ap_list usage
Changes v1 .. v2:
- change data type of dist->enabled to bool
Changelog v3 .. v4:
- fix comment typos
include/kvm/arm_vgic.h | 5 ++ include/kvm/vgic/vgic.h | 201 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 206 insertions(+) create mode 100644 include/kvm/vgic/vgic.h
include/kvm/arm_vgic.h +5 -0
@@ -19,6 +19,10 @@#ifndef __ASM_ARM_KVM_VGIC_H#define __ASM_ARM_KVM_VGIC_H +#ifdef CONFIG_KVM_NEW_VGIC +#include <kvm/vgic/vgic.h> +#else +#include <linux/kernel.h> #include <linux/kvm.h> #include <linux/irqreturn.h>
@@ -367,4 +371,5 @@ static inline int vgic_v3_probe(const struct gic_kvm_info *gic_kvm_info, }#endif +#endif /* old VGIC include */ #endif
include/kvm/vgic/vgic.h new file +201 -0
201 lines not on this page · raw message
Signature · 1 line
2.8.2
PATCH v4 15/56 KVM: arm/arm64: vgic-new: Add acccessor to new struct vgic_irq instance
In reply to Andre PrzywaraFrom: Christoffer Dall <[email protected]>
The new VGIC implementation centers around a struct vgic_irq instance per virtual IRQ.
Provide a function to retrieve the right instance for a given IRQ number and (in case of private interrupts) the right VCPU.
--- virt/kvm/arm/vgic/vgic.c | 41 +++++++++++++++++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic.h | 22 ++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 virt/kvm/arm/vgic/vgic.c create mode 100644 virt/kvm/arm/vgic/vgic.h
virt/kvm/arm/vgic/vgic.c new file +41 -0
@@ -0,0 +1,41 @@ +/* +* Copyright (C) 2015, 2016 ARM Ltd. +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include <linux/kvm.h> +#include <linux/kvm_host.h> + +#include "vgic.h" + +struct vgic_global __section(.hyp.text) kvm_vgic_global_state; + +struct vgic_irq *vgic_get_irq (struct kvm *kvm,struct kvm_vcpu *vcpu, +u32 intid) +{ +/* SGIs and PPIs */ +if (intid <=VGIC_MAX_PRIVATE ) +return &vcpu->arch.vgic_cpu.private_irqs[intid]; + +/* SPIs */ +if (intid <=VGIC_MAX_SPI ) +return &kvm->arch.vgic.spis[intid -VGIC_NR_PRIVATE_IRQS ]; + +/* LPIs are not yet covered */ +if (intid >=VGIC_MIN_LPI ) +return NULL ; + +WARN (1 ,"Looking up struct vgic_irq for reserved INTID" ); +return NULL ; +}
virt/kvm/arm/vgic/vgic.h new file +22 -0
@@ -0,0 +1,22 @@ +/* +* Copyright (C) 2015, 2016 ARM Ltd. +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ +#ifndef __KVM_ARM_VGIC_NEW_H__ +#define __KVM_ARM_VGIC_NEW_H__ + +struct vgic_irq *vgic_get_irq (struct kvm *kvm,struct kvm_vcpu *vcpu, +u32 intid); + +#endif
Signature · 1 line
2.8.2
PATCH v4 8/56 KVM: arm/arm64: Get rid of vgic_cpu->nr_lr
In reply to Andre PrzywaraFrom: Christoffer Dall <[email protected]>
The number of list registers is a property of the underlying system, not of emulated VGIC CPU interface.
As we are about to move this variable to global state in the new vgic for clarity, move it from the legacy implementation as well to make the merge of the new code easier.
--- include/kvm/arm_vgic.h | 3 --- virt/kvm/arm/hyp/vgic-v2-sr.c | 12 +++++++----- virt/kvm/arm/vgic-v2.c | 4 +++- virt/kvm/arm/vgic.c | 12 ++---------- 4 files changed, 12 insertions(+), 19 deletions(-)
include/kvm/arm_vgic.h +0 -3
@@ -304,9 +304,6 @@ struct vgic_cpu {unsigned long *active_shared;unsigned long *pend_act_shared; -/* Number of list registers on this CPU */ -int nr_lr; -/* CPU vif control registers for world switch */ union {struct vgic_v2_cpu_if vgic_v2;
virt/kvm/arm/hyp/vgic-v2-sr.c +7 -5
@@ -21,11 +21,13 @@#include <asm/kvm_hyp.h> +extern struct vgic_params vgic_v2_params; + static void __hyp_text save_maint_int_state(struct kvm_vcpu *vcpu,void __iomem *base) {struct vgic_v2_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v2; -int nr_lr = vcpu->arch.vgic_cpu.nr_lr; +int nr_lr = (kern_hyp_va (&vgic_v2_params))->nr_lr;u32 eisr0, eisr1;int i;bool expect_mi;
@@ -67,7 +69,7 @@ static void __hyp_text save_maint_int_state(struct kvm_vcpu *vcpu, static void __hyp_text save_elrsr(struct kvm_vcpu *vcpu, void __iomem *base) {struct vgic_v2_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v2; -int nr_lr = vcpu->arch.vgic_cpu.nr_lr; +int nr_lr = (kern_hyp_va (&vgic_v2_params))->nr_lr;u32 elrsr0, elrsr1; elrsr0 =readl_relaxed (base +GICH_ELRSR0 );
@@ -86,7 +88,7 @@ static void __hyp_text save_elrsr(struct kvm_vcpu *vcpu, void __iomem *base) static void __hyp_text save_lrs(struct kvm_vcpu *vcpu, void __iomem *base) {struct vgic_v2_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v2; -int nr_lr = vcpu->arch.vgic_cpu.nr_lr; +int nr_lr = (kern_hyp_va (&vgic_v2_params))->nr_lr;int i;for (i =0 ; i < nr_lr; i++) {
@@ -141,13 +143,13 @@ void __hyp_text __vgic_v2_restore_state(struct kvm_vcpu *vcpu)struct vgic_v2_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v2;struct vgic_dist *vgic = &kvm->arch.vgic;void __iomem *base =kern_hyp_va (vgic->vctrl_base); -int i, nr_lr; +int nr_lr = (kern_hyp_va (&vgic_v2_params))->nr_lr; +int i;u64 live_lrs =0 ;if (!base)return ; - nr_lr = vcpu->arch.vgic_cpu.nr_lr;for (i =0 ; i < nr_lr; i++)if (cpu_if->vgic_lr[i] &GICH_LR_STATE )
virt/kvm/arm/vgic-v2.c +3 -1
@@ -171,7 +171,7 @@ static const struct vgic_ops vgic_v2_ops = { .enable = vgic_v2_enable, }; -static struct vgic_params vgic_v2_params; +struct vgic_params __section(.hyp.text) vgic_v2_params;static void vgic_cpu_init_lrs (void *params) {
@@ -201,6 +201,8 @@ int vgic_v2_probe(const struct gic_kvm_info *gic_kvm_info,const struct resource *vctrl_res = &gic_kvm_info->vctrl;const struct resource *vcpu_res = &gic_kvm_info->vcpu; +memset (vgic,0 ,sizeof (*vgic)); +if (!gic_kvm_info->maint_irq) {kvm_err ("error getting vgic maintenance irq\n" ); ret = -ENXIO ;
virt/kvm/arm/vgic.c +2 -10
@@ -690,12 +690,11 @@ bool vgic_handle_cfg_reg(u32 *reg, struct kvm_exit_mmio *mmio,*/ void vgic_unqueue_irqs (struct kvm_vcpu *vcpu) { -struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;u64 elrsr =vgic_get_elrsr (vcpu);unsigned long *elrsr_ptr =u64_to_bitmask (&elrsr);int i; -for_each_clear_bit (i, elrsr_ptr, vgic_cpu->nr_lr) { +for_each_clear_bit (i, elrsr_ptr, vgic->nr_lr) {struct vgic_lr lr =vgic_get_lr (vcpu, i);/*
@@ -1106,7 +1105,7 @@ bool kvm_vgic_map_is_active(struct kvm_vcpu *vcpu, unsigned int virt_irq) {int i; -for (i =0 ; i < vcpu->arch.vgic_cpu.nr_lr; i++) { +for (i =0 ; i < vgic->nr_lr; i++) {struct vgic_lr vlr =vgic_get_lr (vcpu, i);if (vlr.irq == virt_irq && vlr.state &LR_STATE_ACTIVE )
@@ -1866,13 +1865,6 @@ static int vgic_vcpu_init_maps(struct kvm_vcpu *vcpu, int nr_irqs)return -ENOMEM ; } -/* -* Store the number of LRs per vcpu, so we don't have to go -* all the way to the distributor structure to find out. Only -* assembly code should use this one. -*/ - vgic_cpu->nr_lr = vgic->nr_lr; -return 0 ; }
Signature · 1 line
2.8.2
PATCH v4 16/56 KVM: arm/arm64: vgic-new: Implement virtual IRQ injection
In reply to Andre PrzywaraFrom: Christoffer Dall <[email protected]>
Provide a vgic_queue_irq_unlock() function which decides whether a given IRQ needs to be queued to a VCPU's ap_list.
This should be called whenever an IRQ becomes pending or enabled, either as a result of userspace injection, from in-kernel emulated devices like the architected timer or from MMIO accesses to the distributor emulation.
Also provides the necessary functions to allow userland to inject an IRQ to a guest.
Since this is the first code that starts using our locking mechanism, we add some (hopefully) clear documentation of our locking strategy and requirements along with this patch.
[Andre: refactor out vgic_queue_irq_unlock()]
---
Changelog RFC..v1:
- add spinlock checks protected by CONFIG_DEBUG_SPINLOCK
- add more comments to vgic_target_oracle
- remove BUG_ON() if IRQ is neither edge or level
- rename vgic_queue_irq() to vgic_queue_irq_unlock()
- simplify initial check in vgic_queue_irq_unlock()
- extend retry loop to ask the oracle again
- minor comment fixes
Changelog v1 .. v2:
- move most IRQ injection code into vgic_update_irq_pending()
Changelog v3 .. v4:
- simplify vgic_queue_irq_unlock() to allow reusage later
include/kvm/vgic/vgic.h | 3 + virt/kvm/arm/vgic/vgic.c | 211 +++++++++++++++++++++++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic.h | 1 + 3 files changed, 215 insertions(+)
include/kvm/vgic/vgic.h +3 -0
@@ -181,6 +181,9 @@ struct vgic_cpu {u64 live_lrs; }; +int kvm_vgic_inject_irq (struct kvm *kvm,int cpuid,unsigned int intid, +bool level); +#define irqchip_in_kernel (k) (!!((k)->arch.vgic.in_kernel))#define vgic_initialized (k) (false)#define vgic_ready (k) ((k)->arch.vgic.ready)
virt/kvm/arm/vgic/vgic.c +211 -0
222 lines not on this page · raw message
virt/kvm/arm/vgic/vgic.h +1 -0
@@ -18,5 +18,6 @@struct vgic_irq *vgic_get_irq (struct kvm *kvm,struct kvm_vcpu *vcpu,u32 intid); +bool vgic_queue_irq_unlock (struct kvm *kvm,struct vgic_irq *irq);#endif
Signature · 1 line
2.8.2
PATCH v4 10/56 KVM: arm/arm64: Export mmio_read/write_bus
In reply to Andre PrzywaraFrom: Christoffer Dall <[email protected]>
Rename mmio_{read,write}_bus to kvm_mmio_{read,write}_bus and export them out of mmio.c.
This will be needed later for the new VGIC implementation.
--- arch/arm/include/asm/kvm_mmio.h | 3 +++ arch/arm/kvm/mmio.c | 10 +++++----- arch/arm64/include/asm/kvm_mmio.h | 3 +++ 3 files changed, 11 insertions(+), 5 deletions(-)
arch/arm/include/asm/kvm_mmio.h +3 -0
@@ -28,6 +28,9 @@ struct kvm_decode {bool sign_extend; }; +void kvm_mmio_write_buf (void *buf,unsigned int len,unsigned long data); +unsigned long kvm_mmio_read_buf (const void *buf,unsigned int len); +int kvm_handle_mmio_return (struct kvm_vcpu *vcpu,struct kvm_run *run);int io_mem_abort (struct kvm_vcpu *vcpu,struct kvm_run *run,phys_addr_t fault_ipa);
arch/arm/kvm/mmio.c +5 -5
@@ -23,7 +23,7 @@#include "trace.h" -static void mmio_write_buf (char *buf,unsigned int len,unsigned long data) +void kvm_mmio_write_buf (void *buf,unsigned int len,unsigned long data) {void *datap =NULL ;union {
@@ -55,7 +55,7 @@ static void mmio_write_buf(char *buf, unsigned int len, unsigned long data)memcpy (buf, datap, len); } -static unsigned long mmio_read_buf (char *buf,unsigned int len) +unsigned long kvm_mmio_read_buf (const void *buf,unsigned int len) {unsigned long data =0 ;union {
@@ -66,7 +66,7 @@ static unsigned long mmio_read_buf(char *buf, unsigned int len)switch (len) {case 1 : - data = buf[0 ]; + data = *(u8 *)buf;break ;case 2 :memcpy (&tmp.hword, buf, len);
@@ -103,7 +103,7 @@ int kvm_handle_mmio_return(struct kvm_vcpu *vcpu, struct kvm_run *run)if (len >sizeof (unsigned long ))return -EINVAL ; - data =mmio_read_buf (run->mmio.data, len); + data =kvm_mmio_read_buf (run->mmio.data, len);if (vcpu->arch.mmio_decode.sign_extend && len <sizeof (unsigned long )) {
@@ -189,7 +189,7 @@ int io_mem_abort(struct kvm_vcpu *vcpu, struct kvm_run *run, len);trace_kvm_mmio (KVM_TRACE_MMIO_WRITE , len, fault_ipa, data); -mmio_write_buf (data_buf, len, data); +kvm_mmio_write_buf (data_buf, len, data); ret =kvm_io_bus_write (vcpu,KVM_MMIO_BUS , fault_ipa, len, data_buf);
arch/arm64/include/asm/kvm_mmio.h +3 -0
@@ -30,6 +30,9 @@ struct kvm_decode {bool sign_extend; }; +void kvm_mmio_write_buf (void *buf,unsigned int len,unsigned long data); +unsigned long kvm_mmio_read_buf (const void *buf,unsigned int len); +int kvm_handle_mmio_return (struct kvm_vcpu *vcpu,struct kvm_run *run);int io_mem_abort (struct kvm_vcpu *vcpu,struct kvm_run *run,phys_addr_t fault_ipa);
Signature · 1 line
2.8.2
PATCH v4 13/56 KVM: arm/arm64: move GICv2 emulation defines into arm-gic-v3.h
In reply to Andre PrzywaraAs (some) GICv3 hosts can emulate a GICv2, some GICv2 specific masks for the list register definition also apply to GICv3 LRs. At the moment we have those definitions in the KVM VGICv3 implementation, so let's move them into the GICv3 header file to have them automatically defined.
--- include/linux/irqchip/arm-gic-v3.h | 5 +++++ virt/kvm/arm/vgic-v3.c | 8 +------- 2 files changed, 6 insertions(+), 7 deletions(-)
include/linux/irqchip/arm-gic-v3.h +5 -0
@@ -276,6 +276,11 @@#define ICH_LR_PHYS_ID_SHIFT 32#define ICH_LR_PHYS_ID_MASK (0x3ffULL << ICH_LR_PHYS_ID_SHIFT) +/* These are for GICv2 emulation only */ +#define GICH_LR_VIRTUALID (0x3ffUL << 0) +#define GICH_LR_PHYSID_CPUID_SHIFT (10) +#define GICH_LR_PHYSID_CPUID (7UL << GICH_LR_PHYSID_CPUID_SHIFT) +#define ICH_MISR_EOI (1 << 0)#define ICH_MISR_U (1 << 1)
virt/kvm/arm/vgic-v3.c +1 -7
@@ -29,12 +29,6 @@#include <asm/kvm_asm.h> #include <asm/kvm_mmu.h> -/* These are for GICv2 emulation only */ -#define GICH_LR_VIRTUALID (0x3ffUL << 0) -#define GICH_LR_PHYSID_CPUID_SHIFT (10) -#define GICH_LR_PHYSID_CPUID (7UL << GICH_LR_PHYSID_CPUID_SHIFT) -#define ICH_LR_VIRTUALID_MASK (BIT_ULL(32) - 1) -static u32 ich_vtr_el2;static struct vgic_lr vgic_v3_get_lr (const struct kvm_vcpu *vcpu,int lr)
@@ -43,7 +37,7 @@ static struct vgic_lr vgic_v3_get_lr(const struct kvm_vcpu *vcpu, int lr)u64 val = vcpu->arch.vgic_cpu.vgic_v3.vgic_lr[lr];if (vcpu->kvm->arch.vgic.vgic_model ==KVM_DEV_TYPE_ARM_VGIC_V3 ) - lr_desc.irq = val &ICH_LR_VIRTUALID_MASK ; + lr_desc.irq = val &ICH_LR_VIRTUAL_ID_MASK ;else lr_desc.irq = val &GICH_LR_VIRTUALID ;
Signature · 1 line
2.8.2
PATCH v4 14/56 KVM: arm/arm64: vgic-new: Add data structure definitions
In reply to Andre PrzywaraFrom: Christoffer Dall <[email protected]>
Add a new header file for the new and improved GIC implementation. The big change is that we now have a struct vgic_irq per IRQ instead of spreading all the information over various bitmaps.
We include this new header conditionally from within the old header file for the time being to avoid touching all the users.
---
Changes RFC .. v1:
- adapt to 4.6-rc (adding live_lrs member)
- elaborate on ap_list usage
Changes v1 .. v2:
- change data type of dist->enabled to bool
Changelog v3 .. v4:
- fix comment typos
include/kvm/arm_vgic.h | 5 ++ include/kvm/vgic/vgic.h | 201 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 206 insertions(+) create mode 100644 include/kvm/vgic/vgic.h
include/kvm/arm_vgic.h +5 -0
@@ -19,6 +19,10 @@#ifndef __ASM_ARM_KVM_VGIC_H#define __ASM_ARM_KVM_VGIC_H +#ifdef CONFIG_KVM_NEW_VGIC +#include <kvm/vgic/vgic.h> +#else +#include <linux/kernel.h> #include <linux/kvm.h> #include <linux/irqreturn.h>
@@ -367,4 +371,5 @@ static inline int vgic_v3_probe(const struct gic_kvm_info *gic_kvm_info, }#endif +#endif /* old VGIC include */ #endif
include/kvm/vgic/vgic.h new file +201 -0
201 lines not on this page · raw message
Signature · 1 line
2.8.2
PATCH v4 20/56 KVM: arm/arm64: vgic-new: Add GICv3 world switch backend
In reply to Andre PrzywaraFrom: Marc Zyngier <[email protected]>
As the GICv3 virtual interface registers differ from their GICv2 siblings, we need different handlers for processing maintenance interrupts and reading/writing to the LRs.
Implement the respective handler functions and connect them to existing code to be called if the host is using a GICv3.
---
Changelog RFC..v1:
- remove outdated comment about the dist_lock
- add WARN_ON about LR_STATE not being 0 in maintenance interrupts
Changelog v1 .. v2:
- inject the IRQ priority into the list register
Changelog v2 .. v3:
- remove no longer needed irqchip/arm-gic.h inclusion
Changelog v3 .. v4:
- remove ELRSR bit set during process_maintenance()
- refine comment about Group1 interrupts
- remove wrong comment about ap_list_lock in vgic_v3_fold_lr_state()
include/linux/irqchip/arm-gic-v3.h | 1 + virt/kvm/arm/vgic/vgic-v3.c | 162 +++++++++++++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic.c | 25 ++++-- virt/kvm/arm/vgic/vgic.h | 29 +++++++ 4 files changed, 212 insertions(+), 5 deletions(-) create mode 100644 virt/kvm/arm/vgic/vgic-v3.c
include/linux/irqchip/arm-gic-v3.h +1 -0
@@ -275,6 +275,7 @@#define ICH_LR_ACTIVE_BIT (1ULL << 63)#define ICH_LR_PHYS_ID_SHIFT 32#define ICH_LR_PHYS_ID_MASK (0x3ffULL << ICH_LR_PHYS_ID_SHIFT) +#define ICH_LR_PRIORITY_SHIFT 48/* These are for GICv2 emulation only */ #define GICH_LR_VIRTUALID (0x3ffUL << 0)
virt/kvm/arm/vgic/vgic-v3.c new file +162 -0
162 lines not on this page · raw message
virt/kvm/arm/vgic/vgic.c +20 -5
@@ -399,12 +399,18 @@ retry:static inline void vgic_process_maintenance_interrupt (struct kvm_vcpu *vcpu) { -vgic_v2_process_maintenance (vcpu); +if (kvm_vgic_global_state.type ==VGIC_V2 ) +vgic_v2_process_maintenance (vcpu); +else +vgic_v3_process_maintenance (vcpu); }static inline void vgic_fold_lr_state (struct kvm_vcpu *vcpu) { -vgic_v2_fold_lr_state (vcpu); +if (kvm_vgic_global_state.type ==VGIC_V2 ) +vgic_v2_fold_lr_state (vcpu); +else +vgic_v3_fold_lr_state (vcpu); }/* Requires the irq_lock to be held. */
@@ -413,17 +419,26 @@ static inline void vgic_populate_lr(struct kvm_vcpu *vcpu, {DEBUG_SPINLOCK_BUG_ON (!spin_is_locked (&irq->irq_lock)); -vgic_v2_populate_lr (vcpu, irq, lr); +if (kvm_vgic_global_state.type ==VGIC_V2 ) +vgic_v2_populate_lr (vcpu, irq, lr); +else +vgic_v3_populate_lr (vcpu, irq, lr); }static inline void vgic_clear_lr (struct kvm_vcpu *vcpu,int lr) { -vgic_v2_clear_lr (vcpu, lr); +if (kvm_vgic_global_state.type ==VGIC_V2 ) +vgic_v2_clear_lr (vcpu, lr); +else +vgic_v3_clear_lr (vcpu, lr); }static inline void vgic_set_underflow (struct kvm_vcpu *vcpu) { -vgic_v2_set_underflow (vcpu); +if (kvm_vgic_global_state.type ==VGIC_V2 ) +vgic_v2_set_underflow (vcpu); +else +vgic_v3_set_underflow (vcpu); }/* Requires the ap_list_lock to be held. */
virt/kvm/arm/vgic/vgic.h +29 -0
@@ -28,4 +28,33 @@ void vgic_v2_populate_lr(struct kvm_vcpu *vcpu, struct vgic_irq *irq, int lr);void vgic_v2_clear_lr (struct kvm_vcpu *vcpu,int lr);void vgic_v2_set_underflow (struct kvm_vcpu *vcpu); +#ifdef CONFIG_KVM_ARM_VGIC_V3 +void vgic_v3_process_maintenance (struct kvm_vcpu *vcpu); +void vgic_v3_fold_lr_state (struct kvm_vcpu *vcpu); +void vgic_v3_populate_lr (struct kvm_vcpu *vcpu,struct vgic_irq *irq,int lr); +void vgic_v3_clear_lr (struct kvm_vcpu *vcpu,int lr); +void vgic_v3_set_underflow (struct kvm_vcpu *vcpu); +#else +static inline void vgic_v3_process_maintenance (struct kvm_vcpu *vcpu) +{ +} + +static inline void vgic_v3_fold_lr_state (struct kvm_vcpu *vcpu) +{ +} + +static inline void vgic_v3_populate_lr (struct kvm_vcpu *vcpu, +struct vgic_irq *irq,int lr) +{ +} + +static inline void vgic_v3_clear_lr (struct kvm_vcpu *vcpu,int lr) +{ +} + +static inline void vgic_v3_set_underflow (struct kvm_vcpu *vcpu) +{ +} +#endif +#endif
Signature · 1 line
2.8.2
PATCH v4 15/56 KVM: arm/arm64: vgic-new: Add acccessor to new struct vgic_irq instance
In reply to Andre PrzywaraFrom: Christoffer Dall <[email protected]>
The new VGIC implementation centers around a struct vgic_irq instance per virtual IRQ.
Provide a function to retrieve the right instance for a given IRQ number and (in case of private interrupts) the right VCPU.
--- virt/kvm/arm/vgic/vgic.c | 41 +++++++++++++++++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic.h | 22 ++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 virt/kvm/arm/vgic/vgic.c create mode 100644 virt/kvm/arm/vgic/vgic.h
virt/kvm/arm/vgic/vgic.c new file +41 -0
@@ -0,0 +1,41 @@ +/* +* Copyright (C) 2015, 2016 ARM Ltd. +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include <linux/kvm.h> +#include <linux/kvm_host.h> + +#include "vgic.h" + +struct vgic_global __section(.hyp.text) kvm_vgic_global_state; + +struct vgic_irq *vgic_get_irq (struct kvm *kvm,struct kvm_vcpu *vcpu, +u32 intid) +{ +/* SGIs and PPIs */ +if (intid <=VGIC_MAX_PRIVATE ) +return &vcpu->arch.vgic_cpu.private_irqs[intid]; + +/* SPIs */ +if (intid <=VGIC_MAX_SPI ) +return &kvm->arch.vgic.spis[intid -VGIC_NR_PRIVATE_IRQS ]; + +/* LPIs are not yet covered */ +if (intid >=VGIC_MIN_LPI ) +return NULL ; + +WARN (1 ,"Looking up struct vgic_irq for reserved INTID" ); +return NULL ; +}
virt/kvm/arm/vgic/vgic.h new file +22 -0
@@ -0,0 +1,22 @@ +/* +* Copyright (C) 2015, 2016 ARM Ltd. +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ +#ifndef __KVM_ARM_VGIC_NEW_H__ +#define __KVM_ARM_VGIC_NEW_H__ + +struct vgic_irq *vgic_get_irq (struct kvm *kvm,struct kvm_vcpu *vcpu, +u32 intid); + +#endif
Signature · 1 line
2.8.2
PATCH v4 17/56 KVM: arm/arm64: vgic-new: Add IRQ sorting
In reply to Andre PrzywaraFrom: Christoffer Dall <[email protected]>
Adds the sorting function to cover the case where you have more IRQs to consider than you have LRs. We now consider priorities.
--- virt/kvm/arm/vgic/vgic.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+)
virt/kvm/arm/vgic/vgic.c +57 -0
@@ -16,6 +16,7 @@#include <linux/kvm.h> #include <linux/kvm_host.h> +#include <linux/list_sort.h> #include "vgic.h"
@@ -103,6 +104,62 @@ static struct kvm_vcpu *vgic_target_oracle(struct vgic_irq *irq) }/* +* The order of items in the ap_lists defines how we'll pack things in LRs as +* well, the first items in the list being the first things populated in the +* LRs. +* +* A hard rule is that active interrupts can never be pushed out of the LRs +* (and therefore take priority) since we cannot reliably trap on deactivation +* of IRQs and therefore they have to be present in the LRs. +* +* Otherwise things should be sorted by the priority field and the GIC +* hardware support will take care of preemption of priority groups etc. +* +* Return negative if "a" sorts before "b", 0 to preserve order, and positive +* to sort "b" before "a". +*/ +static int vgic_irq_cmp (void *priv,struct list_head *a,struct list_head *b) +{ + struct vgic_irq *irqa = container_of(a, struct vgic_irq, ap_list); + struct vgic_irq *irqb = container_of(b, struct vgic_irq, ap_list); +bool penda, pendb; +int ret; + +spin_lock (&irqa->irq_lock); +spin_lock (&irqb->irq_lock); + +if (irqa->active || irqb->active) { + ret = (int )irqb->active - (int )irqa->active; + goto out; + } + + penda = irqa->enabled && irqa->pending; + pendb = irqb->enabled && irqb->pending; + +if (!penda || !pendb) { + ret = (int )pendb - (int )penda; + goto out; + } + +/* Both pending and enabled, sort by priority */ + ret = irqa->priority - irqb->priority; +out: +spin_unlock (&irqb->irq_lock); +spin_unlock (&irqa->irq_lock); +return ret; +} + +/* Must be called with the ap_list_lock held */ +static void vgic_sort_ap_list (struct kvm_vcpu *vcpu) +{ +struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; + +DEBUG_SPINLOCK_BUG_ON (!spin_is_locked (&vgic_cpu->ap_list_lock)); + +list_sort (NULL , &vgic_cpu->ap_list_head, vgic_irq_cmp); +} + +/* * Only valid injection if changing level for level-triggered IRQs or for a * rising edge. */
Signature · 1 line
2.8.2
PATCH v4 22/56 KVM: arm/arm64: vgic-new: Add MMIO handling framework
In reply to Andre PrzywaraFrom: Marc Zyngier <[email protected]>
Add an MMIO handling framework to the VGIC emulation:
Each register is described by its offset, size (or number of bits per IRQ, if applicable) and the read/write handler functions. We provide initialization macros to describe each GIC register later easily.
Separate dispatch functions for read and write accesses are connected to the kvm_io_bus framework and binary-search for the responsible register handler based on the offset address within the region. We convert the incoming data (referenced by a pointer) to the host's endianess and use pass-by-value to hand the data over to the actual handler functions.
The register handler prototype and the endianess conversion are courtesy of Christoffer Dall.
---
Changelog RFC..v1:
- rework MMIO dispatching to use only one kvm_io_bus device
- document purpose of register region macros
- rename "this" parameter to "dev"
- change IGROUPR to be RAO (returning 1 => Group1 IRQs)
Changelog v1 .. v2:
* MASSIVE rework:
- store register_region pointer in kvm_io_bus linked struct
- replace write_mask_xxx functions with extract_bytes() implementation
- change handler functions' prototypes to take and return unsigned long
- use binary search to find matching register handler
- convert endianess of input data in dispatch_mmio_xxx functions
- improve readability of register initializer macros
- remove any GICv2/GICv3 specific functions from vgic-mmio.c
- rename file from vgic_mmio.c to vgic-mmio.c
Changelog v2 .. v3:
- replace inclusion of vgic/vgic.h with arm_vgic.h
Changelog v3 .. v4:
- add IRQ number accessor macro
- check access width in dispatcher
- treat non-covered MMIO addresses as RAZ/WI
- remove extract_bytes() (re-introduced as static later in the series)
include/kvm/vgic/vgic.h | 13 +++ virt/kvm/arm/vgic/vgic-mmio.c | 184 ++++++++++++++++++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic-mmio.h | 87 ++++++++++++++++++++ 3 files changed, 284 insertions(+) create mode 100644 virt/kvm/arm/vgic/vgic-mmio.c create mode 100644 virt/kvm/arm/vgic/vgic-mmio.h
include/kvm/vgic/vgic.h +13 -0
@@ -106,6 +106,16 @@ struct vgic_irq {enum vgic_irq_config config;/* Level or edge */ }; +struct vgic_register_region ; + +struct vgic_io_device { +gpa_t base_addr; +struct kvm_vcpu *redist_vcpu; +const struct vgic_register_region *regions; +int nr_regions; +struct kvm_io_device dev; +}; +struct vgic_dist {bool in_kernel;bool ready;
@@ -132,6 +142,9 @@ struct vgic_dist {bool enabled;struct vgic_irq *spis; + +struct vgic_io_device dist_iodev; +struct vgic_io_device *redist_iodevs; };struct vgic_v2_cpu_if {
virt/kvm/arm/vgic/vgic-mmio.c new file +184 -0
184 lines not on this page · raw message
virt/kvm/arm/vgic/vgic-mmio.h new file +87 -0
87 lines not on this page · raw message
Signature · 1 line
2.8.2
PATCH v4 19/56 KVM: arm/arm64: vgic-new: Add GICv2 world switch backend
In reply to Andre PrzywaraFrom: Marc Zyngier <[email protected]>
Processing maintenance interrupts and accessing the list registers are dependent on the host's GIC version.
Introduce vgic-v2.c to contain GICv2 specific functions. Implement the GICv2 specific code for syncing the emulation state into the VGIC registers.
---
Changelog RFC..v1:
- remove explicit LR_STATE clearing on maintenance interrupt handling
- improve documentation for vgic_v2_populate_lr()
- remove WARN_ON on non-edge IRQs in maintenance interrupts
- simplify multi-CPU source SGI handling
Changelog v1 .. v2:
- inject the IRQ priority into the list register
Changelog v2 .. v3:
- cleanup diff containing rebase artifacts
Changelog v3 .. v4:
- remove ELRSR bit set during process_maintenance()
include/linux/irqchip/arm-gic.h | 1 + virt/kvm/arm/vgic/vgic-v2.c | 176 ++++++++++++++++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic.c | 6 ++ virt/kvm/arm/vgic/vgic.h | 6 ++ 4 files changed, 189 insertions(+) create mode 100644 virt/kvm/arm/vgic/vgic-v2.c
include/linux/irqchip/arm-gic.h +1 -0
@@ -76,6 +76,7 @@#define GICH_LR_VIRTUALID (0x3ff << 0)#define GICH_LR_PHYSID_CPUID_SHIFT (10)#define GICH_LR_PHYSID_CPUID (0x3ff << GICH_LR_PHYSID_CPUID_SHIFT) +#define GICH_LR_PRIORITY_SHIFT 23#define GICH_LR_STATE (3 << 28)#define GICH_LR_PENDING_BIT (1 << 28)#define GICH_LR_ACTIVE_BIT (1 << 29)
virt/kvm/arm/vgic/vgic-v2.c new file +176 -0
176 lines not on this page · raw message
virt/kvm/arm/vgic/vgic.c +6 -0
@@ -399,10 +399,12 @@ retry:static inline void vgic_process_maintenance_interrupt (struct kvm_vcpu *vcpu) { +vgic_v2_process_maintenance (vcpu); }static inline void vgic_fold_lr_state (struct kvm_vcpu *vcpu) { +vgic_v2_fold_lr_state (vcpu); }/* Requires the irq_lock to be held. */
@@ -410,14 +412,18 @@ static inline void vgic_populate_lr(struct kvm_vcpu *vcpu, struct vgic_irq *irq, int lr) {DEBUG_SPINLOCK_BUG_ON (!spin_is_locked (&irq->irq_lock)); + +vgic_v2_populate_lr (vcpu, irq, lr); }static inline void vgic_clear_lr (struct kvm_vcpu *vcpu,int lr) { +vgic_v2_clear_lr (vcpu, lr); }static inline void vgic_set_underflow (struct kvm_vcpu *vcpu) { +vgic_v2_set_underflow (vcpu); }/* Requires the ap_list_lock to be held. */
virt/kvm/arm/vgic/vgic.h +6 -0
@@ -22,4 +22,10 @@ struct vgic_irq *vgic_get_irq(struct kvm *kvm, struct kvm_vcpu *vcpu, u32 intid);bool vgic_queue_irq_unlock (struct kvm *kvm,struct vgic_irq *irq); +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); +void vgic_v2_clear_lr (struct kvm_vcpu *vcpu,int lr); +void vgic_v2_set_underflow (struct kvm_vcpu *vcpu); +#endif
Signature · 1 line
2.8.2
PATCH v4 21/56 KVM: arm/arm64: vgic-new: Implement kvm_vgic_vcpu_pending_irq
In reply to Andre PrzywaraFrom: Eric Auger <[email protected]>
Tell KVM whether a particular VCPU has an IRQ that needs handling in the guest. This is used to decide whether a VCPU is runnable.
---
Changelog RFC..v1:
- return false if distributor is disabled
- add vgic_kick_vcpus() implementations
Changelog v2 .. v3:
- remove vgic_kick_vcpus() implementation from this patch
include/kvm/vgic/vgic.h | 2 ++ virt/kvm/arm/vgic/vgic.c | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+)
include/kvm/vgic/vgic.h +2 -0
@@ -184,6 +184,8 @@ struct vgic_cpu {int kvm_vgic_inject_irq (struct kvm *kvm,int cpuid,unsigned int intid,bool level); +int kvm_vgic_vcpu_pending_irq (struct kvm_vcpu *vcpu); +#define irqchip_in_kernel (k) (!!((k)->arch.vgic.in_kernel))#define vgic_initialized (k) (false)#define vgic_ready (k) ((k)->arch.vgic.ready)
virt/kvm/arm/vgic/vgic.c +25 -0
@@ -519,3 +519,28 @@ void kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu)vgic_flush_lr_state (vcpu);spin_unlock (&vcpu->arch.vgic_cpu.ap_list_lock); } + +int kvm_vgic_vcpu_pending_irq (struct kvm_vcpu *vcpu) +{ +struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; +struct vgic_irq *irq; +bool pending = false; + +if (!vcpu->kvm->arch.vgic.enabled) +return false; + +spin_lock (&vgic_cpu->ap_list_lock); + +list_for_each_entry (irq, &vgic_cpu->ap_list_head, ap_list) { +spin_lock (&irq->irq_lock); + pending = irq->pending && irq->enabled; +spin_unlock (&irq->irq_lock); + +if (pending) +break ; + } + +spin_unlock (&vgic_cpu->ap_list_lock); + +return pending; +}
Signature · 1 line
2.8.2
PATCH v4 24/56 KVM: arm/arm64: vgic-new: Add CTLR, TYPER and IIDR handlers
In reply to Andre PrzywaraFrom: Marc Zyngier <[email protected]>
Those three registers are v2 emulation specific, so their implementation lives entirely in vgic-mmio-v2.c. Also they are handled in one function, as their implementation is pretty simple.
When the guest enables the distributor, we kick all VCPUs to get potentially pending interrupts serviced.
---
Changelog RFC..v1:
- kick VCPUs is the distributor gets enabled
- improve comment
Changelog v1 .. v2:
- adapt to new MMIO framework
- use switch() statements to improve readability
Changelog v2 .. v3:
- add vgic_kick_vcpus() implementation
Changelog v3 .. v4;
- specify accessor width
- simplify CTLR write access
- replace extract_bytes() with simple return
include/linux/irqchip/arm-gic.h | 1 + virt/kvm/arm/vgic/vgic-mmio-v2.c | 46 +++++++++++++++++++++++++++++++++++++++- virt/kvm/arm/vgic/vgic.c | 15 +++++++++++++ virt/kvm/arm/vgic/vgic.h | 4 ++++ 4 files changed, 65 insertions(+), 1 deletion(-)
include/linux/irqchip/arm-gic.h +1 -0
@@ -33,6 +33,7 @@#define GIC_DIST_CTRL 0x000#define GIC_DIST_CTR 0x004 +#define GIC_DIST_IIDR 0x008#define GIC_DIST_IGROUP 0x080#define GIC_DIST_ENABLE_SET 0x100#define GIC_DIST_ENABLE_CLEAR 0x180
virt/kvm/arm/vgic/vgic-mmio-v2.c +45 -1
54 lines not on this page · raw message
virt/kvm/arm/vgic/vgic.c +15 -0
@@ -544,3 +544,18 @@ int kvm_vgic_vcpu_pending_irq(struct kvm_vcpu *vcpu)return pending; } + +void vgic_kick_vcpus (struct kvm *kvm) +{ +struct kvm_vcpu *vcpu; +int c; + +/* +* We've injected an interrupt, time to find out who deserves +* a good kick... +*/ +kvm_for_each_vcpu (c, vcpu, kvm) { +if (kvm_vgic_vcpu_pending_irq (vcpu)) +kvm_vcpu_kick (vcpu); + } +}
virt/kvm/arm/vgic/vgic.h +4 -0
@@ -16,11 +16,15 @@#ifndef __KVM_ARM_VGIC_NEW_H__#define __KVM_ARM_VGIC_NEW_H__ +#define PRODUCT_ID_KVM 0x4b/* ASCII code K */ +#define IMPLEMENTER_ARM 0x43b +#define vgic_irq_is_sgi (intid) ((intid) < VGIC_NR_SGIS)struct vgic_irq *vgic_get_irq (struct kvm *kvm,struct kvm_vcpu *vcpu,u32 intid);bool vgic_queue_irq_unlock (struct kvm *kvm,struct vgic_irq *irq); +void vgic_kick_vcpus (struct kvm *kvm);void vgic_v2_process_maintenance (struct kvm_vcpu *vcpu);void vgic_v2_fold_lr_state (struct kvm_vcpu *vcpu);
Signature · 1 line
2.8.2
PATCH v4 22/56 KVM: arm/arm64: vgic-new: Add MMIO handling framework
In reply to Andre PrzywaraFrom: Marc Zyngier <[email protected]>
Add an MMIO handling framework to the VGIC emulation:
Each register is described by its offset, size (or number of bits per IRQ, if applicable) and the read/write handler functions. We provide initialization macros to describe each GIC register later easily.
Separate dispatch functions for read and write accesses are connected to the kvm_io_bus framework and binary-search for the responsible register handler based on the offset address within the region. We convert the incoming data (referenced by a pointer) to the host's endianess and use pass-by-value to hand the data over to the actual handler functions.
The register handler prototype and the endianess conversion are courtesy of Christoffer Dall.
---
Changelog RFC..v1:
- rework MMIO dispatching to use only one kvm_io_bus device
- document purpose of register region macros
- rename "this" parameter to "dev"
- change IGROUPR to be RAO (returning 1 => Group1 IRQs)
Changelog v1 .. v2:
* MASSIVE rework:
- store register_region pointer in kvm_io_bus linked struct
- replace write_mask_xxx functions with extract_bytes() implementation
- change handler functions' prototypes to take and return unsigned long
- use binary search to find matching register handler
- convert endianess of input data in dispatch_mmio_xxx functions
- improve readability of register initializer macros
- remove any GICv2/GICv3 specific functions from vgic-mmio.c
- rename file from vgic_mmio.c to vgic-mmio.c
Changelog v2 .. v3:
- replace inclusion of vgic/vgic.h with arm_vgic.h
Changelog v3 .. v4:
- add IRQ number accessor macro
- check access width in dispatcher
- treat non-covered MMIO addresses as RAZ/WI
- remove extract_bytes() (re-introduced as static later in the series)
include/kvm/vgic/vgic.h | 13 +++ virt/kvm/arm/vgic/vgic-mmio.c | 184 ++++++++++++++++++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic-mmio.h | 87 ++++++++++++++++++++ 3 files changed, 284 insertions(+) create mode 100644 virt/kvm/arm/vgic/vgic-mmio.c create mode 100644 virt/kvm/arm/vgic/vgic-mmio.h
include/kvm/vgic/vgic.h +13 -0
@@ -106,6 +106,16 @@ struct vgic_irq {enum vgic_irq_config config;/* Level or edge */ }; +struct vgic_register_region ; + +struct vgic_io_device { +gpa_t base_addr; +struct kvm_vcpu *redist_vcpu; +const struct vgic_register_region *regions; +int nr_regions; +struct kvm_io_device dev; +}; +struct vgic_dist {bool in_kernel;bool ready;
@@ -132,6 +142,9 @@ struct vgic_dist {bool enabled;struct vgic_irq *spis; + +struct vgic_io_device dist_iodev; +struct vgic_io_device *redist_iodevs; };struct vgic_v2_cpu_if {
virt/kvm/arm/vgic/vgic-mmio.c new file +184 -0
184 lines not on this page · raw message
virt/kvm/arm/vgic/vgic-mmio.h new file +87 -0
87 lines not on this page · raw message
Signature · 1 line
2.8.2
PATCH v4 23/56 KVM: arm/arm64: vgic-new: Add GICv2 MMIO handling framework
In reply to Andre PrzywaraCreate vgic-mmio-v2.c to describe GICv2 emulation specific handlers using the initializer macros provided by the VGIC MMIO framework. Provide a function to register the GICv2 distributor registers to the kvm_io_bus framework.
The actual handler functions are still stubs in this patch.
---
Changelog v1 .. v2:
- new patch, split out from the generic MMIO framework patch
- use a separate file to hold GICv2 emulation specific handlers
- replace _nyi stub functions with raz/wi versions
Changelog v2 .. v3:
- replace kvm/vgic/vgic.h with kvm/arm_vgic.h
- add vgic_register_dist_iodev() prototype
Changelog v3 .. v4:
- specify accessor width
virt/kvm/arm/vgic/vgic-mmio-v2.c | 76 ++++++++++++++++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic-mmio.c | 26 ++++++++++++++ virt/kvm/arm/vgic/vgic-mmio.h | 2 ++ virt/kvm/arm/vgic/vgic.h | 2 ++ 4 files changed, 106 insertions(+) create mode 100644 virt/kvm/arm/vgic/vgic-mmio-v2.c
virt/kvm/arm/vgic/vgic-mmio-v2.c new file +76 -0
@@ -0,0 +1,76 @@ +/* +* VGICv2 MMIO handling functions +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +*/ + +#include <linux/irqchip/arm-gic.h> +#include <linux/kvm.h> +#include <linux/kvm_host.h> +#include <kvm/iodev.h> +#include <kvm/arm_vgic.h> + +#include "vgic.h" +#include "vgic-mmio.h" + +static const struct vgic_register_region vgic_v2_dist_registers[] = { +REGISTER_DESC_WITH_LENGTH (GIC_DIST_CTRL , + vgic_mmio_read_raz, vgic_mmio_write_wi,12 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_BITS_PER_IRQ (GIC_DIST_IGROUP , + vgic_mmio_read_rao, vgic_mmio_write_wi,1 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_BITS_PER_IRQ (GIC_DIST_ENABLE_SET , + vgic_mmio_read_raz, vgic_mmio_write_wi,1 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_BITS_PER_IRQ (GIC_DIST_ENABLE_CLEAR , + vgic_mmio_read_raz, vgic_mmio_write_wi,1 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_BITS_PER_IRQ (GIC_DIST_PENDING_SET , + vgic_mmio_read_raz, vgic_mmio_write_wi,1 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_BITS_PER_IRQ (GIC_DIST_PENDING_CLEAR , + vgic_mmio_read_raz, vgic_mmio_write_wi,1 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_BITS_PER_IRQ (GIC_DIST_ACTIVE_SET , + vgic_mmio_read_raz, vgic_mmio_write_wi,1 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_BITS_PER_IRQ (GIC_DIST_ACTIVE_CLEAR , + vgic_mmio_read_raz, vgic_mmio_write_wi,1 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_BITS_PER_IRQ (GIC_DIST_PRI , + vgic_mmio_read_raz, vgic_mmio_write_wi,8 , + VGIC_ACCESS_32bit | VGIC_ACCESS_8bit), +REGISTER_DESC_WITH_BITS_PER_IRQ (GIC_DIST_TARGET , + vgic_mmio_read_raz, vgic_mmio_write_wi,8 , + VGIC_ACCESS_32bit | VGIC_ACCESS_8bit), +REGISTER_DESC_WITH_BITS_PER_IRQ (GIC_DIST_CONFIG , + vgic_mmio_read_raz, vgic_mmio_write_wi,2 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_LENGTH (GIC_DIST_SOFTINT , + vgic_mmio_read_raz, vgic_mmio_write_wi,4 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_LENGTH (GIC_DIST_SGI_PENDING_CLEAR , + vgic_mmio_read_raz, vgic_mmio_write_wi,16 , + VGIC_ACCESS_32bit | VGIC_ACCESS_8bit), +REGISTER_DESC_WITH_LENGTH (GIC_DIST_SGI_PENDING_SET , + vgic_mmio_read_raz, vgic_mmio_write_wi,16 , + VGIC_ACCESS_32bit | VGIC_ACCESS_8bit), +}; + +unsigned int vgic_v2_init_dist_iodev (struct vgic_io_device *dev) +{ + dev->regions = vgic_v2_dist_registers; + dev->nr_regions =ARRAY_SIZE (vgic_v2_dist_registers); + +kvm_iodevice_init (&dev->dev, &kvm_io_gic_ops); + +return SZ_4K ; +}
virt/kvm/arm/vgic/vgic-mmio.c +26 -0
@@ -182,3 +182,29 @@ struct kvm_io_device_ops kvm_io_gic_ops = { .read = dispatch_mmio_read, .write = dispatch_mmio_write, }; + +int vgic_register_dist_iodev (struct kvm *kvm,gpa_t dist_base_address, +enum vgic_type type) +{ +struct vgic_io_device *io_device = &kvm->arch.vgic.dist_iodev; +int ret =0 ; +unsigned int len; + +switch (type) { +case VGIC_V2 : + len =vgic_v2_init_dist_iodev (io_device); +break ; +default : +BUG_ON (1 ); + } + + io_device->base_addr = dist_base_address; + io_device->redist_vcpu =NULL ; + +mutex_lock (&kvm->slots_lock); + ret =kvm_io_bus_register_dev (kvm,KVM_MMIO_BUS , dist_base_address, + len, &io_device->dev); +mutex_unlock (&kvm->slots_lock); + +return ret; +}
virt/kvm/arm/vgic/vgic-mmio.h +2 -0
@@ -84,4 +84,6 @@ unsigned long vgic_mmio_read_rao(struct kvm_vcpu *vcpu,void vgic_mmio_write_wi (struct kvm_vcpu *vcpu,gpa_t addr,unsigned int len,unsigned long val); +unsigned int vgic_v2_init_dist_iodev (struct vgic_io_device *dev); +#endif
virt/kvm/arm/vgic/vgic.h +2 -0
@@ -27,6 +27,8 @@ 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);void vgic_v2_clear_lr (struct kvm_vcpu *vcpu,int lr);void vgic_v2_set_underflow (struct kvm_vcpu *vcpu); +int vgic_register_dist_iodev (struct kvm *kvm,gpa_t dist_base_address, +enum vgic_type );#ifdef CONFIG_KVM_ARM_VGIC_V3 void vgic_v3_process_maintenance (struct kvm_vcpu *vcpu);
Signature · 1 line
2.8.2
PATCH v4 26/56 KVM: arm/arm64: vgic-new: Add PENDING registers handlers
In reply to Andre PrzywaraThe pending register handlers are shared between the v2 and v3 emulation, so their implementation goes into vgic-mmio.c, to be easily referenced from the v3 emulation as well later.
For level triggered interrupts the real line level is unaffected by this write, so we keep this state separate and combine it with the device's level to get the actual pending state.
---
Changelog RFC..v1:
- remove IRQ lock from read handler
- remove TODO from clear pending handler
Changelog v1 .. v2:
- adapt to new MMIO framework
Changelog v3 .. v4:
- specify accessor width
- use IRQ number accessor macro
- replace extract_bytes() with simple return
virt/kvm/arm/vgic/vgic-mmio-v2.c | 4 +-- virt/kvm/arm/vgic/vgic-mmio.c | 60 ++++++++++++++++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic-mmio.h | 12 ++++++++ 3 files changed, 74 insertions(+), 2 deletions(-)
virt/kvm/arm/vgic/vgic-mmio-v2.c +2 -2
@@ -78,10 +78,10 @@ static const struct vgic_register_region vgic_v2_dist_registers[] = { vgic_mmio_read_enable, vgic_mmio_write_cenable, 1, VGIC_ACCESS_32bit),REGISTER_DESC_WITH_BITS_PER_IRQ (GIC_DIST_PENDING_SET , - vgic_mmio_read_raz, vgic_mmio_write_wi,1 , + vgic_mmio_read_pending, vgic_mmio_write_spending,1 , VGIC_ACCESS_32bit),REGISTER_DESC_WITH_BITS_PER_IRQ (GIC_DIST_PENDING_CLEAR , - vgic_mmio_read_raz, vgic_mmio_write_wi,1 , + vgic_mmio_read_pending, vgic_mmio_write_cpending,1 , VGIC_ACCESS_32bit), REGISTER_DESC_WITH_BITS_PER_IRQ(GIC_DIST_ACTIVE_SET, vgic_mmio_read_raz, vgic_mmio_write_wi, 1,
virt/kvm/arm/vgic/vgic-mmio.c +60 -0
66 lines not on this page · raw message
virt/kvm/arm/vgic/vgic-mmio.h +12 -0
@@ -95,6 +95,18 @@ void vgic_mmio_write_cenable(struct kvm_vcpu *vcpu, gpa_t addr, unsigned int len, unsigned long val); +unsigned long vgic_mmio_read_pending (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len); + +void vgic_mmio_write_spending (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len, +unsigned long val); + +void vgic_mmio_write_cpending (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len, +unsigned long val); + +unsigned int vgic_v2_init_dist_iodev (struct vgic_io_device *dev);#endif
Signature · 1 line
2.8.2
PATCH v4 27/56 KVM: arm/arm64: vgic-new: Add ACTIVE registers handlers
In reply to Andre PrzywaraThe active register handlers are shared between the v2 and v3 emulation, so their implementation goes into vgic-mmio.c, to be easily referenced from the v3 emulation as well later. Since activation/deactivation of an interrupt may happen entirely in the guest without it ever exiting, we need some extra logic to properly track the active state.
Putting it on an ap_list on activation is similar to the normal case handled by vgic_queue_irq_unlock(), but differs in some details that make a separate implementation worthwhile.
---
Changelog RFC..v1:
- handling queueing in write handler
- remove IRQ lock from read handler
Changelog v1 .. v2:
- adapt to new MMIO framework
Changelog v3 .. v4:
- specify accessor width
- use IRQ number accessor macro
- drop elaborate write_sactive handler and use new vgic_queue_irq_unlock()
- properly emulate clear active by halting the guest (Christoffer)
- replace extract_bytes() with simple return
virt/kvm/arm/vgic/vgic-mmio-v2.c | 4 +- virt/kvm/arm/vgic/vgic-mmio.c | 82 ++++++++++++++++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic-mmio.h | 10 +++++ 3 files changed, 94 insertions(+), 2 deletions(-)
virt/kvm/arm/vgic/vgic-mmio-v2.c +2 -2
@@ -84,10 +84,10 @@ static const struct vgic_register_region vgic_v2_dist_registers[] = { vgic_mmio_read_pending, vgic_mmio_write_cpending, 1, VGIC_ACCESS_32bit),REGISTER_DESC_WITH_BITS_PER_IRQ (GIC_DIST_ACTIVE_SET , - vgic_mmio_read_raz, vgic_mmio_write_wi,1 , + vgic_mmio_read_active, vgic_mmio_write_sactive,1 , VGIC_ACCESS_32bit),REGISTER_DESC_WITH_BITS_PER_IRQ (GIC_DIST_ACTIVE_CLEAR , - vgic_mmio_read_raz, vgic_mmio_write_wi,1 , + vgic_mmio_read_active, vgic_mmio_write_cactive,1 , VGIC_ACCESS_32bit), REGISTER_DESC_WITH_BITS_PER_IRQ(GIC_DIST_PRI, vgic_mmio_read_raz, vgic_mmio_write_wi, 8,
virt/kvm/arm/vgic/vgic-mmio.c +82 -0
88 lines not on this page · raw message
virt/kvm/arm/vgic/vgic-mmio.h +10 -0
@@ -106,6 +106,16 @@ void vgic_mmio_write_cpending(struct kvm_vcpu *vcpu, gpa_t addr, unsigned int len, unsigned long val); +unsigned long vgic_mmio_read_active (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len); + +void vgic_mmio_write_cactive (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len, +unsigned long val); + +void vgic_mmio_write_sactive (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len, +unsigned long val);unsigned int vgic_v2_init_dist_iodev (struct vgic_io_device *dev);
Signature · 1 line
2.8.2
PATCH v4 26/56 KVM: arm/arm64: vgic-new: Add PENDING registers handlers
In reply to Andre PrzywaraThe pending register handlers are shared between the v2 and v3 emulation, so their implementation goes into vgic-mmio.c, to be easily referenced from the v3 emulation as well later.
For level triggered interrupts the real line level is unaffected by this write, so we keep this state separate and combine it with the device's level to get the actual pending state.
---
Changelog RFC..v1:
- remove IRQ lock from read handler
- remove TODO from clear pending handler
Changelog v1 .. v2:
- adapt to new MMIO framework
Changelog v3 .. v4:
- specify accessor width
- use IRQ number accessor macro
- replace extract_bytes() with simple return
virt/kvm/arm/vgic/vgic-mmio-v2.c | 4 +-- virt/kvm/arm/vgic/vgic-mmio.c | 60 ++++++++++++++++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic-mmio.h | 12 ++++++++ 3 files changed, 74 insertions(+), 2 deletions(-)
virt/kvm/arm/vgic/vgic-mmio-v2.c +2 -2
@@ -78,10 +78,10 @@ static const struct vgic_register_region vgic_v2_dist_registers[] = { vgic_mmio_read_enable, vgic_mmio_write_cenable, 1, VGIC_ACCESS_32bit),REGISTER_DESC_WITH_BITS_PER_IRQ (GIC_DIST_PENDING_SET , - vgic_mmio_read_raz, vgic_mmio_write_wi,1 , + vgic_mmio_read_pending, vgic_mmio_write_spending,1 , VGIC_ACCESS_32bit),REGISTER_DESC_WITH_BITS_PER_IRQ (GIC_DIST_PENDING_CLEAR , - vgic_mmio_read_raz, vgic_mmio_write_wi,1 , + vgic_mmio_read_pending, vgic_mmio_write_cpending,1 , VGIC_ACCESS_32bit), REGISTER_DESC_WITH_BITS_PER_IRQ(GIC_DIST_ACTIVE_SET, vgic_mmio_read_raz, vgic_mmio_write_wi, 1,
virt/kvm/arm/vgic/vgic-mmio.c +60 -0
66 lines not on this page · raw message
virt/kvm/arm/vgic/vgic-mmio.h +12 -0
@@ -95,6 +95,18 @@ void vgic_mmio_write_cenable(struct kvm_vcpu *vcpu, gpa_t addr, unsigned int len, unsigned long val); +unsigned long vgic_mmio_read_pending (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len); + +void vgic_mmio_write_spending (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len, +unsigned long val); + +void vgic_mmio_write_cpending (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len, +unsigned long val); + +unsigned int vgic_v2_init_dist_iodev (struct vgic_io_device *dev);#endif
Signature · 1 line
2.8.2
PATCH v4 28/56 KVM: arm/arm64: vgic-new: Add PRIORITY registers handlers
In reply to Andre PrzywaraThe priority register handlers are shared between the v2 and v3 emulation, so their implementation goes into vgic-mmio.c, to be easily referenced from the v3 emulation as well later. There is a corner case when we change the priority of a pending interrupt which we don't handle at the moment.
---
Changelog v1 .. v2:
- adapt to new MMIO framework
Changelog v3 .. v4:
- specify accessor width
- use IRQ number accessor macro
virt/kvm/arm/vgic/vgic-mmio-v2.c | 2 +- virt/kvm/arm/vgic/vgic-mmio.c | 40 ++++++++++++++++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic-mmio.h | 7 +++++++ virt/kvm/arm/vgic/vgic.h | 2 ++ 4 files changed, 50 insertions(+), 1 deletion(-)
virt/kvm/arm/vgic/vgic-mmio-v2.c +1 -1
@@ -90,7 +90,7 @@ static const struct vgic_register_region vgic_v2_dist_registers[] = { vgic_mmio_read_active, vgic_mmio_write_cactive, 1, VGIC_ACCESS_32bit),REGISTER_DESC_WITH_BITS_PER_IRQ (GIC_DIST_PRI , - vgic_mmio_read_raz, vgic_mmio_write_wi,8 , + vgic_mmio_read_priority, vgic_mmio_write_priority,8 , VGIC_ACCESS_32bit | VGIC_ACCESS_8bit), REGISTER_DESC_WITH_BITS_PER_IRQ(GIC_DIST_TARGET, vgic_mmio_read_raz, vgic_mmio_write_wi, 8,
virt/kvm/arm/vgic/vgic-mmio.c +40 -0
@@ -237,6 +237,46 @@ void vgic_mmio_write_sactive(struct kvm_vcpu *vcpu, } } +unsigned long vgic_mmio_read_priority (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len) +{ +u32 intid =VGIC_ADDR_TO_INTID (addr,8 ); +int i; +u64 val =0 ; + +for (i =0 ; i < len; i++) { +struct vgic_irq *irq =vgic_get_irq (vcpu->kvm, vcpu, intid + i); + + val |= (u64 )irq->priority << (i *8 ); + } + +return val; +} + +/* +* We currently don't handle changing the priority of an interrupt that +* is already pending on a VCPU. If there is a need for this, we would +* need to make this VCPU exit and re-evaluate the priorities, potentially +* leading to this interrupt getting presented now to the guest (if it has +* been masked by the priority mask before). +*/ +void vgic_mmio_write_priority (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len, +unsigned long val) +{ +u32 intid =VGIC_ADDR_TO_INTID (addr,8 ); +int i; + +for (i =0 ; i < len; i++) { +struct vgic_irq *irq =vgic_get_irq (vcpu->kvm, vcpu, intid + i); + +spin_lock (&irq->irq_lock); +/* Narrow the priority range to what we actually support */ + irq->priority = (val >> (i *8 )) &GENMASK (7 ,8 -VGIC_PRI_BITS ); +spin_unlock (&irq->irq_lock); + } +} +static int match_region (const void *key,const void *elt) {const unsigned int offset = (unsigned long )key;
virt/kvm/arm/vgic/vgic-mmio.h +7 -0
@@ -117,6 +117,13 @@ void vgic_mmio_write_sactive(struct kvm_vcpu *vcpu, gpa_t addr, unsigned int len, unsigned long val); +unsigned long vgic_mmio_read_priority (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len); + +void vgic_mmio_write_priority (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len, +unsigned long val); +unsigned int vgic_v2_init_dist_iodev (struct vgic_io_device *dev);#endif
virt/kvm/arm/vgic/vgic.h +2 -0
@@ -19,6 +19,8 @@#define PRODUCT_ID_KVM 0x4b/* ASCII code K */ #define IMPLEMENTER_ARM 0x43b +#define VGIC_PRI_BITS 5 +#define vgic_irq_is_sgi (intid) ((intid) < VGIC_NR_SGIS) struct vgic_irq *vgic_get_irq(struct kvm *kvm, struct kvm_vcpu *vcpu,
Signature · 1 line
2.8.2
PATCH v4 29/56 KVM: arm/arm64: vgic-new: Add CONFIG registers handlers
In reply to Andre PrzywaraThe config register handlers are shared between the v2 and v3 emulation, so their implementation goes into vgic-mmio.c, to be easily referenced from the v3 emulation as well later.
---
Changelog v1 .. v2:
- adapt to new MMIO framework
Changelog v3 .. v4:
- specify accessor width
- use IRQ number accessor macro
- replace extract_bytes() with simple return
virt/kvm/arm/vgic/vgic-mmio-v2.c | 2 +- virt/kvm/arm/vgic/vgic-mmio.c | 46 ++++++++++++++++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic-mmio.h | 7 ++++++ 3 files changed, 54 insertions(+), 1 deletion(-)
virt/kvm/arm/vgic/vgic-mmio-v2.c +1 -1
@@ -96,7 +96,7 @@ static const struct vgic_register_region vgic_v2_dist_registers[] = { vgic_mmio_read_raz, vgic_mmio_write_wi, 8, VGIC_ACCESS_32bit | VGIC_ACCESS_8bit),REGISTER_DESC_WITH_BITS_PER_IRQ (GIC_DIST_CONFIG , - vgic_mmio_read_raz, vgic_mmio_write_wi,2 , + vgic_mmio_read_config, vgic_mmio_write_config,2 , VGIC_ACCESS_32bit), REGISTER_DESC_WITH_LENGTH(GIC_DIST_SOFTINT, vgic_mmio_read_raz, vgic_mmio_write_wi, 4,
virt/kvm/arm/vgic/vgic-mmio.c +46 -0
52 lines not on this page · raw message
virt/kvm/arm/vgic/vgic-mmio.h +7 -0
@@ -124,6 +124,13 @@ void vgic_mmio_write_priority(struct kvm_vcpu *vcpu, gpa_t addr, unsigned int len, unsigned long val); +unsigned long vgic_mmio_read_config (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len); + +void vgic_mmio_write_config (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len, +unsigned long val); +unsigned int vgic_v2_init_dist_iodev (struct vgic_io_device *dev);#endif
Signature · 1 line
2.8.2
PATCH v4 30/56 KVM: arm/arm64: vgic-new: Add TARGET registers handlers
In reply to Andre PrzywaraThe target register handlers are v2 emulation specific, so their implementation lives entirely in vgic-mmio-v2.c.
We copy the old VGIC behaviour of assigning an IRQ to the first VCPU set in the target mask instead of making it possibly pending on multiple VCPUs.
---
Changelog RFC..v1:
- remove runtime VCPU determination from this v2-only register
- fold in implementation of vgic_v2_irq_change_affinity()
- replace ffs() with __ffs()
Changelog v1 .. v2:
- adapt to new MMIO framework
Changelog v3 .. v4:
- specify accessor width
- use IRQ number accessor macro
virt/kvm/arm/vgic/vgic-mmio-v2.c | 43 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-)
virt/kvm/arm/vgic/vgic-mmio-v2.c +42 -1
@@ -64,6 +64,47 @@ static void vgic_mmio_write_v2_misc(struct kvm_vcpu *vcpu, } } +static unsigned long vgic_mmio_read_target (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len) +{ +u32 intid =VGIC_ADDR_TO_INTID (addr,8 ); +int i; +u64 val =0 ; + +for (i =0 ; i < len; i++) { +struct vgic_irq *irq =vgic_get_irq (vcpu->kvm, vcpu, intid + i); + + val |= (u64 )irq->targets << (i *8 ); + } + +return val; +} + +static void vgic_mmio_write_target (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len, +unsigned long val) +{ +u32 intid =VGIC_ADDR_TO_INTID (addr,8 ); +int i; + +/* GICD_ITARGETSR[0-7] are read-only */ +if (intid <VGIC_NR_PRIVATE_IRQS ) +return ; + +for (i =0 ; i < len; i++) { +struct vgic_irq *irq =vgic_get_irq (vcpu->kvm,NULL , intid + i); +int target; + +spin_lock (&irq->irq_lock); + + irq->targets = (val >> (i *8 )) &0xff ; + target = irq->targets ?__ffs (irq->targets) :0 ; + irq->target_vcpu =kvm_get_vcpu (vcpu->kvm, target); + +spin_unlock (&irq->irq_lock); + } +} +static const struct vgic_register_region vgic_v2_dist_registers[] = { REGISTER_DESC_WITH_LENGTH(GIC_DIST_CTRL, vgic_mmio_read_v2_misc, vgic_mmio_write_v2_misc, 12,
@@ -93,7 +134,7 @@ static const struct vgic_register_region vgic_v2_dist_registers[] = { vgic_mmio_read_priority, vgic_mmio_write_priority, 8, VGIC_ACCESS_32bit | VGIC_ACCESS_8bit),REGISTER_DESC_WITH_BITS_PER_IRQ (GIC_DIST_TARGET , - vgic_mmio_read_raz, vgic_mmio_write_wi,8 , + vgic_mmio_read_target, vgic_mmio_write_target,8 , VGIC_ACCESS_32bit | VGIC_ACCESS_8bit), REGISTER_DESC_WITH_BITS_PER_IRQ(GIC_DIST_CONFIG, vgic_mmio_read_config, vgic_mmio_write_config, 2,
Signature · 1 line
2.8.2
PATCH v4 31/56 KVM: arm/arm64: vgic-new: Add SGIR register handler
In reply to Andre PrzywaraTriggering an IPI via this register is v2 specific, so the implementation lives entirely in vgic-mmio-v2.c.
---
Changelog RFC..v1:
- silently return on illegal TargetListFilter value (=3)
Changelog v1 .. v2:
- adapt to new MMIO framework
Changelog v3 .. v4:
- specify accessor width
virt/kvm/arm/vgic/vgic-mmio-v2.c | 43 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-)
virt/kvm/arm/vgic/vgic-mmio-v2.c +42 -1
@@ -64,6 +64,47 @@ static void vgic_mmio_write_v2_misc(struct kvm_vcpu *vcpu, } } +static void vgic_mmio_write_sgir (struct kvm_vcpu *source_vcpu, +gpa_t addr,unsigned int len, +unsigned long val) +{ +int nr_vcpus =atomic_read (&source_vcpu->kvm->online_vcpus); +int intid = val &0xf ; +int targets = (val >>16 ) &0xff ; +int mode = (val >>24 ) &0x03 ; +int c; +struct kvm_vcpu *vcpu; + +switch (mode) { +case 0x0 :/* as specified by targets */ +break ; +case 0x1 : + targets = (1U << nr_vcpus) -1 ;/* all, ... */ + targets &= ~(1U << source_vcpu->vcpu_id);/* but self */ +break ; +case 0x2 :/* this very vCPU only */ + targets = (1U << source_vcpu->vcpu_id); +break ; +case 0x3 :/* reserved */ +return ; + } + +kvm_for_each_vcpu (c, vcpu, source_vcpu->kvm) { +struct vgic_irq *irq; + +if (!(targets & (1U << c))) +continue ; + + irq =vgic_get_irq (source_vcpu->kvm, vcpu, intid); + +spin_lock (&irq->irq_lock); + irq->pending = true; + irq->source |=1U << source_vcpu->vcpu_id; + +vgic_queue_irq_unlock (source_vcpu->kvm, irq); + } +} +static unsigned long vgic_mmio_read_target (struct kvm_vcpu *vcpu,gpa_t addr,unsigned int len) {
@@ -140,7 +181,7 @@ static const struct vgic_register_region vgic_v2_dist_registers[] = { vgic_mmio_read_config, vgic_mmio_write_config, 2, VGIC_ACCESS_32bit),REGISTER_DESC_WITH_LENGTH (GIC_DIST_SOFTINT , - vgic_mmio_read_raz, vgic_mmio_write_wi,4 , + vgic_mmio_read_raz, vgic_mmio_write_sgir,4 , VGIC_ACCESS_32bit), REGISTER_DESC_WITH_LENGTH(GIC_DIST_SGI_PENDING_CLEAR, vgic_mmio_read_raz, vgic_mmio_write_wi, 16,
Signature · 1 line
2.8.2
PATCH v4 32/56 KVM: arm/arm64: vgic-new: Add SGIPENDR register handlers
In reply to Andre PrzywaraAs this register is v2 specific, its implementation lives entirely in vgic-mmio-v2.c.
This register allows setting the source mask of an IPI.
---
Changelog RFC..v1:
- remove IRQ lock from read handler
- update pending bit on setting the first / clearing the last bit
- queue virtual IRQ if necessary
Changelog v1 .. v2:
- adapt to new MMIO framework
Changelog v3 .. v4:
- specify accessor width
virt/kvm/arm/vgic/vgic-mmio-v2.c | 62 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 60 insertions(+), 2 deletions(-)
virt/kvm/arm/vgic/vgic-mmio-v2.c +60 -2
@@ -146,6 +146,64 @@ static void vgic_mmio_write_target(struct kvm_vcpu *vcpu, } } +static unsigned long vgic_mmio_read_sgipend (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len) +{ +u32 intid = addr &0x0f ; +int i; +u64 val =0 ; + +for (i =0 ; i < len; i++) { +struct vgic_irq *irq =vgic_get_irq (vcpu->kvm, vcpu, intid + i); + + val |= (u64 )irq->source << (i *8 ); + } +return val; +} + +static void vgic_mmio_write_sgipendc (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len, +unsigned long val) +{ +u32 intid = addr &0x0f ; +int i; + +for (i =0 ; i < len; i++) { +struct vgic_irq *irq =vgic_get_irq (vcpu->kvm, vcpu, intid + i); + +spin_lock (&irq->irq_lock); + + irq->source &= ~((val >> (i *8 )) &0xff ); +if (!irq->source) + irq->pending = false; + +spin_unlock (&irq->irq_lock); + } +} + +static void vgic_mmio_write_sgipends (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len, +unsigned long val) +{ +u32 intid = addr &0x0f ; +int i; + +for (i =0 ; i < len; i++) { +struct vgic_irq *irq =vgic_get_irq (vcpu->kvm, vcpu, intid + i); + +spin_lock (&irq->irq_lock); + + irq->source |= (val >> (i *8 )) &0xff ; + +if (irq->source) { + irq->pending = true; +vgic_queue_irq_unlock (vcpu->kvm, irq); + }else { +spin_unlock (&irq->irq_lock); + } + } +} +static const struct vgic_register_region vgic_v2_dist_registers[] = { REGISTER_DESC_WITH_LENGTH(GIC_DIST_CTRL, vgic_mmio_read_v2_misc, vgic_mmio_write_v2_misc, 12,
@@ -184,10 +242,10 @@ static const struct vgic_register_region vgic_v2_dist_registers[] = { vgic_mmio_read_raz, vgic_mmio_write_sgir, 4, VGIC_ACCESS_32bit),REGISTER_DESC_WITH_LENGTH (GIC_DIST_SGI_PENDING_CLEAR , - vgic_mmio_read_raz, vgic_mmio_write_wi, 16, + vgic_mmio_read_sgipend, vgic_mmio_write_sgipendc, 16, VGIC_ACCESS_32bit | VGIC_ACCESS_8bit),REGISTER_DESC_WITH_LENGTH (GIC_DIST_SGI_PENDING_SET , - vgic_mmio_read_raz, vgic_mmio_write_wi, 16, + vgic_mmio_read_sgipend, vgic_mmio_write_sgipends, 16, VGIC_ACCESS_32bit | VGIC_ACCESS_8bit), };
Signature · 1 line
2.8.2
PATCH v4 33/56 KVM: arm/arm64: vgic-new: Add GICv3 MMIO handling framework
In reply to Andre PrzywaraCreate a new file called vgic-mmio-v3.c and describe the GICv3 distributor and redistributor registers there.
This adds a special macro to deal with the split of SGI/PPI in the redistributor and SPIs in the distributor, which allows us to reuse the existing GICv2 handlers for those registers which are compatible. Also we provide a function to deal with the registration of the two separate redistributor frames per VCPU.
---
Changelog RFC..v1:
- adapt to new MMIO registration approach:
register one device for the distributor and two for each VCPU
- implement special handling for private interrupts
- remove empty stub functions
- make IGROUPR return RAO
Changelog v1 .. v2:
- adapt to new framework, introduce vgic-mmio-v3.c
- remove userland register access functions (for now)
- precompute .len when describing a VGIC register
- add missed pointer incrementation on registering redist regions
- replace _nyi stub functions with raz/wi versions
Changelog v2 .. v3:
- replace inclusion of kvm/vgic/vgic.h with kvm/arm_vgic.h
- add prototype and stub code for vgic_register_redist_iodevs
- rename register struct variables _rdbase_ and _sgibase_
Changelog v3 .. v4:
- specify accessor width
virt/kvm/arm/vgic/vgic-mmio-v3.c | 224 +++++++++++++++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic-mmio.c | 5 + virt/kvm/arm/vgic/vgic-mmio.h | 2 + virt/kvm/arm/vgic/vgic.h | 7 ++ 4 files changed, 238 insertions(+) create mode 100644 virt/kvm/arm/vgic/vgic-mmio-v3.c
virt/kvm/arm/vgic/vgic-mmio-v3.c new file +224 -0
@@ -0,0 +1,224 @@ +/* +* VGICv3 MMIO handling functions +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +*/ + +#include <linux/irqchip/arm-gic-v3.h> +#include <linux/kvm.h> +#include <linux/kvm_host.h> +#include <kvm/iodev.h> +#include <kvm/arm_vgic.h> + +#include <asm/kvm_emulate.h> + +#include "vgic.h" +#include "vgic-mmio.h" + +/* +* The GICv3 per-IRQ registers are split to control PPIs and SGIs in the +* redistributors, while SPIs are covered by registers in the distributor +* block. Trying to set private IRQs in this block gets ignored. +* We take some special care here to fix the calculation of the register +* offset. +*/ +#define REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED (off, rd, wr, bpi, acc) \ + { \ + .reg_offset = off, \ + .bits_per_irq = bpi, \ + .len = (bpi * VGIC_NR_PRIVATE_IRQS) / 8, \ + .access_flags = acc, \ + .read = vgic_mmio_read_raz, \ + .write = vgic_mmio_write_wi, \ + }, { \ + .reg_offset = off + (bpi * VGIC_NR_PRIVATE_IRQS) / 8, \ + .bits_per_irq = bpi, \ + .len = (bpi * (1024 - VGIC_NR_PRIVATE_IRQS)) / 8, \ + .access_flags = acc, \ + .read = rd, \ + .write = wr, \ + } + +static const struct vgic_register_region vgic_v3_dist_registers[] = { +REGISTER_DESC_WITH_LENGTH (GICD_CTLR , + vgic_mmio_read_raz, vgic_mmio_write_wi,16 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED (GICD_IGROUPR , + vgic_mmio_read_rao, vgic_mmio_write_wi,1 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED (GICD_ISENABLER , + vgic_mmio_read_enable, vgic_mmio_write_senable,1 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED (GICD_ICENABLER , + vgic_mmio_read_enable, vgic_mmio_write_cenable,1 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED (GICD_ISPENDR , + vgic_mmio_read_pending, vgic_mmio_write_spending,1 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED (GICD_ICPENDR , + vgic_mmio_read_pending, vgic_mmio_write_cpending,1 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED (GICD_ISACTIVER , + vgic_mmio_read_active, vgic_mmio_write_sactive,1 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED (GICD_ICACTIVER , + vgic_mmio_read_active, vgic_mmio_write_cactive,1 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED (GICD_IPRIORITYR , + vgic_mmio_read_priority, vgic_mmio_write_priority,8 , + VGIC_ACCESS_32bit | VGIC_ACCESS_8bit), +REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED (GICD_ITARGETSR , + vgic_mmio_read_raz, vgic_mmio_write_wi,8 , + VGIC_ACCESS_32bit | VGIC_ACCESS_8bit), +REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED (GICD_ICFGR , + vgic_mmio_read_config, vgic_mmio_write_config,2 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED (GICD_IGRPMODR , + vgic_mmio_read_raz, vgic_mmio_write_wi,1 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED (GICD_IROUTER , + vgic_mmio_read_raz, vgic_mmio_write_wi,64 , + VGIC_ACCESS_64bit | VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_LENGTH (GICD_IDREGS , + vgic_mmio_read_raz, vgic_mmio_write_wi,48 , + VGIC_ACCESS_32bit), +}; + +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_ACCESS_32bit), +REGISTER_DESC_WITH_LENGTH (GICR_IIDR , + vgic_mmio_read_raz, vgic_mmio_write_wi,4 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_LENGTH (GICR_TYPER , + vgic_mmio_read_raz, vgic_mmio_write_wi,8 , + VGIC_ACCESS_64bit | VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_LENGTH (GICR_PROPBASER , + vgic_mmio_read_raz, vgic_mmio_write_wi,8 , + VGIC_ACCESS_64bit | VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_LENGTH (GICR_PENDBASER , + vgic_mmio_read_raz, vgic_mmio_write_wi,8 , + VGIC_ACCESS_64bit | VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_LENGTH (GICR_IDREGS , + vgic_mmio_read_raz, vgic_mmio_write_wi,48 , + VGIC_ACCESS_32bit), +}; + +static const struct vgic_register_region vgic_v3_sgibase_registers[] = { +REGISTER_DESC_WITH_LENGTH (GICR_IGROUPR0 , + vgic_mmio_read_rao, vgic_mmio_write_wi,4 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_LENGTH (GICR_ISENABLER0 , + vgic_mmio_read_enable, vgic_mmio_write_senable,4 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_LENGTH (GICR_ICENABLER0 , + vgic_mmio_read_enable, vgic_mmio_write_cenable,4 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_LENGTH (GICR_ISPENDR0 , + vgic_mmio_read_pending, vgic_mmio_write_spending,4 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_LENGTH (GICR_ICPENDR0 , + vgic_mmio_read_pending, vgic_mmio_write_cpending,4 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_LENGTH (GICR_ISACTIVER0 , + vgic_mmio_read_active, vgic_mmio_write_sactive,4 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_LENGTH (GICR_ICACTIVER0 , + vgic_mmio_read_active, vgic_mmio_write_cactive,4 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_LENGTH (GICR_IPRIORITYR0 , + vgic_mmio_read_priority, vgic_mmio_write_priority,32 , + VGIC_ACCESS_32bit | VGIC_ACCESS_8bit), +REGISTER_DESC_WITH_LENGTH (GICR_ICFGR0 , + vgic_mmio_read_config, vgic_mmio_write_config,8 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_LENGTH (GICR_IGRPMODR0 , + vgic_mmio_read_raz, vgic_mmio_write_wi,4 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_LENGTH (GICR_NSACR , + vgic_mmio_read_raz, vgic_mmio_write_wi,4 , + VGIC_ACCESS_32bit), +}; + +unsigned int vgic_v3_init_dist_iodev (struct vgic_io_device *dev) +{ + dev->regions = vgic_v3_dist_registers; + dev->nr_regions =ARRAY_SIZE (vgic_v3_dist_registers); + +kvm_iodevice_init (&dev->dev, &kvm_io_gic_ops); + +return SZ_64K ; +} + +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 ]; + +kvm_iodevice_init (&rd_dev->dev, &kvm_io_gic_ops); + rd_dev->base_addr = rd_base; + rd_dev->regions = vgic_v3_rdbase_registers; + rd_dev->nr_regions =ARRAY_SIZE (vgic_v3_rdbase_registers); + rd_dev->redist_vcpu = vcpu; + +mutex_lock (&kvm->slots_lock); + ret =kvm_io_bus_register_dev (kvm,KVM_MMIO_BUS , rd_base, +SZ_64K , &rd_dev->dev); +mutex_unlock (&kvm->slots_lock); + +if (ret) +break ; + +kvm_iodevice_init (&sgi_dev->dev, &kvm_io_gic_ops); + sgi_dev->base_addr = sgi_base; + sgi_dev->regions = vgic_v3_sgibase_registers; + sgi_dev->nr_regions =ARRAY_SIZE (vgic_v3_sgibase_registers); + sgi_dev->redist_vcpu = vcpu; + +mutex_lock (&kvm->slots_lock); + ret =kvm_io_bus_register_dev (kvm,KVM_MMIO_BUS , sgi_base, +SZ_64K , &sgi_dev->dev); +mutex_unlock (&kvm->slots_lock); +if (ret) { +kvm_io_bus_unregister_dev (kvm,KVM_MMIO_BUS , + &devices[c *2 ].dev); +break ; + } + } + +if (ret) { +/* The current c failed, so we start with the previous one. */ +for (c--; c >=0 ; c--) { +kvm_io_bus_unregister_dev (kvm,KVM_MMIO_BUS , + &devices[c *2 ].dev); +kvm_io_bus_unregister_dev (kvm,KVM_MMIO_BUS , + &devices[c *2 +1 ].dev); + } +kfree (devices); + }else { + kvm->arch.vgic.redist_iodevs = devices; + } + +return ret; +}
virt/kvm/arm/vgic/vgic-mmio.c +5 -0
@@ -478,6 +478,11 @@ int vgic_register_dist_iodev(struct kvm *kvm, gpa_t dist_base_address,case VGIC_V2 : len =vgic_v2_init_dist_iodev (io_device);break ; +#ifdef CONFIG_KVM_ARM_VGIC_V3 +case VGIC_V3 : + len =vgic_v3_init_dist_iodev (io_device); +break ; +#endif default :BUG_ON (1 ); }
virt/kvm/arm/vgic/vgic-mmio.h +2 -0
@@ -133,4 +133,6 @@ void vgic_mmio_write_config(struct kvm_vcpu *vcpu,unsigned int vgic_v2_init_dist_iodev (struct vgic_io_device *dev); +unsigned int vgic_v3_init_dist_iodev (struct vgic_io_device *dev); +#endif
virt/kvm/arm/vgic/vgic.h +7 -0
@@ -42,6 +42,7 @@ void vgic_v3_fold_lr_state(struct kvm_vcpu *vcpu);void vgic_v3_populate_lr (struct kvm_vcpu *vcpu,struct vgic_irq *irq,int lr);void vgic_v3_clear_lr (struct kvm_vcpu *vcpu,int lr);void vgic_v3_set_underflow (struct kvm_vcpu *vcpu); +int vgic_register_redist_iodevs (struct kvm *kvm,gpa_t dist_base_address);#else static inline void vgic_v3_process_maintenance (struct kvm_vcpu *vcpu) {
@@ -63,6 +64,12 @@ static inline void vgic_v3_clear_lr(struct kvm_vcpu *vcpu, int lr)static inline void vgic_v3_set_underflow (struct kvm_vcpu *vcpu) { } + +static inline int vgic_register_redist_iodevs (struct kvm *kvm, +gpa_t dist_base_address) +{ +return -ENODEV ; +}#endif #endif
Signature · 1 line
2.8.2
PATCH v4 34/56 KVM: arm/arm64: vgic-new: Add GICv3 CTLR, IIDR, TYPER handlers
In reply to Andre PrzywaraAs in the GICv2 emulation we handle those three registers in one function.
---
Changelog RFC..v1:
- kick VCPUs if distributor gets enabled
Changelog v1 .. v2:
- rewrite write handler to use switch statement
Changelog v3 .. v4:
- specify accessor width
virt/kvm/arm/vgic/vgic-mmio-v3.c | 48 +++++++++++++++++++++++++++++++++++++++- virt/kvm/arm/vgic/vgic.h | 1 + 2 files changed, 48 insertions(+), 1 deletion(-)
virt/kvm/arm/vgic/vgic-mmio-v3.c +47 -1
@@ -22,6 +22,52 @@#include "vgic.h" #include "vgic-mmio.h" +static unsigned long vgic_mmio_read_v3_misc (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len) +{ +u32 value =0 ; + +switch (addr &0x0c ) { +case GICD_CTLR : +if (vcpu->kvm->arch.vgic.enabled) + value |=GICD_CTLR_ENABLE_SS_G1 ; + value |=GICD_CTLR_ARE_NS |GICD_CTLR_DS ; +break ; +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 ; +break ; +case GICD_IIDR : + value = (PRODUCT_ID_KVM <<24 ) | (IMPLEMENTER_ARM <<0 ); +break ; +default : +return 0 ; + } + +return value; +} + +static void vgic_mmio_write_v3_misc (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len, +unsigned long val) +{ +struct vgic_dist *dist = &vcpu->kvm->arch.vgic; +bool was_enabled = dist->enabled; + +switch (addr &0x0c ) { +case GICD_CTLR : + dist->enabled = val &GICD_CTLR_ENABLE_SS_G1 ; + +if (!was_enabled && dist->enabled) +vgic_kick_vcpus (vcpu->kvm); +break ; +case GICD_TYPER : +case GICD_IIDR : +return ; + } +} +/* * The GICv3 per-IRQ registers are split to control PPIs and SGIs in the * redistributors, while SPIs are covered by registers in the distributor
@@ -48,7 +94,7 @@static const struct vgic_register_region vgic_v3_dist_registers[] = {REGISTER_DESC_WITH_LENGTH (GICD_CTLR , - vgic_mmio_read_raz, vgic_mmio_write_wi,16 , + vgic_mmio_read_v3_misc, vgic_mmio_write_v3_misc,16 , VGIC_ACCESS_32bit), REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED(GICD_IGROUPR, vgic_mmio_read_rao, vgic_mmio_write_wi, 1,
virt/kvm/arm/vgic/vgic.h +1 -0
@@ -19,6 +19,7 @@#define PRODUCT_ID_KVM 0x4b/* ASCII code K */ #define IMPLEMENTER_ARM 0x43b +#define INTERRUPT_ID_BITS_SPIS 10#define VGIC_PRI_BITS 5#define vgic_irq_is_sgi (intid) ((intid) < VGIC_NR_SGIS)
Signature · 1 line
2.8.2
PATCH v4 35/56 KVM: arm/arm64: vgic-new: Add GICv3 redistributor IIDR and TYPER handler
In reply to Andre PrzywaraThe redistributor TYPER tells the OS about the associated MPIDR, also the LAST bit is crucial to determine the number of redistributors.
---
Changelog v1 .. v2:
- adapt to new MMIO framework
Changelog v3 .. v4:
- specify accessor width
- add extract_bytes() as static (moved from an earlier patch)
- replace one extract_bytes() access with a simple return
virt/kvm/arm/vgic/vgic-mmio-v3.c | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-)
virt/kvm/arm/vgic/vgic-mmio-v3.c +30 -2
@@ -22,6 +22,13 @@#include "vgic.h" #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) +{ +return (data >> (offset *8 )) &GENMASK_ULL (num *8 -1 ,0 ); +} +static unsigned long vgic_mmio_read_v3_misc (struct kvm_vcpu *vcpu,gpa_t addr,unsigned int len) {
@@ -68,6 +75,27 @@ static void vgic_mmio_write_v3_misc(struct kvm_vcpu *vcpu, } } +static unsigned long vgic_mmio_read_v3r_typer (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len) +{ +unsigned long mpidr =kvm_vcpu_get_mpidr_aff (vcpu); +int target_vcpu_id = vcpu->vcpu_id; +u64 value; + + value = (mpidr &GENMASK (23 ,0 )) <<32 ; + value |= ((target_vcpu_id &0xffff ) <<8 ); +if (target_vcpu_id ==atomic_read (&vcpu->kvm->online_vcpus) -1 ) + value |=GICR_TYPER_LAST ; + +return extract_bytes (value, addr &7 , len); +} + +static unsigned long vgic_mmio_read_v3r_iidr (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len) +{ +return (PRODUCT_ID_KVM <<24 ) | (IMPLEMENTER_ARM <<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
@@ -142,10 +170,10 @@ static const struct vgic_register_region vgic_v3_rdbase_registers[] = { vgic_mmio_read_raz, vgic_mmio_write_wi, 4, VGIC_ACCESS_32bit),REGISTER_DESC_WITH_LENGTH (GICR_IIDR , - vgic_mmio_read_raz, vgic_mmio_write_wi,4 , + vgic_mmio_read_v3r_iidr, vgic_mmio_write_wi,4 , VGIC_ACCESS_32bit),REGISTER_DESC_WITH_LENGTH (GICR_TYPER , - vgic_mmio_read_raz, vgic_mmio_write_wi,8 , + vgic_mmio_read_v3r_typer, vgic_mmio_write_wi,8 , VGIC_ACCESS_64bit | VGIC_ACCESS_32bit), REGISTER_DESC_WITH_LENGTH(GICR_PROPBASER, vgic_mmio_read_raz, vgic_mmio_write_wi, 8,
Signature · 1 line
2.8.2
PATCH v4 36/56 KVM: arm/arm64: vgic-new: Add GICv3 IDREGS register handler
In reply to Andre PrzywaraWe implement the only one ID register that is required by the architecture, also this is the one that Linux actually checks.
---
Changelog v1 .. v2:
- adapt to new MMIO framework
Changelog v3 .. v4:
- specify accessor width
virt/kvm/arm/vgic/vgic-mmio-v3.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-)
virt/kvm/arm/vgic/vgic-mmio-v3.c +14 -2
@@ -96,6 +96,18 @@ static unsigned long vgic_mmio_read_v3r_iidr(struct kvm_vcpu *vcpu,return (PRODUCT_ID_KVM <<24 ) | (IMPLEMENTER_ARM <<0 ); } +static unsigned long vgic_mmio_read_v3_idregs (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len) +{ +switch (addr &0xffff ) { +case GICD_PIDR2 : +/* report a GICv3 compliant implementation */ +return 0x3b ; + } + +return 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
@@ -161,7 +173,7 @@ static const struct vgic_register_region vgic_v3_dist_registers[] = { vgic_mmio_read_raz, vgic_mmio_write_wi, 64, VGIC_ACCESS_64bit | VGIC_ACCESS_32bit), REGISTER_DESC_WITH_LENGTH(GICD_IDREGS, - vgic_mmio_read_raz, vgic_mmio_write_wi, 48, + vgic_mmio_read_v3_idregs, vgic_mmio_write_wi, 48, VGIC_ACCESS_32bit), };
@@ -182,7 +194,7 @@ static const struct vgic_register_region vgic_v3_rdbase_registers[] = { vgic_mmio_read_raz, vgic_mmio_write_wi, 8, VGIC_ACCESS_64bit | VGIC_ACCESS_32bit), REGISTER_DESC_WITH_LENGTH(GICR_IDREGS, - vgic_mmio_read_raz, vgic_mmio_write_wi, 48, + vgic_mmio_read_v3_idregs, vgic_mmio_write_wi, 48, VGIC_ACCESS_32bit), };
Signature · 1 line
2.8.2
PATCH v4 37/56 KVM: arm/arm64: vgic-new: Add GICv3 IROUTER register handlers
In reply to Andre PrzywaraSince GICv3 supports much more than the 8 CPUs the GICv2 ITARGETSR register can handle, the new IROUTER register covers the whole range of possible target (V)CPUs by using the same MPIDR that the cores report themselves.
In addition to translating this MPIDR into a vcpu pointer we store the originally written value as well. The architecture allows to write any values into the register, which must be read back as written.
Since we don't support affinity level 3, we don't need to take care about the upper word of this 64-bit register, which simplifies the handling a bit.
---
Changelog RFC..v1:
- fold in and simplify vgic_v3_irq_change_affinity
Changelog v1 .. v2:
- adapt to new MMIO framework
Changelog v3 .. v4:
- move accessor width check into dispatcher
- use IRQ number accessor macro
virt/kvm/arm/vgic/vgic-mmio-v3.c | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-)
virt/kvm/arm/vgic/vgic-mmio-v3.c +36 -1
@@ -75,6 +75,41 @@ static void vgic_mmio_write_v3_misc(struct kvm_vcpu *vcpu, } } +static unsigned long vgic_mmio_read_irouter (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len) +{ +int intid =VGIC_ADDR_TO_INTID (addr,64 ); +struct vgic_irq *irq =vgic_get_irq (vcpu->kvm,NULL , intid); + +if (!irq) +return 0 ; + +return extract_bytes (READ_ONCE (irq->mpidr), addr &7 , len); +} + +static void vgic_mmio_write_irouter (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len, +unsigned long val) +{ +int intid =VGIC_ADDR_TO_INTID (addr,64 ); +struct vgic_irq *irq =vgic_get_irq (vcpu->kvm,NULL , intid); + +if (!irq) +return ; + +/* The upper word is WI for us since we don't implement Aff3. */ +if (addr &4 ) +return ; + +spin_lock (&irq->irq_lock); + +/* We only care about and preserve Aff0, Aff1 and Aff2. */ + irq->mpidr = val &GENMASK (23 ,0 ); + irq->target_vcpu =kvm_mpidr_to_vcpu (vcpu->kvm, irq->mpidr); + +spin_unlock (&irq->irq_lock); +} +static unsigned long vgic_mmio_read_v3r_typer (struct kvm_vcpu *vcpu,gpa_t addr,unsigned int len) {
@@ -170,7 +205,7 @@ static const struct vgic_register_region vgic_v3_dist_registers[] = { vgic_mmio_read_raz, vgic_mmio_write_wi, 1, VGIC_ACCESS_32bit),REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED (GICD_IROUTER , - vgic_mmio_read_raz, vgic_mmio_write_wi,64 , + vgic_mmio_read_irouter, vgic_mmio_write_irouter,64 , VGIC_ACCESS_64bit | VGIC_ACCESS_32bit), REGISTER_DESC_WITH_LENGTH(GICD_IDREGS, vgic_mmio_read_v3_idregs, vgic_mmio_write_wi, 48,
Signature · 1 line
2.8.2
PATCH v4 38/56 KVM: arm/arm64: vgic-new: Add GICv3 SGI system register trap handler
In reply to Andre PrzywaraIn contrast to GICv2 SGIs in a GICv3 implementation are not triggered by a MMIO write, but with a system register write. KVM knows about that register already, we just need to implement the handler and wire it up to the core KVM/ARM code.
---
Changelog RFC..v1:
- add comment about SGI_AFFINITY_LEVEL macro
include/kvm/vgic/vgic.h | 8 +++ virt/kvm/arm/vgic/vgic-mmio-v3.c | 106 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+)
include/kvm/vgic/vgic.h +8 -0
@@ -209,6 +209,14 @@ bool kvm_vcpu_has_pending_irqs(struct kvm_vcpu *vcpu);void kvm_vgic_sync_hwstate (struct kvm_vcpu *vcpu);void kvm_vgic_flush_hwstate (struct kvm_vcpu *vcpu); +#ifdef CONFIG_KVM_ARM_VGIC_V3 +void vgic_v3_dispatch_sgi (struct kvm_vcpu *vcpu,u64 reg); +#else +static inline void vgic_v3_dispatch_sgi (struct kvm_vcpu *vcpu,u64 reg) +{ +} +#endif +/** * kvm_vgic_get_max_vcpus - Get the maximum number of VCPUs allowed by HW *
virt/kvm/arm/vgic/vgic-mmio-v3.c +106 -0
109 lines not on this page · raw message
Signature · 1 line
2.8.2
PATCH v4 39/56 KVM: arm/arm64: vgic-new: vgic_kvm_device: KVM device ops registration
In reply to Andre PrzywaraFrom: Eric Auger <[email protected]>
This patch introduces the skeleton for the KVM device operations associated to KVM_DEV_TYPE_ARM_VGIC_V2 and KVM_DEV_TYPE_ARM_VGIC_V3.
At that stage kvm_vgic_create is stubbed.
---
Changelog v1 .. v2:
- rename vgic_kvm_device.c to vgic-kvm-device.c
Changelog v2 .. v3:
- replace inclusion of vgic/vgic.h with arm_vgic.h
- add kvm_register_vgic_device() prototype to this patch
virt/kvm/arm/vgic/vgic-kvm-device.c | 108 ++++++++++++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic.h | 2 + 2 files changed, 110 insertions(+) create mode 100644 virt/kvm/arm/vgic/vgic-kvm-device.c
virt/kvm/arm/vgic/vgic-kvm-device.c new file +108 -0
@@ -0,0 +1,108 @@ +/* +* VGIC: KVM DEVICE API +* +* Copyright (C) 2015 ARM Ltd. +* Author: Marc Zyngier <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +*/ +#include <linux/kvm_host.h> +#include <kvm/arm_vgic.h> + +/* common helpers */ + +static int vgic_create (struct kvm_device *dev,u32 type) +{ +return kvm_vgic_create (dev->kvm, type); +} + +static void vgic_destroy (struct kvm_device *dev) +{ +kfree (dev); +} + +void kvm_register_vgic_device (unsigned long type) +{ +switch (type) { +case KVM_DEV_TYPE_ARM_VGIC_V2 : +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 ); +break ; +#endif + } +} + +/* V2 ops */ + +static int vgic_v2_set_attr (struct kvm_device *dev, +struct kvm_device_attr *attr) +{ +return -ENXIO ; +} + +static int vgic_v2_get_attr (struct kvm_device *dev, +struct kvm_device_attr *attr) +{ +return -ENXIO ; +} + +static int vgic_v2_has_attr (struct kvm_device *dev, +struct kvm_device_attr *attr) +{ +return -ENXIO ; +} + +struct kvm_device_ops kvm_arm_vgic_v2_ops = { + .name ="kvm-arm-vgic-v2" , + .create = vgic_create, + .destroy = vgic_destroy, + .set_attr = vgic_v2_set_attr, + .get_attr = vgic_v2_get_attr, + .has_attr = vgic_v2_has_attr, +}; + +/* V3 ops */ + +#ifdef CONFIG_KVM_ARM_VGIC_V3 + +static int vgic_v3_set_attr (struct kvm_device *dev, +struct kvm_device_attr *attr) +{ +return -ENXIO ; +} + +static int vgic_v3_get_attr (struct kvm_device *dev, +struct kvm_device_attr *attr) +{ +return -ENXIO ; +} + +static int vgic_v3_has_attr (struct kvm_device *dev, +struct kvm_device_attr *attr) +{ +return -ENXIO ; +} + +struct kvm_device_ops kvm_arm_vgic_v3_ops = { + .name ="kvm-arm-vgic-v3" , + .create = vgic_create, + .destroy = vgic_destroy, + .set_attr = vgic_v3_set_attr, + .get_attr = vgic_v3_get_attr, + .has_attr = vgic_v3_has_attr, +}; + +#endif /* CONFIG_KVM_ARM_VGIC_V3 */ +
virt/kvm/arm/vgic/vgic.h +2 -0
@@ -73,4 +73,6 @@ static inline int vgic_register_redist_iodevs(struct kvm *kvm, }#endif +void kvm_register_vgic_device (unsigned long type); +#endif
Signature · 1 line
2.8.2
PATCH v4 40/56 KVM: arm/arm64: vgic-new: vgic_kvm_device: KVM_DEV_ARM_VGIC_GRP_NR_IRQS
In reply to Andre PrzywaraFrom: Eric Auger <[email protected]>
This patch implements the KVM_DEV_ARM_VGIC_GRP_NR_IRQS group. This modality is supported by both VGIC V2 and V3 KVM device as will be other groups, hence the introduction of common helpers.
--- virt/kvm/arm/vgic/vgic-kvm-device.c | 83 +++++++++++++++++++++++++++++++++++-- 1 file changed, 79 insertions(+), 4 deletions(-)
virt/kvm/arm/vgic/vgic-kvm-device.c +79 -4
@@ -15,9 +15,69 @@*/ #include <linux/kvm_host.h> #include <kvm/arm_vgic.h> +#include <linux/uaccess.h> +#include "vgic.h" /* common helpers */ +static int vgic_set_common_attr (struct kvm_device *dev, +struct kvm_device_attr *attr) +{ +switch (attr->group) { +case KVM_DEV_ARM_VGIC_GRP_NR_IRQS : { +u32 __user *uaddr = (u32 __user *)(long )attr->addr; +u32 val; +int ret =0 ; + +if (get_user (val, uaddr)) +return -EFAULT ; + +/* +* We require: +* - at least 32 SPIs on top of the 16 SGIs and 16 PPIs +* - at most 1024 interrupts +* - a multiple of 32 interrupts +*/ +if (val < (VGIC_NR_PRIVATE_IRQS +32 ) || + val >VGIC_MAX_RESERVED || + (val &31 )) +return -EINVAL ; + +mutex_lock (&dev->kvm->lock); + +if (vgic_ready (dev->kvm) || dev->kvm->arch.vgic.nr_spis) + ret = -EBUSY ; +else + dev->kvm->arch.vgic.nr_spis = + val -VGIC_NR_PRIVATE_IRQS ; + +mutex_unlock (&dev->kvm->lock); + +return ret; + } + } + +return -ENXIO ; +} + +static int vgic_get_common_attr (struct kvm_device *dev, +struct kvm_device_attr *attr) +{ +int r = -ENXIO ; + +switch (attr->group) { +case KVM_DEV_ARM_VGIC_GRP_NR_IRQS : { +u32 __user *uaddr = (u32 __user *)(long )attr->addr; + + r =put_user (dev->kvm->arch.vgic.nr_spis + +VGIC_NR_PRIVATE_IRQS , uaddr); +break ; + } + } + +return r; +} +static int vgic_create (struct kvm_device *dev,u32 type) {return kvm_vgic_create (dev->kvm, type);
@@ -49,18 +109,29 @@ void kvm_register_vgic_device(unsigned long type)static int vgic_v2_set_attr (struct kvm_device *dev,struct kvm_device_attr *attr) { -return -ENXIO ; +int ret; + + ret =vgic_set_common_attr (dev, attr); +return ret; + }static int vgic_v2_get_attr (struct kvm_device *dev,struct kvm_device_attr *attr) { -return -ENXIO ; +int ret; + + ret =vgic_get_common_attr (dev, attr); +return ret; }static int vgic_v2_has_attr (struct kvm_device *dev,struct kvm_device_attr *attr) { +switch (attr->group) { +case KVM_DEV_ARM_VGIC_GRP_NR_IRQS : +return 0 ; + }return -ENXIO ; }
@@ -80,18 +151,22 @@ struct kvm_device_ops kvm_arm_vgic_v2_ops = {static int vgic_v3_set_attr (struct kvm_device *dev,struct kvm_device_attr *attr) { -return -ENXIO ; +return vgic_set_common_attr (dev, attr); }static int vgic_v3_get_attr (struct kvm_device *dev,struct kvm_device_attr *attr) { -return -ENXIO ; +return vgic_get_common_attr (dev, attr); }static int vgic_v3_has_attr (struct kvm_device *dev,struct kvm_device_attr *attr) { +switch (attr->group) { +case KVM_DEV_ARM_VGIC_GRP_NR_IRQS : +return 0 ; + }return -ENXIO ; }
Signature · 1 line
2.8.2
PATCH v4 41/56 KVM: arm/arm64: vgic-new: vgic_kvm_device: KVM_DEV_ARM_VGIC_GRP_CTRL
In reply to Andre PrzywaraFrom: Eric Auger <[email protected]>
This patch implements the KVM_DEV_ARM_VGIC_GRP_CTRL group API featuring KVM_DEV_ARM_VGIC_CTRL_INIT attribute. The vgic_init function is not yet implemented though.
--- virt/kvm/arm/vgic/vgic-kvm-device.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+)
virt/kvm/arm/vgic/vgic-kvm-device.c +22 -0
@@ -23,6 +23,8 @@static int vgic_set_common_attr (struct kvm_device *dev,struct kvm_device_attr *attr) { +int r; +switch (attr->group) {case KVM_DEV_ARM_VGIC_GRP_NR_IRQS : {u32 __user *uaddr = (u32 __user *)(long )attr->addr;
@@ -55,6 +57,16 @@ static int vgic_set_common_attr(struct kvm_device *dev,return ret; } +case KVM_DEV_ARM_VGIC_GRP_CTRL : { +switch (attr->attr) { +case KVM_DEV_ARM_VGIC_CTRL_INIT : +mutex_lock (&dev->kvm->lock); + r =vgic_init (dev->kvm); +mutex_unlock (&dev->kvm->lock); +return r; + } +break ; + } }return -ENXIO ;
@@ -131,6 +143,11 @@ static int vgic_v2_has_attr(struct kvm_device *dev,switch (attr->group) {case KVM_DEV_ARM_VGIC_GRP_NR_IRQS :return 0 ; +case KVM_DEV_ARM_VGIC_GRP_CTRL : +switch (attr->attr) { +case KVM_DEV_ARM_VGIC_CTRL_INIT : +return 0 ; + } }return -ENXIO ; }
@@ -166,6 +183,11 @@ static int vgic_v3_has_attr(struct kvm_device *dev,switch (attr->group) {case KVM_DEV_ARM_VGIC_GRP_NR_IRQS :return 0 ; +case KVM_DEV_ARM_VGIC_GRP_CTRL : +switch (attr->attr) { +case KVM_DEV_ARM_VGIC_CTRL_INIT : +return 0 ; + } }return -ENXIO ; }
Signature · 1 line
2.8.2
PATCH v4 42/56 KVM: arm/arm64: vgic-new: vgic_kvm_device: implement kvm_vgic_addr
In reply to Andre PrzywaraFrom: Eric Auger <[email protected]>
kvm_vgic_addr is used by the userspace to set the base address of the following register regions, as seen by the guest:
- distributor(v2 and v3),
- re-distributors (v3),
- CPU interface (v2).
---
Changelog v3 .. v4:
- move overlap check to init time (cannot work with GICv3 here)
- refactor remaining address sanity checks
include/kvm/vgic/vgic.h | 2 + virt/kvm/arm/vgic/vgic-kvm-device.c | 86 +++++++++++++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic.h | 3 ++ 3 files changed, 91 insertions(+)
include/kvm/vgic/vgic.h +2 -0
@@ -194,6 +194,8 @@ struct vgic_cpu {u64 live_lrs; }; +int kvm_vgic_addr (struct kvm *kvm,unsigned long type,u64 *addr,bool write); +int kvm_vgic_inject_irq (struct kvm *kvm,int cpuid,unsigned int intid,bool level);
virt/kvm/arm/vgic/vgic-kvm-device.c +86 -0
96 lines not on this page · raw message
virt/kvm/arm/vgic/vgic.h +3 -0
@@ -19,6 +19,9 @@#define PRODUCT_ID_KVM 0x4b/* ASCII code K */ #define IMPLEMENTER_ARM 0x43b +#define VGIC_ADDR_UNDEF (-1) +#define IS_VGIC_ADDR_UNDEF (_x) ((_x) == VGIC_ADDR_UNDEF) +#define INTERRUPT_ID_BITS_SPIS 10#define VGIC_PRI_BITS 5
Signature · 1 line
2.8.2
PATCH v4 43/56 KVM: arm/arm64: vgic-new: vgic_kvm_device: KVM_DEV_ARM_VGIC_GRP_ADDR
In reply to Andre PrzywaraFrom: Eric Auger <[email protected]>
This patch implements the KVM_DEV_ARM_VGIC_GRP_ADDR group which enables to set the base address of GIC regions as seen by the guest.
--- virt/kvm/arm/vgic/vgic-kvm-device.c | 38 +++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+)
virt/kvm/arm/vgic/vgic-kvm-device.c +38 -0
@@ -112,6 +112,17 @@ static int vgic_set_common_attr(struct kvm_device *dev,int r;switch (attr->group) { +case KVM_DEV_ARM_VGIC_GRP_ADDR : { +u64 __user *uaddr = (u64 __user *)(long )attr->addr; +u64 addr; +unsigned long type = (unsigned long )attr->attr; + +if (copy_from_user (&addr, uaddr,sizeof (addr))) +return -EFAULT ; + + r =kvm_vgic_addr (dev->kvm, type, &addr, true); +return (r == -ENODEV ) ? -ENXIO : r; + }case KVM_DEV_ARM_VGIC_GRP_NR_IRQS : {u32 __user *uaddr = (u32 __user *)(long )attr->addr;u32 val;
@@ -164,6 +175,19 @@ static int vgic_get_common_attr(struct kvm_device *dev,int r = -ENXIO ;switch (attr->group) { +case KVM_DEV_ARM_VGIC_GRP_ADDR : { +u64 __user *uaddr = (u64 __user *)(long )attr->addr; +u64 addr; +unsigned long type = (unsigned long )attr->attr; + + r =kvm_vgic_addr (dev->kvm, type, &addr, false); +if (r) +return (r == -ENODEV ) ? -ENXIO : r; + +if (copy_to_user (uaddr, &addr,sizeof (addr))) +return -EFAULT ; +break ; + }case KVM_DEV_ARM_VGIC_GRP_NR_IRQS : {u32 __user *uaddr = (u32 __user *)(long )attr->addr;
@@ -227,6 +251,13 @@ static int vgic_v2_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_V2_ADDR_TYPE_DIST : +case KVM_VGIC_V2_ADDR_TYPE_CPU : +return 0 ; + } +break ;case KVM_DEV_ARM_VGIC_GRP_NR_IRQS :return 0 ;case KVM_DEV_ARM_VGIC_GRP_CTRL :
@@ -267,6 +298,13 @@ static int vgic_v3_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_V3_ADDR_TYPE_DIST : +case KVM_VGIC_V3_ADDR_TYPE_REDIST : +return 0 ; + } +break ;case KVM_DEV_ARM_VGIC_GRP_NR_IRQS :return 0 ;case KVM_DEV_ARM_VGIC_GRP_CTRL :
Signature · 1 line
2.8.2
PATCH v4 44/56 KVM: arm/arm64: vgic-new: vgic_kvm_device: access to VGIC registers
In reply to Andre PrzywaraFrom: Eric Auger <[email protected]>
This patch implements the switches for KVM_DEV_ARM_VGIC_GRP_DIST_REGS and KVM_DEV_ARM_VGIC_GRP_CPU_REGS API which allows the userspace to access VGIC registers.
---
Changelog v3 .. v4:
- check for proper alignment on userland GIC accesses
virt/kvm/arm/vgic/vgic-kvm-device.c | 53 +++++++++++++++++++++++++++++++++++-- virt/kvm/arm/vgic/vgic-mmio-v2.c | 38 ++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic.h | 1 + 3 files changed, 90 insertions(+), 2 deletions(-)
virt/kvm/arm/vgic/vgic-kvm-device.c +51 -2
@@ -226,6 +226,21 @@ void kvm_register_vgic_device(unsigned long type) } } +/** vgic_attr_regs_access: allows user space to read/write VGIC registers +* +* @dev: kvm device handle +* @attr: kvm device attribute +* @reg: address the value is read or written +* @is_write: write flag +* +*/ +static int vgic_attr_regs_access (struct kvm_device *dev, +struct kvm_device_attr *attr, +u32 *reg,bool is_write) +{ +return -ENXIO ; +} +/* V2 ops */ static int vgic_v2_set_attr(struct kvm_device *dev,
@@ -234,8 +249,23 @@ static int vgic_v2_set_attr(struct kvm_device *dev,int ret; ret =vgic_set_common_attr (dev, attr); -return ret; +if (ret != -ENXIO ) +return ret; + +switch (attr->group) { +case KVM_DEV_ARM_VGIC_GRP_DIST_REGS : +case KVM_DEV_ARM_VGIC_GRP_CPU_REGS : { +u32 __user *uaddr = (u32 __user *)(long )attr->addr; +u32 reg; + +if (get_user (reg, uaddr)) +return -EFAULT ; +return vgic_attr_regs_access (dev, attr, ®, true); + } + } + +return -ENXIO ; } static int vgic_v2_get_attr(struct kvm_device *dev,
@@ -244,7 +274,23 @@ static int vgic_v2_get_attr(struct kvm_device *dev,int ret; ret =vgic_get_common_attr (dev, attr); -return ret; +if (ret != -ENXIO ) +return ret; + +switch (attr->group) { +case KVM_DEV_ARM_VGIC_GRP_DIST_REGS : +case KVM_DEV_ARM_VGIC_GRP_CPU_REGS : { +u32 __user *uaddr = (u32 __user *)(long )attr->addr; +u32 reg =0 ; + + ret =vgic_attr_regs_access (dev, attr, ®, false); +if (ret) +return ret; +return put_user (reg, uaddr); + } + } + +return -ENXIO ; } static int vgic_v2_has_attr(struct kvm_device *dev,
@@ -258,6 +304,9 @@ static int vgic_v2_has_attr(struct kvm_device *dev,return 0 ; }break ; +case KVM_DEV_ARM_VGIC_GRP_DIST_REGS : +case KVM_DEV_ARM_VGIC_GRP_CPU_REGS : +return vgic_v2_has_attr_regs (dev, attr);case KVM_DEV_ARM_VGIC_GRP_NR_IRQS :return 0 ;case KVM_DEV_ARM_VGIC_GRP_CTRL :
virt/kvm/arm/vgic/vgic-mmio-v2.c +38 -0
@@ -258,3 +258,41 @@ unsigned int vgic_v2_init_dist_iodev(struct vgic_io_device *dev)return SZ_4K ; } + +int vgic_v2_has_attr_regs (struct kvm_device *dev,struct kvm_device_attr *attr) +{ +int nr_irqs = dev->kvm->arch.vgic.nr_spis +VGIC_NR_PRIVATE_IRQS ; +const struct vgic_register_region *regions; +gpa_t addr; +int nr_regions, i, len; + + addr = attr->attr &KVM_DEV_ARM_VGIC_OFFSET_MASK ; + +switch (attr->group) { +case KVM_DEV_ARM_VGIC_GRP_DIST_REGS : + regions = vgic_v2_dist_registers; + nr_regions =ARRAY_SIZE (vgic_v2_dist_registers); +break ; +case KVM_DEV_ARM_VGIC_GRP_CPU_REGS : +return -ENXIO ;/* TODO: describe CPU i/f regs also */ +default : +return -ENXIO ; + } + +/* We only support aligned 32-bit accesses. */ +if (addr &3 ) +return -ENXIO ; + +for (i =0 ; i < nr_regions; i++) { +if (regions[i].bits_per_irq) + len = (regions[i].bits_per_irq * nr_irqs) /8 ; +else + len = regions[i].len; + +if (regions[i].reg_offset <= addr && + regions[i].reg_offset + len > addr) +return 0 ; + } + +return -ENXIO ; +}
virt/kvm/arm/vgic/vgic.h +1 -0
@@ -37,6 +37,7 @@ 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);void vgic_v2_clear_lr (struct kvm_vcpu *vcpu,int lr);void vgic_v2_set_underflow (struct kvm_vcpu *vcpu); +int vgic_v2_has_attr_regs (struct kvm_device *dev,struct kvm_device_attr *attr);int vgic_register_dist_iodev (struct kvm *kvm,gpa_t dist_base_address,enum vgic_type );
Signature · 1 line
2.8.2
PATCH v4 45/56 KVM: arm/arm64: vgic-new: Export register access interface
In reply to Andre PrzywaraFrom: Christoffer Dall <[email protected]>
Userland can access the emulated GIC to save and restore its state for initialization or migration purposes.
The kvm_io_bus API requires an absolute gpa, which does not fit the KVM_DEV_ARM_VGIC_GRP_DIST_REGS user API, that only provides relative offsets. So we provide a wrapper to plug into our MMIO framework and find the respective register handler.
---
Changelog v1 .. v2:
- adapt to new MMIO framework
- handle endianess explicitly
Changelog v2 .. v3:
- wrap accessor function for easier reusage later
- moved from earlier in the series
virt/kvm/arm/vgic/vgic-mmio-v2.c | 36 ++++++++++++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic.h | 2 ++ 2 files changed, 38 insertions(+)
virt/kvm/arm/vgic/vgic-mmio-v2.c +36 -0
@@ -296,3 +296,39 @@ int vgic_v2_has_attr_regs(struct kvm_device *dev, struct kvm_device_attr *attr)return -ENXIO ; } + +/* +* When userland tries to access the VGIC register handlers, we need to +* create a usable struct vgic_io_device to be passed to the handlers and we +* have to set up a buffer similar to what would have happened if a guest MMIO +* access occurred, including doing endian conversions on BE systems. +*/ +static int vgic_uaccess (struct kvm_vcpu *vcpu,struct vgic_io_device *dev, +bool is_write,int offset,u32 *val) +{ +unsigned int len =4 ; +u8 buf[4 ]; +int ret; + +if (is_write) { +vgic_data_host_to_mmio_bus (buf, len, *val); + ret = kvm_io_gic_ops.write (vcpu, &dev->dev, offset, len, buf); + }else { + ret = kvm_io_gic_ops.read (vcpu, &dev->dev, offset, len, buf); +if (!ret) + *val =vgic_data_mmio_bus_to_host (buf, len); + } + +return ret; +} + +int vgic_v2_dist_uaccess (struct kvm_vcpu *vcpu,bool is_write, +int offset,u32 *val) +{ +struct vgic_io_device dev = { + .regions = vgic_v2_dist_registers, + .nr_regions =ARRAY_SIZE (vgic_v2_dist_registers), + }; + +return vgic_uaccess (vcpu, &dev, is_write, offset, val); +}
virt/kvm/arm/vgic/vgic.h +2 -0
@@ -38,6 +38,8 @@ void vgic_v2_populate_lr(struct kvm_vcpu *vcpu, struct vgic_irq *irq, int lr);void vgic_v2_clear_lr (struct kvm_vcpu *vcpu,int lr);void vgic_v2_set_underflow (struct kvm_vcpu *vcpu);int vgic_v2_has_attr_regs (struct kvm_device *dev,struct kvm_device_attr *attr); +int vgic_v2_dist_uaccess (struct kvm_vcpu *vcpu,bool is_write, +int offset,u32 *val);int vgic_register_dist_iodev (struct kvm *kvm,gpa_t dist_base_address,enum vgic_type );
Signature · 1 line
2.8.2
PATCH v4 46/56 KVM: arm/arm64: vgic-new: Add userland access to VGIC dist registers
In reply to Andre PrzywaraUserland may want to save and restore the state of the in-kernel VGIC, so we provide the code which takes a userland request and translate that into calls to our MMIO framework.
--- virt/kvm/arm/vgic/vgic-kvm-device.c | 50 ++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-)
virt/kvm/arm/vgic/vgic-kvm-device.c +49 -1
@@ -238,7 +238,55 @@ static int vgic_attr_regs_access(struct kvm_device *dev,struct kvm_device_attr *attr, u32 *reg, bool is_write) { - return -ENXIO; + gpa_t addr; +int cpuid, ret, c; +struct kvm_vcpu *vcpu, *tmp_vcpu; + + cpuid = (attr->attr &KVM_DEV_ARM_VGIC_CPUID_MASK ) >> +KVM_DEV_ARM_VGIC_CPUID_SHIFT ; + vcpu =kvm_get_vcpu (dev->kvm, cpuid); + addr = attr->attr &KVM_DEV_ARM_VGIC_OFFSET_MASK ; + +mutex_lock (&dev->kvm->lock); + + ret =vgic_init (dev->kvm); +if (ret) + goto out; + +if (cpuid >=atomic_read (&dev->kvm->online_vcpus)) { + ret = -EINVAL ; + goto out; + } + +/* +* Ensure that no other VCPU is running by checking the vcpu->cpu +* field. If no other VPCUs are running we can safely access the VGIC +* state, because even if another VPU is run after this point, that +* VCPU will not touch the vgic state, because it will block on +* getting the vgic->lock in kvm_vgic_sync_hwstate(). +*/ +kvm_for_each_vcpu (c, tmp_vcpu, dev->kvm) { +if (unlikely (tmp_vcpu->cpu !=-1 )) { + ret = -EBUSY ; + goto out; + } + } + +switch (attr->group) { +case KVM_DEV_ARM_VGIC_GRP_CPU_REGS : + ret = -EINVAL ; +break ; +case KVM_DEV_ARM_VGIC_GRP_DIST_REGS : + ret =vgic_v2_dist_uaccess (vcpu, is_write, addr, reg); +break ; +default : + ret = -EINVAL ; +break ; + } + +out: +mutex_unlock (&dev->kvm->lock); +return ret; }/* V2 ops */
Signature · 1 line
2.8.2
PATCH v4 47/56 KVM: arm/arm64: vgic-new: Add GICH_VMCR accessors
In reply to Andre PrzywaraSince the GIC CPU interface is always virtualized by the hardware, we don't have CPU interface state information readily available in our emulation if userland wants to save or restore it. Fortunately the GIC hypervisor interface provides the VMCR register to access the required virtual CPU interface bits.
Provide wrappers for GICv2 and GICv3 hosts to have access to this register.
---
Changelog v2 .. v3:
- remove exported v2/v3 selector functions (as per Marc's patch)
Changelog v3 .. v4:
- move struct vgic_vmcr into the VGIC's private header file
virt/kvm/arm/vgic/vgic-v2.c | 29 +++++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic-v3.c | 22 ++++++++++++++++++++++ virt/kvm/arm/vgic/vgic.h | 21 +++++++++++++++++++++ 3 files changed, 72 insertions(+)
virt/kvm/arm/vgic/vgic-v2.c +29 -0
@@ -174,3 +174,32 @@ void vgic_v2_clear_lr(struct kvm_vcpu *vcpu, int lr) { vcpu->arch.vgic_cpu.vgic_v2.vgic_lr[lr] =0 ; } + +void vgic_v2_set_vmcr (struct kvm_vcpu *vcpu,struct vgic_vmcr *vmcrp) +{ +u32 vmcr; + + vmcr = (vmcrp->ctlr <<GICH_VMCR_CTRL_SHIFT ) &GICH_VMCR_CTRL_MASK ; + vmcr |= (vmcrp->abpr <<GICH_VMCR_ALIAS_BINPOINT_SHIFT ) & +GICH_VMCR_ALIAS_BINPOINT_MASK ; + vmcr |= (vmcrp->bpr <<GICH_VMCR_BINPOINT_SHIFT ) & +GICH_VMCR_BINPOINT_MASK ; + vmcr |= (vmcrp->pmr <<GICH_VMCR_PRIMASK_SHIFT ) & +GICH_VMCR_PRIMASK_MASK ; + + vcpu->arch.vgic_cpu.vgic_v2.vgic_vmcr = vmcr; +} + +void vgic_v2_get_vmcr (struct kvm_vcpu *vcpu,struct vgic_vmcr *vmcrp) +{ +u32 vmcr = vcpu->arch.vgic_cpu.vgic_v2.vgic_vmcr; + + vmcrp->ctlr = (vmcr &GICH_VMCR_CTRL_MASK ) >> +GICH_VMCR_CTRL_SHIFT ; + vmcrp->abpr = (vmcr &GICH_VMCR_ALIAS_BINPOINT_MASK ) >> +GICH_VMCR_ALIAS_BINPOINT_SHIFT ; + vmcrp->bpr = (vmcr &GICH_VMCR_BINPOINT_MASK ) >> +GICH_VMCR_BINPOINT_SHIFT ; + vmcrp->pmr = (vmcr &GICH_VMCR_PRIMASK_MASK ) >> +GICH_VMCR_PRIMASK_SHIFT ; +}
virt/kvm/arm/vgic/vgic-v3.c +22 -0
@@ -160,3 +160,25 @@ void vgic_v3_clear_lr(struct kvm_vcpu *vcpu, int lr) { vcpu->arch.vgic_cpu.vgic_v3.vgic_lr[lr] =0 ; } + +void vgic_v3_set_vmcr (struct kvm_vcpu *vcpu,struct vgic_vmcr *vmcrp) +{ +u32 vmcr; + + vmcr = (vmcrp->ctlr <<ICH_VMCR_CTLR_SHIFT ) &ICH_VMCR_CTLR_MASK ; + vmcr |= (vmcrp->abpr <<ICH_VMCR_BPR1_SHIFT ) &ICH_VMCR_BPR1_MASK ; + vmcr |= (vmcrp->bpr <<ICH_VMCR_BPR0_SHIFT ) &ICH_VMCR_BPR0_MASK ; + vmcr |= (vmcrp->pmr <<ICH_VMCR_PMR_SHIFT ) &ICH_VMCR_PMR_MASK ; + + vcpu->arch.vgic_cpu.vgic_v3.vgic_vmcr = vmcr; +} + +void vgic_v3_get_vmcr (struct kvm_vcpu *vcpu,struct vgic_vmcr *vmcrp) +{ +u32 vmcr = vcpu->arch.vgic_cpu.vgic_v3.vgic_vmcr; + + vmcrp->ctlr = (vmcr &ICH_VMCR_CTLR_MASK ) >>ICH_VMCR_CTLR_SHIFT ; + vmcrp->abpr = (vmcr &ICH_VMCR_BPR1_MASK ) >>ICH_VMCR_BPR1_SHIFT ; + vmcrp->bpr = (vmcr &ICH_VMCR_BPR0_MASK ) >>ICH_VMCR_BPR0_SHIFT ; + vmcrp->pmr = (vmcr &ICH_VMCR_PMR_MASK ) >>ICH_VMCR_PMR_SHIFT ; +}
virt/kvm/arm/vgic/vgic.h +21 -0
@@ -27,6 +27,13 @@#define vgic_irq_is_sgi (intid) ((intid) < VGIC_NR_SGIS) +struct vgic_vmcr { +u32 ctlr; +u32 abpr; +u32 bpr; +u32 pmr; +}; +struct vgic_irq *vgic_get_irq (struct kvm *kvm,struct kvm_vcpu *vcpu,u32 intid);bool vgic_queue_irq_unlock (struct kvm *kvm,struct vgic_irq *irq);
@@ -40,6 +47,8 @@ void vgic_v2_set_underflow(struct kvm_vcpu *vcpu);int vgic_v2_has_attr_regs (struct kvm_device *dev,struct kvm_device_attr *attr);int vgic_v2_dist_uaccess (struct kvm_vcpu *vcpu,bool is_write,int offset,u32 *val); +void vgic_v2_set_vmcr (struct kvm_vcpu *vcpu,struct vgic_vmcr *vmcr); +void vgic_v2_get_vmcr (struct kvm_vcpu *vcpu,struct vgic_vmcr *vmcr);int vgic_register_dist_iodev (struct kvm *kvm,gpa_t dist_base_address,enum vgic_type );
@@ -49,6 +58,8 @@ void vgic_v3_fold_lr_state(struct kvm_vcpu *vcpu);void vgic_v3_populate_lr (struct kvm_vcpu *vcpu,struct vgic_irq *irq,int lr);void vgic_v3_clear_lr (struct kvm_vcpu *vcpu,int lr);void vgic_v3_set_underflow (struct kvm_vcpu *vcpu); +void vgic_v3_set_vmcr (struct kvm_vcpu *vcpu,struct vgic_vmcr *vmcr); +void vgic_v3_get_vmcr (struct kvm_vcpu *vcpu,struct vgic_vmcr *vmcr);int vgic_register_redist_iodevs (struct kvm *kvm,gpa_t dist_base_address);#else static inline void vgic_v3_process_maintenance (struct kvm_vcpu *vcpu)
@@ -72,6 +83,16 @@ static inline void vgic_v3_set_underflow(struct kvm_vcpu *vcpu) { } +static inline +void vgic_v3_set_vmcr (struct kvm_vcpu *vcpu,struct vgic_vmcr *vmcr) +{ +} + +static inline +void vgic_v3_get_vmcr (struct kvm_vcpu *vcpu,struct vgic_vmcr *vmcr) +{ +} +static inline int vgic_register_redist_iodevs (struct kvm *kvm,gpa_t dist_base_address) {
Signature · 1 line
2.8.2
PATCH v4 48/56 KVM: arm/arm64: vgic-new: Add userland GIC CPU interface access
In reply to Andre PrzywaraUsing the VMCR accessors we provide access to GIC CPU interface state to userland by wiring it up to the existing userland interface. [Marc: move and make VMCR accessors static, streamline MMIO handlers]
---
Changelog v2 .. v3:
- total rework, moving into vgic-mmio-v2.c
- move vmcr accessor wrapper functions into this file
- use the register description table for CPU i/f registers as well
- add RAZ/WI handling for the active priority registers
- streamline MMIO handler functions
Changelog v3 .. v4:
- advertise CPU interface registers to userland
- specify accessor width
- replace call to extract_bytes() with a simple return
virt/kvm/arm/vgic/vgic-kvm-device.c | 2 +- virt/kvm/arm/vgic/vgic-mmio-v2.c | 114 +++++++++++++++++++++++++++++++++++- virt/kvm/arm/vgic/vgic.h | 2 + 3 files changed, 116 insertions(+), 2 deletions(-)
virt/kvm/arm/vgic/vgic-kvm-device.c +1 -1
@@ -274,7 +274,7 @@ static int vgic_attr_regs_access(struct kvm_device *dev,switch (attr->group) {case KVM_DEV_ARM_VGIC_GRP_CPU_REGS : - ret = -EINVAL ; + ret =vgic_v2_cpuif_uaccess (vcpu, is_write, addr, reg);break ;case KVM_DEV_ARM_VGIC_GRP_DIST_REGS : ret =vgic_v2_dist_uaccess (vcpu, is_write, addr, reg);
virt/kvm/arm/vgic/vgic-mmio-v2.c +113 -1
138 lines not on this page · raw message
virt/kvm/arm/vgic/vgic.h +2 -0
@@ -47,6 +47,8 @@ void vgic_v2_set_underflow(struct kvm_vcpu *vcpu);int vgic_v2_has_attr_regs (struct kvm_device *dev,struct kvm_device_attr *attr);int vgic_v2_dist_uaccess (struct kvm_vcpu *vcpu,bool is_write,int offset,u32 *val); +int vgic_v2_cpuif_uaccess (struct kvm_vcpu *vcpu,bool is_write, +int offset,u32 *val);void vgic_v2_set_vmcr (struct kvm_vcpu *vcpu,struct vgic_vmcr *vmcr);void vgic_v2_get_vmcr (struct kvm_vcpu *vcpu,struct vgic_vmcr *vmcr); int vgic_register_dist_iodev(struct kvm *kvm, gpa_t dist_base_address,
Signature · 1 line
2.8.2
PATCH v4 49/56 KVM: arm/arm64: vgic-new: vgic_init: implement kvm_vgic_hyp_init
In reply to Andre PrzywaraFrom: Eric Auger <[email protected]>
Implements kvm_vgic_hyp_init and vgic_probe function.
This uses the new firmware independent VGIC probing to support both ACPI and DT based systems (code from Marc Zyngier).
The vgic_global struct is enriched with new fields populated by those functions.
---
Changelog v1 .. v2:
- rename vgic_init.c to vgic-init.c
Changelog v2 .. v3:
- include kvm/arm_vgic.h instead of kvm/vgic/vgic.h
- move ich_vtr_el2 variable into probe function
Changelog v3 .. v4:
- improve nr_lr determination in GICv2 probe
- switching to firmware-independent probing
include/kvm/vgic/vgic.h | 1 + virt/kvm/arm/vgic/vgic-init.c | 123 ++++++++++++++++++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic-v2.c | 64 ++++++++++++++++++++++ virt/kvm/arm/vgic/vgic-v3.c | 49 +++++++++++++++++ virt/kvm/arm/vgic/vgic.h | 9 ++++ 5 files changed, 246 insertions(+) create mode 100644 virt/kvm/arm/vgic/vgic-init.c
include/kvm/vgic/vgic.h +1 -0
@@ -195,6 +195,7 @@ struct vgic_cpu { };int kvm_vgic_addr (struct kvm *kvm,unsigned long type,u64 *addr,bool write); +int kvm_vgic_hyp_init (void );int kvm_vgic_inject_irq (struct kvm *kvm,int cpuid,unsigned int intid,bool level);
virt/kvm/arm/vgic/vgic-init.c new file +123 -0
123 lines not on this page · raw message
virt/kvm/arm/vgic/vgic-v2.c +64 -0
73 lines not on this page · raw message
virt/kvm/arm/vgic/vgic-v3.c +49 -0
58 lines not on this page · raw message
virt/kvm/arm/vgic/vgic.h +9 -0
@@ -16,6 +16,8 @@#ifndef __KVM_ARM_VGIC_NEW_H__#define __KVM_ARM_VGIC_NEW_H__ +#include <linux/irqchip/arm-gic-common.h> +#define PRODUCT_ID_KVM 0x4b/* ASCII code K */ #define IMPLEMENTER_ARM 0x43b
@@ -51,6 +53,7 @@ int vgic_v2_cpuif_uaccess(struct kvm_vcpu *vcpu, bool is_write, int offset, u32 *val);void vgic_v2_set_vmcr (struct kvm_vcpu *vcpu,struct vgic_vmcr *vmcr);void vgic_v2_get_vmcr (struct kvm_vcpu *vcpu,struct vgic_vmcr *vmcr); +int vgic_v2_probe (const struct gic_kvm_info *info);int vgic_register_dist_iodev (struct kvm *kvm,gpa_t dist_base_address,enum vgic_type );
@@ -62,6 +65,7 @@ void vgic_v3_clear_lr(struct kvm_vcpu *vcpu, int lr);void vgic_v3_set_underflow (struct kvm_vcpu *vcpu);void vgic_v3_set_vmcr (struct kvm_vcpu *vcpu,struct vgic_vmcr *vmcr);void vgic_v3_get_vmcr (struct kvm_vcpu *vcpu,struct vgic_vmcr *vmcr); +int vgic_v3_probe (const struct gic_kvm_info *info);int vgic_register_redist_iodevs (struct kvm *kvm,gpa_t dist_base_address);#else static inline void vgic_v3_process_maintenance (struct kvm_vcpu *vcpu)
@@ -95,6 +99,11 @@ void vgic_v3_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr) { } +static inline int vgic_v3_probe (const struct gic_kvm_info *info) +{ +return -ENODEV ; +} +static inline int vgic_register_redist_iodevs (struct kvm *kvm,gpa_t dist_base_address) {
Signature · 1 line
2.8.2
PATCH v4 50/56 KVM: arm/arm64: vgic-new: vgic_init: implement vgic_create
In reply to Andre PrzywaraFrom: Eric Auger <[email protected]>
This patch implements the vgic_creation function which is called on CREATE_IRQCHIP VM IOCTL (v2 only) or KVM_CREATE_DEVICE
---
Changelog v3 .. v4:
- fix comment
include/kvm/vgic/vgic.h | 1 + virt/kvm/arm/vgic/vgic-init.c | 84 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+)
include/kvm/vgic/vgic.h +1 -0
@@ -195,6 +195,7 @@ struct vgic_cpu { };int kvm_vgic_addr (struct kvm *kvm,unsigned long type,u64 *addr,bool write); +int kvm_vgic_create (struct kvm *kvm,u32 type);int kvm_vgic_hyp_init (void ); int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int intid,
virt/kvm/arm/vgic/vgic-init.c +84 -0
90 lines not on this page · raw message
Signature · 1 line
2.8.2
PATCH v4 2/56 KVM: arm/arm64: vgic: avoid map in kvm_vgic_inject_mapped_irq()
In reply to Andre PrzywaraWhen we want to inject a hardware mapped IRQ into a guest, we actually only need the virtual IRQ number from the irq_phys_map.
So let's pass this number directly from the arch timer to the VGIC to avoid using the map as a parameter.
--- include/kvm/arm_vgic.h | 2 +- virt/kvm/arm/arch_timer.c | 2 +- virt/kvm/arm/vgic.c | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-)
include/kvm/arm_vgic.h +1 -1
@@ -342,7 +342,7 @@ void kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu);int kvm_vgic_inject_irq (struct kvm *kvm,int cpuid,unsigned int irq_num,bool level);int kvm_vgic_inject_mapped_irq (struct kvm *kvm,int cpuid, -struct irq_phys_map *map,bool level); +unsigned int virt_irq,bool level);void vgic_v3_dispatch_sgi (struct kvm_vcpu *vcpu,u64 reg);int kvm_vgic_vcpu_pending_irq (struct kvm_vcpu *vcpu); struct irq_phys_map *kvm_vgic_map_phys_irq(struct kvm_vcpu *vcpu,
virt/kvm/arm/arch_timer.c +1 -1
@@ -177,7 +177,7 @@ static void kvm_timer_update_irq(struct kvm_vcpu *vcpu, bool new_level)trace_kvm_timer_update_irq (vcpu->vcpu_id, timer->map->virt_irq, timer->irq.level); ret =kvm_vgic_inject_mapped_irq (vcpu->kvm, vcpu->vcpu_id, - timer->map, + timer->map->virt_irq, timer->irq.level);WARN_ON (ret); }
virt/kvm/arm/vgic.c +3 -3
@@ -1666,7 +1666,7 @@ int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int irq_num,* kvm_vgic_inject_mapped_irq - Inject a physically mapped IRQ to the vgic * @kvm: The VM structure pointer * @cpuid: The CPU for PPIs -* @map: Pointer to a irq_phys_map structure describing the mapping +* @virt_irq: The virtual IRQ to be injected * @level: Edge-triggered: true: to trigger the interrupt * false: to ignore the call * Level-sensitive true: raise the input signal
@@ -1677,7 +1677,7 @@ int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int irq_num,* being HIGH and 0 being LOW and all devices being active-HIGH. */ int kvm_vgic_inject_mapped_irq (struct kvm *kvm,int cpuid, -struct irq_phys_map *map,bool level) +unsigned int virt_irq,bool level) {int ret;
@@ -1685,7 +1685,7 @@ int kvm_vgic_inject_mapped_irq(struct kvm *kvm, int cpuid,if (ret)return ret; -return vgic_update_irq_pending (kvm, cpuid, map->virt_irq, level); +return vgic_update_irq_pending (kvm, cpuid, virt_irq, level); }static irqreturn_t vgic_maintenance_handler (int irq,void *data)
Signature · 1 line
2.8.2
PATCH v4 51/56 KVM: arm/arm64: vgic-new: vgic_init: implement vgic_init
In reply to Andre PrzywaraFrom: Eric Auger <[email protected]>
This patch allocates and initializes the data structures used to model the vgic distributor and virtual cpu interfaces. At that stage the number of IRQs and number of virtual CPUs is frozen.
---
Changelog RFC..v1:
- initialize v2/v3 default CPU affinities explicitly
Changelog v1 .. v2:
- move lazy_init() into vgic_update_irq_pending()
Changelog v3 .. v4:
- fix comments
- make local functions static
- adapt to new firmware independent probing
include/kvm/vgic/vgic.h | 7 +- virt/kvm/arm/vgic/vgic-init.c | 217 ++++++++++++++++++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic-v2.c | 5 + virt/kvm/arm/vgic/vgic-v3.c | 5 + virt/kvm/arm/vgic/vgic.c | 4 + virt/kvm/arm/vgic/vgic.h | 8 ++ 6 files changed, 245 insertions(+), 1 deletion(-)
include/kvm/vgic/vgic.h +6 -1
@@ -119,6 +119,7 @@ struct vgic_io_device {struct vgic_dist {bool in_kernel;bool ready; +bool initialized;/* vGIC model the kernel emulates for the guest (GICv2 or GICv3) */ u32 vgic_model;
@@ -195,7 +196,11 @@ struct vgic_cpu { };int kvm_vgic_addr (struct kvm *kvm,unsigned long type,u64 *addr,bool write); +void kvm_vgic_early_init (struct kvm *kvm);int kvm_vgic_create (struct kvm *kvm,u32 type); +void kvm_vgic_destroy (struct kvm *kvm); +void kvm_vgic_vcpu_early_init (struct kvm_vcpu *vcpu); +void kvm_vgic_vcpu_destroy (struct kvm_vcpu *vcpu);int kvm_vgic_hyp_init (void ); int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int intid,
@@ -204,7 +209,7 @@ int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int intid,int kvm_vgic_vcpu_pending_irq (struct kvm_vcpu *vcpu);#define irqchip_in_kernel (k) (!!((k)->arch.vgic.in_kernel)) -#define vgic_initialized (k) (false) +#define vgic_initialized (k) ((k)->arch.vgic.initialized)#define vgic_ready (k) ((k)->arch.vgic.ready)#define vgic_valid_spi (k, i) (((i) >= VGIC_NR_PRIVATE_IRQS) && \ ((i) < (k)->arch.vgic.nr_spis + VGIC_NR_PRIVATE_IRQS))
virt/kvm/arm/vgic/vgic-init.c +217 -0
235 lines not on this page · raw message
virt/kvm/arm/vgic/vgic-v2.c +5 -0
@@ -206,6 +206,11 @@ void vgic_v2_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcrp)GICH_VMCR_PRIMASK_SHIFT ; } +/* not yet implemented */ +void vgic_v2_enable (struct kvm_vcpu *vcpu) +{ +} +/** * vgic_v2_probe - probe for a GICv2 compatible interrupt controller in DT * @node: pointer to the DT node
virt/kvm/arm/vgic/vgic-v3.c +5 -0
@@ -186,6 +186,11 @@ void vgic_v3_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcrp) vmcrp->pmr = (vmcr &ICH_VMCR_PMR_MASK ) >>ICH_VMCR_PMR_SHIFT ; } +/* not yet implemented */ +void vgic_v3_enable (struct kvm_vcpu *vcpu) +{ +} +/** * vgic_v3_probe - probe for a GICv3 compatible interrupt controller in DT * @node: pointer to the DT node
virt/kvm/arm/vgic/vgic.c +4 -0
@@ -257,6 +257,10 @@ static int vgic_update_irq_pending(struct kvm *kvm, int cpuid,trace_vgic_update_irq_pending (cpuid, intid, level); + ret =vgic_lazy_init (kvm); +if (ret) +return ret; + vcpu =kvm_get_vcpu (kvm, cpuid);if (!vcpu && intid <VGIC_NR_PRIVATE_IRQS )return -EINVAL ;
virt/kvm/arm/vgic/vgic.h +8 -0
@@ -53,6 +53,7 @@ int vgic_v2_cpuif_uaccess(struct kvm_vcpu *vcpu, bool is_write, int offset, u32 *val);void vgic_v2_set_vmcr (struct kvm_vcpu *vcpu,struct vgic_vmcr *vmcr);void vgic_v2_get_vmcr (struct kvm_vcpu *vcpu,struct vgic_vmcr *vmcr); +void vgic_v2_enable (struct kvm_vcpu *vcpu);int vgic_v2_probe (const struct gic_kvm_info *info);int vgic_register_dist_iodev (struct kvm *kvm,gpa_t dist_base_address,enum vgic_type );
@@ -65,6 +66,7 @@ void vgic_v3_clear_lr(struct kvm_vcpu *vcpu, int lr);void vgic_v3_set_underflow (struct kvm_vcpu *vcpu);void vgic_v3_set_vmcr (struct kvm_vcpu *vcpu,struct vgic_vmcr *vmcr);void vgic_v3_get_vmcr (struct kvm_vcpu *vcpu,struct vgic_vmcr *vmcr); +void vgic_v3_enable (struct kvm_vcpu *vcpu);int vgic_v3_probe (const struct gic_kvm_info *info);int vgic_register_redist_iodevs (struct kvm *kvm,gpa_t dist_base_address);#else
@@ -99,6 +101,10 @@ void vgic_v3_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr) { } +static inline void vgic_v3_enable (struct kvm_vcpu *vcpu) +{ +} +static inline int vgic_v3_probe (const struct gic_kvm_info *info) {return -ENODEV ;
@@ -112,5 +118,7 @@ static inline int vgic_register_redist_iodevs(struct kvm *kvm,#endif void kvm_register_vgic_device (unsigned long type); +int vgic_lazy_init (struct kvm *kvm); +int vgic_init (struct kvm *kvm);#endif
Signature · 1 line
2.8.2
PATCH v4 5/56 KVM: arm/arm64: Remove the IRQ field from struct irq_phys_map
In reply to Andre PrzywaraFrom: Christoffer Dall <[email protected]>
The communication of a Linux IRQ number from outside the VGIC to the vgic was a leftover from the day when the vgic code cared about how a particular device injects virtual interrupts mapped to a physical interrupt.
We can safely remove this notion, leaving all physical IRQ handling to be done in the device driver (the arch timer in this case), which makes room for a saner API for the new VGIC.
---
Changelog v1 .. v2:
- update kerneldoc comments on kvm_vgic_map_phys_irq()
Changelog v2 .. v3:
- include <linux/irq.h>
include/kvm/arm_vgic.h | 3 +-- virt/kvm/arm/arch_timer.c | 23 +++++++++++++++++++++-- virt/kvm/arm/vgic.c | 28 ++++++---------------------- 3 files changed, 28 insertions(+), 26 deletions(-)
include/kvm/arm_vgic.h +1 -2
@@ -158,7 +158,6 @@ struct vgic_io_device {struct irq_phys_map {u32 virt_irq;u32 phys_irq; -u32 irq; };struct irq_phys_map_entry {
@@ -346,7 +345,7 @@ int kvm_vgic_inject_mapped_irq(struct kvm *kvm, int cpuid,void vgic_v3_dispatch_sgi (struct kvm_vcpu *vcpu,u64 reg);int kvm_vgic_vcpu_pending_irq (struct kvm_vcpu *vcpu);struct irq_phys_map *kvm_vgic_map_phys_irq (struct kvm_vcpu *vcpu, -int virt_irq,int irq); +int virt_irq,int phys_irq);int kvm_vgic_unmap_phys_irq (struct kvm_vcpu *vcpu,unsigned int virt_irq);bool kvm_vgic_map_is_active (struct kvm_vcpu *vcpu,unsigned int virt_irq);
virt/kvm/arm/arch_timer.c +21 -2
@@ -20,6 +20,7 @@#include <linux/kvm.h> #include <linux/kvm_host.h> #include <linux/interrupt.h> +#include <linux/irq.h> #include <clocksource/arm_arch_timer.h> #include <asm/arch_timer.h>
@@ -300,7 +301,7 @@ void kvm_timer_flush_hwstate(struct kvm_vcpu *vcpu)if (timer->active_cleared_last && !phys_active)return ; - ret =irq_set_irqchip_state (timer->map->irq, + ret =irq_set_irqchip_state (host_vtimer_irq,IRQCHIP_STATE_ACTIVE , phys_active);WARN_ON (ret);
@@ -333,6 +334,9 @@ int kvm_timer_vcpu_reset(struct kvm_vcpu *vcpu, {struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;struct irq_phys_map *map; +struct irq_desc *desc; +struct irq_data *data; +int phys_irq;/* * The vcpu timer irq number cannot be determined in
@@ -352,10 +356,25 @@ int kvm_timer_vcpu_reset(struct kvm_vcpu *vcpu,kvm_timer_update_state (vcpu);/* +* Find the physical IRQ number corresponding to the host_vtimer_irq +*/ + desc =irq_to_desc (host_vtimer_irq); +if (!desc) { +kvm_err ("%s: no interrupt descriptor\n" , __func__); +return -EINVAL ; + } + + data =irq_desc_get_irq_data (desc); +while (data->parent_data) + data = data->parent_data; + + phys_irq = data->hwirq; + +/* * Tell the VGIC that the virtual interrupt is tied to a * physical interrupt. We do that once per VCPU. */ - map =kvm_vgic_map_phys_irq (vcpu, irq->irq, host_vtimer_irq); + map =kvm_vgic_map_phys_irq (vcpu, irq->irq, phys_irq);if (WARN_ON (IS_ERR (map)))return PTR_ERR (map);
virt/kvm/arm/vgic.c +6 -22
@@ -1711,38 +1711,24 @@ static struct list_head *vgic_get_irq_phys_map_list(struct kvm_vcpu *vcpu,/** * kvm_vgic_map_phys_irq - map a virtual IRQ to a physical IRQ * @vcpu: The VCPU pointer -* @virt_irq: The virtual irq number -* @irq: The Linux IRQ number +* @virt_irq: The virtual IRQ number for the guest +* @phys_irq: The hardware IRQ number of the host * * Establish a mapping between a guest visible irq (@virt_irq) and a -* Linux irq (@irq). On injection, @virt_irq will be associated with -* the physical interrupt represented by @irq. This mapping can be +* hardware irq (@phys_irq). On injection, @virt_irq will be associated with +* the physical interrupt represented by @phys_irq. This mapping can be * established multiple times as long as the parameters are the same. * * Returns a valid pointer on success, and an error pointer otherwise */ struct irq_phys_map *kvm_vgic_map_phys_irq (struct kvm_vcpu *vcpu, -int virt_irq,int irq) +int virt_irq,int phys_irq) {struct vgic_dist *dist = &vcpu->kvm->arch.vgic;struct list_head *root =vgic_get_irq_phys_map_list (vcpu, virt_irq);struct irq_phys_map *map;struct irq_phys_map_entry *entry; -struct irq_desc *desc; -struct irq_data *data; -int phys_irq; - desc =irq_to_desc (irq); -if (!desc) { -kvm_err ("%s: no interrupt descriptor\n" , __func__); -return ERR_PTR (-EINVAL ); - } - - data =irq_desc_get_irq_data (desc); -while (data->parent_data) - data = data->parent_data; - - phys_irq = data->hwirq;/* Create a new mapping */ entry =kzalloc (sizeof (*entry),GFP_KERNEL );
@@ -1755,8 +1741,7 @@ struct irq_phys_map *kvm_vgic_map_phys_irq(struct kvm_vcpu *vcpu, map =vgic_irq_map_search (vcpu, virt_irq);if (map) {/* Make sure this mapping matches */ -if (map->phys_irq != phys_irq || - map->irq != irq) +if (map->phys_irq != phys_irq) map =ERR_PTR (-EINVAL );/* Found an existing, valid mapping */
@@ -1766,7 +1751,6 @@ struct irq_phys_map *kvm_vgic_map_phys_irq(struct kvm_vcpu *vcpu, map = &entry->map; map->virt_irq = virt_irq; map->phys_irq = phys_irq; - map->irq = irq;list_add_tail_rcu (&entry->entry, root);
Signature · 1 line
2.8.2
PATCH v4 53/56 KVM: arm/arm64: vgic-new: Add vgic_v2/v3_enable
In reply to Andre PrzywaraFrom: Eric Auger <[email protected]>
Enable the VGIC operation by properly initialising the registers in the hypervisor GIC interface.
--- virt/kvm/arm/vgic/vgic-v2.c | 11 ++++++++++- virt/kvm/arm/vgic/vgic-v3.c | 23 ++++++++++++++++++++++- 2 files changed, 32 insertions(+), 2 deletions(-)
virt/kvm/arm/vgic/vgic-v2.c +10 -1
@@ -206,9 +206,18 @@ void vgic_v2_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcrp)GICH_VMCR_PRIMASK_SHIFT ; } -/* not yet implemented */ void vgic_v2_enable (struct kvm_vcpu *vcpu) { +/* +* By forcing VMCR to zero, the GIC will restore the binary +* points to their reset values. Anything else resets to zero +* anyway. +*/ + vcpu->arch.vgic_cpu.vgic_v2.vgic_vmcr =0 ; + vcpu->arch.vgic_cpu.vgic_v2.vgic_elrsr = ~0 ; + +/* Get the show on the road... */ + vcpu->arch.vgic_cpu.vgic_v2.vgic_hcr =GICH_HCR_EN ; }/* check for overlapping regions and for regions crossing the end of memory */
virt/kvm/arm/vgic/vgic-v3.c +22 -1
@@ -186,9 +186,30 @@ void vgic_v3_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcrp) vmcrp->pmr = (vmcr &ICH_VMCR_PMR_MASK ) >>ICH_VMCR_PMR_SHIFT ; } -/* not yet implemented */ void vgic_v3_enable (struct kvm_vcpu *vcpu) { +struct vgic_v3_cpu_if *vgic_v3 = &vcpu->arch.vgic_cpu.vgic_v3; + +/* +* By forcing VMCR to zero, the GIC will restore the binary +* points to their reset values. Anything else resets to zero +* anyway. +*/ + vgic_v3->vgic_vmcr =0 ; + vgic_v3->vgic_elrsr = ~0 ; + +/* +* If we are emulating a GICv3, we do it in an non-GICv2-compatible +* way, so we force SRE to 1 to demonstrate this to the guest. +* This goes with the spec allowing the value to be RAO/WI. +*/ +if (vcpu->kvm->arch.vgic.vgic_model ==KVM_DEV_TYPE_ARM_VGIC_V3 ) + vgic_v3->vgic_sre =ICC_SRE_EL1_SRE ; +else + vgic_v3->vgic_sre =0 ; + +/* Get the show on the road... */ + vgic_v3->vgic_hcr =ICH_HCR_EN ; }/* check for overlapping regions and for regions crossing the end of memory */
Signature · 1 line
2.8.2
PATCH v4 54/56 KVM: arm/arm64: vgic-new: Wire up irqfd injection
In reply to Andre PrzywaraConnect to the new VGIC to the irqfd framework, so that we can inject IRQs.
GSI routing and MSI routing is not yet implemented.
--- virt/kvm/arm/vgic/vgic-irqfd.c | 52 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 virt/kvm/arm/vgic/vgic-irqfd.c
virt/kvm/arm/vgic/vgic-irqfd.c new file +52 -0
@@ -0,0 +1,52 @@ +/* +* Copyright (C) 2015, 2016 ARM Ltd. +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include <linux/kvm.h> +#include <linux/kvm_host.h> +#include <trace/events/kvm.h> + +int kvm_irq_map_gsi (struct kvm *kvm, +struct kvm_kernel_irq_routing_entry *entries, +int gsi) +{ +return 0 ; +} + +int kvm_irq_map_chip_pin (struct kvm *kvm,unsigned int irqchip, +unsigned int pin) +{ +return pin; +} + +int kvm_set_irq (struct kvm *kvm,int irq_source_id, +u32 irq,int level,bool line_status) +{ +unsigned int spi = irq +VGIC_NR_PRIVATE_IRQS ; + +trace_kvm_set_irq (irq, level, irq_source_id); + +BUG_ON (!vgic_initialized (kvm)); + +return kvm_vgic_inject_irq (kvm,0 , spi, level); +} + +/* MSI not implemented yet */ +int kvm_set_msi (struct kvm_kernel_irq_routing_entry *e, +struct kvm *kvm,int irq_source_id, +int level,bool line_status) +{ +return 0 ; +}
Signature · 1 line
2.8.2
PATCH v4 55/56 KVM: arm/arm64: vgic-new: implement mapped IRQ handling
In reply to Andre PrzywaraWe now store the mapped hardware IRQ number in our struct, so we don't need the irq_phys_map for the new VGIC. Implement the hardware IRQ mapping on top of the reworked arch timer interface.
---
Changelog RFC..v1:
- adapt to new arch_timer mapped IRQ interface
- implement inject_mapped_irq() as a macro (since it is identical to the "un-mapped" IRQ implementation)
Changelog v1 .. v2:
- replace inject_mapped_irq() macro with a separate implementation, which only allows mapped IRQs to be injected via this interface
include/kvm/vgic/vgic.h | 5 +++++ virt/kvm/arm/vgic/vgic.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+)
include/kvm/vgic/vgic.h +5 -0
@@ -206,6 +206,11 @@ int kvm_vgic_hyp_init(void);int kvm_vgic_inject_irq (struct kvm *kvm,int cpuid,unsigned int intid,bool level); +int kvm_vgic_inject_mapped_irq (struct kvm *kvm,int cpuid,unsigned int intid, +bool level); +int kvm_vgic_map_phys_irq (struct kvm_vcpu *vcpu,u32 virt_irq,u32 phys_irq); +int kvm_vgic_unmap_phys_irq (struct kvm_vcpu *vcpu,unsigned int virt_irq); +bool kvm_vgic_map_is_active (struct kvm_vcpu *vcpu,unsigned int virt_irq);int kvm_vgic_vcpu_pending_irq (struct kvm_vcpu *vcpu);
virt/kvm/arm/vgic/vgic.c +50 -0
59 lines not on this page · raw message
Signature · 1 line
2.8.2
PATCH v4 11/56 KVM: arm/arm64: pmu: abstract access to number of SPIs
In reply to Andre PrzywaraCurrently the PMU uses a member of the struct vgic_dist directly, which not only breaks abstraction, but will fail with the new VGIC. Abstract this access in the VGIC header file and refactor the validity check in the PMU code.
---
Changelog v3 .. v4:
- fix logic to allow PPIs again
- refactor IRQ validity check
include/kvm/arm_vgic.h | 2 ++ virt/kvm/arm/pmu.c | 25 ++++++++++++++----------- 2 files changed, 16 insertions(+), 11 deletions(-)
include/kvm/arm_vgic.h +2 -0
@@ -348,6 +348,8 @@ bool kvm_vgic_map_is_active(struct kvm_vcpu *vcpu, unsigned int virt_irq);#define irqchip_in_kernel (k) (!!((k)->arch.vgic.in_kernel))#define vgic_initialized (k) (!!((k)->arch.vgic.nr_cpus))#define vgic_ready (k) ((k)->arch.vgic.ready) +#define vgic_valid_spi (k, i) (((i) >= VGIC_NR_PRIVATE_IRQS) && \ + ((i) < (k)->arch.vgic.nr_irqs)) int vgic_v2_probe(const struct gic_kvm_info *gic_kvm_info, const struct vgic_ops **ops,
virt/kvm/arm/pmu.c +14 -11
@@ -436,7 +436,14 @@ static int kvm_arm_pmu_v3_init(struct kvm_vcpu *vcpu)return 0 ; } -static bool irq_is_valid (struct kvm *kvm,int irq,bool is_ppi) +#define irq_is_ppi (irq) ((irq) >= VGIC_NR_SGIS && (irq) < VGIC_NR_PRIVATE_IRQS) + +/* +* For one VM the interrupt type must be same for each vcpu. +* As a PPI, the interrupt number is the same for all vcpus, +* while as an SPI it must be a separate number per vcpu. +*/ +static bool pmu_irq_is_valid (struct kvm *kvm,int irq) {int i;struct kvm_vcpu *vcpu;
@@ -445,7 +452,7 @@ static bool irq_is_valid(struct kvm *kvm, int irq, bool is_ppi)if (!kvm_arm_pmu_irq_initialized (vcpu))continue ; -if (is_ppi) { +if (irq_is_ppi (irq)) {if (vcpu->arch.pmu.irq_num != irq)return false; }else {
@@ -457,7 +464,6 @@ static bool irq_is_valid(struct kvm *kvm, int irq, bool is_ppi)return true; } -int kvm_arm_pmu_v3_set_attr (struct kvm_vcpu *vcpu,struct kvm_device_attr *attr) {switch (attr->attr) {
@@ -471,14 +477,11 @@ int kvm_arm_pmu_v3_set_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)if (get_user (irq, uaddr))return -EFAULT ; -/* -* The PMU overflow interrupt could be a PPI or SPI, but for one -* VM the interrupt type must be same for each vcpu. As a PPI, -* the interrupt number is the same for all vcpus, while as an -* SPI it must be a separate number per vcpu. -*/ -if (irq <VGIC_NR_SGIS || irq >= vcpu->kvm->arch.vgic.nr_irqs || - !irq_is_valid (vcpu->kvm, irq, irq <VGIC_NR_PRIVATE_IRQS )) +/* The PMU overflow interrupt can be a PPI or a valid SPI. */ +if (!(irq_is_ppi (irq) ||vgic_valid_spi (vcpu->kvm, irq))) +return -EINVAL ; + +if (!pmu_irq_is_valid (vcpu->kvm, irq))return -EINVAL ;if (kvm_arm_pmu_irq_initialized (vcpu))
Signature · 1 line
2.8.2
PATCH v4 12/56 KVM: arm/arm64: Provide functionality to pause and resume a guest
In reply to Andre PrzywaraFrom: Christoffer Dall <[email protected]>
For some rare corner cases in our VGIC emulation later we have to stop the guest to make sure the VGIC state is consistent. Provide the necessary framework to pause and resume a guest.
--- arch/arm/include/asm/kvm_host.h | 4 ++++ arch/arm/kvm/arm.c | 25 +++++++++++++------------ arch/arm64/include/asm/kvm_host.h | 4 ++++ 3 files changed, 21 insertions(+), 12 deletions(-)
arch/arm/include/asm/kvm_host.h +4 -0
@@ -41,6 +41,8 @@#define KVM_MAX_VCPUS VGIC_V2_MAX_CPUS +#define KVM_REQ_VCPU_EXIT 8 +u32 *kvm_vcpu_reg (struct kvm_vcpu *vcpu,u8 reg_num,u32 mode); int __attribute_const__ kvm_target_cpu(void);int kvm_reset_vcpu (struct kvm_vcpu *vcpu);
@@ -225,6 +227,8 @@ static inline void kvm_arch_mmu_notifier_invalidate_page(struct kvm *kvm,struct kvm_vcpu *kvm_arm_get_running_vcpu (void );struct kvm_vcpu __percpu **kvm_get_running_vcpus (void ); +void kvm_arm_halt_guest (struct kvm *kvm); +void kvm_arm_resume_guest (struct kvm *kvm);int kvm_arm_copy_coproc_indices (struct kvm_vcpu *vcpu,u64 __user *uindices);unsigned long kvm_arm_num_coproc_regs (struct kvm_vcpu *vcpu);
arch/arm/kvm/arm.c +13 -12
@@ -492,30 +492,31 @@ bool kvm_arch_intc_initialized(struct kvm *kvm)return vgic_initialized (kvm); } -static void kvm_arm_halt_guest (struct kvm *kvm)__maybe_unused ; -static void kvm_arm_resume_guest (struct kvm *kvm)__maybe_unused ; - -static void kvm_arm_halt_guest (struct kvm *kvm) +void kvm_arm_halt_guest (struct kvm *kvm) {int i;struct kvm_vcpu *vcpu;kvm_for_each_vcpu (i, vcpu, kvm) vcpu->arch.pause = true; -force_vm_exit (cpu_all_mask); +kvm_make_all_cpus_request (kvm,KVM_REQ_VCPU_EXIT ); +} + +static void kvm_arm_resume_vcpu (struct kvm_vcpu *vcpu) +{ +struct swait_queue_head *wq =kvm_arch_vcpu_wq (vcpu); + + vcpu->arch.pause = false; +swake_up (wq); } -static void kvm_arm_resume_guest (struct kvm *kvm) +void kvm_arm_resume_guest (struct kvm *kvm) {int i;struct kvm_vcpu *vcpu; -kvm_for_each_vcpu (i, vcpu, kvm) { -struct swait_queue_head *wq =kvm_arch_vcpu_wq (vcpu); - - vcpu->arch.pause = false; -swake_up (wq); - } +kvm_for_each_vcpu (i, vcpu, kvm) +kvm_arm_resume_vcpu (vcpu); }static void vcpu_sleep (struct kvm_vcpu *vcpu)
arch/arm64/include/asm/kvm_host.h +4 -0
@@ -44,6 +44,8 @@#define KVM_VCPU_MAX_FEATURES 4 +#define KVM_REQ_VCPU_EXIT 8 + int __attribute_const__ kvm_target_cpu(void);int kvm_reset_vcpu (struct kvm_vcpu *vcpu);int kvm_arch_dev_ioctl_check_extension (long ext);
@@ -325,6 +327,8 @@ static inline void kvm_arch_mmu_notifier_invalidate_page(struct kvm *kvm,struct kvm_vcpu *kvm_arm_get_running_vcpu (void );struct kvm_vcpu *__percpu *kvm_get_running_vcpus (void ); +void kvm_arm_halt_guest (struct kvm *kvm); +void kvm_arm_resume_guest (struct kvm *kvm);u64 __kvm_call_hyp (void *hypfn, ...);#define kvm_call_hyp (f, ...) __kvm_call_hyp(kvm_ksym_ref(f), ##__VA_ARGS__)
Signature · 1 line
2.8.2
PATCH v4 13/56 KVM: arm/arm64: move GICv2 emulation defines into arm-gic-v3.h
In reply to Andre PrzywaraAs (some) GICv3 hosts can emulate a GICv2, some GICv2 specific masks for the list register definition also apply to GICv3 LRs. At the moment we have those definitions in the KVM VGICv3 implementation, so let's move them into the GICv3 header file to have them automatically defined.
--- include/linux/irqchip/arm-gic-v3.h | 5 +++++ virt/kvm/arm/vgic-v3.c | 8 +------- 2 files changed, 6 insertions(+), 7 deletions(-)
include/linux/irqchip/arm-gic-v3.h +5 -0
@@ -276,6 +276,11 @@#define ICH_LR_PHYS_ID_SHIFT 32#define ICH_LR_PHYS_ID_MASK (0x3ffULL << ICH_LR_PHYS_ID_SHIFT) +/* These are for GICv2 emulation only */ +#define GICH_LR_VIRTUALID (0x3ffUL << 0) +#define GICH_LR_PHYSID_CPUID_SHIFT (10) +#define GICH_LR_PHYSID_CPUID (7UL << GICH_LR_PHYSID_CPUID_SHIFT) +#define ICH_MISR_EOI (1 << 0)#define ICH_MISR_U (1 << 1)
virt/kvm/arm/vgic-v3.c +1 -7
@@ -29,12 +29,6 @@#include <asm/kvm_asm.h> #include <asm/kvm_mmu.h> -/* These are for GICv2 emulation only */ -#define GICH_LR_VIRTUALID (0x3ffUL << 0) -#define GICH_LR_PHYSID_CPUID_SHIFT (10) -#define GICH_LR_PHYSID_CPUID (7UL << GICH_LR_PHYSID_CPUID_SHIFT) -#define ICH_LR_VIRTUALID_MASK (BIT_ULL(32) - 1) -static u32 ich_vtr_el2;static struct vgic_lr vgic_v3_get_lr (const struct kvm_vcpu *vcpu,int lr)
@@ -43,7 +37,7 @@ static struct vgic_lr vgic_v3_get_lr(const struct kvm_vcpu *vcpu, int lr)u64 val = vcpu->arch.vgic_cpu.vgic_v3.vgic_lr[lr];if (vcpu->kvm->arch.vgic.vgic_model ==KVM_DEV_TYPE_ARM_VGIC_V3 ) - lr_desc.irq = val &ICH_LR_VIRTUALID_MASK ; + lr_desc.irq = val &ICH_LR_VIRTUAL_ID_MASK ;else lr_desc.irq = val &GICH_LR_VIRTUALID ;
Signature · 1 line
2.8.2
PATCH v4 17/56 KVM: arm/arm64: vgic-new: Add IRQ sorting
In reply to Andre PrzywaraFrom: Christoffer Dall <[email protected]>
Adds the sorting function to cover the case where you have more IRQs to consider than you have LRs. We now consider priorities.
--- virt/kvm/arm/vgic/vgic.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+)
virt/kvm/arm/vgic/vgic.c +57 -0
@@ -16,6 +16,7 @@#include <linux/kvm.h> #include <linux/kvm_host.h> +#include <linux/list_sort.h> #include "vgic.h"
@@ -103,6 +104,62 @@ static struct kvm_vcpu *vgic_target_oracle(struct vgic_irq *irq) }/* +* The order of items in the ap_lists defines how we'll pack things in LRs as +* well, the first items in the list being the first things populated in the +* LRs. +* +* A hard rule is that active interrupts can never be pushed out of the LRs +* (and therefore take priority) since we cannot reliably trap on deactivation +* of IRQs and therefore they have to be present in the LRs. +* +* Otherwise things should be sorted by the priority field and the GIC +* hardware support will take care of preemption of priority groups etc. +* +* Return negative if "a" sorts before "b", 0 to preserve order, and positive +* to sort "b" before "a". +*/ +static int vgic_irq_cmp (void *priv,struct list_head *a,struct list_head *b) +{ + struct vgic_irq *irqa = container_of(a, struct vgic_irq, ap_list); + struct vgic_irq *irqb = container_of(b, struct vgic_irq, ap_list); +bool penda, pendb; +int ret; + +spin_lock (&irqa->irq_lock); +spin_lock (&irqb->irq_lock); + +if (irqa->active || irqb->active) { + ret = (int )irqb->active - (int )irqa->active; + goto out; + } + + penda = irqa->enabled && irqa->pending; + pendb = irqb->enabled && irqb->pending; + +if (!penda || !pendb) { + ret = (int )pendb - (int )penda; + goto out; + } + +/* Both pending and enabled, sort by priority */ + ret = irqa->priority - irqb->priority; +out: +spin_unlock (&irqb->irq_lock); +spin_unlock (&irqa->irq_lock); +return ret; +} + +/* Must be called with the ap_list_lock held */ +static void vgic_sort_ap_list (struct kvm_vcpu *vcpu) +{ +struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; + +DEBUG_SPINLOCK_BUG_ON (!spin_is_locked (&vgic_cpu->ap_list_lock)); + +list_sort (NULL , &vgic_cpu->ap_list_head, vgic_irq_cmp); +} + +/* * Only valid injection if changing level for level-triggered IRQs or for a * rising edge. */
Signature · 1 line
2.8.2
PATCH v4 18/56 KVM: arm/arm64: vgic-new: Add IRQ sync/flush framework
In reply to Andre PrzywaraFrom: Marc Zyngier <[email protected]>
Implement the framework for syncing IRQs between our emulation and the list registers, which represent the guest's view of IRQs. This is done in kvm_vgic_flush_hwstate and kvm_vgic_sync_hwstate, which gets called on guest entry and exit.
The code talking to the actual GICv2/v3 hardware is added in the following patches.
---
Changelog RFC..v1:
- split out vgic_clear_lr() from vgic_populate_lr()
- rename vgic_populate_lrs() to vgic_flush_lr_state()
- clean all LRs when the distributor is disabled
- use list_del() instead of list_del_init()
- add comments to explain the direction of sync/flush_hwstate
- remove unneeded BUG_ON(in_interrupt()
Changelog v2 .. v3:
- remove bogus v2 specific rebase leftovers
Changelog v3 .. v4:
- amend locks requirements
include/kvm/vgic/vgic.h | 4 + virt/kvm/arm/vgic/vgic.c | 191 +++++++++++++++++++++++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic.h | 2 + 3 files changed, 197 insertions(+)
include/kvm/vgic/vgic.h +4 -0
@@ -190,6 +190,10 @@ int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int intid,#define vgic_valid_spi (k, i) (((i) >= VGIC_NR_PRIVATE_IRQS) && \ ((i) < (k)->arch.vgic.nr_spis + VGIC_NR_PRIVATE_IRQS)) +bool kvm_vcpu_has_pending_irqs (struct kvm_vcpu *vcpu); +void kvm_vgic_sync_hwstate (struct kvm_vcpu *vcpu); +void kvm_vgic_flush_hwstate (struct kvm_vcpu *vcpu); +/** * kvm_vgic_get_max_vcpus - Get the maximum number of VCPUs allowed by HW *
virt/kvm/arm/vgic/vgic.c +191 -0
194 lines not on this page · raw message
virt/kvm/arm/vgic/vgic.h +2 -0
@@ -16,6 +16,8 @@#ifndef __KVM_ARM_VGIC_NEW_H__#define __KVM_ARM_VGIC_NEW_H__ +#define vgic_irq_is_sgi (intid) ((intid) < VGIC_NR_SGIS) +struct vgic_irq *vgic_get_irq (struct kvm *kvm,struct kvm_vcpu *vcpu,u32 intid);bool vgic_queue_irq_unlock (struct kvm *kvm,struct vgic_irq *irq);
Signature · 1 line
2.8.2
PATCH v4 19/56 KVM: arm/arm64: vgic-new: Add GICv2 world switch backend
In reply to Andre PrzywaraFrom: Marc Zyngier <[email protected]>
Processing maintenance interrupts and accessing the list registers are dependent on the host's GIC version.
Introduce vgic-v2.c to contain GICv2 specific functions. Implement the GICv2 specific code for syncing the emulation state into the VGIC registers.
---
Changelog RFC..v1:
- remove explicit LR_STATE clearing on maintenance interrupt handling
- improve documentation for vgic_v2_populate_lr()
- remove WARN_ON on non-edge IRQs in maintenance interrupts
- simplify multi-CPU source SGI handling
Changelog v1 .. v2:
- inject the IRQ priority into the list register
Changelog v2 .. v3:
- cleanup diff containing rebase artifacts
Changelog v3 .. v4:
- remove ELRSR bit set during process_maintenance()
include/linux/irqchip/arm-gic.h | 1 + virt/kvm/arm/vgic/vgic-v2.c | 176 ++++++++++++++++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic.c | 6 ++ virt/kvm/arm/vgic/vgic.h | 6 ++ 4 files changed, 189 insertions(+) create mode 100644 virt/kvm/arm/vgic/vgic-v2.c
include/linux/irqchip/arm-gic.h +1 -0
@@ -76,6 +76,7 @@#define GICH_LR_VIRTUALID (0x3ff << 0)#define GICH_LR_PHYSID_CPUID_SHIFT (10)#define GICH_LR_PHYSID_CPUID (0x3ff << GICH_LR_PHYSID_CPUID_SHIFT) +#define GICH_LR_PRIORITY_SHIFT 23#define GICH_LR_STATE (3 << 28)#define GICH_LR_PENDING_BIT (1 << 28)#define GICH_LR_ACTIVE_BIT (1 << 29)
virt/kvm/arm/vgic/vgic-v2.c new file +176 -0
176 lines not on this page · raw message
virt/kvm/arm/vgic/vgic.c +6 -0
@@ -399,10 +399,12 @@ retry:static inline void vgic_process_maintenance_interrupt (struct kvm_vcpu *vcpu) { +vgic_v2_process_maintenance (vcpu); }static inline void vgic_fold_lr_state (struct kvm_vcpu *vcpu) { +vgic_v2_fold_lr_state (vcpu); }/* Requires the irq_lock to be held. */
@@ -410,14 +412,18 @@ static inline void vgic_populate_lr(struct kvm_vcpu *vcpu, struct vgic_irq *irq, int lr) {DEBUG_SPINLOCK_BUG_ON (!spin_is_locked (&irq->irq_lock)); + +vgic_v2_populate_lr (vcpu, irq, lr); }static inline void vgic_clear_lr (struct kvm_vcpu *vcpu,int lr) { +vgic_v2_clear_lr (vcpu, lr); }static inline void vgic_set_underflow (struct kvm_vcpu *vcpu) { +vgic_v2_set_underflow (vcpu); }/* Requires the ap_list_lock to be held. */
virt/kvm/arm/vgic/vgic.h +6 -0
@@ -22,4 +22,10 @@ struct vgic_irq *vgic_get_irq(struct kvm *kvm, struct kvm_vcpu *vcpu, u32 intid);bool vgic_queue_irq_unlock (struct kvm *kvm,struct vgic_irq *irq); +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); +void vgic_v2_clear_lr (struct kvm_vcpu *vcpu,int lr); +void vgic_v2_set_underflow (struct kvm_vcpu *vcpu); +#endif
Signature · 1 line
2.8.2
PATCH v4 21/56 KVM: arm/arm64: vgic-new: Implement kvm_vgic_vcpu_pending_irq
In reply to Andre PrzywaraFrom: Eric Auger <[email protected]>
Tell KVM whether a particular VCPU has an IRQ that needs handling in the guest. This is used to decide whether a VCPU is runnable.
---
Changelog RFC..v1:
- return false if distributor is disabled
- add vgic_kick_vcpus() implementations
Changelog v2 .. v3:
- remove vgic_kick_vcpus() implementation from this patch
include/kvm/vgic/vgic.h | 2 ++ virt/kvm/arm/vgic/vgic.c | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+)
include/kvm/vgic/vgic.h +2 -0
@@ -184,6 +184,8 @@ struct vgic_cpu {int kvm_vgic_inject_irq (struct kvm *kvm,int cpuid,unsigned int intid,bool level); +int kvm_vgic_vcpu_pending_irq (struct kvm_vcpu *vcpu); +#define irqchip_in_kernel (k) (!!((k)->arch.vgic.in_kernel))#define vgic_initialized (k) (false)#define vgic_ready (k) ((k)->arch.vgic.ready)
virt/kvm/arm/vgic/vgic.c +25 -0
@@ -519,3 +519,28 @@ void kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu)vgic_flush_lr_state (vcpu);spin_unlock (&vcpu->arch.vgic_cpu.ap_list_lock); } + +int kvm_vgic_vcpu_pending_irq (struct kvm_vcpu *vcpu) +{ +struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; +struct vgic_irq *irq; +bool pending = false; + +if (!vcpu->kvm->arch.vgic.enabled) +return false; + +spin_lock (&vgic_cpu->ap_list_lock); + +list_for_each_entry (irq, &vgic_cpu->ap_list_head, ap_list) { +spin_lock (&irq->irq_lock); + pending = irq->pending && irq->enabled; +spin_unlock (&irq->irq_lock); + +if (pending) +break ; + } + +spin_unlock (&vgic_cpu->ap_list_lock); + +return pending; +}
Signature · 1 line
2.8.2
PATCH v4 23/56 KVM: arm/arm64: vgic-new: Add GICv2 MMIO handling framework
In reply to Andre PrzywaraCreate vgic-mmio-v2.c to describe GICv2 emulation specific handlers using the initializer macros provided by the VGIC MMIO framework. Provide a function to register the GICv2 distributor registers to the kvm_io_bus framework.
The actual handler functions are still stubs in this patch.
---
Changelog v1 .. v2:
- new patch, split out from the generic MMIO framework patch
- use a separate file to hold GICv2 emulation specific handlers
- replace _nyi stub functions with raz/wi versions
Changelog v2 .. v3:
- replace kvm/vgic/vgic.h with kvm/arm_vgic.h
- add vgic_register_dist_iodev() prototype
Changelog v3 .. v4:
- specify accessor width
virt/kvm/arm/vgic/vgic-mmio-v2.c | 76 ++++++++++++++++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic-mmio.c | 26 ++++++++++++++ virt/kvm/arm/vgic/vgic-mmio.h | 2 ++ virt/kvm/arm/vgic/vgic.h | 2 ++ 4 files changed, 106 insertions(+) create mode 100644 virt/kvm/arm/vgic/vgic-mmio-v2.c
virt/kvm/arm/vgic/vgic-mmio-v2.c new file +76 -0
@@ -0,0 +1,76 @@ +/* +* VGICv2 MMIO handling functions +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +*/ + +#include <linux/irqchip/arm-gic.h> +#include <linux/kvm.h> +#include <linux/kvm_host.h> +#include <kvm/iodev.h> +#include <kvm/arm_vgic.h> + +#include "vgic.h" +#include "vgic-mmio.h" + +static const struct vgic_register_region vgic_v2_dist_registers[] = { +REGISTER_DESC_WITH_LENGTH (GIC_DIST_CTRL , + vgic_mmio_read_raz, vgic_mmio_write_wi,12 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_BITS_PER_IRQ (GIC_DIST_IGROUP , + vgic_mmio_read_rao, vgic_mmio_write_wi,1 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_BITS_PER_IRQ (GIC_DIST_ENABLE_SET , + vgic_mmio_read_raz, vgic_mmio_write_wi,1 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_BITS_PER_IRQ (GIC_DIST_ENABLE_CLEAR , + vgic_mmio_read_raz, vgic_mmio_write_wi,1 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_BITS_PER_IRQ (GIC_DIST_PENDING_SET , + vgic_mmio_read_raz, vgic_mmio_write_wi,1 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_BITS_PER_IRQ (GIC_DIST_PENDING_CLEAR , + vgic_mmio_read_raz, vgic_mmio_write_wi,1 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_BITS_PER_IRQ (GIC_DIST_ACTIVE_SET , + vgic_mmio_read_raz, vgic_mmio_write_wi,1 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_BITS_PER_IRQ (GIC_DIST_ACTIVE_CLEAR , + vgic_mmio_read_raz, vgic_mmio_write_wi,1 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_BITS_PER_IRQ (GIC_DIST_PRI , + vgic_mmio_read_raz, vgic_mmio_write_wi,8 , + VGIC_ACCESS_32bit | VGIC_ACCESS_8bit), +REGISTER_DESC_WITH_BITS_PER_IRQ (GIC_DIST_TARGET , + vgic_mmio_read_raz, vgic_mmio_write_wi,8 , + VGIC_ACCESS_32bit | VGIC_ACCESS_8bit), +REGISTER_DESC_WITH_BITS_PER_IRQ (GIC_DIST_CONFIG , + vgic_mmio_read_raz, vgic_mmio_write_wi,2 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_LENGTH (GIC_DIST_SOFTINT , + vgic_mmio_read_raz, vgic_mmio_write_wi,4 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_LENGTH (GIC_DIST_SGI_PENDING_CLEAR , + vgic_mmio_read_raz, vgic_mmio_write_wi,16 , + VGIC_ACCESS_32bit | VGIC_ACCESS_8bit), +REGISTER_DESC_WITH_LENGTH (GIC_DIST_SGI_PENDING_SET , + vgic_mmio_read_raz, vgic_mmio_write_wi,16 , + VGIC_ACCESS_32bit | VGIC_ACCESS_8bit), +}; + +unsigned int vgic_v2_init_dist_iodev (struct vgic_io_device *dev) +{ + dev->regions = vgic_v2_dist_registers; + dev->nr_regions =ARRAY_SIZE (vgic_v2_dist_registers); + +kvm_iodevice_init (&dev->dev, &kvm_io_gic_ops); + +return SZ_4K ; +}
virt/kvm/arm/vgic/vgic-mmio.c +26 -0
@@ -182,3 +182,29 @@ struct kvm_io_device_ops kvm_io_gic_ops = { .read = dispatch_mmio_read, .write = dispatch_mmio_write, }; + +int vgic_register_dist_iodev (struct kvm *kvm,gpa_t dist_base_address, +enum vgic_type type) +{ +struct vgic_io_device *io_device = &kvm->arch.vgic.dist_iodev; +int ret =0 ; +unsigned int len; + +switch (type) { +case VGIC_V2 : + len =vgic_v2_init_dist_iodev (io_device); +break ; +default : +BUG_ON (1 ); + } + + io_device->base_addr = dist_base_address; + io_device->redist_vcpu =NULL ; + +mutex_lock (&kvm->slots_lock); + ret =kvm_io_bus_register_dev (kvm,KVM_MMIO_BUS , dist_base_address, + len, &io_device->dev); +mutex_unlock (&kvm->slots_lock); + +return ret; +}
virt/kvm/arm/vgic/vgic-mmio.h +2 -0
@@ -84,4 +84,6 @@ unsigned long vgic_mmio_read_rao(struct kvm_vcpu *vcpu,void vgic_mmio_write_wi (struct kvm_vcpu *vcpu,gpa_t addr,unsigned int len,unsigned long val); +unsigned int vgic_v2_init_dist_iodev (struct vgic_io_device *dev); +#endif
virt/kvm/arm/vgic/vgic.h +2 -0
@@ -27,6 +27,8 @@ 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);void vgic_v2_clear_lr (struct kvm_vcpu *vcpu,int lr);void vgic_v2_set_underflow (struct kvm_vcpu *vcpu); +int vgic_register_dist_iodev (struct kvm *kvm,gpa_t dist_base_address, +enum vgic_type );#ifdef CONFIG_KVM_ARM_VGIC_V3 void vgic_v3_process_maintenance (struct kvm_vcpu *vcpu);
Signature · 1 line
2.8.2
PATCH v4 25/56 KVM: arm/arm64: vgic-new: Add ENABLE registers handlers
In reply to Andre PrzywaraAs the enable register handlers are shared between the v2 and v3 emulation, their implementation goes into vgic-mmio.c, to be easily referenced from the v3 emulation as well later.
---
Changelog RFC..v1:
- use lower bits of address to determine IRQ number
- remove TODO, confirmed to be fine
Changelog v1 .. v2:
- adapt to new MMIO framework
Changelog v3 .. v4:
- specify accessor width
- use IRQ number accessor macro
- replace extract_bytes() with simple return
virt/kvm/arm/vgic/vgic-mmio-v2.c | 4 +-- virt/kvm/arm/vgic/vgic-mmio.c | 56 ++++++++++++++++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic-mmio.h | 11 ++++++++ 3 files changed, 69 insertions(+), 2 deletions(-)
virt/kvm/arm/vgic/vgic-mmio-v2.c +2 -2
@@ -72,10 +72,10 @@ static const struct vgic_register_region vgic_v2_dist_registers[] = { vgic_mmio_read_rao, vgic_mmio_write_wi, 1, VGIC_ACCESS_32bit),REGISTER_DESC_WITH_BITS_PER_IRQ (GIC_DIST_ENABLE_SET , - vgic_mmio_read_raz, vgic_mmio_write_wi,1 , + vgic_mmio_read_enable, vgic_mmio_write_senable,1 , VGIC_ACCESS_32bit),REGISTER_DESC_WITH_BITS_PER_IRQ (GIC_DIST_ENABLE_CLEAR , - vgic_mmio_read_raz, vgic_mmio_write_wi,1 , + vgic_mmio_read_enable, vgic_mmio_write_cenable,1 , VGIC_ACCESS_32bit), REGISTER_DESC_WITH_BITS_PER_IRQ(GIC_DIST_PENDING_SET, vgic_mmio_read_raz, vgic_mmio_write_wi, 1,
virt/kvm/arm/vgic/vgic-mmio.c +56 -0
62 lines not on this page · raw message
virt/kvm/arm/vgic/vgic-mmio.h +11 -0
@@ -84,6 +84,17 @@ unsigned long vgic_mmio_read_rao(struct kvm_vcpu *vcpu,void vgic_mmio_write_wi (struct kvm_vcpu *vcpu,gpa_t addr,unsigned int len,unsigned long val); +unsigned long vgic_mmio_read_enable (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len); + +void vgic_mmio_write_senable (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len, +unsigned long val); + +void vgic_mmio_write_cenable (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len, +unsigned long val); +unsigned int vgic_v2_init_dist_iodev (struct vgic_io_device *dev);#endif
Signature · 1 line
2.8.2
PATCH v4 52/56 KVM: arm/arm64: vgic-new: vgic_init: implement map_resources
In reply to Andre PrzywaraFrom: Eric Auger <[email protected]>
map_resources is the last initialization step. It is executed on first VCPU run. At that stage the code checks that userspace has provided the base addresses for the relevant VGIC regions, which depend on the type of VGIC that is exposed to the guest. Also we check if the two regions overlap.
If the checks succeeded, we register the respective register frames with the kvm_io_bus framework.
If we emulate a GICv2, the function also forces vgic_init execution if it has not been executed yet. Also we map the virtual GIC CPU interface onto the guest's CPU interface.
---
Changelog v3 .. v4:
- add overlap check (moved from kvm_vgic_addr())
- reword commit message
- improve comment
include/kvm/vgic/vgic.h | 1 + virt/kvm/arm/vgic/vgic-init.c | 28 +++++++++++++++++ virt/kvm/arm/vgic/vgic-v2.c | 69 +++++++++++++++++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic-v3.c | 71 +++++++++++++++++++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic.h | 7 +++++ 5 files changed, 176 insertions(+)
include/kvm/vgic/vgic.h +1 -0
@@ -201,6 +201,7 @@ int kvm_vgic_create(struct kvm *kvm, u32 type);void kvm_vgic_destroy (struct kvm *kvm);void kvm_vgic_vcpu_early_init (struct kvm_vcpu *vcpu);void kvm_vgic_vcpu_destroy (struct kvm_vcpu *vcpu); +int kvm_vgic_map_resources (struct kvm *kvm);int kvm_vgic_hyp_init (void ); int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int intid,
virt/kvm/arm/vgic/vgic-init.c +28 -0
@@ -323,6 +323,34 @@ int vgic_lazy_init(struct kvm *kvm)return ret; } +/* RESOURCE MAPPING */ + +/** +* Map the MMIO regions depending on the VGIC model exposed to the guest +* called on the first VCPU run. +* Also map the virtual CPU interface into the VM. +* v2/v3 derivatives call vgic_init if not already done. +* vgic_ready() returns true if this function has succeeded. +* @kvm: kvm struct pointer +*/ +int kvm_vgic_map_resources (struct kvm *kvm) +{ +struct vgic_dist *dist = &kvm->arch.vgic; +int ret =0 ; + +mutex_lock (&kvm->lock); +if (!irqchip_in_kernel (kvm)) + goto out; + +if (dist->vgic_model ==KVM_DEV_TYPE_ARM_VGIC_V2 ) + ret =vgic_v2_map_resources (kvm); +else + ret =vgic_v3_map_resources (kvm); +out: +mutex_unlock (&kvm->lock); +return ret; +} +/* GENERIC PROBE */ static void vgic_init_maintenance_interrupt (void *info)
virt/kvm/arm/vgic/vgic-v2.c +69 -0
75 lines not on this page · raw message
virt/kvm/arm/vgic/vgic-v3.c +71 -0
77 lines not on this page · raw message
virt/kvm/arm/vgic/vgic.h +7 -0
@@ -55,6 +55,7 @@ void vgic_v2_set_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr);void vgic_v2_get_vmcr (struct kvm_vcpu *vcpu,struct vgic_vmcr *vmcr);void vgic_v2_enable (struct kvm_vcpu *vcpu);int vgic_v2_probe (const struct gic_kvm_info *info); +int vgic_v2_map_resources (struct kvm *kvm);int vgic_register_dist_iodev (struct kvm *kvm,gpa_t dist_base_address,enum vgic_type );
@@ -68,6 +69,7 @@ void vgic_v3_set_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr);void vgic_v3_get_vmcr (struct kvm_vcpu *vcpu,struct vgic_vmcr *vmcr);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);#else static inline void vgic_v3_process_maintenance (struct kvm_vcpu *vcpu)
@@ -110,6 +112,11 @@ static inline int vgic_v3_probe(const struct gic_kvm_info *info)return -ENODEV ; } +static inline int vgic_v3_map_resources (struct kvm *kvm) +{ +return -ENODEV ; +} +static inline int vgic_register_redist_iodevs (struct kvm *kvm,gpa_t dist_base_address) {
Signature · 1 line
2.8.2
PATCH v4 56/56 KVM: arm/arm64: vgic-new: enable build
In reply to Andre PrzywaraNow that the new VGIC implementation has reached feature parity with the old one, add the new files to the build system and add a Kconfig option to switch between the two versions.
We set the default to the new version to get maximum test coverage, in case people experience problems they can switch back to the old behaviour if needed.
---
Changelog v1 .. v2:
- adjust the changed filenames in the Makefiles
- add vgic-mmio-v2.c and vgic-mmio-v3.c to the Makefiles
arch/arm/kvm/Kconfig | 7 +++++++ arch/arm/kvm/Makefile | 11 +++++++++++ arch/arm64/kvm/Kconfig | 7 +++++++ arch/arm64/kvm/Makefile | 12 ++++++++++++ virt/kvm/arm/hyp/vgic-v2-sr.c | 5 +++++ 5 files changed, 42 insertions(+)
arch/arm/kvm/Kconfig +7 -0
@@ -46,6 +46,13 @@ config KVM_ARM_HOST ---help--- Provides host support for ARM processors. +config KVM_NEW_VGIC + bool "New VGIC implementation" + depends on KVM + default y + ---help--- + uses the new VGIC implementation + source drivers/vhost/Kconfig endif # VIRTUALIZATION
arch/arm/kvm/Makefile +11 -0
@@ -21,7 +21,18 @@ obj-$(CONFIG_KVM_ARM_HOST) += hyp/ obj-y += kvm-arm.o init.o interrupts.o obj-y += arm.o handle_exit.o guest.o mmu.o emulate.o reset.o obj-y += coproc.o coproc_a15.o coproc_a7.o mmio.o psci.o perf.o + +ifeq ($(CONFIG_KVM_NEW_VGIC),y) +obj-y += $(KVM)/arm/vgic/vgic.o +obj-y += $(KVM)/arm/vgic/vgic-init.o +obj-y += $(KVM)/arm/vgic/vgic-irqfd.o +obj-y += $(KVM)/arm/vgic/vgic-v2.o +obj-y += $(KVM)/arm/vgic/vgic-mmio.o +obj-y += $(KVM)/arm/vgic/vgic-mmio-v2.o +obj-y += $(KVM)/arm/vgic/vgic-kvm-device.o +else obj-y += $(KVM)/arm/vgic.o obj-y += $(KVM)/arm/vgic-v2.o obj-y += $(KVM)/arm/vgic-v2-emul.o +endif obj-y += $(KVM)/arm/arch_timer.o
arch/arm64/kvm/Kconfig +7 -0
@@ -54,6 +54,13 @@ config KVM_ARM_PMU Adds support for a virtual Performance Monitoring Unit (PMU) in virtual machines. +config KVM_NEW_VGIC + bool "New VGIC implementation" + depends on KVM + default y + ---help--- + uses the new VGIC implementation + source drivers/vhost/Kconfig endif # VIRTUALIZATION
arch/arm64/kvm/Makefile +12 -0
@@ -20,10 +20,22 @@ kvm-$(CONFIG_KVM_ARM_HOST) += emulate.o inject_fault.o regmap.o kvm-$(CONFIG_KVM_ARM_HOST) += hyp.o hyp-init.o handle_exit.o kvm-$(CONFIG_KVM_ARM_HOST) += guest.o debug.o reset.o sys_regs.o sys_regs_generic_v8.o +ifeq ($(CONFIG_KVM_NEW_VGIC),y) +kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/vgic/vgic.o +kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/vgic/vgic-init.o +kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/vgic/vgic-irqfd.o +kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/vgic/vgic-v2.o +kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/vgic/vgic-v3.o +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 +else kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/vgic.o kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/vgic-v2.o kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/vgic-v2-emul.o kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/vgic-v3.o kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/vgic-v3-emul.o +endif kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/arch_timer.o kvm-$(CONFIG_KVM_ARM_PMU) += $(KVM)/arm/pmu.o
virt/kvm/arm/hyp/vgic-v2-sr.c +5 -0
@@ -21,7 +21,12 @@#include <asm/kvm_hyp.h> +#ifdef CONFIG_KVM_NEW_VGIC +extern struct vgic_global kvm_vgic_global_state; +#define vgic_v2_params kvm_vgic_global_state +#else extern struct vgic_params vgic_v2_params; +#endif static void __hyp_text save_maint_int_state(struct kvm_vcpu *vcpu,void __iomem *base)
Signature · 1 line
2.8.2
Quoted 3 lines · Andre Przywara
Hi,
a new update for the new VGIC series.
....
Oh dear,
apologies for the (double) spam!
Having the file name pattern _twice_ on the git send-email command line really results in having everything send out twice ;-)
As those series are actually identical, please ignore/delete this one (the mails sent out later).
Sorry for the inconvenience!
Andre.
Re: [PATCH v4 18/56] KVM: arm/arm64: vgic-new: Add IRQ sync/flush framework
In reply to Andre PrzywaraQuoted 206 lines · Andre Przywara
From: Marc Zyngier <[email protected]>
Implement the framework for syncing IRQs between our emulation and the list registers, which represent the guest's view of IRQs. This is done in
kvm_vgic_flush_hwstateandkvm_vgic_sync_hwstate, which gets called on guest entry and exit.
The code talking to the actual GICv2/v3 hardware is added in the following patches.Signed-off-by Marc Zyngier <[email protected]>Signed-off-by Christoffer Dall <[email protected]>Signed-off-by Eric Auger <[email protected]>Signed-off-by Andre Przywara <[email protected]>Reviewed-by Eric Auger <[email protected]>Reviewed-by Christoffer Dall <[email protected]>---
Changelog RFC..v1:
- split outvgic_clear_lr()fromvgic_populate_lr()
- renamevgic_populate_lrs()tovgic_flush_lr_state()
- clean all LRs when the distributor is disabled
- uselist_del()instead oflist_del_init()
- add comments to explain the direction of sync/flush_hwstate
- remove unneeded BUG_ON(in_interrupt()Changelog v2 .. v3:
- remove bogus v2 specific rebase leftoversChangelog v3 .. v4:
- amend locks requirementsinclude/kvm/vgic/vgic.h | 4 + virt/kvm/arm/vgic/vgic.c | 191 +++++++++++++++++++++++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic.h | 2 + 3 files changed, 197 insertions(+)include/kvm/vgic/vgic.h +4
-0@@ -190,6 +190,10 @@ int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int intid,#define vgic_valid_spi (k, i) (((i) >= VGIC_NR_PRIVATE_IRQS) && \ ((i) < (k)->arch.vgic.nr_spis + VGIC_NR_PRIVATE_IRQS)) +bool kvm_vcpu_has_pending_irqs (struct kvm_vcpu *vcpu); +void kvm_vgic_sync_hwstate (struct kvm_vcpu *vcpu); +void kvm_vgic_flush_hwstate (struct kvm_vcpu *vcpu); +/** * kvm_vgic_get_max_vcpus - Get the maximum number of VCPUs allowed by HW * virt/kvm/arm/vgic/vgic.c +148
-0@@ -307,3 +307,194 @@ int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int intid, {return vgic_update_irq_pending (kvm, cpuid, intid, level, false); } + +/** +* vgic_prune_ap_list - Remove non-relevant interrupts from the list +* +* @vcpu: The VCPU pointer +* +* Go over the list of "interesting" interrupts, and prune those that we +* won't have to consider in the near future. +*/ +static void vgic_prune_ap_list (struct kvm_vcpu *vcpu) +{ +struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; +struct vgic_irq *irq, *tmp; + +retry: +spin_lock (&vgic_cpu->ap_list_lock); + +list_for_each_entry_safe (irq, tmp, &vgic_cpu->ap_list_head, ap_list) { +struct kvm_vcpu *target_vcpu, *vcpuA, *vcpuB; + +spin_lock (&irq->irq_lock); + +BUG_ON (vcpu != irq->vcpu); + + target_vcpu =vgic_target_oracle (irq); + +if (!target_vcpu) { +/* +* We don't need to process this interrupt any +* further, move it off the list. +*/ +list_del (&irq->ap_list); + irq->vcpu =NULL ; +spin_unlock (&irq->irq_lock); +continue ; + } + +if (target_vcpu == vcpu) { +/* We're on the right CPU */ +spin_unlock (&irq->irq_lock); +continue ; + } + +/* This interrupt looks like it has to be migrated. */ + +spin_unlock (&irq->irq_lock); +spin_unlock (&vgic_cpu->ap_list_lock); + +/* +* Ensure locking order by always locking the smallest +* ID first. +*/ +if (vcpu->vcpu_id < target_vcpu->vcpu_id) { + vcpuA = vcpu; + vcpuB = target_vcpu; + }else { + vcpuA = target_vcpu; + vcpuB = vcpu; + } + +spin_lock (&vcpuA->arch.vgic_cpu.ap_list_lock); +spin_lock (&vcpuB->arch.vgic_cpu.ap_list_lock); +spin_lock (&irq->irq_lock); + +/* +* If the affinity has been preserved, move the +* interrupt around. Otherwise, it means things have +* changed while the interrupt was unlocked, and we +* need to replay this. +* +* In all cases, we cannot trust the list not to have +* changed, so we restart from the beginning. +*/ +if (target_vcpu ==vgic_target_oracle (irq)) { +struct vgic_cpu *new_cpu = &target_vcpu->arch.vgic_cpu; + +list_del (&irq->ap_list); + irq->vcpu = target_vcpu; +list_add_tail (&irq->ap_list, &new_cpu->ap_list_head); + } + +spin_unlock (&irq->irq_lock); +spin_unlock (&vcpuB->arch.vgic_cpu.ap_list_lock); +spin_unlock (&vcpuA->arch.vgic_cpu.ap_list_lock); + goto retry; + } + +spin_unlock (&vgic_cpu->ap_list_lock); +} + +static inline void vgic_process_maintenance_interrupt (struct kvm_vcpu *vcpu) +{ +} + +static inline void vgic_fold_lr_state (struct kvm_vcpu *vcpu) +{ +} + +/* Requires the irq_lock to be held. */ +static inline void vgic_populate_lr (struct kvm_vcpu *vcpu, +struct vgic_irq *irq,int lr) +{ +DEBUG_SPINLOCK_BUG_ON (!spin_is_locked (&irq->irq_lock)); +} + +static inline void vgic_clear_lr (struct kvm_vcpu *vcpu,int lr) +{ +} + +static inline void vgic_set_underflow (struct kvm_vcpu *vcpu) +{ +} + +/* Requires the ap_list_lock to be held. */ +static int compute_ap_list_depth (struct kvm_vcpu *vcpu) +{ +struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; +struct vgic_irq *irq; +int count =0 ; + +DEBUG_SPINLOCK_BUG_ON (!spin_is_locked (&vgic_cpu->ap_list_lock)); + +list_for_each_entry (irq, &vgic_cpu->ap_list_head, ap_list) { +spin_lock (&irq->irq_lock); +/* GICv2 SGIs can count for more than one... */ +if (vgic_irq_is_sgi (irq->intid) && irq->source) + count +=hweight8 (irq->source); +else + count++; +spin_unlock (&irq->irq_lock); + } +return count; +} + +/* Requires the VCPU's ap_list_lock to be held. */ +static void vgic_flush_lr_state (struct kvm_vcpu *vcpu) +{ +struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; +struct vgic_irq *irq; +int count =0 ; + +DEBUG_SPINLOCK_BUG_ON (!spin_is_locked (&vgic_cpu->ap_list_lock)); + +if (compute_ap_list_depth (vcpu) > kvm_vgic_global_state.nr_lr) { +vgic_set_underflow (vcpu); +vgic_sort_ap_list (vcpu); + } +
virt/kvm/arm/vgic/vgic.c int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int intid, · line 467
Quoted 10 lines
virt/kvm/arm/vgic/vgic.c +10
-0+list_for_each_entry (irq, &vgic_cpu->ap_list_head, ap_list) { +spin_lock (&irq->irq_lock); + +if (unlikely (vgic_target_oracle (irq) != vcpu)) + goto next; + +/* +* If we get an SGI with multiple sources, try to get +* them in all at once. +*/
Nit: this comment is out of place, and is a duplicate of the one in compute_ap_list_depth().
Quoted 39 lines
virt/kvm/arm/vgic/vgic.c +33
-0+do { +vgic_populate_lr (vcpu, irq, count++); + }while (irq->source && count < kvm_vgic_global_state.nr_lr); + +next: +spin_unlock (&irq->irq_lock); + +if (count == kvm_vgic_global_state.nr_lr) +break ; + } + + vcpu->arch.vgic_cpu.used_lrs = count; + +/* Nuke remaining LRs */ +for ( ; count < kvm_vgic_global_state.nr_lr; count++) +vgic_clear_lr (vcpu, count); +} + +/* Sync back the hardware VGIC state into our emulation after a guest's run. */ +void kvm_vgic_sync_hwstate (struct kvm_vcpu *vcpu) +{ +vgic_process_maintenance_interrupt (vcpu); +vgic_fold_lr_state (vcpu); +vgic_prune_ap_list (vcpu); +} + +/* Flush our emulation state into the GIC hardware before entering the guest. */ +void kvm_vgic_flush_hwstate (struct kvm_vcpu *vcpu) +{ +spin_lock (&vcpu->arch.vgic_cpu.ap_list_lock); +vgic_flush_lr_state (vcpu); +spin_unlock (&vcpu->arch.vgic_cpu.ap_list_lock); +}virt/kvm/arm/vgic/vgic.h +0
-0
virt/kvm/arm/vgic/vgic.h line 23
Quoted 8 lines
virt/kvm/arm/vgic/vgic.h +2
-0@@ -16,6 +16,8 @@#ifndef __KVM_ARM_VGIC_NEW_H__#define __KVM_ARM_VGIC_NEW_H__ +#define vgic_irq_is_sgi (intid) ((intid) < VGIC_NR_SGIS) +struct vgic_irq *vgic_get_irq (struct kvm *kvm,struct kvm_vcpu *vcpu,u32 intid);bool vgic_queue_irq_unlock (struct kvm *kvm,struct vgic_irq *irq);
Thanks,
M.
Signature · 1 line
Jazz is not dead. It just smells funny...
Re: [PATCH v4 22/56] KVM: arm/arm64: vgic-new: Add MMIO handling framework
In reply to Andre PrzywaraQuoted 307 lines · Andre Przywara
From: Marc Zyngier <[email protected]>
Add an MMIO handling framework to the VGIC emulation:
Each register is described by its offset, size (or number of bits per IRQ, if applicable) and the read/write handler functions. We provide initialization macros to describe each GIC register later easily.Separate dispatch functions for read and write accesses are connected to the
kvm_io_busframework and binary-search for the responsible register handler based on the offset address within the region. We convert the incoming data (referenced by a pointer) to the host's endianess and use pass-by-value to hand the data over to the actual handler functions.The register handler prototype and the endianess conversion are courtesy of Christoffer Dall.
Signed-off-by Marc Zyngier <[email protected]>Signed-off-by Christoffer Dall <[email protected]>Signed-off-by Andre Przywara <[email protected]>---
Changelog RFC..v1:
- rework MMIO dispatching to use only onekvm_io_busdevice
- document purpose of register region macros
- rename "this" parameter to "dev"
- change IGROUPR to be RAO (returning 1 => Group1 IRQs)Changelog v1 .. v2:
* MASSIVE rework:
- storeregister_regionpointer inkvm_io_buslinked struct
- replacewrite_mask_xxxfunctions withextract_bytes()implementation
- change handler functions' prototypes to take and return unsigned long
- use binary search to find matching register handler
- convert endianess of input data indispatch_mmio_xxxfunctions
- improve readability of register initializer macros
- remove any GICv2/GICv3 specific functions fromvgic-mmio.c
- rename file fromvgic_mmio.ctovgic-mmio.cChangelog v2 .. v3:
- replace inclusion ofvgic/vgic.hwitharm_vgic.hChangelog v3 .. v4:
- add IRQ number accessor macro
- check access width in dispatcher
- treat non-covered MMIO addresses as RAZ/WI
- removeextract_bytes()(re-introduced as static later in the series)include/kvm/vgic/vgic.h | 13 +++ virt/kvm/arm/vgic/vgic-mmio.c | 184 ++++++++++++++++++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic-mmio.h | 87 ++++++++++++++++++++ 3 files changed, 284 insertions(+) create mode 100644 virt/kvm/arm/vgic/vgic-mmio.c create mode 100644 virt/kvm/arm/vgic/vgic-mmio.hinclude/kvm/vgic/vgic.h +13
-0@@ -106,6 +106,16 @@ struct vgic_irq {enum vgic_irq_config config;/* Level or edge */ }; +struct vgic_register_region ; + +struct vgic_io_device { +gpa_t base_addr; +struct kvm_vcpu *redist_vcpu; +const struct vgic_register_region *regions; +int nr_regions; +struct kvm_io_device dev; +}; +struct vgic_dist {bool in_kernel;bool ready;@@ -132,6 +142,9 @@ struct vgic_dist {bool enabled;struct vgic_irq *spis; + +struct vgic_io_device dist_iodev; +struct vgic_io_device *redist_iodevs; };struct vgic_v2_cpu_if {virt/kvm/arm/vgic/vgic-mmio.c new file +184
-0@@ -0,0 +1,184 @@ +/* +* VGIC MMIO handling functions +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +*/ + +#include <linux/bitops.h> +#include <linux/bsearch.h> +#include <linux/kvm.h> +#include <linux/kvm_host.h> +#include <kvm/iodev.h> +#include <kvm/arm_vgic.h> + +#include "vgic.h" +#include "vgic-mmio.h" + +unsigned long vgic_mmio_read_raz (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len) +{ +return 0 ; +} + +unsigned long vgic_mmio_read_rao (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len) +{ +return -1UL ; +} + +void vgic_mmio_write_wi (struct kvm_vcpu *vcpu,gpa_t addr, +unsigned int len,unsigned long val) +{ +/* Ignore */ +} + +static int match_region (const void *key,const void *elt) +{ +const unsigned int offset = (unsigned long )key; +const struct vgic_register_region *region = elt; + +if (offset < region->reg_offset) +return -1 ; + +if (offset >= region->reg_offset + region->len) +return 1 ; + +return 0 ; +} + +/* Find the proper register handler entry given a certain address offset. */ +static const struct vgic_register_region * +vgic_find_mmio_region (const struct vgic_register_region *region,int nr_regions, +unsigned int offset) +{ +return bsearch ((void *)(uintptr_t )offset, region, nr_regions, +sizeof (region[0 ]), match_region); +} + +/* +* kvm_mmio_read_buf() returns a value in a format where it can be converted +* to a byte array and be directly observed as the guest wanted it to appear +* in memory if it had done the store itself, which is LE for the GIC, as the +* guest knows the GIC is always LE. +* +* We convert this value to the CPUs native format to deal with it as a data +* value. +*/ +unsigned long vgic_data_mmio_bus_to_host (const void *val,unsigned int len) +{ +unsigned long data =kvm_mmio_read_buf (val, len); + +switch (len) { +case 1 : +return data; +case 2 : +return le16_to_cpu (data); +case 4 : +return le32_to_cpu (data); +default : +return le64_to_cpu (data); + } +} + +/* +* kvm_mmio_write_buf() expects a value in a format such that if converted to +* a byte array it is observed as the guest would see it if it could perform +* the load directly. Since the GIC is LE, and the guest knows this, the +* guest expects a value in little endian format. +* +* We convert the data value from the CPUs native format to LE so that the +* value is returned in the proper format. +*/ +void vgic_data_host_to_mmio_bus (void *buf,unsigned int len, +unsigned long data) +{ +switch (len) { +case 1 : +break ; +case 2 : + data =cpu_to_le16 (data); +break ; +case 4 : + data =cpu_to_le32 (data); +break ; +default : + data =cpu_to_le64 (data); + } + +kvm_mmio_write_buf (buf, len, data); +} + +static +struct vgic_io_device *kvm_to_vgic_iodev (const struct kvm_io_device *dev) +{ + return container_of(dev, struct vgic_io_device, dev); +} + +static bool check_region (const struct vgic_register_region *region, +gpa_t addr,int len) +{ +if ((region->access_flags & VGIC_ACCESS_8bit) && len ==1 ) +return true; +if ((region->access_flags & VGIC_ACCESS_32bit) && + len ==sizeof (u32) && !(addr &3 )) +return true; +if ((region->access_flags & VGIC_ACCESS_64bit) && + len ==sizeof (u64) && !(addr &7 )) +return true; + +return false; +} + +static int dispatch_mmio_read (struct kvm_vcpu *vcpu,struct kvm_io_device *dev, +gpa_t addr,int len,void *val) +{ +struct vgic_io_device *iodev =kvm_to_vgic_iodev (dev); +const struct vgic_register_region *region; +struct kvm_vcpu *r_vcpu; +unsigned long data; + + region =vgic_find_mmio_region (iodev->regions, iodev->nr_regions, + addr - iodev->base_addr); +if (!region || !check_region (region, addr, len)) { +memset (val,0 , len); +return 0 ; + } + + r_vcpu = iodev->redist_vcpu ? iodev->redist_vcpu : vcpu; + data = region->read (r_vcpu, addr, len); +vgic_data_host_to_mmio_bus (val, len, data); +return 0 ; +} + +static int dispatch_mmio_write (struct kvm_vcpu *vcpu,struct kvm_io_device *dev, +gpa_t addr,int len,const void *val) +{ +struct vgic_io_device *iodev =kvm_to_vgic_iodev (dev); +const struct vgic_register_region *region; +struct kvm_vcpu *r_vcpu; +unsigned long data =vgic_data_mmio_bus_to_host (val, len); + + region =vgic_find_mmio_region (iodev->regions, iodev->nr_regions, + addr - iodev->base_addr); +if (!region) +return 0 ; + +if (!check_region (region, addr, len)) +return 0 ; + + r_vcpu = iodev->redist_vcpu ? iodev->redist_vcpu : vcpu; + region->write (r_vcpu, addr, len, data); +return 0 ; +} + +struct kvm_io_device_ops kvm_io_gic_ops = { + .read = dispatch_mmio_read, + .write = dispatch_mmio_write, +};virt/kvm/arm/vgic/vgic-mmio.h new file +26
-0@@ -0,0 +1,87 @@ +/* +* Copyright (C) 2015, 2016 ARM Ltd. +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ +#ifndef __KVM_ARM_VGIC_MMIO_H__ +#define __KVM_ARM_VGIC_MMIO_H__ + +struct vgic_register_region { + unsigned int reg_offset; + unsigned int len; + unsigned int bits_per_irq; + unsigned int access_flags; + unsigned long (*read)(struct kvm_vcpu *vcpu, gpa_t addr, + unsigned int len); + void (*write)(struct kvm_vcpu *vcpu, gpa_t addr, unsigned int len,
virt/kvm/arm/vgic/vgic-mmio.h line 36
Quoted 10 lines
virt/kvm/arm/vgic/vgic-mmio.h new file +10
-0+ unsigned long val); +}; + +extern struct kvm_io_device_ops kvm_io_gic_ops; + +#define VGIC_ACCESS_8bit 1 +#define VGIC_ACCESS_32bit 2 +#define VGIC_ACCESS_64bit 4 + +/* generate a mask that covers 1024 interrupts with <b> bits per IRQ */
Hmmm. I'd appreciate some additional comments, specially when it comes to the various restrictions. May I'd suggest something like:
/*
* Generate a mask that covers the number of bytes required to address
* up to 1024 interrupts, each represented by <b> bits. This assumes
* that <b> is a power of two.
*
* ilog2(b) + ilog2(1024) is the number of bits required to bit-address
* 1024 interrupts, each represented by b bits. Minus ilog2(8) converts
* this to a byte address.
*/
virt/kvm/arm/vgic/vgic-mmio.h line 38
Quoted 2 lines
virt/kvm/arm/vgic/vgic-mmio.h new file +2
-0+#define VGIC_ADDR_IRQ_MASK (b) GENMASK_ULL(ilog2(b) + ilog2(1024) - \ + ilog2(BITS_PER_BYTE) - 1, 0)
/*
* Convert a base address into a base interrupt (each interrupt
* represented by <bits> bits. This assumes that <bits> is a power
* of two, that <addr> both part of a memory region aligned on a
* <b> bits boundary, and itself aligned on that same boundary
* (for regions that describe an interrupt with more than a single
* byte of data).
*/
Quoted 40 lines
virt/kvm/arm/vgic/vgic-mmio.h new file +39
-0+#define VGIC_ADDR_TO_INTID (addr, bits) (((addr) & VGIC_ADDR_IRQ_MASK(bits)) * \ + 64 / (bits) / 8) + +/* +* Some VGIC registers store per-IRQ information, with a different number +* of bits per IRQ. For those registers this macro is used. +* The _WITH_LENGTH version instantiates registers with a fixed length +* and is mutually exclusive with the _PER_IRQ version. +*/ +#define REGISTER_DESC_WITH_BITS_PER_IRQ (off, rd, wr, bpi, acc) \ + { \ + .reg_offset = off, \ + .bits_per_irq = bpi, \ + .len = bpi * 1024 / 8, \ + .access_flags = acc, \ + .read = rd, \ + .write = wr, \ + } + +#define REGISTER_DESC_WITH_LENGTH (off, rd, wr, length, acc) \ + { \ + .reg_offset = off, \ + .bits_per_irq = 0, \ + .len = length, \ + .access_flags = acc, \ + .read = rd, \ + .write = wr, \ + } + +int kvm_vgic_register_mmio_region (struct kvm *kvm,struct kvm_vcpu *vcpu, +struct vgic_register_region *reg_desc, +struct vgic_io_device *region, +int nr_irqs,bool offset_private); + +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); +
virt/kvm/arm/vgic/vgic-mmio.h line 87
Quoted 10 lines
virt/kvm/arm/vgic/vgic-mmio.h new file +10
-0+unsigned long vgic_mmio_read_raz (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len); + +unsigned long vgic_mmio_read_rao (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len); + +void vgic_mmio_write_wi (struct kvm_vcpu *vcpu,gpa_t addr, +unsigned int len,unsigned long val); + +#endif
Thanks,
M.
Signature · 1 line
Jazz is not dead. It just smells funny...
Re: [PATCH v4 49/56] KVM: arm/arm64: vgic-new: vgic_init: implement kvm_vgic_hyp_init
In reply to Andre PrzywaraHi Andre,
virt/kvm/arm/vgic/vgic-v2.c void vgic_v2_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcrp) · line 211
Quoted 4 lines · Andre Przywara
virt/kvm/arm/vgic/vgic-v2.c +4
-0+ +/** +* vgic_v2_probe - probe for a GICv2 compatible interrupt controller in DT +* @node: pointer to the DT node
This needs to be updated to match the parameters of vgic_v2_probe.
Quoted 4 lines
+ *
+ * Returns 0 if a GICv2 has been found, returns an error code otherwise
+ */
+intvgic_v2_probe(const struct gic_kvm_info *info)
[...]
Quoted 15 lines
virt/kvm/arm/vgic/vgic-v3.c +3
-0@@ -15,6 +15,9 @@#include <linux/irqchip/arm-gic-v3.h> #include <linux/kvm.h> #include <linux/kvm_host.h> +#include <kvm/arm_vgic.h> +#include <asm/kvm_mmu.h> +#include <asm/kvm_asm.h> #include "vgic.h"
virt/kvm/arm/vgic/vgic-v3.c void vgic_v3_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcrp) · line 191
Quoted 7 lines
virt/kvm/arm/vgic/vgic-v3.c +4
-0@@ -182,3 +185,49 @@ void vgic_v3_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcrp) vmcrp->bpr = (vmcr &ICH_VMCR_BPR0_MASK ) >>ICH_VMCR_BPR0_SHIFT ; vmcrp->pmr = (vmcr &ICH_VMCR_PMR_MASK ) >>ICH_VMCR_PMR_SHIFT ; } + +/** +* vgic_v3_probe - probe for a GICv3 compatible interrupt controller in DT +* @node: pointer to the DT node
This needs to be updated to match the parameters of vgic_v3_probe.
virt/kvm/arm/vgic/vgic-v3.c void vgic_v3_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcrp) · line 195
Quoted 4 lines
virt/kvm/arm/vgic/vgic-v3.c +4
-0+* +* Returns 0 if a GICv3 has been found, returns an error code otherwise +*/ +int vgic_v3_probe (const struct gic_kvm_info *info)
Regards,
Signature · 1 line
Julien Grall
Re: [PATCH v4 11/56] KVM: arm/arm64: pmu: abstract access to number of SPIs
In reply to Andre PrzywaraQuoted 88 lines · Andre Przywara
Currently the PMU uses a member of the struct
vgic_distdirectly, which not only breaks abstraction, but will fail with the new VGIC. Abstract this access in the VGIC header file and refactor the validity check in the PMU code.Signed-off-by Andre Przywara <[email protected]>---
Changelog v3 .. v4:
- fix logic to allow PPIs again
- refactor IRQ validity checkinclude/kvm/arm_vgic.h | 2 ++ virt/kvm/arm/pmu.c | 25 ++++++++++++++----------- 2 files changed, 16 insertions(+), 11 deletions(-)include/kvm/arm_vgic.h +2
-0@@ -348,6 +348,8 @@ bool kvm_vgic_map_is_active(struct kvm_vcpu *vcpu, unsigned int virt_irq);#define irqchip_in_kernel (k) (!!((k)->arch.vgic.in_kernel))#define vgic_initialized (k) (!!((k)->arch.vgic.nr_cpus))#define vgic_ready (k) ((k)->arch.vgic.ready) +#define vgic_valid_spi (k, i) (((i) >= VGIC_NR_PRIVATE_IRQS) && \ + ((i) < (k)->arch.vgic.nr_irqs)) int vgic_v2_probe(const struct gic_kvm_info *gic_kvm_info, const struct vgic_ops **ops,virt/kvm/arm/pmu.c +14
-11@@ -436,7 +436,14 @@ static int kvm_arm_pmu_v3_init(struct kvm_vcpu *vcpu)return 0 ; } -static bool irq_is_valid (struct kvm *kvm,int irq,bool is_ppi) +#define irq_is_ppi (irq) ((irq) >= VGIC_NR_SGIS && (irq) < VGIC_NR_PRIVATE_IRQS) + +/* +* For one VM the interrupt type must be same for each vcpu. +* As a PPI, the interrupt number is the same for all vcpus, +* while as an SPI it must be a separate number per vcpu. +*/ +static bool pmu_irq_is_valid (struct kvm *kvm,int irq) {int i;struct kvm_vcpu *vcpu;@@ -445,7 +452,7 @@ static bool irq_is_valid(struct kvm *kvm, int irq, bool is_ppi)if (!kvm_arm_pmu_irq_initialized (vcpu))continue ; -if (is_ppi) { +if (irq_is_ppi (irq)) {if (vcpu->arch.pmu.irq_num != irq)return false; }else {@@ -457,7 +464,6 @@ static bool irq_is_valid(struct kvm *kvm, int irq, bool is_ppi)return true; } -int kvm_arm_pmu_v3_set_attr (struct kvm_vcpu *vcpu,struct kvm_device_attr *attr) {switch (attr->attr) {@@ -471,14 +477,11 @@ int kvm_arm_pmu_v3_set_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)if (get_user (irq, uaddr))return -EFAULT ; -/* -* The PMU overflow interrupt could be a PPI or SPI, but for one -* VM the interrupt type must be same for each vcpu. As a PPI, -* the interrupt number is the same for all vcpus, while as an -* SPI it must be a separate number per vcpu. -*/ -if (irq <VGIC_NR_SGIS || irq >= vcpu->kvm->arch.vgic.nr_irqs || - !irq_is_valid (vcpu->kvm, irq, irq <VGIC_NR_PRIVATE_IRQS )) +/* The PMU overflow interrupt can be a PPI or a valid SPI. */ +if (!(irq_is_ppi (irq) ||vgic_valid_spi (vcpu->kvm, irq))) +return -EINVAL ; + +if (!pmu_irq_is_valid (vcpu->kvm, irq))return -EINVAL ;if (kvm_arm_pmu_irq_initialized (vcpu))Signature · 1 line
2.8.2
Re: [PATCH v4 17/56] KVM: arm/arm64: vgic-new: Add IRQ sorting
In reply to Andre PrzywaraQuoted 9 lines · Andre Przywara
From: Christoffer Dall <[email protected]>
Adds the sorting function to cover the case where you have more IRQs to consider than you have LRs. We now consider priorities.
Signed-off-by Christoffer Dall <[email protected]>Signed-off-by Andre Przywara <[email protected]>Reviewed-by Eric Auger <[email protected]>Reviewed-by Marc Zyngier <[email protected]>
Re: [PATCH v4 22/56] KVM: arm/arm64: vgic-new: Add MMIO handling framework
In reply to Marc ZyngierQuoted 322 lines · Marc Zyngier
Quoted 307 lines · Andre Przywara
From: Marc Zyngier <[email protected]>
Add an MMIO handling framework to the VGIC emulation:
Each register is described by its offset, size (or number of bits per IRQ, if applicable) and the read/write handler functions. We provide initialization macros to describe each GIC register later easily.Separate dispatch functions for read and write accesses are connected to the
kvm_io_busframework and binary-search for the responsible register handler based on the offset address within the region. We convert the incoming data (referenced by a pointer) to the host's endianess and use pass-by-value to hand the data over to the actual handler functions.The register handler prototype and the endianess conversion are courtesy of Christoffer Dall.
Signed-off-by Marc Zyngier <[email protected]>Signed-off-by Christoffer Dall <[email protected]>Signed-off-by Andre Przywara <[email protected]>---
Changelog RFC..v1:
- rework MMIO dispatching to use only onekvm_io_busdevice
- document purpose of register region macros
- rename "this" parameter to "dev"
- change IGROUPR to be RAO (returning 1 => Group1 IRQs)Changelog v1 .. v2:
* MASSIVE rework:
- storeregister_regionpointer inkvm_io_buslinked struct
- replacewrite_mask_xxxfunctions withextract_bytes()implementation
- change handler functions' prototypes to take and return unsigned long
- use binary search to find matching register handler
- convert endianess of input data indispatch_mmio_xxxfunctions
- improve readability of register initializer macros
- remove any GICv2/GICv3 specific functions fromvgic-mmio.c
- rename file fromvgic_mmio.ctovgic-mmio.cChangelog v2 .. v3:
- replace inclusion ofvgic/vgic.hwitharm_vgic.hChangelog v3 .. v4:
- add IRQ number accessor macro
- check access width in dispatcher
- treat non-covered MMIO addresses as RAZ/WI
- removeextract_bytes()(re-introduced as static later in the series)include/kvm/vgic/vgic.h | 13 +++ virt/kvm/arm/vgic/vgic-mmio.c | 184 ++++++++++++++++++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic-mmio.h | 87 ++++++++++++++++++++ 3 files changed, 284 insertions(+) create mode 100644 virt/kvm/arm/vgic/vgic-mmio.c create mode 100644 virt/kvm/arm/vgic/vgic-mmio.hinclude/kvm/vgic/vgic.h +13
-0@@ -106,6 +106,16 @@ struct vgic_irq {enum vgic_irq_config config;/* Level or edge */ }; +struct vgic_register_region ; + +struct vgic_io_device { +gpa_t base_addr; +struct kvm_vcpu *redist_vcpu; +const struct vgic_register_region *regions; +int nr_regions; +struct kvm_io_device dev; +}; +struct vgic_dist {bool in_kernel;bool ready;@@ -132,6 +142,9 @@ struct vgic_dist {bool enabled;struct vgic_irq *spis; + +struct vgic_io_device dist_iodev; +struct vgic_io_device *redist_iodevs; };struct vgic_v2_cpu_if {virt/kvm/arm/vgic/vgic-mmio.c new file +184
-0@@ -0,0 +1,184 @@ +/* +* VGIC MMIO handling functions +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +*/ + +#include <linux/bitops.h> +#include <linux/bsearch.h> +#include <linux/kvm.h> +#include <linux/kvm_host.h> +#include <kvm/iodev.h> +#include <kvm/arm_vgic.h> + +#include "vgic.h" +#include "vgic-mmio.h" + +unsigned long vgic_mmio_read_raz (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len) +{ +return 0 ; +} + +unsigned long vgic_mmio_read_rao (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len) +{ +return -1UL ; +} + +void vgic_mmio_write_wi (struct kvm_vcpu *vcpu,gpa_t addr, +unsigned int len,unsigned long val) +{ +/* Ignore */ +} + +static int match_region (const void *key,const void *elt) +{ +const unsigned int offset = (unsigned long )key; +const struct vgic_register_region *region = elt; + +if (offset < region->reg_offset) +return -1 ; + +if (offset >= region->reg_offset + region->len) +return 1 ; + +return 0 ; +} + +/* Find the proper register handler entry given a certain address offset. */ +static const struct vgic_register_region * +vgic_find_mmio_region (const struct vgic_register_region *region,int nr_regions, +unsigned int offset) +{ +return bsearch ((void *)(uintptr_t )offset, region, nr_regions, +sizeof (region[0 ]), match_region); +} + +/* +* kvm_mmio_read_buf() returns a value in a format where it can be converted +* to a byte array and be directly observed as the guest wanted it to appear +* in memory if it had done the store itself, which is LE for the GIC, as the +* guest knows the GIC is always LE. +* +* We convert this value to the CPUs native format to deal with it as a data +* value. +*/ +unsigned long vgic_data_mmio_bus_to_host (const void *val,unsigned int len) +{ +unsigned long data =kvm_mmio_read_buf (val, len); + +switch (len) { +case 1 : +return data; +case 2 : +return le16_to_cpu (data); +case 4 : +return le32_to_cpu (data); +default : +return le64_to_cpu (data); + } +} + +/* +* kvm_mmio_write_buf() expects a value in a format such that if converted to +* a byte array it is observed as the guest would see it if it could perform +* the load directly. Since the GIC is LE, and the guest knows this, the +* guest expects a value in little endian format. +* +* We convert the data value from the CPUs native format to LE so that the +* value is returned in the proper format. +*/ +void vgic_data_host_to_mmio_bus (void *buf,unsigned int len, +unsigned long data) +{ +switch (len) { +case 1 : +break ; +case 2 : + data =cpu_to_le16 (data); +break ; +case 4 : + data =cpu_to_le32 (data); +break ; +default : + data =cpu_to_le64 (data); + } + +kvm_mmio_write_buf (buf, len, data); +} + +static +struct vgic_io_device *kvm_to_vgic_iodev (const struct kvm_io_device *dev) +{ + return container_of(dev, struct vgic_io_device, dev); +} + +static bool check_region (const struct vgic_register_region *region, +gpa_t addr,int len) +{ +if ((region->access_flags & VGIC_ACCESS_8bit) && len ==1 ) +return true; +if ((region->access_flags & VGIC_ACCESS_32bit) && + len ==sizeof (u32) && !(addr &3 )) +return true; +if ((region->access_flags & VGIC_ACCESS_64bit) && + len ==sizeof (u64) && !(addr &7 )) +return true; + +return false; +} + +static int dispatch_mmio_read (struct kvm_vcpu *vcpu,struct kvm_io_device *dev, +gpa_t addr,int len,void *val) +{ +struct vgic_io_device *iodev =kvm_to_vgic_iodev (dev); +const struct vgic_register_region *region; +struct kvm_vcpu *r_vcpu; +unsigned long data; + + region =vgic_find_mmio_region (iodev->regions, iodev->nr_regions, + addr - iodev->base_addr); +if (!region || !check_region (region, addr, len)) { +memset (val,0 , len); +return 0 ; + } + + r_vcpu = iodev->redist_vcpu ? iodev->redist_vcpu : vcpu; + data = region->read (r_vcpu, addr, len); +vgic_data_host_to_mmio_bus (val, len, data); +return 0 ; +} + +static int dispatch_mmio_write (struct kvm_vcpu *vcpu,struct kvm_io_device *dev, +gpa_t addr,int len,const void *val) +{ +struct vgic_io_device *iodev =kvm_to_vgic_iodev (dev); +const struct vgic_register_region *region; +struct kvm_vcpu *r_vcpu; +unsigned long data =vgic_data_mmio_bus_to_host (val, len); + + region =vgic_find_mmio_region (iodev->regions, iodev->nr_regions, + addr - iodev->base_addr); +if (!region) +return 0 ; + +if (!check_region (region, addr, len)) +return 0 ; + + r_vcpu = iodev->redist_vcpu ? iodev->redist_vcpu : vcpu; + region->write (r_vcpu, addr, len, data); +return 0 ; +} + +struct kvm_io_device_ops kvm_io_gic_ops = { + .read = dispatch_mmio_read, + .write = dispatch_mmio_write, +};virt/kvm/arm/vgic/vgic-mmio.h new file +26
-0@@ -0,0 +1,87 @@ +/* +* Copyright (C) 2015, 2016 ARM Ltd. +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ +#ifndef __KVM_ARM_VGIC_MMIO_H__ +#define __KVM_ARM_VGIC_MMIO_H__ + +struct vgic_register_region { + unsigned int reg_offset; + unsigned int len; + unsigned int bits_per_irq; + unsigned int access_flags; + unsigned long (*read)(struct kvm_vcpu *vcpu, gpa_t addr, + unsigned int len); + void (*write)(struct kvm_vcpu *vcpu, gpa_t addr, unsigned int len,virt/kvm/arm/vgic/vgic-mmio.h line 36
Quoted 10 lines
virt/kvm/arm/vgic/vgic-mmio.h new file +10
-0+ unsigned long val); +}; + +extern struct kvm_io_device_ops kvm_io_gic_ops; + +#define VGIC_ACCESS_8bit 1 +#define VGIC_ACCESS_32bit 2 +#define VGIC_ACCESS_64bit 4 + +/* generate a mask that covers 1024 interrupts with <b> bits per IRQ */ Hmmm. I'd appreciate some additional comments, specially when it comes to the various restrictions. May I'd suggest something like:
Quoted 8 lines
/*
* Generate a mask that covers the number of bytes required to address
* up to 1024 interrupts, each represented by <b> bits. This assumes
* that <b> is a power of two.
*
* ilog2(b) + ilog2(1024) is the number of bits required to bit-address
* 1024 interrupts, each represented by b bits. Minus ilog2(8) converts
* this to a byte address.
So I'm guessting this is a rewrite of ilog2( (b * 1024) / 8), but I'm stupid enough to not understand our use of logarithms here. Can someone remind me whatever I forgot at CS 101?
Quoted 9 lines
*/
virt/kvm/arm/vgic/vgic-mmio.h line 38
Quoted 2 lines
virt/kvm/arm/vgic/vgic-mmio.h new file +2
-0+#define VGIC_ADDR_IRQ_MASK (b) GENMASK_ULL(ilog2(b) + ilog2(1024) - \ + ilog2(BITS_PER_BYTE) - 1, 0)/*
* Convert a base address into a base interrupt (each interrupt
* represented by <bits> bits. This assumes that <bits> is a power
* of two, that <addr> both part of a memory region aligned on a
did you mean '<addr> *is* both part of' ?
Quoted 5 lines
* <b> bits boundary, and itself aligned on that same boundary
* (for regions that describe an interrupt with more than a single
* byte of data).
*/
In any case, thanks for the commentary, I was faily lost here.
-Christoffer
Quoted 56 lines
Quoted 40 lines
virt/kvm/arm/vgic/vgic-mmio.h new file +39
-0+#define VGIC_ADDR_TO_INTID (addr, bits) (((addr) & VGIC_ADDR_IRQ_MASK(bits)) * \ + 64 / (bits) / 8) + +/* +* Some VGIC registers store per-IRQ information, with a different number +* of bits per IRQ. For those registers this macro is used. +* The _WITH_LENGTH version instantiates registers with a fixed length +* and is mutually exclusive with the _PER_IRQ version. +*/ +#define REGISTER_DESC_WITH_BITS_PER_IRQ (off, rd, wr, bpi, acc) \ + { \ + .reg_offset = off, \ + .bits_per_irq = bpi, \ + .len = bpi * 1024 / 8, \ + .access_flags = acc, \ + .read = rd, \ + .write = wr, \ + } + +#define REGISTER_DESC_WITH_LENGTH (off, rd, wr, length, acc) \ + { \ + .reg_offset = off, \ + .bits_per_irq = 0, \ + .len = length, \ + .access_flags = acc, \ + .read = rd, \ + .write = wr, \ + } + +int kvm_vgic_register_mmio_region (struct kvm *kvm,struct kvm_vcpu *vcpu, +struct vgic_register_region *reg_desc, +struct vgic_io_device *region, +int nr_irqs,bool offset_private); + +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); +virt/kvm/arm/vgic/vgic-mmio.h line 87
Quoted 10 lines
virt/kvm/arm/vgic/vgic-mmio.h new file +10
-0+unsigned long vgic_mmio_read_raz (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len); + +unsigned long vgic_mmio_read_rao (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len); + +void vgic_mmio_write_wi (struct kvm_vcpu *vcpu,gpa_t addr, +unsigned int len,unsigned long val); + +#endif Thanks,
M.Signature · 1 line
Jazz is not dead. It just smells funny...