Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 22 Apr 2010 18:44:23 +0000 (UTC)
From:      Robert Noland <rnoland@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r207067 - head/sys/dev/drm
Message-ID:  <201004221844.o3MIiNgV059699@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: rnoland
Date: Thu Apr 22 18:44:23 2010
New Revision: 207067
URL: http://svn.freebsd.org/changeset/base/207067

Log:
  re-write scatter gather memory allocation yet again...
  
  This time, abandon the use of busdma and start interacting with the VM
  system directly.  Make use of the new kmem_alloc_attr() which allows us
  to easily allocate non-contiguous pages to back the GART table.  This
  should help a lot when starting or restarting X after the system has
  been running for a while and memory has become fragmented.
  
  MFC after:	2 weeks

Modified:
  head/sys/dev/drm/drmP.h
  head/sys/dev/drm/drm_bufs.c
  head/sys/dev/drm/drm_scatter.c
  head/sys/dev/drm/r128_cce.c
  head/sys/dev/drm/r600_cp.c
  head/sys/dev/drm/radeon_cp.c

Modified: head/sys/dev/drm/drmP.h
==============================================================================
--- head/sys/dev/drm/drmP.h	Thu Apr 22 18:21:25 2010	(r207066)
+++ head/sys/dev/drm/drmP.h	Thu Apr 22 18:44:23 2010	(r207067)
@@ -49,6 +49,7 @@ struct drm_file;
 #include <sys/module.h>
 #include <sys/systm.h>
 #include <sys/conf.h>
+#include <sys/sglist.h>
 #include <sys/stat.h>
 #if __FreeBSD_version >= 700000
 #include <sys/priv.h>
@@ -68,10 +69,13 @@ struct drm_file;
 #include <vm/vm.h>
 #include <vm/pmap.h>
 #include <vm/vm_extern.h>
+#include <vm/vm_kern.h>
 #include <vm/vm_map.h>
 #include <vm/vm_object.h>
 #include <vm/vm_page.h>
+#include <vm/vm_pager.h>
 #include <vm/vm_param.h>
+#include <vm/vm_phys.h>
 #include <machine/param.h>
 #include <machine/pmap.h>
 #include <machine/bus.h>
@@ -474,11 +478,10 @@ typedef struct drm_agp_head {
 } drm_agp_head_t;
 
 typedef struct drm_sg_mem {
-	unsigned long		  handle;
-	void			 *virtual;
-	int			  pages;
-	dma_addr_t		 *busaddr;
-	struct drm_dma_handle	 *dmah;		/* Handle to PCI memory  */
+	vm_offset_t vaddr;
+	vm_paddr_t *busaddr;
+	vm_pindex_t pages;
+	vm_object_t obj;
 } drm_sg_mem_t;
 
 #define DRM_MAP_HANDLE_BITS	(sizeof(void *) == 4 ? 4 : 24)

Modified: head/sys/dev/drm/drm_bufs.c
==============================================================================
--- head/sys/dev/drm/drm_bufs.c	Thu Apr 22 18:21:25 2010	(r207066)
+++ head/sys/dev/drm/drm_bufs.c	Thu Apr 22 18:44:23 2010	(r207067)
@@ -226,8 +226,8 @@ int drm_addmap(struct drm_device * dev, 
 			DRM_LOCK();
 			return EINVAL;
 		}
-		map->virtual = (void *)(dev->sg->handle + offset);
-		map->offset = dev->sg->handle + offset;
+		map->virtual = (void *)(dev->sg->vaddr + offset);
+		map->offset = dev->sg->vaddr + offset;
 		break;
 	case _DRM_CONSISTENT:
 		/* Unfortunately, we don't get any alignment specification from
@@ -744,7 +744,7 @@ static int drm_do_addbufs_sg(struct drm_
 
 		buf->offset  = (dma->byte_count + offset);
 		buf->bus_address = agp_offset + offset;
-		buf->address = (void *)(agp_offset + offset + dev->sg->handle);
+		buf->address = (void *)(agp_offset + offset + dev->sg->vaddr);
 		buf->next    = NULL;
 		buf->pending = 0;
 		buf->file_priv = NULL;

Modified: head/sys/dev/drm/drm_scatter.c
==============================================================================
--- head/sys/dev/drm/drm_scatter.c	Thu Apr 22 18:21:25 2010	(r207066)
+++ head/sys/dev/drm/drm_scatter.c	Thu Apr 22 18:44:23 2010	(r207067)
@@ -1,5 +1,5 @@
 /*-
- * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
+ * Copyright (c) 2009 Robert C. Noland III <rnoland@FreeBSD.org>
  * All Rights Reserved.
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
@@ -20,11 +20,6 @@
  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  * DEALINGS IN THE SOFTWARE.
- *
- * Authors:
- *   Gareth Hughes <gareth@valinux.com>
- *   Eric Anholt <anholt@FreeBSD.org>
- *
  */
 
 #include <sys/cdefs.h>
@@ -32,99 +27,58 @@ __FBSDID("$FreeBSD$");
 
 /** @file drm_scatter.c
  * Allocation of memory for scatter-gather mappings by the graphics chip.
- *
  * The memory allocated here is then made into an aperture in the card
- * by drm_ati_pcigart_init().
+ * by mapping the pages into the GART.
  */
 
 #include "dev/drm/drmP.h"
 
-static void drm_sg_alloc_cb(void *arg, bus_dma_segment_t *segs,
-			    int nsegs, int error);
-
 int
 drm_sg_alloc(struct drm_device *dev, struct drm_scatter_gather *request)
 {
 	struct drm_sg_mem *entry;
-	struct drm_dma_handle *dmah;
-	int ret;
+	vm_size_t size;
+	vm_pindex_t pindex;
 
 	if (dev->sg)
 		return EINVAL;
 
-	entry = malloc(sizeof(*entry), DRM_MEM_SGLISTS, M_WAITOK | M_ZERO);
-	entry->pages = round_page(request->size) / PAGE_SIZE;
-	DRM_DEBUG("sg size=%ld pages=%d\n", request->size, entry->pages);
+	DRM_DEBUG("request size=%ld\n", request->size);
+
+	entry = malloc(sizeof(*entry), DRM_MEM_DRIVER, M_WAITOK | M_ZERO);
 
+	size = round_page(request->size);
+	entry->pages = OFF_TO_IDX(size);
 	entry->busaddr = malloc(entry->pages * sizeof(*entry->busaddr),
-	    DRM_MEM_PAGES, M_WAITOK | M_ZERO);
-	dmah = malloc(sizeof(struct drm_dma_handle), DRM_MEM_DMA,
-	    M_WAITOK | M_ZERO);
-	entry->dmah = dmah;
-
-	ret = bus_dma_tag_create(NULL, PAGE_SIZE, 0, /* tag, align, boundary */
-	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, /* lowaddr, highaddr */
-	    NULL, NULL, /* filtfunc, filtfuncargs */
-	    request->size, entry->pages, /* maxsize, nsegs */
-	    PAGE_SIZE, 0, /* maxsegsize, flags */
-	    NULL, NULL, /* lockfunc, lockfuncargs */
-	    &dmah->tag);
-	if (ret != 0) {
-		drm_sg_cleanup(entry);
-		return ENOMEM;
-	}
+	    DRM_MEM_SGLISTS, M_WAITOK | M_ZERO);
 
-	ret = bus_dmamem_alloc(dmah->tag, &dmah->vaddr,
-	    BUS_DMA_WAITOK | BUS_DMA_ZERO | BUS_DMA_NOCACHE, &dmah->map);
-	if (ret != 0) {
+	entry->vaddr = kmem_alloc_attr(kernel_map, size, M_WAITOK | M_ZERO,
+	    0, BUS_SPACE_MAXADDR_32BIT, VM_MEMATTR_WRITE_COMBINING);
+	if (entry->vaddr == 0) {
 		drm_sg_cleanup(entry);
-		return ENOMEM;
+		return (ENOMEM);
 	}
 
-	entry->handle = (unsigned long)dmah->vaddr;
-	entry->virtual = dmah->vaddr;
-
-	ret = bus_dmamap_load(dmah->tag, dmah->map, dmah->vaddr,
-	    request->size, drm_sg_alloc_cb, entry, BUS_DMA_NOWAIT);
-	if (ret != 0) {
-		drm_sg_cleanup(entry);
-		return ENOMEM;
+	for(pindex = 0; pindex < entry->pages; pindex++) {
+		entry->busaddr[pindex] =
+		    vtophys(entry->vaddr + IDX_TO_OFF(pindex));
 	}
 
 	DRM_LOCK();
 	if (dev->sg) {
 		DRM_UNLOCK();
 		drm_sg_cleanup(entry);
-		return EINVAL;
+		return (EINVAL);
 	}
 	dev->sg = entry;
 	DRM_UNLOCK();
 
-	pmap_change_attr((vm_offset_t)dmah->vaddr, request->size,
-	    PAT_WRITE_COMBINING);
+	request->handle = entry->vaddr;
 
-	request->handle = entry->handle;
+	DRM_DEBUG("allocated %ju pages @ 0x%08zx, contents=%08lx\n",
+	    entry->pages, entry->vaddr, *(unsigned long *)entry->vaddr);
 
-	DRM_DEBUG("handle=%08lx, kva=%p, contents=%08lx\n", entry->handle,
-	    entry->virtual, *(unsigned long *)entry->virtual);
-
-	return 0;
-}
-
-static void
-drm_sg_alloc_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
-{
-	struct drm_sg_mem *entry = arg;
-	int i;
-
-	if (error != 0)
-	    return;
-
-	for(i = 0 ; i < nsegs ; i++) {
-		entry->busaddr[i] = segs[i].ds_addr;
-		DRM_DEBUG("segment %d @ 0x%016lx\n", i,
-		    (unsigned long)segs[i].ds_addr);
-	}
+	return (0);
 }
 
 int
@@ -135,23 +89,22 @@ drm_sg_alloc_ioctl(struct drm_device *de
 
 	DRM_DEBUG("\n");
 
-	return drm_sg_alloc(dev, request);
+	return (drm_sg_alloc(dev, request));
 }
 
 void
 drm_sg_cleanup(struct drm_sg_mem *entry)
 {
-	struct drm_dma_handle *dmah = entry->dmah;
+	if (entry == NULL)
+		return;
+
+	if (entry->vaddr != 0)
+		kmem_free(kernel_map, entry->vaddr, IDX_TO_OFF(entry->pages));
 
-	if (dmah->map != NULL)
-		bus_dmamap_unload(dmah->tag, dmah->map);
-	if (dmah->vaddr != NULL)
-		bus_dmamem_free(dmah->tag, dmah->vaddr, dmah->map);
-	if (dmah->tag != NULL)
-		bus_dma_tag_destroy(dmah->tag);
-	free(dmah, DRM_MEM_DMA);
-	free(entry->busaddr, DRM_MEM_PAGES);
-	free(entry, DRM_MEM_SGLISTS);
+	free(entry->busaddr, DRM_MEM_SGLISTS);
+	free(entry, DRM_MEM_DRIVER);
+
+	return;
 }
 
 int
@@ -165,12 +118,12 @@ drm_sg_free(struct drm_device *dev, void
 	dev->sg = NULL;
 	DRM_UNLOCK();
 
-	if (!entry || entry->handle != request->handle)
-		return EINVAL;
+	if (!entry || entry->vaddr != request->handle)
+		return (EINVAL);
 
-	DRM_DEBUG("sg free virtual = 0x%lx\n", entry->handle);
+	DRM_DEBUG("free 0x%zx\n", entry->vaddr);
 
 	drm_sg_cleanup(entry);
 
-	return 0;
+	return (0);
 }

Modified: head/sys/dev/drm/r128_cce.c
==============================================================================
--- head/sys/dev/drm/r128_cce.c	Thu Apr 22 18:21:25 2010	(r207066)
+++ head/sys/dev/drm/r128_cce.c	Thu Apr 22 18:44:23 2010	(r207067)
@@ -327,8 +327,7 @@ static void r128_cce_init_ring_buffer(st
 		ring_start = dev_priv->cce_ring->offset - dev->agp->base;
 	else
 #endif
-		ring_start = dev_priv->cce_ring->offset -
-				(unsigned long)dev->sg->virtual;
+		ring_start = dev_priv->cce_ring->offset - dev->sg->vaddr;
 
 	R128_WRITE(R128_PM4_BUFFER_OFFSET, ring_start | R128_AGP_OFFSET);
 
@@ -541,7 +540,7 @@ static int r128_do_init_cce(struct drm_d
 		dev_priv->cce_buffers_offset = dev->agp->base;
 	else
 #endif
-		dev_priv->cce_buffers_offset = (unsigned long)dev->sg->virtual;
+		dev_priv->cce_buffers_offset = dev->sg->vaddr;
 
 	dev_priv->ring.start = (u32 *) dev_priv->cce_ring->virtual;
 	dev_priv->ring.end = ((u32 *) dev_priv->cce_ring->virtual

Modified: head/sys/dev/drm/r600_cp.c
==============================================================================
--- head/sys/dev/drm/r600_cp.c	Thu Apr 22 18:21:25 2010	(r207066)
+++ head/sys/dev/drm/r600_cp.c	Thu Apr 22 18:44:23 2010	(r207067)
@@ -1670,9 +1670,8 @@ static void r600_cp_init_ring_buffer(str
 	} else
 #endif
 	{
-		rptr_addr = dev_priv->ring_rptr->offset
-			- ((unsigned long) dev->sg->virtual)
-			+ dev_priv->gart_vm_start;
+		rptr_addr = dev_priv->ring_rptr->offset - dev->sg->vaddr +
+		    dev_priv->gart_vm_start;
 	}
 	RADEON_WRITE(R600_CP_RB_RPTR_ADDR,
 		     rptr_addr & 0xffffffff);
@@ -1706,9 +1705,8 @@ static void r600_cp_init_ring_buffer(str
 			      + dev_priv->gart_vm_start);
 	} else
 #endif
-		ring_start = (dev_priv->cp_ring->offset
-			      - (unsigned long)dev->sg->virtual
-			      + dev_priv->gart_vm_start);
+		ring_start = dev_priv->cp_ring->offset - dev->sg->vaddr +
+		    dev_priv->gart_vm_start;
 
 	RADEON_WRITE(R600_CP_RB_BASE, ring_start >> 8);
 
@@ -2012,9 +2010,8 @@ int r600_do_init_cp(struct drm_device *d
 						 + dev_priv->gart_vm_start);
 	else
 #endif
-		dev_priv->gart_buffers_offset = (dev->agp_buffer_map->offset
-						 - (unsigned long)dev->sg->virtual
-						 + dev_priv->gart_vm_start);
+		dev_priv->gart_buffers_offset = dev->agp_buffer_map->offset -
+		    dev->sg->vaddr + dev_priv->gart_vm_start;
 
 	DRM_DEBUG("fb 0x%08x size %d\n",
 		  (unsigned int) dev_priv->fb_location,

Modified: head/sys/dev/drm/radeon_cp.c
==============================================================================
--- head/sys/dev/drm/radeon_cp.c	Thu Apr 22 18:21:25 2010	(r207066)
+++ head/sys/dev/drm/radeon_cp.c	Thu Apr 22 18:44:23 2010	(r207067)
@@ -720,9 +720,8 @@ static void radeon_cp_init_ring_buffer(s
 			      + dev_priv->gart_vm_start);
 	} else
 #endif
-		ring_start = (dev_priv->cp_ring->offset
-			      - (unsigned long)dev->sg->virtual
-			      + dev_priv->gart_vm_start);
+		ring_start = (dev_priv->cp_ring->offset - dev->sg->vaddr +
+		    dev_priv->gart_vm_start);
 
 	RADEON_WRITE(RADEON_CP_RB_BASE, ring_start);
 
@@ -744,9 +743,8 @@ static void radeon_cp_init_ring_buffer(s
 #endif
 	{
 		RADEON_WRITE(RADEON_CP_RB_RPTR_ADDR,
-			     dev_priv->ring_rptr->offset
-			     - ((unsigned long) dev->sg->virtual)
-			     + dev_priv->gart_vm_start);
+		    dev_priv->ring_rptr->offset - dev->sg->vaddr +
+		    dev_priv->gart_vm_start);
 	}
 
 	/* Set ring buffer size */
@@ -1377,9 +1375,8 @@ static int radeon_do_init_cp(struct drm_
 						 + dev_priv->gart_vm_start);
 	else
 #endif
-		dev_priv->gart_buffers_offset = (dev->agp_buffer_map->offset
-					- (unsigned long)dev->sg->virtual
-					+ dev_priv->gart_vm_start);
+		dev_priv->gart_buffers_offset = dev->agp_buffer_map->offset -
+		    dev->sg->vaddr + dev_priv->gart_vm_start;
 
 	DRM_DEBUG("dev_priv->gart_size %d\n", dev_priv->gart_size);
 	DRM_DEBUG("dev_priv->gart_vm_start 0x%x\n", dev_priv->gart_vm_start);



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201004221844.o3MIiNgV059699>