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