Wanna see this logo while booting your 2.6 kernel? Click here!

17.03.2009 21:40

Trying to port the mga driver to Xorg 1.5


As people using the unofficial mga driver already know: It doesn't work with Xorg 1.5 because too many things in the API have been changed.
Currently, the only error message when trying to load the mga module in Xorg 1.5 is:
dlopen: /usr/lib/xorg/modules//drivers/mga_drv.so: undefined symbol: xf86ScreenIndex
(EE) Failed to load /usr/lib/xorg/modules//drivers/mga_drv.so
(EE) Failed to load module "mga" (loader failed, 7)
(EE) No drivers available.

The reason for that is, that the driver doesn't make use of the libpciaccess functions that have been introduced in Xorg 1.5 (strangely, a search for xf86ScreenIndex doesn't point out a single LOC inside the mga driver source, nor is it referenced by any participating module, but it is used by some PCI related functions for unmapping video memory, etc.).

Anyhow, my time at the moment is _VERY_ limited, but I tried to start with some changes that will eventually end in a working driver version some time soon.

Whoever reads this and can code and wants to help out those poor mga driver users: Please help us/me to complete a working driver (if possible at all, I still don't know for now, if things have to be changed in the mgaHal library, we're out of luck anyways).

As a sidenote: There's a great resource on the X.org wiki that deals with how to rework the PCI access functions, here's the link:

http://www.x.org/wiki/PciReworkHowto

And here's what I've come up with so far:
Download Link: mga-xorg-1.5-migration-step1.diff
diff -Nur a/7.0.0/src/mga/src/clientlx.c b/7.0.0/src/mga/src/clientlx.c
--- a/7.0.0/src/mga/src/clientlx.c	2006-11-06 18:32:44.000000000 +0100
+++ b/7.0.0/src/mga/src/clientlx.c	2009-03-17 22:07:01.000000000 +0100
@@ -67,8 +67,11 @@
 
     ASSERT_HANDLER(pBoard);
 
+#if XSERVER_LIBPCIACCESS
+    pci_device_cfg_read_u8(pMga->PciInfo, *pucByte, ulOffset);
+#else
     *pucByte = pciReadByte(pMga->PciTag,ulOffset);
-
+#endif
     return 0;
 }
 
@@ -95,7 +98,11 @@
 
     ASSERT_HANDLER(pBoard);
 
+#if XSERVER_LIBPCIACCESS
+    pci_device_cfg_read_u32(pMga->PciInfo, *pulDword, ulOffset);
+#else
     *pulDword = pciReadLong(pMga->PciTag,ulOffset);
+#endif
 
     return 0;
 }
diff -Nur a/7.0.0/src/mga/src/mga.h b/7.0.0/src/mga/src/mga.h
--- a/7.0.0/src/mga/src/mga.h	2007-01-23 08:38:09.000000000 +0100
+++ b/7.0.0/src/mga/src/mga.h	2009-03-17 22:12:56.000000000 +0100
@@ -27,6 +27,11 @@
 
 #include "xf86int10.h"
 
+#include "xorg-server.h"
+#ifdef XSERVER_LIBPCIACCESS
+#include <pciaccess.h>
+#endif
+
 #ifdef XF86DRI
 #include "xf86drm.h"
 
@@ -48,6 +53,17 @@
 #include "client.h"
 #endif
 
+#if XSERVER_LIBPCIACCESS
+#define MGA_DEVICE_MATCH(d,i) \
+    { 0x102b, (d), PCI_MATCH_ANY, PCI_MATCH_ANY, 0, 0, (i) }
+
+static const struct pci_id_match mga_device_match[] = {
+   MGA_DEVICE_MATCH (PCI_MATCH_ANY, 0 ),
+    { 0, 0, 0 },
+};
+
+#endif /* XSERVER_LIBPCIACCESS */
+
 #define MGA_XVIMAGE_LIMIT_WIDTH 1280
 #define MGA_XVIMAGE_LIMIT_HEIGHT 1280
 
@@ -165,6 +181,22 @@
 #define MGA_MINOR_VERSION 4
 #define MGA_PATCHLEVEL 1
 
+#if XSERVER_LIBPCIACCESS
+#define MGA_MEMBASE(p,n) (p)->regions[(n)].base_addr
+#define VENDOR_ID(p)      (p)->vendor_id
+#define DEVICE_ID(p)      (p)->device_id
+#define SUBVENDOR_ID(p)          (p)->subvendor_id
+#define SUBSYS_ID(p)      (p)->subdevice_id
+#define CHIP_REVISION(p)  (p)->revision
+#else
+#define MGA_MEMBASE(p,n) (p)->memBase[n]
+#define VENDOR_ID(p)      (p)->vendor
+#define DEVICE_ID(p)      (p)->chipType
+#define SUBVENDOR_ID(p)          (p)->subsysVendor
+#define SUBSYS_ID(p)      (p)->subsysCard
+#define CHIP_REVISION(p)  (p)->chipRev
+#endif
+
 typedef struct {
     unsigned char	ExtVga[6];
     unsigned char 	DacClk[6];
@@ -397,8 +429,15 @@
     EntityInfoPtr	pEnt;
     struct mga_bios_values bios;
     CARD8               BiosOutputMode;
+#if XSERVER_LIBPCIACCESS
+    struct pci_device *PciInfo;
+    int mmio_bar;
+    int fb_bar;
+    int gtt_bar;
+#else
     pciVideoPtr		PciInfo;
     PCITAG		PciTag;
+#endif
     xf86AccessRec	Access;
     int			Chipset;
     int                 ChipRev;
diff -Nur a/7.0.0/src/mga/src/mga_bios.c b/7.0.0/src/mga/src/mga_bios.c
--- a/7.0.0/src/mga/src/mga_bios.c	2006-11-06 18:35:15.000000000 +0100
+++ b/7.0.0/src/mga/src/mga_bios.c	2009-03-17 22:06:20.000000000 +0100
@@ -451,8 +451,12 @@
     }
 
     if (pciBIOS) {
+#if XSERVER_LIBPCIACCESS
+  rlen = pci_device_read_rom(pMga->PciInfo, bios_data);
+#else
 	rlen = xf86ReadPciBIOS(0, pMga->PciTag, pMga->FbBaseReg,
 			       bios_data, sizeof(bios_data));
+#endif
     }
     else {
 	rlen = xf86ReadDomainMemory(pMga->PciTag, pMga->BiosAddress,
diff -Nur a/7.0.0/src/mga/src/mga_dac3026.c b/7.0.0/src/mga/src/mga_dac3026.c
--- a/7.0.0/src/mga/src/mga_dac3026.c	2006-11-06 18:35:33.000000000 +0100
+++ b/7.0.0/src/mga/src/mga_dac3026.c	2009-03-17 22:07:13.000000000 +0100
@@ -865,9 +865,13 @@
 	
 	for (i = 0; i < DACREGSIZE; i++)
 		mgaReg->DacRegs[i]	 = inTi3026(MGADACregs[i]);
-	
+
+#if XSERVER_LIBPCIACCESS
+  pci_device_cfg_read_u32(pMga->PciInfo, mgaReg->Option, PCI_OPTION_REG);
+#else	
 	mgaReg->Option = pciReadLong(pMga->PciTag, PCI_OPTION_REG);
-	
+#endif	
+
 #ifdef DEBUG		
 	ErrorF("read: %02X %02X %02X	%02X %02X %02X	%08lX\n",
 		mgaReg->DacClk[0], mgaReg->DacClk[1], mgaReg->DacClk[2], mgaReg->DacClk[3], mgaReg->DacClk[4], mgaReg->DacClk[5], mgaReg->Option);
diff -Nur a/7.0.0/src/mga/src/mga_dacG.c b/7.0.0/src/mga/src/mga_dacG.c
--- a/7.0.0/src/mga/src/mga_dacG.c	2006-11-06 18:52:16.000000000 +0100
+++ b/7.0.0/src/mga/src/mga_dacG.c	2009-03-17 22:07:33.000000000 +0100
@@ -962,11 +962,20 @@
 
         mgaReg->PIXPLLCSaved = TRUE;
 
+#if XSERVER_LIBPCIACCESS
+  pci_device_cfg_read_u32(pMga->PciInfo, mgaReg->Option, PCI_OPTION_REG);
+  pci_device_cfg_read_u32(pMga->PciInfo, mgaReg->Option2, PCI_MGA_OPTION2);
+#else
 	mgaReg->Option = pciReadLong(pMga->PciTag, PCI_OPTION_REG);
-
 	mgaReg->Option2 = pciReadLong(pMga->PciTag, PCI_MGA_OPTION2);
+#endif
+
 	if (pMga->Chipset == PCI_CHIP_MGAG400 || pMga->Chipset == PCI_CHIP_MGAG550)
+#if XSERVER_LIBPCIACCESS
+      pci_device_cfg_read_u32(pMga->PciInfo, mgaReg->Option3, PCI_MGA_OPTION3);
+#else
 	    mgaReg->Option3 = pciReadLong(pMga->PciTag, PCI_MGA_OPTION3);
+#endif
 	);	/* MGA_NOT_HAL */
 
 	for (i = 0; i < 6; i++)
diff -Nur a/7.0.0/src/mga/src/mga_dri.c b/7.0.0/src/mga/src/mga_dri.c
--- a/7.0.0/src/mga/src/mga_dri.c	2006-11-06 18:57:11.000000000 +0100
+++ b/7.0.0/src/mga/src/mga_dri.c	2009-03-17 21:43:01.000000000 +0100
@@ -606,8 +606,8 @@
 	xf86DrvMsg( pScreen->myNum, X_INFO,
 		    "[agp] Mode 0x%08lx [AGP 0x%04x/0x%04x; Card 0x%04x/0x%04x]\n",
 		    mode, vendor, device,
-		    pMga->PciInfo->vendor,
-		    pMga->PciInfo->chipType );
+		    VENDOR_ID(pMga->PciInfo),
+		    DEVICE_ID(pMga->PciInfo));
 
 	if ( drmAgpEnable( pMga->drmFD, mode ) < 0 ) {
 	    xf86DrvMsg( pScreen->myNum, X_ERROR, "[agp] AGP not enabled\n" );
@@ -858,9 +858,17 @@
    if (!pMga->irq) {
       pMga->irq = drmGetInterruptFromBusID(
 	 pMga->drmFD,
+#if XSERVER_LIBPCIACCESS
+   ((pMga->PciInfo->domain << 8 ) |
+   pMga->PciInfo->bus),
+   pMga->PciInfo->dev,
+   pMga->PciInfo->func
+#else
 	 ((pciConfigPtr)pMga->PciInfo->thisCard)->busnum,
 	 ((pciConfigPtr)pMga->PciInfo->thisCard)->devnum,
-	 ((pciConfigPtr)pMga->PciInfo->thisCard)->funcnum);
+	 ((pciConfigPtr)pMga->PciInfo->thisCard)->funcnum
+#endif
+   );
 
       if((drmCtlInstHandler(pMga->drmFD, pMga->irq)) != 0) {
 	 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
@@ -971,9 +979,15 @@
    } else {
       pDRIInfo->busIdString = xalloc(64);
       sprintf( pDRIInfo->busIdString, "PCI:%d:%d:%d",
+#if XSERVER_LIBPCIACCESS
+         ((pMga->PciInfo->domain << 8 ) | pMga->PciInfo->bus),
+				 pMga->PciInfo->dev, pMga->PciInfo->func
+#else
 	       ((pciConfigPtr)pMga->PciInfo->thisCard)->busnum,
 	       ((pciConfigPtr)pMga->PciInfo->thisCard)->devnum,
-	       ((pciConfigPtr)pMga->PciInfo->thisCard)->funcnum );
+	       ((pciConfigPtr)pMga->PciInfo->thisCard)->funcnum
+#endif
+   );
    }
    pDRIInfo->ddxDriverMajorVersion = MGA_MAJOR_VERSION;
    pDRIInfo->ddxDriverMinorVersion = MGA_MINOR_VERSION;
diff -Nur a/7.0.0/src/mga/src/mga_driver.c b/7.0.0/src/mga/src/mga_driver.c
--- a/7.0.0/src/mga/src/mga_driver.c	2007-01-23 08:52:36.000000000 +0100
+++ b/7.0.0/src/mga/src/mga_driver.c	2009-03-17 22:07:40.000000000 +0100
@@ -180,10 +180,19 @@
     MGA_VERSION,
     MGA_DRIVER_NAME,
     MGAIdentify,
+#if XSERVER_LIBPCIACCESS
+	  NULL,
+#else
     MGAProbe,
+#endif
     MGAAvailableOptions,
     NULL,
-    0
+    0,
+    NULL,
+#if XSERVER_LIBPCIACCESS
+    mga_device_match,
+    mga_pci_probe
+#endif
 };
 
 /* Supported chipsets */
@@ -485,7 +494,13 @@
 
     if (!setupDone) {
 	setupDone = TRUE;
-	xf86AddDriver(&MGA_C_NAME, module, 0);
+	xf86AddDriver(&MGA_C_NAME, module, 
+#if XSERVER_LIBPCIACCESS
+	HaveDriverFuncs
+#else
+	0
+#endif
+  );
 
 	/*
 	 * Modules that this driver always requires may be loaded here
@@ -1093,7 +1108,12 @@
     ErrorF("mga: VgaIOSave: %d:%d:%d\n", sMga->pvp->bus, sMga->pvp->device,
 	   sMga->pvp->func);
 #endif
+
+#if XSERVER_LIBPCIACCESS
+    sMga->enable = 0;
+#else
     sMga->enable = (pciReadLong(tag, PCI_OPTION_REG) & 0x100) != 0;
+#endif
 }
 
 static void
@@ -2303,7 +2323,13 @@
  	     (pMga->Chipset != PCI_CHIP_MGA2164_AGP)) 
  #endif
     {
-        CARD32 option_reg = pciReadLong(pMga->PciTag, PCI_OPTION_REG);
+        CARD32 option_reg;
+
+#if XSERVER_LIBPCIACCESS
+  pci_device_cfg_read_u32(pMga->PciInfo, option_reg, PCI_OPTION_REG);
+#else	
+  option_reg = pciReadLong(pMga->PciTag, PCI_OPTION_REG);
+#endif
 	if(!(option_reg & (1 << 14))) {
 	    pMga->HasSDRAM = TRUE;
 	    xf86DrvMsg(pScrn->scrnIndex, X_PROBED, "Has SDRAM\n");