1181430Sstas/*- 2181430Sstas * Copyright (c) 2006, 2008 Stanislav Sedov <stas@FreeBSD.org>. 3181430Sstas * All rights reserved. 4181430Sstas * 5181430Sstas * Redistribution and use in source and binary forms, with or without 6181430Sstas * modification, are permitted provided that the following conditions 7181430Sstas * are met: 8181430Sstas * 1. Redistributions of source code must retain the above copyright 9181430Sstas * notice, this list of conditions and the following disclaimer. 10181430Sstas * 2. Redistributions in binary form must reproduce the above copyright 11181430Sstas * notice, this list of conditions and the following disclaimer in the 12181430Sstas * documentation and/or other materials provided with the distribution. 13181430Sstas * 14181430Sstas * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15181430Sstas * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16181430Sstas * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17181430Sstas * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 18181430Sstas * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19181430Sstas * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20181430Sstas * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21181430Sstas * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22181430Sstas * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23181430Sstas * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24181430Sstas */ 25181430Sstas 26181430Sstas#include <sys/cdefs.h> 27181430Sstas__FBSDID("$FreeBSD$"); 28181430Sstas 29181430Sstas#include <assert.h> 30181430Sstas#include <stdio.h> 31181430Sstas#include <stdlib.h> 32181430Sstas#include <string.h> 33181430Sstas#include <unistd.h> 34181430Sstas#include <fcntl.h> 35181430Sstas#include <err.h> 36181430Sstas 37181430Sstas#include <sys/types.h> 38181430Sstas#include <sys/stat.h> 39181430Sstas#include <sys/mman.h> 40181430Sstas#include <sys/ioctl.h> 41181430Sstas#include <sys/ioccom.h> 42181430Sstas#include <sys/cpuctl.h> 43181430Sstas 44181430Sstas#include <machine/cpufunc.h> 45181430Sstas#include <machine/specialreg.h> 46181430Sstas 47181430Sstas#include "cpucontrol.h" 48181430Sstas#include "amd.h" 49181430Sstas 50181430Sstasint 51181430Sstasamd_probe(int fd) 52181430Sstas{ 53181430Sstas char vendor[13]; 54181430Sstas int error; 55181430Sstas cpuctl_cpuid_args_t idargs = { 56181430Sstas .level = 0, 57181430Sstas }; 58181430Sstas 59181430Sstas error = ioctl(fd, CPUCTL_CPUID, &idargs); 60181430Sstas if (error < 0) { 61181430Sstas WARN(0, "ioctl()"); 62181430Sstas return (1); 63181430Sstas } 64181430Sstas ((uint32_t *)vendor)[0] = idargs.data[1]; 65181430Sstas ((uint32_t *)vendor)[1] = idargs.data[3]; 66181430Sstas ((uint32_t *)vendor)[2] = idargs.data[2]; 67181430Sstas vendor[12] = '\0'; 68181430Sstas if (strncmp(vendor, AMD_VENDOR_ID, sizeof(AMD_VENDOR_ID)) != 0) 69181430Sstas return (1); 70181430Sstas return (0); 71181430Sstas} 72181430Sstas 73181430Sstasvoid 74181430Sstasamd_update(const char *dev, const char *path) 75181430Sstas{ 76181430Sstas int fd, devfd; 77181430Sstas unsigned int i; 78181430Sstas struct stat st; 79181430Sstas uint32_t *fw_image; 80181430Sstas amd_fw_header_t *fw_header; 81181430Sstas uint32_t sum; 82181430Sstas uint32_t signature; 83181430Sstas uint32_t *fw_data; 84181430Sstas size_t fw_size; 85181430Sstas cpuctl_cpuid_args_t idargs = { 86181430Sstas .level = 1, /* Request signature. */ 87181430Sstas }; 88181430Sstas cpuctl_update_args_t args; 89181430Sstas int error; 90181430Sstas 91181430Sstas assert(path); 92181430Sstas assert(dev); 93181430Sstas 94181430Sstas fd = -1; 95181430Sstas fw_image = MAP_FAILED; 96181430Sstas devfd = open(dev, O_RDWR); 97181430Sstas if (devfd < 0) { 98181430Sstas WARN(0, "could not open %s for writing", dev); 99181430Sstas return; 100181430Sstas } 101181430Sstas error = ioctl(devfd, CPUCTL_CPUID, &idargs); 102181430Sstas if (error < 0) { 103181430Sstas WARN(0, "ioctl()"); 104181430Sstas goto fail; 105181430Sstas } 106181430Sstas signature = idargs.data[0]; 107181430Sstas WARNX(2, "found cpu family %#x model %#x " 108181430Sstas "stepping %#x extfamily %#x extmodel %#x.", 109181430Sstas (signature >> 8) & 0x0f, (signature >> 4) & 0x0f, 110181430Sstas (signature >> 0) & 0x0f, (signature >> 20) & 0xff, 111181430Sstas (signature >> 16) & 0x0f); 112181430Sstas 113181430Sstas /* 114181430Sstas * Open the firmware file. 115181430Sstas */ 116181430Sstas fd = open(path, O_RDONLY, 0); 117181430Sstas if (fd < 0) { 118181430Sstas WARN(0, "open(%s)", path); 119181430Sstas goto fail; 120181430Sstas } 121181430Sstas error = fstat(fd, &st); 122181430Sstas if (error != 0) { 123181430Sstas WARN(0, "fstat(%s)", path); 124181430Sstas goto fail; 125181430Sstas } 126181430Sstas if (st.st_size < 0 || (unsigned)st.st_size < sizeof(*fw_header)) { 127181430Sstas WARNX(2, "file too short: %s", path); 128181430Sstas goto fail; 129181430Sstas } 130181430Sstas /* 131181430Sstas * mmap the whole image. 132181430Sstas */ 133181430Sstas fw_image = (uint32_t *)mmap(NULL, st.st_size, PROT_READ, 134181430Sstas MAP_PRIVATE, fd, 0); 135181430Sstas if (fw_image == MAP_FAILED) { 136181430Sstas WARN(0, "mmap(%s)", path); 137181430Sstas goto fail; 138181430Sstas } 139181430Sstas fw_header = (amd_fw_header_t *)fw_image; 140181430Sstas if ((fw_header->magic >> 8) != AMD_MAGIC) { 141181430Sstas WARNX(2, "%s is not a valid amd firmware: version mismatch", 142181430Sstas path); 143181430Sstas goto fail; 144181430Sstas } 145181430Sstas fw_data = (uint32_t *)(fw_header + 1); 146181430Sstas fw_size = (st.st_size - sizeof(*fw_header)) / sizeof(uint32_t); 147181430Sstas 148181430Sstas /* 149181430Sstas * Check the primary checksum. 150181430Sstas */ 151181430Sstas sum = 0; 152181430Sstas for (i = 0; i < fw_size; i++) 153181430Sstas sum += fw_data[i]; 154181430Sstas if (sum != fw_header->checksum) { 155181430Sstas WARNX(2, "%s: update data checksum invalid", path); 156181430Sstas goto fail; 157181430Sstas } 158181430Sstas if (signature == fw_header->signature) { 159181430Sstas fprintf(stderr, "%s: updating cpu %s... ", path, dev); 160181430Sstas 161181430Sstas args.data = fw_image; 162181430Sstas args.size = st.st_size; 163236504Savg error = ioctl(devfd, CPUCTL_UPDATE, &args); 164181430Sstas if (error < 0) { 165181430Sstas fprintf(stderr, "failed.\n"); 166181430Sstas warn("ioctl()"); 167181430Sstas goto fail; 168181430Sstas } 169181430Sstas fprintf(stderr, "done.\n"); 170181430Sstas } 171181430Sstas 172181430Sstasfail: 173181430Sstas if (fd >= 0) 174181430Sstas close(fd); 175181430Sstas if (devfd >= 0) 176181430Sstas close(devfd); 177181430Sstas if (fw_image != MAP_FAILED) 178181430Sstas if(munmap(fw_image, st.st_size) != 0) 179181430Sstas warn("munmap(%s)", path); 180181430Sstas return; 181181430Sstas} 182