|
| 1 | +/* |
| 2 | + * Copyright (C) 2012-2015 - ARM Ltd |
| 3 | + * Author: Marc Zyngier <[email protected]> |
| 4 | + * |
| 5 | + * This program is free software; you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License version 2 as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + */ |
| 17 | + |
| 18 | +#include <clocksource/arm_arch_timer.h> |
| 19 | +#include <linux/compiler.h> |
| 20 | +#include <linux/kvm_host.h> |
| 21 | + |
| 22 | +#include <asm/kvm_mmu.h> |
| 23 | + |
| 24 | +#include "hyp.h" |
| 25 | + |
| 26 | +/* vcpu is already in the HYP VA space */ |
| 27 | +void __hyp_text __timer_save_state(struct kvm_vcpu *vcpu) |
| 28 | +{ |
| 29 | + struct kvm *kvm = kern_hyp_va(vcpu->kvm); |
| 30 | + struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; |
| 31 | + u64 val; |
| 32 | + |
| 33 | + if (kvm->arch.timer.enabled) { |
| 34 | + timer->cntv_ctl = read_sysreg(CNTV_CTL); |
| 35 | + timer->cntv_cval = read_sysreg(CNTV_CVAL); |
| 36 | + } |
| 37 | + |
| 38 | + /* Disable the virtual timer */ |
| 39 | + write_sysreg(0, CNTV_CTL); |
| 40 | + |
| 41 | + /* Allow physical timer/counter access for the host */ |
| 42 | + val = read_sysreg(CNTHCTL); |
| 43 | + val |= CNTHCTL_EL1PCTEN | CNTHCTL_EL1PCEN; |
| 44 | + write_sysreg(val, CNTHCTL); |
| 45 | + |
| 46 | + /* Clear cntvoff for the host */ |
| 47 | + write_sysreg(0, CNTVOFF); |
| 48 | +} |
| 49 | + |
| 50 | +void __hyp_text __timer_restore_state(struct kvm_vcpu *vcpu) |
| 51 | +{ |
| 52 | + struct kvm *kvm = kern_hyp_va(vcpu->kvm); |
| 53 | + struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; |
| 54 | + u64 val; |
| 55 | + |
| 56 | + /* |
| 57 | + * Disallow physical timer access for the guest |
| 58 | + * Physical counter access is allowed |
| 59 | + */ |
| 60 | + val = read_sysreg(CNTHCTL); |
| 61 | + val &= ~CNTHCTL_EL1PCEN; |
| 62 | + val |= CNTHCTL_EL1PCTEN; |
| 63 | + write_sysreg(val, CNTHCTL); |
| 64 | + |
| 65 | + if (kvm->arch.timer.enabled) { |
| 66 | + write_sysreg(kvm->arch.timer.cntvoff, CNTVOFF); |
| 67 | + write_sysreg(timer->cntv_cval, CNTV_CVAL); |
| 68 | + isb(); |
| 69 | + write_sysreg(timer->cntv_ctl, CNTV_CTL); |
| 70 | + } |
| 71 | +} |
0 commit comments