blob: e139b13f2a33a3572d91e4f7297952e1bf8a1ffc [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
David Howellsaf170c52012-12-14 22:37:13 +00004#include <uapi/asm/msr.h>
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +02005
Glauber de Oliveira Costa8f12dea2008-01-30 13:31:06 +01006#ifndef __ASSEMBLY__
Glauber de Oliveira Costac210d242008-01-30 13:31:07 +01007
8#include <asm/asm.h>
9#include <asm/errno.h>
Borislav Petkov6bc10962009-05-22 12:12:01 +020010#include <asm/cpumask.h>
11
12struct msr {
13 union {
14 struct {
15 u32 l;
16 u32 h;
17 };
18 u64 q;
19 };
20};
Glauber de Oliveira Costac210d242008-01-30 13:31:07 +010021
Borislav Petkov6ede31e2009-12-17 00:16:25 +010022struct msr_info {
23 u32 msr_no;
24 struct msr reg;
25 struct msr *msrs;
26 int err;
27};
28
29struct msr_regs_info {
30 u32 *regs;
31 int err;
32};
33
Andrew Morton1e160cc2008-01-30 13:31:17 +010034static inline unsigned long long native_read_tscp(unsigned int *aux)
Glauber de Oliveira Costa8f12dea2008-01-30 13:31:06 +010035{
36 unsigned long low, high;
Joe Perchesabb0ade2008-03-23 01:02:51 -070037 asm volatile(".byte 0x0f,0x01,0xf9"
38 : "=a" (low), "=d" (high), "=c" (*aux));
Max Asbock41aefdc2008-06-25 14:45:28 -070039 return low | ((u64)high << 32);
Glauber de Oliveira Costa8f12dea2008-01-30 13:31:06 +010040}
41
Glauber de Oliveira Costac210d242008-01-30 13:31:07 +010042/*
Jike Songd4f1b102008-10-17 13:25:07 +080043 * both i386 and x86_64 returns 64-bit value in edx:eax, but gcc's "A"
44 * constraint has different meanings. For i386, "A" means exactly
45 * edx:eax, while for x86_64 it doesn't mean rdx:rax or edx:eax. Instead,
46 * it means rax *or* rdx.
Glauber de Oliveira Costac210d242008-01-30 13:31:07 +010047 */
48#ifdef CONFIG_X86_64
49#define DECLARE_ARGS(val, low, high) unsigned low, high
Joe Perchesabb0ade2008-03-23 01:02:51 -070050#define EAX_EDX_VAL(val, low, high) ((low) | ((u64)(high) << 32))
Glauber de Oliveira Costac210d242008-01-30 13:31:07 +010051#define EAX_EDX_ARGS(val, low, high) "a" (low), "d" (high)
52#define EAX_EDX_RET(val, low, high) "=a" (low), "=d" (high)
53#else
54#define DECLARE_ARGS(val, low, high) unsigned long long val
55#define EAX_EDX_VAL(val, low, high) (val)
56#define EAX_EDX_ARGS(val, low, high) "A" (val)
57#define EAX_EDX_RET(val, low, high) "=A" (val)
Glauber de Oliveira Costa8f12dea2008-01-30 13:31:06 +010058#endif
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020059
60static inline unsigned long long native_read_msr(unsigned int msr)
61{
Glauber de Oliveira Costac210d242008-01-30 13:31:07 +010062 DECLARE_ARGS(val, low, high);
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020063
Glauber de Oliveira Costac210d242008-01-30 13:31:07 +010064 asm volatile("rdmsr" : EAX_EDX_RET(val, low, high) : "c" (msr));
65 return EAX_EDX_VAL(val, low, high);
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020066}
67
68static inline unsigned long long native_read_msr_safe(unsigned int msr,
69 int *err)
70{
Glauber de Oliveira Costac210d242008-01-30 13:31:07 +010071 DECLARE_ARGS(val, low, high);
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020072
H. Peter Anvin08970fc2008-08-25 22:39:15 -070073 asm volatile("2: rdmsr ; xor %[err],%[err]\n"
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020074 "1:\n\t"
75 ".section .fixup,\"ax\"\n\t"
H. Peter Anvin08970fc2008-08-25 22:39:15 -070076 "3: mov %[fault],%[err] ; jmp 1b\n\t"
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020077 ".previous\n\t"
Joe Perchesabb0ade2008-03-23 01:02:51 -070078 _ASM_EXTABLE(2b, 3b)
H. Peter Anvin08970fc2008-08-25 22:39:15 -070079 : [err] "=r" (*err), EAX_EDX_RET(val, low, high)
H. Peter Anvin0cc02132009-08-31 14:23:29 -070080 : "c" (msr), [fault] "i" (-EIO));
Glauber de Oliveira Costac210d242008-01-30 13:31:07 +010081 return EAX_EDX_VAL(val, low, high);
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020082}
83
Glauber de Oliveira Costac9dcda52008-01-30 13:31:07 +010084static inline void native_write_msr(unsigned int msr,
85 unsigned low, unsigned high)
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020086{
Jeremy Fitzhardingeaf2b1c62008-06-25 00:18:59 -040087 asm volatile("wrmsr" : : "c" (msr), "a"(low), "d" (high) : "memory");
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020088}
89
Frederic Weisbecker0ca59dd2008-12-24 23:30:02 +010090/* Can be uninlined because referenced by paravirt */
91notrace static inline int native_write_msr_safe(unsigned int msr,
Glauber de Oliveira Costac9dcda52008-01-30 13:31:07 +010092 unsigned low, unsigned high)
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020093{
94 int err;
H. Peter Anvin08970fc2008-08-25 22:39:15 -070095 asm volatile("2: wrmsr ; xor %[err],%[err]\n"
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020096 "1:\n\t"
97 ".section .fixup,\"ax\"\n\t"
H. Peter Anvin08970fc2008-08-25 22:39:15 -070098 "3: mov %[fault],%[err] ; jmp 1b\n\t"
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +020099 ".previous\n\t"
Joe Perchesabb0ade2008-03-23 01:02:51 -0700100 _ASM_EXTABLE(2b, 3b)
H. Peter Anvin08970fc2008-08-25 22:39:15 -0700101 : [err] "=a" (err)
Glauber de Oliveira Costac9dcda52008-01-30 13:31:07 +0100102 : "c" (msr), "0" (low), "d" (high),
H. Peter Anvin0cc02132009-08-31 14:23:29 -0700103 [fault] "i" (-EIO)
Jeremy Fitzhardingeaf2b1c62008-06-25 00:18:59 -0400104 : "memory");
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200105 return err;
106}
107
Ingo Molnarcdc79572008-01-30 13:32:39 +0100108extern unsigned long long native_read_tsc(void);
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200109
Andre Przywara1f975f72012-06-01 16:52:35 +0200110extern int rdmsr_safe_regs(u32 regs[8]);
111extern int wrmsr_safe_regs(u32 regs[8]);
Borislav Petkov132ec922009-08-31 09:50:09 +0200112
Ingo Molnar92767af2008-01-30 13:32:40 +0100113static __always_inline unsigned long long __native_read_tsc(void)
114{
115 DECLARE_ARGS(val, low, high);
116
Ingo Molnar92767af2008-01-30 13:32:40 +0100117 asm volatile("rdtsc" : EAX_EDX_RET(val, low, high));
Ingo Molnar92767af2008-01-30 13:32:40 +0100118
119 return EAX_EDX_VAL(val, low, high);
120}
121
Glauber de Oliveira Costab8d1fae2008-01-30 13:31:07 +0100122static inline unsigned long long native_read_pmc(int counter)
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200123{
Glauber de Oliveira Costac210d242008-01-30 13:31:07 +0100124 DECLARE_ARGS(val, low, high);
125
126 asm volatile("rdpmc" : EAX_EDX_RET(val, low, high) : "c" (counter));
127 return EAX_EDX_VAL(val, low, high);
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200128}
129
130#ifdef CONFIG_PARAVIRT
131#include <asm/paravirt.h>
Thomas Gleixner96a388d2007-10-11 11:20:03 +0200132#else
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200133#include <linux/errno.h>
134/*
135 * Access to machine-specific registers (available on 586 and better only)
136 * Note: the rd* operations modify the parameters directly (without using
137 * pointer indirection), this allows gcc to optimize better
138 */
139
Borislav Petkov1423bed2013-03-04 21:16:19 +0100140#define rdmsr(msr, low, high) \
Joe Perchesabb0ade2008-03-23 01:02:51 -0700141do { \
142 u64 __val = native_read_msr((msr)); \
Borislav Petkov1423bed2013-03-04 21:16:19 +0100143 (void)((low) = (u32)__val); \
144 (void)((high) = (u32)(__val >> 32)); \
Joe Perchesabb0ade2008-03-23 01:02:51 -0700145} while (0)
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200146
Glauber de Oliveira Costac9dcda52008-01-30 13:31:07 +0100147static inline void wrmsr(unsigned msr, unsigned low, unsigned high)
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200148{
Glauber de Oliveira Costac9dcda52008-01-30 13:31:07 +0100149 native_write_msr(msr, low, high);
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200150}
151
Joe Perchesabb0ade2008-03-23 01:02:51 -0700152#define rdmsrl(msr, val) \
153 ((val) = native_read_msr((msr)))
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200154
Glauber de Oliveira Costac210d242008-01-30 13:31:07 +0100155#define wrmsrl(msr, val) \
Joe Perchesabb0ade2008-03-23 01:02:51 -0700156 native_write_msr((msr), (u32)((u64)(val)), (u32)((u64)(val) >> 32))
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200157
158/* wrmsr with exception handling */
Glauber de Oliveira Costac9dcda52008-01-30 13:31:07 +0100159static inline int wrmsr_safe(unsigned msr, unsigned low, unsigned high)
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200160{
Glauber de Oliveira Costac9dcda52008-01-30 13:31:07 +0100161 return native_write_msr_safe(msr, low, high);
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200162}
163
H. Peter Anvin060feb62012-04-19 17:07:34 -0700164/* rdmsr with exception handling */
Borislav Petkov1423bed2013-03-04 21:16:19 +0100165#define rdmsr_safe(msr, low, high) \
Joe Perchesabb0ade2008-03-23 01:02:51 -0700166({ \
167 int __err; \
168 u64 __val = native_read_msr_safe((msr), &__err); \
Borislav Petkov1423bed2013-03-04 21:16:19 +0100169 (*low) = (u32)__val; \
170 (*high) = (u32)(__val >> 32); \
Joe Perchesabb0ade2008-03-23 01:02:51 -0700171 __err; \
172})
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200173
Andi Kleen1de87bd2008-03-22 10:59:28 +0100174static inline int rdmsrl_safe(unsigned msr, unsigned long long *p)
175{
176 int err;
177
178 *p = native_read_msr_safe(msr, &err);
179 return err;
180}
Borislav Petkov177fed12009-08-31 09:50:10 +0200181
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200182#define rdtscl(low) \
Ken Chen205516c2008-12-16 00:32:21 -0800183 ((low) = (u32)__native_read_tsc())
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200184
185#define rdtscll(val) \
Ken Chen205516c2008-12-16 00:32:21 -0800186 ((val) = __native_read_tsc())
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200187
Joe Perchesabb0ade2008-03-23 01:02:51 -0700188#define rdpmc(counter, low, high) \
189do { \
190 u64 _l = native_read_pmc((counter)); \
191 (low) = (u32)_l; \
192 (high) = (u32)(_l >> 32); \
193} while (0)
Glauber de Oliveira Costac210d242008-01-30 13:31:07 +0100194
Andi Kleen1ff4d582012-06-05 17:56:50 -0700195#define rdpmcl(counter, val) ((val) = native_read_pmc(counter))
196
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
H. Peter Anvin715c85b2012-06-07 13:32:04 -0700208#define wrmsrl_safe(msr, val) wrmsr_safe((msr), (u32)(val), \
Joe Perchesabb0ade2008-03-23 01:02:51 -0700209 (u32)((val) >> 32))
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200210
Borislav Petkov1423bed2013-03-04 21:16:19 +0100211#define write_tsc(low, high) wrmsr(MSR_IA32_TSC, (low), (high))
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200212
Sheng Yang5df97402009-12-16 13:48:04 +0800213#define write_rdtscp_aux(val) wrmsr(MSR_TSC_AUX, (val), 0)
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200214
Borislav Petkov50542252009-12-11 18:14:40 +0100215struct msr *msrs_alloc(void);
216void msrs_free(struct msr *msrs);
217
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200218#ifdef CONFIG_SMP
H. Peter Anvinc6f31932008-08-25 17:27:21 -0700219int rdmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h);
220int wrmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h);
Jacob Pan1a6b9912013-10-11 16:54:58 -0700221int rdmsrl_on_cpu(unsigned int cpu, u32 msr_no, u64 *q);
222int wrmsrl_on_cpu(unsigned int cpu, u32 msr_no, u64 q);
Borislav Petkovb8a47542009-07-30 11:10:02 +0200223void rdmsr_on_cpus(const struct cpumask *mask, u32 msr_no, struct msr *msrs);
224void wrmsr_on_cpus(const struct cpumask *mask, u32 msr_no, struct msr *msrs);
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200225int rdmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h);
226int wrmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h);
Jacob Pan1a6b9912013-10-11 16:54:58 -0700227int rdmsrl_safe_on_cpu(unsigned int cpu, u32 msr_no, u64 *q);
228int wrmsrl_safe_on_cpu(unsigned int cpu, u32 msr_no, u64 q);
H. Peter Anvin8b956bf2009-08-31 14:13:48 -0700229int rdmsr_safe_regs_on_cpu(unsigned int cpu, u32 regs[8]);
230int wrmsr_safe_regs_on_cpu(unsigned int cpu, u32 regs[8]);
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200231#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}
Jacob Pan1a6b9912013-10-11 16:54:58 -0700242static inline int rdmsrl_on_cpu(unsigned int cpu, u32 msr_no, u64 *q)
243{
244 rdmsrl(msr_no, *q);
245 return 0;
246}
247static inline int wrmsrl_on_cpu(unsigned int cpu, u32 msr_no, u64 q)
248{
249 wrmsrl(msr_no, q);
250 return 0;
251}
Rusty Russell0d0fbbd2009-11-05 22:45:41 +1030252static inline void rdmsr_on_cpus(const struct cpumask *m, u32 msr_no,
Borislav Petkovb034c192009-05-22 13:52:19 +0200253 struct msr *msrs)
254{
255 rdmsr_on_cpu(0, msr_no, &(msrs[0].l), &(msrs[0].h));
256}
Rusty Russell0d0fbbd2009-11-05 22:45:41 +1030257static inline void wrmsr_on_cpus(const struct cpumask *m, u32 msr_no,
Borislav Petkovb034c192009-05-22 13:52:19 +0200258 struct msr *msrs)
259{
260 wrmsr_on_cpu(0, msr_no, msrs[0].l, msrs[0].h);
261}
Joe Perchesabb0ade2008-03-23 01:02:51 -0700262static inline int rdmsr_safe_on_cpu(unsigned int cpu, u32 msr_no,
263 u32 *l, u32 *h)
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200264{
265 return rdmsr_safe(msr_no, l, h);
266}
267static inline int wrmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h)
268{
269 return wrmsr_safe(msr_no, l, h);
270}
Jacob Pan1a6b9912013-10-11 16:54:58 -0700271static inline int rdmsrl_safe_on_cpu(unsigned int cpu, u32 msr_no, u64 *q)
272{
273 return rdmsrl_safe(msr_no, q);
274}
275static inline int wrmsrl_safe_on_cpu(unsigned int cpu, u32 msr_no, u64 q)
276{
277 return wrmsrl_safe(msr_no, q);
278}
H. Peter Anvin8b956bf2009-08-31 14:13:48 -0700279static inline int rdmsr_safe_regs_on_cpu(unsigned int cpu, u32 regs[8])
280{
281 return rdmsr_safe_regs(regs);
282}
283static inline int wrmsr_safe_regs_on_cpu(unsigned int cpu, u32 regs[8])
284{
285 return wrmsr_safe_regs(regs);
286}
Thomas Gleixnerbe7baf82007-10-23 22:37:24 +0200287#endif /* CONFIG_SMP */
H. Peter Anvinff55df52009-08-31 14:16:57 -0700288#endif /* __ASSEMBLY__ */
H. Peter Anvin1965aae2008-10-22 22:26:29 -0700289#endif /* _ASM_X86_MSR_H */