x86/amd-iommu: Add per IOMMU reference counting
[linux/fpc-iii.git] / arch / x86 / lib / msr.c
blob33a1e3ca22d81e71240c1509f42c7d4b69d2c9e2
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 /* rdmsr on a bunch of CPUs
76 * @mask: which CPUs
77 * @msr_no: which MSR
78 * @msrs: array of MSR values
81 void rdmsr_on_cpus(const cpumask_t *mask, u32 msr_no, struct msr *msrs)
83 struct msr_info rv;
84 int this_cpu;
86 memset(&rv, 0, sizeof(rv));
88 rv.off = cpumask_first(mask);
89 rv.msrs = msrs;
90 rv.msr_no = msr_no;
92 this_cpu = get_cpu();
94 if (cpumask_test_cpu(this_cpu, mask))
95 __rdmsr_on_cpu(&rv);
97 smp_call_function_many(mask, __rdmsr_on_cpu, &rv, 1);
98 put_cpu();
100 EXPORT_SYMBOL(rdmsr_on_cpus);
103 * wrmsr on a bunch of CPUs
105 * @mask: which CPUs
106 * @msr_no: which MSR
107 * @msrs: array of MSR values
110 void wrmsr_on_cpus(const cpumask_t *mask, u32 msr_no, struct msr *msrs)
112 struct msr_info rv;
113 int this_cpu;
115 memset(&rv, 0, sizeof(rv));
117 rv.off = cpumask_first(mask);
118 rv.msrs = msrs;
119 rv.msr_no = msr_no;
121 this_cpu = get_cpu();
123 if (cpumask_test_cpu(this_cpu, mask))
124 __wrmsr_on_cpu(&rv);
126 smp_call_function_many(mask, __wrmsr_on_cpu, &rv, 1);
127 put_cpu();
129 EXPORT_SYMBOL(wrmsr_on_cpus);
131 /* These "safe" variants are slower and should be used when the target MSR
132 may not actually exist. */
133 static void __rdmsr_safe_on_cpu(void *info)
135 struct msr_info *rv = info;
137 rv->err = rdmsr_safe(rv->msr_no, &rv->reg.l, &rv->reg.h);
140 static void __wrmsr_safe_on_cpu(void *info)
142 struct msr_info *rv = info;
144 rv->err = wrmsr_safe(rv->msr_no, rv->reg.l, rv->reg.h);
147 int rdmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h)
149 int err;
150 struct msr_info rv;
152 memset(&rv, 0, sizeof(rv));
154 rv.msr_no = msr_no;
155 err = smp_call_function_single(cpu, __rdmsr_safe_on_cpu, &rv, 1);
156 *l = rv.reg.l;
157 *h = rv.reg.h;
159 return err ? err : rv.err;
161 EXPORT_SYMBOL(rdmsr_safe_on_cpu);
163 int wrmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h)
165 int err;
166 struct msr_info rv;
168 memset(&rv, 0, sizeof(rv));
170 rv.msr_no = msr_no;
171 rv.reg.l = l;
172 rv.reg.h = h;
173 err = smp_call_function_single(cpu, __wrmsr_safe_on_cpu, &rv, 1);
175 return err ? err : rv.err;
177 EXPORT_SYMBOL(wrmsr_safe_on_cpu);
180 * These variants are significantly slower, but allows control over
181 * the entire 32-bit GPR set.
183 struct msr_regs_info {
184 u32 *regs;
185 int err;
188 static void __rdmsr_safe_regs_on_cpu(void *info)
190 struct msr_regs_info *rv = info;
192 rv->err = rdmsr_safe_regs(rv->regs);
195 static void __wrmsr_safe_regs_on_cpu(void *info)
197 struct msr_regs_info *rv = info;
199 rv->err = wrmsr_safe_regs(rv->regs);
202 int rdmsr_safe_regs_on_cpu(unsigned int cpu, u32 *regs)
204 int err;
205 struct msr_regs_info rv;
207 rv.regs = regs;
208 rv.err = -EIO;
209 err = smp_call_function_single(cpu, __rdmsr_safe_regs_on_cpu, &rv, 1);
211 return err ? err : rv.err;
213 EXPORT_SYMBOL(rdmsr_safe_regs_on_cpu);
215 int wrmsr_safe_regs_on_cpu(unsigned int cpu, u32 *regs)
217 int err;
218 struct msr_regs_info rv;
220 rv.regs = regs;
221 rv.err = -EIO;
222 err = smp_call_function_single(cpu, __wrmsr_safe_regs_on_cpu, &rv, 1);
224 return err ? err : rv.err;
226 EXPORT_SYMBOL(wrmsr_safe_regs_on_cpu);