blob: 6fa0f4d32baacde8a69f0fb5ce9b7971aee6992d [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
21#include <common.h>
Simon Glass1d91ba72019-11-14 12:57:37 -070022#include <cpu_func.h>
Simon Glassda25eff2019-12-28 10:44:56 -070023#include <init.h>
Simon Glasse40633d2020-07-17 08:48:08 -060024#include <log.h>
Simon Glass16a624b2017-01-16 07:03:57 -070025#include <malloc.h>
Simon Glassdd45a7a2019-12-06 21:41:51 -070026#include <spl.h>
Simon Glass16a624b2017-01-16 07:03:57 -070027#include <asm/control_regs.h>
Simon Glass46f4c582020-04-30 21:21:39 -060028#include <asm/coreboot_tables.h>
Simon Glass16a624b2017-01-16 07:03:57 -070029#include <asm/cpu.h>
30#include <asm/mp.h>
31#include <asm/msr.h>
32#include <asm/mtrr.h>
33#include <asm/processor-flags.h>
34
35DECLARE_GLOBAL_DATA_PTR;
36
Simon Glass2d8b3c62020-09-22 12:45:26 -060037#define CPUID_FEATURE_PAE BIT(6)
38#define CPUID_FEATURE_PSE36 BIT(17)
39#define CPUID_FEAURE_HTT BIT(28)
40
Simon Glass16a624b2017-01-16 07:03:57 -070041/*
42 * Constructor for a conventional segment GDT (or LDT) entry
43 * This is a macro so it can be used in initialisers
44 */
45#define GDT_ENTRY(flags, base, limit) \
46 ((((base) & 0xff000000ULL) << (56-24)) | \
47 (((flags) & 0x0000f0ffULL) << 40) | \
48 (((limit) & 0x000f0000ULL) << (48-16)) | \
49 (((base) & 0x00ffffffULL) << 16) | \
50 (((limit) & 0x0000ffffULL)))
51
52struct gdt_ptr {
53 u16 len;
54 u32 ptr;
55} __packed;
56
57struct cpu_device_id {
58 unsigned vendor;
59 unsigned device;
60};
61
62struct cpuinfo_x86 {
63 uint8_t x86; /* CPU family */
64 uint8_t x86_vendor; /* CPU vendor */
65 uint8_t x86_model;
66 uint8_t x86_mask;
67};
68
Simon Glassdd45a7a2019-12-06 21:41:51 -070069/* gcc 7.3 does not wwant to drop x86_vendors, so use #ifdef */
70#ifndef CONFIG_TPL_BUILD
Simon Glass16a624b2017-01-16 07:03:57 -070071/*
72 * List of cpu vendor strings along with their normalized
73 * id values.
74 */
75static const struct {
76 int vendor;
77 const char *name;
78} x86_vendors[] = {
79 { X86_VENDOR_INTEL, "GenuineIntel", },
80 { X86_VENDOR_CYRIX, "CyrixInstead", },
81 { X86_VENDOR_AMD, "AuthenticAMD", },
82 { X86_VENDOR_UMC, "UMC UMC UMC ", },
83 { X86_VENDOR_NEXGEN, "NexGenDriven", },
84 { X86_VENDOR_CENTAUR, "CentaurHauls", },
85 { X86_VENDOR_RISE, "RiseRiseRise", },
86 { X86_VENDOR_TRANSMETA, "GenuineTMx86", },
87 { X86_VENDOR_TRANSMETA, "TransmetaCPU", },
88 { X86_VENDOR_NSC, "Geode by NSC", },
89 { X86_VENDOR_SIS, "SiS SiS SiS ", },
90};
Simon Glassdd45a7a2019-12-06 21:41:51 -070091#endif
Simon Glass16a624b2017-01-16 07:03:57 -070092
93static void load_ds(u32 segment)
94{
95 asm volatile("movl %0, %%ds" : : "r" (segment * X86_GDT_ENTRY_SIZE));
96}
97
98static void load_es(u32 segment)
99{
100 asm volatile("movl %0, %%es" : : "r" (segment * X86_GDT_ENTRY_SIZE));
101}
102
103static void load_fs(u32 segment)
104{
105 asm volatile("movl %0, %%fs" : : "r" (segment * X86_GDT_ENTRY_SIZE));
106}
107
108static void load_gs(u32 segment)
109{
110 asm volatile("movl %0, %%gs" : : "r" (segment * X86_GDT_ENTRY_SIZE));
111}
112
113static void load_ss(u32 segment)
114{
115 asm volatile("movl %0, %%ss" : : "r" (segment * X86_GDT_ENTRY_SIZE));
116}
117
118static void load_gdt(const u64 *boot_gdt, u16 num_entries)
119{
120 struct gdt_ptr gdt;
121
122 gdt.len = (num_entries * X86_GDT_ENTRY_SIZE) - 1;
123 gdt.ptr = (ulong)boot_gdt;
124
125 asm volatile("lgdtl %0\n" : : "m" (gdt));
126}
127
128void arch_setup_gd(gd_t *new_gd)
129{
130 u64 *gdt_addr;
131
132 gdt_addr = new_gd->arch.gdt;
133
134 /*
135 * CS: code, read/execute, 4 GB, base 0
136 *
137 * Some OS (like VxWorks) requires GDT entry 1 to be the 32-bit CS
138 */
139 gdt_addr[X86_GDT_ENTRY_UNUSED] = GDT_ENTRY(0xc09b, 0, 0xfffff);
140 gdt_addr[X86_GDT_ENTRY_32BIT_CS] = GDT_ENTRY(0xc09b, 0, 0xfffff);
141
142 /* DS: data, read/write, 4 GB, base 0 */
143 gdt_addr[X86_GDT_ENTRY_32BIT_DS] = GDT_ENTRY(0xc093, 0, 0xfffff);
144
Masahiro Yamada4dcaa7d2020-01-08 20:13:42 +0900145 /*
146 * FS: data, read/write, sizeof (Global Data Pointer),
147 * base (Global Data Pointer)
148 */
Simon Glass16a624b2017-01-16 07:03:57 -0700149 new_gd->arch.gd_addr = new_gd;
Masahiro Yamada4dcaa7d2020-01-08 20:13:42 +0900150 gdt_addr[X86_GDT_ENTRY_32BIT_FS] = GDT_ENTRY(0x8093,
151 (ulong)&new_gd->arch.gd_addr,
152 sizeof(new_gd->arch.gd_addr) - 1);
Simon Glass16a624b2017-01-16 07:03:57 -0700153
154 /* 16-bit CS: code, read/execute, 64 kB, base 0 */
155 gdt_addr[X86_GDT_ENTRY_16BIT_CS] = GDT_ENTRY(0x009b, 0, 0x0ffff);
156
157 /* 16-bit DS: data, read/write, 64 kB, base 0 */
158 gdt_addr[X86_GDT_ENTRY_16BIT_DS] = GDT_ENTRY(0x0093, 0, 0x0ffff);
159
160 gdt_addr[X86_GDT_ENTRY_16BIT_FLAT_CS] = GDT_ENTRY(0x809b, 0, 0xfffff);
161 gdt_addr[X86_GDT_ENTRY_16BIT_FLAT_DS] = GDT_ENTRY(0x8093, 0, 0xfffff);
162
163 load_gdt(gdt_addr, X86_GDT_NUM_ENTRIES);
164 load_ds(X86_GDT_ENTRY_32BIT_DS);
165 load_es(X86_GDT_ENTRY_32BIT_DS);
166 load_gs(X86_GDT_ENTRY_32BIT_DS);
167 load_ss(X86_GDT_ENTRY_32BIT_DS);
168 load_fs(X86_GDT_ENTRY_32BIT_FS);
169}
170
171#ifdef CONFIG_HAVE_FSP
172/*
173 * Setup FSP execution environment GDT
174 *
175 * Per Intel FSP external architecture specification, before calling any FSP
176 * APIs, we need make sure the system is in flat 32-bit mode and both the code
177 * and data selectors should have full 4GB access range. Here we reuse the one
Heinrich Schuchardtdccdd932020-12-22 07:53:03 +0100178 * we used in arch/x86/cpu/start16.S, and reload the segment registers.
Simon Glass16a624b2017-01-16 07:03:57 -0700179 */
180void setup_fsp_gdt(void)
181{
182 load_gdt((const u64 *)(gdt_rom + CONFIG_RESET_SEG_START), 4);
183 load_ds(X86_GDT_ENTRY_32BIT_DS);
184 load_ss(X86_GDT_ENTRY_32BIT_DS);
185 load_es(X86_GDT_ENTRY_32BIT_DS);
186 load_fs(X86_GDT_ENTRY_32BIT_DS);
187 load_gs(X86_GDT_ENTRY_32BIT_DS);
188}
189#endif
190
191/*
192 * Cyrix CPUs without cpuid or with cpuid not yet enabled can be detected
193 * by the fact that they preserve the flags across the division of 5/2.
194 * PII and PPro exhibit this behavior too, but they have cpuid available.
195 */
196
197/*
198 * Perform the Cyrix 5/2 test. A Cyrix won't change
199 * the flags, while other 486 chips will.
200 */
201static inline int test_cyrix_52div(void)
202{
203 unsigned int test;
204
205 __asm__ __volatile__(
206 "sahf\n\t" /* clear flags (%eax = 0x0005) */
207 "div %b2\n\t" /* divide 5 by 2 */
208 "lahf" /* store flags into %ah */
209 : "=a" (test)
210 : "0" (5), "q" (2)
211 : "cc");
212
213 /* AH is 0x02 on Cyrix after the divide.. */
214 return (unsigned char) (test >> 8) == 0x02;
215}
216
Simon Glassdd45a7a2019-12-06 21:41:51 -0700217#ifndef CONFIG_TPL_BUILD
Simon Glass16a624b2017-01-16 07:03:57 -0700218/*
219 * Detect a NexGen CPU running without BIOS hypercode new enough
220 * to have CPUID. (Thanks to Herbert Oppmann)
221 */
222static int deep_magic_nexgen_probe(void)
223{
224 int ret;
225
226 __asm__ __volatile__ (
227 " movw $0x5555, %%ax\n"
228 " xorw %%dx,%%dx\n"
229 " movw $2, %%cx\n"
230 " divw %%cx\n"
231 " movl $0, %%eax\n"
232 " jnz 1f\n"
233 " movl $1, %%eax\n"
234 "1:\n"
235 : "=a" (ret) : : "cx", "dx");
236 return ret;
237}
Simon Glassdd45a7a2019-12-06 21:41:51 -0700238#endif
Simon Glass16a624b2017-01-16 07:03:57 -0700239
240static bool has_cpuid(void)
241{
242 return flag_is_changeable_p(X86_EFLAGS_ID);
243}
244
245static bool has_mtrr(void)
246{
247 return cpuid_edx(0x00000001) & (1 << 12) ? true : false;
248}
249
Simon Glassdd45a7a2019-12-06 21:41:51 -0700250#ifndef CONFIG_TPL_BUILD
Simon Glass16a624b2017-01-16 07:03:57 -0700251static int build_vendor_name(char *vendor_name)
252{
253 struct cpuid_result result;
254 result = cpuid(0x00000000);
255 unsigned int *name_as_ints = (unsigned int *)vendor_name;
256
257 name_as_ints[0] = result.ebx;
258 name_as_ints[1] = result.edx;
259 name_as_ints[2] = result.ecx;
260
261 return result.eax;
262}
Simon Glassdd45a7a2019-12-06 21:41:51 -0700263#endif
Simon Glass16a624b2017-01-16 07:03:57 -0700264
265static void identify_cpu(struct cpu_device_id *cpu)
266{
Simon Glassdd45a7a2019-12-06 21:41:51 -0700267 cpu->device = 0; /* fix gcc 4.4.4 warning */
268
269 /*
270 * Do a quick and dirty check to save space - Intel and AMD only and
271 * just the vendor. This is enough for most TPL code.
272 */
273 if (spl_phase() == PHASE_TPL) {
274 struct cpuid_result result;
275
276 result = cpuid(0x00000000);
277 switch (result.ecx >> 24) {
278 case 'l': /* GenuineIntel */
279 cpu->vendor = X86_VENDOR_INTEL;
280 break;
281 case 'D': /* AuthenticAMD */
282 cpu->vendor = X86_VENDOR_AMD;
283 break;
284 default:
285 cpu->vendor = X86_VENDOR_ANY;
286 break;
287 }
288 return;
289 }
290
291/* gcc 7.3 does not want to drop x86_vendors, so use #ifdef */
292#ifndef CONFIG_TPL_BUILD
Simon Glass16a624b2017-01-16 07:03:57 -0700293 char vendor_name[16];
294 int i;
295
296 vendor_name[0] = '\0'; /* Unset */
Simon Glass16a624b2017-01-16 07:03:57 -0700297
298 /* Find the id and vendor_name */
299 if (!has_cpuid()) {
300 /* Its a 486 if we can modify the AC flag */
301 if (flag_is_changeable_p(X86_EFLAGS_AC))
302 cpu->device = 0x00000400; /* 486 */
303 else
304 cpu->device = 0x00000300; /* 386 */
305 if ((cpu->device == 0x00000400) && test_cyrix_52div()) {
306 memcpy(vendor_name, "CyrixInstead", 13);
307 /* If we ever care we can enable cpuid here */
308 }
309 /* Detect NexGen with old hypercode */
310 else if (deep_magic_nexgen_probe())
311 memcpy(vendor_name, "NexGenDriven", 13);
Simon Glassdd45a7a2019-12-06 21:41:51 -0700312 } else {
313 int cpuid_level;
Simon Glass16a624b2017-01-16 07:03:57 -0700314
315 cpuid_level = build_vendor_name(vendor_name);
316 vendor_name[12] = '\0';
317
318 /* Intel-defined flags: level 0x00000001 */
319 if (cpuid_level >= 0x00000001) {
320 cpu->device = cpuid_eax(0x00000001);
321 } else {
322 /* Have CPUID level 0 only unheard of */
323 cpu->device = 0x00000400;
324 }
325 }
326 cpu->vendor = X86_VENDOR_UNKNOWN;
327 for (i = 0; i < ARRAY_SIZE(x86_vendors); i++) {
328 if (memcmp(vendor_name, x86_vendors[i].name, 12) == 0) {
329 cpu->vendor = x86_vendors[i].vendor;
330 break;
331 }
332 }
Simon Glassdd45a7a2019-12-06 21:41:51 -0700333#endif
Simon Glass16a624b2017-01-16 07:03:57 -0700334}
335
336static inline void get_fms(struct cpuinfo_x86 *c, uint32_t tfms)
337{
338 c->x86 = (tfms >> 8) & 0xf;
339 c->x86_model = (tfms >> 4) & 0xf;
340 c->x86_mask = tfms & 0xf;
341 if (c->x86 == 0xf)
342 c->x86 += (tfms >> 20) & 0xff;
343 if (c->x86 >= 0x6)
344 c->x86_model += ((tfms >> 16) & 0xF) << 4;
345}
346
347u32 cpu_get_family_model(void)
348{
349 return gd->arch.x86_device & 0x0fff0ff0;
350}
351
352u32 cpu_get_stepping(void)
353{
354 return gd->arch.x86_mask;
355}
356
Simon Glass05e12f72019-04-25 21:58:42 -0600357/* initialise FPU, reset EM, set MP and NE */
358static void setup_cpu_features(void)
Simon Glass16a624b2017-01-16 07:03:57 -0700359{
360 const u32 em_rst = ~X86_CR0_EM;
361 const u32 mp_ne_set = X86_CR0_MP | X86_CR0_NE;
362
Simon Glass05e12f72019-04-25 21:58:42 -0600363 asm ("fninit\n" \
364 "movl %%cr0, %%eax\n" \
365 "andl %0, %%eax\n" \
366 "orl %1, %%eax\n" \
367 "movl %%eax, %%cr0\n" \
368 : : "i" (em_rst), "i" (mp_ne_set) : "eax");
369}
Simon Glass16a624b2017-01-16 07:03:57 -0700370
Simon Glassc5c4ed62020-07-02 21:12:12 -0600371void cpu_reinit_fpu(void)
372{
373 asm ("fninit\n");
374}
375
Simon Glass05e12f72019-04-25 21:58:42 -0600376static void setup_identity(void)
377{
Simon Glass16a624b2017-01-16 07:03:57 -0700378 /* identify CPU via cpuid and store the decoded info into gd->arch */
379 if (has_cpuid()) {
380 struct cpu_device_id cpu;
381 struct cpuinfo_x86 c;
382
383 identify_cpu(&cpu);
384 get_fms(&c, cpu.device);
385 gd->arch.x86 = c.x86;
386 gd->arch.x86_vendor = cpu.vendor;
387 gd->arch.x86_model = c.x86_model;
388 gd->arch.x86_mask = c.x86_mask;
389 gd->arch.x86_device = cpu.device;
390
391 gd->arch.has_mtrr = has_mtrr();
392 }
Simon Glass05e12f72019-04-25 21:58:42 -0600393}
394
Simon Glass2d8b3c62020-09-22 12:45:26 -0600395static uint cpu_cpuid_extended_level(void)
396{
397 return cpuid_eax(0x80000000);
398}
399
400int cpu_phys_address_size(void)
401{
402 if (!has_cpuid())
403 return 32;
404
405 if (cpu_cpuid_extended_level() >= 0x80000008)
406 return cpuid_eax(0x80000008) & 0xff;
407
408 if (cpuid_edx(1) & (CPUID_FEATURE_PAE | CPUID_FEATURE_PSE36))
409 return 36;
410
411 return 32;
412}
413
Simon Glass05e12f72019-04-25 21:58:42 -0600414/* Don't allow PCI region 3 to use memory in the 2-4GB memory hole */
415static void setup_pci_ram_top(void)
416{
Simon Glass16a624b2017-01-16 07:03:57 -0700417 gd->pci_ram_top = 0x80000000U;
Simon Glass05e12f72019-04-25 21:58:42 -0600418}
419
420static void setup_mtrr(void)
421{
422 u64 mtrr_cap;
Simon Glass16a624b2017-01-16 07:03:57 -0700423
424 /* Configure fixed range MTRRs for some legacy regions */
Simon Glass05e12f72019-04-25 21:58:42 -0600425 if (!gd->arch.has_mtrr)
426 return;
Simon Glass16a624b2017-01-16 07:03:57 -0700427
Simon Glass05e12f72019-04-25 21:58:42 -0600428 mtrr_cap = native_read_msr(MTRR_CAP_MSR);
429 if (mtrr_cap & MTRR_CAP_FIX) {
430 /* Mark the VGA RAM area as uncacheable */
431 native_write_msr(MTRR_FIX_16K_A0000_MSR,
432 MTRR_FIX_TYPE(MTRR_TYPE_UNCACHEABLE),
433 MTRR_FIX_TYPE(MTRR_TYPE_UNCACHEABLE));
Simon Glass16a624b2017-01-16 07:03:57 -0700434
Simon Glass05e12f72019-04-25 21:58:42 -0600435 /*
436 * Mark the PCI ROM area as cacheable to improve ROM
437 * execution performance.
438 */
439 native_write_msr(MTRR_FIX_4K_C0000_MSR,
440 MTRR_FIX_TYPE(MTRR_TYPE_WRBACK),
441 MTRR_FIX_TYPE(MTRR_TYPE_WRBACK));
442 native_write_msr(MTRR_FIX_4K_C8000_MSR,
443 MTRR_FIX_TYPE(MTRR_TYPE_WRBACK),
444 MTRR_FIX_TYPE(MTRR_TYPE_WRBACK));
445 native_write_msr(MTRR_FIX_4K_D0000_MSR,
446 MTRR_FIX_TYPE(MTRR_TYPE_WRBACK),
447 MTRR_FIX_TYPE(MTRR_TYPE_WRBACK));
448 native_write_msr(MTRR_FIX_4K_D8000_MSR,
449 MTRR_FIX_TYPE(MTRR_TYPE_WRBACK),
450 MTRR_FIX_TYPE(MTRR_TYPE_WRBACK));
Simon Glass16a624b2017-01-16 07:03:57 -0700451
Simon Glass05e12f72019-04-25 21:58:42 -0600452 /* Enable the fixed range MTRRs */
453 msr_setbits_64(MTRR_DEF_TYPE_MSR, MTRR_DEF_TYPE_FIX_EN);
Simon Glass16a624b2017-01-16 07:03:57 -0700454 }
Simon Glass05e12f72019-04-25 21:58:42 -0600455}
Simon Glass16a624b2017-01-16 07:03:57 -0700456
Simon Glassdc444672019-10-20 21:37:54 -0600457int x86_cpu_init_tpl(void)
458{
459 setup_cpu_features();
460 setup_identity();
461
462 return 0;
463}
464
Simon Glass05e12f72019-04-25 21:58:42 -0600465int x86_cpu_init_f(void)
466{
467 if (ll_boot_init())
468 setup_cpu_features();
469 setup_identity();
470 setup_mtrr();
471 setup_pci_ram_top();
472
Simon Glass16a624b2017-01-16 07:03:57 -0700473 /* Set up the i8254 timer if required */
Simon Glass05e12f72019-04-25 21:58:42 -0600474 if (IS_ENABLED(CONFIG_I8254_TIMER))
475 i8254_init();
476
477 return 0;
478}
479
480int x86_cpu_reinit_f(void)
481{
Simon Glassfb8736d2020-07-16 21:22:34 -0600482 long addr;
483
Simon Glass05e12f72019-04-25 21:58:42 -0600484 setup_identity();
485 setup_pci_ram_top();
Simon Glassfb8736d2020-07-16 21:22:34 -0600486 addr = locate_coreboot_table();
487 if (addr >= 0) {
488 gd->arch.coreboot_table = addr;
Simon Glass6bd98e02020-04-26 09:12:59 -0600489 gd->flags |= GD_FLG_SKIP_LL_INIT;
Simon Glassfb8736d2020-07-16 21:22:34 -0600490 }
Simon Glass16a624b2017-01-16 07:03:57 -0700491
492 return 0;
493}
494
495void x86_enable_caches(void)
496{
497 unsigned long cr0;
498
499 cr0 = read_cr0();
500 cr0 &= ~(X86_CR0_NW | X86_CR0_CD);
501 write_cr0(cr0);
502 wbinvd();
503}
504void enable_caches(void) __attribute__((weak, alias("x86_enable_caches")));
505
506void x86_disable_caches(void)
507{
508 unsigned long cr0;
509
510 cr0 = read_cr0();
511 cr0 |= X86_CR0_NW | X86_CR0_CD;
512 wbinvd();
513 write_cr0(cr0);
514 wbinvd();
515}
516void disable_caches(void) __attribute__((weak, alias("x86_disable_caches")));
517
518int dcache_status(void)
519{
520 return !(read_cr0() & X86_CR0_CD);
521}
522
523void cpu_enable_paging_pae(ulong cr3)
524{
525 __asm__ __volatile__(
526 /* Load the page table address */
527 "movl %0, %%cr3\n"
528 /* Enable pae */
529 "movl %%cr4, %%eax\n"
530 "orl $0x00000020, %%eax\n"
531 "movl %%eax, %%cr4\n"
532 /* Enable paging */
533 "movl %%cr0, %%eax\n"
534 "orl $0x80000000, %%eax\n"
535 "movl %%eax, %%cr0\n"
536 :
537 : "r" (cr3)
538 : "eax");
539}
540
541void cpu_disable_paging_pae(void)
542{
543 /* Turn off paging */
544 __asm__ __volatile__ (
545 /* Disable paging */
546 "movl %%cr0, %%eax\n"
547 "andl $0x7fffffff, %%eax\n"
548 "movl %%eax, %%cr0\n"
549 /* Disable pae */
550 "movl %%cr4, %%eax\n"
551 "andl $0xffffffdf, %%eax\n"
552 "movl %%eax, %%cr4\n"
553 :
554 :
555 : "eax");
556}
557
558static bool can_detect_long_mode(void)
559{
560 return cpuid_eax(0x80000000) > 0x80000000UL;
561}
562
563static bool has_long_mode(void)
564{
565 return cpuid_edx(0x80000001) & (1 << 29) ? true : false;
566}
567
568int cpu_has_64bit(void)
569{
570 return has_cpuid() && can_detect_long_mode() &&
571 has_long_mode();
572}
573
Bin Meng6aac2ca2019-01-31 08:22:12 -0800574#define PAGETABLE_BASE 0x80000
Simon Glass16a624b2017-01-16 07:03:57 -0700575#define PAGETABLE_SIZE (6 * 4096)
576
577/**
578 * build_pagetable() - build a flat 4GiB page table structure for 64-bti mode
579 *
580 * @pgtable: Pointer to a 24iKB block of memory
581 */
582static void build_pagetable(uint32_t *pgtable)
583{
584 uint i;
585
586 memset(pgtable, '\0', PAGETABLE_SIZE);
587
588 /* Level 4 needs a single entry */
589 pgtable[0] = (ulong)&pgtable[1024] + 7;
590
591 /* Level 3 has one 64-bit entry for each GiB of memory */
592 for (i = 0; i < 4; i++)
593 pgtable[1024 + i * 2] = (ulong)&pgtable[2048] + 0x1000 * i + 7;
594
595 /* Level 2 has 2048 64-bit entries, each repesenting 2MiB */
596 for (i = 0; i < 2048; i++)
597 pgtable[2048 + i * 2] = 0x183 + (i << 21UL);
598}
599
600int cpu_jump_to_64bit(ulong setup_base, ulong target)
601{
602 uint32_t *pgtable;
603
604 pgtable = memalign(4096, PAGETABLE_SIZE);
605 if (!pgtable)
606 return -ENOMEM;
607
608 build_pagetable(pgtable);
609 cpu_call64((ulong)pgtable, setup_base, target);
610 free(pgtable);
611
612 return -EFAULT;
613}
614
Simon Glass1e32ede2017-01-16 07:04:15 -0700615/*
616 * Jump from SPL to U-Boot
617 *
618 * This function is work-in-progress with many issues to resolve.
619 *
620 * It works by setting up several regions:
621 * ptr - a place to put the code that jumps into 64-bit mode
622 * gdt - a place to put the global descriptor table
623 * pgtable - a place to put the page tables
624 *
625 * The cpu_call64() code is copied from ROM and then manually patched so that
626 * it has the correct GDT address in RAM. U-Boot is copied from ROM into
627 * its pre-relocation address. Then we jump to the cpu_call64() code in RAM,
628 * which changes to 64-bit mode and starts U-Boot.
629 */
630int cpu_jump_to_64bit_uboot(ulong target)
631{
632 typedef void (*func_t)(ulong pgtable, ulong setup_base, ulong target);
633 uint32_t *pgtable;
634 func_t func;
Bin Meng17b7b602019-01-31 08:22:13 -0800635 char *ptr;
Simon Glass1e32ede2017-01-16 07:04:15 -0700636
Bin Meng6aac2ca2019-01-31 08:22:12 -0800637 pgtable = (uint32_t *)PAGETABLE_BASE;
Simon Glass1e32ede2017-01-16 07:04:15 -0700638
639 build_pagetable(pgtable);
640
Bin Meng17b7b602019-01-31 08:22:13 -0800641 extern long call64_stub_size;
642 ptr = malloc(call64_stub_size);
643 if (!ptr) {
644 printf("Failed to allocate the cpu_call64 stub\n");
645 return -ENOMEM;
646 }
Bin Meng17b7b602019-01-31 08:22:13 -0800647 memcpy(ptr, cpu_call64, call64_stub_size);
Simon Glass1e32ede2017-01-16 07:04:15 -0700648
Simon Glass1e32ede2017-01-16 07:04:15 -0700649 func = (func_t)ptr;
Simon Glass1e32ede2017-01-16 07:04:15 -0700650
Simon Glass1e32ede2017-01-16 07:04:15 -0700651 /* Jump to U-Boot */
652 func((ulong)pgtable, 0, (ulong)target);
653
654 return -EFAULT;
655}
656
Simon Glass16a624b2017-01-16 07:03:57 -0700657int x86_mp_init(void)
658{
Simon Glasse40633d2020-07-17 08:48:08 -0600659 int ret;
Simon Glass16a624b2017-01-16 07:03:57 -0700660
Simon Glasse40633d2020-07-17 08:48:08 -0600661 ret = mp_init();
662 if (ret) {
Simon Glass16a624b2017-01-16 07:03:57 -0700663 printf("Warning: MP init failure\n");
Simon Glasse40633d2020-07-17 08:48:08 -0600664 return log_ret(ret);
Simon Glass16a624b2017-01-16 07:03:57 -0700665 }
666
667 return 0;
668}