Module: Mesa
Branch: main
Commit: f928ead62593238d464ec263c9a287c50ea54780
URL:    
http://cgit.freedesktop.org/mesa/mesa/commit/?id=f928ead62593238d464ec263c9a287c50ea54780

Author: José Roberto de Souza <[email protected]>
Date:   Wed Oct  5 08:20:47 2022 -0700

intel: Add and use intel_gem_create_context()

Add intel_gem_create_context() to common/intel_gem.c/h and use it
on Iris, Crocus, ANV and HASVK.

Signed-off-by: José Roberto de Souza <[email protected]>
Reviewed-by: Lionel Landwerlin <[email protected]>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/18974>

---

 src/gallium/drivers/crocus/crocus_bufmgr.c | 11 +++++------
 src/gallium/drivers/iris/iris_bufmgr.c     |  8 ++------
 src/intel/common/intel_gem.c               | 10 ++++++++++
 src/intel/common/intel_gem.h               |  1 +
 src/intel/common/meson.build               |  2 +-
 src/intel/common/tests/mi_builder_test.cpp |  8 +++-----
 src/intel/vulkan/anv_device.c              |  7 ++-----
 src/intel/vulkan/anv_gem.c                 | 12 ------------
 src/intel/vulkan/anv_gem_stubs.c           |  6 ------
 src/intel/vulkan/anv_private.h             |  3 +--
 src/intel/vulkan_hasvk/anv_device.c        |  7 ++-----
 src/intel/vulkan_hasvk/anv_gem.c           | 12 ------------
 src/intel/vulkan_hasvk/anv_gem_stubs.c     |  6 ------
 src/intel/vulkan_hasvk/anv_private.h       |  3 +--
 14 files changed, 28 insertions(+), 68 deletions(-)

diff --git a/src/gallium/drivers/crocus/crocus_bufmgr.c 
b/src/gallium/drivers/crocus/crocus_bufmgr.c
index f3268de832c..4e8b5a9baba 100644
--- a/src/gallium/drivers/crocus/crocus_bufmgr.c
+++ b/src/gallium/drivers/crocus/crocus_bufmgr.c
@@ -1517,10 +1517,9 @@ init_cache_buckets(struct crocus_bufmgr *bufmgr)
 uint32_t
 crocus_create_hw_context(struct crocus_bufmgr *bufmgr)
 {
-   struct drm_i915_gem_context_create create = { };
-   int ret = intel_ioctl(bufmgr->fd, DRM_IOCTL_I915_GEM_CONTEXT_CREATE, 
&create);
-   if (ret != 0) {
-      DBG("DRM_IOCTL_I915_GEM_CONTEXT_CREATE failed: %s\n", strerror(errno));
+   uint32_t ctx_id;
+   if (!intel_gem_create_context(bufmgr->fd, &ctx_id)) {
+      DBG("intel_gem_create_context failed: %s\n", strerror(errno));
       return 0;
    }
 
@@ -1540,13 +1539,13 @@ crocus_create_hw_context(struct crocus_bufmgr *bufmgr)
     * we'll have two lost batches instead of a continual stream of hangs.
     */
    struct drm_i915_gem_context_param p = {
-      .ctx_id = create.ctx_id,
+      .ctx_id = ctx_id,
       .param = I915_CONTEXT_PARAM_RECOVERABLE,
       .value = false,
    };
    drmIoctl(bufmgr->fd, DRM_IOCTL_I915_GEM_CONTEXT_SETPARAM, &p);
 
-   return create.ctx_id;
+   return ctx_id;
 }
 
 static int
diff --git a/src/gallium/drivers/iris/iris_bufmgr.c 
b/src/gallium/drivers/iris/iris_bufmgr.c
index 4854f0d40eb..de38bce035f 100644
--- a/src/gallium/drivers/iris/iris_bufmgr.c
+++ b/src/gallium/drivers/iris/iris_bufmgr.c
@@ -2223,14 +2223,10 @@ iris_create_hw_context(struct iris_bufmgr *bufmgr, bool 
protected)
          return 0;
       }
    } else {
-      struct drm_i915_gem_context_create create = { };
-      int ret = intel_ioctl(bufmgr->fd, DRM_IOCTL_I915_GEM_CONTEXT_CREATE, 
&create);
-      if (ret != 0) {
-         DBG("DRM_IOCTL_I915_GEM_CONTEXT_CREATE failed: %s\n", 
strerror(errno));
+      if (!intel_gem_create_context(bufmgr->fd, &ctx_id)) {
+         DBG("intel_gem_create_context failed: %s\n", strerror(errno));
          return 0;
       }
-
-      ctx_id = create.ctx_id;
       iris_hw_context_set_unrecoverable(bufmgr, ctx_id);
    }
 
diff --git a/src/intel/common/intel_gem.c b/src/intel/common/intel_gem.c
index 3768d6ef0df..69dde109b49 100644
--- a/src/intel/common/intel_gem.c
+++ b/src/intel/common/intel_gem.c
@@ -58,6 +58,16 @@ intel_gem_supports_syncobj_wait(int fd)
    return ret == -1 && errno == ETIME;
 }
 
+bool
+intel_gem_create_context(int fd, uint32_t *context_id)
+{
+   struct drm_i915_gem_context_create create = {};
+   if (intel_ioctl(fd, DRM_IOCTL_I915_GEM_CONTEXT_CREATE, &create))
+      return false;
+   *context_id = create.ctx_id;
+   return true;
+}
+
 bool
 intel_gem_create_context_engines(int fd,
                                  const struct intel_query_engine_info *info,
diff --git a/src/intel/common/intel_gem.h b/src/intel/common/intel_gem.h
index ddaeb718b5a..b13ef367d6d 100644
--- a/src/intel/common/intel_gem.h
+++ b/src/intel/common/intel_gem.h
@@ -159,6 +159,7 @@ intel_i915_query_alloc(int fd, uint64_t query_id, int32_t 
*query_length)
 
 bool intel_gem_supports_syncobj_wait(int fd);
 
+bool intel_gem_create_context(int fd, uint32_t *context_id);
 bool
 intel_gem_create_context_engines(int fd,
                                  const struct intel_query_engine_info *info,
diff --git a/src/intel/common/meson.build b/src/intel/common/meson.build
index b44dd813592..f3fd0f6fa26 100644
--- a/src/intel/common/meson.build
+++ b/src/intel/common/meson.build
@@ -124,7 +124,7 @@ if with_tests and not with_platform_android
       ],
       gnu_symbol_visibility : 'hidden',
       include_directories : [inc_include, inc_src, inc_intel],
-      link_with : [libintel_dev],
+      link_with : [libintel_dev, libintel_common],
       dependencies : [dep_libdrm, idep_gtest, idep_genxml, idep_mesautil],
       install : install_intel_gpu_tests,
     )
diff --git a/src/intel/common/tests/mi_builder_test.cpp 
b/src/intel/common/tests/mi_builder_test.cpp
index 51021acc23b..791869d4c36 100644
--- a/src/intel/common/tests/mi_builder_test.cpp
+++ b/src/intel/common/tests/mi_builder_test.cpp
@@ -28,6 +28,7 @@
 #include <gtest/gtest.h>
 
 #include "c99_compat.h"
+#include "common/intel_gem.h"
 #include "dev/intel_device_info.h"
 #include "drm-uapi/i915_drm.h"
 #include "genxml/gen_macros.h"
@@ -128,7 +129,7 @@ public:
    }
 
    int fd;
-   int ctx_id;
+   uint32_t ctx_id;
    intel_device_info devinfo;
 
    uint32_t batch_bo_handle;
@@ -209,10 +210,7 @@ mi_builder_test::SetUp()
    }
    ASSERT_TRUE(i < max_devices) << "Failed to find a DRM device";
 
-   drm_i915_gem_context_create ctx_create = drm_i915_gem_context_create();
-   ASSERT_EQ(drmIoctl(fd, DRM_IOCTL_I915_GEM_CONTEXT_CREATE,
-                      (void *)&ctx_create), 0) << strerror(errno);
-   ctx_id = ctx_create.ctx_id;
+   ASSERT_TRUE(intel_gem_create_context(fd, &ctx_id)) << strerror(errno);
 
    if (GFX_VER >= 8) {
       /* On gfx8+, we require softpin */
diff --git a/src/intel/vulkan/anv_device.c b/src/intel/vulkan/anv_device.c
index bf76bd25f51..e19ebfefaeb 100644
--- a/src/intel/vulkan/anv_device.c
+++ b/src/intel/vulkan/anv_device.c
@@ -3163,15 +3163,12 @@ anv_device_setup_context(struct anv_device *device,
                             "kernel context creation failed");
    } else {
       assert(num_queues == 1);
-      device->context_id = anv_gem_create_context(device);
+      if (!intel_gem_create_context(device->fd, &device->context_id))
+         result = vk_error(device, VK_ERROR_INITIALIZATION_FAILED);
    }
 
    if (result != VK_SUCCESS)
       return result;
-   if (device->context_id == -1) {
-      result = vk_error(device, VK_ERROR_INITIALIZATION_FAILED);
-      return result;
-   }
 
    /* Here we tell the kernel not to attempt to recover our context but
     * immediately (on the next batchbuffer submission) report that the
diff --git a/src/intel/vulkan/anv_gem.c b/src/intel/vulkan/anv_gem.c
index 9b5ef3534ed..b87c99d43e2 100644
--- a/src/intel/vulkan/anv_gem.c
+++ b/src/intel/vulkan/anv_gem.c
@@ -302,18 +302,6 @@ anv_gem_has_context_priority(int fd, 
VkQueueGlobalPriorityKHR priority)
                                      priority);
 }
 
-int
-anv_gem_create_context(struct anv_device *device)
-{
-   struct drm_i915_gem_context_create create = { 0 };
-
-   int ret = intel_ioctl(device->fd, DRM_IOCTL_I915_GEM_CONTEXT_CREATE, 
&create);
-   if (ret == -1)
-      return -1;
-
-   return create.ctx_id;
-}
-
 int
 anv_gem_destroy_context(struct anv_device *device, int context)
 {
diff --git a/src/intel/vulkan/anv_gem_stubs.c b/src/intel/vulkan/anv_gem_stubs.c
index 30df5bc01e6..ce01fa40d39 100644
--- a/src/intel/vulkan/anv_gem_stubs.c
+++ b/src/intel/vulkan/anv_gem_stubs.c
@@ -124,12 +124,6 @@ anv_gem_get_param(int fd, uint32_t param)
    unreachable("Unused");
 }
 
-int
-anv_gem_create_context(struct anv_device *device)
-{
-   unreachable("Unused");
-}
-
 int
 anv_gem_destroy_context(struct anv_device *device, int context)
 {
diff --git a/src/intel/vulkan/anv_private.h b/src/intel/vulkan/anv_private.h
index f0028874e7d..49f62cfc73c 100644
--- a/src/intel/vulkan/anv_private.h
+++ b/src/intel/vulkan/anv_private.h
@@ -1135,7 +1135,7 @@ struct anv_device {
     struct anv_physical_device *                physical;
     const struct intel_device_info *            info;
     struct isl_device                           isl_dev;
-    int                                         context_id;
+    uint32_t                                    context_id;
     int                                         fd;
     bool                                        robust_buffer_access;
 
@@ -1346,7 +1346,6 @@ int anv_gem_execbuffer(struct anv_device *device,
                        struct drm_i915_gem_execbuffer2 *execbuf);
 int anv_gem_set_tiling(struct anv_device *device, uint32_t gem_handle,
                        uint32_t stride, uint32_t tiling);
-int anv_gem_create_context(struct anv_device *device);
 bool anv_gem_has_context_priority(int fd, VkQueueGlobalPriorityKHR priority);
 int anv_gem_destroy_context(struct anv_device *device, int context);
 int anv_gem_set_context_param(int fd, int context, uint32_t param,
diff --git a/src/intel/vulkan_hasvk/anv_device.c 
b/src/intel/vulkan_hasvk/anv_device.c
index 46a50b6ca80..047c0d98ed2 100644
--- a/src/intel/vulkan_hasvk/anv_device.c
+++ b/src/intel/vulkan_hasvk/anv_device.c
@@ -2812,15 +2812,12 @@ anv_device_setup_context(struct anv_device *device,
                             "kernel context creation failed");
    } else {
       assert(num_queues == 1);
-      device->context_id = anv_gem_create_context(device);
+      if (!intel_gem_create_context(device->fd, &device->context_id))
+         result = vk_error(device, VK_ERROR_INITIALIZATION_FAILED);
    }
 
    if (result != VK_SUCCESS)
       return result;
-   if (device->context_id == -1) {
-      result = vk_error(device, VK_ERROR_INITIALIZATION_FAILED);
-      return result;
-   }
 
    /* Here we tell the kernel not to attempt to recover our context but
     * immediately (on the next batchbuffer submission) report that the
diff --git a/src/intel/vulkan_hasvk/anv_gem.c b/src/intel/vulkan_hasvk/anv_gem.c
index cabfc3a1a02..903650452c0 100644
--- a/src/intel/vulkan_hasvk/anv_gem.c
+++ b/src/intel/vulkan_hasvk/anv_gem.c
@@ -273,18 +273,6 @@ anv_gem_has_context_priority(int fd, int priority)
                                      priority);
 }
 
-int
-anv_gem_create_context(struct anv_device *device)
-{
-   struct drm_i915_gem_context_create create = { 0 };
-
-   int ret = intel_ioctl(device->fd, DRM_IOCTL_I915_GEM_CONTEXT_CREATE, 
&create);
-   if (ret == -1)
-      return -1;
-
-   return create.ctx_id;
-}
-
 int
 anv_gem_destroy_context(struct anv_device *device, int context)
 {
diff --git a/src/intel/vulkan_hasvk/anv_gem_stubs.c 
b/src/intel/vulkan_hasvk/anv_gem_stubs.c
index 6747276877f..e86ae41a57e 100644
--- a/src/intel/vulkan_hasvk/anv_gem_stubs.c
+++ b/src/intel/vulkan_hasvk/anv_gem_stubs.c
@@ -116,12 +116,6 @@ anv_gem_get_param(int fd, uint32_t param)
    unreachable("Unused");
 }
 
-int
-anv_gem_create_context(struct anv_device *device)
-{
-   unreachable("Unused");
-}
-
 int
 anv_gem_destroy_context(struct anv_device *device, int context)
 {
diff --git a/src/intel/vulkan_hasvk/anv_private.h 
b/src/intel/vulkan_hasvk/anv_private.h
index 479a2fc9433..600489d76a5 100644
--- a/src/intel/vulkan_hasvk/anv_private.h
+++ b/src/intel/vulkan_hasvk/anv_private.h
@@ -1116,7 +1116,7 @@ struct anv_device {
     struct anv_physical_device *                physical;
     const struct intel_device_info *            info;
     struct isl_device                           isl_dev;
-    int                                         context_id;
+    uint32_t                                    context_id;
     int                                         fd;
     bool                                        can_chain_batches;
     bool                                        robust_buffer_access;
@@ -1381,7 +1381,6 @@ int anv_gem_execbuffer(struct anv_device *device,
                        struct drm_i915_gem_execbuffer2 *execbuf);
 int anv_gem_set_tiling(struct anv_device *device, uint32_t gem_handle,
                        uint32_t stride, uint32_t tiling);
-int anv_gem_create_context(struct anv_device *device);
 bool anv_gem_has_context_priority(int fd, int priority);
 int anv_gem_destroy_context(struct anv_device *device, int context);
 int anv_gem_set_context_param(int fd, int context, uint32_t param,

Reply via email to