Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 30 Oct 2009 18:02:11 +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: r198694 - head/sys/dev/drm
Message-ID:  <200910301802.n9UI2B0B053659@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: rnoland
Date: Fri Oct 30 18:02:10 2009
New Revision: 198694
URL: http://svn.freebsd.org/changeset/base/198694

Log:
  Some general cleanup of scatter/gather memory allocation
  
   - We don't need to check malloc return values with M_WAITOK
   - remove variables that we don't really need
   - cleanup the error paths by just calling drm_sg_cleanup()
   - fix drm_sg_cleanup() to be safe to call at any time
  
  MFC after:	2 weeks

Modified:
  head/sys/dev/drm/drm_scatter.c

Modified: head/sys/dev/drm/drm_scatter.c
==============================================================================
--- head/sys/dev/drm/drm_scatter.c	Fri Oct 30 17:28:35 2009	(r198693)
+++ head/sys/dev/drm/drm_scatter.c	Fri Oct 30 18:02:10 2009	(r198694)
@@ -47,35 +47,20 @@ drm_sg_alloc(struct drm_device *dev, str
 {
 	struct drm_sg_mem *entry;
 	struct drm_dma_handle *dmah;
-	unsigned long pages;
 	int ret;
 
 	if (dev->sg)
 		return EINVAL;
 
 	entry = malloc(sizeof(*entry), DRM_MEM_SGLISTS, M_WAITOK | M_ZERO);
-	if (!entry)
-		return ENOMEM;
-
-	pages = round_page(request->size) / PAGE_SIZE;
-	DRM_DEBUG("sg size=%ld pages=%ld\n", request->size, pages);
-
-	entry->pages = pages;
-
-	entry->busaddr = malloc(pages * sizeof(*entry->busaddr), DRM_MEM_PAGES,
-	    M_WAITOK | M_ZERO);
-	if (!entry->busaddr) {
-		free(entry, DRM_MEM_SGLISTS);
-		return ENOMEM;
-	}
+	entry->pages = round_page(request->size) / PAGE_SIZE;
+	DRM_DEBUG("sg size=%ld pages=%d\n", request->size, entry->pages);
 
+	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_ZERO | M_NOWAIT);
-	if (dmah == NULL) {
-		free(entry->busaddr, DRM_MEM_PAGES);
-		free(entry, DRM_MEM_SGLISTS);
-		return ENOMEM;
-	}
+	    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 */
@@ -85,41 +70,27 @@ drm_sg_alloc(struct drm_device *dev, str
 	    NULL, NULL, /* lockfunc, lockfuncargs */
 	    &dmah->tag);
 	if (ret != 0) {
-		free(dmah, DRM_MEM_DMA);
-		free(entry->busaddr, DRM_MEM_PAGES);
-		free(entry, DRM_MEM_SGLISTS);
+		drm_sg_cleanup(entry);
 		return ENOMEM;
 	}
 
 	ret = bus_dmamem_alloc(dmah->tag, &dmah->vaddr,
 	    BUS_DMA_WAITOK | BUS_DMA_ZERO | BUS_DMA_NOCACHE, &dmah->map);
 	if (ret != 0) {
-		bus_dma_tag_destroy(dmah->tag);
-		free(dmah, DRM_MEM_DMA);
-		free(entry->busaddr, DRM_MEM_PAGES);
-		free(entry, DRM_MEM_SGLISTS);
+		drm_sg_cleanup(entry);
 		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) {
-		bus_dmamem_free(dmah->tag, dmah->vaddr, dmah->map);
-		bus_dma_tag_destroy(dmah->tag);
-		free(dmah, DRM_MEM_DMA);
-		free(entry->busaddr, DRM_MEM_PAGES);
-		free(entry, DRM_MEM_SGLISTS);
+		drm_sg_cleanup(entry);
 		return ENOMEM;
 	}
 
-	entry->dmah = dmah;
-	entry->handle = (unsigned long)dmah->vaddr;
-	
-	DRM_DEBUG("sg alloc handle  = %08lx\n", entry->handle);
-
-	entry->virtual = (void *)entry->handle;
-	request->handle = entry->handle;
-
 	DRM_LOCK();
 	if (dev->sg) {
 		DRM_UNLOCK();
@@ -129,6 +100,11 @@ drm_sg_alloc(struct drm_device *dev, str
 	dev->sg = entry;
 	DRM_UNLOCK();
 
+	DRM_DEBUG("handle=%08lx, kva=%p, contents=%08lx\n", entry->handle,
+	    entry->virtual, *(unsigned long *)entry->virtual);
+
+	request->handle = entry->handle;
+
 	return 0;
 }
 
@@ -143,6 +119,8 @@ drm_sg_alloc_cb(void *arg, bus_dma_segme
 
 	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);
 	}
 }
 
@@ -162,9 +140,12 @@ drm_sg_cleanup(struct drm_sg_mem *entry)
 {
 	struct drm_dma_handle *dmah = entry->dmah;
 
-	bus_dmamap_unload(dmah->tag, dmah->map);
-	bus_dmamem_free(dmah->tag, dmah->vaddr, dmah->map);
-	bus_dma_tag_destroy(dmah->tag);
+	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);



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