Skip to contentSkip to conversation

drm/vblank: Deferred Enable and Disable

PATCH Superseded [email protected] 2 participants 7 messages Feb 24 – Feb 25

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

[email protected]

author maintainer
#0 lore raw

cover drm/vblank: Deferred Enable and Disable

From: Leo Li <[email protected]>

# Abstract

The purpose of this series is to introduce a way for drivers to do blocking work as part of their vblank enable/disable callbacks. Today, this isn't possible since drm_crtc_funcs->(enable|disable)_vblank() can be called from atomic context. The proposed solution is to defer drm_vblank_enable() and vblank_disable_fn() to a workqueue from within the DRM vblank core, along with introducing new crtc callbacks where drivers can do blocking work.

A drm-misc-next based branch with this series applied is available here:
https://gitlab.freedesktop.org/leoli/drm-misc/-/commits/drm-misc-next

# Considerations

Why not defer within the driver callbacks? It can introduce concurrency between the deferred work, and access to HW upon the callback's return. See drm_vblank_enable()'s call to drm_update_vblank_count() that reads the HW vblank counter and scanout position, for example. If the underlying HW remains accessible when vblanks are disabled, then this wouldn't be an issue. But that's not always the case (amdgpu_dm being an example, see patch 4/5's commit description). One may be tempted to spin-wait on the driver's enable worker to complete from the callbacks that access HW, (*)but waiting on a deferred process-context worker while possibly in atomic context is not ideal.

Doesn't deferring from the DRM vblank core have the same issue? Yes and no (and here is where I think some additional review is required):

Since the entirety of drm_vblank_enable/disable_and_save() is deferred, access to HW counter and scanout position from within is synchronized with the enable/disable callbacks. However, it is still possible for the caller of drm_vblank_get() to run concurrently with the deferred enable worker that it scheduled. This is not an issue with vblank_put(), since HW is already in an enabled state, and it's OK for HW to disabled a little later.

In cases where the vblank_getter wants to ensure that vblank counts will increment (e.g. for waiting on a specific vblank), this shouldn't be an issue:
HW vblanks will be enabled eventually, and the counter will progress (albeit with some additional delay). This seems the case for all vblank_get() calls from within DRM core, with one exception addressed in patch 1/5.

But if the vblank_getter requires HW to be enabled upon return (e.g. programming something that depends on HW being enabled), a new drm_crtc_vblank_wait_deferred_enable() helper is provided. Drivers can use it to wait for the enable work to complete before proceeding. This doesn't have the same concern as (*) above, since wait_deferred_enable() must run from process context. And if the driver needs deferred enable/disable work, it sounds reasonable to ask it to also do such work from process context.

# Some more context

This is actually a redo of a previous attempt to introduce a "vblank_prepare()" driver callback that the DRM core calls prior to entering atomic context. It was dropped because it has synchronization issues -- there was nothing preventing a previous vblank_put() from "undoing" the prepare work while there is a pending vblank_get() (IOW vblank_prepare() and vblank_disable() runs concurrently)(*). It also asks all callers of vblank_get() to be vblank_prepare()-aware, which isn't great. This series can be found here:

https://lore.kernel.org/dri-devel/[email protected]/

(*) With the pre/post_enable/disable() callbacks for deferred vblank
enable/disable, drivers can acquire/release their own locks, as long as they're properly paired.

Leo Li (5):
drm/vblank: Add drm_crtc_vblank_is_off() helper drm/vblank: Introduce deferred vblank enable/disable drm/amd/display: Refactor amdgpu_dm_crtc_set_vblank drm/amd/display: Implement deferred vblanks on IPS platforms drm/vblank: Add some debugging trace events

 .../gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c |   8 +
 .../amd/display/amdgpu_dm/amdgpu_dm_crtc.c    | 263 ++++++++++++++++--
 .../amd/display/amdgpu_dm/amdgpu_dm_crtc.h    |   4 -
 drivers/gpu/drm/drm_atomic_helper.c           |  11 +-
 drivers/gpu/drm/drm_drv.c                     |   5 +
 drivers/gpu/drm/drm_internal.h                |   1 +
 drivers/gpu/drm/drm_trace.h                   | 112 ++++++++
 drivers/gpu/drm/drm_vblank.c                  | 227 ++++++++++++++-
 include/drm/drm_crtc.h                        |  34 +++
 include/drm/drm_device.h                      |   6 +
 include/drm/drm_vblank.h                      |  20 ++
 11 files changed, 642 insertions(+), 49 deletions(-)
Signature · 1 line
2.52.0
author maintainer
#1 lore raw

PATCH 1/5 drm/vblank: Add drm_crtc_vblank_is_off() helper

From: Leo Li <[email protected]>

Add and use an drm internal helper to check if vblanks have been turned off (via drm_crtc_vblank_off()) for a CRTC, rather than relying on the return value of drm_crtc_vblank_get().

This is in preparation of introducing deferred vblank enable/disable, as vblank_get() will not return the expected error code when the driver defers vblank enable.

No functional change is intended.

---
 drivers/gpu/drm/drm_atomic_helper.c | 11 +++++------
 drivers/gpu/drm/drm_internal.h      |  1 +
 drivers/gpu/drm/drm_vblank.c        | 17 +++++++++++++++++
 3 files changed, 23 insertions(+), 6 deletions(-)
drivers/gpu/drm/drm_atomic_helper.c +5 -6
index 26953ed6b53e8..102937e3ff35b 100644
@@ -48,6 +48,7 @@
 
 #include "drm_crtc_helper_internal.h"
 #include "drm_crtc_internal.h"
+#include "drm_internal.h"
 
 /**
  * DOC: overview
@@ -1259,7 +1260,7 @@ drm_atomic_helper_commit_crtc_disable(struct drm_device *dev, struct drm_atomic_
 
 	for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
 		const struct drm_crtc_helper_funcs *funcs;
-		int ret;
+		bool vblank_off;
 
 		/* Shut down everything that needs a full modeset. */
 		if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
@@ -1287,19 +1288,17 @@ drm_atomic_helper_commit_crtc_disable(struct drm_device *dev, struct drm_atomic_
 		if (!drm_dev_has_vblank(dev))
 			continue;
 
-		ret = drm_crtc_vblank_get(crtc);
+		vblank_off = drm_crtc_vblank_is_off(crtc);
 		/*
 		 * Self-refresh is not a true "disable"; ensure vblank remains
 		 * enabled.
 		 */
 		if (new_crtc_state->self_refresh_active)
-			WARN_ONCE(ret != 0,
+			WARN_ONCE(vblank_off,
 				  "driver disabled vblank in self-refresh\n");
 		else
-			WARN_ONCE(ret != -EINVAL,
+			WARN_ONCE(!vblank_off,
 				  "driver forgot to call drm_crtc_vblank_off()\n");
-		if (ret == 0)
-			drm_crtc_vblank_put(crtc);
 	}
 }
 EXPORT_SYMBOL(drm_atomic_helper_commit_crtc_disable);
drivers/gpu/drm/drm_internal.h +1 -0
index f893b1e3a596e..7d10515fe2ed5 100644
@@ -131,6 +131,7 @@ static inline void drm_vblank_destroy_worker(struct drm_vblank_crtc *vblank)
 int drm_vblank_worker_init(struct drm_vblank_crtc *vblank);
 void drm_vblank_cancel_pending_works(struct drm_vblank_crtc *vblank);
 void drm_handle_vblank_works(struct drm_vblank_crtc *vblank);
+bool drm_crtc_vblank_is_off(struct drm_crtc *crtc);
 
 /* IOCTLS */
 int drm_wait_vblank_ioctl(struct drm_device *dev, void *data,
drivers/gpu/drm/drm_vblank.c +17 -0
index f78bf37f1e0a7..983c131b23694 100644
@@ -2318,3 +2318,20 @@ void drm_crtc_vblank_get_vblank_timeout(struct drm_crtc *crtc, ktime_t *vblank_t
 	*vblank_time = ktime_sub(*vblank_time, vtimer->interval);
 }
 EXPORT_SYMBOL(drm_crtc_vblank_get_vblank_timeout);
+
+/**
+ * drm_crtc_vblank_is_off - check if vblank is off on a CRTC
+ * @crtc: CRTC in question
+ *
+ * Return true if vblanks is currently turned off via drm_crtc_vblank_off().
+ * Return False otherwise.
+ */
+bool drm_crtc_vblank_is_off(struct drm_crtc *crtc)
+{
+	struct drm_vblank_crtc *vblank = drm_crtc_vblank_crtc(crtc);
+
+	/* Spinlock adds the necessary barriers */
+	guard(spinlock_irqsave)(&crtc->dev->vbl_lock);
+	return vblank->inmodeset && !vblank->enabled;
+}
+EXPORT_SYMBOL(drm_crtc_vblank_is_off);
Signature · 1 line
2.52.0
author maintainer
#2 lore raw

PATCH 2/5 drm/vblank: Introduce deferred vblank enable/disable

In reply to [email protected]

From: Leo Li <[email protected]>

Some drivers need to do blocking work as part of enabling/disabling their vblank reporting mechanism in hardware(HW). Acquiring a mutex, for example. However, the driver callbacks can be called from atomic context, and therefore cannot block.

One solution is to have drivers defer their blocking work from their enable/disable callbacks However, it can introduce concurrency between the deferred work, and access to HW upon the callback's return. For example, see drm_vblank_enable()'s call to drm_update_vblank_count() that reads the HW vblank counter and scanout position immediately after the driver callback returns. If the underlying HW remains accessible when vblanks are disabled, then this wouldn't be an issue, but that's not always the case.

This patch introduces a new per-device workqueue for deferring drm_vblank_enable/disable_and_save(). Drivers can advertise their need for deferred vblank by implementing the new &drm_crtc_funcs->(pre|post)_(enable|disable)_vblank() callbacks, in which they can do blocking work. DRM vblank will only defer if one or more of these callbacks are implemented. Otherwise, the existing non-deferred path will be used.

With deferred vblank enable, it is still possible that HW vblanks are not ready by the time drm_vblank_get() returns. In cases where the caller wants to ensure that vblank counts will increment (e.g. for waiting on a specific vblank), this shouldn't be an issue: HW vblanks will be enabled eventually, and the counter will progress (albeit with some additional delay). But if the caller requires HW to be enabled upon return (e.g. programming something that depends on HW being active), a new drm_crtc_vblank_wait_deferred_enable() helper is provided. Drivers can use it to wait for the enable work to complete before proceeding.

The &drm_crtc_funcs->(pre|post)_(enable|disable)_vblank() are also inserted into drm_vblank_on and off(), as they call drm_vblank_enable/disable(). I don't see a case where they're called from atomic context, hence why they're not deferred. If that turns out to be wrong (for a driver that needs to defer), we can change enable/disable to be deferred there as well.

---
 drivers/gpu/drm/drm_drv.c    |   5 +
 drivers/gpu/drm/drm_vblank.c | 188 +++++++++++++++++++++++++++++++++--
 include/drm/drm_crtc.h       |  34 +++++++
 include/drm/drm_device.h     |   6 ++
 include/drm/drm_vblank.h     |  20 ++++
 5 files changed, 242 insertions(+), 11 deletions(-)
drivers/gpu/drm/drm_drv.c +5 -0
index 2915118436ce8..84884231f1f0e 100644
@@ -696,6 +696,11 @@ static void drm_dev_init_release(struct drm_device *dev, void *res)
 	mutex_destroy(&dev->master_mutex);
 	mutex_destroy(&dev->clientlist_mutex);
 	mutex_destroy(&dev->filelist_mutex);
+
+	if (dev->deferred_vblank_wq) {
+		flush_workqueue(dev->deferred_vblank_wq);
+		destroy_workqueue(dev->deferred_vblank_wq);
+	}
 }
 
 static int drm_dev_init(struct drm_device *dev,
drivers/gpu/drm/drm_vblank.c +177 -11

282 lines not on this page · raw message

include/drm/drm_crtc.h +34 -0
index 66278ffeebd68..1088b63b64816 100644
@@ -892,6 +892,40 @@ struct drm_crtc_funcs {
 	 */
 	void (*disable_vblank)(struct drm_crtc *crtc);
 
+	/**
+	 * @pre_enable_vblank:
+	 *
+	 * Optional callback to do blocking work prior to vblank_enable(). If
+	 * set, vblank enable/disable will be deferred to a single-threaded
+	 * worker.
+	 */
+	void (*pre_enable_vblank)(struct drm_crtc *crtc);
+
+	/**
+	 * @post_enable_vblank:
+	 *
+	 * Optional callback to do blocking work after vblank_enable(). If set,
+	 * vblank enable/disable will be deferred to a single-threaded worker.
+	 */
+	void (*post_enable_vblank)(struct drm_crtc *crtc);
+
+	/**
+	 * @pre_disable_vblank:
+	 *
+	 * Optional callback to do blocking work prior to vblank_disable(). If
+	 * set, vblank enable/disable will be deferred to a single-threaded
+	 * worker.
+	 */
+	void (*pre_disable_vblank)(struct drm_crtc *crtc);
+
+	/**
+	 * @post_disable_vblank:
+	 *
+	 * Optional callback to do blocking work after vblank_disable(). If set,
+	 * vblank enable/disable will be deferred to a single-threaded worker.
+	 */
+	void (*post_disable_vblank)(struct drm_crtc *crtc);
+
 	/**
 	 * @get_vblank_timestamp:
 	 *
include/drm/drm_device.h +6 -0
index bc78fb77cc279..c0e22f8d868b2 100644
@@ -333,6 +333,12 @@ struct drm_device {
 	 */
 	spinlock_t event_lock;
 
+	/**
+	 * @deferred_vblank_wq: Workqueue used for deferred vblank
+	 * enable/disable work.
+	 */
+	struct workqueue_struct *deferred_vblank_wq;
+
 	/** @num_crtcs: Number of CRTCs on this device */
 	unsigned int num_crtcs;
 
include/drm/drm_vblank.h +20 -0
index 2fcef9c0f5b1b..6bd3cbf1bb831 100644
@@ -282,6 +282,25 @@ struct drm_vblank_crtc {
 	 * @vblank_timer: Holds the state of the vblank timer
 	 */
 	struct drm_vblank_crtc_timer vblank_timer;
+
+	/**
+	 * @enable_work: Deferred enable work for this vblank CRTC
+	 */
+	struct work_struct enable_work;
+
+	/**
+	 * @disable_work: Delayed disable work for this vblank CRTC
+	 */
+	struct delayed_work disable_work;
+
+	/**
+	 * @enable_done: Signals completion of deferred vblank enable
+	 *
+	 * If deferred enable work is needed, it is reinitialized before
+	 * queueing the enable worker, and completed after the deferred
+	 * drm_vblank_enable() completes.
+	 */
+	struct completion enable_done;
 };
 
 struct drm_vblank_crtc *drm_crtc_vblank_crtc(struct drm_crtc *crtc);
@@ -302,6 +321,7 @@ bool drm_handle_vblank(struct drm_device *dev, unsigned int pipe);
 bool drm_crtc_handle_vblank(struct drm_crtc *crtc);
 int drm_crtc_vblank_get(struct drm_crtc *crtc);
 void drm_crtc_vblank_put(struct drm_crtc *crtc);
+void drm_crtc_vblank_wait_deferred_enable(struct drm_crtc *crtc);
 int drm_crtc_wait_one_vblank(struct drm_crtc *crtc);
 void drm_crtc_vblank_off(struct drm_crtc *crtc);
 void drm_crtc_vblank_reset(struct drm_crtc *crtc);
Signature · 1 line
2.52.0
#3 lore raw

Re: [PATCH 2/5] drm/vblank: Introduce deferred vblank enable/disable

kernel test robot noticed the following build warnings:

Hi,

kernel test robot noticed the following build warnings:

[auto build test WARNING on drm-misc/drm-misc-next]
[also build test WARNING on daeinki-drm-exynos/exynos-drm-next drm/drm-next drm-i915/for-linux-next-fixes drm-tip/drm-tip linus/master v7.0-rc1 next-20260224] [cannot apply to drm-i915/for-linux-next]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/sunpeng-li-amd-com/drm-vblank-Add-drm_crtc_vblank_is_off-helper/20260225-052945
base:   https://gitlab.freedesktop.org/drm/misc/kernel.git drm-misc-next
patch link:    https://lore.kernel.org/r/20260224212639.390768-3-sunpeng.li%40amd.com
patch subject: [PATCH 2/5] drm/vblank: Introduce deferred vblank enable/disable
config: microblaze-randconfig-r053-20260225 (https://download.01.org/0day-ci/archive/20260225/[email protected]/config)
compiler: microblaze-linux-gcc (GCC) 8.5.0

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

cocci warnings: (new ones prefixed by >>)

>> drivers/gpu/drm/drm_vblank.c:1402:6-25: WARNING: atomic_dec_and_test variation before object free at line 1409.

drivers/gpu/drm/drm_vblank.c:1402:6-25: WARNING: atomic_dec_and_test variation before object free at line 1416.

vim +1402 drivers/gpu/drm/drm_vblank.c

1385
1386 void drm_vblank_put(struct drm_device *dev, unsigned int pipe)
1387 {
1388 struct drm_vblank_crtc *vblank = drm_vblank_crtc(dev, pipe);
1389 int vblank_offdelay = vblank->config.offdelay_ms;
1390 bool needs_deferred_disable;
1391
1392 if (drm_WARN_ON(dev, pipe >= dev->num_crtcs))
1393 return;
1394
1395 if (drm_WARN_ON(dev, atomic_read(&vblank->refcount) == 0))
1396 return;
1397
1398 needs_deferred_disable =
1399 drm_crtc_needs_deferred_vblank(drm_crtc_from_index(dev, pipe));
1400
1401 /* Last user schedules interrupt disable */

> 1402		if (!atomic_dec_and_test(&vblank->refcount))
  1403			return;
  1404	
  1405		if (!vblank_offdelay)
  1406			return;
  1407		else if (vblank_offdelay < 0) {
  1408			if (needs_deferred_disable)
> 1409				mod_delayed_work(dev->deferred_vblank_wq,

1410 &vblank->disable_work,
1411 0);
1412 else
1413 vblank_disable_fn(&vblank->disable_timer);
1414 } else if (!vblank->config.disable_immediate) {
1415 if (needs_deferred_disable)
1416 mod_delayed_work(dev->deferred_vblank_wq,
1417 &vblank->disable_work,
1418 msecs_to_jiffies(vblank_offdelay));
1419 else
1420 mod_timer(&vblank->disable_timer,
1421 jiffies + ((vblank_offdelay * HZ) / 1000));
1422 }
1423 }
1424

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
author maintainer
#4 lore raw

PATCH 3/5 drm/amd/display: Refactor amdgpu_dm_crtc_set_vblank

In reply to [email protected]

From: Leo Li <[email protected]>

[Why]

In preparation of implementing deferred vblank work, refactor amdgpu_dm_crtc_set_vblank to split out the actual enabling/disabling of vblank interrupts + restoring vblank counts/timestamps from the rest.

[How]

* Move the vblank_control_work init and queueing out into amdgpu_dm_crtc_queue_vblank_work()
* Move crtc->enabled check out to parent function amdgpu_dm_crtc_enable_vblank()
* Call amdgpu_dm_crtc_queue_vblank_work() from parent functions amdgpu_dm_crtc_(enable|disable)_vblank()
* As a drive-by cleanup, make amdgpu_dm_crtc_(enable|disable)_vblank() static; they're not called from anywhere else.

No functional changes are intended.

---
 .../amd/display/amdgpu_dm/amdgpu_dm_crtc.c    | 88 +++++++++++++------
 .../amd/display/amdgpu_dm/amdgpu_dm_crtc.h    |  4 -
 2 files changed, 60 insertions(+), 32 deletions(-)
drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c +60 -28
index 130190e8a1b29..3df38f3cb7423 100644
@@ -293,20 +293,12 @@ static inline int amdgpu_dm_crtc_set_vblank(struct drm_crtc *crtc, bool enable)
 	struct amdgpu_device *adev = drm_to_adev(crtc->dev);
 	struct dm_crtc_state *acrtc_state = to_dm_crtc_state(crtc->state);
 	struct amdgpu_display_manager *dm = &adev->dm;
-	struct vblank_control_work *work;
 	int irq_type;
 	int rc = 0;
 
-	if (enable && !acrtc->base.enabled) {
-		drm_dbg_vbl(crtc->dev,
-				"Reject vblank enable on unconfigured CRTC %d (enabled=%d)\n",
-				acrtc->crtc_id, acrtc->base.enabled);
-		return -EINVAL;
-	}
-
 	irq_type = amdgpu_display_crtc_idx_to_irq_type(adev, acrtc->crtc_id);
 
-	if (enable) {
+	if (enable && crtc->enabled) {
 		struct dc *dc = adev->dm.dc;
 		struct drm_vblank_crtc *vblank = drm_crtc_vblank_crtc(crtc);
 		struct psr_settings *psr = &acrtc_state->stream->link->psr_settings;
@@ -387,39 +379,79 @@ static inline int amdgpu_dm_crtc_set_vblank(struct drm_crtc *crtc, bool enable)
 			return rc;
 	}
 #endif
+	return 0;
+}
 
-	if (amdgpu_in_reset(adev))
-		return 0;
-
-	if (dm->vblank_control_workqueue) {
-		work = kzalloc_obj(*work, GFP_ATOMIC);
-		if (!work)
-			return -ENOMEM;
+static int amdgpu_dm_crtc_queue_vblank_work(struct amdgpu_display_manager *dm,
+					    struct amdgpu_crtc *acrtc,
+					    struct dm_crtc_state *acrtc_state,
+					    bool enable)
+{
+	struct vblank_control_work *work;
 
-		INIT_WORK(&work->work, amdgpu_dm_crtc_vblank_control_worker);
-		work->dm = dm;
-		work->acrtc = acrtc;
-		work->enable = enable;
+	work = kzalloc_obj(*work, GFP_ATOMIC);
+	if (!work)
+		return -ENOMEM;
 
-		if (acrtc_state->stream) {
-			dc_stream_retain(acrtc_state->stream);
-			work->stream = acrtc_state->stream;
-		}
+	INIT_WORK(&work->work, amdgpu_dm_crtc_vblank_control_worker);
+	work->dm = dm;
+	work->acrtc = acrtc;
+	work->enable = enable;
 
-		queue_work(dm->vblank_control_workqueue, &work->work);
+	if (acrtc_state->stream) {
+		dc_stream_retain(acrtc_state->stream);
+		work->stream = acrtc_state->stream;
 	}
 
+	queue_work(dm->vblank_control_workqueue, &work->work);
+
 	return 0;
 }
 
-int amdgpu_dm_crtc_enable_vblank(struct drm_crtc *crtc)
+static int amdgpu_dm_crtc_enable_vblank(struct drm_crtc *crtc)
 {
-	return amdgpu_dm_crtc_set_vblank(crtc, true);
+	struct dm_crtc_state *acrtc_state = to_dm_crtc_state(crtc->state);
+	struct amdgpu_device *adev = drm_to_adev(crtc->dev);
+	struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
+	struct amdgpu_display_manager *dm = &adev->dm;
+	int ret;
+
+	if (!crtc->enabled) {
+		drm_dbg_vbl(crtc->dev,
+				"Reject vblank enable on unconfigured CRTC %d (enabled=%d)\n",
+				crtc->index, crtc->enabled);
+		return -EINVAL;
+	}
+
+	ret = amdgpu_dm_crtc_set_vblank(crtc, true);
+	if (ret)
+		return ret;
+
+	if (amdgpu_in_reset(adev))
+		return 0;
+
+	if (dm->vblank_control_workqueue)
+		return amdgpu_dm_crtc_queue_vblank_work(dm, acrtc,
+							acrtc_state, true);
+
+	return 0;
 }
 
-void amdgpu_dm_crtc_disable_vblank(struct drm_crtc *crtc)
+static void amdgpu_dm_crtc_disable_vblank(struct drm_crtc *crtc)
 {
+	struct dm_crtc_state *acrtc_state = to_dm_crtc_state(crtc->state);
+	struct amdgpu_device *adev = drm_to_adev(crtc->dev);
+	struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
+	struct amdgpu_display_manager *dm = &adev->dm;
+
 	amdgpu_dm_crtc_set_vblank(crtc, false);
+
+	if (amdgpu_in_reset(adev))
+		return;
+
+	if (dm->vblank_control_workqueue)
+		amdgpu_dm_crtc_queue_vblank_work(dm, acrtc,
+						 acrtc_state, false);
 }
 
 static void amdgpu_dm_crtc_destroy_state(struct drm_crtc *crtc,
drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.h +0 -4
index c1212947a77b8..655a6c4f83fb8 100644
@@ -39,10 +39,6 @@ bool amdgpu_dm_crtc_vrr_active_irq(struct amdgpu_crtc *acrtc);
 
 bool amdgpu_dm_crtc_vrr_active(const struct dm_crtc_state *dm_state);
 
-int amdgpu_dm_crtc_enable_vblank(struct drm_crtc *crtc);
-
-void amdgpu_dm_crtc_disable_vblank(struct drm_crtc *crtc);
-
 int amdgpu_dm_crtc_init(struct amdgpu_display_manager *dm,
 			struct drm_plane *plane,
 			uint32_t link_index);
Signature · 1 line
2.52.0
author maintainer
#5 lore raw

PATCH 4/5 drm/amd/display: Implement deferred vblanks on IPS platforms

In reply to [email protected]

From: Leo Li <[email protected]>

[Why]

Newer generations of Display Core Next (DCN) hardware allow for the entire DCN block to be power-gated in a feature called Idle Power States (IPS). Once DCN is in IPS, HW register access will timeout. Therefore, allowing/disallowing IPS requires synchronization with the rest of the driver through the big dc_lock mutex. It happens that writing vblank interrupt control and reading counter/scanout position registers requires IPS disallow. Since that requires the dc_lock, it turns into a blocking operation.

The immediately obvious (not)solution is to disable IPS. But since it provides sizable power savings, and it's not going away for future APUs, it's not a good option.

The other (non)solution is to get rid of the dc_lock or make it not block. But since all hardware (HW) and Display Micro-controller (DMCU) access is invalid when DCN is in IPS (because everything is gated), it needs to be synchronized with all other HW/DMCU access. It happens that the dc_lock is responsible for that. I don't think replacing it with a spinlock is a valid solution either. Consider link training: IPS cannot be allowed while that is happening, yet the timescale of link training can be in the 100ms. A spinlock would spin too long here.

So let's implement the newly introduced deferred vblank enable/disable callbacks in DRM vblank core.

[How]

Implement the deferred CRTC callbacks to request the DRM vblank core to defer vblank enable/disable. Only implement for ASICs that have IPS support. Otherwise, keep the non-deferred behavior.

To ensure interrupts are enabled before programming flips (otherwise the page flip interrupt for event delivery may not fire), call drm_crtc_vblank_wait_deferred_enable() in the commit_planes code when getting a reference on vblanks.

---
 .../gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c |   8 +
 .../amd/display/amdgpu_dm/amdgpu_dm_crtc.c    | 177 +++++++++++++++++-
 2 files changed, 184 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c +8 -0
index a8e4e3ab5e402..0405466666e2f 100644
@@ -10268,6 +10268,14 @@ static void amdgpu_dm_commit_planes(struct drm_atomic_state *state,
 				&acrtc_attach->dm_irq_params.vrr_params.adjust);
 			spin_unlock_irqrestore(&pcrtc->dev->event_lock, flags);
 		}
+
+		/*
+		 * If vblank is being enabled in worker thread, wait for it to
+		 * enable interrupts before programming pflips, otherwise the
+		 * interrupt may not fire.
+		 */
+		drm_crtc_vblank_wait_deferred_enable(pcrtc);
+
 		mutex_lock(&dm->dc_lock);
 		update_planes_and_stream_adapter(dm->dc,
 					 acrtc_state->update_type,
drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c +176 -1

206 lines not on this page · raw message

Signature · 1 line
2.52.0
author maintainer
#6 lore raw

PATCH 5/5 drm/vblank: Add some debugging trace events

In reply to [email protected]

From: Leo Li <[email protected]>

It's useful to trace vblank get/put and enable/disable (plus their deferred variants) when debugging. The trace stack feature for events can be especially useful.

Using trace-cmd, one can obtain a trace like so:

trace-cmd record -e drm_vblank*
# With deferred events and stack info:
trace-cmd record -e drm_vblank* -e drm_deferred_vblank* -T

---
 drivers/gpu/drm/drm_trace.h  | 112 +++++++++++++++++++++++++++++++++++
 drivers/gpu/drm/drm_vblank.c |  34 +++++++++--
 2 files changed, 140 insertions(+), 6 deletions(-)
drivers/gpu/drm/drm_trace.h +112 -0
index 11c6dd577e8ed..3372513a10eeb 100644
@@ -66,6 +66,118 @@ TRACE_EVENT(drm_vblank_event_delivered,
 		      __entry->seq)
 );
 
+DECLARE_EVENT_CLASS(drm_vblank_get_put_template,
+		    TP_PROTO(int crtc, int refcount),
+		    TP_ARGS(crtc, refcount),
+		    TP_STRUCT__entry(
+			__field(int, crtc)
+			__field(int, refcount)
+		    ),
+		    TP_fast_assign(
+			__entry->crtc = crtc;
+			__entry->refcount = refcount;
+		    ),
+		    TP_printk(
+			"crtc=%d, refcount=%u",
+			__entry->crtc, __entry->refcount
+		    )
+);
+
+DEFINE_EVENT(drm_vblank_get_put_template, drm_vblank_get,
+	     TP_PROTO(int crtc, int refcount),
+	     TP_ARGS(crtc, refcount));
+
+/* put's refcount not sync'd using vbl_lock, use for debugging purposes only */
+DEFINE_EVENT(drm_vblank_get_put_template, drm_vblank_put,
+	     TP_PROTO(int crtc, int refcount),
+	     TP_ARGS(crtc, refcount));
+
+DECLARE_EVENT_CLASS(drm_vblank_on_off_template,
+		    TP_PROTO(int crtc, int refcount, bool enabled, bool inmodeset),
+		    TP_ARGS(crtc, refcount, enabled, inmodeset),
+		    TP_STRUCT__entry(
+			__field(int, crtc)
+			__field(int, refcount)
+			__field(bool, enabled)
+			__field(bool, inmodeset)
+		    ),
+		    TP_fast_assign(
+			__entry->crtc = crtc;
+			__entry->refcount = refcount;
+			__entry->enabled = enabled;
+			__entry->inmodeset = inmodeset;
+		    ),
+		    TP_printk(
+			"crtc=%d, refcount=%u, enabled=%s, inmodeset=%s",
+			__entry->crtc, __entry->refcount,
+			__entry->enabled ? "true" : "false",
+			__entry->inmodeset ? "true" : "false"
+		    )
+);
+
+DEFINE_EVENT(drm_vblank_on_off_template, drm_vblank_on,
+	     TP_PROTO(int crtc, int refcount, bool enabled, bool inmodeset),
+	     TP_ARGS(crtc, refcount, enabled, inmodeset));
+
+DEFINE_EVENT(drm_vblank_on_off_template, drm_vblank_off,
+	     TP_PROTO(int crtc, int refcount, bool enabled, bool inmodeset),
+	     TP_ARGS(crtc, refcount, enabled, inmodeset));
+
+DECLARE_EVENT_CLASS(drm_deferred_vblank_template,
+		    TP_PROTO(int crtc),
+		    TP_ARGS(crtc),
+		    TP_STRUCT__entry(
+			__field(int, crtc)
+		    ),
+		    TP_fast_assign(
+			__entry->crtc = crtc;
+		    ),
+		    TP_printk(
+			"crtc=%d",
+			__entry->crtc
+		    )
+);
+
+DEFINE_EVENT(drm_deferred_vblank_template, drm_deferred_vblank_enable_queued,
+	     TP_PROTO(int crtc),
+	     TP_ARGS(crtc));
+
+DEFINE_EVENT(drm_deferred_vblank_template, drm_deferred_vblank_enable,
+	     TP_PROTO(int crtc),
+	     TP_ARGS(crtc));
+
+TRACE_EVENT(drm_deferred_vblank_disable_queued,
+	    TP_PROTO(int crtc, int delay_ms),
+	    TP_ARGS(crtc, delay_ms),
+	    TP_STRUCT__entry(
+		__field(int, crtc)
+		__field(int, delay_ms)
+	    ),
+	    TP_fast_assign(
+		__entry->crtc = crtc;
+		__entry->delay_ms = delay_ms;
+	    ),
+	    TP_printk(
+		"crtc=%d, delay_ms=%d",
+		__entry->crtc,
+		__entry->delay_ms
+	    )
+);
+
+DEFINE_EVENT(drm_deferred_vblank_template, drm_deferred_vblank_disable,
+	     TP_PROTO(int crtc),
+	     TP_ARGS(crtc));
+
+DEFINE_EVENT(drm_deferred_vblank_template,
+	     drm_deferred_vblank_wait_enable_start,
+	     TP_PROTO(int crtc),
+	     TP_ARGS(crtc));
+
+DEFINE_EVENT(drm_deferred_vblank_template,
+	     drm_deferred_vblank_wait_enable_end,
+	     TP_PROTO(int crtc),
+	     TP_ARGS(crtc));
+
 #endif /* _DRM_TRACE_H_ */
 
 /* This part must be outside protection */
drivers/gpu/drm/drm_vblank.c +28 -6
index db17800f58e03..afa918c508bef 100644
@@ -1265,6 +1265,8 @@ static void drm_vblank_deferred_enable_worker(struct work_struct *work)
 	if (drm_WARN_ON(dev, !crtc))
 		return;
 
+	trace_drm_deferred_vblank_enable(crtc->index);
+
 	if (crtc->funcs->pre_enable_vblank)
 		crtc->funcs->pre_enable_vblank(crtc);
 
@@ -1294,6 +1296,8 @@ static void drm_vblank_deferred_disable_worker(struct work_struct *work)
 	if (drm_WARN_ON(dev, !crtc))
 		return;
 
+	trace_drm_deferred_vblank_disable(crtc->index);
+
 	if (crtc->funcs->pre_disable_vblank)
 		crtc->funcs->pre_disable_vblank(crtc);
 
@@ -1323,10 +1327,14 @@ void drm_crtc_vblank_wait_deferred_enable(struct drm_crtc *crtc)
 	if (!drm_crtc_needs_deferred_vblank(crtc))
 		return;
 
+	trace_drm_deferred_vblank_wait_enable_start(crtc->index);
+
 	if (!wait_for_completion_timeout(&vblank->enable_done,
 	    msecs_to_jiffies(1000)))
 		drm_err(crtc->dev, "CRTC-%d: Timed out waiting for deferred vblank enable\n",
 			 drm_crtc_index(crtc));
+
+	trace_drm_deferred_vblank_wait_enable_end(crtc->index);
 }
 EXPORT_SYMBOL(drm_crtc_vblank_wait_deferred_enable);
 
@@ -1347,11 +1355,15 @@ int drm_vblank_get(struct drm_device *dev, unsigned int pipe)
 		drm_crtc_needs_deferred_vblank(drm_crtc_from_index(dev, pipe));
 
 	spin_lock_irqsave(&dev->vbl_lock, irqflags);
+
+	trace_drm_vblank_get(pipe, atomic_read(&vblank->refcount));
+
 	/* Going from 0->1 means we have to enable interrupts again */
 	if (atomic_add_return(1, &vblank->refcount) == 1) {
 		if (needs_deferred_enable) {
 			/* Arm completion before queueing deferred enable */
 			reinit_completion(&vblank->enable_done);
+			trace_drm_deferred_vblank_enable_queued(pipe);
 			queue_work(dev->deferred_vblank_wq, &vblank->enable_work);
 		} else {
 			ret = drm_vblank_enable(dev, pipe);
@@ -1398,6 +1410,8 @@ void drm_vblank_put(struct drm_device *dev, unsigned int pipe)
 	needs_deferred_disable =
 		drm_crtc_needs_deferred_vblank(drm_crtc_from_index(dev, pipe));
 
+	trace_drm_vblank_put(pipe, atomic_read(&vblank->refcount));
+
 	/* Last user schedules interrupt disable */
 	if (!atomic_dec_and_test(&vblank->refcount))
 		return;
@@ -1405,18 +1419,21 @@ void drm_vblank_put(struct drm_device *dev, unsigned int pipe)
 	if (!vblank_offdelay)
 		return;
 	else if (vblank_offdelay < 0) {
-		if (needs_deferred_disable)
+		if (needs_deferred_disable) {
+			trace_drm_deferred_vblank_disable_queued(pipe, 0);
 			mod_delayed_work(dev->deferred_vblank_wq,
 					 &vblank->disable_work,
 					 0);
-		else
+		} else
 			vblank_disable_fn(&vblank->disable_timer);
 	} else if (!vblank->config.disable_immediate) {
-		if (needs_deferred_disable)
+		if (needs_deferred_disable) {
+			trace_drm_deferred_vblank_disable_queued(
+				pipe, vblank_offdelay);
 			mod_delayed_work(dev->deferred_vblank_wq,
 					 &vblank->disable_work,
 					 msecs_to_jiffies(vblank_offdelay));
-		else
+		} else
 			mod_timer(&vblank->disable_timer,
 				  jiffies + ((vblank_offdelay * HZ) / 1000));
 	}
@@ -1508,6 +1525,8 @@ void drm_crtc_vblank_off(struct drm_crtc *crtc)
 	spin_lock(&dev->vbl_lock);
 	drm_dbg_vbl(dev, "crtc %d, vblank enabled %d, inmodeset %d\n",
 		    pipe, vblank->enabled, vblank->inmodeset);
+	trace_drm_vblank_off(pipe, atomic_read(&vblank->refcount),
+			     vblank->enabled, vblank->inmodeset);
 
 	/* Avoid redundant vblank disables without previous
 	 * drm_crtc_vblank_on(). */
@@ -1649,6 +1668,8 @@ void drm_crtc_vblank_on_config(struct drm_crtc *crtc,
 	spin_lock_irq(&dev->vbl_lock);
 	drm_dbg_vbl(dev, "crtc %d, vblank enabled %d, inmodeset %d\n",
 		    pipe, vblank->enabled, vblank->inmodeset);
+	trace_drm_vblank_on(pipe, atomic_read(&vblank->refcount),
+			    vblank->enabled, vblank->inmodeset);
 
 	vblank->config = *config;
 
@@ -2123,11 +2144,12 @@ bool drm_handle_vblank(struct drm_device *dev, unsigned int pipe)
 	spin_unlock_irqrestore(&dev->event_lock, irqflags);
 
 	if (disable_irq) {
-		if (drm_crtc_needs_deferred_vblank(drm_crtc_from_index(dev, pipe)))
+		if (drm_crtc_needs_deferred_vblank(drm_crtc_from_index(dev, pipe))) {
+			trace_drm_deferred_vblank_disable_queued(pipe, 0);
 			mod_delayed_work(dev->deferred_vblank_wq,
 					 &vblank->disable_work,
 					 0);
-		else
+		} else
 			vblank_disable_fn(&vblank->disable_timer);
 	}
 
Signature · 1 line
2.52.0