[Intel-gfx] [PATCH 3/5] drm/i915: refactor ring object allocation

Ben Widawsky ben at bwidawsk.net
Fri Jul 13 08:16:14 CEST 2012


There is duplication here. Even more will be coming.

Signed-off-by: Ben Widawsky <ben at bwidawsk.net>
---
 drivers/gpu/drm/i915/intel_ringbuffer.c | 94 +++++++++++++++++++--------------
 1 file changed, 53 insertions(+), 41 deletions(-)

diff --git a/drivers/gpu/drm/i915/intel_ringbuffer.c b/drivers/gpu/drm/i915/intel_ringbuffer.c
index bf0195a..33d87ad 100644
--- a/drivers/gpu/drm/i915/intel_ringbuffer.c
+++ b/drivers/gpu/drm/i915/intel_ringbuffer.c
@@ -351,6 +351,43 @@ out:
 	return ret;
 }
 
+static void ring_destroy_object(struct drm_i915_gem_object *obj)
+{
+	kunmap(obj->pages[0]);
+	i915_gem_object_unpin(obj);
+	drm_gem_object_unreference(&obj->base);
+}
+
+static struct drm_i915_gem_object *ring_alloc_object(struct drm_device *dev)
+{
+	struct drm_i915_gem_object *obj;
+	int ret;
+
+	obj = i915_gem_alloc_object(dev, 4096);
+	if (obj == NULL)
+		return ERR_PTR(-ENOMEM);
+
+	i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
+
+	ret = i915_gem_object_pin(obj, 4096, true);
+	if (ret != 0) {
+		goto err_unref;
+	}
+
+	obj->cpu_map = kmap(obj->pages[0]);
+	if (obj->cpu_map == NULL) {
+		ret = -ENOMEM;
+		goto err_unpin;
+	}
+	return obj;
+
+err_unpin:
+	i915_gem_object_unpin(obj);
+err_unref:
+	drm_gem_object_unreference(&obj->base);
+	return ERR_PTR(ret);
+}
+
 static int
 init_pipe_control(struct intel_ring_buffer *ring)
 {
@@ -365,32 +402,25 @@ init_pipe_control(struct intel_ring_buffer *ring)
 	if (!pc)
 		return -ENOMEM;
 
-	obj = i915_gem_alloc_object(ring->dev, 4096);
-	if (obj == NULL) {
-		DRM_ERROR("Failed to allocate seqno page\n");
-		ret = -ENOMEM;
+	obj = ring_alloc_object(ring->dev);
+	if (IS_ERR(obj)) {
+		DRM_ERROR("Failed to allocate pipe control page\n");
+		ret = PTR_ERR(obj);
 		goto err;
 	}
 
-	i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
-
-	ret = i915_gem_object_pin(obj, 4096, true);
-	if (ret)
-		goto err_unref;
 
 	pc->gtt_offset = obj->gtt_offset;
-	pc->cpu_page =  kmap(obj->pages[0]);
-	if (pc->cpu_page == NULL)
-		goto err_unpin;
+	pc->cpu_page =  obj->cpu_map;
+	if (pc->cpu_page == NULL) {
+		ret = -ENOMEM;
+		goto err;
+	}
 
 	pc->obj = obj;
 	ring->private = pc;
 	return 0;
 
-err_unpin:
-	i915_gem_object_unpin(obj);
-err_unref:
-	drm_gem_object_unreference(&obj->base);
 err:
 	kfree(pc);
 	return ret;
@@ -406,9 +436,7 @@ cleanup_pipe_control(struct intel_ring_buffer *ring)
 		return;
 
 	obj = pc->obj;
-	kunmap(obj->pages[0]);
-	i915_gem_object_unpin(obj);
-	drm_gem_object_unreference(&obj->base);
+	ring_destroy_object(obj);
 
 	kfree(pc);
 	ring->private = NULL;
@@ -943,9 +971,7 @@ static void cleanup_status_page(struct intel_ring_buffer *ring)
 	if (obj == NULL)
 		return;
 
-	kunmap(obj->pages[0]);
-	i915_gem_object_unpin(obj);
-	drm_gem_object_unreference(&obj->base);
+	ring_destroy_object(obj);
 	ring->status_page.obj = NULL;
 }
 
@@ -955,26 +981,16 @@ static int init_status_page(struct intel_ring_buffer *ring)
 	struct drm_i915_gem_object *obj;
 	int ret;
 
-	obj = i915_gem_alloc_object(dev, 4096);
-	if (obj == NULL) {
+	obj = ring_alloc_object(dev);
+	if (IS_ERR(obj)) {
 		DRM_ERROR("Failed to allocate status page\n");
-		ret = -ENOMEM;
+		ret = PTR_ERR(obj);
 		goto err;
 	}
 
-	i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
-
-	ret = i915_gem_object_pin(obj, 4096, true);
-	if (ret != 0) {
-		goto err_unref;
-	}
-
 	ring->status_page.gfx_addr = obj->gtt_offset;
-	ring->status_page.page_addr = kmap(obj->pages[0]);
-	if (ring->status_page.page_addr == NULL) {
-		ret = -ENOMEM;
-		goto err_unpin;
-	}
+	ring->status_page.page_addr = obj->cpu_map;
+
 	ring->status_page.obj = obj;
 	memset(ring->status_page.page_addr, 0, PAGE_SIZE);
 
@@ -984,10 +1000,6 @@ static int init_status_page(struct intel_ring_buffer *ring)
 
 	return 0;
 
-err_unpin:
-	i915_gem_object_unpin(obj);
-err_unref:
-	drm_gem_object_unreference(&obj->base);
 err:
 	return ret;
 }
-- 
1.7.11.1




More information about the Intel-gfx mailing list