2 * Copyright (C) 2014 Red Hat
3 * Copyright (C) 2014 Intel Corp.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
24 * Rob Clark <robdclark@gmail.com>
25 * Daniel Vetter <daniel.vetter@ffwll.ch>
30 #include <drm/drm_atomic.h>
31 #include <drm/drm_mode.h>
32 #include <drm/drm_plane_helper.h>
33 #include <drm/drm_print.h>
34 #include <linux/sync_file.h>
36 #include "drm_crtc_internal.h"
38 void __drm_crtc_commit_free(struct kref
*kref
)
40 struct drm_crtc_commit
*commit
=
41 container_of(kref
, struct drm_crtc_commit
, ref
);
45 EXPORT_SYMBOL(__drm_crtc_commit_free
);
48 * drm_atomic_state_default_release -
49 * release memory initialized by drm_atomic_state_init
50 * @state: atomic state
52 * Free all the memory allocated by drm_atomic_state_init.
53 * This is useful for drivers that subclass the atomic state.
55 void drm_atomic_state_default_release(struct drm_atomic_state
*state
)
57 kfree(state
->connectors
);
60 kfree(state
->private_objs
);
62 EXPORT_SYMBOL(drm_atomic_state_default_release
);
65 * drm_atomic_state_init - init new atomic state
67 * @state: atomic state
69 * Default implementation for filling in a new atomic state.
70 * This is useful for drivers that subclass the atomic state.
73 drm_atomic_state_init(struct drm_device
*dev
, struct drm_atomic_state
*state
)
75 kref_init(&state
->ref
);
77 /* TODO legacy paths should maybe do a better job about
78 * setting this appropriately?
80 state
->allow_modeset
= true;
82 state
->crtcs
= kcalloc(dev
->mode_config
.num_crtc
,
83 sizeof(*state
->crtcs
), GFP_KERNEL
);
86 state
->planes
= kcalloc(dev
->mode_config
.num_total_plane
,
87 sizeof(*state
->planes
), GFP_KERNEL
);
93 DRM_DEBUG_ATOMIC("Allocated atomic state %p\n", state
);
97 drm_atomic_state_default_release(state
);
100 EXPORT_SYMBOL(drm_atomic_state_init
);
103 * drm_atomic_state_alloc - allocate atomic state
106 * This allocates an empty atomic state to track updates.
108 struct drm_atomic_state
*
109 drm_atomic_state_alloc(struct drm_device
*dev
)
111 struct drm_mode_config
*config
= &dev
->mode_config
;
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 if (state
->crtcs
[i
].commit
) {
168 kfree(state
->crtcs
[i
].commit
->event
);
169 state
->crtcs
[i
].commit
->event
= NULL
;
170 drm_crtc_commit_put(state
->crtcs
[i
].commit
);
173 state
->crtcs
[i
].commit
= NULL
;
174 state
->crtcs
[i
].ptr
= NULL
;
175 state
->crtcs
[i
].state
= NULL
;
178 for (i
= 0; i
< config
->num_total_plane
; i
++) {
179 struct drm_plane
*plane
= state
->planes
[i
].ptr
;
184 plane
->funcs
->atomic_destroy_state(plane
,
185 state
->planes
[i
].state
);
186 state
->planes
[i
].ptr
= NULL
;
187 state
->planes
[i
].state
= NULL
;
190 for (i
= 0; i
< state
->num_private_objs
; i
++) {
191 void *obj_state
= state
->private_objs
[i
].obj_state
;
193 state
->private_objs
[i
].funcs
->destroy_state(obj_state
);
194 state
->private_objs
[i
].obj
= NULL
;
195 state
->private_objs
[i
].obj_state
= NULL
;
196 state
->private_objs
[i
].funcs
= NULL
;
198 state
->num_private_objs
= 0;
201 EXPORT_SYMBOL(drm_atomic_state_default_clear
);
204 * drm_atomic_state_clear - clear state object
205 * @state: atomic state
207 * When the w/w mutex algorithm detects a deadlock we need to back off and drop
208 * all locks. So someone else could sneak in and change the current modeset
209 * configuration. Which means that all the state assembled in @state is no
210 * longer an atomic update to the current state, but to some arbitrary earlier
211 * state. Which could break assumptions the driver's
212 * &drm_mode_config_funcs.atomic_check likely relies on.
214 * Hence we must clear all cached state and completely start over, using this
217 void drm_atomic_state_clear(struct drm_atomic_state
*state
)
219 struct drm_device
*dev
= state
->dev
;
220 struct drm_mode_config
*config
= &dev
->mode_config
;
222 if (config
->funcs
->atomic_state_clear
)
223 config
->funcs
->atomic_state_clear(state
);
225 drm_atomic_state_default_clear(state
);
227 EXPORT_SYMBOL(drm_atomic_state_clear
);
230 * __drm_atomic_state_free - free all memory for an atomic state
231 * @ref: This atomic state to deallocate
233 * This frees all memory associated with an atomic state, including all the
234 * per-object state for planes, crtcs and connectors.
236 void __drm_atomic_state_free(struct kref
*ref
)
238 struct drm_atomic_state
*state
= container_of(ref
, typeof(*state
), ref
);
239 struct drm_mode_config
*config
= &state
->dev
->mode_config
;
241 drm_atomic_state_clear(state
);
243 DRM_DEBUG_ATOMIC("Freeing atomic state %p\n", state
);
245 if (config
->funcs
->atomic_state_free
) {
246 config
->funcs
->atomic_state_free(state
);
248 drm_atomic_state_default_release(state
);
252 EXPORT_SYMBOL(__drm_atomic_state_free
);
255 * drm_atomic_get_crtc_state - get crtc state
256 * @state: global atomic state object
257 * @crtc: crtc to get state object for
259 * This function returns the crtc state for the given crtc, allocating it if
260 * needed. It will also grab the relevant crtc lock to make sure that the state
265 * Either the allocated state or the error code encoded into the pointer. When
266 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
267 * entire atomic sequence must be restarted. All other errors are fatal.
269 struct drm_crtc_state
*
270 drm_atomic_get_crtc_state(struct drm_atomic_state
*state
,
271 struct drm_crtc
*crtc
)
273 int ret
, index
= drm_crtc_index(crtc
);
274 struct drm_crtc_state
*crtc_state
;
276 WARN_ON(!state
->acquire_ctx
);
278 crtc_state
= drm_atomic_get_existing_crtc_state(state
, crtc
);
282 ret
= drm_modeset_lock(&crtc
->mutex
, state
->acquire_ctx
);
286 crtc_state
= crtc
->funcs
->atomic_duplicate_state(crtc
);
288 return ERR_PTR(-ENOMEM
);
290 state
->crtcs
[index
].state
= crtc_state
;
291 state
->crtcs
[index
].old_state
= crtc
->state
;
292 state
->crtcs
[index
].new_state
= crtc_state
;
293 state
->crtcs
[index
].ptr
= crtc
;
294 crtc_state
->state
= state
;
296 DRM_DEBUG_ATOMIC("Added [CRTC:%d:%s] %p state to %p\n",
297 crtc
->base
.id
, crtc
->name
, crtc_state
, state
);
301 EXPORT_SYMBOL(drm_atomic_get_crtc_state
);
303 static void set_out_fence_for_crtc(struct drm_atomic_state
*state
,
304 struct drm_crtc
*crtc
, s32 __user
*fence_ptr
)
306 state
->crtcs
[drm_crtc_index(crtc
)].out_fence_ptr
= fence_ptr
;
309 static s32 __user
*get_out_fence_for_crtc(struct drm_atomic_state
*state
,
310 struct drm_crtc
*crtc
)
312 s32 __user
*fence_ptr
;
314 fence_ptr
= state
->crtcs
[drm_crtc_index(crtc
)].out_fence_ptr
;
315 state
->crtcs
[drm_crtc_index(crtc
)].out_fence_ptr
= NULL
;
321 * drm_atomic_set_mode_for_crtc - set mode for CRTC
322 * @state: the CRTC whose incoming state to update
323 * @mode: kernel-internal mode to use for the CRTC, or NULL to disable
325 * Set a mode (originating from the kernel) on the desired CRTC state and update
326 * the enable property.
329 * Zero on success, error code on failure. Cannot return -EDEADLK.
331 int drm_atomic_set_mode_for_crtc(struct drm_crtc_state
*state
,
332 const struct drm_display_mode
*mode
)
334 struct drm_mode_modeinfo umode
;
336 /* Early return for no change. */
337 if (mode
&& memcmp(&state
->mode
, mode
, sizeof(*mode
)) == 0)
340 drm_property_blob_put(state
->mode_blob
);
341 state
->mode_blob
= NULL
;
344 drm_mode_convert_to_umode(&umode
, mode
);
346 drm_property_create_blob(state
->crtc
->dev
,
349 if (IS_ERR(state
->mode_blob
))
350 return PTR_ERR(state
->mode_blob
);
352 drm_mode_copy(&state
->mode
, mode
);
353 state
->enable
= true;
354 DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n",
357 memset(&state
->mode
, 0, sizeof(state
->mode
));
358 state
->enable
= false;
359 DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n",
365 EXPORT_SYMBOL(drm_atomic_set_mode_for_crtc
);
368 * drm_atomic_set_mode_prop_for_crtc - set mode for CRTC
369 * @state: the CRTC whose incoming state to update
370 * @blob: pointer to blob property to use for mode
372 * Set a mode (originating from a blob property) on the desired CRTC state.
373 * This function will take a reference on the blob property for the CRTC state,
374 * and release the reference held on the state's existing mode property, if any
378 * Zero on success, error code on failure. Cannot return -EDEADLK.
380 int drm_atomic_set_mode_prop_for_crtc(struct drm_crtc_state
*state
,
381 struct drm_property_blob
*blob
)
383 if (blob
== state
->mode_blob
)
386 drm_property_blob_put(state
->mode_blob
);
387 state
->mode_blob
= NULL
;
389 memset(&state
->mode
, 0, sizeof(state
->mode
));
392 if (blob
->length
!= sizeof(struct drm_mode_modeinfo
) ||
393 drm_mode_convert_umode(&state
->mode
,
394 (const struct drm_mode_modeinfo
*)
398 state
->mode_blob
= drm_property_blob_get(blob
);
399 state
->enable
= true;
400 DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n",
401 state
->mode
.name
, state
);
403 state
->enable
= false;
404 DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n",
410 EXPORT_SYMBOL(drm_atomic_set_mode_prop_for_crtc
);
413 * drm_atomic_replace_property_blob - replace a blob property
414 * @blob: a pointer to the member blob to be replaced
415 * @new_blob: the new blob to replace with
416 * @replaced: whether the blob has been replaced
419 * Zero on success, error code on failure
422 drm_atomic_replace_property_blob(struct drm_property_blob
**blob
,
423 struct drm_property_blob
*new_blob
,
426 struct drm_property_blob
*old_blob
= *blob
;
428 if (old_blob
== new_blob
)
431 drm_property_blob_put(old_blob
);
433 drm_property_blob_get(new_blob
);
441 drm_atomic_replace_property_blob_from_id(struct drm_device
*dev
,
442 struct drm_property_blob
**blob
,
444 ssize_t expected_size
,
447 struct drm_property_blob
*new_blob
= NULL
;
450 new_blob
= drm_property_lookup_blob(dev
, blob_id
);
451 if (new_blob
== NULL
)
454 if (expected_size
> 0 && expected_size
!= new_blob
->length
) {
455 drm_property_blob_put(new_blob
);
460 drm_atomic_replace_property_blob(blob
, new_blob
, replaced
);
461 drm_property_blob_put(new_blob
);
467 * drm_atomic_crtc_set_property - set property on CRTC
468 * @crtc: the drm CRTC to set a property on
469 * @state: the state object to update with the new property value
470 * @property: the property to set
471 * @val: the new property value
473 * This function handles generic/core properties and calls out to driver's
474 * &drm_crtc_funcs.atomic_set_property for driver properties. To ensure
475 * consistent behavior you must call this function rather than the driver hook
479 * Zero on success, error code on failure
481 int drm_atomic_crtc_set_property(struct drm_crtc
*crtc
,
482 struct drm_crtc_state
*state
, struct drm_property
*property
,
485 struct drm_device
*dev
= crtc
->dev
;
486 struct drm_mode_config
*config
= &dev
->mode_config
;
487 bool replaced
= false;
490 if (property
== config
->prop_active
)
492 else if (property
== config
->prop_mode_id
) {
493 struct drm_property_blob
*mode
=
494 drm_property_lookup_blob(dev
, val
);
495 ret
= drm_atomic_set_mode_prop_for_crtc(state
, mode
);
496 drm_property_blob_put(mode
);
498 } else if (property
== config
->degamma_lut_property
) {
499 ret
= drm_atomic_replace_property_blob_from_id(dev
,
504 state
->color_mgmt_changed
|= replaced
;
506 } else if (property
== config
->ctm_property
) {
507 ret
= drm_atomic_replace_property_blob_from_id(dev
,
510 sizeof(struct drm_color_ctm
),
512 state
->color_mgmt_changed
|= replaced
;
514 } else if (property
== config
->gamma_lut_property
) {
515 ret
= drm_atomic_replace_property_blob_from_id(dev
,
520 state
->color_mgmt_changed
|= replaced
;
522 } else if (property
== config
->prop_out_fence_ptr
) {
523 s32 __user
*fence_ptr
= u64_to_user_ptr(val
);
528 if (put_user(-1, fence_ptr
))
531 set_out_fence_for_crtc(state
->state
, crtc
, fence_ptr
);
532 } else if (crtc
->funcs
->atomic_set_property
)
533 return crtc
->funcs
->atomic_set_property(crtc
, state
, property
, val
);
539 EXPORT_SYMBOL(drm_atomic_crtc_set_property
);
542 * drm_atomic_crtc_get_property - get property value from CRTC state
543 * @crtc: the drm CRTC to set a property on
544 * @state: the state object to get the property value from
545 * @property: the property to set
546 * @val: return location for the property value
548 * This function handles generic/core properties and calls out to driver's
549 * &drm_crtc_funcs.atomic_get_property for driver properties. To ensure
550 * consistent behavior you must call this function rather than the driver hook
554 * Zero on success, error code on failure
557 drm_atomic_crtc_get_property(struct drm_crtc
*crtc
,
558 const struct drm_crtc_state
*state
,
559 struct drm_property
*property
, uint64_t *val
)
561 struct drm_device
*dev
= crtc
->dev
;
562 struct drm_mode_config
*config
= &dev
->mode_config
;
564 if (property
== config
->prop_active
)
565 *val
= state
->active
;
566 else if (property
== config
->prop_mode_id
)
567 *val
= (state
->mode_blob
) ? state
->mode_blob
->base
.id
: 0;
568 else if (property
== config
->degamma_lut_property
)
569 *val
= (state
->degamma_lut
) ? state
->degamma_lut
->base
.id
: 0;
570 else if (property
== config
->ctm_property
)
571 *val
= (state
->ctm
) ? state
->ctm
->base
.id
: 0;
572 else if (property
== config
->gamma_lut_property
)
573 *val
= (state
->gamma_lut
) ? state
->gamma_lut
->base
.id
: 0;
574 else if (property
== config
->prop_out_fence_ptr
)
576 else if (crtc
->funcs
->atomic_get_property
)
577 return crtc
->funcs
->atomic_get_property(crtc
, state
, property
, val
);
585 * drm_atomic_crtc_check - check crtc state
586 * @crtc: crtc to check
587 * @state: crtc state to check
589 * Provides core sanity checks for crtc state.
592 * Zero on success, error code on failure
594 static int drm_atomic_crtc_check(struct drm_crtc
*crtc
,
595 struct drm_crtc_state
*state
)
597 /* NOTE: we explicitly don't enforce constraints such as primary
598 * layer covering entire screen, since that is something we want
599 * to allow (on hw that supports it). For hw that does not, it
600 * should be checked in driver's crtc->atomic_check() vfunc.
602 * TODO: Add generic modeset state checks once we support those.
605 if (state
->active
&& !state
->enable
) {
606 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active without enabled\n",
607 crtc
->base
.id
, crtc
->name
);
611 /* The state->enable vs. state->mode_blob checks can be WARN_ON,
612 * as this is a kernel-internal detail that userspace should never
613 * be able to trigger. */
614 if (drm_core_check_feature(crtc
->dev
, DRIVER_ATOMIC
) &&
615 WARN_ON(state
->enable
&& !state
->mode_blob
)) {
616 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled without mode blob\n",
617 crtc
->base
.id
, crtc
->name
);
621 if (drm_core_check_feature(crtc
->dev
, DRIVER_ATOMIC
) &&
622 WARN_ON(!state
->enable
&& state
->mode_blob
)) {
623 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] disabled with mode blob\n",
624 crtc
->base
.id
, crtc
->name
);
629 * Reject event generation for when a CRTC is off and stays off.
630 * It wouldn't be hard to implement this, but userspace has a track
631 * record of happily burning through 100% cpu (or worse, crash) when the
632 * display pipe is suspended. To avoid all that fun just reject updates
633 * that ask for events since likely that indicates a bug in the
634 * compositor's drawing loop. This is consistent with the vblank IOCTL
635 * and legacy page_flip IOCTL which also reject service on a disabled
638 if (state
->event
&& !state
->active
&& !crtc
->state
->active
) {
639 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] requesting event but off\n",
640 crtc
->base
.id
, crtc
->name
);
647 static void drm_atomic_crtc_print_state(struct drm_printer
*p
,
648 const struct drm_crtc_state
*state
)
650 struct drm_crtc
*crtc
= state
->crtc
;
652 drm_printf(p
, "crtc[%u]: %s\n", crtc
->base
.id
, crtc
->name
);
653 drm_printf(p
, "\tenable=%d\n", state
->enable
);
654 drm_printf(p
, "\tactive=%d\n", state
->active
);
655 drm_printf(p
, "\tplanes_changed=%d\n", state
->planes_changed
);
656 drm_printf(p
, "\tmode_changed=%d\n", state
->mode_changed
);
657 drm_printf(p
, "\tactive_changed=%d\n", state
->active_changed
);
658 drm_printf(p
, "\tconnectors_changed=%d\n", state
->connectors_changed
);
659 drm_printf(p
, "\tcolor_mgmt_changed=%d\n", state
->color_mgmt_changed
);
660 drm_printf(p
, "\tplane_mask=%x\n", state
->plane_mask
);
661 drm_printf(p
, "\tconnector_mask=%x\n", state
->connector_mask
);
662 drm_printf(p
, "\tencoder_mask=%x\n", state
->encoder_mask
);
663 drm_printf(p
, "\tmode: " DRM_MODE_FMT
"\n", DRM_MODE_ARG(&state
->mode
));
665 if (crtc
->funcs
->atomic_print_state
)
666 crtc
->funcs
->atomic_print_state(p
, state
);
670 * drm_atomic_get_plane_state - get plane state
671 * @state: global atomic state object
672 * @plane: plane to get state object for
674 * This function returns the plane state for the given plane, allocating it if
675 * needed. It will also grab the relevant plane lock to make sure that the state
680 * Either the allocated state or the error code encoded into the pointer. When
681 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
682 * entire atomic sequence must be restarted. All other errors are fatal.
684 struct drm_plane_state
*
685 drm_atomic_get_plane_state(struct drm_atomic_state
*state
,
686 struct drm_plane
*plane
)
688 int ret
, index
= drm_plane_index(plane
);
689 struct drm_plane_state
*plane_state
;
691 WARN_ON(!state
->acquire_ctx
);
693 plane_state
= drm_atomic_get_existing_plane_state(state
, plane
);
697 ret
= drm_modeset_lock(&plane
->mutex
, state
->acquire_ctx
);
701 plane_state
= plane
->funcs
->atomic_duplicate_state(plane
);
703 return ERR_PTR(-ENOMEM
);
705 state
->planes
[index
].state
= plane_state
;
706 state
->planes
[index
].ptr
= plane
;
707 state
->planes
[index
].old_state
= plane
->state
;
708 state
->planes
[index
].new_state
= plane_state
;
709 plane_state
->state
= state
;
711 DRM_DEBUG_ATOMIC("Added [PLANE:%d:%s] %p state to %p\n",
712 plane
->base
.id
, plane
->name
, plane_state
, state
);
714 if (plane_state
->crtc
) {
715 struct drm_crtc_state
*crtc_state
;
717 crtc_state
= drm_atomic_get_crtc_state(state
,
719 if (IS_ERR(crtc_state
))
720 return ERR_CAST(crtc_state
);
725 EXPORT_SYMBOL(drm_atomic_get_plane_state
);
728 * drm_atomic_plane_set_property - set property on plane
729 * @plane: the drm plane to set a property on
730 * @state: the state object to update with the new property value
731 * @property: the property to set
732 * @val: the new property value
734 * This function handles generic/core properties and calls out to driver's
735 * &drm_plane_funcs.atomic_set_property for driver properties. To ensure
736 * consistent behavior you must call this function rather than the driver hook
740 * Zero on success, error code on failure
742 int drm_atomic_plane_set_property(struct drm_plane
*plane
,
743 struct drm_plane_state
*state
, struct drm_property
*property
,
746 struct drm_device
*dev
= plane
->dev
;
747 struct drm_mode_config
*config
= &dev
->mode_config
;
749 if (property
== config
->prop_fb_id
) {
750 struct drm_framebuffer
*fb
= drm_framebuffer_lookup(dev
, val
);
751 drm_atomic_set_fb_for_plane(state
, fb
);
753 drm_framebuffer_put(fb
);
754 } else if (property
== config
->prop_in_fence_fd
) {
758 if (U642I64(val
) == -1)
761 state
->fence
= sync_file_get_fence(val
);
765 } else if (property
== config
->prop_crtc_id
) {
766 struct drm_crtc
*crtc
= drm_crtc_find(dev
, val
);
767 return drm_atomic_set_crtc_for_plane(state
, crtc
);
768 } else if (property
== config
->prop_crtc_x
) {
769 state
->crtc_x
= U642I64(val
);
770 } else if (property
== config
->prop_crtc_y
) {
771 state
->crtc_y
= U642I64(val
);
772 } else if (property
== config
->prop_crtc_w
) {
774 } else if (property
== config
->prop_crtc_h
) {
776 } else if (property
== config
->prop_src_x
) {
778 } else if (property
== config
->prop_src_y
) {
780 } else if (property
== config
->prop_src_w
) {
782 } else if (property
== config
->prop_src_h
) {
784 } else if (property
== plane
->rotation_property
) {
785 if (!is_power_of_2(val
& DRM_MODE_ROTATE_MASK
))
787 state
->rotation
= val
;
788 } else if (property
== plane
->zpos_property
) {
790 } else if (plane
->funcs
->atomic_set_property
) {
791 return plane
->funcs
->atomic_set_property(plane
, state
,
799 EXPORT_SYMBOL(drm_atomic_plane_set_property
);
802 * drm_atomic_plane_get_property - get property value from plane state
803 * @plane: the drm plane to set a property on
804 * @state: the state object to get the property value from
805 * @property: the property to set
806 * @val: return location for the property value
808 * This function handles generic/core properties and calls out to driver's
809 * &drm_plane_funcs.atomic_get_property for driver properties. To ensure
810 * consistent behavior you must call this function rather than the driver hook
814 * Zero on success, error code on failure
817 drm_atomic_plane_get_property(struct drm_plane
*plane
,
818 const struct drm_plane_state
*state
,
819 struct drm_property
*property
, uint64_t *val
)
821 struct drm_device
*dev
= plane
->dev
;
822 struct drm_mode_config
*config
= &dev
->mode_config
;
824 if (property
== config
->prop_fb_id
) {
825 *val
= (state
->fb
) ? state
->fb
->base
.id
: 0;
826 } else if (property
== config
->prop_in_fence_fd
) {
828 } else if (property
== config
->prop_crtc_id
) {
829 *val
= (state
->crtc
) ? state
->crtc
->base
.id
: 0;
830 } else if (property
== config
->prop_crtc_x
) {
831 *val
= I642U64(state
->crtc_x
);
832 } else if (property
== config
->prop_crtc_y
) {
833 *val
= I642U64(state
->crtc_y
);
834 } else if (property
== config
->prop_crtc_w
) {
835 *val
= state
->crtc_w
;
836 } else if (property
== config
->prop_crtc_h
) {
837 *val
= state
->crtc_h
;
838 } else if (property
== config
->prop_src_x
) {
840 } else if (property
== config
->prop_src_y
) {
842 } else if (property
== config
->prop_src_w
) {
844 } else if (property
== config
->prop_src_h
) {
846 } else if (property
== plane
->rotation_property
) {
847 *val
= state
->rotation
;
848 } else if (property
== plane
->zpos_property
) {
850 } else if (plane
->funcs
->atomic_get_property
) {
851 return plane
->funcs
->atomic_get_property(plane
, state
, property
, val
);
860 plane_switching_crtc(struct drm_atomic_state
*state
,
861 struct drm_plane
*plane
,
862 struct drm_plane_state
*plane_state
)
864 if (!plane
->state
->crtc
|| !plane_state
->crtc
)
867 if (plane
->state
->crtc
== plane_state
->crtc
)
870 /* This could be refined, but currently there's no helper or driver code
871 * to implement direct switching of active planes nor userspace to take
872 * advantage of more direct plane switching without the intermediate
879 * drm_atomic_plane_check - check plane state
880 * @plane: plane to check
881 * @state: plane state to check
883 * Provides core sanity checks for plane state.
886 * Zero on success, error code on failure
888 static int drm_atomic_plane_check(struct drm_plane
*plane
,
889 struct drm_plane_state
*state
)
891 unsigned int fb_width
, fb_height
;
894 /* either *both* CRTC and FB must be set, or neither */
895 if (WARN_ON(state
->crtc
&& !state
->fb
)) {
896 DRM_DEBUG_ATOMIC("CRTC set but no FB\n");
898 } else if (WARN_ON(state
->fb
&& !state
->crtc
)) {
899 DRM_DEBUG_ATOMIC("FB set but no CRTC\n");
903 /* if disabled, we don't care about the rest of the state: */
907 /* Check whether this plane is usable on this CRTC */
908 if (!(plane
->possible_crtcs
& drm_crtc_mask(state
->crtc
))) {
909 DRM_DEBUG_ATOMIC("Invalid crtc for plane\n");
913 /* Check whether this plane supports the fb pixel format. */
914 ret
= drm_plane_check_pixel_format(plane
, state
->fb
->format
->format
);
916 struct drm_format_name_buf format_name
;
917 DRM_DEBUG_ATOMIC("Invalid pixel format %s\n",
918 drm_get_format_name(state
->fb
->format
->format
,
923 /* Give drivers some help against integer overflows */
924 if (state
->crtc_w
> INT_MAX
||
925 state
->crtc_x
> INT_MAX
- (int32_t) state
->crtc_w
||
926 state
->crtc_h
> INT_MAX
||
927 state
->crtc_y
> INT_MAX
- (int32_t) state
->crtc_h
) {
928 DRM_DEBUG_ATOMIC("Invalid CRTC coordinates %ux%u+%d+%d\n",
929 state
->crtc_w
, state
->crtc_h
,
930 state
->crtc_x
, state
->crtc_y
);
934 fb_width
= state
->fb
->width
<< 16;
935 fb_height
= state
->fb
->height
<< 16;
937 /* Make sure source coordinates are inside the fb. */
938 if (state
->src_w
> fb_width
||
939 state
->src_x
> fb_width
- state
->src_w
||
940 state
->src_h
> fb_height
||
941 state
->src_y
> fb_height
- state
->src_h
) {
942 DRM_DEBUG_ATOMIC("Invalid source coordinates "
943 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
944 state
->src_w
>> 16, ((state
->src_w
& 0xffff) * 15625) >> 10,
945 state
->src_h
>> 16, ((state
->src_h
& 0xffff) * 15625) >> 10,
946 state
->src_x
>> 16, ((state
->src_x
& 0xffff) * 15625) >> 10,
947 state
->src_y
>> 16, ((state
->src_y
& 0xffff) * 15625) >> 10);
951 if (plane_switching_crtc(state
->state
, plane
, state
)) {
952 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] switching CRTC directly\n",
953 plane
->base
.id
, plane
->name
);
960 static void drm_atomic_plane_print_state(struct drm_printer
*p
,
961 const struct drm_plane_state
*state
)
963 struct drm_plane
*plane
= state
->plane
;
964 struct drm_rect src
= drm_plane_state_src(state
);
965 struct drm_rect dest
= drm_plane_state_dest(state
);
967 drm_printf(p
, "plane[%u]: %s\n", plane
->base
.id
, plane
->name
);
968 drm_printf(p
, "\tcrtc=%s\n", state
->crtc
? state
->crtc
->name
: "(null)");
969 drm_printf(p
, "\tfb=%u\n", state
->fb
? state
->fb
->base
.id
: 0);
971 struct drm_framebuffer
*fb
= state
->fb
;
972 int i
, n
= fb
->format
->num_planes
;
973 struct drm_format_name_buf format_name
;
975 drm_printf(p
, "\t\tformat=%s\n",
976 drm_get_format_name(fb
->format
->format
, &format_name
));
977 drm_printf(p
, "\t\t\tmodifier=0x%llx\n", fb
->modifier
);
978 drm_printf(p
, "\t\tsize=%dx%d\n", fb
->width
, fb
->height
);
979 drm_printf(p
, "\t\tlayers:\n");
980 for (i
= 0; i
< n
; i
++) {
981 drm_printf(p
, "\t\t\tpitch[%d]=%u\n", i
, fb
->pitches
[i
]);
982 drm_printf(p
, "\t\t\toffset[%d]=%u\n", i
, fb
->offsets
[i
]);
985 drm_printf(p
, "\tcrtc-pos=" DRM_RECT_FMT
"\n", DRM_RECT_ARG(&dest
));
986 drm_printf(p
, "\tsrc-pos=" DRM_RECT_FP_FMT
"\n", DRM_RECT_FP_ARG(&src
));
987 drm_printf(p
, "\trotation=%x\n", state
->rotation
);
989 if (plane
->funcs
->atomic_print_state
)
990 plane
->funcs
->atomic_print_state(p
, state
);
994 * drm_atomic_get_private_obj_state - get private object state
995 * @state: global atomic state
996 * @obj: private object to get the state for
997 * @funcs: pointer to the struct of function pointers that identify the object
1000 * This function returns the private object state for the given private object,
1001 * allocating the state if needed. It does not grab any locks as the caller is
1002 * expected to care of any required locking.
1006 * Either the allocated state or the error code encoded into a pointer.
1009 drm_atomic_get_private_obj_state(struct drm_atomic_state
*state
, void *obj
,
1010 const struct drm_private_state_funcs
*funcs
)
1012 int index
, num_objs
, i
;
1014 struct __drm_private_objs_state
*arr
;
1016 for (i
= 0; i
< state
->num_private_objs
; i
++)
1017 if (obj
== state
->private_objs
[i
].obj
&&
1018 state
->private_objs
[i
].obj_state
)
1019 return state
->private_objs
[i
].obj_state
;
1021 num_objs
= state
->num_private_objs
+ 1;
1022 size
= sizeof(*state
->private_objs
) * num_objs
;
1023 arr
= krealloc(state
->private_objs
, size
, GFP_KERNEL
);
1025 return ERR_PTR(-ENOMEM
);
1027 state
->private_objs
= arr
;
1028 index
= state
->num_private_objs
;
1029 memset(&state
->private_objs
[index
], 0, sizeof(*state
->private_objs
));
1031 state
->private_objs
[index
].obj_state
= funcs
->duplicate_state(state
, obj
);
1032 if (!state
->private_objs
[index
].obj_state
)
1033 return ERR_PTR(-ENOMEM
);
1035 state
->private_objs
[index
].obj
= obj
;
1036 state
->private_objs
[index
].funcs
= funcs
;
1037 state
->num_private_objs
= num_objs
;
1039 DRM_DEBUG_ATOMIC("Added new private object state %p to %p\n",
1040 state
->private_objs
[index
].obj_state
, state
);
1042 return state
->private_objs
[index
].obj_state
;
1044 EXPORT_SYMBOL(drm_atomic_get_private_obj_state
);
1047 * drm_atomic_get_connector_state - get connector state
1048 * @state: global atomic state object
1049 * @connector: connector to get state object for
1051 * This function returns the connector state for the given connector,
1052 * allocating it if needed. It will also grab the relevant connector lock to
1053 * make sure that the state is consistent.
1057 * Either the allocated state or the error code encoded into the pointer. When
1058 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
1059 * entire atomic sequence must be restarted. All other errors are fatal.
1061 struct drm_connector_state
*
1062 drm_atomic_get_connector_state(struct drm_atomic_state
*state
,
1063 struct drm_connector
*connector
)
1066 struct drm_mode_config
*config
= &connector
->dev
->mode_config
;
1067 struct drm_connector_state
*connector_state
;
1069 WARN_ON(!state
->acquire_ctx
);
1071 ret
= drm_modeset_lock(&config
->connection_mutex
, state
->acquire_ctx
);
1073 return ERR_PTR(ret
);
1075 index
= drm_connector_index(connector
);
1077 if (index
>= state
->num_connector
) {
1078 struct __drm_connnectors_state
*c
;
1079 int alloc
= max(index
+ 1, config
->num_connector
);
1081 c
= krealloc(state
->connectors
, alloc
* sizeof(*state
->connectors
), GFP_KERNEL
);
1083 return ERR_PTR(-ENOMEM
);
1085 state
->connectors
= c
;
1086 memset(&state
->connectors
[state
->num_connector
], 0,
1087 sizeof(*state
->connectors
) * (alloc
- state
->num_connector
));
1089 state
->num_connector
= alloc
;
1092 if (state
->connectors
[index
].state
)
1093 return state
->connectors
[index
].state
;
1095 connector_state
= connector
->funcs
->atomic_duplicate_state(connector
);
1096 if (!connector_state
)
1097 return ERR_PTR(-ENOMEM
);
1099 drm_connector_get(connector
);
1100 state
->connectors
[index
].state
= connector_state
;
1101 state
->connectors
[index
].old_state
= connector
->state
;
1102 state
->connectors
[index
].new_state
= connector_state
;
1103 state
->connectors
[index
].ptr
= connector
;
1104 connector_state
->state
= state
;
1106 DRM_DEBUG_ATOMIC("Added [CONNECTOR:%d:%s] %p state to %p\n",
1107 connector
->base
.id
, connector
->name
,
1108 connector_state
, state
);
1110 if (connector_state
->crtc
) {
1111 struct drm_crtc_state
*crtc_state
;
1113 crtc_state
= drm_atomic_get_crtc_state(state
,
1114 connector_state
->crtc
);
1115 if (IS_ERR(crtc_state
))
1116 return ERR_CAST(crtc_state
);
1119 return connector_state
;
1121 EXPORT_SYMBOL(drm_atomic_get_connector_state
);
1124 * drm_atomic_connector_set_property - set property on connector.
1125 * @connector: the drm connector to set a property on
1126 * @state: the state object to update with the new property value
1127 * @property: the property to set
1128 * @val: the new property value
1130 * This function handles generic/core properties and calls out to driver's
1131 * &drm_connector_funcs.atomic_set_property for driver properties. To ensure
1132 * consistent behavior you must call this function rather than the driver hook
1136 * Zero on success, error code on failure
1138 int drm_atomic_connector_set_property(struct drm_connector
*connector
,
1139 struct drm_connector_state
*state
, struct drm_property
*property
,
1142 struct drm_device
*dev
= connector
->dev
;
1143 struct drm_mode_config
*config
= &dev
->mode_config
;
1145 if (property
== config
->prop_crtc_id
) {
1146 struct drm_crtc
*crtc
= drm_crtc_find(dev
, val
);
1147 return drm_atomic_set_crtc_for_connector(state
, crtc
);
1148 } else if (property
== config
->dpms_property
) {
1149 /* setting DPMS property requires special handling, which
1150 * is done in legacy setprop path for us. Disallow (for
1151 * now?) atomic writes to DPMS property:
1154 } else if (property
== config
->tv_select_subconnector_property
) {
1155 state
->tv
.subconnector
= val
;
1156 } else if (property
== config
->tv_left_margin_property
) {
1157 state
->tv
.margins
.left
= val
;
1158 } else if (property
== config
->tv_right_margin_property
) {
1159 state
->tv
.margins
.right
= val
;
1160 } else if (property
== config
->tv_top_margin_property
) {
1161 state
->tv
.margins
.top
= val
;
1162 } else if (property
== config
->tv_bottom_margin_property
) {
1163 state
->tv
.margins
.bottom
= val
;
1164 } else if (property
== config
->tv_mode_property
) {
1165 state
->tv
.mode
= val
;
1166 } else if (property
== config
->tv_brightness_property
) {
1167 state
->tv
.brightness
= val
;
1168 } else if (property
== config
->tv_contrast_property
) {
1169 state
->tv
.contrast
= val
;
1170 } else if (property
== config
->tv_flicker_reduction_property
) {
1171 state
->tv
.flicker_reduction
= val
;
1172 } else if (property
== config
->tv_overscan_property
) {
1173 state
->tv
.overscan
= val
;
1174 } else if (property
== config
->tv_saturation_property
) {
1175 state
->tv
.saturation
= val
;
1176 } else if (property
== config
->tv_hue_property
) {
1177 state
->tv
.hue
= val
;
1178 } else if (property
== config
->link_status_property
) {
1179 /* Never downgrade from GOOD to BAD on userspace's request here,
1180 * only hw issues can do that.
1182 * For an atomic property the userspace doesn't need to be able
1183 * to understand all the properties, but needs to be able to
1184 * restore the state it wants on VT switch. So if the userspace
1185 * tries to change the link_status from GOOD to BAD, driver
1186 * silently rejects it and returns a 0. This prevents userspace
1187 * from accidently breaking the display when it restores the
1190 if (state
->link_status
!= DRM_LINK_STATUS_GOOD
)
1191 state
->link_status
= val
;
1192 } else if (property
== config
->aspect_ratio_property
) {
1193 state
->picture_aspect_ratio
= val
;
1194 } else if (property
== connector
->scaling_mode_property
) {
1195 state
->scaling_mode
= val
;
1196 } else if (connector
->funcs
->atomic_set_property
) {
1197 return connector
->funcs
->atomic_set_property(connector
,
1198 state
, property
, val
);
1205 EXPORT_SYMBOL(drm_atomic_connector_set_property
);
1207 static void drm_atomic_connector_print_state(struct drm_printer
*p
,
1208 const struct drm_connector_state
*state
)
1210 struct drm_connector
*connector
= state
->connector
;
1212 drm_printf(p
, "connector[%u]: %s\n", connector
->base
.id
, connector
->name
);
1213 drm_printf(p
, "\tcrtc=%s\n", state
->crtc
? state
->crtc
->name
: "(null)");
1215 if (connector
->funcs
->atomic_print_state
)
1216 connector
->funcs
->atomic_print_state(p
, state
);
1220 * drm_atomic_connector_get_property - get property value from connector state
1221 * @connector: the drm connector to set a property on
1222 * @state: the state object to get the property value from
1223 * @property: the property to set
1224 * @val: return location for the property value
1226 * This function handles generic/core properties and calls out to driver's
1227 * &drm_connector_funcs.atomic_get_property for driver properties. To ensure
1228 * consistent behavior you must call this function rather than the driver hook
1232 * Zero on success, error code on failure
1235 drm_atomic_connector_get_property(struct drm_connector
*connector
,
1236 const struct drm_connector_state
*state
,
1237 struct drm_property
*property
, uint64_t *val
)
1239 struct drm_device
*dev
= connector
->dev
;
1240 struct drm_mode_config
*config
= &dev
->mode_config
;
1242 if (property
== config
->prop_crtc_id
) {
1243 *val
= (state
->crtc
) ? state
->crtc
->base
.id
: 0;
1244 } else if (property
== config
->dpms_property
) {
1245 *val
= connector
->dpms
;
1246 } else if (property
== config
->tv_select_subconnector_property
) {
1247 *val
= state
->tv
.subconnector
;
1248 } else if (property
== config
->tv_left_margin_property
) {
1249 *val
= state
->tv
.margins
.left
;
1250 } else if (property
== config
->tv_right_margin_property
) {
1251 *val
= state
->tv
.margins
.right
;
1252 } else if (property
== config
->tv_top_margin_property
) {
1253 *val
= state
->tv
.margins
.top
;
1254 } else if (property
== config
->tv_bottom_margin_property
) {
1255 *val
= state
->tv
.margins
.bottom
;
1256 } else if (property
== config
->tv_mode_property
) {
1257 *val
= state
->tv
.mode
;
1258 } else if (property
== config
->tv_brightness_property
) {
1259 *val
= state
->tv
.brightness
;
1260 } else if (property
== config
->tv_contrast_property
) {
1261 *val
= state
->tv
.contrast
;
1262 } else if (property
== config
->tv_flicker_reduction_property
) {
1263 *val
= state
->tv
.flicker_reduction
;
1264 } else if (property
== config
->tv_overscan_property
) {
1265 *val
= state
->tv
.overscan
;
1266 } else if (property
== config
->tv_saturation_property
) {
1267 *val
= state
->tv
.saturation
;
1268 } else if (property
== config
->tv_hue_property
) {
1269 *val
= state
->tv
.hue
;
1270 } else if (property
== config
->link_status_property
) {
1271 *val
= state
->link_status
;
1272 } else if (property
== config
->aspect_ratio_property
) {
1273 *val
= state
->picture_aspect_ratio
;
1274 } else if (property
== connector
->scaling_mode_property
) {
1275 *val
= state
->scaling_mode
;
1276 } else if (connector
->funcs
->atomic_get_property
) {
1277 return connector
->funcs
->atomic_get_property(connector
,
1278 state
, property
, val
);
1286 int drm_atomic_get_property(struct drm_mode_object
*obj
,
1287 struct drm_property
*property
, uint64_t *val
)
1289 struct drm_device
*dev
= property
->dev
;
1292 switch (obj
->type
) {
1293 case DRM_MODE_OBJECT_CONNECTOR
: {
1294 struct drm_connector
*connector
= obj_to_connector(obj
);
1295 WARN_ON(!drm_modeset_is_locked(&dev
->mode_config
.connection_mutex
));
1296 ret
= drm_atomic_connector_get_property(connector
,
1297 connector
->state
, property
, val
);
1300 case DRM_MODE_OBJECT_CRTC
: {
1301 struct drm_crtc
*crtc
= obj_to_crtc(obj
);
1302 WARN_ON(!drm_modeset_is_locked(&crtc
->mutex
));
1303 ret
= drm_atomic_crtc_get_property(crtc
,
1304 crtc
->state
, property
, val
);
1307 case DRM_MODE_OBJECT_PLANE
: {
1308 struct drm_plane
*plane
= obj_to_plane(obj
);
1309 WARN_ON(!drm_modeset_is_locked(&plane
->mutex
));
1310 ret
= drm_atomic_plane_get_property(plane
,
1311 plane
->state
, property
, val
);
1323 * drm_atomic_set_crtc_for_plane - set crtc for plane
1324 * @plane_state: the plane whose incoming state to update
1325 * @crtc: crtc to use for the plane
1327 * Changing the assigned crtc for a plane requires us to grab the lock and state
1328 * for the new crtc, as needed. This function takes care of all these details
1329 * besides updating the pointer in the state object itself.
1332 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1333 * then the w/w mutex code has detected a deadlock and the entire atomic
1334 * sequence must be restarted. All other errors are fatal.
1337 drm_atomic_set_crtc_for_plane(struct drm_plane_state
*plane_state
,
1338 struct drm_crtc
*crtc
)
1340 struct drm_plane
*plane
= plane_state
->plane
;
1341 struct drm_crtc_state
*crtc_state
;
1343 if (plane_state
->crtc
) {
1344 crtc_state
= drm_atomic_get_crtc_state(plane_state
->state
,
1346 if (WARN_ON(IS_ERR(crtc_state
)))
1347 return PTR_ERR(crtc_state
);
1349 crtc_state
->plane_mask
&= ~(1 << drm_plane_index(plane
));
1352 plane_state
->crtc
= crtc
;
1355 crtc_state
= drm_atomic_get_crtc_state(plane_state
->state
,
1357 if (IS_ERR(crtc_state
))
1358 return PTR_ERR(crtc_state
);
1359 crtc_state
->plane_mask
|= (1 << drm_plane_index(plane
));
1363 DRM_DEBUG_ATOMIC("Link plane state %p to [CRTC:%d:%s]\n",
1364 plane_state
, crtc
->base
.id
, crtc
->name
);
1366 DRM_DEBUG_ATOMIC("Link plane state %p to [NOCRTC]\n",
1371 EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane
);
1374 * drm_atomic_set_fb_for_plane - set framebuffer for plane
1375 * @plane_state: atomic state object for the plane
1376 * @fb: fb to use for the plane
1378 * Changing the assigned framebuffer for a plane requires us to grab a reference
1379 * to the new fb and drop the reference to the old fb, if there is one. This
1380 * function takes care of all these details besides updating the pointer in the
1381 * state object itself.
1384 drm_atomic_set_fb_for_plane(struct drm_plane_state
*plane_state
,
1385 struct drm_framebuffer
*fb
)
1388 DRM_DEBUG_ATOMIC("Set [FB:%d] for plane state %p\n",
1389 fb
->base
.id
, plane_state
);
1391 DRM_DEBUG_ATOMIC("Set [NOFB] for plane state %p\n",
1394 drm_framebuffer_assign(&plane_state
->fb
, fb
);
1396 EXPORT_SYMBOL(drm_atomic_set_fb_for_plane
);
1399 * drm_atomic_set_fence_for_plane - set fence for plane
1400 * @plane_state: atomic state object for the plane
1401 * @fence: dma_fence to use for the plane
1403 * Helper to setup the plane_state fence in case it is not set yet.
1404 * By using this drivers doesn't need to worry if the user choose
1405 * implicit or explicit fencing.
1407 * This function will not set the fence to the state if it was set
1408 * via explicit fencing interfaces on the atomic ioctl. In that case it will
1409 * drop the reference to the fence as we are not storing it anywhere.
1410 * Otherwise, if &drm_plane_state.fence is not set this function we just set it
1411 * with the received implicit fence. In both cases this function consumes a
1412 * reference for @fence.
1415 drm_atomic_set_fence_for_plane(struct drm_plane_state
*plane_state
,
1416 struct dma_fence
*fence
)
1418 if (plane_state
->fence
) {
1419 dma_fence_put(fence
);
1423 plane_state
->fence
= fence
;
1425 EXPORT_SYMBOL(drm_atomic_set_fence_for_plane
);
1428 * drm_atomic_set_crtc_for_connector - set crtc for connector
1429 * @conn_state: atomic state object for the connector
1430 * @crtc: crtc to use for the connector
1432 * Changing the assigned crtc for a connector requires us to grab the lock and
1433 * state for the new crtc, as needed. This function takes care of all these
1434 * details besides updating the pointer in the state object itself.
1437 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1438 * then the w/w mutex code has detected a deadlock and the entire atomic
1439 * sequence must be restarted. All other errors are fatal.
1442 drm_atomic_set_crtc_for_connector(struct drm_connector_state
*conn_state
,
1443 struct drm_crtc
*crtc
)
1445 struct drm_crtc_state
*crtc_state
;
1447 if (conn_state
->crtc
== crtc
)
1450 if (conn_state
->crtc
) {
1451 crtc_state
= drm_atomic_get_new_crtc_state(conn_state
->state
,
1454 crtc_state
->connector_mask
&=
1455 ~(1 << drm_connector_index(conn_state
->connector
));
1457 drm_connector_put(conn_state
->connector
);
1458 conn_state
->crtc
= NULL
;
1462 crtc_state
= drm_atomic_get_crtc_state(conn_state
->state
, crtc
);
1463 if (IS_ERR(crtc_state
))
1464 return PTR_ERR(crtc_state
);
1466 crtc_state
->connector_mask
|=
1467 1 << drm_connector_index(conn_state
->connector
);
1469 drm_connector_get(conn_state
->connector
);
1470 conn_state
->crtc
= crtc
;
1472 DRM_DEBUG_ATOMIC("Link connector state %p to [CRTC:%d:%s]\n",
1473 conn_state
, crtc
->base
.id
, crtc
->name
);
1475 DRM_DEBUG_ATOMIC("Link connector state %p to [NOCRTC]\n",
1481 EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector
);
1484 * drm_atomic_add_affected_connectors - add connectors for crtc
1485 * @state: atomic state
1488 * This function walks the current configuration and adds all connectors
1489 * currently using @crtc to the atomic configuration @state. Note that this
1490 * function must acquire the connection mutex. This can potentially cause
1491 * unneeded seralization if the update is just for the planes on one crtc. Hence
1492 * drivers and helpers should only call this when really needed (e.g. when a
1493 * full modeset needs to happen due to some change).
1496 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1497 * then the w/w mutex code has detected a deadlock and the entire atomic
1498 * sequence must be restarted. All other errors are fatal.
1501 drm_atomic_add_affected_connectors(struct drm_atomic_state
*state
,
1502 struct drm_crtc
*crtc
)
1504 struct drm_mode_config
*config
= &state
->dev
->mode_config
;
1505 struct drm_connector
*connector
;
1506 struct drm_connector_state
*conn_state
;
1507 struct drm_connector_list_iter conn_iter
;
1508 struct drm_crtc_state
*crtc_state
;
1511 crtc_state
= drm_atomic_get_crtc_state(state
, crtc
);
1512 if (IS_ERR(crtc_state
))
1513 return PTR_ERR(crtc_state
);
1515 ret
= drm_modeset_lock(&config
->connection_mutex
, state
->acquire_ctx
);
1519 DRM_DEBUG_ATOMIC("Adding all current connectors for [CRTC:%d:%s] to %p\n",
1520 crtc
->base
.id
, crtc
->name
, state
);
1523 * Changed connectors are already in @state, so only need to look
1524 * at the connector_mask in crtc_state.
1526 drm_connector_list_iter_begin(state
->dev
, &conn_iter
);
1527 drm_for_each_connector_iter(connector
, &conn_iter
) {
1528 if (!(crtc_state
->connector_mask
& (1 << drm_connector_index(connector
))))
1531 conn_state
= drm_atomic_get_connector_state(state
, connector
);
1532 if (IS_ERR(conn_state
)) {
1533 drm_connector_list_iter_end(&conn_iter
);
1534 return PTR_ERR(conn_state
);
1537 drm_connector_list_iter_end(&conn_iter
);
1541 EXPORT_SYMBOL(drm_atomic_add_affected_connectors
);
1544 * drm_atomic_add_affected_planes - add planes for crtc
1545 * @state: atomic state
1548 * This function walks the current configuration and adds all planes
1549 * currently used by @crtc to the atomic configuration @state. This is useful
1550 * when an atomic commit also needs to check all currently enabled plane on
1551 * @crtc, e.g. when changing the mode. It's also useful when re-enabling a CRTC
1552 * to avoid special code to force-enable all planes.
1554 * Since acquiring a plane state will always also acquire the w/w mutex of the
1555 * current CRTC for that plane (if there is any) adding all the plane states for
1556 * a CRTC will not reduce parallism of atomic updates.
1559 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1560 * then the w/w mutex code has detected a deadlock and the entire atomic
1561 * sequence must be restarted. All other errors are fatal.
1564 drm_atomic_add_affected_planes(struct drm_atomic_state
*state
,
1565 struct drm_crtc
*crtc
)
1567 struct drm_plane
*plane
;
1569 WARN_ON(!drm_atomic_get_new_crtc_state(state
, crtc
));
1571 drm_for_each_plane_mask(plane
, state
->dev
, crtc
->state
->plane_mask
) {
1572 struct drm_plane_state
*plane_state
=
1573 drm_atomic_get_plane_state(state
, plane
);
1575 if (IS_ERR(plane_state
))
1576 return PTR_ERR(plane_state
);
1580 EXPORT_SYMBOL(drm_atomic_add_affected_planes
);
1583 * drm_atomic_legacy_backoff - locking backoff for legacy ioctls
1584 * @state: atomic state
1586 * This function should be used by legacy entry points which don't understand
1587 * -EDEADLK semantics. For simplicity this one will grab all modeset locks after
1588 * the slowpath completed.
1590 void drm_atomic_legacy_backoff(struct drm_atomic_state
*state
)
1592 struct drm_device
*dev
= state
->dev
;
1594 bool global
= false;
1596 if (WARN_ON(dev
->mode_config
.acquire_ctx
== state
->acquire_ctx
)) {
1599 dev
->mode_config
.acquire_ctx
= NULL
;
1603 drm_modeset_backoff(state
->acquire_ctx
);
1605 ret
= drm_modeset_lock_all_ctx(dev
, state
->acquire_ctx
);
1610 dev
->mode_config
.acquire_ctx
= state
->acquire_ctx
;
1612 EXPORT_SYMBOL(drm_atomic_legacy_backoff
);
1615 * drm_atomic_check_only - check whether a given config would work
1616 * @state: atomic configuration to check
1618 * Note that this function can return -EDEADLK if the driver needed to acquire
1619 * more locks but encountered a deadlock. The caller must then do the usual w/w
1620 * backoff dance and restart. All other errors are fatal.
1623 * 0 on success, negative error code on failure.
1625 int drm_atomic_check_only(struct drm_atomic_state
*state
)
1627 struct drm_device
*dev
= state
->dev
;
1628 struct drm_mode_config
*config
= &dev
->mode_config
;
1629 struct drm_plane
*plane
;
1630 struct drm_plane_state
*plane_state
;
1631 struct drm_crtc
*crtc
;
1632 struct drm_crtc_state
*crtc_state
;
1635 DRM_DEBUG_ATOMIC("checking %p\n", state
);
1637 for_each_new_plane_in_state(state
, plane
, plane_state
, i
) {
1638 ret
= drm_atomic_plane_check(plane
, plane_state
);
1640 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic core check failed\n",
1641 plane
->base
.id
, plane
->name
);
1646 for_each_new_crtc_in_state(state
, crtc
, crtc_state
, i
) {
1647 ret
= drm_atomic_crtc_check(crtc
, crtc_state
);
1649 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic core check failed\n",
1650 crtc
->base
.id
, crtc
->name
);
1655 if (config
->funcs
->atomic_check
)
1656 ret
= config
->funcs
->atomic_check(state
->dev
, state
);
1658 if (!state
->allow_modeset
) {
1659 for_each_new_crtc_in_state(state
, crtc
, crtc_state
, i
) {
1660 if (drm_atomic_crtc_needs_modeset(crtc_state
)) {
1661 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] requires full modeset\n",
1662 crtc
->base
.id
, crtc
->name
);
1670 EXPORT_SYMBOL(drm_atomic_check_only
);
1673 * drm_atomic_commit - commit configuration atomically
1674 * @state: atomic configuration to check
1676 * Note that this function can return -EDEADLK if the driver needed to acquire
1677 * more locks but encountered a deadlock. The caller must then do the usual w/w
1678 * backoff dance and restart. All other errors are fatal.
1680 * This function will take its own reference on @state.
1681 * Callers should always release their reference with drm_atomic_state_put().
1684 * 0 on success, negative error code on failure.
1686 int drm_atomic_commit(struct drm_atomic_state
*state
)
1688 struct drm_mode_config
*config
= &state
->dev
->mode_config
;
1691 ret
= drm_atomic_check_only(state
);
1695 DRM_DEBUG_ATOMIC("committing %p\n", state
);
1697 return config
->funcs
->atomic_commit(state
->dev
, state
, false);
1699 EXPORT_SYMBOL(drm_atomic_commit
);
1702 * drm_atomic_nonblocking_commit - atomic nonblocking commit
1703 * @state: atomic configuration to check
1705 * Note that this function can return -EDEADLK if the driver needed to acquire
1706 * more locks but encountered a deadlock. The caller must then do the usual w/w
1707 * backoff dance and restart. All other errors are fatal.
1709 * This function will take its own reference on @state.
1710 * Callers should always release their reference with drm_atomic_state_put().
1713 * 0 on success, negative error code on failure.
1715 int drm_atomic_nonblocking_commit(struct drm_atomic_state
*state
)
1717 struct drm_mode_config
*config
= &state
->dev
->mode_config
;
1720 ret
= drm_atomic_check_only(state
);
1724 DRM_DEBUG_ATOMIC("committing %p nonblocking\n", state
);
1726 return config
->funcs
->atomic_commit(state
->dev
, state
, true);
1728 EXPORT_SYMBOL(drm_atomic_nonblocking_commit
);
1730 static void drm_atomic_print_state(const struct drm_atomic_state
*state
)
1732 struct drm_printer p
= drm_info_printer(state
->dev
->dev
);
1733 struct drm_plane
*plane
;
1734 struct drm_plane_state
*plane_state
;
1735 struct drm_crtc
*crtc
;
1736 struct drm_crtc_state
*crtc_state
;
1737 struct drm_connector
*connector
;
1738 struct drm_connector_state
*connector_state
;
1741 DRM_DEBUG_ATOMIC("checking %p\n", state
);
1743 for_each_new_plane_in_state(state
, plane
, plane_state
, i
)
1744 drm_atomic_plane_print_state(&p
, plane_state
);
1746 for_each_new_crtc_in_state(state
, crtc
, crtc_state
, i
)
1747 drm_atomic_crtc_print_state(&p
, crtc_state
);
1749 for_each_new_connector_in_state(state
, connector
, connector_state
, i
)
1750 drm_atomic_connector_print_state(&p
, connector_state
);
1753 static void __drm_state_dump(struct drm_device
*dev
, struct drm_printer
*p
,
1756 struct drm_mode_config
*config
= &dev
->mode_config
;
1757 struct drm_plane
*plane
;
1758 struct drm_crtc
*crtc
;
1759 struct drm_connector
*connector
;
1760 struct drm_connector_list_iter conn_iter
;
1762 if (!drm_core_check_feature(dev
, DRIVER_ATOMIC
))
1765 list_for_each_entry(plane
, &config
->plane_list
, head
) {
1767 drm_modeset_lock(&plane
->mutex
, NULL
);
1768 drm_atomic_plane_print_state(p
, plane
->state
);
1770 drm_modeset_unlock(&plane
->mutex
);
1773 list_for_each_entry(crtc
, &config
->crtc_list
, head
) {
1775 drm_modeset_lock(&crtc
->mutex
, NULL
);
1776 drm_atomic_crtc_print_state(p
, crtc
->state
);
1778 drm_modeset_unlock(&crtc
->mutex
);
1781 drm_connector_list_iter_begin(dev
, &conn_iter
);
1783 drm_modeset_lock(&dev
->mode_config
.connection_mutex
, NULL
);
1784 drm_for_each_connector_iter(connector
, &conn_iter
)
1785 drm_atomic_connector_print_state(p
, connector
->state
);
1787 drm_modeset_unlock(&dev
->mode_config
.connection_mutex
);
1788 drm_connector_list_iter_end(&conn_iter
);
1792 * drm_state_dump - dump entire device atomic state
1793 * @dev: the drm device
1794 * @p: where to print the state to
1796 * Just for debugging. Drivers might want an option to dump state
1797 * to dmesg in case of error irq's. (Hint, you probably want to
1800 * The caller must drm_modeset_lock_all(), or if this is called
1801 * from error irq handler, it should not be enabled by default.
1802 * (Ie. if you are debugging errors you might not care that this
1803 * is racey. But calling this without all modeset locks held is
1804 * not inherently safe.)
1806 void drm_state_dump(struct drm_device
*dev
, struct drm_printer
*p
)
1808 __drm_state_dump(dev
, p
, false);
1810 EXPORT_SYMBOL(drm_state_dump
);
1812 #ifdef CONFIG_DEBUG_FS
1813 static int drm_state_info(struct seq_file
*m
, void *data
)
1815 struct drm_info_node
*node
= (struct drm_info_node
*) m
->private;
1816 struct drm_device
*dev
= node
->minor
->dev
;
1817 struct drm_printer p
= drm_seq_file_printer(m
);
1819 __drm_state_dump(dev
, &p
, true);
1824 /* any use in debugfs files to dump individual planes/crtc/etc? */
1825 static const struct drm_info_list drm_atomic_debugfs_list
[] = {
1826 {"state", drm_state_info
, 0},
1829 int drm_atomic_debugfs_init(struct drm_minor
*minor
)
1831 return drm_debugfs_create_files(drm_atomic_debugfs_list
,
1832 ARRAY_SIZE(drm_atomic_debugfs_list
),
1833 minor
->debugfs_root
, minor
);
1838 * The big monstor ioctl
1841 static struct drm_pending_vblank_event
*create_vblank_event(
1842 struct drm_device
*dev
, uint64_t user_data
)
1844 struct drm_pending_vblank_event
*e
= NULL
;
1846 e
= kzalloc(sizeof *e
, GFP_KERNEL
);
1850 e
->event
.base
.type
= DRM_EVENT_FLIP_COMPLETE
;
1851 e
->event
.base
.length
= sizeof(e
->event
);
1852 e
->event
.user_data
= user_data
;
1857 static int atomic_set_prop(struct drm_atomic_state
*state
,
1858 struct drm_mode_object
*obj
, struct drm_property
*prop
,
1859 uint64_t prop_value
)
1861 struct drm_mode_object
*ref
;
1864 if (!drm_property_change_valid_get(prop
, prop_value
, &ref
))
1867 switch (obj
->type
) {
1868 case DRM_MODE_OBJECT_CONNECTOR
: {
1869 struct drm_connector
*connector
= obj_to_connector(obj
);
1870 struct drm_connector_state
*connector_state
;
1872 connector_state
= drm_atomic_get_connector_state(state
, connector
);
1873 if (IS_ERR(connector_state
)) {
1874 ret
= PTR_ERR(connector_state
);
1878 ret
= drm_atomic_connector_set_property(connector
,
1879 connector_state
, prop
, prop_value
);
1882 case DRM_MODE_OBJECT_CRTC
: {
1883 struct drm_crtc
*crtc
= obj_to_crtc(obj
);
1884 struct drm_crtc_state
*crtc_state
;
1886 crtc_state
= drm_atomic_get_crtc_state(state
, crtc
);
1887 if (IS_ERR(crtc_state
)) {
1888 ret
= PTR_ERR(crtc_state
);
1892 ret
= drm_atomic_crtc_set_property(crtc
,
1893 crtc_state
, prop
, prop_value
);
1896 case DRM_MODE_OBJECT_PLANE
: {
1897 struct drm_plane
*plane
= obj_to_plane(obj
);
1898 struct drm_plane_state
*plane_state
;
1900 plane_state
= drm_atomic_get_plane_state(state
, plane
);
1901 if (IS_ERR(plane_state
)) {
1902 ret
= PTR_ERR(plane_state
);
1906 ret
= drm_atomic_plane_set_property(plane
,
1907 plane_state
, prop
, prop_value
);
1915 drm_property_change_valid_put(prop
, ref
);
1920 * drm_atomic_clean_old_fb -- Unset old_fb pointers and set plane->fb pointers.
1922 * @dev: drm device to check.
1923 * @plane_mask: plane mask for planes that were updated.
1924 * @ret: return value, can be -EDEADLK for a retry.
1926 * Before doing an update &drm_plane.old_fb is set to &drm_plane.fb, but before
1927 * dropping the locks old_fb needs to be set to NULL and plane->fb updated. This
1928 * is a common operation for each atomic update, so this call is split off as a
1931 void drm_atomic_clean_old_fb(struct drm_device
*dev
,
1932 unsigned plane_mask
,
1935 struct drm_plane
*plane
;
1937 /* if succeeded, fixup legacy plane crtc/fb ptrs before dropping
1938 * locks (ie. while it is still safe to deref plane->state). We
1939 * need to do this here because the driver entry points cannot
1940 * distinguish between legacy and atomic ioctls.
1942 drm_for_each_plane_mask(plane
, dev
, plane_mask
) {
1944 struct drm_framebuffer
*new_fb
= plane
->state
->fb
;
1946 drm_framebuffer_get(new_fb
);
1948 plane
->crtc
= plane
->state
->crtc
;
1951 drm_framebuffer_put(plane
->old_fb
);
1953 plane
->old_fb
= NULL
;
1956 EXPORT_SYMBOL(drm_atomic_clean_old_fb
);
1959 * DOC: explicit fencing properties
1961 * Explicit fencing allows userspace to control the buffer synchronization
1962 * between devices. A Fence or a group of fences are transfered to/from
1963 * userspace using Sync File fds and there are two DRM properties for that.
1964 * IN_FENCE_FD on each DRM Plane to send fences to the kernel and
1965 * OUT_FENCE_PTR on each DRM CRTC to receive fences from the kernel.
1967 * As a contrast, with implicit fencing the kernel keeps track of any
1968 * ongoing rendering, and automatically ensures that the atomic update waits
1969 * for any pending rendering to complete. For shared buffers represented with
1970 * a &struct dma_buf this is tracked in &struct reservation_object.
1971 * Implicit syncing is how Linux traditionally worked (e.g. DRI2/3 on X.org),
1972 * whereas explicit fencing is what Android wants.
1975 * Use this property to pass a fence that DRM should wait on before
1976 * proceeding with the Atomic Commit request and show the framebuffer for
1977 * the plane on the screen. The fence can be either a normal fence or a
1978 * merged one, the sync_file framework will handle both cases and use a
1979 * fence_array if a merged fence is received. Passing -1 here means no
1980 * fences to wait on.
1982 * If the Atomic Commit request has the DRM_MODE_ATOMIC_TEST_ONLY flag
1983 * it will only check if the Sync File is a valid one.
1985 * On the driver side the fence is stored on the @fence parameter of
1986 * &struct drm_plane_state. Drivers which also support implicit fencing
1987 * should set the implicit fence using drm_atomic_set_fence_for_plane(),
1988 * to make sure there's consistent behaviour between drivers in precedence
1989 * of implicit vs. explicit fencing.
1992 * Use this property to pass a file descriptor pointer to DRM. Once the
1993 * Atomic Commit request call returns OUT_FENCE_PTR will be filled with
1994 * the file descriptor number of a Sync File. This Sync File contains the
1995 * CRTC fence that will be signaled when all framebuffers present on the
1996 * Atomic Commit * request for that given CRTC are scanned out on the
1999 * The Atomic Commit request fails if a invalid pointer is passed. If the
2000 * Atomic Commit request fails for any other reason the out fence fd
2001 * returned will be -1. On a Atomic Commit with the
2002 * DRM_MODE_ATOMIC_TEST_ONLY flag the out fence will also be set to -1.
2004 * Note that out-fences don't have a special interface to drivers and are
2005 * internally represented by a &struct drm_pending_vblank_event in struct
2006 * &drm_crtc_state, which is also used by the nonblocking atomic commit
2007 * helpers and for the DRM event handling for existing userspace.
2010 struct drm_out_fence_state
{
2011 s32 __user
*out_fence_ptr
;
2012 struct sync_file
*sync_file
;
2016 static int setup_out_fence(struct drm_out_fence_state
*fence_state
,
2017 struct dma_fence
*fence
)
2019 fence_state
->fd
= get_unused_fd_flags(O_CLOEXEC
);
2020 if (fence_state
->fd
< 0)
2021 return fence_state
->fd
;
2023 if (put_user(fence_state
->fd
, fence_state
->out_fence_ptr
))
2026 fence_state
->sync_file
= sync_file_create(fence
);
2027 if (!fence_state
->sync_file
)
2033 static int prepare_crtc_signaling(struct drm_device
*dev
,
2034 struct drm_atomic_state
*state
,
2035 struct drm_mode_atomic
*arg
,
2036 struct drm_file
*file_priv
,
2037 struct drm_out_fence_state
**fence_state
,
2038 unsigned int *num_fences
)
2040 struct drm_crtc
*crtc
;
2041 struct drm_crtc_state
*crtc_state
;
2044 if (arg
->flags
& DRM_MODE_ATOMIC_TEST_ONLY
)
2047 for_each_new_crtc_in_state(state
, crtc
, crtc_state
, i
) {
2048 s32 __user
*fence_ptr
;
2050 fence_ptr
= get_out_fence_for_crtc(crtc_state
->state
, crtc
);
2052 if (arg
->flags
& DRM_MODE_PAGE_FLIP_EVENT
|| fence_ptr
) {
2053 struct drm_pending_vblank_event
*e
;
2055 e
= create_vblank_event(dev
, arg
->user_data
);
2059 crtc_state
->event
= e
;
2062 if (arg
->flags
& DRM_MODE_PAGE_FLIP_EVENT
) {
2063 struct drm_pending_vblank_event
*e
= crtc_state
->event
;
2068 ret
= drm_event_reserve_init(dev
, file_priv
, &e
->base
,
2072 crtc_state
->event
= NULL
;
2078 struct dma_fence
*fence
;
2079 struct drm_out_fence_state
*f
;
2081 f
= krealloc(*fence_state
, sizeof(**fence_state
) *
2082 (*num_fences
+ 1), GFP_KERNEL
);
2086 memset(&f
[*num_fences
], 0, sizeof(*f
));
2088 f
[*num_fences
].out_fence_ptr
= fence_ptr
;
2091 fence
= drm_crtc_create_fence(crtc
);
2095 ret
= setup_out_fence(&f
[(*num_fences
)++], fence
);
2097 dma_fence_put(fence
);
2101 crtc_state
->event
->base
.fence
= fence
;
2108 static void complete_crtc_signaling(struct drm_device
*dev
,
2109 struct drm_atomic_state
*state
,
2110 struct drm_out_fence_state
*fence_state
,
2111 unsigned int num_fences
,
2114 struct drm_crtc
*crtc
;
2115 struct drm_crtc_state
*crtc_state
;
2119 for (i
= 0; i
< num_fences
; i
++)
2120 fd_install(fence_state
[i
].fd
,
2121 fence_state
[i
].sync_file
->file
);
2127 for_each_new_crtc_in_state(state
, crtc
, crtc_state
, i
) {
2128 struct drm_pending_vblank_event
*event
= crtc_state
->event
;
2130 * Free the allocated event. drm_atomic_helper_setup_commit
2131 * can allocate an event too, so only free it if it's ours
2132 * to prevent a double free in drm_atomic_state_clear.
2134 if (event
&& (event
->base
.fence
|| event
->base
.file_priv
)) {
2135 drm_event_cancel_free(dev
, &event
->base
);
2136 crtc_state
->event
= NULL
;
2143 for (i
= 0; i
< num_fences
; i
++) {
2144 if (fence_state
[i
].sync_file
)
2145 fput(fence_state
[i
].sync_file
->file
);
2146 if (fence_state
[i
].fd
>= 0)
2147 put_unused_fd(fence_state
[i
].fd
);
2149 /* If this fails log error to the user */
2150 if (fence_state
[i
].out_fence_ptr
&&
2151 put_user(-1, fence_state
[i
].out_fence_ptr
))
2152 DRM_DEBUG_ATOMIC("Couldn't clear out_fence_ptr\n");
2158 int drm_mode_atomic_ioctl(struct drm_device
*dev
,
2159 void *data
, struct drm_file
*file_priv
)
2161 struct drm_mode_atomic
*arg
= data
;
2162 uint32_t __user
*objs_ptr
= (uint32_t __user
*)(unsigned long)(arg
->objs_ptr
);
2163 uint32_t __user
*count_props_ptr
= (uint32_t __user
*)(unsigned long)(arg
->count_props_ptr
);
2164 uint32_t __user
*props_ptr
= (uint32_t __user
*)(unsigned long)(arg
->props_ptr
);
2165 uint64_t __user
*prop_values_ptr
= (uint64_t __user
*)(unsigned long)(arg
->prop_values_ptr
);
2166 unsigned int copied_objs
, copied_props
;
2167 struct drm_atomic_state
*state
;
2168 struct drm_modeset_acquire_ctx ctx
;
2169 struct drm_plane
*plane
;
2170 struct drm_out_fence_state
*fence_state
= NULL
;
2171 unsigned plane_mask
;
2173 unsigned int i
, j
, num_fences
= 0;
2175 /* disallow for drivers not supporting atomic: */
2176 if (!drm_core_check_feature(dev
, DRIVER_ATOMIC
))
2179 /* disallow for userspace that has not enabled atomic cap (even
2180 * though this may be a bit overkill, since legacy userspace
2181 * wouldn't know how to call this ioctl)
2183 if (!file_priv
->atomic
)
2186 if (arg
->flags
& ~DRM_MODE_ATOMIC_FLAGS
)
2192 if ((arg
->flags
& DRM_MODE_PAGE_FLIP_ASYNC
) &&
2193 !dev
->mode_config
.async_page_flip
)
2196 /* can't test and expect an event at the same time. */
2197 if ((arg
->flags
& DRM_MODE_ATOMIC_TEST_ONLY
) &&
2198 (arg
->flags
& DRM_MODE_PAGE_FLIP_EVENT
))
2201 drm_modeset_acquire_init(&ctx
, 0);
2203 state
= drm_atomic_state_alloc(dev
);
2207 state
->acquire_ctx
= &ctx
;
2208 state
->allow_modeset
= !!(arg
->flags
& DRM_MODE_ATOMIC_ALLOW_MODESET
);
2215 for (i
= 0; i
< arg
->count_objs
; i
++) {
2216 uint32_t obj_id
, count_props
;
2217 struct drm_mode_object
*obj
;
2219 if (get_user(obj_id
, objs_ptr
+ copied_objs
)) {
2224 obj
= drm_mode_object_find(dev
, obj_id
, DRM_MODE_OBJECT_ANY
);
2230 if (!obj
->properties
) {
2231 drm_mode_object_put(obj
);
2236 if (get_user(count_props
, count_props_ptr
+ copied_objs
)) {
2237 drm_mode_object_put(obj
);
2244 for (j
= 0; j
< count_props
; j
++) {
2246 uint64_t prop_value
;
2247 struct drm_property
*prop
;
2249 if (get_user(prop_id
, props_ptr
+ copied_props
)) {
2250 drm_mode_object_put(obj
);
2255 prop
= drm_mode_obj_find_prop_id(obj
, prop_id
);
2257 drm_mode_object_put(obj
);
2262 if (copy_from_user(&prop_value
,
2263 prop_values_ptr
+ copied_props
,
2264 sizeof(prop_value
))) {
2265 drm_mode_object_put(obj
);
2270 ret
= atomic_set_prop(state
, obj
, prop
, prop_value
);
2272 drm_mode_object_put(obj
);
2279 if (obj
->type
== DRM_MODE_OBJECT_PLANE
&& count_props
&&
2280 !(arg
->flags
& DRM_MODE_ATOMIC_TEST_ONLY
)) {
2281 plane
= obj_to_plane(obj
);
2282 plane_mask
|= (1 << drm_plane_index(plane
));
2283 plane
->old_fb
= plane
->fb
;
2285 drm_mode_object_put(obj
);
2288 ret
= prepare_crtc_signaling(dev
, state
, arg
, file_priv
, &fence_state
,
2293 if (arg
->flags
& DRM_MODE_ATOMIC_TEST_ONLY
) {
2294 ret
= drm_atomic_check_only(state
);
2295 } else if (arg
->flags
& DRM_MODE_ATOMIC_NONBLOCK
) {
2296 ret
= drm_atomic_nonblocking_commit(state
);
2298 if (unlikely(drm_debug
& DRM_UT_STATE
))
2299 drm_atomic_print_state(state
);
2301 ret
= drm_atomic_commit(state
);
2305 drm_atomic_clean_old_fb(dev
, plane_mask
, ret
);
2307 complete_crtc_signaling(dev
, state
, fence_state
, num_fences
, !ret
);
2309 if (ret
== -EDEADLK
) {
2310 drm_atomic_state_clear(state
);
2311 drm_modeset_backoff(&ctx
);
2315 drm_atomic_state_put(state
);
2317 drm_modeset_drop_locks(&ctx
);
2318 drm_modeset_acquire_fini(&ctx
);