cpu.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. * Copyright (c) 2014 The Chromium OS Authors.
  3. *
  4. * Part of this file is adapted from coreboot
  5. * src/arch/x86/include/arch/cpu.h and
  6. * src/arch/x86/lib/cpu.c
  7. *
  8. * SPDX-License-Identifier: GPL-2.0+
  9. */
  10. #ifndef _ASM_CPU_H
  11. #define _ASM_CPU_H
  12. enum {
  13. X86_VENDOR_INVALID = 0,
  14. X86_VENDOR_INTEL,
  15. X86_VENDOR_CYRIX,
  16. X86_VENDOR_AMD,
  17. X86_VENDOR_UMC,
  18. X86_VENDOR_NEXGEN,
  19. X86_VENDOR_CENTAUR,
  20. X86_VENDOR_RISE,
  21. X86_VENDOR_TRANSMETA,
  22. X86_VENDOR_NSC,
  23. X86_VENDOR_SIS,
  24. X86_VENDOR_ANY = 0xfe,
  25. X86_VENDOR_UNKNOWN = 0xff
  26. };
  27. /* Global descriptor table (GDT) bits */
  28. enum {
  29. GDT_4KB = 1ULL << 55,
  30. GDT_32BIT = 1ULL << 54,
  31. GDT_LONG = 1ULL << 53,
  32. GDT_PRESENT = 1ULL << 47,
  33. GDT_NOTSYS = 1ULL << 44,
  34. GDT_CODE = 1ULL << 43,
  35. GDT_LIMIT_LOW_SHIFT = 0,
  36. GDT_LIMIT_LOW_MASK = 0xffff,
  37. GDT_LIMIT_HIGH_SHIFT = 48,
  38. GDT_LIMIT_HIGH_MASK = 0xf,
  39. GDT_BASE_LOW_SHIFT = 16,
  40. GDT_BASE_LOW_MASK = 0xffff,
  41. GDT_BASE_HIGH_SHIFT = 56,
  42. GDT_BASE_HIGH_MASK = 0xf,
  43. };
  44. /*
  45. * System controllers in an x86 system. We mostly need to just find these and
  46. * use them on PCI. At some point these might have their own uclass (e.g.
  47. * UCLASS_VIDEO for the GMA device).
  48. */
  49. enum {
  50. X86_NONE,
  51. X86_SYSCON_ME, /* Intel Management Engine */
  52. X86_SYSCON_PINCONF, /* Intel x86 pin configuration */
  53. X86_SYSCON_PMU, /* Power Management Unit */
  54. X86_SYSCON_SCU, /* System Controller Unit */
  55. };
  56. struct cpuid_result {
  57. uint32_t eax;
  58. uint32_t ebx;
  59. uint32_t ecx;
  60. uint32_t edx;
  61. };
  62. /*
  63. * Generic CPUID function
  64. */
  65. static inline struct cpuid_result cpuid(int op)
  66. {
  67. struct cpuid_result result;
  68. asm volatile(
  69. "mov %%ebx, %%edi;"
  70. "cpuid;"
  71. "mov %%ebx, %%esi;"
  72. "mov %%edi, %%ebx;"
  73. : "=a" (result.eax),
  74. "=S" (result.ebx),
  75. "=c" (result.ecx),
  76. "=d" (result.edx)
  77. : "0" (op)
  78. : "edi");
  79. return result;
  80. }
  81. /*
  82. * Generic Extended CPUID function
  83. */
  84. static inline struct cpuid_result cpuid_ext(int op, unsigned ecx)
  85. {
  86. struct cpuid_result result;
  87. asm volatile(
  88. "mov %%ebx, %%edi;"
  89. "cpuid;"
  90. "mov %%ebx, %%esi;"
  91. "mov %%edi, %%ebx;"
  92. : "=a" (result.eax),
  93. "=S" (result.ebx),
  94. "=c" (result.ecx),
  95. "=d" (result.edx)
  96. : "0" (op), "2" (ecx)
  97. : "edi");
  98. return result;
  99. }
  100. /*
  101. * CPUID functions returning a single datum
  102. */
  103. static inline unsigned int cpuid_eax(unsigned int op)
  104. {
  105. unsigned int eax;
  106. __asm__("mov %%ebx, %%edi;"
  107. "cpuid;"
  108. "mov %%edi, %%ebx;"
  109. : "=a" (eax)
  110. : "0" (op)
  111. : "ecx", "edx", "edi");
  112. return eax;
  113. }
  114. static inline unsigned int cpuid_ebx(unsigned int op)
  115. {
  116. unsigned int eax, ebx;
  117. __asm__("mov %%ebx, %%edi;"
  118. "cpuid;"
  119. "mov %%ebx, %%esi;"
  120. "mov %%edi, %%ebx;"
  121. : "=a" (eax), "=S" (ebx)
  122. : "0" (op)
  123. : "ecx", "edx", "edi");
  124. return ebx;
  125. }
  126. static inline unsigned int cpuid_ecx(unsigned int op)
  127. {
  128. unsigned int eax, ecx;
  129. __asm__("mov %%ebx, %%edi;"
  130. "cpuid;"
  131. "mov %%edi, %%ebx;"
  132. : "=a" (eax), "=c" (ecx)
  133. : "0" (op)
  134. : "edx", "edi");
  135. return ecx;
  136. }
  137. static inline unsigned int cpuid_edx(unsigned int op)
  138. {
  139. unsigned int eax, edx;
  140. __asm__("mov %%ebx, %%edi;"
  141. "cpuid;"
  142. "mov %%edi, %%ebx;"
  143. : "=a" (eax), "=d" (edx)
  144. : "0" (op)
  145. : "ecx", "edi");
  146. return edx;
  147. }
  148. #if !CONFIG_IS_ENABLED(X86_64)
  149. /* Standard macro to see if a specific flag is changeable */
  150. static inline int flag_is_changeable_p(uint32_t flag)
  151. {
  152. uint32_t f1, f2;
  153. asm(
  154. "pushfl\n\t"
  155. "pushfl\n\t"
  156. "popl %0\n\t"
  157. "movl %0,%1\n\t"
  158. "xorl %2,%0\n\t"
  159. "pushl %0\n\t"
  160. "popfl\n\t"
  161. "pushfl\n\t"
  162. "popl %0\n\t"
  163. "popfl\n\t"
  164. : "=&r" (f1), "=&r" (f2)
  165. : "ir" (flag));
  166. return ((f1^f2) & flag) != 0;
  167. }
  168. #endif
  169. static inline void mfence(void)
  170. {
  171. __asm__ __volatile__("mfence" : : : "memory");
  172. }
  173. /**
  174. * cpu_enable_paging_pae() - Enable PAE-paging
  175. *
  176. * @cr3: Value to set in cr3 (PDPT or PML4T)
  177. */
  178. void cpu_enable_paging_pae(ulong cr3);
  179. /**
  180. * cpu_disable_paging_pae() - Disable paging and PAE
  181. */
  182. void cpu_disable_paging_pae(void);
  183. /**
  184. * cpu_has_64bit() - Check if the CPU has 64-bit support
  185. *
  186. * @return 1 if this CPU supports long mode (64-bit), 0 if not
  187. */
  188. int cpu_has_64bit(void);
  189. /**
  190. * cpu_vendor_name() - Get CPU vendor name
  191. *
  192. * @vendor: CPU vendor enumeration number
  193. *
  194. * @return: Address to hold the CPU vendor name string
  195. */
  196. const char *cpu_vendor_name(int vendor);
  197. #define CPU_MAX_NAME_LEN 49
  198. /**
  199. * cpu_get_name() - Get the name of the current cpu
  200. *
  201. * @name: Place to put name, which must be CPU_MAX_NAME_LEN bytes including
  202. * @return pointer to name, which will likely be a few bytes after the start
  203. * of @name
  204. * \0 terminator
  205. */
  206. char *cpu_get_name(char *name);
  207. /**
  208. * cpu_call64() - Jump to a 64-bit Linux kernel (internal function)
  209. *
  210. * The kernel is uncompressed and the 64-bit entry point is expected to be
  211. * at @target.
  212. *
  213. * This function is used internally - see cpu_jump_to_64bit() for a more
  214. * useful function.
  215. *
  216. * @pgtable: Address of 24KB area containing the page table
  217. * @setup_base: Pointer to the setup.bin information for the kernel
  218. * @target: Pointer to the start of the kernel image
  219. */
  220. void cpu_call64(ulong pgtable, ulong setup_base, ulong target);
  221. /**
  222. * cpu_call32() - Jump to a 32-bit entry point
  223. *
  224. * @code_seg32: 32-bit code segment to use (GDT offset, e.g. 0x20)
  225. * @target: Pointer to the start of the 32-bit U-Boot image/entry point
  226. * @table: Pointer to start of info table to pass to U-Boot
  227. */
  228. void cpu_call32(ulong code_seg32, ulong target, ulong table);
  229. /**
  230. * cpu_jump_to_64bit() - Jump to a 64-bit Linux kernel
  231. *
  232. * The kernel is uncompressed and the 64-bit entry point is expected to be
  233. * at @target.
  234. *
  235. * @setup_base: Pointer to the setup.bin information for the kernel
  236. * @target: Pointer to the start of the kernel image
  237. */
  238. int cpu_jump_to_64bit(ulong setup_base, ulong target);
  239. /**
  240. * cpu_jump_to_64bit_uboot() - special function to jump from SPL to U-Boot
  241. *
  242. * This handles calling from 32-bit SPL to 64-bit U-Boot.
  243. *
  244. * @target: Address of U-Boot in RAM
  245. */
  246. int cpu_jump_to_64bit_uboot(ulong target);
  247. /**
  248. * cpu_get_family_model() - Get the family and model for the CPU
  249. *
  250. * @return the CPU ID masked with 0x0fff0ff0
  251. */
  252. u32 cpu_get_family_model(void);
  253. /**
  254. * cpu_get_stepping() - Get the stepping value for the CPU
  255. *
  256. * @return the CPU ID masked with 0xf
  257. */
  258. u32 cpu_get_stepping(void);
  259. /**
  260. * cpu_run_reference_code() - Run the platform reference code
  261. *
  262. * Some platforms require a binary blob to be executed once SDRAM is
  263. * available. This is used to set up various platform features, such as the
  264. * platform controller hub (PCH). This function should be implemented by the
  265. * CPU-specific code.
  266. *
  267. * @return 0 on success, -ve on failure
  268. */
  269. int cpu_run_reference_code(void);
  270. #endif