blob: c651f2f594535ab47c315d9b9fa4176cfe844748 [file] [log] [blame]
Simon Glassd8d9fec2014-10-10 08:21:52 -06001/*
2 * Copyright (c) 2014 The Chromium OS Authors.
3 *
Bin Meng035c1d22014-11-09 22:18:56 +08004 * Part of this file is adapted from coreboot
5 * src/arch/x86/include/arch/cpu.h and
6 * src/arch/x86/lib/cpu.c
7 *
Simon Glassd8d9fec2014-10-10 08:21:52 -06008 * SPDX-License-Identifier: GPL-2.0+
9 */
Bin Meng035c1d22014-11-09 22:18:56 +080010
11#ifndef _ASM_CPU_H
12#define _ASM_CPU_H
13
14enum {
15 X86_VENDOR_INVALID = 0,
16 X86_VENDOR_INTEL,
17 X86_VENDOR_CYRIX,
18 X86_VENDOR_AMD,
19 X86_VENDOR_UMC,
20 X86_VENDOR_NEXGEN,
21 X86_VENDOR_CENTAUR,
22 X86_VENDOR_RISE,
23 X86_VENDOR_TRANSMETA,
24 X86_VENDOR_NSC,
25 X86_VENDOR_SIS,
26 X86_VENDOR_ANY = 0xfe,
27 X86_VENDOR_UNKNOWN = 0xff
28};
29
Simon Glassd360b2f2015-08-04 12:33:54 -060030/* Global descriptor table (GDT) bits */
31enum {
32 GDT_4KB = 1ULL << 55,
33 GDT_32BIT = 1ULL << 54,
34 GDT_LONG = 1ULL << 53,
35 GDT_PRESENT = 1ULL << 47,
36 GDT_NOTSYS = 1ULL << 44,
37 GDT_CODE = 1ULL << 43,
38 GDT_LIMIT_LOW_SHIFT = 0,
39 GDT_LIMIT_LOW_MASK = 0xffff,
40 GDT_LIMIT_HIGH_SHIFT = 48,
41 GDT_LIMIT_HIGH_MASK = 0xf,
42 GDT_BASE_LOW_SHIFT = 16,
43 GDT_BASE_LOW_MASK = 0xffff,
44 GDT_BASE_HIGH_SHIFT = 56,
45 GDT_BASE_HIGH_MASK = 0xf,
46};
47
Simon Glass43a50342016-01-17 16:11:58 -070048/*
49 * System controllers in an x86 system. We mostly need to just find these and
Simon Glassa75abeb2016-01-17 16:11:59 -070050 * use them on PCI. At some point these might have their own uclass (e.g.
51 * UCLASS_VIDEO for the GMA device).
Simon Glass43a50342016-01-17 16:11:58 -070052 */
53enum {
54 X86_NONE,
55 X86_SYSCON_ME, /* Intel Management Engine */
Simon Glass11cd6312016-03-11 22:07:13 -070056 X86_SYSCON_PINCONF, /* Intel x86 pin configuration */
Simon Glass43a50342016-01-17 16:11:58 -070057};
58
Bin Meng035c1d22014-11-09 22:18:56 +080059struct cpuid_result {
60 uint32_t eax;
61 uint32_t ebx;
62 uint32_t ecx;
63 uint32_t edx;
64};
65
66/*
67 * Generic CPUID function
68 */
69static inline struct cpuid_result cpuid(int op)
70{
71 struct cpuid_result result;
72 asm volatile(
73 "mov %%ebx, %%edi;"
74 "cpuid;"
75 "mov %%ebx, %%esi;"
76 "mov %%edi, %%ebx;"
77 : "=a" (result.eax),
78 "=S" (result.ebx),
79 "=c" (result.ecx),
80 "=d" (result.edx)
81 : "0" (op)
82 : "edi");
83 return result;
84}
85
86/*
87 * Generic Extended CPUID function
88 */
89static inline struct cpuid_result cpuid_ext(int op, unsigned ecx)
90{
91 struct cpuid_result result;
92 asm volatile(
93 "mov %%ebx, %%edi;"
94 "cpuid;"
95 "mov %%ebx, %%esi;"
96 "mov %%edi, %%ebx;"
97 : "=a" (result.eax),
98 "=S" (result.ebx),
99 "=c" (result.ecx),
100 "=d" (result.edx)
101 : "0" (op), "2" (ecx)
102 : "edi");
103 return result;
104}
105
106/*
107 * CPUID functions returning a single datum
108 */
109static inline unsigned int cpuid_eax(unsigned int op)
110{
111 unsigned int eax;
112
113 __asm__("mov %%ebx, %%edi;"
114 "cpuid;"
115 "mov %%edi, %%ebx;"
116 : "=a" (eax)
117 : "0" (op)
118 : "ecx", "edx", "edi");
119 return eax;
120}
121
122static inline unsigned int cpuid_ebx(unsigned int op)
123{
124 unsigned int eax, ebx;
125
126 __asm__("mov %%ebx, %%edi;"
127 "cpuid;"
128 "mov %%ebx, %%esi;"
129 "mov %%edi, %%ebx;"
130 : "=a" (eax), "=S" (ebx)
131 : "0" (op)
132 : "ecx", "edx", "edi");
133 return ebx;
134}
135
136static inline unsigned int cpuid_ecx(unsigned int op)
137{
138 unsigned int eax, ecx;
Simon Glassd8d9fec2014-10-10 08:21:52 -0600139
Bin Meng035c1d22014-11-09 22:18:56 +0800140 __asm__("mov %%ebx, %%edi;"
141 "cpuid;"
142 "mov %%edi, %%ebx;"
143 : "=a" (eax), "=c" (ecx)
144 : "0" (op)
145 : "edx", "edi");
146 return ecx;
147}
Simon Glassd8d9fec2014-10-10 08:21:52 -0600148
Bin Meng035c1d22014-11-09 22:18:56 +0800149static inline unsigned int cpuid_edx(unsigned int op)
150{
151 unsigned int eax, edx;
152
153 __asm__("mov %%ebx, %%edi;"
154 "cpuid;"
155 "mov %%edi, %%ebx;"
156 : "=a" (eax), "=d" (edx)
157 : "0" (op)
158 : "ecx", "edi");
159 return edx;
160}
161
Simon Glass249da612017-01-16 07:04:05 -0700162#if !CONFIG_IS_ENABLED(X86_64)
163
Bin Meng035c1d22014-11-09 22:18:56 +0800164/* Standard macro to see if a specific flag is changeable */
165static inline int flag_is_changeable_p(uint32_t flag)
166{
167 uint32_t f1, f2;
168
169 asm(
170 "pushfl\n\t"
171 "pushfl\n\t"
172 "popl %0\n\t"
173 "movl %0,%1\n\t"
174 "xorl %2,%0\n\t"
175 "pushl %0\n\t"
176 "popfl\n\t"
177 "pushfl\n\t"
178 "popl %0\n\t"
179 "popfl\n\t"
180 : "=&r" (f1), "=&r" (f2)
181 : "ir" (flag));
182 return ((f1^f2) & flag) != 0;
183}
Simon Glass249da612017-01-16 07:04:05 -0700184#endif
Bin Meng035c1d22014-11-09 22:18:56 +0800185
Simon Glassdb139ab2015-04-28 20:25:14 -0600186static inline void mfence(void)
187{
188 __asm__ __volatile__("mfence" : : : "memory");
189}
190
Bin Meng035c1d22014-11-09 22:18:56 +0800191/**
Simon Glassd8d9fec2014-10-10 08:21:52 -0600192 * cpu_enable_paging_pae() - Enable PAE-paging
193 *
Bin Meng035c1d22014-11-09 22:18:56 +0800194 * @cr3: Value to set in cr3 (PDPT or PML4T)
Simon Glassd8d9fec2014-10-10 08:21:52 -0600195 */
196void cpu_enable_paging_pae(ulong cr3);
197
198/**
199 * cpu_disable_paging_pae() - Disable paging and PAE
200 */
201void cpu_disable_paging_pae(void);
202
Simon Glass2f2efbc2014-10-10 08:21:54 -0600203/**
204 * cpu_has_64bit() - Check if the CPU has 64-bit support
205 *
206 * @return 1 if this CPU supports long mode (64-bit), 0 if not
207 */
208int cpu_has_64bit(void);
209
Simon Glass463fac22014-10-10 08:21:55 -0600210/**
Bin Meng035c1d22014-11-09 22:18:56 +0800211 * cpu_vendor_name() - Get CPU vendor name
212 *
213 * @vendor: CPU vendor enumeration number
214 *
215 * @return: Address to hold the CPU vendor name string
216 */
217const char *cpu_vendor_name(int vendor);
218
Simon Glass543bb142014-11-10 18:00:26 -0700219#define CPU_MAX_NAME_LEN 49
220
Bin Meng035c1d22014-11-09 22:18:56 +0800221/**
Simon Glass543bb142014-11-10 18:00:26 -0700222 * cpu_get_name() - Get the name of the current cpu
Bin Meng035c1d22014-11-09 22:18:56 +0800223 *
Simon Glass543bb142014-11-10 18:00:26 -0700224 * @name: Place to put name, which must be CPU_MAX_NAME_LEN bytes including
225 * @return pointer to name, which will likely be a few bytes after the start
226 * of @name
227 * \0 terminator
Bin Meng035c1d22014-11-09 22:18:56 +0800228 */
Simon Glass543bb142014-11-10 18:00:26 -0700229char *cpu_get_name(char *name);
Bin Meng035c1d22014-11-09 22:18:56 +0800230
231/**
Simon Glass463fac22014-10-10 08:21:55 -0600232 * cpu_call64() - Jump to a 64-bit Linux kernel (internal function)
233 *
234 * The kernel is uncompressed and the 64-bit entry point is expected to be
235 * at @target.
236 *
237 * This function is used internally - see cpu_jump_to_64bit() for a more
238 * useful function.
239 *
240 * @pgtable: Address of 24KB area containing the page table
241 * @setup_base: Pointer to the setup.bin information for the kernel
242 * @target: Pointer to the start of the kernel image
243 */
244void cpu_call64(ulong pgtable, ulong setup_base, ulong target);
245
246/**
Simon Glassbae81c72015-08-04 12:33:55 -0600247 * cpu_call32() - Jump to a 32-bit entry point
248 *
249 * @code_seg32: 32-bit code segment to use (GDT offset, e.g. 0x20)
250 * @target: Pointer to the start of the 32-bit U-Boot image/entry point
251 * @table: Pointer to start of info table to pass to U-Boot
252 */
253void cpu_call32(ulong code_seg32, ulong target, ulong table);
254
255/**
Simon Glass463fac22014-10-10 08:21:55 -0600256 * cpu_jump_to_64bit() - Jump to a 64-bit Linux kernel
257 *
258 * The kernel is uncompressed and the 64-bit entry point is expected to be
259 * at @target.
260 *
261 * @setup_base: Pointer to the setup.bin information for the kernel
262 * @target: Pointer to the start of the kernel image
263 */
264int cpu_jump_to_64bit(ulong setup_base, ulong target);
265
Simon Glass2f462fd2016-03-11 22:06:52 -0700266/**
Simon Glass1e32ede2017-01-16 07:04:15 -0700267 * cpu_jump_to_64bit_uboot() - special function to jump from SPL to U-Boot
268 *
269 * This handles calling from 32-bit SPL to 64-bit U-Boot.
270 *
271 * @target: Address of U-Boot in RAM
272 */
273int cpu_jump_to_64bit_uboot(ulong target);
274
275/**
Simon Glass2f462fd2016-03-11 22:06:52 -0700276 * cpu_get_family_model() - Get the family and model for the CPU
277 *
278 * @return the CPU ID masked with 0x0fff0ff0
279 */
280u32 cpu_get_family_model(void);
281
282/**
283 * cpu_get_stepping() - Get the stepping value for the CPU
284 *
285 * @return the CPU ID masked with 0xf
286 */
287u32 cpu_get_stepping(void);
288
Simon Glassecae7fd2016-03-11 22:07:16 -0700289/**
290 * cpu_run_reference_code() - Run the platform reference code
291 *
292 * Some platforms require a binary blob to be executed once SDRAM is
293 * available. This is used to set up various platform features, such as the
294 * platform controller hub (PCH). This function should be implemented by the
295 * CPU-specific code.
296 *
297 * @return 0 on success, -ve on failure
298 */
299int cpu_run_reference_code(void);
300
Simon Glassd8d9fec2014-10-10 08:21:52 -0600301#endif