blob: 22603764e7db35c6098cf5d58befe070fb496a4a [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>
Borislav Petkov6bc10962009-05-22 12:12:01 +020015#include <asm/cpumask.h>
16
17struct msr {
18 union {
19 struct {
20 u32 l;
21 u32 h;
22 };
23 u64 q;
24 };
25};
Glauber de Oliveira Costac210d242008-01-30 13:31:07 +010026
Andrew Morton1e160cc2008-01-30 13:31:17 +010027static inline unsigned long long native_read_tscp(unsigned int *aux)
Glauber de Oliveira Costa8f12dea2008-01-30 13:31:06 +010028{
29 unsigned long low, high;
Joe Perchesabb0ade2008-03-23 01:02:51 -070030 asm volatile(".byte 0x0f,0x01,0xf9"
31 : "=a" (low), "=d" (high), "=c" (*aux));
Max Asbock41aefdc2008-06-25 14:45:28 -070032 return low | ((u64)high << 32);
Glauber de Oliveira Costa8f12dea2008-01-30 13:31:06 +010033}
34
Glauber de Oliveira Costac210d242008-01-30 13:31:07 +010035/*
Jike Songd4f1b102008-10-17 13:25:07 +080036 * both i386 and x86_64 returns 64-bit value in edx:eax, but gcc's "A"
37 * constraint has different meanings. For i386, "A" means exactly
38 * edx:eax, while for x86_64 it doesn't mean rdx:rax or edx:eax. Instead,
39 * it means rax *or* rdx.
Glauber de Oliveira Costac210d242008-01-30 13:31:07 +010040 */
41#ifdef CONFIG_X86_64
42#define DECLARE_ARGS(val, low, high) unsigned low, high
Joe Perchesabb0ade2008-03-23 01:02:51 -070043#define EAX_EDX_VAL(val, low, high) ((low) | ((u64)(high) << 32))
Glauber de Oliveira Costac210d242008-01-30 13:31:07 +010044#define EAX_EDX_ARGS(val, low, high) "a" (low), "d" (high)
45#define EAX_EDX_RET(val, low, high) "=a" (low), "=d" (high)
46#else
47#define DECLARE_ARGS(val, low, high) unsigned long long val
48#define EAX_EDX_VAL(val, low, high) (val)
49#define EAX_EDX_ARGS(val, low, high) "A" (val)
50#define EAX_EDX_RET(val, low, high) "=A" (val)
Glauber de Oliveira Costa8f12dea2008-01-30 13:31:06 +010051#endif
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020052
53static inline unsigned long long native_read_msr(unsigned int msr)
54{
Glauber de Oliveira Costac210d242008-01-30 13:31:07 +010055 DECLARE_ARGS(val, low, high);
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020056
Glauber de Oliveira Costac210d242008-01-30 13:31:07 +010057 asm volatile("rdmsr" : EAX_EDX_RET(val, low, high) : "c" (msr));
58 return EAX_EDX_VAL(val, low, high);
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020059}
60
61static inline unsigned long long native_read_msr_safe(unsigned int msr,
62 int *err)
63{
Glauber de Oliveira Costac210d242008-01-30 13:31:07 +010064 DECLARE_ARGS(val, low, high);
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020065
H. Peter Anvin08970fc2008-08-25 22:39:15 -070066 asm volatile("2: rdmsr ; xor %[err],%[err]\n"
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020067 "1:\n\t"
68 ".section .fixup,\"ax\"\n\t"
H. Peter Anvin08970fc2008-08-25 22:39:15 -070069 "3: mov %[fault],%[err] ; jmp 1b\n\t"
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020070 ".previous\n\t"
Joe Perchesabb0ade2008-03-23 01:02:51 -070071 _ASM_EXTABLE(2b, 3b)
H. Peter Anvin08970fc2008-08-25 22:39:15 -070072 : [err] "=r" (*err), EAX_EDX_RET(val, low, high)
73 : "c" (msr), [fault] "i" (-EFAULT));
Glauber de Oliveira Costac210d242008-01-30 13:31:07 +010074 return EAX_EDX_VAL(val, low, high);
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020075}
76
Yinghai Lub05f78f2008-08-22 01:32:50 -070077static inline unsigned long long native_read_msr_amd_safe(unsigned int msr,
78 int *err)
79{
80 DECLARE_ARGS(val, low, high);
81
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020082 asm volatile("2: rdmsr ; xor %0,%0\n"
83 "1:\n\t"
84 ".section .fixup,\"ax\"\n\t"
85 "3: mov %3,%0 ; jmp 1b\n\t"
86 ".previous\n\t"
87 _ASM_EXTABLE(2b, 3b)
88 : "=r" (*err), EAX_EDX_RET(val, low, high)
Yinghai Lub05f78f2008-08-22 01:32:50 -070089 : "c" (msr), "D" (0x9c5a203a), "i" (-EFAULT));
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020090 return EAX_EDX_VAL(val, low, high);
91}
92
Glauber de Oliveira Costac9dcda52008-01-30 13:31:07 +010093static inline void native_write_msr(unsigned int msr,
94 unsigned low, unsigned high)
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020095{
Jeremy Fitzhardingeaf2b1c62008-06-25 00:18:59 -040096 asm volatile("wrmsr" : : "c" (msr), "a"(low), "d" (high) : "memory");
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020097}
98
Frederic Weisbecker0ca59dd2008-12-24 23:30:02 +010099/* Can be uninlined because referenced by paravirt */
100notrace static inline int native_write_msr_safe(unsigned int msr,
Glauber de Oliveira Costac9dcda52008-01-30 13:31:07 +0100101 unsigned low, unsigned high)
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200102{
103 int err;
H. Peter Anvin08970fc2008-08-25 22:39:15 -0700104 asm volatile("2: wrmsr ; xor %[err],%[err]\n"
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200105 "1:\n\t"
106 ".section .fixup,\"ax\"\n\t"
H. Peter Anvin08970fc2008-08-25 22:39:15 -0700107 "3: mov %[fault],%[err] ; jmp 1b\n\t"
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200108 ".previous\n\t"
Joe Perchesabb0ade2008-03-23 01:02:51 -0700109 _ASM_EXTABLE(2b, 3b)
H. Peter Anvin08970fc2008-08-25 22:39:15 -0700110 : [err] "=a" (err)
Glauber de Oliveira Costac9dcda52008-01-30 13:31:07 +0100111 : "c" (msr), "0" (low), "d" (high),
H. Peter Anvin08970fc2008-08-25 22:39:15 -0700112 [fault] "i" (-EFAULT)
Jeremy Fitzhardingeaf2b1c62008-06-25 00:18:59 -0400113 : "memory");
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200114 return err;
115}
116
Ingo Molnarcdc79572008-01-30 13:32:39 +0100117extern unsigned long long native_read_tsc(void);
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200118
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
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200195#define rdtscl(low) \
Ken Chen205516c2008-12-16 00:32:21 -0800196 ((low) = (u32)__native_read_tsc())
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200197
198#define rdtscll(val) \
Ken Chen205516c2008-12-16 00:32:21 -0800199 ((val) = __native_read_tsc())
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200200
Joe Perchesabb0ade2008-03-23 01:02:51 -0700201#define rdpmc(counter, low, high) \
202do { \
203 u64 _l = native_read_pmc((counter)); \
204 (low) = (u32)_l; \
205 (high) = (u32)(_l >> 32); \
206} while (0)
Glauber de Oliveira Costac210d242008-01-30 13:31:07 +0100207
Joe Perchesabb0ade2008-03-23 01:02:51 -0700208#define rdtscp(low, high, aux) \
209do { \
210 unsigned long long _val = native_read_tscp(&(aux)); \
211 (low) = (u32)_val; \
212 (high) = (u32)(_val >> 32); \
213} while (0)
Glauber de Oliveira Costac210d242008-01-30 13:31:07 +0100214
215#define rdtscpll(val, aux) (val) = native_read_tscp(&(aux))
216
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200217#endif /* !CONFIG_PARAVIRT */
218
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200219
Joe Perchesabb0ade2008-03-23 01:02:51 -0700220#define checking_wrmsrl(msr, val) wrmsr_safe((msr), (u32)(val), \
221 (u32)((val) >> 32))
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200222
Joe Perchesabb0ade2008-03-23 01:02:51 -0700223#define write_tsc(val1, val2) wrmsr(0x10, (val1), (val2))
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200224
Joe Perchesabb0ade2008-03-23 01:02:51 -0700225#define write_rdtscp_aux(val) wrmsr(0xc0000103, (val), 0)
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200226
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200227#ifdef CONFIG_SMP
H. Peter Anvinc6f31932008-08-25 17:27:21 -0700228int rdmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h);
229int wrmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h);
Borislav Petkovb034c192009-05-22 13:52:19 +0200230void rdmsr_on_cpus(const cpumask_t *mask, u32 msr_no, struct msr *msrs);
231void wrmsr_on_cpus(const cpumask_t *mask, u32 msr_no, struct msr *msrs);
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200232int rdmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h);
233int wrmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h);
234#else /* CONFIG_SMP */
H. Peter Anvinc6f31932008-08-25 17:27:21 -0700235static inline int rdmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h)
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200236{
237 rdmsr(msr_no, *l, *h);
H. Peter Anvinc6f31932008-08-25 17:27:21 -0700238 return 0;
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200239}
H. Peter Anvinc6f31932008-08-25 17:27:21 -0700240static inline int wrmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h)
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200241{
242 wrmsr(msr_no, l, h);
H. Peter Anvinc6f31932008-08-25 17:27:21 -0700243 return 0;
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200244}
Borislav Petkovb034c192009-05-22 13:52:19 +0200245static inline void rdmsr_on_cpus(const cpumask_t *m, u32 msr_no,
246 struct msr *msrs)
247{
248 rdmsr_on_cpu(0, msr_no, &(msrs[0].l), &(msrs[0].h));
249}
250static inline void wrmsr_on_cpus(const cpumask_t *m, u32 msr_no,
251 struct msr *msrs)
252{
253 wrmsr_on_cpu(0, msr_no, msrs[0].l, msrs[0].h);
254}
Joe Perchesabb0ade2008-03-23 01:02:51 -0700255static inline int rdmsr_safe_on_cpu(unsigned int cpu, u32 msr_no,
256 u32 *l, u32 *h)
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200257{
258 return rdmsr_safe(msr_no, l, h);
259}
260static inline int wrmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h)
261{
262 return wrmsr_safe(msr_no, l, h);
263}
264#endif /* CONFIG_SMP */
Glauber de Oliveira Costa751de832008-01-30 13:31:03 +0100265#endif /* __ASSEMBLY__ */
Glauber de Oliveira Costac210d242008-01-30 13:31:07 +0100266#endif /* __KERNEL__ */
267
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200268
H. Peter Anvin1965aae2008-10-22 22:26:29 -0700269#endif /* _ASM_X86_MSR_H */