162306a36Sopenharmony_ci// SPDX-License-Identifier: MIT
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (C) 2019 Google, Inc.
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Authors:
662306a36Sopenharmony_ci * Sean Paul <seanpaul@chromium.org>
762306a36Sopenharmony_ci */
862306a36Sopenharmony_ci#include <linux/average.h>
962306a36Sopenharmony_ci#include <linux/bitops.h>
1062306a36Sopenharmony_ci#include <linux/slab.h>
1162306a36Sopenharmony_ci#include <linux/workqueue.h>
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_ci#include <drm/drm_atomic.h>
1462306a36Sopenharmony_ci#include <drm/drm_atomic_helper.h>
1562306a36Sopenharmony_ci#include <drm/drm_connector.h>
1662306a36Sopenharmony_ci#include <drm/drm_crtc.h>
1762306a36Sopenharmony_ci#include <drm/drm_device.h>
1862306a36Sopenharmony_ci#include <drm/drm_mode_config.h>
1962306a36Sopenharmony_ci#include <drm/drm_modeset_lock.h>
2062306a36Sopenharmony_ci#include <drm/drm_print.h>
2162306a36Sopenharmony_ci#include <drm/drm_self_refresh_helper.h>
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_ci/**
2462306a36Sopenharmony_ci * DOC: overview
2562306a36Sopenharmony_ci *
2662306a36Sopenharmony_ci * This helper library provides an easy way for drivers to leverage the atomic
2762306a36Sopenharmony_ci * framework to implement panel self refresh (SR) support. Drivers are
2862306a36Sopenharmony_ci * responsible for initializing and cleaning up the SR helpers on load/unload
2962306a36Sopenharmony_ci * (see &drm_self_refresh_helper_init/&drm_self_refresh_helper_cleanup).
3062306a36Sopenharmony_ci * The connector is responsible for setting
3162306a36Sopenharmony_ci * &drm_connector_state.self_refresh_aware to true at runtime if it is SR-aware
3262306a36Sopenharmony_ci * (meaning it knows how to initiate self refresh on the panel).
3362306a36Sopenharmony_ci *
3462306a36Sopenharmony_ci * Once a crtc has enabled SR using &drm_self_refresh_helper_init, the
3562306a36Sopenharmony_ci * helpers will monitor activity and call back into the driver to enable/disable
3662306a36Sopenharmony_ci * SR as appropriate. The best way to think about this is that it's a DPMS
3762306a36Sopenharmony_ci * on/off request with &drm_crtc_state.self_refresh_active set in crtc state
3862306a36Sopenharmony_ci * that tells you to disable/enable SR on the panel instead of power-cycling it.
3962306a36Sopenharmony_ci *
4062306a36Sopenharmony_ci * During SR, drivers may choose to fully disable their crtc/encoder/bridge
4162306a36Sopenharmony_ci * hardware (in which case no driver changes are necessary), or they can inspect
4262306a36Sopenharmony_ci * &drm_crtc_state.self_refresh_active if they want to enter low power mode
4362306a36Sopenharmony_ci * without full disable (in case full disable/enable is too slow).
4462306a36Sopenharmony_ci *
4562306a36Sopenharmony_ci * SR will be deactivated if there are any atomic updates affecting the
4662306a36Sopenharmony_ci * pipe that is in SR mode. If a crtc is driving multiple connectors, all
4762306a36Sopenharmony_ci * connectors must be SR aware and all will enter/exit SR mode at the same time.
4862306a36Sopenharmony_ci *
4962306a36Sopenharmony_ci * If the crtc and connector are SR aware, but the panel connected does not
5062306a36Sopenharmony_ci * support it (or is otherwise unable to enter SR), the driver should fail
5162306a36Sopenharmony_ci * atomic_check when &drm_crtc_state.self_refresh_active is true.
5262306a36Sopenharmony_ci */
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_ci#define SELF_REFRESH_AVG_SEED_MS 200
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ciDECLARE_EWMA(psr_time, 4, 4)
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_cistruct drm_self_refresh_data {
5962306a36Sopenharmony_ci	struct drm_crtc *crtc;
6062306a36Sopenharmony_ci	struct delayed_work entry_work;
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_ci	struct mutex avg_mutex;
6362306a36Sopenharmony_ci	struct ewma_psr_time entry_avg_ms;
6462306a36Sopenharmony_ci	struct ewma_psr_time exit_avg_ms;
6562306a36Sopenharmony_ci};
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_cistatic void drm_self_refresh_helper_entry_work(struct work_struct *work)
6862306a36Sopenharmony_ci{
6962306a36Sopenharmony_ci	struct drm_self_refresh_data *sr_data = container_of(
7062306a36Sopenharmony_ci				to_delayed_work(work),
7162306a36Sopenharmony_ci				struct drm_self_refresh_data, entry_work);
7262306a36Sopenharmony_ci	struct drm_crtc *crtc = sr_data->crtc;
7362306a36Sopenharmony_ci	struct drm_device *dev = crtc->dev;
7462306a36Sopenharmony_ci	struct drm_modeset_acquire_ctx ctx;
7562306a36Sopenharmony_ci	struct drm_atomic_state *state;
7662306a36Sopenharmony_ci	struct drm_connector *conn;
7762306a36Sopenharmony_ci	struct drm_connector_state *conn_state;
7862306a36Sopenharmony_ci	struct drm_crtc_state *crtc_state;
7962306a36Sopenharmony_ci	int i, ret = 0;
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_ci	drm_modeset_acquire_init(&ctx, 0);
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_ci	state = drm_atomic_state_alloc(dev);
8462306a36Sopenharmony_ci	if (!state) {
8562306a36Sopenharmony_ci		ret = -ENOMEM;
8662306a36Sopenharmony_ci		goto out_drop_locks;
8762306a36Sopenharmony_ci	}
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_ciretry:
9062306a36Sopenharmony_ci	state->acquire_ctx = &ctx;
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ci	crtc_state = drm_atomic_get_crtc_state(state, crtc);
9362306a36Sopenharmony_ci	if (IS_ERR(crtc_state)) {
9462306a36Sopenharmony_ci		ret = PTR_ERR(crtc_state);
9562306a36Sopenharmony_ci		goto out;
9662306a36Sopenharmony_ci	}
9762306a36Sopenharmony_ci
9862306a36Sopenharmony_ci	if (!crtc_state->enable)
9962306a36Sopenharmony_ci		goto out;
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ci	ret = drm_atomic_add_affected_connectors(state, crtc);
10262306a36Sopenharmony_ci	if (ret)
10362306a36Sopenharmony_ci		goto out;
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_ci	for_each_new_connector_in_state(state, conn, conn_state, i) {
10662306a36Sopenharmony_ci		if (!conn_state->self_refresh_aware)
10762306a36Sopenharmony_ci			goto out;
10862306a36Sopenharmony_ci	}
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_ci	crtc_state->active = false;
11162306a36Sopenharmony_ci	crtc_state->self_refresh_active = true;
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_ci	ret = drm_atomic_commit(state);
11462306a36Sopenharmony_ci	if (ret)
11562306a36Sopenharmony_ci		goto out;
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_ciout:
11862306a36Sopenharmony_ci	if (ret == -EDEADLK) {
11962306a36Sopenharmony_ci		drm_atomic_state_clear(state);
12062306a36Sopenharmony_ci		ret = drm_modeset_backoff(&ctx);
12162306a36Sopenharmony_ci		if (!ret)
12262306a36Sopenharmony_ci			goto retry;
12362306a36Sopenharmony_ci	}
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_ci	drm_atomic_state_put(state);
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_ciout_drop_locks:
12862306a36Sopenharmony_ci	drm_modeset_drop_locks(&ctx);
12962306a36Sopenharmony_ci	drm_modeset_acquire_fini(&ctx);
13062306a36Sopenharmony_ci}
13162306a36Sopenharmony_ci
13262306a36Sopenharmony_ci/**
13362306a36Sopenharmony_ci * drm_self_refresh_helper_update_avg_times - Updates a crtc's SR time averages
13462306a36Sopenharmony_ci * @state: the state which has just been applied to hardware
13562306a36Sopenharmony_ci * @commit_time_ms: the amount of time in ms that this commit took to complete
13662306a36Sopenharmony_ci * @new_self_refresh_mask: bitmask of crtc's that have self_refresh_active in
13762306a36Sopenharmony_ci *    new state
13862306a36Sopenharmony_ci *
13962306a36Sopenharmony_ci * Called after &drm_mode_config_funcs.atomic_commit_tail, this function will
14062306a36Sopenharmony_ci * update the average entry/exit self refresh times on self refresh transitions.
14162306a36Sopenharmony_ci * These averages will be used when calculating how long to delay before
14262306a36Sopenharmony_ci * entering self refresh mode after activity.
14362306a36Sopenharmony_ci */
14462306a36Sopenharmony_civoid
14562306a36Sopenharmony_cidrm_self_refresh_helper_update_avg_times(struct drm_atomic_state *state,
14662306a36Sopenharmony_ci					 unsigned int commit_time_ms,
14762306a36Sopenharmony_ci					 unsigned int new_self_refresh_mask)
14862306a36Sopenharmony_ci{
14962306a36Sopenharmony_ci	struct drm_crtc *crtc;
15062306a36Sopenharmony_ci	struct drm_crtc_state *old_crtc_state;
15162306a36Sopenharmony_ci	int i;
15262306a36Sopenharmony_ci
15362306a36Sopenharmony_ci	for_each_old_crtc_in_state(state, crtc, old_crtc_state, i) {
15462306a36Sopenharmony_ci		bool new_self_refresh_active = new_self_refresh_mask & BIT(i);
15562306a36Sopenharmony_ci		struct drm_self_refresh_data *sr_data = crtc->self_refresh_data;
15662306a36Sopenharmony_ci		struct ewma_psr_time *time;
15762306a36Sopenharmony_ci
15862306a36Sopenharmony_ci		if (old_crtc_state->self_refresh_active ==
15962306a36Sopenharmony_ci		    new_self_refresh_active)
16062306a36Sopenharmony_ci			continue;
16162306a36Sopenharmony_ci
16262306a36Sopenharmony_ci		if (new_self_refresh_active)
16362306a36Sopenharmony_ci			time = &sr_data->entry_avg_ms;
16462306a36Sopenharmony_ci		else
16562306a36Sopenharmony_ci			time = &sr_data->exit_avg_ms;
16662306a36Sopenharmony_ci
16762306a36Sopenharmony_ci		mutex_lock(&sr_data->avg_mutex);
16862306a36Sopenharmony_ci		ewma_psr_time_add(time, commit_time_ms);
16962306a36Sopenharmony_ci		mutex_unlock(&sr_data->avg_mutex);
17062306a36Sopenharmony_ci	}
17162306a36Sopenharmony_ci}
17262306a36Sopenharmony_ciEXPORT_SYMBOL(drm_self_refresh_helper_update_avg_times);
17362306a36Sopenharmony_ci
17462306a36Sopenharmony_ci/**
17562306a36Sopenharmony_ci * drm_self_refresh_helper_alter_state - Alters the atomic state for SR exit
17662306a36Sopenharmony_ci * @state: the state currently being checked
17762306a36Sopenharmony_ci *
17862306a36Sopenharmony_ci * Called at the end of atomic check. This function checks the state for flags
17962306a36Sopenharmony_ci * incompatible with self refresh exit and changes them. This is a bit
18062306a36Sopenharmony_ci * disingenuous since userspace is expecting one thing and we're giving it
18162306a36Sopenharmony_ci * another. However in order to keep self refresh entirely hidden from
18262306a36Sopenharmony_ci * userspace, this is required.
18362306a36Sopenharmony_ci *
18462306a36Sopenharmony_ci * At the end, we queue up the self refresh entry work so we can enter PSR after
18562306a36Sopenharmony_ci * the desired delay.
18662306a36Sopenharmony_ci */
18762306a36Sopenharmony_civoid drm_self_refresh_helper_alter_state(struct drm_atomic_state *state)
18862306a36Sopenharmony_ci{
18962306a36Sopenharmony_ci	struct drm_crtc *crtc;
19062306a36Sopenharmony_ci	struct drm_crtc_state *crtc_state;
19162306a36Sopenharmony_ci	int i;
19262306a36Sopenharmony_ci
19362306a36Sopenharmony_ci	if (state->async_update || !state->allow_modeset) {
19462306a36Sopenharmony_ci		for_each_old_crtc_in_state(state, crtc, crtc_state, i) {
19562306a36Sopenharmony_ci			if (crtc_state->self_refresh_active) {
19662306a36Sopenharmony_ci				state->async_update = false;
19762306a36Sopenharmony_ci				state->allow_modeset = true;
19862306a36Sopenharmony_ci				break;
19962306a36Sopenharmony_ci			}
20062306a36Sopenharmony_ci		}
20162306a36Sopenharmony_ci	}
20262306a36Sopenharmony_ci
20362306a36Sopenharmony_ci	for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
20462306a36Sopenharmony_ci		struct drm_self_refresh_data *sr_data;
20562306a36Sopenharmony_ci		unsigned int delay;
20662306a36Sopenharmony_ci
20762306a36Sopenharmony_ci		/* Don't trigger the entry timer when we're already in SR */
20862306a36Sopenharmony_ci		if (crtc_state->self_refresh_active)
20962306a36Sopenharmony_ci			continue;
21062306a36Sopenharmony_ci
21162306a36Sopenharmony_ci		sr_data = crtc->self_refresh_data;
21262306a36Sopenharmony_ci		if (!sr_data)
21362306a36Sopenharmony_ci			continue;
21462306a36Sopenharmony_ci
21562306a36Sopenharmony_ci		mutex_lock(&sr_data->avg_mutex);
21662306a36Sopenharmony_ci		delay = (ewma_psr_time_read(&sr_data->entry_avg_ms) +
21762306a36Sopenharmony_ci			 ewma_psr_time_read(&sr_data->exit_avg_ms)) * 2;
21862306a36Sopenharmony_ci		mutex_unlock(&sr_data->avg_mutex);
21962306a36Sopenharmony_ci
22062306a36Sopenharmony_ci		mod_delayed_work(system_wq, &sr_data->entry_work,
22162306a36Sopenharmony_ci				 msecs_to_jiffies(delay));
22262306a36Sopenharmony_ci	}
22362306a36Sopenharmony_ci}
22462306a36Sopenharmony_ciEXPORT_SYMBOL(drm_self_refresh_helper_alter_state);
22562306a36Sopenharmony_ci
22662306a36Sopenharmony_ci/**
22762306a36Sopenharmony_ci * drm_self_refresh_helper_init - Initializes self refresh helpers for a crtc
22862306a36Sopenharmony_ci * @crtc: the crtc which supports self refresh supported displays
22962306a36Sopenharmony_ci *
23062306a36Sopenharmony_ci * Returns zero if successful or -errno on failure
23162306a36Sopenharmony_ci */
23262306a36Sopenharmony_ciint drm_self_refresh_helper_init(struct drm_crtc *crtc)
23362306a36Sopenharmony_ci{
23462306a36Sopenharmony_ci	struct drm_self_refresh_data *sr_data = crtc->self_refresh_data;
23562306a36Sopenharmony_ci
23662306a36Sopenharmony_ci	/* Helper is already initialized */
23762306a36Sopenharmony_ci	if (WARN_ON(sr_data))
23862306a36Sopenharmony_ci		return -EINVAL;
23962306a36Sopenharmony_ci
24062306a36Sopenharmony_ci	sr_data = kzalloc(sizeof(*sr_data), GFP_KERNEL);
24162306a36Sopenharmony_ci	if (!sr_data)
24262306a36Sopenharmony_ci		return -ENOMEM;
24362306a36Sopenharmony_ci
24462306a36Sopenharmony_ci	INIT_DELAYED_WORK(&sr_data->entry_work,
24562306a36Sopenharmony_ci			  drm_self_refresh_helper_entry_work);
24662306a36Sopenharmony_ci	sr_data->crtc = crtc;
24762306a36Sopenharmony_ci	mutex_init(&sr_data->avg_mutex);
24862306a36Sopenharmony_ci	ewma_psr_time_init(&sr_data->entry_avg_ms);
24962306a36Sopenharmony_ci	ewma_psr_time_init(&sr_data->exit_avg_ms);
25062306a36Sopenharmony_ci
25162306a36Sopenharmony_ci	/*
25262306a36Sopenharmony_ci	 * Seed the averages so they're non-zero (and sufficiently large
25362306a36Sopenharmony_ci	 * for even poorly performing panels). As time goes on, this will be
25462306a36Sopenharmony_ci	 * averaged out and the values will trend to their true value.
25562306a36Sopenharmony_ci	 */
25662306a36Sopenharmony_ci	ewma_psr_time_add(&sr_data->entry_avg_ms, SELF_REFRESH_AVG_SEED_MS);
25762306a36Sopenharmony_ci	ewma_psr_time_add(&sr_data->exit_avg_ms, SELF_REFRESH_AVG_SEED_MS);
25862306a36Sopenharmony_ci
25962306a36Sopenharmony_ci	crtc->self_refresh_data = sr_data;
26062306a36Sopenharmony_ci	return 0;
26162306a36Sopenharmony_ci}
26262306a36Sopenharmony_ciEXPORT_SYMBOL(drm_self_refresh_helper_init);
26362306a36Sopenharmony_ci
26462306a36Sopenharmony_ci/**
26562306a36Sopenharmony_ci * drm_self_refresh_helper_cleanup - Cleans up self refresh helpers for a crtc
26662306a36Sopenharmony_ci * @crtc: the crtc to cleanup
26762306a36Sopenharmony_ci */
26862306a36Sopenharmony_civoid drm_self_refresh_helper_cleanup(struct drm_crtc *crtc)
26962306a36Sopenharmony_ci{
27062306a36Sopenharmony_ci	struct drm_self_refresh_data *sr_data = crtc->self_refresh_data;
27162306a36Sopenharmony_ci
27262306a36Sopenharmony_ci	/* Helper is already uninitialized */
27362306a36Sopenharmony_ci	if (!sr_data)
27462306a36Sopenharmony_ci		return;
27562306a36Sopenharmony_ci
27662306a36Sopenharmony_ci	crtc->self_refresh_data = NULL;
27762306a36Sopenharmony_ci
27862306a36Sopenharmony_ci	cancel_delayed_work_sync(&sr_data->entry_work);
27962306a36Sopenharmony_ci	kfree(sr_data);
28062306a36Sopenharmony_ci}
28162306a36Sopenharmony_ciEXPORT_SYMBOL(drm_self_refresh_helper_cleanup);
282