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> |
| 31 | #include <drm/drm_plane_helper.h> |
| 32 | |
| 33 | static void kfree_state(struct drm_atomic_state *state) |
| 34 | { |
| 35 | kfree(state->connectors); |
| 36 | kfree(state->connector_states); |
| 37 | kfree(state->crtcs); |
| 38 | kfree(state->crtc_states); |
| 39 | kfree(state->planes); |
| 40 | kfree(state->plane_states); |
| 41 | kfree(state); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * drm_atomic_state_alloc - allocate atomic state |
| 46 | * @dev: DRM device |
| 47 | * |
| 48 | * This allocates an empty atomic state to track updates. |
| 49 | */ |
| 50 | struct drm_atomic_state * |
| 51 | drm_atomic_state_alloc(struct drm_device *dev) |
| 52 | { |
| 53 | struct drm_atomic_state *state; |
| 54 | |
| 55 | state = kzalloc(sizeof(*state), GFP_KERNEL); |
| 56 | if (!state) |
| 57 | return NULL; |
| 58 | |
Rob Clark | d34f20d | 2014-12-18 16:01:56 -0500 | [diff] [blame] | 59 | /* TODO legacy paths should maybe do a better job about |
| 60 | * setting this appropriately? |
| 61 | */ |
| 62 | state->allow_modeset = true; |
| 63 | |
Daniel Vetter | f52b69f1 | 2014-11-19 18:38:08 +0100 | [diff] [blame] | 64 | state->num_connector = ACCESS_ONCE(dev->mode_config.num_connector); |
| 65 | |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 66 | state->crtcs = kcalloc(dev->mode_config.num_crtc, |
| 67 | sizeof(*state->crtcs), GFP_KERNEL); |
| 68 | if (!state->crtcs) |
| 69 | goto fail; |
| 70 | state->crtc_states = kcalloc(dev->mode_config.num_crtc, |
| 71 | sizeof(*state->crtc_states), GFP_KERNEL); |
| 72 | if (!state->crtc_states) |
| 73 | goto fail; |
| 74 | state->planes = kcalloc(dev->mode_config.num_total_plane, |
| 75 | sizeof(*state->planes), GFP_KERNEL); |
| 76 | if (!state->planes) |
| 77 | goto fail; |
| 78 | state->plane_states = kcalloc(dev->mode_config.num_total_plane, |
| 79 | sizeof(*state->plane_states), GFP_KERNEL); |
| 80 | if (!state->plane_states) |
| 81 | goto fail; |
Daniel Vetter | f52b69f1 | 2014-11-19 18:38:08 +0100 | [diff] [blame] | 82 | state->connectors = kcalloc(state->num_connector, |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 83 | sizeof(*state->connectors), |
| 84 | GFP_KERNEL); |
| 85 | if (!state->connectors) |
| 86 | goto fail; |
Daniel Vetter | f52b69f1 | 2014-11-19 18:38:08 +0100 | [diff] [blame] | 87 | state->connector_states = kcalloc(state->num_connector, |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 88 | sizeof(*state->connector_states), |
| 89 | GFP_KERNEL); |
| 90 | if (!state->connector_states) |
| 91 | goto fail; |
| 92 | |
| 93 | state->dev = dev; |
| 94 | |
Daniel Vetter | 17a38d9 | 2015-02-22 12:24:16 +0100 | [diff] [blame] | 95 | DRM_DEBUG_ATOMIC("Allocate atomic state %p\n", state); |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 96 | |
| 97 | return state; |
| 98 | fail: |
| 99 | kfree_state(state); |
| 100 | |
| 101 | return NULL; |
| 102 | } |
| 103 | EXPORT_SYMBOL(drm_atomic_state_alloc); |
| 104 | |
| 105 | /** |
| 106 | * drm_atomic_state_clear - clear state object |
| 107 | * @state: atomic state |
| 108 | * |
| 109 | * When the w/w mutex algorithm detects a deadlock we need to back off and drop |
| 110 | * all locks. So someone else could sneak in and change the current modeset |
| 111 | * configuration. Which means that all the state assembled in @state is no |
| 112 | * longer an atomic update to the current state, but to some arbitrary earlier |
| 113 | * state. Which could break assumptions the driver's ->atomic_check likely |
| 114 | * relies on. |
| 115 | * |
| 116 | * Hence we must clear all cached state and completely start over, using this |
| 117 | * function. |
| 118 | */ |
| 119 | void drm_atomic_state_clear(struct drm_atomic_state *state) |
| 120 | { |
| 121 | struct drm_device *dev = state->dev; |
Daniel Vetter | 6f75cea | 2014-11-19 18:38:07 +0100 | [diff] [blame] | 122 | struct drm_mode_config *config = &dev->mode_config; |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 123 | int i; |
| 124 | |
Daniel Vetter | 17a38d9 | 2015-02-22 12:24:16 +0100 | [diff] [blame] | 125 | DRM_DEBUG_ATOMIC("Clearing atomic state %p\n", state); |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 126 | |
Daniel Vetter | f52b69f1 | 2014-11-19 18:38:08 +0100 | [diff] [blame] | 127 | for (i = 0; i < state->num_connector; i++) { |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 128 | struct drm_connector *connector = state->connectors[i]; |
| 129 | |
| 130 | if (!connector) |
| 131 | continue; |
| 132 | |
Daniel Vetter | 6f75cea | 2014-11-19 18:38:07 +0100 | [diff] [blame] | 133 | WARN_ON(!drm_modeset_is_locked(&config->connection_mutex)); |
| 134 | |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 135 | connector->funcs->atomic_destroy_state(connector, |
| 136 | state->connector_states[i]); |
Ander Conselvan de Oliveira | 8a5c0bd | 2015-03-30 10:41:19 +0300 | [diff] [blame] | 137 | state->connectors[i] = NULL; |
Ander Conselvan de Oliveira | 9469244 | 2015-01-23 09:27:59 +0200 | [diff] [blame] | 138 | state->connector_states[i] = NULL; |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 139 | } |
| 140 | |
Daniel Vetter | 6f75cea | 2014-11-19 18:38:07 +0100 | [diff] [blame] | 141 | for (i = 0; i < config->num_crtc; i++) { |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 142 | struct drm_crtc *crtc = state->crtcs[i]; |
| 143 | |
| 144 | if (!crtc) |
| 145 | continue; |
| 146 | |
| 147 | crtc->funcs->atomic_destroy_state(crtc, |
| 148 | state->crtc_states[i]); |
Ander Conselvan de Oliveira | 8a5c0bd | 2015-03-30 10:41:19 +0300 | [diff] [blame] | 149 | state->crtcs[i] = NULL; |
Ander Conselvan de Oliveira | 9469244 | 2015-01-23 09:27:59 +0200 | [diff] [blame] | 150 | state->crtc_states[i] = NULL; |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 151 | } |
| 152 | |
Daniel Vetter | 6f75cea | 2014-11-19 18:38:07 +0100 | [diff] [blame] | 153 | for (i = 0; i < config->num_total_plane; i++) { |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 154 | struct drm_plane *plane = state->planes[i]; |
| 155 | |
| 156 | if (!plane) |
| 157 | continue; |
| 158 | |
| 159 | plane->funcs->atomic_destroy_state(plane, |
| 160 | state->plane_states[i]); |
Ander Conselvan de Oliveira | 8a5c0bd | 2015-03-30 10:41:19 +0300 | [diff] [blame] | 161 | state->planes[i] = NULL; |
Ander Conselvan de Oliveira | 9469244 | 2015-01-23 09:27:59 +0200 | [diff] [blame] | 162 | state->plane_states[i] = NULL; |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | EXPORT_SYMBOL(drm_atomic_state_clear); |
| 166 | |
| 167 | /** |
| 168 | * drm_atomic_state_free - free all memory for an atomic state |
| 169 | * @state: atomic state to deallocate |
| 170 | * |
| 171 | * This frees all memory associated with an atomic state, including all the |
| 172 | * per-object state for planes, crtcs and connectors. |
| 173 | */ |
| 174 | void drm_atomic_state_free(struct drm_atomic_state *state) |
| 175 | { |
Ander Conselvan de Oliveira | a0211bb | 2015-03-30 14:05:43 +0300 | [diff] [blame] | 176 | if (!state) |
| 177 | return; |
| 178 | |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 179 | drm_atomic_state_clear(state); |
| 180 | |
Daniel Vetter | 17a38d9 | 2015-02-22 12:24:16 +0100 | [diff] [blame] | 181 | DRM_DEBUG_ATOMIC("Freeing atomic state %p\n", state); |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 182 | |
| 183 | kfree_state(state); |
| 184 | } |
| 185 | EXPORT_SYMBOL(drm_atomic_state_free); |
| 186 | |
| 187 | /** |
| 188 | * drm_atomic_get_crtc_state - get crtc state |
| 189 | * @state: global atomic state object |
| 190 | * @crtc: crtc to get state object for |
| 191 | * |
| 192 | * This function returns the crtc state for the given crtc, allocating it if |
| 193 | * needed. It will also grab the relevant crtc lock to make sure that the state |
| 194 | * is consistent. |
| 195 | * |
| 196 | * Returns: |
| 197 | * |
| 198 | * Either the allocated state or the error code encoded into the pointer. When |
| 199 | * the error is EDEADLK then the w/w mutex code has detected a deadlock and the |
| 200 | * entire atomic sequence must be restarted. All other errors are fatal. |
| 201 | */ |
| 202 | struct drm_crtc_state * |
| 203 | drm_atomic_get_crtc_state(struct drm_atomic_state *state, |
| 204 | struct drm_crtc *crtc) |
| 205 | { |
Maarten Lankhorst | 1b26a5e | 2015-05-13 10:37:25 +0200 | [diff] [blame^] | 206 | int ret, index = drm_crtc_index(crtc); |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 207 | struct drm_crtc_state *crtc_state; |
| 208 | |
Maarten Lankhorst | 1b26a5e | 2015-05-13 10:37:25 +0200 | [diff] [blame^] | 209 | crtc_state = drm_atomic_get_existing_crtc_state(state, crtc); |
| 210 | if (crtc_state) |
| 211 | return crtc_state; |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 212 | |
| 213 | ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx); |
| 214 | if (ret) |
| 215 | return ERR_PTR(ret); |
| 216 | |
| 217 | crtc_state = crtc->funcs->atomic_duplicate_state(crtc); |
| 218 | if (!crtc_state) |
| 219 | return ERR_PTR(-ENOMEM); |
| 220 | |
| 221 | state->crtc_states[index] = crtc_state; |
| 222 | state->crtcs[index] = crtc; |
| 223 | crtc_state->state = state; |
| 224 | |
Daniel Vetter | 17a38d9 | 2015-02-22 12:24:16 +0100 | [diff] [blame] | 225 | DRM_DEBUG_ATOMIC("Added [CRTC:%d] %p state to %p\n", |
| 226 | crtc->base.id, crtc_state, state); |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 227 | |
| 228 | return crtc_state; |
| 229 | } |
| 230 | EXPORT_SYMBOL(drm_atomic_get_crtc_state); |
| 231 | |
| 232 | /** |
Rob Clark | 40ecc69 | 2014-12-18 16:01:46 -0500 | [diff] [blame] | 233 | * drm_atomic_crtc_set_property - set property on CRTC |
| 234 | * @crtc: the drm CRTC to set a property on |
| 235 | * @state: the state object to update with the new property value |
| 236 | * @property: the property to set |
| 237 | * @val: the new property value |
| 238 | * |
| 239 | * Use this instead of calling crtc->atomic_set_property directly. |
| 240 | * This function handles generic/core properties and calls out to |
| 241 | * driver's ->atomic_set_property() for driver properties. To ensure |
| 242 | * consistent behavior you must call this function rather than the |
| 243 | * driver hook directly. |
| 244 | * |
| 245 | * RETURNS: |
| 246 | * Zero on success, error code on failure |
| 247 | */ |
| 248 | int drm_atomic_crtc_set_property(struct drm_crtc *crtc, |
| 249 | struct drm_crtc_state *state, struct drm_property *property, |
| 250 | uint64_t val) |
| 251 | { |
Daniel Vetter | eab3bbe | 2015-01-22 16:36:21 +0100 | [diff] [blame] | 252 | struct drm_device *dev = crtc->dev; |
| 253 | struct drm_mode_config *config = &dev->mode_config; |
| 254 | |
| 255 | /* FIXME: Mode prop is missing, which also controls ->enable. */ |
Daniel Stone | 2779836 | 2015-03-19 04:33:26 +0000 | [diff] [blame] | 256 | if (property == config->prop_active) |
Daniel Vetter | eab3bbe | 2015-01-22 16:36:21 +0100 | [diff] [blame] | 257 | state->active = val; |
Daniel Stone | 2779836 | 2015-03-19 04:33:26 +0000 | [diff] [blame] | 258 | else if (crtc->funcs->atomic_set_property) |
Rob Clark | 40ecc69 | 2014-12-18 16:01:46 -0500 | [diff] [blame] | 259 | return crtc->funcs->atomic_set_property(crtc, state, property, val); |
Daniel Stone | 2779836 | 2015-03-19 04:33:26 +0000 | [diff] [blame] | 260 | else |
| 261 | return -EINVAL; |
| 262 | |
| 263 | return 0; |
Rob Clark | 40ecc69 | 2014-12-18 16:01:46 -0500 | [diff] [blame] | 264 | } |
| 265 | EXPORT_SYMBOL(drm_atomic_crtc_set_property); |
| 266 | |
Daniel Vetter | a97df1c | 2014-12-18 22:49:02 +0100 | [diff] [blame] | 267 | /* |
Rob Clark | ac9c925 | 2014-12-18 16:01:47 -0500 | [diff] [blame] | 268 | * This function handles generic/core properties and calls out to |
| 269 | * driver's ->atomic_get_property() for driver properties. To ensure |
| 270 | * consistent behavior you must call this function rather than the |
| 271 | * driver hook directly. |
Rob Clark | ac9c925 | 2014-12-18 16:01:47 -0500 | [diff] [blame] | 272 | */ |
| 273 | int drm_atomic_crtc_get_property(struct drm_crtc *crtc, |
| 274 | const struct drm_crtc_state *state, |
| 275 | struct drm_property *property, uint64_t *val) |
| 276 | { |
Daniel Stone | 8f164ce | 2015-03-19 04:33:25 +0000 | [diff] [blame] | 277 | struct drm_device *dev = crtc->dev; |
| 278 | struct drm_mode_config *config = &dev->mode_config; |
| 279 | |
| 280 | if (property == config->prop_active) |
| 281 | *val = state->active; |
| 282 | else if (crtc->funcs->atomic_get_property) |
Rob Clark | ac9c925 | 2014-12-18 16:01:47 -0500 | [diff] [blame] | 283 | return crtc->funcs->atomic_get_property(crtc, state, property, val); |
Daniel Stone | 8f164ce | 2015-03-19 04:33:25 +0000 | [diff] [blame] | 284 | else |
| 285 | return -EINVAL; |
| 286 | |
| 287 | return 0; |
Rob Clark | ac9c925 | 2014-12-18 16:01:47 -0500 | [diff] [blame] | 288 | } |
Rob Clark | ac9c925 | 2014-12-18 16:01:47 -0500 | [diff] [blame] | 289 | |
| 290 | /** |
Rob Clark | 5e74373 | 2014-12-18 16:01:51 -0500 | [diff] [blame] | 291 | * drm_atomic_crtc_check - check crtc state |
| 292 | * @crtc: crtc to check |
| 293 | * @state: crtc state to check |
| 294 | * |
| 295 | * Provides core sanity checks for crtc state. |
| 296 | * |
| 297 | * RETURNS: |
| 298 | * Zero on success, error code on failure |
| 299 | */ |
| 300 | static int drm_atomic_crtc_check(struct drm_crtc *crtc, |
| 301 | struct drm_crtc_state *state) |
| 302 | { |
| 303 | /* NOTE: we explicitly don't enforce constraints such as primary |
| 304 | * layer covering entire screen, since that is something we want |
| 305 | * to allow (on hw that supports it). For hw that does not, it |
| 306 | * should be checked in driver's crtc->atomic_check() vfunc. |
| 307 | * |
| 308 | * TODO: Add generic modeset state checks once we support those. |
| 309 | */ |
Daniel Vetter | eab3bbe | 2015-01-22 16:36:21 +0100 | [diff] [blame] | 310 | |
| 311 | if (state->active && !state->enable) { |
Daniel Vetter | 17a38d9 | 2015-02-22 12:24:16 +0100 | [diff] [blame] | 312 | DRM_DEBUG_ATOMIC("[CRTC:%d] active without enabled\n", |
| 313 | crtc->base.id); |
Daniel Vetter | eab3bbe | 2015-01-22 16:36:21 +0100 | [diff] [blame] | 314 | return -EINVAL; |
| 315 | } |
| 316 | |
Rob Clark | 5e74373 | 2014-12-18 16:01:51 -0500 | [diff] [blame] | 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | /** |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 321 | * drm_atomic_get_plane_state - get plane state |
| 322 | * @state: global atomic state object |
| 323 | * @plane: plane to get state object for |
| 324 | * |
| 325 | * This function returns the plane state for the given plane, allocating it if |
| 326 | * needed. It will also grab the relevant plane lock to make sure that the state |
| 327 | * is consistent. |
| 328 | * |
| 329 | * Returns: |
| 330 | * |
| 331 | * Either the allocated state or the error code encoded into the pointer. When |
| 332 | * the error is EDEADLK then the w/w mutex code has detected a deadlock and the |
| 333 | * entire atomic sequence must be restarted. All other errors are fatal. |
| 334 | */ |
| 335 | struct drm_plane_state * |
| 336 | drm_atomic_get_plane_state(struct drm_atomic_state *state, |
| 337 | struct drm_plane *plane) |
| 338 | { |
Maarten Lankhorst | 1b26a5e | 2015-05-13 10:37:25 +0200 | [diff] [blame^] | 339 | int ret, index = drm_plane_index(plane); |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 340 | struct drm_plane_state *plane_state; |
| 341 | |
Maarten Lankhorst | 1b26a5e | 2015-05-13 10:37:25 +0200 | [diff] [blame^] | 342 | plane_state = drm_atomic_get_existing_plane_state(state, plane); |
| 343 | if (plane_state) |
| 344 | return plane_state; |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 345 | |
Daniel Vetter | 4d02e2d | 2014-11-11 10:12:00 +0100 | [diff] [blame] | 346 | ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx); |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 347 | if (ret) |
| 348 | return ERR_PTR(ret); |
| 349 | |
| 350 | plane_state = plane->funcs->atomic_duplicate_state(plane); |
| 351 | if (!plane_state) |
| 352 | return ERR_PTR(-ENOMEM); |
| 353 | |
| 354 | state->plane_states[index] = plane_state; |
| 355 | state->planes[index] = plane; |
| 356 | plane_state->state = state; |
| 357 | |
Daniel Vetter | 17a38d9 | 2015-02-22 12:24:16 +0100 | [diff] [blame] | 358 | DRM_DEBUG_ATOMIC("Added [PLANE:%d] %p state to %p\n", |
| 359 | plane->base.id, plane_state, state); |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 360 | |
| 361 | if (plane_state->crtc) { |
| 362 | struct drm_crtc_state *crtc_state; |
| 363 | |
| 364 | crtc_state = drm_atomic_get_crtc_state(state, |
| 365 | plane_state->crtc); |
| 366 | if (IS_ERR(crtc_state)) |
| 367 | return ERR_CAST(crtc_state); |
| 368 | } |
| 369 | |
| 370 | return plane_state; |
| 371 | } |
| 372 | EXPORT_SYMBOL(drm_atomic_get_plane_state); |
| 373 | |
| 374 | /** |
Rob Clark | 40ecc69 | 2014-12-18 16:01:46 -0500 | [diff] [blame] | 375 | * drm_atomic_plane_set_property - set property on plane |
| 376 | * @plane: the drm plane to set a property on |
| 377 | * @state: the state object to update with the new property value |
| 378 | * @property: the property to set |
| 379 | * @val: the new property value |
| 380 | * |
| 381 | * Use this instead of calling plane->atomic_set_property directly. |
| 382 | * This function handles generic/core properties and calls out to |
| 383 | * driver's ->atomic_set_property() for driver properties. To ensure |
| 384 | * consistent behavior you must call this function rather than the |
| 385 | * driver hook directly. |
| 386 | * |
| 387 | * RETURNS: |
| 388 | * Zero on success, error code on failure |
| 389 | */ |
| 390 | int drm_atomic_plane_set_property(struct drm_plane *plane, |
| 391 | struct drm_plane_state *state, struct drm_property *property, |
| 392 | uint64_t val) |
| 393 | { |
Rob Clark | 6b4959f | 2014-12-18 16:01:53 -0500 | [diff] [blame] | 394 | struct drm_device *dev = plane->dev; |
| 395 | struct drm_mode_config *config = &dev->mode_config; |
| 396 | |
| 397 | if (property == config->prop_fb_id) { |
| 398 | struct drm_framebuffer *fb = drm_framebuffer_lookup(dev, val); |
| 399 | drm_atomic_set_fb_for_plane(state, fb); |
| 400 | if (fb) |
| 401 | drm_framebuffer_unreference(fb); |
| 402 | } else if (property == config->prop_crtc_id) { |
| 403 | struct drm_crtc *crtc = drm_crtc_find(dev, val); |
| 404 | return drm_atomic_set_crtc_for_plane(state, crtc); |
| 405 | } else if (property == config->prop_crtc_x) { |
| 406 | state->crtc_x = U642I64(val); |
| 407 | } else if (property == config->prop_crtc_y) { |
| 408 | state->crtc_y = U642I64(val); |
| 409 | } else if (property == config->prop_crtc_w) { |
| 410 | state->crtc_w = val; |
| 411 | } else if (property == config->prop_crtc_h) { |
| 412 | state->crtc_h = val; |
| 413 | } else if (property == config->prop_src_x) { |
| 414 | state->src_x = val; |
| 415 | } else if (property == config->prop_src_y) { |
| 416 | state->src_y = val; |
| 417 | } else if (property == config->prop_src_w) { |
| 418 | state->src_w = val; |
| 419 | } else if (property == config->prop_src_h) { |
| 420 | state->src_h = val; |
Matt Roper | 1da3062 | 2015-01-21 16:35:40 -0800 | [diff] [blame] | 421 | } else if (property == config->rotation_property) { |
| 422 | state->rotation = val; |
Rob Clark | 6b4959f | 2014-12-18 16:01:53 -0500 | [diff] [blame] | 423 | } else if (plane->funcs->atomic_set_property) { |
| 424 | return plane->funcs->atomic_set_property(plane, state, |
| 425 | property, val); |
| 426 | } else { |
| 427 | return -EINVAL; |
| 428 | } |
| 429 | |
| 430 | return 0; |
Rob Clark | 40ecc69 | 2014-12-18 16:01:46 -0500 | [diff] [blame] | 431 | } |
| 432 | EXPORT_SYMBOL(drm_atomic_plane_set_property); |
| 433 | |
Daniel Vetter | a97df1c | 2014-12-18 22:49:02 +0100 | [diff] [blame] | 434 | /* |
Rob Clark | ac9c925 | 2014-12-18 16:01:47 -0500 | [diff] [blame] | 435 | * This function handles generic/core properties and calls out to |
| 436 | * driver's ->atomic_get_property() for driver properties. To ensure |
| 437 | * consistent behavior you must call this function rather than the |
| 438 | * driver hook directly. |
Rob Clark | ac9c925 | 2014-12-18 16:01:47 -0500 | [diff] [blame] | 439 | */ |
Daniel Vetter | a97df1c | 2014-12-18 22:49:02 +0100 | [diff] [blame] | 440 | static int |
| 441 | drm_atomic_plane_get_property(struct drm_plane *plane, |
Rob Clark | ac9c925 | 2014-12-18 16:01:47 -0500 | [diff] [blame] | 442 | const struct drm_plane_state *state, |
| 443 | struct drm_property *property, uint64_t *val) |
| 444 | { |
Rob Clark | 6b4959f | 2014-12-18 16:01:53 -0500 | [diff] [blame] | 445 | struct drm_device *dev = plane->dev; |
| 446 | struct drm_mode_config *config = &dev->mode_config; |
| 447 | |
| 448 | if (property == config->prop_fb_id) { |
| 449 | *val = (state->fb) ? state->fb->base.id : 0; |
| 450 | } else if (property == config->prop_crtc_id) { |
| 451 | *val = (state->crtc) ? state->crtc->base.id : 0; |
| 452 | } else if (property == config->prop_crtc_x) { |
| 453 | *val = I642U64(state->crtc_x); |
| 454 | } else if (property == config->prop_crtc_y) { |
| 455 | *val = I642U64(state->crtc_y); |
| 456 | } else if (property == config->prop_crtc_w) { |
| 457 | *val = state->crtc_w; |
| 458 | } else if (property == config->prop_crtc_h) { |
| 459 | *val = state->crtc_h; |
| 460 | } else if (property == config->prop_src_x) { |
| 461 | *val = state->src_x; |
| 462 | } else if (property == config->prop_src_y) { |
| 463 | *val = state->src_y; |
| 464 | } else if (property == config->prop_src_w) { |
| 465 | *val = state->src_w; |
| 466 | } else if (property == config->prop_src_h) { |
| 467 | *val = state->src_h; |
Tvrtko Ursulin | 4cda09c | 2015-02-26 13:49:17 +0000 | [diff] [blame] | 468 | } else if (property == config->rotation_property) { |
| 469 | *val = state->rotation; |
Rob Clark | 6b4959f | 2014-12-18 16:01:53 -0500 | [diff] [blame] | 470 | } else if (plane->funcs->atomic_get_property) { |
Rob Clark | ac9c925 | 2014-12-18 16:01:47 -0500 | [diff] [blame] | 471 | return plane->funcs->atomic_get_property(plane, state, property, val); |
Rob Clark | 6b4959f | 2014-12-18 16:01:53 -0500 | [diff] [blame] | 472 | } else { |
| 473 | return -EINVAL; |
| 474 | } |
| 475 | |
| 476 | return 0; |
Rob Clark | ac9c925 | 2014-12-18 16:01:47 -0500 | [diff] [blame] | 477 | } |
Rob Clark | ac9c925 | 2014-12-18 16:01:47 -0500 | [diff] [blame] | 478 | |
| 479 | /** |
Rob Clark | 5e74373 | 2014-12-18 16:01:51 -0500 | [diff] [blame] | 480 | * drm_atomic_plane_check - check plane state |
| 481 | * @plane: plane to check |
| 482 | * @state: plane state to check |
| 483 | * |
| 484 | * Provides core sanity checks for plane state. |
| 485 | * |
| 486 | * RETURNS: |
| 487 | * Zero on success, error code on failure |
| 488 | */ |
| 489 | static int drm_atomic_plane_check(struct drm_plane *plane, |
| 490 | struct drm_plane_state *state) |
| 491 | { |
| 492 | unsigned int fb_width, fb_height; |
Laurent Pinchart | ead8610 | 2015-03-05 02:25:43 +0200 | [diff] [blame] | 493 | int ret; |
Rob Clark | 5e74373 | 2014-12-18 16:01:51 -0500 | [diff] [blame] | 494 | |
| 495 | /* either *both* CRTC and FB must be set, or neither */ |
| 496 | if (WARN_ON(state->crtc && !state->fb)) { |
Daniel Vetter | 17a38d9 | 2015-02-22 12:24:16 +0100 | [diff] [blame] | 497 | DRM_DEBUG_ATOMIC("CRTC set but no FB\n"); |
Rob Clark | 5e74373 | 2014-12-18 16:01:51 -0500 | [diff] [blame] | 498 | return -EINVAL; |
| 499 | } else if (WARN_ON(state->fb && !state->crtc)) { |
Daniel Vetter | 17a38d9 | 2015-02-22 12:24:16 +0100 | [diff] [blame] | 500 | DRM_DEBUG_ATOMIC("FB set but no CRTC\n"); |
Rob Clark | 5e74373 | 2014-12-18 16:01:51 -0500 | [diff] [blame] | 501 | return -EINVAL; |
| 502 | } |
| 503 | |
| 504 | /* if disabled, we don't care about the rest of the state: */ |
| 505 | if (!state->crtc) |
| 506 | return 0; |
| 507 | |
| 508 | /* Check whether this plane is usable on this CRTC */ |
| 509 | if (!(plane->possible_crtcs & drm_crtc_mask(state->crtc))) { |
Daniel Vetter | 17a38d9 | 2015-02-22 12:24:16 +0100 | [diff] [blame] | 510 | DRM_DEBUG_ATOMIC("Invalid crtc for plane\n"); |
Rob Clark | 5e74373 | 2014-12-18 16:01:51 -0500 | [diff] [blame] | 511 | return -EINVAL; |
| 512 | } |
| 513 | |
| 514 | /* Check whether this plane supports the fb pixel format. */ |
Laurent Pinchart | ead8610 | 2015-03-05 02:25:43 +0200 | [diff] [blame] | 515 | ret = drm_plane_check_pixel_format(plane, state->fb->pixel_format); |
| 516 | if (ret) { |
Daniel Vetter | 17a38d9 | 2015-02-22 12:24:16 +0100 | [diff] [blame] | 517 | DRM_DEBUG_ATOMIC("Invalid pixel format %s\n", |
| 518 | drm_get_format_name(state->fb->pixel_format)); |
Laurent Pinchart | ead8610 | 2015-03-05 02:25:43 +0200 | [diff] [blame] | 519 | return ret; |
Rob Clark | 5e74373 | 2014-12-18 16:01:51 -0500 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | /* Give drivers some help against integer overflows */ |
| 523 | if (state->crtc_w > INT_MAX || |
| 524 | state->crtc_x > INT_MAX - (int32_t) state->crtc_w || |
| 525 | state->crtc_h > INT_MAX || |
| 526 | state->crtc_y > INT_MAX - (int32_t) state->crtc_h) { |
Daniel Vetter | 17a38d9 | 2015-02-22 12:24:16 +0100 | [diff] [blame] | 527 | DRM_DEBUG_ATOMIC("Invalid CRTC coordinates %ux%u+%d+%d\n", |
| 528 | state->crtc_w, state->crtc_h, |
| 529 | state->crtc_x, state->crtc_y); |
Rob Clark | 5e74373 | 2014-12-18 16:01:51 -0500 | [diff] [blame] | 530 | return -ERANGE; |
| 531 | } |
| 532 | |
| 533 | fb_width = state->fb->width << 16; |
| 534 | fb_height = state->fb->height << 16; |
| 535 | |
| 536 | /* Make sure source coordinates are inside the fb. */ |
| 537 | if (state->src_w > fb_width || |
| 538 | state->src_x > fb_width - state->src_w || |
| 539 | state->src_h > fb_height || |
| 540 | state->src_y > fb_height - state->src_h) { |
Daniel Vetter | 17a38d9 | 2015-02-22 12:24:16 +0100 | [diff] [blame] | 541 | DRM_DEBUG_ATOMIC("Invalid source coordinates " |
| 542 | "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n", |
| 543 | state->src_w >> 16, ((state->src_w & 0xffff) * 15625) >> 10, |
| 544 | state->src_h >> 16, ((state->src_h & 0xffff) * 15625) >> 10, |
| 545 | state->src_x >> 16, ((state->src_x & 0xffff) * 15625) >> 10, |
| 546 | state->src_y >> 16, ((state->src_y & 0xffff) * 15625) >> 10); |
Rob Clark | 5e74373 | 2014-12-18 16:01:51 -0500 | [diff] [blame] | 547 | return -ENOSPC; |
| 548 | } |
| 549 | |
| 550 | return 0; |
| 551 | } |
| 552 | |
| 553 | /** |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 554 | * drm_atomic_get_connector_state - get connector state |
| 555 | * @state: global atomic state object |
| 556 | * @connector: connector to get state object for |
| 557 | * |
| 558 | * This function returns the connector state for the given connector, |
| 559 | * allocating it if needed. It will also grab the relevant connector lock to |
| 560 | * make sure that the state is consistent. |
| 561 | * |
| 562 | * Returns: |
| 563 | * |
| 564 | * Either the allocated state or the error code encoded into the pointer. When |
| 565 | * the error is EDEADLK then the w/w mutex code has detected a deadlock and the |
| 566 | * entire atomic sequence must be restarted. All other errors are fatal. |
| 567 | */ |
| 568 | struct drm_connector_state * |
| 569 | drm_atomic_get_connector_state(struct drm_atomic_state *state, |
| 570 | struct drm_connector *connector) |
| 571 | { |
| 572 | int ret, index; |
| 573 | struct drm_mode_config *config = &connector->dev->mode_config; |
| 574 | struct drm_connector_state *connector_state; |
| 575 | |
Daniel Vetter | c7eb76f | 2014-11-19 18:38:06 +0100 | [diff] [blame] | 576 | ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx); |
| 577 | if (ret) |
| 578 | return ERR_PTR(ret); |
| 579 | |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 580 | index = drm_connector_index(connector); |
| 581 | |
Daniel Vetter | f52b69f1 | 2014-11-19 18:38:08 +0100 | [diff] [blame] | 582 | /* |
| 583 | * Construction of atomic state updates can race with a connector |
| 584 | * hot-add which might overflow. In this case flip the table and just |
| 585 | * restart the entire ioctl - no one is fast enough to livelock a cpu |
| 586 | * with physical hotplug events anyway. |
| 587 | * |
| 588 | * Note that we only grab the indexes once we have the right lock to |
| 589 | * prevent hotplug/unplugging of connectors. So removal is no problem, |
| 590 | * at most the array is a bit too large. |
| 591 | */ |
| 592 | if (index >= state->num_connector) { |
Daniel Vetter | 17a38d9 | 2015-02-22 12:24:16 +0100 | [diff] [blame] | 593 | DRM_DEBUG_ATOMIC("Hot-added connector would overflow state array, restarting\n"); |
Daniel Vetter | fc2d2bc | 2014-11-20 09:53:35 +0100 | [diff] [blame] | 594 | return ERR_PTR(-EAGAIN); |
Daniel Vetter | f52b69f1 | 2014-11-19 18:38:08 +0100 | [diff] [blame] | 595 | } |
| 596 | |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 597 | if (state->connector_states[index]) |
| 598 | return state->connector_states[index]; |
| 599 | |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 600 | connector_state = connector->funcs->atomic_duplicate_state(connector); |
| 601 | if (!connector_state) |
| 602 | return ERR_PTR(-ENOMEM); |
| 603 | |
| 604 | state->connector_states[index] = connector_state; |
| 605 | state->connectors[index] = connector; |
| 606 | connector_state->state = state; |
| 607 | |
Daniel Vetter | 17a38d9 | 2015-02-22 12:24:16 +0100 | [diff] [blame] | 608 | DRM_DEBUG_ATOMIC("Added [CONNECTOR:%d] %p state to %p\n", |
| 609 | connector->base.id, connector_state, state); |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 610 | |
| 611 | if (connector_state->crtc) { |
| 612 | struct drm_crtc_state *crtc_state; |
| 613 | |
| 614 | crtc_state = drm_atomic_get_crtc_state(state, |
| 615 | connector_state->crtc); |
| 616 | if (IS_ERR(crtc_state)) |
| 617 | return ERR_CAST(crtc_state); |
| 618 | } |
| 619 | |
| 620 | return connector_state; |
| 621 | } |
| 622 | EXPORT_SYMBOL(drm_atomic_get_connector_state); |
| 623 | |
| 624 | /** |
Rob Clark | 40ecc69 | 2014-12-18 16:01:46 -0500 | [diff] [blame] | 625 | * drm_atomic_connector_set_property - set property on connector. |
| 626 | * @connector: the drm connector to set a property on |
| 627 | * @state: the state object to update with the new property value |
| 628 | * @property: the property to set |
| 629 | * @val: the new property value |
| 630 | * |
| 631 | * Use this instead of calling connector->atomic_set_property directly. |
| 632 | * This function handles generic/core properties and calls out to |
| 633 | * driver's ->atomic_set_property() for driver properties. To ensure |
| 634 | * consistent behavior you must call this function rather than the |
| 635 | * driver hook directly. |
| 636 | * |
| 637 | * RETURNS: |
| 638 | * Zero on success, error code on failure |
| 639 | */ |
| 640 | int drm_atomic_connector_set_property(struct drm_connector *connector, |
| 641 | struct drm_connector_state *state, struct drm_property *property, |
| 642 | uint64_t val) |
| 643 | { |
| 644 | struct drm_device *dev = connector->dev; |
| 645 | struct drm_mode_config *config = &dev->mode_config; |
| 646 | |
Rob Clark | ae16c59 | 2014-12-18 16:01:54 -0500 | [diff] [blame] | 647 | if (property == config->prop_crtc_id) { |
| 648 | struct drm_crtc *crtc = drm_crtc_find(dev, val); |
| 649 | return drm_atomic_set_crtc_for_connector(state, crtc); |
| 650 | } else if (property == config->dpms_property) { |
Rob Clark | 40ecc69 | 2014-12-18 16:01:46 -0500 | [diff] [blame] | 651 | /* setting DPMS property requires special handling, which |
| 652 | * is done in legacy setprop path for us. Disallow (for |
| 653 | * now?) atomic writes to DPMS property: |
| 654 | */ |
| 655 | return -EINVAL; |
| 656 | } else if (connector->funcs->atomic_set_property) { |
| 657 | return connector->funcs->atomic_set_property(connector, |
| 658 | state, property, val); |
| 659 | } else { |
| 660 | return -EINVAL; |
| 661 | } |
| 662 | } |
| 663 | EXPORT_SYMBOL(drm_atomic_connector_set_property); |
| 664 | |
Daniel Vetter | a97df1c | 2014-12-18 22:49:02 +0100 | [diff] [blame] | 665 | /* |
Rob Clark | ac9c925 | 2014-12-18 16:01:47 -0500 | [diff] [blame] | 666 | * This function handles generic/core properties and calls out to |
| 667 | * driver's ->atomic_get_property() for driver properties. To ensure |
| 668 | * consistent behavior you must call this function rather than the |
| 669 | * driver hook directly. |
Rob Clark | ac9c925 | 2014-12-18 16:01:47 -0500 | [diff] [blame] | 670 | */ |
Daniel Vetter | a97df1c | 2014-12-18 22:49:02 +0100 | [diff] [blame] | 671 | static int |
| 672 | drm_atomic_connector_get_property(struct drm_connector *connector, |
Rob Clark | ac9c925 | 2014-12-18 16:01:47 -0500 | [diff] [blame] | 673 | const struct drm_connector_state *state, |
| 674 | struct drm_property *property, uint64_t *val) |
| 675 | { |
| 676 | struct drm_device *dev = connector->dev; |
| 677 | struct drm_mode_config *config = &dev->mode_config; |
| 678 | |
Rob Clark | ae16c59 | 2014-12-18 16:01:54 -0500 | [diff] [blame] | 679 | if (property == config->prop_crtc_id) { |
| 680 | *val = (state->crtc) ? state->crtc->base.id : 0; |
| 681 | } else if (property == config->dpms_property) { |
Rob Clark | ac9c925 | 2014-12-18 16:01:47 -0500 | [diff] [blame] | 682 | *val = connector->dpms; |
| 683 | } else if (connector->funcs->atomic_get_property) { |
| 684 | return connector->funcs->atomic_get_property(connector, |
| 685 | state, property, val); |
| 686 | } else { |
| 687 | return -EINVAL; |
| 688 | } |
| 689 | |
| 690 | return 0; |
| 691 | } |
Rob Clark | ac9c925 | 2014-12-18 16:01:47 -0500 | [diff] [blame] | 692 | |
Rob Clark | 88a48e2 | 2014-12-18 16:01:50 -0500 | [diff] [blame] | 693 | int drm_atomic_get_property(struct drm_mode_object *obj, |
| 694 | struct drm_property *property, uint64_t *val) |
| 695 | { |
| 696 | struct drm_device *dev = property->dev; |
| 697 | int ret; |
| 698 | |
| 699 | switch (obj->type) { |
| 700 | case DRM_MODE_OBJECT_CONNECTOR: { |
| 701 | struct drm_connector *connector = obj_to_connector(obj); |
| 702 | WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex)); |
| 703 | ret = drm_atomic_connector_get_property(connector, |
| 704 | connector->state, property, val); |
| 705 | break; |
| 706 | } |
| 707 | case DRM_MODE_OBJECT_CRTC: { |
| 708 | struct drm_crtc *crtc = obj_to_crtc(obj); |
| 709 | WARN_ON(!drm_modeset_is_locked(&crtc->mutex)); |
| 710 | ret = drm_atomic_crtc_get_property(crtc, |
| 711 | crtc->state, property, val); |
| 712 | break; |
| 713 | } |
| 714 | case DRM_MODE_OBJECT_PLANE: { |
| 715 | struct drm_plane *plane = obj_to_plane(obj); |
| 716 | WARN_ON(!drm_modeset_is_locked(&plane->mutex)); |
| 717 | ret = drm_atomic_plane_get_property(plane, |
| 718 | plane->state, property, val); |
| 719 | break; |
| 720 | } |
| 721 | default: |
| 722 | ret = -EINVAL; |
| 723 | break; |
| 724 | } |
| 725 | |
| 726 | return ret; |
| 727 | } |
| 728 | |
| 729 | /** |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 730 | * drm_atomic_set_crtc_for_plane - set crtc for plane |
Daniel Vetter | 07cc0ef | 2014-11-27 15:49:39 +0100 | [diff] [blame] | 731 | * @plane_state: the plane whose incoming state to update |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 732 | * @crtc: crtc to use for the plane |
| 733 | * |
| 734 | * Changing the assigned crtc for a plane requires us to grab the lock and state |
| 735 | * for the new crtc, as needed. This function takes care of all these details |
| 736 | * besides updating the pointer in the state object itself. |
| 737 | * |
| 738 | * Returns: |
| 739 | * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK |
| 740 | * then the w/w mutex code has detected a deadlock and the entire atomic |
| 741 | * sequence must be restarted. All other errors are fatal. |
| 742 | */ |
| 743 | int |
Daniel Vetter | 07cc0ef | 2014-11-27 15:49:39 +0100 | [diff] [blame] | 744 | drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state, |
| 745 | struct drm_crtc *crtc) |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 746 | { |
Daniel Vetter | 07cc0ef | 2014-11-27 15:49:39 +0100 | [diff] [blame] | 747 | struct drm_plane *plane = plane_state->plane; |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 748 | struct drm_crtc_state *crtc_state; |
| 749 | |
Rob Clark | 6ddd388 | 2014-11-21 15:28:31 -0500 | [diff] [blame] | 750 | if (plane_state->crtc) { |
| 751 | crtc_state = drm_atomic_get_crtc_state(plane_state->state, |
| 752 | plane_state->crtc); |
| 753 | if (WARN_ON(IS_ERR(crtc_state))) |
| 754 | return PTR_ERR(crtc_state); |
| 755 | |
| 756 | crtc_state->plane_mask &= ~(1 << drm_plane_index(plane)); |
| 757 | } |
| 758 | |
| 759 | plane_state->crtc = crtc; |
| 760 | |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 761 | if (crtc) { |
| 762 | crtc_state = drm_atomic_get_crtc_state(plane_state->state, |
| 763 | crtc); |
| 764 | if (IS_ERR(crtc_state)) |
| 765 | return PTR_ERR(crtc_state); |
Rob Clark | 6ddd388 | 2014-11-21 15:28:31 -0500 | [diff] [blame] | 766 | crtc_state->plane_mask |= (1 << drm_plane_index(plane)); |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 767 | } |
| 768 | |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 769 | if (crtc) |
Daniel Vetter | 17a38d9 | 2015-02-22 12:24:16 +0100 | [diff] [blame] | 770 | DRM_DEBUG_ATOMIC("Link plane state %p to [CRTC:%d]\n", |
| 771 | plane_state, crtc->base.id); |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 772 | else |
Daniel Vetter | 17a38d9 | 2015-02-22 12:24:16 +0100 | [diff] [blame] | 773 | DRM_DEBUG_ATOMIC("Link plane state %p to [NOCRTC]\n", |
| 774 | plane_state); |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 775 | |
| 776 | return 0; |
| 777 | } |
| 778 | EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane); |
| 779 | |
| 780 | /** |
John Hunter | 16d78bc2e | 2015-04-07 19:38:50 +0800 | [diff] [blame] | 781 | * drm_atomic_set_fb_for_plane - set framebuffer for plane |
Daniel Vetter | 321ebf0 | 2014-11-04 22:57:27 +0100 | [diff] [blame] | 782 | * @plane_state: atomic state object for the plane |
| 783 | * @fb: fb to use for the plane |
| 784 | * |
| 785 | * Changing the assigned framebuffer for a plane requires us to grab a reference |
| 786 | * to the new fb and drop the reference to the old fb, if there is one. This |
| 787 | * function takes care of all these details besides updating the pointer in the |
| 788 | * state object itself. |
| 789 | */ |
| 790 | void |
| 791 | drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state, |
| 792 | struct drm_framebuffer *fb) |
| 793 | { |
| 794 | if (plane_state->fb) |
| 795 | drm_framebuffer_unreference(plane_state->fb); |
| 796 | if (fb) |
| 797 | drm_framebuffer_reference(fb); |
| 798 | plane_state->fb = fb; |
| 799 | |
| 800 | if (fb) |
Daniel Vetter | 17a38d9 | 2015-02-22 12:24:16 +0100 | [diff] [blame] | 801 | DRM_DEBUG_ATOMIC("Set [FB:%d] for plane state %p\n", |
| 802 | fb->base.id, plane_state); |
Daniel Vetter | 321ebf0 | 2014-11-04 22:57:27 +0100 | [diff] [blame] | 803 | else |
Daniel Vetter | 17a38d9 | 2015-02-22 12:24:16 +0100 | [diff] [blame] | 804 | DRM_DEBUG_ATOMIC("Set [NOFB] for plane state %p\n", |
| 805 | plane_state); |
Daniel Vetter | 321ebf0 | 2014-11-04 22:57:27 +0100 | [diff] [blame] | 806 | } |
| 807 | EXPORT_SYMBOL(drm_atomic_set_fb_for_plane); |
| 808 | |
| 809 | /** |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 810 | * drm_atomic_set_crtc_for_connector - set crtc for connector |
| 811 | * @conn_state: atomic state object for the connector |
| 812 | * @crtc: crtc to use for the connector |
| 813 | * |
| 814 | * Changing the assigned crtc for a connector requires us to grab the lock and |
| 815 | * state for the new crtc, as needed. This function takes care of all these |
| 816 | * details besides updating the pointer in the state object itself. |
| 817 | * |
| 818 | * Returns: |
| 819 | * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK |
| 820 | * then the w/w mutex code has detected a deadlock and the entire atomic |
| 821 | * sequence must be restarted. All other errors are fatal. |
| 822 | */ |
| 823 | int |
| 824 | drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state, |
| 825 | struct drm_crtc *crtc) |
| 826 | { |
| 827 | struct drm_crtc_state *crtc_state; |
| 828 | |
| 829 | if (crtc) { |
| 830 | crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc); |
| 831 | if (IS_ERR(crtc_state)) |
| 832 | return PTR_ERR(crtc_state); |
| 833 | } |
| 834 | |
| 835 | conn_state->crtc = crtc; |
| 836 | |
| 837 | if (crtc) |
Daniel Vetter | 17a38d9 | 2015-02-22 12:24:16 +0100 | [diff] [blame] | 838 | DRM_DEBUG_ATOMIC("Link connector state %p to [CRTC:%d]\n", |
| 839 | conn_state, crtc->base.id); |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 840 | else |
Daniel Vetter | 17a38d9 | 2015-02-22 12:24:16 +0100 | [diff] [blame] | 841 | DRM_DEBUG_ATOMIC("Link connector state %p to [NOCRTC]\n", |
| 842 | conn_state); |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 843 | |
| 844 | return 0; |
| 845 | } |
| 846 | EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector); |
| 847 | |
| 848 | /** |
| 849 | * drm_atomic_add_affected_connectors - add connectors for crtc |
| 850 | * @state: atomic state |
| 851 | * @crtc: DRM crtc |
| 852 | * |
| 853 | * This function walks the current configuration and adds all connectors |
| 854 | * currently using @crtc to the atomic configuration @state. Note that this |
| 855 | * function must acquire the connection mutex. This can potentially cause |
| 856 | * unneeded seralization if the update is just for the planes on one crtc. Hence |
| 857 | * drivers and helpers should only call this when really needed (e.g. when a |
| 858 | * full modeset needs to happen due to some change). |
| 859 | * |
| 860 | * Returns: |
| 861 | * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK |
| 862 | * then the w/w mutex code has detected a deadlock and the entire atomic |
| 863 | * sequence must be restarted. All other errors are fatal. |
| 864 | */ |
| 865 | int |
| 866 | drm_atomic_add_affected_connectors(struct drm_atomic_state *state, |
| 867 | struct drm_crtc *crtc) |
| 868 | { |
| 869 | struct drm_mode_config *config = &state->dev->mode_config; |
| 870 | struct drm_connector *connector; |
| 871 | struct drm_connector_state *conn_state; |
| 872 | int ret; |
| 873 | |
| 874 | ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx); |
| 875 | if (ret) |
| 876 | return ret; |
| 877 | |
Daniel Vetter | 17a38d9 | 2015-02-22 12:24:16 +0100 | [diff] [blame] | 878 | DRM_DEBUG_ATOMIC("Adding all current connectors for [CRTC:%d] to %p\n", |
| 879 | crtc->base.id, state); |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 880 | |
| 881 | /* |
| 882 | * Changed connectors are already in @state, so only need to look at the |
| 883 | * current configuration. |
| 884 | */ |
| 885 | list_for_each_entry(connector, &config->connector_list, head) { |
| 886 | if (connector->state->crtc != crtc) |
| 887 | continue; |
| 888 | |
| 889 | conn_state = drm_atomic_get_connector_state(state, connector); |
| 890 | if (IS_ERR(conn_state)) |
| 891 | return PTR_ERR(conn_state); |
| 892 | } |
| 893 | |
| 894 | return 0; |
| 895 | } |
| 896 | EXPORT_SYMBOL(drm_atomic_add_affected_connectors); |
| 897 | |
| 898 | /** |
| 899 | * drm_atomic_connectors_for_crtc - count number of connected outputs |
| 900 | * @state: atomic state |
| 901 | * @crtc: DRM crtc |
| 902 | * |
| 903 | * This function counts all connectors which will be connected to @crtc |
| 904 | * according to @state. Useful to recompute the enable state for @crtc. |
| 905 | */ |
| 906 | int |
| 907 | drm_atomic_connectors_for_crtc(struct drm_atomic_state *state, |
| 908 | struct drm_crtc *crtc) |
| 909 | { |
Ander Conselvan de Oliveira | df63b99 | 2015-04-10 14:58:39 +0300 | [diff] [blame] | 910 | struct drm_connector *connector; |
| 911 | struct drm_connector_state *conn_state; |
| 912 | |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 913 | int i, num_connected_connectors = 0; |
| 914 | |
Ander Conselvan de Oliveira | df63b99 | 2015-04-10 14:58:39 +0300 | [diff] [blame] | 915 | for_each_connector_in_state(state, connector, conn_state, i) { |
| 916 | if (conn_state->crtc == crtc) |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 917 | num_connected_connectors++; |
| 918 | } |
| 919 | |
Daniel Vetter | 17a38d9 | 2015-02-22 12:24:16 +0100 | [diff] [blame] | 920 | DRM_DEBUG_ATOMIC("State %p has %i connectors for [CRTC:%d]\n", |
| 921 | state, num_connected_connectors, crtc->base.id); |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 922 | |
| 923 | return num_connected_connectors; |
| 924 | } |
| 925 | EXPORT_SYMBOL(drm_atomic_connectors_for_crtc); |
| 926 | |
| 927 | /** |
| 928 | * drm_atomic_legacy_backoff - locking backoff for legacy ioctls |
| 929 | * @state: atomic state |
| 930 | * |
| 931 | * This function should be used by legacy entry points which don't understand |
| 932 | * -EDEADLK semantics. For simplicity this one will grab all modeset locks after |
John Hunter | 16d78bc2e | 2015-04-07 19:38:50 +0800 | [diff] [blame] | 933 | * the slowpath completed. |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 934 | */ |
| 935 | void drm_atomic_legacy_backoff(struct drm_atomic_state *state) |
| 936 | { |
| 937 | int ret; |
| 938 | |
| 939 | retry: |
| 940 | drm_modeset_backoff(state->acquire_ctx); |
| 941 | |
| 942 | ret = drm_modeset_lock(&state->dev->mode_config.connection_mutex, |
| 943 | state->acquire_ctx); |
| 944 | if (ret) |
| 945 | goto retry; |
| 946 | ret = drm_modeset_lock_all_crtcs(state->dev, |
| 947 | state->acquire_ctx); |
| 948 | if (ret) |
| 949 | goto retry; |
| 950 | } |
| 951 | EXPORT_SYMBOL(drm_atomic_legacy_backoff); |
| 952 | |
| 953 | /** |
| 954 | * drm_atomic_check_only - check whether a given config would work |
| 955 | * @state: atomic configuration to check |
| 956 | * |
| 957 | * Note that this function can return -EDEADLK if the driver needed to acquire |
| 958 | * more locks but encountered a deadlock. The caller must then do the usual w/w |
| 959 | * backoff dance and restart. All other errors are fatal. |
| 960 | * |
| 961 | * Returns: |
| 962 | * 0 on success, negative error code on failure. |
| 963 | */ |
| 964 | int drm_atomic_check_only(struct drm_atomic_state *state) |
| 965 | { |
Rob Clark | 5e74373 | 2014-12-18 16:01:51 -0500 | [diff] [blame] | 966 | struct drm_device *dev = state->dev; |
| 967 | struct drm_mode_config *config = &dev->mode_config; |
Ander Conselvan de Oliveira | df63b99 | 2015-04-10 14:58:39 +0300 | [diff] [blame] | 968 | struct drm_plane *plane; |
| 969 | struct drm_plane_state *plane_state; |
| 970 | struct drm_crtc *crtc; |
| 971 | struct drm_crtc_state *crtc_state; |
Rob Clark | 5e74373 | 2014-12-18 16:01:51 -0500 | [diff] [blame] | 972 | int i, ret = 0; |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 973 | |
Daniel Vetter | 17a38d9 | 2015-02-22 12:24:16 +0100 | [diff] [blame] | 974 | DRM_DEBUG_ATOMIC("checking %p\n", state); |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 975 | |
Ander Conselvan de Oliveira | df63b99 | 2015-04-10 14:58:39 +0300 | [diff] [blame] | 976 | for_each_plane_in_state(state, plane, plane_state, i) { |
| 977 | ret = drm_atomic_plane_check(plane, plane_state); |
Rob Clark | 5e74373 | 2014-12-18 16:01:51 -0500 | [diff] [blame] | 978 | if (ret) { |
Daniel Vetter | 17a38d9 | 2015-02-22 12:24:16 +0100 | [diff] [blame] | 979 | DRM_DEBUG_ATOMIC("[PLANE:%d] atomic core check failed\n", |
| 980 | plane->base.id); |
Rob Clark | 5e74373 | 2014-12-18 16:01:51 -0500 | [diff] [blame] | 981 | return ret; |
| 982 | } |
| 983 | } |
| 984 | |
Ander Conselvan de Oliveira | df63b99 | 2015-04-10 14:58:39 +0300 | [diff] [blame] | 985 | for_each_crtc_in_state(state, crtc, crtc_state, i) { |
| 986 | ret = drm_atomic_crtc_check(crtc, crtc_state); |
Rob Clark | 5e74373 | 2014-12-18 16:01:51 -0500 | [diff] [blame] | 987 | if (ret) { |
Daniel Vetter | 17a38d9 | 2015-02-22 12:24:16 +0100 | [diff] [blame] | 988 | DRM_DEBUG_ATOMIC("[CRTC:%d] atomic core check failed\n", |
| 989 | crtc->base.id); |
Rob Clark | 5e74373 | 2014-12-18 16:01:51 -0500 | [diff] [blame] | 990 | return ret; |
| 991 | } |
| 992 | } |
| 993 | |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 994 | if (config->funcs->atomic_check) |
Rob Clark | 5e74373 | 2014-12-18 16:01:51 -0500 | [diff] [blame] | 995 | ret = config->funcs->atomic_check(state->dev, state); |
| 996 | |
Rob Clark | d34f20d | 2014-12-18 16:01:56 -0500 | [diff] [blame] | 997 | if (!state->allow_modeset) { |
Ander Conselvan de Oliveira | df63b99 | 2015-04-10 14:58:39 +0300 | [diff] [blame] | 998 | for_each_crtc_in_state(state, crtc, crtc_state, i) { |
Daniel Vetter | eab3bbe | 2015-01-22 16:36:21 +0100 | [diff] [blame] | 999 | if (crtc_state->mode_changed || |
| 1000 | crtc_state->active_changed) { |
Daniel Vetter | 17a38d9 | 2015-02-22 12:24:16 +0100 | [diff] [blame] | 1001 | DRM_DEBUG_ATOMIC("[CRTC:%d] requires full modeset\n", |
| 1002 | crtc->base.id); |
Rob Clark | d34f20d | 2014-12-18 16:01:56 -0500 | [diff] [blame] | 1003 | return -EINVAL; |
| 1004 | } |
| 1005 | } |
| 1006 | } |
| 1007 | |
Rob Clark | 5e74373 | 2014-12-18 16:01:51 -0500 | [diff] [blame] | 1008 | return ret; |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 1009 | } |
| 1010 | EXPORT_SYMBOL(drm_atomic_check_only); |
| 1011 | |
| 1012 | /** |
| 1013 | * drm_atomic_commit - commit configuration atomically |
| 1014 | * @state: atomic configuration to check |
| 1015 | * |
| 1016 | * Note that this function can return -EDEADLK if the driver needed to acquire |
| 1017 | * more locks but encountered a deadlock. The caller must then do the usual w/w |
| 1018 | * backoff dance and restart. All other errors are fatal. |
| 1019 | * |
| 1020 | * Also note that on successful execution ownership of @state is transferred |
| 1021 | * from the caller of this function to the function itself. The caller must not |
| 1022 | * free or in any other way access @state. If the function fails then the caller |
| 1023 | * must clean up @state itself. |
| 1024 | * |
| 1025 | * Returns: |
| 1026 | * 0 on success, negative error code on failure. |
| 1027 | */ |
| 1028 | int drm_atomic_commit(struct drm_atomic_state *state) |
| 1029 | { |
| 1030 | struct drm_mode_config *config = &state->dev->mode_config; |
| 1031 | int ret; |
| 1032 | |
| 1033 | ret = drm_atomic_check_only(state); |
| 1034 | if (ret) |
| 1035 | return ret; |
| 1036 | |
Daniel Vetter | 17a38d9 | 2015-02-22 12:24:16 +0100 | [diff] [blame] | 1037 | DRM_DEBUG_ATOMIC("commiting %p\n", state); |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 1038 | |
| 1039 | return config->funcs->atomic_commit(state->dev, state, false); |
| 1040 | } |
| 1041 | EXPORT_SYMBOL(drm_atomic_commit); |
| 1042 | |
| 1043 | /** |
| 1044 | * drm_atomic_async_commit - atomic&async configuration commit |
| 1045 | * @state: atomic configuration to check |
| 1046 | * |
| 1047 | * Note that this function can return -EDEADLK if the driver needed to acquire |
| 1048 | * more locks but encountered a deadlock. The caller must then do the usual w/w |
| 1049 | * backoff dance and restart. All other errors are fatal. |
| 1050 | * |
| 1051 | * Also note that on successful execution ownership of @state is transferred |
| 1052 | * from the caller of this function to the function itself. The caller must not |
| 1053 | * free or in any other way access @state. If the function fails then the caller |
| 1054 | * must clean up @state itself. |
| 1055 | * |
| 1056 | * Returns: |
| 1057 | * 0 on success, negative error code on failure. |
| 1058 | */ |
| 1059 | int drm_atomic_async_commit(struct drm_atomic_state *state) |
| 1060 | { |
| 1061 | struct drm_mode_config *config = &state->dev->mode_config; |
| 1062 | int ret; |
| 1063 | |
| 1064 | ret = drm_atomic_check_only(state); |
| 1065 | if (ret) |
| 1066 | return ret; |
| 1067 | |
Daniel Vetter | 17a38d9 | 2015-02-22 12:24:16 +0100 | [diff] [blame] | 1068 | DRM_DEBUG_ATOMIC("commiting %p asynchronously\n", state); |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 1069 | |
| 1070 | return config->funcs->atomic_commit(state->dev, state, true); |
| 1071 | } |
| 1072 | EXPORT_SYMBOL(drm_atomic_async_commit); |
Rob Clark | d34f20d | 2014-12-18 16:01:56 -0500 | [diff] [blame] | 1073 | |
| 1074 | /* |
| 1075 | * The big monstor ioctl |
| 1076 | */ |
| 1077 | |
| 1078 | static struct drm_pending_vblank_event *create_vblank_event( |
| 1079 | struct drm_device *dev, struct drm_file *file_priv, uint64_t user_data) |
| 1080 | { |
| 1081 | struct drm_pending_vblank_event *e = NULL; |
| 1082 | unsigned long flags; |
| 1083 | |
| 1084 | spin_lock_irqsave(&dev->event_lock, flags); |
| 1085 | if (file_priv->event_space < sizeof e->event) { |
| 1086 | spin_unlock_irqrestore(&dev->event_lock, flags); |
| 1087 | goto out; |
| 1088 | } |
| 1089 | file_priv->event_space -= sizeof e->event; |
| 1090 | spin_unlock_irqrestore(&dev->event_lock, flags); |
| 1091 | |
| 1092 | e = kzalloc(sizeof *e, GFP_KERNEL); |
| 1093 | if (e == NULL) { |
| 1094 | spin_lock_irqsave(&dev->event_lock, flags); |
| 1095 | file_priv->event_space += sizeof e->event; |
| 1096 | spin_unlock_irqrestore(&dev->event_lock, flags); |
| 1097 | goto out; |
| 1098 | } |
| 1099 | |
| 1100 | e->event.base.type = DRM_EVENT_FLIP_COMPLETE; |
| 1101 | e->event.base.length = sizeof e->event; |
| 1102 | e->event.user_data = user_data; |
| 1103 | e->base.event = &e->event.base; |
| 1104 | e->base.file_priv = file_priv; |
| 1105 | e->base.destroy = (void (*) (struct drm_pending_event *)) kfree; |
| 1106 | |
| 1107 | out: |
| 1108 | return e; |
| 1109 | } |
| 1110 | |
| 1111 | static void destroy_vblank_event(struct drm_device *dev, |
| 1112 | struct drm_file *file_priv, struct drm_pending_vblank_event *e) |
| 1113 | { |
| 1114 | unsigned long flags; |
| 1115 | |
| 1116 | spin_lock_irqsave(&dev->event_lock, flags); |
| 1117 | file_priv->event_space += sizeof e->event; |
| 1118 | spin_unlock_irqrestore(&dev->event_lock, flags); |
| 1119 | kfree(e); |
| 1120 | } |
| 1121 | |
| 1122 | static int atomic_set_prop(struct drm_atomic_state *state, |
| 1123 | struct drm_mode_object *obj, struct drm_property *prop, |
| 1124 | uint64_t prop_value) |
| 1125 | { |
| 1126 | struct drm_mode_object *ref; |
| 1127 | int ret; |
| 1128 | |
| 1129 | if (!drm_property_change_valid_get(prop, prop_value, &ref)) |
| 1130 | return -EINVAL; |
| 1131 | |
| 1132 | switch (obj->type) { |
| 1133 | case DRM_MODE_OBJECT_CONNECTOR: { |
| 1134 | struct drm_connector *connector = obj_to_connector(obj); |
| 1135 | struct drm_connector_state *connector_state; |
| 1136 | |
| 1137 | connector_state = drm_atomic_get_connector_state(state, connector); |
| 1138 | if (IS_ERR(connector_state)) { |
| 1139 | ret = PTR_ERR(connector_state); |
| 1140 | break; |
| 1141 | } |
| 1142 | |
| 1143 | ret = drm_atomic_connector_set_property(connector, |
| 1144 | connector_state, prop, prop_value); |
| 1145 | break; |
| 1146 | } |
| 1147 | case DRM_MODE_OBJECT_CRTC: { |
| 1148 | struct drm_crtc *crtc = obj_to_crtc(obj); |
| 1149 | struct drm_crtc_state *crtc_state; |
| 1150 | |
| 1151 | crtc_state = drm_atomic_get_crtc_state(state, crtc); |
| 1152 | if (IS_ERR(crtc_state)) { |
| 1153 | ret = PTR_ERR(crtc_state); |
| 1154 | break; |
| 1155 | } |
| 1156 | |
| 1157 | ret = drm_atomic_crtc_set_property(crtc, |
| 1158 | crtc_state, prop, prop_value); |
| 1159 | break; |
| 1160 | } |
| 1161 | case DRM_MODE_OBJECT_PLANE: { |
| 1162 | struct drm_plane *plane = obj_to_plane(obj); |
| 1163 | struct drm_plane_state *plane_state; |
| 1164 | |
| 1165 | plane_state = drm_atomic_get_plane_state(state, plane); |
| 1166 | if (IS_ERR(plane_state)) { |
| 1167 | ret = PTR_ERR(plane_state); |
| 1168 | break; |
| 1169 | } |
| 1170 | |
| 1171 | ret = drm_atomic_plane_set_property(plane, |
| 1172 | plane_state, prop, prop_value); |
| 1173 | break; |
| 1174 | } |
| 1175 | default: |
| 1176 | ret = -EINVAL; |
| 1177 | break; |
| 1178 | } |
| 1179 | |
| 1180 | drm_property_change_valid_put(prop, ref); |
| 1181 | return ret; |
| 1182 | } |
| 1183 | |
| 1184 | int drm_mode_atomic_ioctl(struct drm_device *dev, |
| 1185 | void *data, struct drm_file *file_priv) |
| 1186 | { |
| 1187 | struct drm_mode_atomic *arg = data; |
| 1188 | uint32_t __user *objs_ptr = (uint32_t __user *)(unsigned long)(arg->objs_ptr); |
| 1189 | uint32_t __user *count_props_ptr = (uint32_t __user *)(unsigned long)(arg->count_props_ptr); |
| 1190 | uint32_t __user *props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr); |
| 1191 | uint64_t __user *prop_values_ptr = (uint64_t __user *)(unsigned long)(arg->prop_values_ptr); |
| 1192 | unsigned int copied_objs, copied_props; |
| 1193 | struct drm_atomic_state *state; |
| 1194 | struct drm_modeset_acquire_ctx ctx; |
| 1195 | struct drm_plane *plane; |
Ander Conselvan de Oliveira | df63b99 | 2015-04-10 14:58:39 +0300 | [diff] [blame] | 1196 | struct drm_crtc *crtc; |
| 1197 | struct drm_crtc_state *crtc_state; |
Rob Clark | d34f20d | 2014-12-18 16:01:56 -0500 | [diff] [blame] | 1198 | unsigned plane_mask = 0; |
| 1199 | int ret = 0; |
| 1200 | unsigned int i, j; |
| 1201 | |
| 1202 | /* disallow for drivers not supporting atomic: */ |
| 1203 | if (!drm_core_check_feature(dev, DRIVER_ATOMIC)) |
| 1204 | return -EINVAL; |
| 1205 | |
| 1206 | /* disallow for userspace that has not enabled atomic cap (even |
| 1207 | * though this may be a bit overkill, since legacy userspace |
| 1208 | * wouldn't know how to call this ioctl) |
| 1209 | */ |
| 1210 | if (!file_priv->atomic) |
| 1211 | return -EINVAL; |
| 1212 | |
| 1213 | if (arg->flags & ~DRM_MODE_ATOMIC_FLAGS) |
| 1214 | return -EINVAL; |
| 1215 | |
| 1216 | if (arg->reserved) |
| 1217 | return -EINVAL; |
| 1218 | |
| 1219 | if ((arg->flags & DRM_MODE_PAGE_FLIP_ASYNC) && |
| 1220 | !dev->mode_config.async_page_flip) |
| 1221 | return -EINVAL; |
| 1222 | |
| 1223 | /* can't test and expect an event at the same time. */ |
| 1224 | if ((arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) && |
| 1225 | (arg->flags & DRM_MODE_PAGE_FLIP_EVENT)) |
| 1226 | return -EINVAL; |
| 1227 | |
| 1228 | drm_modeset_acquire_init(&ctx, 0); |
| 1229 | |
| 1230 | state = drm_atomic_state_alloc(dev); |
| 1231 | if (!state) |
| 1232 | return -ENOMEM; |
| 1233 | |
| 1234 | state->acquire_ctx = &ctx; |
| 1235 | state->allow_modeset = !!(arg->flags & DRM_MODE_ATOMIC_ALLOW_MODESET); |
| 1236 | |
| 1237 | retry: |
| 1238 | copied_objs = 0; |
| 1239 | copied_props = 0; |
| 1240 | |
| 1241 | for (i = 0; i < arg->count_objs; i++) { |
| 1242 | uint32_t obj_id, count_props; |
| 1243 | struct drm_mode_object *obj; |
| 1244 | |
| 1245 | if (get_user(obj_id, objs_ptr + copied_objs)) { |
| 1246 | ret = -EFAULT; |
| 1247 | goto fail; |
| 1248 | } |
| 1249 | |
| 1250 | obj = drm_mode_object_find(dev, obj_id, DRM_MODE_OBJECT_ANY); |
| 1251 | if (!obj || !obj->properties) { |
| 1252 | ret = -ENOENT; |
| 1253 | goto fail; |
| 1254 | } |
| 1255 | |
| 1256 | if (obj->type == DRM_MODE_OBJECT_PLANE) { |
| 1257 | plane = obj_to_plane(obj); |
| 1258 | plane_mask |= (1 << drm_plane_index(plane)); |
| 1259 | plane->old_fb = plane->fb; |
| 1260 | } |
| 1261 | |
| 1262 | if (get_user(count_props, count_props_ptr + copied_objs)) { |
| 1263 | ret = -EFAULT; |
| 1264 | goto fail; |
| 1265 | } |
| 1266 | |
| 1267 | copied_objs++; |
| 1268 | |
| 1269 | for (j = 0; j < count_props; j++) { |
| 1270 | uint32_t prop_id; |
| 1271 | uint64_t prop_value; |
| 1272 | struct drm_property *prop; |
| 1273 | |
| 1274 | if (get_user(prop_id, props_ptr + copied_props)) { |
| 1275 | ret = -EFAULT; |
| 1276 | goto fail; |
| 1277 | } |
| 1278 | |
| 1279 | prop = drm_property_find(dev, prop_id); |
| 1280 | if (!prop) { |
| 1281 | ret = -ENOENT; |
| 1282 | goto fail; |
| 1283 | } |
| 1284 | |
Guenter Roeck | 42c5814 | 2015-01-12 21:12:17 -0800 | [diff] [blame] | 1285 | if (copy_from_user(&prop_value, |
| 1286 | prop_values_ptr + copied_props, |
| 1287 | sizeof(prop_value))) { |
Rob Clark | d34f20d | 2014-12-18 16:01:56 -0500 | [diff] [blame] | 1288 | ret = -EFAULT; |
| 1289 | goto fail; |
| 1290 | } |
| 1291 | |
| 1292 | ret = atomic_set_prop(state, obj, prop, prop_value); |
| 1293 | if (ret) |
| 1294 | goto fail; |
| 1295 | |
| 1296 | copied_props++; |
| 1297 | } |
| 1298 | } |
| 1299 | |
| 1300 | if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) { |
Ander Conselvan de Oliveira | df63b99 | 2015-04-10 14:58:39 +0300 | [diff] [blame] | 1301 | for_each_crtc_in_state(state, crtc, crtc_state, i) { |
Rob Clark | d34f20d | 2014-12-18 16:01:56 -0500 | [diff] [blame] | 1302 | struct drm_pending_vblank_event *e; |
| 1303 | |
Rob Clark | d34f20d | 2014-12-18 16:01:56 -0500 | [diff] [blame] | 1304 | e = create_vblank_event(dev, file_priv, arg->user_data); |
| 1305 | if (!e) { |
| 1306 | ret = -ENOMEM; |
| 1307 | goto fail; |
| 1308 | } |
| 1309 | |
| 1310 | crtc_state->event = e; |
| 1311 | } |
| 1312 | } |
| 1313 | |
| 1314 | if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) { |
| 1315 | ret = drm_atomic_check_only(state); |
| 1316 | /* _check_only() does not free state, unlike _commit() */ |
| 1317 | drm_atomic_state_free(state); |
| 1318 | } else if (arg->flags & DRM_MODE_ATOMIC_NONBLOCK) { |
| 1319 | ret = drm_atomic_async_commit(state); |
| 1320 | } else { |
| 1321 | ret = drm_atomic_commit(state); |
| 1322 | } |
| 1323 | |
| 1324 | /* if succeeded, fixup legacy plane crtc/fb ptrs before dropping |
| 1325 | * locks (ie. while it is still safe to deref plane->state). We |
| 1326 | * need to do this here because the driver entry points cannot |
| 1327 | * distinguish between legacy and atomic ioctls. |
| 1328 | */ |
| 1329 | drm_for_each_plane_mask(plane, dev, plane_mask) { |
| 1330 | if (ret == 0) { |
| 1331 | struct drm_framebuffer *new_fb = plane->state->fb; |
| 1332 | if (new_fb) |
| 1333 | drm_framebuffer_reference(new_fb); |
| 1334 | plane->fb = new_fb; |
| 1335 | plane->crtc = plane->state->crtc; |
| 1336 | } else { |
| 1337 | plane->old_fb = NULL; |
| 1338 | } |
| 1339 | if (plane->old_fb) { |
| 1340 | drm_framebuffer_unreference(plane->old_fb); |
| 1341 | plane->old_fb = NULL; |
| 1342 | } |
| 1343 | } |
| 1344 | |
| 1345 | drm_modeset_drop_locks(&ctx); |
| 1346 | drm_modeset_acquire_fini(&ctx); |
| 1347 | |
| 1348 | return ret; |
| 1349 | |
| 1350 | fail: |
| 1351 | if (ret == -EDEADLK) |
| 1352 | goto backoff; |
| 1353 | |
| 1354 | if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) { |
Ander Conselvan de Oliveira | df63b99 | 2015-04-10 14:58:39 +0300 | [diff] [blame] | 1355 | for_each_crtc_in_state(state, crtc, crtc_state, i) { |
Rob Clark | d34f20d | 2014-12-18 16:01:56 -0500 | [diff] [blame] | 1356 | destroy_vblank_event(dev, file_priv, crtc_state->event); |
| 1357 | crtc_state->event = NULL; |
| 1358 | } |
| 1359 | } |
| 1360 | |
| 1361 | drm_atomic_state_free(state); |
| 1362 | |
| 1363 | drm_modeset_drop_locks(&ctx); |
| 1364 | drm_modeset_acquire_fini(&ctx); |
| 1365 | |
| 1366 | return ret; |
| 1367 | |
| 1368 | backoff: |
| 1369 | drm_atomic_state_clear(state); |
| 1370 | drm_modeset_backoff(&ctx); |
| 1371 | |
| 1372 | goto retry; |
| 1373 | } |