netbsd_pci.c revision 74c741d0
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 2756c7645b9Smrg /* Enable the device if necessary */ 2766c7645b9Smrg err = pci_read(domain, bus, dev, func, PCI_COMMAND_STATUS_REG, ®); 2776c7645b9Smrg if (err) 2786c7645b9Smrg return err; 2796c7645b9Smrg if ((reg & (PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE | PCI_COMMAND_MASTER_ENABLE)) != 2806c7645b9Smrg (PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE | PCI_COMMAND_MASTER_ENABLE)) { 2816c7645b9Smrg reg |= PCI_COMMAND_IO_ENABLE | 2826c7645b9Smrg PCI_COMMAND_MEM_ENABLE | 2836c7645b9Smrg PCI_COMMAND_MASTER_ENABLE; 2846c7645b9Smrg err = pci_write(domain, bus, dev, func, PCI_COMMAND_STATUS_REG, 2856c7645b9Smrg reg); 2866c7645b9Smrg if (err) 2876c7645b9Smrg return err; 2886c7645b9Smrg } 2896c7645b9Smrg 2908ce851d2Smacallan err = pci_read(domain, bus, dev, func, PCI_BHLC_REG, ®); 2914f5e7dd7Smrg if (err) 2924f5e7dd7Smrg return err; 2934f5e7dd7Smrg 2944f5e7dd7Smrg priv->header_type = PCI_HDRTYPE_TYPE(reg); 2954f5e7dd7Smrg if (priv->header_type != 0) 2964f5e7dd7Smrg return 0; 2974f5e7dd7Smrg 2984f5e7dd7Smrg region = device->regions; 2994f5e7dd7Smrg for (bar = PCI_MAPREG_START; bar < PCI_MAPREG_END; 3004f5e7dd7Smrg bar += sizeof(uint32_t), region++) { 3018ce851d2Smacallan err = pci_read(domain, bus, dev, func, bar, ®); 3024f5e7dd7Smrg if (err) 3034f5e7dd7Smrg return err; 3044f5e7dd7Smrg 3054f5e7dd7Smrg /* Probe the size of the region. */ 3068ce851d2Smacallan err = pci_write(domain, bus, dev, func, bar, (unsigned int)~0); 3074f5e7dd7Smrg if (err) 3084f5e7dd7Smrg return err; 3098ce851d2Smacallan pci_read(domain, bus, dev, func, bar, &size); 3108ce851d2Smacallan pci_write(domain, bus, dev, func, bar, reg); 3114f5e7dd7Smrg 3124f5e7dd7Smrg if (PCI_MAPREG_TYPE(reg) == PCI_MAPREG_TYPE_IO) { 3134f5e7dd7Smrg region->is_IO = 1; 3144f5e7dd7Smrg region->base_addr = PCI_MAPREG_IO_ADDR(reg); 3154f5e7dd7Smrg region->size = PCI_MAPREG_IO_SIZE(size); 3164f5e7dd7Smrg } else { 3174f5e7dd7Smrg if (PCI_MAPREG_MEM_PREFETCHABLE(reg)) 3184f5e7dd7Smrg region->is_prefetchable = 1; 3194f5e7dd7Smrg switch(PCI_MAPREG_MEM_TYPE(reg)) { 3204f5e7dd7Smrg case PCI_MAPREG_MEM_TYPE_32BIT: 3214f5e7dd7Smrg case PCI_MAPREG_MEM_TYPE_32BIT_1M: 3224f5e7dd7Smrg region->base_addr = PCI_MAPREG_MEM_ADDR(reg); 3234f5e7dd7Smrg region->size = PCI_MAPREG_MEM_SIZE(size); 3244f5e7dd7Smrg break; 3254f5e7dd7Smrg case PCI_MAPREG_MEM_TYPE_64BIT: 3264f5e7dd7Smrg region->is_64 = 1; 3274f5e7dd7Smrg 3284f5e7dd7Smrg reg64 = reg; 3294f5e7dd7Smrg size64 = size; 3304f5e7dd7Smrg 3314f5e7dd7Smrg bar += sizeof(uint32_t); 3324f5e7dd7Smrg 3338ce851d2Smacallan err = pci_read(domain, bus, dev, func, bar, ®); 3344f5e7dd7Smrg if (err) 3354f5e7dd7Smrg return err; 3364f5e7dd7Smrg reg64 |= (uint64_t)reg << 32; 3374f5e7dd7Smrg 3388ce851d2Smacallan err = pci_write(domain, bus, dev, func, bar, 3399e884b7eSchristos (unsigned int)~0); 3404f5e7dd7Smrg if (err) 3414f5e7dd7Smrg return err; 3428ce851d2Smacallan pci_read(domain, bus, dev, func, bar, &size); 3438ce851d2Smacallan pci_write(domain, bus, dev, func, bar, 3449e884b7eSchristos (unsigned int)(reg64 >> 32)); 3454f5e7dd7Smrg size64 |= (uint64_t)size << 32; 3464f5e7dd7Smrg 3479e884b7eSchristos region->base_addr = 3489e884b7eSchristos (unsigned long)PCI_MAPREG_MEM64_ADDR(reg64); 3499e884b7eSchristos region->size = 3509e884b7eSchristos (unsigned long)PCI_MAPREG_MEM64_SIZE(size64); 3514f5e7dd7Smrg region++; 3524f5e7dd7Smrg break; 3534f5e7dd7Smrg } 3544f5e7dd7Smrg } 3554f5e7dd7Smrg } 3564f5e7dd7Smrg 35737a6a21eSmrg /* Probe expansion ROM if present */ 35837a6a21eSmrg err = pci_read(domain, bus, dev, func, PCI_MAPREG_ROM, ®); 35937a6a21eSmrg if (err) 36037a6a21eSmrg return err; 36137a6a21eSmrg if (reg != 0) { 36237a6a21eSmrg err = pci_write(domain, bus, dev, func, PCI_MAPREG_ROM, 36337a6a21eSmrg (uint32_t)(~PCI_MAPREG_ROM_ENABLE)); 36437a6a21eSmrg if (err) 36537a6a21eSmrg return err; 36637a6a21eSmrg pci_read(domain, bus, dev, func, PCI_MAPREG_ROM, &size); 36737a6a21eSmrg pci_write(domain, bus, dev, func, PCI_MAPREG_ROM, reg); 36837a6a21eSmrg if ((reg & PCI_MAPREG_MEM_ADDR_MASK) != 0) { 36937a6a21eSmrg priv->rom_base = reg & PCI_MAPREG_MEM_ADDR_MASK; 37037a6a21eSmrg device->rom_size = -(size & PCI_MAPREG_MEM_ADDR_MASK); 37137a6a21eSmrg } 37237a6a21eSmrg } 37337a6a21eSmrg 3744f5e7dd7Smrg return 0; 3754f5e7dd7Smrg} 3764f5e7dd7Smrg 3779e884b7eSchristos/** 3789e884b7eSchristos * Read a VGA rom using the 0xc0000 mapping. 3799e884b7eSchristos * 3809e884b7eSchristos * This function should be extended to handle access through PCI resources, 3819e884b7eSchristos * which should be more reliable when available. 3829e884b7eSchristos */ 3839e884b7eSchristosstatic int 3849e884b7eSchristospci_device_netbsd_read_rom(struct pci_device *dev, void *buffer) 3859e884b7eSchristos{ 3869e884b7eSchristos struct pci_device_private *priv = (struct pci_device_private *)(void *)dev; 3879e884b7eSchristos void *bios; 3889e884b7eSchristos pciaddr_t rom_base; 3899e884b7eSchristos size_t rom_size; 3909e884b7eSchristos uint32_t bios_val, command_val; 39174c741d0Smacallan int pci_rom; 3929e884b7eSchristos 3939e884b7eSchristos if (((priv->base.device_class >> 16) & 0xff) != PCI_CLASS_DISPLAY || 3949e884b7eSchristos ((priv->base.device_class >> 8) & 0xff) != PCI_SUBCLASS_DISPLAY_VGA) 3959e884b7eSchristos return ENOSYS; 3969e884b7eSchristos 3979e884b7eSchristos if (priv->rom_base == 0) { 3989e884b7eSchristos#if defined(__amd64__) || defined(__i386__) 39937a6a21eSmrg /* 40037a6a21eSmrg * We need a way to detect when this isn't the console and reject 40137a6a21eSmrg * this request outright. 40237a6a21eSmrg */ 4039e884b7eSchristos rom_base = 0xc0000; 4049e884b7eSchristos rom_size = 0x10000; 4059e884b7eSchristos pci_rom = 0; 4069e884b7eSchristos#else 4079e884b7eSchristos return ENOSYS; 4089e884b7eSchristos#endif 4099e884b7eSchristos } else { 4109e884b7eSchristos rom_base = priv->rom_base; 4119e884b7eSchristos rom_size = dev->rom_size; 4129e884b7eSchristos pci_rom = 1; 4138ce851d2Smacallan if ((pcibus_conf_read(buses[dev->domain].fd, (unsigned int)dev->bus, 4149e884b7eSchristos (unsigned int)dev->dev, (unsigned int)dev->func, 4158ce851d2Smacallan PCI_COMMAND_STATUS_REG, &command_val)) == -1) 4169e884b7eSchristos return errno; 4178ce851d2Smacallan if ((command_val & PCI_COMMAND_MEM_ENABLE) == 0) { 4188ce851d2Smacallan if ((pcibus_conf_write(buses[dev->domain].fd, 4198ce851d2Smacallan (unsigned int)dev->bus, (unsigned int)dev->dev, 4208ce851d2Smacallan (unsigned int)dev->func, PCI_COMMAND_STATUS_REG, 4218ce851d2Smacallan command_val | PCI_COMMAND_MEM_ENABLE)) == -1) 4229e884b7eSchristos return errno; 4239e884b7eSchristos } 4248ce851d2Smacallan if ((pcibus_conf_read(buses[dev->domain].fd, (unsigned int)dev->bus, 4259e884b7eSchristos (unsigned int)dev->dev, (unsigned int)dev->func, 4268ce851d2Smacallan PCI_MAPREG_ROM, &bios_val)) == -1) 4279e884b7eSchristos return errno; 4288ce851d2Smacallan if ((bios_val & PCI_MAPREG_ROM_ENABLE) == 0) { 4298ce851d2Smacallan if ((pcibus_conf_write(buses[dev->domain].fd, 4308ce851d2Smacallan (unsigned int)dev->bus, 4319e884b7eSchristos (unsigned int)dev->dev, (unsigned int)dev->func, 4328ce851d2Smacallan PCI_MAPREG_ROM, bios_val | PCI_MAPREG_ROM_ENABLE)) == -1) 4339e884b7eSchristos return errno; 4349e884b7eSchristos } 4359e884b7eSchristos } 4369e884b7eSchristos 43774c741d0Smacallan fprintf(stderr, "Using rom_base = 0x%lx 0x%lx (pci_rom=%d)\n", 43874c741d0Smacallan (long)rom_base, (long)rom_size, pci_rom); 4399e884b7eSchristos 44074c741d0Smacallan bios = mmap(NULL, rom_size, PROT_READ, MAP_SHARED, buses[dev->domain].fd, 44174c741d0Smacallan (off_t)rom_base); 4429e884b7eSchristos if (bios == MAP_FAILED) { 4439e884b7eSchristos int serrno = errno; 4449e884b7eSchristos return serrno; 4459e884b7eSchristos } 4469e884b7eSchristos 4479e884b7eSchristos memcpy(buffer, bios, rom_size); 4489e884b7eSchristos 4499e884b7eSchristos munmap(bios, rom_size); 4509e884b7eSchristos 4519e884b7eSchristos if (pci_rom) { 4528ce851d2Smacallan if ((command_val & PCI_COMMAND_MEM_ENABLE) == 0) { 4538ce851d2Smacallan if ((pcibus_conf_write(buses[dev->domain].fd, 4548ce851d2Smacallan (unsigned int)dev->bus, 4559e884b7eSchristos (unsigned int)dev->dev, (unsigned int)dev->func, 4568ce851d2Smacallan PCI_COMMAND_STATUS_REG, command_val)) == -1) 4579e884b7eSchristos return errno; 4589e884b7eSchristos } 4598ce851d2Smacallan if ((bios_val & PCI_MAPREG_ROM_ENABLE) == 0) { 4608ce851d2Smacallan if ((pcibus_conf_write(buses[dev->domain].fd, 4618ce851d2Smacallan (unsigned int)dev->bus, 4629e884b7eSchristos (unsigned int)dev->dev, (unsigned int)dev->func, 4638ce851d2Smacallan PCI_MAPREG_ROM, bios_val)) == -1) 4649e884b7eSchristos return errno; 4659e884b7eSchristos } 4669e884b7eSchristos } 4679e884b7eSchristos 4689e884b7eSchristos return 0; 4699e884b7eSchristos} 4709e884b7eSchristos 4714f5e7dd7Smrgstatic const struct pci_system_methods netbsd_pci_methods = { 4729e884b7eSchristos .destroy = pci_system_netbsd_destroy, 4739e884b7eSchristos .destroy_device = NULL, 4749e884b7eSchristos .read_rom = pci_device_netbsd_read_rom, 4759e884b7eSchristos .probe = pci_device_netbsd_probe, 4769e884b7eSchristos .map_range = pci_device_netbsd_map_range, 4779e884b7eSchristos .unmap_range = pci_device_netbsd_unmap_range, 4789e884b7eSchristos .read = pci_device_netbsd_read, 4799e884b7eSchristos .write = pci_device_netbsd_write, 4809e884b7eSchristos .fill_capabilities = pci_fill_capabilities_generic 4814f5e7dd7Smrg}; 4824f5e7dd7Smrg 4834f5e7dd7Smrgint 4844f5e7dd7Smrgpci_system_netbsd_create(void) 4854f5e7dd7Smrg{ 4864f5e7dd7Smrg struct pci_device_private *device; 4878ce851d2Smacallan int bus, dev, func, ndevs, nfuncs, domain, pcifd; 4884f5e7dd7Smrg uint32_t reg; 4896c7645b9Smrg char netbsd_devname[32]; 4908ce851d2Smacallan struct pciio_businfo businfo; 4914f5e7dd7Smrg 4924f5e7dd7Smrg pci_sys = calloc(1, sizeof(struct pci_system)); 4934f5e7dd7Smrg 4944f5e7dd7Smrg pci_sys->methods = &netbsd_pci_methods; 4954f5e7dd7Smrg 4964f5e7dd7Smrg ndevs = 0; 4978ce851d2Smacallan nbuses = 0; 4986c7645b9Smrg snprintf(netbsd_devname, 32, "/dev/pci%d", nbuses); 4996c7645b9Smrg pcifd = open(netbsd_devname, O_RDWR); 5008ce851d2Smacallan while (pcifd > 0) { 5018ce851d2Smacallan ioctl(pcifd, PCI_IOC_BUSINFO, &businfo); 5028ce851d2Smacallan buses[nbuses].fd = pcifd; 5038ce851d2Smacallan buses[nbuses].num = bus = businfo.busno; 5048ce851d2Smacallan buses[nbuses].maxdevs = businfo.maxdevs; 5058ce851d2Smacallan domain = nbuses; 5068ce851d2Smacallan nbuses++; 5078ce851d2Smacallan for (dev = 0; dev < businfo.maxdevs; dev++) { 5088ce851d2Smacallan nfuncs = pci_nfuncs(domain, bus, dev); 5094f5e7dd7Smrg for (func = 0; func < nfuncs; func++) { 5108ce851d2Smacallan if (pci_read(domain, bus, dev, func, PCI_ID_REG, 5114f5e7dd7Smrg ®) != 0) 5124f5e7dd7Smrg continue; 5134f5e7dd7Smrg if (PCI_VENDOR(reg) == PCI_VENDOR_INVALID || 5144f5e7dd7Smrg PCI_VENDOR(reg) == 0) 5154f5e7dd7Smrg continue; 5164f5e7dd7Smrg 5174f5e7dd7Smrg ndevs++; 5184f5e7dd7Smrg } 5194f5e7dd7Smrg } 5206c7645b9Smrg snprintf(netbsd_devname, 32, "/dev/pci%d", nbuses); 5216c7645b9Smrg pcifd = open(netbsd_devname, O_RDWR); 5224f5e7dd7Smrg } 5234f5e7dd7Smrg 5244f5e7dd7Smrg pci_sys->num_devices = ndevs; 5254f5e7dd7Smrg pci_sys->devices = calloc(ndevs, sizeof(struct pci_device_private)); 5264f5e7dd7Smrg if (pci_sys->devices == NULL) { 5278ce851d2Smacallan int i; 5288ce851d2Smacallan 5298ce851d2Smacallan for (i = 0; i < nbuses; i++) 5308ce851d2Smacallan close(buses[i].fd); 5314f5e7dd7Smrg free(pci_sys); 5324f5e7dd7Smrg return ENOMEM; 5334f5e7dd7Smrg } 5344f5e7dd7Smrg 5354f5e7dd7Smrg device = pci_sys->devices; 5368ce851d2Smacallan for (domain = 0; domain < nbuses; domain++) { 5378ce851d2Smacallan bus = buses[domain].num; 5388ce851d2Smacallan for (dev = 0; dev < buses[domain].maxdevs; dev++) { 5398ce851d2Smacallan nfuncs = pci_nfuncs(domain, bus, dev); 5404f5e7dd7Smrg for (func = 0; func < nfuncs; func++) { 5418ce851d2Smacallan if (pci_read(domain, bus, dev, func, 5428ce851d2Smacallan PCI_ID_REG, ®) != 0) 5434f5e7dd7Smrg continue; 5444f5e7dd7Smrg if (PCI_VENDOR(reg) == PCI_VENDOR_INVALID || 5454f5e7dd7Smrg PCI_VENDOR(reg) == 0) 5464f5e7dd7Smrg continue; 5474f5e7dd7Smrg 5488ce851d2Smacallan device->base.domain = domain; 5494f5e7dd7Smrg device->base.bus = bus; 5504f5e7dd7Smrg device->base.dev = dev; 5514f5e7dd7Smrg device->base.func = func; 5524f5e7dd7Smrg device->base.vendor_id = PCI_VENDOR(reg); 5534f5e7dd7Smrg device->base.device_id = PCI_PRODUCT(reg); 5544f5e7dd7Smrg 5558ce851d2Smacallan if (pci_read(domain, bus, dev, func, 5568ce851d2Smacallan PCI_CLASS_REG, ®) != 0) 5574f5e7dd7Smrg continue; 5584f5e7dd7Smrg 5594f5e7dd7Smrg device->base.device_class = 5604f5e7dd7Smrg PCI_INTERFACE(reg) | PCI_CLASS(reg) << 16 | 5614f5e7dd7Smrg PCI_SUBCLASS(reg) << 8; 5624f5e7dd7Smrg device->base.revision = PCI_REVISION(reg); 5634f5e7dd7Smrg 5648ce851d2Smacallan if (pci_read(domain, bus, dev, func, 5658ce851d2Smacallan PCI_SUBSYS_ID_REG, ®) != 0) 5664f5e7dd7Smrg continue; 5674f5e7dd7Smrg 5684f5e7dd7Smrg device->base.subvendor_id = PCI_VENDOR(reg); 5694f5e7dd7Smrg device->base.subdevice_id = PCI_PRODUCT(reg); 5704f5e7dd7Smrg 5714f5e7dd7Smrg device++; 5724f5e7dd7Smrg } 5734f5e7dd7Smrg } 5744f5e7dd7Smrg } 5754f5e7dd7Smrg 5764f5e7dd7Smrg return 0; 5774f5e7dd7Smrg} 578