17534ac7aSMatthew N. Dodd /*- 27534ac7aSMatthew N. Dodd * Copyright (c) 2003 Matthew N. Dodd <winter@jurai.net> 37534ac7aSMatthew N. Dodd * All rights reserved. 47534ac7aSMatthew N. Dodd * 57534ac7aSMatthew N. Dodd * Redistribution and use in source and binary forms, with or without 67534ac7aSMatthew N. Dodd * modification, are permitted provided that the following conditions 77534ac7aSMatthew N. Dodd * are met: 87534ac7aSMatthew N. Dodd * 1. Redistributions of source code must retain the above copyright 97534ac7aSMatthew N. Dodd * notice, this list of conditions and the following disclaimer. 107534ac7aSMatthew N. Dodd * 2. Redistributions in binary form must reproduce the above copyright 117534ac7aSMatthew N. Dodd * notice, this list of conditions and the following disclaimer in the 127534ac7aSMatthew N. Dodd * documentation and/or other materials provided with the distribution. 137534ac7aSMatthew N. Dodd * 147534ac7aSMatthew N. Dodd * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 157534ac7aSMatthew N. Dodd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 167534ac7aSMatthew N. Dodd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 177534ac7aSMatthew N. Dodd * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 187534ac7aSMatthew N. Dodd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 197534ac7aSMatthew N. Dodd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 207534ac7aSMatthew N. Dodd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 217534ac7aSMatthew N. Dodd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 227534ac7aSMatthew N. Dodd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 237534ac7aSMatthew N. Dodd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 247534ac7aSMatthew N. Dodd * SUCH DAMAGE. 257534ac7aSMatthew N. Dodd */ 267534ac7aSMatthew N. Dodd 270f0f21c3SDavid E. O'Brien #include <sys/cdefs.h> 280f0f21c3SDavid E. O'Brien __FBSDID("$FreeBSD$"); 290f0f21c3SDavid E. O'Brien 307534ac7aSMatthew N. Dodd #include <sys/param.h> 317534ac7aSMatthew N. Dodd #include <sys/systm.h> 327534ac7aSMatthew N. Dodd #include <sys/kernel.h> 337534ac7aSMatthew N. Dodd 347534ac7aSMatthew N. Dodd #include <sys/module.h> 357534ac7aSMatthew N. Dodd #include <sys/bus.h> 367534ac7aSMatthew N. Dodd #include <sys/conf.h> 377534ac7aSMatthew N. Dodd 387534ac7aSMatthew N. Dodd #include <machine/bus.h> 397534ac7aSMatthew N. Dodd #include <machine/resource.h> 407534ac7aSMatthew N. Dodd #include <sys/rman.h> 417534ac7aSMatthew N. Dodd 4296aa4252SMatthew N. Dodd /* And all this for BIOS_PADDRTOVADDR() */ 4396aa4252SMatthew N. Dodd #include <vm/vm.h> 44092a5c45SJohn Baldwin #include <vm/vm_param.h> 4596aa4252SMatthew N. Dodd #include <vm/pmap.h> 4696aa4252SMatthew N. Dodd #include <machine/md_var.h> 4796aa4252SMatthew N. Dodd #include <machine/pc/bios.h> 4896aa4252SMatthew N. Dodd 497534ac7aSMatthew N. Dodd #include <machine/smapi.h> 5096aa4252SMatthew N. Dodd 5196aa4252SMatthew N. Dodd #define SMAPI_START 0xf0000 5296aa4252SMatthew N. Dodd #define SMAPI_STEP 0x10 5396aa4252SMatthew N. Dodd #define SMAPI_OFF 0 5496aa4252SMatthew N. Dodd #define SMAPI_LEN 4 5596aa4252SMatthew N. Dodd #define SMAPI_SIG "$SMB" 5696aa4252SMatthew N. Dodd 5796aa4252SMatthew N. Dodd #define RES2HDR(res) ((struct smapi_bios_header *)rman_get_virtual(res)) 58a954a4fbSMatthew N. Dodd #define ADDR2HDR(addr) ((struct smapi_bios_header *)BIOS_PADDRTOVADDR(addr)) 5996aa4252SMatthew N. Dodd 6096aa4252SMatthew N. Dodd struct smapi_softc { 6189c9c53dSPoul-Henning Kamp struct cdev *cdev; 6296aa4252SMatthew N. Dodd device_t dev; 6396aa4252SMatthew N. Dodd struct resource * res; 6496aa4252SMatthew N. Dodd int rid; 6596aa4252SMatthew N. Dodd 6696aa4252SMatthew N. Dodd u_int32_t smapi32_entry; 6796aa4252SMatthew N. Dodd 6896aa4252SMatthew N. Dodd struct smapi_bios_header *header; 6996aa4252SMatthew N. Dodd }; 707534ac7aSMatthew N. Dodd 71cba6ce36SMatthew N. Dodd extern u_long smapi32_offset; 72cba6ce36SMatthew N. Dodd extern u_short smapi32_segment; 737534ac7aSMatthew N. Dodd 747534ac7aSMatthew N. Dodd devclass_t smapi_devclass; 757534ac7aSMatthew N. Dodd 767534ac7aSMatthew N. Dodd static d_ioctl_t smapi_ioctl; 777534ac7aSMatthew N. Dodd 787534ac7aSMatthew N. Dodd static struct cdevsw smapi_cdevsw = { 79dc08ffecSPoul-Henning Kamp .d_version = D_VERSION, 807ac40f5fSPoul-Henning Kamp .d_ioctl = smapi_ioctl, 817ac40f5fSPoul-Henning Kamp .d_name = "smapi", 82dc08ffecSPoul-Henning Kamp .d_flags = D_MEM | D_NEEDGIANT, 837534ac7aSMatthew N. Dodd }; 847534ac7aSMatthew N. Dodd 8596aa4252SMatthew N. Dodd static void smapi_identify (driver_t *, device_t); 8696aa4252SMatthew N. Dodd static int smapi_probe (device_t); 8796aa4252SMatthew N. Dodd static int smapi_attach (device_t); 8896aa4252SMatthew N. Dodd static int smapi_detach (device_t); 8996aa4252SMatthew N. Dodd static int smapi_modevent (module_t, int, void *); 907534ac7aSMatthew N. Dodd 9196aa4252SMatthew N. Dodd static int smapi_header_cksum (struct smapi_bios_header *); 927534ac7aSMatthew N. Dodd 9396aa4252SMatthew N. Dodd extern int smapi32 (struct smapi_bios_parameter *, 9496aa4252SMatthew N. Dodd struct smapi_bios_parameter *); 9596aa4252SMatthew N. Dodd extern int smapi32_new (u_long, u_short, 9696aa4252SMatthew N. Dodd struct smapi_bios_parameter *, 9796aa4252SMatthew N. Dodd struct smapi_bios_parameter *); 987534ac7aSMatthew N. Dodd 997534ac7aSMatthew N. Dodd static int 1007534ac7aSMatthew N. Dodd smapi_ioctl (dev, cmd, data, fflag, td) 10189c9c53dSPoul-Henning Kamp struct cdev *dev; 1027534ac7aSMatthew N. Dodd u_long cmd; 1037534ac7aSMatthew N. Dodd caddr_t data; 1047534ac7aSMatthew N. Dodd int fflag; 1057534ac7aSMatthew N. Dodd d_thread_t * td; 1067534ac7aSMatthew N. Dodd { 1077534ac7aSMatthew N. Dodd struct smapi_softc *sc; 1087534ac7aSMatthew N. Dodd int error; 1097534ac7aSMatthew N. Dodd 1107534ac7aSMatthew N. Dodd error = 0; 1117534ac7aSMatthew N. Dodd sc = devclass_get_softc(smapi_devclass, minor(dev)); 1127534ac7aSMatthew N. Dodd if (sc == NULL) { 1137534ac7aSMatthew N. Dodd error = ENXIO; 1147534ac7aSMatthew N. Dodd goto fail; 1157534ac7aSMatthew N. Dodd } 1167534ac7aSMatthew N. Dodd 1177534ac7aSMatthew N. Dodd switch (cmd) { 1187534ac7aSMatthew N. Dodd case SMAPIOGHEADER: 1197534ac7aSMatthew N. Dodd bcopy((caddr_t)sc->header, data, 1207534ac7aSMatthew N. Dodd sizeof(struct smapi_bios_header)); 1217534ac7aSMatthew N. Dodd error = 0; 1227534ac7aSMatthew N. Dodd break; 1237534ac7aSMatthew N. Dodd case SMAPIOCGFUNCTION: 1247534ac7aSMatthew N. Dodd smapi32_offset = sc->smapi32_entry; 125cba6ce36SMatthew N. Dodd error = smapi32((struct smapi_bios_parameter *)data, 1267534ac7aSMatthew N. Dodd (struct smapi_bios_parameter *)data); 1277534ac7aSMatthew N. Dodd break; 1287534ac7aSMatthew N. Dodd default: 1297534ac7aSMatthew N. Dodd error = ENOTTY; 1307534ac7aSMatthew N. Dodd } 1317534ac7aSMatthew N. Dodd 1327534ac7aSMatthew N. Dodd fail: 1337534ac7aSMatthew N. Dodd return (error); 1347534ac7aSMatthew N. Dodd } 1357534ac7aSMatthew N. Dodd 13696aa4252SMatthew N. Dodd static int 13796aa4252SMatthew N. Dodd smapi_header_cksum (struct smapi_bios_header *header) 1387534ac7aSMatthew N. Dodd { 13996aa4252SMatthew N. Dodd u_int8_t *ptr; 14096aa4252SMatthew N. Dodd u_int8_t cksum; 14196aa4252SMatthew N. Dodd int i; 14296aa4252SMatthew N. Dodd 14396aa4252SMatthew N. Dodd ptr = (u_int8_t *)header; 14496aa4252SMatthew N. Dodd cksum = 0; 14596aa4252SMatthew N. Dodd for (i = 0; i < header->length; i++) { 14696aa4252SMatthew N. Dodd cksum += ptr[i]; 14796aa4252SMatthew N. Dodd } 14896aa4252SMatthew N. Dodd 14996aa4252SMatthew N. Dodd return (cksum); 15096aa4252SMatthew N. Dodd } 15196aa4252SMatthew N. Dodd 15296aa4252SMatthew N. Dodd static void 15396aa4252SMatthew N. Dodd smapi_identify (driver_t *driver, device_t parent) 15496aa4252SMatthew N. Dodd { 15596aa4252SMatthew N. Dodd device_t child; 15696aa4252SMatthew N. Dodd u_int32_t addr; 15796aa4252SMatthew N. Dodd int length; 15896aa4252SMatthew N. Dodd int rid; 15996aa4252SMatthew N. Dodd 16096aa4252SMatthew N. Dodd if (!device_is_alive(parent)) 16196aa4252SMatthew N. Dodd return; 16296aa4252SMatthew N. Dodd 16396aa4252SMatthew N. Dodd addr = bios_sigsearch(SMAPI_START, SMAPI_SIG, SMAPI_LEN, 16496aa4252SMatthew N. Dodd SMAPI_STEP, SMAPI_OFF); 16596aa4252SMatthew N. Dodd if (addr != 0) { 16696aa4252SMatthew N. Dodd rid = 0; 167a954a4fbSMatthew N. Dodd length = ADDR2HDR(addr)->length; 16896aa4252SMatthew N. Dodd 16996aa4252SMatthew N. Dodd child = BUS_ADD_CHILD(parent, 0, "smapi", -1); 17096aa4252SMatthew N. Dodd device_set_driver(child, driver); 17196aa4252SMatthew N. Dodd bus_set_resource(child, SYS_RES_MEMORY, rid, addr, length); 17296aa4252SMatthew N. Dodd device_set_desc(child, "SMAPI BIOS"); 17396aa4252SMatthew N. Dodd } 17496aa4252SMatthew N. Dodd 17596aa4252SMatthew N. Dodd return; 17696aa4252SMatthew N. Dodd } 17796aa4252SMatthew N. Dodd 17896aa4252SMatthew N. Dodd static int 17996aa4252SMatthew N. Dodd smapi_probe (device_t dev) 18096aa4252SMatthew N. Dodd { 18196aa4252SMatthew N. Dodd struct resource *res; 18296aa4252SMatthew N. Dodd int rid; 18396aa4252SMatthew N. Dodd int error; 18496aa4252SMatthew N. Dodd 18596aa4252SMatthew N. Dodd error = 0; 18696aa4252SMatthew N. Dodd rid = 0; 1875f96beb9SNate Lawson res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE); 18896aa4252SMatthew N. Dodd if (res == NULL) { 18996aa4252SMatthew N. Dodd device_printf(dev, "Unable to allocate memory resource.\n"); 19096aa4252SMatthew N. Dodd error = ENOMEM; 19196aa4252SMatthew N. Dodd goto bad; 19296aa4252SMatthew N. Dodd } 19396aa4252SMatthew N. Dodd 19496aa4252SMatthew N. Dodd if (smapi_header_cksum(RES2HDR(res))) { 19596aa4252SMatthew N. Dodd device_printf(dev, "SMAPI header checksum failed.\n"); 19696aa4252SMatthew N. Dodd error = ENXIO; 19796aa4252SMatthew N. Dodd goto bad; 19896aa4252SMatthew N. Dodd } 19996aa4252SMatthew N. Dodd 20096aa4252SMatthew N. Dodd bad: 20196aa4252SMatthew N. Dodd if (res) 20296aa4252SMatthew N. Dodd bus_release_resource(dev, SYS_RES_MEMORY, rid, res); 20396aa4252SMatthew N. Dodd return (error); 20496aa4252SMatthew N. Dodd } 20596aa4252SMatthew N. Dodd 20696aa4252SMatthew N. Dodd static int 20796aa4252SMatthew N. Dodd smapi_attach (device_t dev) 20896aa4252SMatthew N. Dodd { 20996aa4252SMatthew N. Dodd struct smapi_softc *sc; 21096aa4252SMatthew N. Dodd int error; 21196aa4252SMatthew N. Dodd 21296aa4252SMatthew N. Dodd sc = device_get_softc(dev); 21396aa4252SMatthew N. Dodd error = 0; 21496aa4252SMatthew N. Dodd 21596aa4252SMatthew N. Dodd sc->dev = dev; 21696aa4252SMatthew N. Dodd sc->rid = 0; 2175f96beb9SNate Lawson sc->res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->rid, 2185f96beb9SNate Lawson RF_ACTIVE); 21996aa4252SMatthew N. Dodd if (sc->res == NULL) { 22096aa4252SMatthew N. Dodd device_printf(dev, "Unable to allocate memory resource.\n"); 22196aa4252SMatthew N. Dodd error = ENOMEM; 22296aa4252SMatthew N. Dodd goto bad; 22396aa4252SMatthew N. Dodd } 22496aa4252SMatthew N. Dodd sc->header = (struct smapi_bios_header *)rman_get_virtual(sc->res); 22596aa4252SMatthew N. Dodd sc->smapi32_entry = (u_int32_t)BIOS_PADDRTOVADDR( 22696aa4252SMatthew N. Dodd sc->header->prot32_segment + 22796aa4252SMatthew N. Dodd sc->header->prot32_offset); 2287534ac7aSMatthew N. Dodd 2297534ac7aSMatthew N. Dodd sc->cdev = make_dev(&smapi_cdevsw, 2307534ac7aSMatthew N. Dodd device_get_unit(sc->dev), 2317534ac7aSMatthew N. Dodd UID_ROOT, GID_WHEEL, 0600, 2327534ac7aSMatthew N. Dodd "%s%d", 2337534ac7aSMatthew N. Dodd smapi_cdevsw.d_name, 2347534ac7aSMatthew N. Dodd device_get_unit(sc->dev)); 2357534ac7aSMatthew N. Dodd 23696aa4252SMatthew N. Dodd device_printf(dev, "Version: %d.%02d, Length: %d, Checksum: 0x%02x\n", 23796aa4252SMatthew N. Dodd bcd2bin(sc->header->version_major), 23896aa4252SMatthew N. Dodd bcd2bin(sc->header->version_minor), 23996aa4252SMatthew N. Dodd sc->header->length, 24096aa4252SMatthew N. Dodd sc->header->checksum); 24196aa4252SMatthew N. Dodd device_printf(dev, "Information=0x%b\n", 24296aa4252SMatthew N. Dodd sc->header->information, 24396aa4252SMatthew N. Dodd "\020" 24496aa4252SMatthew N. Dodd "\001REAL_VM86" 24596aa4252SMatthew N. Dodd "\002PROTECTED_16" 24696aa4252SMatthew N. Dodd "\003PROTECTED_32"); 24796aa4252SMatthew N. Dodd 24896aa4252SMatthew N. Dodd if (bootverbose) { 24996aa4252SMatthew N. Dodd if (sc->header->information & SMAPI_REAL_VM86) 25096aa4252SMatthew N. Dodd device_printf(dev, "Real/VM86 mode: Segment 0x%04x, Offset 0x%04x\n", 25196aa4252SMatthew N. Dodd sc->header->real16_segment, 25296aa4252SMatthew N. Dodd sc->header->real16_offset); 25396aa4252SMatthew N. Dodd if (sc->header->information & SMAPI_PROT_16BIT) 25496aa4252SMatthew N. Dodd device_printf(dev, "16-bit Protected mode: Segment 0x%08x, Offset 0x%04x\n", 25596aa4252SMatthew N. Dodd sc->header->prot16_segment, 25696aa4252SMatthew N. Dodd sc->header->prot16_offset); 25796aa4252SMatthew N. Dodd if (sc->header->information & SMAPI_PROT_32BIT) 25896aa4252SMatthew N. Dodd device_printf(dev, "32-bit Protected mode: Segment 0x%08x, Offset 0x%08x\n", 25996aa4252SMatthew N. Dodd sc->header->prot32_segment, 26096aa4252SMatthew N. Dodd sc->header->prot32_offset); 2617534ac7aSMatthew N. Dodd } 2627534ac7aSMatthew N. Dodd 26396aa4252SMatthew N. Dodd return (0); 26496aa4252SMatthew N. Dodd bad: 26596aa4252SMatthew N. Dodd if (sc->res) 26696aa4252SMatthew N. Dodd bus_release_resource(dev, SYS_RES_MEMORY, sc->rid, sc->res); 26796aa4252SMatthew N. Dodd return (error); 26896aa4252SMatthew N. Dodd } 26996aa4252SMatthew N. Dodd 27096aa4252SMatthew N. Dodd static int 27196aa4252SMatthew N. Dodd smapi_detach (device_t dev) 2727534ac7aSMatthew N. Dodd { 27396aa4252SMatthew N. Dodd struct smapi_softc *sc; 27496aa4252SMatthew N. Dodd 27596aa4252SMatthew N. Dodd sc = device_get_softc(dev); 2767534ac7aSMatthew N. Dodd 2777534ac7aSMatthew N. Dodd destroy_dev(sc->cdev); 27896aa4252SMatthew N. Dodd 27996aa4252SMatthew N. Dodd if (sc->res) 28096aa4252SMatthew N. Dodd bus_release_resource(dev, SYS_RES_MEMORY, sc->rid, sc->res); 28196aa4252SMatthew N. Dodd 2827534ac7aSMatthew N. Dodd return (0); 2837534ac7aSMatthew N. Dodd } 28496aa4252SMatthew N. Dodd 28596aa4252SMatthew N. Dodd static int 28696aa4252SMatthew N. Dodd smapi_modevent (mod, what, arg) 28796aa4252SMatthew N. Dodd module_t mod; 28896aa4252SMatthew N. Dodd int what; 28996aa4252SMatthew N. Dodd void * arg; 29096aa4252SMatthew N. Dodd { 29196aa4252SMatthew N. Dodd device_t * devs; 29296aa4252SMatthew N. Dodd int count; 29396aa4252SMatthew N. Dodd int i; 29496aa4252SMatthew N. Dodd 29596aa4252SMatthew N. Dodd switch (what) { 29696aa4252SMatthew N. Dodd case MOD_LOAD: 29796aa4252SMatthew N. Dodd break; 29896aa4252SMatthew N. Dodd case MOD_UNLOAD: 29996aa4252SMatthew N. Dodd devclass_get_devices(smapi_devclass, &devs, &count); 30096aa4252SMatthew N. Dodd for (i = 0; i < count; i++) { 30196aa4252SMatthew N. Dodd device_delete_child(device_get_parent(devs[i]), devs[i]); 30296aa4252SMatthew N. Dodd } 30396aa4252SMatthew N. Dodd break; 30496aa4252SMatthew N. Dodd default: 30596aa4252SMatthew N. Dodd break; 30696aa4252SMatthew N. Dodd } 30796aa4252SMatthew N. Dodd 30896aa4252SMatthew N. Dodd return (0); 30996aa4252SMatthew N. Dodd } 31096aa4252SMatthew N. Dodd 31196aa4252SMatthew N. Dodd static device_method_t smapi_methods[] = { 31296aa4252SMatthew N. Dodd /* Device interface */ 31396aa4252SMatthew N. Dodd DEVMETHOD(device_identify, smapi_identify), 31496aa4252SMatthew N. Dodd DEVMETHOD(device_probe, smapi_probe), 31596aa4252SMatthew N. Dodd DEVMETHOD(device_attach, smapi_attach), 31696aa4252SMatthew N. Dodd DEVMETHOD(device_detach, smapi_detach), 31796aa4252SMatthew N. Dodd { 0, 0 } 31896aa4252SMatthew N. Dodd }; 31996aa4252SMatthew N. Dodd 32096aa4252SMatthew N. Dodd static driver_t smapi_driver = { 32196aa4252SMatthew N. Dodd "smapi", 32296aa4252SMatthew N. Dodd smapi_methods, 32396aa4252SMatthew N. Dodd sizeof(struct smapi_softc), 32496aa4252SMatthew N. Dodd }; 32596aa4252SMatthew N. Dodd 32696aa4252SMatthew N. Dodd DRIVER_MODULE(smapi, nexus, smapi_driver, smapi_devclass, smapi_modevent, 0); 32796aa4252SMatthew N. Dodd MODULE_VERSION(smapi, 1); 328