cpu.c 14 KB

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