x86/microcode/intel: Handle truncated microcode images more robustly
[pandora-kernel.git] / arch / x86 / kernel / cpu / microcode / intel_early.c
1 /*
2  *      Intel CPU microcode early update for Linux
3  *
4  *      Copyright (C) 2012 Fenghua Yu <fenghua.yu@intel.com>
5  *                         H Peter Anvin" <hpa@zytor.com>
6  *
7  *      This allows to early upgrade microcode on Intel processors
8  *      belonging to IA-32 family - PentiumPro, Pentium II,
9  *      Pentium III, Xeon, Pentium 4, etc.
10  *
11  *      Reference: Section 9.11 of Volume 3, IA-32 Intel Architecture
12  *      Software Developer's Manual.
13  *
14  *      This program is free software; you can redistribute it and/or
15  *      modify it under the terms of the GNU General Public License
16  *      as published by the Free Software Foundation; either version
17  *      2 of the License, or (at your option) any later version.
18  */
19 #include <linux/module.h>
20 #include <linux/mm.h>
21 #include <linux/slab.h>
22 #include <linux/earlycpio.h>
23 #include <linux/initrd.h>
24 #include <linux/cpu.h>
25 #include <asm/msr.h>
26 #include <asm/microcode_intel.h>
27 #include <asm/processor.h>
28 #include <asm/tlbflush.h>
29 #include <asm/setup.h>
30
31 static unsigned long mc_saved_in_initrd[MAX_UCODE_COUNT];
32 static struct mc_saved_data {
33         unsigned int mc_saved_count;
34         struct microcode_intel **mc_saved;
35 } mc_saved_data;
36
37 static enum ucode_state
38 generic_load_microcode_early(struct microcode_intel **mc_saved_p,
39                              unsigned int mc_saved_count,
40                              struct ucode_cpu_info *uci)
41 {
42         struct microcode_intel *ucode_ptr, *new_mc = NULL;
43         int new_rev = uci->cpu_sig.rev;
44         enum ucode_state state = UCODE_OK;
45         unsigned int mc_size;
46         struct microcode_header_intel *mc_header;
47         unsigned int csig = uci->cpu_sig.sig;
48         unsigned int cpf = uci->cpu_sig.pf;
49         int i;
50
51         for (i = 0; i < mc_saved_count; i++) {
52                 ucode_ptr = mc_saved_p[i];
53
54                 mc_header = (struct microcode_header_intel *)ucode_ptr;
55                 mc_size = get_totalsize(mc_header);
56                 if (get_matching_microcode(csig, cpf, ucode_ptr, new_rev)) {
57                         new_rev = mc_header->rev;
58                         new_mc  = ucode_ptr;
59                 }
60         }
61
62         if (!new_mc) {
63                 state = UCODE_NFOUND;
64                 goto out;
65         }
66
67         uci->mc = (struct microcode_intel *)new_mc;
68 out:
69         return state;
70 }
71
72 static void
73 microcode_pointer(struct microcode_intel **mc_saved,
74                   unsigned long *mc_saved_in_initrd,
75                   unsigned long initrd_start, int mc_saved_count)
76 {
77         int i;
78
79         for (i = 0; i < mc_saved_count; i++)
80                 mc_saved[i] = (struct microcode_intel *)
81                               (mc_saved_in_initrd[i] + initrd_start);
82 }
83
84 #ifdef CONFIG_X86_32
85 static void
86 microcode_phys(struct microcode_intel **mc_saved_tmp,
87                struct mc_saved_data *mc_saved_data)
88 {
89         int i;
90         struct microcode_intel ***mc_saved;
91
92         mc_saved = (struct microcode_intel ***)
93                    __pa_nodebug(&mc_saved_data->mc_saved);
94         for (i = 0; i < mc_saved_data->mc_saved_count; i++) {
95                 struct microcode_intel *p;
96
97                 p = *(struct microcode_intel **)
98                         __pa_nodebug(mc_saved_data->mc_saved + i);
99                 mc_saved_tmp[i] = (struct microcode_intel *)__pa_nodebug(p);
100         }
101 }
102 #endif
103
104 static enum ucode_state
105 load_microcode(struct mc_saved_data *mc_saved_data,
106                unsigned long *mc_saved_in_initrd,
107                unsigned long initrd_start,
108                struct ucode_cpu_info *uci)
109 {
110         struct microcode_intel *mc_saved_tmp[MAX_UCODE_COUNT];
111         unsigned int count = mc_saved_data->mc_saved_count;
112
113         if (!mc_saved_data->mc_saved) {
114                 microcode_pointer(mc_saved_tmp, mc_saved_in_initrd,
115                                   initrd_start, count);
116
117                 return generic_load_microcode_early(mc_saved_tmp, count, uci);
118         } else {
119 #ifdef CONFIG_X86_32
120                 microcode_phys(mc_saved_tmp, mc_saved_data);
121                 return generic_load_microcode_early(mc_saved_tmp, count, uci);
122 #else
123                 return generic_load_microcode_early(mc_saved_data->mc_saved,
124                                                     count, uci);
125 #endif
126         }
127 }
128
129 static u8 get_x86_family(unsigned long sig)
130 {
131         u8 x86;
132
133         x86 = (sig >> 8) & 0xf;
134
135         if (x86 == 0xf)
136                 x86 += (sig >> 20) & 0xff;
137
138         return x86;
139 }
140
141 static u8 get_x86_model(unsigned long sig)
142 {
143         u8 x86, x86_model;
144
145         x86 = get_x86_family(sig);
146         x86_model = (sig >> 4) & 0xf;
147
148         if (x86 == 0x6 || x86 == 0xf)
149                 x86_model += ((sig >> 16) & 0xf) << 4;
150
151         return x86_model;
152 }
153
154 /*
155  * Given CPU signature and a microcode patch, this function finds if the
156  * microcode patch has matching family and model with the CPU.
157  */
158 static enum ucode_state
159 matching_model_microcode(struct microcode_header_intel *mc_header,
160                         unsigned long sig)
161 {
162         u8 x86, x86_model;
163         u8 x86_ucode, x86_model_ucode;
164         struct extended_sigtable *ext_header;
165         unsigned long total_size = get_totalsize(mc_header);
166         unsigned long data_size = get_datasize(mc_header);
167         int ext_sigcount, i;
168         struct extended_signature *ext_sig;
169
170         x86 = get_x86_family(sig);
171         x86_model = get_x86_model(sig);
172
173         x86_ucode = get_x86_family(mc_header->sig);
174         x86_model_ucode = get_x86_model(mc_header->sig);
175
176         if (x86 == x86_ucode && x86_model == x86_model_ucode)
177                 return UCODE_OK;
178
179         /* Look for ext. headers: */
180         if (total_size <= data_size + MC_HEADER_SIZE)
181                 return UCODE_NFOUND;
182
183         ext_header = (struct extended_sigtable *)
184                      mc_header + data_size + MC_HEADER_SIZE;
185         ext_sigcount = ext_header->count;
186         ext_sig = (void *)ext_header + EXT_HEADER_SIZE;
187
188         for (i = 0; i < ext_sigcount; i++) {
189                 x86_ucode = get_x86_family(ext_sig->sig);
190                 x86_model_ucode = get_x86_model(ext_sig->sig);
191
192                 if (x86 == x86_ucode && x86_model == x86_model_ucode)
193                         return UCODE_OK;
194
195                 ext_sig++;
196         }
197
198         return UCODE_NFOUND;
199 }
200
201 static int
202 save_microcode(struct mc_saved_data *mc_saved_data,
203                struct microcode_intel **mc_saved_src,
204                unsigned int mc_saved_count)
205 {
206         int i, j;
207         struct microcode_intel **mc_saved_p;
208         int ret;
209
210         if (!mc_saved_count)
211                 return -EINVAL;
212
213         /*
214          * Copy new microcode data.
215          */
216         mc_saved_p = kmalloc(mc_saved_count*sizeof(struct microcode_intel *),
217                              GFP_KERNEL);
218         if (!mc_saved_p)
219                 return -ENOMEM;
220
221         for (i = 0; i < mc_saved_count; i++) {
222                 struct microcode_intel *mc = mc_saved_src[i];
223                 struct microcode_header_intel *mc_header = &mc->hdr;
224                 unsigned long mc_size = get_totalsize(mc_header);
225                 mc_saved_p[i] = kmalloc(mc_size, GFP_KERNEL);
226                 if (!mc_saved_p[i]) {
227                         ret = -ENOMEM;
228                         goto err;
229                 }
230                 if (!mc_saved_src[i]) {
231                         ret = -EINVAL;
232                         goto err;
233                 }
234                 memcpy(mc_saved_p[i], mc, mc_size);
235         }
236
237         /*
238          * Point to newly saved microcode.
239          */
240         mc_saved_data->mc_saved = mc_saved_p;
241         mc_saved_data->mc_saved_count = mc_saved_count;
242
243         return 0;
244
245 err:
246         for (j = 0; j <= i; j++)
247                 kfree(mc_saved_p[j]);
248         kfree(mc_saved_p);
249
250         return ret;
251 }
252
253 /*
254  * A microcode patch in ucode_ptr is saved into mc_saved
255  * - if it has matching signature and newer revision compared to an existing
256  *   patch mc_saved.
257  * - or if it is a newly discovered microcode patch.
258  *
259  * The microcode patch should have matching model with CPU.
260  */
261 static void _save_mc(struct microcode_intel **mc_saved, u8 *ucode_ptr,
262                      unsigned int *mc_saved_count_p)
263 {
264         int i;
265         int found = 0;
266         unsigned int mc_saved_count = *mc_saved_count_p;
267         struct microcode_header_intel *mc_header;
268
269         mc_header = (struct microcode_header_intel *)ucode_ptr;
270         for (i = 0; i < mc_saved_count; i++) {
271                 unsigned int sig, pf;
272                 unsigned int new_rev;
273                 struct microcode_header_intel *mc_saved_header =
274                              (struct microcode_header_intel *)mc_saved[i];
275                 sig = mc_saved_header->sig;
276                 pf = mc_saved_header->pf;
277                 new_rev = mc_header->rev;
278
279                 if (get_matching_sig(sig, pf, ucode_ptr, new_rev)) {
280                         found = 1;
281                         if (update_match_revision(mc_header, new_rev)) {
282                                 /*
283                                  * Found an older ucode saved before.
284                                  * Replace the older one with this newer
285                                  * one.
286                                  */
287                                 mc_saved[i] =
288                                         (struct microcode_intel *)ucode_ptr;
289                                 break;
290                         }
291                 }
292         }
293         if (i >= mc_saved_count && !found)
294                 /*
295                  * This ucode is first time discovered in ucode file.
296                  * Save it to memory.
297                  */
298                 mc_saved[mc_saved_count++] =
299                                  (struct microcode_intel *)ucode_ptr;
300
301         *mc_saved_count_p = mc_saved_count;
302 }
303
304 /*
305  * Get microcode matching with BSP's model. Only CPUs with the same model as
306  * BSP can stay in the platform.
307  */
308 static enum ucode_state __init
309 get_matching_model_microcode(int cpu, unsigned long start,
310                              void *data, size_t size,
311                              struct mc_saved_data *mc_saved_data,
312                              unsigned long *mc_saved_in_initrd,
313                              struct ucode_cpu_info *uci)
314 {
315         u8 *ucode_ptr = data;
316         unsigned int leftover = size;
317         enum ucode_state state = UCODE_OK;
318         unsigned int mc_size;
319         struct microcode_header_intel *mc_header;
320         struct microcode_intel *mc_saved_tmp[MAX_UCODE_COUNT];
321         unsigned int mc_saved_count = mc_saved_data->mc_saved_count;
322         int i;
323
324         while (leftover && mc_saved_count < ARRAY_SIZE(mc_saved_tmp)) {
325
326                 if (leftover < sizeof(mc_header))
327                         break;
328
329                 mc_header = (struct microcode_header_intel *)ucode_ptr;
330
331                 mc_size = get_totalsize(mc_header);
332                 if (!mc_size || mc_size > leftover ||
333                         microcode_sanity_check(ucode_ptr, 0) < 0)
334                         break;
335
336                 leftover -= mc_size;
337
338                 /*
339                  * Since APs with same family and model as the BSP may boot in
340                  * the platform, we need to find and save microcode patches
341                  * with the same family and model as the BSP.
342                  */
343                 if (matching_model_microcode(mc_header, uci->cpu_sig.sig) !=
344                          UCODE_OK) {
345                         ucode_ptr += mc_size;
346                         continue;
347                 }
348
349                 _save_mc(mc_saved_tmp, ucode_ptr, &mc_saved_count);
350
351                 ucode_ptr += mc_size;
352         }
353
354         if (leftover) {
355                 state = UCODE_ERROR;
356                 goto out;
357         }
358
359         if (mc_saved_count == 0) {
360                 state = UCODE_NFOUND;
361                 goto out;
362         }
363
364         for (i = 0; i < mc_saved_count; i++)
365                 mc_saved_in_initrd[i] = (unsigned long)mc_saved_tmp[i] - start;
366
367         mc_saved_data->mc_saved_count = mc_saved_count;
368 out:
369         return state;
370 }
371
372 static int collect_cpu_info_early(struct ucode_cpu_info *uci)
373 {
374         unsigned int val[2];
375         u8 x86, x86_model;
376         struct cpu_signature csig;
377         unsigned int eax, ebx, ecx, edx;
378
379         csig.sig = 0;
380         csig.pf = 0;
381         csig.rev = 0;
382
383         memset(uci, 0, sizeof(*uci));
384
385         eax = 0x00000001;
386         ecx = 0;
387         native_cpuid(&eax, &ebx, &ecx, &edx);
388         csig.sig = eax;
389
390         x86 = get_x86_family(csig.sig);
391         x86_model = get_x86_model(csig.sig);
392
393         if ((x86_model >= 5) || (x86 > 6)) {
394                 /* get processor flags from MSR 0x17 */
395                 native_rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]);
396                 csig.pf = 1 << ((val[1] >> 18) & 7);
397         }
398         native_wrmsr(MSR_IA32_UCODE_REV, 0, 0);
399
400         /* As documented in the SDM: Do a CPUID 1 here */
401         sync_core();
402
403         /* get the current revision from MSR 0x8B */
404         native_rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
405
406         csig.rev = val[1];
407
408         uci->cpu_sig = csig;
409         uci->valid = 1;
410
411         return 0;
412 }
413
414 #ifdef DEBUG
415 static void __ref show_saved_mc(void)
416 {
417         int i, j;
418         unsigned int sig, pf, rev, total_size, data_size, date;
419         struct ucode_cpu_info uci;
420
421         if (mc_saved_data.mc_saved_count == 0) {
422                 pr_debug("no microcode data saved.\n");
423                 return;
424         }
425         pr_debug("Total microcode saved: %d\n", mc_saved_data.mc_saved_count);
426
427         collect_cpu_info_early(&uci);
428
429         sig = uci.cpu_sig.sig;
430         pf = uci.cpu_sig.pf;
431         rev = uci.cpu_sig.rev;
432         pr_debug("CPU%d: sig=0x%x, pf=0x%x, rev=0x%x\n",
433                  smp_processor_id(), sig, pf, rev);
434
435         for (i = 0; i < mc_saved_data.mc_saved_count; i++) {
436                 struct microcode_header_intel *mc_saved_header;
437                 struct extended_sigtable *ext_header;
438                 int ext_sigcount;
439                 struct extended_signature *ext_sig;
440
441                 mc_saved_header = (struct microcode_header_intel *)
442                                   mc_saved_data.mc_saved[i];
443                 sig = mc_saved_header->sig;
444                 pf = mc_saved_header->pf;
445                 rev = mc_saved_header->rev;
446                 total_size = get_totalsize(mc_saved_header);
447                 data_size = get_datasize(mc_saved_header);
448                 date = mc_saved_header->date;
449
450                 pr_debug("mc_saved[%d]: sig=0x%x, pf=0x%x, rev=0x%x, toal size=0x%x, date = %04x-%02x-%02x\n",
451                          i, sig, pf, rev, total_size,
452                          date & 0xffff,
453                          date >> 24,
454                          (date >> 16) & 0xff);
455
456                 /* Look for ext. headers: */
457                 if (total_size <= data_size + MC_HEADER_SIZE)
458                         continue;
459
460                 ext_header = (struct extended_sigtable *)
461                              mc_saved_header + data_size + MC_HEADER_SIZE;
462                 ext_sigcount = ext_header->count;
463                 ext_sig = (void *)ext_header + EXT_HEADER_SIZE;
464
465                 for (j = 0; j < ext_sigcount; j++) {
466                         sig = ext_sig->sig;
467                         pf = ext_sig->pf;
468
469                         pr_debug("\tExtended[%d]: sig=0x%x, pf=0x%x\n",
470                                  j, sig, pf);
471
472                         ext_sig++;
473                 }
474
475         }
476 }
477 #else
478 static inline void show_saved_mc(void)
479 {
480 }
481 #endif
482
483 #if defined(CONFIG_MICROCODE_INTEL_EARLY) && defined(CONFIG_HOTPLUG_CPU)
484 static DEFINE_MUTEX(x86_cpu_microcode_mutex);
485 /*
486  * Save this mc into mc_saved_data. So it will be loaded early when a CPU is
487  * hot added or resumes.
488  *
489  * Please make sure this mc should be a valid microcode patch before calling
490  * this function.
491  */
492 int save_mc_for_early(u8 *mc)
493 {
494         struct microcode_intel *mc_saved_tmp[MAX_UCODE_COUNT];
495         unsigned int mc_saved_count_init;
496         unsigned int mc_saved_count;
497         struct microcode_intel **mc_saved;
498         int ret = 0;
499         int i;
500
501         /*
502          * Hold hotplug lock so mc_saved_data is not accessed by a CPU in
503          * hotplug.
504          */
505         mutex_lock(&x86_cpu_microcode_mutex);
506
507         mc_saved_count_init = mc_saved_data.mc_saved_count;
508         mc_saved_count = mc_saved_data.mc_saved_count;
509         mc_saved = mc_saved_data.mc_saved;
510
511         if (mc_saved && mc_saved_count)
512                 memcpy(mc_saved_tmp, mc_saved,
513                        mc_saved_count * sizeof(struct microcode_intel *));
514         /*
515          * Save the microcode patch mc in mc_save_tmp structure if it's a newer
516          * version.
517          */
518
519         _save_mc(mc_saved_tmp, mc, &mc_saved_count);
520
521         /*
522          * Save the mc_save_tmp in global mc_saved_data.
523          */
524         ret = save_microcode(&mc_saved_data, mc_saved_tmp, mc_saved_count);
525         if (ret) {
526                 pr_err("Cannot save microcode patch.\n");
527                 goto out;
528         }
529
530         show_saved_mc();
531
532         /*
533          * Free old saved microcode data.
534          */
535         if (mc_saved) {
536                 for (i = 0; i < mc_saved_count_init; i++)
537                         kfree(mc_saved[i]);
538                 kfree(mc_saved);
539         }
540
541 out:
542         mutex_unlock(&x86_cpu_microcode_mutex);
543
544         return ret;
545 }
546 EXPORT_SYMBOL_GPL(save_mc_for_early);
547 #endif
548
549 static __initdata char ucode_name[] = "kernel/x86/microcode/GenuineIntel.bin";
550 static __init enum ucode_state
551 scan_microcode(unsigned long start, unsigned long end,
552                 struct mc_saved_data *mc_saved_data,
553                 unsigned long *mc_saved_in_initrd,
554                 struct ucode_cpu_info *uci)
555 {
556         unsigned int size = end - start + 1;
557         struct cpio_data cd;
558         long offset = 0;
559 #ifdef CONFIG_X86_32
560         char *p = (char *)__pa_nodebug(ucode_name);
561 #else
562         char *p = ucode_name;
563 #endif
564
565         cd.data = NULL;
566         cd.size = 0;
567
568         cd = find_cpio_data(p, (void *)start, size, &offset);
569         if (!cd.data)
570                 return UCODE_ERROR;
571
572
573         return get_matching_model_microcode(0, start, cd.data, cd.size,
574                                             mc_saved_data, mc_saved_in_initrd,
575                                             uci);
576 }
577
578 /*
579  * Print ucode update info.
580  */
581 static void
582 print_ucode_info(struct ucode_cpu_info *uci, unsigned int date)
583 {
584         int cpu = smp_processor_id();
585
586         pr_info("CPU%d microcode updated early to revision 0x%x, date = %04x-%02x-%02x\n",
587                 cpu,
588                 uci->cpu_sig.rev,
589                 date & 0xffff,
590                 date >> 24,
591                 (date >> 16) & 0xff);
592 }
593
594 #ifdef CONFIG_X86_32
595
596 static int delay_ucode_info;
597 static int current_mc_date;
598
599 /*
600  * Print early updated ucode info after printk works. This is delayed info dump.
601  */
602 void show_ucode_info_early(void)
603 {
604         struct ucode_cpu_info uci;
605
606         if (delay_ucode_info) {
607                 collect_cpu_info_early(&uci);
608                 print_ucode_info(&uci, current_mc_date);
609                 delay_ucode_info = 0;
610         }
611 }
612
613 /*
614  * At this point, we can not call printk() yet. Keep microcode patch number in
615  * mc_saved_data.mc_saved and delay printing microcode info in
616  * show_ucode_info_early() until printk() works.
617  */
618 static void print_ucode(struct ucode_cpu_info *uci)
619 {
620         struct microcode_intel *mc_intel;
621         int *delay_ucode_info_p;
622         int *current_mc_date_p;
623
624         mc_intel = uci->mc;
625         if (mc_intel == NULL)
626                 return;
627
628         delay_ucode_info_p = (int *)__pa_nodebug(&delay_ucode_info);
629         current_mc_date_p = (int *)__pa_nodebug(&current_mc_date);
630
631         *delay_ucode_info_p = 1;
632         *current_mc_date_p = mc_intel->hdr.date;
633 }
634 #else
635
636 /*
637  * Flush global tlb. We only do this in x86_64 where paging has been enabled
638  * already and PGE should be enabled as well.
639  */
640 static inline void flush_tlb_early(void)
641 {
642         __native_flush_tlb_global_irq_disabled();
643 }
644
645 static inline void print_ucode(struct ucode_cpu_info *uci)
646 {
647         struct microcode_intel *mc_intel;
648
649         mc_intel = uci->mc;
650         if (mc_intel == NULL)
651                 return;
652
653         print_ucode_info(uci, mc_intel->hdr.date);
654 }
655 #endif
656
657 static int apply_microcode_early(struct ucode_cpu_info *uci, bool early)
658 {
659         struct microcode_intel *mc_intel;
660         unsigned int val[2];
661
662         mc_intel = uci->mc;
663         if (mc_intel == NULL)
664                 return 0;
665
666         /* write microcode via MSR 0x79 */
667         native_wrmsr(MSR_IA32_UCODE_WRITE,
668               (unsigned long) mc_intel->bits,
669               (unsigned long) mc_intel->bits >> 16 >> 16);
670         native_wrmsr(MSR_IA32_UCODE_REV, 0, 0);
671
672         /* As documented in the SDM: Do a CPUID 1 here */
673         sync_core();
674
675         /* get the current revision from MSR 0x8B */
676         native_rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
677         if (val[1] != mc_intel->hdr.rev)
678                 return -1;
679
680 #ifdef CONFIG_X86_64
681         /* Flush global tlb. This is precaution. */
682         flush_tlb_early();
683 #endif
684         uci->cpu_sig.rev = val[1];
685
686         if (early)
687                 print_ucode(uci);
688         else
689                 print_ucode_info(uci, mc_intel->hdr.date);
690
691         return 0;
692 }
693
694 /*
695  * This function converts microcode patch offsets previously stored in
696  * mc_saved_in_initrd to pointers and stores the pointers in mc_saved_data.
697  */
698 int __init save_microcode_in_initrd_intel(void)
699 {
700         unsigned int count = mc_saved_data.mc_saved_count;
701         struct microcode_intel *mc_saved[MAX_UCODE_COUNT];
702         int ret = 0;
703
704         if (count == 0)
705                 return ret;
706
707         microcode_pointer(mc_saved, mc_saved_in_initrd, initrd_start, count);
708         ret = save_microcode(&mc_saved_data, mc_saved, count);
709         if (ret)
710                 pr_err("Cannot save microcode patches from initrd.\n");
711
712         show_saved_mc();
713
714         return ret;
715 }
716
717 static void __init
718 _load_ucode_intel_bsp(struct mc_saved_data *mc_saved_data,
719                       unsigned long *mc_saved_in_initrd,
720                       unsigned long initrd_start_early,
721                       unsigned long initrd_end_early,
722                       struct ucode_cpu_info *uci)
723 {
724         enum ucode_state ret;
725
726         collect_cpu_info_early(uci);
727         scan_microcode(initrd_start_early, initrd_end_early, mc_saved_data,
728                        mc_saved_in_initrd, uci);
729
730         ret = load_microcode(mc_saved_data, mc_saved_in_initrd,
731                              initrd_start_early, uci);
732
733         if (ret == UCODE_OK)
734                 apply_microcode_early(uci, true);
735 }
736
737 void __init
738 load_ucode_intel_bsp(void)
739 {
740         u64 ramdisk_image, ramdisk_size;
741         unsigned long initrd_start_early, initrd_end_early;
742         struct ucode_cpu_info uci;
743 #ifdef CONFIG_X86_32
744         struct boot_params *boot_params_p;
745
746         boot_params_p = (struct boot_params *)__pa_nodebug(&boot_params);
747         ramdisk_image = boot_params_p->hdr.ramdisk_image;
748         ramdisk_size  = boot_params_p->hdr.ramdisk_size;
749         initrd_start_early = ramdisk_image;
750         initrd_end_early = initrd_start_early + ramdisk_size;
751
752         _load_ucode_intel_bsp(
753                 (struct mc_saved_data *)__pa_nodebug(&mc_saved_data),
754                 (unsigned long *)__pa_nodebug(&mc_saved_in_initrd),
755                 initrd_start_early, initrd_end_early, &uci);
756 #else
757         ramdisk_image = boot_params.hdr.ramdisk_image;
758         ramdisk_size  = boot_params.hdr.ramdisk_size;
759         initrd_start_early = ramdisk_image + PAGE_OFFSET;
760         initrd_end_early = initrd_start_early + ramdisk_size;
761
762         _load_ucode_intel_bsp(&mc_saved_data, mc_saved_in_initrd,
763                               initrd_start_early, initrd_end_early,
764                               &uci);
765 #endif
766 }
767
768 void load_ucode_intel_ap(void)
769 {
770         struct mc_saved_data *mc_saved_data_p;
771         struct ucode_cpu_info uci;
772         unsigned long *mc_saved_in_initrd_p;
773         unsigned long initrd_start_addr;
774 #ifdef CONFIG_X86_32
775         unsigned long *initrd_start_p;
776
777         mc_saved_in_initrd_p =
778                 (unsigned long *)__pa_nodebug(mc_saved_in_initrd);
779         mc_saved_data_p = (struct mc_saved_data *)__pa_nodebug(&mc_saved_data);
780         initrd_start_p = (unsigned long *)__pa_nodebug(&initrd_start);
781         initrd_start_addr = (unsigned long)__pa_nodebug(*initrd_start_p);
782 #else
783         mc_saved_data_p = &mc_saved_data;
784         mc_saved_in_initrd_p = mc_saved_in_initrd;
785         initrd_start_addr = initrd_start;
786 #endif
787
788         /*
789          * If there is no valid ucode previously saved in memory, no need to
790          * update ucode on this AP.
791          */
792         if (mc_saved_data_p->mc_saved_count == 0)
793                 return;
794
795         collect_cpu_info_early(&uci);
796         load_microcode(mc_saved_data_p, mc_saved_in_initrd_p,
797                        initrd_start_addr, &uci);
798         apply_microcode_early(&uci, true);
799 }
800
801 void reload_ucode_intel(void)
802 {
803         struct ucode_cpu_info uci;
804         enum ucode_state ret;
805
806         if (!mc_saved_data.mc_saved_count)
807                 return;
808
809         collect_cpu_info_early(&uci);
810
811         ret = generic_load_microcode_early(mc_saved_data.mc_saved,
812                                            mc_saved_data.mc_saved_count, &uci);
813         if (ret != UCODE_OK)
814                 return;
815
816         apply_microcode_early(&uci, false);
817 }