cpu.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. };
  54. struct cpuid_result {
  55. uint32_t eax;
  56. uint32_t ebx;
  57. uint32_t ecx;
  58. uint32_t edx;
  59. };
  60. /*
  61. * Generic CPUID function
  62. */
  63. static inline struct cpuid_result cpuid(int op)
  64. {
  65. struct cpuid_result result;
  66. asm volatile(
  67. "mov %%ebx, %%edi;"
  68. "cpuid;"
  69. "mov %%ebx, %%esi;"
  70. "mov %%edi, %%ebx;"
  71. : "=a" (result.eax),
  72. "=S" (result.ebx),
  73. "=c" (result.ecx),
  74. "=d" (result.edx)
  75. : "0" (op)
  76. : "edi");
  77. return result;
  78. }
  79. /*
  80. * Generic Extended CPUID function
  81. */
  82. static inline struct cpuid_result cpuid_ext(int op, unsigned ecx)
  83. {
  84. struct cpuid_result result;
  85. asm volatile(
  86. "mov %%ebx, %%edi;"
  87. "cpuid;"
  88. "mov %%ebx, %%esi;"
  89. "mov %%edi, %%ebx;"
  90. : "=a" (result.eax),
  91. "=S" (result.ebx),
  92. "=c" (result.ecx),
  93. "=d" (result.edx)
  94. : "0" (op), "2" (ecx)
  95. : "edi");
  96. return result;
  97. }
  98. /*
  99. * CPUID functions returning a single datum
  100. */
  101. static inline unsigned int cpuid_eax(unsigned int op)
  102. {
  103. unsigned int eax;
  104. __asm__("mov %%ebx, %%edi;"
  105. "cpuid;"
  106. "mov %%edi, %%ebx;"
  107. : "=a" (eax)
  108. : "0" (op)
  109. : "ecx", "edx", "edi");
  110. return eax;
  111. }
  112. static inline unsigned int cpuid_ebx(unsigned int op)
  113. {
  114. unsigned int eax, ebx;
  115. __asm__("mov %%ebx, %%edi;"
  116. "cpuid;"
  117. "mov %%ebx, %%esi;"
  118. "mov %%edi, %%ebx;"
  119. : "=a" (eax), "=S" (ebx)
  120. : "0" (op)
  121. : "ecx", "edx", "edi");
  122. return ebx;
  123. }
  124. static inline unsigned int cpuid_ecx(unsigned int op)
  125. {
  126. unsigned int eax, ecx;
  127. __asm__("mov %%ebx, %%edi;"
  128. "cpuid;"
  129. "mov %%edi, %%ebx;"
  130. : "=a" (eax), "=c" (ecx)
  131. : "0" (op)
  132. : "edx", "edi");
  133. return ecx;
  134. }
  135. static inline unsigned int cpuid_edx(unsigned int op)
  136. {
  137. unsigned int eax, edx;
  138. __asm__("mov %%ebx, %%edi;"
  139. "cpuid;"
  140. "mov %%edi, %%ebx;"
  141. : "=a" (eax), "=d" (edx)
  142. : "0" (op)
  143. : "ecx", "edi");
  144. return edx;
  145. }
  146. #if !CONFIG_IS_ENABLED(X86_64)
  147. /* Standard macro to see if a specific flag is changeable */
  148. static inline int flag_is_changeable_p(uint32_t flag)
  149. {
  150. uint32_t f1, f2;
  151. asm(
  152. "pushfl\n\t"
  153. "pushfl\n\t"
  154. "popl %0\n\t"
  155. "movl %0,%1\n\t"
  156. "xorl %2,%0\n\t"
  157. "pushl %0\n\t"
  158. "popfl\n\t"
  159. "pushfl\n\t"
  160. "popl %0\n\t"
  161. "popfl\n\t"
  162. : "=&r" (f1), "=&r" (f2)
  163. : "ir" (flag));
  164. return ((f1^f2) & flag) != 0;
  165. }
  166. #endif
  167. static inline void mfence(void)
  168. {
  169. __asm__ __volatile__("mfence" : : : "memory");
  170. }
  171. /**
  172. * cpu_enable_paging_pae() - Enable PAE-paging
  173. *
  174. * @cr3: Value to set in cr3 (PDPT or PML4T)
  175. */
  176. void cpu_enable_paging_pae(ulong cr3);
  177. /**
  178. * cpu_disable_paging_pae() - Disable paging and PAE
  179. */
  180. void cpu_disable_paging_pae(void);
  181. /**
  182. * cpu_has_64bit() - Check if the CPU has 64-bit support
  183. *
  184. * @return 1 if this CPU supports long mode (64-bit), 0 if not
  185. */
  186. int cpu_has_64bit(void);
  187. /**
  188. * cpu_vendor_name() - Get CPU vendor name
  189. *
  190. * @vendor: CPU vendor enumeration number
  191. *
  192. * @return: Address to hold the CPU vendor name string
  193. */
  194. const char *cpu_vendor_name(int vendor);
  195. #define CPU_MAX_NAME_LEN 49
  196. /**
  197. * cpu_get_name() - Get the name of the current cpu
  198. *
  199. * @name: Place to put name, which must be CPU_MAX_NAME_LEN bytes including
  200. * @return pointer to name, which will likely be a few bytes after the start
  201. * of @name
  202. * \0 terminator
  203. */
  204. char *cpu_get_name(char *name);
  205. /**
  206. * cpu_call64() - Jump to a 64-bit Linux kernel (internal function)
  207. *
  208. * The kernel is uncompressed and the 64-bit entry point is expected to be
  209. * at @target.
  210. *
  211. * This function is used internally - see cpu_jump_to_64bit() for a more
  212. * useful function.
  213. *
  214. * @pgtable: Address of 24KB area containing the page table
  215. * @setup_base: Pointer to the setup.bin information for the kernel
  216. * @target: Pointer to the start of the kernel image
  217. */
  218. void cpu_call64(ulong pgtable, ulong setup_base, ulong target);
  219. /**
  220. * cpu_call32() - Jump to a 32-bit entry point
  221. *
  222. * @code_seg32: 32-bit code segment to use (GDT offset, e.g. 0x20)
  223. * @target: Pointer to the start of the 32-bit U-Boot image/entry point
  224. * @table: Pointer to start of info table to pass to U-Boot
  225. */
  226. void cpu_call32(ulong code_seg32, ulong target, ulong table);
  227. /**
  228. * cpu_jump_to_64bit() - Jump to a 64-bit Linux kernel
  229. *
  230. * The kernel is uncompressed and the 64-bit entry point is expected to be
  231. * at @target.
  232. *
  233. * @setup_base: Pointer to the setup.bin information for the kernel
  234. * @target: Pointer to the start of the kernel image
  235. */
  236. int cpu_jump_to_64bit(ulong setup_base, ulong target);
  237. /**
  238. * cpu_get_family_model() - Get the family and model for the CPU
  239. *
  240. * @return the CPU ID masked with 0x0fff0ff0
  241. */
  242. u32 cpu_get_family_model(void);
  243. /**
  244. * cpu_get_stepping() - Get the stepping value for the CPU
  245. *
  246. * @return the CPU ID masked with 0xf
  247. */
  248. u32 cpu_get_stepping(void);
  249. /**
  250. * cpu_run_reference_code() - Run the platform reference code
  251. *
  252. * Some platforms require a binary blob to be executed once SDRAM is
  253. * available. This is used to set up various platform features, such as the
  254. * platform controller hub (PCH). This function should be implemented by the
  255. * CPU-specific code.
  256. *
  257. * @return 0 on success, -ve on failure
  258. */
  259. int cpu_run_reference_code(void);
  260. #endif