xref: /linux/arch/arm64/kernel/paravirt.c (revision 4fd18fc38757217c746aa063ba9e4729814dc737)
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 
22 #include <asm/paravirt.h>
23 #include <asm/pvclock-abi.h>
24 #include <asm/smp_plat.h>
25 
26 struct static_key paravirt_steal_enabled;
27 struct static_key paravirt_steal_rq_enabled;
28 
29 struct paravirt_patch_template pv_ops;
30 EXPORT_SYMBOL_GPL(pv_ops);
31 
32 struct pv_time_stolen_time_region {
33 	struct pvclock_vcpu_stolen_time *kaddr;
34 };
35 
36 static DEFINE_PER_CPU(struct pv_time_stolen_time_region, stolen_time_region);
37 
38 static bool steal_acc = true;
39 static int __init parse_no_stealacc(char *arg)
40 {
41 	steal_acc = false;
42 	return 0;
43 }
44 
45 early_param("no-steal-acc", parse_no_stealacc);
46 
47 /* return stolen time in ns by asking the hypervisor */
48 static u64 pv_steal_clock(int cpu)
49 {
50 	struct pv_time_stolen_time_region *reg;
51 
52 	reg = per_cpu_ptr(&stolen_time_region, cpu);
53 
54 	/*
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.
58 	 */
59 	if (!reg->kaddr)
60 		return 0;
61 
62 	return le64_to_cpu(READ_ONCE(reg->kaddr->stolen_time));
63 }
64 
65 static int stolen_time_cpu_down_prepare(unsigned int cpu)
66 {
67 	struct pv_time_stolen_time_region *reg;
68 
69 	reg = this_cpu_ptr(&stolen_time_region);
70 	if (!reg->kaddr)
71 		return 0;
72 
73 	memunmap(reg->kaddr);
74 	memset(reg, 0, sizeof(*reg));
75 
76 	return 0;
77 }
78 
79 static int stolen_time_cpu_online(unsigned int cpu)
80 {
81 	struct pv_time_stolen_time_region *reg;
82 	struct arm_smccc_res res;
83 
84 	reg = this_cpu_ptr(&stolen_time_region);
85 
86 	arm_smccc_1_1_invoke(ARM_SMCCC_HV_PV_TIME_ST, &res);
87 
88 	if (res.a0 == SMCCC_RET_NOT_SUPPORTED)
89 		return -EINVAL;
90 
91 	reg->kaddr = memremap(res.a0,
92 			      sizeof(struct pvclock_vcpu_stolen_time),
93 			      MEMREMAP_WB);
94 
95 	if (!reg->kaddr) {
96 		pr_warn("Failed to map stolen time data structure\n");
97 		return -ENOMEM;
98 	}
99 
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");
103 		return -ENXIO;
104 	}
105 
106 	return 0;
107 }
108 
109 static int __init pv_time_init_stolen_time(void)
110 {
111 	int ret;
112 
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);
117 	if (ret < 0)
118 		return ret;
119 	return 0;
120 }
121 
122 static bool __init has_pv_steal_clock(void)
123 {
124 	struct arm_smccc_res res;
125 
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)
128 		return false;
129 
130 	arm_smccc_1_1_invoke(ARM_SMCCC_ARCH_FEATURES_FUNC_ID,
131 			     ARM_SMCCC_HV_PV_TIME_FEATURES, &res);
132 
133 	if (res.a0 != SMCCC_RET_SUCCESS)
134 		return false;
135 
136 	arm_smccc_1_1_invoke(ARM_SMCCC_HV_PV_TIME_FEATURES,
137 			     ARM_SMCCC_HV_PV_TIME_ST, &res);
138 
139 	return (res.a0 == SMCCC_RET_SUCCESS);
140 }
141 
142 int __init pv_time_init(void)
143 {
144 	int ret;
145 
146 	if (!has_pv_steal_clock())
147 		return 0;
148 
149 	ret = pv_time_init_stolen_time();
150 	if (ret)
151 		return ret;
152 
153 	pv_ops.time.steal_clock = pv_steal_clock;
154 
155 	static_key_slow_inc(&paravirt_steal_enabled);
156 	if (steal_acc)
157 		static_key_slow_inc(&paravirt_steal_rq_enabled);
158 
159 	pr_info("using stolen time PV\n");
160 
161 	return 0;
162 }
163