blob: 85386da47a047db176860653a5e568483f6467b3 [file] [log] [blame]
Simon Glass7bddac92014-10-10 08:21:52 -06001/*
2 * Copyright (c) 2014 The Chromium OS Authors.
3 *
Bin Meng52f952b2014-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 Glass7bddac92014-10-10 08:21:52 -06008 * SPDX-License-Identifier: GPL-2.0+
9 */
10
Bin Meng52f952b2014-11-09 22:18:56 +080011#ifndef _ASM_CPU_H
12#define _ASM_CPU_H
Simon Glass7bddac92014-10-10 08:21:52 -060013
Bin Meng52f952b2014-11-09 22:18:56 +080014enum {
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 Glass7dfe8bd2015-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 Glass98655f32016-01-17 16:11:58 -070048/*
49 * System controllers in an x86 system. We mostly need to just find these and
Simon Glass25d53522016-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 Glass98655f32016-01-17 16:11:58 -070052 */
53enum {
54 X86_NONE,
55 X86_SYSCON_ME, /* Intel Management Engine */
Simon Glass25d53522016-01-17 16:11:59 -070056 X86_SYSCON_GMA, /* Intel Graphics Media Accelerator */
Simon Glass7ac99be2016-03-11 22:07:13 -070057 X86_SYSCON_PINCONF, /* Intel x86 pin configuration */
Simon Glass98655f32016-01-17 16:11:58 -070058};
59
Bin Meng52f952b2014-11-09 22:18:56 +080060struct cpuid_result {
61 uint32_t eax;
62 uint32_t ebx;
63 uint32_t ecx;
64 uint32_t edx;
65};
66
67/*
68 * Generic CPUID function
69 */
70static inline struct cpuid_result cpuid(int op)
71{
72 struct cpuid_result result;
73 asm volatile(
74 "mov %%ebx, %%edi;"
75 "cpuid;"
76 "mov %%ebx, %%esi;"
77 "mov %%edi, %%ebx;"
78 : "=a" (result.eax),
79 "=S" (result.ebx),
80 "=c" (result.ecx),
81 "=d" (result.edx)
82 : "0" (op)
83 : "edi");
84 return result;
85}
86
87/*
88 * Generic Extended CPUID function
89 */
90static inline struct cpuid_result cpuid_ext(int op, unsigned ecx)
91{
92 struct cpuid_result result;
93 asm volatile(
94 "mov %%ebx, %%edi;"
95 "cpuid;"
96 "mov %%ebx, %%esi;"
97 "mov %%edi, %%ebx;"
98 : "=a" (result.eax),
99 "=S" (result.ebx),
100 "=c" (result.ecx),
101 "=d" (result.edx)
102 : "0" (op), "2" (ecx)
103 : "edi");
104 return result;
105}
106
107/*
108 * CPUID functions returning a single datum
109 */
110static inline unsigned int cpuid_eax(unsigned int op)
111{
112 unsigned int eax;
113
114 __asm__("mov %%ebx, %%edi;"
115 "cpuid;"
116 "mov %%edi, %%ebx;"
117 : "=a" (eax)
118 : "0" (op)
119 : "ecx", "edx", "edi");
120 return eax;
121}
122
123static inline unsigned int cpuid_ebx(unsigned int op)
124{
125 unsigned int eax, ebx;
126
127 __asm__("mov %%ebx, %%edi;"
128 "cpuid;"
129 "mov %%ebx, %%esi;"
130 "mov %%edi, %%ebx;"
131 : "=a" (eax), "=S" (ebx)
132 : "0" (op)
133 : "ecx", "edx", "edi");
134 return ebx;
135}
136
137static inline unsigned int cpuid_ecx(unsigned int op)
138{
139 unsigned int eax, ecx;
140
141 __asm__("mov %%ebx, %%edi;"
142 "cpuid;"
143 "mov %%edi, %%ebx;"
144 : "=a" (eax), "=c" (ecx)
145 : "0" (op)
146 : "edx", "edi");
147 return ecx;
148}
149
150static inline unsigned int cpuid_edx(unsigned int op)
151{
152 unsigned int eax, edx;
153
154 __asm__("mov %%ebx, %%edi;"
155 "cpuid;"
156 "mov %%edi, %%ebx;"
157 : "=a" (eax), "=d" (edx)
158 : "0" (op)
159 : "ecx", "edi");
160 return edx;
161}
162
163/* Standard macro to see if a specific flag is changeable */
164static inline int flag_is_changeable_p(uint32_t flag)
165{
166 uint32_t f1, f2;
167
168 asm(
169 "pushfl\n\t"
170 "pushfl\n\t"
171 "popl %0\n\t"
172 "movl %0,%1\n\t"
173 "xorl %2,%0\n\t"
174 "pushl %0\n\t"
175 "popfl\n\t"
176 "pushfl\n\t"
177 "popl %0\n\t"
178 "popfl\n\t"
179 : "=&r" (f1), "=&r" (f2)
180 : "ir" (flag));
181 return ((f1^f2) & flag) != 0;
182}
183
Simon Glass837a1362015-04-28 20:25:14 -0600184static inline void mfence(void)
185{
186 __asm__ __volatile__("mfence" : : : "memory");
187}
188
Bin Meng52f952b2014-11-09 22:18:56 +0800189/**
Simon Glass7bddac92014-10-10 08:21:52 -0600190 * cpu_enable_paging_pae() - Enable PAE-paging
191 *
Bin Meng52f952b2014-11-09 22:18:56 +0800192 * @cr3: Value to set in cr3 (PDPT or PML4T)
Simon Glass7bddac92014-10-10 08:21:52 -0600193 */
194void cpu_enable_paging_pae(ulong cr3);
195
196/**
197 * cpu_disable_paging_pae() - Disable paging and PAE
198 */
199void cpu_disable_paging_pae(void);
200
Simon Glass92cc94a2014-10-10 08:21:54 -0600201/**
202 * cpu_has_64bit() - Check if the CPU has 64-bit support
203 *
204 * @return 1 if this CPU supports long mode (64-bit), 0 if not
205 */
206int cpu_has_64bit(void);
207
Simon Glass200182a2014-10-10 08:21:55 -0600208/**
Bin Meng52f952b2014-11-09 22:18:56 +0800209 * cpu_vendor_name() - Get CPU vendor name
210 *
211 * @vendor: CPU vendor enumeration number
212 *
213 * @return: Address to hold the CPU vendor name string
214 */
215const char *cpu_vendor_name(int vendor);
216
Simon Glass727c1a92014-11-10 18:00:26 -0700217#define CPU_MAX_NAME_LEN 49
218
Bin Meng52f952b2014-11-09 22:18:56 +0800219/**
Simon Glass727c1a92014-11-10 18:00:26 -0700220 * cpu_get_name() - Get the name of the current cpu
Bin Meng52f952b2014-11-09 22:18:56 +0800221 *
Simon Glass727c1a92014-11-10 18:00:26 -0700222 * @name: Place to put name, which must be CPU_MAX_NAME_LEN bytes including
223 * @return pointer to name, which will likely be a few bytes after the start
224 * of @name
225 * \0 terminator
Bin Meng52f952b2014-11-09 22:18:56 +0800226 */
Simon Glass727c1a92014-11-10 18:00:26 -0700227char *cpu_get_name(char *name);
Bin Meng52f952b2014-11-09 22:18:56 +0800228
229/**
Simon Glass200182a2014-10-10 08:21:55 -0600230 * cpu_call64() - Jump to a 64-bit Linux kernel (internal function)
231 *
232 * The kernel is uncompressed and the 64-bit entry point is expected to be
233 * at @target.
234 *
235 * This function is used internally - see cpu_jump_to_64bit() for a more
236 * useful function.
237 *
238 * @pgtable: Address of 24KB area containing the page table
239 * @setup_base: Pointer to the setup.bin information for the kernel
240 * @target: Pointer to the start of the kernel image
241 */
242void cpu_call64(ulong pgtable, ulong setup_base, ulong target);
243
244/**
Simon Glass6f92ed82015-08-04 12:33:55 -0600245 * cpu_call32() - Jump to a 32-bit entry point
246 *
247 * @code_seg32: 32-bit code segment to use (GDT offset, e.g. 0x20)
248 * @target: Pointer to the start of the 32-bit U-Boot image/entry point
249 * @table: Pointer to start of info table to pass to U-Boot
250 */
251void cpu_call32(ulong code_seg32, ulong target, ulong table);
252
253/**
Simon Glass200182a2014-10-10 08:21:55 -0600254 * cpu_jump_to_64bit() - Jump to a 64-bit Linux kernel
255 *
256 * The kernel is uncompressed and the 64-bit entry point is expected to be
257 * at @target.
258 *
259 * @setup_base: Pointer to the setup.bin information for the kernel
260 * @target: Pointer to the start of the kernel image
261 */
262int cpu_jump_to_64bit(ulong setup_base, ulong target);
263
Simon Glass342727a2016-03-11 22:06:52 -0700264/**
265 * cpu_get_family_model() - Get the family and model for the CPU
266 *
267 * @return the CPU ID masked with 0x0fff0ff0
268 */
269u32 cpu_get_family_model(void);
270
271/**
272 * cpu_get_stepping() - Get the stepping value for the CPU
273 *
274 * @return the CPU ID masked with 0xf
275 */
276u32 cpu_get_stepping(void);
277
Simon Glass7bddac92014-10-10 08:21:52 -0600278#endif