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