blob: 3054fabfde6a4fe3775c4b50f186479c5fa922b3 [file] [log] [blame]
Simon Glassf79d5382014-11-12 22:42:21 -07001/*
2 * Copyright (c) 2014 Google, Inc
3 * Copyright (C) 2000 Ronald G. Minnich
4 *
5 * Microcode update for Intel PIII and later CPUs
6 *
7 * SPDX-License-Identifier: GPL-2.0
8 */
9
10#include <common.h>
11#include <errno.h>
12#include <fdtdec.h>
13#include <libfdt.h>
14#include <asm/cpu.h>
Simon Glass2df61882016-03-11 22:06:54 -070015#include <asm/microcode.h>
Simon Glassf79d5382014-11-12 22:42:21 -070016#include <asm/msr.h>
Simon Glass9281eb52015-01-01 16:18:14 -070017#include <asm/msr-index.h>
Simon Glassf79d5382014-11-12 22:42:21 -070018#include <asm/processor.h>
Simon Glass2df61882016-03-11 22:06:54 -070019
20DECLARE_GLOBAL_DATA_PTR;
Simon Glassf79d5382014-11-12 22:42:21 -070021
22/**
23 * struct microcode_update - standard microcode header from Intel
24 *
25 * We read this information out of the device tree and use it to determine
26 * whether the update is applicable or not. We also use the same structure
27 * to read information from the CPU.
28 */
29struct microcode_update {
30 uint header_version;
31 uint update_revision;
32 uint date_code;
33 uint processor_signature;
34 uint checksum;
35 uint loader_revision;
36 uint processor_flags;
37 const void *data;
38 int size;
39};
40
41static int microcode_decode_node(const void *blob, int node,
42 struct microcode_update *update)
43{
44 update->data = fdt_getprop(blob, node, "data", &update->size);
45 if (!update->data)
46 return -EINVAL;
Simon Glass9281eb52015-01-01 16:18:14 -070047 update->data += UCODE_HEADER_LEN;
48 update->size -= UCODE_HEADER_LEN;
Simon Glassf79d5382014-11-12 22:42:21 -070049
50 update->header_version = fdtdec_get_int(blob, node,
51 "intel,header-version", 0);
52 update->update_revision = fdtdec_get_int(blob, node,
53 "intel,update-revision", 0);
54 update->date_code = fdtdec_get_int(blob, node,
55 "intel,date-code", 0);
56 update->processor_signature = fdtdec_get_int(blob, node,
Simon Glassc8a5c412014-12-15 22:02:41 -070057 "intel,processor-signature", 0);
Simon Glassf79d5382014-11-12 22:42:21 -070058 update->checksum = fdtdec_get_int(blob, node, "intel,checksum", 0);
59 update->loader_revision = fdtdec_get_int(blob, node,
Simon Glassc8a5c412014-12-15 22:02:41 -070060 "intel,loader-revision", 0);
Simon Glassf79d5382014-11-12 22:42:21 -070061 update->processor_flags = fdtdec_get_int(blob, node,
Simon Glassc8a5c412014-12-15 22:02:41 -070062 "intel,processor-flags", 0);
Simon Glassf79d5382014-11-12 22:42:21 -070063
64 return 0;
65}
66
Simon Glassc8a5c412014-12-15 22:02:41 -070067static inline uint32_t microcode_read_rev(void)
Simon Glassf79d5382014-11-12 22:42:21 -070068{
69 /*
70 * Some Intel CPUs can be very finicky about the CPUID sequence used.
71 * So this is implemented in assembly so that it works reliably.
72 */
73 uint32_t low, high;
74
75 asm volatile (
76 "xorl %%eax, %%eax\n"
77 "xorl %%edx, %%edx\n"
Simon Glass9281eb52015-01-01 16:18:14 -070078 "movl %2, %%ecx\n"
Simon Glassf79d5382014-11-12 22:42:21 -070079 "wrmsr\n"
80 "movl $0x01, %%eax\n"
81 "cpuid\n"
Simon Glass9281eb52015-01-01 16:18:14 -070082 "movl %2, %%ecx\n"
Simon Glassf79d5382014-11-12 22:42:21 -070083 "rdmsr\n"
84 : /* outputs */
85 "=a" (low), "=d" (high)
86 : /* inputs */
Simon Glass9281eb52015-01-01 16:18:14 -070087 "i" (MSR_IA32_UCODE_REV)
Simon Glassf79d5382014-11-12 22:42:21 -070088 : /* clobbers */
89 "ebx", "ecx"
90 );
91
92 return high;
93}
94
95static void microcode_read_cpu(struct microcode_update *cpu)
96{
97 /* CPUID sets MSR 0x8B iff a microcode update has been loaded. */
98 unsigned int x86_model, x86_family;
99 struct cpuid_result result;
100 uint32_t low, high;
101
Simon Glass9281eb52015-01-01 16:18:14 -0700102 wrmsr(MSR_IA32_UCODE_REV, 0, 0);
Simon Glassf79d5382014-11-12 22:42:21 -0700103 result = cpuid(1);
Simon Glass9281eb52015-01-01 16:18:14 -0700104 rdmsr(MSR_IA32_UCODE_REV, low, cpu->update_revision);
Simon Glassf79d5382014-11-12 22:42:21 -0700105 x86_model = (result.eax >> 4) & 0x0f;
106 x86_family = (result.eax >> 8) & 0x0f;
107 cpu->processor_signature = result.eax;
108
109 cpu->processor_flags = 0;
110 if ((x86_model >= 5) || (x86_family > 6)) {
111 rdmsr(0x17, low, high);
112 cpu->processor_flags = 1 << ((high >> 18) & 7);
113 }
114 debug("microcode: sig=%#x pf=%#x revision=%#x\n",
115 cpu->processor_signature, cpu->processor_flags,
116 cpu->update_revision);
117}
118
119/* Get a microcode update from the device tree and apply it */
120int microcode_update_intel(void)
121{
122 struct microcode_update cpu, update;
123 const void *blob = gd->fdt_blob;
Simon Glassc8a5c412014-12-15 22:02:41 -0700124 int skipped;
Simon Glassf79d5382014-11-12 22:42:21 -0700125 int count;
126 int node;
127 int ret;
Simon Glass9281eb52015-01-01 16:18:14 -0700128 int rev;
Simon Glassf79d5382014-11-12 22:42:21 -0700129
130 microcode_read_cpu(&cpu);
131 node = 0;
132 count = 0;
Simon Glassc8a5c412014-12-15 22:02:41 -0700133 skipped = 0;
Simon Glassf79d5382014-11-12 22:42:21 -0700134 do {
135 node = fdtdec_next_compatible(blob, node,
136 COMPAT_INTEL_MICROCODE);
137 if (node < 0) {
138 debug("%s: Found %d updates\n", __func__, count);
Simon Glassc8a5c412014-12-15 22:02:41 -0700139 return count ? 0 : skipped ? -EEXIST : -ENOENT;
Simon Glassf79d5382014-11-12 22:42:21 -0700140 }
141
142 ret = microcode_decode_node(blob, node, &update);
143 if (ret) {
144 debug("%s: Unable to decode update: %d\n", __func__,
145 ret);
146 return ret;
147 }
Simon Glassc8a5c412014-12-15 22:02:41 -0700148 if (!(update.processor_signature == cpu.processor_signature &&
149 (update.processor_flags & cpu.processor_flags))) {
150 debug("%s: Skipping non-matching update, sig=%x, pf=%x\n",
151 __func__, update.processor_signature,
152 update.processor_flags);
153 skipped++;
154 continue;
Simon Glassf79d5382014-11-12 22:42:21 -0700155 }
Simon Glass9281eb52015-01-01 16:18:14 -0700156 wrmsr(MSR_IA32_UCODE_WRITE, (ulong)update.data, 0);
157 rev = microcode_read_rev();
Simon Glassf79d5382014-11-12 22:42:21 -0700158 debug("microcode: updated to revision 0x%x date=%04x-%02x-%02x\n",
Simon Glass9281eb52015-01-01 16:18:14 -0700159 rev, update.date_code & 0xffff,
Simon Glassf79d5382014-11-12 22:42:21 -0700160 (update.date_code >> 24) & 0xff,
161 (update.date_code >> 16) & 0xff);
Simon Glass9281eb52015-01-01 16:18:14 -0700162 if (update.update_revision != rev) {
163 printf("Microcode update failed\n");
164 return -EFAULT;
165 }
Simon Glassf79d5382014-11-12 22:42:21 -0700166 count++;
167 } while (1);
168}