Linux 5.7.6
[linux/fpc-iii.git] / arch / x86 / lib / msr-smp.c
blobfee8b9c0520c9954e3cf9093332e1a9b53009b8b
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/export.h>
3 #include <linux/preempt.h>
4 #include <linux/smp.h>
5 #include <linux/completion.h>
6 #include <asm/msr.h>
8 static void __rdmsr_on_cpu(void *info)
10 struct msr_info *rv = info;
11 struct msr *reg;
12 int this_cpu = raw_smp_processor_id();
14 if (rv->msrs)
15 reg = per_cpu_ptr(rv->msrs, this_cpu);
16 else
17 reg = &rv->reg;
19 rdmsr(rv->msr_no, reg->l, reg->h);
22 static void __wrmsr_on_cpu(void *info)
24 struct msr_info *rv = info;
25 struct msr *reg;
26 int this_cpu = raw_smp_processor_id();
28 if (rv->msrs)
29 reg = per_cpu_ptr(rv->msrs, this_cpu);
30 else
31 reg = &rv->reg;
33 wrmsr(rv->msr_no, reg->l, reg->h);
36 int rdmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h)
38 int err;
39 struct msr_info rv;
41 memset(&rv, 0, sizeof(rv));
43 rv.msr_no = msr_no;
44 err = smp_call_function_single(cpu, __rdmsr_on_cpu, &rv, 1);
45 *l = rv.reg.l;
46 *h = rv.reg.h;
48 return err;
50 EXPORT_SYMBOL(rdmsr_on_cpu);
52 int rdmsrl_on_cpu(unsigned int cpu, u32 msr_no, u64 *q)
54 int err;
55 struct msr_info rv;
57 memset(&rv, 0, sizeof(rv));
59 rv.msr_no = msr_no;
60 err = smp_call_function_single(cpu, __rdmsr_on_cpu, &rv, 1);
61 *q = rv.reg.q;
63 return err;
65 EXPORT_SYMBOL(rdmsrl_on_cpu);
67 int wrmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h)
69 int err;
70 struct msr_info rv;
72 memset(&rv, 0, sizeof(rv));
74 rv.msr_no = msr_no;
75 rv.reg.l = l;
76 rv.reg.h = h;
77 err = smp_call_function_single(cpu, __wrmsr_on_cpu, &rv, 1);
79 return err;
81 EXPORT_SYMBOL(wrmsr_on_cpu);
83 int wrmsrl_on_cpu(unsigned int cpu, u32 msr_no, u64 q)
85 int err;
86 struct msr_info rv;
88 memset(&rv, 0, sizeof(rv));
90 rv.msr_no = msr_no;
91 rv.reg.q = q;
93 err = smp_call_function_single(cpu, __wrmsr_on_cpu, &rv, 1);
95 return err;
97 EXPORT_SYMBOL(wrmsrl_on_cpu);
99 static void __rwmsr_on_cpus(const struct cpumask *mask, u32 msr_no,
100 struct msr *msrs,
101 void (*msr_func) (void *info))
103 struct msr_info rv;
104 int this_cpu;
106 memset(&rv, 0, sizeof(rv));
108 rv.msrs = msrs;
109 rv.msr_no = msr_no;
111 this_cpu = get_cpu();
113 if (cpumask_test_cpu(this_cpu, mask))
114 msr_func(&rv);
116 smp_call_function_many(mask, msr_func, &rv, 1);
117 put_cpu();
120 /* rdmsr on a bunch of CPUs
122 * @mask: which CPUs
123 * @msr_no: which MSR
124 * @msrs: array of MSR values
127 void rdmsr_on_cpus(const struct cpumask *mask, u32 msr_no, struct msr *msrs)
129 __rwmsr_on_cpus(mask, msr_no, msrs, __rdmsr_on_cpu);
131 EXPORT_SYMBOL(rdmsr_on_cpus);
134 * wrmsr on a bunch of CPUs
136 * @mask: which CPUs
137 * @msr_no: which MSR
138 * @msrs: array of MSR values
141 void wrmsr_on_cpus(const struct cpumask *mask, u32 msr_no, struct msr *msrs)
143 __rwmsr_on_cpus(mask, msr_no, msrs, __wrmsr_on_cpu);
145 EXPORT_SYMBOL(wrmsr_on_cpus);
147 struct msr_info_completion {
148 struct msr_info msr;
149 struct completion done;
152 /* These "safe" variants are slower and should be used when the target MSR
153 may not actually exist. */
154 static void __rdmsr_safe_on_cpu(void *info)
156 struct msr_info_completion *rv = info;
158 rv->msr.err = rdmsr_safe(rv->msr.msr_no, &rv->msr.reg.l, &rv->msr.reg.h);
159 complete(&rv->done);
162 static void __wrmsr_safe_on_cpu(void *info)
164 struct msr_info *rv = info;
166 rv->err = wrmsr_safe(rv->msr_no, rv->reg.l, rv->reg.h);
169 int rdmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h)
171 struct msr_info_completion rv;
172 call_single_data_t csd = {
173 .func = __rdmsr_safe_on_cpu,
174 .info = &rv,
176 int err;
178 memset(&rv, 0, sizeof(rv));
179 init_completion(&rv.done);
180 rv.msr.msr_no = msr_no;
182 err = smp_call_function_single_async(cpu, &csd);
183 if (!err) {
184 wait_for_completion(&rv.done);
185 err = rv.msr.err;
187 *l = rv.msr.reg.l;
188 *h = rv.msr.reg.h;
190 return err;
192 EXPORT_SYMBOL(rdmsr_safe_on_cpu);
194 int wrmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h)
196 int err;
197 struct msr_info rv;
199 memset(&rv, 0, sizeof(rv));
201 rv.msr_no = msr_no;
202 rv.reg.l = l;
203 rv.reg.h = h;
204 err = smp_call_function_single(cpu, __wrmsr_safe_on_cpu, &rv, 1);
206 return err ? err : rv.err;
208 EXPORT_SYMBOL(wrmsr_safe_on_cpu);
210 int wrmsrl_safe_on_cpu(unsigned int cpu, u32 msr_no, u64 q)
212 int err;
213 struct msr_info rv;
215 memset(&rv, 0, sizeof(rv));
217 rv.msr_no = msr_no;
218 rv.reg.q = q;
220 err = smp_call_function_single(cpu, __wrmsr_safe_on_cpu, &rv, 1);
222 return err ? err : rv.err;
224 EXPORT_SYMBOL(wrmsrl_safe_on_cpu);
226 int rdmsrl_safe_on_cpu(unsigned int cpu, u32 msr_no, u64 *q)
228 u32 low, high;
229 int err;
231 err = rdmsr_safe_on_cpu(cpu, msr_no, &low, &high);
232 *q = (u64)high << 32 | low;
234 return err;
236 EXPORT_SYMBOL(rdmsrl_safe_on_cpu);
239 * These variants are significantly slower, but allows control over
240 * the entire 32-bit GPR set.
242 static void __rdmsr_safe_regs_on_cpu(void *info)
244 struct msr_regs_info *rv = info;
246 rv->err = rdmsr_safe_regs(rv->regs);
249 static void __wrmsr_safe_regs_on_cpu(void *info)
251 struct msr_regs_info *rv = info;
253 rv->err = wrmsr_safe_regs(rv->regs);
256 int rdmsr_safe_regs_on_cpu(unsigned int cpu, u32 *regs)
258 int err;
259 struct msr_regs_info rv;
261 rv.regs = regs;
262 rv.err = -EIO;
263 err = smp_call_function_single(cpu, __rdmsr_safe_regs_on_cpu, &rv, 1);
265 return err ? err : rv.err;
267 EXPORT_SYMBOL(rdmsr_safe_regs_on_cpu);
269 int wrmsr_safe_regs_on_cpu(unsigned int cpu, u32 *regs)
271 int err;
272 struct msr_regs_info rv;
274 rv.regs = regs;
275 rv.err = -EIO;
276 err = smp_call_function_single(cpu, __wrmsr_safe_regs_on_cpu, &rv, 1);
278 return err ? err : rv.err;
280 EXPORT_SYMBOL(wrmsr_safe_regs_on_cpu);