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 | |
Daniel Vetter | f52b69f1 | 2014-11-19 18:38:08 +0100 | [diff] [blame] | 59 | state->num_connector = ACCESS_ONCE(dev->mode_config.num_connector); |
| 60 | |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 61 | state->crtcs = kcalloc(dev->mode_config.num_crtc, |
| 62 | sizeof(*state->crtcs), GFP_KERNEL); |
| 63 | if (!state->crtcs) |
| 64 | goto fail; |
| 65 | state->crtc_states = kcalloc(dev->mode_config.num_crtc, |
| 66 | sizeof(*state->crtc_states), GFP_KERNEL); |
| 67 | if (!state->crtc_states) |
| 68 | goto fail; |
| 69 | state->planes = kcalloc(dev->mode_config.num_total_plane, |
| 70 | sizeof(*state->planes), GFP_KERNEL); |
| 71 | if (!state->planes) |
| 72 | goto fail; |
| 73 | state->plane_states = kcalloc(dev->mode_config.num_total_plane, |
| 74 | sizeof(*state->plane_states), GFP_KERNEL); |
| 75 | if (!state->plane_states) |
| 76 | goto fail; |
Daniel Vetter | f52b69f1 | 2014-11-19 18:38:08 +0100 | [diff] [blame] | 77 | state->connectors = kcalloc(state->num_connector, |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 78 | sizeof(*state->connectors), |
| 79 | GFP_KERNEL); |
| 80 | if (!state->connectors) |
| 81 | goto fail; |
Daniel Vetter | f52b69f1 | 2014-11-19 18:38:08 +0100 | [diff] [blame] | 82 | state->connector_states = kcalloc(state->num_connector, |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 83 | sizeof(*state->connector_states), |
| 84 | GFP_KERNEL); |
| 85 | if (!state->connector_states) |
| 86 | goto fail; |
| 87 | |
| 88 | state->dev = dev; |
| 89 | |
| 90 | DRM_DEBUG_KMS("Allocate atomic state %p\n", state); |
| 91 | |
| 92 | return state; |
| 93 | fail: |
| 94 | kfree_state(state); |
| 95 | |
| 96 | return NULL; |
| 97 | } |
| 98 | EXPORT_SYMBOL(drm_atomic_state_alloc); |
| 99 | |
| 100 | /** |
| 101 | * drm_atomic_state_clear - clear state object |
| 102 | * @state: atomic state |
| 103 | * |
| 104 | * When the w/w mutex algorithm detects a deadlock we need to back off and drop |
| 105 | * all locks. So someone else could sneak in and change the current modeset |
| 106 | * configuration. Which means that all the state assembled in @state is no |
| 107 | * longer an atomic update to the current state, but to some arbitrary earlier |
| 108 | * state. Which could break assumptions the driver's ->atomic_check likely |
| 109 | * relies on. |
| 110 | * |
| 111 | * Hence we must clear all cached state and completely start over, using this |
| 112 | * function. |
| 113 | */ |
| 114 | void drm_atomic_state_clear(struct drm_atomic_state *state) |
| 115 | { |
| 116 | struct drm_device *dev = state->dev; |
Daniel Vetter | 6f75cea | 2014-11-19 18:38:07 +0100 | [diff] [blame] | 117 | struct drm_mode_config *config = &dev->mode_config; |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 118 | int i; |
| 119 | |
| 120 | DRM_DEBUG_KMS("Clearing atomic state %p\n", state); |
| 121 | |
Daniel Vetter | f52b69f1 | 2014-11-19 18:38:08 +0100 | [diff] [blame] | 122 | for (i = 0; i < state->num_connector; i++) { |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 123 | struct drm_connector *connector = state->connectors[i]; |
| 124 | |
| 125 | if (!connector) |
| 126 | continue; |
| 127 | |
Daniel Vetter | 6f75cea | 2014-11-19 18:38:07 +0100 | [diff] [blame] | 128 | WARN_ON(!drm_modeset_is_locked(&config->connection_mutex)); |
| 129 | |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 130 | connector->funcs->atomic_destroy_state(connector, |
| 131 | state->connector_states[i]); |
| 132 | } |
| 133 | |
Daniel Vetter | 6f75cea | 2014-11-19 18:38:07 +0100 | [diff] [blame] | 134 | for (i = 0; i < config->num_crtc; i++) { |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 135 | struct drm_crtc *crtc = state->crtcs[i]; |
| 136 | |
| 137 | if (!crtc) |
| 138 | continue; |
| 139 | |
| 140 | crtc->funcs->atomic_destroy_state(crtc, |
| 141 | state->crtc_states[i]); |
| 142 | } |
| 143 | |
Daniel Vetter | 6f75cea | 2014-11-19 18:38:07 +0100 | [diff] [blame] | 144 | for (i = 0; i < config->num_total_plane; i++) { |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 145 | struct drm_plane *plane = state->planes[i]; |
| 146 | |
| 147 | if (!plane) |
| 148 | continue; |
| 149 | |
| 150 | plane->funcs->atomic_destroy_state(plane, |
| 151 | state->plane_states[i]); |
| 152 | } |
| 153 | } |
| 154 | EXPORT_SYMBOL(drm_atomic_state_clear); |
| 155 | |
| 156 | /** |
| 157 | * drm_atomic_state_free - free all memory for an atomic state |
| 158 | * @state: atomic state to deallocate |
| 159 | * |
| 160 | * This frees all memory associated with an atomic state, including all the |
| 161 | * per-object state for planes, crtcs and connectors. |
| 162 | */ |
| 163 | void drm_atomic_state_free(struct drm_atomic_state *state) |
| 164 | { |
| 165 | drm_atomic_state_clear(state); |
| 166 | |
| 167 | DRM_DEBUG_KMS("Freeing atomic state %p\n", state); |
| 168 | |
| 169 | kfree_state(state); |
| 170 | } |
| 171 | EXPORT_SYMBOL(drm_atomic_state_free); |
| 172 | |
| 173 | /** |
| 174 | * drm_atomic_get_crtc_state - get crtc state |
| 175 | * @state: global atomic state object |
| 176 | * @crtc: crtc to get state object for |
| 177 | * |
| 178 | * This function returns the crtc state for the given crtc, allocating it if |
| 179 | * needed. It will also grab the relevant crtc lock to make sure that the state |
| 180 | * is consistent. |
| 181 | * |
| 182 | * Returns: |
| 183 | * |
| 184 | * Either the allocated state or the error code encoded into the pointer. When |
| 185 | * the error is EDEADLK then the w/w mutex code has detected a deadlock and the |
| 186 | * entire atomic sequence must be restarted. All other errors are fatal. |
| 187 | */ |
| 188 | struct drm_crtc_state * |
| 189 | drm_atomic_get_crtc_state(struct drm_atomic_state *state, |
| 190 | struct drm_crtc *crtc) |
| 191 | { |
| 192 | int ret, index; |
| 193 | struct drm_crtc_state *crtc_state; |
| 194 | |
| 195 | index = drm_crtc_index(crtc); |
| 196 | |
| 197 | if (state->crtc_states[index]) |
| 198 | return state->crtc_states[index]; |
| 199 | |
| 200 | ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx); |
| 201 | if (ret) |
| 202 | return ERR_PTR(ret); |
| 203 | |
| 204 | crtc_state = crtc->funcs->atomic_duplicate_state(crtc); |
| 205 | if (!crtc_state) |
| 206 | return ERR_PTR(-ENOMEM); |
| 207 | |
| 208 | state->crtc_states[index] = crtc_state; |
| 209 | state->crtcs[index] = crtc; |
| 210 | crtc_state->state = state; |
| 211 | |
| 212 | DRM_DEBUG_KMS("Added [CRTC:%d] %p state to %p\n", |
| 213 | crtc->base.id, crtc_state, state); |
| 214 | |
| 215 | return crtc_state; |
| 216 | } |
| 217 | EXPORT_SYMBOL(drm_atomic_get_crtc_state); |
| 218 | |
| 219 | /** |
Rob Clark | 40ecc69 | 2014-12-18 16:01:46 -0500 | [diff] [blame] | 220 | * drm_atomic_crtc_set_property - set property on CRTC |
| 221 | * @crtc: the drm CRTC to set a property on |
| 222 | * @state: the state object to update with the new property value |
| 223 | * @property: the property to set |
| 224 | * @val: the new property value |
| 225 | * |
| 226 | * Use this instead of calling crtc->atomic_set_property directly. |
| 227 | * This function handles generic/core properties and calls out to |
| 228 | * driver's ->atomic_set_property() for driver properties. To ensure |
| 229 | * consistent behavior you must call this function rather than the |
| 230 | * driver hook directly. |
| 231 | * |
| 232 | * RETURNS: |
| 233 | * Zero on success, error code on failure |
| 234 | */ |
| 235 | int drm_atomic_crtc_set_property(struct drm_crtc *crtc, |
| 236 | struct drm_crtc_state *state, struct drm_property *property, |
| 237 | uint64_t val) |
| 238 | { |
| 239 | if (crtc->funcs->atomic_set_property) |
| 240 | return crtc->funcs->atomic_set_property(crtc, state, property, val); |
| 241 | return -EINVAL; |
| 242 | } |
| 243 | EXPORT_SYMBOL(drm_atomic_crtc_set_property); |
| 244 | |
| 245 | /** |
Rob Clark | ac9c925 | 2014-12-18 16:01:47 -0500 | [diff] [blame] | 246 | * drm_atomic_crtc_get_property - get property on CRTC |
| 247 | * @crtc: the drm CRTC to get a property on |
| 248 | * @state: the state object with the property value to read |
| 249 | * @property: the property to get |
| 250 | * @val: the property value (returned by reference) |
| 251 | * |
| 252 | * Use this instead of calling crtc->atomic_get_property directly. |
| 253 | * This function handles generic/core properties and calls out to |
| 254 | * driver's ->atomic_get_property() for driver properties. To ensure |
| 255 | * consistent behavior you must call this function rather than the |
| 256 | * driver hook directly. |
| 257 | * |
| 258 | * RETURNS: |
| 259 | * Zero on success, error code on failure |
| 260 | */ |
| 261 | int drm_atomic_crtc_get_property(struct drm_crtc *crtc, |
| 262 | const struct drm_crtc_state *state, |
| 263 | struct drm_property *property, uint64_t *val) |
| 264 | { |
| 265 | if (crtc->funcs->atomic_get_property) |
| 266 | return crtc->funcs->atomic_get_property(crtc, state, property, val); |
| 267 | return -EINVAL; |
| 268 | } |
| 269 | EXPORT_SYMBOL(drm_atomic_crtc_get_property); |
| 270 | |
| 271 | /** |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 272 | * drm_atomic_get_plane_state - get plane state |
| 273 | * @state: global atomic state object |
| 274 | * @plane: plane to get state object for |
| 275 | * |
| 276 | * This function returns the plane state for the given plane, allocating it if |
| 277 | * needed. It will also grab the relevant plane lock to make sure that the state |
| 278 | * is consistent. |
| 279 | * |
| 280 | * Returns: |
| 281 | * |
| 282 | * Either the allocated state or the error code encoded into the pointer. When |
| 283 | * the error is EDEADLK then the w/w mutex code has detected a deadlock and the |
| 284 | * entire atomic sequence must be restarted. All other errors are fatal. |
| 285 | */ |
| 286 | struct drm_plane_state * |
| 287 | drm_atomic_get_plane_state(struct drm_atomic_state *state, |
| 288 | struct drm_plane *plane) |
| 289 | { |
| 290 | int ret, index; |
| 291 | struct drm_plane_state *plane_state; |
| 292 | |
| 293 | index = drm_plane_index(plane); |
| 294 | |
| 295 | if (state->plane_states[index]) |
| 296 | return state->plane_states[index]; |
| 297 | |
Daniel Vetter | 4d02e2d | 2014-11-11 10:12:00 +0100 | [diff] [blame] | 298 | ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx); |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 299 | if (ret) |
| 300 | return ERR_PTR(ret); |
| 301 | |
| 302 | plane_state = plane->funcs->atomic_duplicate_state(plane); |
| 303 | if (!plane_state) |
| 304 | return ERR_PTR(-ENOMEM); |
| 305 | |
| 306 | state->plane_states[index] = plane_state; |
| 307 | state->planes[index] = plane; |
| 308 | plane_state->state = state; |
| 309 | |
| 310 | DRM_DEBUG_KMS("Added [PLANE:%d] %p state to %p\n", |
| 311 | plane->base.id, plane_state, state); |
| 312 | |
| 313 | if (plane_state->crtc) { |
| 314 | struct drm_crtc_state *crtc_state; |
| 315 | |
| 316 | crtc_state = drm_atomic_get_crtc_state(state, |
| 317 | plane_state->crtc); |
| 318 | if (IS_ERR(crtc_state)) |
| 319 | return ERR_CAST(crtc_state); |
| 320 | } |
| 321 | |
| 322 | return plane_state; |
| 323 | } |
| 324 | EXPORT_SYMBOL(drm_atomic_get_plane_state); |
| 325 | |
| 326 | /** |
Rob Clark | 40ecc69 | 2014-12-18 16:01:46 -0500 | [diff] [blame] | 327 | * drm_atomic_plane_set_property - set property on plane |
| 328 | * @plane: the drm plane to set a property on |
| 329 | * @state: the state object to update with the new property value |
| 330 | * @property: the property to set |
| 331 | * @val: the new property value |
| 332 | * |
| 333 | * Use this instead of calling plane->atomic_set_property directly. |
| 334 | * This function handles generic/core properties and calls out to |
| 335 | * driver's ->atomic_set_property() for driver properties. To ensure |
| 336 | * consistent behavior you must call this function rather than the |
| 337 | * driver hook directly. |
| 338 | * |
| 339 | * RETURNS: |
| 340 | * Zero on success, error code on failure |
| 341 | */ |
| 342 | int drm_atomic_plane_set_property(struct drm_plane *plane, |
| 343 | struct drm_plane_state *state, struct drm_property *property, |
| 344 | uint64_t val) |
| 345 | { |
| 346 | if (plane->funcs->atomic_set_property) |
| 347 | return plane->funcs->atomic_set_property(plane, state, property, val); |
| 348 | return -EINVAL; |
| 349 | } |
| 350 | EXPORT_SYMBOL(drm_atomic_plane_set_property); |
| 351 | |
| 352 | /** |
Rob Clark | ac9c925 | 2014-12-18 16:01:47 -0500 | [diff] [blame] | 353 | * drm_atomic_plane_get_property - get property on plane |
| 354 | * @plane: the drm plane to get a property on |
| 355 | * @state: the state object with the property value to read |
| 356 | * @property: the property to get |
| 357 | * @val: the property value (returned by reference) |
| 358 | * |
| 359 | * Use this instead of calling plane->atomic_get_property directly. |
| 360 | * This function handles generic/core properties and calls out to |
| 361 | * driver's ->atomic_get_property() for driver properties. To ensure |
| 362 | * consistent behavior you must call this function rather than the |
| 363 | * driver hook directly. |
| 364 | * |
| 365 | * RETURNS: |
| 366 | * Zero on success, error code on failure |
| 367 | */ |
| 368 | int drm_atomic_plane_get_property(struct drm_plane *plane, |
| 369 | const struct drm_plane_state *state, |
| 370 | struct drm_property *property, uint64_t *val) |
| 371 | { |
| 372 | if (plane->funcs->atomic_get_property) |
| 373 | return plane->funcs->atomic_get_property(plane, state, property, val); |
| 374 | return -EINVAL; |
| 375 | } |
| 376 | EXPORT_SYMBOL(drm_atomic_plane_get_property); |
| 377 | |
| 378 | /** |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 379 | * drm_atomic_get_connector_state - get connector state |
| 380 | * @state: global atomic state object |
| 381 | * @connector: connector to get state object for |
| 382 | * |
| 383 | * This function returns the connector state for the given connector, |
| 384 | * allocating it if needed. It will also grab the relevant connector lock to |
| 385 | * make sure that the state is consistent. |
| 386 | * |
| 387 | * Returns: |
| 388 | * |
| 389 | * Either the allocated state or the error code encoded into the pointer. When |
| 390 | * the error is EDEADLK then the w/w mutex code has detected a deadlock and the |
| 391 | * entire atomic sequence must be restarted. All other errors are fatal. |
| 392 | */ |
| 393 | struct drm_connector_state * |
| 394 | drm_atomic_get_connector_state(struct drm_atomic_state *state, |
| 395 | struct drm_connector *connector) |
| 396 | { |
| 397 | int ret, index; |
| 398 | struct drm_mode_config *config = &connector->dev->mode_config; |
| 399 | struct drm_connector_state *connector_state; |
| 400 | |
Daniel Vetter | c7eb76f | 2014-11-19 18:38:06 +0100 | [diff] [blame] | 401 | ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx); |
| 402 | if (ret) |
| 403 | return ERR_PTR(ret); |
| 404 | |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 405 | index = drm_connector_index(connector); |
| 406 | |
Daniel Vetter | f52b69f1 | 2014-11-19 18:38:08 +0100 | [diff] [blame] | 407 | /* |
| 408 | * Construction of atomic state updates can race with a connector |
| 409 | * hot-add which might overflow. In this case flip the table and just |
| 410 | * restart the entire ioctl - no one is fast enough to livelock a cpu |
| 411 | * with physical hotplug events anyway. |
| 412 | * |
| 413 | * Note that we only grab the indexes once we have the right lock to |
| 414 | * prevent hotplug/unplugging of connectors. So removal is no problem, |
| 415 | * at most the array is a bit too large. |
| 416 | */ |
| 417 | if (index >= state->num_connector) { |
| 418 | DRM_DEBUG_KMS("Hot-added connector would overflow state array, restarting\n"); |
Daniel Vetter | fc2d2bc | 2014-11-20 09:53:35 +0100 | [diff] [blame] | 419 | return ERR_PTR(-EAGAIN); |
Daniel Vetter | f52b69f1 | 2014-11-19 18:38:08 +0100 | [diff] [blame] | 420 | } |
| 421 | |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 422 | if (state->connector_states[index]) |
| 423 | return state->connector_states[index]; |
| 424 | |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 425 | connector_state = connector->funcs->atomic_duplicate_state(connector); |
| 426 | if (!connector_state) |
| 427 | return ERR_PTR(-ENOMEM); |
| 428 | |
| 429 | state->connector_states[index] = connector_state; |
| 430 | state->connectors[index] = connector; |
| 431 | connector_state->state = state; |
| 432 | |
| 433 | DRM_DEBUG_KMS("Added [CONNECTOR:%d] %p state to %p\n", |
| 434 | connector->base.id, connector_state, state); |
| 435 | |
| 436 | if (connector_state->crtc) { |
| 437 | struct drm_crtc_state *crtc_state; |
| 438 | |
| 439 | crtc_state = drm_atomic_get_crtc_state(state, |
| 440 | connector_state->crtc); |
| 441 | if (IS_ERR(crtc_state)) |
| 442 | return ERR_CAST(crtc_state); |
| 443 | } |
| 444 | |
| 445 | return connector_state; |
| 446 | } |
| 447 | EXPORT_SYMBOL(drm_atomic_get_connector_state); |
| 448 | |
| 449 | /** |
Rob Clark | 40ecc69 | 2014-12-18 16:01:46 -0500 | [diff] [blame] | 450 | * drm_atomic_connector_set_property - set property on connector. |
| 451 | * @connector: the drm connector to set a property on |
| 452 | * @state: the state object to update with the new property value |
| 453 | * @property: the property to set |
| 454 | * @val: the new property value |
| 455 | * |
| 456 | * Use this instead of calling connector->atomic_set_property directly. |
| 457 | * This function handles generic/core properties and calls out to |
| 458 | * driver's ->atomic_set_property() for driver properties. To ensure |
| 459 | * consistent behavior you must call this function rather than the |
| 460 | * driver hook directly. |
| 461 | * |
| 462 | * RETURNS: |
| 463 | * Zero on success, error code on failure |
| 464 | */ |
| 465 | int drm_atomic_connector_set_property(struct drm_connector *connector, |
| 466 | struct drm_connector_state *state, struct drm_property *property, |
| 467 | uint64_t val) |
| 468 | { |
| 469 | struct drm_device *dev = connector->dev; |
| 470 | struct drm_mode_config *config = &dev->mode_config; |
| 471 | |
| 472 | if (property == config->dpms_property) { |
| 473 | /* setting DPMS property requires special handling, which |
| 474 | * is done in legacy setprop path for us. Disallow (for |
| 475 | * now?) atomic writes to DPMS property: |
| 476 | */ |
| 477 | return -EINVAL; |
| 478 | } else if (connector->funcs->atomic_set_property) { |
| 479 | return connector->funcs->atomic_set_property(connector, |
| 480 | state, property, val); |
| 481 | } else { |
| 482 | return -EINVAL; |
| 483 | } |
| 484 | } |
| 485 | EXPORT_SYMBOL(drm_atomic_connector_set_property); |
| 486 | |
| 487 | /** |
Rob Clark | ac9c925 | 2014-12-18 16:01:47 -0500 | [diff] [blame] | 488 | * drm_atomic_connector_get_property - get property on connector |
| 489 | * @connector: the drm connector to get a property on |
| 490 | * @state: the state object with the property value to read |
| 491 | * @property: the property to get |
| 492 | * @val: the property value (returned by reference) |
| 493 | * |
| 494 | * Use this instead of calling connector->atomic_get_property directly. |
| 495 | * This function handles generic/core properties and calls out to |
| 496 | * driver's ->atomic_get_property() for driver properties. To ensure |
| 497 | * consistent behavior you must call this function rather than the |
| 498 | * driver hook directly. |
| 499 | * |
| 500 | * RETURNS: |
| 501 | * Zero on success, error code on failure |
| 502 | */ |
| 503 | int drm_atomic_connector_get_property(struct drm_connector *connector, |
| 504 | const struct drm_connector_state *state, |
| 505 | struct drm_property *property, uint64_t *val) |
| 506 | { |
| 507 | struct drm_device *dev = connector->dev; |
| 508 | struct drm_mode_config *config = &dev->mode_config; |
| 509 | |
| 510 | if (property == config->dpms_property) { |
| 511 | *val = connector->dpms; |
| 512 | } else if (connector->funcs->atomic_get_property) { |
| 513 | return connector->funcs->atomic_get_property(connector, |
| 514 | state, property, val); |
| 515 | } else { |
| 516 | return -EINVAL; |
| 517 | } |
| 518 | |
| 519 | return 0; |
| 520 | } |
| 521 | EXPORT_SYMBOL(drm_atomic_connector_get_property); |
| 522 | |
| 523 | /** |
Rob Clark | 88a48e2 | 2014-12-18 16:01:50 -0500 | [diff] [blame^] | 524 | * drm_atomic_get_property - helper to read atomic property |
| 525 | * @obj: drm mode object whose property to read |
| 526 | * @property: the property to read |
| 527 | * @val: the read value, returned by reference |
| 528 | * |
| 529 | * RETURNS: |
| 530 | * Zero on success, error code on failure |
| 531 | */ |
| 532 | int drm_atomic_get_property(struct drm_mode_object *obj, |
| 533 | struct drm_property *property, uint64_t *val) |
| 534 | { |
| 535 | struct drm_device *dev = property->dev; |
| 536 | int ret; |
| 537 | |
| 538 | switch (obj->type) { |
| 539 | case DRM_MODE_OBJECT_CONNECTOR: { |
| 540 | struct drm_connector *connector = obj_to_connector(obj); |
| 541 | WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex)); |
| 542 | ret = drm_atomic_connector_get_property(connector, |
| 543 | connector->state, property, val); |
| 544 | break; |
| 545 | } |
| 546 | case DRM_MODE_OBJECT_CRTC: { |
| 547 | struct drm_crtc *crtc = obj_to_crtc(obj); |
| 548 | WARN_ON(!drm_modeset_is_locked(&crtc->mutex)); |
| 549 | ret = drm_atomic_crtc_get_property(crtc, |
| 550 | crtc->state, property, val); |
| 551 | break; |
| 552 | } |
| 553 | case DRM_MODE_OBJECT_PLANE: { |
| 554 | struct drm_plane *plane = obj_to_plane(obj); |
| 555 | WARN_ON(!drm_modeset_is_locked(&plane->mutex)); |
| 556 | ret = drm_atomic_plane_get_property(plane, |
| 557 | plane->state, property, val); |
| 558 | break; |
| 559 | } |
| 560 | default: |
| 561 | ret = -EINVAL; |
| 562 | break; |
| 563 | } |
| 564 | |
| 565 | return ret; |
| 566 | } |
| 567 | |
| 568 | /** |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 569 | * drm_atomic_set_crtc_for_plane - set crtc for plane |
Daniel Vetter | 07cc0ef | 2014-11-27 15:49:39 +0100 | [diff] [blame] | 570 | * @plane_state: the plane whose incoming state to update |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 571 | * @crtc: crtc to use for the plane |
| 572 | * |
| 573 | * Changing the assigned crtc for a plane requires us to grab the lock and state |
| 574 | * for the new crtc, as needed. This function takes care of all these details |
| 575 | * besides updating the pointer in the state object itself. |
| 576 | * |
| 577 | * Returns: |
| 578 | * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK |
| 579 | * then the w/w mutex code has detected a deadlock and the entire atomic |
| 580 | * sequence must be restarted. All other errors are fatal. |
| 581 | */ |
| 582 | int |
Daniel Vetter | 07cc0ef | 2014-11-27 15:49:39 +0100 | [diff] [blame] | 583 | drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state, |
| 584 | struct drm_crtc *crtc) |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 585 | { |
Daniel Vetter | 07cc0ef | 2014-11-27 15:49:39 +0100 | [diff] [blame] | 586 | struct drm_plane *plane = plane_state->plane; |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 587 | struct drm_crtc_state *crtc_state; |
| 588 | |
Rob Clark | 6ddd388 | 2014-11-21 15:28:31 -0500 | [diff] [blame] | 589 | if (plane_state->crtc) { |
| 590 | crtc_state = drm_atomic_get_crtc_state(plane_state->state, |
| 591 | plane_state->crtc); |
| 592 | if (WARN_ON(IS_ERR(crtc_state))) |
| 593 | return PTR_ERR(crtc_state); |
| 594 | |
| 595 | crtc_state->plane_mask &= ~(1 << drm_plane_index(plane)); |
| 596 | } |
| 597 | |
| 598 | plane_state->crtc = crtc; |
| 599 | |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 600 | if (crtc) { |
| 601 | crtc_state = drm_atomic_get_crtc_state(plane_state->state, |
| 602 | crtc); |
| 603 | if (IS_ERR(crtc_state)) |
| 604 | return PTR_ERR(crtc_state); |
Rob Clark | 6ddd388 | 2014-11-21 15:28:31 -0500 | [diff] [blame] | 605 | crtc_state->plane_mask |= (1 << drm_plane_index(plane)); |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 606 | } |
| 607 | |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 608 | if (crtc) |
| 609 | DRM_DEBUG_KMS("Link plane state %p to [CRTC:%d]\n", |
| 610 | plane_state, crtc->base.id); |
| 611 | else |
| 612 | DRM_DEBUG_KMS("Link plane state %p to [NOCRTC]\n", plane_state); |
| 613 | |
| 614 | return 0; |
| 615 | } |
| 616 | EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane); |
| 617 | |
| 618 | /** |
Daniel Vetter | 321ebf0 | 2014-11-04 22:57:27 +0100 | [diff] [blame] | 619 | * drm_atomic_set_fb_for_plane - set crtc for plane |
| 620 | * @plane_state: atomic state object for the plane |
| 621 | * @fb: fb to use for the plane |
| 622 | * |
| 623 | * Changing the assigned framebuffer for a plane requires us to grab a reference |
| 624 | * to the new fb and drop the reference to the old fb, if there is one. This |
| 625 | * function takes care of all these details besides updating the pointer in the |
| 626 | * state object itself. |
| 627 | */ |
| 628 | void |
| 629 | drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state, |
| 630 | struct drm_framebuffer *fb) |
| 631 | { |
| 632 | if (plane_state->fb) |
| 633 | drm_framebuffer_unreference(plane_state->fb); |
| 634 | if (fb) |
| 635 | drm_framebuffer_reference(fb); |
| 636 | plane_state->fb = fb; |
| 637 | |
| 638 | if (fb) |
| 639 | DRM_DEBUG_KMS("Set [FB:%d] for plane state %p\n", |
| 640 | fb->base.id, plane_state); |
| 641 | else |
| 642 | DRM_DEBUG_KMS("Set [NOFB] for plane state %p\n", plane_state); |
| 643 | } |
| 644 | EXPORT_SYMBOL(drm_atomic_set_fb_for_plane); |
| 645 | |
| 646 | /** |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 647 | * drm_atomic_set_crtc_for_connector - set crtc for connector |
| 648 | * @conn_state: atomic state object for the connector |
| 649 | * @crtc: crtc to use for the connector |
| 650 | * |
| 651 | * Changing the assigned crtc for a connector requires us to grab the lock and |
| 652 | * state for the new crtc, as needed. This function takes care of all these |
| 653 | * details besides updating the pointer in the state object itself. |
| 654 | * |
| 655 | * Returns: |
| 656 | * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK |
| 657 | * then the w/w mutex code has detected a deadlock and the entire atomic |
| 658 | * sequence must be restarted. All other errors are fatal. |
| 659 | */ |
| 660 | int |
| 661 | drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state, |
| 662 | struct drm_crtc *crtc) |
| 663 | { |
| 664 | struct drm_crtc_state *crtc_state; |
| 665 | |
| 666 | if (crtc) { |
| 667 | crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc); |
| 668 | if (IS_ERR(crtc_state)) |
| 669 | return PTR_ERR(crtc_state); |
| 670 | } |
| 671 | |
| 672 | conn_state->crtc = crtc; |
| 673 | |
| 674 | if (crtc) |
| 675 | DRM_DEBUG_KMS("Link connector state %p to [CRTC:%d]\n", |
| 676 | conn_state, crtc->base.id); |
| 677 | else |
| 678 | DRM_DEBUG_KMS("Link connector state %p to [NOCRTC]\n", |
| 679 | conn_state); |
| 680 | |
| 681 | return 0; |
| 682 | } |
| 683 | EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector); |
| 684 | |
| 685 | /** |
| 686 | * drm_atomic_add_affected_connectors - add connectors for crtc |
| 687 | * @state: atomic state |
| 688 | * @crtc: DRM crtc |
| 689 | * |
| 690 | * This function walks the current configuration and adds all connectors |
| 691 | * currently using @crtc to the atomic configuration @state. Note that this |
| 692 | * function must acquire the connection mutex. This can potentially cause |
| 693 | * unneeded seralization if the update is just for the planes on one crtc. Hence |
| 694 | * drivers and helpers should only call this when really needed (e.g. when a |
| 695 | * full modeset needs to happen due to some change). |
| 696 | * |
| 697 | * Returns: |
| 698 | * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK |
| 699 | * then the w/w mutex code has detected a deadlock and the entire atomic |
| 700 | * sequence must be restarted. All other errors are fatal. |
| 701 | */ |
| 702 | int |
| 703 | drm_atomic_add_affected_connectors(struct drm_atomic_state *state, |
| 704 | struct drm_crtc *crtc) |
| 705 | { |
| 706 | struct drm_mode_config *config = &state->dev->mode_config; |
| 707 | struct drm_connector *connector; |
| 708 | struct drm_connector_state *conn_state; |
| 709 | int ret; |
| 710 | |
| 711 | ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx); |
| 712 | if (ret) |
| 713 | return ret; |
| 714 | |
| 715 | DRM_DEBUG_KMS("Adding all current connectors for [CRTC:%d] to %p\n", |
| 716 | crtc->base.id, state); |
| 717 | |
| 718 | /* |
| 719 | * Changed connectors are already in @state, so only need to look at the |
| 720 | * current configuration. |
| 721 | */ |
| 722 | list_for_each_entry(connector, &config->connector_list, head) { |
| 723 | if (connector->state->crtc != crtc) |
| 724 | continue; |
| 725 | |
| 726 | conn_state = drm_atomic_get_connector_state(state, connector); |
| 727 | if (IS_ERR(conn_state)) |
| 728 | return PTR_ERR(conn_state); |
| 729 | } |
| 730 | |
| 731 | return 0; |
| 732 | } |
| 733 | EXPORT_SYMBOL(drm_atomic_add_affected_connectors); |
| 734 | |
| 735 | /** |
| 736 | * drm_atomic_connectors_for_crtc - count number of connected outputs |
| 737 | * @state: atomic state |
| 738 | * @crtc: DRM crtc |
| 739 | * |
| 740 | * This function counts all connectors which will be connected to @crtc |
| 741 | * according to @state. Useful to recompute the enable state for @crtc. |
| 742 | */ |
| 743 | int |
| 744 | drm_atomic_connectors_for_crtc(struct drm_atomic_state *state, |
| 745 | struct drm_crtc *crtc) |
| 746 | { |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 747 | int i, num_connected_connectors = 0; |
| 748 | |
Daniel Vetter | f52b69f1 | 2014-11-19 18:38:08 +0100 | [diff] [blame] | 749 | for (i = 0; i < state->num_connector; i++) { |
Daniel Vetter | cc4ceb4 | 2014-07-25 21:30:38 +0200 | [diff] [blame] | 750 | struct drm_connector_state *conn_state; |
| 751 | |
| 752 | conn_state = state->connector_states[i]; |
| 753 | |
| 754 | if (conn_state && conn_state->crtc == crtc) |
| 755 | num_connected_connectors++; |
| 756 | } |
| 757 | |
| 758 | DRM_DEBUG_KMS("State %p has %i connectors for [CRTC:%d]\n", |
| 759 | state, num_connected_connectors, crtc->base.id); |
| 760 | |
| 761 | return num_connected_connectors; |
| 762 | } |
| 763 | EXPORT_SYMBOL(drm_atomic_connectors_for_crtc); |
| 764 | |
| 765 | /** |
| 766 | * drm_atomic_legacy_backoff - locking backoff for legacy ioctls |
| 767 | * @state: atomic state |
| 768 | * |
| 769 | * This function should be used by legacy entry points which don't understand |
| 770 | * -EDEADLK semantics. For simplicity this one will grab all modeset locks after |
| 771 | * the slowpath completed. |
| 772 | */ |
| 773 | void drm_atomic_legacy_backoff(struct drm_atomic_state *state) |
| 774 | { |
| 775 | int ret; |
| 776 | |
| 777 | retry: |
| 778 | drm_modeset_backoff(state->acquire_ctx); |
| 779 | |
| 780 | ret = drm_modeset_lock(&state->dev->mode_config.connection_mutex, |
| 781 | state->acquire_ctx); |
| 782 | if (ret) |
| 783 | goto retry; |
| 784 | ret = drm_modeset_lock_all_crtcs(state->dev, |
| 785 | state->acquire_ctx); |
| 786 | if (ret) |
| 787 | goto retry; |
| 788 | } |
| 789 | EXPORT_SYMBOL(drm_atomic_legacy_backoff); |
| 790 | |
| 791 | /** |
| 792 | * drm_atomic_check_only - check whether a given config would work |
| 793 | * @state: atomic configuration to check |
| 794 | * |
| 795 | * Note that this function can return -EDEADLK if the driver needed to acquire |
| 796 | * more locks but encountered a deadlock. The caller must then do the usual w/w |
| 797 | * backoff dance and restart. All other errors are fatal. |
| 798 | * |
| 799 | * Returns: |
| 800 | * 0 on success, negative error code on failure. |
| 801 | */ |
| 802 | int drm_atomic_check_only(struct drm_atomic_state *state) |
| 803 | { |
| 804 | struct drm_mode_config *config = &state->dev->mode_config; |
| 805 | |
| 806 | DRM_DEBUG_KMS("checking %p\n", state); |
| 807 | |
| 808 | if (config->funcs->atomic_check) |
| 809 | return config->funcs->atomic_check(state->dev, state); |
| 810 | else |
| 811 | return 0; |
| 812 | } |
| 813 | EXPORT_SYMBOL(drm_atomic_check_only); |
| 814 | |
| 815 | /** |
| 816 | * drm_atomic_commit - commit configuration atomically |
| 817 | * @state: atomic configuration to check |
| 818 | * |
| 819 | * Note that this function can return -EDEADLK if the driver needed to acquire |
| 820 | * more locks but encountered a deadlock. The caller must then do the usual w/w |
| 821 | * backoff dance and restart. All other errors are fatal. |
| 822 | * |
| 823 | * Also note that on successful execution ownership of @state is transferred |
| 824 | * from the caller of this function to the function itself. The caller must not |
| 825 | * free or in any other way access @state. If the function fails then the caller |
| 826 | * must clean up @state itself. |
| 827 | * |
| 828 | * Returns: |
| 829 | * 0 on success, negative error code on failure. |
| 830 | */ |
| 831 | int drm_atomic_commit(struct drm_atomic_state *state) |
| 832 | { |
| 833 | struct drm_mode_config *config = &state->dev->mode_config; |
| 834 | int ret; |
| 835 | |
| 836 | ret = drm_atomic_check_only(state); |
| 837 | if (ret) |
| 838 | return ret; |
| 839 | |
| 840 | DRM_DEBUG_KMS("commiting %p\n", state); |
| 841 | |
| 842 | return config->funcs->atomic_commit(state->dev, state, false); |
| 843 | } |
| 844 | EXPORT_SYMBOL(drm_atomic_commit); |
| 845 | |
| 846 | /** |
| 847 | * drm_atomic_async_commit - atomic&async configuration commit |
| 848 | * @state: atomic configuration to check |
| 849 | * |
| 850 | * Note that this function can return -EDEADLK if the driver needed to acquire |
| 851 | * more locks but encountered a deadlock. The caller must then do the usual w/w |
| 852 | * backoff dance and restart. All other errors are fatal. |
| 853 | * |
| 854 | * Also note that on successful execution ownership of @state is transferred |
| 855 | * from the caller of this function to the function itself. The caller must not |
| 856 | * free or in any other way access @state. If the function fails then the caller |
| 857 | * must clean up @state itself. |
| 858 | * |
| 859 | * Returns: |
| 860 | * 0 on success, negative error code on failure. |
| 861 | */ |
| 862 | int drm_atomic_async_commit(struct drm_atomic_state *state) |
| 863 | { |
| 864 | struct drm_mode_config *config = &state->dev->mode_config; |
| 865 | int ret; |
| 866 | |
| 867 | ret = drm_atomic_check_only(state); |
| 868 | if (ret) |
| 869 | return ret; |
| 870 | |
| 871 | DRM_DEBUG_KMS("commiting %p asynchronously\n", state); |
| 872 | |
| 873 | return config->funcs->atomic_commit(state->dev, state, true); |
| 874 | } |
| 875 | EXPORT_SYMBOL(drm_atomic_async_commit); |