GNU Linux-libre 4.19.286-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  *                2013-2016 Borislav Petkov <bp@alien8.de>
9  *
10  *  Author: Peter Oruba <peter.oruba@amd.com>
11  *
12  *  Based on work by:
13  *  Tigran Aivazian <aivazian.tigran@gmail.com>
14  *
15  *  early loader:
16  *  Copyright (C) 2013 Advanced Micro Devices, Inc.
17  *
18  *  Author: Jacob Shin <jacob.shin@amd.com>
19  *  Fixes: Borislav Petkov <bp@suse.de>
20  *
21  *  Licensed under the terms of the GNU General Public
22  *  License version 2. See file COPYING for details.
23  */
24 #define pr_fmt(fmt) "microcode: " fmt
25
26 #include <linux/earlycpio.h>
27 #include <linux/firmware.h>
28 #include <linux/uaccess.h>
29 #include <linux/vmalloc.h>
30 #include <linux/initrd.h>
31 #include <linux/kernel.h>
32 #include <linux/pci.h>
33
34 #include <asm/microcode_amd.h>
35 #include <asm/microcode.h>
36 #include <asm/processor.h>
37 #include <asm/setup.h>
38 #include <asm/cpu.h>
39 #include <asm/msr.h>
40
41 static struct equiv_cpu_entry *equiv_cpu_table;
42
43 /*
44  * This points to the current valid container of microcode patches which we will
45  * save from the initrd/builtin before jettisoning its contents. @mc is the
46  * microcode patch we found to match.
47  */
48 struct cont_desc {
49         struct microcode_amd *mc;
50         u32                  cpuid_1_eax;
51         u32                  psize;
52         u8                   *data;
53         size_t               size;
54 };
55
56 static u32 ucode_new_rev;
57
58 /* One blob per node. */
59 static u8 amd_ucode_patch[MAX_NUMNODES][PATCH_MAX_SIZE];
60
61 /*
62  * Microcode patch container file is prepended to the initrd in cpio
63  * format. See Documentation/x86/microcode.txt
64  */
65 static const char
66 ucode_path[] __maybe_unused = "/*(DEBLOBBED)*/";
67
68 static u16 find_equiv_id(struct equiv_cpu_entry *equiv_table, u32 sig)
69 {
70         for (; equiv_table && equiv_table->installed_cpu; equiv_table++) {
71                 if (sig == equiv_table->installed_cpu)
72                         return equiv_table->equiv_cpu;
73         }
74
75         return 0;
76 }
77
78 /*
79  * This scans the ucode blob for the proper container as we can have multiple
80  * containers glued together. Returns the equivalence ID from the equivalence
81  * table or 0 if none found.
82  * Returns the amount of bytes consumed while scanning. @desc contains all the
83  * data we're going to use in later stages of the application.
84  */
85 static ssize_t parse_container(u8 *ucode, ssize_t size, struct cont_desc *desc)
86 {
87         struct equiv_cpu_entry *eq;
88         ssize_t orig_size = size;
89         u32 *hdr = (u32 *)ucode;
90         u16 eq_id;
91         u8 *buf;
92
93         /* Am I looking at an equivalence table header? */
94         if (hdr[0] != UCODE_MAGIC ||
95             hdr[1] != UCODE_EQUIV_CPU_TABLE_TYPE ||
96             hdr[2] == 0)
97                 return CONTAINER_HDR_SZ;
98
99         buf = ucode;
100
101         eq = (struct equiv_cpu_entry *)(buf + CONTAINER_HDR_SZ);
102
103         /* Find the equivalence ID of our CPU in this table: */
104         eq_id = find_equiv_id(eq, desc->cpuid_1_eax);
105
106         buf  += hdr[2] + CONTAINER_HDR_SZ;
107         size -= hdr[2] + CONTAINER_HDR_SZ;
108
109         /*
110          * Scan through the rest of the container to find where it ends. We do
111          * some basic sanity-checking too.
112          */
113         while (size > 0) {
114                 struct microcode_amd *mc;
115                 u32 patch_size;
116
117                 hdr = (u32 *)buf;
118
119                 if (hdr[0] != UCODE_UCODE_TYPE)
120                         break;
121
122                 /* Sanity-check patch size. */
123                 patch_size = hdr[1];
124                 if (patch_size > PATCH_MAX_SIZE)
125                         break;
126
127                 /* Skip patch section header: */
128                 buf  += SECTION_HDR_SIZE;
129                 size -= SECTION_HDR_SIZE;
130
131                 mc = (struct microcode_amd *)buf;
132                 if (eq_id == mc->hdr.processor_rev_id) {
133                         desc->psize = patch_size;
134                         desc->mc = mc;
135                 }
136
137                 buf  += patch_size;
138                 size -= patch_size;
139         }
140
141         /*
142          * If we have found a patch (desc->mc), it means we're looking at the
143          * container which has a patch for this CPU so return 0 to mean, @ucode
144          * already points to the proper container. Otherwise, we return the size
145          * we scanned so that we can advance to the next container in the
146          * buffer.
147          */
148         if (desc->mc) {
149                 desc->data = ucode;
150                 desc->size = orig_size - size;
151
152                 return 0;
153         }
154
155         return orig_size - size;
156 }
157
158 /*
159  * Scan the ucode blob for the proper container as we can have multiple
160  * containers glued together.
161  */
162 static void scan_containers(u8 *ucode, size_t size, struct cont_desc *desc)
163 {
164         ssize_t rem = size;
165
166         while (rem >= 0) {
167                 ssize_t s = parse_container(ucode, rem, desc);
168                 if (!s)
169                         return;
170
171                 ucode += s;
172                 rem   -= s;
173         }
174 }
175
176 static int __apply_microcode_amd(struct microcode_amd *mc)
177 {
178         u32 rev, dummy;
179
180         native_wrmsrl(MSR_AMD64_PATCH_LOADER, (u64)(long)&mc->hdr.data_code);
181
182         /* verify patch application was successful */
183         native_rdmsr(MSR_AMD64_PATCH_LEVEL, rev, dummy);
184         if (rev != mc->hdr.patch_id)
185                 return -1;
186
187         return 0;
188 }
189
190 /*
191  * Early load occurs before we can vmalloc(). So we look for the microcode
192  * patch container file in initrd, traverse equivalent cpu table, look for a
193  * matching microcode patch, and update, all in initrd memory in place.
194  * When vmalloc() is available for use later -- on 64-bit during first AP load,
195  * and on 32-bit during save_microcode_in_initrd_amd() -- we can call
196  * load_microcode_amd() to save equivalent cpu table and microcode patches in
197  * kernel heap memory.
198  *
199  * Returns true if container found (sets @desc), false otherwise.
200  */
201 static bool
202 apply_microcode_early_amd(u32 cpuid_1_eax, void *ucode, size_t size, bool save_patch)
203 {
204         struct cont_desc desc = { 0 };
205         u8 (*patch)[PATCH_MAX_SIZE];
206         struct microcode_amd *mc;
207         u32 rev, dummy, *new_rev;
208         bool ret = false;
209
210 #ifdef CONFIG_X86_32
211         new_rev = (u32 *)__pa_nodebug(&ucode_new_rev);
212         patch   = (u8 (*)[PATCH_MAX_SIZE])__pa_nodebug(&amd_ucode_patch);
213 #else
214         new_rev = &ucode_new_rev;
215         patch   = &amd_ucode_patch[0];
216 #endif
217
218         desc.cpuid_1_eax = cpuid_1_eax;
219
220         scan_containers(ucode, size, &desc);
221
222         mc = desc.mc;
223         if (!mc)
224                 return ret;
225
226         native_rdmsr(MSR_AMD64_PATCH_LEVEL, rev, dummy);
227
228         /*
229          * Allow application of the same revision to pick up SMT-specific
230          * changes even if the revision of the other SMT thread is already
231          * up-to-date.
232          */
233         if (rev > mc->hdr.patch_id)
234                 return ret;
235
236         if (!__apply_microcode_amd(mc)) {
237                 *new_rev = mc->hdr.patch_id;
238                 ret      = true;
239
240                 if (save_patch)
241                         memcpy(patch, mc, min_t(u32, desc.psize, PATCH_MAX_SIZE));
242         }
243
244         return ret;
245 }
246
247 static bool get_builtin_microcode(struct cpio_data *cp, unsigned int family)
248 {
249 #ifdef CONFIG_X86_64
250         char fw_name[36] = "/*(DEBLOBBED)*/";
251
252         if (family >= 0x15)
253                 snprintf(fw_name, sizeof(fw_name),
254                          "/*(DEBLOBBED)*/", family);
255
256         return get_builtin_firmware(cp, fw_name);
257 #else
258         return false;
259 #endif
260 }
261
262 static void __load_ucode_amd(unsigned int cpuid_1_eax, struct cpio_data *ret)
263 {
264         struct ucode_cpu_info *uci;
265         struct cpio_data cp;
266         const char *path;
267         bool use_pa;
268
269         if (IS_ENABLED(CONFIG_X86_32)) {
270                 uci     = (struct ucode_cpu_info *)__pa_nodebug(ucode_cpu_info);
271                 path    = (const char *)__pa_nodebug(ucode_path);
272                 use_pa  = true;
273         } else {
274                 uci     = ucode_cpu_info;
275                 path    = ucode_path;
276                 use_pa  = false;
277         }
278
279         if (!get_builtin_microcode(&cp, x86_family(cpuid_1_eax)))
280                 cp = find_microcode_in_initrd(path, use_pa);
281
282         /* Needed in load_microcode_amd() */
283         uci->cpu_sig.sig = cpuid_1_eax;
284
285         *ret = cp;
286 }
287
288 void __init load_ucode_amd_bsp(unsigned int cpuid_1_eax)
289 {
290         struct cpio_data cp = { };
291
292         __load_ucode_amd(cpuid_1_eax, &cp);
293         if (!(cp.data && cp.size))
294                 return;
295
296         apply_microcode_early_amd(cpuid_1_eax, cp.data, cp.size, true);
297 }
298
299 void load_ucode_amd_ap(unsigned int cpuid_1_eax)
300 {
301         struct microcode_amd *mc;
302         struct cpio_data cp;
303         u32 *new_rev, rev, dummy;
304
305         if (IS_ENABLED(CONFIG_X86_32)) {
306                 mc      = (struct microcode_amd *)__pa_nodebug(amd_ucode_patch);
307                 new_rev = (u32 *)__pa_nodebug(&ucode_new_rev);
308         } else {
309                 mc      = (struct microcode_amd *)amd_ucode_patch;
310                 new_rev = &ucode_new_rev;
311         }
312
313         native_rdmsr(MSR_AMD64_PATCH_LEVEL, rev, dummy);
314
315         /*
316          * Check whether a new patch has been saved already. Also, allow application of
317          * the same revision in order to pick up SMT-thread-specific configuration even
318          * if the sibling SMT thread already has an up-to-date revision.
319          */
320         if (*new_rev && rev <= mc->hdr.patch_id) {
321                 if (!__apply_microcode_amd(mc)) {
322                         *new_rev = mc->hdr.patch_id;
323                         return;
324                 }
325         }
326
327         __load_ucode_amd(cpuid_1_eax, &cp);
328         if (!(cp.data && cp.size))
329                 return;
330
331         apply_microcode_early_amd(cpuid_1_eax, cp.data, cp.size, false);
332 }
333
334 static enum ucode_state load_microcode_amd(u8 family, const u8 *data, size_t size);
335
336 int __init save_microcode_in_initrd_amd(unsigned int cpuid_1_eax)
337 {
338         struct cont_desc desc = { 0 };
339         enum ucode_state ret;
340         struct cpio_data cp;
341
342         cp = find_microcode_in_initrd(ucode_path, false);
343         if (!(cp.data && cp.size))
344                 return -EINVAL;
345
346         desc.cpuid_1_eax = cpuid_1_eax;
347
348         scan_containers(cp.data, cp.size, &desc);
349         if (!desc.mc)
350                 return -EINVAL;
351
352         ret = load_microcode_amd(x86_family(cpuid_1_eax), desc.data, desc.size);
353         if (ret > UCODE_UPDATED)
354                 return -EINVAL;
355
356         return 0;
357 }
358
359 void reload_ucode_amd(unsigned int cpu)
360 {
361         u32 rev, dummy;
362         struct microcode_amd *mc;
363
364         mc = (struct microcode_amd *)amd_ucode_patch[cpu_to_node(cpu)];
365
366         rdmsr(MSR_AMD64_PATCH_LEVEL, rev, dummy);
367
368         if (rev < mc->hdr.patch_id) {
369                 if (!__apply_microcode_amd(mc)) {
370                         ucode_new_rev = mc->hdr.patch_id;
371                         pr_info("reload patch_level=0x%08x\n", ucode_new_rev);
372                 }
373         }
374 }
375 static u16 __find_equiv_id(unsigned int cpu)
376 {
377         struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
378         return find_equiv_id(equiv_cpu_table, uci->cpu_sig.sig);
379 }
380
381 static u32 find_cpu_family_by_equiv_cpu(u16 equiv_cpu)
382 {
383         int i = 0;
384
385         BUG_ON(!equiv_cpu_table);
386
387         while (equiv_cpu_table[i].equiv_cpu != 0) {
388                 if (equiv_cpu == equiv_cpu_table[i].equiv_cpu)
389                         return equiv_cpu_table[i].installed_cpu;
390                 i++;
391         }
392         return 0;
393 }
394
395 /*
396  * a small, trivial cache of per-family ucode patches
397  */
398 static struct ucode_patch *cache_find_patch(u16 equiv_cpu)
399 {
400         struct ucode_patch *p;
401
402         list_for_each_entry(p, &microcode_cache, plist)
403                 if (p->equiv_cpu == equiv_cpu)
404                         return p;
405         return NULL;
406 }
407
408 static void update_cache(struct ucode_patch *new_patch)
409 {
410         struct ucode_patch *p;
411
412         list_for_each_entry(p, &microcode_cache, plist) {
413                 if (p->equiv_cpu == new_patch->equiv_cpu) {
414                         if (p->patch_id >= new_patch->patch_id) {
415                                 /* we already have the latest patch */
416                                 kfree(new_patch->data);
417                                 kfree(new_patch);
418                                 return;
419                         }
420
421                         list_replace(&p->plist, &new_patch->plist);
422                         kfree(p->data);
423                         kfree(p);
424                         return;
425                 }
426         }
427         /* no patch found, add it */
428         list_add_tail(&new_patch->plist, &microcode_cache);
429 }
430
431 static void free_cache(void)
432 {
433         struct ucode_patch *p, *tmp;
434
435         list_for_each_entry_safe(p, tmp, &microcode_cache, plist) {
436                 __list_del(p->plist.prev, p->plist.next);
437                 kfree(p->data);
438                 kfree(p);
439         }
440 }
441
442 static struct ucode_patch *find_patch(unsigned int cpu)
443 {
444         u16 equiv_id;
445
446         equiv_id = __find_equiv_id(cpu);
447         if (!equiv_id)
448                 return NULL;
449
450         return cache_find_patch(equiv_id);
451 }
452
453 static int collect_cpu_info_amd(int cpu, struct cpu_signature *csig)
454 {
455         struct cpuinfo_x86 *c = &cpu_data(cpu);
456         struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
457         struct ucode_patch *p;
458
459         csig->sig = cpuid_eax(0x00000001);
460         csig->rev = c->microcode;
461
462         /*
463          * a patch could have been loaded early, set uci->mc so that
464          * mc_bp_resume() can call apply_microcode()
465          */
466         p = find_patch(cpu);
467         if (p && (p->patch_id == csig->rev))
468                 uci->mc = p->data;
469
470         pr_info("CPU%d: patch_level=0x%08x\n", cpu, csig->rev);
471
472         return 0;
473 }
474
475 static unsigned int verify_patch_size(u8 family, u32 patch_size,
476                                       unsigned int size)
477 {
478         u32 max_size;
479
480 #define F1XH_MPB_MAX_SIZE 2048
481 #define F14H_MPB_MAX_SIZE 1824
482 #define F15H_MPB_MAX_SIZE 4096
483 #define F16H_MPB_MAX_SIZE 3458
484 #define F17H_MPB_MAX_SIZE 3200
485
486         switch (family) {
487         case 0x14:
488                 max_size = F14H_MPB_MAX_SIZE;
489                 break;
490         case 0x15:
491                 max_size = F15H_MPB_MAX_SIZE;
492                 break;
493         case 0x16:
494                 max_size = F16H_MPB_MAX_SIZE;
495                 break;
496         case 0x17:
497                 max_size = F17H_MPB_MAX_SIZE;
498                 break;
499         default:
500                 max_size = F1XH_MPB_MAX_SIZE;
501                 break;
502         }
503
504         if (patch_size > min_t(u32, size, max_size)) {
505                 pr_err("patch size mismatch\n");
506                 return 0;
507         }
508
509         return patch_size;
510 }
511
512 static enum ucode_state apply_microcode_amd(int cpu)
513 {
514         struct cpuinfo_x86 *c = &cpu_data(cpu);
515         struct microcode_amd *mc_amd;
516         struct ucode_cpu_info *uci;
517         struct ucode_patch *p;
518         enum ucode_state ret;
519         u32 rev, dummy;
520
521         BUG_ON(raw_smp_processor_id() != cpu);
522
523         uci = ucode_cpu_info + cpu;
524
525         p = find_patch(cpu);
526         if (!p)
527                 return UCODE_NFOUND;
528
529         mc_amd  = p->data;
530         uci->mc = p->data;
531
532         rdmsr(MSR_AMD64_PATCH_LEVEL, rev, dummy);
533
534         /* need to apply patch? */
535         if (rev >= mc_amd->hdr.patch_id) {
536                 ret = UCODE_OK;
537                 goto out;
538         }
539
540         if (__apply_microcode_amd(mc_amd)) {
541                 pr_err("CPU%d: update failed for patch_level=0x%08x\n",
542                         cpu, mc_amd->hdr.patch_id);
543                 return UCODE_ERROR;
544         }
545
546         rev = mc_amd->hdr.patch_id;
547         ret = UCODE_UPDATED;
548
549         pr_info("CPU%d: new patch_level=0x%08x\n", cpu, rev);
550
551 out:
552         uci->cpu_sig.rev = rev;
553         c->microcode     = rev;
554
555         /* Update boot_cpu_data's revision too, if we're on the BSP: */
556         if (c->cpu_index == boot_cpu_data.cpu_index)
557                 boot_cpu_data.microcode = rev;
558
559         return ret;
560 }
561
562 static int install_equiv_cpu_table(const u8 *buf)
563 {
564         unsigned int *ibuf = (unsigned int *)buf;
565         unsigned int type = ibuf[1];
566         unsigned int size = ibuf[2];
567
568         if (type != UCODE_EQUIV_CPU_TABLE_TYPE || !size) {
569                 pr_err("empty section/"
570                        "invalid type field in container file section header\n");
571                 return -EINVAL;
572         }
573
574         equiv_cpu_table = vmalloc(size);
575         if (!equiv_cpu_table) {
576                 pr_err("failed to allocate equivalent CPU table\n");
577                 return -ENOMEM;
578         }
579
580         memcpy(equiv_cpu_table, buf + CONTAINER_HDR_SZ, size);
581
582         /* add header length */
583         return size + CONTAINER_HDR_SZ;
584 }
585
586 static void free_equiv_cpu_table(void)
587 {
588         vfree(equiv_cpu_table);
589         equiv_cpu_table = NULL;
590 }
591
592 static void cleanup(void)
593 {
594         free_equiv_cpu_table();
595         free_cache();
596 }
597
598 /*
599  * We return the current size even if some of the checks failed so that
600  * we can skip over the next patch. If we return a negative value, we
601  * signal a grave error like a memory allocation has failed and the
602  * driver cannot continue functioning normally. In such cases, we tear
603  * down everything we've used up so far and exit.
604  */
605 static int verify_and_add_patch(u8 family, u8 *fw, unsigned int leftover)
606 {
607         struct microcode_header_amd *mc_hdr;
608         struct ucode_patch *patch;
609         unsigned int patch_size, crnt_size, ret;
610         u32 proc_fam;
611         u16 proc_id;
612
613         patch_size  = *(u32 *)(fw + 4);
614         crnt_size   = patch_size + SECTION_HDR_SIZE;
615         mc_hdr      = (struct microcode_header_amd *)(fw + SECTION_HDR_SIZE);
616         proc_id     = mc_hdr->processor_rev_id;
617
618         proc_fam = find_cpu_family_by_equiv_cpu(proc_id);
619         if (!proc_fam) {
620                 pr_err("No patch family for equiv ID: 0x%04x\n", proc_id);
621                 return crnt_size;
622         }
623
624         /* check if patch is for the current family */
625         proc_fam = ((proc_fam >> 8) & 0xf) + ((proc_fam >> 20) & 0xff);
626         if (proc_fam != family)
627                 return crnt_size;
628
629         if (mc_hdr->nb_dev_id || mc_hdr->sb_dev_id) {
630                 pr_err("Patch-ID 0x%08x: chipset-specific code unsupported.\n",
631                         mc_hdr->patch_id);
632                 return crnt_size;
633         }
634
635         ret = verify_patch_size(family, patch_size, leftover);
636         if (!ret) {
637                 pr_err("Patch-ID 0x%08x: size mismatch.\n", mc_hdr->patch_id);
638                 return crnt_size;
639         }
640
641         patch = kzalloc(sizeof(*patch), GFP_KERNEL);
642         if (!patch) {
643                 pr_err("Patch allocation failure.\n");
644                 return -EINVAL;
645         }
646
647         patch->data = kmemdup(fw + SECTION_HDR_SIZE, patch_size, GFP_KERNEL);
648         if (!patch->data) {
649                 pr_err("Patch data allocation failure.\n");
650                 kfree(patch);
651                 return -EINVAL;
652         }
653
654         INIT_LIST_HEAD(&patch->plist);
655         patch->patch_id  = mc_hdr->patch_id;
656         patch->equiv_cpu = proc_id;
657
658         pr_debug("%s: Added patch_id: 0x%08x, proc_id: 0x%04x\n",
659                  __func__, patch->patch_id, proc_id);
660
661         /* ... and add to cache. */
662         update_cache(patch);
663
664         return crnt_size;
665 }
666
667 static enum ucode_state __load_microcode_amd(u8 family, const u8 *data,
668                                              size_t size)
669 {
670         enum ucode_state ret = UCODE_ERROR;
671         unsigned int leftover;
672         u8 *fw = (u8 *)data;
673         int crnt_size = 0;
674         int offset;
675
676         offset = install_equiv_cpu_table(data);
677         if (offset < 0) {
678                 pr_err("failed to create equivalent cpu table\n");
679                 return ret;
680         }
681         fw += offset;
682         leftover = size - offset;
683
684         if (*(u32 *)fw != UCODE_UCODE_TYPE) {
685                 pr_err("invalid type field in container file section header\n");
686                 free_equiv_cpu_table();
687                 return ret;
688         }
689
690         while (leftover) {
691                 crnt_size = verify_and_add_patch(family, fw, leftover);
692                 if (crnt_size < 0)
693                         return ret;
694
695                 fw       += crnt_size;
696                 leftover -= crnt_size;
697         }
698
699         return UCODE_OK;
700 }
701
702 static enum ucode_state load_microcode_amd(u8 family, const u8 *data, size_t size)
703 {
704         struct cpuinfo_x86 *c;
705         unsigned int nid, cpu;
706         struct ucode_patch *p;
707         enum ucode_state ret;
708
709         /* free old equiv table */
710         free_equiv_cpu_table();
711
712         ret = __load_microcode_amd(family, data, size);
713         if (ret != UCODE_OK) {
714                 cleanup();
715                 return ret;
716         }
717
718         for_each_node(nid) {
719                 cpu = cpumask_first(cpumask_of_node(nid));
720                 c = &cpu_data(cpu);
721
722                 p = find_patch(cpu);
723                 if (!p)
724                         continue;
725
726                 if (c->microcode >= p->patch_id)
727                         continue;
728
729                 ret = UCODE_NEW;
730
731                 memset(&amd_ucode_patch[nid], 0, PATCH_MAX_SIZE);
732                 memcpy(&amd_ucode_patch[nid], p->data, min_t(u32, ksize(p->data), PATCH_MAX_SIZE));
733         }
734
735         return ret;
736 }
737
738 /*(DEBLOBBED)*/
739 static enum ucode_state request_microcode_amd(int cpu, struct device *device,
740                                               bool refresh_fw)
741 {
742         char fw_name[36] = "/*(DEBLOBBED)*/";
743         struct cpuinfo_x86 *c = &cpu_data(cpu);
744         enum ucode_state ret = UCODE_NFOUND;
745         const struct firmware *fw;
746
747         /* reload ucode container only on the boot cpu */
748         if (!refresh_fw)
749                 return UCODE_OK;
750
751         if (c->x86 >= 0x15)
752                 snprintf(fw_name, sizeof(fw_name), "/*(DEBLOBBED)*/", c->x86);
753
754         if (reject_firmware_direct(&fw, (const char *)fw_name, device)) {
755                 pr_debug("failed to load file %s\n", fw_name);
756                 goto out;
757         }
758
759         ret = UCODE_ERROR;
760         if (*(u32 *)fw->data != UCODE_MAGIC) {
761                 pr_err("invalid magic value (0x%08x)\n", *(u32 *)fw->data);
762                 goto fw_release;
763         }
764
765         ret = load_microcode_amd(c->x86, fw->data, fw->size);
766
767  fw_release:
768         release_firmware(fw);
769
770  out:
771         return ret;
772 }
773
774 static enum ucode_state
775 request_microcode_user(int cpu, const void __user *buf, size_t size)
776 {
777         return UCODE_ERROR;
778 }
779
780 static void microcode_fini_cpu_amd(int cpu)
781 {
782         struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
783
784         uci->mc = NULL;
785 }
786
787 static struct microcode_ops microcode_amd_ops = {
788         .request_microcode_user           = request_microcode_user,
789         .request_microcode_fw             = request_microcode_amd,
790         .collect_cpu_info                 = collect_cpu_info_amd,
791         .apply_microcode                  = apply_microcode_amd,
792         .microcode_fini_cpu               = microcode_fini_cpu_amd,
793 };
794
795 struct microcode_ops * __init init_amd_microcode(void)
796 {
797         struct cpuinfo_x86 *c = &boot_cpu_data;
798
799         if (c->x86_vendor != X86_VENDOR_AMD || c->x86 < 0x10) {
800                 pr_warn("AMD CPU family 0x%x not supported\n", c->x86);
801                 return NULL;
802         }
803
804         if (ucode_new_rev)
805                 pr_info_once("microcode updated early to new patch_level=0x%08x\n",
806                              ucode_new_rev);
807
808         return &microcode_amd_ops;
809 }
810
811 void __exit exit_amd_microcode(void)
812 {
813         cleanup();
814 }