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