1*c24c993fSbouyer /* $NetBSD: cpu_ucode.c,v 1.13 2020/04/25 15:26:18 bouyer Exp $ */
2a3f6c067Scegger /*
3a3f6c067Scegger * Copyright (c) 2012 The NetBSD Foundation, Inc.
4a3f6c067Scegger * All rights reserved.
5a3f6c067Scegger *
6a3f6c067Scegger * This code is derived from software contributed to The NetBSD Foundation
7a3f6c067Scegger * by Christoph Egger.
8a3f6c067Scegger *
9a3f6c067Scegger * Redistribution and use in source and binary forms, with or without
10a3f6c067Scegger * modification, are permitted provided that the following conditions
11a3f6c067Scegger * are met:
12a3f6c067Scegger * 1. Redistributions of source code must retain the above copyright
13a3f6c067Scegger * notice, this list of conditions and the following disclaimer.
14a3f6c067Scegger * 2. Redistributions in binary form must reproduce the above copyright
15a3f6c067Scegger * notice, this list of conditions and the following disclaimer in the
16a3f6c067Scegger * documentation and/or other materials provided with the distribution.
17a3f6c067Scegger *
18a3f6c067Scegger * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19a3f6c067Scegger * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20a3f6c067Scegger * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21a3f6c067Scegger * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22a3f6c067Scegger * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23a3f6c067Scegger * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24a3f6c067Scegger * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25a3f6c067Scegger * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26a3f6c067Scegger * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27a3f6c067Scegger * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28a3f6c067Scegger * POSSIBILITY OF SUCH DAMAGE.
29a3f6c067Scegger */
30a3f6c067Scegger
31a3f6c067Scegger #include <sys/cdefs.h>
32*c24c993fSbouyer __KERNEL_RCSID(0, "$NetBSD: cpu_ucode.c,v 1.13 2020/04/25 15:26:18 bouyer Exp $");
33a3f6c067Scegger
34d91f98a8Spgoyette #if defined(_KERNEL_OPT)
35a3f6c067Scegger #include "opt_cpu_ucode.h"
36cb69d2f4Schristos #include "opt_xen.h"
37d91f98a8Spgoyette #endif
38cb69d2f4Schristos
39a3f6c067Scegger #include <sys/param.h>
40a3f6c067Scegger #include <sys/cpuio.h>
41a3f6c067Scegger #include <sys/cpu.h>
42a3f6c067Scegger
43a3f6c067Scegger #include <dev/firmload.h>
44a3f6c067Scegger
45a3f6c067Scegger #include <machine/cpuvar.h>
46a3f6c067Scegger #include <machine/cputypes.h>
47a3f6c067Scegger
48a3f6c067Scegger #include <x86/cpu_ucode.h>
49a3f6c067Scegger
50cb69d2f4Schristos #ifdef XEN
51ac8432e2Scherry #include <xen/include/public/xen.h>
52cb69d2f4Schristos #include <xen/hypervisor.h>
53cb69d2f4Schristos #endif
54cb69d2f4Schristos
55a3f6c067Scegger static struct cpu_ucode_softc ucode_softc;
56a3f6c067Scegger
57a3f6c067Scegger int
cpu_ucode_get_version(struct cpu_ucode_version * data)58312c3390Sdrochner cpu_ucode_get_version(struct cpu_ucode_version *data)
59a3f6c067Scegger {
6069841942Schristos union {
6169841942Schristos struct cpu_ucode_version_amd a;
6269841942Schristos struct cpu_ucode_version_intel1 i;
6369841942Schristos } v;
6469841942Schristos size_t l;
6569841942Schristos int error;
6669841942Schristos
6769841942Schristos if (!data->data)
6869841942Schristos return 0;
69a3f6c067Scegger
70a3f6c067Scegger switch (cpu_vendor) {
71a3f6c067Scegger case CPUVENDOR_AMD:
72aa88658bSmaxv l = sizeof(v.a);
73aa88658bSmaxv error = cpu_ucode_amd_get_version(data, &v, l);
74aa88658bSmaxv break;
75312c3390Sdrochner case CPUVENDOR_INTEL:
76aa88658bSmaxv l = sizeof(v.i);
77aa88658bSmaxv error = cpu_ucode_intel_get_version(data, &v, l);
78aa88658bSmaxv break;
79a3f6c067Scegger default:
80312c3390Sdrochner return EOPNOTSUPP;
81312c3390Sdrochner }
82312c3390Sdrochner
8369841942Schristos if (error)
8469841942Schristos return error;
85312c3390Sdrochner
8669841942Schristos return copyout(&v, data->data, l);
87a3f6c067Scegger }
88a3f6c067Scegger
89a3f6c067Scegger int
cpu_ucode_md_open(firmware_handle_t * fwh,int loader_version,const char * fwname)90312c3390Sdrochner cpu_ucode_md_open(firmware_handle_t *fwh, int loader_version, const char *fwname)
91a3f6c067Scegger {
92a3f6c067Scegger switch (cpu_vendor) {
93a3f6c067Scegger case CPUVENDOR_AMD:
94a3f6c067Scegger return cpu_ucode_amd_firmware_open(fwh, fwname);
95a3f6c067Scegger case CPUVENDOR_INTEL:
96312c3390Sdrochner return cpu_ucode_intel_firmware_open(fwh, fwname);
97a3f6c067Scegger default:
98a3f6c067Scegger return EOPNOTSUPP;
99a3f6c067Scegger }
100a3f6c067Scegger }
101a3f6c067Scegger
102427af037Scherry #ifndef XENPV
103a3f6c067Scegger int
cpu_ucode_apply(const struct cpu_ucode * data)104312c3390Sdrochner cpu_ucode_apply(const struct cpu_ucode *data)
105a3f6c067Scegger {
106a3f6c067Scegger struct cpu_ucode_softc *sc = &ucode_softc;
107a3f6c067Scegger int error;
108a3f6c067Scegger
109312c3390Sdrochner sc->loader_version = data->loader_version;
110312c3390Sdrochner
111312c3390Sdrochner error = cpu_ucode_load(sc, data->fwname);
112a3f6c067Scegger if (error)
113a3f6c067Scegger return error;
114a3f6c067Scegger
115a3f6c067Scegger switch (cpu_vendor) {
116a3f6c067Scegger case CPUVENDOR_AMD:
117312c3390Sdrochner error = cpu_ucode_amd_apply(sc, data->cpu_nr);
118312c3390Sdrochner break;
119312c3390Sdrochner case CPUVENDOR_INTEL:
120312c3390Sdrochner error = cpu_ucode_intel_apply(sc, data->cpu_nr);
121a3f6c067Scegger break;
122a3f6c067Scegger default:
123cb69d2f4Schristos error = EOPNOTSUPP;
124a3f6c067Scegger }
125a3f6c067Scegger
126a3f6c067Scegger if (sc->sc_blob != NULL)
1273cde4cbcSozaki-r firmware_free(sc->sc_blob, sc->sc_blobsize);
128a3f6c067Scegger sc->sc_blob = NULL;
129a3f6c067Scegger sc->sc_blobsize = 0;
130a3f6c067Scegger return error;
131a3f6c067Scegger }
132cb69d2f4Schristos #else
133cb69d2f4Schristos int
cpu_ucode_apply(const struct cpu_ucode * data)134cb69d2f4Schristos cpu_ucode_apply(const struct cpu_ucode *data)
135cb69d2f4Schristos {
136cb69d2f4Schristos struct cpu_ucode_softc *sc = &ucode_softc;
137cb69d2f4Schristos struct xen_platform_op op;
138cb69d2f4Schristos int error;
139cb69d2f4Schristos
140cb69d2f4Schristos /* Xen updates all??? */
141cb69d2f4Schristos if (data->cpu_nr != CPU_UCODE_ALL_CPUS)
142cb69d2f4Schristos return EOPNOTSUPP;
143cb69d2f4Schristos
144cb69d2f4Schristos sc->loader_version = data->loader_version;
145cb69d2f4Schristos error = cpu_ucode_load(sc, data->fwname);
146cb69d2f4Schristos if (error)
147cb69d2f4Schristos return error;
148cb69d2f4Schristos
149cb69d2f4Schristos op.cmd = XENPF_microcode_update;
150cb69d2f4Schristos set_xen_guest_handle(op.u.microcode.data, sc->sc_blob);
151cb69d2f4Schristos op.u.microcode.length = sc->sc_blobsize;
152cb69d2f4Schristos
153cb69d2f4Schristos error = -HYPERVISOR_platform_op(&op);
154cb69d2f4Schristos
155cb69d2f4Schristos if (sc->sc_blob)
156cb69d2f4Schristos firmware_free(sc->sc_blob, sc->sc_blobsize);
157cb69d2f4Schristos sc->sc_blob = NULL;
158cb69d2f4Schristos sc->sc_blobsize = 0;
159cb69d2f4Schristos return error;
160cb69d2f4Schristos }
161*c24c993fSbouyer #endif /* XEN */
162