2014-11-13 05:42:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2014 Google, Inc
|
|
|
|
* Copyright (C) 2000 Ronald G. Minnich
|
|
|
|
*
|
|
|
|
* Microcode update for Intel PIII and later CPUs
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: GPL-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fdtdec.h>
|
|
|
|
#include <libfdt.h>
|
|
|
|
#include <asm/cpu.h>
|
|
|
|
#include <asm/msr.h>
|
2015-01-01 23:18:14 +00:00
|
|
|
#include <asm/msr-index.h>
|
2014-11-13 05:42:21 +00:00
|
|
|
#include <asm/processor.h>
|
2015-01-01 23:18:14 +00:00
|
|
|
#include <asm/arch/microcode.h>
|
2014-11-13 05:42:21 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* struct microcode_update - standard microcode header from Intel
|
|
|
|
*
|
|
|
|
* We read this information out of the device tree and use it to determine
|
|
|
|
* whether the update is applicable or not. We also use the same structure
|
|
|
|
* to read information from the CPU.
|
|
|
|
*/
|
|
|
|
struct microcode_update {
|
|
|
|
uint header_version;
|
|
|
|
uint update_revision;
|
|
|
|
uint date_code;
|
|
|
|
uint processor_signature;
|
|
|
|
uint checksum;
|
|
|
|
uint loader_revision;
|
|
|
|
uint processor_flags;
|
|
|
|
const void *data;
|
|
|
|
int size;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int microcode_decode_node(const void *blob, int node,
|
|
|
|
struct microcode_update *update)
|
|
|
|
{
|
|
|
|
update->data = fdt_getprop(blob, node, "data", &update->size);
|
|
|
|
if (!update->data)
|
|
|
|
return -EINVAL;
|
2015-01-01 23:18:14 +00:00
|
|
|
update->data += UCODE_HEADER_LEN;
|
|
|
|
update->size -= UCODE_HEADER_LEN;
|
2014-11-13 05:42:21 +00:00
|
|
|
|
|
|
|
update->header_version = fdtdec_get_int(blob, node,
|
|
|
|
"intel,header-version", 0);
|
|
|
|
update->update_revision = fdtdec_get_int(blob, node,
|
|
|
|
"intel,update-revision", 0);
|
|
|
|
update->date_code = fdtdec_get_int(blob, node,
|
|
|
|
"intel,date-code", 0);
|
|
|
|
update->processor_signature = fdtdec_get_int(blob, node,
|
2014-12-16 05:02:41 +00:00
|
|
|
"intel,processor-signature", 0);
|
2014-11-13 05:42:21 +00:00
|
|
|
update->checksum = fdtdec_get_int(blob, node, "intel,checksum", 0);
|
|
|
|
update->loader_revision = fdtdec_get_int(blob, node,
|
2014-12-16 05:02:41 +00:00
|
|
|
"intel,loader-revision", 0);
|
2014-11-13 05:42:21 +00:00
|
|
|
update->processor_flags = fdtdec_get_int(blob, node,
|
2014-12-16 05:02:41 +00:00
|
|
|
"intel,processor-flags", 0);
|
2014-11-13 05:42:21 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-12-16 05:02:41 +00:00
|
|
|
static inline uint32_t microcode_read_rev(void)
|
2014-11-13 05:42:21 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Some Intel CPUs can be very finicky about the CPUID sequence used.
|
|
|
|
* So this is implemented in assembly so that it works reliably.
|
|
|
|
*/
|
|
|
|
uint32_t low, high;
|
|
|
|
|
|
|
|
asm volatile (
|
|
|
|
"xorl %%eax, %%eax\n"
|
|
|
|
"xorl %%edx, %%edx\n"
|
2015-01-01 23:18:14 +00:00
|
|
|
"movl %2, %%ecx\n"
|
2014-11-13 05:42:21 +00:00
|
|
|
"wrmsr\n"
|
|
|
|
"movl $0x01, %%eax\n"
|
|
|
|
"cpuid\n"
|
2015-01-01 23:18:14 +00:00
|
|
|
"movl %2, %%ecx\n"
|
2014-11-13 05:42:21 +00:00
|
|
|
"rdmsr\n"
|
|
|
|
: /* outputs */
|
|
|
|
"=a" (low), "=d" (high)
|
|
|
|
: /* inputs */
|
2015-01-01 23:18:14 +00:00
|
|
|
"i" (MSR_IA32_UCODE_REV)
|
2014-11-13 05:42:21 +00:00
|
|
|
: /* clobbers */
|
|
|
|
"ebx", "ecx"
|
|
|
|
);
|
|
|
|
|
|
|
|
return high;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void microcode_read_cpu(struct microcode_update *cpu)
|
|
|
|
{
|
|
|
|
/* CPUID sets MSR 0x8B iff a microcode update has been loaded. */
|
|
|
|
unsigned int x86_model, x86_family;
|
|
|
|
struct cpuid_result result;
|
|
|
|
uint32_t low, high;
|
|
|
|
|
2015-01-01 23:18:14 +00:00
|
|
|
wrmsr(MSR_IA32_UCODE_REV, 0, 0);
|
2014-11-13 05:42:21 +00:00
|
|
|
result = cpuid(1);
|
2015-01-01 23:18:14 +00:00
|
|
|
rdmsr(MSR_IA32_UCODE_REV, low, cpu->update_revision);
|
2014-11-13 05:42:21 +00:00
|
|
|
x86_model = (result.eax >> 4) & 0x0f;
|
|
|
|
x86_family = (result.eax >> 8) & 0x0f;
|
|
|
|
cpu->processor_signature = result.eax;
|
|
|
|
|
|
|
|
cpu->processor_flags = 0;
|
|
|
|
if ((x86_model >= 5) || (x86_family > 6)) {
|
|
|
|
rdmsr(0x17, low, high);
|
|
|
|
cpu->processor_flags = 1 << ((high >> 18) & 7);
|
|
|
|
}
|
|
|
|
debug("microcode: sig=%#x pf=%#x revision=%#x\n",
|
|
|
|
cpu->processor_signature, cpu->processor_flags,
|
|
|
|
cpu->update_revision);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get a microcode update from the device tree and apply it */
|
|
|
|
int microcode_update_intel(void)
|
|
|
|
{
|
|
|
|
struct microcode_update cpu, update;
|
|
|
|
const void *blob = gd->fdt_blob;
|
2014-12-16 05:02:41 +00:00
|
|
|
int skipped;
|
2014-11-13 05:42:21 +00:00
|
|
|
int count;
|
|
|
|
int node;
|
|
|
|
int ret;
|
2015-01-01 23:18:14 +00:00
|
|
|
int rev;
|
2014-11-13 05:42:21 +00:00
|
|
|
|
|
|
|
microcode_read_cpu(&cpu);
|
|
|
|
node = 0;
|
|
|
|
count = 0;
|
2014-12-16 05:02:41 +00:00
|
|
|
skipped = 0;
|
2014-11-13 05:42:21 +00:00
|
|
|
do {
|
|
|
|
node = fdtdec_next_compatible(blob, node,
|
|
|
|
COMPAT_INTEL_MICROCODE);
|
|
|
|
if (node < 0) {
|
|
|
|
debug("%s: Found %d updates\n", __func__, count);
|
2014-12-16 05:02:41 +00:00
|
|
|
return count ? 0 : skipped ? -EEXIST : -ENOENT;
|
2014-11-13 05:42:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = microcode_decode_node(blob, node, &update);
|
|
|
|
if (ret) {
|
|
|
|
debug("%s: Unable to decode update: %d\n", __func__,
|
|
|
|
ret);
|
|
|
|
return ret;
|
|
|
|
}
|
2014-12-16 05:02:41 +00:00
|
|
|
if (!(update.processor_signature == cpu.processor_signature &&
|
|
|
|
(update.processor_flags & cpu.processor_flags))) {
|
|
|
|
debug("%s: Skipping non-matching update, sig=%x, pf=%x\n",
|
|
|
|
__func__, update.processor_signature,
|
|
|
|
update.processor_flags);
|
|
|
|
skipped++;
|
|
|
|
continue;
|
2014-11-13 05:42:21 +00:00
|
|
|
}
|
2015-01-01 23:18:14 +00:00
|
|
|
wrmsr(MSR_IA32_UCODE_WRITE, (ulong)update.data, 0);
|
|
|
|
rev = microcode_read_rev();
|
2014-11-13 05:42:21 +00:00
|
|
|
debug("microcode: updated to revision 0x%x date=%04x-%02x-%02x\n",
|
2015-01-01 23:18:14 +00:00
|
|
|
rev, update.date_code & 0xffff,
|
2014-11-13 05:42:21 +00:00
|
|
|
(update.date_code >> 24) & 0xff,
|
|
|
|
(update.date_code >> 16) & 0xff);
|
2015-01-01 23:18:14 +00:00
|
|
|
if (update.update_revision != rev) {
|
|
|
|
printf("Microcode update failed\n");
|
|
|
|
return -EFAULT;
|
|
|
|
}
|
2014-11-13 05:42:21 +00:00
|
|
|
count++;
|
|
|
|
} while (1);
|
|
|
|
}
|