2 * Copyright (c) 2014 Google, Inc
3 * Copyright (C) 2000 Ronald G. Minnich
5 * Microcode update for Intel PIII and later CPUs
7 * SPDX-License-Identifier: GPL-2.0
16 #include <asm/processor.h>
19 * struct microcode_update - standard microcode header from Intel
21 * We read this information out of the device tree and use it to determine
22 * whether the update is applicable or not. We also use the same structure
23 * to read information from the CPU.
25 struct microcode_update {
29 uint processor_signature;
37 static int microcode_decode_node(const void *blob, int node,
38 struct microcode_update *update)
40 update->data = fdt_getprop(blob, node, "data", &update->size);
44 update->header_version = fdtdec_get_int(blob, node,
45 "intel,header-version", 0);
46 update->update_revision = fdtdec_get_int(blob, node,
47 "intel,update-revision", 0);
48 update->date_code = fdtdec_get_int(blob, node,
49 "intel,date-code", 0);
50 update->processor_signature = fdtdec_get_int(blob, node,
51 "intel.processor-signature", 0);
52 update->checksum = fdtdec_get_int(blob, node, "intel,checksum", 0);
53 update->loader_revision = fdtdec_get_int(blob, node,
54 "loader-revision", 0);
55 update->processor_flags = fdtdec_get_int(blob, node,
56 "processor-flags", 0);
61 static uint32_t microcode_read_rev(void)
64 * Some Intel CPUs can be very finicky about the CPUID sequence used.
65 * So this is implemented in assembly so that it works reliably.
79 "=a" (low), "=d" (high)
88 static void microcode_read_cpu(struct microcode_update *cpu)
90 /* CPUID sets MSR 0x8B iff a microcode update has been loaded. */
91 unsigned int x86_model, x86_family;
92 struct cpuid_result result;
97 rdmsr(0x8b, low, cpu->update_revision);
98 x86_model = (result.eax >> 4) & 0x0f;
99 x86_family = (result.eax >> 8) & 0x0f;
100 cpu->processor_signature = result.eax;
102 cpu->processor_flags = 0;
103 if ((x86_model >= 5) || (x86_family > 6)) {
104 rdmsr(0x17, low, high);
105 cpu->processor_flags = 1 << ((high >> 18) & 7);
107 debug("microcode: sig=%#x pf=%#x revision=%#x\n",
108 cpu->processor_signature, cpu->processor_flags,
109 cpu->update_revision);
112 /* Get a microcode update from the device tree and apply it */
113 int microcode_update_intel(void)
115 struct microcode_update cpu, update;
116 const void *blob = gd->fdt_blob;
121 microcode_read_cpu(&cpu);
125 node = fdtdec_next_compatible(blob, node,
126 COMPAT_INTEL_MICROCODE);
128 debug("%s: Found %d updates\n", __func__, count);
129 return count ? 0 : -ENOENT;
132 ret = microcode_decode_node(blob, node, &update);
134 debug("%s: Unable to decode update: %d\n", __func__,
138 if (update.processor_signature == cpu.processor_signature &&
139 (update.processor_flags & cpu.processor_flags)) {
140 debug("%s: Update already exists\n", __func__);
144 wrmsr(0x79, (ulong)update.data, 0);
145 debug("microcode: updated to revision 0x%x date=%04x-%02x-%02x\n",
146 microcode_read_rev(), update.date_code & 0xffff,
147 (update.date_code >> 24) & 0xff,
148 (update.date_code >> 16) & 0xff);