Diff below is a rewrite the vgafb_pci_probe(). The idea is to save the
different PCI bars in the device structure for later access in drm(4).
It also simplifies the code to make it looks more like vga(4).

Ok?

Martin


Index: pci/vgafb_pci.c
===================================================================
RCS file: /cvs/src/sys/arch/macppc/pci/vgafb_pci.c,v
retrieving revision 1.24
diff -u -p -r1.24 vgafb_pci.c
--- pci/vgafb_pci.c     29 Jan 2012 14:44:16 -0000      1.24
+++ pci/vgafb_pci.c     22 Feb 2012 18:13:29 -0000
@@ -50,19 +50,25 @@
 #include <arch/macppc/pci/vgafbvar.h>
 #include <arch/macppc/pci/vgafb_pcivar.h>
 
-#define PCI_VENDORID(x) ((x) & 0xFFFF)
-#define PCI_CHIPID(x)   (((x) >> 16) & 0xFFFF)
+#ifdef DEBUG_VGAFB
+#define DPRINTF(x...)  do { printf(x); } while (0);
+#else
+#define DPRINTF(x...)
+#endif
 
 struct vgafb_pci_softc {
        struct device sc_dev; 
  
        pcitag_t sc_pcitag;             /* PCI tag, in case we need it. */
        struct vgafb_config *sc_vc;     /* VGA configuration */ 
+
+       struct pci_attach_args pa;
+       struct vga_pci_bar *bars[VGA_PCI_MAX_BARS];
 };
 
-int vgafb_pci_probe(struct pci_attach_args *pa, int id, u_int32_t *ioaddr,
-    u_int32_t *iosize, u_int32_t *memaddr, u_int32_t *memsize,
-    u_int32_t *cacheable, u_int32_t *mmioaddr, u_int32_t *mmiosize);
+void   vgafb_pci_bar_init(struct vgafb_pci_softc *, struct pci_attach_args *,
+               int , uint32_t *, uint32_t *, uint32_t *, uint32_t *,
+               uint32_t *, uint32_t *);
 int    vgafb_pci_match(struct device *, void *, void *);
 void   vgafb_pci_attach(struct device *, struct device *, void *);
 
@@ -73,128 +79,96 @@ struct cfattach vgafb_pci_ca = {
 pcitag_t vgafb_pci_console_tag;
 struct vgafb_config vgafb_pci_console_vc;
 
-#if 0
-#define DEBUG_VGAFB
-#endif
-
-int
-vgafb_pci_probe(struct pci_attach_args *pa, int id, u_int32_t *ioaddr,
-    u_int32_t *iosize, u_int32_t *memaddr, u_int32_t *memsize,
-    u_int32_t *cacheable, u_int32_t *mmioaddr, u_int32_t *mmiosize)
+void
+vgafb_pci_bar_init(struct vgafb_pci_softc *dev, struct pci_attach_args *pa,
+    int id, uint32_t *ioaddr, uint32_t *iosize, uint32_t *memaddr,
+    uint32_t *memsize, uint32_t *mmioaddr, uint32_t *mmiosize)
 {
-       bus_addr_t addr;
-       bus_size_t size;
-       int tcacheable;
-       pci_chipset_tag_t pc = pa->pa_pc;
-       int retval;
-       int i;
+       pcireg_t type;
+       int addr = PCI_MAPREG_START, i;
+
+       memcpy(&dev->pa, pa, sizeof(dev->pa));
 
        *iosize   = 0x0;
        *memsize  = 0x0;
        *mmiosize = 0x0;
-       for (i = PCI_MAPREG_START; i <= PCI_MAPREG_PPB_END; i += 4) {
-#ifdef DEBUG_VGAFB
-               printf("vgafb confread %x %x\n",
-                       i, pci_conf_read(pc, pa->pa_tag, i));
-#endif
-               /* need to check more than just two base addresses? */
-               if (PCI_MAPREG_TYPE(pci_conf_read(pc, pa->pa_tag, i)) ==
-                   PCI_MAPREG_TYPE_IO) {
-                       retval = pci_io_find(pc, pa->pa_tag, i,
-                               &addr, &size);
-                       if (retval != 0) {
-                               continue;
-                       }
-#ifdef DEBUG_VGAFB
-       printf("vgafb_pci_probe: io %x addr %x size %x\n", i, addr, size);
-#endif
-                       if (*iosize == 0) {
-                               *ioaddr = addr;
-                               *iosize = size;
-                       }
 
-               } else {
-                       retval = pci_mem_find(pc, pa->pa_tag, i,
-                               &addr, &size, &tcacheable);
-                       if (retval != 0) {
-                               continue;
+       for (i = 0; i < VGA_PCI_MAX_BARS; i++, addr += 4) {
+               dev->bars[i] = malloc(sizeof((*dev->bars[i])), M_DEVBUF,
+                   M_NOWAIT | M_ZERO);
+               if (dev->bars[i] == NULL)
+                       return;
+
+               dev->bars[i]->addr = addr;
+
+               type = dev->bars[i]->maptype = pci_mapreg_type(pa->pa_pc,
+                   pa->pa_tag, addr);
+
+               DPRINTF("\nvgafb: 0x%04x: BAR ", addr);
+
+               if (pci_mapreg_info(pa->pa_pc, pa->pa_tag, addr,
+                   dev->bars[i]->maptype, &dev->bars[i]->base,
+                   &dev->bars[i]->maxsize, &dev->bars[i]->flags) != 0) {
+                       free(dev->bars[i], M_DEVBUF);
+                       dev->bars[i] = NULL;
+
+                       DPRINTF("empty (%08x)", type);
+                       continue;
+               }
+
+               switch (PCI_MAPREG_TYPE(type)) {
+               case PCI_MAPREG_TYPE_IO:
+                       if (*iosize == 0) {
+                               *ioaddr = dev->bars[i]->base;
+                               *iosize = dev->bars[i]->maxsize;
                        }
-#ifdef DEBUG_VGAFB
-       printf("vgafb_pci_probe: mem %x addr %x size %x\n", i, addr, size);
-#endif
-                       if (size == 0 || addr == 0) {
+                       DPRINTF("io ");
+                       break;
+               case PCI_MAPREG_TYPE_MEM:
+                       if (dev->bars[i]->base == 0 ||
+                           dev->bars[i]->maxsize == 0) {
                                /* ignore this entry */
                        } else if (*memsize == 0) {
                                /*
                                 * first memory slot found goes into memory,
                                 * this is for the case of no mmio
                                 */
-                               *memaddr = addr;
-                               *memsize = size;
-                               *cacheable = tcacheable;
+                               *memaddr = dev->bars[i]->base;
+                               *memsize = dev->bars[i]->maxsize;
                        } else {
                                /*
-                                * Oh, we have a second 'memory' 
+                                * Oh, we have a second 'memory'
                                 * region, is this region the vga memory
                                 * or mmio, we guess that memory is
                                 * the larger of the two.
-                                */ 
-                                if (*memaddr >= size) {
+                                */
+                                if (*memaddr >= dev->bars[i]->maxsize) {
                                        /* this is the mmio */
-                                       *mmioaddr = addr;
-                                       /* ATI driver maps 0x80000 mmio, grr */
-                                       if (size < 0x80000) {
-                                               size = 0x80000;
-                                       }
-                                       *mmiosize = size;
+                                       *mmioaddr = dev->bars[i]->base;
+                                       *mmiosize = dev->bars[i]->maxsize;
                                 } else {
                                        /* this is the memory */
                                        *mmioaddr = *memaddr;
-                                       *memaddr = addr;
                                        *mmiosize = *memsize;
-                                       *memsize = size;
-                                       *cacheable = tcacheable;
-                                       /* ATI driver maps 0x80000 mmio, grr */
-                                       if (*mmiosize < 0x80000) {
-                                               *mmiosize = 0x80000;
-                                       }
+                                       *memaddr = dev->bars[i]->base;
+                                       *memsize = dev->bars[i]->maxsize;
                                 }
                        }
+                       DPRINTF("mem ");
+                       break;
                }
+
+               DPRINTF("addr: 0x%08x/0x%08x", dev->bars[i]->base,
+                   dev->bars[i]->maxsize);
        }
-#ifdef DEBUG_VGAFB
-       printf("vgafb_pci_probe: id %x ioaddr %x, iosize %x, memaddr %x,\n 
memsize %x, mmioaddr %x, mmiosize %x\n",
-               id, *ioaddr, *iosize, *memaddr, *memsize, *mmioaddr, *mmiosize);
-#endif
-       if (*iosize == 0) {
-               if (id == 0) {
-#ifdef powerpc
-                       /* this is only used if on openfirmware system and
-                        * the device does not have a iobase config register,
-                        * eg CirrusLogic 5434 VGA.  (they hardcode iobase to 0
-                        * thus giving standard PC addresses for the registers) 
-                        */
-                       int s;
-                       u_int32_t sizedata;
 
-                       /*
-                        * Open Firmware (yuck) shuts down devices before
-                        * entering a program so we need to bring them back
-                        * 'online' to respond to bus accesses... so far
-                        * this is true on the power.4e.
-                        */
-                       s = splhigh();
-                       sizedata = pci_conf_read(pc, pa->pa_tag,
-                               PCI_COMMAND_STATUS_REG);
-                       sizedata |= (PCI_COMMAND_MASTER_ENABLE |
-                                    PCI_COMMAND_IO_ENABLE |
-                                    PCI_COMMAND_PARITY_ENABLE |
-                                    PCI_COMMAND_SERR_ENABLE);
-                       pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
-                               sizedata);
-                       splx(s);
+       /* ATI driver maps 0x80000 mmio, grr */
+       if (*mmiosize > 0 && *mmiosize < 0x80000) {
+               *mmiosize = 0x80000;
+       }
 
-#endif
+       if (*iosize == 0) {
+               if (id == 0) {
                        /* if this is the first card, allow it
                         * to be accessed in vga iospace
                         */
@@ -206,16 +180,10 @@ vgafb_pci_probe(struct pci_attach_args *
                        *iosize = 0;
                }
        }
-#ifdef DEBUG_VGAFB
-       printf("vgafb_pci_probe: id %x ioaddr %x, iosize %x, memaddr %x,\n 
memsize %x, mmioaddr %x, mmiosize %x\n",
-               id, *ioaddr, *iosize, *memaddr, *memsize, *mmioaddr, *mmiosize);
-#endif
-       
-       /* io and mmio spaces are not required to attach */
-       if (/* *iosize == 0 || */ *memsize == 0 /* || *mmiosize == 0 */)
-               return (0);
 
-       return (1);
+       DPRINTF("\nvgafb: id %x ioaddr %x, iosize %x, memaddr %x, memsize %x,"
+           " mmioaddr %x, mmiosize %x",
+           id, *ioaddr, *iosize, *memaddr, *memsize, *mmioaddr, *mmiosize);
 }
 
 int
@@ -251,7 +219,7 @@ vgafb_pci_attach(struct device *parent, 
        struct pci_attach_args *pa = aux;
        struct vgafb_pci_softc *sc = (struct vgafb_pci_softc *)self;
        struct vgafb_config *vc;
-       u_int32_t memaddr, memsize, cacheable;
+       u_int32_t memaddr, memsize;
        u_int32_t ioaddr, iosize;
        u_int32_t mmioaddr, mmiosize;
        int console;
@@ -260,8 +228,8 @@ vgafb_pci_attach(struct device *parent, 
 
        myid = id;
 
-       vgafb_pci_probe(pa, myid, &ioaddr, &iosize,
-               &memaddr, &memsize, &cacheable, &mmioaddr, &mmiosize);
+       vgafb_pci_bar_init(sc, pa, myid, &ioaddr, &iosize,
+               &memaddr, &memsize, &mmioaddr, &mmiosize);
 
 
        console = (!bcmp(&pa->pa_tag, &vgafb_pci_console_tag, 
sizeof(pa->pa_tag)));
@@ -313,18 +281,9 @@ vgafb_pci_console(bus_space_tag_t iot, u
        pa->pa_iot = iot;
        pa->pa_memt = memt;
        pa->pa_tag = vgafb_pci_console_tag;
-       /* 
-       pa->pa_pc = XXX;
-        */
-
-/* XXX probe pci before pci bus config? */
 
        mmioaddr =0;
        mmiosize =0;
-#if 0
-       vgafb_pci_probe(pa, 0, &ioaddr, &iosize,
-               &memaddr, &memsize, &cacheable, mmioaddr, mmiosize);
-#endif
 
 
        /* set up bus-independent VGA configuration */
Index: pci/vgafb_pcivar.h
===================================================================
RCS file: /cvs/src/sys/arch/macppc/pci/vgafb_pcivar.h,v
retrieving revision 1.5
diff -u -p -r1.5 vgafb_pcivar.h
--- pci/vgafb_pcivar.h  5 Nov 2007 19:24:31 -0000       1.5
+++ pci/vgafb_pcivar.h  22 Feb 2012 18:13:29 -0000
@@ -36,6 +36,21 @@
             (PCI_CLASS(class) == PCI_CLASS_PREHISTORIC &&              \
              PCI_SUBCLASS(class) == PCI_SUBCLASS_PREHISTORIC_VGA)) ? 1 : 0)
 
+#define VGA_PCI_MAX_BARS 6
+
+struct vga_pci_bar {
+       int             addr;
+       u_int           mapped;
+       pcireg_t        maptype;
+       bus_addr_t      base;
+       bus_size_t      size;
+       bus_size_t      maxsize;
+       bus_space_tag_t bst;
+       bus_space_handle_t bsh;
+       int             flags;
+       void            *vaddr;
+};
+
 void    vgafb_pci_console(bus_space_tag_t,
                u_int32_t ioaddr, u_int32_t iosize,
                bus_space_tag_t,

Reply via email to