Sean Paul | 1452c25 | 2019-06-12 10:50:19 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: MIT |
| 2 | /* |
| 3 | * Copyright (C) 2019 Google, Inc. |
| 4 | * |
| 5 | * Authors: |
| 6 | * Sean Paul <seanpaul@chromium.org> |
| 7 | */ |
| 8 | #include <linux/bitops.h> |
| 9 | #include <linux/slab.h> |
| 10 | #include <linux/workqueue.h> |
| 11 | |
| 12 | #include <drm/drm_atomic.h> |
| 13 | #include <drm/drm_atomic_helper.h> |
| 14 | #include <drm/drm_connector.h> |
| 15 | #include <drm/drm_crtc.h> |
| 16 | #include <drm/drm_device.h> |
| 17 | #include <drm/drm_mode_config.h> |
| 18 | #include <drm/drm_modeset_lock.h> |
| 19 | #include <drm/drm_print.h> |
| 20 | #include <drm/drm_self_refresh_helper.h> |
| 21 | |
| 22 | /** |
| 23 | * DOC: overview |
| 24 | * |
| 25 | * This helper library provides an easy way for drivers to leverage the atomic |
| 26 | * framework to implement panel self refresh (SR) support. Drivers are |
| 27 | * responsible for initializing and cleaning up the SR helpers on load/unload |
| 28 | * (see &drm_self_refresh_helper_init/&drm_self_refresh_helper_cleanup). |
| 29 | * The connector is responsible for setting |
| 30 | * &drm_connector_state.self_refresh_aware to true at runtime if it is SR-aware |
| 31 | * (meaning it knows how to initiate self refresh on the panel). |
| 32 | * |
| 33 | * Once a crtc has enabled SR using &drm_self_refresh_helper_init, the |
| 34 | * helpers will monitor activity and call back into the driver to enable/disable |
| 35 | * SR as appropriate. The best way to think about this is that it's a DPMS |
| 36 | * on/off request with &drm_crtc_state.self_refresh_active set in crtc state |
| 37 | * that tells you to disable/enable SR on the panel instead of power-cycling it. |
| 38 | * |
| 39 | * During SR, drivers may choose to fully disable their crtc/encoder/bridge |
| 40 | * hardware (in which case no driver changes are necessary), or they can inspect |
| 41 | * &drm_crtc_state.self_refresh_active if they want to enter low power mode |
| 42 | * without full disable (in case full disable/enable is too slow). |
| 43 | * |
| 44 | * SR will be deactivated if there are any atomic updates affecting the |
| 45 | * pipe that is in SR mode. If a crtc is driving multiple connectors, all |
| 46 | * connectors must be SR aware and all will enter/exit SR mode at the same time. |
| 47 | * |
| 48 | * If the crtc and connector are SR aware, but the panel connected does not |
| 49 | * support it (or is otherwise unable to enter SR), the driver should fail |
| 50 | * atomic_check when &drm_crtc_state.self_refresh_active is true. |
| 51 | */ |
| 52 | |
| 53 | struct drm_self_refresh_data { |
| 54 | struct drm_crtc *crtc; |
| 55 | struct delayed_work entry_work; |
| 56 | struct drm_atomic_state *save_state; |
| 57 | unsigned int entry_delay_ms; |
| 58 | }; |
| 59 | |
| 60 | static void drm_self_refresh_helper_entry_work(struct work_struct *work) |
| 61 | { |
| 62 | struct drm_self_refresh_data *sr_data = container_of( |
| 63 | to_delayed_work(work), |
| 64 | struct drm_self_refresh_data, entry_work); |
| 65 | struct drm_crtc *crtc = sr_data->crtc; |
| 66 | struct drm_device *dev = crtc->dev; |
| 67 | struct drm_modeset_acquire_ctx ctx; |
| 68 | struct drm_atomic_state *state; |
| 69 | struct drm_connector *conn; |
| 70 | struct drm_connector_state *conn_state; |
| 71 | struct drm_crtc_state *crtc_state; |
Sean Paul | 002c845 | 2019-06-19 14:19:47 -0400 | [diff] [blame] | 72 | int i, ret = 0; |
Sean Paul | 1452c25 | 2019-06-12 10:50:19 -0400 | [diff] [blame] | 73 | |
| 74 | drm_modeset_acquire_init(&ctx, 0); |
| 75 | |
| 76 | state = drm_atomic_state_alloc(dev); |
| 77 | if (!state) { |
| 78 | ret = -ENOMEM; |
Sean Paul | 002c845 | 2019-06-19 14:19:47 -0400 | [diff] [blame] | 79 | goto out_drop_locks; |
Sean Paul | 1452c25 | 2019-06-12 10:50:19 -0400 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | retry: |
| 83 | state->acquire_ctx = &ctx; |
| 84 | |
| 85 | crtc_state = drm_atomic_get_crtc_state(state, crtc); |
| 86 | if (IS_ERR(crtc_state)) { |
| 87 | ret = PTR_ERR(crtc_state); |
| 88 | goto out; |
| 89 | } |
| 90 | |
| 91 | if (!crtc_state->enable) |
| 92 | goto out; |
| 93 | |
| 94 | ret = drm_atomic_add_affected_connectors(state, crtc); |
| 95 | if (ret) |
| 96 | goto out; |
| 97 | |
| 98 | for_each_new_connector_in_state(state, conn, conn_state, i) { |
| 99 | if (!conn_state->self_refresh_aware) |
| 100 | goto out; |
| 101 | } |
| 102 | |
| 103 | crtc_state->active = false; |
| 104 | crtc_state->self_refresh_active = true; |
| 105 | |
| 106 | ret = drm_atomic_commit(state); |
| 107 | if (ret) |
| 108 | goto out; |
| 109 | |
| 110 | out: |
| 111 | if (ret == -EDEADLK) { |
| 112 | drm_atomic_state_clear(state); |
| 113 | ret = drm_modeset_backoff(&ctx); |
| 114 | if (!ret) |
| 115 | goto retry; |
| 116 | } |
| 117 | |
| 118 | drm_atomic_state_put(state); |
Sean Paul | 002c845 | 2019-06-19 14:19:47 -0400 | [diff] [blame] | 119 | |
| 120 | out_drop_locks: |
Sean Paul | 1452c25 | 2019-06-12 10:50:19 -0400 | [diff] [blame] | 121 | drm_modeset_drop_locks(&ctx); |
| 122 | drm_modeset_acquire_fini(&ctx); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * drm_self_refresh_helper_alter_state - Alters the atomic state for SR exit |
| 127 | * @state: the state currently being checked |
| 128 | * |
| 129 | * Called at the end of atomic check. This function checks the state for flags |
| 130 | * incompatible with self refresh exit and changes them. This is a bit |
| 131 | * disingenuous since userspace is expecting one thing and we're giving it |
| 132 | * another. However in order to keep self refresh entirely hidden from |
| 133 | * userspace, this is required. |
| 134 | * |
| 135 | * At the end, we queue up the self refresh entry work so we can enter PSR after |
| 136 | * the desired delay. |
| 137 | */ |
| 138 | void drm_self_refresh_helper_alter_state(struct drm_atomic_state *state) |
| 139 | { |
| 140 | struct drm_crtc *crtc; |
| 141 | struct drm_crtc_state *crtc_state; |
| 142 | int i; |
| 143 | |
| 144 | if (state->async_update || !state->allow_modeset) { |
| 145 | for_each_old_crtc_in_state(state, crtc, crtc_state, i) { |
| 146 | if (crtc_state->self_refresh_active) { |
| 147 | state->async_update = false; |
| 148 | state->allow_modeset = true; |
| 149 | break; |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | for_each_new_crtc_in_state(state, crtc, crtc_state, i) { |
| 155 | struct drm_self_refresh_data *sr_data; |
| 156 | |
| 157 | /* Don't trigger the entry timer when we're already in SR */ |
| 158 | if (crtc_state->self_refresh_active) |
| 159 | continue; |
| 160 | |
| 161 | sr_data = crtc->self_refresh_data; |
| 162 | if (!sr_data) |
| 163 | continue; |
| 164 | |
| 165 | mod_delayed_work(system_wq, &sr_data->entry_work, |
| 166 | msecs_to_jiffies(sr_data->entry_delay_ms)); |
| 167 | } |
| 168 | } |
| 169 | EXPORT_SYMBOL(drm_self_refresh_helper_alter_state); |
| 170 | |
| 171 | /** |
| 172 | * drm_self_refresh_helper_init - Initializes self refresh helpers for a crtc |
| 173 | * @crtc: the crtc which supports self refresh supported displays |
| 174 | * @entry_delay_ms: amount of inactivity to wait before entering self refresh |
| 175 | * |
| 176 | * Returns zero if successful or -errno on failure |
| 177 | */ |
| 178 | int drm_self_refresh_helper_init(struct drm_crtc *crtc, |
| 179 | unsigned int entry_delay_ms) |
| 180 | { |
| 181 | struct drm_self_refresh_data *sr_data = crtc->self_refresh_data; |
| 182 | |
| 183 | /* Helper is already initialized */ |
| 184 | if (WARN_ON(sr_data)) |
| 185 | return -EINVAL; |
| 186 | |
| 187 | sr_data = kzalloc(sizeof(*sr_data), GFP_KERNEL); |
| 188 | if (!sr_data) |
| 189 | return -ENOMEM; |
| 190 | |
| 191 | INIT_DELAYED_WORK(&sr_data->entry_work, |
| 192 | drm_self_refresh_helper_entry_work); |
| 193 | sr_data->entry_delay_ms = entry_delay_ms; |
| 194 | sr_data->crtc = crtc; |
| 195 | |
| 196 | crtc->self_refresh_data = sr_data; |
| 197 | return 0; |
| 198 | } |
| 199 | EXPORT_SYMBOL(drm_self_refresh_helper_init); |
| 200 | |
| 201 | /** |
| 202 | * drm_self_refresh_helper_cleanup - Cleans up self refresh helpers for a crtc |
| 203 | * @crtc: the crtc to cleanup |
| 204 | */ |
| 205 | void drm_self_refresh_helper_cleanup(struct drm_crtc *crtc) |
| 206 | { |
| 207 | struct drm_self_refresh_data *sr_data = crtc->self_refresh_data; |
| 208 | |
| 209 | /* Helper is already uninitialized */ |
Dan Carpenter | 7d0ed30 | 2019-06-19 13:01:41 +0300 | [diff] [blame] | 210 | if (!sr_data) |
Sean Paul | 1452c25 | 2019-06-12 10:50:19 -0400 | [diff] [blame] | 211 | return; |
| 212 | |
| 213 | crtc->self_refresh_data = NULL; |
| 214 | |
| 215 | cancel_delayed_work_sync(&sr_data->entry_work); |
| 216 | kfree(sr_data); |
| 217 | } |
| 218 | EXPORT_SYMBOL(drm_self_refresh_helper_cleanup); |