Alexey Dobriyan | b077ffb | 2007-02-16 01:48:11 -0800 | [diff] [blame] | 1 | #include <linux/module.h> |
| 2 | #include <linux/preempt.h> |
| 3 | #include <linux/smp.h> |
| 4 | #include <asm/msr.h> |
| 5 | |
Alexey Dobriyan | b077ffb | 2007-02-16 01:48:11 -0800 | [diff] [blame] | 6 | struct msr_info { |
| 7 | u32 msr_no; |
Borislav Petkov | 6bc1096 | 2009-05-22 12:12:01 +0200 | [diff] [blame] | 8 | struct msr reg; |
Borislav Petkov | b034c19 | 2009-05-22 13:52:19 +0200 | [diff] [blame] | 9 | struct msr *msrs; |
| 10 | int off; |
Rudolf Marek | 4e9baad | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 11 | int err; |
Alexey Dobriyan | b077ffb | 2007-02-16 01:48:11 -0800 | [diff] [blame] | 12 | }; |
| 13 | |
| 14 | static void __rdmsr_on_cpu(void *info) |
| 15 | { |
| 16 | struct msr_info *rv = info; |
Borislav Petkov | b034c19 | 2009-05-22 13:52:19 +0200 | [diff] [blame] | 17 | struct msr *reg; |
| 18 | int this_cpu = raw_smp_processor_id(); |
Alexey Dobriyan | b077ffb | 2007-02-16 01:48:11 -0800 | [diff] [blame] | 19 | |
Borislav Petkov | b034c19 | 2009-05-22 13:52:19 +0200 | [diff] [blame] | 20 | 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 Dobriyan | b077ffb | 2007-02-16 01:48:11 -0800 | [diff] [blame] | 26 | } |
| 27 | |
H. Peter Anvin | bdd3146 | 2008-08-25 17:44:03 -0700 | [diff] [blame] | 28 | static void __wrmsr_on_cpu(void *info) |
Alexey Dobriyan | b077ffb | 2007-02-16 01:48:11 -0800 | [diff] [blame] | 29 | { |
Rudolf Marek | 4e9baad | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 30 | struct msr_info *rv = info; |
Borislav Petkov | b034c19 | 2009-05-22 13:52:19 +0200 | [diff] [blame] | 31 | struct msr *reg; |
| 32 | int this_cpu = raw_smp_processor_id(); |
Rudolf Marek | 4e9baad | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 33 | |
Borislav Petkov | b034c19 | 2009-05-22 13:52:19 +0200 | [diff] [blame] | 34 | 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 Marek | 4e9baad | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 40 | } |
| 41 | |
H. Peter Anvin | bdd3146 | 2008-08-25 17:44:03 -0700 | [diff] [blame] | 42 | int rdmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h) |
Rudolf Marek | 4e9baad | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 43 | { |
H. Peter Anvin | bdd3146 | 2008-08-25 17:44:03 -0700 | [diff] [blame] | 44 | int err; |
Avi Kivity | 5f1f935 | 2007-10-17 18:04:38 +0200 | [diff] [blame] | 45 | struct msr_info rv; |
Alexey Dobriyan | b077ffb | 2007-02-16 01:48:11 -0800 | [diff] [blame] | 46 | |
Borislav Petkov | b034c19 | 2009-05-22 13:52:19 +0200 | [diff] [blame] | 47 | memset(&rv, 0, sizeof(rv)); |
| 48 | |
Avi Kivity | 5f1f935 | 2007-10-17 18:04:38 +0200 | [diff] [blame] | 49 | rv.msr_no = msr_no; |
H. Peter Anvin | bdd3146 | 2008-08-25 17:44:03 -0700 | [diff] [blame] | 50 | err = smp_call_function_single(cpu, __rdmsr_on_cpu, &rv, 1); |
Borislav Petkov | 6bc1096 | 2009-05-22 12:12:01 +0200 | [diff] [blame] | 51 | *l = rv.reg.l; |
| 52 | *h = rv.reg.h; |
Avi Kivity | 5f1f935 | 2007-10-17 18:04:38 +0200 | [diff] [blame] | 53 | |
Rudolf Marek | 4e9baad | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 54 | return err; |
Alexey Dobriyan | b077ffb | 2007-02-16 01:48:11 -0800 | [diff] [blame] | 55 | } |
Borislav Petkov | b034c19 | 2009-05-22 13:52:19 +0200 | [diff] [blame] | 56 | EXPORT_SYMBOL(rdmsr_on_cpu); |
Alexey Dobriyan | b077ffb | 2007-02-16 01:48:11 -0800 | [diff] [blame] | 57 | |
H. Peter Anvin | bdd3146 | 2008-08-25 17:44:03 -0700 | [diff] [blame] | 58 | int wrmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h) |
| 59 | { |
| 60 | int err; |
| 61 | struct msr_info rv; |
| 62 | |
Borislav Petkov | b034c19 | 2009-05-22 13:52:19 +0200 | [diff] [blame] | 63 | memset(&rv, 0, sizeof(rv)); |
| 64 | |
H. Peter Anvin | bdd3146 | 2008-08-25 17:44:03 -0700 | [diff] [blame] | 65 | rv.msr_no = msr_no; |
Borislav Petkov | 6bc1096 | 2009-05-22 12:12:01 +0200 | [diff] [blame] | 66 | rv.reg.l = l; |
| 67 | rv.reg.h = h; |
H. Peter Anvin | bdd3146 | 2008-08-25 17:44:03 -0700 | [diff] [blame] | 68 | err = smp_call_function_single(cpu, __wrmsr_on_cpu, &rv, 1); |
| 69 | |
| 70 | return err; |
| 71 | } |
Borislav Petkov | b034c19 | 2009-05-22 13:52:19 +0200 | [diff] [blame] | 72 | EXPORT_SYMBOL(wrmsr_on_cpu); |
| 73 | |
Borislav Petkov | b8a4754 | 2009-07-30 11:10:02 +0200 | [diff] [blame] | 74 | static void __rwmsr_on_cpus(const struct cpumask *mask, u32 msr_no, |
| 75 | struct msr *msrs, |
| 76 | void (*msr_func) (void *info)) |
Borislav Petkov | b034c19 | 2009-05-22 13:52:19 +0200 | [diff] [blame] | 77 | { |
| 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 Petkov | bab9a3d | 2009-07-30 11:10:01 +0200 | [diff] [blame] | 87 | this_cpu = get_cpu(); |
| 88 | |
| 89 | if (cpumask_test_cpu(this_cpu, mask)) |
Borislav Petkov | b8a4754 | 2009-07-30 11:10:02 +0200 | [diff] [blame] | 90 | msr_func(&rv); |
Borislav Petkov | b034c19 | 2009-05-22 13:52:19 +0200 | [diff] [blame] | 91 | |
Borislav Petkov | b8a4754 | 2009-07-30 11:10:02 +0200 | [diff] [blame] | 92 | smp_call_function_many(mask, msr_func, &rv, 1); |
Borislav Petkov | bab9a3d | 2009-07-30 11:10:01 +0200 | [diff] [blame] | 93 | put_cpu(); |
Borislav Petkov | b034c19 | 2009-05-22 13:52:19 +0200 | [diff] [blame] | 94 | } |
Borislav Petkov | b8a4754 | 2009-07-30 11:10:02 +0200 | [diff] [blame] | 95 | |
| 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 | */ |
| 103 | void 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 Petkov | b034c19 | 2009-05-22 13:52:19 +0200 | [diff] [blame] | 107 | EXPORT_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 Petkov | b8a4754 | 2009-07-30 11:10:02 +0200 | [diff] [blame] | 117 | void wrmsr_on_cpus(const struct cpumask *mask, u32 msr_no, struct msr *msrs) |
Borislav Petkov | b034c19 | 2009-05-22 13:52:19 +0200 | [diff] [blame] | 118 | { |
Borislav Petkov | b8a4754 | 2009-07-30 11:10:02 +0200 | [diff] [blame] | 119 | __rwmsr_on_cpus(mask, msr_no, msrs, __wrmsr_on_cpu); |
Borislav Petkov | b034c19 | 2009-05-22 13:52:19 +0200 | [diff] [blame] | 120 | } |
| 121 | EXPORT_SYMBOL(wrmsr_on_cpus); |
H. Peter Anvin | bdd3146 | 2008-08-25 17:44:03 -0700 | [diff] [blame] | 122 | |
| 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) |
Alexey Dobriyan | b077ffb | 2007-02-16 01:48:11 -0800 | [diff] [blame] | 126 | { |
| 127 | struct msr_info *rv = info; |
| 128 | |
Borislav Petkov | 6bc1096 | 2009-05-22 12:12:01 +0200 | [diff] [blame] | 129 | rv->err = rdmsr_safe(rv->msr_no, &rv->reg.l, &rv->reg.h); |
Alexey Dobriyan | b077ffb | 2007-02-16 01:48:11 -0800 | [diff] [blame] | 130 | } |
| 131 | |
Rudolf Marek | 4e9baad | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 132 | static void __wrmsr_safe_on_cpu(void *info) |
Alexey Dobriyan | b077ffb | 2007-02-16 01:48:11 -0800 | [diff] [blame] | 133 | { |
Rudolf Marek | 4e9baad | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 134 | struct msr_info *rv = info; |
| 135 | |
Borislav Petkov | 6bc1096 | 2009-05-22 12:12:01 +0200 | [diff] [blame] | 136 | rv->err = wrmsr_safe(rv->msr_no, rv->reg.l, rv->reg.h); |
Rudolf Marek | 4e9baad | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 137 | } |
| 138 | |
H. Peter Anvin | bdd3146 | 2008-08-25 17:44:03 -0700 | [diff] [blame] | 139 | int rdmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h) |
Rudolf Marek | 4e9baad | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 140 | { |
H. Peter Anvin | bdd3146 | 2008-08-25 17:44:03 -0700 | [diff] [blame] | 141 | int err; |
| 142 | struct msr_info rv; |
| 143 | |
Borislav Petkov | b034c19 | 2009-05-22 13:52:19 +0200 | [diff] [blame] | 144 | memset(&rv, 0, sizeof(rv)); |
| 145 | |
H. Peter Anvin | bdd3146 | 2008-08-25 17:44:03 -0700 | [diff] [blame] | 146 | rv.msr_no = msr_no; |
| 147 | err = smp_call_function_single(cpu, __rdmsr_safe_on_cpu, &rv, 1); |
Borislav Petkov | 6bc1096 | 2009-05-22 12:12:01 +0200 | [diff] [blame] | 148 | *l = rv.reg.l; |
| 149 | *h = rv.reg.h; |
H. Peter Anvin | bdd3146 | 2008-08-25 17:44:03 -0700 | [diff] [blame] | 150 | |
| 151 | return err ? err : rv.err; |
| 152 | } |
Borislav Petkov | b034c19 | 2009-05-22 13:52:19 +0200 | [diff] [blame] | 153 | EXPORT_SYMBOL(rdmsr_safe_on_cpu); |
H. Peter Anvin | bdd3146 | 2008-08-25 17:44:03 -0700 | [diff] [blame] | 154 | |
| 155 | int wrmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h) |
| 156 | { |
| 157 | int err; |
Avi Kivity | 5f1f935 | 2007-10-17 18:04:38 +0200 | [diff] [blame] | 158 | struct msr_info rv; |
Alexey Dobriyan | b077ffb | 2007-02-16 01:48:11 -0800 | [diff] [blame] | 159 | |
Borislav Petkov | b034c19 | 2009-05-22 13:52:19 +0200 | [diff] [blame] | 160 | memset(&rv, 0, sizeof(rv)); |
| 161 | |
Avi Kivity | 5f1f935 | 2007-10-17 18:04:38 +0200 | [diff] [blame] | 162 | rv.msr_no = msr_no; |
Borislav Petkov | 6bc1096 | 2009-05-22 12:12:01 +0200 | [diff] [blame] | 163 | rv.reg.l = l; |
| 164 | rv.reg.h = h; |
H. Peter Anvin | bdd3146 | 2008-08-25 17:44:03 -0700 | [diff] [blame] | 165 | err = smp_call_function_single(cpu, __wrmsr_safe_on_cpu, &rv, 1); |
Avi Kivity | 5f1f935 | 2007-10-17 18:04:38 +0200 | [diff] [blame] | 166 | |
H. Peter Anvin | bdd3146 | 2008-08-25 17:44:03 -0700 | [diff] [blame] | 167 | return err ? err : rv.err; |
Alexey Dobriyan | b077ffb | 2007-02-16 01:48:11 -0800 | [diff] [blame] | 168 | } |
Rudolf Marek | 4e9baad | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 169 | EXPORT_SYMBOL(wrmsr_safe_on_cpu); |
H. Peter Anvin | 8b956bf | 2009-08-31 14:13:48 -0700 | [diff] [blame] | 170 | |
| 171 | /* |
| 172 | * These variants are significantly slower, but allows control over |
| 173 | * the entire 32-bit GPR set. |
| 174 | */ |
| 175 | struct msr_regs_info { |
| 176 | u32 *regs; |
| 177 | int err; |
| 178 | }; |
| 179 | |
| 180 | static 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 | |
| 187 | static 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 | |
| 194 | int 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 | } |
| 205 | EXPORT_SYMBOL(rdmsr_safe_regs_on_cpu); |
| 206 | |
| 207 | int 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 | } |
| 218 | EXPORT_SYMBOL(wrmsr_safe_regs_on_cpu); |