Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [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 | |
| 29 | #include <drm/drmP.h> |
| 30 | #include <drm/drm_atomic.h> |
Lionel Landwerlin | 5488dc1 | 2016-02-26 17:05:00 +0000 | [diff] [blame] | 31 | #include <drm/drm_mode.h> |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 32 | #include <drm/drm_plane_helper.h> |
| 33 | |
Thierry Reding | be35f94 | 2016-04-28 15:19:56 +0200 | [diff] [blame] | 34 | #include "drm_crtc_internal.h" |
| 35 | |
Maarten Lankhorst | 036ef57 | 2015-05-18 10:06:40 +0200 | [diff] [blame] | 36 | /** |
| 37 | * drm_atomic_state_default_release - |
| 38 | * release memory initialized by drm_atomic_state_init |
| 39 | * @state: atomic state |
| 40 | * |
| 41 | * Free all the memory allocated by drm_atomic_state_init. |
| 42 | * This is useful for drivers that subclass the atomic state. |
| 43 | */ |
| 44 | void drm_atomic_state_default_release(struct drm_atomic_state *state) |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 45 | { |
| 46 | kfree(state->connectors); |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 47 | kfree(state->crtcs); |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 48 | kfree(state->planes); |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 49 | } |
Maarten Lankhorst | 036ef57 | 2015-05-18 10:06:40 +0200 | [diff] [blame] | 50 | EXPORT_SYMBOL(drm_atomic_state_default_release); |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 51 | |
| 52 | /** |
Maarten Lankhorst | 036ef57 | 2015-05-18 10:06:40 +0200 | [diff] [blame] | 53 | * drm_atomic_state_init - init new atomic state |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 54 | * @dev: DRM device |
Maarten Lankhorst | 036ef57 | 2015-05-18 10:06:40 +0200 | [diff] [blame] | 55 | * @state: atomic state |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 56 | * |
Maarten Lankhorst | 036ef57 | 2015-05-18 10:06:40 +0200 | [diff] [blame] | 57 | * Default implementation for filling in a new atomic state. |
| 58 | * This is useful for drivers that subclass the atomic state. |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 59 | */ |
Maarten Lankhorst | 036ef57 | 2015-05-18 10:06:40 +0200 | [diff] [blame] | 60 | int |
| 61 | drm_atomic_state_init(struct drm_device *dev, struct drm_atomic_state *state) |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 62 | { |
Rob Clark | d34f20d | 2014-12-18 16:01:56 -0500 | [diff] [blame] | 63 | /* TODO legacy paths should maybe do a better job about |
| 64 | * setting this appropriately? |
| 65 | */ |
| 66 | state->allow_modeset = true; |
| 67 | |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 68 | state->crtcs = kcalloc(dev->mode_config.num_crtc, |
| 69 | sizeof(*state->crtcs), GFP_KERNEL); |
| 70 | if (!state->crtcs) |
| 71 | goto fail; |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 72 | state->planes = kcalloc(dev->mode_config.num_total_plane, |
| 73 | sizeof(*state->planes), GFP_KERNEL); |
| 74 | if (!state->planes) |
| 75 | goto fail; |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 76 | |
| 77 | state->dev = dev; |
| 78 | |
Maarten Lankhorst | 036ef57 | 2015-05-18 10:06:40 +0200 | [diff] [blame] | 79 | DRM_DEBUG_ATOMIC("Allocated atomic state %p\n", state); |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 80 | |
Maarten Lankhorst | 036ef57 | 2015-05-18 10:06:40 +0200 | [diff] [blame] | 81 | return 0; |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 82 | fail: |
Maarten Lankhorst | 036ef57 | 2015-05-18 10:06:40 +0200 | [diff] [blame] | 83 | drm_atomic_state_default_release(state); |
| 84 | return -ENOMEM; |
| 85 | } |
| 86 | EXPORT_SYMBOL(drm_atomic_state_init); |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 87 | |
Maarten Lankhorst | 036ef57 | 2015-05-18 10:06:40 +0200 | [diff] [blame] | 88 | /** |
| 89 | * drm_atomic_state_alloc - allocate atomic state |
| 90 | * @dev: DRM device |
| 91 | * |
| 92 | * This allocates an empty atomic state to track updates. |
| 93 | */ |
| 94 | struct drm_atomic_state * |
| 95 | drm_atomic_state_alloc(struct drm_device *dev) |
| 96 | { |
| 97 | struct drm_mode_config *config = &dev->mode_config; |
| 98 | struct drm_atomic_state *state; |
| 99 | |
| 100 | if (!config->funcs->atomic_state_alloc) { |
| 101 | state = kzalloc(sizeof(*state), GFP_KERNEL); |
| 102 | if (!state) |
| 103 | return NULL; |
| 104 | if (drm_atomic_state_init(dev, state) < 0) { |
| 105 | kfree(state); |
| 106 | return NULL; |
| 107 | } |
| 108 | return state; |
| 109 | } |
| 110 | |
| 111 | return config->funcs->atomic_state_alloc(dev); |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 112 | } |
| 113 | EXPORT_SYMBOL(drm_atomic_state_alloc); |
| 114 | |
| 115 | /** |
Maarten Lankhorst | 036ef57 | 2015-05-18 10:06:40 +0200 | [diff] [blame] | 116 | * drm_atomic_state_default_clear - clear base atomic state |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 117 | * @state: atomic state |
| 118 | * |
Maarten Lankhorst | 036ef57 | 2015-05-18 10:06:40 +0200 | [diff] [blame] | 119 | * Default implementation for clearing atomic state. |
| 120 | * This is useful for drivers that subclass the atomic state. |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 121 | */ |
Maarten Lankhorst | 036ef57 | 2015-05-18 10:06:40 +0200 | [diff] [blame] | 122 | void drm_atomic_state_default_clear(struct drm_atomic_state *state) |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 123 | { |
| 124 | struct drm_device *dev = state->dev; |
Daniel Vetter | 6f75cea | 2014-11-19 18:38:07 +0100 | [diff] [blame] | 125 | struct drm_mode_config *config = &dev->mode_config; |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 126 | int i; |
| 127 | |
Daniel Vetter | 17a38d9 | 2015-02-22 12:24:16 +0100 | [diff] [blame] | 128 | DRM_DEBUG_ATOMIC("Clearing atomic state %p\n", state); |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 129 | |
Daniel Vetter | f52b69f1 | 2014-11-19 18:38:08 +0100 | [diff] [blame] | 130 | for (i = 0; i < state->num_connector; i++) { |
Daniel Vetter | 63e83c1 | 2016-06-02 00:06:32 +0200 | [diff] [blame] | 131 | struct drm_connector *connector = state->connectors[i].ptr; |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 132 | |
| 133 | if (!connector) |
| 134 | continue; |
| 135 | |
Dave Airlie | d2307de | 2016-04-27 11:27:39 +1000 | [diff] [blame] | 136 | connector->funcs->atomic_destroy_state(connector, |
Daniel Vetter | 63e83c1 | 2016-06-02 00:06:32 +0200 | [diff] [blame] | 137 | state->connectors[i].state); |
| 138 | state->connectors[i].ptr = NULL; |
| 139 | state->connectors[i].state = NULL; |
Dave Airlie | b164d31 | 2016-04-27 11:10:09 +1000 | [diff] [blame] | 140 | drm_connector_unreference(connector); |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 141 | } |
| 142 | |
Daniel Vetter | 6f75cea | 2014-11-19 18:38:07 +0100 | [diff] [blame] | 143 | for (i = 0; i < config->num_crtc; i++) { |
Daniel Vetter | 5d943aa6 | 2016-06-02 00:06:34 +0200 | [diff] [blame^] | 144 | struct drm_crtc *crtc = state->crtcs[i].ptr; |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 145 | |
| 146 | if (!crtc) |
| 147 | continue; |
| 148 | |
| 149 | crtc->funcs->atomic_destroy_state(crtc, |
Daniel Vetter | 5d943aa6 | 2016-06-02 00:06:34 +0200 | [diff] [blame^] | 150 | state->crtcs[i].state); |
| 151 | state->crtcs[i].ptr = NULL; |
| 152 | state->crtcs[i].state = NULL; |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 153 | } |
| 154 | |
Daniel Vetter | 6f75cea | 2014-11-19 18:38:07 +0100 | [diff] [blame] | 155 | for (i = 0; i < config->num_total_plane; i++) { |
Daniel Vetter | b8b5342 | 2016-06-02 00:06:33 +0200 | [diff] [blame] | 156 | struct drm_plane *plane = state->planes[i].ptr; |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 157 | |
| 158 | if (!plane) |
| 159 | continue; |
| 160 | |
| 161 | plane->funcs->atomic_destroy_state(plane, |
Daniel Vetter | b8b5342 | 2016-06-02 00:06:33 +0200 | [diff] [blame] | 162 | state->planes[i].state); |
| 163 | state->planes[i].ptr = NULL; |
| 164 | state->planes[i].state = NULL; |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 165 | } |
| 166 | } |
Maarten Lankhorst | 036ef57 | 2015-05-18 10:06:40 +0200 | [diff] [blame] | 167 | EXPORT_SYMBOL(drm_atomic_state_default_clear); |
| 168 | |
| 169 | /** |
| 170 | * drm_atomic_state_clear - clear state object |
| 171 | * @state: atomic state |
| 172 | * |
| 173 | * When the w/w mutex algorithm detects a deadlock we need to back off and drop |
| 174 | * all locks. So someone else could sneak in and change the current modeset |
| 175 | * configuration. Which means that all the state assembled in @state is no |
| 176 | * longer an atomic update to the current state, but to some arbitrary earlier |
| 177 | * state. Which could break assumptions the driver's ->atomic_check likely |
| 178 | * relies on. |
| 179 | * |
| 180 | * Hence we must clear all cached state and completely start over, using this |
| 181 | * function. |
| 182 | */ |
| 183 | void drm_atomic_state_clear(struct drm_atomic_state *state) |
| 184 | { |
| 185 | struct drm_device *dev = state->dev; |
| 186 | struct drm_mode_config *config = &dev->mode_config; |
| 187 | |
| 188 | if (config->funcs->atomic_state_clear) |
| 189 | config->funcs->atomic_state_clear(state); |
| 190 | else |
| 191 | drm_atomic_state_default_clear(state); |
| 192 | } |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 193 | EXPORT_SYMBOL(drm_atomic_state_clear); |
| 194 | |
| 195 | /** |
| 196 | * drm_atomic_state_free - free all memory for an atomic state |
| 197 | * @state: atomic state to deallocate |
| 198 | * |
| 199 | * This frees all memory associated with an atomic state, including all the |
| 200 | * per-object state for planes, crtcs and connectors. |
| 201 | */ |
| 202 | void drm_atomic_state_free(struct drm_atomic_state *state) |
| 203 | { |
Maarten Lankhorst | 036ef57 | 2015-05-18 10:06:40 +0200 | [diff] [blame] | 204 | struct drm_device *dev; |
| 205 | struct drm_mode_config *config; |
| 206 | |
Ander Conselvan de Oliveira | a0211bb | 2015-03-30 14:05:43 +0300 | [diff] [blame] | 207 | if (!state) |
| 208 | return; |
| 209 | |
Maarten Lankhorst | 036ef57 | 2015-05-18 10:06:40 +0200 | [diff] [blame] | 210 | dev = state->dev; |
| 211 | config = &dev->mode_config; |
| 212 | |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 213 | drm_atomic_state_clear(state); |
| 214 | |
Daniel Vetter | 17a38d9 | 2015-02-22 12:24:16 +0100 | [diff] [blame] | 215 | DRM_DEBUG_ATOMIC("Freeing atomic state %p\n", state); |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 216 | |
Maarten Lankhorst | 036ef57 | 2015-05-18 10:06:40 +0200 | [diff] [blame] | 217 | if (config->funcs->atomic_state_free) { |
| 218 | config->funcs->atomic_state_free(state); |
| 219 | } else { |
| 220 | drm_atomic_state_default_release(state); |
| 221 | kfree(state); |
| 222 | } |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 223 | } |
| 224 | EXPORT_SYMBOL(drm_atomic_state_free); |
| 225 | |
| 226 | /** |
| 227 | * drm_atomic_get_crtc_state - get crtc state |
| 228 | * @state: global atomic state object |
| 229 | * @crtc: crtc to get state object for |
| 230 | * |
| 231 | * This function returns the crtc state for the given crtc, allocating it if |
| 232 | * needed. It will also grab the relevant crtc lock to make sure that the state |
| 233 | * is consistent. |
| 234 | * |
| 235 | * Returns: |
| 236 | * |
| 237 | * Either the allocated state or the error code encoded into the pointer. When |
| 238 | * the error is EDEADLK then the w/w mutex code has detected a deadlock and the |
| 239 | * entire atomic sequence must be restarted. All other errors are fatal. |
| 240 | */ |
| 241 | struct drm_crtc_state * |
| 242 | drm_atomic_get_crtc_state(struct drm_atomic_state *state, |
| 243 | struct drm_crtc *crtc) |
| 244 | { |
Maarten Lankhorst | 1b26a5e | 2015-05-13 10:37:25 +0200 | [diff] [blame] | 245 | int ret, index = drm_crtc_index(crtc); |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 246 | struct drm_crtc_state *crtc_state; |
| 247 | |
Maarten Lankhorst | 7f4eaa8 | 2016-05-03 11:12:31 +0200 | [diff] [blame] | 248 | WARN_ON(!state->acquire_ctx); |
| 249 | |
Maarten Lankhorst | 1b26a5e | 2015-05-13 10:37:25 +0200 | [diff] [blame] | 250 | crtc_state = drm_atomic_get_existing_crtc_state(state, crtc); |
| 251 | if (crtc_state) |
| 252 | return crtc_state; |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 253 | |
| 254 | ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx); |
| 255 | if (ret) |
| 256 | return ERR_PTR(ret); |
| 257 | |
| 258 | crtc_state = crtc->funcs->atomic_duplicate_state(crtc); |
| 259 | if (!crtc_state) |
| 260 | return ERR_PTR(-ENOMEM); |
| 261 | |
Daniel Vetter | 5d943aa6 | 2016-06-02 00:06:34 +0200 | [diff] [blame^] | 262 | state->crtcs[index].state = crtc_state; |
| 263 | state->crtcs[index].ptr = crtc; |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 264 | crtc_state->state = state; |
| 265 | |
Ville Syrjälä | fa3ab4c | 2015-12-08 18:41:53 +0200 | [diff] [blame] | 266 | DRM_DEBUG_ATOMIC("Added [CRTC:%d:%s] %p state to %p\n", |
| 267 | crtc->base.id, crtc->name, crtc_state, state); |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 268 | |
| 269 | return crtc_state; |
| 270 | } |
| 271 | EXPORT_SYMBOL(drm_atomic_get_crtc_state); |
| 272 | |
| 273 | /** |
Daniel Stone | 819364d | 2015-05-26 14:36:48 +0100 | [diff] [blame] | 274 | * drm_atomic_set_mode_for_crtc - set mode for CRTC |
| 275 | * @state: the CRTC whose incoming state to update |
| 276 | * @mode: kernel-internal mode to use for the CRTC, or NULL to disable |
| 277 | * |
| 278 | * Set a mode (originating from the kernel) on the desired CRTC state. Does |
| 279 | * not change any other state properties, including enable, active, or |
| 280 | * mode_changed. |
| 281 | * |
| 282 | * RETURNS: |
| 283 | * Zero on success, error code on failure. Cannot return -EDEADLK. |
| 284 | */ |
| 285 | int drm_atomic_set_mode_for_crtc(struct drm_crtc_state *state, |
| 286 | struct drm_display_mode *mode) |
| 287 | { |
Daniel Stone | 99cf4a2 | 2015-05-25 19:11:51 +0100 | [diff] [blame] | 288 | struct drm_mode_modeinfo umode; |
| 289 | |
Daniel Stone | 819364d | 2015-05-26 14:36:48 +0100 | [diff] [blame] | 290 | /* Early return for no change. */ |
| 291 | if (mode && memcmp(&state->mode, mode, sizeof(*mode)) == 0) |
| 292 | return 0; |
| 293 | |
Markus Elfring | 5f91190 | 2015-11-06 12:03:46 +0100 | [diff] [blame] | 294 | drm_property_unreference_blob(state->mode_blob); |
Daniel Stone | 99cf4a2 | 2015-05-25 19:11:51 +0100 | [diff] [blame] | 295 | state->mode_blob = NULL; |
| 296 | |
Daniel Stone | 819364d | 2015-05-26 14:36:48 +0100 | [diff] [blame] | 297 | if (mode) { |
Daniel Stone | 99cf4a2 | 2015-05-25 19:11:51 +0100 | [diff] [blame] | 298 | drm_mode_convert_to_umode(&umode, mode); |
| 299 | state->mode_blob = |
| 300 | drm_property_create_blob(state->crtc->dev, |
| 301 | sizeof(umode), |
| 302 | &umode); |
| 303 | if (IS_ERR(state->mode_blob)) |
| 304 | return PTR_ERR(state->mode_blob); |
| 305 | |
Daniel Stone | 819364d | 2015-05-26 14:36:48 +0100 | [diff] [blame] | 306 | drm_mode_copy(&state->mode, mode); |
| 307 | state->enable = true; |
| 308 | DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n", |
| 309 | mode->name, state); |
| 310 | } else { |
| 311 | memset(&state->mode, 0, sizeof(state->mode)); |
| 312 | state->enable = false; |
| 313 | DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n", |
| 314 | state); |
| 315 | } |
| 316 | |
| 317 | return 0; |
| 318 | } |
| 319 | EXPORT_SYMBOL(drm_atomic_set_mode_for_crtc); |
| 320 | |
Daniel Stone | 819364d | 2015-05-26 14:36:48 +0100 | [diff] [blame] | 321 | /** |
Daniel Stone | 955f3c3 | 2015-05-25 19:11:52 +0100 | [diff] [blame] | 322 | * drm_atomic_set_mode_prop_for_crtc - set mode for CRTC |
| 323 | * @state: the CRTC whose incoming state to update |
| 324 | * @blob: pointer to blob property to use for mode |
| 325 | * |
| 326 | * Set a mode (originating from a blob property) on the desired CRTC state. |
| 327 | * This function will take a reference on the blob property for the CRTC state, |
| 328 | * and release the reference held on the state's existing mode property, if any |
| 329 | * was set. |
| 330 | * |
| 331 | * RETURNS: |
| 332 | * Zero on success, error code on failure. Cannot return -EDEADLK. |
| 333 | */ |
| 334 | int drm_atomic_set_mode_prop_for_crtc(struct drm_crtc_state *state, |
| 335 | struct drm_property_blob *blob) |
| 336 | { |
| 337 | if (blob == state->mode_blob) |
| 338 | return 0; |
| 339 | |
Markus Elfring | 5f91190 | 2015-11-06 12:03:46 +0100 | [diff] [blame] | 340 | drm_property_unreference_blob(state->mode_blob); |
Daniel Stone | 955f3c3 | 2015-05-25 19:11:52 +0100 | [diff] [blame] | 341 | state->mode_blob = NULL; |
| 342 | |
| 343 | if (blob) { |
| 344 | if (blob->length != sizeof(struct drm_mode_modeinfo) || |
| 345 | drm_mode_convert_umode(&state->mode, |
| 346 | (const struct drm_mode_modeinfo *) |
| 347 | blob->data)) |
| 348 | return -EINVAL; |
| 349 | |
| 350 | state->mode_blob = drm_property_reference_blob(blob); |
| 351 | state->enable = true; |
| 352 | DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n", |
| 353 | state->mode.name, state); |
| 354 | } else { |
| 355 | memset(&state->mode, 0, sizeof(state->mode)); |
| 356 | state->enable = false; |
| 357 | DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n", |
| 358 | state); |
| 359 | } |
| 360 | |
| 361 | return 0; |
| 362 | } |
| 363 | EXPORT_SYMBOL(drm_atomic_set_mode_prop_for_crtc); |
| 364 | |
| 365 | /** |
Lionel Landwerlin | 5488dc1 | 2016-02-26 17:05:00 +0000 | [diff] [blame] | 366 | * drm_atomic_replace_property_blob - replace a blob property |
| 367 | * @blob: a pointer to the member blob to be replaced |
| 368 | * @new_blob: the new blob to replace with |
Lionel Landwerlin | 5488dc1 | 2016-02-26 17:05:00 +0000 | [diff] [blame] | 369 | * @replaced: whether the blob has been replaced |
| 370 | * |
| 371 | * RETURNS: |
| 372 | * Zero on success, error code on failure |
| 373 | */ |
| 374 | static void |
| 375 | drm_atomic_replace_property_blob(struct drm_property_blob **blob, |
| 376 | struct drm_property_blob *new_blob, |
| 377 | bool *replaced) |
| 378 | { |
| 379 | struct drm_property_blob *old_blob = *blob; |
| 380 | |
| 381 | if (old_blob == new_blob) |
| 382 | return; |
| 383 | |
| 384 | if (old_blob) |
| 385 | drm_property_unreference_blob(old_blob); |
| 386 | if (new_blob) |
| 387 | drm_property_reference_blob(new_blob); |
| 388 | *blob = new_blob; |
| 389 | *replaced = true; |
| 390 | |
| 391 | return; |
| 392 | } |
| 393 | |
| 394 | static int |
| 395 | drm_atomic_replace_property_blob_from_id(struct drm_crtc *crtc, |
| 396 | struct drm_property_blob **blob, |
| 397 | uint64_t blob_id, |
| 398 | ssize_t expected_size, |
| 399 | bool *replaced) |
| 400 | { |
| 401 | struct drm_device *dev = crtc->dev; |
| 402 | struct drm_property_blob *new_blob = NULL; |
| 403 | |
| 404 | if (blob_id != 0) { |
| 405 | new_blob = drm_property_lookup_blob(dev, blob_id); |
| 406 | if (new_blob == NULL) |
| 407 | return -EINVAL; |
| 408 | if (expected_size > 0 && expected_size != new_blob->length) |
| 409 | return -EINVAL; |
| 410 | } |
| 411 | |
| 412 | drm_atomic_replace_property_blob(blob, new_blob, replaced); |
| 413 | |
| 414 | return 0; |
| 415 | } |
| 416 | |
| 417 | /** |
Rob Clark | 40ecc69 | 2014-12-18 16:01:46 -0500 | [diff] [blame] | 418 | * drm_atomic_crtc_set_property - set property on CRTC |
| 419 | * @crtc: the drm CRTC to set a property on |
| 420 | * @state: the state object to update with the new property value |
| 421 | * @property: the property to set |
| 422 | * @val: the new property value |
| 423 | * |
| 424 | * Use this instead of calling crtc->atomic_set_property directly. |
| 425 | * This function handles generic/core properties and calls out to |
| 426 | * driver's ->atomic_set_property() for driver properties. To ensure |
| 427 | * consistent behavior you must call this function rather than the |
| 428 | * driver hook directly. |
| 429 | * |
| 430 | * RETURNS: |
| 431 | * Zero on success, error code on failure |
| 432 | */ |
| 433 | int drm_atomic_crtc_set_property(struct drm_crtc *crtc, |
| 434 | struct drm_crtc_state *state, struct drm_property *property, |
| 435 | uint64_t val) |
| 436 | { |
Daniel Vetter | eab3bbe | 2015-01-22 16:36:21 +0100 | [diff] [blame] | 437 | struct drm_device *dev = crtc->dev; |
| 438 | struct drm_mode_config *config = &dev->mode_config; |
Lionel Landwerlin | 5488dc1 | 2016-02-26 17:05:00 +0000 | [diff] [blame] | 439 | bool replaced = false; |
Daniel Stone | 955f3c3 | 2015-05-25 19:11:52 +0100 | [diff] [blame] | 440 | int ret; |
Daniel Vetter | eab3bbe | 2015-01-22 16:36:21 +0100 | [diff] [blame] | 441 | |
Daniel Stone | 2779836 | 2015-03-19 04:33:26 +0000 | [diff] [blame] | 442 | if (property == config->prop_active) |
Daniel Vetter | eab3bbe | 2015-01-22 16:36:21 +0100 | [diff] [blame] | 443 | state->active = val; |
Daniel Stone | 955f3c3 | 2015-05-25 19:11:52 +0100 | [diff] [blame] | 444 | else if (property == config->prop_mode_id) { |
| 445 | struct drm_property_blob *mode = |
| 446 | drm_property_lookup_blob(dev, val); |
| 447 | ret = drm_atomic_set_mode_prop_for_crtc(state, mode); |
Markus Elfring | 5f91190 | 2015-11-06 12:03:46 +0100 | [diff] [blame] | 448 | drm_property_unreference_blob(mode); |
Daniel Stone | 955f3c3 | 2015-05-25 19:11:52 +0100 | [diff] [blame] | 449 | return ret; |
Lionel Landwerlin | 5488dc1 | 2016-02-26 17:05:00 +0000 | [diff] [blame] | 450 | } else if (property == config->degamma_lut_property) { |
| 451 | ret = drm_atomic_replace_property_blob_from_id(crtc, |
| 452 | &state->degamma_lut, |
| 453 | val, |
| 454 | -1, |
| 455 | &replaced); |
| 456 | state->color_mgmt_changed = replaced; |
| 457 | return ret; |
| 458 | } else if (property == config->ctm_property) { |
| 459 | ret = drm_atomic_replace_property_blob_from_id(crtc, |
| 460 | &state->ctm, |
| 461 | val, |
| 462 | sizeof(struct drm_color_ctm), |
| 463 | &replaced); |
| 464 | state->color_mgmt_changed = replaced; |
| 465 | return ret; |
| 466 | } else if (property == config->gamma_lut_property) { |
| 467 | ret = drm_atomic_replace_property_blob_from_id(crtc, |
| 468 | &state->gamma_lut, |
| 469 | val, |
| 470 | -1, |
| 471 | &replaced); |
| 472 | state->color_mgmt_changed = replaced; |
| 473 | return ret; |
| 474 | } else if (crtc->funcs->atomic_set_property) |
Rob Clark | 40ecc69 | 2014-12-18 16:01:46 -0500 | [diff] [blame] | 475 | return crtc->funcs->atomic_set_property(crtc, state, property, val); |
Daniel Stone | 2779836 | 2015-03-19 04:33:26 +0000 | [diff] [blame] | 476 | else |
| 477 | return -EINVAL; |
| 478 | |
| 479 | return 0; |
Rob Clark | 40ecc69 | 2014-12-18 16:01:46 -0500 | [diff] [blame] | 480 | } |
| 481 | EXPORT_SYMBOL(drm_atomic_crtc_set_property); |
| 482 | |
Daniel Vetter | c0714fc | 2015-12-04 09:45:57 +0100 | [diff] [blame] | 483 | /** |
| 484 | * drm_atomic_crtc_get_property - get property value from CRTC state |
| 485 | * @crtc: the drm CRTC to set a property on |
| 486 | * @state: the state object to get the property value from |
| 487 | * @property: the property to set |
| 488 | * @val: return location for the property value |
| 489 | * |
Rob Clark | ac9c925 | 2014-12-18 16:01:47 -0500 | [diff] [blame] | 490 | * This function handles generic/core properties and calls out to |
| 491 | * driver's ->atomic_get_property() for driver properties. To ensure |
| 492 | * consistent behavior you must call this function rather than the |
| 493 | * driver hook directly. |
Daniel Vetter | c0714fc | 2015-12-04 09:45:57 +0100 | [diff] [blame] | 494 | * |
| 495 | * RETURNS: |
| 496 | * Zero on success, error code on failure |
Rob Clark | ac9c925 | 2014-12-18 16:01:47 -0500 | [diff] [blame] | 497 | */ |
Geliang Tang | bf22f3b | 2015-09-24 03:01:03 -0700 | [diff] [blame] | 498 | static int |
| 499 | drm_atomic_crtc_get_property(struct drm_crtc *crtc, |
Rob Clark | ac9c925 | 2014-12-18 16:01:47 -0500 | [diff] [blame] | 500 | const struct drm_crtc_state *state, |
| 501 | struct drm_property *property, uint64_t *val) |
| 502 | { |
Daniel Stone | 8f164ce | 2015-03-19 04:33:25 +0000 | [diff] [blame] | 503 | struct drm_device *dev = crtc->dev; |
| 504 | struct drm_mode_config *config = &dev->mode_config; |
| 505 | |
| 506 | if (property == config->prop_active) |
| 507 | *val = state->active; |
Daniel Stone | 955f3c3 | 2015-05-25 19:11:52 +0100 | [diff] [blame] | 508 | else if (property == config->prop_mode_id) |
| 509 | *val = (state->mode_blob) ? state->mode_blob->base.id : 0; |
Lionel Landwerlin | 5488dc1 | 2016-02-26 17:05:00 +0000 | [diff] [blame] | 510 | else if (property == config->degamma_lut_property) |
| 511 | *val = (state->degamma_lut) ? state->degamma_lut->base.id : 0; |
| 512 | else if (property == config->ctm_property) |
| 513 | *val = (state->ctm) ? state->ctm->base.id : 0; |
| 514 | else if (property == config->gamma_lut_property) |
| 515 | *val = (state->gamma_lut) ? state->gamma_lut->base.id : 0; |
Daniel Stone | 8f164ce | 2015-03-19 04:33:25 +0000 | [diff] [blame] | 516 | else if (crtc->funcs->atomic_get_property) |
Rob Clark | ac9c925 | 2014-12-18 16:01:47 -0500 | [diff] [blame] | 517 | return crtc->funcs->atomic_get_property(crtc, state, property, val); |
Daniel Stone | 8f164ce | 2015-03-19 04:33:25 +0000 | [diff] [blame] | 518 | else |
| 519 | return -EINVAL; |
| 520 | |
| 521 | return 0; |
Rob Clark | ac9c925 | 2014-12-18 16:01:47 -0500 | [diff] [blame] | 522 | } |
Rob Clark | ac9c925 | 2014-12-18 16:01:47 -0500 | [diff] [blame] | 523 | |
| 524 | /** |
Rob Clark | 5e74373 | 2014-12-18 16:01:51 -0500 | [diff] [blame] | 525 | * drm_atomic_crtc_check - check crtc state |
| 526 | * @crtc: crtc to check |
| 527 | * @state: crtc state to check |
| 528 | * |
| 529 | * Provides core sanity checks for crtc state. |
| 530 | * |
| 531 | * RETURNS: |
| 532 | * Zero on success, error code on failure |
| 533 | */ |
| 534 | static int drm_atomic_crtc_check(struct drm_crtc *crtc, |
| 535 | struct drm_crtc_state *state) |
| 536 | { |
| 537 | /* NOTE: we explicitly don't enforce constraints such as primary |
| 538 | * layer covering entire screen, since that is something we want |
| 539 | * to allow (on hw that supports it). For hw that does not, it |
| 540 | * should be checked in driver's crtc->atomic_check() vfunc. |
| 541 | * |
| 542 | * TODO: Add generic modeset state checks once we support those. |
| 543 | */ |
Daniel Vetter | eab3bbe | 2015-01-22 16:36:21 +0100 | [diff] [blame] | 544 | |
| 545 | if (state->active && !state->enable) { |
Ville Syrjälä | fa3ab4c | 2015-12-08 18:41:53 +0200 | [diff] [blame] | 546 | DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active without enabled\n", |
| 547 | crtc->base.id, crtc->name); |
Daniel Vetter | eab3bbe | 2015-01-22 16:36:21 +0100 | [diff] [blame] | 548 | return -EINVAL; |
| 549 | } |
| 550 | |
Daniel Stone | 99cf4a2 | 2015-05-25 19:11:51 +0100 | [diff] [blame] | 551 | /* The state->enable vs. state->mode_blob checks can be WARN_ON, |
| 552 | * as this is a kernel-internal detail that userspace should never |
| 553 | * be able to trigger. */ |
| 554 | if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) && |
| 555 | WARN_ON(state->enable && !state->mode_blob)) { |
Ville Syrjälä | fa3ab4c | 2015-12-08 18:41:53 +0200 | [diff] [blame] | 556 | DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled without mode blob\n", |
| 557 | crtc->base.id, crtc->name); |
Daniel Stone | 99cf4a2 | 2015-05-25 19:11:51 +0100 | [diff] [blame] | 558 | return -EINVAL; |
| 559 | } |
| 560 | |
| 561 | if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) && |
| 562 | WARN_ON(!state->enable && state->mode_blob)) { |
Ville Syrjälä | fa3ab4c | 2015-12-08 18:41:53 +0200 | [diff] [blame] | 563 | DRM_DEBUG_ATOMIC("[CRTC:%d:%s] disabled with mode blob\n", |
| 564 | crtc->base.id, crtc->name); |
Daniel Stone | 99cf4a2 | 2015-05-25 19:11:51 +0100 | [diff] [blame] | 565 | return -EINVAL; |
| 566 | } |
| 567 | |
Daniel Vetter | 4cba685 | 2015-12-08 09:49:20 +0100 | [diff] [blame] | 568 | /* |
| 569 | * Reject event generation for when a CRTC is off and stays off. |
| 570 | * It wouldn't be hard to implement this, but userspace has a track |
| 571 | * record of happily burning through 100% cpu (or worse, crash) when the |
| 572 | * display pipe is suspended. To avoid all that fun just reject updates |
| 573 | * that ask for events since likely that indicates a bug in the |
| 574 | * compositor's drawing loop. This is consistent with the vblank IOCTL |
| 575 | * and legacy page_flip IOCTL which also reject service on a disabled |
| 576 | * pipe. |
| 577 | */ |
| 578 | if (state->event && !state->active && !crtc->state->active) { |
| 579 | DRM_DEBUG_ATOMIC("[CRTC:%d] requesting event but off\n", |
| 580 | crtc->base.id); |
| 581 | return -EINVAL; |
| 582 | } |
| 583 | |
Rob Clark | 5e74373 | 2014-12-18 16:01:51 -0500 | [diff] [blame] | 584 | return 0; |
| 585 | } |
| 586 | |
| 587 | /** |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 588 | * drm_atomic_get_plane_state - get plane state |
| 589 | * @state: global atomic state object |
| 590 | * @plane: plane to get state object for |
| 591 | * |
| 592 | * This function returns the plane state for the given plane, allocating it if |
| 593 | * needed. It will also grab the relevant plane lock to make sure that the state |
| 594 | * is consistent. |
| 595 | * |
| 596 | * Returns: |
| 597 | * |
| 598 | * Either the allocated state or the error code encoded into the pointer. When |
| 599 | * the error is EDEADLK then the w/w mutex code has detected a deadlock and the |
| 600 | * entire atomic sequence must be restarted. All other errors are fatal. |
| 601 | */ |
| 602 | struct drm_plane_state * |
| 603 | drm_atomic_get_plane_state(struct drm_atomic_state *state, |
| 604 | struct drm_plane *plane) |
| 605 | { |
Maarten Lankhorst | 1b26a5e | 2015-05-13 10:37:25 +0200 | [diff] [blame] | 606 | int ret, index = drm_plane_index(plane); |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 607 | struct drm_plane_state *plane_state; |
| 608 | |
Maarten Lankhorst | 7f4eaa8 | 2016-05-03 11:12:31 +0200 | [diff] [blame] | 609 | WARN_ON(!state->acquire_ctx); |
| 610 | |
Maarten Lankhorst | 1b26a5e | 2015-05-13 10:37:25 +0200 | [diff] [blame] | 611 | plane_state = drm_atomic_get_existing_plane_state(state, plane); |
| 612 | if (plane_state) |
| 613 | return plane_state; |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 614 | |
Daniel Vetter | 4d02e2d | 2014-11-11 10:12:00 +0100 | [diff] [blame] | 615 | ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx); |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 616 | if (ret) |
| 617 | return ERR_PTR(ret); |
| 618 | |
| 619 | plane_state = plane->funcs->atomic_duplicate_state(plane); |
| 620 | if (!plane_state) |
| 621 | return ERR_PTR(-ENOMEM); |
| 622 | |
Daniel Vetter | b8b5342 | 2016-06-02 00:06:33 +0200 | [diff] [blame] | 623 | state->planes[index].state = plane_state; |
| 624 | state->planes[index].ptr = plane; |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 625 | plane_state->state = state; |
| 626 | |
Ville Syrjälä | 9f4c97a | 2015-12-08 18:41:54 +0200 | [diff] [blame] | 627 | DRM_DEBUG_ATOMIC("Added [PLANE:%d:%s] %p state to %p\n", |
| 628 | plane->base.id, plane->name, plane_state, state); |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 629 | |
| 630 | if (plane_state->crtc) { |
| 631 | struct drm_crtc_state *crtc_state; |
| 632 | |
| 633 | crtc_state = drm_atomic_get_crtc_state(state, |
| 634 | plane_state->crtc); |
| 635 | if (IS_ERR(crtc_state)) |
| 636 | return ERR_CAST(crtc_state); |
| 637 | } |
| 638 | |
| 639 | return plane_state; |
| 640 | } |
| 641 | EXPORT_SYMBOL(drm_atomic_get_plane_state); |
| 642 | |
| 643 | /** |
Rob Clark | 40ecc69 | 2014-12-18 16:01:46 -0500 | [diff] [blame] | 644 | * drm_atomic_plane_set_property - set property on plane |
| 645 | * @plane: the drm plane to set a property on |
| 646 | * @state: the state object to update with the new property value |
| 647 | * @property: the property to set |
| 648 | * @val: the new property value |
| 649 | * |
| 650 | * Use this instead of calling plane->atomic_set_property directly. |
| 651 | * This function handles generic/core properties and calls out to |
| 652 | * driver's ->atomic_set_property() for driver properties. To ensure |
| 653 | * consistent behavior you must call this function rather than the |
| 654 | * driver hook directly. |
| 655 | * |
| 656 | * RETURNS: |
| 657 | * Zero on success, error code on failure |
| 658 | */ |
| 659 | int drm_atomic_plane_set_property(struct drm_plane *plane, |
| 660 | struct drm_plane_state *state, struct drm_property *property, |
| 661 | uint64_t val) |
| 662 | { |
Rob Clark | 6b4959f | 2014-12-18 16:01:53 -0500 | [diff] [blame] | 663 | struct drm_device *dev = plane->dev; |
| 664 | struct drm_mode_config *config = &dev->mode_config; |
| 665 | |
| 666 | if (property == config->prop_fb_id) { |
| 667 | struct drm_framebuffer *fb = drm_framebuffer_lookup(dev, val); |
| 668 | drm_atomic_set_fb_for_plane(state, fb); |
| 669 | if (fb) |
| 670 | drm_framebuffer_unreference(fb); |
| 671 | } else if (property == config->prop_crtc_id) { |
| 672 | struct drm_crtc *crtc = drm_crtc_find(dev, val); |
| 673 | return drm_atomic_set_crtc_for_plane(state, crtc); |
| 674 | } else if (property == config->prop_crtc_x) { |
| 675 | state->crtc_x = U642I64(val); |
| 676 | } else if (property == config->prop_crtc_y) { |
| 677 | state->crtc_y = U642I64(val); |
| 678 | } else if (property == config->prop_crtc_w) { |
| 679 | state->crtc_w = val; |
| 680 | } else if (property == config->prop_crtc_h) { |
| 681 | state->crtc_h = val; |
| 682 | } else if (property == config->prop_src_x) { |
| 683 | state->src_x = val; |
| 684 | } else if (property == config->prop_src_y) { |
| 685 | state->src_y = val; |
| 686 | } else if (property == config->prop_src_w) { |
| 687 | state->src_w = val; |
| 688 | } else if (property == config->prop_src_h) { |
| 689 | state->src_h = val; |
Matt Roper | 1da3062 | 2015-01-21 16:35:40 -0800 | [diff] [blame] | 690 | } else if (property == config->rotation_property) { |
| 691 | state->rotation = val; |
Rob Clark | 6b4959f | 2014-12-18 16:01:53 -0500 | [diff] [blame] | 692 | } else if (plane->funcs->atomic_set_property) { |
| 693 | return plane->funcs->atomic_set_property(plane, state, |
| 694 | property, val); |
| 695 | } else { |
| 696 | return -EINVAL; |
| 697 | } |
| 698 | |
| 699 | return 0; |
Rob Clark | 40ecc69 | 2014-12-18 16:01:46 -0500 | [diff] [blame] | 700 | } |
| 701 | EXPORT_SYMBOL(drm_atomic_plane_set_property); |
| 702 | |
Daniel Vetter | c0714fc | 2015-12-04 09:45:57 +0100 | [diff] [blame] | 703 | /** |
| 704 | * drm_atomic_plane_get_property - get property value from plane state |
| 705 | * @plane: the drm plane to set a property on |
| 706 | * @state: the state object to get the property value from |
| 707 | * @property: the property to set |
| 708 | * @val: return location for the property value |
| 709 | * |
Rob Clark | ac9c925 | 2014-12-18 16:01:47 -0500 | [diff] [blame] | 710 | * This function handles generic/core properties and calls out to |
| 711 | * driver's ->atomic_get_property() for driver properties. To ensure |
| 712 | * consistent behavior you must call this function rather than the |
| 713 | * driver hook directly. |
Daniel Vetter | c0714fc | 2015-12-04 09:45:57 +0100 | [diff] [blame] | 714 | * |
| 715 | * RETURNS: |
| 716 | * Zero on success, error code on failure |
Rob Clark | ac9c925 | 2014-12-18 16:01:47 -0500 | [diff] [blame] | 717 | */ |
Daniel Vetter | a97df1c | 2014-12-18 22:49:02 +0100 | [diff] [blame] | 718 | static int |
| 719 | drm_atomic_plane_get_property(struct drm_plane *plane, |
Rob Clark | ac9c925 | 2014-12-18 16:01:47 -0500 | [diff] [blame] | 720 | const struct drm_plane_state *state, |
| 721 | struct drm_property *property, uint64_t *val) |
| 722 | { |
Rob Clark | 6b4959f | 2014-12-18 16:01:53 -0500 | [diff] [blame] | 723 | struct drm_device *dev = plane->dev; |
| 724 | struct drm_mode_config *config = &dev->mode_config; |
| 725 | |
| 726 | if (property == config->prop_fb_id) { |
| 727 | *val = (state->fb) ? state->fb->base.id : 0; |
| 728 | } else if (property == config->prop_crtc_id) { |
| 729 | *val = (state->crtc) ? state->crtc->base.id : 0; |
| 730 | } else if (property == config->prop_crtc_x) { |
| 731 | *val = I642U64(state->crtc_x); |
| 732 | } else if (property == config->prop_crtc_y) { |
| 733 | *val = I642U64(state->crtc_y); |
| 734 | } else if (property == config->prop_crtc_w) { |
| 735 | *val = state->crtc_w; |
| 736 | } else if (property == config->prop_crtc_h) { |
| 737 | *val = state->crtc_h; |
| 738 | } else if (property == config->prop_src_x) { |
| 739 | *val = state->src_x; |
| 740 | } else if (property == config->prop_src_y) { |
| 741 | *val = state->src_y; |
| 742 | } else if (property == config->prop_src_w) { |
| 743 | *val = state->src_w; |
| 744 | } else if (property == config->prop_src_h) { |
| 745 | *val = state->src_h; |
Tvrtko Ursulin | 4cda09c | 2015-02-26 13:49:17 +0000 | [diff] [blame] | 746 | } else if (property == config->rotation_property) { |
| 747 | *val = state->rotation; |
Rob Clark | 6b4959f | 2014-12-18 16:01:53 -0500 | [diff] [blame] | 748 | } else if (plane->funcs->atomic_get_property) { |
Rob Clark | ac9c925 | 2014-12-18 16:01:47 -0500 | [diff] [blame] | 749 | return plane->funcs->atomic_get_property(plane, state, property, val); |
Rob Clark | 6b4959f | 2014-12-18 16:01:53 -0500 | [diff] [blame] | 750 | } else { |
| 751 | return -EINVAL; |
| 752 | } |
| 753 | |
| 754 | return 0; |
Rob Clark | ac9c925 | 2014-12-18 16:01:47 -0500 | [diff] [blame] | 755 | } |
Rob Clark | ac9c925 | 2014-12-18 16:01:47 -0500 | [diff] [blame] | 756 | |
Daniel Vetter | f8aeb41 | 2015-08-26 21:49:42 +0200 | [diff] [blame] | 757 | static bool |
| 758 | plane_switching_crtc(struct drm_atomic_state *state, |
| 759 | struct drm_plane *plane, |
| 760 | struct drm_plane_state *plane_state) |
| 761 | { |
| 762 | if (!plane->state->crtc || !plane_state->crtc) |
| 763 | return false; |
| 764 | |
| 765 | if (plane->state->crtc == plane_state->crtc) |
| 766 | return false; |
| 767 | |
| 768 | /* This could be refined, but currently there's no helper or driver code |
| 769 | * to implement direct switching of active planes nor userspace to take |
| 770 | * advantage of more direct plane switching without the intermediate |
| 771 | * full OFF state. |
| 772 | */ |
| 773 | return true; |
| 774 | } |
| 775 | |
Rob Clark | ac9c925 | 2014-12-18 16:01:47 -0500 | [diff] [blame] | 776 | /** |
Rob Clark | 5e74373 | 2014-12-18 16:01:51 -0500 | [diff] [blame] | 777 | * drm_atomic_plane_check - check plane state |
| 778 | * @plane: plane to check |
| 779 | * @state: plane state to check |
| 780 | * |
| 781 | * Provides core sanity checks for plane state. |
| 782 | * |
| 783 | * RETURNS: |
| 784 | * Zero on success, error code on failure |
| 785 | */ |
| 786 | static int drm_atomic_plane_check(struct drm_plane *plane, |
| 787 | struct drm_plane_state *state) |
| 788 | { |
| 789 | unsigned int fb_width, fb_height; |
Laurent Pinchart | ead8610 | 2015-03-05 02:25:43 +0200 | [diff] [blame] | 790 | int ret; |
Rob Clark | 5e74373 | 2014-12-18 16:01:51 -0500 | [diff] [blame] | 791 | |
| 792 | /* either *both* CRTC and FB must be set, or neither */ |
| 793 | if (WARN_ON(state->crtc && !state->fb)) { |
Daniel Vetter | 17a38d9 | 2015-02-22 12:24:16 +0100 | [diff] [blame] | 794 | DRM_DEBUG_ATOMIC("CRTC set but no FB\n"); |
Rob Clark | 5e74373 | 2014-12-18 16:01:51 -0500 | [diff] [blame] | 795 | return -EINVAL; |
| 796 | } else if (WARN_ON(state->fb && !state->crtc)) { |
Daniel Vetter | 17a38d9 | 2015-02-22 12:24:16 +0100 | [diff] [blame] | 797 | DRM_DEBUG_ATOMIC("FB set but no CRTC\n"); |
Rob Clark | 5e74373 | 2014-12-18 16:01:51 -0500 | [diff] [blame] | 798 | return -EINVAL; |
| 799 | } |
| 800 | |
| 801 | /* if disabled, we don't care about the rest of the state: */ |
| 802 | if (!state->crtc) |
| 803 | return 0; |
| 804 | |
| 805 | /* Check whether this plane is usable on this CRTC */ |
| 806 | if (!(plane->possible_crtcs & drm_crtc_mask(state->crtc))) { |
Daniel Vetter | 17a38d9 | 2015-02-22 12:24:16 +0100 | [diff] [blame] | 807 | DRM_DEBUG_ATOMIC("Invalid crtc for plane\n"); |
Rob Clark | 5e74373 | 2014-12-18 16:01:51 -0500 | [diff] [blame] | 808 | return -EINVAL; |
| 809 | } |
| 810 | |
| 811 | /* Check whether this plane supports the fb pixel format. */ |
Laurent Pinchart | ead8610 | 2015-03-05 02:25:43 +0200 | [diff] [blame] | 812 | ret = drm_plane_check_pixel_format(plane, state->fb->pixel_format); |
| 813 | if (ret) { |
Daniel Vetter | 17a38d9 | 2015-02-22 12:24:16 +0100 | [diff] [blame] | 814 | DRM_DEBUG_ATOMIC("Invalid pixel format %s\n", |
| 815 | drm_get_format_name(state->fb->pixel_format)); |
Laurent Pinchart | ead8610 | 2015-03-05 02:25:43 +0200 | [diff] [blame] | 816 | return ret; |
Rob Clark | 5e74373 | 2014-12-18 16:01:51 -0500 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | /* Give drivers some help against integer overflows */ |
| 820 | if (state->crtc_w > INT_MAX || |
| 821 | state->crtc_x > INT_MAX - (int32_t) state->crtc_w || |
| 822 | state->crtc_h > INT_MAX || |
| 823 | state->crtc_y > INT_MAX - (int32_t) state->crtc_h) { |
Daniel Vetter | 17a38d9 | 2015-02-22 12:24:16 +0100 | [diff] [blame] | 824 | DRM_DEBUG_ATOMIC("Invalid CRTC coordinates %ux%u+%d+%d\n", |
| 825 | state->crtc_w, state->crtc_h, |
| 826 | state->crtc_x, state->crtc_y); |
Rob Clark | 5e74373 | 2014-12-18 16:01:51 -0500 | [diff] [blame] | 827 | return -ERANGE; |
| 828 | } |
| 829 | |
| 830 | fb_width = state->fb->width << 16; |
| 831 | fb_height = state->fb->height << 16; |
| 832 | |
| 833 | /* Make sure source coordinates are inside the fb. */ |
| 834 | if (state->src_w > fb_width || |
| 835 | state->src_x > fb_width - state->src_w || |
| 836 | state->src_h > fb_height || |
| 837 | state->src_y > fb_height - state->src_h) { |
Daniel Vetter | 17a38d9 | 2015-02-22 12:24:16 +0100 | [diff] [blame] | 838 | DRM_DEBUG_ATOMIC("Invalid source coordinates " |
| 839 | "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n", |
| 840 | state->src_w >> 16, ((state->src_w & 0xffff) * 15625) >> 10, |
| 841 | state->src_h >> 16, ((state->src_h & 0xffff) * 15625) >> 10, |
| 842 | state->src_x >> 16, ((state->src_x & 0xffff) * 15625) >> 10, |
| 843 | state->src_y >> 16, ((state->src_y & 0xffff) * 15625) >> 10); |
Rob Clark | 5e74373 | 2014-12-18 16:01:51 -0500 | [diff] [blame] | 844 | return -ENOSPC; |
| 845 | } |
| 846 | |
Daniel Vetter | f8aeb41 | 2015-08-26 21:49:42 +0200 | [diff] [blame] | 847 | if (plane_switching_crtc(state->state, plane, state)) { |
Ville Syrjälä | 9f4c97a | 2015-12-08 18:41:54 +0200 | [diff] [blame] | 848 | DRM_DEBUG_ATOMIC("[PLANE:%d:%s] switching CRTC directly\n", |
| 849 | plane->base.id, plane->name); |
Daniel Vetter | f8aeb41 | 2015-08-26 21:49:42 +0200 | [diff] [blame] | 850 | return -EINVAL; |
| 851 | } |
| 852 | |
Rob Clark | 5e74373 | 2014-12-18 16:01:51 -0500 | [diff] [blame] | 853 | return 0; |
| 854 | } |
| 855 | |
| 856 | /** |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 857 | * drm_atomic_get_connector_state - get connector state |
| 858 | * @state: global atomic state object |
| 859 | * @connector: connector to get state object for |
| 860 | * |
| 861 | * This function returns the connector state for the given connector, |
| 862 | * allocating it if needed. It will also grab the relevant connector lock to |
| 863 | * make sure that the state is consistent. |
| 864 | * |
| 865 | * Returns: |
| 866 | * |
| 867 | * Either the allocated state or the error code encoded into the pointer. When |
| 868 | * the error is EDEADLK then the w/w mutex code has detected a deadlock and the |
| 869 | * entire atomic sequence must be restarted. All other errors are fatal. |
| 870 | */ |
| 871 | struct drm_connector_state * |
| 872 | drm_atomic_get_connector_state(struct drm_atomic_state *state, |
| 873 | struct drm_connector *connector) |
| 874 | { |
| 875 | int ret, index; |
| 876 | struct drm_mode_config *config = &connector->dev->mode_config; |
| 877 | struct drm_connector_state *connector_state; |
| 878 | |
Maarten Lankhorst | 7f4eaa8 | 2016-05-03 11:12:31 +0200 | [diff] [blame] | 879 | WARN_ON(!state->acquire_ctx); |
| 880 | |
Daniel Vetter | c7eb76f | 2014-11-19 18:38:06 +0100 | [diff] [blame] | 881 | ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx); |
| 882 | if (ret) |
| 883 | return ERR_PTR(ret); |
| 884 | |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 885 | index = drm_connector_index(connector); |
| 886 | |
Daniel Vetter | f52b69f1 | 2014-11-19 18:38:08 +0100 | [diff] [blame] | 887 | if (index >= state->num_connector) { |
Daniel Vetter | 63e83c1 | 2016-06-02 00:06:32 +0200 | [diff] [blame] | 888 | struct __drm_connnectors_state *c; |
Maarten Lankhorst | 5fff80b | 2016-02-17 08:32:05 +0100 | [diff] [blame] | 889 | int alloc = max(index + 1, config->num_connector); |
| 890 | |
| 891 | c = krealloc(state->connectors, alloc * sizeof(*state->connectors), GFP_KERNEL); |
| 892 | if (!c) |
| 893 | return ERR_PTR(-ENOMEM); |
| 894 | |
| 895 | state->connectors = c; |
| 896 | memset(&state->connectors[state->num_connector], 0, |
| 897 | sizeof(*state->connectors) * (alloc - state->num_connector)); |
| 898 | |
Maarten Lankhorst | 5fff80b | 2016-02-17 08:32:05 +0100 | [diff] [blame] | 899 | state->num_connector = alloc; |
Daniel Vetter | f52b69f1 | 2014-11-19 18:38:08 +0100 | [diff] [blame] | 900 | } |
| 901 | |
Daniel Vetter | 63e83c1 | 2016-06-02 00:06:32 +0200 | [diff] [blame] | 902 | if (state->connectors[index].state) |
| 903 | return state->connectors[index].state; |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 904 | |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 905 | connector_state = connector->funcs->atomic_duplicate_state(connector); |
| 906 | if (!connector_state) |
| 907 | return ERR_PTR(-ENOMEM); |
| 908 | |
Dave Airlie | b164d31 | 2016-04-27 11:10:09 +1000 | [diff] [blame] | 909 | drm_connector_reference(connector); |
Daniel Vetter | 63e83c1 | 2016-06-02 00:06:32 +0200 | [diff] [blame] | 910 | state->connectors[index].state = connector_state; |
| 911 | state->connectors[index].ptr = connector; |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 912 | connector_state->state = state; |
| 913 | |
Daniel Vetter | 17a38d9 | 2015-02-22 12:24:16 +0100 | [diff] [blame] | 914 | DRM_DEBUG_ATOMIC("Added [CONNECTOR:%d] %p state to %p\n", |
| 915 | connector->base.id, connector_state, state); |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 916 | |
| 917 | if (connector_state->crtc) { |
| 918 | struct drm_crtc_state *crtc_state; |
| 919 | |
| 920 | crtc_state = drm_atomic_get_crtc_state(state, |
| 921 | connector_state->crtc); |
| 922 | if (IS_ERR(crtc_state)) |
| 923 | return ERR_CAST(crtc_state); |
| 924 | } |
| 925 | |
| 926 | return connector_state; |
| 927 | } |
| 928 | EXPORT_SYMBOL(drm_atomic_get_connector_state); |
| 929 | |
| 930 | /** |
Rob Clark | 40ecc69 | 2014-12-18 16:01:46 -0500 | [diff] [blame] | 931 | * drm_atomic_connector_set_property - set property on connector. |
| 932 | * @connector: the drm connector to set a property on |
| 933 | * @state: the state object to update with the new property value |
| 934 | * @property: the property to set |
| 935 | * @val: the new property value |
| 936 | * |
| 937 | * Use this instead of calling connector->atomic_set_property directly. |
| 938 | * This function handles generic/core properties and calls out to |
| 939 | * driver's ->atomic_set_property() for driver properties. To ensure |
| 940 | * consistent behavior you must call this function rather than the |
| 941 | * driver hook directly. |
| 942 | * |
| 943 | * RETURNS: |
| 944 | * Zero on success, error code on failure |
| 945 | */ |
| 946 | int drm_atomic_connector_set_property(struct drm_connector *connector, |
| 947 | struct drm_connector_state *state, struct drm_property *property, |
| 948 | uint64_t val) |
| 949 | { |
| 950 | struct drm_device *dev = connector->dev; |
| 951 | struct drm_mode_config *config = &dev->mode_config; |
| 952 | |
Rob Clark | ae16c59 | 2014-12-18 16:01:54 -0500 | [diff] [blame] | 953 | if (property == config->prop_crtc_id) { |
| 954 | struct drm_crtc *crtc = drm_crtc_find(dev, val); |
| 955 | return drm_atomic_set_crtc_for_connector(state, crtc); |
| 956 | } else if (property == config->dpms_property) { |
Rob Clark | 40ecc69 | 2014-12-18 16:01:46 -0500 | [diff] [blame] | 957 | /* setting DPMS property requires special handling, which |
| 958 | * is done in legacy setprop path for us. Disallow (for |
| 959 | * now?) atomic writes to DPMS property: |
| 960 | */ |
| 961 | return -EINVAL; |
| 962 | } else if (connector->funcs->atomic_set_property) { |
| 963 | return connector->funcs->atomic_set_property(connector, |
| 964 | state, property, val); |
| 965 | } else { |
| 966 | return -EINVAL; |
| 967 | } |
| 968 | } |
| 969 | EXPORT_SYMBOL(drm_atomic_connector_set_property); |
| 970 | |
Daniel Vetter | c0714fc | 2015-12-04 09:45:57 +0100 | [diff] [blame] | 971 | /** |
| 972 | * drm_atomic_connector_get_property - get property value from connector state |
| 973 | * @connector: the drm connector to set a property on |
| 974 | * @state: the state object to get the property value from |
| 975 | * @property: the property to set |
| 976 | * @val: return location for the property value |
| 977 | * |
Rob Clark | ac9c925 | 2014-12-18 16:01:47 -0500 | [diff] [blame] | 978 | * This function handles generic/core properties and calls out to |
| 979 | * driver's ->atomic_get_property() for driver properties. To ensure |
| 980 | * consistent behavior you must call this function rather than the |
| 981 | * driver hook directly. |
Daniel Vetter | c0714fc | 2015-12-04 09:45:57 +0100 | [diff] [blame] | 982 | * |
| 983 | * RETURNS: |
| 984 | * Zero on success, error code on failure |
Rob Clark | ac9c925 | 2014-12-18 16:01:47 -0500 | [diff] [blame] | 985 | */ |
Daniel Vetter | a97df1c | 2014-12-18 22:49:02 +0100 | [diff] [blame] | 986 | static int |
| 987 | drm_atomic_connector_get_property(struct drm_connector *connector, |
Rob Clark | ac9c925 | 2014-12-18 16:01:47 -0500 | [diff] [blame] | 988 | const struct drm_connector_state *state, |
| 989 | struct drm_property *property, uint64_t *val) |
| 990 | { |
| 991 | struct drm_device *dev = connector->dev; |
| 992 | struct drm_mode_config *config = &dev->mode_config; |
| 993 | |
Rob Clark | ae16c59 | 2014-12-18 16:01:54 -0500 | [diff] [blame] | 994 | if (property == config->prop_crtc_id) { |
| 995 | *val = (state->crtc) ? state->crtc->base.id : 0; |
| 996 | } else if (property == config->dpms_property) { |
Rob Clark | ac9c925 | 2014-12-18 16:01:47 -0500 | [diff] [blame] | 997 | *val = connector->dpms; |
| 998 | } else if (connector->funcs->atomic_get_property) { |
| 999 | return connector->funcs->atomic_get_property(connector, |
| 1000 | state, property, val); |
| 1001 | } else { |
| 1002 | return -EINVAL; |
| 1003 | } |
| 1004 | |
| 1005 | return 0; |
| 1006 | } |
Rob Clark | ac9c925 | 2014-12-18 16:01:47 -0500 | [diff] [blame] | 1007 | |
Rob Clark | 88a48e2 | 2014-12-18 16:01:50 -0500 | [diff] [blame] | 1008 | int drm_atomic_get_property(struct drm_mode_object *obj, |
| 1009 | struct drm_property *property, uint64_t *val) |
| 1010 | { |
| 1011 | struct drm_device *dev = property->dev; |
| 1012 | int ret; |
| 1013 | |
| 1014 | switch (obj->type) { |
| 1015 | case DRM_MODE_OBJECT_CONNECTOR: { |
| 1016 | struct drm_connector *connector = obj_to_connector(obj); |
| 1017 | WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex)); |
| 1018 | ret = drm_atomic_connector_get_property(connector, |
| 1019 | connector->state, property, val); |
| 1020 | break; |
| 1021 | } |
| 1022 | case DRM_MODE_OBJECT_CRTC: { |
| 1023 | struct drm_crtc *crtc = obj_to_crtc(obj); |
| 1024 | WARN_ON(!drm_modeset_is_locked(&crtc->mutex)); |
| 1025 | ret = drm_atomic_crtc_get_property(crtc, |
| 1026 | crtc->state, property, val); |
| 1027 | break; |
| 1028 | } |
| 1029 | case DRM_MODE_OBJECT_PLANE: { |
| 1030 | struct drm_plane *plane = obj_to_plane(obj); |
| 1031 | WARN_ON(!drm_modeset_is_locked(&plane->mutex)); |
| 1032 | ret = drm_atomic_plane_get_property(plane, |
| 1033 | plane->state, property, val); |
| 1034 | break; |
| 1035 | } |
| 1036 | default: |
| 1037 | ret = -EINVAL; |
| 1038 | break; |
| 1039 | } |
| 1040 | |
| 1041 | return ret; |
| 1042 | } |
| 1043 | |
| 1044 | /** |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 1045 | * drm_atomic_set_crtc_for_plane - set crtc for plane |
Daniel Vetter | 07cc0ef | 2014-11-27 15:49:39 +0100 | [diff] [blame] | 1046 | * @plane_state: the plane whose incoming state to update |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 1047 | * @crtc: crtc to use for the plane |
| 1048 | * |
| 1049 | * Changing the assigned crtc for a plane requires us to grab the lock and state |
| 1050 | * for the new crtc, as needed. This function takes care of all these details |
| 1051 | * besides updating the pointer in the state object itself. |
| 1052 | * |
| 1053 | * Returns: |
| 1054 | * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK |
| 1055 | * then the w/w mutex code has detected a deadlock and the entire atomic |
| 1056 | * sequence must be restarted. All other errors are fatal. |
| 1057 | */ |
| 1058 | int |
Daniel Vetter | 07cc0ef | 2014-11-27 15:49:39 +0100 | [diff] [blame] | 1059 | drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state, |
| 1060 | struct drm_crtc *crtc) |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 1061 | { |
Daniel Vetter | 07cc0ef | 2014-11-27 15:49:39 +0100 | [diff] [blame] | 1062 | struct drm_plane *plane = plane_state->plane; |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 1063 | struct drm_crtc_state *crtc_state; |
| 1064 | |
Rob Clark | 6ddd388 | 2014-11-21 15:28:31 -0500 | [diff] [blame] | 1065 | if (plane_state->crtc) { |
| 1066 | crtc_state = drm_atomic_get_crtc_state(plane_state->state, |
| 1067 | plane_state->crtc); |
| 1068 | if (WARN_ON(IS_ERR(crtc_state))) |
| 1069 | return PTR_ERR(crtc_state); |
| 1070 | |
| 1071 | crtc_state->plane_mask &= ~(1 << drm_plane_index(plane)); |
| 1072 | } |
| 1073 | |
| 1074 | plane_state->crtc = crtc; |
| 1075 | |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 1076 | if (crtc) { |
| 1077 | crtc_state = drm_atomic_get_crtc_state(plane_state->state, |
| 1078 | crtc); |
| 1079 | if (IS_ERR(crtc_state)) |
| 1080 | return PTR_ERR(crtc_state); |
Rob Clark | 6ddd388 | 2014-11-21 15:28:31 -0500 | [diff] [blame] | 1081 | crtc_state->plane_mask |= (1 << drm_plane_index(plane)); |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 1082 | } |
| 1083 | |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 1084 | if (crtc) |
Ville Syrjälä | fa3ab4c | 2015-12-08 18:41:53 +0200 | [diff] [blame] | 1085 | DRM_DEBUG_ATOMIC("Link plane state %p to [CRTC:%d:%s]\n", |
| 1086 | plane_state, crtc->base.id, crtc->name); |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 1087 | else |
Daniel Vetter | 17a38d9 | 2015-02-22 12:24:16 +0100 | [diff] [blame] | 1088 | DRM_DEBUG_ATOMIC("Link plane state %p to [NOCRTC]\n", |
| 1089 | plane_state); |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 1090 | |
| 1091 | return 0; |
| 1092 | } |
| 1093 | EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane); |
| 1094 | |
| 1095 | /** |
John Hunter | 16d78bc2e | 2015-04-07 19:38:50 +0800 | [diff] [blame] | 1096 | * drm_atomic_set_fb_for_plane - set framebuffer for plane |
Daniel Vetter | 321ebf0 | 2014-11-04 22:57:27 +0100 | [diff] [blame] | 1097 | * @plane_state: atomic state object for the plane |
| 1098 | * @fb: fb to use for the plane |
| 1099 | * |
| 1100 | * Changing the assigned framebuffer for a plane requires us to grab a reference |
| 1101 | * to the new fb and drop the reference to the old fb, if there is one. This |
| 1102 | * function takes care of all these details besides updating the pointer in the |
| 1103 | * state object itself. |
| 1104 | */ |
| 1105 | void |
| 1106 | drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state, |
| 1107 | struct drm_framebuffer *fb) |
| 1108 | { |
| 1109 | if (plane_state->fb) |
| 1110 | drm_framebuffer_unreference(plane_state->fb); |
| 1111 | if (fb) |
| 1112 | drm_framebuffer_reference(fb); |
| 1113 | plane_state->fb = fb; |
| 1114 | |
| 1115 | if (fb) |
Daniel Vetter | 17a38d9 | 2015-02-22 12:24:16 +0100 | [diff] [blame] | 1116 | DRM_DEBUG_ATOMIC("Set [FB:%d] for plane state %p\n", |
| 1117 | fb->base.id, plane_state); |
Daniel Vetter | 321ebf0 | 2014-11-04 22:57:27 +0100 | [diff] [blame] | 1118 | else |
Daniel Vetter | 17a38d9 | 2015-02-22 12:24:16 +0100 | [diff] [blame] | 1119 | DRM_DEBUG_ATOMIC("Set [NOFB] for plane state %p\n", |
| 1120 | plane_state); |
Daniel Vetter | 321ebf0 | 2014-11-04 22:57:27 +0100 | [diff] [blame] | 1121 | } |
| 1122 | EXPORT_SYMBOL(drm_atomic_set_fb_for_plane); |
| 1123 | |
| 1124 | /** |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 1125 | * drm_atomic_set_crtc_for_connector - set crtc for connector |
| 1126 | * @conn_state: atomic state object for the connector |
| 1127 | * @crtc: crtc to use for the connector |
| 1128 | * |
| 1129 | * Changing the assigned crtc for a connector requires us to grab the lock and |
| 1130 | * state for the new crtc, as needed. This function takes care of all these |
| 1131 | * details besides updating the pointer in the state object itself. |
| 1132 | * |
| 1133 | * Returns: |
| 1134 | * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK |
| 1135 | * then the w/w mutex code has detected a deadlock and the entire atomic |
| 1136 | * sequence must be restarted. All other errors are fatal. |
| 1137 | */ |
| 1138 | int |
| 1139 | drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state, |
| 1140 | struct drm_crtc *crtc) |
| 1141 | { |
| 1142 | struct drm_crtc_state *crtc_state; |
| 1143 | |
Chris Wilson | e2d800a | 2016-05-06 12:47:45 +0100 | [diff] [blame] | 1144 | if (conn_state->crtc == crtc) |
| 1145 | return 0; |
| 1146 | |
| 1147 | if (conn_state->crtc) { |
Maarten Lankhorst | 4cd9fa5 | 2016-01-04 12:53:18 +0100 | [diff] [blame] | 1148 | crtc_state = drm_atomic_get_existing_crtc_state(conn_state->state, |
| 1149 | conn_state->crtc); |
| 1150 | |
| 1151 | crtc_state->connector_mask &= |
| 1152 | ~(1 << drm_connector_index(conn_state->connector)); |
Chris Wilson | e2d800a | 2016-05-06 12:47:45 +0100 | [diff] [blame] | 1153 | |
| 1154 | drm_connector_unreference(conn_state->connector); |
| 1155 | conn_state->crtc = NULL; |
Maarten Lankhorst | 4cd9fa5 | 2016-01-04 12:53:18 +0100 | [diff] [blame] | 1156 | } |
| 1157 | |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 1158 | if (crtc) { |
| 1159 | crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc); |
| 1160 | if (IS_ERR(crtc_state)) |
| 1161 | return PTR_ERR(crtc_state); |
Maarten Lankhorst | 4cd9fa5 | 2016-01-04 12:53:18 +0100 | [diff] [blame] | 1162 | |
| 1163 | crtc_state->connector_mask |= |
| 1164 | 1 << drm_connector_index(conn_state->connector); |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 1165 | |
Chris Wilson | e2d800a | 2016-05-06 12:47:45 +0100 | [diff] [blame] | 1166 | drm_connector_reference(conn_state->connector); |
| 1167 | conn_state->crtc = crtc; |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 1168 | |
Ville Syrjälä | fa3ab4c | 2015-12-08 18:41:53 +0200 | [diff] [blame] | 1169 | DRM_DEBUG_ATOMIC("Link connector state %p to [CRTC:%d:%s]\n", |
| 1170 | conn_state, crtc->base.id, crtc->name); |
Chris Wilson | e2d800a | 2016-05-06 12:47:45 +0100 | [diff] [blame] | 1171 | } else { |
Daniel Vetter | 17a38d9 | 2015-02-22 12:24:16 +0100 | [diff] [blame] | 1172 | DRM_DEBUG_ATOMIC("Link connector state %p to [NOCRTC]\n", |
| 1173 | conn_state); |
Chris Wilson | e2d800a | 2016-05-06 12:47:45 +0100 | [diff] [blame] | 1174 | } |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 1175 | |
| 1176 | return 0; |
| 1177 | } |
| 1178 | EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector); |
| 1179 | |
| 1180 | /** |
| 1181 | * drm_atomic_add_affected_connectors - add connectors for crtc |
| 1182 | * @state: atomic state |
| 1183 | * @crtc: DRM crtc |
| 1184 | * |
| 1185 | * This function walks the current configuration and adds all connectors |
| 1186 | * currently using @crtc to the atomic configuration @state. Note that this |
| 1187 | * function must acquire the connection mutex. This can potentially cause |
| 1188 | * unneeded seralization if the update is just for the planes on one crtc. Hence |
| 1189 | * drivers and helpers should only call this when really needed (e.g. when a |
| 1190 | * full modeset needs to happen due to some change). |
| 1191 | * |
| 1192 | * Returns: |
| 1193 | * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK |
| 1194 | * then the w/w mutex code has detected a deadlock and the entire atomic |
| 1195 | * sequence must be restarted. All other errors are fatal. |
| 1196 | */ |
| 1197 | int |
| 1198 | drm_atomic_add_affected_connectors(struct drm_atomic_state *state, |
| 1199 | struct drm_crtc *crtc) |
| 1200 | { |
| 1201 | struct drm_mode_config *config = &state->dev->mode_config; |
| 1202 | struct drm_connector *connector; |
| 1203 | struct drm_connector_state *conn_state; |
| 1204 | int ret; |
| 1205 | |
| 1206 | ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx); |
| 1207 | if (ret) |
| 1208 | return ret; |
| 1209 | |
Ville Syrjälä | fa3ab4c | 2015-12-08 18:41:53 +0200 | [diff] [blame] | 1210 | DRM_DEBUG_ATOMIC("Adding all current connectors for [CRTC:%d:%s] to %p\n", |
| 1211 | crtc->base.id, crtc->name, state); |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 1212 | |
| 1213 | /* |
| 1214 | * Changed connectors are already in @state, so only need to look at the |
| 1215 | * current configuration. |
| 1216 | */ |
Daniel Vetter | 9a9f5ce | 2015-07-09 23:44:34 +0200 | [diff] [blame] | 1217 | drm_for_each_connector(connector, state->dev) { |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 1218 | if (connector->state->crtc != crtc) |
| 1219 | continue; |
| 1220 | |
| 1221 | conn_state = drm_atomic_get_connector_state(state, connector); |
| 1222 | if (IS_ERR(conn_state)) |
| 1223 | return PTR_ERR(conn_state); |
| 1224 | } |
| 1225 | |
| 1226 | return 0; |
| 1227 | } |
| 1228 | EXPORT_SYMBOL(drm_atomic_add_affected_connectors); |
| 1229 | |
| 1230 | /** |
Maarten Lankhorst | e01e9f7 | 2015-05-19 16:41:02 +0200 | [diff] [blame] | 1231 | * drm_atomic_add_affected_planes - add planes for crtc |
| 1232 | * @state: atomic state |
| 1233 | * @crtc: DRM crtc |
| 1234 | * |
| 1235 | * This function walks the current configuration and adds all planes |
| 1236 | * currently used by @crtc to the atomic configuration @state. This is useful |
| 1237 | * when an atomic commit also needs to check all currently enabled plane on |
| 1238 | * @crtc, e.g. when changing the mode. It's also useful when re-enabling a CRTC |
| 1239 | * to avoid special code to force-enable all planes. |
| 1240 | * |
| 1241 | * Since acquiring a plane state will always also acquire the w/w mutex of the |
| 1242 | * current CRTC for that plane (if there is any) adding all the plane states for |
| 1243 | * a CRTC will not reduce parallism of atomic updates. |
| 1244 | * |
| 1245 | * Returns: |
| 1246 | * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK |
| 1247 | * then the w/w mutex code has detected a deadlock and the entire atomic |
| 1248 | * sequence must be restarted. All other errors are fatal. |
| 1249 | */ |
| 1250 | int |
| 1251 | drm_atomic_add_affected_planes(struct drm_atomic_state *state, |
| 1252 | struct drm_crtc *crtc) |
| 1253 | { |
| 1254 | struct drm_plane *plane; |
| 1255 | |
| 1256 | WARN_ON(!drm_atomic_get_existing_crtc_state(state, crtc)); |
| 1257 | |
| 1258 | drm_for_each_plane_mask(plane, state->dev, crtc->state->plane_mask) { |
| 1259 | struct drm_plane_state *plane_state = |
| 1260 | drm_atomic_get_plane_state(state, plane); |
| 1261 | |
| 1262 | if (IS_ERR(plane_state)) |
| 1263 | return PTR_ERR(plane_state); |
| 1264 | } |
| 1265 | return 0; |
| 1266 | } |
| 1267 | EXPORT_SYMBOL(drm_atomic_add_affected_planes); |
| 1268 | |
| 1269 | /** |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 1270 | * drm_atomic_legacy_backoff - locking backoff for legacy ioctls |
| 1271 | * @state: atomic state |
| 1272 | * |
| 1273 | * This function should be used by legacy entry points which don't understand |
| 1274 | * -EDEADLK semantics. For simplicity this one will grab all modeset locks after |
John Hunter | 16d78bc2e | 2015-04-07 19:38:50 +0800 | [diff] [blame] | 1275 | * the slowpath completed. |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 1276 | */ |
| 1277 | void drm_atomic_legacy_backoff(struct drm_atomic_state *state) |
| 1278 | { |
| 1279 | int ret; |
| 1280 | |
| 1281 | retry: |
| 1282 | drm_modeset_backoff(state->acquire_ctx); |
| 1283 | |
Thierry Reding | 06eaae4 | 2015-12-02 17:50:03 +0100 | [diff] [blame] | 1284 | ret = drm_modeset_lock_all_ctx(state->dev, state->acquire_ctx); |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 1285 | if (ret) |
| 1286 | goto retry; |
| 1287 | } |
| 1288 | EXPORT_SYMBOL(drm_atomic_legacy_backoff); |
| 1289 | |
| 1290 | /** |
| 1291 | * drm_atomic_check_only - check whether a given config would work |
| 1292 | * @state: atomic configuration to check |
| 1293 | * |
| 1294 | * Note that this function can return -EDEADLK if the driver needed to acquire |
| 1295 | * more locks but encountered a deadlock. The caller must then do the usual w/w |
| 1296 | * backoff dance and restart. All other errors are fatal. |
| 1297 | * |
| 1298 | * Returns: |
| 1299 | * 0 on success, negative error code on failure. |
| 1300 | */ |
| 1301 | int drm_atomic_check_only(struct drm_atomic_state *state) |
| 1302 | { |
Rob Clark | 5e74373 | 2014-12-18 16:01:51 -0500 | [diff] [blame] | 1303 | struct drm_device *dev = state->dev; |
| 1304 | struct drm_mode_config *config = &dev->mode_config; |
Ander Conselvan de Oliveira | df63b99 | 2015-04-10 14:58:39 +0300 | [diff] [blame] | 1305 | struct drm_plane *plane; |
| 1306 | struct drm_plane_state *plane_state; |
| 1307 | struct drm_crtc *crtc; |
| 1308 | struct drm_crtc_state *crtc_state; |
Rob Clark | 5e74373 | 2014-12-18 16:01:51 -0500 | [diff] [blame] | 1309 | int i, ret = 0; |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 1310 | |
Daniel Vetter | 17a38d9 | 2015-02-22 12:24:16 +0100 | [diff] [blame] | 1311 | DRM_DEBUG_ATOMIC("checking %p\n", state); |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 1312 | |
Ander Conselvan de Oliveira | df63b99 | 2015-04-10 14:58:39 +0300 | [diff] [blame] | 1313 | for_each_plane_in_state(state, plane, plane_state, i) { |
| 1314 | ret = drm_atomic_plane_check(plane, plane_state); |
Rob Clark | 5e74373 | 2014-12-18 16:01:51 -0500 | [diff] [blame] | 1315 | if (ret) { |
Ville Syrjälä | 9f4c97a | 2015-12-08 18:41:54 +0200 | [diff] [blame] | 1316 | DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic core check failed\n", |
| 1317 | plane->base.id, plane->name); |
Rob Clark | 5e74373 | 2014-12-18 16:01:51 -0500 | [diff] [blame] | 1318 | return ret; |
| 1319 | } |
| 1320 | } |
| 1321 | |
Ander Conselvan de Oliveira | df63b99 | 2015-04-10 14:58:39 +0300 | [diff] [blame] | 1322 | for_each_crtc_in_state(state, crtc, crtc_state, i) { |
| 1323 | ret = drm_atomic_crtc_check(crtc, crtc_state); |
Rob Clark | 5e74373 | 2014-12-18 16:01:51 -0500 | [diff] [blame] | 1324 | if (ret) { |
Ville Syrjälä | fa3ab4c | 2015-12-08 18:41:53 +0200 | [diff] [blame] | 1325 | DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic core check failed\n", |
| 1326 | crtc->base.id, crtc->name); |
Rob Clark | 5e74373 | 2014-12-18 16:01:51 -0500 | [diff] [blame] | 1327 | return ret; |
| 1328 | } |
| 1329 | } |
| 1330 | |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 1331 | if (config->funcs->atomic_check) |
Rob Clark | 5e74373 | 2014-12-18 16:01:51 -0500 | [diff] [blame] | 1332 | ret = config->funcs->atomic_check(state->dev, state); |
| 1333 | |
Rob Clark | d34f20d | 2014-12-18 16:01:56 -0500 | [diff] [blame] | 1334 | if (!state->allow_modeset) { |
Ander Conselvan de Oliveira | df63b99 | 2015-04-10 14:58:39 +0300 | [diff] [blame] | 1335 | for_each_crtc_in_state(state, crtc, crtc_state, i) { |
Daniel Vetter | 2465ff6 | 2015-06-18 09:58:55 +0200 | [diff] [blame] | 1336 | if (drm_atomic_crtc_needs_modeset(crtc_state)) { |
Ville Syrjälä | fa3ab4c | 2015-12-08 18:41:53 +0200 | [diff] [blame] | 1337 | DRM_DEBUG_ATOMIC("[CRTC:%d:%s] requires full modeset\n", |
| 1338 | crtc->base.id, crtc->name); |
Rob Clark | d34f20d | 2014-12-18 16:01:56 -0500 | [diff] [blame] | 1339 | return -EINVAL; |
| 1340 | } |
| 1341 | } |
| 1342 | } |
| 1343 | |
Rob Clark | 5e74373 | 2014-12-18 16:01:51 -0500 | [diff] [blame] | 1344 | return ret; |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 1345 | } |
| 1346 | EXPORT_SYMBOL(drm_atomic_check_only); |
| 1347 | |
| 1348 | /** |
| 1349 | * drm_atomic_commit - commit configuration atomically |
| 1350 | * @state: atomic configuration to check |
| 1351 | * |
| 1352 | * Note that this function can return -EDEADLK if the driver needed to acquire |
| 1353 | * more locks but encountered a deadlock. The caller must then do the usual w/w |
| 1354 | * backoff dance and restart. All other errors are fatal. |
| 1355 | * |
| 1356 | * Also note that on successful execution ownership of @state is transferred |
| 1357 | * from the caller of this function to the function itself. The caller must not |
| 1358 | * free or in any other way access @state. If the function fails then the caller |
| 1359 | * must clean up @state itself. |
| 1360 | * |
| 1361 | * Returns: |
| 1362 | * 0 on success, negative error code on failure. |
| 1363 | */ |
| 1364 | int drm_atomic_commit(struct drm_atomic_state *state) |
| 1365 | { |
| 1366 | struct drm_mode_config *config = &state->dev->mode_config; |
| 1367 | int ret; |
| 1368 | |
| 1369 | ret = drm_atomic_check_only(state); |
| 1370 | if (ret) |
| 1371 | return ret; |
| 1372 | |
Daniel Vetter | 17a38d9 | 2015-02-22 12:24:16 +0100 | [diff] [blame] | 1373 | DRM_DEBUG_ATOMIC("commiting %p\n", state); |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 1374 | |
| 1375 | return config->funcs->atomic_commit(state->dev, state, false); |
| 1376 | } |
| 1377 | EXPORT_SYMBOL(drm_atomic_commit); |
| 1378 | |
| 1379 | /** |
Maarten Lankhorst | b837ba0 | 2016-04-26 16:11:35 +0200 | [diff] [blame] | 1380 | * drm_atomic_nonblocking_commit - atomic&nonblocking configuration commit |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 1381 | * @state: atomic configuration to check |
| 1382 | * |
| 1383 | * Note that this function can return -EDEADLK if the driver needed to acquire |
| 1384 | * more locks but encountered a deadlock. The caller must then do the usual w/w |
| 1385 | * backoff dance and restart. All other errors are fatal. |
| 1386 | * |
| 1387 | * Also note that on successful execution ownership of @state is transferred |
| 1388 | * from the caller of this function to the function itself. The caller must not |
| 1389 | * free or in any other way access @state. If the function fails then the caller |
| 1390 | * must clean up @state itself. |
| 1391 | * |
| 1392 | * Returns: |
| 1393 | * 0 on success, negative error code on failure. |
| 1394 | */ |
Maarten Lankhorst | b837ba0 | 2016-04-26 16:11:35 +0200 | [diff] [blame] | 1395 | int drm_atomic_nonblocking_commit(struct drm_atomic_state *state) |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 1396 | { |
| 1397 | struct drm_mode_config *config = &state->dev->mode_config; |
| 1398 | int ret; |
| 1399 | |
| 1400 | ret = drm_atomic_check_only(state); |
| 1401 | if (ret) |
| 1402 | return ret; |
| 1403 | |
Maarten Lankhorst | b837ba0 | 2016-04-26 16:11:35 +0200 | [diff] [blame] | 1404 | DRM_DEBUG_ATOMIC("commiting %p nonblocking\n", state); |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 1405 | |
| 1406 | return config->funcs->atomic_commit(state->dev, state, true); |
| 1407 | } |
Maarten Lankhorst | b837ba0 | 2016-04-26 16:11:35 +0200 | [diff] [blame] | 1408 | EXPORT_SYMBOL(drm_atomic_nonblocking_commit); |
Rob Clark | d34f20d | 2014-12-18 16:01:56 -0500 | [diff] [blame] | 1409 | |
| 1410 | /* |
| 1411 | * The big monstor ioctl |
| 1412 | */ |
| 1413 | |
| 1414 | static struct drm_pending_vblank_event *create_vblank_event( |
| 1415 | struct drm_device *dev, struct drm_file *file_priv, uint64_t user_data) |
| 1416 | { |
| 1417 | struct drm_pending_vblank_event *e = NULL; |
Daniel Vetter | 2dd500f | 2016-01-11 22:40:56 +0100 | [diff] [blame] | 1418 | int ret; |
Rob Clark | d34f20d | 2014-12-18 16:01:56 -0500 | [diff] [blame] | 1419 | |
| 1420 | e = kzalloc(sizeof *e, GFP_KERNEL); |
Daniel Vetter | 2dd500f | 2016-01-11 22:40:56 +0100 | [diff] [blame] | 1421 | if (!e) |
| 1422 | return NULL; |
Rob Clark | d34f20d | 2014-12-18 16:01:56 -0500 | [diff] [blame] | 1423 | |
| 1424 | e->event.base.type = DRM_EVENT_FLIP_COMPLETE; |
Daniel Vetter | 2dd500f | 2016-01-11 22:40:56 +0100 | [diff] [blame] | 1425 | e->event.base.length = sizeof(e->event); |
Rob Clark | d34f20d | 2014-12-18 16:01:56 -0500 | [diff] [blame] | 1426 | e->event.user_data = user_data; |
Rob Clark | d34f20d | 2014-12-18 16:01:56 -0500 | [diff] [blame] | 1427 | |
Daniel Vetter | 2dd500f | 2016-01-11 22:40:56 +0100 | [diff] [blame] | 1428 | ret = drm_event_reserve_init(dev, file_priv, &e->base, &e->event.base); |
| 1429 | if (ret) { |
| 1430 | kfree(e); |
| 1431 | return NULL; |
| 1432 | } |
| 1433 | |
Rob Clark | d34f20d | 2014-12-18 16:01:56 -0500 | [diff] [blame] | 1434 | return e; |
| 1435 | } |
| 1436 | |
Rob Clark | d34f20d | 2014-12-18 16:01:56 -0500 | [diff] [blame] | 1437 | static int atomic_set_prop(struct drm_atomic_state *state, |
| 1438 | struct drm_mode_object *obj, struct drm_property *prop, |
| 1439 | uint64_t prop_value) |
| 1440 | { |
| 1441 | struct drm_mode_object *ref; |
| 1442 | int ret; |
| 1443 | |
| 1444 | if (!drm_property_change_valid_get(prop, prop_value, &ref)) |
| 1445 | return -EINVAL; |
| 1446 | |
| 1447 | switch (obj->type) { |
| 1448 | case DRM_MODE_OBJECT_CONNECTOR: { |
| 1449 | struct drm_connector *connector = obj_to_connector(obj); |
| 1450 | struct drm_connector_state *connector_state; |
| 1451 | |
| 1452 | connector_state = drm_atomic_get_connector_state(state, connector); |
| 1453 | if (IS_ERR(connector_state)) { |
| 1454 | ret = PTR_ERR(connector_state); |
| 1455 | break; |
| 1456 | } |
| 1457 | |
| 1458 | ret = drm_atomic_connector_set_property(connector, |
| 1459 | connector_state, prop, prop_value); |
| 1460 | break; |
| 1461 | } |
| 1462 | case DRM_MODE_OBJECT_CRTC: { |
| 1463 | struct drm_crtc *crtc = obj_to_crtc(obj); |
| 1464 | struct drm_crtc_state *crtc_state; |
| 1465 | |
| 1466 | crtc_state = drm_atomic_get_crtc_state(state, crtc); |
| 1467 | if (IS_ERR(crtc_state)) { |
| 1468 | ret = PTR_ERR(crtc_state); |
| 1469 | break; |
| 1470 | } |
| 1471 | |
| 1472 | ret = drm_atomic_crtc_set_property(crtc, |
| 1473 | crtc_state, prop, prop_value); |
| 1474 | break; |
| 1475 | } |
| 1476 | case DRM_MODE_OBJECT_PLANE: { |
| 1477 | struct drm_plane *plane = obj_to_plane(obj); |
| 1478 | struct drm_plane_state *plane_state; |
| 1479 | |
| 1480 | plane_state = drm_atomic_get_plane_state(state, plane); |
| 1481 | if (IS_ERR(plane_state)) { |
| 1482 | ret = PTR_ERR(plane_state); |
| 1483 | break; |
| 1484 | } |
| 1485 | |
| 1486 | ret = drm_atomic_plane_set_property(plane, |
| 1487 | plane_state, prop, prop_value); |
| 1488 | break; |
| 1489 | } |
| 1490 | default: |
| 1491 | ret = -EINVAL; |
| 1492 | break; |
| 1493 | } |
| 1494 | |
| 1495 | drm_property_change_valid_put(prop, ref); |
| 1496 | return ret; |
| 1497 | } |
| 1498 | |
Maarten Lankhorst | 0f45c26 | 2015-11-11 11:29:09 +0100 | [diff] [blame] | 1499 | /** |
Maarten Lankhorst | 9744bf4 | 2015-11-24 10:34:34 +0100 | [diff] [blame] | 1500 | * drm_atomic_clean_old_fb -- Unset old_fb pointers and set plane->fb pointers. |
Maarten Lankhorst | 0f45c26 | 2015-11-11 11:29:09 +0100 | [diff] [blame] | 1501 | * |
| 1502 | * @dev: drm device to check. |
| 1503 | * @plane_mask: plane mask for planes that were updated. |
| 1504 | * @ret: return value, can be -EDEADLK for a retry. |
| 1505 | * |
| 1506 | * Before doing an update plane->old_fb is set to plane->fb, |
| 1507 | * but before dropping the locks old_fb needs to be set to NULL |
| 1508 | * and plane->fb updated. This is a common operation for each |
| 1509 | * atomic update, so this call is split off as a helper. |
| 1510 | */ |
| 1511 | void drm_atomic_clean_old_fb(struct drm_device *dev, |
| 1512 | unsigned plane_mask, |
| 1513 | int ret) |
| 1514 | { |
| 1515 | struct drm_plane *plane; |
| 1516 | |
| 1517 | /* if succeeded, fixup legacy plane crtc/fb ptrs before dropping |
| 1518 | * locks (ie. while it is still safe to deref plane->state). We |
| 1519 | * need to do this here because the driver entry points cannot |
| 1520 | * distinguish between legacy and atomic ioctls. |
| 1521 | */ |
| 1522 | drm_for_each_plane_mask(plane, dev, plane_mask) { |
| 1523 | if (ret == 0) { |
| 1524 | struct drm_framebuffer *new_fb = plane->state->fb; |
| 1525 | if (new_fb) |
| 1526 | drm_framebuffer_reference(new_fb); |
| 1527 | plane->fb = new_fb; |
| 1528 | plane->crtc = plane->state->crtc; |
| 1529 | |
| 1530 | if (plane->old_fb) |
| 1531 | drm_framebuffer_unreference(plane->old_fb); |
| 1532 | } |
| 1533 | plane->old_fb = NULL; |
| 1534 | } |
| 1535 | } |
| 1536 | EXPORT_SYMBOL(drm_atomic_clean_old_fb); |
| 1537 | |
Rob Clark | d34f20d | 2014-12-18 16:01:56 -0500 | [diff] [blame] | 1538 | int drm_mode_atomic_ioctl(struct drm_device *dev, |
| 1539 | void *data, struct drm_file *file_priv) |
| 1540 | { |
| 1541 | struct drm_mode_atomic *arg = data; |
| 1542 | uint32_t __user *objs_ptr = (uint32_t __user *)(unsigned long)(arg->objs_ptr); |
| 1543 | uint32_t __user *count_props_ptr = (uint32_t __user *)(unsigned long)(arg->count_props_ptr); |
| 1544 | uint32_t __user *props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr); |
| 1545 | uint64_t __user *prop_values_ptr = (uint64_t __user *)(unsigned long)(arg->prop_values_ptr); |
| 1546 | unsigned int copied_objs, copied_props; |
| 1547 | struct drm_atomic_state *state; |
| 1548 | struct drm_modeset_acquire_ctx ctx; |
| 1549 | struct drm_plane *plane; |
Ander Conselvan de Oliveira | df63b99 | 2015-04-10 14:58:39 +0300 | [diff] [blame] | 1550 | struct drm_crtc *crtc; |
| 1551 | struct drm_crtc_state *crtc_state; |
Maarten Lankhorst | 4572372 | 2015-11-11 11:29:08 +0100 | [diff] [blame] | 1552 | unsigned plane_mask; |
Rob Clark | d34f20d | 2014-12-18 16:01:56 -0500 | [diff] [blame] | 1553 | int ret = 0; |
| 1554 | unsigned int i, j; |
| 1555 | |
| 1556 | /* disallow for drivers not supporting atomic: */ |
| 1557 | if (!drm_core_check_feature(dev, DRIVER_ATOMIC)) |
| 1558 | return -EINVAL; |
| 1559 | |
| 1560 | /* disallow for userspace that has not enabled atomic cap (even |
| 1561 | * though this may be a bit overkill, since legacy userspace |
| 1562 | * wouldn't know how to call this ioctl) |
| 1563 | */ |
| 1564 | if (!file_priv->atomic) |
| 1565 | return -EINVAL; |
| 1566 | |
| 1567 | if (arg->flags & ~DRM_MODE_ATOMIC_FLAGS) |
| 1568 | return -EINVAL; |
| 1569 | |
| 1570 | if (arg->reserved) |
| 1571 | return -EINVAL; |
| 1572 | |
| 1573 | if ((arg->flags & DRM_MODE_PAGE_FLIP_ASYNC) && |
| 1574 | !dev->mode_config.async_page_flip) |
| 1575 | return -EINVAL; |
| 1576 | |
| 1577 | /* can't test and expect an event at the same time. */ |
| 1578 | if ((arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) && |
| 1579 | (arg->flags & DRM_MODE_PAGE_FLIP_EVENT)) |
| 1580 | return -EINVAL; |
| 1581 | |
| 1582 | drm_modeset_acquire_init(&ctx, 0); |
| 1583 | |
| 1584 | state = drm_atomic_state_alloc(dev); |
| 1585 | if (!state) |
| 1586 | return -ENOMEM; |
| 1587 | |
| 1588 | state->acquire_ctx = &ctx; |
| 1589 | state->allow_modeset = !!(arg->flags & DRM_MODE_ATOMIC_ALLOW_MODESET); |
| 1590 | |
| 1591 | retry: |
Maarten Lankhorst | 4572372 | 2015-11-11 11:29:08 +0100 | [diff] [blame] | 1592 | plane_mask = 0; |
Rob Clark | d34f20d | 2014-12-18 16:01:56 -0500 | [diff] [blame] | 1593 | copied_objs = 0; |
| 1594 | copied_props = 0; |
| 1595 | |
| 1596 | for (i = 0; i < arg->count_objs; i++) { |
| 1597 | uint32_t obj_id, count_props; |
| 1598 | struct drm_mode_object *obj; |
| 1599 | |
| 1600 | if (get_user(obj_id, objs_ptr + copied_objs)) { |
| 1601 | ret = -EFAULT; |
Maarten Lankhorst | ec9f932 | 2015-06-24 08:59:25 +0200 | [diff] [blame] | 1602 | goto out; |
Rob Clark | d34f20d | 2014-12-18 16:01:56 -0500 | [diff] [blame] | 1603 | } |
| 1604 | |
| 1605 | obj = drm_mode_object_find(dev, obj_id, DRM_MODE_OBJECT_ANY); |
Dave Airlie | b164d31 | 2016-04-27 11:10:09 +1000 | [diff] [blame] | 1606 | if (!obj) { |
| 1607 | ret = -ENOENT; |
| 1608 | goto out; |
| 1609 | } |
| 1610 | |
| 1611 | if (!obj->properties) { |
| 1612 | drm_mode_object_unreference(obj); |
Rob Clark | d34f20d | 2014-12-18 16:01:56 -0500 | [diff] [blame] | 1613 | ret = -ENOENT; |
Maarten Lankhorst | ec9f932 | 2015-06-24 08:59:25 +0200 | [diff] [blame] | 1614 | goto out; |
Rob Clark | d34f20d | 2014-12-18 16:01:56 -0500 | [diff] [blame] | 1615 | } |
| 1616 | |
Rob Clark | d34f20d | 2014-12-18 16:01:56 -0500 | [diff] [blame] | 1617 | if (get_user(count_props, count_props_ptr + copied_objs)) { |
Dave Airlie | b164d31 | 2016-04-27 11:10:09 +1000 | [diff] [blame] | 1618 | drm_mode_object_unreference(obj); |
Rob Clark | d34f20d | 2014-12-18 16:01:56 -0500 | [diff] [blame] | 1619 | ret = -EFAULT; |
Maarten Lankhorst | ec9f932 | 2015-06-24 08:59:25 +0200 | [diff] [blame] | 1620 | goto out; |
Rob Clark | d34f20d | 2014-12-18 16:01:56 -0500 | [diff] [blame] | 1621 | } |
| 1622 | |
| 1623 | copied_objs++; |
| 1624 | |
| 1625 | for (j = 0; j < count_props; j++) { |
| 1626 | uint32_t prop_id; |
| 1627 | uint64_t prop_value; |
| 1628 | struct drm_property *prop; |
| 1629 | |
| 1630 | if (get_user(prop_id, props_ptr + copied_props)) { |
Dave Airlie | b164d31 | 2016-04-27 11:10:09 +1000 | [diff] [blame] | 1631 | drm_mode_object_unreference(obj); |
Rob Clark | d34f20d | 2014-12-18 16:01:56 -0500 | [diff] [blame] | 1632 | ret = -EFAULT; |
Maarten Lankhorst | ec9f932 | 2015-06-24 08:59:25 +0200 | [diff] [blame] | 1633 | goto out; |
Rob Clark | d34f20d | 2014-12-18 16:01:56 -0500 | [diff] [blame] | 1634 | } |
| 1635 | |
| 1636 | prop = drm_property_find(dev, prop_id); |
| 1637 | if (!prop) { |
Dave Airlie | b164d31 | 2016-04-27 11:10:09 +1000 | [diff] [blame] | 1638 | drm_mode_object_unreference(obj); |
Rob Clark | d34f20d | 2014-12-18 16:01:56 -0500 | [diff] [blame] | 1639 | ret = -ENOENT; |
Maarten Lankhorst | ec9f932 | 2015-06-24 08:59:25 +0200 | [diff] [blame] | 1640 | goto out; |
Rob Clark | d34f20d | 2014-12-18 16:01:56 -0500 | [diff] [blame] | 1641 | } |
| 1642 | |
Guenter Roeck | 42c5814 | 2015-01-12 21:12:17 -0800 | [diff] [blame] | 1643 | if (copy_from_user(&prop_value, |
| 1644 | prop_values_ptr + copied_props, |
| 1645 | sizeof(prop_value))) { |
Dave Airlie | b164d31 | 2016-04-27 11:10:09 +1000 | [diff] [blame] | 1646 | drm_mode_object_unreference(obj); |
Rob Clark | d34f20d | 2014-12-18 16:01:56 -0500 | [diff] [blame] | 1647 | ret = -EFAULT; |
Maarten Lankhorst | ec9f932 | 2015-06-24 08:59:25 +0200 | [diff] [blame] | 1648 | goto out; |
Rob Clark | d34f20d | 2014-12-18 16:01:56 -0500 | [diff] [blame] | 1649 | } |
| 1650 | |
| 1651 | ret = atomic_set_prop(state, obj, prop, prop_value); |
Dave Airlie | b164d31 | 2016-04-27 11:10:09 +1000 | [diff] [blame] | 1652 | if (ret) { |
| 1653 | drm_mode_object_unreference(obj); |
Maarten Lankhorst | ec9f932 | 2015-06-24 08:59:25 +0200 | [diff] [blame] | 1654 | goto out; |
Dave Airlie | b164d31 | 2016-04-27 11:10:09 +1000 | [diff] [blame] | 1655 | } |
Rob Clark | d34f20d | 2014-12-18 16:01:56 -0500 | [diff] [blame] | 1656 | |
| 1657 | copied_props++; |
| 1658 | } |
Maarten Lankhorst | a9cc54e | 2015-06-24 08:59:24 +0200 | [diff] [blame] | 1659 | |
Maarten Lankhorst | c4749c9 | 2015-08-31 12:25:04 +0200 | [diff] [blame] | 1660 | if (obj->type == DRM_MODE_OBJECT_PLANE && count_props && |
| 1661 | !(arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)) { |
Maarten Lankhorst | a9cc54e | 2015-06-24 08:59:24 +0200 | [diff] [blame] | 1662 | plane = obj_to_plane(obj); |
| 1663 | plane_mask |= (1 << drm_plane_index(plane)); |
| 1664 | plane->old_fb = plane->fb; |
| 1665 | } |
Dave Airlie | b164d31 | 2016-04-27 11:10:09 +1000 | [diff] [blame] | 1666 | drm_mode_object_unreference(obj); |
Rob Clark | d34f20d | 2014-12-18 16:01:56 -0500 | [diff] [blame] | 1667 | } |
| 1668 | |
| 1669 | if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) { |
Ander Conselvan de Oliveira | df63b99 | 2015-04-10 14:58:39 +0300 | [diff] [blame] | 1670 | for_each_crtc_in_state(state, crtc, crtc_state, i) { |
Rob Clark | d34f20d | 2014-12-18 16:01:56 -0500 | [diff] [blame] | 1671 | struct drm_pending_vblank_event *e; |
| 1672 | |
Rob Clark | d34f20d | 2014-12-18 16:01:56 -0500 | [diff] [blame] | 1673 | e = create_vblank_event(dev, file_priv, arg->user_data); |
| 1674 | if (!e) { |
| 1675 | ret = -ENOMEM; |
Maarten Lankhorst | ec9f932 | 2015-06-24 08:59:25 +0200 | [diff] [blame] | 1676 | goto out; |
Rob Clark | d34f20d | 2014-12-18 16:01:56 -0500 | [diff] [blame] | 1677 | } |
| 1678 | |
| 1679 | crtc_state->event = e; |
| 1680 | } |
| 1681 | } |
| 1682 | |
| 1683 | if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) { |
Maarten Lankhorst | c4749c9 | 2015-08-31 12:25:04 +0200 | [diff] [blame] | 1684 | /* |
| 1685 | * Unlike commit, check_only does not clean up state. |
| 1686 | * Below we call drm_atomic_state_free for it. |
| 1687 | */ |
Rob Clark | d34f20d | 2014-12-18 16:01:56 -0500 | [diff] [blame] | 1688 | ret = drm_atomic_check_only(state); |
Rob Clark | d34f20d | 2014-12-18 16:01:56 -0500 | [diff] [blame] | 1689 | } else if (arg->flags & DRM_MODE_ATOMIC_NONBLOCK) { |
Maarten Lankhorst | b837ba0 | 2016-04-26 16:11:35 +0200 | [diff] [blame] | 1690 | ret = drm_atomic_nonblocking_commit(state); |
Rob Clark | d34f20d | 2014-12-18 16:01:56 -0500 | [diff] [blame] | 1691 | } else { |
| 1692 | ret = drm_atomic_commit(state); |
| 1693 | } |
| 1694 | |
Maarten Lankhorst | ec9f932 | 2015-06-24 08:59:25 +0200 | [diff] [blame] | 1695 | out: |
Maarten Lankhorst | 0f45c26 | 2015-11-11 11:29:09 +0100 | [diff] [blame] | 1696 | drm_atomic_clean_old_fb(dev, plane_mask, ret); |
Maarten Lankhorst | ec9f932 | 2015-06-24 08:59:25 +0200 | [diff] [blame] | 1697 | |
Maarten Lankhorst | c4749c9 | 2015-08-31 12:25:04 +0200 | [diff] [blame] | 1698 | if (ret && arg->flags & DRM_MODE_PAGE_FLIP_EVENT) { |
| 1699 | /* |
| 1700 | * TEST_ONLY and PAGE_FLIP_EVENT are mutually exclusive, |
| 1701 | * if they weren't, this code should be called on success |
| 1702 | * for TEST_ONLY too. |
| 1703 | */ |
| 1704 | |
| 1705 | for_each_crtc_in_state(state, crtc, crtc_state, i) { |
| 1706 | if (!crtc_state->event) |
| 1707 | continue; |
| 1708 | |
Daniel Vetter | 2dd500f | 2016-01-11 22:40:56 +0100 | [diff] [blame] | 1709 | drm_event_cancel_free(dev, &crtc_state->event->base); |
Maarten Lankhorst | c4749c9 | 2015-08-31 12:25:04 +0200 | [diff] [blame] | 1710 | } |
| 1711 | } |
| 1712 | |
Maarten Lankhorst | ec9f932 | 2015-06-24 08:59:25 +0200 | [diff] [blame] | 1713 | if (ret == -EDEADLK) { |
| 1714 | drm_atomic_state_clear(state); |
| 1715 | drm_modeset_backoff(&ctx); |
| 1716 | goto retry; |
| 1717 | } |
| 1718 | |
Maarten Lankhorst | c4749c9 | 2015-08-31 12:25:04 +0200 | [diff] [blame] | 1719 | if (ret || arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) |
Maarten Lankhorst | ec9f932 | 2015-06-24 08:59:25 +0200 | [diff] [blame] | 1720 | drm_atomic_state_free(state); |
Rob Clark | d34f20d | 2014-12-18 16:01:56 -0500 | [diff] [blame] | 1721 | |
| 1722 | drm_modeset_drop_locks(&ctx); |
| 1723 | drm_modeset_acquire_fini(&ctx); |
| 1724 | |
| 1725 | return ret; |
Rob Clark | d34f20d | 2014-12-18 16:01:56 -0500 | [diff] [blame] | 1726 | } |