1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2008-2011
10 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
14 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
17 * Part of this file is adapted from coreboot
18 * src/arch/x86/lib/cpu.c
26 #include <asm/control_regs.h>
27 #include <asm/coreboot_tables.h>
29 #include <asm/global_data.h>
33 #include <asm/processor-flags.h>
34 #include <asm/u-boot-x86.h>
36 DECLARE_GLOBAL_DATA_PTR;
38 #define CPUID_FEATURE_PAE BIT(6)
39 #define CPUID_FEATURE_PSE36 BIT(17)
40 #define CPUID_FEAURE_HTT BIT(28)
43 * Constructor for a conventional segment GDT (or LDT) entry
44 * This is a macro so it can be used in initialisers
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)))
58 struct cpu_device_id {
64 uint8_t x86; /* CPU family */
65 uint8_t x86_vendor; /* CPU vendor */
70 /* gcc 7.3 does not wwant to drop x86_vendors, so use #ifdef */
71 #ifndef CONFIG_TPL_BUILD
73 * List of cpu vendor strings along with their normalized
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 ", },
94 static void load_ds(u32 segment)
96 asm volatile("movl %0, %%ds" : : "r" (segment * X86_GDT_ENTRY_SIZE));
99 static void load_es(u32 segment)
101 asm volatile("movl %0, %%es" : : "r" (segment * X86_GDT_ENTRY_SIZE));
104 static void load_fs(u32 segment)
106 asm volatile("movl %0, %%fs" : : "r" (segment * X86_GDT_ENTRY_SIZE));
109 static void load_gs(u32 segment)
111 asm volatile("movl %0, %%gs" : : "r" (segment * X86_GDT_ENTRY_SIZE));
114 static void load_ss(u32 segment)
116 asm volatile("movl %0, %%ss" : : "r" (segment * X86_GDT_ENTRY_SIZE));
119 static void load_gdt(const u64 *boot_gdt, u16 num_entries)
123 gdt.len = (num_entries * X86_GDT_ENTRY_SIZE) - 1;
124 gdt.ptr = (ulong)boot_gdt;
126 asm volatile("lgdtl %0\n" : : "m" (gdt));
129 void arch_setup_gd(gd_t *new_gd)
133 gdt_addr = new_gd->arch.gdt;
136 * CS: code, read/execute, 4 GB, base 0
138 * Some OS (like VxWorks) requires GDT entry 1 to be the 32-bit CS
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);
143 /* DS: data, read/write, 4 GB, base 0 */
144 gdt_addr[X86_GDT_ENTRY_32BIT_DS] = GDT_ENTRY(0xc093, 0, 0xfffff);
147 * FS: data, read/write, sizeof (Global Data Pointer),
148 * base (Global Data Pointer)
150 new_gd->arch.gd_addr = new_gd;
151 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);
155 /* 16-bit CS: code, read/execute, 64 kB, base 0 */
156 gdt_addr[X86_GDT_ENTRY_16BIT_CS] = GDT_ENTRY(0x009b, 0, 0x0ffff);
158 /* 16-bit DS: data, read/write, 64 kB, base 0 */
159 gdt_addr[X86_GDT_ENTRY_16BIT_DS] = GDT_ENTRY(0x0093, 0, 0x0ffff);
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);
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);
172 #ifdef CONFIG_HAVE_FSP
174 * Setup FSP execution environment GDT
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
179 * we used in arch/x86/cpu/start16.S, and reload the segment registers.
181 void setup_fsp_gdt(void)
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);
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.
199 * Perform the Cyrix 5/2 test. A Cyrix won't change
200 * the flags, while other 486 chips will.
202 static inline int test_cyrix_52div(void)
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 */
214 /* AH is 0x02 on Cyrix after the divide.. */
215 return (unsigned char) (test >> 8) == 0x02;
218 #ifndef CONFIG_TPL_BUILD
220 * Detect a NexGen CPU running without BIOS hypercode new enough
221 * to have CPUID. (Thanks to Herbert Oppmann)
223 static int deep_magic_nexgen_probe(void)
227 __asm__ __volatile__ (
228 " movw $0x5555, %%ax\n"
236 : "=a" (ret) : : "cx", "dx");
241 static bool has_cpuid(void)
243 return flag_is_changeable_p(X86_EFLAGS_ID);
246 static bool has_mtrr(void)
248 return cpuid_edx(0x00000001) & (1 << 12) ? true : false;
251 #ifndef CONFIG_TPL_BUILD
252 static int build_vendor_name(char *vendor_name)
254 struct cpuid_result result;
255 result = cpuid(0x00000000);
256 unsigned int *name_as_ints = (unsigned int *)vendor_name;
258 name_as_ints[0] = result.ebx;
259 name_as_ints[1] = result.edx;
260 name_as_ints[2] = result.ecx;
266 static void identify_cpu(struct cpu_device_id *cpu)
268 cpu->device = 0; /* fix gcc 4.4.4 warning */
271 * Do a quick and dirty check to save space - Intel and AMD only and
272 * just the vendor. This is enough for most TPL code.
274 if (xpl_phase() == PHASE_TPL) {
275 struct cpuid_result result;
277 result = cpuid(0x00000000);
278 switch (result.ecx >> 24) {
279 case 'l': /* GenuineIntel */
280 cpu->vendor = X86_VENDOR_INTEL;
282 case 'D': /* AuthenticAMD */
283 cpu->vendor = X86_VENDOR_AMD;
286 cpu->vendor = X86_VENDOR_ANY;
292 /* gcc 7.3 does not want to drop x86_vendors, so use #ifdef */
293 #ifndef CONFIG_TPL_BUILD
294 char vendor_name[16];
297 vendor_name[0] = '\0'; /* Unset */
299 /* Find the id and vendor_name */
301 /* Its a 486 if we can modify the AC flag */
302 if (flag_is_changeable_p(X86_EFLAGS_AC))
303 cpu->device = 0x00000400; /* 486 */
305 cpu->device = 0x00000300; /* 386 */
306 if ((cpu->device == 0x00000400) && test_cyrix_52div()) {
307 memcpy(vendor_name, "CyrixInstead", 13);
308 /* If we ever care we can enable cpuid here */
310 /* Detect NexGen with old hypercode */
311 else if (deep_magic_nexgen_probe())
312 memcpy(vendor_name, "NexGenDriven", 13);
316 cpuid_level = build_vendor_name(vendor_name);
317 vendor_name[12] = '\0';
319 /* Intel-defined flags: level 0x00000001 */
320 if (cpuid_level >= 0x00000001) {
321 cpu->device = cpuid_eax(0x00000001);
323 /* Have CPUID level 0 only unheard of */
324 cpu->device = 0x00000400;
327 cpu->vendor = X86_VENDOR_UNKNOWN;
328 for (i = 0; i < ARRAY_SIZE(x86_vendors); i++) {
329 if (memcmp(vendor_name, x86_vendors[i].name, 12) == 0) {
330 cpu->vendor = x86_vendors[i].vendor;
337 static inline void get_fms(struct cpuinfo_x86 *c, uint32_t tfms)
339 c->x86 = (tfms >> 8) & 0xf;
340 c->x86_model = (tfms >> 4) & 0xf;
341 c->x86_mask = tfms & 0xf;
343 c->x86 += (tfms >> 20) & 0xff;
345 c->x86_model += ((tfms >> 16) & 0xF) << 4;
348 u32 cpu_get_family_model(void)
350 return gd->arch.x86_device & 0x0fff0ff0;
353 u32 cpu_get_stepping(void)
355 return gd->arch.x86_mask;
358 /* initialise FPU, reset EM, set MP and NE */
359 static void setup_cpu_features(void)
361 const u32 em_rst = ~X86_CR0_EM;
362 const u32 mp_ne_set = X86_CR0_MP | X86_CR0_NE;
365 "movl %%cr0, %%eax\n" \
368 "movl %%eax, %%cr0\n" \
369 : : "i" (em_rst), "i" (mp_ne_set) : "eax");
372 void cpu_reinit_fpu(void)
377 static void setup_identity(void)
379 /* identify CPU via cpuid and store the decoded info into gd->arch */
381 struct cpu_device_id cpu;
382 struct cpuinfo_x86 c;
385 get_fms(&c, cpu.device);
386 gd->arch.x86 = c.x86;
387 gd->arch.x86_vendor = cpu.vendor;
388 gd->arch.x86_model = c.x86_model;
389 gd->arch.x86_mask = c.x86_mask;
390 gd->arch.x86_device = cpu.device;
392 gd->arch.has_mtrr = has_mtrr();
396 static uint cpu_cpuid_extended_level(void)
398 return cpuid_eax(0x80000000);
401 int cpu_phys_address_size(void)
406 if (cpu_cpuid_extended_level() >= 0x80000008)
407 return cpuid_eax(0x80000008) & 0xff;
409 if (cpuid_edx(1) & (CPUID_FEATURE_PAE | CPUID_FEATURE_PSE36))
415 static void setup_mtrr(void)
419 /* Configure fixed range MTRRs for some legacy regions */
420 if (!gd->arch.has_mtrr || !ll_boot_init())
423 mtrr_cap = native_read_msr(MTRR_CAP_MSR);
424 if (mtrr_cap & MTRR_CAP_FIX) {
425 /* Mark the VGA RAM area as uncacheable */
426 native_write_msr(MTRR_FIX_16K_A0000_MSR,
427 MTRR_FIX_TYPE(MTRR_TYPE_UNCACHEABLE),
428 MTRR_FIX_TYPE(MTRR_TYPE_UNCACHEABLE));
431 * Mark the PCI ROM area as cacheable to improve ROM
432 * execution performance.
434 native_write_msr(MTRR_FIX_4K_C0000_MSR,
435 MTRR_FIX_TYPE(MTRR_TYPE_WRBACK),
436 MTRR_FIX_TYPE(MTRR_TYPE_WRBACK));
437 native_write_msr(MTRR_FIX_4K_C8000_MSR,
438 MTRR_FIX_TYPE(MTRR_TYPE_WRBACK),
439 MTRR_FIX_TYPE(MTRR_TYPE_WRBACK));
440 native_write_msr(MTRR_FIX_4K_D0000_MSR,
441 MTRR_FIX_TYPE(MTRR_TYPE_WRBACK),
442 MTRR_FIX_TYPE(MTRR_TYPE_WRBACK));
443 native_write_msr(MTRR_FIX_4K_D8000_MSR,
444 MTRR_FIX_TYPE(MTRR_TYPE_WRBACK),
445 MTRR_FIX_TYPE(MTRR_TYPE_WRBACK));
447 /* Enable the fixed range MTRRs */
448 msr_setbits_64(MTRR_DEF_TYPE_MSR, MTRR_DEF_TYPE_FIX_EN);
452 int x86_cpu_init_tpl(void)
454 setup_cpu_features();
460 int x86_cpu_init_f(void)
463 setup_cpu_features();
467 /* Set up the i8254 timer if required */
468 if (IS_ENABLED(CONFIG_I8254_TIMER))
474 int x86_cpu_reinit_f(void)
479 addr = locate_coreboot_table();
481 gd->arch.coreboot_table = addr;
482 gd->flags |= GD_FLG_SKIP_LL_INIT;
488 void x86_enable_caches(void)
493 cr0 &= ~(X86_CR0_NW | X86_CR0_CD);
497 void enable_caches(void) __attribute__((weak, alias("x86_enable_caches")));
499 void x86_disable_caches(void)
504 cr0 |= X86_CR0_NW | X86_CR0_CD;
509 void disable_caches(void) __attribute__((weak, alias("x86_disable_caches")));
511 int dcache_status(void)
513 return !(read_cr0() & X86_CR0_CD);
516 void cpu_enable_paging_pae(ulong cr3)
518 __asm__ __volatile__(
519 /* Load the page table address */
522 "movl %%cr4, %%eax\n"
523 "orl $0x00000020, %%eax\n"
524 "movl %%eax, %%cr4\n"
526 "movl %%cr0, %%eax\n"
527 "orl $0x80000000, %%eax\n"
528 "movl %%eax, %%cr0\n"
534 void cpu_disable_paging_pae(void)
536 /* Turn off paging */
537 __asm__ __volatile__ (
539 "movl %%cr0, %%eax\n"
540 "andl $0x7fffffff, %%eax\n"
541 "movl %%eax, %%cr0\n"
543 "movl %%cr4, %%eax\n"
544 "andl $0xffffffdf, %%eax\n"
545 "movl %%eax, %%cr4\n"
551 static bool can_detect_long_mode(void)
553 return cpuid_eax(0x80000000) > 0x80000000UL;
556 static bool has_long_mode(void)
558 return cpuid_edx(0x80000001) & (1 << 29) ? true : false;
561 int cpu_has_64bit(void)
563 return has_cpuid() && can_detect_long_mode() &&
567 /* Base address for page tables used for 64-bit mode */
568 #define PAGETABLE_BASE 0x80000
569 #define PAGETABLE_SIZE (6 * 4096)
572 * build_pagetable() - build a flat 4GiB page table structure for 64-bti mode
574 * @pgtable: Pointer to a 24iKB block of memory
576 static void build_pagetable(uint32_t *pgtable)
580 memset(pgtable, '\0', PAGETABLE_SIZE);
582 /* Level 4 needs a single entry */
583 pgtable[0] = (ulong)&pgtable[1024] + 7;
585 /* Level 3 has one 64-bit entry for each GiB of memory */
586 for (i = 0; i < 4; i++)
587 pgtable[1024 + i * 2] = (ulong)&pgtable[2048] + 0x1000 * i + 7;
589 /* Level 2 has 2048 64-bit entries, each repesenting 2MiB */
590 for (i = 0; i < 2048; i++)
591 pgtable[2048 + i * 2] = 0x183 + (i << 21UL);
594 int cpu_jump_to_64bit(ulong setup_base, ulong target)
598 pgtable = memalign(4096, PAGETABLE_SIZE);
602 build_pagetable(pgtable);
603 cpu_call64((ulong)pgtable, setup_base, target);
610 * cpu_jump_to_64bit_uboot() - Jump from SPL to U-Boot
612 * It works by setting up page tables and calling the code to enter 64-bit long
615 int cpu_jump_to_64bit_uboot(ulong target)
619 pgtable = (uint32_t *)PAGETABLE_BASE;
620 build_pagetable(pgtable);
623 cpu_call64(PAGETABLE_BASE, 0, (ulong)target);
628 int x86_mp_init(void)
634 printf("Warning: MP init failure\n");