blob: 4d8e1d210838718b493bab85481bc10f4c8bd326 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Simon Glassf79d5382014-11-12 22:42:21 -07002/*
3 * Copyright (c) 2014 Google, Inc
4 * Copyright (C) 2000 Ronald G. Minnich
5 *
6 * Microcode update for Intel PIII and later CPUs
Simon Glassf79d5382014-11-12 22:42:21 -07007 */
8
Tom Riniabb9a042024-05-18 20:20:43 -06009#include <common.h>
Simon Glassf79d5382014-11-12 22:42:21 -070010#include <errno.h>
11#include <fdtdec.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060013#include <asm/global_data.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090014#include <linux/libfdt.h>
Simon Glassf79d5382014-11-12 22:42:21 -070015#include <asm/cpu.h>
Simon Glass2df61882016-03-11 22:06:54 -070016#include <asm/microcode.h>
Simon Glassf79d5382014-11-12 22:42:21 -070017#include <asm/msr.h>
Simon Glass9281eb52015-01-01 16:18:14 -070018#include <asm/msr-index.h>
Simon Glassf79d5382014-11-12 22:42:21 -070019#include <asm/processor.h>
Simon Glass2df61882016-03-11 22:06:54 -070020
21DECLARE_GLOBAL_DATA_PTR;
Simon Glassf79d5382014-11-12 22:42:21 -070022
23/**
24 * struct microcode_update - standard microcode header from Intel
25 *
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.
29 */
30struct microcode_update {
31 uint header_version;
32 uint update_revision;
33 uint date_code;
34 uint processor_signature;
35 uint checksum;
36 uint loader_revision;
37 uint processor_flags;
38 const void *data;
39 int size;
40};
41
42static int microcode_decode_node(const void *blob, int node,
43 struct microcode_update *update)
44{
45 update->data = fdt_getprop(blob, node, "data", &update->size);
46 if (!update->data)
Simon Glassbd241242016-07-25 18:58:58 -060047 return -ENOENT;
Simon Glassf79d5382014-11-12 22:42:21 -070048
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,
Simon Glassc8a5c412014-12-15 22:02:41 -070056 "intel,processor-signature", 0);
Simon Glassf79d5382014-11-12 22:42:21 -070057 update->checksum = fdtdec_get_int(blob, node, "intel,checksum", 0);
58 update->loader_revision = fdtdec_get_int(blob, node,
Simon Glassc8a5c412014-12-15 22:02:41 -070059 "intel,loader-revision", 0);
Simon Glassf79d5382014-11-12 22:42:21 -070060 update->processor_flags = fdtdec_get_int(blob, node,
Simon Glassc8a5c412014-12-15 22:02:41 -070061 "intel,processor-flags", 0);
Simon Glassf79d5382014-11-12 22:42:21 -070062
63 return 0;
64}
65
Simon Glassc17d4502016-03-11 22:07:09 -070066int microcode_read_rev(void)
Simon Glassf79d5382014-11-12 22:42:21 -070067{
Simon Glassc17d4502016-03-11 22:07:09 -070068 /* Quark does not have microcode MSRs */
69#ifdef CONFIG_INTEL_QUARK
70 return 0;
71#else
Simon Glassf79d5382014-11-12 22:42:21 -070072 /*
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.
75 */
76 uint32_t low, high;
77
78 asm volatile (
79 "xorl %%eax, %%eax\n"
80 "xorl %%edx, %%edx\n"
Simon Glass9281eb52015-01-01 16:18:14 -070081 "movl %2, %%ecx\n"
Simon Glassf79d5382014-11-12 22:42:21 -070082 "wrmsr\n"
83 "movl $0x01, %%eax\n"
84 "cpuid\n"
Simon Glass9281eb52015-01-01 16:18:14 -070085 "movl %2, %%ecx\n"
Simon Glassf79d5382014-11-12 22:42:21 -070086 "rdmsr\n"
87 : /* outputs */
88 "=a" (low), "=d" (high)
89 : /* inputs */
Simon Glass9281eb52015-01-01 16:18:14 -070090 "i" (MSR_IA32_UCODE_REV)
Simon Glassf79d5382014-11-12 22:42:21 -070091 : /* clobbers */
92 "ebx", "ecx"
93 );
94
95 return high;
Simon Glassc17d4502016-03-11 22:07:09 -070096#endif
Simon Glassf79d5382014-11-12 22:42:21 -070097}
98
99static void microcode_read_cpu(struct microcode_update *cpu)
100{
101 /* CPUID sets MSR 0x8B iff a microcode update has been loaded. */
102 unsigned int x86_model, x86_family;
103 struct cpuid_result result;
104 uint32_t low, high;
105
Simon Glass9281eb52015-01-01 16:18:14 -0700106 wrmsr(MSR_IA32_UCODE_REV, 0, 0);
Simon Glassf79d5382014-11-12 22:42:21 -0700107 result = cpuid(1);
Simon Glass9281eb52015-01-01 16:18:14 -0700108 rdmsr(MSR_IA32_UCODE_REV, low, cpu->update_revision);
Simon Glassf79d5382014-11-12 22:42:21 -0700109 x86_model = (result.eax >> 4) & 0x0f;
110 x86_family = (result.eax >> 8) & 0x0f;
111 cpu->processor_signature = result.eax;
112
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);
117 }
118 debug("microcode: sig=%#x pf=%#x revision=%#x\n",
119 cpu->processor_signature, cpu->processor_flags,
120 cpu->update_revision);
121}
122
123/* Get a microcode update from the device tree and apply it */
124int microcode_update_intel(void)
125{
126 struct microcode_update cpu, update;
Ivan Gorinov7fe40752018-06-21 21:16:16 -0700127 ulong address;
Simon Glassf79d5382014-11-12 22:42:21 -0700128 const void *blob = gd->fdt_blob;
Simon Glassc8a5c412014-12-15 22:02:41 -0700129 int skipped;
Simon Glassf79d5382014-11-12 22:42:21 -0700130 int count;
131 int node;
132 int ret;
Simon Glass9281eb52015-01-01 16:18:14 -0700133 int rev;
Simon Glassf79d5382014-11-12 22:42:21 -0700134
135 microcode_read_cpu(&cpu);
136 node = 0;
137 count = 0;
Simon Glassc8a5c412014-12-15 22:02:41 -0700138 skipped = 0;
Simon Glassf79d5382014-11-12 22:42:21 -0700139 do {
140 node = fdtdec_next_compatible(blob, node,
141 COMPAT_INTEL_MICROCODE);
142 if (node < 0) {
143 debug("%s: Found %d updates\n", __func__, count);
Simon Glassc8a5c412014-12-15 22:02:41 -0700144 return count ? 0 : skipped ? -EEXIST : -ENOENT;
Simon Glassf79d5382014-11-12 22:42:21 -0700145 }
146
147 ret = microcode_decode_node(blob, node, &update);
Simon Glassbd241242016-07-25 18:58:58 -0600148 if (ret == -ENOENT && ucode_base) {
149 /*
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().
153 */
154 debug("%s: Microcode data not available\n", __func__);
155 skipped++;
156 continue;
157 }
Simon Glassf79d5382014-11-12 22:42:21 -0700158 if (ret) {
159 debug("%s: Unable to decode update: %d\n", __func__,
160 ret);
161 return ret;
162 }
Simon Glassc8a5c412014-12-15 22:02:41 -0700163 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);
168 skipped++;
169 continue;
Simon Glassf79d5382014-11-12 22:42:21 -0700170 }
Ivan Gorinov7fe40752018-06-21 21:16:16 -0700171 address = (ulong)update.data + UCODE_HEADER_LEN;
172 wrmsr(MSR_IA32_UCODE_WRITE, address, 0);
Simon Glass9281eb52015-01-01 16:18:14 -0700173 rev = microcode_read_rev();
Simon Glassf79d5382014-11-12 22:42:21 -0700174 debug("microcode: updated to revision 0x%x date=%04x-%02x-%02x\n",
Simon Glass9281eb52015-01-01 16:18:14 -0700175 rev, update.date_code & 0xffff,
Simon Glassf79d5382014-11-12 22:42:21 -0700176 (update.date_code >> 24) & 0xff,
177 (update.date_code >> 16) & 0xff);
Simon Glass9281eb52015-01-01 16:18:14 -0700178 if (update.update_revision != rev) {
179 printf("Microcode update failed\n");
180 return -EFAULT;
181 }
Simon Glassf79d5382014-11-12 22:42:21 -0700182 count++;
Ivan Gorinov7fe40752018-06-21 21:16:16 -0700183 if (!ucode_base) {
184 ucode_base = (ulong)update.data;
185 ucode_size = update.size;
186 }
Simon Glassf79d5382014-11-12 22:42:21 -0700187 } while (1);
188}