blob: 792fde2e8908fd94846fbe407cc8f008c63a5aaa [file] [log] [blame]
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +02001#ifndef __ASM_X86_MSR_H_
2#define __ASM_X86_MSR_H_
3
4#include <asm/msr-index.h>
5
Mike Frysingerd43a3312008-01-15 16:44:38 +01006#ifndef __ASSEMBLY__
7# include <linux/types.h>
8#endif
9
Glauber de Oliveira Costa8f12dea2008-01-30 13:31:06 +010010#ifdef __KERNEL__
11#ifndef __ASSEMBLY__
12static inline unsigned long long native_read_tscp(int *aux)
13{
14 unsigned long low, high;
15 asm volatile (".byte 0x0f,0x01,0xf9"
16 : "=a" (low), "=d" (high), "=c" (*aux));
17 return low | ((u64)high >> 32);
18}
19
20#define rdtscp(low, high, aux) \
21 do { \
22 unsigned long long _val = native_read_tscp(&(aux)); \
23 (low) = (u32)_val; \
24 (high) = (u32)(_val >> 32); \
25 } while (0)
26
27#define rdtscpll(val, aux) (val) = native_read_tscp(&(aux))
28#endif
29#endif
30
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020031#ifdef __i386__
32
Thomas Gleixner96a388d2007-10-11 11:20:03 +020033#ifdef __KERNEL__
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020034#ifndef __ASSEMBLY__
35
Glauber de Oliveira Costa56ec1dd2008-01-30 13:31:07 +010036#include <asm/asm.h>
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020037#include <asm/errno.h>
38
39static inline unsigned long long native_read_msr(unsigned int msr)
40{
41 unsigned long long val;
42
43 asm volatile("rdmsr" : "=A" (val) : "c" (msr));
44 return val;
45}
46
47static inline unsigned long long native_read_msr_safe(unsigned int msr,
48 int *err)
49{
50 unsigned long long val;
51
Glauber de Oliveira Costa56ec1dd2008-01-30 13:31:07 +010052 asm volatile("2: rdmsr ; xor %0,%0\n"
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020053 "1:\n\t"
54 ".section .fixup,\"ax\"\n\t"
Glauber de Oliveira Costa56ec1dd2008-01-30 13:31:07 +010055 "3: mov %3,%0 ; jmp 1b\n\t"
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020056 ".previous\n\t"
57 ".section __ex_table,\"a\"\n"
Glauber de Oliveira Costa56ec1dd2008-01-30 13:31:07 +010058 _ASM_ALIGN "\n\t"
59 _ASM_PTR " 2b,3b\n\t"
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020060 ".previous"
61 : "=r" (*err), "=A" (val)
62 : "c" (msr), "i" (-EFAULT));
63
64 return val;
65}
66
Glauber de Oliveira Costac9dcda52008-01-30 13:31:07 +010067static inline void native_write_msr(unsigned int msr,
68 unsigned low, unsigned high)
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020069{
Glauber de Oliveira Costac9dcda52008-01-30 13:31:07 +010070 asm volatile("wrmsr" : : "c" (msr), "a"(low), "d" (high));
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020071}
72
73static inline int native_write_msr_safe(unsigned int msr,
Glauber de Oliveira Costac9dcda52008-01-30 13:31:07 +010074 unsigned low, unsigned high)
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020075{
76 int err;
Glauber de Oliveira Costa56ec1dd2008-01-30 13:31:07 +010077 asm volatile("2: wrmsr ; xor %0,%0\n"
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020078 "1:\n\t"
79 ".section .fixup,\"ax\"\n\t"
Glauber de Oliveira Costa56ec1dd2008-01-30 13:31:07 +010080 "3: mov %4,%0 ; jmp 1b\n\t"
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020081 ".previous\n\t"
82 ".section __ex_table,\"a\"\n"
Glauber de Oliveira Costa56ec1dd2008-01-30 13:31:07 +010083 _ASM_ALIGN "\n\t"
84 _ASM_PTR " 2b,3b\n\t"
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020085 ".previous"
86 : "=a" (err)
Glauber de Oliveira Costac9dcda52008-01-30 13:31:07 +010087 : "c" (msr), "0" (low), "d" (high),
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020088 "i" (-EFAULT));
89 return err;
90}
91
92static inline unsigned long long native_read_tsc(void)
93{
94 unsigned long long val;
95 asm volatile("rdtsc" : "=A" (val));
96 return val;
97}
98
Glauber de Oliveira Costab8d1fae2008-01-30 13:31:07 +010099static inline unsigned long long native_read_pmc(int counter)
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200100{
101 unsigned long long val;
Glauber de Oliveira Costab8d1fae2008-01-30 13:31:07 +0100102 asm volatile("rdpmc" : "=A" (val) : "c" (counter));
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200103 return val;
104}
105
106#ifdef CONFIG_PARAVIRT
107#include <asm/paravirt.h>
Thomas Gleixner96a388d2007-10-11 11:20:03 +0200108#else
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200109#include <linux/errno.h>
110/*
111 * Access to machine-specific registers (available on 586 and better only)
112 * Note: the rd* operations modify the parameters directly (without using
113 * pointer indirection), this allows gcc to optimize better
114 */
115
116#define rdmsr(msr,val1,val2) \
117 do { \
118 u64 __val = native_read_msr(msr); \
119 (val1) = (u32)__val; \
120 (val2) = (u32)(__val >> 32); \
121 } while(0)
122
Glauber de Oliveira Costac9dcda52008-01-30 13:31:07 +0100123static inline void wrmsr(unsigned msr, unsigned low, unsigned high)
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200124{
Glauber de Oliveira Costac9dcda52008-01-30 13:31:07 +0100125 native_write_msr(msr, low, high);
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200126}
127
128#define rdmsrl(msr,val) \
129 ((val) = native_read_msr(msr))
130
Glauber de Oliveira Costac9dcda52008-01-30 13:31:07 +0100131#define wrmsrl(msr, val) native_write_msr(msr, (u32)val, (u32)(val >> 32))
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200132
133/* wrmsr with exception handling */
Glauber de Oliveira Costac9dcda52008-01-30 13:31:07 +0100134static inline int wrmsr_safe(unsigned msr, unsigned low, unsigned high)
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200135{
Glauber de Oliveira Costac9dcda52008-01-30 13:31:07 +0100136 return native_write_msr_safe(msr, low, high);
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200137}
138
139/* rdmsr with exception handling */
140#define rdmsr_safe(msr,p1,p2) \
141 ({ \
142 int __err; \
143 u64 __val = native_read_msr_safe(msr, &__err); \
144 (*p1) = (u32)__val; \
145 (*p2) = (u32)(__val >> 32); \
146 __err; \
147 })
148
149#define rdtscl(low) \
150 ((low) = (u32)native_read_tsc())
151
152#define rdtscll(val) \
153 ((val) = native_read_tsc())
154
155#define write_tsc(val1,val2) wrmsr(0x10, val1, val2)
156
157#define rdpmc(counter,low,high) \
158 do { \
Glauber de Oliveira Costab8d1fae2008-01-30 13:31:07 +0100159 u64 _l = native_read_pmc(counter); \
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200160 (low) = (u32)_l; \
161 (high) = (u32)(_l >> 32); \
162 } while(0)
163#endif /* !CONFIG_PARAVIRT */
164
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200165#endif /* ! __ASSEMBLY__ */
166#endif /* __KERNEL__ */
167
168#else /* __i386__ */
169
170#ifndef __ASSEMBLY__
171#include <linux/errno.h>
172/*
173 * Access to machine-specific registers (available on 586 and better only)
174 * Note: the rd* operations modify the parameters directly (without using
175 * pointer indirection), this allows gcc to optimize better
176 */
177
178#define rdmsr(msr,val1,val2) \
179 __asm__ __volatile__("rdmsr" \
180 : "=a" (val1), "=d" (val2) \
181 : "c" (msr))
182
183
184#define rdmsrl(msr,val) do { unsigned long a__,b__; \
185 __asm__ __volatile__("rdmsr" \
186 : "=a" (a__), "=d" (b__) \
187 : "c" (msr)); \
188 val = a__ | (b__<<32); \
189} while(0)
190
191#define wrmsr(msr,val1,val2) \
192 __asm__ __volatile__("wrmsr" \
193 : /* no outputs */ \
194 : "c" (msr), "a" (val1), "d" (val2))
195
196#define wrmsrl(msr,val) wrmsr(msr,(__u32)((__u64)(val)),((__u64)(val))>>32)
197
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200198#define rdtsc(low,high) \
199 __asm__ __volatile__("rdtsc" : "=a" (low), "=d" (high))
200
201#define rdtscl(low) \
202 __asm__ __volatile__ ("rdtsc" : "=a" (low) : : "edx")
203
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200204
205#define rdtscll(val) do { \
206 unsigned int __a,__d; \
Mike Frysinger56986d42008-01-01 19:12:15 +0100207 __asm__ __volatile__("rdtsc" : "=a" (__a), "=d" (__d)); \
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200208 (val) = ((unsigned long)__a) | (((unsigned long)__d)<<32); \
209} while(0)
210
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200211#define write_tsc(val1,val2) wrmsr(0x10, val1, val2)
212
213#define write_rdtscp_aux(val) wrmsr(0xc0000103, val, 0)
214
215#define rdpmc(counter,low,high) \
216 __asm__ __volatile__("rdpmc" \
217 : "=a" (low), "=d" (high) \
218 : "c" (counter))
219
Mike Frysinger56986d42008-01-01 19:12:15 +0100220
Mike Frysinger56986d42008-01-01 19:12:15 +0100221#ifdef __KERNEL__
222
223/* wrmsr with exception handling */
224#define wrmsr_safe(msr,a,b) ({ int ret__; \
225 asm volatile("2: wrmsr ; xorl %0,%0\n" \
226 "1:\n\t" \
227 ".section .fixup,\"ax\"\n\t" \
228 "3: movl %4,%0 ; jmp 1b\n\t" \
229 ".previous\n\t" \
230 ".section __ex_table,\"a\"\n" \
231 " .align 8\n\t" \
232 " .quad 2b,3b\n\t" \
233 ".previous" \
234 : "=a" (ret__) \
235 : "c" (msr), "0" (a), "d" (b), "i" (-EFAULT)); \
236 ret__; })
237
238#define checking_wrmsrl(msr,val) wrmsr_safe(msr,(u32)(val),(u32)((val)>>32))
239
240#define rdmsr_safe(msr,a,b) \
241 ({ int ret__; \
242 asm volatile ("1: rdmsr\n" \
243 "2:\n" \
244 ".section .fixup,\"ax\"\n" \
245 "3: movl %4,%0\n" \
246 " jmp 2b\n" \
247 ".previous\n" \
248 ".section __ex_table,\"a\"\n" \
249 " .align 8\n" \
250 " .quad 1b,3b\n" \
251 ".previous":"=&bDS" (ret__), "=a"(*(a)), "=d"(*(b)) \
252 :"c"(msr), "i"(-EIO), "0"(0)); \
253 ret__; })
254
Glauber de Oliveira Costa751de832008-01-30 13:31:03 +0100255#endif /* __ASSEMBLY__ */
256
257#endif /* !__i386__ */
258
259#ifndef __ASSEMBLY__
260
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200261#ifdef CONFIG_SMP
262void rdmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h);
263void wrmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h);
264int rdmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h);
265int wrmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h);
266#else /* CONFIG_SMP */
267static inline void rdmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h)
268{
269 rdmsr(msr_no, *l, *h);
270}
271static inline void wrmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h)
272{
273 wrmsr(msr_no, l, h);
274}
275static inline int rdmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h)
276{
277 return rdmsr_safe(msr_no, l, h);
278}
279static inline int wrmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h)
280{
281 return wrmsr_safe(msr_no, l, h);
282}
283#endif /* CONFIG_SMP */
Mike Frysinger56986d42008-01-01 19:12:15 +0100284#endif /* __KERNEL__ */
Glauber de Oliveira Costa751de832008-01-30 13:31:03 +0100285#endif /* __ASSEMBLY__ */
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200286
Thomas Gleixner96a388d2007-10-11 11:20:03 +0200287#endif