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 state
->num_connector
= ACCESS_ONCE(dev
->mode_config
.num_connector
);
61 state
->crtcs
= kcalloc(dev
->mode_config
.num_crtc
,
62 sizeof(*state
->crtcs
), GFP_KERNEL
);
65 state
->crtc_states
= kcalloc(dev
->mode_config
.num_crtc
,
66 sizeof(*state
->crtc_states
), GFP_KERNEL
);
67 if (!state
->crtc_states
)
69 state
->planes
= kcalloc(dev
->mode_config
.num_total_plane
,
70 sizeof(*state
->planes
), GFP_KERNEL
);
73 state
->plane_states
= kcalloc(dev
->mode_config
.num_total_plane
,
74 sizeof(*state
->plane_states
), GFP_KERNEL
);
75 if (!state
->plane_states
)
77 state
->connectors
= kcalloc(state
->num_connector
,
78 sizeof(*state
->connectors
),
80 if (!state
->connectors
)
82 state
->connector_states
= kcalloc(state
->num_connector
,
83 sizeof(*state
->connector_states
),
85 if (!state
->connector_states
)
90 DRM_DEBUG_KMS("Allocate atomic state %p\n", state
);
98 EXPORT_SYMBOL(drm_atomic_state_alloc
);
101 * drm_atomic_state_clear - clear state object
102 * @state: atomic state
104 * When the w/w mutex algorithm detects a deadlock we need to back off and drop
105 * all locks. So someone else could sneak in and change the current modeset
106 * configuration. Which means that all the state assembled in @state is no
107 * longer an atomic update to the current state, but to some arbitrary earlier
108 * state. Which could break assumptions the driver's ->atomic_check likely
111 * Hence we must clear all cached state and completely start over, using this
114 void drm_atomic_state_clear(struct drm_atomic_state
*state
)
116 struct drm_device
*dev
= state
->dev
;
117 struct drm_mode_config
*config
= &dev
->mode_config
;
120 DRM_DEBUG_KMS("Clearing atomic state %p\n", state
);
122 for (i
= 0; i
< state
->num_connector
; i
++) {
123 struct drm_connector
*connector
= state
->connectors
[i
];
128 WARN_ON(!drm_modeset_is_locked(&config
->connection_mutex
));
130 connector
->funcs
->atomic_destroy_state(connector
,
131 state
->connector_states
[i
]);
134 for (i
= 0; i
< config
->num_crtc
; i
++) {
135 struct drm_crtc
*crtc
= state
->crtcs
[i
];
140 crtc
->funcs
->atomic_destroy_state(crtc
,
141 state
->crtc_states
[i
]);
144 for (i
= 0; i
< config
->num_total_plane
; i
++) {
145 struct drm_plane
*plane
= state
->planes
[i
];
150 plane
->funcs
->atomic_destroy_state(plane
,
151 state
->plane_states
[i
]);
154 EXPORT_SYMBOL(drm_atomic_state_clear
);
157 * drm_atomic_state_free - free all memory for an atomic state
158 * @state: atomic state to deallocate
160 * This frees all memory associated with an atomic state, including all the
161 * per-object state for planes, crtcs and connectors.
163 void drm_atomic_state_free(struct drm_atomic_state
*state
)
165 drm_atomic_state_clear(state
);
167 DRM_DEBUG_KMS("Freeing atomic state %p\n", state
);
171 EXPORT_SYMBOL(drm_atomic_state_free
);
174 * drm_atomic_get_crtc_state - get crtc state
175 * @state: global atomic state object
176 * @crtc: crtc to get state object for
178 * This function returns the crtc state for the given crtc, allocating it if
179 * needed. It will also grab the relevant crtc lock to make sure that the state
184 * Either the allocated state or the error code encoded into the pointer. When
185 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
186 * entire atomic sequence must be restarted. All other errors are fatal.
188 struct drm_crtc_state
*
189 drm_atomic_get_crtc_state(struct drm_atomic_state
*state
,
190 struct drm_crtc
*crtc
)
193 struct drm_crtc_state
*crtc_state
;
195 index
= drm_crtc_index(crtc
);
197 if (state
->crtc_states
[index
])
198 return state
->crtc_states
[index
];
200 ret
= drm_modeset_lock(&crtc
->mutex
, state
->acquire_ctx
);
204 crtc_state
= crtc
->funcs
->atomic_duplicate_state(crtc
);
206 return ERR_PTR(-ENOMEM
);
208 state
->crtc_states
[index
] = crtc_state
;
209 state
->crtcs
[index
] = crtc
;
210 crtc_state
->state
= state
;
212 DRM_DEBUG_KMS("Added [CRTC:%d] %p state to %p\n",
213 crtc
->base
.id
, crtc_state
, state
);
217 EXPORT_SYMBOL(drm_atomic_get_crtc_state
);
220 * drm_atomic_crtc_set_property - set property on CRTC
221 * @crtc: the drm CRTC to set a property on
222 * @state: the state object to update with the new property value
223 * @property: the property to set
224 * @val: the new property value
226 * Use this instead of calling crtc->atomic_set_property directly.
227 * This function handles generic/core properties and calls out to
228 * driver's ->atomic_set_property() for driver properties. To ensure
229 * consistent behavior you must call this function rather than the
230 * driver hook directly.
233 * Zero on success, error code on failure
235 int drm_atomic_crtc_set_property(struct drm_crtc
*crtc
,
236 struct drm_crtc_state
*state
, struct drm_property
*property
,
239 if (crtc
->funcs
->atomic_set_property
)
240 return crtc
->funcs
->atomic_set_property(crtc
, state
, property
, val
);
243 EXPORT_SYMBOL(drm_atomic_crtc_set_property
);
246 * drm_atomic_crtc_get_property - get property on CRTC
247 * @crtc: the drm CRTC to get a property on
248 * @state: the state object with the property value to read
249 * @property: the property to get
250 * @val: the property value (returned by reference)
252 * Use this instead of calling crtc->atomic_get_property directly.
253 * This function handles generic/core properties and calls out to
254 * driver's ->atomic_get_property() for driver properties. To ensure
255 * consistent behavior you must call this function rather than the
256 * driver hook directly.
259 * Zero on success, error code on failure
261 int drm_atomic_crtc_get_property(struct drm_crtc
*crtc
,
262 const struct drm_crtc_state
*state
,
263 struct drm_property
*property
, uint64_t *val
)
265 if (crtc
->funcs
->atomic_get_property
)
266 return crtc
->funcs
->atomic_get_property(crtc
, state
, property
, val
);
269 EXPORT_SYMBOL(drm_atomic_crtc_get_property
);
272 * drm_atomic_crtc_check - check crtc state
273 * @crtc: crtc to check
274 * @state: crtc state to check
276 * Provides core sanity checks for crtc state.
279 * Zero on success, error code on failure
281 static int drm_atomic_crtc_check(struct drm_crtc
*crtc
,
282 struct drm_crtc_state
*state
)
284 /* NOTE: we explicitly don't enforce constraints such as primary
285 * layer covering entire screen, since that is something we want
286 * to allow (on hw that supports it). For hw that does not, it
287 * should be checked in driver's crtc->atomic_check() vfunc.
289 * TODO: Add generic modeset state checks once we support those.
295 * drm_atomic_get_plane_state - get plane state
296 * @state: global atomic state object
297 * @plane: plane to get state object for
299 * This function returns the plane state for the given plane, allocating it if
300 * needed. It will also grab the relevant plane lock to make sure that the state
305 * Either the allocated state or the error code encoded into the pointer. When
306 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
307 * entire atomic sequence must be restarted. All other errors are fatal.
309 struct drm_plane_state
*
310 drm_atomic_get_plane_state(struct drm_atomic_state
*state
,
311 struct drm_plane
*plane
)
314 struct drm_plane_state
*plane_state
;
316 index
= drm_plane_index(plane
);
318 if (state
->plane_states
[index
])
319 return state
->plane_states
[index
];
321 ret
= drm_modeset_lock(&plane
->mutex
, state
->acquire_ctx
);
325 plane_state
= plane
->funcs
->atomic_duplicate_state(plane
);
327 return ERR_PTR(-ENOMEM
);
329 state
->plane_states
[index
] = plane_state
;
330 state
->planes
[index
] = plane
;
331 plane_state
->state
= state
;
333 DRM_DEBUG_KMS("Added [PLANE:%d] %p state to %p\n",
334 plane
->base
.id
, plane_state
, state
);
336 if (plane_state
->crtc
) {
337 struct drm_crtc_state
*crtc_state
;
339 crtc_state
= drm_atomic_get_crtc_state(state
,
341 if (IS_ERR(crtc_state
))
342 return ERR_CAST(crtc_state
);
347 EXPORT_SYMBOL(drm_atomic_get_plane_state
);
350 * drm_atomic_plane_set_property - set property on plane
351 * @plane: the drm plane to set a property on
352 * @state: the state object to update with the new property value
353 * @property: the property to set
354 * @val: the new property value
356 * Use this instead of calling plane->atomic_set_property directly.
357 * This function handles generic/core properties and calls out to
358 * driver's ->atomic_set_property() for driver properties. To ensure
359 * consistent behavior you must call this function rather than the
360 * driver hook directly.
363 * Zero on success, error code on failure
365 int drm_atomic_plane_set_property(struct drm_plane
*plane
,
366 struct drm_plane_state
*state
, struct drm_property
*property
,
369 if (plane
->funcs
->atomic_set_property
)
370 return plane
->funcs
->atomic_set_property(plane
, state
, property
, val
);
373 EXPORT_SYMBOL(drm_atomic_plane_set_property
);
376 * drm_atomic_plane_get_property - get property on plane
377 * @plane: the drm plane to get a property on
378 * @state: the state object with the property value to read
379 * @property: the property to get
380 * @val: the property value (returned by reference)
382 * Use this instead of calling plane->atomic_get_property directly.
383 * This function handles generic/core properties and calls out to
384 * driver's ->atomic_get_property() for driver properties. To ensure
385 * consistent behavior you must call this function rather than the
386 * driver hook directly.
389 * Zero on success, error code on failure
391 int drm_atomic_plane_get_property(struct drm_plane
*plane
,
392 const struct drm_plane_state
*state
,
393 struct drm_property
*property
, uint64_t *val
)
395 if (plane
->funcs
->atomic_get_property
)
396 return plane
->funcs
->atomic_get_property(plane
, state
, property
, val
);
399 EXPORT_SYMBOL(drm_atomic_plane_get_property
);
402 * drm_atomic_plane_check - check plane state
403 * @plane: plane to check
404 * @state: plane state to check
406 * Provides core sanity checks for plane state.
409 * Zero on success, error code on failure
411 static int drm_atomic_plane_check(struct drm_plane
*plane
,
412 struct drm_plane_state
*state
)
414 unsigned int fb_width
, fb_height
;
417 /* either *both* CRTC and FB must be set, or neither */
418 if (WARN_ON(state
->crtc
&& !state
->fb
)) {
419 DRM_DEBUG_KMS("CRTC set but no FB\n");
421 } else if (WARN_ON(state
->fb
&& !state
->crtc
)) {
422 DRM_DEBUG_KMS("FB set but no CRTC\n");
426 /* if disabled, we don't care about the rest of the state: */
430 /* Check whether this plane is usable on this CRTC */
431 if (!(plane
->possible_crtcs
& drm_crtc_mask(state
->crtc
))) {
432 DRM_DEBUG_KMS("Invalid crtc for plane\n");
436 /* Check whether this plane supports the fb pixel format. */
437 for (i
= 0; i
< plane
->format_count
; i
++)
438 if (state
->fb
->pixel_format
== plane
->format_types
[i
])
440 if (i
== plane
->format_count
) {
441 DRM_DEBUG_KMS("Invalid pixel format %s\n",
442 drm_get_format_name(state
->fb
->pixel_format
));
446 /* Give drivers some help against integer overflows */
447 if (state
->crtc_w
> INT_MAX
||
448 state
->crtc_x
> INT_MAX
- (int32_t) state
->crtc_w
||
449 state
->crtc_h
> INT_MAX
||
450 state
->crtc_y
> INT_MAX
- (int32_t) state
->crtc_h
) {
451 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
452 state
->crtc_w
, state
->crtc_h
,
453 state
->crtc_x
, state
->crtc_y
);
457 fb_width
= state
->fb
->width
<< 16;
458 fb_height
= state
->fb
->height
<< 16;
460 /* Make sure source coordinates are inside the fb. */
461 if (state
->src_w
> fb_width
||
462 state
->src_x
> fb_width
- state
->src_w
||
463 state
->src_h
> fb_height
||
464 state
->src_y
> fb_height
- state
->src_h
) {
465 DRM_DEBUG_KMS("Invalid source coordinates "
466 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
467 state
->src_w
>> 16, ((state
->src_w
& 0xffff) * 15625) >> 10,
468 state
->src_h
>> 16, ((state
->src_h
& 0xffff) * 15625) >> 10,
469 state
->src_x
>> 16, ((state
->src_x
& 0xffff) * 15625) >> 10,
470 state
->src_y
>> 16, ((state
->src_y
& 0xffff) * 15625) >> 10);
478 * drm_atomic_get_connector_state - get connector state
479 * @state: global atomic state object
480 * @connector: connector to get state object for
482 * This function returns the connector state for the given connector,
483 * allocating it if needed. It will also grab the relevant connector lock to
484 * make sure that the state is consistent.
488 * Either the allocated state or the error code encoded into the pointer. When
489 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
490 * entire atomic sequence must be restarted. All other errors are fatal.
492 struct drm_connector_state
*
493 drm_atomic_get_connector_state(struct drm_atomic_state
*state
,
494 struct drm_connector
*connector
)
497 struct drm_mode_config
*config
= &connector
->dev
->mode_config
;
498 struct drm_connector_state
*connector_state
;
500 ret
= drm_modeset_lock(&config
->connection_mutex
, state
->acquire_ctx
);
504 index
= drm_connector_index(connector
);
507 * Construction of atomic state updates can race with a connector
508 * hot-add which might overflow. In this case flip the table and just
509 * restart the entire ioctl - no one is fast enough to livelock a cpu
510 * with physical hotplug events anyway.
512 * Note that we only grab the indexes once we have the right lock to
513 * prevent hotplug/unplugging of connectors. So removal is no problem,
514 * at most the array is a bit too large.
516 if (index
>= state
->num_connector
) {
517 DRM_DEBUG_KMS("Hot-added connector would overflow state array, restarting\n");
518 return ERR_PTR(-EAGAIN
);
521 if (state
->connector_states
[index
])
522 return state
->connector_states
[index
];
524 connector_state
= connector
->funcs
->atomic_duplicate_state(connector
);
525 if (!connector_state
)
526 return ERR_PTR(-ENOMEM
);
528 state
->connector_states
[index
] = connector_state
;
529 state
->connectors
[index
] = connector
;
530 connector_state
->state
= state
;
532 DRM_DEBUG_KMS("Added [CONNECTOR:%d] %p state to %p\n",
533 connector
->base
.id
, connector_state
, state
);
535 if (connector_state
->crtc
) {
536 struct drm_crtc_state
*crtc_state
;
538 crtc_state
= drm_atomic_get_crtc_state(state
,
539 connector_state
->crtc
);
540 if (IS_ERR(crtc_state
))
541 return ERR_CAST(crtc_state
);
544 return connector_state
;
546 EXPORT_SYMBOL(drm_atomic_get_connector_state
);
549 * drm_atomic_connector_set_property - set property on connector.
550 * @connector: the drm connector to set a property on
551 * @state: the state object to update with the new property value
552 * @property: the property to set
553 * @val: the new property value
555 * Use this instead of calling connector->atomic_set_property directly.
556 * This function handles generic/core properties and calls out to
557 * driver's ->atomic_set_property() for driver properties. To ensure
558 * consistent behavior you must call this function rather than the
559 * driver hook directly.
562 * Zero on success, error code on failure
564 int drm_atomic_connector_set_property(struct drm_connector
*connector
,
565 struct drm_connector_state
*state
, struct drm_property
*property
,
568 struct drm_device
*dev
= connector
->dev
;
569 struct drm_mode_config
*config
= &dev
->mode_config
;
571 if (property
== config
->dpms_property
) {
572 /* setting DPMS property requires special handling, which
573 * is done in legacy setprop path for us. Disallow (for
574 * now?) atomic writes to DPMS property:
577 } else if (connector
->funcs
->atomic_set_property
) {
578 return connector
->funcs
->atomic_set_property(connector
,
579 state
, property
, val
);
584 EXPORT_SYMBOL(drm_atomic_connector_set_property
);
587 * drm_atomic_connector_get_property - get property on connector
588 * @connector: the drm connector to get a property on
589 * @state: the state object with the property value to read
590 * @property: the property to get
591 * @val: the property value (returned by reference)
593 * Use this instead of calling connector->atomic_get_property directly.
594 * This function handles generic/core properties and calls out to
595 * driver's ->atomic_get_property() for driver properties. To ensure
596 * consistent behavior you must call this function rather than the
597 * driver hook directly.
600 * Zero on success, error code on failure
602 int drm_atomic_connector_get_property(struct drm_connector
*connector
,
603 const struct drm_connector_state
*state
,
604 struct drm_property
*property
, uint64_t *val
)
606 struct drm_device
*dev
= connector
->dev
;
607 struct drm_mode_config
*config
= &dev
->mode_config
;
609 if (property
== config
->dpms_property
) {
610 *val
= connector
->dpms
;
611 } else if (connector
->funcs
->atomic_get_property
) {
612 return connector
->funcs
->atomic_get_property(connector
,
613 state
, property
, val
);
620 EXPORT_SYMBOL(drm_atomic_connector_get_property
);
623 * drm_atomic_get_property - helper to read atomic property
624 * @obj: drm mode object whose property to read
625 * @property: the property to read
626 * @val: the read value, returned by reference
629 * Zero on success, error code on failure
631 int drm_atomic_get_property(struct drm_mode_object
*obj
,
632 struct drm_property
*property
, uint64_t *val
)
634 struct drm_device
*dev
= property
->dev
;
638 case DRM_MODE_OBJECT_CONNECTOR
: {
639 struct drm_connector
*connector
= obj_to_connector(obj
);
640 WARN_ON(!drm_modeset_is_locked(&dev
->mode_config
.connection_mutex
));
641 ret
= drm_atomic_connector_get_property(connector
,
642 connector
->state
, property
, val
);
645 case DRM_MODE_OBJECT_CRTC
: {
646 struct drm_crtc
*crtc
= obj_to_crtc(obj
);
647 WARN_ON(!drm_modeset_is_locked(&crtc
->mutex
));
648 ret
= drm_atomic_crtc_get_property(crtc
,
649 crtc
->state
, property
, val
);
652 case DRM_MODE_OBJECT_PLANE
: {
653 struct drm_plane
*plane
= obj_to_plane(obj
);
654 WARN_ON(!drm_modeset_is_locked(&plane
->mutex
));
655 ret
= drm_atomic_plane_get_property(plane
,
656 plane
->state
, property
, val
);
668 * drm_atomic_set_crtc_for_plane - set crtc for plane
669 * @plane_state: the plane whose incoming state to update
670 * @crtc: crtc to use for the plane
672 * Changing the assigned crtc for a plane requires us to grab the lock and state
673 * for the new crtc, as needed. This function takes care of all these details
674 * besides updating the pointer in the state object itself.
677 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
678 * then the w/w mutex code has detected a deadlock and the entire atomic
679 * sequence must be restarted. All other errors are fatal.
682 drm_atomic_set_crtc_for_plane(struct drm_plane_state
*plane_state
,
683 struct drm_crtc
*crtc
)
685 struct drm_plane
*plane
= plane_state
->plane
;
686 struct drm_crtc_state
*crtc_state
;
688 if (plane_state
->crtc
) {
689 crtc_state
= drm_atomic_get_crtc_state(plane_state
->state
,
691 if (WARN_ON(IS_ERR(crtc_state
)))
692 return PTR_ERR(crtc_state
);
694 crtc_state
->plane_mask
&= ~(1 << drm_plane_index(plane
));
697 plane_state
->crtc
= crtc
;
700 crtc_state
= drm_atomic_get_crtc_state(plane_state
->state
,
702 if (IS_ERR(crtc_state
))
703 return PTR_ERR(crtc_state
);
704 crtc_state
->plane_mask
|= (1 << drm_plane_index(plane
));
708 DRM_DEBUG_KMS("Link plane state %p to [CRTC:%d]\n",
709 plane_state
, crtc
->base
.id
);
711 DRM_DEBUG_KMS("Link plane state %p to [NOCRTC]\n", plane_state
);
715 EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane
);
718 * drm_atomic_set_fb_for_plane - set crtc for plane
719 * @plane_state: atomic state object for the plane
720 * @fb: fb to use for the plane
722 * Changing the assigned framebuffer for a plane requires us to grab a reference
723 * to the new fb and drop the reference to the old fb, if there is one. This
724 * function takes care of all these details besides updating the pointer in the
725 * state object itself.
728 drm_atomic_set_fb_for_plane(struct drm_plane_state
*plane_state
,
729 struct drm_framebuffer
*fb
)
732 drm_framebuffer_unreference(plane_state
->fb
);
734 drm_framebuffer_reference(fb
);
735 plane_state
->fb
= fb
;
738 DRM_DEBUG_KMS("Set [FB:%d] for plane state %p\n",
739 fb
->base
.id
, plane_state
);
741 DRM_DEBUG_KMS("Set [NOFB] for plane state %p\n", plane_state
);
743 EXPORT_SYMBOL(drm_atomic_set_fb_for_plane
);
746 * drm_atomic_set_crtc_for_connector - set crtc for connector
747 * @conn_state: atomic state object for the connector
748 * @crtc: crtc to use for the connector
750 * Changing the assigned crtc for a connector requires us to grab the lock and
751 * state for the new crtc, as needed. This function takes care of all these
752 * details besides updating the pointer in the state object itself.
755 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
756 * then the w/w mutex code has detected a deadlock and the entire atomic
757 * sequence must be restarted. All other errors are fatal.
760 drm_atomic_set_crtc_for_connector(struct drm_connector_state
*conn_state
,
761 struct drm_crtc
*crtc
)
763 struct drm_crtc_state
*crtc_state
;
766 crtc_state
= drm_atomic_get_crtc_state(conn_state
->state
, crtc
);
767 if (IS_ERR(crtc_state
))
768 return PTR_ERR(crtc_state
);
771 conn_state
->crtc
= crtc
;
774 DRM_DEBUG_KMS("Link connector state %p to [CRTC:%d]\n",
775 conn_state
, crtc
->base
.id
);
777 DRM_DEBUG_KMS("Link connector state %p to [NOCRTC]\n",
782 EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector
);
785 * drm_atomic_add_affected_connectors - add connectors for crtc
786 * @state: atomic state
789 * This function walks the current configuration and adds all connectors
790 * currently using @crtc to the atomic configuration @state. Note that this
791 * function must acquire the connection mutex. This can potentially cause
792 * unneeded seralization if the update is just for the planes on one crtc. Hence
793 * drivers and helpers should only call this when really needed (e.g. when a
794 * full modeset needs to happen due to some change).
797 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
798 * then the w/w mutex code has detected a deadlock and the entire atomic
799 * sequence must be restarted. All other errors are fatal.
802 drm_atomic_add_affected_connectors(struct drm_atomic_state
*state
,
803 struct drm_crtc
*crtc
)
805 struct drm_mode_config
*config
= &state
->dev
->mode_config
;
806 struct drm_connector
*connector
;
807 struct drm_connector_state
*conn_state
;
810 ret
= drm_modeset_lock(&config
->connection_mutex
, state
->acquire_ctx
);
814 DRM_DEBUG_KMS("Adding all current connectors for [CRTC:%d] to %p\n",
815 crtc
->base
.id
, state
);
818 * Changed connectors are already in @state, so only need to look at the
819 * current configuration.
821 list_for_each_entry(connector
, &config
->connector_list
, head
) {
822 if (connector
->state
->crtc
!= crtc
)
825 conn_state
= drm_atomic_get_connector_state(state
, connector
);
826 if (IS_ERR(conn_state
))
827 return PTR_ERR(conn_state
);
832 EXPORT_SYMBOL(drm_atomic_add_affected_connectors
);
835 * drm_atomic_connectors_for_crtc - count number of connected outputs
836 * @state: atomic state
839 * This function counts all connectors which will be connected to @crtc
840 * according to @state. Useful to recompute the enable state for @crtc.
843 drm_atomic_connectors_for_crtc(struct drm_atomic_state
*state
,
844 struct drm_crtc
*crtc
)
846 int i
, num_connected_connectors
= 0;
848 for (i
= 0; i
< state
->num_connector
; i
++) {
849 struct drm_connector_state
*conn_state
;
851 conn_state
= state
->connector_states
[i
];
853 if (conn_state
&& conn_state
->crtc
== crtc
)
854 num_connected_connectors
++;
857 DRM_DEBUG_KMS("State %p has %i connectors for [CRTC:%d]\n",
858 state
, num_connected_connectors
, crtc
->base
.id
);
860 return num_connected_connectors
;
862 EXPORT_SYMBOL(drm_atomic_connectors_for_crtc
);
865 * drm_atomic_legacy_backoff - locking backoff for legacy ioctls
866 * @state: atomic state
868 * This function should be used by legacy entry points which don't understand
869 * -EDEADLK semantics. For simplicity this one will grab all modeset locks after
870 * the slowpath completed.
872 void drm_atomic_legacy_backoff(struct drm_atomic_state
*state
)
877 drm_modeset_backoff(state
->acquire_ctx
);
879 ret
= drm_modeset_lock(&state
->dev
->mode_config
.connection_mutex
,
883 ret
= drm_modeset_lock_all_crtcs(state
->dev
,
888 EXPORT_SYMBOL(drm_atomic_legacy_backoff
);
891 * drm_atomic_check_only - check whether a given config would work
892 * @state: atomic configuration to check
894 * Note that this function can return -EDEADLK if the driver needed to acquire
895 * more locks but encountered a deadlock. The caller must then do the usual w/w
896 * backoff dance and restart. All other errors are fatal.
899 * 0 on success, negative error code on failure.
901 int drm_atomic_check_only(struct drm_atomic_state
*state
)
903 struct drm_device
*dev
= state
->dev
;
904 struct drm_mode_config
*config
= &dev
->mode_config
;
905 int nplanes
= config
->num_total_plane
;
906 int ncrtcs
= config
->num_crtc
;
909 DRM_DEBUG_KMS("checking %p\n", state
);
911 for (i
= 0; i
< nplanes
; i
++) {
912 struct drm_plane
*plane
= state
->planes
[i
];
917 ret
= drm_atomic_plane_check(plane
, state
->plane_states
[i
]);
919 DRM_DEBUG_KMS("[PLANE:%d] atomic core check failed\n",
925 for (i
= 0; i
< ncrtcs
; i
++) {
926 struct drm_crtc
*crtc
= state
->crtcs
[i
];
931 ret
= drm_atomic_crtc_check(crtc
, state
->crtc_states
[i
]);
933 DRM_DEBUG_KMS("[CRTC:%d] atomic core check failed\n",
939 if (config
->funcs
->atomic_check
)
940 ret
= config
->funcs
->atomic_check(state
->dev
, state
);
944 EXPORT_SYMBOL(drm_atomic_check_only
);
947 * drm_atomic_commit - commit configuration atomically
948 * @state: atomic configuration to check
950 * Note that this function can return -EDEADLK if the driver needed to acquire
951 * more locks but encountered a deadlock. The caller must then do the usual w/w
952 * backoff dance and restart. All other errors are fatal.
954 * Also note that on successful execution ownership of @state is transferred
955 * from the caller of this function to the function itself. The caller must not
956 * free or in any other way access @state. If the function fails then the caller
957 * must clean up @state itself.
960 * 0 on success, negative error code on failure.
962 int drm_atomic_commit(struct drm_atomic_state
*state
)
964 struct drm_mode_config
*config
= &state
->dev
->mode_config
;
967 ret
= drm_atomic_check_only(state
);
971 DRM_DEBUG_KMS("commiting %p\n", state
);
973 return config
->funcs
->atomic_commit(state
->dev
, state
, false);
975 EXPORT_SYMBOL(drm_atomic_commit
);
978 * drm_atomic_async_commit - atomic&async configuration commit
979 * @state: atomic configuration to check
981 * Note that this function can return -EDEADLK if the driver needed to acquire
982 * more locks but encountered a deadlock. The caller must then do the usual w/w
983 * backoff dance and restart. All other errors are fatal.
985 * Also note that on successful execution ownership of @state is transferred
986 * from the caller of this function to the function itself. The caller must not
987 * free or in any other way access @state. If the function fails then the caller
988 * must clean up @state itself.
991 * 0 on success, negative error code on failure.
993 int drm_atomic_async_commit(struct drm_atomic_state
*state
)
995 struct drm_mode_config
*config
= &state
->dev
->mode_config
;
998 ret
= drm_atomic_check_only(state
);
1002 DRM_DEBUG_KMS("commiting %p asynchronously\n", state
);
1004 return config
->funcs
->atomic_commit(state
->dev
, state
, true);
1006 EXPORT_SYMBOL(drm_atomic_async_commit
);