1 // SPDX-License-Identifier: GPL-2.0-only
4 * Copyright (C) 2013 Citrix Systems
6 * Author: Stefano Stabellini <stefano.stabellini@eu.citrix.com>
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 *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 pv_time_stolen_time_region *reg;
57 reg = per_cpu_ptr(&stolen_time_region, cpu);
60 * paravirt_steal_clock() may be called before the CPU
61 * online notification callback runs. Until the callback
62 * has run we just return zero.
67 return le64_to_cpu(READ_ONCE(reg->kaddr->stolen_time));
70 static int stolen_time_cpu_down_prepare(unsigned int cpu)
72 struct pv_time_stolen_time_region *reg;
74 reg = this_cpu_ptr(&stolen_time_region);
79 memset(reg, 0, sizeof(*reg));
84 static int stolen_time_cpu_online(unsigned int cpu)
86 struct pv_time_stolen_time_region *reg;
87 struct arm_smccc_res res;
89 reg = this_cpu_ptr(&stolen_time_region);
91 arm_smccc_1_1_invoke(ARM_SMCCC_HV_PV_TIME_ST, &res);
93 if (res.a0 == SMCCC_RET_NOT_SUPPORTED)
96 reg->kaddr = memremap(res.a0,
97 sizeof(struct pvclock_vcpu_stolen_time),
101 pr_warn("Failed to map stolen time data structure\n");
105 if (le32_to_cpu(reg->kaddr->revision) != 0 ||
106 le32_to_cpu(reg->kaddr->attributes) != 0) {
107 pr_warn_once("Unexpected revision or attributes in stolen time data\n");
114 static int __init pv_time_init_stolen_time(void)
118 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
119 "hypervisor/arm/pvtime:online",
120 stolen_time_cpu_online,
121 stolen_time_cpu_down_prepare);
127 static bool __init has_pv_steal_clock(void)
129 struct arm_smccc_res res;
131 /* To detect the presence of PV time support we require SMCCC 1.1+ */
132 if (arm_smccc_1_1_get_conduit() == SMCCC_CONDUIT_NONE)
135 arm_smccc_1_1_invoke(ARM_SMCCC_ARCH_FEATURES_FUNC_ID,
136 ARM_SMCCC_HV_PV_TIME_FEATURES, &res);
138 if (res.a0 != SMCCC_RET_SUCCESS)
141 arm_smccc_1_1_invoke(ARM_SMCCC_HV_PV_TIME_FEATURES,
142 ARM_SMCCC_HV_PV_TIME_ST, &res);
144 return (res.a0 == SMCCC_RET_SUCCESS);
147 int __init pv_time_init(void)
151 if (!has_pv_steal_clock())
154 ret = pv_time_init_stolen_time();
158 static_call_update(pv_steal_clock, para_steal_clock);
160 static_key_slow_inc(¶virt_steal_enabled);
162 static_key_slow_inc(¶virt_steal_rq_enabled);
164 pr_info("using stolen time PV\n");