1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
43
44
45
46
47static unsigned long mc_tmp_ptrs[MAX_UCODE_COUNT];
48
49static struct mc_saved_data {
50 unsigned int num_saved;
51 struct microcode_intel **mc_saved;
52} mc_saved_data;
53
54
55static struct ucode_blobs {
56 unsigned long start;
57 bool valid;
58} blobs;
59
60static enum ucode_state
61load_microcode_early(struct microcode_intel **saved,
62 unsigned int num_saved, struct ucode_cpu_info *uci)
63{
64 struct microcode_intel *ucode_ptr, *new_mc = NULL;
65 struct microcode_header_intel *mc_hdr;
66 int new_rev, ret, i;
67
68 new_rev = uci->cpu_sig.rev;
69
70 for (i = 0; i < num_saved; i++) {
71 ucode_ptr = saved[i];
72 mc_hdr = (struct microcode_header_intel *)ucode_ptr;
73
74 ret = has_newer_microcode(ucode_ptr,
75 uci->cpu_sig.sig,
76 uci->cpu_sig.pf,
77 new_rev);
78 if (!ret)
79 continue;
80
81 new_rev = mc_hdr->rev;
82 new_mc = ucode_ptr;
83 }
84
85 if (!new_mc)
86 return UCODE_NFOUND;
87
88 uci->mc = (struct microcode_intel *)new_mc;
89 return UCODE_OK;
90}
91
92static inline void
93copy_ptrs(struct microcode_intel **mc_saved, unsigned long *mc_ptrs,
94 unsigned long off, int num_saved)
95{
96 int i;
97
98 for (i = 0; i < num_saved; i++)
99 mc_saved[i] = (struct microcode_intel *)(mc_ptrs[i] + off);
100}
101
102#ifdef CONFIG_X86_32
103static void
104microcode_phys(struct microcode_intel **mc_saved_tmp, struct mc_saved_data *mcs)
105{
106 int i;
107 struct microcode_intel ***mc_saved;
108
109 mc_saved = (struct microcode_intel ***)__pa_nodebug(&mcs->mc_saved);
110
111 for (i = 0; i < mcs->num_saved; i++) {
112 struct microcode_intel *p;
113
114 p = *(struct microcode_intel **)__pa_nodebug(mcs->mc_saved + i);
115 mc_saved_tmp[i] = (struct microcode_intel *)__pa_nodebug(p);
116 }
117}
118#endif
119
120static enum ucode_state
121load_microcode(struct mc_saved_data *mcs, unsigned long *mc_ptrs,
122 unsigned long offset, struct ucode_cpu_info *uci)
123{
124 struct microcode_intel *mc_saved_tmp[MAX_UCODE_COUNT];
125 unsigned int count = mcs->num_saved;
126
127 if (!mcs->mc_saved) {
128 copy_ptrs(mc_saved_tmp, mc_ptrs, offset, count);
129
130 return load_microcode_early(mc_saved_tmp, count, uci);
131 } else {
132#ifdef CONFIG_X86_32
133 microcode_phys(mc_saved_tmp, mcs);
134 return load_microcode_early(mc_saved_tmp, count, uci);
135#else
136 return load_microcode_early(mcs->mc_saved, count, uci);
137#endif
138 }
139}
140
141
142
143
144
145static enum ucode_state
146matching_model_microcode(struct microcode_header_intel *mc_header,
147 unsigned long sig)
148{
149 unsigned int fam, model;
150 unsigned int fam_ucode, model_ucode;
151 struct extended_sigtable *ext_header;
152 unsigned long total_size = get_totalsize(mc_header);
153 unsigned long data_size = get_datasize(mc_header);
154 int ext_sigcount, i;
155 struct extended_signature *ext_sig;
156
157 fam = x86_family(sig);
158 model = x86_model(sig);
159
160 fam_ucode = x86_family(mc_header->sig);
161 model_ucode = x86_model(mc_header->sig);
162
163 if (fam == fam_ucode && model == model_ucode)
164 return UCODE_OK;
165
166
167 if (total_size <= data_size + MC_HEADER_SIZE)
168 return UCODE_NFOUND;
169
170 ext_header = (void *) mc_header + data_size + MC_HEADER_SIZE;
171 ext_sig = (void *)ext_header + EXT_HEADER_SIZE;
172 ext_sigcount = ext_header->count;
173
174 for (i = 0; i < ext_sigcount; i++) {
175 fam_ucode = x86_family(ext_sig->sig);
176 model_ucode = x86_model(ext_sig->sig);
177
178 if (fam == fam_ucode && model == model_ucode)
179 return UCODE_OK;
180
181 ext_sig++;
182 }
183 return UCODE_NFOUND;
184}
185
186static int
187save_microcode(struct mc_saved_data *mcs,
188 struct microcode_intel **mc_saved_src,
189 unsigned int num_saved)
190{
191 int i, j;
192 struct microcode_intel **saved_ptr;
193 int ret;
194
195 if (!num_saved)
196 return -EINVAL;
197
198
199
200
201 saved_ptr = kcalloc(num_saved, sizeof(struct microcode_intel *), GFP_KERNEL);
202 if (!saved_ptr)
203 return -ENOMEM;
204
205 for (i = 0; i < num_saved; i++) {
206 struct microcode_header_intel *mc_hdr;
207 struct microcode_intel *mc;
208 unsigned long size;
209
210 if (!mc_saved_src[i]) {
211 ret = -EINVAL;
212 goto err;
213 }
214
215 mc = mc_saved_src[i];
216 mc_hdr = &mc->hdr;
217 size = get_totalsize(mc_hdr);
218
219 saved_ptr[i] = kmemdup(mc, size, GFP_KERNEL);
220 if (!saved_ptr[i]) {
221 ret = -ENOMEM;
222 goto err;
223 }
224 }
225
226
227
228
229 mcs->mc_saved = saved_ptr;
230 mcs->num_saved = num_saved;
231
232 return 0;
233
234err:
235 for (j = 0; j <= i; j++)
236 kfree(saved_ptr[j]);
237 kfree(saved_ptr);
238
239 return ret;
240}
241
242
243
244
245
246
247
248
249
250
251
252static unsigned int _save_mc(struct microcode_intel **mc_saved,
253 u8 *ucode_ptr, unsigned int num_saved)
254{
255 struct microcode_header_intel *mc_hdr, *mc_saved_hdr;
256 unsigned int sig, pf;
257 int found = 0, i;
258
259 mc_hdr = (struct microcode_header_intel *)ucode_ptr;
260
261 for (i = 0; i < num_saved; i++) {
262 mc_saved_hdr = (struct microcode_header_intel *)mc_saved[i];
263 sig = mc_saved_hdr->sig;
264 pf = mc_saved_hdr->pf;
265
266 if (!find_matching_signature(ucode_ptr, sig, pf))
267 continue;
268
269 found = 1;
270
271 if (mc_hdr->rev <= mc_saved_hdr->rev)
272 continue;
273
274
275
276
277
278 mc_saved[i] = (struct microcode_intel *)ucode_ptr;
279 break;
280 }
281
282
283 if (i >= num_saved && !found)
284 mc_saved[num_saved++] = (struct microcode_intel *)ucode_ptr;
285
286 return num_saved;
287}
288
289
290
291
292
293static enum ucode_state __init
294get_matching_model_microcode(unsigned long start, void *data, size_t size,
295 struct mc_saved_data *mcs, unsigned long *mc_ptrs,
296 struct ucode_cpu_info *uci)
297{
298 struct microcode_intel *mc_saved_tmp[MAX_UCODE_COUNT];
299 struct microcode_header_intel *mc_header;
300 unsigned int num_saved = mcs->num_saved;
301 enum ucode_state state = UCODE_OK;
302 unsigned int leftover = size;
303 u8 *ucode_ptr = data;
304 unsigned int mc_size;
305 int i;
306
307 while (leftover && num_saved < ARRAY_SIZE(mc_saved_tmp)) {
308
309 if (leftover < sizeof(mc_header))
310 break;
311
312 mc_header = (struct microcode_header_intel *)ucode_ptr;
313
314 mc_size = get_totalsize(mc_header);
315 if (!mc_size || mc_size > leftover ||
316 microcode_sanity_check(ucode_ptr, 0) < 0)
317 break;
318
319 leftover -= mc_size;
320
321
322
323
324
325
326 if (matching_model_microcode(mc_header, uci->cpu_sig.sig) != UCODE_OK) {
327 ucode_ptr += mc_size;
328 continue;
329 }
330
331 num_saved = _save_mc(mc_saved_tmp, ucode_ptr, num_saved);
332
333 ucode_ptr += mc_size;
334 }
335
336 if (leftover) {
337 state = UCODE_ERROR;
338 return state;
339 }
340
341 if (!num_saved) {
342 state = UCODE_NFOUND;
343 return state;
344 }
345
346 for (i = 0; i < num_saved; i++)
347 mc_ptrs[i] = (unsigned long)mc_saved_tmp[i] - start;
348
349 mcs->num_saved = num_saved;
350
351 return state;
352}
353
354static int collect_cpu_info_early(struct ucode_cpu_info *uci)
355{
356 unsigned int val[2];
357 unsigned int family, model;
358 struct cpu_signature csig;
359 unsigned int eax, ebx, ecx, edx;
360
361 csig.sig = 0;
362 csig.pf = 0;
363 csig.rev = 0;
364
365 memset(uci, 0, sizeof(*uci));
366
367 eax = 0x00000001;
368 ecx = 0;
369 native_cpuid(&eax, &ebx, &ecx, &edx);
370 csig.sig = eax;
371
372 family = x86_family(csig.sig);
373 model = x86_model(csig.sig);
374
375 if ((model >= 5) || (family > 6)) {
376
377 native_rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]);
378 csig.pf = 1 << ((val[1] >> 18) & 7);
379 }
380 csig.rev = intel_get_microcode_revision();
381
382 uci->cpu_sig = csig;
383 uci->valid = 1;
384
385 return 0;
386}
387
388static void show_saved_mc(void)
389{
390#ifdef DEBUG
391 int i, j;
392 unsigned int sig, pf, rev, total_size, data_size, date;
393 struct ucode_cpu_info uci;
394
395 if (!mc_saved_data.num_saved) {
396 pr_debug("no microcode data saved.\n");
397 return;
398 }
399 pr_debug("Total microcode saved: %d\n", mc_saved_data.num_saved);
400
401 collect_cpu_info_early(&uci);
402
403 sig = uci.cpu_sig.sig;
404 pf = uci.cpu_sig.pf;
405 rev = uci.cpu_sig.rev;
406 pr_debug("CPU: sig=0x%x, pf=0x%x, rev=0x%x\n", sig, pf, rev);
407
408 for (i = 0; i < mc_saved_data.num_saved; 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
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#endif
449}
450
451#ifdef CONFIG_HOTPLUG_CPU
452static DEFINE_MUTEX(x86_cpu_microcode_mutex);
453
454
455
456
457
458
459
460int save_mc_for_early(u8 *mc)
461{
462 struct microcode_intel *mc_saved_tmp[MAX_UCODE_COUNT];
463 unsigned int mc_saved_count_init;
464 unsigned int num_saved;
465 struct microcode_intel **mc_saved;
466 int ret = 0;
467 int i;
468
469
470
471
472
473 mutex_lock(&x86_cpu_microcode_mutex);
474
475 mc_saved_count_init = mc_saved_data.num_saved;
476 num_saved = mc_saved_data.num_saved;
477 mc_saved = mc_saved_data.mc_saved;
478
479 if (mc_saved && num_saved)
480 memcpy(mc_saved_tmp, mc_saved,
481 num_saved * sizeof(struct microcode_intel *));
482
483
484
485
486 num_saved = _save_mc(mc_saved_tmp, mc, num_saved);
487
488
489
490
491 ret = save_microcode(&mc_saved_data, mc_saved_tmp, num_saved);
492 if (ret) {
493 pr_err("Cannot save microcode patch.\n");
494 goto out;
495 }
496
497 show_saved_mc();
498
499
500
501
502 if (mc_saved) {
503 for (i = 0; i < mc_saved_count_init; i++)
504 kfree(mc_saved[i]);
505 kfree(mc_saved);
506 }
507
508out:
509 mutex_unlock(&x86_cpu_microcode_mutex);
510
511 return ret;
512}
513EXPORT_SYMBOL_GPL(save_mc_for_early);
514#endif
515
516static bool __init load_builtin_intel_microcode(struct cpio_data *cp)
517{
518#ifdef CONFIG_X86_64
519 unsigned int eax = 0x00000001, ebx, ecx = 0, edx;
520 char name[30];
521
522 native_cpuid(&eax, &ebx, &ecx, &edx);
523
524 sprintf(name, "intel-ucode/%02x-%02x-%02x",
525 x86_family(eax), x86_model(eax), x86_stepping(eax));
526
527 return get_builtin_firmware(cp, name);
528#else
529 return false;
530#endif
531}
532
533
534
535
536static void
537print_ucode_info(struct ucode_cpu_info *uci, unsigned int date)
538{
539 pr_info_once("microcode updated early to revision 0x%x, date = %04x-%02x-%02x\n",
540 uci->cpu_sig.rev,
541 date & 0xffff,
542 date >> 24,
543 (date >> 16) & 0xff);
544}
545
546#ifdef CONFIG_X86_32
547
548static int delay_ucode_info;
549static int current_mc_date;
550
551
552
553
554void show_ucode_info_early(void)
555{
556 struct ucode_cpu_info uci;
557
558 if (delay_ucode_info) {
559 collect_cpu_info_early(&uci);
560 print_ucode_info(&uci, current_mc_date);
561 delay_ucode_info = 0;
562 }
563}
564
565
566
567
568
569
570static void print_ucode(struct ucode_cpu_info *uci)
571{
572 struct microcode_intel *mc;
573 int *delay_ucode_info_p;
574 int *current_mc_date_p;
575
576 mc = uci->mc;
577 if (!mc)
578 return;
579
580 delay_ucode_info_p = (int *)__pa_nodebug(&delay_ucode_info);
581 current_mc_date_p = (int *)__pa_nodebug(¤t_mc_date);
582
583 *delay_ucode_info_p = 1;
584 *current_mc_date_p = mc->hdr.date;
585}
586#else
587
588
589
590
591
592static inline void flush_tlb_early(void)
593{
594 __native_flush_tlb_global_irq_disabled();
595}
596
597static inline void print_ucode(struct ucode_cpu_info *uci)
598{
599 struct microcode_intel *mc;
600
601 mc = uci->mc;
602 if (!mc)
603 return;
604
605 print_ucode_info(uci, mc->hdr.date);
606}
607#endif
608
609static int apply_microcode_early(struct ucode_cpu_info *uci, bool early)
610{
611 struct microcode_intel *mc;
612 u32 rev;
613
614 mc = uci->mc;
615 if (!mc)
616 return 0;
617
618
619
620
621
622
623 rev = intel_get_microcode_revision();
624 if (rev >= mc->hdr.rev) {
625 uci->cpu_sig.rev = rev;
626 return UCODE_OK;
627 }
628
629
630
631
632
633 native_wbinvd();
634
635
636 native_wrmsrl(MSR_IA32_UCODE_WRITE, (unsigned long)mc->bits);
637 rev = intel_get_microcode_revision();
638 if (rev != mc->hdr.rev)
639 return -1;
640
641#ifdef CONFIG_X86_64
642
643 flush_tlb_early();
644#endif
645 uci->cpu_sig.rev = rev;
646
647 if (early)
648 print_ucode(uci);
649 else
650 print_ucode_info(uci, mc->hdr.date);
651
652 return 0;
653}
654
655
656
657
658
659int __init save_microcode_in_initrd_intel(void)
660{
661 struct microcode_intel *mc_saved[MAX_UCODE_COUNT];
662 unsigned int count = mc_saved_data.num_saved;
663 unsigned long offset = 0;
664 int ret;
665
666 if (!count)
667 return 0;
668
669
670
671
672
673 if (IS_ENABLED(CONFIG_BLK_DEV_INITRD) && blobs.valid)
674 offset = initrd_start;
675
676 copy_ptrs(mc_saved, mc_tmp_ptrs, offset, count);
677
678 ret = save_microcode(&mc_saved_data, mc_saved, count);
679 if (ret)
680 pr_err("Cannot save microcode patches from initrd.\n");
681
682 show_saved_mc();
683
684 return ret;
685}
686
687static __init enum ucode_state
688__scan_microcode_initrd(struct cpio_data *cd, struct ucode_blobs *blbp)
689{
690#ifdef CONFIG_BLK_DEV_INITRD
691 long offset = 0;
692 static __initdata char ucode_name[] = "kernel/x86/microcode/GenuineIntel.bin";
693 char *p = IS_ENABLED(CONFIG_X86_32) ? (char *)__pa_nodebug(ucode_name)
694 : ucode_name;
695# ifdef CONFIG_X86_32
696 unsigned long start = 0, size;
697 struct boot_params *params;
698
699 params = (struct boot_params *)__pa_nodebug(&boot_params);
700 size = params->hdr.ramdisk_size;
701
702
703
704
705
706 start = (size ? params->hdr.ramdisk_image : 0);
707
708# else
709 unsigned long start = 0, size;
710
711 size = (u64)boot_params.ext_ramdisk_size << 32;
712 size |= boot_params.hdr.ramdisk_size;
713
714 if (size) {
715 start = (u64)boot_params.ext_ramdisk_image << 32;
716 start |= boot_params.hdr.ramdisk_image;
717
718 start += PAGE_OFFSET;
719 }
720# endif
721
722 *cd = find_cpio_data(p, (void *)start, size, &offset);
723 if (cd->data) {
724 blbp->start = start;
725 blbp->valid = true;
726
727 return UCODE_OK;
728 } else
729#endif
730 return UCODE_ERROR;
731}
732
733static __init enum ucode_state
734scan_microcode(struct mc_saved_data *mcs, unsigned long *mc_ptrs,
735 struct ucode_cpu_info *uci, struct ucode_blobs *blbp)
736{
737 struct cpio_data cd = { NULL, 0, "" };
738 enum ucode_state ret;
739
740
741 if (load_builtin_intel_microcode(&cd))
742
743
744
745
746
747
748 blbp->valid = false;
749 else {
750 ret = __scan_microcode_initrd(&cd, blbp);
751 if (ret != UCODE_OK)
752 return ret;
753 }
754
755 return get_matching_model_microcode(blbp->start, cd.data, cd.size,
756 mcs, mc_ptrs, uci);
757}
758
759static void __init
760_load_ucode_intel_bsp(struct mc_saved_data *mcs, unsigned long *mc_ptrs,
761 struct ucode_blobs *blbp)
762{
763 struct ucode_cpu_info uci;
764 enum ucode_state ret;
765
766 collect_cpu_info_early(&uci);
767
768 ret = scan_microcode(mcs, mc_ptrs, &uci, blbp);
769 if (ret != UCODE_OK)
770 return;
771
772 ret = load_microcode(mcs, mc_ptrs, blbp->start, &uci);
773 if (ret != UCODE_OK)
774 return;
775
776 apply_microcode_early(&uci, true);
777}
778
779void __init load_ucode_intel_bsp(void)
780{
781 struct ucode_blobs *blobs_p;
782 struct mc_saved_data *mcs;
783 unsigned long *ptrs;
784
785#ifdef CONFIG_X86_32
786 mcs = (struct mc_saved_data *)__pa_nodebug(&mc_saved_data);
787 ptrs = (unsigned long *)__pa_nodebug(&mc_tmp_ptrs);
788 blobs_p = (struct ucode_blobs *)__pa_nodebug(&blobs);
789#else
790 mcs = &mc_saved_data;
791 ptrs = mc_tmp_ptrs;
792 blobs_p = &blobs;
793#endif
794
795 _load_ucode_intel_bsp(mcs, ptrs, blobs_p);
796}
797
798void load_ucode_intel_ap(void)
799{
800 struct ucode_blobs *blobs_p;
801 unsigned long *ptrs, start = 0;
802 struct mc_saved_data *mcs;
803 struct ucode_cpu_info uci;
804 enum ucode_state ret;
805
806#ifdef CONFIG_X86_32
807 mcs = (struct mc_saved_data *)__pa_nodebug(&mc_saved_data);
808 ptrs = (unsigned long *)__pa_nodebug(mc_tmp_ptrs);
809 blobs_p = (struct ucode_blobs *)__pa_nodebug(&blobs);
810#else
811 mcs = &mc_saved_data;
812 ptrs = mc_tmp_ptrs;
813 blobs_p = &blobs;
814#endif
815
816
817
818
819
820 if (!mcs->num_saved)
821 return;
822
823 if (blobs_p->valid) {
824 start = blobs_p->start;
825
826
827
828
829
830 start += PAGE_OFFSET - __PAGE_OFFSET_BASE;
831 }
832 if (initrd_start)
833 start = initrd_start;
834
835 collect_cpu_info_early(&uci);
836 ret = load_microcode(mcs, ptrs, start, &uci);
837 if (ret != UCODE_OK)
838 return;
839
840 apply_microcode_early(&uci, true);
841}
842
843void reload_ucode_intel(void)
844{
845 struct ucode_cpu_info uci;
846 enum ucode_state ret;
847
848 if (!mc_saved_data.num_saved)
849 return;
850
851 collect_cpu_info_early(&uci);
852
853 ret = load_microcode_early(mc_saved_data.mc_saved,
854 mc_saved_data.num_saved, &uci);
855 if (ret != UCODE_OK)
856 return;
857
858 apply_microcode_early(&uci, false);
859}
860
861static int collect_cpu_info(int cpu_num, struct cpu_signature *csig)
862{
863 static struct cpu_signature prev;
864 struct cpuinfo_x86 *c = &cpu_data(cpu_num);
865 unsigned int val[2];
866
867 memset(csig, 0, sizeof(*csig));
868
869 csig->sig = cpuid_eax(0x00000001);
870
871 if ((c->x86_model >= 5) || (c->x86 > 6)) {
872
873 rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]);
874 csig->pf = 1 << ((val[1] >> 18) & 7);
875 }
876
877 csig->rev = c->microcode;
878
879
880 if (csig->sig != prev.sig || csig->pf != prev.pf || csig->rev != prev.rev) {
881 pr_info("sig=0x%x, pf=0x%x, revision=0x%x\n",
882 csig->sig, csig->pf, csig->rev);
883 prev = *csig;
884 }
885
886 return 0;
887}
888
889
890
891
892
893static int get_matching_mc(struct microcode_intel *mc, int cpu)
894{
895 struct cpu_signature cpu_sig;
896 unsigned int csig, cpf, crev;
897
898 collect_cpu_info(cpu, &cpu_sig);
899
900 csig = cpu_sig.sig;
901 cpf = cpu_sig.pf;
902 crev = cpu_sig.rev;
903
904 return has_newer_microcode(mc, csig, cpf, crev);
905}
906
907static enum ucode_state apply_microcode_intel(int cpu)
908{
909 struct microcode_intel *mc;
910 struct ucode_cpu_info *uci;
911 struct cpuinfo_x86 *c = &cpu_data(cpu);
912 static int prev_rev;
913 u32 rev;
914
915
916 if (WARN_ON(raw_smp_processor_id() != cpu))
917 return UCODE_ERROR;
918
919 uci = ucode_cpu_info + cpu;
920 mc = uci->mc;
921 if (!mc)
922 return UCODE_NFOUND;
923
924
925
926
927
928
929 if (!get_matching_mc(mc, cpu))
930 return UCODE_NFOUND;
931
932
933
934
935
936
937 rev = intel_get_microcode_revision();
938 if (rev >= mc->hdr.rev) {
939 uci->cpu_sig.rev = rev;
940 c->microcode = rev;
941 return UCODE_OK;
942 }
943
944
945
946
947
948 native_wbinvd();
949
950
951 wrmsrl(MSR_IA32_UCODE_WRITE, (unsigned long)mc->bits);
952 rev = intel_get_microcode_revision();
953
954 if (rev != mc->hdr.rev) {
955 pr_err("CPU%d update to revision 0x%x failed\n",
956 cpu, mc->hdr.rev);
957 return UCODE_ERROR;
958 }
959
960 if (rev != prev_rev) {
961 pr_info("updated to revision 0x%x, date = %04x-%02x-%02x\n",
962 rev,
963 mc->hdr.date & 0xffff,
964 mc->hdr.date >> 24,
965 (mc->hdr.date >> 16) & 0xff);
966 prev_rev = rev;
967 }
968
969 uci->cpu_sig.rev = rev;
970 c->microcode = rev;
971
972 return UCODE_UPDATED;
973}
974
975static enum ucode_state generic_load_microcode(int cpu, void *data, size_t size,
976 int (*get_ucode_data)(void *, const void *, size_t))
977{
978 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
979 u8 *ucode_ptr = data, *new_mc = NULL, *mc = NULL;
980 int new_rev = uci->cpu_sig.rev;
981 unsigned int leftover = size;
982 unsigned int curr_mc_size = 0;
983 unsigned int csig, cpf;
984 enum ucode_state ret = UCODE_OK;
985
986 while (leftover) {
987 struct microcode_header_intel mc_header;
988 unsigned int mc_size;
989
990 if (leftover < sizeof(mc_header)) {
991 pr_err("error! Truncated header in microcode data file\n");
992 break;
993 }
994
995 if (get_ucode_data(&mc_header, ucode_ptr, sizeof(mc_header)))
996 break;
997
998 mc_size = get_totalsize(&mc_header);
999 if (!mc_size || mc_size > leftover) {
1000 pr_err("error! Bad data in microcode data file\n");
1001 break;
1002 }
1003
1004
1005 if (!mc || mc_size > curr_mc_size) {
1006 vfree(mc);
1007 mc = vmalloc(mc_size);
1008 if (!mc)
1009 break;
1010 curr_mc_size = mc_size;
1011 }
1012
1013 if (get_ucode_data(mc, ucode_ptr, mc_size) ||
1014 microcode_sanity_check(mc, 1) < 0) {
1015 break;
1016 }
1017
1018 csig = uci->cpu_sig.sig;
1019 cpf = uci->cpu_sig.pf;
1020 if (has_newer_microcode(mc, csig, cpf, new_rev)) {
1021 vfree(new_mc);
1022 new_rev = mc_header.rev;
1023 new_mc = mc;
1024 mc = NULL;
1025 ret = UCODE_NEW;
1026 }
1027
1028 ucode_ptr += mc_size;
1029 leftover -= mc_size;
1030 }
1031
1032 vfree(mc);
1033
1034 if (leftover) {
1035 vfree(new_mc);
1036 return UCODE_ERROR;
1037 }
1038
1039 if (!new_mc)
1040 return UCODE_NFOUND;
1041
1042 vfree(uci->mc);
1043 uci->mc = (struct microcode_intel *)new_mc;
1044
1045
1046
1047
1048
1049
1050 save_mc_for_early(new_mc);
1051
1052 pr_debug("CPU%d found a matching microcode update with version 0x%x (current=0x%x)\n",
1053 cpu, new_rev, uci->cpu_sig.rev);
1054
1055 return ret;
1056}
1057
1058static int get_ucode_fw(void *to, const void *from, size_t n)
1059{
1060 memcpy(to, from, n);
1061 return 0;
1062}
1063
1064static enum ucode_state request_microcode_fw(int cpu, struct device *device,
1065 bool refresh_fw)
1066{
1067 char name[30];
1068 struct cpuinfo_x86 *c = &cpu_data(cpu);
1069 const struct firmware *firmware;
1070 enum ucode_state ret;
1071
1072 sprintf(name, "intel-ucode/%02x-%02x-%02x",
1073 c->x86, c->x86_model, c->x86_mask);
1074
1075 if (request_firmware(&firmware, name, device)) {
1076 pr_debug("data file %s load failed\n", name);
1077 return UCODE_NFOUND;
1078 }
1079
1080 ret = generic_load_microcode(cpu, (void *)firmware->data,
1081 firmware->size, &get_ucode_fw);
1082
1083 release_firmware(firmware);
1084
1085 return ret;
1086}
1087
1088static int get_ucode_user(void *to, const void *from, size_t n)
1089{
1090 return copy_from_user(to, from, n);
1091}
1092
1093static enum ucode_state
1094request_microcode_user(int cpu, const void __user *buf, size_t size)
1095{
1096 return generic_load_microcode(cpu, (void *)buf, size, &get_ucode_user);
1097}
1098
1099static void microcode_fini_cpu(int cpu)
1100{
1101 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
1102
1103 vfree(uci->mc);
1104 uci->mc = NULL;
1105}
1106
1107static struct microcode_ops microcode_intel_ops = {
1108 .request_microcode_user = request_microcode_user,
1109 .request_microcode_fw = request_microcode_fw,
1110 .collect_cpu_info = collect_cpu_info,
1111 .apply_microcode = apply_microcode_intel,
1112 .microcode_fini_cpu = microcode_fini_cpu,
1113};
1114
1115struct microcode_ops * __init init_intel_microcode(void)
1116{
1117 struct cpuinfo_x86 *c = &boot_cpu_data;
1118
1119 if (c->x86_vendor != X86_VENDOR_INTEL || c->x86 < 6 ||
1120 cpu_has(c, X86_FEATURE_IA64)) {
1121 pr_err("Intel CPU family 0x%x not supported\n", c->x86);
1122 return NULL;
1123 }
1124
1125 return µcode_intel_ops;
1126}
1127
1128