netbsd_pci.c revision 8ce851d2
14f5e7dd7Smrg/* 24f5e7dd7Smrg * Copyright (c) 2008 Juan Romero Pardines 34f5e7dd7Smrg * Copyright (c) 2008 Mark Kettenis 48ce851d2Smacallan * Copyright (c) 2009 Michael Lorenz 54f5e7dd7Smrg * 64f5e7dd7Smrg * Permission to use, copy, modify, and distribute this software for any 74f5e7dd7Smrg * purpose with or without fee is hereby granted, provided that the above 84f5e7dd7Smrg * copyright notice and this permission notice appear in all copies. 94f5e7dd7Smrg * 104f5e7dd7Smrg * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 114f5e7dd7Smrg * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 124f5e7dd7Smrg * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 134f5e7dd7Smrg * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 144f5e7dd7Smrg * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 154f5e7dd7Smrg * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 164f5e7dd7Smrg * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 174f5e7dd7Smrg */ 184f5e7dd7Smrg 194f5e7dd7Smrg#include <sys/param.h> 204f5e7dd7Smrg#include <sys/ioctl.h> 214f5e7dd7Smrg#include <sys/mman.h> 224f5e7dd7Smrg#include <sys/types.h> 234f5e7dd7Smrg 2422df1cdfSmrg#ifdef HAVE_MTRR 254f5e7dd7Smrg#include <machine/sysarch.h> 264f5e7dd7Smrg#include <machine/mtrr.h> 2722df1cdfSmrg#define netbsd_set_mtrr(mr, num) _X86_SYSARCH_L(set_mtrr)(mr, num) 2822df1cdfSmrg#endif 294f5e7dd7Smrg 3022df1cdfSmrg#include <dev/pci/pcidevs.h> 314f5e7dd7Smrg#include <dev/pci/pciio.h> 324f5e7dd7Smrg#include <dev/pci/pcireg.h> 334f5e7dd7Smrg 344f5e7dd7Smrg#include <errno.h> 354f5e7dd7Smrg#include <fcntl.h> 364f5e7dd7Smrg#include <stdio.h> 374f5e7dd7Smrg#include <stdlib.h> 384f5e7dd7Smrg#include <string.h> 394f5e7dd7Smrg#include <unistd.h> 404f5e7dd7Smrg 414f5e7dd7Smrg 4222df1cdfSmrg#include <pci.h> 4322df1cdfSmrg 444f5e7dd7Smrg#include "pciaccess.h" 454f5e7dd7Smrg#include "pciaccess_private.h" 464f5e7dd7Smrg 478ce851d2Smacallantypedef struct _pcibus { 488ce851d2Smacallan int fd; /* /dev/pci* */ 498ce851d2Smacallan int num; /* bus number */ 508ce851d2Smacallan int maxdevs; /* maximum number of devices */ 518ce851d2Smacallan} PciBus; 529e884b7eSchristos 538ce851d2Smacallanstatic PciBus buses[32]; /* indexed by pci_device.domain */ 548ce851d2Smacallanstatic int nbuses = 0; /* number of buses found */ 558ce851d2Smacallan 568ce851d2Smacallan/* 578ce851d2Smacallan * NetBSD's userland has a /dev/pci* entry for each bus but userland has no way 588ce851d2Smacallan * to tell if a bus is a subordinate of another one or if it's on a different 598ce851d2Smacallan * host bridge. On some architectures ( macppc for example ) all root buses have 608ce851d2Smacallan * bus number 0 but on sparc64 for example the two roots in an Ultra60 have 618ce851d2Smacallan * different bus numbers - one is 0 and the other 128. 628ce851d2Smacallan * With each /dev/pci* we can map everything on the same root and we can also 638ce851d2Smacallan * see all devices on the same root, trying to do that causes problems though: 648ce851d2Smacallan * - since we can't tell which /dev/pci* is a subordinate we would find some 658ce851d2Smacallan * devices more than once 668ce851d2Smacallan * - we would have to guess subordinate bus numbers which is a waste of time 678ce851d2Smacallan * since we can ask each /dev/pci* for its bus number so we can scan only the 688ce851d2Smacallan * buses we know exist, not all 256 which may exist in each domain. 698ce851d2Smacallan * - some bus_space_mmap() methods may limit mappings to address ranges which 708ce851d2Smacallan * belong to known devices on that bus only. 718ce851d2Smacallan * Each host bridge may or may not have its own IO range, to avoid guesswork 728ce851d2Smacallan * here each /dev/pci* will let userland map its appropriate IO range at 738ce851d2Smacallan * PCI_MAGIC_IO_RANGE if defined in <machine/param.h> 748ce851d2Smacallan * With all this we should be able to use any PCI graphics device on any PCI 758ce851d2Smacallan * bus on any architecture as long as Xorg has a driver, without allowing 768ce851d2Smacallan * arbitrary mappings via /dev/mem and without userland having to know or care 778ce851d2Smacallan * about translating bus addresses to physical addresses or the other way 788ce851d2Smacallan * around. 798ce851d2Smacallan */ 804f5e7dd7Smrg 814f5e7dd7Smrgstatic int 828ce851d2Smacallanpci_read(int domain, int bus, int dev, int func, uint32_t reg, uint32_t *val) 834f5e7dd7Smrg{ 8422df1cdfSmrg uint32_t rval; 854f5e7dd7Smrg 868ce851d2Smacallan if ((domain < 0) || (domain > nbuses)) 878ce851d2Smacallan return -1; 888ce851d2Smacallan 898ce851d2Smacallan if (pcibus_conf_read(buses[domain].fd, (unsigned int)bus, 908ce851d2Smacallan (unsigned int)dev, (unsigned int)func, reg, &rval) == -1) 9122df1cdfSmrg return (-1); 924f5e7dd7Smrg 9322df1cdfSmrg *val = rval; 944f5e7dd7Smrg 954f5e7dd7Smrg return 0; 964f5e7dd7Smrg} 974f5e7dd7Smrg 984f5e7dd7Smrgstatic int 998ce851d2Smacallanpci_write(int domain, int bus, int dev, int func, uint32_t reg, uint32_t val) 1004f5e7dd7Smrg{ 1018ce851d2Smacallan 1028ce851d2Smacallan if ((domain < 0) || (domain > nbuses)) 1038ce851d2Smacallan return -1; 1048ce851d2Smacallan 1058ce851d2Smacallan return pcibus_conf_write(buses[domain].fd, (unsigned int)bus, 1068ce851d2Smacallan (unsigned int)dev, (unsigned int)func, reg, val); 1074f5e7dd7Smrg} 1084f5e7dd7Smrg 1094f5e7dd7Smrgstatic int 1108ce851d2Smacallanpci_nfuncs(int domain, int bus, int dev) 1114f5e7dd7Smrg{ 1124f5e7dd7Smrg uint32_t hdr; 1134f5e7dd7Smrg 1148ce851d2Smacallan if ((domain < 0) || (domain > nbuses)) 1158ce851d2Smacallan return -1; 1168ce851d2Smacallan 1178ce851d2Smacallan if (pci_read(domain, bus, dev, 0, PCI_BHLC_REG, &hdr) != 0) 1184f5e7dd7Smrg return -1; 1194f5e7dd7Smrg 1204f5e7dd7Smrg return (PCI_HDRTYPE_MULTIFN(hdr) ? 8 : 1); 1214f5e7dd7Smrg} 1224f5e7dd7Smrg 1239e884b7eSchristos/*ARGSUSED*/ 1244f5e7dd7Smrgstatic int 1254f5e7dd7Smrgpci_device_netbsd_map_range(struct pci_device *dev, 1264f5e7dd7Smrg struct pci_device_mapping *map) 1274f5e7dd7Smrg{ 12822df1cdfSmrg#ifdef HAVE_MTRR 12922df1cdfSmrg struct mtrr m; 13022df1cdfSmrg int n = 1; 13122df1cdfSmrg#endif 1328ce851d2Smacallan int prot, ret = 0; 1334f5e7dd7Smrg 13422df1cdfSmrg prot = PROT_READ; 1354f5e7dd7Smrg 1364f5e7dd7Smrg if (map->flags & PCI_DEV_MAP_FLAG_WRITABLE) 1374f5e7dd7Smrg prot |= PROT_WRITE; 1388ce851d2Smacallan map->memory = mmap(NULL, map->size, prot, MAP_SHARED, 1398ce851d2Smacallan buses[dev->domain].fd, (off_t)map->base); 1404f5e7dd7Smrg if (map->memory == MAP_FAILED) 1414f5e7dd7Smrg return errno; 1424f5e7dd7Smrg 14322df1cdfSmrg#ifdef HAVE_MTRR 14422df1cdfSmrg memset(&m, 0, sizeof(m)); 14522df1cdfSmrg 1464f5e7dd7Smrg /* No need to set an MTRR if it's the default mode. */ 1474f5e7dd7Smrg if ((map->flags & PCI_DEV_MAP_FLAG_CACHABLE) || 1484f5e7dd7Smrg (map->flags & PCI_DEV_MAP_FLAG_WRITE_COMBINE)) { 14922df1cdfSmrg m.base = base; 15022df1cdfSmrg m.flags = MTRR_VALID | MTRR_PRIVATE; 15122df1cdfSmrg m.len = size; 15222df1cdfSmrg m.owner = getpid(); 15322df1cdfSmrg if (map->flags & PCI_DEV_MAP_FLAG_CACHEABLE) 15422df1cdfSmrg m.type = MTRR_TYPE_WB; 1554f5e7dd7Smrg if (map->flags & PCI_DEV_MAP_FLAG_WRITE_COMBINE) 15622df1cdfSmrg m.type = MTRR_TYPE_WC; 15722df1cdfSmrg 15822df1cdfSmrg if ((netbsd_set_mtrr(&m, &n)) == -1) 15922df1cdfSmrg ret = errno; 1604f5e7dd7Smrg } 16122df1cdfSmrg#endif 1624f5e7dd7Smrg 16322df1cdfSmrg return ret; 1644f5e7dd7Smrg} 1654f5e7dd7Smrg 1664f5e7dd7Smrgstatic int 1674f5e7dd7Smrgpci_device_netbsd_unmap_range(struct pci_device *dev, 1684f5e7dd7Smrg struct pci_device_mapping *map) 1694f5e7dd7Smrg{ 17022df1cdfSmrg#ifdef HAVE_MTRR 17122df1cdfSmrg struct mtrr m; 17222df1cdfSmrg int n = 1; 17322df1cdfSmrg 17422df1cdfSmrg memset(&m, 0, sizeof(m)); 1754f5e7dd7Smrg 1764f5e7dd7Smrg if ((map->flags & PCI_DEV_MAP_FLAG_CACHABLE) || 1774f5e7dd7Smrg (map->flags & PCI_DEV_MAP_FLAG_WRITE_COMBINE)) { 17822df1cdfSmrg m.base = map->base; 17922df1cdfSmrg m.flags = 0; 18022df1cdfSmrg m.len = size; 18122df1cdfSmrg m.type = MTRR_TYPE_UC; 18222df1cdfSmrg (void)netbsd_set_mtrr(&m, &n); 1834f5e7dd7Smrg } 18422df1cdfSmrg#endif 1854f5e7dd7Smrg 1864f5e7dd7Smrg return pci_device_generic_unmap_range(dev, map); 1874f5e7dd7Smrg} 1884f5e7dd7Smrg 1894f5e7dd7Smrgstatic int 1904f5e7dd7Smrgpci_device_netbsd_read(struct pci_device *dev, void *data, 1914f5e7dd7Smrg pciaddr_t offset, pciaddr_t size, pciaddr_t *bytes_read) 1924f5e7dd7Smrg{ 19322df1cdfSmrg u_int reg, rval; 1944f5e7dd7Smrg 1954f5e7dd7Smrg *bytes_read = 0; 1964f5e7dd7Smrg while (size > 0) { 1979e884b7eSchristos size_t toread = MIN(size, 4 - (offset & 0x3)); 1984f5e7dd7Smrg 1999e884b7eSchristos reg = (u_int)(offset & ~0x3); 2004f5e7dd7Smrg 2018ce851d2Smacallan if ((pcibus_conf_read(buses[dev->domain].fd, 2028ce851d2Smacallan (unsigned int)dev->bus, (unsigned int)dev->dev, 2038ce851d2Smacallan (unsigned int)dev->func, reg, &rval)) == -1) 2044f5e7dd7Smrg return errno; 2054f5e7dd7Smrg 20622df1cdfSmrg rval = htole32(rval); 20722df1cdfSmrg rval >>= ((offset & 0x3) * 8); 2084f5e7dd7Smrg 20922df1cdfSmrg memcpy(data, &rval, toread); 2104f5e7dd7Smrg 2114f5e7dd7Smrg offset += toread; 2124f5e7dd7Smrg data = (char *)data + toread; 2134f5e7dd7Smrg size -= toread; 2144f5e7dd7Smrg *bytes_read += toread; 2154f5e7dd7Smrg } 2164f5e7dd7Smrg 2174f5e7dd7Smrg return 0; 2184f5e7dd7Smrg} 2194f5e7dd7Smrg 2204f5e7dd7Smrgstatic int 2214f5e7dd7Smrgpci_device_netbsd_write(struct pci_device *dev, const void *data, 2224f5e7dd7Smrg pciaddr_t offset, pciaddr_t size, pciaddr_t *bytes_written) 2234f5e7dd7Smrg{ 22422df1cdfSmrg u_int reg, val; 2254f5e7dd7Smrg 2264f5e7dd7Smrg if ((offset % 4) != 0 || (size % 4) != 0) 2274f5e7dd7Smrg return EINVAL; 2284f5e7dd7Smrg 2294f5e7dd7Smrg *bytes_written = 0; 2304f5e7dd7Smrg while (size > 0) { 2319e884b7eSchristos reg = (u_int)offset; 23222df1cdfSmrg memcpy(&val, data, 4); 2334f5e7dd7Smrg 2348ce851d2Smacallan if ((pcibus_conf_write(buses[dev->domain].fd, 2358ce851d2Smacallan (unsigned int)dev->bus, (unsigned int)dev->dev, 2368ce851d2Smacallan (unsigned int)dev->func, reg, val)) == -1) 2374f5e7dd7Smrg return errno; 2384f5e7dd7Smrg 2394f5e7dd7Smrg offset += 4; 2409e884b7eSchristos data = (const char *)data + 4; 2414f5e7dd7Smrg size -= 4; 2424f5e7dd7Smrg *bytes_written += 4; 2434f5e7dd7Smrg } 2444f5e7dd7Smrg 2454f5e7dd7Smrg return 0; 2464f5e7dd7Smrg} 2474f5e7dd7Smrg 2484f5e7dd7Smrgstatic void 2494f5e7dd7Smrgpci_system_netbsd_destroy(void) 2504f5e7dd7Smrg{ 2518ce851d2Smacallan int i; 2528ce851d2Smacallan 2538ce851d2Smacallan for (i = 0; i < nbuses; i++) { 2548ce851d2Smacallan close(buses[i].fd); 2558ce851d2Smacallan } 2564f5e7dd7Smrg free(pci_sys); 2574f5e7dd7Smrg pci_sys = NULL; 2584f5e7dd7Smrg} 2594f5e7dd7Smrg 2604f5e7dd7Smrgstatic int 2614f5e7dd7Smrgpci_device_netbsd_probe(struct pci_device *device) 2624f5e7dd7Smrg{ 2639e884b7eSchristos struct pci_device_private *priv = 2649e884b7eSchristos (struct pci_device_private *)(void *)device; 2654f5e7dd7Smrg struct pci_mem_region *region; 2664f5e7dd7Smrg uint64_t reg64, size64; 2674f5e7dd7Smrg uint32_t bar, reg, size; 2688ce851d2Smacallan int bus, dev, func, err, domain; 2694f5e7dd7Smrg 2708ce851d2Smacallan domain = device->domain; 2714f5e7dd7Smrg bus = device->bus; 2724f5e7dd7Smrg dev = device->dev; 2734f5e7dd7Smrg func = device->func; 2744f5e7dd7Smrg 2758ce851d2Smacallan err = pci_read(domain, bus, dev, func, PCI_BHLC_REG, ®); 2764f5e7dd7Smrg if (err) 2774f5e7dd7Smrg return err; 2784f5e7dd7Smrg 2794f5e7dd7Smrg priv->header_type = PCI_HDRTYPE_TYPE(reg); 2804f5e7dd7Smrg if (priv->header_type != 0) 2814f5e7dd7Smrg return 0; 2824f5e7dd7Smrg 2834f5e7dd7Smrg region = device->regions; 2844f5e7dd7Smrg for (bar = PCI_MAPREG_START; bar < PCI_MAPREG_END; 2854f5e7dd7Smrg bar += sizeof(uint32_t), region++) { 2868ce851d2Smacallan err = pci_read(domain, bus, dev, func, bar, ®); 2874f5e7dd7Smrg if (err) 2884f5e7dd7Smrg return err; 2894f5e7dd7Smrg 2904f5e7dd7Smrg /* Probe the size of the region. */ 2918ce851d2Smacallan err = pci_write(domain, bus, dev, func, bar, (unsigned int)~0); 2924f5e7dd7Smrg if (err) 2934f5e7dd7Smrg return err; 2948ce851d2Smacallan pci_read(domain, bus, dev, func, bar, &size); 2958ce851d2Smacallan pci_write(domain, bus, dev, func, bar, reg); 2964f5e7dd7Smrg 2974f5e7dd7Smrg if (PCI_MAPREG_TYPE(reg) == PCI_MAPREG_TYPE_IO) { 2984f5e7dd7Smrg region->is_IO = 1; 2994f5e7dd7Smrg region->base_addr = PCI_MAPREG_IO_ADDR(reg); 3004f5e7dd7Smrg region->size = PCI_MAPREG_IO_SIZE(size); 3014f5e7dd7Smrg } else { 3024f5e7dd7Smrg if (PCI_MAPREG_MEM_PREFETCHABLE(reg)) 3034f5e7dd7Smrg region->is_prefetchable = 1; 3044f5e7dd7Smrg switch(PCI_MAPREG_MEM_TYPE(reg)) { 3054f5e7dd7Smrg case PCI_MAPREG_MEM_TYPE_32BIT: 3064f5e7dd7Smrg case PCI_MAPREG_MEM_TYPE_32BIT_1M: 3074f5e7dd7Smrg region->base_addr = PCI_MAPREG_MEM_ADDR(reg); 3084f5e7dd7Smrg region->size = PCI_MAPREG_MEM_SIZE(size); 3094f5e7dd7Smrg break; 3104f5e7dd7Smrg case PCI_MAPREG_MEM_TYPE_64BIT: 3114f5e7dd7Smrg region->is_64 = 1; 3124f5e7dd7Smrg 3134f5e7dd7Smrg reg64 = reg; 3144f5e7dd7Smrg size64 = size; 3154f5e7dd7Smrg 3164f5e7dd7Smrg bar += sizeof(uint32_t); 3174f5e7dd7Smrg 3188ce851d2Smacallan err = pci_read(domain, bus, dev, func, bar, ®); 3194f5e7dd7Smrg if (err) 3204f5e7dd7Smrg return err; 3214f5e7dd7Smrg reg64 |= (uint64_t)reg << 32; 3224f5e7dd7Smrg 3238ce851d2Smacallan err = pci_write(domain, bus, dev, func, bar, 3249e884b7eSchristos (unsigned int)~0); 3254f5e7dd7Smrg if (err) 3264f5e7dd7Smrg return err; 3278ce851d2Smacallan pci_read(domain, bus, dev, func, bar, &size); 3288ce851d2Smacallan pci_write(domain, bus, dev, func, bar, 3299e884b7eSchristos (unsigned int)(reg64 >> 32)); 3304f5e7dd7Smrg size64 |= (uint64_t)size << 32; 3314f5e7dd7Smrg 3329e884b7eSchristos region->base_addr = 3339e884b7eSchristos (unsigned long)PCI_MAPREG_MEM64_ADDR(reg64); 3349e884b7eSchristos region->size = 3359e884b7eSchristos (unsigned long)PCI_MAPREG_MEM64_SIZE(size64); 3364f5e7dd7Smrg region++; 3374f5e7dd7Smrg break; 3384f5e7dd7Smrg } 3394f5e7dd7Smrg } 3404f5e7dd7Smrg } 3414f5e7dd7Smrg 3424f5e7dd7Smrg return 0; 3434f5e7dd7Smrg} 3444f5e7dd7Smrg 3459e884b7eSchristos/** 3469e884b7eSchristos * Read a VGA rom using the 0xc0000 mapping. 3479e884b7eSchristos * 3489e884b7eSchristos * This function should be extended to handle access through PCI resources, 3499e884b7eSchristos * which should be more reliable when available. 3509e884b7eSchristos */ 3519e884b7eSchristosstatic int 3529e884b7eSchristospci_device_netbsd_read_rom(struct pci_device *dev, void *buffer) 3539e884b7eSchristos{ 3549e884b7eSchristos struct pci_device_private *priv = (struct pci_device_private *)(void *)dev; 3559e884b7eSchristos void *bios; 3569e884b7eSchristos pciaddr_t rom_base; 3579e884b7eSchristos size_t rom_size; 3589e884b7eSchristos uint32_t bios_val, command_val; 3599e884b7eSchristos int pci_rom, memfd; 3609e884b7eSchristos 3619e884b7eSchristos if (((priv->base.device_class >> 16) & 0xff) != PCI_CLASS_DISPLAY || 3629e884b7eSchristos ((priv->base.device_class >> 8) & 0xff) != PCI_SUBCLASS_DISPLAY_VGA) 3639e884b7eSchristos return ENOSYS; 3649e884b7eSchristos 3659e884b7eSchristos if (priv->rom_base == 0) { 3669e884b7eSchristos#if defined(__amd64__) || defined(__i386__) 3679e884b7eSchristos rom_base = 0xc0000; 3689e884b7eSchristos rom_size = 0x10000; 3699e884b7eSchristos pci_rom = 0; 3709e884b7eSchristos#else 3719e884b7eSchristos return ENOSYS; 3729e884b7eSchristos#endif 3739e884b7eSchristos } else { 3749e884b7eSchristos rom_base = priv->rom_base; 3759e884b7eSchristos rom_size = dev->rom_size; 3769e884b7eSchristos pci_rom = 1; 3778ce851d2Smacallan if ((pcibus_conf_read(buses[dev->domain].fd, (unsigned int)dev->bus, 3789e884b7eSchristos (unsigned int)dev->dev, (unsigned int)dev->func, 3798ce851d2Smacallan PCI_COMMAND_STATUS_REG, &command_val)) == -1) 3809e884b7eSchristos return errno; 3818ce851d2Smacallan if ((command_val & PCI_COMMAND_MEM_ENABLE) == 0) { 3828ce851d2Smacallan if ((pcibus_conf_write(buses[dev->domain].fd, 3838ce851d2Smacallan (unsigned int)dev->bus, (unsigned int)dev->dev, 3848ce851d2Smacallan (unsigned int)dev->func, PCI_COMMAND_STATUS_REG, 3858ce851d2Smacallan command_val | PCI_COMMAND_MEM_ENABLE)) == -1) 3869e884b7eSchristos return errno; 3879e884b7eSchristos } 3888ce851d2Smacallan if ((pcibus_conf_read(buses[dev->domain].fd, (unsigned int)dev->bus, 3899e884b7eSchristos (unsigned int)dev->dev, (unsigned int)dev->func, 3908ce851d2Smacallan PCI_MAPREG_ROM, &bios_val)) == -1) 3919e884b7eSchristos return errno; 3928ce851d2Smacallan if ((bios_val & PCI_MAPREG_ROM_ENABLE) == 0) { 3938ce851d2Smacallan if ((pcibus_conf_write(buses[dev->domain].fd, 3948ce851d2Smacallan (unsigned int)dev->bus, 3959e884b7eSchristos (unsigned int)dev->dev, (unsigned int)dev->func, 3968ce851d2Smacallan PCI_MAPREG_ROM, bios_val | PCI_MAPREG_ROM_ENABLE)) == -1) 3979e884b7eSchristos return errno; 3989e884b7eSchristos } 3999e884b7eSchristos } 4009e884b7eSchristos 4019e884b7eSchristos fprintf(stderr, "Using rom_base = 0x%lx (pci_rom=%d)\n", (long)rom_base, 4029e884b7eSchristos pci_rom); 4039e884b7eSchristos memfd = open("/dev/mem", O_RDONLY); 4049e884b7eSchristos if (memfd == -1) 4059e884b7eSchristos return errno; 4069e884b7eSchristos 4079e884b7eSchristos bios = mmap(NULL, rom_size, PROT_READ, 0, memfd, (off_t)rom_base); 4089e884b7eSchristos if (bios == MAP_FAILED) { 4099e884b7eSchristos int serrno = errno; 4109e884b7eSchristos close(memfd); 4119e884b7eSchristos return serrno; 4129e884b7eSchristos } 4139e884b7eSchristos 4149e884b7eSchristos memcpy(buffer, bios, rom_size); 4159e884b7eSchristos 4169e884b7eSchristos munmap(bios, rom_size); 4179e884b7eSchristos close(memfd); 4189e884b7eSchristos 4199e884b7eSchristos if (pci_rom) { 4208ce851d2Smacallan if ((command_val & PCI_COMMAND_MEM_ENABLE) == 0) { 4218ce851d2Smacallan if ((pcibus_conf_write(buses[dev->domain].fd, 4228ce851d2Smacallan (unsigned int)dev->bus, 4239e884b7eSchristos (unsigned int)dev->dev, (unsigned int)dev->func, 4248ce851d2Smacallan PCI_COMMAND_STATUS_REG, command_val)) == -1) 4259e884b7eSchristos return errno; 4269e884b7eSchristos } 4278ce851d2Smacallan if ((bios_val & PCI_MAPREG_ROM_ENABLE) == 0) { 4288ce851d2Smacallan if ((pcibus_conf_write(buses[dev->domain].fd, 4298ce851d2Smacallan (unsigned int)dev->bus, 4309e884b7eSchristos (unsigned int)dev->dev, (unsigned int)dev->func, 4318ce851d2Smacallan PCI_MAPREG_ROM, bios_val)) == -1) 4329e884b7eSchristos return errno; 4339e884b7eSchristos } 4349e884b7eSchristos } 4359e884b7eSchristos 4369e884b7eSchristos return 0; 4379e884b7eSchristos} 4389e884b7eSchristos 4394f5e7dd7Smrgstatic const struct pci_system_methods netbsd_pci_methods = { 4409e884b7eSchristos .destroy = pci_system_netbsd_destroy, 4419e884b7eSchristos .destroy_device = NULL, 4429e884b7eSchristos .read_rom = pci_device_netbsd_read_rom, 4439e884b7eSchristos .probe = pci_device_netbsd_probe, 4449e884b7eSchristos .map_range = pci_device_netbsd_map_range, 4459e884b7eSchristos .unmap_range = pci_device_netbsd_unmap_range, 4469e884b7eSchristos .read = pci_device_netbsd_read, 4479e884b7eSchristos .write = pci_device_netbsd_write, 4489e884b7eSchristos .fill_capabilities = pci_fill_capabilities_generic 4494f5e7dd7Smrg}; 4504f5e7dd7Smrg 4514f5e7dd7Smrgint 4524f5e7dd7Smrgpci_system_netbsd_create(void) 4534f5e7dd7Smrg{ 4544f5e7dd7Smrg struct pci_device_private *device; 4558ce851d2Smacallan int bus, dev, func, ndevs, nfuncs, domain, pcifd; 4564f5e7dd7Smrg uint32_t reg; 4578ce851d2Smacallan char devname[32]; 4588ce851d2Smacallan struct pciio_businfo businfo; 4594f5e7dd7Smrg 4604f5e7dd7Smrg pci_sys = calloc(1, sizeof(struct pci_system)); 4614f5e7dd7Smrg 4624f5e7dd7Smrg pci_sys->methods = &netbsd_pci_methods; 4634f5e7dd7Smrg 4644f5e7dd7Smrg ndevs = 0; 4658ce851d2Smacallan nbuses = 0; 4668ce851d2Smacallan snprintf(devname, 32, "/dev/pci%d", nbuses); 4678ce851d2Smacallan pcifd = open(devname, O_RDWR); 4688ce851d2Smacallan while (pcifd > 0) { 4698ce851d2Smacallan ioctl(pcifd, PCI_IOC_BUSINFO, &businfo); 4708ce851d2Smacallan buses[nbuses].fd = pcifd; 4718ce851d2Smacallan buses[nbuses].num = bus = businfo.busno; 4728ce851d2Smacallan buses[nbuses].maxdevs = businfo.maxdevs; 4738ce851d2Smacallan domain = nbuses; 4748ce851d2Smacallan nbuses++; 4758ce851d2Smacallan for (dev = 0; dev < businfo.maxdevs; dev++) { 4768ce851d2Smacallan nfuncs = pci_nfuncs(domain, bus, dev); 4774f5e7dd7Smrg for (func = 0; func < nfuncs; func++) { 4788ce851d2Smacallan if (pci_read(domain, bus, dev, func, PCI_ID_REG, 4794f5e7dd7Smrg ®) != 0) 4804f5e7dd7Smrg continue; 4814f5e7dd7Smrg if (PCI_VENDOR(reg) == PCI_VENDOR_INVALID || 4824f5e7dd7Smrg PCI_VENDOR(reg) == 0) 4834f5e7dd7Smrg continue; 4844f5e7dd7Smrg 4854f5e7dd7Smrg ndevs++; 4864f5e7dd7Smrg } 4874f5e7dd7Smrg } 4888ce851d2Smacallan snprintf(devname, 32, "/dev/pci%d", nbuses); 4898ce851d2Smacallan pcifd = open(devname, O_RDWR); 4904f5e7dd7Smrg } 4914f5e7dd7Smrg 4924f5e7dd7Smrg pci_sys->num_devices = ndevs; 4934f5e7dd7Smrg pci_sys->devices = calloc(ndevs, sizeof(struct pci_device_private)); 4944f5e7dd7Smrg if (pci_sys->devices == NULL) { 4958ce851d2Smacallan int i; 4968ce851d2Smacallan 4978ce851d2Smacallan for (i = 0; i < nbuses; i++) 4988ce851d2Smacallan close(buses[i].fd); 4994f5e7dd7Smrg free(pci_sys); 5004f5e7dd7Smrg return ENOMEM; 5014f5e7dd7Smrg } 5024f5e7dd7Smrg 5034f5e7dd7Smrg device = pci_sys->devices; 5048ce851d2Smacallan for (domain = 0; domain < nbuses; domain++) { 5058ce851d2Smacallan bus = buses[domain].num; 5068ce851d2Smacallan for (dev = 0; dev < buses[domain].maxdevs; dev++) { 5078ce851d2Smacallan nfuncs = pci_nfuncs(domain, bus, dev); 5084f5e7dd7Smrg for (func = 0; func < nfuncs; func++) { 5098ce851d2Smacallan if (pci_read(domain, bus, dev, func, 5108ce851d2Smacallan PCI_ID_REG, ®) != 0) 5114f5e7dd7Smrg continue; 5124f5e7dd7Smrg if (PCI_VENDOR(reg) == PCI_VENDOR_INVALID || 5134f5e7dd7Smrg PCI_VENDOR(reg) == 0) 5144f5e7dd7Smrg continue; 5154f5e7dd7Smrg 5168ce851d2Smacallan device->base.domain = domain; 5174f5e7dd7Smrg device->base.bus = bus; 5184f5e7dd7Smrg device->base.dev = dev; 5194f5e7dd7Smrg device->base.func = func; 5204f5e7dd7Smrg device->base.vendor_id = PCI_VENDOR(reg); 5214f5e7dd7Smrg device->base.device_id = PCI_PRODUCT(reg); 5224f5e7dd7Smrg 5238ce851d2Smacallan if (pci_read(domain, bus, dev, func, 5248ce851d2Smacallan PCI_CLASS_REG, ®) != 0) 5254f5e7dd7Smrg continue; 5264f5e7dd7Smrg 5274f5e7dd7Smrg device->base.device_class = 5284f5e7dd7Smrg PCI_INTERFACE(reg) | PCI_CLASS(reg) << 16 | 5294f5e7dd7Smrg PCI_SUBCLASS(reg) << 8; 5304f5e7dd7Smrg device->base.revision = PCI_REVISION(reg); 5314f5e7dd7Smrg 5328ce851d2Smacallan if (pci_read(domain, bus, dev, func, 5338ce851d2Smacallan PCI_SUBSYS_ID_REG, ®) != 0) 5344f5e7dd7Smrg continue; 5354f5e7dd7Smrg 5364f5e7dd7Smrg device->base.subvendor_id = PCI_VENDOR(reg); 5374f5e7dd7Smrg device->base.subdevice_id = PCI_PRODUCT(reg); 5384f5e7dd7Smrg 5394f5e7dd7Smrg device++; 5404f5e7dd7Smrg } 5414f5e7dd7Smrg } 5424f5e7dd7Smrg } 5434f5e7dd7Smrg 5444f5e7dd7Smrg return 0; 5454f5e7dd7Smrg} 546