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>
33 #include <drm/drm_print.h>
34 #include <linux/sync_file.h>
36 #include "drm_crtc_internal.h"
38 void __drm_crtc_commit_free(struct kref *kref)
40 struct drm_crtc_commit *commit =
41 container_of(kref, struct drm_crtc_commit, ref);
45 EXPORT_SYMBOL(__drm_crtc_commit_free);
48 * drm_atomic_state_default_release -
49 * release memory initialized by drm_atomic_state_init
50 * @state: atomic state
52 * Free all the memory allocated by drm_atomic_state_init.
53 * This is useful for drivers that subclass the atomic state.
55 void drm_atomic_state_default_release(struct drm_atomic_state *state)
57 kfree(state->connectors);
61 EXPORT_SYMBOL(drm_atomic_state_default_release);
64 * drm_atomic_state_init - init new atomic state
66 * @state: atomic state
68 * Default implementation for filling in a new atomic state.
69 * This is useful for drivers that subclass the atomic state.
72 drm_atomic_state_init(struct drm_device *dev, struct drm_atomic_state *state)
74 kref_init(&state->ref);
76 /* TODO legacy paths should maybe do a better job about
77 * setting this appropriately?
79 state->allow_modeset = true;
81 state->crtcs = kcalloc(dev->mode_config.num_crtc,
82 sizeof(*state->crtcs), GFP_KERNEL);
85 state->planes = kcalloc(dev->mode_config.num_total_plane,
86 sizeof(*state->planes), GFP_KERNEL);
92 DRM_DEBUG_ATOMIC("Allocated atomic state %p\n", state);
96 drm_atomic_state_default_release(state);
99 EXPORT_SYMBOL(drm_atomic_state_init);
102 * drm_atomic_state_alloc - allocate atomic state
105 * This allocates an empty atomic state to track updates.
107 struct drm_atomic_state *
108 drm_atomic_state_alloc(struct drm_device *dev)
110 struct drm_mode_config *config = &dev->mode_config;
111 struct drm_atomic_state *state;
113 if (!config->funcs->atomic_state_alloc) {
114 state = kzalloc(sizeof(*state), GFP_KERNEL);
117 if (drm_atomic_state_init(dev, state) < 0) {
124 return config->funcs->atomic_state_alloc(dev);
126 EXPORT_SYMBOL(drm_atomic_state_alloc);
129 * drm_atomic_state_default_clear - clear base atomic state
130 * @state: atomic state
132 * Default implementation for clearing atomic state.
133 * This is useful for drivers that subclass the atomic state.
135 void drm_atomic_state_default_clear(struct drm_atomic_state *state)
137 struct drm_device *dev = state->dev;
138 struct drm_mode_config *config = &dev->mode_config;
141 DRM_DEBUG_ATOMIC("Clearing atomic state %p\n", state);
143 for (i = 0; i < state->num_connector; i++) {
144 struct drm_connector *connector = state->connectors[i].ptr;
149 connector->funcs->atomic_destroy_state(connector,
150 state->connectors[i].state);
151 state->connectors[i].ptr = NULL;
152 state->connectors[i].state = NULL;
153 drm_connector_put(connector);
156 for (i = 0; i < config->num_crtc; i++) {
157 struct drm_crtc *crtc = state->crtcs[i].ptr;
162 crtc->funcs->atomic_destroy_state(crtc,
163 state->crtcs[i].state);
165 if (state->crtcs[i].commit) {
166 kfree(state->crtcs[i].commit->event);
167 state->crtcs[i].commit->event = NULL;
168 drm_crtc_commit_put(state->crtcs[i].commit);
171 state->crtcs[i].commit = NULL;
172 state->crtcs[i].ptr = NULL;
173 state->crtcs[i].state = NULL;
176 for (i = 0; i < config->num_total_plane; i++) {
177 struct drm_plane *plane = state->planes[i].ptr;
182 plane->funcs->atomic_destroy_state(plane,
183 state->planes[i].state);
184 state->planes[i].ptr = NULL;
185 state->planes[i].state = NULL;
188 EXPORT_SYMBOL(drm_atomic_state_default_clear);
191 * drm_atomic_state_clear - clear state object
192 * @state: atomic state
194 * When the w/w mutex algorithm detects a deadlock we need to back off and drop
195 * all locks. So someone else could sneak in and change the current modeset
196 * configuration. Which means that all the state assembled in @state is no
197 * longer an atomic update to the current state, but to some arbitrary earlier
198 * state. Which could break assumptions the driver's
199 * &drm_mode_config_funcs.atomic_check likely relies on.
201 * Hence we must clear all cached state and completely start over, using this
204 void drm_atomic_state_clear(struct drm_atomic_state *state)
206 struct drm_device *dev = state->dev;
207 struct drm_mode_config *config = &dev->mode_config;
209 if (config->funcs->atomic_state_clear)
210 config->funcs->atomic_state_clear(state);
212 drm_atomic_state_default_clear(state);
214 EXPORT_SYMBOL(drm_atomic_state_clear);
217 * __drm_atomic_state_free - free all memory for an atomic state
218 * @ref: This atomic state to deallocate
220 * This frees all memory associated with an atomic state, including all the
221 * per-object state for planes, crtcs and connectors.
223 void __drm_atomic_state_free(struct kref *ref)
225 struct drm_atomic_state *state = container_of(ref, typeof(*state), ref);
226 struct drm_mode_config *config = &state->dev->mode_config;
228 drm_atomic_state_clear(state);
230 DRM_DEBUG_ATOMIC("Freeing atomic state %p\n", state);
232 if (config->funcs->atomic_state_free) {
233 config->funcs->atomic_state_free(state);
235 drm_atomic_state_default_release(state);
239 EXPORT_SYMBOL(__drm_atomic_state_free);
242 * drm_atomic_get_crtc_state - get crtc state
243 * @state: global atomic state object
244 * @crtc: crtc to get state object for
246 * This function returns the crtc state for the given crtc, allocating it if
247 * needed. It will also grab the relevant crtc lock to make sure that the state
252 * Either the allocated state or the error code encoded into the pointer. When
253 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
254 * entire atomic sequence must be restarted. All other errors are fatal.
256 struct drm_crtc_state *
257 drm_atomic_get_crtc_state(struct drm_atomic_state *state,
258 struct drm_crtc *crtc)
260 int ret, index = drm_crtc_index(crtc);
261 struct drm_crtc_state *crtc_state;
263 WARN_ON(!state->acquire_ctx);
265 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
269 ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx);
273 crtc_state = crtc->funcs->atomic_duplicate_state(crtc);
275 return ERR_PTR(-ENOMEM);
277 state->crtcs[index].state = crtc_state;
278 state->crtcs[index].old_state = crtc->state;
279 state->crtcs[index].new_state = crtc_state;
280 state->crtcs[index].ptr = crtc;
281 crtc_state->state = state;
283 DRM_DEBUG_ATOMIC("Added [CRTC:%d:%s] %p state to %p\n",
284 crtc->base.id, crtc->name, crtc_state, state);
288 EXPORT_SYMBOL(drm_atomic_get_crtc_state);
290 static void set_out_fence_for_crtc(struct drm_atomic_state *state,
291 struct drm_crtc *crtc, s32 __user *fence_ptr)
293 state->crtcs[drm_crtc_index(crtc)].out_fence_ptr = fence_ptr;
296 static s32 __user *get_out_fence_for_crtc(struct drm_atomic_state *state,
297 struct drm_crtc *crtc)
299 s32 __user *fence_ptr;
301 fence_ptr = state->crtcs[drm_crtc_index(crtc)].out_fence_ptr;
302 state->crtcs[drm_crtc_index(crtc)].out_fence_ptr = NULL;
308 * drm_atomic_set_mode_for_crtc - set mode for CRTC
309 * @state: the CRTC whose incoming state to update
310 * @mode: kernel-internal mode to use for the CRTC, or NULL to disable
312 * Set a mode (originating from the kernel) on the desired CRTC state and update
313 * the enable property.
316 * Zero on success, error code on failure. Cannot return -EDEADLK.
318 int drm_atomic_set_mode_for_crtc(struct drm_crtc_state *state,
319 struct drm_display_mode *mode)
321 struct drm_mode_modeinfo umode;
323 /* Early return for no change. */
324 if (mode && memcmp(&state->mode, mode, sizeof(*mode)) == 0)
327 drm_property_blob_put(state->mode_blob);
328 state->mode_blob = NULL;
331 drm_mode_convert_to_umode(&umode, mode);
333 drm_property_create_blob(state->crtc->dev,
336 if (IS_ERR(state->mode_blob))
337 return PTR_ERR(state->mode_blob);
339 drm_mode_copy(&state->mode, mode);
340 state->enable = true;
341 DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n",
344 memset(&state->mode, 0, sizeof(state->mode));
345 state->enable = false;
346 DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n",
352 EXPORT_SYMBOL(drm_atomic_set_mode_for_crtc);
355 * drm_atomic_set_mode_prop_for_crtc - set mode for CRTC
356 * @state: the CRTC whose incoming state to update
357 * @blob: pointer to blob property to use for mode
359 * Set a mode (originating from a blob property) on the desired CRTC state.
360 * This function will take a reference on the blob property for the CRTC state,
361 * and release the reference held on the state's existing mode property, if any
365 * Zero on success, error code on failure. Cannot return -EDEADLK.
367 int drm_atomic_set_mode_prop_for_crtc(struct drm_crtc_state *state,
368 struct drm_property_blob *blob)
370 if (blob == state->mode_blob)
373 drm_property_blob_put(state->mode_blob);
374 state->mode_blob = NULL;
376 memset(&state->mode, 0, sizeof(state->mode));
379 if (blob->length != sizeof(struct drm_mode_modeinfo) ||
380 drm_mode_convert_umode(&state->mode,
381 (const struct drm_mode_modeinfo *)
385 state->mode_blob = drm_property_blob_get(blob);
386 state->enable = true;
387 DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n",
388 state->mode.name, state);
390 state->enable = false;
391 DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n",
397 EXPORT_SYMBOL(drm_atomic_set_mode_prop_for_crtc);
400 * drm_atomic_replace_property_blob - replace a blob property
401 * @blob: a pointer to the member blob to be replaced
402 * @new_blob: the new blob to replace with
403 * @replaced: whether the blob has been replaced
406 * Zero on success, error code on failure
409 drm_atomic_replace_property_blob(struct drm_property_blob **blob,
410 struct drm_property_blob *new_blob,
413 struct drm_property_blob *old_blob = *blob;
415 if (old_blob == new_blob)
418 drm_property_blob_put(old_blob);
420 drm_property_blob_get(new_blob);
428 drm_atomic_replace_property_blob_from_id(struct drm_crtc *crtc,
429 struct drm_property_blob **blob,
431 ssize_t expected_size,
434 struct drm_property_blob *new_blob = NULL;
437 new_blob = drm_property_lookup_blob(crtc->dev, blob_id);
438 if (new_blob == NULL)
441 if (expected_size > 0 && expected_size != new_blob->length) {
442 drm_property_blob_put(new_blob);
447 drm_atomic_replace_property_blob(blob, new_blob, replaced);
448 drm_property_blob_put(new_blob);
454 * drm_atomic_crtc_set_property - set property on CRTC
455 * @crtc: the drm CRTC to set a property on
456 * @state: the state object to update with the new property value
457 * @property: the property to set
458 * @val: the new property value
460 * This function handles generic/core properties and calls out to driver's
461 * &drm_crtc_funcs.atomic_set_property for driver properties. To ensure
462 * consistent behavior you must call this function rather than the driver hook
466 * Zero on success, error code on failure
468 int drm_atomic_crtc_set_property(struct drm_crtc *crtc,
469 struct drm_crtc_state *state, struct drm_property *property,
472 struct drm_device *dev = crtc->dev;
473 struct drm_mode_config *config = &dev->mode_config;
474 bool replaced = false;
477 if (property == config->prop_active)
479 else if (property == config->prop_mode_id) {
480 struct drm_property_blob *mode =
481 drm_property_lookup_blob(dev, val);
482 ret = drm_atomic_set_mode_prop_for_crtc(state, mode);
483 drm_property_blob_put(mode);
485 } else if (property == config->degamma_lut_property) {
486 ret = drm_atomic_replace_property_blob_from_id(crtc,
491 state->color_mgmt_changed |= replaced;
493 } else if (property == config->ctm_property) {
494 ret = drm_atomic_replace_property_blob_from_id(crtc,
497 sizeof(struct drm_color_ctm),
499 state->color_mgmt_changed |= replaced;
501 } else if (property == config->gamma_lut_property) {
502 ret = drm_atomic_replace_property_blob_from_id(crtc,
507 state->color_mgmt_changed |= replaced;
509 } else if (property == config->prop_out_fence_ptr) {
510 s32 __user *fence_ptr = u64_to_user_ptr(val);
515 if (put_user(-1, fence_ptr))
518 set_out_fence_for_crtc(state->state, crtc, fence_ptr);
519 } else if (crtc->funcs->atomic_set_property)
520 return crtc->funcs->atomic_set_property(crtc, state, property, val);
526 EXPORT_SYMBOL(drm_atomic_crtc_set_property);
529 * drm_atomic_crtc_get_property - get property value from CRTC state
530 * @crtc: the drm CRTC to set a property on
531 * @state: the state object to get the property value from
532 * @property: the property to set
533 * @val: return location for the property value
535 * This function handles generic/core properties and calls out to driver's
536 * &drm_crtc_funcs.atomic_get_property for driver properties. To ensure
537 * consistent behavior you must call this function rather than the driver hook
541 * Zero on success, error code on failure
544 drm_atomic_crtc_get_property(struct drm_crtc *crtc,
545 const struct drm_crtc_state *state,
546 struct drm_property *property, uint64_t *val)
548 struct drm_device *dev = crtc->dev;
549 struct drm_mode_config *config = &dev->mode_config;
551 if (property == config->prop_active)
552 *val = state->active;
553 else if (property == config->prop_mode_id)
554 *val = (state->mode_blob) ? state->mode_blob->base.id : 0;
555 else if (property == config->degamma_lut_property)
556 *val = (state->degamma_lut) ? state->degamma_lut->base.id : 0;
557 else if (property == config->ctm_property)
558 *val = (state->ctm) ? state->ctm->base.id : 0;
559 else if (property == config->gamma_lut_property)
560 *val = (state->gamma_lut) ? state->gamma_lut->base.id : 0;
561 else if (property == config->prop_out_fence_ptr)
563 else if (crtc->funcs->atomic_get_property)
564 return crtc->funcs->atomic_get_property(crtc, state, property, val);
572 * drm_atomic_crtc_check - check crtc state
573 * @crtc: crtc to check
574 * @state: crtc state to check
576 * Provides core sanity checks for crtc state.
579 * Zero on success, error code on failure
581 static int drm_atomic_crtc_check(struct drm_crtc *crtc,
582 struct drm_crtc_state *state)
584 /* NOTE: we explicitly don't enforce constraints such as primary
585 * layer covering entire screen, since that is something we want
586 * to allow (on hw that supports it). For hw that does not, it
587 * should be checked in driver's crtc->atomic_check() vfunc.
589 * TODO: Add generic modeset state checks once we support those.
592 if (state->active && !state->enable) {
593 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active without enabled\n",
594 crtc->base.id, crtc->name);
598 /* The state->enable vs. state->mode_blob checks can be WARN_ON,
599 * as this is a kernel-internal detail that userspace should never
600 * be able to trigger. */
601 if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
602 WARN_ON(state->enable && !state->mode_blob)) {
603 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled without mode blob\n",
604 crtc->base.id, crtc->name);
608 if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
609 WARN_ON(!state->enable && state->mode_blob)) {
610 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] disabled with mode blob\n",
611 crtc->base.id, crtc->name);
616 * Reject event generation for when a CRTC is off and stays off.
617 * It wouldn't be hard to implement this, but userspace has a track
618 * record of happily burning through 100% cpu (or worse, crash) when the
619 * display pipe is suspended. To avoid all that fun just reject updates
620 * that ask for events since likely that indicates a bug in the
621 * compositor's drawing loop. This is consistent with the vblank IOCTL
622 * and legacy page_flip IOCTL which also reject service on a disabled
625 if (state->event && !state->active && !crtc->state->active) {
626 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] requesting event but off\n",
627 crtc->base.id, crtc->name);
634 static void drm_atomic_crtc_print_state(struct drm_printer *p,
635 const struct drm_crtc_state *state)
637 struct drm_crtc *crtc = state->crtc;
639 drm_printf(p, "crtc[%u]: %s\n", crtc->base.id, crtc->name);
640 drm_printf(p, "\tenable=%d\n", state->enable);
641 drm_printf(p, "\tactive=%d\n", state->active);
642 drm_printf(p, "\tplanes_changed=%d\n", state->planes_changed);
643 drm_printf(p, "\tmode_changed=%d\n", state->mode_changed);
644 drm_printf(p, "\tactive_changed=%d\n", state->active_changed);
645 drm_printf(p, "\tconnectors_changed=%d\n", state->connectors_changed);
646 drm_printf(p, "\tcolor_mgmt_changed=%d\n", state->color_mgmt_changed);
647 drm_printf(p, "\tplane_mask=%x\n", state->plane_mask);
648 drm_printf(p, "\tconnector_mask=%x\n", state->connector_mask);
649 drm_printf(p, "\tencoder_mask=%x\n", state->encoder_mask);
650 drm_printf(p, "\tmode: " DRM_MODE_FMT "\n", DRM_MODE_ARG(&state->mode));
652 if (crtc->funcs->atomic_print_state)
653 crtc->funcs->atomic_print_state(p, state);
657 * drm_atomic_get_plane_state - get plane state
658 * @state: global atomic state object
659 * @plane: plane to get state object for
661 * This function returns the plane state for the given plane, allocating it if
662 * needed. It will also grab the relevant plane lock to make sure that the state
667 * Either the allocated state or the error code encoded into the pointer. When
668 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
669 * entire atomic sequence must be restarted. All other errors are fatal.
671 struct drm_plane_state *
672 drm_atomic_get_plane_state(struct drm_atomic_state *state,
673 struct drm_plane *plane)
675 int ret, index = drm_plane_index(plane);
676 struct drm_plane_state *plane_state;
678 WARN_ON(!state->acquire_ctx);
680 plane_state = drm_atomic_get_existing_plane_state(state, plane);
684 ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
688 plane_state = plane->funcs->atomic_duplicate_state(plane);
690 return ERR_PTR(-ENOMEM);
692 state->planes[index].state = plane_state;
693 state->planes[index].ptr = plane;
694 state->planes[index].old_state = plane->state;
695 state->planes[index].new_state = plane_state;
696 plane_state->state = state;
698 DRM_DEBUG_ATOMIC("Added [PLANE:%d:%s] %p state to %p\n",
699 plane->base.id, plane->name, plane_state, state);
701 if (plane_state->crtc) {
702 struct drm_crtc_state *crtc_state;
704 crtc_state = drm_atomic_get_crtc_state(state,
706 if (IS_ERR(crtc_state))
707 return ERR_CAST(crtc_state);
712 EXPORT_SYMBOL(drm_atomic_get_plane_state);
715 * drm_atomic_plane_set_property - set property on plane
716 * @plane: the drm plane to set a property on
717 * @state: the state object to update with the new property value
718 * @property: the property to set
719 * @val: the new property value
721 * This function handles generic/core properties and calls out to driver's
722 * &drm_plane_funcs.atomic_set_property for driver properties. To ensure
723 * consistent behavior you must call this function rather than the driver hook
727 * Zero on success, error code on failure
729 int drm_atomic_plane_set_property(struct drm_plane *plane,
730 struct drm_plane_state *state, struct drm_property *property,
733 struct drm_device *dev = plane->dev;
734 struct drm_mode_config *config = &dev->mode_config;
736 if (property == config->prop_fb_id) {
737 struct drm_framebuffer *fb = drm_framebuffer_lookup(dev, val);
738 drm_atomic_set_fb_for_plane(state, fb);
740 drm_framebuffer_put(fb);
741 } else if (property == config->prop_in_fence_fd) {
745 if (U642I64(val) == -1)
748 state->fence = sync_file_get_fence(val);
752 } else if (property == config->prop_crtc_id) {
753 struct drm_crtc *crtc = drm_crtc_find(dev, val);
754 return drm_atomic_set_crtc_for_plane(state, crtc);
755 } else if (property == config->prop_crtc_x) {
756 state->crtc_x = U642I64(val);
757 } else if (property == config->prop_crtc_y) {
758 state->crtc_y = U642I64(val);
759 } else if (property == config->prop_crtc_w) {
761 } else if (property == config->prop_crtc_h) {
763 } else if (property == config->prop_src_x) {
765 } else if (property == config->prop_src_y) {
767 } else if (property == config->prop_src_w) {
769 } else if (property == config->prop_src_h) {
771 } else if (property == plane->rotation_property) {
772 if (!is_power_of_2(val & DRM_ROTATE_MASK))
774 state->rotation = val;
775 } else if (property == plane->zpos_property) {
777 } else if (plane->funcs->atomic_set_property) {
778 return plane->funcs->atomic_set_property(plane, state,
786 EXPORT_SYMBOL(drm_atomic_plane_set_property);
789 * drm_atomic_plane_get_property - get property value from plane state
790 * @plane: the drm plane to set a property on
791 * @state: the state object to get the property value from
792 * @property: the property to set
793 * @val: return location for the property value
795 * This function handles generic/core properties and calls out to driver's
796 * &drm_plane_funcs.atomic_get_property for driver properties. To ensure
797 * consistent behavior you must call this function rather than the driver hook
801 * Zero on success, error code on failure
804 drm_atomic_plane_get_property(struct drm_plane *plane,
805 const struct drm_plane_state *state,
806 struct drm_property *property, uint64_t *val)
808 struct drm_device *dev = plane->dev;
809 struct drm_mode_config *config = &dev->mode_config;
811 if (property == config->prop_fb_id) {
812 *val = (state->fb) ? state->fb->base.id : 0;
813 } else if (property == config->prop_in_fence_fd) {
815 } else if (property == config->prop_crtc_id) {
816 *val = (state->crtc) ? state->crtc->base.id : 0;
817 } else if (property == config->prop_crtc_x) {
818 *val = I642U64(state->crtc_x);
819 } else if (property == config->prop_crtc_y) {
820 *val = I642U64(state->crtc_y);
821 } else if (property == config->prop_crtc_w) {
822 *val = state->crtc_w;
823 } else if (property == config->prop_crtc_h) {
824 *val = state->crtc_h;
825 } else if (property == config->prop_src_x) {
827 } else if (property == config->prop_src_y) {
829 } else if (property == config->prop_src_w) {
831 } else if (property == config->prop_src_h) {
833 } else if (property == plane->rotation_property) {
834 *val = state->rotation;
835 } else if (property == plane->zpos_property) {
837 } else if (plane->funcs->atomic_get_property) {
838 return plane->funcs->atomic_get_property(plane, state, property, val);
847 plane_switching_crtc(struct drm_atomic_state *state,
848 struct drm_plane *plane,
849 struct drm_plane_state *plane_state)
851 if (!plane->state->crtc || !plane_state->crtc)
854 if (plane->state->crtc == plane_state->crtc)
857 /* This could be refined, but currently there's no helper or driver code
858 * to implement direct switching of active planes nor userspace to take
859 * advantage of more direct plane switching without the intermediate
866 * drm_atomic_plane_check - check plane state
867 * @plane: plane to check
868 * @state: plane state to check
870 * Provides core sanity checks for plane state.
873 * Zero on success, error code on failure
875 static int drm_atomic_plane_check(struct drm_plane *plane,
876 struct drm_plane_state *state)
878 unsigned int fb_width, fb_height;
881 /* either *both* CRTC and FB must be set, or neither */
882 if (WARN_ON(state->crtc && !state->fb)) {
883 DRM_DEBUG_ATOMIC("CRTC set but no FB\n");
885 } else if (WARN_ON(state->fb && !state->crtc)) {
886 DRM_DEBUG_ATOMIC("FB set but no CRTC\n");
890 /* if disabled, we don't care about the rest of the state: */
894 /* Check whether this plane is usable on this CRTC */
895 if (!(plane->possible_crtcs & drm_crtc_mask(state->crtc))) {
896 DRM_DEBUG_ATOMIC("Invalid crtc for plane\n");
900 /* Check whether this plane supports the fb pixel format. */
901 ret = drm_plane_check_pixel_format(plane, state->fb->format->format);
903 struct drm_format_name_buf format_name;
904 DRM_DEBUG_ATOMIC("Invalid pixel format %s\n",
905 drm_get_format_name(state->fb->format->format,
910 /* Give drivers some help against integer overflows */
911 if (state->crtc_w > INT_MAX ||
912 state->crtc_x > INT_MAX - (int32_t) state->crtc_w ||
913 state->crtc_h > INT_MAX ||
914 state->crtc_y > INT_MAX - (int32_t) state->crtc_h) {
915 DRM_DEBUG_ATOMIC("Invalid CRTC coordinates %ux%u+%d+%d\n",
916 state->crtc_w, state->crtc_h,
917 state->crtc_x, state->crtc_y);
921 fb_width = state->fb->width << 16;
922 fb_height = state->fb->height << 16;
924 /* Make sure source coordinates are inside the fb. */
925 if (state->src_w > fb_width ||
926 state->src_x > fb_width - state->src_w ||
927 state->src_h > fb_height ||
928 state->src_y > fb_height - state->src_h) {
929 DRM_DEBUG_ATOMIC("Invalid source coordinates "
930 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
931 state->src_w >> 16, ((state->src_w & 0xffff) * 15625) >> 10,
932 state->src_h >> 16, ((state->src_h & 0xffff) * 15625) >> 10,
933 state->src_x >> 16, ((state->src_x & 0xffff) * 15625) >> 10,
934 state->src_y >> 16, ((state->src_y & 0xffff) * 15625) >> 10);
938 if (plane_switching_crtc(state->state, plane, state)) {
939 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] switching CRTC directly\n",
940 plane->base.id, plane->name);
947 static void drm_atomic_plane_print_state(struct drm_printer *p,
948 const struct drm_plane_state *state)
950 struct drm_plane *plane = state->plane;
951 struct drm_rect src = drm_plane_state_src(state);
952 struct drm_rect dest = drm_plane_state_dest(state);
954 drm_printf(p, "plane[%u]: %s\n", plane->base.id, plane->name);
955 drm_printf(p, "\tcrtc=%s\n", state->crtc ? state->crtc->name : "(null)");
956 drm_printf(p, "\tfb=%u\n", state->fb ? state->fb->base.id : 0);
958 struct drm_framebuffer *fb = state->fb;
959 int i, n = fb->format->num_planes;
960 struct drm_format_name_buf format_name;
962 drm_printf(p, "\t\tformat=%s\n",
963 drm_get_format_name(fb->format->format, &format_name));
964 drm_printf(p, "\t\t\tmodifier=0x%lx\n", fb->modifier);
965 drm_printf(p, "\t\tsize=%dx%d\n", fb->width, fb->height);
966 drm_printf(p, "\t\tlayers:\n");
967 for (i = 0; i < n; i++) {
968 drm_printf(p, "\t\t\tpitch[%d]=%u\n", i, fb->pitches[i]);
969 drm_printf(p, "\t\t\toffset[%d]=%u\n", i, fb->offsets[i]);
972 drm_printf(p, "\tcrtc-pos=" DRM_RECT_FMT "\n", DRM_RECT_ARG(&dest));
973 drm_printf(p, "\tsrc-pos=" DRM_RECT_FP_FMT "\n", DRM_RECT_FP_ARG(&src));
974 drm_printf(p, "\trotation=%x\n", state->rotation);
976 if (plane->funcs->atomic_print_state)
977 plane->funcs->atomic_print_state(p, state);
981 * drm_atomic_get_connector_state - get connector state
982 * @state: global atomic state object
983 * @connector: connector to get state object for
985 * This function returns the connector state for the given connector,
986 * allocating it if needed. It will also grab the relevant connector lock to
987 * make sure that the state is consistent.
991 * Either the allocated state or the error code encoded into the pointer. When
992 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
993 * entire atomic sequence must be restarted. All other errors are fatal.
995 struct drm_connector_state *
996 drm_atomic_get_connector_state(struct drm_atomic_state *state,
997 struct drm_connector *connector)
1000 struct drm_mode_config *config = &connector->dev->mode_config;
1001 struct drm_connector_state *connector_state;
1003 WARN_ON(!state->acquire_ctx);
1005 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
1007 return ERR_PTR(ret);
1009 index = drm_connector_index(connector);
1011 if (index >= state->num_connector) {
1012 struct __drm_connnectors_state *c;
1013 int alloc = max(index + 1, config->num_connector);
1015 c = krealloc(state->connectors,
1016 alloc * sizeof(*state->connectors), M_DRM,
1019 return ERR_PTR(-ENOMEM);
1021 state->connectors = c;
1022 memset(&state->connectors[state->num_connector], 0,
1023 sizeof(*state->connectors) * (alloc - state->num_connector));
1025 state->num_connector = alloc;
1028 if (state->connectors[index].state)
1029 return state->connectors[index].state;
1031 connector_state = connector->funcs->atomic_duplicate_state(connector);
1032 if (!connector_state)
1033 return ERR_PTR(-ENOMEM);
1035 drm_connector_get(connector);
1036 state->connectors[index].state = connector_state;
1037 state->connectors[index].old_state = connector->state;
1038 state->connectors[index].new_state = connector_state;
1039 state->connectors[index].ptr = connector;
1040 connector_state->state = state;
1042 DRM_DEBUG_ATOMIC("Added [CONNECTOR:%d:%s] %p state to %p\n",
1043 connector->base.id, connector->name,
1044 connector_state, state);
1046 if (connector_state->crtc) {
1047 struct drm_crtc_state *crtc_state;
1049 crtc_state = drm_atomic_get_crtc_state(state,
1050 connector_state->crtc);
1051 if (IS_ERR(crtc_state))
1052 return ERR_CAST(crtc_state);
1055 return connector_state;
1057 EXPORT_SYMBOL(drm_atomic_get_connector_state);
1060 * drm_atomic_connector_set_property - set property on connector.
1061 * @connector: the drm connector to set a property on
1062 * @state: the state object to update with the new property value
1063 * @property: the property to set
1064 * @val: the new property value
1066 * This function handles generic/core properties and calls out to driver's
1067 * &drm_connector_funcs.atomic_set_property for driver properties. To ensure
1068 * consistent behavior you must call this function rather than the driver hook
1072 * Zero on success, error code on failure
1074 int drm_atomic_connector_set_property(struct drm_connector *connector,
1075 struct drm_connector_state *state, struct drm_property *property,
1078 struct drm_device *dev = connector->dev;
1079 struct drm_mode_config *config = &dev->mode_config;
1081 if (property == config->prop_crtc_id) {
1082 struct drm_crtc *crtc = drm_crtc_find(dev, val);
1083 return drm_atomic_set_crtc_for_connector(state, crtc);
1084 } else if (property == config->dpms_property) {
1085 /* setting DPMS property requires special handling, which
1086 * is done in legacy setprop path for us. Disallow (for
1087 * now?) atomic writes to DPMS property:
1090 } else if (property == config->tv_select_subconnector_property) {
1091 state->tv.subconnector = val;
1092 } else if (property == config->tv_left_margin_property) {
1093 state->tv.margins.left = val;
1094 } else if (property == config->tv_right_margin_property) {
1095 state->tv.margins.right = val;
1096 } else if (property == config->tv_top_margin_property) {
1097 state->tv.margins.top = val;
1098 } else if (property == config->tv_bottom_margin_property) {
1099 state->tv.margins.bottom = val;
1100 } else if (property == config->tv_mode_property) {
1101 state->tv.mode = val;
1102 } else if (property == config->tv_brightness_property) {
1103 state->tv.brightness = val;
1104 } else if (property == config->tv_contrast_property) {
1105 state->tv.contrast = val;
1106 } else if (property == config->tv_flicker_reduction_property) {
1107 state->tv.flicker_reduction = val;
1108 } else if (property == config->tv_overscan_property) {
1109 state->tv.overscan = val;
1110 } else if (property == config->tv_saturation_property) {
1111 state->tv.saturation = val;
1112 } else if (property == config->tv_hue_property) {
1113 state->tv.hue = val;
1114 } else if (property == config->link_status_property) {
1115 /* Never downgrade from GOOD to BAD on userspace's request here,
1116 * only hw issues can do that.
1118 * For an atomic property the userspace doesn't need to be able
1119 * to understand all the properties, but needs to be able to
1120 * restore the state it wants on VT switch. So if the userspace
1121 * tries to change the link_status from GOOD to BAD, driver
1122 * silently rejects it and returns a 0. This prevents userspace
1123 * from accidently breaking the display when it restores the
1126 if (state->link_status != DRM_LINK_STATUS_GOOD)
1127 state->link_status = val;
1128 } else if (connector->funcs->atomic_set_property) {
1129 return connector->funcs->atomic_set_property(connector,
1130 state, property, val);
1137 EXPORT_SYMBOL(drm_atomic_connector_set_property);
1139 static void drm_atomic_connector_print_state(struct drm_printer *p,
1140 const struct drm_connector_state *state)
1142 struct drm_connector *connector = state->connector;
1144 drm_printf(p, "connector[%u]: %s\n", connector->base.id, connector->name);
1145 drm_printf(p, "\tcrtc=%s\n", state->crtc ? state->crtc->name : "(null)");
1147 if (connector->funcs->atomic_print_state)
1148 connector->funcs->atomic_print_state(p, state);
1152 * drm_atomic_connector_get_property - get property value from connector state
1153 * @connector: the drm connector to set a property on
1154 * @state: the state object to get the property value from
1155 * @property: the property to set
1156 * @val: return location for the property value
1158 * This function handles generic/core properties and calls out to driver's
1159 * &drm_connector_funcs.atomic_get_property for driver properties. To ensure
1160 * consistent behavior you must call this function rather than the driver hook
1164 * Zero on success, error code on failure
1167 drm_atomic_connector_get_property(struct drm_connector *connector,
1168 const struct drm_connector_state *state,
1169 struct drm_property *property, uint64_t *val)
1171 struct drm_device *dev = connector->dev;
1172 struct drm_mode_config *config = &dev->mode_config;
1174 if (property == config->prop_crtc_id) {
1175 *val = (state->crtc) ? state->crtc->base.id : 0;
1176 } else if (property == config->dpms_property) {
1177 *val = connector->dpms;
1178 } else if (property == config->tv_select_subconnector_property) {
1179 *val = state->tv.subconnector;
1180 } else if (property == config->tv_left_margin_property) {
1181 *val = state->tv.margins.left;
1182 } else if (property == config->tv_right_margin_property) {
1183 *val = state->tv.margins.right;
1184 } else if (property == config->tv_top_margin_property) {
1185 *val = state->tv.margins.top;
1186 } else if (property == config->tv_bottom_margin_property) {
1187 *val = state->tv.margins.bottom;
1188 } else if (property == config->tv_mode_property) {
1189 *val = state->tv.mode;
1190 } else if (property == config->tv_brightness_property) {
1191 *val = state->tv.brightness;
1192 } else if (property == config->tv_contrast_property) {
1193 *val = state->tv.contrast;
1194 } else if (property == config->tv_flicker_reduction_property) {
1195 *val = state->tv.flicker_reduction;
1196 } else if (property == config->tv_overscan_property) {
1197 *val = state->tv.overscan;
1198 } else if (property == config->tv_saturation_property) {
1199 *val = state->tv.saturation;
1200 } else if (property == config->tv_hue_property) {
1201 *val = state->tv.hue;
1202 } else if (property == config->link_status_property) {
1203 *val = state->link_status;
1204 } else if (connector->funcs->atomic_get_property) {
1205 return connector->funcs->atomic_get_property(connector,
1206 state, property, val);
1214 int drm_atomic_get_property(struct drm_mode_object *obj,
1215 struct drm_property *property, uint64_t *val)
1217 struct drm_device *dev = property->dev;
1220 switch (obj->type) {
1221 case DRM_MODE_OBJECT_CONNECTOR: {
1222 struct drm_connector *connector = obj_to_connector(obj);
1223 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
1224 ret = drm_atomic_connector_get_property(connector,
1225 connector->state, property, val);
1228 case DRM_MODE_OBJECT_CRTC: {
1229 struct drm_crtc *crtc = obj_to_crtc(obj);
1230 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
1231 ret = drm_atomic_crtc_get_property(crtc,
1232 crtc->state, property, val);
1235 case DRM_MODE_OBJECT_PLANE: {
1236 struct drm_plane *plane = obj_to_plane(obj);
1237 WARN_ON(!drm_modeset_is_locked(&plane->mutex));
1238 ret = drm_atomic_plane_get_property(plane,
1239 plane->state, property, val);
1251 * drm_atomic_set_crtc_for_plane - set crtc for plane
1252 * @plane_state: the plane whose incoming state to update
1253 * @crtc: crtc to use for the plane
1255 * Changing the assigned crtc for a plane requires us to grab the lock and state
1256 * for the new crtc, as needed. This function takes care of all these details
1257 * besides updating the pointer in the state object itself.
1260 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1261 * then the w/w mutex code has detected a deadlock and the entire atomic
1262 * sequence must be restarted. All other errors are fatal.
1265 drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
1266 struct drm_crtc *crtc)
1268 struct drm_plane *plane = plane_state->plane;
1269 struct drm_crtc_state *crtc_state;
1271 if (plane_state->crtc) {
1272 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
1274 if (WARN_ON(IS_ERR(crtc_state)))
1275 return PTR_ERR(crtc_state);
1277 crtc_state->plane_mask &= ~(1 << drm_plane_index(plane));
1280 plane_state->crtc = crtc;
1283 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
1285 if (IS_ERR(crtc_state))
1286 return PTR_ERR(crtc_state);
1287 crtc_state->plane_mask |= (1 << drm_plane_index(plane));
1291 DRM_DEBUG_ATOMIC("Link plane state %p to [CRTC:%d:%s]\n",
1292 plane_state, crtc->base.id, crtc->name);
1294 DRM_DEBUG_ATOMIC("Link plane state %p to [NOCRTC]\n",
1299 EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane);
1302 * drm_atomic_set_fb_for_plane - set framebuffer for plane
1303 * @plane_state: atomic state object for the plane
1304 * @fb: fb to use for the plane
1306 * Changing the assigned framebuffer for a plane requires us to grab a reference
1307 * to the new fb and drop the reference to the old fb, if there is one. This
1308 * function takes care of all these details besides updating the pointer in the
1309 * state object itself.
1312 drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
1313 struct drm_framebuffer *fb)
1316 DRM_DEBUG_ATOMIC("Set [FB:%d] for plane state %p\n",
1317 fb->base.id, plane_state);
1319 DRM_DEBUG_ATOMIC("Set [NOFB] for plane state %p\n",
1322 drm_framebuffer_assign(&plane_state->fb, fb);
1324 EXPORT_SYMBOL(drm_atomic_set_fb_for_plane);
1327 * drm_atomic_set_fence_for_plane - set fence for plane
1328 * @plane_state: atomic state object for the plane
1329 * @fence: dma_fence to use for the plane
1331 * Helper to setup the plane_state fence in case it is not set yet.
1332 * By using this drivers doesn't need to worry if the user choose
1333 * implicit or explicit fencing.
1335 * This function will not set the fence to the state if it was set
1336 * via explicit fencing interfaces on the atomic ioctl. In that case it will
1337 * drop the reference to the fence as we are not storing it anywhere.
1338 * Otherwise, if &drm_plane_state.fence is not set this function we just set it
1339 * with the received implicit fence. In both cases this function consumes a
1340 * reference for @fence.
1343 drm_atomic_set_fence_for_plane(struct drm_plane_state *plane_state,
1344 struct dma_fence *fence)
1346 if (plane_state->fence) {
1347 dma_fence_put(fence);
1351 plane_state->fence = fence;
1353 EXPORT_SYMBOL(drm_atomic_set_fence_for_plane);
1356 * drm_atomic_set_crtc_for_connector - set crtc for connector
1357 * @conn_state: atomic state object for the connector
1358 * @crtc: crtc to use for the connector
1360 * Changing the assigned crtc for a connector requires us to grab the lock and
1361 * state for the new crtc, as needed. This function takes care of all these
1362 * details besides updating the pointer in the state object itself.
1365 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1366 * then the w/w mutex code has detected a deadlock and the entire atomic
1367 * sequence must be restarted. All other errors are fatal.
1370 drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
1371 struct drm_crtc *crtc)
1373 struct drm_crtc_state *crtc_state;
1375 if (conn_state->crtc == crtc)
1378 if (conn_state->crtc) {
1379 crtc_state = drm_atomic_get_new_crtc_state(conn_state->state,
1382 crtc_state->connector_mask &=
1383 ~(1 << drm_connector_index(conn_state->connector));
1385 drm_connector_put(conn_state->connector);
1386 conn_state->crtc = NULL;
1390 crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc);
1391 if (IS_ERR(crtc_state))
1392 return PTR_ERR(crtc_state);
1394 crtc_state->connector_mask |=
1395 1 << drm_connector_index(conn_state->connector);
1397 drm_connector_get(conn_state->connector);
1398 conn_state->crtc = crtc;
1400 DRM_DEBUG_ATOMIC("Link connector state %p to [CRTC:%d:%s]\n",
1401 conn_state, crtc->base.id, crtc->name);
1403 DRM_DEBUG_ATOMIC("Link connector state %p to [NOCRTC]\n",
1409 EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector);
1412 * drm_atomic_add_affected_connectors - add connectors for crtc
1413 * @state: atomic state
1416 * This function walks the current configuration and adds all connectors
1417 * currently using @crtc to the atomic configuration @state. Note that this
1418 * function must acquire the connection mutex. This can potentially cause
1419 * unneeded seralization if the update is just for the planes on one crtc. Hence
1420 * drivers and helpers should only call this when really needed (e.g. when a
1421 * full modeset needs to happen due to some change).
1424 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1425 * then the w/w mutex code has detected a deadlock and the entire atomic
1426 * sequence must be restarted. All other errors are fatal.
1429 drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
1430 struct drm_crtc *crtc)
1432 struct drm_mode_config *config = &state->dev->mode_config;
1433 struct drm_connector *connector;
1434 struct drm_connector_state *conn_state;
1435 struct drm_connector_list_iter conn_iter;
1436 struct drm_crtc_state *crtc_state;
1439 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1440 if (IS_ERR(crtc_state))
1441 return PTR_ERR(crtc_state);
1443 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
1447 DRM_DEBUG_ATOMIC("Adding all current connectors for [CRTC:%d:%s] to %p\n",
1448 crtc->base.id, crtc->name, state);
1451 * Changed connectors are already in @state, so only need to look
1452 * at the connector_mask in crtc_state.
1454 drm_connector_list_iter_begin(state->dev, &conn_iter);
1455 drm_for_each_connector_iter(connector, &conn_iter) {
1456 if (!(crtc_state->connector_mask & (1 << drm_connector_index(connector))))
1459 conn_state = drm_atomic_get_connector_state(state, connector);
1460 if (IS_ERR(conn_state)) {
1461 drm_connector_list_iter_end(&conn_iter);
1462 return PTR_ERR(conn_state);
1465 drm_connector_list_iter_end(&conn_iter);
1469 EXPORT_SYMBOL(drm_atomic_add_affected_connectors);
1472 * drm_atomic_add_affected_planes - add planes for crtc
1473 * @state: atomic state
1476 * This function walks the current configuration and adds all planes
1477 * currently used by @crtc to the atomic configuration @state. This is useful
1478 * when an atomic commit also needs to check all currently enabled plane on
1479 * @crtc, e.g. when changing the mode. It's also useful when re-enabling a CRTC
1480 * to avoid special code to force-enable all planes.
1482 * Since acquiring a plane state will always also acquire the w/w mutex of the
1483 * current CRTC for that plane (if there is any) adding all the plane states for
1484 * a CRTC will not reduce parallism of atomic updates.
1487 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1488 * then the w/w mutex code has detected a deadlock and the entire atomic
1489 * sequence must be restarted. All other errors are fatal.
1492 drm_atomic_add_affected_planes(struct drm_atomic_state *state,
1493 struct drm_crtc *crtc)
1495 struct drm_plane *plane;
1497 WARN_ON(!drm_atomic_get_new_crtc_state(state, crtc));
1499 drm_for_each_plane_mask(plane, state->dev, crtc->state->plane_mask) {
1500 struct drm_plane_state *plane_state =
1501 drm_atomic_get_plane_state(state, plane);
1503 if (IS_ERR(plane_state))
1504 return PTR_ERR(plane_state);
1508 EXPORT_SYMBOL(drm_atomic_add_affected_planes);
1511 * drm_atomic_legacy_backoff - locking backoff for legacy ioctls
1512 * @state: atomic state
1514 * This function should be used by legacy entry points which don't understand
1515 * -EDEADLK semantics. For simplicity this one will grab all modeset locks after
1516 * the slowpath completed.
1518 void drm_atomic_legacy_backoff(struct drm_atomic_state *state)
1520 struct drm_device *dev = state->dev;
1522 bool global = false;
1524 if (WARN_ON(dev->mode_config.acquire_ctx == state->acquire_ctx)) {
1527 dev->mode_config.acquire_ctx = NULL;
1531 drm_modeset_backoff(state->acquire_ctx);
1533 ret = drm_modeset_lock_all_ctx(dev, state->acquire_ctx);
1538 dev->mode_config.acquire_ctx = state->acquire_ctx;
1540 EXPORT_SYMBOL(drm_atomic_legacy_backoff);
1543 * drm_atomic_check_only - check whether a given config would work
1544 * @state: atomic configuration to check
1546 * Note that this function can return -EDEADLK if the driver needed to acquire
1547 * more locks but encountered a deadlock. The caller must then do the usual w/w
1548 * backoff dance and restart. All other errors are fatal.
1551 * 0 on success, negative error code on failure.
1553 int drm_atomic_check_only(struct drm_atomic_state *state)
1555 struct drm_device *dev = state->dev;
1556 struct drm_mode_config *config = &dev->mode_config;
1557 struct drm_plane *plane;
1558 struct drm_plane_state *plane_state;
1559 struct drm_crtc *crtc;
1560 struct drm_crtc_state *crtc_state;
1563 DRM_DEBUG_ATOMIC("checking %p\n", state);
1565 for_each_new_plane_in_state(state, plane, plane_state, i) {
1566 ret = drm_atomic_plane_check(plane, plane_state);
1568 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic core check failed\n",
1569 plane->base.id, plane->name);
1574 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
1575 ret = drm_atomic_crtc_check(crtc, crtc_state);
1577 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic core check failed\n",
1578 crtc->base.id, crtc->name);
1583 if (config->funcs->atomic_check)
1584 ret = config->funcs->atomic_check(state->dev, state);
1589 if (!state->allow_modeset) {
1590 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
1591 if (drm_atomic_crtc_needs_modeset(crtc_state)) {
1592 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] requires full modeset\n",
1593 crtc->base.id, crtc->name);
1601 EXPORT_SYMBOL(drm_atomic_check_only);
1604 * drm_atomic_commit - commit configuration atomically
1605 * @state: atomic configuration to check
1607 * Note that this function can return -EDEADLK if the driver needed to acquire
1608 * more locks but encountered a deadlock. The caller must then do the usual w/w
1609 * backoff dance and restart. All other errors are fatal.
1611 * This function will take its own reference on @state.
1612 * Callers should always release their reference with drm_atomic_state_put().
1615 * 0 on success, negative error code on failure.
1617 int drm_atomic_commit(struct drm_atomic_state *state)
1619 struct drm_mode_config *config = &state->dev->mode_config;
1622 ret = drm_atomic_check_only(state);
1626 DRM_DEBUG_ATOMIC("commiting %p\n", state);
1628 return config->funcs->atomic_commit(state->dev, state, false);
1630 EXPORT_SYMBOL(drm_atomic_commit);
1633 * drm_atomic_nonblocking_commit - atomic nonblocking commit
1634 * @state: atomic configuration to check
1636 * Note that this function can return -EDEADLK if the driver needed to acquire
1637 * more locks but encountered a deadlock. The caller must then do the usual w/w
1638 * backoff dance and restart. All other errors are fatal.
1640 * This function will take its own reference on @state.
1641 * Callers should always release their reference with drm_atomic_state_put().
1644 * 0 on success, negative error code on failure.
1646 int drm_atomic_nonblocking_commit(struct drm_atomic_state *state)
1648 struct drm_mode_config *config = &state->dev->mode_config;
1651 ret = drm_atomic_check_only(state);
1655 DRM_DEBUG_ATOMIC("commiting %p nonblocking\n", state);
1657 return config->funcs->atomic_commit(state->dev, state, true);
1659 EXPORT_SYMBOL(drm_atomic_nonblocking_commit);
1661 static void drm_atomic_print_state(const struct drm_atomic_state *state)
1663 struct drm_printer p = drm_info_printer(state->dev->dev);
1664 struct drm_plane *plane;
1665 struct drm_plane_state *plane_state;
1666 struct drm_crtc *crtc;
1667 struct drm_crtc_state *crtc_state;
1668 struct drm_connector *connector;
1669 struct drm_connector_state *connector_state;
1672 DRM_DEBUG_ATOMIC("checking %p\n", state);
1674 for_each_new_plane_in_state(state, plane, plane_state, i)
1675 drm_atomic_plane_print_state(&p, plane_state);
1677 for_each_new_crtc_in_state(state, crtc, crtc_state, i)
1678 drm_atomic_crtc_print_state(&p, crtc_state);
1680 for_each_new_connector_in_state(state, connector, connector_state, i)
1681 drm_atomic_connector_print_state(&p, connector_state);
1684 static void __drm_state_dump(struct drm_device *dev, struct drm_printer *p,
1687 struct drm_mode_config *config = &dev->mode_config;
1688 struct drm_plane *plane;
1689 struct drm_crtc *crtc;
1690 struct drm_connector *connector;
1691 struct drm_connector_list_iter conn_iter;
1693 if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
1696 list_for_each_entry(plane, &config->plane_list, head) {
1698 drm_modeset_lock(&plane->mutex, NULL);
1699 drm_atomic_plane_print_state(p, plane->state);
1701 drm_modeset_unlock(&plane->mutex);
1704 list_for_each_entry(crtc, &config->crtc_list, head) {
1706 drm_modeset_lock(&crtc->mutex, NULL);
1707 drm_atomic_crtc_print_state(p, crtc->state);
1709 drm_modeset_unlock(&crtc->mutex);
1712 drm_connector_list_iter_begin(dev, &conn_iter);
1714 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
1715 drm_for_each_connector_iter(connector, &conn_iter)
1716 drm_atomic_connector_print_state(p, connector->state);
1718 drm_modeset_unlock(&dev->mode_config.connection_mutex);
1719 drm_connector_list_iter_end(&conn_iter);
1723 * drm_state_dump - dump entire device atomic state
1724 * @dev: the drm device
1725 * @p: where to print the state to
1727 * Just for debugging. Drivers might want an option to dump state
1728 * to dmesg in case of error irq's. (Hint, you probably want to
1731 * The caller must drm_modeset_lock_all(), or if this is called
1732 * from error irq handler, it should not be enabled by default.
1733 * (Ie. if you are debugging errors you might not care that this
1734 * is racey. But calling this without all modeset locks held is
1735 * not inherently safe.)
1737 void drm_state_dump(struct drm_device *dev, struct drm_printer *p)
1739 __drm_state_dump(dev, p, false);
1741 EXPORT_SYMBOL(drm_state_dump);
1743 #ifdef CONFIG_DEBUG_FS
1744 static int drm_state_info(struct seq_file *m, void *data)
1746 struct drm_info_node *node = (struct drm_info_node *) m->private;
1747 struct drm_device *dev = node->minor->dev;
1748 struct drm_printer p = drm_seq_file_printer(m);
1750 __drm_state_dump(dev, &p, true);
1755 /* any use in debugfs files to dump individual planes/crtc/etc? */
1756 static const struct drm_info_list drm_atomic_debugfs_list[] = {
1757 {"state", drm_state_info, 0},
1760 int drm_atomic_debugfs_init(struct drm_minor *minor)
1762 return drm_debugfs_create_files(drm_atomic_debugfs_list,
1763 ARRAY_SIZE(drm_atomic_debugfs_list),
1764 minor->debugfs_root, minor);
1769 * The big monstor ioctl
1772 static struct drm_pending_vblank_event *create_vblank_event(
1773 struct drm_device *dev, uint64_t user_data)
1775 struct drm_pending_vblank_event *e = NULL;
1777 e = kzalloc(sizeof *e, GFP_KERNEL);
1781 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
1782 e->event.base.length = sizeof(e->event);
1783 e->event.user_data = user_data;
1788 static int atomic_set_prop(struct drm_atomic_state *state,
1789 struct drm_mode_object *obj, struct drm_property *prop,
1790 uint64_t prop_value)
1792 struct drm_mode_object *ref;
1795 if (!drm_property_change_valid_get(prop, prop_value, &ref))
1798 switch (obj->type) {
1799 case DRM_MODE_OBJECT_CONNECTOR: {
1800 struct drm_connector *connector = obj_to_connector(obj);
1801 struct drm_connector_state *connector_state;
1803 connector_state = drm_atomic_get_connector_state(state, connector);
1804 if (IS_ERR(connector_state)) {
1805 ret = PTR_ERR(connector_state);
1809 ret = drm_atomic_connector_set_property(connector,
1810 connector_state, prop, prop_value);
1813 case DRM_MODE_OBJECT_CRTC: {
1814 struct drm_crtc *crtc = obj_to_crtc(obj);
1815 struct drm_crtc_state *crtc_state;
1817 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1818 if (IS_ERR(crtc_state)) {
1819 ret = PTR_ERR(crtc_state);
1823 ret = drm_atomic_crtc_set_property(crtc,
1824 crtc_state, prop, prop_value);
1827 case DRM_MODE_OBJECT_PLANE: {
1828 struct drm_plane *plane = obj_to_plane(obj);
1829 struct drm_plane_state *plane_state;
1831 plane_state = drm_atomic_get_plane_state(state, plane);
1832 if (IS_ERR(plane_state)) {
1833 ret = PTR_ERR(plane_state);
1837 ret = drm_atomic_plane_set_property(plane,
1838 plane_state, prop, prop_value);
1846 drm_property_change_valid_put(prop, ref);
1851 * drm_atomic_clean_old_fb -- Unset old_fb pointers and set plane->fb pointers.
1853 * @dev: drm device to check.
1854 * @plane_mask: plane mask for planes that were updated.
1855 * @ret: return value, can be -EDEADLK for a retry.
1857 * Before doing an update &drm_plane.old_fb is set to &drm_plane.fb, but before
1858 * dropping the locks old_fb needs to be set to NULL and plane->fb updated. This
1859 * is a common operation for each atomic update, so this call is split off as a
1862 void drm_atomic_clean_old_fb(struct drm_device *dev,
1863 unsigned plane_mask,
1866 struct drm_plane *plane;
1868 /* if succeeded, fixup legacy plane crtc/fb ptrs before dropping
1869 * locks (ie. while it is still safe to deref plane->state). We
1870 * need to do this here because the driver entry points cannot
1871 * distinguish between legacy and atomic ioctls.
1873 drm_for_each_plane_mask(plane, dev, plane_mask) {
1875 struct drm_framebuffer *new_fb = plane->state->fb;
1877 drm_framebuffer_get(new_fb);
1879 plane->crtc = plane->state->crtc;
1882 drm_framebuffer_put(plane->old_fb);
1884 plane->old_fb = NULL;
1887 EXPORT_SYMBOL(drm_atomic_clean_old_fb);
1890 * DOC: explicit fencing properties
1892 * Explicit fencing allows userspace to control the buffer synchronization
1893 * between devices. A Fence or a group of fences are transfered to/from
1894 * userspace using Sync File fds and there are two DRM properties for that.
1895 * IN_FENCE_FD on each DRM Plane to send fences to the kernel and
1896 * OUT_FENCE_PTR on each DRM CRTC to receive fences from the kernel.
1898 * As a contrast, with implicit fencing the kernel keeps track of any
1899 * ongoing rendering, and automatically ensures that the atomic update waits
1900 * for any pending rendering to complete. For shared buffers represented with
1901 * a &struct dma_buf this is tracked in &struct reservation_object.
1902 * Implicit syncing is how Linux traditionally worked (e.g. DRI2/3 on X.org),
1903 * whereas explicit fencing is what Android wants.
1906 * Use this property to pass a fence that DRM should wait on before
1907 * proceeding with the Atomic Commit request and show the framebuffer for
1908 * the plane on the screen. The fence can be either a normal fence or a
1909 * merged one, the sync_file framework will handle both cases and use a
1910 * fence_array if a merged fence is received. Passing -1 here means no
1911 * fences to wait on.
1913 * If the Atomic Commit request has the DRM_MODE_ATOMIC_TEST_ONLY flag
1914 * it will only check if the Sync File is a valid one.
1916 * On the driver side the fence is stored on the @fence parameter of
1917 * &struct drm_plane_state. Drivers which also support implicit fencing
1918 * should set the implicit fence using drm_atomic_set_fence_for_plane(),
1919 * to make sure there's consistent behaviour between drivers in precedence
1920 * of implicit vs. explicit fencing.
1923 * Use this property to pass a file descriptor pointer to DRM. Once the
1924 * Atomic Commit request call returns OUT_FENCE_PTR will be filled with
1925 * the file descriptor number of a Sync File. This Sync File contains the
1926 * CRTC fence that will be signaled when all framebuffers present on the
1927 * Atomic Commit * request for that given CRTC are scanned out on the
1930 * The Atomic Commit request fails if a invalid pointer is passed. If the
1931 * Atomic Commit request fails for any other reason the out fence fd
1932 * returned will be -1. On a Atomic Commit with the
1933 * DRM_MODE_ATOMIC_TEST_ONLY flag the out fence will also be set to -1.
1935 * Note that out-fences don't have a special interface to drivers and are
1936 * internally represented by a &struct drm_pending_vblank_event in struct
1937 * &drm_crtc_state, which is also used by the nonblocking atomic commit
1938 * helpers and for the DRM event handling for existing userspace.
1941 struct drm_out_fence_state {
1942 s32 __user *out_fence_ptr;
1943 struct sync_file *sync_file;
1947 static int setup_out_fence(struct drm_out_fence_state *fence_state,
1948 struct dma_fence *fence)
1950 fence_state->fd = get_unused_fd_flags(O_CLOEXEC);
1951 if (fence_state->fd < 0)
1952 return fence_state->fd;
1954 if (put_user(fence_state->fd, fence_state->out_fence_ptr))
1957 fence_state->sync_file = sync_file_create(fence);
1958 if (!fence_state->sync_file)
1964 static int prepare_crtc_signaling(struct drm_device *dev,
1965 struct drm_atomic_state *state,
1966 struct drm_mode_atomic *arg,
1967 struct drm_file *file_priv,
1968 struct drm_out_fence_state **fence_state,
1969 unsigned int *num_fences)
1971 struct drm_crtc *crtc;
1972 struct drm_crtc_state *crtc_state;
1975 if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)
1978 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
1979 s32 __user *fence_ptr;
1981 fence_ptr = get_out_fence_for_crtc(crtc_state->state, crtc);
1983 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT || fence_ptr) {
1984 struct drm_pending_vblank_event *e;
1986 e = create_vblank_event(dev, arg->user_data);
1990 crtc_state->event = e;
1993 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
1994 struct drm_pending_vblank_event *e = crtc_state->event;
1999 ret = drm_event_reserve_init(dev, file_priv, &e->base,
2003 crtc_state->event = NULL;
2009 struct dma_fence *fence;
2010 struct drm_out_fence_state *f;
2012 f = krealloc(*fence_state,
2013 sizeof(**fence_state) * (*num_fences + 1),
2018 memset(&f[*num_fences], 0, sizeof(*f));
2020 f[*num_fences].out_fence_ptr = fence_ptr;
2023 fence = drm_crtc_create_fence(crtc);
2027 ret = setup_out_fence(&f[(*num_fences)++], fence);
2029 dma_fence_put(fence);
2033 crtc_state->event->base.fence = fence;
2040 static void complete_crtc_signaling(struct drm_device *dev,
2041 struct drm_atomic_state *state,
2042 struct drm_out_fence_state *fence_state,
2043 unsigned int num_fences,
2046 struct drm_crtc *crtc;
2047 struct drm_crtc_state *crtc_state;
2051 for (i = 0; i < num_fences; i++)
2052 fd_install(fence_state[i].fd,
2053 fence_state[i].sync_file->file);
2059 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
2060 struct drm_pending_vblank_event *event = crtc_state->event;
2062 * Free the allocated event. drm_atomic_helper_setup_commit
2063 * can allocate an event too, so only free it if it's ours
2064 * to prevent a double free in drm_atomic_state_clear.
2066 if (event && (event->base.fence || event->base.file_priv)) {
2067 drm_event_cancel_free(dev, &event->base);
2068 crtc_state->event = NULL;
2075 for (i = 0; i < num_fences; i++) {
2076 if (fence_state[i].sync_file)
2077 fput(fence_state[i].sync_file->file);
2078 if (fence_state[i].fd >= 0)
2079 put_unused_fd(fence_state[i].fd);
2081 /* If this fails log error to the user */
2082 if (fence_state[i].out_fence_ptr &&
2083 put_user(-1, fence_state[i].out_fence_ptr))
2084 DRM_DEBUG_ATOMIC("Couldn't clear out_fence_ptr\n");
2090 int drm_mode_atomic_ioctl(struct drm_device *dev,
2091 void *data, struct drm_file *file_priv)
2093 struct drm_mode_atomic *arg = data;
2094 uint32_t __user *objs_ptr = (uint32_t __user *)(unsigned long)(arg->objs_ptr);
2095 uint32_t __user *count_props_ptr = (uint32_t __user *)(unsigned long)(arg->count_props_ptr);
2096 uint32_t __user *props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
2097 uint64_t __user *prop_values_ptr = (uint64_t __user *)(unsigned long)(arg->prop_values_ptr);
2098 unsigned int copied_objs, copied_props;
2099 struct drm_atomic_state *state;
2100 struct drm_modeset_acquire_ctx ctx;
2101 struct drm_plane *plane;
2102 struct drm_out_fence_state *fence_state;
2103 unsigned plane_mask;
2105 unsigned int i, j, num_fences;
2107 /* disallow for drivers not supporting atomic: */
2108 if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
2111 /* disallow for userspace that has not enabled atomic cap (even
2112 * though this may be a bit overkill, since legacy userspace
2113 * wouldn't know how to call this ioctl)
2115 if (!file_priv->atomic)
2118 if (arg->flags & ~DRM_MODE_ATOMIC_FLAGS)
2124 if ((arg->flags & DRM_MODE_PAGE_FLIP_ASYNC) &&
2125 !dev->mode_config.async_page_flip)
2128 /* can't test and expect an event at the same time. */
2129 if ((arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) &&
2130 (arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
2133 drm_modeset_acquire_init(&ctx, 0);
2135 state = drm_atomic_state_alloc(dev);
2139 state->acquire_ctx = &ctx;
2140 state->allow_modeset = !!(arg->flags & DRM_MODE_ATOMIC_ALLOW_MODESET);
2149 for (i = 0; i < arg->count_objs; i++) {
2150 uint32_t obj_id, count_props;
2151 struct drm_mode_object *obj;
2153 if (get_user(obj_id, objs_ptr + copied_objs)) {
2158 obj = drm_mode_object_find(dev, obj_id, DRM_MODE_OBJECT_ANY);
2164 if (!obj->properties) {
2165 drm_mode_object_put(obj);
2170 if (get_user(count_props, count_props_ptr + copied_objs)) {
2171 drm_mode_object_put(obj);
2178 for (j = 0; j < count_props; j++) {
2180 uint64_t prop_value;
2181 struct drm_property *prop;
2183 if (get_user(prop_id, props_ptr + copied_props)) {
2184 drm_mode_object_put(obj);
2189 prop = drm_mode_obj_find_prop_id(obj, prop_id);
2191 drm_mode_object_put(obj);
2196 if (copy_from_user(&prop_value,
2197 prop_values_ptr + copied_props,
2198 sizeof(prop_value))) {
2199 drm_mode_object_put(obj);
2204 ret = atomic_set_prop(state, obj, prop, prop_value);
2206 drm_mode_object_put(obj);
2213 if (obj->type == DRM_MODE_OBJECT_PLANE && count_props &&
2214 !(arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)) {
2215 plane = obj_to_plane(obj);
2216 plane_mask |= (1 << drm_plane_index(plane));
2217 plane->old_fb = plane->fb;
2219 drm_mode_object_put(obj);
2222 ret = prepare_crtc_signaling(dev, state, arg, file_priv, &fence_state,
2227 if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) {
2228 ret = drm_atomic_check_only(state);
2229 } else if (arg->flags & DRM_MODE_ATOMIC_NONBLOCK) {
2230 ret = drm_atomic_nonblocking_commit(state);
2232 if (unlikely(drm_debug & DRM_UT_STATE))
2233 drm_atomic_print_state(state);
2235 ret = drm_atomic_commit(state);
2239 drm_atomic_clean_old_fb(dev, plane_mask, ret);
2241 complete_crtc_signaling(dev, state, fence_state, num_fences, !ret);
2243 if (ret == -EDEADLK) {
2244 drm_atomic_state_clear(state);
2245 drm_modeset_backoff(&ctx);
2249 drm_atomic_state_put(state);
2251 drm_modeset_drop_locks(&ctx);
2252 drm_modeset_acquire_fini(&ctx);