blob: 845e00ca4397824fa69d68fe5386f4be5def12f0 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass16a624b2017-01-16 07:03:57 -07002/*
3 * (C) Copyright 2008-2011
4 * Graeme Russ, <graeme.russ@gmail.com>
5 *
6 * (C) Copyright 2002
7 * Daniel Engström, Omicron Ceti AB, <daniel@omicron.se>
8 *
9 * (C) Copyright 2002
10 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
11 * Marius Groeger <mgroeger@sysgo.de>
12 *
13 * (C) Copyright 2002
14 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
15 * Alex Zuepke <azu@sysgo.de>
16 *
17 * Part of this file is adapted from coreboot
18 * src/arch/x86/lib/cpu.c
Simon Glass16a624b2017-01-16 07:03:57 -070019 */
20
Simon Glass1d91ba72019-11-14 12:57:37 -070021#include <cpu_func.h>
Simon Glassda25eff2019-12-28 10:44:56 -070022#include <init.h>
Simon Glasse40633d2020-07-17 08:48:08 -060023#include <log.h>
Simon Glass16a624b2017-01-16 07:03:57 -070024#include <malloc.h>
Simon Glassdd45a7a2019-12-06 21:41:51 -070025#include <spl.h>
Simon Glass16a624b2017-01-16 07:03:57 -070026#include <asm/control_regs.h>
Simon Glass46f4c582020-04-30 21:21:39 -060027#include <asm/coreboot_tables.h>
Simon Glass16a624b2017-01-16 07:03:57 -070028#include <asm/cpu.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060029#include <asm/global_data.h>
Simon Glass16a624b2017-01-16 07:03:57 -070030#include <asm/mp.h>
31#include <asm/msr.h>
32#include <asm/mtrr.h>
33#include <asm/processor-flags.h>
Tom Rinidec7ea02024-05-20 13:35:03 -060034#include <asm/u-boot-x86.h>
Simon Glass16a624b2017-01-16 07:03:57 -070035
36DECLARE_GLOBAL_DATA_PTR;
37
Simon Glass2d8b3c62020-09-22 12:45:26 -060038#define CPUID_FEATURE_PAE BIT(6)
39#define CPUID_FEATURE_PSE36 BIT(17)
40#define CPUID_FEAURE_HTT BIT(28)
41
Simon Glass16a624b2017-01-16 07:03:57 -070042/*
43 * Constructor for a conventional segment GDT (or LDT) entry
44 * This is a macro so it can be used in initialisers
45 */
46#define GDT_ENTRY(flags, base, limit) \
47 ((((base) & 0xff000000ULL) << (56-24)) | \
48 (((flags) & 0x0000f0ffULL) << 40) | \
49 (((limit) & 0x000f0000ULL) << (48-16)) | \
50 (((base) & 0x00ffffffULL) << 16) | \
51 (((limit) & 0x0000ffffULL)))
52
53struct gdt_ptr {
54 u16 len;
55 u32 ptr;
56} __packed;
57
58struct cpu_device_id {
59 unsigned vendor;
60 unsigned device;
61};
62
63struct cpuinfo_x86 {
64 uint8_t x86; /* CPU family */
65 uint8_t x86_vendor; /* CPU vendor */
66 uint8_t x86_model;
67 uint8_t x86_mask;
68};
69
Simon Glassdd45a7a2019-12-06 21:41:51 -070070/* gcc 7.3 does not wwant to drop x86_vendors, so use #ifdef */
71#ifndef CONFIG_TPL_BUILD
Simon Glass16a624b2017-01-16 07:03:57 -070072/*
73 * List of cpu vendor strings along with their normalized
74 * id values.
75 */
76static const struct {
77 int vendor;
78 const char *name;
79} x86_vendors[] = {
80 { X86_VENDOR_INTEL, "GenuineIntel", },
81 { X86_VENDOR_CYRIX, "CyrixInstead", },
82 { X86_VENDOR_AMD, "AuthenticAMD", },
83 { X86_VENDOR_UMC, "UMC UMC UMC ", },
84 { X86_VENDOR_NEXGEN, "NexGenDriven", },
85 { X86_VENDOR_CENTAUR, "CentaurHauls", },
86 { X86_VENDOR_RISE, "RiseRiseRise", },
87 { X86_VENDOR_TRANSMETA, "GenuineTMx86", },
88 { X86_VENDOR_TRANSMETA, "TransmetaCPU", },
89 { X86_VENDOR_NSC, "Geode by NSC", },
90 { X86_VENDOR_SIS, "SiS SiS SiS ", },
91};
Simon Glassdd45a7a2019-12-06 21:41:51 -070092#endif
Simon Glass16a624b2017-01-16 07:03:57 -070093
94static void load_ds(u32 segment)
95{
96 asm volatile("movl %0, %%ds" : : "r" (segment * X86_GDT_ENTRY_SIZE));
97}
98
99static void load_es(u32 segment)
100{
101 asm volatile("movl %0, %%es" : : "r" (segment * X86_GDT_ENTRY_SIZE));
102}
103
104static void load_fs(u32 segment)
105{
106 asm volatile("movl %0, %%fs" : : "r" (segment * X86_GDT_ENTRY_SIZE));
107}
108
109static void load_gs(u32 segment)
110{
111 asm volatile("movl %0, %%gs" : : "r" (segment * X86_GDT_ENTRY_SIZE));
112}
113
114static void load_ss(u32 segment)
115{
116 asm volatile("movl %0, %%ss" : : "r" (segment * X86_GDT_ENTRY_SIZE));
117}
118
119static void load_gdt(const u64 *boot_gdt, u16 num_entries)
120{
121 struct gdt_ptr gdt;
122
123 gdt.len = (num_entries * X86_GDT_ENTRY_SIZE) - 1;
124 gdt.ptr = (ulong)boot_gdt;
125
126 asm volatile("lgdtl %0\n" : : "m" (gdt));
127}
128
129void arch_setup_gd(gd_t *new_gd)
130{
131 u64 *gdt_addr;
132
133 gdt_addr = new_gd->arch.gdt;
134
135 /*
136 * CS: code, read/execute, 4 GB, base 0
137 *
138 * Some OS (like VxWorks) requires GDT entry 1 to be the 32-bit CS
139 */
140 gdt_addr[X86_GDT_ENTRY_UNUSED] = GDT_ENTRY(0xc09b, 0, 0xfffff);
141 gdt_addr[X86_GDT_ENTRY_32BIT_CS] = GDT_ENTRY(0xc09b, 0, 0xfffff);
142
143 /* DS: data, read/write, 4 GB, base 0 */
144 gdt_addr[X86_GDT_ENTRY_32BIT_DS] = GDT_ENTRY(0xc093, 0, 0xfffff);
145
Masahiro Yamada4dcaa7d2020-01-08 20:13:42 +0900146 /*
147 * FS: data, read/write, sizeof (Global Data Pointer),
148 * base (Global Data Pointer)
149 */
Simon Glass16a624b2017-01-16 07:03:57 -0700150 new_gd->arch.gd_addr = new_gd;
Masahiro Yamada4dcaa7d2020-01-08 20:13:42 +0900151 gdt_addr[X86_GDT_ENTRY_32BIT_FS] = GDT_ENTRY(0x8093,
152 (ulong)&new_gd->arch.gd_addr,
153 sizeof(new_gd->arch.gd_addr) - 1);
Simon Glass16a624b2017-01-16 07:03:57 -0700154
155 /* 16-bit CS: code, read/execute, 64 kB, base 0 */
156 gdt_addr[X86_GDT_ENTRY_16BIT_CS] = GDT_ENTRY(0x009b, 0, 0x0ffff);
157
158 /* 16-bit DS: data, read/write, 64 kB, base 0 */
159 gdt_addr[X86_GDT_ENTRY_16BIT_DS] = GDT_ENTRY(0x0093, 0, 0x0ffff);
160
161 gdt_addr[X86_GDT_ENTRY_16BIT_FLAT_CS] = GDT_ENTRY(0x809b, 0, 0xfffff);
162 gdt_addr[X86_GDT_ENTRY_16BIT_FLAT_DS] = GDT_ENTRY(0x8093, 0, 0xfffff);
Simon Glasse0793102025-03-15 14:25:31 +0000163 gdt_addr[X86_GDT_ENTRY_64BIT_CS] = GDT_ENTRY(0xaf9b, 0, 0xfffff);
164 gdt_addr[X86_GDT_ENTRY_64BIT_TS1] = GDT_ENTRY(0x8980, 0, 0xfffff);
165 gdt_addr[X86_GDT_ENTRY_64BIT_TS2] = 0;
Simon Glass16a624b2017-01-16 07:03:57 -0700166
167 load_gdt(gdt_addr, X86_GDT_NUM_ENTRIES);
168 load_ds(X86_GDT_ENTRY_32BIT_DS);
169 load_es(X86_GDT_ENTRY_32BIT_DS);
170 load_gs(X86_GDT_ENTRY_32BIT_DS);
171 load_ss(X86_GDT_ENTRY_32BIT_DS);
172 load_fs(X86_GDT_ENTRY_32BIT_FS);
173}
174
175#ifdef CONFIG_HAVE_FSP
176/*
177 * Setup FSP execution environment GDT
178 *
179 * Per Intel FSP external architecture specification, before calling any FSP
180 * APIs, we need make sure the system is in flat 32-bit mode and both the code
181 * and data selectors should have full 4GB access range. Here we reuse the one
Heinrich Schuchardtdccdd932020-12-22 07:53:03 +0100182 * we used in arch/x86/cpu/start16.S, and reload the segment registers.
Simon Glass16a624b2017-01-16 07:03:57 -0700183 */
184void setup_fsp_gdt(void)
185{
186 load_gdt((const u64 *)(gdt_rom + CONFIG_RESET_SEG_START), 4);
187 load_ds(X86_GDT_ENTRY_32BIT_DS);
188 load_ss(X86_GDT_ENTRY_32BIT_DS);
189 load_es(X86_GDT_ENTRY_32BIT_DS);
190 load_fs(X86_GDT_ENTRY_32BIT_DS);
191 load_gs(X86_GDT_ENTRY_32BIT_DS);
192}
193#endif
194
195/*
196 * Cyrix CPUs without cpuid or with cpuid not yet enabled can be detected
197 * by the fact that they preserve the flags across the division of 5/2.
198 * PII and PPro exhibit this behavior too, but they have cpuid available.
199 */
200
201/*
202 * Perform the Cyrix 5/2 test. A Cyrix won't change
203 * the flags, while other 486 chips will.
204 */
205static inline int test_cyrix_52div(void)
206{
207 unsigned int test;
208
209 __asm__ __volatile__(
210 "sahf\n\t" /* clear flags (%eax = 0x0005) */
211 "div %b2\n\t" /* divide 5 by 2 */
212 "lahf" /* store flags into %ah */
213 : "=a" (test)
214 : "0" (5), "q" (2)
215 : "cc");
216
217 /* AH is 0x02 on Cyrix after the divide.. */
218 return (unsigned char) (test >> 8) == 0x02;
219}
220
Simon Glassdd45a7a2019-12-06 21:41:51 -0700221#ifndef CONFIG_TPL_BUILD
Simon Glass16a624b2017-01-16 07:03:57 -0700222/*
223 * Detect a NexGen CPU running without BIOS hypercode new enough
224 * to have CPUID. (Thanks to Herbert Oppmann)
225 */
226static int deep_magic_nexgen_probe(void)
227{
228 int ret;
229
230 __asm__ __volatile__ (
231 " movw $0x5555, %%ax\n"
232 " xorw %%dx,%%dx\n"
233 " movw $2, %%cx\n"
234 " divw %%cx\n"
235 " movl $0, %%eax\n"
236 " jnz 1f\n"
237 " movl $1, %%eax\n"
238 "1:\n"
239 : "=a" (ret) : : "cx", "dx");
240 return ret;
241}
Simon Glassdd45a7a2019-12-06 21:41:51 -0700242#endif
Simon Glass16a624b2017-01-16 07:03:57 -0700243
244static bool has_cpuid(void)
245{
246 return flag_is_changeable_p(X86_EFLAGS_ID);
247}
248
249static bool has_mtrr(void)
250{
251 return cpuid_edx(0x00000001) & (1 << 12) ? true : false;
252}
253
Simon Glassdd45a7a2019-12-06 21:41:51 -0700254#ifndef CONFIG_TPL_BUILD
Simon Glass16a624b2017-01-16 07:03:57 -0700255static int build_vendor_name(char *vendor_name)
256{
257 struct cpuid_result result;
258 result = cpuid(0x00000000);
259 unsigned int *name_as_ints = (unsigned int *)vendor_name;
260
261 name_as_ints[0] = result.ebx;
262 name_as_ints[1] = result.edx;
263 name_as_ints[2] = result.ecx;
264
265 return result.eax;
266}
Simon Glassdd45a7a2019-12-06 21:41:51 -0700267#endif
Simon Glass16a624b2017-01-16 07:03:57 -0700268
Simon Glass5684fe12024-08-27 19:44:24 -0600269int x86_cpu_vendor_info(char *name)
270{
271 uint cpu_device;
272
273 cpu_device = 0;
274
275 /* gcc 7.3 does not want to drop x86_vendors, so use #ifdef */
276#ifndef CONFIG_TPL_BUILD
277 *name = '\0'; /* Unset */
278
279 /* Find the id and vendor_name */
280 if (!has_cpuid()) {
281 /* Its a 486 if we can modify the AC flag */
282 if (flag_is_changeable_p(X86_EFLAGS_AC))
283 cpu_device = 0x00000400; /* 486 */
284 else
285 cpu_device = 0x00000300; /* 386 */
286 if (cpu_device == 0x00000400 && test_cyrix_52div()) {
287 /* If we ever care we can enable cpuid here */
288 memcpy(name, "CyrixInstead", 13);
289
290 /* Detect NexGen with old hypercode */
291 } else if (deep_magic_nexgen_probe()) {
292 memcpy(name, "NexGenDriven", 13);
293 }
294 } else {
295 int cpuid_level;
296
297 cpuid_level = build_vendor_name(name);
298 name[12] = '\0';
299
300 /* Intel-defined flags: level 0x00000001 */
301 if (cpuid_level >= 0x00000001)
302 cpu_device = cpuid_eax(0x00000001);
303 else
304 /* Have CPUID level 0 only unheard of */
305 cpu_device = 0x00000400;
306 }
307#endif /* CONFIG_TPL_BUILD */
308
309 return cpu_device;
310}
311
Simon Glass16a624b2017-01-16 07:03:57 -0700312static void identify_cpu(struct cpu_device_id *cpu)
313{
Simon Glassdd45a7a2019-12-06 21:41:51 -0700314 cpu->device = 0; /* fix gcc 4.4.4 warning */
315
316 /*
317 * Do a quick and dirty check to save space - Intel and AMD only and
318 * just the vendor. This is enough for most TPL code.
319 */
Simon Glassd4dce4a2024-09-29 19:49:36 -0600320 if (xpl_phase() == PHASE_TPL) {
Simon Glassdd45a7a2019-12-06 21:41:51 -0700321 struct cpuid_result result;
322
323 result = cpuid(0x00000000);
324 switch (result.ecx >> 24) {
325 case 'l': /* GenuineIntel */
326 cpu->vendor = X86_VENDOR_INTEL;
327 break;
328 case 'D': /* AuthenticAMD */
329 cpu->vendor = X86_VENDOR_AMD;
330 break;
331 default:
332 cpu->vendor = X86_VENDOR_ANY;
333 break;
334 }
335 return;
336 }
337
Simon Glassdd45a7a2019-12-06 21:41:51 -0700338#ifndef CONFIG_TPL_BUILD
Simon Glass5684fe12024-08-27 19:44:24 -0600339 {
340 char vendor_name[16];
341 int i;
Simon Glass16a624b2017-01-16 07:03:57 -0700342
Simon Glass5684fe12024-08-27 19:44:24 -0600343 cpu->device = x86_cpu_vendor_info(vendor_name);
Simon Glass16a624b2017-01-16 07:03:57 -0700344
Simon Glass5684fe12024-08-27 19:44:24 -0600345 cpu->vendor = X86_VENDOR_UNKNOWN;
346 for (i = 0; i < ARRAY_SIZE(x86_vendors); i++) {
347 if (memcmp(vendor_name, x86_vendors[i].name, 12) == 0) {
348 cpu->vendor = x86_vendors[i].vendor;
349 break;
350 }
Simon Glass16a624b2017-01-16 07:03:57 -0700351 }
352 }
Simon Glassdd45a7a2019-12-06 21:41:51 -0700353#endif
Simon Glass16a624b2017-01-16 07:03:57 -0700354}
355
356static inline void get_fms(struct cpuinfo_x86 *c, uint32_t tfms)
357{
358 c->x86 = (tfms >> 8) & 0xf;
359 c->x86_model = (tfms >> 4) & 0xf;
360 c->x86_mask = tfms & 0xf;
361 if (c->x86 == 0xf)
362 c->x86 += (tfms >> 20) & 0xff;
363 if (c->x86 >= 0x6)
364 c->x86_model += ((tfms >> 16) & 0xF) << 4;
365}
366
367u32 cpu_get_family_model(void)
368{
369 return gd->arch.x86_device & 0x0fff0ff0;
370}
371
372u32 cpu_get_stepping(void)
373{
374 return gd->arch.x86_mask;
375}
376
Simon Glass05e12f72019-04-25 21:58:42 -0600377/* initialise FPU, reset EM, set MP and NE */
378static void setup_cpu_features(void)
Simon Glass16a624b2017-01-16 07:03:57 -0700379{
380 const u32 em_rst = ~X86_CR0_EM;
381 const u32 mp_ne_set = X86_CR0_MP | X86_CR0_NE;
382
Simon Glass05e12f72019-04-25 21:58:42 -0600383 asm ("fninit\n" \
384 "movl %%cr0, %%eax\n" \
385 "andl %0, %%eax\n" \
386 "orl %1, %%eax\n" \
387 "movl %%eax, %%cr0\n" \
388 : : "i" (em_rst), "i" (mp_ne_set) : "eax");
389}
Simon Glass16a624b2017-01-16 07:03:57 -0700390
Simon Glassc5c4ed62020-07-02 21:12:12 -0600391void cpu_reinit_fpu(void)
392{
393 asm ("fninit\n");
394}
395
Simon Glass05e12f72019-04-25 21:58:42 -0600396static void setup_identity(void)
397{
Simon Glass16a624b2017-01-16 07:03:57 -0700398 /* identify CPU via cpuid and store the decoded info into gd->arch */
399 if (has_cpuid()) {
400 struct cpu_device_id cpu;
401 struct cpuinfo_x86 c;
402
403 identify_cpu(&cpu);
404 get_fms(&c, cpu.device);
405 gd->arch.x86 = c.x86;
406 gd->arch.x86_vendor = cpu.vendor;
407 gd->arch.x86_model = c.x86_model;
408 gd->arch.x86_mask = c.x86_mask;
409 gd->arch.x86_device = cpu.device;
410
411 gd->arch.has_mtrr = has_mtrr();
412 }
Simon Glass05e12f72019-04-25 21:58:42 -0600413}
414
Simon Glass2d8b3c62020-09-22 12:45:26 -0600415static uint cpu_cpuid_extended_level(void)
416{
417 return cpuid_eax(0x80000000);
418}
419
420int cpu_phys_address_size(void)
421{
422 if (!has_cpuid())
423 return 32;
424
425 if (cpu_cpuid_extended_level() >= 0x80000008)
426 return cpuid_eax(0x80000008) & 0xff;
427
428 if (cpuid_edx(1) & (CPUID_FEATURE_PAE | CPUID_FEATURE_PSE36))
429 return 36;
430
431 return 32;
432}
433
Simon Glass05e12f72019-04-25 21:58:42 -0600434static void setup_mtrr(void)
435{
436 u64 mtrr_cap;
Simon Glass16a624b2017-01-16 07:03:57 -0700437
438 /* Configure fixed range MTRRs for some legacy regions */
Simon Glassbccaa762021-06-27 17:51:01 -0600439 if (!gd->arch.has_mtrr || !ll_boot_init())
Simon Glass05e12f72019-04-25 21:58:42 -0600440 return;
Simon Glass16a624b2017-01-16 07:03:57 -0700441
Simon Glass05e12f72019-04-25 21:58:42 -0600442 mtrr_cap = native_read_msr(MTRR_CAP_MSR);
443 if (mtrr_cap & MTRR_CAP_FIX) {
444 /* Mark the VGA RAM area as uncacheable */
445 native_write_msr(MTRR_FIX_16K_A0000_MSR,
446 MTRR_FIX_TYPE(MTRR_TYPE_UNCACHEABLE),
447 MTRR_FIX_TYPE(MTRR_TYPE_UNCACHEABLE));
Simon Glass16a624b2017-01-16 07:03:57 -0700448
Simon Glass05e12f72019-04-25 21:58:42 -0600449 /*
450 * Mark the PCI ROM area as cacheable to improve ROM
451 * execution performance.
452 */
453 native_write_msr(MTRR_FIX_4K_C0000_MSR,
454 MTRR_FIX_TYPE(MTRR_TYPE_WRBACK),
455 MTRR_FIX_TYPE(MTRR_TYPE_WRBACK));
456 native_write_msr(MTRR_FIX_4K_C8000_MSR,
457 MTRR_FIX_TYPE(MTRR_TYPE_WRBACK),
458 MTRR_FIX_TYPE(MTRR_TYPE_WRBACK));
459 native_write_msr(MTRR_FIX_4K_D0000_MSR,
460 MTRR_FIX_TYPE(MTRR_TYPE_WRBACK),
461 MTRR_FIX_TYPE(MTRR_TYPE_WRBACK));
462 native_write_msr(MTRR_FIX_4K_D8000_MSR,
463 MTRR_FIX_TYPE(MTRR_TYPE_WRBACK),
464 MTRR_FIX_TYPE(MTRR_TYPE_WRBACK));
Simon Glass16a624b2017-01-16 07:03:57 -0700465
Simon Glass05e12f72019-04-25 21:58:42 -0600466 /* Enable the fixed range MTRRs */
467 msr_setbits_64(MTRR_DEF_TYPE_MSR, MTRR_DEF_TYPE_FIX_EN);
Simon Glass16a624b2017-01-16 07:03:57 -0700468 }
Simon Glass05e12f72019-04-25 21:58:42 -0600469}
Simon Glass16a624b2017-01-16 07:03:57 -0700470
Simon Glassdc444672019-10-20 21:37:54 -0600471int x86_cpu_init_tpl(void)
472{
473 setup_cpu_features();
474 setup_identity();
475
476 return 0;
477}
478
Simon Glass05e12f72019-04-25 21:58:42 -0600479int x86_cpu_init_f(void)
480{
481 if (ll_boot_init())
482 setup_cpu_features();
483 setup_identity();
484 setup_mtrr();
Simon Glass05e12f72019-04-25 21:58:42 -0600485
Simon Glass16a624b2017-01-16 07:03:57 -0700486 /* Set up the i8254 timer if required */
Simon Glass05e12f72019-04-25 21:58:42 -0600487 if (IS_ENABLED(CONFIG_I8254_TIMER))
488 i8254_init();
489
490 return 0;
491}
492
493int x86_cpu_reinit_f(void)
494{
Simon Glassfb8736d2020-07-16 21:22:34 -0600495 long addr;
496
Simon Glass05e12f72019-04-25 21:58:42 -0600497 setup_identity();
Simon Glassfb8736d2020-07-16 21:22:34 -0600498 addr = locate_coreboot_table();
499 if (addr >= 0) {
500 gd->arch.coreboot_table = addr;
Simon Glass6bd98e02020-04-26 09:12:59 -0600501 gd->flags |= GD_FLG_SKIP_LL_INIT;
Simon Glassfb8736d2020-07-16 21:22:34 -0600502 }
Simon Glass16a624b2017-01-16 07:03:57 -0700503
504 return 0;
505}
506
Simon Glass3ebdefc2024-08-27 19:44:25 -0600507void x86_get_identity_for_timer(void)
508{
509 setup_identity();
510}
511
Simon Glass16a624b2017-01-16 07:03:57 -0700512void x86_enable_caches(void)
513{
514 unsigned long cr0;
515
516 cr0 = read_cr0();
517 cr0 &= ~(X86_CR0_NW | X86_CR0_CD);
518 write_cr0(cr0);
519 wbinvd();
520}
521void enable_caches(void) __attribute__((weak, alias("x86_enable_caches")));
522
523void x86_disable_caches(void)
524{
525 unsigned long cr0;
526
527 cr0 = read_cr0();
528 cr0 |= X86_CR0_NW | X86_CR0_CD;
529 wbinvd();
530 write_cr0(cr0);
531 wbinvd();
532}
533void disable_caches(void) __attribute__((weak, alias("x86_disable_caches")));
534
535int dcache_status(void)
536{
537 return !(read_cr0() & X86_CR0_CD);
538}
539
540void cpu_enable_paging_pae(ulong cr3)
541{
542 __asm__ __volatile__(
543 /* Load the page table address */
544 "movl %0, %%cr3\n"
545 /* Enable pae */
546 "movl %%cr4, %%eax\n"
547 "orl $0x00000020, %%eax\n"
548 "movl %%eax, %%cr4\n"
549 /* Enable paging */
550 "movl %%cr0, %%eax\n"
551 "orl $0x80000000, %%eax\n"
552 "movl %%eax, %%cr0\n"
553 :
554 : "r" (cr3)
555 : "eax");
556}
557
558void cpu_disable_paging_pae(void)
559{
560 /* Turn off paging */
561 __asm__ __volatile__ (
562 /* Disable paging */
563 "movl %%cr0, %%eax\n"
564 "andl $0x7fffffff, %%eax\n"
565 "movl %%eax, %%cr0\n"
566 /* Disable pae */
567 "movl %%cr4, %%eax\n"
568 "andl $0xffffffdf, %%eax\n"
569 "movl %%eax, %%cr4\n"
570 :
571 :
572 : "eax");
573}
574
575static bool can_detect_long_mode(void)
576{
577 return cpuid_eax(0x80000000) > 0x80000000UL;
578}
579
580static bool has_long_mode(void)
581{
582 return cpuid_edx(0x80000001) & (1 << 29) ? true : false;
583}
584
585int cpu_has_64bit(void)
586{
587 return has_cpuid() && can_detect_long_mode() &&
588 has_long_mode();
589}
590
Simon Glass134826f2023-05-04 16:50:59 -0600591/* Base address for page tables used for 64-bit mode */
Bin Meng6aac2ca2019-01-31 08:22:12 -0800592#define PAGETABLE_BASE 0x80000
Simon Glass16a624b2017-01-16 07:03:57 -0700593#define PAGETABLE_SIZE (6 * 4096)
594
Simon Glass28dc4f82025-03-15 14:25:32 +0000595#define _PRES BIT(0) /* present */
596#define _RW BIT(1) /* write allowed */
597#define _US BIT(2) /* user-access allowed */
598#define _A BIT(5) /* has been accessed */
599#define _D BIT(6) /* has been written to */
600#define _PS BIT(7) /* indicates 2MB page size here */
601
Simon Glass16a624b2017-01-16 07:03:57 -0700602/**
603 * build_pagetable() - build a flat 4GiB page table structure for 64-bti mode
604 *
605 * @pgtable: Pointer to a 24iKB block of memory
606 */
607static void build_pagetable(uint32_t *pgtable)
608{
609 uint i;
610
611 memset(pgtable, '\0', PAGETABLE_SIZE);
612
613 /* Level 4 needs a single entry */
Simon Glass28dc4f82025-03-15 14:25:32 +0000614 pgtable[0] = (ulong)&pgtable[1024] + _PRES + _RW + _US + _A;
Simon Glass16a624b2017-01-16 07:03:57 -0700615
616 /* Level 3 has one 64-bit entry for each GiB of memory */
617 for (i = 0; i < 4; i++)
Simon Glass28dc4f82025-03-15 14:25:32 +0000618 pgtable[1024 + i * 2] = (ulong)&pgtable[2048] + 0x1000 * i +
619 _PRES + _RW + _US + _A;
Simon Glass16a624b2017-01-16 07:03:57 -0700620
621 /* Level 2 has 2048 64-bit entries, each repesenting 2MiB */
622 for (i = 0; i < 2048; i++)
Simon Glass28dc4f82025-03-15 14:25:32 +0000623 pgtable[2048 + i * 2] = _PRES + _RW + _US + _PS + _A + _D +
624 (i << 21UL);
Simon Glass16a624b2017-01-16 07:03:57 -0700625}
626
627int cpu_jump_to_64bit(ulong setup_base, ulong target)
628{
629 uint32_t *pgtable;
630
631 pgtable = memalign(4096, PAGETABLE_SIZE);
632 if (!pgtable)
633 return -ENOMEM;
634
635 build_pagetable(pgtable);
636 cpu_call64((ulong)pgtable, setup_base, target);
637 free(pgtable);
638
639 return -EFAULT;
640}
641
Simon Glass1e32ede2017-01-16 07:04:15 -0700642/*
Simon Glass134826f2023-05-04 16:50:59 -0600643 * cpu_jump_to_64bit_uboot() - Jump from SPL to U-Boot
Simon Glass1e32ede2017-01-16 07:04:15 -0700644 *
Simon Glass134826f2023-05-04 16:50:59 -0600645 * It works by setting up page tables and calling the code to enter 64-bit long
646 * mode
Simon Glass1e32ede2017-01-16 07:04:15 -0700647 */
648int cpu_jump_to_64bit_uboot(ulong target)
649{
Simon Glass1e32ede2017-01-16 07:04:15 -0700650 uint32_t *pgtable;
Simon Glass1e32ede2017-01-16 07:04:15 -0700651
Bin Meng6aac2ca2019-01-31 08:22:12 -0800652 pgtable = (uint32_t *)PAGETABLE_BASE;
Simon Glass1e32ede2017-01-16 07:04:15 -0700653 build_pagetable(pgtable);
654
Simon Glass1e32ede2017-01-16 07:04:15 -0700655 /* Jump to U-Boot */
Simon Glass134826f2023-05-04 16:50:59 -0600656 cpu_call64(PAGETABLE_BASE, 0, (ulong)target);
Simon Glass1e32ede2017-01-16 07:04:15 -0700657
658 return -EFAULT;
659}
660
Simon Glass16a624b2017-01-16 07:03:57 -0700661int x86_mp_init(void)
662{
Simon Glasse40633d2020-07-17 08:48:08 -0600663 int ret;
Simon Glass16a624b2017-01-16 07:03:57 -0700664
Simon Glasse40633d2020-07-17 08:48:08 -0600665 ret = mp_init();
666 if (ret) {
Simon Glass16a624b2017-01-16 07:03:57 -0700667 printf("Warning: MP init failure\n");
Simon Glasse40633d2020-07-17 08:48:08 -0600668 return log_ret(ret);
Simon Glass16a624b2017-01-16 07:03:57 -0700669 }
670
671 return 0;
672}