1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * AMD CPU Microcode Update Driver for Linux
4 *
5 * This driver allows to upgrade microcode on F10h AMD
6 * CPUs and later.
7 *
8 * Copyright (C) 2008-2011 Advanced Micro Devices Inc.
9 * 2013-2018 Borislav Petkov <bp@alien8.de>
10 *
11 * Author: Peter Oruba <peter.oruba@amd.com>
12 *
13 * Based on work by:
14 * Tigran Aivazian <aivazian.tigran@gmail.com>
15 *
16 * early loader:
17 * Copyright (C) 2013 Advanced Micro Devices, Inc.
18 *
19 * Author: Jacob Shin <jacob.shin@amd.com>
20 * Fixes: Borislav Petkov <bp@suse.de>
21 */
22 #define pr_fmt(fmt) "microcode: " fmt
23
24 #include <linux/earlycpio.h>
25 #include <linux/firmware.h>
26 #include <linux/uaccess.h>
27 #include <linux/vmalloc.h>
28 #include <linux/initrd.h>
29 #include <linux/kernel.h>
30 #include <linux/pci.h>
31
32 #include <asm/microcode.h>
33 #include <asm/processor.h>
34 #include <asm/setup.h>
35 #include <asm/cpu.h>
36 #include <asm/msr.h>
37
38 #include "internal.h"
39
40 #define UCODE_MAGIC 0x00414d44
41 #define UCODE_EQUIV_CPU_TABLE_TYPE 0x00000000
42 #define UCODE_UCODE_TYPE 0x00000001
43
44 #define SECTION_HDR_SIZE 8
45 #define CONTAINER_HDR_SZ 12
46
47 struct equiv_cpu_entry {
48 u32 installed_cpu;
49 u32 fixed_errata_mask;
50 u32 fixed_errata_compare;
51 u16 equiv_cpu;
52 u16 res;
53 } __packed;
54
55 struct microcode_header_amd {
56 u32 data_code;
57 u32 patch_id;
58 u16 mc_patch_data_id;
59 u8 mc_patch_data_len;
60 u8 init_flag;
61 u32 mc_patch_data_checksum;
62 u32 nb_dev_id;
63 u32 sb_dev_id;
64 u16 processor_rev_id;
65 u8 nb_rev_id;
66 u8 sb_rev_id;
67 u8 bios_api_rev;
68 u8 reserved1[3];
69 u32 match_reg[8];
70 } __packed;
71
72 struct microcode_amd {
73 struct microcode_header_amd hdr;
74 unsigned int mpb[];
75 };
76
77 #define PATCH_MAX_SIZE (3 * PAGE_SIZE)
78
79 static struct equiv_cpu_table {
80 unsigned int num_entries;
81 struct equiv_cpu_entry *entry;
82 } equiv_table;
83
84 /*
85 * This points to the current valid container of microcode patches which we will
86 * save from the initrd/builtin before jettisoning its contents. @mc is the
87 * microcode patch we found to match.
88 */
89 struct cont_desc {
90 struct microcode_amd *mc;
91 u32 cpuid_1_eax;
92 u32 psize;
93 u8 *data;
94 size_t size;
95 };
96
97 static u32 ucode_new_rev;
98
99 /*
100 * Microcode patch container file is prepended to the initrd in cpio
101 * format. See Documentation/arch/x86/microcode.rst
102 */
103 static const char
104 ucode_path[] __maybe_unused = "kernel/x86/microcode/AuthenticAMD.bin";
105
find_equiv_id(struct equiv_cpu_table * et,u32 sig)106 static u16 find_equiv_id(struct equiv_cpu_table *et, u32 sig)
107 {
108 unsigned int i;
109
110 if (!et || !et->num_entries)
111 return 0;
112
113 for (i = 0; i < et->num_entries; i++) {
114 struct equiv_cpu_entry *e = &et->entry[i];
115
116 if (sig == e->installed_cpu)
117 return e->equiv_cpu;
118 }
119 return 0;
120 }
121
122 /*
123 * Check whether there is a valid microcode container file at the beginning
124 * of @buf of size @buf_size. Set @early to use this function in the early path.
125 */
verify_container(const u8 * buf,size_t buf_size,bool early)126 static bool verify_container(const u8 *buf, size_t buf_size, bool early)
127 {
128 u32 cont_magic;
129
130 if (buf_size <= CONTAINER_HDR_SZ) {
131 if (!early)
132 pr_debug("Truncated microcode container header.\n");
133
134 return false;
135 }
136
137 cont_magic = *(const u32 *)buf;
138 if (cont_magic != UCODE_MAGIC) {
139 if (!early)
140 pr_debug("Invalid magic value (0x%08x).\n", cont_magic);
141
142 return false;
143 }
144
145 return true;
146 }
147
148 /*
149 * Check whether there is a valid, non-truncated CPU equivalence table at the
150 * beginning of @buf of size @buf_size. Set @early to use this function in the
151 * early path.
152 */
verify_equivalence_table(const u8 * buf,size_t buf_size,bool early)153 static bool verify_equivalence_table(const u8 *buf, size_t buf_size, bool early)
154 {
155 const u32 *hdr = (const u32 *)buf;
156 u32 cont_type, equiv_tbl_len;
157
158 if (!verify_container(buf, buf_size, early))
159 return false;
160
161 cont_type = hdr[1];
162 if (cont_type != UCODE_EQUIV_CPU_TABLE_TYPE) {
163 if (!early)
164 pr_debug("Wrong microcode container equivalence table type: %u.\n",
165 cont_type);
166
167 return false;
168 }
169
170 buf_size -= CONTAINER_HDR_SZ;
171
172 equiv_tbl_len = hdr[2];
173 if (equiv_tbl_len < sizeof(struct equiv_cpu_entry) ||
174 buf_size < equiv_tbl_len) {
175 if (!early)
176 pr_debug("Truncated equivalence table.\n");
177
178 return false;
179 }
180
181 return true;
182 }
183
184 /*
185 * Check whether there is a valid, non-truncated microcode patch section at the
186 * beginning of @buf of size @buf_size. Set @early to use this function in the
187 * early path.
188 *
189 * On success, @sh_psize returns the patch size according to the section header,
190 * to the caller.
191 */
192 static bool
__verify_patch_section(const u8 * buf,size_t buf_size,u32 * sh_psize,bool early)193 __verify_patch_section(const u8 *buf, size_t buf_size, u32 *sh_psize, bool early)
194 {
195 u32 p_type, p_size;
196 const u32 *hdr;
197
198 if (buf_size < SECTION_HDR_SIZE) {
199 if (!early)
200 pr_debug("Truncated patch section.\n");
201
202 return false;
203 }
204
205 hdr = (const u32 *)buf;
206 p_type = hdr[0];
207 p_size = hdr[1];
208
209 if (p_type != UCODE_UCODE_TYPE) {
210 if (!early)
211 pr_debug("Invalid type field (0x%x) in container file section header.\n",
212 p_type);
213
214 return false;
215 }
216
217 if (p_size < sizeof(struct microcode_header_amd)) {
218 if (!early)
219 pr_debug("Patch of size %u too short.\n", p_size);
220
221 return false;
222 }
223
224 *sh_psize = p_size;
225
226 return true;
227 }
228
229 /*
230 * Check whether the passed remaining file @buf_size is large enough to contain
231 * a patch of the indicated @sh_psize (and also whether this size does not
232 * exceed the per-family maximum). @sh_psize is the size read from the section
233 * header.
234 */
__verify_patch_size(u8 family,u32 sh_psize,size_t buf_size)235 static unsigned int __verify_patch_size(u8 family, u32 sh_psize, size_t buf_size)
236 {
237 u32 max_size;
238
239 if (family >= 0x15)
240 return min_t(u32, sh_psize, buf_size);
241
242 #define F1XH_MPB_MAX_SIZE 2048
243 #define F14H_MPB_MAX_SIZE 1824
244
245 switch (family) {
246 case 0x10 ... 0x12:
247 max_size = F1XH_MPB_MAX_SIZE;
248 break;
249 case 0x14:
250 max_size = F14H_MPB_MAX_SIZE;
251 break;
252 default:
253 WARN(1, "%s: WTF family: 0x%x\n", __func__, family);
254 return 0;
255 }
256
257 if (sh_psize > min_t(u32, buf_size, max_size))
258 return 0;
259
260 return sh_psize;
261 }
262
263 /*
264 * Verify the patch in @buf.
265 *
266 * Returns:
267 * negative: on error
268 * positive: patch is not for this family, skip it
269 * 0: success
270 */
271 static int
verify_patch(u8 family,const u8 * buf,size_t buf_size,u32 * patch_size,bool early)272 verify_patch(u8 family, const u8 *buf, size_t buf_size, u32 *patch_size, bool early)
273 {
274 struct microcode_header_amd *mc_hdr;
275 unsigned int ret;
276 u32 sh_psize;
277 u16 proc_id;
278 u8 patch_fam;
279
280 if (!__verify_patch_section(buf, buf_size, &sh_psize, early))
281 return -1;
282
283 /*
284 * The section header length is not included in this indicated size
285 * but is present in the leftover file length so we need to subtract
286 * it before passing this value to the function below.
287 */
288 buf_size -= SECTION_HDR_SIZE;
289
290 /*
291 * Check if the remaining buffer is big enough to contain a patch of
292 * size sh_psize, as the section claims.
293 */
294 if (buf_size < sh_psize) {
295 if (!early)
296 pr_debug("Patch of size %u truncated.\n", sh_psize);
297
298 return -1;
299 }
300
301 ret = __verify_patch_size(family, sh_psize, buf_size);
302 if (!ret) {
303 if (!early)
304 pr_debug("Per-family patch size mismatch.\n");
305 return -1;
306 }
307
308 *patch_size = sh_psize;
309
310 mc_hdr = (struct microcode_header_amd *)(buf + SECTION_HDR_SIZE);
311 if (mc_hdr->nb_dev_id || mc_hdr->sb_dev_id) {
312 if (!early)
313 pr_err("Patch-ID 0x%08x: chipset-specific code unsupported.\n", mc_hdr->patch_id);
314 return -1;
315 }
316
317 proc_id = mc_hdr->processor_rev_id;
318 patch_fam = 0xf + (proc_id >> 12);
319 if (patch_fam != family)
320 return 1;
321
322 return 0;
323 }
324
325 /*
326 * This scans the ucode blob for the proper container as we can have multiple
327 * containers glued together. Returns the equivalence ID from the equivalence
328 * table or 0 if none found.
329 * Returns the amount of bytes consumed while scanning. @desc contains all the
330 * data we're going to use in later stages of the application.
331 */
parse_container(u8 * ucode,size_t size,struct cont_desc * desc)332 static size_t parse_container(u8 *ucode, size_t size, struct cont_desc *desc)
333 {
334 struct equiv_cpu_table table;
335 size_t orig_size = size;
336 u32 *hdr = (u32 *)ucode;
337 u16 eq_id;
338 u8 *buf;
339
340 if (!verify_equivalence_table(ucode, size, true))
341 return 0;
342
343 buf = ucode;
344
345 table.entry = (struct equiv_cpu_entry *)(buf + CONTAINER_HDR_SZ);
346 table.num_entries = hdr[2] / sizeof(struct equiv_cpu_entry);
347
348 /*
349 * Find the equivalence ID of our CPU in this table. Even if this table
350 * doesn't contain a patch for the CPU, scan through the whole container
351 * so that it can be skipped in case there are other containers appended.
352 */
353 eq_id = find_equiv_id(&table, desc->cpuid_1_eax);
354
355 buf += hdr[2] + CONTAINER_HDR_SZ;
356 size -= hdr[2] + CONTAINER_HDR_SZ;
357
358 /*
359 * Scan through the rest of the container to find where it ends. We do
360 * some basic sanity-checking too.
361 */
362 while (size > 0) {
363 struct microcode_amd *mc;
364 u32 patch_size;
365 int ret;
366
367 ret = verify_patch(x86_family(desc->cpuid_1_eax), buf, size, &patch_size, true);
368 if (ret < 0) {
369 /*
370 * Patch verification failed, skip to the next container, if
371 * there is one. Before exit, check whether that container has
372 * found a patch already. If so, use it.
373 */
374 goto out;
375 } else if (ret > 0) {
376 goto skip;
377 }
378
379 mc = (struct microcode_amd *)(buf + SECTION_HDR_SIZE);
380 if (eq_id == mc->hdr.processor_rev_id) {
381 desc->psize = patch_size;
382 desc->mc = mc;
383 }
384
385 skip:
386 /* Skip patch section header too: */
387 buf += patch_size + SECTION_HDR_SIZE;
388 size -= patch_size + SECTION_HDR_SIZE;
389 }
390
391 out:
392 /*
393 * If we have found a patch (desc->mc), it means we're looking at the
394 * container which has a patch for this CPU so return 0 to mean, @ucode
395 * already points to the proper container. Otherwise, we return the size
396 * we scanned so that we can advance to the next container in the
397 * buffer.
398 */
399 if (desc->mc) {
400 desc->data = ucode;
401 desc->size = orig_size - size;
402
403 return 0;
404 }
405
406 return orig_size - size;
407 }
408
409 /*
410 * Scan the ucode blob for the proper container as we can have multiple
411 * containers glued together.
412 */
scan_containers(u8 * ucode,size_t size,struct cont_desc * desc)413 static void scan_containers(u8 *ucode, size_t size, struct cont_desc *desc)
414 {
415 while (size) {
416 size_t s = parse_container(ucode, size, desc);
417 if (!s)
418 return;
419
420 /* catch wraparound */
421 if (size >= s) {
422 ucode += s;
423 size -= s;
424 } else {
425 return;
426 }
427 }
428 }
429
__apply_microcode_amd(struct microcode_amd * mc)430 static int __apply_microcode_amd(struct microcode_amd *mc)
431 {
432 u32 rev, dummy;
433
434 native_wrmsrl(MSR_AMD64_PATCH_LOADER, (u64)(long)&mc->hdr.data_code);
435
436 /* verify patch application was successful */
437 native_rdmsr(MSR_AMD64_PATCH_LEVEL, rev, dummy);
438 if (rev != mc->hdr.patch_id)
439 return -1;
440
441 return 0;
442 }
443
444 /*
445 * Early load occurs before we can vmalloc(). So we look for the microcode
446 * patch container file in initrd, traverse equivalent cpu table, look for a
447 * matching microcode patch, and update, all in initrd memory in place.
448 * When vmalloc() is available for use later -- on 64-bit during first AP load,
449 * and on 32-bit during save_microcode_in_initrd_amd() -- we can call
450 * load_microcode_amd() to save equivalent cpu table and microcode patches in
451 * kernel heap memory.
452 *
453 * Returns true if container found (sets @desc), false otherwise.
454 */
early_apply_microcode(u32 cpuid_1_eax,void * ucode,size_t size)455 static bool early_apply_microcode(u32 cpuid_1_eax, void *ucode, size_t size)
456 {
457 struct cont_desc desc = { 0 };
458 struct microcode_amd *mc;
459 u32 rev, dummy, *new_rev;
460 bool ret = false;
461
462 #ifdef CONFIG_X86_32
463 new_rev = (u32 *)__pa_nodebug(&ucode_new_rev);
464 #else
465 new_rev = &ucode_new_rev;
466 #endif
467
468 desc.cpuid_1_eax = cpuid_1_eax;
469
470 scan_containers(ucode, size, &desc);
471
472 mc = desc.mc;
473 if (!mc)
474 return ret;
475
476 native_rdmsr(MSR_AMD64_PATCH_LEVEL, rev, dummy);
477
478 /*
479 * Allow application of the same revision to pick up SMT-specific
480 * changes even if the revision of the other SMT thread is already
481 * up-to-date.
482 */
483 if (rev > mc->hdr.patch_id)
484 return ret;
485
486 if (!__apply_microcode_amd(mc)) {
487 *new_rev = mc->hdr.patch_id;
488 ret = true;
489 }
490
491 return ret;
492 }
493
get_builtin_microcode(struct cpio_data * cp,unsigned int family)494 static bool get_builtin_microcode(struct cpio_data *cp, unsigned int family)
495 {
496 char fw_name[36] = "amd-ucode/microcode_amd.bin";
497 struct firmware fw;
498
499 if (IS_ENABLED(CONFIG_X86_32))
500 return false;
501
502 if (family >= 0x15)
503 snprintf(fw_name, sizeof(fw_name),
504 "amd-ucode/microcode_amd_fam%.2xh.bin", family);
505
506 if (firmware_request_builtin(&fw, fw_name)) {
507 cp->size = fw.size;
508 cp->data = (void *)fw.data;
509 return true;
510 }
511
512 return false;
513 }
514
find_blobs_in_containers(unsigned int cpuid_1_eax,struct cpio_data * ret)515 static void find_blobs_in_containers(unsigned int cpuid_1_eax, struct cpio_data *ret)
516 {
517 struct ucode_cpu_info *uci;
518 struct cpio_data cp;
519 const char *path;
520 bool use_pa;
521
522 if (IS_ENABLED(CONFIG_X86_32)) {
523 uci = (struct ucode_cpu_info *)__pa_nodebug(ucode_cpu_info);
524 path = (const char *)__pa_nodebug(ucode_path);
525 use_pa = true;
526 } else {
527 uci = ucode_cpu_info;
528 path = ucode_path;
529 use_pa = false;
530 }
531
532 if (!get_builtin_microcode(&cp, x86_family(cpuid_1_eax)))
533 cp = find_microcode_in_initrd(path, use_pa);
534
535 /* Needed in load_microcode_amd() */
536 uci->cpu_sig.sig = cpuid_1_eax;
537
538 *ret = cp;
539 }
540
apply_ucode_from_containers(unsigned int cpuid_1_eax)541 static void apply_ucode_from_containers(unsigned int cpuid_1_eax)
542 {
543 struct cpio_data cp = { };
544
545 find_blobs_in_containers(cpuid_1_eax, &cp);
546 if (!(cp.data && cp.size))
547 return;
548
549 early_apply_microcode(cpuid_1_eax, cp.data, cp.size);
550 }
551
load_ucode_amd_early(unsigned int cpuid_1_eax)552 void load_ucode_amd_early(unsigned int cpuid_1_eax)
553 {
554 return apply_ucode_from_containers(cpuid_1_eax);
555 }
556
557 static enum ucode_state load_microcode_amd(u8 family, const u8 *data, size_t size);
558
save_microcode_in_initrd_amd(unsigned int cpuid_1_eax)559 int __init save_microcode_in_initrd_amd(unsigned int cpuid_1_eax)
560 {
561 struct cont_desc desc = { 0 };
562 enum ucode_state ret;
563 struct cpio_data cp;
564
565 cp = find_microcode_in_initrd(ucode_path, false);
566 if (!(cp.data && cp.size))
567 return -EINVAL;
568
569 desc.cpuid_1_eax = cpuid_1_eax;
570
571 scan_containers(cp.data, cp.size, &desc);
572 if (!desc.mc)
573 return -EINVAL;
574
575 ret = load_microcode_amd(x86_family(cpuid_1_eax), desc.data, desc.size);
576 if (ret > UCODE_UPDATED)
577 return -EINVAL;
578
579 return 0;
580 }
581
582 /*
583 * a small, trivial cache of per-family ucode patches
584 */
cache_find_patch(u16 equiv_cpu)585 static struct ucode_patch *cache_find_patch(u16 equiv_cpu)
586 {
587 struct ucode_patch *p;
588
589 list_for_each_entry(p, µcode_cache, plist)
590 if (p->equiv_cpu == equiv_cpu)
591 return p;
592 return NULL;
593 }
594
update_cache(struct ucode_patch * new_patch)595 static void update_cache(struct ucode_patch *new_patch)
596 {
597 struct ucode_patch *p;
598
599 list_for_each_entry(p, µcode_cache, plist) {
600 if (p->equiv_cpu == new_patch->equiv_cpu) {
601 if (p->patch_id >= new_patch->patch_id) {
602 /* we already have the latest patch */
603 kfree(new_patch->data);
604 kfree(new_patch);
605 return;
606 }
607
608 list_replace(&p->plist, &new_patch->plist);
609 kfree(p->data);
610 kfree(p);
611 return;
612 }
613 }
614 /* no patch found, add it */
615 list_add_tail(&new_patch->plist, µcode_cache);
616 }
617
free_cache(void)618 static void free_cache(void)
619 {
620 struct ucode_patch *p, *tmp;
621
622 list_for_each_entry_safe(p, tmp, µcode_cache, plist) {
623 __list_del(p->plist.prev, p->plist.next);
624 kfree(p->data);
625 kfree(p);
626 }
627 }
628
find_patch(unsigned int cpu)629 static struct ucode_patch *find_patch(unsigned int cpu)
630 {
631 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
632 u16 equiv_id;
633
634
635 equiv_id = find_equiv_id(&equiv_table, uci->cpu_sig.sig);
636 if (!equiv_id)
637 return NULL;
638
639 return cache_find_patch(equiv_id);
640 }
641
reload_ucode_amd(unsigned int cpu)642 void reload_ucode_amd(unsigned int cpu)
643 {
644 u32 rev, dummy __always_unused;
645 struct microcode_amd *mc;
646 struct ucode_patch *p;
647
648 p = find_patch(cpu);
649 if (!p)
650 return;
651
652 mc = p->data;
653
654 rdmsr(MSR_AMD64_PATCH_LEVEL, rev, dummy);
655
656 if (rev < mc->hdr.patch_id) {
657 if (!__apply_microcode_amd(mc)) {
658 ucode_new_rev = mc->hdr.patch_id;
659 pr_info("reload patch_level=0x%08x\n", ucode_new_rev);
660 }
661 }
662 }
663
collect_cpu_info_amd(int cpu,struct cpu_signature * csig)664 static int collect_cpu_info_amd(int cpu, struct cpu_signature *csig)
665 {
666 struct cpuinfo_x86 *c = &cpu_data(cpu);
667 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
668 struct ucode_patch *p;
669
670 csig->sig = cpuid_eax(0x00000001);
671 csig->rev = c->microcode;
672
673 /*
674 * a patch could have been loaded early, set uci->mc so that
675 * mc_bp_resume() can call apply_microcode()
676 */
677 p = find_patch(cpu);
678 if (p && (p->patch_id == csig->rev))
679 uci->mc = p->data;
680
681 pr_info("CPU%d: patch_level=0x%08x\n", cpu, csig->rev);
682
683 return 0;
684 }
685
apply_microcode_amd(int cpu)686 static enum ucode_state apply_microcode_amd(int cpu)
687 {
688 struct cpuinfo_x86 *c = &cpu_data(cpu);
689 struct microcode_amd *mc_amd;
690 struct ucode_cpu_info *uci;
691 struct ucode_patch *p;
692 enum ucode_state ret;
693 u32 rev, dummy __always_unused;
694
695 BUG_ON(raw_smp_processor_id() != cpu);
696
697 uci = ucode_cpu_info + cpu;
698
699 p = find_patch(cpu);
700 if (!p)
701 return UCODE_NFOUND;
702
703 mc_amd = p->data;
704 uci->mc = p->data;
705
706 rdmsr(MSR_AMD64_PATCH_LEVEL, rev, dummy);
707
708 /* need to apply patch? */
709 if (rev > mc_amd->hdr.patch_id) {
710 ret = UCODE_OK;
711 goto out;
712 }
713
714 if (__apply_microcode_amd(mc_amd)) {
715 pr_err("CPU%d: update failed for patch_level=0x%08x\n",
716 cpu, mc_amd->hdr.patch_id);
717 return UCODE_ERROR;
718 }
719
720 rev = mc_amd->hdr.patch_id;
721 ret = UCODE_UPDATED;
722
723 pr_info("CPU%d: new patch_level=0x%08x\n", cpu, rev);
724
725 out:
726 uci->cpu_sig.rev = rev;
727 c->microcode = rev;
728
729 /* Update boot_cpu_data's revision too, if we're on the BSP: */
730 if (c->cpu_index == boot_cpu_data.cpu_index)
731 boot_cpu_data.microcode = rev;
732
733 return ret;
734 }
735
install_equiv_cpu_table(const u8 * buf,size_t buf_size)736 static size_t install_equiv_cpu_table(const u8 *buf, size_t buf_size)
737 {
738 u32 equiv_tbl_len;
739 const u32 *hdr;
740
741 if (!verify_equivalence_table(buf, buf_size, false))
742 return 0;
743
744 hdr = (const u32 *)buf;
745 equiv_tbl_len = hdr[2];
746
747 equiv_table.entry = vmalloc(equiv_tbl_len);
748 if (!equiv_table.entry) {
749 pr_err("failed to allocate equivalent CPU table\n");
750 return 0;
751 }
752
753 memcpy(equiv_table.entry, buf + CONTAINER_HDR_SZ, equiv_tbl_len);
754 equiv_table.num_entries = equiv_tbl_len / sizeof(struct equiv_cpu_entry);
755
756 /* add header length */
757 return equiv_tbl_len + CONTAINER_HDR_SZ;
758 }
759
free_equiv_cpu_table(void)760 static void free_equiv_cpu_table(void)
761 {
762 vfree(equiv_table.entry);
763 memset(&equiv_table, 0, sizeof(equiv_table));
764 }
765
cleanup(void)766 static void cleanup(void)
767 {
768 free_equiv_cpu_table();
769 free_cache();
770 }
771
772 /*
773 * Return a non-negative value even if some of the checks failed so that
774 * we can skip over the next patch. If we return a negative value, we
775 * signal a grave error like a memory allocation has failed and the
776 * driver cannot continue functioning normally. In such cases, we tear
777 * down everything we've used up so far and exit.
778 */
verify_and_add_patch(u8 family,u8 * fw,unsigned int leftover,unsigned int * patch_size)779 static int verify_and_add_patch(u8 family, u8 *fw, unsigned int leftover,
780 unsigned int *patch_size)
781 {
782 struct microcode_header_amd *mc_hdr;
783 struct ucode_patch *patch;
784 u16 proc_id;
785 int ret;
786
787 ret = verify_patch(family, fw, leftover, patch_size, false);
788 if (ret)
789 return ret;
790
791 patch = kzalloc(sizeof(*patch), GFP_KERNEL);
792 if (!patch) {
793 pr_err("Patch allocation failure.\n");
794 return -EINVAL;
795 }
796
797 patch->data = kmemdup(fw + SECTION_HDR_SIZE, *patch_size, GFP_KERNEL);
798 if (!patch->data) {
799 pr_err("Patch data allocation failure.\n");
800 kfree(patch);
801 return -EINVAL;
802 }
803 patch->size = *patch_size;
804
805 mc_hdr = (struct microcode_header_amd *)(fw + SECTION_HDR_SIZE);
806 proc_id = mc_hdr->processor_rev_id;
807
808 INIT_LIST_HEAD(&patch->plist);
809 patch->patch_id = mc_hdr->patch_id;
810 patch->equiv_cpu = proc_id;
811
812 pr_debug("%s: Added patch_id: 0x%08x, proc_id: 0x%04x\n",
813 __func__, patch->patch_id, proc_id);
814
815 /* ... and add to cache. */
816 update_cache(patch);
817
818 return 0;
819 }
820
821 /* Scan the blob in @data and add microcode patches to the cache. */
__load_microcode_amd(u8 family,const u8 * data,size_t size)822 static enum ucode_state __load_microcode_amd(u8 family, const u8 *data,
823 size_t size)
824 {
825 u8 *fw = (u8 *)data;
826 size_t offset;
827
828 offset = install_equiv_cpu_table(data, size);
829 if (!offset)
830 return UCODE_ERROR;
831
832 fw += offset;
833 size -= offset;
834
835 if (*(u32 *)fw != UCODE_UCODE_TYPE) {
836 pr_err("invalid type field in container file section header\n");
837 free_equiv_cpu_table();
838 return UCODE_ERROR;
839 }
840
841 while (size > 0) {
842 unsigned int crnt_size = 0;
843 int ret;
844
845 ret = verify_and_add_patch(family, fw, size, &crnt_size);
846 if (ret < 0)
847 return UCODE_ERROR;
848
849 fw += crnt_size + SECTION_HDR_SIZE;
850 size -= (crnt_size + SECTION_HDR_SIZE);
851 }
852
853 return UCODE_OK;
854 }
855
load_microcode_amd(u8 family,const u8 * data,size_t size)856 static enum ucode_state load_microcode_amd(u8 family, const u8 *data, size_t size)
857 {
858 struct cpuinfo_x86 *c;
859 unsigned int nid, cpu;
860 struct ucode_patch *p;
861 enum ucode_state ret;
862
863 /* free old equiv table */
864 free_equiv_cpu_table();
865
866 ret = __load_microcode_amd(family, data, size);
867 if (ret != UCODE_OK) {
868 cleanup();
869 return ret;
870 }
871
872 for_each_node(nid) {
873 cpu = cpumask_first(cpumask_of_node(nid));
874 c = &cpu_data(cpu);
875
876 p = find_patch(cpu);
877 if (!p)
878 continue;
879
880 if (c->microcode >= p->patch_id)
881 continue;
882
883 ret = UCODE_NEW;
884 }
885
886 return ret;
887 }
888
889 /*
890 * AMD microcode firmware naming convention, up to family 15h they are in
891 * the legacy file:
892 *
893 * amd-ucode/microcode_amd.bin
894 *
895 * This legacy file is always smaller than 2K in size.
896 *
897 * Beginning with family 15h, they are in family-specific firmware files:
898 *
899 * amd-ucode/microcode_amd_fam15h.bin
900 * amd-ucode/microcode_amd_fam16h.bin
901 * ...
902 *
903 * These might be larger than 2K.
904 */
request_microcode_amd(int cpu,struct device * device)905 static enum ucode_state request_microcode_amd(int cpu, struct device *device)
906 {
907 char fw_name[36] = "amd-ucode/microcode_amd.bin";
908 struct cpuinfo_x86 *c = &cpu_data(cpu);
909 enum ucode_state ret = UCODE_NFOUND;
910 const struct firmware *fw;
911
912 if (c->x86 >= 0x15)
913 snprintf(fw_name, sizeof(fw_name), "amd-ucode/microcode_amd_fam%.2xh.bin", c->x86);
914
915 if (request_firmware_direct(&fw, (const char *)fw_name, device)) {
916 pr_debug("failed to load file %s\n", fw_name);
917 goto out;
918 }
919
920 ret = UCODE_ERROR;
921 if (!verify_container(fw->data, fw->size, false))
922 goto fw_release;
923
924 ret = load_microcode_amd(c->x86, fw->data, fw->size);
925
926 fw_release:
927 release_firmware(fw);
928
929 out:
930 return ret;
931 }
932
microcode_fini_cpu_amd(int cpu)933 static void microcode_fini_cpu_amd(int cpu)
934 {
935 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
936
937 uci->mc = NULL;
938 }
939
940 static struct microcode_ops microcode_amd_ops = {
941 .request_microcode_fw = request_microcode_amd,
942 .collect_cpu_info = collect_cpu_info_amd,
943 .apply_microcode = apply_microcode_amd,
944 .microcode_fini_cpu = microcode_fini_cpu_amd,
945 };
946
init_amd_microcode(void)947 struct microcode_ops * __init init_amd_microcode(void)
948 {
949 struct cpuinfo_x86 *c = &boot_cpu_data;
950
951 if (c->x86_vendor != X86_VENDOR_AMD || c->x86 < 0x10) {
952 pr_warn("AMD CPU family 0x%x not supported\n", c->x86);
953 return NULL;
954 }
955
956 if (ucode_new_rev)
957 pr_info_once("microcode updated early to new patch_level=0x%08x\n",
958 ucode_new_rev);
959
960 return µcode_amd_ops;
961 }
962
exit_amd_microcode(void)963 void __exit exit_amd_microcode(void)
964 {
965 cleanup();
966 }
967