Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 13 Dec 2009 15:03:54 +0000 (UTC)
From:      Robert Noland <rnoland@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-8@freebsd.org
Subject:   svn commit: r200475 - stable/8/sys/dev/drm
Message-ID:  <200912131503.nBDF3sFC089629@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: rnoland
Date: Sun Dec 13 15:03:54 2009
New Revision: 200475
URL: http://svn.freebsd.org/changeset/base/200475

Log:
  MFC r198694,r198697
  
      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

Modified:
  stable/8/sys/dev/drm/drm_scatter.c
Directory Properties:
  stable/8/sys/   (props changed)
  stable/8/sys/amd64/include/xen/   (props changed)
  stable/8/sys/cddl/contrib/opensolaris/   (props changed)
  stable/8/sys/contrib/dev/acpica/   (props changed)
  stable/8/sys/contrib/pf/   (props changed)
  stable/8/sys/dev/xen/xenpci/   (props changed)

Modified: stable/8/sys/dev/drm/drm_scatter.c
==============================================================================
--- stable/8/sys/dev/drm/drm_scatter.c	Sun Dec 13 14:55:50 2009	(r200474)
+++ stable/8/sys/dev/drm/drm_scatter.c	Sun Dec 13 15:03:54 2009	(r200475)
@@ -47,79 +47,50 @@ 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 */
 	    NULL, NULL, /* filtfunc, filtfuncargs */
-	    request->size, pages, /* maxsize, nsegs */
+	    request->size, entry->pages, /* maxsize, nsegs */
 	    PAGE_SIZE, 0, /* maxsegsize, flags */
 	    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?200912131503.nBDF3sFC089629>