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