1 /*-
2 * Copyright (c) 2012 Andriy Gapon <[email protected]>.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 #include <sys/cdefs.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <sys/mman.h>
30 #include <sys/ioctl.h>
31 #include <sys/ioccom.h>
32 #include <sys/cpuctl.h>
33
34 #include <machine/cpufunc.h>
35 #include <machine/specialreg.h>
36
37 #include <assert.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42 #include <fcntl.h>
43 #include <err.h>
44
45 #include "cpucontrol.h"
46 #include "amd.h"
47
48 int
amd10h_probe(int fd)49 amd10h_probe(int fd)
50 {
51 char vendor[13];
52 cpuctl_cpuid_args_t idargs;
53 uint32_t family;
54 uint32_t signature;
55 int error;
56
57 idargs.level = 0;
58 error = ioctl(fd, CPUCTL_CPUID, &idargs);
59 if (error < 0) {
60 WARN(0, "ioctl()");
61 return (1);
62 }
63 ((uint32_t *)vendor)[0] = idargs.data[1];
64 ((uint32_t *)vendor)[1] = idargs.data[3];
65 ((uint32_t *)vendor)[2] = idargs.data[2];
66 vendor[12] = '\0';
67 if (strncmp(vendor, AMD_VENDOR_ID, sizeof(AMD_VENDOR_ID)) != 0)
68 return (1);
69
70 idargs.level = 1;
71 error = ioctl(fd, CPUCTL_CPUID, &idargs);
72 if (error < 0) {
73 WARN(0, "ioctl()");
74 return (1);
75 }
76 signature = idargs.data[0];
77 family = ((signature >> 8) & 0x0f) + ((signature >> 20) & 0xff);
78 if (family < 0x10)
79 return (1);
80 return (0);
81 }
82
83 /*
84 * NB: the format of microcode update files is not documented by AMD.
85 * It has been reverse engineered from studying Coreboot, illumos and Linux
86 * source code.
87 */
88 void
amd10h_update(const struct ucode_update_params * params)89 amd10h_update(const struct ucode_update_params *params)
90 {
91 cpuctl_cpuid_args_t idargs;
92 cpuctl_msr_args_t msrargs;
93 cpuctl_update_args_t args;
94 const amd_10h_fw_header_t *fw_header;
95 const amd_10h_fw_header_t *selected_fw;
96 const equiv_cpu_entry_t *equiv_cpu_table;
97 const section_header_t *section_header;
98 const container_header_t *container_header;
99 const uint8_t *fw_data;
100 const uint8_t *fw_image;
101 const char *dev, *path;
102 size_t fw_size;
103 size_t selected_size;
104 uint32_t revision;
105 uint32_t new_rev;
106 uint32_t signature;
107 uint16_t equiv_id;
108 int devfd;
109 unsigned int i;
110 int error;
111
112 dev = params->dev_path;
113 path = params->fw_path;
114 devfd = params->devfd;
115 fw_image = params->fwimage;
116 fw_size = params->fwsize;
117
118 assert(path);
119 assert(dev);
120
121 idargs.level = 1;
122 error = ioctl(devfd, CPUCTL_CPUID, &idargs);
123 if (error < 0) {
124 WARN(0, "ioctl()");
125 goto done;
126 }
127 signature = idargs.data[0];
128
129 msrargs.msr = MSR_BIOS_SIGN;
130 error = ioctl(devfd, CPUCTL_RDMSR, &msrargs);
131 if (error < 0) {
132 WARN(0, "ioctl(%s)", dev);
133 goto done;
134 }
135 revision = (uint32_t)msrargs.data;
136
137 WARNX(1, "found cpu family %#x model %#x "
138 "stepping %#x extfamily %#x extmodel %#x.",
139 ((signature >> 8) & 0x0f) + ((signature >> 20) & 0xff),
140 (signature >> 4) & 0x0f,
141 (signature >> 0) & 0x0f, (signature >> 20) & 0xff,
142 (signature >> 16) & 0x0f);
143 WARNX(1, "microcode revision %#x", revision);
144
145 /*
146 * Open the firmware file.
147 */
148 WARNX(1, "checking %s for update.", path);
149 if (fw_size <
150 (sizeof(*container_header) + sizeof(*section_header))) {
151 WARNX(2, "file too short: %s", path);
152 goto done;
153 }
154
155 /*
156 * mmap the whole image.
157 */
158 fw_data = fw_image;
159 container_header = (const container_header_t *)fw_data;
160 if (container_header->magic != AMD_10H_MAGIC) {
161 WARNX(2, "%s is not a valid amd firmware: bad magic", path);
162 goto done;
163 }
164 fw_data += sizeof(*container_header);
165 fw_size -= sizeof(*container_header);
166
167 section_header = (const section_header_t *)fw_data;
168 if (section_header->type != AMD_10H_EQUIV_TABLE_TYPE) {
169 WARNX(2, "%s is not a valid amd firmware: "
170 "first section is not CPU equivalence table", path);
171 goto done;
172 }
173 if (section_header->size == 0) {
174 WARNX(2, "%s is not a valid amd firmware: "
175 "first section is empty", path);
176 goto done;
177 }
178 fw_data += sizeof(*section_header);
179 fw_size -= sizeof(*section_header);
180
181 if (section_header->size > fw_size) {
182 WARNX(2, "%s is not a valid amd firmware: "
183 "file is truncated", path);
184 goto done;
185 }
186 if (section_header->size < sizeof(*equiv_cpu_table)) {
187 WARNX(2, "%s is not a valid amd firmware: "
188 "first section is too short", path);
189 goto done;
190 }
191 equiv_cpu_table = (const equiv_cpu_entry_t *)fw_data;
192 fw_data += section_header->size;
193 fw_size -= section_header->size;
194
195 equiv_id = 0;
196 for (i = 0; equiv_cpu_table[i].installed_cpu != 0; i++) {
197 if (signature == equiv_cpu_table[i].installed_cpu) {
198 equiv_id = equiv_cpu_table[i].equiv_cpu;
199 WARNX(3, "equiv_id: %x, signature %8x,"
200 " equiv_cpu_table[%d] %8x", equiv_id, signature,
201 i, equiv_cpu_table[i].installed_cpu);
202 break;
203 }
204 }
205 if (equiv_id == 0) {
206 WARNX(2, "CPU is not found in the equivalence table");
207 goto done;
208 }
209
210 selected_fw = NULL;
211 selected_size = 0;
212 while (fw_size >= sizeof(*section_header)) {
213 section_header = (const section_header_t *)fw_data;
214 fw_data += sizeof(*section_header);
215 fw_size -= sizeof(*section_header);
216 if (section_header->type != AMD_10H_uCODE_TYPE) {
217 WARNX(2, "%s is not a valid amd firmware: "
218 "section has incorrect type", path);
219 goto done;
220 }
221 if (section_header->size > fw_size) {
222 WARNX(2, "%s is not a valid amd firmware: "
223 "file is truncated", path);
224 goto done;
225 }
226 if (section_header->size < sizeof(*fw_header)) {
227 WARNX(2, "%s is not a valid amd firmware: "
228 "section is too short", path);
229 goto done;
230 }
231 fw_header = (const amd_10h_fw_header_t *)fw_data;
232 fw_data += section_header->size;
233 fw_size -= section_header->size;
234
235 if (fw_header->processor_rev_id != equiv_id) {
236 WARNX(1, "firmware processor_rev_id %x, equiv_id %x",
237 fw_header->processor_rev_id, equiv_id);
238 continue; /* different cpu */
239 }
240 if (fw_header->patch_id <= revision) {
241 WARNX(1, "patch_id %x, revision %x",
242 fw_header->patch_id, revision);
243 continue; /* not newer revision */
244 }
245 if (fw_header->nb_dev_id != 0 || fw_header->sb_dev_id != 0) {
246 WARNX(2, "Chipset-specific microcode is not supported");
247 }
248
249 WARNX(3, "selecting revision: %x", fw_header->patch_id);
250 revision = fw_header->patch_id;
251 selected_fw = fw_header;
252 selected_size = section_header->size;
253 }
254
255 if (fw_size != 0) {
256 WARNX(2, "%s is not a valid amd firmware: "
257 "file is truncated", path);
258 goto done;
259 }
260
261 if (selected_fw != NULL) {
262 WARNX(1, "selected ucode size is %zu", selected_size);
263 fprintf(stderr, "%s: updating cpu %s to revision %#x... ",
264 path, dev, revision);
265
266 args.data = __DECONST(void *, selected_fw);
267 args.size = selected_size;
268 error = ioctl(devfd, CPUCTL_UPDATE, &args);
269 if (error < 0) {
270 fprintf(stderr, "failed.\n");
271 warn("ioctl()");
272 goto done;
273 }
274 fprintf(stderr, "done.\n");
275 }
276
277 msrargs.msr = MSR_BIOS_SIGN;
278 error = ioctl(devfd, CPUCTL_RDMSR, &msrargs);
279 if (error < 0) {
280 WARN(0, "ioctl(%s)", dev);
281 goto done;
282 }
283 new_rev = (uint32_t)msrargs.data;
284 if (new_rev != revision)
285 WARNX(0, "revision after update %#x", new_rev);
286
287 done:
288 return;
289 }
290