blob: 41628b104b9e9ed0bd59724de5ef7d1d131f7aef [file] [log] [blame]
Alexey Dobriyanb077ffb2007-02-16 01:48:11 -08001#include <linux/module.h>
2#include <linux/preempt.h>
3#include <linux/smp.h>
4#include <asm/msr.h>
5
Alexey Dobriyanb077ffb2007-02-16 01:48:11 -08006struct msr_info {
7 u32 msr_no;
Borislav Petkov6bc10962009-05-22 12:12:01 +02008 struct msr reg;
Borislav Petkovb034c192009-05-22 13:52:19 +02009 struct msr *msrs;
10 int off;
Rudolf Marek4e9baad2007-05-08 17:22:01 +020011 int err;
Alexey Dobriyanb077ffb2007-02-16 01:48:11 -080012};
13
14static void __rdmsr_on_cpu(void *info)
15{
16 struct msr_info *rv = info;
Borislav Petkovb034c192009-05-22 13:52:19 +020017 struct msr *reg;
18 int this_cpu = raw_smp_processor_id();
Alexey Dobriyanb077ffb2007-02-16 01:48:11 -080019
Borislav Petkovb034c192009-05-22 13:52:19 +020020 if (rv->msrs)
21 reg = &rv->msrs[this_cpu - rv->off];
22 else
23 reg = &rv->reg;
24
25 rdmsr(rv->msr_no, reg->l, reg->h);
Alexey Dobriyanb077ffb2007-02-16 01:48:11 -080026}
27
H. Peter Anvinbdd31462008-08-25 17:44:03 -070028static void __wrmsr_on_cpu(void *info)
Alexey Dobriyanb077ffb2007-02-16 01:48:11 -080029{
Rudolf Marek4e9baad2007-05-08 17:22:01 +020030 struct msr_info *rv = info;
Borislav Petkovb034c192009-05-22 13:52:19 +020031 struct msr *reg;
32 int this_cpu = raw_smp_processor_id();
Rudolf Marek4e9baad2007-05-08 17:22:01 +020033
Borislav Petkovb034c192009-05-22 13:52:19 +020034 if (rv->msrs)
35 reg = &rv->msrs[this_cpu - rv->off];
36 else
37 reg = &rv->reg;
38
39 wrmsr(rv->msr_no, reg->l, reg->h);
Rudolf Marek4e9baad2007-05-08 17:22:01 +020040}
41
H. Peter Anvinbdd31462008-08-25 17:44:03 -070042int rdmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h)
Rudolf Marek4e9baad2007-05-08 17:22:01 +020043{
H. Peter Anvinbdd31462008-08-25 17:44:03 -070044 int err;
Avi Kivity5f1f9352007-10-17 18:04:38 +020045 struct msr_info rv;
Alexey Dobriyanb077ffb2007-02-16 01:48:11 -080046
Borislav Petkovb034c192009-05-22 13:52:19 +020047 memset(&rv, 0, sizeof(rv));
48
Avi Kivity5f1f9352007-10-17 18:04:38 +020049 rv.msr_no = msr_no;
H. Peter Anvinbdd31462008-08-25 17:44:03 -070050 err = smp_call_function_single(cpu, __rdmsr_on_cpu, &rv, 1);
Borislav Petkov6bc10962009-05-22 12:12:01 +020051 *l = rv.reg.l;
52 *h = rv.reg.h;
Avi Kivity5f1f9352007-10-17 18:04:38 +020053
Rudolf Marek4e9baad2007-05-08 17:22:01 +020054 return err;
Alexey Dobriyanb077ffb2007-02-16 01:48:11 -080055}
Borislav Petkovb034c192009-05-22 13:52:19 +020056EXPORT_SYMBOL(rdmsr_on_cpu);
Alexey Dobriyanb077ffb2007-02-16 01:48:11 -080057
H. Peter Anvinbdd31462008-08-25 17:44:03 -070058int wrmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h)
59{
60 int err;
61 struct msr_info rv;
62
Borislav Petkovb034c192009-05-22 13:52:19 +020063 memset(&rv, 0, sizeof(rv));
64
H. Peter Anvinbdd31462008-08-25 17:44:03 -070065 rv.msr_no = msr_no;
Borislav Petkov6bc10962009-05-22 12:12:01 +020066 rv.reg.l = l;
67 rv.reg.h = h;
H. Peter Anvinbdd31462008-08-25 17:44:03 -070068 err = smp_call_function_single(cpu, __wrmsr_on_cpu, &rv, 1);
69
70 return err;
71}
Borislav Petkovb034c192009-05-22 13:52:19 +020072EXPORT_SYMBOL(wrmsr_on_cpu);
73
Borislav Petkovb8a47542009-07-30 11:10:02 +020074static void __rwmsr_on_cpus(const struct cpumask *mask, u32 msr_no,
75 struct msr *msrs,
76 void (*msr_func) (void *info))
Borislav Petkovb034c192009-05-22 13:52:19 +020077{
78 struct msr_info rv;
79 int this_cpu;
80
81 memset(&rv, 0, sizeof(rv));
82
83 rv.off = cpumask_first(mask);
84 rv.msrs = msrs;
85 rv.msr_no = msr_no;
86
Borislav Petkovbab9a3d2009-07-30 11:10:01 +020087 this_cpu = get_cpu();
88
89 if (cpumask_test_cpu(this_cpu, mask))
Borislav Petkovb8a47542009-07-30 11:10:02 +020090 msr_func(&rv);
Borislav Petkovb034c192009-05-22 13:52:19 +020091
Borislav Petkovb8a47542009-07-30 11:10:02 +020092 smp_call_function_many(mask, msr_func, &rv, 1);
Borislav Petkovbab9a3d2009-07-30 11:10:01 +020093 put_cpu();
Borislav Petkovb034c192009-05-22 13:52:19 +020094}
Borislav Petkovb8a47542009-07-30 11:10:02 +020095
96/* rdmsr on a bunch of CPUs
97 *
98 * @mask: which CPUs
99 * @msr_no: which MSR
100 * @msrs: array of MSR values
101 *
102 */
103void rdmsr_on_cpus(const struct cpumask *mask, u32 msr_no, struct msr *msrs)
104{
105 __rwmsr_on_cpus(mask, msr_no, msrs, __rdmsr_on_cpu);
106}
Borislav Petkovb034c192009-05-22 13:52:19 +0200107EXPORT_SYMBOL(rdmsr_on_cpus);
108
109/*
110 * wrmsr on a bunch of CPUs
111 *
112 * @mask: which CPUs
113 * @msr_no: which MSR
114 * @msrs: array of MSR values
115 *
116 */
Borislav Petkovb8a47542009-07-30 11:10:02 +0200117void wrmsr_on_cpus(const struct cpumask *mask, u32 msr_no, struct msr *msrs)
Borislav Petkovb034c192009-05-22 13:52:19 +0200118{
Borislav Petkovb8a47542009-07-30 11:10:02 +0200119 __rwmsr_on_cpus(mask, msr_no, msrs, __wrmsr_on_cpu);
Borislav Petkovb034c192009-05-22 13:52:19 +0200120}
121EXPORT_SYMBOL(wrmsr_on_cpus);
H. Peter Anvinbdd31462008-08-25 17:44:03 -0700122
123/* These "safe" variants are slower and should be used when the target MSR
124 may not actually exist. */
125static void __rdmsr_safe_on_cpu(void *info)
Alexey Dobriyanb077ffb2007-02-16 01:48:11 -0800126{
127 struct msr_info *rv = info;
128
Borislav Petkov6bc10962009-05-22 12:12:01 +0200129 rv->err = rdmsr_safe(rv->msr_no, &rv->reg.l, &rv->reg.h);
Alexey Dobriyanb077ffb2007-02-16 01:48:11 -0800130}
131
Rudolf Marek4e9baad2007-05-08 17:22:01 +0200132static void __wrmsr_safe_on_cpu(void *info)
Alexey Dobriyanb077ffb2007-02-16 01:48:11 -0800133{
Rudolf Marek4e9baad2007-05-08 17:22:01 +0200134 struct msr_info *rv = info;
135
Borislav Petkov6bc10962009-05-22 12:12:01 +0200136 rv->err = wrmsr_safe(rv->msr_no, rv->reg.l, rv->reg.h);
Rudolf Marek4e9baad2007-05-08 17:22:01 +0200137}
138
H. Peter Anvinbdd31462008-08-25 17:44:03 -0700139int rdmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h)
Rudolf Marek4e9baad2007-05-08 17:22:01 +0200140{
H. Peter Anvinbdd31462008-08-25 17:44:03 -0700141 int err;
142 struct msr_info rv;
143
Borislav Petkovb034c192009-05-22 13:52:19 +0200144 memset(&rv, 0, sizeof(rv));
145
H. Peter Anvinbdd31462008-08-25 17:44:03 -0700146 rv.msr_no = msr_no;
147 err = smp_call_function_single(cpu, __rdmsr_safe_on_cpu, &rv, 1);
Borislav Petkov6bc10962009-05-22 12:12:01 +0200148 *l = rv.reg.l;
149 *h = rv.reg.h;
H. Peter Anvinbdd31462008-08-25 17:44:03 -0700150
151 return err ? err : rv.err;
152}
Borislav Petkovb034c192009-05-22 13:52:19 +0200153EXPORT_SYMBOL(rdmsr_safe_on_cpu);
H. Peter Anvinbdd31462008-08-25 17:44:03 -0700154
155int wrmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h)
156{
157 int err;
Avi Kivity5f1f9352007-10-17 18:04:38 +0200158 struct msr_info rv;
Alexey Dobriyanb077ffb2007-02-16 01:48:11 -0800159
Borislav Petkovb034c192009-05-22 13:52:19 +0200160 memset(&rv, 0, sizeof(rv));
161
Avi Kivity5f1f9352007-10-17 18:04:38 +0200162 rv.msr_no = msr_no;
Borislav Petkov6bc10962009-05-22 12:12:01 +0200163 rv.reg.l = l;
164 rv.reg.h = h;
H. Peter Anvinbdd31462008-08-25 17:44:03 -0700165 err = smp_call_function_single(cpu, __wrmsr_safe_on_cpu, &rv, 1);
Avi Kivity5f1f9352007-10-17 18:04:38 +0200166
H. Peter Anvinbdd31462008-08-25 17:44:03 -0700167 return err ? err : rv.err;
Alexey Dobriyanb077ffb2007-02-16 01:48:11 -0800168}
Rudolf Marek4e9baad2007-05-08 17:22:01 +0200169EXPORT_SYMBOL(wrmsr_safe_on_cpu);
H. Peter Anvin8b956bf2009-08-31 14:13:48 -0700170
171/*
172 * These variants are significantly slower, but allows control over
173 * the entire 32-bit GPR set.
174 */
175struct msr_regs_info {
176 u32 *regs;
177 int err;
178};
179
180static void __rdmsr_safe_regs_on_cpu(void *info)
181{
182 struct msr_regs_info *rv = info;
183
184 rv->err = rdmsr_safe_regs(rv->regs);
185}
186
187static void __wrmsr_safe_regs_on_cpu(void *info)
188{
189 struct msr_regs_info *rv = info;
190
191 rv->err = wrmsr_safe_regs(rv->regs);
192}
193
194int rdmsr_safe_regs_on_cpu(unsigned int cpu, u32 *regs)
195{
196 int err;
197 struct msr_regs_info rv;
198
199 rv.regs = regs;
200 rv.err = -EIO;
201 err = smp_call_function_single(cpu, __rdmsr_safe_regs_on_cpu, &rv, 1);
202
203 return err ? err : rv.err;
204}
205EXPORT_SYMBOL(rdmsr_safe_regs_on_cpu);
206
207int wrmsr_safe_regs_on_cpu(unsigned int cpu, u32 *regs)
208{
209 int err;
210 struct msr_regs_info rv;
211
212 rv.regs = regs;
213 rv.err = -EIO;
214 err = smp_call_function_single(cpu, __wrmsr_safe_regs_on_cpu, &rv, 1);
215
216 return err ? err : rv.err;
217}
218EXPORT_SYMBOL(wrmsr_safe_regs_on_cpu);