blob: 0648b2d57a383626309c566157c5e4ea4c6d51d4 [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
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020010#ifdef __i386__
11
Thomas Gleixner96a388d2007-10-11 11:20:03 +020012#ifdef __KERNEL__
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020013#ifndef __ASSEMBLY__
14
15#include <asm/errno.h>
16
17static inline unsigned long long native_read_msr(unsigned int msr)
18{
19 unsigned long long val;
20
21 asm volatile("rdmsr" : "=A" (val) : "c" (msr));
22 return val;
23}
24
25static inline unsigned long long native_read_msr_safe(unsigned int msr,
26 int *err)
27{
28 unsigned long long val;
29
30 asm volatile("2: rdmsr ; xorl %0,%0\n"
31 "1:\n\t"
32 ".section .fixup,\"ax\"\n\t"
33 "3: movl %3,%0 ; jmp 1b\n\t"
34 ".previous\n\t"
35 ".section __ex_table,\"a\"\n"
36 " .align 4\n\t"
37 " .long 2b,3b\n\t"
38 ".previous"
39 : "=r" (*err), "=A" (val)
40 : "c" (msr), "i" (-EFAULT));
41
42 return val;
43}
44
45static inline void native_write_msr(unsigned int msr, unsigned long long val)
46{
47 asm volatile("wrmsr" : : "c" (msr), "A"(val));
48}
49
50static inline int native_write_msr_safe(unsigned int msr,
51 unsigned long long val)
52{
53 int err;
54 asm volatile("2: wrmsr ; xorl %0,%0\n"
55 "1:\n\t"
56 ".section .fixup,\"ax\"\n\t"
57 "3: movl %4,%0 ; jmp 1b\n\t"
58 ".previous\n\t"
59 ".section __ex_table,\"a\"\n"
60 " .align 4\n\t"
61 " .long 2b,3b\n\t"
62 ".previous"
63 : "=a" (err)
64 : "c" (msr), "0" ((u32)val), "d" ((u32)(val>>32)),
65 "i" (-EFAULT));
66 return err;
67}
68
69static inline unsigned long long native_read_tsc(void)
70{
71 unsigned long long val;
72 asm volatile("rdtsc" : "=A" (val));
73 return val;
74}
75
76static inline unsigned long long native_read_pmc(void)
77{
78 unsigned long long val;
79 asm volatile("rdpmc" : "=A" (val));
80 return val;
81}
82
83#ifdef CONFIG_PARAVIRT
84#include <asm/paravirt.h>
Thomas Gleixner96a388d2007-10-11 11:20:03 +020085#else
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020086#include <linux/errno.h>
87/*
88 * Access to machine-specific registers (available on 586 and better only)
89 * Note: the rd* operations modify the parameters directly (without using
90 * pointer indirection), this allows gcc to optimize better
91 */
92
93#define rdmsr(msr,val1,val2) \
94 do { \
95 u64 __val = native_read_msr(msr); \
96 (val1) = (u32)__val; \
97 (val2) = (u32)(__val >> 32); \
98 } while(0)
99
100static inline void wrmsr(u32 __msr, u32 __low, u32 __high)
101{
102 native_write_msr(__msr, ((u64)__high << 32) | __low);
103}
104
105#define rdmsrl(msr,val) \
106 ((val) = native_read_msr(msr))
107
108#define wrmsrl(msr,val) native_write_msr(msr, val)
109
110/* wrmsr with exception handling */
111static inline int wrmsr_safe(u32 __msr, u32 __low, u32 __high)
112{
113 return native_write_msr_safe(__msr, ((u64)__high << 32) | __low);
114}
115
116/* rdmsr with exception handling */
117#define rdmsr_safe(msr,p1,p2) \
118 ({ \
119 int __err; \
120 u64 __val = native_read_msr_safe(msr, &__err); \
121 (*p1) = (u32)__val; \
122 (*p2) = (u32)(__val >> 32); \
123 __err; \
124 })
125
126#define rdtscl(low) \
127 ((low) = (u32)native_read_tsc())
128
129#define rdtscll(val) \
130 ((val) = native_read_tsc())
131
132#define write_tsc(val1,val2) wrmsr(0x10, val1, val2)
133
134#define rdpmc(counter,low,high) \
135 do { \
136 u64 _l = native_read_pmc(); \
137 (low) = (u32)_l; \
138 (high) = (u32)(_l >> 32); \
139 } while(0)
140#endif /* !CONFIG_PARAVIRT */
141
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200142#endif /* ! __ASSEMBLY__ */
143#endif /* __KERNEL__ */
144
145#else /* __i386__ */
146
147#ifndef __ASSEMBLY__
148#include <linux/errno.h>
149/*
150 * Access to machine-specific registers (available on 586 and better only)
151 * Note: the rd* operations modify the parameters directly (without using
152 * pointer indirection), this allows gcc to optimize better
153 */
154
155#define rdmsr(msr,val1,val2) \
156 __asm__ __volatile__("rdmsr" \
157 : "=a" (val1), "=d" (val2) \
158 : "c" (msr))
159
160
161#define rdmsrl(msr,val) do { unsigned long a__,b__; \
162 __asm__ __volatile__("rdmsr" \
163 : "=a" (a__), "=d" (b__) \
164 : "c" (msr)); \
165 val = a__ | (b__<<32); \
166} while(0)
167
168#define wrmsr(msr,val1,val2) \
169 __asm__ __volatile__("wrmsr" \
170 : /* no outputs */ \
171 : "c" (msr), "a" (val1), "d" (val2))
172
173#define wrmsrl(msr,val) wrmsr(msr,(__u32)((__u64)(val)),((__u64)(val))>>32)
174
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200175#define rdtsc(low,high) \
176 __asm__ __volatile__("rdtsc" : "=a" (low), "=d" (high))
177
178#define rdtscl(low) \
179 __asm__ __volatile__ ("rdtsc" : "=a" (low) : : "edx")
180
181#define rdtscp(low,high,aux) \
Mike Frysinger56986d42008-01-01 19:12:15 +0100182 __asm__ __volatile__ (".byte 0x0f,0x01,0xf9" : "=a" (low), "=d" (high), "=c" (aux))
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200183
184#define rdtscll(val) do { \
185 unsigned int __a,__d; \
Mike Frysinger56986d42008-01-01 19:12:15 +0100186 __asm__ __volatile__("rdtsc" : "=a" (__a), "=d" (__d)); \
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200187 (val) = ((unsigned long)__a) | (((unsigned long)__d)<<32); \
188} while(0)
189
190#define rdtscpll(val, aux) do { \
191 unsigned long __a, __d; \
Mike Frysinger56986d42008-01-01 19:12:15 +0100192 __asm__ __volatile__ (".byte 0x0f,0x01,0xf9" : "=a" (__a), "=d" (__d), "=c" (aux)); \
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200193 (val) = (__d << 32) | __a; \
194} while (0)
195
196#define write_tsc(val1,val2) wrmsr(0x10, val1, val2)
197
198#define write_rdtscp_aux(val) wrmsr(0xc0000103, val, 0)
199
200#define rdpmc(counter,low,high) \
201 __asm__ __volatile__("rdpmc" \
202 : "=a" (low), "=d" (high) \
203 : "c" (counter))
204
Mike Frysinger56986d42008-01-01 19:12:15 +0100205
Mike Frysinger56986d42008-01-01 19:12:15 +0100206#ifdef __KERNEL__
207
208/* wrmsr with exception handling */
209#define wrmsr_safe(msr,a,b) ({ int ret__; \
210 asm volatile("2: wrmsr ; xorl %0,%0\n" \
211 "1:\n\t" \
212 ".section .fixup,\"ax\"\n\t" \
213 "3: movl %4,%0 ; jmp 1b\n\t" \
214 ".previous\n\t" \
215 ".section __ex_table,\"a\"\n" \
216 " .align 8\n\t" \
217 " .quad 2b,3b\n\t" \
218 ".previous" \
219 : "=a" (ret__) \
220 : "c" (msr), "0" (a), "d" (b), "i" (-EFAULT)); \
221 ret__; })
222
223#define checking_wrmsrl(msr,val) wrmsr_safe(msr,(u32)(val),(u32)((val)>>32))
224
225#define rdmsr_safe(msr,a,b) \
226 ({ int ret__; \
227 asm volatile ("1: rdmsr\n" \
228 "2:\n" \
229 ".section .fixup,\"ax\"\n" \
230 "3: movl %4,%0\n" \
231 " jmp 2b\n" \
232 ".previous\n" \
233 ".section __ex_table,\"a\"\n" \
234 " .align 8\n" \
235 " .quad 1b,3b\n" \
236 ".previous":"=&bDS" (ret__), "=a"(*(a)), "=d"(*(b)) \
237 :"c"(msr), "i"(-EIO), "0"(0)); \
238 ret__; })
239
Glauber de Oliveira Costa751de832008-01-30 13:31:03 +0100240#endif /* __ASSEMBLY__ */
241
242#endif /* !__i386__ */
243
244#ifndef __ASSEMBLY__
245
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200246#ifdef CONFIG_SMP
247void rdmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h);
248void wrmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h);
249int rdmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h);
250int wrmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h);
251#else /* CONFIG_SMP */
252static inline void rdmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h)
253{
254 rdmsr(msr_no, *l, *h);
255}
256static inline void wrmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h)
257{
258 wrmsr(msr_no, l, h);
259}
260static inline int rdmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h)
261{
262 return rdmsr_safe(msr_no, l, h);
263}
264static inline int wrmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h)
265{
266 return wrmsr_safe(msr_no, l, h);
267}
268#endif /* CONFIG_SMP */
Mike Frysinger56986d42008-01-01 19:12:15 +0100269#endif /* __KERNEL__ */
Glauber de Oliveira Costa751de832008-01-30 13:31:03 +0100270#endif /* __ASSEMBLY__ */
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200271
Thomas Gleixner96a388d2007-10-11 11:20:03 +0200272#endif