linux/arch/x86/kernel/cpu/microcode/intel_early.c
<<
>>
Prefs
   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
  31unsigned long mc_saved_in_initrd[MAX_UCODE_COUNT];
  32struct mc_saved_data {
  33        unsigned int mc_saved_count;
  34        struct microcode_intel **mc_saved;
  35} mc_saved_data;
  36
  37static enum ucode_state
  38generic_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;
  68out:
  69        return state;
  70}
  71
  72static void
  73microcode_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
  85static void
  86microcode_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_nodebug(&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_nodebug(mc_saved_data->mc_saved + i);
  99                mc_saved_tmp[i] = (struct microcode_intel *)__pa_nodebug(p);
 100        }
 101}
 102#endif
 103
 104static enum ucode_state
 105load_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
 129static 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
 141static 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 */
 158static enum ucode_state
 159matching_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
 201static int
 202save_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
 245err:
 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 */
 261static 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 */
 308static enum ucode_state __init
 309get_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;
 364out:
 365        return state;
 366}
 367
 368static int collect_cpu_info_early(struct ucode_cpu_info *uci)
 369{
 370        unsigned int val[2];
 371        u8 x86, x86_model;
 372        struct cpu_signature csig;
 373        unsigned int eax, ebx, ecx, edx;
 374
 375        csig.sig = 0;
 376        csig.pf = 0;
 377        csig.rev = 0;
 378
 379        memset(uci, 0, sizeof(*uci));
 380
 381        eax = 0x00000001;
 382        ecx = 0;
 383        native_cpuid(&eax, &ebx, &ecx, &edx);
 384        csig.sig = eax;
 385
 386        x86 = get_x86_family(csig.sig);
 387        x86_model = get_x86_model(csig.sig);
 388
 389        if ((x86_model >= 5) || (x86 > 6)) {
 390                /* get processor flags from MSR 0x17 */
 391                native_rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]);
 392                csig.pf = 1 << ((val[1] >> 18) & 7);
 393        }
 394        native_wrmsr(MSR_IA32_UCODE_REV, 0, 0);
 395
 396        /* As documented in the SDM: Do a CPUID 1 here */
 397        sync_core();
 398
 399        /* get the current revision from MSR 0x8B */
 400        native_rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
 401
 402        csig.rev = val[1];
 403
 404        uci->cpu_sig = csig;
 405        uci->valid = 1;
 406
 407        return 0;
 408}
 409
 410#ifdef DEBUG
 411static void __ref show_saved_mc(void)
 412{
 413        int i, j;
 414        unsigned int sig, pf, rev, total_size, data_size, date;
 415        struct ucode_cpu_info uci;
 416
 417        if (mc_saved_data.mc_saved_count == 0) {
 418                pr_debug("no micorcode data saved.\n");
 419                return;
 420        }
 421        pr_debug("Total microcode saved: %d\n", mc_saved_data.mc_saved_count);
 422
 423        collect_cpu_info_early(&uci);
 424
 425        sig = uci.cpu_sig.sig;
 426        pf = uci.cpu_sig.pf;
 427        rev = uci.cpu_sig.rev;
 428        pr_debug("CPU%d: sig=0x%x, pf=0x%x, rev=0x%x\n",
 429                 smp_processor_id(), sig, pf, rev);
 430
 431        for (i = 0; i < mc_saved_data.mc_saved_count; i++) {
 432                struct microcode_header_intel *mc_saved_header;
 433                struct extended_sigtable *ext_header;
 434                int ext_sigcount;
 435                struct extended_signature *ext_sig;
 436
 437                mc_saved_header = (struct microcode_header_intel *)
 438                                  mc_saved_data.mc_saved[i];
 439                sig = mc_saved_header->sig;
 440                pf = mc_saved_header->pf;
 441                rev = mc_saved_header->rev;
 442                total_size = get_totalsize(mc_saved_header);
 443                data_size = get_datasize(mc_saved_header);
 444                date = mc_saved_header->date;
 445
 446                pr_debug("mc_saved[%d]: sig=0x%x, pf=0x%x, rev=0x%x, toal size=0x%x, date = %04x-%02x-%02x\n",
 447                         i, sig, pf, rev, total_size,
 448                         date & 0xffff,
 449                         date >> 24,
 450                         (date >> 16) & 0xff);
 451
 452                /* Look for ext. headers: */
 453                if (total_size <= data_size + MC_HEADER_SIZE)
 454                        continue;
 455
 456                ext_header = (struct extended_sigtable *)
 457                             mc_saved_header + data_size + MC_HEADER_SIZE;
 458                ext_sigcount = ext_header->count;
 459                ext_sig = (void *)ext_header + EXT_HEADER_SIZE;
 460
 461                for (j = 0; j < ext_sigcount; j++) {
 462                        sig = ext_sig->sig;
 463                        pf = ext_sig->pf;
 464
 465                        pr_debug("\tExtended[%d]: sig=0x%x, pf=0x%x\n",
 466                                 j, sig, pf);
 467
 468                        ext_sig++;
 469                }
 470
 471        }
 472}
 473#else
 474static inline void show_saved_mc(void)
 475{
 476}
 477#endif
 478
 479#if defined(CONFIG_MICROCODE_INTEL_EARLY) && defined(CONFIG_HOTPLUG_CPU)
 480static DEFINE_MUTEX(x86_cpu_microcode_mutex);
 481/*
 482 * Save this mc into mc_saved_data. So it will be loaded early when a CPU is
 483 * hot added or resumes.
 484 *
 485 * Please make sure this mc should be a valid microcode patch before calling
 486 * this function.
 487 */
 488int save_mc_for_early(u8 *mc)
 489{
 490        struct microcode_intel *mc_saved_tmp[MAX_UCODE_COUNT];
 491        unsigned int mc_saved_count_init;
 492        unsigned int mc_saved_count;
 493        struct microcode_intel **mc_saved;
 494        int ret = 0;
 495        int i;
 496
 497        /*
 498         * Hold hotplug lock so mc_saved_data is not accessed by a CPU in
 499         * hotplug.
 500         */
 501        mutex_lock(&x86_cpu_microcode_mutex);
 502
 503        mc_saved_count_init = mc_saved_data.mc_saved_count;
 504        mc_saved_count = mc_saved_data.mc_saved_count;
 505        mc_saved = mc_saved_data.mc_saved;
 506
 507        if (mc_saved && mc_saved_count)
 508                memcpy(mc_saved_tmp, mc_saved,
 509                       mc_saved_count * sizeof(struct mirocode_intel *));
 510        /*
 511         * Save the microcode patch mc in mc_save_tmp structure if it's a newer
 512         * version.
 513         */
 514
 515        _save_mc(mc_saved_tmp, mc, &mc_saved_count);
 516
 517        /*
 518         * Save the mc_save_tmp in global mc_saved_data.
 519         */
 520        ret = save_microcode(&mc_saved_data, mc_saved_tmp, mc_saved_count);
 521        if (ret) {
 522                pr_err("Cannot save microcode patch.\n");
 523                goto out;
 524        }
 525
 526        show_saved_mc();
 527
 528        /*
 529         * Free old saved microcod data.
 530         */
 531        if (mc_saved) {
 532                for (i = 0; i < mc_saved_count_init; i++)
 533                        kfree(mc_saved[i]);
 534                kfree(mc_saved);
 535        }
 536
 537out:
 538        mutex_unlock(&x86_cpu_microcode_mutex);
 539
 540        return ret;
 541}
 542EXPORT_SYMBOL_GPL(save_mc_for_early);
 543#endif
 544
 545static __initdata char ucode_name[] = "kernel/x86/microcode/GenuineIntel.bin";
 546static __init enum ucode_state
 547scan_microcode(unsigned long start, unsigned long end,
 548                struct mc_saved_data *mc_saved_data,
 549                unsigned long *mc_saved_in_initrd,
 550                struct ucode_cpu_info *uci)
 551{
 552        unsigned int size = end - start + 1;
 553        struct cpio_data cd;
 554        long offset = 0;
 555#ifdef CONFIG_X86_32
 556        char *p = (char *)__pa_nodebug(ucode_name);
 557#else
 558        char *p = ucode_name;
 559#endif
 560
 561        cd.data = NULL;
 562        cd.size = 0;
 563
 564        cd = find_cpio_data(p, (void *)start, size, &offset);
 565        if (!cd.data)
 566                return UCODE_ERROR;
 567
 568
 569        return get_matching_model_microcode(0, start, cd.data, cd.size,
 570                                            mc_saved_data, mc_saved_in_initrd,
 571                                            uci);
 572}
 573
 574/*
 575 * Print ucode update info.
 576 */
 577static void
 578print_ucode_info(struct ucode_cpu_info *uci, unsigned int date)
 579{
 580        int cpu = smp_processor_id();
 581
 582        pr_info("CPU%d microcode updated early to revision 0x%x, date = %04x-%02x-%02x\n",
 583                cpu,
 584                uci->cpu_sig.rev,
 585                date & 0xffff,
 586                date >> 24,
 587                (date >> 16) & 0xff);
 588}
 589
 590#ifdef CONFIG_X86_32
 591
 592static int delay_ucode_info;
 593static int current_mc_date;
 594
 595/*
 596 * Print early updated ucode info after printk works. This is delayed info dump.
 597 */
 598void show_ucode_info_early(void)
 599{
 600        struct ucode_cpu_info uci;
 601
 602        if (delay_ucode_info) {
 603                collect_cpu_info_early(&uci);
 604                print_ucode_info(&uci, current_mc_date);
 605                delay_ucode_info = 0;
 606        }
 607}
 608
 609/*
 610 * At this point, we can not call printk() yet. Keep microcode patch number in
 611 * mc_saved_data.mc_saved and delay printing microcode info in
 612 * show_ucode_info_early() until printk() works.
 613 */
 614static void print_ucode(struct ucode_cpu_info *uci)
 615{
 616        struct microcode_intel *mc_intel;
 617        int *delay_ucode_info_p;
 618        int *current_mc_date_p;
 619
 620        mc_intel = uci->mc;
 621        if (mc_intel == NULL)
 622                return;
 623
 624        delay_ucode_info_p = (int *)__pa_nodebug(&delay_ucode_info);
 625        current_mc_date_p = (int *)__pa_nodebug(&current_mc_date);
 626
 627        *delay_ucode_info_p = 1;
 628        *current_mc_date_p = mc_intel->hdr.date;
 629}
 630#else
 631
 632/*
 633 * Flush global tlb. We only do this in x86_64 where paging has been enabled
 634 * already and PGE should be enabled as well.
 635 */
 636static inline void flush_tlb_early(void)
 637{
 638        __native_flush_tlb_global_irq_disabled();
 639}
 640
 641static inline void print_ucode(struct ucode_cpu_info *uci)
 642{
 643        struct microcode_intel *mc_intel;
 644
 645        mc_intel = uci->mc;
 646        if (mc_intel == NULL)
 647                return;
 648
 649        print_ucode_info(uci, mc_intel->hdr.date);
 650}
 651#endif
 652
 653static int apply_microcode_early(struct mc_saved_data *mc_saved_data,
 654                                 struct ucode_cpu_info *uci)
 655{
 656        struct microcode_intel *mc_intel;
 657        unsigned int val[2];
 658
 659        mc_intel = uci->mc;
 660        if (mc_intel == NULL)
 661                return 0;
 662
 663        /* write microcode via MSR 0x79 */
 664        native_wrmsr(MSR_IA32_UCODE_WRITE,
 665              (unsigned long) mc_intel->bits,
 666              (unsigned long) mc_intel->bits >> 16 >> 16);
 667        native_wrmsr(MSR_IA32_UCODE_REV, 0, 0);
 668
 669        /* As documented in the SDM: Do a CPUID 1 here */
 670        sync_core();
 671
 672        /* get the current revision from MSR 0x8B */
 673        native_rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
 674        if (val[1] != mc_intel->hdr.rev)
 675                return -1;
 676
 677#ifdef CONFIG_X86_64
 678        /* Flush global tlb. This is precaution. */
 679        flush_tlb_early();
 680#endif
 681        uci->cpu_sig.rev = val[1];
 682
 683        print_ucode(uci);
 684
 685        return 0;
 686}
 687
 688/*
 689 * This function converts microcode patch offsets previously stored in
 690 * mc_saved_in_initrd to pointers and stores the pointers in mc_saved_data.
 691 */
 692int __init save_microcode_in_initrd_intel(void)
 693{
 694        unsigned int count = mc_saved_data.mc_saved_count;
 695        struct microcode_intel *mc_saved[MAX_UCODE_COUNT];
 696        int ret = 0;
 697
 698        if (count == 0)
 699                return ret;
 700
 701        microcode_pointer(mc_saved, mc_saved_in_initrd, initrd_start, count);
 702        ret = save_microcode(&mc_saved_data, mc_saved, count);
 703        if (ret)
 704                pr_err("Cannot save microcode patches from initrd.\n");
 705
 706        show_saved_mc();
 707
 708        return ret;
 709}
 710
 711static void __init
 712_load_ucode_intel_bsp(struct mc_saved_data *mc_saved_data,
 713                      unsigned long *mc_saved_in_initrd,
 714                      unsigned long initrd_start_early,
 715                      unsigned long initrd_end_early,
 716                      struct ucode_cpu_info *uci)
 717{
 718        collect_cpu_info_early(uci);
 719        scan_microcode(initrd_start_early, initrd_end_early, mc_saved_data,
 720                       mc_saved_in_initrd, uci);
 721        load_microcode(mc_saved_data, mc_saved_in_initrd,
 722                       initrd_start_early, uci);
 723        apply_microcode_early(mc_saved_data, uci);
 724}
 725
 726void __init
 727load_ucode_intel_bsp(void)
 728{
 729        u64 ramdisk_image, ramdisk_size;
 730        unsigned long initrd_start_early, initrd_end_early;
 731        struct ucode_cpu_info uci;
 732#ifdef CONFIG_X86_32
 733        struct boot_params *boot_params_p;
 734
 735        boot_params_p = (struct boot_params *)__pa_nodebug(&boot_params);
 736        ramdisk_image = boot_params_p->hdr.ramdisk_image;
 737        ramdisk_size  = boot_params_p->hdr.ramdisk_size;
 738        initrd_start_early = ramdisk_image;
 739        initrd_end_early = initrd_start_early + ramdisk_size;
 740
 741        _load_ucode_intel_bsp(
 742                (struct mc_saved_data *)__pa_nodebug(&mc_saved_data),
 743                (unsigned long *)__pa_nodebug(&mc_saved_in_initrd),
 744                initrd_start_early, initrd_end_early, &uci);
 745#else
 746        ramdisk_image = boot_params.hdr.ramdisk_image;
 747        ramdisk_size  = boot_params.hdr.ramdisk_size;
 748        initrd_start_early = ramdisk_image + PAGE_OFFSET;
 749        initrd_end_early = initrd_start_early + ramdisk_size;
 750
 751        _load_ucode_intel_bsp(&mc_saved_data, mc_saved_in_initrd,
 752                              initrd_start_early, initrd_end_early, &uci);
 753#endif
 754}
 755
 756void load_ucode_intel_ap(void)
 757{
 758        struct mc_saved_data *mc_saved_data_p;
 759        struct ucode_cpu_info uci;
 760        unsigned long *mc_saved_in_initrd_p;
 761        unsigned long initrd_start_addr;
 762#ifdef CONFIG_X86_32
 763        unsigned long *initrd_start_p;
 764
 765        mc_saved_in_initrd_p =
 766                (unsigned long *)__pa_nodebug(mc_saved_in_initrd);
 767        mc_saved_data_p = (struct mc_saved_data *)__pa_nodebug(&mc_saved_data);
 768        initrd_start_p = (unsigned long *)__pa_nodebug(&initrd_start);
 769        initrd_start_addr = (unsigned long)__pa_nodebug(*initrd_start_p);
 770#else
 771        mc_saved_data_p = &mc_saved_data;
 772        mc_saved_in_initrd_p = mc_saved_in_initrd;
 773        initrd_start_addr = initrd_start;
 774#endif
 775
 776        /*
 777         * If there is no valid ucode previously saved in memory, no need to
 778         * update ucode on this AP.
 779         */
 780        if (mc_saved_data_p->mc_saved_count == 0)
 781                return;
 782
 783        collect_cpu_info_early(&uci);
 784        load_microcode(mc_saved_data_p, mc_saved_in_initrd_p,
 785                       initrd_start_addr, &uci);
 786        apply_microcode_early(mc_saved_data_p, &uci);
 787}
 788