Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 1 | #ifndef __ASM_X86_MSR_H_ |
| 2 | #define __ASM_X86_MSR_H_ |
| 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)); |
Glauber de Oliveira Costa | 8f12dea | 2008-01-30 13:31:06 +0100 | [diff] [blame] | 21 | return low | ((u64)high >> 32); |
| 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 | |
Glauber de Oliveira Costa | 56ec1dd | 2008-01-30 13:31:07 +0100 | [diff] [blame] | 55 | asm volatile("2: rdmsr ; xor %0,%0\n" |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 56 | "1:\n\t" |
| 57 | ".section .fixup,\"ax\"\n\t" |
Glauber de Oliveira Costa | 56ec1dd | 2008-01-30 13:31:07 +0100 | [diff] [blame] | 58 | "3: mov %3,%0 ; 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) |
Glauber de Oliveira Costa | c210d24 | 2008-01-30 13:31:07 +0100 | [diff] [blame] | 61 | : "=r" (*err), EAX_EDX_RET(val, low, high) |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 62 | : "c" (msr), "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 | |
Glauber de Oliveira Costa | c9dcda5 | 2008-01-30 13:31:07 +0100 | [diff] [blame] | 66 | static inline void native_write_msr(unsigned int msr, |
| 67 | unsigned low, unsigned high) |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 68 | { |
Glauber de Oliveira Costa | c9dcda5 | 2008-01-30 13:31:07 +0100 | [diff] [blame] | 69 | asm volatile("wrmsr" : : "c" (msr), "a"(low), "d" (high)); |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | static inline int native_write_msr_safe(unsigned int msr, |
Glauber de Oliveira Costa | c9dcda5 | 2008-01-30 13:31:07 +0100 | [diff] [blame] | 73 | unsigned low, unsigned high) |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 74 | { |
| 75 | int err; |
Glauber de Oliveira Costa | 56ec1dd | 2008-01-30 13:31:07 +0100 | [diff] [blame] | 76 | asm volatile("2: wrmsr ; xor %0,%0\n" |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 77 | "1:\n\t" |
| 78 | ".section .fixup,\"ax\"\n\t" |
Glauber de Oliveira Costa | 56ec1dd | 2008-01-30 13:31:07 +0100 | [diff] [blame] | 79 | "3: mov %4,%0 ; jmp 1b\n\t" |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 80 | ".previous\n\t" |
Joe Perches | abb0ade | 2008-03-23 01:02:51 -0700 | [diff] [blame] | 81 | _ASM_EXTABLE(2b, 3b) |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 82 | : "=a" (err) |
Glauber de Oliveira Costa | c9dcda5 | 2008-01-30 13:31:07 +0100 | [diff] [blame] | 83 | : "c" (msr), "0" (low), "d" (high), |
Joe Perches | abb0ade | 2008-03-23 01:02:51 -0700 | [diff] [blame] | 84 | "i" (-EFAULT)); |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 85 | return err; |
| 86 | } |
| 87 | |
Ingo Molnar | cdc7957 | 2008-01-30 13:32:39 +0100 | [diff] [blame] | 88 | extern unsigned long long native_read_tsc(void); |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 89 | |
Ingo Molnar | 92767af | 2008-01-30 13:32:40 +0100 | [diff] [blame] | 90 | static __always_inline unsigned long long __native_read_tsc(void) |
| 91 | { |
| 92 | DECLARE_ARGS(val, low, high); |
| 93 | |
| 94 | rdtsc_barrier(); |
| 95 | asm volatile("rdtsc" : EAX_EDX_RET(val, low, high)); |
| 96 | rdtsc_barrier(); |
| 97 | |
| 98 | return EAX_EDX_VAL(val, low, high); |
| 99 | } |
| 100 | |
Glauber de Oliveira Costa | b8d1fae | 2008-01-30 13:31:07 +0100 | [diff] [blame] | 101 | static inline unsigned long long native_read_pmc(int counter) |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 102 | { |
Glauber de Oliveira Costa | c210d24 | 2008-01-30 13:31:07 +0100 | [diff] [blame] | 103 | DECLARE_ARGS(val, low, high); |
| 104 | |
| 105 | asm volatile("rdpmc" : EAX_EDX_RET(val, low, high) : "c" (counter)); |
| 106 | return EAX_EDX_VAL(val, low, high); |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | #ifdef CONFIG_PARAVIRT |
| 110 | #include <asm/paravirt.h> |
Thomas Gleixner | 96a388d | 2007-10-11 11:20:03 +0200 | [diff] [blame] | 111 | #else |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 112 | #include <linux/errno.h> |
| 113 | /* |
| 114 | * Access to machine-specific registers (available on 586 and better only) |
| 115 | * Note: the rd* operations modify the parameters directly (without using |
| 116 | * pointer indirection), this allows gcc to optimize better |
| 117 | */ |
| 118 | |
Joe Perches | abb0ade | 2008-03-23 01:02:51 -0700 | [diff] [blame] | 119 | #define rdmsr(msr, val1, val2) \ |
| 120 | do { \ |
| 121 | u64 __val = native_read_msr((msr)); \ |
| 122 | (val1) = (u32)__val; \ |
| 123 | (val2) = (u32)(__val >> 32); \ |
| 124 | } while (0) |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 125 | |
Glauber de Oliveira Costa | c9dcda5 | 2008-01-30 13:31:07 +0100 | [diff] [blame] | 126 | static inline void wrmsr(unsigned msr, unsigned low, unsigned high) |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 127 | { |
Glauber de Oliveira Costa | c9dcda5 | 2008-01-30 13:31:07 +0100 | [diff] [blame] | 128 | native_write_msr(msr, low, high); |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 129 | } |
| 130 | |
Joe Perches | abb0ade | 2008-03-23 01:02:51 -0700 | [diff] [blame] | 131 | #define rdmsrl(msr, val) \ |
| 132 | ((val) = native_read_msr((msr))) |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 133 | |
Glauber de Oliveira Costa | c210d24 | 2008-01-30 13:31:07 +0100 | [diff] [blame] | 134 | #define wrmsrl(msr, val) \ |
Joe Perches | abb0ade | 2008-03-23 01:02:51 -0700 | [diff] [blame] | 135 | native_write_msr((msr), (u32)((u64)(val)), (u32)((u64)(val) >> 32)) |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 136 | |
| 137 | /* wrmsr with exception handling */ |
Glauber de Oliveira Costa | c9dcda5 | 2008-01-30 13:31:07 +0100 | [diff] [blame] | 138 | static inline int wrmsr_safe(unsigned msr, unsigned low, unsigned high) |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 139 | { |
Glauber de Oliveira Costa | c9dcda5 | 2008-01-30 13:31:07 +0100 | [diff] [blame] | 140 | return native_write_msr_safe(msr, low, high); |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | /* rdmsr with exception handling */ |
Joe Perches | abb0ade | 2008-03-23 01:02:51 -0700 | [diff] [blame] | 144 | #define rdmsr_safe(msr, p1, p2) \ |
| 145 | ({ \ |
| 146 | int __err; \ |
| 147 | u64 __val = native_read_msr_safe((msr), &__err); \ |
| 148 | (*p1) = (u32)__val; \ |
| 149 | (*p2) = (u32)(__val >> 32); \ |
| 150 | __err; \ |
| 151 | }) |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 152 | |
Andi Kleen | 1de87bd | 2008-03-22 10:59:28 +0100 | [diff] [blame] | 153 | static inline int rdmsrl_safe(unsigned msr, unsigned long long *p) |
| 154 | { |
| 155 | int err; |
| 156 | |
| 157 | *p = native_read_msr_safe(msr, &err); |
| 158 | return err; |
| 159 | } |
| 160 | |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 161 | #define rdtscl(low) \ |
| 162 | ((low) = (u32)native_read_tsc()) |
| 163 | |
| 164 | #define rdtscll(val) \ |
| 165 | ((val) = native_read_tsc()) |
| 166 | |
Joe Perches | abb0ade | 2008-03-23 01:02:51 -0700 | [diff] [blame] | 167 | #define rdpmc(counter, low, high) \ |
| 168 | do { \ |
| 169 | u64 _l = native_read_pmc((counter)); \ |
| 170 | (low) = (u32)_l; \ |
| 171 | (high) = (u32)(_l >> 32); \ |
| 172 | } while (0) |
Glauber de Oliveira Costa | c210d24 | 2008-01-30 13:31:07 +0100 | [diff] [blame] | 173 | |
Joe Perches | abb0ade | 2008-03-23 01:02:51 -0700 | [diff] [blame] | 174 | #define rdtscp(low, high, aux) \ |
| 175 | do { \ |
| 176 | unsigned long long _val = native_read_tscp(&(aux)); \ |
| 177 | (low) = (u32)_val; \ |
| 178 | (high) = (u32)(_val >> 32); \ |
| 179 | } while (0) |
Glauber de Oliveira Costa | c210d24 | 2008-01-30 13:31:07 +0100 | [diff] [blame] | 180 | |
| 181 | #define rdtscpll(val, aux) (val) = native_read_tscp(&(aux)) |
| 182 | |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 183 | #endif /* !CONFIG_PARAVIRT */ |
| 184 | |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 185 | |
Joe Perches | abb0ade | 2008-03-23 01:02:51 -0700 | [diff] [blame] | 186 | #define checking_wrmsrl(msr, val) wrmsr_safe((msr), (u32)(val), \ |
| 187 | (u32)((val) >> 32)) |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 188 | |
Joe Perches | abb0ade | 2008-03-23 01:02:51 -0700 | [diff] [blame] | 189 | #define write_tsc(val1, val2) wrmsr(0x10, (val1), (val2)) |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 190 | |
Joe Perches | abb0ade | 2008-03-23 01:02:51 -0700 | [diff] [blame] | 191 | #define write_rdtscp_aux(val) wrmsr(0xc0000103, (val), 0) |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 192 | |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 193 | #ifdef CONFIG_SMP |
| 194 | void rdmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h); |
| 195 | void wrmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h); |
| 196 | int rdmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h); |
Joe Perches | abb0ade | 2008-03-23 01:02:51 -0700 | [diff] [blame] | 197 | |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 198 | int wrmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h); |
| 199 | #else /* CONFIG_SMP */ |
| 200 | static inline void rdmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h) |
| 201 | { |
| 202 | rdmsr(msr_no, *l, *h); |
| 203 | } |
| 204 | static inline void wrmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h) |
| 205 | { |
| 206 | wrmsr(msr_no, l, h); |
| 207 | } |
Joe Perches | abb0ade | 2008-03-23 01:02:51 -0700 | [diff] [blame] | 208 | static inline int rdmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, |
| 209 | u32 *l, u32 *h) |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 210 | { |
| 211 | return rdmsr_safe(msr_no, l, h); |
| 212 | } |
| 213 | static inline int wrmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h) |
| 214 | { |
| 215 | return wrmsr_safe(msr_no, l, h); |
| 216 | } |
| 217 | #endif /* CONFIG_SMP */ |
Glauber de Oliveira Costa | 751de83 | 2008-01-30 13:31:03 +0100 | [diff] [blame] | 218 | #endif /* __ASSEMBLY__ */ |
Glauber de Oliveira Costa | c210d24 | 2008-01-30 13:31:07 +0100 | [diff] [blame] | 219 | #endif /* __KERNEL__ */ |
| 220 | |
Thomas Gleixner | be7baf8 | 2007-10-23 22:37:24 +0200 | [diff] [blame] | 221 | |
Thomas Gleixner | 96a388d | 2007-10-11 11:20:03 +0200 | [diff] [blame] | 222 | #endif |