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