iris: Use the new I915_USERPTR_PROBE API
authorKenneth Graunke <kenneth@whitecape.org>
Wed, 21 Jul 2021 19:02:54 +0000 (12:02 -0700)
committerJason Ekstrand <jason@jlekstrand.net>
Thu, 12 Aug 2021 20:07:33 +0000 (15:07 -0500)
We need to raise an error when importing a user pointer as a BO if the
supplied pointer can't actually be used on the GPU.  Previously, we were
(ab)using the SET_DOMAIN ioctl for this, but it's not really intended
for that purpose, and is going away on discrete.

Fortunately, there's a new kernel API for this: the I915_USERPTR_PROBE
flag tries to perform basic sanity checking of the supplied pointer so
that we can at least reject obvious misuse of this API up front.

Use the new API where available.  Fall back to SET_DOMAIN if it isn't.

Reviewed-by: Jason Ekstrand <jason@jlekstrand.net>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/12044>

src/gallium/drivers/iris/iris_bufmgr.c

index 76c7391..dd69492 100644 (file)
@@ -210,6 +210,7 @@ struct iris_bufmgr {
    bool has_local_mem:1;
    bool has_mmap_offset:1;
    bool has_tiling_uapi:1;
+   bool has_userptr_probe:1;
    bool bo_reuse:1;
 
    struct intel_aux_map_context *aux_map_ctx;
@@ -707,18 +708,21 @@ iris_bo_create_userptr(struct iris_bufmgr *bufmgr, const char *name,
    struct drm_i915_gem_userptr arg = {
       .user_ptr = (uintptr_t)ptr,
       .user_size = size,
+      .flags = bufmgr->has_userptr_probe ? I915_USERPTR_PROBE : 0,
    };
    if (intel_ioctl(bufmgr->fd, DRM_IOCTL_I915_GEM_USERPTR, &arg))
       goto err_free;
    bo->gem_handle = arg.handle;
 
-   /* Check the buffer for validity before we try and use it in a batch */
-   struct drm_i915_gem_set_domain sd = {
-      .handle = bo->gem_handle,
-      .read_domains = I915_GEM_DOMAIN_CPU,
-   };
-   if (intel_ioctl(bufmgr->fd, DRM_IOCTL_I915_GEM_SET_DOMAIN, &sd))
-      goto err_close;
+   if (!bufmgr->has_userptr_probe) {
+      /* Check the buffer for validity before we try and use it in a batch */
+      struct drm_i915_gem_set_domain sd = {
+         .handle = bo->gem_handle,
+         .read_domains = I915_GEM_DOMAIN_CPU,
+      };
+      if (intel_ioctl(bufmgr->fd, DRM_IOCTL_I915_GEM_SET_DOMAIN, &sd))
+         goto err_close;
+   }
 
    bo->name = name;
    bo->size = size;
@@ -1774,6 +1778,8 @@ iris_bufmgr_create(struct intel_device_info *devinfo, int fd, bool bo_reuse)
    bufmgr->has_tiling_uapi = devinfo->has_tiling_uapi;
    bufmgr->bo_reuse = bo_reuse;
    bufmgr->has_mmap_offset = gem_param(fd, I915_PARAM_MMAP_GTT_VERSION) >= 4;
+   bufmgr->has_userptr_probe =
+      gem_param(fd, I915_PARAM_HAS_USERPTR_PROBE) >= 1;
    iris_bufmgr_query_meminfo(bufmgr);
 
    STATIC_ASSERT(IRIS_MEMZONE_SHADER_START == 0ull);