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