microcode.h 4.0 KB

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