blob: 638bf6241807a9ab9c36def7541c690ccb230e07 [file] [log] [blame]
H. Peter Anvin1965aae2008-10-22 22:26:29 -07001#ifndef _ASM_X86_MSR_H
2#define _ASM_X86_MSR_H
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +02003
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__
Glauber de Oliveira Costac210d242008-01-30 13:31:07 +010012
13#include <asm/asm.h>
14#include <asm/errno.h>
15
Andrew Morton1e160cc2008-01-30 13:31:17 +010016static inline unsigned long long native_read_tscp(unsigned int *aux)
Glauber de Oliveira Costa8f12dea2008-01-30 13:31:06 +010017{
18 unsigned long low, high;
Joe Perchesabb0ade2008-03-23 01:02:51 -070019 asm volatile(".byte 0x0f,0x01,0xf9"
20 : "=a" (low), "=d" (high), "=c" (*aux));
Max Asbock41aefdc2008-06-25 14:45:28 -070021 return low | ((u64)high << 32);
Glauber de Oliveira Costa8f12dea2008-01-30 13:31:06 +010022}
23
Glauber de Oliveira Costac210d242008-01-30 13:31:07 +010024/*
Jike Songd4f1b102008-10-17 13:25:07 +080025 * both i386 and x86_64 returns 64-bit value in edx:eax, but gcc's "A"
26 * constraint has different meanings. For i386, "A" means exactly
27 * edx:eax, while for x86_64 it doesn't mean rdx:rax or edx:eax. Instead,
28 * it means rax *or* rdx.
Glauber de Oliveira Costac210d242008-01-30 13:31:07 +010029 */
30#ifdef CONFIG_X86_64
31#define DECLARE_ARGS(val, low, high) unsigned low, high
Joe Perchesabb0ade2008-03-23 01:02:51 -070032#define EAX_EDX_VAL(val, low, high) ((low) | ((u64)(high) << 32))
Glauber de Oliveira Costac210d242008-01-30 13:31:07 +010033#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 Costa8f12dea2008-01-30 13:31:06 +010040#endif
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020041
42static inline unsigned long long native_read_msr(unsigned int msr)
43{
Glauber de Oliveira Costac210d242008-01-30 13:31:07 +010044 DECLARE_ARGS(val, low, high);
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020045
Glauber de Oliveira Costac210d242008-01-30 13:31:07 +010046 asm volatile("rdmsr" : EAX_EDX_RET(val, low, high) : "c" (msr));
47 return EAX_EDX_VAL(val, low, high);
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020048}
49
50static inline unsigned long long native_read_msr_safe(unsigned int msr,
51 int *err)
52{
Glauber de Oliveira Costac210d242008-01-30 13:31:07 +010053 DECLARE_ARGS(val, low, high);
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020054
H. Peter Anvin08970fc2008-08-25 22:39:15 -070055 asm volatile("2: rdmsr ; xor %[err],%[err]\n"
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020056 "1:\n\t"
57 ".section .fixup,\"ax\"\n\t"
H. Peter Anvin08970fc2008-08-25 22:39:15 -070058 "3: mov %[fault],%[err] ; jmp 1b\n\t"
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020059 ".previous\n\t"
Joe Perchesabb0ade2008-03-23 01:02:51 -070060 _ASM_EXTABLE(2b, 3b)
H. Peter Anvin08970fc2008-08-25 22:39:15 -070061 : [err] "=r" (*err), EAX_EDX_RET(val, low, high)
62 : "c" (msr), [fault] "i" (-EFAULT));
Glauber de Oliveira Costac210d242008-01-30 13:31:07 +010063 return EAX_EDX_VAL(val, low, high);
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020064}
65
Yinghai Lub05f78f2008-08-22 01:32:50 -070066static inline unsigned long long native_read_msr_amd_safe(unsigned int msr,
67 int *err)
68{
69 DECLARE_ARGS(val, low, high);
70
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020071 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 Lub05f78f2008-08-22 01:32:50 -070078 : "c" (msr), "D" (0x9c5a203a), "i" (-EFAULT));
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020079 return EAX_EDX_VAL(val, low, high);
80}
81
Glauber de Oliveira Costac9dcda52008-01-30 13:31:07 +010082static inline void native_write_msr(unsigned int msr,
83 unsigned low, unsigned high)
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020084{
Jeremy Fitzhardingeaf2b1c62008-06-25 00:18:59 -040085 asm volatile("wrmsr" : : "c" (msr), "a"(low), "d" (high) : "memory");
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020086}
87
Frederic Weisbecker0ca59dd2008-12-24 23:30:02 +010088/* Can be uninlined because referenced by paravirt */
89notrace static inline int native_write_msr_safe(unsigned int msr,
Glauber de Oliveira Costac9dcda52008-01-30 13:31:07 +010090 unsigned low, unsigned high)
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020091{
92 int err;
H. Peter Anvin08970fc2008-08-25 22:39:15 -070093 asm volatile("2: wrmsr ; xor %[err],%[err]\n"
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020094 "1:\n\t"
95 ".section .fixup,\"ax\"\n\t"
H. Peter Anvin08970fc2008-08-25 22:39:15 -070096 "3: mov %[fault],%[err] ; jmp 1b\n\t"
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020097 ".previous\n\t"
Joe Perchesabb0ade2008-03-23 01:02:51 -070098 _ASM_EXTABLE(2b, 3b)
H. Peter Anvin08970fc2008-08-25 22:39:15 -070099 : [err] "=a" (err)
Glauber de Oliveira Costac9dcda52008-01-30 13:31:07 +0100100 : "c" (msr), "0" (low), "d" (high),
H. Peter Anvin08970fc2008-08-25 22:39:15 -0700101 [fault] "i" (-EFAULT)
Jeremy Fitzhardingeaf2b1c62008-06-25 00:18:59 -0400102 : "memory");
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200103 return err;
104}
105
Ingo Molnarcdc79572008-01-30 13:32:39 +0100106extern unsigned long long native_read_tsc(void);
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200107
Ingo Molnar92767af2008-01-30 13:32:40 +0100108static __always_inline unsigned long long __native_read_tsc(void)
109{
110 DECLARE_ARGS(val, low, high);
111
Ingo Molnar92767af2008-01-30 13:32:40 +0100112 asm volatile("rdtsc" : EAX_EDX_RET(val, low, high));
Ingo Molnar92767af2008-01-30 13:32:40 +0100113
114 return EAX_EDX_VAL(val, low, high);
115}
116
Glauber de Oliveira Costab8d1fae2008-01-30 13:31:07 +0100117static inline unsigned long long native_read_pmc(int counter)
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200118{
Glauber de Oliveira Costac210d242008-01-30 13:31:07 +0100119 DECLARE_ARGS(val, low, high);
120
121 asm volatile("rdpmc" : EAX_EDX_RET(val, low, high) : "c" (counter));
122 return EAX_EDX_VAL(val, low, high);
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200123}
124
125#ifdef CONFIG_PARAVIRT
126#include <asm/paravirt.h>
Thomas Gleixner96a388d2007-10-11 11:20:03 +0200127#else
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200128#include <linux/errno.h>
129/*
130 * Access to machine-specific registers (available on 586 and better only)
131 * Note: the rd* operations modify the parameters directly (without using
132 * pointer indirection), this allows gcc to optimize better
133 */
134
Joe Perchesabb0ade2008-03-23 01:02:51 -0700135#define rdmsr(msr, val1, val2) \
136do { \
137 u64 __val = native_read_msr((msr)); \
138 (val1) = (u32)__val; \
139 (val2) = (u32)(__val >> 32); \
140} while (0)
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200141
Glauber de Oliveira Costac9dcda52008-01-30 13:31:07 +0100142static inline void wrmsr(unsigned msr, unsigned low, unsigned high)
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200143{
Glauber de Oliveira Costac9dcda52008-01-30 13:31:07 +0100144 native_write_msr(msr, low, high);
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200145}
146
Joe Perchesabb0ade2008-03-23 01:02:51 -0700147#define rdmsrl(msr, val) \
148 ((val) = native_read_msr((msr)))
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200149
Glauber de Oliveira Costac210d242008-01-30 13:31:07 +0100150#define wrmsrl(msr, val) \
Joe Perchesabb0ade2008-03-23 01:02:51 -0700151 native_write_msr((msr), (u32)((u64)(val)), (u32)((u64)(val) >> 32))
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200152
153/* wrmsr with exception handling */
Glauber de Oliveira Costac9dcda52008-01-30 13:31:07 +0100154static inline int wrmsr_safe(unsigned msr, unsigned low, unsigned high)
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200155{
Glauber de Oliveira Costac9dcda52008-01-30 13:31:07 +0100156 return native_write_msr_safe(msr, low, high);
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200157}
158
159/* rdmsr with exception handling */
Joe Perchesabb0ade2008-03-23 01:02:51 -0700160#define rdmsr_safe(msr, p1, p2) \
161({ \
162 int __err; \
163 u64 __val = native_read_msr_safe((msr), &__err); \
164 (*p1) = (u32)__val; \
165 (*p2) = (u32)(__val >> 32); \
166 __err; \
167})
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200168
Andi Kleen1de87bd2008-03-22 10:59:28 +0100169static inline int rdmsrl_safe(unsigned msr, unsigned long long *p)
170{
171 int err;
172
173 *p = native_read_msr_safe(msr, &err);
174 return err;
175}
Yinghai Lub05f78f2008-08-22 01:32:50 -0700176static inline int rdmsrl_amd_safe(unsigned msr, unsigned long long *p)
177{
178 int err;
179
180 *p = native_read_msr_amd_safe(msr, &err);
181 return err;
182}
Andi Kleen1de87bd2008-03-22 10:59:28 +0100183
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200184#define rdtscl(low) \
Ken Chen205516c2008-12-16 00:32:21 -0800185 ((low) = (u32)__native_read_tsc())
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200186
187#define rdtscll(val) \
Ken Chen205516c2008-12-16 00:32:21 -0800188 ((val) = __native_read_tsc())
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200189
Joe Perchesabb0ade2008-03-23 01:02:51 -0700190#define rdpmc(counter, low, high) \
191do { \
192 u64 _l = native_read_pmc((counter)); \
193 (low) = (u32)_l; \
194 (high) = (u32)(_l >> 32); \
195} while (0)
Glauber de Oliveira Costac210d242008-01-30 13:31:07 +0100196
Joe Perchesabb0ade2008-03-23 01:02:51 -0700197#define rdtscp(low, high, aux) \
198do { \
199 unsigned long long _val = native_read_tscp(&(aux)); \
200 (low) = (u32)_val; \
201 (high) = (u32)(_val >> 32); \
202} while (0)
Glauber de Oliveira Costac210d242008-01-30 13:31:07 +0100203
204#define rdtscpll(val, aux) (val) = native_read_tscp(&(aux))
205
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200206#endif /* !CONFIG_PARAVIRT */
207
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200208
Joe Perchesabb0ade2008-03-23 01:02:51 -0700209#define checking_wrmsrl(msr, val) wrmsr_safe((msr), (u32)(val), \
210 (u32)((val) >> 32))
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200211
Joe Perchesabb0ade2008-03-23 01:02:51 -0700212#define write_tsc(val1, val2) wrmsr(0x10, (val1), (val2))
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200213
Joe Perchesabb0ade2008-03-23 01:02:51 -0700214#define write_rdtscp_aux(val) wrmsr(0xc0000103, (val), 0)
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200215
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200216#ifdef CONFIG_SMP
H. Peter Anvinc6f31932008-08-25 17:27:21 -0700217int rdmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h);
218int wrmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h);
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200219int rdmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h);
220int wrmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h);
221#else /* CONFIG_SMP */
H. Peter Anvinc6f31932008-08-25 17:27:21 -0700222static inline int rdmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h)
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200223{
224 rdmsr(msr_no, *l, *h);
H. Peter Anvinc6f31932008-08-25 17:27:21 -0700225 return 0;
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200226}
H. Peter Anvinc6f31932008-08-25 17:27:21 -0700227static inline int wrmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h)
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200228{
229 wrmsr(msr_no, l, h);
H. Peter Anvinc6f31932008-08-25 17:27:21 -0700230 return 0;
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200231}
Joe Perchesabb0ade2008-03-23 01:02:51 -0700232static inline int rdmsr_safe_on_cpu(unsigned int cpu, u32 msr_no,
233 u32 *l, u32 *h)
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200234{
235 return rdmsr_safe(msr_no, l, h);
236}
237static inline int wrmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h)
238{
239 return wrmsr_safe(msr_no, l, h);
240}
241#endif /* CONFIG_SMP */
Glauber de Oliveira Costa751de832008-01-30 13:31:03 +0100242#endif /* __ASSEMBLY__ */
Glauber de Oliveira Costac210d242008-01-30 13:31:07 +0100243#endif /* __KERNEL__ */
244
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200245
H. Peter Anvin1965aae2008-10-22 22:26:29 -0700246#endif /* _ASM_X86_MSR_H */