aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBorislav Petkov <bp@suse.de>2016-10-25 05:55:21 -0400
committerIngo Molnar <mingo@kernel.org>2016-10-25 06:28:59 -0400
commit06b8534cb72837379b3a4781ab252df3d37c2669 (patch)
tree3a7daa56b48067c21556850750b77ed546621916
parent8027923ab4df9b2bdb9d642b7dceeb135c032f5e (diff)
x86/microcode: Rework microcode loading
Yeah, I know, I know, this is a huuge patch and reviewing it is hard. Sorry but this is the only way I could think of in which I can rewrite the microcode patches loading procedure without breaking (knowingly) the driver. So maybe this patch is easier to review if one looks at the files after the patch has been applied instead at the diff. Because then it becomes pretty obvious: * The BSP-loading path - load_ucode_bsp() is working independently from the AP path now and it doesn't save any pointers or patches anymore - it solely parses the builtin or initrd microcode and applies the patch. That's it. This fixes the CONFIG_RANDOMIZE_MEMORY offset fun more solidly. * The AP-loading path - load_ucode_ap() then goes and scans builtin/initrd *again* for the microcode patches but it caches them this time so that we don't have to do that scan on each AP but only once. This simplifies the code considerably. Then, when we save the microcode from the initrd/builtin, we go and add the relevant patches to our own cache. The AMD side did do that and now the Intel side does it too. So no more pointer copying and blabla, we save the microcode patches ourselves and are independent from initrd/builtin. This whole conversion gives us other benefits like unifying the initrd parsing into a single function: find_microcode_in_initrd() is used by both. The diffstat speaks for itself: 456 insertions(+), 695 deletions(-) Signed-off-by: Borislav Petkov <bp@suse.de> Cc: Andy Lutomirski <luto@kernel.org> Cc: Borislav Petkov <bp@alien8.de> Cc: Brian Gerst <brgerst@gmail.com> Cc: Denys Vlasenko <dvlasenk@redhat.com> Cc: H. Peter Anvin <hpa@zytor.com> Cc: Josh Poimboeuf <jpoimboe@redhat.com> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Thomas Gleixner <tglx@linutronix.de> Link: http://lkml.kernel.org/r/20161025095522.11964-12-bp@alien8.de Signed-off-by: Ingo Molnar <mingo@kernel.org>
-rw-r--r--arch/x86/include/asm/microcode.h1
-rw-r--r--arch/x86/kernel/cpu/microcode/amd.c365
-rw-r--r--arch/x86/kernel/cpu/microcode/core.c63
-rw-r--r--arch/x86/kernel/cpu/microcode/intel.c714
4 files changed, 446 insertions, 697 deletions
diff --git a/arch/x86/include/asm/microcode.h b/arch/x86/include/asm/microcode.h
index 91595ecb3689..38711df3bcb5 100644
--- a/arch/x86/include/asm/microcode.h
+++ b/arch/x86/include/asm/microcode.h
@@ -64,6 +64,7 @@ struct ucode_cpu_info {
64 void *mc; 64 void *mc;
65}; 65};
66extern struct ucode_cpu_info ucode_cpu_info[]; 66extern struct ucode_cpu_info ucode_cpu_info[];
67struct cpio_data find_microcode_in_initrd(const char *path, bool use_pa);
67 68
68#ifdef CONFIG_MICROCODE_INTEL 69#ifdef CONFIG_MICROCODE_INTEL
69extern struct microcode_ops * __init init_intel_microcode(void); 70extern struct microcode_ops * __init init_intel_microcode(void);
diff --git a/arch/x86/kernel/cpu/microcode/amd.c b/arch/x86/kernel/cpu/microcode/amd.c
index 03c19a68d61f..0dd8db1213d3 100644
--- a/arch/x86/kernel/cpu/microcode/amd.c
+++ b/arch/x86/kernel/cpu/microcode/amd.c
@@ -41,53 +41,23 @@ static struct equiv_cpu_entry *equiv_cpu_table;
41 41
42/* 42/*
43 * This points to the current valid container of microcode patches which we will 43 * This points to the current valid container of microcode patches which we will
44 * save from the initrd before jettisoning its contents. 44 * save from the initrd/builtin before jettisoning its contents.
45 */ 45 */
46static u8 *container; 46struct container {
47static size_t container_size; 47 u8 *data;
48static bool ucode_builtin; 48 size_t size;
49} cont;
49 50
50static u32 ucode_new_rev; 51static u32 ucode_new_rev;
51static u8 amd_ucode_patch[PATCH_MAX_SIZE]; 52static u8 amd_ucode_patch[PATCH_MAX_SIZE];
52static u16 this_equiv_id; 53static u16 this_equiv_id;
53 54
54static struct cpio_data ucode_cpio; 55/*
55 56 * Microcode patch container file is prepended to the initrd in cpio
56static struct cpio_data __init find_ucode_in_initrd(void) 57 * format. See Documentation/x86/early-microcode.txt
57{ 58 */
58#ifdef CONFIG_BLK_DEV_INITRD 59static const char
59 char *path; 60ucode_path[] __maybe_unused = "kernel/x86/microcode/AuthenticAMD.bin";
60 void *start;
61 size_t size;
62
63 /*
64 * Microcode patch container file is prepended to the initrd in cpio
65 * format. See Documentation/x86/early-microcode.txt
66 */
67 static __initdata char ucode_path[] = "kernel/x86/microcode/AuthenticAMD.bin";
68
69#ifdef CONFIG_X86_32
70 struct boot_params *p;
71
72 /*
73 * On 32-bit, early load occurs before paging is turned on so we need
74 * to use physical addresses.
75 */
76 p = (struct boot_params *)__pa_nodebug(&boot_params);
77 path = (char *)__pa_nodebug(ucode_path);
78 start = (void *)p->hdr.ramdisk_image;
79 size = p->hdr.ramdisk_size;
80#else
81 path = ucode_path;
82 start = (void *)(boot_params.hdr.ramdisk_image + PAGE_OFFSET);
83 size = boot_params.hdr.ramdisk_size;
84#endif /* !CONFIG_X86_32 */
85
86 return find_cpio_data(path, start, size, NULL);
87#else
88 return (struct cpio_data){ NULL, 0, "" };
89#endif
90}
91 61
92static size_t compute_container_size(u8 *data, u32 total_size) 62static size_t compute_container_size(u8 *data, u32 total_size)
93{ 63{
@@ -143,62 +113,31 @@ static inline u16 find_equiv_id(struct equiv_cpu_entry *equiv_cpu_table,
143 return 0; 113 return 0;
144} 114}
145 115
146static int __apply_microcode_amd(struct microcode_amd *mc_amd)
147{
148 u32 rev, dummy;
149
150 native_wrmsrl(MSR_AMD64_PATCH_LOADER, (u64)(long)&mc_amd->hdr.data_code);
151
152 /* verify patch application was successful */
153 native_rdmsr(MSR_AMD64_PATCH_LEVEL, rev, dummy);
154 if (rev != mc_amd->hdr.patch_id)
155 return -1;
156
157 return 0;
158}
159
160/* 116/*
161 * Early load occurs before we can vmalloc(). So we look for the microcode 117 * This scans the ucode blob for the proper container as we can have multiple
162 * patch container file in initrd, traverse equivalent cpu table, look for a 118 * containers glued together.
163 * matching microcode patch, and update, all in initrd memory in place.
164 * When vmalloc() is available for use later -- on 64-bit during first AP load,
165 * and on 32-bit during save_microcode_in_initrd_amd() -- we can call
166 * load_microcode_amd() to save equivalent cpu table and microcode patches in
167 * kernel heap memory.
168 */ 119 */
169static void apply_ucode_in_initrd(void *ucode, size_t size, bool save_patch) 120static struct container
121find_proper_container(u8 *ucode, size_t size, u16 *ret_id)
170{ 122{
123 struct container ret = { NULL, 0 };
124 u32 eax, ebx, ecx, edx;
171 struct equiv_cpu_entry *eq; 125 struct equiv_cpu_entry *eq;
172 size_t *cont_sz;
173 u32 *header;
174 u8 *data, **cont;
175 u8 (*patch)[PATCH_MAX_SIZE];
176 u16 eq_id = 0;
177 int offset, left; 126 int offset, left;
178 u32 rev, eax, ebx, ecx, edx; 127 u16 eq_id = 0;
179 u32 *new_rev; 128 u32 *header;
180 129 u8 *data;
181#ifdef CONFIG_X86_32
182 new_rev = (u32 *)__pa_nodebug(&ucode_new_rev);
183 cont_sz = (size_t *)__pa_nodebug(&container_size);
184 cont = (u8 **)__pa_nodebug(&container);
185 patch = (u8 (*)[PATCH_MAX_SIZE])__pa_nodebug(&amd_ucode_patch);
186#else
187 new_rev = &ucode_new_rev;
188 cont_sz = &container_size;
189 cont = &container;
190 patch = &amd_ucode_patch;
191#endif
192 130
193 data = ucode; 131 data = ucode;
194 left = size; 132 left = size;
195 header = (u32 *)data; 133 header = (u32 *)data;
196 134
135
197 /* find equiv cpu table */ 136 /* find equiv cpu table */
198 if (header[0] != UCODE_MAGIC || 137 if (header[0] != UCODE_MAGIC ||
199 header[1] != UCODE_EQUIV_CPU_TABLE_TYPE || /* type */ 138 header[1] != UCODE_EQUIV_CPU_TABLE_TYPE || /* type */
200 header[2] == 0) /* size */ 139 header[2] == 0) /* size */
201 return; 140 return ret;
202 141
203 eax = 0x00000001; 142 eax = 0x00000001;
204 ecx = 0; 143 ecx = 0;
@@ -207,7 +146,7 @@ static void apply_ucode_in_initrd(void *ucode, size_t size, bool save_patch)
207 while (left > 0) { 146 while (left > 0) {
208 eq = (struct equiv_cpu_entry *)(data + CONTAINER_HDR_SZ); 147 eq = (struct equiv_cpu_entry *)(data + CONTAINER_HDR_SZ);
209 148
210 *cont = data; 149 ret.data = data;
211 150
212 /* Advance past the container header */ 151 /* Advance past the container header */
213 offset = header[2] + CONTAINER_HDR_SZ; 152 offset = header[2] + CONTAINER_HDR_SZ;
@@ -216,15 +155,15 @@ static void apply_ucode_in_initrd(void *ucode, size_t size, bool save_patch)
216 155
217 eq_id = find_equiv_id(eq, eax); 156 eq_id = find_equiv_id(eq, eax);
218 if (eq_id) { 157 if (eq_id) {
219 this_equiv_id = eq_id; 158 ret.size = compute_container_size(ret.data, left + offset);
220 *cont_sz = compute_container_size(*cont, left + offset);
221 159
222 /* 160 /*
223 * truncate how much we need to iterate over in the 161 * truncate how much we need to iterate over in the
224 * ucode update loop below 162 * ucode update loop below
225 */ 163 */
226 left = *cont_sz - offset; 164 left = ret.size - offset;
227 break; 165 *ret_id = eq_id;
166 return ret;
228 } 167 }
229 168
230 /* 169 /*
@@ -234,6 +173,7 @@ static void apply_ucode_in_initrd(void *ucode, size_t size, bool save_patch)
234 */ 173 */
235 while (left > 0) { 174 while (left > 0) {
236 header = (u32 *)data; 175 header = (u32 *)data;
176
237 if (header[0] == UCODE_MAGIC && 177 if (header[0] == UCODE_MAGIC &&
238 header[1] == UCODE_EQUIV_CPU_TABLE_TYPE) 178 header[1] == UCODE_EQUIV_CPU_TABLE_TYPE)
239 break; 179 break;
@@ -248,14 +188,64 @@ static void apply_ucode_in_initrd(void *ucode, size_t size, bool save_patch)
248 ucode = data; 188 ucode = data;
249 } 189 }
250 190
251 if (!eq_id) { 191 return ret;
252 *cont = NULL; 192}
253 *cont_sz = 0; 193
254 return; 194static int __apply_microcode_amd(struct microcode_amd *mc_amd)
255 } 195{
196 u32 rev, dummy;
197
198 native_wrmsrl(MSR_AMD64_PATCH_LOADER, (u64)(long)&mc_amd->hdr.data_code);
199
200 /* verify patch application was successful */
201 native_rdmsr(MSR_AMD64_PATCH_LEVEL, rev, dummy);
202 if (rev != mc_amd->hdr.patch_id)
203 return -1;
204
205 return 0;
206}
207
208/*
209 * Early load occurs before we can vmalloc(). So we look for the microcode
210 * patch container file in initrd, traverse equivalent cpu table, look for a
211 * matching microcode patch, and update, all in initrd memory in place.
212 * When vmalloc() is available for use later -- on 64-bit during first AP load,
213 * and on 32-bit during save_microcode_in_initrd_amd() -- we can call
214 * load_microcode_amd() to save equivalent cpu table and microcode patches in
215 * kernel heap memory.
216 */
217static struct container
218apply_microcode_early_amd(void *ucode, size_t size, bool save_patch)
219{
220 struct container ret = { NULL, 0 };
221 u8 (*patch)[PATCH_MAX_SIZE];
222 int offset, left;
223 u32 rev, *header;
224 u8 *data;
225 u16 eq_id = 0;
226 u32 *new_rev;
227
228#ifdef CONFIG_X86_32
229 new_rev = (u32 *)__pa_nodebug(&ucode_new_rev);
230 patch = (u8 (*)[PATCH_MAX_SIZE])__pa_nodebug(&amd_ucode_patch);
231#else
232 new_rev = &ucode_new_rev;
233 patch = &amd_ucode_patch;
234#endif
256 235
257 if (check_current_patch_level(&rev, true)) 236 if (check_current_patch_level(&rev, true))
258 return; 237 return (struct container){ NULL, 0 };
238
239 ret = find_proper_container(ucode, size, &eq_id);
240 if (!eq_id)
241 return (struct container){ NULL, 0 };
242
243 this_equiv_id = eq_id;
244 header = (u32 *)ret.data;
245
246 /* We're pointing to an equiv table, skip over it. */
247 data = ret.data + header[2] + CONTAINER_HDR_SZ;
248 left = ret.size - (header[2] + CONTAINER_HDR_SZ);
259 249
260 while (left > 0) { 250 while (left > 0) {
261 struct microcode_amd *mc; 251 struct microcode_amd *mc;
@@ -274,8 +264,7 @@ static void apply_ucode_in_initrd(void *ucode, size_t size, bool save_patch)
274 *new_rev = rev; 264 *new_rev = rev;
275 265
276 if (save_patch) 266 if (save_patch)
277 memcpy(patch, mc, 267 memcpy(patch, mc, min_t(u32, header[1], PATCH_MAX_SIZE));
278 min_t(u32, header[1], PATCH_MAX_SIZE));
279 } 268 }
280 } 269 }
281 270
@@ -283,10 +272,10 @@ static void apply_ucode_in_initrd(void *ucode, size_t size, bool save_patch)
283 data += offset; 272 data += offset;
284 left -= offset; 273 left -= offset;
285 } 274 }
275 return ret;
286} 276}
287 277
288static bool __init load_builtin_amd_microcode(struct cpio_data *cp, 278static bool get_builtin_microcode(struct cpio_data *cp, unsigned int family)
289 unsigned int family)
290{ 279{
291#ifdef CONFIG_X86_64 280#ifdef CONFIG_X86_64
292 char fw_name[36] = "amd-ucode/microcode_amd.bin"; 281 char fw_name[36] = "amd-ucode/microcode_amd.bin";
@@ -303,32 +292,31 @@ static bool __init load_builtin_amd_microcode(struct cpio_data *cp,
303 292
304void __init load_ucode_amd_bsp(unsigned int family) 293void __init load_ucode_amd_bsp(unsigned int family)
305{ 294{
295 struct ucode_cpu_info *uci;
306 struct cpio_data cp; 296 struct cpio_data cp;
307 bool *builtin; 297 const char *path;
308 void **data; 298 bool use_pa;
309 size_t *size;
310 299
311#ifdef CONFIG_X86_32 300 if (IS_ENABLED(CONFIG_X86_32)) {
312 data = (void **)__pa_nodebug(&ucode_cpio.data); 301 uci = (struct ucode_cpu_info *)__pa_nodebug(ucode_cpu_info);
313 size = (size_t *)__pa_nodebug(&ucode_cpio.size); 302 path = (const char *)__pa_nodebug(ucode_path);
314 builtin = (bool *)__pa_nodebug(&ucode_builtin); 303 use_pa = true;
315#else 304 } else {
316 data = &ucode_cpio.data; 305 uci = ucode_cpu_info;
317 size = &ucode_cpio.size; 306 path = ucode_path;
318 builtin = &ucode_builtin; 307 use_pa = false;
319#endif 308 }
320 309
321 *builtin = load_builtin_amd_microcode(&cp, family); 310 if (!get_builtin_microcode(&cp, family))
322 if (!*builtin) 311 cp = find_microcode_in_initrd(path, use_pa);
323 cp = find_ucode_in_initrd();
324 312
325 if (!(cp.data && cp.size)) 313 if (!(cp.data && cp.size))
326 return; 314 return;
327 315
328 *data = cp.data; 316 /* Get BSP's CPUID.EAX(1), needed in load_microcode_amd() */
329 *size = cp.size; 317 uci->cpu_sig.sig = cpuid_eax(1);
330 318
331 apply_ucode_in_initrd(cp.data, cp.size, true); 319 apply_microcode_early_amd(cp.data, cp.size, true);
332} 320}
333 321
334#ifdef CONFIG_X86_32 322#ifdef CONFIG_X86_32
@@ -342,8 +330,7 @@ void __init load_ucode_amd_bsp(unsigned int family)
342void load_ucode_amd_ap(unsigned int family) 330void load_ucode_amd_ap(unsigned int family)
343{ 331{
344 struct microcode_amd *mc; 332 struct microcode_amd *mc;
345 size_t *usize; 333 struct cpio_data cp;
346 void **ucode;
347 334
348 mc = (struct microcode_amd *)__pa_nodebug(amd_ucode_patch); 335 mc = (struct microcode_amd *)__pa_nodebug(amd_ucode_patch);
349 if (mc->hdr.patch_id && mc->hdr.processor_rev_id) { 336 if (mc->hdr.patch_id && mc->hdr.processor_rev_id) {
@@ -351,55 +338,63 @@ void load_ucode_amd_ap(unsigned int family)
351 return; 338 return;
352 } 339 }
353 340
354 ucode = (void *)__pa_nodebug(&container); 341 if (!get_builtin_microcode(&cp, family))
355 usize = (size_t *)__pa_nodebug(&container_size); 342 cp = find_microcode_in_initrd((const char *)__pa_nodebug(ucode_path), true);
356 343
357 if (!*ucode || !*usize) 344 if (!(cp.data && cp.size))
358 return; 345 return;
359 346
360 apply_ucode_in_initrd(*ucode, *usize, false); 347 /*
361} 348 * This would set amd_ucode_patch above so that the following APs can
362 349 * use it directly instead of going down this path again.
363static void __init collect_cpu_sig_on_bsp(void *arg) 350 */
364{ 351 apply_microcode_early_amd(cp.data, cp.size, true);
365 unsigned int cpu = smp_processor_id();
366 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
367
368 uci->cpu_sig.sig = cpuid_eax(0x00000001);
369}
370
371static void __init get_bsp_sig(void)
372{
373 unsigned int bsp = boot_cpu_data.cpu_index;
374 struct ucode_cpu_info *uci = ucode_cpu_info + bsp;
375
376 if (!uci->cpu_sig.sig)
377 smp_call_function_single(bsp, collect_cpu_sig_on_bsp, NULL, 1);
378} 352}
379#else 353#else
380void load_ucode_amd_ap(unsigned int family) 354void load_ucode_amd_ap(unsigned int family)
381{ 355{
382 struct equiv_cpu_entry *eq; 356 struct equiv_cpu_entry *eq;
383 struct microcode_amd *mc; 357 struct microcode_amd *mc;
384 u8 *cont = container;
385 u32 rev, eax; 358 u32 rev, eax;
386 u16 eq_id; 359 u16 eq_id;
387 360
388 if (!container) 361 /* 64-bit runs with paging enabled, thus early==false. */
389 return;
390
391 /*
392 * 64-bit runs with paging enabled, thus early==false.
393 */
394 if (check_current_patch_level(&rev, false)) 362 if (check_current_patch_level(&rev, false))
395 return; 363 return;
396 364
397 /* Add CONFIG_RANDOMIZE_MEMORY offset. */ 365 /* First AP hasn't cached it yet, go through the blob. */
398 if (!ucode_builtin) 366 if (!cont.data) {
399 cont += PAGE_OFFSET - __PAGE_OFFSET_BASE; 367 struct cpio_data cp = { NULL, 0, "" };
368
369 if (cont.size == -1)
370 return;
371
372reget:
373 if (!get_builtin_microcode(&cp, family)) {
374#ifdef CONFIG_BLK_DEV_INITRD
375 cp = find_cpio_data(ucode_path, (void *)initrd_start,
376 initrd_end - initrd_start, NULL);
377#endif
378 if (!(cp.data && cp.size)) {
379 /*
380 * Mark it so that other APs do not scan again
381 * for no real reason and slow down boot
382 * needlessly.
383 */
384 cont.size = -1;
385 return;
386 }
387 }
388
389 cont = apply_microcode_early_amd(cp.data, cp.size, false);
390 if (!(cont.data && cont.size)) {
391 cont.size = -1;
392 return;
393 }
394 }
400 395
401 eax = cpuid_eax(0x00000001); 396 eax = cpuid_eax(0x00000001);
402 eq = (struct equiv_cpu_entry *)(cont + CONTAINER_HDR_SZ); 397 eq = (struct equiv_cpu_entry *)(cont.data + CONTAINER_HDR_SZ);
403 398
404 eq_id = find_equiv_id(eq, eax); 399 eq_id = find_equiv_id(eq, eax);
405 if (!eq_id) 400 if (!eq_id)
@@ -414,64 +409,50 @@ void load_ucode_amd_ap(unsigned int family)
414 } 409 }
415 410
416 } else { 411 } else {
417 if (!ucode_cpio.data)
418 return;
419 412
420 /* 413 /*
421 * AP has a different equivalence ID than BSP, looks like 414 * AP has a different equivalence ID than BSP, looks like
422 * mixed-steppings silicon so go through the ucode blob anew. 415 * mixed-steppings silicon so go through the ucode blob anew.
423 */ 416 */
424 apply_ucode_in_initrd(ucode_cpio.data, ucode_cpio.size, false); 417 goto reget;
425 } 418 }
426} 419}
427#endif 420#endif /* CONFIG_X86_32 */
428 421
429static enum ucode_state 422static enum ucode_state
430load_microcode_amd(int cpu, u8 family, const u8 *data, size_t size); 423load_microcode_amd(int cpu, u8 family, const u8 *data, size_t size);
431 424
432int __init save_microcode_in_initrd_amd(unsigned int family) 425int __init save_microcode_in_initrd_amd(unsigned int fam)
433{ 426{
434 unsigned long cont;
435 int retval = 0;
436 enum ucode_state ret; 427 enum ucode_state ret;
437 u8 *cont_va; 428 int retval = 0;
438 u32 eax; 429 u16 eq_id;
439 430
440 if (!container) 431 if (!cont.data) {
441 return -EINVAL; 432 if (IS_ENABLED(CONFIG_X86_32) && (cont.size != -1)) {
433 struct cpio_data cp = { NULL, 0, "" };
442 434
443#ifdef CONFIG_X86_32 435#ifdef CONFIG_BLK_DEV_INITRD
444 get_bsp_sig(); 436 cp = find_cpio_data(ucode_path, (void *)initrd_start,
445 cont = (unsigned long)container; 437 initrd_end - initrd_start, NULL);
446 cont_va = __va(container);
447#else
448 /*
449 * We need the physical address of the container for both bitness since
450 * boot_params.hdr.ramdisk_image is a physical address.
451 */
452 cont = __pa(container);
453 cont_va = container;
454#endif 438#endif
455 439
456 /* 440 if (!(cp.data && cp.size)) {
457 * Take into account the fact that the ramdisk might get relocated and 441 cont.size = -1;
458 * therefore we need to recompute the container's position in virtual 442 return -EINVAL;
459 * memory space. 443 }
460 */
461 if (relocated_ramdisk)
462 container = (u8 *)(__va(relocated_ramdisk) +
463 (cont - boot_params.hdr.ramdisk_image));
464 else
465 container = cont_va;
466 444
467 /* Add CONFIG_RANDOMIZE_MEMORY offset. */ 445 cont = find_proper_container(cp.data, cp.size, &eq_id);
468 if (!ucode_builtin) 446 if (!eq_id) {
469 container += PAGE_OFFSET - __PAGE_OFFSET_BASE; 447 cont.size = -1;
448 return -EINVAL;
449 }
470 450
471 eax = cpuid_eax(0x00000001); 451 } else
472 eax = ((eax >> 8) & 0xf) + ((eax >> 20) & 0xff); 452 return -EINVAL;
453 }
473 454
474 ret = load_microcode_amd(smp_processor_id(), eax, container, container_size); 455 ret = load_microcode_amd(smp_processor_id(), fam, cont.data, cont.size);
475 if (ret != UCODE_OK) 456 if (ret != UCODE_OK)
476 retval = -EINVAL; 457 retval = -EINVAL;
477 458
@@ -479,8 +460,8 @@ int __init save_microcode_in_initrd_amd(unsigned int family)
479 * This will be freed any msec now, stash patches for the current 460 * This will be freed any msec now, stash patches for the current
480 * family and switch to patch cache for cpu hotplug, etc later. 461 * family and switch to patch cache for cpu hotplug, etc later.
481 */ 462 */
482 container = NULL; 463 cont.data = NULL;
483 container_size = 0; 464 cont.size = 0;
484 465
485 return retval; 466 return retval;
486} 467}
@@ -498,8 +479,10 @@ void reload_ucode_amd(void)
498 return; 479 return;
499 480
500 mc = (struct microcode_amd *)amd_ucode_patch; 481 mc = (struct microcode_amd *)amd_ucode_patch;
482 if (!mc)
483 return;
501 484
502 if (mc && rev < mc->hdr.patch_id) { 485 if (rev < mc->hdr.patch_id) {
503 if (!__apply_microcode_amd(mc)) { 486 if (!__apply_microcode_amd(mc)) {
504 ucode_new_rev = mc->hdr.patch_id; 487 ucode_new_rev = mc->hdr.patch_id;
505 pr_info("reload patch_level=0x%08x\n", ucode_new_rev); 488 pr_info("reload patch_level=0x%08x\n", ucode_new_rev);
diff --git a/arch/x86/kernel/cpu/microcode/core.c b/arch/x86/kernel/cpu/microcode/core.c
index f737039d59b2..dd3d64f07364 100644
--- a/arch/x86/kernel/cpu/microcode/core.c
+++ b/arch/x86/kernel/cpu/microcode/core.c
@@ -39,6 +39,7 @@
39#include <asm/microcode.h> 39#include <asm/microcode.h>
40#include <asm/processor.h> 40#include <asm/processor.h>
41#include <asm/cmdline.h> 41#include <asm/cmdline.h>
42#include <asm/setup.h>
42 43
43#define MICROCODE_VERSION "2.01" 44#define MICROCODE_VERSION "2.01"
44 45
@@ -196,6 +197,58 @@ static int __init save_microcode_in_initrd(void)
196 return -EINVAL; 197 return -EINVAL;
197} 198}
198 199
200struct cpio_data find_microcode_in_initrd(const char *path, bool use_pa)
201{
202#ifdef CONFIG_BLK_DEV_INITRD
203 unsigned long start = 0;
204 size_t size;
205
206#ifdef CONFIG_X86_32
207 struct boot_params *params;
208
209 if (use_pa)
210 params = (struct boot_params *)__pa_nodebug(&boot_params);
211 else
212 params = &boot_params;
213
214 size = params->hdr.ramdisk_size;
215
216 /*
217 * Set start only if we have an initrd image. We cannot use initrd_start
218 * because it is not set that early yet.
219 */
220 if (size)
221 start = params->hdr.ramdisk_image;
222
223# else /* CONFIG_X86_64 */
224 size = (unsigned long)boot_params.ext_ramdisk_size << 32;
225 size |= boot_params.hdr.ramdisk_size;
226
227 if (size) {
228 start = (unsigned long)boot_params.ext_ramdisk_image << 32;
229 start |= boot_params.hdr.ramdisk_image;
230
231 start += PAGE_OFFSET;
232 }
233# endif
234
235 /*
236 * Did we relocate the ramdisk?
237 *
238 * So we possibly relocate the ramdisk *after* applying microcode on the
239 * BSP so we rely on use_pa (use physical addresses) - even if it is not
240 * absolutely correct - to determine whether we've done the ramdisk
241 * relocation already.
242 */
243 if (!use_pa && relocated_ramdisk)
244 start = initrd_start;
245
246 return find_cpio_data(path, (void *)start, size, NULL);
247#else /* !CONFIG_BLK_DEV_INITRD */
248 return (struct cpio_data){ NULL, 0, "" };
249#endif
250}
251
199void reload_early_microcode(void) 252void reload_early_microcode(void)
200{ 253{
201 int vendor, family; 254 int vendor, family;
@@ -455,7 +508,8 @@ static struct attribute_group mc_attr_group = {
455 508
456static void microcode_fini_cpu(int cpu) 509static void microcode_fini_cpu(int cpu)
457{ 510{
458 microcode_ops->microcode_fini_cpu(cpu); 511 if (microcode_ops->microcode_fini_cpu)
512 microcode_ops->microcode_fini_cpu(cpu);
459} 513}
460 514
461static enum ucode_state microcode_resume_cpu(int cpu) 515static enum ucode_state microcode_resume_cpu(int cpu)
@@ -584,12 +638,7 @@ static int mc_cpu_down_prep(unsigned int cpu)
584 /* Suspend is in progress, only remove the interface */ 638 /* Suspend is in progress, only remove the interface */
585 sysfs_remove_group(&dev->kobj, &mc_attr_group); 639 sysfs_remove_group(&dev->kobj, &mc_attr_group);
586 pr_debug("CPU%d removed\n", cpu); 640 pr_debug("CPU%d removed\n", cpu);
587 /* 641
588 * When a CPU goes offline, don't free up or invalidate the copy of
589 * the microcode in kernel memory, so that we can reuse it when the
590 * CPU comes back online without unnecessarily requesting the userspace
591 * for it again.
592 */
593 return 0; 642 return 0;
594} 643}
595 644
diff --git a/arch/x86/kernel/cpu/microcode/intel.c b/arch/x86/kernel/cpu/microcode/intel.c
index 5e631d2cf37f..54d50c3694d8 100644
--- a/arch/x86/kernel/cpu/microcode/intel.c
+++ b/arch/x86/kernel/cpu/microcode/intel.c
@@ -39,27 +39,10 @@
39#include <asm/setup.h> 39#include <asm/setup.h>
40#include <asm/msr.h> 40#include <asm/msr.h>
41 41
42/* 42static const char ucode_path[] = "kernel/x86/microcode/GenuineIntel.bin";
43 * Temporary microcode blobs pointers storage. We note here during early load
44 * the pointers to microcode blobs we've got from whatever storage (detached
45 * initrd, builtin). Later on, we put those into final storage
46 * mc_saved_data.mc_saved.
47 *
48 * Important: those are offsets from the beginning of initrd or absolute
49 * addresses within the kernel image when built-in.
50 */
51static unsigned long mc_tmp_ptrs[MAX_UCODE_COUNT];
52
53static struct mc_saved_data {
54 unsigned int num_saved;
55 struct microcode_intel **mc_saved;
56} mc_saved_data;
57 43
58/* Microcode blobs within the initrd. 0 if builtin. */ 44/* Current microcode patch used in early patching */
59static struct ucode_blobs { 45struct microcode_intel *intel_ucode_patch;
60 unsigned long start;
61 bool valid;
62} blobs;
63 46
64static inline bool cpu_signatures_match(unsigned int s1, unsigned int p1, 47static inline bool cpu_signatures_match(unsigned int s1, unsigned int p1,
65 unsigned int s2, unsigned int p2) 48 unsigned int s2, unsigned int p2)
@@ -116,103 +99,23 @@ static int has_newer_microcode(void *mc, unsigned int csig, int cpf, int new_rev
116 return find_matching_signature(mc, csig, cpf); 99 return find_matching_signature(mc, csig, cpf);
117} 100}
118 101
119/* Go through saved patches and find the one suitable for the current CPU. */
120static enum ucode_state
121find_microcode_patch(struct microcode_intel **saved,
122 unsigned int num_saved, struct ucode_cpu_info *uci)
123{
124 struct microcode_intel *ucode_ptr, *new_mc = NULL;
125 struct microcode_header_intel *mc_hdr;
126 int new_rev, ret, i;
127
128 new_rev = uci->cpu_sig.rev;
129
130 for (i = 0; i < num_saved; i++) {
131 ucode_ptr = saved[i];
132 mc_hdr = (struct microcode_header_intel *)ucode_ptr;
133
134 ret = has_newer_microcode(ucode_ptr,
135 uci->cpu_sig.sig,
136 uci->cpu_sig.pf,
137 new_rev);
138 if (!ret)
139 continue;
140
141 new_rev = mc_hdr->rev;
142 new_mc = ucode_ptr;
143 }
144
145 if (!new_mc)
146 return UCODE_NFOUND;
147
148 uci->mc = (struct microcode_intel *)new_mc;
149 return UCODE_OK;
150}
151
152static inline void
153copy_ptrs(struct microcode_intel **mc_saved, unsigned long *mc_ptrs,
154 unsigned long off, int num_saved)
155{
156 int i;
157
158 for (i = 0; i < num_saved; i++)
159 mc_saved[i] = (struct microcode_intel *)(mc_ptrs[i] + off);
160}
161
162#ifdef CONFIG_X86_32
163static void
164microcode_phys(struct microcode_intel **mc_saved_tmp, struct mc_saved_data *mcs)
165{
166 int i;
167 struct microcode_intel ***mc_saved;
168
169 mc_saved = (struct microcode_intel ***)__pa_nodebug(&mcs->mc_saved);
170
171 for (i = 0; i < mcs->num_saved; i++) {
172 struct microcode_intel *p;
173
174 p = *(struct microcode_intel **)__pa_nodebug(mcs->mc_saved + i);
175 mc_saved_tmp[i] = (struct microcode_intel *)__pa_nodebug(p);
176 }
177}
178#endif
179
180static enum ucode_state
181load_microcode(struct mc_saved_data *mcs, unsigned long *mc_ptrs,
182 unsigned long offset, struct ucode_cpu_info *uci)
183{
184 struct microcode_intel *mc_saved_tmp[MAX_UCODE_COUNT];
185 unsigned int count = mcs->num_saved;
186
187 if (!mcs->mc_saved) {
188 copy_ptrs(mc_saved_tmp, mc_ptrs, offset, count);
189
190 return find_microcode_patch(mc_saved_tmp, count, uci);
191 } else {
192#ifdef CONFIG_X86_32
193 microcode_phys(mc_saved_tmp, mcs);
194 return find_microcode_patch(mc_saved_tmp, count, uci);
195#else
196 return find_microcode_patch(mcs->mc_saved, count, uci);
197#endif
198 }
199}
200
201/* 102/*
202 * Given CPU signature and a microcode patch, this function finds if the 103 * Given CPU signature and a microcode patch, this function finds if the
203 * microcode patch has matching family and model with the CPU. 104 * microcode patch has matching family and model with the CPU.
105 *
106 * %true - if there's a match
107 * %false - otherwise
204 */ 108 */
205static enum ucode_state 109static bool microcode_matches(struct microcode_header_intel *mc_header,
206matching_model_microcode(struct microcode_header_intel *mc_header, 110 unsigned long sig)
207 unsigned long sig)
208{ 111{
209 unsigned int fam, model;
210 unsigned int fam_ucode, model_ucode;
211 struct extended_sigtable *ext_header;
212 unsigned long total_size = get_totalsize(mc_header); 112 unsigned long total_size = get_totalsize(mc_header);
213 unsigned long data_size = get_datasize(mc_header); 113 unsigned long data_size = get_datasize(mc_header);
214 int ext_sigcount, i; 114 struct extended_sigtable *ext_header;
115 unsigned int fam_ucode, model_ucode;
215 struct extended_signature *ext_sig; 116 struct extended_signature *ext_sig;
117 unsigned int fam, model;
118 int ext_sigcount, i;
216 119
217 fam = x86_family(sig); 120 fam = x86_family(sig);
218 model = x86_model(sig); 121 model = x86_model(sig);
@@ -221,11 +124,11 @@ matching_model_microcode(struct microcode_header_intel *mc_header,
221 model_ucode = x86_model(mc_header->sig); 124 model_ucode = x86_model(mc_header->sig);
222 125
223 if (fam == fam_ucode && model == model_ucode) 126 if (fam == fam_ucode && model == model_ucode)
224 return UCODE_OK; 127 return true;
225 128
226 /* Look for ext. headers: */ 129 /* Look for ext. headers: */
227 if (total_size <= data_size + MC_HEADER_SIZE) 130 if (total_size <= data_size + MC_HEADER_SIZE)
228 return UCODE_NFOUND; 131 return false;
229 132
230 ext_header = (void *) mc_header + data_size + MC_HEADER_SIZE; 133 ext_header = (void *) mc_header + data_size + MC_HEADER_SIZE;
231 ext_sig = (void *)ext_header + EXT_HEADER_SIZE; 134 ext_sig = (void *)ext_header + EXT_HEADER_SIZE;
@@ -236,114 +139,69 @@ matching_model_microcode(struct microcode_header_intel *mc_header,
236 model_ucode = x86_model(ext_sig->sig); 139 model_ucode = x86_model(ext_sig->sig);
237 140
238 if (fam == fam_ucode && model == model_ucode) 141 if (fam == fam_ucode && model == model_ucode)
239 return UCODE_OK; 142 return true;
240 143
241 ext_sig++; 144 ext_sig++;
242 } 145 }
243 return UCODE_NFOUND; 146 return false;
244} 147}
245 148
246static int 149static struct ucode_patch *__alloc_microcode_buf(void *data, unsigned int size)
247save_microcode(struct mc_saved_data *mcs,
248 struct microcode_intel **mc_saved_src,
249 unsigned int num_saved)
250{ 150{
251 int i, j; 151 struct ucode_patch *p;
252 struct microcode_intel **saved_ptr;
253 int ret;
254
255 if (!num_saved)
256 return -EINVAL;
257
258 /*
259 * Copy new microcode data.
260 */
261 saved_ptr = kcalloc(num_saved, sizeof(struct microcode_intel *), GFP_KERNEL);
262 if (!saved_ptr)
263 return -ENOMEM;
264
265 for (i = 0; i < num_saved; i++) {
266 struct microcode_header_intel *mc_hdr;
267 struct microcode_intel *mc;
268 unsigned long size;
269
270 if (!mc_saved_src[i]) {
271 ret = -EINVAL;
272 goto err;
273 }
274 152
275 mc = mc_saved_src[i]; 153 p = kzalloc(size, GFP_KERNEL);
276 mc_hdr = &mc->hdr; 154 if (!p)
277 size = get_totalsize(mc_hdr); 155 return ERR_PTR(-ENOMEM);
278 156
279 saved_ptr[i] = kmemdup(mc, size, GFP_KERNEL); 157 p->data = kmemdup(data, size, GFP_KERNEL);
280 if (!saved_ptr[i]) { 158 if (!p->data) {
281 ret = -ENOMEM; 159 kfree(p);
282 goto err; 160 return ERR_PTR(-ENOMEM);
283 }
284 } 161 }
285 162
286 /* 163 return p;
287 * Point to newly saved microcode.
288 */
289 mcs->mc_saved = saved_ptr;
290 mcs->num_saved = num_saved;
291
292 return 0;
293
294err:
295 for (j = 0; j <= i; j++)
296 kfree(saved_ptr[j]);
297 kfree(saved_ptr);
298
299 return ret;
300} 164}
301 165
302/* 166static void save_microcode_patch(void *data, unsigned int size)
303 * A microcode patch in ucode_ptr is saved into mc_saved
304 * - if it has matching signature and newer revision compared to an existing
305 * patch mc_saved.
306 * - or if it is a newly discovered microcode patch.
307 *
308 * The microcode patch should have matching model with CPU.
309 *
310 * Returns: The updated number @num_saved of saved microcode patches.
311 */
312static unsigned int _save_mc(struct microcode_intel **mc_saved,
313 u8 *ucode_ptr, unsigned int num_saved)
314{ 167{
315 struct microcode_header_intel *mc_hdr, *mc_saved_hdr; 168 struct microcode_header_intel *mc_hdr, *mc_saved_hdr;
169 struct ucode_patch *iter, *tmp, *p;
170 bool prev_found = false;
316 unsigned int sig, pf; 171 unsigned int sig, pf;
317 int found = 0, i;
318 172
319 mc_hdr = (struct microcode_header_intel *)ucode_ptr; 173 mc_hdr = (struct microcode_header_intel *)data;
320 174
321 for (i = 0; i < num_saved; i++) { 175 list_for_each_entry_safe(iter, tmp, &microcode_cache, plist) {
322 mc_saved_hdr = (struct microcode_header_intel *)mc_saved[i]; 176 mc_saved_hdr = (struct microcode_header_intel *)iter->data;
323 sig = mc_saved_hdr->sig; 177 sig = mc_saved_hdr->sig;
324 pf = mc_saved_hdr->pf; 178 pf = mc_saved_hdr->pf;
325 179
326 if (!find_matching_signature(ucode_ptr, sig, pf)) 180 if (find_matching_signature(data, sig, pf)) {
327 continue; 181 prev_found = true;
328
329 found = 1;
330 182
331 if (mc_hdr->rev <= mc_saved_hdr->rev) 183 if (mc_hdr->rev <= mc_saved_hdr->rev)
332 continue; 184 continue;
333 185
334 /* 186 p = __alloc_microcode_buf(data, size);
335 * Found an older ucode saved earlier. Replace it with 187 if (IS_ERR(p))
336 * this newer one. 188 pr_err("Error allocating buffer %p\n", data);
337 */ 189 else
338 mc_saved[i] = (struct microcode_intel *)ucode_ptr; 190 list_replace(&iter->plist, &p->plist);
339 break; 191 }
340 } 192 }
341 193
342 /* Newly detected microcode, save it to memory. */ 194 /*
343 if (i >= num_saved && !found) 195 * There weren't any previous patches found in the list cache; save the
344 mc_saved[num_saved++] = (struct microcode_intel *)ucode_ptr; 196 * newly found.
345 197 */
346 return num_saved; 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 }
347} 205}
348 206
349static int microcode_sanity_check(void *mc, int print_err) 207static int microcode_sanity_check(void *mc, int print_err)
@@ -448,78 +306,75 @@ static int microcode_sanity_check(void *mc, int print_err)
448 * Get microcode matching with BSP's model. Only CPUs with the same model as 306 * Get microcode matching with BSP's model. Only CPUs with the same model as
449 * BSP can stay in the platform. 307 * BSP can stay in the platform.
450 */ 308 */
451static enum ucode_state __init 309static struct microcode_intel *
452get_matching_model_microcode(unsigned long start, void *data, size_t size, 310scan_microcode(void *data, size_t size, struct ucode_cpu_info *uci, bool save)
453 struct mc_saved_data *mcs, unsigned long *mc_ptrs,
454 struct ucode_cpu_info *uci)
455{ 311{
456 struct microcode_intel *mc_saved_tmp[MAX_UCODE_COUNT];
457 struct microcode_header_intel *mc_header; 312 struct microcode_header_intel *mc_header;
458 unsigned int num_saved = mcs->num_saved; 313 struct microcode_intel *patch = NULL;
459 enum ucode_state state = UCODE_OK;
460 unsigned int leftover = size;
461 u8 *ucode_ptr = data;
462 unsigned int mc_size; 314 unsigned int mc_size;
463 int i;
464
465 while (leftover && num_saved < ARRAY_SIZE(mc_saved_tmp)) {
466 315
467 if (leftover < sizeof(mc_header)) 316 while (size) {
317 if (size < sizeof(struct microcode_header_intel))
468 break; 318 break;
469 319
470 mc_header = (struct microcode_header_intel *)ucode_ptr; 320 mc_header = (struct microcode_header_intel *)data;
471 321
472 mc_size = get_totalsize(mc_header); 322 mc_size = get_totalsize(mc_header);
473 if (!mc_size || mc_size > leftover || 323 if (!mc_size ||
474 microcode_sanity_check(ucode_ptr, 0) < 0) 324 mc_size > size ||
325 microcode_sanity_check(data, 0) < 0)
475 break; 326 break;
476 327
477 leftover -= mc_size; 328 size -= mc_size;
478 329
479 /* 330 if (!microcode_matches(mc_header, uci->cpu_sig.sig)) {
480 * Since APs with same family and model as the BSP may boot in 331 data += mc_size;
481 * the platform, we need to find and save microcode patches
482 * with the same family and model as the BSP.
483 */
484 if (matching_model_microcode(mc_header, uci->cpu_sig.sig) != UCODE_OK) {
485 ucode_ptr += mc_size;
486 continue; 332 continue;
487 } 333 }
488 334
489 num_saved = _save_mc(mc_saved_tmp, ucode_ptr, num_saved); 335 if (save) {
336 save_microcode_patch(data, mc_size);
337 goto next;
338 }
490 339
491 ucode_ptr += mc_size;
492 }
493 340
494 if (leftover) { 341 if (!patch) {
495 state = UCODE_ERROR; 342 if (!has_newer_microcode(data,
496 return state; 343 uci->cpu_sig.sig,
497 } 344 uci->cpu_sig.pf,
345 uci->cpu_sig.rev))
346 goto next;
498 347
499 if (!num_saved) { 348 } else {
500 state = UCODE_NFOUND; 349 struct microcode_header_intel *phdr = &patch->hdr;
501 return state; 350
502 } 351 if (!has_newer_microcode(data,
352 phdr->sig,
353 phdr->pf,
354 phdr->rev))
355 goto next;
356 }
503 357
504 for (i = 0; i < num_saved; i++) 358 /* We have a newer patch, save it. */
505 mc_ptrs[i] = (unsigned long)mc_saved_tmp[i] - start; 359 patch = data;
506 360
507 mcs->num_saved = num_saved; 361next:
362 data += mc_size;
363 }
508 364
509 return state; 365 if (size)
366 return NULL;
367
368 return patch;
510} 369}
511 370
512static int collect_cpu_info_early(struct ucode_cpu_info *uci) 371static int collect_cpu_info_early(struct ucode_cpu_info *uci)
513{ 372{
514 unsigned int val[2]; 373 unsigned int val[2];
515 unsigned int family, model; 374 unsigned int family, model;
516 struct cpu_signature csig; 375 struct cpu_signature csig = { 0 };
517 unsigned int eax, ebx, ecx, edx; 376 unsigned int eax, ebx, ecx, edx;
518 377
519 csig.sig = 0;
520 csig.pf = 0;
521 csig.rev = 0;
522
523 memset(uci, 0, sizeof(*uci)); 378 memset(uci, 0, sizeof(*uci));
524 379
525 eax = 0x00000001; 380 eax = 0x00000001;
@@ -527,8 +382,8 @@ static int collect_cpu_info_early(struct ucode_cpu_info *uci)
527 native_cpuid(&eax, &ebx, &ecx, &edx); 382 native_cpuid(&eax, &ebx, &ecx, &edx);
528 csig.sig = eax; 383 csig.sig = eax;
529 384
530 family = x86_family(csig.sig); 385 family = x86_family(eax);
531 model = x86_model(csig.sig); 386 model = x86_model(eax);
532 387
533 if ((model >= 5) || (family > 6)) { 388 if ((model >= 5) || (family > 6)) {
534 /* get processor flags from MSR 0x17 */ 389 /* get processor flags from MSR 0x17 */
@@ -554,40 +409,41 @@ static int collect_cpu_info_early(struct ucode_cpu_info *uci)
554static void show_saved_mc(void) 409static void show_saved_mc(void)
555{ 410{
556#ifdef DEBUG 411#ifdef DEBUG
557 int i, j; 412 int i = 0, j;
558 unsigned int sig, pf, rev, total_size, data_size, date; 413 unsigned int sig, pf, rev, total_size, data_size, date;
559 struct ucode_cpu_info uci; 414 struct ucode_cpu_info uci;
415 struct ucode_patch *p;
560 416
561 if (!mc_saved_data.num_saved) { 417 if (list_empty(&microcode_cache)) {
562 pr_debug("no microcode data saved.\n"); 418 pr_debug("no microcode data saved.\n");
563 return; 419 return;
564 } 420 }
565 pr_debug("Total microcode saved: %d\n", mc_saved_data.num_saved);
566 421
567 collect_cpu_info_early(&uci); 422 collect_cpu_info_early(&uci);
568 423
569 sig = uci.cpu_sig.sig; 424 sig = uci.cpu_sig.sig;
570 pf = uci.cpu_sig.pf; 425 pf = uci.cpu_sig.pf;
571 rev = uci.cpu_sig.rev; 426 rev = uci.cpu_sig.rev;
572 pr_debug("CPU: sig=0x%x, pf=0x%x, rev=0x%x\n", sig, pf, rev); 427 pr_debug("CPU: sig=0x%x, pf=0x%x, rev=0x%x\n", sig, pf, rev);
573 428
574 for (i = 0; i < mc_saved_data.num_saved; i++) { 429 list_for_each_entry(p, &microcode_cache, plist) {
575 struct microcode_header_intel *mc_saved_header; 430 struct microcode_header_intel *mc_saved_header;
576 struct extended_sigtable *ext_header; 431 struct extended_sigtable *ext_header;
577 int ext_sigcount;
578 struct extended_signature *ext_sig; 432 struct extended_signature *ext_sig;
433 int ext_sigcount;
434
435 mc_saved_header = (struct microcode_header_intel *)p->data;
436
437 sig = mc_saved_header->sig;
438 pf = mc_saved_header->pf;
439 rev = mc_saved_header->rev;
440 date = mc_saved_header->date;
579 441
580 mc_saved_header = (struct microcode_header_intel *) 442 total_size = get_totalsize(mc_saved_header);
581 mc_saved_data.mc_saved[i]; 443 data_size = get_datasize(mc_saved_header);
582 sig = mc_saved_header->sig;
583 pf = mc_saved_header->pf;
584 rev = mc_saved_header->rev;
585 total_size = get_totalsize(mc_saved_header);
586 data_size = get_datasize(mc_saved_header);
587 date = mc_saved_header->date;
588 444
589 pr_debug("mc_saved[%d]: sig=0x%x, pf=0x%x, rev=0x%x, total size=0x%x, date = %04x-%02x-%02x\n", 445 pr_debug("mc_saved[%d]: sig=0x%x, pf=0x%x, rev=0x%x, total size=0x%x, date = %04x-%02x-%02x\n",
590 i, sig, pf, rev, total_size, 446 i++, sig, pf, rev, total_size,
591 date & 0xffff, 447 date & 0xffff,
592 date >> 24, 448 date >> 24,
593 (date >> 16) & 0xff); 449 (date >> 16) & 0xff);
@@ -596,7 +452,7 @@ static void show_saved_mc(void)
596 if (total_size <= data_size + MC_HEADER_SIZE) 452 if (total_size <= data_size + MC_HEADER_SIZE)
597 continue; 453 continue;
598 454
599 ext_header = (void *) mc_saved_header + data_size + MC_HEADER_SIZE; 455 ext_header = (void *)mc_saved_header + data_size + MC_HEADER_SIZE;
600 ext_sigcount = ext_header->count; 456 ext_sigcount = ext_header->count;
601 ext_sig = (void *)ext_header + EXT_HEADER_SIZE; 457 ext_sig = (void *)ext_header + EXT_HEADER_SIZE;
602 458
@@ -609,85 +465,43 @@ static void show_saved_mc(void)
609 465
610 ext_sig++; 466 ext_sig++;
611 } 467 }
612
613 } 468 }
614#endif 469#endif
615} 470}
616 471
617/* 472/*
618 * Save this mc into mc_saved_data. So it will be loaded early when a CPU is 473 * Save this microcode patch. It will be loaded early when a CPU is
619 * hot added or resumes. 474 * hot-added or resumes.
620 *
621 * Please make sure this mc should be a valid microcode patch before calling
622 * this function.
623 */ 475 */
624static void save_mc_for_early(u8 *mc) 476static void save_mc_for_early(u8 *mc, unsigned int size)
625{ 477{
626#ifdef CONFIG_HOTPLUG_CPU 478#ifdef CONFIG_HOTPLUG_CPU
627 /* Synchronization during CPU hotplug. */ 479 /* Synchronization during CPU hotplug. */
628 static DEFINE_MUTEX(x86_cpu_microcode_mutex); 480 static DEFINE_MUTEX(x86_cpu_microcode_mutex);
629 481
630 struct microcode_intel *mc_saved_tmp[MAX_UCODE_COUNT];
631 unsigned int mc_saved_count_init;
632 unsigned int num_saved;
633 struct microcode_intel **mc_saved;
634 int ret, i;
635
636 mutex_lock(&x86_cpu_microcode_mutex); 482 mutex_lock(&x86_cpu_microcode_mutex);
637 483
638 mc_saved_count_init = mc_saved_data.num_saved; 484 save_microcode_patch(mc, size);
639 num_saved = mc_saved_data.num_saved;
640 mc_saved = mc_saved_data.mc_saved;
641
642 if (mc_saved && num_saved)
643 memcpy(mc_saved_tmp, mc_saved,
644 num_saved * sizeof(struct microcode_intel *));
645 /*
646 * Save the microcode patch mc in mc_save_tmp structure if it's a newer
647 * version.
648 */
649 num_saved = _save_mc(mc_saved_tmp, mc, num_saved);
650
651 /*
652 * Save the mc_save_tmp in global mc_saved_data.
653 */
654 ret = save_microcode(&mc_saved_data, mc_saved_tmp, num_saved);
655 if (ret) {
656 pr_err("Cannot save microcode patch.\n");
657 goto out;
658 }
659
660 show_saved_mc(); 485 show_saved_mc();
661 486
662 /*
663 * Free old saved microcode data.
664 */
665 if (mc_saved) {
666 for (i = 0; i < mc_saved_count_init; i++)
667 kfree(mc_saved[i]);
668 kfree(mc_saved);
669 }
670
671out:
672 mutex_unlock(&x86_cpu_microcode_mutex); 487 mutex_unlock(&x86_cpu_microcode_mutex);
673#endif 488#endif
674} 489}
675 490
676static bool __init load_builtin_intel_microcode(struct cpio_data *cp) 491static bool load_builtin_intel_microcode(struct cpio_data *cp)
677{ 492{
678#ifdef CONFIG_X86_64 493 unsigned int eax = 1, ebx, ecx = 0, edx;
679 unsigned int eax = 0x00000001, ebx, ecx = 0, edx;
680 char name[30]; 494 char name[30];
681 495
496 if (IS_ENABLED(CONFIG_X86_32))
497 return false;
498
682 native_cpuid(&eax, &ebx, &ecx, &edx); 499 native_cpuid(&eax, &ebx, &ecx, &edx);
683 500
684 sprintf(name, "intel-ucode/%02x-%02x-%02x", 501 sprintf(name, "intel-ucode/%02x-%02x-%02x",
685 x86_family(eax), x86_model(eax), x86_stepping(eax)); 502 x86_family(eax), x86_model(eax), x86_stepping(eax));
686 503
687 return get_builtin_firmware(cp, name); 504 return get_builtin_firmware(cp, name);
688#else
689 return false;
690#endif
691} 505}
692 506
693/* 507/*
@@ -723,8 +537,7 @@ void show_ucode_info_early(void)
723} 537}
724 538
725/* 539/*
726 * At this point, we can not call printk() yet. Keep microcode patch number in 540 * At this point, we can not call printk() yet. Delay printing microcode info in
727 * mc_saved_data.mc_saved and delay printing microcode info in
728 * show_ucode_info_early() until printk() works. 541 * show_ucode_info_early() until printk() works.
729 */ 542 */
730static void print_ucode(struct ucode_cpu_info *uci) 543static void print_ucode(struct ucode_cpu_info *uci)
@@ -801,206 +614,140 @@ static int apply_microcode_early(struct ucode_cpu_info *uci, bool early)
801 return 0; 614 return 0;
802} 615}
803 616
804/*
805 * This function converts microcode patch offsets previously stored in
806 * mc_tmp_ptrs to pointers and stores the pointers in mc_saved_data.
807 */
808int __init save_microcode_in_initrd_intel(void) 617int __init save_microcode_in_initrd_intel(void)
809{ 618{
810 struct microcode_intel *mc_saved[MAX_UCODE_COUNT]; 619 struct ucode_cpu_info uci;
811 unsigned int count = mc_saved_data.num_saved; 620 struct cpio_data cp;
812 unsigned long offset = 0;
813 int ret;
814
815 if (!count)
816 return 0;
817 621
818 /* 622 /*
819 * We have found a valid initrd but it might've been relocated in the 623 * AP loading didn't find any microcode patch, no need to save anything.
820 * meantime so get its updated address.
821 */ 624 */
822 if (IS_ENABLED(CONFIG_BLK_DEV_INITRD) && blobs.valid) 625 if (!intel_ucode_patch || IS_ERR(intel_ucode_patch))
823 offset = initrd_start; 626 return 0;
824
825 copy_ptrs(mc_saved, mc_tmp_ptrs, offset, count);
826 627
827 ret = save_microcode(&mc_saved_data, mc_saved, count); 628 if (!load_builtin_intel_microcode(&cp))
828 if (ret) 629 cp = find_microcode_in_initrd(ucode_path, false);
829 pr_err("Cannot save microcode patches from initrd.\n");
830 else
831 show_saved_mc();
832 630
833 return ret; 631 if (!(cp.data && cp.size))
834} 632 return 0;
835 633
836static __init enum ucode_state 634 collect_cpu_info_early(&uci);
837__scan_microcode_initrd(struct cpio_data *cd, struct ucode_blobs *blbp)
838{
839#ifdef CONFIG_BLK_DEV_INITRD
840 static __initdata char ucode_name[] = "kernel/x86/microcode/GenuineIntel.bin";
841 char *p = IS_ENABLED(CONFIG_X86_32) ? (char *)__pa_nodebug(ucode_name)
842 : ucode_name;
843# ifdef CONFIG_X86_32
844 unsigned long start = 0, size;
845 struct boot_params *params;
846 635
847 params = (struct boot_params *)__pa_nodebug(&boot_params); 636 scan_microcode(cp.data, cp.size, &uci, true);
848 size = params->hdr.ramdisk_size;
849 637
850 /* 638 show_saved_mc();
851 * Set start only if we have an initrd image. We cannot use initrd_start
852 * because it is not set that early yet.
853 */
854 start = (size ? params->hdr.ramdisk_image : 0);
855 639
856# else /* CONFIG_X86_64 */ 640 return 0;
857 unsigned long start = 0, size; 641}
858 642
859 size = (u64)boot_params.ext_ramdisk_size << 32;
860 size |= boot_params.hdr.ramdisk_size;
861 643
862 if (size) { 644/*
863 start = (u64)boot_params.ext_ramdisk_image << 32; 645 * @res_patch, output: a pointer to the patch we found.
864 start |= boot_params.hdr.ramdisk_image; 646 */
647static struct microcode_intel *__load_ucode_intel(struct ucode_cpu_info *uci)
648{
649 static const char *path;
650 struct cpio_data cp;
651 bool use_pa;
865 652
866 start += PAGE_OFFSET; 653 if (IS_ENABLED(CONFIG_X86_32)) {
654 path = (const char *)__pa_nodebug(ucode_path);
655 use_pa = true;
656 } else {
657 path = ucode_path;
658 use_pa = false;
867 } 659 }
868# endif
869 660
870 *cd = find_cpio_data(p, (void *)start, size, NULL); 661 /* try built-in microcode first */
871 if (cd->data) { 662 if (!load_builtin_intel_microcode(&cp))
872 blbp->start = start; 663 cp = find_microcode_in_initrd(path, use_pa);
873 blbp->valid = true;
874
875 return UCODE_OK;
876 } else
877#endif /* CONFIG_BLK_DEV_INITRD */
878 return UCODE_ERROR;
879}
880 664
881static __init enum ucode_state 665 if (!(cp.data && cp.size))
882scan_microcode(struct mc_saved_data *mcs, unsigned long *mc_ptrs, 666 return NULL;
883 struct ucode_cpu_info *uci, struct ucode_blobs *blbp)
884{
885 struct cpio_data cd = { NULL, 0, "" };
886 enum ucode_state ret;
887 667
888 /* try built-in microcode first */ 668 collect_cpu_info_early(uci);
889 if (load_builtin_intel_microcode(&cd))
890 /*
891 * Invalidate blobs as we might've gotten an initrd too,
892 * supplied by the boot loader, by mistake or simply forgotten
893 * there. That's fine, we ignore it since we've found builtin
894 * microcode already.
895 */
896 blbp->valid = false;
897 else {
898 ret = __scan_microcode_initrd(&cd, blbp);
899 if (ret != UCODE_OK)
900 return ret;
901 }
902 669
903 return get_matching_model_microcode(blbp->start, cd.data, cd.size, 670 return scan_microcode(cp.data, cp.size, uci, false);
904 mcs, mc_ptrs, uci);
905} 671}
906 672
907static void __init 673void __init load_ucode_intel_bsp(void)
908_load_ucode_intel_bsp(struct mc_saved_data *mcs, unsigned long *mc_ptrs,
909 struct ucode_blobs *blbp)
910{ 674{
675 struct microcode_intel *patch;
911 struct ucode_cpu_info uci; 676 struct ucode_cpu_info uci;
912 enum ucode_state ret;
913 677
914 collect_cpu_info_early(&uci); 678 patch = __load_ucode_intel(&uci);
915 679 if (!patch)
916 ret = scan_microcode(mcs, mc_ptrs, &uci, blbp);
917 if (ret != UCODE_OK)
918 return; 680 return;
919 681
920 ret = load_microcode(mcs, mc_ptrs, blbp->start, &uci); 682 uci.mc = patch;
921 if (ret != UCODE_OK)
922 return;
923 683
924 apply_microcode_early(&uci, true); 684 apply_microcode_early(&uci, true);
925} 685}
926 686
927void __init load_ucode_intel_bsp(void) 687void load_ucode_intel_ap(void)
928{ 688{
929 struct ucode_blobs *blobs_p; 689 struct microcode_intel *patch, **iup;
930 struct mc_saved_data *mcs; 690 struct ucode_cpu_info uci;
931 unsigned long *ptrs;
932 691
933#ifdef CONFIG_X86_32 692 if (IS_ENABLED(CONFIG_X86_32))
934 mcs = (struct mc_saved_data *)__pa_nodebug(&mc_saved_data); 693 iup = (struct microcode_intel **) __pa_nodebug(&intel_ucode_patch);
935 ptrs = (unsigned long *)__pa_nodebug(&mc_tmp_ptrs); 694 else
936 blobs_p = (struct ucode_blobs *)__pa_nodebug(&blobs); 695 iup = &intel_ucode_patch;
937#else 696
938 mcs = &mc_saved_data; 697reget:
939 ptrs = mc_tmp_ptrs; 698 if (!*iup) {
940 blobs_p = &blobs; 699 patch = __load_ucode_intel(&uci);
941#endif 700 if (!patch)
701 return;
942 702
943 _load_ucode_intel_bsp(mcs, ptrs, blobs_p); 703 *iup = patch;
704 }
705
706 uci.mc = *iup;
707
708 if (apply_microcode_early(&uci, true)) {
709 /* Mixed-silicon system? Try to refetch the proper patch: */
710 *iup = NULL;
711
712 goto reget;
713 }
944} 714}
945 715
946void load_ucode_intel_ap(void) 716static struct microcode_intel *find_patch(struct ucode_cpu_info *uci)
947{ 717{
948 struct ucode_blobs *blobs_p; 718 struct microcode_header_intel *phdr;
949 unsigned long *ptrs, start = 0; 719 struct ucode_patch *iter, *tmp;
950 struct mc_saved_data *mcs;
951 struct ucode_cpu_info uci;
952 enum ucode_state ret;
953 720
954#ifdef CONFIG_X86_32 721 list_for_each_entry_safe(iter, tmp, &microcode_cache, plist) {
955 mcs = (struct mc_saved_data *)__pa_nodebug(&mc_saved_data);
956 ptrs = (unsigned long *)__pa_nodebug(mc_tmp_ptrs);
957 blobs_p = (struct ucode_blobs *)__pa_nodebug(&blobs);
958#else
959 mcs = &mc_saved_data;
960 ptrs = mc_tmp_ptrs;
961 blobs_p = &blobs;
962#endif
963
964 /*
965 * If there is no valid ucode previously saved in memory, no need to
966 * update ucode on this AP.
967 */
968 if (!mcs->num_saved)
969 return;
970 722
971 if (blobs_p->valid) { 723 phdr = (struct microcode_header_intel *)iter->data;
972 start = blobs_p->start;
973 724
974 /* 725 if (phdr->rev <= uci->cpu_sig.rev)
975 * Pay attention to CONFIG_RANDOMIZE_MEMORY=y as it shuffles 726 continue;
976 * physmem mapping too and there we have the initrd.
977 */
978 start += PAGE_OFFSET - __PAGE_OFFSET_BASE;
979 }
980 727
981 collect_cpu_info_early(&uci); 728 if (!find_matching_signature(phdr,
982 ret = load_microcode(mcs, ptrs, start, &uci); 729 uci->cpu_sig.sig,
983 if (ret != UCODE_OK) 730 uci->cpu_sig.pf))
984 return; 731 continue;
985 732
986 apply_microcode_early(&uci, true); 733 return iter->data;
734 }
735 return NULL;
987} 736}
988 737
989void reload_ucode_intel(void) 738void reload_ucode_intel(void)
990{ 739{
740 struct microcode_intel *p;
991 struct ucode_cpu_info uci; 741 struct ucode_cpu_info uci;
992 enum ucode_state ret;
993
994 if (!mc_saved_data.num_saved)
995 return;
996 742
997 collect_cpu_info_early(&uci); 743 collect_cpu_info_early(&uci);
998 744
999 ret = find_microcode_patch(mc_saved_data.mc_saved, 745 p = find_patch(&uci);
1000 mc_saved_data.num_saved, &uci); 746 if (!p)
1001 if (ret != UCODE_OK)
1002 return; 747 return;
1003 748
749 uci.mc = p;
750
1004 apply_microcode_early(&uci, false); 751 apply_microcode_early(&uci, false);
1005} 752}
1006 753
@@ -1032,24 +779,6 @@ static int collect_cpu_info(int cpu_num, struct cpu_signature *csig)
1032 return 0; 779 return 0;
1033} 780}
1034 781
1035/*
1036 * return 0 - no update found
1037 * return 1 - found update
1038 */
1039static int get_matching_mc(struct microcode_intel *mc, int cpu)
1040{
1041 struct cpu_signature cpu_sig;
1042 unsigned int csig, cpf, crev;
1043
1044 collect_cpu_info(cpu, &cpu_sig);
1045
1046 csig = cpu_sig.sig;
1047 cpf = cpu_sig.pf;
1048 crev = cpu_sig.rev;
1049
1050 return has_newer_microcode(mc, csig, cpf, crev);
1051}
1052
1053static int apply_microcode_intel(int cpu) 782static int apply_microcode_intel(int cpu)
1054{ 783{
1055 struct microcode_intel *mc; 784 struct microcode_intel *mc;
@@ -1064,16 +793,12 @@ static int apply_microcode_intel(int cpu)
1064 793
1065 uci = ucode_cpu_info + cpu; 794 uci = ucode_cpu_info + cpu;
1066 mc = uci->mc; 795 mc = uci->mc;
1067 if (!mc) 796 if (!mc) {
1068 return 0; 797 /* Look for a newer patch in our cache: */
1069 798 mc = find_patch(uci);
1070 /* 799 if (!mc)
1071 * Microcode on this CPU could be updated earlier. Only apply the 800 return 0;
1072 * microcode patch in mc when it is newer than the one on this 801 }
1073 * CPU.
1074 */
1075 if (!get_matching_mc(mc, cpu))
1076 return 0;
1077 802
1078 /* write microcode via MSR 0x79 */ 803 /* write microcode via MSR 0x79 */
1079 wrmsrl(MSR_IA32_UCODE_WRITE, (unsigned long)mc->bits); 804 wrmsrl(MSR_IA32_UCODE_WRITE, (unsigned long)mc->bits);
@@ -1181,7 +906,7 @@ static enum ucode_state generic_load_microcode(int cpu, void *data, size_t size,
1181 * permanent memory. So it will be loaded early when a CPU is hot added 906 * permanent memory. So it will be loaded early when a CPU is hot added
1182 * or resumes. 907 * or resumes.
1183 */ 908 */
1184 save_mc_for_early(new_mc); 909 save_mc_for_early(new_mc, curr_mc_size);
1185 910
1186 pr_debug("CPU%d found a matching microcode update with version 0x%x (current=0x%x)\n", 911 pr_debug("CPU%d found a matching microcode update with version 0x%x (current=0x%x)\n",
1187 cpu, new_rev, uci->cpu_sig.rev); 912 cpu, new_rev, uci->cpu_sig.rev);
@@ -1230,20 +955,11 @@ request_microcode_user(int cpu, const void __user *buf, size_t size)
1230 return generic_load_microcode(cpu, (void *)buf, size, &get_ucode_user); 955 return generic_load_microcode(cpu, (void *)buf, size, &get_ucode_user);
1231} 956}
1232 957
1233static void microcode_fini_cpu(int cpu)
1234{
1235 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
1236
1237 vfree(uci->mc);
1238 uci->mc = NULL;
1239}
1240
1241static struct microcode_ops microcode_intel_ops = { 958static struct microcode_ops microcode_intel_ops = {
1242 .request_microcode_user = request_microcode_user, 959 .request_microcode_user = request_microcode_user,
1243 .request_microcode_fw = request_microcode_fw, 960 .request_microcode_fw = request_microcode_fw,
1244 .collect_cpu_info = collect_cpu_info, 961 .collect_cpu_info = collect_cpu_info,
1245 .apply_microcode = apply_microcode_intel, 962 .apply_microcode = apply_microcode_intel,
1246 .microcode_fini_cpu = microcode_fini_cpu,
1247}; 963};
1248 964
1249struct microcode_ops * __init init_intel_microcode(void) 965struct microcode_ops * __init init_intel_microcode(void)