[patch 17/30] x86/microcode/intel: Unify microcode apply() functions

From: Thomas Gleixner
Date: Thu Aug 10 2023 - 14:38:54 EST


Deduplicate the early and late apply() functions.

Signed-off-by: Thomas Gleixner <tglx@xxxxxxxxxxxxx>
---
arch/x86/kernel/cpu/microcode/intel.c | 106 +++++++++++-----------------------
1 file changed, 36 insertions(+), 70 deletions(-)

--- a/arch/x86/kernel/cpu/microcode/intel.c
+++ b/arch/x86/kernel/cpu/microcode/intel.c
@@ -325,12 +325,11 @@ static inline void print_ucode(int old_r
}
#endif

-static enum ucode_state apply_microcode_early(struct ucode_cpu_info *uci, bool early)
+static enum ucode_state apply_microcode(struct ucode_cpu_info *uci, struct microcode_intel *mc,
+ u32 *cur_rev)
{
- struct microcode_intel *mc;
- u32 rev, old_rev;
+ u32 rev;

- mc = uci->mc;
if (!mc)
return UCODE_NFOUND;

@@ -339,14 +338,12 @@ static enum ucode_state apply_microcode_
* operation - when the other hyperthread has updated the microcode
* already.
*/
- rev = intel_get_microcode_revision();
- if (rev >= mc->hdr.rev) {
- uci->cpu_sig.rev = rev;
+ *cur_rev = intel_get_microcode_revision();
+ if (*cur_rev >= mc->hdr.rev) {
+ uci->cpu_sig.rev = *cur_rev;
return UCODE_OK;
}

- old_rev = rev;
-
/*
* Writeback and invalidate caches before updating microcode to avoid
* internal issues depending on what the microcode is updating.
@@ -361,13 +358,23 @@ static enum ucode_state apply_microcode_
return UCODE_ERROR;

uci->cpu_sig.rev = rev;
+ return UCODE_UPDATED;
+}

- if (early)
- print_ucode(old_rev, uci->cpu_sig.rev, mc->hdr.date);
- else
- print_ucode_info(old_rev, uci->cpu_sig.rev, mc->hdr.date);
+static enum ucode_state apply_microcode_early(struct ucode_cpu_info *uci, bool early)
+{
+ struct microcode_intel *mc = uci->mc;
+ enum ucode_state ret;
+ u32 cur_rev;

- return UCODE_UPDATED;
+ ret = apply_microcode(uci, mc, &cur_rev);
+ if (ret == UCODE_UPDATED) {
+ if (early)
+ print_ucode(cur_rev, uci->cpu_sig.rev, mc->hdr.date);
+ else
+ print_ucode_info(cur_rev, uci->cpu_sig.rev, mc->hdr.date);
+ }
+ return ret;
}

static __init bool load_builtin_intel_microcode(struct cpio_data *cp)
@@ -508,70 +515,29 @@ static int collect_cpu_info(int cpu_num,
return 0;
}

-static enum ucode_state apply_microcode_intel(int cpu)
+static enum ucode_state apply_microcode_late(int cpu)
{
struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
- struct cpuinfo_x86 *c = &cpu_data(cpu);
- bool bsp = c->cpu_index == boot_cpu_data.cpu_index;
- struct microcode_intel *mc;
+ struct microcode_intel *mc = ucode_patch_late;
enum ucode_state ret;
- static int prev_rev;
- u32 rev;
-
- /* We should bind the task to the CPU */
- if (WARN_ON(raw_smp_processor_id() != cpu))
- return UCODE_ERROR;
-
- mc = ucode_patch_late;
- if (!mc)
- return UCODE_NFOUND;
+ u32 cur_rev;

- /*
- * Save us the MSR write below - which is a particular expensive
- * operation - when the other hyperthread has updated the microcode
- * already.
- */
- rev = intel_get_microcode_revision();
- if (rev >= mc->hdr.rev) {
- ret = UCODE_OK;
- goto out;
- }
-
- /*
- * Writeback and invalidate caches before updating microcode to avoid
- * internal issues depending on what the microcode is updating.
- */
- native_wbinvd();
-
- /* write microcode via MSR 0x79 */
- wrmsrl(MSR_IA32_UCODE_WRITE, (unsigned long)mc->bits);
-
- rev = intel_get_microcode_revision();
-
- if (rev != mc->hdr.rev) {
- pr_err("CPU%d update to revision 0x%x failed\n",
- cpu, mc->hdr.rev);
+ if (WARN_ON_ONCE(smp_processor_id() != cpu))
return UCODE_ERROR;
- }

- if (bsp && rev != prev_rev) {
- pr_info("updated to revision 0x%x, date = %04x-%02x-%02x\n",
- rev,
- mc->hdr.date & 0xffff,
- mc->hdr.date >> 24,
+ ret = apply_microcode(uci, mc, &cur_rev);
+ if (ret != UCODE_UPDATED && ret != UCODE_OK)
+ return ret;
+
+ if (!cpu && uci->cpu_sig.rev != cur_rev) {
+ pr_info("Updated to revision 0x%x, date = %04x-%02x-%02x\n",
+ uci->cpu_sig.rev, mc->hdr.date & 0xffff, mc->hdr.date >> 24,
(mc->hdr.date >> 16) & 0xff);
- prev_rev = rev;
}

- ret = UCODE_UPDATED;
-
-out:
- uci->cpu_sig.rev = rev;
- c->microcode = rev;
-
- /* Update boot_cpu_data's revision too, if we're on the BSP: */
- if (bsp)
- boot_cpu_data.microcode = rev;
+ cpu_data(cpu).microcode = uci->cpu_sig.rev;
+ if (!cpu)
+ boot_cpu_data.microcode = uci->cpu_sig.rev;

return ret;
}
@@ -718,7 +684,7 @@ static void finalize_late_load(int resul
static struct microcode_ops microcode_intel_ops = {
.request_microcode_fw = request_microcode_fw,
.collect_cpu_info = collect_cpu_info,
- .apply_microcode = apply_microcode_intel,
+ .apply_microcode = apply_microcode_late,
.finalize_late_load = finalize_late_load,
};