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