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