x86/microcode/AMD: Some small cleanups
Subsystems (2)
cover x86/microcode/AMD: Some small cleanups
From: "Borislav Petkov (AMD)" <[email protected]>
A bunch of small and trivial cleanups which sprang up from recent staring at the loader.
No functional changes.
Thx.
Borislav Petkov (AMD) (5):
x86/microcode/AMD: Remove ugly linebreak in __verify_patch_section()
signature
x86/microcode/AMD: Remove unused save_microcode_in_initrd_amd()
declarations
x86/microcode/AMD: Merge early_apply_microcode() into its single
callsite
x86/microcode/AMD: Get rid of the _load_microcode_amd() forward
declaration
x86/microcode/AMD: Add get_patch_level()
arch/x86/kernel/cpu/microcode/amd.c | 155 +++++++++++------------ arch/x86/kernel/cpu/microcode/internal.h | 2 - 2 files changed, 73 insertions(+), 84 deletions(-)
Signature · 1 line
2.43.0
PATCH 1/5 x86/microcode/AMD: Remove ugly linebreak in __verify_patch_section() signature
From: "Borislav Petkov (AMD)" <[email protected]>
No functional changes.
--- arch/x86/kernel/cpu/microcode/amd.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
arch/x86/kernel/cpu/microcode/amd.c +1 -2
@@ -246,8 +246,7 @@ static bool verify_equivalence_table(const u8 *buf, size_t buf_size)* On success, @sh_psize returns the patch size according to the section header, * to the caller. */ -static bool -__verify_patch_section (const u8 *buf,size_t buf_size,u32 *sh_psize) +static bool __verify_patch_section (const u8 *buf,size_t buf_size,u32 *sh_psize) {u32 p_type, p_size;const u32 *hdr;
Signature · 1 line
2.43.0
[tip: x86/microcode] x86/microcode/AMD: Remove ugly linebreak in __verify_patch_section() signature
The following commit has been merged into the x86/microcode branch of tip:
The following commit has been merged into the x86/microcode branch of tip:
Commit-ID: 7103f0589ac220eac3d2b1e8411494b31b883d06 Gitweb: https://git.kernel.org/tip/7103f0589ac220eac3d2b1e8411494b31b883d06 Author: Borislav Petkov (AMD) <[email protected]> AuthorDate: Thu, 23 Jan 2025 13:14:34 +01:00 Committer: Borislav Petkov (AMD) <[email protected]> CommitterDate: Mon, 17 Feb 2025 09:42:13 +01:00
x86/microcode/AMD: Remove ugly linebreak in __verify_patch_section() signature
No functional changes.
--- arch/x86/kernel/cpu/microcode/amd.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
arch/x86/kernel/cpu/microcode/amd.c +1 -2
@@ -246,8 +246,7 @@ static bool verify_equivalence_table(const u8 *buf, size_t buf_size)* On success, @sh_psize returns the patch size according to the section header, * to the caller. */ -static bool -__verify_patch_section (const u8 *buf,size_t buf_size,u32 *sh_psize) +static bool __verify_patch_section (const u8 *buf,size_t buf_size,u32 *sh_psize) {u32 p_type, p_size;const u32 *hdr;
PATCH 2/5 x86/microcode/AMD: Remove unused save_microcode_in_initrd_amd() declarations
In reply to Borislav PetkovFrom: "Borislav Petkov (AMD)" <[email protected]>
Commit
a7939f016720 ("x86/microcode/amd: Cache builtin/initrd microcode early")
renamed it to save_microcode_in_initrd() and made it static. Zap the forgotten declarations.
No functional changes.
--- arch/x86/kernel/cpu/microcode/amd.c | 2 +- arch/x86/kernel/cpu/microcode/internal.h | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-)
arch/x86/kernel/cpu/microcode/amd.c +1 -1
@@ -517,7 +517,7 @@ static bool __apply_microcode_amd(struct microcode_amd *mc, unsigned int psize)* patch container file in initrd, traverse equivalent cpu table, look for a * matching microcode patch, and update, all in initrd memory in place. * When vmalloc() is available for use later -- on 64-bit during first AP load, -* and on 32-bit during save_microcode_in_initrd_amd() -- we can call +* and on 32-bit during save_microcode_in_initrd() -- we can call * load_microcode_amd() to save equivalent cpu table and microcode patches in * kernel heap memory. *
arch/x86/kernel/cpu/microcode/internal.h +0 -2
@@ -100,14 +100,12 @@ extern bool force_minrev;#ifdef CONFIG_CPU_SUP_AMD void load_ucode_amd_bsp (struct early_load_data *ed,unsigned int family);void load_ucode_amd_ap (unsigned int family); -int save_microcode_in_initrd_amd (unsigned int family);void reload_ucode_amd (unsigned int cpu);struct microcode_ops *init_amd_microcode (void );void exit_amd_microcode (void );#else /* CONFIG_CPU_SUP_AMD */ static inline void load_ucode_amd_bsp (struct early_load_data *ed,unsigned int family) { }static inline void load_ucode_amd_ap (unsigned int family) { } -static inline int save_microcode_in_initrd_amd (unsigned int family) {return -EINVAL ; }static inline void reload_ucode_amd (unsigned int cpu) { }static inline struct microcode_ops *init_amd_microcode (void ) {return NULL ; }static inline void exit_amd_microcode (void ) { }
Signature · 1 line
2.43.0
[tip: x86/microcode] x86/microcode/AMD: Remove unused save_microcode_in_initrd_amd() declarations
The following commit has been merged into the x86/microcode branch of tip:
The following commit has been merged into the x86/microcode branch of tip:
Commit-ID: 3ef0740d10b005a45e8ae5b4b7b5d37bfddf63c0 Gitweb: https://git.kernel.org/tip/3ef0740d10b005a45e8ae5b4b7b5d37bfddf63c0 Author: Borislav Petkov (AMD) <[email protected]> AuthorDate: Thu, 23 Jan 2025 12:23:47 +01:00 Committer: Borislav Petkov (AMD) <[email protected]> CommitterDate: Mon, 17 Feb 2025 09:42:31 +01:00
x86/microcode/AMD: Remove unused save_microcode_in_initrd_amd() declarations
Commit
a7939f016720 ("x86/microcode/amd: Cache builtin/initrd microcode early")
renamed it to save_microcode_in_initrd() and made it static. Zap the forgotten declarations.
No functional changes.
--- arch/x86/kernel/cpu/microcode/amd.c | 2 +- arch/x86/kernel/cpu/microcode/internal.h | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-)
arch/x86/kernel/cpu/microcode/amd.c +1 -1
@@ -517,7 +517,7 @@ static bool __apply_microcode_amd(struct microcode_amd *mc, unsigned int psize)* patch container file in initrd, traverse equivalent cpu table, look for a * matching microcode patch, and update, all in initrd memory in place. * When vmalloc() is available for use later -- on 64-bit during first AP load, -* and on 32-bit during save_microcode_in_initrd_amd() -- we can call +* and on 32-bit during save_microcode_in_initrd() -- we can call * load_microcode_amd() to save equivalent cpu table and microcode patches in * kernel heap memory. *
arch/x86/kernel/cpu/microcode/internal.h +0 -2
@@ -100,14 +100,12 @@ extern bool force_minrev;#ifdef CONFIG_CPU_SUP_AMD void load_ucode_amd_bsp (struct early_load_data *ed,unsigned int family);void load_ucode_amd_ap (unsigned int family); -int save_microcode_in_initrd_amd (unsigned int family);void reload_ucode_amd (unsigned int cpu);struct microcode_ops *init_amd_microcode (void );void exit_amd_microcode (void );#else /* CONFIG_CPU_SUP_AMD */ static inline void load_ucode_amd_bsp (struct early_load_data *ed,unsigned int family) { }static inline void load_ucode_amd_ap (unsigned int family) { } -static inline int save_microcode_in_initrd_amd (unsigned int family) {return -EINVAL ; }static inline void reload_ucode_amd (unsigned int cpu) { }static inline struct microcode_ops *init_amd_microcode (void ) {return NULL ; }static inline void exit_amd_microcode (void ) { }
PATCH 3/5 x86/microcode/AMD: Merge early_apply_microcode() into its single callsite
In reply to Borislav PetkovFrom: "Borislav Petkov (AMD)" <[email protected]>
No functional changes.
--- arch/x86/kernel/cpu/microcode/amd.c | 60 +++++++++++++---------------- 1 file changed, 26 insertions(+), 34 deletions(-)
arch/x86/kernel/cpu/microcode/amd.c +26 -34
@@ -512,39 +512,6 @@ static bool __apply_microcode_amd(struct microcode_amd *mc, unsigned int psize)return true; } -/* -* Early load occurs before we can vmalloc(). So we look for the microcode -* patch container file in initrd, traverse equivalent cpu table, look for a -* matching microcode patch, and update, all in initrd memory in place. -* When vmalloc() is available for use later -- on 64-bit during first AP load, -* and on 32-bit during save_microcode_in_initrd() -- we can call -* load_microcode_amd() to save equivalent cpu table and microcode patches in -* kernel heap memory. -* -* Returns true if container found (sets @desc), false otherwise. -*/ -static bool early_apply_microcode (u32 old_rev,void *ucode,size_t size) -{ -struct cont_desc desc = {0 }; -struct microcode_amd *mc; - -scan_containers (ucode, size, &desc); - - mc = desc.mc; -if (!mc) -return false; - -/* -* Allow application of the same revision to pick up SMT-specific -* changes even if the revision of the other SMT thread is already -* up-to-date. -*/ -if (old_rev > mc->hdr.patch_id) -return false; - -return __apply_microcode_amd (mc, desc.psize); -} -static bool get_builtin_microcode (struct cpio_data *cp) {char fw_name[36 ] ="amd-ucode/microcode_amd.bin" ;
@@ -582,8 +549,19 @@ static bool __init find_blobs_in_containers(struct cpio_data *ret)return found; } +/* +* Early load occurs before we can vmalloc(). So we look for the microcode +* patch container file in initrd, traverse equivalent cpu table, look for a +* matching microcode patch, and update, all in initrd memory in place. +* When vmalloc() is available for use later -- on 64-bit during first AP load, +* and on 32-bit during save_microcode_in_initrd() -- we can call +* load_microcode_amd() to save equivalent cpu table and microcode patches in +* kernel heap memory. +*/ void __init load_ucode_amd_bsp (struct early_load_data *ed,unsigned int cpuid_1_eax) { +struct cont_desc desc = { }; +struct microcode_amd *mc;struct cpio_data cp = { };u32 dummy;
@@ -597,7 +575,21 @@ void __init load_ucode_amd_bsp(struct early_load_data *ed, unsigned int cpuid_1_if (!find_blobs_in_containers (&cp))return ; -if (early_apply_microcode (ed->old_rev, cp.data, cp.size)) +scan_containers (cp.data, cp.size, &desc); + + mc = desc.mc; +if (!mc) +return ; + +/* +* Allow application of the same revision to pick up SMT-specific +* changes even if the revision of the other SMT thread is already +* up-to-date. +*/ +if (ed->old_rev > mc->hdr.patch_id) +return ; + +if (__apply_microcode_amd (mc, desc.psize))native_rdmsr (MSR_AMD64_PATCH_LEVEL , ed->new_rev, dummy); }
Signature · 1 line
2.43.0
[tip: x86/microcode] x86/microcode/AMD: Merge early_apply_microcode() into its single callsite
The following commit has been merged into the x86/microcode branch of tip:
The following commit has been merged into the x86/microcode branch of tip:
Commit-ID: dc15675074dcfd79a2f10a6e39f96b0244961a01 Gitweb: https://git.kernel.org/tip/dc15675074dcfd79a2f10a6e39f96b0244961a01 Author: Borislav Petkov (AMD) <[email protected]> AuthorDate: Thu, 23 Jan 2025 12:46:45 +01:00 Committer: Borislav Petkov (AMD) <[email protected]> CommitterDate: Mon, 17 Feb 2025 09:42:34 +01:00
x86/microcode/AMD: Merge early_apply_microcode() into its single callsite
No functional changes.
--- arch/x86/kernel/cpu/microcode/amd.c | 60 ++++++++++++---------------- 1 file changed, 26 insertions(+), 34 deletions(-)
arch/x86/kernel/cpu/microcode/amd.c +26 -34
@@ -512,39 +512,6 @@ static bool __apply_microcode_amd(struct microcode_amd *mc, unsigned int psize)return true; } -/* -* Early load occurs before we can vmalloc(). So we look for the microcode -* patch container file in initrd, traverse equivalent cpu table, look for a -* matching microcode patch, and update, all in initrd memory in place. -* When vmalloc() is available for use later -- on 64-bit during first AP load, -* and on 32-bit during save_microcode_in_initrd() -- we can call -* load_microcode_amd() to save equivalent cpu table and microcode patches in -* kernel heap memory. -* -* Returns true if container found (sets @desc), false otherwise. -*/ -static bool early_apply_microcode (u32 old_rev,void *ucode,size_t size) -{ -struct cont_desc desc = {0 }; -struct microcode_amd *mc; - -scan_containers (ucode, size, &desc); - - mc = desc.mc; -if (!mc) -return false; - -/* -* Allow application of the same revision to pick up SMT-specific -* changes even if the revision of the other SMT thread is already -* up-to-date. -*/ -if (old_rev > mc->hdr.patch_id) -return false; - -return __apply_microcode_amd (mc, desc.psize); -} -static bool get_builtin_microcode (struct cpio_data *cp) {char fw_name[36 ] ="amd-ucode/microcode_amd.bin" ;
@@ -582,8 +549,19 @@ static bool __init find_blobs_in_containers(struct cpio_data *ret)return found; } +/* +* Early load occurs before we can vmalloc(). So we look for the microcode +* patch container file in initrd, traverse equivalent cpu table, look for a +* matching microcode patch, and update, all in initrd memory in place. +* When vmalloc() is available for use later -- on 64-bit during first AP load, +* and on 32-bit during save_microcode_in_initrd() -- we can call +* load_microcode_amd() to save equivalent cpu table and microcode patches in +* kernel heap memory. +*/ void __init load_ucode_amd_bsp (struct early_load_data *ed,unsigned int cpuid_1_eax) { +struct cont_desc desc = { }; +struct microcode_amd *mc;struct cpio_data cp = { };u32 dummy;
@@ -597,7 +575,21 @@ void __init load_ucode_amd_bsp(struct early_load_data *ed, unsigned int cpuid_1_if (!find_blobs_in_containers (&cp))return ; -if (early_apply_microcode (ed->old_rev, cp.data, cp.size)) +scan_containers (cp.data, cp.size, &desc); + + mc = desc.mc; +if (!mc) +return ; + +/* +* Allow application of the same revision to pick up SMT-specific +* changes even if the revision of the other SMT thread is already +* up-to-date. +*/ +if (ed->old_rev > mc->hdr.patch_id) +return ; + +if (__apply_microcode_amd (mc, desc.psize))native_rdmsr (MSR_AMD64_PATCH_LEVEL , ed->new_rev, dummy); }
PATCH 4/5 x86/microcode/AMD: Get rid of the _load_microcode_amd() forward declaration
In reply to Borislav PetkovFrom: "Borislav Petkov (AMD)" <[email protected]>
Simply move save_microcode_in_initrd() down.
No functional changes.
--- arch/x86/kernel/cpu/microcode/amd.c | 54 ++++++++++++++--------------- 1 file changed, 26 insertions(+), 28 deletions(-)
arch/x86/kernel/cpu/microcode/amd.c +26 -28
@@ -593,34 +593,6 @@ void __init load_ucode_amd_bsp(struct early_load_data *ed, unsigned int cpuid_1_native_rdmsr (MSR_AMD64_PATCH_LEVEL , ed->new_rev, dummy); } -static enum ucode_state _load_microcode_amd (u8 family,const u8 *data,size_t size); - -static int __init save_microcode_in_initrd (void ) -{ -unsigned int cpuid_1_eax =native_cpuid_eax (1 ); -struct cpuinfo_x86 *c = &boot_cpu_data; -struct cont_desc desc = {0 }; -enum ucode_state ret; -struct cpio_data cp; - -if (dis_ucode_ldr || c->x86_vendor !=X86_VENDOR_AMD || c->x86 <0x10 ) -return 0 ; - -if (!find_blobs_in_containers (&cp)) -return -EINVAL ; - -scan_containers (cp.data, cp.size, &desc); -if (!desc.mc) -return -EINVAL ; - - ret =_load_microcode_amd (x86_family (cpuid_1_eax), desc.data, desc.size); -if (ret >UCODE_UPDATED ) -return -EINVAL ; - -return 0 ; -} -early_initcall (save_microcode_in_initrd); -static inline bool patch_cpus_equivalent (struct ucode_patch *p,struct ucode_patch *n,bool ignore_stepping)
@@ -1004,6 +976,32 @@ static enum ucode_state load_microcode_amd(u8 family, const u8 *data, size_t sizreturn ret; } +static int __init save_microcode_in_initrd (void ) +{ +unsigned int cpuid_1_eax =native_cpuid_eax (1 ); +struct cpuinfo_x86 *c = &boot_cpu_data; +struct cont_desc desc = {0 }; +enum ucode_state ret; +struct cpio_data cp; + +if (dis_ucode_ldr || c->x86_vendor !=X86_VENDOR_AMD || c->x86 <0x10 ) +return 0 ; + +if (!find_blobs_in_containers (&cp)) +return -EINVAL ; + +scan_containers (cp.data, cp.size, &desc); +if (!desc.mc) +return -EINVAL ; + + ret =_load_microcode_amd (x86_family (cpuid_1_eax), desc.data, desc.size); +if (ret >UCODE_UPDATED ) +return -EINVAL ; + +return 0 ; +} +early_initcall (save_microcode_in_initrd); +/* * AMD microcode firmware naming convention, up to family 15h they are in * the legacy file:
Signature · 1 line
2.43.0
[tip: x86/microcode] x86/microcode/AMD: Get rid of the _load_microcode_amd() forward declaration
The following commit has been merged into the x86/microcode branch of tip:
The following commit has been merged into the x86/microcode branch of tip:
Commit-ID: b39c387164879eef71886fc93cee5ca7dd7bf500 Gitweb: https://git.kernel.org/tip/b39c387164879eef71886fc93cee5ca7dd7bf500 Author: Borislav Petkov (AMD) <[email protected]> AuthorDate: Thu, 23 Jan 2025 12:51:37 +01:00 Committer: Borislav Petkov (AMD) <[email protected]> CommitterDate: Mon, 17 Feb 2025 09:42:37 +01:00
x86/microcode/AMD: Get rid of the _load_microcode_amd() forward declaration
Simply move save_microcode_in_initrd() down.
No functional changes.
--- arch/x86/kernel/cpu/microcode/amd.c | 54 +++++++++++++--------------- 1 file changed, 26 insertions(+), 28 deletions(-)
arch/x86/kernel/cpu/microcode/amd.c +26 -28
@@ -593,34 +593,6 @@ void __init load_ucode_amd_bsp(struct early_load_data *ed, unsigned int cpuid_1_native_rdmsr (MSR_AMD64_PATCH_LEVEL , ed->new_rev, dummy); } -static enum ucode_state _load_microcode_amd (u8 family,const u8 *data,size_t size); - -static int __init save_microcode_in_initrd (void ) -{ -unsigned int cpuid_1_eax =native_cpuid_eax (1 ); -struct cpuinfo_x86 *c = &boot_cpu_data; -struct cont_desc desc = {0 }; -enum ucode_state ret; -struct cpio_data cp; - -if (dis_ucode_ldr || c->x86_vendor !=X86_VENDOR_AMD || c->x86 <0x10 ) -return 0 ; - -if (!find_blobs_in_containers (&cp)) -return -EINVAL ; - -scan_containers (cp.data, cp.size, &desc); -if (!desc.mc) -return -EINVAL ; - - ret =_load_microcode_amd (x86_family (cpuid_1_eax), desc.data, desc.size); -if (ret >UCODE_UPDATED ) -return -EINVAL ; - -return 0 ; -} -early_initcall (save_microcode_in_initrd); -static inline bool patch_cpus_equivalent (struct ucode_patch *p,struct ucode_patch *n,bool ignore_stepping)
@@ -1004,6 +976,32 @@ static enum ucode_state load_microcode_amd(u8 family, const u8 *data, size_t sizreturn ret; } +static int __init save_microcode_in_initrd (void ) +{ +unsigned int cpuid_1_eax =native_cpuid_eax (1 ); +struct cpuinfo_x86 *c = &boot_cpu_data; +struct cont_desc desc = {0 }; +enum ucode_state ret; +struct cpio_data cp; + +if (dis_ucode_ldr || c->x86_vendor !=X86_VENDOR_AMD || c->x86 <0x10 ) +return 0 ; + +if (!find_blobs_in_containers (&cp)) +return -EINVAL ; + +scan_containers (cp.data, cp.size, &desc); +if (!desc.mc) +return -EINVAL ; + + ret =_load_microcode_amd (x86_family (cpuid_1_eax), desc.data, desc.size); +if (ret >UCODE_UPDATED ) +return -EINVAL ; + +return 0 ; +} +early_initcall (save_microcode_in_initrd); +/* * AMD microcode firmware naming convention, up to family 15h they are in * the legacy file:
PATCH 5/5 x86/microcode/AMD: Add get_patch_level()
In reply to Borislav PetkovFrom: "Borislav Petkov (AMD)" <[email protected]>
Put the MSR_AMD64_PATCH_LEVEL reading of the current microcode revision the hw has, into a separate function.
--- arch/x86/kernel/cpu/microcode/amd.c | 46 +++++++++++++++-------------- 1 file changed, 24 insertions(+), 22 deletions(-)
arch/x86/kernel/cpu/microcode/amd.c +24 -22
@@ -145,6 +145,15 @@ ucode_path[] __maybe_unused = "kernel/x86/microcode/AuthenticAMD.bin";*/ static u32 bsp_cpuid_1_eax__ro_after_init ; +static u32 get_patch_level (void ) +{ +u32 rev, dummy__always_unused ; + +native_rdmsr (MSR_AMD64_PATCH_LEVEL , rev, dummy); + +return rev; +} +static union cpuid_1_eax ucode_rev_to_cpuid (unsigned int val) {union zen_patch_rev p;
@@ -483,10 +492,10 @@ static void scan_containers(u8 *ucode, size_t size, struct cont_desc *desc) } } -static bool __apply_microcode_amd (struct microcode_amd *mc,unsigned int psize) +static bool __apply_microcode_amd (struct microcode_amd *mc,u32 *cur_rev, +unsigned int psize) {unsigned long p_addr = (unsigned long )&mc->hdr.data_code; -u32 rev, dummy;native_wrmsrl (MSR_AMD64_PATCH_LOADER , p_addr);
@@ -504,9 +513,8 @@ static bool __apply_microcode_amd(struct microcode_amd *mc, unsigned int psize) }/* verify patch application was successful */ -native_rdmsr (MSR_AMD64_PATCH_LEVEL , rev, dummy); - -if (rev != mc->hdr.patch_id) + *cur_rev =get_patch_level (); +if (*cur_rev != mc->hdr.patch_id)return false;return true;
@@ -563,11 +571,12 @@ void __init load_ucode_amd_bsp(struct early_load_data *ed, unsigned int cpuid_1_struct cont_desc desc = { };struct microcode_amd *mc;struct cpio_data cp = { }; -u32 dummy; +u32 rev; bsp_cpuid_1_eax = cpuid_1_eax; -native_rdmsr (MSR_AMD64_PATCH_LEVEL , ed->old_rev, dummy); + rev =get_patch_level (); + ed->old_rev = rev;/* Needed in load_microcode_amd() */ 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_if (ed->old_rev > mc->hdr.patch_id)return ; -if (__apply_microcode_amd (mc, desc.psize)) -native_rdmsr (MSR_AMD64_PATCH_LEVEL , ed->new_rev, dummy); +if (__apply_microcode_amd (mc, &rev, desc.psize)) + ed->new_rev = rev; } static inline bool patch_cpus_equivalent(struct ucode_patch *p,
@@ -692,14 +701,9 @@ static void free_cache(void)static struct ucode_patch *find_patch (unsigned int cpu) {struct ucode_cpu_info *uci = ucode_cpu_info + cpu; -u32 rev, dummy__always_unused ;u16 equiv_id =0 ; -/* fetch rev if not populated yet: */ -if (!uci->cpu_sig.rev) { -rdmsr (MSR_AMD64_PATCH_LEVEL , rev, dummy); - uci->cpu_sig.rev = rev; - } + uci->cpu_sig.rev =get_patch_level ();if (x86_family (bsp_cpuid_1_eax) <0x17 ) { equiv_id =find_equiv_id (&equiv_table, uci->cpu_sig.sig);
@@ -722,22 +726,20 @@ void reload_ucode_amd(unsigned int cpu) mc = p->data; -rdmsr (MSR_AMD64_PATCH_LEVEL , rev, dummy); - + rev =get_patch_level ();if (rev < mc->hdr.patch_id) { -if (__apply_microcode_amd (mc, p->size)) -pr_info_once ("reload revision: 0x%08x\n" , mc->hdr.patch_id); +if (__apply_microcode_amd (mc, &rev, p->size)) +pr_info_once ("reload revision: 0x%08x\n" , rev); } }static int collect_cpu_info_amd (int cpu,struct cpu_signature *csig) { -struct cpuinfo_x86 *c = &cpu_data (cpu);struct ucode_cpu_info *uci = ucode_cpu_info + cpu;struct ucode_patch *p; csig->sig =cpuid_eax (0x00000001 ); - csig->rev = c->microcode; + csig->rev =get_patch_level ();/* * 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) goto out; } -if (!__apply_microcode_amd (mc_amd, p->size)) { +if (!__apply_microcode_amd (mc_amd, &rev, p->size)) {pr_err ("CPU%d: update failed for patch_level=0x%08x\n" , cpu, mc_amd->hdr.patch_id);return UCODE_ERROR ;
Signature · 1 line
2.43.0
[tip: x86/microcode] x86/microcode/AMD: Add get_patch_level()
The following commit has been merged into the x86/microcode branch of tip:
The following commit has been merged into the x86/microcode branch of tip:
Commit-ID: 037e81fb9d2dfe7b31fd97e5f578854e38f09887 Gitweb: https://git.kernel.org/tip/037e81fb9d2dfe7b31fd97e5f578854e38f09887 Author: Borislav Petkov (AMD) <[email protected]> AuthorDate: Thu, 23 Jan 2025 13:02:32 +01:00 Committer: Borislav Petkov (AMD) <[email protected]> CommitterDate: Mon, 17 Feb 2025 09:42:40 +01:00
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.
--- arch/x86/kernel/cpu/microcode/amd.c | 46 ++++++++++++++-------------- 1 file changed, 24 insertions(+), 22 deletions(-)
arch/x86/kernel/cpu/microcode/amd.c +24 -22
@@ -145,6 +145,15 @@ ucode_path[] __maybe_unused = "kernel/x86/microcode/AuthenticAMD.bin";*/ static u32 bsp_cpuid_1_eax__ro_after_init ; +static u32 get_patch_level (void ) +{ +u32 rev, dummy__always_unused ; + +native_rdmsr (MSR_AMD64_PATCH_LEVEL , rev, dummy); + +return rev; +} +static union cpuid_1_eax ucode_rev_to_cpuid (unsigned int val) {union zen_patch_rev p;
@@ -483,10 +492,10 @@ static void scan_containers(u8 *ucode, size_t size, struct cont_desc *desc) } } -static bool __apply_microcode_amd (struct microcode_amd *mc,unsigned int psize) +static bool __apply_microcode_amd (struct microcode_amd *mc,u32 *cur_rev, +unsigned int psize) {unsigned long p_addr = (unsigned long )&mc->hdr.data_code; -u32 rev, dummy;native_wrmsrl (MSR_AMD64_PATCH_LOADER , p_addr);
@@ -504,9 +513,8 @@ static bool __apply_microcode_amd(struct microcode_amd *mc, unsigned int psize) }/* verify patch application was successful */ -native_rdmsr (MSR_AMD64_PATCH_LEVEL , rev, dummy); - -if (rev != mc->hdr.patch_id) + *cur_rev =get_patch_level (); +if (*cur_rev != mc->hdr.patch_id)return false;return true;
@@ -563,11 +571,12 @@ void __init load_ucode_amd_bsp(struct early_load_data *ed, unsigned int cpuid_1_struct cont_desc desc = { };struct microcode_amd *mc;struct cpio_data cp = { }; -u32 dummy; +u32 rev; bsp_cpuid_1_eax = cpuid_1_eax; -native_rdmsr (MSR_AMD64_PATCH_LEVEL , ed->old_rev, dummy); + rev =get_patch_level (); + ed->old_rev = rev;/* Needed in load_microcode_amd() */ 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_if (ed->old_rev > mc->hdr.patch_id)return ; -if (__apply_microcode_amd (mc, desc.psize)) -native_rdmsr (MSR_AMD64_PATCH_LEVEL , ed->new_rev, dummy); +if (__apply_microcode_amd (mc, &rev, desc.psize)) + ed->new_rev = rev; } static inline bool patch_cpus_equivalent(struct ucode_patch *p,
@@ -692,14 +701,9 @@ static void free_cache(void)static struct ucode_patch *find_patch (unsigned int cpu) {struct ucode_cpu_info *uci = ucode_cpu_info + cpu; -u32 rev, dummy__always_unused ;u16 equiv_id =0 ; -/* fetch rev if not populated yet: */ -if (!uci->cpu_sig.rev) { -rdmsr (MSR_AMD64_PATCH_LEVEL , rev, dummy); - uci->cpu_sig.rev = rev; - } + uci->cpu_sig.rev =get_patch_level ();if (x86_family (bsp_cpuid_1_eax) <0x17 ) { equiv_id =find_equiv_id (&equiv_table, uci->cpu_sig.sig);
@@ -722,22 +726,20 @@ void reload_ucode_amd(unsigned int cpu) mc = p->data; -rdmsr (MSR_AMD64_PATCH_LEVEL , rev, dummy); - + rev =get_patch_level ();if (rev < mc->hdr.patch_id) { -if (__apply_microcode_amd (mc, p->size)) -pr_info_once ("reload revision: 0x%08x\n" , mc->hdr.patch_id); +if (__apply_microcode_amd (mc, &rev, p->size)) +pr_info_once ("reload revision: 0x%08x\n" , rev); } }static int collect_cpu_info_amd (int cpu,struct cpu_signature *csig) { -struct cpuinfo_x86 *c = &cpu_data (cpu);struct ucode_cpu_info *uci = ucode_cpu_info + cpu;struct ucode_patch *p; csig->sig =cpuid_eax (0x00000001 ); - csig->rev = c->microcode; + csig->rev =get_patch_level ();/* * 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) goto out; } -if (!__apply_microcode_amd (mc_amd, p->size)) { +if (!__apply_microcode_amd (mc_amd, &rev, p->size)) {pr_err ("CPU%d: update failed for patch_level=0x%08x\n" , cpu, mc_amd->hdr.patch_id);return UCODE_ERROR ;
Quoted 6 lines · Borislav Petkov
From: "Borislav Petkov (AMD)" <[email protected]>
A bunch of small and trivial cleanups which sprang up from recent staring at the loader.
No functional changes.