GNU Linux-libre 4.9.326-gnu1
[releases.git] / arch / x86 / kernel / cpu / microcode / amd.c
1 /*
2  *  AMD CPU Microcode Update Driver for Linux
3  *
4  *  This driver allows to upgrade microcode on F10h AMD
5  *  CPUs and later.
6  *
7  *  Copyright (C) 2008-2011 Advanced Micro Devices Inc.
8  *
9  *  Author: Peter Oruba <peter.oruba@amd.com>
10  *
11  *  Based on work by:
12  *  Tigran Aivazian <tigran@aivazian.fsnet.co.uk>
13  *
14  *  early loader:
15  *  Copyright (C) 2013 Advanced Micro Devices, Inc.
16  *
17  *  Author: Jacob Shin <jacob.shin@amd.com>
18  *  Fixes: Borislav Petkov <bp@suse.de>
19  *
20  *  Licensed under the terms of the GNU General Public
21  *  License version 2. See file COPYING for details.
22  */
23 #define pr_fmt(fmt) "microcode: " fmt
24
25 #include <linux/earlycpio.h>
26 #include <linux/firmware.h>
27 #include <linux/uaccess.h>
28 #include <linux/vmalloc.h>
29 #include <linux/initrd.h>
30 #include <linux/kernel.h>
31 #include <linux/pci.h>
32
33 #include <asm/microcode_amd.h>
34 #include <asm/microcode.h>
35 #include <asm/processor.h>
36 #include <asm/setup.h>
37 #include <asm/cpu.h>
38 #include <asm/msr.h>
39
40 static struct equiv_cpu_entry *equiv_cpu_table;
41
42 struct ucode_patch {
43         struct list_head plist;
44         void *data;
45         u32 patch_id;
46         u16 equiv_cpu;
47 };
48
49 static LIST_HEAD(pcache);
50
51 /*
52  * This points to the current valid container of microcode patches which we will
53  * save from the initrd before jettisoning its contents.
54  */
55 static u8 *container;
56 static size_t container_size;
57 static bool ucode_builtin;
58
59 static u32 ucode_new_rev;
60 static u8 amd_ucode_patch[PATCH_MAX_SIZE];
61 static u16 this_equiv_id;
62
63 static struct cpio_data ucode_cpio;
64
65 static struct cpio_data __init find_ucode_in_initrd(void)
66 {
67 #ifdef CONFIG_BLK_DEV_INITRD
68         char *path;
69         void *start;
70         size_t size;
71
72         /*
73          * Microcode patch container file is prepended to the initrd in cpio
74          * format. See Documentation/x86/early-microcode.txt
75          */
76         static __initdata char ucode_path[] = "/*(DEBLOBBED)*/";
77
78 #ifdef CONFIG_X86_32
79         struct boot_params *p;
80
81         /*
82          * On 32-bit, early load occurs before paging is turned on so we need
83          * to use physical addresses.
84          */
85         p       = (struct boot_params *)__pa_nodebug(&boot_params);
86         path    = (char *)__pa_nodebug(ucode_path);
87         start   = (void *)p->hdr.ramdisk_image;
88         size    = p->hdr.ramdisk_size;
89 #else
90         path    = ucode_path;
91         start   = (void *)(boot_params.hdr.ramdisk_image + PAGE_OFFSET);
92         size    = boot_params.hdr.ramdisk_size;
93 #endif /* !CONFIG_X86_32 */
94
95         return find_cpio_data(path, start, size, NULL);
96 #else
97         return (struct cpio_data){ NULL, 0, "" };
98 #endif
99 }
100
101 static size_t compute_container_size(u8 *data, u32 total_size)
102 {
103         size_t size = 0;
104         u32 *header = (u32 *)data;
105
106         if (header[0] != UCODE_MAGIC ||
107             header[1] != UCODE_EQUIV_CPU_TABLE_TYPE || /* type */
108             header[2] == 0)                            /* size */
109                 return size;
110
111         size = header[2] + CONTAINER_HDR_SZ;
112         total_size -= size;
113         data += size;
114
115         while (total_size) {
116                 u16 patch_size;
117
118                 header = (u32 *)data;
119
120                 if (header[0] != UCODE_UCODE_TYPE)
121                         break;
122
123                 /*
124                  * Sanity-check patch size.
125                  */
126                 patch_size = header[1];
127                 if (patch_size > PATCH_MAX_SIZE)
128                         break;
129
130                 size       += patch_size + SECTION_HDR_SIZE;
131                 data       += patch_size + SECTION_HDR_SIZE;
132                 total_size -= patch_size + SECTION_HDR_SIZE;
133         }
134
135         return size;
136 }
137
138 static enum ucode_state
139 load_microcode_amd(bool save, u8 family, const u8 *data, size_t size);
140
141 /*
142  * Early load occurs before we can vmalloc(). So we look for the microcode
143  * patch container file in initrd, traverse equivalent cpu table, look for a
144  * matching microcode patch, and update, all in initrd memory in place.
145  * When vmalloc() is available for use later -- on 64-bit during first AP load,
146  * and on 32-bit during save_microcode_in_initrd_amd() -- we can call
147  * load_microcode_amd() to save equivalent cpu table and microcode patches in
148  * kernel heap memory.
149  */
150 static void apply_ucode_in_initrd(void *ucode, size_t size, bool save_patch)
151 {
152         struct equiv_cpu_entry *eq;
153         size_t *cont_sz;
154         u32 *header;
155         u8  *data, **cont;
156         u8 (*patch)[PATCH_MAX_SIZE];
157         u16 eq_id = 0;
158         int offset, left;
159         u32 rev, eax, ebx, ecx, edx;
160         u32 *new_rev;
161
162 #ifdef CONFIG_X86_32
163         new_rev = (u32 *)__pa_nodebug(&ucode_new_rev);
164         cont_sz = (size_t *)__pa_nodebug(&container_size);
165         cont    = (u8 **)__pa_nodebug(&container);
166         patch   = (u8 (*)[PATCH_MAX_SIZE])__pa_nodebug(&amd_ucode_patch);
167 #else
168         new_rev = &ucode_new_rev;
169         cont_sz = &container_size;
170         cont    = &container;
171         patch   = &amd_ucode_patch;
172 #endif
173
174         data   = ucode;
175         left   = size;
176         header = (u32 *)data;
177
178         /* find equiv cpu table */
179         if (header[0] != UCODE_MAGIC ||
180             header[1] != UCODE_EQUIV_CPU_TABLE_TYPE || /* type */
181             header[2] == 0)                            /* size */
182                 return;
183
184         eax = 0x00000001;
185         ecx = 0;
186         native_cpuid(&eax, &ebx, &ecx, &edx);
187
188         while (left > 0) {
189                 eq = (struct equiv_cpu_entry *)(data + CONTAINER_HDR_SZ);
190
191                 *cont = data;
192
193                 /* Advance past the container header */
194                 offset = header[2] + CONTAINER_HDR_SZ;
195                 data  += offset;
196                 left  -= offset;
197
198                 eq_id = find_equiv_id(eq, eax);
199                 if (eq_id) {
200                         this_equiv_id = eq_id;
201                         *cont_sz = compute_container_size(*cont, left + offset);
202
203                         /*
204                          * truncate how much we need to iterate over in the
205                          * ucode update loop below
206                          */
207                         left = *cont_sz - offset;
208                         break;
209                 }
210
211                 /*
212                  * support multiple container files appended together. if this
213                  * one does not have a matching equivalent cpu entry, we fast
214                  * forward to the next container file.
215                  */
216                 while (left > 0) {
217                         header = (u32 *)data;
218                         if (header[0] == UCODE_MAGIC &&
219                             header[1] == UCODE_EQUIV_CPU_TABLE_TYPE)
220                                 break;
221
222                         offset = header[1] + SECTION_HDR_SIZE;
223                         data  += offset;
224                         left  -= offset;
225                 }
226
227                 /* mark where the next microcode container file starts */
228                 offset    = data - (u8 *)ucode;
229                 ucode     = data;
230         }
231
232         if (!eq_id) {
233                 *cont = NULL;
234                 *cont_sz = 0;
235                 return;
236         }
237
238         if (check_current_patch_level(&rev, true))
239                 return;
240
241         while (left > 0) {
242                 struct microcode_amd *mc;
243
244                 header = (u32 *)data;
245                 if (header[0] != UCODE_UCODE_TYPE || /* type */
246                     header[1] == 0)                  /* size */
247                         break;
248
249                 mc = (struct microcode_amd *)(data + SECTION_HDR_SIZE);
250
251                 if (eq_id == mc->hdr.processor_rev_id && rev < mc->hdr.patch_id) {
252
253                         if (!__apply_microcode_amd(mc)) {
254                                 rev = mc->hdr.patch_id;
255                                 *new_rev = rev;
256
257                                 if (save_patch)
258                                         memcpy(patch, mc,
259                                                min_t(u32, header[1], PATCH_MAX_SIZE));
260                         }
261                 }
262
263                 offset  = header[1] + SECTION_HDR_SIZE;
264                 data   += offset;
265                 left   -= offset;
266         }
267 }
268
269 static bool __init load_builtin_amd_microcode(struct cpio_data *cp,
270                                               unsigned int family)
271 {
272 #ifdef CONFIG_X86_64
273         char fw_name[36] = "/*(DEBLOBBED)*/";
274
275         if (family >= 0x15)
276                 snprintf(fw_name, sizeof(fw_name),
277                          "/*(DEBLOBBED)*/", family);
278
279         return get_builtin_firmware(cp, fw_name);
280 #else
281         return false;
282 #endif
283 }
284
285 void __init load_ucode_amd_bsp(unsigned int family)
286 {
287         struct cpio_data cp;
288         bool *builtin;
289         void **data;
290         size_t *size;
291
292 #ifdef CONFIG_X86_32
293         data =  (void **)__pa_nodebug(&ucode_cpio.data);
294         size = (size_t *)__pa_nodebug(&ucode_cpio.size);
295         builtin = (bool *)__pa_nodebug(&ucode_builtin);
296 #else
297         data = &ucode_cpio.data;
298         size = &ucode_cpio.size;
299         builtin = &ucode_builtin;
300 #endif
301
302         *builtin = load_builtin_amd_microcode(&cp, family);
303         if (!*builtin)
304                 cp = find_ucode_in_initrd();
305
306         if (!(cp.data && cp.size))
307                 return;
308
309         *data = cp.data;
310         *size = cp.size;
311
312         apply_ucode_in_initrd(cp.data, cp.size, true);
313 }
314
315 #ifdef CONFIG_X86_32
316 /*
317  * On 32-bit, since AP's early load occurs before paging is turned on, we
318  * cannot traverse cpu_equiv_table and pcache in kernel heap memory. So during
319  * cold boot, AP will apply_ucode_in_initrd() just like the BSP. During
320  * save_microcode_in_initrd_amd() BSP's patch is copied to amd_ucode_patch,
321  * which is used upon resume from suspend.
322  */
323 void load_ucode_amd_ap(void)
324 {
325         struct microcode_amd *mc;
326         size_t *usize;
327         void **ucode;
328
329         mc = (struct microcode_amd *)__pa_nodebug(amd_ucode_patch);
330         if (mc->hdr.patch_id && mc->hdr.processor_rev_id) {
331                 __apply_microcode_amd(mc);
332                 return;
333         }
334
335         ucode = (void *)__pa_nodebug(&container);
336         usize = (size_t *)__pa_nodebug(&container_size);
337
338         if (!*ucode || !*usize)
339                 return;
340
341         apply_ucode_in_initrd(*ucode, *usize, false);
342 }
343
344 static void __init collect_cpu_sig_on_bsp(void *arg)
345 {
346         unsigned int cpu = smp_processor_id();
347         struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
348
349         uci->cpu_sig.sig = cpuid_eax(0x00000001);
350 }
351
352 static void __init get_bsp_sig(void)
353 {
354         unsigned int bsp = boot_cpu_data.cpu_index;
355         struct ucode_cpu_info *uci = ucode_cpu_info + bsp;
356
357         if (!uci->cpu_sig.sig)
358                 smp_call_function_single(bsp, collect_cpu_sig_on_bsp, NULL, 1);
359 }
360 #else
361 void load_ucode_amd_ap(void)
362 {
363         unsigned int cpu = smp_processor_id();
364         struct equiv_cpu_entry *eq;
365         struct microcode_amd *mc;
366         u8 *cont = container;
367         u32 rev, eax;
368         u16 eq_id;
369
370         /* Exit if called on the BSP. */
371         if (!cpu)
372                 return;
373
374         if (!container)
375                 return;
376
377         /*
378          * 64-bit runs with paging enabled, thus early==false.
379          */
380         if (check_current_patch_level(&rev, false))
381                 return;
382
383         /* Add CONFIG_RANDOMIZE_MEMORY offset. */
384         if (!ucode_builtin)
385                 cont += PAGE_OFFSET - __PAGE_OFFSET_BASE;
386
387         eax = cpuid_eax(0x00000001);
388         eq  = (struct equiv_cpu_entry *)(cont + CONTAINER_HDR_SZ);
389
390         eq_id = find_equiv_id(eq, eax);
391         if (!eq_id)
392                 return;
393
394         if (eq_id == this_equiv_id) {
395                 mc = (struct microcode_amd *)amd_ucode_patch;
396
397                 if (mc && rev < mc->hdr.patch_id) {
398                         if (!__apply_microcode_amd(mc))
399                                 ucode_new_rev = mc->hdr.patch_id;
400                 }
401
402         } else {
403                 if (!ucode_cpio.data)
404                         return;
405
406                 /*
407                  * AP has a different equivalence ID than BSP, looks like
408                  * mixed-steppings silicon so go through the ucode blob anew.
409                  */
410                 apply_ucode_in_initrd(ucode_cpio.data, ucode_cpio.size, false);
411         }
412 }
413 #endif
414
415 int __init save_microcode_in_initrd_amd(void)
416 {
417         unsigned long cont;
418         int retval = 0;
419         enum ucode_state ret;
420         u8 *cont_va;
421         u32 eax;
422
423         if (!container)
424                 return -EINVAL;
425
426 #ifdef CONFIG_X86_32
427         get_bsp_sig();
428         cont    = (unsigned long)container;
429         cont_va = __va(container);
430 #else
431         /*
432          * We need the physical address of the container for both bitness since
433          * boot_params.hdr.ramdisk_image is a physical address.
434          */
435         cont    = __pa_nodebug(container);
436         cont_va = container;
437 #endif
438
439         /*
440          * Take into account the fact that the ramdisk might get relocated and
441          * therefore we need to recompute the container's position in virtual
442          * memory space.
443          */
444         if (relocated_ramdisk)
445                 container = (u8 *)(__va(relocated_ramdisk) +
446                              (cont - boot_params.hdr.ramdisk_image));
447         else
448                 container = cont_va;
449
450         /* Add CONFIG_RANDOMIZE_MEMORY offset. */
451         if (!ucode_builtin)
452                 container += PAGE_OFFSET - __PAGE_OFFSET_BASE;
453
454         eax   = cpuid_eax(0x00000001);
455         eax   = ((eax >> 8) & 0xf) + ((eax >> 20) & 0xff);
456
457         ret = load_microcode_amd(true, eax, container, container_size);
458         if (ret != UCODE_OK)
459                 retval = -EINVAL;
460
461         /*
462          * This will be freed any msec now, stash patches for the current
463          * family and switch to patch cache for cpu hotplug, etc later.
464          */
465         container = NULL;
466         container_size = 0;
467
468         return retval;
469 }
470
471 void reload_ucode_amd(void)
472 {
473         struct microcode_amd *mc;
474         u32 rev;
475
476         /*
477          * early==false because this is a syscore ->resume path and by
478          * that time paging is long enabled.
479          */
480         if (check_current_patch_level(&rev, false))
481                 return;
482
483         mc = (struct microcode_amd *)amd_ucode_patch;
484
485         if (mc && rev < mc->hdr.patch_id) {
486                 if (!__apply_microcode_amd(mc)) {
487                         ucode_new_rev = mc->hdr.patch_id;
488                         pr_info("reload patch_level=0x%08x\n", ucode_new_rev);
489                 }
490         }
491 }
492 static u16 __find_equiv_id(unsigned int cpu)
493 {
494         struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
495         return find_equiv_id(equiv_cpu_table, uci->cpu_sig.sig);
496 }
497
498 static u32 find_cpu_family_by_equiv_cpu(u16 equiv_cpu)
499 {
500         int i = 0;
501
502         BUG_ON(!equiv_cpu_table);
503
504         while (equiv_cpu_table[i].equiv_cpu != 0) {
505                 if (equiv_cpu == equiv_cpu_table[i].equiv_cpu)
506                         return equiv_cpu_table[i].installed_cpu;
507                 i++;
508         }
509         return 0;
510 }
511
512 /*
513  * a small, trivial cache of per-family ucode patches
514  */
515 static struct ucode_patch *cache_find_patch(u16 equiv_cpu)
516 {
517         struct ucode_patch *p;
518
519         list_for_each_entry(p, &pcache, plist)
520                 if (p->equiv_cpu == equiv_cpu)
521                         return p;
522         return NULL;
523 }
524
525 static void update_cache(struct ucode_patch *new_patch)
526 {
527         struct ucode_patch *p;
528
529         list_for_each_entry(p, &pcache, plist) {
530                 if (p->equiv_cpu == new_patch->equiv_cpu) {
531                         if (p->patch_id >= new_patch->patch_id)
532                                 /* we already have the latest patch */
533                                 return;
534
535                         list_replace(&p->plist, &new_patch->plist);
536                         kfree(p->data);
537                         kfree(p);
538                         return;
539                 }
540         }
541         /* no patch found, add it */
542         list_add_tail(&new_patch->plist, &pcache);
543 }
544
545 static void free_cache(void)
546 {
547         struct ucode_patch *p, *tmp;
548
549         list_for_each_entry_safe(p, tmp, &pcache, plist) {
550                 __list_del(p->plist.prev, p->plist.next);
551                 kfree(p->data);
552                 kfree(p);
553         }
554 }
555
556 static struct ucode_patch *find_patch(unsigned int cpu)
557 {
558         u16 equiv_id;
559
560         equiv_id = __find_equiv_id(cpu);
561         if (!equiv_id)
562                 return NULL;
563
564         return cache_find_patch(equiv_id);
565 }
566
567 static int collect_cpu_info_amd(int cpu, struct cpu_signature *csig)
568 {
569         struct cpuinfo_x86 *c = &cpu_data(cpu);
570         struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
571         struct ucode_patch *p;
572
573         csig->sig = cpuid_eax(0x00000001);
574         csig->rev = c->microcode;
575
576         /*
577          * a patch could have been loaded early, set uci->mc so that
578          * mc_bp_resume() can call apply_microcode()
579          */
580         p = find_patch(cpu);
581         if (p && (p->patch_id == csig->rev))
582                 uci->mc = p->data;
583
584         pr_info("CPU%d: patch_level=0x%08x\n", cpu, csig->rev);
585
586         return 0;
587 }
588
589 static unsigned int verify_patch_size(u8 family, u32 patch_size,
590                                       unsigned int size)
591 {
592         u32 max_size;
593
594 #define F1XH_MPB_MAX_SIZE 2048
595 #define F14H_MPB_MAX_SIZE 1824
596 #define F15H_MPB_MAX_SIZE 4096
597 #define F16H_MPB_MAX_SIZE 3458
598 #define F17H_MPB_MAX_SIZE 3200
599
600         switch (family) {
601         case 0x14:
602                 max_size = F14H_MPB_MAX_SIZE;
603                 break;
604         case 0x15:
605                 max_size = F15H_MPB_MAX_SIZE;
606                 break;
607         case 0x16:
608                 max_size = F16H_MPB_MAX_SIZE;
609                 break;
610         case 0x17:
611                 max_size = F17H_MPB_MAX_SIZE;
612                 break;
613         default:
614                 max_size = F1XH_MPB_MAX_SIZE;
615                 break;
616         }
617
618         if (patch_size > min_t(u32, size, max_size)) {
619                 pr_err("patch size mismatch\n");
620                 return 0;
621         }
622
623         return patch_size;
624 }
625
626 /*
627  * Those patch levels cannot be updated to newer ones and thus should be final.
628  */
629 static u32 final_levels[] = {
630         0x01000098,
631         0x0100009f,
632         0x010000af,
633         0, /* T-101 terminator */
634 };
635
636 /*
637  * Check the current patch level on this CPU.
638  *
639  * @rev: Use it to return the patch level. It is set to 0 in the case of
640  * error.
641  *
642  * Returns:
643  *  - true: if update should stop
644  *  - false: otherwise
645  */
646 bool check_current_patch_level(u32 *rev, bool early)
647 {
648         u32 lvl, dummy, i;
649         bool ret = false;
650         u32 *levels;
651
652         native_rdmsr(MSR_AMD64_PATCH_LEVEL, lvl, dummy);
653
654         if (IS_ENABLED(CONFIG_X86_32) && early)
655                 levels = (u32 *)__pa_nodebug(&final_levels);
656         else
657                 levels = final_levels;
658
659         for (i = 0; levels[i]; i++) {
660                 if (lvl == levels[i]) {
661                         lvl = 0;
662                         ret = true;
663                         break;
664                 }
665         }
666
667         if (rev)
668                 *rev = lvl;
669
670         return ret;
671 }
672
673 int __apply_microcode_amd(struct microcode_amd *mc_amd)
674 {
675         u32 rev, dummy;
676
677         native_wrmsrl(MSR_AMD64_PATCH_LOADER, (u64)(long)&mc_amd->hdr.data_code);
678
679         /* verify patch application was successful */
680         native_rdmsr(MSR_AMD64_PATCH_LEVEL, rev, dummy);
681         if (rev != mc_amd->hdr.patch_id)
682                 return -1;
683
684         return 0;
685 }
686
687 int apply_microcode_amd(int cpu)
688 {
689         struct cpuinfo_x86 *c = &cpu_data(cpu);
690         struct microcode_amd *mc_amd;
691         struct ucode_cpu_info *uci;
692         struct ucode_patch *p;
693         u32 rev;
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 0;
702
703         mc_amd  = p->data;
704         uci->mc = p->data;
705
706         if (check_current_patch_level(&rev, false))
707                 return -1;
708
709         /* need to apply patch? */
710         if (rev >= mc_amd->hdr.patch_id)
711                 goto out;
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 -1;
717         }
718
719         rev = mc_amd->hdr.patch_id;
720
721         pr_info("CPU%d: new patch_level=0x%08x\n", cpu, rev);
722
723 out:
724         uci->cpu_sig.rev = rev;
725         c->microcode     = rev;
726
727         /* Update boot_cpu_data's revision too, if we're on the BSP: */
728         if (c->cpu_index == boot_cpu_data.cpu_index)
729                 boot_cpu_data.microcode = rev;
730
731         return 0;
732 }
733
734 static int install_equiv_cpu_table(const u8 *buf)
735 {
736         unsigned int *ibuf = (unsigned int *)buf;
737         unsigned int type = ibuf[1];
738         unsigned int size = ibuf[2];
739
740         if (type != UCODE_EQUIV_CPU_TABLE_TYPE || !size) {
741                 pr_err("empty section/"
742                        "invalid type field in container file section header\n");
743                 return -EINVAL;
744         }
745
746         equiv_cpu_table = vmalloc(size);
747         if (!equiv_cpu_table) {
748                 pr_err("failed to allocate equivalent CPU table\n");
749                 return -ENOMEM;
750         }
751
752         memcpy(equiv_cpu_table, buf + CONTAINER_HDR_SZ, size);
753
754         /* add header length */
755         return size + CONTAINER_HDR_SZ;
756 }
757
758 static void free_equiv_cpu_table(void)
759 {
760         vfree(equiv_cpu_table);
761         equiv_cpu_table = NULL;
762 }
763
764 static void cleanup(void)
765 {
766         free_equiv_cpu_table();
767         free_cache();
768 }
769
770 /*
771  * We return the current size even if some of the checks failed so that
772  * we can skip over the next patch. If we return a negative value, we
773  * signal a grave error like a memory allocation has failed and the
774  * driver cannot continue functioning normally. In such cases, we tear
775  * down everything we've used up so far and exit.
776  */
777 static int verify_and_add_patch(u8 family, u8 *fw, unsigned int leftover)
778 {
779         struct microcode_header_amd *mc_hdr;
780         struct ucode_patch *patch;
781         unsigned int patch_size, crnt_size, ret;
782         u32 proc_fam;
783         u16 proc_id;
784
785         patch_size  = *(u32 *)(fw + 4);
786         crnt_size   = patch_size + SECTION_HDR_SIZE;
787         mc_hdr      = (struct microcode_header_amd *)(fw + SECTION_HDR_SIZE);
788         proc_id     = mc_hdr->processor_rev_id;
789
790         proc_fam = find_cpu_family_by_equiv_cpu(proc_id);
791         if (!proc_fam) {
792                 pr_err("No patch family for equiv ID: 0x%04x\n", proc_id);
793                 return crnt_size;
794         }
795
796         /* check if patch is for the current family */
797         proc_fam = ((proc_fam >> 8) & 0xf) + ((proc_fam >> 20) & 0xff);
798         if (proc_fam != family)
799                 return crnt_size;
800
801         if (mc_hdr->nb_dev_id || mc_hdr->sb_dev_id) {
802                 pr_err("Patch-ID 0x%08x: chipset-specific code unsupported.\n",
803                         mc_hdr->patch_id);
804                 return crnt_size;
805         }
806
807         ret = verify_patch_size(family, patch_size, leftover);
808         if (!ret) {
809                 pr_err("Patch-ID 0x%08x: size mismatch.\n", mc_hdr->patch_id);
810                 return crnt_size;
811         }
812
813         patch = kzalloc(sizeof(*patch), GFP_KERNEL);
814         if (!patch) {
815                 pr_err("Patch allocation failure.\n");
816                 return -EINVAL;
817         }
818
819         patch->data = kmemdup(fw + SECTION_HDR_SIZE, patch_size, GFP_KERNEL);
820         if (!patch->data) {
821                 pr_err("Patch data allocation failure.\n");
822                 kfree(patch);
823                 return -EINVAL;
824         }
825
826         INIT_LIST_HEAD(&patch->plist);
827         patch->patch_id  = mc_hdr->patch_id;
828         patch->equiv_cpu = proc_id;
829
830         pr_debug("%s: Added patch_id: 0x%08x, proc_id: 0x%04x\n",
831                  __func__, patch->patch_id, proc_id);
832
833         /* ... and add to cache. */
834         update_cache(patch);
835
836         return crnt_size;
837 }
838
839 static enum ucode_state __load_microcode_amd(u8 family, const u8 *data,
840                                              size_t size)
841 {
842         enum ucode_state ret = UCODE_ERROR;
843         unsigned int leftover;
844         u8 *fw = (u8 *)data;
845         int crnt_size = 0;
846         int offset;
847
848         offset = install_equiv_cpu_table(data);
849         if (offset < 0) {
850                 pr_err("failed to create equivalent cpu table\n");
851                 return ret;
852         }
853         fw += offset;
854         leftover = size - offset;
855
856         if (*(u32 *)fw != UCODE_UCODE_TYPE) {
857                 pr_err("invalid type field in container file section header\n");
858                 free_equiv_cpu_table();
859                 return ret;
860         }
861
862         while (leftover) {
863                 crnt_size = verify_and_add_patch(family, fw, leftover);
864                 if (crnt_size < 0)
865                         return ret;
866
867                 fw       += crnt_size;
868                 leftover -= crnt_size;
869         }
870
871         return UCODE_OK;
872 }
873
874 static enum ucode_state
875 load_microcode_amd(bool save, u8 family, const u8 *data, size_t size)
876 {
877         enum ucode_state ret;
878
879         /* free old equiv table */
880         free_equiv_cpu_table();
881
882         ret = __load_microcode_amd(family, data, size);
883
884         if (ret != UCODE_OK)
885                 cleanup();
886
887 #ifdef CONFIG_X86_32
888         /* save BSP's matching patch for early load */
889         if (save) {
890                 struct ucode_patch *p = find_patch(0);
891                 if (p) {
892                         memset(amd_ucode_patch, 0, PATCH_MAX_SIZE);
893                         memcpy(amd_ucode_patch, p->data, min_t(u32, ksize(p->data),
894                                                                PATCH_MAX_SIZE));
895                 }
896         }
897 #endif
898         return ret;
899 }
900
901 /*(DEBLOBBED)*/
902 static enum ucode_state request_microcode_amd(int cpu, struct device *device,
903                                               bool refresh_fw)
904 {
905         char fw_name[36] = "/*(DEBLOBBED)*/";
906         struct cpuinfo_x86 *c = &cpu_data(cpu);
907         bool bsp = c->cpu_index == boot_cpu_data.cpu_index;
908         enum ucode_state ret = UCODE_NFOUND;
909         const struct firmware *fw;
910
911         /* reload ucode container only on the boot cpu */
912         if (!refresh_fw || !bsp)
913                 return UCODE_OK;
914
915         if (c->x86 >= 0x15)
916                 snprintf(fw_name, sizeof(fw_name), "/*(DEBLOBBED)*/", c->x86);
917
918         if (reject_firmware_direct(&fw, (const char *)fw_name, device)) {
919                 pr_debug("failed to load file %s\n", fw_name);
920                 goto out;
921         }
922
923         ret = UCODE_ERROR;
924         if (*(u32 *)fw->data != UCODE_MAGIC) {
925                 pr_err("invalid magic value (0x%08x)\n", *(u32 *)fw->data);
926                 goto fw_release;
927         }
928
929         ret = load_microcode_amd(bsp, c->x86, fw->data, fw->size);
930
931  fw_release:
932         release_firmware(fw);
933
934  out:
935         return ret;
936 }
937
938 static enum ucode_state
939 request_microcode_user(int cpu, const void __user *buf, size_t size)
940 {
941         return UCODE_ERROR;
942 }
943
944 static void microcode_fini_cpu_amd(int cpu)
945 {
946         struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
947
948         uci->mc = NULL;
949 }
950
951 static struct microcode_ops microcode_amd_ops = {
952         .request_microcode_user           = request_microcode_user,
953         .request_microcode_fw             = request_microcode_amd,
954         .collect_cpu_info                 = collect_cpu_info_amd,
955         .apply_microcode                  = apply_microcode_amd,
956         .microcode_fini_cpu               = microcode_fini_cpu_amd,
957 };
958
959 struct microcode_ops * __init init_amd_microcode(void)
960 {
961         struct cpuinfo_x86 *c = &boot_cpu_data;
962
963         if (c->x86_vendor != X86_VENDOR_AMD || c->x86 < 0x10) {
964                 pr_warn("AMD CPU family 0x%x not supported\n", c->x86);
965                 return NULL;
966         }
967
968         if (ucode_new_rev)
969                 pr_info_once("microcode updated early to new patch_level=0x%08x\n",
970                              ucode_new_rev);
971
972         return &microcode_amd_ops;
973 }
974
975 void __exit exit_amd_microcode(void)
976 {
977         cleanup();
978 }