Skip to contentSkip to conversation

drm/amdgpu: rework amdgpu open() path to function in sleep state

PATCH In review Shahyan Soltani 3 participants 8 messages Nov 30 – Dec 1

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

v4 cover drm/amdgpu: rework amdgpu open() path to function in sleep state

This patch series reworks the amdgpu open/close, vm, cs, and validation functionality to allow for access of the GPU's CPU sided bookkeeping data without waking up the GPU from idle.

The overhaul will allow a few tools to function without waking up the GPU, which can improve power efficiency within relevant domains, and can hopefully be expanded on later to adapt for useful utilities.

v2:

- Force a vm->generation mismatch by adding 1 to the result of amdgpu_vm_generation.

- Separate validation logic from patch "drm/amdgpu: defer GPU resume from open/close to first use" into a new patch.

- Change validation logic to not depend on flags to operate. Instead relying on a vm->generation mismatch.

- Clear vm pts in the kernel.evicted loop instead.

- Remove the root_cleared flag.

- Let the vm domain be initialized to VRAM in amdgpu_vm_pt_create if the vm->root.bo is not null.

- Clear the root page table on vm_init if pm_ref was true.

v3:

- Merged: commit fbbcdad4c7c8 ("drm/amdgpu: clear vm pts upon a vm->generation mismatch")

- Remove pm_runtime_get_sync() and pm_runtime_put_autosuspend() from open/postclose kms.

- Remove the powered_init flag and parameter to vm_init.

- Remove amdgpu_vm_generation() result + 1 patch to instead depend on setting vm->generation to ~0ULL to force the mismatch.

- Initialize the root PD's domain to 0 (system memory) instead of GTT.

- Add validation handling in accordance with amdgpu_ualink.c vm initialization.

v4:

- Add patch "drm/amdgpu: implement IOCTL specific skip resume list" to allow DRM IOCTL's to resume the GPU from a suspended state.

- Add pm_runtime_get_if_active() within the uvd and vcn path of a amdgpu_driver_postclose_kms call.

Shahyan Soltani (3):
drm/amdgpu: implement IOCTL specific skip resume list drm/amdgpu: defer amdgpu_vm_pt_clear() to validation drm/amdgpu: remove pm_runtime from open/close kms paths

 drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c   | 102 +++++++++++++++++++++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_drv.h   |   4 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c   |  34 ++++----
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c    |   5 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c |  13 ++-
 5 files changed, 132 insertions(+), 26 deletions(-)
Signature · 1 line
2.56.0
#1 lore raw

PATCH v4 1/3 drm/amdgpu: implement IOCTL specific skip resume list

Commit a66aee8f3057 ("drm/amdgpu: get rid of amdgpu_drm_ioctl") removed the amdgpu IOCTL wrapper in favor of DRM upstream's IOCTL handler. Removing the wrapper allowed an amdgpu device to resume from BACO/BOCO mode only when required, however, it failed to account for DRM core IOCTL's which require the device to be active.

Add back the amdgpu_ioctl wrappers for both 64bit and 32bit systems but conditionally resume the GPU only if the IOCTL is not specified in the newly introduced amdgpu_ioctls_skip_resume[] list.

Fixes a66aee8f3057 ("drm/amdgpu: get rid of amdgpu_drm_ioctl")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c | 102 +++++++++++++++++++++++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_drv.h |   4 +
 2 files changed, 104 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +100 -2
index d2db20c172c6..5cfc857906fc 100644
@@ -3009,6 +3009,104 @@ static int amdgpu_drm_release(struct inode *inode, struct file *filp)
 	return drm_release(inode, filp);
 }
 
+/**
+ * amdgpu_ioctls_skip_resume[] - List of IOCTLs that do not require hardware access.
+ *
+ * This list needs to be maintained against the most recent DRM IOCTLs.
+ *
+ * If an IOCTL is specified within this list, a runtime reference will not be
+ * held for the IOCTL, preventing the GPU from being resumed upon entry.
+ *
+ * TODO: Add the remaining DRM IOCTLs which don't require hardware access.
+ */
+static const bool amdgpu_ioctls_skip_resume[] = {
+	/* DRM IOCTLs */
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_VERSION),
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_GET_CLIENT),
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_MODE_GETPROPERTY),
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_MODE_GETRESOURCES),
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_MODE_LIST_LESSEES),
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_MODE_GETPLANE),
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_MODE_OBJ_GETPROPERTIES),
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_MODE_GETPROPBLOB),
+	/* AMDGPU IOCTLs */
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_GEM_CREATE),
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_CTX),
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_VM),
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_SCHED),
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_BO_LIST),
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_FENCE_TO_HANDLE),
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_GEM_MMAP),
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_GEM_WAIT_IDLE),
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_CS),
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_INFO),
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_WAIT_CS),
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_WAIT_FENCES),
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_GEM_METADATA),
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_GEM_VA),
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_GEM_OP),
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_GEM_USERPTR),
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_USERQ),
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_USERQ_SIGNAL),
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_USERQ_WAIT),
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_GEM_LIST_HANDLES),
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_PROC_OPTIONS),
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_UALINK_HANDLE),
+};
+
+long amdgpu_drm_ioctl(struct file *filp,
+		      unsigned int cmd, unsigned long arg)
+{
+	struct drm_file *file_priv = filp->private_data;
+	struct drm_device *dev;
+	unsigned int nr = DRM_IOCTL_NR(cmd);
+	bool pm_ref = nr >= ARRAY_SIZE(amdgpu_ioctls_skip_resume) ?
+		      true : !amdgpu_ioctls_skip_resume[nr];
+	long ret;
+
+	if (pm_ref) {
+		dev = file_priv->minor->dev;
+		ret = pm_runtime_resume_and_get(dev->dev);
+		if (ret < 0)
+			goto out;
+	}
+
+	ret = drm_ioctl(filp, cmd, arg);
+
+	if (pm_ref)
+		pm_runtime_put_autosuspend(dev->dev);
+out:
+	return ret;
+}
+
+#ifdef CONFIG_COMPAT
+static long amdgpu_kms_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
+{
+	struct drm_file *file_priv = filp->private_data;
+	struct drm_device *dev;
+	unsigned int nr = DRM_IOCTL_NR(cmd);
+	bool pm_ref = nr >= ARRAY_SIZE(amdgpu_ioctls_skip_resume) ?
+		      true : !amdgpu_ioctls_skip_resume[nr];
+	long ret;
+
+	if (nr < DRM_COMMAND_BASE) {
+		if (pm_ref) {
+			dev = file_priv->minor->dev;
+			ret = pm_runtime_resume_and_get(dev->dev);
+			if (ret < 0)
+				goto out;
+		}
+
+		ret = drm_compat_ioctl(filp, cmd, arg);
+		if (pm_ref)
+			pm_runtime_put_autosuspend(dev->dev);
+	} else
+		ret = amdgpu_drm_ioctl(filp, cmd, arg);
+out:
+	return ret;
+}
+#endif
+
 static const struct dev_pm_ops amdgpu_pm_ops = {
 	.prepare = pm_sleep_ptr(amdgpu_pmops_prepare),
 	.complete = pm_sleep_ptr(amdgpu_pmops_complete),
@@ -3041,12 +3139,12 @@ static const struct file_operations amdgpu_driver_kms_fops = {
 	.open = drm_open,
 	.flush = amdgpu_flush,
 	.release = amdgpu_drm_release,
-	.unlocked_ioctl = drm_ioctl,
+	.unlocked_ioctl = amdgpu_drm_ioctl,
 	.mmap = drm_gem_mmap,
 	.poll = drm_poll,
 	.read = drm_read,
 #ifdef CONFIG_COMPAT
-	.compat_ioctl = drm_compat_ioctl,
+	.compat_ioctl = amdgpu_kms_compat_ioctl,
 #endif
 #ifdef CONFIG_PROC_FS
 	.show_fdinfo = drm_show_fdinfo,
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.h +4 -0
index 6b06c0956e48..d213b3b420ed 100644
@@ -43,4 +43,8 @@
 
 extern const struct drm_driver amdgpu_partition_driver;
 
+#define AMDGPU_IOCTL_SKIP_RESUME(ioctl)	[DRM_IOCTL_NR(ioctl)] = true
+
+long amdgpu_drm_ioctl(struct file *filp,
+		      unsigned int cmd, unsigned long arg);
 #endif
Signature · 1 line
2.56.0
Reviewed-by on v4 1/3
#2 lore raw

Re: [PATCH v4 1/3] drm/amdgpu: implement IOCTL specific skip resume list

Quoted 110 lines · Shahyan Soltani

Commit a66aee8f3057 ("drm/amdgpu: get rid of amdgpu_drm_ioctl") removed the amdgpu IOCTL wrapper in favor of DRM upstream's IOCTL handler. Removing the wrapper allowed an amdgpu device to resume from BACO/BOCO mode only when required, however, it failed to account for DRM core IOCTL's which require the device to be active.

Add back the amdgpu_ioctl wrappers for both 64bit and 32bit systems but conditionally resume the GPU only if the IOCTL is not specified in the newly introduced amdgpu_ioctls_skip_resume[] list.

Fixes a66aee8f3057 ("drm/amdgpu: get rid of amdgpu_drm_ioctl")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c | 102 +++++++++++++++++++++++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_drv.h |   4 +
 2 files changed, 104 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +83 -0
index d2db20c172c6..5cfc857906fc 100644
@@ -3009,6 +3009,104 @@ static int amdgpu_drm_release(struct inode *inode, struct file *filp)
        return drm_release(inode, filp);
 }
 
+/**
+ * amdgpu_ioctls_skip_resume[] - List of IOCTLs that do not require hardware access.
+ *
+ * This list needs to be maintained against the most recent DRM IOCTLs.
+ *
+ * If an IOCTL is specified within this list, a runtime reference will not be
+ * held for the IOCTL, preventing the GPU from being resumed upon entry.
+ *
+ * TODO: Add the remaining DRM IOCTLs which don't require hardware access.
+ */
+static const bool amdgpu_ioctls_skip_resume[] = {
+       /* DRM IOCTLs */
+       AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_VERSION),
+       AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_GET_CLIENT),
+       AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_MODE_GETPROPERTY),
+       AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_MODE_GETRESOURCES),
+       AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_MODE_LIST_LESSEES),
+       AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_MODE_GETPLANE),
+       AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_MODE_OBJ_GETPROPERTIES),
+       AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_MODE_GETPROPBLOB),
+       /* AMDGPU IOCTLs */
+       AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_GEM_CREATE),
+       AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_CTX),
+       AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_VM),
+       AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_SCHED),
+       AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_BO_LIST),
+       AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_FENCE_TO_HANDLE),
+       AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_GEM_MMAP),
+       AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_GEM_WAIT_IDLE),
+       AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_CS),
+       AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_INFO),
+       AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_WAIT_CS),
+       AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_WAIT_FENCES),
+       AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_GEM_METADATA),
+       AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_GEM_VA),
+       AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_GEM_OP),
+       AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_GEM_USERPTR),
+       AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_USERQ),
+       AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_USERQ_SIGNAL),
+       AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_USERQ_WAIT),
+       AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_GEM_LIST_HANDLES),
+       AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_PROC_OPTIONS),
+       AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_UALINK_HANDLE),
+};
+
+long amdgpu_drm_ioctl(struct file *filp,
+                     unsigned int cmd, unsigned long arg)
+{
+       struct drm_file *file_priv = filp->private_data;
+       struct drm_device *dev;
+       unsigned int nr = DRM_IOCTL_NR(cmd);
+       bool pm_ref = nr >= ARRAY_SIZE(amdgpu_ioctls_skip_resume) ?
+                     true : !amdgpu_ioctls_skip_resume[nr];
+       long ret;
+
+       if (pm_ref) {
+               dev = file_priv->minor->dev;
+               ret = pm_runtime_resume_and_get(dev->dev);
+               if (ret < 0)
+                       goto out;
+       }
+
+       ret = drm_ioctl(filp, cmd, arg);
+
+       if (pm_ref)
+               pm_runtime_put_autosuspend(dev->dev);
+out:
+       return ret;
+}
+
+#ifdef CONFIG_COMPAT
+static long amdgpu_kms_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
+{
+       struct drm_file *file_priv = filp->private_data;
+       struct drm_device *dev;
+       unsigned int nr = DRM_IOCTL_NR(cmd);
+       bool pm_ref = nr >= ARRAY_SIZE(amdgpu_ioctls_skip_resume) ?
+                     true : !amdgpu_ioctls_skip_resume[nr];
+       long ret;
+
+       if (nr < DRM_COMMAND_BASE) {
+               if (pm_ref) {
+                       dev = file_priv->minor->dev;

drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c static int amdgpu_drm_release(struct inode *inode, struct file *filp) · line 3104

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +10 -0
+                       ret = pm_runtime_resume_and_get(dev->dev);
+                       if (ret < 0)
+                               goto out;
+               }
+
+               ret = drm_compat_ioctl(filp, cmd, arg);
+               if (pm_ref)
+                       pm_runtime_put_autosuspend(dev->dev);
+       } else
+               ret = amdgpu_drm_ioctl(filp, cmd, arg);

Kernel coding style. please add the {} around the else block. With that fixed:

Quoted 39 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +7 -2
+out:
+       return ret;
+}
+#endif
+
 static const struct dev_pm_ops amdgpu_pm_ops = {
        .prepare = pm_sleep_ptr(amdgpu_pmops_prepare),
        .complete = pm_sleep_ptr(amdgpu_pmops_complete),
@@ -3041,12 +3139,12 @@ static const struct file_operations amdgpu_driver_kms_fops = {
        .open = drm_open,
        .flush = amdgpu_flush,
        .release = amdgpu_drm_release,
-       .unlocked_ioctl = drm_ioctl,
+       .unlocked_ioctl = amdgpu_drm_ioctl,
        .mmap = drm_gem_mmap,
        .poll = drm_poll,
        .read = drm_read,
 #ifdef CONFIG_COMPAT
-       .compat_ioctl = drm_compat_ioctl,
+       .compat_ioctl = amdgpu_kms_compat_ioctl,
 #endif
 #ifdef CONFIG_PROC_FS
        .show_fdinfo = drm_show_fdinfo,
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.h +4 -1
index 6b06c0956e48..d213b3b420ed 100644
@@ -43,4 +43,8 @@
 
 extern const struct drm_driver amdgpu_partition_driver;
 
+#define AMDGPU_IOCTL_SKIP_RESUME(ioctl)        [DRM_IOCTL_NR(ioctl)] = true
+
+long amdgpu_drm_ioctl(struct file *filp,
+                     unsigned int cmd, unsigned long arg);
 #endif
--

2.56.0

maintainer
#3 lore raw

Re: [PATCH v4 1/3] drm/amdgpu: implement IOCTL specific skip resume list

In reply to Shahyan Soltani
Quoted 11 lines · Shahyan Soltani

Commit a66aee8f3057 ("drm/amdgpu: get rid of amdgpu_drm_ioctl") removed the amdgpu IOCTL wrapper in favor of DRM upstream's IOCTL handler. Removing the wrapper allowed an amdgpu device to resume from BACO/BOCO mode only when required, however, it failed to account for DRM core IOCTL's which require the device to be active.

Add back the amdgpu_ioctl wrappers for both 64bit and 32bit systems but conditionally resume the GPU only if the IOCTL is not specified in the newly introduced amdgpu_ioctls_skip_resume[] list.

Fixes a66aee8f3057 ("drm/amdgpu: get rid of amdgpu_drm_ioctl")

Maybe we should revert that one first.

Apart from that and the style issues Alex pointed out looks good to me as well.

Regards,
Christian.

Quoted 144 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c | 102 +++++++++++++++++++++++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_drv.h |   4 +
 2 files changed, 104 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +100 -2
index d2db20c172c6..5cfc857906fc 100644
@@ -3009,6 +3009,104 @@ static int amdgpu_drm_release(struct inode *inode, struct file *filp)
 	return drm_release(inode, filp);
 }
 
+/**
+ * amdgpu_ioctls_skip_resume[] - List of IOCTLs that do not require hardware access.
+ *
+ * This list needs to be maintained against the most recent DRM IOCTLs.
+ *
+ * If an IOCTL is specified within this list, a runtime reference will not be
+ * held for the IOCTL, preventing the GPU from being resumed upon entry.
+ *
+ * TODO: Add the remaining DRM IOCTLs which don't require hardware access.
+ */
+static const bool amdgpu_ioctls_skip_resume[] = {
+	/* DRM IOCTLs */
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_VERSION),
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_GET_CLIENT),
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_MODE_GETPROPERTY),
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_MODE_GETRESOURCES),
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_MODE_LIST_LESSEES),
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_MODE_GETPLANE),
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_MODE_OBJ_GETPROPERTIES),
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_MODE_GETPROPBLOB),
+	/* AMDGPU IOCTLs */
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_GEM_CREATE),
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_CTX),
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_VM),
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_SCHED),
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_BO_LIST),
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_FENCE_TO_HANDLE),
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_GEM_MMAP),
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_GEM_WAIT_IDLE),
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_CS),
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_INFO),
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_WAIT_CS),
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_WAIT_FENCES),
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_GEM_METADATA),
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_GEM_VA),
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_GEM_OP),
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_GEM_USERPTR),
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_USERQ),
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_USERQ_SIGNAL),
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_USERQ_WAIT),
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_GEM_LIST_HANDLES),
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_PROC_OPTIONS),
+	AMDGPU_IOCTL_SKIP_RESUME(DRM_IOCTL_AMDGPU_UALINK_HANDLE),
+};
+
+long amdgpu_drm_ioctl(struct file *filp,
+		      unsigned int cmd, unsigned long arg)
+{
+	struct drm_file *file_priv = filp->private_data;
+	struct drm_device *dev;
+	unsigned int nr = DRM_IOCTL_NR(cmd);
+	bool pm_ref = nr >= ARRAY_SIZE(amdgpu_ioctls_skip_resume) ?
+		      true : !amdgpu_ioctls_skip_resume[nr];
+	long ret;
+
+	if (pm_ref) {
+		dev = file_priv->minor->dev;
+		ret = pm_runtime_resume_and_get(dev->dev);
+		if (ret < 0)
+			goto out;
+	}
+
+	ret = drm_ioctl(filp, cmd, arg);
+
+	if (pm_ref)
+		pm_runtime_put_autosuspend(dev->dev);
+out:
+	return ret;
+}
+
+#ifdef CONFIG_COMPAT
+static long amdgpu_kms_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
+{
+	struct drm_file *file_priv = filp->private_data;
+	struct drm_device *dev;
+	unsigned int nr = DRM_IOCTL_NR(cmd);
+	bool pm_ref = nr >= ARRAY_SIZE(amdgpu_ioctls_skip_resume) ?
+		      true : !amdgpu_ioctls_skip_resume[nr];
+	long ret;
+
+	if (nr < DRM_COMMAND_BASE) {
+		if (pm_ref) {
+			dev = file_priv->minor->dev;
+			ret = pm_runtime_resume_and_get(dev->dev);
+			if (ret < 0)
+				goto out;
+		}
+
+		ret = drm_compat_ioctl(filp, cmd, arg);
+		if (pm_ref)
+			pm_runtime_put_autosuspend(dev->dev);
+	} else
+		ret = amdgpu_drm_ioctl(filp, cmd, arg);
+out:
+	return ret;
+}
+#endif
+
 static const struct dev_pm_ops amdgpu_pm_ops = {
 	.prepare = pm_sleep_ptr(amdgpu_pmops_prepare),
 	.complete = pm_sleep_ptr(amdgpu_pmops_complete),
@@ -3041,12 +3139,12 @@ static const struct file_operations amdgpu_driver_kms_fops = {
 	.open = drm_open,
 	.flush = amdgpu_flush,
 	.release = amdgpu_drm_release,
-	.unlocked_ioctl = drm_ioctl,
+	.unlocked_ioctl = amdgpu_drm_ioctl,
 	.mmap = drm_gem_mmap,
 	.poll = drm_poll,
 	.read = drm_read,
 #ifdef CONFIG_COMPAT
-	.compat_ioctl = drm_compat_ioctl,
+	.compat_ioctl = amdgpu_kms_compat_ioctl,
 #endif
 #ifdef CONFIG_PROC_FS
 	.show_fdinfo = drm_show_fdinfo,
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.h +4 -0
index 6b06c0956e48..d213b3b420ed 100644
@@ -43,4 +43,8 @@
 
 extern const struct drm_driver amdgpu_partition_driver;
 
+#define AMDGPU_IOCTL_SKIP_RESUME(ioctl)	[DRM_IOCTL_NR(ioctl)] = true
+
+long amdgpu_drm_ioctl(struct file *filp,
+		      unsigned int cmd, unsigned long arg);
 #endif
#4 lore raw

PATCH v4 2/3 drm/amdgpu: defer amdgpu_vm_pt_clear() to validation

In reply to Shahyan Soltani

Currently amdgpu_vm_pt_clear() is called at open time in amdgpu_vm_init(). It submits an SDMA job, which needs the GPU to be awake and adds complications when it comes to preventing the GPU from resuming when direct hardware access isn't required. Instead, amdgpu_vm_pt_clear() can be moved out of amdgpu_vm_init() to run later on when it's required, such as when amdgpu_vm_validate() is called.

Introduce delayed virtual memory hardware initialization by changing the vm->generation assignment on amdgpu_vm_init() to ~0ULL. By setting vm->generation to this value, it will force a mismatch to occur on init, processing the amdgpu_vm_validate() logic, and clearing the root PT much later on in the stack.

Set the initial domain of vm->root.bo to 0, this will allow all contents of the root PT to exist in system memory without requiring the GPU to be active. The vm->root.bo is then put on the evicted list, which will then migrate the PT to its preferred/allowed domain at the best possible time (usually through amdgpu_vm_validate() on the CS path).

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c    |  5 +----
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c | 13 +++++++++++--
 2 files changed, 12 insertions(+), 6 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1 -4
index 2f47a9c29c5f..437018c95683 100644
@@ -2678,6 +2678,7 @@ int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 
 	vm->is_compute_context = false;
 	vm->need_tlb_fence = amdgpu_userq_enabled(&adev->ddev);
+	vm->generation = ~0ULL;
 
 	vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
 				    AMDGPU_VM_USE_CPU_FOR_GFX);
@@ -2720,10 +2721,6 @@ int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 	if (r)
 		goto error_free_root;
 
-	r = amdgpu_vm_pt_clear(adev, vm, root, false);
-	if (r)
-		goto error_free_root;
-
 	r = amdgpu_vm_create_task_info(vm);
 	if (r)
 		dev_dbg(adev->dev, "Failed to create task info for VM\n");
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c +11 -2
index 8f1e0bc7d65a..7c835545419d 100644
@@ -453,6 +453,7 @@ int amdgpu_vm_pt_create(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 {
 	struct amdgpu_bo_param bp;
 	unsigned int num_entries;
+	u32 set_domain;
 	int r;
 
 	memset(&bp, 0, sizeof(bp));
@@ -461,9 +462,11 @@ int amdgpu_vm_pt_create(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 	bp.byte_align = AMDGPU_GPU_PAGE_SIZE;
 
 	if (!adev->gmc.is_app_apu)
-		bp.domain = AMDGPU_GEM_DOMAIN_VRAM;
+		set_domain = AMDGPU_GEM_DOMAIN_VRAM;
 	else
-		bp.domain = AMDGPU_GEM_DOMAIN_GTT;
+		set_domain = AMDGPU_GEM_DOMAIN_GTT;
+
+	bp.domain = (!vm->root.bo) ? 0 : set_domain;
 
 	bp.domain = amdgpu_bo_get_preferred_domain(adev, bp.domain);
 	bp.flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS |
@@ -490,6 +493,12 @@ int amdgpu_vm_pt_create(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 	if (r)
 		return r;
 
+	if (!vm->root.bo) {
+		(*vmbo)->bo.preferred_domains = set_domain;
+		(*vmbo)->bo.allowed_domains = (set_domain == AMDGPU_GEM_DOMAIN_VRAM) ?
+			AMDGPU_GEM_DOMAIN_VRAM | AMDGPU_GEM_DOMAIN_GTT : set_domain;
+	}
+
 	/* Assumes that reservation is shared with the VM root and that the
 	 * reservation is locked
 	 */
Signature · 1 line
2.56.0
maintainer
#5 lore raw

Re: [PATCH v4 2/3] drm/amdgpu: defer amdgpu_vm_pt_clear() to validation

Quoted 75 lines · Shahyan Soltani

Currently amdgpu_vm_pt_clear() is called at open time in amdgpu_vm_init(). It submits an SDMA job, which needs the GPU to be awake and adds complications when it comes to preventing the GPU from resuming when direct hardware access isn't required. Instead, amdgpu_vm_pt_clear() can be moved out of amdgpu_vm_init() to run later on when it's required, such as when amdgpu_vm_validate() is called.

Introduce delayed virtual memory hardware initialization by changing the vm->generation assignment on amdgpu_vm_init() to ~0ULL. By setting vm->generation to this value, it will force a mismatch to occur on init, processing the amdgpu_vm_validate() logic, and clearing the root PT much later on in the stack.

Set the initial domain of vm->root.bo to 0, this will allow all contents of the root PT to exist in system memory without requiring the GPU to be active. The vm->root.bo is then put on the evicted list, which will then migrate the PT to its preferred/allowed domain at the best possible time (usually through amdgpu_vm_validate() on the CS path).

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c    |  5 +----
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c | 13 +++++++++++--
 2 files changed, 12 insertions(+), 6 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1 -4
index 2f47a9c29c5f..437018c95683 100644
@@ -2678,6 +2678,7 @@ int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 
 	vm->is_compute_context = false;
 	vm->need_tlb_fence = amdgpu_userq_enabled(&adev->ddev);
+	vm->generation = ~0ULL;
 
 	vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
 				    AMDGPU_VM_USE_CPU_FOR_GFX);
@@ -2720,10 +2721,6 @@ int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 	if (r)
 		goto error_free_root;
 
-	r = amdgpu_vm_pt_clear(adev, vm, root, false);
-	if (r)
-		goto error_free_root;
-
 	r = amdgpu_vm_create_task_info(vm);
 	if (r)
 		dev_dbg(adev->dev, "Failed to create task info for VM\n");
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c +5 -2
index 8f1e0bc7d65a..7c835545419d 100644
@@ -453,6 +453,7 @@ int amdgpu_vm_pt_create(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 {
 	struct amdgpu_bo_param bp;
 	unsigned int num_entries;
+	u32 set_domain;
 	int r;
 
 	memset(&bp, 0, sizeof(bp));
@@ -461,9 +462,11 @@ int amdgpu_vm_pt_create(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 	bp.byte_align = AMDGPU_GPU_PAGE_SIZE;
 
 	if (!adev->gmc.is_app_apu)
-		bp.domain = AMDGPU_GEM_DOMAIN_VRAM;
+		set_domain = AMDGPU_GEM_DOMAIN_VRAM;
 	else
-		bp.domain = AMDGPU_GEM_DOMAIN_GTT;
+		set_domain = AMDGPU_GEM_DOMAIN_GTT;
+
+	bp.domain = (!vm->root.bo) ? 0 : set_domain;
 
 	bp.domain = amdgpu_bo_get_preferred_domain(adev, bp.domain);
 	bp.flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS |

drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c int amdgpu_vm_pt_create(struct amdgpu_device *adev, struct amdgpu_vm *vm, · line 499

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c +4 -0
@@ -490,6 +493,12 @@ int amdgpu_vm_pt_create(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 	if (r)
 		return r;
 
+	if (!vm->root.bo) {
+		(*vmbo)->bo.preferred_domains = set_domain;
+		(*vmbo)->bo.allowed_domains = (set_domain == AMDGPU_GEM_DOMAIN_VRAM) ?
+			AMDGPU_GEM_DOMAIN_VRAM | AMDGPU_GEM_DOMAIN_GTT : set_domain;

That's actually not correct. This should be VRAM only on dGPUs. E.g. just set_domain.

Where have you seen that?

Regards,
Christian.

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c +2 -0
+	}
+
 	/* Assumes that reservation is shared with the VM root and that the
 	 * reservation is locked
 	 */
#6 lore raw

Re: [PATCH v4 2/3] drm/amdgpu: defer amdgpu_vm_pt_clear() to validation

Quoted 86 lines · Christian König
Quoted 75 lines · Shahyan Soltani

Currently amdgpu_vm_pt_clear() is called at open time in amdgpu_vm_init(). It submits an SDMA job, which needs the GPU to be awake and adds complications when it comes to preventing the GPU from resuming when direct hardware access isn't required. Instead, amdgpu_vm_pt_clear() can be moved out of amdgpu_vm_init() to run later on when it's required, such as when amdgpu_vm_validate() is called.

Introduce delayed virtual memory hardware initialization by changing the vm->generation assignment on amdgpu_vm_init() to ~0ULL. By setting vm->generation to this value, it will force a mismatch to occur on init, processing the amdgpu_vm_validate() logic, and clearing the root PT much later on in the stack.

Set the initial domain of vm->root.bo to 0, this will allow all contents of the root PT to exist in system memory without requiring the GPU to be active. The vm->root.bo is then put on the evicted list, which will then migrate the PT to its preferred/allowed domain at the best possible time (usually through amdgpu_vm_validate() on the CS path).

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c    |  5 +----
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c | 13 +++++++++++--
  2 files changed, 12 insertions(+), 6 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1 -4
index 2f47a9c29c5f..437018c95683 100644
@@ -2678,6 +2678,7 @@ int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 
  	vm->is_compute_context = false;
  	vm->need_tlb_fence = amdgpu_userq_enabled(&adev->ddev);
+	vm->generation = ~0ULL;
 
  	vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
  				    AMDGPU_VM_USE_CPU_FOR_GFX);
@@ -2720,10 +2721,6 @@ int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  	if (r)
  		goto error_free_root;
 
-	r = amdgpu_vm_pt_clear(adev, vm, root, false);
-	if (r)
-		goto error_free_root;
-
  	r = amdgpu_vm_create_task_info(vm);
  	if (r)
  		dev_dbg(adev->dev, "Failed to create task info for VM\n");
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c +5 -2
index 8f1e0bc7d65a..7c835545419d 100644
@@ -453,6 +453,7 @@ int amdgpu_vm_pt_create(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  {
  	struct amdgpu_bo_param bp;
  	unsigned int num_entries;
+	u32 set_domain;
  	int r;
 
  	memset(&bp, 0, sizeof(bp));
@@ -461,9 +462,11 @@ int amdgpu_vm_pt_create(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  	bp.byte_align = AMDGPU_GPU_PAGE_SIZE;
 
  	if (!adev->gmc.is_app_apu)
-		bp.domain = AMDGPU_GEM_DOMAIN_VRAM;
+		set_domain = AMDGPU_GEM_DOMAIN_VRAM;
  	else
-		bp.domain = AMDGPU_GEM_DOMAIN_GTT;
+		set_domain = AMDGPU_GEM_DOMAIN_GTT;
+
+	bp.domain = (!vm->root.bo) ? 0 : set_domain;
 
  	bp.domain = amdgpu_bo_get_preferred_domain(adev, bp.domain);
  	bp.flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS |

drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c int amdgpu_vm_pt_create(struct amdgpu_device *adev, struct amdgpu_vm *vm, · line 499

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c +4 -0
@@ -490,6 +493,12 @@ int amdgpu_vm_pt_create(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  	if (r)
  		return r;
 
+	if (!vm->root.bo) {
+		(*vmbo)->bo.preferred_domains = set_domain;
+		(*vmbo)->bo.allowed_domains = (set_domain == AMDGPU_GEM_DOMAIN_VRAM) ?
+			AMDGPU_GEM_DOMAIN_VRAM | AMDGPU_GEM_DOMAIN_GTT : set_domain;

That's actually not correct. This should be VRAM only on dGPUs. E.g. just set_domain.

Quoted 5 lines

Where have you seen that?

Regards,
Christian.

Hi Christian,

I read that within amdgpu_bo_create(), more specifically the part that sets "bo->allowed_domains |= AMDGPU_GEM_DOMAIN_GTT;". However, it seems that I completely missed/misread the conditional checks for it that requires the ttm bo type to be of kernel and of the discardable flag not being set.

I'll fix that in v5 as well as update the commit description for patch 3 to match the UVD/VCN pm_runtime_get_if_active() addition.

Thanks,
Shahyan

Quoted 6 lines
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c +2 -0
+	}
+
  	/* Assumes that reservation is shared with the VM root and that the
  	 * reservation is locked
  	 */
#7 lore raw

PATCH v4 3/3 drm/amdgpu: remove pm_runtime from open/close kms paths

In reply to Shahyan Soltani

Every open system call for an amdgpu device holds a runtime reference and resumes the device from a BACO/BOCO sleep state even if direct hardware access is not required.

Drop pm_runtime_get_sync() and pm_runtime_put_autosuspend() from open/close KMS to prevent unnecessary resumes.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c | 34 ++++++++++++-------------
 1 file changed, 16 insertions(+), 18 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c +16 -18
index d7872acb4d79..9e0e5892fffa 100644
@@ -1506,7 +1506,6 @@ int amdgpu_driver_open_kms(struct drm_device *dev, struct drm_file *file_priv)
 	/* Ensure IB tests are run on ring */
 	flush_delayed_work(&adev->delayed_init_work);
 
-
 	if (amdgpu_ras_intr_triggered()) {
 		DRM_ERROR("RAS Intr triggered, device disabled!!");
 		return -EHWPOISON;
@@ -1514,14 +1513,10 @@ int amdgpu_driver_open_kms(struct drm_device *dev, struct drm_file *file_priv)
 
 	file_priv->driver_priv = NULL;
 
-	r = pm_runtime_get_sync(dev->dev);
-	if (r < 0)
-		goto pm_put;
-
 	fpriv = kzalloc(sizeof(*fpriv), GFP_KERNEL);
 	if (unlikely(!fpriv)) {
 		r = -ENOMEM;
-		goto out_suspend;
+		goto out;
 	}
 
 	r = amdgpu_xcp_open_device(adev, fpriv, file_priv);
@@ -1583,7 +1578,7 @@ int amdgpu_driver_open_kms(struct drm_device *dev, struct drm_file *file_priv)
 	amdgpu_ctx_mgr_init(&fpriv->ctx_mgr, adev);
 
 	file_priv->driver_priv = fpriv;
-	goto out_suspend;
+	goto out;
 
 error_vm:
 	if (pasid) {
@@ -1596,10 +1591,7 @@ int amdgpu_driver_open_kms(struct drm_device *dev, struct drm_file *file_priv)
 error_pasid:
 	kfree(fpriv);
 
-out_suspend:
-pm_put:
-	pm_runtime_put_autosuspend(dev->dev);
-
+out:
 	return r;
 }
 
@@ -1624,12 +1616,20 @@ void amdgpu_driver_postclose_kms(struct drm_device *dev,
 	if (!fpriv)
 		return;
 
-	pm_runtime_get_sync(dev->dev);
+	bool has_uvd = (amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_UVD) != NULL);
+	bool has_vce = (amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_VCE) != NULL);
 
-	if (amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_UVD) != NULL)
-		amdgpu_uvd_free_handles(adev, file_priv);
-	if (amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_VCE) != NULL)
-		amdgpu_vce_free_handles(adev, file_priv);
+	if (has_uvd || has_vce) {
+		int r = pm_runtime_get_if_active(dev->dev);
+
+		if (has_uvd)
+			amdgpu_uvd_free_handles(adev, file_priv);
+		if (has_vce)
+			amdgpu_vce_free_handles(adev, file_priv);
+
+		if (r > 0)
+			pm_runtime_put_autosuspend(dev->dev);
+	}
 
 	if (fpriv->csa_va) {
 		uint64_t csa_addr = amdgpu_csa_vaddr(adev) & AMDGPU_GMC_HOLE_MASK;
@@ -1662,8 +1662,6 @@ void amdgpu_driver_postclose_kms(struct drm_device *dev,
 
 	kfree(fpriv);
 	file_priv->driver_priv = NULL;
-
-	pm_runtime_put_autosuspend(dev->dev);
 }
 
 
Signature · 1 line
2.56.0