177704Seric// SPDX-License-Identifier: MIT
277704Seric/*
377704Seric * Copyright (C) 2019 Google, Inc.
477704Seric *
577704Seric * Authors:
677704Seric * Sean Paul <seanpaul@chromium.org>
777704Seric */
877704Seric#include <linux/average.h>
977704Seric#include <linux/bitops.h>
1077704Seric#include <linux/slab.h>
1177704Seric#include <linux/workqueue.h>
1277704Seric
1377704Seric#include <drm/drm_atomic.h>
1477704Seric#include <drm/drm_atomic_helper.h>
1577704Seric#include <drm/drm_connector.h>
1677704Seric#include <drm/drm_crtc.h>
1777704Seric#include <drm/drm_device.h>
1877704Seric#include <drm/drm_mode_config.h>
1977704Seric#include <drm/drm_modeset_lock.h>
2077704Seric#include <drm/drm_print.h>
2177704Seric#include <drm/drm_self_refresh_helper.h>
2277704Seric
2377704Seric/**
2477704Seric * DOC: overview
2577704Seric *
2677704Seric * This helper library provides an easy way for drivers to leverage the atomic
2777704Seric * framework to implement panel self refresh (SR) support. Drivers are
2877704Seric * responsible for initializing and cleaning up the SR helpers on load/unload
2977704Seric * (see &drm_self_refresh_helper_init/&drm_self_refresh_helper_cleanup).
3077704Seric * The connector is responsible for setting
3177704Seric * &drm_connector_state.self_refresh_aware to true at runtime if it is SR-aware
3277704Seric * (meaning it knows how to initiate self refresh on the panel).
3377704Seric *
3477704Seric * Once a crtc has enabled SR using &drm_self_refresh_helper_init, the
3577704Seric * helpers will monitor activity and call back into the driver to enable/disable
3677704Seric * SR as appropriate. The best way to think about this is that it's a DPMS
3777704Seric * on/off request with &drm_crtc_state.self_refresh_active set in crtc state
3877704Seric * that tells you to disable/enable SR on the panel instead of power-cycling it.
3977704Seric *
4077704Seric * During SR, drivers may choose to fully disable their crtc/encoder/bridge
4177704Seric * hardware (in which case no driver changes are necessary), or they can inspect
4277704Seric * &drm_crtc_state.self_refresh_active if they want to enter low power mode
4377704Seric * without full disable (in case full disable/enable is too slow).
4477704Seric *
4577704Seric * SR will be deactivated if there are any atomic updates affecting the
4677704Seric * pipe that is in SR mode. If a crtc is driving multiple connectors, all
4777704Seric * connectors must be SR aware and all will enter/exit SR mode at the same time.
4877704Seric *
4977704Seric * If the crtc and connector are SR aware, but the panel connected does not
5077704Seric * support it (or is otherwise unable to enter SR), the driver should fail
5177704Seric * atomic_check when &drm_crtc_state.self_refresh_active is true.
5277704Seric */
5377704Seric
5477704Seric#define SELF_REFRESH_AVG_SEED_MS 200
5577704Seric
5677704SericDECLARE_EWMA(psr_time, 4, 4)
5777704Seric
5877704Sericstruct drm_self_refresh_data {
5977704Seric	struct drm_crtc *crtc;
6077704Seric	struct delayed_work entry_work;
6177704Seric
6277704Seric	struct mutex avg_mutex;
6377704Seric	struct ewma_psr_time entry_avg_ms;
6477704Seric	struct ewma_psr_time exit_avg_ms;
6577704Seric};
6677704Seric
6777704Sericstatic void drm_self_refresh_helper_entry_work(struct work_struct *work)
6877704Seric{
6977704Seric	struct drm_self_refresh_data *sr_data = container_of(
7077704Seric				to_delayed_work(work),
7177704Seric				struct drm_self_refresh_data, entry_work);
7277704Seric	struct drm_crtc *crtc = sr_data->crtc;
7377704Seric	struct drm_device *dev = crtc->dev;
7477704Seric	struct drm_modeset_acquire_ctx ctx;
7577704Seric	struct drm_atomic_state *state;
7677704Seric	struct drm_connector *conn;
7777704Seric	struct drm_connector_state *conn_state;
7877704Seric	struct drm_crtc_state *crtc_state;
7977704Seric	int i, ret = 0;
8077704Seric
8177704Seric	drm_modeset_acquire_init(&ctx, 0);
8277704Seric
8377704Seric	state = drm_atomic_state_alloc(dev);
8477704Seric	if (!state) {
8577704Seric		ret = -ENOMEM;
8677704Seric		goto out_drop_locks;
8777704Seric	}
8877704Seric
8977704Sericretry:
9077704Seric	state->acquire_ctx = &ctx;
9177704Seric
9277704Seric	crtc_state = drm_atomic_get_crtc_state(state, crtc);
9377704Seric	if (IS_ERR(crtc_state)) {
9477704Seric		ret = PTR_ERR(crtc_state);
9577704Seric		goto out;
9677704Seric	}
9777704Seric
9877704Seric	if (!crtc_state->enable)
9977704Seric		goto out;
10077704Seric
10177704Seric	ret = drm_atomic_add_affected_connectors(state, crtc);
10277704Seric	if (ret)
10377704Seric		goto out;
10477704Seric
10577704Seric	for_each_new_connector_in_state(state, conn, conn_state, i) {
10677704Seric		if (!conn_state->self_refresh_aware)
10777704Seric			goto out;
10877704Seric	}
10977704Seric
11077704Seric	crtc_state->active = false;
11177704Seric	crtc_state->self_refresh_active = true;
11277704Seric
11377704Seric	ret = drm_atomic_commit(state);
11477704Seric	if (ret)
11577704Seric		goto out;
11677704Seric
11777704Sericout:
11877704Seric	if (ret == -EDEADLK) {
11977704Seric		drm_atomic_state_clear(state);
12077704Seric		ret = drm_modeset_backoff(&ctx);
12177704Seric		if (!ret)
12277704Seric			goto retry;
12377704Seric	}
12477704Seric
12577704Seric	drm_atomic_state_put(state);
12677704Seric
12777704Sericout_drop_locks:
12877704Seric	drm_modeset_drop_locks(&ctx);
12977704Seric	drm_modeset_acquire_fini(&ctx);
13077704Seric}
13177704Seric
13277704Seric/**
13377704Seric * drm_self_refresh_helper_update_avg_times - Updates a crtc's SR time averages
13477704Seric * @state: the state which has just been applied to hardware
13577704Seric * @commit_time_ms: the amount of time in ms that this commit took to complete
13677704Seric * @new_self_refresh_mask: bitmask of crtc's that have self_refresh_active in
13777704Seric *    new state
13877704Seric *
13977704Seric * Called after &drm_mode_config_funcs.atomic_commit_tail, this function will
14077704Seric * update the average entry/exit self refresh times on self refresh transitions.
14177704Seric * These averages will be used when calculating how long to delay before
14277704Seric * entering self refresh mode after activity.
14377704Seric */
14477704Sericvoid
14577704Sericdrm_self_refresh_helper_update_avg_times(struct drm_atomic_state *state,
14677704Seric					 unsigned int commit_time_ms,
14777704Seric					 unsigned int new_self_refresh_mask)
14877704Seric{
14977704Seric	struct drm_crtc *crtc;
15077704Seric	struct drm_crtc_state *old_crtc_state;
15177704Seric	int i;
15277704Seric
15377704Seric	for_each_old_crtc_in_state(state, crtc, old_crtc_state, i) {
15477704Seric		bool new_self_refresh_active = new_self_refresh_mask & BIT(i);
15577704Seric		struct drm_self_refresh_data *sr_data = crtc->self_refresh_data;
15677704Seric		struct ewma_psr_time *time;
15777704Seric
15877704Seric		if (old_crtc_state->self_refresh_active ==
15977704Seric		    new_self_refresh_active)
16077704Seric			continue;
16177704Seric
16277704Seric		if (new_self_refresh_active)
16377704Seric			time = &sr_data->entry_avg_ms;
16477704Seric		else
16577704Seric			time = &sr_data->exit_avg_ms;
16677704Seric
16777704Seric		mutex_lock(&sr_data->avg_mutex);
16877704Seric		ewma_psr_time_add(time, commit_time_ms);
16977704Seric		mutex_unlock(&sr_data->avg_mutex);
17077704Seric	}
17177704Seric}
17277704SericEXPORT_SYMBOL(drm_self_refresh_helper_update_avg_times);
17377704Seric
17477704Seric/**
17577704Seric * drm_self_refresh_helper_alter_state - Alters the atomic state for SR exit
17677704Seric * @state: the state currently being checked
17777704Seric *
17877704Seric * Called at the end of atomic check. This function checks the state for flags
17977704Seric * incompatible with self refresh exit and changes them. This is a bit
18077704Seric * disingenuous since userspace is expecting one thing and we're giving it
181 * another. However in order to keep self refresh entirely hidden from
182 * userspace, this is required.
183 *
184 * At the end, we queue up the self refresh entry work so we can enter PSR after
185 * the desired delay.
186 */
187void drm_self_refresh_helper_alter_state(struct drm_atomic_state *state)
188{
189	struct drm_crtc *crtc;
190	struct drm_crtc_state *crtc_state;
191	int i;
192
193	if (state->async_update || !state->allow_modeset) {
194		for_each_old_crtc_in_state(state, crtc, crtc_state, i) {
195			if (crtc_state->self_refresh_active) {
196				state->async_update = false;
197				state->allow_modeset = true;
198				break;
199			}
200		}
201	}
202
203	for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
204		struct drm_self_refresh_data *sr_data;
205		unsigned int delay;
206
207		/* Don't trigger the entry timer when we're already in SR */
208		if (crtc_state->self_refresh_active)
209			continue;
210
211		sr_data = crtc->self_refresh_data;
212		if (!sr_data)
213			continue;
214
215		mutex_lock(&sr_data->avg_mutex);
216		delay = (ewma_psr_time_read(&sr_data->entry_avg_ms) +
217			 ewma_psr_time_read(&sr_data->exit_avg_ms)) * 2;
218		mutex_unlock(&sr_data->avg_mutex);
219
220		mod_delayed_work(system_wq, &sr_data->entry_work,
221				 msecs_to_jiffies(delay));
222	}
223}
224EXPORT_SYMBOL(drm_self_refresh_helper_alter_state);
225
226/**
227 * drm_self_refresh_helper_init - Initializes self refresh helpers for a crtc
228 * @crtc: the crtc which supports self refresh supported displays
229 *
230 * Returns zero if successful or -errno on failure
231 */
232int drm_self_refresh_helper_init(struct drm_crtc *crtc)
233{
234	struct drm_self_refresh_data *sr_data = crtc->self_refresh_data;
235
236	/* Helper is already initialized */
237	if (WARN_ON(sr_data))
238		return -EINVAL;
239
240	sr_data = kzalloc(sizeof(*sr_data), GFP_KERNEL);
241	if (!sr_data)
242		return -ENOMEM;
243
244	INIT_DELAYED_WORK(&sr_data->entry_work,
245			  drm_self_refresh_helper_entry_work);
246	sr_data->crtc = crtc;
247	mutex_init(&sr_data->avg_mutex);
248	ewma_psr_time_init(&sr_data->entry_avg_ms);
249	ewma_psr_time_init(&sr_data->exit_avg_ms);
250
251	/*
252	 * Seed the averages so they're non-zero (and sufficiently large
253	 * for even poorly performing panels). As time goes on, this will be
254	 * averaged out and the values will trend to their true value.
255	 */
256	ewma_psr_time_add(&sr_data->entry_avg_ms, SELF_REFRESH_AVG_SEED_MS);
257	ewma_psr_time_add(&sr_data->exit_avg_ms, SELF_REFRESH_AVG_SEED_MS);
258
259	crtc->self_refresh_data = sr_data;
260	return 0;
261}
262EXPORT_SYMBOL(drm_self_refresh_helper_init);
263
264/**
265 * drm_self_refresh_helper_cleanup - Cleans up self refresh helpers for a crtc
266 * @crtc: the crtc to cleanup
267 */
268void drm_self_refresh_helper_cleanup(struct drm_crtc *crtc)
269{
270	struct drm_self_refresh_data *sr_data = crtc->self_refresh_data;
271
272	/* Helper is already uninitialized */
273	if (!sr_data)
274		return;
275
276	crtc->self_refresh_data = NULL;
277
278	cancel_delayed_work_sync(&sr_data->entry_work);
279	kfree(sr_data);
280}
281EXPORT_SYMBOL(drm_self_refresh_helper_cleanup);
282