1 // SPDX-License-Identifier: GPL-2.0-only
4 * Copyright (C) 2013 Citrix Systems
9 #define pr_fmt(fmt) "arm-pv: " fmt
11 #include <linux/arm-smccc.h>
12 #include <linux/cpuhotplug.h>
13 #include <linux/export.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>
23 #include <asm/paravirt.h>
24 #include <asm/pvclock-abi.h>
25 #include <asm/smp_plat.h>
27 struct static_key paravirt_steal_enabled;
28 struct static_key paravirt_steal_rq_enabled;
30 static u64 native_steal_clock(int cpu)
35 DEFINE_STATIC_CALL(pv_steal_clock, native_steal_clock);
37 struct pv_time_stolen_time_region {
38 struct pvclock_vcpu_stolen_time __rcu *kaddr;
41 static DEFINE_PER_CPU(struct pv_time_stolen_time_region, stolen_time_region);
43 static bool steal_acc = true;
44 static int __init parse_no_stealacc(char *arg)
50 early_param("no-steal-acc", parse_no_stealacc);
52 /* return stolen time in ns by asking the hypervisor */
53 static u64 para_steal_clock(int cpu)
55 struct pvclock_vcpu_stolen_time *kaddr = NULL;
56 struct pv_time_stolen_time_region *reg;
59 reg = per_cpu_ptr(&stolen_time_region, cpu);
62 * paravirt_steal_clock() may be called before the CPU
63 * online notification callback runs. Until the callback
64 * has run we just return zero.
67 kaddr = rcu_dereference(reg->kaddr);
73 ret = le64_to_cpu(READ_ONCE(kaddr->stolen_time));
78 static int stolen_time_cpu_down_prepare(unsigned int cpu)
80 struct pvclock_vcpu_stolen_time *kaddr = NULL;
81 struct pv_time_stolen_time_region *reg;
83 reg = this_cpu_ptr(&stolen_time_region);
87 kaddr = rcu_replace_pointer(reg->kaddr, NULL, true);
94 static int stolen_time_cpu_online(unsigned int cpu)
96 struct pvclock_vcpu_stolen_time *kaddr = NULL;
97 struct pv_time_stolen_time_region *reg;
98 struct arm_smccc_res res;
100 reg = this_cpu_ptr(&stolen_time_region);
102 arm_smccc_1_1_invoke(ARM_SMCCC_HV_PV_TIME_ST, &res);
104 if (res.a0 == SMCCC_RET_NOT_SUPPORTED)
107 kaddr = memremap(res.a0,
108 sizeof(struct pvclock_vcpu_stolen_time),
111 rcu_assign_pointer(reg->kaddr, kaddr);
114 pr_warn("Failed to map stolen time data structure\n");
118 if (le32_to_cpu(kaddr->revision) != 0 ||
119 le32_to_cpu(kaddr->attributes) != 0) {
120 pr_warn_once("Unexpected revision or attributes in stolen time data\n");
127 static int __init pv_time_init_stolen_time(void)
131 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
132 "hypervisor/arm/pvtime:online",
133 stolen_time_cpu_online,
134 stolen_time_cpu_down_prepare);
140 static bool __init has_pv_steal_clock(void)
142 struct arm_smccc_res res;
144 arm_smccc_1_1_invoke(ARM_SMCCC_ARCH_FEATURES_FUNC_ID,
145 ARM_SMCCC_HV_PV_TIME_FEATURES, &res);
147 if (res.a0 != SMCCC_RET_SUCCESS)
150 arm_smccc_1_1_invoke(ARM_SMCCC_HV_PV_TIME_FEATURES,
151 ARM_SMCCC_HV_PV_TIME_ST, &res);
153 return (res.a0 == SMCCC_RET_SUCCESS);
156 int __init pv_time_init(void)
160 if (!has_pv_steal_clock())
163 ret = pv_time_init_stolen_time();
167 static_call_update(pv_steal_clock, para_steal_clock);
169 static_key_slow_inc(¶virt_steal_enabled);
171 static_key_slow_inc(¶virt_steal_rq_enabled);
173 pr_info("using stolen time PV\n");