2 * Copyright (C) 2014 Red Hat
3 * Copyright (C) 2014 Intel Corp.
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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.
24 * Rob Clark <robdclark@gmail.com>
25 * Daniel Vetter <daniel.vetter@ffwll.ch>
30 #include <drm/drm_atomic.h>
31 #include <drm/drm_mode.h>
32 #include <drm/drm_plane_helper.h>
34 #include "drm_crtc_internal.h"
37 * drm_atomic_state_default_release -
38 * release memory initialized by drm_atomic_state_init
39 * @state: atomic state
41 * Free all the memory allocated by drm_atomic_state_init.
42 * This is useful for drivers that subclass the atomic state.
44 void drm_atomic_state_default_release(struct drm_atomic_state *state)
46 kfree(state->connectors);
50 EXPORT_SYMBOL(drm_atomic_state_default_release);
53 * drm_atomic_state_init - init new atomic state
55 * @state: atomic state
57 * Default implementation for filling in a new atomic state.
58 * This is useful for drivers that subclass the atomic state.
61 drm_atomic_state_init(struct drm_device *dev, struct drm_atomic_state *state)
63 /* TODO legacy paths should maybe do a better job about
64 * setting this appropriately?
66 state->allow_modeset = true;
68 state->crtcs = kcalloc(dev->mode_config.num_crtc,
69 sizeof(*state->crtcs), GFP_KERNEL);
72 state->planes = kcalloc(dev->mode_config.num_total_plane,
73 sizeof(*state->planes), GFP_KERNEL);
79 DRM_DEBUG_ATOMIC("Allocated atomic state %p\n", state);
83 drm_atomic_state_default_release(state);
86 EXPORT_SYMBOL(drm_atomic_state_init);
89 * drm_atomic_state_alloc - allocate atomic state
92 * This allocates an empty atomic state to track updates.
94 struct drm_atomic_state *
95 drm_atomic_state_alloc(struct drm_device *dev)
97 struct drm_mode_config *config = &dev->mode_config;
98 struct drm_atomic_state *state;
100 if (!config->funcs->atomic_state_alloc) {
101 state = kzalloc(sizeof(*state), GFP_KERNEL);
104 if (drm_atomic_state_init(dev, state) < 0) {
111 return config->funcs->atomic_state_alloc(dev);
113 EXPORT_SYMBOL(drm_atomic_state_alloc);
116 * drm_atomic_state_default_clear - clear base atomic state
117 * @state: atomic state
119 * Default implementation for clearing atomic state.
120 * This is useful for drivers that subclass the atomic state.
122 void drm_atomic_state_default_clear(struct drm_atomic_state *state)
124 struct drm_device *dev = state->dev;
125 struct drm_mode_config *config = &dev->mode_config;
128 DRM_DEBUG_ATOMIC("Clearing atomic state %p\n", state);
130 for (i = 0; i < state->num_connector; i++) {
131 struct drm_connector *connector = state->connectors[i].ptr;
136 connector->funcs->atomic_destroy_state(connector,
137 state->connectors[i].state);
138 state->connectors[i].ptr = NULL;
139 state->connectors[i].state = NULL;
140 drm_connector_unreference(connector);
143 for (i = 0; i < config->num_crtc; i++) {
144 struct drm_crtc *crtc = state->crtcs[i].ptr;
149 crtc->funcs->atomic_destroy_state(crtc,
150 state->crtcs[i].state);
151 state->crtcs[i].ptr = NULL;
152 state->crtcs[i].state = NULL;
155 for (i = 0; i < config->num_total_plane; i++) {
156 struct drm_plane *plane = state->planes[i].ptr;
161 plane->funcs->atomic_destroy_state(plane,
162 state->planes[i].state);
163 state->planes[i].ptr = NULL;
164 state->planes[i].state = NULL;
167 EXPORT_SYMBOL(drm_atomic_state_default_clear);
170 * drm_atomic_state_clear - clear state object
171 * @state: atomic state
173 * When the w/w mutex algorithm detects a deadlock we need to back off and drop
174 * all locks. So someone else could sneak in and change the current modeset
175 * configuration. Which means that all the state assembled in @state is no
176 * longer an atomic update to the current state, but to some arbitrary earlier
177 * state. Which could break assumptions the driver's ->atomic_check likely
180 * Hence we must clear all cached state and completely start over, using this
183 void drm_atomic_state_clear(struct drm_atomic_state *state)
185 struct drm_device *dev = state->dev;
186 struct drm_mode_config *config = &dev->mode_config;
188 if (config->funcs->atomic_state_clear)
189 config->funcs->atomic_state_clear(state);
191 drm_atomic_state_default_clear(state);
193 EXPORT_SYMBOL(drm_atomic_state_clear);
196 * drm_atomic_state_free - free all memory for an atomic state
197 * @state: atomic state to deallocate
199 * This frees all memory associated with an atomic state, including all the
200 * per-object state for planes, crtcs and connectors.
202 void drm_atomic_state_free(struct drm_atomic_state *state)
204 struct drm_device *dev;
205 struct drm_mode_config *config;
211 config = &dev->mode_config;
213 drm_atomic_state_clear(state);
215 DRM_DEBUG_ATOMIC("Freeing atomic state %p\n", state);
217 if (config->funcs->atomic_state_free) {
218 config->funcs->atomic_state_free(state);
220 drm_atomic_state_default_release(state);
224 EXPORT_SYMBOL(drm_atomic_state_free);
227 * drm_atomic_get_crtc_state - get crtc state
228 * @state: global atomic state object
229 * @crtc: crtc to get state object for
231 * This function returns the crtc state for the given crtc, allocating it if
232 * needed. It will also grab the relevant crtc lock to make sure that the state
237 * Either the allocated state or the error code encoded into the pointer. When
238 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
239 * entire atomic sequence must be restarted. All other errors are fatal.
241 struct drm_crtc_state *
242 drm_atomic_get_crtc_state(struct drm_atomic_state *state,
243 struct drm_crtc *crtc)
245 int ret, index = drm_crtc_index(crtc);
246 struct drm_crtc_state *crtc_state;
248 WARN_ON(!state->acquire_ctx);
250 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
254 ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx);
258 crtc_state = crtc->funcs->atomic_duplicate_state(crtc);
260 return ERR_PTR(-ENOMEM);
262 state->crtcs[index].state = crtc_state;
263 state->crtcs[index].ptr = crtc;
264 crtc_state->state = state;
266 DRM_DEBUG_ATOMIC("Added [CRTC:%d:%s] %p state to %p\n",
267 crtc->base.id, crtc->name, crtc_state, state);
271 EXPORT_SYMBOL(drm_atomic_get_crtc_state);
274 * drm_atomic_set_mode_for_crtc - set mode for CRTC
275 * @state: the CRTC whose incoming state to update
276 * @mode: kernel-internal mode to use for the CRTC, or NULL to disable
278 * Set a mode (originating from the kernel) on the desired CRTC state. Does
279 * not change any other state properties, including enable, active, or
283 * Zero on success, error code on failure. Cannot return -EDEADLK.
285 int drm_atomic_set_mode_for_crtc(struct drm_crtc_state *state,
286 struct drm_display_mode *mode)
288 struct drm_mode_modeinfo umode;
290 /* Early return for no change. */
291 if (mode && memcmp(&state->mode, mode, sizeof(*mode)) == 0)
294 drm_property_unreference_blob(state->mode_blob);
295 state->mode_blob = NULL;
298 drm_mode_convert_to_umode(&umode, mode);
300 drm_property_create_blob(state->crtc->dev,
303 if (IS_ERR(state->mode_blob))
304 return PTR_ERR(state->mode_blob);
306 drm_mode_copy(&state->mode, mode);
307 state->enable = true;
308 DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n",
311 memset(&state->mode, 0, sizeof(state->mode));
312 state->enable = false;
313 DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n",
319 EXPORT_SYMBOL(drm_atomic_set_mode_for_crtc);
322 * drm_atomic_set_mode_prop_for_crtc - set mode for CRTC
323 * @state: the CRTC whose incoming state to update
324 * @blob: pointer to blob property to use for mode
326 * Set a mode (originating from a blob property) on the desired CRTC state.
327 * This function will take a reference on the blob property for the CRTC state,
328 * and release the reference held on the state's existing mode property, if any
332 * Zero on success, error code on failure. Cannot return -EDEADLK.
334 int drm_atomic_set_mode_prop_for_crtc(struct drm_crtc_state *state,
335 struct drm_property_blob *blob)
337 if (blob == state->mode_blob)
340 drm_property_unreference_blob(state->mode_blob);
341 state->mode_blob = NULL;
344 if (blob->length != sizeof(struct drm_mode_modeinfo) ||
345 drm_mode_convert_umode(&state->mode,
346 (const struct drm_mode_modeinfo *)
350 state->mode_blob = drm_property_reference_blob(blob);
351 state->enable = true;
352 DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n",
353 state->mode.name, state);
355 memset(&state->mode, 0, sizeof(state->mode));
356 state->enable = false;
357 DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n",
363 EXPORT_SYMBOL(drm_atomic_set_mode_prop_for_crtc);
366 * drm_atomic_replace_property_blob - replace a blob property
367 * @blob: a pointer to the member blob to be replaced
368 * @new_blob: the new blob to replace with
369 * @replaced: whether the blob has been replaced
372 * Zero on success, error code on failure
375 drm_atomic_replace_property_blob(struct drm_property_blob **blob,
376 struct drm_property_blob *new_blob,
379 struct drm_property_blob *old_blob = *blob;
381 if (old_blob == new_blob)
385 drm_property_unreference_blob(old_blob);
387 drm_property_reference_blob(new_blob);
395 drm_atomic_replace_property_blob_from_id(struct drm_crtc *crtc,
396 struct drm_property_blob **blob,
398 ssize_t expected_size,
401 struct drm_device *dev = crtc->dev;
402 struct drm_property_blob *new_blob = NULL;
405 new_blob = drm_property_lookup_blob(dev, blob_id);
406 if (new_blob == NULL)
408 if (expected_size > 0 && expected_size != new_blob->length)
412 drm_atomic_replace_property_blob(blob, new_blob, replaced);
418 * drm_atomic_crtc_set_property - set property on CRTC
419 * @crtc: the drm CRTC to set a property on
420 * @state: the state object to update with the new property value
421 * @property: the property to set
422 * @val: the new property value
424 * Use this instead of calling crtc->atomic_set_property directly.
425 * This function handles generic/core properties and calls out to
426 * driver's ->atomic_set_property() for driver properties. To ensure
427 * consistent behavior you must call this function rather than the
428 * driver hook directly.
431 * Zero on success, error code on failure
433 int drm_atomic_crtc_set_property(struct drm_crtc *crtc,
434 struct drm_crtc_state *state, struct drm_property *property,
437 struct drm_device *dev = crtc->dev;
438 struct drm_mode_config *config = &dev->mode_config;
439 bool replaced = false;
442 if (property == config->prop_active)
444 else if (property == config->prop_mode_id) {
445 struct drm_property_blob *mode =
446 drm_property_lookup_blob(dev, val);
447 ret = drm_atomic_set_mode_prop_for_crtc(state, mode);
448 drm_property_unreference_blob(mode);
450 } else if (property == config->degamma_lut_property) {
451 ret = drm_atomic_replace_property_blob_from_id(crtc,
456 state->color_mgmt_changed = replaced;
458 } else if (property == config->ctm_property) {
459 ret = drm_atomic_replace_property_blob_from_id(crtc,
462 sizeof(struct drm_color_ctm),
464 state->color_mgmt_changed = replaced;
466 } else if (property == config->gamma_lut_property) {
467 ret = drm_atomic_replace_property_blob_from_id(crtc,
472 state->color_mgmt_changed = replaced;
474 } else if (crtc->funcs->atomic_set_property)
475 return crtc->funcs->atomic_set_property(crtc, state, property, val);
481 EXPORT_SYMBOL(drm_atomic_crtc_set_property);
484 * drm_atomic_crtc_get_property - get property value from CRTC state
485 * @crtc: the drm CRTC to set a property on
486 * @state: the state object to get the property value from
487 * @property: the property to set
488 * @val: return location for the property value
490 * This function handles generic/core properties and calls out to
491 * driver's ->atomic_get_property() for driver properties. To ensure
492 * consistent behavior you must call this function rather than the
493 * driver hook directly.
496 * Zero on success, error code on failure
499 drm_atomic_crtc_get_property(struct drm_crtc *crtc,
500 const struct drm_crtc_state *state,
501 struct drm_property *property, uint64_t *val)
503 struct drm_device *dev = crtc->dev;
504 struct drm_mode_config *config = &dev->mode_config;
506 if (property == config->prop_active)
507 *val = state->active;
508 else if (property == config->prop_mode_id)
509 *val = (state->mode_blob) ? state->mode_blob->base.id : 0;
510 else if (property == config->degamma_lut_property)
511 *val = (state->degamma_lut) ? state->degamma_lut->base.id : 0;
512 else if (property == config->ctm_property)
513 *val = (state->ctm) ? state->ctm->base.id : 0;
514 else if (property == config->gamma_lut_property)
515 *val = (state->gamma_lut) ? state->gamma_lut->base.id : 0;
516 else if (crtc->funcs->atomic_get_property)
517 return crtc->funcs->atomic_get_property(crtc, state, property, val);
525 * drm_atomic_crtc_check - check crtc state
526 * @crtc: crtc to check
527 * @state: crtc state to check
529 * Provides core sanity checks for crtc state.
532 * Zero on success, error code on failure
534 static int drm_atomic_crtc_check(struct drm_crtc *crtc,
535 struct drm_crtc_state *state)
537 /* NOTE: we explicitly don't enforce constraints such as primary
538 * layer covering entire screen, since that is something we want
539 * to allow (on hw that supports it). For hw that does not, it
540 * should be checked in driver's crtc->atomic_check() vfunc.
542 * TODO: Add generic modeset state checks once we support those.
545 if (state->active && !state->enable) {
546 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active without enabled\n",
547 crtc->base.id, crtc->name);
551 /* The state->enable vs. state->mode_blob checks can be WARN_ON,
552 * as this is a kernel-internal detail that userspace should never
553 * be able to trigger. */
554 if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
555 WARN_ON(state->enable && !state->mode_blob)) {
556 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled without mode blob\n",
557 crtc->base.id, crtc->name);
561 if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
562 WARN_ON(!state->enable && state->mode_blob)) {
563 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] disabled with mode blob\n",
564 crtc->base.id, crtc->name);
569 * Reject event generation for when a CRTC is off and stays off.
570 * It wouldn't be hard to implement this, but userspace has a track
571 * record of happily burning through 100% cpu (or worse, crash) when the
572 * display pipe is suspended. To avoid all that fun just reject updates
573 * that ask for events since likely that indicates a bug in the
574 * compositor's drawing loop. This is consistent with the vblank IOCTL
575 * and legacy page_flip IOCTL which also reject service on a disabled
578 if (state->event && !state->active && !crtc->state->active) {
579 DRM_DEBUG_ATOMIC("[CRTC:%d] requesting event but off\n",
588 * drm_atomic_get_plane_state - get plane state
589 * @state: global atomic state object
590 * @plane: plane to get state object for
592 * This function returns the plane state for the given plane, allocating it if
593 * needed. It will also grab the relevant plane lock to make sure that the state
598 * Either the allocated state or the error code encoded into the pointer. When
599 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
600 * entire atomic sequence must be restarted. All other errors are fatal.
602 struct drm_plane_state *
603 drm_atomic_get_plane_state(struct drm_atomic_state *state,
604 struct drm_plane *plane)
606 int ret, index = drm_plane_index(plane);
607 struct drm_plane_state *plane_state;
609 WARN_ON(!state->acquire_ctx);
611 plane_state = drm_atomic_get_existing_plane_state(state, plane);
615 ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
619 plane_state = plane->funcs->atomic_duplicate_state(plane);
621 return ERR_PTR(-ENOMEM);
623 state->planes[index].state = plane_state;
624 state->planes[index].ptr = plane;
625 plane_state->state = state;
627 DRM_DEBUG_ATOMIC("Added [PLANE:%d:%s] %p state to %p\n",
628 plane->base.id, plane->name, plane_state, state);
630 if (plane_state->crtc) {
631 struct drm_crtc_state *crtc_state;
633 crtc_state = drm_atomic_get_crtc_state(state,
635 if (IS_ERR(crtc_state))
636 return ERR_CAST(crtc_state);
641 EXPORT_SYMBOL(drm_atomic_get_plane_state);
644 * drm_atomic_plane_set_property - set property on plane
645 * @plane: the drm plane to set a property on
646 * @state: the state object to update with the new property value
647 * @property: the property to set
648 * @val: the new property value
650 * Use this instead of calling plane->atomic_set_property directly.
651 * This function handles generic/core properties and calls out to
652 * driver's ->atomic_set_property() for driver properties. To ensure
653 * consistent behavior you must call this function rather than the
654 * driver hook directly.
657 * Zero on success, error code on failure
659 int drm_atomic_plane_set_property(struct drm_plane *plane,
660 struct drm_plane_state *state, struct drm_property *property,
663 struct drm_device *dev = plane->dev;
664 struct drm_mode_config *config = &dev->mode_config;
666 if (property == config->prop_fb_id) {
667 struct drm_framebuffer *fb = drm_framebuffer_lookup(dev, val);
668 drm_atomic_set_fb_for_plane(state, fb);
670 drm_framebuffer_unreference(fb);
671 } else if (property == config->prop_crtc_id) {
672 struct drm_crtc *crtc = drm_crtc_find(dev, val);
673 return drm_atomic_set_crtc_for_plane(state, crtc);
674 } else if (property == config->prop_crtc_x) {
675 state->crtc_x = U642I64(val);
676 } else if (property == config->prop_crtc_y) {
677 state->crtc_y = U642I64(val);
678 } else if (property == config->prop_crtc_w) {
680 } else if (property == config->prop_crtc_h) {
682 } else if (property == config->prop_src_x) {
684 } else if (property == config->prop_src_y) {
686 } else if (property == config->prop_src_w) {
688 } else if (property == config->prop_src_h) {
690 } else if (property == config->rotation_property) {
691 state->rotation = val;
692 } else if (plane->funcs->atomic_set_property) {
693 return plane->funcs->atomic_set_property(plane, state,
701 EXPORT_SYMBOL(drm_atomic_plane_set_property);
704 * drm_atomic_plane_get_property - get property value from plane state
705 * @plane: the drm plane to set a property on
706 * @state: the state object to get the property value from
707 * @property: the property to set
708 * @val: return location for the property value
710 * This function handles generic/core properties and calls out to
711 * driver's ->atomic_get_property() for driver properties. To ensure
712 * consistent behavior you must call this function rather than the
713 * driver hook directly.
716 * Zero on success, error code on failure
719 drm_atomic_plane_get_property(struct drm_plane *plane,
720 const struct drm_plane_state *state,
721 struct drm_property *property, uint64_t *val)
723 struct drm_device *dev = plane->dev;
724 struct drm_mode_config *config = &dev->mode_config;
726 if (property == config->prop_fb_id) {
727 *val = (state->fb) ? state->fb->base.id : 0;
728 } else if (property == config->prop_crtc_id) {
729 *val = (state->crtc) ? state->crtc->base.id : 0;
730 } else if (property == config->prop_crtc_x) {
731 *val = I642U64(state->crtc_x);
732 } else if (property == config->prop_crtc_y) {
733 *val = I642U64(state->crtc_y);
734 } else if (property == config->prop_crtc_w) {
735 *val = state->crtc_w;
736 } else if (property == config->prop_crtc_h) {
737 *val = state->crtc_h;
738 } else if (property == config->prop_src_x) {
740 } else if (property == config->prop_src_y) {
742 } else if (property == config->prop_src_w) {
744 } else if (property == config->prop_src_h) {
746 } else if (property == config->rotation_property) {
747 *val = state->rotation;
748 } else if (plane->funcs->atomic_get_property) {
749 return plane->funcs->atomic_get_property(plane, state, property, val);
758 plane_switching_crtc(struct drm_atomic_state *state,
759 struct drm_plane *plane,
760 struct drm_plane_state *plane_state)
762 if (!plane->state->crtc || !plane_state->crtc)
765 if (plane->state->crtc == plane_state->crtc)
768 /* This could be refined, but currently there's no helper or driver code
769 * to implement direct switching of active planes nor userspace to take
770 * advantage of more direct plane switching without the intermediate
777 * drm_atomic_plane_check - check plane state
778 * @plane: plane to check
779 * @state: plane state to check
781 * Provides core sanity checks for plane state.
784 * Zero on success, error code on failure
786 static int drm_atomic_plane_check(struct drm_plane *plane,
787 struct drm_plane_state *state)
789 unsigned int fb_width, fb_height;
792 /* either *both* CRTC and FB must be set, or neither */
793 if (WARN_ON(state->crtc && !state->fb)) {
794 DRM_DEBUG_ATOMIC("CRTC set but no FB\n");
796 } else if (WARN_ON(state->fb && !state->crtc)) {
797 DRM_DEBUG_ATOMIC("FB set but no CRTC\n");
801 /* if disabled, we don't care about the rest of the state: */
805 /* Check whether this plane is usable on this CRTC */
806 if (!(plane->possible_crtcs & drm_crtc_mask(state->crtc))) {
807 DRM_DEBUG_ATOMIC("Invalid crtc for plane\n");
811 /* Check whether this plane supports the fb pixel format. */
812 ret = drm_plane_check_pixel_format(plane, state->fb->pixel_format);
814 DRM_DEBUG_ATOMIC("Invalid pixel format %s\n",
815 drm_get_format_name(state->fb->pixel_format));
819 /* Give drivers some help against integer overflows */
820 if (state->crtc_w > INT_MAX ||
821 state->crtc_x > INT_MAX - (int32_t) state->crtc_w ||
822 state->crtc_h > INT_MAX ||
823 state->crtc_y > INT_MAX - (int32_t) state->crtc_h) {
824 DRM_DEBUG_ATOMIC("Invalid CRTC coordinates %ux%u+%d+%d\n",
825 state->crtc_w, state->crtc_h,
826 state->crtc_x, state->crtc_y);
830 fb_width = state->fb->width << 16;
831 fb_height = state->fb->height << 16;
833 /* Make sure source coordinates are inside the fb. */
834 if (state->src_w > fb_width ||
835 state->src_x > fb_width - state->src_w ||
836 state->src_h > fb_height ||
837 state->src_y > fb_height - state->src_h) {
838 DRM_DEBUG_ATOMIC("Invalid source coordinates "
839 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
840 state->src_w >> 16, ((state->src_w & 0xffff) * 15625) >> 10,
841 state->src_h >> 16, ((state->src_h & 0xffff) * 15625) >> 10,
842 state->src_x >> 16, ((state->src_x & 0xffff) * 15625) >> 10,
843 state->src_y >> 16, ((state->src_y & 0xffff) * 15625) >> 10);
847 if (plane_switching_crtc(state->state, plane, state)) {
848 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] switching CRTC directly\n",
849 plane->base.id, plane->name);
857 * drm_atomic_get_connector_state - get connector state
858 * @state: global atomic state object
859 * @connector: connector to get state object for
861 * This function returns the connector state for the given connector,
862 * allocating it if needed. It will also grab the relevant connector lock to
863 * make sure that the state is consistent.
867 * Either the allocated state or the error code encoded into the pointer. When
868 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
869 * entire atomic sequence must be restarted. All other errors are fatal.
871 struct drm_connector_state *
872 drm_atomic_get_connector_state(struct drm_atomic_state *state,
873 struct drm_connector *connector)
876 struct drm_mode_config *config = &connector->dev->mode_config;
877 struct drm_connector_state *connector_state;
879 WARN_ON(!state->acquire_ctx);
881 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
885 index = drm_connector_index(connector);
887 if (index >= state->num_connector) {
888 struct __drm_connnectors_state *c;
889 int alloc = max(index + 1, config->num_connector);
891 c = krealloc(state->connectors, alloc * sizeof(*state->connectors), GFP_KERNEL);
893 return ERR_PTR(-ENOMEM);
895 state->connectors = c;
896 memset(&state->connectors[state->num_connector], 0,
897 sizeof(*state->connectors) * (alloc - state->num_connector));
899 state->num_connector = alloc;
902 if (state->connectors[index].state)
903 return state->connectors[index].state;
905 connector_state = connector->funcs->atomic_duplicate_state(connector);
906 if (!connector_state)
907 return ERR_PTR(-ENOMEM);
909 drm_connector_reference(connector);
910 state->connectors[index].state = connector_state;
911 state->connectors[index].ptr = connector;
912 connector_state->state = state;
914 DRM_DEBUG_ATOMIC("Added [CONNECTOR:%d] %p state to %p\n",
915 connector->base.id, connector_state, state);
917 if (connector_state->crtc) {
918 struct drm_crtc_state *crtc_state;
920 crtc_state = drm_atomic_get_crtc_state(state,
921 connector_state->crtc);
922 if (IS_ERR(crtc_state))
923 return ERR_CAST(crtc_state);
926 return connector_state;
928 EXPORT_SYMBOL(drm_atomic_get_connector_state);
931 * drm_atomic_connector_set_property - set property on connector.
932 * @connector: the drm connector to set a property on
933 * @state: the state object to update with the new property value
934 * @property: the property to set
935 * @val: the new property value
937 * Use this instead of calling connector->atomic_set_property directly.
938 * This function handles generic/core properties and calls out to
939 * driver's ->atomic_set_property() for driver properties. To ensure
940 * consistent behavior you must call this function rather than the
941 * driver hook directly.
944 * Zero on success, error code on failure
946 int drm_atomic_connector_set_property(struct drm_connector *connector,
947 struct drm_connector_state *state, struct drm_property *property,
950 struct drm_device *dev = connector->dev;
951 struct drm_mode_config *config = &dev->mode_config;
953 if (property == config->prop_crtc_id) {
954 struct drm_crtc *crtc = drm_crtc_find(dev, val);
955 return drm_atomic_set_crtc_for_connector(state, crtc);
956 } else if (property == config->dpms_property) {
957 /* setting DPMS property requires special handling, which
958 * is done in legacy setprop path for us. Disallow (for
959 * now?) atomic writes to DPMS property:
962 } else if (connector->funcs->atomic_set_property) {
963 return connector->funcs->atomic_set_property(connector,
964 state, property, val);
969 EXPORT_SYMBOL(drm_atomic_connector_set_property);
972 * drm_atomic_connector_get_property - get property value from connector state
973 * @connector: the drm connector to set a property on
974 * @state: the state object to get the property value from
975 * @property: the property to set
976 * @val: return location for the property value
978 * This function handles generic/core properties and calls out to
979 * driver's ->atomic_get_property() for driver properties. To ensure
980 * consistent behavior you must call this function rather than the
981 * driver hook directly.
984 * Zero on success, error code on failure
987 drm_atomic_connector_get_property(struct drm_connector *connector,
988 const struct drm_connector_state *state,
989 struct drm_property *property, uint64_t *val)
991 struct drm_device *dev = connector->dev;
992 struct drm_mode_config *config = &dev->mode_config;
994 if (property == config->prop_crtc_id) {
995 *val = (state->crtc) ? state->crtc->base.id : 0;
996 } else if (property == config->dpms_property) {
997 *val = connector->dpms;
998 } else if (connector->funcs->atomic_get_property) {
999 return connector->funcs->atomic_get_property(connector,
1000 state, property, val);
1008 int drm_atomic_get_property(struct drm_mode_object *obj,
1009 struct drm_property *property, uint64_t *val)
1011 struct drm_device *dev = property->dev;
1014 switch (obj->type) {
1015 case DRM_MODE_OBJECT_CONNECTOR: {
1016 struct drm_connector *connector = obj_to_connector(obj);
1017 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
1018 ret = drm_atomic_connector_get_property(connector,
1019 connector->state, property, val);
1022 case DRM_MODE_OBJECT_CRTC: {
1023 struct drm_crtc *crtc = obj_to_crtc(obj);
1024 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
1025 ret = drm_atomic_crtc_get_property(crtc,
1026 crtc->state, property, val);
1029 case DRM_MODE_OBJECT_PLANE: {
1030 struct drm_plane *plane = obj_to_plane(obj);
1031 WARN_ON(!drm_modeset_is_locked(&plane->mutex));
1032 ret = drm_atomic_plane_get_property(plane,
1033 plane->state, property, val);
1045 * drm_atomic_set_crtc_for_plane - set crtc for plane
1046 * @plane_state: the plane whose incoming state to update
1047 * @crtc: crtc to use for the plane
1049 * Changing the assigned crtc for a plane requires us to grab the lock and state
1050 * for the new crtc, as needed. This function takes care of all these details
1051 * besides updating the pointer in the state object itself.
1054 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1055 * then the w/w mutex code has detected a deadlock and the entire atomic
1056 * sequence must be restarted. All other errors are fatal.
1059 drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
1060 struct drm_crtc *crtc)
1062 struct drm_plane *plane = plane_state->plane;
1063 struct drm_crtc_state *crtc_state;
1065 if (plane_state->crtc) {
1066 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
1068 if (WARN_ON(IS_ERR(crtc_state)))
1069 return PTR_ERR(crtc_state);
1071 crtc_state->plane_mask &= ~(1 << drm_plane_index(plane));
1074 plane_state->crtc = crtc;
1077 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
1079 if (IS_ERR(crtc_state))
1080 return PTR_ERR(crtc_state);
1081 crtc_state->plane_mask |= (1 << drm_plane_index(plane));
1085 DRM_DEBUG_ATOMIC("Link plane state %p to [CRTC:%d:%s]\n",
1086 plane_state, crtc->base.id, crtc->name);
1088 DRM_DEBUG_ATOMIC("Link plane state %p to [NOCRTC]\n",
1093 EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane);
1096 * drm_atomic_set_fb_for_plane - set framebuffer for plane
1097 * @plane_state: atomic state object for the plane
1098 * @fb: fb to use for the plane
1100 * Changing the assigned framebuffer for a plane requires us to grab a reference
1101 * to the new fb and drop the reference to the old fb, if there is one. This
1102 * function takes care of all these details besides updating the pointer in the
1103 * state object itself.
1106 drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
1107 struct drm_framebuffer *fb)
1109 if (plane_state->fb)
1110 drm_framebuffer_unreference(plane_state->fb);
1112 drm_framebuffer_reference(fb);
1113 plane_state->fb = fb;
1116 DRM_DEBUG_ATOMIC("Set [FB:%d] for plane state %p\n",
1117 fb->base.id, plane_state);
1119 DRM_DEBUG_ATOMIC("Set [NOFB] for plane state %p\n",
1122 EXPORT_SYMBOL(drm_atomic_set_fb_for_plane);
1125 * drm_atomic_set_crtc_for_connector - set crtc for connector
1126 * @conn_state: atomic state object for the connector
1127 * @crtc: crtc to use for the connector
1129 * Changing the assigned crtc for a connector requires us to grab the lock and
1130 * state for the new crtc, as needed. This function takes care of all these
1131 * details besides updating the pointer in the state object itself.
1134 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1135 * then the w/w mutex code has detected a deadlock and the entire atomic
1136 * sequence must be restarted. All other errors are fatal.
1139 drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
1140 struct drm_crtc *crtc)
1142 struct drm_crtc_state *crtc_state;
1144 if (conn_state->crtc == crtc)
1147 if (conn_state->crtc) {
1148 crtc_state = drm_atomic_get_existing_crtc_state(conn_state->state,
1151 crtc_state->connector_mask &=
1152 ~(1 << drm_connector_index(conn_state->connector));
1154 drm_connector_unreference(conn_state->connector);
1155 conn_state->crtc = NULL;
1159 crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc);
1160 if (IS_ERR(crtc_state))
1161 return PTR_ERR(crtc_state);
1163 crtc_state->connector_mask |=
1164 1 << drm_connector_index(conn_state->connector);
1166 drm_connector_reference(conn_state->connector);
1167 conn_state->crtc = crtc;
1169 DRM_DEBUG_ATOMIC("Link connector state %p to [CRTC:%d:%s]\n",
1170 conn_state, crtc->base.id, crtc->name);
1172 DRM_DEBUG_ATOMIC("Link connector state %p to [NOCRTC]\n",
1178 EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector);
1181 * drm_atomic_add_affected_connectors - add connectors for crtc
1182 * @state: atomic state
1185 * This function walks the current configuration and adds all connectors
1186 * currently using @crtc to the atomic configuration @state. Note that this
1187 * function must acquire the connection mutex. This can potentially cause
1188 * unneeded seralization if the update is just for the planes on one crtc. Hence
1189 * drivers and helpers should only call this when really needed (e.g. when a
1190 * full modeset needs to happen due to some change).
1193 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1194 * then the w/w mutex code has detected a deadlock and the entire atomic
1195 * sequence must be restarted. All other errors are fatal.
1198 drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
1199 struct drm_crtc *crtc)
1201 struct drm_mode_config *config = &state->dev->mode_config;
1202 struct drm_connector *connector;
1203 struct drm_connector_state *conn_state;
1206 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
1210 DRM_DEBUG_ATOMIC("Adding all current connectors for [CRTC:%d:%s] to %p\n",
1211 crtc->base.id, crtc->name, state);
1214 * Changed connectors are already in @state, so only need to look at the
1215 * current configuration.
1217 drm_for_each_connector(connector, state->dev) {
1218 if (connector->state->crtc != crtc)
1221 conn_state = drm_atomic_get_connector_state(state, connector);
1222 if (IS_ERR(conn_state))
1223 return PTR_ERR(conn_state);
1228 EXPORT_SYMBOL(drm_atomic_add_affected_connectors);
1231 * drm_atomic_add_affected_planes - add planes for crtc
1232 * @state: atomic state
1235 * This function walks the current configuration and adds all planes
1236 * currently used by @crtc to the atomic configuration @state. This is useful
1237 * when an atomic commit also needs to check all currently enabled plane on
1238 * @crtc, e.g. when changing the mode. It's also useful when re-enabling a CRTC
1239 * to avoid special code to force-enable all planes.
1241 * Since acquiring a plane state will always also acquire the w/w mutex of the
1242 * current CRTC for that plane (if there is any) adding all the plane states for
1243 * a CRTC will not reduce parallism of atomic updates.
1246 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1247 * then the w/w mutex code has detected a deadlock and the entire atomic
1248 * sequence must be restarted. All other errors are fatal.
1251 drm_atomic_add_affected_planes(struct drm_atomic_state *state,
1252 struct drm_crtc *crtc)
1254 struct drm_plane *plane;
1256 WARN_ON(!drm_atomic_get_existing_crtc_state(state, crtc));
1258 drm_for_each_plane_mask(plane, state->dev, crtc->state->plane_mask) {
1259 struct drm_plane_state *plane_state =
1260 drm_atomic_get_plane_state(state, plane);
1262 if (IS_ERR(plane_state))
1263 return PTR_ERR(plane_state);
1267 EXPORT_SYMBOL(drm_atomic_add_affected_planes);
1270 * drm_atomic_legacy_backoff - locking backoff for legacy ioctls
1271 * @state: atomic state
1273 * This function should be used by legacy entry points which don't understand
1274 * -EDEADLK semantics. For simplicity this one will grab all modeset locks after
1275 * the slowpath completed.
1277 void drm_atomic_legacy_backoff(struct drm_atomic_state *state)
1282 drm_modeset_backoff(state->acquire_ctx);
1284 ret = drm_modeset_lock_all_ctx(state->dev, state->acquire_ctx);
1288 EXPORT_SYMBOL(drm_atomic_legacy_backoff);
1291 * drm_atomic_check_only - check whether a given config would work
1292 * @state: atomic configuration to check
1294 * Note that this function can return -EDEADLK if the driver needed to acquire
1295 * more locks but encountered a deadlock. The caller must then do the usual w/w
1296 * backoff dance and restart. All other errors are fatal.
1299 * 0 on success, negative error code on failure.
1301 int drm_atomic_check_only(struct drm_atomic_state *state)
1303 struct drm_device *dev = state->dev;
1304 struct drm_mode_config *config = &dev->mode_config;
1305 struct drm_plane *plane;
1306 struct drm_plane_state *plane_state;
1307 struct drm_crtc *crtc;
1308 struct drm_crtc_state *crtc_state;
1311 DRM_DEBUG_ATOMIC("checking %p\n", state);
1313 for_each_plane_in_state(state, plane, plane_state, i) {
1314 ret = drm_atomic_plane_check(plane, plane_state);
1316 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic core check failed\n",
1317 plane->base.id, plane->name);
1322 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1323 ret = drm_atomic_crtc_check(crtc, crtc_state);
1325 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic core check failed\n",
1326 crtc->base.id, crtc->name);
1331 if (config->funcs->atomic_check)
1332 ret = config->funcs->atomic_check(state->dev, state);
1334 if (!state->allow_modeset) {
1335 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1336 if (drm_atomic_crtc_needs_modeset(crtc_state)) {
1337 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] requires full modeset\n",
1338 crtc->base.id, crtc->name);
1346 EXPORT_SYMBOL(drm_atomic_check_only);
1349 * drm_atomic_commit - commit configuration atomically
1350 * @state: atomic configuration to check
1352 * Note that this function can return -EDEADLK if the driver needed to acquire
1353 * more locks but encountered a deadlock. The caller must then do the usual w/w
1354 * backoff dance and restart. All other errors are fatal.
1356 * Also note that on successful execution ownership of @state is transferred
1357 * from the caller of this function to the function itself. The caller must not
1358 * free or in any other way access @state. If the function fails then the caller
1359 * must clean up @state itself.
1362 * 0 on success, negative error code on failure.
1364 int drm_atomic_commit(struct drm_atomic_state *state)
1366 struct drm_mode_config *config = &state->dev->mode_config;
1369 ret = drm_atomic_check_only(state);
1373 DRM_DEBUG_ATOMIC("commiting %p\n", state);
1375 return config->funcs->atomic_commit(state->dev, state, false);
1377 EXPORT_SYMBOL(drm_atomic_commit);
1380 * drm_atomic_nonblocking_commit - atomic&nonblocking configuration commit
1381 * @state: atomic configuration to check
1383 * Note that this function can return -EDEADLK if the driver needed to acquire
1384 * more locks but encountered a deadlock. The caller must then do the usual w/w
1385 * backoff dance and restart. All other errors are fatal.
1387 * Also note that on successful execution ownership of @state is transferred
1388 * from the caller of this function to the function itself. The caller must not
1389 * free or in any other way access @state. If the function fails then the caller
1390 * must clean up @state itself.
1393 * 0 on success, negative error code on failure.
1395 int drm_atomic_nonblocking_commit(struct drm_atomic_state *state)
1397 struct drm_mode_config *config = &state->dev->mode_config;
1400 ret = drm_atomic_check_only(state);
1404 DRM_DEBUG_ATOMIC("commiting %p nonblocking\n", state);
1406 return config->funcs->atomic_commit(state->dev, state, true);
1408 EXPORT_SYMBOL(drm_atomic_nonblocking_commit);
1411 * The big monstor ioctl
1414 static struct drm_pending_vblank_event *create_vblank_event(
1415 struct drm_device *dev, struct drm_file *file_priv,
1416 struct fence *fence, uint64_t user_data)
1418 struct drm_pending_vblank_event *e = NULL;
1421 e = kzalloc(sizeof *e, GFP_KERNEL);
1425 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
1426 e->event.base.length = sizeof(e->event);
1427 e->event.user_data = user_data;
1430 ret = drm_event_reserve_init(dev, file_priv, &e->base,
1438 e->base.fence = fence;
1443 static int atomic_set_prop(struct drm_atomic_state *state,
1444 struct drm_mode_object *obj, struct drm_property *prop,
1445 uint64_t prop_value)
1447 struct drm_mode_object *ref;
1450 if (!drm_property_change_valid_get(prop, prop_value, &ref))
1453 switch (obj->type) {
1454 case DRM_MODE_OBJECT_CONNECTOR: {
1455 struct drm_connector *connector = obj_to_connector(obj);
1456 struct drm_connector_state *connector_state;
1458 connector_state = drm_atomic_get_connector_state(state, connector);
1459 if (IS_ERR(connector_state)) {
1460 ret = PTR_ERR(connector_state);
1464 ret = drm_atomic_connector_set_property(connector,
1465 connector_state, prop, prop_value);
1468 case DRM_MODE_OBJECT_CRTC: {
1469 struct drm_crtc *crtc = obj_to_crtc(obj);
1470 struct drm_crtc_state *crtc_state;
1472 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1473 if (IS_ERR(crtc_state)) {
1474 ret = PTR_ERR(crtc_state);
1478 ret = drm_atomic_crtc_set_property(crtc,
1479 crtc_state, prop, prop_value);
1482 case DRM_MODE_OBJECT_PLANE: {
1483 struct drm_plane *plane = obj_to_plane(obj);
1484 struct drm_plane_state *plane_state;
1486 plane_state = drm_atomic_get_plane_state(state, plane);
1487 if (IS_ERR(plane_state)) {
1488 ret = PTR_ERR(plane_state);
1492 ret = drm_atomic_plane_set_property(plane,
1493 plane_state, prop, prop_value);
1501 drm_property_change_valid_put(prop, ref);
1506 * drm_atomic_clean_old_fb -- Unset old_fb pointers and set plane->fb pointers.
1508 * @dev: drm device to check.
1509 * @plane_mask: plane mask for planes that were updated.
1510 * @ret: return value, can be -EDEADLK for a retry.
1512 * Before doing an update plane->old_fb is set to plane->fb,
1513 * but before dropping the locks old_fb needs to be set to NULL
1514 * and plane->fb updated. This is a common operation for each
1515 * atomic update, so this call is split off as a helper.
1517 void drm_atomic_clean_old_fb(struct drm_device *dev,
1518 unsigned plane_mask,
1521 struct drm_plane *plane;
1523 /* if succeeded, fixup legacy plane crtc/fb ptrs before dropping
1524 * locks (ie. while it is still safe to deref plane->state). We
1525 * need to do this here because the driver entry points cannot
1526 * distinguish between legacy and atomic ioctls.
1528 drm_for_each_plane_mask(plane, dev, plane_mask) {
1530 struct drm_framebuffer *new_fb = plane->state->fb;
1532 drm_framebuffer_reference(new_fb);
1534 plane->crtc = plane->state->crtc;
1537 drm_framebuffer_unreference(plane->old_fb);
1539 plane->old_fb = NULL;
1542 EXPORT_SYMBOL(drm_atomic_clean_old_fb);
1544 int drm_mode_atomic_ioctl(struct drm_device *dev,
1545 void *data, struct drm_file *file_priv)
1547 struct drm_mode_atomic *arg = data;
1548 uint32_t __user *objs_ptr = (uint32_t __user *)(unsigned long)(arg->objs_ptr);
1549 uint32_t __user *count_props_ptr = (uint32_t __user *)(unsigned long)(arg->count_props_ptr);
1550 uint32_t __user *props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
1551 uint64_t __user *prop_values_ptr = (uint64_t __user *)(unsigned long)(arg->prop_values_ptr);
1552 unsigned int copied_objs, copied_props;
1553 struct drm_atomic_state *state;
1554 struct drm_modeset_acquire_ctx ctx;
1555 struct drm_plane *plane;
1556 struct drm_crtc *crtc;
1557 struct drm_crtc_state *crtc_state;
1558 unsigned plane_mask;
1562 /* disallow for drivers not supporting atomic: */
1563 if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
1566 /* disallow for userspace that has not enabled atomic cap (even
1567 * though this may be a bit overkill, since legacy userspace
1568 * wouldn't know how to call this ioctl)
1570 if (!file_priv->atomic)
1573 if (arg->flags & ~DRM_MODE_ATOMIC_FLAGS)
1579 if ((arg->flags & DRM_MODE_PAGE_FLIP_ASYNC) &&
1580 !dev->mode_config.async_page_flip)
1583 /* can't test and expect an event at the same time. */
1584 if ((arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) &&
1585 (arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
1588 drm_modeset_acquire_init(&ctx, 0);
1590 state = drm_atomic_state_alloc(dev);
1594 state->acquire_ctx = &ctx;
1595 state->allow_modeset = !!(arg->flags & DRM_MODE_ATOMIC_ALLOW_MODESET);
1602 for (i = 0; i < arg->count_objs; i++) {
1603 uint32_t obj_id, count_props;
1604 struct drm_mode_object *obj;
1606 if (get_user(obj_id, objs_ptr + copied_objs)) {
1611 obj = drm_mode_object_find(dev, obj_id, DRM_MODE_OBJECT_ANY);
1617 if (!obj->properties) {
1618 drm_mode_object_unreference(obj);
1623 if (get_user(count_props, count_props_ptr + copied_objs)) {
1624 drm_mode_object_unreference(obj);
1631 for (j = 0; j < count_props; j++) {
1633 uint64_t prop_value;
1634 struct drm_property *prop;
1636 if (get_user(prop_id, props_ptr + copied_props)) {
1637 drm_mode_object_unreference(obj);
1642 prop = drm_property_find(dev, prop_id);
1644 drm_mode_object_unreference(obj);
1649 if (copy_from_user(&prop_value,
1650 prop_values_ptr + copied_props,
1651 sizeof(prop_value))) {
1652 drm_mode_object_unreference(obj);
1657 ret = atomic_set_prop(state, obj, prop, prop_value);
1659 drm_mode_object_unreference(obj);
1666 if (obj->type == DRM_MODE_OBJECT_PLANE && count_props &&
1667 !(arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)) {
1668 plane = obj_to_plane(obj);
1669 plane_mask |= (1 << drm_plane_index(plane));
1670 plane->old_fb = plane->fb;
1672 drm_mode_object_unreference(obj);
1675 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
1676 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1677 struct drm_pending_vblank_event *e;
1679 e = create_vblank_event(dev, file_priv, NULL,
1686 crtc_state->event = e;
1690 if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) {
1692 * Unlike commit, check_only does not clean up state.
1693 * Below we call drm_atomic_state_free for it.
1695 ret = drm_atomic_check_only(state);
1696 } else if (arg->flags & DRM_MODE_ATOMIC_NONBLOCK) {
1697 ret = drm_atomic_nonblocking_commit(state);
1699 ret = drm_atomic_commit(state);
1703 drm_atomic_clean_old_fb(dev, plane_mask, ret);
1705 if (ret && arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
1707 * TEST_ONLY and PAGE_FLIP_EVENT are mutually exclusive,
1708 * if they weren't, this code should be called on success
1709 * for TEST_ONLY too.
1712 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1713 if (!crtc_state->event)
1716 drm_event_cancel_free(dev, &crtc_state->event->base);
1720 if (ret == -EDEADLK) {
1721 drm_atomic_state_clear(state);
1722 drm_modeset_backoff(&ctx);
1726 if (ret || arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)
1727 drm_atomic_state_free(state);
1729 drm_modeset_drop_locks(&ctx);
1730 drm_modeset_acquire_fini(&ctx);