Skip to contentSkip to conversation

drm/vmwgfx: Add support for userspace managed surfaces.

PATCH New Maaz Mombasawala 1 participant 4 messages Dec 10, 2024

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v3 cover drm/vmwgfx: Add support for userspace managed surfaces.

This series introduces basic support for userspace managed surfaces. The lifetime and id's of these surfaces is managed by userspace submitted commands instead of relying on the kernel to manage them.

v2: Add flag for userspace to check userspace surface support.

v3:
- Unref surface resource properly if there is error in creating surface object.
- Check that guest backed objects are supported when reporting DRM_VMW_PARAM_USER_SRF.
- Bind hw_destroy function when command buffer gets commited.

Maaz Mombasawala (3):
drm/vmwgfx: Introduce userspace managed surfaces drm/vmwgfx: Support hw_destroy for userspace managed surfaces drm/vmwgfx: Add support for older define commands for userspace
surfaces

 drivers/gpu/drm/vmwgfx/vmwgfx_drv.h     |  24 +-
 drivers/gpu/drm/vmwgfx/vmwgfx_execbuf.c | 331 ++++++++++++++++++++++--
 drivers/gpu/drm/vmwgfx/vmwgfx_ioctl.c   |   3 +
 drivers/gpu/drm/vmwgfx/vmwgfx_surface.c | 198 +++++++++++++-
 include/uapi/drm/vmwgfx_drm.h           |   4 +
 5 files changed, 527 insertions(+), 33 deletions(-)

Maaz Mombasawala (3):
drm/vmwgfx: Introduce userspace managed surfaces drm/vmwgfx: Support hw_destroy for userspace managed surfaces drm/vmwgfx: Add support for older define commands for userspace
surfaces

 drivers/gpu/drm/vmwgfx/vmwgfx_drv.h     |  23 +-
 drivers/gpu/drm/vmwgfx/vmwgfx_execbuf.c | 326 ++++++++++++++++++++++--
 drivers/gpu/drm/vmwgfx/vmwgfx_ioctl.c   |   3 +
 drivers/gpu/drm/vmwgfx/vmwgfx_surface.c | 203 ++++++++++++++-
 include/uapi/drm/vmwgfx_drm.h           |   4 +
 5 files changed, 526 insertions(+), 33 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH v3 1/3 drm/vmwgfx: Introduce userspace managed surfaces

The kernel currently exposes both mobs and surfaces to userspace through ioctls. We would like to move to a model where kernel would only expose mobs and have userspace manage surfaces. This would simplify kernel paths for surfaces since these userspace managed surfaces will not support prime transfer.

Allow userspace submission of surface create and destroy commands. Userspace submits its own surface id which is mapped to a ttm base object and a resource with their corresponding ids.

---
 drivers/gpu/drm/vmwgfx/vmwgfx_drv.h     |  23 ++-
 drivers/gpu/drm/vmwgfx/vmwgfx_execbuf.c | 234 +++++++++++++++++++++---
 drivers/gpu/drm/vmwgfx/vmwgfx_ioctl.c   |   3 +
 drivers/gpu/drm/vmwgfx/vmwgfx_surface.c | 160 +++++++++++++++-
 include/uapi/drm/vmwgfx_drm.h           |   4 +
 5 files changed, 393 insertions(+), 31 deletions(-)
drivers/gpu/drm/vmwgfx/vmwgfx_drv.h +20 -3
index b21831ef214a..41f8faf83180 100644
@@ -1,8 +1,8 @@
 /* SPDX-License-Identifier: GPL-2.0 OR MIT */
 /**************************************************************************
  *
- * Copyright (c) 2009-2024 Broadcom. All Rights Reserved. The term
- * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
+ * Copyright (c) 2009-2024 Broadcom. All Rights Reserved.
+ * The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the
@@ -196,7 +196,8 @@ enum vmw_res_type {
 enum vmw_cmdbuf_res_type {
 	vmw_cmdbuf_res_shader,
 	vmw_cmdbuf_res_view,
-	vmw_cmdbuf_res_streamoutput
+	vmw_cmdbuf_res_streamoutput,
+	vmw_cmdbuf_res_surface
 };
 
 struct vmw_cmdbuf_res_manager;
@@ -263,6 +264,11 @@ struct vmw_surface {
 	struct list_head view_list;
 };
 
+struct vmw_cmdbuf_surface {
+	struct vmw_surface surface;
+	struct ttm_base_object base;
+};
+
 struct vmw_fifo_state {
 	unsigned long reserved_size;
 	u32 *dynamic_buffer;
@@ -1199,6 +1205,17 @@ int vmw_dumb_create(struct drm_file *file_priv,
 		    struct drm_device *dev,
 		    struct drm_mode_create_dumb *args);
 
+struct vmw_cmdbuf_surface *vmw_res_to_cmdbuf_srf(struct vmw_resource *res);
+
+int vmw_cmdbuf_surface_define(struct vmw_private *dev_priv,
+			      struct vmw_sw_context *sw_context,
+			      struct vmw_surface_metadata *metadata,
+			      uint32 user_key);
+
+int vmw_cmdbuf_surface_destroy(struct vmw_private *dev_priv,
+			       struct vmw_sw_context *sw_context,
+			       uint32 user_key);
+
 /*
  * Shader management - vmwgfx_shader.c
  */
drivers/gpu/drm/vmwgfx/vmwgfx_execbuf.c +208 -26

283 lines not on this page · raw message

drivers/gpu/drm/vmwgfx/vmwgfx_ioctl.c +3 -0
index 835d1eed8dd9..db85d6b6cbbe 100644
@@ -112,6 +112,9 @@ int vmw_getparam_ioctl(struct drm_device *dev, void *data,
 	case DRM_VMW_PARAM_DEVICE_ID:
 		param->value = to_pci_dev(dev_priv->drm.dev)->device;
 		break;
+	case DRM_VMW_PARAM_USER_SRF:
+		param->value = dev_priv->capabilities & SVGA_CAP_GBOBJECTS;
+		break;
 	default:
 		return -EINVAL;
 	}
drivers/gpu/drm/vmwgfx/vmwgfx_surface.c +158 -2

181 lines not on this page · raw message

include/uapi/drm/vmwgfx_drm.h +4 -0
index 7d786a0cc835..9b92ab1fd113 100644
@@ -99,6 +99,9 @@ extern "C" {
  *
  * DRM_VMW_PARAM_DEVICE_ID
  * PCI ID of the underlying SVGA device.
+ *
+ * DRM_VMW_PARAM_USER_SRF
+ * Userspace managed surface support is enabled.
  */
 
 #define DRM_VMW_PARAM_NUM_STREAMS      0
@@ -119,6 +122,7 @@ extern "C" {
 #define DRM_VMW_PARAM_SM5              15
 #define DRM_VMW_PARAM_GL43             16
 #define DRM_VMW_PARAM_DEVICE_ID        17
+#define DRM_VMW_PARAM_USER_SRF         18
 
 /**
  * enum drm_vmw_handle_type - handle type for ref ioctls
Signature · 1 line
2.43.0
#2 lore raw

PATCH v3 2/3 drm/vmwgfx: Support hw_destroy for userspace managed surfaces

In reply to Maaz Mombasawala

A userspace may create a userspace managed surface but not destroy it, add hw_destroy function for userspace surfaces so that vmwgfx records the destroy command and submits it when the userspace context is destroyed.

---
 drivers/gpu/drm/vmwgfx/vmwgfx_surface.c | 43 +++++++++++++++++++++++++
 1 file changed, 43 insertions(+)
drivers/gpu/drm/vmwgfx/vmwgfx_surface.c +43 -0
index 3f541e96e6b6..4e7edcbe398b 100644
@@ -113,6 +113,8 @@ static int vmw_surface_dirty_sync(struct vmw_resource *res);
 static void vmw_surface_dirty_range_add(struct vmw_resource *res, size_t start,
 					size_t end);
 static int vmw_surface_clean(struct vmw_resource *res);
+static void vmw_cmdbuf_surface_commit_notify(struct vmw_resource *res,
+					     enum vmw_cmdbuf_res_state state);
 
 static const struct vmw_user_resource_conv user_surface_conv = {
 	.object_type = VMW_RES_SURFACE,
@@ -165,6 +167,7 @@ static const struct vmw_res_func vmw_gb_cmdbuf_surface_func = {
 	.type_name = "guest backed command buffer managed surface",
 	.domain = VMW_BO_DOMAIN_MOB,
 	.busy_domain = VMW_BO_DOMAIN_MOB,
+	.commit_notify = vmw_cmdbuf_surface_commit_notify,
 };
 
 /*
@@ -2408,6 +2411,46 @@ static void vmw_cmdbuf_surface_base_release(struct ttm_base_object **p_base)
 }
 
 
+static void vmw_cmdbuf_surface_hw_destroy(struct vmw_resource *res)
+{
+	struct vmw_private *dev_priv = res->dev_priv;
+	struct vmw_cmdbuf_surface *surface;
+	struct {
+		SVGA3dCmdHeader header;
+		SVGA3dCmdDestroyGBSurface body;
+	} *cmd;
+
+	if (res->id == -1)
+		return;
+
+	surface = vmw_res_to_cmdbuf_srf(res);
+	mutex_lock(&dev_priv->binding_mutex);
+	vmw_view_surface_list_destroy(dev_priv, &surface->surface.view_list);
+	vmw_binding_res_list_scrub(&res->binding_head);
+
+	cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd));
+	if (unlikely(!cmd)) {
+		mutex_unlock(&dev_priv->binding_mutex);
+		return;
+	}
+
+	cmd->header.id = SVGA_3D_CMD_DESTROY_GB_SURFACE;
+	cmd->header.size = sizeof(cmd->body);
+	cmd->body.sid = res->id;
+	vmw_cmd_commit(dev_priv, sizeof(*cmd));
+	mutex_unlock(&dev_priv->binding_mutex);
+}
+
+static void vmw_cmdbuf_surface_commit_notify(struct vmw_resource *res,
+					     enum vmw_cmdbuf_res_state state)
+{
+	if (state == VMW_CMDBUF_RES_ADD)
+		res->hw_destroy = vmw_cmdbuf_surface_hw_destroy;
+	else
+		res->hw_destroy = NULL;
+}
+
+
 int vmw_cmdbuf_surface_define(struct vmw_private *dev_priv,
 			      struct vmw_sw_context *sw_context,
 			      struct vmw_surface_metadata *metadata,
Signature · 1 line
2.43.0
#3 lore raw

PATCH v3 3/3 drm/vmwgfx: Add support for older define commands for userspace surfaces

In reply to Maaz Mombasawala

Older HW versions of the svga device will only support older versions of DefineGBSurface command.
Allow userspace to submit v3 and v2 of the DefineGBSurface command for userspace managed surfaces. The first version of the command is not supported since it is only used for pre-dx hardware.

---
 drivers/gpu/drm/vmwgfx/vmwgfx_execbuf.c | 92 ++++++++++++++++++++++++-
 1 file changed, 90 insertions(+), 2 deletions(-)
drivers/gpu/drm/vmwgfx/vmwgfx_execbuf.c +90 -2
index 0468c9f4f293..71c1e0092d55 100644
@@ -3324,6 +3324,92 @@ vmw_cmd_get_expected_surface_version(struct vmw_private *dev_priv,
 	return 0;
 }
 
+static int vmw_cmd_define_gb_surface_v2(struct vmw_private *dev_priv,
+					struct vmw_sw_context *sw_context,
+					SVGA3dCmdHeader *header)
+{
+	VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdDefineGBSurface_v2);
+	uint32 expected_cmd_id;
+	struct vmw_surface_metadata metadata = {0};
+	int ret;
+
+	cmd = container_of(header, typeof(*cmd), header);
+
+	ret = vmw_cmd_get_expected_surface_version(dev_priv,
+						   cmd->body.arraySize,
+						   &expected_cmd_id);
+	if (ret || (expected_cmd_id != header->id))
+		return -EINVAL;
+
+	if (cmd->body.sid >= VMWGFX_NUM_GB_SURFACE)
+		return -EINVAL;
+
+	metadata.flags = cmd->body.surfaceFlags;
+	metadata.format = cmd->body.format;
+	metadata.mip_levels[0] = cmd->body.numMipLevels;
+	metadata.multisample_count = cmd->body.multisampleCount;
+	metadata.autogen_filter = cmd->body.autogenFilter;
+	metadata.array_size = cmd->body.arraySize;
+	metadata.num_sizes = 1;
+	metadata.base_size.width = cmd->body.size.width;
+	metadata.base_size.height = cmd->body.size.height;
+	metadata.base_size.depth = cmd->body.size.depth;
+
+	ret = vmw_cmdbuf_surface_define(dev_priv, sw_context, &metadata,
+					cmd->body.sid);
+	if (unlikely(ret))
+		return ret;
+
+	ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
+				VMW_RES_DIRTY_NONE, NULL, &cmd->body.sid, NULL);
+
+	return ret;
+}
+
+static int vmw_cmd_define_gb_surface_v3(struct vmw_private *dev_priv,
+					struct vmw_sw_context *sw_context,
+					SVGA3dCmdHeader *header)
+{
+	VMW_DECLARE_CMD_VAR(*cmd, SVGA3dCmdDefineGBSurface_v3);
+	uint32 expected_cmd_id;
+	struct vmw_surface_metadata metadata = {0};
+	int ret;
+
+	cmd = container_of(header, typeof(*cmd), header);
+
+	ret = vmw_cmd_get_expected_surface_version(dev_priv,
+						   cmd->body.arraySize,
+						   &expected_cmd_id);
+	if (ret || (expected_cmd_id != header->id))
+		return -EINVAL;
+
+	if (cmd->body.sid >= VMWGFX_NUM_GB_SURFACE)
+		return -EINVAL;
+
+	metadata.flags = cmd->body.surfaceFlags;
+	metadata.format = cmd->body.format;
+	metadata.mip_levels[0] = cmd->body.numMipLevels;
+	metadata.multisample_count = cmd->body.multisampleCount;
+	metadata.multisample_pattern = cmd->body.multisamplePattern;
+	metadata.quality_level = cmd->body.qualityLevel;
+	metadata.autogen_filter = cmd->body.autogenFilter;
+	metadata.array_size = cmd->body.arraySize;
+	metadata.num_sizes = 1;
+	metadata.base_size.width = cmd->body.size.width;
+	metadata.base_size.height = cmd->body.size.height;
+	metadata.base_size.depth = cmd->body.size.depth;
+
+	ret = vmw_cmdbuf_surface_define(dev_priv, sw_context, &metadata,
+					cmd->body.sid);
+	if (unlikely(ret))
+		return ret;
+
+	ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
+				VMW_RES_DIRTY_NONE, NULL, &cmd->body.sid, NULL);
+
+	return ret;
+}
+
 static int vmw_cmd_define_gb_surface_v4(struct vmw_private *dev_priv,
 					struct vmw_sw_context *sw_context,
 					SVGA3dCmdHeader *header)
@@ -3606,8 +3692,8 @@ static const struct vmw_cmd_entry vmw_cmd_entries[SVGA_3D_CMD_MAX] = {
 		    false, false, true),
 	VMW_CMD_DEF(SVGA_3D_CMD_GB_MOB_FENCE, &vmw_cmd_invalid,
 		    false, false, true),
-	VMW_CMD_DEF(SVGA_3D_CMD_DEFINE_GB_SURFACE_V2, &vmw_cmd_invalid,
-		    false, false, true),
+	VMW_CMD_DEF(SVGA_3D_CMD_DEFINE_GB_SURFACE_V2,
+		    &vmw_cmd_define_gb_surface_v2, true, false, true),
 
 	/* SM commands */
 	VMW_CMD_DEF(SVGA_3D_CMD_DX_DEFINE_CONTEXT, &vmw_cmd_invalid,
@@ -3761,6 +3847,8 @@ static const struct vmw_cmd_entry vmw_cmd_entries[SVGA_3D_CMD_MAX] = {
 		    true, false, true),
 	VMW_CMD_DEF(SVGA_3D_CMD_INTRA_SURFACE_COPY, &vmw_cmd_intra_surface_copy,
 		    true, false, true),
+	VMW_CMD_DEF(SVGA_3D_CMD_DEFINE_GB_SURFACE_V3,
+		    &vmw_cmd_define_gb_surface_v3, true, false, true),
 
 	/*
 	 * SM5 commands
Signature · 1 line
2.43.0