Subject: XFree86 4.2 diffs for using the display device for pci ops
To: None <tech-x11@netbsd.org>
From: Simon Burge <simonb@wasabisystems.com>
List: tech-x11
Date: 06/22/2002 04:45:21
Folks,

I've been playing around with the following diffs for the Xserver.
These are semi-cleaned up version of the Alpha diffs I did a while ago,
but unfortunately they don't yet work on the Alpha due to problems with
the sparse address space mappings there.

So far they've only been tested on my main box at home (using a NVidia
Riva TNT2 M64) and my laptop (using a ATI Rage 128 Mobility LF).

They _should_ work on any port that uses wscons (except Alpha - see
above).  I'd be curious of any reports of sucess or failure on any
variety of machines.

Simon.
--
Simon Burge                            <simonb@wasabisystems.com>
NetBSD Support and Service:         http://www.wasabisystems.com/


Index: xc/config/cf/NetBSD.cf
===================================================================
RCS file: /cvsroot/xsrc/xfree/xc/config/cf/NetBSD.cf,v
retrieving revision 1.27
diff -d -p -u -r1.27 NetBSD.cf
--- xc/config/cf/NetBSD.cf	2002/04/29 12:58:33	1.27
+++ xc/config/cf/NetBSD.cf	2002/06/19 22:28:59
@@ -190,7 +190,13 @@ XCOMM operating system:  OSName (OSMajor
 # else
 #  define ServerOSDefines	XFree86ServerOSDefines
 # endif
-# if OSMajorVersion == 1 && OSMinorVersion >= 1 || OSMajorVersion >= 2
+  /* libpci support from NetBSD 1.5Y onwards */
+# if ((OSMajorVersion > 1) || \
+      (OSMajorVersion == 1 && OSMinorVersion >= 6) || \
+      (OSMajorVersion == 1 && OSMinorVersion == 5 && \
+       OSTeenyVersion >= 25))
+#  define ServerExtraSysLibs	-li386 -lpci
+# elif OSMajorVersion == 1 && OSMinorVersion >= 1 || OSMajorVersion >= 2
 #  define ServerExtraSysLibs	-li386
 # endif
 #else
@@ -206,10 +212,22 @@ XCOMM operating system:  OSName (OSMajor
 # else
 #  define ServerOSDefines 	/**/
 # endif
-# if defined(AlphaArchitecture)
-#  define ServerExtraSysLibs	-lalpha
+  /* libpci support from NetBSD 1.5Y onwards */
+# if ((OSMajorVersion > 1) || \
+      (OSMajorVersion == 1 && OSMinorVersion >= 6) || \
+      (OSMajorVersion == 1 && OSMinorVersion == 5 && \
+       OSTeenyVersion >= 25))
+#  if defined(AlphaArchitecture)
+#   define ServerExtraSysLibs	-lpci -lalpha
+#  else
+#   define ServerExtraSysLibs	-lpci
+#  endif
 # else
-#  define ServerExtraSysLibs	/**/
+#  if defined(AlphaArchitecture)
+#   define ServerExtraSysLibs	-lalpha
+#  else
+#   define ServerExtraSysLibs	/**/
+#  endif
 # endif
 #endif
 #ifdef i386Architecture
Index: xc/programs/Xserver/hw/xfree86/common/xf86Config.c
===================================================================
RCS file: /cvsroot/xsrc/xfree/xc/programs/Xserver/hw/xfree86/common/xf86Config.c,v
retrieving revision 1.1.1.4
diff -d -p -u -r1.1.1.4 xf86Config.c
--- common/xf86Config.c	2002/01/19 15:06:47	1.1.1.4
+++ common/xf86Config.c	2002/06/12 05:42:15
@@ -41,7 +41,9 @@ extern DeviceAssocRec mouse_assoc;
 #if (defined(i386) || defined(__i386__)) && \
     (defined(__FreeBSD__) || defined(__NetBSD__) || defined(linux) || \
      (defined(SVR4) && !defined(sun)) || defined(__GNU__))
+#ifndef __NetBSD__	/* XXX this calls xf86ReadBIOS which isn't available with new wscons code */
 #define SUPPORT_PC98
+#endif
 #endif
 
 #ifdef __EMX__
Index: xc/programs/Xserver/hw/xfree86/os-support/bsd/bsd_axp.c
===================================================================
RCS file: /cvsroot/xsrc/xfree/xc/programs/Xserver/hw/xfree86/os-support/bsd/bsd_axp.c,v
retrieving revision 1.1.1.2
diff -d -p -u -r1.1.1.2 bsd_axp.c
--- os-support/bsd/bsd_axp.c	2001/06/09 15:08:13	1.1.1.2
+++ os-support/bsd/bsd_axp.c	2002/06/12 05:42:18
@@ -6,13 +6,15 @@
 #include "xf86Priv.h"
 #include "xf86Axp.h"
 #include "xf86_OSlib.h"
-#include <stdio.h>
-#include <sys/sysctl.h>
 
 axpDevice bsdGetAXP(void);
 
+#ifdef __FreeBSD__
+#include <stdio.h>
+#include <sys/sysctl.h>
+
 /*
- * BSD does a very nice job providing system information to
+ * FreeBSD does a very nice job providing system information to
  * user space programs. Unfortunately it doesn't provide all
  * the information required. Therefore we just obtain the
  * system type and look up the rest from a list we maintain
@@ -55,3 +57,19 @@ bsdGetAXP(void)
 			return axpList[i].type;
 	}
 }	
+#elif defined(__NetBSD__)
+/*
+ * There is no need to frob the HAE at all under NetBSD.  So here, we
+ * just return a systype that doesn't use BXW so that at least the code
+ * in bsd_video.c doesn't break.  The results of any calculations are
+ * passed to a sethae() that does nothing (and may even be optimised
+ * away if the compiler is smart enough).
+ */
+axpDevice
+bsdGetAXP(void)
+{
+	return CIA;
+}
+#else
+# error Unknown BSD system
+#endif
Index: xc/programs/Xserver/hw/xfree86/os-support/bsd/bsd_video.c
===================================================================
RCS file: /cvsroot/xsrc/xfree/xc/programs/Xserver/hw/xfree86/os-support/bsd/bsd_video.c,v
retrieving revision 1.8
diff -d -p -u -r1.8 bsd_video.c
--- os-support/bsd/bsd_video.c	2002/01/19 23:19:35	1.8
+++ os-support/bsd/bsd_video.c	2002/06/12 05:42:20
@@ -59,6 +59,10 @@
 
 /* $XConsortium: bsd_video.c /main/10 1996/10/25 11:37:57 kaleb $ */
 
+#ifdef __NetBSD__
+#include <sys/param.h>		/* for __NetBSD_Version__ */
+#endif
+
 #include "X.h"
 #include "xf86.h"
 #include "xf86Priv.h"
@@ -132,17 +136,18 @@ struct memAccess ioMemInfo = { CONSOLE_G
 		"Check that you have set 'machdep.allowaperture=2'\n" \
 		"\tin /etc/sysctl.conf and reboot your machine\n" \
 		"\trefer to xf86(4) for details\n"
+#ifdef __powerpc__
+#define	__OpenBSD_powerpc__
+#endif
 #endif
 
 #ifdef __alpha__
-#ifdef __FreeBSD__
-extern unsigned long dense_base(void);
-
 static int axpSystem = -1;
 static unsigned long hae_thresh;
 static unsigned long hae_mask;
-static unsigned long bus_base;
-static unsigned long sparse_size;
+
+#ifdef __FreeBSD__
+extern unsigned long dense_base(void);
 
 static unsigned long
 memory_base(void)
@@ -252,7 +257,7 @@ static void unmapVidMem(int, pointer, un
 static pointer mapVidMemSparse(int, unsigned long, unsigned long, int);
 static void unmapVidMemSparse(int, pointer, unsigned long);
 #endif
-#ifdef __powerpc__
+#ifdef __OpenBSD_powerpc__
 static pointer ppcMapVidMem(int, unsigned long, unsigned long);
 static void ppcUnmapVidMem(int, pointer, unsigned long);
 #endif
@@ -268,7 +273,7 @@ static void NetBSDundoWC(int, pointer);
 #endif
 
 
-#if !defined(__powerpc__)
+#if !defined(__OpenBSD_powerpc__)
 /*
  * Check if /dev/mem can be mmap'd.  If it can't print a warning when
  * "warn" is TRUE.
@@ -284,6 +289,27 @@ checkDevMem(Bool warn)
 	    return;
 	devMemChecked = TRUE;
 
+#if defined(__NetBSD__) && __NetBSD_Version__ >= 105250000
+	base = mmap(0, getpagesize(), PROT_READ | PROT_WRITE,
+	    MAP_SHARED, xf86Info.screenFd, 0xa0000);
+	if (base != MAP_FAILED)
+	{
+		munmap(base, getpagesize());
+		/* XXX
+		 * We use "useDevMem = TRUE" since the wsdisplay code
+		 * uses the /dev/mem semantics assumed by the rest of
+		 * this code.
+		 */
+		useDevMem = TRUE;
+		devMemFd = xf86Info.screenFd;
+		return;
+	}
+	else
+	{
+		xf86Msg(X_WARNING, "checkDevMem: failed to mmap %s (%s)\n",
+		    "wscons display", strerror(errno));
+	}
+#endif
 	if ((fd = open(DEV_MEM, O_RDWR)) >= 0)
 	{
 	    /* Try to map a page at the VGA address */
@@ -363,12 +389,12 @@ checkDevMem(Bool warn)
 
 #endif
 }
-#endif /* !__powerpc__ */
+#endif /* !__OpenBSD_powerpc__ */
 
 void
 xf86OSInitVidMem(VidMemInfoPtr pVidMem)
 {
-#if defined(__powerpc__)
+#if defined(__OpenBSD_powerpc__)
 	pVidMem->linearSupported = TRUE;
 #else
 	checkDevMem(TRUE);
@@ -387,12 +413,11 @@ xf86OSInitVidMem(VidMemInfoPtr pVidMem)
                 axpSystem = bsdGetAXP(); 
 	    hae_thresh = xf86AXPParams[axpSystem].hae_thresh;
             hae_mask = xf86AXPParams[axpSystem].hae_mask;
-            sparse_size = xf86AXPParams[axpSystem].size;
 	}
 #elif defined(__arm32__)
 	pVidMem->mapMem = armMapVidMem;
 	pVidMem->unmapVidMem = armUnmapVidMem;
-#elif defined(__powerpc__)
+#elif defined(__OpenBSD_powerpc__)
 	pVidMem->mapMem = ppcMapVidMem;
 	pVidMem->unmapMem = ppcUnmapVidMem;
 #else
@@ -415,7 +440,7 @@ xf86OSInitVidMem(VidMemInfoPtr pVidMem)
 	pVidMem->initialised = TRUE;
 }
 
-#if !defined(__powerpc__)
+#if !defined(__OpenBSD_powerpc__)
 static pointer
 mapVidMem(int ScreenNum, unsigned long Base, unsigned long Size, int flags)
 {
@@ -524,7 +549,7 @@ xf86ReadBIOS(unsigned long Base, unsigne
 	return(Len);
 }
 
-#endif /* !__powerpc__ */
+#endif /* !__OpenBSD_powerpc__ */
 
 #ifdef __arm32__
 
@@ -702,7 +727,7 @@ armUnmapVidMem(int ScreenNum, pointer Ba
 }
 #endif /* __arm32__ */
 
-#if defined(__powerpc__)
+#if defined(__OpenBSD_powerpc__)
 
 volatile unsigned char *ioBase = MAP_FAILED;
 
@@ -758,7 +783,7 @@ xf86ReadBIOS(unsigned long Base, unsigne
 }
 
 
-#endif /* __powerpc__ */
+#endif /* __OpenBSD_powerpc__ */
 
 #ifdef USE_I386_IOPL
 /***************************************************************************/
@@ -1561,6 +1586,7 @@ mapVidMemSparse(int ScreenNum, unsigned 
 {
     static Bool was_here = FALSE;
 
+xf86Msg(X_INFO, "mapVidMemSparse: base = 0x%lx, size = 0x%lx\n", Base, Size);
     if (!was_here) {
       was_here = TRUE;
 
@@ -1576,13 +1602,21 @@ mapVidMemSparse(int ScreenNum, unsigned 
       xf86ReadMmio16 = readSparse16;
       xf86ReadMmio32 = readSparse32;
 	
+#if 1
 xf86Msg(X_INFO, "memBase = mmap(0, 0x100000000, ... %#lx)\n", BUS_BASE);		/* XXXX */
-      memBase = mmap((caddr_t)0, 0x100000000,
+#else
+xf86Msg(X_INFO, "memBase = mmap(0, 0x%lx, ... %#lx)\n", Size, BUS_BASE);		/* XXXX */
+#endif
+      memBase = mmap((caddr_t)0, /* 0x100000000 */ Size,
 		     PROT_READ | PROT_WRITE,
 		     MAP_SHARED, devMemFd,
 		     (off_t) BUS_BASE);
+#if 1
 xf86Msg(X_INFO, "memSBase = mmap(0, 0x100000000, ... %#lx)\n", BUS_BASE_BWX);		/* XXXX */
-      memSBase = mmap((caddr_t)0, 0x100000000,
+#else
+xf86Msg(X_INFO, "memSBase = mmap(0, 0x%lx, ... %#lx)\n", Size << 5, BUS_BASE_BWX);		/* XXXX */
+#endif
+      memSBase = mmap((caddr_t)0, /* 0x100000000 */ Size << 5,
 		      PROT_READ | PROT_WRITE,
 		      MAP_SHARED, devMemFd,
 		      (off_t) BUS_BASE_BWX);
Index: xc/programs/Xserver/hw/xfree86/os-support/bus/Imakefile
===================================================================
RCS file: /cvsroot/xsrc/xfree/xc/programs/Xserver/hw/xfree86/os-support/bus/Imakefile,v
retrieving revision 1.5
diff -d -p -u -r1.5 Imakefile
--- os-support/bus/Imakefile	2002/01/19 23:19:35	1.5
+++ os-support/bus/Imakefile	2002/06/12 05:42:21
@@ -7,7 +7,14 @@ XCOMM $XFree86: xc/programs/Xserver/hw/x
 
 #include <Server.tmpl>
 
-#if defined(LinuxArchitecture) && defined(AlphaArchitecture)
+#if defined(NetBSDArchitecture)
+
+XCOMM NetBSD PCI driver
+
+PCIDRVRSRC = netbsdPci.c
+PCIDRVROBJ = netbsdPci.o
+
+#elif defined(LinuxArchitecture) && defined(AlphaArchitecture)
 
 XCOMM Alpha (Linux) PCI driver
 
@@ -63,18 +70,10 @@ PCIDRVROBJ = ix86Pci.o linuxPci.o
 
 #elif defined(FreeBSDArchitecture) && defined(AlphaArchitecture)
 
-
 XCOMM generic FreeBSD PCI driver (using /dev/pci)
 
 PCIDRVRSRC = freebsdPci.c
 PCIDRVROBJ = freebsdPci.o
-
-#elif defined(NetBSDArchitecture) && defined(AlphaArchitecture)
-
-XCOMM Alpha (NetBSD) PCI driver
-
-PCIDRVRSRC = netbsdPci.c
-PCIDRVROBJ = netbsdPci.o
 
 #elif defined(i386Architecture) || defined(ia64Architecture)
 
Index: xc/programs/Xserver/hw/xfree86/os-support/bus/Pci.h
===================================================================
RCS file: /cvsroot/xsrc/xfree/xc/programs/Xserver/hw/xfree86/os-support/bus/Pci.h,v
retrieving revision 1.5
diff -d -p -u -r1.5 Pci.h
--- os-support/bus/Pci.h	2002/01/19 23:19:35	1.5
+++ os-support/bus/Pci.h	2002/06/12 05:42:21
@@ -144,6 +144,9 @@
 #if (defined(__powerpc__) || defined(__mips__) || defined(__sh__) || defined(__mc68000__) || defined(__arm__) || defined(__s390__) || defined(__hppa__)) && defined(linux)
 # define ARCH_PCI_INIT linuxPciInit
 # define INCLUDE_XF86_MAP_PCI_MEM
+#elif defined(__NetBSD__)
+# define ARCH_PCI_INIT netbsdPciInit
+# define INCLUDE_XF86_MAP_PCI_MEM
 #elif defined(__powerpc__) && defined(__OpenBSD__)
 # define  ARCH_PCI_INIT freebsdPciInit
 # define INCLUDE_XF86_MAP_PCI_MEM
@@ -167,9 +170,6 @@
 # define INCLUDE_XF86_MAP_PCI_MEM
 #elif defined(__alpha__) && defined(__FreeBSD__)
 # define ARCH_PCI_INIT freebsdPciInit
-# define INCLUDE_XF86_MAP_PCI_MEM
-#elif defined(__alpha__) && defined(__NetBSD__)
-# define ARCH_PCI_INIT netbsdPciInit
 # define INCLUDE_XF86_MAP_PCI_MEM
 #else
 # define ARCH_PCI_INIT ix86PciInit
Index: xc/programs/Xserver/hw/xfree86/os-support/bus/netbsdPci.c
===================================================================
RCS file: /cvsroot/xsrc/xfree/xc/programs/Xserver/hw/xfree86/os-support/bus/netbsdPci.c,v
retrieving revision 1.3
diff -d -p -u -r1.3 netbsdPci.c
--- os-support/bus/netbsdPci.c	2001/01/15 15:00:20	1.3
+++ os-support/bus/netbsdPci.c	2002/06/12 05:42:21
@@ -36,25 +36,37 @@
  * POSSIBILITY OF SUCH DAMAGE.
  */
 
-#include "Pci.h"
+#include <sys/types.h>
+#include <sys/ioctl.h>
+#include <pci.h>
 
-#if defined(__alpha__)
-#include <machine/pio.h>
-#include <machine/sysarch.h>
+#include "xf86Priv.h"
+#include "Pci.h"
 
 /*
- * NetBSD/alpha platform specific PCI access functions.
- * Needs NetBSD kernel version 1.5 or later.
+ * NetBSD generic PCI access functions.
+ * Needs NetBSD kernel version 1.5ZC(ish) or later.
  */
 CARD32 netbsdPciCfgRead(PCITAG tag, int off);
 void netbsdPciCfgWrite(PCITAG, int off, CARD32 val);
 void netbsdPciCfgSetBits(PCITAG tag, int off, CARD32 mask, CARD32 bits);
+PCITAG netbsdPciFindFirst(void);
+PCITAG netbsdPciFindNext(void);
 
 pciBusInfo_t netbsdPci0 = {
-	PCI_CFG_MECH_OTHER, 32, FALSE, 0, 0, 0,
-	{ netbsdPciCfgRead, netbsdPciCfgWrite, netbsdPciCfgSetBits,
-	  pciAddrNOOP, pciAddrNOOP},
-	NULL
+/* configMech */	PCI_CFG_MECH_OTHER,
+/* numDevices */	32,
+/* secondary */		FALSE,
+/* primary_bus */	0,
+/* ppc_io_base */	0,
+/* ppc_io_size */	0,
+/* funcs */		{
+                          netbsdPciCfgRead,
+                          netbsdPciCfgWrite,
+                          netbsdPciCfgSetBits,
+                          pciAddrNOOP,
+                          pciAddrNOOP},
+/* pciBusPriv */	NULL
 };
 
 #define BUS(tag)	(((tag)>>16)&0xff)
@@ -63,28 +75,37 @@ pciBusInfo_t netbsdPci0 = {
 #define FNC(dfn)	((DFN(tag))&0x7)
 
 void  
-netbsdPciInit()
+netbsdPciInit(void)
 {
-	if (alpha_pci_io_enable(1) != 0)
-		fprintf(stderr, "alpha_pci_io_enable failed!\n");
 
 	pciNumBuses    = 1;
 	pciBusInfo[0]  = &netbsdPci0;
-	pciFindFirstFP = pciGenFindFirst;
-	pciFindNextFP  = pciGenFindNext;
+	pciFindFirstFP = netbsdPciFindFirst;
+	pciFindNextFP  = netbsdPciFindNext;
+
+	/* XXX */
+	/* pciFindFirstFP = pciGenFindFirst; */
+	/* pciFindNextFP  = pciGenFindNext; */
+	/* XXX */
 }
 
 CARD32
 netbsdPciCfgRead(PCITAG tag, int off)
 {
-	return(alpha_pci_conf_read(BUS(tag), DEV(tag), FNC(tag), off));
-		return ~0;
+	pcireg_t val;
+
+	if (pcidev_conf_read(xf86Info.screenFd, off, &val) < 0)
+		return (~0);
+	else
+		return (val);
 }
 
 void
 netbsdPciCfgWrite(PCITAG tag, int off, CARD32 val)
 {
-	alpha_pci_conf_write(BUS(tag), DEV(tag), FNC(tag), off, val);
+
+	/* XXX ignore errors */
+	pcidev_conf_write(xf86Info.screenFd, off, val);
 }
 
 void
@@ -94,7 +115,25 @@ netbsdPciCfgSetBits(PCITAG tag, int off,
 	val = (val & ~mask) | (bits & mask);
 	netbsdPciCfgWrite(tag, off, val);
 }
-/* #elif defined(__something_else__) */
-#else
-# error architecture not supported
-#endif
+
+PCITAG
+netbsdPciFindFirst(void)
+{
+
+	/* XXX
+	 * "0" is a valid PCI tag, and we don't really care what the tag
+	 * is since we ignore it in the above access functions.
+	 */
+	return (0);
+}
+
+PCITAG
+netbsdPciFindNext(void)
+{
+
+	/*
+	 * Only one device for now, until we can probe multiple
+	 * wsdisplay devices.
+	 */
+	return(PCI_NOT_FOUND);
+}