Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 1 | /* |
| 2 | * AMD CPU Microcode Update Driver for Linux |
Borislav Petkov | 597e11a | 2011-12-02 18:09:23 +0100 | [diff] [blame] | 3 | * Copyright (C) 2008-2011 Advanced Micro Devices Inc. |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 4 | * |
| 5 | * Author: Peter Oruba <peter.oruba@amd.com> |
| 6 | * |
| 7 | * Based on work by: |
| 8 | * Tigran Aivazian <tigran@aivazian.fsnet.co.uk> |
| 9 | * |
Borislav Petkov | 597e11a | 2011-12-02 18:09:23 +0100 | [diff] [blame] | 10 | * Maintainers: |
| 11 | * Andreas Herrmann <andreas.herrmann3@amd.com> |
| 12 | * Borislav Petkov <borislav.petkov@amd.com> |
| 13 | * |
| 14 | * This driver allows to upgrade microcode on F10h AMD |
| 15 | * CPUs and later. |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 16 | * |
Andreas Herrmann | 2a3282a7 | 2008-12-16 19:08:53 +0100 | [diff] [blame] | 17 | * Licensed under the terms of the GNU General Public |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 18 | * License version 2. See file COPYING for details. |
Ingo Molnar | 4bae196 | 2009-03-11 11:19:46 +0100 | [diff] [blame] | 19 | */ |
Joe Perches | f58e1f5 | 2009-12-08 22:30:50 -0800 | [diff] [blame] | 20 | |
| 21 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 22 | |
Ingo Molnar | 4bae196 | 2009-03-11 11:19:46 +0100 | [diff] [blame] | 23 | #include <linux/firmware.h> |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 24 | #include <linux/pci_ids.h> |
Andreas Herrmann | be95776 | 2008-12-16 19:11:23 +0100 | [diff] [blame] | 25 | #include <linux/uaccess.h> |
Ingo Molnar | 4bae196 | 2009-03-11 11:19:46 +0100 | [diff] [blame] | 26 | #include <linux/vmalloc.h> |
| 27 | #include <linux/kernel.h> |
| 28 | #include <linux/module.h> |
Ingo Molnar | 4bae196 | 2009-03-11 11:19:46 +0100 | [diff] [blame] | 29 | #include <linux/pci.h> |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 30 | |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 31 | #include <asm/microcode.h> |
Ingo Molnar | 4bae196 | 2009-03-11 11:19:46 +0100 | [diff] [blame] | 32 | #include <asm/processor.h> |
| 33 | #include <asm/msr.h> |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 34 | |
| 35 | MODULE_DESCRIPTION("AMD Microcode Update Driver"); |
Peter Oruba | 3c52204 | 2008-10-17 15:30:38 +0200 | [diff] [blame] | 36 | MODULE_AUTHOR("Peter Oruba"); |
Ingo Molnar | 5d7b605 | 2008-07-29 10:07:36 +0200 | [diff] [blame] | 37 | MODULE_LICENSE("GPL v2"); |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 38 | |
| 39 | #define UCODE_MAGIC 0x00414d44 |
| 40 | #define UCODE_EQUIV_CPU_TABLE_TYPE 0x00000000 |
| 41 | #define UCODE_UCODE_TYPE 0x00000001 |
| 42 | |
Dmitry Adamushko | 18dbc91 | 2008-09-23 12:08:44 +0200 | [diff] [blame] | 43 | struct equiv_cpu_entry { |
Andreas Herrmann | 5549b94 | 2008-12-16 19:21:30 +0100 | [diff] [blame] | 44 | u32 installed_cpu; |
| 45 | u32 fixed_errata_mask; |
| 46 | u32 fixed_errata_compare; |
| 47 | u16 equiv_cpu; |
| 48 | u16 res; |
| 49 | } __attribute__((packed)); |
Dmitry Adamushko | 18dbc91 | 2008-09-23 12:08:44 +0200 | [diff] [blame] | 50 | |
| 51 | struct microcode_header_amd { |
Andreas Herrmann | 5549b94 | 2008-12-16 19:21:30 +0100 | [diff] [blame] | 52 | u32 data_code; |
| 53 | u32 patch_id; |
| 54 | u16 mc_patch_data_id; |
| 55 | u8 mc_patch_data_len; |
| 56 | u8 init_flag; |
| 57 | u32 mc_patch_data_checksum; |
| 58 | u32 nb_dev_id; |
| 59 | u32 sb_dev_id; |
| 60 | u16 processor_rev_id; |
| 61 | u8 nb_rev_id; |
| 62 | u8 sb_rev_id; |
| 63 | u8 bios_api_rev; |
| 64 | u8 reserved1[3]; |
| 65 | u32 match_reg[8]; |
| 66 | } __attribute__((packed)); |
Dmitry Adamushko | 18dbc91 | 2008-09-23 12:08:44 +0200 | [diff] [blame] | 67 | |
| 68 | struct microcode_amd { |
Ingo Molnar | 4bae196 | 2009-03-11 11:19:46 +0100 | [diff] [blame] | 69 | struct microcode_header_amd hdr; |
| 70 | unsigned int mpb[0]; |
Dmitry Adamushko | 18dbc91 | 2008-09-23 12:08:44 +0200 | [diff] [blame] | 71 | }; |
| 72 | |
Borislav Petkov | 40b7f3d | 2011-06-15 15:34:57 +0200 | [diff] [blame] | 73 | #define SECTION_HDR_SIZE 8 |
| 74 | #define CONTAINER_HDR_SZ 12 |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 75 | |
Dmitry Adamushko | a0a29b6 | 2008-09-11 23:27:52 +0200 | [diff] [blame] | 76 | static struct equiv_cpu_entry *equiv_cpu_table; |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 77 | |
Borislav Petkov | a3eb3b4 | 2012-08-01 15:38:18 +0200 | [diff] [blame] | 78 | struct ucode_patch { |
| 79 | struct list_head plist; |
| 80 | void *data; |
| 81 | u32 patch_id; |
| 82 | u16 equiv_cpu; |
| 83 | }; |
| 84 | |
| 85 | static LIST_HEAD(pcache); |
| 86 | |
Borislav Petkov | c96d2c0 | 2012-08-01 14:55:01 +0200 | [diff] [blame] | 87 | static u16 find_equiv_id(unsigned int cpu) |
| 88 | { |
| 89 | struct ucode_cpu_info *uci = ucode_cpu_info + cpu; |
| 90 | int i = 0; |
| 91 | |
Borislav Petkov | a3eb3b4 | 2012-08-01 15:38:18 +0200 | [diff] [blame] | 92 | if (!equiv_cpu_table) |
| 93 | return 0; |
Borislav Petkov | c96d2c0 | 2012-08-01 14:55:01 +0200 | [diff] [blame] | 94 | |
| 95 | while (equiv_cpu_table[i].installed_cpu != 0) { |
| 96 | if (uci->cpu_sig.sig == equiv_cpu_table[i].installed_cpu) |
| 97 | return equiv_cpu_table[i].equiv_cpu; |
| 98 | |
| 99 | i++; |
| 100 | } |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | static u32 find_cpu_family_by_equiv_cpu(u16 equiv_cpu) |
| 105 | { |
| 106 | int i = 0; |
| 107 | |
| 108 | BUG_ON(!equiv_cpu_table); |
| 109 | |
| 110 | while (equiv_cpu_table[i].equiv_cpu != 0) { |
| 111 | if (equiv_cpu == equiv_cpu_table[i].equiv_cpu) |
| 112 | return equiv_cpu_table[i].installed_cpu; |
| 113 | i++; |
| 114 | } |
| 115 | return 0; |
| 116 | } |
| 117 | |
Borislav Petkov | a3eb3b4 | 2012-08-01 15:38:18 +0200 | [diff] [blame] | 118 | /* |
| 119 | * a small, trivial cache of per-family ucode patches |
| 120 | */ |
| 121 | static struct ucode_patch *cache_find_patch(u16 equiv_cpu) |
| 122 | { |
| 123 | struct ucode_patch *p; |
| 124 | |
| 125 | list_for_each_entry(p, &pcache, plist) |
| 126 | if (p->equiv_cpu == equiv_cpu) |
| 127 | return p; |
| 128 | return NULL; |
| 129 | } |
| 130 | |
| 131 | static void update_cache(struct ucode_patch *new_patch) |
| 132 | { |
| 133 | struct ucode_patch *p; |
| 134 | |
| 135 | list_for_each_entry(p, &pcache, plist) { |
| 136 | if (p->equiv_cpu == new_patch->equiv_cpu) { |
| 137 | if (p->patch_id >= new_patch->patch_id) |
| 138 | /* we already have the latest patch */ |
| 139 | return; |
| 140 | |
| 141 | list_replace(&p->plist, &new_patch->plist); |
| 142 | kfree(p->data); |
| 143 | kfree(p); |
| 144 | return; |
| 145 | } |
| 146 | } |
| 147 | /* no patch found, add it */ |
| 148 | list_add_tail(&new_patch->plist, &pcache); |
| 149 | } |
| 150 | |
| 151 | static void free_cache(void) |
| 152 | { |
| 153 | struct ucode_patch *p; |
| 154 | |
| 155 | list_for_each_entry_reverse(p, &pcache, plist) { |
| 156 | __list_del(p->plist.prev, p->plist.next); |
| 157 | kfree(p->data); |
| 158 | kfree(p); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | static struct ucode_patch *find_patch(unsigned int cpu) |
| 163 | { |
| 164 | u16 equiv_id; |
| 165 | |
| 166 | equiv_id = find_equiv_id(cpu); |
| 167 | if (!equiv_id) |
| 168 | return NULL; |
| 169 | |
| 170 | return cache_find_patch(equiv_id); |
| 171 | } |
| 172 | |
Dmitry Adamushko | d45de40 | 2008-08-20 00:22:26 +0200 | [diff] [blame] | 173 | static int collect_cpu_info_amd(int cpu, struct cpu_signature *csig) |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 174 | { |
Andreas Herrmann | 3b2e3d8 | 2010-01-22 21:34:56 +0100 | [diff] [blame] | 175 | struct cpuinfo_x86 *c = &cpu_data(cpu); |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 176 | |
Borislav Petkov | 5f5b747 | 2012-07-25 20:06:54 +0200 | [diff] [blame] | 177 | csig->sig = cpuid_eax(0x00000001); |
Borislav Petkov | bcb80e5 | 2011-10-17 16:34:36 +0200 | [diff] [blame] | 178 | csig->rev = c->microcode; |
Borislav Petkov | 258721e | 2011-01-05 18:13:19 +0100 | [diff] [blame] | 179 | pr_info("CPU%d: patch_level=0x%08x\n", cpu, csig->rev); |
| 180 | |
Dmitry Adamushko | d45de40 | 2008-08-20 00:22:26 +0200 | [diff] [blame] | 181 | return 0; |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 182 | } |
| 183 | |
Borislav Petkov | 2efb05e | 2012-08-01 16:16:13 +0200 | [diff] [blame^] | 184 | static unsigned int verify_patch_size(int cpu, u32 patch_size, |
Borislav Petkov | be62adb | 2011-12-02 18:02:17 +0100 | [diff] [blame] | 185 | unsigned int size) |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 186 | { |
Borislav Petkov | be62adb | 2011-12-02 18:02:17 +0100 | [diff] [blame] | 187 | struct cpuinfo_x86 *c = &cpu_data(cpu); |
| 188 | u32 max_size; |
| 189 | |
| 190 | #define F1XH_MPB_MAX_SIZE 2048 |
| 191 | #define F14H_MPB_MAX_SIZE 1824 |
| 192 | #define F15H_MPB_MAX_SIZE 4096 |
| 193 | |
| 194 | switch (c->x86) { |
| 195 | case 0x14: |
| 196 | max_size = F14H_MPB_MAX_SIZE; |
| 197 | break; |
| 198 | case 0x15: |
| 199 | max_size = F15H_MPB_MAX_SIZE; |
| 200 | break; |
| 201 | default: |
| 202 | max_size = F1XH_MPB_MAX_SIZE; |
| 203 | break; |
| 204 | } |
| 205 | |
| 206 | if (patch_size > min_t(u32, size, max_size)) { |
| 207 | pr_err("patch size mismatch\n"); |
| 208 | return 0; |
| 209 | } |
| 210 | |
| 211 | return patch_size; |
| 212 | } |
| 213 | |
Dmitry Adamushko | 871b72d | 2009-05-11 23:48:27 +0200 | [diff] [blame] | 214 | static int apply_microcode_amd(int cpu) |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 215 | { |
Borislav Petkov | bcb80e5 | 2011-10-17 16:34:36 +0200 | [diff] [blame] | 216 | struct cpuinfo_x86 *c = &cpu_data(cpu); |
Borislav Petkov | 2efb05e | 2012-08-01 16:16:13 +0200 | [diff] [blame^] | 217 | struct microcode_amd *mc_amd; |
| 218 | struct ucode_cpu_info *uci; |
| 219 | struct ucode_patch *p; |
| 220 | u32 rev, dummy; |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 221 | |
Borislav Petkov | 2efb05e | 2012-08-01 16:16:13 +0200 | [diff] [blame^] | 222 | BUG_ON(raw_smp_processor_id() != cpu); |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 223 | |
Borislav Petkov | 2efb05e | 2012-08-01 16:16:13 +0200 | [diff] [blame^] | 224 | uci = ucode_cpu_info + cpu; |
| 225 | |
| 226 | p = find_patch(cpu); |
| 227 | if (!p) |
Dmitry Adamushko | 871b72d | 2009-05-11 23:48:27 +0200 | [diff] [blame] | 228 | return 0; |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 229 | |
Borislav Petkov | 2efb05e | 2012-08-01 16:16:13 +0200 | [diff] [blame^] | 230 | mc_amd = p->data; |
| 231 | uci->mc = p->data; |
| 232 | |
Andreas Herrmann | 29d0887 | 2008-12-16 19:16:34 +0100 | [diff] [blame] | 233 | rdmsr(MSR_AMD64_PATCH_LEVEL, rev, dummy); |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 234 | |
Borislav Petkov | 685ca6d | 2012-06-20 16:17:51 +0200 | [diff] [blame] | 235 | /* need to apply patch? */ |
| 236 | if (rev >= mc_amd->hdr.patch_id) { |
| 237 | c->microcode = rev; |
| 238 | return 0; |
| 239 | } |
| 240 | |
| 241 | wrmsrl(MSR_AMD64_PATCH_LOADER, (u64)(long)&mc_amd->hdr.data_code); |
| 242 | |
| 243 | /* verify patch application was successful */ |
| 244 | rdmsr(MSR_AMD64_PATCH_LEVEL, rev, dummy); |
Dmitry Adamushko | 18dbc91 | 2008-09-23 12:08:44 +0200 | [diff] [blame] | 245 | if (rev != mc_amd->hdr.patch_id) { |
Borislav Petkov | 258721e | 2011-01-05 18:13:19 +0100 | [diff] [blame] | 246 | pr_err("CPU%d: update failed for patch_level=0x%08x\n", |
Joe Perches | f58e1f5 | 2009-12-08 22:30:50 -0800 | [diff] [blame] | 247 | cpu, mc_amd->hdr.patch_id); |
Dmitry Adamushko | 871b72d | 2009-05-11 23:48:27 +0200 | [diff] [blame] | 248 | return -1; |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 249 | } |
| 250 | |
Borislav Petkov | 258721e | 2011-01-05 18:13:19 +0100 | [diff] [blame] | 251 | pr_info("CPU%d: new patch_level=0x%08x\n", cpu, rev); |
Dmitry Adamushko | d45de40 | 2008-08-20 00:22:26 +0200 | [diff] [blame] | 252 | uci->cpu_sig.rev = rev; |
Borislav Petkov | bcb80e5 | 2011-10-17 16:34:36 +0200 | [diff] [blame] | 253 | c->microcode = rev; |
Dmitry Adamushko | 871b72d | 2009-05-11 23:48:27 +0200 | [diff] [blame] | 254 | |
| 255 | return 0; |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 256 | } |
| 257 | |
Andreas Herrmann | 0657d9e | 2008-12-16 19:14:05 +0100 | [diff] [blame] | 258 | static int install_equiv_cpu_table(const u8 *buf) |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 259 | { |
Borislav Petkov | 10de52d | 2010-12-30 22:10:12 +0100 | [diff] [blame] | 260 | unsigned int *ibuf = (unsigned int *)buf; |
| 261 | unsigned int type = ibuf[1]; |
| 262 | unsigned int size = ibuf[2]; |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 263 | |
Borislav Petkov | 10de52d | 2010-12-30 22:10:12 +0100 | [diff] [blame] | 264 | if (type != UCODE_EQUIV_CPU_TABLE_TYPE || !size) { |
Borislav Petkov | 258721e | 2011-01-05 18:13:19 +0100 | [diff] [blame] | 265 | pr_err("empty section/" |
| 266 | "invalid type field in container file section header\n"); |
Borislav Petkov | 10de52d | 2010-12-30 22:10:12 +0100 | [diff] [blame] | 267 | return -EINVAL; |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 268 | } |
| 269 | |
Jesper Juhl | 8e5e952 | 2010-11-09 00:08:11 +0100 | [diff] [blame] | 270 | equiv_cpu_table = vmalloc(size); |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 271 | if (!equiv_cpu_table) { |
Joe Perches | f58e1f5 | 2009-12-08 22:30:50 -0800 | [diff] [blame] | 272 | pr_err("failed to allocate equivalent CPU table\n"); |
Borislav Petkov | 10de52d | 2010-12-30 22:10:12 +0100 | [diff] [blame] | 273 | return -ENOMEM; |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 274 | } |
| 275 | |
Borislav Petkov | e7e632f | 2012-07-20 14:12:21 +0200 | [diff] [blame] | 276 | memcpy(equiv_cpu_table, buf + CONTAINER_HDR_SZ, size); |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 277 | |
Borislav Petkov | 40b7f3d | 2011-06-15 15:34:57 +0200 | [diff] [blame] | 278 | /* add header length */ |
| 279 | return size + CONTAINER_HDR_SZ; |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 280 | } |
| 281 | |
Dmitry Adamushko | a0a29b6 | 2008-09-11 23:27:52 +0200 | [diff] [blame] | 282 | static void free_equiv_cpu_table(void) |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 283 | { |
Figo.zhang | aeef50b | 2009-06-07 22:30:36 +0800 | [diff] [blame] | 284 | vfree(equiv_cpu_table); |
| 285 | equiv_cpu_table = NULL; |
Dmitry Adamushko | a0a29b6 | 2008-09-11 23:27:52 +0200 | [diff] [blame] | 286 | } |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 287 | |
Borislav Petkov | 2efb05e | 2012-08-01 16:16:13 +0200 | [diff] [blame^] | 288 | static void cleanup(void) |
Dmitry Adamushko | a0a29b6 | 2008-09-11 23:27:52 +0200 | [diff] [blame] | 289 | { |
Borislav Petkov | 2efb05e | 2012-08-01 16:16:13 +0200 | [diff] [blame^] | 290 | free_equiv_cpu_table(); |
| 291 | free_cache(); |
| 292 | } |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 293 | |
Borislav Petkov | 2efb05e | 2012-08-01 16:16:13 +0200 | [diff] [blame^] | 294 | /* |
| 295 | * We return the current size even if some of the checks failed so that |
| 296 | * we can skip over the next patch. If we return a negative value, we |
| 297 | * signal a grave error like a memory allocation has failed and the |
| 298 | * driver cannot continue functioning normally. In such cases, we tear |
| 299 | * down everything we've used up so far and exit. |
| 300 | */ |
| 301 | static int verify_and_add_patch(unsigned int cpu, u8 *fw, unsigned int leftover) |
| 302 | { |
| 303 | struct cpuinfo_x86 *c = &cpu_data(cpu); |
| 304 | struct microcode_header_amd *mc_hdr; |
| 305 | struct ucode_patch *patch; |
| 306 | unsigned int patch_size, crnt_size, ret; |
| 307 | u32 proc_fam; |
| 308 | u16 proc_id; |
| 309 | |
| 310 | patch_size = *(u32 *)(fw + 4); |
| 311 | crnt_size = patch_size + SECTION_HDR_SIZE; |
| 312 | mc_hdr = (struct microcode_header_amd *)(fw + SECTION_HDR_SIZE); |
| 313 | proc_id = mc_hdr->processor_rev_id; |
| 314 | |
| 315 | proc_fam = find_cpu_family_by_equiv_cpu(proc_id); |
| 316 | if (!proc_fam) { |
| 317 | pr_err("No patch family for equiv ID: 0x%04x\n", proc_id); |
| 318 | return crnt_size; |
| 319 | } |
| 320 | |
| 321 | /* check if patch is for the current family */ |
| 322 | proc_fam = ((proc_fam >> 8) & 0xf) + ((proc_fam >> 20) & 0xff); |
| 323 | if (proc_fam != c->x86) |
| 324 | return crnt_size; |
| 325 | |
| 326 | if (mc_hdr->nb_dev_id || mc_hdr->sb_dev_id) { |
| 327 | pr_err("Patch-ID 0x%08x: chipset-specific code unsupported.\n", |
| 328 | mc_hdr->patch_id); |
| 329 | return crnt_size; |
| 330 | } |
| 331 | |
| 332 | ret = verify_patch_size(cpu, patch_size, leftover); |
| 333 | if (!ret) { |
| 334 | pr_err("Patch-ID 0x%08x: size mismatch.\n", mc_hdr->patch_id); |
| 335 | return crnt_size; |
| 336 | } |
| 337 | |
| 338 | patch = kzalloc(sizeof(*patch), GFP_KERNEL); |
| 339 | if (!patch) { |
| 340 | pr_err("Patch allocation failure.\n"); |
| 341 | return -EINVAL; |
| 342 | } |
| 343 | |
| 344 | patch->data = kzalloc(patch_size, GFP_KERNEL); |
| 345 | if (!patch->data) { |
| 346 | pr_err("Patch data allocation failure.\n"); |
| 347 | kfree(patch); |
| 348 | return -EINVAL; |
| 349 | } |
| 350 | |
| 351 | /* All looks ok, copy patch... */ |
| 352 | memcpy(patch->data, fw + SECTION_HDR_SIZE, patch_size); |
| 353 | INIT_LIST_HEAD(&patch->plist); |
| 354 | patch->patch_id = mc_hdr->patch_id; |
| 355 | patch->equiv_cpu = proc_id; |
| 356 | |
| 357 | /* ... and add to cache. */ |
| 358 | update_cache(patch); |
| 359 | |
| 360 | return crnt_size; |
| 361 | } |
| 362 | |
| 363 | static enum ucode_state load_microcode_amd(int cpu, const u8 *data, size_t size) |
| 364 | { |
| 365 | enum ucode_state ret = UCODE_ERROR; |
| 366 | unsigned int leftover; |
| 367 | u8 *fw = (u8 *)data; |
| 368 | int crnt_size = 0; |
| 369 | int offset; |
| 370 | |
| 371 | offset = install_equiv_cpu_table(data); |
Borislav Petkov | 10de52d | 2010-12-30 22:10:12 +0100 | [diff] [blame] | 372 | if (offset < 0) { |
Joe Perches | f58e1f5 | 2009-12-08 22:30:50 -0800 | [diff] [blame] | 373 | pr_err("failed to create equivalent cpu table\n"); |
Borislav Petkov | 2efb05e | 2012-08-01 16:16:13 +0200 | [diff] [blame^] | 374 | return ret; |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 375 | } |
Borislav Petkov | 2efb05e | 2012-08-01 16:16:13 +0200 | [diff] [blame^] | 376 | fw += offset; |
Dmitry Adamushko | a0a29b6 | 2008-09-11 23:27:52 +0200 | [diff] [blame] | 377 | leftover = size - offset; |
| 378 | |
Borislav Petkov | 2efb05e | 2012-08-01 16:16:13 +0200 | [diff] [blame^] | 379 | if (*(u32 *)fw != UCODE_UCODE_TYPE) { |
Borislav Petkov | be62adb | 2011-12-02 18:02:17 +0100 | [diff] [blame] | 380 | pr_err("invalid type field in container file section header\n"); |
Borislav Petkov | 2efb05e | 2012-08-01 16:16:13 +0200 | [diff] [blame^] | 381 | free_equiv_cpu_table(); |
| 382 | return ret; |
Borislav Petkov | be62adb | 2011-12-02 18:02:17 +0100 | [diff] [blame] | 383 | } |
| 384 | |
Dmitry Adamushko | a0a29b6 | 2008-09-11 23:27:52 +0200 | [diff] [blame] | 385 | while (leftover) { |
Borislav Petkov | 2efb05e | 2012-08-01 16:16:13 +0200 | [diff] [blame^] | 386 | crnt_size = verify_and_add_patch(cpu, fw, leftover); |
| 387 | if (crnt_size < 0) |
| 388 | return ret; |
Borislav Petkov | d733689 | 2011-12-07 17:26:56 +0100 | [diff] [blame] | 389 | |
Borislav Petkov | 2efb05e | 2012-08-01 16:16:13 +0200 | [diff] [blame^] | 390 | fw += crnt_size; |
| 391 | leftover -= crnt_size; |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 392 | } |
Dmitry Adamushko | a0a29b6 | 2008-09-11 23:27:52 +0200 | [diff] [blame] | 393 | |
Borislav Petkov | 2efb05e | 2012-08-01 16:16:13 +0200 | [diff] [blame^] | 394 | return UCODE_OK; |
Dmitry Adamushko | a0a29b6 | 2008-09-11 23:27:52 +0200 | [diff] [blame] | 395 | } |
| 396 | |
Andreas Herrmann | 5b68edc | 2012-01-20 17:44:12 +0100 | [diff] [blame] | 397 | /* |
| 398 | * AMD microcode firmware naming convention, up to family 15h they are in |
| 399 | * the legacy file: |
| 400 | * |
| 401 | * amd-ucode/microcode_amd.bin |
| 402 | * |
| 403 | * This legacy file is always smaller than 2K in size. |
| 404 | * |
Borislav Petkov | 2efb05e | 2012-08-01 16:16:13 +0200 | [diff] [blame^] | 405 | * Beginning with family 15h, they are in family-specific firmware files: |
Andreas Herrmann | 5b68edc | 2012-01-20 17:44:12 +0100 | [diff] [blame] | 406 | * |
| 407 | * amd-ucode/microcode_amd_fam15h.bin |
| 408 | * amd-ucode/microcode_amd_fam16h.bin |
| 409 | * ... |
| 410 | * |
| 411 | * These might be larger than 2K. |
| 412 | */ |
Borislav Petkov | 48e3068 | 2012-07-26 15:51:00 +0200 | [diff] [blame] | 413 | static enum ucode_state request_microcode_amd(int cpu, struct device *device, |
| 414 | bool refresh_fw) |
Dmitry Adamushko | a0a29b6 | 2008-09-11 23:27:52 +0200 | [diff] [blame] | 415 | { |
Andreas Herrmann | 5b68edc | 2012-01-20 17:44:12 +0100 | [diff] [blame] | 416 | char fw_name[36] = "amd-ucode/microcode_amd.bin"; |
Andreas Herrmann | 5b68edc | 2012-01-20 17:44:12 +0100 | [diff] [blame] | 417 | struct cpuinfo_x86 *c = &cpu_data(cpu); |
Borislav Petkov | 2efb05e | 2012-08-01 16:16:13 +0200 | [diff] [blame^] | 418 | enum ucode_state ret = UCODE_NFOUND; |
| 419 | const struct firmware *fw; |
| 420 | |
| 421 | /* reload ucode container only on the boot cpu */ |
| 422 | if (!refresh_fw || c->cpu_index != boot_cpu_data.cpu_index) |
| 423 | return UCODE_OK; |
Dmitry Adamushko | a0a29b6 | 2008-09-11 23:27:52 +0200 | [diff] [blame] | 424 | |
Andreas Herrmann | 5b68edc | 2012-01-20 17:44:12 +0100 | [diff] [blame] | 425 | if (c->x86 >= 0x15) |
| 426 | snprintf(fw_name, sizeof(fw_name), "amd-ucode/microcode_amd_fam%.2xh.bin", c->x86); |
| 427 | |
| 428 | if (request_firmware(&fw, (const char *)fw_name, device)) { |
Borislav Petkov | 258721e | 2011-01-05 18:13:19 +0100 | [diff] [blame] | 429 | pr_err("failed to load file %s\n", fw_name); |
Borislav Petkov | ffc7e8a | 2010-12-30 21:06:01 +0100 | [diff] [blame] | 430 | goto out; |
Andreas Herrmann | 3b2e3d8 | 2010-01-22 21:34:56 +0100 | [diff] [blame] | 431 | } |
Dmitry Adamushko | a0a29b6 | 2008-09-11 23:27:52 +0200 | [diff] [blame] | 432 | |
Borislav Petkov | ffc7e8a | 2010-12-30 21:06:01 +0100 | [diff] [blame] | 433 | ret = UCODE_ERROR; |
| 434 | if (*(u32 *)fw->data != UCODE_MAGIC) { |
Borislav Petkov | 258721e | 2011-01-05 18:13:19 +0100 | [diff] [blame] | 435 | pr_err("invalid magic value (0x%08x)\n", *(u32 *)fw->data); |
Borislav Petkov | ffc7e8a | 2010-12-30 21:06:01 +0100 | [diff] [blame] | 436 | goto fw_release; |
Borislav Petkov | 506f90e | 2009-10-29 14:45:52 +0100 | [diff] [blame] | 437 | } |
| 438 | |
Borislav Petkov | 2efb05e | 2012-08-01 16:16:13 +0200 | [diff] [blame^] | 439 | /* free old equiv table */ |
| 440 | free_equiv_cpu_table(); |
Dmitry Adamushko | a0a29b6 | 2008-09-11 23:27:52 +0200 | [diff] [blame] | 441 | |
Borislav Petkov | 2efb05e | 2012-08-01 16:16:13 +0200 | [diff] [blame^] | 442 | ret = load_microcode_amd(cpu, fw->data, fw->size); |
| 443 | if (ret != UCODE_OK) |
| 444 | cleanup(); |
| 445 | |
| 446 | fw_release: |
Borislav Petkov | ffc7e8a | 2010-12-30 21:06:01 +0100 | [diff] [blame] | 447 | release_firmware(fw); |
Andreas Herrmann | 3b2e3d8 | 2010-01-22 21:34:56 +0100 | [diff] [blame] | 448 | |
Borislav Petkov | 2efb05e | 2012-08-01 16:16:13 +0200 | [diff] [blame^] | 449 | out: |
Dmitry Adamushko | a0a29b6 | 2008-09-11 23:27:52 +0200 | [diff] [blame] | 450 | return ret; |
| 451 | } |
| 452 | |
Dmitry Adamushko | 871b72d | 2009-05-11 23:48:27 +0200 | [diff] [blame] | 453 | static enum ucode_state |
| 454 | request_microcode_user(int cpu, const void __user *buf, size_t size) |
Dmitry Adamushko | a0a29b6 | 2008-09-11 23:27:52 +0200 | [diff] [blame] | 455 | { |
Dmitry Adamushko | 871b72d | 2009-05-11 23:48:27 +0200 | [diff] [blame] | 456 | return UCODE_ERROR; |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 457 | } |
| 458 | |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 459 | static void microcode_fini_cpu_amd(int cpu) |
| 460 | { |
| 461 | struct ucode_cpu_info *uci = ucode_cpu_info + cpu; |
| 462 | |
Dmitry Adamushko | 18dbc91 | 2008-09-23 12:08:44 +0200 | [diff] [blame] | 463 | uci->mc = NULL; |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | static struct microcode_ops microcode_amd_ops = { |
Dmitry Adamushko | a0a29b6 | 2008-09-11 23:27:52 +0200 | [diff] [blame] | 467 | .request_microcode_user = request_microcode_user, |
Borislav Petkov | ffc7e8a | 2010-12-30 21:06:01 +0100 | [diff] [blame] | 468 | .request_microcode_fw = request_microcode_amd, |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 469 | .collect_cpu_info = collect_cpu_info_amd, |
| 470 | .apply_microcode = apply_microcode_amd, |
| 471 | .microcode_fini_cpu = microcode_fini_cpu_amd, |
| 472 | }; |
| 473 | |
Dmitry Adamushko | 18dbc91 | 2008-09-23 12:08:44 +0200 | [diff] [blame] | 474 | struct microcode_ops * __init init_amd_microcode(void) |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 475 | { |
Andreas Herrmann | 283c1f2 | 2012-04-12 16:51:57 +0200 | [diff] [blame] | 476 | struct cpuinfo_x86 *c = &cpu_data(0); |
| 477 | |
| 478 | if (c->x86_vendor != X86_VENDOR_AMD || c->x86 < 0x10) { |
| 479 | pr_warning("AMD CPU family 0x%x not supported\n", c->x86); |
| 480 | return NULL; |
| 481 | } |
| 482 | |
Dmitry Adamushko | 18dbc91 | 2008-09-23 12:08:44 +0200 | [diff] [blame] | 483 | return µcode_amd_ops; |
Peter Oruba | 80cc9f1 | 2008-07-28 18:44:22 +0200 | [diff] [blame] | 484 | } |
Borislav Petkov | f72c1a5 | 2011-12-02 16:50:04 +0100 | [diff] [blame] | 485 | |
| 486 | void __exit exit_amd_microcode(void) |
| 487 | { |
Borislav Petkov | 2efb05e | 2012-08-01 16:16:13 +0200 | [diff] [blame^] | 488 | cleanup(); |
Borislav Petkov | f72c1a5 | 2011-12-02 16:50:04 +0100 | [diff] [blame] | 489 | } |