Home | History | Annotate | Line # | Download | only in drm
drm_self_refresh_helper.c revision 1.1
      1 /*	$NetBSD: drm_self_refresh_helper.c,v 1.1 2021/12/18 20:11:03 riastradh Exp $	*/
      2 
      3 // SPDX-License-Identifier: MIT
      4 /*
      5  * Copyright (C) 2019 Google, Inc.
      6  *
      7  * Authors:
      8  * Sean Paul <seanpaul (at) chromium.org>
      9  */
     10 #include <sys/cdefs.h>
     11 __KERNEL_RCSID(0, "$NetBSD: drm_self_refresh_helper.c,v 1.1 2021/12/18 20:11:03 riastradh Exp $");
     12 
     13 #include <linux/average.h>
     14 #include <linux/bitops.h>
     15 #include <linux/slab.h>
     16 #include <linux/workqueue.h>
     17 
     18 #include <drm/drm_atomic.h>
     19 #include <drm/drm_atomic_helper.h>
     20 #include <drm/drm_connector.h>
     21 #include <drm/drm_crtc.h>
     22 #include <drm/drm_device.h>
     23 #include <drm/drm_mode_config.h>
     24 #include <drm/drm_modeset_lock.h>
     25 #include <drm/drm_print.h>
     26 #include <drm/drm_self_refresh_helper.h>
     27 
     28 /**
     29  * DOC: overview
     30  *
     31  * This helper library provides an easy way for drivers to leverage the atomic
     32  * framework to implement panel self refresh (SR) support. Drivers are
     33  * responsible for initializing and cleaning up the SR helpers on load/unload
     34  * (see &drm_self_refresh_helper_init/&drm_self_refresh_helper_cleanup).
     35  * The connector is responsible for setting
     36  * &drm_connector_state.self_refresh_aware to true at runtime if it is SR-aware
     37  * (meaning it knows how to initiate self refresh on the panel).
     38  *
     39  * Once a crtc has enabled SR using &drm_self_refresh_helper_init, the
     40  * helpers will monitor activity and call back into the driver to enable/disable
     41  * SR as appropriate. The best way to think about this is that it's a DPMS
     42  * on/off request with &drm_crtc_state.self_refresh_active set in crtc state
     43  * that tells you to disable/enable SR on the panel instead of power-cycling it.
     44  *
     45  * During SR, drivers may choose to fully disable their crtc/encoder/bridge
     46  * hardware (in which case no driver changes are necessary), or they can inspect
     47  * &drm_crtc_state.self_refresh_active if they want to enter low power mode
     48  * without full disable (in case full disable/enable is too slow).
     49  *
     50  * SR will be deactivated if there are any atomic updates affecting the
     51  * pipe that is in SR mode. If a crtc is driving multiple connectors, all
     52  * connectors must be SR aware and all will enter/exit SR mode at the same time.
     53  *
     54  * If the crtc and connector are SR aware, but the panel connected does not
     55  * support it (or is otherwise unable to enter SR), the driver should fail
     56  * atomic_check when &drm_crtc_state.self_refresh_active is true.
     57  */
     58 
     59 #define SELF_REFRESH_AVG_SEED_MS 200
     60 
     61 DECLARE_EWMA(psr_time, 4, 4)
     62 
     63 struct drm_self_refresh_data {
     64 	struct drm_crtc *crtc;
     65 	struct delayed_work entry_work;
     66 
     67 	struct mutex avg_mutex;
     68 	struct ewma_psr_time entry_avg_ms;
     69 	struct ewma_psr_time exit_avg_ms;
     70 };
     71 
     72 static void drm_self_refresh_helper_entry_work(struct work_struct *work)
     73 {
     74 	struct drm_self_refresh_data *sr_data = container_of(
     75 				to_delayed_work(work),
     76 				struct drm_self_refresh_data, entry_work);
     77 	struct drm_crtc *crtc = sr_data->crtc;
     78 	struct drm_device *dev = crtc->dev;
     79 	struct drm_modeset_acquire_ctx ctx;
     80 	struct drm_atomic_state *state;
     81 	struct drm_connector *conn;
     82 	struct drm_connector_state *conn_state;
     83 	struct drm_crtc_state *crtc_state;
     84 	int i, ret = 0;
     85 
     86 	drm_modeset_acquire_init(&ctx, 0);
     87 
     88 	state = drm_atomic_state_alloc(dev);
     89 	if (!state) {
     90 		ret = -ENOMEM;
     91 		goto out_drop_locks;
     92 	}
     93 
     94 retry:
     95 	state->acquire_ctx = &ctx;
     96 
     97 	crtc_state = drm_atomic_get_crtc_state(state, crtc);
     98 	if (IS_ERR(crtc_state)) {
     99 		ret = PTR_ERR(crtc_state);
    100 		goto out;
    101 	}
    102 
    103 	if (!crtc_state->enable)
    104 		goto out;
    105 
    106 	ret = drm_atomic_add_affected_connectors(state, crtc);
    107 	if (ret)
    108 		goto out;
    109 
    110 	for_each_new_connector_in_state(state, conn, conn_state, i) {
    111 		if (!conn_state->self_refresh_aware)
    112 			goto out;
    113 	}
    114 
    115 	crtc_state->active = false;
    116 	crtc_state->self_refresh_active = true;
    117 
    118 	ret = drm_atomic_commit(state);
    119 	if (ret)
    120 		goto out;
    121 
    122 out:
    123 	if (ret == -EDEADLK) {
    124 		drm_atomic_state_clear(state);
    125 		ret = drm_modeset_backoff(&ctx);
    126 		if (!ret)
    127 			goto retry;
    128 	}
    129 
    130 	drm_atomic_state_put(state);
    131 
    132 out_drop_locks:
    133 	drm_modeset_drop_locks(&ctx);
    134 	drm_modeset_acquire_fini(&ctx);
    135 }
    136 
    137 /**
    138  * drm_self_refresh_helper_update_avg_times - Updates a crtc's SR time averages
    139  * @state: the state which has just been applied to hardware
    140  * @commit_time_ms: the amount of time in ms that this commit took to complete
    141  * @new_self_refresh_mask: bitmask of crtc's that have self_refresh_active in
    142  *    new state
    143  *
    144  * Called after &drm_mode_config_funcs.atomic_commit_tail, this function will
    145  * update the average entry/exit self refresh times on self refresh transitions.
    146  * These averages will be used when calculating how long to delay before
    147  * entering self refresh mode after activity.
    148  */
    149 void
    150 drm_self_refresh_helper_update_avg_times(struct drm_atomic_state *state,
    151 					 unsigned int commit_time_ms,
    152 					 unsigned int new_self_refresh_mask)
    153 {
    154 	struct drm_crtc *crtc;
    155 	struct drm_crtc_state *old_crtc_state;
    156 	int i;
    157 
    158 	for_each_old_crtc_in_state(state, crtc, old_crtc_state, i) {
    159 		bool new_self_refresh_active = new_self_refresh_mask & BIT(i);
    160 		struct drm_self_refresh_data *sr_data = crtc->self_refresh_data;
    161 		struct ewma_psr_time *time;
    162 
    163 		if (old_crtc_state->self_refresh_active ==
    164 		    new_self_refresh_active)
    165 			continue;
    166 
    167 		if (new_self_refresh_active)
    168 			time = &sr_data->entry_avg_ms;
    169 		else
    170 			time = &sr_data->exit_avg_ms;
    171 
    172 		mutex_lock(&sr_data->avg_mutex);
    173 		ewma_psr_time_add(time, commit_time_ms);
    174 		mutex_unlock(&sr_data->avg_mutex);
    175 	}
    176 }
    177 EXPORT_SYMBOL(drm_self_refresh_helper_update_avg_times);
    178 
    179 /**
    180  * drm_self_refresh_helper_alter_state - Alters the atomic state for SR exit
    181  * @state: the state currently being checked
    182  *
    183  * Called at the end of atomic check. This function checks the state for flags
    184  * incompatible with self refresh exit and changes them. This is a bit
    185  * disingenuous since userspace is expecting one thing and we're giving it
    186  * another. However in order to keep self refresh entirely hidden from
    187  * userspace, this is required.
    188  *
    189  * At the end, we queue up the self refresh entry work so we can enter PSR after
    190  * the desired delay.
    191  */
    192 void drm_self_refresh_helper_alter_state(struct drm_atomic_state *state)
    193 {
    194 	struct drm_crtc *crtc;
    195 	struct drm_crtc_state *crtc_state;
    196 	int i;
    197 
    198 	if (state->async_update || !state->allow_modeset) {
    199 		for_each_old_crtc_in_state(state, crtc, crtc_state, i) {
    200 			if (crtc_state->self_refresh_active) {
    201 				state->async_update = false;
    202 				state->allow_modeset = true;
    203 				break;
    204 			}
    205 		}
    206 	}
    207 
    208 	for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
    209 		struct drm_self_refresh_data *sr_data;
    210 		unsigned int delay;
    211 
    212 		/* Don't trigger the entry timer when we're already in SR */
    213 		if (crtc_state->self_refresh_active)
    214 			continue;
    215 
    216 		sr_data = crtc->self_refresh_data;
    217 		if (!sr_data)
    218 			continue;
    219 
    220 		mutex_lock(&sr_data->avg_mutex);
    221 		delay = (ewma_psr_time_read(&sr_data->entry_avg_ms) +
    222 			 ewma_psr_time_read(&sr_data->exit_avg_ms)) * 2;
    223 		mutex_unlock(&sr_data->avg_mutex);
    224 
    225 		mod_delayed_work(system_wq, &sr_data->entry_work,
    226 				 msecs_to_jiffies(delay));
    227 	}
    228 }
    229 EXPORT_SYMBOL(drm_self_refresh_helper_alter_state);
    230 
    231 /**
    232  * drm_self_refresh_helper_init - Initializes self refresh helpers for a crtc
    233  * @crtc: the crtc which supports self refresh supported displays
    234  *
    235  * Returns zero if successful or -errno on failure
    236  */
    237 int drm_self_refresh_helper_init(struct drm_crtc *crtc)
    238 {
    239 	struct drm_self_refresh_data *sr_data = crtc->self_refresh_data;
    240 
    241 	/* Helper is already initialized */
    242 	if (WARN_ON(sr_data))
    243 		return -EINVAL;
    244 
    245 	sr_data = kzalloc(sizeof(*sr_data), GFP_KERNEL);
    246 	if (!sr_data)
    247 		return -ENOMEM;
    248 
    249 	INIT_DELAYED_WORK(&sr_data->entry_work,
    250 			  drm_self_refresh_helper_entry_work);
    251 	sr_data->crtc = crtc;
    252 	mutex_init(&sr_data->avg_mutex);
    253 	ewma_psr_time_init(&sr_data->entry_avg_ms);
    254 	ewma_psr_time_init(&sr_data->exit_avg_ms);
    255 
    256 	/*
    257 	 * Seed the averages so they're non-zero (and sufficiently large
    258 	 * for even poorly performing panels). As time goes on, this will be
    259 	 * averaged out and the values will trend to their true value.
    260 	 */
    261 	ewma_psr_time_add(&sr_data->entry_avg_ms, SELF_REFRESH_AVG_SEED_MS);
    262 	ewma_psr_time_add(&sr_data->exit_avg_ms, SELF_REFRESH_AVG_SEED_MS);
    263 
    264 	crtc->self_refresh_data = sr_data;
    265 	return 0;
    266 }
    267 EXPORT_SYMBOL(drm_self_refresh_helper_init);
    268 
    269 /**
    270  * drm_self_refresh_helper_cleanup - Cleans up self refresh helpers for a crtc
    271  * @crtc: the crtc to cleanup
    272  */
    273 void drm_self_refresh_helper_cleanup(struct drm_crtc *crtc)
    274 {
    275 	struct drm_self_refresh_data *sr_data = crtc->self_refresh_data;
    276 
    277 	/* Helper is already uninitialized */
    278 	if (!sr_data)
    279 		return;
    280 
    281 	crtc->self_refresh_data = NULL;
    282 
    283 	cancel_delayed_work_sync(&sr_data->entry_work);
    284 	kfree(sr_data);
    285 }
    286 EXPORT_SYMBOL(drm_self_refresh_helper_cleanup);
    287