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);
48 kfree(state->crtc_states);
50 kfree(state->plane_states);
52 EXPORT_SYMBOL(drm_atomic_state_default_release);
55 * drm_atomic_state_init - init new atomic state
57 * @state: atomic state
59 * Default implementation for filling in a new atomic state.
60 * This is useful for drivers that subclass the atomic state.
63 drm_atomic_state_init(struct drm_device *dev, struct drm_atomic_state *state)
65 /* TODO legacy paths should maybe do a better job about
66 * setting this appropriately?
68 state->allow_modeset = true;
70 state->crtcs = kcalloc(dev->mode_config.num_crtc,
71 sizeof(*state->crtcs), GFP_KERNEL);
74 state->crtc_states = kcalloc(dev->mode_config.num_crtc,
75 sizeof(*state->crtc_states), GFP_KERNEL);
76 if (!state->crtc_states)
78 state->planes = kcalloc(dev->mode_config.num_total_plane,
79 sizeof(*state->planes), GFP_KERNEL);
82 state->plane_states = kcalloc(dev->mode_config.num_total_plane,
83 sizeof(*state->plane_states), GFP_KERNEL);
84 if (!state->plane_states)
89 DRM_DEBUG_ATOMIC("Allocated atomic state %p\n", state);
93 drm_atomic_state_default_release(state);
96 EXPORT_SYMBOL(drm_atomic_state_init);
99 * drm_atomic_state_alloc - allocate atomic state
102 * This allocates an empty atomic state to track updates.
104 struct drm_atomic_state *
105 drm_atomic_state_alloc(struct drm_device *dev)
107 struct drm_mode_config *config = &dev->mode_config;
108 struct drm_atomic_state *state;
110 if (!config->funcs->atomic_state_alloc) {
111 state = kzalloc(sizeof(*state), GFP_KERNEL);
114 if (drm_atomic_state_init(dev, state) < 0) {
121 return config->funcs->atomic_state_alloc(dev);
123 EXPORT_SYMBOL(drm_atomic_state_alloc);
126 * drm_atomic_state_default_clear - clear base atomic state
127 * @state: atomic state
129 * Default implementation for clearing atomic state.
130 * This is useful for drivers that subclass the atomic state.
132 void drm_atomic_state_default_clear(struct drm_atomic_state *state)
134 struct drm_device *dev = state->dev;
135 struct drm_mode_config *config = &dev->mode_config;
138 DRM_DEBUG_ATOMIC("Clearing atomic state %p\n", state);
140 for (i = 0; i < state->num_connector; i++) {
141 struct drm_connector *connector = state->connectors[i].ptr;
146 connector->funcs->atomic_destroy_state(connector,
147 state->connectors[i].state);
148 state->connectors[i].ptr = NULL;
149 state->connectors[i].state = NULL;
150 drm_connector_unreference(connector);
153 for (i = 0; i < config->num_crtc; i++) {
154 struct drm_crtc *crtc = state->crtcs[i];
159 crtc->funcs->atomic_destroy_state(crtc,
160 state->crtc_states[i]);
161 state->crtcs[i] = NULL;
162 state->crtc_states[i] = NULL;
165 for (i = 0; i < config->num_total_plane; i++) {
166 struct drm_plane *plane = state->planes[i];
171 plane->funcs->atomic_destroy_state(plane,
172 state->plane_states[i]);
173 state->planes[i] = NULL;
174 state->plane_states[i] = NULL;
177 EXPORT_SYMBOL(drm_atomic_state_default_clear);
180 * drm_atomic_state_clear - clear state object
181 * @state: atomic state
183 * When the w/w mutex algorithm detects a deadlock we need to back off and drop
184 * all locks. So someone else could sneak in and change the current modeset
185 * configuration. Which means that all the state assembled in @state is no
186 * longer an atomic update to the current state, but to some arbitrary earlier
187 * state. Which could break assumptions the driver's ->atomic_check likely
190 * Hence we must clear all cached state and completely start over, using this
193 void drm_atomic_state_clear(struct drm_atomic_state *state)
195 struct drm_device *dev = state->dev;
196 struct drm_mode_config *config = &dev->mode_config;
198 if (config->funcs->atomic_state_clear)
199 config->funcs->atomic_state_clear(state);
201 drm_atomic_state_default_clear(state);
203 EXPORT_SYMBOL(drm_atomic_state_clear);
206 * drm_atomic_state_free - free all memory for an atomic state
207 * @state: atomic state to deallocate
209 * This frees all memory associated with an atomic state, including all the
210 * per-object state for planes, crtcs and connectors.
212 void drm_atomic_state_free(struct drm_atomic_state *state)
214 struct drm_device *dev;
215 struct drm_mode_config *config;
221 config = &dev->mode_config;
223 drm_atomic_state_clear(state);
225 DRM_DEBUG_ATOMIC("Freeing atomic state %p\n", state);
227 if (config->funcs->atomic_state_free) {
228 config->funcs->atomic_state_free(state);
230 drm_atomic_state_default_release(state);
234 EXPORT_SYMBOL(drm_atomic_state_free);
237 * drm_atomic_get_crtc_state - get crtc state
238 * @state: global atomic state object
239 * @crtc: crtc to get state object for
241 * This function returns the crtc state for the given crtc, allocating it if
242 * needed. It will also grab the relevant crtc lock to make sure that the state
247 * Either the allocated state or the error code encoded into the pointer. When
248 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
249 * entire atomic sequence must be restarted. All other errors are fatal.
251 struct drm_crtc_state *
252 drm_atomic_get_crtc_state(struct drm_atomic_state *state,
253 struct drm_crtc *crtc)
255 int ret, index = drm_crtc_index(crtc);
256 struct drm_crtc_state *crtc_state;
258 WARN_ON(!state->acquire_ctx);
260 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
264 ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx);
268 crtc_state = crtc->funcs->atomic_duplicate_state(crtc);
270 return ERR_PTR(-ENOMEM);
272 state->crtc_states[index] = crtc_state;
273 state->crtcs[index] = crtc;
274 crtc_state->state = state;
276 DRM_DEBUG_ATOMIC("Added [CRTC:%d:%s] %p state to %p\n",
277 crtc->base.id, crtc->name, crtc_state, state);
281 EXPORT_SYMBOL(drm_atomic_get_crtc_state);
284 * drm_atomic_set_mode_for_crtc - set mode for CRTC
285 * @state: the CRTC whose incoming state to update
286 * @mode: kernel-internal mode to use for the CRTC, or NULL to disable
288 * Set a mode (originating from the kernel) on the desired CRTC state. Does
289 * not change any other state properties, including enable, active, or
293 * Zero on success, error code on failure. Cannot return -EDEADLK.
295 int drm_atomic_set_mode_for_crtc(struct drm_crtc_state *state,
296 struct drm_display_mode *mode)
298 struct drm_mode_modeinfo umode;
300 /* Early return for no change. */
301 if (mode && memcmp(&state->mode, mode, sizeof(*mode)) == 0)
304 drm_property_unreference_blob(state->mode_blob);
305 state->mode_blob = NULL;
308 drm_mode_convert_to_umode(&umode, mode);
310 drm_property_create_blob(state->crtc->dev,
313 if (IS_ERR(state->mode_blob))
314 return PTR_ERR(state->mode_blob);
316 drm_mode_copy(&state->mode, mode);
317 state->enable = true;
318 DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n",
321 memset(&state->mode, 0, sizeof(state->mode));
322 state->enable = false;
323 DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n",
329 EXPORT_SYMBOL(drm_atomic_set_mode_for_crtc);
332 * drm_atomic_set_mode_prop_for_crtc - set mode for CRTC
333 * @state: the CRTC whose incoming state to update
334 * @blob: pointer to blob property to use for mode
336 * Set a mode (originating from a blob property) on the desired CRTC state.
337 * This function will take a reference on the blob property for the CRTC state,
338 * and release the reference held on the state's existing mode property, if any
342 * Zero on success, error code on failure. Cannot return -EDEADLK.
344 int drm_atomic_set_mode_prop_for_crtc(struct drm_crtc_state *state,
345 struct drm_property_blob *blob)
347 if (blob == state->mode_blob)
350 drm_property_unreference_blob(state->mode_blob);
351 state->mode_blob = NULL;
354 if (blob->length != sizeof(struct drm_mode_modeinfo) ||
355 drm_mode_convert_umode(&state->mode,
356 (const struct drm_mode_modeinfo *)
360 state->mode_blob = drm_property_reference_blob(blob);
361 state->enable = true;
362 DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n",
363 state->mode.name, state);
365 memset(&state->mode, 0, sizeof(state->mode));
366 state->enable = false;
367 DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n",
373 EXPORT_SYMBOL(drm_atomic_set_mode_prop_for_crtc);
376 * drm_atomic_replace_property_blob - replace a blob property
377 * @blob: a pointer to the member blob to be replaced
378 * @new_blob: the new blob to replace with
379 * @replaced: whether the blob has been replaced
382 * Zero on success, error code on failure
385 drm_atomic_replace_property_blob(struct drm_property_blob **blob,
386 struct drm_property_blob *new_blob,
389 struct drm_property_blob *old_blob = *blob;
391 if (old_blob == new_blob)
395 drm_property_unreference_blob(old_blob);
397 drm_property_reference_blob(new_blob);
405 drm_atomic_replace_property_blob_from_id(struct drm_crtc *crtc,
406 struct drm_property_blob **blob,
408 ssize_t expected_size,
411 struct drm_device *dev = crtc->dev;
412 struct drm_property_blob *new_blob = NULL;
415 new_blob = drm_property_lookup_blob(dev, blob_id);
416 if (new_blob == NULL)
418 if (expected_size > 0 && expected_size != new_blob->length)
422 drm_atomic_replace_property_blob(blob, new_blob, replaced);
428 * drm_atomic_crtc_set_property - set property on CRTC
429 * @crtc: the drm CRTC to set a property on
430 * @state: the state object to update with the new property value
431 * @property: the property to set
432 * @val: the new property value
434 * Use this instead of calling crtc->atomic_set_property directly.
435 * This function handles generic/core properties and calls out to
436 * driver's ->atomic_set_property() for driver properties. To ensure
437 * consistent behavior you must call this function rather than the
438 * driver hook directly.
441 * Zero on success, error code on failure
443 int drm_atomic_crtc_set_property(struct drm_crtc *crtc,
444 struct drm_crtc_state *state, struct drm_property *property,
447 struct drm_device *dev = crtc->dev;
448 struct drm_mode_config *config = &dev->mode_config;
449 bool replaced = false;
452 if (property == config->prop_active)
454 else if (property == config->prop_mode_id) {
455 struct drm_property_blob *mode =
456 drm_property_lookup_blob(dev, val);
457 ret = drm_atomic_set_mode_prop_for_crtc(state, mode);
458 drm_property_unreference_blob(mode);
460 } else if (property == config->degamma_lut_property) {
461 ret = drm_atomic_replace_property_blob_from_id(crtc,
466 state->color_mgmt_changed = replaced;
468 } else if (property == config->ctm_property) {
469 ret = drm_atomic_replace_property_blob_from_id(crtc,
472 sizeof(struct drm_color_ctm),
474 state->color_mgmt_changed = replaced;
476 } else if (property == config->gamma_lut_property) {
477 ret = drm_atomic_replace_property_blob_from_id(crtc,
482 state->color_mgmt_changed = replaced;
484 } else if (crtc->funcs->atomic_set_property)
485 return crtc->funcs->atomic_set_property(crtc, state, property, val);
491 EXPORT_SYMBOL(drm_atomic_crtc_set_property);
494 * drm_atomic_crtc_get_property - get property value from CRTC state
495 * @crtc: the drm CRTC to set a property on
496 * @state: the state object to get the property value from
497 * @property: the property to set
498 * @val: return location for the property value
500 * This function handles generic/core properties and calls out to
501 * driver's ->atomic_get_property() for driver properties. To ensure
502 * consistent behavior you must call this function rather than the
503 * driver hook directly.
506 * Zero on success, error code on failure
509 drm_atomic_crtc_get_property(struct drm_crtc *crtc,
510 const struct drm_crtc_state *state,
511 struct drm_property *property, uint64_t *val)
513 struct drm_device *dev = crtc->dev;
514 struct drm_mode_config *config = &dev->mode_config;
516 if (property == config->prop_active)
517 *val = state->active;
518 else if (property == config->prop_mode_id)
519 *val = (state->mode_blob) ? state->mode_blob->base.id : 0;
520 else if (property == config->degamma_lut_property)
521 *val = (state->degamma_lut) ? state->degamma_lut->base.id : 0;
522 else if (property == config->ctm_property)
523 *val = (state->ctm) ? state->ctm->base.id : 0;
524 else if (property == config->gamma_lut_property)
525 *val = (state->gamma_lut) ? state->gamma_lut->base.id : 0;
526 else if (crtc->funcs->atomic_get_property)
527 return crtc->funcs->atomic_get_property(crtc, state, property, val);
535 * drm_atomic_crtc_check - check crtc state
536 * @crtc: crtc to check
537 * @state: crtc state to check
539 * Provides core sanity checks for crtc state.
542 * Zero on success, error code on failure
544 static int drm_atomic_crtc_check(struct drm_crtc *crtc,
545 struct drm_crtc_state *state)
547 /* NOTE: we explicitly don't enforce constraints such as primary
548 * layer covering entire screen, since that is something we want
549 * to allow (on hw that supports it). For hw that does not, it
550 * should be checked in driver's crtc->atomic_check() vfunc.
552 * TODO: Add generic modeset state checks once we support those.
555 if (state->active && !state->enable) {
556 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active without enabled\n",
557 crtc->base.id, crtc->name);
561 /* The state->enable vs. state->mode_blob checks can be WARN_ON,
562 * as this is a kernel-internal detail that userspace should never
563 * be able to trigger. */
564 if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
565 WARN_ON(state->enable && !state->mode_blob)) {
566 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled without mode blob\n",
567 crtc->base.id, crtc->name);
571 if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
572 WARN_ON(!state->enable && state->mode_blob)) {
573 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] disabled with mode blob\n",
574 crtc->base.id, crtc->name);
579 * Reject event generation for when a CRTC is off and stays off.
580 * It wouldn't be hard to implement this, but userspace has a track
581 * record of happily burning through 100% cpu (or worse, crash) when the
582 * display pipe is suspended. To avoid all that fun just reject updates
583 * that ask for events since likely that indicates a bug in the
584 * compositor's drawing loop. This is consistent with the vblank IOCTL
585 * and legacy page_flip IOCTL which also reject service on a disabled
588 if (state->event && !state->active && !crtc->state->active) {
589 DRM_DEBUG_ATOMIC("[CRTC:%d] requesting event but off\n",
598 * drm_atomic_get_plane_state - get plane state
599 * @state: global atomic state object
600 * @plane: plane to get state object for
602 * This function returns the plane state for the given plane, allocating it if
603 * needed. It will also grab the relevant plane lock to make sure that the state
608 * Either the allocated state or the error code encoded into the pointer. When
609 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
610 * entire atomic sequence must be restarted. All other errors are fatal.
612 struct drm_plane_state *
613 drm_atomic_get_plane_state(struct drm_atomic_state *state,
614 struct drm_plane *plane)
616 int ret, index = drm_plane_index(plane);
617 struct drm_plane_state *plane_state;
619 WARN_ON(!state->acquire_ctx);
621 plane_state = drm_atomic_get_existing_plane_state(state, plane);
625 ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
629 plane_state = plane->funcs->atomic_duplicate_state(plane);
631 return ERR_PTR(-ENOMEM);
633 state->plane_states[index] = plane_state;
634 state->planes[index] = plane;
635 plane_state->state = state;
637 DRM_DEBUG_ATOMIC("Added [PLANE:%d:%s] %p state to %p\n",
638 plane->base.id, plane->name, plane_state, state);
640 if (plane_state->crtc) {
641 struct drm_crtc_state *crtc_state;
643 crtc_state = drm_atomic_get_crtc_state(state,
645 if (IS_ERR(crtc_state))
646 return ERR_CAST(crtc_state);
651 EXPORT_SYMBOL(drm_atomic_get_plane_state);
654 * drm_atomic_plane_set_property - set property on plane
655 * @plane: the drm plane to set a property on
656 * @state: the state object to update with the new property value
657 * @property: the property to set
658 * @val: the new property value
660 * Use this instead of calling plane->atomic_set_property directly.
661 * This function handles generic/core properties and calls out to
662 * driver's ->atomic_set_property() for driver properties. To ensure
663 * consistent behavior you must call this function rather than the
664 * driver hook directly.
667 * Zero on success, error code on failure
669 int drm_atomic_plane_set_property(struct drm_plane *plane,
670 struct drm_plane_state *state, struct drm_property *property,
673 struct drm_device *dev = plane->dev;
674 struct drm_mode_config *config = &dev->mode_config;
676 if (property == config->prop_fb_id) {
677 struct drm_framebuffer *fb = drm_framebuffer_lookup(dev, val);
678 drm_atomic_set_fb_for_plane(state, fb);
680 drm_framebuffer_unreference(fb);
681 } else if (property == config->prop_crtc_id) {
682 struct drm_crtc *crtc = drm_crtc_find(dev, val);
683 return drm_atomic_set_crtc_for_plane(state, crtc);
684 } else if (property == config->prop_crtc_x) {
685 state->crtc_x = U642I64(val);
686 } else if (property == config->prop_crtc_y) {
687 state->crtc_y = U642I64(val);
688 } else if (property == config->prop_crtc_w) {
690 } else if (property == config->prop_crtc_h) {
692 } else if (property == config->prop_src_x) {
694 } else if (property == config->prop_src_y) {
696 } else if (property == config->prop_src_w) {
698 } else if (property == config->prop_src_h) {
700 } else if (property == config->rotation_property) {
701 state->rotation = val;
702 } else if (plane->funcs->atomic_set_property) {
703 return plane->funcs->atomic_set_property(plane, state,
711 EXPORT_SYMBOL(drm_atomic_plane_set_property);
714 * drm_atomic_plane_get_property - get property value from plane state
715 * @plane: the drm plane to set a property on
716 * @state: the state object to get the property value from
717 * @property: the property to set
718 * @val: return location for the property value
720 * This function handles generic/core properties and calls out to
721 * driver's ->atomic_get_property() for driver properties. To ensure
722 * consistent behavior you must call this function rather than the
723 * driver hook directly.
726 * Zero on success, error code on failure
729 drm_atomic_plane_get_property(struct drm_plane *plane,
730 const struct drm_plane_state *state,
731 struct drm_property *property, uint64_t *val)
733 struct drm_device *dev = plane->dev;
734 struct drm_mode_config *config = &dev->mode_config;
736 if (property == config->prop_fb_id) {
737 *val = (state->fb) ? state->fb->base.id : 0;
738 } else if (property == config->prop_crtc_id) {
739 *val = (state->crtc) ? state->crtc->base.id : 0;
740 } else if (property == config->prop_crtc_x) {
741 *val = I642U64(state->crtc_x);
742 } else if (property == config->prop_crtc_y) {
743 *val = I642U64(state->crtc_y);
744 } else if (property == config->prop_crtc_w) {
745 *val = state->crtc_w;
746 } else if (property == config->prop_crtc_h) {
747 *val = state->crtc_h;
748 } else if (property == config->prop_src_x) {
750 } else if (property == config->prop_src_y) {
752 } else if (property == config->prop_src_w) {
754 } else if (property == config->prop_src_h) {
756 } else if (property == config->rotation_property) {
757 *val = state->rotation;
758 } else if (plane->funcs->atomic_get_property) {
759 return plane->funcs->atomic_get_property(plane, state, property, val);
768 plane_switching_crtc(struct drm_atomic_state *state,
769 struct drm_plane *plane,
770 struct drm_plane_state *plane_state)
772 if (!plane->state->crtc || !plane_state->crtc)
775 if (plane->state->crtc == plane_state->crtc)
778 /* This could be refined, but currently there's no helper or driver code
779 * to implement direct switching of active planes nor userspace to take
780 * advantage of more direct plane switching without the intermediate
787 * drm_atomic_plane_check - check plane state
788 * @plane: plane to check
789 * @state: plane state to check
791 * Provides core sanity checks for plane state.
794 * Zero on success, error code on failure
796 static int drm_atomic_plane_check(struct drm_plane *plane,
797 struct drm_plane_state *state)
799 unsigned int fb_width, fb_height;
802 /* either *both* CRTC and FB must be set, or neither */
803 if (WARN_ON(state->crtc && !state->fb)) {
804 DRM_DEBUG_ATOMIC("CRTC set but no FB\n");
806 } else if (WARN_ON(state->fb && !state->crtc)) {
807 DRM_DEBUG_ATOMIC("FB set but no CRTC\n");
811 /* if disabled, we don't care about the rest of the state: */
815 /* Check whether this plane is usable on this CRTC */
816 if (!(plane->possible_crtcs & drm_crtc_mask(state->crtc))) {
817 DRM_DEBUG_ATOMIC("Invalid crtc for plane\n");
821 /* Check whether this plane supports the fb pixel format. */
822 ret = drm_plane_check_pixel_format(plane, state->fb->pixel_format);
824 DRM_DEBUG_ATOMIC("Invalid pixel format %s\n",
825 drm_get_format_name(state->fb->pixel_format));
829 /* Give drivers some help against integer overflows */
830 if (state->crtc_w > INT_MAX ||
831 state->crtc_x > INT_MAX - (int32_t) state->crtc_w ||
832 state->crtc_h > INT_MAX ||
833 state->crtc_y > INT_MAX - (int32_t) state->crtc_h) {
834 DRM_DEBUG_ATOMIC("Invalid CRTC coordinates %ux%u+%d+%d\n",
835 state->crtc_w, state->crtc_h,
836 state->crtc_x, state->crtc_y);
840 fb_width = state->fb->width << 16;
841 fb_height = state->fb->height << 16;
843 /* Make sure source coordinates are inside the fb. */
844 if (state->src_w > fb_width ||
845 state->src_x > fb_width - state->src_w ||
846 state->src_h > fb_height ||
847 state->src_y > fb_height - state->src_h) {
848 DRM_DEBUG_ATOMIC("Invalid source coordinates "
849 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
850 state->src_w >> 16, ((state->src_w & 0xffff) * 15625) >> 10,
851 state->src_h >> 16, ((state->src_h & 0xffff) * 15625) >> 10,
852 state->src_x >> 16, ((state->src_x & 0xffff) * 15625) >> 10,
853 state->src_y >> 16, ((state->src_y & 0xffff) * 15625) >> 10);
857 if (plane_switching_crtc(state->state, plane, state)) {
858 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] switching CRTC directly\n",
859 plane->base.id, plane->name);
867 * drm_atomic_get_connector_state - get connector state
868 * @state: global atomic state object
869 * @connector: connector to get state object for
871 * This function returns the connector state for the given connector,
872 * allocating it if needed. It will also grab the relevant connector lock to
873 * make sure that the state is consistent.
877 * Either the allocated state or the error code encoded into the pointer. When
878 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
879 * entire atomic sequence must be restarted. All other errors are fatal.
881 struct drm_connector_state *
882 drm_atomic_get_connector_state(struct drm_atomic_state *state,
883 struct drm_connector *connector)
886 struct drm_mode_config *config = &connector->dev->mode_config;
887 struct drm_connector_state *connector_state;
889 WARN_ON(!state->acquire_ctx);
891 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
895 index = drm_connector_index(connector);
897 if (index >= state->num_connector) {
898 struct __drm_connnectors_state *c;
899 int alloc = max(index + 1, config->num_connector);
901 c = krealloc(state->connectors, alloc * sizeof(*state->connectors), GFP_KERNEL);
903 return ERR_PTR(-ENOMEM);
905 state->connectors = c;
906 memset(&state->connectors[state->num_connector], 0,
907 sizeof(*state->connectors) * (alloc - state->num_connector));
909 state->num_connector = alloc;
912 if (state->connectors[index].state)
913 return state->connectors[index].state;
915 connector_state = connector->funcs->atomic_duplicate_state(connector);
916 if (!connector_state)
917 return ERR_PTR(-ENOMEM);
919 drm_connector_reference(connector);
920 state->connectors[index].state = connector_state;
921 state->connectors[index].ptr = connector;
922 connector_state->state = state;
924 DRM_DEBUG_ATOMIC("Added [CONNECTOR:%d] %p state to %p\n",
925 connector->base.id, connector_state, state);
927 if (connector_state->crtc) {
928 struct drm_crtc_state *crtc_state;
930 crtc_state = drm_atomic_get_crtc_state(state,
931 connector_state->crtc);
932 if (IS_ERR(crtc_state))
933 return ERR_CAST(crtc_state);
936 return connector_state;
938 EXPORT_SYMBOL(drm_atomic_get_connector_state);
941 * drm_atomic_connector_set_property - set property on connector.
942 * @connector: the drm connector to set a property on
943 * @state: the state object to update with the new property value
944 * @property: the property to set
945 * @val: the new property value
947 * Use this instead of calling connector->atomic_set_property directly.
948 * This function handles generic/core properties and calls out to
949 * driver's ->atomic_set_property() for driver properties. To ensure
950 * consistent behavior you must call this function rather than the
951 * driver hook directly.
954 * Zero on success, error code on failure
956 int drm_atomic_connector_set_property(struct drm_connector *connector,
957 struct drm_connector_state *state, struct drm_property *property,
960 struct drm_device *dev = connector->dev;
961 struct drm_mode_config *config = &dev->mode_config;
963 if (property == config->prop_crtc_id) {
964 struct drm_crtc *crtc = drm_crtc_find(dev, val);
965 return drm_atomic_set_crtc_for_connector(state, crtc);
966 } else if (property == config->dpms_property) {
967 /* setting DPMS property requires special handling, which
968 * is done in legacy setprop path for us. Disallow (for
969 * now?) atomic writes to DPMS property:
972 } else if (connector->funcs->atomic_set_property) {
973 return connector->funcs->atomic_set_property(connector,
974 state, property, val);
979 EXPORT_SYMBOL(drm_atomic_connector_set_property);
982 * drm_atomic_connector_get_property - get property value from connector state
983 * @connector: the drm connector to set a property on
984 * @state: the state object to get the property value from
985 * @property: the property to set
986 * @val: return location for the property value
988 * This function handles generic/core properties and calls out to
989 * driver's ->atomic_get_property() for driver properties. To ensure
990 * consistent behavior you must call this function rather than the
991 * driver hook directly.
994 * Zero on success, error code on failure
997 drm_atomic_connector_get_property(struct drm_connector *connector,
998 const struct drm_connector_state *state,
999 struct drm_property *property, uint64_t *val)
1001 struct drm_device *dev = connector->dev;
1002 struct drm_mode_config *config = &dev->mode_config;
1004 if (property == config->prop_crtc_id) {
1005 *val = (state->crtc) ? state->crtc->base.id : 0;
1006 } else if (property == config->dpms_property) {
1007 *val = connector->dpms;
1008 } else if (connector->funcs->atomic_get_property) {
1009 return connector->funcs->atomic_get_property(connector,
1010 state, property, val);
1018 int drm_atomic_get_property(struct drm_mode_object *obj,
1019 struct drm_property *property, uint64_t *val)
1021 struct drm_device *dev = property->dev;
1024 switch (obj->type) {
1025 case DRM_MODE_OBJECT_CONNECTOR: {
1026 struct drm_connector *connector = obj_to_connector(obj);
1027 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
1028 ret = drm_atomic_connector_get_property(connector,
1029 connector->state, property, val);
1032 case DRM_MODE_OBJECT_CRTC: {
1033 struct drm_crtc *crtc = obj_to_crtc(obj);
1034 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
1035 ret = drm_atomic_crtc_get_property(crtc,
1036 crtc->state, property, val);
1039 case DRM_MODE_OBJECT_PLANE: {
1040 struct drm_plane *plane = obj_to_plane(obj);
1041 WARN_ON(!drm_modeset_is_locked(&plane->mutex));
1042 ret = drm_atomic_plane_get_property(plane,
1043 plane->state, property, val);
1055 * drm_atomic_set_crtc_for_plane - set crtc for plane
1056 * @plane_state: the plane whose incoming state to update
1057 * @crtc: crtc to use for the plane
1059 * Changing the assigned crtc for a plane requires us to grab the lock and state
1060 * for the new crtc, as needed. This function takes care of all these details
1061 * besides updating the pointer in the state object itself.
1064 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1065 * then the w/w mutex code has detected a deadlock and the entire atomic
1066 * sequence must be restarted. All other errors are fatal.
1069 drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
1070 struct drm_crtc *crtc)
1072 struct drm_plane *plane = plane_state->plane;
1073 struct drm_crtc_state *crtc_state;
1075 if (plane_state->crtc) {
1076 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
1078 if (WARN_ON(IS_ERR(crtc_state)))
1079 return PTR_ERR(crtc_state);
1081 crtc_state->plane_mask &= ~(1 << drm_plane_index(plane));
1084 plane_state->crtc = crtc;
1087 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
1089 if (IS_ERR(crtc_state))
1090 return PTR_ERR(crtc_state);
1091 crtc_state->plane_mask |= (1 << drm_plane_index(plane));
1095 DRM_DEBUG_ATOMIC("Link plane state %p to [CRTC:%d:%s]\n",
1096 plane_state, crtc->base.id, crtc->name);
1098 DRM_DEBUG_ATOMIC("Link plane state %p to [NOCRTC]\n",
1103 EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane);
1106 * drm_atomic_set_fb_for_plane - set framebuffer for plane
1107 * @plane_state: atomic state object for the plane
1108 * @fb: fb to use for the plane
1110 * Changing the assigned framebuffer for a plane requires us to grab a reference
1111 * to the new fb and drop the reference to the old fb, if there is one. This
1112 * function takes care of all these details besides updating the pointer in the
1113 * state object itself.
1116 drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
1117 struct drm_framebuffer *fb)
1119 if (plane_state->fb)
1120 drm_framebuffer_unreference(plane_state->fb);
1122 drm_framebuffer_reference(fb);
1123 plane_state->fb = fb;
1126 DRM_DEBUG_ATOMIC("Set [FB:%d] for plane state %p\n",
1127 fb->base.id, plane_state);
1129 DRM_DEBUG_ATOMIC("Set [NOFB] for plane state %p\n",
1132 EXPORT_SYMBOL(drm_atomic_set_fb_for_plane);
1135 * drm_atomic_set_crtc_for_connector - set crtc for connector
1136 * @conn_state: atomic state object for the connector
1137 * @crtc: crtc to use for the connector
1139 * Changing the assigned crtc for a connector requires us to grab the lock and
1140 * state for the new crtc, as needed. This function takes care of all these
1141 * details besides updating the pointer in the state object itself.
1144 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1145 * then the w/w mutex code has detected a deadlock and the entire atomic
1146 * sequence must be restarted. All other errors are fatal.
1149 drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
1150 struct drm_crtc *crtc)
1152 struct drm_crtc_state *crtc_state;
1154 if (conn_state->crtc == crtc)
1157 if (conn_state->crtc) {
1158 crtc_state = drm_atomic_get_existing_crtc_state(conn_state->state,
1161 crtc_state->connector_mask &=
1162 ~(1 << drm_connector_index(conn_state->connector));
1164 drm_connector_unreference(conn_state->connector);
1165 conn_state->crtc = NULL;
1169 crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc);
1170 if (IS_ERR(crtc_state))
1171 return PTR_ERR(crtc_state);
1173 crtc_state->connector_mask |=
1174 1 << drm_connector_index(conn_state->connector);
1176 drm_connector_reference(conn_state->connector);
1177 conn_state->crtc = crtc;
1179 DRM_DEBUG_ATOMIC("Link connector state %p to [CRTC:%d:%s]\n",
1180 conn_state, crtc->base.id, crtc->name);
1182 DRM_DEBUG_ATOMIC("Link connector state %p to [NOCRTC]\n",
1188 EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector);
1191 * drm_atomic_add_affected_connectors - add connectors for crtc
1192 * @state: atomic state
1195 * This function walks the current configuration and adds all connectors
1196 * currently using @crtc to the atomic configuration @state. Note that this
1197 * function must acquire the connection mutex. This can potentially cause
1198 * unneeded seralization if the update is just for the planes on one crtc. Hence
1199 * drivers and helpers should only call this when really needed (e.g. when a
1200 * full modeset needs to happen due to some change).
1203 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1204 * then the w/w mutex code has detected a deadlock and the entire atomic
1205 * sequence must be restarted. All other errors are fatal.
1208 drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
1209 struct drm_crtc *crtc)
1211 struct drm_mode_config *config = &state->dev->mode_config;
1212 struct drm_connector *connector;
1213 struct drm_connector_state *conn_state;
1216 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
1220 DRM_DEBUG_ATOMIC("Adding all current connectors for [CRTC:%d:%s] to %p\n",
1221 crtc->base.id, crtc->name, state);
1224 * Changed connectors are already in @state, so only need to look at the
1225 * current configuration.
1227 drm_for_each_connector(connector, state->dev) {
1228 if (connector->state->crtc != crtc)
1231 conn_state = drm_atomic_get_connector_state(state, connector);
1232 if (IS_ERR(conn_state))
1233 return PTR_ERR(conn_state);
1238 EXPORT_SYMBOL(drm_atomic_add_affected_connectors);
1241 * drm_atomic_add_affected_planes - add planes for crtc
1242 * @state: atomic state
1245 * This function walks the current configuration and adds all planes
1246 * currently used by @crtc to the atomic configuration @state. This is useful
1247 * when an atomic commit also needs to check all currently enabled plane on
1248 * @crtc, e.g. when changing the mode. It's also useful when re-enabling a CRTC
1249 * to avoid special code to force-enable all planes.
1251 * Since acquiring a plane state will always also acquire the w/w mutex of the
1252 * current CRTC for that plane (if there is any) adding all the plane states for
1253 * a CRTC will not reduce parallism of atomic updates.
1256 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1257 * then the w/w mutex code has detected a deadlock and the entire atomic
1258 * sequence must be restarted. All other errors are fatal.
1261 drm_atomic_add_affected_planes(struct drm_atomic_state *state,
1262 struct drm_crtc *crtc)
1264 struct drm_plane *plane;
1266 WARN_ON(!drm_atomic_get_existing_crtc_state(state, crtc));
1268 drm_for_each_plane_mask(plane, state->dev, crtc->state->plane_mask) {
1269 struct drm_plane_state *plane_state =
1270 drm_atomic_get_plane_state(state, plane);
1272 if (IS_ERR(plane_state))
1273 return PTR_ERR(plane_state);
1277 EXPORT_SYMBOL(drm_atomic_add_affected_planes);
1280 * drm_atomic_legacy_backoff - locking backoff for legacy ioctls
1281 * @state: atomic state
1283 * This function should be used by legacy entry points which don't understand
1284 * -EDEADLK semantics. For simplicity this one will grab all modeset locks after
1285 * the slowpath completed.
1287 void drm_atomic_legacy_backoff(struct drm_atomic_state *state)
1292 drm_modeset_backoff(state->acquire_ctx);
1294 ret = drm_modeset_lock_all_ctx(state->dev, state->acquire_ctx);
1298 EXPORT_SYMBOL(drm_atomic_legacy_backoff);
1301 * drm_atomic_check_only - check whether a given config would work
1302 * @state: atomic configuration to check
1304 * Note that this function can return -EDEADLK if the driver needed to acquire
1305 * more locks but encountered a deadlock. The caller must then do the usual w/w
1306 * backoff dance and restart. All other errors are fatal.
1309 * 0 on success, negative error code on failure.
1311 int drm_atomic_check_only(struct drm_atomic_state *state)
1313 struct drm_device *dev = state->dev;
1314 struct drm_mode_config *config = &dev->mode_config;
1315 struct drm_plane *plane;
1316 struct drm_plane_state *plane_state;
1317 struct drm_crtc *crtc;
1318 struct drm_crtc_state *crtc_state;
1321 DRM_DEBUG_ATOMIC("checking %p\n", state);
1323 for_each_plane_in_state(state, plane, plane_state, i) {
1324 ret = drm_atomic_plane_check(plane, plane_state);
1326 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic core check failed\n",
1327 plane->base.id, plane->name);
1332 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1333 ret = drm_atomic_crtc_check(crtc, crtc_state);
1335 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic core check failed\n",
1336 crtc->base.id, crtc->name);
1341 if (config->funcs->atomic_check)
1342 ret = config->funcs->atomic_check(state->dev, state);
1344 if (!state->allow_modeset) {
1345 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1346 if (drm_atomic_crtc_needs_modeset(crtc_state)) {
1347 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] requires full modeset\n",
1348 crtc->base.id, crtc->name);
1356 EXPORT_SYMBOL(drm_atomic_check_only);
1359 * drm_atomic_commit - commit configuration atomically
1360 * @state: atomic configuration to check
1362 * Note that this function can return -EDEADLK if the driver needed to acquire
1363 * more locks but encountered a deadlock. The caller must then do the usual w/w
1364 * backoff dance and restart. All other errors are fatal.
1366 * Also note that on successful execution ownership of @state is transferred
1367 * from the caller of this function to the function itself. The caller must not
1368 * free or in any other way access @state. If the function fails then the caller
1369 * must clean up @state itself.
1372 * 0 on success, negative error code on failure.
1374 int drm_atomic_commit(struct drm_atomic_state *state)
1376 struct drm_mode_config *config = &state->dev->mode_config;
1379 ret = drm_atomic_check_only(state);
1383 DRM_DEBUG_ATOMIC("commiting %p\n", state);
1385 return config->funcs->atomic_commit(state->dev, state, false);
1387 EXPORT_SYMBOL(drm_atomic_commit);
1390 * drm_atomic_nonblocking_commit - atomic&nonblocking configuration commit
1391 * @state: atomic configuration to check
1393 * Note that this function can return -EDEADLK if the driver needed to acquire
1394 * more locks but encountered a deadlock. The caller must then do the usual w/w
1395 * backoff dance and restart. All other errors are fatal.
1397 * Also note that on successful execution ownership of @state is transferred
1398 * from the caller of this function to the function itself. The caller must not
1399 * free or in any other way access @state. If the function fails then the caller
1400 * must clean up @state itself.
1403 * 0 on success, negative error code on failure.
1405 int drm_atomic_nonblocking_commit(struct drm_atomic_state *state)
1407 struct drm_mode_config *config = &state->dev->mode_config;
1410 ret = drm_atomic_check_only(state);
1414 DRM_DEBUG_ATOMIC("commiting %p nonblocking\n", state);
1416 return config->funcs->atomic_commit(state->dev, state, true);
1418 EXPORT_SYMBOL(drm_atomic_nonblocking_commit);
1421 * The big monstor ioctl
1424 static struct drm_pending_vblank_event *create_vblank_event(
1425 struct drm_device *dev, struct drm_file *file_priv, uint64_t user_data)
1427 struct drm_pending_vblank_event *e = NULL;
1430 e = kzalloc(sizeof *e, GFP_KERNEL);
1434 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
1435 e->event.base.length = sizeof(e->event);
1436 e->event.user_data = user_data;
1438 ret = drm_event_reserve_init(dev, file_priv, &e->base, &e->event.base);
1447 static int atomic_set_prop(struct drm_atomic_state *state,
1448 struct drm_mode_object *obj, struct drm_property *prop,
1449 uint64_t prop_value)
1451 struct drm_mode_object *ref;
1454 if (!drm_property_change_valid_get(prop, prop_value, &ref))
1457 switch (obj->type) {
1458 case DRM_MODE_OBJECT_CONNECTOR: {
1459 struct drm_connector *connector = obj_to_connector(obj);
1460 struct drm_connector_state *connector_state;
1462 connector_state = drm_atomic_get_connector_state(state, connector);
1463 if (IS_ERR(connector_state)) {
1464 ret = PTR_ERR(connector_state);
1468 ret = drm_atomic_connector_set_property(connector,
1469 connector_state, prop, prop_value);
1472 case DRM_MODE_OBJECT_CRTC: {
1473 struct drm_crtc *crtc = obj_to_crtc(obj);
1474 struct drm_crtc_state *crtc_state;
1476 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1477 if (IS_ERR(crtc_state)) {
1478 ret = PTR_ERR(crtc_state);
1482 ret = drm_atomic_crtc_set_property(crtc,
1483 crtc_state, prop, prop_value);
1486 case DRM_MODE_OBJECT_PLANE: {
1487 struct drm_plane *plane = obj_to_plane(obj);
1488 struct drm_plane_state *plane_state;
1490 plane_state = drm_atomic_get_plane_state(state, plane);
1491 if (IS_ERR(plane_state)) {
1492 ret = PTR_ERR(plane_state);
1496 ret = drm_atomic_plane_set_property(plane,
1497 plane_state, prop, prop_value);
1505 drm_property_change_valid_put(prop, ref);
1510 * drm_atomic_clean_old_fb -- Unset old_fb pointers and set plane->fb pointers.
1512 * @dev: drm device to check.
1513 * @plane_mask: plane mask for planes that were updated.
1514 * @ret: return value, can be -EDEADLK for a retry.
1516 * Before doing an update plane->old_fb is set to plane->fb,
1517 * but before dropping the locks old_fb needs to be set to NULL
1518 * and plane->fb updated. This is a common operation for each
1519 * atomic update, so this call is split off as a helper.
1521 void drm_atomic_clean_old_fb(struct drm_device *dev,
1522 unsigned plane_mask,
1525 struct drm_plane *plane;
1527 /* if succeeded, fixup legacy plane crtc/fb ptrs before dropping
1528 * locks (ie. while it is still safe to deref plane->state). We
1529 * need to do this here because the driver entry points cannot
1530 * distinguish between legacy and atomic ioctls.
1532 drm_for_each_plane_mask(plane, dev, plane_mask) {
1534 struct drm_framebuffer *new_fb = plane->state->fb;
1536 drm_framebuffer_reference(new_fb);
1538 plane->crtc = plane->state->crtc;
1541 drm_framebuffer_unreference(plane->old_fb);
1543 plane->old_fb = NULL;
1546 EXPORT_SYMBOL(drm_atomic_clean_old_fb);
1548 int drm_mode_atomic_ioctl(struct drm_device *dev,
1549 void *data, struct drm_file *file_priv)
1551 struct drm_mode_atomic *arg = data;
1552 uint32_t __user *objs_ptr = (uint32_t __user *)(unsigned long)(arg->objs_ptr);
1553 uint32_t __user *count_props_ptr = (uint32_t __user *)(unsigned long)(arg->count_props_ptr);
1554 uint32_t __user *props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
1555 uint64_t __user *prop_values_ptr = (uint64_t __user *)(unsigned long)(arg->prop_values_ptr);
1556 unsigned int copied_objs, copied_props;
1557 struct drm_atomic_state *state;
1558 struct drm_modeset_acquire_ctx ctx;
1559 struct drm_plane *plane;
1560 struct drm_crtc *crtc;
1561 struct drm_crtc_state *crtc_state;
1562 unsigned plane_mask;
1566 /* disallow for drivers not supporting atomic: */
1567 if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
1570 /* disallow for userspace that has not enabled atomic cap (even
1571 * though this may be a bit overkill, since legacy userspace
1572 * wouldn't know how to call this ioctl)
1574 if (!file_priv->atomic)
1577 if (arg->flags & ~DRM_MODE_ATOMIC_FLAGS)
1583 if ((arg->flags & DRM_MODE_PAGE_FLIP_ASYNC) &&
1584 !dev->mode_config.async_page_flip)
1587 /* can't test and expect an event at the same time. */
1588 if ((arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) &&
1589 (arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
1592 drm_modeset_acquire_init(&ctx, 0);
1594 state = drm_atomic_state_alloc(dev);
1598 state->acquire_ctx = &ctx;
1599 state->allow_modeset = !!(arg->flags & DRM_MODE_ATOMIC_ALLOW_MODESET);
1606 for (i = 0; i < arg->count_objs; i++) {
1607 uint32_t obj_id, count_props;
1608 struct drm_mode_object *obj;
1610 if (get_user(obj_id, objs_ptr + copied_objs)) {
1615 obj = drm_mode_object_find(dev, obj_id, DRM_MODE_OBJECT_ANY);
1621 if (!obj->properties) {
1622 drm_mode_object_unreference(obj);
1627 if (get_user(count_props, count_props_ptr + copied_objs)) {
1628 drm_mode_object_unreference(obj);
1635 for (j = 0; j < count_props; j++) {
1637 uint64_t prop_value;
1638 struct drm_property *prop;
1640 if (get_user(prop_id, props_ptr + copied_props)) {
1641 drm_mode_object_unreference(obj);
1646 prop = drm_property_find(dev, prop_id);
1648 drm_mode_object_unreference(obj);
1653 if (copy_from_user(&prop_value,
1654 prop_values_ptr + copied_props,
1655 sizeof(prop_value))) {
1656 drm_mode_object_unreference(obj);
1661 ret = atomic_set_prop(state, obj, prop, prop_value);
1663 drm_mode_object_unreference(obj);
1670 if (obj->type == DRM_MODE_OBJECT_PLANE && count_props &&
1671 !(arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)) {
1672 plane = obj_to_plane(obj);
1673 plane_mask |= (1 << drm_plane_index(plane));
1674 plane->old_fb = plane->fb;
1676 drm_mode_object_unreference(obj);
1679 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
1680 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1681 struct drm_pending_vblank_event *e;
1683 e = create_vblank_event(dev, file_priv, arg->user_data);
1689 crtc_state->event = e;
1693 if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) {
1695 * Unlike commit, check_only does not clean up state.
1696 * Below we call drm_atomic_state_free for it.
1698 ret = drm_atomic_check_only(state);
1699 } else if (arg->flags & DRM_MODE_ATOMIC_NONBLOCK) {
1700 ret = drm_atomic_nonblocking_commit(state);
1702 ret = drm_atomic_commit(state);
1706 drm_atomic_clean_old_fb(dev, plane_mask, ret);
1708 if (ret && arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
1710 * TEST_ONLY and PAGE_FLIP_EVENT are mutually exclusive,
1711 * if they weren't, this code should be called on success
1712 * for TEST_ONLY too.
1715 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1716 if (!crtc_state->event)
1719 drm_event_cancel_free(dev, &crtc_state->event->base);
1723 if (ret == -EDEADLK) {
1724 drm_atomic_state_clear(state);
1725 drm_modeset_backoff(&ctx);
1729 if (ret || arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)
1730 drm_atomic_state_free(state);
1732 drm_modeset_drop_locks(&ctx);
1733 drm_modeset_acquire_fini(&ctx);