OSDN Git Service

Use PCIOCREADMASK on OpenBSD.
authorMark Kettenis <kettenis@openbsd.org>
Mon, 29 Sep 2014 20:34:25 +0000 (22:34 +0200)
committerMark Kettenis <kettenis@openbsd.org>
Mon, 29 Sep 2014 20:43:30 +0000 (22:43 +0200)
If the machdep.allowaperture sysctl(8) variable is set to 0, writing to PCI
config space is not allowed.  So instead of writing 0xffffffff to the BARs
in order to determine their size, use the PCIOCREADMASK ioctl(2) which
returns the mask of changeable bits that was saved by the kernel when the
devices was initially probed.

Reviewed-by: Matthieu Herrb <matthieu@herbb.eu>
Signed-off-by: Mark Kettenis <kettenis@openbsd.org>
src/openbsd_pci.c

index fe034f3..4d1b5cd 100644 (file)
@@ -81,6 +81,29 @@ pci_write(int domain, int bus, int dev, int func, uint32_t reg, uint32_t val)
        return ioctl(pcifd[domain], PCIOCWRITE, &io);
 }
 
+static int
+pci_readmask(int domain, int bus, int dev, int func, uint32_t reg,
+    uint32_t *val)
+{
+       struct pci_io io;
+       int err;
+
+       bzero(&io, sizeof(io));
+       io.pi_sel.pc_bus = bus;
+       io.pi_sel.pc_dev = dev;
+       io.pi_sel.pc_func = func;
+       io.pi_reg = reg;
+       io.pi_width = 4;
+
+       err = ioctl(pcifd[domain], PCIOCREADMASK, &io);
+       if (err)
+               return (err);
+
+       *val = io.pi_data;
+
+       return 0;
+}
+
 /**
  * Read a VGA ROM
  *
@@ -328,11 +351,9 @@ pci_device_openbsd_probe(struct pci_device *device)
                        return err;
 
                /* Probe the size of the region. */
-               err = pci_write(domain, bus, dev, func, bar, ~0);
+               err = pci_readmask(domain, bus, dev, func, bar, &size);
                if (err)
                        return err;
-               pci_read(domain, bus, dev, func, bar, &size);
-               pci_write(domain, bus, dev, func, bar, reg);
 
                if (PCI_MAPREG_TYPE(reg) == PCI_MAPREG_TYPE_IO) {
                        region->is_IO = 1;
@@ -360,11 +381,9 @@ pci_device_openbsd_probe(struct pci_device *device)
                                        return err;
                                reg64 |= (uint64_t)reg << 32;
 
-                               err = pci_write(domain, bus, dev, func, bar, ~0);
+                               err = pci_readmask(domain, bus, dev, func, bar, &size);
                                if (err)
                                        return err;
-                               pci_read(domain, bus, dev, func, bar, &size);
-                               pci_write(domain, bus, dev, func, bar, reg64 >> 32);
                                size64 |= (uint64_t)size << 32;
 
                                region->base_addr = PCI_MAPREG_MEM64_ADDR(reg64);