7bcad1aacce353df7e75113a6cfc38007c3dd3e6
2 * Intel CPU microcode early update for Linux
4 * Copyright (C) 2012 Fenghua Yu <fenghua.yu@intel.com>
5 * H Peter Anvin" <hpa@zytor.com>
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.
11 * Reference: Section 9.11 of Volume 3, IA-32 Intel Architecture
12 * Software Developer's Manual.
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.
19 #include <linux/module.h>
21 #include <linux/slab.h>
22 #include <linux/earlycpio.h>
23 #include <linux/initrd.h>
24 #include <linux/cpu.h>
26 #include <asm/microcode_intel.h>
27 #include <asm/processor.h>
28 #include <asm/tlbflush.h>
29 #include <asm/setup.h>
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
;
37 static enum ucode_state
38 load_microcode_early(struct microcode_intel
**saved
,
39 unsigned int num_saved
, struct ucode_cpu_info
*uci
)
41 struct microcode_intel
*ucode_ptr
, *new_mc
= NULL
;
42 struct microcode_header_intel
*mc_hdr
;
45 new_rev
= uci
->cpu_sig
.rev
;
47 for (i
= 0; i
< num_saved
; i
++) {
49 mc_hdr
= (struct microcode_header_intel
*)ucode_ptr
;
51 ret
= get_matching_microcode(uci
->cpu_sig
.sig
,
58 new_rev
= mc_hdr
->rev
;
65 uci
->mc
= (struct microcode_intel
*)new_mc
;
70 copy_initrd_ptrs(struct microcode_intel
**mc_saved
, unsigned long *initrd
,
71 unsigned long off
, int num_saved
)
75 for (i
= 0; i
< num_saved
; i
++)
76 mc_saved
[i
] = (struct microcode_intel
*)(initrd
[i
] + off
);
81 microcode_phys(struct microcode_intel
**mc_saved_tmp
,
82 struct mc_saved_data
*mc_saved_data
)
85 struct microcode_intel
***mc_saved
;
87 mc_saved
= (struct microcode_intel
***)
88 __pa_nodebug(&mc_saved_data
->mc_saved
);
89 for (i
= 0; i
< mc_saved_data
->mc_saved_count
; i
++) {
90 struct microcode_intel
*p
;
92 p
= *(struct microcode_intel
**)
93 __pa_nodebug(mc_saved_data
->mc_saved
+ i
);
94 mc_saved_tmp
[i
] = (struct microcode_intel
*)__pa_nodebug(p
);
99 static enum ucode_state
100 load_microcode(struct mc_saved_data
*mc_saved_data
, unsigned long *initrd
,
101 unsigned long initrd_start
, struct ucode_cpu_info
*uci
)
103 struct microcode_intel
*mc_saved_tmp
[MAX_UCODE_COUNT
];
104 unsigned int count
= mc_saved_data
->mc_saved_count
;
106 if (!mc_saved_data
->mc_saved
) {
107 copy_initrd_ptrs(mc_saved_tmp
, initrd
, initrd_start
, count
);
109 return load_microcode_early(mc_saved_tmp
, count
, uci
);
112 microcode_phys(mc_saved_tmp
, mc_saved_data
);
113 return load_microcode_early(mc_saved_tmp
, count
, uci
);
115 return load_microcode_early(mc_saved_data
->mc_saved
,
122 * Given CPU signature and a microcode patch, this function finds if the
123 * microcode patch has matching family and model with the CPU.
125 static enum ucode_state
126 matching_model_microcode(struct microcode_header_intel
*mc_header
,
129 unsigned int fam
, model
;
130 unsigned int fam_ucode
, model_ucode
;
131 struct extended_sigtable
*ext_header
;
132 unsigned long total_size
= get_totalsize(mc_header
);
133 unsigned long data_size
= get_datasize(mc_header
);
135 struct extended_signature
*ext_sig
;
137 fam
= __x86_family(sig
);
138 model
= x86_model(sig
);
140 fam_ucode
= __x86_family(mc_header
->sig
);
141 model_ucode
= x86_model(mc_header
->sig
);
143 if (fam
== fam_ucode
&& model
== model_ucode
)
146 /* Look for ext. headers: */
147 if (total_size
<= data_size
+ MC_HEADER_SIZE
)
150 ext_header
= (void *) mc_header
+ data_size
+ MC_HEADER_SIZE
;
151 ext_sig
= (void *)ext_header
+ EXT_HEADER_SIZE
;
152 ext_sigcount
= ext_header
->count
;
154 for (i
= 0; i
< ext_sigcount
; i
++) {
155 fam_ucode
= __x86_family(ext_sig
->sig
);
156 model_ucode
= x86_model(ext_sig
->sig
);
158 if (fam
== fam_ucode
&& model
== model_ucode
)
167 save_microcode(struct mc_saved_data
*mc_saved_data
,
168 struct microcode_intel
**mc_saved_src
,
169 unsigned int mc_saved_count
)
172 struct microcode_intel
**saved_ptr
;
179 * Copy new microcode data.
181 saved_ptr
= kcalloc(mc_saved_count
, sizeof(struct microcode_intel
*), GFP_KERNEL
);
185 for (i
= 0; i
< mc_saved_count
; i
++) {
186 struct microcode_header_intel
*mc_hdr
;
187 struct microcode_intel
*mc
;
190 if (!mc_saved_src
[i
]) {
195 mc
= mc_saved_src
[i
];
197 size
= get_totalsize(mc_hdr
);
199 saved_ptr
[i
] = kmalloc(size
, GFP_KERNEL
);
205 memcpy(saved_ptr
[i
], mc
, size
);
209 * Point to newly saved microcode.
211 mc_saved_data
->mc_saved
= saved_ptr
;
212 mc_saved_data
->mc_saved_count
= mc_saved_count
;
217 for (j
= 0; j
<= i
; j
++)
225 * A microcode patch in ucode_ptr is saved into mc_saved
226 * - if it has matching signature and newer revision compared to an existing
228 * - or if it is a newly discovered microcode patch.
230 * The microcode patch should have matching model with CPU.
232 * Returns: The updated number @num_saved of saved microcode patches.
234 static unsigned int _save_mc(struct microcode_intel
**mc_saved
,
235 u8
*ucode_ptr
, unsigned int num_saved
)
237 struct microcode_header_intel
*mc_hdr
, *mc_saved_hdr
;
238 unsigned int sig
, pf
, new_rev
;
241 mc_hdr
= (struct microcode_header_intel
*)ucode_ptr
;
243 for (i
= 0; i
< num_saved
; i
++) {
244 mc_saved_hdr
= (struct microcode_header_intel
*)mc_saved
[i
];
245 sig
= mc_saved_hdr
->sig
;
246 pf
= mc_saved_hdr
->pf
;
247 new_rev
= mc_hdr
->rev
;
249 if (!get_matching_sig(sig
, pf
, new_rev
, ucode_ptr
))
254 if (!revision_is_newer(mc_hdr
, new_rev
))
258 * Found an older ucode saved earlier. Replace it with
261 mc_saved
[i
] = (struct microcode_intel
*)ucode_ptr
;
265 /* Newly detected microcode, save it to memory. */
266 if (i
>= num_saved
&& !found
)
267 mc_saved
[num_saved
++] = (struct microcode_intel
*)ucode_ptr
;
273 * Get microcode matching with BSP's model. Only CPUs with the same model as
274 * BSP can stay in the platform.
276 static enum ucode_state __init
277 get_matching_model_microcode(int cpu
, unsigned long start
,
278 void *data
, size_t size
,
279 struct mc_saved_data
*mc_saved_data
,
280 unsigned long *mc_saved_in_initrd
,
281 struct ucode_cpu_info
*uci
)
283 u8
*ucode_ptr
= data
;
284 unsigned int leftover
= size
;
285 enum ucode_state state
= UCODE_OK
;
286 unsigned int mc_size
;
287 struct microcode_header_intel
*mc_header
;
288 struct microcode_intel
*mc_saved_tmp
[MAX_UCODE_COUNT
];
289 unsigned int mc_saved_count
= mc_saved_data
->mc_saved_count
;
292 while (leftover
&& mc_saved_count
< ARRAY_SIZE(mc_saved_tmp
)) {
294 if (leftover
< sizeof(mc_header
))
297 mc_header
= (struct microcode_header_intel
*)ucode_ptr
;
299 mc_size
= get_totalsize(mc_header
);
300 if (!mc_size
|| mc_size
> leftover
||
301 microcode_sanity_check(ucode_ptr
, 0) < 0)
307 * Since APs with same family and model as the BSP may boot in
308 * the platform, we need to find and save microcode patches
309 * with the same family and model as the BSP.
311 if (matching_model_microcode(mc_header
, uci
->cpu_sig
.sig
) !=
313 ucode_ptr
+= mc_size
;
317 mc_saved_count
= _save_mc(mc_saved_tmp
, ucode_ptr
, mc_saved_count
);
319 ucode_ptr
+= mc_size
;
327 if (mc_saved_count
== 0) {
328 state
= UCODE_NFOUND
;
332 for (i
= 0; i
< mc_saved_count
; i
++)
333 mc_saved_in_initrd
[i
] = (unsigned long)mc_saved_tmp
[i
] - start
;
335 mc_saved_data
->mc_saved_count
= mc_saved_count
;
340 static int collect_cpu_info_early(struct ucode_cpu_info
*uci
)
343 unsigned int family
, model
;
344 struct cpu_signature csig
;
345 unsigned int eax
, ebx
, ecx
, edx
;
351 memset(uci
, 0, sizeof(*uci
));
355 native_cpuid(&eax
, &ebx
, &ecx
, &edx
);
358 family
= __x86_family(csig
.sig
);
359 model
= x86_model(csig
.sig
);
361 if ((model
>= 5) || (family
> 6)) {
362 /* get processor flags from MSR 0x17 */
363 native_rdmsr(MSR_IA32_PLATFORM_ID
, val
[0], val
[1]);
364 csig
.pf
= 1 << ((val
[1] >> 18) & 7);
366 native_wrmsr(MSR_IA32_UCODE_REV
, 0, 0);
368 /* As documented in the SDM: Do a CPUID 1 here */
371 /* get the current revision from MSR 0x8B */
372 native_rdmsr(MSR_IA32_UCODE_REV
, val
[0], val
[1]);
383 static void __ref
show_saved_mc(void)
386 unsigned int sig
, pf
, rev
, total_size
, data_size
, date
;
387 struct ucode_cpu_info uci
;
389 if (mc_saved_data
.mc_saved_count
== 0) {
390 pr_debug("no microcode data saved.\n");
393 pr_debug("Total microcode saved: %d\n", mc_saved_data
.mc_saved_count
);
395 collect_cpu_info_early(&uci
);
397 sig
= uci
.cpu_sig
.sig
;
399 rev
= uci
.cpu_sig
.rev
;
400 pr_debug("CPU%d: sig=0x%x, pf=0x%x, rev=0x%x\n",
401 smp_processor_id(), sig
, pf
, rev
);
403 for (i
= 0; i
< mc_saved_data
.mc_saved_count
; i
++) {
404 struct microcode_header_intel
*mc_saved_header
;
405 struct extended_sigtable
*ext_header
;
407 struct extended_signature
*ext_sig
;
409 mc_saved_header
= (struct microcode_header_intel
*)
410 mc_saved_data
.mc_saved
[i
];
411 sig
= mc_saved_header
->sig
;
412 pf
= mc_saved_header
->pf
;
413 rev
= mc_saved_header
->rev
;
414 total_size
= get_totalsize(mc_saved_header
);
415 data_size
= get_datasize(mc_saved_header
);
416 date
= mc_saved_header
->date
;
418 pr_debug("mc_saved[%d]: sig=0x%x, pf=0x%x, rev=0x%x, toal size=0x%x, date = %04x-%02x-%02x\n",
419 i
, sig
, pf
, rev
, total_size
,
422 (date
>> 16) & 0xff);
424 /* Look for ext. headers: */
425 if (total_size
<= data_size
+ MC_HEADER_SIZE
)
428 ext_header
= (void *) mc_saved_header
+ data_size
+ MC_HEADER_SIZE
;
429 ext_sigcount
= ext_header
->count
;
430 ext_sig
= (void *)ext_header
+ EXT_HEADER_SIZE
;
432 for (j
= 0; j
< ext_sigcount
; j
++) {
436 pr_debug("\tExtended[%d]: sig=0x%x, pf=0x%x\n",
445 static inline void show_saved_mc(void)
450 #if defined(CONFIG_MICROCODE_INTEL_EARLY) && defined(CONFIG_HOTPLUG_CPU)
451 static DEFINE_MUTEX(x86_cpu_microcode_mutex
);
453 * Save this mc into mc_saved_data. So it will be loaded early when a CPU is
454 * hot added or resumes.
456 * Please make sure this mc should be a valid microcode patch before calling
459 int save_mc_for_early(u8
*mc
)
461 struct microcode_intel
*mc_saved_tmp
[MAX_UCODE_COUNT
];
462 unsigned int mc_saved_count_init
;
463 unsigned int mc_saved_count
;
464 struct microcode_intel
**mc_saved
;
469 * Hold hotplug lock so mc_saved_data is not accessed by a CPU in
472 mutex_lock(&x86_cpu_microcode_mutex
);
474 mc_saved_count_init
= mc_saved_data
.mc_saved_count
;
475 mc_saved_count
= mc_saved_data
.mc_saved_count
;
476 mc_saved
= mc_saved_data
.mc_saved
;
478 if (mc_saved
&& mc_saved_count
)
479 memcpy(mc_saved_tmp
, mc_saved
,
480 mc_saved_count
* sizeof(struct microcode_intel
*));
482 * Save the microcode patch mc in mc_save_tmp structure if it's a newer
485 mc_saved_count
= _save_mc(mc_saved_tmp
, mc
, mc_saved_count
);
488 * Save the mc_save_tmp in global mc_saved_data.
490 ret
= save_microcode(&mc_saved_data
, mc_saved_tmp
, mc_saved_count
);
492 pr_err("Cannot save microcode patch.\n");
499 * Free old saved microcode data.
502 for (i
= 0; i
< mc_saved_count_init
; i
++)
508 mutex_unlock(&x86_cpu_microcode_mutex
);
512 EXPORT_SYMBOL_GPL(save_mc_for_early
);
515 static __initdata
char ucode_name
[] = "kernel/x86/microcode/GenuineIntel.bin";
516 static __init
enum ucode_state
517 scan_microcode(unsigned long start
, unsigned long size
,
518 struct mc_saved_data
*mc_saved_data
,
519 unsigned long *mc_saved_in_initrd
, struct ucode_cpu_info
*uci
)
524 char *p
= (char *)__pa_nodebug(ucode_name
);
526 char *p
= ucode_name
;
532 cd
= find_cpio_data(p
, (void *)start
, size
, &offset
);
536 return get_matching_model_microcode(0, start
, cd
.data
, cd
.size
,
537 mc_saved_data
, mc_saved_in_initrd
,
542 * Print ucode update info.
545 print_ucode_info(struct ucode_cpu_info
*uci
, unsigned int date
)
547 int cpu
= smp_processor_id();
549 pr_info("CPU%d microcode updated early to revision 0x%x, date = %04x-%02x-%02x\n",
554 (date
>> 16) & 0xff);
559 static int delay_ucode_info
;
560 static int current_mc_date
;
563 * Print early updated ucode info after printk works. This is delayed info dump.
565 void show_ucode_info_early(void)
567 struct ucode_cpu_info uci
;
569 if (delay_ucode_info
) {
570 collect_cpu_info_early(&uci
);
571 print_ucode_info(&uci
, current_mc_date
);
572 delay_ucode_info
= 0;
577 * At this point, we can not call printk() yet. Keep microcode patch number in
578 * mc_saved_data.mc_saved and delay printing microcode info in
579 * show_ucode_info_early() until printk() works.
581 static void print_ucode(struct ucode_cpu_info
*uci
)
583 struct microcode_intel
*mc_intel
;
584 int *delay_ucode_info_p
;
585 int *current_mc_date_p
;
588 if (mc_intel
== NULL
)
591 delay_ucode_info_p
= (int *)__pa_nodebug(&delay_ucode_info
);
592 current_mc_date_p
= (int *)__pa_nodebug(¤t_mc_date
);
594 *delay_ucode_info_p
= 1;
595 *current_mc_date_p
= mc_intel
->hdr
.date
;
600 * Flush global tlb. We only do this in x86_64 where paging has been enabled
601 * already and PGE should be enabled as well.
603 static inline void flush_tlb_early(void)
605 __native_flush_tlb_global_irq_disabled();
608 static inline void print_ucode(struct ucode_cpu_info
*uci
)
610 struct microcode_intel
*mc_intel
;
613 if (mc_intel
== NULL
)
616 print_ucode_info(uci
, mc_intel
->hdr
.date
);
620 static int apply_microcode_early(struct ucode_cpu_info
*uci
, bool early
)
622 struct microcode_intel
*mc_intel
;
626 if (mc_intel
== NULL
)
629 /* write microcode via MSR 0x79 */
630 native_wrmsr(MSR_IA32_UCODE_WRITE
,
631 (unsigned long) mc_intel
->bits
,
632 (unsigned long) mc_intel
->bits
>> 16 >> 16);
633 native_wrmsr(MSR_IA32_UCODE_REV
, 0, 0);
635 /* As documented in the SDM: Do a CPUID 1 here */
638 /* get the current revision from MSR 0x8B */
639 native_rdmsr(MSR_IA32_UCODE_REV
, val
[0], val
[1]);
640 if (val
[1] != mc_intel
->hdr
.rev
)
644 /* Flush global tlb. This is precaution. */
647 uci
->cpu_sig
.rev
= val
[1];
652 print_ucode_info(uci
, mc_intel
->hdr
.date
);
658 * This function converts microcode patch offsets previously stored in
659 * mc_saved_in_initrd to pointers and stores the pointers in mc_saved_data.
661 int __init
save_microcode_in_initrd_intel(void)
663 unsigned int count
= mc_saved_data
.mc_saved_count
;
664 struct microcode_intel
*mc_saved
[MAX_UCODE_COUNT
];
670 copy_initrd_ptrs(mc_saved
, mc_saved_in_initrd
, initrd_start
, count
);
671 ret
= save_microcode(&mc_saved_data
, mc_saved
, count
);
673 pr_err("Cannot save microcode patches from initrd.\n");
681 _load_ucode_intel_bsp(struct mc_saved_data
*mc_saved_data
,
682 unsigned long *mc_saved_in_initrd
,
683 unsigned long start
, unsigned long size
)
685 struct ucode_cpu_info uci
;
686 enum ucode_state ret
;
688 collect_cpu_info_early(&uci
);
689 scan_microcode(start
, size
, mc_saved_data
, mc_saved_in_initrd
, &uci
);
691 ret
= load_microcode(mc_saved_data
, mc_saved_in_initrd
, start
, &uci
);
695 apply_microcode_early(&uci
, true);
698 void __init
load_ucode_intel_bsp(void)
702 struct boot_params
*p
;
704 p
= (struct boot_params
*)__pa_nodebug(&boot_params
);
705 start
= p
->hdr
.ramdisk_image
;
706 size
= p
->hdr
.ramdisk_size
;
708 _load_ucode_intel_bsp(
709 (struct mc_saved_data
*)__pa_nodebug(&mc_saved_data
),
710 (unsigned long *)__pa_nodebug(&mc_saved_in_initrd
),
713 start
= boot_params
.hdr
.ramdisk_image
+ PAGE_OFFSET
;
714 size
= boot_params
.hdr
.ramdisk_size
;
716 _load_ucode_intel_bsp(&mc_saved_data
, mc_saved_in_initrd
, start
, size
);
720 void load_ucode_intel_ap(void)
722 struct mc_saved_data
*mc_saved_data_p
;
723 struct ucode_cpu_info uci
;
724 unsigned long *mc_saved_in_initrd_p
;
725 unsigned long initrd_start_addr
;
726 enum ucode_state ret
;
728 unsigned long *initrd_start_p
;
730 mc_saved_in_initrd_p
=
731 (unsigned long *)__pa_nodebug(mc_saved_in_initrd
);
732 mc_saved_data_p
= (struct mc_saved_data
*)__pa_nodebug(&mc_saved_data
);
733 initrd_start_p
= (unsigned long *)__pa_nodebug(&initrd_start
);
734 initrd_start_addr
= (unsigned long)__pa_nodebug(*initrd_start_p
);
736 mc_saved_data_p
= &mc_saved_data
;
737 mc_saved_in_initrd_p
= mc_saved_in_initrd
;
738 initrd_start_addr
= initrd_start
;
742 * If there is no valid ucode previously saved in memory, no need to
743 * update ucode on this AP.
745 if (mc_saved_data_p
->mc_saved_count
== 0)
748 collect_cpu_info_early(&uci
);
749 ret
= load_microcode(mc_saved_data_p
, mc_saved_in_initrd_p
,
750 initrd_start_addr
, &uci
);
755 apply_microcode_early(&uci
, true);
758 void reload_ucode_intel(void)
760 struct ucode_cpu_info uci
;
761 enum ucode_state ret
;
763 if (!mc_saved_data
.mc_saved_count
)
766 collect_cpu_info_early(&uci
);
768 ret
= load_microcode_early(mc_saved_data
.mc_saved
,
769 mc_saved_data
.mc_saved_count
, &uci
);
773 apply_microcode_early(&uci
, false);
This page took 0.052972 seconds and 4 git commands to generate.