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