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