microcode.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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_UPDATED,
  34. UCODE_NFOUND,
  35. UCODE_ERROR,
  36. };
  37. struct microcode_ops {
  38. enum ucode_state (*request_microcode_user) (int cpu,
  39. const void __user *buf, size_t size);
  40. enum ucode_state (*request_microcode_fw) (int cpu, struct device *,
  41. bool refresh_fw);
  42. void (*microcode_fini_cpu) (int cpu);
  43. /*
  44. * The generic 'microcode_core' part guarantees that
  45. * the callbacks below run on a target cpu when they
  46. * are being called.
  47. * See also the "Synchronization" section in microcode_core.c.
  48. */
  49. enum ucode_state (*apply_microcode) (int cpu);
  50. int (*collect_cpu_info) (int cpu, struct cpu_signature *csig);
  51. };
  52. struct ucode_cpu_info {
  53. struct cpu_signature cpu_sig;
  54. int valid;
  55. void *mc;
  56. };
  57. extern struct ucode_cpu_info ucode_cpu_info[];
  58. struct cpio_data find_microcode_in_initrd(const char *path, bool use_pa);
  59. #ifdef CONFIG_MICROCODE_INTEL
  60. extern struct microcode_ops * __init init_intel_microcode(void);
  61. #else
  62. static inline struct microcode_ops * __init init_intel_microcode(void)
  63. {
  64. return NULL;
  65. }
  66. #endif /* CONFIG_MICROCODE_INTEL */
  67. #ifdef CONFIG_MICROCODE_AMD
  68. extern struct microcode_ops * __init init_amd_microcode(void);
  69. extern void __exit exit_amd_microcode(void);
  70. #else
  71. static inline struct microcode_ops * __init init_amd_microcode(void)
  72. {
  73. return NULL;
  74. }
  75. static inline void __exit exit_amd_microcode(void) {}
  76. #endif
  77. #define MAX_UCODE_COUNT 128
  78. #define QCHAR(a, b, c, d) ((a) + ((b) << 8) + ((c) << 16) + ((d) << 24))
  79. #define CPUID_INTEL1 QCHAR('G', 'e', 'n', 'u')
  80. #define CPUID_INTEL2 QCHAR('i', 'n', 'e', 'I')
  81. #define CPUID_INTEL3 QCHAR('n', 't', 'e', 'l')
  82. #define CPUID_AMD1 QCHAR('A', 'u', 't', 'h')
  83. #define CPUID_AMD2 QCHAR('e', 'n', 't', 'i')
  84. #define CPUID_AMD3 QCHAR('c', 'A', 'M', 'D')
  85. #define CPUID_IS(a, b, c, ebx, ecx, edx) \
  86. (!((ebx ^ (a))|(edx ^ (b))|(ecx ^ (c))))
  87. /*
  88. * In early loading microcode phase on BSP, boot_cpu_data is not set up yet.
  89. * x86_cpuid_vendor() gets vendor id for BSP.
  90. *
  91. * In 32 bit AP case, accessing boot_cpu_data needs linear address. To simplify
  92. * coding, we still use x86_cpuid_vendor() to get vendor id for AP.
  93. *
  94. * x86_cpuid_vendor() gets vendor information directly from CPUID.
  95. */
  96. static inline int x86_cpuid_vendor(void)
  97. {
  98. u32 eax = 0x00000000;
  99. u32 ebx, ecx = 0, edx;
  100. native_cpuid(&eax, &ebx, &ecx, &edx);
  101. if (CPUID_IS(CPUID_INTEL1, CPUID_INTEL2, CPUID_INTEL3, ebx, ecx, edx))
  102. return X86_VENDOR_INTEL;
  103. if (CPUID_IS(CPUID_AMD1, CPUID_AMD2, CPUID_AMD3, ebx, ecx, edx))
  104. return X86_VENDOR_AMD;
  105. return X86_VENDOR_UNKNOWN;
  106. }
  107. static inline unsigned int x86_cpuid_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. #ifdef CONFIG_MICROCODE
  115. int __init microcode_init(void);
  116. extern void __init load_ucode_bsp(void);
  117. extern void load_ucode_ap(void);
  118. void reload_early_microcode(void);
  119. extern bool get_builtin_firmware(struct cpio_data *cd, const char *name);
  120. extern bool initrd_gone;
  121. #else
  122. static inline int __init microcode_init(void) { return 0; };
  123. static inline void __init load_ucode_bsp(void) { }
  124. static inline void load_ucode_ap(void) { }
  125. static inline void reload_early_microcode(void) { }
  126. static inline bool
  127. get_builtin_firmware(struct cpio_data *cd, const char *name) { return false; }
  128. #endif
  129. #endif /* _ASM_X86_MICROCODE_H */