Home | History | Annotate | Line # | Download | only in x86
cpu_ucode_intel.c revision 1.13
      1  1.13  christos /* $NetBSD: cpu_ucode_intel.c,v 1.13 2018/03/17 15:56:32 christos Exp $ */
      2   1.1  drochner /*
      3   1.1  drochner  * Copyright (c) 2012 The NetBSD Foundation, Inc.
      4   1.1  drochner  * All rights reserved.
      5   1.1  drochner  *
      6   1.1  drochner  * This code is derived from software contributed to The NetBSD Foundation
      7   1.1  drochner  * by Matthias Drochner.
      8   1.1  drochner  *
      9   1.1  drochner  * Redistribution and use in source and binary forms, with or without
     10   1.1  drochner  * modification, are permitted provided that the following conditions
     11   1.1  drochner  * are met:
     12   1.1  drochner  * 1. Redistributions of source code must retain the above copyright
     13   1.1  drochner  *    notice, this list of conditions and the following disclaimer.
     14   1.1  drochner  * 2. Redistributions in binary form must reproduce the above copyright
     15   1.1  drochner  *    notice, this list of conditions and the following disclaimer in the
     16   1.1  drochner  *    documentation and/or other materials provided with the distribution.
     17   1.1  drochner  *
     18   1.1  drochner  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     19   1.1  drochner  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     20   1.1  drochner  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     21   1.1  drochner  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     22   1.1  drochner  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     23   1.1  drochner  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     24   1.1  drochner  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     25   1.1  drochner  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     26   1.1  drochner  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     27   1.1  drochner  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     28   1.1  drochner  * POSSIBILITY OF SUCH DAMAGE.
     29   1.1  drochner  */
     30   1.1  drochner 
     31   1.1  drochner #include <sys/cdefs.h>
     32  1.13  christos __KERNEL_RCSID(0, "$NetBSD: cpu_ucode_intel.c,v 1.13 2018/03/17 15:56:32 christos Exp $");
     33   1.1  drochner 
     34   1.1  drochner #include "opt_xen.h"
     35   1.1  drochner #include "opt_cpu_ucode.h"
     36   1.1  drochner 
     37   1.1  drochner #include <sys/param.h>
     38   1.1  drochner #include <sys/conf.h>
     39   1.1  drochner #include <sys/cpuio.h>
     40   1.1  drochner #include <sys/cpu.h>
     41   1.1  drochner #include <sys/kmem.h>
     42   1.1  drochner 
     43   1.1  drochner #include <machine/cpufunc.h>
     44   1.1  drochner #include <machine/specialreg.h>
     45   1.1  drochner #include <x86/cpu_ucode.h>
     46   1.1  drochner 
     47   1.1  drochner static void
     48   1.1  drochner intel_getcurrentucode(uint32_t *ucodeversion, int *platformid)
     49   1.1  drochner {
     50   1.1  drochner 	unsigned int unneeded_ids[4];
     51   1.1  drochner 	uint64_t msr;
     52   1.1  drochner 
     53   1.1  drochner 	kpreempt_disable();
     54   1.1  drochner 
     55   1.6   msaitoh 	wrmsr(MSR_BIOS_SIGN, 0);
     56   1.1  drochner 	x86_cpuid(0, unneeded_ids);
     57   1.6   msaitoh 	msr = rdmsr(MSR_BIOS_SIGN);
     58   1.1  drochner 	*ucodeversion = msr >> 32;
     59   1.1  drochner 
     60   1.1  drochner 	kpreempt_enable();
     61   1.1  drochner 
     62   1.1  drochner 	msr = rdmsr(MSR_IA32_PLATFORM_ID);
     63   1.1  drochner 	*platformid = ((int)(msr >> 50)) & 7;
     64   1.1  drochner }
     65   1.1  drochner 
     66   1.1  drochner int
     67  1.13  christos cpu_ucode_intel_get_version(struct cpu_ucode_version *ucode,
     68  1.13  christos     void *ptr, size_t len)
     69   1.1  drochner {
     70   1.1  drochner 	struct cpu_info *ci = curcpu();
     71  1.13  christos 	struct cpu_ucode_version_intel1 *data = ptr;
     72   1.1  drochner 
     73   1.1  drochner 	if (ucode->loader_version != CPU_UCODE_LOADER_INTEL1 ||
     74   1.4   msaitoh 	    CPUID_TO_FAMILY(ci->ci_signature) < 6)
     75   1.1  drochner 		return EOPNOTSUPP;
     76   1.1  drochner 
     77  1.13  christos 	if (len < sizeof(*data))
     78  1.13  christos 		return ENOSPC;
     79   1.1  drochner 
     80  1.13  christos 	intel_getcurrentucode(&data->ucodeversion, &data->platformid);
     81  1.13  christos 	return 0;
     82   1.1  drochner }
     83   1.1  drochner 
     84   1.1  drochner int
     85   1.1  drochner cpu_ucode_intel_firmware_open(firmware_handle_t *fwh, const char *fwname)
     86   1.1  drochner {
     87   1.1  drochner 	const char *fw_path = "cpu_x86_intel1";
     88   1.1  drochner 	uint32_t ucodeversion, cpu_signature;
     89   1.1  drochner 	int platformid;
     90   1.1  drochner 	char cpuspec[11];
     91   1.1  drochner 
     92   1.1  drochner 	if (fwname != NULL && fwname[0] != '\0')
     93   1.1  drochner 		return firmware_open(fw_path, fwname, fwh);
     94   1.1  drochner 
     95   1.1  drochner 	cpu_signature = curcpu()->ci_signature;
     96   1.4   msaitoh 	if (CPUID_TO_FAMILY(cpu_signature) < 6)
     97   1.1  drochner 		return EOPNOTSUPP;
     98   1.1  drochner 
     99   1.1  drochner 	intel_getcurrentucode(&ucodeversion, &platformid);
    100   1.5  christos 	snprintf(cpuspec, sizeof(cpuspec), "%08x-%d", cpu_signature,
    101   1.5  christos 	    platformid);
    102   1.1  drochner 
    103   1.1  drochner 	return firmware_open(fw_path, cpuspec, fwh);
    104   1.1  drochner }
    105   1.1  drochner 
    106   1.1  drochner #ifndef XEN
    107   1.1  drochner int
    108   1.1  drochner cpu_ucode_intel_apply(struct cpu_ucode_softc *sc, int cpuno)
    109   1.1  drochner {
    110   1.1  drochner 	uint32_t ucodetarget, oucodeversion, nucodeversion;
    111  1.10       jym 	int platformid, cpuid;
    112   1.1  drochner 	struct intel1_ucode_header *uh;
    113   1.9       mrg 	void *uha;
    114   1.7   msaitoh 	size_t newbufsize = 0;
    115   1.7   msaitoh 	int rv = 0;
    116   1.1  drochner 
    117   1.1  drochner 	if (sc->loader_version != CPU_UCODE_LOADER_INTEL1
    118   1.1  drochner 	    || cpuno != CPU_UCODE_CURRENT_CPU)
    119   1.1  drochner 		return EINVAL;
    120   1.1  drochner 
    121   1.1  drochner 	uh = (struct intel1_ucode_header *)(sc->sc_blob);
    122   1.1  drochner 	if (uh->uh_header_ver != 1 || uh->uh_loader_rev != 1)
    123   1.1  drochner 		return EINVAL;
    124   1.1  drochner 	ucodetarget = uh->uh_rev;
    125   1.1  drochner 
    126   1.7   msaitoh 	if ((uintptr_t)(sc->sc_blob) & 15) {
    127   1.7   msaitoh 		/* Make the buffer 16 byte aligned */
    128   1.7   msaitoh 		newbufsize = sc->sc_blobsize + 15;
    129   1.9       mrg 		uha = kmem_alloc(newbufsize, KM_SLEEP);
    130   1.9       mrg 		uh = (struct intel1_ucode_header *)roundup2((uintptr_t)uha, 16);
    131   1.7   msaitoh 		/* Copy to the new area */
    132   1.7   msaitoh 		memcpy(uh, sc->sc_blob, sc->sc_blobsize);
    133   1.7   msaitoh 	}
    134   1.7   msaitoh 
    135   1.1  drochner 	kpreempt_disable();
    136   1.1  drochner 
    137   1.1  drochner 	intel_getcurrentucode(&oucodeversion, &platformid);
    138   1.1  drochner 	if (oucodeversion >= ucodetarget) {
    139   1.1  drochner 		kpreempt_enable();
    140   1.7   msaitoh 		rv = EEXIST; /* ??? */
    141   1.7   msaitoh 		goto out;
    142   1.1  drochner 	}
    143   1.9       mrg 	wrmsr(MSR_BIOS_UPDT_TRIG, (uintptr_t)uh + 48);
    144   1.1  drochner 	intel_getcurrentucode(&nucodeversion, &platformid);
    145  1.10       jym 	cpuid = curcpu()->ci_index;
    146   1.1  drochner 
    147   1.1  drochner 	kpreempt_enable();
    148   1.1  drochner 
    149   1.7   msaitoh 	if (nucodeversion != ucodetarget) {
    150   1.7   msaitoh 		rv = EIO;
    151   1.7   msaitoh 		goto out;
    152   1.7   msaitoh 	}
    153   1.1  drochner 
    154  1.10       jym 	printf("cpu %d: ucode 0x%x->0x%x\n", cpuid,
    155   1.1  drochner 	       oucodeversion, nucodeversion);
    156   1.7   msaitoh out:
    157   1.7   msaitoh 	if (newbufsize != 0)
    158   1.9       mrg 		kmem_free(uha, newbufsize);
    159   1.7   msaitoh 	return rv;
    160   1.1  drochner }
    161   1.3       gdt #endif /* ! XEN */
    162