x86/microcode/intel: Rename local variables of type struct mc_saved_data
[deliverable/linux.git] / arch / x86 / kernel / cpu / microcode / intel.c
1 /*
2 * Intel CPU Microcode Update Driver for Linux
3 *
4 * Copyright (C) 2000-2006 Tigran Aivazian <tigran@aivazian.fsnet.co.uk>
5 * 2006 Shaohua Li <shaohua.li@intel.com>
6 *
7 * Intel CPU microcode early update for Linux
8 *
9 * Copyright (C) 2012 Fenghua Yu <fenghua.yu@intel.com>
10 * H Peter Anvin" <hpa@zytor.com>
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
16 */
17
18 /*
19 * This needs to be before all headers so that pr_debug in printk.h doesn't turn
20 * printk calls into no_printk().
21 *
22 *#define DEBUG
23 */
24 #define pr_fmt(fmt) "microcode: " fmt
25
26 #include <linux/earlycpio.h>
27 #include <linux/firmware.h>
28 #include <linux/uaccess.h>
29 #include <linux/vmalloc.h>
30 #include <linux/initrd.h>
31 #include <linux/kernel.h>
32 #include <linux/slab.h>
33 #include <linux/cpu.h>
34 #include <linux/mm.h>
35
36 #include <asm/microcode_intel.h>
37 #include <asm/processor.h>
38 #include <asm/tlbflush.h>
39 #include <asm/setup.h>
40 #include <asm/msr.h>
41
42 static unsigned long mc_saved_in_initrd[MAX_UCODE_COUNT];
43 static struct mc_saved_data {
44 unsigned int mc_saved_count;
45 struct microcode_intel **mc_saved;
46 } mc_saved_data;
47
48 static enum ucode_state
49 load_microcode_early(struct microcode_intel **saved,
50 unsigned int num_saved, struct ucode_cpu_info *uci)
51 {
52 struct microcode_intel *ucode_ptr, *new_mc = NULL;
53 struct microcode_header_intel *mc_hdr;
54 int new_rev, ret, i;
55
56 new_rev = uci->cpu_sig.rev;
57
58 for (i = 0; i < num_saved; i++) {
59 ucode_ptr = saved[i];
60 mc_hdr = (struct microcode_header_intel *)ucode_ptr;
61
62 ret = has_newer_microcode(ucode_ptr,
63 uci->cpu_sig.sig,
64 uci->cpu_sig.pf,
65 new_rev);
66 if (!ret)
67 continue;
68
69 new_rev = mc_hdr->rev;
70 new_mc = ucode_ptr;
71 }
72
73 if (!new_mc)
74 return UCODE_NFOUND;
75
76 uci->mc = (struct microcode_intel *)new_mc;
77 return UCODE_OK;
78 }
79
80 static inline void
81 copy_initrd_ptrs(struct microcode_intel **mc_saved, unsigned long *initrd,
82 unsigned long off, int num_saved)
83 {
84 int i;
85
86 for (i = 0; i < num_saved; i++)
87 mc_saved[i] = (struct microcode_intel *)(initrd[i] + off);
88 }
89
90 #ifdef CONFIG_X86_32
91 static void
92 microcode_phys(struct microcode_intel **mc_saved_tmp, struct mc_saved_data *mcs)
93 {
94 int i;
95 struct microcode_intel ***mc_saved;
96
97 mc_saved = (struct microcode_intel ***)__pa_nodebug(&mcs->mc_saved);
98
99 for (i = 0; i < mcs->mc_saved_count; i++) {
100 struct microcode_intel *p;
101
102 p = *(struct microcode_intel **)__pa_nodebug(mcs->mc_saved + i);
103 mc_saved_tmp[i] = (struct microcode_intel *)__pa_nodebug(p);
104 }
105 }
106 #endif
107
108 static enum ucode_state
109 load_microcode(struct mc_saved_data *mcs, unsigned long *initrd,
110 unsigned long initrd_start, struct ucode_cpu_info *uci)
111 {
112 struct microcode_intel *mc_saved_tmp[MAX_UCODE_COUNT];
113 unsigned int count = mcs->mc_saved_count;
114
115 if (!mcs->mc_saved) {
116 copy_initrd_ptrs(mc_saved_tmp, initrd, initrd_start, count);
117
118 return load_microcode_early(mc_saved_tmp, count, uci);
119 } else {
120 #ifdef CONFIG_X86_32
121 microcode_phys(mc_saved_tmp, mcs);
122 return load_microcode_early(mc_saved_tmp, count, uci);
123 #else
124 return load_microcode_early(mcs->mc_saved, count, uci);
125 #endif
126 }
127 }
128
129 /*
130 * Given CPU signature and a microcode patch, this function finds if the
131 * microcode patch has matching family and model with the CPU.
132 */
133 static enum ucode_state
134 matching_model_microcode(struct microcode_header_intel *mc_header,
135 unsigned long sig)
136 {
137 unsigned int fam, model;
138 unsigned int fam_ucode, model_ucode;
139 struct extended_sigtable *ext_header;
140 unsigned long total_size = get_totalsize(mc_header);
141 unsigned long data_size = get_datasize(mc_header);
142 int ext_sigcount, i;
143 struct extended_signature *ext_sig;
144
145 fam = x86_family(sig);
146 model = x86_model(sig);
147
148 fam_ucode = x86_family(mc_header->sig);
149 model_ucode = x86_model(mc_header->sig);
150
151 if (fam == fam_ucode && model == model_ucode)
152 return UCODE_OK;
153
154 /* Look for ext. headers: */
155 if (total_size <= data_size + MC_HEADER_SIZE)
156 return UCODE_NFOUND;
157
158 ext_header = (void *) mc_header + data_size + MC_HEADER_SIZE;
159 ext_sig = (void *)ext_header + EXT_HEADER_SIZE;
160 ext_sigcount = ext_header->count;
161
162 for (i = 0; i < ext_sigcount; i++) {
163 fam_ucode = x86_family(ext_sig->sig);
164 model_ucode = x86_model(ext_sig->sig);
165
166 if (fam == fam_ucode && model == model_ucode)
167 return UCODE_OK;
168
169 ext_sig++;
170 }
171 return UCODE_NFOUND;
172 }
173
174 static int
175 save_microcode(struct mc_saved_data *mcs,
176 struct microcode_intel **mc_saved_src,
177 unsigned int mc_saved_count)
178 {
179 int i, j;
180 struct microcode_intel **saved_ptr;
181 int ret;
182
183 if (!mc_saved_count)
184 return -EINVAL;
185
186 /*
187 * Copy new microcode data.
188 */
189 saved_ptr = kcalloc(mc_saved_count, sizeof(struct microcode_intel *), GFP_KERNEL);
190 if (!saved_ptr)
191 return -ENOMEM;
192
193 for (i = 0; i < mc_saved_count; i++) {
194 struct microcode_header_intel *mc_hdr;
195 struct microcode_intel *mc;
196 unsigned long size;
197
198 if (!mc_saved_src[i]) {
199 ret = -EINVAL;
200 goto err;
201 }
202
203 mc = mc_saved_src[i];
204 mc_hdr = &mc->hdr;
205 size = get_totalsize(mc_hdr);
206
207 saved_ptr[i] = kmalloc(size, GFP_KERNEL);
208 if (!saved_ptr[i]) {
209 ret = -ENOMEM;
210 goto err;
211 }
212
213 memcpy(saved_ptr[i], mc, size);
214 }
215
216 /*
217 * Point to newly saved microcode.
218 */
219 mcs->mc_saved = saved_ptr;
220 mcs->mc_saved_count = mc_saved_count;
221
222 return 0;
223
224 err:
225 for (j = 0; j <= i; j++)
226 kfree(saved_ptr[j]);
227 kfree(saved_ptr);
228
229 return ret;
230 }
231
232 /*
233 * A microcode patch in ucode_ptr is saved into mc_saved
234 * - if it has matching signature and newer revision compared to an existing
235 * patch mc_saved.
236 * - or if it is a newly discovered microcode patch.
237 *
238 * The microcode patch should have matching model with CPU.
239 *
240 * Returns: The updated number @num_saved of saved microcode patches.
241 */
242 static unsigned int _save_mc(struct microcode_intel **mc_saved,
243 u8 *ucode_ptr, unsigned int num_saved)
244 {
245 struct microcode_header_intel *mc_hdr, *mc_saved_hdr;
246 unsigned int sig, pf;
247 int found = 0, i;
248
249 mc_hdr = (struct microcode_header_intel *)ucode_ptr;
250
251 for (i = 0; i < num_saved; i++) {
252 mc_saved_hdr = (struct microcode_header_intel *)mc_saved[i];
253 sig = mc_saved_hdr->sig;
254 pf = mc_saved_hdr->pf;
255
256 if (!find_matching_signature(ucode_ptr, sig, pf))
257 continue;
258
259 found = 1;
260
261 if (mc_hdr->rev <= mc_saved_hdr->rev)
262 continue;
263
264 /*
265 * Found an older ucode saved earlier. Replace it with
266 * this newer one.
267 */
268 mc_saved[i] = (struct microcode_intel *)ucode_ptr;
269 break;
270 }
271
272 /* Newly detected microcode, save it to memory. */
273 if (i >= num_saved && !found)
274 mc_saved[num_saved++] = (struct microcode_intel *)ucode_ptr;
275
276 return num_saved;
277 }
278
279 /*
280 * Get microcode matching with BSP's model. Only CPUs with the same model as
281 * BSP can stay in the platform.
282 */
283 static enum ucode_state __init
284 get_matching_model_microcode(int cpu, unsigned long start,
285 void *data, size_t size,
286 struct mc_saved_data *mcs,
287 unsigned long *mc_saved_in_initrd,
288 struct ucode_cpu_info *uci)
289 {
290 u8 *ucode_ptr = data;
291 unsigned int leftover = size;
292 enum ucode_state state = UCODE_OK;
293 unsigned int mc_size;
294 struct microcode_header_intel *mc_header;
295 struct microcode_intel *mc_saved_tmp[MAX_UCODE_COUNT];
296 unsigned int mc_saved_count = mcs->mc_saved_count;
297 int i;
298
299 while (leftover && mc_saved_count < ARRAY_SIZE(mc_saved_tmp)) {
300
301 if (leftover < sizeof(mc_header))
302 break;
303
304 mc_header = (struct microcode_header_intel *)ucode_ptr;
305
306 mc_size = get_totalsize(mc_header);
307 if (!mc_size || mc_size > leftover ||
308 microcode_sanity_check(ucode_ptr, 0) < 0)
309 break;
310
311 leftover -= mc_size;
312
313 /*
314 * Since APs with same family and model as the BSP may boot in
315 * the platform, we need to find and save microcode patches
316 * with the same family and model as the BSP.
317 */
318 if (matching_model_microcode(mc_header, uci->cpu_sig.sig) !=
319 UCODE_OK) {
320 ucode_ptr += mc_size;
321 continue;
322 }
323
324 mc_saved_count = _save_mc(mc_saved_tmp, ucode_ptr, mc_saved_count);
325
326 ucode_ptr += mc_size;
327 }
328
329 if (leftover) {
330 state = UCODE_ERROR;
331 goto out;
332 }
333
334 if (mc_saved_count == 0) {
335 state = UCODE_NFOUND;
336 goto out;
337 }
338
339 for (i = 0; i < mc_saved_count; i++)
340 mc_saved_in_initrd[i] = (unsigned long)mc_saved_tmp[i] - start;
341
342 mcs->mc_saved_count = mc_saved_count;
343 out:
344 return state;
345 }
346
347 static int collect_cpu_info_early(struct ucode_cpu_info *uci)
348 {
349 unsigned int val[2];
350 unsigned int family, model;
351 struct cpu_signature csig;
352 unsigned int eax, ebx, ecx, edx;
353
354 csig.sig = 0;
355 csig.pf = 0;
356 csig.rev = 0;
357
358 memset(uci, 0, sizeof(*uci));
359
360 eax = 0x00000001;
361 ecx = 0;
362 native_cpuid(&eax, &ebx, &ecx, &edx);
363 csig.sig = eax;
364
365 family = x86_family(csig.sig);
366 model = x86_model(csig.sig);
367
368 if ((model >= 5) || (family > 6)) {
369 /* get processor flags from MSR 0x17 */
370 native_rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]);
371 csig.pf = 1 << ((val[1] >> 18) & 7);
372 }
373 native_wrmsr(MSR_IA32_UCODE_REV, 0, 0);
374
375 /* As documented in the SDM: Do a CPUID 1 here */
376 sync_core();
377
378 /* get the current revision from MSR 0x8B */
379 native_rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
380
381 csig.rev = val[1];
382
383 uci->cpu_sig = csig;
384 uci->valid = 1;
385
386 return 0;
387 }
388
389 static void show_saved_mc(void)
390 {
391 #ifdef DEBUG
392 int i, j;
393 unsigned int sig, pf, rev, total_size, data_size, date;
394 struct ucode_cpu_info uci;
395
396 if (mc_saved_data.mc_saved_count == 0) {
397 pr_debug("no microcode data saved.\n");
398 return;
399 }
400 pr_debug("Total microcode saved: %d\n", mc_saved_data.mc_saved_count);
401
402 collect_cpu_info_early(&uci);
403
404 sig = uci.cpu_sig.sig;
405 pf = uci.cpu_sig.pf;
406 rev = uci.cpu_sig.rev;
407 pr_debug("CPU: sig=0x%x, pf=0x%x, rev=0x%x\n", sig, pf, rev);
408
409 for (i = 0; i < mc_saved_data.mc_saved_count; i++) {
410 struct microcode_header_intel *mc_saved_header;
411 struct extended_sigtable *ext_header;
412 int ext_sigcount;
413 struct extended_signature *ext_sig;
414
415 mc_saved_header = (struct microcode_header_intel *)
416 mc_saved_data.mc_saved[i];
417 sig = mc_saved_header->sig;
418 pf = mc_saved_header->pf;
419 rev = mc_saved_header->rev;
420 total_size = get_totalsize(mc_saved_header);
421 data_size = get_datasize(mc_saved_header);
422 date = mc_saved_header->date;
423
424 pr_debug("mc_saved[%d]: sig=0x%x, pf=0x%x, rev=0x%x, toal size=0x%x, date = %04x-%02x-%02x\n",
425 i, sig, pf, rev, total_size,
426 date & 0xffff,
427 date >> 24,
428 (date >> 16) & 0xff);
429
430 /* Look for ext. headers: */
431 if (total_size <= data_size + MC_HEADER_SIZE)
432 continue;
433
434 ext_header = (void *) mc_saved_header + data_size + MC_HEADER_SIZE;
435 ext_sigcount = ext_header->count;
436 ext_sig = (void *)ext_header + EXT_HEADER_SIZE;
437
438 for (j = 0; j < ext_sigcount; j++) {
439 sig = ext_sig->sig;
440 pf = ext_sig->pf;
441
442 pr_debug("\tExtended[%d]: sig=0x%x, pf=0x%x\n",
443 j, sig, pf);
444
445 ext_sig++;
446 }
447
448 }
449 #endif
450 }
451
452 #ifdef CONFIG_HOTPLUG_CPU
453 static DEFINE_MUTEX(x86_cpu_microcode_mutex);
454 /*
455 * Save this mc into mc_saved_data. So it will be loaded early when a CPU is
456 * hot added or resumes.
457 *
458 * Please make sure this mc should be a valid microcode patch before calling
459 * this function.
460 */
461 int save_mc_for_early(u8 *mc)
462 {
463 struct microcode_intel *mc_saved_tmp[MAX_UCODE_COUNT];
464 unsigned int mc_saved_count_init;
465 unsigned int mc_saved_count;
466 struct microcode_intel **mc_saved;
467 int ret = 0;
468 int i;
469
470 /*
471 * Hold hotplug lock so mc_saved_data is not accessed by a CPU in
472 * hotplug.
473 */
474 mutex_lock(&x86_cpu_microcode_mutex);
475
476 mc_saved_count_init = mc_saved_data.mc_saved_count;
477 mc_saved_count = mc_saved_data.mc_saved_count;
478 mc_saved = mc_saved_data.mc_saved;
479
480 if (mc_saved && mc_saved_count)
481 memcpy(mc_saved_tmp, mc_saved,
482 mc_saved_count * sizeof(struct microcode_intel *));
483 /*
484 * Save the microcode patch mc in mc_save_tmp structure if it's a newer
485 * version.
486 */
487 mc_saved_count = _save_mc(mc_saved_tmp, mc, mc_saved_count);
488
489 /*
490 * Save the mc_save_tmp in global mc_saved_data.
491 */
492 ret = save_microcode(&mc_saved_data, mc_saved_tmp, mc_saved_count);
493 if (ret) {
494 pr_err("Cannot save microcode patch.\n");
495 goto out;
496 }
497
498 show_saved_mc();
499
500 /*
501 * Free old saved microcode data.
502 */
503 if (mc_saved) {
504 for (i = 0; i < mc_saved_count_init; i++)
505 kfree(mc_saved[i]);
506 kfree(mc_saved);
507 }
508
509 out:
510 mutex_unlock(&x86_cpu_microcode_mutex);
511
512 return ret;
513 }
514 EXPORT_SYMBOL_GPL(save_mc_for_early);
515 #endif
516
517 static bool __init load_builtin_intel_microcode(struct cpio_data *cp)
518 {
519 #ifdef CONFIG_X86_64
520 unsigned int eax = 0x00000001, ebx, ecx = 0, edx;
521 char name[30];
522
523 native_cpuid(&eax, &ebx, &ecx, &edx);
524
525 sprintf(name, "intel-ucode/%02x-%02x-%02x",
526 x86_family(eax), x86_model(eax), x86_stepping(eax));
527
528 return get_builtin_firmware(cp, name);
529 #else
530 return false;
531 #endif
532 }
533
534 static __initdata char ucode_name[] = "kernel/x86/microcode/GenuineIntel.bin";
535 static __init enum ucode_state
536 scan_microcode(struct mc_saved_data *mcs, unsigned long *initrd,
537 unsigned long start, unsigned long size,
538 struct ucode_cpu_info *uci)
539 {
540 struct cpio_data cd;
541 long offset = 0;
542 #ifdef CONFIG_X86_32
543 char *p = (char *)__pa_nodebug(ucode_name);
544 #else
545 char *p = ucode_name;
546 #endif
547
548 cd.data = NULL;
549 cd.size = 0;
550
551 /* try built-in microcode if no initrd */
552 if (!size) {
553 if (!load_builtin_intel_microcode(&cd))
554 return UCODE_ERROR;
555 } else {
556 cd = find_cpio_data(p, (void *)start, size, &offset);
557 if (!cd.data)
558 return UCODE_ERROR;
559 }
560
561 return get_matching_model_microcode(0, start, cd.data, cd.size,
562 mcs, initrd, uci);
563 }
564
565 /*
566 * Print ucode update info.
567 */
568 static void
569 print_ucode_info(struct ucode_cpu_info *uci, unsigned int date)
570 {
571 pr_info_once("microcode updated early to revision 0x%x, date = %04x-%02x-%02x\n",
572 uci->cpu_sig.rev,
573 date & 0xffff,
574 date >> 24,
575 (date >> 16) & 0xff);
576 }
577
578 #ifdef CONFIG_X86_32
579
580 static int delay_ucode_info;
581 static int current_mc_date;
582
583 /*
584 * Print early updated ucode info after printk works. This is delayed info dump.
585 */
586 void show_ucode_info_early(void)
587 {
588 struct ucode_cpu_info uci;
589
590 if (delay_ucode_info) {
591 collect_cpu_info_early(&uci);
592 print_ucode_info(&uci, current_mc_date);
593 delay_ucode_info = 0;
594 }
595 }
596
597 /*
598 * At this point, we can not call printk() yet. Keep microcode patch number in
599 * mc_saved_data.mc_saved and delay printing microcode info in
600 * show_ucode_info_early() until printk() works.
601 */
602 static void print_ucode(struct ucode_cpu_info *uci)
603 {
604 struct microcode_intel *mc_intel;
605 int *delay_ucode_info_p;
606 int *current_mc_date_p;
607
608 mc_intel = uci->mc;
609 if (mc_intel == NULL)
610 return;
611
612 delay_ucode_info_p = (int *)__pa_nodebug(&delay_ucode_info);
613 current_mc_date_p = (int *)__pa_nodebug(&current_mc_date);
614
615 *delay_ucode_info_p = 1;
616 *current_mc_date_p = mc_intel->hdr.date;
617 }
618 #else
619
620 /*
621 * Flush global tlb. We only do this in x86_64 where paging has been enabled
622 * already and PGE should be enabled as well.
623 */
624 static inline void flush_tlb_early(void)
625 {
626 __native_flush_tlb_global_irq_disabled();
627 }
628
629 static inline void print_ucode(struct ucode_cpu_info *uci)
630 {
631 struct microcode_intel *mc_intel;
632
633 mc_intel = uci->mc;
634 if (mc_intel == NULL)
635 return;
636
637 print_ucode_info(uci, mc_intel->hdr.date);
638 }
639 #endif
640
641 static int apply_microcode_early(struct ucode_cpu_info *uci, bool early)
642 {
643 struct microcode_intel *mc_intel;
644 unsigned int val[2];
645
646 mc_intel = uci->mc;
647 if (mc_intel == NULL)
648 return 0;
649
650 /* write microcode via MSR 0x79 */
651 native_wrmsr(MSR_IA32_UCODE_WRITE,
652 (unsigned long) mc_intel->bits,
653 (unsigned long) mc_intel->bits >> 16 >> 16);
654 native_wrmsr(MSR_IA32_UCODE_REV, 0, 0);
655
656 /* As documented in the SDM: Do a CPUID 1 here */
657 sync_core();
658
659 /* get the current revision from MSR 0x8B */
660 native_rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
661 if (val[1] != mc_intel->hdr.rev)
662 return -1;
663
664 #ifdef CONFIG_X86_64
665 /* Flush global tlb. This is precaution. */
666 flush_tlb_early();
667 #endif
668 uci->cpu_sig.rev = val[1];
669
670 if (early)
671 print_ucode(uci);
672 else
673 print_ucode_info(uci, mc_intel->hdr.date);
674
675 return 0;
676 }
677
678 /*
679 * This function converts microcode patch offsets previously stored in
680 * mc_saved_in_initrd to pointers and stores the pointers in mc_saved_data.
681 */
682 int __init save_microcode_in_initrd_intel(void)
683 {
684 unsigned int count = mc_saved_data.mc_saved_count;
685 struct microcode_intel *mc_saved[MAX_UCODE_COUNT];
686 int ret = 0;
687
688 if (count == 0)
689 return ret;
690
691 copy_initrd_ptrs(mc_saved, mc_saved_in_initrd, get_initrd_start(), count);
692 ret = save_microcode(&mc_saved_data, mc_saved, count);
693 if (ret)
694 pr_err("Cannot save microcode patches from initrd.\n");
695
696 show_saved_mc();
697
698 return ret;
699 }
700
701 static void __init
702 _load_ucode_intel_bsp(struct mc_saved_data *mcs, unsigned long *initrd,
703 unsigned long start, unsigned long size)
704 {
705 struct ucode_cpu_info uci;
706 enum ucode_state ret;
707
708 collect_cpu_info_early(&uci);
709
710 ret = scan_microcode(mcs, initrd, start, size, &uci);
711 if (ret != UCODE_OK)
712 return;
713
714 ret = load_microcode(mcs, initrd, start, &uci);
715 if (ret != UCODE_OK)
716 return;
717
718 apply_microcode_early(&uci, true);
719 }
720
721 void __init load_ucode_intel_bsp(void)
722 {
723 u64 start, size;
724 #ifdef CONFIG_X86_32
725 struct boot_params *p;
726
727 p = (struct boot_params *)__pa_nodebug(&boot_params);
728 size = p->hdr.ramdisk_size;
729
730 /*
731 * Set start only if we have an initrd image. We cannot use initrd_start
732 * because it is not set that early yet.
733 */
734 start = (size ? p->hdr.ramdisk_image : 0);
735
736 _load_ucode_intel_bsp((struct mc_saved_data *)__pa_nodebug(&mc_saved_data),
737 (unsigned long *)__pa_nodebug(&mc_saved_in_initrd),
738 start, size);
739 #else
740 size = boot_params.hdr.ramdisk_size;
741 start = (size ? boot_params.hdr.ramdisk_image + PAGE_OFFSET : 0);
742
743 _load_ucode_intel_bsp(&mc_saved_data, mc_saved_in_initrd, start, size);
744 #endif
745 }
746
747 void load_ucode_intel_ap(void)
748 {
749 unsigned long *mc_saved_in_initrd_p;
750 struct mc_saved_data *mcs_p;
751 struct ucode_cpu_info uci;
752 enum ucode_state ret;
753 #ifdef CONFIG_X86_32
754
755 mc_saved_in_initrd_p = (unsigned long *)__pa_nodebug(mc_saved_in_initrd);
756 mcs_p = (struct mc_saved_data *)__pa_nodebug(&mc_saved_data);
757 #else
758 mc_saved_in_initrd_p = mc_saved_in_initrd;
759 mcs_p = &mc_saved_data;
760 #endif
761
762 /*
763 * If there is no valid ucode previously saved in memory, no need to
764 * update ucode on this AP.
765 */
766 if (mcs_p->mc_saved_count == 0)
767 return;
768
769 collect_cpu_info_early(&uci);
770 ret = load_microcode(mcs_p, mc_saved_in_initrd_p,
771 get_initrd_start_addr(), &uci);
772
773 if (ret != UCODE_OK)
774 return;
775
776 apply_microcode_early(&uci, true);
777 }
778
779 void reload_ucode_intel(void)
780 {
781 struct ucode_cpu_info uci;
782 enum ucode_state ret;
783
784 if (!mc_saved_data.mc_saved_count)
785 return;
786
787 collect_cpu_info_early(&uci);
788
789 ret = load_microcode_early(mc_saved_data.mc_saved,
790 mc_saved_data.mc_saved_count, &uci);
791 if (ret != UCODE_OK)
792 return;
793
794 apply_microcode_early(&uci, false);
795 }
796
797 static int collect_cpu_info(int cpu_num, struct cpu_signature *csig)
798 {
799 struct cpuinfo_x86 *c = &cpu_data(cpu_num);
800 unsigned int val[2];
801
802 memset(csig, 0, sizeof(*csig));
803
804 csig->sig = cpuid_eax(0x00000001);
805
806 if ((c->x86_model >= 5) || (c->x86 > 6)) {
807 /* get processor flags from MSR 0x17 */
808 rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]);
809 csig->pf = 1 << ((val[1] >> 18) & 7);
810 }
811
812 csig->rev = c->microcode;
813 pr_info("CPU%d sig=0x%x, pf=0x%x, revision=0x%x\n",
814 cpu_num, csig->sig, csig->pf, csig->rev);
815
816 return 0;
817 }
818
819 /*
820 * return 0 - no update found
821 * return 1 - found update
822 */
823 static int get_matching_mc(struct microcode_intel *mc_intel, int cpu)
824 {
825 struct cpu_signature cpu_sig;
826 unsigned int csig, cpf, crev;
827
828 collect_cpu_info(cpu, &cpu_sig);
829
830 csig = cpu_sig.sig;
831 cpf = cpu_sig.pf;
832 crev = cpu_sig.rev;
833
834 return has_newer_microcode(mc_intel, csig, cpf, crev);
835 }
836
837 static int apply_microcode_intel(int cpu)
838 {
839 struct microcode_intel *mc_intel;
840 struct ucode_cpu_info *uci;
841 unsigned int val[2];
842 int cpu_num = raw_smp_processor_id();
843 struct cpuinfo_x86 *c = &cpu_data(cpu_num);
844
845 uci = ucode_cpu_info + cpu;
846 mc_intel = uci->mc;
847
848 /* We should bind the task to the CPU */
849 BUG_ON(cpu_num != cpu);
850
851 if (mc_intel == NULL)
852 return 0;
853
854 /*
855 * Microcode on this CPU could be updated earlier. Only apply the
856 * microcode patch in mc_intel when it is newer than the one on this
857 * CPU.
858 */
859 if (get_matching_mc(mc_intel, cpu) == 0)
860 return 0;
861
862 /* write microcode via MSR 0x79 */
863 wrmsr(MSR_IA32_UCODE_WRITE,
864 (unsigned long) mc_intel->bits,
865 (unsigned long) mc_intel->bits >> 16 >> 16);
866 wrmsr(MSR_IA32_UCODE_REV, 0, 0);
867
868 /* As documented in the SDM: Do a CPUID 1 here */
869 sync_core();
870
871 /* get the current revision from MSR 0x8B */
872 rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
873
874 if (val[1] != mc_intel->hdr.rev) {
875 pr_err("CPU%d update to revision 0x%x failed\n",
876 cpu_num, mc_intel->hdr.rev);
877 return -1;
878 }
879 pr_info("CPU%d updated to revision 0x%x, date = %04x-%02x-%02x\n",
880 cpu_num, val[1],
881 mc_intel->hdr.date & 0xffff,
882 mc_intel->hdr.date >> 24,
883 (mc_intel->hdr.date >> 16) & 0xff);
884
885 uci->cpu_sig.rev = val[1];
886 c->microcode = val[1];
887
888 return 0;
889 }
890
891 static enum ucode_state generic_load_microcode(int cpu, void *data, size_t size,
892 int (*get_ucode_data)(void *, const void *, size_t))
893 {
894 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
895 u8 *ucode_ptr = data, *new_mc = NULL, *mc = NULL;
896 int new_rev = uci->cpu_sig.rev;
897 unsigned int leftover = size;
898 enum ucode_state state = UCODE_OK;
899 unsigned int curr_mc_size = 0;
900 unsigned int csig, cpf;
901
902 while (leftover) {
903 struct microcode_header_intel mc_header;
904 unsigned int mc_size;
905
906 if (leftover < sizeof(mc_header)) {
907 pr_err("error! Truncated header in microcode data file\n");
908 break;
909 }
910
911 if (get_ucode_data(&mc_header, ucode_ptr, sizeof(mc_header)))
912 break;
913
914 mc_size = get_totalsize(&mc_header);
915 if (!mc_size || mc_size > leftover) {
916 pr_err("error! Bad data in microcode data file\n");
917 break;
918 }
919
920 /* For performance reasons, reuse mc area when possible */
921 if (!mc || mc_size > curr_mc_size) {
922 vfree(mc);
923 mc = vmalloc(mc_size);
924 if (!mc)
925 break;
926 curr_mc_size = mc_size;
927 }
928
929 if (get_ucode_data(mc, ucode_ptr, mc_size) ||
930 microcode_sanity_check(mc, 1) < 0) {
931 break;
932 }
933
934 csig = uci->cpu_sig.sig;
935 cpf = uci->cpu_sig.pf;
936 if (has_newer_microcode(mc, csig, cpf, new_rev)) {
937 vfree(new_mc);
938 new_rev = mc_header.rev;
939 new_mc = mc;
940 mc = NULL; /* trigger new vmalloc */
941 }
942
943 ucode_ptr += mc_size;
944 leftover -= mc_size;
945 }
946
947 vfree(mc);
948
949 if (leftover) {
950 vfree(new_mc);
951 state = UCODE_ERROR;
952 goto out;
953 }
954
955 if (!new_mc) {
956 state = UCODE_NFOUND;
957 goto out;
958 }
959
960 vfree(uci->mc);
961 uci->mc = (struct microcode_intel *)new_mc;
962
963 /*
964 * If early loading microcode is supported, save this mc into
965 * permanent memory. So it will be loaded early when a CPU is hot added
966 * or resumes.
967 */
968 save_mc_for_early(new_mc);
969
970 pr_debug("CPU%d found a matching microcode update with version 0x%x (current=0x%x)\n",
971 cpu, new_rev, uci->cpu_sig.rev);
972 out:
973 return state;
974 }
975
976 static int get_ucode_fw(void *to, const void *from, size_t n)
977 {
978 memcpy(to, from, n);
979 return 0;
980 }
981
982 static enum ucode_state request_microcode_fw(int cpu, struct device *device,
983 bool refresh_fw)
984 {
985 char name[30];
986 struct cpuinfo_x86 *c = &cpu_data(cpu);
987 const struct firmware *firmware;
988 enum ucode_state ret;
989
990 sprintf(name, "intel-ucode/%02x-%02x-%02x",
991 c->x86, c->x86_model, c->x86_mask);
992
993 if (request_firmware_direct(&firmware, name, device)) {
994 pr_debug("data file %s load failed\n", name);
995 return UCODE_NFOUND;
996 }
997
998 ret = generic_load_microcode(cpu, (void *)firmware->data,
999 firmware->size, &get_ucode_fw);
1000
1001 release_firmware(firmware);
1002
1003 return ret;
1004 }
1005
1006 static int get_ucode_user(void *to, const void *from, size_t n)
1007 {
1008 return copy_from_user(to, from, n);
1009 }
1010
1011 static enum ucode_state
1012 request_microcode_user(int cpu, const void __user *buf, size_t size)
1013 {
1014 return generic_load_microcode(cpu, (void *)buf, size, &get_ucode_user);
1015 }
1016
1017 static void microcode_fini_cpu(int cpu)
1018 {
1019 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
1020
1021 vfree(uci->mc);
1022 uci->mc = NULL;
1023 }
1024
1025 static struct microcode_ops microcode_intel_ops = {
1026 .request_microcode_user = request_microcode_user,
1027 .request_microcode_fw = request_microcode_fw,
1028 .collect_cpu_info = collect_cpu_info,
1029 .apply_microcode = apply_microcode_intel,
1030 .microcode_fini_cpu = microcode_fini_cpu,
1031 };
1032
1033 struct microcode_ops * __init init_intel_microcode(void)
1034 {
1035 struct cpuinfo_x86 *c = &boot_cpu_data;
1036
1037 if (c->x86_vendor != X86_VENDOR_INTEL || c->x86 < 6 ||
1038 cpu_has(c, X86_FEATURE_IA64)) {
1039 pr_err("Intel CPU family 0x%x not supported\n", c->x86);
1040 return NULL;
1041 }
1042
1043 return &microcode_intel_ops;
1044 }
1045
This page took 0.052101 seconds and 6 git commands to generate.