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>
29 #include <drm/drm_atomic.h>
30 #include <drm/drm_plane_helper.h>
31 #include <drm/drm_crtc_helper.h>
32 #include <drm/drm_atomic_helper.h>
33 #include <linux/fence.h>
38 * This helper library provides implementations of check and commit functions on
39 * top of the CRTC modeset helper callbacks and the plane helper callbacks. It
40 * also provides convenience implementations for the atomic state handling
41 * callbacks for drivers which don't need to subclass the drm core structures to
42 * add their own additional internal state.
44 * This library also provides default implementations for the check callback in
45 * drm_atomic_helper_check and for the commit callback with
46 * drm_atomic_helper_commit. But the individual stages and callbacks are expose
47 * to allow drivers to mix and match and e.g. use the plane helpers only
48 * together with a driver private modeset implementation.
50 * This library also provides implementations for all the legacy driver
51 * interfaces on top of the atomic interface. See drm_atomic_helper_set_config,
52 * drm_atomic_helper_disable_plane, drm_atomic_helper_disable_plane and the
53 * various functions to implement set_property callbacks. New drivers must not
54 * implement these functions themselves but must use the provided helpers.
57 drm_atomic_helper_plane_changed(struct drm_atomic_state
*state
,
58 struct drm_plane_state
*plane_state
,
59 struct drm_plane
*plane
)
61 struct drm_crtc_state
*crtc_state
;
63 if (plane
->state
->crtc
) {
64 crtc_state
= state
->crtc_states
[drm_crtc_index(plane
->state
->crtc
)];
66 if (WARN_ON(!crtc_state
))
69 crtc_state
->planes_changed
= true;
72 if (plane_state
->crtc
) {
74 state
->crtc_states
[drm_crtc_index(plane_state
->crtc
)];
76 if (WARN_ON(!crtc_state
))
79 crtc_state
->planes_changed
= true;
83 static struct drm_crtc
*
84 get_current_crtc_for_encoder(struct drm_device
*dev
,
85 struct drm_encoder
*encoder
)
87 struct drm_mode_config
*config
= &dev
->mode_config
;
88 struct drm_connector
*connector
;
90 WARN_ON(!drm_modeset_is_locked(&config
->connection_mutex
));
92 list_for_each_entry(connector
, &config
->connector_list
, head
) {
93 if (connector
->state
->best_encoder
!= encoder
)
96 return connector
->state
->crtc
;
103 steal_encoder(struct drm_atomic_state
*state
,
104 struct drm_encoder
*encoder
,
105 struct drm_crtc
*encoder_crtc
)
107 struct drm_mode_config
*config
= &state
->dev
->mode_config
;
108 struct drm_crtc_state
*crtc_state
;
109 struct drm_connector
*connector
;
110 struct drm_connector_state
*connector_state
;
114 * We can only steal an encoder coming from a connector, which means we
115 * must already hold the connection_mutex.
117 WARN_ON(!drm_modeset_is_locked(&config
->connection_mutex
));
119 DRM_DEBUG_KMS("[ENCODER:%d:%s] in use on [CRTC:%d], stealing it\n",
120 encoder
->base
.id
, encoder
->name
,
121 encoder_crtc
->base
.id
);
123 crtc_state
= drm_atomic_get_crtc_state(state
, encoder_crtc
);
124 if (IS_ERR(crtc_state
))
125 return PTR_ERR(crtc_state
);
127 crtc_state
->mode_changed
= true;
129 list_for_each_entry(connector
, &config
->connector_list
, head
) {
130 if (connector
->state
->best_encoder
!= encoder
)
133 DRM_DEBUG_KMS("Stealing encoder from [CONNECTOR:%d:%s]\n",
137 connector_state
= drm_atomic_get_connector_state(state
,
139 if (IS_ERR(connector_state
))
140 return PTR_ERR(connector_state
);
142 ret
= drm_atomic_set_crtc_for_connector(connector_state
, NULL
);
145 connector_state
->best_encoder
= NULL
;
152 update_connector_routing(struct drm_atomic_state
*state
, int conn_idx
)
154 struct drm_connector_helper_funcs
*funcs
;
155 struct drm_encoder
*new_encoder
;
156 struct drm_crtc
*encoder_crtc
;
157 struct drm_connector
*connector
;
158 struct drm_connector_state
*connector_state
;
159 struct drm_crtc_state
*crtc_state
;
162 connector
= state
->connectors
[conn_idx
];
163 connector_state
= state
->connector_states
[conn_idx
];
168 DRM_DEBUG_KMS("Updating routing for [CONNECTOR:%d:%s]\n",
172 if (connector
->state
->crtc
!= connector_state
->crtc
) {
173 if (connector
->state
->crtc
) {
174 idx
= drm_crtc_index(connector
->state
->crtc
);
176 crtc_state
= state
->crtc_states
[idx
];
177 crtc_state
->mode_changed
= true;
180 if (connector_state
->crtc
) {
181 idx
= drm_crtc_index(connector_state
->crtc
);
183 crtc_state
= state
->crtc_states
[idx
];
184 crtc_state
->mode_changed
= true;
188 if (!connector_state
->crtc
) {
189 DRM_DEBUG_KMS("Disabling [CONNECTOR:%d:%s]\n",
193 connector_state
->best_encoder
= NULL
;
198 funcs
= connector
->helper_private
;
199 new_encoder
= funcs
->best_encoder(connector
);
202 DRM_DEBUG_KMS("No suitable encoder found for [CONNECTOR:%d:%s]\n",
208 if (new_encoder
== connector_state
->best_encoder
) {
209 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] keeps [ENCODER:%d:%s], now on [CRTC:%d]\n",
212 new_encoder
->base
.id
,
214 connector_state
->crtc
->base
.id
);
219 encoder_crtc
= get_current_crtc_for_encoder(state
->dev
,
223 ret
= steal_encoder(state
, new_encoder
, encoder_crtc
);
225 DRM_DEBUG_KMS("Encoder stealing failed for [CONNECTOR:%d:%s]\n",
232 connector_state
->best_encoder
= new_encoder
;
233 idx
= drm_crtc_index(connector_state
->crtc
);
235 crtc_state
= state
->crtc_states
[idx
];
236 crtc_state
->mode_changed
= true;
238 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] using [ENCODER:%d:%s] on [CRTC:%d]\n",
241 new_encoder
->base
.id
,
243 connector_state
->crtc
->base
.id
);
249 mode_fixup(struct drm_atomic_state
*state
)
251 int ncrtcs
= state
->dev
->mode_config
.num_crtc
;
252 struct drm_crtc_state
*crtc_state
;
253 struct drm_connector_state
*conn_state
;
257 for (i
= 0; i
< ncrtcs
; i
++) {
258 crtc_state
= state
->crtc_states
[i
];
260 if (!crtc_state
|| !crtc_state
->mode_changed
)
263 drm_mode_copy(&crtc_state
->adjusted_mode
, &crtc_state
->mode
);
266 for (i
= 0; i
< state
->num_connector
; i
++) {
267 struct drm_encoder_helper_funcs
*funcs
;
268 struct drm_encoder
*encoder
;
270 conn_state
= state
->connector_states
[i
];
275 WARN_ON(!!conn_state
->best_encoder
!= !!conn_state
->crtc
);
277 if (!conn_state
->crtc
|| !conn_state
->best_encoder
)
281 state
->crtc_states
[drm_crtc_index(conn_state
->crtc
)];
284 * Each encoder has at most one connector (since we always steal
285 * it away), so we won't call ->mode_fixup twice.
287 encoder
= conn_state
->best_encoder
;
288 funcs
= encoder
->helper_private
;
290 if (encoder
->bridge
&& encoder
->bridge
->funcs
->mode_fixup
) {
291 ret
= encoder
->bridge
->funcs
->mode_fixup(
292 encoder
->bridge
, &crtc_state
->mode
,
293 &crtc_state
->adjusted_mode
);
295 DRM_DEBUG_KMS("Bridge fixup failed\n");
301 ret
= funcs
->mode_fixup(encoder
, &crtc_state
->mode
,
302 &crtc_state
->adjusted_mode
);
304 DRM_DEBUG_KMS("[ENCODER:%d:%s] fixup failed\n",
305 encoder
->base
.id
, encoder
->name
);
310 for (i
= 0; i
< ncrtcs
; i
++) {
311 struct drm_crtc_helper_funcs
*funcs
;
312 struct drm_crtc
*crtc
;
314 crtc_state
= state
->crtc_states
[i
];
315 crtc
= state
->crtcs
[i
];
317 if (!crtc_state
|| !crtc_state
->mode_changed
)
320 funcs
= crtc
->helper_private
;
321 ret
= funcs
->mode_fixup(crtc
, &crtc_state
->mode
,
322 &crtc_state
->adjusted_mode
);
324 DRM_DEBUG_KMS("[CRTC:%d] fixup failed\n",
334 * drm_atomic_helper_check - validate state object for modeset changes
336 * @state: the driver state object
338 * Check the state object to see if the requested state is physically possible.
339 * This does all the crtc and connector related computations for an atomic
340 * update. It computes and updates crtc_state->mode_changed, adds any additional
341 * connectors needed for full modesets and calls down into ->mode_fixup
342 * functions of the driver backend.
346 * Drivers which update ->mode_changed (e.g. in their ->atomic_check hooks if a
347 * plane update can't be done without a full modeset) _must_ call this function
348 * afterwards after that change. It is permitted to call this function multiple
349 * times for the same update, e.g. when the ->atomic_check functions depend upon
350 * the adjusted dotclock for fifo space allocation and watermark computation.
353 * Zero for success or -errno
356 drm_atomic_helper_check_modeset(struct drm_device
*dev
,
357 struct drm_atomic_state
*state
)
359 int ncrtcs
= dev
->mode_config
.num_crtc
;
360 struct drm_crtc
*crtc
;
361 struct drm_crtc_state
*crtc_state
;
364 for (i
= 0; i
< ncrtcs
; i
++) {
365 crtc
= state
->crtcs
[i
];
366 crtc_state
= state
->crtc_states
[i
];
371 if (!drm_mode_equal(&crtc
->state
->mode
, &crtc_state
->mode
)) {
372 DRM_DEBUG_KMS("[CRTC:%d] mode changed\n",
374 crtc_state
->mode_changed
= true;
377 if (crtc
->state
->enable
!= crtc_state
->enable
) {
378 DRM_DEBUG_KMS("[CRTC:%d] enable changed\n",
380 crtc_state
->mode_changed
= true;
384 for (i
= 0; i
< state
->num_connector
; i
++) {
386 * This only sets crtc->mode_changed for routing changes,
387 * drivers must set crtc->mode_changed themselves when connector
388 * properties need to be updated.
390 ret
= update_connector_routing(state
, i
);
396 * After all the routing has been prepared we need to add in any
397 * connector which is itself unchanged, but who's crtc changes it's
398 * configuration. This must be done before calling mode_fixup in case a
399 * crtc only changed its mode but has the same set of connectors.
401 for (i
= 0; i
< ncrtcs
; i
++) {
404 crtc
= state
->crtcs
[i
];
405 crtc_state
= state
->crtc_states
[i
];
407 if (!crtc
|| !crtc_state
->mode_changed
)
410 DRM_DEBUG_KMS("[CRTC:%d] needs full modeset, enable: %c\n",
412 crtc_state
->enable
? 'y' : 'n');
414 ret
= drm_atomic_add_affected_connectors(state
, crtc
);
418 num_connectors
= drm_atomic_connectors_for_crtc(state
,
421 if (crtc_state
->enable
!= !!num_connectors
) {
422 DRM_DEBUG_KMS("[CRTC:%d] enabled/connectors mismatch\n",
429 return mode_fixup(state
);
431 EXPORT_SYMBOL(drm_atomic_helper_check_modeset
);
434 * drm_atomic_helper_check - validate state object for modeset changes
436 * @state: the driver state object
438 * Check the state object to see if the requested state is physically possible.
439 * This does all the plane update related checks using by calling into the
440 * ->atomic_check hooks provided by the driver.
443 * Zero for success or -errno
446 drm_atomic_helper_check_planes(struct drm_device
*dev
,
447 struct drm_atomic_state
*state
)
449 int nplanes
= dev
->mode_config
.num_total_plane
;
450 int ncrtcs
= dev
->mode_config
.num_crtc
;
453 for (i
= 0; i
< nplanes
; i
++) {
454 struct drm_plane_helper_funcs
*funcs
;
455 struct drm_plane
*plane
= state
->planes
[i
];
456 struct drm_plane_state
*plane_state
= state
->plane_states
[i
];
461 funcs
= plane
->helper_private
;
463 drm_atomic_helper_plane_changed(state
, plane_state
, plane
);
465 if (!funcs
|| !funcs
->atomic_check
)
468 ret
= funcs
->atomic_check(plane
, plane_state
);
470 DRM_DEBUG_KMS("[PLANE:%d] atomic check failed\n",
476 for (i
= 0; i
< ncrtcs
; i
++) {
477 struct drm_crtc_helper_funcs
*funcs
;
478 struct drm_crtc
*crtc
= state
->crtcs
[i
];
483 funcs
= crtc
->helper_private
;
485 if (!funcs
|| !funcs
->atomic_check
)
488 ret
= funcs
->atomic_check(crtc
, state
->crtc_states
[i
]);
490 DRM_DEBUG_KMS("[CRTC:%d] atomic check failed\n",
498 EXPORT_SYMBOL(drm_atomic_helper_check_planes
);
501 * drm_atomic_helper_check - validate state object
503 * @state: the driver state object
505 * Check the state object to see if the requested state is physically possible.
506 * Only crtcs and planes have check callbacks, so for any additional (global)
507 * checking that a driver needs it can simply wrap that around this function.
508 * Drivers without such needs can directly use this as their ->atomic_check()
511 * This just wraps the two parts of the state checking for planes and modeset
512 * state in the default order: First it calls drm_atomic_helper_check_modeset()
513 * and then drm_atomic_helper_check_planes(). The assumption is that the
514 * ->atomic_check functions depend upon an updated adjusted_mode.clock to
515 * e.g. properly compute watermarks.
518 * Zero for success or -errno
520 int drm_atomic_helper_check(struct drm_device
*dev
,
521 struct drm_atomic_state
*state
)
525 ret
= drm_atomic_helper_check_modeset(dev
, state
);
529 ret
= drm_atomic_helper_check_planes(dev
, state
);
535 EXPORT_SYMBOL(drm_atomic_helper_check
);
538 disable_outputs(struct drm_device
*dev
, struct drm_atomic_state
*old_state
)
540 int ncrtcs
= old_state
->dev
->mode_config
.num_crtc
;
543 for (i
= 0; i
< old_state
->num_connector
; i
++) {
544 struct drm_connector_state
*old_conn_state
;
545 struct drm_connector
*connector
;
546 struct drm_encoder_helper_funcs
*funcs
;
547 struct drm_encoder
*encoder
;
549 old_conn_state
= old_state
->connector_states
[i
];
550 connector
= old_state
->connectors
[i
];
552 /* Shut down everything that's in the changeset and currently
553 * still on. So need to check the old, saved state. */
554 if (!old_conn_state
|| !old_conn_state
->crtc
)
557 encoder
= old_conn_state
->best_encoder
;
559 /* We shouldn't get this far if we didn't previously have
560 * an encoder.. but WARN_ON() rather than explode.
562 if (WARN_ON(!encoder
))
565 funcs
= encoder
->helper_private
;
568 * Each encoder has at most one connector (since we always steal
569 * it away), so we won't call call disable hooks twice.
572 encoder
->bridge
->funcs
->disable(encoder
->bridge
);
574 /* Right function depends upon target state. */
575 if (connector
->state
->crtc
)
576 funcs
->prepare(encoder
);
577 else if (funcs
->disable
)
578 funcs
->disable(encoder
);
580 funcs
->dpms(encoder
, DRM_MODE_DPMS_OFF
);
583 encoder
->bridge
->funcs
->post_disable(encoder
->bridge
);
586 for (i
= 0; i
< ncrtcs
; i
++) {
587 struct drm_crtc_helper_funcs
*funcs
;
588 struct drm_crtc
*crtc
;
590 crtc
= old_state
->crtcs
[i
];
592 /* Shut down everything that needs a full modeset. */
593 if (!crtc
|| !crtc
->state
->mode_changed
)
596 funcs
= crtc
->helper_private
;
598 /* Right function depends upon target state. */
599 if (crtc
->state
->enable
)
600 funcs
->prepare(crtc
);
601 else if (funcs
->disable
)
602 funcs
->disable(crtc
);
604 funcs
->dpms(crtc
, DRM_MODE_DPMS_OFF
);
609 set_routing_links(struct drm_device
*dev
, struct drm_atomic_state
*old_state
)
611 int ncrtcs
= old_state
->dev
->mode_config
.num_crtc
;
614 /* clear out existing links */
615 for (i
= 0; i
< old_state
->num_connector
; i
++) {
616 struct drm_connector
*connector
;
618 connector
= old_state
->connectors
[i
];
620 if (!connector
|| !connector
->encoder
)
623 WARN_ON(!connector
->encoder
->crtc
);
625 connector
->encoder
->crtc
= NULL
;
626 connector
->encoder
= NULL
;
630 for (i
= 0; i
< old_state
->num_connector
; i
++) {
631 struct drm_connector
*connector
;
633 connector
= old_state
->connectors
[i
];
635 if (!connector
|| !connector
->state
->crtc
)
638 if (WARN_ON(!connector
->state
->best_encoder
))
641 connector
->encoder
= connector
->state
->best_encoder
;
642 connector
->encoder
->crtc
= connector
->state
->crtc
;
645 /* set legacy state in the crtc structure */
646 for (i
= 0; i
< ncrtcs
; i
++) {
647 struct drm_crtc
*crtc
;
649 crtc
= old_state
->crtcs
[i
];
654 crtc
->mode
= crtc
->state
->mode
;
655 crtc
->enabled
= crtc
->state
->enable
;
656 crtc
->x
= crtc
->primary
->state
->src_x
>> 16;
657 crtc
->y
= crtc
->primary
->state
->src_y
>> 16;
662 crtc_set_mode(struct drm_device
*dev
, struct drm_atomic_state
*old_state
)
664 int ncrtcs
= old_state
->dev
->mode_config
.num_crtc
;
667 for (i
= 0; i
< ncrtcs
; i
++) {
668 struct drm_crtc_helper_funcs
*funcs
;
669 struct drm_crtc
*crtc
;
671 crtc
= old_state
->crtcs
[i
];
673 if (!crtc
|| !crtc
->state
->mode_changed
)
676 funcs
= crtc
->helper_private
;
678 if (crtc
->state
->enable
)
679 funcs
->mode_set_nofb(crtc
);
682 for (i
= 0; i
< old_state
->num_connector
; i
++) {
683 struct drm_connector
*connector
;
684 struct drm_crtc_state
*new_crtc_state
;
685 struct drm_encoder_helper_funcs
*funcs
;
686 struct drm_encoder
*encoder
;
687 struct drm_display_mode
*mode
, *adjusted_mode
;
689 connector
= old_state
->connectors
[i
];
691 if (!connector
|| !connector
->state
->best_encoder
)
694 encoder
= connector
->state
->best_encoder
;
695 funcs
= encoder
->helper_private
;
696 new_crtc_state
= connector
->state
->crtc
->state
;
697 mode
= &new_crtc_state
->mode
;
698 adjusted_mode
= &new_crtc_state
->adjusted_mode
;
701 * Each encoder has at most one connector (since we always steal
702 * it away), so we won't call call mode_set hooks twice.
704 funcs
->mode_set(encoder
, mode
, adjusted_mode
);
706 if (encoder
->bridge
&& encoder
->bridge
->funcs
->mode_set
)
707 encoder
->bridge
->funcs
->mode_set(encoder
->bridge
,
708 mode
, adjusted_mode
);
713 * drm_atomic_helper_commit_pre_planes - modeset commit before plane updates
715 * @state: atomic state
717 * This function commits the modeset changes that need to be committed before
718 * updating planes. It shuts down all the outputs that need to be shut down and
719 * prepares them (if required) with the new mode.
721 void drm_atomic_helper_commit_pre_planes(struct drm_device
*dev
,
722 struct drm_atomic_state
*state
)
724 disable_outputs(dev
, state
);
725 set_routing_links(dev
, state
);
726 crtc_set_mode(dev
, state
);
728 EXPORT_SYMBOL(drm_atomic_helper_commit_pre_planes
);
731 * drm_atomic_helper_commit_post_planes - modeset commit after plane updates
733 * @old_state: atomic state object with old state structures
735 * This function commits the modeset changes that need to be committed after
736 * updating planes: It enables all the outputs with the new configuration which
737 * had to be turned off for the update.
739 void drm_atomic_helper_commit_post_planes(struct drm_device
*dev
,
740 struct drm_atomic_state
*old_state
)
742 int ncrtcs
= old_state
->dev
->mode_config
.num_crtc
;
745 for (i
= 0; i
< ncrtcs
; i
++) {
746 struct drm_crtc_helper_funcs
*funcs
;
747 struct drm_crtc
*crtc
;
749 crtc
= old_state
->crtcs
[i
];
751 /* Need to filter out CRTCs where only planes change. */
752 if (!crtc
|| !crtc
->state
->mode_changed
)
755 funcs
= crtc
->helper_private
;
757 if (crtc
->state
->enable
)
761 for (i
= 0; i
< old_state
->num_connector
; i
++) {
762 struct drm_connector
*connector
;
763 struct drm_encoder_helper_funcs
*funcs
;
764 struct drm_encoder
*encoder
;
766 connector
= old_state
->connectors
[i
];
768 if (!connector
|| !connector
->state
->best_encoder
)
771 encoder
= connector
->state
->best_encoder
;
772 funcs
= encoder
->helper_private
;
775 * Each encoder has at most one connector (since we always steal
776 * it away), so we won't call call enable hooks twice.
779 encoder
->bridge
->funcs
->pre_enable(encoder
->bridge
);
781 funcs
->commit(encoder
);
784 encoder
->bridge
->funcs
->enable(encoder
->bridge
);
787 EXPORT_SYMBOL(drm_atomic_helper_commit_post_planes
);
789 static void wait_for_fences(struct drm_device
*dev
,
790 struct drm_atomic_state
*state
)
792 int nplanes
= dev
->mode_config
.num_total_plane
;
795 for (i
= 0; i
< nplanes
; i
++) {
796 struct drm_plane
*plane
= state
->planes
[i
];
798 if (!plane
|| !plane
->state
->fence
)
801 WARN_ON(!plane
->state
->fb
);
803 fence_wait(plane
->state
->fence
, false);
804 fence_put(plane
->state
->fence
);
805 plane
->state
->fence
= NULL
;
809 static bool framebuffer_changed(struct drm_device
*dev
,
810 struct drm_atomic_state
*old_state
,
811 struct drm_crtc
*crtc
)
813 struct drm_plane
*plane
;
814 struct drm_plane_state
*old_plane_state
;
815 int nplanes
= old_state
->dev
->mode_config
.num_total_plane
;
818 for (i
= 0; i
< nplanes
; i
++) {
819 plane
= old_state
->planes
[i
];
820 old_plane_state
= old_state
->plane_states
[i
];
825 if (plane
->state
->crtc
!= crtc
&&
826 old_plane_state
->crtc
!= crtc
)
829 if (plane
->state
->fb
!= old_plane_state
->fb
)
837 * drm_atomic_helper_wait_for_vblanks - wait for vblank on crtcs
839 * @old_state: atomic state object with old state structures
841 * Helper to, after atomic commit, wait for vblanks on all effected
842 * crtcs (ie. before cleaning up old framebuffers using
843 * drm_atomic_helper_cleanup_planes()). It will only wait on crtcs where the
844 * framebuffers have actually changed to optimize for the legacy cursor and
845 * plane update use-case.
848 drm_atomic_helper_wait_for_vblanks(struct drm_device
*dev
,
849 struct drm_atomic_state
*old_state
)
851 struct drm_crtc
*crtc
;
852 struct drm_crtc_state
*old_crtc_state
;
853 int ncrtcs
= old_state
->dev
->mode_config
.num_crtc
;
856 for (i
= 0; i
< ncrtcs
; i
++) {
857 crtc
= old_state
->crtcs
[i
];
858 old_crtc_state
= old_state
->crtc_states
[i
];
863 /* No one cares about the old state, so abuse it for tracking
864 * and store whether we hold a vblank reference (and should do a
865 * vblank wait) in the ->enable boolean. */
866 old_crtc_state
->enable
= false;
868 if (!crtc
->state
->enable
)
871 if (!framebuffer_changed(dev
, old_state
, crtc
))
874 ret
= drm_crtc_vblank_get(crtc
);
878 old_crtc_state
->enable
= true;
879 old_crtc_state
->last_vblank_count
= drm_vblank_count(dev
, i
);
882 for (i
= 0; i
< ncrtcs
; i
++) {
883 crtc
= old_state
->crtcs
[i
];
884 old_crtc_state
= old_state
->crtc_states
[i
];
886 if (!crtc
|| !old_crtc_state
->enable
)
889 ret
= wait_event_timeout(dev
->vblank
[i
].queue
,
890 old_crtc_state
->last_vblank_count
!=
891 drm_vblank_count(dev
, i
),
892 msecs_to_jiffies(50));
894 drm_crtc_vblank_put(crtc
);
897 EXPORT_SYMBOL(drm_atomic_helper_wait_for_vblanks
);
900 * drm_atomic_helper_commit - commit validated state object
902 * @state: the driver state object
903 * @async: asynchronous commit
905 * This function commits a with drm_atomic_helper_check() pre-validated state
906 * object. This can still fail when e.g. the framebuffer reservation fails. For
907 * now this doesn't implement asynchronous commits.
910 * Zero for success or -errno.
912 int drm_atomic_helper_commit(struct drm_device
*dev
,
913 struct drm_atomic_state
*state
,
921 ret
= drm_atomic_helper_prepare_planes(dev
, state
);
926 * This is the point of no return - everything below never fails except
927 * when the hw goes bonghits. Which means we can commit the new state on
928 * the software side now.
931 drm_atomic_helper_swap_state(dev
, state
);
934 * Everything below can be run asynchronously without the need to grab
935 * any modeset locks at all under one conditions: It must be guaranteed
936 * that the asynchronous work has either been cancelled (if the driver
937 * supports it, which at least requires that the framebuffers get
938 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
939 * before the new state gets committed on the software side with
940 * drm_atomic_helper_swap_state().
942 * This scheme allows new atomic state updates to be prepared and
943 * checked in parallel to the asynchronous completion of the previous
944 * update. Which is important since compositors need to figure out the
945 * composition of the next frame right after having submitted the
949 wait_for_fences(dev
, state
);
951 drm_atomic_helper_commit_pre_planes(dev
, state
);
953 drm_atomic_helper_commit_planes(dev
, state
);
955 drm_atomic_helper_commit_post_planes(dev
, state
);
957 drm_atomic_helper_wait_for_vblanks(dev
, state
);
959 drm_atomic_helper_cleanup_planes(dev
, state
);
961 drm_atomic_state_free(state
);
965 EXPORT_SYMBOL(drm_atomic_helper_commit
);
968 * DOC: implementing async commit
970 * For now the atomic helpers don't support async commit directly. If there is
971 * real need it could be added though, using the dma-buf fence infrastructure
972 * for generic synchronization with outstanding rendering.
974 * For now drivers have to implement async commit themselves, with the following
975 * sequence being the recommended one:
977 * 1. Run drm_atomic_helper_prepare_planes() first. This is the only function
978 * which commit needs to call which can fail, so we want to run it first and
981 * 2. Synchronize with any outstanding asynchronous commit worker threads which
982 * might be affected the new state update. This can be done by either cancelling
983 * or flushing the work items, depending upon whether the driver can deal with
984 * cancelled updates. Note that it is important to ensure that the framebuffer
985 * cleanup is still done when cancelling.
987 * For sufficient parallelism it is recommended to have a work item per crtc
988 * (for updates which don't touch global state) and a global one. Then we only
989 * need to synchronize with the crtc work items for changed crtcs and the global
990 * work item, which allows nice concurrent updates on disjoint sets of crtcs.
992 * 3. The software state is updated synchronously with
993 * drm_atomic_helper_swap_state. Doing this under the protection of all modeset
994 * locks means concurrent callers never see inconsistent state. And doing this
995 * while it's guaranteed that no relevant async worker runs means that async
996 * workers do not need grab any locks. Actually they must not grab locks, for
997 * otherwise the work flushing will deadlock.
999 * 4. Schedule a work item to do all subsequent steps, using the split-out
1000 * commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and
1001 * then cleaning up the framebuffers after the old framebuffer is no longer
1006 * drm_atomic_helper_prepare_planes - prepare plane resources after commit
1008 * @state: atomic state object with old state structures
1010 * This function prepares plane state, specifically framebuffers, for the new
1011 * configuration. If any failure is encountered this function will call
1012 * ->cleanup_fb on any already successfully prepared framebuffer.
1015 * 0 on success, negative error code on failure.
1017 int drm_atomic_helper_prepare_planes(struct drm_device
*dev
,
1018 struct drm_atomic_state
*state
)
1020 int nplanes
= dev
->mode_config
.num_total_plane
;
1023 for (i
= 0; i
< nplanes
; i
++) {
1024 struct drm_plane_helper_funcs
*funcs
;
1025 struct drm_plane
*plane
= state
->planes
[i
];
1026 struct drm_framebuffer
*fb
;
1031 funcs
= plane
->helper_private
;
1033 fb
= state
->plane_states
[i
]->fb
;
1035 if (fb
&& funcs
->prepare_fb
) {
1036 ret
= funcs
->prepare_fb(plane
, fb
);
1045 for (i
--; i
>= 0; i
--) {
1046 struct drm_plane_helper_funcs
*funcs
;
1047 struct drm_plane
*plane
= state
->planes
[i
];
1048 struct drm_framebuffer
*fb
;
1053 funcs
= plane
->helper_private
;
1055 fb
= state
->plane_states
[i
]->fb
;
1057 if (fb
&& funcs
->cleanup_fb
)
1058 funcs
->cleanup_fb(plane
, fb
);
1064 EXPORT_SYMBOL(drm_atomic_helper_prepare_planes
);
1067 * drm_atomic_helper_commit_planes - commit plane state
1069 * @old_state: atomic state object with old state structures
1071 * This function commits the new plane state using the plane and atomic helper
1072 * functions for planes and crtcs. It assumes that the atomic state has already
1073 * been pushed into the relevant object state pointers, since this step can no
1076 * It still requires the global state object @old_state to know which planes and
1077 * crtcs need to be updated though.
1079 void drm_atomic_helper_commit_planes(struct drm_device
*dev
,
1080 struct drm_atomic_state
*old_state
)
1082 int nplanes
= dev
->mode_config
.num_total_plane
;
1083 int ncrtcs
= dev
->mode_config
.num_crtc
;
1086 for (i
= 0; i
< ncrtcs
; i
++) {
1087 struct drm_crtc_helper_funcs
*funcs
;
1088 struct drm_crtc
*crtc
= old_state
->crtcs
[i
];
1093 funcs
= crtc
->helper_private
;
1095 if (!funcs
|| !funcs
->atomic_begin
)
1098 funcs
->atomic_begin(crtc
);
1101 for (i
= 0; i
< nplanes
; i
++) {
1102 struct drm_plane_helper_funcs
*funcs
;
1103 struct drm_plane
*plane
= old_state
->planes
[i
];
1104 struct drm_plane_state
*old_plane_state
;
1109 funcs
= plane
->helper_private
;
1111 if (!funcs
|| !funcs
->atomic_update
)
1114 old_plane_state
= old_state
->plane_states
[i
];
1116 funcs
->atomic_update(plane
, old_plane_state
);
1119 for (i
= 0; i
< ncrtcs
; i
++) {
1120 struct drm_crtc_helper_funcs
*funcs
;
1121 struct drm_crtc
*crtc
= old_state
->crtcs
[i
];
1126 funcs
= crtc
->helper_private
;
1128 if (!funcs
|| !funcs
->atomic_flush
)
1131 funcs
->atomic_flush(crtc
);
1134 EXPORT_SYMBOL(drm_atomic_helper_commit_planes
);
1137 * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
1139 * @old_state: atomic state object with old state structures
1141 * This function cleans up plane state, specifically framebuffers, from the old
1142 * configuration. Hence the old configuration must be perserved in @old_state to
1143 * be able to call this function.
1145 * This function must also be called on the new state when the atomic update
1146 * fails at any point after calling drm_atomic_helper_prepare_planes().
1148 void drm_atomic_helper_cleanup_planes(struct drm_device
*dev
,
1149 struct drm_atomic_state
*old_state
)
1151 int nplanes
= dev
->mode_config
.num_total_plane
;
1154 for (i
= 0; i
< nplanes
; i
++) {
1155 struct drm_plane_helper_funcs
*funcs
;
1156 struct drm_plane
*plane
= old_state
->planes
[i
];
1157 struct drm_framebuffer
*old_fb
;
1162 funcs
= plane
->helper_private
;
1164 old_fb
= old_state
->plane_states
[i
]->fb
;
1166 if (old_fb
&& funcs
->cleanup_fb
)
1167 funcs
->cleanup_fb(plane
, old_fb
);
1170 EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes
);
1173 * drm_atomic_helper_swap_state - store atomic state into current sw state
1175 * @state: atomic state
1177 * This function stores the atomic state into the current state pointers in all
1178 * driver objects. It should be called after all failing steps have been done
1179 * and succeeded, but before the actual hardware state is committed.
1181 * For cleanup and error recovery the current state for all changed objects will
1182 * be swaped into @state.
1184 * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
1186 * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
1188 * 2. Do any other steps that might fail.
1190 * 3. Put the staged state into the current state pointers with this function.
1192 * 4. Actually commit the hardware state.
1194 * 5. Call drm_atomic_helper_cleanup_planes with @state, which since step 3
1195 * contains the old state. Also do any other cleanup required with that state.
1197 void drm_atomic_helper_swap_state(struct drm_device
*dev
,
1198 struct drm_atomic_state
*state
)
1202 for (i
= 0; i
< dev
->mode_config
.num_connector
; i
++) {
1203 struct drm_connector
*connector
= state
->connectors
[i
];
1208 connector
->state
->state
= state
;
1209 swap(state
->connector_states
[i
], connector
->state
);
1210 connector
->state
->state
= NULL
;
1213 for (i
= 0; i
< dev
->mode_config
.num_crtc
; i
++) {
1214 struct drm_crtc
*crtc
= state
->crtcs
[i
];
1219 crtc
->state
->state
= state
;
1220 swap(state
->crtc_states
[i
], crtc
->state
);
1221 crtc
->state
->state
= NULL
;
1224 for (i
= 0; i
< dev
->mode_config
.num_total_plane
; i
++) {
1225 struct drm_plane
*plane
= state
->planes
[i
];
1230 plane
->state
->state
= state
;
1231 swap(state
->plane_states
[i
], plane
->state
);
1232 plane
->state
->state
= NULL
;
1235 EXPORT_SYMBOL(drm_atomic_helper_swap_state
);
1238 * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
1239 * @plane: plane object to update
1240 * @crtc: owning CRTC of owning plane
1241 * @fb: framebuffer to flip onto plane
1242 * @crtc_x: x offset of primary plane on crtc
1243 * @crtc_y: y offset of primary plane on crtc
1244 * @crtc_w: width of primary plane rectangle on crtc
1245 * @crtc_h: height of primary plane rectangle on crtc
1246 * @src_x: x offset of @fb for panning
1247 * @src_y: y offset of @fb for panning
1248 * @src_w: width of source rectangle in @fb
1249 * @src_h: height of source rectangle in @fb
1251 * Provides a default plane update handler using the atomic driver interface.
1254 * Zero on success, error code on failure
1256 int drm_atomic_helper_update_plane(struct drm_plane
*plane
,
1257 struct drm_crtc
*crtc
,
1258 struct drm_framebuffer
*fb
,
1259 int crtc_x
, int crtc_y
,
1260 unsigned int crtc_w
, unsigned int crtc_h
,
1261 uint32_t src_x
, uint32_t src_y
,
1262 uint32_t src_w
, uint32_t src_h
)
1264 struct drm_atomic_state
*state
;
1265 struct drm_plane_state
*plane_state
;
1268 state
= drm_atomic_state_alloc(plane
->dev
);
1272 state
->acquire_ctx
= drm_modeset_legacy_acquire_ctx(crtc
);
1274 plane_state
= drm_atomic_get_plane_state(state
, plane
);
1275 if (IS_ERR(plane_state
)) {
1276 ret
= PTR_ERR(plane_state
);
1280 ret
= drm_atomic_set_crtc_for_plane(plane_state
, crtc
);
1283 drm_atomic_set_fb_for_plane(plane_state
, fb
);
1284 plane_state
->crtc_x
= crtc_x
;
1285 plane_state
->crtc_y
= crtc_y
;
1286 plane_state
->crtc_h
= crtc_h
;
1287 plane_state
->crtc_w
= crtc_w
;
1288 plane_state
->src_x
= src_x
;
1289 plane_state
->src_y
= src_y
;
1290 plane_state
->src_h
= src_h
;
1291 plane_state
->src_w
= src_w
;
1293 ret
= drm_atomic_commit(state
);
1297 /* Driver takes ownership of state on successful commit. */
1300 if (ret
== -EDEADLK
)
1303 drm_atomic_state_free(state
);
1307 drm_atomic_state_clear(state
);
1308 drm_atomic_legacy_backoff(state
);
1311 * Someone might have exchanged the framebuffer while we dropped locks
1312 * in the backoff code. We need to fix up the fb refcount tracking the
1315 plane
->old_fb
= plane
->fb
;
1319 EXPORT_SYMBOL(drm_atomic_helper_update_plane
);
1322 * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
1323 * @plane: plane to disable
1325 * Provides a default plane disable handler using the atomic driver interface.
1328 * Zero on success, error code on failure
1330 int drm_atomic_helper_disable_plane(struct drm_plane
*plane
)
1332 struct drm_atomic_state
*state
;
1333 struct drm_plane_state
*plane_state
;
1337 * FIXME: Without plane->crtc set we can't get at the implicit legacy
1338 * acquire context. The real fix will be to wire the acquire ctx through
1339 * everywhere we need it, but meanwhile prevent chaos by just skipping
1340 * this noop. The critical case is the cursor ioctls which a) only grab
1341 * crtc/cursor-plane locks (so we need the crtc to get at the right
1342 * acquire context) and b) can try to disable the plane multiple times.
1347 state
= drm_atomic_state_alloc(plane
->dev
);
1351 state
->acquire_ctx
= drm_modeset_legacy_acquire_ctx(plane
->crtc
);
1353 plane_state
= drm_atomic_get_plane_state(state
, plane
);
1354 if (IS_ERR(plane_state
)) {
1355 ret
= PTR_ERR(plane_state
);
1359 ret
= drm_atomic_set_crtc_for_plane(plane_state
, NULL
);
1362 drm_atomic_set_fb_for_plane(plane_state
, NULL
);
1363 plane_state
->crtc_x
= 0;
1364 plane_state
->crtc_y
= 0;
1365 plane_state
->crtc_h
= 0;
1366 plane_state
->crtc_w
= 0;
1367 plane_state
->src_x
= 0;
1368 plane_state
->src_y
= 0;
1369 plane_state
->src_h
= 0;
1370 plane_state
->src_w
= 0;
1372 ret
= drm_atomic_commit(state
);
1376 /* Driver takes ownership of state on successful commit. */
1379 if (ret
== -EDEADLK
)
1382 drm_atomic_state_free(state
);
1386 drm_atomic_state_clear(state
);
1387 drm_atomic_legacy_backoff(state
);
1390 * Someone might have exchanged the framebuffer while we dropped locks
1391 * in the backoff code. We need to fix up the fb refcount tracking the
1394 plane
->old_fb
= plane
->fb
;
1398 EXPORT_SYMBOL(drm_atomic_helper_disable_plane
);
1400 static int update_output_state(struct drm_atomic_state
*state
,
1401 struct drm_mode_set
*set
)
1403 struct drm_device
*dev
= set
->crtc
->dev
;
1404 struct drm_connector_state
*conn_state
;
1405 int ncrtcs
= state
->dev
->mode_config
.num_crtc
;
1408 ret
= drm_modeset_lock(&dev
->mode_config
.connection_mutex
,
1409 state
->acquire_ctx
);
1413 /* First grab all affected connector/crtc states. */
1414 for (i
= 0; i
< set
->num_connectors
; i
++) {
1415 conn_state
= drm_atomic_get_connector_state(state
,
1416 set
->connectors
[i
]);
1417 if (IS_ERR(conn_state
))
1418 return PTR_ERR(conn_state
);
1421 for (i
= 0; i
< ncrtcs
; i
++) {
1422 struct drm_crtc
*crtc
= state
->crtcs
[i
];
1427 ret
= drm_atomic_add_affected_connectors(state
, crtc
);
1432 /* Then recompute connector->crtc links and crtc enabling state. */
1433 for (i
= 0; i
< state
->num_connector
; i
++) {
1434 struct drm_connector
*connector
;
1436 connector
= state
->connectors
[i
];
1437 conn_state
= state
->connector_states
[i
];
1442 if (conn_state
->crtc
== set
->crtc
) {
1443 ret
= drm_atomic_set_crtc_for_connector(conn_state
,
1449 for (j
= 0; j
< set
->num_connectors
; j
++) {
1450 if (set
->connectors
[j
] == connector
) {
1451 ret
= drm_atomic_set_crtc_for_connector(conn_state
,
1460 for (i
= 0; i
< ncrtcs
; i
++) {
1461 struct drm_crtc
*crtc
= state
->crtcs
[i
];
1462 struct drm_crtc_state
*crtc_state
= state
->crtc_states
[i
];
1467 /* Don't update ->enable for the CRTC in the set_config request,
1468 * since a mismatch would indicate a bug in the upper layers.
1469 * The actual modeset code later on will catch any
1470 * inconsistencies here. */
1471 if (crtc
== set
->crtc
)
1474 crtc_state
->enable
=
1475 drm_atomic_connectors_for_crtc(state
, crtc
);
1482 * drm_atomic_helper_set_config - set a new config from userspace
1483 * @set: mode set configuration
1485 * Provides a default crtc set_config handler using the atomic driver interface.
1488 * Returns 0 on success, negative errno numbers on failure.
1490 int drm_atomic_helper_set_config(struct drm_mode_set
*set
)
1492 struct drm_atomic_state
*state
;
1493 struct drm_crtc
*crtc
= set
->crtc
;
1494 struct drm_crtc_state
*crtc_state
;
1495 struct drm_plane_state
*primary_state
;
1498 state
= drm_atomic_state_alloc(crtc
->dev
);
1502 state
->acquire_ctx
= drm_modeset_legacy_acquire_ctx(crtc
);
1504 crtc_state
= drm_atomic_get_crtc_state(state
, crtc
);
1505 if (IS_ERR(crtc_state
)) {
1506 ret
= PTR_ERR(crtc_state
);
1510 primary_state
= drm_atomic_get_plane_state(state
, crtc
->primary
);
1511 if (IS_ERR(primary_state
)) {
1512 ret
= PTR_ERR(primary_state
);
1518 WARN_ON(set
->num_connectors
);
1520 crtc_state
->enable
= false;
1522 ret
= drm_atomic_set_crtc_for_plane(primary_state
, NULL
);
1526 drm_atomic_set_fb_for_plane(primary_state
, NULL
);
1532 WARN_ON(!set
->num_connectors
);
1534 crtc_state
->enable
= true;
1535 drm_mode_copy(&crtc_state
->mode
, set
->mode
);
1537 ret
= drm_atomic_set_crtc_for_plane(primary_state
, crtc
);
1540 drm_atomic_set_fb_for_plane(primary_state
, set
->fb
);
1541 primary_state
->crtc_x
= 0;
1542 primary_state
->crtc_y
= 0;
1543 primary_state
->crtc_h
= set
->mode
->vdisplay
;
1544 primary_state
->crtc_w
= set
->mode
->hdisplay
;
1545 primary_state
->src_x
= set
->x
<< 16;
1546 primary_state
->src_y
= set
->y
<< 16;
1547 primary_state
->src_h
= set
->mode
->vdisplay
<< 16;
1548 primary_state
->src_w
= set
->mode
->hdisplay
<< 16;
1551 ret
= update_output_state(state
, set
);
1555 ret
= drm_atomic_commit(state
);
1559 /* Driver takes ownership of state on successful commit. */
1562 if (ret
== -EDEADLK
)
1565 drm_atomic_state_free(state
);
1569 drm_atomic_state_clear(state
);
1570 drm_atomic_legacy_backoff(state
);
1573 * Someone might have exchanged the framebuffer while we dropped locks
1574 * in the backoff code. We need to fix up the fb refcount tracking the
1577 crtc
->primary
->old_fb
= crtc
->primary
->fb
;
1581 EXPORT_SYMBOL(drm_atomic_helper_set_config
);
1584 * drm_atomic_helper_crtc_set_property - helper for crtc prorties
1586 * @property: DRM property
1587 * @val: value of property
1589 * Provides a default plane disablle handler using the atomic driver interface.
1592 * Zero on success, error code on failure
1595 drm_atomic_helper_crtc_set_property(struct drm_crtc
*crtc
,
1596 struct drm_property
*property
,
1599 struct drm_atomic_state
*state
;
1600 struct drm_crtc_state
*crtc_state
;
1603 state
= drm_atomic_state_alloc(crtc
->dev
);
1607 /* ->set_property is always called with all locks held. */
1608 state
->acquire_ctx
= crtc
->dev
->mode_config
.acquire_ctx
;
1610 crtc_state
= drm_atomic_get_crtc_state(state
, crtc
);
1611 if (IS_ERR(crtc_state
)) {
1612 ret
= PTR_ERR(crtc_state
);
1616 ret
= crtc
->funcs
->atomic_set_property(crtc
, crtc_state
,
1621 ret
= drm_atomic_commit(state
);
1625 /* Driver takes ownership of state on successful commit. */
1628 if (ret
== -EDEADLK
)
1631 drm_atomic_state_free(state
);
1635 drm_atomic_state_clear(state
);
1636 drm_atomic_legacy_backoff(state
);
1640 EXPORT_SYMBOL(drm_atomic_helper_crtc_set_property
);
1643 * drm_atomic_helper_plane_set_property - helper for plane prorties
1645 * @property: DRM property
1646 * @val: value of property
1648 * Provides a default plane disable handler using the atomic driver interface.
1651 * Zero on success, error code on failure
1654 drm_atomic_helper_plane_set_property(struct drm_plane
*plane
,
1655 struct drm_property
*property
,
1658 struct drm_atomic_state
*state
;
1659 struct drm_plane_state
*plane_state
;
1662 state
= drm_atomic_state_alloc(plane
->dev
);
1666 /* ->set_property is always called with all locks held. */
1667 state
->acquire_ctx
= plane
->dev
->mode_config
.acquire_ctx
;
1669 plane_state
= drm_atomic_get_plane_state(state
, plane
);
1670 if (IS_ERR(plane_state
)) {
1671 ret
= PTR_ERR(plane_state
);
1675 ret
= plane
->funcs
->atomic_set_property(plane
, plane_state
,
1680 ret
= drm_atomic_commit(state
);
1684 /* Driver takes ownership of state on successful commit. */
1687 if (ret
== -EDEADLK
)
1690 drm_atomic_state_free(state
);
1694 drm_atomic_state_clear(state
);
1695 drm_atomic_legacy_backoff(state
);
1699 EXPORT_SYMBOL(drm_atomic_helper_plane_set_property
);
1702 * drm_atomic_helper_connector_set_property - helper for connector prorties
1703 * @connector: DRM connector
1704 * @property: DRM property
1705 * @val: value of property
1707 * Provides a default plane disablle handler using the atomic driver interface.
1710 * Zero on success, error code on failure
1713 drm_atomic_helper_connector_set_property(struct drm_connector
*connector
,
1714 struct drm_property
*property
,
1717 struct drm_atomic_state
*state
;
1718 struct drm_connector_state
*connector_state
;
1721 state
= drm_atomic_state_alloc(connector
->dev
);
1725 /* ->set_property is always called with all locks held. */
1726 state
->acquire_ctx
= connector
->dev
->mode_config
.acquire_ctx
;
1728 connector_state
= drm_atomic_get_connector_state(state
, connector
);
1729 if (IS_ERR(connector_state
)) {
1730 ret
= PTR_ERR(connector_state
);
1734 ret
= connector
->funcs
->atomic_set_property(connector
, connector_state
,
1739 ret
= drm_atomic_commit(state
);
1743 /* Driver takes ownership of state on successful commit. */
1746 if (ret
== -EDEADLK
)
1749 drm_atomic_state_free(state
);
1753 drm_atomic_state_clear(state
);
1754 drm_atomic_legacy_backoff(state
);
1758 EXPORT_SYMBOL(drm_atomic_helper_connector_set_property
);
1761 * drm_atomic_helper_page_flip - execute a legacy page flip
1763 * @fb: DRM framebuffer
1764 * @event: optional DRM event to signal upon completion
1765 * @flags: flip flags for non-vblank sync'ed updates
1767 * Provides a default page flip implementation using the atomic driver interface.
1769 * Note that for now so called async page flips (i.e. updates which are not
1770 * synchronized to vblank) are not supported, since the atomic interfaces have
1771 * no provisions for this yet.
1774 * Returns 0 on success, negative errno numbers on failure.
1776 int drm_atomic_helper_page_flip(struct drm_crtc
*crtc
,
1777 struct drm_framebuffer
*fb
,
1778 struct drm_pending_vblank_event
*event
,
1781 struct drm_plane
*plane
= crtc
->primary
;
1782 struct drm_atomic_state
*state
;
1783 struct drm_plane_state
*plane_state
;
1784 struct drm_crtc_state
*crtc_state
;
1787 if (flags
& DRM_MODE_PAGE_FLIP_ASYNC
)
1790 state
= drm_atomic_state_alloc(plane
->dev
);
1794 state
->acquire_ctx
= drm_modeset_legacy_acquire_ctx(crtc
);
1796 crtc_state
= drm_atomic_get_crtc_state(state
, crtc
);
1797 if (IS_ERR(crtc_state
)) {
1798 ret
= PTR_ERR(crtc_state
);
1801 crtc_state
->event
= event
;
1803 plane_state
= drm_atomic_get_plane_state(state
, plane
);
1804 if (IS_ERR(plane_state
)) {
1805 ret
= PTR_ERR(plane_state
);
1809 ret
= drm_atomic_set_crtc_for_plane(plane_state
, crtc
);
1812 drm_atomic_set_fb_for_plane(plane_state
, fb
);
1814 ret
= drm_atomic_async_commit(state
);
1818 /* TODO: ->page_flip is the only driver callback where the core
1819 * doesn't update plane->fb. For now patch it up here. */
1820 plane
->fb
= plane
->state
->fb
;
1822 /* Driver takes ownership of state on successful async commit. */
1825 if (ret
== -EDEADLK
)
1828 drm_atomic_state_free(state
);
1832 drm_atomic_state_clear(state
);
1833 drm_atomic_legacy_backoff(state
);
1836 * Someone might have exchanged the framebuffer while we dropped locks
1837 * in the backoff code. We need to fix up the fb refcount tracking the
1840 plane
->old_fb
= plane
->fb
;
1844 EXPORT_SYMBOL(drm_atomic_helper_page_flip
);
1847 * DOC: atomic state reset and initialization
1849 * Both the drm core and the atomic helpers assume that there is always the full
1850 * and correct atomic software state for all connectors, CRTCs and planes
1851 * available. Which is a bit a problem on driver load and also after system
1852 * suspend. One way to solve this is to have a hardware state read-out
1853 * infrastructure which reconstructs the full software state (e.g. the i915
1856 * The simpler solution is to just reset the software state to everything off,
1857 * which is easiest to do by calling drm_mode_config_reset(). To facilitate this
1858 * the atomic helpers provide default reset implementations for all hooks.
1862 * drm_atomic_helper_crtc_reset - default ->reset hook for CRTCs
1865 * Resets the atomic state for @crtc by freeing the state pointer (which might
1866 * be NULL, e.g. at driver load time) and allocating a new empty state object.
1868 void drm_atomic_helper_crtc_reset(struct drm_crtc
*crtc
)
1871 crtc
->state
= kzalloc(sizeof(*crtc
->state
), GFP_KERNEL
);
1874 crtc
->state
->crtc
= crtc
;
1876 EXPORT_SYMBOL(drm_atomic_helper_crtc_reset
);
1879 * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
1882 * Default CRTC state duplicate hook for drivers which don't have their own
1883 * subclassed CRTC state structure.
1885 struct drm_crtc_state
*
1886 drm_atomic_helper_crtc_duplicate_state(struct drm_crtc
*crtc
)
1888 struct drm_crtc_state
*state
;
1890 if (WARN_ON(!crtc
->state
))
1893 state
= kmemdup(crtc
->state
, sizeof(*crtc
->state
), GFP_KERNEL
);
1896 state
->mode_changed
= false;
1897 state
->planes_changed
= false;
1898 state
->event
= NULL
;
1903 EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state
);
1906 * drm_atomic_helper_crtc_destroy_state - default state destroy hook
1908 * @state: CRTC state object to release
1910 * Default CRTC state destroy hook for drivers which don't have their own
1911 * subclassed CRTC state structure.
1913 void drm_atomic_helper_crtc_destroy_state(struct drm_crtc
*crtc
,
1914 struct drm_crtc_state
*state
)
1918 EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state
);
1921 * drm_atomic_helper_plane_reset - default ->reset hook for planes
1924 * Resets the atomic state for @plane by freeing the state pointer (which might
1925 * be NULL, e.g. at driver load time) and allocating a new empty state object.
1927 void drm_atomic_helper_plane_reset(struct drm_plane
*plane
)
1929 if (plane
->state
&& plane
->state
->fb
)
1930 drm_framebuffer_unreference(plane
->state
->fb
);
1932 kfree(plane
->state
);
1933 plane
->state
= kzalloc(sizeof(*plane
->state
), GFP_KERNEL
);
1936 plane
->state
->plane
= plane
;
1938 EXPORT_SYMBOL(drm_atomic_helper_plane_reset
);
1941 * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
1944 * Default plane state duplicate hook for drivers which don't have their own
1945 * subclassed plane state structure.
1947 struct drm_plane_state
*
1948 drm_atomic_helper_plane_duplicate_state(struct drm_plane
*plane
)
1950 struct drm_plane_state
*state
;
1952 if (WARN_ON(!plane
->state
))
1955 state
= kmemdup(plane
->state
, sizeof(*plane
->state
), GFP_KERNEL
);
1957 if (state
&& state
->fb
)
1958 drm_framebuffer_reference(state
->fb
);
1962 EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state
);
1965 * drm_atomic_helper_plane_destroy_state - default state destroy hook
1967 * @state: plane state object to release
1969 * Default plane state destroy hook for drivers which don't have their own
1970 * subclassed plane state structure.
1972 void drm_atomic_helper_plane_destroy_state(struct drm_plane
*plane
,
1973 struct drm_plane_state
*state
)
1976 drm_framebuffer_unreference(state
->fb
);
1980 EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state
);
1983 * drm_atomic_helper_connector_reset - default ->reset hook for connectors
1984 * @connector: drm connector
1986 * Resets the atomic state for @connector by freeing the state pointer (which
1987 * might be NULL, e.g. at driver load time) and allocating a new empty state
1990 void drm_atomic_helper_connector_reset(struct drm_connector
*connector
)
1992 kfree(connector
->state
);
1993 connector
->state
= kzalloc(sizeof(*connector
->state
), GFP_KERNEL
);
1995 if (connector
->state
)
1996 connector
->state
->connector
= connector
;
1998 EXPORT_SYMBOL(drm_atomic_helper_connector_reset
);
2001 * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
2002 * @connector: drm connector
2004 * Default connector state duplicate hook for drivers which don't have their own
2005 * subclassed connector state structure.
2007 struct drm_connector_state
*
2008 drm_atomic_helper_connector_duplicate_state(struct drm_connector
*connector
)
2010 if (WARN_ON(!connector
->state
))
2013 return kmemdup(connector
->state
, sizeof(*connector
->state
), GFP_KERNEL
);
2015 EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state
);
2018 * drm_atomic_helper_connector_destroy_state - default state destroy hook
2019 * @connector: drm connector
2020 * @state: connector state object to release
2022 * Default connector state destroy hook for drivers which don't have their own
2023 * subclassed connector state structure.
2025 void drm_atomic_helper_connector_destroy_state(struct drm_connector
*connector
,
2026 struct drm_connector_state
*state
)
2030 EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state
);