blob: 184d4a113961adeba656ae4ef25b1dfbc890946f [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
Glauber de Oliveira Costa8f12dea2008-01-30 13:31:06 +01006#ifdef __KERNEL__
7#ifndef __ASSEMBLY__
Glauber de Oliveira Costac210d242008-01-30 13:31:07 +01008
Jaswinder Singh Rajput8fa62ad2009-06-17 14:11:10 +05309#include <linux/types.h>
Glauber de Oliveira Costac210d242008-01-30 13:31:07 +010010#include <asm/asm.h>
11#include <asm/errno.h>
Borislav Petkov6bc10962009-05-22 12:12:01 +020012#include <asm/cpumask.h>
13
14struct msr {
15 union {
16 struct {
17 u32 l;
18 u32 h;
19 };
20 u64 q;
21 };
22};
Glauber de Oliveira Costac210d242008-01-30 13:31:07 +010023
Andrew Morton1e160cc2008-01-30 13:31:17 +010024static inline unsigned long long native_read_tscp(unsigned int *aux)
Glauber de Oliveira Costa8f12dea2008-01-30 13:31:06 +010025{
26 unsigned long low, high;
Joe Perchesabb0ade2008-03-23 01:02:51 -070027 asm volatile(".byte 0x0f,0x01,0xf9"
28 : "=a" (low), "=d" (high), "=c" (*aux));
Max Asbock41aefdc2008-06-25 14:45:28 -070029 return low | ((u64)high << 32);
Glauber de Oliveira Costa8f12dea2008-01-30 13:31:06 +010030}
31
Glauber de Oliveira Costac210d242008-01-30 13:31:07 +010032/*
Jike Songd4f1b102008-10-17 13:25:07 +080033 * both i386 and x86_64 returns 64-bit value in edx:eax, but gcc's "A"
34 * constraint has different meanings. For i386, "A" means exactly
35 * edx:eax, while for x86_64 it doesn't mean rdx:rax or edx:eax. Instead,
36 * it means rax *or* rdx.
Glauber de Oliveira Costac210d242008-01-30 13:31:07 +010037 */
38#ifdef CONFIG_X86_64
39#define DECLARE_ARGS(val, low, high) unsigned low, high
Joe Perchesabb0ade2008-03-23 01:02:51 -070040#define EAX_EDX_VAL(val, low, high) ((low) | ((u64)(high) << 32))
Glauber de Oliveira Costac210d242008-01-30 13:31:07 +010041#define EAX_EDX_ARGS(val, low, high) "a" (low), "d" (high)
42#define EAX_EDX_RET(val, low, high) "=a" (low), "=d" (high)
43#else
44#define DECLARE_ARGS(val, low, high) unsigned long long val
45#define EAX_EDX_VAL(val, low, high) (val)
46#define EAX_EDX_ARGS(val, low, high) "A" (val)
47#define EAX_EDX_RET(val, low, high) "=A" (val)
Glauber de Oliveira Costa8f12dea2008-01-30 13:31:06 +010048#endif
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020049
50static inline unsigned long long native_read_msr(unsigned int msr)
51{
Glauber de Oliveira Costac210d242008-01-30 13:31:07 +010052 DECLARE_ARGS(val, low, high);
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020053
Glauber de Oliveira Costac210d242008-01-30 13:31:07 +010054 asm volatile("rdmsr" : EAX_EDX_RET(val, low, high) : "c" (msr));
55 return EAX_EDX_VAL(val, low, high);
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020056}
57
58static inline unsigned long long native_read_msr_safe(unsigned int msr,
59 int *err)
60{
Glauber de Oliveira Costac210d242008-01-30 13:31:07 +010061 DECLARE_ARGS(val, low, high);
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020062
H. Peter Anvin08970fc2008-08-25 22:39:15 -070063 asm volatile("2: rdmsr ; xor %[err],%[err]\n"
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020064 "1:\n\t"
65 ".section .fixup,\"ax\"\n\t"
H. Peter Anvin08970fc2008-08-25 22:39:15 -070066 "3: mov %[fault],%[err] ; jmp 1b\n\t"
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020067 ".previous\n\t"
Joe Perchesabb0ade2008-03-23 01:02:51 -070068 _ASM_EXTABLE(2b, 3b)
H. Peter Anvin08970fc2008-08-25 22:39:15 -070069 : [err] "=r" (*err), EAX_EDX_RET(val, low, high)
70 : "c" (msr), [fault] "i" (-EFAULT));
Glauber de Oliveira Costac210d242008-01-30 13:31:07 +010071 return EAX_EDX_VAL(val, low, high);
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020072}
73
Yinghai Lub05f78f2008-08-22 01:32:50 -070074static inline unsigned long long native_read_msr_amd_safe(unsigned int msr,
75 int *err)
76{
77 DECLARE_ARGS(val, low, high);
78
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020079 asm volatile("2: rdmsr ; xor %0,%0\n"
80 "1:\n\t"
81 ".section .fixup,\"ax\"\n\t"
82 "3: mov %3,%0 ; jmp 1b\n\t"
83 ".previous\n\t"
84 _ASM_EXTABLE(2b, 3b)
85 : "=r" (*err), EAX_EDX_RET(val, low, high)
Yinghai Lub05f78f2008-08-22 01:32:50 -070086 : "c" (msr), "D" (0x9c5a203a), "i" (-EFAULT));
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020087 return EAX_EDX_VAL(val, low, high);
88}
89
Glauber de Oliveira Costac9dcda52008-01-30 13:31:07 +010090static inline void native_write_msr(unsigned int msr,
91 unsigned low, unsigned high)
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020092{
Jeremy Fitzhardingeaf2b1c62008-06-25 00:18:59 -040093 asm volatile("wrmsr" : : "c" (msr), "a"(low), "d" (high) : "memory");
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020094}
95
Frederic Weisbecker0ca59dd2008-12-24 23:30:02 +010096/* Can be uninlined because referenced by paravirt */
97notrace static inline int native_write_msr_safe(unsigned int msr,
Glauber de Oliveira Costac9dcda52008-01-30 13:31:07 +010098 unsigned low, unsigned high)
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020099{
100 int err;
H. Peter Anvin08970fc2008-08-25 22:39:15 -0700101 asm volatile("2: wrmsr ; xor %[err],%[err]\n"
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200102 "1:\n\t"
103 ".section .fixup,\"ax\"\n\t"
H. Peter Anvin08970fc2008-08-25 22:39:15 -0700104 "3: mov %[fault],%[err] ; jmp 1b\n\t"
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200105 ".previous\n\t"
Joe Perchesabb0ade2008-03-23 01:02:51 -0700106 _ASM_EXTABLE(2b, 3b)
H. Peter Anvin08970fc2008-08-25 22:39:15 -0700107 : [err] "=a" (err)
Glauber de Oliveira Costac9dcda52008-01-30 13:31:07 +0100108 : "c" (msr), "0" (low), "d" (high),
H. Peter Anvin08970fc2008-08-25 22:39:15 -0700109 [fault] "i" (-EFAULT)
Jeremy Fitzhardingeaf2b1c62008-06-25 00:18:59 -0400110 : "memory");
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200111 return err;
112}
113
Ingo Molnarcdc79572008-01-30 13:32:39 +0100114extern unsigned long long native_read_tsc(void);
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200115
Borislav Petkov132ec922009-08-31 09:50:09 +0200116extern int native_rdmsr_safe_regs(u32 *regs);
117extern int native_wrmsr_safe_regs(u32 *regs);
118
Ingo Molnar92767af2008-01-30 13:32:40 +0100119static __always_inline unsigned long long __native_read_tsc(void)
120{
121 DECLARE_ARGS(val, low, high);
122
Ingo Molnar92767af2008-01-30 13:32:40 +0100123 asm volatile("rdtsc" : EAX_EDX_RET(val, low, high));
Ingo Molnar92767af2008-01-30 13:32:40 +0100124
125 return EAX_EDX_VAL(val, low, high);
126}
127
Glauber de Oliveira Costab8d1fae2008-01-30 13:31:07 +0100128static inline unsigned long long native_read_pmc(int counter)
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200129{
Glauber de Oliveira Costac210d242008-01-30 13:31:07 +0100130 DECLARE_ARGS(val, low, high);
131
132 asm volatile("rdpmc" : EAX_EDX_RET(val, low, high) : "c" (counter));
133 return EAX_EDX_VAL(val, low, high);
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200134}
135
136#ifdef CONFIG_PARAVIRT
137#include <asm/paravirt.h>
Thomas Gleixner96a388d2007-10-11 11:20:03 +0200138#else
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200139#include <linux/errno.h>
140/*
141 * Access to machine-specific registers (available on 586 and better only)
142 * Note: the rd* operations modify the parameters directly (without using
143 * pointer indirection), this allows gcc to optimize better
144 */
145
Joe Perchesabb0ade2008-03-23 01:02:51 -0700146#define rdmsr(msr, val1, val2) \
147do { \
148 u64 __val = native_read_msr((msr)); \
149 (val1) = (u32)__val; \
150 (val2) = (u32)(__val >> 32); \
151} while (0)
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200152
Glauber de Oliveira Costac9dcda52008-01-30 13:31:07 +0100153static inline void wrmsr(unsigned msr, unsigned low, unsigned high)
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200154{
Glauber de Oliveira Costac9dcda52008-01-30 13:31:07 +0100155 native_write_msr(msr, low, high);
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200156}
157
Joe Perchesabb0ade2008-03-23 01:02:51 -0700158#define rdmsrl(msr, val) \
159 ((val) = native_read_msr((msr)))
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200160
Glauber de Oliveira Costac210d242008-01-30 13:31:07 +0100161#define wrmsrl(msr, val) \
Joe Perchesabb0ade2008-03-23 01:02:51 -0700162 native_write_msr((msr), (u32)((u64)(val)), (u32)((u64)(val) >> 32))
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200163
164/* wrmsr with exception handling */
Glauber de Oliveira Costac9dcda52008-01-30 13:31:07 +0100165static inline int wrmsr_safe(unsigned msr, unsigned low, unsigned high)
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200166{
Glauber de Oliveira Costac9dcda52008-01-30 13:31:07 +0100167 return native_write_msr_safe(msr, low, high);
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200168}
169
170/* rdmsr with exception handling */
Joe Perchesabb0ade2008-03-23 01:02:51 -0700171#define rdmsr_safe(msr, p1, p2) \
172({ \
173 int __err; \
174 u64 __val = native_read_msr_safe((msr), &__err); \
175 (*p1) = (u32)__val; \
176 (*p2) = (u32)(__val >> 32); \
177 __err; \
178})
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200179
Andi Kleen1de87bd2008-03-22 10:59:28 +0100180static inline int rdmsrl_safe(unsigned msr, unsigned long long *p)
181{
182 int err;
183
184 *p = native_read_msr_safe(msr, &err);
185 return err;
186}
Yinghai Lub05f78f2008-08-22 01:32:50 -0700187static inline int rdmsrl_amd_safe(unsigned msr, unsigned long long *p)
188{
189 int err;
190
191 *p = native_read_msr_amd_safe(msr, &err);
192 return err;
193}
Andi Kleen1de87bd2008-03-22 10:59:28 +0100194
Borislav Petkov132ec922009-08-31 09:50:09 +0200195static inline int rdmsr_safe_regs(u32 *regs)
196{
197 return native_rdmsr_safe_regs(regs);
198}
199
200static inline int wrmsr_safe_regs(u32 *regs)
201{
202 return native_wrmsr_safe_regs(regs);
203}
204
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200205#define rdtscl(low) \
Ken Chen205516c2008-12-16 00:32:21 -0800206 ((low) = (u32)__native_read_tsc())
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200207
208#define rdtscll(val) \
Ken Chen205516c2008-12-16 00:32:21 -0800209 ((val) = __native_read_tsc())
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200210
Joe Perchesabb0ade2008-03-23 01:02:51 -0700211#define rdpmc(counter, low, high) \
212do { \
213 u64 _l = native_read_pmc((counter)); \
214 (low) = (u32)_l; \
215 (high) = (u32)(_l >> 32); \
216} while (0)
Glauber de Oliveira Costac210d242008-01-30 13:31:07 +0100217
Joe Perchesabb0ade2008-03-23 01:02:51 -0700218#define rdtscp(low, high, aux) \
219do { \
220 unsigned long long _val = native_read_tscp(&(aux)); \
221 (low) = (u32)_val; \
222 (high) = (u32)(_val >> 32); \
223} while (0)
Glauber de Oliveira Costac210d242008-01-30 13:31:07 +0100224
225#define rdtscpll(val, aux) (val) = native_read_tscp(&(aux))
226
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200227#endif /* !CONFIG_PARAVIRT */
228
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200229
Joe Perchesabb0ade2008-03-23 01:02:51 -0700230#define checking_wrmsrl(msr, val) wrmsr_safe((msr), (u32)(val), \
231 (u32)((val) >> 32))
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200232
Joe Perchesabb0ade2008-03-23 01:02:51 -0700233#define write_tsc(val1, val2) wrmsr(0x10, (val1), (val2))
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200234
Joe Perchesabb0ade2008-03-23 01:02:51 -0700235#define write_rdtscp_aux(val) wrmsr(0xc0000103, (val), 0)
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200236
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200237#ifdef CONFIG_SMP
H. Peter Anvinc6f31932008-08-25 17:27:21 -0700238int rdmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h);
239int wrmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h);
Borislav Petkovb034c192009-05-22 13:52:19 +0200240void rdmsr_on_cpus(const cpumask_t *mask, u32 msr_no, struct msr *msrs);
241void wrmsr_on_cpus(const cpumask_t *mask, u32 msr_no, struct msr *msrs);
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200242int rdmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h);
243int wrmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h);
244#else /* CONFIG_SMP */
H. Peter Anvinc6f31932008-08-25 17:27:21 -0700245static inline int rdmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h)
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200246{
247 rdmsr(msr_no, *l, *h);
H. Peter Anvinc6f31932008-08-25 17:27:21 -0700248 return 0;
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200249}
H. Peter Anvinc6f31932008-08-25 17:27:21 -0700250static inline int wrmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h)
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200251{
252 wrmsr(msr_no, l, h);
H. Peter Anvinc6f31932008-08-25 17:27:21 -0700253 return 0;
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200254}
Borislav Petkovb034c192009-05-22 13:52:19 +0200255static inline void rdmsr_on_cpus(const cpumask_t *m, u32 msr_no,
256 struct msr *msrs)
257{
258 rdmsr_on_cpu(0, msr_no, &(msrs[0].l), &(msrs[0].h));
259}
260static inline void wrmsr_on_cpus(const cpumask_t *m, u32 msr_no,
261 struct msr *msrs)
262{
263 wrmsr_on_cpu(0, msr_no, msrs[0].l, msrs[0].h);
264}
Joe Perchesabb0ade2008-03-23 01:02:51 -0700265static inline int rdmsr_safe_on_cpu(unsigned int cpu, u32 msr_no,
266 u32 *l, u32 *h)
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200267{
268 return rdmsr_safe(msr_no, l, h);
269}
270static inline int wrmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h)
271{
272 return wrmsr_safe(msr_no, l, h);
273}
274#endif /* CONFIG_SMP */
Glauber de Oliveira Costa751de832008-01-30 13:31:03 +0100275#endif /* __ASSEMBLY__ */
Glauber de Oliveira Costac210d242008-01-30 13:31:07 +0100276#endif /* __KERNEL__ */
H. Peter Anvin1965aae2008-10-22 22:26:29 -0700277#endif /* _ASM_X86_MSR_H */