cpu_ucode_intel.c revision 1.7
1/* $NetBSD: cpu_ucode_intel.c,v 1.7 2015/05/11 08:24:50 msaitoh 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.7 2015/05/11 08:24:50 msaitoh 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 49static void 50intel_getcurrentucode(uint32_t *ucodeversion, int *platformid) 51{ 52 unsigned int unneeded_ids[4]; 53 uint64_t msr; 54 55 kpreempt_disable(); 56 57 wrmsr(MSR_BIOS_SIGN, 0); 58 x86_cpuid(0, unneeded_ids); 59 msr = rdmsr(MSR_BIOS_SIGN); 60 *ucodeversion = msr >> 32; 61 62 kpreempt_enable(); 63 64 msr = rdmsr(MSR_IA32_PLATFORM_ID); 65 *platformid = ((int)(msr >> 50)) & 7; 66} 67 68int 69cpu_ucode_intel_get_version(struct cpu_ucode_version *ucode) 70{ 71 struct cpu_info *ci = curcpu(); 72 struct cpu_ucode_version_intel1 data; 73 74 if (ucode->loader_version != CPU_UCODE_LOADER_INTEL1 || 75 CPUID_TO_FAMILY(ci->ci_signature) < 6) 76 return EOPNOTSUPP; 77 if (!ucode->data) 78 return 0; 79 80 intel_getcurrentucode(&data.ucodeversion, &data.platformid); 81 82 return copyout(&data, ucode->data, sizeof(data)); 83} 84 85int 86cpu_ucode_intel_firmware_open(firmware_handle_t *fwh, const char *fwname) 87{ 88 const char *fw_path = "cpu_x86_intel1"; 89 uint32_t ucodeversion, cpu_signature; 90 int platformid; 91 char cpuspec[11]; 92 93 if (fwname != NULL && fwname[0] != '\0') 94 return firmware_open(fw_path, fwname, fwh); 95 96 cpu_signature = curcpu()->ci_signature; 97 if (CPUID_TO_FAMILY(cpu_signature) < 6) 98 return EOPNOTSUPP; 99 100 intel_getcurrentucode(&ucodeversion, &platformid); 101 snprintf(cpuspec, sizeof(cpuspec), "%08x-%d", cpu_signature, 102 platformid); 103 104 return firmware_open(fw_path, cpuspec, fwh); 105} 106 107#ifndef XEN 108int 109cpu_ucode_intel_apply(struct cpu_ucode_softc *sc, int cpuno) 110{ 111 uint32_t ucodetarget, oucodeversion, nucodeversion; 112 int platformid; 113 struct intel1_ucode_header *uh; 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 uh = kmem_alloc(newbufsize, KM_SLEEP); 130 if (uh == NULL) { 131 printf("%s: memory allocation failed\n", __func__); 132 return EINVAL; 133 } 134 uh = (struct intel1_ucode_header *) 135 (((unsigned long)uh + 15) & ~0x0000000f); 136 /* Copy to the new area */ 137 memcpy(uh, sc->sc_blob, sc->sc_blobsize); 138 } 139 140 kpreempt_disable(); 141 142 intel_getcurrentucode(&oucodeversion, &platformid); 143 if (oucodeversion >= ucodetarget) { 144 kpreempt_enable(); 145 rv = EEXIST; /* ??? */ 146 goto out; 147 } 148 wrmsr(MSR_BIOS_UPDT_TRIG, (uintptr_t)(sc->sc_blob) + 48); 149 intel_getcurrentucode(&nucodeversion, &platformid); 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", curcpu()->ci_index, 159 oucodeversion, nucodeversion); 160out: 161 if (newbufsize != 0) 162 kmem_free(uh, newbufsize); 163 return rv; 164} 165#endif /* ! XEN */ 166