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_plane_helper.h>
34 * drm_atomic_state_default_release -
35 * release memory initialized by drm_atomic_state_init
36 * @state: atomic state
38 * Free all the memory allocated by drm_atomic_state_init.
39 * This is useful for drivers that subclass the atomic state.
41 void drm_atomic_state_default_release(struct drm_atomic_state
*state
)
43 kfree(state
->connectors
);
44 kfree(state
->connector_states
);
46 kfree(state
->crtc_states
);
48 kfree(state
->plane_states
);
50 EXPORT_SYMBOL(drm_atomic_state_default_release
);
53 * drm_atomic_state_init - init new atomic state
55 * @state: atomic state
57 * Default implementation for filling in a new atomic state.
58 * This is useful for drivers that subclass the atomic state.
61 drm_atomic_state_init(struct drm_device
*dev
, struct drm_atomic_state
*state
)
63 /* TODO legacy paths should maybe do a better job about
64 * setting this appropriately?
66 state
->allow_modeset
= true;
68 state
->num_connector
= ACCESS_ONCE(dev
->mode_config
.num_connector
);
70 state
->crtcs
= kcalloc(dev
->mode_config
.num_crtc
,
71 sizeof(*state
->crtcs
), GFP_KERNEL
);
74 state
->crtc_states
= kcalloc(dev
->mode_config
.num_crtc
,
75 sizeof(*state
->crtc_states
), GFP_KERNEL
);
76 if (!state
->crtc_states
)
78 state
->planes
= kcalloc(dev
->mode_config
.num_total_plane
,
79 sizeof(*state
->planes
), GFP_KERNEL
);
82 state
->plane_states
= kcalloc(dev
->mode_config
.num_total_plane
,
83 sizeof(*state
->plane_states
), GFP_KERNEL
);
84 if (!state
->plane_states
)
86 state
->connectors
= kcalloc(state
->num_connector
,
87 sizeof(*state
->connectors
),
89 if (!state
->connectors
)
91 state
->connector_states
= kcalloc(state
->num_connector
,
92 sizeof(*state
->connector_states
),
94 if (!state
->connector_states
)
99 DRM_DEBUG_ATOMIC("Allocated atomic state %p\n", state
);
103 drm_atomic_state_default_release(state
);
106 EXPORT_SYMBOL(drm_atomic_state_init
);
109 * drm_atomic_state_alloc - allocate atomic state
112 * This allocates an empty atomic state to track updates.
114 struct drm_atomic_state
*
115 drm_atomic_state_alloc(struct drm_device
*dev
)
117 struct drm_mode_config
*config
= &dev
->mode_config
;
118 struct drm_atomic_state
*state
;
120 if (!config
->funcs
->atomic_state_alloc
) {
121 state
= kzalloc(sizeof(*state
), GFP_KERNEL
);
124 if (drm_atomic_state_init(dev
, state
) < 0) {
131 return config
->funcs
->atomic_state_alloc(dev
);
133 EXPORT_SYMBOL(drm_atomic_state_alloc
);
136 * drm_atomic_state_default_clear - clear base atomic state
137 * @state: atomic state
139 * Default implementation for clearing atomic state.
140 * This is useful for drivers that subclass the atomic state.
142 void drm_atomic_state_default_clear(struct drm_atomic_state
*state
)
144 struct drm_device
*dev
= state
->dev
;
145 struct drm_mode_config
*config
= &dev
->mode_config
;
148 DRM_DEBUG_ATOMIC("Clearing atomic state %p\n", state
);
150 for (i
= 0; i
< state
->num_connector
; i
++) {
151 struct drm_connector
*connector
= state
->connectors
[i
];
157 * FIXME: Async commits can race with connector unplugging and
158 * there's currently nothing that prevents cleanup up state for
159 * deleted connectors. As long as the callback doesn't look at
160 * the connector we'll be fine though, so make sure that's the
161 * case by setting all connector pointers to NULL.
163 state
->connector_states
[i
]->connector
= NULL
;
164 connector
->funcs
->atomic_destroy_state(NULL
,
165 state
->connector_states
[i
]);
166 state
->connectors
[i
] = NULL
;
167 state
->connector_states
[i
] = NULL
;
170 for (i
= 0; i
< config
->num_crtc
; i
++) {
171 struct drm_crtc
*crtc
= state
->crtcs
[i
];
176 crtc
->funcs
->atomic_destroy_state(crtc
,
177 state
->crtc_states
[i
]);
178 state
->crtcs
[i
] = NULL
;
179 state
->crtc_states
[i
] = NULL
;
182 for (i
= 0; i
< config
->num_total_plane
; i
++) {
183 struct drm_plane
*plane
= state
->planes
[i
];
188 plane
->funcs
->atomic_destroy_state(plane
,
189 state
->plane_states
[i
]);
190 state
->planes
[i
] = NULL
;
191 state
->plane_states
[i
] = NULL
;
194 EXPORT_SYMBOL(drm_atomic_state_default_clear
);
197 * drm_atomic_state_clear - clear state object
198 * @state: atomic state
200 * When the w/w mutex algorithm detects a deadlock we need to back off and drop
201 * all locks. So someone else could sneak in and change the current modeset
202 * configuration. Which means that all the state assembled in @state is no
203 * longer an atomic update to the current state, but to some arbitrary earlier
204 * state. Which could break assumptions the driver's ->atomic_check likely
207 * Hence we must clear all cached state and completely start over, using this
210 void drm_atomic_state_clear(struct drm_atomic_state
*state
)
212 struct drm_device
*dev
= state
->dev
;
213 struct drm_mode_config
*config
= &dev
->mode_config
;
215 if (config
->funcs
->atomic_state_clear
)
216 config
->funcs
->atomic_state_clear(state
);
218 drm_atomic_state_default_clear(state
);
220 EXPORT_SYMBOL(drm_atomic_state_clear
);
223 * drm_atomic_state_free - free all memory for an atomic state
224 * @state: atomic state to deallocate
226 * This frees all memory associated with an atomic state, including all the
227 * per-object state for planes, crtcs and connectors.
229 void drm_atomic_state_free(struct drm_atomic_state
*state
)
231 struct drm_device
*dev
;
232 struct drm_mode_config
*config
;
238 config
= &dev
->mode_config
;
240 drm_atomic_state_clear(state
);
242 DRM_DEBUG_ATOMIC("Freeing atomic state %p\n", state
);
244 if (config
->funcs
->atomic_state_free
) {
245 config
->funcs
->atomic_state_free(state
);
247 drm_atomic_state_default_release(state
);
251 EXPORT_SYMBOL(drm_atomic_state_free
);
254 * drm_atomic_get_crtc_state - get crtc state
255 * @state: global atomic state object
256 * @crtc: crtc to get state object for
258 * This function returns the crtc state for the given crtc, allocating it if
259 * needed. It will also grab the relevant crtc lock to make sure that the state
264 * Either the allocated state or the error code encoded into the pointer. When
265 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
266 * entire atomic sequence must be restarted. All other errors are fatal.
268 struct drm_crtc_state
*
269 drm_atomic_get_crtc_state(struct drm_atomic_state
*state
,
270 struct drm_crtc
*crtc
)
272 int ret
, index
= drm_crtc_index(crtc
);
273 struct drm_crtc_state
*crtc_state
;
275 crtc_state
= drm_atomic_get_existing_crtc_state(state
, crtc
);
279 ret
= drm_modeset_lock(&crtc
->mutex
, state
->acquire_ctx
);
283 crtc_state
= crtc
->funcs
->atomic_duplicate_state(crtc
);
285 return ERR_PTR(-ENOMEM
);
287 state
->crtc_states
[index
] = crtc_state
;
288 state
->crtcs
[index
] = crtc
;
289 crtc_state
->state
= state
;
291 DRM_DEBUG_ATOMIC("Added [CRTC:%d:%s] %p state to %p\n",
292 crtc
->base
.id
, crtc
->name
, crtc_state
, state
);
296 EXPORT_SYMBOL(drm_atomic_get_crtc_state
);
299 * drm_atomic_set_mode_for_crtc - set mode for CRTC
300 * @state: the CRTC whose incoming state to update
301 * @mode: kernel-internal mode to use for the CRTC, or NULL to disable
303 * Set a mode (originating from the kernel) on the desired CRTC state. Does
304 * not change any other state properties, including enable, active, or
308 * Zero on success, error code on failure. Cannot return -EDEADLK.
310 int drm_atomic_set_mode_for_crtc(struct drm_crtc_state
*state
,
311 struct drm_display_mode
*mode
)
313 struct drm_mode_modeinfo umode
;
315 /* Early return for no change. */
316 if (mode
&& memcmp(&state
->mode
, mode
, sizeof(*mode
)) == 0)
319 drm_property_unreference_blob(state
->mode_blob
);
320 state
->mode_blob
= NULL
;
323 drm_mode_convert_to_umode(&umode
, mode
);
325 drm_property_create_blob(state
->crtc
->dev
,
328 if (IS_ERR(state
->mode_blob
))
329 return PTR_ERR(state
->mode_blob
);
331 drm_mode_copy(&state
->mode
, mode
);
332 state
->enable
= true;
333 DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n",
336 memset(&state
->mode
, 0, sizeof(state
->mode
));
337 state
->enable
= false;
338 DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n",
344 EXPORT_SYMBOL(drm_atomic_set_mode_for_crtc
);
347 * drm_atomic_set_mode_prop_for_crtc - set mode for CRTC
348 * @state: the CRTC whose incoming state to update
349 * @blob: pointer to blob property to use for mode
351 * Set a mode (originating from a blob property) on the desired CRTC state.
352 * This function will take a reference on the blob property for the CRTC state,
353 * and release the reference held on the state's existing mode property, if any
357 * Zero on success, error code on failure. Cannot return -EDEADLK.
359 int drm_atomic_set_mode_prop_for_crtc(struct drm_crtc_state
*state
,
360 struct drm_property_blob
*blob
)
362 if (blob
== state
->mode_blob
)
365 drm_property_unreference_blob(state
->mode_blob
);
366 state
->mode_blob
= NULL
;
369 if (blob
->length
!= sizeof(struct drm_mode_modeinfo
) ||
370 drm_mode_convert_umode(&state
->mode
,
371 (const struct drm_mode_modeinfo
*)
375 state
->mode_blob
= drm_property_reference_blob(blob
);
376 state
->enable
= true;
377 DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n",
378 state
->mode
.name
, state
);
380 memset(&state
->mode
, 0, sizeof(state
->mode
));
381 state
->enable
= false;
382 DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n",
388 EXPORT_SYMBOL(drm_atomic_set_mode_prop_for_crtc
);
391 * drm_atomic_crtc_set_property - set property on CRTC
392 * @crtc: the drm CRTC to set a property on
393 * @state: the state object to update with the new property value
394 * @property: the property to set
395 * @val: the new property value
397 * Use this instead of calling crtc->atomic_set_property directly.
398 * This function handles generic/core properties and calls out to
399 * driver's ->atomic_set_property() for driver properties. To ensure
400 * consistent behavior you must call this function rather than the
401 * driver hook directly.
404 * Zero on success, error code on failure
406 int drm_atomic_crtc_set_property(struct drm_crtc
*crtc
,
407 struct drm_crtc_state
*state
, struct drm_property
*property
,
410 struct drm_device
*dev
= crtc
->dev
;
411 struct drm_mode_config
*config
= &dev
->mode_config
;
414 if (property
== config
->prop_active
)
416 else if (property
== config
->prop_mode_id
) {
417 struct drm_property_blob
*mode
=
418 drm_property_lookup_blob(dev
, val
);
419 ret
= drm_atomic_set_mode_prop_for_crtc(state
, mode
);
420 drm_property_unreference_blob(mode
);
423 else if (crtc
->funcs
->atomic_set_property
)
424 return crtc
->funcs
->atomic_set_property(crtc
, state
, property
, val
);
430 EXPORT_SYMBOL(drm_atomic_crtc_set_property
);
433 * drm_atomic_crtc_get_property - get property value from CRTC state
434 * @crtc: the drm CRTC to set a property on
435 * @state: the state object to get the property value from
436 * @property: the property to set
437 * @val: return location for the property value
439 * This function handles generic/core properties and calls out to
440 * driver's ->atomic_get_property() for driver properties. To ensure
441 * consistent behavior you must call this function rather than the
442 * driver hook directly.
445 * Zero on success, error code on failure
448 drm_atomic_crtc_get_property(struct drm_crtc
*crtc
,
449 const struct drm_crtc_state
*state
,
450 struct drm_property
*property
, uint64_t *val
)
452 struct drm_device
*dev
= crtc
->dev
;
453 struct drm_mode_config
*config
= &dev
->mode_config
;
455 if (property
== config
->prop_active
)
456 *val
= state
->active
;
457 else if (property
== config
->prop_mode_id
)
458 *val
= (state
->mode_blob
) ? state
->mode_blob
->base
.id
: 0;
459 else if (crtc
->funcs
->atomic_get_property
)
460 return crtc
->funcs
->atomic_get_property(crtc
, state
, property
, val
);
468 * drm_atomic_crtc_check - check crtc state
469 * @crtc: crtc to check
470 * @state: crtc state to check
472 * Provides core sanity checks for crtc state.
475 * Zero on success, error code on failure
477 static int drm_atomic_crtc_check(struct drm_crtc
*crtc
,
478 struct drm_crtc_state
*state
)
480 /* NOTE: we explicitly don't enforce constraints such as primary
481 * layer covering entire screen, since that is something we want
482 * to allow (on hw that supports it). For hw that does not, it
483 * should be checked in driver's crtc->atomic_check() vfunc.
485 * TODO: Add generic modeset state checks once we support those.
488 if (state
->active
&& !state
->enable
) {
489 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active without enabled\n",
490 crtc
->base
.id
, crtc
->name
);
494 /* The state->enable vs. state->mode_blob checks can be WARN_ON,
495 * as this is a kernel-internal detail that userspace should never
496 * be able to trigger. */
497 if (drm_core_check_feature(crtc
->dev
, DRIVER_ATOMIC
) &&
498 WARN_ON(state
->enable
&& !state
->mode_blob
)) {
499 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled without mode blob\n",
500 crtc
->base
.id
, crtc
->name
);
504 if (drm_core_check_feature(crtc
->dev
, DRIVER_ATOMIC
) &&
505 WARN_ON(!state
->enable
&& state
->mode_blob
)) {
506 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] disabled with mode blob\n",
507 crtc
->base
.id
, crtc
->name
);
515 * drm_atomic_get_plane_state - get plane state
516 * @state: global atomic state object
517 * @plane: plane to get state object for
519 * This function returns the plane state for the given plane, allocating it if
520 * needed. It will also grab the relevant plane lock to make sure that the state
525 * Either the allocated state or the error code encoded into the pointer. When
526 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
527 * entire atomic sequence must be restarted. All other errors are fatal.
529 struct drm_plane_state
*
530 drm_atomic_get_plane_state(struct drm_atomic_state
*state
,
531 struct drm_plane
*plane
)
533 int ret
, index
= drm_plane_index(plane
);
534 struct drm_plane_state
*plane_state
;
536 plane_state
= drm_atomic_get_existing_plane_state(state
, plane
);
540 ret
= drm_modeset_lock(&plane
->mutex
, state
->acquire_ctx
);
544 plane_state
= plane
->funcs
->atomic_duplicate_state(plane
);
546 return ERR_PTR(-ENOMEM
);
548 state
->plane_states
[index
] = plane_state
;
549 state
->planes
[index
] = plane
;
550 plane_state
->state
= state
;
552 DRM_DEBUG_ATOMIC("Added [PLANE:%d:%s] %p state to %p\n",
553 plane
->base
.id
, plane
->name
, plane_state
, state
);
555 if (plane_state
->crtc
) {
556 struct drm_crtc_state
*crtc_state
;
558 crtc_state
= drm_atomic_get_crtc_state(state
,
560 if (IS_ERR(crtc_state
))
561 return ERR_CAST(crtc_state
);
566 EXPORT_SYMBOL(drm_atomic_get_plane_state
);
569 * drm_atomic_plane_set_property - set property on plane
570 * @plane: the drm plane to set a property on
571 * @state: the state object to update with the new property value
572 * @property: the property to set
573 * @val: the new property value
575 * Use this instead of calling plane->atomic_set_property directly.
576 * This function handles generic/core properties and calls out to
577 * driver's ->atomic_set_property() for driver properties. To ensure
578 * consistent behavior you must call this function rather than the
579 * driver hook directly.
582 * Zero on success, error code on failure
584 int drm_atomic_plane_set_property(struct drm_plane
*plane
,
585 struct drm_plane_state
*state
, struct drm_property
*property
,
588 struct drm_device
*dev
= plane
->dev
;
589 struct drm_mode_config
*config
= &dev
->mode_config
;
591 if (property
== config
->prop_fb_id
) {
592 struct drm_framebuffer
*fb
= drm_framebuffer_lookup(dev
, val
);
593 drm_atomic_set_fb_for_plane(state
, fb
);
595 drm_framebuffer_unreference(fb
);
596 } else if (property
== config
->prop_crtc_id
) {
597 struct drm_crtc
*crtc
= drm_crtc_find(dev
, val
);
598 return drm_atomic_set_crtc_for_plane(state
, crtc
);
599 } else if (property
== config
->prop_crtc_x
) {
600 state
->crtc_x
= U642I64(val
);
601 } else if (property
== config
->prop_crtc_y
) {
602 state
->crtc_y
= U642I64(val
);
603 } else if (property
== config
->prop_crtc_w
) {
605 } else if (property
== config
->prop_crtc_h
) {
607 } else if (property
== config
->prop_src_x
) {
609 } else if (property
== config
->prop_src_y
) {
611 } else if (property
== config
->prop_src_w
) {
613 } else if (property
== config
->prop_src_h
) {
615 } else if (property
== config
->rotation_property
) {
616 state
->rotation
= val
;
617 } else if (plane
->funcs
->atomic_set_property
) {
618 return plane
->funcs
->atomic_set_property(plane
, state
,
626 EXPORT_SYMBOL(drm_atomic_plane_set_property
);
629 * drm_atomic_plane_get_property - get property value from plane state
630 * @plane: the drm plane to set a property on
631 * @state: the state object to get the property value from
632 * @property: the property to set
633 * @val: return location for the property value
635 * This function handles generic/core properties and calls out to
636 * driver's ->atomic_get_property() for driver properties. To ensure
637 * consistent behavior you must call this function rather than the
638 * driver hook directly.
641 * Zero on success, error code on failure
644 drm_atomic_plane_get_property(struct drm_plane
*plane
,
645 const struct drm_plane_state
*state
,
646 struct drm_property
*property
, uint64_t *val
)
648 struct drm_device
*dev
= plane
->dev
;
649 struct drm_mode_config
*config
= &dev
->mode_config
;
651 if (property
== config
->prop_fb_id
) {
652 *val
= (state
->fb
) ? state
->fb
->base
.id
: 0;
653 } else if (property
== config
->prop_crtc_id
) {
654 *val
= (state
->crtc
) ? state
->crtc
->base
.id
: 0;
655 } else if (property
== config
->prop_crtc_x
) {
656 *val
= I642U64(state
->crtc_x
);
657 } else if (property
== config
->prop_crtc_y
) {
658 *val
= I642U64(state
->crtc_y
);
659 } else if (property
== config
->prop_crtc_w
) {
660 *val
= state
->crtc_w
;
661 } else if (property
== config
->prop_crtc_h
) {
662 *val
= state
->crtc_h
;
663 } else if (property
== config
->prop_src_x
) {
665 } else if (property
== config
->prop_src_y
) {
667 } else if (property
== config
->prop_src_w
) {
669 } else if (property
== config
->prop_src_h
) {
671 } else if (property
== config
->rotation_property
) {
672 *val
= state
->rotation
;
673 } else if (plane
->funcs
->atomic_get_property
) {
674 return plane
->funcs
->atomic_get_property(plane
, state
, property
, val
);
683 plane_switching_crtc(struct drm_atomic_state
*state
,
684 struct drm_plane
*plane
,
685 struct drm_plane_state
*plane_state
)
687 if (!plane
->state
->crtc
|| !plane_state
->crtc
)
690 if (plane
->state
->crtc
== plane_state
->crtc
)
693 /* This could be refined, but currently there's no helper or driver code
694 * to implement direct switching of active planes nor userspace to take
695 * advantage of more direct plane switching without the intermediate
702 * drm_atomic_plane_check - check plane state
703 * @plane: plane to check
704 * @state: plane state to check
706 * Provides core sanity checks for plane state.
709 * Zero on success, error code on failure
711 static int drm_atomic_plane_check(struct drm_plane
*plane
,
712 struct drm_plane_state
*state
)
714 unsigned int fb_width
, fb_height
;
717 /* either *both* CRTC and FB must be set, or neither */
718 if (WARN_ON(state
->crtc
&& !state
->fb
)) {
719 DRM_DEBUG_ATOMIC("CRTC set but no FB\n");
721 } else if (WARN_ON(state
->fb
&& !state
->crtc
)) {
722 DRM_DEBUG_ATOMIC("FB set but no CRTC\n");
726 /* if disabled, we don't care about the rest of the state: */
730 /* Check whether this plane is usable on this CRTC */
731 if (!(plane
->possible_crtcs
& drm_crtc_mask(state
->crtc
))) {
732 DRM_DEBUG_ATOMIC("Invalid crtc for plane\n");
736 /* Check whether this plane supports the fb pixel format. */
737 ret
= drm_plane_check_pixel_format(plane
, state
->fb
->pixel_format
);
739 DRM_DEBUG_ATOMIC("Invalid pixel format %s\n",
740 drm_get_format_name(state
->fb
->pixel_format
));
744 /* Give drivers some help against integer overflows */
745 if (state
->crtc_w
> INT_MAX
||
746 state
->crtc_x
> INT_MAX
- (int32_t) state
->crtc_w
||
747 state
->crtc_h
> INT_MAX
||
748 state
->crtc_y
> INT_MAX
- (int32_t) state
->crtc_h
) {
749 DRM_DEBUG_ATOMIC("Invalid CRTC coordinates %ux%u+%d+%d\n",
750 state
->crtc_w
, state
->crtc_h
,
751 state
->crtc_x
, state
->crtc_y
);
755 fb_width
= state
->fb
->width
<< 16;
756 fb_height
= state
->fb
->height
<< 16;
758 /* Make sure source coordinates are inside the fb. */
759 if (state
->src_w
> fb_width
||
760 state
->src_x
> fb_width
- state
->src_w
||
761 state
->src_h
> fb_height
||
762 state
->src_y
> fb_height
- state
->src_h
) {
763 DRM_DEBUG_ATOMIC("Invalid source coordinates "
764 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
765 state
->src_w
>> 16, ((state
->src_w
& 0xffff) * 15625) >> 10,
766 state
->src_h
>> 16, ((state
->src_h
& 0xffff) * 15625) >> 10,
767 state
->src_x
>> 16, ((state
->src_x
& 0xffff) * 15625) >> 10,
768 state
->src_y
>> 16, ((state
->src_y
& 0xffff) * 15625) >> 10);
772 if (plane_switching_crtc(state
->state
, plane
, state
)) {
773 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] switching CRTC directly\n",
774 plane
->base
.id
, plane
->name
);
782 * drm_atomic_get_connector_state - get connector state
783 * @state: global atomic state object
784 * @connector: connector to get state object for
786 * This function returns the connector state for the given connector,
787 * allocating it if needed. It will also grab the relevant connector lock to
788 * make sure that the state is consistent.
792 * Either the allocated state or the error code encoded into the pointer. When
793 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
794 * entire atomic sequence must be restarted. All other errors are fatal.
796 struct drm_connector_state
*
797 drm_atomic_get_connector_state(struct drm_atomic_state
*state
,
798 struct drm_connector
*connector
)
801 struct drm_mode_config
*config
= &connector
->dev
->mode_config
;
802 struct drm_connector_state
*connector_state
;
804 ret
= drm_modeset_lock(&config
->connection_mutex
, state
->acquire_ctx
);
808 index
= drm_connector_index(connector
);
811 * Construction of atomic state updates can race with a connector
812 * hot-add which might overflow. In this case flip the table and just
813 * restart the entire ioctl - no one is fast enough to livelock a cpu
814 * with physical hotplug events anyway.
816 * Note that we only grab the indexes once we have the right lock to
817 * prevent hotplug/unplugging of connectors. So removal is no problem,
818 * at most the array is a bit too large.
820 if (index
>= state
->num_connector
) {
821 DRM_DEBUG_ATOMIC("Hot-added connector would overflow state array, restarting\n");
822 return ERR_PTR(-EAGAIN
);
825 if (state
->connector_states
[index
])
826 return state
->connector_states
[index
];
828 connector_state
= connector
->funcs
->atomic_duplicate_state(connector
);
829 if (!connector_state
)
830 return ERR_PTR(-ENOMEM
);
832 state
->connector_states
[index
] = connector_state
;
833 state
->connectors
[index
] = connector
;
834 connector_state
->state
= state
;
836 DRM_DEBUG_ATOMIC("Added [CONNECTOR:%d] %p state to %p\n",
837 connector
->base
.id
, connector_state
, state
);
839 if (connector_state
->crtc
) {
840 struct drm_crtc_state
*crtc_state
;
842 crtc_state
= drm_atomic_get_crtc_state(state
,
843 connector_state
->crtc
);
844 if (IS_ERR(crtc_state
))
845 return ERR_CAST(crtc_state
);
848 return connector_state
;
850 EXPORT_SYMBOL(drm_atomic_get_connector_state
);
853 * drm_atomic_connector_set_property - set property on connector.
854 * @connector: the drm connector to set a property on
855 * @state: the state object to update with the new property value
856 * @property: the property to set
857 * @val: the new property value
859 * Use this instead of calling connector->atomic_set_property directly.
860 * This function handles generic/core properties and calls out to
861 * driver's ->atomic_set_property() for driver properties. To ensure
862 * consistent behavior you must call this function rather than the
863 * driver hook directly.
866 * Zero on success, error code on failure
868 int drm_atomic_connector_set_property(struct drm_connector
*connector
,
869 struct drm_connector_state
*state
, struct drm_property
*property
,
872 struct drm_device
*dev
= connector
->dev
;
873 struct drm_mode_config
*config
= &dev
->mode_config
;
875 if (property
== config
->prop_crtc_id
) {
876 struct drm_crtc
*crtc
= drm_crtc_find(dev
, val
);
877 return drm_atomic_set_crtc_for_connector(state
, crtc
);
878 } else if (property
== config
->dpms_property
) {
879 /* setting DPMS property requires special handling, which
880 * is done in legacy setprop path for us. Disallow (for
881 * now?) atomic writes to DPMS property:
884 } else if (connector
->funcs
->atomic_set_property
) {
885 return connector
->funcs
->atomic_set_property(connector
,
886 state
, property
, val
);
891 EXPORT_SYMBOL(drm_atomic_connector_set_property
);
894 * drm_atomic_connector_get_property - get property value from connector state
895 * @connector: the drm connector to set a property on
896 * @state: the state object to get the property value from
897 * @property: the property to set
898 * @val: return location for the property value
900 * This function handles generic/core properties and calls out to
901 * driver's ->atomic_get_property() for driver properties. To ensure
902 * consistent behavior you must call this function rather than the
903 * driver hook directly.
906 * Zero on success, error code on failure
909 drm_atomic_connector_get_property(struct drm_connector
*connector
,
910 const struct drm_connector_state
*state
,
911 struct drm_property
*property
, uint64_t *val
)
913 struct drm_device
*dev
= connector
->dev
;
914 struct drm_mode_config
*config
= &dev
->mode_config
;
916 if (property
== config
->prop_crtc_id
) {
917 *val
= (state
->crtc
) ? state
->crtc
->base
.id
: 0;
918 } else if (property
== config
->dpms_property
) {
919 *val
= connector
->dpms
;
920 } else if (connector
->funcs
->atomic_get_property
) {
921 return connector
->funcs
->atomic_get_property(connector
,
922 state
, property
, val
);
930 int drm_atomic_get_property(struct drm_mode_object
*obj
,
931 struct drm_property
*property
, uint64_t *val
)
933 struct drm_device
*dev
= property
->dev
;
937 case DRM_MODE_OBJECT_CONNECTOR
: {
938 struct drm_connector
*connector
= obj_to_connector(obj
);
939 WARN_ON(!drm_modeset_is_locked(&dev
->mode_config
.connection_mutex
));
940 ret
= drm_atomic_connector_get_property(connector
,
941 connector
->state
, property
, val
);
944 case DRM_MODE_OBJECT_CRTC
: {
945 struct drm_crtc
*crtc
= obj_to_crtc(obj
);
946 WARN_ON(!drm_modeset_is_locked(&crtc
->mutex
));
947 ret
= drm_atomic_crtc_get_property(crtc
,
948 crtc
->state
, property
, val
);
951 case DRM_MODE_OBJECT_PLANE
: {
952 struct drm_plane
*plane
= obj_to_plane(obj
);
953 WARN_ON(!drm_modeset_is_locked(&plane
->mutex
));
954 ret
= drm_atomic_plane_get_property(plane
,
955 plane
->state
, property
, val
);
967 * drm_atomic_set_crtc_for_plane - set crtc for plane
968 * @plane_state: the plane whose incoming state to update
969 * @crtc: crtc to use for the plane
971 * Changing the assigned crtc for a plane requires us to grab the lock and state
972 * for the new crtc, as needed. This function takes care of all these details
973 * besides updating the pointer in the state object itself.
976 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
977 * then the w/w mutex code has detected a deadlock and the entire atomic
978 * sequence must be restarted. All other errors are fatal.
981 drm_atomic_set_crtc_for_plane(struct drm_plane_state
*plane_state
,
982 struct drm_crtc
*crtc
)
984 struct drm_plane
*plane
= plane_state
->plane
;
985 struct drm_crtc_state
*crtc_state
;
987 if (plane_state
->crtc
) {
988 crtc_state
= drm_atomic_get_crtc_state(plane_state
->state
,
990 if (WARN_ON(IS_ERR(crtc_state
)))
991 return PTR_ERR(crtc_state
);
993 crtc_state
->plane_mask
&= ~(1 << drm_plane_index(plane
));
996 plane_state
->crtc
= crtc
;
999 crtc_state
= drm_atomic_get_crtc_state(plane_state
->state
,
1001 if (IS_ERR(crtc_state
))
1002 return PTR_ERR(crtc_state
);
1003 crtc_state
->plane_mask
|= (1 << drm_plane_index(plane
));
1007 DRM_DEBUG_ATOMIC("Link plane state %p to [CRTC:%d:%s]\n",
1008 plane_state
, crtc
->base
.id
, crtc
->name
);
1010 DRM_DEBUG_ATOMIC("Link plane state %p to [NOCRTC]\n",
1015 EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane
);
1018 * drm_atomic_set_fb_for_plane - set framebuffer for plane
1019 * @plane_state: atomic state object for the plane
1020 * @fb: fb to use for the plane
1022 * Changing the assigned framebuffer for a plane requires us to grab a reference
1023 * to the new fb and drop the reference to the old fb, if there is one. This
1024 * function takes care of all these details besides updating the pointer in the
1025 * state object itself.
1028 drm_atomic_set_fb_for_plane(struct drm_plane_state
*plane_state
,
1029 struct drm_framebuffer
*fb
)
1031 if (plane_state
->fb
)
1032 drm_framebuffer_unreference(plane_state
->fb
);
1034 drm_framebuffer_reference(fb
);
1035 plane_state
->fb
= fb
;
1038 DRM_DEBUG_ATOMIC("Set [FB:%d] for plane state %p\n",
1039 fb
->base
.id
, plane_state
);
1041 DRM_DEBUG_ATOMIC("Set [NOFB] for plane state %p\n",
1044 EXPORT_SYMBOL(drm_atomic_set_fb_for_plane
);
1047 * drm_atomic_set_crtc_for_connector - set crtc for connector
1048 * @conn_state: atomic state object for the connector
1049 * @crtc: crtc to use for the connector
1051 * Changing the assigned crtc for a connector requires us to grab the lock and
1052 * state for the new crtc, as needed. This function takes care of all these
1053 * details besides updating the pointer in the state object itself.
1056 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1057 * then the w/w mutex code has detected a deadlock and the entire atomic
1058 * sequence must be restarted. All other errors are fatal.
1061 drm_atomic_set_crtc_for_connector(struct drm_connector_state
*conn_state
,
1062 struct drm_crtc
*crtc
)
1064 struct drm_crtc_state
*crtc_state
;
1067 crtc_state
= drm_atomic_get_crtc_state(conn_state
->state
, crtc
);
1068 if (IS_ERR(crtc_state
))
1069 return PTR_ERR(crtc_state
);
1072 conn_state
->crtc
= crtc
;
1075 DRM_DEBUG_ATOMIC("Link connector state %p to [CRTC:%d:%s]\n",
1076 conn_state
, crtc
->base
.id
, crtc
->name
);
1078 DRM_DEBUG_ATOMIC("Link connector state %p to [NOCRTC]\n",
1083 EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector
);
1086 * drm_atomic_add_affected_connectors - add connectors for crtc
1087 * @state: atomic state
1090 * This function walks the current configuration and adds all connectors
1091 * currently using @crtc to the atomic configuration @state. Note that this
1092 * function must acquire the connection mutex. This can potentially cause
1093 * unneeded seralization if the update is just for the planes on one crtc. Hence
1094 * drivers and helpers should only call this when really needed (e.g. when a
1095 * full modeset needs to happen due to some change).
1098 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1099 * then the w/w mutex code has detected a deadlock and the entire atomic
1100 * sequence must be restarted. All other errors are fatal.
1103 drm_atomic_add_affected_connectors(struct drm_atomic_state
*state
,
1104 struct drm_crtc
*crtc
)
1106 struct drm_mode_config
*config
= &state
->dev
->mode_config
;
1107 struct drm_connector
*connector
;
1108 struct drm_connector_state
*conn_state
;
1111 ret
= drm_modeset_lock(&config
->connection_mutex
, state
->acquire_ctx
);
1115 DRM_DEBUG_ATOMIC("Adding all current connectors for [CRTC:%d:%s] to %p\n",
1116 crtc
->base
.id
, crtc
->name
, state
);
1119 * Changed connectors are already in @state, so only need to look at the
1120 * current configuration.
1122 drm_for_each_connector(connector
, state
->dev
) {
1123 if (connector
->state
->crtc
!= crtc
)
1126 conn_state
= drm_atomic_get_connector_state(state
, connector
);
1127 if (IS_ERR(conn_state
))
1128 return PTR_ERR(conn_state
);
1133 EXPORT_SYMBOL(drm_atomic_add_affected_connectors
);
1136 * drm_atomic_add_affected_planes - add planes for crtc
1137 * @state: atomic state
1140 * This function walks the current configuration and adds all planes
1141 * currently used by @crtc to the atomic configuration @state. This is useful
1142 * when an atomic commit also needs to check all currently enabled plane on
1143 * @crtc, e.g. when changing the mode. It's also useful when re-enabling a CRTC
1144 * to avoid special code to force-enable all planes.
1146 * Since acquiring a plane state will always also acquire the w/w mutex of the
1147 * current CRTC for that plane (if there is any) adding all the plane states for
1148 * a CRTC will not reduce parallism of atomic updates.
1151 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1152 * then the w/w mutex code has detected a deadlock and the entire atomic
1153 * sequence must be restarted. All other errors are fatal.
1156 drm_atomic_add_affected_planes(struct drm_atomic_state
*state
,
1157 struct drm_crtc
*crtc
)
1159 struct drm_plane
*plane
;
1161 WARN_ON(!drm_atomic_get_existing_crtc_state(state
, crtc
));
1163 drm_for_each_plane_mask(plane
, state
->dev
, crtc
->state
->plane_mask
) {
1164 struct drm_plane_state
*plane_state
=
1165 drm_atomic_get_plane_state(state
, plane
);
1167 if (IS_ERR(plane_state
))
1168 return PTR_ERR(plane_state
);
1172 EXPORT_SYMBOL(drm_atomic_add_affected_planes
);
1175 * drm_atomic_connectors_for_crtc - count number of connected outputs
1176 * @state: atomic state
1179 * This function counts all connectors which will be connected to @crtc
1180 * according to @state. Useful to recompute the enable state for @crtc.
1183 drm_atomic_connectors_for_crtc(struct drm_atomic_state
*state
,
1184 struct drm_crtc
*crtc
)
1186 struct drm_connector
*connector
;
1187 struct drm_connector_state
*conn_state
;
1189 int i
, num_connected_connectors
= 0;
1191 for_each_connector_in_state(state
, connector
, conn_state
, i
) {
1192 if (conn_state
->crtc
== crtc
)
1193 num_connected_connectors
++;
1196 DRM_DEBUG_ATOMIC("State %p has %i connectors for [CRTC:%d:%s]\n",
1197 state
, num_connected_connectors
,
1198 crtc
->base
.id
, crtc
->name
);
1200 return num_connected_connectors
;
1202 EXPORT_SYMBOL(drm_atomic_connectors_for_crtc
);
1205 * drm_atomic_legacy_backoff - locking backoff for legacy ioctls
1206 * @state: atomic state
1208 * This function should be used by legacy entry points which don't understand
1209 * -EDEADLK semantics. For simplicity this one will grab all modeset locks after
1210 * the slowpath completed.
1212 void drm_atomic_legacy_backoff(struct drm_atomic_state
*state
)
1217 drm_modeset_backoff(state
->acquire_ctx
);
1219 ret
= drm_modeset_lock_all_ctx(state
->dev
, state
->acquire_ctx
);
1223 EXPORT_SYMBOL(drm_atomic_legacy_backoff
);
1226 * drm_atomic_check_only - check whether a given config would work
1227 * @state: atomic configuration to check
1229 * Note that this function can return -EDEADLK if the driver needed to acquire
1230 * more locks but encountered a deadlock. The caller must then do the usual w/w
1231 * backoff dance and restart. All other errors are fatal.
1234 * 0 on success, negative error code on failure.
1236 int drm_atomic_check_only(struct drm_atomic_state
*state
)
1238 struct drm_device
*dev
= state
->dev
;
1239 struct drm_mode_config
*config
= &dev
->mode_config
;
1240 struct drm_plane
*plane
;
1241 struct drm_plane_state
*plane_state
;
1242 struct drm_crtc
*crtc
;
1243 struct drm_crtc_state
*crtc_state
;
1246 DRM_DEBUG_ATOMIC("checking %p\n", state
);
1248 for_each_plane_in_state(state
, plane
, plane_state
, i
) {
1249 ret
= drm_atomic_plane_check(plane
, plane_state
);
1251 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic core check failed\n",
1252 plane
->base
.id
, plane
->name
);
1257 for_each_crtc_in_state(state
, crtc
, crtc_state
, i
) {
1258 ret
= drm_atomic_crtc_check(crtc
, crtc_state
);
1260 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic core check failed\n",
1261 crtc
->base
.id
, crtc
->name
);
1266 if (config
->funcs
->atomic_check
)
1267 ret
= config
->funcs
->atomic_check(state
->dev
, state
);
1269 if (!state
->allow_modeset
) {
1270 for_each_crtc_in_state(state
, crtc
, crtc_state
, i
) {
1271 if (drm_atomic_crtc_needs_modeset(crtc_state
)) {
1272 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] requires full modeset\n",
1273 crtc
->base
.id
, crtc
->name
);
1281 EXPORT_SYMBOL(drm_atomic_check_only
);
1284 * drm_atomic_commit - commit configuration atomically
1285 * @state: atomic configuration to check
1287 * Note that this function can return -EDEADLK if the driver needed to acquire
1288 * more locks but encountered a deadlock. The caller must then do the usual w/w
1289 * backoff dance and restart. All other errors are fatal.
1291 * Also note that on successful execution ownership of @state is transferred
1292 * from the caller of this function to the function itself. The caller must not
1293 * free or in any other way access @state. If the function fails then the caller
1294 * must clean up @state itself.
1297 * 0 on success, negative error code on failure.
1299 int drm_atomic_commit(struct drm_atomic_state
*state
)
1301 struct drm_mode_config
*config
= &state
->dev
->mode_config
;
1304 ret
= drm_atomic_check_only(state
);
1308 DRM_DEBUG_ATOMIC("commiting %p\n", state
);
1310 return config
->funcs
->atomic_commit(state
->dev
, state
, false);
1312 EXPORT_SYMBOL(drm_atomic_commit
);
1315 * drm_atomic_async_commit - atomic&async configuration commit
1316 * @state: atomic configuration to check
1318 * Note that this function can return -EDEADLK if the driver needed to acquire
1319 * more locks but encountered a deadlock. The caller must then do the usual w/w
1320 * backoff dance and restart. All other errors are fatal.
1322 * Also note that on successful execution ownership of @state is transferred
1323 * from the caller of this function to the function itself. The caller must not
1324 * free or in any other way access @state. If the function fails then the caller
1325 * must clean up @state itself.
1328 * 0 on success, negative error code on failure.
1330 int drm_atomic_async_commit(struct drm_atomic_state
*state
)
1332 struct drm_mode_config
*config
= &state
->dev
->mode_config
;
1335 ret
= drm_atomic_check_only(state
);
1339 DRM_DEBUG_ATOMIC("commiting %p asynchronously\n", state
);
1341 return config
->funcs
->atomic_commit(state
->dev
, state
, true);
1343 EXPORT_SYMBOL(drm_atomic_async_commit
);
1346 * The big monstor ioctl
1349 static struct drm_pending_vblank_event
*create_vblank_event(
1350 struct drm_device
*dev
, struct drm_file
*file_priv
, uint64_t user_data
)
1352 struct drm_pending_vblank_event
*e
= NULL
;
1353 unsigned long flags
;
1355 spin_lock_irqsave(&dev
->event_lock
, flags
);
1356 if (file_priv
->event_space
< sizeof e
->event
) {
1357 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
1360 file_priv
->event_space
-= sizeof e
->event
;
1361 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
1363 e
= kzalloc(sizeof *e
, GFP_KERNEL
);
1365 spin_lock_irqsave(&dev
->event_lock
, flags
);
1366 file_priv
->event_space
+= sizeof e
->event
;
1367 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
1371 e
->event
.base
.type
= DRM_EVENT_FLIP_COMPLETE
;
1372 e
->event
.base
.length
= sizeof e
->event
;
1373 e
->event
.user_data
= user_data
;
1374 e
->base
.event
= &e
->event
.base
;
1375 e
->base
.file_priv
= file_priv
;
1376 e
->base
.destroy
= (void (*) (struct drm_pending_event
*)) kfree
;
1382 static void destroy_vblank_event(struct drm_device
*dev
,
1383 struct drm_file
*file_priv
, struct drm_pending_vblank_event
*e
)
1385 unsigned long flags
;
1387 spin_lock_irqsave(&dev
->event_lock
, flags
);
1388 file_priv
->event_space
+= sizeof e
->event
;
1389 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
1393 static int atomic_set_prop(struct drm_atomic_state
*state
,
1394 struct drm_mode_object
*obj
, struct drm_property
*prop
,
1395 uint64_t prop_value
)
1397 struct drm_mode_object
*ref
;
1400 if (!drm_property_change_valid_get(prop
, prop_value
, &ref
))
1403 switch (obj
->type
) {
1404 case DRM_MODE_OBJECT_CONNECTOR
: {
1405 struct drm_connector
*connector
= obj_to_connector(obj
);
1406 struct drm_connector_state
*connector_state
;
1408 connector_state
= drm_atomic_get_connector_state(state
, connector
);
1409 if (IS_ERR(connector_state
)) {
1410 ret
= PTR_ERR(connector_state
);
1414 ret
= drm_atomic_connector_set_property(connector
,
1415 connector_state
, prop
, prop_value
);
1418 case DRM_MODE_OBJECT_CRTC
: {
1419 struct drm_crtc
*crtc
= obj_to_crtc(obj
);
1420 struct drm_crtc_state
*crtc_state
;
1422 crtc_state
= drm_atomic_get_crtc_state(state
, crtc
);
1423 if (IS_ERR(crtc_state
)) {
1424 ret
= PTR_ERR(crtc_state
);
1428 ret
= drm_atomic_crtc_set_property(crtc
,
1429 crtc_state
, prop
, prop_value
);
1432 case DRM_MODE_OBJECT_PLANE
: {
1433 struct drm_plane
*plane
= obj_to_plane(obj
);
1434 struct drm_plane_state
*plane_state
;
1436 plane_state
= drm_atomic_get_plane_state(state
, plane
);
1437 if (IS_ERR(plane_state
)) {
1438 ret
= PTR_ERR(plane_state
);
1442 ret
= drm_atomic_plane_set_property(plane
,
1443 plane_state
, prop
, prop_value
);
1451 drm_property_change_valid_put(prop
, ref
);
1456 * drm_atomic_clean_old_fb -- Unset old_fb pointers and set plane->fb pointers.
1458 * @dev: drm device to check.
1459 * @plane_mask: plane mask for planes that were updated.
1460 * @ret: return value, can be -EDEADLK for a retry.
1462 * Before doing an update plane->old_fb is set to plane->fb,
1463 * but before dropping the locks old_fb needs to be set to NULL
1464 * and plane->fb updated. This is a common operation for each
1465 * atomic update, so this call is split off as a helper.
1467 void drm_atomic_clean_old_fb(struct drm_device
*dev
,
1468 unsigned plane_mask
,
1471 struct drm_plane
*plane
;
1473 /* if succeeded, fixup legacy plane crtc/fb ptrs before dropping
1474 * locks (ie. while it is still safe to deref plane->state). We
1475 * need to do this here because the driver entry points cannot
1476 * distinguish between legacy and atomic ioctls.
1478 drm_for_each_plane_mask(plane
, dev
, plane_mask
) {
1480 struct drm_framebuffer
*new_fb
= plane
->state
->fb
;
1482 drm_framebuffer_reference(new_fb
);
1484 plane
->crtc
= plane
->state
->crtc
;
1487 drm_framebuffer_unreference(plane
->old_fb
);
1489 plane
->old_fb
= NULL
;
1492 EXPORT_SYMBOL(drm_atomic_clean_old_fb
);
1494 int drm_mode_atomic_ioctl(struct drm_device
*dev
,
1495 void *data
, struct drm_file
*file_priv
)
1497 struct drm_mode_atomic
*arg
= data
;
1498 uint32_t __user
*objs_ptr
= (uint32_t __user
*)(unsigned long)(arg
->objs_ptr
);
1499 uint32_t __user
*count_props_ptr
= (uint32_t __user
*)(unsigned long)(arg
->count_props_ptr
);
1500 uint32_t __user
*props_ptr
= (uint32_t __user
*)(unsigned long)(arg
->props_ptr
);
1501 uint64_t __user
*prop_values_ptr
= (uint64_t __user
*)(unsigned long)(arg
->prop_values_ptr
);
1502 unsigned int copied_objs
, copied_props
;
1503 struct drm_atomic_state
*state
;
1504 struct drm_modeset_acquire_ctx ctx
;
1505 struct drm_plane
*plane
;
1506 struct drm_crtc
*crtc
;
1507 struct drm_crtc_state
*crtc_state
;
1508 unsigned plane_mask
;
1512 /* disallow for drivers not supporting atomic: */
1513 if (!drm_core_check_feature(dev
, DRIVER_ATOMIC
))
1516 /* disallow for userspace that has not enabled atomic cap (even
1517 * though this may be a bit overkill, since legacy userspace
1518 * wouldn't know how to call this ioctl)
1520 if (!file_priv
->atomic
)
1523 if (arg
->flags
& ~DRM_MODE_ATOMIC_FLAGS
)
1529 if ((arg
->flags
& DRM_MODE_PAGE_FLIP_ASYNC
) &&
1530 !dev
->mode_config
.async_page_flip
)
1533 /* can't test and expect an event at the same time. */
1534 if ((arg
->flags
& DRM_MODE_ATOMIC_TEST_ONLY
) &&
1535 (arg
->flags
& DRM_MODE_PAGE_FLIP_EVENT
))
1538 drm_modeset_acquire_init(&ctx
, 0);
1540 state
= drm_atomic_state_alloc(dev
);
1544 state
->acquire_ctx
= &ctx
;
1545 state
->allow_modeset
= !!(arg
->flags
& DRM_MODE_ATOMIC_ALLOW_MODESET
);
1552 for (i
= 0; i
< arg
->count_objs
; i
++) {
1553 uint32_t obj_id
, count_props
;
1554 struct drm_mode_object
*obj
;
1556 if (get_user(obj_id
, objs_ptr
+ copied_objs
)) {
1561 obj
= drm_mode_object_find(dev
, obj_id
, DRM_MODE_OBJECT_ANY
);
1562 if (!obj
|| !obj
->properties
) {
1567 if (get_user(count_props
, count_props_ptr
+ copied_objs
)) {
1574 for (j
= 0; j
< count_props
; j
++) {
1576 uint64_t prop_value
;
1577 struct drm_property
*prop
;
1579 if (get_user(prop_id
, props_ptr
+ copied_props
)) {
1584 prop
= drm_property_find(dev
, prop_id
);
1590 if (copy_from_user(&prop_value
,
1591 prop_values_ptr
+ copied_props
,
1592 sizeof(prop_value
))) {
1597 ret
= atomic_set_prop(state
, obj
, prop
, prop_value
);
1604 if (obj
->type
== DRM_MODE_OBJECT_PLANE
&& count_props
&&
1605 !(arg
->flags
& DRM_MODE_ATOMIC_TEST_ONLY
)) {
1606 plane
= obj_to_plane(obj
);
1607 plane_mask
|= (1 << drm_plane_index(plane
));
1608 plane
->old_fb
= plane
->fb
;
1612 if (arg
->flags
& DRM_MODE_PAGE_FLIP_EVENT
) {
1613 for_each_crtc_in_state(state
, crtc
, crtc_state
, i
) {
1614 struct drm_pending_vblank_event
*e
;
1616 e
= create_vblank_event(dev
, file_priv
, arg
->user_data
);
1622 crtc_state
->event
= e
;
1626 if (arg
->flags
& DRM_MODE_ATOMIC_TEST_ONLY
) {
1628 * Unlike commit, check_only does not clean up state.
1629 * Below we call drm_atomic_state_free for it.
1631 ret
= drm_atomic_check_only(state
);
1632 } else if (arg
->flags
& DRM_MODE_ATOMIC_NONBLOCK
) {
1633 ret
= drm_atomic_async_commit(state
);
1635 ret
= drm_atomic_commit(state
);
1639 drm_atomic_clean_old_fb(dev
, plane_mask
, ret
);
1641 if (ret
&& arg
->flags
& DRM_MODE_PAGE_FLIP_EVENT
) {
1643 * TEST_ONLY and PAGE_FLIP_EVENT are mutually exclusive,
1644 * if they weren't, this code should be called on success
1645 * for TEST_ONLY too.
1648 for_each_crtc_in_state(state
, crtc
, crtc_state
, i
) {
1649 if (!crtc_state
->event
)
1652 destroy_vblank_event(dev
, file_priv
,
1657 if (ret
== -EDEADLK
) {
1658 drm_atomic_state_clear(state
);
1659 drm_modeset_backoff(&ctx
);
1663 if (ret
|| arg
->flags
& DRM_MODE_ATOMIC_TEST_ONLY
)
1664 drm_atomic_state_free(state
);
1666 drm_modeset_drop_locks(&ctx
);
1667 drm_modeset_acquire_fini(&ctx
);