1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <amdblocks/cpu.h>
5 #include <commonlib/helpers.h>
6 #include <console/console.h>
7 #include <cpu/amd/microcode.h>
8 #include <cpu/amd/msr.h>
10 #include <cpu/x86/msr.h>
12 #include <timestamp.h>
15 #define CPU_MICROCODE_BLOB_NAME "cpu_microcode_XXXX.bin"
16 #define CPU_MICROCODE_BLOB_FORMAT "cpu_microcode_%04x.bin"
22 uint16_t mc_patch_data_id
;
25 uint32_t chipset1_dev_id
;
26 uint32_t chipset2_dev_id
;
28 uint16_t processor_rev_id
;
30 uint8_t chipset1_rev_id
;
31 uint8_t chipset2_rev_id
;
36 static const struct microcode
*ucode
;
38 /* This function requires the ucode variable to be initialized by amd_load_microcode_from_cbfs()
39 and then allocated memory should be freed by the amd_free_microcode(). */
40 void amd_apply_microcode_patch(void)
42 uint32_t new_patch_id
;
46 printk(BIOS_ERR
, "%s: NULL pointer to microcode\n", __func__
);
50 msr
.raw
= (uintptr_t)ucode
;
52 wrmsr(MSR_PATCH_LOADER
, msr
);
54 printk(BIOS_DEBUG
, "microcode: patch id to apply = 0x%08x\n", ucode
->patch_id
);
56 msr
= rdmsr(IA32_BIOS_SIGN_ID
);
57 new_patch_id
= msr
.lo
;
59 if (new_patch_id
== ucode
->patch_id
)
60 printk(BIOS_INFO
, "microcode: being updated to patch id = 0x%08x succeeded\n",
63 printk(BIOS_ERR
, "microcode: being updated to patch id = 0x%08x failed\n",
67 static uint16_t get_equivalent_processor_rev_id(void)
69 uint32_t cpuid_family
= cpuid_eax(1);
71 return (uint16_t)((cpuid_family
& 0xff0000) >> 8 | (cpuid_family
& 0xff));
74 static const struct microcode
*find_microcode(const struct microcode
*microcode
,
75 uint16_t equivalent_processor_rev_id
)
77 if (microcode
->processor_rev_id
== equivalent_processor_rev_id
)
80 printk(BIOS_WARNING
, "Failed to find microcode for processor rev: %hx.\n",
81 equivalent_processor_rev_id
);
86 void amd_load_microcode_from_cbfs(void)
88 char name
[] = CPU_MICROCODE_BLOB_NAME
;
89 uint16_t equivalent_processor_rev_id
;
94 /* Store the pointer to the buffer in global variable, so each CPU doesn't need to read
95 * the uCode from flash. Note that this code assumes all cores are the same */
96 timestamp_add_now(TS_READ_UCODE_START
);
97 equivalent_processor_rev_id
= get_equivalent_processor_rev_id();
98 snprintf(name
, sizeof(name
), CPU_MICROCODE_BLOB_FORMAT
, equivalent_processor_rev_id
);
100 ucode
= cbfs_map(name
, NULL
);
102 printk(BIOS_WARNING
, "%s not found. Skipping updates.\n", name
);
103 timestamp_add_now(TS_READ_UCODE_END
);
107 if (find_microcode(ucode
, equivalent_processor_rev_id
) == NULL
) {
108 cbfs_unmap((void *)ucode
);
112 timestamp_add_now(TS_READ_UCODE_END
);
115 void amd_free_microcode(void)
118 cbfs_unmap((void *)ucode
);
123 void preload_microcode(void)
125 if (!CONFIG(CBFS_PRELOAD
))
128 char name
[] = CPU_MICROCODE_BLOB_NAME
;
129 uint16_t equivalent_processor_rev_id
= get_equivalent_processor_rev_id();
131 snprintf(name
, sizeof(name
), CPU_MICROCODE_BLOB_FORMAT
, equivalent_processor_rev_id
);
132 printk(BIOS_DEBUG
, "Preloading microcode %s\n", name
);