soc/mediatek/mt8196: Correct the region size for mcufw_reserved
[coreboot2.git] / src / soc / amd / common / block / cpu / update_microcode.c
blob14c4f36b989bd6b812a991b88910ed0717c3c70a
1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <amdblocks/cpu.h>
4 #include <cbfs.h>
5 #include <commonlib/helpers.h>
6 #include <console/console.h>
7 #include <cpu/amd/microcode.h>
8 #include <cpu/amd/msr.h>
9 #include <cpu/cpu.h>
10 #include <cpu/x86/msr.h>
11 #include <stdio.h>
12 #include <timestamp.h>
13 #include <types.h>
15 #define CPU_MICROCODE_BLOB_NAME "cpu_microcode_XXXX.bin"
16 #define CPU_MICROCODE_BLOB_FORMAT "cpu_microcode_%04x.bin"
18 struct microcode {
19 uint32_t date_code;
20 uint32_t patch_id;
22 uint16_t mc_patch_data_id;
23 uint8_t reserved1[6];
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;
33 uint8_t reserved2[4];
34 } __packed;
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;
43 msr_t msr;
45 if (!ucode) {
46 printk(BIOS_ERR, "%s: NULL pointer to microcode\n", __func__);
47 return;
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",
61 new_patch_id);
62 else
63 printk(BIOS_ERR, "microcode: being updated to patch id = 0x%08x failed\n",
64 new_patch_id);
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)
78 return microcode;
80 printk(BIOS_WARNING, "Failed to find microcode for processor rev: %hx.\n",
81 equivalent_processor_rev_id);
83 return NULL;
86 void amd_load_microcode_from_cbfs(void)
88 char name[] = CPU_MICROCODE_BLOB_NAME;
89 uint16_t equivalent_processor_rev_id;
91 if (ucode)
92 return;
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);
101 if (!ucode) {
102 printk(BIOS_WARNING, "%s not found. Skipping updates.\n", name);
103 timestamp_add_now(TS_READ_UCODE_END);
104 return;
107 if (find_microcode(ucode, equivalent_processor_rev_id) == NULL) {
108 cbfs_unmap((void *)ucode);
109 ucode = NULL;
112 timestamp_add_now(TS_READ_UCODE_END);
115 void amd_free_microcode(void)
117 if (ucode) {
118 cbfs_unmap((void *)ucode);
119 ucode = NULL;
123 void preload_microcode(void)
125 if (!CONFIG(CBFS_PRELOAD))
126 return;
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);
133 cbfs_preload(name);