]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - arch/x86/cpu/ivybridge/microcode_intel.c
8c11a6351f376a660d9a825336cda14da7bef79a
[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
44         update->header_version = fdtdec_get_int(blob, node,
45                                                 "intel,header-version", 0);
46         update->update_revision = fdtdec_get_int(blob, node,
47                                                  "intel,update-revision", 0);
48         update->date_code = fdtdec_get_int(blob, node,
49                                            "intel,date-code", 0);
50         update->processor_signature = fdtdec_get_int(blob, node,
51                                         "intel.processor-signature", 0);
52         update->checksum = fdtdec_get_int(blob, node, "intel,checksum", 0);
53         update->loader_revision = fdtdec_get_int(blob, node,
54                                                  "loader-revision", 0);
55         update->processor_flags = fdtdec_get_int(blob, node,
56                                                  "processor-flags", 0);
57
58         return 0;
59 }
60
61 static uint32_t microcode_read_rev(void)
62 {
63         /*
64          * Some Intel CPUs can be very finicky about the CPUID sequence used.
65          * So this is implemented in assembly so that it works reliably.
66          */
67         uint32_t low, high;
68
69         asm volatile (
70                 "xorl %%eax, %%eax\n"
71                 "xorl %%edx, %%edx\n"
72                 "movl $0x8b, %%ecx\n"
73                 "wrmsr\n"
74                 "movl $0x01, %%eax\n"
75                 "cpuid\n"
76                 "movl $0x8b, %%ecx\n"
77                 "rdmsr\n"
78                 : /* outputs */
79                 "=a" (low), "=d" (high)
80                 : /* inputs */
81                 : /* clobbers */
82                  "ebx", "ecx"
83         );
84
85         return high;
86 }
87
88 static void microcode_read_cpu(struct microcode_update *cpu)
89 {
90         /* CPUID sets MSR 0x8B iff a microcode update has been loaded. */
91         unsigned int x86_model, x86_family;
92         struct cpuid_result result;
93         uint32_t low, high;
94
95         wrmsr(0x8b, 0, 0);
96         result = cpuid(1);
97         rdmsr(0x8b, low, cpu->update_revision);
98         x86_model = (result.eax >> 4) & 0x0f;
99         x86_family = (result.eax >> 8) & 0x0f;
100         cpu->processor_signature = result.eax;
101
102         cpu->processor_flags = 0;
103         if ((x86_model >= 5) || (x86_family > 6)) {
104                 rdmsr(0x17, low, high);
105                 cpu->processor_flags = 1 << ((high >> 18) & 7);
106         }
107         debug("microcode: sig=%#x pf=%#x revision=%#x\n",
108               cpu->processor_signature, cpu->processor_flags,
109               cpu->update_revision);
110 }
111
112 /* Get a microcode update from the device tree and apply it */
113 int microcode_update_intel(void)
114 {
115         struct microcode_update cpu, update;
116         const void *blob = gd->fdt_blob;
117         int count;
118         int node;
119         int ret;
120
121         microcode_read_cpu(&cpu);
122         node = 0;
123         count = 0;
124         do {
125                 node = fdtdec_next_compatible(blob, node,
126                                               COMPAT_INTEL_MICROCODE);
127                 if (node < 0) {
128                         debug("%s: Found %d updates\n", __func__, count);
129                         return count ? 0 : -ENOENT;
130                 }
131
132                 ret = microcode_decode_node(blob, node, &update);
133                 if (ret) {
134                         debug("%s: Unable to decode update: %d\n", __func__,
135                               ret);
136                         return ret;
137                 }
138                 if (update.processor_signature == cpu.processor_signature &&
139                     (update.processor_flags & cpu.processor_flags)) {
140                         debug("%s: Update already exists\n", __func__);
141                         return -EEXIST;
142                 }
143
144                 wrmsr(0x79, (ulong)update.data, 0);
145                 debug("microcode: updated to revision 0x%x date=%04x-%02x-%02x\n",
146                       microcode_read_rev(), update.date_code & 0xffff,
147                       (update.date_code >> 24) & 0xff,
148                       (update.date_code >> 16) & 0xff);
149                 count++;
150         } while (1);
151 }