microcode.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #ifndef _ASM_X86_MICROCODE_H
  2. #define _ASM_X86_MICROCODE_H
  3. #include <linux/earlycpio.h>
  4. #define native_rdmsr(msr, val1, val2) \
  5. do { \
  6. u64 __val = native_read_msr((msr)); \
  7. (void)((val1) = (u32)__val); \
  8. (void)((val2) = (u32)(__val >> 32)); \
  9. } while (0)
  10. #define native_wrmsr(msr, low, high) \
  11. native_write_msr(msr, low, high)
  12. #define native_wrmsrl(msr, val) \
  13. native_write_msr((msr), \
  14. (u32)((u64)(val)), \
  15. (u32)((u64)(val) >> 32))
  16. struct cpu_signature {
  17. unsigned int sig;
  18. unsigned int pf;
  19. unsigned int rev;
  20. };
  21. struct device;
  22. enum ucode_state { UCODE_ERROR, UCODE_OK, UCODE_NFOUND };
  23. extern bool dis_ucode_ldr;
  24. struct microcode_ops {
  25. enum ucode_state (*request_microcode_user) (int cpu,
  26. const void __user *buf, size_t size);
  27. enum ucode_state (*request_microcode_fw) (int cpu, struct device *,
  28. bool refresh_fw);
  29. void (*microcode_fini_cpu) (int cpu);
  30. /*
  31. * The generic 'microcode_core' part guarantees that
  32. * the callbacks below run on a target cpu when they
  33. * are being called.
  34. * See also the "Synchronization" section in microcode_core.c.
  35. */
  36. int (*apply_microcode) (int cpu);
  37. int (*collect_cpu_info) (int cpu, struct cpu_signature *csig);
  38. };
  39. struct ucode_cpu_info {
  40. struct cpu_signature cpu_sig;
  41. int valid;
  42. void *mc;
  43. };
  44. extern struct ucode_cpu_info ucode_cpu_info[];
  45. #ifdef CONFIG_MICROCODE
  46. int __init microcode_init(void);
  47. #else
  48. static inline int __init microcode_init(void) { return 0; };
  49. #endif
  50. #ifdef CONFIG_MICROCODE_INTEL
  51. extern struct microcode_ops * __init init_intel_microcode(void);
  52. #else
  53. static inline struct microcode_ops * __init init_intel_microcode(void)
  54. {
  55. return NULL;
  56. }
  57. #endif /* CONFIG_MICROCODE_INTEL */
  58. #ifdef CONFIG_MICROCODE_AMD
  59. extern struct microcode_ops * __init init_amd_microcode(void);
  60. extern void __exit exit_amd_microcode(void);
  61. #else
  62. static inline struct microcode_ops * __init init_amd_microcode(void)
  63. {
  64. return NULL;
  65. }
  66. static inline void __exit exit_amd_microcode(void) {}
  67. #endif
  68. #ifdef CONFIG_MICROCODE_EARLY
  69. #define MAX_UCODE_COUNT 128
  70. #define QCHAR(a, b, c, d) ((a) + ((b) << 8) + ((c) << 16) + ((d) << 24))
  71. #define CPUID_INTEL1 QCHAR('G', 'e', 'n', 'u')
  72. #define CPUID_INTEL2 QCHAR('i', 'n', 'e', 'I')
  73. #define CPUID_INTEL3 QCHAR('n', 't', 'e', 'l')
  74. #define CPUID_AMD1 QCHAR('A', 'u', 't', 'h')
  75. #define CPUID_AMD2 QCHAR('e', 'n', 't', 'i')
  76. #define CPUID_AMD3 QCHAR('c', 'A', 'M', 'D')
  77. #define CPUID_IS(a, b, c, ebx, ecx, edx) \
  78. (!((ebx ^ (a))|(edx ^ (b))|(ecx ^ (c))))
  79. /*
  80. * In early loading microcode phase on BSP, boot_cpu_data is not set up yet.
  81. * x86_vendor() gets vendor id for BSP.
  82. *
  83. * In 32 bit AP case, accessing boot_cpu_data needs linear address. To simplify
  84. * coding, we still use x86_vendor() to get vendor id for AP.
  85. *
  86. * x86_vendor() gets vendor information directly from CPUID.
  87. */
  88. static inline int x86_vendor(void)
  89. {
  90. u32 eax = 0x00000000;
  91. u32 ebx, ecx = 0, edx;
  92. native_cpuid(&eax, &ebx, &ecx, &edx);
  93. if (CPUID_IS(CPUID_INTEL1, CPUID_INTEL2, CPUID_INTEL3, ebx, ecx, edx))
  94. return X86_VENDOR_INTEL;
  95. if (CPUID_IS(CPUID_AMD1, CPUID_AMD2, CPUID_AMD3, ebx, ecx, edx))
  96. return X86_VENDOR_AMD;
  97. return X86_VENDOR_UNKNOWN;
  98. }
  99. static inline unsigned int __x86_family(unsigned int sig)
  100. {
  101. unsigned int x86;
  102. x86 = (sig >> 8) & 0xf;
  103. if (x86 == 0xf)
  104. x86 += (sig >> 20) & 0xff;
  105. return x86;
  106. }
  107. static inline unsigned int x86_family(void)
  108. {
  109. u32 eax = 0x00000001;
  110. u32 ebx, ecx = 0, edx;
  111. native_cpuid(&eax, &ebx, &ecx, &edx);
  112. return __x86_family(eax);
  113. }
  114. static inline unsigned int x86_model(unsigned int sig)
  115. {
  116. unsigned int x86, model;
  117. x86 = __x86_family(sig);
  118. model = (sig >> 4) & 0xf;
  119. if (x86 == 0x6 || x86 == 0xf)
  120. model += ((sig >> 16) & 0xf) << 4;
  121. return model;
  122. }
  123. extern void __init load_ucode_bsp(void);
  124. extern void load_ucode_ap(void);
  125. extern int __init save_microcode_in_initrd(void);
  126. void reload_early_microcode(void);
  127. extern bool get_builtin_firmware(struct cpio_data *cd, const char *name);
  128. #else
  129. static inline void __init load_ucode_bsp(void) {}
  130. static inline void load_ucode_ap(void) {}
  131. static inline int __init save_microcode_in_initrd(void)
  132. {
  133. return 0;
  134. }
  135. static inline void reload_early_microcode(void) {}
  136. static inline bool get_builtin_firmware(struct cpio_data *cd, const char *name)
  137. {
  138. return false;
  139. }
  140. #endif
  141. #endif /* _ASM_X86_MICROCODE_H */