1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/export.h>
3 #include <linux/preempt.h>
5 #include <linux/completion.h>
8 static void __rdmsr_on_cpu(void *info
)
10 struct msr_info
*rv
= info
;
12 int this_cpu
= raw_smp_processor_id();
15 reg
= per_cpu_ptr(rv
->msrs
, this_cpu
);
19 rdmsr(rv
->msr_no
, reg
->l
, reg
->h
);
22 static void __wrmsr_on_cpu(void *info
)
24 struct msr_info
*rv
= info
;
26 int this_cpu
= raw_smp_processor_id();
29 reg
= per_cpu_ptr(rv
->msrs
, this_cpu
);
33 wrmsr(rv
->msr_no
, reg
->l
, reg
->h
);
36 int rdmsr_on_cpu(unsigned int cpu
, u32 msr_no
, u32
*l
, u32
*h
)
41 memset(&rv
, 0, sizeof(rv
));
44 err
= smp_call_function_single(cpu
, __rdmsr_on_cpu
, &rv
, 1);
50 EXPORT_SYMBOL(rdmsr_on_cpu
);
52 int rdmsrl_on_cpu(unsigned int cpu
, u32 msr_no
, u64
*q
)
57 memset(&rv
, 0, sizeof(rv
));
60 err
= smp_call_function_single(cpu
, __rdmsr_on_cpu
, &rv
, 1);
65 EXPORT_SYMBOL(rdmsrl_on_cpu
);
67 int wrmsr_on_cpu(unsigned int cpu
, u32 msr_no
, u32 l
, u32 h
)
72 memset(&rv
, 0, sizeof(rv
));
77 err
= smp_call_function_single(cpu
, __wrmsr_on_cpu
, &rv
, 1);
81 EXPORT_SYMBOL(wrmsr_on_cpu
);
83 int wrmsrl_on_cpu(unsigned int cpu
, u32 msr_no
, u64 q
)
88 memset(&rv
, 0, sizeof(rv
));
93 err
= smp_call_function_single(cpu
, __wrmsr_on_cpu
, &rv
, 1);
97 EXPORT_SYMBOL(wrmsrl_on_cpu
);
99 static void __rwmsr_on_cpus(const struct cpumask
*mask
, u32 msr_no
,
101 void (*msr_func
) (void *info
))
106 memset(&rv
, 0, sizeof(rv
));
111 this_cpu
= get_cpu();
113 if (cpumask_test_cpu(this_cpu
, mask
))
116 smp_call_function_many(mask
, msr_func
, &rv
, 1);
120 /* rdmsr on a bunch of CPUs
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
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
{
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
);
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
;
175 INIT_CSD(&csd
, __rdmsr_safe_on_cpu
, &rv
);
177 memset(&rv
, 0, sizeof(rv
));
178 init_completion(&rv
.done
);
179 rv
.msr
.msr_no
= msr_no
;
181 err
= smp_call_function_single_async(cpu
, &csd
);
183 wait_for_completion(&rv
.done
);
191 EXPORT_SYMBOL(rdmsr_safe_on_cpu
);
193 int wrmsr_safe_on_cpu(unsigned int cpu
, u32 msr_no
, u32 l
, u32 h
)
198 memset(&rv
, 0, sizeof(rv
));
203 err
= smp_call_function_single(cpu
, __wrmsr_safe_on_cpu
, &rv
, 1);
205 return err
? err
: rv
.err
;
207 EXPORT_SYMBOL(wrmsr_safe_on_cpu
);
209 int wrmsrl_safe_on_cpu(unsigned int cpu
, u32 msr_no
, u64 q
)
214 memset(&rv
, 0, sizeof(rv
));
219 err
= smp_call_function_single(cpu
, __wrmsr_safe_on_cpu
, &rv
, 1);
221 return err
? err
: rv
.err
;
223 EXPORT_SYMBOL(wrmsrl_safe_on_cpu
);
225 int rdmsrl_safe_on_cpu(unsigned int cpu
, u32 msr_no
, u64
*q
)
230 err
= rdmsr_safe_on_cpu(cpu
, msr_no
, &low
, &high
);
231 *q
= (u64
)high
<< 32 | low
;
235 EXPORT_SYMBOL(rdmsrl_safe_on_cpu
);
238 * These variants are significantly slower, but allows control over
239 * the entire 32-bit GPR set.
241 static void __rdmsr_safe_regs_on_cpu(void *info
)
243 struct msr_regs_info
*rv
= info
;
245 rv
->err
= rdmsr_safe_regs(rv
->regs
);
248 static void __wrmsr_safe_regs_on_cpu(void *info
)
250 struct msr_regs_info
*rv
= info
;
252 rv
->err
= wrmsr_safe_regs(rv
->regs
);
255 int rdmsr_safe_regs_on_cpu(unsigned int cpu
, u32
*regs
)
258 struct msr_regs_info rv
;
262 err
= smp_call_function_single(cpu
, __rdmsr_safe_regs_on_cpu
, &rv
, 1);
264 return err
? err
: rv
.err
;
266 EXPORT_SYMBOL(rdmsr_safe_regs_on_cpu
);
268 int wrmsr_safe_regs_on_cpu(unsigned int cpu
, u32
*regs
)
271 struct msr_regs_info rv
;
275 err
= smp_call_function_single(cpu
, __wrmsr_safe_regs_on_cpu
, &rv
, 1);
277 return err
? err
: rv
.err
;
279 EXPORT_SYMBOL(wrmsr_safe_regs_on_cpu
);