[Mesa-dev] [PATCH 47/53] i965: Just use a uint32_t context handle rather than a malloc'd wrapper.

Kenneth Graunke kenneth at whitecape.org
Wed Apr 5 00:10:37 UTC 2017


drm_bacon_context is a malloc'd struct containing a uint32_t context ID
and a pointer back to the bufmgr.  The bufmgr pointer is pretty useless,
as everybody already has brw->bufmgr.  At that point...we may as well
just use the ctx_id handle directly.  A number of places already had to
call drm_bacon_gem_context_get_id() to extract the ID anyway.  Now they
just have it.
---
 src/mesa/drivers/dri/i965/brw_bufmgr.h            |  7 +--
 src/mesa/drivers/dri/i965/brw_context.c           |  4 +-
 src/mesa/drivers/dri/i965/brw_context.h           |  2 +-
 src/mesa/drivers/dri/i965/brw_performance_query.c |  6 +--
 src/mesa/drivers/dri/i965/brw_reset.c             | 11 ++---
 src/mesa/drivers/dri/i965/intel_batchbuffer.c     | 10 ++---
 src/mesa/drivers/dri/i965/intel_bufmgr_gem.c      | 52 ++++-------------------
 7 files changed, 22 insertions(+), 70 deletions(-)

diff --git a/src/mesa/drivers/dri/i965/brw_bufmgr.h b/src/mesa/drivers/dri/i965/brw_bufmgr.h
index 026cc00c792..898989575b0 100644
--- a/src/mesa/drivers/dri/i965/brw_bufmgr.h
+++ b/src/mesa/drivers/dri/i965/brw_bufmgr.h
@@ -46,7 +46,6 @@ extern "C" {
 struct gen_device_info;
 
 typedef struct _drm_bacon_bufmgr drm_bacon_bufmgr;
-typedef struct _drm_bacon_context drm_bacon_context;
 typedef struct _drm_bacon_bo drm_bacon_bo;
 
 struct _drm_bacon_bo {
@@ -304,10 +303,8 @@ void drm_bacon_gem_bo_start_gtt_access(drm_bacon_bo *bo, int write_enable);
 
 int drm_bacon_gem_bo_wait(drm_bacon_bo *bo, int64_t timeout_ns);
 
-drm_bacon_context *drm_bacon_gem_context_create(drm_bacon_bufmgr *bufmgr);
-int drm_bacon_gem_context_get_id(drm_bacon_context *ctx,
-                                 uint32_t *ctx_id);
-void drm_bacon_gem_context_destroy(drm_bacon_context *ctx);
+uint32_t brw_create_hw_context(drm_bacon_bufmgr *bufmgr);
+void brw_destroy_hw_context(drm_bacon_bufmgr *bufmgr, uint32_t ctx_id);
 
 int drm_bacon_bo_gem_export_to_prime(drm_bacon_bo *bo, int *prime_fd);
 drm_bacon_bo *drm_bacon_bo_gem_create_from_prime(drm_bacon_bufmgr *bufmgr,
diff --git a/src/mesa/drivers/dri/i965/brw_context.c b/src/mesa/drivers/dri/i965/brw_context.c
index 077a0cab71b..97fd9bdad33 100644
--- a/src/mesa/drivers/dri/i965/brw_context.c
+++ b/src/mesa/drivers/dri/i965/brw_context.c
@@ -1094,7 +1094,7 @@ brwCreateContext(gl_api api,
        * This is required for transform feedback buffer offsets, query objects,
        * and also allows us to reduce how much state we have to emit.
        */
-      brw->hw_ctx = drm_bacon_gem_context_create(brw->bufmgr);
+      brw->hw_ctx = brw_create_hw_context(brw->bufmgr);
 
       if (!brw->hw_ctx) {
          fprintf(stderr, "Failed to create hardware context.\n");
@@ -1200,7 +1200,7 @@ intelDestroyContext(__DRIcontext * driContextPriv)
    if (brw->wm.base.scratch_bo)
       drm_bacon_bo_unreference(brw->wm.base.scratch_bo);
 
-   drm_bacon_gem_context_destroy(brw->hw_ctx);
+   brw_destroy_hw_context(brw->bufmgr, brw->hw_ctx);
 
    if (ctx->swrast_context) {
       _swsetup_DestroyContext(&brw->ctx);
diff --git a/src/mesa/drivers/dri/i965/brw_context.h b/src/mesa/drivers/dri/i965/brw_context.h
index 186ce826801..e6a0594edc5 100644
--- a/src/mesa/drivers/dri/i965/brw_context.h
+++ b/src/mesa/drivers/dri/i965/brw_context.h
@@ -670,7 +670,7 @@ struct brw_context
 
    drm_bacon_bufmgr *bufmgr;
 
-   drm_bacon_context *hw_ctx;
+   uint32_t hw_ctx;
 
    /** BO for post-sync nonzero writes for gen6 workaround. */
    drm_bacon_bo *workaround_bo;
diff --git a/src/mesa/drivers/dri/i965/brw_performance_query.c b/src/mesa/drivers/dri/i965/brw_performance_query.c
index c9ba4fe9256..a0f4d9b2903 100644
--- a/src/mesa/drivers/dri/i965/brw_performance_query.c
+++ b/src/mesa/drivers/dri/i965/brw_performance_query.c
@@ -948,12 +948,8 @@ brw_begin_perf_query(struct gl_context *ctx,
       /* If the OA counters aren't already on, enable them. */
       if (brw->perfquery.oa_stream_fd == -1) {
          __DRIscreen *screen = brw->screen->driScrnPriv;
-         uint32_t ctx_id;
          int period_exponent;
 
-         if (drm_bacon_gem_context_get_id(brw->hw_ctx, &ctx_id) != 0)
-            return false;
-
          /* The timestamp for HSW+ increments every 80ns
           *
           * The period_exponent gives a sampling period as follows:
@@ -973,7 +969,7 @@ brw_begin_perf_query(struct gl_context *ctx,
                                        query->oa_format,
                                        period_exponent,
                                        screen->fd, /* drm fd */
-                                       ctx_id))
+                                       brw->hw_ctx))
             return false;
       } else {
          assert(brw->perfquery.current_oa_metrics_set_id ==
diff --git a/src/mesa/drivers/dri/i965/brw_reset.c b/src/mesa/drivers/dri/i965/brw_reset.c
index aa68102d2e3..ad8c44f2d1c 100644
--- a/src/mesa/drivers/dri/i965/brw_reset.c
+++ b/src/mesa/drivers/dri/i965/brw_reset.c
@@ -36,16 +36,13 @@ brw_get_graphics_reset_status(struct gl_context *ctx)
 {
    struct brw_context *brw = brw_context(ctx);
    __DRIscreen *dri_screen = brw->screen->driScrnPriv;
-   struct drm_i915_reset_stats stats;
+   struct drm_i915_reset_stats stats = { .ctx_id = brw->hw_ctx };
 
    /* If hardware contexts are not being used (or
     * DRM_IOCTL_I915_GET_RESET_STATS is not supported), this function should
     * not be accessible.
     */
-   assert(brw->hw_ctx != NULL);
-
-   memset(&stats, 0, sizeof(stats));
-   drm_bacon_gem_context_get_id(brw->hw_ctx, &stats.ctx_id);
+   assert(brw->hw_ctx != 0);
 
    /* A reset status other than NO_ERROR was returned last time. I915 returns
     * nonzero active/pending only if reset has been encountered and completed.
@@ -81,9 +78,7 @@ void
 brw_check_for_reset(struct brw_context *brw)
 {
    __DRIscreen *dri_screen = brw->screen->driScrnPriv;
-   struct drm_i915_reset_stats stats;
-   memset(&stats, 0, sizeof(stats));
-   drm_bacon_gem_context_get_id(brw->hw_ctx, &stats.ctx_id);
+   struct drm_i915_reset_stats stats = { .ctx_id = brw->hw_ctx };
 
    if (drmIoctl(dri_screen->fd, DRM_IOCTL_I915_GET_RESET_STATS, &stats) != 0)
       return;
diff --git a/src/mesa/drivers/dri/i965/intel_batchbuffer.c b/src/mesa/drivers/dri/i965/intel_batchbuffer.c
index 00559bb6599..19aebc98d62 100644
--- a/src/mesa/drivers/dri/i965/intel_batchbuffer.c
+++ b/src/mesa/drivers/dri/i965/intel_batchbuffer.c
@@ -384,7 +384,7 @@ brw_new_batch(struct brw_context *brw)
     * would otherwise be stored in the context (which for all intents and
     * purposes means everything).
     */
-   if (brw->hw_ctx == NULL)
+   if (brw->hw_ctx == 0)
       brw->ctx.NewDriverState |= BRW_NEW_CONTEXT;
 
    brw->ctx.NewDriverState |= BRW_NEW_BATCH;
@@ -538,7 +538,7 @@ add_exec_bo(struct intel_batchbuffer *batch, drm_bacon_bo *bo)
 static int
 execbuffer(int fd,
            struct intel_batchbuffer *batch,
-           drm_bacon_context *ctx,
+           uint32_t ctx_id,
            int used,
            int in_fence,
            int *out_fence,
@@ -557,8 +557,6 @@ execbuffer(int fd,
       .rsvd2 = 0,
    };
 
-   uint32_t ctx_id = 0;
-   drm_bacon_gem_context_get_id(ctx, &ctx_id);
    i915_execbuffer2_set_context_id(execbuf, ctx_id);
 
    if (in_fence != -1) {
@@ -625,8 +623,8 @@ do_flush_locked(struct brw_context *brw, int in_fence_fd, int *out_fence_fd)
 	 flags |= I915_EXEC_GEN7_SOL_RESET;
 
       if (ret == 0) {
-         void *hw_ctx = batch->ring != RENDER_RING ? NULL : brw->hw_ctx;
-	 if (hw_ctx == NULL) {
+         uint32_t hw_ctx = batch->ring == RENDER_RING ? brw->hw_ctx : 0;
+         if (hw_ctx == 0) {
             assert(in_fence_fd == -1);
             assert(out_fence_fd == NULL);
          }
diff --git a/src/mesa/drivers/dri/i965/intel_bufmgr_gem.c b/src/mesa/drivers/dri/i965/intel_bufmgr_gem.c
index 317265b65cf..dd9af19bbc7 100644
--- a/src/mesa/drivers/dri/i965/intel_bufmgr_gem.c
+++ b/src/mesa/drivers/dri/i965/intel_bufmgr_gem.c
@@ -90,11 +90,6 @@ atomic_add_unless(int *v, int add, int unless)
    return c == unless;
 }
 
-struct _drm_bacon_context {
-	unsigned int ctx_id;
-	struct _drm_bacon_bufmgr *bufmgr;
-};
-
 struct bo_cache_bucket {
 	struct list_head head;
 	unsigned long size;
@@ -1419,62 +1414,33 @@ drm_bacon_bufmgr_gem_set_vma_cache_size(drm_bacon_bufmgr *bufmgr, int limit)
 	bo_purge_vma_cache(bufmgr);
 }
 
-drm_bacon_context *
-drm_bacon_gem_context_create(drm_bacon_bufmgr *bufmgr)
+uint32_t
+brw_create_hw_context(drm_bacon_bufmgr *bufmgr)
 {
 	struct drm_i915_gem_context_create create;
-	drm_bacon_context *context = NULL;
 	int ret;
 
-	context = calloc(1, sizeof(*context));
-	if (!context)
-		return NULL;
-
 	memclear(create);
 	ret = drmIoctl(bufmgr->fd, DRM_IOCTL_I915_GEM_CONTEXT_CREATE, &create);
 	if (ret != 0) {
 		DBG("DRM_IOCTL_I915_GEM_CONTEXT_CREATE failed: %s\n",
 		    strerror(errno));
-		free(context);
-		return NULL;
+		return 0;
 	}
 
-	context->ctx_id = create.ctx_id;
-	context->bufmgr = bufmgr;
-
-	return context;
-}
-
-int
-drm_bacon_gem_context_get_id(drm_bacon_context *ctx, uint32_t *ctx_id)
-{
-	if (ctx == NULL)
-		return -EINVAL;
-
-	*ctx_id = ctx->ctx_id;
-
-	return 0;
+	return create.ctx_id;
 }
 
 void
-drm_bacon_gem_context_destroy(drm_bacon_context *ctx)
+brw_destroy_hw_context(drm_bacon_bufmgr *bufmgr, uint32_t ctx_id)
 {
-	struct drm_i915_gem_context_destroy destroy;
-	int ret;
-
-	if (ctx == NULL)
-		return;
-
-	memclear(destroy);
+	struct drm_i915_gem_context_destroy d = { .ctx_id = ctx_id };
 
-	destroy.ctx_id = ctx->ctx_id;
-	ret = drmIoctl(ctx->bufmgr->fd, DRM_IOCTL_I915_GEM_CONTEXT_DESTROY,
-		       &destroy);
-	if (ret != 0)
+	if (ctx_id != 0 &&
+	    drmIoctl(bufmgr->fd, DRM_IOCTL_I915_GEM_CONTEXT_DESTROY, &d) != 0) {
 		fprintf(stderr, "DRM_IOCTL_I915_GEM_CONTEXT_DESTROY failed: %s\n",
 			strerror(errno));
-
-	free(ctx);
+	}
 }
 
 int
-- 
2.12.1



More information about the mesa-dev mailing list