1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2014 Google, Inc
4 * Copyright (C) 2000 Ronald G. Minnich
6 * Microcode update for Intel PIII and later CPUs
13 #include <asm/global_data.h>
14 #include <linux/libfdt.h>
16 #include <asm/microcode.h>
18 #include <asm/msr-index.h>
19 #include <asm/processor.h>
21 DECLARE_GLOBAL_DATA_PTR;
24 * struct microcode_update - standard microcode header from Intel
26 * We read this information out of the device tree and use it to determine
27 * whether the update is applicable or not. We also use the same structure
28 * to read information from the CPU.
30 struct microcode_update {
34 uint processor_signature;
42 static int microcode_decode_node(const void *blob, int node,
43 struct microcode_update *update)
45 update->data = fdt_getprop(blob, node, "data", &update->size);
49 update->header_version = fdtdec_get_int(blob, node,
50 "intel,header-version", 0);
51 update->update_revision = fdtdec_get_int(blob, node,
52 "intel,update-revision", 0);
53 update->date_code = fdtdec_get_int(blob, node,
54 "intel,date-code", 0);
55 update->processor_signature = fdtdec_get_int(blob, node,
56 "intel,processor-signature", 0);
57 update->checksum = fdtdec_get_int(blob, node, "intel,checksum", 0);
58 update->loader_revision = fdtdec_get_int(blob, node,
59 "intel,loader-revision", 0);
60 update->processor_flags = fdtdec_get_int(blob, node,
61 "intel,processor-flags", 0);
66 int microcode_read_rev(void)
68 /* Quark does not have microcode MSRs */
69 #ifdef CONFIG_INTEL_QUARK
73 * Some Intel CPUs can be very finicky about the CPUID sequence used.
74 * So this is implemented in assembly so that it works reliably.
88 "=a" (low), "=d" (high)
90 "i" (MSR_IA32_UCODE_REV)
99 static void microcode_read_cpu(struct microcode_update *cpu)
101 /* CPUID sets MSR 0x8B iff a microcode update has been loaded. */
102 unsigned int x86_model, x86_family;
103 struct cpuid_result result;
106 wrmsr(MSR_IA32_UCODE_REV, 0, 0);
108 rdmsr(MSR_IA32_UCODE_REV, low, cpu->update_revision);
109 x86_model = (result.eax >> 4) & 0x0f;
110 x86_family = (result.eax >> 8) & 0x0f;
111 cpu->processor_signature = result.eax;
113 cpu->processor_flags = 0;
114 if ((x86_model >= 5) || (x86_family > 6)) {
115 rdmsr(0x17, low, high);
116 cpu->processor_flags = 1 << ((high >> 18) & 7);
118 debug("microcode: sig=%#x pf=%#x revision=%#x\n",
119 cpu->processor_signature, cpu->processor_flags,
120 cpu->update_revision);
123 /* Get a microcode update from the device tree and apply it */
124 int microcode_update_intel(void)
126 struct microcode_update cpu, update;
128 const void *blob = gd->fdt_blob;
135 microcode_read_cpu(&cpu);
140 node = fdtdec_next_compatible(blob, node,
141 COMPAT_INTEL_MICROCODE);
143 debug("%s: Found %d updates\n", __func__, count);
144 return count ? 0 : skipped ? -EEXIST : -ENOENT;
147 ret = microcode_decode_node(blob, node, &update);
148 if (ret == -ENOENT && ucode_base) {
150 * The microcode has been removed from the device tree
151 * in the build system. In that case it will have
152 * already been updated in car_init().
154 debug("%s: Microcode data not available\n", __func__);
159 debug("%s: Unable to decode update: %d\n", __func__,
163 if (!(update.processor_signature == cpu.processor_signature &&
164 (update.processor_flags & cpu.processor_flags))) {
165 debug("%s: Skipping non-matching update, sig=%x, pf=%x\n",
166 __func__, update.processor_signature,
167 update.processor_flags);
171 address = (ulong)update.data + UCODE_HEADER_LEN;
172 wrmsr(MSR_IA32_UCODE_WRITE, address, 0);
173 rev = microcode_read_rev();
174 debug("microcode: updated to revision 0x%x date=%04x-%02x-%02x\n",
175 rev, update.date_code & 0xffff,
176 (update.date_code >> 24) & 0xff,
177 (update.date_code >> 16) & 0xff);
178 if (update.update_revision != rev) {
179 printf("Microcode update failed\n");
184 ucode_base = (ulong)update.data;
185 ucode_size = update.size;