21 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/pci.h>
32 #include <asm/processor.h>
39 #define UCODE_MAGIC 0x00414d44
40 #define UCODE_EQUIV_CPU_TABLE_TYPE 0x00000000
41 #define UCODE_UCODE_TYPE 0x00000001
73 #define SECTION_HDR_SIZE 8
74 #define CONTAINER_HDR_SZ 12
87 static u16 find_equiv_id(
unsigned int cpu)
110 while (equiv_cpu_table[i].equiv_cpu != 0) {
111 if (equiv_cpu == equiv_cpu_table[i].equiv_cpu)
126 if (p->equiv_cpu == equiv_cpu)
136 if (p->
equiv_cpu == new_patch->equiv_cpu) {
137 if (p->
patch_id >= new_patch->patch_id)
141 list_replace(&p->
plist, &new_patch->plist);
151 static void free_cache(
void)
162 static struct ucode_patch *find_patch(
unsigned int cpu)
166 equiv_id = find_equiv_id(cpu);
170 return cache_find_patch(equiv_id);
173 static int collect_cpu_info_amd(
int cpu,
struct cpu_signature *csig)
177 csig->
sig = cpuid_eax(0x00000001);
179 pr_info(
"CPU%d: patch_level=0x%08x\n", cpu, csig->
rev);
184 static unsigned int verify_patch_size(
int cpu,
u32 patch_size,
190 #define F1XH_MPB_MAX_SIZE 2048
191 #define F14H_MPB_MAX_SIZE 1824
192 #define F15H_MPB_MAX_SIZE 4096
193 #define F16H_MPB_MAX_SIZE 3458
210 if (patch_size >
min_t(
u32, size, max_size)) {
211 pr_err(
"patch size mismatch\n");
218 static int apply_microcode_amd(
int cpu)
240 if (rev >= mc_amd->
hdr.patch_id) {
249 if (rev != mc_amd->
hdr.patch_id) {
250 pr_err(
"CPU%d: update failed for patch_level=0x%08x\n",
251 cpu, mc_amd->
hdr.patch_id);
255 pr_info(
"CPU%d: new patch_level=0x%08x\n", cpu, rev);
262 static int install_equiv_cpu_table(
const u8 *
buf)
264 unsigned int *ibuf = (
unsigned int *)buf;
265 unsigned int type = ibuf[1];
266 unsigned int size = ibuf[2];
270 "invalid type field in container file section header\n");
274 equiv_cpu_table =
vmalloc(size);
275 if (!equiv_cpu_table) {
276 pr_err(
"failed to allocate equivalent CPU table\n");
286 static void free_equiv_cpu_table(
void)
288 vfree(equiv_cpu_table);
289 equiv_cpu_table =
NULL;
292 static void cleanup(
void)
294 free_equiv_cpu_table();
305 static int verify_and_add_patch(
unsigned int cpu,
u8 *
fw,
unsigned int leftover)
310 unsigned int patch_size, crnt_size,
ret;
314 patch_size = *(
u32 *)(fw + 4);
319 proc_fam = find_cpu_family_by_equiv_cpu(proc_id);
321 pr_err(
"No patch family for equiv ID: 0x%04x\n", proc_id);
326 proc_fam = ((proc_fam >> 8) & 0xf) + ((proc_fam >> 20) & 0xff);
327 if (proc_fam != c->
x86)
331 pr_err(
"Patch-ID 0x%08x: chipset-specific code unsupported.\n",
336 ret = verify_patch_size(cpu, patch_size, leftover);
344 pr_err(
"Patch allocation failure.\n");
350 pr_err(
"Patch data allocation failure.\n");
356 memcpy(patch->
data, fw + SECTION_HDR_SIZE, patch_size);
357 INIT_LIST_HEAD(&patch->
plist);
370 unsigned int leftover;
375 offset = install_equiv_cpu_table(data);
377 pr_err(
"failed to create equivalent cpu table\n");
384 pr_err(
"invalid type field in container file section header\n");
385 free_equiv_cpu_table();
390 crnt_size = verify_and_add_patch(cpu, fw, leftover);
395 leftover -= crnt_size;
420 char fw_name[36] =
"amd-ucode/microcode_amd.bin";
430 snprintf(fw_name,
sizeof(fw_name),
"amd-ucode/microcode_amd_fam%.2xh.bin", c->
x86);
433 pr_err(
"failed to load file %s\n", fw_name);
444 free_equiv_cpu_table();
446 ret = load_microcode_amd(cpu, fw->
data, fw->
size);
458 request_microcode_user(
int cpu,
const void __user *buf,
size_t size)
463 static void microcode_fini_cpu_amd(
int cpu)
472 .request_microcode_fw = request_microcode_amd,
473 .collect_cpu_info = collect_cpu_info_amd,
474 .apply_microcode = apply_microcode_amd,
475 .microcode_fini_cpu = microcode_fini_cpu_amd,
487 return µcode_amd_ops;