Home | History | Annotate | Line # | Download | only in x86
cpu_ucode_intel.c revision 1.13
      1 /* $NetBSD: cpu_ucode_intel.c,v 1.13 2018/03/17 15:56:32 christos 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.13 2018/03/17 15:56:32 christos Exp $");
     33 
     34 #include "opt_xen.h"
     35 #include "opt_cpu_ucode.h"
     36 
     37 #include <sys/param.h>
     38 #include <sys/conf.h>
     39 #include <sys/cpuio.h>
     40 #include <sys/cpu.h>
     41 #include <sys/kmem.h>
     42 
     43 #include <machine/cpufunc.h>
     44 #include <machine/specialreg.h>
     45 #include <x86/cpu_ucode.h>
     46 
     47 static void
     48 intel_getcurrentucode(uint32_t *ucodeversion, int *platformid)
     49 {
     50 	unsigned int unneeded_ids[4];
     51 	uint64_t msr;
     52 
     53 	kpreempt_disable();
     54 
     55 	wrmsr(MSR_BIOS_SIGN, 0);
     56 	x86_cpuid(0, unneeded_ids);
     57 	msr = rdmsr(MSR_BIOS_SIGN);
     58 	*ucodeversion = msr >> 32;
     59 
     60 	kpreempt_enable();
     61 
     62 	msr = rdmsr(MSR_IA32_PLATFORM_ID);
     63 	*platformid = ((int)(msr >> 50)) & 7;
     64 }
     65 
     66 int
     67 cpu_ucode_intel_get_version(struct cpu_ucode_version *ucode,
     68     void *ptr, size_t len)
     69 {
     70 	struct cpu_info *ci = curcpu();
     71 	struct cpu_ucode_version_intel1 *data = ptr;
     72 
     73 	if (ucode->loader_version != CPU_UCODE_LOADER_INTEL1 ||
     74 	    CPUID_TO_FAMILY(ci->ci_signature) < 6)
     75 		return EOPNOTSUPP;
     76 
     77 	if (len < sizeof(*data))
     78 		return ENOSPC;
     79 
     80 	intel_getcurrentucode(&data->ucodeversion, &data->platformid);
     81 	return 0;
     82 }
     83 
     84 int
     85 cpu_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
    107 int
    108 cpu_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 		uh = (struct intel1_ucode_header *)roundup2((uintptr_t)uha, 16);
    131 		/* Copy to the new area */
    132 		memcpy(uh, sc->sc_blob, sc->sc_blobsize);
    133 	}
    134 
    135 	kpreempt_disable();
    136 
    137 	intel_getcurrentucode(&oucodeversion, &platformid);
    138 	if (oucodeversion >= ucodetarget) {
    139 		kpreempt_enable();
    140 		rv = EEXIST; /* ??? */
    141 		goto out;
    142 	}
    143 	wrmsr(MSR_BIOS_UPDT_TRIG, (uintptr_t)uh + 48);
    144 	intel_getcurrentucode(&nucodeversion, &platformid);
    145 	cpuid = curcpu()->ci_index;
    146 
    147 	kpreempt_enable();
    148 
    149 	if (nucodeversion != ucodetarget) {
    150 		rv = EIO;
    151 		goto out;
    152 	}
    153 
    154 	printf("cpu %d: ucode 0x%x->0x%x\n", cpuid,
    155 	       oucodeversion, nucodeversion);
    156 out:
    157 	if (newbufsize != 0)
    158 		kmem_free(uha, newbufsize);
    159 	return rv;
    160 }
    161 #endif /* ! XEN */
    162