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 microcode_pointer(struct microcode_intel **mc_saved,
71 unsigned long *mc_saved_in_initrd,
72 unsigned long initrd_start, int mc_saved_count)
76 for (i = 0; i < mc_saved_count; i++)
77 mc_saved[i] = (struct microcode_intel *)
78 (mc_saved_in_initrd[i] + initrd_start);
83 microcode_phys(struct microcode_intel **mc_saved_tmp,
84 struct mc_saved_data *mc_saved_data)
87 struct microcode_intel ***mc_saved;
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;
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);
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)
107 struct microcode_intel *mc_saved_tmp[MAX_UCODE_COUNT];
108 unsigned int count = mc_saved_data->mc_saved_count;
110 if (!mc_saved_data->mc_saved) {
111 microcode_pointer(mc_saved_tmp, mc_saved_in_initrd,
112 initrd_start, count);
114 return load_microcode_early(mc_saved_tmp, count, uci);
117 microcode_phys(mc_saved_tmp, mc_saved_data);
118 return load_microcode_early(mc_saved_tmp, count, uci);
120 return load_microcode_early(mc_saved_data->mc_saved,
127 * Given CPU signature and a microcode patch, this function finds if the
128 * microcode patch has matching family and model with the CPU.
130 static enum ucode_state
131 matching_model_microcode(struct microcode_header_intel *mc_header,
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);
140 struct extended_signature *ext_sig;
142 fam = __x86_family(sig);
143 model = x86_model(sig);
145 fam_ucode = __x86_family(mc_header->sig);
146 model_ucode = x86_model(mc_header->sig);
148 if (fam == fam_ucode && model == model_ucode)
151 /* Look for ext. headers: */
152 if (total_size <= data_size + MC_HEADER_SIZE)
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;
159 for (i = 0; i < ext_sigcount; i++) {
160 fam_ucode = __x86_family(ext_sig->sig);
161 model_ucode = x86_model(ext_sig->sig);
163 if (fam == fam_ucode && model == model_ucode)
172 save_microcode(struct mc_saved_data *mc_saved_data,
173 struct microcode_intel **mc_saved_src,
174 unsigned int mc_saved_count)
177 struct microcode_intel **saved_ptr;
184 * Copy new microcode data.
186 saved_ptr = kcalloc(mc_saved_count, sizeof(struct microcode_intel *), GFP_KERNEL);
190 for (i = 0; i < mc_saved_count; i++) {
191 struct microcode_header_intel *mc_hdr;
192 struct microcode_intel *mc;
195 if (!mc_saved_src[i]) {
200 mc = mc_saved_src[i];
202 size = get_totalsize(mc_hdr);
204 saved_ptr[i] = kmalloc(size, GFP_KERNEL);
210 memcpy(saved_ptr[i], mc, size);
214 * Point to newly saved microcode.
216 mc_saved_data->mc_saved = saved_ptr;
217 mc_saved_data->mc_saved_count = mc_saved_count;
222 for (j = 0; j <= i; j++)
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
233 * - or if it is a newly discovered microcode patch.
235 * The microcode patch should have matching model with CPU.
237 * Returns: The updated number @num_saved of saved microcode patches.
239 static unsigned int _save_mc(struct microcode_intel **mc_saved,
240 u8 *ucode_ptr, unsigned int num_saved)
242 struct microcode_header_intel *mc_hdr, *mc_saved_hdr;
243 unsigned int sig, pf, new_rev;
246 mc_hdr = (struct microcode_header_intel *)ucode_ptr;
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;
254 if (!get_matching_sig(sig, pf, new_rev, ucode_ptr))
259 if (!revision_is_newer(mc_hdr, new_rev))
263 * Found an older ucode saved earlier. Replace it with
266 mc_saved[i] = (struct microcode_intel *)ucode_ptr;
270 /* Newly detected microcode, save it to memory. */
271 if (i >= num_saved && !found)
272 mc_saved[num_saved++] = (struct microcode_intel *)ucode_ptr;
278 * Get microcode matching with BSP's model. Only CPUs with the same model as
279 * BSP can stay in the platform.
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)
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;
297 while (leftover && mc_saved_count < ARRAY_SIZE(mc_saved_tmp)) {
299 if (leftover < sizeof(mc_header))
302 mc_header = (struct microcode_header_intel *)ucode_ptr;
304 mc_size = get_totalsize(mc_header);
305 if (!mc_size || mc_size > leftover ||
306 microcode_sanity_check(ucode_ptr, 0) < 0)
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.
316 if (matching_model_microcode(mc_header, uci->cpu_sig.sig) !=
318 ucode_ptr += mc_size;
322 mc_saved_count = _save_mc(mc_saved_tmp, ucode_ptr, mc_saved_count);
324 ucode_ptr += mc_size;
332 if (mc_saved_count == 0) {
333 state = UCODE_NFOUND;
337 for (i = 0; i < mc_saved_count; i++)
338 mc_saved_in_initrd[i] = (unsigned long)mc_saved_tmp[i] - start;
340 mc_saved_data->mc_saved_count = mc_saved_count;
345 static int collect_cpu_info_early(struct ucode_cpu_info *uci)
348 unsigned int family, model;
349 struct cpu_signature csig;
350 unsigned int eax, ebx, ecx, edx;
356 memset(uci, 0, sizeof(*uci));
360 native_cpuid(&eax, &ebx, &ecx, &edx);
363 family = __x86_family(csig.sig);
364 model = x86_model(csig.sig);
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);
371 native_wrmsr(MSR_IA32_UCODE_REV, 0, 0);
373 /* As documented in the SDM: Do a CPUID 1 here */
376 /* get the current revision from MSR 0x8B */
377 native_rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
388 static void __ref show_saved_mc(void)
391 unsigned int sig, pf, rev, total_size, data_size, date;
392 struct ucode_cpu_info uci;
394 if (mc_saved_data.mc_saved_count == 0) {
395 pr_debug("no microcode data saved.\n");
398 pr_debug("Total microcode saved: %d\n", mc_saved_data.mc_saved_count);
400 collect_cpu_info_early(&uci);
402 sig = uci.cpu_sig.sig;
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);
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;
412 struct extended_signature *ext_sig;
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;
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,
427 (date >> 16) & 0xff);
429 /* Look for ext. headers: */
430 if (total_size <= data_size + MC_HEADER_SIZE)
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;
437 for (j = 0; j < ext_sigcount; j++) {
441 pr_debug("\tExtended[%d]: sig=0x%x, pf=0x%x\n",
450 static inline void show_saved_mc(void)
455 #if defined(CONFIG_MICROCODE_INTEL_EARLY) && defined(CONFIG_HOTPLUG_CPU)
456 static DEFINE_MUTEX(x86_cpu_microcode_mutex);
458 * Save this mc into mc_saved_data. So it will be loaded early when a CPU is
459 * hot added or resumes.
461 * Please make sure this mc should be a valid microcode patch before calling
464 int save_mc_for_early(u8 *mc)
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;
474 * Hold hotplug lock so mc_saved_data is not accessed by a CPU in
477 mutex_lock(&x86_cpu_microcode_mutex);
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;
483 if (mc_saved && mc_saved_count)
484 memcpy(mc_saved_tmp, mc_saved,
485 mc_saved_count * sizeof(struct microcode_intel *));
487 * Save the microcode patch mc in mc_save_tmp structure if it's a newer
490 mc_saved_count = _save_mc(mc_saved_tmp, mc, mc_saved_count);
493 * Save the mc_save_tmp in global mc_saved_data.
495 ret = save_microcode(&mc_saved_data, mc_saved_tmp, mc_saved_count);
497 pr_err("Cannot save microcode patch.\n");
504 * Free old saved microcode data.
507 for (i = 0; i < mc_saved_count_init; i++)
513 mutex_unlock(&x86_cpu_microcode_mutex);
517 EXPORT_SYMBOL_GPL(save_mc_for_early);
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)
529 char *p = (char *)__pa_nodebug(ucode_name);
531 char *p = ucode_name;
537 cd = find_cpio_data(p, (void *)start, size, &offset);
541 return get_matching_model_microcode(0, start, cd.data, cd.size,
542 mc_saved_data, mc_saved_in_initrd,
547 * Print ucode update info.
550 print_ucode_info(struct ucode_cpu_info *uci, unsigned int date)
552 int cpu = smp_processor_id();
554 pr_info("CPU%d microcode updated early to revision 0x%x, date = %04x-%02x-%02x\n",
559 (date >> 16) & 0xff);
564 static int delay_ucode_info;
565 static int current_mc_date;
568 * Print early updated ucode info after printk works. This is delayed info dump.
570 void show_ucode_info_early(void)
572 struct ucode_cpu_info uci;
574 if (delay_ucode_info) {
575 collect_cpu_info_early(&uci);
576 print_ucode_info(&uci, current_mc_date);
577 delay_ucode_info = 0;
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.
586 static void print_ucode(struct ucode_cpu_info *uci)
588 struct microcode_intel *mc_intel;
589 int *delay_ucode_info_p;
590 int *current_mc_date_p;
593 if (mc_intel == NULL)
596 delay_ucode_info_p = (int *)__pa_nodebug(&delay_ucode_info);
597 current_mc_date_p = (int *)__pa_nodebug(¤t_mc_date);
599 *delay_ucode_info_p = 1;
600 *current_mc_date_p = mc_intel->hdr.date;
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.
608 static inline void flush_tlb_early(void)
610 __native_flush_tlb_global_irq_disabled();
613 static inline void print_ucode(struct ucode_cpu_info *uci)
615 struct microcode_intel *mc_intel;
618 if (mc_intel == NULL)
621 print_ucode_info(uci, mc_intel->hdr.date);
625 static int apply_microcode_early(struct ucode_cpu_info *uci, bool early)
627 struct microcode_intel *mc_intel;
631 if (mc_intel == NULL)
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);
640 /* As documented in the SDM: Do a CPUID 1 here */
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)
649 /* Flush global tlb. This is precaution. */
652 uci->cpu_sig.rev = val[1];
657 print_ucode_info(uci, mc_intel->hdr.date);
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.
666 int __init save_microcode_in_initrd_intel(void)
668 unsigned int count = mc_saved_data.mc_saved_count;
669 struct microcode_intel *mc_saved[MAX_UCODE_COUNT];
675 microcode_pointer(mc_saved, mc_saved_in_initrd, initrd_start, count);
676 ret = save_microcode(&mc_saved_data, mc_saved, count);
678 pr_err("Cannot save microcode patches from initrd.\n");
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)
690 struct ucode_cpu_info uci;
691 enum ucode_state ret;
693 collect_cpu_info_early(&uci);
694 scan_microcode(start, size, mc_saved_data, mc_saved_in_initrd, &uci);
696 ret = load_microcode(mc_saved_data, mc_saved_in_initrd, start, &uci);
700 apply_microcode_early(&uci, true);
703 void __init load_ucode_intel_bsp(void)
707 struct boot_params *p;
709 p = (struct boot_params *)__pa_nodebug(&boot_params);
710 start = p->hdr.ramdisk_image;
711 size = p->hdr.ramdisk_size;
713 _load_ucode_intel_bsp(
714 (struct mc_saved_data *)__pa_nodebug(&mc_saved_data),
715 (unsigned long *)__pa_nodebug(&mc_saved_in_initrd),
718 start = boot_params.hdr.ramdisk_image + PAGE_OFFSET;
719 size = boot_params.hdr.ramdisk_size;
721 _load_ucode_intel_bsp(&mc_saved_data, mc_saved_in_initrd, start, size);
725 void load_ucode_intel_ap(void)
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;
733 unsigned long *initrd_start_p;
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);
741 mc_saved_data_p = &mc_saved_data;
742 mc_saved_in_initrd_p = mc_saved_in_initrd;
743 initrd_start_addr = initrd_start;
747 * If there is no valid ucode previously saved in memory, no need to
748 * update ucode on this AP.
750 if (mc_saved_data_p->mc_saved_count == 0)
753 collect_cpu_info_early(&uci);
754 ret = load_microcode(mc_saved_data_p, mc_saved_in_initrd_p,
755 initrd_start_addr, &uci);
760 apply_microcode_early(&uci, true);
763 void reload_ucode_intel(void)
765 struct ucode_cpu_info uci;
766 enum ucode_state ret;
768 if (!mc_saved_data.mc_saved_count)
771 collect_cpu_info_early(&uci);
773 ret = load_microcode_early(mc_saved_data.mc_saved,
774 mc_saved_data.mc_saved_count, &uci);
778 apply_microcode_early(&uci, false);