]> Git Repo - J-u-boot.git/blob - arch/x86/cpu/i386/cpu.c
xpl: Rename spl_phase() to xpl_phase()
[J-u-boot.git] / arch / x86 / cpu / i386 / cpu.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2008-2011
4  * Graeme Russ, <[email protected]>
5  *
6  * (C) Copyright 2002
7  * Daniel Engström, Omicron Ceti AB, <[email protected]>
8  *
9  * (C) Copyright 2002
10  * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
11  * Marius Groeger <[email protected]>
12  *
13  * (C) Copyright 2002
14  * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
15  * Alex Zuepke <[email protected]>
16  *
17  * Part of this file is adapted from coreboot
18  * src/arch/x86/lib/cpu.c
19  */
20
21 #include <cpu_func.h>
22 #include <init.h>
23 #include <log.h>
24 #include <malloc.h>
25 #include <spl.h>
26 #include <asm/control_regs.h>
27 #include <asm/coreboot_tables.h>
28 #include <asm/cpu.h>
29 #include <asm/global_data.h>
30 #include <asm/mp.h>
31 #include <asm/msr.h>
32 #include <asm/mtrr.h>
33 #include <asm/processor-flags.h>
34 #include <asm/u-boot-x86.h>
35
36 DECLARE_GLOBAL_DATA_PTR;
37
38 #define CPUID_FEATURE_PAE       BIT(6)
39 #define CPUID_FEATURE_PSE36     BIT(17)
40 #define CPUID_FEAURE_HTT        BIT(28)
41
42 /*
43  * Constructor for a conventional segment GDT (or LDT) entry
44  * This is a macro so it can be used in initialisers
45  */
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)))
52
53 struct gdt_ptr {
54         u16 len;
55         u32 ptr;
56 } __packed;
57
58 struct cpu_device_id {
59         unsigned vendor;
60         unsigned device;
61 };
62
63 struct cpuinfo_x86 {
64         uint8_t x86;            /* CPU family */
65         uint8_t x86_vendor;     /* CPU vendor */
66         uint8_t x86_model;
67         uint8_t x86_mask;
68 };
69
70 /* gcc 7.3 does not wwant to drop x86_vendors, so use #ifdef */
71 #ifndef CONFIG_TPL_BUILD
72 /*
73  * List of cpu vendor strings along with their normalized
74  * id values.
75  */
76 static const struct {
77         int vendor;
78         const char *name;
79 } x86_vendors[] = {
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 ", },
91 };
92 #endif
93
94 static void load_ds(u32 segment)
95 {
96         asm volatile("movl %0, %%ds" : : "r" (segment * X86_GDT_ENTRY_SIZE));
97 }
98
99 static void load_es(u32 segment)
100 {
101         asm volatile("movl %0, %%es" : : "r" (segment * X86_GDT_ENTRY_SIZE));
102 }
103
104 static void load_fs(u32 segment)
105 {
106         asm volatile("movl %0, %%fs" : : "r" (segment * X86_GDT_ENTRY_SIZE));
107 }
108
109 static void load_gs(u32 segment)
110 {
111         asm volatile("movl %0, %%gs" : : "r" (segment * X86_GDT_ENTRY_SIZE));
112 }
113
114 static void load_ss(u32 segment)
115 {
116         asm volatile("movl %0, %%ss" : : "r" (segment * X86_GDT_ENTRY_SIZE));
117 }
118
119 static void load_gdt(const u64 *boot_gdt, u16 num_entries)
120 {
121         struct gdt_ptr gdt;
122
123         gdt.len = (num_entries * X86_GDT_ENTRY_SIZE) - 1;
124         gdt.ptr = (ulong)boot_gdt;
125
126         asm volatile("lgdtl %0\n" : : "m" (gdt));
127 }
128
129 void arch_setup_gd(gd_t *new_gd)
130 {
131         u64 *gdt_addr;
132
133         gdt_addr = new_gd->arch.gdt;
134
135         /*
136          * CS: code, read/execute, 4 GB, base 0
137          *
138          * Some OS (like VxWorks) requires GDT entry 1 to be the 32-bit CS
139          */
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);
142
143         /* DS: data, read/write, 4 GB, base 0 */
144         gdt_addr[X86_GDT_ENTRY_32BIT_DS] = GDT_ENTRY(0xc093, 0, 0xfffff);
145
146         /*
147          * FS: data, read/write, sizeof (Global Data Pointer),
148          * base (Global Data Pointer)
149          */
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);
154
155         /* 16-bit CS: code, read/execute, 64 kB, base 0 */
156         gdt_addr[X86_GDT_ENTRY_16BIT_CS] = GDT_ENTRY(0x009b, 0, 0x0ffff);
157
158         /* 16-bit DS: data, read/write, 64 kB, base 0 */
159         gdt_addr[X86_GDT_ENTRY_16BIT_DS] = GDT_ENTRY(0x0093, 0, 0x0ffff);
160
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);
163
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);
170 }
171
172 #ifdef CONFIG_HAVE_FSP
173 /*
174  * Setup FSP execution environment GDT
175  *
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.
180  */
181 void setup_fsp_gdt(void)
182 {
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);
189 }
190 #endif
191
192 /*
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.
196  */
197
198 /*
199  * Perform the Cyrix 5/2 test. A Cyrix won't change
200  * the flags, while other 486 chips will.
201  */
202 static inline int test_cyrix_52div(void)
203 {
204         unsigned int test;
205
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 */
210              : "=a" (test)
211              : "0" (5), "q" (2)
212              : "cc");
213
214         /* AH is 0x02 on Cyrix after the divide.. */
215         return (unsigned char) (test >> 8) == 0x02;
216 }
217
218 #ifndef CONFIG_TPL_BUILD
219 /*
220  *      Detect a NexGen CPU running without BIOS hypercode new enough
221  *      to have CPUID. (Thanks to Herbert Oppmann)
222  */
223 static int deep_magic_nexgen_probe(void)
224 {
225         int ret;
226
227         __asm__ __volatile__ (
228                 "       movw    $0x5555, %%ax\n"
229                 "       xorw    %%dx,%%dx\n"
230                 "       movw    $2, %%cx\n"
231                 "       divw    %%cx\n"
232                 "       movl    $0, %%eax\n"
233                 "       jnz     1f\n"
234                 "       movl    $1, %%eax\n"
235                 "1:\n"
236                 : "=a" (ret) : : "cx", "dx");
237         return  ret;
238 }
239 #endif
240
241 static bool has_cpuid(void)
242 {
243         return flag_is_changeable_p(X86_EFLAGS_ID);
244 }
245
246 static bool has_mtrr(void)
247 {
248         return cpuid_edx(0x00000001) & (1 << 12) ? true : false;
249 }
250
251 #ifndef CONFIG_TPL_BUILD
252 static int build_vendor_name(char *vendor_name)
253 {
254         struct cpuid_result result;
255         result = cpuid(0x00000000);
256         unsigned int *name_as_ints = (unsigned int *)vendor_name;
257
258         name_as_ints[0] = result.ebx;
259         name_as_ints[1] = result.edx;
260         name_as_ints[2] = result.ecx;
261
262         return result.eax;
263 }
264 #endif
265
266 static void identify_cpu(struct cpu_device_id *cpu)
267 {
268         cpu->device = 0; /* fix gcc 4.4.4 warning */
269
270         /*
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.
273          */
274         if (xpl_phase() == PHASE_TPL) {
275                 struct cpuid_result result;
276
277                 result = cpuid(0x00000000);
278                 switch (result.ecx >> 24) {
279                 case 'l': /* GenuineIntel */
280                         cpu->vendor = X86_VENDOR_INTEL;
281                         break;
282                 case 'D': /* AuthenticAMD */
283                         cpu->vendor = X86_VENDOR_AMD;
284                         break;
285                 default:
286                         cpu->vendor = X86_VENDOR_ANY;
287                         break;
288                 }
289                 return;
290         }
291
292 /* gcc 7.3 does not want to drop x86_vendors, so use #ifdef */
293 #ifndef CONFIG_TPL_BUILD
294         char vendor_name[16];
295         int i;
296
297         vendor_name[0] = '\0'; /* Unset */
298
299         /* Find the id and vendor_name */
300         if (!has_cpuid()) {
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 */
304                 else
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 */
309                 }
310                 /* Detect NexGen with old hypercode */
311                 else if (deep_magic_nexgen_probe())
312                         memcpy(vendor_name, "NexGenDriven", 13);
313         } else {
314                 int cpuid_level;
315
316                 cpuid_level = build_vendor_name(vendor_name);
317                 vendor_name[12] = '\0';
318
319                 /* Intel-defined flags: level 0x00000001 */
320                 if (cpuid_level >= 0x00000001) {
321                         cpu->device = cpuid_eax(0x00000001);
322                 } else {
323                         /* Have CPUID level 0 only unheard of */
324                         cpu->device = 0x00000400;
325                 }
326         }
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;
331                         break;
332                 }
333         }
334 #endif
335 }
336
337 static inline void get_fms(struct cpuinfo_x86 *c, uint32_t tfms)
338 {
339         c->x86 = (tfms >> 8) & 0xf;
340         c->x86_model = (tfms >> 4) & 0xf;
341         c->x86_mask = tfms & 0xf;
342         if (c->x86 == 0xf)
343                 c->x86 += (tfms >> 20) & 0xff;
344         if (c->x86 >= 0x6)
345                 c->x86_model += ((tfms >> 16) & 0xF) << 4;
346 }
347
348 u32 cpu_get_family_model(void)
349 {
350         return gd->arch.x86_device & 0x0fff0ff0;
351 }
352
353 u32 cpu_get_stepping(void)
354 {
355         return gd->arch.x86_mask;
356 }
357
358 /* initialise FPU, reset EM, set MP and NE */
359 static void setup_cpu_features(void)
360 {
361         const u32 em_rst = ~X86_CR0_EM;
362         const u32 mp_ne_set = X86_CR0_MP | X86_CR0_NE;
363
364         asm ("fninit\n" \
365         "movl %%cr0, %%eax\n" \
366         "andl %0, %%eax\n" \
367         "orl  %1, %%eax\n" \
368         "movl %%eax, %%cr0\n" \
369         : : "i" (em_rst), "i" (mp_ne_set) : "eax");
370 }
371
372 void cpu_reinit_fpu(void)
373 {
374         asm ("fninit\n");
375 }
376
377 static void setup_identity(void)
378 {
379         /* identify CPU via cpuid and store the decoded info into gd->arch */
380         if (has_cpuid()) {
381                 struct cpu_device_id cpu;
382                 struct cpuinfo_x86 c;
383
384                 identify_cpu(&cpu);
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;
391
392                 gd->arch.has_mtrr = has_mtrr();
393         }
394 }
395
396 static uint cpu_cpuid_extended_level(void)
397 {
398         return cpuid_eax(0x80000000);
399 }
400
401 int cpu_phys_address_size(void)
402 {
403         if (!has_cpuid())
404                 return 32;
405
406         if (cpu_cpuid_extended_level() >= 0x80000008)
407                 return cpuid_eax(0x80000008) & 0xff;
408
409         if (cpuid_edx(1) & (CPUID_FEATURE_PAE | CPUID_FEATURE_PSE36))
410                 return 36;
411
412         return 32;
413 }
414
415 static void setup_mtrr(void)
416 {
417         u64 mtrr_cap;
418
419         /* Configure fixed range MTRRs for some legacy regions */
420         if (!gd->arch.has_mtrr || !ll_boot_init())
421                 return;
422
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));
429
430                 /*
431                  * Mark the PCI ROM area as cacheable to improve ROM
432                  * execution performance.
433                  */
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));
446
447                 /* Enable the fixed range MTRRs */
448                 msr_setbits_64(MTRR_DEF_TYPE_MSR, MTRR_DEF_TYPE_FIX_EN);
449         }
450 }
451
452 int x86_cpu_init_tpl(void)
453 {
454         setup_cpu_features();
455         setup_identity();
456
457         return 0;
458 }
459
460 int x86_cpu_init_f(void)
461 {
462         if (ll_boot_init())
463                 setup_cpu_features();
464         setup_identity();
465         setup_mtrr();
466
467         /* Set up the i8254 timer if required */
468         if (IS_ENABLED(CONFIG_I8254_TIMER))
469                 i8254_init();
470
471         return 0;
472 }
473
474 int x86_cpu_reinit_f(void)
475 {
476         long addr;
477
478         setup_identity();
479         addr = locate_coreboot_table();
480         if (addr >= 0) {
481                 gd->arch.coreboot_table = addr;
482                 gd->flags |= GD_FLG_SKIP_LL_INIT;
483         }
484
485         return 0;
486 }
487
488 void x86_enable_caches(void)
489 {
490         unsigned long cr0;
491
492         cr0 = read_cr0();
493         cr0 &= ~(X86_CR0_NW | X86_CR0_CD);
494         write_cr0(cr0);
495         wbinvd();
496 }
497 void enable_caches(void) __attribute__((weak, alias("x86_enable_caches")));
498
499 void x86_disable_caches(void)
500 {
501         unsigned long cr0;
502
503         cr0 = read_cr0();
504         cr0 |= X86_CR0_NW | X86_CR0_CD;
505         wbinvd();
506         write_cr0(cr0);
507         wbinvd();
508 }
509 void disable_caches(void) __attribute__((weak, alias("x86_disable_caches")));
510
511 int dcache_status(void)
512 {
513         return !(read_cr0() & X86_CR0_CD);
514 }
515
516 void cpu_enable_paging_pae(ulong cr3)
517 {
518         __asm__ __volatile__(
519                 /* Load the page table address */
520                 "movl   %0, %%cr3\n"
521                 /* Enable pae */
522                 "movl   %%cr4, %%eax\n"
523                 "orl    $0x00000020, %%eax\n"
524                 "movl   %%eax, %%cr4\n"
525                 /* Enable paging */
526                 "movl   %%cr0, %%eax\n"
527                 "orl    $0x80000000, %%eax\n"
528                 "movl   %%eax, %%cr0\n"
529                 :
530                 : "r" (cr3)
531                 : "eax");
532 }
533
534 void cpu_disable_paging_pae(void)
535 {
536         /* Turn off paging */
537         __asm__ __volatile__ (
538                 /* Disable paging */
539                 "movl   %%cr0, %%eax\n"
540                 "andl   $0x7fffffff, %%eax\n"
541                 "movl   %%eax, %%cr0\n"
542                 /* Disable pae */
543                 "movl   %%cr4, %%eax\n"
544                 "andl   $0xffffffdf, %%eax\n"
545                 "movl   %%eax, %%cr4\n"
546                 :
547                 :
548                 : "eax");
549 }
550
551 static bool can_detect_long_mode(void)
552 {
553         return cpuid_eax(0x80000000) > 0x80000000UL;
554 }
555
556 static bool has_long_mode(void)
557 {
558         return cpuid_edx(0x80000001) & (1 << 29) ? true : false;
559 }
560
561 int cpu_has_64bit(void)
562 {
563         return has_cpuid() && can_detect_long_mode() &&
564                 has_long_mode();
565 }
566
567 /* Base address for page tables used for 64-bit mode */
568 #define PAGETABLE_BASE          0x80000
569 #define PAGETABLE_SIZE          (6 * 4096)
570
571 /**
572  * build_pagetable() - build a flat 4GiB page table structure for 64-bti mode
573  *
574  * @pgtable: Pointer to a 24iKB block of memory
575  */
576 static void build_pagetable(uint32_t *pgtable)
577 {
578         uint i;
579
580         memset(pgtable, '\0', PAGETABLE_SIZE);
581
582         /* Level 4 needs a single entry */
583         pgtable[0] = (ulong)&pgtable[1024] + 7;
584
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;
588
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);
592 }
593
594 int cpu_jump_to_64bit(ulong setup_base, ulong target)
595 {
596         uint32_t *pgtable;
597
598         pgtable = memalign(4096, PAGETABLE_SIZE);
599         if (!pgtable)
600                 return -ENOMEM;
601
602         build_pagetable(pgtable);
603         cpu_call64((ulong)pgtable, setup_base, target);
604         free(pgtable);
605
606         return -EFAULT;
607 }
608
609 /*
610  * cpu_jump_to_64bit_uboot() - Jump from SPL to U-Boot
611  *
612  * It works by setting up page tables and calling the code to enter 64-bit long
613  * mode
614  */
615 int cpu_jump_to_64bit_uboot(ulong target)
616 {
617         uint32_t *pgtable;
618
619         pgtable = (uint32_t *)PAGETABLE_BASE;
620         build_pagetable(pgtable);
621
622         /* Jump to U-Boot */
623         cpu_call64(PAGETABLE_BASE, 0, (ulong)target);
624
625         return -EFAULT;
626 }
627
628 int x86_mp_init(void)
629 {
630         int ret;
631
632         ret = mp_init();
633         if (ret) {
634                 printf("Warning: MP init failure\n");
635                 return log_ret(ret);
636         }
637
638         return 0;
639 }
This page took 0.063531 seconds and 4 git commands to generate.