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 unsigned long mc_saved_in_initrd
[MAX_UCODE_COUNT
];
32 struct mc_saved_data
{
33 unsigned int mc_saved_count
;
34 struct microcode_intel
**mc_saved
;
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
)
42 struct microcode_intel
*ucode_ptr
, *new_mc
= NULL
;
43 int new_rev
= uci
->cpu_sig
.rev
;
44 enum ucode_state state
= UCODE_OK
;
46 struct microcode_header_intel
*mc_header
;
47 unsigned int csig
= uci
->cpu_sig
.sig
;
48 unsigned int cpf
= uci
->cpu_sig
.pf
;
51 for (i
= 0; i
< mc_saved_count
; i
++) {
52 ucode_ptr
= mc_saved_p
[i
];
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
;
67 uci
->mc
= (struct microcode_intel
*)new_mc
;
73 microcode_pointer(struct microcode_intel
**mc_saved
,
74 unsigned long *mc_saved_in_initrd
,
75 unsigned long initrd_start
, int mc_saved_count
)
79 for (i
= 0; i
< mc_saved_count
; i
++)
80 mc_saved
[i
] = (struct microcode_intel
*)
81 (mc_saved_in_initrd
[i
] + initrd_start
);
86 microcode_phys(struct microcode_intel
**mc_saved_tmp
,
87 struct mc_saved_data
*mc_saved_data
)
90 struct microcode_intel
***mc_saved
;
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
;
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
);
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
)
110 struct microcode_intel
*mc_saved_tmp
[MAX_UCODE_COUNT
];
111 unsigned int count
= mc_saved_data
->mc_saved_count
;
113 if (!mc_saved_data
->mc_saved
) {
114 microcode_pointer(mc_saved_tmp
, mc_saved_in_initrd
,
115 initrd_start
, count
);
117 return generic_load_microcode_early(mc_saved_tmp
, count
, uci
);
120 microcode_phys(mc_saved_tmp
, mc_saved_data
);
121 return generic_load_microcode_early(mc_saved_tmp
, count
, uci
);
123 return generic_load_microcode_early(mc_saved_data
->mc_saved
,
129 static u8
get_x86_family(unsigned long sig
)
133 x86
= (sig
>> 8) & 0xf;
136 x86
+= (sig
>> 20) & 0xff;
141 static u8
get_x86_model(unsigned long sig
)
145 x86
= get_x86_family(sig
);
146 x86_model
= (sig
>> 4) & 0xf;
148 if (x86
== 0x6 || x86
== 0xf)
149 x86_model
+= ((sig
>> 16) & 0xf) << 4;
155 * Given CPU signature and a microcode patch, this function finds if the
156 * microcode patch has matching family and model with the CPU.
158 static enum ucode_state
159 matching_model_microcode(struct microcode_header_intel
*mc_header
,
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
);
168 struct extended_signature
*ext_sig
;
170 x86
= get_x86_family(sig
);
171 x86_model
= get_x86_model(sig
);
173 x86_ucode
= get_x86_family(mc_header
->sig
);
174 x86_model_ucode
= get_x86_model(mc_header
->sig
);
176 if (x86
== x86_ucode
&& x86_model
== x86_model_ucode
)
179 /* Look for ext. headers: */
180 if (total_size
<= data_size
+ MC_HEADER_SIZE
)
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
;
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
);
192 if (x86
== x86_ucode
&& x86_model
== x86_model_ucode
)
202 save_microcode(struct mc_saved_data
*mc_saved_data
,
203 struct microcode_intel
**mc_saved_src
,
204 unsigned int mc_saved_count
)
207 struct microcode_intel
**mc_saved_p
;
214 * Copy new microcode data.
216 mc_saved_p
= kmalloc(mc_saved_count
*sizeof(struct microcode_intel
*),
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
]) {
230 if (!mc_saved_src
[i
]) {
234 memcpy(mc_saved_p
[i
], mc
, mc_size
);
238 * Point to newly saved microcode.
240 mc_saved_data
->mc_saved
= mc_saved_p
;
241 mc_saved_data
->mc_saved_count
= mc_saved_count
;
246 for (j
= 0; j
<= i
; j
++)
247 kfree(mc_saved_p
[j
]);
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
257 * - or if it is a newly discovered microcode patch.
259 * The microcode patch should have matching model with CPU.
261 static void _save_mc(struct microcode_intel
**mc_saved
, u8
*ucode_ptr
,
262 unsigned int *mc_saved_count_p
)
266 unsigned int mc_saved_count
= *mc_saved_count_p
;
267 struct microcode_header_intel
*mc_header
;
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
;
279 if (get_matching_sig(sig
, pf
, ucode_ptr
, new_rev
)) {
281 if (update_match_revision(mc_header
, new_rev
)) {
283 * Found an older ucode saved before.
284 * Replace the older one with this newer
288 (struct microcode_intel
*)ucode_ptr
;
293 if (i
>= mc_saved_count
&& !found
)
295 * This ucode is first time discovered in ucode file.
298 mc_saved
[mc_saved_count
++] =
299 (struct microcode_intel
*)ucode_ptr
;
301 *mc_saved_count_p
= mc_saved_count
;
305 * Get microcode matching with BSP's model. Only CPUs with the same model as
306 * BSP can stay in the platform.
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
)
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
;
325 mc_header
= (struct microcode_header_intel
*)ucode_ptr
;
327 mc_size
= get_totalsize(mc_header
);
328 if (!mc_size
|| mc_size
> leftover
||
329 microcode_sanity_check(ucode_ptr
, 0) < 0)
335 * Since APs with same family and model as the BSP may boot in
336 * the platform, we need to find and save microcode patches
337 * with the same family and model as the BSP.
339 if (matching_model_microcode(mc_header
, uci
->cpu_sig
.sig
) !=
341 ucode_ptr
+= mc_size
;
345 _save_mc(mc_saved_tmp
, ucode_ptr
, &mc_saved_count
);
347 ucode_ptr
+= mc_size
;
355 if (mc_saved_count
== 0) {
356 state
= UCODE_NFOUND
;
360 for (i
= 0; i
< mc_saved_count
; i
++)
361 mc_saved_in_initrd
[i
] = (unsigned long)mc_saved_tmp
[i
] - start
;
363 mc_saved_data
->mc_saved_count
= mc_saved_count
;
368 #define native_rdmsr(msr, val1, val2) \
370 u64 __val = native_read_msr((msr)); \
371 (void)((val1) = (u32)__val); \
372 (void)((val2) = (u32)(__val >> 32)); \
375 #define native_wrmsr(msr, low, high) \
376 native_write_msr(msr, low, high);
378 static int collect_cpu_info_early(struct ucode_cpu_info
*uci
)
382 struct cpu_signature csig
;
383 unsigned int eax
, ebx
, ecx
, edx
;
389 memset(uci
, 0, sizeof(*uci
));
393 native_cpuid(&eax
, &ebx
, &ecx
, &edx
);
396 x86
= get_x86_family(csig
.sig
);
397 x86_model
= get_x86_model(csig
.sig
);
399 if ((x86_model
>= 5) || (x86
> 6)) {
400 /* get processor flags from MSR 0x17 */
401 native_rdmsr(MSR_IA32_PLATFORM_ID
, val
[0], val
[1]);
402 csig
.pf
= 1 << ((val
[1] >> 18) & 7);
404 native_wrmsr(MSR_IA32_UCODE_REV
, 0, 0);
406 /* As documented in the SDM: Do a CPUID 1 here */
409 /* get the current revision from MSR 0x8B */
410 native_rdmsr(MSR_IA32_UCODE_REV
, val
[0], val
[1]);
421 static void __ref
show_saved_mc(void)
424 unsigned int sig
, pf
, rev
, total_size
, data_size
, date
;
425 struct ucode_cpu_info uci
;
427 if (mc_saved_data
.mc_saved_count
== 0) {
428 pr_debug("no micorcode data saved.\n");
431 pr_debug("Total microcode saved: %d\n", mc_saved_data
.mc_saved_count
);
433 collect_cpu_info_early(&uci
);
435 sig
= uci
.cpu_sig
.sig
;
437 rev
= uci
.cpu_sig
.rev
;
438 pr_debug("CPU%d: sig=0x%x, pf=0x%x, rev=0x%x\n",
439 smp_processor_id(), sig
, pf
, rev
);
441 for (i
= 0; i
< mc_saved_data
.mc_saved_count
; i
++) {
442 struct microcode_header_intel
*mc_saved_header
;
443 struct extended_sigtable
*ext_header
;
445 struct extended_signature
*ext_sig
;
447 mc_saved_header
= (struct microcode_header_intel
*)
448 mc_saved_data
.mc_saved
[i
];
449 sig
= mc_saved_header
->sig
;
450 pf
= mc_saved_header
->pf
;
451 rev
= mc_saved_header
->rev
;
452 total_size
= get_totalsize(mc_saved_header
);
453 data_size
= get_datasize(mc_saved_header
);
454 date
= mc_saved_header
->date
;
456 pr_debug("mc_saved[%d]: sig=0x%x, pf=0x%x, rev=0x%x, toal size=0x%x, date = %04x-%02x-%02x\n",
457 i
, sig
, pf
, rev
, total_size
,
460 (date
>> 16) & 0xff);
462 /* Look for ext. headers: */
463 if (total_size
<= data_size
+ MC_HEADER_SIZE
)
466 ext_header
= (struct extended_sigtable
*)
467 mc_saved_header
+ data_size
+ MC_HEADER_SIZE
;
468 ext_sigcount
= ext_header
->count
;
469 ext_sig
= (void *)ext_header
+ EXT_HEADER_SIZE
;
471 for (j
= 0; j
< ext_sigcount
; j
++) {
475 pr_debug("\tExtended[%d]: sig=0x%x, pf=0x%x\n",
484 static inline void show_saved_mc(void)
489 #if defined(CONFIG_MICROCODE_INTEL_EARLY) && defined(CONFIG_HOTPLUG_CPU)
490 static DEFINE_MUTEX(x86_cpu_microcode_mutex
);
492 * Save this mc into mc_saved_data. So it will be loaded early when a CPU is
493 * hot added or resumes.
495 * Please make sure this mc should be a valid microcode patch before calling
498 int save_mc_for_early(u8
*mc
)
500 struct microcode_intel
*mc_saved_tmp
[MAX_UCODE_COUNT
];
501 unsigned int mc_saved_count_init
;
502 unsigned int mc_saved_count
;
503 struct microcode_intel
**mc_saved
;
508 * Hold hotplug lock so mc_saved_data is not accessed by a CPU in
511 mutex_lock(&x86_cpu_microcode_mutex
);
513 mc_saved_count_init
= mc_saved_data
.mc_saved_count
;
514 mc_saved_count
= mc_saved_data
.mc_saved_count
;
515 mc_saved
= mc_saved_data
.mc_saved
;
517 if (mc_saved
&& mc_saved_count
)
518 memcpy(mc_saved_tmp
, mc_saved
,
519 mc_saved_count
* sizeof(struct mirocode_intel
*));
521 * Save the microcode patch mc in mc_save_tmp structure if it's a newer
525 _save_mc(mc_saved_tmp
, mc
, &mc_saved_count
);
528 * Save the mc_save_tmp in global mc_saved_data.
530 ret
= save_microcode(&mc_saved_data
, mc_saved_tmp
, mc_saved_count
);
532 pr_err("Cannot save microcode patch.\n");
539 * Free old saved microcod data.
542 for (i
= 0; i
< mc_saved_count_init
; i
++)
548 mutex_unlock(&x86_cpu_microcode_mutex
);
552 EXPORT_SYMBOL_GPL(save_mc_for_early
);
555 static __initdata
char ucode_name
[] = "kernel/x86/microcode/GenuineIntel.bin";
556 static __init
enum ucode_state
557 scan_microcode(unsigned long start
, unsigned long end
,
558 struct mc_saved_data
*mc_saved_data
,
559 unsigned long *mc_saved_in_initrd
,
560 struct ucode_cpu_info
*uci
)
562 unsigned int size
= end
- start
+ 1;
566 char *p
= (char *)__pa_nodebug(ucode_name
);
568 char *p
= ucode_name
;
574 cd
= find_cpio_data(p
, (void *)start
, size
, &offset
);
579 return get_matching_model_microcode(0, start
, cd
.data
, cd
.size
,
580 mc_saved_data
, mc_saved_in_initrd
,
585 * Print ucode update info.
588 print_ucode_info(struct ucode_cpu_info
*uci
, unsigned int date
)
590 int cpu
= smp_processor_id();
592 pr_info("CPU%d microcode updated early to revision 0x%x, date = %04x-%02x-%02x\n",
597 (date
>> 16) & 0xff);
602 static int delay_ucode_info
;
603 static int current_mc_date
;
606 * Print early updated ucode info after printk works. This is delayed info dump.
608 void show_ucode_info_early(void)
610 struct ucode_cpu_info uci
;
612 if (delay_ucode_info
) {
613 collect_cpu_info_early(&uci
);
614 print_ucode_info(&uci
, current_mc_date
);
615 delay_ucode_info
= 0;
620 * At this point, we can not call printk() yet. Keep microcode patch number in
621 * mc_saved_data.mc_saved and delay printing microcode info in
622 * show_ucode_info_early() until printk() works.
624 static void print_ucode(struct ucode_cpu_info
*uci
)
626 struct microcode_intel
*mc_intel
;
627 int *delay_ucode_info_p
;
628 int *current_mc_date_p
;
631 if (mc_intel
== NULL
)
634 delay_ucode_info_p
= (int *)__pa_nodebug(&delay_ucode_info
);
635 current_mc_date_p
= (int *)__pa_nodebug(¤t_mc_date
);
637 *delay_ucode_info_p
= 1;
638 *current_mc_date_p
= mc_intel
->hdr
.date
;
643 * Flush global tlb. We only do this in x86_64 where paging has been enabled
644 * already and PGE should be enabled as well.
646 static inline void flush_tlb_early(void)
648 __native_flush_tlb_global_irq_disabled();
651 static inline void print_ucode(struct ucode_cpu_info
*uci
)
653 struct microcode_intel
*mc_intel
;
656 if (mc_intel
== NULL
)
659 print_ucode_info(uci
, mc_intel
->hdr
.date
);
663 static int apply_microcode_early(struct mc_saved_data
*mc_saved_data
,
664 struct ucode_cpu_info
*uci
)
666 struct microcode_intel
*mc_intel
;
670 if (mc_intel
== NULL
)
673 /* write microcode via MSR 0x79 */
674 native_wrmsr(MSR_IA32_UCODE_WRITE
,
675 (unsigned long) mc_intel
->bits
,
676 (unsigned long) mc_intel
->bits
>> 16 >> 16);
677 native_wrmsr(MSR_IA32_UCODE_REV
, 0, 0);
679 /* As documented in the SDM: Do a CPUID 1 here */
682 /* get the current revision from MSR 0x8B */
683 native_rdmsr(MSR_IA32_UCODE_REV
, val
[0], val
[1]);
684 if (val
[1] != mc_intel
->hdr
.rev
)
688 /* Flush global tlb. This is precaution. */
691 uci
->cpu_sig
.rev
= val
[1];
699 * This function converts microcode patch offsets previously stored in
700 * mc_saved_in_initrd to pointers and stores the pointers in mc_saved_data.
702 int __init
save_microcode_in_initrd_intel(void)
704 unsigned int count
= mc_saved_data
.mc_saved_count
;
705 struct microcode_intel
*mc_saved
[MAX_UCODE_COUNT
];
711 microcode_pointer(mc_saved
, mc_saved_in_initrd
, initrd_start
, count
);
712 ret
= save_microcode(&mc_saved_data
, mc_saved
, count
);
714 pr_err("Cannot save microcode patches from initrd.\n");
722 _load_ucode_intel_bsp(struct mc_saved_data
*mc_saved_data
,
723 unsigned long *mc_saved_in_initrd
,
724 unsigned long initrd_start_early
,
725 unsigned long initrd_end_early
,
726 struct ucode_cpu_info
*uci
)
728 collect_cpu_info_early(uci
);
729 scan_microcode(initrd_start_early
, initrd_end_early
, mc_saved_data
,
730 mc_saved_in_initrd
, uci
);
731 load_microcode(mc_saved_data
, mc_saved_in_initrd
,
732 initrd_start_early
, uci
);
733 apply_microcode_early(mc_saved_data
, uci
);
737 load_ucode_intel_bsp(void)
739 u64 ramdisk_image
, ramdisk_size
;
740 unsigned long initrd_start_early
, initrd_end_early
;
741 struct ucode_cpu_info uci
;
743 struct boot_params
*boot_params_p
;
745 boot_params_p
= (struct boot_params
*)__pa_nodebug(&boot_params
);
746 ramdisk_image
= boot_params_p
->hdr
.ramdisk_image
;
747 ramdisk_size
= boot_params_p
->hdr
.ramdisk_size
;
748 initrd_start_early
= ramdisk_image
;
749 initrd_end_early
= initrd_start_early
+ ramdisk_size
;
751 _load_ucode_intel_bsp(
752 (struct mc_saved_data
*)__pa_nodebug(&mc_saved_data
),
753 (unsigned long *)__pa_nodebug(&mc_saved_in_initrd
),
754 initrd_start_early
, initrd_end_early
, &uci
);
756 ramdisk_image
= boot_params
.hdr
.ramdisk_image
;
757 ramdisk_size
= boot_params
.hdr
.ramdisk_size
;
758 initrd_start_early
= ramdisk_image
+ PAGE_OFFSET
;
759 initrd_end_early
= initrd_start_early
+ ramdisk_size
;
761 _load_ucode_intel_bsp(&mc_saved_data
, mc_saved_in_initrd
,
762 initrd_start_early
, initrd_end_early
, &uci
);
766 void load_ucode_intel_ap(void)
768 struct mc_saved_data
*mc_saved_data_p
;
769 struct ucode_cpu_info uci
;
770 unsigned long *mc_saved_in_initrd_p
;
771 unsigned long initrd_start_addr
;
773 unsigned long *initrd_start_p
;
775 mc_saved_in_initrd_p
=
776 (unsigned long *)__pa_nodebug(mc_saved_in_initrd
);
777 mc_saved_data_p
= (struct mc_saved_data
*)__pa_nodebug(&mc_saved_data
);
778 initrd_start_p
= (unsigned long *)__pa_nodebug(&initrd_start
);
779 initrd_start_addr
= (unsigned long)__pa_nodebug(*initrd_start_p
);
781 mc_saved_data_p
= &mc_saved_data
;
782 mc_saved_in_initrd_p
= mc_saved_in_initrd
;
783 initrd_start_addr
= initrd_start
;
787 * If there is no valid ucode previously saved in memory, no need to
788 * update ucode on this AP.
790 if (mc_saved_data_p
->mc_saved_count
== 0)
793 collect_cpu_info_early(&uci
);
794 load_microcode(mc_saved_data_p
, mc_saved_in_initrd_p
,
795 initrd_start_addr
, &uci
);
796 apply_microcode_early(mc_saved_data_p
, &uci
);