tcp: Cache align ACK queue header.
[dragonfly.git] / usr.sbin / cpucontrol / intel.c
blob9c479d1a378327cf7debb53841d33cc24a63d160
1 /*-
2 * Copyright (c) 2006, 2008 Stanislav Sedov <stas@FreeBSD.org>.
3 * All rights reserved.
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.
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/cdefs.h>
27 __FBSDID("$FreeBSD: release/10.0.0/usr.sbin/cpucontrol/intel.c 245491 2013-01-16 05:00:51Z eadler $");
29 #include <assert.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
34 #include <fcntl.h>
35 #include <err.h>
36 #include <errno.h>
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #include <sys/mman.h>
41 #include <sys/ioctl.h>
42 #include <sys/ioccom.h>
43 #include <sys/cpuctl.h>
45 #include <machine/cpufunc.h>
46 #include <machine/specialreg.h>
48 #include "cpucontrol.h"
49 #include "intel.h"
51 #define DEFAULT_UCODE_SIZE 2000 /* Size of update data if not specified. */
53 int
54 intel_probe(int fd)
56 char vendor[13];
57 int error;
58 cpuctl_cpuid_args_t idargs = {
59 .level = 0,
62 error = ioctl(fd, CPUCTL_CPUID, &idargs);
63 if (error < 0) {
64 WARN(0, "ioctl()");
65 return (1);
67 ((uint32_t *)vendor)[0] = idargs.data[1];
68 ((uint32_t *)vendor)[1] = idargs.data[3];
69 ((uint32_t *)vendor)[2] = idargs.data[2];
70 vendor[12] = '\0';
71 if (strncmp(vendor, INTEL_VENDOR_ID, sizeof(INTEL_VENDOR_ID)) != 0)
72 return (1);
73 return (0);
76 void
77 intel_update(const char *dev, const char *path)
79 int fd, devfd;
80 struct stat st;
81 uint32_t *fw_image;
82 int have_ext_table;
83 uint32_t sum;
84 unsigned int i;
85 size_t payload_size;
86 intel_fw_header_t *fw_header;
87 intel_cpu_signature_t *ext_table;
88 intel_ext_header_t *ext_header;
89 uint32_t signature, flags;
90 int32_t revision;
91 ssize_t ext_size;
92 size_t ext_table_size;
93 void *fw_data;
94 size_t data_size, total_size;
95 cpuctl_msr_args_t msrargs = {
96 .msr = MSR_IA32_PLATFORM_ID,
98 cpuctl_cpuid_args_t idargs = {
99 .level = 1, /* Signature. */
101 cpuctl_update_args_t args;
102 int error;
104 assert(path);
105 assert(dev);
107 fd = -1;
108 fw_image = MAP_FAILED;
109 ext_table = NULL;
110 ext_header = NULL;
111 devfd = open(dev, O_RDWR);
112 if (devfd < 0) {
113 WARN(0, "could not open %s for writing", dev);
114 return;
116 error = ioctl(devfd, CPUCTL_CPUID, &idargs);
117 if (error < 0) {
118 WARN(0, "ioctl(%s)", dev);
119 goto fail;
121 signature = idargs.data[0];
122 error = ioctl(devfd, CPUCTL_RDMSR, &msrargs);
123 if (error < 0) {
124 WARN(0, "ioctl(%s)", dev);
125 goto fail;
129 * MSR_IA32_PLATFORM_ID contains flag in BCD in bits 52-50.
131 flags = 1 << ((msrargs.data >> 50) & 7);
132 msrargs.msr = MSR_BIOS_SIGN;
133 error = ioctl(devfd, CPUCTL_RDMSR, &msrargs);
134 if (error < 0) {
135 WARN(0, "ioctl(%s)", dev);
136 goto fail;
138 revision = msrargs.data >> 32; /* Revision in the high dword. */
139 WARNX(2, "found cpu type %#x family %#x model %#x stepping %#x.",
140 (signature >> 12) & 0x03, (signature >> 8) & 0x0f,
141 (signature >> 4) & 0x0f, (signature >> 0) & 0x0f);
143 * Open firmware image.
145 fd = open(path, O_RDONLY, 0);
146 if (fd < 0) {
147 WARN(0, "open(%s)", path);
148 return;
150 error = fstat(fd, &st);
151 if (error != 0) {
152 WARN(0, "fstat(%s)", path);
153 goto fail;
155 if (st.st_size < 0 || (unsigned)st.st_size < sizeof(*fw_header)) {
156 WARNX(2, "file too short: %s", path);
157 goto fail;
161 * mmap the whole image.
163 fw_image = (uint32_t *)mmap(NULL, st.st_size, PROT_READ,
164 MAP_PRIVATE, fd, 0);
165 if (fw_image == MAP_FAILED) {
166 WARN(0, "mmap(%s)", path);
167 goto fail;
169 fw_header = (intel_fw_header_t *)fw_image;
170 if (fw_header->header_version != INTEL_HEADER_VERSION ||
171 fw_header->loader_revision != INTEL_LOADER_REVISION) {
172 WARNX(2, "%s is not a valid intel firmware: version mismatch",
173 path);
174 goto fail;
177 * According to spec, if data_size == 0, then size of ucode = 2000.
179 if (fw_header->data_size == 0)
180 data_size = DEFAULT_UCODE_SIZE;
181 else
182 data_size = fw_header->data_size;
183 if (fw_header->total_size == 0)
184 total_size = data_size + sizeof(*fw_header);
185 else
186 total_size = fw_header->total_size;
187 if (total_size > (unsigned)st.st_size || st.st_size < 0) {
188 WARNX(2, "file too short: %s", path);
189 goto fail;
191 payload_size = data_size + sizeof(*fw_header);
194 * Check the primary checksum.
196 sum = 0;
197 for (i = 0; i < (payload_size / sizeof(uint32_t)); i++)
198 sum += *((uint32_t *)fw_image + i);
199 if (sum != 0) {
200 WARNX(2, "%s: update data checksum invalid", path);
201 goto fail;
205 * Check if there is an extended signature table.
207 ext_size = total_size - payload_size;
208 have_ext_table = 0;
210 if (ext_size > (signed)sizeof(*ext_header)) {
211 ext_header =
212 (intel_ext_header_t *)((char *)fw_image + payload_size);
213 ext_table = (intel_cpu_signature_t *)(ext_header + 1);
216 * Check the extended table size.
218 ext_table_size = sizeof(*ext_header) +
219 ext_header->sig_count * sizeof(*ext_table);
220 if (ext_table_size + payload_size > total_size) {
221 WARNX(2, "%s: broken extended signature table", path);
222 goto no_table;
226 * Check the extended table signature.
228 sum = 0;
229 for (i = 0; i < (ext_table_size / sizeof(uint32_t)); i++)
230 sum += *((uint32_t *)ext_header + i);
231 if (sum != 0) {
232 WARNX(2, "%s: extended signature table checksum invalid",
233 path);
234 goto no_table;
236 have_ext_table = 1;
239 no_table:
240 fw_data = fw_header + 1; /* Pointer to the update data. */
243 * Check if the given image is ok for this cpu.
245 if (signature == fw_header->cpu_signature &&
246 (flags & fw_header->cpu_flags) != 0)
247 goto matched;
248 else if (have_ext_table != 0) {
249 for (i = 0; i < ext_header->sig_count; i++) {
250 uint32_t sig = ext_table[i].cpu_signature;
251 if (signature == sig &&
252 (flags & ext_table[i].cpu_flags) != 0)
253 goto matched;
255 } else
256 goto fail;
258 matched:
259 if (revision >= fw_header->revision) {
260 WARNX(1, "skipping %s of rev %#x: up to date",
261 path, fw_header->revision);
262 return;
264 fprintf(stderr, "%s: updating cpu %s from rev %#x to rev %#x... ",
265 path, dev, revision, fw_header->revision);
266 args.data = fw_data;
267 args.size = data_size;
268 error = ioctl(devfd, CPUCTL_UPDATE, &args);
269 if (error < 0) {
270 error = errno;
271 fprintf(stderr, "failed.\n");
272 errno = error;
273 WARN(0, "ioctl()");
274 goto fail;
276 fprintf(stderr, "done.\n");
278 fail:
279 if (fw_image != MAP_FAILED)
280 if (munmap(fw_image, st.st_size) != 0)
281 warn("munmap(%s)", path);
282 if (devfd >= 0)
283 close(devfd);
284 if (fd >= 0)
285 close(fd);
286 return;