blob: f73327397b898a3b0ec7cb801bd16070324b6894 [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001/* 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
9struct ucode_patch {
10 struct list_head plist;
11 void *data; /* Intel uses only this one */
Olivier Deprez92d4c212022-12-06 15:05:30 +010012 unsigned int size;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000013 u32 patch_id;
14 u16 equiv_cpu;
15};
16
17extern struct list_head microcode_cache;
18
19struct cpu_signature {
20 unsigned int sig;
21 unsigned int pf;
22 unsigned int rev;
23};
24
25struct device;
26
27enum ucode_state {
28 UCODE_OK = 0,
29 UCODE_NEW,
30 UCODE_UPDATED,
31 UCODE_NFOUND,
32 UCODE_ERROR,
33};
34
35struct microcode_ops {
36 enum ucode_state (*request_microcode_user) (int cpu,
37 const void __user *buf, size_t size);
38
39 enum ucode_state (*request_microcode_fw) (int cpu, struct device *,
40 bool refresh_fw);
41
42 void (*microcode_fini_cpu) (int cpu);
43
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
54struct ucode_cpu_info {
55 struct cpu_signature cpu_sig;
56 int valid;
57 void *mc;
58};
59extern struct ucode_cpu_info ucode_cpu_info[];
60struct cpio_data find_microcode_in_initrd(const char *path, bool use_pa);
61
62#ifdef CONFIG_MICROCODE_INTEL
63extern struct microcode_ops * __init init_intel_microcode(void);
64#else
65static inline struct microcode_ops * __init init_intel_microcode(void)
66{
67 return NULL;
68}
69#endif /* CONFIG_MICROCODE_INTEL */
70
71#ifdef CONFIG_MICROCODE_AMD
72extern struct microcode_ops * __init init_amd_microcode(void);
73extern void __exit exit_amd_microcode(void);
74#else
75static inline struct microcode_ops * __init init_amd_microcode(void)
76{
77 return NULL;
78}
79static inline void __exit exit_amd_microcode(void) {}
80#endif
81
82#define MAX_UCODE_COUNT 128
83
84#define QCHAR(a, b, c, d) ((a) + ((b) << 8) + ((c) << 16) + ((d) << 24))
85#define CPUID_INTEL1 QCHAR('G', 'e', 'n', 'u')
86#define CPUID_INTEL2 QCHAR('i', 'n', 'e', 'I')
87#define CPUID_INTEL3 QCHAR('n', 't', 'e', 'l')
88#define CPUID_AMD1 QCHAR('A', 'u', 't', 'h')
89#define CPUID_AMD2 QCHAR('e', 'n', 't', 'i')
90#define CPUID_AMD3 QCHAR('c', 'A', 'M', 'D')
91
92#define CPUID_IS(a, b, c, ebx, ecx, edx) \
93 (!((ebx ^ (a))|(edx ^ (b))|(ecx ^ (c))))
94
95/*
96 * In early loading microcode phase on BSP, boot_cpu_data is not set up yet.
97 * x86_cpuid_vendor() gets vendor id for BSP.
98 *
99 * In 32 bit AP case, accessing boot_cpu_data needs linear address. To simplify
100 * coding, we still use x86_cpuid_vendor() to get vendor id for AP.
101 *
102 * x86_cpuid_vendor() gets vendor information directly from CPUID.
103 */
104static inline int x86_cpuid_vendor(void)
105{
106 u32 eax = 0x00000000;
107 u32 ebx, ecx = 0, edx;
108
109 native_cpuid(&eax, &ebx, &ecx, &edx);
110
111 if (CPUID_IS(CPUID_INTEL1, CPUID_INTEL2, CPUID_INTEL3, ebx, ecx, edx))
112 return X86_VENDOR_INTEL;
113
114 if (CPUID_IS(CPUID_AMD1, CPUID_AMD2, CPUID_AMD3, ebx, ecx, edx))
115 return X86_VENDOR_AMD;
116
117 return X86_VENDOR_UNKNOWN;
118}
119
120static inline unsigned int x86_cpuid_family(void)
121{
122 u32 eax = 0x00000001;
123 u32 ebx, ecx = 0, edx;
124
125 native_cpuid(&eax, &ebx, &ecx, &edx);
126
127 return x86_family(eax);
128}
129
130#ifdef CONFIG_MICROCODE
131int __init microcode_init(void);
132extern void __init load_ucode_bsp(void);
133extern void load_ucode_ap(void);
134void reload_early_microcode(void);
135extern bool get_builtin_firmware(struct cpio_data *cd, const char *name);
136extern bool initrd_gone;
Olivier Deprez92d4c212022-12-06 15:05:30 +0100137void microcode_bsp_resume(void);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000138#else
139static inline int __init microcode_init(void) { return 0; };
140static inline void __init load_ucode_bsp(void) { }
141static inline void load_ucode_ap(void) { }
142static inline void reload_early_microcode(void) { }
Olivier Deprez92d4c212022-12-06 15:05:30 +0100143static inline void microcode_bsp_resume(void) { }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000144static inline bool
145get_builtin_firmware(struct cpio_data *cd, const char *name) { return false; }
146#endif
147
148#endif /* _ASM_X86_MICROCODE_H */