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 <arch/cpu.h>
4 #include <stdint.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 #include <timestamp.h>
12 
13 #define CPU_MICROCODE_BLOB_NAME "cpu_microcode_blob.bin"
14 
15 _Static_assert(CONFIG_SOC_AMD_COMMON_BLOCK_UCODE_SIZE > 0,
16  "SOC_AMD_COMMON_BLOCK_UCODE_SIZE is not set");
17 
18 #define MPB_MAX_SIZE CONFIG_SOC_AMD_COMMON_BLOCK_UCODE_SIZE
19 #define MPB_DATA_OFFSET 32
20 
21 struct microcode {
24 
26  uint8_t reserved1[6];
27 
30 
32 
35 
36  uint8_t reserved2[4];
37 
40 
41 _Static_assert(sizeof(struct microcode) == MPB_MAX_SIZE, "microcode size is invalid");
42 
43 static void apply_microcode_patch(const struct microcode *m)
44 {
45  uint32_t new_patch_id;
46  msr_t msr;
47 
48  msr.hi = (uint64_t)(uintptr_t)m >> 32;
49  msr.lo = (uintptr_t)m & 0xffffffff;
50 
51  wrmsr(MSR_PATCH_LOADER, msr);
52 
53  printk(BIOS_DEBUG, "microcode: patch id to apply = 0x%08x\n",
54  m->patch_id);
55 
56  msr = rdmsr(IA32_BIOS_SIGN_ID);
57  new_patch_id = msr.lo;
58 
59  if (new_patch_id == m->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);
65 }
66 
68 {
69  uint32_t cpuid_family = cpuid_eax(1);
70 
71  return (uint16_t)((cpuid_family & 0xff0000) >> 8 | (cpuid_family & 0xff));
72 }
73 
74 static const struct microcode *find_microcode(const struct microcode *ucode, size_t ucode_len)
75 {
76  uint16_t equivalent_processor_rev_id = get_equivalent_processor_rev_id();
77  const struct microcode *m;
78 
79  for (m = ucode; m < ucode + ucode_len / MPB_MAX_SIZE; m++) {
80  if (m->processor_rev_id == equivalent_processor_rev_id)
81  return m;
82  }
83 
84  printk(BIOS_WARNING, "Failed to find microcode for processor rev: %hx.\n",
85  equivalent_processor_rev_id);
86 
87  return NULL;
88 }
89 
91 {
92  static struct microcode ucode_cache;
93  static bool cache_valid;
94 
95  struct microcode *ucode_list;
96  const struct microcode *matching_ucode;
97  size_t ucode_len;
98 
99  /* Cache the buffer so each CPU doesn't need to read the uCode from flash */
100  if (!cache_valid) {
102  ucode_list = cbfs_map(CPU_MICROCODE_BLOB_NAME, &ucode_len);
103  if (!ucode_list) {
105  CPU_MICROCODE_BLOB_NAME " not found. Skipping updates.\n");
106  return;
107  }
108 
109  matching_ucode = find_microcode(ucode_list, ucode_len);
110 
111  if (!matching_ucode) {
112  cbfs_unmap(ucode_list);
113  return;
114  }
115 
116  ucode_cache = *matching_ucode;
117  cache_valid = true;
118 
119  cbfs_unmap(ucode_list);
120 
122  }
123 
124  apply_microcode_patch(&ucode_cache);
125 }
126 
128 {
129  if (!CONFIG(CBFS_PRELOAD))
130  return;
131 
132  printk(BIOS_DEBUG, "Preloading microcode %s\n", CPU_MICROCODE_BLOB_NAME);
134 }
static unsigned int cpuid_eax(unsigned int op)
Definition: cpu.h:79
void cbfs_unmap(void *mapping)
Definition: cbfs.c:86
static void * cbfs_map(const char *name, size_t *size_out)
Definition: cbfs.h:246
void cbfs_preload(const char *name)
Definition: cbfs.c:299
#define printk(level,...)
Definition: stdlib.h:16
void amd_update_microcode_from_cbfs(void)
struct microcode __packed
@ CONFIG
Definition: dsi_common.h:201
#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
void timestamp_add_now(enum timestamp_id id)
Definition: timestamp.c:141
#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
_Static_assert(CONFIG_SOC_AMD_COMMON_BLOCK_UCODE_SIZE > 0, "SOC_AMD_COMMON_BLOCK_UCODE_SIZE is not set")
#define MPB_MAX_SIZE
static uint16_t get_equivalent_processor_rev_id(void)
#define CPU_MICROCODE_BLOB_NAME
void preload_microcode(void)
static void apply_microcode_patch(const struct microcode *m)
#define MPB_DATA_OFFSET
static const struct microcode * find_microcode(const struct microcode *ucode, size_t ucode_len)
#define NULL
Definition: stddef.h:19
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)
@ TS_READ_UCODE_END
@ TS_READ_UCODE_START