netbsd_pci.c revision a5d2dc35
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;
138a5d2dc35Sjmcneill	map->memory = mmap(NULL, (size_t)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)) {
149a5d2dc35Sjmcneill		m.base = map->base;
15022df1cdfSmrg		m.flags = MTRR_VALID | MTRR_PRIVATE;
151a5d2dc35Sjmcneill		m.len = map->size;
15222df1cdfSmrg		m.owner = getpid();
153a5d2dc35Sjmcneill		if (map->flags & PCI_DEV_MAP_FLAG_CACHABLE)
15422df1cdfSmrg			m.type = MTRR_TYPE_WB;
1554f5e7dd7Smrg		if (map->flags & PCI_DEV_MAP_FLAG_WRITE_COMBINE)
15622df1cdfSmrg			m.type = MTRR_TYPE_WC;
15722df1cdfSmrg
158a5d2dc35Sjmcneill		if ((netbsd_set_mtrr(&m, &n)) == -1) {
159a5d2dc35Sjmcneill			fprintf(stderr, "mtrr set failed: %s\n",
160a5d2dc35Sjmcneill			    strerror(errno));
161a5d2dc35Sjmcneill		}
1624f5e7dd7Smrg	}
16322df1cdfSmrg#endif
1644f5e7dd7Smrg
16522df1cdfSmrg	return ret;
1664f5e7dd7Smrg}
1674f5e7dd7Smrg
1684f5e7dd7Smrgstatic int
1694f5e7dd7Smrgpci_device_netbsd_unmap_range(struct pci_device *dev,
1704f5e7dd7Smrg    struct pci_device_mapping *map)
1714f5e7dd7Smrg{
17222df1cdfSmrg#ifdef HAVE_MTRR
17322df1cdfSmrg	struct mtrr m;
17422df1cdfSmrg	int n = 1;
17522df1cdfSmrg
17622df1cdfSmrg	memset(&m, 0, sizeof(m));
1774f5e7dd7Smrg
1784f5e7dd7Smrg	if ((map->flags & PCI_DEV_MAP_FLAG_CACHABLE) ||
1794f5e7dd7Smrg	    (map->flags & PCI_DEV_MAP_FLAG_WRITE_COMBINE)) {
18022df1cdfSmrg		m.base = map->base;
18122df1cdfSmrg		m.flags = 0;
182a5d2dc35Sjmcneill		m.len = map->size;
18322df1cdfSmrg		m.type = MTRR_TYPE_UC;
18422df1cdfSmrg		(void)netbsd_set_mtrr(&m, &n);
1854f5e7dd7Smrg	}
18622df1cdfSmrg#endif
1874f5e7dd7Smrg
1884f5e7dd7Smrg	return pci_device_generic_unmap_range(dev, map);
1894f5e7dd7Smrg}
1904f5e7dd7Smrg
1914f5e7dd7Smrgstatic int
1924f5e7dd7Smrgpci_device_netbsd_read(struct pci_device *dev, void *data,
1934f5e7dd7Smrg    pciaddr_t offset, pciaddr_t size, pciaddr_t *bytes_read)
1944f5e7dd7Smrg{
19522df1cdfSmrg	u_int reg, rval;
1964f5e7dd7Smrg
1974f5e7dd7Smrg	*bytes_read = 0;
1984f5e7dd7Smrg	while (size > 0) {
1999e884b7eSchristos		size_t toread = MIN(size, 4 - (offset & 0x3));
2004f5e7dd7Smrg
2019e884b7eSchristos		reg = (u_int)(offset & ~0x3);
2024f5e7dd7Smrg
2038ce851d2Smacallan		if ((pcibus_conf_read(buses[dev->domain].fd,
2048ce851d2Smacallan		    (unsigned int)dev->bus, (unsigned int)dev->dev,
2058ce851d2Smacallan		    (unsigned int)dev->func, reg, &rval)) == -1)
2064f5e7dd7Smrg			return errno;
2074f5e7dd7Smrg
20822df1cdfSmrg		rval = htole32(rval);
20922df1cdfSmrg		rval >>= ((offset & 0x3) * 8);
2104f5e7dd7Smrg
21122df1cdfSmrg		memcpy(data, &rval, toread);
2124f5e7dd7Smrg
2134f5e7dd7Smrg		offset += toread;
2144f5e7dd7Smrg		data = (char *)data + toread;
2154f5e7dd7Smrg		size -= toread;
2164f5e7dd7Smrg		*bytes_read += toread;
2174f5e7dd7Smrg	}
2184f5e7dd7Smrg
2194f5e7dd7Smrg	return 0;
2204f5e7dd7Smrg}
2214f5e7dd7Smrg
2224f5e7dd7Smrgstatic int
2234f5e7dd7Smrgpci_device_netbsd_write(struct pci_device *dev, const void *data,
2244f5e7dd7Smrg    pciaddr_t offset, pciaddr_t size, pciaddr_t *bytes_written)
2254f5e7dd7Smrg{
22622df1cdfSmrg	u_int reg, val;
2274f5e7dd7Smrg
2284f5e7dd7Smrg	if ((offset % 4) != 0 || (size % 4) != 0)
2294f5e7dd7Smrg		return EINVAL;
2304f5e7dd7Smrg
2314f5e7dd7Smrg	*bytes_written = 0;
2324f5e7dd7Smrg	while (size > 0) {
2339e884b7eSchristos		reg = (u_int)offset;
23422df1cdfSmrg		memcpy(&val, data, 4);
2354f5e7dd7Smrg
2368ce851d2Smacallan		if ((pcibus_conf_write(buses[dev->domain].fd,
2378ce851d2Smacallan		    (unsigned int)dev->bus, (unsigned int)dev->dev,
2388ce851d2Smacallan		    (unsigned int)dev->func, reg, val)) == -1)
2394f5e7dd7Smrg			return errno;
2404f5e7dd7Smrg
2414f5e7dd7Smrg		offset += 4;
2429e884b7eSchristos		data = (const char *)data + 4;
2434f5e7dd7Smrg		size -= 4;
2444f5e7dd7Smrg		*bytes_written += 4;
2454f5e7dd7Smrg	}
2464f5e7dd7Smrg
2474f5e7dd7Smrg	return 0;
2484f5e7dd7Smrg}
2494f5e7dd7Smrg
2504f5e7dd7Smrgstatic void
2514f5e7dd7Smrgpci_system_netbsd_destroy(void)
2524f5e7dd7Smrg{
2538ce851d2Smacallan	int i;
2548ce851d2Smacallan
2558ce851d2Smacallan	for (i = 0; i < nbuses; i++) {
2568ce851d2Smacallan		close(buses[i].fd);
2578ce851d2Smacallan	}
2584f5e7dd7Smrg	free(pci_sys);
2594f5e7dd7Smrg	pci_sys = NULL;
2604f5e7dd7Smrg}
2614f5e7dd7Smrg
2624f5e7dd7Smrgstatic int
2634f5e7dd7Smrgpci_device_netbsd_probe(struct pci_device *device)
2644f5e7dd7Smrg{
2659e884b7eSchristos	struct pci_device_private *priv =
2669e884b7eSchristos	    (struct pci_device_private *)(void *)device;
2674f5e7dd7Smrg	struct pci_mem_region *region;
2684f5e7dd7Smrg	uint64_t reg64, size64;
2694f5e7dd7Smrg	uint32_t bar, reg, size;
2708ce851d2Smacallan	int bus, dev, func, err, domain;
2714f5e7dd7Smrg
2728ce851d2Smacallan	domain = device->domain;
2734f5e7dd7Smrg	bus = device->bus;
2744f5e7dd7Smrg	dev = device->dev;
2754f5e7dd7Smrg	func = device->func;
2764f5e7dd7Smrg
2776c7645b9Smrg	/* Enable the device if necessary */
2786c7645b9Smrg	err = pci_read(domain, bus, dev, func, PCI_COMMAND_STATUS_REG, &reg);
2796c7645b9Smrg	if (err)
2806c7645b9Smrg		return err;
2816c7645b9Smrg	if ((reg & (PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE | PCI_COMMAND_MASTER_ENABLE)) !=
2826c7645b9Smrg	    (PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE | PCI_COMMAND_MASTER_ENABLE)) {
2836c7645b9Smrg		reg |= PCI_COMMAND_IO_ENABLE |
2846c7645b9Smrg		       PCI_COMMAND_MEM_ENABLE |
2856c7645b9Smrg		       PCI_COMMAND_MASTER_ENABLE;
2866c7645b9Smrg		err = pci_write(domain, bus, dev, func, PCI_COMMAND_STATUS_REG,
2876c7645b9Smrg				reg);
2886c7645b9Smrg		if (err)
2896c7645b9Smrg			return err;
2906c7645b9Smrg	}
2916c7645b9Smrg
2928ce851d2Smacallan	err = pci_read(domain, bus, dev, func, PCI_BHLC_REG, &reg);
2934f5e7dd7Smrg	if (err)
2944f5e7dd7Smrg		return err;
2954f5e7dd7Smrg
2964f5e7dd7Smrg	priv->header_type = PCI_HDRTYPE_TYPE(reg);
2974f5e7dd7Smrg	if (priv->header_type != 0)
2984f5e7dd7Smrg		return 0;
2994f5e7dd7Smrg
3004f5e7dd7Smrg	region = device->regions;
3014f5e7dd7Smrg	for (bar = PCI_MAPREG_START; bar < PCI_MAPREG_END;
3024f5e7dd7Smrg	     bar += sizeof(uint32_t), region++) {
3038ce851d2Smacallan		err = pci_read(domain, bus, dev, func, bar, &reg);
3044f5e7dd7Smrg		if (err)
3054f5e7dd7Smrg			return err;
3064f5e7dd7Smrg
3074f5e7dd7Smrg		/* Probe the size of the region. */
3088ce851d2Smacallan		err = pci_write(domain, bus, dev, func, bar, (unsigned int)~0);
3094f5e7dd7Smrg		if (err)
3104f5e7dd7Smrg			return err;
3118ce851d2Smacallan		pci_read(domain, bus, dev, func, bar, &size);
3128ce851d2Smacallan		pci_write(domain, bus, dev, func, bar, reg);
3134f5e7dd7Smrg
3144f5e7dd7Smrg		if (PCI_MAPREG_TYPE(reg) == PCI_MAPREG_TYPE_IO) {
3154f5e7dd7Smrg			region->is_IO = 1;
3164f5e7dd7Smrg			region->base_addr = PCI_MAPREG_IO_ADDR(reg);
3174f5e7dd7Smrg			region->size = PCI_MAPREG_IO_SIZE(size);
3184f5e7dd7Smrg		} else {
3194f5e7dd7Smrg			if (PCI_MAPREG_MEM_PREFETCHABLE(reg))
3204f5e7dd7Smrg				region->is_prefetchable = 1;
3214f5e7dd7Smrg			switch(PCI_MAPREG_MEM_TYPE(reg)) {
3224f5e7dd7Smrg			case PCI_MAPREG_MEM_TYPE_32BIT:
3234f5e7dd7Smrg			case PCI_MAPREG_MEM_TYPE_32BIT_1M:
3244f5e7dd7Smrg				region->base_addr = PCI_MAPREG_MEM_ADDR(reg);
3254f5e7dd7Smrg				region->size = PCI_MAPREG_MEM_SIZE(size);
3264f5e7dd7Smrg				break;
3274f5e7dd7Smrg			case PCI_MAPREG_MEM_TYPE_64BIT:
3284f5e7dd7Smrg				region->is_64 = 1;
3294f5e7dd7Smrg
3304f5e7dd7Smrg				reg64 = reg;
3314f5e7dd7Smrg				size64 = size;
3324f5e7dd7Smrg
3334f5e7dd7Smrg				bar += sizeof(uint32_t);
3344f5e7dd7Smrg
3358ce851d2Smacallan				err = pci_read(domain, bus, dev, func, bar, &reg);
3364f5e7dd7Smrg				if (err)
3374f5e7dd7Smrg					return err;
3384f5e7dd7Smrg				reg64 |= (uint64_t)reg << 32;
3394f5e7dd7Smrg
3408ce851d2Smacallan				err = pci_write(domain, bus, dev, func, bar,
3419e884b7eSchristos				    (unsigned int)~0);
3424f5e7dd7Smrg				if (err)
3434f5e7dd7Smrg					return err;
3448ce851d2Smacallan				pci_read(domain, bus, dev, func, bar, &size);
3458ce851d2Smacallan				pci_write(domain, bus, dev, func, bar,
3469e884b7eSchristos				    (unsigned int)(reg64 >> 32));
3474f5e7dd7Smrg				size64 |= (uint64_t)size << 32;
3484f5e7dd7Smrg
3499e884b7eSchristos				region->base_addr =
3509e884b7eSchristos				    (unsigned long)PCI_MAPREG_MEM64_ADDR(reg64);
3519e884b7eSchristos				region->size =
3529e884b7eSchristos				    (unsigned long)PCI_MAPREG_MEM64_SIZE(size64);
3534f5e7dd7Smrg				region++;
3544f5e7dd7Smrg				break;
3554f5e7dd7Smrg			}
3564f5e7dd7Smrg		}
3574f5e7dd7Smrg	}
3584f5e7dd7Smrg
35937a6a21eSmrg	/* Probe expansion ROM if present */
36037a6a21eSmrg	err = pci_read(domain, bus, dev, func, PCI_MAPREG_ROM, &reg);
36137a6a21eSmrg	if (err)
36237a6a21eSmrg		return err;
36337a6a21eSmrg	if (reg != 0) {
36437a6a21eSmrg		err = pci_write(domain, bus, dev, func, PCI_MAPREG_ROM,
36537a6a21eSmrg		    (uint32_t)(~PCI_MAPREG_ROM_ENABLE));
36637a6a21eSmrg		if (err)
36737a6a21eSmrg			return err;
36837a6a21eSmrg		pci_read(domain, bus, dev, func, PCI_MAPREG_ROM, &size);
36937a6a21eSmrg		pci_write(domain, bus, dev, func, PCI_MAPREG_ROM, reg);
37037a6a21eSmrg		if ((reg & PCI_MAPREG_MEM_ADDR_MASK) != 0) {
37137a6a21eSmrg			priv->rom_base = reg & PCI_MAPREG_MEM_ADDR_MASK;
37237a6a21eSmrg			device->rom_size = -(size & PCI_MAPREG_MEM_ADDR_MASK);
37337a6a21eSmrg		}
37437a6a21eSmrg	}
37537a6a21eSmrg
3764f5e7dd7Smrg	return 0;
3774f5e7dd7Smrg}
3784f5e7dd7Smrg
3799e884b7eSchristos/**
3809e884b7eSchristos * Read a VGA rom using the 0xc0000 mapping.
3819e884b7eSchristos *
3829e884b7eSchristos * This function should be extended to handle access through PCI resources,
3839e884b7eSchristos * which should be more reliable when available.
3849e884b7eSchristos */
3859e884b7eSchristosstatic int
3869e884b7eSchristospci_device_netbsd_read_rom(struct pci_device *dev, void *buffer)
3879e884b7eSchristos{
3889e884b7eSchristos    struct pci_device_private *priv = (struct pci_device_private *)(void *)dev;
3899e884b7eSchristos    void *bios;
3909e884b7eSchristos    pciaddr_t rom_base;
3919e884b7eSchristos    size_t rom_size;
3929e884b7eSchristos    uint32_t bios_val, command_val;
39374c741d0Smacallan    int pci_rom;
3949e884b7eSchristos
3959e884b7eSchristos    if (((priv->base.device_class >> 16) & 0xff) != PCI_CLASS_DISPLAY ||
3969e884b7eSchristos	((priv->base.device_class >> 8) & 0xff) != PCI_SUBCLASS_DISPLAY_VGA)
3979e884b7eSchristos	return ENOSYS;
3989e884b7eSchristos
3999e884b7eSchristos    if (priv->rom_base == 0) {
4009e884b7eSchristos#if defined(__amd64__) || defined(__i386__)
40137a6a21eSmrg	/*
40237a6a21eSmrg	 * We need a way to detect when this isn't the console and reject
40337a6a21eSmrg	 * this request outright.
40437a6a21eSmrg	 */
4059e884b7eSchristos	rom_base = 0xc0000;
4069e884b7eSchristos	rom_size = 0x10000;
4079e884b7eSchristos	pci_rom = 0;
4089e884b7eSchristos#else
4099e884b7eSchristos	return ENOSYS;
4109e884b7eSchristos#endif
4119e884b7eSchristos    } else {
4129e884b7eSchristos	rom_base = priv->rom_base;
4139e884b7eSchristos	rom_size = dev->rom_size;
4149e884b7eSchristos	pci_rom = 1;
4158ce851d2Smacallan	if ((pcibus_conf_read(buses[dev->domain].fd, (unsigned int)dev->bus,
4169e884b7eSchristos	    (unsigned int)dev->dev, (unsigned int)dev->func,
4178ce851d2Smacallan	    PCI_COMMAND_STATUS_REG, &command_val)) == -1)
4189e884b7eSchristos	    return errno;
4198ce851d2Smacallan	if ((command_val & PCI_COMMAND_MEM_ENABLE) == 0) {
4208ce851d2Smacallan	    if ((pcibus_conf_write(buses[dev->domain].fd,
4218ce851d2Smacallan	        (unsigned int)dev->bus, (unsigned int)dev->dev,
4228ce851d2Smacallan		(unsigned int)dev->func, PCI_COMMAND_STATUS_REG,
4238ce851d2Smacallan		command_val | PCI_COMMAND_MEM_ENABLE)) == -1)
4249e884b7eSchristos		return errno;
4259e884b7eSchristos	}
4268ce851d2Smacallan	if ((pcibus_conf_read(buses[dev->domain].fd, (unsigned int)dev->bus,
4279e884b7eSchristos	    (unsigned int)dev->dev, (unsigned int)dev->func,
4288ce851d2Smacallan	    PCI_MAPREG_ROM, &bios_val)) == -1)
4299e884b7eSchristos	    return errno;
4308ce851d2Smacallan	if ((bios_val & PCI_MAPREG_ROM_ENABLE) == 0) {
4318ce851d2Smacallan	    if ((pcibus_conf_write(buses[dev->domain].fd,
4328ce851d2Smacallan	        (unsigned int)dev->bus,
4339e884b7eSchristos		(unsigned int)dev->dev, (unsigned int)dev->func,
4348ce851d2Smacallan		PCI_MAPREG_ROM, bios_val | PCI_MAPREG_ROM_ENABLE)) == -1)
4359e884b7eSchristos		return errno;
4369e884b7eSchristos	}
4379e884b7eSchristos    }
4389e884b7eSchristos
43974c741d0Smacallan    fprintf(stderr, "Using rom_base = 0x%lx 0x%lx (pci_rom=%d)\n",
44074c741d0Smacallan        (long)rom_base, (long)rom_size, pci_rom);
4419e884b7eSchristos
44274c741d0Smacallan    bios = mmap(NULL, rom_size, PROT_READ, MAP_SHARED, buses[dev->domain].fd,
44374c741d0Smacallan        (off_t)rom_base);
4449e884b7eSchristos    if (bios == MAP_FAILED) {
4459e884b7eSchristos	int serrno = errno;
4469e884b7eSchristos	return serrno;
4479e884b7eSchristos    }
4489e884b7eSchristos
4499e884b7eSchristos    memcpy(buffer, bios, rom_size);
4509e884b7eSchristos
4519e884b7eSchristos    munmap(bios, rom_size);
4529e884b7eSchristos
4539e884b7eSchristos    if (pci_rom) {
4548ce851d2Smacallan	if ((command_val & PCI_COMMAND_MEM_ENABLE) == 0) {
4558ce851d2Smacallan	    if ((pcibus_conf_write(buses[dev->domain].fd,
4568ce851d2Smacallan	        (unsigned int)dev->bus,
4579e884b7eSchristos		(unsigned int)dev->dev, (unsigned int)dev->func,
4588ce851d2Smacallan		PCI_COMMAND_STATUS_REG, command_val)) == -1)
4599e884b7eSchristos		return errno;
4609e884b7eSchristos	}
4618ce851d2Smacallan	if ((bios_val & PCI_MAPREG_ROM_ENABLE) == 0) {
4628ce851d2Smacallan	    if ((pcibus_conf_write(buses[dev->domain].fd,
4638ce851d2Smacallan	        (unsigned int)dev->bus,
4649e884b7eSchristos		(unsigned int)dev->dev, (unsigned int)dev->func,
4658ce851d2Smacallan		PCI_MAPREG_ROM, bios_val)) == -1)
4669e884b7eSchristos		return errno;
4679e884b7eSchristos	}
4689e884b7eSchristos    }
4699e884b7eSchristos
4709e884b7eSchristos    return 0;
4719e884b7eSchristos}
4729e884b7eSchristos
4734f5e7dd7Smrgstatic const struct pci_system_methods netbsd_pci_methods = {
4749e884b7eSchristos	.destroy = pci_system_netbsd_destroy,
4759e884b7eSchristos	.destroy_device = NULL,
4769e884b7eSchristos	.read_rom = pci_device_netbsd_read_rom,
4779e884b7eSchristos	.probe = pci_device_netbsd_probe,
4789e884b7eSchristos	.map_range = pci_device_netbsd_map_range,
4799e884b7eSchristos	.unmap_range = pci_device_netbsd_unmap_range,
4809e884b7eSchristos	.read = pci_device_netbsd_read,
4819e884b7eSchristos	.write = pci_device_netbsd_write,
4829e884b7eSchristos	.fill_capabilities = pci_fill_capabilities_generic
4834f5e7dd7Smrg};
4844f5e7dd7Smrg
4854f5e7dd7Smrgint
4864f5e7dd7Smrgpci_system_netbsd_create(void)
4874f5e7dd7Smrg{
4884f5e7dd7Smrg	struct pci_device_private *device;
4898ce851d2Smacallan	int bus, dev, func, ndevs, nfuncs, domain, pcifd;
4904f5e7dd7Smrg	uint32_t reg;
4916c7645b9Smrg	char netbsd_devname[32];
4928ce851d2Smacallan	struct pciio_businfo businfo;
4934f5e7dd7Smrg
4944f5e7dd7Smrg	pci_sys = calloc(1, sizeof(struct pci_system));
4954f5e7dd7Smrg
4964f5e7dd7Smrg	pci_sys->methods = &netbsd_pci_methods;
4974f5e7dd7Smrg
4984f5e7dd7Smrg	ndevs = 0;
4998ce851d2Smacallan	nbuses = 0;
5006c7645b9Smrg	snprintf(netbsd_devname, 32, "/dev/pci%d", nbuses);
5016c7645b9Smrg	pcifd = open(netbsd_devname, O_RDWR);
5028ce851d2Smacallan	while (pcifd > 0) {
5038ce851d2Smacallan		ioctl(pcifd, PCI_IOC_BUSINFO, &businfo);
5048ce851d2Smacallan		buses[nbuses].fd = pcifd;
5058ce851d2Smacallan		buses[nbuses].num = bus = businfo.busno;
5068ce851d2Smacallan		buses[nbuses].maxdevs = businfo.maxdevs;
5078ce851d2Smacallan		domain = nbuses;
5088ce851d2Smacallan		nbuses++;
5098ce851d2Smacallan		for (dev = 0; dev < businfo.maxdevs; dev++) {
5108ce851d2Smacallan			nfuncs = pci_nfuncs(domain, bus, dev);
5114f5e7dd7Smrg			for (func = 0; func < nfuncs; func++) {
5128ce851d2Smacallan				if (pci_read(domain, bus, dev, func, PCI_ID_REG,
5134f5e7dd7Smrg				    &reg) != 0)
5144f5e7dd7Smrg					continue;
5154f5e7dd7Smrg				if (PCI_VENDOR(reg) == PCI_VENDOR_INVALID ||
5164f5e7dd7Smrg				    PCI_VENDOR(reg) == 0)
5174f5e7dd7Smrg					continue;
5184f5e7dd7Smrg
5194f5e7dd7Smrg				ndevs++;
5204f5e7dd7Smrg			}
5214f5e7dd7Smrg		}
5226c7645b9Smrg		snprintf(netbsd_devname, 32, "/dev/pci%d", nbuses);
5236c7645b9Smrg		pcifd = open(netbsd_devname, O_RDWR);
5244f5e7dd7Smrg	}
5254f5e7dd7Smrg
5264f5e7dd7Smrg	pci_sys->num_devices = ndevs;
5274f5e7dd7Smrg	pci_sys->devices = calloc(ndevs, sizeof(struct pci_device_private));
5284f5e7dd7Smrg	if (pci_sys->devices == NULL) {
5298ce851d2Smacallan		int i;
5308ce851d2Smacallan
5318ce851d2Smacallan		for (i = 0; i < nbuses; i++)
5328ce851d2Smacallan			close(buses[i].fd);
5334f5e7dd7Smrg		free(pci_sys);
5344f5e7dd7Smrg		return ENOMEM;
5354f5e7dd7Smrg	}
5364f5e7dd7Smrg
5374f5e7dd7Smrg	device = pci_sys->devices;
5388ce851d2Smacallan	for (domain = 0; domain < nbuses; domain++) {
5398ce851d2Smacallan		bus = buses[domain].num;
5408ce851d2Smacallan		for (dev = 0; dev < buses[domain].maxdevs; dev++) {
5418ce851d2Smacallan			nfuncs = pci_nfuncs(domain, bus, dev);
5424f5e7dd7Smrg			for (func = 0; func < nfuncs; func++) {
5438ce851d2Smacallan				if (pci_read(domain, bus, dev, func,
5448ce851d2Smacallan				    PCI_ID_REG, &reg) != 0)
5454f5e7dd7Smrg					continue;
5464f5e7dd7Smrg				if (PCI_VENDOR(reg) == PCI_VENDOR_INVALID ||
5474f5e7dd7Smrg				    PCI_VENDOR(reg) == 0)
5484f5e7dd7Smrg					continue;
5494f5e7dd7Smrg
5508ce851d2Smacallan				device->base.domain = domain;
5514f5e7dd7Smrg				device->base.bus = bus;
5524f5e7dd7Smrg				device->base.dev = dev;
5534f5e7dd7Smrg				device->base.func = func;
5544f5e7dd7Smrg				device->base.vendor_id = PCI_VENDOR(reg);
5554f5e7dd7Smrg				device->base.device_id = PCI_PRODUCT(reg);
5564f5e7dd7Smrg
5578ce851d2Smacallan				if (pci_read(domain, bus, dev, func,
5588ce851d2Smacallan				    PCI_CLASS_REG, &reg) != 0)
5594f5e7dd7Smrg					continue;
5604f5e7dd7Smrg
5614f5e7dd7Smrg				device->base.device_class =
5624f5e7dd7Smrg				    PCI_INTERFACE(reg) | PCI_CLASS(reg) << 16 |
5634f5e7dd7Smrg				    PCI_SUBCLASS(reg) << 8;
5644f5e7dd7Smrg				device->base.revision = PCI_REVISION(reg);
5654f5e7dd7Smrg
5668ce851d2Smacallan				if (pci_read(domain, bus, dev, func,
5678ce851d2Smacallan				    PCI_SUBSYS_ID_REG, &reg) != 0)
5684f5e7dd7Smrg					continue;
5694f5e7dd7Smrg
5704f5e7dd7Smrg				device->base.subvendor_id = PCI_VENDOR(reg);
5714f5e7dd7Smrg				device->base.subdevice_id = PCI_PRODUCT(reg);
5724f5e7dd7Smrg
5734f5e7dd7Smrg				device++;
5744f5e7dd7Smrg			}
5754f5e7dd7Smrg		}
5764f5e7dd7Smrg	}
5774f5e7dd7Smrg
5784f5e7dd7Smrg	return 0;
5794f5e7dd7Smrg}
580