blob: c07d7a03494108a9a75fea9e9bb6b98830b3d0fa [file] [log] [blame]
Thomas Gleixner1802d0b2019-05-27 08:55:21 +02001// SPDX-License-Identifier: GPL-2.0-only
Stefano Stabellinidfd57bc2015-11-23 10:33:49 +00002/*
Stefano Stabellinidfd57bc2015-11-23 10:33:49 +00003 *
4 * Copyright (C) 2013 Citrix Systems
5 *
6 * Author: Stefano Stabellini <stefano.stabellini@eu.citrix.com>
7 */
8
Steven Pricee0685fa2019-10-21 16:28:23 +01009#define pr_fmt(fmt) "arm-pv: " fmt
10
11#include <linux/arm-smccc.h>
12#include <linux/cpuhotplug.h>
Stefano Stabellinidfd57bc2015-11-23 10:33:49 +000013#include <linux/export.h>
Steven Pricee0685fa2019-10-21 16:28:23 +010014#include <linux/io.h>
Stefano Stabellinidfd57bc2015-11-23 10:33:49 +000015#include <linux/jump_label.h>
Steven Pricee0685fa2019-10-21 16:28:23 +010016#include <linux/printk.h>
17#include <linux/psci.h>
18#include <linux/reboot.h>
19#include <linux/slab.h>
Stefano Stabellinidfd57bc2015-11-23 10:33:49 +000020#include <linux/types.h>
Steven Pricee0685fa2019-10-21 16:28:23 +010021
Stefano Stabellinidfd57bc2015-11-23 10:33:49 +000022#include <asm/paravirt.h>
Steven Pricee0685fa2019-10-21 16:28:23 +010023#include <asm/pvclock-abi.h>
24#include <asm/smp_plat.h>
Stefano Stabellinidfd57bc2015-11-23 10:33:49 +000025
26struct static_key paravirt_steal_enabled;
27struct static_key paravirt_steal_rq_enabled;
28
Juergen Gross5c835112018-08-28 09:40:19 +020029struct paravirt_patch_template pv_ops;
30EXPORT_SYMBOL_GPL(pv_ops);
Steven Pricee0685fa2019-10-21 16:28:23 +010031
32struct pv_time_stolen_time_region {
33 struct pvclock_vcpu_stolen_time *kaddr;
34};
35
36static DEFINE_PER_CPU(struct pv_time_stolen_time_region, stolen_time_region);
37
38static bool steal_acc = true;
39static int __init parse_no_stealacc(char *arg)
40{
41 steal_acc = false;
42 return 0;
43}
44
45early_param("no-steal-acc", parse_no_stealacc);
46
47/* return stolen time in ns by asking the hypervisor */
48static 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);
Andrew Jones75df5292020-09-16 17:45:30 +020053
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)
Steven Pricee0685fa2019-10-21 16:28:23 +010060 return 0;
Steven Pricee0685fa2019-10-21 16:28:23 +010061
62 return le64_to_cpu(READ_ONCE(reg->kaddr->stolen_time));
63}
64
Andrew Jones75df5292020-09-16 17:45:30 +020065static int stolen_time_cpu_down_prepare(unsigned int cpu)
Steven Pricee0685fa2019-10-21 16:28:23 +010066{
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
Andrew Jones75df5292020-09-16 17:45:30 +020079static int stolen_time_cpu_online(unsigned int cpu)
Steven Pricee0685fa2019-10-21 16:28:23 +010080{
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
Andrew Jones75df5292020-09-16 17:45:30 +0200109static int __init pv_time_init_stolen_time(void)
Steven Pricee0685fa2019-10-21 16:28:23 +0100110{
111 int ret;
112
Andrew Jones75df5292020-09-16 17:45:30 +0200113 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
114 "hypervisor/arm/pvtime:online",
115 stolen_time_cpu_online,
116 stolen_time_cpu_down_prepare);
Steven Pricee0685fa2019-10-21 16:28:23 +0100117 if (ret < 0)
118 return ret;
119 return 0;
120}
121
Andrew Jones75df5292020-09-16 17:45:30 +0200122static bool __init has_pv_steal_clock(void)
Steven Pricee0685fa2019-10-21 16:28:23 +0100123{
124 struct arm_smccc_res res;
125
126 /* To detect the presence of PV time support we require SMCCC 1.1+ */
Sudeep Hollaad5a57d2020-05-18 10:12:19 +0100127 if (arm_smccc_1_1_get_conduit() == SMCCC_CONDUIT_NONE)
Steven Pricee0685fa2019-10-21 16:28:23 +0100128 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
142int __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}