Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 16a624b | 2017-01-16 07:03:57 -0700 | [diff] [blame] | 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 |
Simon Glass | 16a624b | 2017-01-16 07:03:57 -0700 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | #include <common.h> |
Simon Glass | 1d91ba7 | 2019-11-14 12:57:37 -0700 | [diff] [blame] | 22 | #include <cpu_func.h> |
Simon Glass | 16a624b | 2017-01-16 07:03:57 -0700 | [diff] [blame] | 23 | #include <malloc.h> |
Simon Glass | dd45a7a | 2019-12-06 21:41:51 -0700 | [diff] [blame^] | 24 | #include <spl.h> |
Simon Glass | 16a624b | 2017-01-16 07:03:57 -0700 | [diff] [blame] | 25 | #include <asm/control_regs.h> |
| 26 | #include <asm/cpu.h> |
| 27 | #include <asm/mp.h> |
| 28 | #include <asm/msr.h> |
| 29 | #include <asm/mtrr.h> |
| 30 | #include <asm/processor-flags.h> |
| 31 | |
| 32 | DECLARE_GLOBAL_DATA_PTR; |
| 33 | |
| 34 | /* |
| 35 | * Constructor for a conventional segment GDT (or LDT) entry |
| 36 | * This is a macro so it can be used in initialisers |
| 37 | */ |
| 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))) |
| 44 | |
| 45 | struct gdt_ptr { |
| 46 | u16 len; |
| 47 | u32 ptr; |
| 48 | } __packed; |
| 49 | |
| 50 | struct cpu_device_id { |
| 51 | unsigned vendor; |
| 52 | unsigned device; |
| 53 | }; |
| 54 | |
| 55 | struct cpuinfo_x86 { |
| 56 | uint8_t x86; /* CPU family */ |
| 57 | uint8_t x86_vendor; /* CPU vendor */ |
| 58 | uint8_t x86_model; |
| 59 | uint8_t x86_mask; |
| 60 | }; |
| 61 | |
Simon Glass | dd45a7a | 2019-12-06 21:41:51 -0700 | [diff] [blame^] | 62 | /* gcc 7.3 does not wwant to drop x86_vendors, so use #ifdef */ |
| 63 | #ifndef CONFIG_TPL_BUILD |
Simon Glass | 16a624b | 2017-01-16 07:03:57 -0700 | [diff] [blame] | 64 | /* |
| 65 | * List of cpu vendor strings along with their normalized |
| 66 | * id values. |
| 67 | */ |
| 68 | static const struct { |
| 69 | int vendor; |
| 70 | const char *name; |
| 71 | } x86_vendors[] = { |
| 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 ", }, |
| 83 | }; |
Simon Glass | dd45a7a | 2019-12-06 21:41:51 -0700 | [diff] [blame^] | 84 | #endif |
Simon Glass | 16a624b | 2017-01-16 07:03:57 -0700 | [diff] [blame] | 85 | |
| 86 | static void load_ds(u32 segment) |
| 87 | { |
| 88 | asm volatile("movl %0, %%ds" : : "r" (segment * X86_GDT_ENTRY_SIZE)); |
| 89 | } |
| 90 | |
| 91 | static void load_es(u32 segment) |
| 92 | { |
| 93 | asm volatile("movl %0, %%es" : : "r" (segment * X86_GDT_ENTRY_SIZE)); |
| 94 | } |
| 95 | |
| 96 | static void load_fs(u32 segment) |
| 97 | { |
| 98 | asm volatile("movl %0, %%fs" : : "r" (segment * X86_GDT_ENTRY_SIZE)); |
| 99 | } |
| 100 | |
| 101 | static void load_gs(u32 segment) |
| 102 | { |
| 103 | asm volatile("movl %0, %%gs" : : "r" (segment * X86_GDT_ENTRY_SIZE)); |
| 104 | } |
| 105 | |
| 106 | static void load_ss(u32 segment) |
| 107 | { |
| 108 | asm volatile("movl %0, %%ss" : : "r" (segment * X86_GDT_ENTRY_SIZE)); |
| 109 | } |
| 110 | |
| 111 | static void load_gdt(const u64 *boot_gdt, u16 num_entries) |
| 112 | { |
| 113 | struct gdt_ptr gdt; |
| 114 | |
| 115 | gdt.len = (num_entries * X86_GDT_ENTRY_SIZE) - 1; |
| 116 | gdt.ptr = (ulong)boot_gdt; |
| 117 | |
| 118 | asm volatile("lgdtl %0\n" : : "m" (gdt)); |
| 119 | } |
| 120 | |
| 121 | void arch_setup_gd(gd_t *new_gd) |
| 122 | { |
| 123 | u64 *gdt_addr; |
| 124 | |
| 125 | gdt_addr = new_gd->arch.gdt; |
| 126 | |
| 127 | /* |
| 128 | * CS: code, read/execute, 4 GB, base 0 |
| 129 | * |
| 130 | * Some OS (like VxWorks) requires GDT entry 1 to be the 32-bit CS |
| 131 | */ |
| 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); |
| 134 | |
| 135 | /* DS: data, read/write, 4 GB, base 0 */ |
| 136 | gdt_addr[X86_GDT_ENTRY_32BIT_DS] = GDT_ENTRY(0xc093, 0, 0xfffff); |
| 137 | |
| 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); |
| 142 | |
| 143 | /* 16-bit CS: code, read/execute, 64 kB, base 0 */ |
| 144 | gdt_addr[X86_GDT_ENTRY_16BIT_CS] = GDT_ENTRY(0x009b, 0, 0x0ffff); |
| 145 | |
| 146 | /* 16-bit DS: data, read/write, 64 kB, base 0 */ |
| 147 | gdt_addr[X86_GDT_ENTRY_16BIT_DS] = GDT_ENTRY(0x0093, 0, 0x0ffff); |
| 148 | |
| 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); |
| 151 | |
| 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); |
| 158 | } |
| 159 | |
| 160 | #ifdef CONFIG_HAVE_FSP |
| 161 | /* |
| 162 | * Setup FSP execution environment GDT |
| 163 | * |
| 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. |
| 168 | */ |
| 169 | void setup_fsp_gdt(void) |
| 170 | { |
| 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); |
| 177 | } |
| 178 | #endif |
| 179 | |
| 180 | /* |
| 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. |
| 184 | */ |
| 185 | |
| 186 | /* |
| 187 | * Perform the Cyrix 5/2 test. A Cyrix won't change |
| 188 | * the flags, while other 486 chips will. |
| 189 | */ |
| 190 | static inline int test_cyrix_52div(void) |
| 191 | { |
| 192 | unsigned int test; |
| 193 | |
| 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 */ |
| 198 | : "=a" (test) |
| 199 | : "0" (5), "q" (2) |
| 200 | : "cc"); |
| 201 | |
| 202 | /* AH is 0x02 on Cyrix after the divide.. */ |
| 203 | return (unsigned char) (test >> 8) == 0x02; |
| 204 | } |
| 205 | |
Simon Glass | dd45a7a | 2019-12-06 21:41:51 -0700 | [diff] [blame^] | 206 | #ifndef CONFIG_TPL_BUILD |
Simon Glass | 16a624b | 2017-01-16 07:03:57 -0700 | [diff] [blame] | 207 | /* |
| 208 | * Detect a NexGen CPU running without BIOS hypercode new enough |
| 209 | * to have CPUID. (Thanks to Herbert Oppmann) |
| 210 | */ |
| 211 | static int deep_magic_nexgen_probe(void) |
| 212 | { |
| 213 | int ret; |
| 214 | |
| 215 | __asm__ __volatile__ ( |
| 216 | " movw $0x5555, %%ax\n" |
| 217 | " xorw %%dx,%%dx\n" |
| 218 | " movw $2, %%cx\n" |
| 219 | " divw %%cx\n" |
| 220 | " movl $0, %%eax\n" |
| 221 | " jnz 1f\n" |
| 222 | " movl $1, %%eax\n" |
| 223 | "1:\n" |
| 224 | : "=a" (ret) : : "cx", "dx"); |
| 225 | return ret; |
| 226 | } |
Simon Glass | dd45a7a | 2019-12-06 21:41:51 -0700 | [diff] [blame^] | 227 | #endif |
Simon Glass | 16a624b | 2017-01-16 07:03:57 -0700 | [diff] [blame] | 228 | |
| 229 | static bool has_cpuid(void) |
| 230 | { |
| 231 | return flag_is_changeable_p(X86_EFLAGS_ID); |
| 232 | } |
| 233 | |
| 234 | static bool has_mtrr(void) |
| 235 | { |
| 236 | return cpuid_edx(0x00000001) & (1 << 12) ? true : false; |
| 237 | } |
| 238 | |
Simon Glass | dd45a7a | 2019-12-06 21:41:51 -0700 | [diff] [blame^] | 239 | #ifndef CONFIG_TPL_BUILD |
Simon Glass | 16a624b | 2017-01-16 07:03:57 -0700 | [diff] [blame] | 240 | static int build_vendor_name(char *vendor_name) |
| 241 | { |
| 242 | struct cpuid_result result; |
| 243 | result = cpuid(0x00000000); |
| 244 | unsigned int *name_as_ints = (unsigned int *)vendor_name; |
| 245 | |
| 246 | name_as_ints[0] = result.ebx; |
| 247 | name_as_ints[1] = result.edx; |
| 248 | name_as_ints[2] = result.ecx; |
| 249 | |
| 250 | return result.eax; |
| 251 | } |
Simon Glass | dd45a7a | 2019-12-06 21:41:51 -0700 | [diff] [blame^] | 252 | #endif |
Simon Glass | 16a624b | 2017-01-16 07:03:57 -0700 | [diff] [blame] | 253 | |
| 254 | static void identify_cpu(struct cpu_device_id *cpu) |
| 255 | { |
Simon Glass | dd45a7a | 2019-12-06 21:41:51 -0700 | [diff] [blame^] | 256 | cpu->device = 0; /* fix gcc 4.4.4 warning */ |
| 257 | |
| 258 | /* |
| 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. |
| 261 | */ |
| 262 | if (spl_phase() == PHASE_TPL) { |
| 263 | struct cpuid_result result; |
| 264 | |
| 265 | result = cpuid(0x00000000); |
| 266 | switch (result.ecx >> 24) { |
| 267 | case 'l': /* GenuineIntel */ |
| 268 | cpu->vendor = X86_VENDOR_INTEL; |
| 269 | break; |
| 270 | case 'D': /* AuthenticAMD */ |
| 271 | cpu->vendor = X86_VENDOR_AMD; |
| 272 | break; |
| 273 | default: |
| 274 | cpu->vendor = X86_VENDOR_ANY; |
| 275 | break; |
| 276 | } |
| 277 | return; |
| 278 | } |
| 279 | |
| 280 | /* gcc 7.3 does not want to drop x86_vendors, so use #ifdef */ |
| 281 | #ifndef CONFIG_TPL_BUILD |
Simon Glass | 16a624b | 2017-01-16 07:03:57 -0700 | [diff] [blame] | 282 | char vendor_name[16]; |
| 283 | int i; |
| 284 | |
| 285 | vendor_name[0] = '\0'; /* Unset */ |
Simon Glass | 16a624b | 2017-01-16 07:03:57 -0700 | [diff] [blame] | 286 | |
| 287 | /* Find the id and vendor_name */ |
| 288 | if (!has_cpuid()) { |
| 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 */ |
| 292 | else |
| 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 */ |
| 297 | } |
| 298 | /* Detect NexGen with old hypercode */ |
| 299 | else if (deep_magic_nexgen_probe()) |
| 300 | memcpy(vendor_name, "NexGenDriven", 13); |
Simon Glass | dd45a7a | 2019-12-06 21:41:51 -0700 | [diff] [blame^] | 301 | } else { |
| 302 | int cpuid_level; |
Simon Glass | 16a624b | 2017-01-16 07:03:57 -0700 | [diff] [blame] | 303 | |
| 304 | cpuid_level = build_vendor_name(vendor_name); |
| 305 | vendor_name[12] = '\0'; |
| 306 | |
| 307 | /* Intel-defined flags: level 0x00000001 */ |
| 308 | if (cpuid_level >= 0x00000001) { |
| 309 | cpu->device = cpuid_eax(0x00000001); |
| 310 | } else { |
| 311 | /* Have CPUID level 0 only unheard of */ |
| 312 | cpu->device = 0x00000400; |
| 313 | } |
| 314 | } |
| 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; |
| 319 | break; |
| 320 | } |
| 321 | } |
Simon Glass | dd45a7a | 2019-12-06 21:41:51 -0700 | [diff] [blame^] | 322 | #endif |
Simon Glass | 16a624b | 2017-01-16 07:03:57 -0700 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | static inline void get_fms(struct cpuinfo_x86 *c, uint32_t tfms) |
| 326 | { |
| 327 | c->x86 = (tfms >> 8) & 0xf; |
| 328 | c->x86_model = (tfms >> 4) & 0xf; |
| 329 | c->x86_mask = tfms & 0xf; |
| 330 | if (c->x86 == 0xf) |
| 331 | c->x86 += (tfms >> 20) & 0xff; |
| 332 | if (c->x86 >= 0x6) |
| 333 | c->x86_model += ((tfms >> 16) & 0xF) << 4; |
| 334 | } |
| 335 | |
| 336 | u32 cpu_get_family_model(void) |
| 337 | { |
| 338 | return gd->arch.x86_device & 0x0fff0ff0; |
| 339 | } |
| 340 | |
| 341 | u32 cpu_get_stepping(void) |
| 342 | { |
| 343 | return gd->arch.x86_mask; |
| 344 | } |
| 345 | |
Simon Glass | 05e12f7 | 2019-04-25 21:58:42 -0600 | [diff] [blame] | 346 | /* initialise FPU, reset EM, set MP and NE */ |
| 347 | static void setup_cpu_features(void) |
Simon Glass | 16a624b | 2017-01-16 07:03:57 -0700 | [diff] [blame] | 348 | { |
| 349 | const u32 em_rst = ~X86_CR0_EM; |
| 350 | const u32 mp_ne_set = X86_CR0_MP | X86_CR0_NE; |
| 351 | |
Simon Glass | 05e12f7 | 2019-04-25 21:58:42 -0600 | [diff] [blame] | 352 | asm ("fninit\n" \ |
| 353 | "movl %%cr0, %%eax\n" \ |
| 354 | "andl %0, %%eax\n" \ |
| 355 | "orl %1, %%eax\n" \ |
| 356 | "movl %%eax, %%cr0\n" \ |
| 357 | : : "i" (em_rst), "i" (mp_ne_set) : "eax"); |
| 358 | } |
Simon Glass | 16a624b | 2017-01-16 07:03:57 -0700 | [diff] [blame] | 359 | |
Simon Glass | 05e12f7 | 2019-04-25 21:58:42 -0600 | [diff] [blame] | 360 | static void setup_identity(void) |
| 361 | { |
Simon Glass | 16a624b | 2017-01-16 07:03:57 -0700 | [diff] [blame] | 362 | /* identify CPU via cpuid and store the decoded info into gd->arch */ |
| 363 | if (has_cpuid()) { |
| 364 | struct cpu_device_id cpu; |
| 365 | struct cpuinfo_x86 c; |
| 366 | |
| 367 | identify_cpu(&cpu); |
| 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; |
| 374 | |
| 375 | gd->arch.has_mtrr = has_mtrr(); |
| 376 | } |
Simon Glass | 05e12f7 | 2019-04-25 21:58:42 -0600 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | /* Don't allow PCI region 3 to use memory in the 2-4GB memory hole */ |
| 380 | static void setup_pci_ram_top(void) |
| 381 | { |
Simon Glass | 16a624b | 2017-01-16 07:03:57 -0700 | [diff] [blame] | 382 | gd->pci_ram_top = 0x80000000U; |
Simon Glass | 05e12f7 | 2019-04-25 21:58:42 -0600 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | static void setup_mtrr(void) |
| 386 | { |
| 387 | u64 mtrr_cap; |
Simon Glass | 16a624b | 2017-01-16 07:03:57 -0700 | [diff] [blame] | 388 | |
| 389 | /* Configure fixed range MTRRs for some legacy regions */ |
Simon Glass | 05e12f7 | 2019-04-25 21:58:42 -0600 | [diff] [blame] | 390 | if (!gd->arch.has_mtrr) |
| 391 | return; |
Simon Glass | 16a624b | 2017-01-16 07:03:57 -0700 | [diff] [blame] | 392 | |
Simon Glass | 05e12f7 | 2019-04-25 21:58:42 -0600 | [diff] [blame] | 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)); |
Simon Glass | 16a624b | 2017-01-16 07:03:57 -0700 | [diff] [blame] | 399 | |
Simon Glass | 05e12f7 | 2019-04-25 21:58:42 -0600 | [diff] [blame] | 400 | /* |
| 401 | * Mark the PCI ROM area as cacheable to improve ROM |
| 402 | * execution performance. |
| 403 | */ |
| 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)); |
Simon Glass | 16a624b | 2017-01-16 07:03:57 -0700 | [diff] [blame] | 416 | |
Simon Glass | 05e12f7 | 2019-04-25 21:58:42 -0600 | [diff] [blame] | 417 | /* Enable the fixed range MTRRs */ |
| 418 | msr_setbits_64(MTRR_DEF_TYPE_MSR, MTRR_DEF_TYPE_FIX_EN); |
Simon Glass | 16a624b | 2017-01-16 07:03:57 -0700 | [diff] [blame] | 419 | } |
Simon Glass | 05e12f7 | 2019-04-25 21:58:42 -0600 | [diff] [blame] | 420 | } |
Simon Glass | 16a624b | 2017-01-16 07:03:57 -0700 | [diff] [blame] | 421 | |
Simon Glass | dc44467 | 2019-10-20 21:37:54 -0600 | [diff] [blame] | 422 | int x86_cpu_init_tpl(void) |
| 423 | { |
| 424 | setup_cpu_features(); |
| 425 | setup_identity(); |
| 426 | |
| 427 | return 0; |
| 428 | } |
| 429 | |
Simon Glass | 05e12f7 | 2019-04-25 21:58:42 -0600 | [diff] [blame] | 430 | int x86_cpu_init_f(void) |
| 431 | { |
| 432 | if (ll_boot_init()) |
| 433 | setup_cpu_features(); |
| 434 | setup_identity(); |
| 435 | setup_mtrr(); |
| 436 | setup_pci_ram_top(); |
| 437 | |
Simon Glass | 16a624b | 2017-01-16 07:03:57 -0700 | [diff] [blame] | 438 | /* Set up the i8254 timer if required */ |
Simon Glass | 05e12f7 | 2019-04-25 21:58:42 -0600 | [diff] [blame] | 439 | if (IS_ENABLED(CONFIG_I8254_TIMER)) |
| 440 | i8254_init(); |
| 441 | |
| 442 | return 0; |
| 443 | } |
| 444 | |
| 445 | int x86_cpu_reinit_f(void) |
| 446 | { |
| 447 | setup_identity(); |
| 448 | setup_pci_ram_top(); |
Simon Glass | 16a624b | 2017-01-16 07:03:57 -0700 | [diff] [blame] | 449 | |
| 450 | return 0; |
| 451 | } |
| 452 | |
| 453 | void x86_enable_caches(void) |
| 454 | { |
| 455 | unsigned long cr0; |
| 456 | |
| 457 | cr0 = read_cr0(); |
| 458 | cr0 &= ~(X86_CR0_NW | X86_CR0_CD); |
| 459 | write_cr0(cr0); |
| 460 | wbinvd(); |
| 461 | } |
| 462 | void enable_caches(void) __attribute__((weak, alias("x86_enable_caches"))); |
| 463 | |
| 464 | void x86_disable_caches(void) |
| 465 | { |
| 466 | unsigned long cr0; |
| 467 | |
| 468 | cr0 = read_cr0(); |
| 469 | cr0 |= X86_CR0_NW | X86_CR0_CD; |
| 470 | wbinvd(); |
| 471 | write_cr0(cr0); |
| 472 | wbinvd(); |
| 473 | } |
| 474 | void disable_caches(void) __attribute__((weak, alias("x86_disable_caches"))); |
| 475 | |
| 476 | int dcache_status(void) |
| 477 | { |
| 478 | return !(read_cr0() & X86_CR0_CD); |
| 479 | } |
| 480 | |
| 481 | void cpu_enable_paging_pae(ulong cr3) |
| 482 | { |
| 483 | __asm__ __volatile__( |
| 484 | /* Load the page table address */ |
| 485 | "movl %0, %%cr3\n" |
| 486 | /* Enable pae */ |
| 487 | "movl %%cr4, %%eax\n" |
| 488 | "orl $0x00000020, %%eax\n" |
| 489 | "movl %%eax, %%cr4\n" |
| 490 | /* Enable paging */ |
| 491 | "movl %%cr0, %%eax\n" |
| 492 | "orl $0x80000000, %%eax\n" |
| 493 | "movl %%eax, %%cr0\n" |
| 494 | : |
| 495 | : "r" (cr3) |
| 496 | : "eax"); |
| 497 | } |
| 498 | |
| 499 | void cpu_disable_paging_pae(void) |
| 500 | { |
| 501 | /* Turn off paging */ |
| 502 | __asm__ __volatile__ ( |
| 503 | /* Disable paging */ |
| 504 | "movl %%cr0, %%eax\n" |
| 505 | "andl $0x7fffffff, %%eax\n" |
| 506 | "movl %%eax, %%cr0\n" |
| 507 | /* Disable pae */ |
| 508 | "movl %%cr4, %%eax\n" |
| 509 | "andl $0xffffffdf, %%eax\n" |
| 510 | "movl %%eax, %%cr4\n" |
| 511 | : |
| 512 | : |
| 513 | : "eax"); |
| 514 | } |
| 515 | |
| 516 | static bool can_detect_long_mode(void) |
| 517 | { |
| 518 | return cpuid_eax(0x80000000) > 0x80000000UL; |
| 519 | } |
| 520 | |
| 521 | static bool has_long_mode(void) |
| 522 | { |
| 523 | return cpuid_edx(0x80000001) & (1 << 29) ? true : false; |
| 524 | } |
| 525 | |
| 526 | int cpu_has_64bit(void) |
| 527 | { |
| 528 | return has_cpuid() && can_detect_long_mode() && |
| 529 | has_long_mode(); |
| 530 | } |
| 531 | |
Bin Meng | 6aac2ca | 2019-01-31 08:22:12 -0800 | [diff] [blame] | 532 | #define PAGETABLE_BASE 0x80000 |
Simon Glass | 16a624b | 2017-01-16 07:03:57 -0700 | [diff] [blame] | 533 | #define PAGETABLE_SIZE (6 * 4096) |
| 534 | |
| 535 | /** |
| 536 | * build_pagetable() - build a flat 4GiB page table structure for 64-bti mode |
| 537 | * |
| 538 | * @pgtable: Pointer to a 24iKB block of memory |
| 539 | */ |
| 540 | static void build_pagetable(uint32_t *pgtable) |
| 541 | { |
| 542 | uint i; |
| 543 | |
| 544 | memset(pgtable, '\0', PAGETABLE_SIZE); |
| 545 | |
| 546 | /* Level 4 needs a single entry */ |
| 547 | pgtable[0] = (ulong)&pgtable[1024] + 7; |
| 548 | |
| 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; |
| 552 | |
| 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); |
| 556 | } |
| 557 | |
| 558 | int cpu_jump_to_64bit(ulong setup_base, ulong target) |
| 559 | { |
| 560 | uint32_t *pgtable; |
| 561 | |
| 562 | pgtable = memalign(4096, PAGETABLE_SIZE); |
| 563 | if (!pgtable) |
| 564 | return -ENOMEM; |
| 565 | |
| 566 | build_pagetable(pgtable); |
| 567 | cpu_call64((ulong)pgtable, setup_base, target); |
| 568 | free(pgtable); |
| 569 | |
| 570 | return -EFAULT; |
| 571 | } |
| 572 | |
Simon Glass | 1e32ede | 2017-01-16 07:04:15 -0700 | [diff] [blame] | 573 | /* |
| 574 | * Jump from SPL to U-Boot |
| 575 | * |
| 576 | * This function is work-in-progress with many issues to resolve. |
| 577 | * |
| 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 |
| 582 | * |
| 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. |
| 587 | */ |
| 588 | int cpu_jump_to_64bit_uboot(ulong target) |
| 589 | { |
| 590 | typedef void (*func_t)(ulong pgtable, ulong setup_base, ulong target); |
| 591 | uint32_t *pgtable; |
| 592 | func_t func; |
Bin Meng | 17b7b60 | 2019-01-31 08:22:13 -0800 | [diff] [blame] | 593 | char *ptr; |
Simon Glass | 1e32ede | 2017-01-16 07:04:15 -0700 | [diff] [blame] | 594 | |
Bin Meng | 6aac2ca | 2019-01-31 08:22:12 -0800 | [diff] [blame] | 595 | pgtable = (uint32_t *)PAGETABLE_BASE; |
Simon Glass | 1e32ede | 2017-01-16 07:04:15 -0700 | [diff] [blame] | 596 | |
| 597 | build_pagetable(pgtable); |
| 598 | |
Bin Meng | 17b7b60 | 2019-01-31 08:22:13 -0800 | [diff] [blame] | 599 | extern long call64_stub_size; |
| 600 | ptr = malloc(call64_stub_size); |
| 601 | if (!ptr) { |
| 602 | printf("Failed to allocate the cpu_call64 stub\n"); |
| 603 | return -ENOMEM; |
| 604 | } |
Bin Meng | 17b7b60 | 2019-01-31 08:22:13 -0800 | [diff] [blame] | 605 | memcpy(ptr, cpu_call64, call64_stub_size); |
Simon Glass | 1e32ede | 2017-01-16 07:04:15 -0700 | [diff] [blame] | 606 | |
Simon Glass | 1e32ede | 2017-01-16 07:04:15 -0700 | [diff] [blame] | 607 | func = (func_t)ptr; |
Simon Glass | 1e32ede | 2017-01-16 07:04:15 -0700 | [diff] [blame] | 608 | |
| 609 | /* |
| 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. |
| 613 | * |
| 614 | * Also consider using FIT so we get the correct image length and |
| 615 | * parameters. |
| 616 | */ |
| 617 | memcpy((char *)target, (char *)0xfff00000, 0x100000); |
| 618 | |
| 619 | /* Jump to U-Boot */ |
| 620 | func((ulong)pgtable, 0, (ulong)target); |
| 621 | |
| 622 | return -EFAULT; |
| 623 | } |
| 624 | |
Simon Glass | 16a624b | 2017-01-16 07:03:57 -0700 | [diff] [blame] | 625 | #ifdef CONFIG_SMP |
| 626 | static int enable_smis(struct udevice *cpu, void *unused) |
| 627 | { |
| 628 | return 0; |
| 629 | } |
| 630 | |
| 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), |
| 635 | }; |
| 636 | |
| 637 | int x86_mp_init(void) |
| 638 | { |
| 639 | struct mp_params mp_params; |
| 640 | |
| 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; |
| 645 | |
| 646 | if (mp_init(&mp_params)) { |
| 647 | printf("Warning: MP init failure\n"); |
| 648 | return -EIO; |
| 649 | } |
| 650 | |
| 651 | return 0; |
| 652 | } |
| 653 | #endif |