Home | History | Annotate | Line # | Download | only in x86
cpu_ucode_intel.c revision 1.19
      1  1.19   msaitoh /* $NetBSD: cpu_ucode_intel.c,v 1.19 2022/09/15 01:30:56 msaitoh Exp $ */
      2  1.17      maxv 
      3   1.1  drochner /*
      4  1.17      maxv  * Copyright (c) 2012, 2019 The NetBSD Foundation, Inc.
      5   1.1  drochner  * All rights reserved.
      6   1.1  drochner  *
      7   1.1  drochner  * This code is derived from software contributed to The NetBSD Foundation
      8  1.17      maxv  * by Matthias Drochner and Maxime Villard.
      9   1.1  drochner  *
     10   1.1  drochner  * Redistribution and use in source and binary forms, with or without
     11   1.1  drochner  * modification, are permitted provided that the following conditions
     12   1.1  drochner  * are met:
     13   1.1  drochner  * 1. Redistributions of source code must retain the above copyright
     14   1.1  drochner  *    notice, this list of conditions and the following disclaimer.
     15   1.1  drochner  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1  drochner  *    notice, this list of conditions and the following disclaimer in the
     17   1.1  drochner  *    documentation and/or other materials provided with the distribution.
     18   1.1  drochner  *
     19   1.1  drochner  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.1  drochner  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.1  drochner  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.1  drochner  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.1  drochner  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.1  drochner  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.1  drochner  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.1  drochner  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.1  drochner  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.1  drochner  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.1  drochner  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1  drochner  */
     31   1.1  drochner 
     32   1.1  drochner #include <sys/cdefs.h>
     33  1.19   msaitoh __KERNEL_RCSID(0, "$NetBSD: cpu_ucode_intel.c,v 1.19 2022/09/15 01:30:56 msaitoh Exp $");
     34   1.1  drochner 
     35  1.15  pgoyette #ifdef _KERNEL_OPT
     36   1.1  drochner #include "opt_xen.h"
     37   1.1  drochner #include "opt_cpu_ucode.h"
     38  1.15  pgoyette #endif
     39   1.1  drochner 
     40   1.1  drochner #include <sys/param.h>
     41   1.1  drochner #include <sys/conf.h>
     42   1.1  drochner #include <sys/cpuio.h>
     43   1.1  drochner #include <sys/cpu.h>
     44   1.1  drochner #include <sys/kmem.h>
     45   1.1  drochner 
     46   1.1  drochner #include <machine/cpufunc.h>
     47   1.1  drochner #include <machine/specialreg.h>
     48   1.1  drochner #include <x86/cpu_ucode.h>
     49   1.1  drochner 
     50   1.1  drochner static void
     51   1.1  drochner intel_getcurrentucode(uint32_t *ucodeversion, int *platformid)
     52   1.1  drochner {
     53   1.1  drochner 	unsigned int unneeded_ids[4];
     54   1.1  drochner 	uint64_t msr;
     55   1.1  drochner 
     56   1.1  drochner 	kpreempt_disable();
     57   1.1  drochner 
     58   1.6   msaitoh 	wrmsr(MSR_BIOS_SIGN, 0);
     59   1.1  drochner 	x86_cpuid(0, unneeded_ids);
     60   1.6   msaitoh 	msr = rdmsr(MSR_BIOS_SIGN);
     61   1.1  drochner 	*ucodeversion = msr >> 32;
     62   1.1  drochner 
     63   1.1  drochner 	kpreempt_enable();
     64   1.1  drochner 
     65   1.1  drochner 	msr = rdmsr(MSR_IA32_PLATFORM_ID);
     66   1.1  drochner 	*platformid = ((int)(msr >> 50)) & 7;
     67   1.1  drochner }
     68   1.1  drochner 
     69   1.1  drochner int
     70  1.13  christos cpu_ucode_intel_get_version(struct cpu_ucode_version *ucode,
     71  1.13  christos     void *ptr, size_t len)
     72   1.1  drochner {
     73   1.1  drochner 	struct cpu_info *ci = curcpu();
     74  1.13  christos 	struct cpu_ucode_version_intel1 *data = ptr;
     75   1.1  drochner 
     76   1.1  drochner 	if (ucode->loader_version != CPU_UCODE_LOADER_INTEL1 ||
     77   1.4   msaitoh 	    CPUID_TO_FAMILY(ci->ci_signature) < 6)
     78   1.1  drochner 		return EOPNOTSUPP;
     79   1.1  drochner 
     80  1.13  christos 	if (len < sizeof(*data))
     81  1.13  christos 		return ENOSPC;
     82   1.1  drochner 
     83  1.13  christos 	intel_getcurrentucode(&data->ucodeversion, &data->platformid);
     84  1.13  christos 	return 0;
     85   1.1  drochner }
     86   1.1  drochner 
     87   1.1  drochner int
     88   1.1  drochner cpu_ucode_intel_firmware_open(firmware_handle_t *fwh, const char *fwname)
     89   1.1  drochner {
     90   1.1  drochner 	const char *fw_path = "cpu_x86_intel1";
     91   1.1  drochner 	uint32_t ucodeversion, cpu_signature;
     92  1.17      maxv 	char cpuspec[11];
     93   1.1  drochner 	int platformid;
     94   1.1  drochner 
     95   1.1  drochner 	if (fwname != NULL && fwname[0] != '\0')
     96   1.1  drochner 		return firmware_open(fw_path, fwname, fwh);
     97   1.1  drochner 
     98   1.1  drochner 	cpu_signature = curcpu()->ci_signature;
     99   1.4   msaitoh 	if (CPUID_TO_FAMILY(cpu_signature) < 6)
    100   1.1  drochner 		return EOPNOTSUPP;
    101   1.1  drochner 
    102   1.1  drochner 	intel_getcurrentucode(&ucodeversion, &platformid);
    103   1.5  christos 	snprintf(cpuspec, sizeof(cpuspec), "%08x-%d", cpu_signature,
    104   1.5  christos 	    platformid);
    105   1.1  drochner 
    106   1.1  drochner 	return firmware_open(fw_path, cpuspec, fwh);
    107   1.1  drochner }
    108   1.1  drochner 
    109  1.18    bouyer #ifndef XENPV
    110  1.14   msaitoh static int
    111  1.17      maxv cpu_ucode_intel_verify(struct cpu_ucode_softc *sc,
    112  1.17      maxv     struct intel1_ucode_header *buf)
    113  1.14   msaitoh {
    114  1.17      maxv 	uint32_t data_size, total_size, payload_size, ext_size;
    115  1.14   msaitoh 	uint32_t sum;
    116  1.14   msaitoh 	int i;
    117  1.17      maxv 
    118  1.14   msaitoh 	if ((buf->uh_header_ver != 1) || (buf->uh_loader_rev != 1))
    119  1.14   msaitoh 		return EINVAL;
    120  1.14   msaitoh 
    121  1.19   msaitoh 	/* Data size. */
    122  1.19   msaitoh 	if (buf->uh_data_size == 0)
    123  1.14   msaitoh 		data_size = 2000;
    124  1.19   msaitoh 	else
    125  1.14   msaitoh 		data_size = buf->uh_data_size;
    126  1.17      maxv 	if ((data_size % 4) != 0)
    127  1.17      maxv 		return EINVAL;
    128  1.17      maxv 	if (data_size > sc->sc_blobsize)
    129  1.14   msaitoh 		return EINVAL;
    130  1.14   msaitoh 
    131  1.19   msaitoh 	/* Total size. */
    132  1.19   msaitoh 	if (buf->uh_total_size == 0)
    133  1.14   msaitoh 		total_size = data_size + 48;
    134  1.19   msaitoh 	else
    135  1.14   msaitoh 		total_size = buf->uh_total_size;
    136  1.17      maxv 	if ((total_size % 1024) != 0)
    137  1.17      maxv 		return EINVAL;
    138  1.17      maxv 	if (total_size > sc->sc_blobsize)
    139  1.14   msaitoh 		return EINVAL;
    140  1.14   msaitoh 
    141  1.19   msaitoh 	/* Payload size. */
    142  1.14   msaitoh 	payload_size = data_size + 48;
    143  1.17      maxv 	if (payload_size > sc->sc_blobsize)
    144  1.17      maxv 		return EINVAL;
    145  1.14   msaitoh 
    146  1.14   msaitoh 	/*
    147  1.17      maxv 	 * Verify checksum of update data and header. Exclude extended
    148  1.17      maxv 	 * signature.
    149  1.14   msaitoh 	 */
    150  1.14   msaitoh 	sum = 0;
    151  1.17      maxv 	for (i = 0; i < (payload_size / sizeof(uint32_t)); i++) {
    152  1.14   msaitoh 		sum += *((uint32_t *)buf + i);
    153  1.17      maxv 	}
    154  1.17      maxv 	if (sum != 0)
    155  1.14   msaitoh 		return EINVAL;
    156  1.17      maxv 
    157  1.19   msaitoh 	/* Extended table size. Ignored for now. */
    158  1.17      maxv 	ext_size = total_size - payload_size;
    159  1.19   msaitoh 	if (ext_size > 0)
    160  1.19   msaitoh 		printf("This image has extended signature table.\n");
    161  1.14   msaitoh 
    162  1.14   msaitoh 	return 0;
    163  1.14   msaitoh }
    164  1.14   msaitoh 
    165   1.1  drochner int
    166   1.1  drochner cpu_ucode_intel_apply(struct cpu_ucode_softc *sc, int cpuno)
    167   1.1  drochner {
    168   1.1  drochner 	uint32_t ucodetarget, oucodeversion, nucodeversion;
    169   1.1  drochner 	struct intel1_ucode_header *uh;
    170  1.17      maxv 	int platformid, cpuid, error;
    171  1.17      maxv 	size_t newbufsize = 0;
    172   1.9       mrg 	void *uha;
    173   1.1  drochner 
    174  1.17      maxv 	if (sc->loader_version != CPU_UCODE_LOADER_INTEL1 ||
    175  1.17      maxv 	    cpuno != CPU_UCODE_CURRENT_CPU)
    176   1.1  drochner 		return EINVAL;
    177   1.1  drochner 
    178  1.17      maxv 	uh = (struct intel1_ucode_header *)sc->sc_blob;
    179  1.17      maxv 
    180  1.17      maxv 	error = cpu_ucode_intel_verify(sc, uh);
    181  1.17      maxv 	if (error != 0)
    182  1.17      maxv 		return error;
    183  1.14   msaitoh 
    184   1.1  drochner 	ucodetarget = uh->uh_rev;
    185   1.1  drochner 
    186  1.17      maxv 	if (((uintptr_t)sc->sc_blob) & 15) {
    187  1.17      maxv 		/* Make the buffer 16 byte aligned. */
    188   1.7   msaitoh 		newbufsize = sc->sc_blobsize + 15;
    189   1.9       mrg 		uha = kmem_alloc(newbufsize, KM_SLEEP);
    190  1.19   msaitoh 		uh =
    191  1.19   msaitoh 		    (struct intel1_ucode_header *)roundup2((uintptr_t)uha, 16);
    192   1.7   msaitoh 		memcpy(uh, sc->sc_blob, sc->sc_blobsize);
    193   1.7   msaitoh 	}
    194   1.7   msaitoh 
    195   1.1  drochner 	kpreempt_disable();
    196   1.1  drochner 
    197   1.1  drochner 	intel_getcurrentucode(&oucodeversion, &platformid);
    198   1.1  drochner 	if (oucodeversion >= ucodetarget) {
    199   1.1  drochner 		kpreempt_enable();
    200  1.17      maxv 		error = EEXIST;
    201   1.7   msaitoh 		goto out;
    202   1.1  drochner 	}
    203  1.16      maxv 
    204  1.16      maxv 	/*
    205  1.16      maxv 	 * Perform update. On some platforms a cache invalidation is
    206  1.16      maxv 	 * required.
    207  1.16      maxv 	 */
    208  1.16      maxv 	wbinvd();
    209   1.9       mrg 	wrmsr(MSR_BIOS_UPDT_TRIG, (uintptr_t)uh + 48);
    210  1.16      maxv 
    211   1.1  drochner 	intel_getcurrentucode(&nucodeversion, &platformid);
    212  1.10       jym 	cpuid = curcpu()->ci_index;
    213   1.1  drochner 
    214   1.1  drochner 	kpreempt_enable();
    215   1.1  drochner 
    216   1.7   msaitoh 	if (nucodeversion != ucodetarget) {
    217  1.17      maxv 		error = EIO;
    218   1.7   msaitoh 		goto out;
    219   1.7   msaitoh 	}
    220   1.1  drochner 
    221  1.17      maxv 	printf("cpu %d: ucode 0x%x->0x%x\n", cpuid, oucodeversion,
    222  1.17      maxv 	    nucodeversion);
    223  1.17      maxv 
    224   1.7   msaitoh out:
    225   1.7   msaitoh 	if (newbufsize != 0)
    226   1.9       mrg 		kmem_free(uha, newbufsize);
    227  1.17      maxv 	return error;
    228   1.1  drochner }
    229   1.3       gdt #endif /* ! XEN */
    230