1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * 4 * Copyright (C) 2013 Citrix Systems 5 * 6 * Author: Stefano Stabellini <stefano.stabellini@eu.citrix.com> 7 */ 8 9 #define pr_fmt(fmt) "arm-pv: " fmt 10 11 #include <linux/arm-smccc.h> 12 #include <linux/cpuhotplug.h> 13 #include <linux/export.h> 14 #include <linux/io.h> 15 #include <linux/jump_label.h> 16 #include <linux/printk.h> 17 #include <linux/psci.h> 18 #include <linux/reboot.h> 19 #include <linux/slab.h> 20 #include <linux/types.h> 21 #include <linux/static_call.h> 22 #include <linux/sched/cputime.h> 23 24 #include <asm/paravirt.h> 25 #include <asm/pvclock-abi.h> 26 #include <asm/smp_plat.h> 27 28 struct pv_time_stolen_time_region { 29 struct pvclock_vcpu_stolen_time __rcu *kaddr; 30 }; 31 32 static DEFINE_PER_CPU(struct pv_time_stolen_time_region, stolen_time_region); 33 34 static bool steal_acc = true; 35 static int __init parse_no_stealacc(char *arg) 36 { 37 steal_acc = false; 38 return 0; 39 } 40 41 early_param("no-steal-acc", parse_no_stealacc); 42 43 /* return stolen time in ns by asking the hypervisor */ 44 static u64 para_steal_clock(int cpu) 45 { 46 struct pvclock_vcpu_stolen_time *kaddr = NULL; 47 struct pv_time_stolen_time_region *reg; 48 u64 ret = 0; 49 50 reg = per_cpu_ptr(&stolen_time_region, cpu); 51 52 /* 53 * paravirt_steal_clock() may be called before the CPU 54 * online notification callback runs. Until the callback 55 * has run we just return zero. 56 */ 57 rcu_read_lock(); 58 kaddr = rcu_dereference(reg->kaddr); 59 if (!kaddr) { 60 rcu_read_unlock(); 61 return 0; 62 } 63 64 ret = le64_to_cpu(READ_ONCE(kaddr->stolen_time)); 65 rcu_read_unlock(); 66 return ret; 67 } 68 69 static int stolen_time_cpu_down_prepare(unsigned int cpu) 70 { 71 struct pvclock_vcpu_stolen_time *kaddr = NULL; 72 struct pv_time_stolen_time_region *reg; 73 74 reg = this_cpu_ptr(&stolen_time_region); 75 if (!reg->kaddr) 76 return 0; 77 78 kaddr = rcu_replace_pointer(reg->kaddr, NULL, true); 79 synchronize_rcu(); 80 memunmap(kaddr); 81 82 return 0; 83 } 84 85 static int stolen_time_cpu_online(unsigned int cpu) 86 { 87 struct pvclock_vcpu_stolen_time *kaddr = NULL; 88 struct pv_time_stolen_time_region *reg; 89 struct arm_smccc_res res; 90 91 reg = this_cpu_ptr(&stolen_time_region); 92 93 arm_smccc_1_1_invoke(ARM_SMCCC_HV_PV_TIME_ST, &res); 94 95 if (res.a0 == SMCCC_RET_NOT_SUPPORTED) 96 return -EINVAL; 97 98 kaddr = memremap(res.a0, 99 sizeof(struct pvclock_vcpu_stolen_time), 100 MEMREMAP_WB); 101 102 rcu_assign_pointer(reg->kaddr, kaddr); 103 104 if (!reg->kaddr) { 105 pr_warn("Failed to map stolen time data structure\n"); 106 return -ENOMEM; 107 } 108 109 if (le32_to_cpu(kaddr->revision) != 0 || 110 le32_to_cpu(kaddr->attributes) != 0) { 111 pr_warn_once("Unexpected revision or attributes in stolen time data\n"); 112 return -ENXIO; 113 } 114 115 return 0; 116 } 117 118 static int __init pv_time_init_stolen_time(void) 119 { 120 int ret; 121 122 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, 123 "hypervisor/arm/pvtime:online", 124 stolen_time_cpu_online, 125 stolen_time_cpu_down_prepare); 126 if (ret < 0) 127 return ret; 128 return 0; 129 } 130 131 static bool __init has_pv_steal_clock(void) 132 { 133 struct arm_smccc_res res; 134 135 arm_smccc_1_1_invoke(ARM_SMCCC_ARCH_FEATURES_FUNC_ID, 136 ARM_SMCCC_HV_PV_TIME_FEATURES, &res); 137 138 if (res.a0 != SMCCC_RET_SUCCESS) 139 return false; 140 141 arm_smccc_1_1_invoke(ARM_SMCCC_HV_PV_TIME_FEATURES, 142 ARM_SMCCC_HV_PV_TIME_ST, &res); 143 144 return (res.a0 == SMCCC_RET_SUCCESS); 145 } 146 147 int __init pv_time_init(void) 148 { 149 int ret; 150 151 if (!has_pv_steal_clock()) 152 return 0; 153 154 ret = pv_time_init_stolen_time(); 155 if (ret) 156 return ret; 157 158 static_call_update(pv_steal_clock, para_steal_clock); 159 160 static_key_slow_inc(¶virt_steal_enabled); 161 if (steal_acc) 162 static_key_slow_inc(¶virt_steal_rq_enabled); 163 164 pr_info("using stolen time PV\n"); 165 166 return 0; 167 } 168