Merge branch '2020-01-17-reduce-size-of-common-h-even-more'
[oweals/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, <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
19  */
20
21 #include <common.h>
22 #include <cpu_func.h>
23 #include <init.h>
24 #include <malloc.h>
25 #include <spl.h>
26 #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
33 DECLARE_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
46 struct gdt_ptr {
47         u16 len;
48         u32 ptr;
49 } __packed;
50
51 struct cpu_device_id {
52         unsigned vendor;
53         unsigned device;
54 };
55
56 struct 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
63 /* gcc 7.3 does not wwant to drop x86_vendors, so use #ifdef */
64 #ifndef CONFIG_TPL_BUILD
65 /*
66  * List of cpu vendor strings along with their normalized
67  * id values.
68  */
69 static 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 };
85 #endif
86
87 static void load_ds(u32 segment)
88 {
89         asm volatile("movl %0, %%ds" : : "r" (segment * X86_GDT_ENTRY_SIZE));
90 }
91
92 static void load_es(u32 segment)
93 {
94         asm volatile("movl %0, %%es" : : "r" (segment * X86_GDT_ENTRY_SIZE));
95 }
96
97 static void load_fs(u32 segment)
98 {
99         asm volatile("movl %0, %%fs" : : "r" (segment * X86_GDT_ENTRY_SIZE));
100 }
101
102 static void load_gs(u32 segment)
103 {
104         asm volatile("movl %0, %%gs" : : "r" (segment * X86_GDT_ENTRY_SIZE));
105 }
106
107 static void load_ss(u32 segment)
108 {
109         asm volatile("movl %0, %%ss" : : "r" (segment * X86_GDT_ENTRY_SIZE));
110 }
111
112 static 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
122 void 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
139         /* FS: data, read/write, 4 GB, base (Global Data Pointer) */
140         new_gd->arch.gd_addr = new_gd;
141         gdt_addr[X86_GDT_ENTRY_32BIT_FS] = GDT_ENTRY(0xc093,
142                      (ulong)&new_gd->arch.gd_addr, 0xfffff);
143
144         /* 16-bit CS: code, read/execute, 64 kB, base 0 */
145         gdt_addr[X86_GDT_ENTRY_16BIT_CS] = GDT_ENTRY(0x009b, 0, 0x0ffff);
146
147         /* 16-bit DS: data, read/write, 64 kB, base 0 */
148         gdt_addr[X86_GDT_ENTRY_16BIT_DS] = GDT_ENTRY(0x0093, 0, 0x0ffff);
149
150         gdt_addr[X86_GDT_ENTRY_16BIT_FLAT_CS] = GDT_ENTRY(0x809b, 0, 0xfffff);
151         gdt_addr[X86_GDT_ENTRY_16BIT_FLAT_DS] = GDT_ENTRY(0x8093, 0, 0xfffff);
152
153         load_gdt(gdt_addr, X86_GDT_NUM_ENTRIES);
154         load_ds(X86_GDT_ENTRY_32BIT_DS);
155         load_es(X86_GDT_ENTRY_32BIT_DS);
156         load_gs(X86_GDT_ENTRY_32BIT_DS);
157         load_ss(X86_GDT_ENTRY_32BIT_DS);
158         load_fs(X86_GDT_ENTRY_32BIT_FS);
159 }
160
161 #ifdef CONFIG_HAVE_FSP
162 /*
163  * Setup FSP execution environment GDT
164  *
165  * Per Intel FSP external architecture specification, before calling any FSP
166  * APIs, we need make sure the system is in flat 32-bit mode and both the code
167  * and data selectors should have full 4GB access range. Here we reuse the one
168  * we used in arch/x86/cpu/start16.S, and reload the segement registers.
169  */
170 void setup_fsp_gdt(void)
171 {
172         load_gdt((const u64 *)(gdt_rom + CONFIG_RESET_SEG_START), 4);
173         load_ds(X86_GDT_ENTRY_32BIT_DS);
174         load_ss(X86_GDT_ENTRY_32BIT_DS);
175         load_es(X86_GDT_ENTRY_32BIT_DS);
176         load_fs(X86_GDT_ENTRY_32BIT_DS);
177         load_gs(X86_GDT_ENTRY_32BIT_DS);
178 }
179 #endif
180
181 /*
182  * Cyrix CPUs without cpuid or with cpuid not yet enabled can be detected
183  * by the fact that they preserve the flags across the division of 5/2.
184  * PII and PPro exhibit this behavior too, but they have cpuid available.
185  */
186
187 /*
188  * Perform the Cyrix 5/2 test. A Cyrix won't change
189  * the flags, while other 486 chips will.
190  */
191 static inline int test_cyrix_52div(void)
192 {
193         unsigned int test;
194
195         __asm__ __volatile__(
196              "sahf\n\t"         /* clear flags (%eax = 0x0005) */
197              "div %b2\n\t"      /* divide 5 by 2 */
198              "lahf"             /* store flags into %ah */
199              : "=a" (test)
200              : "0" (5), "q" (2)
201              : "cc");
202
203         /* AH is 0x02 on Cyrix after the divide.. */
204         return (unsigned char) (test >> 8) == 0x02;
205 }
206
207 #ifndef CONFIG_TPL_BUILD
208 /*
209  *      Detect a NexGen CPU running without BIOS hypercode new enough
210  *      to have CPUID. (Thanks to Herbert Oppmann)
211  */
212 static int deep_magic_nexgen_probe(void)
213 {
214         int ret;
215
216         __asm__ __volatile__ (
217                 "       movw    $0x5555, %%ax\n"
218                 "       xorw    %%dx,%%dx\n"
219                 "       movw    $2, %%cx\n"
220                 "       divw    %%cx\n"
221                 "       movl    $0, %%eax\n"
222                 "       jnz     1f\n"
223                 "       movl    $1, %%eax\n"
224                 "1:\n"
225                 : "=a" (ret) : : "cx", "dx");
226         return  ret;
227 }
228 #endif
229
230 static bool has_cpuid(void)
231 {
232         return flag_is_changeable_p(X86_EFLAGS_ID);
233 }
234
235 static bool has_mtrr(void)
236 {
237         return cpuid_edx(0x00000001) & (1 << 12) ? true : false;
238 }
239
240 #ifndef CONFIG_TPL_BUILD
241 static int build_vendor_name(char *vendor_name)
242 {
243         struct cpuid_result result;
244         result = cpuid(0x00000000);
245         unsigned int *name_as_ints = (unsigned int *)vendor_name;
246
247         name_as_ints[0] = result.ebx;
248         name_as_ints[1] = result.edx;
249         name_as_ints[2] = result.ecx;
250
251         return result.eax;
252 }
253 #endif
254
255 static void identify_cpu(struct cpu_device_id *cpu)
256 {
257         cpu->device = 0; /* fix gcc 4.4.4 warning */
258
259         /*
260          * Do a quick and dirty check to save space - Intel and AMD only and
261          * just the vendor. This is enough for most TPL code.
262          */
263         if (spl_phase() == PHASE_TPL) {
264                 struct cpuid_result result;
265
266                 result = cpuid(0x00000000);
267                 switch (result.ecx >> 24) {
268                 case 'l': /* GenuineIntel */
269                         cpu->vendor = X86_VENDOR_INTEL;
270                         break;
271                 case 'D': /* AuthenticAMD */
272                         cpu->vendor = X86_VENDOR_AMD;
273                         break;
274                 default:
275                         cpu->vendor = X86_VENDOR_ANY;
276                         break;
277                 }
278                 return;
279         }
280
281 /* gcc 7.3 does not want to drop x86_vendors, so use #ifdef */
282 #ifndef CONFIG_TPL_BUILD
283         char vendor_name[16];
284         int i;
285
286         vendor_name[0] = '\0'; /* Unset */
287
288         /* Find the id and vendor_name */
289         if (!has_cpuid()) {
290                 /* Its a 486 if we can modify the AC flag */
291                 if (flag_is_changeable_p(X86_EFLAGS_AC))
292                         cpu->device = 0x00000400; /* 486 */
293                 else
294                         cpu->device = 0x00000300; /* 386 */
295                 if ((cpu->device == 0x00000400) && test_cyrix_52div()) {
296                         memcpy(vendor_name, "CyrixInstead", 13);
297                         /* If we ever care we can enable cpuid here */
298                 }
299                 /* Detect NexGen with old hypercode */
300                 else if (deep_magic_nexgen_probe())
301                         memcpy(vendor_name, "NexGenDriven", 13);
302         } else {
303                 int cpuid_level;
304
305                 cpuid_level = build_vendor_name(vendor_name);
306                 vendor_name[12] = '\0';
307
308                 /* Intel-defined flags: level 0x00000001 */
309                 if (cpuid_level >= 0x00000001) {
310                         cpu->device = cpuid_eax(0x00000001);
311                 } else {
312                         /* Have CPUID level 0 only unheard of */
313                         cpu->device = 0x00000400;
314                 }
315         }
316         cpu->vendor = X86_VENDOR_UNKNOWN;
317         for (i = 0; i < ARRAY_SIZE(x86_vendors); i++) {
318                 if (memcmp(vendor_name, x86_vendors[i].name, 12) == 0) {
319                         cpu->vendor = x86_vendors[i].vendor;
320                         break;
321                 }
322         }
323 #endif
324 }
325
326 static inline void get_fms(struct cpuinfo_x86 *c, uint32_t tfms)
327 {
328         c->x86 = (tfms >> 8) & 0xf;
329         c->x86_model = (tfms >> 4) & 0xf;
330         c->x86_mask = tfms & 0xf;
331         if (c->x86 == 0xf)
332                 c->x86 += (tfms >> 20) & 0xff;
333         if (c->x86 >= 0x6)
334                 c->x86_model += ((tfms >> 16) & 0xF) << 4;
335 }
336
337 u32 cpu_get_family_model(void)
338 {
339         return gd->arch.x86_device & 0x0fff0ff0;
340 }
341
342 u32 cpu_get_stepping(void)
343 {
344         return gd->arch.x86_mask;
345 }
346
347 /* initialise FPU, reset EM, set MP and NE */
348 static void setup_cpu_features(void)
349 {
350         const u32 em_rst = ~X86_CR0_EM;
351         const u32 mp_ne_set = X86_CR0_MP | X86_CR0_NE;
352
353         asm ("fninit\n" \
354         "movl %%cr0, %%eax\n" \
355         "andl %0, %%eax\n" \
356         "orl  %1, %%eax\n" \
357         "movl %%eax, %%cr0\n" \
358         : : "i" (em_rst), "i" (mp_ne_set) : "eax");
359 }
360
361 static void setup_identity(void)
362 {
363         /* identify CPU via cpuid and store the decoded info into gd->arch */
364         if (has_cpuid()) {
365                 struct cpu_device_id cpu;
366                 struct cpuinfo_x86 c;
367
368                 identify_cpu(&cpu);
369                 get_fms(&c, cpu.device);
370                 gd->arch.x86 = c.x86;
371                 gd->arch.x86_vendor = cpu.vendor;
372                 gd->arch.x86_model = c.x86_model;
373                 gd->arch.x86_mask = c.x86_mask;
374                 gd->arch.x86_device = cpu.device;
375
376                 gd->arch.has_mtrr = has_mtrr();
377         }
378 }
379
380 /* Don't allow PCI region 3 to use memory in the 2-4GB memory hole */
381 static void setup_pci_ram_top(void)
382 {
383         gd->pci_ram_top = 0x80000000U;
384 }
385
386 static void setup_mtrr(void)
387 {
388         u64 mtrr_cap;
389
390         /* Configure fixed range MTRRs for some legacy regions */
391         if (!gd->arch.has_mtrr)
392                 return;
393
394         mtrr_cap = native_read_msr(MTRR_CAP_MSR);
395         if (mtrr_cap & MTRR_CAP_FIX) {
396                 /* Mark the VGA RAM area as uncacheable */
397                 native_write_msr(MTRR_FIX_16K_A0000_MSR,
398                                  MTRR_FIX_TYPE(MTRR_TYPE_UNCACHEABLE),
399                                  MTRR_FIX_TYPE(MTRR_TYPE_UNCACHEABLE));
400
401                 /*
402                  * Mark the PCI ROM area as cacheable to improve ROM
403                  * execution performance.
404                  */
405                 native_write_msr(MTRR_FIX_4K_C0000_MSR,
406                                  MTRR_FIX_TYPE(MTRR_TYPE_WRBACK),
407                                  MTRR_FIX_TYPE(MTRR_TYPE_WRBACK));
408                 native_write_msr(MTRR_FIX_4K_C8000_MSR,
409                                  MTRR_FIX_TYPE(MTRR_TYPE_WRBACK),
410                                  MTRR_FIX_TYPE(MTRR_TYPE_WRBACK));
411                 native_write_msr(MTRR_FIX_4K_D0000_MSR,
412                                  MTRR_FIX_TYPE(MTRR_TYPE_WRBACK),
413                                  MTRR_FIX_TYPE(MTRR_TYPE_WRBACK));
414                 native_write_msr(MTRR_FIX_4K_D8000_MSR,
415                                  MTRR_FIX_TYPE(MTRR_TYPE_WRBACK),
416                                  MTRR_FIX_TYPE(MTRR_TYPE_WRBACK));
417
418                 /* Enable the fixed range MTRRs */
419                 msr_setbits_64(MTRR_DEF_TYPE_MSR, MTRR_DEF_TYPE_FIX_EN);
420         }
421 }
422
423 int x86_cpu_init_tpl(void)
424 {
425         setup_cpu_features();
426         setup_identity();
427
428         return 0;
429 }
430
431 int x86_cpu_init_f(void)
432 {
433         if (ll_boot_init())
434                 setup_cpu_features();
435         setup_identity();
436         setup_mtrr();
437         setup_pci_ram_top();
438
439         /* Set up the i8254 timer if required */
440         if (IS_ENABLED(CONFIG_I8254_TIMER))
441                 i8254_init();
442
443         return 0;
444 }
445
446 int x86_cpu_reinit_f(void)
447 {
448         setup_identity();
449         setup_pci_ram_top();
450
451         return 0;
452 }
453
454 void x86_enable_caches(void)
455 {
456         unsigned long cr0;
457
458         cr0 = read_cr0();
459         cr0 &= ~(X86_CR0_NW | X86_CR0_CD);
460         write_cr0(cr0);
461         wbinvd();
462 }
463 void enable_caches(void) __attribute__((weak, alias("x86_enable_caches")));
464
465 void x86_disable_caches(void)
466 {
467         unsigned long cr0;
468
469         cr0 = read_cr0();
470         cr0 |= X86_CR0_NW | X86_CR0_CD;
471         wbinvd();
472         write_cr0(cr0);
473         wbinvd();
474 }
475 void disable_caches(void) __attribute__((weak, alias("x86_disable_caches")));
476
477 int dcache_status(void)
478 {
479         return !(read_cr0() & X86_CR0_CD);
480 }
481
482 void cpu_enable_paging_pae(ulong cr3)
483 {
484         __asm__ __volatile__(
485                 /* Load the page table address */
486                 "movl   %0, %%cr3\n"
487                 /* Enable pae */
488                 "movl   %%cr4, %%eax\n"
489                 "orl    $0x00000020, %%eax\n"
490                 "movl   %%eax, %%cr4\n"
491                 /* Enable paging */
492                 "movl   %%cr0, %%eax\n"
493                 "orl    $0x80000000, %%eax\n"
494                 "movl   %%eax, %%cr0\n"
495                 :
496                 : "r" (cr3)
497                 : "eax");
498 }
499
500 void cpu_disable_paging_pae(void)
501 {
502         /* Turn off paging */
503         __asm__ __volatile__ (
504                 /* Disable paging */
505                 "movl   %%cr0, %%eax\n"
506                 "andl   $0x7fffffff, %%eax\n"
507                 "movl   %%eax, %%cr0\n"
508                 /* Disable pae */
509                 "movl   %%cr4, %%eax\n"
510                 "andl   $0xffffffdf, %%eax\n"
511                 "movl   %%eax, %%cr4\n"
512                 :
513                 :
514                 : "eax");
515 }
516
517 static bool can_detect_long_mode(void)
518 {
519         return cpuid_eax(0x80000000) > 0x80000000UL;
520 }
521
522 static bool has_long_mode(void)
523 {
524         return cpuid_edx(0x80000001) & (1 << 29) ? true : false;
525 }
526
527 int cpu_has_64bit(void)
528 {
529         return has_cpuid() && can_detect_long_mode() &&
530                 has_long_mode();
531 }
532
533 #define PAGETABLE_BASE          0x80000
534 #define PAGETABLE_SIZE          (6 * 4096)
535
536 /**
537  * build_pagetable() - build a flat 4GiB page table structure for 64-bti mode
538  *
539  * @pgtable: Pointer to a 24iKB block of memory
540  */
541 static void build_pagetable(uint32_t *pgtable)
542 {
543         uint i;
544
545         memset(pgtable, '\0', PAGETABLE_SIZE);
546
547         /* Level 4 needs a single entry */
548         pgtable[0] = (ulong)&pgtable[1024] + 7;
549
550         /* Level 3 has one 64-bit entry for each GiB of memory */
551         for (i = 0; i < 4; i++)
552                 pgtable[1024 + i * 2] = (ulong)&pgtable[2048] + 0x1000 * i + 7;
553
554         /* Level 2 has 2048 64-bit entries, each repesenting 2MiB */
555         for (i = 0; i < 2048; i++)
556                 pgtable[2048 + i * 2] = 0x183 + (i << 21UL);
557 }
558
559 int cpu_jump_to_64bit(ulong setup_base, ulong target)
560 {
561         uint32_t *pgtable;
562
563         pgtable = memalign(4096, PAGETABLE_SIZE);
564         if (!pgtable)
565                 return -ENOMEM;
566
567         build_pagetable(pgtable);
568         cpu_call64((ulong)pgtable, setup_base, target);
569         free(pgtable);
570
571         return -EFAULT;
572 }
573
574 /*
575  * Jump from SPL to U-Boot
576  *
577  * This function is work-in-progress with many issues to resolve.
578  *
579  * It works by setting up several regions:
580  *   ptr      - a place to put the code that jumps into 64-bit mode
581  *   gdt      - a place to put the global descriptor table
582  *   pgtable  - a place to put the page tables
583  *
584  * The cpu_call64() code is copied from ROM and then manually patched so that
585  * it has the correct GDT address in RAM. U-Boot is copied from ROM into
586  * its pre-relocation address. Then we jump to the cpu_call64() code in RAM,
587  * which changes to 64-bit mode and starts U-Boot.
588  */
589 int cpu_jump_to_64bit_uboot(ulong target)
590 {
591         typedef void (*func_t)(ulong pgtable, ulong setup_base, ulong target);
592         uint32_t *pgtable;
593         func_t func;
594         char *ptr;
595
596         pgtable = (uint32_t *)PAGETABLE_BASE;
597
598         build_pagetable(pgtable);
599
600         extern long call64_stub_size;
601         ptr = malloc(call64_stub_size);
602         if (!ptr) {
603                 printf("Failed to allocate the cpu_call64 stub\n");
604                 return -ENOMEM;
605         }
606         memcpy(ptr, cpu_call64, call64_stub_size);
607
608         func = (func_t)ptr;
609
610         /*
611          * Copy U-Boot from ROM
612          * TODO(sjg@chromium.org): Figure out a way to get the text base
613          * correctly here, and in the device-tree binman definition.
614          *
615          * Also consider using FIT so we get the correct image length and
616          * parameters.
617          */
618         memcpy((char *)target, (char *)0xfff00000, 0x100000);
619
620         /* Jump to U-Boot */
621         func((ulong)pgtable, 0, (ulong)target);
622
623         return -EFAULT;
624 }
625
626 #ifdef CONFIG_SMP
627 static int enable_smis(struct udevice *cpu, void *unused)
628 {
629         return 0;
630 }
631
632 static struct mp_flight_record mp_steps[] = {
633         MP_FR_BLOCK_APS(mp_init_cpu, NULL, mp_init_cpu, NULL),
634         /* Wait for APs to finish initialization before proceeding */
635         MP_FR_BLOCK_APS(NULL, NULL, enable_smis, NULL),
636 };
637
638 int x86_mp_init(void)
639 {
640         struct mp_params mp_params;
641
642         mp_params.parallel_microcode_load = 0,
643         mp_params.flight_plan = &mp_steps[0];
644         mp_params.num_records = ARRAY_SIZE(mp_steps);
645         mp_params.microcode_pointer = 0;
646
647         if (mp_init(&mp_params)) {
648                 printf("Warning: MP init failure\n");
649                 return -EIO;
650         }
651
652         return 0;
653 }
654 #endif