Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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 <daniel.vetter@ffwll.ch>
Signed-off-by: Sean Paul <seanpaul@chromium.org>
Link to v1: https://patchwork.freedesktop.org/patch/msgid/20190917200443.64481-2-sean@poorly.run
Link: https://patchwork.freedesktop.org/patch/msgid/20190918200734.149876-2-sean@poorly.run

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

+90 -13
+20
drivers/gpu/drm/drm_atomic_helper.c
··· 26 26 */ 27 27 28 28 #include <linux/dma-fence.h> 29 + #include <linux/ktime.h> 29 30 30 31 #include <drm/drm_atomic.h> 31 32 #include <drm/drm_atomic_helper.h> ··· 1581 1580 { 1582 1581 struct drm_device *dev = old_state->dev; 1583 1582 const struct drm_mode_config_helper_funcs *funcs; 1583 + ktime_t start; 1584 + s64 commit_time_ms; 1584 1585 1585 1586 funcs = dev->mode_config.helper_private; 1587 + 1588 + /* 1589 + * We're measuring the _entire_ commit, so the time will vary depending 1590 + * on how many fences and objects are involved. For the purposes of self 1591 + * refresh, this is desirable since it'll give us an idea of how 1592 + * congested things are. This will inform our decision on how often we 1593 + * should enter self refresh after idle. 1594 + * 1595 + * These times will be averaged out in the self refresh helpers to avoid 1596 + * overreacting over one outlier frame 1597 + */ 1598 + start = ktime_get(); 1586 1599 1587 1600 drm_atomic_helper_wait_for_fences(dev, old_state, false); 1588 1601 ··· 1606 1591 funcs->atomic_commit_tail(old_state); 1607 1592 else 1608 1593 drm_atomic_helper_commit_tail(old_state); 1594 + 1595 + commit_time_ms = ktime_ms_delta(ktime_get(), start); 1596 + if (commit_time_ms > 0) 1597 + drm_self_refresh_helper_update_avg_times(old_state, 1598 + (unsigned long)commit_time_ms); 1609 1599 1610 1600 drm_atomic_helper_commit_cleanup_done(old_state); 1611 1601
+66 -6
drivers/gpu/drm/drm_self_refresh_helper.c
··· 5 5 * Authors: 6 6 * Sean Paul <seanpaul@chromium.org> 7 7 */ 8 + #include <linux/average.h> 8 9 #include <linux/bitops.h> 9 10 #include <linux/slab.h> 10 11 #include <linux/workqueue.h> ··· 51 50 * atomic_check when &drm_crtc_state.self_refresh_active is true. 52 51 */ 53 52 53 + #define SELF_REFRESH_AVG_SEED_MS 200 54 + 55 + DECLARE_EWMA(psr_time, 4, 4) 56 + 54 57 struct drm_self_refresh_data { 55 58 struct drm_crtc *crtc; 56 59 struct delayed_work entry_work; 57 - unsigned int entry_delay_ms; 60 + 61 + struct mutex avg_mutex; 62 + struct ewma_psr_time entry_avg_ms; 63 + struct ewma_psr_time exit_avg_ms; 58 64 }; 59 65 60 66 static void drm_self_refresh_helper_entry_work(struct work_struct *work) ··· 130 122 } 131 123 132 124 /** 125 + * drm_self_refresh_helper_update_avg_times - Updates a crtc's SR time averages 126 + * @state: the state which has just been applied to hardware 127 + * @commit_time_ms: the amount of time in ms that this commit took to complete 128 + * 129 + * Called after &drm_mode_config_funcs.atomic_commit_tail, this function will 130 + * update the average entry/exit self refresh times on self refresh transitions. 131 + * These averages will be used when calculating how long to delay before 132 + * entering self refresh mode after activity. 133 + */ 134 + void drm_self_refresh_helper_update_avg_times(struct drm_atomic_state *state, 135 + unsigned int commit_time_ms) 136 + { 137 + struct drm_crtc *crtc; 138 + struct drm_crtc_state *old_crtc_state, *new_crtc_state; 139 + int i; 140 + 141 + for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, 142 + new_crtc_state, i) { 143 + struct drm_self_refresh_data *sr_data = crtc->self_refresh_data; 144 + struct ewma_psr_time *time; 145 + 146 + if (old_crtc_state->self_refresh_active == 147 + new_crtc_state->self_refresh_active) 148 + continue; 149 + 150 + if (new_crtc_state->self_refresh_active) 151 + time = &sr_data->entry_avg_ms; 152 + else 153 + time = &sr_data->exit_avg_ms; 154 + 155 + mutex_lock(&sr_data->avg_mutex); 156 + ewma_psr_time_add(time, commit_time_ms); 157 + mutex_unlock(&sr_data->avg_mutex); 158 + } 159 + } 160 + EXPORT_SYMBOL(drm_self_refresh_helper_update_avg_times); 161 + 162 + /** 133 163 * drm_self_refresh_helper_alter_state - Alters the atomic state for SR exit 134 164 * @state: the state currently being checked 135 165 * ··· 198 152 199 153 for_each_new_crtc_in_state(state, crtc, crtc_state, i) { 200 154 struct drm_self_refresh_data *sr_data; 155 + unsigned int delay; 201 156 202 157 /* Don't trigger the entry timer when we're already in SR */ 203 158 if (crtc_state->self_refresh_active) ··· 208 161 if (!sr_data) 209 162 continue; 210 163 164 + mutex_lock(&sr_data->avg_mutex); 165 + delay = (ewma_psr_time_read(&sr_data->entry_avg_ms) + 166 + ewma_psr_time_read(&sr_data->exit_avg_ms)) * 2; 167 + mutex_unlock(&sr_data->avg_mutex); 168 + 211 169 mod_delayed_work(system_wq, &sr_data->entry_work, 212 - msecs_to_jiffies(sr_data->entry_delay_ms)); 170 + msecs_to_jiffies(delay)); 213 171 } 214 172 } 215 173 EXPORT_SYMBOL(drm_self_refresh_helper_alter_state); ··· 222 170 /** 223 171 * drm_self_refresh_helper_init - Initializes self refresh helpers for a crtc 224 172 * @crtc: the crtc which supports self refresh supported displays 225 - * @entry_delay_ms: amount of inactivity to wait before entering self refresh 226 173 * 227 174 * Returns zero if successful or -errno on failure 228 175 */ 229 - int drm_self_refresh_helper_init(struct drm_crtc *crtc, 230 - unsigned int entry_delay_ms) 176 + int drm_self_refresh_helper_init(struct drm_crtc *crtc) 231 177 { 232 178 struct drm_self_refresh_data *sr_data = crtc->self_refresh_data; 233 179 ··· 239 189 240 190 INIT_DELAYED_WORK(&sr_data->entry_work, 241 191 drm_self_refresh_helper_entry_work); 242 - sr_data->entry_delay_ms = entry_delay_ms; 243 192 sr_data->crtc = crtc; 193 + mutex_init(&sr_data->avg_mutex); 194 + ewma_psr_time_init(&sr_data->entry_avg_ms); 195 + ewma_psr_time_init(&sr_data->exit_avg_ms); 196 + 197 + /* 198 + * Seed the averages so they're non-zero (and sufficiently large 199 + * for even poorly performing panels). As time goes on, this will be 200 + * averaged out and the values will trend to their true value. 201 + */ 202 + ewma_psr_time_add(&sr_data->entry_avg_ms, SELF_REFRESH_AVG_SEED_MS); 203 + ewma_psr_time_add(&sr_data->exit_avg_ms, SELF_REFRESH_AVG_SEED_MS); 244 204 245 205 crtc->self_refresh_data = sr_data; 246 206 return 0;
+1 -4
drivers/gpu/drm/rockchip/rockchip_drm_vop.c
··· 39 39 #include "rockchip_drm_vop.h" 40 40 #include "rockchip_rgb.h" 41 41 42 - #define VOP_SELF_REFRESH_ENTRY_DELAY_MS 100 43 - 44 42 #define VOP_WIN_SET(vop, win, name, v) \ 45 43 vop_reg_set(vop, &win->phy->name, win->base, ~0, v, #name) 46 44 #define VOP_SCL_SET(vop, win, name, v) \ ··· 1561 1563 init_completion(&vop->line_flag_completion); 1562 1564 crtc->port = port; 1563 1565 1564 - ret = drm_self_refresh_helper_init(crtc, 1565 - VOP_SELF_REFRESH_ENTRY_DELAY_MS); 1566 + ret = drm_self_refresh_helper_init(crtc); 1566 1567 if (ret) 1567 1568 DRM_DEV_DEBUG_KMS(vop->dev, 1568 1569 "Failed to init %s with SR helpers %d, ignoring\n",
+3 -3
include/drm/drm_self_refresh_helper.h
··· 12 12 struct drm_crtc; 13 13 14 14 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); 15 17 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); 19 19 void drm_self_refresh_helper_cleanup(struct drm_crtc *crtc); 20 20 #endif