cpu.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  1. /*
  2. * (C) Copyright 2008-2011
  3. * Graeme Russ, <graeme.russ@gmail.com>
  4. *
  5. * (C) Copyright 2002
  6. * Daniel Engström, Omicron Ceti AB, <daniel@omicron.se>
  7. *
  8. * (C) Copyright 2002
  9. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  10. * Marius Groeger <mgroeger@sysgo.de>
  11. *
  12. * (C) Copyright 2002
  13. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  14. * Alex Zuepke <azu@sysgo.de>
  15. *
  16. * Part of this file is adapted from coreboot
  17. * src/arch/x86/lib/cpu.c
  18. *
  19. * SPDX-License-Identifier: GPL-2.0+
  20. */
  21. #include <common.h>
  22. #include <command.h>
  23. #include <dm.h>
  24. #include <errno.h>
  25. #include <malloc.h>
  26. #include <asm/control_regs.h>
  27. #include <asm/coreboot_tables.h>
  28. #include <asm/cpu.h>
  29. #include <asm/lapic.h>
  30. #include <asm/microcode.h>
  31. #include <asm/mp.h>
  32. #include <asm/mrccache.h>
  33. #include <asm/msr.h>
  34. #include <asm/mtrr.h>
  35. #include <asm/post.h>
  36. #include <asm/processor.h>
  37. #include <asm/processor-flags.h>
  38. #include <asm/interrupt.h>
  39. #include <asm/tables.h>
  40. #include <linux/compiler.h>
  41. DECLARE_GLOBAL_DATA_PTR;
  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. struct gdt_ptr {
  53. u16 len;
  54. u32 ptr;
  55. } __packed;
  56. struct cpu_device_id {
  57. unsigned vendor;
  58. unsigned device;
  59. };
  60. struct cpuinfo_x86 {
  61. uint8_t x86; /* CPU family */
  62. uint8_t x86_vendor; /* CPU vendor */
  63. uint8_t x86_model;
  64. uint8_t x86_mask;
  65. };
  66. /*
  67. * List of cpu vendor strings along with their normalized
  68. * id values.
  69. */
  70. static const struct {
  71. int vendor;
  72. const char *name;
  73. } x86_vendors[] = {
  74. { X86_VENDOR_INTEL, "GenuineIntel", },
  75. { X86_VENDOR_CYRIX, "CyrixInstead", },
  76. { X86_VENDOR_AMD, "AuthenticAMD", },
  77. { X86_VENDOR_UMC, "UMC UMC UMC ", },
  78. { X86_VENDOR_NEXGEN, "NexGenDriven", },
  79. { X86_VENDOR_CENTAUR, "CentaurHauls", },
  80. { X86_VENDOR_RISE, "RiseRiseRise", },
  81. { X86_VENDOR_TRANSMETA, "GenuineTMx86", },
  82. { X86_VENDOR_TRANSMETA, "TransmetaCPU", },
  83. { X86_VENDOR_NSC, "Geode by NSC", },
  84. { X86_VENDOR_SIS, "SiS SiS SiS ", },
  85. };
  86. static const char *const x86_vendor_name[] = {
  87. [X86_VENDOR_INTEL] = "Intel",
  88. [X86_VENDOR_CYRIX] = "Cyrix",
  89. [X86_VENDOR_AMD] = "AMD",
  90. [X86_VENDOR_UMC] = "UMC",
  91. [X86_VENDOR_NEXGEN] = "NexGen",
  92. [X86_VENDOR_CENTAUR] = "Centaur",
  93. [X86_VENDOR_RISE] = "Rise",
  94. [X86_VENDOR_TRANSMETA] = "Transmeta",
  95. [X86_VENDOR_NSC] = "NSC",
  96. [X86_VENDOR_SIS] = "SiS",
  97. };
  98. static void load_ds(u32 segment)
  99. {
  100. asm volatile("movl %0, %%ds" : : "r" (segment * X86_GDT_ENTRY_SIZE));
  101. }
  102. static void load_es(u32 segment)
  103. {
  104. asm volatile("movl %0, %%es" : : "r" (segment * X86_GDT_ENTRY_SIZE));
  105. }
  106. static void load_fs(u32 segment)
  107. {
  108. asm volatile("movl %0, %%fs" : : "r" (segment * X86_GDT_ENTRY_SIZE));
  109. }
  110. static void load_gs(u32 segment)
  111. {
  112. asm volatile("movl %0, %%gs" : : "r" (segment * X86_GDT_ENTRY_SIZE));
  113. }
  114. static void load_ss(u32 segment)
  115. {
  116. asm volatile("movl %0, %%ss" : : "r" (segment * X86_GDT_ENTRY_SIZE));
  117. }
  118. static void load_gdt(const u64 *boot_gdt, u16 num_entries)
  119. {
  120. struct gdt_ptr gdt;
  121. gdt.len = (num_entries * X86_GDT_ENTRY_SIZE) - 1;
  122. gdt.ptr = (u32)boot_gdt;
  123. asm volatile("lgdtl %0\n" : : "m" (gdt));
  124. }
  125. void arch_setup_gd(gd_t *new_gd)
  126. {
  127. u64 *gdt_addr;
  128. gdt_addr = new_gd->arch.gdt;
  129. /*
  130. * CS: code, read/execute, 4 GB, base 0
  131. *
  132. * Some OS (like VxWorks) requires GDT entry 1 to be the 32-bit CS
  133. */
  134. gdt_addr[X86_GDT_ENTRY_UNUSED] = GDT_ENTRY(0xc09b, 0, 0xfffff);
  135. gdt_addr[X86_GDT_ENTRY_32BIT_CS] = GDT_ENTRY(0xc09b, 0, 0xfffff);
  136. /* DS: data, read/write, 4 GB, base 0 */
  137. gdt_addr[X86_GDT_ENTRY_32BIT_DS] = GDT_ENTRY(0xc093, 0, 0xfffff);
  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. /* 16-bit CS: code, read/execute, 64 kB, base 0 */
  143. gdt_addr[X86_GDT_ENTRY_16BIT_CS] = GDT_ENTRY(0x009b, 0, 0x0ffff);
  144. /* 16-bit DS: data, read/write, 64 kB, base 0 */
  145. gdt_addr[X86_GDT_ENTRY_16BIT_DS] = GDT_ENTRY(0x0093, 0, 0x0ffff);
  146. gdt_addr[X86_GDT_ENTRY_16BIT_FLAT_CS] = GDT_ENTRY(0x809b, 0, 0xfffff);
  147. gdt_addr[X86_GDT_ENTRY_16BIT_FLAT_DS] = GDT_ENTRY(0x8093, 0, 0xfffff);
  148. load_gdt(gdt_addr, X86_GDT_NUM_ENTRIES);
  149. load_ds(X86_GDT_ENTRY_32BIT_DS);
  150. load_es(X86_GDT_ENTRY_32BIT_DS);
  151. load_gs(X86_GDT_ENTRY_32BIT_DS);
  152. load_ss(X86_GDT_ENTRY_32BIT_DS);
  153. load_fs(X86_GDT_ENTRY_32BIT_FS);
  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. int __weak x86_cleanup_before_linux(void)
  175. {
  176. #ifdef CONFIG_BOOTSTAGE_STASH
  177. bootstage_stash((void *)CONFIG_BOOTSTAGE_STASH_ADDR,
  178. CONFIG_BOOTSTAGE_STASH_SIZE);
  179. #endif
  180. return 0;
  181. }
  182. /*
  183. * Cyrix CPUs without cpuid or with cpuid not yet enabled can be detected
  184. * by the fact that they preserve the flags across the division of 5/2.
  185. * PII and PPro exhibit this behavior too, but they have cpuid available.
  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. __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. /* AH is 0x02 on Cyrix after the divide.. */
  202. return (unsigned char) (test >> 8) == 0x02;
  203. }
  204. /*
  205. * Detect a NexGen CPU running without BIOS hypercode new enough
  206. * to have CPUID. (Thanks to Herbert Oppmann)
  207. */
  208. static int deep_magic_nexgen_probe(void)
  209. {
  210. int ret;
  211. __asm__ __volatile__ (
  212. " movw $0x5555, %%ax\n"
  213. " xorw %%dx,%%dx\n"
  214. " movw $2, %%cx\n"
  215. " divw %%cx\n"
  216. " movl $0, %%eax\n"
  217. " jnz 1f\n"
  218. " movl $1, %%eax\n"
  219. "1:\n"
  220. : "=a" (ret) : : "cx", "dx");
  221. return ret;
  222. }
  223. static bool has_cpuid(void)
  224. {
  225. return flag_is_changeable_p(X86_EFLAGS_ID);
  226. }
  227. static bool has_mtrr(void)
  228. {
  229. return cpuid_edx(0x00000001) & (1 << 12) ? true : false;
  230. }
  231. static int build_vendor_name(char *vendor_name)
  232. {
  233. struct cpuid_result result;
  234. result = cpuid(0x00000000);
  235. unsigned int *name_as_ints = (unsigned int *)vendor_name;
  236. name_as_ints[0] = result.ebx;
  237. name_as_ints[1] = result.edx;
  238. name_as_ints[2] = result.ecx;
  239. return result.eax;
  240. }
  241. static void identify_cpu(struct cpu_device_id *cpu)
  242. {
  243. char vendor_name[16];
  244. int i;
  245. vendor_name[0] = '\0'; /* Unset */
  246. cpu->device = 0; /* fix gcc 4.4.4 warning */
  247. /* Find the id and vendor_name */
  248. if (!has_cpuid()) {
  249. /* Its a 486 if we can modify the AC flag */
  250. if (flag_is_changeable_p(X86_EFLAGS_AC))
  251. cpu->device = 0x00000400; /* 486 */
  252. else
  253. cpu->device = 0x00000300; /* 386 */
  254. if ((cpu->device == 0x00000400) && test_cyrix_52div()) {
  255. memcpy(vendor_name, "CyrixInstead", 13);
  256. /* If we ever care we can enable cpuid here */
  257. }
  258. /* Detect NexGen with old hypercode */
  259. else if (deep_magic_nexgen_probe())
  260. memcpy(vendor_name, "NexGenDriven", 13);
  261. }
  262. if (has_cpuid()) {
  263. int cpuid_level;
  264. cpuid_level = build_vendor_name(vendor_name);
  265. vendor_name[12] = '\0';
  266. /* Intel-defined flags: level 0x00000001 */
  267. if (cpuid_level >= 0x00000001) {
  268. cpu->device = cpuid_eax(0x00000001);
  269. } else {
  270. /* Have CPUID level 0 only unheard of */
  271. cpu->device = 0x00000400;
  272. }
  273. }
  274. cpu->vendor = X86_VENDOR_UNKNOWN;
  275. for (i = 0; i < ARRAY_SIZE(x86_vendors); i++) {
  276. if (memcmp(vendor_name, x86_vendors[i].name, 12) == 0) {
  277. cpu->vendor = x86_vendors[i].vendor;
  278. break;
  279. }
  280. }
  281. }
  282. static inline void get_fms(struct cpuinfo_x86 *c, uint32_t tfms)
  283. {
  284. c->x86 = (tfms >> 8) & 0xf;
  285. c->x86_model = (tfms >> 4) & 0xf;
  286. c->x86_mask = tfms & 0xf;
  287. if (c->x86 == 0xf)
  288. c->x86 += (tfms >> 20) & 0xff;
  289. if (c->x86 >= 0x6)
  290. c->x86_model += ((tfms >> 16) & 0xF) << 4;
  291. }
  292. u32 cpu_get_family_model(void)
  293. {
  294. return gd->arch.x86_device & 0x0fff0ff0;
  295. }
  296. u32 cpu_get_stepping(void)
  297. {
  298. return gd->arch.x86_mask;
  299. }
  300. int x86_cpu_init_f(void)
  301. {
  302. const u32 em_rst = ~X86_CR0_EM;
  303. const u32 mp_ne_set = X86_CR0_MP | X86_CR0_NE;
  304. if (ll_boot_init()) {
  305. /* initialize FPU, reset EM, set MP and NE */
  306. asm ("fninit\n" \
  307. "movl %%cr0, %%eax\n" \
  308. "andl %0, %%eax\n" \
  309. "orl %1, %%eax\n" \
  310. "movl %%eax, %%cr0\n" \
  311. : : "i" (em_rst), "i" (mp_ne_set) : "eax");
  312. }
  313. /* identify CPU via cpuid and store the decoded info into gd->arch */
  314. if (has_cpuid()) {
  315. struct cpu_device_id cpu;
  316. struct cpuinfo_x86 c;
  317. identify_cpu(&cpu);
  318. get_fms(&c, cpu.device);
  319. gd->arch.x86 = c.x86;
  320. gd->arch.x86_vendor = cpu.vendor;
  321. gd->arch.x86_model = c.x86_model;
  322. gd->arch.x86_mask = c.x86_mask;
  323. gd->arch.x86_device = cpu.device;
  324. gd->arch.has_mtrr = has_mtrr();
  325. }
  326. /* Don't allow PCI region 3 to use memory in the 2-4GB memory hole */
  327. gd->pci_ram_top = 0x80000000U;
  328. /* Configure fixed range MTRRs for some legacy regions */
  329. if (gd->arch.has_mtrr) {
  330. u64 mtrr_cap;
  331. mtrr_cap = native_read_msr(MTRR_CAP_MSR);
  332. if (mtrr_cap & MTRR_CAP_FIX) {
  333. /* Mark the VGA RAM area as uncacheable */
  334. native_write_msr(MTRR_FIX_16K_A0000_MSR,
  335. MTRR_FIX_TYPE(MTRR_TYPE_UNCACHEABLE),
  336. MTRR_FIX_TYPE(MTRR_TYPE_UNCACHEABLE));
  337. /*
  338. * Mark the PCI ROM area as cacheable to improve ROM
  339. * execution performance.
  340. */
  341. native_write_msr(MTRR_FIX_4K_C0000_MSR,
  342. MTRR_FIX_TYPE(MTRR_TYPE_WRBACK),
  343. MTRR_FIX_TYPE(MTRR_TYPE_WRBACK));
  344. native_write_msr(MTRR_FIX_4K_C8000_MSR,
  345. MTRR_FIX_TYPE(MTRR_TYPE_WRBACK),
  346. MTRR_FIX_TYPE(MTRR_TYPE_WRBACK));
  347. native_write_msr(MTRR_FIX_4K_D0000_MSR,
  348. MTRR_FIX_TYPE(MTRR_TYPE_WRBACK),
  349. MTRR_FIX_TYPE(MTRR_TYPE_WRBACK));
  350. native_write_msr(MTRR_FIX_4K_D8000_MSR,
  351. MTRR_FIX_TYPE(MTRR_TYPE_WRBACK),
  352. MTRR_FIX_TYPE(MTRR_TYPE_WRBACK));
  353. /* Enable the fixed range MTRRs */
  354. msr_setbits_64(MTRR_DEF_TYPE_MSR, MTRR_DEF_TYPE_FIX_EN);
  355. }
  356. }
  357. #ifdef CONFIG_I8254_TIMER
  358. /* Set up the i8254 timer if required */
  359. i8254_init();
  360. #endif
  361. return 0;
  362. }
  363. void x86_enable_caches(void)
  364. {
  365. unsigned long cr0;
  366. cr0 = read_cr0();
  367. cr0 &= ~(X86_CR0_NW | X86_CR0_CD);
  368. write_cr0(cr0);
  369. wbinvd();
  370. }
  371. void enable_caches(void) __attribute__((weak, alias("x86_enable_caches")));
  372. void x86_disable_caches(void)
  373. {
  374. unsigned long cr0;
  375. cr0 = read_cr0();
  376. cr0 |= X86_CR0_NW | X86_CR0_CD;
  377. wbinvd();
  378. write_cr0(cr0);
  379. wbinvd();
  380. }
  381. void disable_caches(void) __attribute__((weak, alias("x86_disable_caches")));
  382. int x86_init_cache(void)
  383. {
  384. enable_caches();
  385. return 0;
  386. }
  387. int init_cache(void) __attribute__((weak, alias("x86_init_cache")));
  388. int do_reset(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  389. {
  390. printf("resetting ...\n");
  391. /* wait 50 ms */
  392. udelay(50000);
  393. disable_interrupts();
  394. reset_cpu(0);
  395. /*NOTREACHED*/
  396. return 0;
  397. }
  398. void flush_cache(unsigned long dummy1, unsigned long dummy2)
  399. {
  400. asm("wbinvd\n");
  401. }
  402. __weak void reset_cpu(ulong addr)
  403. {
  404. /* Do a hard reset through the chipset's reset control register */
  405. outb(SYS_RST | RST_CPU, IO_PORT_RESET);
  406. for (;;)
  407. cpu_hlt();
  408. }
  409. void x86_full_reset(void)
  410. {
  411. outb(FULL_RST | SYS_RST | RST_CPU, IO_PORT_RESET);
  412. }
  413. int dcache_status(void)
  414. {
  415. return !(read_cr0() & X86_CR0_CD);
  416. }
  417. /* Define these functions to allow ehch-hcd to function */
  418. void flush_dcache_range(unsigned long start, unsigned long stop)
  419. {
  420. }
  421. void invalidate_dcache_range(unsigned long start, unsigned long stop)
  422. {
  423. }
  424. void dcache_enable(void)
  425. {
  426. enable_caches();
  427. }
  428. void dcache_disable(void)
  429. {
  430. disable_caches();
  431. }
  432. void icache_enable(void)
  433. {
  434. }
  435. void icache_disable(void)
  436. {
  437. }
  438. int icache_status(void)
  439. {
  440. return 1;
  441. }
  442. void cpu_enable_paging_pae(ulong cr3)
  443. {
  444. __asm__ __volatile__(
  445. /* Load the page table address */
  446. "movl %0, %%cr3\n"
  447. /* Enable pae */
  448. "movl %%cr4, %%eax\n"
  449. "orl $0x00000020, %%eax\n"
  450. "movl %%eax, %%cr4\n"
  451. /* Enable paging */
  452. "movl %%cr0, %%eax\n"
  453. "orl $0x80000000, %%eax\n"
  454. "movl %%eax, %%cr0\n"
  455. :
  456. : "r" (cr3)
  457. : "eax");
  458. }
  459. void cpu_disable_paging_pae(void)
  460. {
  461. /* Turn off paging */
  462. __asm__ __volatile__ (
  463. /* Disable paging */
  464. "movl %%cr0, %%eax\n"
  465. "andl $0x7fffffff, %%eax\n"
  466. "movl %%eax, %%cr0\n"
  467. /* Disable pae */
  468. "movl %%cr4, %%eax\n"
  469. "andl $0xffffffdf, %%eax\n"
  470. "movl %%eax, %%cr4\n"
  471. :
  472. :
  473. : "eax");
  474. }
  475. static bool can_detect_long_mode(void)
  476. {
  477. return cpuid_eax(0x80000000) > 0x80000000UL;
  478. }
  479. static bool has_long_mode(void)
  480. {
  481. return cpuid_edx(0x80000001) & (1 << 29) ? true : false;
  482. }
  483. int cpu_has_64bit(void)
  484. {
  485. return has_cpuid() && can_detect_long_mode() &&
  486. has_long_mode();
  487. }
  488. const char *cpu_vendor_name(int vendor)
  489. {
  490. const char *name;
  491. name = "<invalid cpu vendor>";
  492. if ((vendor < (ARRAY_SIZE(x86_vendor_name))) &&
  493. (x86_vendor_name[vendor] != 0))
  494. name = x86_vendor_name[vendor];
  495. return name;
  496. }
  497. char *cpu_get_name(char *name)
  498. {
  499. unsigned int *name_as_ints = (unsigned int *)name;
  500. struct cpuid_result regs;
  501. char *ptr;
  502. int i;
  503. /* This bit adds up to 48 bytes */
  504. for (i = 0; i < 3; i++) {
  505. regs = cpuid(0x80000002 + i);
  506. name_as_ints[i * 4 + 0] = regs.eax;
  507. name_as_ints[i * 4 + 1] = regs.ebx;
  508. name_as_ints[i * 4 + 2] = regs.ecx;
  509. name_as_ints[i * 4 + 3] = regs.edx;
  510. }
  511. name[CPU_MAX_NAME_LEN - 1] = '\0';
  512. /* Skip leading spaces. */
  513. ptr = name;
  514. while (*ptr == ' ')
  515. ptr++;
  516. return ptr;
  517. }
  518. int default_print_cpuinfo(void)
  519. {
  520. printf("CPU: %s, vendor %s, device %xh\n",
  521. cpu_has_64bit() ? "x86_64" : "x86",
  522. cpu_vendor_name(gd->arch.x86_vendor), gd->arch.x86_device);
  523. return 0;
  524. }
  525. #define PAGETABLE_SIZE (6 * 4096)
  526. /**
  527. * build_pagetable() - build a flat 4GiB page table structure for 64-bti mode
  528. *
  529. * @pgtable: Pointer to a 24iKB block of memory
  530. */
  531. static void build_pagetable(uint32_t *pgtable)
  532. {
  533. uint i;
  534. memset(pgtable, '\0', PAGETABLE_SIZE);
  535. /* Level 4 needs a single entry */
  536. pgtable[0] = (uint32_t)&pgtable[1024] + 7;
  537. /* Level 3 has one 64-bit entry for each GiB of memory */
  538. for (i = 0; i < 4; i++) {
  539. pgtable[1024 + i * 2] = (uint32_t)&pgtable[2048] +
  540. 0x1000 * i + 7;
  541. }
  542. /* Level 2 has 2048 64-bit entries, each repesenting 2MiB */
  543. for (i = 0; i < 2048; i++)
  544. pgtable[2048 + i * 2] = 0x183 + (i << 21UL);
  545. }
  546. int cpu_jump_to_64bit(ulong setup_base, ulong target)
  547. {
  548. uint32_t *pgtable;
  549. pgtable = memalign(4096, PAGETABLE_SIZE);
  550. if (!pgtable)
  551. return -ENOMEM;
  552. build_pagetable(pgtable);
  553. cpu_call64((ulong)pgtable, setup_base, target);
  554. free(pgtable);
  555. return -EFAULT;
  556. }
  557. void show_boot_progress(int val)
  558. {
  559. outb(val, POST_PORT);
  560. }
  561. #ifndef CONFIG_SYS_COREBOOT
  562. /*
  563. * Implement a weak default function for boards that optionally
  564. * need to clean up the system before jumping to the kernel.
  565. */
  566. __weak void board_final_cleanup(void)
  567. {
  568. }
  569. int last_stage_init(void)
  570. {
  571. write_tables();
  572. board_final_cleanup();
  573. return 0;
  574. }
  575. #endif
  576. #ifdef CONFIG_SMP
  577. static int enable_smis(struct udevice *cpu, void *unused)
  578. {
  579. return 0;
  580. }
  581. static struct mp_flight_record mp_steps[] = {
  582. MP_FR_BLOCK_APS(mp_init_cpu, NULL, mp_init_cpu, NULL),
  583. /* Wait for APs to finish initialization before proceeding */
  584. MP_FR_BLOCK_APS(NULL, NULL, enable_smis, NULL),
  585. };
  586. static int x86_mp_init(void)
  587. {
  588. struct mp_params mp_params;
  589. mp_params.parallel_microcode_load = 0,
  590. mp_params.flight_plan = &mp_steps[0];
  591. mp_params.num_records = ARRAY_SIZE(mp_steps);
  592. mp_params.microcode_pointer = 0;
  593. if (mp_init(&mp_params)) {
  594. printf("Warning: MP init failure\n");
  595. return -EIO;
  596. }
  597. return 0;
  598. }
  599. #endif
  600. static int x86_init_cpus(void)
  601. {
  602. #ifdef CONFIG_SMP
  603. debug("Init additional CPUs\n");
  604. x86_mp_init();
  605. #else
  606. struct udevice *dev;
  607. /*
  608. * This causes the cpu-x86 driver to be probed.
  609. * We don't check return value here as we want to allow boards
  610. * which have not been converted to use cpu uclass driver to boot.
  611. */
  612. uclass_first_device(UCLASS_CPU, &dev);
  613. #endif
  614. return 0;
  615. }
  616. int cpu_init_r(void)
  617. {
  618. struct udevice *dev;
  619. int ret;
  620. if (!ll_boot_init())
  621. return 0;
  622. ret = x86_init_cpus();
  623. if (ret)
  624. return ret;
  625. /*
  626. * Set up the northbridge, PCH and LPC if available. Note that these
  627. * may have had some limited pre-relocation init if they were probed
  628. * before relocation, but this is post relocation.
  629. */
  630. uclass_first_device(UCLASS_NORTHBRIDGE, &dev);
  631. uclass_first_device(UCLASS_PCH, &dev);
  632. uclass_first_device(UCLASS_LPC, &dev);
  633. return 0;
  634. }
  635. #ifndef CONFIG_EFI_STUB
  636. int reserve_arch(void)
  637. {
  638. #ifdef CONFIG_ENABLE_MRC_CACHE
  639. mrccache_reserve();
  640. #endif
  641. #ifdef CONFIG_SEABIOS
  642. high_table_reserve();
  643. #endif
  644. return 0;
  645. }
  646. #endif