blob: 693cce0be82dffb822cecd0c7e38d2821aff896c [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Paul Gortmakere6830142016-07-13 20:18:57 -04002#include <linux/export.h>
Borislav Petkov6ede31e2009-12-17 00:16:25 +01003#include <linux/preempt.h>
4#include <linux/smp.h>
5#include <asm/msr.h>
6
7static void __rdmsr_on_cpu(void *info)
8{
9 struct msr_info *rv = info;
10 struct msr *reg;
11 int this_cpu = raw_smp_processor_id();
12
13 if (rv->msrs)
14 reg = per_cpu_ptr(rv->msrs, this_cpu);
15 else
16 reg = &rv->reg;
17
18 rdmsr(rv->msr_no, reg->l, reg->h);
19}
20
21static void __wrmsr_on_cpu(void *info)
22{
23 struct msr_info *rv = info;
24 struct msr *reg;
25 int this_cpu = raw_smp_processor_id();
26
27 if (rv->msrs)
28 reg = per_cpu_ptr(rv->msrs, this_cpu);
29 else
30 reg = &rv->reg;
31
32 wrmsr(rv->msr_no, reg->l, reg->h);
33}
34
35int rdmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h)
36{
37 int err;
38 struct msr_info rv;
39
40 memset(&rv, 0, sizeof(rv));
41
42 rv.msr_no = msr_no;
43 err = smp_call_function_single(cpu, __rdmsr_on_cpu, &rv, 1);
44 *l = rv.reg.l;
45 *h = rv.reg.h;
46
47 return err;
48}
49EXPORT_SYMBOL(rdmsr_on_cpu);
50
Jacob Pan1a6b9912013-10-11 16:54:58 -070051int rdmsrl_on_cpu(unsigned int cpu, u32 msr_no, u64 *q)
52{
53 int err;
54 struct msr_info rv;
55
56 memset(&rv, 0, sizeof(rv));
57
58 rv.msr_no = msr_no;
59 err = smp_call_function_single(cpu, __rdmsr_on_cpu, &rv, 1);
60 *q = rv.reg.q;
61
62 return err;
63}
64EXPORT_SYMBOL(rdmsrl_on_cpu);
65
Borislav Petkov6ede31e2009-12-17 00:16:25 +010066int wrmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h)
67{
68 int err;
69 struct msr_info rv;
70
71 memset(&rv, 0, sizeof(rv));
72
73 rv.msr_no = msr_no;
74 rv.reg.l = l;
75 rv.reg.h = h;
76 err = smp_call_function_single(cpu, __wrmsr_on_cpu, &rv, 1);
77
78 return err;
79}
80EXPORT_SYMBOL(wrmsr_on_cpu);
81
Jacob Pan1a6b9912013-10-11 16:54:58 -070082int wrmsrl_on_cpu(unsigned int cpu, u32 msr_no, u64 q)
83{
84 int err;
85 struct msr_info rv;
86
87 memset(&rv, 0, sizeof(rv));
88
89 rv.msr_no = msr_no;
90 rv.reg.q = q;
91
92 err = smp_call_function_single(cpu, __wrmsr_on_cpu, &rv, 1);
93
94 return err;
95}
96EXPORT_SYMBOL(wrmsrl_on_cpu);
97
Borislav Petkov6ede31e2009-12-17 00:16:25 +010098static void __rwmsr_on_cpus(const struct cpumask *mask, u32 msr_no,
99 struct msr *msrs,
100 void (*msr_func) (void *info))
101{
102 struct msr_info rv;
103 int this_cpu;
104
105 memset(&rv, 0, sizeof(rv));
106
107 rv.msrs = msrs;
108 rv.msr_no = msr_no;
109
110 this_cpu = get_cpu();
111
112 if (cpumask_test_cpu(this_cpu, mask))
113 msr_func(&rv);
114
115 smp_call_function_many(mask, msr_func, &rv, 1);
116 put_cpu();
117}
118
119/* rdmsr on a bunch of CPUs
120 *
121 * @mask: which CPUs
122 * @msr_no: which MSR
123 * @msrs: array of MSR values
124 *
125 */
126void rdmsr_on_cpus(const struct cpumask *mask, u32 msr_no, struct msr *msrs)
127{
128 __rwmsr_on_cpus(mask, msr_no, msrs, __rdmsr_on_cpu);
129}
130EXPORT_SYMBOL(rdmsr_on_cpus);
131
132/*
133 * wrmsr on a bunch of CPUs
134 *
135 * @mask: which CPUs
136 * @msr_no: which MSR
137 * @msrs: array of MSR values
138 *
139 */
140void wrmsr_on_cpus(const struct cpumask *mask, u32 msr_no, struct msr *msrs)
141{
142 __rwmsr_on_cpus(mask, msr_no, msrs, __wrmsr_on_cpu);
143}
144EXPORT_SYMBOL(wrmsr_on_cpus);
145
146/* These "safe" variants are slower and should be used when the target MSR
147 may not actually exist. */
148static void __rdmsr_safe_on_cpu(void *info)
149{
150 struct msr_info *rv = info;
151
152 rv->err = rdmsr_safe(rv->msr_no, &rv->reg.l, &rv->reg.h);
153}
154
155static void __wrmsr_safe_on_cpu(void *info)
156{
157 struct msr_info *rv = info;
158
159 rv->err = wrmsr_safe(rv->msr_no, rv->reg.l, rv->reg.h);
160}
161
162int rdmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h)
163{
164 int err;
165 struct msr_info rv;
166
167 memset(&rv, 0, sizeof(rv));
168
169 rv.msr_no = msr_no;
170 err = smp_call_function_single(cpu, __rdmsr_safe_on_cpu, &rv, 1);
171 *l = rv.reg.l;
172 *h = rv.reg.h;
173
174 return err ? err : rv.err;
175}
176EXPORT_SYMBOL(rdmsr_safe_on_cpu);
177
178int wrmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h)
179{
180 int err;
181 struct msr_info rv;
182
183 memset(&rv, 0, sizeof(rv));
184
185 rv.msr_no = msr_no;
186 rv.reg.l = l;
187 rv.reg.h = h;
188 err = smp_call_function_single(cpu, __wrmsr_safe_on_cpu, &rv, 1);
189
190 return err ? err : rv.err;
191}
192EXPORT_SYMBOL(wrmsr_safe_on_cpu);
193
Jacob Pan1a6b9912013-10-11 16:54:58 -0700194int wrmsrl_safe_on_cpu(unsigned int cpu, u32 msr_no, u64 q)
195{
196 int err;
197 struct msr_info rv;
198
199 memset(&rv, 0, sizeof(rv));
200
201 rv.msr_no = msr_no;
202 rv.reg.q = q;
203
204 err = smp_call_function_single(cpu, __wrmsr_safe_on_cpu, &rv, 1);
205
206 return err ? err : rv.err;
207}
208EXPORT_SYMBOL(wrmsrl_safe_on_cpu);
209
210int rdmsrl_safe_on_cpu(unsigned int cpu, u32 msr_no, u64 *q)
211{
212 int err;
213 struct msr_info rv;
214
215 memset(&rv, 0, sizeof(rv));
216
217 rv.msr_no = msr_no;
218 err = smp_call_function_single(cpu, __rdmsr_safe_on_cpu, &rv, 1);
219 *q = rv.reg.q;
220
221 return err ? err : rv.err;
222}
223EXPORT_SYMBOL(rdmsrl_safe_on_cpu);
224
Borislav Petkov6ede31e2009-12-17 00:16:25 +0100225/*
226 * These variants are significantly slower, but allows control over
227 * the entire 32-bit GPR set.
228 */
229static void __rdmsr_safe_regs_on_cpu(void *info)
230{
231 struct msr_regs_info *rv = info;
232
233 rv->err = rdmsr_safe_regs(rv->regs);
234}
235
236static void __wrmsr_safe_regs_on_cpu(void *info)
237{
238 struct msr_regs_info *rv = info;
239
240 rv->err = wrmsr_safe_regs(rv->regs);
241}
242
243int rdmsr_safe_regs_on_cpu(unsigned int cpu, u32 *regs)
244{
245 int err;
246 struct msr_regs_info rv;
247
248 rv.regs = regs;
249 rv.err = -EIO;
250 err = smp_call_function_single(cpu, __rdmsr_safe_regs_on_cpu, &rv, 1);
251
252 return err ? err : rv.err;
253}
254EXPORT_SYMBOL(rdmsr_safe_regs_on_cpu);
255
256int wrmsr_safe_regs_on_cpu(unsigned int cpu, u32 *regs)
257{
258 int err;
259 struct msr_regs_info rv;
260
261 rv.regs = regs;
262 rv.err = -EIO;
263 err = smp_call_function_single(cpu, __wrmsr_safe_regs_on_cpu, &rv, 1);
264
265 return err ? err : rv.err;
266}
267EXPORT_SYMBOL(wrmsr_safe_regs_on_cpu);