cpu.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 <command.h>
  22. #include <dm.h>
  23. #include <errno.h>
  24. #include <malloc.h>
  25. #include <syscon.h>
  26. #include <asm/acpi.h>
  27. #include <asm/acpi_s3.h>
  28. #include <asm/acpi_table.h>
  29. #include <asm/control_regs.h>
  30. #include <asm/coreboot_tables.h>
  31. #include <asm/cpu.h>
  32. #include <asm/lapic.h>
  33. #include <asm/microcode.h>
  34. #include <asm/mp.h>
  35. #include <asm/mrccache.h>
  36. #include <asm/msr.h>
  37. #include <asm/mtrr.h>
  38. #include <asm/post.h>
  39. #include <asm/processor.h>
  40. #include <asm/processor-flags.h>
  41. #include <asm/interrupt.h>
  42. #include <asm/tables.h>
  43. #include <linux/compiler.h>
  44. DECLARE_GLOBAL_DATA_PTR;
  45. static const char *const x86_vendor_name[] = {
  46. [X86_VENDOR_INTEL] = "Intel",
  47. [X86_VENDOR_CYRIX] = "Cyrix",
  48. [X86_VENDOR_AMD] = "AMD",
  49. [X86_VENDOR_UMC] = "UMC",
  50. [X86_VENDOR_NEXGEN] = "NexGen",
  51. [X86_VENDOR_CENTAUR] = "Centaur",
  52. [X86_VENDOR_RISE] = "Rise",
  53. [X86_VENDOR_TRANSMETA] = "Transmeta",
  54. [X86_VENDOR_NSC] = "NSC",
  55. [X86_VENDOR_SIS] = "SiS",
  56. };
  57. int __weak x86_cleanup_before_linux(void)
  58. {
  59. #ifdef CONFIG_BOOTSTAGE_STASH
  60. bootstage_stash((void *)CONFIG_BOOTSTAGE_STASH_ADDR,
  61. CONFIG_BOOTSTAGE_STASH_SIZE);
  62. #endif
  63. return 0;
  64. }
  65. int x86_init_cache(void)
  66. {
  67. enable_caches();
  68. return 0;
  69. }
  70. int init_cache(void) __attribute__((weak, alias("x86_init_cache")));
  71. void flush_cache(unsigned long dummy1, unsigned long dummy2)
  72. {
  73. asm("wbinvd\n");
  74. }
  75. /* Define these functions to allow ehch-hcd to function */
  76. void flush_dcache_range(unsigned long start, unsigned long stop)
  77. {
  78. }
  79. void invalidate_dcache_range(unsigned long start, unsigned long stop)
  80. {
  81. }
  82. void dcache_enable(void)
  83. {
  84. enable_caches();
  85. }
  86. void dcache_disable(void)
  87. {
  88. disable_caches();
  89. }
  90. void icache_enable(void)
  91. {
  92. }
  93. void icache_disable(void)
  94. {
  95. }
  96. int icache_status(void)
  97. {
  98. return 1;
  99. }
  100. const char *cpu_vendor_name(int vendor)
  101. {
  102. const char *name;
  103. name = "<invalid cpu vendor>";
  104. if (vendor < ARRAY_SIZE(x86_vendor_name) &&
  105. x86_vendor_name[vendor])
  106. name = x86_vendor_name[vendor];
  107. return name;
  108. }
  109. char *cpu_get_name(char *name)
  110. {
  111. unsigned int *name_as_ints = (unsigned int *)name;
  112. struct cpuid_result regs;
  113. char *ptr;
  114. int i;
  115. /* This bit adds up to 48 bytes */
  116. for (i = 0; i < 3; i++) {
  117. regs = cpuid(0x80000002 + i);
  118. name_as_ints[i * 4 + 0] = regs.eax;
  119. name_as_ints[i * 4 + 1] = regs.ebx;
  120. name_as_ints[i * 4 + 2] = regs.ecx;
  121. name_as_ints[i * 4 + 3] = regs.edx;
  122. }
  123. name[CPU_MAX_NAME_LEN - 1] = '\0';
  124. /* Skip leading spaces. */
  125. ptr = name;
  126. while (*ptr == ' ')
  127. ptr++;
  128. return ptr;
  129. }
  130. int default_print_cpuinfo(void)
  131. {
  132. printf("CPU: %s, vendor %s, device %xh\n",
  133. cpu_has_64bit() ? "x86_64" : "x86",
  134. cpu_vendor_name(gd->arch.x86_vendor), gd->arch.x86_device);
  135. #ifdef CONFIG_HAVE_ACPI_RESUME
  136. debug("ACPI previous sleep state: %s\n",
  137. acpi_ss_string(gd->arch.prev_sleep_state));
  138. #endif
  139. return 0;
  140. }
  141. void show_boot_progress(int val)
  142. {
  143. outb(val, POST_PORT);
  144. }
  145. #if !defined(CONFIG_SYS_COREBOOT) && !defined(CONFIG_EFI_STUB)
  146. /*
  147. * Implement a weak default function for boards that optionally
  148. * need to clean up the system before jumping to the kernel.
  149. */
  150. __weak void board_final_cleanup(void)
  151. {
  152. }
  153. int last_stage_init(void)
  154. {
  155. board_final_cleanup();
  156. #if CONFIG_HAVE_ACPI_RESUME
  157. struct acpi_fadt *fadt = acpi_find_fadt();
  158. if (fadt != NULL && gd->arch.prev_sleep_state == ACPI_S3)
  159. acpi_resume(fadt);
  160. #endif
  161. write_tables();
  162. return 0;
  163. }
  164. #endif
  165. static int x86_init_cpus(void)
  166. {
  167. #ifdef CONFIG_SMP
  168. debug("Init additional CPUs\n");
  169. x86_mp_init();
  170. #else
  171. struct udevice *dev;
  172. /*
  173. * This causes the cpu-x86 driver to be probed.
  174. * We don't check return value here as we want to allow boards
  175. * which have not been converted to use cpu uclass driver to boot.
  176. */
  177. uclass_first_device(UCLASS_CPU, &dev);
  178. #endif
  179. return 0;
  180. }
  181. int cpu_init_r(void)
  182. {
  183. struct udevice *dev;
  184. int ret;
  185. if (!ll_boot_init())
  186. return 0;
  187. ret = x86_init_cpus();
  188. if (ret)
  189. return ret;
  190. /*
  191. * Set up the northbridge, PCH and LPC if available. Note that these
  192. * may have had some limited pre-relocation init if they were probed
  193. * before relocation, but this is post relocation.
  194. */
  195. uclass_first_device(UCLASS_NORTHBRIDGE, &dev);
  196. uclass_first_device(UCLASS_PCH, &dev);
  197. uclass_first_device(UCLASS_LPC, &dev);
  198. /* Set up pin control if available */
  199. ret = syscon_get_by_driver_data(X86_SYSCON_PINCONF, &dev);
  200. debug("%s, pinctrl=%p, ret=%d\n", __func__, dev, ret);
  201. return 0;
  202. }
  203. #ifndef CONFIG_EFI_STUB
  204. int reserve_arch(void)
  205. {
  206. #ifdef CONFIG_ENABLE_MRC_CACHE
  207. mrccache_reserve();
  208. #endif
  209. #ifdef CONFIG_SEABIOS
  210. high_table_reserve();
  211. #endif
  212. #ifdef CONFIG_HAVE_ACPI_RESUME
  213. acpi_s3_reserve();
  214. #ifdef CONFIG_HAVE_FSP
  215. /*
  216. * Save stack address to CMOS so that at next S3 boot,
  217. * we can use it as the stack address for fsp_contiue()
  218. */
  219. fsp_save_s3_stack();
  220. #endif /* CONFIG_HAVE_FSP */
  221. #endif /* CONFIG_HAVE_ACPI_RESUME */
  222. return 0;
  223. }
  224. #endif