cpu.c 16 KB

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