2019-05-29 23:57:47 +00:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-only */
|
2013-01-23 18:21:58 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 ARM Ltd.
|
|
|
|
* Author: Marc Zyngier <marc.zyngier@arm.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __ASM_ARM_KVM_ARCH_TIMER_H
|
|
|
|
#define __ASM_ARM_KVM_ARCH_TIMER_H
|
|
|
|
|
|
|
|
#include <linux/clocksource.h>
|
|
|
|
#include <linux/hrtimer.h>
|
|
|
|
|
2018-07-05 15:48:23 +00:00
|
|
|
enum kvm_arch_timers {
|
|
|
|
TIMER_PTIMER,
|
|
|
|
TIMER_VTIMER,
|
2023-03-30 17:47:57 +00:00
|
|
|
NR_KVM_EL0_TIMERS,
|
|
|
|
TIMER_HVTIMER = NR_KVM_EL0_TIMERS,
|
|
|
|
TIMER_HPTIMER,
|
2018-07-05 15:48:23 +00:00
|
|
|
NR_KVM_TIMERS
|
|
|
|
};
|
|
|
|
|
|
|
|
enum kvm_arch_timer_regs {
|
|
|
|
TIMER_REG_CNT,
|
|
|
|
TIMER_REG_CVAL,
|
|
|
|
TIMER_REG_TVAL,
|
|
|
|
TIMER_REG_CTL,
|
2023-03-30 17:47:57 +00:00
|
|
|
TIMER_REG_VOFF,
|
2018-07-05 15:48:23 +00:00
|
|
|
};
|
|
|
|
|
2023-02-24 19:16:40 +00:00
|
|
|
struct arch_timer_offset {
|
|
|
|
/*
|
|
|
|
* If set, pointer to one of the offsets in the kvm's offset
|
|
|
|
* structure. If NULL, assume a zero offset.
|
|
|
|
*/
|
|
|
|
u64 *vm_offset;
|
2023-03-30 17:47:56 +00:00
|
|
|
/*
|
|
|
|
* If set, pointer to one of the offsets in the vcpu's sysreg
|
|
|
|
* array. If NULL, assume a zero offset.
|
|
|
|
*/
|
|
|
|
u64 *vcpu_offset;
|
2023-02-24 19:16:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct arch_timer_vm_data {
|
|
|
|
/* Offset applied to the virtual timer/counter */
|
|
|
|
u64 voffset;
|
2023-03-30 17:47:44 +00:00
|
|
|
/* Offset applied to the physical timer/counter */
|
|
|
|
u64 poffset;
|
2023-03-30 17:47:51 +00:00
|
|
|
|
|
|
|
/* The PPI for each timer, global to the VM */
|
|
|
|
u8 ppi[NR_KVM_TIMERS];
|
2023-02-24 19:16:40 +00:00
|
|
|
};
|
|
|
|
|
2017-02-03 15:19:59 +00:00
|
|
|
struct arch_timer_context {
|
2018-09-18 17:08:18 +00:00
|
|
|
struct kvm_vcpu *vcpu;
|
|
|
|
|
|
|
|
/* Emulated Timer (may be unused) */
|
|
|
|
struct hrtimer hrtimer;
|
2023-03-30 17:47:41 +00:00
|
|
|
u64 ns_frac;
|
2019-02-19 13:04:30 +00:00
|
|
|
|
2023-02-24 19:16:40 +00:00
|
|
|
/* Offset for this counter/timer */
|
|
|
|
struct arch_timer_offset offset;
|
2019-01-04 12:31:22 +00:00
|
|
|
/*
|
|
|
|
* We have multiple paths which can save/restore the timer state onto
|
|
|
|
* the hardware, so we need some way of keeping track of where the
|
|
|
|
* latest state is.
|
|
|
|
*/
|
|
|
|
bool loaded;
|
|
|
|
|
2023-03-30 17:47:51 +00:00
|
|
|
/* Output level of the timer IRQ */
|
|
|
|
struct {
|
|
|
|
bool level;
|
|
|
|
} irq;
|
|
|
|
|
2019-02-19 13:04:30 +00:00
|
|
|
/* Duplicated state from arch_timer.c for convenience */
|
|
|
|
u32 host_timer_irq;
|
2017-02-03 15:19:59 +00:00
|
|
|
};
|
2013-01-23 18:21:58 +00:00
|
|
|
|
2019-01-04 12:31:22 +00:00
|
|
|
struct timer_map {
|
|
|
|
struct arch_timer_context *direct_vtimer;
|
|
|
|
struct arch_timer_context *direct_ptimer;
|
2023-03-30 17:47:57 +00:00
|
|
|
struct arch_timer_context *emul_vtimer;
|
2019-01-04 12:31:22 +00:00
|
|
|
struct arch_timer_context *emul_ptimer;
|
|
|
|
};
|
|
|
|
|
2023-08-22 12:18:10 +00:00
|
|
|
void get_timer_map(struct kvm_vcpu *vcpu, struct timer_map *map);
|
|
|
|
|
2017-02-03 15:19:59 +00:00
|
|
|
struct arch_timer_cpu {
|
2018-09-18 17:08:18 +00:00
|
|
|
struct arch_timer_context timers[NR_KVM_TIMERS];
|
2013-01-23 18:21:58 +00:00
|
|
|
|
|
|
|
/* Background timer used when the guest is not running */
|
2017-06-17 14:33:02 +00:00
|
|
|
struct hrtimer bg_timer;
|
2013-01-23 18:21:58 +00:00
|
|
|
|
2016-05-18 15:26:00 +00:00
|
|
|
/* Is the timer enabled */
|
|
|
|
bool enabled;
|
2013-01-23 18:21:58 +00:00
|
|
|
};
|
|
|
|
|
KVM: x86: Unify pr_fmt to use module name for all KVM modules
Define pr_fmt using KBUILD_MODNAME for all KVM x86 code so that printks
use consistent formatting across common x86, Intel, and AMD code. In
addition to providing consistent print formatting, using KBUILD_MODNAME,
e.g. kvm_amd and kvm_intel, allows referencing SVM and VMX (and SEV and
SGX and ...) as technologies without generating weird messages, and
without causing naming conflicts with other kernel code, e.g. "SEV: ",
"tdx: ", "sgx: " etc.. are all used by the kernel for non-KVM subsystems.
Opportunistically move away from printk() for prints that need to be
modified anyways, e.g. to drop a manual "kvm: " prefix.
Opportunistically convert a few SGX WARNs that are similarly modified to
WARN_ONCE; in the very unlikely event that the WARNs fire, odds are good
that they would fire repeatedly and spam the kernel log without providing
unique information in each print.
Note, defining pr_fmt yields undesirable results for code that uses KVM's
printk wrappers, e.g. vcpu_unimpl(). But, that's a pre-existing problem
as SVM/kvm_amd already defines a pr_fmt, and thankfully use of KVM's
wrappers is relatively limited in KVM x86 code.
Signed-off-by: Sean Christopherson <seanjc@google.com>
Reviewed-by: Paul Durrant <paul@xen.org>
Message-Id: <20221130230934.1014142-35-seanjc@google.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2022-11-30 23:09:18 +00:00
|
|
|
int __init kvm_timer_hyp_init(bool has_gic);
|
2016-05-18 15:26:00 +00:00
|
|
|
int kvm_timer_enable(struct kvm_vcpu *vcpu);
|
2023-09-20 19:50:35 +00:00
|
|
|
void kvm_timer_vcpu_reset(struct kvm_vcpu *vcpu);
|
2013-01-23 18:21:58 +00:00
|
|
|
void kvm_timer_vcpu_init(struct kvm_vcpu *vcpu);
|
2020-04-22 07:58:22 +00:00
|
|
|
void kvm_timer_sync_user(struct kvm_vcpu *vcpu);
|
KVM: arm/arm64: Support arch timers with a userspace gic
If you're running with a userspace gic or other interrupt controller
(that is no vgic in the kernel), then you have so far not been able to
use the architected timers, because the output of the architected
timers, which are driven inside the kernel, was a kernel-only construct
between the arch timer code and the vgic.
This patch implements the new KVM_CAP_ARM_USER_IRQ feature, where we use a
side channel on the kvm_run structure, run->s.regs.device_irq_level, to
always notify userspace of the timer output levels when using a userspace
irqchip.
This works by ensuring that before we enter the guest, if the timer
output level has changed compared to what we last told userspace, we
don't enter the guest, but instead return to userspace to notify it of
the new level. If we are exiting, because of an MMIO for example, and
the level changed at the same time, the value is also updated and
userspace can sample the line as it needs. This is nicely achieved
simply always updating the timer_irq_level field after the main run
loop.
Note that the kvm_timer_update_irq trace event is changed to show the
host IRQ number for the timer instead of the guest IRQ number, because
the kernel no longer know which IRQ userspace wires up the timer signal
to.
Also note that this patch implements all required functionality but does
not yet advertise the capability.
Reviewed-by: Alexander Graf <agraf@suse.de>
Reviewed-by: Marc Zyngier <marc.zyngier@arm.com>
Signed-off-by: Alexander Graf <agraf@suse.de>
Signed-off-by: Christoffer Dall <christoffer.dall@linaro.org>
2016-09-27 19:08:06 +00:00
|
|
|
bool kvm_timer_should_notify_user(struct kvm_vcpu *vcpu);
|
|
|
|
void kvm_timer_update_run(struct kvm_vcpu *vcpu);
|
2013-01-23 18:21:58 +00:00
|
|
|
void kvm_timer_vcpu_terminate(struct kvm_vcpu *vcpu);
|
2014-07-04 14:54:14 +00:00
|
|
|
|
2023-03-30 17:47:51 +00:00
|
|
|
void kvm_timer_init_vm(struct kvm *kvm);
|
|
|
|
|
2014-07-04 14:54:14 +00:00
|
|
|
u64 kvm_arm_timer_get_reg(struct kvm_vcpu *, u64 regid);
|
|
|
|
int kvm_arm_timer_set_reg(struct kvm_vcpu *, u64 regid, u64 value);
|
|
|
|
|
2017-05-02 18:19:15 +00:00
|
|
|
int kvm_arm_timer_set_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr);
|
|
|
|
int kvm_arm_timer_get_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr);
|
|
|
|
int kvm_arm_timer_has_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr);
|
|
|
|
|
2017-02-03 15:20:08 +00:00
|
|
|
u64 kvm_phys_timer_read(void);
|
|
|
|
|
KVM: arm/arm64: Avoid timer save/restore in vcpu entry/exit
We don't need to save and restore the hardware timer state and examine
if it generates interrupts on on every entry/exit to the guest. The
timer hardware is perfectly capable of telling us when it has expired
by signaling interrupts.
When taking a vtimer interrupt in the host, we don't want to mess with
the timer configuration, we just want to forward the physical interrupt
to the guest as a virtual interrupt. We can use the split priority drop
and deactivate feature of the GIC to do this, which leaves an EOI'ed
interrupt active on the physical distributor, making sure we don't keep
taking timer interrupts which would prevent the guest from running. We
can then forward the physical interrupt to the VM using the HW bit in
the LR of the GIC, like we do already, which lets the guest directly
deactivate both the physical and virtual timer simultaneously, allowing
the timer hardware to exit the VM and generate a new physical interrupt
when the timer output is again asserted later on.
We do need to capture this state when migrating VCPUs between physical
CPUs, however, which we use the vcpu put/load functions for, which are
called through preempt notifiers whenever the thread is scheduled away
from the CPU or called directly if we return from the ioctl to
userspace.
One caveat is that we have to save and restore the timer state in both
kvm_timer_vcpu_[put/load] and kvm_timer_[schedule/unschedule], because
we can have the following flows:
1. kvm_vcpu_block
2. kvm_timer_schedule
3. schedule
4. kvm_timer_vcpu_put (preempt notifier)
5. schedule (vcpu thread gets scheduled back)
6. kvm_timer_vcpu_load (preempt notifier)
7. kvm_timer_unschedule
And a version where we don't actually call schedule:
1. kvm_vcpu_block
2. kvm_timer_schedule
7. kvm_timer_unschedule
Since kvm_timer_[schedule/unschedule] may not be followed by put/load,
but put/load also may be called independently, we call the timer
save/restore functions from both paths. Since they rely on the loaded
flag to never save/restore when unnecessary, this doesn't cause any
harm, and we ensure that all invokations of either set of functions work
as intended.
An added benefit beyond not having to read and write the timer sysregs
on every entry and exit is that we no longer have to actively write the
active state to the physical distributor, because we configured the
irq for the vtimer to only get a priority drop when handling the
interrupt in the GIC driver (we called irq_set_vcpu_affinity()), and
the interrupt stays active after firing on the host.
Reviewed-by: Marc Zyngier <marc.zyngier@arm.com>
Signed-off-by: Christoffer Dall <cdall@linaro.org>
2016-10-16 18:30:38 +00:00
|
|
|
void kvm_timer_vcpu_load(struct kvm_vcpu *vcpu);
|
2016-01-29 19:04:48 +00:00
|
|
|
void kvm_timer_vcpu_put(struct kvm_vcpu *vcpu);
|
|
|
|
|
2016-12-01 19:32:05 +00:00
|
|
|
void kvm_timer_init_vhe(void);
|
2017-02-03 15:19:59 +00:00
|
|
|
|
2018-09-18 17:08:18 +00:00
|
|
|
#define vcpu_timer(v) (&(v)->arch.timer_cpu)
|
|
|
|
#define vcpu_get_timer(v,t) (&vcpu_timer(v)->timers[(t)])
|
|
|
|
#define vcpu_vtimer(v) (&(v)->arch.timer_cpu.timers[TIMER_VTIMER])
|
|
|
|
#define vcpu_ptimer(v) (&(v)->arch.timer_cpu.timers[TIMER_PTIMER])
|
2023-03-30 17:47:57 +00:00
|
|
|
#define vcpu_hvtimer(v) (&(v)->arch.timer_cpu.timers[TIMER_HVTIMER])
|
|
|
|
#define vcpu_hptimer(v) (&(v)->arch.timer_cpu.timers[TIMER_HPTIMER])
|
2018-07-05 15:48:23 +00:00
|
|
|
|
2019-02-19 13:04:30 +00:00
|
|
|
#define arch_timer_ctx_index(ctx) ((ctx) - vcpu_timer((ctx)->vcpu)->timers)
|
|
|
|
|
2023-03-30 17:47:51 +00:00
|
|
|
#define timer_vm_data(ctx) (&(ctx)->vcpu->kvm->arch.timer_data)
|
|
|
|
#define timer_irq(ctx) (timer_vm_data(ctx)->ppi[arch_timer_ctx_index(ctx)])
|
2023-03-30 17:47:50 +00:00
|
|
|
|
2018-07-05 15:48:23 +00:00
|
|
|
u64 kvm_arm_timer_read_sysreg(struct kvm_vcpu *vcpu,
|
|
|
|
enum kvm_arch_timers tmr,
|
|
|
|
enum kvm_arch_timer_regs treg);
|
|
|
|
void kvm_arm_timer_write_sysreg(struct kvm_vcpu *vcpu,
|
|
|
|
enum kvm_arch_timers tmr,
|
|
|
|
enum kvm_arch_timer_regs treg,
|
|
|
|
u64 val);
|
KVM: arm/arm64: Avoid timer save/restore in vcpu entry/exit
We don't need to save and restore the hardware timer state and examine
if it generates interrupts on on every entry/exit to the guest. The
timer hardware is perfectly capable of telling us when it has expired
by signaling interrupts.
When taking a vtimer interrupt in the host, we don't want to mess with
the timer configuration, we just want to forward the physical interrupt
to the guest as a virtual interrupt. We can use the split priority drop
and deactivate feature of the GIC to do this, which leaves an EOI'ed
interrupt active on the physical distributor, making sure we don't keep
taking timer interrupts which would prevent the guest from running. We
can then forward the physical interrupt to the VM using the HW bit in
the LR of the GIC, like we do already, which lets the guest directly
deactivate both the physical and virtual timer simultaneously, allowing
the timer hardware to exit the VM and generate a new physical interrupt
when the timer output is again asserted later on.
We do need to capture this state when migrating VCPUs between physical
CPUs, however, which we use the vcpu put/load functions for, which are
called through preempt notifiers whenever the thread is scheduled away
from the CPU or called directly if we return from the ioctl to
userspace.
One caveat is that we have to save and restore the timer state in both
kvm_timer_vcpu_[put/load] and kvm_timer_[schedule/unschedule], because
we can have the following flows:
1. kvm_vcpu_block
2. kvm_timer_schedule
3. schedule
4. kvm_timer_vcpu_put (preempt notifier)
5. schedule (vcpu thread gets scheduled back)
6. kvm_timer_vcpu_load (preempt notifier)
7. kvm_timer_unschedule
And a version where we don't actually call schedule:
1. kvm_vcpu_block
2. kvm_timer_schedule
7. kvm_timer_unschedule
Since kvm_timer_[schedule/unschedule] may not be followed by put/load,
but put/load also may be called independently, we call the timer
save/restore functions from both paths. Since they rely on the loaded
flag to never save/restore when unnecessary, this doesn't cause any
harm, and we ensure that all invokations of either set of functions work
as intended.
An added benefit beyond not having to read and write the timer sysregs
on every entry and exit is that we no longer have to actively write the
active state to the physical distributor, because we configured the
irq for the vtimer to only get a priority drop when handling the
interrupt in the GIC driver (we called irq_set_vcpu_affinity()), and
the interrupt stays active after firing on the host.
Reviewed-by: Marc Zyngier <marc.zyngier@arm.com>
Signed-off-by: Christoffer Dall <cdall@linaro.org>
2016-10-16 18:30:38 +00:00
|
|
|
|
2019-06-28 14:23:43 +00:00
|
|
|
/* Needed for tracing */
|
|
|
|
u32 timer_get_ctl(struct arch_timer_context *ctxt);
|
|
|
|
u64 timer_get_cval(struct arch_timer_context *ctxt);
|
|
|
|
|
2022-11-30 23:09:00 +00:00
|
|
|
/* CPU HP callbacks */
|
|
|
|
void kvm_timer_cpu_up(void);
|
|
|
|
void kvm_timer_cpu_down(void);
|
|
|
|
|
2023-08-22 12:18:10 +00:00
|
|
|
static inline bool has_cntpoff(void)
|
|
|
|
{
|
|
|
|
return (has_vhe() && cpus_have_final_cap(ARM64_HAS_ECV_CNTPOFF));
|
|
|
|
}
|
|
|
|
|
2013-01-23 18:21:58 +00:00
|
|
|
#endif
|