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.
30 #include <drm/drm_atomic.h>
31 #include <drm/drm_mode.h>
32 #include <drm/drm_print.h>
33 #include <linux/sync_file.h>
35 #include "drm_crtc_internal.h"
36 #include "drm_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;
113 if (!config->funcs->atomic_state_alloc) {
114 struct drm_atomic_state *state;
116 state = kzalloc(sizeof(*state), GFP_KERNEL);
119 if (drm_atomic_state_init(dev, state) < 0) {
126 return config->funcs->atomic_state_alloc(dev);
128 EXPORT_SYMBOL(drm_atomic_state_alloc);
131 * drm_atomic_state_default_clear - clear base atomic state
132 * @state: atomic state
134 * Default implementation for clearing atomic state.
135 * This is useful for drivers that subclass the atomic state.
137 void drm_atomic_state_default_clear(struct drm_atomic_state *state)
139 struct drm_device *dev = state->dev;
140 struct drm_mode_config *config = &dev->mode_config;
143 DRM_DEBUG_ATOMIC("Clearing atomic state %p\n", state);
145 for (i = 0; i < state->num_connector; i++) {
146 struct drm_connector *connector = state->connectors[i].ptr;
151 connector->funcs->atomic_destroy_state(connector,
152 state->connectors[i].state);
153 state->connectors[i].ptr = NULL;
154 state->connectors[i].state = NULL;
155 drm_connector_put(connector);
158 for (i = 0; i < config->num_crtc; i++) {
159 struct drm_crtc *crtc = state->crtcs[i].ptr;
164 crtc->funcs->atomic_destroy_state(crtc,
165 state->crtcs[i].state);
167 state->crtcs[i].ptr = NULL;
168 state->crtcs[i].state = NULL;
171 for (i = 0; i < config->num_total_plane; i++) {
172 struct drm_plane *plane = state->planes[i].ptr;
177 plane->funcs->atomic_destroy_state(plane,
178 state->planes[i].state);
179 state->planes[i].ptr = NULL;
180 state->planes[i].state = NULL;
183 for (i = 0; i < state->num_private_objs; i++) {
184 struct drm_private_obj *obj = state->private_objs[i].ptr;
186 obj->funcs->atomic_destroy_state(obj,
187 state->private_objs[i].state);
188 state->private_objs[i].ptr = NULL;
189 state->private_objs[i].state = NULL;
191 state->num_private_objs = 0;
193 if (state->fake_commit) {
194 drm_crtc_commit_put(state->fake_commit);
195 state->fake_commit = NULL;
198 EXPORT_SYMBOL(drm_atomic_state_default_clear);
201 * drm_atomic_state_clear - clear state object
202 * @state: atomic state
204 * When the w/w mutex algorithm detects a deadlock we need to back off and drop
205 * all locks. So someone else could sneak in and change the current modeset
206 * configuration. Which means that all the state assembled in @state is no
207 * longer an atomic update to the current state, but to some arbitrary earlier
208 * state. Which could break assumptions the driver's
209 * &drm_mode_config_funcs.atomic_check likely relies on.
211 * Hence we must clear all cached state and completely start over, using this
214 void drm_atomic_state_clear(struct drm_atomic_state *state)
216 struct drm_device *dev = state->dev;
217 struct drm_mode_config *config = &dev->mode_config;
219 if (config->funcs->atomic_state_clear)
220 config->funcs->atomic_state_clear(state);
222 drm_atomic_state_default_clear(state);
224 EXPORT_SYMBOL(drm_atomic_state_clear);
227 * __drm_atomic_state_free - free all memory for an atomic state
228 * @ref: This atomic state to deallocate
230 * This frees all memory associated with an atomic state, including all the
231 * per-object state for planes, crtcs and connectors.
233 void __drm_atomic_state_free(struct kref *ref)
235 struct drm_atomic_state *state = container_of(ref, typeof(*state), ref);
236 struct drm_mode_config *config = &state->dev->mode_config;
238 drm_atomic_state_clear(state);
240 DRM_DEBUG_ATOMIC("Freeing atomic state %p\n", state);
242 if (config->funcs->atomic_state_free) {
243 config->funcs->atomic_state_free(state);
245 drm_atomic_state_default_release(state);
249 EXPORT_SYMBOL(__drm_atomic_state_free);
252 * drm_atomic_get_crtc_state - get crtc state
253 * @state: global atomic state object
254 * @crtc: crtc to get state object for
256 * This function returns the crtc state for the given crtc, allocating it if
257 * needed. It will also grab the relevant crtc lock to make sure that the state
262 * Either the allocated state or the error code encoded into the pointer. When
263 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
264 * entire atomic sequence must be restarted. All other errors are fatal.
266 struct drm_crtc_state *
267 drm_atomic_get_crtc_state(struct drm_atomic_state *state,
268 struct drm_crtc *crtc)
270 int ret, index = drm_crtc_index(crtc);
271 struct drm_crtc_state *crtc_state;
273 WARN_ON(!state->acquire_ctx);
275 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
279 ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx);
283 crtc_state = crtc->funcs->atomic_duplicate_state(crtc);
285 return ERR_PTR(-ENOMEM);
287 state->crtcs[index].state = crtc_state;
288 state->crtcs[index].old_state = crtc->state;
289 state->crtcs[index].new_state = crtc_state;
290 state->crtcs[index].ptr = crtc;
291 crtc_state->state = state;
293 DRM_DEBUG_ATOMIC("Added [CRTC:%d:%s] %p state to %p\n",
294 crtc->base.id, crtc->name, crtc_state, state);
298 EXPORT_SYMBOL(drm_atomic_get_crtc_state);
300 static void set_out_fence_for_crtc(struct drm_atomic_state *state,
301 struct drm_crtc *crtc, s32 __user *fence_ptr)
303 state->crtcs[drm_crtc_index(crtc)].out_fence_ptr = fence_ptr;
306 static s32 __user *get_out_fence_for_crtc(struct drm_atomic_state *state,
307 struct drm_crtc *crtc)
309 s32 __user *fence_ptr;
311 fence_ptr = state->crtcs[drm_crtc_index(crtc)].out_fence_ptr;
312 state->crtcs[drm_crtc_index(crtc)].out_fence_ptr = NULL;
318 * drm_atomic_set_mode_for_crtc - set mode for CRTC
319 * @state: the CRTC whose incoming state to update
320 * @mode: kernel-internal mode to use for the CRTC, or NULL to disable
322 * Set a mode (originating from the kernel) on the desired CRTC state and update
323 * the enable property.
326 * Zero on success, error code on failure. Cannot return -EDEADLK.
328 int drm_atomic_set_mode_for_crtc(struct drm_crtc_state *state,
329 const struct drm_display_mode *mode)
331 struct drm_mode_modeinfo umode;
333 /* Early return for no change. */
334 if (mode && memcmp(&state->mode, mode, sizeof(*mode)) == 0)
337 drm_property_blob_put(state->mode_blob);
338 state->mode_blob = NULL;
341 drm_mode_convert_to_umode(&umode, mode);
343 drm_property_create_blob(state->crtc->dev,
346 if (IS_ERR(state->mode_blob))
347 return PTR_ERR(state->mode_blob);
349 drm_mode_copy(&state->mode, mode);
350 state->enable = true;
351 DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n",
354 memset(&state->mode, 0, sizeof(state->mode));
355 state->enable = false;
356 DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n",
362 EXPORT_SYMBOL(drm_atomic_set_mode_for_crtc);
365 * drm_atomic_set_mode_prop_for_crtc - set mode for CRTC
366 * @state: the CRTC whose incoming state to update
367 * @blob: pointer to blob property to use for mode
369 * Set a mode (originating from a blob property) on the desired CRTC state.
370 * This function will take a reference on the blob property for the CRTC state,
371 * and release the reference held on the state's existing mode property, if any
375 * Zero on success, error code on failure. Cannot return -EDEADLK.
377 int drm_atomic_set_mode_prop_for_crtc(struct drm_crtc_state *state,
378 struct drm_property_blob *blob)
380 if (blob == state->mode_blob)
383 drm_property_blob_put(state->mode_blob);
384 state->mode_blob = NULL;
386 memset(&state->mode, 0, sizeof(state->mode));
389 if (blob->length != sizeof(struct drm_mode_modeinfo) ||
390 drm_mode_convert_umode(&state->mode,
391 (const struct drm_mode_modeinfo *)
395 state->mode_blob = drm_property_blob_get(blob);
396 state->enable = true;
397 DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n",
398 state->mode.name, state);
400 state->enable = false;
401 DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n",
407 EXPORT_SYMBOL(drm_atomic_set_mode_prop_for_crtc);
410 drm_atomic_replace_property_blob_from_id(struct drm_device *dev,
411 struct drm_property_blob **blob,
413 ssize_t expected_size,
416 struct drm_property_blob *new_blob = NULL;
419 new_blob = drm_property_lookup_blob(dev, blob_id);
420 if (new_blob == NULL)
423 if (expected_size > 0 && expected_size != new_blob->length) {
424 drm_property_blob_put(new_blob);
429 *replaced |= drm_property_replace_blob(blob, new_blob);
430 drm_property_blob_put(new_blob);
436 * drm_atomic_crtc_set_property - set property on CRTC
437 * @crtc: the drm CRTC to set a property on
438 * @state: the state object to update with the new property value
439 * @property: the property to set
440 * @val: the new property value
442 * This function handles generic/core properties and calls out to driver's
443 * &drm_crtc_funcs.atomic_set_property for driver properties. To ensure
444 * consistent behavior you must call this function rather than the driver hook
448 * Zero on success, error code on failure
450 int drm_atomic_crtc_set_property(struct drm_crtc *crtc,
451 struct drm_crtc_state *state, struct drm_property *property,
454 struct drm_device *dev = crtc->dev;
455 struct drm_mode_config *config = &dev->mode_config;
456 bool replaced = false;
459 if (property == config->prop_active)
461 else if (property == config->prop_mode_id) {
462 struct drm_property_blob *mode =
463 drm_property_lookup_blob(dev, val);
464 ret = drm_atomic_set_mode_prop_for_crtc(state, mode);
465 drm_property_blob_put(mode);
467 } else if (property == config->degamma_lut_property) {
468 ret = drm_atomic_replace_property_blob_from_id(dev,
473 state->color_mgmt_changed |= replaced;
475 } else if (property == config->ctm_property) {
476 ret = drm_atomic_replace_property_blob_from_id(dev,
479 sizeof(struct drm_color_ctm),
481 state->color_mgmt_changed |= replaced;
483 } else if (property == config->gamma_lut_property) {
484 ret = drm_atomic_replace_property_blob_from_id(dev,
489 state->color_mgmt_changed |= replaced;
491 } else if (property == config->prop_out_fence_ptr) {
492 s32 __user *fence_ptr = u64_to_user_ptr(val);
497 if (put_user(-1, fence_ptr))
500 set_out_fence_for_crtc(state->state, crtc, fence_ptr);
501 } else if (crtc->funcs->atomic_set_property)
502 return crtc->funcs->atomic_set_property(crtc, state, property, val);
508 EXPORT_SYMBOL(drm_atomic_crtc_set_property);
511 * drm_atomic_crtc_get_property - get property value from CRTC state
512 * @crtc: the drm CRTC to set a property on
513 * @state: the state object to get the property value from
514 * @property: the property to set
515 * @val: return location for the property value
517 * This function handles generic/core properties and calls out to driver's
518 * &drm_crtc_funcs.atomic_get_property for driver properties. To ensure
519 * consistent behavior you must call this function rather than the driver hook
523 * Zero on success, error code on failure
526 drm_atomic_crtc_get_property(struct drm_crtc *crtc,
527 const struct drm_crtc_state *state,
528 struct drm_property *property, uint64_t *val)
530 struct drm_device *dev = crtc->dev;
531 struct drm_mode_config *config = &dev->mode_config;
533 if (property == config->prop_active)
534 *val = state->active;
535 else if (property == config->prop_mode_id)
536 *val = (state->mode_blob) ? state->mode_blob->base.id : 0;
537 else if (property == config->degamma_lut_property)
538 *val = (state->degamma_lut) ? state->degamma_lut->base.id : 0;
539 else if (property == config->ctm_property)
540 *val = (state->ctm) ? state->ctm->base.id : 0;
541 else if (property == config->gamma_lut_property)
542 *val = (state->gamma_lut) ? state->gamma_lut->base.id : 0;
543 else if (property == config->prop_out_fence_ptr)
545 else if (crtc->funcs->atomic_get_property)
546 return crtc->funcs->atomic_get_property(crtc, state, property, val);
554 * drm_atomic_crtc_check - check crtc state
555 * @crtc: crtc to check
556 * @state: crtc state to check
558 * Provides core sanity checks for crtc state.
561 * Zero on success, error code on failure
563 static int drm_atomic_crtc_check(struct drm_crtc *crtc,
564 struct drm_crtc_state *state)
566 /* NOTE: we explicitly don't enforce constraints such as primary
567 * layer covering entire screen, since that is something we want
568 * to allow (on hw that supports it). For hw that does not, it
569 * should be checked in driver's crtc->atomic_check() vfunc.
571 * TODO: Add generic modeset state checks once we support those.
574 if (state->active && !state->enable) {
575 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active without enabled\n",
576 crtc->base.id, crtc->name);
580 /* The state->enable vs. state->mode_blob checks can be WARN_ON,
581 * as this is a kernel-internal detail that userspace should never
582 * be able to trigger. */
583 if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
584 WARN_ON(state->enable && !state->mode_blob)) {
585 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled without mode blob\n",
586 crtc->base.id, crtc->name);
590 if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
591 WARN_ON(!state->enable && state->mode_blob)) {
592 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] disabled with mode blob\n",
593 crtc->base.id, crtc->name);
598 * Reject event generation for when a CRTC is off and stays off.
599 * It wouldn't be hard to implement this, but userspace has a track
600 * record of happily burning through 100% cpu (or worse, crash) when the
601 * display pipe is suspended. To avoid all that fun just reject updates
602 * that ask for events since likely that indicates a bug in the
603 * compositor's drawing loop. This is consistent with the vblank IOCTL
604 * and legacy page_flip IOCTL which also reject service on a disabled
607 if (state->event && !state->active && !crtc->state->active) {
608 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] requesting event but off\n",
609 crtc->base.id, crtc->name);
616 static void drm_atomic_crtc_print_state(struct drm_printer *p,
617 const struct drm_crtc_state *state)
619 struct drm_crtc *crtc = state->crtc;
621 drm_printf(p, "crtc[%u]: %s\n", crtc->base.id, crtc->name);
622 drm_printf(p, "\tenable=%d\n", state->enable);
623 drm_printf(p, "\tactive=%d\n", state->active);
624 drm_printf(p, "\tplanes_changed=%d\n", state->planes_changed);
625 drm_printf(p, "\tmode_changed=%d\n", state->mode_changed);
626 drm_printf(p, "\tactive_changed=%d\n", state->active_changed);
627 drm_printf(p, "\tconnectors_changed=%d\n", state->connectors_changed);
628 drm_printf(p, "\tcolor_mgmt_changed=%d\n", state->color_mgmt_changed);
629 drm_printf(p, "\tplane_mask=%x\n", state->plane_mask);
630 drm_printf(p, "\tconnector_mask=%x\n", state->connector_mask);
631 drm_printf(p, "\tencoder_mask=%x\n", state->encoder_mask);
632 drm_printf(p, "\tmode: " DRM_MODE_FMT "\n", DRM_MODE_ARG(&state->mode));
634 if (crtc->funcs->atomic_print_state)
635 crtc->funcs->atomic_print_state(p, state);
639 * drm_atomic_get_plane_state - get plane state
640 * @state: global atomic state object
641 * @plane: plane to get state object for
643 * This function returns the plane state for the given plane, allocating it if
644 * needed. It will also grab the relevant plane lock to make sure that the state
649 * Either the allocated state or the error code encoded into the pointer. When
650 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
651 * entire atomic sequence must be restarted. All other errors are fatal.
653 struct drm_plane_state *
654 drm_atomic_get_plane_state(struct drm_atomic_state *state,
655 struct drm_plane *plane)
657 int ret, index = drm_plane_index(plane);
658 struct drm_plane_state *plane_state;
660 WARN_ON(!state->acquire_ctx);
662 plane_state = drm_atomic_get_existing_plane_state(state, plane);
666 ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
670 plane_state = plane->funcs->atomic_duplicate_state(plane);
672 return ERR_PTR(-ENOMEM);
674 state->planes[index].state = plane_state;
675 state->planes[index].ptr = plane;
676 state->planes[index].old_state = plane->state;
677 state->planes[index].new_state = plane_state;
678 plane_state->state = state;
680 DRM_DEBUG_ATOMIC("Added [PLANE:%d:%s] %p state to %p\n",
681 plane->base.id, plane->name, plane_state, state);
683 if (plane_state->crtc) {
684 struct drm_crtc_state *crtc_state;
686 crtc_state = drm_atomic_get_crtc_state(state,
688 if (IS_ERR(crtc_state))
689 return ERR_CAST(crtc_state);
694 EXPORT_SYMBOL(drm_atomic_get_plane_state);
697 * drm_atomic_plane_set_property - set property on plane
698 * @plane: the drm plane to set a property on
699 * @state: the state object to update with the new property value
700 * @property: the property to set
701 * @val: the new property value
703 * This function handles generic/core properties and calls out to driver's
704 * &drm_plane_funcs.atomic_set_property for driver properties. To ensure
705 * consistent behavior you must call this function rather than the driver hook
709 * Zero on success, error code on failure
711 static int drm_atomic_plane_set_property(struct drm_plane *plane,
712 struct drm_plane_state *state, struct drm_property *property,
715 struct drm_device *dev = plane->dev;
716 struct drm_mode_config *config = &dev->mode_config;
718 if (property == config->prop_fb_id) {
719 struct drm_framebuffer *fb = drm_framebuffer_lookup(dev, NULL, val);
720 drm_atomic_set_fb_for_plane(state, fb);
722 drm_framebuffer_put(fb);
723 } else if (property == config->prop_in_fence_fd) {
727 if (U642I64(val) == -1)
730 state->fence = sync_file_get_fence(val);
734 } else if (property == config->prop_crtc_id) {
735 struct drm_crtc *crtc = drm_crtc_find(dev, NULL, val);
736 return drm_atomic_set_crtc_for_plane(state, crtc);
737 } else if (property == config->prop_crtc_x) {
738 state->crtc_x = U642I64(val);
739 } else if (property == config->prop_crtc_y) {
740 state->crtc_y = U642I64(val);
741 } else if (property == config->prop_crtc_w) {
743 } else if (property == config->prop_crtc_h) {
745 } else if (property == config->prop_src_x) {
747 } else if (property == config->prop_src_y) {
749 } else if (property == config->prop_src_w) {
751 } else if (property == config->prop_src_h) {
753 } else if (property == plane->rotation_property) {
754 if (!is_power_of_2(val & DRM_MODE_ROTATE_MASK))
756 state->rotation = val;
757 } else if (property == plane->zpos_property) {
759 } else if (plane->funcs->atomic_set_property) {
760 return plane->funcs->atomic_set_property(plane, state,
770 * drm_atomic_plane_get_property - get property value from plane state
771 * @plane: the drm plane to set a property on
772 * @state: the state object to get the property value from
773 * @property: the property to set
774 * @val: return location for the property value
776 * This function handles generic/core properties and calls out to driver's
777 * &drm_plane_funcs.atomic_get_property for driver properties. To ensure
778 * consistent behavior you must call this function rather than the driver hook
782 * Zero on success, error code on failure
785 drm_atomic_plane_get_property(struct drm_plane *plane,
786 const struct drm_plane_state *state,
787 struct drm_property *property, uint64_t *val)
789 struct drm_device *dev = plane->dev;
790 struct drm_mode_config *config = &dev->mode_config;
792 if (property == config->prop_fb_id) {
793 *val = (state->fb) ? state->fb->base.id : 0;
794 } else if (property == config->prop_in_fence_fd) {
796 } else if (property == config->prop_crtc_id) {
797 *val = (state->crtc) ? state->crtc->base.id : 0;
798 } else if (property == config->prop_crtc_x) {
799 *val = I642U64(state->crtc_x);
800 } else if (property == config->prop_crtc_y) {
801 *val = I642U64(state->crtc_y);
802 } else if (property == config->prop_crtc_w) {
803 *val = state->crtc_w;
804 } else if (property == config->prop_crtc_h) {
805 *val = state->crtc_h;
806 } else if (property == config->prop_src_x) {
808 } else if (property == config->prop_src_y) {
810 } else if (property == config->prop_src_w) {
812 } else if (property == config->prop_src_h) {
814 } else if (property == plane->rotation_property) {
815 *val = state->rotation;
816 } else if (property == plane->zpos_property) {
818 } else if (plane->funcs->atomic_get_property) {
819 return plane->funcs->atomic_get_property(plane, state, property, val);
828 plane_switching_crtc(struct drm_atomic_state *state,
829 struct drm_plane *plane,
830 struct drm_plane_state *plane_state)
832 if (!plane->state->crtc || !plane_state->crtc)
835 if (plane->state->crtc == plane_state->crtc)
838 /* This could be refined, but currently there's no helper or driver code
839 * to implement direct switching of active planes nor userspace to take
840 * advantage of more direct plane switching without the intermediate
847 * drm_atomic_plane_check - check plane state
848 * @plane: plane to check
849 * @state: plane state to check
851 * Provides core sanity checks for plane state.
854 * Zero on success, error code on failure
856 static int drm_atomic_plane_check(struct drm_plane *plane,
857 struct drm_plane_state *state)
859 unsigned int fb_width, fb_height;
862 /* either *both* CRTC and FB must be set, or neither */
863 if (WARN_ON(state->crtc && !state->fb)) {
864 DRM_DEBUG_ATOMIC("CRTC set but no FB\n");
866 } else if (WARN_ON(state->fb && !state->crtc)) {
867 DRM_DEBUG_ATOMIC("FB set but no CRTC\n");
871 /* if disabled, we don't care about the rest of the state: */
875 /* Check whether this plane is usable on this CRTC */
876 if (!(plane->possible_crtcs & drm_crtc_mask(state->crtc))) {
877 DRM_DEBUG_ATOMIC("Invalid crtc for plane\n");
881 /* Check whether this plane supports the fb pixel format. */
882 ret = drm_plane_check_pixel_format(plane, state->fb->format->format);
884 struct drm_format_name_buf format_name;
885 DRM_DEBUG_ATOMIC("Invalid pixel format %s\n",
886 drm_get_format_name(state->fb->format->format,
891 /* Give drivers some help against integer overflows */
892 if (state->crtc_w > INT_MAX ||
893 state->crtc_x > INT_MAX - (int32_t) state->crtc_w ||
894 state->crtc_h > INT_MAX ||
895 state->crtc_y > INT_MAX - (int32_t) state->crtc_h) {
896 DRM_DEBUG_ATOMIC("Invalid CRTC coordinates %ux%u+%d+%d\n",
897 state->crtc_w, state->crtc_h,
898 state->crtc_x, state->crtc_y);
902 fb_width = state->fb->width << 16;
903 fb_height = state->fb->height << 16;
905 /* Make sure source coordinates are inside the fb. */
906 if (state->src_w > fb_width ||
907 state->src_x > fb_width - state->src_w ||
908 state->src_h > fb_height ||
909 state->src_y > fb_height - state->src_h) {
910 DRM_DEBUG_ATOMIC("Invalid source coordinates "
911 "%u.%06ux%u.%06u+%u.%06u+%u.%06u (fb %ux%u)\n",
912 state->src_w >> 16, ((state->src_w & 0xffff) * 15625) >> 10,
913 state->src_h >> 16, ((state->src_h & 0xffff) * 15625) >> 10,
914 state->src_x >> 16, ((state->src_x & 0xffff) * 15625) >> 10,
915 state->src_y >> 16, ((state->src_y & 0xffff) * 15625) >> 10,
916 state->fb->width, state->fb->height);
920 if (plane_switching_crtc(state->state, plane, state)) {
921 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] switching CRTC directly\n",
922 plane->base.id, plane->name);
929 static void drm_atomic_plane_print_state(struct drm_printer *p,
930 const struct drm_plane_state *state)
932 struct drm_plane *plane = state->plane;
933 struct drm_rect src = drm_plane_state_src(state);
934 struct drm_rect dest = drm_plane_state_dest(state);
936 drm_printf(p, "plane[%u]: %s\n", plane->base.id, plane->name);
937 drm_printf(p, "\tcrtc=%s\n", state->crtc ? state->crtc->name : "(null)");
938 drm_printf(p, "\tfb=%u\n", state->fb ? state->fb->base.id : 0);
940 drm_framebuffer_print_info(p, 2, state->fb);
941 drm_printf(p, "\tcrtc-pos=" DRM_RECT_FMT "\n", DRM_RECT_ARG(&dest));
942 drm_printf(p, "\tsrc-pos=" DRM_RECT_FP_FMT "\n", DRM_RECT_FP_ARG(&src));
943 drm_printf(p, "\trotation=%x\n", state->rotation);
945 if (plane->funcs->atomic_print_state)
946 plane->funcs->atomic_print_state(p, state);
950 * drm_atomic_private_obj_init - initialize private object
951 * @obj: private object
952 * @state: initial private object state
953 * @funcs: pointer to the struct of function pointers that identify the object
956 * Initialize the private object, which can be embedded into any
957 * driver private object that needs its own atomic state.
960 drm_atomic_private_obj_init(struct drm_private_obj *obj,
961 struct drm_private_state *state,
962 const struct drm_private_state_funcs *funcs)
964 memset(obj, 0, sizeof(*obj));
969 EXPORT_SYMBOL(drm_atomic_private_obj_init);
972 * drm_atomic_private_obj_fini - finalize private object
973 * @obj: private object
975 * Finalize the private object.
978 drm_atomic_private_obj_fini(struct drm_private_obj *obj)
980 obj->funcs->atomic_destroy_state(obj, obj->state);
982 EXPORT_SYMBOL(drm_atomic_private_obj_fini);
985 * drm_atomic_get_private_obj_state - get private object state
986 * @state: global atomic state
987 * @obj: private object to get the state for
989 * This function returns the private object state for the given private object,
990 * allocating the state if needed. It does not grab any locks as the caller is
991 * expected to care of any required locking.
995 * Either the allocated state or the error code encoded into a pointer.
997 struct drm_private_state *
998 drm_atomic_get_private_obj_state(struct drm_atomic_state *state,
999 struct drm_private_obj *obj)
1001 int index, num_objs, i;
1003 struct __drm_private_objs_state *arr;
1004 struct drm_private_state *obj_state;
1006 for (i = 0; i < state->num_private_objs; i++)
1007 if (obj == state->private_objs[i].ptr)
1008 return state->private_objs[i].state;
1010 num_objs = state->num_private_objs + 1;
1011 size = sizeof(*state->private_objs) * num_objs;
1012 arr = krealloc(state->private_objs, size, GFP_KERNEL);
1014 return ERR_PTR(-ENOMEM);
1016 state->private_objs = arr;
1017 index = state->num_private_objs;
1018 memset(&state->private_objs[index], 0, sizeof(*state->private_objs));
1020 obj_state = obj->funcs->atomic_duplicate_state(obj);
1022 return ERR_PTR(-ENOMEM);
1024 state->private_objs[index].state = obj_state;
1025 state->private_objs[index].old_state = obj->state;
1026 state->private_objs[index].new_state = obj_state;
1027 state->private_objs[index].ptr = obj;
1029 state->num_private_objs = num_objs;
1031 DRM_DEBUG_ATOMIC("Added new private object %p state %p to %p\n",
1032 obj, obj_state, state);
1036 EXPORT_SYMBOL(drm_atomic_get_private_obj_state);
1039 * drm_atomic_get_connector_state - get connector state
1040 * @state: global atomic state object
1041 * @connector: connector to get state object for
1043 * This function returns the connector state for the given connector,
1044 * allocating it if needed. It will also grab the relevant connector lock to
1045 * make sure that the state is consistent.
1049 * Either the allocated state or the error code encoded into the pointer. When
1050 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
1051 * entire atomic sequence must be restarted. All other errors are fatal.
1053 struct drm_connector_state *
1054 drm_atomic_get_connector_state(struct drm_atomic_state *state,
1055 struct drm_connector *connector)
1058 struct drm_mode_config *config = &connector->dev->mode_config;
1059 struct drm_connector_state *connector_state;
1061 WARN_ON(!state->acquire_ctx);
1063 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
1065 return ERR_PTR(ret);
1067 index = drm_connector_index(connector);
1069 if (index >= state->num_connector) {
1070 struct __drm_connnectors_state *c;
1071 int alloc = max(index + 1, config->num_connector);
1073 c = krealloc(state->connectors, alloc * sizeof(*state->connectors), GFP_KERNEL);
1075 return ERR_PTR(-ENOMEM);
1077 state->connectors = c;
1078 memset(&state->connectors[state->num_connector], 0,
1079 sizeof(*state->connectors) * (alloc - state->num_connector));
1081 state->num_connector = alloc;
1084 if (state->connectors[index].state)
1085 return state->connectors[index].state;
1087 connector_state = connector->funcs->atomic_duplicate_state(connector);
1088 if (!connector_state)
1089 return ERR_PTR(-ENOMEM);
1091 drm_connector_get(connector);
1092 state->connectors[index].state = connector_state;
1093 state->connectors[index].old_state = connector->state;
1094 state->connectors[index].new_state = connector_state;
1095 state->connectors[index].ptr = connector;
1096 connector_state->state = state;
1098 DRM_DEBUG_ATOMIC("Added [CONNECTOR:%d:%s] %p state to %p\n",
1099 connector->base.id, connector->name,
1100 connector_state, state);
1102 if (connector_state->crtc) {
1103 struct drm_crtc_state *crtc_state;
1105 crtc_state = drm_atomic_get_crtc_state(state,
1106 connector_state->crtc);
1107 if (IS_ERR(crtc_state))
1108 return ERR_CAST(crtc_state);
1111 return connector_state;
1113 EXPORT_SYMBOL(drm_atomic_get_connector_state);
1116 * drm_atomic_connector_set_property - set property on connector.
1117 * @connector: the drm connector to set a property on
1118 * @state: the state object to update with the new property value
1119 * @property: the property to set
1120 * @val: the new property value
1122 * This function handles generic/core properties and calls out to driver's
1123 * &drm_connector_funcs.atomic_set_property for driver properties. To ensure
1124 * consistent behavior you must call this function rather than the driver hook
1128 * Zero on success, error code on failure
1130 static int drm_atomic_connector_set_property(struct drm_connector *connector,
1131 struct drm_connector_state *state, struct drm_property *property,
1134 struct drm_device *dev = connector->dev;
1135 struct drm_mode_config *config = &dev->mode_config;
1137 if (property == config->prop_crtc_id) {
1138 struct drm_crtc *crtc = drm_crtc_find(dev, NULL, val);
1139 return drm_atomic_set_crtc_for_connector(state, crtc);
1140 } else if (property == config->dpms_property) {
1141 /* setting DPMS property requires special handling, which
1142 * is done in legacy setprop path for us. Disallow (for
1143 * now?) atomic writes to DPMS property:
1146 } else if (property == config->tv_select_subconnector_property) {
1147 state->tv.subconnector = val;
1148 } else if (property == config->tv_left_margin_property) {
1149 state->tv.margins.left = val;
1150 } else if (property == config->tv_right_margin_property) {
1151 state->tv.margins.right = val;
1152 } else if (property == config->tv_top_margin_property) {
1153 state->tv.margins.top = val;
1154 } else if (property == config->tv_bottom_margin_property) {
1155 state->tv.margins.bottom = val;
1156 } else if (property == config->tv_mode_property) {
1157 state->tv.mode = val;
1158 } else if (property == config->tv_brightness_property) {
1159 state->tv.brightness = val;
1160 } else if (property == config->tv_contrast_property) {
1161 state->tv.contrast = val;
1162 } else if (property == config->tv_flicker_reduction_property) {
1163 state->tv.flicker_reduction = val;
1164 } else if (property == config->tv_overscan_property) {
1165 state->tv.overscan = val;
1166 } else if (property == config->tv_saturation_property) {
1167 state->tv.saturation = val;
1168 } else if (property == config->tv_hue_property) {
1169 state->tv.hue = val;
1170 } else if (property == config->link_status_property) {
1171 /* Never downgrade from GOOD to BAD on userspace's request here,
1172 * only hw issues can do that.
1174 * For an atomic property the userspace doesn't need to be able
1175 * to understand all the properties, but needs to be able to
1176 * restore the state it wants on VT switch. So if the userspace
1177 * tries to change the link_status from GOOD to BAD, driver
1178 * silently rejects it and returns a 0. This prevents userspace
1179 * from accidently breaking the display when it restores the
1182 if (state->link_status != DRM_LINK_STATUS_GOOD)
1183 state->link_status = val;
1184 } else if (property == config->aspect_ratio_property) {
1185 state->picture_aspect_ratio = val;
1186 } else if (property == connector->scaling_mode_property) {
1187 state->scaling_mode = val;
1188 } else if (connector->funcs->atomic_set_property) {
1189 return connector->funcs->atomic_set_property(connector,
1190 state, property, val);
1198 static void drm_atomic_connector_print_state(struct drm_printer *p,
1199 const struct drm_connector_state *state)
1201 struct drm_connector *connector = state->connector;
1203 drm_printf(p, "connector[%u]: %s\n", connector->base.id, connector->name);
1204 drm_printf(p, "\tcrtc=%s\n", state->crtc ? state->crtc->name : "(null)");
1206 if (connector->funcs->atomic_print_state)
1207 connector->funcs->atomic_print_state(p, state);
1211 * drm_atomic_connector_get_property - get property value from connector state
1212 * @connector: the drm connector to set a property on
1213 * @state: the state object to get the property value from
1214 * @property: the property to set
1215 * @val: return location for the property value
1217 * This function handles generic/core properties and calls out to driver's
1218 * &drm_connector_funcs.atomic_get_property for driver properties. To ensure
1219 * consistent behavior you must call this function rather than the driver hook
1223 * Zero on success, error code on failure
1226 drm_atomic_connector_get_property(struct drm_connector *connector,
1227 const struct drm_connector_state *state,
1228 struct drm_property *property, uint64_t *val)
1230 struct drm_device *dev = connector->dev;
1231 struct drm_mode_config *config = &dev->mode_config;
1233 if (property == config->prop_crtc_id) {
1234 *val = (state->crtc) ? state->crtc->base.id : 0;
1235 } else if (property == config->dpms_property) {
1236 *val = connector->dpms;
1237 } else if (property == config->tv_select_subconnector_property) {
1238 *val = state->tv.subconnector;
1239 } else if (property == config->tv_left_margin_property) {
1240 *val = state->tv.margins.left;
1241 } else if (property == config->tv_right_margin_property) {
1242 *val = state->tv.margins.right;
1243 } else if (property == config->tv_top_margin_property) {
1244 *val = state->tv.margins.top;
1245 } else if (property == config->tv_bottom_margin_property) {
1246 *val = state->tv.margins.bottom;
1247 } else if (property == config->tv_mode_property) {
1248 *val = state->tv.mode;
1249 } else if (property == config->tv_brightness_property) {
1250 *val = state->tv.brightness;
1251 } else if (property == config->tv_contrast_property) {
1252 *val = state->tv.contrast;
1253 } else if (property == config->tv_flicker_reduction_property) {
1254 *val = state->tv.flicker_reduction;
1255 } else if (property == config->tv_overscan_property) {
1256 *val = state->tv.overscan;
1257 } else if (property == config->tv_saturation_property) {
1258 *val = state->tv.saturation;
1259 } else if (property == config->tv_hue_property) {
1260 *val = state->tv.hue;
1261 } else if (property == config->link_status_property) {
1262 *val = state->link_status;
1263 } else if (property == config->aspect_ratio_property) {
1264 *val = state->picture_aspect_ratio;
1265 } else if (property == connector->scaling_mode_property) {
1266 *val = state->scaling_mode;
1267 } else if (connector->funcs->atomic_get_property) {
1268 return connector->funcs->atomic_get_property(connector,
1269 state, property, val);
1277 int drm_atomic_get_property(struct drm_mode_object *obj,
1278 struct drm_property *property, uint64_t *val)
1280 struct drm_device *dev = property->dev;
1283 switch (obj->type) {
1284 case DRM_MODE_OBJECT_CONNECTOR: {
1285 struct drm_connector *connector = obj_to_connector(obj);
1286 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
1287 ret = drm_atomic_connector_get_property(connector,
1288 connector->state, property, val);
1291 case DRM_MODE_OBJECT_CRTC: {
1292 struct drm_crtc *crtc = obj_to_crtc(obj);
1293 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
1294 ret = drm_atomic_crtc_get_property(crtc,
1295 crtc->state, property, val);
1298 case DRM_MODE_OBJECT_PLANE: {
1299 struct drm_plane *plane = obj_to_plane(obj);
1300 WARN_ON(!drm_modeset_is_locked(&plane->mutex));
1301 ret = drm_atomic_plane_get_property(plane,
1302 plane->state, property, val);
1314 * drm_atomic_set_crtc_for_plane - set crtc for plane
1315 * @plane_state: the plane whose incoming state to update
1316 * @crtc: crtc to use for the plane
1318 * Changing the assigned crtc for a plane requires us to grab the lock and state
1319 * for the new crtc, as needed. This function takes care of all these details
1320 * besides updating the pointer in the state object itself.
1323 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1324 * then the w/w mutex code has detected a deadlock and the entire atomic
1325 * sequence must be restarted. All other errors are fatal.
1328 drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
1329 struct drm_crtc *crtc)
1331 struct drm_plane *plane = plane_state->plane;
1332 struct drm_crtc_state *crtc_state;
1334 if (plane_state->crtc) {
1335 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
1337 if (WARN_ON(IS_ERR(crtc_state)))
1338 return PTR_ERR(crtc_state);
1340 crtc_state->plane_mask &= ~(1 << drm_plane_index(plane));
1343 plane_state->crtc = crtc;
1346 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
1348 if (IS_ERR(crtc_state))
1349 return PTR_ERR(crtc_state);
1350 crtc_state->plane_mask |= (1 << drm_plane_index(plane));
1354 DRM_DEBUG_ATOMIC("Link plane state %p to [CRTC:%d:%s]\n",
1355 plane_state, crtc->base.id, crtc->name);
1357 DRM_DEBUG_ATOMIC("Link plane state %p to [NOCRTC]\n",
1362 EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane);
1365 * drm_atomic_set_fb_for_plane - set framebuffer for plane
1366 * @plane_state: atomic state object for the plane
1367 * @fb: fb to use for the plane
1369 * Changing the assigned framebuffer for a plane requires us to grab a reference
1370 * to the new fb and drop the reference to the old fb, if there is one. This
1371 * function takes care of all these details besides updating the pointer in the
1372 * state object itself.
1375 drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
1376 struct drm_framebuffer *fb)
1379 DRM_DEBUG_ATOMIC("Set [FB:%d] for plane state %p\n",
1380 fb->base.id, plane_state);
1382 DRM_DEBUG_ATOMIC("Set [NOFB] for plane state %p\n",
1385 drm_framebuffer_assign(&plane_state->fb, fb);
1387 EXPORT_SYMBOL(drm_atomic_set_fb_for_plane);
1390 * drm_atomic_set_fence_for_plane - set fence for plane
1391 * @plane_state: atomic state object for the plane
1392 * @fence: dma_fence to use for the plane
1394 * Helper to setup the plane_state fence in case it is not set yet.
1395 * By using this drivers doesn't need to worry if the user choose
1396 * implicit or explicit fencing.
1398 * This function will not set the fence to the state if it was set
1399 * via explicit fencing interfaces on the atomic ioctl. In that case it will
1400 * drop the reference to the fence as we are not storing it anywhere.
1401 * Otherwise, if &drm_plane_state.fence is not set this function we just set it
1402 * with the received implicit fence. In both cases this function consumes a
1403 * reference for @fence.
1406 drm_atomic_set_fence_for_plane(struct drm_plane_state *plane_state,
1407 struct dma_fence *fence)
1409 if (plane_state->fence) {
1410 dma_fence_put(fence);
1414 plane_state->fence = fence;
1416 EXPORT_SYMBOL(drm_atomic_set_fence_for_plane);
1419 * drm_atomic_set_crtc_for_connector - set crtc for connector
1420 * @conn_state: atomic state object for the connector
1421 * @crtc: crtc to use for the connector
1423 * Changing the assigned crtc for a connector requires us to grab the lock and
1424 * state for the new crtc, as needed. This function takes care of all these
1425 * details besides updating the pointer in the state object itself.
1428 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1429 * then the w/w mutex code has detected a deadlock and the entire atomic
1430 * sequence must be restarted. All other errors are fatal.
1433 drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
1434 struct drm_crtc *crtc)
1436 struct drm_crtc_state *crtc_state;
1438 if (conn_state->crtc == crtc)
1441 if (conn_state->crtc) {
1442 crtc_state = drm_atomic_get_new_crtc_state(conn_state->state,
1445 crtc_state->connector_mask &=
1446 ~(1 << drm_connector_index(conn_state->connector));
1448 drm_connector_put(conn_state->connector);
1449 conn_state->crtc = NULL;
1453 crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc);
1454 if (IS_ERR(crtc_state))
1455 return PTR_ERR(crtc_state);
1457 crtc_state->connector_mask |=
1458 1 << drm_connector_index(conn_state->connector);
1460 drm_connector_get(conn_state->connector);
1461 conn_state->crtc = crtc;
1463 DRM_DEBUG_ATOMIC("Link connector state %p to [CRTC:%d:%s]\n",
1464 conn_state, crtc->base.id, crtc->name);
1466 DRM_DEBUG_ATOMIC("Link connector state %p to [NOCRTC]\n",
1472 EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector);
1475 * drm_atomic_add_affected_connectors - add connectors for crtc
1476 * @state: atomic state
1479 * This function walks the current configuration and adds all connectors
1480 * currently using @crtc to the atomic configuration @state. Note that this
1481 * function must acquire the connection mutex. This can potentially cause
1482 * unneeded seralization if the update is just for the planes on one crtc. Hence
1483 * drivers and helpers should only call this when really needed (e.g. when a
1484 * full modeset needs to happen due to some change).
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_connectors(struct drm_atomic_state *state,
1493 struct drm_crtc *crtc)
1495 struct drm_mode_config *config = &state->dev->mode_config;
1496 struct drm_connector *connector;
1497 struct drm_connector_state *conn_state;
1498 struct drm_connector_list_iter conn_iter;
1499 struct drm_crtc_state *crtc_state;
1502 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1503 if (IS_ERR(crtc_state))
1504 return PTR_ERR(crtc_state);
1506 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
1510 DRM_DEBUG_ATOMIC("Adding all current connectors for [CRTC:%d:%s] to %p\n",
1511 crtc->base.id, crtc->name, state);
1514 * Changed connectors are already in @state, so only need to look
1515 * at the connector_mask in crtc_state.
1517 drm_connector_list_iter_begin(state->dev, &conn_iter);
1518 drm_for_each_connector_iter(connector, &conn_iter) {
1519 if (!(crtc_state->connector_mask & (1 << drm_connector_index(connector))))
1522 conn_state = drm_atomic_get_connector_state(state, connector);
1523 if (IS_ERR(conn_state)) {
1524 drm_connector_list_iter_end(&conn_iter);
1525 return PTR_ERR(conn_state);
1528 drm_connector_list_iter_end(&conn_iter);
1532 EXPORT_SYMBOL(drm_atomic_add_affected_connectors);
1535 * drm_atomic_add_affected_planes - add planes for crtc
1536 * @state: atomic state
1539 * This function walks the current configuration and adds all planes
1540 * currently used by @crtc to the atomic configuration @state. This is useful
1541 * when an atomic commit also needs to check all currently enabled plane on
1542 * @crtc, e.g. when changing the mode. It's also useful when re-enabling a CRTC
1543 * to avoid special code to force-enable all planes.
1545 * Since acquiring a plane state will always also acquire the w/w mutex of the
1546 * current CRTC for that plane (if there is any) adding all the plane states for
1547 * a CRTC will not reduce parallism of atomic updates.
1550 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1551 * then the w/w mutex code has detected a deadlock and the entire atomic
1552 * sequence must be restarted. All other errors are fatal.
1555 drm_atomic_add_affected_planes(struct drm_atomic_state *state,
1556 struct drm_crtc *crtc)
1558 struct drm_plane *plane;
1560 WARN_ON(!drm_atomic_get_new_crtc_state(state, crtc));
1562 drm_for_each_plane_mask(plane, state->dev, crtc->state->plane_mask) {
1563 struct drm_plane_state *plane_state =
1564 drm_atomic_get_plane_state(state, plane);
1566 if (IS_ERR(plane_state))
1567 return PTR_ERR(plane_state);
1571 EXPORT_SYMBOL(drm_atomic_add_affected_planes);
1574 * drm_atomic_check_only - check whether a given config would work
1575 * @state: atomic configuration to check
1577 * Note that this function can return -EDEADLK if the driver needed to acquire
1578 * more locks but encountered a deadlock. The caller must then do the usual w/w
1579 * backoff dance and restart. All other errors are fatal.
1582 * 0 on success, negative error code on failure.
1584 int drm_atomic_check_only(struct drm_atomic_state *state)
1586 struct drm_device *dev = state->dev;
1587 struct drm_mode_config *config = &dev->mode_config;
1588 struct drm_plane *plane;
1589 struct drm_plane_state *plane_state;
1590 struct drm_crtc *crtc;
1591 struct drm_crtc_state *crtc_state;
1594 DRM_DEBUG_ATOMIC("checking %p\n", state);
1596 for_each_new_plane_in_state(state, plane, plane_state, i) {
1597 ret = drm_atomic_plane_check(plane, plane_state);
1599 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic core check failed\n",
1600 plane->base.id, plane->name);
1605 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
1606 ret = drm_atomic_crtc_check(crtc, crtc_state);
1608 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic core check failed\n",
1609 crtc->base.id, crtc->name);
1614 if (config->funcs->atomic_check)
1615 ret = config->funcs->atomic_check(state->dev, state);
1620 if (!state->allow_modeset) {
1621 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
1622 if (drm_atomic_crtc_needs_modeset(crtc_state)) {
1623 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] requires full modeset\n",
1624 crtc->base.id, crtc->name);
1632 EXPORT_SYMBOL(drm_atomic_check_only);
1635 * drm_atomic_commit - commit configuration atomically
1636 * @state: atomic configuration to check
1638 * Note that this function can return -EDEADLK if the driver needed to acquire
1639 * more locks but encountered a deadlock. The caller must then do the usual w/w
1640 * backoff dance and restart. All other errors are fatal.
1642 * This function will take its own reference on @state.
1643 * Callers should always release their reference with drm_atomic_state_put().
1646 * 0 on success, negative error code on failure.
1648 int drm_atomic_commit(struct drm_atomic_state *state)
1650 struct drm_mode_config *config = &state->dev->mode_config;
1653 ret = drm_atomic_check_only(state);
1657 DRM_DEBUG_ATOMIC("committing %p\n", state);
1659 return config->funcs->atomic_commit(state->dev, state, false);
1661 EXPORT_SYMBOL(drm_atomic_commit);
1664 * drm_atomic_nonblocking_commit - atomic nonblocking commit
1665 * @state: atomic configuration to check
1667 * Note that this function can return -EDEADLK if the driver needed to acquire
1668 * more locks but encountered a deadlock. The caller must then do the usual w/w
1669 * backoff dance and restart. All other errors are fatal.
1671 * This function will take its own reference on @state.
1672 * Callers should always release their reference with drm_atomic_state_put().
1675 * 0 on success, negative error code on failure.
1677 int drm_atomic_nonblocking_commit(struct drm_atomic_state *state)
1679 struct drm_mode_config *config = &state->dev->mode_config;
1682 ret = drm_atomic_check_only(state);
1686 DRM_DEBUG_ATOMIC("committing %p nonblocking\n", state);
1688 return config->funcs->atomic_commit(state->dev, state, true);
1690 EXPORT_SYMBOL(drm_atomic_nonblocking_commit);
1692 static void drm_atomic_print_state(const struct drm_atomic_state *state)
1694 struct drm_printer p = drm_info_printer(state->dev->dev);
1695 struct drm_plane *plane;
1696 struct drm_plane_state *plane_state;
1697 struct drm_crtc *crtc;
1698 struct drm_crtc_state *crtc_state;
1699 struct drm_connector *connector;
1700 struct drm_connector_state *connector_state;
1703 DRM_DEBUG_ATOMIC("checking %p\n", state);
1705 for_each_new_plane_in_state(state, plane, plane_state, i)
1706 drm_atomic_plane_print_state(&p, plane_state);
1708 for_each_new_crtc_in_state(state, crtc, crtc_state, i)
1709 drm_atomic_crtc_print_state(&p, crtc_state);
1711 for_each_new_connector_in_state(state, connector, connector_state, i)
1712 drm_atomic_connector_print_state(&p, connector_state);
1715 static void __drm_state_dump(struct drm_device *dev, struct drm_printer *p,
1718 struct drm_mode_config *config = &dev->mode_config;
1719 struct drm_plane *plane;
1720 struct drm_crtc *crtc;
1721 struct drm_connector *connector;
1722 struct drm_connector_list_iter conn_iter;
1724 if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
1727 list_for_each_entry(plane, &config->plane_list, head) {
1729 drm_modeset_lock(&plane->mutex, NULL);
1730 drm_atomic_plane_print_state(p, plane->state);
1732 drm_modeset_unlock(&plane->mutex);
1735 list_for_each_entry(crtc, &config->crtc_list, head) {
1737 drm_modeset_lock(&crtc->mutex, NULL);
1738 drm_atomic_crtc_print_state(p, crtc->state);
1740 drm_modeset_unlock(&crtc->mutex);
1743 drm_connector_list_iter_begin(dev, &conn_iter);
1745 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
1746 drm_for_each_connector_iter(connector, &conn_iter)
1747 drm_atomic_connector_print_state(p, connector->state);
1749 drm_modeset_unlock(&dev->mode_config.connection_mutex);
1750 drm_connector_list_iter_end(&conn_iter);
1754 * drm_state_dump - dump entire device atomic state
1755 * @dev: the drm device
1756 * @p: where to print the state to
1758 * Just for debugging. Drivers might want an option to dump state
1759 * to dmesg in case of error irq's. (Hint, you probably want to
1762 * The caller must drm_modeset_lock_all(), or if this is called
1763 * from error irq handler, it should not be enabled by default.
1764 * (Ie. if you are debugging errors you might not care that this
1765 * is racey. But calling this without all modeset locks held is
1766 * not inherently safe.)
1768 void drm_state_dump(struct drm_device *dev, struct drm_printer *p)
1770 __drm_state_dump(dev, p, false);
1772 EXPORT_SYMBOL(drm_state_dump);
1774 #ifdef CONFIG_DEBUG_FS
1775 static int drm_state_info(struct seq_file *m, void *data)
1777 struct drm_info_node *node = (struct drm_info_node *) m->private;
1778 struct drm_device *dev = node->minor->dev;
1779 struct drm_printer p = drm_seq_file_printer(m);
1781 __drm_state_dump(dev, &p, true);
1786 /* any use in debugfs files to dump individual planes/crtc/etc? */
1787 static const struct drm_info_list drm_atomic_debugfs_list[] = {
1788 {"state", drm_state_info, 0},
1791 int drm_atomic_debugfs_init(struct drm_minor *minor)
1793 return drm_debugfs_create_files(drm_atomic_debugfs_list,
1794 ARRAY_SIZE(drm_atomic_debugfs_list),
1795 minor->debugfs_root, minor);
1800 * The big monster ioctl
1803 static struct drm_pending_vblank_event *create_vblank_event(
1804 struct drm_crtc *crtc, uint64_t user_data)
1806 struct drm_pending_vblank_event *e = NULL;
1808 e = kzalloc(sizeof *e, GFP_KERNEL);
1812 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
1813 e->event.base.length = sizeof(e->event);
1814 e->event.vbl.crtc_id = crtc->base.id;
1815 e->event.vbl.user_data = user_data;
1820 int drm_atomic_connector_commit_dpms(struct drm_atomic_state *state,
1821 struct drm_connector *connector,
1824 struct drm_connector *tmp_connector;
1825 struct drm_connector_state *new_conn_state;
1826 struct drm_crtc *crtc;
1827 struct drm_crtc_state *crtc_state;
1828 int i, ret, old_mode = connector->dpms;
1829 bool active = false;
1831 ret = drm_modeset_lock(&state->dev->mode_config.connection_mutex,
1832 state->acquire_ctx);
1836 if (mode != DRM_MODE_DPMS_ON)
1837 mode = DRM_MODE_DPMS_OFF;
1838 connector->dpms = mode;
1840 crtc = connector->state->crtc;
1843 ret = drm_atomic_add_affected_connectors(state, crtc);
1847 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1848 if (IS_ERR(crtc_state)) {
1849 ret = PTR_ERR(crtc_state);
1853 for_each_new_connector_in_state(state, tmp_connector, new_conn_state, i) {
1854 if (new_conn_state->crtc != crtc)
1856 if (tmp_connector->dpms == DRM_MODE_DPMS_ON) {
1862 crtc_state->active = active;
1863 ret = drm_atomic_commit(state);
1866 connector->dpms = old_mode;
1870 int drm_atomic_set_property(struct drm_atomic_state *state,
1871 struct drm_mode_object *obj,
1872 struct drm_property *prop,
1873 uint64_t prop_value)
1875 struct drm_mode_object *ref;
1878 if (!drm_property_change_valid_get(prop, prop_value, &ref))
1881 switch (obj->type) {
1882 case DRM_MODE_OBJECT_CONNECTOR: {
1883 struct drm_connector *connector = obj_to_connector(obj);
1884 struct drm_connector_state *connector_state;
1886 connector_state = drm_atomic_get_connector_state(state, connector);
1887 if (IS_ERR(connector_state)) {
1888 ret = PTR_ERR(connector_state);
1892 ret = drm_atomic_connector_set_property(connector,
1893 connector_state, prop, prop_value);
1896 case DRM_MODE_OBJECT_CRTC: {
1897 struct drm_crtc *crtc = obj_to_crtc(obj);
1898 struct drm_crtc_state *crtc_state;
1900 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1901 if (IS_ERR(crtc_state)) {
1902 ret = PTR_ERR(crtc_state);
1906 ret = drm_atomic_crtc_set_property(crtc,
1907 crtc_state, prop, prop_value);
1910 case DRM_MODE_OBJECT_PLANE: {
1911 struct drm_plane *plane = obj_to_plane(obj);
1912 struct drm_plane_state *plane_state;
1914 plane_state = drm_atomic_get_plane_state(state, plane);
1915 if (IS_ERR(plane_state)) {
1916 ret = PTR_ERR(plane_state);
1920 ret = drm_atomic_plane_set_property(plane,
1921 plane_state, prop, prop_value);
1929 drm_property_change_valid_put(prop, ref);
1934 * drm_atomic_clean_old_fb -- Unset old_fb pointers and set plane->fb pointers.
1936 * @dev: drm device to check.
1937 * @plane_mask: plane mask for planes that were updated.
1938 * @ret: return value, can be -EDEADLK for a retry.
1940 * Before doing an update &drm_plane.old_fb is set to &drm_plane.fb, but before
1941 * dropping the locks old_fb needs to be set to NULL and plane->fb updated. This
1942 * is a common operation for each atomic update, so this call is split off as a
1945 void drm_atomic_clean_old_fb(struct drm_device *dev,
1946 unsigned plane_mask,
1949 struct drm_plane *plane;
1951 /* if succeeded, fixup legacy plane crtc/fb ptrs before dropping
1952 * locks (ie. while it is still safe to deref plane->state). We
1953 * need to do this here because the driver entry points cannot
1954 * distinguish between legacy and atomic ioctls.
1956 drm_for_each_plane_mask(plane, dev, plane_mask) {
1958 struct drm_framebuffer *new_fb = plane->state->fb;
1960 drm_framebuffer_get(new_fb);
1962 plane->crtc = plane->state->crtc;
1965 drm_framebuffer_put(plane->old_fb);
1967 plane->old_fb = NULL;
1970 EXPORT_SYMBOL(drm_atomic_clean_old_fb);
1973 * DOC: explicit fencing properties
1975 * Explicit fencing allows userspace to control the buffer synchronization
1976 * between devices. A Fence or a group of fences are transfered to/from
1977 * userspace using Sync File fds and there are two DRM properties for that.
1978 * IN_FENCE_FD on each DRM Plane to send fences to the kernel and
1979 * OUT_FENCE_PTR on each DRM CRTC to receive fences from the kernel.
1981 * As a contrast, with implicit fencing the kernel keeps track of any
1982 * ongoing rendering, and automatically ensures that the atomic update waits
1983 * for any pending rendering to complete. For shared buffers represented with
1984 * a &struct dma_buf this is tracked in &struct reservation_object.
1985 * Implicit syncing is how Linux traditionally worked (e.g. DRI2/3 on X.org),
1986 * whereas explicit fencing is what Android wants.
1989 * Use this property to pass a fence that DRM should wait on before
1990 * proceeding with the Atomic Commit request and show the framebuffer for
1991 * the plane on the screen. The fence can be either a normal fence or a
1992 * merged one, the sync_file framework will handle both cases and use a
1993 * fence_array if a merged fence is received. Passing -1 here means no
1994 * fences to wait on.
1996 * If the Atomic Commit request has the DRM_MODE_ATOMIC_TEST_ONLY flag
1997 * it will only check if the Sync File is a valid one.
1999 * On the driver side the fence is stored on the @fence parameter of
2000 * &struct drm_plane_state. Drivers which also support implicit fencing
2001 * should set the implicit fence using drm_atomic_set_fence_for_plane(),
2002 * to make sure there's consistent behaviour between drivers in precedence
2003 * of implicit vs. explicit fencing.
2006 * Use this property to pass a file descriptor pointer to DRM. Once the
2007 * Atomic Commit request call returns OUT_FENCE_PTR will be filled with
2008 * the file descriptor number of a Sync File. This Sync File contains the
2009 * CRTC fence that will be signaled when all framebuffers present on the
2010 * Atomic Commit * request for that given CRTC are scanned out on the
2013 * The Atomic Commit request fails if a invalid pointer is passed. If the
2014 * Atomic Commit request fails for any other reason the out fence fd
2015 * returned will be -1. On a Atomic Commit with the
2016 * DRM_MODE_ATOMIC_TEST_ONLY flag the out fence will also be set to -1.
2018 * Note that out-fences don't have a special interface to drivers and are
2019 * internally represented by a &struct drm_pending_vblank_event in struct
2020 * &drm_crtc_state, which is also used by the nonblocking atomic commit
2021 * helpers and for the DRM event handling for existing userspace.
2024 struct drm_out_fence_state {
2025 s32 __user *out_fence_ptr;
2026 struct sync_file *sync_file;
2030 static int setup_out_fence(struct drm_out_fence_state *fence_state,
2031 struct dma_fence *fence)
2033 fence_state->fd = get_unused_fd_flags(O_CLOEXEC);
2034 if (fence_state->fd < 0)
2035 return fence_state->fd;
2037 if (put_user(fence_state->fd, fence_state->out_fence_ptr))
2040 fence_state->sync_file = sync_file_create(fence);
2041 if (!fence_state->sync_file)
2047 static int prepare_crtc_signaling(struct drm_device *dev,
2048 struct drm_atomic_state *state,
2049 struct drm_mode_atomic *arg,
2050 struct drm_file *file_priv,
2051 struct drm_out_fence_state **fence_state,
2052 unsigned int *num_fences)
2054 struct drm_crtc *crtc;
2055 struct drm_crtc_state *crtc_state;
2058 if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)
2061 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
2062 s32 __user *fence_ptr;
2064 fence_ptr = get_out_fence_for_crtc(crtc_state->state, crtc);
2066 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT || fence_ptr) {
2067 struct drm_pending_vblank_event *e;
2069 e = create_vblank_event(crtc, arg->user_data);
2073 crtc_state->event = e;
2076 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
2077 struct drm_pending_vblank_event *e = crtc_state->event;
2082 ret = drm_event_reserve_init(dev, file_priv, &e->base,
2086 crtc_state->event = NULL;
2092 struct dma_fence *fence;
2093 struct drm_out_fence_state *f;
2095 f = krealloc(*fence_state, sizeof(**fence_state) *
2096 (*num_fences + 1), GFP_KERNEL);
2100 memset(&f[*num_fences], 0, sizeof(*f));
2102 f[*num_fences].out_fence_ptr = fence_ptr;
2105 fence = drm_crtc_create_fence(crtc);
2109 ret = setup_out_fence(&f[(*num_fences)++], fence);
2111 dma_fence_put(fence);
2115 crtc_state->event->base.fence = fence;
2122 * Having this flag means user mode pends on event which will never
2123 * reach due to lack of at least one CRTC for signaling
2125 if (c == 0 && (arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
2131 static void complete_crtc_signaling(struct drm_device *dev,
2132 struct drm_atomic_state *state,
2133 struct drm_out_fence_state *fence_state,
2134 unsigned int num_fences,
2137 struct drm_crtc *crtc;
2138 struct drm_crtc_state *crtc_state;
2142 for (i = 0; i < num_fences; i++)
2143 fd_install(fence_state[i].fd,
2144 fence_state[i].sync_file->file);
2150 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
2151 struct drm_pending_vblank_event *event = crtc_state->event;
2153 * Free the allocated event. drm_atomic_helper_setup_commit
2154 * can allocate an event too, so only free it if it's ours
2155 * to prevent a double free in drm_atomic_state_clear.
2157 if (event && (event->base.fence || event->base.file_priv)) {
2158 drm_event_cancel_free(dev, &event->base);
2159 crtc_state->event = NULL;
2166 for (i = 0; i < num_fences; i++) {
2167 if (fence_state[i].sync_file)
2168 fput(fence_state[i].sync_file->file);
2169 if (fence_state[i].fd >= 0)
2170 put_unused_fd(fence_state[i].fd);
2172 /* If this fails log error to the user */
2173 if (fence_state[i].out_fence_ptr &&
2174 put_user(-1, fence_state[i].out_fence_ptr))
2175 DRM_DEBUG_ATOMIC("Couldn't clear out_fence_ptr\n");
2181 int drm_mode_atomic_ioctl(struct drm_device *dev,
2182 void *data, struct drm_file *file_priv)
2184 struct drm_mode_atomic *arg = data;
2185 uint32_t __user *objs_ptr = (uint32_t __user *)(unsigned long)(arg->objs_ptr);
2186 uint32_t __user *count_props_ptr = (uint32_t __user *)(unsigned long)(arg->count_props_ptr);
2187 uint32_t __user *props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
2188 uint64_t __user *prop_values_ptr = (uint64_t __user *)(unsigned long)(arg->prop_values_ptr);
2189 unsigned int copied_objs, copied_props;
2190 struct drm_atomic_state *state;
2191 struct drm_modeset_acquire_ctx ctx;
2192 struct drm_plane *plane;
2193 struct drm_out_fence_state *fence_state;
2194 unsigned plane_mask;
2196 unsigned int i, j, num_fences;
2198 /* disallow for drivers not supporting atomic: */
2199 if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
2202 /* disallow for userspace that has not enabled atomic cap (even
2203 * though this may be a bit overkill, since legacy userspace
2204 * wouldn't know how to call this ioctl)
2206 if (!file_priv->atomic)
2209 if (arg->flags & ~DRM_MODE_ATOMIC_FLAGS)
2215 if ((arg->flags & DRM_MODE_PAGE_FLIP_ASYNC) &&
2216 !dev->mode_config.async_page_flip)
2219 /* can't test and expect an event at the same time. */
2220 if ((arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) &&
2221 (arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
2224 drm_modeset_acquire_init(&ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE);
2226 state = drm_atomic_state_alloc(dev);
2230 state->acquire_ctx = &ctx;
2231 state->allow_modeset = !!(arg->flags & DRM_MODE_ATOMIC_ALLOW_MODESET);
2240 for (i = 0; i < arg->count_objs; i++) {
2241 uint32_t obj_id, count_props;
2242 struct drm_mode_object *obj;
2244 if (get_user(obj_id, objs_ptr + copied_objs)) {
2249 obj = drm_mode_object_find(dev, file_priv, obj_id, DRM_MODE_OBJECT_ANY);
2255 if (!obj->properties) {
2256 drm_mode_object_put(obj);
2261 if (get_user(count_props, count_props_ptr + copied_objs)) {
2262 drm_mode_object_put(obj);
2269 for (j = 0; j < count_props; j++) {
2271 uint64_t prop_value;
2272 struct drm_property *prop;
2274 if (get_user(prop_id, props_ptr + copied_props)) {
2275 drm_mode_object_put(obj);
2280 prop = drm_mode_obj_find_prop_id(obj, prop_id);
2282 drm_mode_object_put(obj);
2287 if (copy_from_user(&prop_value,
2288 prop_values_ptr + copied_props,
2289 sizeof(prop_value))) {
2290 drm_mode_object_put(obj);
2295 ret = drm_atomic_set_property(state, obj, prop,
2298 drm_mode_object_put(obj);
2305 if (obj->type == DRM_MODE_OBJECT_PLANE && count_props &&
2306 !(arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)) {
2307 plane = obj_to_plane(obj);
2308 plane_mask |= (1 << drm_plane_index(plane));
2309 plane->old_fb = plane->fb;
2311 drm_mode_object_put(obj);
2314 ret = prepare_crtc_signaling(dev, state, arg, file_priv, &fence_state,
2319 if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) {
2320 ret = drm_atomic_check_only(state);
2321 } else if (arg->flags & DRM_MODE_ATOMIC_NONBLOCK) {
2322 ret = drm_atomic_nonblocking_commit(state);
2324 if (unlikely(drm_debug & DRM_UT_STATE))
2325 drm_atomic_print_state(state);
2327 ret = drm_atomic_commit(state);
2331 drm_atomic_clean_old_fb(dev, plane_mask, ret);
2333 complete_crtc_signaling(dev, state, fence_state, num_fences, !ret);
2335 if (ret == -EDEADLK) {
2336 drm_atomic_state_clear(state);
2337 ret = drm_modeset_backoff(&ctx);
2342 drm_atomic_state_put(state);
2344 drm_modeset_drop_locks(&ctx);
2345 drm_modeset_acquire_fini(&ctx);