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);
60 kfree(state->private_objs);
62 EXPORT_SYMBOL(drm_atomic_state_default_release);
65 * drm_atomic_state_init - init new atomic state
67 * @state: atomic state
69 * Default implementation for filling in a new atomic state.
70 * This is useful for drivers that subclass the atomic state.
73 drm_atomic_state_init(struct drm_device *dev, struct drm_atomic_state *state)
75 kref_init(&state->ref);
77 /* TODO legacy paths should maybe do a better job about
78 * setting this appropriately?
80 state->allow_modeset = true;
82 state->crtcs = kcalloc(dev->mode_config.num_crtc,
83 sizeof(*state->crtcs), GFP_KERNEL);
86 state->planes = kcalloc(dev->mode_config.num_total_plane,
87 sizeof(*state->planes), GFP_KERNEL);
93 DRM_DEBUG_ATOMIC("Allocated atomic state %p\n", state);
97 drm_atomic_state_default_release(state);
100 EXPORT_SYMBOL(drm_atomic_state_init);
103 * drm_atomic_state_alloc - allocate atomic state
106 * This allocates an empty atomic state to track updates.
108 struct drm_atomic_state *
109 drm_atomic_state_alloc(struct drm_device *dev)
111 struct drm_mode_config *config = &dev->mode_config;
112 struct drm_atomic_state *state;
114 if (!config->funcs->atomic_state_alloc) {
115 state = kzalloc(sizeof(*state), GFP_KERNEL);
118 if (drm_atomic_state_init(dev, state) < 0) {
125 return config->funcs->atomic_state_alloc(dev);
127 EXPORT_SYMBOL(drm_atomic_state_alloc);
130 * drm_atomic_state_default_clear - clear base atomic state
131 * @state: atomic state
133 * Default implementation for clearing atomic state.
134 * This is useful for drivers that subclass the atomic state.
136 void drm_atomic_state_default_clear(struct drm_atomic_state *state)
138 struct drm_device *dev = state->dev;
139 struct drm_mode_config *config = &dev->mode_config;
142 DRM_DEBUG_ATOMIC("Clearing atomic state %p\n", state);
144 for (i = 0; i < state->num_connector; i++) {
145 struct drm_connector *connector = state->connectors[i].ptr;
150 connector->funcs->atomic_destroy_state(connector,
151 state->connectors[i].state);
152 state->connectors[i].ptr = NULL;
153 state->connectors[i].state = NULL;
154 drm_connector_put(connector);
157 for (i = 0; i < config->num_crtc; i++) {
158 struct drm_crtc *crtc = state->crtcs[i].ptr;
163 crtc->funcs->atomic_destroy_state(crtc,
164 state->crtcs[i].state);
166 if (state->crtcs[i].commit) {
167 kfree(state->crtcs[i].commit->event);
168 state->crtcs[i].commit->event = NULL;
169 drm_crtc_commit_put(state->crtcs[i].commit);
172 state->crtcs[i].commit = NULL;
173 state->crtcs[i].ptr = NULL;
174 state->crtcs[i].state = NULL;
177 for (i = 0; i < config->num_total_plane; i++) {
178 struct drm_plane *plane = state->planes[i].ptr;
183 plane->funcs->atomic_destroy_state(plane,
184 state->planes[i].state);
185 state->planes[i].ptr = NULL;
186 state->planes[i].state = NULL;
189 for (i = 0; i < state->num_private_objs; i++) {
190 void *obj_state = state->private_objs[i].obj_state;
192 state->private_objs[i].funcs->destroy_state(obj_state);
193 state->private_objs[i].obj = NULL;
194 state->private_objs[i].obj_state = NULL;
195 state->private_objs[i].funcs = NULL;
197 state->num_private_objs = 0;
200 EXPORT_SYMBOL(drm_atomic_state_default_clear);
203 * drm_atomic_state_clear - clear state object
204 * @state: atomic state
206 * When the w/w mutex algorithm detects a deadlock we need to back off and drop
207 * all locks. So someone else could sneak in and change the current modeset
208 * configuration. Which means that all the state assembled in @state is no
209 * longer an atomic update to the current state, but to some arbitrary earlier
210 * state. Which could break assumptions the driver's
211 * &drm_mode_config_funcs.atomic_check likely relies on.
213 * Hence we must clear all cached state and completely start over, using this
216 void drm_atomic_state_clear(struct drm_atomic_state *state)
218 struct drm_device *dev = state->dev;
219 struct drm_mode_config *config = &dev->mode_config;
221 if (config->funcs->atomic_state_clear)
222 config->funcs->atomic_state_clear(state);
224 drm_atomic_state_default_clear(state);
226 EXPORT_SYMBOL(drm_atomic_state_clear);
229 * __drm_atomic_state_free - free all memory for an atomic state
230 * @ref: This atomic state to deallocate
232 * This frees all memory associated with an atomic state, including all the
233 * per-object state for planes, crtcs and connectors.
235 void __drm_atomic_state_free(struct kref *ref)
237 struct drm_atomic_state *state = container_of(ref, typeof(*state), ref);
238 struct drm_mode_config *config = &state->dev->mode_config;
240 drm_atomic_state_clear(state);
242 DRM_DEBUG_ATOMIC("Freeing atomic state %p\n", state);
244 if (config->funcs->atomic_state_free) {
245 config->funcs->atomic_state_free(state);
247 drm_atomic_state_default_release(state);
251 EXPORT_SYMBOL(__drm_atomic_state_free);
254 * drm_atomic_get_crtc_state - get crtc state
255 * @state: global atomic state object
256 * @crtc: crtc to get state object for
258 * This function returns the crtc state for the given crtc, allocating it if
259 * needed. It will also grab the relevant crtc lock to make sure that the state
264 * Either the allocated state or the error code encoded into the pointer. When
265 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
266 * entire atomic sequence must be restarted. All other errors are fatal.
268 struct drm_crtc_state *
269 drm_atomic_get_crtc_state(struct drm_atomic_state *state,
270 struct drm_crtc *crtc)
272 int ret, index = drm_crtc_index(crtc);
273 struct drm_crtc_state *crtc_state;
275 WARN_ON(!state->acquire_ctx);
277 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
281 ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx);
285 crtc_state = crtc->funcs->atomic_duplicate_state(crtc);
287 return ERR_PTR(-ENOMEM);
289 state->crtcs[index].state = crtc_state;
290 state->crtcs[index].old_state = crtc->state;
291 state->crtcs[index].new_state = crtc_state;
292 state->crtcs[index].ptr = crtc;
293 crtc_state->state = state;
295 DRM_DEBUG_ATOMIC("Added [CRTC:%d:%s] %p state to %p\n",
296 crtc->base.id, crtc->name, crtc_state, state);
300 EXPORT_SYMBOL(drm_atomic_get_crtc_state);
302 static void set_out_fence_for_crtc(struct drm_atomic_state *state,
303 struct drm_crtc *crtc, s32 __user *fence_ptr)
305 state->crtcs[drm_crtc_index(crtc)].out_fence_ptr = fence_ptr;
308 static s32 __user *get_out_fence_for_crtc(struct drm_atomic_state *state,
309 struct drm_crtc *crtc)
311 s32 __user *fence_ptr;
313 fence_ptr = state->crtcs[drm_crtc_index(crtc)].out_fence_ptr;
314 state->crtcs[drm_crtc_index(crtc)].out_fence_ptr = NULL;
320 * drm_atomic_set_mode_for_crtc - set mode for CRTC
321 * @state: the CRTC whose incoming state to update
322 * @mode: kernel-internal mode to use for the CRTC, or NULL to disable
324 * Set a mode (originating from the kernel) on the desired CRTC state and update
325 * the enable property.
328 * Zero on success, error code on failure. Cannot return -EDEADLK.
330 int drm_atomic_set_mode_for_crtc(struct drm_crtc_state *state,
331 struct drm_display_mode *mode)
333 struct drm_mode_modeinfo umode;
335 /* Early return for no change. */
336 if (mode && memcmp(&state->mode, mode, sizeof(*mode)) == 0)
339 drm_property_blob_put(state->mode_blob);
340 state->mode_blob = NULL;
343 drm_mode_convert_to_umode(&umode, mode);
345 drm_property_create_blob(state->crtc->dev,
348 if (IS_ERR(state->mode_blob))
349 return PTR_ERR(state->mode_blob);
351 drm_mode_copy(&state->mode, mode);
352 state->enable = true;
353 DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n",
356 memset(&state->mode, 0, sizeof(state->mode));
357 state->enable = false;
358 DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n",
364 EXPORT_SYMBOL(drm_atomic_set_mode_for_crtc);
367 * drm_atomic_set_mode_prop_for_crtc - set mode for CRTC
368 * @state: the CRTC whose incoming state to update
369 * @blob: pointer to blob property to use for mode
371 * Set a mode (originating from a blob property) on the desired CRTC state.
372 * This function will take a reference on the blob property for the CRTC state,
373 * and release the reference held on the state's existing mode property, if any
377 * Zero on success, error code on failure. Cannot return -EDEADLK.
379 int drm_atomic_set_mode_prop_for_crtc(struct drm_crtc_state *state,
380 struct drm_property_blob *blob)
382 if (blob == state->mode_blob)
385 drm_property_blob_put(state->mode_blob);
386 state->mode_blob = NULL;
388 memset(&state->mode, 0, sizeof(state->mode));
391 if (blob->length != sizeof(struct drm_mode_modeinfo) ||
392 drm_mode_convert_umode(&state->mode,
393 (const struct drm_mode_modeinfo *)
397 state->mode_blob = drm_property_blob_get(blob);
398 state->enable = true;
399 DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n",
400 state->mode.name, state);
402 state->enable = false;
403 DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n",
409 EXPORT_SYMBOL(drm_atomic_set_mode_prop_for_crtc);
412 * drm_atomic_replace_property_blob - replace a blob property
413 * @blob: a pointer to the member blob to be replaced
414 * @new_blob: the new blob to replace with
415 * @replaced: whether the blob has been replaced
418 * Zero on success, error code on failure
421 drm_atomic_replace_property_blob(struct drm_property_blob **blob,
422 struct drm_property_blob *new_blob,
425 struct drm_property_blob *old_blob = *blob;
427 if (old_blob == new_blob)
430 drm_property_blob_put(old_blob);
432 drm_property_blob_get(new_blob);
440 drm_atomic_replace_property_blob_from_id(struct drm_device *dev,
441 struct drm_property_blob **blob,
443 ssize_t expected_size,
446 struct drm_property_blob *new_blob = NULL;
449 new_blob = drm_property_lookup_blob(dev, blob_id);
450 if (new_blob == NULL)
453 if (expected_size > 0 && expected_size != new_blob->length) {
454 drm_property_blob_put(new_blob);
459 drm_atomic_replace_property_blob(blob, new_blob, replaced);
460 drm_property_blob_put(new_blob);
466 * drm_atomic_crtc_set_property - set property on CRTC
467 * @crtc: the drm CRTC to set a property on
468 * @state: the state object to update with the new property value
469 * @property: the property to set
470 * @val: the new property value
472 * This function handles generic/core properties and calls out to driver's
473 * &drm_crtc_funcs.atomic_set_property for driver properties. To ensure
474 * consistent behavior you must call this function rather than the driver hook
478 * Zero on success, error code on failure
480 int drm_atomic_crtc_set_property(struct drm_crtc *crtc,
481 struct drm_crtc_state *state, struct drm_property *property,
484 struct drm_device *dev = crtc->dev;
485 struct drm_mode_config *config = &dev->mode_config;
486 bool replaced = false;
489 if (property == config->prop_active)
491 else if (property == config->prop_mode_id) {
492 struct drm_property_blob *mode =
493 drm_property_lookup_blob(dev, val);
494 ret = drm_atomic_set_mode_prop_for_crtc(state, mode);
495 drm_property_blob_put(mode);
497 } else if (property == config->degamma_lut_property) {
498 ret = drm_atomic_replace_property_blob_from_id(dev,
503 state->color_mgmt_changed |= replaced;
505 } else if (property == config->ctm_property) {
506 ret = drm_atomic_replace_property_blob_from_id(dev,
509 sizeof(struct drm_color_ctm),
511 state->color_mgmt_changed |= replaced;
513 } else if (property == config->gamma_lut_property) {
514 ret = drm_atomic_replace_property_blob_from_id(dev,
519 state->color_mgmt_changed |= replaced;
521 } else if (property == config->prop_out_fence_ptr) {
522 s32 __user *fence_ptr = u64_to_user_ptr(val);
527 if (put_user(-1, fence_ptr))
530 set_out_fence_for_crtc(state->state, crtc, fence_ptr);
531 } else if (crtc->funcs->atomic_set_property)
532 return crtc->funcs->atomic_set_property(crtc, state, property, val);
538 EXPORT_SYMBOL(drm_atomic_crtc_set_property);
541 * drm_atomic_crtc_get_property - get property value from CRTC state
542 * @crtc: the drm CRTC to set a property on
543 * @state: the state object to get the property value from
544 * @property: the property to set
545 * @val: return location for the property value
547 * This function handles generic/core properties and calls out to driver's
548 * &drm_crtc_funcs.atomic_get_property for driver properties. To ensure
549 * consistent behavior you must call this function rather than the driver hook
553 * Zero on success, error code on failure
556 drm_atomic_crtc_get_property(struct drm_crtc *crtc,
557 const struct drm_crtc_state *state,
558 struct drm_property *property, uint64_t *val)
560 struct drm_device *dev = crtc->dev;
561 struct drm_mode_config *config = &dev->mode_config;
563 if (property == config->prop_active)
564 *val = state->active;
565 else if (property == config->prop_mode_id)
566 *val = (state->mode_blob) ? state->mode_blob->base.id : 0;
567 else if (property == config->degamma_lut_property)
568 *val = (state->degamma_lut) ? state->degamma_lut->base.id : 0;
569 else if (property == config->ctm_property)
570 *val = (state->ctm) ? state->ctm->base.id : 0;
571 else if (property == config->gamma_lut_property)
572 *val = (state->gamma_lut) ? state->gamma_lut->base.id : 0;
573 else if (property == config->prop_out_fence_ptr)
575 else if (crtc->funcs->atomic_get_property)
576 return crtc->funcs->atomic_get_property(crtc, state, property, val);
584 * drm_atomic_crtc_check - check crtc state
585 * @crtc: crtc to check
586 * @state: crtc state to check
588 * Provides core sanity checks for crtc state.
591 * Zero on success, error code on failure
593 static int drm_atomic_crtc_check(struct drm_crtc *crtc,
594 struct drm_crtc_state *state)
596 /* NOTE: we explicitly don't enforce constraints such as primary
597 * layer covering entire screen, since that is something we want
598 * to allow (on hw that supports it). For hw that does not, it
599 * should be checked in driver's crtc->atomic_check() vfunc.
601 * TODO: Add generic modeset state checks once we support those.
604 if (state->active && !state->enable) {
605 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active without enabled\n",
606 crtc->base.id, crtc->name);
610 /* The state->enable vs. state->mode_blob checks can be WARN_ON,
611 * as this is a kernel-internal detail that userspace should never
612 * be able to trigger. */
613 if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
614 WARN_ON(state->enable && !state->mode_blob)) {
615 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled without mode blob\n",
616 crtc->base.id, crtc->name);
620 if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
621 WARN_ON(!state->enable && state->mode_blob)) {
622 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] disabled with mode blob\n",
623 crtc->base.id, crtc->name);
628 * Reject event generation for when a CRTC is off and stays off.
629 * It wouldn't be hard to implement this, but userspace has a track
630 * record of happily burning through 100% cpu (or worse, crash) when the
631 * display pipe is suspended. To avoid all that fun just reject updates
632 * that ask for events since likely that indicates a bug in the
633 * compositor's drawing loop. This is consistent with the vblank IOCTL
634 * and legacy page_flip IOCTL which also reject service on a disabled
637 if (state->event && !state->active && !crtc->state->active) {
638 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] requesting event but off\n",
639 crtc->base.id, crtc->name);
646 static void drm_atomic_crtc_print_state(struct drm_printer *p,
647 const struct drm_crtc_state *state)
649 struct drm_crtc *crtc = state->crtc;
651 drm_printf(p, "crtc[%u]: %s\n", crtc->base.id, crtc->name);
652 drm_printf(p, "\tenable=%d\n", state->enable);
653 drm_printf(p, "\tactive=%d\n", state->active);
654 drm_printf(p, "\tplanes_changed=%d\n", state->planes_changed);
655 drm_printf(p, "\tmode_changed=%d\n", state->mode_changed);
656 drm_printf(p, "\tactive_changed=%d\n", state->active_changed);
657 drm_printf(p, "\tconnectors_changed=%d\n", state->connectors_changed);
658 drm_printf(p, "\tcolor_mgmt_changed=%d\n", state->color_mgmt_changed);
659 drm_printf(p, "\tplane_mask=%x\n", state->plane_mask);
660 drm_printf(p, "\tconnector_mask=%x\n", state->connector_mask);
661 drm_printf(p, "\tencoder_mask=%x\n", state->encoder_mask);
662 drm_printf(p, "\tmode: " DRM_MODE_FMT "\n", DRM_MODE_ARG(&state->mode));
664 if (crtc->funcs->atomic_print_state)
665 crtc->funcs->atomic_print_state(p, state);
669 * drm_atomic_get_plane_state - get plane state
670 * @state: global atomic state object
671 * @plane: plane to get state object for
673 * This function returns the plane state for the given plane, allocating it if
674 * needed. It will also grab the relevant plane lock to make sure that the state
679 * Either the allocated state or the error code encoded into the pointer. When
680 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
681 * entire atomic sequence must be restarted. All other errors are fatal.
683 struct drm_plane_state *
684 drm_atomic_get_plane_state(struct drm_atomic_state *state,
685 struct drm_plane *plane)
687 int ret, index = drm_plane_index(plane);
688 struct drm_plane_state *plane_state;
690 WARN_ON(!state->acquire_ctx);
692 plane_state = drm_atomic_get_existing_plane_state(state, plane);
696 ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
700 plane_state = plane->funcs->atomic_duplicate_state(plane);
702 return ERR_PTR(-ENOMEM);
704 state->planes[index].state = plane_state;
705 state->planes[index].ptr = plane;
706 state->planes[index].old_state = plane->state;
707 state->planes[index].new_state = plane_state;
708 plane_state->state = state;
710 DRM_DEBUG_ATOMIC("Added [PLANE:%d:%s] %p state to %p\n",
711 plane->base.id, plane->name, plane_state, state);
713 if (plane_state->crtc) {
714 struct drm_crtc_state *crtc_state;
716 crtc_state = drm_atomic_get_crtc_state(state,
718 if (IS_ERR(crtc_state))
719 return ERR_CAST(crtc_state);
724 EXPORT_SYMBOL(drm_atomic_get_plane_state);
727 * drm_atomic_plane_set_property - set property on plane
728 * @plane: the drm plane to set a property on
729 * @state: the state object to update with the new property value
730 * @property: the property to set
731 * @val: the new property value
733 * This function handles generic/core properties and calls out to driver's
734 * &drm_plane_funcs.atomic_set_property for driver properties. To ensure
735 * consistent behavior you must call this function rather than the driver hook
739 * Zero on success, error code on failure
741 int drm_atomic_plane_set_property(struct drm_plane *plane,
742 struct drm_plane_state *state, struct drm_property *property,
745 struct drm_device *dev = plane->dev;
746 struct drm_mode_config *config = &dev->mode_config;
748 if (property == config->prop_fb_id) {
749 struct drm_framebuffer *fb = drm_framebuffer_lookup(dev, val);
750 drm_atomic_set_fb_for_plane(state, fb);
752 drm_framebuffer_put(fb);
753 } else if (property == config->prop_in_fence_fd) {
757 if (U642I64(val) == -1)
760 state->fence = sync_file_get_fence(val);
764 } else if (property == config->prop_crtc_id) {
765 struct drm_crtc *crtc = drm_crtc_find(dev, val);
766 return drm_atomic_set_crtc_for_plane(state, crtc);
767 } else if (property == config->prop_crtc_x) {
768 state->crtc_x = U642I64(val);
769 } else if (property == config->prop_crtc_y) {
770 state->crtc_y = U642I64(val);
771 } else if (property == config->prop_crtc_w) {
773 } else if (property == config->prop_crtc_h) {
775 } else if (property == config->prop_src_x) {
777 } else if (property == config->prop_src_y) {
779 } else if (property == config->prop_src_w) {
781 } else if (property == config->prop_src_h) {
783 } else if (property == plane->rotation_property) {
784 if (!is_power_of_2(val & DRM_ROTATE_MASK))
786 state->rotation = val;
787 } else if (property == plane->zpos_property) {
789 } else if (plane->funcs->atomic_set_property) {
790 return plane->funcs->atomic_set_property(plane, state,
798 EXPORT_SYMBOL(drm_atomic_plane_set_property);
801 * drm_atomic_plane_get_property - get property value from plane state
802 * @plane: the drm plane to set a property on
803 * @state: the state object to get the property value from
804 * @property: the property to set
805 * @val: return location for the property value
807 * This function handles generic/core properties and calls out to driver's
808 * &drm_plane_funcs.atomic_get_property for driver properties. To ensure
809 * consistent behavior you must call this function rather than the driver hook
813 * Zero on success, error code on failure
816 drm_atomic_plane_get_property(struct drm_plane *plane,
817 const struct drm_plane_state *state,
818 struct drm_property *property, uint64_t *val)
820 struct drm_device *dev = plane->dev;
821 struct drm_mode_config *config = &dev->mode_config;
823 if (property == config->prop_fb_id) {
824 *val = (state->fb) ? state->fb->base.id : 0;
825 } else if (property == config->prop_in_fence_fd) {
827 } else if (property == config->prop_crtc_id) {
828 *val = (state->crtc) ? state->crtc->base.id : 0;
829 } else if (property == config->prop_crtc_x) {
830 *val = I642U64(state->crtc_x);
831 } else if (property == config->prop_crtc_y) {
832 *val = I642U64(state->crtc_y);
833 } else if (property == config->prop_crtc_w) {
834 *val = state->crtc_w;
835 } else if (property == config->prop_crtc_h) {
836 *val = state->crtc_h;
837 } else if (property == config->prop_src_x) {
839 } else if (property == config->prop_src_y) {
841 } else if (property == config->prop_src_w) {
843 } else if (property == config->prop_src_h) {
845 } else if (property == plane->rotation_property) {
846 *val = state->rotation;
847 } else if (property == plane->zpos_property) {
849 } else if (plane->funcs->atomic_get_property) {
850 return plane->funcs->atomic_get_property(plane, state, property, val);
859 plane_switching_crtc(struct drm_atomic_state *state,
860 struct drm_plane *plane,
861 struct drm_plane_state *plane_state)
863 if (!plane->state->crtc || !plane_state->crtc)
866 if (plane->state->crtc == plane_state->crtc)
869 /* This could be refined, but currently there's no helper or driver code
870 * to implement direct switching of active planes nor userspace to take
871 * advantage of more direct plane switching without the intermediate
878 * drm_atomic_plane_check - check plane state
879 * @plane: plane to check
880 * @state: plane state to check
882 * Provides core sanity checks for plane state.
885 * Zero on success, error code on failure
887 static int drm_atomic_plane_check(struct drm_plane *plane,
888 struct drm_plane_state *state)
890 unsigned int fb_width, fb_height;
893 /* either *both* CRTC and FB must be set, or neither */
894 if (WARN_ON(state->crtc && !state->fb)) {
895 DRM_DEBUG_ATOMIC("CRTC set but no FB\n");
897 } else if (WARN_ON(state->fb && !state->crtc)) {
898 DRM_DEBUG_ATOMIC("FB set but no CRTC\n");
902 /* if disabled, we don't care about the rest of the state: */
906 /* Check whether this plane is usable on this CRTC */
907 if (!(plane->possible_crtcs & drm_crtc_mask(state->crtc))) {
908 DRM_DEBUG_ATOMIC("Invalid crtc for plane\n");
912 /* Check whether this plane supports the fb pixel format. */
913 ret = drm_plane_check_pixel_format(plane, state->fb->format->format);
915 struct drm_format_name_buf format_name;
916 DRM_DEBUG_ATOMIC("Invalid pixel format %s\n",
917 drm_get_format_name(state->fb->format->format,
922 /* Give drivers some help against integer overflows */
923 if (state->crtc_w > INT_MAX ||
924 state->crtc_x > INT_MAX - (int32_t) state->crtc_w ||
925 state->crtc_h > INT_MAX ||
926 state->crtc_y > INT_MAX - (int32_t) state->crtc_h) {
927 DRM_DEBUG_ATOMIC("Invalid CRTC coordinates %ux%u+%d+%d\n",
928 state->crtc_w, state->crtc_h,
929 state->crtc_x, state->crtc_y);
933 fb_width = state->fb->width << 16;
934 fb_height = state->fb->height << 16;
936 /* Make sure source coordinates are inside the fb. */
937 if (state->src_w > fb_width ||
938 state->src_x > fb_width - state->src_w ||
939 state->src_h > fb_height ||
940 state->src_y > fb_height - state->src_h) {
941 DRM_DEBUG_ATOMIC("Invalid source coordinates "
942 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
943 state->src_w >> 16, ((state->src_w & 0xffff) * 15625) >> 10,
944 state->src_h >> 16, ((state->src_h & 0xffff) * 15625) >> 10,
945 state->src_x >> 16, ((state->src_x & 0xffff) * 15625) >> 10,
946 state->src_y >> 16, ((state->src_y & 0xffff) * 15625) >> 10);
950 if (plane_switching_crtc(state->state, plane, state)) {
951 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] switching CRTC directly\n",
952 plane->base.id, plane->name);
959 static void drm_atomic_plane_print_state(struct drm_printer *p,
960 const struct drm_plane_state *state)
962 struct drm_plane *plane = state->plane;
963 struct drm_rect src = drm_plane_state_src(state);
964 struct drm_rect dest = drm_plane_state_dest(state);
966 drm_printf(p, "plane[%u]: %s\n", plane->base.id, plane->name);
967 drm_printf(p, "\tcrtc=%s\n", state->crtc ? state->crtc->name : "(null)");
968 drm_printf(p, "\tfb=%u\n", state->fb ? state->fb->base.id : 0);
970 struct drm_framebuffer *fb = state->fb;
971 int i, n = fb->format->num_planes;
972 struct drm_format_name_buf format_name;
974 drm_printf(p, "\t\tformat=%s\n",
975 drm_get_format_name(fb->format->format, &format_name));
976 drm_printf(p, "\t\t\tmodifier=0x%llx\n", fb->modifier);
977 drm_printf(p, "\t\tsize=%dx%d\n", fb->width, fb->height);
978 drm_printf(p, "\t\tlayers:\n");
979 for (i = 0; i < n; i++) {
980 drm_printf(p, "\t\t\tpitch[%d]=%u\n", i, fb->pitches[i]);
981 drm_printf(p, "\t\t\toffset[%d]=%u\n", i, fb->offsets[i]);
984 drm_printf(p, "\tcrtc-pos=" DRM_RECT_FMT "\n", DRM_RECT_ARG(&dest));
985 drm_printf(p, "\tsrc-pos=" DRM_RECT_FP_FMT "\n", DRM_RECT_FP_ARG(&src));
986 drm_printf(p, "\trotation=%x\n", state->rotation);
988 if (plane->funcs->atomic_print_state)
989 plane->funcs->atomic_print_state(p, state);
993 * drm_atomic_get_private_obj_state - get private object state
994 * @state: global atomic state
995 * @obj: private object to get the state for
996 * @funcs: pointer to the struct of function pointers that identify the object
999 * This function returns the private object state for the given private object,
1000 * allocating the state if needed. It does not grab any locks as the caller is
1001 * expected to care of any required locking.
1005 * Either the allocated state or the error code encoded into a pointer.
1008 drm_atomic_get_private_obj_state(struct drm_atomic_state *state, void *obj,
1009 const struct drm_private_state_funcs *funcs)
1011 int index, num_objs, i;
1013 struct __drm_private_objs_state *arr;
1015 for (i = 0; i < state->num_private_objs; i++)
1016 if (obj == state->private_objs[i].obj &&
1017 state->private_objs[i].obj_state)
1018 return state->private_objs[i].obj_state;
1020 num_objs = state->num_private_objs + 1;
1021 size = sizeof(*state->private_objs) * num_objs;
1022 arr = krealloc(state->private_objs, size, GFP_KERNEL);
1024 return ERR_PTR(-ENOMEM);
1026 state->private_objs = arr;
1027 index = state->num_private_objs;
1028 memset(&state->private_objs[index], 0, sizeof(*state->private_objs));
1030 state->private_objs[index].obj_state = funcs->duplicate_state(state, obj);
1031 if (!state->private_objs[index].obj_state)
1032 return ERR_PTR(-ENOMEM);
1034 state->private_objs[index].obj = obj;
1035 state->private_objs[index].funcs = funcs;
1036 state->num_private_objs = num_objs;
1038 DRM_DEBUG_ATOMIC("Added new private object state %p to %p\n",
1039 state->private_objs[index].obj_state, state);
1041 return state->private_objs[index].obj_state;
1043 EXPORT_SYMBOL(drm_atomic_get_private_obj_state);
1046 * drm_atomic_get_connector_state - get connector state
1047 * @state: global atomic state object
1048 * @connector: connector to get state object for
1050 * This function returns the connector state for the given connector,
1051 * allocating it if needed. It will also grab the relevant connector lock to
1052 * make sure that the state is consistent.
1056 * Either the allocated state or the error code encoded into the pointer. When
1057 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
1058 * entire atomic sequence must be restarted. All other errors are fatal.
1060 struct drm_connector_state *
1061 drm_atomic_get_connector_state(struct drm_atomic_state *state,
1062 struct drm_connector *connector)
1065 struct drm_mode_config *config = &connector->dev->mode_config;
1066 struct drm_connector_state *connector_state;
1068 WARN_ON(!state->acquire_ctx);
1070 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
1072 return ERR_PTR(ret);
1074 index = drm_connector_index(connector);
1076 if (index >= state->num_connector) {
1077 struct __drm_connnectors_state *c;
1078 int alloc = max(index + 1, config->num_connector);
1080 c = krealloc(state->connectors, alloc * sizeof(*state->connectors), GFP_KERNEL);
1082 return ERR_PTR(-ENOMEM);
1084 state->connectors = c;
1085 memset(&state->connectors[state->num_connector], 0,
1086 sizeof(*state->connectors) * (alloc - state->num_connector));
1088 state->num_connector = alloc;
1091 if (state->connectors[index].state)
1092 return state->connectors[index].state;
1094 connector_state = connector->funcs->atomic_duplicate_state(connector);
1095 if (!connector_state)
1096 return ERR_PTR(-ENOMEM);
1098 drm_connector_get(connector);
1099 state->connectors[index].state = connector_state;
1100 state->connectors[index].old_state = connector->state;
1101 state->connectors[index].new_state = connector_state;
1102 state->connectors[index].ptr = connector;
1103 connector_state->state = state;
1105 DRM_DEBUG_ATOMIC("Added [CONNECTOR:%d:%s] %p state to %p\n",
1106 connector->base.id, connector->name,
1107 connector_state, state);
1109 if (connector_state->crtc) {
1110 struct drm_crtc_state *crtc_state;
1112 crtc_state = drm_atomic_get_crtc_state(state,
1113 connector_state->crtc);
1114 if (IS_ERR(crtc_state))
1115 return ERR_CAST(crtc_state);
1118 return connector_state;
1120 EXPORT_SYMBOL(drm_atomic_get_connector_state);
1123 * drm_atomic_connector_set_property - set property on connector.
1124 * @connector: the drm connector to set a property on
1125 * @state: the state object to update with the new property value
1126 * @property: the property to set
1127 * @val: the new property value
1129 * This function handles generic/core properties and calls out to driver's
1130 * &drm_connector_funcs.atomic_set_property for driver properties. To ensure
1131 * consistent behavior you must call this function rather than the driver hook
1135 * Zero on success, error code on failure
1137 int drm_atomic_connector_set_property(struct drm_connector *connector,
1138 struct drm_connector_state *state, struct drm_property *property,
1141 struct drm_device *dev = connector->dev;
1142 struct drm_mode_config *config = &dev->mode_config;
1144 if (property == config->prop_crtc_id) {
1145 struct drm_crtc *crtc = drm_crtc_find(dev, val);
1146 return drm_atomic_set_crtc_for_connector(state, crtc);
1147 } else if (property == config->dpms_property) {
1148 /* setting DPMS property requires special handling, which
1149 * is done in legacy setprop path for us. Disallow (for
1150 * now?) atomic writes to DPMS property:
1153 } else if (property == config->tv_select_subconnector_property) {
1154 state->tv.subconnector = val;
1155 } else if (property == config->tv_left_margin_property) {
1156 state->tv.margins.left = val;
1157 } else if (property == config->tv_right_margin_property) {
1158 state->tv.margins.right = val;
1159 } else if (property == config->tv_top_margin_property) {
1160 state->tv.margins.top = val;
1161 } else if (property == config->tv_bottom_margin_property) {
1162 state->tv.margins.bottom = val;
1163 } else if (property == config->tv_mode_property) {
1164 state->tv.mode = val;
1165 } else if (property == config->tv_brightness_property) {
1166 state->tv.brightness = val;
1167 } else if (property == config->tv_contrast_property) {
1168 state->tv.contrast = val;
1169 } else if (property == config->tv_flicker_reduction_property) {
1170 state->tv.flicker_reduction = val;
1171 } else if (property == config->tv_overscan_property) {
1172 state->tv.overscan = val;
1173 } else if (property == config->tv_saturation_property) {
1174 state->tv.saturation = val;
1175 } else if (property == config->tv_hue_property) {
1176 state->tv.hue = val;
1177 } else if (property == config->link_status_property) {
1178 /* Never downgrade from GOOD to BAD on userspace's request here,
1179 * only hw issues can do that.
1181 * For an atomic property the userspace doesn't need to be able
1182 * to understand all the properties, but needs to be able to
1183 * restore the state it wants on VT switch. So if the userspace
1184 * tries to change the link_status from GOOD to BAD, driver
1185 * silently rejects it and returns a 0. This prevents userspace
1186 * from accidently breaking the display when it restores the
1189 if (state->link_status != DRM_LINK_STATUS_GOOD)
1190 state->link_status = val;
1191 } else if (property == config->aspect_ratio_property) {
1192 state->picture_aspect_ratio = val;
1193 } else if (property == connector->scaling_mode_property) {
1194 state->scaling_mode = val;
1195 } else if (connector->funcs->atomic_set_property) {
1196 return connector->funcs->atomic_set_property(connector,
1197 state, property, val);
1204 EXPORT_SYMBOL(drm_atomic_connector_set_property);
1206 static void drm_atomic_connector_print_state(struct drm_printer *p,
1207 const struct drm_connector_state *state)
1209 struct drm_connector *connector = state->connector;
1211 drm_printf(p, "connector[%u]: %s\n", connector->base.id, connector->name);
1212 drm_printf(p, "\tcrtc=%s\n", state->crtc ? state->crtc->name : "(null)");
1214 if (connector->funcs->atomic_print_state)
1215 connector->funcs->atomic_print_state(p, state);
1219 * drm_atomic_connector_get_property - get property value from connector state
1220 * @connector: the drm connector to set a property on
1221 * @state: the state object to get the property value from
1222 * @property: the property to set
1223 * @val: return location for the property value
1225 * This function handles generic/core properties and calls out to driver's
1226 * &drm_connector_funcs.atomic_get_property for driver properties. To ensure
1227 * consistent behavior you must call this function rather than the driver hook
1231 * Zero on success, error code on failure
1234 drm_atomic_connector_get_property(struct drm_connector *connector,
1235 const struct drm_connector_state *state,
1236 struct drm_property *property, uint64_t *val)
1238 struct drm_device *dev = connector->dev;
1239 struct drm_mode_config *config = &dev->mode_config;
1241 if (property == config->prop_crtc_id) {
1242 *val = (state->crtc) ? state->crtc->base.id : 0;
1243 } else if (property == config->dpms_property) {
1244 *val = connector->dpms;
1245 } else if (property == config->tv_select_subconnector_property) {
1246 *val = state->tv.subconnector;
1247 } else if (property == config->tv_left_margin_property) {
1248 *val = state->tv.margins.left;
1249 } else if (property == config->tv_right_margin_property) {
1250 *val = state->tv.margins.right;
1251 } else if (property == config->tv_top_margin_property) {
1252 *val = state->tv.margins.top;
1253 } else if (property == config->tv_bottom_margin_property) {
1254 *val = state->tv.margins.bottom;
1255 } else if (property == config->tv_mode_property) {
1256 *val = state->tv.mode;
1257 } else if (property == config->tv_brightness_property) {
1258 *val = state->tv.brightness;
1259 } else if (property == config->tv_contrast_property) {
1260 *val = state->tv.contrast;
1261 } else if (property == config->tv_flicker_reduction_property) {
1262 *val = state->tv.flicker_reduction;
1263 } else if (property == config->tv_overscan_property) {
1264 *val = state->tv.overscan;
1265 } else if (property == config->tv_saturation_property) {
1266 *val = state->tv.saturation;
1267 } else if (property == config->tv_hue_property) {
1268 *val = state->tv.hue;
1269 } else if (property == config->link_status_property) {
1270 *val = state->link_status;
1271 } else if (property == config->aspect_ratio_property) {
1272 *val = state->picture_aspect_ratio;
1273 } else if (property == connector->scaling_mode_property) {
1274 *val = state->scaling_mode;
1275 } else if (connector->funcs->atomic_get_property) {
1276 return connector->funcs->atomic_get_property(connector,
1277 state, property, val);
1285 int drm_atomic_get_property(struct drm_mode_object *obj,
1286 struct drm_property *property, uint64_t *val)
1288 struct drm_device *dev = property->dev;
1291 switch (obj->type) {
1292 case DRM_MODE_OBJECT_CONNECTOR: {
1293 struct drm_connector *connector = obj_to_connector(obj);
1294 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
1295 ret = drm_atomic_connector_get_property(connector,
1296 connector->state, property, val);
1299 case DRM_MODE_OBJECT_CRTC: {
1300 struct drm_crtc *crtc = obj_to_crtc(obj);
1301 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
1302 ret = drm_atomic_crtc_get_property(crtc,
1303 crtc->state, property, val);
1306 case DRM_MODE_OBJECT_PLANE: {
1307 struct drm_plane *plane = obj_to_plane(obj);
1308 WARN_ON(!drm_modeset_is_locked(&plane->mutex));
1309 ret = drm_atomic_plane_get_property(plane,
1310 plane->state, property, val);
1322 * drm_atomic_set_crtc_for_plane - set crtc for plane
1323 * @plane_state: the plane whose incoming state to update
1324 * @crtc: crtc to use for the plane
1326 * Changing the assigned crtc for a plane requires us to grab the lock and state
1327 * for the new crtc, as needed. This function takes care of all these details
1328 * besides updating the pointer in the state object itself.
1331 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1332 * then the w/w mutex code has detected a deadlock and the entire atomic
1333 * sequence must be restarted. All other errors are fatal.
1336 drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
1337 struct drm_crtc *crtc)
1339 struct drm_plane *plane = plane_state->plane;
1340 struct drm_crtc_state *crtc_state;
1342 if (plane_state->crtc) {
1343 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
1345 if (WARN_ON(IS_ERR(crtc_state)))
1346 return PTR_ERR(crtc_state);
1348 crtc_state->plane_mask &= ~(1 << drm_plane_index(plane));
1351 plane_state->crtc = crtc;
1354 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
1356 if (IS_ERR(crtc_state))
1357 return PTR_ERR(crtc_state);
1358 crtc_state->plane_mask |= (1 << drm_plane_index(plane));
1362 DRM_DEBUG_ATOMIC("Link plane state %p to [CRTC:%d:%s]\n",
1363 plane_state, crtc->base.id, crtc->name);
1365 DRM_DEBUG_ATOMIC("Link plane state %p to [NOCRTC]\n",
1370 EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane);
1373 * drm_atomic_set_fb_for_plane - set framebuffer for plane
1374 * @plane_state: atomic state object for the plane
1375 * @fb: fb to use for the plane
1377 * Changing the assigned framebuffer for a plane requires us to grab a reference
1378 * to the new fb and drop the reference to the old fb, if there is one. This
1379 * function takes care of all these details besides updating the pointer in the
1380 * state object itself.
1383 drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
1384 struct drm_framebuffer *fb)
1387 DRM_DEBUG_ATOMIC("Set [FB:%d] for plane state %p\n",
1388 fb->base.id, plane_state);
1390 DRM_DEBUG_ATOMIC("Set [NOFB] for plane state %p\n",
1393 drm_framebuffer_assign(&plane_state->fb, fb);
1395 EXPORT_SYMBOL(drm_atomic_set_fb_for_plane);
1398 * drm_atomic_set_fence_for_plane - set fence for plane
1399 * @plane_state: atomic state object for the plane
1400 * @fence: dma_fence to use for the plane
1402 * Helper to setup the plane_state fence in case it is not set yet.
1403 * By using this drivers doesn't need to worry if the user choose
1404 * implicit or explicit fencing.
1406 * This function will not set the fence to the state if it was set
1407 * via explicit fencing interfaces on the atomic ioctl. In that case it will
1408 * drop the reference to the fence as we are not storing it anywhere.
1409 * Otherwise, if &drm_plane_state.fence is not set this function we just set it
1410 * with the received implicit fence. In both cases this function consumes a
1411 * reference for @fence.
1414 drm_atomic_set_fence_for_plane(struct drm_plane_state *plane_state,
1415 struct dma_fence *fence)
1417 if (plane_state->fence) {
1418 dma_fence_put(fence);
1422 plane_state->fence = fence;
1424 EXPORT_SYMBOL(drm_atomic_set_fence_for_plane);
1427 * drm_atomic_set_crtc_for_connector - set crtc for connector
1428 * @conn_state: atomic state object for the connector
1429 * @crtc: crtc to use for the connector
1431 * Changing the assigned crtc for a connector requires us to grab the lock and
1432 * state for the new crtc, as needed. This function takes care of all these
1433 * details besides updating the pointer in the state object itself.
1436 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1437 * then the w/w mutex code has detected a deadlock and the entire atomic
1438 * sequence must be restarted. All other errors are fatal.
1441 drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
1442 struct drm_crtc *crtc)
1444 struct drm_crtc_state *crtc_state;
1446 if (conn_state->crtc == crtc)
1449 if (conn_state->crtc) {
1450 crtc_state = drm_atomic_get_new_crtc_state(conn_state->state,
1453 crtc_state->connector_mask &=
1454 ~(1 << drm_connector_index(conn_state->connector));
1456 drm_connector_put(conn_state->connector);
1457 conn_state->crtc = NULL;
1461 crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc);
1462 if (IS_ERR(crtc_state))
1463 return PTR_ERR(crtc_state);
1465 crtc_state->connector_mask |=
1466 1 << drm_connector_index(conn_state->connector);
1468 drm_connector_get(conn_state->connector);
1469 conn_state->crtc = crtc;
1471 DRM_DEBUG_ATOMIC("Link connector state %p to [CRTC:%d:%s]\n",
1472 conn_state, crtc->base.id, crtc->name);
1474 DRM_DEBUG_ATOMIC("Link connector state %p to [NOCRTC]\n",
1480 EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector);
1483 * drm_atomic_add_affected_connectors - add connectors for crtc
1484 * @state: atomic state
1487 * This function walks the current configuration and adds all connectors
1488 * currently using @crtc to the atomic configuration @state. Note that this
1489 * function must acquire the connection mutex. This can potentially cause
1490 * unneeded seralization if the update is just for the planes on one crtc. Hence
1491 * drivers and helpers should only call this when really needed (e.g. when a
1492 * full modeset needs to happen due to some change).
1495 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1496 * then the w/w mutex code has detected a deadlock and the entire atomic
1497 * sequence must be restarted. All other errors are fatal.
1500 drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
1501 struct drm_crtc *crtc)
1503 struct drm_mode_config *config = &state->dev->mode_config;
1504 struct drm_connector *connector;
1505 struct drm_connector_state *conn_state;
1506 struct drm_connector_list_iter conn_iter;
1507 struct drm_crtc_state *crtc_state;
1510 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1511 if (IS_ERR(crtc_state))
1512 return PTR_ERR(crtc_state);
1514 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
1518 DRM_DEBUG_ATOMIC("Adding all current connectors for [CRTC:%d:%s] to %p\n",
1519 crtc->base.id, crtc->name, state);
1522 * Changed connectors are already in @state, so only need to look
1523 * at the connector_mask in crtc_state.
1525 drm_connector_list_iter_begin(state->dev, &conn_iter);
1526 drm_for_each_connector_iter(connector, &conn_iter) {
1527 if (!(crtc_state->connector_mask & (1 << drm_connector_index(connector))))
1530 conn_state = drm_atomic_get_connector_state(state, connector);
1531 if (IS_ERR(conn_state)) {
1532 drm_connector_list_iter_end(&conn_iter);
1533 return PTR_ERR(conn_state);
1536 drm_connector_list_iter_end(&conn_iter);
1540 EXPORT_SYMBOL(drm_atomic_add_affected_connectors);
1543 * drm_atomic_add_affected_planes - add planes for crtc
1544 * @state: atomic state
1547 * This function walks the current configuration and adds all planes
1548 * currently used by @crtc to the atomic configuration @state. This is useful
1549 * when an atomic commit also needs to check all currently enabled plane on
1550 * @crtc, e.g. when changing the mode. It's also useful when re-enabling a CRTC
1551 * to avoid special code to force-enable all planes.
1553 * Since acquiring a plane state will always also acquire the w/w mutex of the
1554 * current CRTC for that plane (if there is any) adding all the plane states for
1555 * a CRTC will not reduce parallism of atomic updates.
1558 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1559 * then the w/w mutex code has detected a deadlock and the entire atomic
1560 * sequence must be restarted. All other errors are fatal.
1563 drm_atomic_add_affected_planes(struct drm_atomic_state *state,
1564 struct drm_crtc *crtc)
1566 struct drm_plane *plane;
1568 WARN_ON(!drm_atomic_get_new_crtc_state(state, crtc));
1570 drm_for_each_plane_mask(plane, state->dev, crtc->state->plane_mask) {
1571 struct drm_plane_state *plane_state =
1572 drm_atomic_get_plane_state(state, plane);
1574 if (IS_ERR(plane_state))
1575 return PTR_ERR(plane_state);
1579 EXPORT_SYMBOL(drm_atomic_add_affected_planes);
1582 * drm_atomic_legacy_backoff - locking backoff for legacy ioctls
1583 * @state: atomic state
1585 * This function should be used by legacy entry points which don't understand
1586 * -EDEADLK semantics. For simplicity this one will grab all modeset locks after
1587 * the slowpath completed.
1589 void drm_atomic_legacy_backoff(struct drm_atomic_state *state)
1591 struct drm_device *dev = state->dev;
1593 bool global = false;
1595 if (WARN_ON(dev->mode_config.acquire_ctx == state->acquire_ctx)) {
1598 dev->mode_config.acquire_ctx = NULL;
1602 drm_modeset_backoff(state->acquire_ctx);
1604 ret = drm_modeset_lock_all_ctx(dev, state->acquire_ctx);
1609 dev->mode_config.acquire_ctx = state->acquire_ctx;
1611 EXPORT_SYMBOL(drm_atomic_legacy_backoff);
1614 * drm_atomic_check_only - check whether a given config would work
1615 * @state: atomic configuration to check
1617 * Note that this function can return -EDEADLK if the driver needed to acquire
1618 * more locks but encountered a deadlock. The caller must then do the usual w/w
1619 * backoff dance and restart. All other errors are fatal.
1622 * 0 on success, negative error code on failure.
1624 int drm_atomic_check_only(struct drm_atomic_state *state)
1626 struct drm_device *dev = state->dev;
1627 struct drm_mode_config *config = &dev->mode_config;
1628 struct drm_plane *plane;
1629 struct drm_plane_state *plane_state;
1630 struct drm_crtc *crtc;
1631 struct drm_crtc_state *crtc_state;
1634 DRM_DEBUG_ATOMIC("checking %p\n", state);
1636 for_each_new_plane_in_state(state, plane, plane_state, i) {
1637 ret = drm_atomic_plane_check(plane, plane_state);
1639 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic core check failed\n",
1640 plane->base.id, plane->name);
1645 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
1646 ret = drm_atomic_crtc_check(crtc, crtc_state);
1648 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic core check failed\n",
1649 crtc->base.id, crtc->name);
1654 if (config->funcs->atomic_check)
1655 ret = config->funcs->atomic_check(state->dev, state);
1657 if (!state->allow_modeset) {
1658 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
1659 if (drm_atomic_crtc_needs_modeset(crtc_state)) {
1660 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] requires full modeset\n",
1661 crtc->base.id, crtc->name);
1669 EXPORT_SYMBOL(drm_atomic_check_only);
1672 * drm_atomic_commit - commit configuration atomically
1673 * @state: atomic configuration to check
1675 * Note that this function can return -EDEADLK if the driver needed to acquire
1676 * more locks but encountered a deadlock. The caller must then do the usual w/w
1677 * backoff dance and restart. All other errors are fatal.
1679 * This function will take its own reference on @state.
1680 * Callers should always release their reference with drm_atomic_state_put().
1683 * 0 on success, negative error code on failure.
1685 int drm_atomic_commit(struct drm_atomic_state *state)
1687 struct drm_mode_config *config = &state->dev->mode_config;
1690 ret = drm_atomic_check_only(state);
1694 DRM_DEBUG_ATOMIC("committing %p\n", state);
1696 return config->funcs->atomic_commit(state->dev, state, false);
1698 EXPORT_SYMBOL(drm_atomic_commit);
1701 * drm_atomic_nonblocking_commit - atomic nonblocking commit
1702 * @state: atomic configuration to check
1704 * Note that this function can return -EDEADLK if the driver needed to acquire
1705 * more locks but encountered a deadlock. The caller must then do the usual w/w
1706 * backoff dance and restart. All other errors are fatal.
1708 * This function will take its own reference on @state.
1709 * Callers should always release their reference with drm_atomic_state_put().
1712 * 0 on success, negative error code on failure.
1714 int drm_atomic_nonblocking_commit(struct drm_atomic_state *state)
1716 struct drm_mode_config *config = &state->dev->mode_config;
1719 ret = drm_atomic_check_only(state);
1723 DRM_DEBUG_ATOMIC("committing %p nonblocking\n", state);
1725 return config->funcs->atomic_commit(state->dev, state, true);
1727 EXPORT_SYMBOL(drm_atomic_nonblocking_commit);
1729 static void drm_atomic_print_state(const struct drm_atomic_state *state)
1731 struct drm_printer p = drm_info_printer(state->dev->dev);
1732 struct drm_plane *plane;
1733 struct drm_plane_state *plane_state;
1734 struct drm_crtc *crtc;
1735 struct drm_crtc_state *crtc_state;
1736 struct drm_connector *connector;
1737 struct drm_connector_state *connector_state;
1740 DRM_DEBUG_ATOMIC("checking %p\n", state);
1742 for_each_new_plane_in_state(state, plane, plane_state, i)
1743 drm_atomic_plane_print_state(&p, plane_state);
1745 for_each_new_crtc_in_state(state, crtc, crtc_state, i)
1746 drm_atomic_crtc_print_state(&p, crtc_state);
1748 for_each_new_connector_in_state(state, connector, connector_state, i)
1749 drm_atomic_connector_print_state(&p, connector_state);
1752 static void __drm_state_dump(struct drm_device *dev, struct drm_printer *p,
1755 struct drm_mode_config *config = &dev->mode_config;
1756 struct drm_plane *plane;
1757 struct drm_crtc *crtc;
1758 struct drm_connector *connector;
1759 struct drm_connector_list_iter conn_iter;
1761 if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
1764 list_for_each_entry(plane, &config->plane_list, head) {
1766 drm_modeset_lock(&plane->mutex, NULL);
1767 drm_atomic_plane_print_state(p, plane->state);
1769 drm_modeset_unlock(&plane->mutex);
1772 list_for_each_entry(crtc, &config->crtc_list, head) {
1774 drm_modeset_lock(&crtc->mutex, NULL);
1775 drm_atomic_crtc_print_state(p, crtc->state);
1777 drm_modeset_unlock(&crtc->mutex);
1780 drm_connector_list_iter_begin(dev, &conn_iter);
1782 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
1783 drm_for_each_connector_iter(connector, &conn_iter)
1784 drm_atomic_connector_print_state(p, connector->state);
1786 drm_modeset_unlock(&dev->mode_config.connection_mutex);
1787 drm_connector_list_iter_end(&conn_iter);
1791 * drm_state_dump - dump entire device atomic state
1792 * @dev: the drm device
1793 * @p: where to print the state to
1795 * Just for debugging. Drivers might want an option to dump state
1796 * to dmesg in case of error irq's. (Hint, you probably want to
1799 * The caller must drm_modeset_lock_all(), or if this is called
1800 * from error irq handler, it should not be enabled by default.
1801 * (Ie. if you are debugging errors you might not care that this
1802 * is racey. But calling this without all modeset locks held is
1803 * not inherently safe.)
1805 void drm_state_dump(struct drm_device *dev, struct drm_printer *p)
1807 __drm_state_dump(dev, p, false);
1809 EXPORT_SYMBOL(drm_state_dump);
1811 #ifdef CONFIG_DEBUG_FS
1812 static int drm_state_info(struct seq_file *m, void *data)
1814 struct drm_info_node *node = (struct drm_info_node *) m->private;
1815 struct drm_device *dev = node->minor->dev;
1816 struct drm_printer p = drm_seq_file_printer(m);
1818 __drm_state_dump(dev, &p, true);
1823 /* any use in debugfs files to dump individual planes/crtc/etc? */
1824 static const struct drm_info_list drm_atomic_debugfs_list[] = {
1825 {"state", drm_state_info, 0},
1828 int drm_atomic_debugfs_init(struct drm_minor *minor)
1830 return drm_debugfs_create_files(drm_atomic_debugfs_list,
1831 ARRAY_SIZE(drm_atomic_debugfs_list),
1832 minor->debugfs_root, minor);
1837 * The big monstor ioctl
1840 static struct drm_pending_vblank_event *create_vblank_event(
1841 struct drm_device *dev, uint64_t user_data)
1843 struct drm_pending_vblank_event *e = NULL;
1845 e = kzalloc(sizeof *e, GFP_KERNEL);
1849 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
1850 e->event.base.length = sizeof(e->event);
1851 e->event.user_data = user_data;
1856 static int atomic_set_prop(struct drm_atomic_state *state,
1857 struct drm_mode_object *obj, struct drm_property *prop,
1858 uint64_t prop_value)
1860 struct drm_mode_object *ref;
1863 if (!drm_property_change_valid_get(prop, prop_value, &ref))
1866 switch (obj->type) {
1867 case DRM_MODE_OBJECT_CONNECTOR: {
1868 struct drm_connector *connector = obj_to_connector(obj);
1869 struct drm_connector_state *connector_state;
1871 connector_state = drm_atomic_get_connector_state(state, connector);
1872 if (IS_ERR(connector_state)) {
1873 ret = PTR_ERR(connector_state);
1877 ret = drm_atomic_connector_set_property(connector,
1878 connector_state, prop, prop_value);
1881 case DRM_MODE_OBJECT_CRTC: {
1882 struct drm_crtc *crtc = obj_to_crtc(obj);
1883 struct drm_crtc_state *crtc_state;
1885 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1886 if (IS_ERR(crtc_state)) {
1887 ret = PTR_ERR(crtc_state);
1891 ret = drm_atomic_crtc_set_property(crtc,
1892 crtc_state, prop, prop_value);
1895 case DRM_MODE_OBJECT_PLANE: {
1896 struct drm_plane *plane = obj_to_plane(obj);
1897 struct drm_plane_state *plane_state;
1899 plane_state = drm_atomic_get_plane_state(state, plane);
1900 if (IS_ERR(plane_state)) {
1901 ret = PTR_ERR(plane_state);
1905 ret = drm_atomic_plane_set_property(plane,
1906 plane_state, prop, prop_value);
1914 drm_property_change_valid_put(prop, ref);
1919 * drm_atomic_clean_old_fb -- Unset old_fb pointers and set plane->fb pointers.
1921 * @dev: drm device to check.
1922 * @plane_mask: plane mask for planes that were updated.
1923 * @ret: return value, can be -EDEADLK for a retry.
1925 * Before doing an update &drm_plane.old_fb is set to &drm_plane.fb, but before
1926 * dropping the locks old_fb needs to be set to NULL and plane->fb updated. This
1927 * is a common operation for each atomic update, so this call is split off as a
1930 void drm_atomic_clean_old_fb(struct drm_device *dev,
1931 unsigned plane_mask,
1934 struct drm_plane *plane;
1936 /* if succeeded, fixup legacy plane crtc/fb ptrs before dropping
1937 * locks (ie. while it is still safe to deref plane->state). We
1938 * need to do this here because the driver entry points cannot
1939 * distinguish between legacy and atomic ioctls.
1941 drm_for_each_plane_mask(plane, dev, plane_mask) {
1943 struct drm_framebuffer *new_fb = plane->state->fb;
1945 drm_framebuffer_get(new_fb);
1947 plane->crtc = plane->state->crtc;
1950 drm_framebuffer_put(plane->old_fb);
1952 plane->old_fb = NULL;
1955 EXPORT_SYMBOL(drm_atomic_clean_old_fb);
1958 * DOC: explicit fencing properties
1960 * Explicit fencing allows userspace to control the buffer synchronization
1961 * between devices. A Fence or a group of fences are transfered to/from
1962 * userspace using Sync File fds and there are two DRM properties for that.
1963 * IN_FENCE_FD on each DRM Plane to send fences to the kernel and
1964 * OUT_FENCE_PTR on each DRM CRTC to receive fences from the kernel.
1966 * As a contrast, with implicit fencing the kernel keeps track of any
1967 * ongoing rendering, and automatically ensures that the atomic update waits
1968 * for any pending rendering to complete. For shared buffers represented with
1969 * a &struct dma_buf this is tracked in &struct reservation_object.
1970 * Implicit syncing is how Linux traditionally worked (e.g. DRI2/3 on X.org),
1971 * whereas explicit fencing is what Android wants.
1974 * Use this property to pass a fence that DRM should wait on before
1975 * proceeding with the Atomic Commit request and show the framebuffer for
1976 * the plane on the screen. The fence can be either a normal fence or a
1977 * merged one, the sync_file framework will handle both cases and use a
1978 * fence_array if a merged fence is received. Passing -1 here means no
1979 * fences to wait on.
1981 * If the Atomic Commit request has the DRM_MODE_ATOMIC_TEST_ONLY flag
1982 * it will only check if the Sync File is a valid one.
1984 * On the driver side the fence is stored on the @fence parameter of
1985 * &struct drm_plane_state. Drivers which also support implicit fencing
1986 * should set the implicit fence using drm_atomic_set_fence_for_plane(),
1987 * to make sure there's consistent behaviour between drivers in precedence
1988 * of implicit vs. explicit fencing.
1991 * Use this property to pass a file descriptor pointer to DRM. Once the
1992 * Atomic Commit request call returns OUT_FENCE_PTR will be filled with
1993 * the file descriptor number of a Sync File. This Sync File contains the
1994 * CRTC fence that will be signaled when all framebuffers present on the
1995 * Atomic Commit * request for that given CRTC are scanned out on the
1998 * The Atomic Commit request fails if a invalid pointer is passed. If the
1999 * Atomic Commit request fails for any other reason the out fence fd
2000 * returned will be -1. On a Atomic Commit with the
2001 * DRM_MODE_ATOMIC_TEST_ONLY flag the out fence will also be set to -1.
2003 * Note that out-fences don't have a special interface to drivers and are
2004 * internally represented by a &struct drm_pending_vblank_event in struct
2005 * &drm_crtc_state, which is also used by the nonblocking atomic commit
2006 * helpers and for the DRM event handling for existing userspace.
2009 struct drm_out_fence_state {
2010 s32 __user *out_fence_ptr;
2011 struct sync_file *sync_file;
2015 static int setup_out_fence(struct drm_out_fence_state *fence_state,
2016 struct dma_fence *fence)
2018 fence_state->fd = get_unused_fd_flags(O_CLOEXEC);
2019 if (fence_state->fd < 0)
2020 return fence_state->fd;
2022 if (put_user(fence_state->fd, fence_state->out_fence_ptr))
2025 fence_state->sync_file = sync_file_create(fence);
2026 if (!fence_state->sync_file)
2032 static int prepare_crtc_signaling(struct drm_device *dev,
2033 struct drm_atomic_state *state,
2034 struct drm_mode_atomic *arg,
2035 struct drm_file *file_priv,
2036 struct drm_out_fence_state **fence_state,
2037 unsigned int *num_fences)
2039 struct drm_crtc *crtc;
2040 struct drm_crtc_state *crtc_state;
2043 if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)
2046 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
2047 s32 __user *fence_ptr;
2049 fence_ptr = get_out_fence_for_crtc(crtc_state->state, crtc);
2051 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT || fence_ptr) {
2052 struct drm_pending_vblank_event *e;
2054 e = create_vblank_event(dev, arg->user_data);
2058 crtc_state->event = e;
2061 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
2062 struct drm_pending_vblank_event *e = crtc_state->event;
2067 ret = drm_event_reserve_init(dev, file_priv, &e->base,
2071 crtc_state->event = NULL;
2077 struct dma_fence *fence;
2078 struct drm_out_fence_state *f;
2080 f = krealloc(*fence_state, sizeof(**fence_state) *
2081 (*num_fences + 1), GFP_KERNEL);
2085 memset(&f[*num_fences], 0, sizeof(*f));
2087 f[*num_fences].out_fence_ptr = fence_ptr;
2090 fence = drm_crtc_create_fence(crtc);
2094 ret = setup_out_fence(&f[(*num_fences)++], fence);
2096 dma_fence_put(fence);
2100 crtc_state->event->base.fence = fence;
2107 static void complete_crtc_signaling(struct drm_device *dev,
2108 struct drm_atomic_state *state,
2109 struct drm_out_fence_state *fence_state,
2110 unsigned int num_fences,
2113 struct drm_crtc *crtc;
2114 struct drm_crtc_state *crtc_state;
2118 for (i = 0; i < num_fences; i++)
2119 fd_install(fence_state[i].fd,
2120 fence_state[i].sync_file->file);
2126 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
2127 struct drm_pending_vblank_event *event = crtc_state->event;
2129 * Free the allocated event. drm_atomic_helper_setup_commit
2130 * can allocate an event too, so only free it if it's ours
2131 * to prevent a double free in drm_atomic_state_clear.
2133 if (event && (event->base.fence || event->base.file_priv)) {
2134 drm_event_cancel_free(dev, &event->base);
2135 crtc_state->event = NULL;
2142 for (i = 0; i < num_fences; i++) {
2143 if (fence_state[i].sync_file)
2144 fput(fence_state[i].sync_file->file);
2145 if (fence_state[i].fd >= 0)
2146 put_unused_fd(fence_state[i].fd);
2148 /* If this fails log error to the user */
2149 if (fence_state[i].out_fence_ptr &&
2150 put_user(-1, fence_state[i].out_fence_ptr))
2151 DRM_DEBUG_ATOMIC("Couldn't clear out_fence_ptr\n");
2157 int drm_mode_atomic_ioctl(struct drm_device *dev,
2158 void *data, struct drm_file *file_priv)
2160 struct drm_mode_atomic *arg = data;
2161 uint32_t __user *objs_ptr = (uint32_t __user *)(unsigned long)(arg->objs_ptr);
2162 uint32_t __user *count_props_ptr = (uint32_t __user *)(unsigned long)(arg->count_props_ptr);
2163 uint32_t __user *props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
2164 uint64_t __user *prop_values_ptr = (uint64_t __user *)(unsigned long)(arg->prop_values_ptr);
2165 unsigned int copied_objs, copied_props;
2166 struct drm_atomic_state *state;
2167 struct drm_modeset_acquire_ctx ctx;
2168 struct drm_plane *plane;
2169 struct drm_out_fence_state *fence_state = NULL;
2170 unsigned plane_mask;
2172 unsigned int i, j, num_fences = 0;
2174 /* disallow for drivers not supporting atomic: */
2175 if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
2178 /* disallow for userspace that has not enabled atomic cap (even
2179 * though this may be a bit overkill, since legacy userspace
2180 * wouldn't know how to call this ioctl)
2182 if (!file_priv->atomic)
2185 if (arg->flags & ~DRM_MODE_ATOMIC_FLAGS)
2191 if ((arg->flags & DRM_MODE_PAGE_FLIP_ASYNC) &&
2192 !dev->mode_config.async_page_flip)
2195 /* can't test and expect an event at the same time. */
2196 if ((arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) &&
2197 (arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
2200 drm_modeset_acquire_init(&ctx, 0);
2202 state = drm_atomic_state_alloc(dev);
2206 state->acquire_ctx = &ctx;
2207 state->allow_modeset = !!(arg->flags & DRM_MODE_ATOMIC_ALLOW_MODESET);
2214 for (i = 0; i < arg->count_objs; i++) {
2215 uint32_t obj_id, count_props;
2216 struct drm_mode_object *obj;
2218 if (get_user(obj_id, objs_ptr + copied_objs)) {
2223 obj = drm_mode_object_find(dev, obj_id, DRM_MODE_OBJECT_ANY);
2229 if (!obj->properties) {
2230 drm_mode_object_put(obj);
2235 if (get_user(count_props, count_props_ptr + copied_objs)) {
2236 drm_mode_object_put(obj);
2243 for (j = 0; j < count_props; j++) {
2245 uint64_t prop_value;
2246 struct drm_property *prop;
2248 if (get_user(prop_id, props_ptr + copied_props)) {
2249 drm_mode_object_put(obj);
2254 prop = drm_mode_obj_find_prop_id(obj, prop_id);
2256 drm_mode_object_put(obj);
2261 if (copy_from_user(&prop_value,
2262 prop_values_ptr + copied_props,
2263 sizeof(prop_value))) {
2264 drm_mode_object_put(obj);
2269 ret = atomic_set_prop(state, obj, prop, prop_value);
2271 drm_mode_object_put(obj);
2278 if (obj->type == DRM_MODE_OBJECT_PLANE && count_props &&
2279 !(arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)) {
2280 plane = obj_to_plane(obj);
2281 plane_mask |= (1 << drm_plane_index(plane));
2282 plane->old_fb = plane->fb;
2284 drm_mode_object_put(obj);
2287 ret = prepare_crtc_signaling(dev, state, arg, file_priv, &fence_state,
2292 if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) {
2293 ret = drm_atomic_check_only(state);
2294 } else if (arg->flags & DRM_MODE_ATOMIC_NONBLOCK) {
2295 ret = drm_atomic_nonblocking_commit(state);
2297 if (unlikely(drm_debug & DRM_UT_STATE))
2298 drm_atomic_print_state(state);
2300 ret = drm_atomic_commit(state);
2304 drm_atomic_clean_old_fb(dev, plane_mask, ret);
2306 complete_crtc_signaling(dev, state, fence_state, num_fences, !ret);
2308 if (ret == -EDEADLK) {
2309 drm_atomic_state_clear(state);
2310 drm_modeset_backoff(&ctx);
2314 drm_atomic_state_put(state);
2316 drm_modeset_drop_locks(&ctx);
2317 drm_modeset_acquire_fini(&ctx);