On Tue, Nov 06, 2007 at 02:33:53AM -0800, akpm@linux-foundation.org wrote:
[mmotm.git] / arch / x86 / lib / msr.c
blob41628b104b9e9ed0bd59724de5ef7d1d131f7aef
1 #include <linux/module.h>
2 #include <linux/preempt.h>
3 #include <linux/smp.h>
4 #include <asm/msr.h>
6 struct msr_info {
7 u32 msr_no;
8 struct msr reg;
9 struct msr *msrs;
10 int off;
11 int err;
14 static void __rdmsr_on_cpu(void *info)
16 struct msr_info *rv = info;
17 struct msr *reg;
18 int this_cpu = raw_smp_processor_id();
20 if (rv->msrs)
21 reg = &rv->msrs[this_cpu - rv->off];
22 else
23 reg = &rv->reg;
25 rdmsr(rv->msr_no, reg->l, reg->h);
28 static void __wrmsr_on_cpu(void *info)
30 struct msr_info *rv = info;
31 struct msr *reg;
32 int this_cpu = raw_smp_processor_id();
34 if (rv->msrs)
35 reg = &rv->msrs[this_cpu - rv->off];
36 else
37 reg = &rv->reg;
39 wrmsr(rv->msr_no, reg->l, reg->h);
42 int rdmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h)
44 int err;
45 struct msr_info rv;
47 memset(&rv, 0, sizeof(rv));
49 rv.msr_no = msr_no;
50 err = smp_call_function_single(cpu, __rdmsr_on_cpu, &rv, 1);
51 *l = rv.reg.l;
52 *h = rv.reg.h;
54 return err;
56 EXPORT_SYMBOL(rdmsr_on_cpu);
58 int wrmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h)
60 int err;
61 struct msr_info rv;
63 memset(&rv, 0, sizeof(rv));
65 rv.msr_no = msr_no;
66 rv.reg.l = l;
67 rv.reg.h = h;
68 err = smp_call_function_single(cpu, __wrmsr_on_cpu, &rv, 1);
70 return err;
72 EXPORT_SYMBOL(wrmsr_on_cpu);
74 static void __rwmsr_on_cpus(const struct cpumask *mask, u32 msr_no,
75 struct msr *msrs,
76 void (*msr_func) (void *info))
78 struct msr_info rv;
79 int this_cpu;
81 memset(&rv, 0, sizeof(rv));
83 rv.off = cpumask_first(mask);
84 rv.msrs = msrs;
85 rv.msr_no = msr_no;
87 this_cpu = get_cpu();
89 if (cpumask_test_cpu(this_cpu, mask))
90 msr_func(&rv);
92 smp_call_function_many(mask, msr_func, &rv, 1);
93 put_cpu();
96 /* rdmsr on a bunch of CPUs
98 * @mask: which CPUs
99 * @msr_no: which MSR
100 * @msrs: array of MSR values
103 void rdmsr_on_cpus(const struct cpumask *mask, u32 msr_no, struct msr *msrs)
105 __rwmsr_on_cpus(mask, msr_no, msrs, __rdmsr_on_cpu);
107 EXPORT_SYMBOL(rdmsr_on_cpus);
110 * wrmsr on a bunch of CPUs
112 * @mask: which CPUs
113 * @msr_no: which MSR
114 * @msrs: array of MSR values
117 void wrmsr_on_cpus(const struct cpumask *mask, u32 msr_no, struct msr *msrs)
119 __rwmsr_on_cpus(mask, msr_no, msrs, __wrmsr_on_cpu);
121 EXPORT_SYMBOL(wrmsr_on_cpus);
123 /* These "safe" variants are slower and should be used when the target MSR
124 may not actually exist. */
125 static void __rdmsr_safe_on_cpu(void *info)
127 struct msr_info *rv = info;
129 rv->err = rdmsr_safe(rv->msr_no, &rv->reg.l, &rv->reg.h);
132 static void __wrmsr_safe_on_cpu(void *info)
134 struct msr_info *rv = info;
136 rv->err = wrmsr_safe(rv->msr_no, rv->reg.l, rv->reg.h);
139 int rdmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h)
141 int err;
142 struct msr_info rv;
144 memset(&rv, 0, sizeof(rv));
146 rv.msr_no = msr_no;
147 err = smp_call_function_single(cpu, __rdmsr_safe_on_cpu, &rv, 1);
148 *l = rv.reg.l;
149 *h = rv.reg.h;
151 return err ? err : rv.err;
153 EXPORT_SYMBOL(rdmsr_safe_on_cpu);
155 int wrmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h)
157 int err;
158 struct msr_info rv;
160 memset(&rv, 0, sizeof(rv));
162 rv.msr_no = msr_no;
163 rv.reg.l = l;
164 rv.reg.h = h;
165 err = smp_call_function_single(cpu, __wrmsr_safe_on_cpu, &rv, 1);
167 return err ? err : rv.err;
169 EXPORT_SYMBOL(wrmsr_safe_on_cpu);
172 * These variants are significantly slower, but allows control over
173 * the entire 32-bit GPR set.
175 struct msr_regs_info {
176 u32 *regs;
177 int err;
180 static void __rdmsr_safe_regs_on_cpu(void *info)
182 struct msr_regs_info *rv = info;
184 rv->err = rdmsr_safe_regs(rv->regs);
187 static void __wrmsr_safe_regs_on_cpu(void *info)
189 struct msr_regs_info *rv = info;
191 rv->err = wrmsr_safe_regs(rv->regs);
194 int rdmsr_safe_regs_on_cpu(unsigned int cpu, u32 *regs)
196 int err;
197 struct msr_regs_info rv;
199 rv.regs = regs;
200 rv.err = -EIO;
201 err = smp_call_function_single(cpu, __rdmsr_safe_regs_on_cpu, &rv, 1);
203 return err ? err : rv.err;
205 EXPORT_SYMBOL(rdmsr_safe_regs_on_cpu);
207 int wrmsr_safe_regs_on_cpu(unsigned int cpu, u32 *regs)
209 int err;
210 struct msr_regs_info rv;
212 rv.regs = regs;
213 rv.err = -EIO;
214 err = smp_call_function_single(cpu, __wrmsr_safe_regs_on_cpu, &rv, 1);
216 return err ? err : rv.err;
218 EXPORT_SYMBOL(wrmsr_safe_regs_on_cpu);