blob: a51a24498a7f4c96c93f9e922eb64c8c7810e822 [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);
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 Schuchardtdccdd932020-12-22 07:53:03 +0100179 * we used in arch/x86/cpu/start16.S, and reload the segment registers.
Simon Glass16a624b2017-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 Glassdd45a7a2019-12-06 21:41:51 -0700218#ifndef CONFIG_TPL_BUILD
Simon Glass16a624b2017-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 Glassdd45a7a2019-12-06 21:41:51 -0700239#endif
Simon Glass16a624b2017-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 Glassdd45a7a2019-12-06 21:41:51 -0700251#ifndef CONFIG_TPL_BUILD
Simon Glass16a624b2017-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 Glassdd45a7a2019-12-06 21:41:51 -0700264#endif
Simon Glass16a624b2017-01-16 07:03:57 -0700265
Simon Glass5684fe12024-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 Glass16a624b2017-01-16 07:03:57 -0700309static void identify_cpu(struct cpu_device_id *cpu)
310{
Simon Glassdd45a7a2019-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 Glassd4dce4a2024-09-29 19:49:36 -0600317 if (xpl_phase() == PHASE_TPL) {
Simon Glassdd45a7a2019-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 Glassdd45a7a2019-12-06 21:41:51 -0700335#ifndef CONFIG_TPL_BUILD
Simon Glass5684fe12024-08-27 19:44:24 -0600336 {
337 char vendor_name[16];
338 int i;
Simon Glass16a624b2017-01-16 07:03:57 -0700339
Simon Glass5684fe12024-08-27 19:44:24 -0600340 cpu->device = x86_cpu_vendor_info(vendor_name);
Simon Glass16a624b2017-01-16 07:03:57 -0700341
Simon Glass5684fe12024-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 Glass16a624b2017-01-16 07:03:57 -0700348 }
349 }
Simon Glassdd45a7a2019-12-06 21:41:51 -0700350#endif
Simon Glass16a624b2017-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 Glass05e12f72019-04-25 21:58:42 -0600374/* initialise FPU, reset EM, set MP and NE */
375static void setup_cpu_features(void)
Simon Glass16a624b2017-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 Glass05e12f72019-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 Glass16a624b2017-01-16 07:03:57 -0700387
Simon Glassc5c4ed62020-07-02 21:12:12 -0600388void cpu_reinit_fpu(void)
389{
390 asm ("fninit\n");
391}
392
Simon Glass05e12f72019-04-25 21:58:42 -0600393static void setup_identity(void)
394{
Simon Glass16a624b2017-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 Glass05e12f72019-04-25 21:58:42 -0600410}
411
Simon Glass2d8b3c62020-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 Glass05e12f72019-04-25 21:58:42 -0600431static void setup_mtrr(void)
432{
433 u64 mtrr_cap;
Simon Glass16a624b2017-01-16 07:03:57 -0700434
435 /* Configure fixed range MTRRs for some legacy regions */
Simon Glassbccaa762021-06-27 17:51:01 -0600436 if (!gd->arch.has_mtrr || !ll_boot_init())
Simon Glass05e12f72019-04-25 21:58:42 -0600437 return;
Simon Glass16a624b2017-01-16 07:03:57 -0700438
Simon Glass05e12f72019-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 Glass16a624b2017-01-16 07:03:57 -0700445
Simon Glass05e12f72019-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 Glass16a624b2017-01-16 07:03:57 -0700462
Simon Glass05e12f72019-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 Glass16a624b2017-01-16 07:03:57 -0700465 }
Simon Glass05e12f72019-04-25 21:58:42 -0600466}
Simon Glass16a624b2017-01-16 07:03:57 -0700467
Simon Glassdc444672019-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 Glass05e12f72019-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 Glass05e12f72019-04-25 21:58:42 -0600482
Simon Glass16a624b2017-01-16 07:03:57 -0700483 /* Set up the i8254 timer if required */
Simon Glass05e12f72019-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 Glassfb8736d2020-07-16 21:22:34 -0600492 long addr;
493
Simon Glass05e12f72019-04-25 21:58:42 -0600494 setup_identity();
Simon Glassfb8736d2020-07-16 21:22:34 -0600495 addr = locate_coreboot_table();
496 if (addr >= 0) {
497 gd->arch.coreboot_table = addr;
Simon Glass6bd98e02020-04-26 09:12:59 -0600498 gd->flags |= GD_FLG_SKIP_LL_INIT;
Simon Glassfb8736d2020-07-16 21:22:34 -0600499 }
Simon Glass16a624b2017-01-16 07:03:57 -0700500
501 return 0;
502}
503
Simon Glass3ebdefc2024-08-27 19:44:25 -0600504void x86_get_identity_for_timer(void)
505{
506 setup_identity();
507}
508
Simon Glass16a624b2017-01-16 07:03:57 -0700509void x86_enable_caches(void)
510{
511 unsigned long cr0;
512
513 cr0 = read_cr0();
514 cr0 &= ~(X86_CR0_NW | X86_CR0_CD);
515 write_cr0(cr0);
516 wbinvd();
517}
518void enable_caches(void) __attribute__((weak, alias("x86_enable_caches")));
519
520void x86_disable_caches(void)
521{
522 unsigned long cr0;
523
524 cr0 = read_cr0();
525 cr0 |= X86_CR0_NW | X86_CR0_CD;
526 wbinvd();
527 write_cr0(cr0);
528 wbinvd();
529}
530void disable_caches(void) __attribute__((weak, alias("x86_disable_caches")));
531
532int dcache_status(void)
533{
534 return !(read_cr0() & X86_CR0_CD);
535}
536
537void cpu_enable_paging_pae(ulong cr3)
538{
539 __asm__ __volatile__(
540 /* Load the page table address */
541 "movl %0, %%cr3\n"
542 /* Enable pae */
543 "movl %%cr4, %%eax\n"
544 "orl $0x00000020, %%eax\n"
545 "movl %%eax, %%cr4\n"
546 /* Enable paging */
547 "movl %%cr0, %%eax\n"
548 "orl $0x80000000, %%eax\n"
549 "movl %%eax, %%cr0\n"
550 :
551 : "r" (cr3)
552 : "eax");
553}
554
555void cpu_disable_paging_pae(void)
556{
557 /* Turn off paging */
558 __asm__ __volatile__ (
559 /* Disable paging */
560 "movl %%cr0, %%eax\n"
561 "andl $0x7fffffff, %%eax\n"
562 "movl %%eax, %%cr0\n"
563 /* Disable pae */
564 "movl %%cr4, %%eax\n"
565 "andl $0xffffffdf, %%eax\n"
566 "movl %%eax, %%cr4\n"
567 :
568 :
569 : "eax");
570}
571
572static bool can_detect_long_mode(void)
573{
574 return cpuid_eax(0x80000000) > 0x80000000UL;
575}
576
577static bool has_long_mode(void)
578{
579 return cpuid_edx(0x80000001) & (1 << 29) ? true : false;
580}
581
582int cpu_has_64bit(void)
583{
584 return has_cpuid() && can_detect_long_mode() &&
585 has_long_mode();
586}
587
Simon Glass134826f2023-05-04 16:50:59 -0600588/* Base address for page tables used for 64-bit mode */
Bin Meng6aac2ca2019-01-31 08:22:12 -0800589#define PAGETABLE_BASE 0x80000
Simon Glass16a624b2017-01-16 07:03:57 -0700590#define PAGETABLE_SIZE (6 * 4096)
591
592/**
593 * build_pagetable() - build a flat 4GiB page table structure for 64-bti mode
594 *
595 * @pgtable: Pointer to a 24iKB block of memory
596 */
597static void build_pagetable(uint32_t *pgtable)
598{
599 uint i;
600
601 memset(pgtable, '\0', PAGETABLE_SIZE);
602
603 /* Level 4 needs a single entry */
604 pgtable[0] = (ulong)&pgtable[1024] + 7;
605
606 /* Level 3 has one 64-bit entry for each GiB of memory */
607 for (i = 0; i < 4; i++)
608 pgtable[1024 + i * 2] = (ulong)&pgtable[2048] + 0x1000 * i + 7;
609
610 /* Level 2 has 2048 64-bit entries, each repesenting 2MiB */
611 for (i = 0; i < 2048; i++)
612 pgtable[2048 + i * 2] = 0x183 + (i << 21UL);
613}
614
615int cpu_jump_to_64bit(ulong setup_base, ulong target)
616{
617 uint32_t *pgtable;
618
619 pgtable = memalign(4096, PAGETABLE_SIZE);
620 if (!pgtable)
621 return -ENOMEM;
622
623 build_pagetable(pgtable);
624 cpu_call64((ulong)pgtable, setup_base, target);
625 free(pgtable);
626
627 return -EFAULT;
628}
629
Simon Glass1e32ede2017-01-16 07:04:15 -0700630/*
Simon Glass134826f2023-05-04 16:50:59 -0600631 * cpu_jump_to_64bit_uboot() - Jump from SPL to U-Boot
Simon Glass1e32ede2017-01-16 07:04:15 -0700632 *
Simon Glass134826f2023-05-04 16:50:59 -0600633 * It works by setting up page tables and calling the code to enter 64-bit long
634 * mode
Simon Glass1e32ede2017-01-16 07:04:15 -0700635 */
636int cpu_jump_to_64bit_uboot(ulong target)
637{
Simon Glass1e32ede2017-01-16 07:04:15 -0700638 uint32_t *pgtable;
Simon Glass1e32ede2017-01-16 07:04:15 -0700639
Bin Meng6aac2ca2019-01-31 08:22:12 -0800640 pgtable = (uint32_t *)PAGETABLE_BASE;
Simon Glass1e32ede2017-01-16 07:04:15 -0700641 build_pagetable(pgtable);
642
Simon Glass1e32ede2017-01-16 07:04:15 -0700643 /* Jump to U-Boot */
Simon Glass134826f2023-05-04 16:50:59 -0600644 cpu_call64(PAGETABLE_BASE, 0, (ulong)target);
Simon Glass1e32ede2017-01-16 07:04:15 -0700645
646 return -EFAULT;
647}
648
Simon Glass16a624b2017-01-16 07:03:57 -0700649int x86_mp_init(void)
650{
Simon Glasse40633d2020-07-17 08:48:08 -0600651 int ret;
Simon Glass16a624b2017-01-16 07:03:57 -0700652
Simon Glasse40633d2020-07-17 08:48:08 -0600653 ret = mp_init();
654 if (ret) {
Simon Glass16a624b2017-01-16 07:03:57 -0700655 printf("Warning: MP init failure\n");
Simon Glasse40633d2020-07-17 08:48:08 -0600656 return log_ret(ret);
Simon Glass16a624b2017-01-16 07:03:57 -0700657 }
658
659 return 0;
660}