Skip to content

Commit 037e81f

Browse files
committed
x86/microcode/AMD: Add get_patch_level()
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
1 parent b39c387 commit 037e81f

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;
@@ -563,11 +571,12 @@ void __init load_ucode_amd_bsp(struct early_load_data *ed, unsigned int cpuid_1_
563571
struct cont_desc desc = { };
564572
struct microcode_amd *mc;
565573
struct cpio_data cp = { };
566-
u32 dummy;
574+
u32 rev;
567575

568576
bsp_cpuid_1_eax = cpuid_1_eax;
569577

570-
native_rdmsr(MSR_AMD64_PATCH_LEVEL, ed->old_rev, dummy);
578+
rev = get_patch_level();
579+
ed->old_rev = rev;
571580

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

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

596605
static inline bool patch_cpus_equivalent(struct ucode_patch *p,
@@ -692,14 +701,9 @@ static void free_cache(void)
692701
static struct ucode_patch *find_patch(unsigned int cpu)
693702
{
694703
struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
695-
u32 rev, dummy __always_unused;
696704
u16 equiv_id = 0;
697705

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

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

723727
mc = p->data;
724728

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

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

739741
csig->sig = cpuid_eax(0x00000001);
740-
csig->rev = c->microcode;
742+
csig->rev = get_patch_level();
741743

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

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

0 commit comments

Comments
 (0)