Subject: Re: DRM on 4.99.42 i386
To: None <tech-kern@netbsd.org, tech-x11@netbsd.org>
From: Yorick Hardy <yhardy@uj.ac.za>
List: tech-kern
Date: 12/20/2007 11:10:34
This is a multi-part message in MIME format.
--------------030805080500090007060108
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

I have a slight improvement (attached) for anyone who would like
to try it out.

> To get this running again, I had to slightly modify the patch from
> http://mail-index.netbsd.org/tech-kern/2007/10/08/0002.html with the
> attached patch.

-- 
Kind regards,

Yorick Hardy

--------------030805080500090007060108
Content-Type: text/plain;
 name="drm.patch"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="drm.patch"

--- sys/dev/drm/drm_drv.c.orig	2007-11-26 09:18:51.000000000 +0200
+++ sys/dev/drm/drm_drv.c	2007-11-27 12:33:43.000000000 +0200
@@ -166,6 +166,7 @@
 	int unit;
 	drm_device_t *dev;
 	drm_pci_id_list_t *id_entry;
+	struct agp_softc *agpsc = (struct agp_softc *)agp_find_device(0);
 
         if(init_units)
 	{
@@ -224,6 +225,26 @@
 		}
 		if(dev->pci_map_data[unit].maptype == PCI_MAPREG_TYPE_MEM)
 			dev->pci_map_data[unit].flags |= BUS_SPACE_MAP_LINEAR;
+		if(dev->pci_map_data[unit].maptype
+			== (PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT))
+			dev->pci_map_data[unit].flags |= BUS_SPACE_MAP_LINEAR;
+		DRM_DEBUG("agp: %x, drm: %x\n", agpsc->as_id, dev->pa.pa_id);
+        	if (agpsc != NULL)
+		{
+			/* the register might be in use by agp */
+			if (AGP_GET_PCIMAP(agpsc, dev->pa.pa_id,
+					   PCI_MAPREG_START + unit*4,
+					   &dev->pci_map_data[unit].bsh))
+			{
+				DRM_DEBUG("agp provides pci reg %d\n",
+					 PCI_MAPREG_START + unit*4);
+				/* never unmap */
+				dev->pci_map_data[unit].mapped = -1;
+				dev->pci_map_data[unit].vaddr =
+					bus_space_vaddr(dev->pa.pa_memt,
+						dev->pci_map_data[unit].bsh);
+			}
+		}
 	}
 	for(unit=0; unit<DRM_MAX_PCI_RESOURCE; unit++) {
 		dev->agp_map_data[unit].mapped = 0;
@@ -400,7 +421,7 @@
 	}
 
 	for(i = 0; i<DRM_MAX_PCI_RESOURCE; i++) {
-		if (dev->pci_map_data[i].mapped > 1) {
+		if (dev->pci_map_data[i].mapped > 0) {
 			bus_space_unmap(dev->pci_map_data[i].maptype,
 					dev->pci_map_data[i].bsh,
 					dev->pci_map_data[i].size);
@@ -529,7 +550,7 @@
 	i = 0;
 
 	for (i = 0; i < DRM_MAX_PCI_RESOURCE; i++)
-		if (dev->pci_map_data[i].mapped != 0)
+		if (dev->pci_map_data[i].mapped > 0)
 		{
 			bus_space_unmap(dev->pa.pa_memt,
 					dev->pci_map_data[i].bsh,
--- sys/dev/drm/drm_memory.c.orig	2007-11-26 09:18:51.000000000 +0200
+++ sys/dev/drm/drm_memory.c	2007-11-27 12:33:43.000000000 +0200
@@ -87,15 +87,19 @@
 	int i, reg, reason;
 	for(i = 0; i<DRM_MAX_PCI_RESOURCE; i++) {
 		reg = PCI_MAPREG_START + i*4;
-		if (dev->pci_map_data[i].maptype == PCI_MAPREG_TYPE_MEM &&
-		    dev->pci_map_data[i].base == map->offset            &&
-		    dev->pci_map_data[i].size >= map->size)
+		if ((dev->pci_map_data[i].maptype
+			== PCI_MAPREG_TYPE_MEM ||
+		     dev->pci_map_data[i].maptype
+			== (PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT))
+		    && dev->pci_map_data[i].base == map->offset
+	            && dev->pci_map_data[i].size >= map->size)
 		{
 			map->bst = dev->pa.pa_memt;
 			map->cnt = &(dev->pci_map_data[i].mapped);
 			map->mapsize = dev->pci_map_data[i].size;
-			dev->pci_map_data[i].mapped++;
-			if (dev->pci_map_data[i].mapped > 1)
+			if (dev->pci_map_data[i].mapped >= 0)
+				dev->pci_map_data[i].mapped++;
+			if (dev->pci_map_data[i].mapped != 1)
 			{
 				map->bsh = dev->pci_map_data[i].bsh;
 				return dev->pci_map_data[i].vaddr;
@@ -118,6 +122,7 @@
 		}
 	}
 	/* failed to find a valid mapping; all hope isn't lost though */
+/*
 	for(i = 0; i<DRM_MAX_PCI_RESOURCE; i++) {
 		if (dev->agp_map_data[i].mapped > 0 &&
 		    dev->agp_map_data[i].base == map->offset &&
@@ -156,6 +161,7 @@
 			return dev->agp_map_data[i].vaddr;
 		}
 	}
+*/
 
 	/* now we can give up... */
 	DRM_DEBUG("drm_ioremap failed: offset=%lx size=%lu\n",

--------------030805080500090007060108
Content-Type: text/plain;
 name="drmagp.patch"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="drmagp.patch"

--- sys/dev/pci/agpvar.h.orig	2007-08-06 18:39:42.000000000 +0200
+++ sys/dev/pci/agpvar.h	2007-11-27 12:33:43.000000000 +0200
@@ -111,6 +111,7 @@
 	int (*free_memory)(struct agp_softc *, struct agp_memory *);
 	int (*bind_memory)(struct agp_softc *, struct agp_memory *, off_t);
 	int (*unbind_memory)(struct agp_softc *, struct agp_memory *);
+	int (*get_pcimap)(struct agp_softc *, pcireg_t, int, bus_space_handle_t *);
 };
 
 #define AGP_GET_APERTURE(sc)	 ((sc)->as_methods->get_aperture(sc))
@@ -123,6 +124,7 @@
 #define AGP_FREE_MEMORY(sc,m)	 ((sc)->as_methods->free_memory((sc),(m)))
 #define AGP_BIND_MEMORY(sc,m,o)	 ((sc)->as_methods->bind_memory((sc),(m),(o)))
 #define AGP_UNBIND_MEMORY(sc,m)	 ((sc)->as_methods->unbind_memory((sc),(m)))
+#define AGP_GET_PCIMAP(sc,i,r,h) ((sc)->as_methods->get_pcimap((sc),(i),(r),(h)))
 
 /*
  * All chipset drivers must have this at the start of their softc.
@@ -178,6 +180,7 @@
 int agp_generic_free_memory(struct agp_softc *, struct agp_memory *);
 int agp_generic_bind_memory(struct agp_softc *, struct agp_memory *, off_t);
 int agp_generic_unbind_memory(struct agp_softc *, struct agp_memory *);
+int agp_generic_get_pcimap(struct agp_softc *, pcireg_t, int, bus_space_handle_t *);
 
 /* The vendor has already been matched when these functions are called */
 int agp_amd_match(const struct pci_attach_args *);
--- sys/dev/pci/agp_ali.c.orig	2007-10-24 18:29:50.000000000 +0200
+++ sys/dev/pci/agp_ali.c	2007-11-27 12:33:43.000000000 +0200
@@ -74,6 +74,7 @@
 	agp_generic_free_memory,
 	agp_generic_bind_memory,
 	agp_generic_unbind_memory,
+	agp_generic_get_pcimap,
 };
 
 int
--- sys/dev/pci/agp_amd.c.orig	2007-03-04 08:02:15.000000000 +0200
+++ sys/dev/pci/agp_amd.c	2007-11-27 12:33:43.000000000 +0200
@@ -92,6 +92,7 @@
 	agp_generic_free_memory,
 	agp_generic_bind_memory,
 	agp_generic_unbind_memory,
+	agp_generic_get_pcimap,
 };
 
 
--- sys/dev/pci/agp_amd64.c.orig	2007-10-24 18:29:50.000000000 +0200
+++ sys/dev/pci/agp_amd64.c	2007-11-27 12:33:44.000000000 +0200
@@ -98,6 +98,7 @@
 	agp_generic_free_memory,
 	agp_generic_bind_memory,
 	agp_generic_unbind_memory,
+	agp_generic_get_pcimap,
 };
 
 
--- sys/dev/pci/agp_apple.c.orig	2007-10-24 18:29:50.000000000 +0200
+++ sys/dev/pci/agp_apple.c	2007-11-27 12:33:44.000000000 +0200
@@ -67,6 +67,7 @@
 	agp_generic_free_memory,
 	agp_generic_bind_memory,
 	agp_generic_unbind_memory,
+	agp_generic_get_pcimap,
 };
 
 struct agp_apple_softc {
--- sys/dev/pci/agp_intel.c.orig	2007-11-16 17:28:25.000000000 +0200
+++ sys/dev/pci/agp_intel.c	2007-11-27 12:33:44.000000000 +0200
@@ -87,6 +87,7 @@
 	agp_generic_free_memory,
 	agp_generic_bind_memory,
 	agp_generic_unbind_memory,
+	agp_generic_get_pcimap,
 };
 
 static int
--- sys/dev/pci/agp_sis.c.orig	2007-10-24 18:29:50.000000000 +0200
+++ sys/dev/pci/agp_sis.c	2007-11-27 12:33:44.000000000 +0200
@@ -72,6 +72,7 @@
 	agp_generic_free_memory,
 	agp_generic_bind_memory,
 	agp_generic_unbind_memory,
+	agp_generic_get_pcimap,
 };
 
 int
--- sys/dev/pci/agp_via.c.orig	2007-10-24 18:29:50.000000000 +0200
+++ sys/dev/pci/agp_via.c	2007-11-27 12:33:44.000000000 +0200
@@ -68,6 +68,7 @@
 	agp_generic_free_memory,
 	agp_generic_bind_memory,
 	agp_generic_unbind_memory,
+	agp_generic_get_pcimap,
 };
 
 struct agp_via_softc {
--- ./sys/dev/pci/agp.c.orig	2007-12-13 13:14:00.000000000 +0200
+++ ./sys/dev/pci/agp.c	2007-12-18 13:37:51.000000000 +0200
@@ -710,6 +710,13 @@
 	return 0;
 }
 
+int
+agp_generic_get_pcimap(struct agp_softc *sc, pcireg_t id, int reg, bus_space_handle_t *h)
+{
+	printf("%s: get_pcimap not implemented\n", sc->as_dev.dv_xname);
+	return 0;
+}
+
 /* Helper functions for implementing user/kernel api */
 
 static int
--- ./sys/dev/pci/agp_i810.c.orig	2007-12-13 13:14:00.000000000 +0200
+++ ./sys/dev/pci/agp_i810.c	2007-12-18 13:44:00.000000000 +0200
@@ -109,6 +109,7 @@
 static int agp_i810_free_memory(struct agp_softc *, struct agp_memory *);
 static int agp_i810_bind_memory(struct agp_softc *, struct agp_memory *, off_t);
 static int agp_i810_unbind_memory(struct agp_softc *, struct agp_memory *);
+static int agp_i810_get_pcimap(struct agp_softc *, pcireg_t, int, bus_space_handle_t *);
 
 static bool agp_i810_resume(device_t);
 static int agp_i810_init(struct agp_softc *);
@@ -126,6 +127,7 @@
 	agp_i810_free_memory,
 	agp_i810_bind_memory,
 	agp_i810_unbind_memory,
+	agp_i810_get_pcimap,
 };
 
 /* XXXthorpej -- duplicated code (see arch/i386/pci/pchb.c) */
@@ -504,14 +506,6 @@
 	 */
 	agp_flush_cache();
 
-#if 0
-	/*      
-	 * another device (drm) may need access to this region
-	 * we do not need it anymore
-	 */     
-	bus_space_unmap(isc->bst, isc->bsh, mmadrsize);
-#endif
-
 	return 0;
 }
 
@@ -831,7 +825,6 @@
 	 * Until the issue is solved, simply restore it.
 	 */
 
-#if 0
 	regval = bus_space_read_4(isc->bst, isc->bsh, AGP_I810_PGTBL_CTL);
 	if (regval != (isc->gatt->ag_physical | 1)) {
 		printf("agp_i810_bind_memory: PGTBL_CTL is 0x%x - fixing\n",
@@ -839,8 +832,6 @@
 		bus_space_write_4(isc->bst, isc->bsh, AGP_I810_PGTBL_CTL,
 				  isc->gatt->ag_physical | 1);
 	}
-#endif
-	regval = 0;
 
 	if (mem->am_type == 2) {
 		WRITEGTT(offset, mem->am_physical | 1);
@@ -886,6 +877,28 @@
 	return 0;
 }
 
+static int
+agp_i810_get_pcimap(struct agp_softc *sc, pcireg_t id, int reg, bus_space_handle_t *bh)
+{
+	struct agp_i810_softc *isc = sc->as_chipc;
+
+	if(id != isc->vga_pa.pa_id) return 0;
+
+	if((reg == AGP_I810_MMADR && isc->chiptype == CHIP_I810) ||
+	   (reg == AGP_I915_MMADR && isc->chiptype == CHIP_I915) ||
+	   (reg == AGP_I965_MMADR && isc->chiptype == CHIP_I965)) {
+		*bh = isc->bsh;
+		return 1;
+	}
+
+	if(reg == AGP_I915_GTTADR && isc->chiptype == CHIP_I915) {
+		*bh = isc->gtt_bsh; /* should this be allowed? */
+		return 1;
+	}
+
+	return 0;
+}
+
 static bool
 agp_i810_resume(device_t dv)
 {

--------------030805080500090007060108--