Skip to content

Commit d4da4e3

Browse files
committed
drm: Measure Self Refresh Entry/Exit times to avoid thrashing
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 Reviewed-by: Daniel Vetter <[email protected]> Signed-off-by: Sean Paul <[email protected]> Link to v1: https://patchwork.freedesktop.org/patch/msgid/[email protected] Link: 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)
1 parent 2d2e0b9 commit d4da4e3

File tree

4 files changed

+90
-13
lines changed

4 files changed

+90
-13
lines changed

drivers/gpu/drm/drm_atomic_helper.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
*/
2727

2828
#include <linux/dma-fence.h>
29+
#include <linux/ktime.h>
2930

3031
#include <drm/drm_atomic.h>
3132
#include <drm/drm_atomic_helper.h>
@@ -1580,9 +1581,23 @@ static void commit_tail(struct drm_atomic_state *old_state)
15801581
{
15811582
struct drm_device *dev = old_state->dev;
15821583
const struct drm_mode_config_helper_funcs *funcs;
1584+
ktime_t start;
1585+
s64 commit_time_ms;
15831586

15841587
funcs = dev->mode_config.helper_private;
15851588

1589+
/*
1590+
* We're measuring the _entire_ commit, so the time will vary depending
1591+
* on how many fences and objects are involved. For the purposes of self
1592+
* refresh, this is desirable since it'll give us an idea of how
1593+
* congested things are. This will inform our decision on how often we
1594+
* should enter self refresh after idle.
1595+
*
1596+
* These times will be averaged out in the self refresh helpers to avoid
1597+
* overreacting over one outlier frame
1598+
*/
1599+
start = ktime_get();
1600+
15861601
drm_atomic_helper_wait_for_fences(dev, old_state, false);
15871602

15881603
drm_atomic_helper_wait_for_dependencies(old_state);
@@ -1592,6 +1607,11 @@ static void commit_tail(struct drm_atomic_state *old_state)
15921607
else
15931608
drm_atomic_helper_commit_tail(old_state);
15941609

1610+
commit_time_ms = ktime_ms_delta(ktime_get(), start);
1611+
if (commit_time_ms > 0)
1612+
drm_self_refresh_helper_update_avg_times(old_state,
1613+
(unsigned long)commit_time_ms);
1614+
15951615
drm_atomic_helper_commit_cleanup_done(old_state);
15961616

15971617
drm_atomic_state_put(old_state);

drivers/gpu/drm/drm_self_refresh_helper.c

Lines changed: 66 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* Authors:
66
* Sean Paul <[email protected]>
77
*/
8+
#include <linux/average.h>
89
#include <linux/bitops.h>
910
#include <linux/slab.h>
1011
#include <linux/workqueue.h>
@@ -50,10 +51,17 @@
5051
* atomic_check when &drm_crtc_state.self_refresh_active is true.
5152
*/
5253

54+
#define SELF_REFRESH_AVG_SEED_MS 200
55+
56+
DECLARE_EWMA(psr_time, 4, 4)
57+
5358
struct drm_self_refresh_data {
5459
struct drm_crtc *crtc;
5560
struct delayed_work entry_work;
56-
unsigned int entry_delay_ms;
61+
62+
struct mutex avg_mutex;
63+
struct ewma_psr_time entry_avg_ms;
64+
struct ewma_psr_time exit_avg_ms;
5765
};
5866

5967
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)
121129
drm_modeset_acquire_fini(&ctx);
122130
}
123131

132+
/**
133+
* drm_self_refresh_helper_update_avg_times - Updates a crtc's SR time averages
134+
* @state: the state which has just been applied to hardware
135+
* @commit_time_ms: the amount of time in ms that this commit took to complete
136+
*
137+
* Called after &drm_mode_config_funcs.atomic_commit_tail, this function will
138+
* update the average entry/exit self refresh times on self refresh transitions.
139+
* These averages will be used when calculating how long to delay before
140+
* entering self refresh mode after activity.
141+
*/
142+
void drm_self_refresh_helper_update_avg_times(struct drm_atomic_state *state,
143+
unsigned int commit_time_ms)
144+
{
145+
struct drm_crtc *crtc;
146+
struct drm_crtc_state *old_crtc_state, *new_crtc_state;
147+
int i;
148+
149+
for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state,
150+
new_crtc_state, i) {
151+
struct drm_self_refresh_data *sr_data = crtc->self_refresh_data;
152+
struct ewma_psr_time *time;
153+
154+
if (old_crtc_state->self_refresh_active ==
155+
new_crtc_state->self_refresh_active)
156+
continue;
157+
158+
if (new_crtc_state->self_refresh_active)
159+
time = &sr_data->entry_avg_ms;
160+
else
161+
time = &sr_data->exit_avg_ms;
162+
163+
mutex_lock(&sr_data->avg_mutex);
164+
ewma_psr_time_add(time, commit_time_ms);
165+
mutex_unlock(&sr_data->avg_mutex);
166+
}
167+
}
168+
EXPORT_SYMBOL(drm_self_refresh_helper_update_avg_times);
169+
124170
/**
125171
* drm_self_refresh_helper_alter_state - Alters the atomic state for SR exit
126172
* @state: the state currently being checked
@@ -152,6 +198,7 @@ void drm_self_refresh_helper_alter_state(struct drm_atomic_state *state)
152198

153199
for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
154200
struct drm_self_refresh_data *sr_data;
201+
unsigned int delay;
155202

156203
/* Don't trigger the entry timer when we're already in SR */
157204
if (crtc_state->self_refresh_active)
@@ -161,21 +208,24 @@ void drm_self_refresh_helper_alter_state(struct drm_atomic_state *state)
161208
if (!sr_data)
162209
continue;
163210

211+
mutex_lock(&sr_data->avg_mutex);
212+
delay = (ewma_psr_time_read(&sr_data->entry_avg_ms) +
213+
ewma_psr_time_read(&sr_data->exit_avg_ms)) * 2;
214+
mutex_unlock(&sr_data->avg_mutex);
215+
164216
mod_delayed_work(system_wq, &sr_data->entry_work,
165-
msecs_to_jiffies(sr_data->entry_delay_ms));
217+
msecs_to_jiffies(delay));
166218
}
167219
}
168220
EXPORT_SYMBOL(drm_self_refresh_helper_alter_state);
169221

170222
/**
171223
* drm_self_refresh_helper_init - Initializes self refresh helpers for a crtc
172224
* @crtc: the crtc which supports self refresh supported displays
173-
* @entry_delay_ms: amount of inactivity to wait before entering self refresh
174225
*
175226
* Returns zero if successful or -errno on failure
176227
*/
177-
int drm_self_refresh_helper_init(struct drm_crtc *crtc,
178-
unsigned int entry_delay_ms)
228+
int drm_self_refresh_helper_init(struct drm_crtc *crtc)
179229
{
180230
struct drm_self_refresh_data *sr_data = crtc->self_refresh_data;
181231

@@ -189,8 +239,18 @@ int drm_self_refresh_helper_init(struct drm_crtc *crtc,
189239

190240
INIT_DELAYED_WORK(&sr_data->entry_work,
191241
drm_self_refresh_helper_entry_work);
192-
sr_data->entry_delay_ms = entry_delay_ms;
193242
sr_data->crtc = crtc;
243+
mutex_init(&sr_data->avg_mutex);
244+
ewma_psr_time_init(&sr_data->entry_avg_ms);
245+
ewma_psr_time_init(&sr_data->exit_avg_ms);
246+
247+
/*
248+
* Seed the averages so they're non-zero (and sufficiently large
249+
* for even poorly performing panels). As time goes on, this will be
250+
* averaged out and the values will trend to their true value.
251+
*/
252+
ewma_psr_time_add(&sr_data->entry_avg_ms, SELF_REFRESH_AVG_SEED_MS);
253+
ewma_psr_time_add(&sr_data->exit_avg_ms, SELF_REFRESH_AVG_SEED_MS);
194254

195255
crtc->self_refresh_data = sr_data;
196256
return 0;

drivers/gpu/drm/rockchip/rockchip_drm_vop.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@
3939
#include "rockchip_drm_vop.h"
4040
#include "rockchip_rgb.h"
4141

42-
#define VOP_SELF_REFRESH_ENTRY_DELAY_MS 100
43-
4442
#define VOP_WIN_SET(vop, win, name, v) \
4543
vop_reg_set(vop, &win->phy->name, win->base, ~0, v, #name)
4644
#define VOP_SCL_SET(vop, win, name, v) \
@@ -1563,8 +1561,7 @@ static int vop_create_crtc(struct vop *vop)
15631561
init_completion(&vop->line_flag_completion);
15641562
crtc->port = port;
15651563

1566-
ret = drm_self_refresh_helper_init(crtc,
1567-
VOP_SELF_REFRESH_ENTRY_DELAY_MS);
1564+
ret = drm_self_refresh_helper_init(crtc);
15681565
if (ret)
15691566
DRM_DEV_DEBUG_KMS(vop->dev,
15701567
"Failed to init %s with SR helpers %d, ignoring\n",

include/drm/drm_self_refresh_helper.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ struct drm_atomic_state;
1212
struct drm_crtc;
1313

1414
void drm_self_refresh_helper_alter_state(struct drm_atomic_state *state);
15+
void drm_self_refresh_helper_update_avg_times(struct drm_atomic_state *state,
16+
unsigned int commit_time_ms);
1517

16-
int drm_self_refresh_helper_init(struct drm_crtc *crtc,
17-
unsigned int entry_delay_ms);
18-
18+
int drm_self_refresh_helper_init(struct drm_crtc *crtc);
1919
void drm_self_refresh_helper_cleanup(struct drm_crtc *crtc);
2020
#endif

0 commit comments

Comments
 (0)