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>
33 static void kfree_state(struct drm_atomic_state
*state
)
35 kfree(state
->connectors
);
36 kfree(state
->connector_states
);
38 kfree(state
->crtc_states
);
40 kfree(state
->plane_states
);
45 * drm_atomic_state_alloc - allocate atomic state
48 * This allocates an empty atomic state to track updates.
50 struct drm_atomic_state
*
51 drm_atomic_state_alloc(struct drm_device
*dev
)
53 struct drm_atomic_state
*state
;
55 state
= kzalloc(sizeof(*state
), GFP_KERNEL
);
59 /* TODO legacy paths should maybe do a better job about
60 * setting this appropriately?
62 state
->allow_modeset
= true;
64 state
->num_connector
= ACCESS_ONCE(dev
->mode_config
.num_connector
);
66 state
->crtcs
= kcalloc(dev
->mode_config
.num_crtc
,
67 sizeof(*state
->crtcs
), GFP_KERNEL
);
70 state
->crtc_states
= kcalloc(dev
->mode_config
.num_crtc
,
71 sizeof(*state
->crtc_states
), GFP_KERNEL
);
72 if (!state
->crtc_states
)
74 state
->planes
= kcalloc(dev
->mode_config
.num_total_plane
,
75 sizeof(*state
->planes
), GFP_KERNEL
);
78 state
->plane_states
= kcalloc(dev
->mode_config
.num_total_plane
,
79 sizeof(*state
->plane_states
), GFP_KERNEL
);
80 if (!state
->plane_states
)
82 state
->connectors
= kcalloc(state
->num_connector
,
83 sizeof(*state
->connectors
),
85 if (!state
->connectors
)
87 state
->connector_states
= kcalloc(state
->num_connector
,
88 sizeof(*state
->connector_states
),
90 if (!state
->connector_states
)
95 DRM_DEBUG_KMS("Allocate atomic state %p\n", state
);
103 EXPORT_SYMBOL(drm_atomic_state_alloc
);
106 * drm_atomic_state_clear - clear state object
107 * @state: atomic state
109 * When the w/w mutex algorithm detects a deadlock we need to back off and drop
110 * all locks. So someone else could sneak in and change the current modeset
111 * configuration. Which means that all the state assembled in @state is no
112 * longer an atomic update to the current state, but to some arbitrary earlier
113 * state. Which could break assumptions the driver's ->atomic_check likely
116 * Hence we must clear all cached state and completely start over, using this
119 void drm_atomic_state_clear(struct drm_atomic_state
*state
)
121 struct drm_device
*dev
= state
->dev
;
122 struct drm_mode_config
*config
= &dev
->mode_config
;
125 DRM_DEBUG_KMS("Clearing atomic state %p\n", state
);
127 for (i
= 0; i
< state
->num_connector
; i
++) {
128 struct drm_connector
*connector
= state
->connectors
[i
];
133 WARN_ON(!drm_modeset_is_locked(&config
->connection_mutex
));
135 connector
->funcs
->atomic_destroy_state(connector
,
136 state
->connector_states
[i
]);
137 state
->connector_states
[i
] = NULL
;
140 for (i
= 0; i
< config
->num_crtc
; i
++) {
141 struct drm_crtc
*crtc
= state
->crtcs
[i
];
146 crtc
->funcs
->atomic_destroy_state(crtc
,
147 state
->crtc_states
[i
]);
148 state
->crtc_states
[i
] = NULL
;
151 for (i
= 0; i
< config
->num_total_plane
; i
++) {
152 struct drm_plane
*plane
= state
->planes
[i
];
157 plane
->funcs
->atomic_destroy_state(plane
,
158 state
->plane_states
[i
]);
159 state
->plane_states
[i
] = NULL
;
162 EXPORT_SYMBOL(drm_atomic_state_clear
);
165 * drm_atomic_state_free - free all memory for an atomic state
166 * @state: atomic state to deallocate
168 * This frees all memory associated with an atomic state, including all the
169 * per-object state for planes, crtcs and connectors.
171 void drm_atomic_state_free(struct drm_atomic_state
*state
)
173 drm_atomic_state_clear(state
);
175 DRM_DEBUG_KMS("Freeing atomic state %p\n", state
);
179 EXPORT_SYMBOL(drm_atomic_state_free
);
182 * drm_atomic_get_crtc_state - get crtc state
183 * @state: global atomic state object
184 * @crtc: crtc to get state object for
186 * This function returns the crtc state for the given crtc, allocating it if
187 * needed. It will also grab the relevant crtc lock to make sure that the state
192 * Either the allocated state or the error code encoded into the pointer. When
193 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
194 * entire atomic sequence must be restarted. All other errors are fatal.
196 struct drm_crtc_state
*
197 drm_atomic_get_crtc_state(struct drm_atomic_state
*state
,
198 struct drm_crtc
*crtc
)
201 struct drm_crtc_state
*crtc_state
;
203 index
= drm_crtc_index(crtc
);
205 if (state
->crtc_states
[index
])
206 return state
->crtc_states
[index
];
208 ret
= drm_modeset_lock(&crtc
->mutex
, state
->acquire_ctx
);
212 crtc_state
= crtc
->funcs
->atomic_duplicate_state(crtc
);
214 return ERR_PTR(-ENOMEM
);
216 state
->crtc_states
[index
] = crtc_state
;
217 state
->crtcs
[index
] = crtc
;
218 crtc_state
->state
= state
;
220 DRM_DEBUG_KMS("Added [CRTC:%d] %p state to %p\n",
221 crtc
->base
.id
, crtc_state
, state
);
225 EXPORT_SYMBOL(drm_atomic_get_crtc_state
);
228 * drm_atomic_crtc_set_property - set property on CRTC
229 * @crtc: the drm CRTC to set a property on
230 * @state: the state object to update with the new property value
231 * @property: the property to set
232 * @val: the new property value
234 * Use this instead of calling crtc->atomic_set_property directly.
235 * This function handles generic/core properties and calls out to
236 * driver's ->atomic_set_property() for driver properties. To ensure
237 * consistent behavior you must call this function rather than the
238 * driver hook directly.
241 * Zero on success, error code on failure
243 int drm_atomic_crtc_set_property(struct drm_crtc
*crtc
,
244 struct drm_crtc_state
*state
, struct drm_property
*property
,
247 struct drm_device
*dev
= crtc
->dev
;
248 struct drm_mode_config
*config
= &dev
->mode_config
;
250 /* FIXME: Mode prop is missing, which also controls ->enable. */
251 if (property
== config
->prop_active
) {
253 } else if (crtc
->funcs
->atomic_set_property
)
254 return crtc
->funcs
->atomic_set_property(crtc
, state
, property
, val
);
257 EXPORT_SYMBOL(drm_atomic_crtc_set_property
);
260 * This function handles generic/core properties and calls out to
261 * driver's ->atomic_get_property() for driver properties. To ensure
262 * consistent behavior you must call this function rather than the
263 * driver hook directly.
265 int drm_atomic_crtc_get_property(struct drm_crtc
*crtc
,
266 const struct drm_crtc_state
*state
,
267 struct drm_property
*property
, uint64_t *val
)
269 if (crtc
->funcs
->atomic_get_property
)
270 return crtc
->funcs
->atomic_get_property(crtc
, state
, property
, val
);
275 * drm_atomic_crtc_check - check crtc state
276 * @crtc: crtc to check
277 * @state: crtc state to check
279 * Provides core sanity checks for crtc state.
282 * Zero on success, error code on failure
284 static int drm_atomic_crtc_check(struct drm_crtc
*crtc
,
285 struct drm_crtc_state
*state
)
287 /* NOTE: we explicitly don't enforce constraints such as primary
288 * layer covering entire screen, since that is something we want
289 * to allow (on hw that supports it). For hw that does not, it
290 * should be checked in driver's crtc->atomic_check() vfunc.
292 * TODO: Add generic modeset state checks once we support those.
295 if (state
->active
&& !state
->enable
) {
296 DRM_DEBUG_KMS("[CRTC:%d] active without enabled\n",
305 * drm_atomic_get_plane_state - get plane state
306 * @state: global atomic state object
307 * @plane: plane to get state object for
309 * This function returns the plane state for the given plane, allocating it if
310 * needed. It will also grab the relevant plane lock to make sure that the state
315 * Either the allocated state or the error code encoded into the pointer. When
316 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
317 * entire atomic sequence must be restarted. All other errors are fatal.
319 struct drm_plane_state
*
320 drm_atomic_get_plane_state(struct drm_atomic_state
*state
,
321 struct drm_plane
*plane
)
324 struct drm_plane_state
*plane_state
;
326 index
= drm_plane_index(plane
);
328 if (state
->plane_states
[index
])
329 return state
->plane_states
[index
];
331 ret
= drm_modeset_lock(&plane
->mutex
, state
->acquire_ctx
);
335 plane_state
= plane
->funcs
->atomic_duplicate_state(plane
);
337 return ERR_PTR(-ENOMEM
);
339 state
->plane_states
[index
] = plane_state
;
340 state
->planes
[index
] = plane
;
341 plane_state
->state
= state
;
343 DRM_DEBUG_KMS("Added [PLANE:%d] %p state to %p\n",
344 plane
->base
.id
, plane_state
, state
);
346 if (plane_state
->crtc
) {
347 struct drm_crtc_state
*crtc_state
;
349 crtc_state
= drm_atomic_get_crtc_state(state
,
351 if (IS_ERR(crtc_state
))
352 return ERR_CAST(crtc_state
);
357 EXPORT_SYMBOL(drm_atomic_get_plane_state
);
360 * drm_atomic_plane_set_property - set property on plane
361 * @plane: the drm plane to set a property on
362 * @state: the state object to update with the new property value
363 * @property: the property to set
364 * @val: the new property value
366 * Use this instead of calling plane->atomic_set_property directly.
367 * This function handles generic/core properties and calls out to
368 * driver's ->atomic_set_property() for driver properties. To ensure
369 * consistent behavior you must call this function rather than the
370 * driver hook directly.
373 * Zero on success, error code on failure
375 int drm_atomic_plane_set_property(struct drm_plane
*plane
,
376 struct drm_plane_state
*state
, struct drm_property
*property
,
379 struct drm_device
*dev
= plane
->dev
;
380 struct drm_mode_config
*config
= &dev
->mode_config
;
382 if (property
== config
->prop_fb_id
) {
383 struct drm_framebuffer
*fb
= drm_framebuffer_lookup(dev
, val
);
384 drm_atomic_set_fb_for_plane(state
, fb
);
386 drm_framebuffer_unreference(fb
);
387 } else if (property
== config
->prop_crtc_id
) {
388 struct drm_crtc
*crtc
= drm_crtc_find(dev
, val
);
389 return drm_atomic_set_crtc_for_plane(state
, crtc
);
390 } else if (property
== config
->prop_crtc_x
) {
391 state
->crtc_x
= U642I64(val
);
392 } else if (property
== config
->prop_crtc_y
) {
393 state
->crtc_y
= U642I64(val
);
394 } else if (property
== config
->prop_crtc_w
) {
396 } else if (property
== config
->prop_crtc_h
) {
398 } else if (property
== config
->prop_src_x
) {
400 } else if (property
== config
->prop_src_y
) {
402 } else if (property
== config
->prop_src_w
) {
404 } else if (property
== config
->prop_src_h
) {
406 } else if (property
== config
->rotation_property
) {
407 state
->rotation
= val
;
408 } else if (plane
->funcs
->atomic_set_property
) {
409 return plane
->funcs
->atomic_set_property(plane
, state
,
417 EXPORT_SYMBOL(drm_atomic_plane_set_property
);
420 * This function handles generic/core properties and calls out to
421 * driver's ->atomic_get_property() for driver properties. To ensure
422 * consistent behavior you must call this function rather than the
423 * driver hook directly.
426 drm_atomic_plane_get_property(struct drm_plane
*plane
,
427 const struct drm_plane_state
*state
,
428 struct drm_property
*property
, uint64_t *val
)
430 struct drm_device
*dev
= plane
->dev
;
431 struct drm_mode_config
*config
= &dev
->mode_config
;
433 if (property
== config
->prop_fb_id
) {
434 *val
= (state
->fb
) ? state
->fb
->base
.id
: 0;
435 } else if (property
== config
->prop_crtc_id
) {
436 *val
= (state
->crtc
) ? state
->crtc
->base
.id
: 0;
437 } else if (property
== config
->prop_crtc_x
) {
438 *val
= I642U64(state
->crtc_x
);
439 } else if (property
== config
->prop_crtc_y
) {
440 *val
= I642U64(state
->crtc_y
);
441 } else if (property
== config
->prop_crtc_w
) {
442 *val
= state
->crtc_w
;
443 } else if (property
== config
->prop_crtc_h
) {
444 *val
= state
->crtc_h
;
445 } else if (property
== config
->prop_src_x
) {
447 } else if (property
== config
->prop_src_y
) {
449 } else if (property
== config
->prop_src_w
) {
451 } else if (property
== config
->prop_src_h
) {
453 } else if (plane
->funcs
->atomic_get_property
) {
454 return plane
->funcs
->atomic_get_property(plane
, state
, property
, val
);
463 * drm_atomic_plane_check - check plane state
464 * @plane: plane to check
465 * @state: plane state to check
467 * Provides core sanity checks for plane state.
470 * Zero on success, error code on failure
472 static int drm_atomic_plane_check(struct drm_plane
*plane
,
473 struct drm_plane_state
*state
)
475 unsigned int fb_width
, fb_height
;
478 /* either *both* CRTC and FB must be set, or neither */
479 if (WARN_ON(state
->crtc
&& !state
->fb
)) {
480 DRM_DEBUG_KMS("CRTC set but no FB\n");
482 } else if (WARN_ON(state
->fb
&& !state
->crtc
)) {
483 DRM_DEBUG_KMS("FB set but no CRTC\n");
487 /* if disabled, we don't care about the rest of the state: */
491 /* Check whether this plane is usable on this CRTC */
492 if (!(plane
->possible_crtcs
& drm_crtc_mask(state
->crtc
))) {
493 DRM_DEBUG_KMS("Invalid crtc for plane\n");
497 /* Check whether this plane supports the fb pixel format. */
498 for (i
= 0; i
< plane
->format_count
; i
++)
499 if (state
->fb
->pixel_format
== plane
->format_types
[i
])
501 if (i
== plane
->format_count
) {
502 DRM_DEBUG_KMS("Invalid pixel format %s\n",
503 drm_get_format_name(state
->fb
->pixel_format
));
507 /* Give drivers some help against integer overflows */
508 if (state
->crtc_w
> INT_MAX
||
509 state
->crtc_x
> INT_MAX
- (int32_t) state
->crtc_w
||
510 state
->crtc_h
> INT_MAX
||
511 state
->crtc_y
> INT_MAX
- (int32_t) state
->crtc_h
) {
512 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
513 state
->crtc_w
, state
->crtc_h
,
514 state
->crtc_x
, state
->crtc_y
);
518 fb_width
= state
->fb
->width
<< 16;
519 fb_height
= state
->fb
->height
<< 16;
521 /* Make sure source coordinates are inside the fb. */
522 if (state
->src_w
> fb_width
||
523 state
->src_x
> fb_width
- state
->src_w
||
524 state
->src_h
> fb_height
||
525 state
->src_y
> fb_height
- state
->src_h
) {
526 DRM_DEBUG_KMS("Invalid source coordinates "
527 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
528 state
->src_w
>> 16, ((state
->src_w
& 0xffff) * 15625) >> 10,
529 state
->src_h
>> 16, ((state
->src_h
& 0xffff) * 15625) >> 10,
530 state
->src_x
>> 16, ((state
->src_x
& 0xffff) * 15625) >> 10,
531 state
->src_y
>> 16, ((state
->src_y
& 0xffff) * 15625) >> 10);
539 * drm_atomic_get_connector_state - get connector state
540 * @state: global atomic state object
541 * @connector: connector to get state object for
543 * This function returns the connector state for the given connector,
544 * allocating it if needed. It will also grab the relevant connector lock to
545 * make sure that the state is consistent.
549 * Either the allocated state or the error code encoded into the pointer. When
550 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
551 * entire atomic sequence must be restarted. All other errors are fatal.
553 struct drm_connector_state
*
554 drm_atomic_get_connector_state(struct drm_atomic_state
*state
,
555 struct drm_connector
*connector
)
558 struct drm_mode_config
*config
= &connector
->dev
->mode_config
;
559 struct drm_connector_state
*connector_state
;
561 ret
= drm_modeset_lock(&config
->connection_mutex
, state
->acquire_ctx
);
565 index
= drm_connector_index(connector
);
568 * Construction of atomic state updates can race with a connector
569 * hot-add which might overflow. In this case flip the table and just
570 * restart the entire ioctl - no one is fast enough to livelock a cpu
571 * with physical hotplug events anyway.
573 * Note that we only grab the indexes once we have the right lock to
574 * prevent hotplug/unplugging of connectors. So removal is no problem,
575 * at most the array is a bit too large.
577 if (index
>= state
->num_connector
) {
578 DRM_DEBUG_KMS("Hot-added connector would overflow state array, restarting\n");
579 return ERR_PTR(-EAGAIN
);
582 if (state
->connector_states
[index
])
583 return state
->connector_states
[index
];
585 connector_state
= connector
->funcs
->atomic_duplicate_state(connector
);
586 if (!connector_state
)
587 return ERR_PTR(-ENOMEM
);
589 state
->connector_states
[index
] = connector_state
;
590 state
->connectors
[index
] = connector
;
591 connector_state
->state
= state
;
593 DRM_DEBUG_KMS("Added [CONNECTOR:%d] %p state to %p\n",
594 connector
->base
.id
, connector_state
, state
);
596 if (connector_state
->crtc
) {
597 struct drm_crtc_state
*crtc_state
;
599 crtc_state
= drm_atomic_get_crtc_state(state
,
600 connector_state
->crtc
);
601 if (IS_ERR(crtc_state
))
602 return ERR_CAST(crtc_state
);
605 return connector_state
;
607 EXPORT_SYMBOL(drm_atomic_get_connector_state
);
610 * drm_atomic_connector_set_property - set property on connector.
611 * @connector: the drm connector to set a property on
612 * @state: the state object to update with the new property value
613 * @property: the property to set
614 * @val: the new property value
616 * Use this instead of calling connector->atomic_set_property directly.
617 * This function handles generic/core properties and calls out to
618 * driver's ->atomic_set_property() for driver properties. To ensure
619 * consistent behavior you must call this function rather than the
620 * driver hook directly.
623 * Zero on success, error code on failure
625 int drm_atomic_connector_set_property(struct drm_connector
*connector
,
626 struct drm_connector_state
*state
, struct drm_property
*property
,
629 struct drm_device
*dev
= connector
->dev
;
630 struct drm_mode_config
*config
= &dev
->mode_config
;
632 if (property
== config
->prop_crtc_id
) {
633 struct drm_crtc
*crtc
= drm_crtc_find(dev
, val
);
634 return drm_atomic_set_crtc_for_connector(state
, crtc
);
635 } else if (property
== config
->dpms_property
) {
636 /* setting DPMS property requires special handling, which
637 * is done in legacy setprop path for us. Disallow (for
638 * now?) atomic writes to DPMS property:
641 } else if (connector
->funcs
->atomic_set_property
) {
642 return connector
->funcs
->atomic_set_property(connector
,
643 state
, property
, val
);
648 EXPORT_SYMBOL(drm_atomic_connector_set_property
);
651 * This function handles generic/core properties and calls out to
652 * driver's ->atomic_get_property() for driver properties. To ensure
653 * consistent behavior you must call this function rather than the
654 * driver hook directly.
657 drm_atomic_connector_get_property(struct drm_connector
*connector
,
658 const struct drm_connector_state
*state
,
659 struct drm_property
*property
, uint64_t *val
)
661 struct drm_device
*dev
= connector
->dev
;
662 struct drm_mode_config
*config
= &dev
->mode_config
;
664 if (property
== config
->prop_crtc_id
) {
665 *val
= (state
->crtc
) ? state
->crtc
->base
.id
: 0;
666 } else if (property
== config
->dpms_property
) {
667 *val
= connector
->dpms
;
668 } else if (connector
->funcs
->atomic_get_property
) {
669 return connector
->funcs
->atomic_get_property(connector
,
670 state
, property
, val
);
678 int drm_atomic_get_property(struct drm_mode_object
*obj
,
679 struct drm_property
*property
, uint64_t *val
)
681 struct drm_device
*dev
= property
->dev
;
685 case DRM_MODE_OBJECT_CONNECTOR
: {
686 struct drm_connector
*connector
= obj_to_connector(obj
);
687 WARN_ON(!drm_modeset_is_locked(&dev
->mode_config
.connection_mutex
));
688 ret
= drm_atomic_connector_get_property(connector
,
689 connector
->state
, property
, val
);
692 case DRM_MODE_OBJECT_CRTC
: {
693 struct drm_crtc
*crtc
= obj_to_crtc(obj
);
694 WARN_ON(!drm_modeset_is_locked(&crtc
->mutex
));
695 ret
= drm_atomic_crtc_get_property(crtc
,
696 crtc
->state
, property
, val
);
699 case DRM_MODE_OBJECT_PLANE
: {
700 struct drm_plane
*plane
= obj_to_plane(obj
);
701 WARN_ON(!drm_modeset_is_locked(&plane
->mutex
));
702 ret
= drm_atomic_plane_get_property(plane
,
703 plane
->state
, property
, val
);
715 * drm_atomic_set_crtc_for_plane - set crtc for plane
716 * @plane_state: the plane whose incoming state to update
717 * @crtc: crtc to use for the plane
719 * Changing the assigned crtc for a plane requires us to grab the lock and state
720 * for the new crtc, as needed. This function takes care of all these details
721 * besides updating the pointer in the state object itself.
724 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
725 * then the w/w mutex code has detected a deadlock and the entire atomic
726 * sequence must be restarted. All other errors are fatal.
729 drm_atomic_set_crtc_for_plane(struct drm_plane_state
*plane_state
,
730 struct drm_crtc
*crtc
)
732 struct drm_plane
*plane
= plane_state
->plane
;
733 struct drm_crtc_state
*crtc_state
;
735 if (plane_state
->crtc
) {
736 crtc_state
= drm_atomic_get_crtc_state(plane_state
->state
,
738 if (WARN_ON(IS_ERR(crtc_state
)))
739 return PTR_ERR(crtc_state
);
741 crtc_state
->plane_mask
&= ~(1 << drm_plane_index(plane
));
744 plane_state
->crtc
= crtc
;
747 crtc_state
= drm_atomic_get_crtc_state(plane_state
->state
,
749 if (IS_ERR(crtc_state
))
750 return PTR_ERR(crtc_state
);
751 crtc_state
->plane_mask
|= (1 << drm_plane_index(plane
));
755 DRM_DEBUG_KMS("Link plane state %p to [CRTC:%d]\n",
756 plane_state
, crtc
->base
.id
);
758 DRM_DEBUG_KMS("Link plane state %p to [NOCRTC]\n", plane_state
);
762 EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane
);
765 * drm_atomic_set_fb_for_plane - set crtc for plane
766 * @plane_state: atomic state object for the plane
767 * @fb: fb to use for the plane
769 * Changing the assigned framebuffer for a plane requires us to grab a reference
770 * to the new fb and drop the reference to the old fb, if there is one. This
771 * function takes care of all these details besides updating the pointer in the
772 * state object itself.
775 drm_atomic_set_fb_for_plane(struct drm_plane_state
*plane_state
,
776 struct drm_framebuffer
*fb
)
779 drm_framebuffer_unreference(plane_state
->fb
);
781 drm_framebuffer_reference(fb
);
782 plane_state
->fb
= fb
;
785 DRM_DEBUG_KMS("Set [FB:%d] for plane state %p\n",
786 fb
->base
.id
, plane_state
);
788 DRM_DEBUG_KMS("Set [NOFB] for plane state %p\n", plane_state
);
790 EXPORT_SYMBOL(drm_atomic_set_fb_for_plane
);
793 * drm_atomic_set_crtc_for_connector - set crtc for connector
794 * @conn_state: atomic state object for the connector
795 * @crtc: crtc to use for the connector
797 * Changing the assigned crtc for a connector requires us to grab the lock and
798 * state for the new crtc, as needed. This function takes care of all these
799 * details besides updating the pointer in the state object itself.
802 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
803 * then the w/w mutex code has detected a deadlock and the entire atomic
804 * sequence must be restarted. All other errors are fatal.
807 drm_atomic_set_crtc_for_connector(struct drm_connector_state
*conn_state
,
808 struct drm_crtc
*crtc
)
810 struct drm_crtc_state
*crtc_state
;
813 crtc_state
= drm_atomic_get_crtc_state(conn_state
->state
, crtc
);
814 if (IS_ERR(crtc_state
))
815 return PTR_ERR(crtc_state
);
818 conn_state
->crtc
= crtc
;
821 DRM_DEBUG_KMS("Link connector state %p to [CRTC:%d]\n",
822 conn_state
, crtc
->base
.id
);
824 DRM_DEBUG_KMS("Link connector state %p to [NOCRTC]\n",
829 EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector
);
832 * drm_atomic_add_affected_connectors - add connectors for crtc
833 * @state: atomic state
836 * This function walks the current configuration and adds all connectors
837 * currently using @crtc to the atomic configuration @state. Note that this
838 * function must acquire the connection mutex. This can potentially cause
839 * unneeded seralization if the update is just for the planes on one crtc. Hence
840 * drivers and helpers should only call this when really needed (e.g. when a
841 * full modeset needs to happen due to some change).
844 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
845 * then the w/w mutex code has detected a deadlock and the entire atomic
846 * sequence must be restarted. All other errors are fatal.
849 drm_atomic_add_affected_connectors(struct drm_atomic_state
*state
,
850 struct drm_crtc
*crtc
)
852 struct drm_mode_config
*config
= &state
->dev
->mode_config
;
853 struct drm_connector
*connector
;
854 struct drm_connector_state
*conn_state
;
857 ret
= drm_modeset_lock(&config
->connection_mutex
, state
->acquire_ctx
);
861 DRM_DEBUG_KMS("Adding all current connectors for [CRTC:%d] to %p\n",
862 crtc
->base
.id
, state
);
865 * Changed connectors are already in @state, so only need to look at the
866 * current configuration.
868 list_for_each_entry(connector
, &config
->connector_list
, head
) {
869 if (connector
->state
->crtc
!= crtc
)
872 conn_state
= drm_atomic_get_connector_state(state
, connector
);
873 if (IS_ERR(conn_state
))
874 return PTR_ERR(conn_state
);
879 EXPORT_SYMBOL(drm_atomic_add_affected_connectors
);
882 * drm_atomic_connectors_for_crtc - count number of connected outputs
883 * @state: atomic state
886 * This function counts all connectors which will be connected to @crtc
887 * according to @state. Useful to recompute the enable state for @crtc.
890 drm_atomic_connectors_for_crtc(struct drm_atomic_state
*state
,
891 struct drm_crtc
*crtc
)
893 int i
, num_connected_connectors
= 0;
895 for (i
= 0; i
< state
->num_connector
; i
++) {
896 struct drm_connector_state
*conn_state
;
898 conn_state
= state
->connector_states
[i
];
900 if (conn_state
&& conn_state
->crtc
== crtc
)
901 num_connected_connectors
++;
904 DRM_DEBUG_KMS("State %p has %i connectors for [CRTC:%d]\n",
905 state
, num_connected_connectors
, crtc
->base
.id
);
907 return num_connected_connectors
;
909 EXPORT_SYMBOL(drm_atomic_connectors_for_crtc
);
912 * drm_atomic_legacy_backoff - locking backoff for legacy ioctls
913 * @state: atomic state
915 * This function should be used by legacy entry points which don't understand
916 * -EDEADLK semantics. For simplicity this one will grab all modeset locks after
917 * the slowpath completed.
919 void drm_atomic_legacy_backoff(struct drm_atomic_state
*state
)
924 drm_modeset_backoff(state
->acquire_ctx
);
926 ret
= drm_modeset_lock(&state
->dev
->mode_config
.connection_mutex
,
930 ret
= drm_modeset_lock_all_crtcs(state
->dev
,
935 EXPORT_SYMBOL(drm_atomic_legacy_backoff
);
938 * drm_atomic_check_only - check whether a given config would work
939 * @state: atomic configuration to check
941 * Note that this function can return -EDEADLK if the driver needed to acquire
942 * more locks but encountered a deadlock. The caller must then do the usual w/w
943 * backoff dance and restart. All other errors are fatal.
946 * 0 on success, negative error code on failure.
948 int drm_atomic_check_only(struct drm_atomic_state
*state
)
950 struct drm_device
*dev
= state
->dev
;
951 struct drm_mode_config
*config
= &dev
->mode_config
;
952 int nplanes
= config
->num_total_plane
;
953 int ncrtcs
= config
->num_crtc
;
956 DRM_DEBUG_KMS("checking %p\n", state
);
958 for (i
= 0; i
< nplanes
; i
++) {
959 struct drm_plane
*plane
= state
->planes
[i
];
964 ret
= drm_atomic_plane_check(plane
, state
->plane_states
[i
]);
966 DRM_DEBUG_KMS("[PLANE:%d] atomic core check failed\n",
972 for (i
= 0; i
< ncrtcs
; i
++) {
973 struct drm_crtc
*crtc
= state
->crtcs
[i
];
978 ret
= drm_atomic_crtc_check(crtc
, state
->crtc_states
[i
]);
980 DRM_DEBUG_KMS("[CRTC:%d] atomic core check failed\n",
986 if (config
->funcs
->atomic_check
)
987 ret
= config
->funcs
->atomic_check(state
->dev
, state
);
989 if (!state
->allow_modeset
) {
990 for (i
= 0; i
< ncrtcs
; i
++) {
991 struct drm_crtc
*crtc
= state
->crtcs
[i
];
992 struct drm_crtc_state
*crtc_state
= state
->crtc_states
[i
];
997 if (crtc_state
->mode_changed
||
998 crtc_state
->active_changed
) {
999 DRM_DEBUG_KMS("[CRTC:%d] requires full modeset\n",
1008 EXPORT_SYMBOL(drm_atomic_check_only
);
1011 * drm_atomic_commit - commit configuration atomically
1012 * @state: atomic configuration to check
1014 * Note that this function can return -EDEADLK if the driver needed to acquire
1015 * more locks but encountered a deadlock. The caller must then do the usual w/w
1016 * backoff dance and restart. All other errors are fatal.
1018 * Also note that on successful execution ownership of @state is transferred
1019 * from the caller of this function to the function itself. The caller must not
1020 * free or in any other way access @state. If the function fails then the caller
1021 * must clean up @state itself.
1024 * 0 on success, negative error code on failure.
1026 int drm_atomic_commit(struct drm_atomic_state
*state
)
1028 struct drm_mode_config
*config
= &state
->dev
->mode_config
;
1031 ret
= drm_atomic_check_only(state
);
1035 DRM_DEBUG_KMS("commiting %p\n", state
);
1037 return config
->funcs
->atomic_commit(state
->dev
, state
, false);
1039 EXPORT_SYMBOL(drm_atomic_commit
);
1042 * drm_atomic_async_commit - atomic&async configuration commit
1043 * @state: atomic configuration to check
1045 * Note that this function can return -EDEADLK if the driver needed to acquire
1046 * more locks but encountered a deadlock. The caller must then do the usual w/w
1047 * backoff dance and restart. All other errors are fatal.
1049 * Also note that on successful execution ownership of @state is transferred
1050 * from the caller of this function to the function itself. The caller must not
1051 * free or in any other way access @state. If the function fails then the caller
1052 * must clean up @state itself.
1055 * 0 on success, negative error code on failure.
1057 int drm_atomic_async_commit(struct drm_atomic_state
*state
)
1059 struct drm_mode_config
*config
= &state
->dev
->mode_config
;
1062 ret
= drm_atomic_check_only(state
);
1066 DRM_DEBUG_KMS("commiting %p asynchronously\n", state
);
1068 return config
->funcs
->atomic_commit(state
->dev
, state
, true);
1070 EXPORT_SYMBOL(drm_atomic_async_commit
);
1073 * The big monstor ioctl
1076 static struct drm_pending_vblank_event
*create_vblank_event(
1077 struct drm_device
*dev
, struct drm_file
*file_priv
, uint64_t user_data
)
1079 struct drm_pending_vblank_event
*e
= NULL
;
1080 unsigned long flags
;
1082 spin_lock_irqsave(&dev
->event_lock
, flags
);
1083 if (file_priv
->event_space
< sizeof e
->event
) {
1084 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
1087 file_priv
->event_space
-= sizeof e
->event
;
1088 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
1090 e
= kzalloc(sizeof *e
, GFP_KERNEL
);
1092 spin_lock_irqsave(&dev
->event_lock
, flags
);
1093 file_priv
->event_space
+= sizeof e
->event
;
1094 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
1098 e
->event
.base
.type
= DRM_EVENT_FLIP_COMPLETE
;
1099 e
->event
.base
.length
= sizeof e
->event
;
1100 e
->event
.user_data
= user_data
;
1101 e
->base
.event
= &e
->event
.base
;
1102 e
->base
.file_priv
= file_priv
;
1103 e
->base
.destroy
= (void (*) (struct drm_pending_event
*)) kfree
;
1109 static void destroy_vblank_event(struct drm_device
*dev
,
1110 struct drm_file
*file_priv
, struct drm_pending_vblank_event
*e
)
1112 unsigned long flags
;
1114 spin_lock_irqsave(&dev
->event_lock
, flags
);
1115 file_priv
->event_space
+= sizeof e
->event
;
1116 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
1120 static int atomic_set_prop(struct drm_atomic_state
*state
,
1121 struct drm_mode_object
*obj
, struct drm_property
*prop
,
1122 uint64_t prop_value
)
1124 struct drm_mode_object
*ref
;
1127 if (!drm_property_change_valid_get(prop
, prop_value
, &ref
))
1130 switch (obj
->type
) {
1131 case DRM_MODE_OBJECT_CONNECTOR
: {
1132 struct drm_connector
*connector
= obj_to_connector(obj
);
1133 struct drm_connector_state
*connector_state
;
1135 connector_state
= drm_atomic_get_connector_state(state
, connector
);
1136 if (IS_ERR(connector_state
)) {
1137 ret
= PTR_ERR(connector_state
);
1141 ret
= drm_atomic_connector_set_property(connector
,
1142 connector_state
, prop
, prop_value
);
1145 case DRM_MODE_OBJECT_CRTC
: {
1146 struct drm_crtc
*crtc
= obj_to_crtc(obj
);
1147 struct drm_crtc_state
*crtc_state
;
1149 crtc_state
= drm_atomic_get_crtc_state(state
, crtc
);
1150 if (IS_ERR(crtc_state
)) {
1151 ret
= PTR_ERR(crtc_state
);
1155 ret
= drm_atomic_crtc_set_property(crtc
,
1156 crtc_state
, prop
, prop_value
);
1159 case DRM_MODE_OBJECT_PLANE
: {
1160 struct drm_plane
*plane
= obj_to_plane(obj
);
1161 struct drm_plane_state
*plane_state
;
1163 plane_state
= drm_atomic_get_plane_state(state
, plane
);
1164 if (IS_ERR(plane_state
)) {
1165 ret
= PTR_ERR(plane_state
);
1169 ret
= drm_atomic_plane_set_property(plane
,
1170 plane_state
, prop
, prop_value
);
1178 drm_property_change_valid_put(prop
, ref
);
1182 int drm_mode_atomic_ioctl(struct drm_device
*dev
,
1183 void *data
, struct drm_file
*file_priv
)
1185 struct drm_mode_atomic
*arg
= data
;
1186 uint32_t __user
*objs_ptr
= (uint32_t __user
*)(unsigned long)(arg
->objs_ptr
);
1187 uint32_t __user
*count_props_ptr
= (uint32_t __user
*)(unsigned long)(arg
->count_props_ptr
);
1188 uint32_t __user
*props_ptr
= (uint32_t __user
*)(unsigned long)(arg
->props_ptr
);
1189 uint64_t __user
*prop_values_ptr
= (uint64_t __user
*)(unsigned long)(arg
->prop_values_ptr
);
1190 unsigned int copied_objs
, copied_props
;
1191 struct drm_atomic_state
*state
;
1192 struct drm_modeset_acquire_ctx ctx
;
1193 struct drm_plane
*plane
;
1194 unsigned plane_mask
= 0;
1198 /* disallow for drivers not supporting atomic: */
1199 if (!drm_core_check_feature(dev
, DRIVER_ATOMIC
))
1202 /* disallow for userspace that has not enabled atomic cap (even
1203 * though this may be a bit overkill, since legacy userspace
1204 * wouldn't know how to call this ioctl)
1206 if (!file_priv
->atomic
)
1209 if (arg
->flags
& ~DRM_MODE_ATOMIC_FLAGS
)
1215 if ((arg
->flags
& DRM_MODE_PAGE_FLIP_ASYNC
) &&
1216 !dev
->mode_config
.async_page_flip
)
1219 /* can't test and expect an event at the same time. */
1220 if ((arg
->flags
& DRM_MODE_ATOMIC_TEST_ONLY
) &&
1221 (arg
->flags
& DRM_MODE_PAGE_FLIP_EVENT
))
1224 drm_modeset_acquire_init(&ctx
, 0);
1226 state
= drm_atomic_state_alloc(dev
);
1230 state
->acquire_ctx
= &ctx
;
1231 state
->allow_modeset
= !!(arg
->flags
& DRM_MODE_ATOMIC_ALLOW_MODESET
);
1237 for (i
= 0; i
< arg
->count_objs
; i
++) {
1238 uint32_t obj_id
, count_props
;
1239 struct drm_mode_object
*obj
;
1241 if (get_user(obj_id
, objs_ptr
+ copied_objs
)) {
1246 obj
= drm_mode_object_find(dev
, obj_id
, DRM_MODE_OBJECT_ANY
);
1247 if (!obj
|| !obj
->properties
) {
1252 if (obj
->type
== DRM_MODE_OBJECT_PLANE
) {
1253 plane
= obj_to_plane(obj
);
1254 plane_mask
|= (1 << drm_plane_index(plane
));
1255 plane
->old_fb
= plane
->fb
;
1258 if (get_user(count_props
, count_props_ptr
+ copied_objs
)) {
1265 for (j
= 0; j
< count_props
; j
++) {
1267 uint64_t prop_value
;
1268 struct drm_property
*prop
;
1270 if (get_user(prop_id
, props_ptr
+ copied_props
)) {
1275 prop
= drm_property_find(dev
, prop_id
);
1281 if (copy_from_user(&prop_value
,
1282 prop_values_ptr
+ copied_props
,
1283 sizeof(prop_value
))) {
1288 ret
= atomic_set_prop(state
, obj
, prop
, prop_value
);
1296 if (arg
->flags
& DRM_MODE_PAGE_FLIP_EVENT
) {
1297 int ncrtcs
= dev
->mode_config
.num_crtc
;
1299 for (i
= 0; i
< ncrtcs
; i
++) {
1300 struct drm_crtc_state
*crtc_state
= state
->crtc_states
[i
];
1301 struct drm_pending_vblank_event
*e
;
1306 e
= create_vblank_event(dev
, file_priv
, arg
->user_data
);
1312 crtc_state
->event
= e
;
1316 if (arg
->flags
& DRM_MODE_ATOMIC_TEST_ONLY
) {
1317 ret
= drm_atomic_check_only(state
);
1318 /* _check_only() does not free state, unlike _commit() */
1319 drm_atomic_state_free(state
);
1320 } else if (arg
->flags
& DRM_MODE_ATOMIC_NONBLOCK
) {
1321 ret
= drm_atomic_async_commit(state
);
1323 ret
= drm_atomic_commit(state
);
1326 /* if succeeded, fixup legacy plane crtc/fb ptrs before dropping
1327 * locks (ie. while it is still safe to deref plane->state). We
1328 * need to do this here because the driver entry points cannot
1329 * distinguish between legacy and atomic ioctls.
1331 drm_for_each_plane_mask(plane
, dev
, plane_mask
) {
1333 struct drm_framebuffer
*new_fb
= plane
->state
->fb
;
1335 drm_framebuffer_reference(new_fb
);
1337 plane
->crtc
= plane
->state
->crtc
;
1339 plane
->old_fb
= NULL
;
1341 if (plane
->old_fb
) {
1342 drm_framebuffer_unreference(plane
->old_fb
);
1343 plane
->old_fb
= NULL
;
1347 drm_modeset_drop_locks(&ctx
);
1348 drm_modeset_acquire_fini(&ctx
);
1353 if (ret
== -EDEADLK
)
1356 if (arg
->flags
& DRM_MODE_PAGE_FLIP_EVENT
) {
1357 int ncrtcs
= dev
->mode_config
.num_crtc
;
1359 for (i
= 0; i
< ncrtcs
; i
++) {
1360 struct drm_crtc_state
*crtc_state
= state
->crtc_states
[i
];
1365 destroy_vblank_event(dev
, file_priv
, crtc_state
->event
);
1366 crtc_state
->event
= NULL
;
1370 drm_atomic_state_free(state
);
1372 drm_modeset_drop_locks(&ctx
);
1373 drm_modeset_acquire_fini(&ctx
);
1378 drm_atomic_state_clear(state
);
1379 drm_modeset_backoff(&ctx
);