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