cpu.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. /* Standard macro to see if a specific flag is changeable */
  147. static inline int flag_is_changeable_p(uint32_t flag)
  148. {
  149. uint32_t f1, f2;
  150. asm(
  151. "pushfl\n\t"
  152. "pushfl\n\t"
  153. "popl %0\n\t"
  154. "movl %0,%1\n\t"
  155. "xorl %2,%0\n\t"
  156. "pushl %0\n\t"
  157. "popfl\n\t"
  158. "pushfl\n\t"
  159. "popl %0\n\t"
  160. "popfl\n\t"
  161. : "=&r" (f1), "=&r" (f2)
  162. : "ir" (flag));
  163. return ((f1^f2) & flag) != 0;
  164. }
  165. static inline void mfence(void)
  166. {
  167. __asm__ __volatile__("mfence" : : : "memory");
  168. }
  169. /**
  170. * cpu_enable_paging_pae() - Enable PAE-paging
  171. *
  172. * @cr3: Value to set in cr3 (PDPT or PML4T)
  173. */
  174. void cpu_enable_paging_pae(ulong cr3);
  175. /**
  176. * cpu_disable_paging_pae() - Disable paging and PAE
  177. */
  178. void cpu_disable_paging_pae(void);
  179. /**
  180. * cpu_has_64bit() - Check if the CPU has 64-bit support
  181. *
  182. * @return 1 if this CPU supports long mode (64-bit), 0 if not
  183. */
  184. int cpu_has_64bit(void);
  185. /**
  186. * cpu_vendor_name() - Get CPU vendor name
  187. *
  188. * @vendor: CPU vendor enumeration number
  189. *
  190. * @return: Address to hold the CPU vendor name string
  191. */
  192. const char *cpu_vendor_name(int vendor);
  193. #define CPU_MAX_NAME_LEN 49
  194. /**
  195. * cpu_get_name() - Get the name of the current cpu
  196. *
  197. * @name: Place to put name, which must be CPU_MAX_NAME_LEN bytes including
  198. * @return pointer to name, which will likely be a few bytes after the start
  199. * of @name
  200. * \0 terminator
  201. */
  202. char *cpu_get_name(char *name);
  203. /**
  204. * cpu_call64() - Jump to a 64-bit Linux kernel (internal function)
  205. *
  206. * The kernel is uncompressed and the 64-bit entry point is expected to be
  207. * at @target.
  208. *
  209. * This function is used internally - see cpu_jump_to_64bit() for a more
  210. * useful function.
  211. *
  212. * @pgtable: Address of 24KB area containing the page table
  213. * @setup_base: Pointer to the setup.bin information for the kernel
  214. * @target: Pointer to the start of the kernel image
  215. */
  216. void cpu_call64(ulong pgtable, ulong setup_base, ulong target);
  217. /**
  218. * cpu_call32() - Jump to a 32-bit entry point
  219. *
  220. * @code_seg32: 32-bit code segment to use (GDT offset, e.g. 0x20)
  221. * @target: Pointer to the start of the 32-bit U-Boot image/entry point
  222. * @table: Pointer to start of info table to pass to U-Boot
  223. */
  224. void cpu_call32(ulong code_seg32, ulong target, ulong table);
  225. /**
  226. * cpu_jump_to_64bit() - Jump to a 64-bit Linux kernel
  227. *
  228. * The kernel is uncompressed and the 64-bit entry point is expected to be
  229. * at @target.
  230. *
  231. * @setup_base: Pointer to the setup.bin information for the kernel
  232. * @target: Pointer to the start of the kernel image
  233. */
  234. int cpu_jump_to_64bit(ulong setup_base, ulong target);
  235. /**
  236. * cpu_get_family_model() - Get the family and model for the CPU
  237. *
  238. * @return the CPU ID masked with 0x0fff0ff0
  239. */
  240. u32 cpu_get_family_model(void);
  241. /**
  242. * cpu_get_stepping() - Get the stepping value for the CPU
  243. *
  244. * @return the CPU ID masked with 0xf
  245. */
  246. u32 cpu_get_stepping(void);
  247. /**
  248. * cpu_run_reference_code() - Run the platform reference code
  249. *
  250. * Some platforms require a binary blob to be executed once SDRAM is
  251. * available. This is used to set up various platform features, such as the
  252. * platform controller hub (PCH). This function should be implemented by the
  253. * CPU-specific code.
  254. *
  255. * @return 0 on success, -ve on failure
  256. */
  257. int cpu_run_reference_code(void);
  258. #endif