1 // SPDX-License-Identifier: GPL-2.0-only
3 * AMD CPU Microcode Update Driver for Linux
5 * This driver allows to upgrade microcode on F10h AMD
8 * Copyright (C) 2008-2011 Advanced Micro Devices Inc.
17 * Copyright (C) 2013 Advanced Micro Devices, Inc.
22 #define pr_fmt(fmt) "microcode: " fmt
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>
32 #include <asm/microcode.h>
33 #include <asm/processor.h>
34 #include <asm/setup.h>
41 struct list_head plist;
48 static LIST_HEAD(microcode_cache);
50 #define UCODE_MAGIC 0x00414d44
51 #define UCODE_EQUIV_CPU_TABLE_TYPE 0x00000000
52 #define UCODE_UCODE_TYPE 0x00000001
54 #define SECTION_HDR_SIZE 8
55 #define CONTAINER_HDR_SZ 12
57 struct equiv_cpu_entry {
59 u32 fixed_errata_mask;
60 u32 fixed_errata_compare;
65 struct microcode_header_amd {
71 u32 mc_patch_data_checksum;
82 struct microcode_amd {
83 struct microcode_header_amd hdr;
87 #define PATCH_MAX_SIZE (3 * PAGE_SIZE)
89 static struct equiv_cpu_table {
90 unsigned int num_entries;
91 struct equiv_cpu_entry *entry;
95 * This points to the current valid container of microcode patches which we will
96 * save from the initrd/builtin before jettisoning its contents. @mc is the
97 * microcode patch we found to match.
100 struct microcode_amd *mc;
108 * Microcode patch container file is prepended to the initrd in cpio
109 * format. See Documentation/arch/x86/microcode.rst
112 ucode_path[] __maybe_unused = "kernel/x86/microcode/AuthenticAMD.bin";
114 static u16 find_equiv_id(struct equiv_cpu_table *et, u32 sig)
118 if (!et || !et->num_entries)
121 for (i = 0; i < et->num_entries; i++) {
122 struct equiv_cpu_entry *e = &et->entry[i];
124 if (sig == e->installed_cpu)
131 * Check whether there is a valid microcode container file at the beginning
132 * of @buf of size @buf_size.
134 static bool verify_container(const u8 *buf, size_t buf_size)
138 if (buf_size <= CONTAINER_HDR_SZ) {
139 pr_debug("Truncated microcode container header.\n");
143 cont_magic = *(const u32 *)buf;
144 if (cont_magic != UCODE_MAGIC) {
145 pr_debug("Invalid magic value (0x%08x).\n", cont_magic);
153 * Check whether there is a valid, non-truncated CPU equivalence table at the
154 * beginning of @buf of size @buf_size.
156 static bool verify_equivalence_table(const u8 *buf, size_t buf_size)
158 const u32 *hdr = (const u32 *)buf;
159 u32 cont_type, equiv_tbl_len;
161 if (!verify_container(buf, buf_size))
165 if (cont_type != UCODE_EQUIV_CPU_TABLE_TYPE) {
166 pr_debug("Wrong microcode container equivalence table type: %u.\n",
171 buf_size -= CONTAINER_HDR_SZ;
173 equiv_tbl_len = hdr[2];
174 if (equiv_tbl_len < sizeof(struct equiv_cpu_entry) ||
175 buf_size < equiv_tbl_len) {
176 pr_debug("Truncated equivalence table.\n");
184 * Check whether there is a valid, non-truncated microcode patch section at the
185 * beginning of @buf of size @buf_size.
187 * On success, @sh_psize returns the patch size according to the section header,
191 __verify_patch_section(const u8 *buf, size_t buf_size, u32 *sh_psize)
196 if (buf_size < SECTION_HDR_SIZE) {
197 pr_debug("Truncated patch section.\n");
201 hdr = (const u32 *)buf;
205 if (p_type != UCODE_UCODE_TYPE) {
206 pr_debug("Invalid type field (0x%x) in container file section header.\n",
211 if (p_size < sizeof(struct microcode_header_amd)) {
212 pr_debug("Patch of size %u too short.\n", p_size);
222 * Check whether the passed remaining file @buf_size is large enough to contain
223 * a patch of the indicated @sh_psize (and also whether this size does not
224 * exceed the per-family maximum). @sh_psize is the size read from the section
227 static unsigned int __verify_patch_size(u8 family, u32 sh_psize, size_t buf_size)
232 return min_t(u32, sh_psize, buf_size);
234 #define F1XH_MPB_MAX_SIZE 2048
235 #define F14H_MPB_MAX_SIZE 1824
239 max_size = F1XH_MPB_MAX_SIZE;
242 max_size = F14H_MPB_MAX_SIZE;
245 WARN(1, "%s: WTF family: 0x%x\n", __func__, family);
249 if (sh_psize > min_t(u32, buf_size, max_size))
256 * Verify the patch in @buf.
260 * positive: patch is not for this family, skip it
264 verify_patch(u8 family, const u8 *buf, size_t buf_size, u32 *patch_size)
266 struct microcode_header_amd *mc_hdr;
272 if (!__verify_patch_section(buf, buf_size, &sh_psize))
276 * The section header length is not included in this indicated size
277 * but is present in the leftover file length so we need to subtract
278 * it before passing this value to the function below.
280 buf_size -= SECTION_HDR_SIZE;
283 * Check if the remaining buffer is big enough to contain a patch of
284 * size sh_psize, as the section claims.
286 if (buf_size < sh_psize) {
287 pr_debug("Patch of size %u truncated.\n", sh_psize);
291 ret = __verify_patch_size(family, sh_psize, buf_size);
293 pr_debug("Per-family patch size mismatch.\n");
297 *patch_size = sh_psize;
299 mc_hdr = (struct microcode_header_amd *)(buf + SECTION_HDR_SIZE);
300 if (mc_hdr->nb_dev_id || mc_hdr->sb_dev_id) {
301 pr_err("Patch-ID 0x%08x: chipset-specific code unsupported.\n", mc_hdr->patch_id);
305 proc_id = mc_hdr->processor_rev_id;
306 patch_fam = 0xf + (proc_id >> 12);
307 if (patch_fam != family)
314 * This scans the ucode blob for the proper container as we can have multiple
315 * containers glued together. Returns the equivalence ID from the equivalence
316 * table or 0 if none found.
317 * Returns the amount of bytes consumed while scanning. @desc contains all the
318 * data we're going to use in later stages of the application.
320 static size_t parse_container(u8 *ucode, size_t size, struct cont_desc *desc)
322 struct equiv_cpu_table table;
323 size_t orig_size = size;
324 u32 *hdr = (u32 *)ucode;
328 if (!verify_equivalence_table(ucode, size))
333 table.entry = (struct equiv_cpu_entry *)(buf + CONTAINER_HDR_SZ);
334 table.num_entries = hdr[2] / sizeof(struct equiv_cpu_entry);
337 * Find the equivalence ID of our CPU in this table. Even if this table
338 * doesn't contain a patch for the CPU, scan through the whole container
339 * so that it can be skipped in case there are other containers appended.
341 eq_id = find_equiv_id(&table, desc->cpuid_1_eax);
343 buf += hdr[2] + CONTAINER_HDR_SZ;
344 size -= hdr[2] + CONTAINER_HDR_SZ;
347 * Scan through the rest of the container to find where it ends. We do
348 * some basic sanity-checking too.
351 struct microcode_amd *mc;
355 ret = verify_patch(x86_family(desc->cpuid_1_eax), buf, size, &patch_size);
358 * Patch verification failed, skip to the next container, if
359 * there is one. Before exit, check whether that container has
360 * found a patch already. If so, use it.
363 } else if (ret > 0) {
367 mc = (struct microcode_amd *)(buf + SECTION_HDR_SIZE);
368 if (eq_id == mc->hdr.processor_rev_id) {
369 desc->psize = patch_size;
374 /* Skip patch section header too: */
375 buf += patch_size + SECTION_HDR_SIZE;
376 size -= patch_size + SECTION_HDR_SIZE;
381 * If we have found a patch (desc->mc), it means we're looking at the
382 * container which has a patch for this CPU so return 0 to mean, @ucode
383 * already points to the proper container. Otherwise, we return the size
384 * we scanned so that we can advance to the next container in the
389 desc->size = orig_size - size;
394 return orig_size - size;
398 * Scan the ucode blob for the proper container as we can have multiple
399 * containers glued together.
401 static void scan_containers(u8 *ucode, size_t size, struct cont_desc *desc)
404 size_t s = parse_container(ucode, size, desc);
408 /* catch wraparound */
418 static int __apply_microcode_amd(struct microcode_amd *mc)
422 native_wrmsrl(MSR_AMD64_PATCH_LOADER, (u64)(long)&mc->hdr.data_code);
424 /* verify patch application was successful */
425 native_rdmsr(MSR_AMD64_PATCH_LEVEL, rev, dummy);
426 if (rev != mc->hdr.patch_id)
433 * Early load occurs before we can vmalloc(). So we look for the microcode
434 * patch container file in initrd, traverse equivalent cpu table, look for a
435 * matching microcode patch, and update, all in initrd memory in place.
436 * When vmalloc() is available for use later -- on 64-bit during first AP load,
437 * and on 32-bit during save_microcode_in_initrd_amd() -- we can call
438 * load_microcode_amd() to save equivalent cpu table and microcode patches in
439 * kernel heap memory.
441 * Returns true if container found (sets @desc), false otherwise.
443 static bool early_apply_microcode(u32 cpuid_1_eax, u32 old_rev, void *ucode, size_t size)
445 struct cont_desc desc = { 0 };
446 struct microcode_amd *mc;
449 desc.cpuid_1_eax = cpuid_1_eax;
451 scan_containers(ucode, size, &desc);
458 * Allow application of the same revision to pick up SMT-specific
459 * changes even if the revision of the other SMT thread is already
462 if (old_rev > mc->hdr.patch_id)
465 return !__apply_microcode_amd(mc);
468 static bool get_builtin_microcode(struct cpio_data *cp, unsigned int family)
470 char fw_name[36] = "amd-ucode/microcode_amd.bin";
473 if (IS_ENABLED(CONFIG_X86_32))
477 snprintf(fw_name, sizeof(fw_name),
478 "amd-ucode/microcode_amd_fam%02hhxh.bin", family);
480 if (firmware_request_builtin(&fw, fw_name)) {
482 cp->data = (void *)fw.data;
489 static void __init find_blobs_in_containers(unsigned int cpuid_1_eax, struct cpio_data *ret)
493 if (!get_builtin_microcode(&cp, x86_family(cpuid_1_eax)))
494 cp = find_microcode_in_initrd(ucode_path);
499 void __init load_ucode_amd_bsp(struct early_load_data *ed, unsigned int cpuid_1_eax)
501 struct cpio_data cp = { };
504 native_rdmsr(MSR_AMD64_PATCH_LEVEL, ed->old_rev, dummy);
506 /* Needed in load_microcode_amd() */
507 ucode_cpu_info[0].cpu_sig.sig = cpuid_1_eax;
509 find_blobs_in_containers(cpuid_1_eax, &cp);
510 if (!(cp.data && cp.size))
513 if (early_apply_microcode(cpuid_1_eax, ed->old_rev, cp.data, cp.size))
514 native_rdmsr(MSR_AMD64_PATCH_LEVEL, ed->new_rev, dummy);
517 static enum ucode_state load_microcode_amd(u8 family, const u8 *data, size_t size);
519 static int __init save_microcode_in_initrd(void)
521 unsigned int cpuid_1_eax = native_cpuid_eax(1);
522 struct cpuinfo_x86 *c = &boot_cpu_data;
523 struct cont_desc desc = { 0 };
524 enum ucode_state ret;
527 if (dis_ucode_ldr || c->x86_vendor != X86_VENDOR_AMD || c->x86 < 0x10)
530 find_blobs_in_containers(cpuid_1_eax, &cp);
531 if (!(cp.data && cp.size))
534 desc.cpuid_1_eax = cpuid_1_eax;
536 scan_containers(cp.data, cp.size, &desc);
540 ret = load_microcode_amd(x86_family(cpuid_1_eax), desc.data, desc.size);
541 if (ret > UCODE_UPDATED)
546 early_initcall(save_microcode_in_initrd);
549 * a small, trivial cache of per-family ucode patches
551 static struct ucode_patch *cache_find_patch(u16 equiv_cpu)
553 struct ucode_patch *p;
555 list_for_each_entry(p, µcode_cache, plist)
556 if (p->equiv_cpu == equiv_cpu)
561 static void update_cache(struct ucode_patch *new_patch)
563 struct ucode_patch *p;
565 list_for_each_entry(p, µcode_cache, plist) {
566 if (p->equiv_cpu == new_patch->equiv_cpu) {
567 if (p->patch_id >= new_patch->patch_id) {
568 /* we already have the latest patch */
569 kfree(new_patch->data);
574 list_replace(&p->plist, &new_patch->plist);
580 /* no patch found, add it */
581 list_add_tail(&new_patch->plist, µcode_cache);
584 static void free_cache(void)
586 struct ucode_patch *p, *tmp;
588 list_for_each_entry_safe(p, tmp, µcode_cache, plist) {
589 __list_del(p->plist.prev, p->plist.next);
595 static struct ucode_patch *find_patch(unsigned int cpu)
597 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
600 equiv_id = find_equiv_id(&equiv_table, uci->cpu_sig.sig);
604 return cache_find_patch(equiv_id);
607 void reload_ucode_amd(unsigned int cpu)
609 u32 rev, dummy __always_unused;
610 struct microcode_amd *mc;
611 struct ucode_patch *p;
619 rdmsr(MSR_AMD64_PATCH_LEVEL, rev, dummy);
621 if (rev < mc->hdr.patch_id) {
622 if (!__apply_microcode_amd(mc))
623 pr_info_once("reload revision: 0x%08x\n", mc->hdr.patch_id);
627 static int collect_cpu_info_amd(int cpu, struct cpu_signature *csig)
629 struct cpuinfo_x86 *c = &cpu_data(cpu);
630 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
631 struct ucode_patch *p;
633 csig->sig = cpuid_eax(0x00000001);
634 csig->rev = c->microcode;
637 * a patch could have been loaded early, set uci->mc so that
638 * mc_bp_resume() can call apply_microcode()
641 if (p && (p->patch_id == csig->rev))
647 static enum ucode_state apply_microcode_amd(int cpu)
649 struct cpuinfo_x86 *c = &cpu_data(cpu);
650 struct microcode_amd *mc_amd;
651 struct ucode_cpu_info *uci;
652 struct ucode_patch *p;
653 enum ucode_state ret;
654 u32 rev, dummy __always_unused;
656 BUG_ON(raw_smp_processor_id() != cpu);
658 uci = ucode_cpu_info + cpu;
667 rdmsr(MSR_AMD64_PATCH_LEVEL, rev, dummy);
669 /* need to apply patch? */
670 if (rev > mc_amd->hdr.patch_id) {
675 if (__apply_microcode_amd(mc_amd)) {
676 pr_err("CPU%d: update failed for patch_level=0x%08x\n",
677 cpu, mc_amd->hdr.patch_id);
681 rev = mc_amd->hdr.patch_id;
685 uci->cpu_sig.rev = rev;
688 /* Update boot_cpu_data's revision too, if we're on the BSP: */
689 if (c->cpu_index == boot_cpu_data.cpu_index)
690 boot_cpu_data.microcode = rev;
695 void load_ucode_amd_ap(unsigned int cpuid_1_eax)
697 unsigned int cpu = smp_processor_id();
699 ucode_cpu_info[cpu].cpu_sig.sig = cpuid_1_eax;
700 apply_microcode_amd(cpu);
703 static size_t install_equiv_cpu_table(const u8 *buf, size_t buf_size)
708 if (!verify_equivalence_table(buf, buf_size))
711 hdr = (const u32 *)buf;
712 equiv_tbl_len = hdr[2];
714 equiv_table.entry = vmalloc(equiv_tbl_len);
715 if (!equiv_table.entry) {
716 pr_err("failed to allocate equivalent CPU table\n");
720 memcpy(equiv_table.entry, buf + CONTAINER_HDR_SZ, equiv_tbl_len);
721 equiv_table.num_entries = equiv_tbl_len / sizeof(struct equiv_cpu_entry);
723 /* add header length */
724 return equiv_tbl_len + CONTAINER_HDR_SZ;
727 static void free_equiv_cpu_table(void)
729 vfree(equiv_table.entry);
730 memset(&equiv_table, 0, sizeof(equiv_table));
733 static void cleanup(void)
735 free_equiv_cpu_table();
740 * Return a non-negative value even if some of the checks failed so that
741 * we can skip over the next patch. If we return a negative value, we
742 * signal a grave error like a memory allocation has failed and the
743 * driver cannot continue functioning normally. In such cases, we tear
744 * down everything we've used up so far and exit.
746 static int verify_and_add_patch(u8 family, u8 *fw, unsigned int leftover,
747 unsigned int *patch_size)
749 struct microcode_header_amd *mc_hdr;
750 struct ucode_patch *patch;
754 ret = verify_patch(family, fw, leftover, patch_size);
758 patch = kzalloc(sizeof(*patch), GFP_KERNEL);
760 pr_err("Patch allocation failure.\n");
764 patch->data = kmemdup(fw + SECTION_HDR_SIZE, *patch_size, GFP_KERNEL);
766 pr_err("Patch data allocation failure.\n");
770 patch->size = *patch_size;
772 mc_hdr = (struct microcode_header_amd *)(fw + SECTION_HDR_SIZE);
773 proc_id = mc_hdr->processor_rev_id;
775 INIT_LIST_HEAD(&patch->plist);
776 patch->patch_id = mc_hdr->patch_id;
777 patch->equiv_cpu = proc_id;
779 pr_debug("%s: Added patch_id: 0x%08x, proc_id: 0x%04x\n",
780 __func__, patch->patch_id, proc_id);
782 /* ... and add to cache. */
788 /* Scan the blob in @data and add microcode patches to the cache. */
789 static enum ucode_state __load_microcode_amd(u8 family, const u8 *data,
795 offset = install_equiv_cpu_table(data, size);
802 if (*(u32 *)fw != UCODE_UCODE_TYPE) {
803 pr_err("invalid type field in container file section header\n");
804 free_equiv_cpu_table();
809 unsigned int crnt_size = 0;
812 ret = verify_and_add_patch(family, fw, size, &crnt_size);
816 fw += crnt_size + SECTION_HDR_SIZE;
817 size -= (crnt_size + SECTION_HDR_SIZE);
823 static enum ucode_state load_microcode_amd(u8 family, const u8 *data, size_t size)
825 struct cpuinfo_x86 *c;
826 unsigned int nid, cpu;
827 struct ucode_patch *p;
828 enum ucode_state ret;
830 /* free old equiv table */
831 free_equiv_cpu_table();
833 ret = __load_microcode_amd(family, data, size);
834 if (ret != UCODE_OK) {
840 cpu = cpumask_first(cpumask_of_node(nid));
847 if (c->microcode >= p->patch_id)
857 * AMD microcode firmware naming convention, up to family 15h they are in
860 * amd-ucode/microcode_amd.bin
862 * This legacy file is always smaller than 2K in size.
864 * Beginning with family 15h, they are in family-specific firmware files:
866 * amd-ucode/microcode_amd_fam15h.bin
867 * amd-ucode/microcode_amd_fam16h.bin
870 * These might be larger than 2K.
872 static enum ucode_state request_microcode_amd(int cpu, struct device *device)
874 char fw_name[36] = "amd-ucode/microcode_amd.bin";
875 struct cpuinfo_x86 *c = &cpu_data(cpu);
876 enum ucode_state ret = UCODE_NFOUND;
877 const struct firmware *fw;
883 snprintf(fw_name, sizeof(fw_name), "amd-ucode/microcode_amd_fam%.2xh.bin", c->x86);
885 if (request_firmware_direct(&fw, (const char *)fw_name, device)) {
886 pr_debug("failed to load file %s\n", fw_name);
891 if (!verify_container(fw->data, fw->size))
894 ret = load_microcode_amd(c->x86, fw->data, fw->size);
897 release_firmware(fw);
903 static void microcode_fini_cpu_amd(int cpu)
905 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
910 static struct microcode_ops microcode_amd_ops = {
911 .request_microcode_fw = request_microcode_amd,
912 .collect_cpu_info = collect_cpu_info_amd,
913 .apply_microcode = apply_microcode_amd,
914 .microcode_fini_cpu = microcode_fini_cpu_amd,
918 struct microcode_ops * __init init_amd_microcode(void)
920 struct cpuinfo_x86 *c = &boot_cpu_data;
922 if (c->x86_vendor != X86_VENDOR_AMD || c->x86 < 0x10) {
923 pr_warn("AMD CPU family 0x%x not supported\n", c->x86);
926 return µcode_amd_ops;
929 void __exit exit_amd_microcode(void)