microcode.h 4.1 KB

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