blob: 532f690c8748f9647f42c3d69aa3e96fca728b4d [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassbe059e82017-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 Glassbe059e82017-01-16 07:03:57 -070019 */
20
Simon Glass9edefc22019-11-14 12:57:37 -070021#include <cpu_func.h>
Simon Glass35a3f872019-12-28 10:44:56 -070022#include <init.h>
Simon Glass78d57d62020-07-17 08:48:08 -060023#include <log.h>
Simon Glassbe059e82017-01-16 07:03:57 -070024#include <malloc.h>
Simon Glasscaca13f2019-12-06 21:41:51 -070025#include <spl.h>
Simon Glassbe059e82017-01-16 07:03:57 -070026#include <asm/control_regs.h>
Simon Glass7ec0e7b2020-04-30 21:21:39 -060027#include <asm/coreboot_tables.h>
Simon Glassbe059e82017-01-16 07:03:57 -070028#include <asm/cpu.h>
Simon Glass401d1c42020-10-30 21:38:53 -060029#include <asm/global_data.h>
Simon Glassbe059e82017-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 Rini03de3052024-05-20 13:35:03 -060034#include <asm/u-boot-x86.h>
Simon Glassbe059e82017-01-16 07:03:57 -070035
36DECLARE_GLOBAL_DATA_PTR;
37
Simon Glassaec7c1c2020-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 Glassbe059e82017-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 Glasscaca13f2019-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 Glassbe059e82017-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 Glasscaca13f2019-12-06 21:41:51 -070092#endif
Simon Glassbe059e82017-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 Yamada2fa863e2020-01-08 20:13:42 +0900146 /*
147 * FS: data, read/write, sizeof (Global Data Pointer),
148 * base (Global Data Pointer)
149 */
Simon Glassbe059e82017-01-16 07:03:57 -0700150 new_gd->arch.gd_addr = new_gd;
Masahiro Yamada2fa863e2020-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 Glassbe059e82017-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);
163
164 load_gdt(gdt_addr, X86_GDT_NUM_ENTRIES);
165 load_ds(X86_GDT_ENTRY_32BIT_DS);
166 load_es(X86_GDT_ENTRY_32BIT_DS);
167 load_gs(X86_GDT_ENTRY_32BIT_DS);
168 load_ss(X86_GDT_ENTRY_32BIT_DS);
169 load_fs(X86_GDT_ENTRY_32BIT_FS);
170}
171
172#ifdef CONFIG_HAVE_FSP
173/*
174 * Setup FSP execution environment GDT
175 *
176 * Per Intel FSP external architecture specification, before calling any FSP
177 * APIs, we need make sure the system is in flat 32-bit mode and both the code
178 * and data selectors should have full 4GB access range. Here we reuse the one
Heinrich Schuchardta0df9242020-12-22 07:53:03 +0100179 * we used in arch/x86/cpu/start16.S, and reload the segment registers.
Simon Glassbe059e82017-01-16 07:03:57 -0700180 */
181void setup_fsp_gdt(void)
182{
183 load_gdt((const u64 *)(gdt_rom + CONFIG_RESET_SEG_START), 4);
184 load_ds(X86_GDT_ENTRY_32BIT_DS);
185 load_ss(X86_GDT_ENTRY_32BIT_DS);
186 load_es(X86_GDT_ENTRY_32BIT_DS);
187 load_fs(X86_GDT_ENTRY_32BIT_DS);
188 load_gs(X86_GDT_ENTRY_32BIT_DS);
189}
190#endif
191
192/*
193 * Cyrix CPUs without cpuid or with cpuid not yet enabled can be detected
194 * by the fact that they preserve the flags across the division of 5/2.
195 * PII and PPro exhibit this behavior too, but they have cpuid available.
196 */
197
198/*
199 * Perform the Cyrix 5/2 test. A Cyrix won't change
200 * the flags, while other 486 chips will.
201 */
202static inline int test_cyrix_52div(void)
203{
204 unsigned int test;
205
206 __asm__ __volatile__(
207 "sahf\n\t" /* clear flags (%eax = 0x0005) */
208 "div %b2\n\t" /* divide 5 by 2 */
209 "lahf" /* store flags into %ah */
210 : "=a" (test)
211 : "0" (5), "q" (2)
212 : "cc");
213
214 /* AH is 0x02 on Cyrix after the divide.. */
215 return (unsigned char) (test >> 8) == 0x02;
216}
217
Simon Glasscaca13f2019-12-06 21:41:51 -0700218#ifndef CONFIG_TPL_BUILD
Simon Glassbe059e82017-01-16 07:03:57 -0700219/*
220 * Detect a NexGen CPU running without BIOS hypercode new enough
221 * to have CPUID. (Thanks to Herbert Oppmann)
222 */
223static int deep_magic_nexgen_probe(void)
224{
225 int ret;
226
227 __asm__ __volatile__ (
228 " movw $0x5555, %%ax\n"
229 " xorw %%dx,%%dx\n"
230 " movw $2, %%cx\n"
231 " divw %%cx\n"
232 " movl $0, %%eax\n"
233 " jnz 1f\n"
234 " movl $1, %%eax\n"
235 "1:\n"
236 : "=a" (ret) : : "cx", "dx");
237 return ret;
238}
Simon Glasscaca13f2019-12-06 21:41:51 -0700239#endif
Simon Glassbe059e82017-01-16 07:03:57 -0700240
241static bool has_cpuid(void)
242{
243 return flag_is_changeable_p(X86_EFLAGS_ID);
244}
245
246static bool has_mtrr(void)
247{
248 return cpuid_edx(0x00000001) & (1 << 12) ? true : false;
249}
250
Simon Glasscaca13f2019-12-06 21:41:51 -0700251#ifndef CONFIG_TPL_BUILD
Simon Glassbe059e82017-01-16 07:03:57 -0700252static int build_vendor_name(char *vendor_name)
253{
254 struct cpuid_result result;
255 result = cpuid(0x00000000);
256 unsigned int *name_as_ints = (unsigned int *)vendor_name;
257
258 name_as_ints[0] = result.ebx;
259 name_as_ints[1] = result.edx;
260 name_as_ints[2] = result.ecx;
261
262 return result.eax;
263}
Simon Glasscaca13f2019-12-06 21:41:51 -0700264#endif
Simon Glassbe059e82017-01-16 07:03:57 -0700265
Simon Glass6ab545b2024-08-27 19:44:24 -0600266int x86_cpu_vendor_info(char *name)
267{
268 uint cpu_device;
269
270 cpu_device = 0;
271
272 /* gcc 7.3 does not want to drop x86_vendors, so use #ifdef */
273#ifndef CONFIG_TPL_BUILD
274 *name = '\0'; /* Unset */
275
276 /* Find the id and vendor_name */
277 if (!has_cpuid()) {
278 /* Its a 486 if we can modify the AC flag */
279 if (flag_is_changeable_p(X86_EFLAGS_AC))
280 cpu_device = 0x00000400; /* 486 */
281 else
282 cpu_device = 0x00000300; /* 386 */
283 if (cpu_device == 0x00000400 && test_cyrix_52div()) {
284 /* If we ever care we can enable cpuid here */
285 memcpy(name, "CyrixInstead", 13);
286
287 /* Detect NexGen with old hypercode */
288 } else if (deep_magic_nexgen_probe()) {
289 memcpy(name, "NexGenDriven", 13);
290 }
291 } else {
292 int cpuid_level;
293
294 cpuid_level = build_vendor_name(name);
295 name[12] = '\0';
296
297 /* Intel-defined flags: level 0x00000001 */
298 if (cpuid_level >= 0x00000001)
299 cpu_device = cpuid_eax(0x00000001);
300 else
301 /* Have CPUID level 0 only unheard of */
302 cpu_device = 0x00000400;
303 }
304#endif /* CONFIG_TPL_BUILD */
305
306 return cpu_device;
307}
308
Simon Glassbe059e82017-01-16 07:03:57 -0700309static void identify_cpu(struct cpu_device_id *cpu)
310{
Simon Glasscaca13f2019-12-06 21:41:51 -0700311 cpu->device = 0; /* fix gcc 4.4.4 warning */
312
313 /*
314 * Do a quick and dirty check to save space - Intel and AMD only and
315 * just the vendor. This is enough for most TPL code.
316 */
Simon Glass456bdb72024-09-29 19:49:36 -0600317 if (xpl_phase() == PHASE_TPL) {
Simon Glasscaca13f2019-12-06 21:41:51 -0700318 struct cpuid_result result;
319
320 result = cpuid(0x00000000);
321 switch (result.ecx >> 24) {
322 case 'l': /* GenuineIntel */
323 cpu->vendor = X86_VENDOR_INTEL;
324 break;
325 case 'D': /* AuthenticAMD */
326 cpu->vendor = X86_VENDOR_AMD;
327 break;
328 default:
329 cpu->vendor = X86_VENDOR_ANY;
330 break;
331 }
332 return;
333 }
334
Simon Glasscaca13f2019-12-06 21:41:51 -0700335#ifndef CONFIG_TPL_BUILD
Simon Glass6ab545b2024-08-27 19:44:24 -0600336 {
337 char vendor_name[16];
338 int i;
Simon Glassbe059e82017-01-16 07:03:57 -0700339
Simon Glass6ab545b2024-08-27 19:44:24 -0600340 cpu->device = x86_cpu_vendor_info(vendor_name);
Simon Glassbe059e82017-01-16 07:03:57 -0700341
Simon Glass6ab545b2024-08-27 19:44:24 -0600342 cpu->vendor = X86_VENDOR_UNKNOWN;
343 for (i = 0; i < ARRAY_SIZE(x86_vendors); i++) {
344 if (memcmp(vendor_name, x86_vendors[i].name, 12) == 0) {
345 cpu->vendor = x86_vendors[i].vendor;
346 break;
347 }
Simon Glassbe059e82017-01-16 07:03:57 -0700348 }
349 }
Simon Glasscaca13f2019-12-06 21:41:51 -0700350#endif
Simon Glassbe059e82017-01-16 07:03:57 -0700351}
352
353static inline void get_fms(struct cpuinfo_x86 *c, uint32_t tfms)
354{
355 c->x86 = (tfms >> 8) & 0xf;
356 c->x86_model = (tfms >> 4) & 0xf;
357 c->x86_mask = tfms & 0xf;
358 if (c->x86 == 0xf)
359 c->x86 += (tfms >> 20) & 0xff;
360 if (c->x86 >= 0x6)
361 c->x86_model += ((tfms >> 16) & 0xF) << 4;
362}
363
364u32 cpu_get_family_model(void)
365{
366 return gd->arch.x86_device & 0x0fff0ff0;
367}
368
369u32 cpu_get_stepping(void)
370{
371 return gd->arch.x86_mask;
372}
373
Simon Glassc0069e92019-04-25 21:58:42 -0600374/* initialise FPU, reset EM, set MP and NE */
375static void setup_cpu_features(void)
Simon Glassbe059e82017-01-16 07:03:57 -0700376{
377 const u32 em_rst = ~X86_CR0_EM;
378 const u32 mp_ne_set = X86_CR0_MP | X86_CR0_NE;
379
Simon Glassc0069e92019-04-25 21:58:42 -0600380 asm ("fninit\n" \
381 "movl %%cr0, %%eax\n" \
382 "andl %0, %%eax\n" \
383 "orl %1, %%eax\n" \
384 "movl %%eax, %%cr0\n" \
385 : : "i" (em_rst), "i" (mp_ne_set) : "eax");
386}
Simon Glassbe059e82017-01-16 07:03:57 -0700387
Simon Glass3dada5a2020-07-02 21:12:12 -0600388void cpu_reinit_fpu(void)
389{
390 asm ("fninit\n");
391}
392
Simon Glassc0069e92019-04-25 21:58:42 -0600393static void setup_identity(void)
394{
Simon Glassbe059e82017-01-16 07:03:57 -0700395 /* identify CPU via cpuid and store the decoded info into gd->arch */
396 if (has_cpuid()) {
397 struct cpu_device_id cpu;
398 struct cpuinfo_x86 c;
399
400 identify_cpu(&cpu);
401 get_fms(&c, cpu.device);
402 gd->arch.x86 = c.x86;
403 gd->arch.x86_vendor = cpu.vendor;
404 gd->arch.x86_model = c.x86_model;
405 gd->arch.x86_mask = c.x86_mask;
406 gd->arch.x86_device = cpu.device;
407
408 gd->arch.has_mtrr = has_mtrr();
409 }
Simon Glassc0069e92019-04-25 21:58:42 -0600410}
411
Simon Glassaec7c1c2020-09-22 12:45:26 -0600412static uint cpu_cpuid_extended_level(void)
413{
414 return cpuid_eax(0x80000000);
415}
416
417int cpu_phys_address_size(void)
418{
419 if (!has_cpuid())
420 return 32;
421
422 if (cpu_cpuid_extended_level() >= 0x80000008)
423 return cpuid_eax(0x80000008) & 0xff;
424
425 if (cpuid_edx(1) & (CPUID_FEATURE_PAE | CPUID_FEATURE_PSE36))
426 return 36;
427
428 return 32;
429}
430
Simon Glassc0069e92019-04-25 21:58:42 -0600431static void setup_mtrr(void)
432{
433 u64 mtrr_cap;
Simon Glassbe059e82017-01-16 07:03:57 -0700434
435 /* Configure fixed range MTRRs for some legacy regions */
Simon Glass92873f82021-06-27 17:51:01 -0600436 if (!gd->arch.has_mtrr || !ll_boot_init())
Simon Glassc0069e92019-04-25 21:58:42 -0600437 return;
Simon Glassbe059e82017-01-16 07:03:57 -0700438
Simon Glassc0069e92019-04-25 21:58:42 -0600439 mtrr_cap = native_read_msr(MTRR_CAP_MSR);
440 if (mtrr_cap & MTRR_CAP_FIX) {
441 /* Mark the VGA RAM area as uncacheable */
442 native_write_msr(MTRR_FIX_16K_A0000_MSR,
443 MTRR_FIX_TYPE(MTRR_TYPE_UNCACHEABLE),
444 MTRR_FIX_TYPE(MTRR_TYPE_UNCACHEABLE));
Simon Glassbe059e82017-01-16 07:03:57 -0700445
Simon Glassc0069e92019-04-25 21:58:42 -0600446 /*
447 * Mark the PCI ROM area as cacheable to improve ROM
448 * execution performance.
449 */
450 native_write_msr(MTRR_FIX_4K_C0000_MSR,
451 MTRR_FIX_TYPE(MTRR_TYPE_WRBACK),
452 MTRR_FIX_TYPE(MTRR_TYPE_WRBACK));
453 native_write_msr(MTRR_FIX_4K_C8000_MSR,
454 MTRR_FIX_TYPE(MTRR_TYPE_WRBACK),
455 MTRR_FIX_TYPE(MTRR_TYPE_WRBACK));
456 native_write_msr(MTRR_FIX_4K_D0000_MSR,
457 MTRR_FIX_TYPE(MTRR_TYPE_WRBACK),
458 MTRR_FIX_TYPE(MTRR_TYPE_WRBACK));
459 native_write_msr(MTRR_FIX_4K_D8000_MSR,
460 MTRR_FIX_TYPE(MTRR_TYPE_WRBACK),
461 MTRR_FIX_TYPE(MTRR_TYPE_WRBACK));
Simon Glassbe059e82017-01-16 07:03:57 -0700462
Simon Glassc0069e92019-04-25 21:58:42 -0600463 /* Enable the fixed range MTRRs */
464 msr_setbits_64(MTRR_DEF_TYPE_MSR, MTRR_DEF_TYPE_FIX_EN);
Simon Glassbe059e82017-01-16 07:03:57 -0700465 }
Simon Glassc0069e92019-04-25 21:58:42 -0600466}
Simon Glassbe059e82017-01-16 07:03:57 -0700467
Simon Glassece3a462019-10-20 21:37:54 -0600468int x86_cpu_init_tpl(void)
469{
470 setup_cpu_features();
471 setup_identity();
472
473 return 0;
474}
475
Simon Glassc0069e92019-04-25 21:58:42 -0600476int x86_cpu_init_f(void)
477{
478 if (ll_boot_init())
479 setup_cpu_features();
480 setup_identity();
481 setup_mtrr();
Simon Glassc0069e92019-04-25 21:58:42 -0600482
Simon Glassbe059e82017-01-16 07:03:57 -0700483 /* Set up the i8254 timer if required */
Simon Glassc0069e92019-04-25 21:58:42 -0600484 if (IS_ENABLED(CONFIG_I8254_TIMER))
485 i8254_init();
486
487 return 0;
488}
489
490int x86_cpu_reinit_f(void)
491{
Simon Glass9ef16862020-07-16 21:22:34 -0600492 long addr;
493
Simon Glassc0069e92019-04-25 21:58:42 -0600494 setup_identity();
Simon Glass9ef16862020-07-16 21:22:34 -0600495 addr = locate_coreboot_table();
496 if (addr >= 0) {
497 gd->arch.coreboot_table = addr;
Simon Glasscfe7a102020-04-26 09:12:59 -0600498 gd->flags |= GD_FLG_SKIP_LL_INIT;
Simon Glass9ef16862020-07-16 21:22:34 -0600499 }
Simon Glassbe059e82017-01-16 07:03:57 -0700500
501 return 0;
502}
503
504void x86_enable_caches(void)
505{
506 unsigned long cr0;
507
508 cr0 = read_cr0();
509 cr0 &= ~(X86_CR0_NW | X86_CR0_CD);
510 write_cr0(cr0);
511 wbinvd();
512}
513void enable_caches(void) __attribute__((weak, alias("x86_enable_caches")));
514
515void x86_disable_caches(void)
516{
517 unsigned long cr0;
518
519 cr0 = read_cr0();
520 cr0 |= X86_CR0_NW | X86_CR0_CD;
521 wbinvd();
522 write_cr0(cr0);
523 wbinvd();
524}
525void disable_caches(void) __attribute__((weak, alias("x86_disable_caches")));
526
527int dcache_status(void)
528{
529 return !(read_cr0() & X86_CR0_CD);
530}
531
532void cpu_enable_paging_pae(ulong cr3)
533{
534 __asm__ __volatile__(
535 /* Load the page table address */
536 "movl %0, %%cr3\n"
537 /* Enable pae */
538 "movl %%cr4, %%eax\n"
539 "orl $0x00000020, %%eax\n"
540 "movl %%eax, %%cr4\n"
541 /* Enable paging */
542 "movl %%cr0, %%eax\n"
543 "orl $0x80000000, %%eax\n"
544 "movl %%eax, %%cr0\n"
545 :
546 : "r" (cr3)
547 : "eax");
548}
549
550void cpu_disable_paging_pae(void)
551{
552 /* Turn off paging */
553 __asm__ __volatile__ (
554 /* Disable paging */
555 "movl %%cr0, %%eax\n"
556 "andl $0x7fffffff, %%eax\n"
557 "movl %%eax, %%cr0\n"
558 /* Disable pae */
559 "movl %%cr4, %%eax\n"
560 "andl $0xffffffdf, %%eax\n"
561 "movl %%eax, %%cr4\n"
562 :
563 :
564 : "eax");
565}
566
567static bool can_detect_long_mode(void)
568{
569 return cpuid_eax(0x80000000) > 0x80000000UL;
570}
571
572static bool has_long_mode(void)
573{
574 return cpuid_edx(0x80000001) & (1 << 29) ? true : false;
575}
576
577int cpu_has_64bit(void)
578{
579 return has_cpuid() && can_detect_long_mode() &&
580 has_long_mode();
581}
582
Simon Glass4cb3b9f2023-05-04 16:50:59 -0600583/* Base address for page tables used for 64-bit mode */
Bin Mengdbb06962019-01-31 08:22:12 -0800584#define PAGETABLE_BASE 0x80000
Simon Glassbe059e82017-01-16 07:03:57 -0700585#define PAGETABLE_SIZE (6 * 4096)
586
587/**
588 * build_pagetable() - build a flat 4GiB page table structure for 64-bti mode
589 *
590 * @pgtable: Pointer to a 24iKB block of memory
591 */
592static void build_pagetable(uint32_t *pgtable)
593{
594 uint i;
595
596 memset(pgtable, '\0', PAGETABLE_SIZE);
597
598 /* Level 4 needs a single entry */
599 pgtable[0] = (ulong)&pgtable[1024] + 7;
600
601 /* Level 3 has one 64-bit entry for each GiB of memory */
602 for (i = 0; i < 4; i++)
603 pgtable[1024 + i * 2] = (ulong)&pgtable[2048] + 0x1000 * i + 7;
604
605 /* Level 2 has 2048 64-bit entries, each repesenting 2MiB */
606 for (i = 0; i < 2048; i++)
607 pgtable[2048 + i * 2] = 0x183 + (i << 21UL);
608}
609
610int cpu_jump_to_64bit(ulong setup_base, ulong target)
611{
612 uint32_t *pgtable;
613
614 pgtable = memalign(4096, PAGETABLE_SIZE);
615 if (!pgtable)
616 return -ENOMEM;
617
618 build_pagetable(pgtable);
619 cpu_call64((ulong)pgtable, setup_base, target);
620 free(pgtable);
621
622 return -EFAULT;
623}
624
Simon Glassfa5fcb32017-01-16 07:04:15 -0700625/*
Simon Glass4cb3b9f2023-05-04 16:50:59 -0600626 * cpu_jump_to_64bit_uboot() - Jump from SPL to U-Boot
Simon Glassfa5fcb32017-01-16 07:04:15 -0700627 *
Simon Glass4cb3b9f2023-05-04 16:50:59 -0600628 * It works by setting up page tables and calling the code to enter 64-bit long
629 * mode
Simon Glassfa5fcb32017-01-16 07:04:15 -0700630 */
631int cpu_jump_to_64bit_uboot(ulong target)
632{
Simon Glassfa5fcb32017-01-16 07:04:15 -0700633 uint32_t *pgtable;
Simon Glassfa5fcb32017-01-16 07:04:15 -0700634
Bin Mengdbb06962019-01-31 08:22:12 -0800635 pgtable = (uint32_t *)PAGETABLE_BASE;
Simon Glassfa5fcb32017-01-16 07:04:15 -0700636 build_pagetable(pgtable);
637
Simon Glassfa5fcb32017-01-16 07:04:15 -0700638 /* Jump to U-Boot */
Simon Glass4cb3b9f2023-05-04 16:50:59 -0600639 cpu_call64(PAGETABLE_BASE, 0, (ulong)target);
Simon Glassfa5fcb32017-01-16 07:04:15 -0700640
641 return -EFAULT;
642}
643
Simon Glassbe059e82017-01-16 07:03:57 -0700644int x86_mp_init(void)
645{
Simon Glass78d57d62020-07-17 08:48:08 -0600646 int ret;
Simon Glassbe059e82017-01-16 07:03:57 -0700647
Simon Glass78d57d62020-07-17 08:48:08 -0600648 ret = mp_init();
649 if (ret) {
Simon Glassbe059e82017-01-16 07:03:57 -0700650 printf("Warning: MP init failure\n");
Simon Glass78d57d62020-07-17 08:48:08 -0600651 return log_ret(ret);
Simon Glassbe059e82017-01-16 07:03:57 -0700652 }
653
654 return 0;
655}