blob: 39dc7b33aa0084692e52ac3527a81ac5c25c1130 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Graeme Russa0190bd2012-12-02 04:55:11 +00002/*
3 * Taken from the linux kernel file of the same name
4 *
5 * (C) Copyright 2012
6 * Graeme Russ, <graeme.russ@gmail.com>
Graeme Russa0190bd2012-12-02 04:55:11 +00007 */
8
9#ifndef _ASM_X86_MSR_H
10#define _ASM_X86_MSR_H
11
12#include <asm/msr-index.h>
13
14#ifndef __ASSEMBLY__
15
16#include <linux/types.h>
17#include <linux/ioctl.h>
18
19#define X86_IOC_RDMSR_REGS _IOWR('c', 0xA0, __u32[8])
20#define X86_IOC_WRMSR_REGS _IOWR('c', 0xA1, __u32[8])
21
22#ifdef __KERNEL__
23
Masahiro Yamada56a931c2016-09-21 11:28:55 +090024#include <linux/errno.h>
Graeme Russa0190bd2012-12-02 04:55:11 +000025
26struct msr {
27 union {
28 struct {
29 u32 l;
30 u32 h;
31 };
32 u64 q;
33 };
34};
35
36struct msr_info {
37 u32 msr_no;
38 struct msr reg;
39 struct msr *msrs;
40 int err;
41};
42
43struct msr_regs_info {
44 u32 *regs;
45 int err;
46};
47
48static inline unsigned long long native_read_tscp(unsigned int *aux)
49{
50 unsigned long low, high;
51 asm volatile(".byte 0x0f,0x01,0xf9"
52 : "=a" (low), "=d" (high), "=c" (*aux));
53 return low | ((u64)high << 32);
54}
55
56/*
57 * both i386 and x86_64 returns 64-bit value in edx:eax, but gcc's "A"
58 * constraint has different meanings. For i386, "A" means exactly
59 * edx:eax, while for x86_64 it doesn't mean rdx:rax or edx:eax. Instead,
60 * it means rax *or* rdx.
61 */
Simon Glass0b755a92025-03-15 14:25:45 +000062#if CONFIG_IS_ENABLED(X86_64)
63/* Using 64-bit values saves one instruction clearing the high half of low */
64#define DECLARE_ARGS(val, low, high) unsigned long low, high
65#define EAX_EDX_VAL(val, low, high) ((low) | (high) << 32)
Graeme Russa0190bd2012-12-02 04:55:11 +000066#define EAX_EDX_RET(val, low, high) "=a" (low), "=d" (high)
67#else
68#define DECLARE_ARGS(val, low, high) unsigned long long val
69#define EAX_EDX_VAL(val, low, high) (val)
Graeme Russa0190bd2012-12-02 04:55:11 +000070#define EAX_EDX_RET(val, low, high) "=A" (val)
71#endif
72
Simon Glass56da76d2022-12-21 16:08:15 -070073static inline notrace
Simon Glass42081ce2013-06-11 11:14:52 -070074 unsigned long long native_read_msr(unsigned int msr)
Graeme Russa0190bd2012-12-02 04:55:11 +000075{
76 DECLARE_ARGS(val, low, high);
77
78 asm volatile("rdmsr" : EAX_EDX_RET(val, low, high) : "c" (msr));
79 return EAX_EDX_VAL(val, low, high);
80}
81
82static inline void native_write_msr(unsigned int msr,
83 unsigned low, unsigned high)
84{
85 asm volatile("wrmsr" : : "c" (msr), "a"(low), "d" (high) : "memory");
86}
87
88extern unsigned long long native_read_tsc(void);
89
90extern int native_rdmsr_safe_regs(u32 regs[8]);
91extern int native_wrmsr_safe_regs(u32 regs[8]);
92
93static inline unsigned long long native_read_pmc(int counter)
94{
95 DECLARE_ARGS(val, low, high);
96
97 asm volatile("rdpmc" : EAX_EDX_RET(val, low, high) : "c" (counter));
98 return EAX_EDX_VAL(val, low, high);
99}
100
101#ifdef CONFIG_PARAVIRT
102#include <asm/paravirt.h>
103#else
104#include <errno.h>
105/*
106 * Access to machine-specific registers (available on 586 and better only)
107 * Note: the rd* operations modify the parameters directly (without using
108 * pointer indirection), this allows gcc to optimize better
109 */
110
111#define rdmsr(msr, val1, val2) \
112do { \
113 u64 __val = native_read_msr((msr)); \
114 (void)((val1) = (u32)__val); \
115 (void)((val2) = (u32)(__val >> 32)); \
116} while (0)
117
118static inline void wrmsr(unsigned msr, unsigned low, unsigned high)
119{
120 native_write_msr(msr, low, high);
121}
122
123#define rdmsrl(msr, val) \
124 ((val) = native_read_msr((msr)))
125
126#define wrmsrl(msr, val) \
127 native_write_msr((msr), (u32)((u64)(val)), (u32)((u64)(val) >> 32))
128
Simon Glass699a90d2015-04-29 22:26:00 -0600129static inline void msr_clrsetbits_64(unsigned msr, u64 clear, u64 set)
130{
131 u64 val;
132
133 val = native_read_msr(msr);
134 val &= ~clear;
135 val |= set;
136 wrmsrl(msr, val);
137}
138
139static inline void msr_setbits_64(unsigned msr, u64 set)
140{
141 u64 val;
142
143 val = native_read_msr(msr);
144 val |= set;
145 wrmsrl(msr, val);
146}
147
148static inline void msr_clrbits_64(unsigned msr, u64 clear)
149{
150 u64 val;
151
152 val = native_read_msr(msr);
153 val &= ~clear;
154 wrmsrl(msr, val);
155}
156
Graeme Russa0190bd2012-12-02 04:55:11 +0000157/* rdmsr with exception handling */
158#define rdmsr_safe(msr, p1, p2) \
159({ \
160 int __err; \
161 u64 __val = native_read_msr_safe((msr), &__err); \
162 (*p1) = (u32)__val; \
163 (*p2) = (u32)(__val >> 32); \
164 __err; \
165})
166
167static inline int rdmsrl_amd_safe(unsigned msr, unsigned long long *p)
168{
169 u32 gprs[8] = { 0 };
170 int err;
171
172 gprs[1] = msr;
173 gprs[7] = 0x9c5a203a;
174
175 err = native_rdmsr_safe_regs(gprs);
176
177 *p = gprs[0] | ((u64)gprs[2] << 32);
178
179 return err;
180}
181
182static inline int wrmsrl_amd_safe(unsigned msr, unsigned long long val)
183{
184 u32 gprs[8] = { 0 };
185
186 gprs[0] = (u32)val;
187 gprs[1] = msr;
188 gprs[2] = val >> 32;
189 gprs[7] = 0x9c5a203a;
190
191 return native_wrmsr_safe_regs(gprs);
192}
193
194static inline int rdmsr_safe_regs(u32 regs[8])
195{
196 return native_rdmsr_safe_regs(regs);
197}
198
199static inline int wrmsr_safe_regs(u32 regs[8])
200{
201 return native_wrmsr_safe_regs(regs);
202}
203
Simon Glassf41118e2014-11-12 22:42:18 -0700204typedef struct msr_t {
205 uint32_t lo;
206 uint32_t hi;
207} msr_t;
208
209static inline struct msr_t msr_read(unsigned msr_num)
210{
211 struct msr_t msr;
212
213 rdmsr(msr_num, msr.lo, msr.hi);
214
215 return msr;
216}
217
218static inline void msr_write(unsigned msr_num, msr_t msr)
219{
220 wrmsr(msr_num, msr.lo, msr.hi);
221}
222
Graeme Russa0190bd2012-12-02 04:55:11 +0000223#define rdtscl(low) \
224 ((low) = (u32)__native_read_tsc())
225
226#define rdtscll(val) \
227 ((val) = __native_read_tsc())
228
229#define rdpmc(counter, low, high) \
230do { \
231 u64 _l = native_read_pmc((counter)); \
232 (low) = (u32)_l; \
233 (high) = (u32)(_l >> 32); \
234} while (0)
235
236#define rdtscp(low, high, aux) \
237do { \
238 unsigned long long _val = native_read_tscp(&(aux)); \
239 (low) = (u32)_val; \
240 (high) = (u32)(_val >> 32); \
241} while (0)
242
243#define rdtscpll(val, aux) (val) = native_read_tscp(&(aux))
244
245#endif /* !CONFIG_PARAVIRT */
246
Graeme Russa0190bd2012-12-02 04:55:11 +0000247#define checking_wrmsrl(msr, val) wrmsr_safe((msr), (u32)(val), \
248 (u32)((val) >> 32))
249
250#define write_tsc(val1, val2) wrmsr(MSR_IA32_TSC, (val1), (val2))
251
252#define write_rdtscp_aux(val) wrmsr(MSR_TSC_AUX, (val), 0)
253
254struct msr *msrs_alloc(void);
255void msrs_free(struct msr *msrs);
256
Graeme Russa0190bd2012-12-02 04:55:11 +0000257#endif /* __KERNEL__ */
258#endif /* __ASSEMBLY__ */
259#endif /* _ASM_X86_MSR_H */