Simon Glass | f79d538 | 2014-11-12 22:42:21 -0700 | [diff] [blame] | 1 | /* |
| 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> |
| 15 | #include <asm/msr.h> |
Simon Glass | 9281eb5 | 2015-01-01 16:18:14 -0700 | [diff] [blame] | 16 | #include <asm/msr-index.h> |
Simon Glass | f79d538 | 2014-11-12 22:42:21 -0700 | [diff] [blame] | 17 | #include <asm/processor.h> |
Simon Glass | 9281eb5 | 2015-01-01 16:18:14 -0700 | [diff] [blame] | 18 | #include <asm/arch/microcode.h> |
Simon Glass | f79d538 | 2014-11-12 22:42:21 -0700 | [diff] [blame] | 19 | |
| 20 | /** |
| 21 | * struct microcode_update - standard microcode header from Intel |
| 22 | * |
| 23 | * We read this information out of the device tree and use it to determine |
| 24 | * whether the update is applicable or not. We also use the same structure |
| 25 | * to read information from the CPU. |
| 26 | */ |
| 27 | struct microcode_update { |
| 28 | uint header_version; |
| 29 | uint update_revision; |
| 30 | uint date_code; |
| 31 | uint processor_signature; |
| 32 | uint checksum; |
| 33 | uint loader_revision; |
| 34 | uint processor_flags; |
| 35 | const void *data; |
| 36 | int size; |
| 37 | }; |
| 38 | |
| 39 | static int microcode_decode_node(const void *blob, int node, |
| 40 | struct microcode_update *update) |
| 41 | { |
| 42 | update->data = fdt_getprop(blob, node, "data", &update->size); |
| 43 | if (!update->data) |
| 44 | return -EINVAL; |
Simon Glass | 9281eb5 | 2015-01-01 16:18:14 -0700 | [diff] [blame] | 45 | update->data += UCODE_HEADER_LEN; |
| 46 | update->size -= UCODE_HEADER_LEN; |
Simon Glass | f79d538 | 2014-11-12 22:42:21 -0700 | [diff] [blame] | 47 | |
| 48 | update->header_version = fdtdec_get_int(blob, node, |
| 49 | "intel,header-version", 0); |
| 50 | update->update_revision = fdtdec_get_int(blob, node, |
| 51 | "intel,update-revision", 0); |
| 52 | update->date_code = fdtdec_get_int(blob, node, |
| 53 | "intel,date-code", 0); |
| 54 | update->processor_signature = fdtdec_get_int(blob, node, |
Simon Glass | c8a5c41 | 2014-12-15 22:02:41 -0700 | [diff] [blame] | 55 | "intel,processor-signature", 0); |
Simon Glass | f79d538 | 2014-11-12 22:42:21 -0700 | [diff] [blame] | 56 | update->checksum = fdtdec_get_int(blob, node, "intel,checksum", 0); |
| 57 | update->loader_revision = fdtdec_get_int(blob, node, |
Simon Glass | c8a5c41 | 2014-12-15 22:02:41 -0700 | [diff] [blame] | 58 | "intel,loader-revision", 0); |
Simon Glass | f79d538 | 2014-11-12 22:42:21 -0700 | [diff] [blame] | 59 | update->processor_flags = fdtdec_get_int(blob, node, |
Simon Glass | c8a5c41 | 2014-12-15 22:02:41 -0700 | [diff] [blame] | 60 | "intel,processor-flags", 0); |
Simon Glass | f79d538 | 2014-11-12 22:42:21 -0700 | [diff] [blame] | 61 | |
| 62 | return 0; |
| 63 | } |
| 64 | |
Simon Glass | c8a5c41 | 2014-12-15 22:02:41 -0700 | [diff] [blame] | 65 | static inline uint32_t microcode_read_rev(void) |
Simon Glass | f79d538 | 2014-11-12 22:42:21 -0700 | [diff] [blame] | 66 | { |
| 67 | /* |
| 68 | * Some Intel CPUs can be very finicky about the CPUID sequence used. |
| 69 | * So this is implemented in assembly so that it works reliably. |
| 70 | */ |
| 71 | uint32_t low, high; |
| 72 | |
| 73 | asm volatile ( |
| 74 | "xorl %%eax, %%eax\n" |
| 75 | "xorl %%edx, %%edx\n" |
Simon Glass | 9281eb5 | 2015-01-01 16:18:14 -0700 | [diff] [blame] | 76 | "movl %2, %%ecx\n" |
Simon Glass | f79d538 | 2014-11-12 22:42:21 -0700 | [diff] [blame] | 77 | "wrmsr\n" |
| 78 | "movl $0x01, %%eax\n" |
| 79 | "cpuid\n" |
Simon Glass | 9281eb5 | 2015-01-01 16:18:14 -0700 | [diff] [blame] | 80 | "movl %2, %%ecx\n" |
Simon Glass | f79d538 | 2014-11-12 22:42:21 -0700 | [diff] [blame] | 81 | "rdmsr\n" |
| 82 | : /* outputs */ |
| 83 | "=a" (low), "=d" (high) |
| 84 | : /* inputs */ |
Simon Glass | 9281eb5 | 2015-01-01 16:18:14 -0700 | [diff] [blame] | 85 | "i" (MSR_IA32_UCODE_REV) |
Simon Glass | f79d538 | 2014-11-12 22:42:21 -0700 | [diff] [blame] | 86 | : /* clobbers */ |
| 87 | "ebx", "ecx" |
| 88 | ); |
| 89 | |
| 90 | return high; |
| 91 | } |
| 92 | |
| 93 | static void microcode_read_cpu(struct microcode_update *cpu) |
| 94 | { |
| 95 | /* CPUID sets MSR 0x8B iff a microcode update has been loaded. */ |
| 96 | unsigned int x86_model, x86_family; |
| 97 | struct cpuid_result result; |
| 98 | uint32_t low, high; |
| 99 | |
Simon Glass | 9281eb5 | 2015-01-01 16:18:14 -0700 | [diff] [blame] | 100 | wrmsr(MSR_IA32_UCODE_REV, 0, 0); |
Simon Glass | f79d538 | 2014-11-12 22:42:21 -0700 | [diff] [blame] | 101 | result = cpuid(1); |
Simon Glass | 9281eb5 | 2015-01-01 16:18:14 -0700 | [diff] [blame] | 102 | rdmsr(MSR_IA32_UCODE_REV, low, cpu->update_revision); |
Simon Glass | f79d538 | 2014-11-12 22:42:21 -0700 | [diff] [blame] | 103 | x86_model = (result.eax >> 4) & 0x0f; |
| 104 | x86_family = (result.eax >> 8) & 0x0f; |
| 105 | cpu->processor_signature = result.eax; |
| 106 | |
| 107 | cpu->processor_flags = 0; |
| 108 | if ((x86_model >= 5) || (x86_family > 6)) { |
| 109 | rdmsr(0x17, low, high); |
| 110 | cpu->processor_flags = 1 << ((high >> 18) & 7); |
| 111 | } |
| 112 | debug("microcode: sig=%#x pf=%#x revision=%#x\n", |
| 113 | cpu->processor_signature, cpu->processor_flags, |
| 114 | cpu->update_revision); |
| 115 | } |
| 116 | |
| 117 | /* Get a microcode update from the device tree and apply it */ |
| 118 | int microcode_update_intel(void) |
| 119 | { |
| 120 | struct microcode_update cpu, update; |
| 121 | const void *blob = gd->fdt_blob; |
Simon Glass | c8a5c41 | 2014-12-15 22:02:41 -0700 | [diff] [blame] | 122 | int skipped; |
Simon Glass | f79d538 | 2014-11-12 22:42:21 -0700 | [diff] [blame] | 123 | int count; |
| 124 | int node; |
| 125 | int ret; |
Simon Glass | 9281eb5 | 2015-01-01 16:18:14 -0700 | [diff] [blame] | 126 | int rev; |
Simon Glass | f79d538 | 2014-11-12 22:42:21 -0700 | [diff] [blame] | 127 | |
| 128 | microcode_read_cpu(&cpu); |
| 129 | node = 0; |
| 130 | count = 0; |
Simon Glass | c8a5c41 | 2014-12-15 22:02:41 -0700 | [diff] [blame] | 131 | skipped = 0; |
Simon Glass | f79d538 | 2014-11-12 22:42:21 -0700 | [diff] [blame] | 132 | do { |
| 133 | node = fdtdec_next_compatible(blob, node, |
| 134 | COMPAT_INTEL_MICROCODE); |
| 135 | if (node < 0) { |
| 136 | debug("%s: Found %d updates\n", __func__, count); |
Simon Glass | c8a5c41 | 2014-12-15 22:02:41 -0700 | [diff] [blame] | 137 | return count ? 0 : skipped ? -EEXIST : -ENOENT; |
Simon Glass | f79d538 | 2014-11-12 22:42:21 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | ret = microcode_decode_node(blob, node, &update); |
| 141 | if (ret) { |
| 142 | debug("%s: Unable to decode update: %d\n", __func__, |
| 143 | ret); |
| 144 | return ret; |
| 145 | } |
Simon Glass | c8a5c41 | 2014-12-15 22:02:41 -0700 | [diff] [blame] | 146 | if (!(update.processor_signature == cpu.processor_signature && |
| 147 | (update.processor_flags & cpu.processor_flags))) { |
| 148 | debug("%s: Skipping non-matching update, sig=%x, pf=%x\n", |
| 149 | __func__, update.processor_signature, |
| 150 | update.processor_flags); |
| 151 | skipped++; |
| 152 | continue; |
Simon Glass | f79d538 | 2014-11-12 22:42:21 -0700 | [diff] [blame] | 153 | } |
Simon Glass | 9281eb5 | 2015-01-01 16:18:14 -0700 | [diff] [blame] | 154 | wrmsr(MSR_IA32_UCODE_WRITE, (ulong)update.data, 0); |
| 155 | rev = microcode_read_rev(); |
Simon Glass | f79d538 | 2014-11-12 22:42:21 -0700 | [diff] [blame] | 156 | debug("microcode: updated to revision 0x%x date=%04x-%02x-%02x\n", |
Simon Glass | 9281eb5 | 2015-01-01 16:18:14 -0700 | [diff] [blame] | 157 | rev, update.date_code & 0xffff, |
Simon Glass | f79d538 | 2014-11-12 22:42:21 -0700 | [diff] [blame] | 158 | (update.date_code >> 24) & 0xff, |
| 159 | (update.date_code >> 16) & 0xff); |
Simon Glass | 9281eb5 | 2015-01-01 16:18:14 -0700 | [diff] [blame] | 160 | if (update.update_revision != rev) { |
| 161 | printf("Microcode update failed\n"); |
| 162 | return -EFAULT; |
| 163 | } |
Simon Glass | f79d538 | 2014-11-12 22:42:21 -0700 | [diff] [blame] | 164 | count++; |
| 165 | } while (1); |
| 166 | } |