diff options
| author | Fenghua Yu <fenghua.yu@intel.com> | 2012-12-21 02:44:28 -0500 |
|---|---|---|
| committer | H. Peter Anvin <hpa@linux.intel.com> | 2013-01-31 16:19:18 -0500 |
| commit | ec400ddeff200b068ddc6c70f7321f49ecf32ed5 (patch) | |
| tree | ccd0456a90f4dcc5b0aad47233d5d33836008601 | |
| parent | 086fc8f8037bf16f55f82c66b26a8b834f7349ec (diff) | |
x86/microcode_intel_early.c: Early update ucode on Intel's CPU
Implementation of early update ucode on Intel's CPU.
load_ucode_intel_bsp() scans ucode in initrd image file which is a cpio format
ucode followed by ordinary initrd image file. The binary ucode file is stored
in kernel/x86/microcode/GenuineIntel.bin in the cpio data. All ucode
patches with the same model as BSP are saved in memory. A matching ucode patch
is updated on BSP.
load_ucode_intel_ap() reads saved ucoded patches and updates ucode on AP.
Signed-off-by: Fenghua Yu <fenghua.yu@intel.com>
Link: http://lkml.kernel.org/r/1356075872-3054-9-git-send-email-fenghua.yu@intel.com
Signed-off-by: H. Peter Anvin <hpa@linux.intel.com>
| -rw-r--r-- | arch/x86/kernel/microcode_intel_early.c | 796 |
1 files changed, 796 insertions, 0 deletions
diff --git a/arch/x86/kernel/microcode_intel_early.c b/arch/x86/kernel/microcode_intel_early.c new file mode 100644 index 000000000000..7890bc838952 --- /dev/null +++ b/arch/x86/kernel/microcode_intel_early.c | |||
| @@ -0,0 +1,796 @@ | |||
| 1 | /* | ||
| 2 | * Intel CPU microcode early update for Linux | ||
| 3 | * | ||
| 4 | * Copyright (C) 2012 Fenghua Yu <fenghua.yu@intel.com> | ||
| 5 | * H Peter Anvin" <hpa@zytor.com> | ||
| 6 | * | ||
| 7 | * This allows to early upgrade microcode on Intel processors | ||
| 8 | * belonging to IA-32 family - PentiumPro, Pentium II, | ||
| 9 | * Pentium III, Xeon, Pentium 4, etc. | ||
| 10 | * | ||
| 11 | * Reference: Section 9.11 of Volume 3, IA-32 Intel Architecture | ||
| 12 | * Software Developer's Manual. | ||
| 13 | * | ||
| 14 | * This program is free software; you can redistribute it and/or | ||
| 15 | * modify it under the terms of the GNU General Public License | ||
| 16 | * as published by the Free Software Foundation; either version | ||
| 17 | * 2 of the License, or (at your option) any later version. | ||
| 18 | */ | ||
| 19 | #include <linux/module.h> | ||
| 20 | #include <linux/mm.h> | ||
| 21 | #include <linux/slab.h> | ||
| 22 | #include <linux/earlycpio.h> | ||
| 23 | #include <linux/initrd.h> | ||
| 24 | #include <linux/cpu.h> | ||
| 25 | #include <asm/msr.h> | ||
| 26 | #include <asm/microcode_intel.h> | ||
| 27 | #include <asm/processor.h> | ||
| 28 | #include <asm/tlbflush.h> | ||
| 29 | #include <asm/setup.h> | ||
| 30 | |||
| 31 | unsigned long mc_saved_in_initrd[MAX_UCODE_COUNT]; | ||
| 32 | struct mc_saved_data { | ||
| 33 | unsigned int mc_saved_count; | ||
| 34 | struct microcode_intel **mc_saved; | ||
| 35 | } mc_saved_data; | ||
| 36 | |||
| 37 | static enum ucode_state __cpuinit | ||
| 38 | generic_load_microcode_early(struct microcode_intel **mc_saved_p, | ||
| 39 | unsigned int mc_saved_count, | ||
| 40 | struct ucode_cpu_info *uci) | ||
| 41 | { | ||
| 42 | struct microcode_intel *ucode_ptr, *new_mc = NULL; | ||
| 43 | int new_rev = uci->cpu_sig.rev; | ||
| 44 | enum ucode_state state = UCODE_OK; | ||
| 45 | unsigned int mc_size; | ||
| 46 | struct microcode_header_intel *mc_header; | ||
| 47 | unsigned int csig = uci->cpu_sig.sig; | ||
| 48 | unsigned int cpf = uci->cpu_sig.pf; | ||
| 49 | int i; | ||
| 50 | |||
| 51 | for (i = 0; i < mc_saved_count; i++) { | ||
| 52 | ucode_ptr = mc_saved_p[i]; | ||
| 53 | |||
| 54 | mc_header = (struct microcode_header_intel *)ucode_ptr; | ||
| 55 | mc_size = get_totalsize(mc_header); | ||
| 56 | if (get_matching_microcode(csig, cpf, ucode_ptr, new_rev)) { | ||
| 57 | new_rev = mc_header->rev; | ||
| 58 | new_mc = ucode_ptr; | ||
| 59 | } | ||
| 60 | } | ||
| 61 | |||
| 62 | if (!new_mc) { | ||
| 63 | state = UCODE_NFOUND; | ||
| 64 | goto out; | ||
| 65 | } | ||
| 66 | |||
| 67 | uci->mc = (struct microcode_intel *)new_mc; | ||
| 68 | out: | ||
| 69 | return state; | ||
| 70 | } | ||
| 71 | |||
| 72 | static void __cpuinit | ||
| 73 | microcode_pointer(struct microcode_intel **mc_saved, | ||
| 74 | unsigned long *mc_saved_in_initrd, | ||
| 75 | unsigned long initrd_start, int mc_saved_count) | ||
| 76 | { | ||
| 77 | int i; | ||
| 78 | |||
| 79 | for (i = 0; i < mc_saved_count; i++) | ||
| 80 | mc_saved[i] = (struct microcode_intel *) | ||
| 81 | (mc_saved_in_initrd[i] + initrd_start); | ||
| 82 | } | ||
| 83 | |||
| 84 | #ifdef CONFIG_X86_32 | ||
| 85 | static void __cpuinit | ||
| 86 | microcode_phys(struct microcode_intel **mc_saved_tmp, | ||
| 87 | struct mc_saved_data *mc_saved_data) | ||
| 88 | { | ||
| 89 | int i; | ||
| 90 | struct microcode_intel ***mc_saved; | ||
| 91 | |||
| 92 | mc_saved = (struct microcode_intel ***) | ||
| 93 | __pa_symbol(&mc_saved_data->mc_saved); | ||
| 94 | for (i = 0; i < mc_saved_data->mc_saved_count; i++) { | ||
| 95 | struct microcode_intel *p; | ||
| 96 | |||
| 97 | p = *(struct microcode_intel **) | ||
| 98 | __pa(mc_saved_data->mc_saved + i); | ||
| 99 | mc_saved_tmp[i] = (struct microcode_intel *)__pa(p); | ||
| 100 | } | ||
| 101 | } | ||
| 102 | #endif | ||
| 103 | |||
| 104 | static enum ucode_state __cpuinit | ||
| 105 | load_microcode(struct mc_saved_data *mc_saved_data, | ||
| 106 | unsigned long *mc_saved_in_initrd, | ||
| 107 | unsigned long initrd_start, | ||
| 108 | struct ucode_cpu_info *uci) | ||
| 109 | { | ||
| 110 | struct microcode_intel *mc_saved_tmp[MAX_UCODE_COUNT]; | ||
| 111 | unsigned int count = mc_saved_data->mc_saved_count; | ||
| 112 | |||
| 113 | if (!mc_saved_data->mc_saved) { | ||
| 114 | microcode_pointer(mc_saved_tmp, mc_saved_in_initrd, | ||
| 115 | initrd_start, count); | ||
| 116 | |||
| 117 | return generic_load_microcode_early(mc_saved_tmp, count, uci); | ||
| 118 | } else { | ||
| 119 | #ifdef CONFIG_X86_32 | ||
| 120 | microcode_phys(mc_saved_tmp, mc_saved_data); | ||
| 121 | return generic_load_microcode_early(mc_saved_tmp, count, uci); | ||
| 122 | #else | ||
| 123 | return generic_load_microcode_early(mc_saved_data->mc_saved, | ||
| 124 | count, uci); | ||
| 125 | #endif | ||
| 126 | } | ||
| 127 | } | ||
| 128 | |||
| 129 | static u8 get_x86_family(unsigned long sig) | ||
| 130 | { | ||
| 131 | u8 x86; | ||
| 132 | |||
| 133 | x86 = (sig >> 8) & 0xf; | ||
| 134 | |||
| 135 | if (x86 == 0xf) | ||
| 136 | x86 += (sig >> 20) & 0xff; | ||
| 137 | |||
| 138 | return x86; | ||
| 139 | } | ||
| 140 | |||
| 141 | static u8 get_x86_model(unsigned long sig) | ||
| 142 | { | ||
| 143 | u8 x86, x86_model; | ||
| 144 | |||
| 145 | x86 = get_x86_family(sig); | ||
| 146 | x86_model = (sig >> 4) & 0xf; | ||
| 147 | |||
| 148 | if (x86 == 0x6 || x86 == 0xf) | ||
| 149 | x86_model += ((sig >> 16) & 0xf) << 4; | ||
| 150 | |||
| 151 | return x86_model; | ||
| 152 | } | ||
| 153 | |||
| 154 | /* | ||
| 155 | * Given CPU signature and a microcode patch, this function finds if the | ||
| 156 | * microcode patch has matching family and model with the CPU. | ||
| 157 | */ | ||
| 158 | static enum ucode_state | ||
| 159 | matching_model_microcode(struct microcode_header_intel *mc_header, | ||
| 160 | unsigned long sig) | ||
| 161 | { | ||
| 162 | u8 x86, x86_model; | ||
| 163 | u8 x86_ucode, x86_model_ucode; | ||
| 164 | struct extended_sigtable *ext_header; | ||
| 165 | unsigned long total_size = get_totalsize(mc_header); | ||
| 166 | unsigned long data_size = get_datasize(mc_header); | ||
| 167 | int ext_sigcount, i; | ||
| 168 | struct extended_signature *ext_sig; | ||
| 169 | |||
| 170 | x86 = get_x86_family(sig); | ||
| 171 | x86_model = get_x86_model(sig); | ||
| 172 | |||
| 173 | x86_ucode = get_x86_family(mc_header->sig); | ||
| 174 | x86_model_ucode = get_x86_model(mc_header->sig); | ||
| 175 | |||
| 176 | if (x86 == x86_ucode && x86_model == x86_model_ucode) | ||
| 177 | return UCODE_OK; | ||
| 178 | |||
| 179 | /* Look for ext. headers: */ | ||
| 180 | if (total_size <= data_size + MC_HEADER_SIZE) | ||
| 181 | return UCODE_NFOUND; | ||
| 182 | |||
| 183 | ext_header = (struct extended_sigtable *) | ||
| 184 | mc_header + data_size + MC_HEADER_SIZE; | ||
| 185 | ext_sigcount = ext_header->count; | ||
| 186 | ext_sig = (void *)ext_header + EXT_HEADER_SIZE; | ||
| 187 | |||
| 188 | for (i = 0; i < ext_sigcount; i++) { | ||
| 189 | x86_ucode = get_x86_family(ext_sig->sig); | ||
| 190 | x86_model_ucode = get_x86_model(ext_sig->sig); | ||
| 191 | |||
| 192 | if (x86 == x86_ucode && x86_model == x86_model_ucode) | ||
| 193 | return UCODE_OK; | ||
| 194 | |||
| 195 | ext_sig++; | ||
| 196 | } | ||
| 197 | |||
| 198 | return UCODE_NFOUND; | ||
| 199 | } | ||
| 200 | |||
| 201 | static int | ||
| 202 | save_microcode(struct mc_saved_data *mc_saved_data, | ||
| 203 | struct microcode_intel **mc_saved_src, | ||
| 204 | unsigned int mc_saved_count) | ||
| 205 | { | ||
| 206 | int i, j; | ||
| 207 | struct microcode_intel **mc_saved_p; | ||
| 208 | int ret; | ||
| 209 | |||
| 210 | if (!mc_saved_count) | ||
| 211 | return -EINVAL; | ||
| 212 | |||
| 213 | /* | ||
| 214 | * Copy new microcode data. | ||
| 215 | */ | ||
| 216 | mc_saved_p = kmalloc(mc_saved_count*sizeof(struct microcode_intel *), | ||
| 217 | GFP_KERNEL); | ||
| 218 | if (!mc_saved_p) | ||
| 219 | return -ENOMEM; | ||
| 220 | |||
| 221 | for (i = 0; i < mc_saved_count; i++) { | ||
| 222 | struct microcode_intel *mc = mc_saved_src[i]; | ||
| 223 | struct microcode_header_intel *mc_header = &mc->hdr; | ||
| 224 | unsigned long mc_size = get_totalsize(mc_header); | ||
| 225 | mc_saved_p[i] = kmalloc(mc_size, GFP_KERNEL); | ||
| 226 | if (!mc_saved_p[i]) { | ||
| 227 | ret = -ENOMEM; | ||
| 228 | goto err; | ||
| 229 | } | ||
| 230 | if (!mc_saved_src[i]) { | ||
| 231 | ret = -EINVAL; | ||
| 232 | goto err; | ||
| 233 | } | ||
| 234 | memcpy(mc_saved_p[i], mc, mc_size); | ||
| 235 | } | ||
| 236 | |||
| 237 | /* | ||
| 238 | * Point to newly saved microcode. | ||
| 239 | */ | ||
| 240 | mc_saved_data->mc_saved = mc_saved_p; | ||
| 241 | mc_saved_data->mc_saved_count = mc_saved_count; | ||
| 242 | |||
| 243 | return 0; | ||
| 244 | |||
| 245 | err: | ||
| 246 | for (j = 0; j <= i; j++) | ||
| 247 | kfree(mc_saved_p[j]); | ||
| 248 | kfree(mc_saved_p); | ||
| 249 | |||
| 250 | return ret; | ||
| 251 | } | ||
| 252 | |||
| 253 | /* | ||
| 254 | * A microcode patch in ucode_ptr is saved into mc_saved | ||
| 255 | * - if it has matching signature and newer revision compared to an existing | ||
| 256 | * patch mc_saved. | ||
| 257 | * - or if it is a newly discovered microcode patch. | ||
| 258 | * | ||
| 259 | * The microcode patch should have matching model with CPU. | ||
| 260 | */ | ||
| 261 | static void _save_mc(struct microcode_intel **mc_saved, u8 *ucode_ptr, | ||
| 262 | unsigned int *mc_saved_count_p) | ||
| 263 | { | ||
| 264 | int i; | ||
| 265 | int found = 0; | ||
| 266 | unsigned int mc_saved_count = *mc_saved_count_p; | ||
| 267 | struct microcode_header_intel *mc_header; | ||
| 268 | |||
| 269 | mc_header = (struct microcode_header_intel *)ucode_ptr; | ||
| 270 | for (i = 0; i < mc_saved_count; i++) { | ||
| 271 | unsigned int sig, pf; | ||
| 272 | unsigned int new_rev; | ||
| 273 | struct microcode_header_intel *mc_saved_header = | ||
| 274 | (struct microcode_header_intel *)mc_saved[i]; | ||
| 275 | sig = mc_saved_header->sig; | ||
| 276 | pf = mc_saved_header->pf; | ||
| 277 | new_rev = mc_header->rev; | ||
| 278 | |||
| 279 | if (get_matching_sig(sig, pf, ucode_ptr, new_rev)) { | ||
| 280 | found = 1; | ||
| 281 | if (update_match_revision(mc_header, new_rev)) { | ||
| 282 | /* | ||
| 283 | * Found an older ucode saved before. | ||
| 284 | * Replace the older one with this newer | ||
| 285 | * one. | ||
| 286 | */ | ||
| 287 | mc_saved[i] = | ||
| 288 | (struct microcode_intel *)ucode_ptr; | ||
| 289 | break; | ||
| 290 | } | ||
| 291 | } | ||
| 292 | } | ||
| 293 | if (i >= mc_saved_count && !found) | ||
| 294 | /* | ||
| 295 | * This ucode is first time discovered in ucode file. | ||
| 296 | * Save it to memory. | ||
| 297 | */ | ||
| 298 | mc_saved[mc_saved_count++] = | ||
| 299 | (struct microcode_intel *)ucode_ptr; | ||
| 300 | |||
| 301 | *mc_saved_count_p = mc_saved_count; | ||
| 302 | } | ||
| 303 | |||
| 304 | /* | ||
| 305 | * Get microcode matching with BSP's model. Only CPUs with the same model as | ||
| 306 | * BSP can stay in the platform. | ||
| 307 | */ | ||
| 308 | static enum ucode_state __init | ||
| 309 | get_matching_model_microcode(int cpu, unsigned long start, | ||
| 310 | void *data, size_t size, | ||
| 311 | struct mc_saved_data *mc_saved_data, | ||
| 312 | unsigned long *mc_saved_in_initrd, | ||
| 313 | struct ucode_cpu_info *uci) | ||
| 314 | { | ||
| 315 | u8 *ucode_ptr = data; | ||
| 316 | unsigned int leftover = size; | ||
| 317 | enum ucode_state state = UCODE_OK; | ||
| 318 | unsigned int mc_size; | ||
| 319 | struct microcode_header_intel *mc_header; | ||
| 320 | struct microcode_intel *mc_saved_tmp[MAX_UCODE_COUNT]; | ||
| 321 | unsigned int mc_saved_count = mc_saved_data->mc_saved_count; | ||
| 322 | int i; | ||
| 323 | |||
| 324 | while (leftover) { | ||
| 325 | mc_header = (struct microcode_header_intel *)ucode_ptr; | ||
| 326 | |||
| 327 | mc_size = get_totalsize(mc_header); | ||
| 328 | if (!mc_size || mc_size > leftover || | ||
| 329 | microcode_sanity_check(ucode_ptr, 0) < 0) | ||
| 330 | break; | ||
| 331 | |||
| 332 | leftover -= mc_size; | ||
| 333 | |||
| 334 | /* | ||
| 335 | * Since APs with same family and model as the BSP may boot in | ||
| 336 | * the platform, we need to find and save microcode patches | ||
| 337 | * with the same family and model as the BSP. | ||
| 338 | */ | ||
| 339 | if (matching_model_microcode(mc_header, uci->cpu_sig.sig) != | ||
| 340 | UCODE_OK) { | ||
| 341 | ucode_ptr += mc_size; | ||
| 342 | continue; | ||
| 343 | } | ||
| 344 | |||
| 345 | _save_mc(mc_saved_tmp, ucode_ptr, &mc_saved_count); | ||
| 346 | |||
| 347 | ucode_ptr += mc_size; | ||
| 348 | } | ||
| 349 | |||
| 350 | if (leftover) { | ||
| 351 | state = UCODE_ERROR; | ||
| 352 | goto out; | ||
| 353 | } | ||
| 354 | |||
| 355 | if (mc_saved_count == 0) { | ||
| 356 | state = UCODE_NFOUND; | ||
| 357 | goto out; | ||
| 358 | } | ||
| 359 | |||
| 360 | for (i = 0; i < mc_saved_count; i++) | ||
| 361 | mc_saved_in_initrd[i] = (unsigned long)mc_saved_tmp[i] - start; | ||
| 362 | |||
| 363 | mc_saved_data->mc_saved_count = mc_saved_count; | ||
| 364 | out: | ||
| 365 | return state; | ||
| 366 | } | ||
| 367 | |||
| 368 | #define native_rdmsr(msr, val1, val2) \ | ||
| 369 | do { \ | ||
| 370 | u64 __val = native_read_msr((msr)); \ | ||
| 371 | (void)((val1) = (u32)__val); \ | ||
| 372 | (void)((val2) = (u32)(__val >> 32)); \ | ||
| 373 | } while (0) | ||
| 374 | |||
| 375 | #define native_wrmsr(msr, low, high) \ | ||
| 376 | native_write_msr(msr, low, high); | ||
| 377 | |||
| 378 | static int __cpuinit collect_cpu_info_early(struct ucode_cpu_info *uci) | ||
| 379 | { | ||
| 380 | unsigned int val[2]; | ||
| 381 | u8 x86, x86_model; | ||
| 382 | struct cpu_signature csig; | ||
| 383 | unsigned int eax, ebx, ecx, edx; | ||
| 384 | |||
| 385 | csig.sig = 0; | ||
| 386 | csig.pf = 0; | ||
| 387 | csig.rev = 0; | ||
| 388 | |||
| 389 | memset(uci, 0, sizeof(*uci)); | ||
| 390 | |||
| 391 | eax = 0x00000001; | ||
| 392 | ecx = 0; | ||
| 393 | native_cpuid(&eax, &ebx, &ecx, &edx); | ||
| 394 | csig.sig = eax; | ||
| 395 | |||
| 396 | x86 = get_x86_family(csig.sig); | ||
| 397 | x86_model = get_x86_model(csig.sig); | ||
| 398 | |||
| 399 | if ((x86_model >= 5) || (x86 > 6)) { | ||
| 400 | /* get processor flags from MSR 0x17 */ | ||
| 401 | native_rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]); | ||
| 402 | csig.pf = 1 << ((val[1] >> 18) & 7); | ||
| 403 | } | ||
| 404 | native_wrmsr(MSR_IA32_UCODE_REV, 0, 0); | ||
| 405 | |||
| 406 | /* As documented in the SDM: Do a CPUID 1 here */ | ||
| 407 | sync_core(); | ||
| 408 | |||
| 409 | /* get the current revision from MSR 0x8B */ | ||
| 410 | native_rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]); | ||
| 411 | |||
| 412 | csig.rev = val[1]; | ||
| 413 | |||
| 414 | uci->cpu_sig = csig; | ||
| 415 | uci->valid = 1; | ||
| 416 | |||
| 417 | return 0; | ||
| 418 | } | ||
| 419 | |||
| 420 | #ifdef DEBUG | ||
| 421 | static void __ref show_saved_mc(void) | ||
| 422 | { | ||
| 423 | int i, j; | ||
| 424 | unsigned int sig, pf, rev, total_size, data_size, date; | ||
| 425 | struct ucode_cpu_info uci; | ||
| 426 | |||
| 427 | if (mc_saved_data.mc_saved_count == 0) { | ||
| 428 | pr_debug("no micorcode data saved.\n"); | ||
| 429 | return; | ||
| 430 | } | ||
| 431 | pr_debug("Total microcode saved: %d\n", mc_saved_data.mc_saved_count); | ||
| 432 | |||
| 433 | collect_cpu_info_early(&uci); | ||
| 434 | |||
| 435 | sig = uci.cpu_sig.sig; | ||
| 436 | pf = uci.cpu_sig.pf; | ||
| 437 | rev = uci.cpu_sig.rev; | ||
| 438 | pr_debug("CPU%d: sig=0x%x, pf=0x%x, rev=0x%x\n", | ||
| 439 | smp_processor_id(), sig, pf, rev); | ||
| 440 | |||
| 441 | for (i = 0; i < mc_saved_data.mc_saved_count; i++) { | ||
| 442 | struct microcode_header_intel *mc_saved_header; | ||
| 443 | struct extended_sigtable *ext_header; | ||
| 444 | int ext_sigcount; | ||
| 445 | struct extended_signature *ext_sig; | ||
| 446 | |||
| 447 | mc_saved_header = (struct microcode_header_intel *) | ||
| 448 | mc_saved_data.mc_saved[i]; | ||
| 449 | sig = mc_saved_header->sig; | ||
| 450 | pf = mc_saved_header->pf; | ||
| 451 | rev = mc_saved_header->rev; | ||
| 452 | total_size = get_totalsize(mc_saved_header); | ||
| 453 | data_size = get_datasize(mc_saved_header); | ||
| 454 | date = mc_saved_header->date; | ||
| 455 | |||
| 456 | pr_debug("mc_saved[%d]: sig=0x%x, pf=0x%x, rev=0x%x, toal size=0x%x, date = %04x-%02x-%02x\n", | ||
| 457 | i, sig, pf, rev, total_size, | ||
| 458 | date & 0xffff, | ||
| 459 | date >> 24, | ||
| 460 | (date >> 16) & 0xff); | ||
| 461 | |||
| 462 | /* Look for ext. headers: */ | ||
| 463 | if (total_size <= data_size + MC_HEADER_SIZE) | ||
| 464 | continue; | ||
| 465 | |||
| 466 | ext_header = (struct extended_sigtable *) | ||
| 467 | mc_saved_header + data_size + MC_HEADER_SIZE; | ||
| 468 | ext_sigcount = ext_header->count; | ||
| 469 | ext_sig = (void *)ext_header + EXT_HEADER_SIZE; | ||
| 470 | |||
| 471 | for (j = 0; j < ext_sigcount; j++) { | ||
| 472 | sig = ext_sig->sig; | ||
| 473 | pf = ext_sig->pf; | ||
| 474 | |||
| 475 | pr_debug("\tExtended[%d]: sig=0x%x, pf=0x%x\n", | ||
| 476 | j, sig, pf); | ||
| 477 | |||
| 478 | ext_sig++; | ||
| 479 | } | ||
| 480 | |||
| 481 | } | ||
| 482 | } | ||
| 483 | #else | ||
| 484 | static inline void show_saved_mc(void) | ||
| 485 | { | ||
| 486 | } | ||
| 487 | #endif | ||
| 488 | |||
| 489 | #if defined(CONFIG_MICROCODE_INTEL_EARLY) && defined(CONFIG_HOTPLUG_CPU) | ||
| 490 | /* | ||
| 491 | * Save this mc into mc_saved_data. So it will be loaded early when a CPU is | ||
| 492 | * hot added or resumes. | ||
| 493 | * | ||
| 494 | * Please make sure this mc should be a valid microcode patch before calling | ||
| 495 | * this function. | ||
| 496 | */ | ||
| 497 | int save_mc_for_early(u8 *mc) | ||
| 498 | { | ||
| 499 | struct microcode_intel *mc_saved_tmp[MAX_UCODE_COUNT]; | ||
| 500 | unsigned int mc_saved_count_init; | ||
| 501 | unsigned int mc_saved_count; | ||
| 502 | struct microcode_intel **mc_saved; | ||
| 503 | int ret = 0; | ||
| 504 | int i; | ||
| 505 | |||
| 506 | /* | ||
| 507 | * Hold hotplug lock so mc_saved_data is not accessed by a CPU in | ||
| 508 | * hotplug. | ||
| 509 | */ | ||
| 510 | cpu_hotplug_driver_lock(); | ||
| 511 | |||
| 512 | mc_saved_count_init = mc_saved_data.mc_saved_count; | ||
| 513 | mc_saved_count = mc_saved_data.mc_saved_count; | ||
| 514 | mc_saved = mc_saved_data.mc_saved; | ||
| 515 | |||
| 516 | if (mc_saved && mc_saved_count) | ||
| 517 | memcpy(mc_saved_tmp, mc_saved, | ||
| 518 | mc_saved_count * sizeof(struct mirocode_intel *)); | ||
| 519 | /* | ||
| 520 | * Save the microcode patch mc in mc_save_tmp structure if it's a newer | ||
| 521 | * version. | ||
| 522 | */ | ||
| 523 | |||
| 524 | _save_mc(mc_saved_tmp, mc, &mc_saved_count); | ||
| 525 | |||
| 526 | /* | ||
| 527 | * Save the mc_save_tmp in global mc_saved_data. | ||
| 528 | */ | ||
| 529 | ret = save_microcode(&mc_saved_data, mc_saved_tmp, mc_saved_count); | ||
| 530 | if (ret) { | ||
| 531 | pr_err("Can not save microcode patch.\n"); | ||
| 532 | goto out; | ||
| 533 | } | ||
| 534 | |||
| 535 | show_saved_mc(); | ||
| 536 | |||
| 537 | /* | ||
| 538 | * Free old saved microcod data. | ||
| 539 | */ | ||
| 540 | if (mc_saved) { | ||
| 541 | for (i = 0; i < mc_saved_count_init; i++) | ||
| 542 | kfree(mc_saved[i]); | ||
| 543 | kfree(mc_saved); | ||
| 544 | } | ||
| 545 | |||
| 546 | out: | ||
| 547 | cpu_hotplug_driver_unlock(); | ||
| 548 | |||
| 549 | return ret; | ||
| 550 | } | ||
| 551 | EXPORT_SYMBOL_GPL(save_mc_for_early); | ||
| 552 | #endif | ||
| 553 | |||
| 554 | static __initdata char ucode_name[] = "kernel/x86/microcode/GenuineIntel.bin"; | ||
| 555 | static __init enum ucode_state | ||
| 556 | scan_microcode(unsigned long start, unsigned long end, | ||
| 557 | struct mc_saved_data *mc_saved_data, | ||
| 558 | unsigned long *mc_saved_in_initrd, | ||
| 559 | struct ucode_cpu_info *uci) | ||
| 560 | { | ||
| 561 | unsigned int size = end - start + 1; | ||
| 562 | struct cpio_data cd; | ||
| 563 | long offset = 0; | ||
| 564 | #ifdef CONFIG_X86_32 | ||
| 565 | char *p = (char *)__pa_symbol(ucode_name); | ||
| 566 | #else | ||
| 567 | char *p = ucode_name; | ||
| 568 | #endif | ||
| 569 | |||
| 570 | cd.data = NULL; | ||
| 571 | cd.size = 0; | ||
| 572 | |||
| 573 | cd = find_cpio_data(p, (void *)start, size, &offset); | ||
| 574 | if (!cd.data) | ||
| 575 | return UCODE_ERROR; | ||
| 576 | |||
| 577 | |||
| 578 | return get_matching_model_microcode(0, start, cd.data, cd.size, | ||
| 579 | mc_saved_data, mc_saved_in_initrd, | ||
| 580 | uci); | ||
| 581 | } | ||
| 582 | |||
| 583 | /* | ||
| 584 | * Print ucode update info. | ||
| 585 | */ | ||
| 586 | static void __cpuinit | ||
| 587 | print_ucode_info(struct ucode_cpu_info *uci, unsigned int date) | ||
| 588 | { | ||
| 589 | int cpu = smp_processor_id(); | ||
| 590 | |||
| 591 | pr_info("CPU%d microcode updated early to revision 0x%x, date = %04x-%02x-%02x\n", | ||
| 592 | cpu, | ||
| 593 | uci->cpu_sig.rev, | ||
| 594 | date & 0xffff, | ||
| 595 | date >> 24, | ||
| 596 | (date >> 16) & 0xff); | ||
| 597 | } | ||
| 598 | |||
| 599 | #ifdef CONFIG_X86_32 | ||
| 600 | |||
| 601 | static int delay_ucode_info; | ||
| 602 | static int current_mc_date; | ||
| 603 | |||
| 604 | /* | ||
| 605 | * Print early updated ucode info after printk works. This is delayed info dump. | ||
| 606 | */ | ||
| 607 | void __cpuinit show_ucode_info_early(void) | ||
| 608 | { | ||
| 609 | struct ucode_cpu_info uci; | ||
| 610 | |||
| 611 | if (delay_ucode_info) { | ||
| 612 | collect_cpu_info_early(&uci); | ||
| 613 | print_ucode_info(&uci, current_mc_date); | ||
| 614 | delay_ucode_info = 0; | ||
| 615 | } | ||
| 616 | } | ||
| 617 | |||
| 618 | /* | ||
| 619 | * At this point, we can not call printk() yet. Keep microcode patch number in | ||
| 620 | * mc_saved_data.mc_saved and delay printing microcode info in | ||
| 621 | * show_ucode_info_early() until printk() works. | ||
| 622 | */ | ||
| 623 | static void __cpuinit print_ucode(struct ucode_cpu_info *uci) | ||
| 624 | { | ||
| 625 | struct microcode_intel *mc_intel; | ||
| 626 | int *delay_ucode_info_p; | ||
| 627 | int *current_mc_date_p; | ||
| 628 | |||
| 629 | mc_intel = uci->mc; | ||
| 630 | if (mc_intel == NULL) | ||
| 631 | return; | ||
| 632 | |||
| 633 | delay_ucode_info_p = (int *)__pa_symbol(&delay_ucode_info); | ||
| 634 | current_mc_date_p = (int *)__pa_symbol(¤t_mc_date); | ||
| 635 | |||
| 636 | *delay_ucode_info_p = 1; | ||
| 637 | *current_mc_date_p = mc_intel->hdr.date; | ||
| 638 | } | ||
| 639 | #else | ||
| 640 | |||
| 641 | /* | ||
| 642 | * Flush global tlb. We only do this in x86_64 where paging has been enabled | ||
| 643 | * already and PGE should be enabled as well. | ||
| 644 | */ | ||
| 645 | static inline void __cpuinit flush_tlb_early(void) | ||
| 646 | { | ||
| 647 | __native_flush_tlb_global_irq_disabled(); | ||
| 648 | } | ||
| 649 | |||
| 650 | static inline void __cpuinit print_ucode(struct ucode_cpu_info *uci) | ||
| 651 | { | ||
| 652 | struct microcode_intel *mc_intel; | ||
| 653 | |||
| 654 | mc_intel = uci->mc; | ||
| 655 | if (mc_intel == NULL) | ||
| 656 | return; | ||
| 657 | |||
| 658 | print_ucode_info(uci, mc_intel->hdr.date); | ||
| 659 | } | ||
| 660 | #endif | ||
| 661 | |||
| 662 | static int apply_microcode_early(struct mc_saved_data *mc_saved_data, | ||
| 663 | struct ucode_cpu_info *uci) | ||
| 664 | { | ||
| 665 | struct microcode_intel *mc_intel; | ||
| 666 | unsigned int val[2]; | ||
| 667 | |||
| 668 | mc_intel = uci->mc; | ||
| 669 | if (mc_intel == NULL) | ||
| 670 | return 0; | ||
| 671 | |||
| 672 | /* write microcode via MSR 0x79 */ | ||
| 673 | native_wrmsr(MSR_IA32_UCODE_WRITE, | ||
| 674 | (unsigned long) mc_intel->bits, | ||
| 675 | (unsigned long) mc_intel->bits >> 16 >> 16); | ||
| 676 | native_wrmsr(MSR_IA32_UCODE_REV, 0, 0); | ||
| 677 | |||
| 678 | /* As documented in the SDM: Do a CPUID 1 here */ | ||
| 679 | sync_core(); | ||
| 680 | |||
| 681 | /* get the current revision from MSR 0x8B */ | ||
| 682 | native_rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]); | ||
| 683 | if (val[1] != mc_intel->hdr.rev) | ||
| 684 | return -1; | ||
| 685 | |||
| 686 | #ifdef CONFIG_X86_64 | ||
| 687 | /* Flush global tlb. This is precaution. */ | ||
| 688 | flush_tlb_early(); | ||
| 689 | #endif | ||
| 690 | uci->cpu_sig.rev = val[1]; | ||
| 691 | |||
| 692 | print_ucode(uci); | ||
| 693 | |||
| 694 | return 0; | ||
| 695 | } | ||
| 696 | |||
| 697 | /* | ||
| 698 | * This function converts microcode patch offsets previously stored in | ||
| 699 | * mc_saved_in_initrd to pointers and stores the pointers in mc_saved_data. | ||
| 700 | */ | ||
| 701 | int __init save_microcode_in_initrd(void) | ||
| 702 | { | ||
| 703 | unsigned int count = mc_saved_data.mc_saved_count; | ||
| 704 | struct microcode_intel *mc_saved[MAX_UCODE_COUNT]; | ||
| 705 | int ret = 0; | ||
| 706 | |||
| 707 | if (count == 0) | ||
| 708 | return ret; | ||
| 709 | |||
| 710 | microcode_pointer(mc_saved, mc_saved_in_initrd, initrd_start, count); | ||
| 711 | ret = save_microcode(&mc_saved_data, mc_saved, count); | ||
| 712 | if (ret) | ||
| 713 | pr_err("Can not save microcod patches from initrd"); | ||
| 714 | |||
| 715 | show_saved_mc(); | ||
| 716 | |||
| 717 | return ret; | ||
| 718 | } | ||
| 719 | |||
| 720 | static void __init | ||
| 721 | _load_ucode_intel_bsp(struct mc_saved_data *mc_saved_data, | ||
| 722 | unsigned long *mc_saved_in_initrd, | ||
| 723 | unsigned long initrd_start_early, | ||
| 724 | unsigned long initrd_end_early, | ||
| 725 | struct ucode_cpu_info *uci) | ||
| 726 | { | ||
| 727 | collect_cpu_info_early(uci); | ||
| 728 | scan_microcode(initrd_start_early, initrd_end_early, mc_saved_data, | ||
| 729 | mc_saved_in_initrd, uci); | ||
| 730 | load_microcode(mc_saved_data, mc_saved_in_initrd, | ||
| 731 | initrd_start_early, uci); | ||
| 732 | apply_microcode_early(mc_saved_data, uci); | ||
| 733 | } | ||
| 734 | |||
| 735 | void __init | ||
| 736 | load_ucode_intel_bsp(void) | ||
| 737 | { | ||
| 738 | u64 ramdisk_image, ramdisk_size; | ||
| 739 | unsigned long initrd_start_early, initrd_end_early; | ||
| 740 | struct ucode_cpu_info uci; | ||
| 741 | #ifdef CONFIG_X86_32 | ||
| 742 | struct boot_params *boot_params_p; | ||
| 743 | |||
| 744 | boot_params_p = (struct boot_params *)__pa_symbol(&boot_params); | ||
| 745 | ramdisk_image = boot_params_p->hdr.ramdisk_image; | ||
| 746 | ramdisk_size = boot_params_p->hdr.ramdisk_size; | ||
| 747 | initrd_start_early = ramdisk_image; | ||
| 748 | initrd_end_early = initrd_start_early + ramdisk_size; | ||
| 749 | |||
| 750 | _load_ucode_intel_bsp( | ||
| 751 | (struct mc_saved_data *)__pa_symbol(&mc_saved_data), | ||
| 752 | (unsigned long *)__pa_symbol(&mc_saved_in_initrd), | ||
| 753 | initrd_start_early, initrd_end_early, &uci); | ||
| 754 | #else | ||
| 755 | ramdisk_image = boot_params.hdr.ramdisk_image; | ||
| 756 | ramdisk_size = boot_params.hdr.ramdisk_size; | ||
| 757 | initrd_start_early = ramdisk_image + PAGE_OFFSET; | ||
| 758 | initrd_end_early = initrd_start_early + ramdisk_size; | ||
| 759 | |||
| 760 | _load_ucode_intel_bsp(&mc_saved_data, mc_saved_in_initrd, | ||
| 761 | initrd_start_early, initrd_end_early, &uci); | ||
| 762 | #endif | ||
| 763 | } | ||
| 764 | |||
| 765 | void __cpuinit load_ucode_intel_ap(void) | ||
| 766 | { | ||
| 767 | struct mc_saved_data *mc_saved_data_p; | ||
| 768 | struct ucode_cpu_info uci; | ||
| 769 | unsigned long *mc_saved_in_initrd_p; | ||
| 770 | unsigned long initrd_start_addr; | ||
| 771 | #ifdef CONFIG_X86_32 | ||
| 772 | unsigned long *initrd_start_p; | ||
| 773 | |||
| 774 | mc_saved_in_initrd_p = | ||
| 775 | (unsigned long *)__pa_symbol(mc_saved_in_initrd); | ||
| 776 | mc_saved_data_p = (struct mc_saved_data *)__pa_symbol(&mc_saved_data); | ||
| 777 | initrd_start_p = (unsigned long *)__pa_symbol(&initrd_start); | ||
| 778 | initrd_start_addr = (unsigned long)__pa_symbol(*initrd_start_p); | ||
| 779 | #else | ||
| 780 | mc_saved_data_p = &mc_saved_data; | ||
| 781 | mc_saved_in_initrd_p = mc_saved_in_initrd; | ||
| 782 | initrd_start_addr = initrd_start; | ||
| 783 | #endif | ||
| 784 | |||
| 785 | /* | ||
| 786 | * If there is no valid ucode previously saved in memory, no need to | ||
| 787 | * update ucode on this AP. | ||
| 788 | */ | ||
| 789 | if (mc_saved_data_p->mc_saved_count == 0) | ||
| 790 | return; | ||
| 791 | |||
| 792 | collect_cpu_info_early(&uci); | ||
| 793 | load_microcode(mc_saved_data_p, mc_saved_in_initrd_p, | ||
| 794 | initrd_start_addr, &uci); | ||
| 795 | apply_microcode_early(mc_saved_data_p, &uci); | ||
| 796 | } | ||
