1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2008-2011
4 * Graeme Russ, <graeme.russ@gmail.com>
7 * Daniel Engström, Omicron Ceti AB, <daniel@omicron.se>
10 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
11 * Marius Groeger <mgroeger@sysgo.de>
14 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
15 * Alex Zuepke <azu@sysgo.de>
17 * Part of this file is adapted from coreboot
18 * src/arch/x86/lib/cpu.c
25 #include <asm/control_regs.h>
30 #include <asm/processor-flags.h>
32 DECLARE_GLOBAL_DATA_PTR;
35 * Constructor for a conventional segment GDT (or LDT) entry
36 * This is a macro so it can be used in initialisers
38 #define GDT_ENTRY(flags, base, limit) \
39 ((((base) & 0xff000000ULL) << (56-24)) | \
40 (((flags) & 0x0000f0ffULL) << 40) | \
41 (((limit) & 0x000f0000ULL) << (48-16)) | \
42 (((base) & 0x00ffffffULL) << 16) | \
43 (((limit) & 0x0000ffffULL)))
50 struct cpu_device_id {
56 uint8_t x86; /* CPU family */
57 uint8_t x86_vendor; /* CPU vendor */
62 /* gcc 7.3 does not wwant to drop x86_vendors, so use #ifdef */
63 #ifndef CONFIG_TPL_BUILD
65 * List of cpu vendor strings along with their normalized
72 { X86_VENDOR_INTEL, "GenuineIntel", },
73 { X86_VENDOR_CYRIX, "CyrixInstead", },
74 { X86_VENDOR_AMD, "AuthenticAMD", },
75 { X86_VENDOR_UMC, "UMC UMC UMC ", },
76 { X86_VENDOR_NEXGEN, "NexGenDriven", },
77 { X86_VENDOR_CENTAUR, "CentaurHauls", },
78 { X86_VENDOR_RISE, "RiseRiseRise", },
79 { X86_VENDOR_TRANSMETA, "GenuineTMx86", },
80 { X86_VENDOR_TRANSMETA, "TransmetaCPU", },
81 { X86_VENDOR_NSC, "Geode by NSC", },
82 { X86_VENDOR_SIS, "SiS SiS SiS ", },
86 static void load_ds(u32 segment)
88 asm volatile("movl %0, %%ds" : : "r" (segment * X86_GDT_ENTRY_SIZE));
91 static void load_es(u32 segment)
93 asm volatile("movl %0, %%es" : : "r" (segment * X86_GDT_ENTRY_SIZE));
96 static void load_fs(u32 segment)
98 asm volatile("movl %0, %%fs" : : "r" (segment * X86_GDT_ENTRY_SIZE));
101 static void load_gs(u32 segment)
103 asm volatile("movl %0, %%gs" : : "r" (segment * X86_GDT_ENTRY_SIZE));
106 static void load_ss(u32 segment)
108 asm volatile("movl %0, %%ss" : : "r" (segment * X86_GDT_ENTRY_SIZE));
111 static void load_gdt(const u64 *boot_gdt, u16 num_entries)
115 gdt.len = (num_entries * X86_GDT_ENTRY_SIZE) - 1;
116 gdt.ptr = (ulong)boot_gdt;
118 asm volatile("lgdtl %0\n" : : "m" (gdt));
121 void arch_setup_gd(gd_t *new_gd)
125 gdt_addr = new_gd->arch.gdt;
128 * CS: code, read/execute, 4 GB, base 0
130 * Some OS (like VxWorks) requires GDT entry 1 to be the 32-bit CS
132 gdt_addr[X86_GDT_ENTRY_UNUSED] = GDT_ENTRY(0xc09b, 0, 0xfffff);
133 gdt_addr[X86_GDT_ENTRY_32BIT_CS] = GDT_ENTRY(0xc09b, 0, 0xfffff);
135 /* DS: data, read/write, 4 GB, base 0 */
136 gdt_addr[X86_GDT_ENTRY_32BIT_DS] = GDT_ENTRY(0xc093, 0, 0xfffff);
138 /* FS: data, read/write, 4 GB, base (Global Data Pointer) */
139 new_gd->arch.gd_addr = new_gd;
140 gdt_addr[X86_GDT_ENTRY_32BIT_FS] = GDT_ENTRY(0xc093,
141 (ulong)&new_gd->arch.gd_addr, 0xfffff);
143 /* 16-bit CS: code, read/execute, 64 kB, base 0 */
144 gdt_addr[X86_GDT_ENTRY_16BIT_CS] = GDT_ENTRY(0x009b, 0, 0x0ffff);
146 /* 16-bit DS: data, read/write, 64 kB, base 0 */
147 gdt_addr[X86_GDT_ENTRY_16BIT_DS] = GDT_ENTRY(0x0093, 0, 0x0ffff);
149 gdt_addr[X86_GDT_ENTRY_16BIT_FLAT_CS] = GDT_ENTRY(0x809b, 0, 0xfffff);
150 gdt_addr[X86_GDT_ENTRY_16BIT_FLAT_DS] = GDT_ENTRY(0x8093, 0, 0xfffff);
152 load_gdt(gdt_addr, X86_GDT_NUM_ENTRIES);
153 load_ds(X86_GDT_ENTRY_32BIT_DS);
154 load_es(X86_GDT_ENTRY_32BIT_DS);
155 load_gs(X86_GDT_ENTRY_32BIT_DS);
156 load_ss(X86_GDT_ENTRY_32BIT_DS);
157 load_fs(X86_GDT_ENTRY_32BIT_FS);
160 #ifdef CONFIG_HAVE_FSP
162 * Setup FSP execution environment GDT
164 * Per Intel FSP external architecture specification, before calling any FSP
165 * APIs, we need make sure the system is in flat 32-bit mode and both the code
166 * and data selectors should have full 4GB access range. Here we reuse the one
167 * we used in arch/x86/cpu/start16.S, and reload the segement registers.
169 void setup_fsp_gdt(void)
171 load_gdt((const u64 *)(gdt_rom + CONFIG_RESET_SEG_START), 4);
172 load_ds(X86_GDT_ENTRY_32BIT_DS);
173 load_ss(X86_GDT_ENTRY_32BIT_DS);
174 load_es(X86_GDT_ENTRY_32BIT_DS);
175 load_fs(X86_GDT_ENTRY_32BIT_DS);
176 load_gs(X86_GDT_ENTRY_32BIT_DS);
181 * Cyrix CPUs without cpuid or with cpuid not yet enabled can be detected
182 * by the fact that they preserve the flags across the division of 5/2.
183 * PII and PPro exhibit this behavior too, but they have cpuid available.
187 * Perform the Cyrix 5/2 test. A Cyrix won't change
188 * the flags, while other 486 chips will.
190 static inline int test_cyrix_52div(void)
194 __asm__ __volatile__(
195 "sahf\n\t" /* clear flags (%eax = 0x0005) */
196 "div %b2\n\t" /* divide 5 by 2 */
197 "lahf" /* store flags into %ah */
202 /* AH is 0x02 on Cyrix after the divide.. */
203 return (unsigned char) (test >> 8) == 0x02;
206 #ifndef CONFIG_TPL_BUILD
208 * Detect a NexGen CPU running without BIOS hypercode new enough
209 * to have CPUID. (Thanks to Herbert Oppmann)
211 static int deep_magic_nexgen_probe(void)
215 __asm__ __volatile__ (
216 " movw $0x5555, %%ax\n"
224 : "=a" (ret) : : "cx", "dx");
229 static bool has_cpuid(void)
231 return flag_is_changeable_p(X86_EFLAGS_ID);
234 static bool has_mtrr(void)
236 return cpuid_edx(0x00000001) & (1 << 12) ? true : false;
239 #ifndef CONFIG_TPL_BUILD
240 static int build_vendor_name(char *vendor_name)
242 struct cpuid_result result;
243 result = cpuid(0x00000000);
244 unsigned int *name_as_ints = (unsigned int *)vendor_name;
246 name_as_ints[0] = result.ebx;
247 name_as_ints[1] = result.edx;
248 name_as_ints[2] = result.ecx;
254 static void identify_cpu(struct cpu_device_id *cpu)
256 cpu->device = 0; /* fix gcc 4.4.4 warning */
259 * Do a quick and dirty check to save space - Intel and AMD only and
260 * just the vendor. This is enough for most TPL code.
262 if (spl_phase() == PHASE_TPL) {
263 struct cpuid_result result;
265 result = cpuid(0x00000000);
266 switch (result.ecx >> 24) {
267 case 'l': /* GenuineIntel */
268 cpu->vendor = X86_VENDOR_INTEL;
270 case 'D': /* AuthenticAMD */
271 cpu->vendor = X86_VENDOR_AMD;
274 cpu->vendor = X86_VENDOR_ANY;
280 /* gcc 7.3 does not want to drop x86_vendors, so use #ifdef */
281 #ifndef CONFIG_TPL_BUILD
282 char vendor_name[16];
285 vendor_name[0] = '\0'; /* Unset */
287 /* Find the id and vendor_name */
289 /* Its a 486 if we can modify the AC flag */
290 if (flag_is_changeable_p(X86_EFLAGS_AC))
291 cpu->device = 0x00000400; /* 486 */
293 cpu->device = 0x00000300; /* 386 */
294 if ((cpu->device == 0x00000400) && test_cyrix_52div()) {
295 memcpy(vendor_name, "CyrixInstead", 13);
296 /* If we ever care we can enable cpuid here */
298 /* Detect NexGen with old hypercode */
299 else if (deep_magic_nexgen_probe())
300 memcpy(vendor_name, "NexGenDriven", 13);
304 cpuid_level = build_vendor_name(vendor_name);
305 vendor_name[12] = '\0';
307 /* Intel-defined flags: level 0x00000001 */
308 if (cpuid_level >= 0x00000001) {
309 cpu->device = cpuid_eax(0x00000001);
311 /* Have CPUID level 0 only unheard of */
312 cpu->device = 0x00000400;
315 cpu->vendor = X86_VENDOR_UNKNOWN;
316 for (i = 0; i < ARRAY_SIZE(x86_vendors); i++) {
317 if (memcmp(vendor_name, x86_vendors[i].name, 12) == 0) {
318 cpu->vendor = x86_vendors[i].vendor;
325 static inline void get_fms(struct cpuinfo_x86 *c, uint32_t tfms)
327 c->x86 = (tfms >> 8) & 0xf;
328 c->x86_model = (tfms >> 4) & 0xf;
329 c->x86_mask = tfms & 0xf;
331 c->x86 += (tfms >> 20) & 0xff;
333 c->x86_model += ((tfms >> 16) & 0xF) << 4;
336 u32 cpu_get_family_model(void)
338 return gd->arch.x86_device & 0x0fff0ff0;
341 u32 cpu_get_stepping(void)
343 return gd->arch.x86_mask;
346 /* initialise FPU, reset EM, set MP and NE */
347 static void setup_cpu_features(void)
349 const u32 em_rst = ~X86_CR0_EM;
350 const u32 mp_ne_set = X86_CR0_MP | X86_CR0_NE;
353 "movl %%cr0, %%eax\n" \
356 "movl %%eax, %%cr0\n" \
357 : : "i" (em_rst), "i" (mp_ne_set) : "eax");
360 static void setup_identity(void)
362 /* identify CPU via cpuid and store the decoded info into gd->arch */
364 struct cpu_device_id cpu;
365 struct cpuinfo_x86 c;
368 get_fms(&c, cpu.device);
369 gd->arch.x86 = c.x86;
370 gd->arch.x86_vendor = cpu.vendor;
371 gd->arch.x86_model = c.x86_model;
372 gd->arch.x86_mask = c.x86_mask;
373 gd->arch.x86_device = cpu.device;
375 gd->arch.has_mtrr = has_mtrr();
379 /* Don't allow PCI region 3 to use memory in the 2-4GB memory hole */
380 static void setup_pci_ram_top(void)
382 gd->pci_ram_top = 0x80000000U;
385 static void setup_mtrr(void)
389 /* Configure fixed range MTRRs for some legacy regions */
390 if (!gd->arch.has_mtrr)
393 mtrr_cap = native_read_msr(MTRR_CAP_MSR);
394 if (mtrr_cap & MTRR_CAP_FIX) {
395 /* Mark the VGA RAM area as uncacheable */
396 native_write_msr(MTRR_FIX_16K_A0000_MSR,
397 MTRR_FIX_TYPE(MTRR_TYPE_UNCACHEABLE),
398 MTRR_FIX_TYPE(MTRR_TYPE_UNCACHEABLE));
401 * Mark the PCI ROM area as cacheable to improve ROM
402 * execution performance.
404 native_write_msr(MTRR_FIX_4K_C0000_MSR,
405 MTRR_FIX_TYPE(MTRR_TYPE_WRBACK),
406 MTRR_FIX_TYPE(MTRR_TYPE_WRBACK));
407 native_write_msr(MTRR_FIX_4K_C8000_MSR,
408 MTRR_FIX_TYPE(MTRR_TYPE_WRBACK),
409 MTRR_FIX_TYPE(MTRR_TYPE_WRBACK));
410 native_write_msr(MTRR_FIX_4K_D0000_MSR,
411 MTRR_FIX_TYPE(MTRR_TYPE_WRBACK),
412 MTRR_FIX_TYPE(MTRR_TYPE_WRBACK));
413 native_write_msr(MTRR_FIX_4K_D8000_MSR,
414 MTRR_FIX_TYPE(MTRR_TYPE_WRBACK),
415 MTRR_FIX_TYPE(MTRR_TYPE_WRBACK));
417 /* Enable the fixed range MTRRs */
418 msr_setbits_64(MTRR_DEF_TYPE_MSR, MTRR_DEF_TYPE_FIX_EN);
422 int x86_cpu_init_tpl(void)
424 setup_cpu_features();
430 int x86_cpu_init_f(void)
433 setup_cpu_features();
438 /* Set up the i8254 timer if required */
439 if (IS_ENABLED(CONFIG_I8254_TIMER))
445 int x86_cpu_reinit_f(void)
453 void x86_enable_caches(void)
458 cr0 &= ~(X86_CR0_NW | X86_CR0_CD);
462 void enable_caches(void) __attribute__((weak, alias("x86_enable_caches")));
464 void x86_disable_caches(void)
469 cr0 |= X86_CR0_NW | X86_CR0_CD;
474 void disable_caches(void) __attribute__((weak, alias("x86_disable_caches")));
476 int dcache_status(void)
478 return !(read_cr0() & X86_CR0_CD);
481 void cpu_enable_paging_pae(ulong cr3)
483 __asm__ __volatile__(
484 /* Load the page table address */
487 "movl %%cr4, %%eax\n"
488 "orl $0x00000020, %%eax\n"
489 "movl %%eax, %%cr4\n"
491 "movl %%cr0, %%eax\n"
492 "orl $0x80000000, %%eax\n"
493 "movl %%eax, %%cr0\n"
499 void cpu_disable_paging_pae(void)
501 /* Turn off paging */
502 __asm__ __volatile__ (
504 "movl %%cr0, %%eax\n"
505 "andl $0x7fffffff, %%eax\n"
506 "movl %%eax, %%cr0\n"
508 "movl %%cr4, %%eax\n"
509 "andl $0xffffffdf, %%eax\n"
510 "movl %%eax, %%cr4\n"
516 static bool can_detect_long_mode(void)
518 return cpuid_eax(0x80000000) > 0x80000000UL;
521 static bool has_long_mode(void)
523 return cpuid_edx(0x80000001) & (1 << 29) ? true : false;
526 int cpu_has_64bit(void)
528 return has_cpuid() && can_detect_long_mode() &&
532 #define PAGETABLE_BASE 0x80000
533 #define PAGETABLE_SIZE (6 * 4096)
536 * build_pagetable() - build a flat 4GiB page table structure for 64-bti mode
538 * @pgtable: Pointer to a 24iKB block of memory
540 static void build_pagetable(uint32_t *pgtable)
544 memset(pgtable, '\0', PAGETABLE_SIZE);
546 /* Level 4 needs a single entry */
547 pgtable[0] = (ulong)&pgtable[1024] + 7;
549 /* Level 3 has one 64-bit entry for each GiB of memory */
550 for (i = 0; i < 4; i++)
551 pgtable[1024 + i * 2] = (ulong)&pgtable[2048] + 0x1000 * i + 7;
553 /* Level 2 has 2048 64-bit entries, each repesenting 2MiB */
554 for (i = 0; i < 2048; i++)
555 pgtable[2048 + i * 2] = 0x183 + (i << 21UL);
558 int cpu_jump_to_64bit(ulong setup_base, ulong target)
562 pgtable = memalign(4096, PAGETABLE_SIZE);
566 build_pagetable(pgtable);
567 cpu_call64((ulong)pgtable, setup_base, target);
574 * Jump from SPL to U-Boot
576 * This function is work-in-progress with many issues to resolve.
578 * It works by setting up several regions:
579 * ptr - a place to put the code that jumps into 64-bit mode
580 * gdt - a place to put the global descriptor table
581 * pgtable - a place to put the page tables
583 * The cpu_call64() code is copied from ROM and then manually patched so that
584 * it has the correct GDT address in RAM. U-Boot is copied from ROM into
585 * its pre-relocation address. Then we jump to the cpu_call64() code in RAM,
586 * which changes to 64-bit mode and starts U-Boot.
588 int cpu_jump_to_64bit_uboot(ulong target)
590 typedef void (*func_t)(ulong pgtable, ulong setup_base, ulong target);
595 pgtable = (uint32_t *)PAGETABLE_BASE;
597 build_pagetable(pgtable);
599 extern long call64_stub_size;
600 ptr = malloc(call64_stub_size);
602 printf("Failed to allocate the cpu_call64 stub\n");
605 memcpy(ptr, cpu_call64, call64_stub_size);
610 * Copy U-Boot from ROM
611 * TODO(sjg@chromium.org): Figure out a way to get the text base
612 * correctly here, and in the device-tree binman definition.
614 * Also consider using FIT so we get the correct image length and
617 memcpy((char *)target, (char *)0xfff00000, 0x100000);
620 func((ulong)pgtable, 0, (ulong)target);
626 static int enable_smis(struct udevice *cpu, void *unused)
631 static struct mp_flight_record mp_steps[] = {
632 MP_FR_BLOCK_APS(mp_init_cpu, NULL, mp_init_cpu, NULL),
633 /* Wait for APs to finish initialization before proceeding */
634 MP_FR_BLOCK_APS(NULL, NULL, enable_smis, NULL),
637 int x86_mp_init(void)
639 struct mp_params mp_params;
641 mp_params.parallel_microcode_load = 0,
642 mp_params.flight_plan = &mp_steps[0];
643 mp_params.num_records = ARRAY_SIZE(mp_steps);
644 mp_params.microcode_pointer = 0;
646 if (mp_init(&mp_params)) {
647 printf("Warning: MP init failure\n");