blob: b624b54912e11e4e58992b9e1ca2d7cab20ce9e4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Borislav Petkov6b44e722015-05-11 10:15:47 +02002 * Intel CPU Microcode Update Driver for Linux
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Borislav Petkov6b44e722015-05-11 10:15:47 +02004 * Copyright (C) 2000-2006 Tigran Aivazian <tigran@aivazian.fsnet.co.uk>
5 * 2006 Shaohua Li <shaohua.li@intel.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
Borislav Petkovfe055892015-10-20 11:54:45 +02007 * 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 *
Borislav Petkov6b44e722015-05-11 10:15:47 +020012 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 */
Joe Perchesf58e1f52009-12-08 22:30:50 -080017
Borislav Petkovfe055892015-10-20 11:54:45 +020018/*
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 */
Borislav Petkov6b26e1b2015-10-20 11:54:46 +020024#define pr_fmt(fmt) "microcode: " fmt
Joe Perchesf58e1f52009-12-08 22:30:50 -080025
Borislav Petkovfe055892015-10-20 11:54:45 +020026#include <linux/earlycpio.h>
Ingo Molnar4bae1962009-03-11 11:19:46 +010027#include <linux/firmware.h>
Jaswinder Singh Rajputdd3feda2009-01-12 14:44:29 +053028#include <linux/uaccess.h>
Borislav Petkovfe055892015-10-20 11:54:45 +020029#include <linux/vmalloc.h>
30#include <linux/initrd.h>
Ingo Molnar4bae1962009-03-11 11:19:46 +010031#include <linux/kernel.h>
Borislav Petkovfe055892015-10-20 11:54:45 +020032#include <linux/slab.h>
33#include <linux/cpu.h>
34#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Fenghua Yu9cd4d782012-12-20 23:44:22 -080036#include <asm/microcode_intel.h>
Ingo Molnar4bae1962009-03-11 11:19:46 +010037#include <asm/processor.h>
Borislav Petkovfe055892015-10-20 11:54:45 +020038#include <asm/tlbflush.h>
39#include <asm/setup.h>
Ingo Molnar4bae1962009-03-11 11:19:46 +010040#include <asm/msr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Borislav Petkov06b85342016-10-25 11:55:21 +020042static const char ucode_path[] = "kernel/x86/microcode/GenuineIntel.bin";
Borislav Petkovf8bb45e2016-02-03 12:33:41 +010043
Borislav Petkov06b85342016-10-25 11:55:21 +020044/* Current microcode patch used in early patching */
45struct microcode_intel *intel_ucode_patch;
Borislav Petkov6c545642016-06-06 17:10:42 +020046
Borislav Petkov80279232016-10-25 11:55:20 +020047static inline bool cpu_signatures_match(unsigned int s1, unsigned int p1,
48 unsigned int s2, unsigned int p2)
49{
50 if (s1 != s2)
51 return false;
52
53 /* Processor flags are either both 0 ... */
54 if (!p1 && !p2)
55 return true;
56
57 /* ... or they intersect. */
58 return p1 & p2;
59}
60
61/*
62 * Returns 1 if update has been found, 0 otherwise.
63 */
64static int find_matching_signature(void *mc, unsigned int csig, int cpf)
65{
66 struct microcode_header_intel *mc_hdr = mc;
67 struct extended_sigtable *ext_hdr;
68 struct extended_signature *ext_sig;
69 int i;
70
71 if (cpu_signatures_match(csig, cpf, mc_hdr->sig, mc_hdr->pf))
72 return 1;
73
74 /* Look for ext. headers: */
75 if (get_totalsize(mc_hdr) <= get_datasize(mc_hdr) + MC_HEADER_SIZE)
76 return 0;
77
78 ext_hdr = mc + get_datasize(mc_hdr) + MC_HEADER_SIZE;
79 ext_sig = (void *)ext_hdr + EXT_HEADER_SIZE;
80
81 for (i = 0; i < ext_hdr->count; i++) {
82 if (cpu_signatures_match(csig, cpf, ext_sig->sig, ext_sig->pf))
83 return 1;
84 ext_sig++;
85 }
86 return 0;
87}
88
89/*
90 * Returns 1 if update has been found, 0 otherwise.
91 */
92static int has_newer_microcode(void *mc, unsigned int csig, int cpf, int new_rev)
93{
94 struct microcode_header_intel *mc_hdr = mc;
95
96 if (mc_hdr->rev <= new_rev)
97 return 0;
98
99 return find_matching_signature(mc, csig, cpf);
100}
101
Borislav Petkovfe055892015-10-20 11:54:45 +0200102/*
103 * Given CPU signature and a microcode patch, this function finds if the
104 * microcode patch has matching family and model with the CPU.
Borislav Petkov06b85342016-10-25 11:55:21 +0200105 *
106 * %true - if there's a match
107 * %false - otherwise
Borislav Petkovfe055892015-10-20 11:54:45 +0200108 */
Borislav Petkov06b85342016-10-25 11:55:21 +0200109static bool microcode_matches(struct microcode_header_intel *mc_header,
110 unsigned long sig)
Borislav Petkovfe055892015-10-20 11:54:45 +0200111{
Borislav Petkovfe055892015-10-20 11:54:45 +0200112 unsigned long total_size = get_totalsize(mc_header);
113 unsigned long data_size = get_datasize(mc_header);
Borislav Petkov06b85342016-10-25 11:55:21 +0200114 struct extended_sigtable *ext_header;
115 unsigned int fam_ucode, model_ucode;
Borislav Petkovfe055892015-10-20 11:54:45 +0200116 struct extended_signature *ext_sig;
Borislav Petkov06b85342016-10-25 11:55:21 +0200117 unsigned int fam, model;
118 int ext_sigcount, i;
Borislav Petkovfe055892015-10-20 11:54:45 +0200119
Borislav Petkov99f925c2015-11-23 11:12:21 +0100120 fam = x86_family(sig);
Borislav Petkovfe055892015-10-20 11:54:45 +0200121 model = x86_model(sig);
122
Borislav Petkov99f925c2015-11-23 11:12:21 +0100123 fam_ucode = x86_family(mc_header->sig);
Borislav Petkovfe055892015-10-20 11:54:45 +0200124 model_ucode = x86_model(mc_header->sig);
125
126 if (fam == fam_ucode && model == model_ucode)
Borislav Petkov06b85342016-10-25 11:55:21 +0200127 return true;
Borislav Petkovfe055892015-10-20 11:54:45 +0200128
129 /* Look for ext. headers: */
130 if (total_size <= data_size + MC_HEADER_SIZE)
Borislav Petkov06b85342016-10-25 11:55:21 +0200131 return false;
Borislav Petkovfe055892015-10-20 11:54:45 +0200132
133 ext_header = (void *) mc_header + data_size + MC_HEADER_SIZE;
134 ext_sig = (void *)ext_header + EXT_HEADER_SIZE;
135 ext_sigcount = ext_header->count;
136
137 for (i = 0; i < ext_sigcount; i++) {
Borislav Petkov99f925c2015-11-23 11:12:21 +0100138 fam_ucode = x86_family(ext_sig->sig);
Borislav Petkovfe055892015-10-20 11:54:45 +0200139 model_ucode = x86_model(ext_sig->sig);
140
141 if (fam == fam_ucode && model == model_ucode)
Borislav Petkov06b85342016-10-25 11:55:21 +0200142 return true;
Borislav Petkovfe055892015-10-20 11:54:45 +0200143
144 ext_sig++;
145 }
Borislav Petkov06b85342016-10-25 11:55:21 +0200146 return false;
Borislav Petkovfe055892015-10-20 11:54:45 +0200147}
148
Borislav Petkov06b85342016-10-25 11:55:21 +0200149static struct ucode_patch *__alloc_microcode_buf(void *data, unsigned int size)
Borislav Petkovfe055892015-10-20 11:54:45 +0200150{
Borislav Petkov06b85342016-10-25 11:55:21 +0200151 struct ucode_patch *p;
Borislav Petkovfe055892015-10-20 11:54:45 +0200152
Borislav Petkov06b85342016-10-25 11:55:21 +0200153 p = kzalloc(size, GFP_KERNEL);
154 if (!p)
155 return ERR_PTR(-ENOMEM);
Borislav Petkovfe055892015-10-20 11:54:45 +0200156
Borislav Petkov06b85342016-10-25 11:55:21 +0200157 p->data = kmemdup(data, size, GFP_KERNEL);
158 if (!p->data) {
159 kfree(p);
160 return ERR_PTR(-ENOMEM);
Borislav Petkovfe055892015-10-20 11:54:45 +0200161 }
162
Borislav Petkov06b85342016-10-25 11:55:21 +0200163 return p;
Borislav Petkovfe055892015-10-20 11:54:45 +0200164}
165
Borislav Petkov06b85342016-10-25 11:55:21 +0200166static void save_microcode_patch(void *data, unsigned int size)
Borislav Petkovfe055892015-10-20 11:54:45 +0200167{
168 struct microcode_header_intel *mc_hdr, *mc_saved_hdr;
Borislav Petkov06b85342016-10-25 11:55:21 +0200169 struct ucode_patch *iter, *tmp, *p;
170 bool prev_found = false;
Borislav Petkovfe055892015-10-20 11:54:45 +0200171 unsigned int sig, pf;
Borislav Petkovfe055892015-10-20 11:54:45 +0200172
Borislav Petkov06b85342016-10-25 11:55:21 +0200173 mc_hdr = (struct microcode_header_intel *)data;
Borislav Petkovfe055892015-10-20 11:54:45 +0200174
Borislav Petkov06b85342016-10-25 11:55:21 +0200175 list_for_each_entry_safe(iter, tmp, &microcode_cache, plist) {
176 mc_saved_hdr = (struct microcode_header_intel *)iter->data;
Borislav Petkovfe055892015-10-20 11:54:45 +0200177 sig = mc_saved_hdr->sig;
178 pf = mc_saved_hdr->pf;
179
Borislav Petkov06b85342016-10-25 11:55:21 +0200180 if (find_matching_signature(data, sig, pf)) {
181 prev_found = true;
Borislav Petkovfe055892015-10-20 11:54:45 +0200182
Borislav Petkov06b85342016-10-25 11:55:21 +0200183 if (mc_hdr->rev <= mc_saved_hdr->rev)
184 continue;
Borislav Petkovfe055892015-10-20 11:54:45 +0200185
Borislav Petkov06b85342016-10-25 11:55:21 +0200186 p = __alloc_microcode_buf(data, size);
187 if (IS_ERR(p))
188 pr_err("Error allocating buffer %p\n", data);
189 else
190 list_replace(&iter->plist, &p->plist);
191 }
Borislav Petkovfe055892015-10-20 11:54:45 +0200192 }
193
Borislav Petkov06b85342016-10-25 11:55:21 +0200194 /*
195 * There weren't any previous patches found in the list cache; save the
196 * newly found.
197 */
198 if (!prev_found) {
199 p = __alloc_microcode_buf(data, size);
200 if (IS_ERR(p))
201 pr_err("Error allocating buffer for %p\n", data);
202 else
203 list_add_tail(&p->plist, &microcode_cache);
204 }
Borislav Petkovfe055892015-10-20 11:54:45 +0200205}
206
Borislav Petkov80279232016-10-25 11:55:20 +0200207static int microcode_sanity_check(void *mc, int print_err)
208{
209 unsigned long total_size, data_size, ext_table_size;
210 struct microcode_header_intel *mc_header = mc;
211 struct extended_sigtable *ext_header = NULL;
212 u32 sum, orig_sum, ext_sigcount = 0, i;
213 struct extended_signature *ext_sig;
214
215 total_size = get_totalsize(mc_header);
216 data_size = get_datasize(mc_header);
217
218 if (data_size + MC_HEADER_SIZE > total_size) {
219 if (print_err)
220 pr_err("Error: bad microcode data file size.\n");
221 return -EINVAL;
222 }
223
224 if (mc_header->ldrver != 1 || mc_header->hdrver != 1) {
225 if (print_err)
226 pr_err("Error: invalid/unknown microcode update format.\n");
227 return -EINVAL;
228 }
229
230 ext_table_size = total_size - (MC_HEADER_SIZE + data_size);
231 if (ext_table_size) {
232 u32 ext_table_sum = 0;
233 u32 *ext_tablep;
234
235 if ((ext_table_size < EXT_HEADER_SIZE)
236 || ((ext_table_size - EXT_HEADER_SIZE) % EXT_SIGNATURE_SIZE)) {
237 if (print_err)
238 pr_err("Error: truncated extended signature table.\n");
239 return -EINVAL;
240 }
241
242 ext_header = mc + MC_HEADER_SIZE + data_size;
243 if (ext_table_size != exttable_size(ext_header)) {
244 if (print_err)
245 pr_err("Error: extended signature table size mismatch.\n");
246 return -EFAULT;
247 }
248
249 ext_sigcount = ext_header->count;
250
251 /*
252 * Check extended table checksum: the sum of all dwords that
253 * comprise a valid table must be 0.
254 */
255 ext_tablep = (u32 *)ext_header;
256
257 i = ext_table_size / sizeof(u32);
258 while (i--)
259 ext_table_sum += ext_tablep[i];
260
261 if (ext_table_sum) {
262 if (print_err)
263 pr_warn("Bad extended signature table checksum, aborting.\n");
264 return -EINVAL;
265 }
266 }
267
268 /*
269 * Calculate the checksum of update data and header. The checksum of
270 * valid update data and header including the extended signature table
271 * must be 0.
272 */
273 orig_sum = 0;
274 i = (MC_HEADER_SIZE + data_size) / sizeof(u32);
275 while (i--)
276 orig_sum += ((u32 *)mc)[i];
277
278 if (orig_sum) {
279 if (print_err)
280 pr_err("Bad microcode data checksum, aborting.\n");
281 return -EINVAL;
282 }
283
284 if (!ext_table_size)
285 return 0;
286
287 /*
288 * Check extended signature checksum: 0 => valid.
289 */
290 for (i = 0; i < ext_sigcount; i++) {
291 ext_sig = (void *)ext_header + EXT_HEADER_SIZE +
292 EXT_SIGNATURE_SIZE * i;
293
294 sum = (mc_header->sig + mc_header->pf + mc_header->cksum) -
295 (ext_sig->sig + ext_sig->pf + ext_sig->cksum);
296 if (sum) {
297 if (print_err)
298 pr_err("Bad extended signature checksum, aborting.\n");
299 return -EINVAL;
300 }
301 }
302 return 0;
303}
304
Borislav Petkovfe055892015-10-20 11:54:45 +0200305/*
306 * Get microcode matching with BSP's model. Only CPUs with the same model as
307 * BSP can stay in the platform.
308 */
Borislav Petkov06b85342016-10-25 11:55:21 +0200309static struct microcode_intel *
310scan_microcode(void *data, size_t size, struct ucode_cpu_info *uci, bool save)
Borislav Petkovfe055892015-10-20 11:54:45 +0200311{
Borislav Petkovf96fde52016-02-03 12:33:43 +0100312 struct microcode_header_intel *mc_header;
Borislav Petkov06b85342016-10-25 11:55:21 +0200313 struct microcode_intel *patch = NULL;
Borislav Petkovf96fde52016-02-03 12:33:43 +0100314 unsigned int mc_size;
Borislav Petkovfe055892015-10-20 11:54:45 +0200315
Borislav Petkov06b85342016-10-25 11:55:21 +0200316 while (size) {
317 if (size < sizeof(struct microcode_header_intel))
Borislav Petkovfe055892015-10-20 11:54:45 +0200318 break;
319
Borislav Petkov06b85342016-10-25 11:55:21 +0200320 mc_header = (struct microcode_header_intel *)data;
Borislav Petkovfe055892015-10-20 11:54:45 +0200321
322 mc_size = get_totalsize(mc_header);
Borislav Petkov06b85342016-10-25 11:55:21 +0200323 if (!mc_size ||
324 mc_size > size ||
325 microcode_sanity_check(data, 0) < 0)
Borislav Petkovfe055892015-10-20 11:54:45 +0200326 break;
327
Borislav Petkov06b85342016-10-25 11:55:21 +0200328 size -= mc_size;
Borislav Petkovfe055892015-10-20 11:54:45 +0200329
Borislav Petkov06b85342016-10-25 11:55:21 +0200330 if (!microcode_matches(mc_header, uci->cpu_sig.sig)) {
331 data += mc_size;
Borislav Petkovfe055892015-10-20 11:54:45 +0200332 continue;
333 }
334
Borislav Petkov06b85342016-10-25 11:55:21 +0200335 if (save) {
336 save_microcode_patch(data, mc_size);
337 goto next;
338 }
Borislav Petkovfe055892015-10-20 11:54:45 +0200339
Borislav Petkov06b85342016-10-25 11:55:21 +0200340
341 if (!patch) {
342 if (!has_newer_microcode(data,
343 uci->cpu_sig.sig,
344 uci->cpu_sig.pf,
345 uci->cpu_sig.rev))
346 goto next;
347
348 } else {
349 struct microcode_header_intel *phdr = &patch->hdr;
350
351 if (!has_newer_microcode(data,
352 phdr->sig,
353 phdr->pf,
354 phdr->rev))
355 goto next;
356 }
357
358 /* We have a newer patch, save it. */
359 patch = data;
360
361next:
362 data += mc_size;
Borislav Petkovfe055892015-10-20 11:54:45 +0200363 }
364
Borislav Petkov06b85342016-10-25 11:55:21 +0200365 if (size)
366 return NULL;
Borislav Petkovfe055892015-10-20 11:54:45 +0200367
Borislav Petkov06b85342016-10-25 11:55:21 +0200368 return patch;
Borislav Petkovfe055892015-10-20 11:54:45 +0200369}
370
Andy Lutomirski484d0e52016-12-09 10:24:07 -0800371static void cpuid_1(void)
372{
373 /*
374 * According to the Intel SDM, Volume 3, 9.11.7:
375 *
376 * CPUID returns a value in a model specific register in
377 * addition to its usual register return values. The
378 * semantics of CPUID cause it to deposit an update ID value
379 * in the 64-bit model-specific register at address 08BH
380 * (IA32_BIOS_SIGN_ID). If no update is present in the
381 * processor, the value in the MSR remains unmodified.
382 *
383 * Use native_cpuid -- this code runs very early and we don't
384 * want to mess with paravirt.
385 */
386 unsigned int eax = 1, ebx, ecx = 0, edx;
387
388 native_cpuid(&eax, &ebx, &ecx, &edx);
389}
390
Borislav Petkovfe055892015-10-20 11:54:45 +0200391static int collect_cpu_info_early(struct ucode_cpu_info *uci)
392{
393 unsigned int val[2];
394 unsigned int family, model;
Borislav Petkov06b85342016-10-25 11:55:21 +0200395 struct cpu_signature csig = { 0 };
Borislav Petkovfe055892015-10-20 11:54:45 +0200396 unsigned int eax, ebx, ecx, edx;
397
Borislav Petkovfe055892015-10-20 11:54:45 +0200398 memset(uci, 0, sizeof(*uci));
399
400 eax = 0x00000001;
401 ecx = 0;
402 native_cpuid(&eax, &ebx, &ecx, &edx);
403 csig.sig = eax;
404
Borislav Petkov06b85342016-10-25 11:55:21 +0200405 family = x86_family(eax);
406 model = x86_model(eax);
Borislav Petkovfe055892015-10-20 11:54:45 +0200407
408 if ((model >= 5) || (family > 6)) {
409 /* get processor flags from MSR 0x17 */
410 native_rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]);
411 csig.pf = 1 << ((val[1] >> 18) & 7);
412 }
Borislav Petkovc416e612016-02-03 12:33:40 +0100413 native_wrmsrl(MSR_IA32_UCODE_REV, 0);
Borislav Petkovfe055892015-10-20 11:54:45 +0200414
415 /* As documented in the SDM: Do a CPUID 1 here */
Andy Lutomirski484d0e52016-12-09 10:24:07 -0800416 cpuid_1();
Borislav Petkovfe055892015-10-20 11:54:45 +0200417
418 /* get the current revision from MSR 0x8B */
419 native_rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
420
421 csig.rev = val[1];
422
423 uci->cpu_sig = csig;
424 uci->valid = 1;
425
426 return 0;
427}
428
Borislav Petkovfe055892015-10-20 11:54:45 +0200429static void show_saved_mc(void)
430{
Borislav Petkovc595ac22015-10-20 11:54:48 +0200431#ifdef DEBUG
Borislav Petkov06b85342016-10-25 11:55:21 +0200432 int i = 0, j;
Borislav Petkovfe055892015-10-20 11:54:45 +0200433 unsigned int sig, pf, rev, total_size, data_size, date;
434 struct ucode_cpu_info uci;
Borislav Petkov06b85342016-10-25 11:55:21 +0200435 struct ucode_patch *p;
Borislav Petkovfe055892015-10-20 11:54:45 +0200436
Borislav Petkov06b85342016-10-25 11:55:21 +0200437 if (list_empty(&microcode_cache)) {
Borislav Petkovfe055892015-10-20 11:54:45 +0200438 pr_debug("no microcode data saved.\n");
439 return;
440 }
Borislav Petkovfe055892015-10-20 11:54:45 +0200441
442 collect_cpu_info_early(&uci);
443
Borislav Petkov06b85342016-10-25 11:55:21 +0200444 sig = uci.cpu_sig.sig;
445 pf = uci.cpu_sig.pf;
446 rev = uci.cpu_sig.rev;
Borislav Petkovfe055892015-10-20 11:54:45 +0200447 pr_debug("CPU: sig=0x%x, pf=0x%x, rev=0x%x\n", sig, pf, rev);
448
Borislav Petkov06b85342016-10-25 11:55:21 +0200449 list_for_each_entry(p, &microcode_cache, plist) {
Borislav Petkovfe055892015-10-20 11:54:45 +0200450 struct microcode_header_intel *mc_saved_header;
451 struct extended_sigtable *ext_header;
Borislav Petkovfe055892015-10-20 11:54:45 +0200452 struct extended_signature *ext_sig;
Borislav Petkov06b85342016-10-25 11:55:21 +0200453 int ext_sigcount;
Borislav Petkovfe055892015-10-20 11:54:45 +0200454
Borislav Petkov06b85342016-10-25 11:55:21 +0200455 mc_saved_header = (struct microcode_header_intel *)p->data;
456
457 sig = mc_saved_header->sig;
458 pf = mc_saved_header->pf;
459 rev = mc_saved_header->rev;
460 date = mc_saved_header->date;
461
462 total_size = get_totalsize(mc_saved_header);
463 data_size = get_datasize(mc_saved_header);
Borislav Petkovfe055892015-10-20 11:54:45 +0200464
Masanari Iidac19ca6c2016-02-08 20:53:12 +0900465 pr_debug("mc_saved[%d]: sig=0x%x, pf=0x%x, rev=0x%x, total size=0x%x, date = %04x-%02x-%02x\n",
Borislav Petkov06b85342016-10-25 11:55:21 +0200466 i++, sig, pf, rev, total_size,
Borislav Petkovfe055892015-10-20 11:54:45 +0200467 date & 0xffff,
468 date >> 24,
469 (date >> 16) & 0xff);
470
471 /* Look for ext. headers: */
472 if (total_size <= data_size + MC_HEADER_SIZE)
473 continue;
474
Borislav Petkov06b85342016-10-25 11:55:21 +0200475 ext_header = (void *)mc_saved_header + data_size + MC_HEADER_SIZE;
Borislav Petkovfe055892015-10-20 11:54:45 +0200476 ext_sigcount = ext_header->count;
477 ext_sig = (void *)ext_header + EXT_HEADER_SIZE;
478
479 for (j = 0; j < ext_sigcount; j++) {
480 sig = ext_sig->sig;
481 pf = ext_sig->pf;
482
483 pr_debug("\tExtended[%d]: sig=0x%x, pf=0x%x\n",
484 j, sig, pf);
485
486 ext_sig++;
487 }
Borislav Petkovfe055892015-10-20 11:54:45 +0200488 }
Borislav Petkovfe055892015-10-20 11:54:45 +0200489#endif
Borislav Petkovc595ac22015-10-20 11:54:48 +0200490}
Borislav Petkovfe055892015-10-20 11:54:45 +0200491
Borislav Petkovfe055892015-10-20 11:54:45 +0200492/*
Borislav Petkov06b85342016-10-25 11:55:21 +0200493 * Save this microcode patch. It will be loaded early when a CPU is
494 * hot-added or resumes.
Borislav Petkovfe055892015-10-20 11:54:45 +0200495 */
Borislav Petkov06b85342016-10-25 11:55:21 +0200496static void save_mc_for_early(u8 *mc, unsigned int size)
Borislav Petkovfe055892015-10-20 11:54:45 +0200497{
Borislav Petkov0c5fa822016-06-06 17:10:48 +0200498#ifdef CONFIG_HOTPLUG_CPU
Borislav Petkov9f3cc2a2016-06-06 17:10:50 +0200499 /* Synchronization during CPU hotplug. */
Borislav Petkov0c5fa822016-06-06 17:10:48 +0200500 static DEFINE_MUTEX(x86_cpu_microcode_mutex);
501
Borislav Petkovfe055892015-10-20 11:54:45 +0200502 mutex_lock(&x86_cpu_microcode_mutex);
503
Borislav Petkov06b85342016-10-25 11:55:21 +0200504 save_microcode_patch(mc, size);
Borislav Petkovfe055892015-10-20 11:54:45 +0200505 show_saved_mc();
506
Borislav Petkovfe055892015-10-20 11:54:45 +0200507 mutex_unlock(&x86_cpu_microcode_mutex);
Borislav Petkovfe055892015-10-20 11:54:45 +0200508#endif
Borislav Petkov0c5fa822016-06-06 17:10:48 +0200509}
Borislav Petkovfe055892015-10-20 11:54:45 +0200510
Borislav Petkov06b85342016-10-25 11:55:21 +0200511static bool load_builtin_intel_microcode(struct cpio_data *cp)
Borislav Petkovfe055892015-10-20 11:54:45 +0200512{
Borislav Petkov06b85342016-10-25 11:55:21 +0200513 unsigned int eax = 1, ebx, ecx = 0, edx;
Borislav Petkovfe055892015-10-20 11:54:45 +0200514 char name[30];
515
Borislav Petkov06b85342016-10-25 11:55:21 +0200516 if (IS_ENABLED(CONFIG_X86_32))
517 return false;
518
Borislav Petkovfe055892015-10-20 11:54:45 +0200519 native_cpuid(&eax, &ebx, &ecx, &edx);
520
Borislav Petkov99f925c2015-11-23 11:12:21 +0100521 sprintf(name, "intel-ucode/%02x-%02x-%02x",
522 x86_family(eax), x86_model(eax), x86_stepping(eax));
Borislav Petkovfe055892015-10-20 11:54:45 +0200523
524 return get_builtin_firmware(cp, name);
Borislav Petkovfe055892015-10-20 11:54:45 +0200525}
526
Borislav Petkovfe055892015-10-20 11:54:45 +0200527/*
528 * Print ucode update info.
529 */
530static void
531print_ucode_info(struct ucode_cpu_info *uci, unsigned int date)
532{
Borislav Petkovb7f500a2016-02-03 12:33:33 +0100533 pr_info_once("microcode updated early to revision 0x%x, date = %04x-%02x-%02x\n",
534 uci->cpu_sig.rev,
535 date & 0xffff,
536 date >> 24,
537 (date >> 16) & 0xff);
Borislav Petkovfe055892015-10-20 11:54:45 +0200538}
539
540#ifdef CONFIG_X86_32
541
542static int delay_ucode_info;
543static int current_mc_date;
544
545/*
546 * Print early updated ucode info after printk works. This is delayed info dump.
547 */
548void show_ucode_info_early(void)
549{
550 struct ucode_cpu_info uci;
551
552 if (delay_ucode_info) {
553 collect_cpu_info_early(&uci);
554 print_ucode_info(&uci, current_mc_date);
555 delay_ucode_info = 0;
556 }
557}
558
559/*
Borislav Petkov06b85342016-10-25 11:55:21 +0200560 * At this point, we can not call printk() yet. Delay printing microcode info in
Borislav Petkovfe055892015-10-20 11:54:45 +0200561 * show_ucode_info_early() until printk() works.
562 */
563static void print_ucode(struct ucode_cpu_info *uci)
564{
Borislav Petkovde778272016-02-03 12:33:37 +0100565 struct microcode_intel *mc;
Borislav Petkovfe055892015-10-20 11:54:45 +0200566 int *delay_ucode_info_p;
567 int *current_mc_date_p;
568
Borislav Petkovde778272016-02-03 12:33:37 +0100569 mc = uci->mc;
570 if (!mc)
Borislav Petkovfe055892015-10-20 11:54:45 +0200571 return;
572
573 delay_ucode_info_p = (int *)__pa_nodebug(&delay_ucode_info);
574 current_mc_date_p = (int *)__pa_nodebug(&current_mc_date);
575
576 *delay_ucode_info_p = 1;
Borislav Petkovde778272016-02-03 12:33:37 +0100577 *current_mc_date_p = mc->hdr.date;
Borislav Petkovfe055892015-10-20 11:54:45 +0200578}
579#else
580
581/*
582 * Flush global tlb. We only do this in x86_64 where paging has been enabled
583 * already and PGE should be enabled as well.
584 */
585static inline void flush_tlb_early(void)
586{
587 __native_flush_tlb_global_irq_disabled();
588}
589
590static inline void print_ucode(struct ucode_cpu_info *uci)
591{
Borislav Petkovde778272016-02-03 12:33:37 +0100592 struct microcode_intel *mc;
Borislav Petkovfe055892015-10-20 11:54:45 +0200593
Borislav Petkovde778272016-02-03 12:33:37 +0100594 mc = uci->mc;
595 if (!mc)
Borislav Petkovfe055892015-10-20 11:54:45 +0200596 return;
597
Borislav Petkovde778272016-02-03 12:33:37 +0100598 print_ucode_info(uci, mc->hdr.date);
Borislav Petkovfe055892015-10-20 11:54:45 +0200599}
600#endif
601
602static int apply_microcode_early(struct ucode_cpu_info *uci, bool early)
603{
Borislav Petkovde778272016-02-03 12:33:37 +0100604 struct microcode_intel *mc;
Borislav Petkovfe055892015-10-20 11:54:45 +0200605 unsigned int val[2];
606
Borislav Petkovde778272016-02-03 12:33:37 +0100607 mc = uci->mc;
608 if (!mc)
Borislav Petkovfe055892015-10-20 11:54:45 +0200609 return 0;
610
611 /* write microcode via MSR 0x79 */
Borislav Petkovc416e612016-02-03 12:33:40 +0100612 native_wrmsrl(MSR_IA32_UCODE_WRITE, (unsigned long)mc->bits);
613 native_wrmsrl(MSR_IA32_UCODE_REV, 0);
Borislav Petkovfe055892015-10-20 11:54:45 +0200614
615 /* As documented in the SDM: Do a CPUID 1 here */
Andy Lutomirski484d0e52016-12-09 10:24:07 -0800616 cpuid_1();
Borislav Petkovfe055892015-10-20 11:54:45 +0200617
618 /* get the current revision from MSR 0x8B */
619 native_rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
Borislav Petkovde778272016-02-03 12:33:37 +0100620 if (val[1] != mc->hdr.rev)
Borislav Petkovfe055892015-10-20 11:54:45 +0200621 return -1;
622
623#ifdef CONFIG_X86_64
624 /* Flush global tlb. This is precaution. */
625 flush_tlb_early();
626#endif
627 uci->cpu_sig.rev = val[1];
628
629 if (early)
630 print_ucode(uci);
631 else
Borislav Petkovde778272016-02-03 12:33:37 +0100632 print_ucode_info(uci, mc->hdr.date);
Borislav Petkovfe055892015-10-20 11:54:45 +0200633
634 return 0;
635}
636
Borislav Petkovfe055892015-10-20 11:54:45 +0200637int __init save_microcode_in_initrd_intel(void)
638{
Borislav Petkov06b85342016-10-25 11:55:21 +0200639 struct ucode_cpu_info uci;
640 struct cpio_data cp;
Borislav Petkovfe055892015-10-20 11:54:45 +0200641
Borislav Petkov06b85342016-10-25 11:55:21 +0200642 /*
643 * AP loading didn't find any microcode patch, no need to save anything.
644 */
645 if (!intel_ucode_patch || IS_ERR(intel_ucode_patch))
Borislav Petkov6c545642016-06-06 17:10:42 +0200646 return 0;
Borislav Petkovfe055892015-10-20 11:54:45 +0200647
Borislav Petkov06b85342016-10-25 11:55:21 +0200648 if (!load_builtin_intel_microcode(&cp))
649 cp = find_microcode_in_initrd(ucode_path, false);
Borislav Petkov6c545642016-06-06 17:10:42 +0200650
Borislav Petkov06b85342016-10-25 11:55:21 +0200651 if (!(cp.data && cp.size))
652 return 0;
Borislav Petkovfe055892015-10-20 11:54:45 +0200653
654 collect_cpu_info_early(&uci);
655
Borislav Petkov06b85342016-10-25 11:55:21 +0200656 scan_microcode(cp.data, cp.size, &uci, true);
Borislav Petkovfe055892015-10-20 11:54:45 +0200657
Borislav Petkov06b85342016-10-25 11:55:21 +0200658 show_saved_mc();
Borislav Petkovfe055892015-10-20 11:54:45 +0200659
Borislav Petkov06b85342016-10-25 11:55:21 +0200660 return 0;
661}
662
663
664/*
665 * @res_patch, output: a pointer to the patch we found.
666 */
667static struct microcode_intel *__load_ucode_intel(struct ucode_cpu_info *uci)
668{
669 static const char *path;
670 struct cpio_data cp;
671 bool use_pa;
672
673 if (IS_ENABLED(CONFIG_X86_32)) {
674 path = (const char *)__pa_nodebug(ucode_path);
675 use_pa = true;
676 } else {
677 path = ucode_path;
678 use_pa = false;
679 }
680
681 /* try built-in microcode first */
682 if (!load_builtin_intel_microcode(&cp))
683 cp = find_microcode_in_initrd(path, use_pa);
684
685 if (!(cp.data && cp.size))
686 return NULL;
687
688 collect_cpu_info_early(uci);
689
690 return scan_microcode(cp.data, cp.size, uci, false);
Borislav Petkovfe055892015-10-20 11:54:45 +0200691}
692
693void __init load_ucode_intel_bsp(void)
694{
Borislav Petkov06b85342016-10-25 11:55:21 +0200695 struct microcode_intel *patch;
Borislav Petkovbd6fe582016-02-03 12:33:35 +0100696 struct ucode_cpu_info uci;
Borislav Petkovfe055892015-10-20 11:54:45 +0200697
Borislav Petkov06b85342016-10-25 11:55:21 +0200698 patch = __load_ucode_intel(&uci);
699 if (!patch)
Borislav Petkovfe055892015-10-20 11:54:45 +0200700 return;
701
Borislav Petkov06b85342016-10-25 11:55:21 +0200702 uci.mc = patch;
Borislav Petkovfe055892015-10-20 11:54:45 +0200703
704 apply_microcode_early(&uci, true);
705}
706
Borislav Petkov06b85342016-10-25 11:55:21 +0200707void load_ucode_intel_ap(void)
708{
709 struct microcode_intel *patch, **iup;
710 struct ucode_cpu_info uci;
711
712 if (IS_ENABLED(CONFIG_X86_32))
713 iup = (struct microcode_intel **) __pa_nodebug(&intel_ucode_patch);
714 else
715 iup = &intel_ucode_patch;
716
717reget:
718 if (!*iup) {
719 patch = __load_ucode_intel(&uci);
720 if (!patch)
721 return;
722
723 *iup = patch;
724 }
725
726 uci.mc = *iup;
727
728 if (apply_microcode_early(&uci, true)) {
729 /* Mixed-silicon system? Try to refetch the proper patch: */
730 *iup = NULL;
731
732 goto reget;
733 }
734}
735
736static struct microcode_intel *find_patch(struct ucode_cpu_info *uci)
737{
738 struct microcode_header_intel *phdr;
739 struct ucode_patch *iter, *tmp;
740
741 list_for_each_entry_safe(iter, tmp, &microcode_cache, plist) {
742
743 phdr = (struct microcode_header_intel *)iter->data;
744
745 if (phdr->rev <= uci->cpu_sig.rev)
746 continue;
747
748 if (!find_matching_signature(phdr,
749 uci->cpu_sig.sig,
750 uci->cpu_sig.pf))
751 continue;
752
753 return iter->data;
754 }
755 return NULL;
756}
757
Borislav Petkovfe055892015-10-20 11:54:45 +0200758void reload_ucode_intel(void)
759{
Borislav Petkov06b85342016-10-25 11:55:21 +0200760 struct microcode_intel *p;
Borislav Petkovfe055892015-10-20 11:54:45 +0200761 struct ucode_cpu_info uci;
Borislav Petkovfe055892015-10-20 11:54:45 +0200762
763 collect_cpu_info_early(&uci);
764
Borislav Petkov06b85342016-10-25 11:55:21 +0200765 p = find_patch(&uci);
766 if (!p)
Borislav Petkovfe055892015-10-20 11:54:45 +0200767 return;
768
Borislav Petkov06b85342016-10-25 11:55:21 +0200769 uci.mc = p;
770
Borislav Petkovfe055892015-10-20 11:54:45 +0200771 apply_microcode_early(&uci, false);
772}
773
Dmitry Adamushkod45de402008-08-20 00:22:26 +0200774static int collect_cpu_info(int cpu_num, struct cpu_signature *csig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
Andi Kleen354542d2016-06-09 06:41:41 -0700776 static struct cpu_signature prev;
Mike Travis92cb7612007-10-19 20:35:04 +0200777 struct cpuinfo_x86 *c = &cpu_data(cpu_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 unsigned int val[2];
779
Dmitry Adamushkod45de402008-08-20 00:22:26 +0200780 memset(csig, 0, sizeof(*csig));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Dmitry Adamushkod45de402008-08-20 00:22:26 +0200782 csig->sig = cpuid_eax(0x00000001);
Shaohua Li9a3110b2006-09-27 01:50:51 -0700783
784 if ((c->x86_model >= 5) || (c->x86 > 6)) {
785 /* get processor flags from MSR 0x17 */
786 rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]);
Dmitry Adamushkod45de402008-08-20 00:22:26 +0200787 csig->pf = 1 << ((val[1] >> 18) & 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 }
789
Andi Kleen506ed6b2011-10-12 17:46:33 -0700790 csig->rev = c->microcode;
Andi Kleen354542d2016-06-09 06:41:41 -0700791
792 /* No extra locking on prev, races are harmless. */
793 if (csig->sig != prev.sig || csig->pf != prev.pf || csig->rev != prev.rev) {
794 pr_info("sig=0x%x, pf=0x%x, revision=0x%x\n",
795 csig->sig, csig->pf, csig->rev);
796 prev = *csig;
797 }
Dmitry Adamushkod45de402008-08-20 00:22:26 +0200798
799 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800}
801
Henrique de Moraes Holschuh532ed372014-07-24 15:23:21 -0300802static int apply_microcode_intel(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803{
Borislav Petkovde778272016-02-03 12:33:37 +0100804 struct microcode_intel *mc;
Ingo Molnar4bae1962009-03-11 11:19:46 +0100805 struct ucode_cpu_info *uci;
Borislav Petkov26cbaa42016-02-03 12:33:39 +0100806 struct cpuinfo_x86 *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 unsigned int val[2];
Andi Kleen354542d2016-06-09 06:41:41 -0700808 static int prev_rev;
Ingo Molnar4bae1962009-03-11 11:19:46 +0100809
Borislav Petkov58b5f2c2016-02-03 12:33:38 +0100810 /* We should bind the task to the CPU */
Borislav Petkov26cbaa42016-02-03 12:33:39 +0100811 if (WARN_ON(raw_smp_processor_id() != cpu))
Borislav Petkov58b5f2c2016-02-03 12:33:38 +0100812 return -1;
813
Ingo Molnar4bae1962009-03-11 11:19:46 +0100814 uci = ucode_cpu_info + cpu;
Borislav Petkovde778272016-02-03 12:33:37 +0100815 mc = uci->mc;
Borislav Petkov06b85342016-10-25 11:55:21 +0200816 if (!mc) {
817 /* Look for a newer patch in our cache: */
818 mc = find_patch(uci);
819 if (!mc)
820 return 0;
821 }
Fenghua Yu9cd4d782012-12-20 23:44:22 -0800822
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 /* write microcode via MSR 0x79 */
Borislav Petkovc416e612016-02-03 12:33:40 +0100824 wrmsrl(MSR_IA32_UCODE_WRITE, (unsigned long)mc->bits);
825 wrmsrl(MSR_IA32_UCODE_REV, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Andi Kleen506ed6b2011-10-12 17:46:33 -0700827 /* As documented in the SDM: Do a CPUID 1 here */
Andy Lutomirski484d0e52016-12-09 10:24:07 -0800828 cpuid_1();
Zachary Amsden245067d2005-09-03 15:56:37 -0700829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 /* get the current revision from MSR 0x8B */
831 rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
832
Borislav Petkovde778272016-02-03 12:33:37 +0100833 if (val[1] != mc->hdr.rev) {
Joe Perchesf58e1f52009-12-08 22:30:50 -0800834 pr_err("CPU%d update to revision 0x%x failed\n",
Borislav Petkov26cbaa42016-02-03 12:33:39 +0100835 cpu, mc->hdr.rev);
Dmitry Adamushko871b72d2009-05-11 23:48:27 +0200836 return -1;
Shaohua Li9a3110b2006-09-27 01:50:51 -0700837 }
Borislav Petkov26cbaa42016-02-03 12:33:39 +0100838
Andi Kleen354542d2016-06-09 06:41:41 -0700839 if (val[1] != prev_rev) {
840 pr_info("updated to revision 0x%x, date = %04x-%02x-%02x\n",
841 val[1],
842 mc->hdr.date & 0xffff,
843 mc->hdr.date >> 24,
844 (mc->hdr.date >> 16) & 0xff);
845 prev_rev = val[1];
846 }
Ingo Molnar4bae1962009-03-11 11:19:46 +0100847
Borislav Petkov26cbaa42016-02-03 12:33:39 +0100848 c = &cpu_data(cpu);
849
Dmitry Adamushkod45de402008-08-20 00:22:26 +0200850 uci->cpu_sig.rev = val[1];
Andi Kleen506ed6b2011-10-12 17:46:33 -0700851 c->microcode = val[1];
Dmitry Adamushko871b72d2009-05-11 23:48:27 +0200852
853 return 0;
Shaohua Li9a3110b2006-09-27 01:50:51 -0700854}
855
Dmitry Adamushko871b72d2009-05-11 23:48:27 +0200856static enum ucode_state generic_load_microcode(int cpu, void *data, size_t size,
857 int (*get_ucode_data)(void *, const void *, size_t))
Shaohua Li9a3110b2006-09-27 01:50:51 -0700858{
Dmitry Adamushkoa0a29b62008-09-11 23:27:52 +0200859 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
Dimitri Sivanich938179b2010-03-05 11:42:03 -0600860 u8 *ucode_ptr = data, *new_mc = NULL, *mc = NULL;
Dmitry Adamushkoa0a29b62008-09-11 23:27:52 +0200861 int new_rev = uci->cpu_sig.rev;
862 unsigned int leftover = size;
Dimitri Sivanich938179b2010-03-05 11:42:03 -0600863 unsigned int curr_mc_size = 0;
Fenghua Yu9cd4d782012-12-20 23:44:22 -0800864 unsigned int csig, cpf;
Shaohua Li9a3110b2006-09-27 01:50:51 -0700865
Dmitry Adamushkoa0a29b62008-09-11 23:27:52 +0200866 while (leftover) {
867 struct microcode_header_intel mc_header;
868 unsigned int mc_size;
Shaohua Li9a3110b2006-09-27 01:50:51 -0700869
Quentin Casasnovas35a9ff42015-02-03 13:00:24 +0100870 if (leftover < sizeof(mc_header)) {
871 pr_err("error! Truncated header in microcode data file\n");
872 break;
873 }
874
Dmitry Adamushkoa0a29b62008-09-11 23:27:52 +0200875 if (get_ucode_data(&mc_header, ucode_ptr, sizeof(mc_header)))
876 break;
Shaohua Lia30a6a22006-09-27 01:50:52 -0700877
Dmitry Adamushkoa0a29b62008-09-11 23:27:52 +0200878 mc_size = get_totalsize(&mc_header);
879 if (!mc_size || mc_size > leftover) {
Joe Perchesf58e1f52009-12-08 22:30:50 -0800880 pr_err("error! Bad data in microcode data file\n");
Dmitry Adamushkoa0a29b62008-09-11 23:27:52 +0200881 break;
882 }
Shaohua Lia30a6a22006-09-27 01:50:52 -0700883
Dimitri Sivanich938179b2010-03-05 11:42:03 -0600884 /* For performance reasons, reuse mc area when possible */
885 if (!mc || mc_size > curr_mc_size) {
Jesper Juhl5cdd2de2010-12-25 19:57:41 +0100886 vfree(mc);
Dimitri Sivanich938179b2010-03-05 11:42:03 -0600887 mc = vmalloc(mc_size);
888 if (!mc)
889 break;
890 curr_mc_size = mc_size;
891 }
Dmitry Adamushkoa0a29b62008-09-11 23:27:52 +0200892
893 if (get_ucode_data(mc, ucode_ptr, mc_size) ||
Fenghua Yu9cd4d782012-12-20 23:44:22 -0800894 microcode_sanity_check(mc, 1) < 0) {
Dmitry Adamushkoa0a29b62008-09-11 23:27:52 +0200895 break;
896 }
897
Fenghua Yu9cd4d782012-12-20 23:44:22 -0800898 csig = uci->cpu_sig.sig;
899 cpf = uci->cpu_sig.pf;
Borislav Petkov8de3eaf2015-05-17 12:54:58 +0200900 if (has_newer_microcode(mc, csig, cpf, new_rev)) {
Jesper Juhl5cdd2de2010-12-25 19:57:41 +0100901 vfree(new_mc);
Dmitry Adamushkoa0a29b62008-09-11 23:27:52 +0200902 new_rev = mc_header.rev;
903 new_mc = mc;
Dimitri Sivanich938179b2010-03-05 11:42:03 -0600904 mc = NULL; /* trigger new vmalloc */
905 }
Dmitry Adamushkoa0a29b62008-09-11 23:27:52 +0200906
907 ucode_ptr += mc_size;
908 leftover -= mc_size;
Shaohua Lia30a6a22006-09-27 01:50:52 -0700909 }
910
Jesper Juhl5cdd2de2010-12-25 19:57:41 +0100911 vfree(mc);
Dimitri Sivanich938179b2010-03-05 11:42:03 -0600912
Ingo Molnar4bae1962009-03-11 11:19:46 +0100913 if (leftover) {
Jesper Juhl5cdd2de2010-12-25 19:57:41 +0100914 vfree(new_mc);
Borislav Petkovf61337d2016-10-25 11:55:13 +0200915 return UCODE_ERROR;
Dmitry Adamushko871b72d2009-05-11 23:48:27 +0200916 }
917
Borislav Petkovf61337d2016-10-25 11:55:13 +0200918 if (!new_mc)
919 return UCODE_NFOUND;
Dmitry Adamushkoa0a29b62008-09-11 23:27:52 +0200920
Jesper Juhl5cdd2de2010-12-25 19:57:41 +0100921 vfree(uci->mc);
Ingo Molnar4bae1962009-03-11 11:19:46 +0100922 uci->mc = (struct microcode_intel *)new_mc;
923
Fenghua Yu9cd4d782012-12-20 23:44:22 -0800924 /*
925 * If early loading microcode is supported, save this mc into
926 * permanent memory. So it will be loaded early when a CPU is hot added
927 * or resumes.
928 */
Borislav Petkov06b85342016-10-25 11:55:21 +0200929 save_mc_for_early(new_mc, curr_mc_size);
Fenghua Yu9cd4d782012-12-20 23:44:22 -0800930
Joe Perchesf58e1f52009-12-08 22:30:50 -0800931 pr_debug("CPU%d found a matching microcode update with version 0x%x (current=0x%x)\n",
932 cpu, new_rev, uci->cpu_sig.rev);
Borislav Petkovf61337d2016-10-25 11:55:13 +0200933
934 return UCODE_OK;
Shaohua Lia30a6a22006-09-27 01:50:52 -0700935}
936
Dmitry Adamushkoa0a29b62008-09-11 23:27:52 +0200937static int get_ucode_fw(void *to, const void *from, size_t n)
938{
939 memcpy(to, from, n);
940 return 0;
941}
Shaohua Lia30a6a22006-09-27 01:50:52 -0700942
Borislav Petkov48e30682012-07-26 15:51:00 +0200943static enum ucode_state request_microcode_fw(int cpu, struct device *device,
944 bool refresh_fw)
Shaohua Lia30a6a22006-09-27 01:50:52 -0700945{
946 char name[30];
Mike Travis92cb7612007-10-19 20:35:04 +0200947 struct cpuinfo_x86 *c = &cpu_data(cpu);
Shaohua Lia30a6a22006-09-27 01:50:52 -0700948 const struct firmware *firmware;
Dmitry Adamushko871b72d2009-05-11 23:48:27 +0200949 enum ucode_state ret;
Shaohua Lia30a6a22006-09-27 01:50:52 -0700950
Peter Oruba3e135d82008-07-28 18:44:17 +0200951 sprintf(name, "intel-ucode/%02x-%02x-%02x",
Shaohua Lia30a6a22006-09-27 01:50:52 -0700952 c->x86, c->x86_model, c->x86_mask);
Dmitry Adamushko871b72d2009-05-11 23:48:27 +0200953
Takashi Iwai75da02b2013-12-02 15:38:17 +0100954 if (request_firmware_direct(&firmware, name, device)) {
Joe Perchesf58e1f52009-12-08 22:30:50 -0800955 pr_debug("data file %s load failed\n", name);
Dmitry Adamushko871b72d2009-05-11 23:48:27 +0200956 return UCODE_NFOUND;
Shaohua Lia30a6a22006-09-27 01:50:52 -0700957 }
Dmitry Adamushkoa0a29b62008-09-11 23:27:52 +0200958
Jaswinder Singh Rajputdd3feda2009-01-12 14:44:29 +0530959 ret = generic_load_microcode(cpu, (void *)firmware->data,
960 firmware->size, &get_ucode_fw);
Dmitry Adamushkoa0a29b62008-09-11 23:27:52 +0200961
Shaohua Lia30a6a22006-09-27 01:50:52 -0700962 release_firmware(firmware);
963
Dmitry Adamushkoa0a29b62008-09-11 23:27:52 +0200964 return ret;
965}
966
967static int get_ucode_user(void *to, const void *from, size_t n)
968{
969 return copy_from_user(to, from, n);
970}
971
Dmitry Adamushko871b72d2009-05-11 23:48:27 +0200972static enum ucode_state
973request_microcode_user(int cpu, const void __user *buf, size_t size)
Dmitry Adamushkoa0a29b62008-09-11 23:27:52 +0200974{
Jaswinder Singh Rajputdd3feda2009-01-12 14:44:29 +0530975 return generic_load_microcode(cpu, (void *)buf, size, &get_ucode_user);
Shaohua Lia30a6a22006-09-27 01:50:52 -0700976}
977
Hannes Eder4db646b2008-11-23 20:49:52 +0100978static struct microcode_ops microcode_intel_ops = {
Dmitry Adamushkoa0a29b62008-09-11 23:27:52 +0200979 .request_microcode_user = request_microcode_user,
980 .request_microcode_fw = request_microcode_fw,
Peter Oruba8d86f392008-07-28 18:44:21 +0200981 .collect_cpu_info = collect_cpu_info,
Henrique de Moraes Holschuh532ed372014-07-24 15:23:21 -0300982 .apply_microcode = apply_microcode_intel,
Peter Oruba8d86f392008-07-28 18:44:21 +0200983};
984
Dmitry Adamushko18dbc912008-09-23 12:08:44 +0200985struct microcode_ops * __init init_intel_microcode(void)
Peter Oruba8d86f392008-07-28 18:44:21 +0200986{
Borislav Petkov9a2bc332015-10-20 11:54:44 +0200987 struct cpuinfo_x86 *c = &boot_cpu_data;
Srivatsa S. Bhat7164b3f2012-04-16 14:12:00 +0530988
989 if (c->x86_vendor != X86_VENDOR_INTEL || c->x86 < 6 ||
990 cpu_has(c, X86_FEATURE_IA64)) {
991 pr_err("Intel CPU family 0x%x not supported\n", c->x86);
992 return NULL;
993 }
994
Dmitry Adamushko18dbc912008-09-23 12:08:44 +0200995 return &microcode_intel_ops;
Peter Oruba8d86f392008-07-28 18:44:21 +0200996}