blob: a6a6afec8cca529cccc42dfa228d2b8f0cfadf5a [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
21#include <common.h>
Simon Glass9edefc22019-11-14 12:57:37 -070022#include <cpu_func.h>
Simon Glass35a3f872019-12-28 10:44:56 -070023#include <init.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>
29#include <asm/mp.h>
30#include <asm/msr.h>
31#include <asm/mtrr.h>
32#include <asm/processor-flags.h>
33
34DECLARE_GLOBAL_DATA_PTR;
35
36/*
37 * Constructor for a conventional segment GDT (or LDT) entry
38 * This is a macro so it can be used in initialisers
39 */
40#define GDT_ENTRY(flags, base, limit) \
41 ((((base) & 0xff000000ULL) << (56-24)) | \
42 (((flags) & 0x0000f0ffULL) << 40) | \
43 (((limit) & 0x000f0000ULL) << (48-16)) | \
44 (((base) & 0x00ffffffULL) << 16) | \
45 (((limit) & 0x0000ffffULL)))
46
47struct gdt_ptr {
48 u16 len;
49 u32 ptr;
50} __packed;
51
52struct cpu_device_id {
53 unsigned vendor;
54 unsigned device;
55};
56
57struct cpuinfo_x86 {
58 uint8_t x86; /* CPU family */
59 uint8_t x86_vendor; /* CPU vendor */
60 uint8_t x86_model;
61 uint8_t x86_mask;
62};
63
Simon Glasscaca13f2019-12-06 21:41:51 -070064/* gcc 7.3 does not wwant to drop x86_vendors, so use #ifdef */
65#ifndef CONFIG_TPL_BUILD
Simon Glassbe059e82017-01-16 07:03:57 -070066/*
67 * List of cpu vendor strings along with their normalized
68 * id values.
69 */
70static const struct {
71 int vendor;
72 const char *name;
73} x86_vendors[] = {
74 { X86_VENDOR_INTEL, "GenuineIntel", },
75 { X86_VENDOR_CYRIX, "CyrixInstead", },
76 { X86_VENDOR_AMD, "AuthenticAMD", },
77 { X86_VENDOR_UMC, "UMC UMC UMC ", },
78 { X86_VENDOR_NEXGEN, "NexGenDriven", },
79 { X86_VENDOR_CENTAUR, "CentaurHauls", },
80 { X86_VENDOR_RISE, "RiseRiseRise", },
81 { X86_VENDOR_TRANSMETA, "GenuineTMx86", },
82 { X86_VENDOR_TRANSMETA, "TransmetaCPU", },
83 { X86_VENDOR_NSC, "Geode by NSC", },
84 { X86_VENDOR_SIS, "SiS SiS SiS ", },
85};
Simon Glasscaca13f2019-12-06 21:41:51 -070086#endif
Simon Glassbe059e82017-01-16 07:03:57 -070087
88static void load_ds(u32 segment)
89{
90 asm volatile("movl %0, %%ds" : : "r" (segment * X86_GDT_ENTRY_SIZE));
91}
92
93static void load_es(u32 segment)
94{
95 asm volatile("movl %0, %%es" : : "r" (segment * X86_GDT_ENTRY_SIZE));
96}
97
98static void load_fs(u32 segment)
99{
100 asm volatile("movl %0, %%fs" : : "r" (segment * X86_GDT_ENTRY_SIZE));
101}
102
103static void load_gs(u32 segment)
104{
105 asm volatile("movl %0, %%gs" : : "r" (segment * X86_GDT_ENTRY_SIZE));
106}
107
108static void load_ss(u32 segment)
109{
110 asm volatile("movl %0, %%ss" : : "r" (segment * X86_GDT_ENTRY_SIZE));
111}
112
113static void load_gdt(const u64 *boot_gdt, u16 num_entries)
114{
115 struct gdt_ptr gdt;
116
117 gdt.len = (num_entries * X86_GDT_ENTRY_SIZE) - 1;
118 gdt.ptr = (ulong)boot_gdt;
119
120 asm volatile("lgdtl %0\n" : : "m" (gdt));
121}
122
123void arch_setup_gd(gd_t *new_gd)
124{
125 u64 *gdt_addr;
126
127 gdt_addr = new_gd->arch.gdt;
128
129 /*
130 * CS: code, read/execute, 4 GB, base 0
131 *
132 * Some OS (like VxWorks) requires GDT entry 1 to be the 32-bit CS
133 */
134 gdt_addr[X86_GDT_ENTRY_UNUSED] = GDT_ENTRY(0xc09b, 0, 0xfffff);
135 gdt_addr[X86_GDT_ENTRY_32BIT_CS] = GDT_ENTRY(0xc09b, 0, 0xfffff);
136
137 /* DS: data, read/write, 4 GB, base 0 */
138 gdt_addr[X86_GDT_ENTRY_32BIT_DS] = GDT_ENTRY(0xc093, 0, 0xfffff);
139
Masahiro Yamada2fa863e2020-01-08 20:13:42 +0900140 /*
141 * FS: data, read/write, sizeof (Global Data Pointer),
142 * base (Global Data Pointer)
143 */
Simon Glassbe059e82017-01-16 07:03:57 -0700144 new_gd->arch.gd_addr = new_gd;
Masahiro Yamada2fa863e2020-01-08 20:13:42 +0900145 gdt_addr[X86_GDT_ENTRY_32BIT_FS] = GDT_ENTRY(0x8093,
146 (ulong)&new_gd->arch.gd_addr,
147 sizeof(new_gd->arch.gd_addr) - 1);
Simon Glassbe059e82017-01-16 07:03:57 -0700148
149 /* 16-bit CS: code, read/execute, 64 kB, base 0 */
150 gdt_addr[X86_GDT_ENTRY_16BIT_CS] = GDT_ENTRY(0x009b, 0, 0x0ffff);
151
152 /* 16-bit DS: data, read/write, 64 kB, base 0 */
153 gdt_addr[X86_GDT_ENTRY_16BIT_DS] = GDT_ENTRY(0x0093, 0, 0x0ffff);
154
155 gdt_addr[X86_GDT_ENTRY_16BIT_FLAT_CS] = GDT_ENTRY(0x809b, 0, 0xfffff);
156 gdt_addr[X86_GDT_ENTRY_16BIT_FLAT_DS] = GDT_ENTRY(0x8093, 0, 0xfffff);
157
158 load_gdt(gdt_addr, X86_GDT_NUM_ENTRIES);
159 load_ds(X86_GDT_ENTRY_32BIT_DS);
160 load_es(X86_GDT_ENTRY_32BIT_DS);
161 load_gs(X86_GDT_ENTRY_32BIT_DS);
162 load_ss(X86_GDT_ENTRY_32BIT_DS);
163 load_fs(X86_GDT_ENTRY_32BIT_FS);
164}
165
166#ifdef CONFIG_HAVE_FSP
167/*
168 * Setup FSP execution environment GDT
169 *
170 * Per Intel FSP external architecture specification, before calling any FSP
171 * APIs, we need make sure the system is in flat 32-bit mode and both the code
172 * and data selectors should have full 4GB access range. Here we reuse the one
173 * we used in arch/x86/cpu/start16.S, and reload the segement registers.
174 */
175void setup_fsp_gdt(void)
176{
177 load_gdt((const u64 *)(gdt_rom + CONFIG_RESET_SEG_START), 4);
178 load_ds(X86_GDT_ENTRY_32BIT_DS);
179 load_ss(X86_GDT_ENTRY_32BIT_DS);
180 load_es(X86_GDT_ENTRY_32BIT_DS);
181 load_fs(X86_GDT_ENTRY_32BIT_DS);
182 load_gs(X86_GDT_ENTRY_32BIT_DS);
183}
184#endif
185
186/*
187 * Cyrix CPUs without cpuid or with cpuid not yet enabled can be detected
188 * by the fact that they preserve the flags across the division of 5/2.
189 * PII and PPro exhibit this behavior too, but they have cpuid available.
190 */
191
192/*
193 * Perform the Cyrix 5/2 test. A Cyrix won't change
194 * the flags, while other 486 chips will.
195 */
196static inline int test_cyrix_52div(void)
197{
198 unsigned int test;
199
200 __asm__ __volatile__(
201 "sahf\n\t" /* clear flags (%eax = 0x0005) */
202 "div %b2\n\t" /* divide 5 by 2 */
203 "lahf" /* store flags into %ah */
204 : "=a" (test)
205 : "0" (5), "q" (2)
206 : "cc");
207
208 /* AH is 0x02 on Cyrix after the divide.. */
209 return (unsigned char) (test >> 8) == 0x02;
210}
211
Simon Glasscaca13f2019-12-06 21:41:51 -0700212#ifndef CONFIG_TPL_BUILD
Simon Glassbe059e82017-01-16 07:03:57 -0700213/*
214 * Detect a NexGen CPU running without BIOS hypercode new enough
215 * to have CPUID. (Thanks to Herbert Oppmann)
216 */
217static int deep_magic_nexgen_probe(void)
218{
219 int ret;
220
221 __asm__ __volatile__ (
222 " movw $0x5555, %%ax\n"
223 " xorw %%dx,%%dx\n"
224 " movw $2, %%cx\n"
225 " divw %%cx\n"
226 " movl $0, %%eax\n"
227 " jnz 1f\n"
228 " movl $1, %%eax\n"
229 "1:\n"
230 : "=a" (ret) : : "cx", "dx");
231 return ret;
232}
Simon Glasscaca13f2019-12-06 21:41:51 -0700233#endif
Simon Glassbe059e82017-01-16 07:03:57 -0700234
235static bool has_cpuid(void)
236{
237 return flag_is_changeable_p(X86_EFLAGS_ID);
238}
239
240static bool has_mtrr(void)
241{
242 return cpuid_edx(0x00000001) & (1 << 12) ? true : false;
243}
244
Simon Glasscaca13f2019-12-06 21:41:51 -0700245#ifndef CONFIG_TPL_BUILD
Simon Glassbe059e82017-01-16 07:03:57 -0700246static int build_vendor_name(char *vendor_name)
247{
248 struct cpuid_result result;
249 result = cpuid(0x00000000);
250 unsigned int *name_as_ints = (unsigned int *)vendor_name;
251
252 name_as_ints[0] = result.ebx;
253 name_as_ints[1] = result.edx;
254 name_as_ints[2] = result.ecx;
255
256 return result.eax;
257}
Simon Glasscaca13f2019-12-06 21:41:51 -0700258#endif
Simon Glassbe059e82017-01-16 07:03:57 -0700259
260static void identify_cpu(struct cpu_device_id *cpu)
261{
Simon Glasscaca13f2019-12-06 21:41:51 -0700262 cpu->device = 0; /* fix gcc 4.4.4 warning */
263
264 /*
265 * Do a quick and dirty check to save space - Intel and AMD only and
266 * just the vendor. This is enough for most TPL code.
267 */
268 if (spl_phase() == PHASE_TPL) {
269 struct cpuid_result result;
270
271 result = cpuid(0x00000000);
272 switch (result.ecx >> 24) {
273 case 'l': /* GenuineIntel */
274 cpu->vendor = X86_VENDOR_INTEL;
275 break;
276 case 'D': /* AuthenticAMD */
277 cpu->vendor = X86_VENDOR_AMD;
278 break;
279 default:
280 cpu->vendor = X86_VENDOR_ANY;
281 break;
282 }
283 return;
284 }
285
286/* gcc 7.3 does not want to drop x86_vendors, so use #ifdef */
287#ifndef CONFIG_TPL_BUILD
Simon Glassbe059e82017-01-16 07:03:57 -0700288 char vendor_name[16];
289 int i;
290
291 vendor_name[0] = '\0'; /* Unset */
Simon Glassbe059e82017-01-16 07:03:57 -0700292
293 /* Find the id and vendor_name */
294 if (!has_cpuid()) {
295 /* Its a 486 if we can modify the AC flag */
296 if (flag_is_changeable_p(X86_EFLAGS_AC))
297 cpu->device = 0x00000400; /* 486 */
298 else
299 cpu->device = 0x00000300; /* 386 */
300 if ((cpu->device == 0x00000400) && test_cyrix_52div()) {
301 memcpy(vendor_name, "CyrixInstead", 13);
302 /* If we ever care we can enable cpuid here */
303 }
304 /* Detect NexGen with old hypercode */
305 else if (deep_magic_nexgen_probe())
306 memcpy(vendor_name, "NexGenDriven", 13);
Simon Glasscaca13f2019-12-06 21:41:51 -0700307 } else {
308 int cpuid_level;
Simon Glassbe059e82017-01-16 07:03:57 -0700309
310 cpuid_level = build_vendor_name(vendor_name);
311 vendor_name[12] = '\0';
312
313 /* Intel-defined flags: level 0x00000001 */
314 if (cpuid_level >= 0x00000001) {
315 cpu->device = cpuid_eax(0x00000001);
316 } else {
317 /* Have CPUID level 0 only unheard of */
318 cpu->device = 0x00000400;
319 }
320 }
321 cpu->vendor = X86_VENDOR_UNKNOWN;
322 for (i = 0; i < ARRAY_SIZE(x86_vendors); i++) {
323 if (memcmp(vendor_name, x86_vendors[i].name, 12) == 0) {
324 cpu->vendor = x86_vendors[i].vendor;
325 break;
326 }
327 }
Simon Glasscaca13f2019-12-06 21:41:51 -0700328#endif
Simon Glassbe059e82017-01-16 07:03:57 -0700329}
330
331static inline void get_fms(struct cpuinfo_x86 *c, uint32_t tfms)
332{
333 c->x86 = (tfms >> 8) & 0xf;
334 c->x86_model = (tfms >> 4) & 0xf;
335 c->x86_mask = tfms & 0xf;
336 if (c->x86 == 0xf)
337 c->x86 += (tfms >> 20) & 0xff;
338 if (c->x86 >= 0x6)
339 c->x86_model += ((tfms >> 16) & 0xF) << 4;
340}
341
342u32 cpu_get_family_model(void)
343{
344 return gd->arch.x86_device & 0x0fff0ff0;
345}
346
347u32 cpu_get_stepping(void)
348{
349 return gd->arch.x86_mask;
350}
351
Simon Glassc0069e92019-04-25 21:58:42 -0600352/* initialise FPU, reset EM, set MP and NE */
353static void setup_cpu_features(void)
Simon Glassbe059e82017-01-16 07:03:57 -0700354{
355 const u32 em_rst = ~X86_CR0_EM;
356 const u32 mp_ne_set = X86_CR0_MP | X86_CR0_NE;
357
Simon Glassc0069e92019-04-25 21:58:42 -0600358 asm ("fninit\n" \
359 "movl %%cr0, %%eax\n" \
360 "andl %0, %%eax\n" \
361 "orl %1, %%eax\n" \
362 "movl %%eax, %%cr0\n" \
363 : : "i" (em_rst), "i" (mp_ne_set) : "eax");
364}
Simon Glassbe059e82017-01-16 07:03:57 -0700365
Simon Glass3dada5a2020-07-02 21:12:12 -0600366void cpu_reinit_fpu(void)
367{
368 asm ("fninit\n");
369}
370
Simon Glassc0069e92019-04-25 21:58:42 -0600371static void setup_identity(void)
372{
Simon Glassbe059e82017-01-16 07:03:57 -0700373 /* identify CPU via cpuid and store the decoded info into gd->arch */
374 if (has_cpuid()) {
375 struct cpu_device_id cpu;
376 struct cpuinfo_x86 c;
377
378 identify_cpu(&cpu);
379 get_fms(&c, cpu.device);
380 gd->arch.x86 = c.x86;
381 gd->arch.x86_vendor = cpu.vendor;
382 gd->arch.x86_model = c.x86_model;
383 gd->arch.x86_mask = c.x86_mask;
384 gd->arch.x86_device = cpu.device;
385
386 gd->arch.has_mtrr = has_mtrr();
387 }
Simon Glassc0069e92019-04-25 21:58:42 -0600388}
389
390/* Don't allow PCI region 3 to use memory in the 2-4GB memory hole */
391static void setup_pci_ram_top(void)
392{
Simon Glassbe059e82017-01-16 07:03:57 -0700393 gd->pci_ram_top = 0x80000000U;
Simon Glassc0069e92019-04-25 21:58:42 -0600394}
395
396static void setup_mtrr(void)
397{
398 u64 mtrr_cap;
Simon Glassbe059e82017-01-16 07:03:57 -0700399
400 /* Configure fixed range MTRRs for some legacy regions */
Simon Glassc0069e92019-04-25 21:58:42 -0600401 if (!gd->arch.has_mtrr)
402 return;
Simon Glassbe059e82017-01-16 07:03:57 -0700403
Simon Glassc0069e92019-04-25 21:58:42 -0600404 mtrr_cap = native_read_msr(MTRR_CAP_MSR);
405 if (mtrr_cap & MTRR_CAP_FIX) {
406 /* Mark the VGA RAM area as uncacheable */
407 native_write_msr(MTRR_FIX_16K_A0000_MSR,
408 MTRR_FIX_TYPE(MTRR_TYPE_UNCACHEABLE),
409 MTRR_FIX_TYPE(MTRR_TYPE_UNCACHEABLE));
Simon Glassbe059e82017-01-16 07:03:57 -0700410
Simon Glassc0069e92019-04-25 21:58:42 -0600411 /*
412 * Mark the PCI ROM area as cacheable to improve ROM
413 * execution performance.
414 */
415 native_write_msr(MTRR_FIX_4K_C0000_MSR,
416 MTRR_FIX_TYPE(MTRR_TYPE_WRBACK),
417 MTRR_FIX_TYPE(MTRR_TYPE_WRBACK));
418 native_write_msr(MTRR_FIX_4K_C8000_MSR,
419 MTRR_FIX_TYPE(MTRR_TYPE_WRBACK),
420 MTRR_FIX_TYPE(MTRR_TYPE_WRBACK));
421 native_write_msr(MTRR_FIX_4K_D0000_MSR,
422 MTRR_FIX_TYPE(MTRR_TYPE_WRBACK),
423 MTRR_FIX_TYPE(MTRR_TYPE_WRBACK));
424 native_write_msr(MTRR_FIX_4K_D8000_MSR,
425 MTRR_FIX_TYPE(MTRR_TYPE_WRBACK),
426 MTRR_FIX_TYPE(MTRR_TYPE_WRBACK));
Simon Glassbe059e82017-01-16 07:03:57 -0700427
Simon Glassc0069e92019-04-25 21:58:42 -0600428 /* Enable the fixed range MTRRs */
429 msr_setbits_64(MTRR_DEF_TYPE_MSR, MTRR_DEF_TYPE_FIX_EN);
Simon Glassbe059e82017-01-16 07:03:57 -0700430 }
Simon Glassc0069e92019-04-25 21:58:42 -0600431}
Simon Glassbe059e82017-01-16 07:03:57 -0700432
Simon Glassece3a462019-10-20 21:37:54 -0600433int x86_cpu_init_tpl(void)
434{
435 setup_cpu_features();
436 setup_identity();
437
438 return 0;
439}
440
Simon Glassc0069e92019-04-25 21:58:42 -0600441int x86_cpu_init_f(void)
442{
443 if (ll_boot_init())
444 setup_cpu_features();
445 setup_identity();
446 setup_mtrr();
447 setup_pci_ram_top();
448
Simon Glassbe059e82017-01-16 07:03:57 -0700449 /* Set up the i8254 timer if required */
Simon Glassc0069e92019-04-25 21:58:42 -0600450 if (IS_ENABLED(CONFIG_I8254_TIMER))
451 i8254_init();
452
453 return 0;
454}
455
456int x86_cpu_reinit_f(void)
457{
Simon Glass9ef16862020-07-16 21:22:34 -0600458 long addr;
459
Simon Glassc0069e92019-04-25 21:58:42 -0600460 setup_identity();
461 setup_pci_ram_top();
Simon Glass9ef16862020-07-16 21:22:34 -0600462 addr = locate_coreboot_table();
463 if (addr >= 0) {
464 gd->arch.coreboot_table = addr;
Simon Glasscfe7a102020-04-26 09:12:59 -0600465 gd->flags |= GD_FLG_SKIP_LL_INIT;
Simon Glass9ef16862020-07-16 21:22:34 -0600466 }
Simon Glassbe059e82017-01-16 07:03:57 -0700467
468 return 0;
469}
470
471void x86_enable_caches(void)
472{
473 unsigned long cr0;
474
475 cr0 = read_cr0();
476 cr0 &= ~(X86_CR0_NW | X86_CR0_CD);
477 write_cr0(cr0);
478 wbinvd();
479}
480void enable_caches(void) __attribute__((weak, alias("x86_enable_caches")));
481
482void x86_disable_caches(void)
483{
484 unsigned long cr0;
485
486 cr0 = read_cr0();
487 cr0 |= X86_CR0_NW | X86_CR0_CD;
488 wbinvd();
489 write_cr0(cr0);
490 wbinvd();
491}
492void disable_caches(void) __attribute__((weak, alias("x86_disable_caches")));
493
494int dcache_status(void)
495{
496 return !(read_cr0() & X86_CR0_CD);
497}
498
499void cpu_enable_paging_pae(ulong cr3)
500{
501 __asm__ __volatile__(
502 /* Load the page table address */
503 "movl %0, %%cr3\n"
504 /* Enable pae */
505 "movl %%cr4, %%eax\n"
506 "orl $0x00000020, %%eax\n"
507 "movl %%eax, %%cr4\n"
508 /* Enable paging */
509 "movl %%cr0, %%eax\n"
510 "orl $0x80000000, %%eax\n"
511 "movl %%eax, %%cr0\n"
512 :
513 : "r" (cr3)
514 : "eax");
515}
516
517void cpu_disable_paging_pae(void)
518{
519 /* Turn off paging */
520 __asm__ __volatile__ (
521 /* Disable paging */
522 "movl %%cr0, %%eax\n"
523 "andl $0x7fffffff, %%eax\n"
524 "movl %%eax, %%cr0\n"
525 /* Disable pae */
526 "movl %%cr4, %%eax\n"
527 "andl $0xffffffdf, %%eax\n"
528 "movl %%eax, %%cr4\n"
529 :
530 :
531 : "eax");
532}
533
534static bool can_detect_long_mode(void)
535{
536 return cpuid_eax(0x80000000) > 0x80000000UL;
537}
538
539static bool has_long_mode(void)
540{
541 return cpuid_edx(0x80000001) & (1 << 29) ? true : false;
542}
543
544int cpu_has_64bit(void)
545{
546 return has_cpuid() && can_detect_long_mode() &&
547 has_long_mode();
548}
549
Bin Mengdbb06962019-01-31 08:22:12 -0800550#define PAGETABLE_BASE 0x80000
Simon Glassbe059e82017-01-16 07:03:57 -0700551#define PAGETABLE_SIZE (6 * 4096)
552
553/**
554 * build_pagetable() - build a flat 4GiB page table structure for 64-bti mode
555 *
556 * @pgtable: Pointer to a 24iKB block of memory
557 */
558static void build_pagetable(uint32_t *pgtable)
559{
560 uint i;
561
562 memset(pgtable, '\0', PAGETABLE_SIZE);
563
564 /* Level 4 needs a single entry */
565 pgtable[0] = (ulong)&pgtable[1024] + 7;
566
567 /* Level 3 has one 64-bit entry for each GiB of memory */
568 for (i = 0; i < 4; i++)
569 pgtable[1024 + i * 2] = (ulong)&pgtable[2048] + 0x1000 * i + 7;
570
571 /* Level 2 has 2048 64-bit entries, each repesenting 2MiB */
572 for (i = 0; i < 2048; i++)
573 pgtable[2048 + i * 2] = 0x183 + (i << 21UL);
574}
575
576int cpu_jump_to_64bit(ulong setup_base, ulong target)
577{
578 uint32_t *pgtable;
579
580 pgtable = memalign(4096, PAGETABLE_SIZE);
581 if (!pgtable)
582 return -ENOMEM;
583
584 build_pagetable(pgtable);
585 cpu_call64((ulong)pgtable, setup_base, target);
586 free(pgtable);
587
588 return -EFAULT;
589}
590
Simon Glassfa5fcb32017-01-16 07:04:15 -0700591/*
592 * Jump from SPL to U-Boot
593 *
594 * This function is work-in-progress with many issues to resolve.
595 *
596 * It works by setting up several regions:
597 * ptr - a place to put the code that jumps into 64-bit mode
598 * gdt - a place to put the global descriptor table
599 * pgtable - a place to put the page tables
600 *
601 * The cpu_call64() code is copied from ROM and then manually patched so that
602 * it has the correct GDT address in RAM. U-Boot is copied from ROM into
603 * its pre-relocation address. Then we jump to the cpu_call64() code in RAM,
604 * which changes to 64-bit mode and starts U-Boot.
605 */
606int cpu_jump_to_64bit_uboot(ulong target)
607{
608 typedef void (*func_t)(ulong pgtable, ulong setup_base, ulong target);
609 uint32_t *pgtable;
610 func_t func;
Bin Meng91683262019-01-31 08:22:13 -0800611 char *ptr;
Simon Glassfa5fcb32017-01-16 07:04:15 -0700612
Bin Mengdbb06962019-01-31 08:22:12 -0800613 pgtable = (uint32_t *)PAGETABLE_BASE;
Simon Glassfa5fcb32017-01-16 07:04:15 -0700614
615 build_pagetable(pgtable);
616
Bin Meng91683262019-01-31 08:22:13 -0800617 extern long call64_stub_size;
618 ptr = malloc(call64_stub_size);
619 if (!ptr) {
620 printf("Failed to allocate the cpu_call64 stub\n");
621 return -ENOMEM;
622 }
Bin Meng91683262019-01-31 08:22:13 -0800623 memcpy(ptr, cpu_call64, call64_stub_size);
Simon Glassfa5fcb32017-01-16 07:04:15 -0700624
Simon Glassfa5fcb32017-01-16 07:04:15 -0700625 func = (func_t)ptr;
Simon Glassfa5fcb32017-01-16 07:04:15 -0700626
Simon Glassfa5fcb32017-01-16 07:04:15 -0700627 /* Jump to U-Boot */
628 func((ulong)pgtable, 0, (ulong)target);
629
630 return -EFAULT;
631}
632
Simon Glassbe059e82017-01-16 07:03:57 -0700633#ifdef CONFIG_SMP
634static int enable_smis(struct udevice *cpu, void *unused)
635{
636 return 0;
637}
638
639static struct mp_flight_record mp_steps[] = {
640 MP_FR_BLOCK_APS(mp_init_cpu, NULL, mp_init_cpu, NULL),
641 /* Wait for APs to finish initialization before proceeding */
642 MP_FR_BLOCK_APS(NULL, NULL, enable_smis, NULL),
643};
644
645int x86_mp_init(void)
646{
647 struct mp_params mp_params;
648
649 mp_params.parallel_microcode_load = 0,
650 mp_params.flight_plan = &mp_steps[0];
651 mp_params.num_records = ARRAY_SIZE(mp_steps);
652 mp_params.microcode_pointer = 0;
653
654 if (mp_init(&mp_params)) {
655 printf("Warning: MP init failure\n");
656 return -EIO;
657 }
658
659 return 0;
660}
661#endif