cpu_ucode_intel.c revision 1.3 1 /* $NetBSD: cpu_ucode_intel.c,v 1.3 2013/07/06 12:11:54 gdt Exp $ */
2 /*
3 * Copyright (c) 2012 The NetBSD Foundation, Inc.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to The NetBSD Foundation
7 * by Matthias Drochner.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: cpu_ucode_intel.c,v 1.3 2013/07/06 12:11:54 gdt Exp $");
33
34 #include "opt_xen.h"
35 #include "opt_cpu_ucode.h"
36 #include "opt_compat_netbsd.h"
37
38 #include <sys/param.h>
39 #include <sys/conf.h>
40 #include <sys/cpuio.h>
41 #include <sys/cpu.h>
42 #include <sys/kmem.h>
43 #include <sys/xcall.h>
44
45 #include <machine/cpufunc.h>
46 #include <machine/specialreg.h>
47 #include <x86/cpu_ucode.h>
48
49 #define MSR_IA32_PLATFORM_ID 0x17
50 #define MSR_IA32_BIOS_UPDT_TRIGGER 0x79
51 #define MSR_IA32_BIOS_SIGN_ID 0x8b
52
53 static void
54 intel_getcurrentucode(uint32_t *ucodeversion, int *platformid)
55 {
56 unsigned int unneeded_ids[4];
57 uint64_t msr;
58
59 kpreempt_disable();
60
61 wrmsr(MSR_IA32_BIOS_SIGN_ID, 0);
62 x86_cpuid(0, unneeded_ids);
63 msr = rdmsr(MSR_IA32_BIOS_SIGN_ID);
64 *ucodeversion = msr >> 32;
65
66 kpreempt_enable();
67
68 msr = rdmsr(MSR_IA32_PLATFORM_ID);
69 *platformid = ((int)(msr >> 50)) & 7;
70 }
71
72 int
73 cpu_ucode_intel_get_version(struct cpu_ucode_version *ucode)
74 {
75 struct cpu_info *ci = curcpu();
76 struct cpu_ucode_version_intel1 data;
77
78 if (ucode->loader_version != CPU_UCODE_LOADER_INTEL1 ||
79 CPUID2FAMILY(ci->ci_signature) < 6)
80 return EOPNOTSUPP;
81 if (!ucode->data)
82 return 0;
83
84 intel_getcurrentucode(&data.ucodeversion, &data.platformid);
85
86 return copyout(&data, ucode->data, sizeof(data));
87 }
88
89 int
90 cpu_ucode_intel_firmware_open(firmware_handle_t *fwh, const char *fwname)
91 {
92 const char *fw_path = "cpu_x86_intel1";
93 uint32_t ucodeversion, cpu_signature;
94 int platformid;
95 char cpuspec[11];
96
97 if (fwname != NULL && fwname[0] != '\0')
98 return firmware_open(fw_path, fwname, fwh);
99
100 cpu_signature = curcpu()->ci_signature;
101 if (CPUID2FAMILY(cpu_signature) < 6)
102 return EOPNOTSUPP;
103
104 intel_getcurrentucode(&ucodeversion, &platformid);
105 sprintf(cpuspec, "%08x-%d", cpu_signature, platformid);
106
107 return firmware_open(fw_path, cpuspec, fwh);
108 }
109
110 #ifndef XEN
111 int
112 cpu_ucode_intel_apply(struct cpu_ucode_softc *sc, int cpuno)
113 {
114 uint32_t ucodetarget, oucodeversion, nucodeversion;
115 int platformid;
116 struct intel1_ucode_header *uh;
117
118 if (sc->loader_version != CPU_UCODE_LOADER_INTEL1
119 || cpuno != CPU_UCODE_CURRENT_CPU)
120 return EINVAL;
121
122 /* XXX relies on malloc alignment */
123 if ((uintptr_t)(sc->sc_blob) & 15) {
124 printf("ucode alignment bad\n");
125 return EINVAL;
126 }
127
128 uh = (struct intel1_ucode_header *)(sc->sc_blob);
129 if (uh->uh_header_ver != 1 || uh->uh_loader_rev != 1)
130 return EINVAL;
131 ucodetarget = uh->uh_rev;
132
133 kpreempt_disable();
134
135 intel_getcurrentucode(&oucodeversion, &platformid);
136 if (oucodeversion >= ucodetarget) {
137 kpreempt_enable();
138 return EEXIST; /* ??? */
139 }
140 wrmsr(MSR_IA32_BIOS_UPDT_TRIGGER, (uintptr_t)(sc->sc_blob) + 48);
141 intel_getcurrentucode(&nucodeversion, &platformid);
142
143 kpreempt_enable();
144
145 if (nucodeversion != ucodetarget)
146 return EIO;
147
148 printf("cpu %d: ucode 0x%x->0x%x\n", curcpu()->ci_index,
149 oucodeversion, nucodeversion);
150
151 return 0;
152 }
153 #endif /* ! XEN */
154