cpu_ucode_intel.c revision 1.7 1 1.7 msaitoh /* $NetBSD: cpu_ucode_intel.c,v 1.7 2015/05/11 08:24:50 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.7 msaitoh __KERNEL_RCSID(0, "$NetBSD: cpu_ucode_intel.c,v 1.7 2015/05/11 08:24:50 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.2 drochner #include "opt_compat_netbsd.h"
37 1.1 drochner
38 1.1 drochner #include <sys/param.h>
39 1.1 drochner #include <sys/conf.h>
40 1.1 drochner #include <sys/cpuio.h>
41 1.1 drochner #include <sys/cpu.h>
42 1.1 drochner #include <sys/kmem.h>
43 1.1 drochner #include <sys/xcall.h>
44 1.1 drochner
45 1.1 drochner #include <machine/cpufunc.h>
46 1.1 drochner #include <machine/specialreg.h>
47 1.1 drochner #include <x86/cpu_ucode.h>
48 1.1 drochner
49 1.1 drochner static void
50 1.1 drochner intel_getcurrentucode(uint32_t *ucodeversion, int *platformid)
51 1.1 drochner {
52 1.1 drochner unsigned int unneeded_ids[4];
53 1.1 drochner uint64_t msr;
54 1.1 drochner
55 1.1 drochner kpreempt_disable();
56 1.1 drochner
57 1.6 msaitoh wrmsr(MSR_BIOS_SIGN, 0);
58 1.1 drochner x86_cpuid(0, unneeded_ids);
59 1.6 msaitoh msr = rdmsr(MSR_BIOS_SIGN);
60 1.1 drochner *ucodeversion = msr >> 32;
61 1.1 drochner
62 1.1 drochner kpreempt_enable();
63 1.1 drochner
64 1.1 drochner msr = rdmsr(MSR_IA32_PLATFORM_ID);
65 1.1 drochner *platformid = ((int)(msr >> 50)) & 7;
66 1.1 drochner }
67 1.1 drochner
68 1.1 drochner int
69 1.1 drochner cpu_ucode_intel_get_version(struct cpu_ucode_version *ucode)
70 1.1 drochner {
71 1.1 drochner struct cpu_info *ci = curcpu();
72 1.1 drochner struct cpu_ucode_version_intel1 data;
73 1.1 drochner
74 1.1 drochner if (ucode->loader_version != CPU_UCODE_LOADER_INTEL1 ||
75 1.4 msaitoh CPUID_TO_FAMILY(ci->ci_signature) < 6)
76 1.1 drochner return EOPNOTSUPP;
77 1.1 drochner if (!ucode->data)
78 1.1 drochner return 0;
79 1.1 drochner
80 1.1 drochner intel_getcurrentucode(&data.ucodeversion, &data.platformid);
81 1.1 drochner
82 1.1 drochner return copyout(&data, ucode->data, sizeof(data));
83 1.1 drochner }
84 1.1 drochner
85 1.1 drochner int
86 1.1 drochner cpu_ucode_intel_firmware_open(firmware_handle_t *fwh, const char *fwname)
87 1.1 drochner {
88 1.1 drochner const char *fw_path = "cpu_x86_intel1";
89 1.1 drochner uint32_t ucodeversion, cpu_signature;
90 1.1 drochner int platformid;
91 1.1 drochner char cpuspec[11];
92 1.1 drochner
93 1.1 drochner if (fwname != NULL && fwname[0] != '\0')
94 1.1 drochner return firmware_open(fw_path, fwname, fwh);
95 1.1 drochner
96 1.1 drochner cpu_signature = curcpu()->ci_signature;
97 1.4 msaitoh if (CPUID_TO_FAMILY(cpu_signature) < 6)
98 1.1 drochner return EOPNOTSUPP;
99 1.1 drochner
100 1.1 drochner intel_getcurrentucode(&ucodeversion, &platformid);
101 1.5 christos snprintf(cpuspec, sizeof(cpuspec), "%08x-%d", cpu_signature,
102 1.5 christos platformid);
103 1.1 drochner
104 1.1 drochner return firmware_open(fw_path, cpuspec, fwh);
105 1.1 drochner }
106 1.1 drochner
107 1.1 drochner #ifndef XEN
108 1.1 drochner int
109 1.1 drochner cpu_ucode_intel_apply(struct cpu_ucode_softc *sc, int cpuno)
110 1.1 drochner {
111 1.1 drochner uint32_t ucodetarget, oucodeversion, nucodeversion;
112 1.1 drochner int platformid;
113 1.1 drochner struct intel1_ucode_header *uh;
114 1.7 msaitoh size_t newbufsize = 0;
115 1.7 msaitoh int rv = 0;
116 1.1 drochner
117 1.1 drochner if (sc->loader_version != CPU_UCODE_LOADER_INTEL1
118 1.1 drochner || cpuno != CPU_UCODE_CURRENT_CPU)
119 1.1 drochner return EINVAL;
120 1.1 drochner
121 1.1 drochner uh = (struct intel1_ucode_header *)(sc->sc_blob);
122 1.1 drochner if (uh->uh_header_ver != 1 || uh->uh_loader_rev != 1)
123 1.1 drochner return EINVAL;
124 1.1 drochner ucodetarget = uh->uh_rev;
125 1.1 drochner
126 1.7 msaitoh if ((uintptr_t)(sc->sc_blob) & 15) {
127 1.7 msaitoh /* Make the buffer 16 byte aligned */
128 1.7 msaitoh newbufsize = sc->sc_blobsize + 15;
129 1.7 msaitoh uh = kmem_alloc(newbufsize, KM_SLEEP);
130 1.7 msaitoh if (uh == NULL) {
131 1.7 msaitoh printf("%s: memory allocation failed\n", __func__);
132 1.7 msaitoh return EINVAL;
133 1.7 msaitoh }
134 1.7 msaitoh uh = (struct intel1_ucode_header *)
135 1.7 msaitoh (((unsigned long)uh + 15) & ~0x0000000f);
136 1.7 msaitoh /* Copy to the new area */
137 1.7 msaitoh memcpy(uh, sc->sc_blob, sc->sc_blobsize);
138 1.7 msaitoh }
139 1.7 msaitoh
140 1.1 drochner kpreempt_disable();
141 1.1 drochner
142 1.1 drochner intel_getcurrentucode(&oucodeversion, &platformid);
143 1.1 drochner if (oucodeversion >= ucodetarget) {
144 1.1 drochner kpreempt_enable();
145 1.7 msaitoh rv = EEXIST; /* ??? */
146 1.7 msaitoh goto out;
147 1.1 drochner }
148 1.6 msaitoh wrmsr(MSR_BIOS_UPDT_TRIG, (uintptr_t)(sc->sc_blob) + 48);
149 1.1 drochner intel_getcurrentucode(&nucodeversion, &platformid);
150 1.1 drochner
151 1.1 drochner kpreempt_enable();
152 1.1 drochner
153 1.7 msaitoh if (nucodeversion != ucodetarget) {
154 1.7 msaitoh rv = EIO;
155 1.7 msaitoh goto out;
156 1.7 msaitoh }
157 1.1 drochner
158 1.1 drochner printf("cpu %d: ucode 0x%x->0x%x\n", curcpu()->ci_index,
159 1.1 drochner oucodeversion, nucodeversion);
160 1.7 msaitoh out:
161 1.7 msaitoh if (newbufsize != 0)
162 1.7 msaitoh kmem_free(uh, newbufsize);
163 1.7 msaitoh return rv;
164 1.1 drochner }
165 1.3 gdt #endif /* ! XEN */
166