| 1 |
|
| 2 |
|
| 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> |
| 14 |
|
| 15 |
#define CPU_MICROCODE_BLOB_NAME "cpu_microcode_XXXX.bin" |
| 16 |
#define CPU_MICROCODE_BLOB_FORMAT "cpu_microcode_%04x.bin" |
| 17 |
|
| 18 |
struct microcode { |
| 19 |
uint32_t date_code; |
| 20 |
uint32_t patch_id; |
| 21 |
|
| 22 |
uint16_t mc_patch_data_id; |
| 23 |
uint8_t reserved1[6]; |
| 24 |
|
| 25 |
uint32_t chipset1_dev_id; |
| 26 |
uint32_t chipset2_dev_id; |
| 27 |
|
| 28 |
uint16_t processor_rev_id; |
| 29 |
|
| 30 |
uint8_t chipset1_rev_id; |
| 31 |
uint8_t chipset2_rev_id; |
| 32 |
|
| 33 |
uint8_t reserved2[4]; |
| 34 |
} __packed; |
| 35 |
|
| 36 |
static const struct microcode *ucode; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
void amd_apply_microcode_patch(void) |
| 41 |
{ |
| 42 |
uint32_t new_patch_id; |
| 43 |
msr_t msr; |
| 44 |
|
| 45 |
if (!ucode) { |
| 46 |
printk(BIOS_ERR, "%s: NULL pointer to microcode\n", __func__); |
| 47 |
return; |
| 48 |
} |
| 49 |
|
| 50 |
msr.raw = (uintptr_t)ucode; |
| 51 |
|
| 52 |
wrmsr(MSR_PATCH_LOADER, msr); |
| 53 |
|
| 54 |
printk(BIOS_DEBUG, "microcode: patch id to apply = 0x%08x\n", ucode->patch_id); |
| 55 |
|
| 56 |
msr = rdmsr(IA32_BIOS_SIGN_ID); |
| 57 |
new_patch_id = msr.lo; |
| 58 |
|
| 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); |
| 65 |
} |
| 66 |
|
| 67 |
static uint16_t get_equivalent_processor_rev_id(void) |
| 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 *microcode, |
| 75 |
uint16_t equivalent_processor_rev_id) |
| 76 |
{ |
| 77 |
if (microcode->processor_rev_id == equivalent_processor_rev_id) |
| 78 |
return microcode; |
| 79 |
|
| 80 |
printk(BIOS_WARNING, "Failed to find microcode for processor rev: %hx.\n", |
| 81 |
equivalent_processor_rev_id); |
| 82 |
|
| 83 |
return NULL; |
| 84 |
} |
| 85 |
|
| 86 |
void amd_load_microcode_from_cbfs(void) |
| 87 |
{ |
| 88 |
char name[] = CPU_MICROCODE_BLOB_NAME; |
| 89 |
uint16_t equivalent_processor_rev_id; |
| 90 |
|
| 91 |
if (ucode) |
| 92 |
return; |
| 93 |
|
| 94 |
|
| 95 |
|
| 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); |
| 99 |
|
| 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; |
| 105 |
} |
| 106 |
|
| 107 |
if (find_microcode(ucode, equivalent_processor_rev_id) == NULL) { |
| 108 |
cbfs_unmap((void *)ucode); |
| 109 |
ucode = NULL; |
| 110 |
} |
| 111 |
|
| 112 |
timestamp_add_now(TS_READ_UCODE_END); |
| 113 |
} |
| 114 |
|
| 115 |
void amd_free_microcode(void) |
| 116 |
{ |
| 117 |
if (ucode) { |
| 118 |
cbfs_unmap((void *)ucode); |
| 119 |
ucode = NULL; |
| 120 |
} |
| 121 |
} |