cpu.h 6.2 KB

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