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