2 * Copyright (c) 2012 Andriy Gapon <avg@FreeBSD.org>.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
26 #include <sys/types.h>
29 #include <sys/ioctl.h>
30 #include <sys/ioccom.h>
31 #include <sys/cpuctl.h>
33 #include <machine/cpufunc.h>
34 #include <machine/specialreg.h>
44 #include "cpucontrol.h"
51 cpuctl_cpuid_args_t idargs;
57 error = ioctl(fd, CPUCTL_CPUID, &idargs);
62 ((uint32_t *)vendor)[0] = idargs.data[1];
63 ((uint32_t *)vendor)[1] = idargs.data[3];
64 ((uint32_t *)vendor)[2] = idargs.data[2];
66 if (strncmp(vendor, AMD_VENDOR_ID, sizeof(AMD_VENDOR_ID)) != 0)
70 error = ioctl(fd, CPUCTL_CPUID, &idargs);
75 signature = idargs.data[0];
76 family = ((signature >> 8) & 0x0f) + ((signature >> 20) & 0xff);
83 * NB: the format of microcode update files is not documented by AMD.
84 * It has been reverse engineered from studying Coreboot, illumos and Linux
88 amd10h_update(const struct ucode_update_params *params)
90 cpuctl_cpuid_args_t idargs;
91 cpuctl_msr_args_t msrargs;
92 cpuctl_update_args_t args;
93 const amd_10h_fw_header_t *fw_header;
94 const amd_10h_fw_header_t *selected_fw;
95 const equiv_cpu_entry_t *equiv_cpu_table;
96 const section_header_t *section_header;
97 const container_header_t *container_header;
98 const uint8_t *fw_data;
99 const uint8_t *fw_image;
100 const char *dev, *path;
102 size_t selected_size;
111 dev = params->dev_path;
112 path = params->fw_path;
113 devfd = params->devfd;
114 fw_image = params->fwimage;
115 fw_size = params->fwsize;
121 error = ioctl(devfd, CPUCTL_CPUID, &idargs);
126 signature = idargs.data[0];
128 msrargs.msr = MSR_BIOS_SIGN;
129 error = ioctl(devfd, CPUCTL_RDMSR, &msrargs);
131 WARN(0, "ioctl(%s)", dev);
134 revision = (uint32_t)msrargs.data;
136 WARNX(1, "found cpu family %#x model %#x "
137 "stepping %#x extfamily %#x extmodel %#x.",
138 ((signature >> 8) & 0x0f) + ((signature >> 20) & 0xff),
139 (signature >> 4) & 0x0f,
140 (signature >> 0) & 0x0f, (signature >> 20) & 0xff,
141 (signature >> 16) & 0x0f);
142 WARNX(1, "microcode revision %#x", revision);
145 * Open the firmware file.
147 WARNX(1, "checking %s for update.", path);
149 (sizeof(*container_header) + sizeof(*section_header))) {
150 WARNX(2, "file too short: %s", path);
155 * mmap the whole image.
158 container_header = (const container_header_t *)fw_data;
159 if (container_header->magic != AMD_10H_MAGIC) {
160 WARNX(2, "%s is not a valid amd firmware: bad magic", path);
163 fw_data += sizeof(*container_header);
164 fw_size -= sizeof(*container_header);
166 section_header = (const section_header_t *)fw_data;
167 if (section_header->type != AMD_10H_EQUIV_TABLE_TYPE) {
168 WARNX(2, "%s is not a valid amd firmware: "
169 "first section is not CPU equivalence table", path);
172 if (section_header->size == 0) {
173 WARNX(2, "%s is not a valid amd firmware: "
174 "first section is empty", path);
177 fw_data += sizeof(*section_header);
178 fw_size -= sizeof(*section_header);
180 if (section_header->size > fw_size) {
181 WARNX(2, "%s is not a valid amd firmware: "
182 "file is truncated", path);
185 if (section_header->size < sizeof(*equiv_cpu_table)) {
186 WARNX(2, "%s is not a valid amd firmware: "
187 "first section is too short", path);
190 equiv_cpu_table = (const equiv_cpu_entry_t *)fw_data;
191 fw_data += section_header->size;
192 fw_size -= section_header->size;
195 for (i = 0; equiv_cpu_table[i].installed_cpu != 0; i++) {
196 if (signature == equiv_cpu_table[i].installed_cpu) {
197 equiv_id = equiv_cpu_table[i].equiv_cpu;
198 WARNX(3, "equiv_id: %x, signature %8x,"
199 " equiv_cpu_table[%d] %8x", equiv_id, signature,
200 i, equiv_cpu_table[i].installed_cpu);
205 WARNX(2, "CPU is not found in the equivalence table");
211 while (fw_size >= sizeof(*section_header)) {
212 section_header = (const section_header_t *)fw_data;
213 fw_data += sizeof(*section_header);
214 fw_size -= sizeof(*section_header);
215 if (section_header->type != AMD_10H_uCODE_TYPE) {
216 WARNX(2, "%s is not a valid amd firmware: "
217 "section has incorrect type", path);
220 if (section_header->size > fw_size) {
221 WARNX(2, "%s is not a valid amd firmware: "
222 "file is truncated", path);
225 if (section_header->size < sizeof(*fw_header)) {
226 WARNX(2, "%s is not a valid amd firmware: "
227 "section is too short", path);
230 fw_header = (const amd_10h_fw_header_t *)fw_data;
231 fw_data += section_header->size;
232 fw_size -= section_header->size;
234 if (fw_header->processor_rev_id != equiv_id) {
235 WARNX(1, "firmware processor_rev_id %x, equiv_id %x",
236 fw_header->processor_rev_id, equiv_id);
237 continue; /* different cpu */
239 if (fw_header->patch_id <= revision) {
240 WARNX(1, "patch_id %x, revision %x",
241 fw_header->patch_id, revision);
242 continue; /* not newer revision */
244 if (fw_header->nb_dev_id != 0 || fw_header->sb_dev_id != 0) {
245 WARNX(2, "Chipset-specific microcode is not supported");
248 WARNX(3, "selecting revision: %x", fw_header->patch_id);
249 revision = fw_header->patch_id;
250 selected_fw = fw_header;
251 selected_size = section_header->size;
255 WARNX(2, "%s is not a valid amd firmware: "
256 "file is truncated", path);
260 if (selected_fw != NULL) {
261 WARNX(1, "selected ucode size is %zu", selected_size);
262 fprintf(stderr, "%s: updating cpu %s to revision %#x... ",
263 path, dev, revision);
265 args.data = __DECONST(void *, selected_fw);
266 args.size = selected_size;
267 error = ioctl(devfd, CPUCTL_UPDATE, &args);
269 fprintf(stderr, "failed.\n");
273 fprintf(stderr, "done.\n");
276 msrargs.msr = MSR_BIOS_SIGN;
277 error = ioctl(devfd, CPUCTL_RDMSR, &msrargs);
279 WARN(0, "ioctl(%s)", dev);
282 new_rev = (uint32_t)msrargs.data;
283 if (new_rev != revision)
284 WARNX(0, "revision after update %#x", new_rev);