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_print.h>
33 #include <drm/drm_writeback.h>
34 #include <linux/sync_file.h>
36 #include "drm_crtc_internal.h"
37 #include "drm_internal.h"
39 void __drm_crtc_commit_free(struct kref *kref)
41 struct drm_crtc_commit *commit =
42 container_of(kref, struct drm_crtc_commit, ref);
46 EXPORT_SYMBOL(__drm_crtc_commit_free);
49 * drm_atomic_state_default_release -
50 * release memory initialized by drm_atomic_state_init
51 * @state: atomic state
53 * Free all the memory allocated by drm_atomic_state_init.
54 * This should only be used by drivers which are still subclassing
55 * &drm_atomic_state and haven't switched to &drm_private_state yet.
57 void drm_atomic_state_default_release(struct drm_atomic_state *state)
59 kfree(state->connectors);
62 kfree(state->private_objs);
64 EXPORT_SYMBOL(drm_atomic_state_default_release);
67 * drm_atomic_state_init - init new atomic state
69 * @state: atomic state
71 * Default implementation for filling in a new atomic state.
72 * This should only be used by drivers which are still subclassing
73 * &drm_atomic_state and haven't switched to &drm_private_state yet.
76 drm_atomic_state_init(struct drm_device *dev, struct drm_atomic_state *state)
78 kref_init(&state->ref);
80 /* TODO legacy paths should maybe do a better job about
81 * setting this appropriately?
83 state->allow_modeset = true;
85 state->crtcs = kcalloc(dev->mode_config.num_crtc,
86 sizeof(*state->crtcs), GFP_KERNEL);
89 state->planes = kcalloc(dev->mode_config.num_total_plane,
90 sizeof(*state->planes), GFP_KERNEL);
95 * Because drm_atomic_state can be committed asynchronously we need our
96 * own reference and cannot rely on the on implied by drm_file in the
102 DRM_DEBUG_ATOMIC("Allocated atomic state %p\n", state);
106 drm_atomic_state_default_release(state);
109 EXPORT_SYMBOL(drm_atomic_state_init);
112 * drm_atomic_state_alloc - allocate atomic state
115 * This allocates an empty atomic state to track updates.
117 struct drm_atomic_state *
118 drm_atomic_state_alloc(struct drm_device *dev)
120 struct drm_mode_config *config = &dev->mode_config;
122 if (!config->funcs->atomic_state_alloc) {
123 struct drm_atomic_state *state;
125 state = kzalloc(sizeof(*state), GFP_KERNEL);
128 if (drm_atomic_state_init(dev, state) < 0) {
135 return config->funcs->atomic_state_alloc(dev);
137 EXPORT_SYMBOL(drm_atomic_state_alloc);
140 * drm_atomic_state_default_clear - clear base atomic state
141 * @state: atomic state
143 * Default implementation for clearing atomic state.
144 * This should only be used by drivers which are still subclassing
145 * &drm_atomic_state and haven't switched to &drm_private_state yet.
147 void drm_atomic_state_default_clear(struct drm_atomic_state *state)
149 struct drm_device *dev = state->dev;
150 struct drm_mode_config *config = &dev->mode_config;
153 DRM_DEBUG_ATOMIC("Clearing atomic state %p\n", state);
155 for (i = 0; i < state->num_connector; i++) {
156 struct drm_connector *connector = state->connectors[i].ptr;
161 connector->funcs->atomic_destroy_state(connector,
162 state->connectors[i].state);
163 state->connectors[i].ptr = NULL;
164 state->connectors[i].state = NULL;
165 state->connectors[i].old_state = NULL;
166 state->connectors[i].new_state = NULL;
167 drm_connector_put(connector);
170 for (i = 0; i < config->num_crtc; i++) {
171 struct drm_crtc *crtc = state->crtcs[i].ptr;
176 crtc->funcs->atomic_destroy_state(crtc,
177 state->crtcs[i].state);
179 state->crtcs[i].ptr = NULL;
180 state->crtcs[i].state = NULL;
181 state->crtcs[i].old_state = NULL;
182 state->crtcs[i].new_state = NULL;
184 if (state->crtcs[i].commit) {
185 drm_crtc_commit_put(state->crtcs[i].commit);
186 state->crtcs[i].commit = NULL;
190 for (i = 0; i < config->num_total_plane; i++) {
191 struct drm_plane *plane = state->planes[i].ptr;
196 plane->funcs->atomic_destroy_state(plane,
197 state->planes[i].state);
198 state->planes[i].ptr = NULL;
199 state->planes[i].state = NULL;
200 state->planes[i].old_state = NULL;
201 state->planes[i].new_state = NULL;
204 for (i = 0; i < state->num_private_objs; i++) {
205 struct drm_private_obj *obj = state->private_objs[i].ptr;
207 obj->funcs->atomic_destroy_state(obj,
208 state->private_objs[i].state);
209 state->private_objs[i].ptr = NULL;
210 state->private_objs[i].state = NULL;
211 state->private_objs[i].old_state = NULL;
212 state->private_objs[i].new_state = NULL;
214 state->num_private_objs = 0;
216 if (state->fake_commit) {
217 drm_crtc_commit_put(state->fake_commit);
218 state->fake_commit = NULL;
221 EXPORT_SYMBOL(drm_atomic_state_default_clear);
224 * drm_atomic_state_clear - clear state object
225 * @state: atomic state
227 * When the w/w mutex algorithm detects a deadlock we need to back off and drop
228 * all locks. So someone else could sneak in and change the current modeset
229 * configuration. Which means that all the state assembled in @state is no
230 * longer an atomic update to the current state, but to some arbitrary earlier
231 * state. Which could break assumptions the driver's
232 * &drm_mode_config_funcs.atomic_check likely relies on.
234 * Hence we must clear all cached state and completely start over, using this
237 void drm_atomic_state_clear(struct drm_atomic_state *state)
239 struct drm_device *dev = state->dev;
240 struct drm_mode_config *config = &dev->mode_config;
242 if (config->funcs->atomic_state_clear)
243 config->funcs->atomic_state_clear(state);
245 drm_atomic_state_default_clear(state);
247 EXPORT_SYMBOL(drm_atomic_state_clear);
250 * __drm_atomic_state_free - free all memory for an atomic state
251 * @ref: This atomic state to deallocate
253 * This frees all memory associated with an atomic state, including all the
254 * per-object state for planes, crtcs and connectors.
256 void __drm_atomic_state_free(struct kref *ref)
258 struct drm_atomic_state *state = container_of(ref, typeof(*state), ref);
259 struct drm_device *dev = state->dev;
260 struct drm_mode_config *config = &dev->mode_config;
262 drm_atomic_state_clear(state);
264 DRM_DEBUG_ATOMIC("Freeing atomic state %p\n", state);
266 if (config->funcs->atomic_state_free) {
267 config->funcs->atomic_state_free(state);
269 drm_atomic_state_default_release(state);
275 EXPORT_SYMBOL(__drm_atomic_state_free);
278 * drm_atomic_get_crtc_state - get crtc state
279 * @state: global atomic state object
280 * @crtc: crtc to get state object for
282 * This function returns the crtc state for the given crtc, allocating it if
283 * needed. It will also grab the relevant crtc lock to make sure that the state
288 * Either the allocated state or the error code encoded into the pointer. When
289 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
290 * entire atomic sequence must be restarted. All other errors are fatal.
292 struct drm_crtc_state *
293 drm_atomic_get_crtc_state(struct drm_atomic_state *state,
294 struct drm_crtc *crtc)
296 int ret, index = drm_crtc_index(crtc);
297 struct drm_crtc_state *crtc_state;
299 WARN_ON(!state->acquire_ctx);
301 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
305 ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx);
309 crtc_state = crtc->funcs->atomic_duplicate_state(crtc);
311 return ERR_PTR(-ENOMEM);
313 state->crtcs[index].state = crtc_state;
314 state->crtcs[index].old_state = crtc->state;
315 state->crtcs[index].new_state = crtc_state;
316 state->crtcs[index].ptr = crtc;
317 crtc_state->state = state;
319 DRM_DEBUG_ATOMIC("Added [CRTC:%d:%s] %p state to %p\n",
320 crtc->base.id, crtc->name, crtc_state, state);
324 EXPORT_SYMBOL(drm_atomic_get_crtc_state);
326 static void set_out_fence_for_crtc(struct drm_atomic_state *state,
327 struct drm_crtc *crtc, s32 __user *fence_ptr)
329 state->crtcs[drm_crtc_index(crtc)].out_fence_ptr = fence_ptr;
332 static s32 __user *get_out_fence_for_crtc(struct drm_atomic_state *state,
333 struct drm_crtc *crtc)
335 s32 __user *fence_ptr;
337 fence_ptr = state->crtcs[drm_crtc_index(crtc)].out_fence_ptr;
338 state->crtcs[drm_crtc_index(crtc)].out_fence_ptr = NULL;
343 static int set_out_fence_for_connector(struct drm_atomic_state *state,
344 struct drm_connector *connector,
345 s32 __user *fence_ptr)
347 unsigned int index = drm_connector_index(connector);
352 if (put_user(-1, fence_ptr))
355 state->connectors[index].out_fence_ptr = fence_ptr;
360 static s32 __user *get_out_fence_for_connector(struct drm_atomic_state *state,
361 struct drm_connector *connector)
363 unsigned int index = drm_connector_index(connector);
364 s32 __user *fence_ptr;
366 fence_ptr = state->connectors[index].out_fence_ptr;
367 state->connectors[index].out_fence_ptr = NULL;
373 * drm_atomic_set_mode_for_crtc - set mode for CRTC
374 * @state: the CRTC whose incoming state to update
375 * @mode: kernel-internal mode to use for the CRTC, or NULL to disable
377 * Set a mode (originating from the kernel) on the desired CRTC state and update
378 * the enable property.
381 * Zero on success, error code on failure. Cannot return -EDEADLK.
383 int drm_atomic_set_mode_for_crtc(struct drm_crtc_state *state,
384 const struct drm_display_mode *mode)
386 struct drm_crtc *crtc = state->crtc;
387 struct drm_mode_modeinfo umode;
389 /* Early return for no change. */
390 if (mode && memcmp(&state->mode, mode, sizeof(*mode)) == 0)
393 drm_property_blob_put(state->mode_blob);
394 state->mode_blob = NULL;
397 drm_mode_convert_to_umode(&umode, mode);
399 drm_property_create_blob(state->crtc->dev,
402 if (IS_ERR(state->mode_blob))
403 return PTR_ERR(state->mode_blob);
405 drm_mode_copy(&state->mode, mode);
406 state->enable = true;
407 DRM_DEBUG_ATOMIC("Set [MODE:%s] for [CRTC:%d:%s] state %p\n",
408 mode->name, crtc->base.id, crtc->name, state);
410 memset(&state->mode, 0, sizeof(state->mode));
411 state->enable = false;
412 DRM_DEBUG_ATOMIC("Set [NOMODE] for [CRTC:%d:%s] state %p\n",
413 crtc->base.id, crtc->name, state);
418 EXPORT_SYMBOL(drm_atomic_set_mode_for_crtc);
421 * drm_atomic_set_mode_prop_for_crtc - set mode for CRTC
422 * @state: the CRTC whose incoming state to update
423 * @blob: pointer to blob property to use for mode
425 * Set a mode (originating from a blob property) on the desired CRTC state.
426 * This function will take a reference on the blob property for the CRTC state,
427 * and release the reference held on the state's existing mode property, if any
431 * Zero on success, error code on failure. Cannot return -EDEADLK.
433 int drm_atomic_set_mode_prop_for_crtc(struct drm_crtc_state *state,
434 struct drm_property_blob *blob)
436 struct drm_crtc *crtc = state->crtc;
438 if (blob == state->mode_blob)
441 drm_property_blob_put(state->mode_blob);
442 state->mode_blob = NULL;
444 memset(&state->mode, 0, sizeof(state->mode));
449 if (blob->length != sizeof(struct drm_mode_modeinfo)) {
450 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] bad mode blob length: %zu\n",
451 crtc->base.id, crtc->name,
456 ret = drm_mode_convert_umode(crtc->dev,
457 &state->mode, blob->data);
459 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] invalid mode (ret=%d, status=%s):\n",
460 crtc->base.id, crtc->name,
461 ret, drm_get_mode_status_name(state->mode.status));
462 drm_mode_debug_printmodeline(&state->mode);
466 state->mode_blob = drm_property_blob_get(blob);
467 state->enable = true;
468 DRM_DEBUG_ATOMIC("Set [MODE:%s] for [CRTC:%d:%s] state %p\n",
469 state->mode.name, crtc->base.id, crtc->name,
472 state->enable = false;
473 DRM_DEBUG_ATOMIC("Set [NOMODE] for [CRTC:%d:%s] state %p\n",
474 crtc->base.id, crtc->name, state);
479 EXPORT_SYMBOL(drm_atomic_set_mode_prop_for_crtc);
482 * drm_atomic_replace_property_blob_from_id - lookup the new blob and replace the old one with it
484 * @blob: a pointer to the member blob to be replaced
485 * @blob_id: ID of the new blob
486 * @expected_size: total expected size of the blob data (in bytes)
487 * @expected_elem_size: expected element size of the blob data (in bytes)
488 * @replaced: did the blob get replaced?
490 * Replace @blob with another blob with the ID @blob_id. If @blob_id is zero
491 * @blob becomes NULL.
493 * If @expected_size is positive the new blob length is expected to be equal
494 * to @expected_size bytes. If @expected_elem_size is positive the new blob
495 * length is expected to be a multiple of @expected_elem_size bytes. Otherwise
496 * an error is returned.
498 * @replaced will indicate to the caller whether the blob was replaced or not.
499 * If the old and new blobs were in fact the same blob @replaced will be false
500 * otherwise it will be true.
503 * Zero on success, error code on failure.
506 drm_atomic_replace_property_blob_from_id(struct drm_device *dev,
507 struct drm_property_blob **blob,
509 ssize_t expected_size,
510 ssize_t expected_elem_size,
513 struct drm_property_blob *new_blob = NULL;
516 new_blob = drm_property_lookup_blob(dev, blob_id);
517 if (new_blob == NULL)
520 if (expected_size > 0 &&
521 new_blob->length != expected_size) {
522 drm_property_blob_put(new_blob);
525 if (expected_elem_size > 0 &&
526 new_blob->length % expected_elem_size != 0) {
527 drm_property_blob_put(new_blob);
532 *replaced |= drm_property_replace_blob(blob, new_blob);
533 drm_property_blob_put(new_blob);
539 * drm_atomic_crtc_set_property - set property on CRTC
540 * @crtc: the drm CRTC to set a property on
541 * @state: the state object to update with the new property value
542 * @property: the property to set
543 * @val: the new property value
545 * This function handles generic/core properties and calls out to driver's
546 * &drm_crtc_funcs.atomic_set_property for driver properties. To ensure
547 * consistent behavior you must call this function rather than the driver hook
551 * Zero on success, error code on failure
553 int drm_atomic_crtc_set_property(struct drm_crtc *crtc,
554 struct drm_crtc_state *state, struct drm_property *property,
557 struct drm_device *dev = crtc->dev;
558 struct drm_mode_config *config = &dev->mode_config;
559 bool replaced = false;
562 if (property == config->prop_active)
564 else if (property == config->prop_mode_id) {
565 struct drm_property_blob *mode =
566 drm_property_lookup_blob(dev, val);
567 ret = drm_atomic_set_mode_prop_for_crtc(state, mode);
568 drm_property_blob_put(mode);
570 } else if (property == config->degamma_lut_property) {
571 ret = drm_atomic_replace_property_blob_from_id(dev,
574 -1, sizeof(struct drm_color_lut),
576 state->color_mgmt_changed |= replaced;
578 } else if (property == config->ctm_property) {
579 ret = drm_atomic_replace_property_blob_from_id(dev,
582 sizeof(struct drm_color_ctm), -1,
584 state->color_mgmt_changed |= replaced;
586 } else if (property == config->gamma_lut_property) {
587 ret = drm_atomic_replace_property_blob_from_id(dev,
590 -1, sizeof(struct drm_color_lut),
592 state->color_mgmt_changed |= replaced;
594 } else if (property == config->prop_out_fence_ptr) {
595 s32 __user *fence_ptr = u64_to_user_ptr(val);
600 if (put_user(-1, fence_ptr))
603 set_out_fence_for_crtc(state->state, crtc, fence_ptr);
604 } else if (crtc->funcs->atomic_set_property) {
605 return crtc->funcs->atomic_set_property(crtc, state, property, val);
607 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] unknown property [PROP:%d:%s]]\n",
608 crtc->base.id, crtc->name,
609 property->base.id, property->name);
615 EXPORT_SYMBOL(drm_atomic_crtc_set_property);
618 * drm_atomic_crtc_get_property - get property value from CRTC state
619 * @crtc: the drm CRTC to set a property on
620 * @state: the state object to get the property value from
621 * @property: the property to set
622 * @val: return location for the property value
624 * This function handles generic/core properties and calls out to driver's
625 * &drm_crtc_funcs.atomic_get_property for driver properties. To ensure
626 * consistent behavior you must call this function rather than the driver hook
630 * Zero on success, error code on failure
633 drm_atomic_crtc_get_property(struct drm_crtc *crtc,
634 const struct drm_crtc_state *state,
635 struct drm_property *property, uint64_t *val)
637 struct drm_device *dev = crtc->dev;
638 struct drm_mode_config *config = &dev->mode_config;
640 if (property == config->prop_active)
641 *val = state->active;
642 else if (property == config->prop_mode_id)
643 *val = (state->mode_blob) ? state->mode_blob->base.id : 0;
644 else if (property == config->degamma_lut_property)
645 *val = (state->degamma_lut) ? state->degamma_lut->base.id : 0;
646 else if (property == config->ctm_property)
647 *val = (state->ctm) ? state->ctm->base.id : 0;
648 else if (property == config->gamma_lut_property)
649 *val = (state->gamma_lut) ? state->gamma_lut->base.id : 0;
650 else if (property == config->prop_out_fence_ptr)
652 else if (crtc->funcs->atomic_get_property)
653 return crtc->funcs->atomic_get_property(crtc, state, property, val);
661 * drm_atomic_crtc_check - check crtc state
662 * @crtc: crtc to check
663 * @state: crtc state to check
665 * Provides core sanity checks for crtc state.
668 * Zero on success, error code on failure
670 static int drm_atomic_crtc_check(struct drm_crtc *crtc,
671 struct drm_crtc_state *state)
673 /* NOTE: we explicitly don't enforce constraints such as primary
674 * layer covering entire screen, since that is something we want
675 * to allow (on hw that supports it). For hw that does not, it
676 * should be checked in driver's crtc->atomic_check() vfunc.
678 * TODO: Add generic modeset state checks once we support those.
681 if (state->active && !state->enable) {
682 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active without enabled\n",
683 crtc->base.id, crtc->name);
687 /* The state->enable vs. state->mode_blob checks can be WARN_ON,
688 * as this is a kernel-internal detail that userspace should never
689 * be able to trigger. */
690 if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
691 WARN_ON(state->enable && !state->mode_blob)) {
692 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled without mode blob\n",
693 crtc->base.id, crtc->name);
697 if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
698 WARN_ON(!state->enable && state->mode_blob)) {
699 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] disabled with mode blob\n",
700 crtc->base.id, crtc->name);
705 * Reject event generation for when a CRTC is off and stays off.
706 * It wouldn't be hard to implement this, but userspace has a track
707 * record of happily burning through 100% cpu (or worse, crash) when the
708 * display pipe is suspended. To avoid all that fun just reject updates
709 * that ask for events since likely that indicates a bug in the
710 * compositor's drawing loop. This is consistent with the vblank IOCTL
711 * and legacy page_flip IOCTL which also reject service on a disabled
714 if (state->event && !state->active && !crtc->state->active) {
715 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] requesting event but off\n",
716 crtc->base.id, crtc->name);
723 static void drm_atomic_crtc_print_state(struct drm_printer *p,
724 const struct drm_crtc_state *state)
726 struct drm_crtc *crtc = state->crtc;
728 drm_printf(p, "crtc[%u]: %s\n", crtc->base.id, crtc->name);
729 drm_printf(p, "\tenable=%d\n", state->enable);
730 drm_printf(p, "\tactive=%d\n", state->active);
731 drm_printf(p, "\tplanes_changed=%d\n", state->planes_changed);
732 drm_printf(p, "\tmode_changed=%d\n", state->mode_changed);
733 drm_printf(p, "\tactive_changed=%d\n", state->active_changed);
734 drm_printf(p, "\tconnectors_changed=%d\n", state->connectors_changed);
735 drm_printf(p, "\tcolor_mgmt_changed=%d\n", state->color_mgmt_changed);
736 drm_printf(p, "\tplane_mask=%x\n", state->plane_mask);
737 drm_printf(p, "\tconnector_mask=%x\n", state->connector_mask);
738 drm_printf(p, "\tencoder_mask=%x\n", state->encoder_mask);
739 drm_printf(p, "\tmode: " DRM_MODE_FMT "\n", DRM_MODE_ARG(&state->mode));
741 if (crtc->funcs->atomic_print_state)
742 crtc->funcs->atomic_print_state(p, state);
746 * drm_atomic_connector_check - check connector state
747 * @connector: connector to check
748 * @state: connector state to check
750 * Provides core sanity checks for connector state.
753 * Zero on success, error code on failure
755 static int drm_atomic_connector_check(struct drm_connector *connector,
756 struct drm_connector_state *state)
758 struct drm_crtc_state *crtc_state;
759 struct drm_writeback_job *writeback_job = state->writeback_job;
761 if ((connector->connector_type != DRM_MODE_CONNECTOR_WRITEBACK) || !writeback_job)
764 if (writeback_job->fb && !state->crtc) {
765 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] framebuffer without CRTC\n",
766 connector->base.id, connector->name);
771 crtc_state = drm_atomic_get_existing_crtc_state(state->state,
774 if (writeback_job->fb && !crtc_state->active) {
775 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] has framebuffer, but [CRTC:%d] is off\n",
776 connector->base.id, connector->name,
777 state->crtc->base.id);
781 if (writeback_job->out_fence && !writeback_job->fb) {
782 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] requesting out-fence without framebuffer\n",
783 connector->base.id, connector->name);
791 * drm_atomic_get_plane_state - get plane state
792 * @state: global atomic state object
793 * @plane: plane to get state object for
795 * This function returns the plane state for the given plane, allocating it if
796 * needed. It will also grab the relevant plane lock to make sure that the state
801 * Either the allocated state or the error code encoded into the pointer. When
802 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
803 * entire atomic sequence must be restarted. All other errors are fatal.
805 struct drm_plane_state *
806 drm_atomic_get_plane_state(struct drm_atomic_state *state,
807 struct drm_plane *plane)
809 int ret, index = drm_plane_index(plane);
810 struct drm_plane_state *plane_state;
812 WARN_ON(!state->acquire_ctx);
814 /* the legacy pointers should never be set */
816 WARN_ON(plane->old_fb);
817 WARN_ON(plane->crtc);
819 plane_state = drm_atomic_get_existing_plane_state(state, plane);
823 ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
827 plane_state = plane->funcs->atomic_duplicate_state(plane);
829 return ERR_PTR(-ENOMEM);
831 state->planes[index].state = plane_state;
832 state->planes[index].ptr = plane;
833 state->planes[index].old_state = plane->state;
834 state->planes[index].new_state = plane_state;
835 plane_state->state = state;
837 DRM_DEBUG_ATOMIC("Added [PLANE:%d:%s] %p state to %p\n",
838 plane->base.id, plane->name, plane_state, state);
840 if (plane_state->crtc) {
841 struct drm_crtc_state *crtc_state;
843 crtc_state = drm_atomic_get_crtc_state(state,
845 if (IS_ERR(crtc_state))
846 return ERR_CAST(crtc_state);
851 EXPORT_SYMBOL(drm_atomic_get_plane_state);
854 * drm_atomic_plane_set_property - set property on plane
855 * @plane: the drm plane to set a property on
856 * @state: the state object to update with the new property value
857 * @property: the property to set
858 * @val: the new property value
860 * This function handles generic/core properties and calls out to driver's
861 * &drm_plane_funcs.atomic_set_property for driver properties. To ensure
862 * consistent behavior you must call this function rather than the driver hook
866 * Zero on success, error code on failure
868 static int drm_atomic_plane_set_property(struct drm_plane *plane,
869 struct drm_plane_state *state, struct drm_property *property,
872 struct drm_device *dev = plane->dev;
873 struct drm_mode_config *config = &dev->mode_config;
875 if (property == config->prop_fb_id) {
876 struct drm_framebuffer *fb = drm_framebuffer_lookup(dev, NULL, val);
877 drm_atomic_set_fb_for_plane(state, fb);
879 drm_framebuffer_put(fb);
880 } else if (property == config->prop_in_fence_fd) {
884 if (U642I64(val) == -1)
887 state->fence = sync_file_get_fence(val);
891 } else if (property == config->prop_crtc_id) {
892 struct drm_crtc *crtc = drm_crtc_find(dev, NULL, val);
893 return drm_atomic_set_crtc_for_plane(state, crtc);
894 } else if (property == config->prop_crtc_x) {
895 state->crtc_x = U642I64(val);
896 } else if (property == config->prop_crtc_y) {
897 state->crtc_y = U642I64(val);
898 } else if (property == config->prop_crtc_w) {
900 } else if (property == config->prop_crtc_h) {
902 } else if (property == config->prop_src_x) {
904 } else if (property == config->prop_src_y) {
906 } else if (property == config->prop_src_w) {
908 } else if (property == config->prop_src_h) {
910 } else if (property == plane->alpha_property) {
912 } else if (property == plane->rotation_property) {
913 if (!is_power_of_2(val & DRM_MODE_ROTATE_MASK)) {
914 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] bad rotation bitmask: 0x%llx\n",
915 plane->base.id, plane->name, val);
918 state->rotation = val;
919 } else if (property == plane->zpos_property) {
921 } else if (property == plane->color_encoding_property) {
922 state->color_encoding = val;
923 } else if (property == plane->color_range_property) {
924 state->color_range = val;
925 } else if (plane->funcs->atomic_set_property) {
926 return plane->funcs->atomic_set_property(plane, state,
929 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] unknown property [PROP:%d:%s]]\n",
930 plane->base.id, plane->name,
931 property->base.id, property->name);
939 * drm_atomic_plane_get_property - get property value from plane state
940 * @plane: the drm plane to set a property on
941 * @state: the state object to get the property value from
942 * @property: the property to set
943 * @val: return location for the property value
945 * This function handles generic/core properties and calls out to driver's
946 * &drm_plane_funcs.atomic_get_property for driver properties. To ensure
947 * consistent behavior you must call this function rather than the driver hook
951 * Zero on success, error code on failure
954 drm_atomic_plane_get_property(struct drm_plane *plane,
955 const struct drm_plane_state *state,
956 struct drm_property *property, uint64_t *val)
958 struct drm_device *dev = plane->dev;
959 struct drm_mode_config *config = &dev->mode_config;
961 if (property == config->prop_fb_id) {
962 *val = (state->fb) ? state->fb->base.id : 0;
963 } else if (property == config->prop_in_fence_fd) {
965 } else if (property == config->prop_crtc_id) {
966 *val = (state->crtc) ? state->crtc->base.id : 0;
967 } else if (property == config->prop_crtc_x) {
968 *val = I642U64(state->crtc_x);
969 } else if (property == config->prop_crtc_y) {
970 *val = I642U64(state->crtc_y);
971 } else if (property == config->prop_crtc_w) {
972 *val = state->crtc_w;
973 } else if (property == config->prop_crtc_h) {
974 *val = state->crtc_h;
975 } else if (property == config->prop_src_x) {
977 } else if (property == config->prop_src_y) {
979 } else if (property == config->prop_src_w) {
981 } else if (property == config->prop_src_h) {
983 } else if (property == plane->alpha_property) {
985 } else if (property == plane->rotation_property) {
986 *val = state->rotation;
987 } else if (property == plane->zpos_property) {
989 } else if (property == plane->color_encoding_property) {
990 *val = state->color_encoding;
991 } else if (property == plane->color_range_property) {
992 *val = state->color_range;
993 } else if (plane->funcs->atomic_get_property) {
994 return plane->funcs->atomic_get_property(plane, state, property, val);
1003 plane_switching_crtc(struct drm_atomic_state *state,
1004 struct drm_plane *plane,
1005 struct drm_plane_state *plane_state)
1007 if (!plane->state->crtc || !plane_state->crtc)
1010 if (plane->state->crtc == plane_state->crtc)
1013 /* This could be refined, but currently there's no helper or driver code
1014 * to implement direct switching of active planes nor userspace to take
1015 * advantage of more direct plane switching without the intermediate
1022 * drm_atomic_plane_check - check plane state
1023 * @plane: plane to check
1024 * @state: plane state to check
1026 * Provides core sanity checks for plane state.
1029 * Zero on success, error code on failure
1031 static int drm_atomic_plane_check(struct drm_plane *plane,
1032 struct drm_plane_state *state)
1034 unsigned int fb_width, fb_height;
1037 /* either *both* CRTC and FB must be set, or neither */
1038 if (state->crtc && !state->fb) {
1039 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] CRTC set but no FB\n",
1040 plane->base.id, plane->name);
1042 } else if (state->fb && !state->crtc) {
1043 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] FB set but no CRTC\n",
1044 plane->base.id, plane->name);
1048 /* if disabled, we don't care about the rest of the state: */
1052 /* Check whether this plane is usable on this CRTC */
1053 if (!(plane->possible_crtcs & drm_crtc_mask(state->crtc))) {
1054 DRM_DEBUG_ATOMIC("Invalid [CRTC:%d:%s] for [PLANE:%d:%s]\n",
1055 state->crtc->base.id, state->crtc->name,
1056 plane->base.id, plane->name);
1060 /* Check whether this plane supports the fb pixel format. */
1061 ret = drm_plane_check_pixel_format(plane, state->fb->format->format,
1062 state->fb->modifier);
1064 struct drm_format_name_buf format_name;
1065 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] invalid pixel format %s, modifier 0x%llx\n",
1066 plane->base.id, plane->name,
1067 drm_get_format_name(state->fb->format->format,
1069 state->fb->modifier);
1073 /* Give drivers some help against integer overflows */
1074 if (state->crtc_w > INT_MAX ||
1075 state->crtc_x > INT_MAX - (int32_t) state->crtc_w ||
1076 state->crtc_h > INT_MAX ||
1077 state->crtc_y > INT_MAX - (int32_t) state->crtc_h) {
1078 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] invalid CRTC coordinates %ux%u+%d+%d\n",
1079 plane->base.id, plane->name,
1080 state->crtc_w, state->crtc_h,
1081 state->crtc_x, state->crtc_y);
1085 fb_width = state->fb->width << 16;
1086 fb_height = state->fb->height << 16;
1088 /* Make sure source coordinates are inside the fb. */
1089 if (state->src_w > fb_width ||
1090 state->src_x > fb_width - state->src_w ||
1091 state->src_h > fb_height ||
1092 state->src_y > fb_height - state->src_h) {
1093 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] invalid source coordinates "
1094 "%u.%06ux%u.%06u+%u.%06u+%u.%06u (fb %ux%u)\n",
1095 plane->base.id, plane->name,
1096 state->src_w >> 16, ((state->src_w & 0xffff) * 15625) >> 10,
1097 state->src_h >> 16, ((state->src_h & 0xffff) * 15625) >> 10,
1098 state->src_x >> 16, ((state->src_x & 0xffff) * 15625) >> 10,
1099 state->src_y >> 16, ((state->src_y & 0xffff) * 15625) >> 10,
1100 state->fb->width, state->fb->height);
1104 if (plane_switching_crtc(state->state, plane, state)) {
1105 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] switching CRTC directly\n",
1106 plane->base.id, plane->name);
1113 static void drm_atomic_plane_print_state(struct drm_printer *p,
1114 const struct drm_plane_state *state)
1116 struct drm_plane *plane = state->plane;
1117 struct drm_rect src = drm_plane_state_src(state);
1118 struct drm_rect dest = drm_plane_state_dest(state);
1120 drm_printf(p, "plane[%u]: %s\n", plane->base.id, plane->name);
1121 drm_printf(p, "\tcrtc=%s\n", state->crtc ? state->crtc->name : "(null)");
1122 drm_printf(p, "\tfb=%u\n", state->fb ? state->fb->base.id : 0);
1124 drm_framebuffer_print_info(p, 2, state->fb);
1125 drm_printf(p, "\tcrtc-pos=" DRM_RECT_FMT "\n", DRM_RECT_ARG(&dest));
1126 drm_printf(p, "\tsrc-pos=" DRM_RECT_FP_FMT "\n", DRM_RECT_FP_ARG(&src));
1127 drm_printf(p, "\trotation=%x\n", state->rotation);
1128 drm_printf(p, "\tnormalized-zpos=%x\n", state->normalized_zpos);
1129 drm_printf(p, "\tcolor-encoding=%s\n",
1130 drm_get_color_encoding_name(state->color_encoding));
1131 drm_printf(p, "\tcolor-range=%s\n",
1132 drm_get_color_range_name(state->color_range));
1134 if (plane->funcs->atomic_print_state)
1135 plane->funcs->atomic_print_state(p, state);
1139 * DOC: handling driver private state
1141 * Very often the DRM objects exposed to userspace in the atomic modeset api
1142 * (&drm_connector, &drm_crtc and &drm_plane) do not map neatly to the
1143 * underlying hardware. Especially for any kind of shared resources (e.g. shared
1144 * clocks, scaler units, bandwidth and fifo limits shared among a group of
1145 * planes or CRTCs, and so on) it makes sense to model these as independent
1146 * objects. Drivers then need to do similar state tracking and commit ordering for
1147 * such private (since not exposed to userpace) objects as the atomic core and
1148 * helpers already provide for connectors, planes and CRTCs.
1150 * To make this easier on drivers the atomic core provides some support to track
1151 * driver private state objects using struct &drm_private_obj, with the
1152 * associated state struct &drm_private_state.
1154 * Similar to userspace-exposed objects, private state structures can be
1155 * acquired by calling drm_atomic_get_private_obj_state(). Since this function
1156 * does not take care of locking, drivers should wrap it for each type of
1157 * private state object they have with the required call to drm_modeset_lock()
1158 * for the corresponding &drm_modeset_lock.
1160 * All private state structures contained in a &drm_atomic_state update can be
1161 * iterated using for_each_oldnew_private_obj_in_state(),
1162 * for_each_new_private_obj_in_state() and for_each_old_private_obj_in_state().
1163 * Drivers are recommended to wrap these for each type of driver private state
1164 * object they have, filtering on &drm_private_obj.funcs using for_each_if(), at
1165 * least if they want to iterate over all objects of a given type.
1167 * An earlier way to handle driver private state was by subclassing struct
1168 * &drm_atomic_state. But since that encourages non-standard ways to implement
1169 * the check/commit split atomic requires (by using e.g. "check and rollback or
1170 * commit instead" of "duplicate state, check, then either commit or release
1171 * duplicated state) it is deprecated in favour of using &drm_private_state.
1175 * drm_atomic_private_obj_init - initialize private object
1176 * @obj: private object
1177 * @state: initial private object state
1178 * @funcs: pointer to the struct of function pointers that identify the object
1181 * Initialize the private object, which can be embedded into any
1182 * driver private object that needs its own atomic state.
1185 drm_atomic_private_obj_init(struct drm_private_obj *obj,
1186 struct drm_private_state *state,
1187 const struct drm_private_state_funcs *funcs)
1189 memset(obj, 0, sizeof(*obj));
1194 EXPORT_SYMBOL(drm_atomic_private_obj_init);
1197 * drm_atomic_private_obj_fini - finalize private object
1198 * @obj: private object
1200 * Finalize the private object.
1203 drm_atomic_private_obj_fini(struct drm_private_obj *obj)
1205 obj->funcs->atomic_destroy_state(obj, obj->state);
1207 EXPORT_SYMBOL(drm_atomic_private_obj_fini);
1210 * drm_atomic_get_private_obj_state - get private object state
1211 * @state: global atomic state
1212 * @obj: private object to get the state for
1214 * This function returns the private object state for the given private object,
1215 * allocating the state if needed. It does not grab any locks as the caller is
1216 * expected to care of any required locking.
1220 * Either the allocated state or the error code encoded into a pointer.
1222 struct drm_private_state *
1223 drm_atomic_get_private_obj_state(struct drm_atomic_state *state,
1224 struct drm_private_obj *obj)
1226 int index, num_objs, i;
1228 struct __drm_private_objs_state *arr;
1229 struct drm_private_state *obj_state;
1231 for (i = 0; i < state->num_private_objs; i++)
1232 if (obj == state->private_objs[i].ptr)
1233 return state->private_objs[i].state;
1235 num_objs = state->num_private_objs + 1;
1236 size = sizeof(*state->private_objs) * num_objs;
1237 arr = krealloc(state->private_objs, size, GFP_KERNEL);
1239 return ERR_PTR(-ENOMEM);
1241 state->private_objs = arr;
1242 index = state->num_private_objs;
1243 memset(&state->private_objs[index], 0, sizeof(*state->private_objs));
1245 obj_state = obj->funcs->atomic_duplicate_state(obj);
1247 return ERR_PTR(-ENOMEM);
1249 state->private_objs[index].state = obj_state;
1250 state->private_objs[index].old_state = obj->state;
1251 state->private_objs[index].new_state = obj_state;
1252 state->private_objs[index].ptr = obj;
1253 obj_state->state = state;
1255 state->num_private_objs = num_objs;
1257 DRM_DEBUG_ATOMIC("Added new private object %p state %p to %p\n",
1258 obj, obj_state, state);
1262 EXPORT_SYMBOL(drm_atomic_get_private_obj_state);
1265 * drm_atomic_get_connector_state - get connector state
1266 * @state: global atomic state object
1267 * @connector: connector to get state object for
1269 * This function returns the connector state for the given connector,
1270 * allocating it if needed. It will also grab the relevant connector lock to
1271 * make sure that the state is consistent.
1275 * Either the allocated state or the error code encoded into the pointer. When
1276 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
1277 * entire atomic sequence must be restarted. All other errors are fatal.
1279 struct drm_connector_state *
1280 drm_atomic_get_connector_state(struct drm_atomic_state *state,
1281 struct drm_connector *connector)
1284 struct drm_mode_config *config = &connector->dev->mode_config;
1285 struct drm_connector_state *connector_state;
1287 WARN_ON(!state->acquire_ctx);
1289 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
1291 return ERR_PTR(ret);
1293 index = drm_connector_index(connector);
1295 if (index >= state->num_connector) {
1296 struct __drm_connnectors_state *c;
1297 int alloc = max(index + 1, config->num_connector);
1299 c = krealloc(state->connectors, alloc * sizeof(*state->connectors), GFP_KERNEL);
1301 return ERR_PTR(-ENOMEM);
1303 state->connectors = c;
1304 memset(&state->connectors[state->num_connector], 0,
1305 sizeof(*state->connectors) * (alloc - state->num_connector));
1307 state->num_connector = alloc;
1310 if (state->connectors[index].state)
1311 return state->connectors[index].state;
1313 connector_state = connector->funcs->atomic_duplicate_state(connector);
1314 if (!connector_state)
1315 return ERR_PTR(-ENOMEM);
1317 drm_connector_get(connector);
1318 state->connectors[index].state = connector_state;
1319 state->connectors[index].old_state = connector->state;
1320 state->connectors[index].new_state = connector_state;
1321 state->connectors[index].ptr = connector;
1322 connector_state->state = state;
1324 DRM_DEBUG_ATOMIC("Added [CONNECTOR:%d:%s] %p state to %p\n",
1325 connector->base.id, connector->name,
1326 connector_state, state);
1328 if (connector_state->crtc) {
1329 struct drm_crtc_state *crtc_state;
1331 crtc_state = drm_atomic_get_crtc_state(state,
1332 connector_state->crtc);
1333 if (IS_ERR(crtc_state))
1334 return ERR_CAST(crtc_state);
1337 return connector_state;
1339 EXPORT_SYMBOL(drm_atomic_get_connector_state);
1342 * drm_atomic_connector_set_property - set property on connector.
1343 * @connector: the drm connector to set a property on
1344 * @state: the state object to update with the new property value
1345 * @property: the property to set
1346 * @val: the new property value
1348 * This function handles generic/core properties and calls out to driver's
1349 * &drm_connector_funcs.atomic_set_property for driver properties. To ensure
1350 * consistent behavior you must call this function rather than the driver hook
1354 * Zero on success, error code on failure
1356 static int drm_atomic_connector_set_property(struct drm_connector *connector,
1357 struct drm_connector_state *state, struct drm_property *property,
1360 struct drm_device *dev = connector->dev;
1361 struct drm_mode_config *config = &dev->mode_config;
1363 if (property == config->prop_crtc_id) {
1364 struct drm_crtc *crtc = drm_crtc_find(dev, NULL, val);
1365 return drm_atomic_set_crtc_for_connector(state, crtc);
1366 } else if (property == config->dpms_property) {
1367 /* setting DPMS property requires special handling, which
1368 * is done in legacy setprop path for us. Disallow (for
1369 * now?) atomic writes to DPMS property:
1372 } else if (property == config->tv_select_subconnector_property) {
1373 state->tv.subconnector = val;
1374 } else if (property == config->tv_left_margin_property) {
1375 state->tv.margins.left = val;
1376 } else if (property == config->tv_right_margin_property) {
1377 state->tv.margins.right = val;
1378 } else if (property == config->tv_top_margin_property) {
1379 state->tv.margins.top = val;
1380 } else if (property == config->tv_bottom_margin_property) {
1381 state->tv.margins.bottom = val;
1382 } else if (property == config->tv_mode_property) {
1383 state->tv.mode = val;
1384 } else if (property == config->tv_brightness_property) {
1385 state->tv.brightness = val;
1386 } else if (property == config->tv_contrast_property) {
1387 state->tv.contrast = val;
1388 } else if (property == config->tv_flicker_reduction_property) {
1389 state->tv.flicker_reduction = val;
1390 } else if (property == config->tv_overscan_property) {
1391 state->tv.overscan = val;
1392 } else if (property == config->tv_saturation_property) {
1393 state->tv.saturation = val;
1394 } else if (property == config->tv_hue_property) {
1395 state->tv.hue = val;
1396 } else if (property == config->link_status_property) {
1397 /* Never downgrade from GOOD to BAD on userspace's request here,
1398 * only hw issues can do that.
1400 * For an atomic property the userspace doesn't need to be able
1401 * to understand all the properties, but needs to be able to
1402 * restore the state it wants on VT switch. So if the userspace
1403 * tries to change the link_status from GOOD to BAD, driver
1404 * silently rejects it and returns a 0. This prevents userspace
1405 * from accidently breaking the display when it restores the
1408 if (state->link_status != DRM_LINK_STATUS_GOOD)
1409 state->link_status = val;
1410 } else if (property == config->aspect_ratio_property) {
1411 state->picture_aspect_ratio = val;
1412 } else if (property == config->content_type_property) {
1413 state->content_type = val;
1414 } else if (property == connector->scaling_mode_property) {
1415 state->scaling_mode = val;
1416 } else if (property == connector->content_protection_property) {
1417 if (val == DRM_MODE_CONTENT_PROTECTION_ENABLED) {
1418 DRM_DEBUG_KMS("only drivers can set CP Enabled\n");
1421 state->content_protection = val;
1422 } else if (property == config->writeback_fb_id_property) {
1423 struct drm_framebuffer *fb = drm_framebuffer_lookup(dev, NULL, val);
1424 int ret = drm_atomic_set_writeback_fb_for_connector(state, fb);
1426 drm_framebuffer_put(fb);
1428 } else if (property == config->writeback_out_fence_ptr_property) {
1429 s32 __user *fence_ptr = u64_to_user_ptr(val);
1431 return set_out_fence_for_connector(state->state, connector,
1433 } else if (connector->funcs->atomic_set_property) {
1434 return connector->funcs->atomic_set_property(connector,
1435 state, property, val);
1437 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] unknown property [PROP:%d:%s]]\n",
1438 connector->base.id, connector->name,
1439 property->base.id, property->name);
1446 static void drm_atomic_connector_print_state(struct drm_printer *p,
1447 const struct drm_connector_state *state)
1449 struct drm_connector *connector = state->connector;
1451 drm_printf(p, "connector[%u]: %s\n", connector->base.id, connector->name);
1452 drm_printf(p, "\tcrtc=%s\n", state->crtc ? state->crtc->name : "(null)");
1454 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
1455 if (state->writeback_job && state->writeback_job->fb)
1456 drm_printf(p, "\tfb=%d\n", state->writeback_job->fb->base.id);
1458 if (connector->funcs->atomic_print_state)
1459 connector->funcs->atomic_print_state(p, state);
1463 * drm_atomic_connector_get_property - get property value from connector state
1464 * @connector: the drm connector to set a property on
1465 * @state: the state object to get the property value from
1466 * @property: the property to set
1467 * @val: return location for the property value
1469 * This function handles generic/core properties and calls out to driver's
1470 * &drm_connector_funcs.atomic_get_property for driver properties. To ensure
1471 * consistent behavior you must call this function rather than the driver hook
1475 * Zero on success, error code on failure
1478 drm_atomic_connector_get_property(struct drm_connector *connector,
1479 const struct drm_connector_state *state,
1480 struct drm_property *property, uint64_t *val)
1482 struct drm_device *dev = connector->dev;
1483 struct drm_mode_config *config = &dev->mode_config;
1485 if (property == config->prop_crtc_id) {
1486 *val = (state->crtc) ? state->crtc->base.id : 0;
1487 } else if (property == config->dpms_property) {
1488 *val = connector->dpms;
1489 } else if (property == config->tv_select_subconnector_property) {
1490 *val = state->tv.subconnector;
1491 } else if (property == config->tv_left_margin_property) {
1492 *val = state->tv.margins.left;
1493 } else if (property == config->tv_right_margin_property) {
1494 *val = state->tv.margins.right;
1495 } else if (property == config->tv_top_margin_property) {
1496 *val = state->tv.margins.top;
1497 } else if (property == config->tv_bottom_margin_property) {
1498 *val = state->tv.margins.bottom;
1499 } else if (property == config->tv_mode_property) {
1500 *val = state->tv.mode;
1501 } else if (property == config->tv_brightness_property) {
1502 *val = state->tv.brightness;
1503 } else if (property == config->tv_contrast_property) {
1504 *val = state->tv.contrast;
1505 } else if (property == config->tv_flicker_reduction_property) {
1506 *val = state->tv.flicker_reduction;
1507 } else if (property == config->tv_overscan_property) {
1508 *val = state->tv.overscan;
1509 } else if (property == config->tv_saturation_property) {
1510 *val = state->tv.saturation;
1511 } else if (property == config->tv_hue_property) {
1512 *val = state->tv.hue;
1513 } else if (property == config->link_status_property) {
1514 *val = state->link_status;
1515 } else if (property == config->aspect_ratio_property) {
1516 *val = state->picture_aspect_ratio;
1517 } else if (property == config->content_type_property) {
1518 *val = state->content_type;
1519 } else if (property == connector->scaling_mode_property) {
1520 *val = state->scaling_mode;
1521 } else if (property == connector->content_protection_property) {
1522 *val = state->content_protection;
1523 } else if (property == config->writeback_fb_id_property) {
1524 /* Writeback framebuffer is one-shot, write and forget */
1526 } else if (property == config->writeback_out_fence_ptr_property) {
1528 } else if (connector->funcs->atomic_get_property) {
1529 return connector->funcs->atomic_get_property(connector,
1530 state, property, val);
1538 int drm_atomic_get_property(struct drm_mode_object *obj,
1539 struct drm_property *property, uint64_t *val)
1541 struct drm_device *dev = property->dev;
1544 switch (obj->type) {
1545 case DRM_MODE_OBJECT_CONNECTOR: {
1546 struct drm_connector *connector = obj_to_connector(obj);
1547 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
1548 ret = drm_atomic_connector_get_property(connector,
1549 connector->state, property, val);
1552 case DRM_MODE_OBJECT_CRTC: {
1553 struct drm_crtc *crtc = obj_to_crtc(obj);
1554 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
1555 ret = drm_atomic_crtc_get_property(crtc,
1556 crtc->state, property, val);
1559 case DRM_MODE_OBJECT_PLANE: {
1560 struct drm_plane *plane = obj_to_plane(obj);
1561 WARN_ON(!drm_modeset_is_locked(&plane->mutex));
1562 ret = drm_atomic_plane_get_property(plane,
1563 plane->state, property, val);
1575 * drm_atomic_set_crtc_for_plane - set crtc for plane
1576 * @plane_state: the plane whose incoming state to update
1577 * @crtc: crtc to use for the plane
1579 * Changing the assigned crtc for a plane requires us to grab the lock and state
1580 * for the new crtc, as needed. This function takes care of all these details
1581 * besides updating the pointer in the state object itself.
1584 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1585 * then the w/w mutex code has detected a deadlock and the entire atomic
1586 * sequence must be restarted. All other errors are fatal.
1589 drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
1590 struct drm_crtc *crtc)
1592 struct drm_plane *plane = plane_state->plane;
1593 struct drm_crtc_state *crtc_state;
1594 /* Nothing to do for same crtc*/
1595 if (plane_state->crtc == crtc)
1597 if (plane_state->crtc) {
1598 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
1600 if (WARN_ON(IS_ERR(crtc_state)))
1601 return PTR_ERR(crtc_state);
1603 crtc_state->plane_mask &= ~drm_plane_mask(plane);
1606 plane_state->crtc = crtc;
1609 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
1611 if (IS_ERR(crtc_state))
1612 return PTR_ERR(crtc_state);
1613 crtc_state->plane_mask |= drm_plane_mask(plane);
1617 DRM_DEBUG_ATOMIC("Link [PLANE:%d:%s] state %p to [CRTC:%d:%s]\n",
1618 plane->base.id, plane->name, plane_state,
1619 crtc->base.id, crtc->name);
1621 DRM_DEBUG_ATOMIC("Link [PLANE:%d:%s] state %p to [NOCRTC]\n",
1622 plane->base.id, plane->name, plane_state);
1626 EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane);
1629 * drm_atomic_set_fb_for_plane - set framebuffer for plane
1630 * @plane_state: atomic state object for the plane
1631 * @fb: fb to use for the plane
1633 * Changing the assigned framebuffer for a plane requires us to grab a reference
1634 * to the new fb and drop the reference to the old fb, if there is one. This
1635 * function takes care of all these details besides updating the pointer in the
1636 * state object itself.
1639 drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
1640 struct drm_framebuffer *fb)
1642 struct drm_plane *plane = plane_state->plane;
1645 DRM_DEBUG_ATOMIC("Set [FB:%d] for [PLANE:%d:%s] state %p\n",
1646 fb->base.id, plane->base.id, plane->name,
1649 DRM_DEBUG_ATOMIC("Set [NOFB] for [PLANE:%d:%s] state %p\n",
1650 plane->base.id, plane->name, plane_state);
1652 drm_framebuffer_assign(&plane_state->fb, fb);
1654 EXPORT_SYMBOL(drm_atomic_set_fb_for_plane);
1657 * drm_atomic_set_fence_for_plane - set fence for plane
1658 * @plane_state: atomic state object for the plane
1659 * @fence: dma_fence to use for the plane
1661 * Helper to setup the plane_state fence in case it is not set yet.
1662 * By using this drivers doesn't need to worry if the user choose
1663 * implicit or explicit fencing.
1665 * This function will not set the fence to the state if it was set
1666 * via explicit fencing interfaces on the atomic ioctl. In that case it will
1667 * drop the reference to the fence as we are not storing it anywhere.
1668 * Otherwise, if &drm_plane_state.fence is not set this function we just set it
1669 * with the received implicit fence. In both cases this function consumes a
1670 * reference for @fence.
1672 * This way explicit fencing can be used to overrule implicit fencing, which is
1673 * important to make explicit fencing use-cases work: One example is using one
1674 * buffer for 2 screens with different refresh rates. Implicit fencing will
1675 * clamp rendering to the refresh rate of the slower screen, whereas explicit
1676 * fence allows 2 independent render and display loops on a single buffer. If a
1677 * driver allows obeys both implicit and explicit fences for plane updates, then
1678 * it will break all the benefits of explicit fencing.
1681 drm_atomic_set_fence_for_plane(struct drm_plane_state *plane_state,
1682 struct dma_fence *fence)
1684 if (plane_state->fence) {
1685 dma_fence_put(fence);
1689 plane_state->fence = fence;
1691 EXPORT_SYMBOL(drm_atomic_set_fence_for_plane);
1694 * drm_atomic_set_crtc_for_connector - set crtc for connector
1695 * @conn_state: atomic state object for the connector
1696 * @crtc: crtc to use for the connector
1698 * Changing the assigned crtc for a connector requires us to grab the lock and
1699 * state for the new crtc, as needed. This function takes care of all these
1700 * details besides updating the pointer in the state object itself.
1703 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1704 * then the w/w mutex code has detected a deadlock and the entire atomic
1705 * sequence must be restarted. All other errors are fatal.
1708 drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
1709 struct drm_crtc *crtc)
1711 struct drm_connector *connector = conn_state->connector;
1712 struct drm_crtc_state *crtc_state;
1714 if (conn_state->crtc == crtc)
1717 if (conn_state->crtc) {
1718 crtc_state = drm_atomic_get_new_crtc_state(conn_state->state,
1721 crtc_state->connector_mask &=
1722 ~drm_connector_mask(conn_state->connector);
1724 drm_connector_put(conn_state->connector);
1725 conn_state->crtc = NULL;
1729 crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc);
1730 if (IS_ERR(crtc_state))
1731 return PTR_ERR(crtc_state);
1733 crtc_state->connector_mask |=
1734 drm_connector_mask(conn_state->connector);
1736 drm_connector_get(conn_state->connector);
1737 conn_state->crtc = crtc;
1739 DRM_DEBUG_ATOMIC("Link [CONNECTOR:%d:%s] state %p to [CRTC:%d:%s]\n",
1740 connector->base.id, connector->name,
1741 conn_state, crtc->base.id, crtc->name);
1743 DRM_DEBUG_ATOMIC("Link [CONNECTOR:%d:%s] state %p to [NOCRTC]\n",
1744 connector->base.id, connector->name,
1750 EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector);
1753 * drm_atomic_get_writeback_job - return or allocate a writeback job
1754 * @conn_state: Connector state to get the job for
1756 * Writeback jobs have a different lifetime to the atomic state they are
1757 * associated with. This convenience function takes care of allocating a job
1758 * if there isn't yet one associated with the connector state, otherwise
1759 * it just returns the existing job.
1761 * Returns: The writeback job for the given connector state
1763 static struct drm_writeback_job *
1764 drm_atomic_get_writeback_job(struct drm_connector_state *conn_state)
1766 WARN_ON(conn_state->connector->connector_type != DRM_MODE_CONNECTOR_WRITEBACK);
1768 if (!conn_state->writeback_job)
1769 conn_state->writeback_job =
1770 kzalloc(sizeof(*conn_state->writeback_job), GFP_KERNEL);
1772 return conn_state->writeback_job;
1776 * drm_atomic_set_writeback_fb_for_connector - set writeback framebuffer
1777 * @conn_state: atomic state object for the connector
1778 * @fb: fb to use for the connector
1780 * This is used to set the framebuffer for a writeback connector, which outputs
1781 * to a buffer instead of an actual physical connector.
1782 * Changing the assigned framebuffer requires us to grab a reference to the new
1783 * fb and drop the reference to the old fb, if there is one. This function
1784 * takes care of all these details besides updating the pointer in the
1785 * state object itself.
1787 * Note: The only way conn_state can already have an fb set is if the commit
1788 * sets the property more than once.
1790 * See also: drm_writeback_connector_init()
1792 * Returns: 0 on success
1794 int drm_atomic_set_writeback_fb_for_connector(
1795 struct drm_connector_state *conn_state,
1796 struct drm_framebuffer *fb)
1798 struct drm_writeback_job *job =
1799 drm_atomic_get_writeback_job(conn_state);
1803 drm_framebuffer_assign(&job->fb, fb);
1806 DRM_DEBUG_ATOMIC("Set [FB:%d] for connector state %p\n",
1807 fb->base.id, conn_state);
1809 DRM_DEBUG_ATOMIC("Set [NOFB] for connector state %p\n",
1814 EXPORT_SYMBOL(drm_atomic_set_writeback_fb_for_connector);
1817 * drm_atomic_add_affected_connectors - add connectors for crtc
1818 * @state: atomic state
1821 * This function walks the current configuration and adds all connectors
1822 * currently using @crtc to the atomic configuration @state. Note that this
1823 * function must acquire the connection mutex. This can potentially cause
1824 * unneeded seralization if the update is just for the planes on one crtc. Hence
1825 * drivers and helpers should only call this when really needed (e.g. when a
1826 * full modeset needs to happen due to some change).
1829 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1830 * then the w/w mutex code has detected a deadlock and the entire atomic
1831 * sequence must be restarted. All other errors are fatal.
1834 drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
1835 struct drm_crtc *crtc)
1837 struct drm_mode_config *config = &state->dev->mode_config;
1838 struct drm_connector *connector;
1839 struct drm_connector_state *conn_state;
1840 struct drm_connector_list_iter conn_iter;
1841 struct drm_crtc_state *crtc_state;
1844 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1845 if (IS_ERR(crtc_state))
1846 return PTR_ERR(crtc_state);
1848 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
1852 DRM_DEBUG_ATOMIC("Adding all current connectors for [CRTC:%d:%s] to %p\n",
1853 crtc->base.id, crtc->name, state);
1856 * Changed connectors are already in @state, so only need to look
1857 * at the connector_mask in crtc_state.
1859 drm_connector_list_iter_begin(state->dev, &conn_iter);
1860 drm_for_each_connector_iter(connector, &conn_iter) {
1861 if (!(crtc_state->connector_mask & drm_connector_mask(connector)))
1864 conn_state = drm_atomic_get_connector_state(state, connector);
1865 if (IS_ERR(conn_state)) {
1866 drm_connector_list_iter_end(&conn_iter);
1867 return PTR_ERR(conn_state);
1870 drm_connector_list_iter_end(&conn_iter);
1874 EXPORT_SYMBOL(drm_atomic_add_affected_connectors);
1877 * drm_atomic_add_affected_planes - add planes for crtc
1878 * @state: atomic state
1881 * This function walks the current configuration and adds all planes
1882 * currently used by @crtc to the atomic configuration @state. This is useful
1883 * when an atomic commit also needs to check all currently enabled plane on
1884 * @crtc, e.g. when changing the mode. It's also useful when re-enabling a CRTC
1885 * to avoid special code to force-enable all planes.
1887 * Since acquiring a plane state will always also acquire the w/w mutex of the
1888 * current CRTC for that plane (if there is any) adding all the plane states for
1889 * a CRTC will not reduce parallism of atomic updates.
1892 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1893 * then the w/w mutex code has detected a deadlock and the entire atomic
1894 * sequence must be restarted. All other errors are fatal.
1897 drm_atomic_add_affected_planes(struct drm_atomic_state *state,
1898 struct drm_crtc *crtc)
1900 struct drm_plane *plane;
1902 WARN_ON(!drm_atomic_get_new_crtc_state(state, crtc));
1904 DRM_DEBUG_ATOMIC("Adding all current planes for [CRTC:%d:%s] to %p\n",
1905 crtc->base.id, crtc->name, state);
1907 drm_for_each_plane_mask(plane, state->dev, crtc->state->plane_mask) {
1908 struct drm_plane_state *plane_state =
1909 drm_atomic_get_plane_state(state, plane);
1911 if (IS_ERR(plane_state))
1912 return PTR_ERR(plane_state);
1916 EXPORT_SYMBOL(drm_atomic_add_affected_planes);
1919 * drm_atomic_check_only - check whether a given config would work
1920 * @state: atomic configuration to check
1922 * Note that this function can return -EDEADLK if the driver needed to acquire
1923 * more locks but encountered a deadlock. The caller must then do the usual w/w
1924 * backoff dance and restart. All other errors are fatal.
1927 * 0 on success, negative error code on failure.
1929 int drm_atomic_check_only(struct drm_atomic_state *state)
1931 struct drm_device *dev = state->dev;
1932 struct drm_mode_config *config = &dev->mode_config;
1933 struct drm_plane *plane;
1934 struct drm_plane_state *plane_state;
1935 struct drm_crtc *crtc;
1936 struct drm_crtc_state *crtc_state;
1937 struct drm_connector *conn;
1938 struct drm_connector_state *conn_state;
1941 DRM_DEBUG_ATOMIC("checking %p\n", state);
1943 for_each_new_plane_in_state(state, plane, plane_state, i) {
1944 ret = drm_atomic_plane_check(plane, plane_state);
1946 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic core check failed\n",
1947 plane->base.id, plane->name);
1952 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
1953 ret = drm_atomic_crtc_check(crtc, crtc_state);
1955 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic core check failed\n",
1956 crtc->base.id, crtc->name);
1961 for_each_new_connector_in_state(state, conn, conn_state, i) {
1962 ret = drm_atomic_connector_check(conn, conn_state);
1964 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] atomic core check failed\n",
1965 conn->base.id, conn->name);
1970 if (config->funcs->atomic_check) {
1971 ret = config->funcs->atomic_check(state->dev, state);
1974 DRM_DEBUG_ATOMIC("atomic driver check for %p failed: %d\n",
1980 if (!state->allow_modeset) {
1981 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
1982 if (drm_atomic_crtc_needs_modeset(crtc_state)) {
1983 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] requires full modeset\n",
1984 crtc->base.id, crtc->name);
1992 EXPORT_SYMBOL(drm_atomic_check_only);
1995 * drm_atomic_commit - commit configuration atomically
1996 * @state: atomic configuration to check
1998 * Note that this function can return -EDEADLK if the driver needed to acquire
1999 * more locks but encountered a deadlock. The caller must then do the usual w/w
2000 * backoff dance and restart. All other errors are fatal.
2002 * This function will take its own reference on @state.
2003 * Callers should always release their reference with drm_atomic_state_put().
2006 * 0 on success, negative error code on failure.
2008 int drm_atomic_commit(struct drm_atomic_state *state)
2010 struct drm_mode_config *config = &state->dev->mode_config;
2013 ret = drm_atomic_check_only(state);
2017 DRM_DEBUG_ATOMIC("committing %p\n", state);
2019 return config->funcs->atomic_commit(state->dev, state, false);
2021 EXPORT_SYMBOL(drm_atomic_commit);
2024 * drm_atomic_nonblocking_commit - atomic nonblocking commit
2025 * @state: atomic configuration to check
2027 * Note that this function can return -EDEADLK if the driver needed to acquire
2028 * more locks but encountered a deadlock. The caller must then do the usual w/w
2029 * backoff dance and restart. All other errors are fatal.
2031 * This function will take its own reference on @state.
2032 * Callers should always release their reference with drm_atomic_state_put().
2035 * 0 on success, negative error code on failure.
2037 int drm_atomic_nonblocking_commit(struct drm_atomic_state *state)
2039 struct drm_mode_config *config = &state->dev->mode_config;
2042 ret = drm_atomic_check_only(state);
2046 DRM_DEBUG_ATOMIC("committing %p nonblocking\n", state);
2048 return config->funcs->atomic_commit(state->dev, state, true);
2050 EXPORT_SYMBOL(drm_atomic_nonblocking_commit);
2052 static void drm_atomic_print_state(const struct drm_atomic_state *state)
2054 struct drm_printer p = drm_info_printer(state->dev->dev);
2055 struct drm_plane *plane;
2056 struct drm_plane_state *plane_state;
2057 struct drm_crtc *crtc;
2058 struct drm_crtc_state *crtc_state;
2059 struct drm_connector *connector;
2060 struct drm_connector_state *connector_state;
2063 DRM_DEBUG_ATOMIC("checking %p\n", state);
2065 for_each_new_plane_in_state(state, plane, plane_state, i)
2066 drm_atomic_plane_print_state(&p, plane_state);
2068 for_each_new_crtc_in_state(state, crtc, crtc_state, i)
2069 drm_atomic_crtc_print_state(&p, crtc_state);
2071 for_each_new_connector_in_state(state, connector, connector_state, i)
2072 drm_atomic_connector_print_state(&p, connector_state);
2075 static void __drm_state_dump(struct drm_device *dev, struct drm_printer *p,
2078 struct drm_mode_config *config = &dev->mode_config;
2079 struct drm_plane *plane;
2080 struct drm_crtc *crtc;
2081 struct drm_connector *connector;
2082 struct drm_connector_list_iter conn_iter;
2084 if (!drm_drv_uses_atomic_modeset(dev))
2087 list_for_each_entry(plane, &config->plane_list, head) {
2089 drm_modeset_lock(&plane->mutex, NULL);
2090 drm_atomic_plane_print_state(p, plane->state);
2092 drm_modeset_unlock(&plane->mutex);
2095 list_for_each_entry(crtc, &config->crtc_list, head) {
2097 drm_modeset_lock(&crtc->mutex, NULL);
2098 drm_atomic_crtc_print_state(p, crtc->state);
2100 drm_modeset_unlock(&crtc->mutex);
2103 drm_connector_list_iter_begin(dev, &conn_iter);
2105 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
2106 drm_for_each_connector_iter(connector, &conn_iter)
2107 drm_atomic_connector_print_state(p, connector->state);
2109 drm_modeset_unlock(&dev->mode_config.connection_mutex);
2110 drm_connector_list_iter_end(&conn_iter);
2114 * drm_state_dump - dump entire device atomic state
2115 * @dev: the drm device
2116 * @p: where to print the state to
2118 * Just for debugging. Drivers might want an option to dump state
2119 * to dmesg in case of error irq's. (Hint, you probably want to
2122 * The caller must drm_modeset_lock_all(), or if this is called
2123 * from error irq handler, it should not be enabled by default.
2124 * (Ie. if you are debugging errors you might not care that this
2125 * is racey. But calling this without all modeset locks held is
2126 * not inherently safe.)
2128 void drm_state_dump(struct drm_device *dev, struct drm_printer *p)
2130 __drm_state_dump(dev, p, false);
2132 EXPORT_SYMBOL(drm_state_dump);
2134 #ifdef CONFIG_DEBUG_FS
2135 static int drm_state_info(struct seq_file *m, void *data)
2137 struct drm_info_node *node = (struct drm_info_node *) m->private;
2138 struct drm_device *dev = node->minor->dev;
2139 struct drm_printer p = drm_seq_file_printer(m);
2141 __drm_state_dump(dev, &p, true);
2146 /* any use in debugfs files to dump individual planes/crtc/etc? */
2147 static const struct drm_info_list drm_atomic_debugfs_list[] = {
2148 {"state", drm_state_info, 0},
2151 int drm_atomic_debugfs_init(struct drm_minor *minor)
2153 return drm_debugfs_create_files(drm_atomic_debugfs_list,
2154 ARRAY_SIZE(drm_atomic_debugfs_list),
2155 minor->debugfs_root, minor);
2160 * The big monster ioctl
2163 static struct drm_pending_vblank_event *create_vblank_event(
2164 struct drm_crtc *crtc, uint64_t user_data)
2166 struct drm_pending_vblank_event *e = NULL;
2168 e = kzalloc(sizeof *e, GFP_KERNEL);
2172 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
2173 e->event.base.length = sizeof(e->event);
2174 e->event.vbl.crtc_id = crtc->base.id;
2175 e->event.vbl.user_data = user_data;
2180 int drm_atomic_connector_commit_dpms(struct drm_atomic_state *state,
2181 struct drm_connector *connector,
2184 struct drm_connector *tmp_connector;
2185 struct drm_connector_state *new_conn_state;
2186 struct drm_crtc *crtc;
2187 struct drm_crtc_state *crtc_state;
2188 int i, ret, old_mode = connector->dpms;
2189 bool active = false;
2191 ret = drm_modeset_lock(&state->dev->mode_config.connection_mutex,
2192 state->acquire_ctx);
2196 if (mode != DRM_MODE_DPMS_ON)
2197 mode = DRM_MODE_DPMS_OFF;
2198 connector->dpms = mode;
2200 crtc = connector->state->crtc;
2203 ret = drm_atomic_add_affected_connectors(state, crtc);
2207 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2208 if (IS_ERR(crtc_state)) {
2209 ret = PTR_ERR(crtc_state);
2213 for_each_new_connector_in_state(state, tmp_connector, new_conn_state, i) {
2214 if (new_conn_state->crtc != crtc)
2216 if (tmp_connector->dpms == DRM_MODE_DPMS_ON) {
2222 crtc_state->active = active;
2223 ret = drm_atomic_commit(state);
2226 connector->dpms = old_mode;
2230 int drm_atomic_set_property(struct drm_atomic_state *state,
2231 struct drm_mode_object *obj,
2232 struct drm_property *prop,
2233 uint64_t prop_value)
2235 struct drm_mode_object *ref;
2238 if (!drm_property_change_valid_get(prop, prop_value, &ref))
2241 switch (obj->type) {
2242 case DRM_MODE_OBJECT_CONNECTOR: {
2243 struct drm_connector *connector = obj_to_connector(obj);
2244 struct drm_connector_state *connector_state;
2246 connector_state = drm_atomic_get_connector_state(state, connector);
2247 if (IS_ERR(connector_state)) {
2248 ret = PTR_ERR(connector_state);
2252 ret = drm_atomic_connector_set_property(connector,
2253 connector_state, prop, prop_value);
2256 case DRM_MODE_OBJECT_CRTC: {
2257 struct drm_crtc *crtc = obj_to_crtc(obj);
2258 struct drm_crtc_state *crtc_state;
2260 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2261 if (IS_ERR(crtc_state)) {
2262 ret = PTR_ERR(crtc_state);
2266 ret = drm_atomic_crtc_set_property(crtc,
2267 crtc_state, prop, prop_value);
2270 case DRM_MODE_OBJECT_PLANE: {
2271 struct drm_plane *plane = obj_to_plane(obj);
2272 struct drm_plane_state *plane_state;
2274 plane_state = drm_atomic_get_plane_state(state, plane);
2275 if (IS_ERR(plane_state)) {
2276 ret = PTR_ERR(plane_state);
2280 ret = drm_atomic_plane_set_property(plane,
2281 plane_state, prop, prop_value);
2289 drm_property_change_valid_put(prop, ref);
2294 * DOC: explicit fencing properties
2296 * Explicit fencing allows userspace to control the buffer synchronization
2297 * between devices. A Fence or a group of fences are transfered to/from
2298 * userspace using Sync File fds and there are two DRM properties for that.
2299 * IN_FENCE_FD on each DRM Plane to send fences to the kernel and
2300 * OUT_FENCE_PTR on each DRM CRTC to receive fences from the kernel.
2302 * As a contrast, with implicit fencing the kernel keeps track of any
2303 * ongoing rendering, and automatically ensures that the atomic update waits
2304 * for any pending rendering to complete. For shared buffers represented with
2305 * a &struct dma_buf this is tracked in &struct reservation_object.
2306 * Implicit syncing is how Linux traditionally worked (e.g. DRI2/3 on X.org),
2307 * whereas explicit fencing is what Android wants.
2310 * Use this property to pass a fence that DRM should wait on before
2311 * proceeding with the Atomic Commit request and show the framebuffer for
2312 * the plane on the screen. The fence can be either a normal fence or a
2313 * merged one, the sync_file framework will handle both cases and use a
2314 * fence_array if a merged fence is received. Passing -1 here means no
2315 * fences to wait on.
2317 * If the Atomic Commit request has the DRM_MODE_ATOMIC_TEST_ONLY flag
2318 * it will only check if the Sync File is a valid one.
2320 * On the driver side the fence is stored on the @fence parameter of
2321 * &struct drm_plane_state. Drivers which also support implicit fencing
2322 * should set the implicit fence using drm_atomic_set_fence_for_plane(),
2323 * to make sure there's consistent behaviour between drivers in precedence
2324 * of implicit vs. explicit fencing.
2327 * Use this property to pass a file descriptor pointer to DRM. Once the
2328 * Atomic Commit request call returns OUT_FENCE_PTR will be filled with
2329 * the file descriptor number of a Sync File. This Sync File contains the
2330 * CRTC fence that will be signaled when all framebuffers present on the
2331 * Atomic Commit * request for that given CRTC are scanned out on the
2334 * The Atomic Commit request fails if a invalid pointer is passed. If the
2335 * Atomic Commit request fails for any other reason the out fence fd
2336 * returned will be -1. On a Atomic Commit with the
2337 * DRM_MODE_ATOMIC_TEST_ONLY flag the out fence will also be set to -1.
2339 * Note that out-fences don't have a special interface to drivers and are
2340 * internally represented by a &struct drm_pending_vblank_event in struct
2341 * &drm_crtc_state, which is also used by the nonblocking atomic commit
2342 * helpers and for the DRM event handling for existing userspace.
2345 struct drm_out_fence_state {
2346 s32 __user *out_fence_ptr;
2347 struct sync_file *sync_file;
2351 static int setup_out_fence(struct drm_out_fence_state *fence_state,
2352 struct dma_fence *fence)
2354 fence_state->fd = get_unused_fd_flags(O_CLOEXEC);
2355 if (fence_state->fd < 0)
2356 return fence_state->fd;
2358 if (put_user(fence_state->fd, fence_state->out_fence_ptr))
2361 fence_state->sync_file = sync_file_create(fence);
2362 if (!fence_state->sync_file)
2368 static int prepare_signaling(struct drm_device *dev,
2369 struct drm_atomic_state *state,
2370 struct drm_mode_atomic *arg,
2371 struct drm_file *file_priv,
2372 struct drm_out_fence_state **fence_state,
2373 unsigned int *num_fences)
2375 struct drm_crtc *crtc;
2376 struct drm_crtc_state *crtc_state;
2377 struct drm_connector *conn;
2378 struct drm_connector_state *conn_state;
2381 if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)
2384 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
2385 s32 __user *fence_ptr;
2387 fence_ptr = get_out_fence_for_crtc(crtc_state->state, crtc);
2389 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT || fence_ptr) {
2390 struct drm_pending_vblank_event *e;
2392 e = create_vblank_event(crtc, arg->user_data);
2396 crtc_state->event = e;
2399 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
2400 struct drm_pending_vblank_event *e = crtc_state->event;
2405 ret = drm_event_reserve_init(dev, file_priv, &e->base,
2409 crtc_state->event = NULL;
2415 struct dma_fence *fence;
2416 struct drm_out_fence_state *f;
2418 f = krealloc(*fence_state, sizeof(**fence_state) *
2419 (*num_fences + 1), GFP_KERNEL);
2423 memset(&f[*num_fences], 0, sizeof(*f));
2425 f[*num_fences].out_fence_ptr = fence_ptr;
2428 fence = drm_crtc_create_fence(crtc);
2432 ret = setup_out_fence(&f[(*num_fences)++], fence);
2434 dma_fence_put(fence);
2438 crtc_state->event->base.fence = fence;
2444 for_each_new_connector_in_state(state, conn, conn_state, i) {
2445 struct drm_writeback_connector *wb_conn;
2446 struct drm_writeback_job *job;
2447 struct drm_out_fence_state *f;
2448 struct dma_fence *fence;
2449 s32 __user *fence_ptr;
2451 fence_ptr = get_out_fence_for_connector(state, conn);
2455 job = drm_atomic_get_writeback_job(conn_state);
2459 f = krealloc(*fence_state, sizeof(**fence_state) *
2460 (*num_fences + 1), GFP_KERNEL);
2464 memset(&f[*num_fences], 0, sizeof(*f));
2466 f[*num_fences].out_fence_ptr = fence_ptr;
2469 wb_conn = drm_connector_to_writeback(conn);
2470 fence = drm_writeback_get_out_fence(wb_conn);
2474 ret = setup_out_fence(&f[(*num_fences)++], fence);
2476 dma_fence_put(fence);
2480 job->out_fence = fence;
2484 * Having this flag means user mode pends on event which will never
2485 * reach due to lack of at least one CRTC for signaling
2487 if (c == 0 && (arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
2493 static void complete_signaling(struct drm_device *dev,
2494 struct drm_atomic_state *state,
2495 struct drm_out_fence_state *fence_state,
2496 unsigned int num_fences,
2499 struct drm_crtc *crtc;
2500 struct drm_crtc_state *crtc_state;
2504 for (i = 0; i < num_fences; i++)
2505 fd_install(fence_state[i].fd,
2506 fence_state[i].sync_file->file);
2512 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
2513 struct drm_pending_vblank_event *event = crtc_state->event;
2515 * Free the allocated event. drm_atomic_helper_setup_commit
2516 * can allocate an event too, so only free it if it's ours
2517 * to prevent a double free in drm_atomic_state_clear.
2519 if (event && (event->base.fence || event->base.file_priv)) {
2520 drm_event_cancel_free(dev, &event->base);
2521 crtc_state->event = NULL;
2528 for (i = 0; i < num_fences; i++) {
2529 if (fence_state[i].sync_file)
2530 fput(fence_state[i].sync_file->file);
2531 if (fence_state[i].fd >= 0)
2532 put_unused_fd(fence_state[i].fd);
2534 /* If this fails log error to the user */
2535 if (fence_state[i].out_fence_ptr &&
2536 put_user(-1, fence_state[i].out_fence_ptr))
2537 DRM_DEBUG_ATOMIC("Couldn't clear out_fence_ptr\n");
2543 int drm_mode_atomic_ioctl(struct drm_device *dev,
2544 void *data, struct drm_file *file_priv)
2546 struct drm_mode_atomic *arg = data;
2547 uint32_t __user *objs_ptr = (uint32_t __user *)(unsigned long)(arg->objs_ptr);
2548 uint32_t __user *count_props_ptr = (uint32_t __user *)(unsigned long)(arg->count_props_ptr);
2549 uint32_t __user *props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
2550 uint64_t __user *prop_values_ptr = (uint64_t __user *)(unsigned long)(arg->prop_values_ptr);
2551 unsigned int copied_objs, copied_props;
2552 struct drm_atomic_state *state;
2553 struct drm_modeset_acquire_ctx ctx;
2554 struct drm_out_fence_state *fence_state;
2556 unsigned int i, j, num_fences;
2558 /* disallow for drivers not supporting atomic: */
2559 if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
2562 /* disallow for userspace that has not enabled atomic cap (even
2563 * though this may be a bit overkill, since legacy userspace
2564 * wouldn't know how to call this ioctl)
2566 if (!file_priv->atomic)
2569 if (arg->flags & ~DRM_MODE_ATOMIC_FLAGS)
2575 if ((arg->flags & DRM_MODE_PAGE_FLIP_ASYNC) &&
2576 !dev->mode_config.async_page_flip)
2579 /* can't test and expect an event at the same time. */
2580 if ((arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) &&
2581 (arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
2584 drm_modeset_acquire_init(&ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE);
2586 state = drm_atomic_state_alloc(dev);
2590 state->acquire_ctx = &ctx;
2591 state->allow_modeset = !!(arg->flags & DRM_MODE_ATOMIC_ALLOW_MODESET);
2599 for (i = 0; i < arg->count_objs; i++) {
2600 uint32_t obj_id, count_props;
2601 struct drm_mode_object *obj;
2603 if (get_user(obj_id, objs_ptr + copied_objs)) {
2608 obj = drm_mode_object_find(dev, file_priv, obj_id, DRM_MODE_OBJECT_ANY);
2614 if (!obj->properties) {
2615 drm_mode_object_put(obj);
2620 if (get_user(count_props, count_props_ptr + copied_objs)) {
2621 drm_mode_object_put(obj);
2628 for (j = 0; j < count_props; j++) {
2630 uint64_t prop_value;
2631 struct drm_property *prop;
2633 if (get_user(prop_id, props_ptr + copied_props)) {
2634 drm_mode_object_put(obj);
2639 prop = drm_mode_obj_find_prop_id(obj, prop_id);
2641 drm_mode_object_put(obj);
2646 if (copy_from_user(&prop_value,
2647 prop_values_ptr + copied_props,
2648 sizeof(prop_value))) {
2649 drm_mode_object_put(obj);
2654 ret = drm_atomic_set_property(state, obj, prop,
2657 drm_mode_object_put(obj);
2664 drm_mode_object_put(obj);
2667 ret = prepare_signaling(dev, state, arg, file_priv, &fence_state,
2672 if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) {
2673 ret = drm_atomic_check_only(state);
2674 } else if (arg->flags & DRM_MODE_ATOMIC_NONBLOCK) {
2675 ret = drm_atomic_nonblocking_commit(state);
2677 if (unlikely(drm_debug & DRM_UT_STATE))
2678 drm_atomic_print_state(state);
2680 ret = drm_atomic_commit(state);
2684 complete_signaling(dev, state, fence_state, num_fences, !ret);
2686 if (ret == -EDEADLK) {
2687 drm_atomic_state_clear(state);
2688 ret = drm_modeset_backoff(&ctx);
2693 drm_atomic_state_put(state);
2695 drm_modeset_drop_locks(&ctx);
2696 drm_modeset_acquire_fini(&ctx);