H. Peter Anvin | 1965aae | 2008-10-22 22:26:29 -0700 | [diff] [blame] | 1 | #ifndef _ASM_X86_MSR_H |
| 2 | #define _ASM_X86_MSR_H |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 3 | |
| 4 | #include <asm/msr-index.h> |
| 5 | |
Mike Frysinger | d43a331 | 2008-01-15 16:44:38 +0100 | [diff] [blame] | 6 | #ifndef __ASSEMBLY__ |
| 7 | # include <linux/types.h> |
| 8 | #endif |
| 9 | |
Glauber de Oliveira Costa | 8f12dea | 2008-01-30 13:31:06 +0100 | [diff] [blame] | 10 | #ifdef __KERNEL__ |
| 11 | #ifndef __ASSEMBLY__ |
Glauber de Oliveira Costa | c210d24 | 2008-01-30 13:31:07 +0100 | [diff] [blame] | 12 | |
| 13 | #include <asm/asm.h> |
| 14 | #include <asm/errno.h> |
| 15 | |
Andrew Morton | 1e160cc | 2008-01-30 13:31:17 +0100 | [diff] [blame] | 16 | static inline unsigned long long native_read_tscp(unsigned int *aux) |
Glauber de Oliveira Costa | 8f12dea | 2008-01-30 13:31:06 +0100 | [diff] [blame] | 17 | { |
| 18 | unsigned long low, high; |
Joe Perches | abb0ade | 2008-03-23 01:02:51 -0700 | [diff] [blame] | 19 | asm volatile(".byte 0x0f,0x01,0xf9" |
| 20 | : "=a" (low), "=d" (high), "=c" (*aux)); |
Max Asbock | 41aefdc | 2008-06-25 14:45:28 -0700 | [diff] [blame] | 21 | return low | ((u64)high << 32); |
Glauber de Oliveira Costa | 8f12dea | 2008-01-30 13:31:06 +0100 | [diff] [blame] | 22 | } |
| 23 | |
Glauber de Oliveira Costa | c210d24 | 2008-01-30 13:31:07 +0100 | [diff] [blame] | 24 | /* |
| 25 | * i386 calling convention returns 64-bit value in edx:eax, while |
| 26 | * x86_64 returns at rax. Also, the "A" constraint does not really |
| 27 | * mean rdx:rax in x86_64, so we need specialized behaviour for each |
| 28 | * architecture |
| 29 | */ |
| 30 | #ifdef CONFIG_X86_64 |
| 31 | #define DECLARE_ARGS(val, low, high) unsigned low, high |
Joe Perches | abb0ade | 2008-03-23 01:02:51 -0700 | [diff] [blame] | 32 | #define EAX_EDX_VAL(val, low, high) ((low) | ((u64)(high) << 32)) |
Glauber de Oliveira Costa | c210d24 | 2008-01-30 13:31:07 +0100 | [diff] [blame] | 33 | #define EAX_EDX_ARGS(val, low, high) "a" (low), "d" (high) |
| 34 | #define EAX_EDX_RET(val, low, high) "=a" (low), "=d" (high) |
| 35 | #else |
| 36 | #define DECLARE_ARGS(val, low, high) unsigned long long val |
| 37 | #define EAX_EDX_VAL(val, low, high) (val) |
| 38 | #define EAX_EDX_ARGS(val, low, high) "A" (val) |
| 39 | #define EAX_EDX_RET(val, low, high) "=A" (val) |
Glauber de Oliveira Costa | 8f12dea | 2008-01-30 13:31:06 +0100 | [diff] [blame] | 40 | #endif |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 41 | |
| 42 | static inline unsigned long long native_read_msr(unsigned int msr) |
| 43 | { |
Glauber de Oliveira Costa | c210d24 | 2008-01-30 13:31:07 +0100 | [diff] [blame] | 44 | DECLARE_ARGS(val, low, high); |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 45 | |
Glauber de Oliveira Costa | c210d24 | 2008-01-30 13:31:07 +0100 | [diff] [blame] | 46 | asm volatile("rdmsr" : EAX_EDX_RET(val, low, high) : "c" (msr)); |
| 47 | return EAX_EDX_VAL(val, low, high); |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | static inline unsigned long long native_read_msr_safe(unsigned int msr, |
| 51 | int *err) |
| 52 | { |
Glauber de Oliveira Costa | c210d24 | 2008-01-30 13:31:07 +0100 | [diff] [blame] | 53 | DECLARE_ARGS(val, low, high); |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 54 | |
H. Peter Anvin | 08970fc | 2008-08-25 22:39:15 -0700 | [diff] [blame] | 55 | asm volatile("2: rdmsr ; xor %[err],%[err]\n" |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 56 | "1:\n\t" |
| 57 | ".section .fixup,\"ax\"\n\t" |
H. Peter Anvin | 08970fc | 2008-08-25 22:39:15 -0700 | [diff] [blame] | 58 | "3: mov %[fault],%[err] ; jmp 1b\n\t" |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 59 | ".previous\n\t" |
Joe Perches | abb0ade | 2008-03-23 01:02:51 -0700 | [diff] [blame] | 60 | _ASM_EXTABLE(2b, 3b) |
H. Peter Anvin | 08970fc | 2008-08-25 22:39:15 -0700 | [diff] [blame] | 61 | : [err] "=r" (*err), EAX_EDX_RET(val, low, high) |
| 62 | : "c" (msr), [fault] "i" (-EFAULT)); |
Glauber de Oliveira Costa | c210d24 | 2008-01-30 13:31:07 +0100 | [diff] [blame] | 63 | return EAX_EDX_VAL(val, low, high); |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 64 | } |
| 65 | |
Yinghai Lu | b05f78f | 2008-08-22 01:32:50 -0700 | [diff] [blame] | 66 | static inline unsigned long long native_read_msr_amd_safe(unsigned int msr, |
| 67 | int *err) |
| 68 | { |
| 69 | DECLARE_ARGS(val, low, high); |
| 70 | |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 71 | asm volatile("2: rdmsr ; xor %0,%0\n" |
| 72 | "1:\n\t" |
| 73 | ".section .fixup,\"ax\"\n\t" |
| 74 | "3: mov %3,%0 ; jmp 1b\n\t" |
| 75 | ".previous\n\t" |
| 76 | _ASM_EXTABLE(2b, 3b) |
| 77 | : "=r" (*err), EAX_EDX_RET(val, low, high) |
Yinghai Lu | b05f78f | 2008-08-22 01:32:50 -0700 | [diff] [blame] | 78 | : "c" (msr), "D" (0x9c5a203a), "i" (-EFAULT)); |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 79 | return EAX_EDX_VAL(val, low, high); |
| 80 | } |
| 81 | |
Glauber de Oliveira Costa | c9dcda5 | 2008-01-30 13:31:07 +0100 | [diff] [blame] | 82 | static inline void native_write_msr(unsigned int msr, |
| 83 | unsigned low, unsigned high) |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 84 | { |
Jeremy Fitzhardinge | af2b1c6 | 2008-06-25 00:18:59 -0400 | [diff] [blame] | 85 | asm volatile("wrmsr" : : "c" (msr), "a"(low), "d" (high) : "memory"); |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | static inline int native_write_msr_safe(unsigned int msr, |
Glauber de Oliveira Costa | c9dcda5 | 2008-01-30 13:31:07 +0100 | [diff] [blame] | 89 | unsigned low, unsigned high) |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 90 | { |
| 91 | int err; |
H. Peter Anvin | 08970fc | 2008-08-25 22:39:15 -0700 | [diff] [blame] | 92 | asm volatile("2: wrmsr ; xor %[err],%[err]\n" |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 93 | "1:\n\t" |
| 94 | ".section .fixup,\"ax\"\n\t" |
H. Peter Anvin | 08970fc | 2008-08-25 22:39:15 -0700 | [diff] [blame] | 95 | "3: mov %[fault],%[err] ; jmp 1b\n\t" |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 96 | ".previous\n\t" |
Joe Perches | abb0ade | 2008-03-23 01:02:51 -0700 | [diff] [blame] | 97 | _ASM_EXTABLE(2b, 3b) |
H. Peter Anvin | 08970fc | 2008-08-25 22:39:15 -0700 | [diff] [blame] | 98 | : [err] "=a" (err) |
Glauber de Oliveira Costa | c9dcda5 | 2008-01-30 13:31:07 +0100 | [diff] [blame] | 99 | : "c" (msr), "0" (low), "d" (high), |
H. Peter Anvin | 08970fc | 2008-08-25 22:39:15 -0700 | [diff] [blame] | 100 | [fault] "i" (-EFAULT) |
Jeremy Fitzhardinge | af2b1c6 | 2008-06-25 00:18:59 -0400 | [diff] [blame] | 101 | : "memory"); |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 102 | return err; |
| 103 | } |
| 104 | |
Ingo Molnar | cdc7957 | 2008-01-30 13:32:39 +0100 | [diff] [blame] | 105 | extern unsigned long long native_read_tsc(void); |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 106 | |
Ingo Molnar | 92767af | 2008-01-30 13:32:40 +0100 | [diff] [blame] | 107 | static __always_inline unsigned long long __native_read_tsc(void) |
| 108 | { |
| 109 | DECLARE_ARGS(val, low, high); |
| 110 | |
Ingo Molnar | 92767af | 2008-01-30 13:32:40 +0100 | [diff] [blame] | 111 | asm volatile("rdtsc" : EAX_EDX_RET(val, low, high)); |
Ingo Molnar | 92767af | 2008-01-30 13:32:40 +0100 | [diff] [blame] | 112 | |
| 113 | return EAX_EDX_VAL(val, low, high); |
| 114 | } |
| 115 | |
Glauber de Oliveira Costa | b8d1fae | 2008-01-30 13:31:07 +0100 | [diff] [blame] | 116 | static inline unsigned long long native_read_pmc(int counter) |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 117 | { |
Glauber de Oliveira Costa | c210d24 | 2008-01-30 13:31:07 +0100 | [diff] [blame] | 118 | DECLARE_ARGS(val, low, high); |
| 119 | |
| 120 | asm volatile("rdpmc" : EAX_EDX_RET(val, low, high) : "c" (counter)); |
| 121 | return EAX_EDX_VAL(val, low, high); |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | #ifdef CONFIG_PARAVIRT |
| 125 | #include <asm/paravirt.h> |
Thomas Gleixner | 96a388d | 2007-10-11 11:20:03 +0200 | [diff] [blame] | 126 | #else |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 127 | #include <linux/errno.h> |
| 128 | /* |
| 129 | * Access to machine-specific registers (available on 586 and better only) |
| 130 | * Note: the rd* operations modify the parameters directly (without using |
| 131 | * pointer indirection), this allows gcc to optimize better |
| 132 | */ |
| 133 | |
Joe Perches | abb0ade | 2008-03-23 01:02:51 -0700 | [diff] [blame] | 134 | #define rdmsr(msr, val1, val2) \ |
| 135 | do { \ |
| 136 | u64 __val = native_read_msr((msr)); \ |
| 137 | (val1) = (u32)__val; \ |
| 138 | (val2) = (u32)(__val >> 32); \ |
| 139 | } while (0) |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 140 | |
Glauber de Oliveira Costa | c9dcda5 | 2008-01-30 13:31:07 +0100 | [diff] [blame] | 141 | static inline void wrmsr(unsigned msr, unsigned low, unsigned high) |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 142 | { |
Glauber de Oliveira Costa | c9dcda5 | 2008-01-30 13:31:07 +0100 | [diff] [blame] | 143 | native_write_msr(msr, low, high); |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 144 | } |
| 145 | |
Joe Perches | abb0ade | 2008-03-23 01:02:51 -0700 | [diff] [blame] | 146 | #define rdmsrl(msr, val) \ |
| 147 | ((val) = native_read_msr((msr))) |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 148 | |
Glauber de Oliveira Costa | c210d24 | 2008-01-30 13:31:07 +0100 | [diff] [blame] | 149 | #define wrmsrl(msr, val) \ |
Joe Perches | abb0ade | 2008-03-23 01:02:51 -0700 | [diff] [blame] | 150 | native_write_msr((msr), (u32)((u64)(val)), (u32)((u64)(val) >> 32)) |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 151 | |
| 152 | /* wrmsr with exception handling */ |
Glauber de Oliveira Costa | c9dcda5 | 2008-01-30 13:31:07 +0100 | [diff] [blame] | 153 | static inline int wrmsr_safe(unsigned msr, unsigned low, unsigned high) |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 154 | { |
Glauber de Oliveira Costa | c9dcda5 | 2008-01-30 13:31:07 +0100 | [diff] [blame] | 155 | return native_write_msr_safe(msr, low, high); |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | /* rdmsr with exception handling */ |
Joe Perches | abb0ade | 2008-03-23 01:02:51 -0700 | [diff] [blame] | 159 | #define rdmsr_safe(msr, p1, p2) \ |
| 160 | ({ \ |
| 161 | int __err; \ |
| 162 | u64 __val = native_read_msr_safe((msr), &__err); \ |
| 163 | (*p1) = (u32)__val; \ |
| 164 | (*p2) = (u32)(__val >> 32); \ |
| 165 | __err; \ |
| 166 | }) |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 167 | |
Andi Kleen | 1de87bd | 2008-03-22 10:59:28 +0100 | [diff] [blame] | 168 | static inline int rdmsrl_safe(unsigned msr, unsigned long long *p) |
| 169 | { |
| 170 | int err; |
| 171 | |
| 172 | *p = native_read_msr_safe(msr, &err); |
| 173 | return err; |
| 174 | } |
Yinghai Lu | b05f78f | 2008-08-22 01:32:50 -0700 | [diff] [blame] | 175 | static inline int rdmsrl_amd_safe(unsigned msr, unsigned long long *p) |
| 176 | { |
| 177 | int err; |
| 178 | |
| 179 | *p = native_read_msr_amd_safe(msr, &err); |
| 180 | return err; |
| 181 | } |
Andi Kleen | 1de87bd | 2008-03-22 10:59:28 +0100 | [diff] [blame] | 182 | |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 183 | #define rdtscl(low) \ |
| 184 | ((low) = (u32)native_read_tsc()) |
| 185 | |
| 186 | #define rdtscll(val) \ |
| 187 | ((val) = native_read_tsc()) |
| 188 | |
Joe Perches | abb0ade | 2008-03-23 01:02:51 -0700 | [diff] [blame] | 189 | #define rdpmc(counter, low, high) \ |
| 190 | do { \ |
| 191 | u64 _l = native_read_pmc((counter)); \ |
| 192 | (low) = (u32)_l; \ |
| 193 | (high) = (u32)(_l >> 32); \ |
| 194 | } while (0) |
Glauber de Oliveira Costa | c210d24 | 2008-01-30 13:31:07 +0100 | [diff] [blame] | 195 | |
Joe Perches | abb0ade | 2008-03-23 01:02:51 -0700 | [diff] [blame] | 196 | #define rdtscp(low, high, aux) \ |
| 197 | do { \ |
| 198 | unsigned long long _val = native_read_tscp(&(aux)); \ |
| 199 | (low) = (u32)_val; \ |
| 200 | (high) = (u32)(_val >> 32); \ |
| 201 | } while (0) |
Glauber de Oliveira Costa | c210d24 | 2008-01-30 13:31:07 +0100 | [diff] [blame] | 202 | |
| 203 | #define rdtscpll(val, aux) (val) = native_read_tscp(&(aux)) |
| 204 | |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 205 | #endif /* !CONFIG_PARAVIRT */ |
| 206 | |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 207 | |
Joe Perches | abb0ade | 2008-03-23 01:02:51 -0700 | [diff] [blame] | 208 | #define checking_wrmsrl(msr, val) wrmsr_safe((msr), (u32)(val), \ |
| 209 | (u32)((val) >> 32)) |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 210 | |
Joe Perches | abb0ade | 2008-03-23 01:02:51 -0700 | [diff] [blame] | 211 | #define write_tsc(val1, val2) wrmsr(0x10, (val1), (val2)) |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 212 | |
Joe Perches | abb0ade | 2008-03-23 01:02:51 -0700 | [diff] [blame] | 213 | #define write_rdtscp_aux(val) wrmsr(0xc0000103, (val), 0) |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 214 | |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 215 | #ifdef CONFIG_SMP |
H. Peter Anvin | c6f3193 | 2008-08-25 17:27:21 -0700 | [diff] [blame] | 216 | int rdmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h); |
| 217 | int wrmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h); |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 218 | int rdmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h); |
| 219 | int wrmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h); |
| 220 | #else /* CONFIG_SMP */ |
H. Peter Anvin | c6f3193 | 2008-08-25 17:27:21 -0700 | [diff] [blame] | 221 | static inline int rdmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h) |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 222 | { |
| 223 | rdmsr(msr_no, *l, *h); |
H. Peter Anvin | c6f3193 | 2008-08-25 17:27:21 -0700 | [diff] [blame] | 224 | return 0; |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 225 | } |
H. Peter Anvin | c6f3193 | 2008-08-25 17:27:21 -0700 | [diff] [blame] | 226 | static inline int wrmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h) |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 227 | { |
| 228 | wrmsr(msr_no, l, h); |
H. Peter Anvin | c6f3193 | 2008-08-25 17:27:21 -0700 | [diff] [blame] | 229 | return 0; |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 230 | } |
Joe Perches | abb0ade | 2008-03-23 01:02:51 -0700 | [diff] [blame] | 231 | static inline int rdmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, |
| 232 | u32 *l, u32 *h) |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 233 | { |
| 234 | return rdmsr_safe(msr_no, l, h); |
| 235 | } |
| 236 | static inline int wrmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h) |
| 237 | { |
| 238 | return wrmsr_safe(msr_no, l, h); |
| 239 | } |
| 240 | #endif /* CONFIG_SMP */ |
Glauber de Oliveira Costa | 751de83 | 2008-01-30 13:31:03 +0100 | [diff] [blame] | 241 | #endif /* __ASSEMBLY__ */ |
Glauber de Oliveira Costa | c210d24 | 2008-01-30 13:31:07 +0100 | [diff] [blame] | 242 | #endif /* __KERNEL__ */ |
| 243 | |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 244 | |
H. Peter Anvin | 1965aae | 2008-10-22 22:26:29 -0700 | [diff] [blame] | 245 | #endif /* _ASM_X86_MSR_H */ |