Skip to content

Commit 0c110da

Browse files
bp3tk0vgregkh
authored andcommitted
x86/microcode/AMD: Add get_patch_level()
commit 037e81f upstream. Put the MSR_AMD64_PATCH_LEVEL reading of the current microcode revision the hw has, into a separate function. Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de> Reviewed-by: Thomas Gleixner <tglx@linutronix.de> Link: https://lore.kernel.org/r/20250211163648.30531-6-bp@kernel.org Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 5c20003 commit 0c110da

File tree

1 file changed

+24
-22
lines changed
  • arch/x86/kernel/cpu/microcode

1 file changed

+24
-22
lines changed

arch/x86/kernel/cpu/microcode/amd.c

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,15 @@ ucode_path[] __maybe_unused = "kernel/x86/microcode/AuthenticAMD.bin";
145145
*/
146146
static u32 bsp_cpuid_1_eax __ro_after_init;
147147

148+
static u32 get_patch_level(void)
149+
{
150+
u32 rev, dummy __always_unused;
151+
152+
native_rdmsr(MSR_AMD64_PATCH_LEVEL, rev, dummy);
153+
154+
return rev;
155+
}
156+
148157
static union cpuid_1_eax ucode_rev_to_cpuid(unsigned int val)
149158
{
150159
union zen_patch_rev p;
@@ -483,10 +492,10 @@ static void scan_containers(u8 *ucode, size_t size, struct cont_desc *desc)
483492
}
484493
}
485494

486-
static bool __apply_microcode_amd(struct microcode_amd *mc, unsigned int psize)
495+
static bool __apply_microcode_amd(struct microcode_amd *mc, u32 *cur_rev,
496+
unsigned int psize)
487497
{
488498
unsigned long p_addr = (unsigned long)&mc->hdr.data_code;
489-
u32 rev, dummy;
490499

491500
native_wrmsrl(MSR_AMD64_PATCH_LOADER, p_addr);
492501

@@ -504,9 +513,8 @@ static bool __apply_microcode_amd(struct microcode_amd *mc, unsigned int psize)
504513
}
505514

506515
/* verify patch application was successful */
507-
native_rdmsr(MSR_AMD64_PATCH_LEVEL, rev, dummy);
508-
509-
if (rev != mc->hdr.patch_id)
516+
*cur_rev = get_patch_level();
517+
if (*cur_rev != mc->hdr.patch_id)
510518
return false;
511519

512520
return true;
@@ -564,11 +572,12 @@ void __init load_ucode_amd_bsp(struct early_load_data *ed, unsigned int cpuid_1_
564572
struct cont_desc desc = { };
565573
struct microcode_amd *mc;
566574
struct cpio_data cp = { };
567-
u32 dummy;
575+
u32 rev;
568576

569577
bsp_cpuid_1_eax = cpuid_1_eax;
570578

571-
native_rdmsr(MSR_AMD64_PATCH_LEVEL, ed->old_rev, dummy);
579+
rev = get_patch_level();
580+
ed->old_rev = rev;
572581

573582
/* Needed in load_microcode_amd() */
574583
ucode_cpu_info[0].cpu_sig.sig = cpuid_1_eax;
@@ -590,8 +599,8 @@ void __init load_ucode_amd_bsp(struct early_load_data *ed, unsigned int cpuid_1_
590599
if (ed->old_rev > mc->hdr.patch_id)
591600
return;
592601

593-
if (__apply_microcode_amd(mc, desc.psize))
594-
native_rdmsr(MSR_AMD64_PATCH_LEVEL, ed->new_rev, dummy);
602+
if (__apply_microcode_amd(mc, &rev, desc.psize))
603+
ed->new_rev = rev;
595604
}
596605

597606
static inline bool patch_cpus_equivalent(struct ucode_patch *p,
@@ -693,14 +702,9 @@ static void free_cache(void)
693702
static struct ucode_patch *find_patch(unsigned int cpu)
694703
{
695704
struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
696-
u32 rev, dummy __always_unused;
697705
u16 equiv_id = 0;
698706

699-
/* fetch rev if not populated yet: */
700-
if (!uci->cpu_sig.rev) {
701-
rdmsr(MSR_AMD64_PATCH_LEVEL, rev, dummy);
702-
uci->cpu_sig.rev = rev;
703-
}
707+
uci->cpu_sig.rev = get_patch_level();
704708

705709
if (x86_family(bsp_cpuid_1_eax) < 0x17) {
706710
equiv_id = find_equiv_id(&equiv_table, uci->cpu_sig.sig);
@@ -723,22 +727,20 @@ void reload_ucode_amd(unsigned int cpu)
723727

724728
mc = p->data;
725729

726-
rdmsr(MSR_AMD64_PATCH_LEVEL, rev, dummy);
727-
730+
rev = get_patch_level();
728731
if (rev < mc->hdr.patch_id) {
729-
if (__apply_microcode_amd(mc, p->size))
730-
pr_info_once("reload revision: 0x%08x\n", mc->hdr.patch_id);
732+
if (__apply_microcode_amd(mc, &rev, p->size))
733+
pr_info_once("reload revision: 0x%08x\n", rev);
731734
}
732735
}
733736

734737
static int collect_cpu_info_amd(int cpu, struct cpu_signature *csig)
735738
{
736-
struct cpuinfo_x86 *c = &cpu_data(cpu);
737739
struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
738740
struct ucode_patch *p;
739741

740742
csig->sig = cpuid_eax(0x00000001);
741-
csig->rev = c->microcode;
743+
csig->rev = get_patch_level();
742744

743745
/*
744746
* a patch could have been loaded early, set uci->mc so that
@@ -779,7 +781,7 @@ static enum ucode_state apply_microcode_amd(int cpu)
779781
goto out;
780782
}
781783

782-
if (!__apply_microcode_amd(mc_amd, p->size)) {
784+
if (!__apply_microcode_amd(mc_amd, &rev, p->size)) {
783785
pr_err("CPU%d: update failed for patch_level=0x%08x\n",
784786
cpu, mc_amd->hdr.patch_id);
785787
return UCODE_ERROR;

0 commit comments

Comments
 (0)