9 #include <linux/kvm_host.h>
10 #include <linux/interrupt.h>
11 #include <linux/irq.h>
12 #include <linux/irqdomain.h>
13 #include <linux/uaccess.h>
15 #include <clocksource/arm_arch_timer.h>
16 #include <asm/arch_timer.h>
17 #include <asm/kvm_emulate.h>
18 #include <asm/kvm_hyp.h>
19 #include <asm/kvm_nested.h>
60 if (!vcpu_has_nv(vcpu))
68 struct kvm_vcpu *vcpu = ctxt->
vcpu;
72 return __vcpu_sys_reg(vcpu, CNTV_CTL_EL0);
74 return __vcpu_sys_reg(vcpu, CNTP_CTL_EL0);
76 return __vcpu_sys_reg(vcpu, CNTHV_CTL_EL2);
78 return __vcpu_sys_reg(vcpu, CNTHP_CTL_EL2);
87 struct kvm_vcpu *vcpu = ctxt->
vcpu;
91 return __vcpu_sys_reg(vcpu, CNTV_CVAL_EL0);
93 return __vcpu_sys_reg(vcpu, CNTP_CVAL_EL0);
95 return __vcpu_sys_reg(vcpu, CNTHV_CVAL_EL2);
97 return __vcpu_sys_reg(vcpu, CNTHP_CVAL_EL2);
121 struct kvm_vcpu *vcpu = ctxt->
vcpu;
125 __vcpu_sys_reg(vcpu, CNTV_CTL_EL0) = ctl;
128 __vcpu_sys_reg(vcpu, CNTP_CTL_EL0) = ctl;
131 __vcpu_sys_reg(vcpu, CNTHV_CTL_EL2) = ctl;
134 __vcpu_sys_reg(vcpu, CNTHP_CTL_EL2) = ctl;
143 struct kvm_vcpu *vcpu = ctxt->
vcpu;
147 __vcpu_sys_reg(vcpu, CNTV_CVAL_EL0) = cval;
150 __vcpu_sys_reg(vcpu, CNTP_CVAL_EL0) = cval;
153 __vcpu_sys_reg(vcpu, CNTHV_CVAL_EL2) = cval;
156 __vcpu_sys_reg(vcpu, CNTHP_CVAL_EL2) = cval;
180 if (vcpu_has_nv(vcpu)) {
181 if (is_hyp_ctxt(vcpu)) {
192 }
else if (has_vhe()) {
204 trace_kvm_get_timer_map(vcpu->vcpu_id, map);
209 return static_branch_unlikely(&userspace_irqchip_in_use) &&
215 hrtimer_start(hrt, ktime_add_ns(ktime_get(), ns),
216 HRTIMER_MODE_ABS_HARD);
226 struct kvm_vcpu *vcpu = *(
struct kvm_vcpu **)dev_id;
250 !static_branch_unlikely(&has_gic_active_state))
281 WARN_ON(timer_ctx && timer_ctx->
loaded);
284 (ARCH_TIMER_CTRL_IT_MASK | ARCH_TIMER_CTRL_ENABLE)) == ARCH_TIMER_CTRL_ENABLE);
289 return (cpus_have_final_cap(ARM64_HAS_WFXT) &&
290 vcpu_get_flag(vcpu, IN_WFIT));
295 u64 val = vcpu_get_reg(vcpu, kvm_vcpu_sys_get_rt(vcpu));
309 u64 min_delta = ULLONG_MAX;
315 WARN(ctx->
loaded,
"timer %d loaded\n", i);
324 if (min_delta == ULLONG_MAX)
333 struct kvm_vcpu *vcpu;
337 vcpu = container_of(timer,
struct kvm_vcpu, arch.timer_cpu);
346 hrtimer_forward_now(hrt, ns_to_ktime(ns));
347 return HRTIMER_RESTART;
351 return HRTIMER_NORESTART;
357 struct kvm_vcpu *vcpu;
363 trace_kvm_timer_hrtimer_expire(ctx);
372 hrtimer_forward_now(hrt, ns_to_ktime(ns));
373 return HRTIMER_RESTART;
377 return HRTIMER_NORESTART;
396 cnt_ctl = read_sysreg_el0(SYS_CNTV_CTL);
400 cnt_ctl = read_sysreg_el0(SYS_CNTP_CTL);
408 return (cnt_ctl & ARCH_TIMER_CTRL_ENABLE) &&
409 (cnt_ctl & ARCH_TIMER_CTRL_IT_STAT) &&
410 !(cnt_ctl & ARCH_TIMER_CTRL_IT_MASK);
434 struct kvm_sync_regs *regs = &vcpu->run->s.regs;
437 regs->device_irq_level &= ~(KVM_ARM_DEV_EL1_VTIMER |
438 KVM_ARM_DEV_EL1_PTIMER);
440 regs->device_irq_level |= KVM_ARM_DEV_EL1_VTIMER;
442 regs->device_irq_level |= KVM_ARM_DEV_EL1_PTIMER;
451 trace_kvm_timer_update_irq(vcpu->vcpu_id,
timer_irq(timer_ctx),
468 trace_kvm_timer_emulate(ctx, should_fire);
470 if (should_fire != ctx->
irq.
level) {
494 write_sysreg_s(cntpoff, SYS_CNTPOFF_EL2);
506 local_irq_save(flags);
520 write_sysreg_el0(0, SYS_CNTV_CTL);
543 cval = read_sysreg_el0(SYS_CNTP_CVAL);
550 write_sysreg_el0(0, SYS_CNTP_CTL);
559 trace_kvm_timer_save_state(ctx);
563 local_irq_restore(flags);
612 local_irq_save(flags);
633 write_sysreg_el0(cval, SYS_CNTP_CVAL);
641 trace_kvm_timer_restore_state(ctx);
645 local_irq_restore(flags);
651 r = irq_set_irqchip_state(ctx->
host_timer_irq, IRQCHIP_STATE_ACTIVE, active);
657 struct kvm_vcpu *vcpu = ctx->
vcpu;
658 bool phys_active =
false;
705 #define assign_clear_set_bit(_pred, _bit, _clr, _set) \
756 if (vcpu_el2_e2h_is_set(vcpu))
759 offs->
vcpu_offset = &__vcpu_sys_reg(vcpu, CNTVOFF_EL2);
801 if (vcpu_has_nv(vcpu) && !is_hyp_ctxt(vcpu)) {
802 u64 val = __vcpu_sys_reg(vcpu, CNTHCTL_EL2);
805 if (!vcpu_el2_e2h_is_set(vcpu))
806 val = (val & (CNTHCTL_EL1PCEN | CNTHCTL_EL1PCTEN)) << 10;
808 tpt |= !(val & (CNTHCTL_EL1PCEN << 10));
809 tpc |= !(val & (CNTHCTL_EL1PCTEN << 10));
823 sysreg_clear_set(cnthctl_el2, clr, set);
836 if (static_branch_likely(&has_gic_active_state)) {
837 if (vcpu_has_nv(vcpu))
864 struct kvm_sync_regs *sregs = &vcpu->run->s.regs;
870 vlevel = sregs->device_irq_level & KVM_ARM_DEV_EL1_VTIMER;
871 plevel = sregs->device_irq_level & KVM_ARM_DEV_EL1_PTIMER;
905 if (kvm_vcpu_is_blocking(vcpu))
920 if (static_branch_likely(&has_gic_active_state))
951 for (
int i = 0; i <
nr_timers(vcpu); i++)
959 if (vcpu_has_nv(vcpu)) {
962 offs->
vcpu_offset = &__vcpu_sys_reg(vcpu, CNTVOFF_EL2);
963 offs->
vm_offset = &vcpu->kvm->arch.timer_data.poffset;
967 for (
int i = 0; i <
nr_timers(vcpu); i++)
987 struct kvm *kvm = vcpu->kvm;
996 hrtimer_init(&ctxt->
hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_HARD);
1019 if (!test_bit(KVM_ARCH_FLAG_VM_COUNTER_OFFSET, &vcpu->kvm->arch.flags)) {
1024 hrtimer_init(&timer->
bg_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_HARD);
1053 case KVM_REG_ARM_TIMER_CTL:
1057 case KVM_REG_ARM_TIMER_CNT:
1058 if (!test_bit(KVM_ARCH_FLAG_VM_COUNTER_OFFSET,
1059 &
vcpu->kvm->arch.flags)) {
1064 case KVM_REG_ARM_TIMER_CVAL:
1068 case KVM_REG_ARM_PTIMER_CTL:
1072 case KVM_REG_ARM_PTIMER_CNT:
1073 if (!test_bit(KVM_ARCH_FLAG_VM_COUNTER_OFFSET,
1074 &
vcpu->kvm->arch.flags)) {
1079 case KVM_REG_ARM_PTIMER_CVAL:
1102 ctl |= ARCH_TIMER_CTRL_IT_STAT;
1110 case KVM_REG_ARM_TIMER_CTL:
1113 case KVM_REG_ARM_TIMER_CNT:
1116 case KVM_REG_ARM_TIMER_CVAL:
1119 case KVM_REG_ARM_PTIMER_CTL:
1122 case KVM_REG_ARM_PTIMER_CNT:
1125 case KVM_REG_ARM_PTIMER_CVAL:
1141 val = lower_32_bits(val);
1245 irqd_set_forwarded_to_vcpu(d);
1247 irqd_clr_forwarded_to_vcpu(d);
1253 enum irqchip_irq_state which,
bool val)
1255 if (which != IRQCHIP_STATE_ACTIVE || !irqd_is_forwarded_to_vcpu(d))
1256 return irq_chip_set_parent_state(d, which, val);
1259 irq_chip_mask_parent(d);
1261 irq_chip_unmask_parent(d);
1268 if (!irqd_is_forwarded_to_vcpu(d))
1269 irq_chip_eoi_parent(d);
1275 if (d->chip->irq_ack)
1276 d->chip->irq_ack(d);
1282 .irq_mask = irq_chip_mask_parent,
1283 .irq_unmask = irq_chip_unmask_parent,
1285 .irq_set_type = irq_chip_set_type_parent,
1291 unsigned int nr_irqs,
void *arg)
1293 irq_hw_number_t hwirq = (uintptr_t)arg;
1295 return irq_domain_set_hwirq_and_chip(domain, virq, hwirq,
1300 unsigned int nr_irqs)
1311 *flags = irq_get_trigger_type(virq);
1312 if (*flags != IRQF_TRIGGER_HIGH && *flags != IRQF_TRIGGER_LOW) {
1313 kvm_err(
"Invalid trigger for timer IRQ%d, assuming level low\n",
1315 *flags = IRQF_TRIGGER_LOW;
1321 struct irq_domain *domain = NULL;
1323 if (info->virtual_irq <= 0) {
1324 kvm_err(
"kvm_arch_timer: invalid virtual timer IRQ: %d\n",
1333 struct fwnode_handle *fwnode;
1334 struct irq_data *data;
1336 fwnode = irq_domain_alloc_named_fwnode(
"kvm-timer");
1342 domain = irq_domain_create_hierarchy(data->domain, 0,
1346 irq_domain_free_fwnode(fwnode);
1355 if (info->physical_irq > 0) {
1369 struct arch_timer_kvm_info *info;
1372 info = arch_timer_get_kvm_info();
1376 kvm_err(
"kvm_arch_timer: uninitialized timecounter\n");
1389 kvm_err(
"kvm_arch_timer: can't request vtimer interrupt %d (%d)\n",
1398 kvm_err(
"kvm_arch_timer: error setting vcpu affinity\n");
1399 goto out_free_vtimer_irq;
1402 static_branch_enable(&has_gic_active_state);
1409 if (info->physical_irq > 0) {
1413 kvm_err(
"kvm_arch_timer: can't request ptimer interrupt %d (%d)\n",
1415 goto out_free_vtimer_irq;
1422 kvm_err(
"kvm_arch_timer: error setting vcpu affinity\n");
1423 goto out_free_ptimer_irq;
1428 }
else if (has_vhe()) {
1429 kvm_err(
"kvm_arch_timer: invalid physical timer IRQ: %d\n",
1430 info->physical_irq);
1432 goto out_free_vtimer_irq;
1437 out_free_ptimer_irq:
1438 if (info->physical_irq > 0)
1440 out_free_vtimer_irq:
1457 mutex_lock(&vcpu->kvm->arch.config_lock);
1459 for (
int i = 0; i <
nr_timers(vcpu); i++) {
1478 set_bit(KVM_ARCH_FLAG_TIMER_PPIS_IMMUTABLE, &
vcpu->kvm->arch.flags);
1480 mutex_unlock(&
vcpu->kvm->arch.config_lock);
1489 if (WARN(!vcpu,
"No vcpu context!\n"))
1492 for (
int i = 0; i <
nr_timers(vcpu); i++) {
1501 WARN_RATELIMIT(1,
"timer INTID%d unknown\n", vintid);
1524 kvm_debug(
"incorrectly configured timer irqs\n");
1555 if (cpus_have_final_cap(ARM64_HAS_ECV_CNTPOFF))
1556 sysreg_clear_set(cnthctl_el2, 0, CNTHCTL_ECV);
1561 int __user *uaddr = (
int __user *)(
long)attr->addr;
1562 int irq, idx, ret = 0;
1567 if (get_user(irq, uaddr))
1573 mutex_lock(&vcpu->kvm->arch.config_lock);
1575 if (test_bit(KVM_ARCH_FLAG_TIMER_PPIS_IMMUTABLE,
1576 &vcpu->kvm->arch.flags)) {
1581 switch (attr->attr) {
1582 case KVM_ARM_VCPU_TIMER_IRQ_VTIMER:
1585 case KVM_ARM_VCPU_TIMER_IRQ_PTIMER:
1588 case KVM_ARM_VCPU_TIMER_IRQ_HVTIMER:
1591 case KVM_ARM_VCPU_TIMER_IRQ_HPTIMER:
1604 vcpu->kvm->arch.timer_data.ppi[idx] = irq;
1607 mutex_unlock(&vcpu->kvm->arch.config_lock);
1613 int __user *uaddr = (
int __user *)(
long)attr->addr;
1617 switch (attr->attr) {
1618 case KVM_ARM_VCPU_TIMER_IRQ_VTIMER:
1621 case KVM_ARM_VCPU_TIMER_IRQ_PTIMER:
1624 case KVM_ARM_VCPU_TIMER_IRQ_HVTIMER:
1627 case KVM_ARM_VCPU_TIMER_IRQ_HPTIMER:
1635 return put_user(
irq, uaddr);
1640 switch (attr->attr) {
1641 case KVM_ARM_VCPU_TIMER_IRQ_VTIMER:
1642 case KVM_ARM_VCPU_TIMER_IRQ_PTIMER:
1643 case KVM_ARM_VCPU_TIMER_IRQ_HVTIMER:
1644 case KVM_ARM_VCPU_TIMER_IRQ_HPTIMER:
1652 struct kvm_arm_counter_offset *
offset)
1659 mutex_lock(&kvm->lock);
1662 set_bit(KVM_ARCH_FLAG_VM_COUNTER_OFFSET, &kvm->arch.flags);
1670 kvm->arch.timer_data.voffset =
offset->counter_offset;
1671 kvm->arch.timer_data.poffset =
offset->counter_offset;
1678 mutex_unlock(&kvm->lock);
void kvm_timer_vcpu_put(struct kvm_vcpu *vcpu)
static void soft_timer_start(struct hrtimer *hrt, u64 ns)
void kvm_arm_timer_write_sysreg(struct kvm_vcpu *vcpu, enum kvm_arch_timers tmr, enum kvm_arch_timer_regs treg, u64 val)
static void timer_save_state(struct arch_timer_context *ctx)
static void kvm_timer_blocking(struct kvm_vcpu *vcpu)
void kvm_timer_vcpu_reset(struct kvm_vcpu *vcpu)
int kvm_arm_timer_set_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
static int kvm_irq_init(struct arch_timer_kvm_info *info)
void kvm_timer_vcpu_terminate(struct kvm_vcpu *vcpu)
static void kvm_timer_vcpu_load_gic(struct arch_timer_context *ctx)
static void set_cntvoff(u64 cntvoff)
static unsigned int host_ptimer_irq
static bool kvm_arch_timer_get_input_level(int vintid)
static void timer_context_init(struct kvm_vcpu *vcpu, int timerid)
int __init kvm_timer_hyp_init(bool has_gic)
static void timer_set_ctl(struct arch_timer_context *ctxt, u32 ctl)
static const u8 default_ppi[]
static u64 read_timer_ctl(struct arch_timer_context *timer)
static void timer_irq_domain_free(struct irq_domain *domain, unsigned int virq, unsigned int nr_irqs)
static struct timecounter * timecounter
void kvm_timer_cpu_up(void)
static u64 kvm_arm_timer_read(struct kvm_vcpu *vcpu, struct arch_timer_context *timer, enum kvm_arch_timer_regs treg)
static struct irq_ops arch_timer_irq_ops
static int timer_irq_set_irqchip_state(struct irq_data *d, enum irqchip_irq_state which, bool val)
u64 kvm_arm_timer_read_sysreg(struct kvm_vcpu *vcpu, enum kvm_arch_timers tmr, enum kvm_arch_timer_regs treg)
void kvm_timer_vcpu_load(struct kvm_vcpu *vcpu)
static bool userspace_irqchip(struct kvm *kvm)
static void kvm_timer_update_irq(struct kvm_vcpu *vcpu, bool new_level, struct arch_timer_context *timer_ctx)
void kvm_timer_update_run(struct kvm_vcpu *vcpu)
static DEFINE_STATIC_KEY_FALSE(has_gic_active_state)
static bool kvm_timer_should_fire(struct arch_timer_context *timer_ctx)
int kvm_arm_timer_get_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
void kvm_timer_sync_user(struct kvm_vcpu *vcpu)
static int timer_irq_domain_alloc(struct irq_domain *domain, unsigned int virq, unsigned int nr_irqs, void *arg)
static u64 kvm_counter_compute_delta(struct arch_timer_context *timer_ctx, u64 val)
int kvm_timer_enable(struct kvm_vcpu *vcpu)
static void set_cntpoff(u64 cntpoff)
int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
static bool timer_irqs_are_valid(struct kvm_vcpu *vcpu)
static void unmask_vtimer_irq_user(struct kvm_vcpu *vcpu)
void kvm_timer_init_vm(struct kvm *kvm)
static irqreturn_t kvm_arch_timer_handler(int irq, void *dev_id)
static void timer_restore_state(struct arch_timer_context *ctx)
static u64 timer_get_offset(struct arch_timer_context *ctxt)
static void timer_irq_ack(struct irq_data *d)
u64 timer_get_cval(struct arch_timer_context *ctxt)
static int timer_irq_set_vcpu_affinity(struct irq_data *d, void *vcpu)
static void kvm_timer_vcpu_load_nogic(struct kvm_vcpu *vcpu)
bool kvm_timer_should_notify_user(struct kvm_vcpu *vcpu)
static void kvm_irq_fixup_flags(unsigned int virq, u32 *flags)
int kvm_vm_ioctl_set_counter_offset(struct kvm *kvm, struct kvm_arm_counter_offset *offset)
void kvm_timer_cpu_down(void)
static u64 wfit_delay_ns(struct kvm_vcpu *vcpu)
static void kvm_timer_vcpu_load_nested_switch(struct kvm_vcpu *vcpu, struct timer_map *map)
void kvm_timer_init_vhe(void)
static enum hrtimer_restart kvm_bg_timer_expire(struct hrtimer *hrt)
u64 kvm_arm_timer_get_reg(struct kvm_vcpu *vcpu, u64 regid)
int kvm_arm_timer_set_reg(struct kvm_vcpu *vcpu, u64 regid, u64 value)
static void kvm_timer_unblocking(struct kvm_vcpu *vcpu)
static void timer_set_offset(struct arch_timer_context *ctxt, u64 offset)
static void timer_set_cval(struct arch_timer_context *ctxt, u64 cval)
void kvm_timer_vcpu_init(struct kvm_vcpu *vcpu)
static enum hrtimer_restart kvm_hrtimer_expire(struct hrtimer *hrt)
u64 kvm_phys_timer_read(void)
static void timer_emulate(struct arch_timer_context *ctx)
static struct irq_chip timer_chip
static u32 host_vtimer_irq_flags
static void soft_timer_cancel(struct hrtimer *hrt)
static bool vcpu_has_wfit_active(struct kvm_vcpu *vcpu)
static int nr_timers(struct kvm_vcpu *vcpu)
u32 timer_get_ctl(struct arch_timer_context *ctxt)
void get_timer_map(struct kvm_vcpu *vcpu, struct timer_map *map)
static u64 kvm_timer_earliest_exp(struct kvm_vcpu *vcpu)
static u32 host_ptimer_irq_flags
static const struct irq_domain_ops timer_domain_ops
static bool kvm_timer_irq_can_fire(struct arch_timer_context *timer_ctx)
static void set_timer_irq_phys_active(struct arch_timer_context *ctx, bool active)
#define assign_clear_set_bit(_pred, _bit, _clr, _set)
static u64 kvm_timer_compute_delta(struct arch_timer_context *timer_ctx)
int kvm_arm_timer_has_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
static void timer_set_traps(struct kvm_vcpu *vcpu, struct timer_map *map)
static unsigned int host_vtimer_irq
static void timer_irq_eoi(struct irq_data *d)
static void kvm_arm_timer_write(struct kvm_vcpu *vcpu, struct arch_timer_context *timer, enum kvm_arch_timer_regs treg, u64 val)
bool lock_all_vcpus(struct kvm *kvm)
void unlock_all_vcpus(struct kvm *kvm)
static bool has_cntpoff(void)
#define arch_timer_ctx_index(ctx)
#define vcpu_get_timer(v, t)
#define irqchip_in_kernel(k)
struct vgic_global kvm_vgic_global_state
#define VGIC_IRQ_SW_RESAMPLE
struct kvm_vcpu *__percpu * kvm_get_running_vcpus(void)
struct kvm_vcpu * kvm_get_running_vcpu(void)
bool kvm_vcpu_wake_up(struct kvm_vcpu *vcpu)
void __kvm_timer_set_cntvoff(u64 cntvoff)
struct arch_timer_offset offset
struct arch_timer_context::@18 irq
bool(* get_input_level)(int vintid)
struct arch_timer_context * direct_vtimer
struct arch_timer_context * direct_ptimer
struct arch_timer_context * emul_ptimer
struct arch_timer_context * emul_vtimer
int kvm_vgic_get_map(struct kvm_vcpu *vcpu, unsigned int vintid)
bool kvm_vgic_map_is_active(struct kvm_vcpu *vcpu, unsigned int vintid)
void kvm_vgic_reset_mapped_irq(struct kvm_vcpu *vcpu, u32 vintid)
int kvm_vgic_unmap_phys_irq(struct kvm_vcpu *vcpu, unsigned int vintid)
int kvm_vgic_inject_irq(struct kvm *kvm, struct kvm_vcpu *vcpu, unsigned int intid, bool level, void *owner)
int kvm_vgic_set_owner(struct kvm_vcpu *vcpu, unsigned int intid, void *owner)
int kvm_vgic_map_phys_irq(struct kvm_vcpu *vcpu, unsigned int host_irq, u32 vintid, struct irq_ops *ops)