Skip to contentSkip to conversation

drm: Fix kerneldoc and remove unused struct member in self_refresh helper

PATCH In review Sean Paul 3 participants 9 messages Sep 18, 2019 – Nov 4, 2019

Subsystems (4)
lore
Share & tools
lore

Sean Paul

author
#0 lore raw

PATCH v2 1/3 drm: Fix kerneldoc and remove unused struct member in self_refresh helper

From: Sean Paul <[email protected]>

Artifacts of previous revisions.

Link to v1: https://patchwork.freedesktop.org/patch/msgid/[email protected]

Changes in v2:
- None

---
 drivers/gpu/drm/drm_self_refresh_helper.c | 1 -
 include/drm/drm_crtc.h                    | 2 +-
 2 files changed, 1 insertion(+), 2 deletions(-)
drivers/gpu/drm/drm_self_refresh_helper.c +0 -1
index 4b9424a8f1f1..9095cebf2147 100644
@@ -53,7 +53,6 @@
 struct drm_self_refresh_data {
 	struct drm_crtc *crtc;
 	struct delayed_work entry_work;
-	struct drm_atomic_state *save_state;
 	unsigned int entry_delay_ms;
 };
 
include/drm/drm_crtc.h +1 -1
index 7e2963cad543..742b31043898 100644
@@ -1107,7 +1107,7 @@ struct drm_crtc {
 	/**
 	 * @self_refresh_data: Holds the state for the self refresh helpers
 	 *
-	 * Initialized via drm_self_refresh_helper_register().
+	 * Initialized via drm_self_refresh_helper_init().
 	 */
 	struct drm_self_refresh_data *self_refresh_data;
 };
Signature · 6 lines
Sean Paul, Software Engineer, Google / Chromium OS

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
author
#1 lore raw

PATCH v2 2/3 drm: Measure Self Refresh Entry/Exit times to avoid thrashing

From: Sean Paul <[email protected]>

Currently the self refresh idle timer is a const set by the crtc. This is fine if the self refresh entry/exit times are well-known for all panels used on that crtc. However panels and workloads can vary quite a bit, and a timeout which works well for one doesn't work well for another.

In the extreme, if the timeout is too short we could get in a situation where the self refresh exits are taking so long we queue up a self refresh entry before the exit commit is even finished.

This patch changes the idle timeout to a moving average of the entry times + a moving average of exit times + the crtc constant.

This patch was tested on rockchip, with a kevin CrOS panel the idle delay averages out to about ~235ms (35 entry + 100 exit + 100 const). On the same board, the bob panel idle delay lands around ~340ms (90 entry
+ 150 exit + 100 const).

WRT the dedicated mutex in self_refresh_data, it would be nice if we could rely on drm_crtc.mutex to protect the average times, but there are a few reasons why a separate lock is a better choice:
- We can't rely on drm_crtc.mutex being held if we're doing a nonblocking commit
- We can't grab drm_crtc.mutex since drm_modeset_lock() doesn't tell us whether the lock was already held in the acquire context (it eats -EALREADY), so we can't tell if we should drop it or not
- We don't need such a heavy-handed lock for what we're trying to do, commit ordering doesn't matter, so a point-of-use lock will be less contentious

Link to v1: https://patchwork.freedesktop.org/patch/msgid/[email protected]

Changes in v2:
- Migrate locking explanation from comment to commit msg (Daniel)
- Turf constant entry delay and multiply the avg times by 2 (Daniel)

---
 drivers/gpu/drm/drm_atomic_helper.c         | 20 ++++++
 drivers/gpu/drm/drm_self_refresh_helper.c   | 72 +++++++++++++++++++--
 drivers/gpu/drm/rockchip/rockchip_drm_vop.c |  5 +-
 include/drm/drm_self_refresh_helper.h       |  6 +-
 4 files changed, 90 insertions(+), 13 deletions(-)
drivers/gpu/drm/drm_atomic_helper.c +20 -0
index 9d7e4da6c292..3f13fa9a9e24 100644
@@ -26,6 +26,7 @@
  */
 
 #include <linux/dma-fence.h>
+#include <linux/ktime.h>
 
 #include <drm/drm_atomic.h>
 #include <drm/drm_atomic_helper.h>
@@ -1570,9 +1571,23 @@ static void commit_tail(struct drm_atomic_state *old_state)
 {
 	struct drm_device *dev = old_state->dev;
 	const struct drm_mode_config_helper_funcs *funcs;
+	ktime_t start;
+	s64 commit_time_ms;
 
 	funcs = dev->mode_config.helper_private;
 
+	/*
+	 * We're measuring the _entire_ commit, so the time will vary depending
+	 * on how many fences and objects are involved. For the purposes of self
+	 * refresh, this is desirable since it'll give us an idea of how
+	 * congested things are. This will inform our decision on how often we
+	 * should enter self refresh after idle.
+	 *
+	 * These times will be averaged out in the self refresh helpers to avoid
+	 * overreacting over one outlier frame
+	 */
+	start = ktime_get();
+
 	drm_atomic_helper_wait_for_fences(dev, old_state, false);
 
 	drm_atomic_helper_wait_for_dependencies(old_state);
@@ -1582,6 +1597,11 @@ static void commit_tail(struct drm_atomic_state *old_state)
 	else
 		drm_atomic_helper_commit_tail(old_state);
 
+	commit_time_ms = ktime_ms_delta(ktime_get(), start);
+	if (commit_time_ms > 0)
+		drm_self_refresh_helper_update_avg_times(old_state,
+						 (unsigned long)commit_time_ms);
+
 	drm_atomic_helper_commit_cleanup_done(old_state);
 
 	drm_atomic_state_put(old_state);
drivers/gpu/drm/drm_self_refresh_helper.c +66 -6

122 lines not on this page · raw message

drivers/gpu/drm/rockchip/rockchip_drm_vop.c +1 -4
index 2f821c58007c..613404f86668 100644
@@ -39,8 +39,6 @@
 #include "rockchip_drm_vop.h"
 #include "rockchip_rgb.h"
 
-#define VOP_SELF_REFRESH_ENTRY_DELAY_MS 100
-
 #define VOP_WIN_SET(vop, win, name, v) \
 		vop_reg_set(vop, &win->phy->name, win->base, ~0, v, #name)
 #define VOP_SCL_SET(vop, win, name, v) \
@@ -1563,8 +1561,7 @@ static int vop_create_crtc(struct vop *vop)
 	init_completion(&vop->line_flag_completion);
 	crtc->port = port;
 
-	ret = drm_self_refresh_helper_init(crtc,
-					   VOP_SELF_REFRESH_ENTRY_DELAY_MS);
+	ret = drm_self_refresh_helper_init(crtc);
 	if (ret)
 		DRM_DEV_DEBUG_KMS(vop->dev,
 			"Failed to init %s with SR helpers %d, ignoring\n",
include/drm/drm_self_refresh_helper.h +3 -3
index 397a583ccca7..5b79d253fb46 100644
@@ -12,9 +12,9 @@ struct drm_atomic_state;
 struct drm_crtc;
 
 void drm_self_refresh_helper_alter_state(struct drm_atomic_state *state);
+void drm_self_refresh_helper_update_avg_times(struct drm_atomic_state *state,
+					      unsigned int commit_time_ms);
 
-int drm_self_refresh_helper_init(struct drm_crtc *crtc,
-				 unsigned int entry_delay_ms);
-
+int drm_self_refresh_helper_init(struct drm_crtc *crtc);
 void drm_self_refresh_helper_cleanup(struct drm_crtc *crtc);
 #endif
Signature · 1 line
Sean Paul, Software Engineer, Google / Chromium OS
author
#2 lore raw

Re: [PATCH v2 2/3] drm: Measure Self Refresh Entry/Exit times to avoid thrashing

Quoted 32 lines · Sean Paul

From: Sean Paul <[email protected]>

Currently the self refresh idle timer is a const set by the crtc. This is fine if the self refresh entry/exit times are well-known for all panels used on that crtc. However panels and workloads can vary quite a bit, and a timeout which works well for one doesn't work well for another.

In the extreme, if the timeout is too short we could get in a situation where the self refresh exits are taking so long we queue up a self refresh entry before the exit commit is even finished.

This patch changes the idle timeout to a moving average of the entry times + a moving average of exit times + the crtc constant.

This patch was tested on rockchip, with a kevin CrOS panel the idle delay averages out to about ~235ms (35 entry + 100 exit + 100 const). On the same board, the bob panel idle delay lands around ~340ms (90 entry
+ 150 exit + 100 const).

WRT the dedicated mutex in self_refresh_data, it would be nice if we could rely on drm_crtc.mutex to protect the average times, but there are a few reasons why a separate lock is a better choice:
- We can't rely on drm_crtc.mutex being held if we're doing a nonblocking commit
- We can't grab drm_crtc.mutex since drm_modeset_lock() doesn't tell us whether the lock was already held in the acquire context (it eats -EALREADY), so we can't tell if we should drop it or not
- We don't need such a heavy-handed lock for what we're trying to do, commit ordering doesn't matter, so a point-of-use lock will be less contentious

Quoted 1 line

Pushed the first 2 to drm-misc-next-fixes to fix the gru-bob regression. I'll fix up the 3rd patch separately.

Thank you for the reviews!

Sean

Quoted 237 lines

Link to v1: https://patchwork.freedesktop.org/patch/msgid/[email protected]

Changes in v2:
- Migrate locking explanation from comment to commit msg (Daniel)
- Turf constant entry delay and multiply the avg times by 2 (Daniel)

---
 drivers/gpu/drm/drm_atomic_helper.c         | 20 ++++++
 drivers/gpu/drm/drm_self_refresh_helper.c   | 72 +++++++++++++++++++--
 drivers/gpu/drm/rockchip/rockchip_drm_vop.c |  5 +-
 include/drm/drm_self_refresh_helper.h       |  6 +-
 4 files changed, 90 insertions(+), 13 deletions(-)
drivers/gpu/drm/drm_atomic_helper.c +20 -0
index 9d7e4da6c292..3f13fa9a9e24 100644
@@ -26,6 +26,7 @@
  */
 
 #include <linux/dma-fence.h>
+#include <linux/ktime.h>
 
 #include <drm/drm_atomic.h>
 #include <drm/drm_atomic_helper.h>
@@ -1570,9 +1571,23 @@ static void commit_tail(struct drm_atomic_state *old_state)
 {
 	struct drm_device *dev = old_state->dev;
 	const struct drm_mode_config_helper_funcs *funcs;
+	ktime_t start;
+	s64 commit_time_ms;
 
 	funcs = dev->mode_config.helper_private;
 
+	/*
+	 * We're measuring the _entire_ commit, so the time will vary depending
+	 * on how many fences and objects are involved. For the purposes of self
+	 * refresh, this is desirable since it'll give us an idea of how
+	 * congested things are. This will inform our decision on how often we
+	 * should enter self refresh after idle.
+	 *
+	 * These times will be averaged out in the self refresh helpers to avoid
+	 * overreacting over one outlier frame
+	 */
+	start = ktime_get();
+
 	drm_atomic_helper_wait_for_fences(dev, old_state, false);
 
 	drm_atomic_helper_wait_for_dependencies(old_state);
@@ -1582,6 +1597,11 @@ static void commit_tail(struct drm_atomic_state *old_state)
 	else
 		drm_atomic_helper_commit_tail(old_state);
 
+	commit_time_ms = ktime_ms_delta(ktime_get(), start);
+	if (commit_time_ms > 0)
+		drm_self_refresh_helper_update_avg_times(old_state,
+						 (unsigned long)commit_time_ms);
+
 	drm_atomic_helper_commit_cleanup_done(old_state);
 
 	drm_atomic_state_put(old_state);
drivers/gpu/drm/drm_self_refresh_helper.c +66 -6
index 9095cebf2147..68f4765a5896 100644
@@ -5,6 +5,7 @@
  * Authors:
  * Sean Paul <[email protected]>
  */
+#include <linux/average.h>
 #include <linux/bitops.h>
 #include <linux/slab.h>
 #include <linux/workqueue.h>
@@ -50,10 +51,17 @@
  * atomic_check when &drm_crtc_state.self_refresh_active is true.
  */
 
+#define SELF_REFRESH_AVG_SEED_MS 200
+
+DECLARE_EWMA(psr_time, 4, 4)
+
 struct drm_self_refresh_data {
 	struct drm_crtc *crtc;
 	struct delayed_work entry_work;
-	unsigned int entry_delay_ms;
+
+	struct mutex avg_mutex;
+	struct ewma_psr_time entry_avg_ms;
+	struct ewma_psr_time exit_avg_ms;
 };
 
 static void drm_self_refresh_helper_entry_work(struct work_struct *work)
@@ -121,6 +129,44 @@ static void drm_self_refresh_helper_entry_work(struct work_struct *work)
 	drm_modeset_acquire_fini(&ctx);
 }
 
+/**
+ * drm_self_refresh_helper_update_avg_times - Updates a crtc's SR time averages
+ * @state: the state which has just been applied to hardware
+ * @commit_time_ms: the amount of time in ms that this commit took to complete
+ *
+ * Called after &drm_mode_config_funcs.atomic_commit_tail, this function will
+ * update the average entry/exit self refresh times on self refresh transitions.
+ * These averages will be used when calculating how long to delay before
+ * entering self refresh mode after activity.
+ */
+void drm_self_refresh_helper_update_avg_times(struct drm_atomic_state *state,
+					      unsigned int commit_time_ms)
+{
+	struct drm_crtc *crtc;
+	struct drm_crtc_state *old_crtc_state, *new_crtc_state;
+	int i;
+
+	for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state,
+				      new_crtc_state, i) {
+		struct drm_self_refresh_data *sr_data = crtc->self_refresh_data;
+		struct ewma_psr_time *time;
+
+		if (old_crtc_state->self_refresh_active ==
+		    new_crtc_state->self_refresh_active)
+			continue;
+
+		if (new_crtc_state->self_refresh_active)
+			time = &sr_data->entry_avg_ms;
+		else
+			time = &sr_data->exit_avg_ms;
+
+		mutex_lock(&sr_data->avg_mutex);
+		ewma_psr_time_add(time, commit_time_ms);
+		mutex_unlock(&sr_data->avg_mutex);
+	}
+}
+EXPORT_SYMBOL(drm_self_refresh_helper_update_avg_times);
+
 /**
  * drm_self_refresh_helper_alter_state - Alters the atomic state for SR exit
  * @state: the state currently being checked
@@ -152,6 +198,7 @@ void drm_self_refresh_helper_alter_state(struct drm_atomic_state *state)
 
 	for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
 		struct drm_self_refresh_data *sr_data;
+		unsigned int delay;
 
 		/* Don't trigger the entry timer when we're already in SR */
 		if (crtc_state->self_refresh_active)
@@ -161,8 +208,13 @@ void drm_self_refresh_helper_alter_state(struct drm_atomic_state *state)
 		if (!sr_data)
 			continue;
 
+		mutex_lock(&sr_data->avg_mutex);
+		delay = (ewma_psr_time_read(&sr_data->entry_avg_ms) +
+			 ewma_psr_time_read(&sr_data->exit_avg_ms)) * 2;
+		mutex_unlock(&sr_data->avg_mutex);
+
 		mod_delayed_work(system_wq, &sr_data->entry_work,
-				 msecs_to_jiffies(sr_data->entry_delay_ms));
+				 msecs_to_jiffies(delay));
 	}
 }
 EXPORT_SYMBOL(drm_self_refresh_helper_alter_state);
@@ -170,12 +222,10 @@ EXPORT_SYMBOL(drm_self_refresh_helper_alter_state);
 /**
  * drm_self_refresh_helper_init - Initializes self refresh helpers for a crtc
  * @crtc: the crtc which supports self refresh supported displays
- * @entry_delay_ms: amount of inactivity to wait before entering self refresh
  *
  * Returns zero if successful or -errno on failure
  */
-int drm_self_refresh_helper_init(struct drm_crtc *crtc,
-				 unsigned int entry_delay_ms)
+int drm_self_refresh_helper_init(struct drm_crtc *crtc)
 {
 	struct drm_self_refresh_data *sr_data = crtc->self_refresh_data;
 
@@ -189,8 +239,18 @@ int drm_self_refresh_helper_init(struct drm_crtc *crtc,
 
 	INIT_DELAYED_WORK(&sr_data->entry_work,
 			  drm_self_refresh_helper_entry_work);
-	sr_data->entry_delay_ms = entry_delay_ms;
 	sr_data->crtc = crtc;
+	mutex_init(&sr_data->avg_mutex);
+	ewma_psr_time_init(&sr_data->entry_avg_ms);
+	ewma_psr_time_init(&sr_data->exit_avg_ms);
+
+	/*
+	 * Seed the averages so they're non-zero (and sufficiently large
+	 * for even poorly performing panels). As time goes on, this will be
+	 * averaged out and the values will trend to their true value.
+	 */
+	ewma_psr_time_add(&sr_data->entry_avg_ms, SELF_REFRESH_AVG_SEED_MS);
+	ewma_psr_time_add(&sr_data->exit_avg_ms, SELF_REFRESH_AVG_SEED_MS);
 
 	crtc->self_refresh_data = sr_data;
 	return 0;
drivers/gpu/drm/rockchip/rockchip_drm_vop.c +1 -4
index 2f821c58007c..613404f86668 100644
@@ -39,8 +39,6 @@
 #include "rockchip_drm_vop.h"
 #include "rockchip_rgb.h"
 
-#define VOP_SELF_REFRESH_ENTRY_DELAY_MS 100
-
 #define VOP_WIN_SET(vop, win, name, v) \
 		vop_reg_set(vop, &win->phy->name, win->base, ~0, v, #name)
 #define VOP_SCL_SET(vop, win, name, v) \
@@ -1563,8 +1561,7 @@ static int vop_create_crtc(struct vop *vop)
 	init_completion(&vop->line_flag_completion);
 	crtc->port = port;
 
-	ret = drm_self_refresh_helper_init(crtc,
-					   VOP_SELF_REFRESH_ENTRY_DELAY_MS);
+	ret = drm_self_refresh_helper_init(crtc);
 	if (ret)
 		DRM_DEV_DEBUG_KMS(vop->dev,
 			"Failed to init %s with SR helpers %d, ignoring\n",
include/drm/drm_self_refresh_helper.h +3 -3
index 397a583ccca7..5b79d253fb46 100644
@@ -12,9 +12,9 @@ struct drm_atomic_state;
 struct drm_crtc;
 
 void drm_self_refresh_helper_alter_state(struct drm_atomic_state *state);
+void drm_self_refresh_helper_update_avg_times(struct drm_atomic_state *state,
+					      unsigned int commit_time_ms);
 
-int drm_self_refresh_helper_init(struct drm_crtc *crtc,
-				 unsigned int entry_delay_ms);
-
+int drm_self_refresh_helper_init(struct drm_crtc *crtc);
 void drm_self_refresh_helper_cleanup(struct drm_crtc *crtc);
 #endif
Signature · 1 line
Sean Paul, Software Engineer, Google / Chromium OS
Signature · 1 line
Sean Paul, Software Engineer, Google / Chromium OS
#3 lore raw

Re: [PATCH v2 2/3] drm: Measure Self Refresh Entry/Exit times to avoid thrashing

Hi Sean,

Since 5.4-rc1 my Samsung Chromebook Plus (kevin) doesn't suspend/resume correctly, at least once every ten suspend/resume cycles the display doesn't turn on, and when this happens the kernel log message reports:

[   60.420230] PM: suspend exit
[   60.463866] rockchip-dp ff970000.edp: AUX CH cmd reply timeout!
[   60.971653] rockchip-dp ff970000.edp: AUX CH enable timeout!
[   61.478668] rockchip-dp ff970000.edp: AUX CH enable timeout!
[   61.985661] rockchip-dp ff970000.edp: AUX CH enable timeout!
[   62.492644] rockchip-dp ff970000.edp: AUX CH enable timeout!
[   62.999617] rockchip-dp ff970000.edp: AUX CH enable timeout!
[   63.506595] rockchip-dp ff970000.edp: AUX CH enable timeout!
[   64.013678] rockchip-dp ff970000.edp: AUX CH enable timeout!
[   64.522856] rockchip-dp ff970000.edp: AUX CH enable timeout!
[   60.463866] rockchip-dp ff970000.edp: AUX CH cmd reply timeout!
[   60.971653] rockchip-dp ff970000.edp: AUX CH enable timeout!
[   61.478668] rockchip-dp ff970000.edp: AUX CH enable timeout!
[   61.985661] rockchip-dp ff970000.edp: AUX CH enable timeout!
[   62.492644] rockchip-dp ff970000.edp: AUX CH enable timeout!

...

Today I bisected the problem and pointed me to this commit. Reverting the commit fixes the issue, but from I quick look I don't see any obvious problem. I'll spend more time tomorrow looking at this but any idea will be welcome.

Thanks,
Enric

Missatge de Sean Paul <[email protected]> del dia dj., 19 de set. 2019 a les 16:14:

Quoted 287 lines · Sean Paul
Quoted 32 lines

From: Sean Paul <[email protected]>

Currently the self refresh idle timer is a const set by the crtc. This is fine if the self refresh entry/exit times are well-known for all panels used on that crtc. However panels and workloads can vary quite a bit, and a timeout which works well for one doesn't work well for another.

In the extreme, if the timeout is too short we could get in a situation where the self refresh exits are taking so long we queue up a self refresh entry before the exit commit is even finished.

This patch changes the idle timeout to a moving average of the entry times + a moving average of exit times + the crtc constant.

This patch was tested on rockchip, with a kevin CrOS panel the idle delay averages out to about ~235ms (35 entry + 100 exit + 100 const). On the same board, the bob panel idle delay lands around ~340ms (90 entry
+ 150 exit + 100 const).

WRT the dedicated mutex in self_refresh_data, it would be nice if we could rely on drm_crtc.mutex to protect the average times, but there are a few reasons why a separate lock is a better choice:
- We can't rely on drm_crtc.mutex being held if we're doing a nonblocking commit
- We can't grab drm_crtc.mutex since drm_modeset_lock() doesn't tell us whether the lock was already held in the acquire context (it eats -EALREADY), so we can't tell if we should drop it or not
- We don't need such a heavy-handed lock for what we're trying to do, commit ordering doesn't matter, so a point-of-use lock will be less contentious

Quoted 1 line

Pushed the first 2 to drm-misc-next-fixes to fix the gru-bob regression. I'll fix up the 3rd patch separately.

Thank you for the reviews!

Sean

Quoted 235 lines

Link to v1: https://patchwork.freedesktop.org/patch/msgid/[email protected]

Changes in v2:
- Migrate locking explanation from comment to commit msg (Daniel)
- Turf constant entry delay and multiply the avg times by 2 (Daniel)

---
 drivers/gpu/drm/drm_atomic_helper.c         | 20 ++++++
 drivers/gpu/drm/drm_self_refresh_helper.c   | 72 +++++++++++++++++++--
 drivers/gpu/drm/rockchip/rockchip_drm_vop.c |  5 +-
 include/drm/drm_self_refresh_helper.h       |  6 +-
 4 files changed, 90 insertions(+), 13 deletions(-)
drivers/gpu/drm/drm_atomic_helper.c +20 -0
index 9d7e4da6c292..3f13fa9a9e24 100644
@@ -26,6 +26,7 @@
  */
 
 #include <linux/dma-fence.h>
+#include <linux/ktime.h>
 
 #include <drm/drm_atomic.h>
 #include <drm/drm_atomic_helper.h>
@@ -1570,9 +1571,23 @@ static void commit_tail(struct drm_atomic_state *old_state)
 {
      struct drm_device *dev = old_state->dev;
      const struct drm_mode_config_helper_funcs *funcs;
+     ktime_t start;
+     s64 commit_time_ms;
 
      funcs = dev->mode_config.helper_private;
 
+     /*
+      * We're measuring the _entire_ commit, so the time will vary depending
+      * on how many fences and objects are involved. For the purposes of self
+      * refresh, this is desirable since it'll give us an idea of how
+      * congested things are. This will inform our decision on how often we
+      * should enter self refresh after idle.
+      *
+      * These times will be averaged out in the self refresh helpers to avoid
+      * overreacting over one outlier frame
+      */
+     start = ktime_get();
+
      drm_atomic_helper_wait_for_fences(dev, old_state, false);
 
      drm_atomic_helper_wait_for_dependencies(old_state);
@@ -1582,6 +1597,11 @@ static void commit_tail(struct drm_atomic_state *old_state)
      else
              drm_atomic_helper_commit_tail(old_state);
 
+     commit_time_ms = ktime_ms_delta(ktime_get(), start);
+     if (commit_time_ms > 0)
+             drm_self_refresh_helper_update_avg_times(old_state,
+                                              (unsigned long)commit_time_ms);
+
      drm_atomic_helper_commit_cleanup_done(old_state);
 
      drm_atomic_state_put(old_state);
drivers/gpu/drm/drm_self_refresh_helper.c +66 -6
index 9095cebf2147..68f4765a5896 100644
@@ -5,6 +5,7 @@
  * Authors:
  * Sean Paul <[email protected]>
  */
+#include <linux/average.h>
 #include <linux/bitops.h>
 #include <linux/slab.h>
 #include <linux/workqueue.h>
@@ -50,10 +51,17 @@
  * atomic_check when &drm_crtc_state.self_refresh_active is true.
  */
 
+#define SELF_REFRESH_AVG_SEED_MS 200
+
+DECLARE_EWMA(psr_time, 4, 4)
+
 struct drm_self_refresh_data {
      struct drm_crtc *crtc;
      struct delayed_work entry_work;
-     unsigned int entry_delay_ms;
+
+     struct mutex avg_mutex;
+     struct ewma_psr_time entry_avg_ms;
+     struct ewma_psr_time exit_avg_ms;
 };
 
 static void drm_self_refresh_helper_entry_work(struct work_struct *work)
@@ -121,6 +129,44 @@ static void drm_self_refresh_helper_entry_work(struct work_struct *work)
      drm_modeset_acquire_fini(&ctx);
 }
 
+/**
+ * drm_self_refresh_helper_update_avg_times - Updates a crtc's SR time averages
+ * @state: the state which has just been applied to hardware
+ * @commit_time_ms: the amount of time in ms that this commit took to complete
+ *
+ * Called after &drm_mode_config_funcs.atomic_commit_tail, this function will
+ * update the average entry/exit self refresh times on self refresh transitions.
+ * These averages will be used when calculating how long to delay before
+ * entering self refresh mode after activity.
+ */
+void drm_self_refresh_helper_update_avg_times(struct drm_atomic_state *state,
+                                           unsigned int commit_time_ms)
+{
+     struct drm_crtc *crtc;
+     struct drm_crtc_state *old_crtc_state, *new_crtc_state;
+     int i;
+
+     for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state,
+                                   new_crtc_state, i) {
+             struct drm_self_refresh_data *sr_data = crtc->self_refresh_data;
+             struct ewma_psr_time *time;
+
+             if (old_crtc_state->self_refresh_active ==
+                 new_crtc_state->self_refresh_active)
+                     continue;
+
+             if (new_crtc_state->self_refresh_active)
+                     time = &sr_data->entry_avg_ms;
+             else
+                     time = &sr_data->exit_avg_ms;
+
+             mutex_lock(&sr_data->avg_mutex);
+             ewma_psr_time_add(time, commit_time_ms);
+             mutex_unlock(&sr_data->avg_mutex);
+     }
+}
+EXPORT_SYMBOL(drm_self_refresh_helper_update_avg_times);
+
 /**
  * drm_self_refresh_helper_alter_state - Alters the atomic state for SR exit
  * @state: the state currently being checked
@@ -152,6 +198,7 @@ void drm_self_refresh_helper_alter_state(struct drm_atomic_state *state)
 
      for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
              struct drm_self_refresh_data *sr_data;
+             unsigned int delay;
 
              /* Don't trigger the entry timer when we're already in SR */
              if (crtc_state->self_refresh_active)
@@ -161,8 +208,13 @@ void drm_self_refresh_helper_alter_state(struct drm_atomic_state *state)
              if (!sr_data)
                      continue;
 
+             mutex_lock(&sr_data->avg_mutex);
+             delay = (ewma_psr_time_read(&sr_data->entry_avg_ms) +
+                      ewma_psr_time_read(&sr_data->exit_avg_ms)) * 2;
+             mutex_unlock(&sr_data->avg_mutex);
+
              mod_delayed_work(system_wq, &sr_data->entry_work,
-                              msecs_to_jiffies(sr_data->entry_delay_ms));
+                              msecs_to_jiffies(delay));
      }
 }
 EXPORT_SYMBOL(drm_self_refresh_helper_alter_state);
@@ -170,12 +222,10 @@ EXPORT_SYMBOL(drm_self_refresh_helper_alter_state);
 /**
  * drm_self_refresh_helper_init - Initializes self refresh helpers for a crtc
  * @crtc: the crtc which supports self refresh supported displays
- * @entry_delay_ms: amount of inactivity to wait before entering self refresh
  *
  * Returns zero if successful or -errno on failure
  */
-int drm_self_refresh_helper_init(struct drm_crtc *crtc,
-                              unsigned int entry_delay_ms)
+int drm_self_refresh_helper_init(struct drm_crtc *crtc)
 {
      struct drm_self_refresh_data *sr_data = crtc->self_refresh_data;
 
@@ -189,8 +239,18 @@ int drm_self_refresh_helper_init(struct drm_crtc *crtc,
 
      INIT_DELAYED_WORK(&sr_data->entry_work,
                        drm_self_refresh_helper_entry_work);
-     sr_data->entry_delay_ms = entry_delay_ms;
      sr_data->crtc = crtc;
+     mutex_init(&sr_data->avg_mutex);
+     ewma_psr_time_init(&sr_data->entry_avg_ms);
+     ewma_psr_time_init(&sr_data->exit_avg_ms);
+
+     /*
+      * Seed the averages so they're non-zero (and sufficiently large
+      * for even poorly performing panels). As time goes on, this will be
+      * averaged out and the values will trend to their true value.
+      */
+     ewma_psr_time_add(&sr_data->entry_avg_ms, SELF_REFRESH_AVG_SEED_MS);
+     ewma_psr_time_add(&sr_data->exit_avg_ms, SELF_REFRESH_AVG_SEED_MS);
 
      crtc->self_refresh_data = sr_data;
      return 0;
drivers/gpu/drm/rockchip/rockchip_drm_vop.c +1 -4
index 2f821c58007c..613404f86668 100644
@@ -39,8 +39,6 @@
 #include "rockchip_drm_vop.h"
 #include "rockchip_rgb.h"
 
-#define VOP_SELF_REFRESH_ENTRY_DELAY_MS 100
-
 #define VOP_WIN_SET(vop, win, name, v) \
              vop_reg_set(vop, &win->phy->name, win->base, ~0, v, #name)
 #define VOP_SCL_SET(vop, win, name, v) \
@@ -1563,8 +1561,7 @@ static int vop_create_crtc(struct vop *vop)
      init_completion(&vop->line_flag_completion);
      crtc->port = port;
 
-     ret = drm_self_refresh_helper_init(crtc,
-                                        VOP_SELF_REFRESH_ENTRY_DELAY_MS);
+     ret = drm_self_refresh_helper_init(crtc);
      if (ret)
              DRM_DEV_DEBUG_KMS(vop->dev,
                      "Failed to init %s with SR helpers %d, ignoring\n",
include/drm/drm_self_refresh_helper.h +3 -4
index 397a583ccca7..5b79d253fb46 100644
@@ -12,9 +12,9 @@ struct drm_atomic_state;
 struct drm_crtc;
 
 void drm_self_refresh_helper_alter_state(struct drm_atomic_state *state);
+void drm_self_refresh_helper_update_avg_times(struct drm_atomic_state *state,
+                                           unsigned int commit_time_ms);
 
-int drm_self_refresh_helper_init(struct drm_crtc *crtc,
-                              unsigned int entry_delay_ms);
-
+int drm_self_refresh_helper_init(struct drm_crtc *crtc);
 void drm_self_refresh_helper_cleanup(struct drm_crtc *crtc);
 #endif
--
Quoted 2 lines

Sean Paul, Software Engineer, Google / Chromium OS

--
Sean Paul, Software Engineer, Google / Chromium OS
_______________________________________________
dri-devel mailing list [email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#4 lore raw

Re: [PATCH v2 2/3] drm: Measure Self Refresh Entry/Exit times to avoid thrashing

Missatge de Enric Balletbo Serra <[email protected]> del dia dc., 30 d’oct. 2019 a les 17:59:

Quoted 23 lines

Hi Sean,

Since 5.4-rc1 my Samsung Chromebook Plus (kevin) doesn't suspend/resume correctly, at least once every ten suspend/resume cycles the display doesn't turn on, and when this happens the kernel log message reports:

[   60.420230] PM: suspend exit
[   60.463866] rockchip-dp ff970000.edp: AUX CH cmd reply timeout!
[   60.971653] rockchip-dp ff970000.edp: AUX CH enable timeout!
[   61.478668] rockchip-dp ff970000.edp: AUX CH enable timeout!
[   61.985661] rockchip-dp ff970000.edp: AUX CH enable timeout!
[   62.492644] rockchip-dp ff970000.edp: AUX CH enable timeout!
[   62.999617] rockchip-dp ff970000.edp: AUX CH enable timeout!
[   63.506595] rockchip-dp ff970000.edp: AUX CH enable timeout!
[   64.013678] rockchip-dp ff970000.edp: AUX CH enable timeout!
[   64.522856] rockchip-dp ff970000.edp: AUX CH enable timeout!
[   60.463866] rockchip-dp ff970000.edp: AUX CH cmd reply timeout!
[   60.971653] rockchip-dp ff970000.edp: AUX CH enable timeout!
[   61.478668] rockchip-dp ff970000.edp: AUX CH enable timeout!
[   61.985661] rockchip-dp ff970000.edp: AUX CH enable timeout!
[   62.492644] rockchip-dp ff970000.edp: AUX CH enable timeout!
Quoted 7 lines

...

Today I bisected the problem and pointed me to this commit. Reverting the commit fixes the issue, but from I quick look I don't see any obvious problem. I'll spend more time tomorrow looking at this but any idea will be welcome.

Update for the record. After looking a bit more I don't think this patch is problematic. I think the problem was already there but after this patch is easier to reproduce just because the PSR entry code work is delayed a bit (before was called every 100ms). I think the problem is more related with the current enable/disable path when panel is in SR mode or not after a suspend/resume.

Anyway, I will continue the discussion on another thread when I find the root cause.

Thanks,
Enric

Quoted 292 lines

Thanks,
Enric

Missatge de Sean Paul <[email protected]> del dia dj., 19 de set. 2019 a les 16:14:

Quoted 287 lines · Sean Paul
Quoted 32 lines

From: Sean Paul <[email protected]>

Currently the self refresh idle timer is a const set by the crtc. This is fine if the self refresh entry/exit times are well-known for all panels used on that crtc. However panels and workloads can vary quite a bit, and a timeout which works well for one doesn't work well for another.

In the extreme, if the timeout is too short we could get in a situation where the self refresh exits are taking so long we queue up a self refresh entry before the exit commit is even finished.

This patch changes the idle timeout to a moving average of the entry times + a moving average of exit times + the crtc constant.

This patch was tested on rockchip, with a kevin CrOS panel the idle delay averages out to about ~235ms (35 entry + 100 exit + 100 const). On the same board, the bob panel idle delay lands around ~340ms (90 entry
+ 150 exit + 100 const).

WRT the dedicated mutex in self_refresh_data, it would be nice if we could rely on drm_crtc.mutex to protect the average times, but there are a few reasons why a separate lock is a better choice:
- We can't rely on drm_crtc.mutex being held if we're doing a nonblocking commit
- We can't grab drm_crtc.mutex since drm_modeset_lock() doesn't tell us whether the lock was already held in the acquire context (it eats -EALREADY), so we can't tell if we should drop it or not
- We don't need such a heavy-handed lock for what we're trying to do, commit ordering doesn't matter, so a point-of-use lock will be less contentious

Quoted 1 line

Pushed the first 2 to drm-misc-next-fixes to fix the gru-bob regression. I'll fix up the 3rd patch separately.

Thank you for the reviews!

Sean

Quoted 235 lines

Link to v1: https://patchwork.freedesktop.org/patch/msgid/[email protected]

Changes in v2:
- Migrate locking explanation from comment to commit msg (Daniel)
- Turf constant entry delay and multiply the avg times by 2 (Daniel)

---
 drivers/gpu/drm/drm_atomic_helper.c         | 20 ++++++
 drivers/gpu/drm/drm_self_refresh_helper.c   | 72 +++++++++++++++++++--
 drivers/gpu/drm/rockchip/rockchip_drm_vop.c |  5 +-
 include/drm/drm_self_refresh_helper.h       |  6 +-
 4 files changed, 90 insertions(+), 13 deletions(-)
drivers/gpu/drm/drm_atomic_helper.c +20 -0
index 9d7e4da6c292..3f13fa9a9e24 100644
@@ -26,6 +26,7 @@
  */
 
 #include <linux/dma-fence.h>
+#include <linux/ktime.h>
 
 #include <drm/drm_atomic.h>
 #include <drm/drm_atomic_helper.h>
@@ -1570,9 +1571,23 @@ static void commit_tail(struct drm_atomic_state *old_state)
 {
      struct drm_device *dev = old_state->dev;
      const struct drm_mode_config_helper_funcs *funcs;
+     ktime_t start;
+     s64 commit_time_ms;
 
      funcs = dev->mode_config.helper_private;
 
+     /*
+      * We're measuring the _entire_ commit, so the time will vary depending
+      * on how many fences and objects are involved. For the purposes of self
+      * refresh, this is desirable since it'll give us an idea of how
+      * congested things are. This will inform our decision on how often we
+      * should enter self refresh after idle.
+      *
+      * These times will be averaged out in the self refresh helpers to avoid
+      * overreacting over one outlier frame
+      */
+     start = ktime_get();
+
      drm_atomic_helper_wait_for_fences(dev, old_state, false);
 
      drm_atomic_helper_wait_for_dependencies(old_state);
@@ -1582,6 +1597,11 @@ static void commit_tail(struct drm_atomic_state *old_state)
      else
              drm_atomic_helper_commit_tail(old_state);
 
+     commit_time_ms = ktime_ms_delta(ktime_get(), start);
+     if (commit_time_ms > 0)
+             drm_self_refresh_helper_update_avg_times(old_state,
+                                              (unsigned long)commit_time_ms);
+
      drm_atomic_helper_commit_cleanup_done(old_state);
 
      drm_atomic_state_put(old_state);
drivers/gpu/drm/drm_self_refresh_helper.c +66 -6
index 9095cebf2147..68f4765a5896 100644
@@ -5,6 +5,7 @@
  * Authors:
  * Sean Paul <[email protected]>
  */
+#include <linux/average.h>
 #include <linux/bitops.h>
 #include <linux/slab.h>
 #include <linux/workqueue.h>
@@ -50,10 +51,17 @@
  * atomic_check when &drm_crtc_state.self_refresh_active is true.
  */
 
+#define SELF_REFRESH_AVG_SEED_MS 200
+
+DECLARE_EWMA(psr_time, 4, 4)
+
 struct drm_self_refresh_data {
      struct drm_crtc *crtc;
      struct delayed_work entry_work;
-     unsigned int entry_delay_ms;
+
+     struct mutex avg_mutex;
+     struct ewma_psr_time entry_avg_ms;
+     struct ewma_psr_time exit_avg_ms;
 };
 
 static void drm_self_refresh_helper_entry_work(struct work_struct *work)
@@ -121,6 +129,44 @@ static void drm_self_refresh_helper_entry_work(struct work_struct *work)
      drm_modeset_acquire_fini(&ctx);
 }
 
+/**
+ * drm_self_refresh_helper_update_avg_times - Updates a crtc's SR time averages
+ * @state: the state which has just been applied to hardware
+ * @commit_time_ms: the amount of time in ms that this commit took to complete
+ *
+ * Called after &drm_mode_config_funcs.atomic_commit_tail, this function will
+ * update the average entry/exit self refresh times on self refresh transitions.
+ * These averages will be used when calculating how long to delay before
+ * entering self refresh mode after activity.
+ */
+void drm_self_refresh_helper_update_avg_times(struct drm_atomic_state *state,
+                                           unsigned int commit_time_ms)
+{
+     struct drm_crtc *crtc;
+     struct drm_crtc_state *old_crtc_state, *new_crtc_state;
+     int i;
+
+     for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state,
+                                   new_crtc_state, i) {
+             struct drm_self_refresh_data *sr_data = crtc->self_refresh_data;
+             struct ewma_psr_time *time;
+
+             if (old_crtc_state->self_refresh_active ==
+                 new_crtc_state->self_refresh_active)
+                     continue;
+
+             if (new_crtc_state->self_refresh_active)
+                     time = &sr_data->entry_avg_ms;
+             else
+                     time = &sr_data->exit_avg_ms;
+
+             mutex_lock(&sr_data->avg_mutex);
+             ewma_psr_time_add(time, commit_time_ms);
+             mutex_unlock(&sr_data->avg_mutex);
+     }
+}
+EXPORT_SYMBOL(drm_self_refresh_helper_update_avg_times);
+
 /**
  * drm_self_refresh_helper_alter_state - Alters the atomic state for SR exit
  * @state: the state currently being checked
@@ -152,6 +198,7 @@ void drm_self_refresh_helper_alter_state(struct drm_atomic_state *state)
 
      for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
              struct drm_self_refresh_data *sr_data;
+             unsigned int delay;
 
              /* Don't trigger the entry timer when we're already in SR */
              if (crtc_state->self_refresh_active)
@@ -161,8 +208,13 @@ void drm_self_refresh_helper_alter_state(struct drm_atomic_state *state)
              if (!sr_data)
                      continue;
 
+             mutex_lock(&sr_data->avg_mutex);
+             delay = (ewma_psr_time_read(&sr_data->entry_avg_ms) +
+                      ewma_psr_time_read(&sr_data->exit_avg_ms)) * 2;
+             mutex_unlock(&sr_data->avg_mutex);
+
              mod_delayed_work(system_wq, &sr_data->entry_work,
-                              msecs_to_jiffies(sr_data->entry_delay_ms));
+                              msecs_to_jiffies(delay));
      }
 }
 EXPORT_SYMBOL(drm_self_refresh_helper_alter_state);
@@ -170,12 +222,10 @@ EXPORT_SYMBOL(drm_self_refresh_helper_alter_state);
 /**
  * drm_self_refresh_helper_init - Initializes self refresh helpers for a crtc
  * @crtc: the crtc which supports self refresh supported displays
- * @entry_delay_ms: amount of inactivity to wait before entering self refresh
  *
  * Returns zero if successful or -errno on failure
  */
-int drm_self_refresh_helper_init(struct drm_crtc *crtc,
-                              unsigned int entry_delay_ms)
+int drm_self_refresh_helper_init(struct drm_crtc *crtc)
 {
      struct drm_self_refresh_data *sr_data = crtc->self_refresh_data;
 
@@ -189,8 +239,18 @@ int drm_self_refresh_helper_init(struct drm_crtc *crtc,
 
      INIT_DELAYED_WORK(&sr_data->entry_work,
                        drm_self_refresh_helper_entry_work);
-     sr_data->entry_delay_ms = entry_delay_ms;
      sr_data->crtc = crtc;
+     mutex_init(&sr_data->avg_mutex);
+     ewma_psr_time_init(&sr_data->entry_avg_ms);
+     ewma_psr_time_init(&sr_data->exit_avg_ms);
+
+     /*
+      * Seed the averages so they're non-zero (and sufficiently large
+      * for even poorly performing panels). As time goes on, this will be
+      * averaged out and the values will trend to their true value.
+      */
+     ewma_psr_time_add(&sr_data->entry_avg_ms, SELF_REFRESH_AVG_SEED_MS);
+     ewma_psr_time_add(&sr_data->exit_avg_ms, SELF_REFRESH_AVG_SEED_MS);
 
      crtc->self_refresh_data = sr_data;
      return 0;
drivers/gpu/drm/rockchip/rockchip_drm_vop.c +1 -4
index 2f821c58007c..613404f86668 100644
@@ -39,8 +39,6 @@
 #include "rockchip_drm_vop.h"
 #include "rockchip_rgb.h"
 
-#define VOP_SELF_REFRESH_ENTRY_DELAY_MS 100
-
 #define VOP_WIN_SET(vop, win, name, v) \
              vop_reg_set(vop, &win->phy->name, win->base, ~0, v, #name)
 #define VOP_SCL_SET(vop, win, name, v) \
@@ -1563,8 +1561,7 @@ static int vop_create_crtc(struct vop *vop)
      init_completion(&vop->line_flag_completion);
      crtc->port = port;
 
-     ret = drm_self_refresh_helper_init(crtc,
-                                        VOP_SELF_REFRESH_ENTRY_DELAY_MS);
+     ret = drm_self_refresh_helper_init(crtc);
      if (ret)
              DRM_DEV_DEBUG_KMS(vop->dev,
                      "Failed to init %s with SR helpers %d, ignoring\n",
include/drm/drm_self_refresh_helper.h +3 -4
index 397a583ccca7..5b79d253fb46 100644
@@ -12,9 +12,9 @@ struct drm_atomic_state;
 struct drm_crtc;
 
 void drm_self_refresh_helper_alter_state(struct drm_atomic_state *state);
+void drm_self_refresh_helper_update_avg_times(struct drm_atomic_state *state,
+                                           unsigned int commit_time_ms);
 
-int drm_self_refresh_helper_init(struct drm_crtc *crtc,
-                              unsigned int entry_delay_ms);
-
+int drm_self_refresh_helper_init(struct drm_crtc *crtc);
 void drm_self_refresh_helper_cleanup(struct drm_crtc *crtc);
 #endif
--
Quoted 2 lines

Sean Paul, Software Engineer, Google / Chromium OS

--
Sean Paul, Software Engineer, Google / Chromium OS
_______________________________________________
dri-devel mailing list [email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

author
#5 lore raw

PATCH v2 3/3 drm: Add self_refresh_state debugfs entry

In reply to Sean Paul

From: Sean Paul <[email protected]>

This patch adds a debugfs entry to surface the entry and exit times as well as the calculated delay.

Changes in v2:
- Added to the set
---

Wasn't too sure how to initialize this, as calling the helper function from drm_debugfs.c seemed... wrong. However there weren't any other compelling solutions, so I figured I'd post this and learn something new.

 drivers/gpu/drm/drm_debugfs.c             | 10 +++++
 drivers/gpu/drm/drm_self_refresh_helper.c | 55 ++++++++++++++++++++++-
 include/drm/drm_self_refresh_helper.h     |  6 +++
 3 files changed, 69 insertions(+), 2 deletions(-)
drivers/gpu/drm/drm_debugfs.c +10 -0
index eab0f2687cd6..175c2451ae72 100644
@@ -38,6 +38,9 @@
 #include <drm/drm_edid.h>
 #include <drm/drm_file.h>
 #include <drm/drm_gem.h>
+#if defined(CONFIG_DRM_KMS_HELPER)
+#include <drm/drm_self_refresh_helper.h>
+#endif
 
 #include "drm_crtc_internal.h"
 #include "drm_internal.h"
@@ -231,6 +234,13 @@ int drm_debugfs_init(struct drm_minor *minor, int minor_id,
 			DRM_ERROR("Failed to create atomic debugfs files\n");
 			return ret;
 		}
+#if defined(CONFIG_DRM_KMS_HELPER)
+		ret = drm_self_refresh_debugfs_init(minor);
+		if (ret) {
+			DRM_ERROR("Failed to init self refresh debugfs\n");
+			return ret;
+		}
+#endif
 	}
 
 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
drivers/gpu/drm/drm_self_refresh_helper.c +53 -2

77 lines not on this page · raw message

include/drm/drm_self_refresh_helper.h +6 -0
index 5b79d253fb46..779f81e035f1 100644
@@ -17,4 +17,10 @@ void drm_self_refresh_helper_update_avg_times(struct drm_atomic_state *state,
 
 int drm_self_refresh_helper_init(struct drm_crtc *crtc);
 void drm_self_refresh_helper_cleanup(struct drm_crtc *crtc);
+
+#ifdef CONFIG_DEBUG_FS
+struct drm_minor;
+int drm_self_refresh_debugfs_init(struct drm_minor *minor);
+#endif
+
 #endif
Signature · 6 lines
Sean Paul, Software Engineer, Google / Chromium OS

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#6 lore raw

Re: [PATCH v2 3/3] drm: Add self_refresh_state debugfs entry

Quoted 9 lines · Sean Paul

From: Sean Paul <[email protected]>

This patch adds a debugfs entry to surface the entry and exit times as well as the calculated delay.

Quoted 8 lines

Changes in v2:
- Added to the set
---

Wasn't too sure how to initialize this, as calling the helper function from drm_debugfs.c seemed... wrong. However there weren't any other compelling solutions, so I figured I'd post this and learn something new.

Won't build, because drm.ko can't depend upon stuff in drm-kms-helper.ko, since that already depends upon the former.

I think we need a drm_self_refresh_helper_register which drivers can call from their crtc->late_register callback. I think Noralf was working on some infrastructure to make this neater, but it didn't land yet.
-Daniel

Quoted 139 lines


 drivers/gpu/drm/drm_debugfs.c             | 10 +++++
 drivers/gpu/drm/drm_self_refresh_helper.c | 55 ++++++++++++++++++++++-
 include/drm/drm_self_refresh_helper.h     |  6 +++
 3 files changed, 69 insertions(+), 2 deletions(-)
drivers/gpu/drm/drm_debugfs.c +10 -0
index eab0f2687cd6..175c2451ae72 100644
@@ -38,6 +38,9 @@
 #include <drm/drm_edid.h>
 #include <drm/drm_file.h>
 #include <drm/drm_gem.h>
+#if defined(CONFIG_DRM_KMS_HELPER)
+#include <drm/drm_self_refresh_helper.h>
+#endif
 
 #include "drm_crtc_internal.h"
 #include "drm_internal.h"
@@ -231,6 +234,13 @@ int drm_debugfs_init(struct drm_minor *minor, int minor_id,
                        DRM_ERROR("Failed to create atomic debugfs files\n");
                        return ret;
                }
+#if defined(CONFIG_DRM_KMS_HELPER)
+               ret = drm_self_refresh_debugfs_init(minor);
+               if (ret) {
+                       DRM_ERROR("Failed to init self refresh debugfs\n");
+                       return ret;
+               }
+#endif
        }
 
        if (drm_core_check_feature(dev, DRIVER_MODESET)) {
drivers/gpu/drm/drm_self_refresh_helper.c +53 -2
index 68f4765a5896..e7544ae1e47b 100644
@@ -14,7 +14,9 @@
 #include <drm/drm_atomic_helper.h>
 #include <drm/drm_connector.h>
 #include <drm/drm_crtc.h>
+#include <drm/drm_debugfs.h>
 #include <drm/drm_device.h>
+#include <drm/drm_file.h>
 #include <drm/drm_mode_config.h>
 #include <drm/drm_modeset_lock.h>
 #include <drm/drm_print.h>
@@ -167,6 +169,16 @@ void drm_self_refresh_helper_update_avg_times(struct drm_atomic_state *state,
 }
 EXPORT_SYMBOL(drm_self_refresh_helper_update_avg_times);
 
+static unsigned int
+drm_self_refresh_calc_idle_delay(struct drm_self_refresh_data *sr_data)
+{
+       if (WARN_ON(!mutex_is_locked(&sr_data->avg_mutex)))
+           return SELF_REFRESH_AVG_SEED_MS * 4;
+
+       return (ewma_psr_time_read(&sr_data->entry_avg_ms) +
+               ewma_psr_time_read(&sr_data->exit_avg_ms)) * 2;
+}
+
 /**
  * drm_self_refresh_helper_alter_state - Alters the atomic state for SR exit
  * @state: the state currently being checked
@@ -209,8 +221,7 @@ void drm_self_refresh_helper_alter_state(struct drm_atomic_state *state)
                        continue;
 
                mutex_lock(&sr_data->avg_mutex);
-               delay = (ewma_psr_time_read(&sr_data->entry_avg_ms) +
-                        ewma_psr_time_read(&sr_data->exit_avg_ms)) * 2;
+               delay = drm_self_refresh_calc_idle_delay(sr_data);
                mutex_unlock(&sr_data->avg_mutex);
 
                mod_delayed_work(system_wq, &sr_data->entry_work,
@@ -275,3 +286,43 @@ void drm_self_refresh_helper_cleanup(struct drm_crtc *crtc)
        kfree(sr_data);
 }
 EXPORT_SYMBOL(drm_self_refresh_helper_cleanup);
+
+#ifdef CONFIG_DEBUG_FS
+
+static int drm_self_refresh_debugfs_state(struct seq_file *m, void *data)
+{
+       struct drm_info_node *node = (struct drm_info_node *) m->private;
+       struct drm_device *dev = node->minor->dev;
+       struct drm_printer p = drm_seq_file_printer(m);
+       struct drm_crtc *crtc;
+
+       drm_for_each_crtc(crtc, dev) {
+               struct drm_self_refresh_data *sr_data = crtc->self_refresh_data;
+               if (!sr_data)
+                       continue;
+
+               mutex_lock(&sr_data->avg_mutex);
+               drm_printf(&p, "crtc[%u]: %s\n", crtc->base.id, crtc->name);
+               drm_printf(&p, "\tentry_avg_ms=%lu\n",
+                          ewma_psr_time_read(&sr_data->entry_avg_ms));
+               drm_printf(&p, "\texit_avg_ms=%lu\n",
+                          ewma_psr_time_read(&sr_data->exit_avg_ms));
+               drm_printf(&p, "\tidle_delay=%u\n",
+                          drm_self_refresh_calc_idle_delay(sr_data));
+               mutex_unlock(&sr_data->avg_mutex);
+       }
+       return 0;
+}
+
+static const struct drm_info_list drm_atomic_debugfs_list[] = {
+       {"self_refresh_state", drm_self_refresh_debugfs_state, 0},
+};
+
+int drm_self_refresh_debugfs_init(struct drm_minor *minor)
+{
+       return drm_debugfs_create_files(drm_atomic_debugfs_list,
+                       ARRAY_SIZE(drm_atomic_debugfs_list),
+                       minor->debugfs_root, minor);
+}
+EXPORT_SYMBOL(drm_self_refresh_debugfs_init);
+#endif
include/drm/drm_self_refresh_helper.h +6 -1
index 5b79d253fb46..779f81e035f1 100644
@@ -17,4 +17,10 @@ void drm_self_refresh_helper_update_avg_times(struct drm_atomic_state *state,
 
 int drm_self_refresh_helper_init(struct drm_crtc *crtc);
 void drm_self_refresh_helper_cleanup(struct drm_crtc *crtc);
+
+#ifdef CONFIG_DEBUG_FS
+struct drm_minor;
+int drm_self_refresh_debugfs_init(struct drm_minor *minor);
+#endif
+
 #endif
--

Sean Paul, Software Engineer, Google / Chromium OS

Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
+41 (0) 79 365 57 48 - http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
author
#7 lore raw

PATCH v3 3/3 drm: Add self_refresh_state debugfs entry

From: Sean Paul <[email protected]>

This patch adds a debugfs entry to surface the entry and exit times as well as the calculated delay.

Link to v2: https://patchwork.freedesktop.org/patch/msgid/[email protected]

Changes in v2:
- Added to the set
Changes in v3:
- Move the debugfs out of core and into driver (Daniel)
- Place a debugfs entry per crtc (Daniel)
---

Here's the per-crtc version of this, mostly as an rfc since it still feels rough (particularly grabbing drm_minor from crtc->dev->primary). We should also probably introduce some drm_debugfs helpers for crtc entries instead of opencoding debugfs goo here and in debugfs_crc.

I've got to switch over to MST-related stuff for the moment, but will get back to this once we have a clear picture of what we want to do.

 drivers/gpu/drm/drm_self_refresh_helper.c   | 63 ++++++++++++++++++++-
 drivers/gpu/drm/rockchip/rockchip_drm_vop.c | 14 +++++
 include/drm/drm_self_refresh_helper.h       |  5 ++
 3 files changed, 80 insertions(+), 2 deletions(-)
drivers/gpu/drm/drm_self_refresh_helper.c +61 -2
index 68f4765a58964..4be7348e105c6 100644
@@ -14,7 +14,9 @@
 #include <drm/drm_atomic_helper.h>
 #include <drm/drm_connector.h>
 #include <drm/drm_crtc.h>
+#include <drm/drm_debugfs.h>
 #include <drm/drm_device.h>
+#include <drm/drm_file.h>
 #include <drm/drm_mode_config.h>
 #include <drm/drm_modeset_lock.h>
 #include <drm/drm_print.h>
@@ -167,6 +169,16 @@ void drm_self_refresh_helper_update_avg_times(struct drm_atomic_state *state,
 }
 EXPORT_SYMBOL(drm_self_refresh_helper_update_avg_times);
 
+static unsigned int
+drm_self_refresh_calc_idle_delay(struct drm_self_refresh_data *sr_data)
+{
+	if (WARN_ON(!mutex_is_locked(&sr_data->avg_mutex)))
+	    return SELF_REFRESH_AVG_SEED_MS * 4;
+
+	return (ewma_psr_time_read(&sr_data->entry_avg_ms) +
+		ewma_psr_time_read(&sr_data->exit_avg_ms)) * 2;
+}
+
 /**
  * drm_self_refresh_helper_alter_state - Alters the atomic state for SR exit
  * @state: the state currently being checked
@@ -209,8 +221,7 @@ void drm_self_refresh_helper_alter_state(struct drm_atomic_state *state)
 			continue;
 
 		mutex_lock(&sr_data->avg_mutex);
-		delay = (ewma_psr_time_read(&sr_data->entry_avg_ms) +
-			 ewma_psr_time_read(&sr_data->exit_avg_ms)) * 2;
+		delay = drm_self_refresh_calc_idle_delay(sr_data);
 		mutex_unlock(&sr_data->avg_mutex);
 
 		mod_delayed_work(system_wq, &sr_data->entry_work,
@@ -275,3 +286,51 @@ void drm_self_refresh_helper_cleanup(struct drm_crtc *crtc)
 	kfree(sr_data);
 }
 EXPORT_SYMBOL(drm_self_refresh_helper_cleanup);
+
+#ifdef CONFIG_DEBUG_FS
+
+static int drm_self_refresh_debugfs_show(struct seq_file *m, void *data)
+{
+	struct drm_crtc *crtc = m->private;
+	struct drm_self_refresh_data *sr_data = crtc->self_refresh_data;
+	struct drm_printer p = drm_seq_file_printer(m);
+
+	if (!sr_data)
+		return 0;
+
+	mutex_lock(&sr_data->avg_mutex);
+	drm_printf(&p, "entry_avg_ms=%lu\n",
+		   ewma_psr_time_read(&sr_data->entry_avg_ms));
+	drm_printf(&p, "exit_avg_ms=%lu\n",
+		   ewma_psr_time_read(&sr_data->exit_avg_ms));
+	drm_printf(&p, "idle_delay=%u\n",
+		   drm_self_refresh_calc_idle_delay(sr_data));
+	mutex_unlock(&sr_data->avg_mutex);
+	return 0;
+}
+
+static int drm_self_refresh_debugfs_open(struct inode *inode, struct file *file)
+{
+	struct drm_crtc *crtc = inode->i_private;
+
+	return single_open(file, drm_self_refresh_debugfs_show, crtc);
+}
+
+static const struct file_operations drm_self_refresh_debugfs_fops = {
+	.owner = THIS_MODULE,
+	.open = drm_self_refresh_debugfs_open,
+	.read = seq_read,
+	.llseek = seq_lseek,
+	.release = single_release,
+};
+
+int drm_self_refresh_debugfs_init(struct drm_crtc *crtc)
+{
+	debugfs_create_file("self_refresh_state", S_IFREG | S_IRUGO,
+			    crtc->debugfs_entry, crtc,
+			    &drm_self_refresh_debugfs_fops);
+	return 0;
+}
+EXPORT_SYMBOL(drm_self_refresh_debugfs_init);
+
+#endif
drivers/gpu/drm/rockchip/rockchip_drm_vop.c +14 -0
index 613404f86668d..181873b7146ac 100644
@@ -1303,6 +1303,19 @@ static struct drm_connector *vop_get_edp_connector(struct vop *vop)
 	return NULL;
 }
 
+static int vop_crtc_late_register(struct drm_crtc *crtc)
+{
+	int ret;
+#if defined(CONFIG_DRM_KMS_HELPER)
+	ret = drm_self_refresh_debugfs_init(crtc);
+	if (ret) {
+		DRM_ERROR("Failed to init self refresh debugfs\n");
+		return ret;
+	}
+#endif
+	return ret;
+}
+
 static int vop_crtc_set_crc_source(struct drm_crtc *crtc,
 				   const char *source_name)
 {
@@ -1359,6 +1372,7 @@ static const struct drm_crtc_funcs vop_crtc_funcs = {
 	.atomic_destroy_state = vop_crtc_destroy_state,
 	.enable_vblank = vop_crtc_enable_vblank,
 	.disable_vblank = vop_crtc_disable_vblank,
+	.late_register = vop_crtc_late_register,
 	.set_crc_source = vop_crtc_set_crc_source,
 	.verify_crc_source = vop_crtc_verify_crc_source,
 };
include/drm/drm_self_refresh_helper.h +5 -0
index 5b79d253fb46e..3ed54ce102f1b 100644
@@ -17,4 +17,9 @@ void drm_self_refresh_helper_update_avg_times(struct drm_atomic_state *state,
 
 int drm_self_refresh_helper_init(struct drm_crtc *crtc);
 void drm_self_refresh_helper_cleanup(struct drm_crtc *crtc);
+
+#ifdef CONFIG_DEBUG_FS
+int drm_self_refresh_debugfs_init(struct drm_crtc *crtc);
+#endif
+
 #endif
Signature · 6 lines
Sean Paul, Software Engineer, Google / Chromium OS

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
author
#8 lore raw

Re: [PATCH v3 3/3] drm: Add self_refresh_state debugfs entry

Quoted 127 lines · Sean Paul

From: Sean Paul <[email protected]>

This patch adds a debugfs entry to surface the entry and exit times as well as the calculated delay.

Link to v2: https://patchwork.freedesktop.org/patch/msgid/[email protected]

Changes in v2:
- Added to the set
Changes in v3:
- Move the debugfs out of core and into driver (Daniel)
- Place a debugfs entry per crtc (Daniel)
---

Here's the per-crtc version of this, mostly as an rfc since it still feels rough (particularly grabbing drm_minor from crtc->dev->primary). We should also probably introduce some drm_debugfs helpers for crtc entries instead of opencoding debugfs goo here and in debugfs_crc.

I've got to switch over to MST-related stuff for the moment, but will get back to this once we have a clear picture of what we want to do.

 drivers/gpu/drm/drm_self_refresh_helper.c   | 63 ++++++++++++++++++++-
 drivers/gpu/drm/rockchip/rockchip_drm_vop.c | 14 +++++
 include/drm/drm_self_refresh_helper.h       |  5 ++
 3 files changed, 80 insertions(+), 2 deletions(-)
drivers/gpu/drm/drm_self_refresh_helper.c +61 -2
index 68f4765a58964..4be7348e105c6 100644
@@ -14,7 +14,9 @@
 #include <drm/drm_atomic_helper.h>
 #include <drm/drm_connector.h>
 #include <drm/drm_crtc.h>
+#include <drm/drm_debugfs.h>
 #include <drm/drm_device.h>
+#include <drm/drm_file.h>
 #include <drm/drm_mode_config.h>
 #include <drm/drm_modeset_lock.h>
 #include <drm/drm_print.h>
@@ -167,6 +169,16 @@ void drm_self_refresh_helper_update_avg_times(struct drm_atomic_state *state,
 }
 EXPORT_SYMBOL(drm_self_refresh_helper_update_avg_times);
 
+static unsigned int
+drm_self_refresh_calc_idle_delay(struct drm_self_refresh_data *sr_data)
+{
+	if (WARN_ON(!mutex_is_locked(&sr_data->avg_mutex)))
+	    return SELF_REFRESH_AVG_SEED_MS * 4;
+
+	return (ewma_psr_time_read(&sr_data->entry_avg_ms) +
+		ewma_psr_time_read(&sr_data->exit_avg_ms)) * 2;
+}
+
 /**
  * drm_self_refresh_helper_alter_state - Alters the atomic state for SR exit
  * @state: the state currently being checked
@@ -209,8 +221,7 @@ void drm_self_refresh_helper_alter_state(struct drm_atomic_state *state)
 			continue;
 
 		mutex_lock(&sr_data->avg_mutex);
-		delay = (ewma_psr_time_read(&sr_data->entry_avg_ms) +
-			 ewma_psr_time_read(&sr_data->exit_avg_ms)) * 2;
+		delay = drm_self_refresh_calc_idle_delay(sr_data);
 		mutex_unlock(&sr_data->avg_mutex);
 
 		mod_delayed_work(system_wq, &sr_data->entry_work,
@@ -275,3 +286,51 @@ void drm_self_refresh_helper_cleanup(struct drm_crtc *crtc)
 	kfree(sr_data);
 }
 EXPORT_SYMBOL(drm_self_refresh_helper_cleanup);
+
+#ifdef CONFIG_DEBUG_FS
+
+static int drm_self_refresh_debugfs_show(struct seq_file *m, void *data)
+{
+	struct drm_crtc *crtc = m->private;
+	struct drm_self_refresh_data *sr_data = crtc->self_refresh_data;
+	struct drm_printer p = drm_seq_file_printer(m);
+
+	if (!sr_data)
+		return 0;
+
+	mutex_lock(&sr_data->avg_mutex);
+	drm_printf(&p, "entry_avg_ms=%lu\n",
+		   ewma_psr_time_read(&sr_data->entry_avg_ms));
+	drm_printf(&p, "exit_avg_ms=%lu\n",
+		   ewma_psr_time_read(&sr_data->exit_avg_ms));
+	drm_printf(&p, "idle_delay=%u\n",
+		   drm_self_refresh_calc_idle_delay(sr_data));
+	mutex_unlock(&sr_data->avg_mutex);
+	return 0;
+}
+
+static int drm_self_refresh_debugfs_open(struct inode *inode, struct file *file)
+{
+	struct drm_crtc *crtc = inode->i_private;
+
+	return single_open(file, drm_self_refresh_debugfs_show, crtc);
+}
+
+static const struct file_operations drm_self_refresh_debugfs_fops = {
+	.owner = THIS_MODULE,
+	.open = drm_self_refresh_debugfs_open,
+	.read = seq_read,
+	.llseek = seq_lseek,
+	.release = single_release,
+};
+
+int drm_self_refresh_debugfs_init(struct drm_crtc *crtc)
+{
+	debugfs_create_file("self_refresh_state", S_IFREG | S_IRUGO,
+			    crtc->debugfs_entry, crtc,
+			    &drm_self_refresh_debugfs_fops);
+	return 0;
+}
+EXPORT_SYMBOL(drm_self_refresh_debugfs_init);
+
+#endif
drivers/gpu/drm/rockchip/rockchip_drm_vop.c +0 -0
index 613404f86668d..181873b7146ac 100644

drivers/gpu/drm/rockchip/rockchip_drm_vop.c static struct drm_connector *vop_get_edp_connector(struct vop *vop) · line 1308

Quoted 6 lines
drivers/gpu/drm/rockchip/rockchip_drm_vop.c +3 -0
@@ -1303,6 +1303,19 @@ static struct drm_connector *vop_get_edp_connector(struct vop *vop)
 	return NULL;
 }
 
+static int vop_crtc_late_register(struct drm_crtc *crtc)
+{
+	int ret;

pretend this is

int ret = 0;

:-)

Quoted 38 lines
drivers/gpu/drm/rockchip/rockchip_drm_vop.c +11 -0
+#if defined(CONFIG_DRM_KMS_HELPER)
+	ret = drm_self_refresh_debugfs_init(crtc);
+	if (ret) {
+		DRM_ERROR("Failed to init self refresh debugfs\n");
+		return ret;
+	}
+#endif
+	return ret;
+}
+
 static int vop_crtc_set_crc_source(struct drm_crtc *crtc,
 				   const char *source_name)
 {
@@ -1359,6 +1372,7 @@ static const struct drm_crtc_funcs vop_crtc_funcs = {
 	.atomic_destroy_state = vop_crtc_destroy_state,
 	.enable_vblank = vop_crtc_enable_vblank,
 	.disable_vblank = vop_crtc_disable_vblank,
+	.late_register = vop_crtc_late_register,
 	.set_crc_source = vop_crtc_set_crc_source,
 	.verify_crc_source = vop_crtc_verify_crc_source,
 };
include/drm/drm_self_refresh_helper.h +5 -0
index 5b79d253fb46e..3ed54ce102f1b 100644
@@ -17,4 +17,9 @@ void drm_self_refresh_helper_update_avg_times(struct drm_atomic_state *state,
 
 int drm_self_refresh_helper_init(struct drm_crtc *crtc);
 void drm_self_refresh_helper_cleanup(struct drm_crtc *crtc);
+
+#ifdef CONFIG_DEBUG_FS
+int drm_self_refresh_debugfs_init(struct drm_crtc *crtc);
+#endif
+
 #endif
Signature · 1 line
Sean Paul, Software Engineer, Google / Chromium OS
Signature · 5 lines
Sean Paul, Software Engineer, Google / Chromium OS
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel