cpu_ucode_intel.c revision 1.5 1 1.5 christos /* $NetBSD: cpu_ucode_intel.c,v 1.5 2014/03/26 08:04:19 christos 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.5 christos __KERNEL_RCSID(0, "$NetBSD: cpu_ucode_intel.c,v 1.5 2014/03/26 08:04:19 christos 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 #define MSR_IA32_PLATFORM_ID 0x17
50 1.1 drochner #define MSR_IA32_BIOS_UPDT_TRIGGER 0x79
51 1.1 drochner #define MSR_IA32_BIOS_SIGN_ID 0x8b
52 1.1 drochner
53 1.1 drochner static void
54 1.1 drochner intel_getcurrentucode(uint32_t *ucodeversion, int *platformid)
55 1.1 drochner {
56 1.1 drochner unsigned int unneeded_ids[4];
57 1.1 drochner uint64_t msr;
58 1.1 drochner
59 1.1 drochner kpreempt_disable();
60 1.1 drochner
61 1.1 drochner wrmsr(MSR_IA32_BIOS_SIGN_ID, 0);
62 1.1 drochner x86_cpuid(0, unneeded_ids);
63 1.1 drochner msr = rdmsr(MSR_IA32_BIOS_SIGN_ID);
64 1.1 drochner *ucodeversion = msr >> 32;
65 1.1 drochner
66 1.1 drochner kpreempt_enable();
67 1.1 drochner
68 1.1 drochner msr = rdmsr(MSR_IA32_PLATFORM_ID);
69 1.1 drochner *platformid = ((int)(msr >> 50)) & 7;
70 1.1 drochner }
71 1.1 drochner
72 1.1 drochner int
73 1.1 drochner cpu_ucode_intel_get_version(struct cpu_ucode_version *ucode)
74 1.1 drochner {
75 1.1 drochner struct cpu_info *ci = curcpu();
76 1.1 drochner struct cpu_ucode_version_intel1 data;
77 1.1 drochner
78 1.1 drochner if (ucode->loader_version != CPU_UCODE_LOADER_INTEL1 ||
79 1.4 msaitoh CPUID_TO_FAMILY(ci->ci_signature) < 6)
80 1.1 drochner return EOPNOTSUPP;
81 1.1 drochner if (!ucode->data)
82 1.1 drochner return 0;
83 1.1 drochner
84 1.1 drochner intel_getcurrentucode(&data.ucodeversion, &data.platformid);
85 1.1 drochner
86 1.1 drochner return copyout(&data, ucode->data, sizeof(data));
87 1.1 drochner }
88 1.1 drochner
89 1.1 drochner int
90 1.1 drochner cpu_ucode_intel_firmware_open(firmware_handle_t *fwh, const char *fwname)
91 1.1 drochner {
92 1.1 drochner const char *fw_path = "cpu_x86_intel1";
93 1.1 drochner uint32_t ucodeversion, cpu_signature;
94 1.1 drochner int platformid;
95 1.1 drochner char cpuspec[11];
96 1.1 drochner
97 1.1 drochner if (fwname != NULL && fwname[0] != '\0')
98 1.1 drochner return firmware_open(fw_path, fwname, fwh);
99 1.1 drochner
100 1.1 drochner cpu_signature = curcpu()->ci_signature;
101 1.4 msaitoh if (CPUID_TO_FAMILY(cpu_signature) < 6)
102 1.1 drochner return EOPNOTSUPP;
103 1.1 drochner
104 1.1 drochner intel_getcurrentucode(&ucodeversion, &platformid);
105 1.5 christos snprintf(cpuspec, sizeof(cpuspec), "%08x-%d", cpu_signature,
106 1.5 christos platformid);
107 1.1 drochner
108 1.1 drochner return firmware_open(fw_path, cpuspec, fwh);
109 1.1 drochner }
110 1.1 drochner
111 1.1 drochner #ifndef XEN
112 1.1 drochner int
113 1.1 drochner cpu_ucode_intel_apply(struct cpu_ucode_softc *sc, int cpuno)
114 1.1 drochner {
115 1.1 drochner uint32_t ucodetarget, oucodeversion, nucodeversion;
116 1.1 drochner int platformid;
117 1.1 drochner struct intel1_ucode_header *uh;
118 1.1 drochner
119 1.1 drochner if (sc->loader_version != CPU_UCODE_LOADER_INTEL1
120 1.1 drochner || cpuno != CPU_UCODE_CURRENT_CPU)
121 1.1 drochner return EINVAL;
122 1.1 drochner
123 1.1 drochner /* XXX relies on malloc alignment */
124 1.1 drochner if ((uintptr_t)(sc->sc_blob) & 15) {
125 1.1 drochner printf("ucode alignment bad\n");
126 1.1 drochner return EINVAL;
127 1.1 drochner }
128 1.1 drochner
129 1.1 drochner uh = (struct intel1_ucode_header *)(sc->sc_blob);
130 1.1 drochner if (uh->uh_header_ver != 1 || uh->uh_loader_rev != 1)
131 1.1 drochner return EINVAL;
132 1.1 drochner ucodetarget = uh->uh_rev;
133 1.1 drochner
134 1.1 drochner kpreempt_disable();
135 1.1 drochner
136 1.1 drochner intel_getcurrentucode(&oucodeversion, &platformid);
137 1.1 drochner if (oucodeversion >= ucodetarget) {
138 1.1 drochner kpreempt_enable();
139 1.1 drochner return EEXIST; /* ??? */
140 1.1 drochner }
141 1.1 drochner wrmsr(MSR_IA32_BIOS_UPDT_TRIGGER, (uintptr_t)(sc->sc_blob) + 48);
142 1.1 drochner intel_getcurrentucode(&nucodeversion, &platformid);
143 1.1 drochner
144 1.1 drochner kpreempt_enable();
145 1.1 drochner
146 1.1 drochner if (nucodeversion != ucodetarget)
147 1.1 drochner return EIO;
148 1.1 drochner
149 1.1 drochner printf("cpu %d: ucode 0x%x->0x%x\n", curcpu()->ci_index,
150 1.1 drochner oucodeversion, nucodeversion);
151 1.1 drochner
152 1.1 drochner return 0;
153 1.1 drochner }
154 1.3 gdt #endif /* ! XEN */
155