x86/microcode: Fix loading precedence
[linux-2.6-block.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 <tigran@aivazian.fsnet.co.uk>
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/processor.h>
38 #include <asm/tlbflush.h>
39 #include <asm/setup.h>
40 #include <asm/msr.h>
41
42 /*
43  * Temporary microcode blobs pointers storage. We note here the pointers to
44  * microcode blobs we've got from whatever storage (detached initrd, builtin).
45  * Later on, we put those into final storage mc_saved_data.mc_saved.
46  */
47 static unsigned long mc_tmp_ptrs[MAX_UCODE_COUNT];
48
49 static struct mc_saved_data {
50         unsigned int num_saved;
51         struct microcode_intel **mc_saved;
52 } mc_saved_data;
53
54 /* Microcode blobs within the initrd. 0 if builtin. */
55 static struct ucode_blobs {
56         unsigned long start;
57         bool valid;
58 } blobs;
59
60 static enum ucode_state
61 load_microcode_early(struct microcode_intel **saved,
62                      unsigned int num_saved, struct ucode_cpu_info *uci)
63 {
64         struct microcode_intel *ucode_ptr, *new_mc = NULL;
65         struct microcode_header_intel *mc_hdr;
66         int new_rev, ret, i;
67
68         new_rev = uci->cpu_sig.rev;
69
70         for (i = 0; i < num_saved; i++) {
71                 ucode_ptr = saved[i];
72                 mc_hdr    = (struct microcode_header_intel *)ucode_ptr;
73
74                 ret = has_newer_microcode(ucode_ptr,
75                                           uci->cpu_sig.sig,
76                                           uci->cpu_sig.pf,
77                                           new_rev);
78                 if (!ret)
79                         continue;
80
81                 new_rev = mc_hdr->rev;
82                 new_mc  = ucode_ptr;
83         }
84
85         if (!new_mc)
86                 return UCODE_NFOUND;
87
88         uci->mc = (struct microcode_intel *)new_mc;
89         return UCODE_OK;
90 }
91
92 static inline void
93 copy_ptrs(struct microcode_intel **mc_saved, unsigned long *mc_ptrs,
94           unsigned long off, int num_saved)
95 {
96         int i;
97
98         for (i = 0; i < num_saved; i++)
99                 mc_saved[i] = (struct microcode_intel *)(mc_ptrs[i] + off);
100 }
101
102 #ifdef CONFIG_X86_32
103 static void
104 microcode_phys(struct microcode_intel **mc_saved_tmp, struct mc_saved_data *mcs)
105 {
106         int i;
107         struct microcode_intel ***mc_saved;
108
109         mc_saved = (struct microcode_intel ***)__pa_nodebug(&mcs->mc_saved);
110
111         for (i = 0; i < mcs->num_saved; i++) {
112                 struct microcode_intel *p;
113
114                 p = *(struct microcode_intel **)__pa_nodebug(mcs->mc_saved + i);
115                 mc_saved_tmp[i] = (struct microcode_intel *)__pa_nodebug(p);
116         }
117 }
118 #endif
119
120 static enum ucode_state
121 load_microcode(struct mc_saved_data *mcs, unsigned long *mc_ptrs,
122                unsigned long offset, struct ucode_cpu_info *uci)
123 {
124         struct microcode_intel *mc_saved_tmp[MAX_UCODE_COUNT];
125         unsigned int count = mcs->num_saved;
126
127         if (!mcs->mc_saved) {
128                 copy_ptrs(mc_saved_tmp, mc_ptrs, offset, count);
129
130                 return load_microcode_early(mc_saved_tmp, count, uci);
131         } else {
132 #ifdef CONFIG_X86_32
133                 microcode_phys(mc_saved_tmp, mcs);
134                 return load_microcode_early(mc_saved_tmp, count, uci);
135 #else
136                 return load_microcode_early(mcs->mc_saved, count, uci);
137 #endif
138         }
139 }
140
141 /*
142  * Given CPU signature and a microcode patch, this function finds if the
143  * microcode patch has matching family and model with the CPU.
144  */
145 static enum ucode_state
146 matching_model_microcode(struct microcode_header_intel *mc_header,
147                         unsigned long sig)
148 {
149         unsigned int fam, model;
150         unsigned int fam_ucode, model_ucode;
151         struct extended_sigtable *ext_header;
152         unsigned long total_size = get_totalsize(mc_header);
153         unsigned long data_size = get_datasize(mc_header);
154         int ext_sigcount, i;
155         struct extended_signature *ext_sig;
156
157         fam   = x86_family(sig);
158         model = x86_model(sig);
159
160         fam_ucode   = x86_family(mc_header->sig);
161         model_ucode = x86_model(mc_header->sig);
162
163         if (fam == fam_ucode && model == model_ucode)
164                 return UCODE_OK;
165
166         /* Look for ext. headers: */
167         if (total_size <= data_size + MC_HEADER_SIZE)
168                 return UCODE_NFOUND;
169
170         ext_header   = (void *) mc_header + data_size + MC_HEADER_SIZE;
171         ext_sig      = (void *)ext_header + EXT_HEADER_SIZE;
172         ext_sigcount = ext_header->count;
173
174         for (i = 0; i < ext_sigcount; i++) {
175                 fam_ucode   = x86_family(ext_sig->sig);
176                 model_ucode = x86_model(ext_sig->sig);
177
178                 if (fam == fam_ucode && model == model_ucode)
179                         return UCODE_OK;
180
181                 ext_sig++;
182         }
183         return UCODE_NFOUND;
184 }
185
186 static int
187 save_microcode(struct mc_saved_data *mcs,
188                struct microcode_intel **mc_saved_src,
189                unsigned int num_saved)
190 {
191         int i, j;
192         struct microcode_intel **saved_ptr;
193         int ret;
194
195         if (!num_saved)
196                 return -EINVAL;
197
198         /*
199          * Copy new microcode data.
200          */
201         saved_ptr = kcalloc(num_saved, sizeof(struct microcode_intel *), GFP_KERNEL);
202         if (!saved_ptr)
203                 return -ENOMEM;
204
205         for (i = 0; i < num_saved; i++) {
206                 struct microcode_header_intel *mc_hdr;
207                 struct microcode_intel *mc;
208                 unsigned long size;
209
210                 if (!mc_saved_src[i]) {
211                         ret = -EINVAL;
212                         goto err;
213                 }
214
215                 mc     = mc_saved_src[i];
216                 mc_hdr = &mc->hdr;
217                 size   = get_totalsize(mc_hdr);
218
219                 saved_ptr[i] = kmemdup(mc, size, GFP_KERNEL);
220                 if (!saved_ptr[i]) {
221                         ret = -ENOMEM;
222                         goto err;
223                 }
224         }
225
226         /*
227          * Point to newly saved microcode.
228          */
229         mcs->mc_saved  = saved_ptr;
230         mcs->num_saved = num_saved;
231
232         return 0;
233
234 err:
235         for (j = 0; j <= i; j++)
236                 kfree(saved_ptr[j]);
237         kfree(saved_ptr);
238
239         return ret;
240 }
241
242 /*
243  * A microcode patch in ucode_ptr is saved into mc_saved
244  * - if it has matching signature and newer revision compared to an existing
245  *   patch mc_saved.
246  * - or if it is a newly discovered microcode patch.
247  *
248  * The microcode patch should have matching model with CPU.
249  *
250  * Returns: The updated number @num_saved of saved microcode patches.
251  */
252 static unsigned int _save_mc(struct microcode_intel **mc_saved,
253                              u8 *ucode_ptr, unsigned int num_saved)
254 {
255         struct microcode_header_intel *mc_hdr, *mc_saved_hdr;
256         unsigned int sig, pf;
257         int found = 0, i;
258
259         mc_hdr = (struct microcode_header_intel *)ucode_ptr;
260
261         for (i = 0; i < num_saved; i++) {
262                 mc_saved_hdr = (struct microcode_header_intel *)mc_saved[i];
263                 sig          = mc_saved_hdr->sig;
264                 pf           = mc_saved_hdr->pf;
265
266                 if (!find_matching_signature(ucode_ptr, sig, pf))
267                         continue;
268
269                 found = 1;
270
271                 if (mc_hdr->rev <= mc_saved_hdr->rev)
272                         continue;
273
274                 /*
275                  * Found an older ucode saved earlier. Replace it with
276                  * this newer one.
277                  */
278                 mc_saved[i] = (struct microcode_intel *)ucode_ptr;
279                 break;
280         }
281
282         /* Newly detected microcode, save it to memory. */
283         if (i >= num_saved && !found)
284                 mc_saved[num_saved++] = (struct microcode_intel *)ucode_ptr;
285
286         return num_saved;
287 }
288
289 /*
290  * Get microcode matching with BSP's model. Only CPUs with the same model as
291  * BSP can stay in the platform.
292  */
293 static enum ucode_state __init
294 get_matching_model_microcode(unsigned long start, void *data, size_t size,
295                              struct mc_saved_data *mcs, unsigned long *mc_ptrs,
296                              struct ucode_cpu_info *uci)
297 {
298         struct microcode_intel *mc_saved_tmp[MAX_UCODE_COUNT];
299         struct microcode_header_intel *mc_header;
300         unsigned int num_saved = mcs->num_saved;
301         enum ucode_state state = UCODE_OK;
302         unsigned int leftover = size;
303         u8 *ucode_ptr = data;
304         unsigned int mc_size;
305         int i;
306
307         while (leftover && num_saved < ARRAY_SIZE(mc_saved_tmp)) {
308
309                 if (leftover < sizeof(mc_header))
310                         break;
311
312                 mc_header = (struct microcode_header_intel *)ucode_ptr;
313
314                 mc_size = get_totalsize(mc_header);
315                 if (!mc_size || mc_size > leftover ||
316                         microcode_sanity_check(ucode_ptr, 0) < 0)
317                         break;
318
319                 leftover -= mc_size;
320
321                 /*
322                  * Since APs with same family and model as the BSP may boot in
323                  * the platform, we need to find and save microcode patches
324                  * with the same family and model as the BSP.
325                  */
326                 if (matching_model_microcode(mc_header, uci->cpu_sig.sig) != UCODE_OK) {
327                         ucode_ptr += mc_size;
328                         continue;
329                 }
330
331                 num_saved = _save_mc(mc_saved_tmp, ucode_ptr, num_saved);
332
333                 ucode_ptr += mc_size;
334         }
335
336         if (leftover) {
337                 state = UCODE_ERROR;
338                 return state;
339         }
340
341         if (!num_saved) {
342                 state = UCODE_NFOUND;
343                 return state;
344         }
345
346         for (i = 0; i < num_saved; i++)
347                 mc_ptrs[i] = (unsigned long)mc_saved_tmp[i] - start;
348
349         mcs->num_saved = num_saved;
350
351         return state;
352 }
353
354 static int collect_cpu_info_early(struct ucode_cpu_info *uci)
355 {
356         unsigned int val[2];
357         unsigned int family, model;
358         struct cpu_signature csig;
359         unsigned int eax, ebx, ecx, edx;
360
361         csig.sig = 0;
362         csig.pf = 0;
363         csig.rev = 0;
364
365         memset(uci, 0, sizeof(*uci));
366
367         eax = 0x00000001;
368         ecx = 0;
369         native_cpuid(&eax, &ebx, &ecx, &edx);
370         csig.sig = eax;
371
372         family = x86_family(csig.sig);
373         model  = x86_model(csig.sig);
374
375         if ((model >= 5) || (family > 6)) {
376                 /* get processor flags from MSR 0x17 */
377                 native_rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]);
378                 csig.pf = 1 << ((val[1] >> 18) & 7);
379         }
380         native_wrmsrl(MSR_IA32_UCODE_REV, 0);
381
382         /* As documented in the SDM: Do a CPUID 1 here */
383         sync_core();
384
385         /* get the current revision from MSR 0x8B */
386         native_rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
387
388         csig.rev = val[1];
389
390         uci->cpu_sig = csig;
391         uci->valid = 1;
392
393         return 0;
394 }
395
396 static void show_saved_mc(void)
397 {
398 #ifdef DEBUG
399         int i, j;
400         unsigned int sig, pf, rev, total_size, data_size, date;
401         struct ucode_cpu_info uci;
402
403         if (!mc_saved_data.num_saved) {
404                 pr_debug("no microcode data saved.\n");
405                 return;
406         }
407         pr_debug("Total microcode saved: %d\n", mc_saved_data.num_saved);
408
409         collect_cpu_info_early(&uci);
410
411         sig = uci.cpu_sig.sig;
412         pf = uci.cpu_sig.pf;
413         rev = uci.cpu_sig.rev;
414         pr_debug("CPU: sig=0x%x, pf=0x%x, rev=0x%x\n", sig, pf, rev);
415
416         for (i = 0; i < mc_saved_data.num_saved; i++) {
417                 struct microcode_header_intel *mc_saved_header;
418                 struct extended_sigtable *ext_header;
419                 int ext_sigcount;
420                 struct extended_signature *ext_sig;
421
422                 mc_saved_header = (struct microcode_header_intel *)
423                                   mc_saved_data.mc_saved[i];
424                 sig = mc_saved_header->sig;
425                 pf = mc_saved_header->pf;
426                 rev = mc_saved_header->rev;
427                 total_size = get_totalsize(mc_saved_header);
428                 data_size = get_datasize(mc_saved_header);
429                 date = mc_saved_header->date;
430
431                 pr_debug("mc_saved[%d]: sig=0x%x, pf=0x%x, rev=0x%x, total size=0x%x, date = %04x-%02x-%02x\n",
432                          i, sig, pf, rev, total_size,
433                          date & 0xffff,
434                          date >> 24,
435                          (date >> 16) & 0xff);
436
437                 /* Look for ext. headers: */
438                 if (total_size <= data_size + MC_HEADER_SIZE)
439                         continue;
440
441                 ext_header = (void *) mc_saved_header + data_size + MC_HEADER_SIZE;
442                 ext_sigcount = ext_header->count;
443                 ext_sig = (void *)ext_header + EXT_HEADER_SIZE;
444
445                 for (j = 0; j < ext_sigcount; j++) {
446                         sig = ext_sig->sig;
447                         pf = ext_sig->pf;
448
449                         pr_debug("\tExtended[%d]: sig=0x%x, pf=0x%x\n",
450                                  j, sig, pf);
451
452                         ext_sig++;
453                 }
454
455         }
456 #endif
457 }
458
459 #ifdef CONFIG_HOTPLUG_CPU
460 static DEFINE_MUTEX(x86_cpu_microcode_mutex);
461 /*
462  * Save this mc into mc_saved_data. So it will be loaded early when a CPU is
463  * hot added or resumes.
464  *
465  * Please make sure this mc should be a valid microcode patch before calling
466  * this function.
467  */
468 int save_mc_for_early(u8 *mc)
469 {
470         struct microcode_intel *mc_saved_tmp[MAX_UCODE_COUNT];
471         unsigned int mc_saved_count_init;
472         unsigned int num_saved;
473         struct microcode_intel **mc_saved;
474         int ret = 0;
475         int i;
476
477         /*
478          * Hold hotplug lock so mc_saved_data is not accessed by a CPU in
479          * hotplug.
480          */
481         mutex_lock(&x86_cpu_microcode_mutex);
482
483         mc_saved_count_init = mc_saved_data.num_saved;
484         num_saved = mc_saved_data.num_saved;
485         mc_saved = mc_saved_data.mc_saved;
486
487         if (mc_saved && num_saved)
488                 memcpy(mc_saved_tmp, mc_saved,
489                        num_saved * sizeof(struct microcode_intel *));
490         /*
491          * Save the microcode patch mc in mc_save_tmp structure if it's a newer
492          * version.
493          */
494         num_saved = _save_mc(mc_saved_tmp, mc, num_saved);
495
496         /*
497          * Save the mc_save_tmp in global mc_saved_data.
498          */
499         ret = save_microcode(&mc_saved_data, mc_saved_tmp, num_saved);
500         if (ret) {
501                 pr_err("Cannot save microcode patch.\n");
502                 goto out;
503         }
504
505         show_saved_mc();
506
507         /*
508          * Free old saved microcode data.
509          */
510         if (mc_saved) {
511                 for (i = 0; i < mc_saved_count_init; i++)
512                         kfree(mc_saved[i]);
513                 kfree(mc_saved);
514         }
515
516 out:
517         mutex_unlock(&x86_cpu_microcode_mutex);
518
519         return ret;
520 }
521 EXPORT_SYMBOL_GPL(save_mc_for_early);
522 #endif
523
524 static bool __init load_builtin_intel_microcode(struct cpio_data *cp)
525 {
526 #ifdef CONFIG_X86_64
527         unsigned int eax = 0x00000001, ebx, ecx = 0, edx;
528         char name[30];
529
530         native_cpuid(&eax, &ebx, &ecx, &edx);
531
532         sprintf(name, "intel-ucode/%02x-%02x-%02x",
533                       x86_family(eax), x86_model(eax), x86_stepping(eax));
534
535         return get_builtin_firmware(cp, name);
536 #else
537         return false;
538 #endif
539 }
540
541 /*
542  * Print ucode update info.
543  */
544 static void
545 print_ucode_info(struct ucode_cpu_info *uci, unsigned int date)
546 {
547         pr_info_once("microcode updated early to revision 0x%x, date = %04x-%02x-%02x\n",
548                      uci->cpu_sig.rev,
549                      date & 0xffff,
550                      date >> 24,
551                      (date >> 16) & 0xff);
552 }
553
554 #ifdef CONFIG_X86_32
555
556 static int delay_ucode_info;
557 static int current_mc_date;
558
559 /*
560  * Print early updated ucode info after printk works. This is delayed info dump.
561  */
562 void show_ucode_info_early(void)
563 {
564         struct ucode_cpu_info uci;
565
566         if (delay_ucode_info) {
567                 collect_cpu_info_early(&uci);
568                 print_ucode_info(&uci, current_mc_date);
569                 delay_ucode_info = 0;
570         }
571 }
572
573 /*
574  * At this point, we can not call printk() yet. Keep microcode patch number in
575  * mc_saved_data.mc_saved and delay printing microcode info in
576  * show_ucode_info_early() until printk() works.
577  */
578 static void print_ucode(struct ucode_cpu_info *uci)
579 {
580         struct microcode_intel *mc;
581         int *delay_ucode_info_p;
582         int *current_mc_date_p;
583
584         mc = uci->mc;
585         if (!mc)
586                 return;
587
588         delay_ucode_info_p = (int *)__pa_nodebug(&delay_ucode_info);
589         current_mc_date_p = (int *)__pa_nodebug(&current_mc_date);
590
591         *delay_ucode_info_p = 1;
592         *current_mc_date_p = mc->hdr.date;
593 }
594 #else
595
596 /*
597  * Flush global tlb. We only do this in x86_64 where paging has been enabled
598  * already and PGE should be enabled as well.
599  */
600 static inline void flush_tlb_early(void)
601 {
602         __native_flush_tlb_global_irq_disabled();
603 }
604
605 static inline void print_ucode(struct ucode_cpu_info *uci)
606 {
607         struct microcode_intel *mc;
608
609         mc = uci->mc;
610         if (!mc)
611                 return;
612
613         print_ucode_info(uci, mc->hdr.date);
614 }
615 #endif
616
617 static int apply_microcode_early(struct ucode_cpu_info *uci, bool early)
618 {
619         struct microcode_intel *mc;
620         unsigned int val[2];
621
622         mc = uci->mc;
623         if (!mc)
624                 return 0;
625
626         /* write microcode via MSR 0x79 */
627         native_wrmsrl(MSR_IA32_UCODE_WRITE, (unsigned long)mc->bits);
628         native_wrmsrl(MSR_IA32_UCODE_REV, 0);
629
630         /* As documented in the SDM: Do a CPUID 1 here */
631         sync_core();
632
633         /* get the current revision from MSR 0x8B */
634         native_rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
635         if (val[1] != mc->hdr.rev)
636                 return -1;
637
638 #ifdef CONFIG_X86_64
639         /* Flush global tlb. This is precaution. */
640         flush_tlb_early();
641 #endif
642         uci->cpu_sig.rev = val[1];
643
644         if (early)
645                 print_ucode(uci);
646         else
647                 print_ucode_info(uci, mc->hdr.date);
648
649         return 0;
650 }
651
652 /*
653  * This function converts microcode patch offsets previously stored in
654  * mc_tmp_ptrs to pointers and stores the pointers in mc_saved_data.
655  */
656 int __init save_microcode_in_initrd_intel(void)
657 {
658         struct microcode_intel *mc_saved[MAX_UCODE_COUNT];
659         unsigned int count = mc_saved_data.num_saved;
660         unsigned long offset = 0;
661         int ret;
662
663         if (!count)
664                 return 0;
665
666         /*
667          * We have found a valid initrd but it might've been relocated in the
668          * meantime so get its updated address.
669          */
670         if (IS_ENABLED(CONFIG_BLK_DEV_INITRD) && blobs.valid)
671                 offset = initrd_start;
672
673         copy_ptrs(mc_saved, mc_tmp_ptrs, offset, count);
674
675         ret = save_microcode(&mc_saved_data, mc_saved, count);
676         if (ret)
677                 pr_err("Cannot save microcode patches from initrd.\n");
678
679         show_saved_mc();
680
681         return ret;
682 }
683
684 static __init enum ucode_state
685 __scan_microcode_initrd(struct cpio_data *cd, struct ucode_blobs *blbp)
686 {
687 #ifdef CONFIG_BLK_DEV_INITRD
688         long offset = 0;
689         static __initdata char ucode_name[] = "kernel/x86/microcode/GenuineIntel.bin";
690         char *p = IS_ENABLED(CONFIG_X86_32) ? (char *)__pa_nodebug(ucode_name)
691                                                     : ucode_name;
692 # ifdef CONFIG_X86_32
693         unsigned long start = 0, size;
694         struct boot_params *params;
695
696         params = (struct boot_params *)__pa_nodebug(&boot_params);
697         size   = params->hdr.ramdisk_size;
698
699         /*
700          * Set start only if we have an initrd image. We cannot use initrd_start
701          * because it is not set that early yet.
702          */
703         start = (size ? params->hdr.ramdisk_image : 0);
704
705 # else /* CONFIG_X86_64 */
706         unsigned long start = 0, size;
707
708         size  = (u64)boot_params.ext_ramdisk_size << 32;
709         size |= boot_params.hdr.ramdisk_size;
710
711         if (size) {
712                 start  = (u64)boot_params.ext_ramdisk_image << 32;
713                 start |= boot_params.hdr.ramdisk_image;
714
715                 start += PAGE_OFFSET;
716         }
717 # endif
718
719         *cd = find_cpio_data(p, (void *)start, size, &offset);
720         if (cd->data) {
721                 blbp->start = start;
722                 blbp->valid = true;
723
724                 return UCODE_OK;
725         } else
726 #endif /* CONFIG_BLK_DEV_INITRD */
727                 return UCODE_ERROR;
728 }
729
730 static __init enum ucode_state
731 scan_microcode(struct mc_saved_data *mcs, unsigned long *mc_ptrs,
732                struct ucode_cpu_info *uci, struct ucode_blobs *blbp)
733 {
734         struct cpio_data cd = { NULL, 0, "" };
735         enum ucode_state ret;
736
737         /* try built-in microcode first */
738         if (load_builtin_intel_microcode(&cd))
739                 /*
740                  * Invalidate blobs as we might've gotten an initrd too,
741                  * supplied by the boot loader, by mistake or simply forgotten
742                  * there. That's fine, we ignore it since we've found builtin
743                  * microcode already.
744                  */
745                 blbp->valid = false;
746         else {
747                 ret = __scan_microcode_initrd(&cd, blbp);
748                 if (ret != UCODE_OK)
749                         return ret;
750         }
751
752         return get_matching_model_microcode(blbp->start, cd.data, cd.size,
753                                             mcs, mc_ptrs, uci);
754 }
755
756 static void __init
757 _load_ucode_intel_bsp(struct mc_saved_data *mcs, unsigned long *mc_ptrs,
758                       struct ucode_blobs *blbp)
759 {
760         struct ucode_cpu_info uci;
761         enum ucode_state ret;
762
763         collect_cpu_info_early(&uci);
764
765         ret = scan_microcode(mcs, mc_ptrs, &uci, blbp);
766         if (ret != UCODE_OK)
767                 return;
768
769         ret = load_microcode(mcs, mc_ptrs, blbp->start, &uci);
770         if (ret != UCODE_OK)
771                 return;
772
773         apply_microcode_early(&uci, true);
774 }
775
776 void __init load_ucode_intel_bsp(void)
777 {
778         struct ucode_blobs *blobs_p;
779         struct mc_saved_data *mcs;
780         unsigned long *ptrs;
781
782 #ifdef CONFIG_X86_32
783         mcs     = (struct mc_saved_data *)__pa_nodebug(&mc_saved_data);
784         ptrs    = (unsigned long *)__pa_nodebug(&mc_tmp_ptrs);
785         blobs_p = (struct ucode_blobs *)__pa_nodebug(&blobs);
786 #else
787         mcs     = &mc_saved_data;
788         ptrs    = mc_tmp_ptrs;
789         blobs_p = &blobs;
790 #endif
791
792         _load_ucode_intel_bsp(mcs, ptrs, blobs_p);
793 }
794
795 void load_ucode_intel_ap(void)
796 {
797         struct ucode_blobs *blobs_p;
798         struct mc_saved_data *mcs;
799         struct ucode_cpu_info uci;
800         enum ucode_state ret;
801         unsigned long *ptrs;
802
803 #ifdef CONFIG_X86_32
804         mcs     = (struct mc_saved_data *)__pa_nodebug(&mc_saved_data);
805         ptrs    = (unsigned long *)__pa_nodebug(mc_tmp_ptrs);
806         blobs_p = (struct ucode_blobs *)__pa_nodebug(&blobs);
807 #else
808         mcs     = &mc_saved_data;
809         ptrs    = mc_tmp_ptrs;
810         blobs_p = &blobs;
811 #endif
812
813         /*
814          * If there is no valid ucode previously saved in memory, no need to
815          * update ucode on this AP.
816          */
817         if (!mcs->num_saved)
818                 return;
819
820         collect_cpu_info_early(&uci);
821         ret = load_microcode(mcs, ptrs, blobs_p->start, &uci);
822         if (ret != UCODE_OK)
823                 return;
824
825         apply_microcode_early(&uci, true);
826 }
827
828 void reload_ucode_intel(void)
829 {
830         struct ucode_cpu_info uci;
831         enum ucode_state ret;
832
833         if (!mc_saved_data.num_saved)
834                 return;
835
836         collect_cpu_info_early(&uci);
837
838         ret = load_microcode_early(mc_saved_data.mc_saved,
839                                    mc_saved_data.num_saved, &uci);
840         if (ret != UCODE_OK)
841                 return;
842
843         apply_microcode_early(&uci, false);
844 }
845
846 static int collect_cpu_info(int cpu_num, struct cpu_signature *csig)
847 {
848         struct cpuinfo_x86 *c = &cpu_data(cpu_num);
849         unsigned int val[2];
850
851         memset(csig, 0, sizeof(*csig));
852
853         csig->sig = cpuid_eax(0x00000001);
854
855         if ((c->x86_model >= 5) || (c->x86 > 6)) {
856                 /* get processor flags from MSR 0x17 */
857                 rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]);
858                 csig->pf = 1 << ((val[1] >> 18) & 7);
859         }
860
861         csig->rev = c->microcode;
862         pr_info("CPU%d sig=0x%x, pf=0x%x, revision=0x%x\n",
863                 cpu_num, csig->sig, csig->pf, csig->rev);
864
865         return 0;
866 }
867
868 /*
869  * return 0 - no update found
870  * return 1 - found update
871  */
872 static int get_matching_mc(struct microcode_intel *mc, int cpu)
873 {
874         struct cpu_signature cpu_sig;
875         unsigned int csig, cpf, crev;
876
877         collect_cpu_info(cpu, &cpu_sig);
878
879         csig = cpu_sig.sig;
880         cpf = cpu_sig.pf;
881         crev = cpu_sig.rev;
882
883         return has_newer_microcode(mc, csig, cpf, crev);
884 }
885
886 static int apply_microcode_intel(int cpu)
887 {
888         struct microcode_intel *mc;
889         struct ucode_cpu_info *uci;
890         struct cpuinfo_x86 *c;
891         unsigned int val[2];
892
893         /* We should bind the task to the CPU */
894         if (WARN_ON(raw_smp_processor_id() != cpu))
895                 return -1;
896
897         uci = ucode_cpu_info + cpu;
898         mc = uci->mc;
899         if (!mc)
900                 return 0;
901
902         /*
903          * Microcode on this CPU could be updated earlier. Only apply the
904          * microcode patch in mc when it is newer than the one on this
905          * CPU.
906          */
907         if (!get_matching_mc(mc, cpu))
908                 return 0;
909
910         /* write microcode via MSR 0x79 */
911         wrmsrl(MSR_IA32_UCODE_WRITE, (unsigned long)mc->bits);
912         wrmsrl(MSR_IA32_UCODE_REV, 0);
913
914         /* As documented in the SDM: Do a CPUID 1 here */
915         sync_core();
916
917         /* get the current revision from MSR 0x8B */
918         rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
919
920         if (val[1] != mc->hdr.rev) {
921                 pr_err("CPU%d update to revision 0x%x failed\n",
922                        cpu, mc->hdr.rev);
923                 return -1;
924         }
925
926         pr_info("CPU%d updated to revision 0x%x, date = %04x-%02x-%02x\n",
927                 cpu, val[1],
928                 mc->hdr.date & 0xffff,
929                 mc->hdr.date >> 24,
930                 (mc->hdr.date >> 16) & 0xff);
931
932         c = &cpu_data(cpu);
933
934         uci->cpu_sig.rev = val[1];
935         c->microcode = val[1];
936
937         return 0;
938 }
939
940 static enum ucode_state generic_load_microcode(int cpu, void *data, size_t size,
941                                 int (*get_ucode_data)(void *, const void *, size_t))
942 {
943         struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
944         u8 *ucode_ptr = data, *new_mc = NULL, *mc = NULL;
945         int new_rev = uci->cpu_sig.rev;
946         unsigned int leftover = size;
947         enum ucode_state state = UCODE_OK;
948         unsigned int curr_mc_size = 0;
949         unsigned int csig, cpf;
950
951         while (leftover) {
952                 struct microcode_header_intel mc_header;
953                 unsigned int mc_size;
954
955                 if (leftover < sizeof(mc_header)) {
956                         pr_err("error! Truncated header in microcode data file\n");
957                         break;
958                 }
959
960                 if (get_ucode_data(&mc_header, ucode_ptr, sizeof(mc_header)))
961                         break;
962
963                 mc_size = get_totalsize(&mc_header);
964                 if (!mc_size || mc_size > leftover) {
965                         pr_err("error! Bad data in microcode data file\n");
966                         break;
967                 }
968
969                 /* For performance reasons, reuse mc area when possible */
970                 if (!mc || mc_size > curr_mc_size) {
971                         vfree(mc);
972                         mc = vmalloc(mc_size);
973                         if (!mc)
974                                 break;
975                         curr_mc_size = mc_size;
976                 }
977
978                 if (get_ucode_data(mc, ucode_ptr, mc_size) ||
979                     microcode_sanity_check(mc, 1) < 0) {
980                         break;
981                 }
982
983                 csig = uci->cpu_sig.sig;
984                 cpf = uci->cpu_sig.pf;
985                 if (has_newer_microcode(mc, csig, cpf, new_rev)) {
986                         vfree(new_mc);
987                         new_rev = mc_header.rev;
988                         new_mc  = mc;
989                         mc = NULL;      /* trigger new vmalloc */
990                 }
991
992                 ucode_ptr += mc_size;
993                 leftover  -= mc_size;
994         }
995
996         vfree(mc);
997
998         if (leftover) {
999                 vfree(new_mc);
1000                 state = UCODE_ERROR;
1001                 goto out;
1002         }
1003
1004         if (!new_mc) {
1005                 state = UCODE_NFOUND;
1006                 goto out;
1007         }
1008
1009         vfree(uci->mc);
1010         uci->mc = (struct microcode_intel *)new_mc;
1011
1012         /*
1013          * If early loading microcode is supported, save this mc into
1014          * permanent memory. So it will be loaded early when a CPU is hot added
1015          * or resumes.
1016          */
1017         save_mc_for_early(new_mc);
1018
1019         pr_debug("CPU%d found a matching microcode update with version 0x%x (current=0x%x)\n",
1020                  cpu, new_rev, uci->cpu_sig.rev);
1021 out:
1022         return state;
1023 }
1024
1025 static int get_ucode_fw(void *to, const void *from, size_t n)
1026 {
1027         memcpy(to, from, n);
1028         return 0;
1029 }
1030
1031 static enum ucode_state request_microcode_fw(int cpu, struct device *device,
1032                                              bool refresh_fw)
1033 {
1034         char name[30];
1035         struct cpuinfo_x86 *c = &cpu_data(cpu);
1036         const struct firmware *firmware;
1037         enum ucode_state ret;
1038
1039         sprintf(name, "intel-ucode/%02x-%02x-%02x",
1040                 c->x86, c->x86_model, c->x86_mask);
1041
1042         if (request_firmware_direct(&firmware, name, device)) {
1043                 pr_debug("data file %s load failed\n", name);
1044                 return UCODE_NFOUND;
1045         }
1046
1047         ret = generic_load_microcode(cpu, (void *)firmware->data,
1048                                      firmware->size, &get_ucode_fw);
1049
1050         release_firmware(firmware);
1051
1052         return ret;
1053 }
1054
1055 static int get_ucode_user(void *to, const void *from, size_t n)
1056 {
1057         return copy_from_user(to, from, n);
1058 }
1059
1060 static enum ucode_state
1061 request_microcode_user(int cpu, const void __user *buf, size_t size)
1062 {
1063         return generic_load_microcode(cpu, (void *)buf, size, &get_ucode_user);
1064 }
1065
1066 static void microcode_fini_cpu(int cpu)
1067 {
1068         struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
1069
1070         vfree(uci->mc);
1071         uci->mc = NULL;
1072 }
1073
1074 static struct microcode_ops microcode_intel_ops = {
1075         .request_microcode_user           = request_microcode_user,
1076         .request_microcode_fw             = request_microcode_fw,
1077         .collect_cpu_info                 = collect_cpu_info,
1078         .apply_microcode                  = apply_microcode_intel,
1079         .microcode_fini_cpu               = microcode_fini_cpu,
1080 };
1081
1082 struct microcode_ops * __init init_intel_microcode(void)
1083 {
1084         struct cpuinfo_x86 *c = &boot_cpu_data;
1085
1086         if (c->x86_vendor != X86_VENDOR_INTEL || c->x86 < 6 ||
1087             cpu_has(c, X86_FEATURE_IA64)) {
1088                 pr_err("Intel CPU family 0x%x not supported\n", c->x86);
1089                 return NULL;
1090         }
1091
1092         return &microcode_intel_ops;
1093 }
1094