1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * Copyright (c) 2014 The Chromium OS Authors.
4  *
5  * Part of this file is adapted from coreboot
6  * src/arch/x86/include/arch/cpu.h and
7  * src/arch/x86/lib/cpu.c
8  */
9 
10 #ifndef _ASM_CPU_H
11 #define _ASM_CPU_H
12 
13 enum {
14 	X86_VENDOR_INVALID = 0,
15 	X86_VENDOR_INTEL,
16 	X86_VENDOR_CYRIX,
17 	X86_VENDOR_AMD,
18 	X86_VENDOR_UMC,
19 	X86_VENDOR_NEXGEN,
20 	X86_VENDOR_CENTAUR,
21 	X86_VENDOR_RISE,
22 	X86_VENDOR_TRANSMETA,
23 	X86_VENDOR_NSC,
24 	X86_VENDOR_SIS,
25 	X86_VENDOR_ANY = 0xfe,
26 	X86_VENDOR_UNKNOWN = 0xff
27 };
28 
29 /* Global descriptor table (GDT) bits */
30 enum {
31 	GDT_4KB			= 1ULL << 55,
32 	GDT_32BIT		= 1ULL << 54,
33 	GDT_LONG		= 1ULL << 53,
34 	GDT_PRESENT		= 1ULL << 47,
35 	GDT_NOTSYS		= 1ULL << 44,
36 	GDT_CODE		= 1ULL << 43,
37 	GDT_LIMIT_LOW_SHIFT	= 0,
38 	GDT_LIMIT_LOW_MASK	= 0xffff,
39 	GDT_LIMIT_HIGH_SHIFT	= 48,
40 	GDT_LIMIT_HIGH_MASK	= 0xf,
41 	GDT_BASE_LOW_SHIFT	= 16,
42 	GDT_BASE_LOW_MASK	= 0xffff,
43 	GDT_BASE_HIGH_SHIFT	= 56,
44 	GDT_BASE_HIGH_MASK	= 0xf,
45 };
46 
47 /*
48  * System controllers in an x86 system. We mostly need to just find these and
49  * use them on PCI. At some point these might have their own uclass (e.g.
50  * UCLASS_VIDEO for the GMA device).
51  */
52 enum {
53 	X86_NONE,
54 	X86_SYSCON_ME,		/* Intel Management Engine */
55 	X86_SYSCON_PINCONF,	/* Intel x86 pin configuration */
56 	X86_SYSCON_PMU,		/* Power Management Unit */
57 	X86_SYSCON_SCU,		/* System Controller Unit */
58 	X86_SYSCON_PUNIT,	/* Power unit */
59 };
60 
61 #define CPUID_FEATURE_PAE	BIT(6)
62 #define CPUID_FEATURE_PSE36	BIT(17)
63 #define CPUID_FEAURE_HTT	BIT(28)
64 
65 struct cpuid_result {
66 	uint32_t eax;
67 	uint32_t ebx;
68 	uint32_t ecx;
69 	uint32_t edx;
70 };
71 
72 /*
73  * Generic CPUID function
74  */
cpuid(int op)75 static inline struct cpuid_result cpuid(int op)
76 {
77 	struct cpuid_result result;
78 	asm volatile(
79 		"mov %%ebx, %%edi;"
80 		"cpuid;"
81 		"mov %%ebx, %%esi;"
82 		"mov %%edi, %%ebx;"
83 		: "=a" (result.eax),
84 		  "=S" (result.ebx),
85 		  "=c" (result.ecx),
86 		  "=d" (result.edx)
87 		: "0" (op)
88 		: "edi");
89 	return result;
90 }
91 
92 /*
93  * Generic Extended CPUID function
94  */
cpuid_ext(int op,unsigned ecx)95 static inline struct cpuid_result cpuid_ext(int op, unsigned ecx)
96 {
97 	struct cpuid_result result;
98 	asm volatile(
99 		"mov %%ebx, %%edi;"
100 		"cpuid;"
101 		"mov %%ebx, %%esi;"
102 		"mov %%edi, %%ebx;"
103 		: "=a" (result.eax),
104 		  "=S" (result.ebx),
105 		  "=c" (result.ecx),
106 		  "=d" (result.edx)
107 		: "0" (op), "2" (ecx)
108 		: "edi");
109 	return result;
110 }
111 
native_cpuid(unsigned int * eax,unsigned int * ebx,unsigned int * ecx,unsigned int * edx)112 static inline void native_cpuid(unsigned int *eax, unsigned int *ebx,
113 				unsigned int *ecx, unsigned int *edx)
114 {
115 	/* ecx is often an input as well as an output. */
116 	asm volatile("cpuid"
117 	    : "=a" (*eax),
118 	      "=b" (*ebx),
119 	      "=c" (*ecx),
120 	      "=d" (*edx)
121 	    : "0" (*eax), "2" (*ecx)
122 	    : "memory");
123 }
124 
125 #define native_cpuid_reg(reg)					\
126 static inline unsigned int cpuid_##reg(unsigned int op)	\
127 {								\
128 	unsigned int eax = op, ebx, ecx = 0, edx;		\
129 								\
130 	native_cpuid(&eax, &ebx, &ecx, &edx);			\
131 								\
132 	return reg;						\
133 }
134 
135 /*
136  * Native CPUID functions returning a single datum.
137  */
138 native_cpuid_reg(eax)
native_cpuid_reg(ebx)139 native_cpuid_reg(ebx)
140 native_cpuid_reg(ecx)
141 native_cpuid_reg(edx)
142 
143 #if CONFIG_IS_ENABLED(X86_64)
144 static inline int flag_is_changeable_p(u32 flag)
145 {
146 	return 1;
147 }
148 #else
149 /* Standard macro to see if a specific flag is changeable */
150 static inline int flag_is_changeable_p(u32 flag)
151 {
152 	u32 f1, f2;
153 
154 	asm(
155 		"pushfl\n\t"
156 		"pushfl\n\t"
157 		"popl %0\n\t"
158 		"movl %0,%1\n\t"
159 		"xorl %2,%0\n\t"
160 		"pushl %0\n\t"
161 		"popfl\n\t"
162 		"pushfl\n\t"
163 		"popl %0\n\t"
164 		"popfl\n\t"
165 		: "=&r" (f1), "=&r" (f2)
166 		: "ir" (flag));
167 	return ((f1 ^ f2) & flag) != 0;
168 }
169 #endif /* X86_64 */
170 
171 /**
172  * cpu_enable_paging_pae() - Enable PAE-paging
173  *
174  * @cr3:	Value to set in cr3 (PDPT or PML4T)
175  */
176 void cpu_enable_paging_pae(ulong cr3);
177 
178 /**
179  * cpu_disable_paging_pae() - Disable paging and PAE
180  */
181 void cpu_disable_paging_pae(void);
182 
183 /**
184  * cpu_has_64bit() - Check if the CPU has 64-bit support
185  *
186  * Return: 1 if this CPU supports long mode (64-bit), 0 if not
187  */
188 int cpu_has_64bit(void);
189 
190 /**
191  * cpu_vendor_name() - Get CPU vendor name
192  *
193  * @vendor:	CPU vendor enumeration number
194  *
195  * @return:	Address to hold the CPU vendor name string
196  */
197 const char *cpu_vendor_name(int vendor);
198 
199 #define CPU_MAX_NAME_LEN	49
200 
201 /**
202  * cpu_get_name() - Get the name of the current cpu
203  *
204  * @name: Place to put name, which must be CPU_MAX_NAME_LEN bytes including
205  * Return: pointer to name, which will likely be a few bytes after the start
206  * of @name
207  * \0 terminator
208  */
209 char *cpu_get_name(char *name);
210 
211 /**
212  * cpu_call64() - Jump to a 64-bit Linux kernel (internal function)
213  *
214  * The kernel is uncompressed and the 64-bit entry point is expected to be
215  * at @target.
216  *
217  * This function is used internally - see cpu_jump_to_64bit() for a more
218  * useful function.
219  *
220  * @pgtable:	Address of 24KB area containing the page table
221  * @setup_base:	Pointer to the setup.bin information for the kernel
222  * @target:	Pointer to the start of the kernel image
223  */
224 void cpu_call64(ulong pgtable, ulong setup_base, ulong target);
225 
226 /**
227  * cpu_call32() - Jump to a 32-bit entry point
228  *
229  * @code_seg32:	32-bit code segment to use (GDT offset, e.g. 0x20)
230  * @target:	Pointer to the start of the 32-bit U-Boot image/entry point
231  * @table:	Pointer to start of info table to pass to U-Boot
232  */
233 void cpu_call32(ulong code_seg32, ulong target, ulong table);
234 
235 /**
236  * cpu_jump_to_64bit() - Jump to a 64-bit Linux kernel
237  *
238  * The kernel is uncompressed and the 64-bit entry point is expected to be
239  * at @target.
240  *
241  * @setup_base:	Pointer to the setup.bin information for the kernel
242  * @target:	Pointer to the start of the kernel image
243  * Return: -EFAULT if the kernel returned; otherwise does not return
244  */
245 int cpu_jump_to_64bit(ulong setup_base, ulong target);
246 
247 /**
248  * cpu_jump_to_64bit_uboot() - special function to jump from SPL to U-Boot
249  *
250  * This handles calling from 32-bit SPL to 64-bit U-Boot.
251  *
252  * @target:	Address of U-Boot in RAM
253  */
254 int cpu_jump_to_64bit_uboot(ulong target);
255 
256 /**
257  * cpu_get_family_model() - Get the family and model for the CPU
258  *
259  * Return: the CPU ID masked with 0x0fff0ff0
260  */
261 u32 cpu_get_family_model(void);
262 
263 /**
264  * cpu_get_stepping() - Get the stepping value for the CPU
265  *
266  * Return: the CPU ID masked with 0xf
267  */
268 u32 cpu_get_stepping(void);
269 
270 /**
271  * board_final_init() - Final initialization hook (optional)
272  *
273  * Implements a custom initialization for boards that need to do it
274  * before the system is ready.
275  */
276 void board_final_init(void);
277 
278 /**
279  * board_final_cleanup() - Final cleanup hook (optional)
280  *
281  * Implements a custom cleanup for boards that need to do it before
282  * booting the OS.
283  */
284 void board_final_cleanup(void);
285 
286 #ifndef CONFIG_EFI_STUB
287 int reserve_arch(void);
288 #endif
289 
290 #endif
291