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