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