cpu_ucode_intel.c revision 1.14 1 1.14 msaitoh /* $NetBSD: cpu_ucode_intel.c,v 1.14 2018/04/12 10:30:24 msaitoh Exp $ */
2 1.1 drochner /*
3 1.1 drochner * Copyright (c) 2012 The NetBSD Foundation, Inc.
4 1.1 drochner * All rights reserved.
5 1.1 drochner *
6 1.1 drochner * This code is derived from software contributed to The NetBSD Foundation
7 1.1 drochner * by Matthias Drochner.
8 1.1 drochner *
9 1.1 drochner * Redistribution and use in source and binary forms, with or without
10 1.1 drochner * modification, are permitted provided that the following conditions
11 1.1 drochner * are met:
12 1.1 drochner * 1. Redistributions of source code must retain the above copyright
13 1.1 drochner * notice, this list of conditions and the following disclaimer.
14 1.1 drochner * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 drochner * notice, this list of conditions and the following disclaimer in the
16 1.1 drochner * documentation and/or other materials provided with the distribution.
17 1.1 drochner *
18 1.1 drochner * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 1.1 drochner * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 1.1 drochner * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 1.1 drochner * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 1.1 drochner * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 1.1 drochner * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 1.1 drochner * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 1.1 drochner * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 1.1 drochner * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 1.1 drochner * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 1.1 drochner * POSSIBILITY OF SUCH DAMAGE.
29 1.1 drochner */
30 1.1 drochner
31 1.1 drochner #include <sys/cdefs.h>
32 1.14 msaitoh __KERNEL_RCSID(0, "$NetBSD: cpu_ucode_intel.c,v 1.14 2018/04/12 10:30:24 msaitoh Exp $");
33 1.1 drochner
34 1.1 drochner #include "opt_xen.h"
35 1.1 drochner #include "opt_cpu_ucode.h"
36 1.1 drochner
37 1.1 drochner #include <sys/param.h>
38 1.1 drochner #include <sys/conf.h>
39 1.1 drochner #include <sys/cpuio.h>
40 1.1 drochner #include <sys/cpu.h>
41 1.1 drochner #include <sys/kmem.h>
42 1.1 drochner
43 1.1 drochner #include <machine/cpufunc.h>
44 1.1 drochner #include <machine/specialreg.h>
45 1.1 drochner #include <x86/cpu_ucode.h>
46 1.1 drochner
47 1.1 drochner static void
48 1.1 drochner intel_getcurrentucode(uint32_t *ucodeversion, int *platformid)
49 1.1 drochner {
50 1.1 drochner unsigned int unneeded_ids[4];
51 1.1 drochner uint64_t msr;
52 1.1 drochner
53 1.1 drochner kpreempt_disable();
54 1.1 drochner
55 1.6 msaitoh wrmsr(MSR_BIOS_SIGN, 0);
56 1.1 drochner x86_cpuid(0, unneeded_ids);
57 1.6 msaitoh msr = rdmsr(MSR_BIOS_SIGN);
58 1.1 drochner *ucodeversion = msr >> 32;
59 1.1 drochner
60 1.1 drochner kpreempt_enable();
61 1.1 drochner
62 1.1 drochner msr = rdmsr(MSR_IA32_PLATFORM_ID);
63 1.1 drochner *platformid = ((int)(msr >> 50)) & 7;
64 1.1 drochner }
65 1.1 drochner
66 1.1 drochner int
67 1.13 christos cpu_ucode_intel_get_version(struct cpu_ucode_version *ucode,
68 1.13 christos void *ptr, size_t len)
69 1.1 drochner {
70 1.1 drochner struct cpu_info *ci = curcpu();
71 1.13 christos struct cpu_ucode_version_intel1 *data = ptr;
72 1.1 drochner
73 1.1 drochner if (ucode->loader_version != CPU_UCODE_LOADER_INTEL1 ||
74 1.4 msaitoh CPUID_TO_FAMILY(ci->ci_signature) < 6)
75 1.1 drochner return EOPNOTSUPP;
76 1.1 drochner
77 1.13 christos if (len < sizeof(*data))
78 1.13 christos return ENOSPC;
79 1.1 drochner
80 1.13 christos intel_getcurrentucode(&data->ucodeversion, &data->platformid);
81 1.13 christos return 0;
82 1.1 drochner }
83 1.1 drochner
84 1.1 drochner int
85 1.1 drochner cpu_ucode_intel_firmware_open(firmware_handle_t *fwh, const char *fwname)
86 1.1 drochner {
87 1.1 drochner const char *fw_path = "cpu_x86_intel1";
88 1.1 drochner uint32_t ucodeversion, cpu_signature;
89 1.1 drochner int platformid;
90 1.1 drochner char cpuspec[11];
91 1.1 drochner
92 1.1 drochner if (fwname != NULL && fwname[0] != '\0')
93 1.1 drochner return firmware_open(fw_path, fwname, fwh);
94 1.1 drochner
95 1.1 drochner cpu_signature = curcpu()->ci_signature;
96 1.4 msaitoh if (CPUID_TO_FAMILY(cpu_signature) < 6)
97 1.1 drochner return EOPNOTSUPP;
98 1.1 drochner
99 1.1 drochner intel_getcurrentucode(&ucodeversion, &platformid);
100 1.5 christos snprintf(cpuspec, sizeof(cpuspec), "%08x-%d", cpu_signature,
101 1.5 christos platformid);
102 1.1 drochner
103 1.1 drochner return firmware_open(fw_path, cpuspec, fwh);
104 1.1 drochner }
105 1.1 drochner
106 1.1 drochner #ifndef XEN
107 1.14 msaitoh /* Check header version and checksum */
108 1.14 msaitoh static int
109 1.14 msaitoh cpu_ucode_intel_verify(struct intel1_ucode_header *buf)
110 1.14 msaitoh {
111 1.14 msaitoh uint32_t data_size, total_size, payload_size, extended_table_size;
112 1.14 msaitoh #if 0 /* not yet */
113 1.14 msaitoh struct intel1_ucode_ext_table *ext_table;
114 1.14 msaitoh struct intel1_ucode_proc_signature *ext_psig;
115 1.14 msaitoh #endif
116 1.14 msaitoh uint32_t sum;
117 1.14 msaitoh int i;
118 1.14 msaitoh
119 1.14 msaitoh if ((buf->uh_header_ver != 1) || (buf->uh_loader_rev != 1))
120 1.14 msaitoh return EINVAL;
121 1.14 msaitoh
122 1.14 msaitoh /* Data size */
123 1.14 msaitoh if (buf->uh_data_size == 0)
124 1.14 msaitoh data_size = 2000;
125 1.14 msaitoh else
126 1.14 msaitoh data_size = buf->uh_data_size;
127 1.14 msaitoh
128 1.14 msaitoh if ((data_size % 4) != 0) {
129 1.14 msaitoh /* Wrong size */
130 1.14 msaitoh return EINVAL;
131 1.14 msaitoh }
132 1.14 msaitoh
133 1.14 msaitoh /* Total size */
134 1.14 msaitoh if (buf->uh_total_size == 0)
135 1.14 msaitoh total_size = data_size + 48;
136 1.14 msaitoh else
137 1.14 msaitoh total_size = buf->uh_total_size;
138 1.14 msaitoh
139 1.14 msaitoh if ((total_size % 1024) != 0) {
140 1.14 msaitoh /* Wrong size */
141 1.14 msaitoh return EINVAL;
142 1.14 msaitoh }
143 1.14 msaitoh
144 1.14 msaitoh payload_size = data_size + 48;
145 1.14 msaitoh
146 1.14 msaitoh /* Extended table size */
147 1.14 msaitoh extended_table_size = total_size - payload_size;
148 1.14 msaitoh
149 1.14 msaitoh /*
150 1.14 msaitoh * Verify checksum of update data and header
151 1.14 msaitoh * (exclude extended signature).
152 1.14 msaitoh */
153 1.14 msaitoh sum = 0;
154 1.14 msaitoh for (i = 0; i < (payload_size / sizeof(uint32_t)); i++)
155 1.14 msaitoh sum += *((uint32_t *)buf + i);
156 1.14 msaitoh if (sum != 0) {
157 1.14 msaitoh /* Checksum mismatch */
158 1.14 msaitoh return EINVAL;
159 1.14 msaitoh }
160 1.14 msaitoh
161 1.14 msaitoh if (extended_table_size == 0)
162 1.14 msaitoh return 0;
163 1.14 msaitoh
164 1.14 msaitoh #if 0
165 1.14 msaitoh /* Verify extended signature's checksum */
166 1.14 msaitoh ext_table = (void *)buf + payload_size;
167 1.14 msaitoh ext_psig = (void *)ext_table + sizeof(struct intel1_ucode_ext_table);
168 1.14 msaitoh printf("ext_table = %p, extsig = %p\n", ext_table, ext_psig);
169 1.14 msaitoh #else
170 1.14 msaitoh printf("This image has extended signature table.");
171 1.14 msaitoh #endif
172 1.14 msaitoh
173 1.14 msaitoh return 0;
174 1.14 msaitoh }
175 1.14 msaitoh
176 1.1 drochner int
177 1.1 drochner cpu_ucode_intel_apply(struct cpu_ucode_softc *sc, int cpuno)
178 1.1 drochner {
179 1.1 drochner uint32_t ucodetarget, oucodeversion, nucodeversion;
180 1.10 jym int platformid, cpuid;
181 1.1 drochner struct intel1_ucode_header *uh;
182 1.9 mrg void *uha;
183 1.7 msaitoh size_t newbufsize = 0;
184 1.7 msaitoh int rv = 0;
185 1.1 drochner
186 1.1 drochner if (sc->loader_version != CPU_UCODE_LOADER_INTEL1
187 1.1 drochner || cpuno != CPU_UCODE_CURRENT_CPU)
188 1.1 drochner return EINVAL;
189 1.1 drochner
190 1.1 drochner uh = (struct intel1_ucode_header *)(sc->sc_blob);
191 1.14 msaitoh rv = cpu_ucode_intel_verify(uh);
192 1.14 msaitoh if (rv != 0)
193 1.1 drochner return EINVAL;
194 1.14 msaitoh
195 1.1 drochner ucodetarget = uh->uh_rev;
196 1.1 drochner
197 1.7 msaitoh if ((uintptr_t)(sc->sc_blob) & 15) {
198 1.7 msaitoh /* Make the buffer 16 byte aligned */
199 1.7 msaitoh newbufsize = sc->sc_blobsize + 15;
200 1.9 mrg uha = kmem_alloc(newbufsize, KM_SLEEP);
201 1.9 mrg uh = (struct intel1_ucode_header *)roundup2((uintptr_t)uha, 16);
202 1.7 msaitoh /* Copy to the new area */
203 1.7 msaitoh memcpy(uh, sc->sc_blob, sc->sc_blobsize);
204 1.7 msaitoh }
205 1.7 msaitoh
206 1.1 drochner kpreempt_disable();
207 1.1 drochner
208 1.1 drochner intel_getcurrentucode(&oucodeversion, &platformid);
209 1.1 drochner if (oucodeversion >= ucodetarget) {
210 1.1 drochner kpreempt_enable();
211 1.7 msaitoh rv = EEXIST; /* ??? */
212 1.7 msaitoh goto out;
213 1.1 drochner }
214 1.9 mrg wrmsr(MSR_BIOS_UPDT_TRIG, (uintptr_t)uh + 48);
215 1.1 drochner intel_getcurrentucode(&nucodeversion, &platformid);
216 1.10 jym cpuid = curcpu()->ci_index;
217 1.1 drochner
218 1.1 drochner kpreempt_enable();
219 1.1 drochner
220 1.7 msaitoh if (nucodeversion != ucodetarget) {
221 1.7 msaitoh rv = EIO;
222 1.7 msaitoh goto out;
223 1.7 msaitoh }
224 1.1 drochner
225 1.10 jym printf("cpu %d: ucode 0x%x->0x%x\n", cpuid,
226 1.1 drochner oucodeversion, nucodeversion);
227 1.7 msaitoh out:
228 1.7 msaitoh if (newbufsize != 0)
229 1.9 mrg kmem_free(uha, newbufsize);
230 1.7 msaitoh return rv;
231 1.1 drochner }
232 1.3 gdt #endif /* ! XEN */
233