blob: 75fed4460407dee05914177a86c234423a6efe54 [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>
Juergen Grossa0e2bf72021-03-11 15:23:09 +010021#include <linux/static_call.h>
Steven Pricee0685fa2019-10-21 16:28:23 +010022
Stefano Stabellinidfd57bc2015-11-23 10:33:49 +000023#include <asm/paravirt.h>
Steven Pricee0685fa2019-10-21 16:28:23 +010024#include <asm/pvclock-abi.h>
25#include <asm/smp_plat.h>
Stefano Stabellinidfd57bc2015-11-23 10:33:49 +000026
27struct static_key paravirt_steal_enabled;
28struct static_key paravirt_steal_rq_enabled;
29
Juergen Grossa0e2bf72021-03-11 15:23:09 +010030static u64 native_steal_clock(int cpu)
31{
32 return 0;
33}
34
35DEFINE_STATIC_CALL(pv_steal_clock, native_steal_clock);
Steven Pricee0685fa2019-10-21 16:28:23 +010036
37struct pv_time_stolen_time_region {
38 struct pvclock_vcpu_stolen_time *kaddr;
39};
40
41static DEFINE_PER_CPU(struct pv_time_stolen_time_region, stolen_time_region);
42
43static bool steal_acc = true;
44static int __init parse_no_stealacc(char *arg)
45{
46 steal_acc = false;
47 return 0;
48}
49
50early_param("no-steal-acc", parse_no_stealacc);
51
52/* return stolen time in ns by asking the hypervisor */
Juergen Grossa0e2bf72021-03-11 15:23:09 +010053static u64 para_steal_clock(int cpu)
Steven Pricee0685fa2019-10-21 16:28:23 +010054{
55 struct pv_time_stolen_time_region *reg;
56
57 reg = per_cpu_ptr(&stolen_time_region, cpu);
Andrew Jones75df5292020-09-16 17:45:30 +020058
59 /*
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.
63 */
64 if (!reg->kaddr)
Steven Pricee0685fa2019-10-21 16:28:23 +010065 return 0;
Steven Pricee0685fa2019-10-21 16:28:23 +010066
67 return le64_to_cpu(READ_ONCE(reg->kaddr->stolen_time));
68}
69
Andrew Jones75df5292020-09-16 17:45:30 +020070static int stolen_time_cpu_down_prepare(unsigned int cpu)
Steven Pricee0685fa2019-10-21 16:28:23 +010071{
72 struct pv_time_stolen_time_region *reg;
73
74 reg = this_cpu_ptr(&stolen_time_region);
75 if (!reg->kaddr)
76 return 0;
77
78 memunmap(reg->kaddr);
79 memset(reg, 0, sizeof(*reg));
80
81 return 0;
82}
83
Andrew Jones75df5292020-09-16 17:45:30 +020084static int stolen_time_cpu_online(unsigned int cpu)
Steven Pricee0685fa2019-10-21 16:28:23 +010085{
86 struct pv_time_stolen_time_region *reg;
87 struct arm_smccc_res res;
88
89 reg = this_cpu_ptr(&stolen_time_region);
90
91 arm_smccc_1_1_invoke(ARM_SMCCC_HV_PV_TIME_ST, &res);
92
93 if (res.a0 == SMCCC_RET_NOT_SUPPORTED)
94 return -EINVAL;
95
96 reg->kaddr = memremap(res.a0,
97 sizeof(struct pvclock_vcpu_stolen_time),
98 MEMREMAP_WB);
99
100 if (!reg->kaddr) {
101 pr_warn("Failed to map stolen time data structure\n");
102 return -ENOMEM;
103 }
104
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");
108 return -ENXIO;
109 }
110
111 return 0;
112}
113
Andrew Jones75df5292020-09-16 17:45:30 +0200114static int __init pv_time_init_stolen_time(void)
Steven Pricee0685fa2019-10-21 16:28:23 +0100115{
116 int ret;
117
Andrew Jones75df5292020-09-16 17:45:30 +0200118 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
119 "hypervisor/arm/pvtime:online",
120 stolen_time_cpu_online,
121 stolen_time_cpu_down_prepare);
Steven Pricee0685fa2019-10-21 16:28:23 +0100122 if (ret < 0)
123 return ret;
124 return 0;
125}
126
Andrew Jones75df5292020-09-16 17:45:30 +0200127static bool __init has_pv_steal_clock(void)
Steven Pricee0685fa2019-10-21 16:28:23 +0100128{
129 struct arm_smccc_res res;
130
131 /* To detect the presence of PV time support we require SMCCC 1.1+ */
Sudeep Hollaad5a57d2020-05-18 10:12:19 +0100132 if (arm_smccc_1_1_get_conduit() == SMCCC_CONDUIT_NONE)
Steven Pricee0685fa2019-10-21 16:28:23 +0100133 return false;
134
135 arm_smccc_1_1_invoke(ARM_SMCCC_ARCH_FEATURES_FUNC_ID,
136 ARM_SMCCC_HV_PV_TIME_FEATURES, &res);
137
138 if (res.a0 != SMCCC_RET_SUCCESS)
139 return false;
140
141 arm_smccc_1_1_invoke(ARM_SMCCC_HV_PV_TIME_FEATURES,
142 ARM_SMCCC_HV_PV_TIME_ST, &res);
143
144 return (res.a0 == SMCCC_RET_SUCCESS);
145}
146
147int __init pv_time_init(void)
148{
149 int ret;
150
151 if (!has_pv_steal_clock())
152 return 0;
153
154 ret = pv_time_init_stolen_time();
155 if (ret)
156 return ret;
157
Juergen Grossa0e2bf72021-03-11 15:23:09 +0100158 static_call_update(pv_steal_clock, para_steal_clock);
Steven Pricee0685fa2019-10-21 16:28:23 +0100159
160 static_key_slow_inc(&paravirt_steal_enabled);
161 if (steal_acc)
162 static_key_slow_inc(&paravirt_steal_rq_enabled);
163
164 pr_info("using stolen time PV\n");
165
166 return 0;
167}