coreboot
coreboot is an Open Source project aimed at replacing the proprietary BIOS found in most computers.
update_microcode.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #include <stdint.h>
4 #include <arch/cpu.h>
5 #include <cpu/amd/microcode.h>
6 #include <commonlib/helpers.h>
7 #include <console/console.h>
8 #include <cpu/x86/msr.h>
9 #include <cpu/amd/msr.h>
10 #include <cbfs.h>
11 
12 /*
13  * Values and header structure from:
14  * BKDG for AMD Family 16h Models 30h-3Fh Processors
15  * 52740 Rev 3.06 - March 18, 2016
16  */
17 
18 #define F16H_MPB_MAX_SIZE 3458
19 #define F16H_MPB_DATA_OFFSET 32
20 
21  /*
22  * STRUCTURE OF A MICROCODE (UCODE) FILE FOR FAM16h
23  * Microcode Patch Block
24  * Microcode Header
25  * Microcode "Blob"
26  * ...
27  * ...
28  * (end of file)
29  *
30  *
31  * MICROCODE HEADER (offset 0 bytes from start of file)
32  * Total size = 32 bytes
33  * [0:3] Date code (32 bits)
34  * [4:7] Patch level (32 bits)
35  * [8:9] Microcode patch data ID (16 bits)
36  * [10:15] Reserved (48 bits)
37  * [16:19] Chipset 1 device ID (32 bits)
38  * [20:23] Chipset 2 device ID (32 bits)
39  * [24:25] Processor Revisions ID (16 bits)
40  * [26] Chipset 1 revision ID (8 bits)
41  * [27] Chipset 2 revision ID (8 bits)
42  * [28:31] Reserved (32 bits)
43  *
44  * MICROCODE BLOB (offset += 32)
45  * Total size = m bytes
46  *
47  */
48 
49 struct microcode {
52 
55 
58 
60 
63 
65 
68 
69 static void apply_microcode_patch(const struct microcode *m)
70 {
71  uint32_t new_patch_id;
72  msr_t msr;
73 
74  msr.hi = (uint64_t)(uintptr_t)m >> 32;
75  msr.lo = (uintptr_t)m & 0xffffffff;
76 
77  wrmsr(MSR_PATCH_LOADER, msr);
78 
79  printk(BIOS_DEBUG, "microcode: patch id to apply = 0x%08x\n",
80  m->patch_id);
81 
82  msr = rdmsr(IA32_BIOS_SIGN_ID);
83  new_patch_id = msr.lo;
84 
85  if (new_patch_id == m->patch_id)
86  printk(BIOS_INFO, "microcode: being updated to patch id = 0x%08x succeeded\n",
87  new_patch_id);
88  else
89  printk(BIOS_ERR, "microcode: being updated to patch id = 0x%08x failed\n",
90  new_patch_id);
91 }
92 
94 {
95  uint32_t cpuid_family = cpuid_eax(1);
96 
97  return (uint16_t)((cpuid_family & 0xff0000) >> 8 | (cpuid_family & 0xff));
98 }
99 
100 static void amd_update_microcode(const void *ucode, size_t ucode_len,
101  uint16_t equivalent_processor_rev_id)
102 {
103  const struct microcode *m;
104  const uint8_t *c = ucode;
105 
106  m = (struct microcode *)c;
107 
108  /* Assume cpu_microcode_blob only contains one microcode. */
109  if (m->processor_rev_id == equivalent_processor_rev_id)
111 }
112 
114 {
115  const void *ucode;
116  size_t ucode_len;
117  uint16_t equivalent_processor_rev_id = get_equivalent_processor_rev_id();
118 
119  ucode = cbfs_map("cpu_microcode_blob.bin", &ucode_len);
120  if (!ucode) {
121  printk(BIOS_WARNING, "cpu_microcode_blob.bin not found. Skipping updates.\n");
122  return;
123  }
124 
125  if (ucode_len > F16H_MPB_MAX_SIZE ||
126  ucode_len < F16H_MPB_DATA_OFFSET) {
127  printk(BIOS_DEBUG, "microcode file invalid. Skipping updates.\n");
128  return;
129  }
130 
131  amd_update_microcode(ucode, ucode_len, equivalent_processor_rev_id);
132 }
static unsigned int cpuid_eax(unsigned int op)
Definition: cpu.h:79
static void * cbfs_map(const char *name, size_t *size_out)
Definition: cbfs.h:246
#define printk(level,...)
Definition: stdlib.h:16
static uint16_t get_equivalent_processor_rev_id(void)
void amd_update_microcode_from_cbfs(void)
static void amd_update_microcode(const void *ucode, size_t ucode_len, uint16_t equivalent_processor_rev_id)
#define F16H_MPB_MAX_SIZE
static void apply_microcode_patch(const struct microcode *m)
#define F16H_MPB_DATA_OFFSET
struct microcode __packed
#define MSR_PATCH_LOADER
Definition: msr.h:50
static __always_inline msr_t rdmsr(unsigned int index)
Definition: msr.h:146
#define IA32_BIOS_SIGN_ID
Definition: msr.h:32
static __always_inline void wrmsr(unsigned int index, msr_t msr)
Definition: msr.h:157
#define BIOS_INFO
BIOS_INFO - Expected events.
Definition: loglevel.h:113
#define BIOS_DEBUG
BIOS_DEBUG - Verbose output.
Definition: loglevel.h:128
#define BIOS_ERR
BIOS_ERR - System in incomplete state.
Definition: loglevel.h:72
#define BIOS_WARNING
BIOS_WARNING - Bad configuration.
Definition: loglevel.h:86
unsigned short uint16_t
Definition: stdint.h:11
unsigned int uint32_t
Definition: stdint.h:14
unsigned long uintptr_t
Definition: stdint.h:21
unsigned long long uint64_t
Definition: stdint.h:17
unsigned char uint8_t
Definition: stdint.h:8
uint16_t mc_patch_data_id
uint16_t processor_rev_id
uint8_t reserved2[4]
uint32_t patch_id
uint32_t date_code
uint8_t chipset1_rev_id
uint8_t chipset2_rev_id
uint32_t chipset2_dev_id
uint8_t reserved1[6]
uint32_t chipset1_dev_id
uint8_t m_patch_data[F16H_MPB_MAX_SIZE-F16H_MPB_DATA_OFFSET]
unsigned int hi
Definition: msr.h:112
unsigned int lo
Definition: msr.h:111
#define m(clkreg, src_bits, pmcreg, dst_bits)
#define c(value, pmcreg, dst_bits)