blob: 48ad9d29484a03ff9d95f30c0b38de00e11896db [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
Ingo Molnar92767af2008-01-30 13:32:40 +0100116static __always_inline unsigned long long __native_read_tsc(void)
117{
118 DECLARE_ARGS(val, low, high);
119
Ingo Molnar92767af2008-01-30 13:32:40 +0100120 asm volatile("rdtsc" : EAX_EDX_RET(val, low, high));
Ingo Molnar92767af2008-01-30 13:32:40 +0100121
122 return EAX_EDX_VAL(val, low, high);
123}
124
Glauber de Oliveira Costab8d1fae2008-01-30 13:31:07 +0100125static inline unsigned long long native_read_pmc(int counter)
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200126{
Glauber de Oliveira Costac210d242008-01-30 13:31:07 +0100127 DECLARE_ARGS(val, low, high);
128
129 asm volatile("rdpmc" : EAX_EDX_RET(val, low, high) : "c" (counter));
130 return EAX_EDX_VAL(val, low, high);
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200131}
132
133#ifdef CONFIG_PARAVIRT
134#include <asm/paravirt.h>
Thomas Gleixner96a388d2007-10-11 11:20:03 +0200135#else
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200136#include <linux/errno.h>
137/*
138 * Access to machine-specific registers (available on 586 and better only)
139 * Note: the rd* operations modify the parameters directly (without using
140 * pointer indirection), this allows gcc to optimize better
141 */
142
Joe Perchesabb0ade2008-03-23 01:02:51 -0700143#define rdmsr(msr, val1, val2) \
144do { \
145 u64 __val = native_read_msr((msr)); \
146 (val1) = (u32)__val; \
147 (val2) = (u32)(__val >> 32); \
148} while (0)
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200149
Glauber de Oliveira Costac9dcda52008-01-30 13:31:07 +0100150static inline void wrmsr(unsigned msr, unsigned low, unsigned high)
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200151{
Glauber de Oliveira Costac9dcda52008-01-30 13:31:07 +0100152 native_write_msr(msr, low, high);
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200153}
154
Joe Perchesabb0ade2008-03-23 01:02:51 -0700155#define rdmsrl(msr, val) \
156 ((val) = native_read_msr((msr)))
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200157
Glauber de Oliveira Costac210d242008-01-30 13:31:07 +0100158#define wrmsrl(msr, val) \
Joe Perchesabb0ade2008-03-23 01:02:51 -0700159 native_write_msr((msr), (u32)((u64)(val)), (u32)((u64)(val) >> 32))
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200160
161/* wrmsr with exception handling */
Glauber de Oliveira Costac9dcda52008-01-30 13:31:07 +0100162static inline int wrmsr_safe(unsigned msr, unsigned low, unsigned high)
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200163{
Glauber de Oliveira Costac9dcda52008-01-30 13:31:07 +0100164 return native_write_msr_safe(msr, low, high);
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200165}
166
167/* rdmsr with exception handling */
Joe Perchesabb0ade2008-03-23 01:02:51 -0700168#define rdmsr_safe(msr, p1, p2) \
169({ \
170 int __err; \
171 u64 __val = native_read_msr_safe((msr), &__err); \
172 (*p1) = (u32)__val; \
173 (*p2) = (u32)(__val >> 32); \
174 __err; \
175})
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200176
Andi Kleen1de87bd2008-03-22 10:59:28 +0100177static inline int rdmsrl_safe(unsigned msr, unsigned long long *p)
178{
179 int err;
180
181 *p = native_read_msr_safe(msr, &err);
182 return err;
183}
Yinghai Lub05f78f2008-08-22 01:32:50 -0700184static inline int rdmsrl_amd_safe(unsigned msr, unsigned long long *p)
185{
186 int err;
187
188 *p = native_read_msr_amd_safe(msr, &err);
189 return err;
190}
Andi Kleen1de87bd2008-03-22 10:59:28 +0100191
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200192#define rdtscl(low) \
Ken Chen205516c2008-12-16 00:32:21 -0800193 ((low) = (u32)__native_read_tsc())
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200194
195#define rdtscll(val) \
Ken Chen205516c2008-12-16 00:32:21 -0800196 ((val) = __native_read_tsc())
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200197
Joe Perchesabb0ade2008-03-23 01:02:51 -0700198#define rdpmc(counter, low, high) \
199do { \
200 u64 _l = native_read_pmc((counter)); \
201 (low) = (u32)_l; \
202 (high) = (u32)(_l >> 32); \
203} while (0)
Glauber de Oliveira Costac210d242008-01-30 13:31:07 +0100204
Joe Perchesabb0ade2008-03-23 01:02:51 -0700205#define rdtscp(low, high, aux) \
206do { \
207 unsigned long long _val = native_read_tscp(&(aux)); \
208 (low) = (u32)_val; \
209 (high) = (u32)(_val >> 32); \
210} while (0)
Glauber de Oliveira Costac210d242008-01-30 13:31:07 +0100211
212#define rdtscpll(val, aux) (val) = native_read_tscp(&(aux))
213
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200214#endif /* !CONFIG_PARAVIRT */
215
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200216
Joe Perchesabb0ade2008-03-23 01:02:51 -0700217#define checking_wrmsrl(msr, val) wrmsr_safe((msr), (u32)(val), \
218 (u32)((val) >> 32))
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200219
Joe Perchesabb0ade2008-03-23 01:02:51 -0700220#define write_tsc(val1, val2) wrmsr(0x10, (val1), (val2))
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200221
Joe Perchesabb0ade2008-03-23 01:02:51 -0700222#define write_rdtscp_aux(val) wrmsr(0xc0000103, (val), 0)
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200223
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200224#ifdef CONFIG_SMP
H. Peter Anvinc6f31932008-08-25 17:27:21 -0700225int rdmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h);
226int wrmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h);
Borislav Petkovb034c192009-05-22 13:52:19 +0200227void rdmsr_on_cpus(const cpumask_t *mask, u32 msr_no, struct msr *msrs);
228void wrmsr_on_cpus(const cpumask_t *mask, u32 msr_no, struct msr *msrs);
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200229int rdmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h);
230int wrmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h);
231#else /* CONFIG_SMP */
H. Peter Anvinc6f31932008-08-25 17:27:21 -0700232static inline int rdmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h)
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200233{
234 rdmsr(msr_no, *l, *h);
H. Peter Anvinc6f31932008-08-25 17:27:21 -0700235 return 0;
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200236}
H. Peter Anvinc6f31932008-08-25 17:27:21 -0700237static inline int wrmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h)
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200238{
239 wrmsr(msr_no, l, h);
H. Peter Anvinc6f31932008-08-25 17:27:21 -0700240 return 0;
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200241}
Borislav Petkovb034c192009-05-22 13:52:19 +0200242static inline void rdmsr_on_cpus(const cpumask_t *m, u32 msr_no,
243 struct msr *msrs)
244{
245 rdmsr_on_cpu(0, msr_no, &(msrs[0].l), &(msrs[0].h));
246}
247static inline void wrmsr_on_cpus(const cpumask_t *m, u32 msr_no,
248 struct msr *msrs)
249{
250 wrmsr_on_cpu(0, msr_no, msrs[0].l, msrs[0].h);
251}
Joe Perchesabb0ade2008-03-23 01:02:51 -0700252static inline int rdmsr_safe_on_cpu(unsigned int cpu, u32 msr_no,
253 u32 *l, u32 *h)
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200254{
255 return rdmsr_safe(msr_no, l, h);
256}
257static inline int wrmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h)
258{
259 return wrmsr_safe(msr_no, l, h);
260}
261#endif /* CONFIG_SMP */
Glauber de Oliveira Costa751de832008-01-30 13:31:03 +0100262#endif /* __ASSEMBLY__ */
Glauber de Oliveira Costac210d242008-01-30 13:31:07 +0100263#endif /* __KERNEL__ */
H. Peter Anvin1965aae2008-10-22 22:26:29 -0700264#endif /* _ASM_X86_MSR_H */