1/*-
2 * Copyright (c) 2012 Andriy Gapon <avg@FreeBSD.org>.
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__FBSDID("$FreeBSD$");
28
29#include <sys/types.h>
30#include <sys/stat.h>
31#include <sys/mman.h>
32#include <sys/ioctl.h>
33#include <sys/ioccom.h>
34#include <sys/cpuctl.h>
35
36#include <machine/cpufunc.h>
37#include <machine/specialreg.h>
38
39#include <assert.h>
40#include <stdio.h>
41#include <stdlib.h>
42#include <string.h>
43#include <unistd.h>
44#include <fcntl.h>
45#include <err.h>
46
47#include "cpucontrol.h"
48#include "amd.h"
49
50int
51amd10h_probe(int fd)
52{
53	char vendor[13];
54	cpuctl_cpuid_args_t idargs;
55	uint32_t family;
56	uint32_t signature;
57	int error;
58
59	idargs.level = 0;
60	error = ioctl(fd, CPUCTL_CPUID, &idargs);
61	if (error < 0) {
62		WARN(0, "ioctl()");
63		return (1);
64	}
65	((uint32_t *)vendor)[0] = idargs.data[1];
66	((uint32_t *)vendor)[1] = idargs.data[3];
67	((uint32_t *)vendor)[2] = idargs.data[2];
68	vendor[12] = '\0';
69	if (strncmp(vendor, AMD_VENDOR_ID, sizeof(AMD_VENDOR_ID)) != 0)
70		return (1);
71
72	idargs.level = 1;
73	error = ioctl(fd, CPUCTL_CPUID, &idargs);
74	if (error < 0) {
75		WARN(0, "ioctl()");
76		return (1);
77	}
78	signature = idargs.data[0];
79	family = ((signature >> 8) & 0x0f) + ((signature >> 20) & 0xff);
80	if (family < 0x10)
81		return (1);
82	return (0);
83}
84
85/*
86 * NB: the format of microcode update files is not documented by AMD.
87 * It has been reverse engineered from studying Coreboot, illumos and Linux
88 * source code.
89 */
90void
91amd10h_update(const struct ucode_update_params *params)
92{
93	cpuctl_cpuid_args_t idargs;
94	cpuctl_msr_args_t msrargs;
95	cpuctl_update_args_t args;
96	const amd_10h_fw_header_t *fw_header;
97	const amd_10h_fw_header_t *selected_fw;
98	const equiv_cpu_entry_t *equiv_cpu_table;
99	const section_header_t *section_header;
100	const container_header_t *container_header;
101	const uint8_t *fw_data;
102	const uint8_t *fw_image;
103	const char *dev, *path;
104	size_t fw_size;
105	size_t selected_size;
106	uint32_t revision;
107	uint32_t new_rev;
108	uint32_t signature;
109	uint16_t equiv_id;
110	int devfd;
111	unsigned int i;
112	int error;
113
114	dev = params->dev_path;
115	path = params->fw_path;
116	devfd = params->devfd;
117	fw_image = params->fwimage;
118	fw_size = params->fwsize;
119
120	assert(path);
121	assert(dev);
122
123	idargs.level = 1;
124	error = ioctl(devfd, CPUCTL_CPUID, &idargs);
125	if (error < 0) {
126		WARN(0, "ioctl()");
127		goto done;
128	}
129	signature = idargs.data[0];
130
131	msrargs.msr = MSR_BIOS_SIGN;
132	error = ioctl(devfd, CPUCTL_RDMSR, &msrargs);
133	if (error < 0) {
134		WARN(0, "ioctl(%s)", dev);
135		goto done;
136	}
137	revision = (uint32_t)msrargs.data;
138
139	WARNX(1, "found cpu family %#x model %#x "
140	    "stepping %#x extfamily %#x extmodel %#x.",
141	    ((signature >> 8) & 0x0f) + ((signature >> 20) & 0xff),
142	    (signature >> 4) & 0x0f,
143	    (signature >> 0) & 0x0f, (signature >> 20) & 0xff,
144	    (signature >> 16) & 0x0f);
145	WARNX(1, "microcode revision %#x", revision);
146
147	/*
148	 * Open the firmware file.
149	 */
150	WARNX(1, "checking %s for update.", path);
151	if (fw_size <
152	    (sizeof(*container_header) + sizeof(*section_header))) {
153		WARNX(2, "file too short: %s", path);
154		goto done;
155	}
156
157	/*
158	 * mmap the whole image.
159	 */
160	fw_data = fw_image;
161	container_header = (const container_header_t *)fw_data;
162	if (container_header->magic != AMD_10H_MAGIC) {
163		WARNX(2, "%s is not a valid amd firmware: bad magic", path);
164		goto done;
165	}
166	fw_data += sizeof(*container_header);
167	fw_size -= sizeof(*container_header);
168
169	section_header = (const section_header_t *)fw_data;
170	if (section_header->type != AMD_10H_EQUIV_TABLE_TYPE) {
171		WARNX(2, "%s is not a valid amd firmware: "
172		    "first section is not CPU equivalence table", path);
173		goto done;
174	}
175	if (section_header->size == 0) {
176		WARNX(2, "%s is not a valid amd firmware: "
177		    "first section is empty", path);
178		goto done;
179	}
180	fw_data += sizeof(*section_header);
181	fw_size -= sizeof(*section_header);
182
183	if (section_header->size > fw_size) {
184		WARNX(2, "%s is not a valid amd firmware: "
185		    "file is truncated", path);
186		goto done;
187	}
188	if (section_header->size < sizeof(*equiv_cpu_table)) {
189		WARNX(2, "%s is not a valid amd firmware: "
190		    "first section is too short", path);
191		goto done;
192	}
193	equiv_cpu_table = (const equiv_cpu_entry_t *)fw_data;
194	fw_data += section_header->size;
195	fw_size -= section_header->size;
196
197	equiv_id = 0;
198	for (i = 0; equiv_cpu_table[i].installed_cpu != 0; i++) {
199		if (signature == equiv_cpu_table[i].installed_cpu) {
200			equiv_id = equiv_cpu_table[i].equiv_cpu;
201			WARNX(3, "equiv_id: %x, signature %8x,"
202			    " equiv_cpu_table[%d] %8x", equiv_id, signature,
203			    i, equiv_cpu_table[i].installed_cpu);
204			break;
205		}
206	}
207	if (equiv_id == 0) {
208		WARNX(2, "CPU is not found in the equivalence table");
209		goto done;
210	}
211
212	selected_fw = NULL;
213	selected_size = 0;
214	while (fw_size >= sizeof(*section_header)) {
215		section_header = (const section_header_t *)fw_data;
216		fw_data += sizeof(*section_header);
217		fw_size -= sizeof(*section_header);
218		if (section_header->type != AMD_10H_uCODE_TYPE) {
219			WARNX(2, "%s is not a valid amd firmware: "
220			    "section has incorret type", path);
221			goto done;
222		}
223		if (section_header->size > fw_size) {
224			WARNX(2, "%s is not a valid amd firmware: "
225			    "file is truncated", path);
226			goto done;
227		}
228		if (section_header->size < sizeof(*fw_header)) {
229			WARNX(2, "%s is not a valid amd firmware: "
230			    "section is too short", path);
231			goto done;
232		}
233		fw_header = (const amd_10h_fw_header_t *)fw_data;
234		fw_data += section_header->size;
235		fw_size -= section_header->size;
236
237		if (fw_header->processor_rev_id != equiv_id) {
238			WARNX(1, "firmware processor_rev_id %x, equiv_id %x",
239			    fw_header->processor_rev_id, equiv_id);
240			continue; /* different cpu */
241		}
242		if (fw_header->patch_id <= revision) {
243			WARNX(1, "patch_id %x, revision %x",
244			    fw_header->patch_id, revision);
245			continue; /* not newer revision */
246		}
247		if (fw_header->nb_dev_id != 0 || fw_header->sb_dev_id != 0) {
248			WARNX(2, "Chipset-specific microcode is not supported");
249		}
250
251		WARNX(3, "selecting revision: %x", fw_header->patch_id);
252		revision = fw_header->patch_id;
253		selected_fw = fw_header;
254		selected_size = section_header->size;
255	}
256
257	if (fw_size != 0) {
258		WARNX(2, "%s is not a valid amd firmware: "
259		    "file is truncated", path);
260		goto done;
261	}
262
263	if (selected_fw != NULL) {
264		WARNX(1, "selected ucode size is %zu", selected_size);
265		fprintf(stderr, "%s: updating cpu %s to revision %#x... ",
266		    path, dev, revision);
267
268		args.data = __DECONST(void *, selected_fw);
269		args.size = selected_size;
270		error = ioctl(devfd, CPUCTL_UPDATE, &args);
271		if (error < 0) {
272			fprintf(stderr, "failed.\n");
273			warn("ioctl()");
274			goto done;
275		}
276		fprintf(stderr, "done.\n");
277	}
278
279	msrargs.msr = MSR_BIOS_SIGN;
280	error = ioctl(devfd, CPUCTL_RDMSR, &msrargs);
281	if (error < 0) {
282		WARN(0, "ioctl(%s)", dev);
283		goto done;
284	}
285	new_rev = (uint32_t)msrargs.data;
286	if (new_rev != revision)
287		WARNX(0, "revision after update %#x", new_rev);
288
289done:
290	return;
291}
292