GNU Linux-libre 4.14.266-gnu1
[releases.git] / arch / x86 / kernel / cpu / microcode / intel.c
1 /*
2  * Intel CPU Microcode Update Driver for Linux
3  *
4  * Copyright (C) 2000-2006 Tigran Aivazian <aivazian.tigran@gmail.com>
5  *               2006 Shaohua Li <shaohua.li@intel.com>
6  *
7  * Intel CPU microcode early update for Linux
8  *
9  * Copyright (C) 2012 Fenghua Yu <fenghua.yu@intel.com>
10  *                    H Peter Anvin" <hpa@zytor.com>
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version
15  * 2 of the License, or (at your option) any later version.
16  */
17
18 /*
19  * This needs to be before all headers so that pr_debug in printk.h doesn't turn
20  * printk calls into no_printk().
21  *
22  *#define DEBUG
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/slab.h>
33 #include <linux/cpu.h>
34 #include <linux/mm.h>
35
36 #include <asm/microcode_intel.h>
37 #include <asm/intel-family.h>
38 #include <asm/processor.h>
39 #include <asm/tlbflush.h>
40 #include <asm/setup.h>
41 #include <asm/msr.h>
42
43 static const char ucode_path[] = "/*(DEBLOBBED)*/";
44
45 /* Current microcode patch used in early patching on the APs. */
46 static struct microcode_intel *intel_ucode_patch;
47
48 /* last level cache size per core */
49 static int llc_size_per_core;
50
51 static inline bool cpu_signatures_match(unsigned int s1, unsigned int p1,
52                                         unsigned int s2, unsigned int p2)
53 {
54         if (s1 != s2)
55                 return false;
56
57         /* Processor flags are either both 0 ... */
58         if (!p1 && !p2)
59                 return true;
60
61         /* ... or they intersect. */
62         return p1 & p2;
63 }
64
65 /*
66  * Returns 1 if update has been found, 0 otherwise.
67  */
68 static int find_matching_signature(void *mc, unsigned int csig, int cpf)
69 {
70         struct microcode_header_intel *mc_hdr = mc;
71         struct extended_sigtable *ext_hdr;
72         struct extended_signature *ext_sig;
73         int i;
74
75         if (cpu_signatures_match(csig, cpf, mc_hdr->sig, mc_hdr->pf))
76                 return 1;
77
78         /* Look for ext. headers: */
79         if (get_totalsize(mc_hdr) <= get_datasize(mc_hdr) + MC_HEADER_SIZE)
80                 return 0;
81
82         ext_hdr = mc + get_datasize(mc_hdr) + MC_HEADER_SIZE;
83         ext_sig = (void *)ext_hdr + EXT_HEADER_SIZE;
84
85         for (i = 0; i < ext_hdr->count; i++) {
86                 if (cpu_signatures_match(csig, cpf, ext_sig->sig, ext_sig->pf))
87                         return 1;
88                 ext_sig++;
89         }
90         return 0;
91 }
92
93 /*
94  * Returns 1 if update has been found, 0 otherwise.
95  */
96 static int has_newer_microcode(void *mc, unsigned int csig, int cpf, int new_rev)
97 {
98         struct microcode_header_intel *mc_hdr = mc;
99
100         if (mc_hdr->rev <= new_rev)
101                 return 0;
102
103         return find_matching_signature(mc, csig, cpf);
104 }
105
106 static struct ucode_patch *memdup_patch(void *data, unsigned int size)
107 {
108         struct ucode_patch *p;
109
110         p = kzalloc(sizeof(struct ucode_patch), GFP_KERNEL);
111         if (!p)
112                 return NULL;
113
114         p->data = kmemdup(data, size, GFP_KERNEL);
115         if (!p->data) {
116                 kfree(p);
117                 return NULL;
118         }
119
120         return p;
121 }
122
123 static void save_microcode_patch(struct ucode_cpu_info *uci, void *data, unsigned int size)
124 {
125         struct microcode_header_intel *mc_hdr, *mc_saved_hdr;
126         struct ucode_patch *iter, *tmp, *p = NULL;
127         bool prev_found = false;
128         unsigned int sig, pf;
129
130         mc_hdr = (struct microcode_header_intel *)data;
131
132         list_for_each_entry_safe(iter, tmp, &microcode_cache, plist) {
133                 mc_saved_hdr = (struct microcode_header_intel *)iter->data;
134                 sig          = mc_saved_hdr->sig;
135                 pf           = mc_saved_hdr->pf;
136
137                 if (find_matching_signature(data, sig, pf)) {
138                         prev_found = true;
139
140                         if (mc_hdr->rev <= mc_saved_hdr->rev)
141                                 continue;
142
143                         p = memdup_patch(data, size);
144                         if (!p)
145                                 pr_err("Error allocating buffer %p\n", data);
146                         else {
147                                 list_replace(&iter->plist, &p->plist);
148                                 kfree(iter->data);
149                                 kfree(iter);
150                         }
151                 }
152         }
153
154         /*
155          * There weren't any previous patches found in the list cache; save the
156          * newly found.
157          */
158         if (!prev_found) {
159                 p = memdup_patch(data, size);
160                 if (!p)
161                         pr_err("Error allocating buffer for %p\n", data);
162                 else
163                         list_add_tail(&p->plist, &microcode_cache);
164         }
165
166         if (!p)
167                 return;
168
169         if (!find_matching_signature(p->data, uci->cpu_sig.sig, uci->cpu_sig.pf))
170                 return;
171
172         /*
173          * Save for early loading. On 32-bit, that needs to be a physical
174          * address as the APs are running from physical addresses, before
175          * paging has been enabled.
176          */
177         if (IS_ENABLED(CONFIG_X86_32))
178                 intel_ucode_patch = (struct microcode_intel *)__pa_nodebug(p->data);
179         else
180                 intel_ucode_patch = p->data;
181 }
182
183 static int microcode_sanity_check(void *mc, int print_err)
184 {
185         unsigned long total_size, data_size, ext_table_size;
186         struct microcode_header_intel *mc_header = mc;
187         struct extended_sigtable *ext_header = NULL;
188         u32 sum, orig_sum, ext_sigcount = 0, i;
189         struct extended_signature *ext_sig;
190
191         total_size = get_totalsize(mc_header);
192         data_size = get_datasize(mc_header);
193
194         if (data_size + MC_HEADER_SIZE > total_size) {
195                 if (print_err)
196                         pr_err("Error: bad microcode data file size.\n");
197                 return -EINVAL;
198         }
199
200         if (mc_header->ldrver != 1 || mc_header->hdrver != 1) {
201                 if (print_err)
202                         pr_err("Error: invalid/unknown microcode update format.\n");
203                 return -EINVAL;
204         }
205
206         ext_table_size = total_size - (MC_HEADER_SIZE + data_size);
207         if (ext_table_size) {
208                 u32 ext_table_sum = 0;
209                 u32 *ext_tablep;
210
211                 if ((ext_table_size < EXT_HEADER_SIZE)
212                  || ((ext_table_size - EXT_HEADER_SIZE) % EXT_SIGNATURE_SIZE)) {
213                         if (print_err)
214                                 pr_err("Error: truncated extended signature table.\n");
215                         return -EINVAL;
216                 }
217
218                 ext_header = mc + MC_HEADER_SIZE + data_size;
219                 if (ext_table_size != exttable_size(ext_header)) {
220                         if (print_err)
221                                 pr_err("Error: extended signature table size mismatch.\n");
222                         return -EFAULT;
223                 }
224
225                 ext_sigcount = ext_header->count;
226
227                 /*
228                  * Check extended table checksum: the sum of all dwords that
229                  * comprise a valid table must be 0.
230                  */
231                 ext_tablep = (u32 *)ext_header;
232
233                 i = ext_table_size / sizeof(u32);
234                 while (i--)
235                         ext_table_sum += ext_tablep[i];
236
237                 if (ext_table_sum) {
238                         if (print_err)
239                                 pr_warn("Bad extended signature table checksum, aborting.\n");
240                         return -EINVAL;
241                 }
242         }
243
244         /*
245          * Calculate the checksum of update data and header. The checksum of
246          * valid update data and header including the extended signature table
247          * must be 0.
248          */
249         orig_sum = 0;
250         i = (MC_HEADER_SIZE + data_size) / sizeof(u32);
251         while (i--)
252                 orig_sum += ((u32 *)mc)[i];
253
254         if (orig_sum) {
255                 if (print_err)
256                         pr_err("Bad microcode data checksum, aborting.\n");
257                 return -EINVAL;
258         }
259
260         if (!ext_table_size)
261                 return 0;
262
263         /*
264          * Check extended signature checksum: 0 => valid.
265          */
266         for (i = 0; i < ext_sigcount; i++) {
267                 ext_sig = (void *)ext_header + EXT_HEADER_SIZE +
268                           EXT_SIGNATURE_SIZE * i;
269
270                 sum = (mc_header->sig + mc_header->pf + mc_header->cksum) -
271                       (ext_sig->sig + ext_sig->pf + ext_sig->cksum);
272                 if (sum) {
273                         if (print_err)
274                                 pr_err("Bad extended signature checksum, aborting.\n");
275                         return -EINVAL;
276                 }
277         }
278         return 0;
279 }
280
281 /*
282  * Get microcode matching with BSP's model. Only CPUs with the same model as
283  * BSP can stay in the platform.
284  */
285 static struct microcode_intel *
286 scan_microcode(void *data, size_t size, struct ucode_cpu_info *uci, bool save)
287 {
288         struct microcode_header_intel *mc_header;
289         struct microcode_intel *patch = NULL;
290         unsigned int mc_size;
291
292         while (size) {
293                 if (size < sizeof(struct microcode_header_intel))
294                         break;
295
296                 mc_header = (struct microcode_header_intel *)data;
297
298                 mc_size = get_totalsize(mc_header);
299                 if (!mc_size ||
300                     mc_size > size ||
301                     microcode_sanity_check(data, 0) < 0)
302                         break;
303
304                 size -= mc_size;
305
306                 if (!find_matching_signature(data, uci->cpu_sig.sig,
307                                              uci->cpu_sig.pf)) {
308                         data += mc_size;
309                         continue;
310                 }
311
312                 if (save) {
313                         save_microcode_patch(uci, data, mc_size);
314                         goto next;
315                 }
316
317
318                 if (!patch) {
319                         if (!has_newer_microcode(data,
320                                                  uci->cpu_sig.sig,
321                                                  uci->cpu_sig.pf,
322                                                  uci->cpu_sig.rev))
323                                 goto next;
324
325                 } else {
326                         struct microcode_header_intel *phdr = &patch->hdr;
327
328                         if (!has_newer_microcode(data,
329                                                  phdr->sig,
330                                                  phdr->pf,
331                                                  phdr->rev))
332                                 goto next;
333                 }
334
335                 /* We have a newer patch, save it. */
336                 patch = data;
337
338 next:
339                 data += mc_size;
340         }
341
342         if (size)
343                 return NULL;
344
345         return patch;
346 }
347
348 static int collect_cpu_info_early(struct ucode_cpu_info *uci)
349 {
350         unsigned int val[2];
351         unsigned int family, model;
352         struct cpu_signature csig = { 0 };
353         unsigned int eax, ebx, ecx, edx;
354
355         memset(uci, 0, sizeof(*uci));
356
357         eax = 0x00000001;
358         ecx = 0;
359         native_cpuid(&eax, &ebx, &ecx, &edx);
360         csig.sig = eax;
361
362         family = x86_family(eax);
363         model  = x86_model(eax);
364
365         if ((model >= 5) || (family > 6)) {
366                 /* get processor flags from MSR 0x17 */
367                 native_rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]);
368                 csig.pf = 1 << ((val[1] >> 18) & 7);
369         }
370
371         csig.rev = intel_get_microcode_revision();
372
373         uci->cpu_sig = csig;
374         uci->valid = 1;
375
376         return 0;
377 }
378
379 static void show_saved_mc(void)
380 {
381 #ifdef DEBUG
382         int i = 0, j;
383         unsigned int sig, pf, rev, total_size, data_size, date;
384         struct ucode_cpu_info uci;
385         struct ucode_patch *p;
386
387         if (list_empty(&microcode_cache)) {
388                 pr_debug("no microcode data saved.\n");
389                 return;
390         }
391
392         collect_cpu_info_early(&uci);
393
394         sig     = uci.cpu_sig.sig;
395         pf      = uci.cpu_sig.pf;
396         rev     = uci.cpu_sig.rev;
397         pr_debug("CPU: sig=0x%x, pf=0x%x, rev=0x%x\n", sig, pf, rev);
398
399         list_for_each_entry(p, &microcode_cache, plist) {
400                 struct microcode_header_intel *mc_saved_header;
401                 struct extended_sigtable *ext_header;
402                 struct extended_signature *ext_sig;
403                 int ext_sigcount;
404
405                 mc_saved_header = (struct microcode_header_intel *)p->data;
406
407                 sig     = mc_saved_header->sig;
408                 pf      = mc_saved_header->pf;
409                 rev     = mc_saved_header->rev;
410                 date    = mc_saved_header->date;
411
412                 total_size      = get_totalsize(mc_saved_header);
413                 data_size       = get_datasize(mc_saved_header);
414
415                 pr_debug("mc_saved[%d]: sig=0x%x, pf=0x%x, rev=0x%x, total size=0x%x, date = %04x-%02x-%02x\n",
416                          i++, sig, pf, rev, total_size,
417                          date & 0xffff,
418                          date >> 24,
419                          (date >> 16) & 0xff);
420
421                 /* Look for ext. headers: */
422                 if (total_size <= data_size + MC_HEADER_SIZE)
423                         continue;
424
425                 ext_header = (void *)mc_saved_header + data_size + MC_HEADER_SIZE;
426                 ext_sigcount = ext_header->count;
427                 ext_sig = (void *)ext_header + EXT_HEADER_SIZE;
428
429                 for (j = 0; j < ext_sigcount; j++) {
430                         sig = ext_sig->sig;
431                         pf = ext_sig->pf;
432
433                         pr_debug("\tExtended[%d]: sig=0x%x, pf=0x%x\n",
434                                  j, sig, pf);
435
436                         ext_sig++;
437                 }
438         }
439 #endif
440 }
441
442 /*
443  * Save this microcode patch. It will be loaded early when a CPU is
444  * hot-added or resumes.
445  */
446 static void save_mc_for_early(struct ucode_cpu_info *uci, u8 *mc, unsigned int size)
447 {
448         /* Synchronization during CPU hotplug. */
449         static DEFINE_MUTEX(x86_cpu_microcode_mutex);
450
451         mutex_lock(&x86_cpu_microcode_mutex);
452
453         save_microcode_patch(uci, mc, size);
454         show_saved_mc();
455
456         mutex_unlock(&x86_cpu_microcode_mutex);
457 }
458
459 static bool load_builtin_intel_microcode(struct cpio_data *cp)
460 {
461         unsigned int eax = 1, ebx, ecx = 0, edx;
462         char name[30];
463
464         if (IS_ENABLED(CONFIG_X86_32))
465                 return false;
466
467         native_cpuid(&eax, &ebx, &ecx, &edx);
468
469         sprintf(name, "/*(DEBLOBBED)*/",
470                       x86_family(eax), x86_model(eax), x86_stepping(eax));
471
472         return get_builtin_firmware(cp, name);
473 }
474
475 /*
476  * Print ucode update info.
477  */
478 static void
479 print_ucode_info(struct ucode_cpu_info *uci, unsigned int date)
480 {
481         pr_info_once("microcode updated early to revision 0x%x, date = %04x-%02x-%02x\n",
482                      uci->cpu_sig.rev,
483                      date & 0xffff,
484                      date >> 24,
485                      (date >> 16) & 0xff);
486 }
487
488 #ifdef CONFIG_X86_32
489
490 static int delay_ucode_info;
491 static int current_mc_date;
492
493 /*
494  * Print early updated ucode info after printk works. This is delayed info dump.
495  */
496 void show_ucode_info_early(void)
497 {
498         struct ucode_cpu_info uci;
499
500         if (delay_ucode_info) {
501                 collect_cpu_info_early(&uci);
502                 print_ucode_info(&uci, current_mc_date);
503                 delay_ucode_info = 0;
504         }
505 }
506
507 /*
508  * At this point, we can not call printk() yet. Delay printing microcode info in
509  * show_ucode_info_early() until printk() works.
510  */
511 static void print_ucode(struct ucode_cpu_info *uci)
512 {
513         struct microcode_intel *mc;
514         int *delay_ucode_info_p;
515         int *current_mc_date_p;
516
517         mc = uci->mc;
518         if (!mc)
519                 return;
520
521         delay_ucode_info_p = (int *)__pa_nodebug(&delay_ucode_info);
522         current_mc_date_p = (int *)__pa_nodebug(&current_mc_date);
523
524         *delay_ucode_info_p = 1;
525         *current_mc_date_p = mc->hdr.date;
526 }
527 #else
528
529 static inline void print_ucode(struct ucode_cpu_info *uci)
530 {
531         struct microcode_intel *mc;
532
533         mc = uci->mc;
534         if (!mc)
535                 return;
536
537         print_ucode_info(uci, mc->hdr.date);
538 }
539 #endif
540
541 static int apply_microcode_early(struct ucode_cpu_info *uci, bool early)
542 {
543         struct microcode_intel *mc;
544         u32 rev;
545
546         mc = uci->mc;
547         if (!mc)
548                 return 0;
549
550         /*
551          * Save us the MSR write below - which is a particular expensive
552          * operation - when the other hyperthread has updated the microcode
553          * already.
554          */
555         rev = intel_get_microcode_revision();
556         if (rev >= mc->hdr.rev) {
557                 uci->cpu_sig.rev = rev;
558                 return UCODE_OK;
559         }
560
561         /*
562          * Writeback and invalidate caches before updating microcode to avoid
563          * internal issues depending on what the microcode is updating.
564          */
565         native_wbinvd();
566
567         /* write microcode via MSR 0x79 */
568         native_wrmsrl(MSR_IA32_UCODE_WRITE, (unsigned long)mc->bits);
569
570         rev = intel_get_microcode_revision();
571         if (rev != mc->hdr.rev)
572                 return -1;
573
574         uci->cpu_sig.rev = rev;
575
576         if (early)
577                 print_ucode(uci);
578         else
579                 print_ucode_info(uci, mc->hdr.date);
580
581         return 0;
582 }
583
584 int __init save_microcode_in_initrd_intel(void)
585 {
586         struct ucode_cpu_info uci;
587         struct cpio_data cp;
588
589         /*
590          * initrd is going away, clear patch ptr. We will scan the microcode one
591          * last time before jettisoning and save a patch, if found. Then we will
592          * update that pointer too, with a stable patch address to use when
593          * resuming the cores.
594          */
595         intel_ucode_patch = NULL;
596
597         if (!load_builtin_intel_microcode(&cp))
598                 cp = find_microcode_in_initrd(ucode_path, false);
599
600         if (!(cp.data && cp.size))
601                 return 0;
602
603         collect_cpu_info_early(&uci);
604
605         scan_microcode(cp.data, cp.size, &uci, true);
606
607         show_saved_mc();
608
609         return 0;
610 }
611
612 /*
613  * @res_patch, output: a pointer to the patch we found.
614  */
615 static struct microcode_intel *__load_ucode_intel(struct ucode_cpu_info *uci)
616 {
617         static const char *path;
618         struct cpio_data cp;
619         bool use_pa;
620
621         if (IS_ENABLED(CONFIG_X86_32)) {
622                 path      = (const char *)__pa_nodebug(ucode_path);
623                 use_pa    = true;
624         } else {
625                 path      = ucode_path;
626                 use_pa    = false;
627         }
628
629         /* try built-in microcode first */
630         if (!load_builtin_intel_microcode(&cp))
631                 cp = find_microcode_in_initrd(path, use_pa);
632
633         if (!(cp.data && cp.size))
634                 return NULL;
635
636         collect_cpu_info_early(uci);
637
638         return scan_microcode(cp.data, cp.size, uci, false);
639 }
640
641 void __init load_ucode_intel_bsp(void)
642 {
643         struct microcode_intel *patch;
644         struct ucode_cpu_info uci;
645
646         patch = __load_ucode_intel(&uci);
647         if (!patch)
648                 return;
649
650         uci.mc = patch;
651
652         apply_microcode_early(&uci, true);
653 }
654
655 void load_ucode_intel_ap(void)
656 {
657         struct microcode_intel *patch, **iup;
658         struct ucode_cpu_info uci;
659
660         if (IS_ENABLED(CONFIG_X86_32))
661                 iup = (struct microcode_intel **) __pa_nodebug(&intel_ucode_patch);
662         else
663                 iup = &intel_ucode_patch;
664
665 reget:
666         if (!*iup) {
667                 patch = __load_ucode_intel(&uci);
668                 if (!patch)
669                         return;
670
671                 *iup = patch;
672         }
673
674         uci.mc = *iup;
675
676         if (apply_microcode_early(&uci, true)) {
677                 /* Mixed-silicon system? Try to refetch the proper patch: */
678                 *iup = NULL;
679
680                 goto reget;
681         }
682 }
683
684 static struct microcode_intel *find_patch(struct ucode_cpu_info *uci)
685 {
686         struct microcode_header_intel *phdr;
687         struct ucode_patch *iter, *tmp;
688
689         list_for_each_entry_safe(iter, tmp, &microcode_cache, plist) {
690
691                 phdr = (struct microcode_header_intel *)iter->data;
692
693                 if (phdr->rev <= uci->cpu_sig.rev)
694                         continue;
695
696                 if (!find_matching_signature(phdr,
697                                              uci->cpu_sig.sig,
698                                              uci->cpu_sig.pf))
699                         continue;
700
701                 return iter->data;
702         }
703         return NULL;
704 }
705
706 void reload_ucode_intel(void)
707 {
708         struct microcode_intel *p;
709         struct ucode_cpu_info uci;
710
711         collect_cpu_info_early(&uci);
712
713         p = find_patch(&uci);
714         if (!p)
715                 return;
716
717         uci.mc = p;
718
719         apply_microcode_early(&uci, false);
720 }
721
722 static int collect_cpu_info(int cpu_num, struct cpu_signature *csig)
723 {
724         static struct cpu_signature prev;
725         struct cpuinfo_x86 *c = &cpu_data(cpu_num);
726         unsigned int val[2];
727
728         memset(csig, 0, sizeof(*csig));
729
730         csig->sig = cpuid_eax(0x00000001);
731
732         if ((c->x86_model >= 5) || (c->x86 > 6)) {
733                 /* get processor flags from MSR 0x17 */
734                 rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]);
735                 csig->pf = 1 << ((val[1] >> 18) & 7);
736         }
737
738         csig->rev = c->microcode;
739
740         /* No extra locking on prev, races are harmless. */
741         if (csig->sig != prev.sig || csig->pf != prev.pf || csig->rev != prev.rev) {
742                 pr_info("sig=0x%x, pf=0x%x, revision=0x%x\n",
743                         csig->sig, csig->pf, csig->rev);
744                 prev = *csig;
745         }
746
747         return 0;
748 }
749
750 static enum ucode_state apply_microcode_intel(int cpu)
751 {
752         struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
753         struct cpuinfo_x86 *c = &cpu_data(cpu);
754         struct microcode_intel *mc;
755         enum ucode_state ret;
756         static int prev_rev;
757         u32 rev;
758
759         /* We should bind the task to the CPU */
760         if (WARN_ON(raw_smp_processor_id() != cpu))
761                 return UCODE_ERROR;
762
763         /* Look for a newer patch in our cache: */
764         mc = find_patch(uci);
765         if (!mc) {
766                 mc = uci->mc;
767                 if (!mc)
768                         return UCODE_NFOUND;
769         }
770
771         /*
772          * Save us the MSR write below - which is a particular expensive
773          * operation - when the other hyperthread has updated the microcode
774          * already.
775          */
776         rev = intel_get_microcode_revision();
777         if (rev >= mc->hdr.rev) {
778                 ret = UCODE_OK;
779                 goto out;
780         }
781
782         /*
783          * Writeback and invalidate caches before updating microcode to avoid
784          * internal issues depending on what the microcode is updating.
785          */
786         native_wbinvd();
787
788         /* write microcode via MSR 0x79 */
789         wrmsrl(MSR_IA32_UCODE_WRITE, (unsigned long)mc->bits);
790
791         rev = intel_get_microcode_revision();
792
793         if (rev != mc->hdr.rev) {
794                 pr_err("CPU%d update to revision 0x%x failed\n",
795                        cpu, mc->hdr.rev);
796                 return UCODE_ERROR;
797         }
798
799         if (rev != prev_rev) {
800                 pr_info("updated to revision 0x%x, date = %04x-%02x-%02x\n",
801                         rev,
802                         mc->hdr.date & 0xffff,
803                         mc->hdr.date >> 24,
804                         (mc->hdr.date >> 16) & 0xff);
805                 prev_rev = rev;
806         }
807
808         ret = UCODE_UPDATED;
809
810 out:
811         uci->cpu_sig.rev = rev;
812         c->microcode     = rev;
813
814         /* Update boot_cpu_data's revision too, if we're on the BSP: */
815         if (c->cpu_index == boot_cpu_data.cpu_index)
816                 boot_cpu_data.microcode = rev;
817
818         return ret;
819 }
820
821 static enum ucode_state generic_load_microcode(int cpu, void *data, size_t size,
822                                 int (*get_ucode_data)(void *, const void *, size_t))
823 {
824         struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
825         u8 *ucode_ptr = data, *new_mc = NULL, *mc = NULL;
826         int new_rev = uci->cpu_sig.rev;
827         unsigned int leftover = size;
828         unsigned int curr_mc_size = 0, new_mc_size = 0;
829         unsigned int csig, cpf;
830         enum ucode_state ret = UCODE_OK;
831
832         while (leftover) {
833                 struct microcode_header_intel mc_header;
834                 unsigned int mc_size;
835
836                 if (leftover < sizeof(mc_header)) {
837                         pr_err("error! Truncated header in microcode data file\n");
838                         break;
839                 }
840
841                 if (get_ucode_data(&mc_header, ucode_ptr, sizeof(mc_header)))
842                         break;
843
844                 mc_size = get_totalsize(&mc_header);
845                 if (!mc_size || mc_size > leftover) {
846                         pr_err("error! Bad data in microcode data file\n");
847                         break;
848                 }
849
850                 /* For performance reasons, reuse mc area when possible */
851                 if (!mc || mc_size > curr_mc_size) {
852                         vfree(mc);
853                         mc = vmalloc(mc_size);
854                         if (!mc)
855                                 break;
856                         curr_mc_size = mc_size;
857                 }
858
859                 if (get_ucode_data(mc, ucode_ptr, mc_size) ||
860                     microcode_sanity_check(mc, 1) < 0) {
861                         break;
862                 }
863
864                 csig = uci->cpu_sig.sig;
865                 cpf = uci->cpu_sig.pf;
866                 if (has_newer_microcode(mc, csig, cpf, new_rev)) {
867                         vfree(new_mc);
868                         new_rev = mc_header.rev;
869                         new_mc  = mc;
870                         new_mc_size = mc_size;
871                         mc = NULL;      /* trigger new vmalloc */
872                         ret = UCODE_NEW;
873                 }
874
875                 ucode_ptr += mc_size;
876                 leftover  -= mc_size;
877         }
878
879         vfree(mc);
880
881         if (leftover) {
882                 vfree(new_mc);
883                 return UCODE_ERROR;
884         }
885
886         if (!new_mc)
887                 return UCODE_NFOUND;
888
889         vfree(uci->mc);
890         uci->mc = (struct microcode_intel *)new_mc;
891
892         /*
893          * If early loading microcode is supported, save this mc into
894          * permanent memory. So it will be loaded early when a CPU is hot added
895          * or resumes.
896          */
897         save_mc_for_early(uci, new_mc, new_mc_size);
898
899         pr_debug("CPU%d found a matching microcode update with version 0x%x (current=0x%x)\n",
900                  cpu, new_rev, uci->cpu_sig.rev);
901
902         return ret;
903 }
904
905 static int get_ucode_fw(void *to, const void *from, size_t n)
906 {
907         memcpy(to, from, n);
908         return 0;
909 }
910
911 static bool is_blacklisted(unsigned int cpu)
912 {
913         struct cpuinfo_x86 *c = &cpu_data(cpu);
914
915         /*
916          * Late loading on model 79 with microcode revision less than 0x0b000021
917          * and LLC size per core bigger than 2.5MB may result in a system hang.
918          * This behavior is documented in item BDF90, #334165 (Intel Xeon
919          * Processor E7-8800/4800 v4 Product Family).
920          */
921         if (c->x86 == 6 &&
922             c->x86_model == INTEL_FAM6_BROADWELL_X &&
923             c->x86_stepping == 0x01 &&
924             llc_size_per_core > 2621440 &&
925             c->microcode < 0x0b000021) {
926                 pr_err_once("Erratum BDF90: late loading with revision < 0x0b000021 (0x%x) disabled.\n", c->microcode);
927                 pr_err_once("Please consider either early loading through initrd/built-in or a potential BIOS update.\n");
928                 return true;
929         }
930
931         return false;
932 }
933
934 static enum ucode_state request_microcode_fw(int cpu, struct device *device,
935                                              bool refresh_fw)
936 {
937         char name[30];
938         struct cpuinfo_x86 *c = &cpu_data(cpu);
939         const struct firmware *firmware;
940         enum ucode_state ret;
941
942         if (is_blacklisted(cpu))
943                 return UCODE_NFOUND;
944
945         sprintf(name, "/*(DEBLOBBED)*/",
946                 c->x86, c->x86_model, c->x86_stepping);
947
948         if (reject_firmware_direct(&firmware, name, device)) {
949                 pr_debug("data file %s load failed\n", name);
950                 return UCODE_NFOUND;
951         }
952
953         ret = generic_load_microcode(cpu, (void *)firmware->data,
954                                      firmware->size, &get_ucode_fw);
955
956         release_firmware(firmware);
957
958         return ret;
959 }
960
961 static int get_ucode_user(void *to, const void *from, size_t n)
962 {
963         return copy_from_user(to, from, n);
964 }
965
966 static enum ucode_state
967 request_microcode_user(int cpu, const void __user *buf, size_t size)
968 {
969         if (is_blacklisted(cpu))
970                 return UCODE_NFOUND;
971
972         return generic_load_microcode(cpu, (void *)buf, size, &get_ucode_user);
973 }
974
975 static struct microcode_ops microcode_intel_ops = {
976         .request_microcode_user           = request_microcode_user,
977         .request_microcode_fw             = request_microcode_fw,
978         .collect_cpu_info                 = collect_cpu_info,
979         .apply_microcode                  = apply_microcode_intel,
980 };
981
982 static int __init calc_llc_size_per_core(struct cpuinfo_x86 *c)
983 {
984         u64 llc_size = c->x86_cache_size * 1024ULL;
985
986         do_div(llc_size, c->x86_max_cores);
987
988         return (int)llc_size;
989 }
990
991 struct microcode_ops * __init init_intel_microcode(void)
992 {
993         struct cpuinfo_x86 *c = &boot_cpu_data;
994
995         if (c->x86_vendor != X86_VENDOR_INTEL || c->x86 < 6 ||
996             cpu_has(c, X86_FEATURE_IA64)) {
997                 pr_err("Intel CPU family 0x%x not supported\n", c->x86);
998                 return NULL;
999         }
1000
1001         llc_size_per_core = calc_llc_size_per_core(c);
1002
1003         return &microcode_intel_ops;
1004 }