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>
22 #include <asm/paravirt.h>
23 #include <asm/pvclock-abi.h>
24 #include <asm/smp_plat.h>
26 struct static_key paravirt_steal_enabled;
27 struct static_key paravirt_steal_rq_enabled;
29 struct paravirt_patch_template pv_ops;
30 EXPORT_SYMBOL_GPL(pv_ops);
32 struct pv_time_stolen_time_region {
33 struct pvclock_vcpu_stolen_time *kaddr;
36 static DEFINE_PER_CPU(struct pv_time_stolen_time_region, stolen_time_region);
38 static bool steal_acc = true;
39 static int __init parse_no_stealacc(char *arg)
45 early_param("no-steal-acc", parse_no_stealacc);
47 /* return stolen time in ns by asking the hypervisor */
48 static u64 pv_steal_clock(int cpu)
50 struct pv_time_stolen_time_region *reg;
52 reg = per_cpu_ptr(&stolen_time_region, cpu);
54 pr_warn_once("stolen time enabled but not configured for cpu %d\n",
59 return le64_to_cpu(READ_ONCE(reg->kaddr->stolen_time));
62 static int stolen_time_dying_cpu(unsigned int cpu)
64 struct pv_time_stolen_time_region *reg;
66 reg = this_cpu_ptr(&stolen_time_region);
71 memset(reg, 0, sizeof(*reg));
76 static int init_stolen_time_cpu(unsigned int cpu)
78 struct pv_time_stolen_time_region *reg;
79 struct arm_smccc_res res;
81 reg = this_cpu_ptr(&stolen_time_region);
83 arm_smccc_1_1_invoke(ARM_SMCCC_HV_PV_TIME_ST, &res);
85 if (res.a0 == SMCCC_RET_NOT_SUPPORTED)
88 reg->kaddr = memremap(res.a0,
89 sizeof(struct pvclock_vcpu_stolen_time),
93 pr_warn("Failed to map stolen time data structure\n");
97 if (le32_to_cpu(reg->kaddr->revision) != 0 ||
98 le32_to_cpu(reg->kaddr->attributes) != 0) {
99 pr_warn_once("Unexpected revision or attributes in stolen time data\n");
106 static int pv_time_init_stolen_time(void)
110 ret = cpuhp_setup_state(CPUHP_AP_ARM_KVMPV_STARTING,
111 "hypervisor/arm/pvtime:starting",
112 init_stolen_time_cpu, stolen_time_dying_cpu);
118 static bool has_pv_steal_clock(void)
120 struct arm_smccc_res res;
122 /* To detect the presence of PV time support we require SMCCC 1.1+ */
123 if (psci_ops.smccc_version < SMCCC_VERSION_1_1)
126 arm_smccc_1_1_invoke(ARM_SMCCC_ARCH_FEATURES_FUNC_ID,
127 ARM_SMCCC_HV_PV_TIME_FEATURES, &res);
129 if (res.a0 != SMCCC_RET_SUCCESS)
132 arm_smccc_1_1_invoke(ARM_SMCCC_HV_PV_TIME_FEATURES,
133 ARM_SMCCC_HV_PV_TIME_ST, &res);
135 return (res.a0 == SMCCC_RET_SUCCESS);
138 int __init pv_time_init(void)
142 if (!has_pv_steal_clock())
145 ret = pv_time_init_stolen_time();
149 pv_ops.time.steal_clock = pv_steal_clock;
151 static_key_slow_inc(¶virt_steal_enabled);
153 static_key_slow_inc(¶virt_steal_rq_enabled);
155 pr_info("using stolen time PV\n");