Ronald G. Minnich | 9b6b3d2 | 2005-11-23 04:56:36 +0000 | [diff] [blame] | 1 | /* |
Stefan Reinauer | 7e61e45 | 2008-01-18 10:35:56 +0000 | [diff] [blame] | 2 | * This file is part of the coreboot project. |
Marc Jones | 2006b38 | 2007-12-19 00:47:09 +0000 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2007 Advanced Micro Devices, Inc. |
Audrey Pearson | 83e4c56 | 2015-10-08 12:47:24 -0500 | [diff] [blame] | 5 | * Copyright (C) 2015 Raptor Engineering |
Marc Jones | 2006b38 | 2007-12-19 00:47:09 +0000 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; version 2 of the License. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
Marc Jones | 2006b38 | 2007-12-19 00:47:09 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
Ronald G. Minnich | 9b6b3d2 | 2005-11-23 04:56:36 +0000 | [diff] [blame] | 17 | #include <stdint.h> |
| 18 | #include <console/console.h> |
Ronald G. Minnich | 9b6b3d2 | 2005-11-23 04:56:36 +0000 | [diff] [blame] | 19 | #include <cpu/x86/msr.h> |
Elyes HAOUAS | 1a5f1c8 | 2018-10-31 08:06:12 +0100 | [diff] [blame] | 20 | #include <cpu/amd/msr.h> |
Ronald G. Minnich | 9b6b3d2 | 2005-11-23 04:56:36 +0000 | [diff] [blame] | 21 | #include <cpu/amd/microcode.h> |
Kyösti Mälkki | 5fe1fb7a | 2013-12-08 07:21:05 +0200 | [diff] [blame] | 22 | #include <cbfs.h> |
Kyösti Mälkki | 13f6650 | 2019-03-03 08:01:05 +0200 | [diff] [blame] | 23 | #include <device/mmio.h> |
Timothy Pearson | c764c74 | 2015-08-28 20:48:17 -0500 | [diff] [blame] | 24 | #include <smp/spinlock.h> |
Kyösti Mälkki | 5fe1fb7a | 2013-12-08 07:21:05 +0200 | [diff] [blame] | 25 | |
| 26 | #define UCODE_DEBUG(fmt, args...) \ |
Elyes HAOUAS | cbe7464c | 2016-08-23 21:07:28 +0200 | [diff] [blame] | 27 | do { printk(BIOS_DEBUG, "[microcode] "fmt, ##args); } while (0) |
Marc Jones | 8ae8c88 | 2007-12-19 01:32:08 +0000 | [diff] [blame] | 28 | |
Audrey Pearson | 83e4c56 | 2015-10-08 12:47:24 -0500 | [diff] [blame] | 29 | #define UCODE_MAGIC 0x00414d44 |
| 30 | #define UCODE_EQUIV_CPU_TABLE_TYPE 0x00000000 |
| 31 | #define UCODE_SECTION_START_ID 0x00000001 |
| 32 | #define UCODE_MAGIC 0x00414d44 |
| 33 | |
Elyes HAOUAS | 9d75957 | 2018-05-28 15:41:12 +0200 | [diff] [blame] | 34 | #define F1XH_MPB_MAX_SIZE 2048 |
| 35 | #define F15H_MPB_MAX_SIZE 4096 |
| 36 | #define CONT_HDR 12 |
Audrey Pearson | 83e4c56 | 2015-10-08 12:47:24 -0500 | [diff] [blame] | 37 | #define SECT_HDR 8 |
| 38 | |
| 39 | /* |
| 40 | * STRUCTURE OF A MICROCODE (UCODE) FILE |
| 41 | * Container Header |
| 42 | * Section Header |
| 43 | * Microcode Header |
| 44 | * Microcode "Blob" |
| 45 | * Section Header |
| 46 | * Microcode Header |
| 47 | * Microcode "Blob" |
| 48 | * ... |
| 49 | * ... |
| 50 | * (end of file) |
| 51 | * |
| 52 | * |
| 53 | * CONTAINER HEADER (offset 0 bytes from start of file) |
| 54 | * Total size = fixed size (12 bytes) + variable size |
| 55 | * [0:3] 32-bit unique ID |
| 56 | * [4:7] don't-care |
| 57 | * [8-11] Size (n) in bytes of variable portion of container header |
| 58 | * [12-n] don't-care |
| 59 | * |
| 60 | * SECTION HEADER (offset += 12+n) |
| 61 | * Total size = 8 bytes |
| 62 | * [0:3] Unique identifier signaling start of section (0x00000001) |
| 63 | * [4:7] Total size (m) of following microcode section, including microcode header |
| 64 | * |
| 65 | * MICROCODE HEADER (offset += 8) |
| 66 | * Total size = 64 bytes |
| 67 | * [0:3] Data code (32 bits) |
| 68 | * [4:7] Patch ID (32 bits) |
| 69 | * [8:9] Microcode patch data ID (16 bits) |
| 70 | * [10] c patch data length (8 bits) |
| 71 | * [11] init flag (8 bits) |
| 72 | * [12:15] ucode patch data cksum (32 bits) |
| 73 | * [16:19] nb dev ID (32 bits) |
| 74 | * [20:23] sb dev ID (32 bits) |
| 75 | * [24:25] Processor rev ID (16 bits) |
| 76 | * [26] nb revision ID (8 bits) |
| 77 | * [27] sb revision ID (8 bits) |
| 78 | * [28] BIOS API revision (8 bits) |
| 79 | * [29-31] Reserved 1 (array of three 8-bit values) |
| 80 | * [32-63] Match reg (array of eight 32-bit values) |
| 81 | * |
| 82 | * MICROCODE BLOB (offset += 64) |
| 83 | * Total size = m bytes |
| 84 | * |
| 85 | */ |
| 86 | |
Ronald G. Minnich | 9b6b3d2 | 2005-11-23 04:56:36 +0000 | [diff] [blame] | 87 | struct microcode { |
Audrey Pearson | 83e4c56 | 2015-10-08 12:47:24 -0500 | [diff] [blame] | 88 | uint32_t data_code; |
| 89 | uint32_t patch_id; |
Ronald G. Minnich | 9b6b3d2 | 2005-11-23 04:56:36 +0000 | [diff] [blame] | 90 | |
Audrey Pearson | 83e4c56 | 2015-10-08 12:47:24 -0500 | [diff] [blame] | 91 | uint16_t mc_patch_data_id; |
| 92 | uint8_t mc_patch_data_len; |
| 93 | uint8_t init_flag; |
Ronald G. Minnich | 9b6b3d2 | 2005-11-23 04:56:36 +0000 | [diff] [blame] | 94 | |
Audrey Pearson | 83e4c56 | 2015-10-08 12:47:24 -0500 | [diff] [blame] | 95 | uint32_t mc_patch_data_checksum; |
Ronald G. Minnich | 9b6b3d2 | 2005-11-23 04:56:36 +0000 | [diff] [blame] | 96 | |
Audrey Pearson | 83e4c56 | 2015-10-08 12:47:24 -0500 | [diff] [blame] | 97 | uint32_t nb_dev_id; |
| 98 | uint32_t sb_dev_id; |
Ronald G. Minnich | 9b6b3d2 | 2005-11-23 04:56:36 +0000 | [diff] [blame] | 99 | |
Audrey Pearson | 83e4c56 | 2015-10-08 12:47:24 -0500 | [diff] [blame] | 100 | uint16_t processor_rev_id; |
| 101 | uint8_t nb_rev_id; |
| 102 | uint8_t sb_rev_id; |
Ronald G. Minnich | 9b6b3d2 | 2005-11-23 04:56:36 +0000 | [diff] [blame] | 103 | |
Audrey Pearson | 83e4c56 | 2015-10-08 12:47:24 -0500 | [diff] [blame] | 104 | uint8_t bios_api_rev; |
| 105 | uint8_t reserved1[3]; |
Marc Jones | 2006b38 | 2007-12-19 00:47:09 +0000 | [diff] [blame] | 106 | |
Audrey Pearson | 83e4c56 | 2015-10-08 12:47:24 -0500 | [diff] [blame] | 107 | uint32_t match_reg[8]; |
Marc Jones | 2006b38 | 2007-12-19 00:47:09 +0000 | [diff] [blame] | 108 | |
Audrey Pearson | 83e4c56 | 2015-10-08 12:47:24 -0500 | [diff] [blame] | 109 | uint8_t m_patch_data[896]; |
| 110 | uint8_t resv2[896]; |
Marc Jones | 2006b38 | 2007-12-19 00:47:09 +0000 | [diff] [blame] | 111 | |
Audrey Pearson | 83e4c56 | 2015-10-08 12:47:24 -0500 | [diff] [blame] | 112 | uint8_t x86_code_present; |
| 113 | uint8_t x86_code_entry[191]; |
Ronald G. Minnich | 9b6b3d2 | 2005-11-23 04:56:36 +0000 | [diff] [blame] | 114 | }; |
| 115 | |
Kyösti Mälkki | 5fe1fb7a | 2013-12-08 07:21:05 +0200 | [diff] [blame] | 116 | static void apply_microcode_patch(const struct microcode *m) |
Ronald G. Minnich | 9b6b3d2 | 2005-11-23 04:56:36 +0000 | [diff] [blame] | 117 | { |
Kyösti Mälkki | 5fe1fb7a | 2013-12-08 07:21:05 +0200 | [diff] [blame] | 118 | uint32_t new_patch_id; |
Ronald G. Minnich | 9b6b3d2 | 2005-11-23 04:56:36 +0000 | [diff] [blame] | 119 | msr_t msr; |
Marc Jones | 2006b38 | 2007-12-19 00:47:09 +0000 | [diff] [blame] | 120 | |
Kyösti Mälkki | 5fe1fb7a | 2013-12-08 07:21:05 +0200 | [diff] [blame] | 121 | /* apply patch */ |
| 122 | msr.hi = 0; |
| 123 | msr.lo = (uint32_t)m; |
| 124 | |
Elyes HAOUAS | 1a5f1c8 | 2018-10-31 08:06:12 +0100 | [diff] [blame] | 125 | wrmsr(MSR_PATCH_LOADER, msr); |
Kyösti Mälkki | 5fe1fb7a | 2013-12-08 07:21:05 +0200 | [diff] [blame] | 126 | |
| 127 | UCODE_DEBUG("patch id to apply = 0x%08x\n", m->patch_id); |
| 128 | |
| 129 | /* read the patch_id again */ |
Elyes HAOUAS | a9473ec | 2018-10-24 15:55:53 +0200 | [diff] [blame] | 130 | msr = rdmsr(IA32_BIOS_SIGN_ID); |
Kyösti Mälkki | 5fe1fb7a | 2013-12-08 07:21:05 +0200 | [diff] [blame] | 131 | new_patch_id = msr.lo; |
Ronald G. Minnich | 9b6b3d2 | 2005-11-23 04:56:36 +0000 | [diff] [blame] | 132 | |
Elyes HAOUAS | a342f39 | 2018-10-17 10:56:26 +0200 | [diff] [blame] | 133 | UCODE_DEBUG("updated to patch id = 0x%08x %s\n", new_patch_id, |
Kyösti Mälkki | 5fe1fb7a | 2013-12-08 07:21:05 +0200 | [diff] [blame] | 134 | (new_patch_id == m->patch_id) ? "success" : "fail"); |
| 135 | } |
Ronald G. Minnich | 9b6b3d2 | 2005-11-23 04:56:36 +0000 | [diff] [blame] | 136 | |
Kyösti Mälkki | 5fe1fb7a | 2013-12-08 07:21:05 +0200 | [diff] [blame] | 137 | static void amd_update_microcode(const void *ucode, size_t ucode_len, |
| 138 | uint32_t equivalent_processor_rev_id) |
| 139 | { |
| 140 | const struct microcode *m; |
Kyösti Mälkki | 1608f36 | 2013-12-16 07:19:09 +0200 | [diff] [blame] | 141 | const uint8_t *c = ucode; |
| 142 | const uint8_t *ucode_end = (uint8_t*)ucode + ucode_len; |
Audrey Pearson | 83e4c56 | 2015-10-08 12:47:24 -0500 | [diff] [blame] | 143 | const uint8_t *cur_section_hdr; |
Ronald G. Minnich | 9b6b3d2 | 2005-11-23 04:56:36 +0000 | [diff] [blame] | 144 | |
Audrey Pearson | 83e4c56 | 2015-10-08 12:47:24 -0500 | [diff] [blame] | 145 | uint32_t container_hdr_id; |
| 146 | uint32_t container_hdr_size; |
| 147 | uint32_t blob_size; |
| 148 | uint32_t sec_hdr_id; |
| 149 | |
| 150 | /* Container Header */ |
| 151 | container_hdr_id = read32(c); |
| 152 | if (container_hdr_id != UCODE_MAGIC) { |
| 153 | UCODE_DEBUG("Invalid container header ID\n"); |
| 154 | return; |
| 155 | } |
| 156 | |
| 157 | container_hdr_size = read32(c + 8); |
| 158 | cur_section_hdr = c + CONT_HDR + container_hdr_size; |
| 159 | |
| 160 | /* Read in first section header ID */ |
| 161 | sec_hdr_id = read32(cur_section_hdr); |
| 162 | c = cur_section_hdr + 4; |
| 163 | |
| 164 | /* Loop through sections */ |
| 165 | while (sec_hdr_id == UCODE_SECTION_START_ID && |
| 166 | c <= (ucode_end - F15H_MPB_MAX_SIZE)) { |
| 167 | |
| 168 | blob_size = read32(c); |
| 169 | |
| 170 | m = (struct microcode *)(c + 4); |
| 171 | |
Kyösti Mälkki | 75b68d8 | 2013-12-12 06:53:05 +0200 | [diff] [blame] | 172 | if (m->processor_rev_id == equivalent_processor_rev_id) { |
Kyösti Mälkki | 5fe1fb7a | 2013-12-08 07:21:05 +0200 | [diff] [blame] | 173 | apply_microcode_patch(m); |
Ronald G. Minnich | 9b6b3d2 | 2005-11-23 04:56:36 +0000 | [diff] [blame] | 174 | break; |
| 175 | } |
Audrey Pearson | 83e4c56 | 2015-10-08 12:47:24 -0500 | [diff] [blame] | 176 | |
| 177 | cur_section_hdr = c + 4 + blob_size; |
| 178 | sec_hdr_id = read32(cur_section_hdr); |
Elyes HAOUAS | b67cfbb | 2018-03-27 19:57:51 +0200 | [diff] [blame] | 179 | c = cur_section_hdr + 4; |
Ronald G. Minnich | 9b6b3d2 | 2005-11-23 04:56:36 +0000 | [diff] [blame] | 180 | } |
Kyösti Mälkki | 5fe1fb7a | 2013-12-08 07:21:05 +0200 | [diff] [blame] | 181 | } |
Ronald G. Minnich | 9b6b3d2 | 2005-11-23 04:56:36 +0000 | [diff] [blame] | 182 | |
Audrey Pearson | 83e4c56 | 2015-10-08 12:47:24 -0500 | [diff] [blame] | 183 | static const char *microcode_cbfs_file[] = { |
| 184 | "microcode_amd.bin", |
| 185 | "microcode_amd_fam15h.bin", |
| 186 | }; |
Kyösti Mälkki | 5fe1fb7a | 2013-12-08 07:21:05 +0200 | [diff] [blame] | 187 | |
Audrey Pearson | 83e4c56 | 2015-10-08 12:47:24 -0500 | [diff] [blame] | 188 | void amd_update_microcode_from_cbfs(uint32_t equivalent_processor_rev_id) |
Kyösti Mälkki | 5fe1fb7a | 2013-12-08 07:21:05 +0200 | [diff] [blame] | 189 | { |
| 190 | const void *ucode; |
| 191 | size_t ucode_len; |
| 192 | |
Audrey Pearson | 83e4c56 | 2015-10-08 12:47:24 -0500 | [diff] [blame] | 193 | uint32_t i; |
Kyösti Mälkki | 5fe1fb7a | 2013-12-08 07:21:05 +0200 | [diff] [blame] | 194 | |
Audrey Pearson | 83e4c56 | 2015-10-08 12:47:24 -0500 | [diff] [blame] | 195 | for (i = 0; i < ARRAY_SIZE(microcode_cbfs_file); i++) |
| 196 | { |
| 197 | if (equivalent_processor_rev_id == 0) { |
| 198 | UCODE_DEBUG("rev id not found. Skipping microcode patch!\n"); |
| 199 | return; |
| 200 | } |
Kyösti Mälkki | 5fe1fb7a | 2013-12-08 07:21:05 +0200 | [diff] [blame] | 201 | |
Timothy Pearson | c764c74 | 2015-08-28 20:48:17 -0500 | [diff] [blame] | 202 | #ifdef __PRE_RAM__ |
| 203 | #if IS_ENABLED(CONFIG_HAVE_ROMSTAGE_MICROCODE_CBFS_SPINLOCK) |
| 204 | spin_lock(romstage_microcode_cbfs_lock()); |
| 205 | #endif |
| 206 | #endif |
| 207 | |
Audrey Pearson | 83e4c56 | 2015-10-08 12:47:24 -0500 | [diff] [blame] | 208 | ucode = cbfs_boot_map_with_leak(microcode_cbfs_file[i], |
| 209 | CBFS_TYPE_MICROCODE, &ucode_len); |
| 210 | if (!ucode) { |
| 211 | UCODE_DEBUG("microcode file not found. Skipping updates.\n"); |
Timothy Pearson | c764c74 | 2015-08-28 20:48:17 -0500 | [diff] [blame] | 212 | #ifdef __PRE_RAM__ |
| 213 | #if IS_ENABLED(CONFIG_HAVE_ROMSTAGE_MICROCODE_CBFS_SPINLOCK) |
| 214 | spin_unlock(romstage_microcode_cbfs_lock()); |
| 215 | #endif |
| 216 | #endif |
Audrey Pearson | 83e4c56 | 2015-10-08 12:47:24 -0500 | [diff] [blame] | 217 | return; |
| 218 | } |
| 219 | |
| 220 | amd_update_microcode(ucode, ucode_len, equivalent_processor_rev_id); |
Timothy Pearson | c764c74 | 2015-08-28 20:48:17 -0500 | [diff] [blame] | 221 | |
| 222 | #ifdef __PRE_RAM__ |
| 223 | #if IS_ENABLED(CONFIG_HAVE_ROMSTAGE_MICROCODE_CBFS_SPINLOCK) |
| 224 | spin_unlock(romstage_microcode_cbfs_lock()); |
| 225 | #endif |
| 226 | #endif |
Audrey Pearson | 83e4c56 | 2015-10-08 12:47:24 -0500 | [diff] [blame] | 227 | } |
Ronald G. Minnich | 9b6b3d2 | 2005-11-23 04:56:36 +0000 | [diff] [blame] | 228 | } |