Daniel Vetter | c2fcd27 | 2014-11-05 00:14:14 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 Red Hat |
| 3 | * Copyright (C) 2014 Intel Corp. |
| 4 | * |
| 5 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 6 | * copy of this software and associated documentation files (the "Software"), |
| 7 | * to deal in the Software without restriction, including without limitation |
| 8 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 9 | * and/or sell copies of the Software, and to permit persons to whom the |
| 10 | * Software is furnished to do so, subject to the following conditions: |
| 11 | * |
| 12 | * The above copyright notice and this permission notice shall be included in |
| 13 | * all copies or substantial portions of the Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 19 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 20 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 21 | * OTHER DEALINGS IN THE SOFTWARE. |
| 22 | * |
| 23 | * Authors: |
| 24 | * Rob Clark <robdclark@gmail.com> |
| 25 | * Daniel Vetter <daniel.vetter@ffwll.ch> |
| 26 | */ |
| 27 | |
| 28 | #include <drm/drmP.h> |
| 29 | #include <drm/drm_atomic.h> |
| 30 | #include <drm/drm_plane_helper.h> |
| 31 | #include <drm/drm_crtc_helper.h> |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 32 | #include <drm/drm_atomic_helper.h> |
Daniel Vetter | e2330f0 | 2014-10-29 11:34:56 +0100 | [diff] [blame] | 33 | #include <linux/fence.h> |
Daniel Vetter | c2fcd27 | 2014-11-05 00:14:14 +0100 | [diff] [blame] | 34 | |
Daniel Vetter | 3150c7d | 2014-11-06 20:53:29 +0100 | [diff] [blame] | 35 | /** |
| 36 | * DOC: overview |
| 37 | * |
| 38 | * This helper library provides implementations of check and commit functions on |
| 39 | * top of the CRTC modeset helper callbacks and the plane helper callbacks. It |
| 40 | * also provides convenience implementations for the atomic state handling |
| 41 | * callbacks for drivers which don't need to subclass the drm core structures to |
| 42 | * add their own additional internal state. |
| 43 | * |
| 44 | * This library also provides default implementations for the check callback in |
Daniel Vetter | 26196f7 | 2015-08-25 16:26:03 +0200 | [diff] [blame] | 45 | * drm_atomic_helper_check() and for the commit callback with |
| 46 | * drm_atomic_helper_commit(). But the individual stages and callbacks are |
| 47 | * exposed to allow drivers to mix and match and e.g. use the plane helpers only |
Daniel Vetter | 3150c7d | 2014-11-06 20:53:29 +0100 | [diff] [blame] | 48 | * together with a driver private modeset implementation. |
| 49 | * |
| 50 | * This library also provides implementations for all the legacy driver |
Daniel Vetter | 26196f7 | 2015-08-25 16:26:03 +0200 | [diff] [blame] | 51 | * interfaces on top of the atomic interface. See drm_atomic_helper_set_config(), |
| 52 | * drm_atomic_helper_disable_plane(), drm_atomic_helper_disable_plane() and the |
Daniel Vetter | 3150c7d | 2014-11-06 20:53:29 +0100 | [diff] [blame] | 53 | * various functions to implement set_property callbacks. New drivers must not |
| 54 | * implement these functions themselves but must use the provided helpers. |
Daniel Vetter | 092d01d | 2015-12-04 09:45:44 +0100 | [diff] [blame] | 55 | * |
| 56 | * The atomic helper uses the same function table structures as all other |
| 57 | * modesetting helpers. See the documentation for struct &drm_crtc_helper_funcs, |
| 58 | * struct &drm_encoder_helper_funcs and struct &drm_connector_helper_funcs. It |
| 59 | * also shares the struct &drm_plane_helper_funcs function table with the plane |
| 60 | * helpers. |
Daniel Vetter | 3150c7d | 2014-11-06 20:53:29 +0100 | [diff] [blame] | 61 | */ |
Daniel Vetter | c2fcd27 | 2014-11-05 00:14:14 +0100 | [diff] [blame] | 62 | static void |
| 63 | drm_atomic_helper_plane_changed(struct drm_atomic_state *state, |
| 64 | struct drm_plane_state *plane_state, |
| 65 | struct drm_plane *plane) |
| 66 | { |
| 67 | struct drm_crtc_state *crtc_state; |
| 68 | |
| 69 | if (plane->state->crtc) { |
Rob Clark | 4b08eae | 2014-12-08 10:45:43 -0500 | [diff] [blame] | 70 | crtc_state = state->crtc_states[drm_crtc_index(plane->state->crtc)]; |
Daniel Vetter | c2fcd27 | 2014-11-05 00:14:14 +0100 | [diff] [blame] | 71 | |
| 72 | if (WARN_ON(!crtc_state)) |
| 73 | return; |
| 74 | |
| 75 | crtc_state->planes_changed = true; |
| 76 | } |
| 77 | |
| 78 | if (plane_state->crtc) { |
| 79 | crtc_state = |
| 80 | state->crtc_states[drm_crtc_index(plane_state->crtc)]; |
| 81 | |
| 82 | if (WARN_ON(!crtc_state)) |
| 83 | return; |
| 84 | |
| 85 | crtc_state->planes_changed = true; |
| 86 | } |
| 87 | } |
| 88 | |
Daniel Vetter | 97ac320 | 2015-12-03 10:49:14 +0100 | [diff] [blame] | 89 | static bool |
| 90 | check_pending_encoder_assignment(struct drm_atomic_state *state, |
Daniel Vetter | 07bad49 | 2015-12-08 09:49:18 +0100 | [diff] [blame] | 91 | struct drm_encoder *new_encoder) |
Daniel Vetter | 97ac320 | 2015-12-03 10:49:14 +0100 | [diff] [blame] | 92 | { |
| 93 | struct drm_connector *connector; |
| 94 | struct drm_connector_state *conn_state; |
| 95 | int i; |
| 96 | |
| 97 | for_each_connector_in_state(state, connector, conn_state, i) { |
| 98 | if (conn_state->best_encoder != new_encoder) |
| 99 | continue; |
| 100 | |
| 101 | /* encoder already assigned and we're trying to re-steal it! */ |
| 102 | if (connector->state->best_encoder != conn_state->best_encoder) |
| 103 | return false; |
| 104 | } |
| 105 | |
| 106 | return true; |
| 107 | } |
| 108 | |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 109 | static struct drm_crtc * |
| 110 | get_current_crtc_for_encoder(struct drm_device *dev, |
| 111 | struct drm_encoder *encoder) |
| 112 | { |
| 113 | struct drm_mode_config *config = &dev->mode_config; |
| 114 | struct drm_connector *connector; |
| 115 | |
| 116 | WARN_ON(!drm_modeset_is_locked(&config->connection_mutex)); |
| 117 | |
Daniel Vetter | 9a9f5ce | 2015-07-09 23:44:34 +0200 | [diff] [blame] | 118 | drm_for_each_connector(connector, dev) { |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 119 | if (connector->state->best_encoder != encoder) |
| 120 | continue; |
| 121 | |
| 122 | return connector->state->crtc; |
| 123 | } |
| 124 | |
| 125 | return NULL; |
| 126 | } |
| 127 | |
| 128 | static int |
| 129 | steal_encoder(struct drm_atomic_state *state, |
| 130 | struct drm_encoder *encoder, |
| 131 | struct drm_crtc *encoder_crtc) |
| 132 | { |
| 133 | struct drm_mode_config *config = &state->dev->mode_config; |
| 134 | struct drm_crtc_state *crtc_state; |
| 135 | struct drm_connector *connector; |
| 136 | struct drm_connector_state *connector_state; |
Daniel Vetter | 042652e | 2014-07-27 13:46:52 +0200 | [diff] [blame] | 137 | int ret; |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 138 | |
| 139 | /* |
| 140 | * We can only steal an encoder coming from a connector, which means we |
| 141 | * must already hold the connection_mutex. |
| 142 | */ |
| 143 | WARN_ON(!drm_modeset_is_locked(&config->connection_mutex)); |
| 144 | |
Ville Syrjälä | fa3ab4c | 2015-12-08 18:41:53 +0200 | [diff] [blame] | 145 | DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s], stealing it\n", |
Daniel Vetter | 17a38d9 | 2015-02-22 12:24:16 +0100 | [diff] [blame] | 146 | encoder->base.id, encoder->name, |
Ville Syrjälä | fa3ab4c | 2015-12-08 18:41:53 +0200 | [diff] [blame] | 147 | encoder_crtc->base.id, encoder_crtc->name); |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 148 | |
| 149 | crtc_state = drm_atomic_get_crtc_state(state, encoder_crtc); |
| 150 | if (IS_ERR(crtc_state)) |
| 151 | return PTR_ERR(crtc_state); |
| 152 | |
Maarten Lankhorst | fc59666 | 2015-07-21 13:28:57 +0200 | [diff] [blame] | 153 | crtc_state->connectors_changed = true; |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 154 | |
| 155 | list_for_each_entry(connector, &config->connector_list, head) { |
| 156 | if (connector->state->best_encoder != encoder) |
| 157 | continue; |
| 158 | |
Daniel Vetter | 17a38d9 | 2015-02-22 12:24:16 +0100 | [diff] [blame] | 159 | DRM_DEBUG_ATOMIC("Stealing encoder from [CONNECTOR:%d:%s]\n", |
| 160 | connector->base.id, |
| 161 | connector->name); |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 162 | |
| 163 | connector_state = drm_atomic_get_connector_state(state, |
| 164 | connector); |
| 165 | if (IS_ERR(connector_state)) |
| 166 | return PTR_ERR(connector_state); |
| 167 | |
Daniel Vetter | 042652e | 2014-07-27 13:46:52 +0200 | [diff] [blame] | 168 | ret = drm_atomic_set_crtc_for_connector(connector_state, NULL); |
| 169 | if (ret) |
| 170 | return ret; |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 171 | connector_state->best_encoder = NULL; |
| 172 | } |
| 173 | |
| 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | static int |
| 178 | update_connector_routing(struct drm_atomic_state *state, int conn_idx) |
| 179 | { |
Ville Syrjälä | b5ceff2 | 2015-03-10 14:35:20 +0200 | [diff] [blame] | 180 | const struct drm_connector_helper_funcs *funcs; |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 181 | struct drm_encoder *new_encoder; |
| 182 | struct drm_crtc *encoder_crtc; |
| 183 | struct drm_connector *connector; |
| 184 | struct drm_connector_state *connector_state; |
| 185 | struct drm_crtc_state *crtc_state; |
| 186 | int idx, ret; |
| 187 | |
| 188 | connector = state->connectors[conn_idx]; |
| 189 | connector_state = state->connector_states[conn_idx]; |
| 190 | |
| 191 | if (!connector) |
| 192 | return 0; |
| 193 | |
Daniel Vetter | 17a38d9 | 2015-02-22 12:24:16 +0100 | [diff] [blame] | 194 | DRM_DEBUG_ATOMIC("Updating routing for [CONNECTOR:%d:%s]\n", |
| 195 | connector->base.id, |
| 196 | connector->name); |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 197 | |
| 198 | if (connector->state->crtc != connector_state->crtc) { |
| 199 | if (connector->state->crtc) { |
| 200 | idx = drm_crtc_index(connector->state->crtc); |
| 201 | |
| 202 | crtc_state = state->crtc_states[idx]; |
Maarten Lankhorst | fc59666 | 2015-07-21 13:28:57 +0200 | [diff] [blame] | 203 | crtc_state->connectors_changed = true; |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | if (connector_state->crtc) { |
| 207 | idx = drm_crtc_index(connector_state->crtc); |
| 208 | |
| 209 | crtc_state = state->crtc_states[idx]; |
Maarten Lankhorst | fc59666 | 2015-07-21 13:28:57 +0200 | [diff] [blame] | 210 | crtc_state->connectors_changed = true; |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | |
| 214 | if (!connector_state->crtc) { |
Daniel Vetter | 17a38d9 | 2015-02-22 12:24:16 +0100 | [diff] [blame] | 215 | DRM_DEBUG_ATOMIC("Disabling [CONNECTOR:%d:%s]\n", |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 216 | connector->base.id, |
| 217 | connector->name); |
| 218 | |
| 219 | connector_state->best_encoder = NULL; |
| 220 | |
| 221 | return 0; |
| 222 | } |
| 223 | |
| 224 | funcs = connector->helper_private; |
Daniel Vetter | 3b8a684 | 2015-08-03 17:24:08 +0200 | [diff] [blame] | 225 | |
| 226 | if (funcs->atomic_best_encoder) |
| 227 | new_encoder = funcs->atomic_best_encoder(connector, |
| 228 | connector_state); |
| 229 | else |
| 230 | new_encoder = funcs->best_encoder(connector); |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 231 | |
| 232 | if (!new_encoder) { |
Daniel Vetter | 17a38d9 | 2015-02-22 12:24:16 +0100 | [diff] [blame] | 233 | DRM_DEBUG_ATOMIC("No suitable encoder found for [CONNECTOR:%d:%s]\n", |
| 234 | connector->base.id, |
| 235 | connector->name); |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 236 | return -EINVAL; |
| 237 | } |
| 238 | |
Daniel Vetter | 5481c8f | 2015-11-18 18:46:48 +0100 | [diff] [blame] | 239 | if (!drm_encoder_crtc_ok(new_encoder, connector_state->crtc)) { |
| 240 | DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] incompatible with [CRTC:%d]\n", |
| 241 | new_encoder->base.id, |
| 242 | new_encoder->name, |
| 243 | connector_state->crtc->base.id); |
| 244 | return -EINVAL; |
| 245 | } |
| 246 | |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 247 | if (new_encoder == connector_state->best_encoder) { |
Ville Syrjälä | fa3ab4c | 2015-12-08 18:41:53 +0200 | [diff] [blame] | 248 | DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] keeps [ENCODER:%d:%s], now on [CRTC:%d:%s]\n", |
Daniel Vetter | 17a38d9 | 2015-02-22 12:24:16 +0100 | [diff] [blame] | 249 | connector->base.id, |
| 250 | connector->name, |
| 251 | new_encoder->base.id, |
| 252 | new_encoder->name, |
Ville Syrjälä | fa3ab4c | 2015-12-08 18:41:53 +0200 | [diff] [blame] | 253 | connector_state->crtc->base.id, |
| 254 | connector_state->crtc->name); |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 255 | |
| 256 | return 0; |
| 257 | } |
| 258 | |
Daniel Vetter | 07bad49 | 2015-12-08 09:49:18 +0100 | [diff] [blame] | 259 | if (!check_pending_encoder_assignment(state, new_encoder)) { |
Daniel Vetter | 97ac320 | 2015-12-03 10:49:14 +0100 | [diff] [blame] | 260 | DRM_DEBUG_ATOMIC("Encoder for [CONNECTOR:%d:%s] already assigned\n", |
| 261 | connector->base.id, |
| 262 | connector->name); |
| 263 | return -EINVAL; |
| 264 | } |
| 265 | |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 266 | encoder_crtc = get_current_crtc_for_encoder(state->dev, |
| 267 | new_encoder); |
| 268 | |
| 269 | if (encoder_crtc) { |
| 270 | ret = steal_encoder(state, new_encoder, encoder_crtc); |
| 271 | if (ret) { |
Daniel Vetter | 17a38d9 | 2015-02-22 12:24:16 +0100 | [diff] [blame] | 272 | DRM_DEBUG_ATOMIC("Encoder stealing failed for [CONNECTOR:%d:%s]\n", |
| 273 | connector->base.id, |
| 274 | connector->name); |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 275 | return ret; |
| 276 | } |
| 277 | } |
| 278 | |
Daniel Vetter | 6ea76f3c | 2015-08-03 17:24:11 +0200 | [diff] [blame] | 279 | if (WARN_ON(!connector_state->crtc)) |
| 280 | return -EINVAL; |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 281 | |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 282 | connector_state->best_encoder = new_encoder; |
| 283 | idx = drm_crtc_index(connector_state->crtc); |
| 284 | |
| 285 | crtc_state = state->crtc_states[idx]; |
Maarten Lankhorst | fc59666 | 2015-07-21 13:28:57 +0200 | [diff] [blame] | 286 | crtc_state->connectors_changed = true; |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 287 | |
Ville Syrjälä | fa3ab4c | 2015-12-08 18:41:53 +0200 | [diff] [blame] | 288 | DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] using [ENCODER:%d:%s] on [CRTC:%d:%s]\n", |
Daniel Vetter | 17a38d9 | 2015-02-22 12:24:16 +0100 | [diff] [blame] | 289 | connector->base.id, |
| 290 | connector->name, |
| 291 | new_encoder->base.id, |
| 292 | new_encoder->name, |
Ville Syrjälä | fa3ab4c | 2015-12-08 18:41:53 +0200 | [diff] [blame] | 293 | connector_state->crtc->base.id, |
| 294 | connector_state->crtc->name); |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 295 | |
| 296 | return 0; |
| 297 | } |
| 298 | |
| 299 | static int |
| 300 | mode_fixup(struct drm_atomic_state *state) |
| 301 | { |
Ander Conselvan de Oliveira | df63b99 | 2015-04-10 14:58:39 +0300 | [diff] [blame] | 302 | struct drm_crtc *crtc; |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 303 | struct drm_crtc_state *crtc_state; |
Ander Conselvan de Oliveira | df63b99 | 2015-04-10 14:58:39 +0300 | [diff] [blame] | 304 | struct drm_connector *connector; |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 305 | struct drm_connector_state *conn_state; |
| 306 | int i; |
| 307 | bool ret; |
| 308 | |
Ander Conselvan de Oliveira | df63b99 | 2015-04-10 14:58:39 +0300 | [diff] [blame] | 309 | for_each_crtc_in_state(state, crtc, crtc_state, i) { |
Maarten Lankhorst | fc59666 | 2015-07-21 13:28:57 +0200 | [diff] [blame] | 310 | if (!crtc_state->mode_changed && |
| 311 | !crtc_state->connectors_changed) |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 312 | continue; |
| 313 | |
| 314 | drm_mode_copy(&crtc_state->adjusted_mode, &crtc_state->mode); |
| 315 | } |
| 316 | |
Ander Conselvan de Oliveira | df63b99 | 2015-04-10 14:58:39 +0300 | [diff] [blame] | 317 | for_each_connector_in_state(state, connector, conn_state, i) { |
Ville Syrjälä | b5ceff2 | 2015-03-10 14:35:20 +0200 | [diff] [blame] | 318 | const struct drm_encoder_helper_funcs *funcs; |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 319 | struct drm_encoder *encoder; |
| 320 | |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 321 | WARN_ON(!!conn_state->best_encoder != !!conn_state->crtc); |
| 322 | |
| 323 | if (!conn_state->crtc || !conn_state->best_encoder) |
| 324 | continue; |
| 325 | |
| 326 | crtc_state = |
| 327 | state->crtc_states[drm_crtc_index(conn_state->crtc)]; |
| 328 | |
| 329 | /* |
| 330 | * Each encoder has at most one connector (since we always steal |
| 331 | * it away), so we won't call ->mode_fixup twice. |
| 332 | */ |
| 333 | encoder = conn_state->best_encoder; |
| 334 | funcs = encoder->helper_private; |
Ander Conselvan de Oliveira | 840bfe9 | 2015-04-21 17:13:18 +0300 | [diff] [blame] | 335 | if (!funcs) |
| 336 | continue; |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 337 | |
Archit Taneja | 862e686 | 2015-05-21 11:03:16 +0530 | [diff] [blame] | 338 | ret = drm_bridge_mode_fixup(encoder->bridge, &crtc_state->mode, |
| 339 | &crtc_state->adjusted_mode); |
| 340 | if (!ret) { |
| 341 | DRM_DEBUG_ATOMIC("Bridge fixup failed\n"); |
| 342 | return -EINVAL; |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 343 | } |
| 344 | |
Thierry Reding | 4cd4df8 | 2014-12-03 16:44:34 +0100 | [diff] [blame] | 345 | if (funcs->atomic_check) { |
| 346 | ret = funcs->atomic_check(encoder, crtc_state, |
| 347 | conn_state); |
| 348 | if (ret) { |
Daniel Vetter | 17a38d9 | 2015-02-22 12:24:16 +0100 | [diff] [blame] | 349 | DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] check failed\n", |
| 350 | encoder->base.id, encoder->name); |
Thierry Reding | 4cd4df8 | 2014-12-03 16:44:34 +0100 | [diff] [blame] | 351 | return ret; |
| 352 | } |
Inki Dae | 8452491 | 2015-08-11 21:23:49 +0900 | [diff] [blame] | 353 | } else if (funcs->mode_fixup) { |
Thierry Reding | 4cd4df8 | 2014-12-03 16:44:34 +0100 | [diff] [blame] | 354 | ret = funcs->mode_fixup(encoder, &crtc_state->mode, |
| 355 | &crtc_state->adjusted_mode); |
| 356 | if (!ret) { |
Daniel Vetter | 17a38d9 | 2015-02-22 12:24:16 +0100 | [diff] [blame] | 357 | DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] fixup failed\n", |
| 358 | encoder->base.id, encoder->name); |
Thierry Reding | 4cd4df8 | 2014-12-03 16:44:34 +0100 | [diff] [blame] | 359 | return -EINVAL; |
| 360 | } |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 361 | } |
| 362 | } |
| 363 | |
Ander Conselvan de Oliveira | df63b99 | 2015-04-10 14:58:39 +0300 | [diff] [blame] | 364 | for_each_crtc_in_state(state, crtc, crtc_state, i) { |
Ville Syrjälä | b5ceff2 | 2015-03-10 14:35:20 +0200 | [diff] [blame] | 365 | const struct drm_crtc_helper_funcs *funcs; |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 366 | |
Maarten Lankhorst | fc59666 | 2015-07-21 13:28:57 +0200 | [diff] [blame] | 367 | if (!crtc_state->mode_changed && |
| 368 | !crtc_state->connectors_changed) |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 369 | continue; |
| 370 | |
| 371 | funcs = crtc->helper_private; |
Ander Conselvan de Oliveira | 840bfe9 | 2015-04-21 17:13:18 +0300 | [diff] [blame] | 372 | if (!funcs->mode_fixup) |
| 373 | continue; |
| 374 | |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 375 | ret = funcs->mode_fixup(crtc, &crtc_state->mode, |
| 376 | &crtc_state->adjusted_mode); |
| 377 | if (!ret) { |
Ville Syrjälä | fa3ab4c | 2015-12-08 18:41:53 +0200 | [diff] [blame] | 378 | DRM_DEBUG_ATOMIC("[CRTC:%d:%s] fixup failed\n", |
| 379 | crtc->base.id, crtc->name); |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 380 | return -EINVAL; |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | return 0; |
| 385 | } |
| 386 | |
Daniel Vetter | d9b1362 | 2014-11-26 16:57:41 +0100 | [diff] [blame] | 387 | /** |
John Hunter | f98bd3e | 2015-03-17 15:30:26 +0800 | [diff] [blame] | 388 | * drm_atomic_helper_check_modeset - validate state object for modeset changes |
Daniel Vetter | d9b1362 | 2014-11-26 16:57:41 +0100 | [diff] [blame] | 389 | * @dev: DRM device |
| 390 | * @state: the driver state object |
| 391 | * |
| 392 | * Check the state object to see if the requested state is physically possible. |
| 393 | * This does all the crtc and connector related computations for an atomic |
Maarten Lankhorst | fc59666 | 2015-07-21 13:28:57 +0200 | [diff] [blame] | 394 | * update and adds any additional connectors needed for full modesets and calls |
| 395 | * down into ->mode_fixup functions of the driver backend. |
| 396 | * |
| 397 | * crtc_state->mode_changed is set when the input mode is changed. |
| 398 | * crtc_state->connectors_changed is set when a connector is added or |
| 399 | * removed from the crtc. |
| 400 | * crtc_state->active_changed is set when crtc_state->active changes, |
| 401 | * which is used for dpms. |
Daniel Vetter | d9b1362 | 2014-11-26 16:57:41 +0100 | [diff] [blame] | 402 | * |
| 403 | * IMPORTANT: |
| 404 | * |
| 405 | * Drivers which update ->mode_changed (e.g. in their ->atomic_check hooks if a |
| 406 | * plane update can't be done without a full modeset) _must_ call this function |
| 407 | * afterwards after that change. It is permitted to call this function multiple |
| 408 | * times for the same update, e.g. when the ->atomic_check functions depend upon |
| 409 | * the adjusted dotclock for fifo space allocation and watermark computation. |
| 410 | * |
| 411 | * RETURNS |
| 412 | * Zero for success or -errno |
| 413 | */ |
| 414 | int |
Rob Clark | 934ce1c | 2014-11-19 16:41:33 -0500 | [diff] [blame] | 415 | drm_atomic_helper_check_modeset(struct drm_device *dev, |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 416 | struct drm_atomic_state *state) |
| 417 | { |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 418 | struct drm_crtc *crtc; |
| 419 | struct drm_crtc_state *crtc_state; |
Ander Conselvan de Oliveira | df63b99 | 2015-04-10 14:58:39 +0300 | [diff] [blame] | 420 | struct drm_connector *connector; |
| 421 | struct drm_connector_state *connector_state; |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 422 | int i, ret; |
| 423 | |
Ander Conselvan de Oliveira | df63b99 | 2015-04-10 14:58:39 +0300 | [diff] [blame] | 424 | for_each_crtc_in_state(state, crtc, crtc_state, i) { |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 425 | if (!drm_mode_equal(&crtc->state->mode, &crtc_state->mode)) { |
Ville Syrjälä | fa3ab4c | 2015-12-08 18:41:53 +0200 | [diff] [blame] | 426 | DRM_DEBUG_ATOMIC("[CRTC:%d:%s] mode changed\n", |
| 427 | crtc->base.id, crtc->name); |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 428 | crtc_state->mode_changed = true; |
| 429 | } |
| 430 | |
| 431 | if (crtc->state->enable != crtc_state->enable) { |
Ville Syrjälä | fa3ab4c | 2015-12-08 18:41:53 +0200 | [diff] [blame] | 432 | DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enable changed\n", |
| 433 | crtc->base.id, crtc->name); |
Maarten Lankhorst | fc59666 | 2015-07-21 13:28:57 +0200 | [diff] [blame] | 434 | |
| 435 | /* |
| 436 | * For clarity this assignment is done here, but |
| 437 | * enable == 0 is only true when there are no |
| 438 | * connectors and a NULL mode. |
| 439 | * |
| 440 | * The other way around is true as well. enable != 0 |
| 441 | * iff connectors are attached and a mode is set. |
| 442 | */ |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 443 | crtc_state->mode_changed = true; |
Maarten Lankhorst | fc59666 | 2015-07-21 13:28:57 +0200 | [diff] [blame] | 444 | crtc_state->connectors_changed = true; |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 445 | } |
| 446 | } |
| 447 | |
Ander Conselvan de Oliveira | df63b99 | 2015-04-10 14:58:39 +0300 | [diff] [blame] | 448 | for_each_connector_in_state(state, connector, connector_state, i) { |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 449 | /* |
| 450 | * This only sets crtc->mode_changed for routing changes, |
| 451 | * drivers must set crtc->mode_changed themselves when connector |
| 452 | * properties need to be updated. |
| 453 | */ |
| 454 | ret = update_connector_routing(state, i); |
| 455 | if (ret) |
| 456 | return ret; |
| 457 | } |
| 458 | |
| 459 | /* |
| 460 | * After all the routing has been prepared we need to add in any |
| 461 | * connector which is itself unchanged, but who's crtc changes it's |
| 462 | * configuration. This must be done before calling mode_fixup in case a |
| 463 | * crtc only changed its mode but has the same set of connectors. |
| 464 | */ |
Ander Conselvan de Oliveira | df63b99 | 2015-04-10 14:58:39 +0300 | [diff] [blame] | 465 | for_each_crtc_in_state(state, crtc, crtc_state, i) { |
Maarten Lankhorst | 14de6c4 | 2016-01-04 12:53:20 +0100 | [diff] [blame] | 466 | bool has_connectors = |
| 467 | !!crtc_state->connector_mask; |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 468 | |
Daniel Vetter | eab3bbe | 2015-01-22 16:36:21 +0100 | [diff] [blame] | 469 | /* |
| 470 | * We must set ->active_changed after walking connectors for |
| 471 | * otherwise an update that only changes active would result in |
| 472 | * a full modeset because update_connector_routing force that. |
| 473 | */ |
| 474 | if (crtc->state->active != crtc_state->active) { |
Ville Syrjälä | fa3ab4c | 2015-12-08 18:41:53 +0200 | [diff] [blame] | 475 | DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active changed\n", |
| 476 | crtc->base.id, crtc->name); |
Daniel Vetter | eab3bbe | 2015-01-22 16:36:21 +0100 | [diff] [blame] | 477 | crtc_state->active_changed = true; |
| 478 | } |
| 479 | |
Daniel Vetter | 2465ff6 | 2015-06-18 09:58:55 +0200 | [diff] [blame] | 480 | if (!drm_atomic_crtc_needs_modeset(crtc_state)) |
Daniel Vetter | eab3bbe | 2015-01-22 16:36:21 +0100 | [diff] [blame] | 481 | continue; |
| 482 | |
Ville Syrjälä | fa3ab4c | 2015-12-08 18:41:53 +0200 | [diff] [blame] | 483 | DRM_DEBUG_ATOMIC("[CRTC:%d:%s] needs all connectors, enable: %c, active: %c\n", |
| 484 | crtc->base.id, crtc->name, |
Daniel Vetter | 17a38d9 | 2015-02-22 12:24:16 +0100 | [diff] [blame] | 485 | crtc_state->enable ? 'y' : 'n', |
Ville Syrjälä | fa3ab4c | 2015-12-08 18:41:53 +0200 | [diff] [blame] | 486 | crtc_state->active ? 'y' : 'n'); |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 487 | |
| 488 | ret = drm_atomic_add_affected_connectors(state, crtc); |
| 489 | if (ret != 0) |
| 490 | return ret; |
| 491 | |
Maarten Lankhorst | 57744aa | 2015-05-19 16:41:03 +0200 | [diff] [blame] | 492 | ret = drm_atomic_add_affected_planes(state, crtc); |
| 493 | if (ret != 0) |
| 494 | return ret; |
| 495 | |
Maarten Lankhorst | 14de6c4 | 2016-01-04 12:53:20 +0100 | [diff] [blame] | 496 | if (crtc_state->enable != has_connectors) { |
Ville Syrjälä | fa3ab4c | 2015-12-08 18:41:53 +0200 | [diff] [blame] | 497 | DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled/connectors mismatch\n", |
| 498 | crtc->base.id, crtc->name); |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 499 | |
| 500 | return -EINVAL; |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | return mode_fixup(state); |
| 505 | } |
Daniel Vetter | d9b1362 | 2014-11-26 16:57:41 +0100 | [diff] [blame] | 506 | EXPORT_SYMBOL(drm_atomic_helper_check_modeset); |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 507 | |
Daniel Vetter | c2fcd27 | 2014-11-05 00:14:14 +0100 | [diff] [blame] | 508 | /** |
John Hunter | f98bd3e | 2015-03-17 15:30:26 +0800 | [diff] [blame] | 509 | * drm_atomic_helper_check_planes - validate state object for planes changes |
Daniel Vetter | c2fcd27 | 2014-11-05 00:14:14 +0100 | [diff] [blame] | 510 | * @dev: DRM device |
| 511 | * @state: the driver state object |
| 512 | * |
| 513 | * Check the state object to see if the requested state is physically possible. |
Daniel Vetter | d9b1362 | 2014-11-26 16:57:41 +0100 | [diff] [blame] | 514 | * This does all the plane update related checks using by calling into the |
| 515 | * ->atomic_check hooks provided by the driver. |
Daniel Vetter | c2fcd27 | 2014-11-05 00:14:14 +0100 | [diff] [blame] | 516 | * |
Maarten Lankhorst | fc59666 | 2015-07-21 13:28:57 +0200 | [diff] [blame] | 517 | * It also sets crtc_state->planes_changed to indicate that a crtc has |
| 518 | * updated planes. |
| 519 | * |
Daniel Vetter | c2fcd27 | 2014-11-05 00:14:14 +0100 | [diff] [blame] | 520 | * RETURNS |
| 521 | * Zero for success or -errno |
| 522 | */ |
Daniel Vetter | d9b1362 | 2014-11-26 16:57:41 +0100 | [diff] [blame] | 523 | int |
| 524 | drm_atomic_helper_check_planes(struct drm_device *dev, |
| 525 | struct drm_atomic_state *state) |
Daniel Vetter | c2fcd27 | 2014-11-05 00:14:14 +0100 | [diff] [blame] | 526 | { |
Ander Conselvan de Oliveira | df63b99 | 2015-04-10 14:58:39 +0300 | [diff] [blame] | 527 | struct drm_crtc *crtc; |
| 528 | struct drm_crtc_state *crtc_state; |
| 529 | struct drm_plane *plane; |
| 530 | struct drm_plane_state *plane_state; |
Daniel Vetter | c2fcd27 | 2014-11-05 00:14:14 +0100 | [diff] [blame] | 531 | int i, ret = 0; |
| 532 | |
Ander Conselvan de Oliveira | df63b99 | 2015-04-10 14:58:39 +0300 | [diff] [blame] | 533 | for_each_plane_in_state(state, plane, plane_state, i) { |
Ville Syrjälä | b5ceff2 | 2015-03-10 14:35:20 +0200 | [diff] [blame] | 534 | const struct drm_plane_helper_funcs *funcs; |
Daniel Vetter | c2fcd27 | 2014-11-05 00:14:14 +0100 | [diff] [blame] | 535 | |
| 536 | funcs = plane->helper_private; |
| 537 | |
| 538 | drm_atomic_helper_plane_changed(state, plane_state, plane); |
| 539 | |
| 540 | if (!funcs || !funcs->atomic_check) |
| 541 | continue; |
| 542 | |
| 543 | ret = funcs->atomic_check(plane, plane_state); |
| 544 | if (ret) { |
Ville Syrjälä | 9f4c97a | 2015-12-08 18:41:54 +0200 | [diff] [blame] | 545 | DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic driver check failed\n", |
| 546 | plane->base.id, plane->name); |
Daniel Vetter | c2fcd27 | 2014-11-05 00:14:14 +0100 | [diff] [blame] | 547 | return ret; |
| 548 | } |
| 549 | } |
| 550 | |
Ander Conselvan de Oliveira | df63b99 | 2015-04-10 14:58:39 +0300 | [diff] [blame] | 551 | for_each_crtc_in_state(state, crtc, crtc_state, i) { |
Ville Syrjälä | b5ceff2 | 2015-03-10 14:35:20 +0200 | [diff] [blame] | 552 | const struct drm_crtc_helper_funcs *funcs; |
Daniel Vetter | c2fcd27 | 2014-11-05 00:14:14 +0100 | [diff] [blame] | 553 | |
| 554 | funcs = crtc->helper_private; |
| 555 | |
| 556 | if (!funcs || !funcs->atomic_check) |
| 557 | continue; |
| 558 | |
| 559 | ret = funcs->atomic_check(crtc, state->crtc_states[i]); |
| 560 | if (ret) { |
Ville Syrjälä | fa3ab4c | 2015-12-08 18:41:53 +0200 | [diff] [blame] | 561 | DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic driver check failed\n", |
| 562 | crtc->base.id, crtc->name); |
Daniel Vetter | c2fcd27 | 2014-11-05 00:14:14 +0100 | [diff] [blame] | 563 | return ret; |
| 564 | } |
| 565 | } |
| 566 | |
Daniel Vetter | d9b1362 | 2014-11-26 16:57:41 +0100 | [diff] [blame] | 567 | return ret; |
| 568 | } |
| 569 | EXPORT_SYMBOL(drm_atomic_helper_check_planes); |
| 570 | |
| 571 | /** |
| 572 | * drm_atomic_helper_check - validate state object |
| 573 | * @dev: DRM device |
| 574 | * @state: the driver state object |
| 575 | * |
| 576 | * Check the state object to see if the requested state is physically possible. |
| 577 | * Only crtcs and planes have check callbacks, so for any additional (global) |
| 578 | * checking that a driver needs it can simply wrap that around this function. |
| 579 | * Drivers without such needs can directly use this as their ->atomic_check() |
| 580 | * callback. |
| 581 | * |
Daniel Vetter | b4274fb | 2014-11-26 17:02:18 +0100 | [diff] [blame] | 582 | * This just wraps the two parts of the state checking for planes and modeset |
| 583 | * state in the default order: First it calls drm_atomic_helper_check_modeset() |
| 584 | * and then drm_atomic_helper_check_planes(). The assumption is that the |
| 585 | * ->atomic_check functions depend upon an updated adjusted_mode.clock to |
| 586 | * e.g. properly compute watermarks. |
| 587 | * |
Daniel Vetter | d9b1362 | 2014-11-26 16:57:41 +0100 | [diff] [blame] | 588 | * RETURNS |
| 589 | * Zero for success or -errno |
| 590 | */ |
| 591 | int drm_atomic_helper_check(struct drm_device *dev, |
| 592 | struct drm_atomic_state *state) |
| 593 | { |
| 594 | int ret; |
| 595 | |
Daniel Vetter | b4274fb | 2014-11-26 17:02:18 +0100 | [diff] [blame] | 596 | ret = drm_atomic_helper_check_modeset(dev, state); |
Daniel Vetter | d9b1362 | 2014-11-26 16:57:41 +0100 | [diff] [blame] | 597 | if (ret) |
| 598 | return ret; |
| 599 | |
Daniel Vetter | b4274fb | 2014-11-26 17:02:18 +0100 | [diff] [blame] | 600 | ret = drm_atomic_helper_check_planes(dev, state); |
Rob Clark | 934ce1c | 2014-11-19 16:41:33 -0500 | [diff] [blame] | 601 | if (ret) |
| 602 | return ret; |
| 603 | |
Daniel Vetter | c2fcd27 | 2014-11-05 00:14:14 +0100 | [diff] [blame] | 604 | return ret; |
| 605 | } |
| 606 | EXPORT_SYMBOL(drm_atomic_helper_check); |
| 607 | |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 608 | static void |
| 609 | disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state) |
| 610 | { |
Ander Conselvan de Oliveira | df63b99 | 2015-04-10 14:58:39 +0300 | [diff] [blame] | 611 | struct drm_connector *connector; |
| 612 | struct drm_connector_state *old_conn_state; |
| 613 | struct drm_crtc *crtc; |
| 614 | struct drm_crtc_state *old_crtc_state; |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 615 | int i; |
| 616 | |
Ander Conselvan de Oliveira | df63b99 | 2015-04-10 14:58:39 +0300 | [diff] [blame] | 617 | for_each_connector_in_state(old_state, connector, old_conn_state, i) { |
Ville Syrjälä | b5ceff2 | 2015-03-10 14:35:20 +0200 | [diff] [blame] | 618 | const struct drm_encoder_helper_funcs *funcs; |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 619 | struct drm_encoder *encoder; |
Daniel Vetter | eab3bbe | 2015-01-22 16:36:21 +0100 | [diff] [blame] | 620 | struct drm_crtc_state *old_crtc_state; |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 621 | |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 622 | /* Shut down everything that's in the changeset and currently |
| 623 | * still on. So need to check the old, saved state. */ |
Ander Conselvan de Oliveira | df63b99 | 2015-04-10 14:58:39 +0300 | [diff] [blame] | 624 | if (!old_conn_state->crtc) |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 625 | continue; |
| 626 | |
Daniel Vetter | eab3bbe | 2015-01-22 16:36:21 +0100 | [diff] [blame] | 627 | old_crtc_state = old_state->crtc_states[drm_crtc_index(old_conn_state->crtc)]; |
| 628 | |
Daniel Vetter | 4218a32 | 2015-03-26 22:18:40 +0100 | [diff] [blame] | 629 | if (!old_crtc_state->active || |
Daniel Vetter | 2465ff6 | 2015-06-18 09:58:55 +0200 | [diff] [blame] | 630 | !drm_atomic_crtc_needs_modeset(old_conn_state->crtc->state)) |
Daniel Vetter | eab3bbe | 2015-01-22 16:36:21 +0100 | [diff] [blame] | 631 | continue; |
| 632 | |
Rob Clark | 46df9ad | 2014-11-20 15:40:36 -0500 | [diff] [blame] | 633 | encoder = old_conn_state->best_encoder; |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 634 | |
Rob Clark | 46df9ad | 2014-11-20 15:40:36 -0500 | [diff] [blame] | 635 | /* We shouldn't get this far if we didn't previously have |
| 636 | * an encoder.. but WARN_ON() rather than explode. |
| 637 | */ |
| 638 | if (WARN_ON(!encoder)) |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 639 | continue; |
| 640 | |
| 641 | funcs = encoder->helper_private; |
| 642 | |
Daniel Vetter | 17a38d9 | 2015-02-22 12:24:16 +0100 | [diff] [blame] | 643 | DRM_DEBUG_ATOMIC("disabling [ENCODER:%d:%s]\n", |
| 644 | encoder->base.id, encoder->name); |
Daniel Vetter | 95d6eb3 | 2015-01-22 16:36:25 +0100 | [diff] [blame] | 645 | |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 646 | /* |
| 647 | * Each encoder has at most one connector (since we always steal |
John Hunter | f98bd3e | 2015-03-17 15:30:26 +0800 | [diff] [blame] | 648 | * it away), so we won't call disable hooks twice. |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 649 | */ |
Archit Taneja | 862e686 | 2015-05-21 11:03:16 +0530 | [diff] [blame] | 650 | drm_bridge_disable(encoder->bridge); |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 651 | |
| 652 | /* Right function depends upon target state. */ |
Daniel Vetter | ee0a89c | 2015-01-22 16:36:24 +0100 | [diff] [blame] | 653 | if (connector->state->crtc && funcs->prepare) |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 654 | funcs->prepare(encoder); |
| 655 | else if (funcs->disable) |
| 656 | funcs->disable(encoder); |
| 657 | else |
| 658 | funcs->dpms(encoder, DRM_MODE_DPMS_OFF); |
| 659 | |
Archit Taneja | 862e686 | 2015-05-21 11:03:16 +0530 | [diff] [blame] | 660 | drm_bridge_post_disable(encoder->bridge); |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 661 | } |
| 662 | |
Ander Conselvan de Oliveira | df63b99 | 2015-04-10 14:58:39 +0300 | [diff] [blame] | 663 | for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) { |
Ville Syrjälä | b5ceff2 | 2015-03-10 14:35:20 +0200 | [diff] [blame] | 664 | const struct drm_crtc_helper_funcs *funcs; |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 665 | |
| 666 | /* Shut down everything that needs a full modeset. */ |
Daniel Vetter | 2465ff6 | 2015-06-18 09:58:55 +0200 | [diff] [blame] | 667 | if (!drm_atomic_crtc_needs_modeset(crtc->state)) |
Daniel Vetter | eab3bbe | 2015-01-22 16:36:21 +0100 | [diff] [blame] | 668 | continue; |
| 669 | |
| 670 | if (!old_crtc_state->active) |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 671 | continue; |
| 672 | |
| 673 | funcs = crtc->helper_private; |
| 674 | |
Ville Syrjälä | fa3ab4c | 2015-12-08 18:41:53 +0200 | [diff] [blame] | 675 | DRM_DEBUG_ATOMIC("disabling [CRTC:%d:%s]\n", |
| 676 | crtc->base.id, crtc->name); |
Daniel Vetter | 95d6eb3 | 2015-01-22 16:36:25 +0100 | [diff] [blame] | 677 | |
| 678 | |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 679 | /* Right function depends upon target state. */ |
Daniel Vetter | ee0a89c | 2015-01-22 16:36:24 +0100 | [diff] [blame] | 680 | if (crtc->state->enable && funcs->prepare) |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 681 | funcs->prepare(crtc); |
| 682 | else if (funcs->disable) |
| 683 | funcs->disable(crtc); |
| 684 | else |
| 685 | funcs->dpms(crtc, DRM_MODE_DPMS_OFF); |
| 686 | } |
| 687 | } |
| 688 | |
Daniel Vetter | 4c18d30 | 2015-05-12 15:27:37 +0200 | [diff] [blame] | 689 | /** |
| 690 | * drm_atomic_helper_update_legacy_modeset_state - update legacy modeset state |
| 691 | * @dev: DRM device |
| 692 | * @old_state: atomic state object with old state structures |
| 693 | * |
| 694 | * This function updates all the various legacy modeset state pointers in |
| 695 | * connectors, encoders and crtcs. It also updates the timestamping constants |
| 696 | * used for precise vblank timestamps by calling |
| 697 | * drm_calc_timestamping_constants(). |
| 698 | * |
| 699 | * Drivers can use this for building their own atomic commit if they don't have |
| 700 | * a pure helper-based modeset implementation. |
| 701 | */ |
| 702 | void |
| 703 | drm_atomic_helper_update_legacy_modeset_state(struct drm_device *dev, |
| 704 | struct drm_atomic_state *old_state) |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 705 | { |
Ander Conselvan de Oliveira | df63b99 | 2015-04-10 14:58:39 +0300 | [diff] [blame] | 706 | struct drm_connector *connector; |
| 707 | struct drm_connector_state *old_conn_state; |
| 708 | struct drm_crtc *crtc; |
| 709 | struct drm_crtc_state *old_crtc_state; |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 710 | int i; |
| 711 | |
Maarten Lankhorst | 8c10342 | 2015-07-27 13:24:29 +0200 | [diff] [blame] | 712 | /* clear out existing links and update dpms */ |
Ander Conselvan de Oliveira | df63b99 | 2015-04-10 14:58:39 +0300 | [diff] [blame] | 713 | for_each_connector_in_state(old_state, connector, old_conn_state, i) { |
Maarten Lankhorst | 8c10342 | 2015-07-27 13:24:29 +0200 | [diff] [blame] | 714 | if (connector->encoder) { |
| 715 | WARN_ON(!connector->encoder->crtc); |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 716 | |
Maarten Lankhorst | 8c10342 | 2015-07-27 13:24:29 +0200 | [diff] [blame] | 717 | connector->encoder->crtc = NULL; |
| 718 | connector->encoder = NULL; |
| 719 | } |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 720 | |
Maarten Lankhorst | 8c10342 | 2015-07-27 13:24:29 +0200 | [diff] [blame] | 721 | crtc = connector->state->crtc; |
| 722 | if ((!crtc && old_conn_state->crtc) || |
| 723 | (crtc && drm_atomic_crtc_needs_modeset(crtc->state))) { |
| 724 | struct drm_property *dpms_prop = |
| 725 | dev->mode_config.dpms_property; |
| 726 | int mode = DRM_MODE_DPMS_OFF; |
| 727 | |
| 728 | if (crtc && crtc->state->active) |
| 729 | mode = DRM_MODE_DPMS_ON; |
| 730 | |
| 731 | connector->dpms = mode; |
| 732 | drm_object_property_set_value(&connector->base, |
| 733 | dpms_prop, mode); |
| 734 | } |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | /* set new links */ |
Ander Conselvan de Oliveira | df63b99 | 2015-04-10 14:58:39 +0300 | [diff] [blame] | 738 | for_each_connector_in_state(old_state, connector, old_conn_state, i) { |
| 739 | if (!connector->state->crtc) |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 740 | continue; |
| 741 | |
| 742 | if (WARN_ON(!connector->state->best_encoder)) |
| 743 | continue; |
| 744 | |
| 745 | connector->encoder = connector->state->best_encoder; |
| 746 | connector->encoder->crtc = connector->state->crtc; |
| 747 | } |
| 748 | |
| 749 | /* set legacy state in the crtc structure */ |
Ander Conselvan de Oliveira | df63b99 | 2015-04-10 14:58:39 +0300 | [diff] [blame] | 750 | for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) { |
Maarten Lankhorst | 2660801 | 2015-07-16 15:51:01 +0200 | [diff] [blame] | 751 | struct drm_plane *primary = crtc->primary; |
| 752 | |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 753 | crtc->mode = crtc->state->mode; |
| 754 | crtc->enabled = crtc->state->enable; |
Maarten Lankhorst | 2660801 | 2015-07-16 15:51:01 +0200 | [diff] [blame] | 755 | |
| 756 | if (drm_atomic_get_existing_plane_state(old_state, primary) && |
| 757 | primary->state->crtc == crtc) { |
| 758 | crtc->x = primary->state->src_x >> 16; |
| 759 | crtc->y = primary->state->src_y >> 16; |
| 760 | } |
Daniel Vetter | 3d51d2d | 2015-05-12 15:21:06 +0200 | [diff] [blame] | 761 | |
| 762 | if (crtc->state->enable) |
| 763 | drm_calc_timestamping_constants(crtc, |
| 764 | &crtc->state->adjusted_mode); |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 765 | } |
| 766 | } |
Daniel Vetter | 4c18d30 | 2015-05-12 15:27:37 +0200 | [diff] [blame] | 767 | EXPORT_SYMBOL(drm_atomic_helper_update_legacy_modeset_state); |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 768 | |
| 769 | static void |
| 770 | crtc_set_mode(struct drm_device *dev, struct drm_atomic_state *old_state) |
| 771 | { |
Ander Conselvan de Oliveira | df63b99 | 2015-04-10 14:58:39 +0300 | [diff] [blame] | 772 | struct drm_crtc *crtc; |
| 773 | struct drm_crtc_state *old_crtc_state; |
| 774 | struct drm_connector *connector; |
| 775 | struct drm_connector_state *old_conn_state; |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 776 | int i; |
| 777 | |
Ander Conselvan de Oliveira | df63b99 | 2015-04-10 14:58:39 +0300 | [diff] [blame] | 778 | for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) { |
Ville Syrjälä | b5ceff2 | 2015-03-10 14:35:20 +0200 | [diff] [blame] | 779 | const struct drm_crtc_helper_funcs *funcs; |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 780 | |
Ander Conselvan de Oliveira | df63b99 | 2015-04-10 14:58:39 +0300 | [diff] [blame] | 781 | if (!crtc->state->mode_changed) |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 782 | continue; |
| 783 | |
| 784 | funcs = crtc->helper_private; |
| 785 | |
Daniel Vetter | c982bd9 | 2015-02-22 12:24:20 +0100 | [diff] [blame] | 786 | if (crtc->state->enable && funcs->mode_set_nofb) { |
Ville Syrjälä | fa3ab4c | 2015-12-08 18:41:53 +0200 | [diff] [blame] | 787 | DRM_DEBUG_ATOMIC("modeset on [CRTC:%d:%s]\n", |
| 788 | crtc->base.id, crtc->name); |
Daniel Vetter | 95d6eb3 | 2015-01-22 16:36:25 +0100 | [diff] [blame] | 789 | |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 790 | funcs->mode_set_nofb(crtc); |
Daniel Vetter | 95d6eb3 | 2015-01-22 16:36:25 +0100 | [diff] [blame] | 791 | } |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 792 | } |
| 793 | |
Ander Conselvan de Oliveira | df63b99 | 2015-04-10 14:58:39 +0300 | [diff] [blame] | 794 | for_each_connector_in_state(old_state, connector, old_conn_state, i) { |
Ville Syrjälä | b5ceff2 | 2015-03-10 14:35:20 +0200 | [diff] [blame] | 795 | const struct drm_encoder_helper_funcs *funcs; |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 796 | struct drm_crtc_state *new_crtc_state; |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 797 | struct drm_encoder *encoder; |
| 798 | struct drm_display_mode *mode, *adjusted_mode; |
| 799 | |
Ander Conselvan de Oliveira | df63b99 | 2015-04-10 14:58:39 +0300 | [diff] [blame] | 800 | if (!connector->state->best_encoder) |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 801 | continue; |
| 802 | |
| 803 | encoder = connector->state->best_encoder; |
| 804 | funcs = encoder->helper_private; |
| 805 | new_crtc_state = connector->state->crtc->state; |
| 806 | mode = &new_crtc_state->mode; |
| 807 | adjusted_mode = &new_crtc_state->adjusted_mode; |
| 808 | |
Daniel Vetter | eab3bbe | 2015-01-22 16:36:21 +0100 | [diff] [blame] | 809 | if (!new_crtc_state->mode_changed) |
| 810 | continue; |
| 811 | |
Daniel Vetter | 17a38d9 | 2015-02-22 12:24:16 +0100 | [diff] [blame] | 812 | DRM_DEBUG_ATOMIC("modeset on [ENCODER:%d:%s]\n", |
| 813 | encoder->base.id, encoder->name); |
Daniel Vetter | 95d6eb3 | 2015-01-22 16:36:25 +0100 | [diff] [blame] | 814 | |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 815 | /* |
| 816 | * Each encoder has at most one connector (since we always steal |
John Hunter | f98bd3e | 2015-03-17 15:30:26 +0800 | [diff] [blame] | 817 | * it away), so we won't call mode_set hooks twice. |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 818 | */ |
Daniel Vetter | c982bd9 | 2015-02-22 12:24:20 +0100 | [diff] [blame] | 819 | if (funcs->mode_set) |
| 820 | funcs->mode_set(encoder, mode, adjusted_mode); |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 821 | |
Archit Taneja | 862e686 | 2015-05-21 11:03:16 +0530 | [diff] [blame] | 822 | drm_bridge_mode_set(encoder->bridge, mode, adjusted_mode); |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 823 | } |
| 824 | } |
| 825 | |
| 826 | /** |
Daniel Vetter | 1af434a | 2015-02-22 12:24:19 +0100 | [diff] [blame] | 827 | * drm_atomic_helper_commit_modeset_disables - modeset commit to disable outputs |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 828 | * @dev: DRM device |
Laurent Pinchart | a072f80 | 2015-02-22 12:24:18 +0100 | [diff] [blame] | 829 | * @old_state: atomic state object with old state structures |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 830 | * |
Daniel Vetter | 1af434a | 2015-02-22 12:24:19 +0100 | [diff] [blame] | 831 | * This function shuts down all the outputs that need to be shut down and |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 832 | * prepares them (if required) with the new mode. |
Daniel Vetter | 1af434a | 2015-02-22 12:24:19 +0100 | [diff] [blame] | 833 | * |
Laurent Pinchart | 60acc4e | 2015-05-27 15:05:42 +0300 | [diff] [blame] | 834 | * For compatibility with legacy crtc helpers this should be called before |
Daniel Vetter | 1af434a | 2015-02-22 12:24:19 +0100 | [diff] [blame] | 835 | * drm_atomic_helper_commit_planes(), which is what the default commit function |
| 836 | * does. But drivers with different needs can group the modeset commits together |
| 837 | * and do the plane commits at the end. This is useful for drivers doing runtime |
| 838 | * PM since planes updates then only happen when the CRTC is actually enabled. |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 839 | */ |
Daniel Vetter | 1af434a | 2015-02-22 12:24:19 +0100 | [diff] [blame] | 840 | void drm_atomic_helper_commit_modeset_disables(struct drm_device *dev, |
| 841 | struct drm_atomic_state *old_state) |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 842 | { |
Laurent Pinchart | a072f80 | 2015-02-22 12:24:18 +0100 | [diff] [blame] | 843 | disable_outputs(dev, old_state); |
Daniel Vetter | 4c18d30 | 2015-05-12 15:27:37 +0200 | [diff] [blame] | 844 | |
| 845 | drm_atomic_helper_update_legacy_modeset_state(dev, old_state); |
| 846 | |
Laurent Pinchart | a072f80 | 2015-02-22 12:24:18 +0100 | [diff] [blame] | 847 | crtc_set_mode(dev, old_state); |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 848 | } |
Daniel Vetter | 1af434a | 2015-02-22 12:24:19 +0100 | [diff] [blame] | 849 | EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_disables); |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 850 | |
| 851 | /** |
Daniel Vetter | 1af434a | 2015-02-22 12:24:19 +0100 | [diff] [blame] | 852 | * drm_atomic_helper_commit_modeset_enables - modeset commit to enable outputs |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 853 | * @dev: DRM device |
| 854 | * @old_state: atomic state object with old state structures |
| 855 | * |
Daniel Vetter | 1af434a | 2015-02-22 12:24:19 +0100 | [diff] [blame] | 856 | * This function enables all the outputs with the new configuration which had to |
| 857 | * be turned off for the update. |
| 858 | * |
Laurent Pinchart | 60acc4e | 2015-05-27 15:05:42 +0300 | [diff] [blame] | 859 | * For compatibility with legacy crtc helpers this should be called after |
Daniel Vetter | 1af434a | 2015-02-22 12:24:19 +0100 | [diff] [blame] | 860 | * drm_atomic_helper_commit_planes(), which is what the default commit function |
| 861 | * does. But drivers with different needs can group the modeset commits together |
| 862 | * and do the plane commits at the end. This is useful for drivers doing runtime |
| 863 | * PM since planes updates then only happen when the CRTC is actually enabled. |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 864 | */ |
Daniel Vetter | 1af434a | 2015-02-22 12:24:19 +0100 | [diff] [blame] | 865 | void drm_atomic_helper_commit_modeset_enables(struct drm_device *dev, |
| 866 | struct drm_atomic_state *old_state) |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 867 | { |
Ander Conselvan de Oliveira | df63b99 | 2015-04-10 14:58:39 +0300 | [diff] [blame] | 868 | struct drm_crtc *crtc; |
| 869 | struct drm_crtc_state *old_crtc_state; |
| 870 | struct drm_connector *connector; |
| 871 | struct drm_connector_state *old_conn_state; |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 872 | int i; |
| 873 | |
Ander Conselvan de Oliveira | df63b99 | 2015-04-10 14:58:39 +0300 | [diff] [blame] | 874 | for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) { |
Ville Syrjälä | b5ceff2 | 2015-03-10 14:35:20 +0200 | [diff] [blame] | 875 | const struct drm_crtc_helper_funcs *funcs; |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 876 | |
| 877 | /* Need to filter out CRTCs where only planes change. */ |
Daniel Vetter | 2465ff6 | 2015-06-18 09:58:55 +0200 | [diff] [blame] | 878 | if (!drm_atomic_crtc_needs_modeset(crtc->state)) |
Daniel Vetter | eab3bbe | 2015-01-22 16:36:21 +0100 | [diff] [blame] | 879 | continue; |
| 880 | |
| 881 | if (!crtc->state->active) |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 882 | continue; |
| 883 | |
| 884 | funcs = crtc->helper_private; |
| 885 | |
Daniel Vetter | ee0a89c | 2015-01-22 16:36:24 +0100 | [diff] [blame] | 886 | if (crtc->state->enable) { |
Ville Syrjälä | fa3ab4c | 2015-12-08 18:41:53 +0200 | [diff] [blame] | 887 | DRM_DEBUG_ATOMIC("enabling [CRTC:%d:%s]\n", |
| 888 | crtc->base.id, crtc->name); |
Daniel Vetter | 95d6eb3 | 2015-01-22 16:36:25 +0100 | [diff] [blame] | 889 | |
Daniel Vetter | ee0a89c | 2015-01-22 16:36:24 +0100 | [diff] [blame] | 890 | if (funcs->enable) |
| 891 | funcs->enable(crtc); |
| 892 | else |
| 893 | funcs->commit(crtc); |
| 894 | } |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 895 | } |
| 896 | |
Ander Conselvan de Oliveira | df63b99 | 2015-04-10 14:58:39 +0300 | [diff] [blame] | 897 | for_each_connector_in_state(old_state, connector, old_conn_state, i) { |
Ville Syrjälä | b5ceff2 | 2015-03-10 14:35:20 +0200 | [diff] [blame] | 898 | const struct drm_encoder_helper_funcs *funcs; |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 899 | struct drm_encoder *encoder; |
| 900 | |
Ander Conselvan de Oliveira | df63b99 | 2015-04-10 14:58:39 +0300 | [diff] [blame] | 901 | if (!connector->state->best_encoder) |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 902 | continue; |
| 903 | |
Daniel Vetter | 4218a32 | 2015-03-26 22:18:40 +0100 | [diff] [blame] | 904 | if (!connector->state->crtc->state->active || |
Daniel Vetter | 2465ff6 | 2015-06-18 09:58:55 +0200 | [diff] [blame] | 905 | !drm_atomic_crtc_needs_modeset(connector->state->crtc->state)) |
Daniel Vetter | eab3bbe | 2015-01-22 16:36:21 +0100 | [diff] [blame] | 906 | continue; |
| 907 | |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 908 | encoder = connector->state->best_encoder; |
| 909 | funcs = encoder->helper_private; |
| 910 | |
Daniel Vetter | 17a38d9 | 2015-02-22 12:24:16 +0100 | [diff] [blame] | 911 | DRM_DEBUG_ATOMIC("enabling [ENCODER:%d:%s]\n", |
| 912 | encoder->base.id, encoder->name); |
Daniel Vetter | 95d6eb3 | 2015-01-22 16:36:25 +0100 | [diff] [blame] | 913 | |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 914 | /* |
| 915 | * Each encoder has at most one connector (since we always steal |
John Hunter | f98bd3e | 2015-03-17 15:30:26 +0800 | [diff] [blame] | 916 | * it away), so we won't call enable hooks twice. |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 917 | */ |
Archit Taneja | 862e686 | 2015-05-21 11:03:16 +0530 | [diff] [blame] | 918 | drm_bridge_pre_enable(encoder->bridge); |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 919 | |
Daniel Vetter | ee0a89c | 2015-01-22 16:36:24 +0100 | [diff] [blame] | 920 | if (funcs->enable) |
| 921 | funcs->enable(encoder); |
| 922 | else |
| 923 | funcs->commit(encoder); |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 924 | |
Archit Taneja | 862e686 | 2015-05-21 11:03:16 +0530 | [diff] [blame] | 925 | drm_bridge_enable(encoder->bridge); |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 926 | } |
| 927 | } |
Daniel Vetter | 1af434a | 2015-02-22 12:24:19 +0100 | [diff] [blame] | 928 | EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_enables); |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 929 | |
Daniel Vetter | e2330f0 | 2014-10-29 11:34:56 +0100 | [diff] [blame] | 930 | static void wait_for_fences(struct drm_device *dev, |
| 931 | struct drm_atomic_state *state) |
| 932 | { |
Ander Conselvan de Oliveira | df63b99 | 2015-04-10 14:58:39 +0300 | [diff] [blame] | 933 | struct drm_plane *plane; |
| 934 | struct drm_plane_state *plane_state; |
Daniel Vetter | e2330f0 | 2014-10-29 11:34:56 +0100 | [diff] [blame] | 935 | int i; |
| 936 | |
Ander Conselvan de Oliveira | df63b99 | 2015-04-10 14:58:39 +0300 | [diff] [blame] | 937 | for_each_plane_in_state(state, plane, plane_state, i) { |
| 938 | if (!plane->state->fence) |
Daniel Vetter | e2330f0 | 2014-10-29 11:34:56 +0100 | [diff] [blame] | 939 | continue; |
| 940 | |
| 941 | WARN_ON(!plane->state->fb); |
| 942 | |
| 943 | fence_wait(plane->state->fence, false); |
| 944 | fence_put(plane->state->fence); |
| 945 | plane->state->fence = NULL; |
| 946 | } |
| 947 | } |
| 948 | |
John Keeping | c240906 | 2016-01-19 10:46:58 +0000 | [diff] [blame] | 949 | /** |
| 950 | * drm_atomic_helper_framebuffer_changed - check if framebuffer has changed |
| 951 | * @dev: DRM device |
| 952 | * @old_state: atomic state object with old state structures |
| 953 | * @crtc: DRM crtc |
| 954 | * |
| 955 | * Checks whether the framebuffer used for this CRTC changes as a result of |
| 956 | * the atomic update. This is useful for drivers which cannot use |
| 957 | * drm_atomic_helper_wait_for_vblanks() and need to reimplement its |
| 958 | * functionality. |
| 959 | * |
| 960 | * Returns: |
| 961 | * true if the framebuffer changed. |
| 962 | */ |
| 963 | bool drm_atomic_helper_framebuffer_changed(struct drm_device *dev, |
| 964 | struct drm_atomic_state *old_state, |
| 965 | struct drm_crtc *crtc) |
Daniel Vetter | ab58e33 | 2014-11-24 20:42:42 +0100 | [diff] [blame] | 966 | { |
| 967 | struct drm_plane *plane; |
| 968 | struct drm_plane_state *old_plane_state; |
Daniel Vetter | ab58e33 | 2014-11-24 20:42:42 +0100 | [diff] [blame] | 969 | int i; |
| 970 | |
Ander Conselvan de Oliveira | df63b99 | 2015-04-10 14:58:39 +0300 | [diff] [blame] | 971 | for_each_plane_in_state(old_state, plane, old_plane_state, i) { |
Daniel Vetter | ab58e33 | 2014-11-24 20:42:42 +0100 | [diff] [blame] | 972 | if (plane->state->crtc != crtc && |
| 973 | old_plane_state->crtc != crtc) |
| 974 | continue; |
| 975 | |
| 976 | if (plane->state->fb != old_plane_state->fb) |
| 977 | return true; |
| 978 | } |
| 979 | |
| 980 | return false; |
| 981 | } |
John Keeping | c240906 | 2016-01-19 10:46:58 +0000 | [diff] [blame] | 982 | EXPORT_SYMBOL(drm_atomic_helper_framebuffer_changed); |
Daniel Vetter | ab58e33 | 2014-11-24 20:42:42 +0100 | [diff] [blame] | 983 | |
Rob Clark | 5ee3229 | 2014-11-11 19:38:59 -0500 | [diff] [blame] | 984 | /** |
| 985 | * drm_atomic_helper_wait_for_vblanks - wait for vblank on crtcs |
| 986 | * @dev: DRM device |
| 987 | * @old_state: atomic state object with old state structures |
| 988 | * |
| 989 | * Helper to, after atomic commit, wait for vblanks on all effected |
| 990 | * crtcs (ie. before cleaning up old framebuffers using |
Daniel Vetter | ab58e33 | 2014-11-24 20:42:42 +0100 | [diff] [blame] | 991 | * drm_atomic_helper_cleanup_planes()). It will only wait on crtcs where the |
| 992 | * framebuffers have actually changed to optimize for the legacy cursor and |
| 993 | * plane update use-case. |
Rob Clark | 5ee3229 | 2014-11-11 19:38:59 -0500 | [diff] [blame] | 994 | */ |
| 995 | void |
| 996 | drm_atomic_helper_wait_for_vblanks(struct drm_device *dev, |
| 997 | struct drm_atomic_state *old_state) |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 998 | { |
| 999 | struct drm_crtc *crtc; |
| 1000 | struct drm_crtc_state *old_crtc_state; |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 1001 | int i, ret; |
| 1002 | |
Ander Conselvan de Oliveira | df63b99 | 2015-04-10 14:58:39 +0300 | [diff] [blame] | 1003 | for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) { |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 1004 | /* No one cares about the old state, so abuse it for tracking |
| 1005 | * and store whether we hold a vblank reference (and should do a |
| 1006 | * vblank wait) in the ->enable boolean. */ |
| 1007 | old_crtc_state->enable = false; |
| 1008 | |
| 1009 | if (!crtc->state->enable) |
| 1010 | continue; |
| 1011 | |
Daniel Vetter | f02ad90 | 2015-01-22 16:36:23 +0100 | [diff] [blame] | 1012 | /* Legacy cursor ioctls are completely unsynced, and userspace |
| 1013 | * relies on that (by doing tons of cursor updates). */ |
| 1014 | if (old_state->legacy_cursor_update) |
| 1015 | continue; |
| 1016 | |
John Keeping | c240906 | 2016-01-19 10:46:58 +0000 | [diff] [blame] | 1017 | if (!drm_atomic_helper_framebuffer_changed(dev, |
| 1018 | old_state, crtc)) |
Daniel Vetter | ab58e33 | 2014-11-24 20:42:42 +0100 | [diff] [blame] | 1019 | continue; |
| 1020 | |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 1021 | ret = drm_crtc_vblank_get(crtc); |
| 1022 | if (ret != 0) |
| 1023 | continue; |
| 1024 | |
| 1025 | old_crtc_state->enable = true; |
Thierry Reding | d485363 | 2015-08-12 17:00:35 +0200 | [diff] [blame] | 1026 | old_crtc_state->last_vblank_count = drm_crtc_vblank_count(crtc); |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 1027 | } |
| 1028 | |
Ander Conselvan de Oliveira | df63b99 | 2015-04-10 14:58:39 +0300 | [diff] [blame] | 1029 | for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) { |
| 1030 | if (!old_crtc_state->enable) |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 1031 | continue; |
| 1032 | |
| 1033 | ret = wait_event_timeout(dev->vblank[i].queue, |
| 1034 | old_crtc_state->last_vblank_count != |
Thierry Reding | d485363 | 2015-08-12 17:00:35 +0200 | [diff] [blame] | 1035 | drm_crtc_vblank_count(crtc), |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 1036 | msecs_to_jiffies(50)); |
| 1037 | |
| 1038 | drm_crtc_vblank_put(crtc); |
| 1039 | } |
| 1040 | } |
Rob Clark | 5ee3229 | 2014-11-11 19:38:59 -0500 | [diff] [blame] | 1041 | EXPORT_SYMBOL(drm_atomic_helper_wait_for_vblanks); |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 1042 | |
| 1043 | /** |
| 1044 | * drm_atomic_helper_commit - commit validated state object |
| 1045 | * @dev: DRM device |
| 1046 | * @state: the driver state object |
| 1047 | * @async: asynchronous commit |
| 1048 | * |
| 1049 | * This function commits a with drm_atomic_helper_check() pre-validated state |
| 1050 | * object. This can still fail when e.g. the framebuffer reservation fails. For |
| 1051 | * now this doesn't implement asynchronous commits. |
| 1052 | * |
Daniel Vetter | 6e48ae3 | 2015-09-08 13:52:45 +0200 | [diff] [blame] | 1053 | * Note that right now this function does not support async commits, and hence |
| 1054 | * driver writers must implement their own version for now. Also note that the |
| 1055 | * default ordering of how the various stages are called is to match the legacy |
| 1056 | * modeset helper library closest. One peculiarity of that is that it doesn't |
| 1057 | * mesh well with runtime PM at all. |
| 1058 | * |
| 1059 | * For drivers supporting runtime PM the recommended sequence is |
| 1060 | * |
| 1061 | * drm_atomic_helper_commit_modeset_disables(dev, state); |
| 1062 | * |
| 1063 | * drm_atomic_helper_commit_modeset_enables(dev, state); |
| 1064 | * |
| 1065 | * drm_atomic_helper_commit_planes(dev, state, true); |
| 1066 | * |
| 1067 | * See the kerneldoc entries for these three functions for more details. |
| 1068 | * |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 1069 | * RETURNS |
| 1070 | * Zero for success or -errno. |
| 1071 | */ |
| 1072 | int drm_atomic_helper_commit(struct drm_device *dev, |
| 1073 | struct drm_atomic_state *state, |
| 1074 | bool async) |
| 1075 | { |
| 1076 | int ret; |
| 1077 | |
| 1078 | if (async) |
| 1079 | return -EBUSY; |
| 1080 | |
| 1081 | ret = drm_atomic_helper_prepare_planes(dev, state); |
| 1082 | if (ret) |
| 1083 | return ret; |
| 1084 | |
| 1085 | /* |
| 1086 | * This is the point of no return - everything below never fails except |
| 1087 | * when the hw goes bonghits. Which means we can commit the new state on |
| 1088 | * the software side now. |
| 1089 | */ |
| 1090 | |
| 1091 | drm_atomic_helper_swap_state(dev, state); |
| 1092 | |
| 1093 | /* |
| 1094 | * Everything below can be run asynchronously without the need to grab |
John Hunter | f98bd3e | 2015-03-17 15:30:26 +0800 | [diff] [blame] | 1095 | * any modeset locks at all under one condition: It must be guaranteed |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 1096 | * that the asynchronous work has either been cancelled (if the driver |
| 1097 | * supports it, which at least requires that the framebuffers get |
| 1098 | * cleaned up with drm_atomic_helper_cleanup_planes()) or completed |
| 1099 | * before the new state gets committed on the software side with |
| 1100 | * drm_atomic_helper_swap_state(). |
| 1101 | * |
| 1102 | * This scheme allows new atomic state updates to be prepared and |
| 1103 | * checked in parallel to the asynchronous completion of the previous |
| 1104 | * update. Which is important since compositors need to figure out the |
| 1105 | * composition of the next frame right after having submitted the |
| 1106 | * current layout. |
| 1107 | */ |
| 1108 | |
Daniel Vetter | e2330f0 | 2014-10-29 11:34:56 +0100 | [diff] [blame] | 1109 | wait_for_fences(dev, state); |
| 1110 | |
Daniel Vetter | 1af434a | 2015-02-22 12:24:19 +0100 | [diff] [blame] | 1111 | drm_atomic_helper_commit_modeset_disables(dev, state); |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 1112 | |
Daniel Vetter | aef9dbb | 2015-09-08 12:02:07 +0200 | [diff] [blame] | 1113 | drm_atomic_helper_commit_planes(dev, state, false); |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 1114 | |
Daniel Vetter | 1af434a | 2015-02-22 12:24:19 +0100 | [diff] [blame] | 1115 | drm_atomic_helper_commit_modeset_enables(dev, state); |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 1116 | |
Rob Clark | 5ee3229 | 2014-11-11 19:38:59 -0500 | [diff] [blame] | 1117 | drm_atomic_helper_wait_for_vblanks(dev, state); |
Daniel Vetter | 623369e | 2014-09-16 17:50:47 +0200 | [diff] [blame] | 1118 | |
| 1119 | drm_atomic_helper_cleanup_planes(dev, state); |
| 1120 | |
| 1121 | drm_atomic_state_free(state); |
| 1122 | |
| 1123 | return 0; |
| 1124 | } |
| 1125 | EXPORT_SYMBOL(drm_atomic_helper_commit); |
| 1126 | |
Daniel Vetter | c2fcd27 | 2014-11-05 00:14:14 +0100 | [diff] [blame] | 1127 | /** |
Daniel Vetter | e8c833a | 2014-07-27 18:30:19 +0200 | [diff] [blame] | 1128 | * DOC: implementing async commit |
| 1129 | * |
| 1130 | * For now the atomic helpers don't support async commit directly. If there is |
| 1131 | * real need it could be added though, using the dma-buf fence infrastructure |
| 1132 | * for generic synchronization with outstanding rendering. |
| 1133 | * |
| 1134 | * For now drivers have to implement async commit themselves, with the following |
| 1135 | * sequence being the recommended one: |
| 1136 | * |
| 1137 | * 1. Run drm_atomic_helper_prepare_planes() first. This is the only function |
| 1138 | * which commit needs to call which can fail, so we want to run it first and |
| 1139 | * synchronously. |
| 1140 | * |
| 1141 | * 2. Synchronize with any outstanding asynchronous commit worker threads which |
| 1142 | * might be affected the new state update. This can be done by either cancelling |
| 1143 | * or flushing the work items, depending upon whether the driver can deal with |
| 1144 | * cancelled updates. Note that it is important to ensure that the framebuffer |
| 1145 | * cleanup is still done when cancelling. |
| 1146 | * |
| 1147 | * For sufficient parallelism it is recommended to have a work item per crtc |
| 1148 | * (for updates which don't touch global state) and a global one. Then we only |
| 1149 | * need to synchronize with the crtc work items for changed crtcs and the global |
| 1150 | * work item, which allows nice concurrent updates on disjoint sets of crtcs. |
| 1151 | * |
| 1152 | * 3. The software state is updated synchronously with |
Daniel Vetter | 26196f7 | 2015-08-25 16:26:03 +0200 | [diff] [blame] | 1153 | * drm_atomic_helper_swap_state(). Doing this under the protection of all modeset |
Daniel Vetter | e8c833a | 2014-07-27 18:30:19 +0200 | [diff] [blame] | 1154 | * locks means concurrent callers never see inconsistent state. And doing this |
| 1155 | * while it's guaranteed that no relevant async worker runs means that async |
| 1156 | * workers do not need grab any locks. Actually they must not grab locks, for |
| 1157 | * otherwise the work flushing will deadlock. |
| 1158 | * |
| 1159 | * 4. Schedule a work item to do all subsequent steps, using the split-out |
| 1160 | * commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and |
| 1161 | * then cleaning up the framebuffers after the old framebuffer is no longer |
| 1162 | * being displayed. |
| 1163 | */ |
| 1164 | |
| 1165 | /** |
Daniel Vetter | 2e3afd4 | 2015-02-26 14:17:38 +0100 | [diff] [blame] | 1166 | * drm_atomic_helper_prepare_planes - prepare plane resources before commit |
Daniel Vetter | c2fcd27 | 2014-11-05 00:14:14 +0100 | [diff] [blame] | 1167 | * @dev: DRM device |
Daniel Vetter | 2e3afd4 | 2015-02-26 14:17:38 +0100 | [diff] [blame] | 1168 | * @state: atomic state object with new state structures |
Daniel Vetter | c2fcd27 | 2014-11-05 00:14:14 +0100 | [diff] [blame] | 1169 | * |
| 1170 | * This function prepares plane state, specifically framebuffers, for the new |
| 1171 | * configuration. If any failure is encountered this function will call |
| 1172 | * ->cleanup_fb on any already successfully prepared framebuffer. |
| 1173 | * |
| 1174 | * Returns: |
| 1175 | * 0 on success, negative error code on failure. |
| 1176 | */ |
| 1177 | int drm_atomic_helper_prepare_planes(struct drm_device *dev, |
| 1178 | struct drm_atomic_state *state) |
| 1179 | { |
| 1180 | int nplanes = dev->mode_config.num_total_plane; |
| 1181 | int ret, i; |
| 1182 | |
| 1183 | for (i = 0; i < nplanes; i++) { |
Ville Syrjälä | b5ceff2 | 2015-03-10 14:35:20 +0200 | [diff] [blame] | 1184 | const struct drm_plane_helper_funcs *funcs; |
Daniel Vetter | c2fcd27 | 2014-11-05 00:14:14 +0100 | [diff] [blame] | 1185 | struct drm_plane *plane = state->planes[i]; |
Tvrtko Ursulin | d136dfe | 2015-03-03 14:22:31 +0000 | [diff] [blame] | 1186 | struct drm_plane_state *plane_state = state->plane_states[i]; |
Daniel Vetter | c2fcd27 | 2014-11-05 00:14:14 +0100 | [diff] [blame] | 1187 | |
| 1188 | if (!plane) |
| 1189 | continue; |
| 1190 | |
| 1191 | funcs = plane->helper_private; |
| 1192 | |
Maarten Lankhorst | 844f911 | 2015-09-02 10:42:40 +0200 | [diff] [blame] | 1193 | if (funcs->prepare_fb) { |
| 1194 | ret = funcs->prepare_fb(plane, plane_state); |
Daniel Vetter | c2fcd27 | 2014-11-05 00:14:14 +0100 | [diff] [blame] | 1195 | if (ret) |
| 1196 | goto fail; |
| 1197 | } |
| 1198 | } |
| 1199 | |
| 1200 | return 0; |
| 1201 | |
| 1202 | fail: |
| 1203 | for (i--; i >= 0; i--) { |
Ville Syrjälä | b5ceff2 | 2015-03-10 14:35:20 +0200 | [diff] [blame] | 1204 | const struct drm_plane_helper_funcs *funcs; |
Daniel Vetter | c2fcd27 | 2014-11-05 00:14:14 +0100 | [diff] [blame] | 1205 | struct drm_plane *plane = state->planes[i]; |
Tvrtko Ursulin | d136dfe | 2015-03-03 14:22:31 +0000 | [diff] [blame] | 1206 | struct drm_plane_state *plane_state = state->plane_states[i]; |
Daniel Vetter | c2fcd27 | 2014-11-05 00:14:14 +0100 | [diff] [blame] | 1207 | |
| 1208 | if (!plane) |
| 1209 | continue; |
| 1210 | |
| 1211 | funcs = plane->helper_private; |
| 1212 | |
Maarten Lankhorst | 844f911 | 2015-09-02 10:42:40 +0200 | [diff] [blame] | 1213 | if (funcs->cleanup_fb) |
| 1214 | funcs->cleanup_fb(plane, plane_state); |
Daniel Vetter | c2fcd27 | 2014-11-05 00:14:14 +0100 | [diff] [blame] | 1215 | |
| 1216 | } |
| 1217 | |
| 1218 | return ret; |
| 1219 | } |
| 1220 | EXPORT_SYMBOL(drm_atomic_helper_prepare_planes); |
| 1221 | |
Daniel Vetter | aef9dbb | 2015-09-08 12:02:07 +0200 | [diff] [blame] | 1222 | bool plane_crtc_active(struct drm_plane_state *state) |
| 1223 | { |
| 1224 | return state->crtc && state->crtc->state->active; |
| 1225 | } |
| 1226 | |
Daniel Vetter | c2fcd27 | 2014-11-05 00:14:14 +0100 | [diff] [blame] | 1227 | /** |
| 1228 | * drm_atomic_helper_commit_planes - commit plane state |
| 1229 | * @dev: DRM device |
Daniel Vetter | b0fcfc8 | 2014-11-19 18:38:11 +0100 | [diff] [blame] | 1230 | * @old_state: atomic state object with old state structures |
Daniel Vetter | aef9dbb | 2015-09-08 12:02:07 +0200 | [diff] [blame] | 1231 | * @active_only: Only commit on active CRTC if set |
Daniel Vetter | c2fcd27 | 2014-11-05 00:14:14 +0100 | [diff] [blame] | 1232 | * |
| 1233 | * This function commits the new plane state using the plane and atomic helper |
| 1234 | * functions for planes and crtcs. It assumes that the atomic state has already |
| 1235 | * been pushed into the relevant object state pointers, since this step can no |
| 1236 | * longer fail. |
| 1237 | * |
Daniel Vetter | b0fcfc8 | 2014-11-19 18:38:11 +0100 | [diff] [blame] | 1238 | * It still requires the global state object @old_state to know which planes and |
Daniel Vetter | c2fcd27 | 2014-11-05 00:14:14 +0100 | [diff] [blame] | 1239 | * crtcs need to be updated though. |
Maarten Lankhorst | de28d02 | 2015-05-19 16:41:01 +0200 | [diff] [blame] | 1240 | * |
| 1241 | * Note that this function does all plane updates across all CRTCs in one step. |
| 1242 | * If the hardware can't support this approach look at |
| 1243 | * drm_atomic_helper_commit_planes_on_crtc() instead. |
Daniel Vetter | 6e48ae3 | 2015-09-08 13:52:45 +0200 | [diff] [blame] | 1244 | * |
| 1245 | * Plane parameters can be updated by applications while the associated CRTC is |
| 1246 | * disabled. The DRM/KMS core will store the parameters in the plane state, |
| 1247 | * which will be available to the driver when the CRTC is turned on. As a result |
| 1248 | * most drivers don't need to be immediately notified of plane updates for a |
| 1249 | * disabled CRTC. |
| 1250 | * |
| 1251 | * Unless otherwise needed, drivers are advised to set the @active_only |
| 1252 | * parameters to true in order not to receive plane update notifications related |
| 1253 | * to a disabled CRTC. This avoids the need to manually ignore plane updates in |
| 1254 | * driver code when the driver and/or hardware can't or just don't need to deal |
| 1255 | * with updates on disabled CRTCs, for example when supporting runtime PM. |
| 1256 | * |
| 1257 | * The drm_atomic_helper_commit() default implementation only sets @active_only |
| 1258 | * to false to most closely match the behaviour of the legacy helpers. This should |
| 1259 | * not be copied blindly by drivers. |
Daniel Vetter | c2fcd27 | 2014-11-05 00:14:14 +0100 | [diff] [blame] | 1260 | */ |
| 1261 | void drm_atomic_helper_commit_planes(struct drm_device *dev, |
Daniel Vetter | aef9dbb | 2015-09-08 12:02:07 +0200 | [diff] [blame] | 1262 | struct drm_atomic_state *old_state, |
| 1263 | bool active_only) |
Daniel Vetter | c2fcd27 | 2014-11-05 00:14:14 +0100 | [diff] [blame] | 1264 | { |
Ander Conselvan de Oliveira | df63b99 | 2015-04-10 14:58:39 +0300 | [diff] [blame] | 1265 | struct drm_crtc *crtc; |
| 1266 | struct drm_crtc_state *old_crtc_state; |
| 1267 | struct drm_plane *plane; |
| 1268 | struct drm_plane_state *old_plane_state; |
Daniel Vetter | c2fcd27 | 2014-11-05 00:14:14 +0100 | [diff] [blame] | 1269 | int i; |
| 1270 | |
Ander Conselvan de Oliveira | df63b99 | 2015-04-10 14:58:39 +0300 | [diff] [blame] | 1271 | for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) { |
Ville Syrjälä | b5ceff2 | 2015-03-10 14:35:20 +0200 | [diff] [blame] | 1272 | const struct drm_crtc_helper_funcs *funcs; |
Daniel Vetter | c2fcd27 | 2014-11-05 00:14:14 +0100 | [diff] [blame] | 1273 | |
| 1274 | funcs = crtc->helper_private; |
| 1275 | |
| 1276 | if (!funcs || !funcs->atomic_begin) |
| 1277 | continue; |
| 1278 | |
Daniel Vetter | aef9dbb | 2015-09-08 12:02:07 +0200 | [diff] [blame] | 1279 | if (active_only && !crtc->state->active) |
| 1280 | continue; |
| 1281 | |
Maarten Lankhorst | 613d2b2 | 2015-07-21 13:28:58 +0200 | [diff] [blame] | 1282 | funcs->atomic_begin(crtc, old_crtc_state); |
Daniel Vetter | c2fcd27 | 2014-11-05 00:14:14 +0100 | [diff] [blame] | 1283 | } |
| 1284 | |
Ander Conselvan de Oliveira | df63b99 | 2015-04-10 14:58:39 +0300 | [diff] [blame] | 1285 | for_each_plane_in_state(old_state, plane, old_plane_state, i) { |
Ville Syrjälä | b5ceff2 | 2015-03-10 14:35:20 +0200 | [diff] [blame] | 1286 | const struct drm_plane_helper_funcs *funcs; |
Laurent Pinchart | 216c59d | 2015-09-11 00:07:19 +0300 | [diff] [blame] | 1287 | bool disabling; |
Daniel Vetter | c2fcd27 | 2014-11-05 00:14:14 +0100 | [diff] [blame] | 1288 | |
| 1289 | funcs = plane->helper_private; |
| 1290 | |
Thierry Reding | 3cad4b6 | 2014-11-25 13:05:12 +0100 | [diff] [blame] | 1291 | if (!funcs) |
Daniel Vetter | c2fcd27 | 2014-11-05 00:14:14 +0100 | [diff] [blame] | 1292 | continue; |
| 1293 | |
Laurent Pinchart | 216c59d | 2015-09-11 00:07:19 +0300 | [diff] [blame] | 1294 | disabling = drm_atomic_plane_disabling(plane, old_plane_state); |
| 1295 | |
| 1296 | if (active_only) { |
| 1297 | /* |
| 1298 | * Skip planes related to inactive CRTCs. If the plane |
| 1299 | * is enabled use the state of the current CRTC. If the |
| 1300 | * plane is being disabled use the state of the old |
| 1301 | * CRTC to avoid skipping planes being disabled on an |
| 1302 | * active CRTC. |
| 1303 | */ |
| 1304 | if (!disabling && !plane_crtc_active(plane->state)) |
| 1305 | continue; |
| 1306 | if (disabling && !plane_crtc_active(old_plane_state)) |
| 1307 | continue; |
| 1308 | } |
Daniel Vetter | aef9dbb | 2015-09-08 12:02:07 +0200 | [diff] [blame] | 1309 | |
Thierry Reding | 407b8bd | 2014-11-20 12:05:50 +0100 | [diff] [blame] | 1310 | /* |
| 1311 | * Special-case disabling the plane if drivers support it. |
| 1312 | */ |
Laurent Pinchart | 216c59d | 2015-09-11 00:07:19 +0300 | [diff] [blame] | 1313 | if (disabling && funcs->atomic_disable) |
Thierry Reding | 407b8bd | 2014-11-20 12:05:50 +0100 | [diff] [blame] | 1314 | funcs->atomic_disable(plane, old_plane_state); |
Laurent Pinchart | 216c59d | 2015-09-11 00:07:19 +0300 | [diff] [blame] | 1315 | else if (plane->state->crtc || disabling) |
Thierry Reding | 407b8bd | 2014-11-20 12:05:50 +0100 | [diff] [blame] | 1316 | funcs->atomic_update(plane, old_plane_state); |
Daniel Vetter | c2fcd27 | 2014-11-05 00:14:14 +0100 | [diff] [blame] | 1317 | } |
| 1318 | |
Ander Conselvan de Oliveira | df63b99 | 2015-04-10 14:58:39 +0300 | [diff] [blame] | 1319 | for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) { |
Ville Syrjälä | b5ceff2 | 2015-03-10 14:35:20 +0200 | [diff] [blame] | 1320 | const struct drm_crtc_helper_funcs *funcs; |
Daniel Vetter | c2fcd27 | 2014-11-05 00:14:14 +0100 | [diff] [blame] | 1321 | |
| 1322 | funcs = crtc->helper_private; |
| 1323 | |
| 1324 | if (!funcs || !funcs->atomic_flush) |
| 1325 | continue; |
| 1326 | |
Daniel Vetter | aef9dbb | 2015-09-08 12:02:07 +0200 | [diff] [blame] | 1327 | if (active_only && !crtc->state->active) |
| 1328 | continue; |
| 1329 | |
Maarten Lankhorst | 613d2b2 | 2015-07-21 13:28:58 +0200 | [diff] [blame] | 1330 | funcs->atomic_flush(crtc, old_crtc_state); |
Daniel Vetter | c2fcd27 | 2014-11-05 00:14:14 +0100 | [diff] [blame] | 1331 | } |
| 1332 | } |
| 1333 | EXPORT_SYMBOL(drm_atomic_helper_commit_planes); |
| 1334 | |
| 1335 | /** |
Maarten Lankhorst | de28d02 | 2015-05-19 16:41:01 +0200 | [diff] [blame] | 1336 | * drm_atomic_helper_commit_planes_on_crtc - commit plane state for a crtc |
| 1337 | * @old_crtc_state: atomic state object with the old crtc state |
| 1338 | * |
| 1339 | * This function commits the new plane state using the plane and atomic helper |
| 1340 | * functions for planes on the specific crtc. It assumes that the atomic state |
| 1341 | * has already been pushed into the relevant object state pointers, since this |
| 1342 | * step can no longer fail. |
| 1343 | * |
| 1344 | * This function is useful when plane updates should be done crtc-by-crtc |
| 1345 | * instead of one global step like drm_atomic_helper_commit_planes() does. |
| 1346 | * |
| 1347 | * This function can only be savely used when planes are not allowed to move |
| 1348 | * between different CRTCs because this function doesn't handle inter-CRTC |
| 1349 | * depencies. Callers need to ensure that either no such depencies exist, |
| 1350 | * resolve them through ordering of commit calls or through some other means. |
| 1351 | */ |
| 1352 | void |
| 1353 | drm_atomic_helper_commit_planes_on_crtc(struct drm_crtc_state *old_crtc_state) |
| 1354 | { |
| 1355 | const struct drm_crtc_helper_funcs *crtc_funcs; |
| 1356 | struct drm_crtc *crtc = old_crtc_state->crtc; |
| 1357 | struct drm_atomic_state *old_state = old_crtc_state->state; |
| 1358 | struct drm_plane *plane; |
| 1359 | unsigned plane_mask; |
| 1360 | |
| 1361 | plane_mask = old_crtc_state->plane_mask; |
| 1362 | plane_mask |= crtc->state->plane_mask; |
| 1363 | |
| 1364 | crtc_funcs = crtc->helper_private; |
| 1365 | if (crtc_funcs && crtc_funcs->atomic_begin) |
Maarten Lankhorst | 613d2b2 | 2015-07-21 13:28:58 +0200 | [diff] [blame] | 1366 | crtc_funcs->atomic_begin(crtc, old_crtc_state); |
Maarten Lankhorst | de28d02 | 2015-05-19 16:41:01 +0200 | [diff] [blame] | 1367 | |
| 1368 | drm_for_each_plane_mask(plane, crtc->dev, plane_mask) { |
| 1369 | struct drm_plane_state *old_plane_state = |
| 1370 | drm_atomic_get_existing_plane_state(old_state, plane); |
| 1371 | const struct drm_plane_helper_funcs *plane_funcs; |
| 1372 | |
| 1373 | plane_funcs = plane->helper_private; |
| 1374 | |
| 1375 | if (!old_plane_state || !plane_funcs) |
| 1376 | continue; |
| 1377 | |
| 1378 | WARN_ON(plane->state->crtc && plane->state->crtc != crtc); |
| 1379 | |
| 1380 | if (drm_atomic_plane_disabling(plane, old_plane_state) && |
| 1381 | plane_funcs->atomic_disable) |
| 1382 | plane_funcs->atomic_disable(plane, old_plane_state); |
| 1383 | else if (plane->state->crtc || |
| 1384 | drm_atomic_plane_disabling(plane, old_plane_state)) |
| 1385 | plane_funcs->atomic_update(plane, old_plane_state); |
| 1386 | } |
| 1387 | |
| 1388 | if (crtc_funcs && crtc_funcs->atomic_flush) |
Maarten Lankhorst | 613d2b2 | 2015-07-21 13:28:58 +0200 | [diff] [blame] | 1389 | crtc_funcs->atomic_flush(crtc, old_crtc_state); |
Maarten Lankhorst | de28d02 | 2015-05-19 16:41:01 +0200 | [diff] [blame] | 1390 | } |
| 1391 | EXPORT_SYMBOL(drm_atomic_helper_commit_planes_on_crtc); |
| 1392 | |
| 1393 | /** |
Jyri Sarha | 6753ba9 | 2015-11-27 16:14:01 +0200 | [diff] [blame] | 1394 | * drm_atomic_helper_disable_planes_on_crtc - helper to disable CRTC's planes |
| 1395 | * @crtc: CRTC |
| 1396 | * @atomic: if set, synchronize with CRTC's atomic_begin/flush hooks |
| 1397 | * |
| 1398 | * Disables all planes associated with the given CRTC. This can be |
| 1399 | * used for instance in the CRTC helper disable callback to disable |
| 1400 | * all planes before shutting down the display pipeline. |
| 1401 | * |
| 1402 | * If the atomic-parameter is set the function calls the CRTC's |
| 1403 | * atomic_begin hook before and atomic_flush hook after disabling the |
| 1404 | * planes. |
| 1405 | * |
| 1406 | * It is a bug to call this function without having implemented the |
| 1407 | * ->atomic_disable() plane hook. |
| 1408 | */ |
| 1409 | void drm_atomic_helper_disable_planes_on_crtc(struct drm_crtc *crtc, |
| 1410 | bool atomic) |
| 1411 | { |
| 1412 | const struct drm_crtc_helper_funcs *crtc_funcs = |
| 1413 | crtc->helper_private; |
| 1414 | struct drm_plane *plane; |
| 1415 | |
| 1416 | if (atomic && crtc_funcs && crtc_funcs->atomic_begin) |
| 1417 | crtc_funcs->atomic_begin(crtc, NULL); |
| 1418 | |
| 1419 | drm_for_each_plane(plane, crtc->dev) { |
| 1420 | const struct drm_plane_helper_funcs *plane_funcs = |
| 1421 | plane->helper_private; |
| 1422 | |
| 1423 | if (plane->state->crtc != crtc || !plane_funcs) |
| 1424 | continue; |
| 1425 | |
| 1426 | WARN_ON(!plane_funcs->atomic_disable); |
| 1427 | if (plane_funcs->atomic_disable) |
| 1428 | plane_funcs->atomic_disable(plane, NULL); |
| 1429 | } |
| 1430 | |
| 1431 | if (atomic && crtc_funcs && crtc_funcs->atomic_flush) |
| 1432 | crtc_funcs->atomic_flush(crtc, NULL); |
| 1433 | } |
| 1434 | EXPORT_SYMBOL(drm_atomic_helper_disable_planes_on_crtc); |
| 1435 | |
| 1436 | /** |
Daniel Vetter | c2fcd27 | 2014-11-05 00:14:14 +0100 | [diff] [blame] | 1437 | * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit |
| 1438 | * @dev: DRM device |
| 1439 | * @old_state: atomic state object with old state structures |
| 1440 | * |
| 1441 | * This function cleans up plane state, specifically framebuffers, from the old |
| 1442 | * configuration. Hence the old configuration must be perserved in @old_state to |
| 1443 | * be able to call this function. |
| 1444 | * |
| 1445 | * This function must also be called on the new state when the atomic update |
| 1446 | * fails at any point after calling drm_atomic_helper_prepare_planes(). |
| 1447 | */ |
| 1448 | void drm_atomic_helper_cleanup_planes(struct drm_device *dev, |
| 1449 | struct drm_atomic_state *old_state) |
| 1450 | { |
Ander Conselvan de Oliveira | df63b99 | 2015-04-10 14:58:39 +0300 | [diff] [blame] | 1451 | struct drm_plane *plane; |
| 1452 | struct drm_plane_state *plane_state; |
Daniel Vetter | c2fcd27 | 2014-11-05 00:14:14 +0100 | [diff] [blame] | 1453 | int i; |
| 1454 | |
Ander Conselvan de Oliveira | df63b99 | 2015-04-10 14:58:39 +0300 | [diff] [blame] | 1455 | for_each_plane_in_state(old_state, plane, plane_state, i) { |
Ville Syrjälä | b5ceff2 | 2015-03-10 14:35:20 +0200 | [diff] [blame] | 1456 | const struct drm_plane_helper_funcs *funcs; |
Daniel Vetter | c2fcd27 | 2014-11-05 00:14:14 +0100 | [diff] [blame] | 1457 | |
Daniel Vetter | c2fcd27 | 2014-11-05 00:14:14 +0100 | [diff] [blame] | 1458 | funcs = plane->helper_private; |
| 1459 | |
Maarten Lankhorst | 844f911 | 2015-09-02 10:42:40 +0200 | [diff] [blame] | 1460 | if (funcs->cleanup_fb) |
| 1461 | funcs->cleanup_fb(plane, plane_state); |
Daniel Vetter | c2fcd27 | 2014-11-05 00:14:14 +0100 | [diff] [blame] | 1462 | } |
| 1463 | } |
| 1464 | EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes); |
| 1465 | |
| 1466 | /** |
| 1467 | * drm_atomic_helper_swap_state - store atomic state into current sw state |
| 1468 | * @dev: DRM device |
| 1469 | * @state: atomic state |
| 1470 | * |
| 1471 | * This function stores the atomic state into the current state pointers in all |
| 1472 | * driver objects. It should be called after all failing steps have been done |
| 1473 | * and succeeded, but before the actual hardware state is committed. |
| 1474 | * |
| 1475 | * For cleanup and error recovery the current state for all changed objects will |
| 1476 | * be swaped into @state. |
| 1477 | * |
| 1478 | * With that sequence it fits perfectly into the plane prepare/cleanup sequence: |
| 1479 | * |
| 1480 | * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state. |
| 1481 | * |
| 1482 | * 2. Do any other steps that might fail. |
| 1483 | * |
| 1484 | * 3. Put the staged state into the current state pointers with this function. |
| 1485 | * |
| 1486 | * 4. Actually commit the hardware state. |
| 1487 | * |
Daniel Vetter | 26196f7 | 2015-08-25 16:26:03 +0200 | [diff] [blame] | 1488 | * 5. Call drm_atomic_helper_cleanup_planes() with @state, which since step 3 |
Daniel Vetter | c2fcd27 | 2014-11-05 00:14:14 +0100 | [diff] [blame] | 1489 | * contains the old state. Also do any other cleanup required with that state. |
| 1490 | */ |
| 1491 | void drm_atomic_helper_swap_state(struct drm_device *dev, |
| 1492 | struct drm_atomic_state *state) |
| 1493 | { |
| 1494 | int i; |
| 1495 | |
Maarten Lankhorst | 5fff80b | 2016-02-17 08:32:05 +0100 | [diff] [blame] | 1496 | for (i = 0; i < state->num_connector; i++) { |
Daniel Vetter | c2fcd27 | 2014-11-05 00:14:14 +0100 | [diff] [blame] | 1497 | struct drm_connector *connector = state->connectors[i]; |
| 1498 | |
| 1499 | if (!connector) |
| 1500 | continue; |
| 1501 | |
| 1502 | connector->state->state = state; |
| 1503 | swap(state->connector_states[i], connector->state); |
| 1504 | connector->state->state = NULL; |
| 1505 | } |
| 1506 | |
| 1507 | for (i = 0; i < dev->mode_config.num_crtc; i++) { |
| 1508 | struct drm_crtc *crtc = state->crtcs[i]; |
| 1509 | |
| 1510 | if (!crtc) |
| 1511 | continue; |
| 1512 | |
| 1513 | crtc->state->state = state; |
| 1514 | swap(state->crtc_states[i], crtc->state); |
| 1515 | crtc->state->state = NULL; |
| 1516 | } |
| 1517 | |
| 1518 | for (i = 0; i < dev->mode_config.num_total_plane; i++) { |
| 1519 | struct drm_plane *plane = state->planes[i]; |
| 1520 | |
| 1521 | if (!plane) |
| 1522 | continue; |
| 1523 | |
| 1524 | plane->state->state = state; |
| 1525 | swap(state->plane_states[i], plane->state); |
| 1526 | plane->state->state = NULL; |
| 1527 | } |
| 1528 | } |
| 1529 | EXPORT_SYMBOL(drm_atomic_helper_swap_state); |
Daniel Vetter | 042652e | 2014-07-27 13:46:52 +0200 | [diff] [blame] | 1530 | |
| 1531 | /** |
| 1532 | * drm_atomic_helper_update_plane - Helper for primary plane update using atomic |
| 1533 | * @plane: plane object to update |
| 1534 | * @crtc: owning CRTC of owning plane |
| 1535 | * @fb: framebuffer to flip onto plane |
| 1536 | * @crtc_x: x offset of primary plane on crtc |
| 1537 | * @crtc_y: y offset of primary plane on crtc |
| 1538 | * @crtc_w: width of primary plane rectangle on crtc |
| 1539 | * @crtc_h: height of primary plane rectangle on crtc |
| 1540 | * @src_x: x offset of @fb for panning |
| 1541 | * @src_y: y offset of @fb for panning |
| 1542 | * @src_w: width of source rectangle in @fb |
| 1543 | * @src_h: height of source rectangle in @fb |
| 1544 | * |
| 1545 | * Provides a default plane update handler using the atomic driver interface. |
| 1546 | * |
| 1547 | * RETURNS: |
| 1548 | * Zero on success, error code on failure |
| 1549 | */ |
| 1550 | int drm_atomic_helper_update_plane(struct drm_plane *plane, |
| 1551 | struct drm_crtc *crtc, |
| 1552 | struct drm_framebuffer *fb, |
| 1553 | int crtc_x, int crtc_y, |
| 1554 | unsigned int crtc_w, unsigned int crtc_h, |
| 1555 | uint32_t src_x, uint32_t src_y, |
| 1556 | uint32_t src_w, uint32_t src_h) |
| 1557 | { |
| 1558 | struct drm_atomic_state *state; |
| 1559 | struct drm_plane_state *plane_state; |
| 1560 | int ret = 0; |
| 1561 | |
| 1562 | state = drm_atomic_state_alloc(plane->dev); |
| 1563 | if (!state) |
| 1564 | return -ENOMEM; |
| 1565 | |
| 1566 | state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc); |
| 1567 | retry: |
| 1568 | plane_state = drm_atomic_get_plane_state(state, plane); |
| 1569 | if (IS_ERR(plane_state)) { |
| 1570 | ret = PTR_ERR(plane_state); |
| 1571 | goto fail; |
| 1572 | } |
| 1573 | |
Daniel Vetter | 07cc0ef | 2014-11-27 15:49:39 +0100 | [diff] [blame] | 1574 | ret = drm_atomic_set_crtc_for_plane(plane_state, crtc); |
Daniel Vetter | 042652e | 2014-07-27 13:46:52 +0200 | [diff] [blame] | 1575 | if (ret != 0) |
| 1576 | goto fail; |
Daniel Vetter | 321ebf0 | 2014-11-04 22:57:27 +0100 | [diff] [blame] | 1577 | drm_atomic_set_fb_for_plane(plane_state, fb); |
Daniel Vetter | 042652e | 2014-07-27 13:46:52 +0200 | [diff] [blame] | 1578 | plane_state->crtc_x = crtc_x; |
| 1579 | plane_state->crtc_y = crtc_y; |
Daniel Vetter | 042652e | 2014-07-27 13:46:52 +0200 | [diff] [blame] | 1580 | plane_state->crtc_w = crtc_w; |
Ville Syrjälä | 02e6f37 | 2015-11-16 17:02:35 +0200 | [diff] [blame] | 1581 | plane_state->crtc_h = crtc_h; |
Daniel Vetter | 042652e | 2014-07-27 13:46:52 +0200 | [diff] [blame] | 1582 | plane_state->src_x = src_x; |
| 1583 | plane_state->src_y = src_y; |
Daniel Vetter | 042652e | 2014-07-27 13:46:52 +0200 | [diff] [blame] | 1584 | plane_state->src_w = src_w; |
Ville Syrjälä | 02e6f37 | 2015-11-16 17:02:35 +0200 | [diff] [blame] | 1585 | plane_state->src_h = src_h; |
Daniel Vetter | 042652e | 2014-07-27 13:46:52 +0200 | [diff] [blame] | 1586 | |
Daniel Vetter | 3671c58 | 2015-05-04 15:40:52 +0200 | [diff] [blame] | 1587 | if (plane == crtc->cursor) |
| 1588 | state->legacy_cursor_update = true; |
| 1589 | |
Daniel Vetter | 042652e | 2014-07-27 13:46:52 +0200 | [diff] [blame] | 1590 | ret = drm_atomic_commit(state); |
| 1591 | if (ret != 0) |
| 1592 | goto fail; |
| 1593 | |
| 1594 | /* Driver takes ownership of state on successful commit. */ |
| 1595 | return 0; |
| 1596 | fail: |
| 1597 | if (ret == -EDEADLK) |
| 1598 | goto backoff; |
| 1599 | |
| 1600 | drm_atomic_state_free(state); |
| 1601 | |
| 1602 | return ret; |
| 1603 | backoff: |
Daniel Vetter | 042652e | 2014-07-27 13:46:52 +0200 | [diff] [blame] | 1604 | drm_atomic_state_clear(state); |
Daniel Vetter | 6f75cea | 2014-11-19 18:38:07 +0100 | [diff] [blame] | 1605 | drm_atomic_legacy_backoff(state); |
Daniel Vetter | 042652e | 2014-07-27 13:46:52 +0200 | [diff] [blame] | 1606 | |
| 1607 | /* |
| 1608 | * Someone might have exchanged the framebuffer while we dropped locks |
| 1609 | * in the backoff code. We need to fix up the fb refcount tracking the |
| 1610 | * core does for us. |
| 1611 | */ |
| 1612 | plane->old_fb = plane->fb; |
| 1613 | |
| 1614 | goto retry; |
| 1615 | } |
| 1616 | EXPORT_SYMBOL(drm_atomic_helper_update_plane); |
| 1617 | |
| 1618 | /** |
| 1619 | * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic |
| 1620 | * @plane: plane to disable |
| 1621 | * |
| 1622 | * Provides a default plane disable handler using the atomic driver interface. |
| 1623 | * |
| 1624 | * RETURNS: |
| 1625 | * Zero on success, error code on failure |
| 1626 | */ |
| 1627 | int drm_atomic_helper_disable_plane(struct drm_plane *plane) |
| 1628 | { |
| 1629 | struct drm_atomic_state *state; |
| 1630 | struct drm_plane_state *plane_state; |
| 1631 | int ret = 0; |
| 1632 | |
Jasper St. Pierre | aa54e2e | 2014-11-20 19:59:15 -0800 | [diff] [blame] | 1633 | /* |
| 1634 | * FIXME: Without plane->crtc set we can't get at the implicit legacy |
| 1635 | * acquire context. The real fix will be to wire the acquire ctx through |
| 1636 | * everywhere we need it, but meanwhile prevent chaos by just skipping |
| 1637 | * this noop. The critical case is the cursor ioctls which a) only grab |
| 1638 | * crtc/cursor-plane locks (so we need the crtc to get at the right |
| 1639 | * acquire context) and b) can try to disable the plane multiple times. |
| 1640 | */ |
| 1641 | if (!plane->crtc) |
| 1642 | return 0; |
| 1643 | |
Daniel Vetter | 042652e | 2014-07-27 13:46:52 +0200 | [diff] [blame] | 1644 | state = drm_atomic_state_alloc(plane->dev); |
| 1645 | if (!state) |
| 1646 | return -ENOMEM; |
| 1647 | |
| 1648 | state->acquire_ctx = drm_modeset_legacy_acquire_ctx(plane->crtc); |
| 1649 | retry: |
| 1650 | plane_state = drm_atomic_get_plane_state(state, plane); |
| 1651 | if (IS_ERR(plane_state)) { |
| 1652 | ret = PTR_ERR(plane_state); |
| 1653 | goto fail; |
| 1654 | } |
| 1655 | |
Maarten Lankhorst | 24e79d0 | 2015-11-11 11:29:07 +0100 | [diff] [blame] | 1656 | if (plane_state->crtc && (plane == plane->crtc->cursor)) |
| 1657 | plane_state->state->legacy_cursor_update = true; |
| 1658 | |
Rob Clark | bbb1e52 | 2015-08-25 15:35:58 -0400 | [diff] [blame] | 1659 | ret = __drm_atomic_helper_disable_plane(plane, plane_state); |
Daniel Vetter | 042652e | 2014-07-27 13:46:52 +0200 | [diff] [blame] | 1660 | if (ret != 0) |
| 1661 | goto fail; |
Daniel Vetter | f02ad90 | 2015-01-22 16:36:23 +0100 | [diff] [blame] | 1662 | |
Daniel Vetter | 042652e | 2014-07-27 13:46:52 +0200 | [diff] [blame] | 1663 | ret = drm_atomic_commit(state); |
| 1664 | if (ret != 0) |
| 1665 | goto fail; |
| 1666 | |
| 1667 | /* Driver takes ownership of state on successful commit. */ |
| 1668 | return 0; |
| 1669 | fail: |
| 1670 | if (ret == -EDEADLK) |
| 1671 | goto backoff; |
| 1672 | |
| 1673 | drm_atomic_state_free(state); |
| 1674 | |
| 1675 | return ret; |
| 1676 | backoff: |
Daniel Vetter | 042652e | 2014-07-27 13:46:52 +0200 | [diff] [blame] | 1677 | drm_atomic_state_clear(state); |
Daniel Vetter | 6f75cea | 2014-11-19 18:38:07 +0100 | [diff] [blame] | 1678 | drm_atomic_legacy_backoff(state); |
Daniel Vetter | 042652e | 2014-07-27 13:46:52 +0200 | [diff] [blame] | 1679 | |
| 1680 | /* |
| 1681 | * Someone might have exchanged the framebuffer while we dropped locks |
| 1682 | * in the backoff code. We need to fix up the fb refcount tracking the |
| 1683 | * core does for us. |
| 1684 | */ |
| 1685 | plane->old_fb = plane->fb; |
| 1686 | |
| 1687 | goto retry; |
| 1688 | } |
| 1689 | EXPORT_SYMBOL(drm_atomic_helper_disable_plane); |
| 1690 | |
Rob Clark | bbb1e52 | 2015-08-25 15:35:58 -0400 | [diff] [blame] | 1691 | /* just used from fb-helper and atomic-helper: */ |
| 1692 | int __drm_atomic_helper_disable_plane(struct drm_plane *plane, |
| 1693 | struct drm_plane_state *plane_state) |
| 1694 | { |
| 1695 | int ret; |
| 1696 | |
| 1697 | ret = drm_atomic_set_crtc_for_plane(plane_state, NULL); |
| 1698 | if (ret != 0) |
| 1699 | return ret; |
| 1700 | |
| 1701 | drm_atomic_set_fb_for_plane(plane_state, NULL); |
| 1702 | plane_state->crtc_x = 0; |
| 1703 | plane_state->crtc_y = 0; |
Rob Clark | bbb1e52 | 2015-08-25 15:35:58 -0400 | [diff] [blame] | 1704 | plane_state->crtc_w = 0; |
Ville Syrjälä | 02e6f37 | 2015-11-16 17:02:35 +0200 | [diff] [blame] | 1705 | plane_state->crtc_h = 0; |
Rob Clark | bbb1e52 | 2015-08-25 15:35:58 -0400 | [diff] [blame] | 1706 | plane_state->src_x = 0; |
| 1707 | plane_state->src_y = 0; |
Rob Clark | bbb1e52 | 2015-08-25 15:35:58 -0400 | [diff] [blame] | 1708 | plane_state->src_w = 0; |
Ville Syrjälä | 02e6f37 | 2015-11-16 17:02:35 +0200 | [diff] [blame] | 1709 | plane_state->src_h = 0; |
Rob Clark | bbb1e52 | 2015-08-25 15:35:58 -0400 | [diff] [blame] | 1710 | |
Rob Clark | bbb1e52 | 2015-08-25 15:35:58 -0400 | [diff] [blame] | 1711 | return 0; |
| 1712 | } |
| 1713 | |
Daniel Vetter | 042652e | 2014-07-27 13:46:52 +0200 | [diff] [blame] | 1714 | static int update_output_state(struct drm_atomic_state *state, |
| 1715 | struct drm_mode_set *set) |
| 1716 | { |
| 1717 | struct drm_device *dev = set->crtc->dev; |
Ander Conselvan de Oliveira | df63b99 | 2015-04-10 14:58:39 +0300 | [diff] [blame] | 1718 | struct drm_crtc *crtc; |
| 1719 | struct drm_crtc_state *crtc_state; |
| 1720 | struct drm_connector *connector; |
Daniel Vetter | 042652e | 2014-07-27 13:46:52 +0200 | [diff] [blame] | 1721 | struct drm_connector_state *conn_state; |
Daniel Vetter | 042652e | 2014-07-27 13:46:52 +0200 | [diff] [blame] | 1722 | int ret, i, j; |
| 1723 | |
| 1724 | ret = drm_modeset_lock(&dev->mode_config.connection_mutex, |
| 1725 | state->acquire_ctx); |
| 1726 | if (ret) |
| 1727 | return ret; |
| 1728 | |
| 1729 | /* First grab all affected connector/crtc states. */ |
| 1730 | for (i = 0; i < set->num_connectors; i++) { |
| 1731 | conn_state = drm_atomic_get_connector_state(state, |
| 1732 | set->connectors[i]); |
| 1733 | if (IS_ERR(conn_state)) |
| 1734 | return PTR_ERR(conn_state); |
| 1735 | } |
| 1736 | |
Ander Conselvan de Oliveira | df63b99 | 2015-04-10 14:58:39 +0300 | [diff] [blame] | 1737 | for_each_crtc_in_state(state, crtc, crtc_state, i) { |
Daniel Vetter | 042652e | 2014-07-27 13:46:52 +0200 | [diff] [blame] | 1738 | ret = drm_atomic_add_affected_connectors(state, crtc); |
| 1739 | if (ret) |
| 1740 | return ret; |
| 1741 | } |
| 1742 | |
| 1743 | /* Then recompute connector->crtc links and crtc enabling state. */ |
Ander Conselvan de Oliveira | df63b99 | 2015-04-10 14:58:39 +0300 | [diff] [blame] | 1744 | for_each_connector_in_state(state, connector, conn_state, i) { |
Daniel Vetter | 042652e | 2014-07-27 13:46:52 +0200 | [diff] [blame] | 1745 | if (conn_state->crtc == set->crtc) { |
| 1746 | ret = drm_atomic_set_crtc_for_connector(conn_state, |
| 1747 | NULL); |
| 1748 | if (ret) |
| 1749 | return ret; |
| 1750 | } |
| 1751 | |
| 1752 | for (j = 0; j < set->num_connectors; j++) { |
| 1753 | if (set->connectors[j] == connector) { |
| 1754 | ret = drm_atomic_set_crtc_for_connector(conn_state, |
| 1755 | set->crtc); |
| 1756 | if (ret) |
| 1757 | return ret; |
| 1758 | break; |
| 1759 | } |
| 1760 | } |
| 1761 | } |
| 1762 | |
Ander Conselvan de Oliveira | df63b99 | 2015-04-10 14:58:39 +0300 | [diff] [blame] | 1763 | for_each_crtc_in_state(state, crtc, crtc_state, i) { |
Daniel Vetter | 042652e | 2014-07-27 13:46:52 +0200 | [diff] [blame] | 1764 | /* Don't update ->enable for the CRTC in the set_config request, |
| 1765 | * since a mismatch would indicate a bug in the upper layers. |
| 1766 | * The actual modeset code later on will catch any |
| 1767 | * inconsistencies here. */ |
| 1768 | if (crtc == set->crtc) |
| 1769 | continue; |
| 1770 | |
Maarten Lankhorst | 14de6c4 | 2016-01-04 12:53:20 +0100 | [diff] [blame] | 1771 | if (!crtc_state->connector_mask) { |
Laurent Pinchart | c30f55a | 2015-06-22 13:37:46 +0300 | [diff] [blame] | 1772 | ret = drm_atomic_set_mode_prop_for_crtc(crtc_state, |
| 1773 | NULL); |
| 1774 | if (ret < 0) |
| 1775 | return ret; |
| 1776 | |
Maarten Lankhorst | 9b5edbf | 2015-06-01 08:59:53 +0200 | [diff] [blame] | 1777 | crtc_state->active = false; |
Laurent Pinchart | c30f55a | 2015-06-22 13:37:46 +0300 | [diff] [blame] | 1778 | } |
Daniel Vetter | 042652e | 2014-07-27 13:46:52 +0200 | [diff] [blame] | 1779 | } |
| 1780 | |
| 1781 | return 0; |
| 1782 | } |
| 1783 | |
| 1784 | /** |
| 1785 | * drm_atomic_helper_set_config - set a new config from userspace |
| 1786 | * @set: mode set configuration |
| 1787 | * |
| 1788 | * Provides a default crtc set_config handler using the atomic driver interface. |
| 1789 | * |
| 1790 | * Returns: |
| 1791 | * Returns 0 on success, negative errno numbers on failure. |
| 1792 | */ |
| 1793 | int drm_atomic_helper_set_config(struct drm_mode_set *set) |
| 1794 | { |
| 1795 | struct drm_atomic_state *state; |
| 1796 | struct drm_crtc *crtc = set->crtc; |
Daniel Vetter | 042652e | 2014-07-27 13:46:52 +0200 | [diff] [blame] | 1797 | int ret = 0; |
| 1798 | |
| 1799 | state = drm_atomic_state_alloc(crtc->dev); |
| 1800 | if (!state) |
| 1801 | return -ENOMEM; |
| 1802 | |
| 1803 | state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc); |
| 1804 | retry: |
Rob Clark | bbb1e52 | 2015-08-25 15:35:58 -0400 | [diff] [blame] | 1805 | ret = __drm_atomic_helper_set_config(set, state); |
Daniel Stone | 819364d | 2015-05-26 14:36:48 +0100 | [diff] [blame] | 1806 | if (ret != 0) |
| 1807 | goto fail; |
| 1808 | |
Daniel Vetter | 042652e | 2014-07-27 13:46:52 +0200 | [diff] [blame] | 1809 | ret = drm_atomic_commit(state); |
| 1810 | if (ret != 0) |
| 1811 | goto fail; |
| 1812 | |
| 1813 | /* Driver takes ownership of state on successful commit. */ |
| 1814 | return 0; |
| 1815 | fail: |
| 1816 | if (ret == -EDEADLK) |
| 1817 | goto backoff; |
| 1818 | |
| 1819 | drm_atomic_state_free(state); |
| 1820 | |
| 1821 | return ret; |
| 1822 | backoff: |
Daniel Vetter | 042652e | 2014-07-27 13:46:52 +0200 | [diff] [blame] | 1823 | drm_atomic_state_clear(state); |
Daniel Vetter | 6f75cea | 2014-11-19 18:38:07 +0100 | [diff] [blame] | 1824 | drm_atomic_legacy_backoff(state); |
Daniel Vetter | 042652e | 2014-07-27 13:46:52 +0200 | [diff] [blame] | 1825 | |
| 1826 | /* |
| 1827 | * Someone might have exchanged the framebuffer while we dropped locks |
| 1828 | * in the backoff code. We need to fix up the fb refcount tracking the |
| 1829 | * core does for us. |
| 1830 | */ |
| 1831 | crtc->primary->old_fb = crtc->primary->fb; |
| 1832 | |
| 1833 | goto retry; |
| 1834 | } |
| 1835 | EXPORT_SYMBOL(drm_atomic_helper_set_config); |
| 1836 | |
Rob Clark | bbb1e52 | 2015-08-25 15:35:58 -0400 | [diff] [blame] | 1837 | /* just used from fb-helper and atomic-helper: */ |
| 1838 | int __drm_atomic_helper_set_config(struct drm_mode_set *set, |
| 1839 | struct drm_atomic_state *state) |
| 1840 | { |
| 1841 | struct drm_crtc_state *crtc_state; |
| 1842 | struct drm_plane_state *primary_state; |
| 1843 | struct drm_crtc *crtc = set->crtc; |
Ville Syrjälä | 8392611 | 2015-11-16 17:02:34 +0200 | [diff] [blame] | 1844 | int hdisplay, vdisplay; |
Rob Clark | bbb1e52 | 2015-08-25 15:35:58 -0400 | [diff] [blame] | 1845 | int ret; |
| 1846 | |
| 1847 | crtc_state = drm_atomic_get_crtc_state(state, crtc); |
| 1848 | if (IS_ERR(crtc_state)) |
| 1849 | return PTR_ERR(crtc_state); |
| 1850 | |
| 1851 | primary_state = drm_atomic_get_plane_state(state, crtc->primary); |
| 1852 | if (IS_ERR(primary_state)) |
| 1853 | return PTR_ERR(primary_state); |
| 1854 | |
| 1855 | if (!set->mode) { |
| 1856 | WARN_ON(set->fb); |
| 1857 | WARN_ON(set->num_connectors); |
| 1858 | |
| 1859 | ret = drm_atomic_set_mode_for_crtc(crtc_state, NULL); |
| 1860 | if (ret != 0) |
| 1861 | return ret; |
| 1862 | |
| 1863 | crtc_state->active = false; |
| 1864 | |
| 1865 | ret = drm_atomic_set_crtc_for_plane(primary_state, NULL); |
| 1866 | if (ret != 0) |
| 1867 | return ret; |
| 1868 | |
| 1869 | drm_atomic_set_fb_for_plane(primary_state, NULL); |
| 1870 | |
| 1871 | goto commit; |
| 1872 | } |
| 1873 | |
| 1874 | WARN_ON(!set->fb); |
| 1875 | WARN_ON(!set->num_connectors); |
| 1876 | |
| 1877 | ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode); |
| 1878 | if (ret != 0) |
| 1879 | return ret; |
| 1880 | |
| 1881 | crtc_state->active = true; |
| 1882 | |
| 1883 | ret = drm_atomic_set_crtc_for_plane(primary_state, crtc); |
| 1884 | if (ret != 0) |
| 1885 | return ret; |
| 1886 | |
Ville Syrjälä | 8392611 | 2015-11-16 17:02:34 +0200 | [diff] [blame] | 1887 | drm_crtc_get_hv_timing(set->mode, &hdisplay, &vdisplay); |
| 1888 | |
Rob Clark | bbb1e52 | 2015-08-25 15:35:58 -0400 | [diff] [blame] | 1889 | drm_atomic_set_fb_for_plane(primary_state, set->fb); |
| 1890 | primary_state->crtc_x = 0; |
| 1891 | primary_state->crtc_y = 0; |
Ville Syrjälä | 8392611 | 2015-11-16 17:02:34 +0200 | [diff] [blame] | 1892 | primary_state->crtc_w = hdisplay; |
Ville Syrjälä | 02e6f37 | 2015-11-16 17:02:35 +0200 | [diff] [blame] | 1893 | primary_state->crtc_h = vdisplay; |
Rob Clark | bbb1e52 | 2015-08-25 15:35:58 -0400 | [diff] [blame] | 1894 | primary_state->src_x = set->x << 16; |
| 1895 | primary_state->src_y = set->y << 16; |
Ville Syrjälä | 4112124 | 2015-10-15 20:39:59 +0300 | [diff] [blame] | 1896 | if (primary_state->rotation & (BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270))) { |
Ville Syrjälä | 8392611 | 2015-11-16 17:02:34 +0200 | [diff] [blame] | 1897 | primary_state->src_w = vdisplay << 16; |
Ville Syrjälä | 02e6f37 | 2015-11-16 17:02:35 +0200 | [diff] [blame] | 1898 | primary_state->src_h = hdisplay << 16; |
Ville Syrjälä | 4112124 | 2015-10-15 20:39:59 +0300 | [diff] [blame] | 1899 | } else { |
Ville Syrjälä | 8392611 | 2015-11-16 17:02:34 +0200 | [diff] [blame] | 1900 | primary_state->src_w = hdisplay << 16; |
Ville Syrjälä | 02e6f37 | 2015-11-16 17:02:35 +0200 | [diff] [blame] | 1901 | primary_state->src_h = vdisplay << 16; |
Ville Syrjälä | 4112124 | 2015-10-15 20:39:59 +0300 | [diff] [blame] | 1902 | } |
Rob Clark | bbb1e52 | 2015-08-25 15:35:58 -0400 | [diff] [blame] | 1903 | |
| 1904 | commit: |
| 1905 | ret = update_output_state(state, set); |
| 1906 | if (ret) |
| 1907 | return ret; |
| 1908 | |
| 1909 | return 0; |
| 1910 | } |
| 1911 | |
Daniel Vetter | 042652e | 2014-07-27 13:46:52 +0200 | [diff] [blame] | 1912 | /** |
Thierry Reding | 1494276 | 2015-12-02 17:50:04 +0100 | [diff] [blame] | 1913 | * drm_atomic_helper_disable_all - disable all currently active outputs |
| 1914 | * @dev: DRM device |
| 1915 | * @ctx: lock acquisition context |
| 1916 | * |
| 1917 | * Loops through all connectors, finding those that aren't turned off and then |
| 1918 | * turns them off by setting their DPMS mode to OFF and deactivating the CRTC |
| 1919 | * that they are connected to. |
| 1920 | * |
| 1921 | * This is used for example in suspend/resume to disable all currently active |
| 1922 | * functions when suspending. |
| 1923 | * |
| 1924 | * Note that if callers haven't already acquired all modeset locks this might |
| 1925 | * return -EDEADLK, which must be handled by calling drm_modeset_backoff(). |
| 1926 | * |
| 1927 | * Returns: |
| 1928 | * 0 on success or a negative error code on failure. |
| 1929 | * |
| 1930 | * See also: |
| 1931 | * drm_atomic_helper_suspend(), drm_atomic_helper_resume() |
| 1932 | */ |
| 1933 | int drm_atomic_helper_disable_all(struct drm_device *dev, |
| 1934 | struct drm_modeset_acquire_ctx *ctx) |
| 1935 | { |
| 1936 | struct drm_atomic_state *state; |
| 1937 | struct drm_connector *conn; |
| 1938 | int err; |
| 1939 | |
| 1940 | state = drm_atomic_state_alloc(dev); |
| 1941 | if (!state) |
| 1942 | return -ENOMEM; |
| 1943 | |
| 1944 | state->acquire_ctx = ctx; |
| 1945 | |
| 1946 | drm_for_each_connector(conn, dev) { |
| 1947 | struct drm_crtc *crtc = conn->state->crtc; |
| 1948 | struct drm_crtc_state *crtc_state; |
| 1949 | |
| 1950 | if (!crtc || conn->dpms != DRM_MODE_DPMS_ON) |
| 1951 | continue; |
| 1952 | |
| 1953 | crtc_state = drm_atomic_get_crtc_state(state, crtc); |
| 1954 | if (IS_ERR(crtc_state)) { |
| 1955 | err = PTR_ERR(crtc_state); |
| 1956 | goto free; |
| 1957 | } |
| 1958 | |
| 1959 | crtc_state->active = false; |
| 1960 | } |
| 1961 | |
| 1962 | err = drm_atomic_commit(state); |
| 1963 | |
| 1964 | free: |
| 1965 | if (err < 0) |
| 1966 | drm_atomic_state_free(state); |
| 1967 | |
| 1968 | return err; |
| 1969 | } |
| 1970 | EXPORT_SYMBOL(drm_atomic_helper_disable_all); |
| 1971 | |
| 1972 | /** |
| 1973 | * drm_atomic_helper_suspend - subsystem-level suspend helper |
| 1974 | * @dev: DRM device |
| 1975 | * |
| 1976 | * Duplicates the current atomic state, disables all active outputs and then |
| 1977 | * returns a pointer to the original atomic state to the caller. Drivers can |
| 1978 | * pass this pointer to the drm_atomic_helper_resume() helper upon resume to |
| 1979 | * restore the output configuration that was active at the time the system |
| 1980 | * entered suspend. |
| 1981 | * |
| 1982 | * Note that it is potentially unsafe to use this. The atomic state object |
| 1983 | * returned by this function is assumed to be persistent. Drivers must ensure |
| 1984 | * that this holds true. Before calling this function, drivers must make sure |
| 1985 | * to suspend fbdev emulation so that nothing can be using the device. |
| 1986 | * |
| 1987 | * Returns: |
| 1988 | * A pointer to a copy of the state before suspend on success or an ERR_PTR()- |
| 1989 | * encoded error code on failure. Drivers should store the returned atomic |
| 1990 | * state object and pass it to the drm_atomic_helper_resume() helper upon |
| 1991 | * resume. |
| 1992 | * |
| 1993 | * See also: |
| 1994 | * drm_atomic_helper_duplicate_state(), drm_atomic_helper_disable_all(), |
| 1995 | * drm_atomic_helper_resume() |
| 1996 | */ |
| 1997 | struct drm_atomic_state *drm_atomic_helper_suspend(struct drm_device *dev) |
| 1998 | { |
| 1999 | struct drm_modeset_acquire_ctx ctx; |
| 2000 | struct drm_atomic_state *state; |
| 2001 | int err; |
| 2002 | |
| 2003 | drm_modeset_acquire_init(&ctx, 0); |
| 2004 | |
| 2005 | retry: |
| 2006 | err = drm_modeset_lock_all_ctx(dev, &ctx); |
| 2007 | if (err < 0) { |
| 2008 | state = ERR_PTR(err); |
| 2009 | goto unlock; |
| 2010 | } |
| 2011 | |
| 2012 | state = drm_atomic_helper_duplicate_state(dev, &ctx); |
| 2013 | if (IS_ERR(state)) |
| 2014 | goto unlock; |
| 2015 | |
| 2016 | err = drm_atomic_helper_disable_all(dev, &ctx); |
| 2017 | if (err < 0) { |
| 2018 | drm_atomic_state_free(state); |
| 2019 | state = ERR_PTR(err); |
| 2020 | goto unlock; |
| 2021 | } |
| 2022 | |
| 2023 | unlock: |
| 2024 | if (PTR_ERR(state) == -EDEADLK) { |
| 2025 | drm_modeset_backoff(&ctx); |
| 2026 | goto retry; |
| 2027 | } |
| 2028 | |
| 2029 | drm_modeset_drop_locks(&ctx); |
| 2030 | drm_modeset_acquire_fini(&ctx); |
| 2031 | return state; |
| 2032 | } |
| 2033 | EXPORT_SYMBOL(drm_atomic_helper_suspend); |
| 2034 | |
| 2035 | /** |
| 2036 | * drm_atomic_helper_resume - subsystem-level resume helper |
| 2037 | * @dev: DRM device |
| 2038 | * @state: atomic state to resume to |
| 2039 | * |
| 2040 | * Calls drm_mode_config_reset() to synchronize hardware and software states, |
| 2041 | * grabs all modeset locks and commits the atomic state object. This can be |
| 2042 | * used in conjunction with the drm_atomic_helper_suspend() helper to |
| 2043 | * implement suspend/resume for drivers that support atomic mode-setting. |
| 2044 | * |
| 2045 | * Returns: |
| 2046 | * 0 on success or a negative error code on failure. |
| 2047 | * |
| 2048 | * See also: |
| 2049 | * drm_atomic_helper_suspend() |
| 2050 | */ |
| 2051 | int drm_atomic_helper_resume(struct drm_device *dev, |
| 2052 | struct drm_atomic_state *state) |
| 2053 | { |
| 2054 | struct drm_mode_config *config = &dev->mode_config; |
| 2055 | int err; |
| 2056 | |
| 2057 | drm_mode_config_reset(dev); |
| 2058 | drm_modeset_lock_all(dev); |
| 2059 | state->acquire_ctx = config->acquire_ctx; |
| 2060 | err = drm_atomic_commit(state); |
| 2061 | drm_modeset_unlock_all(dev); |
| 2062 | |
| 2063 | return err; |
| 2064 | } |
| 2065 | EXPORT_SYMBOL(drm_atomic_helper_resume); |
| 2066 | |
| 2067 | /** |
Laurent Pinchart | 7f50002 | 2015-02-23 02:50:23 +0200 | [diff] [blame] | 2068 | * drm_atomic_helper_crtc_set_property - helper for crtc properties |
Daniel Vetter | 042652e | 2014-07-27 13:46:52 +0200 | [diff] [blame] | 2069 | * @crtc: DRM crtc |
| 2070 | * @property: DRM property |
| 2071 | * @val: value of property |
| 2072 | * |
Laurent Pinchart | 7f50002 | 2015-02-23 02:50:23 +0200 | [diff] [blame] | 2073 | * Provides a default crtc set_property handler using the atomic driver |
| 2074 | * interface. |
Daniel Vetter | 042652e | 2014-07-27 13:46:52 +0200 | [diff] [blame] | 2075 | * |
| 2076 | * RETURNS: |
| 2077 | * Zero on success, error code on failure |
| 2078 | */ |
| 2079 | int |
| 2080 | drm_atomic_helper_crtc_set_property(struct drm_crtc *crtc, |
| 2081 | struct drm_property *property, |
| 2082 | uint64_t val) |
| 2083 | { |
| 2084 | struct drm_atomic_state *state; |
| 2085 | struct drm_crtc_state *crtc_state; |
| 2086 | int ret = 0; |
| 2087 | |
| 2088 | state = drm_atomic_state_alloc(crtc->dev); |
| 2089 | if (!state) |
| 2090 | return -ENOMEM; |
| 2091 | |
| 2092 | /* ->set_property is always called with all locks held. */ |
| 2093 | state->acquire_ctx = crtc->dev->mode_config.acquire_ctx; |
| 2094 | retry: |
| 2095 | crtc_state = drm_atomic_get_crtc_state(state, crtc); |
| 2096 | if (IS_ERR(crtc_state)) { |
| 2097 | ret = PTR_ERR(crtc_state); |
| 2098 | goto fail; |
| 2099 | } |
| 2100 | |
Rob Clark | 40ecc69 | 2014-12-18 16:01:46 -0500 | [diff] [blame] | 2101 | ret = drm_atomic_crtc_set_property(crtc, crtc_state, |
| 2102 | property, val); |
Daniel Vetter | 042652e | 2014-07-27 13:46:52 +0200 | [diff] [blame] | 2103 | if (ret) |
| 2104 | goto fail; |
| 2105 | |
| 2106 | ret = drm_atomic_commit(state); |
| 2107 | if (ret != 0) |
| 2108 | goto fail; |
| 2109 | |
| 2110 | /* Driver takes ownership of state on successful commit. */ |
| 2111 | return 0; |
| 2112 | fail: |
| 2113 | if (ret == -EDEADLK) |
| 2114 | goto backoff; |
| 2115 | |
| 2116 | drm_atomic_state_free(state); |
| 2117 | |
| 2118 | return ret; |
| 2119 | backoff: |
Daniel Vetter | 042652e | 2014-07-27 13:46:52 +0200 | [diff] [blame] | 2120 | drm_atomic_state_clear(state); |
Daniel Vetter | 6f75cea | 2014-11-19 18:38:07 +0100 | [diff] [blame] | 2121 | drm_atomic_legacy_backoff(state); |
Daniel Vetter | 042652e | 2014-07-27 13:46:52 +0200 | [diff] [blame] | 2122 | |
| 2123 | goto retry; |
| 2124 | } |
| 2125 | EXPORT_SYMBOL(drm_atomic_helper_crtc_set_property); |
| 2126 | |
| 2127 | /** |
Laurent Pinchart | 7f50002 | 2015-02-23 02:50:23 +0200 | [diff] [blame] | 2128 | * drm_atomic_helper_plane_set_property - helper for plane properties |
Daniel Vetter | 042652e | 2014-07-27 13:46:52 +0200 | [diff] [blame] | 2129 | * @plane: DRM plane |
| 2130 | * @property: DRM property |
| 2131 | * @val: value of property |
| 2132 | * |
Laurent Pinchart | 7f50002 | 2015-02-23 02:50:23 +0200 | [diff] [blame] | 2133 | * Provides a default plane set_property handler using the atomic driver |
| 2134 | * interface. |
Daniel Vetter | 042652e | 2014-07-27 13:46:52 +0200 | [diff] [blame] | 2135 | * |
| 2136 | * RETURNS: |
| 2137 | * Zero on success, error code on failure |
| 2138 | */ |
| 2139 | int |
| 2140 | drm_atomic_helper_plane_set_property(struct drm_plane *plane, |
| 2141 | struct drm_property *property, |
| 2142 | uint64_t val) |
| 2143 | { |
| 2144 | struct drm_atomic_state *state; |
| 2145 | struct drm_plane_state *plane_state; |
| 2146 | int ret = 0; |
| 2147 | |
| 2148 | state = drm_atomic_state_alloc(plane->dev); |
| 2149 | if (!state) |
| 2150 | return -ENOMEM; |
| 2151 | |
| 2152 | /* ->set_property is always called with all locks held. */ |
| 2153 | state->acquire_ctx = plane->dev->mode_config.acquire_ctx; |
| 2154 | retry: |
| 2155 | plane_state = drm_atomic_get_plane_state(state, plane); |
| 2156 | if (IS_ERR(plane_state)) { |
| 2157 | ret = PTR_ERR(plane_state); |
| 2158 | goto fail; |
| 2159 | } |
| 2160 | |
Rob Clark | 40ecc69 | 2014-12-18 16:01:46 -0500 | [diff] [blame] | 2161 | ret = drm_atomic_plane_set_property(plane, plane_state, |
| 2162 | property, val); |
Daniel Vetter | 042652e | 2014-07-27 13:46:52 +0200 | [diff] [blame] | 2163 | if (ret) |
| 2164 | goto fail; |
| 2165 | |
| 2166 | ret = drm_atomic_commit(state); |
| 2167 | if (ret != 0) |
| 2168 | goto fail; |
| 2169 | |
| 2170 | /* Driver takes ownership of state on successful commit. */ |
| 2171 | return 0; |
| 2172 | fail: |
| 2173 | if (ret == -EDEADLK) |
| 2174 | goto backoff; |
| 2175 | |
| 2176 | drm_atomic_state_free(state); |
| 2177 | |
| 2178 | return ret; |
| 2179 | backoff: |
Daniel Vetter | 042652e | 2014-07-27 13:46:52 +0200 | [diff] [blame] | 2180 | drm_atomic_state_clear(state); |
Daniel Vetter | 6f75cea | 2014-11-19 18:38:07 +0100 | [diff] [blame] | 2181 | drm_atomic_legacy_backoff(state); |
Daniel Vetter | 042652e | 2014-07-27 13:46:52 +0200 | [diff] [blame] | 2182 | |
| 2183 | goto retry; |
| 2184 | } |
| 2185 | EXPORT_SYMBOL(drm_atomic_helper_plane_set_property); |
| 2186 | |
| 2187 | /** |
Laurent Pinchart | 7f50002 | 2015-02-23 02:50:23 +0200 | [diff] [blame] | 2188 | * drm_atomic_helper_connector_set_property - helper for connector properties |
Daniel Vetter | 042652e | 2014-07-27 13:46:52 +0200 | [diff] [blame] | 2189 | * @connector: DRM connector |
| 2190 | * @property: DRM property |
| 2191 | * @val: value of property |
| 2192 | * |
Laurent Pinchart | 7f50002 | 2015-02-23 02:50:23 +0200 | [diff] [blame] | 2193 | * Provides a default connector set_property handler using the atomic driver |
| 2194 | * interface. |
Daniel Vetter | 042652e | 2014-07-27 13:46:52 +0200 | [diff] [blame] | 2195 | * |
| 2196 | * RETURNS: |
| 2197 | * Zero on success, error code on failure |
| 2198 | */ |
| 2199 | int |
| 2200 | drm_atomic_helper_connector_set_property(struct drm_connector *connector, |
| 2201 | struct drm_property *property, |
| 2202 | uint64_t val) |
| 2203 | { |
| 2204 | struct drm_atomic_state *state; |
| 2205 | struct drm_connector_state *connector_state; |
| 2206 | int ret = 0; |
| 2207 | |
| 2208 | state = drm_atomic_state_alloc(connector->dev); |
| 2209 | if (!state) |
| 2210 | return -ENOMEM; |
| 2211 | |
| 2212 | /* ->set_property is always called with all locks held. */ |
| 2213 | state->acquire_ctx = connector->dev->mode_config.acquire_ctx; |
| 2214 | retry: |
| 2215 | connector_state = drm_atomic_get_connector_state(state, connector); |
| 2216 | if (IS_ERR(connector_state)) { |
| 2217 | ret = PTR_ERR(connector_state); |
| 2218 | goto fail; |
| 2219 | } |
| 2220 | |
Rob Clark | 40ecc69 | 2014-12-18 16:01:46 -0500 | [diff] [blame] | 2221 | ret = drm_atomic_connector_set_property(connector, connector_state, |
| 2222 | property, val); |
Daniel Vetter | 042652e | 2014-07-27 13:46:52 +0200 | [diff] [blame] | 2223 | if (ret) |
| 2224 | goto fail; |
| 2225 | |
| 2226 | ret = drm_atomic_commit(state); |
| 2227 | if (ret != 0) |
| 2228 | goto fail; |
| 2229 | |
| 2230 | /* Driver takes ownership of state on successful commit. */ |
| 2231 | return 0; |
| 2232 | fail: |
| 2233 | if (ret == -EDEADLK) |
| 2234 | goto backoff; |
| 2235 | |
| 2236 | drm_atomic_state_free(state); |
| 2237 | |
| 2238 | return ret; |
| 2239 | backoff: |
Daniel Vetter | 042652e | 2014-07-27 13:46:52 +0200 | [diff] [blame] | 2240 | drm_atomic_state_clear(state); |
Daniel Vetter | 6f75cea | 2014-11-19 18:38:07 +0100 | [diff] [blame] | 2241 | drm_atomic_legacy_backoff(state); |
Daniel Vetter | 042652e | 2014-07-27 13:46:52 +0200 | [diff] [blame] | 2242 | |
| 2243 | goto retry; |
| 2244 | } |
| 2245 | EXPORT_SYMBOL(drm_atomic_helper_connector_set_property); |
Daniel Vetter | 8bc0f31 | 2014-07-27 18:42:37 +0200 | [diff] [blame] | 2246 | |
| 2247 | /** |
| 2248 | * drm_atomic_helper_page_flip - execute a legacy page flip |
| 2249 | * @crtc: DRM crtc |
| 2250 | * @fb: DRM framebuffer |
| 2251 | * @event: optional DRM event to signal upon completion |
| 2252 | * @flags: flip flags for non-vblank sync'ed updates |
| 2253 | * |
| 2254 | * Provides a default page flip implementation using the atomic driver interface. |
| 2255 | * |
| 2256 | * Note that for now so called async page flips (i.e. updates which are not |
| 2257 | * synchronized to vblank) are not supported, since the atomic interfaces have |
| 2258 | * no provisions for this yet. |
| 2259 | * |
| 2260 | * Returns: |
| 2261 | * Returns 0 on success, negative errno numbers on failure. |
| 2262 | */ |
| 2263 | int drm_atomic_helper_page_flip(struct drm_crtc *crtc, |
| 2264 | struct drm_framebuffer *fb, |
| 2265 | struct drm_pending_vblank_event *event, |
| 2266 | uint32_t flags) |
| 2267 | { |
| 2268 | struct drm_plane *plane = crtc->primary; |
| 2269 | struct drm_atomic_state *state; |
| 2270 | struct drm_plane_state *plane_state; |
| 2271 | struct drm_crtc_state *crtc_state; |
| 2272 | int ret = 0; |
| 2273 | |
| 2274 | if (flags & DRM_MODE_PAGE_FLIP_ASYNC) |
| 2275 | return -EINVAL; |
| 2276 | |
| 2277 | state = drm_atomic_state_alloc(plane->dev); |
| 2278 | if (!state) |
| 2279 | return -ENOMEM; |
| 2280 | |
| 2281 | state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc); |
| 2282 | retry: |
| 2283 | crtc_state = drm_atomic_get_crtc_state(state, crtc); |
| 2284 | if (IS_ERR(crtc_state)) { |
| 2285 | ret = PTR_ERR(crtc_state); |
| 2286 | goto fail; |
| 2287 | } |
| 2288 | crtc_state->event = event; |
| 2289 | |
| 2290 | plane_state = drm_atomic_get_plane_state(state, plane); |
| 2291 | if (IS_ERR(plane_state)) { |
| 2292 | ret = PTR_ERR(plane_state); |
| 2293 | goto fail; |
| 2294 | } |
| 2295 | |
Daniel Vetter | 07cc0ef | 2014-11-27 15:49:39 +0100 | [diff] [blame] | 2296 | ret = drm_atomic_set_crtc_for_plane(plane_state, crtc); |
Daniel Vetter | 8bc0f31 | 2014-07-27 18:42:37 +0200 | [diff] [blame] | 2297 | if (ret != 0) |
| 2298 | goto fail; |
Daniel Vetter | 321ebf0 | 2014-11-04 22:57:27 +0100 | [diff] [blame] | 2299 | drm_atomic_set_fb_for_plane(plane_state, fb); |
Daniel Vetter | 8bc0f31 | 2014-07-27 18:42:37 +0200 | [diff] [blame] | 2300 | |
Daniel Vetter | 4cba685 | 2015-12-08 09:49:20 +0100 | [diff] [blame] | 2301 | /* Make sure we don't accidentally do a full modeset. */ |
| 2302 | state->allow_modeset = false; |
| 2303 | if (!crtc_state->active) { |
| 2304 | DRM_DEBUG_ATOMIC("[CRTC:%d] disabled, rejecting legacy flip\n", |
| 2305 | crtc->base.id); |
| 2306 | ret = -EINVAL; |
| 2307 | goto fail; |
| 2308 | } |
| 2309 | |
Daniel Vetter | 8bc0f31 | 2014-07-27 18:42:37 +0200 | [diff] [blame] | 2310 | ret = drm_atomic_async_commit(state); |
| 2311 | if (ret != 0) |
| 2312 | goto fail; |
| 2313 | |
Daniel Vetter | 8bc0f31 | 2014-07-27 18:42:37 +0200 | [diff] [blame] | 2314 | /* Driver takes ownership of state on successful async commit. */ |
| 2315 | return 0; |
| 2316 | fail: |
| 2317 | if (ret == -EDEADLK) |
| 2318 | goto backoff; |
| 2319 | |
| 2320 | drm_atomic_state_free(state); |
| 2321 | |
| 2322 | return ret; |
| 2323 | backoff: |
Daniel Vetter | 8bc0f31 | 2014-07-27 18:42:37 +0200 | [diff] [blame] | 2324 | drm_atomic_state_clear(state); |
Daniel Vetter | 6f75cea | 2014-11-19 18:38:07 +0100 | [diff] [blame] | 2325 | drm_atomic_legacy_backoff(state); |
Daniel Vetter | 8bc0f31 | 2014-07-27 18:42:37 +0200 | [diff] [blame] | 2326 | |
| 2327 | /* |
| 2328 | * Someone might have exchanged the framebuffer while we dropped locks |
| 2329 | * in the backoff code. We need to fix up the fb refcount tracking the |
| 2330 | * core does for us. |
| 2331 | */ |
| 2332 | plane->old_fb = plane->fb; |
| 2333 | |
| 2334 | goto retry; |
| 2335 | } |
| 2336 | EXPORT_SYMBOL(drm_atomic_helper_page_flip); |
Daniel Vetter | d461701 | 2014-11-03 15:56:43 +0100 | [diff] [blame] | 2337 | |
| 2338 | /** |
Daniel Vetter | b486e0e | 2015-01-22 18:53:05 +0100 | [diff] [blame] | 2339 | * drm_atomic_helper_connector_dpms() - connector dpms helper implementation |
| 2340 | * @connector: affected connector |
| 2341 | * @mode: DPMS mode |
| 2342 | * |
| 2343 | * This is the main helper function provided by the atomic helper framework for |
| 2344 | * implementing the legacy DPMS connector interface. It computes the new desired |
| 2345 | * ->active state for the corresponding CRTC (if the connector is enabled) and |
| 2346 | * updates it. |
Maarten Lankhorst | 9a69a9a | 2015-07-21 11:34:55 +0200 | [diff] [blame] | 2347 | * |
| 2348 | * Returns: |
| 2349 | * Returns 0 on success, negative errno numbers on failure. |
Daniel Vetter | b486e0e | 2015-01-22 18:53:05 +0100 | [diff] [blame] | 2350 | */ |
Maarten Lankhorst | 9a69a9a | 2015-07-21 11:34:55 +0200 | [diff] [blame] | 2351 | int drm_atomic_helper_connector_dpms(struct drm_connector *connector, |
| 2352 | int mode) |
Daniel Vetter | b486e0e | 2015-01-22 18:53:05 +0100 | [diff] [blame] | 2353 | { |
| 2354 | struct drm_mode_config *config = &connector->dev->mode_config; |
| 2355 | struct drm_atomic_state *state; |
| 2356 | struct drm_crtc_state *crtc_state; |
| 2357 | struct drm_crtc *crtc; |
| 2358 | struct drm_connector *tmp_connector; |
| 2359 | int ret; |
| 2360 | bool active = false; |
Maarten Lankhorst | 9a69a9a | 2015-07-21 11:34:55 +0200 | [diff] [blame] | 2361 | int old_mode = connector->dpms; |
Daniel Vetter | b486e0e | 2015-01-22 18:53:05 +0100 | [diff] [blame] | 2362 | |
| 2363 | if (mode != DRM_MODE_DPMS_ON) |
| 2364 | mode = DRM_MODE_DPMS_OFF; |
| 2365 | |
| 2366 | connector->dpms = mode; |
| 2367 | crtc = connector->state->crtc; |
| 2368 | |
| 2369 | if (!crtc) |
Maarten Lankhorst | 9a69a9a | 2015-07-21 11:34:55 +0200 | [diff] [blame] | 2370 | return 0; |
Daniel Vetter | b486e0e | 2015-01-22 18:53:05 +0100 | [diff] [blame] | 2371 | |
Daniel Vetter | b486e0e | 2015-01-22 18:53:05 +0100 | [diff] [blame] | 2372 | state = drm_atomic_state_alloc(connector->dev); |
| 2373 | if (!state) |
Maarten Lankhorst | 9a69a9a | 2015-07-21 11:34:55 +0200 | [diff] [blame] | 2374 | return -ENOMEM; |
Daniel Vetter | b486e0e | 2015-01-22 18:53:05 +0100 | [diff] [blame] | 2375 | |
| 2376 | state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc); |
| 2377 | retry: |
| 2378 | crtc_state = drm_atomic_get_crtc_state(state, crtc); |
Maarten Lankhorst | 9a69a9a | 2015-07-21 11:34:55 +0200 | [diff] [blame] | 2379 | if (IS_ERR(crtc_state)) { |
| 2380 | ret = PTR_ERR(crtc_state); |
| 2381 | goto fail; |
| 2382 | } |
Daniel Vetter | b486e0e | 2015-01-22 18:53:05 +0100 | [diff] [blame] | 2383 | |
| 2384 | WARN_ON(!drm_modeset_is_locked(&config->connection_mutex)); |
| 2385 | |
Daniel Vetter | 9a9f5ce | 2015-07-09 23:44:34 +0200 | [diff] [blame] | 2386 | drm_for_each_connector(tmp_connector, connector->dev) { |
John Hunter | 0388df0 | 2015-03-17 15:30:28 +0800 | [diff] [blame] | 2387 | if (tmp_connector->state->crtc != crtc) |
Daniel Vetter | b486e0e | 2015-01-22 18:53:05 +0100 | [diff] [blame] | 2388 | continue; |
| 2389 | |
John Hunter | 0388df0 | 2015-03-17 15:30:28 +0800 | [diff] [blame] | 2390 | if (tmp_connector->dpms == DRM_MODE_DPMS_ON) { |
Daniel Vetter | b486e0e | 2015-01-22 18:53:05 +0100 | [diff] [blame] | 2391 | active = true; |
| 2392 | break; |
| 2393 | } |
| 2394 | } |
| 2395 | crtc_state->active = active; |
| 2396 | |
| 2397 | ret = drm_atomic_commit(state); |
| 2398 | if (ret != 0) |
| 2399 | goto fail; |
| 2400 | |
Maarten Lankhorst | 9a69a9a | 2015-07-21 11:34:55 +0200 | [diff] [blame] | 2401 | /* Driver takes ownership of state on successful commit. */ |
| 2402 | return 0; |
Daniel Vetter | b486e0e | 2015-01-22 18:53:05 +0100 | [diff] [blame] | 2403 | fail: |
| 2404 | if (ret == -EDEADLK) |
| 2405 | goto backoff; |
| 2406 | |
Maarten Lankhorst | 9a69a9a | 2015-07-21 11:34:55 +0200 | [diff] [blame] | 2407 | connector->dpms = old_mode; |
Daniel Vetter | b486e0e | 2015-01-22 18:53:05 +0100 | [diff] [blame] | 2408 | drm_atomic_state_free(state); |
| 2409 | |
Maarten Lankhorst | 9a69a9a | 2015-07-21 11:34:55 +0200 | [diff] [blame] | 2410 | return ret; |
Daniel Vetter | b486e0e | 2015-01-22 18:53:05 +0100 | [diff] [blame] | 2411 | backoff: |
| 2412 | drm_atomic_state_clear(state); |
| 2413 | drm_atomic_legacy_backoff(state); |
| 2414 | |
| 2415 | goto retry; |
| 2416 | } |
| 2417 | EXPORT_SYMBOL(drm_atomic_helper_connector_dpms); |
| 2418 | |
| 2419 | /** |
Daniel Vetter | 3150c7d | 2014-11-06 20:53:29 +0100 | [diff] [blame] | 2420 | * DOC: atomic state reset and initialization |
| 2421 | * |
| 2422 | * Both the drm core and the atomic helpers assume that there is always the full |
| 2423 | * and correct atomic software state for all connectors, CRTCs and planes |
| 2424 | * available. Which is a bit a problem on driver load and also after system |
| 2425 | * suspend. One way to solve this is to have a hardware state read-out |
| 2426 | * infrastructure which reconstructs the full software state (e.g. the i915 |
| 2427 | * driver). |
| 2428 | * |
| 2429 | * The simpler solution is to just reset the software state to everything off, |
| 2430 | * which is easiest to do by calling drm_mode_config_reset(). To facilitate this |
| 2431 | * the atomic helpers provide default reset implementations for all hooks. |
Daniel Vetter | 7f8ee3e | 2015-12-04 09:46:06 +0100 | [diff] [blame] | 2432 | * |
| 2433 | * On the upside the precise state tracking of atomic simplifies system suspend |
| 2434 | * and resume a lot. For drivers using drm_mode_config_reset() a complete recipe |
| 2435 | * is implemented in drm_atomic_helper_suspend() and drm_atomic_helper_resume(). |
| 2436 | * For other drivers the building blocks are split out, see the documentation |
| 2437 | * for these functions. |
Daniel Vetter | 3150c7d | 2014-11-06 20:53:29 +0100 | [diff] [blame] | 2438 | */ |
| 2439 | |
| 2440 | /** |
Daniel Vetter | d461701 | 2014-11-03 15:56:43 +0100 | [diff] [blame] | 2441 | * drm_atomic_helper_crtc_reset - default ->reset hook for CRTCs |
| 2442 | * @crtc: drm CRTC |
| 2443 | * |
| 2444 | * Resets the atomic state for @crtc by freeing the state pointer (which might |
| 2445 | * be NULL, e.g. at driver load time) and allocating a new empty state object. |
| 2446 | */ |
| 2447 | void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc) |
| 2448 | { |
Markus Elfring | 5f91190 | 2015-11-06 12:03:46 +0100 | [diff] [blame] | 2449 | if (crtc->state) |
Daniel Stone | 99cf4a2 | 2015-05-25 19:11:51 +0100 | [diff] [blame] | 2450 | drm_property_unreference_blob(crtc->state->mode_blob); |
Daniel Vetter | d461701 | 2014-11-03 15:56:43 +0100 | [diff] [blame] | 2451 | kfree(crtc->state); |
| 2452 | crtc->state = kzalloc(sizeof(*crtc->state), GFP_KERNEL); |
Daniel Vetter | 07cc0ef | 2014-11-27 15:49:39 +0100 | [diff] [blame] | 2453 | |
| 2454 | if (crtc->state) |
| 2455 | crtc->state->crtc = crtc; |
Daniel Vetter | d461701 | 2014-11-03 15:56:43 +0100 | [diff] [blame] | 2456 | } |
| 2457 | EXPORT_SYMBOL(drm_atomic_helper_crtc_reset); |
| 2458 | |
| 2459 | /** |
Thierry Reding | f5e7840 | 2015-01-28 14:54:32 +0100 | [diff] [blame] | 2460 | * __drm_atomic_helper_crtc_duplicate_state - copy atomic CRTC state |
| 2461 | * @crtc: CRTC object |
| 2462 | * @state: atomic CRTC state |
| 2463 | * |
| 2464 | * Copies atomic state from a CRTC's current state and resets inferred values. |
| 2465 | * This is useful for drivers that subclass the CRTC state. |
| 2466 | */ |
| 2467 | void __drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc, |
| 2468 | struct drm_crtc_state *state) |
| 2469 | { |
| 2470 | memcpy(state, crtc->state, sizeof(*state)); |
| 2471 | |
Daniel Stone | 99cf4a2 | 2015-05-25 19:11:51 +0100 | [diff] [blame] | 2472 | if (state->mode_blob) |
| 2473 | drm_property_reference_blob(state->mode_blob); |
Thierry Reding | f5e7840 | 2015-01-28 14:54:32 +0100 | [diff] [blame] | 2474 | state->mode_changed = false; |
| 2475 | state->active_changed = false; |
| 2476 | state->planes_changed = false; |
Maarten Lankhorst | fc59666 | 2015-07-21 13:28:57 +0200 | [diff] [blame] | 2477 | state->connectors_changed = false; |
Thierry Reding | f5e7840 | 2015-01-28 14:54:32 +0100 | [diff] [blame] | 2478 | state->event = NULL; |
| 2479 | } |
| 2480 | EXPORT_SYMBOL(__drm_atomic_helper_crtc_duplicate_state); |
| 2481 | |
| 2482 | /** |
Daniel Vetter | d461701 | 2014-11-03 15:56:43 +0100 | [diff] [blame] | 2483 | * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook |
| 2484 | * @crtc: drm CRTC |
| 2485 | * |
| 2486 | * Default CRTC state duplicate hook for drivers which don't have their own |
| 2487 | * subclassed CRTC state structure. |
| 2488 | */ |
| 2489 | struct drm_crtc_state * |
| 2490 | drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc) |
| 2491 | { |
| 2492 | struct drm_crtc_state *state; |
| 2493 | |
| 2494 | if (WARN_ON(!crtc->state)) |
| 2495 | return NULL; |
| 2496 | |
Thierry Reding | f5e7840 | 2015-01-28 14:54:32 +0100 | [diff] [blame] | 2497 | state = kmalloc(sizeof(*state), GFP_KERNEL); |
| 2498 | if (state) |
| 2499 | __drm_atomic_helper_crtc_duplicate_state(crtc, state); |
Daniel Vetter | d461701 | 2014-11-03 15:56:43 +0100 | [diff] [blame] | 2500 | |
| 2501 | return state; |
| 2502 | } |
| 2503 | EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state); |
| 2504 | |
| 2505 | /** |
Thierry Reding | f5e7840 | 2015-01-28 14:54:32 +0100 | [diff] [blame] | 2506 | * __drm_atomic_helper_crtc_destroy_state - release CRTC state |
| 2507 | * @crtc: CRTC object |
| 2508 | * @state: CRTC state object to release |
| 2509 | * |
| 2510 | * Releases all resources stored in the CRTC state without actually freeing |
| 2511 | * the memory of the CRTC state. This is useful for drivers that subclass the |
| 2512 | * CRTC state. |
| 2513 | */ |
| 2514 | void __drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc, |
| 2515 | struct drm_crtc_state *state) |
| 2516 | { |
Markus Elfring | 5f91190 | 2015-11-06 12:03:46 +0100 | [diff] [blame] | 2517 | drm_property_unreference_blob(state->mode_blob); |
Thierry Reding | f5e7840 | 2015-01-28 14:54:32 +0100 | [diff] [blame] | 2518 | } |
| 2519 | EXPORT_SYMBOL(__drm_atomic_helper_crtc_destroy_state); |
| 2520 | |
| 2521 | /** |
Daniel Vetter | d461701 | 2014-11-03 15:56:43 +0100 | [diff] [blame] | 2522 | * drm_atomic_helper_crtc_destroy_state - default state destroy hook |
| 2523 | * @crtc: drm CRTC |
| 2524 | * @state: CRTC state object to release |
| 2525 | * |
| 2526 | * Default CRTC state destroy hook for drivers which don't have their own |
| 2527 | * subclassed CRTC state structure. |
| 2528 | */ |
| 2529 | void drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc, |
| 2530 | struct drm_crtc_state *state) |
| 2531 | { |
Thierry Reding | f5e7840 | 2015-01-28 14:54:32 +0100 | [diff] [blame] | 2532 | __drm_atomic_helper_crtc_destroy_state(crtc, state); |
Daniel Vetter | d461701 | 2014-11-03 15:56:43 +0100 | [diff] [blame] | 2533 | kfree(state); |
| 2534 | } |
| 2535 | EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state); |
| 2536 | |
| 2537 | /** |
| 2538 | * drm_atomic_helper_plane_reset - default ->reset hook for planes |
| 2539 | * @plane: drm plane |
| 2540 | * |
| 2541 | * Resets the atomic state for @plane by freeing the state pointer (which might |
| 2542 | * be NULL, e.g. at driver load time) and allocating a new empty state object. |
| 2543 | */ |
| 2544 | void drm_atomic_helper_plane_reset(struct drm_plane *plane) |
| 2545 | { |
Daniel Vetter | 321ebf0 | 2014-11-04 22:57:27 +0100 | [diff] [blame] | 2546 | if (plane->state && plane->state->fb) |
| 2547 | drm_framebuffer_unreference(plane->state->fb); |
| 2548 | |
Daniel Vetter | d461701 | 2014-11-03 15:56:43 +0100 | [diff] [blame] | 2549 | kfree(plane->state); |
| 2550 | plane->state = kzalloc(sizeof(*plane->state), GFP_KERNEL); |
Daniel Vetter | 07cc0ef | 2014-11-27 15:49:39 +0100 | [diff] [blame] | 2551 | |
| 2552 | if (plane->state) |
| 2553 | plane->state->plane = plane; |
Daniel Vetter | d461701 | 2014-11-03 15:56:43 +0100 | [diff] [blame] | 2554 | } |
| 2555 | EXPORT_SYMBOL(drm_atomic_helper_plane_reset); |
| 2556 | |
| 2557 | /** |
Thierry Reding | f5e7840 | 2015-01-28 14:54:32 +0100 | [diff] [blame] | 2558 | * __drm_atomic_helper_plane_duplicate_state - copy atomic plane state |
| 2559 | * @plane: plane object |
| 2560 | * @state: atomic plane state |
| 2561 | * |
| 2562 | * Copies atomic state from a plane's current state. This is useful for |
| 2563 | * drivers that subclass the plane state. |
| 2564 | */ |
| 2565 | void __drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane, |
| 2566 | struct drm_plane_state *state) |
| 2567 | { |
| 2568 | memcpy(state, plane->state, sizeof(*state)); |
| 2569 | |
| 2570 | if (state->fb) |
| 2571 | drm_framebuffer_reference(state->fb); |
| 2572 | } |
| 2573 | EXPORT_SYMBOL(__drm_atomic_helper_plane_duplicate_state); |
| 2574 | |
| 2575 | /** |
Daniel Vetter | d461701 | 2014-11-03 15:56:43 +0100 | [diff] [blame] | 2576 | * drm_atomic_helper_plane_duplicate_state - default state duplicate hook |
| 2577 | * @plane: drm plane |
| 2578 | * |
| 2579 | * Default plane state duplicate hook for drivers which don't have their own |
| 2580 | * subclassed plane state structure. |
| 2581 | */ |
| 2582 | struct drm_plane_state * |
| 2583 | drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane) |
| 2584 | { |
Daniel Vetter | 321ebf0 | 2014-11-04 22:57:27 +0100 | [diff] [blame] | 2585 | struct drm_plane_state *state; |
| 2586 | |
Daniel Vetter | d461701 | 2014-11-03 15:56:43 +0100 | [diff] [blame] | 2587 | if (WARN_ON(!plane->state)) |
| 2588 | return NULL; |
| 2589 | |
Thierry Reding | f5e7840 | 2015-01-28 14:54:32 +0100 | [diff] [blame] | 2590 | state = kmalloc(sizeof(*state), GFP_KERNEL); |
| 2591 | if (state) |
| 2592 | __drm_atomic_helper_plane_duplicate_state(plane, state); |
Daniel Vetter | 321ebf0 | 2014-11-04 22:57:27 +0100 | [diff] [blame] | 2593 | |
| 2594 | return state; |
Daniel Vetter | d461701 | 2014-11-03 15:56:43 +0100 | [diff] [blame] | 2595 | } |
| 2596 | EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state); |
| 2597 | |
| 2598 | /** |
Thierry Reding | f5e7840 | 2015-01-28 14:54:32 +0100 | [diff] [blame] | 2599 | * __drm_atomic_helper_plane_destroy_state - release plane state |
| 2600 | * @plane: plane object |
| 2601 | * @state: plane state object to release |
| 2602 | * |
| 2603 | * Releases all resources stored in the plane state without actually freeing |
| 2604 | * the memory of the plane state. This is useful for drivers that subclass the |
| 2605 | * plane state. |
| 2606 | */ |
| 2607 | void __drm_atomic_helper_plane_destroy_state(struct drm_plane *plane, |
| 2608 | struct drm_plane_state *state) |
| 2609 | { |
| 2610 | if (state->fb) |
| 2611 | drm_framebuffer_unreference(state->fb); |
| 2612 | } |
| 2613 | EXPORT_SYMBOL(__drm_atomic_helper_plane_destroy_state); |
| 2614 | |
| 2615 | /** |
Daniel Vetter | d461701 | 2014-11-03 15:56:43 +0100 | [diff] [blame] | 2616 | * drm_atomic_helper_plane_destroy_state - default state destroy hook |
| 2617 | * @plane: drm plane |
| 2618 | * @state: plane state object to release |
| 2619 | * |
| 2620 | * Default plane state destroy hook for drivers which don't have their own |
| 2621 | * subclassed plane state structure. |
| 2622 | */ |
| 2623 | void drm_atomic_helper_plane_destroy_state(struct drm_plane *plane, |
Daniel Vetter | 321ebf0 | 2014-11-04 22:57:27 +0100 | [diff] [blame] | 2624 | struct drm_plane_state *state) |
Daniel Vetter | d461701 | 2014-11-03 15:56:43 +0100 | [diff] [blame] | 2625 | { |
Thierry Reding | f5e7840 | 2015-01-28 14:54:32 +0100 | [diff] [blame] | 2626 | __drm_atomic_helper_plane_destroy_state(plane, state); |
Daniel Vetter | d461701 | 2014-11-03 15:56:43 +0100 | [diff] [blame] | 2627 | kfree(state); |
| 2628 | } |
| 2629 | EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state); |
| 2630 | |
| 2631 | /** |
Maarten Lankhorst | 4cd3991 | 2016-01-04 12:53:16 +0100 | [diff] [blame] | 2632 | * __drm_atomic_helper_connector_reset - reset state on connector |
| 2633 | * @connector: drm connector |
| 2634 | * @conn_state: connector state to assign |
| 2635 | * |
| 2636 | * Initializes the newly allocated @conn_state and assigns it to |
| 2637 | * #connector ->state, usually required when initializing the drivers |
| 2638 | * or when called from the ->reset hook. |
| 2639 | * |
| 2640 | * This is useful for drivers that subclass the connector state. |
| 2641 | */ |
| 2642 | void |
| 2643 | __drm_atomic_helper_connector_reset(struct drm_connector *connector, |
| 2644 | struct drm_connector_state *conn_state) |
| 2645 | { |
| 2646 | if (conn_state) |
| 2647 | conn_state->connector = connector; |
| 2648 | |
| 2649 | connector->state = conn_state; |
| 2650 | } |
| 2651 | EXPORT_SYMBOL(__drm_atomic_helper_connector_reset); |
| 2652 | |
| 2653 | /** |
Daniel Vetter | d461701 | 2014-11-03 15:56:43 +0100 | [diff] [blame] | 2654 | * drm_atomic_helper_connector_reset - default ->reset hook for connectors |
| 2655 | * @connector: drm connector |
| 2656 | * |
| 2657 | * Resets the atomic state for @connector by freeing the state pointer (which |
| 2658 | * might be NULL, e.g. at driver load time) and allocating a new empty state |
| 2659 | * object. |
| 2660 | */ |
| 2661 | void drm_atomic_helper_connector_reset(struct drm_connector *connector) |
| 2662 | { |
Maarten Lankhorst | 4cd3991 | 2016-01-04 12:53:16 +0100 | [diff] [blame] | 2663 | struct drm_connector_state *conn_state = |
| 2664 | kzalloc(sizeof(*conn_state), GFP_KERNEL); |
Daniel Vetter | 07cc0ef | 2014-11-27 15:49:39 +0100 | [diff] [blame] | 2665 | |
Maarten Lankhorst | 4cd3991 | 2016-01-04 12:53:16 +0100 | [diff] [blame] | 2666 | kfree(connector->state); |
| 2667 | __drm_atomic_helper_connector_reset(connector, conn_state); |
Daniel Vetter | d461701 | 2014-11-03 15:56:43 +0100 | [diff] [blame] | 2668 | } |
| 2669 | EXPORT_SYMBOL(drm_atomic_helper_connector_reset); |
| 2670 | |
| 2671 | /** |
Thierry Reding | f5e7840 | 2015-01-28 14:54:32 +0100 | [diff] [blame] | 2672 | * __drm_atomic_helper_connector_duplicate_state - copy atomic connector state |
| 2673 | * @connector: connector object |
| 2674 | * @state: atomic connector state |
| 2675 | * |
| 2676 | * Copies atomic state from a connector's current state. This is useful for |
| 2677 | * drivers that subclass the connector state. |
| 2678 | */ |
| 2679 | void |
| 2680 | __drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector, |
| 2681 | struct drm_connector_state *state) |
| 2682 | { |
| 2683 | memcpy(state, connector->state, sizeof(*state)); |
| 2684 | } |
| 2685 | EXPORT_SYMBOL(__drm_atomic_helper_connector_duplicate_state); |
| 2686 | |
| 2687 | /** |
Daniel Vetter | d461701 | 2014-11-03 15:56:43 +0100 | [diff] [blame] | 2688 | * drm_atomic_helper_connector_duplicate_state - default state duplicate hook |
| 2689 | * @connector: drm connector |
| 2690 | * |
| 2691 | * Default connector state duplicate hook for drivers which don't have their own |
| 2692 | * subclassed connector state structure. |
| 2693 | */ |
| 2694 | struct drm_connector_state * |
| 2695 | drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector) |
| 2696 | { |
Thierry Reding | f5e7840 | 2015-01-28 14:54:32 +0100 | [diff] [blame] | 2697 | struct drm_connector_state *state; |
| 2698 | |
Daniel Vetter | d461701 | 2014-11-03 15:56:43 +0100 | [diff] [blame] | 2699 | if (WARN_ON(!connector->state)) |
| 2700 | return NULL; |
| 2701 | |
Thierry Reding | f5e7840 | 2015-01-28 14:54:32 +0100 | [diff] [blame] | 2702 | state = kmalloc(sizeof(*state), GFP_KERNEL); |
| 2703 | if (state) |
| 2704 | __drm_atomic_helper_connector_duplicate_state(connector, state); |
| 2705 | |
| 2706 | return state; |
Daniel Vetter | d461701 | 2014-11-03 15:56:43 +0100 | [diff] [blame] | 2707 | } |
| 2708 | EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state); |
| 2709 | |
| 2710 | /** |
Thierry Reding | 397fd77 | 2015-09-08 15:00:45 +0200 | [diff] [blame] | 2711 | * drm_atomic_helper_duplicate_state - duplicate an atomic state object |
| 2712 | * @dev: DRM device |
| 2713 | * @ctx: lock acquisition context |
| 2714 | * |
| 2715 | * Makes a copy of the current atomic state by looping over all objects and |
Thierry Reding | 1494276 | 2015-12-02 17:50:04 +0100 | [diff] [blame] | 2716 | * duplicating their respective states. This is used for example by suspend/ |
| 2717 | * resume support code to save the state prior to suspend such that it can |
| 2718 | * be restored upon resume. |
Thierry Reding | 397fd77 | 2015-09-08 15:00:45 +0200 | [diff] [blame] | 2719 | * |
| 2720 | * Note that this treats atomic state as persistent between save and restore. |
| 2721 | * Drivers must make sure that this is possible and won't result in confusion |
| 2722 | * or erroneous behaviour. |
| 2723 | * |
| 2724 | * Note that if callers haven't already acquired all modeset locks this might |
| 2725 | * return -EDEADLK, which must be handled by calling drm_modeset_backoff(). |
| 2726 | * |
| 2727 | * Returns: |
| 2728 | * A pointer to the copy of the atomic state object on success or an |
| 2729 | * ERR_PTR()-encoded error code on failure. |
Thierry Reding | 1494276 | 2015-12-02 17:50:04 +0100 | [diff] [blame] | 2730 | * |
| 2731 | * See also: |
| 2732 | * drm_atomic_helper_suspend(), drm_atomic_helper_resume() |
Thierry Reding | 397fd77 | 2015-09-08 15:00:45 +0200 | [diff] [blame] | 2733 | */ |
| 2734 | struct drm_atomic_state * |
| 2735 | drm_atomic_helper_duplicate_state(struct drm_device *dev, |
| 2736 | struct drm_modeset_acquire_ctx *ctx) |
| 2737 | { |
| 2738 | struct drm_atomic_state *state; |
| 2739 | struct drm_connector *conn; |
| 2740 | struct drm_plane *plane; |
| 2741 | struct drm_crtc *crtc; |
| 2742 | int err = 0; |
| 2743 | |
| 2744 | state = drm_atomic_state_alloc(dev); |
| 2745 | if (!state) |
| 2746 | return ERR_PTR(-ENOMEM); |
| 2747 | |
| 2748 | state->acquire_ctx = ctx; |
| 2749 | |
| 2750 | drm_for_each_crtc(crtc, dev) { |
| 2751 | struct drm_crtc_state *crtc_state; |
| 2752 | |
| 2753 | crtc_state = drm_atomic_get_crtc_state(state, crtc); |
| 2754 | if (IS_ERR(crtc_state)) { |
| 2755 | err = PTR_ERR(crtc_state); |
| 2756 | goto free; |
| 2757 | } |
| 2758 | } |
| 2759 | |
| 2760 | drm_for_each_plane(plane, dev) { |
| 2761 | struct drm_plane_state *plane_state; |
| 2762 | |
| 2763 | plane_state = drm_atomic_get_plane_state(state, plane); |
| 2764 | if (IS_ERR(plane_state)) { |
| 2765 | err = PTR_ERR(plane_state); |
| 2766 | goto free; |
| 2767 | } |
| 2768 | } |
| 2769 | |
| 2770 | drm_for_each_connector(conn, dev) { |
| 2771 | struct drm_connector_state *conn_state; |
| 2772 | |
| 2773 | conn_state = drm_atomic_get_connector_state(state, conn); |
| 2774 | if (IS_ERR(conn_state)) { |
| 2775 | err = PTR_ERR(conn_state); |
| 2776 | goto free; |
| 2777 | } |
| 2778 | } |
| 2779 | |
| 2780 | /* clear the acquire context so that it isn't accidentally reused */ |
| 2781 | state->acquire_ctx = NULL; |
| 2782 | |
| 2783 | free: |
| 2784 | if (err < 0) { |
| 2785 | drm_atomic_state_free(state); |
| 2786 | state = ERR_PTR(err); |
| 2787 | } |
| 2788 | |
| 2789 | return state; |
| 2790 | } |
| 2791 | EXPORT_SYMBOL(drm_atomic_helper_duplicate_state); |
| 2792 | |
| 2793 | /** |
Thierry Reding | f5e7840 | 2015-01-28 14:54:32 +0100 | [diff] [blame] | 2794 | * __drm_atomic_helper_connector_destroy_state - release connector state |
| 2795 | * @connector: connector object |
| 2796 | * @state: connector state object to release |
| 2797 | * |
| 2798 | * Releases all resources stored in the connector state without actually |
| 2799 | * freeing the memory of the connector state. This is useful for drivers that |
| 2800 | * subclass the connector state. |
| 2801 | */ |
| 2802 | void |
| 2803 | __drm_atomic_helper_connector_destroy_state(struct drm_connector *connector, |
| 2804 | struct drm_connector_state *state) |
| 2805 | { |
| 2806 | /* |
| 2807 | * This is currently a placeholder so that drivers that subclass the |
| 2808 | * state will automatically do the right thing if code is ever added |
| 2809 | * to this function. |
| 2810 | */ |
| 2811 | } |
| 2812 | EXPORT_SYMBOL(__drm_atomic_helper_connector_destroy_state); |
| 2813 | |
| 2814 | /** |
Daniel Vetter | d461701 | 2014-11-03 15:56:43 +0100 | [diff] [blame] | 2815 | * drm_atomic_helper_connector_destroy_state - default state destroy hook |
| 2816 | * @connector: drm connector |
| 2817 | * @state: connector state object to release |
| 2818 | * |
| 2819 | * Default connector state destroy hook for drivers which don't have their own |
| 2820 | * subclassed connector state structure. |
| 2821 | */ |
| 2822 | void drm_atomic_helper_connector_destroy_state(struct drm_connector *connector, |
| 2823 | struct drm_connector_state *state) |
| 2824 | { |
Thierry Reding | f5e7840 | 2015-01-28 14:54:32 +0100 | [diff] [blame] | 2825 | __drm_atomic_helper_connector_destroy_state(connector, state); |
Daniel Vetter | d461701 | 2014-11-03 15:56:43 +0100 | [diff] [blame] | 2826 | kfree(state); |
| 2827 | } |
| 2828 | EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state); |