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>
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);
55 * paravirt_steal_clock() may be called before the CPU
56 * online notification callback runs. Until the callback
57 * has run we just return zero.
62 return le64_to_cpu(READ_ONCE(reg->kaddr->stolen_time));
65 static int stolen_time_cpu_down_prepare(unsigned int cpu)
67 struct pv_time_stolen_time_region *reg;
69 reg = this_cpu_ptr(&stolen_time_region);
74 memset(reg, 0, sizeof(*reg));
79 static int stolen_time_cpu_online(unsigned int cpu)
81 struct pv_time_stolen_time_region *reg;
82 struct arm_smccc_res res;
84 reg = this_cpu_ptr(&stolen_time_region);
86 arm_smccc_1_1_invoke(ARM_SMCCC_HV_PV_TIME_ST, &res);
88 if (res.a0 == SMCCC_RET_NOT_SUPPORTED)
91 reg->kaddr = memremap(res.a0,
92 sizeof(struct pvclock_vcpu_stolen_time),
96 pr_warn("Failed to map stolen time data structure\n");
100 if (le32_to_cpu(reg->kaddr->revision) != 0 ||
101 le32_to_cpu(reg->kaddr->attributes) != 0) {
102 pr_warn_once("Unexpected revision or attributes in stolen time data\n");
109 static int __init pv_time_init_stolen_time(void)
113 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
114 "hypervisor/arm/pvtime:online",
115 stolen_time_cpu_online,
116 stolen_time_cpu_down_prepare);
122 static bool __init has_pv_steal_clock(void)
124 struct arm_smccc_res res;
126 /* To detect the presence of PV time support we require SMCCC 1.1+ */
127 if (arm_smccc_1_1_get_conduit() == SMCCC_CONDUIT_NONE)
130 arm_smccc_1_1_invoke(ARM_SMCCC_ARCH_FEATURES_FUNC_ID,
131 ARM_SMCCC_HV_PV_TIME_FEATURES, &res);
133 if (res.a0 != SMCCC_RET_SUCCESS)
136 arm_smccc_1_1_invoke(ARM_SMCCC_HV_PV_TIME_FEATURES,
137 ARM_SMCCC_HV_PV_TIME_ST, &res);
139 return (res.a0 == SMCCC_RET_SUCCESS);
142 int __init pv_time_init(void)
146 if (!has_pv_steal_clock())
149 ret = pv_time_init_stolen_time();
153 pv_ops.time.steal_clock = pv_steal_clock;
155 static_key_slow_inc(¶virt_steal_enabled);
157 static_key_slow_inc(¶virt_steal_rq_enabled);
159 pr_info("using stolen time PV\n");