Marc Zyngier | 1431af3 | 2015-10-19 16:32:20 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012-2015 - ARM Ltd |
| 3 | * Author: Marc Zyngier <marc.zyngier@arm.com> |
| 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 | |
Marc Zyngier | 13720a5 | 2016-01-28 13:44:07 +0000 | [diff] [blame] | 22 | #include <asm/kvm_hyp.h> |
Marc Zyngier | 1431af3 | 2015-10-19 16:32:20 +0100 | [diff] [blame] | 23 | |
Christoffer Dall | 688c50a | 2017-01-04 16:10:28 +0100 | [diff] [blame] | 24 | void __hyp_text __kvm_timer_set_cntvoff(u32 cntvoff_low, u32 cntvoff_high) |
Marc Zyngier | 1431af3 | 2015-10-19 16:32:20 +0100 | [diff] [blame] | 25 | { |
Christoffer Dall | 688c50a | 2017-01-04 16:10:28 +0100 | [diff] [blame] | 26 | u64 cntvoff = (u64)cntvoff_high << 32 | cntvoff_low; |
| 27 | write_sysreg(cntvoff, cntvoff_el2); |
| 28 | } |
| 29 | |
| 30 | void __hyp_text enable_el1_phys_timer_access(void) |
| 31 | { |
Marc Zyngier | 1431af3 | 2015-10-19 16:32:20 +0100 | [diff] [blame] | 32 | u64 val; |
| 33 | |
Christoffer Dall | 688c50a | 2017-01-04 16:10:28 +0100 | [diff] [blame] | 34 | /* Allow physical timer/counter access for the host */ |
| 35 | val = read_sysreg(cnthctl_el2); |
| 36 | val |= CNTHCTL_EL1PCTEN | CNTHCTL_EL1PCEN; |
| 37 | write_sysreg(val, cnthctl_el2); |
| 38 | } |
Marc Zyngier | 1431af3 | 2015-10-19 16:32:20 +0100 | [diff] [blame] | 39 | |
Christoffer Dall | 688c50a | 2017-01-04 16:10:28 +0100 | [diff] [blame] | 40 | void __hyp_text disable_el1_phys_timer_access(void) |
| 41 | { |
| 42 | u64 val; |
Marc Zyngier | 1431af3 | 2015-10-19 16:32:20 +0100 | [diff] [blame] | 43 | |
Jintack Lim | 488f94d | 2016-12-01 14:32:05 -0500 | [diff] [blame] | 44 | /* |
Christoffer Dall | 688c50a | 2017-01-04 16:10:28 +0100 | [diff] [blame] | 45 | * Disallow physical timer access for the guest |
| 46 | * Physical counter access is allowed |
| 47 | */ |
| 48 | val = read_sysreg(cnthctl_el2); |
| 49 | val &= ~CNTHCTL_EL1PCEN; |
| 50 | val |= CNTHCTL_EL1PCTEN; |
| 51 | write_sysreg(val, cnthctl_el2); |
| 52 | } |
| 53 | |
| 54 | void __hyp_text __timer_disable_traps(struct kvm_vcpu *vcpu) |
| 55 | { |
| 56 | /* |
Jintack Lim | 488f94d | 2016-12-01 14:32:05 -0500 | [diff] [blame] | 57 | * We don't need to do this for VHE since the host kernel runs in EL2 |
| 58 | * with HCR_EL2.TGE ==1, which makes those bits have no impact. |
| 59 | */ |
Christoffer Dall | 688c50a | 2017-01-04 16:10:28 +0100 | [diff] [blame] | 60 | if (!has_vhe()) |
| 61 | enable_el1_phys_timer_access(); |
Marc Zyngier | 1431af3 | 2015-10-19 16:32:20 +0100 | [diff] [blame] | 62 | } |
| 63 | |
Christoffer Dall | 688c50a | 2017-01-04 16:10:28 +0100 | [diff] [blame] | 64 | void __hyp_text __timer_enable_traps(struct kvm_vcpu *vcpu) |
Marc Zyngier | 1431af3 | 2015-10-19 16:32:20 +0100 | [diff] [blame] | 65 | { |
Christoffer Dall | 688c50a | 2017-01-04 16:10:28 +0100 | [diff] [blame] | 66 | if (!has_vhe()) |
| 67 | disable_el1_phys_timer_access(); |
Marc Zyngier | 1431af3 | 2015-10-19 16:32:20 +0100 | [diff] [blame] | 68 | } |