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 drm_for_each_connector(connector
, dev
) {
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_ATOMIC("[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
->connectors_changed
= true;
129 list_for_each_entry(connector
, &config
->connector_list
, head
) {
130 if (connector
->state
->best_encoder
!= encoder
)
133 DRM_DEBUG_ATOMIC("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 const 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_ATOMIC("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
->connectors_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
->connectors_changed
= true;
188 if (!connector_state
->crtc
) {
189 DRM_DEBUG_ATOMIC("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_ATOMIC("No suitable encoder found for [CONNECTOR:%d:%s]\n",
208 if (new_encoder
== connector_state
->best_encoder
) {
209 DRM_DEBUG_ATOMIC("[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_ATOMIC("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
->connectors_changed
= true;
238 DRM_DEBUG_ATOMIC("[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 struct drm_crtc
*crtc
;
252 struct drm_crtc_state
*crtc_state
;
253 struct drm_connector
*connector
;
254 struct drm_connector_state
*conn_state
;
258 for_each_crtc_in_state(state
, crtc
, crtc_state
, i
) {
259 if (!crtc_state
->mode_changed
&&
260 !crtc_state
->connectors_changed
)
263 drm_mode_copy(&crtc_state
->adjusted_mode
, &crtc_state
->mode
);
266 for_each_connector_in_state(state
, connector
, conn_state
, i
) {
267 const struct drm_encoder_helper_funcs
*funcs
;
268 struct drm_encoder
*encoder
;
270 WARN_ON(!!conn_state
->best_encoder
!= !!conn_state
->crtc
);
272 if (!conn_state
->crtc
|| !conn_state
->best_encoder
)
276 state
->crtc_states
[drm_crtc_index(conn_state
->crtc
)];
279 * Each encoder has at most one connector (since we always steal
280 * it away), so we won't call ->mode_fixup twice.
282 encoder
= conn_state
->best_encoder
;
283 funcs
= encoder
->helper_private
;
287 ret
= drm_bridge_mode_fixup(encoder
->bridge
, &crtc_state
->mode
,
288 &crtc_state
->adjusted_mode
);
290 DRM_DEBUG_ATOMIC("Bridge fixup failed\n");
294 if (funcs
->atomic_check
) {
295 ret
= funcs
->atomic_check(encoder
, crtc_state
,
298 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] check failed\n",
299 encoder
->base
.id
, encoder
->name
);
303 ret
= funcs
->mode_fixup(encoder
, &crtc_state
->mode
,
304 &crtc_state
->adjusted_mode
);
306 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] fixup failed\n",
307 encoder
->base
.id
, encoder
->name
);
313 for_each_crtc_in_state(state
, crtc
, crtc_state
, i
) {
314 const struct drm_crtc_helper_funcs
*funcs
;
316 if (!crtc_state
->mode_changed
&&
317 !crtc_state
->connectors_changed
)
320 funcs
= crtc
->helper_private
;
321 if (!funcs
->mode_fixup
)
324 ret
= funcs
->mode_fixup(crtc
, &crtc_state
->mode
,
325 &crtc_state
->adjusted_mode
);
327 DRM_DEBUG_ATOMIC("[CRTC:%d] fixup failed\n",
337 * drm_atomic_helper_check_modeset - validate state object for modeset changes
339 * @state: the driver state object
341 * Check the state object to see if the requested state is physically possible.
342 * This does all the crtc and connector related computations for an atomic
343 * update and adds any additional connectors needed for full modesets and calls
344 * down into ->mode_fixup functions of the driver backend.
346 * crtc_state->mode_changed is set when the input mode is changed.
347 * crtc_state->connectors_changed is set when a connector is added or
348 * removed from the crtc.
349 * crtc_state->active_changed is set when crtc_state->active changes,
350 * which is used for dpms.
354 * Drivers which update ->mode_changed (e.g. in their ->atomic_check hooks if a
355 * plane update can't be done without a full modeset) _must_ call this function
356 * afterwards after that change. It is permitted to call this function multiple
357 * times for the same update, e.g. when the ->atomic_check functions depend upon
358 * the adjusted dotclock for fifo space allocation and watermark computation.
361 * Zero for success or -errno
364 drm_atomic_helper_check_modeset(struct drm_device
*dev
,
365 struct drm_atomic_state
*state
)
367 struct drm_crtc
*crtc
;
368 struct drm_crtc_state
*crtc_state
;
369 struct drm_connector
*connector
;
370 struct drm_connector_state
*connector_state
;
373 for_each_crtc_in_state(state
, crtc
, crtc_state
, i
) {
374 if (!drm_mode_equal(&crtc
->state
->mode
, &crtc_state
->mode
)) {
375 DRM_DEBUG_ATOMIC("[CRTC:%d] mode changed\n",
377 crtc_state
->mode_changed
= true;
380 if (crtc
->state
->enable
!= crtc_state
->enable
) {
381 DRM_DEBUG_ATOMIC("[CRTC:%d] enable changed\n",
385 * For clarity this assignment is done here, but
386 * enable == 0 is only true when there are no
387 * connectors and a NULL mode.
389 * The other way around is true as well. enable != 0
390 * iff connectors are attached and a mode is set.
392 crtc_state
->mode_changed
= true;
393 crtc_state
->connectors_changed
= true;
397 for_each_connector_in_state(state
, connector
, connector_state
, i
) {
399 * This only sets crtc->mode_changed for routing changes,
400 * drivers must set crtc->mode_changed themselves when connector
401 * properties need to be updated.
403 ret
= update_connector_routing(state
, i
);
409 * After all the routing has been prepared we need to add in any
410 * connector which is itself unchanged, but who's crtc changes it's
411 * configuration. This must be done before calling mode_fixup in case a
412 * crtc only changed its mode but has the same set of connectors.
414 for_each_crtc_in_state(state
, crtc
, crtc_state
, i
) {
418 * We must set ->active_changed after walking connectors for
419 * otherwise an update that only changes active would result in
420 * a full modeset because update_connector_routing force that.
422 if (crtc
->state
->active
!= crtc_state
->active
) {
423 DRM_DEBUG_ATOMIC("[CRTC:%d] active changed\n",
425 crtc_state
->active_changed
= true;
428 if (!drm_atomic_crtc_needs_modeset(crtc_state
))
431 DRM_DEBUG_ATOMIC("[CRTC:%d] needs all connectors, enable: %c, active: %c\n",
433 crtc_state
->enable
? 'y' : 'n',
434 crtc_state
->active
? 'y' : 'n');
436 ret
= drm_atomic_add_affected_connectors(state
, crtc
);
440 ret
= drm_atomic_add_affected_planes(state
, crtc
);
444 num_connectors
= drm_atomic_connectors_for_crtc(state
,
447 if (crtc_state
->enable
!= !!num_connectors
) {
448 DRM_DEBUG_ATOMIC("[CRTC:%d] enabled/connectors mismatch\n",
455 return mode_fixup(state
);
457 EXPORT_SYMBOL(drm_atomic_helper_check_modeset
);
460 * drm_atomic_helper_check_planes - validate state object for planes changes
462 * @state: the driver state object
464 * Check the state object to see if the requested state is physically possible.
465 * This does all the plane update related checks using by calling into the
466 * ->atomic_check hooks provided by the driver.
468 * It also sets crtc_state->planes_changed to indicate that a crtc has
472 * Zero for success or -errno
475 drm_atomic_helper_check_planes(struct drm_device
*dev
,
476 struct drm_atomic_state
*state
)
478 struct drm_crtc
*crtc
;
479 struct drm_crtc_state
*crtc_state
;
480 struct drm_plane
*plane
;
481 struct drm_plane_state
*plane_state
;
484 for_each_plane_in_state(state
, plane
, plane_state
, i
) {
485 const struct drm_plane_helper_funcs
*funcs
;
487 funcs
= plane
->helper_private
;
489 drm_atomic_helper_plane_changed(state
, plane_state
, plane
);
491 if (!funcs
|| !funcs
->atomic_check
)
494 ret
= funcs
->atomic_check(plane
, plane_state
);
496 DRM_DEBUG_ATOMIC("[PLANE:%d] atomic driver check failed\n",
502 for_each_crtc_in_state(state
, crtc
, crtc_state
, i
) {
503 const struct drm_crtc_helper_funcs
*funcs
;
505 funcs
= crtc
->helper_private
;
507 if (!funcs
|| !funcs
->atomic_check
)
510 ret
= funcs
->atomic_check(crtc
, state
->crtc_states
[i
]);
512 DRM_DEBUG_ATOMIC("[CRTC:%d] atomic driver check failed\n",
520 EXPORT_SYMBOL(drm_atomic_helper_check_planes
);
523 * drm_atomic_helper_check - validate state object
525 * @state: the driver state object
527 * Check the state object to see if the requested state is physically possible.
528 * Only crtcs and planes have check callbacks, so for any additional (global)
529 * checking that a driver needs it can simply wrap that around this function.
530 * Drivers without such needs can directly use this as their ->atomic_check()
533 * This just wraps the two parts of the state checking for planes and modeset
534 * state in the default order: First it calls drm_atomic_helper_check_modeset()
535 * and then drm_atomic_helper_check_planes(). The assumption is that the
536 * ->atomic_check functions depend upon an updated adjusted_mode.clock to
537 * e.g. properly compute watermarks.
540 * Zero for success or -errno
542 int drm_atomic_helper_check(struct drm_device
*dev
,
543 struct drm_atomic_state
*state
)
547 ret
= drm_atomic_helper_check_modeset(dev
, state
);
551 ret
= drm_atomic_helper_check_planes(dev
, state
);
557 EXPORT_SYMBOL(drm_atomic_helper_check
);
560 disable_outputs(struct drm_device
*dev
, struct drm_atomic_state
*old_state
)
562 struct drm_connector
*connector
;
563 struct drm_connector_state
*old_conn_state
;
564 struct drm_crtc
*crtc
;
565 struct drm_crtc_state
*old_crtc_state
;
568 for_each_connector_in_state(old_state
, connector
, old_conn_state
, i
) {
569 const struct drm_encoder_helper_funcs
*funcs
;
570 struct drm_encoder
*encoder
;
571 struct drm_crtc_state
*old_crtc_state
;
573 /* Shut down everything that's in the changeset and currently
574 * still on. So need to check the old, saved state. */
575 if (!old_conn_state
->crtc
)
578 old_crtc_state
= old_state
->crtc_states
[drm_crtc_index(old_conn_state
->crtc
)];
580 if (!old_crtc_state
->active
||
581 !drm_atomic_crtc_needs_modeset(old_conn_state
->crtc
->state
))
584 encoder
= old_conn_state
->best_encoder
;
586 /* We shouldn't get this far if we didn't previously have
587 * an encoder.. but WARN_ON() rather than explode.
589 if (WARN_ON(!encoder
))
592 funcs
= encoder
->helper_private
;
594 DRM_DEBUG_ATOMIC("disabling [ENCODER:%d:%s]\n",
595 encoder
->base
.id
, encoder
->name
);
598 * Each encoder has at most one connector (since we always steal
599 * it away), so we won't call disable hooks twice.
601 drm_bridge_disable(encoder
->bridge
);
603 /* Right function depends upon target state. */
604 if (connector
->state
->crtc
&& funcs
->prepare
)
605 funcs
->prepare(encoder
);
606 else if (funcs
->disable
)
607 funcs
->disable(encoder
);
609 funcs
->dpms(encoder
, DRM_MODE_DPMS_OFF
);
611 drm_bridge_post_disable(encoder
->bridge
);
614 for_each_crtc_in_state(old_state
, crtc
, old_crtc_state
, i
) {
615 const struct drm_crtc_helper_funcs
*funcs
;
617 /* Shut down everything that needs a full modeset. */
618 if (!drm_atomic_crtc_needs_modeset(crtc
->state
))
621 if (!old_crtc_state
->active
)
624 funcs
= crtc
->helper_private
;
626 DRM_DEBUG_ATOMIC("disabling [CRTC:%d]\n",
630 /* Right function depends upon target state. */
631 if (crtc
->state
->enable
&& funcs
->prepare
)
632 funcs
->prepare(crtc
);
633 else if (funcs
->disable
)
634 funcs
->disable(crtc
);
636 funcs
->dpms(crtc
, DRM_MODE_DPMS_OFF
);
641 * drm_atomic_helper_update_legacy_modeset_state - update legacy modeset state
643 * @old_state: atomic state object with old state structures
645 * This function updates all the various legacy modeset state pointers in
646 * connectors, encoders and crtcs. It also updates the timestamping constants
647 * used for precise vblank timestamps by calling
648 * drm_calc_timestamping_constants().
650 * Drivers can use this for building their own atomic commit if they don't have
651 * a pure helper-based modeset implementation.
654 drm_atomic_helper_update_legacy_modeset_state(struct drm_device
*dev
,
655 struct drm_atomic_state
*old_state
)
657 struct drm_connector
*connector
;
658 struct drm_connector_state
*old_conn_state
;
659 struct drm_crtc
*crtc
;
660 struct drm_crtc_state
*old_crtc_state
;
663 /* clear out existing links and update dpms */
664 for_each_connector_in_state(old_state
, connector
, old_conn_state
, i
) {
665 if (connector
->encoder
) {
666 WARN_ON(!connector
->encoder
->crtc
);
668 connector
->encoder
->crtc
= NULL
;
669 connector
->encoder
= NULL
;
672 crtc
= connector
->state
->crtc
;
673 if ((!crtc
&& old_conn_state
->crtc
) ||
674 (crtc
&& drm_atomic_crtc_needs_modeset(crtc
->state
))) {
675 struct drm_property
*dpms_prop
=
676 dev
->mode_config
.dpms_property
;
677 int mode
= DRM_MODE_DPMS_OFF
;
679 if (crtc
&& crtc
->state
->active
)
680 mode
= DRM_MODE_DPMS_ON
;
682 connector
->dpms
= mode
;
683 drm_object_property_set_value(&connector
->base
,
689 for_each_connector_in_state(old_state
, connector
, old_conn_state
, i
) {
690 if (!connector
->state
->crtc
)
693 if (WARN_ON(!connector
->state
->best_encoder
))
696 connector
->encoder
= connector
->state
->best_encoder
;
697 connector
->encoder
->crtc
= connector
->state
->crtc
;
700 /* set legacy state in the crtc structure */
701 for_each_crtc_in_state(old_state
, crtc
, old_crtc_state
, i
) {
702 struct drm_plane
*primary
= crtc
->primary
;
704 crtc
->mode
= crtc
->state
->mode
;
705 crtc
->enabled
= crtc
->state
->enable
;
707 if (drm_atomic_get_existing_plane_state(old_state
, primary
) &&
708 primary
->state
->crtc
== crtc
) {
709 crtc
->x
= primary
->state
->src_x
>> 16;
710 crtc
->y
= primary
->state
->src_y
>> 16;
713 if (crtc
->state
->enable
)
714 drm_calc_timestamping_constants(crtc
,
715 &crtc
->state
->adjusted_mode
);
718 EXPORT_SYMBOL(drm_atomic_helper_update_legacy_modeset_state
);
721 crtc_set_mode(struct drm_device
*dev
, struct drm_atomic_state
*old_state
)
723 struct drm_crtc
*crtc
;
724 struct drm_crtc_state
*old_crtc_state
;
725 struct drm_connector
*connector
;
726 struct drm_connector_state
*old_conn_state
;
729 for_each_crtc_in_state(old_state
, crtc
, old_crtc_state
, i
) {
730 const struct drm_crtc_helper_funcs
*funcs
;
732 if (!crtc
->state
->mode_changed
)
735 funcs
= crtc
->helper_private
;
737 if (crtc
->state
->enable
&& funcs
->mode_set_nofb
) {
738 DRM_DEBUG_ATOMIC("modeset on [CRTC:%d]\n",
741 funcs
->mode_set_nofb(crtc
);
745 for_each_connector_in_state(old_state
, connector
, old_conn_state
, i
) {
746 const struct drm_encoder_helper_funcs
*funcs
;
747 struct drm_crtc_state
*new_crtc_state
;
748 struct drm_encoder
*encoder
;
749 struct drm_display_mode
*mode
, *adjusted_mode
;
751 if (!connector
->state
->best_encoder
)
754 encoder
= connector
->state
->best_encoder
;
755 funcs
= encoder
->helper_private
;
756 new_crtc_state
= connector
->state
->crtc
->state
;
757 mode
= &new_crtc_state
->mode
;
758 adjusted_mode
= &new_crtc_state
->adjusted_mode
;
760 if (!new_crtc_state
->mode_changed
)
763 DRM_DEBUG_ATOMIC("modeset on [ENCODER:%d:%s]\n",
764 encoder
->base
.id
, encoder
->name
);
767 * Each encoder has at most one connector (since we always steal
768 * it away), so we won't call mode_set hooks twice.
771 funcs
->mode_set(encoder
, mode
, adjusted_mode
);
773 drm_bridge_mode_set(encoder
->bridge
, mode
, adjusted_mode
);
778 * drm_atomic_helper_commit_modeset_disables - modeset commit to disable outputs
780 * @old_state: atomic state object with old state structures
782 * This function shuts down all the outputs that need to be shut down and
783 * prepares them (if required) with the new mode.
785 * For compatability with legacy crtc helpers this should be called before
786 * drm_atomic_helper_commit_planes(), which is what the default commit function
787 * does. But drivers with different needs can group the modeset commits together
788 * and do the plane commits at the end. This is useful for drivers doing runtime
789 * PM since planes updates then only happen when the CRTC is actually enabled.
791 void drm_atomic_helper_commit_modeset_disables(struct drm_device
*dev
,
792 struct drm_atomic_state
*old_state
)
794 disable_outputs(dev
, old_state
);
796 drm_atomic_helper_update_legacy_modeset_state(dev
, old_state
);
798 crtc_set_mode(dev
, old_state
);
800 EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_disables
);
803 * drm_atomic_helper_commit_modeset_enables - modeset commit to enable outputs
805 * @old_state: atomic state object with old state structures
807 * This function enables all the outputs with the new configuration which had to
808 * be turned off for the update.
810 * For compatability with legacy crtc helpers this should be called after
811 * drm_atomic_helper_commit_planes(), which is what the default commit function
812 * does. But drivers with different needs can group the modeset commits together
813 * and do the plane commits at the end. This is useful for drivers doing runtime
814 * PM since planes updates then only happen when the CRTC is actually enabled.
816 void drm_atomic_helper_commit_modeset_enables(struct drm_device
*dev
,
817 struct drm_atomic_state
*old_state
)
819 struct drm_crtc
*crtc
;
820 struct drm_crtc_state
*old_crtc_state
;
821 struct drm_connector
*connector
;
822 struct drm_connector_state
*old_conn_state
;
825 for_each_crtc_in_state(old_state
, crtc
, old_crtc_state
, i
) {
826 const struct drm_crtc_helper_funcs
*funcs
;
828 /* Need to filter out CRTCs where only planes change. */
829 if (!drm_atomic_crtc_needs_modeset(crtc
->state
))
832 if (!crtc
->state
->active
)
835 funcs
= crtc
->helper_private
;
837 if (crtc
->state
->enable
) {
838 DRM_DEBUG_ATOMIC("enabling [CRTC:%d]\n",
848 for_each_connector_in_state(old_state
, connector
, old_conn_state
, i
) {
849 const struct drm_encoder_helper_funcs
*funcs
;
850 struct drm_encoder
*encoder
;
852 if (!connector
->state
->best_encoder
)
855 if (!connector
->state
->crtc
->state
->active
||
856 !drm_atomic_crtc_needs_modeset(connector
->state
->crtc
->state
))
859 encoder
= connector
->state
->best_encoder
;
860 funcs
= encoder
->helper_private
;
862 DRM_DEBUG_ATOMIC("enabling [ENCODER:%d:%s]\n",
863 encoder
->base
.id
, encoder
->name
);
866 * Each encoder has at most one connector (since we always steal
867 * it away), so we won't call enable hooks twice.
869 drm_bridge_pre_enable(encoder
->bridge
);
872 funcs
->enable(encoder
);
874 funcs
->commit(encoder
);
876 drm_bridge_enable(encoder
->bridge
);
879 EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_enables
);
881 static void wait_for_fences(struct drm_device
*dev
,
882 struct drm_atomic_state
*state
)
884 struct drm_plane
*plane
;
885 struct drm_plane_state
*plane_state
;
888 for_each_plane_in_state(state
, plane
, plane_state
, i
) {
889 if (!plane
->state
->fence
)
892 WARN_ON(!plane
->state
->fb
);
894 fence_wait(plane
->state
->fence
, false);
895 fence_put(plane
->state
->fence
);
896 plane
->state
->fence
= NULL
;
900 static bool framebuffer_changed(struct drm_device
*dev
,
901 struct drm_atomic_state
*old_state
,
902 struct drm_crtc
*crtc
)
904 struct drm_plane
*plane
;
905 struct drm_plane_state
*old_plane_state
;
908 for_each_plane_in_state(old_state
, plane
, old_plane_state
, i
) {
909 if (plane
->state
->crtc
!= crtc
&&
910 old_plane_state
->crtc
!= crtc
)
913 if (plane
->state
->fb
!= old_plane_state
->fb
)
921 * drm_atomic_helper_wait_for_vblanks - wait for vblank on crtcs
923 * @old_state: atomic state object with old state structures
925 * Helper to, after atomic commit, wait for vblanks on all effected
926 * crtcs (ie. before cleaning up old framebuffers using
927 * drm_atomic_helper_cleanup_planes()). It will only wait on crtcs where the
928 * framebuffers have actually changed to optimize for the legacy cursor and
929 * plane update use-case.
932 drm_atomic_helper_wait_for_vblanks(struct drm_device
*dev
,
933 struct drm_atomic_state
*old_state
)
935 struct drm_crtc
*crtc
;
936 struct drm_crtc_state
*old_crtc_state
;
939 for_each_crtc_in_state(old_state
, crtc
, old_crtc_state
, i
) {
940 /* No one cares about the old state, so abuse it for tracking
941 * and store whether we hold a vblank reference (and should do a
942 * vblank wait) in the ->enable boolean. */
943 old_crtc_state
->enable
= false;
945 if (!crtc
->state
->enable
)
948 /* Legacy cursor ioctls are completely unsynced, and userspace
949 * relies on that (by doing tons of cursor updates). */
950 if (old_state
->legacy_cursor_update
)
953 if (!framebuffer_changed(dev
, old_state
, crtc
))
956 ret
= drm_crtc_vblank_get(crtc
);
960 old_crtc_state
->enable
= true;
961 old_crtc_state
->last_vblank_count
= drm_vblank_count(dev
, i
);
964 for_each_crtc_in_state(old_state
, crtc
, old_crtc_state
, i
) {
965 if (!old_crtc_state
->enable
)
968 ret
= wait_event_timeout(dev
->vblank
[i
].queue
,
969 old_crtc_state
->last_vblank_count
!=
970 drm_vblank_count(dev
, i
),
971 msecs_to_jiffies(50));
973 drm_crtc_vblank_put(crtc
);
976 EXPORT_SYMBOL(drm_atomic_helper_wait_for_vblanks
);
979 * drm_atomic_helper_commit - commit validated state object
981 * @state: the driver state object
982 * @async: asynchronous commit
984 * This function commits a with drm_atomic_helper_check() pre-validated state
985 * object. This can still fail when e.g. the framebuffer reservation fails. For
986 * now this doesn't implement asynchronous commits.
989 * Zero for success or -errno.
991 int drm_atomic_helper_commit(struct drm_device
*dev
,
992 struct drm_atomic_state
*state
,
1000 ret
= drm_atomic_helper_prepare_planes(dev
, state
);
1005 * This is the point of no return - everything below never fails except
1006 * when the hw goes bonghits. Which means we can commit the new state on
1007 * the software side now.
1010 drm_atomic_helper_swap_state(dev
, state
);
1013 * Everything below can be run asynchronously without the need to grab
1014 * any modeset locks at all under one condition: It must be guaranteed
1015 * that the asynchronous work has either been cancelled (if the driver
1016 * supports it, which at least requires that the framebuffers get
1017 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
1018 * before the new state gets committed on the software side with
1019 * drm_atomic_helper_swap_state().
1021 * This scheme allows new atomic state updates to be prepared and
1022 * checked in parallel to the asynchronous completion of the previous
1023 * update. Which is important since compositors need to figure out the
1024 * composition of the next frame right after having submitted the
1028 wait_for_fences(dev
, state
);
1030 drm_atomic_helper_commit_modeset_disables(dev
, state
);
1032 drm_atomic_helper_commit_planes(dev
, state
);
1034 drm_atomic_helper_commit_modeset_enables(dev
, state
);
1036 drm_atomic_helper_wait_for_vblanks(dev
, state
);
1038 drm_atomic_helper_cleanup_planes(dev
, state
);
1040 drm_atomic_state_free(state
);
1044 EXPORT_SYMBOL(drm_atomic_helper_commit
);
1047 * DOC: implementing async commit
1049 * For now the atomic helpers don't support async commit directly. If there is
1050 * real need it could be added though, using the dma-buf fence infrastructure
1051 * for generic synchronization with outstanding rendering.
1053 * For now drivers have to implement async commit themselves, with the following
1054 * sequence being the recommended one:
1056 * 1. Run drm_atomic_helper_prepare_planes() first. This is the only function
1057 * which commit needs to call which can fail, so we want to run it first and
1060 * 2. Synchronize with any outstanding asynchronous commit worker threads which
1061 * might be affected the new state update. This can be done by either cancelling
1062 * or flushing the work items, depending upon whether the driver can deal with
1063 * cancelled updates. Note that it is important to ensure that the framebuffer
1064 * cleanup is still done when cancelling.
1066 * For sufficient parallelism it is recommended to have a work item per crtc
1067 * (for updates which don't touch global state) and a global one. Then we only
1068 * need to synchronize with the crtc work items for changed crtcs and the global
1069 * work item, which allows nice concurrent updates on disjoint sets of crtcs.
1071 * 3. The software state is updated synchronously with
1072 * drm_atomic_helper_swap_state. Doing this under the protection of all modeset
1073 * locks means concurrent callers never see inconsistent state. And doing this
1074 * while it's guaranteed that no relevant async worker runs means that async
1075 * workers do not need grab any locks. Actually they must not grab locks, for
1076 * otherwise the work flushing will deadlock.
1078 * 4. Schedule a work item to do all subsequent steps, using the split-out
1079 * commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and
1080 * then cleaning up the framebuffers after the old framebuffer is no longer
1085 * drm_atomic_helper_prepare_planes - prepare plane resources before commit
1087 * @state: atomic state object with new state structures
1089 * This function prepares plane state, specifically framebuffers, for the new
1090 * configuration. If any failure is encountered this function will call
1091 * ->cleanup_fb on any already successfully prepared framebuffer.
1094 * 0 on success, negative error code on failure.
1096 int drm_atomic_helper_prepare_planes(struct drm_device
*dev
,
1097 struct drm_atomic_state
*state
)
1099 int nplanes
= dev
->mode_config
.num_total_plane
;
1102 for (i
= 0; i
< nplanes
; i
++) {
1103 const struct drm_plane_helper_funcs
*funcs
;
1104 struct drm_plane
*plane
= state
->planes
[i
];
1105 struct drm_plane_state
*plane_state
= state
->plane_states
[i
];
1106 struct drm_framebuffer
*fb
;
1111 funcs
= plane
->helper_private
;
1113 fb
= plane_state
->fb
;
1115 if (fb
&& funcs
->prepare_fb
) {
1116 ret
= funcs
->prepare_fb(plane
, fb
, plane_state
);
1125 for (i
--; i
>= 0; i
--) {
1126 const struct drm_plane_helper_funcs
*funcs
;
1127 struct drm_plane
*plane
= state
->planes
[i
];
1128 struct drm_plane_state
*plane_state
= state
->plane_states
[i
];
1129 struct drm_framebuffer
*fb
;
1134 funcs
= plane
->helper_private
;
1136 fb
= state
->plane_states
[i
]->fb
;
1138 if (fb
&& funcs
->cleanup_fb
)
1139 funcs
->cleanup_fb(plane
, fb
, plane_state
);
1145 EXPORT_SYMBOL(drm_atomic_helper_prepare_planes
);
1148 * drm_atomic_helper_commit_planes - commit plane state
1150 * @old_state: atomic state object with old state structures
1152 * This function commits the new plane state using the plane and atomic helper
1153 * functions for planes and crtcs. It assumes that the atomic state has already
1154 * been pushed into the relevant object state pointers, since this step can no
1157 * It still requires the global state object @old_state to know which planes and
1158 * crtcs need to be updated though.
1160 * Note that this function does all plane updates across all CRTCs in one step.
1161 * If the hardware can't support this approach look at
1162 * drm_atomic_helper_commit_planes_on_crtc() instead.
1164 void drm_atomic_helper_commit_planes(struct drm_device
*dev
,
1165 struct drm_atomic_state
*old_state
)
1167 struct drm_crtc
*crtc
;
1168 struct drm_crtc_state
*old_crtc_state
;
1169 struct drm_plane
*plane
;
1170 struct drm_plane_state
*old_plane_state
;
1173 for_each_crtc_in_state(old_state
, crtc
, old_crtc_state
, i
) {
1174 const struct drm_crtc_helper_funcs
*funcs
;
1176 funcs
= crtc
->helper_private
;
1178 if (!funcs
|| !funcs
->atomic_begin
)
1181 funcs
->atomic_begin(crtc
, old_crtc_state
);
1184 for_each_plane_in_state(old_state
, plane
, old_plane_state
, i
) {
1185 const struct drm_plane_helper_funcs
*funcs
;
1187 funcs
= plane
->helper_private
;
1193 * Special-case disabling the plane if drivers support it.
1195 if (drm_atomic_plane_disabling(plane
, old_plane_state
) &&
1196 funcs
->atomic_disable
)
1197 funcs
->atomic_disable(plane
, old_plane_state
);
1198 else if (plane
->state
->crtc
||
1199 drm_atomic_plane_disabling(plane
, old_plane_state
))
1200 funcs
->atomic_update(plane
, old_plane_state
);
1203 for_each_crtc_in_state(old_state
, crtc
, old_crtc_state
, i
) {
1204 const struct drm_crtc_helper_funcs
*funcs
;
1206 funcs
= crtc
->helper_private
;
1208 if (!funcs
|| !funcs
->atomic_flush
)
1211 funcs
->atomic_flush(crtc
, old_crtc_state
);
1214 EXPORT_SYMBOL(drm_atomic_helper_commit_planes
);
1217 * drm_atomic_helper_commit_planes_on_crtc - commit plane state for a crtc
1218 * @old_crtc_state: atomic state object with the old crtc state
1220 * This function commits the new plane state using the plane and atomic helper
1221 * functions for planes on the specific crtc. It assumes that the atomic state
1222 * has already been pushed into the relevant object state pointers, since this
1223 * step can no longer fail.
1225 * This function is useful when plane updates should be done crtc-by-crtc
1226 * instead of one global step like drm_atomic_helper_commit_planes() does.
1228 * This function can only be savely used when planes are not allowed to move
1229 * between different CRTCs because this function doesn't handle inter-CRTC
1230 * depencies. Callers need to ensure that either no such depencies exist,
1231 * resolve them through ordering of commit calls or through some other means.
1234 drm_atomic_helper_commit_planes_on_crtc(struct drm_crtc_state
*old_crtc_state
)
1236 const struct drm_crtc_helper_funcs
*crtc_funcs
;
1237 struct drm_crtc
*crtc
= old_crtc_state
->crtc
;
1238 struct drm_atomic_state
*old_state
= old_crtc_state
->state
;
1239 struct drm_plane
*plane
;
1240 unsigned plane_mask
;
1242 plane_mask
= old_crtc_state
->plane_mask
;
1243 plane_mask
|= crtc
->state
->plane_mask
;
1245 crtc_funcs
= crtc
->helper_private
;
1246 if (crtc_funcs
&& crtc_funcs
->atomic_begin
)
1247 crtc_funcs
->atomic_begin(crtc
, old_crtc_state
);
1249 drm_for_each_plane_mask(plane
, crtc
->dev
, plane_mask
) {
1250 struct drm_plane_state
*old_plane_state
=
1251 drm_atomic_get_existing_plane_state(old_state
, plane
);
1252 const struct drm_plane_helper_funcs
*plane_funcs
;
1254 plane_funcs
= plane
->helper_private
;
1256 if (!old_plane_state
|| !plane_funcs
)
1259 WARN_ON(plane
->state
->crtc
&& plane
->state
->crtc
!= crtc
);
1261 if (drm_atomic_plane_disabling(plane
, old_plane_state
) &&
1262 plane_funcs
->atomic_disable
)
1263 plane_funcs
->atomic_disable(plane
, old_plane_state
);
1264 else if (plane
->state
->crtc
||
1265 drm_atomic_plane_disabling(plane
, old_plane_state
))
1266 plane_funcs
->atomic_update(plane
, old_plane_state
);
1269 if (crtc_funcs
&& crtc_funcs
->atomic_flush
)
1270 crtc_funcs
->atomic_flush(crtc
, old_crtc_state
);
1272 EXPORT_SYMBOL(drm_atomic_helper_commit_planes_on_crtc
);
1275 * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
1277 * @old_state: atomic state object with old state structures
1279 * This function cleans up plane state, specifically framebuffers, from the old
1280 * configuration. Hence the old configuration must be perserved in @old_state to
1281 * be able to call this function.
1283 * This function must also be called on the new state when the atomic update
1284 * fails at any point after calling drm_atomic_helper_prepare_planes().
1286 void drm_atomic_helper_cleanup_planes(struct drm_device
*dev
,
1287 struct drm_atomic_state
*old_state
)
1289 struct drm_plane
*plane
;
1290 struct drm_plane_state
*plane_state
;
1293 for_each_plane_in_state(old_state
, plane
, plane_state
, i
) {
1294 const struct drm_plane_helper_funcs
*funcs
;
1295 struct drm_framebuffer
*old_fb
;
1297 funcs
= plane
->helper_private
;
1299 old_fb
= plane_state
->fb
;
1301 if (old_fb
&& funcs
->cleanup_fb
)
1302 funcs
->cleanup_fb(plane
, old_fb
, plane_state
);
1305 EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes
);
1308 * drm_atomic_helper_swap_state - store atomic state into current sw state
1310 * @state: atomic state
1312 * This function stores the atomic state into the current state pointers in all
1313 * driver objects. It should be called after all failing steps have been done
1314 * and succeeded, but before the actual hardware state is committed.
1316 * For cleanup and error recovery the current state for all changed objects will
1317 * be swaped into @state.
1319 * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
1321 * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
1323 * 2. Do any other steps that might fail.
1325 * 3. Put the staged state into the current state pointers with this function.
1327 * 4. Actually commit the hardware state.
1329 * 5. Call drm_atomic_helper_cleanup_planes with @state, which since step 3
1330 * contains the old state. Also do any other cleanup required with that state.
1332 void drm_atomic_helper_swap_state(struct drm_device
*dev
,
1333 struct drm_atomic_state
*state
)
1337 for (i
= 0; i
< dev
->mode_config
.num_connector
; i
++) {
1338 struct drm_connector
*connector
= state
->connectors
[i
];
1343 connector
->state
->state
= state
;
1344 swap(state
->connector_states
[i
], connector
->state
);
1345 connector
->state
->state
= NULL
;
1348 for (i
= 0; i
< dev
->mode_config
.num_crtc
; i
++) {
1349 struct drm_crtc
*crtc
= state
->crtcs
[i
];
1354 crtc
->state
->state
= state
;
1355 swap(state
->crtc_states
[i
], crtc
->state
);
1356 crtc
->state
->state
= NULL
;
1359 for (i
= 0; i
< dev
->mode_config
.num_total_plane
; i
++) {
1360 struct drm_plane
*plane
= state
->planes
[i
];
1365 plane
->state
->state
= state
;
1366 swap(state
->plane_states
[i
], plane
->state
);
1367 plane
->state
->state
= NULL
;
1370 EXPORT_SYMBOL(drm_atomic_helper_swap_state
);
1373 * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
1374 * @plane: plane object to update
1375 * @crtc: owning CRTC of owning plane
1376 * @fb: framebuffer to flip onto plane
1377 * @crtc_x: x offset of primary plane on crtc
1378 * @crtc_y: y offset of primary plane on crtc
1379 * @crtc_w: width of primary plane rectangle on crtc
1380 * @crtc_h: height of primary plane rectangle on crtc
1381 * @src_x: x offset of @fb for panning
1382 * @src_y: y offset of @fb for panning
1383 * @src_w: width of source rectangle in @fb
1384 * @src_h: height of source rectangle in @fb
1386 * Provides a default plane update handler using the atomic driver interface.
1389 * Zero on success, error code on failure
1391 int drm_atomic_helper_update_plane(struct drm_plane
*plane
,
1392 struct drm_crtc
*crtc
,
1393 struct drm_framebuffer
*fb
,
1394 int crtc_x
, int crtc_y
,
1395 unsigned int crtc_w
, unsigned int crtc_h
,
1396 uint32_t src_x
, uint32_t src_y
,
1397 uint32_t src_w
, uint32_t src_h
)
1399 struct drm_atomic_state
*state
;
1400 struct drm_plane_state
*plane_state
;
1403 state
= drm_atomic_state_alloc(plane
->dev
);
1407 state
->acquire_ctx
= drm_modeset_legacy_acquire_ctx(crtc
);
1409 plane_state
= drm_atomic_get_plane_state(state
, plane
);
1410 if (IS_ERR(plane_state
)) {
1411 ret
= PTR_ERR(plane_state
);
1415 ret
= drm_atomic_set_crtc_for_plane(plane_state
, crtc
);
1418 drm_atomic_set_fb_for_plane(plane_state
, fb
);
1419 plane_state
->crtc_x
= crtc_x
;
1420 plane_state
->crtc_y
= crtc_y
;
1421 plane_state
->crtc_h
= crtc_h
;
1422 plane_state
->crtc_w
= crtc_w
;
1423 plane_state
->src_x
= src_x
;
1424 plane_state
->src_y
= src_y
;
1425 plane_state
->src_h
= src_h
;
1426 plane_state
->src_w
= src_w
;
1428 if (plane
== crtc
->cursor
)
1429 state
->legacy_cursor_update
= true;
1431 ret
= drm_atomic_commit(state
);
1435 /* Driver takes ownership of state on successful commit. */
1438 if (ret
== -EDEADLK
)
1441 drm_atomic_state_free(state
);
1445 drm_atomic_state_clear(state
);
1446 drm_atomic_legacy_backoff(state
);
1449 * Someone might have exchanged the framebuffer while we dropped locks
1450 * in the backoff code. We need to fix up the fb refcount tracking the
1453 plane
->old_fb
= plane
->fb
;
1457 EXPORT_SYMBOL(drm_atomic_helper_update_plane
);
1460 * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
1461 * @plane: plane to disable
1463 * Provides a default plane disable handler using the atomic driver interface.
1466 * Zero on success, error code on failure
1468 int drm_atomic_helper_disable_plane(struct drm_plane
*plane
)
1470 struct drm_atomic_state
*state
;
1471 struct drm_plane_state
*plane_state
;
1475 * FIXME: Without plane->crtc set we can't get at the implicit legacy
1476 * acquire context. The real fix will be to wire the acquire ctx through
1477 * everywhere we need it, but meanwhile prevent chaos by just skipping
1478 * this noop. The critical case is the cursor ioctls which a) only grab
1479 * crtc/cursor-plane locks (so we need the crtc to get at the right
1480 * acquire context) and b) can try to disable the plane multiple times.
1485 state
= drm_atomic_state_alloc(plane
->dev
);
1489 state
->acquire_ctx
= drm_modeset_legacy_acquire_ctx(plane
->crtc
);
1491 plane_state
= drm_atomic_get_plane_state(state
, plane
);
1492 if (IS_ERR(plane_state
)) {
1493 ret
= PTR_ERR(plane_state
);
1497 ret
= drm_atomic_set_crtc_for_plane(plane_state
, NULL
);
1500 drm_atomic_set_fb_for_plane(plane_state
, NULL
);
1501 plane_state
->crtc_x
= 0;
1502 plane_state
->crtc_y
= 0;
1503 plane_state
->crtc_h
= 0;
1504 plane_state
->crtc_w
= 0;
1505 plane_state
->src_x
= 0;
1506 plane_state
->src_y
= 0;
1507 plane_state
->src_h
= 0;
1508 plane_state
->src_w
= 0;
1510 if (plane
== plane
->crtc
->cursor
)
1511 state
->legacy_cursor_update
= true;
1513 ret
= drm_atomic_commit(state
);
1517 /* Driver takes ownership of state on successful commit. */
1520 if (ret
== -EDEADLK
)
1523 drm_atomic_state_free(state
);
1527 drm_atomic_state_clear(state
);
1528 drm_atomic_legacy_backoff(state
);
1531 * Someone might have exchanged the framebuffer while we dropped locks
1532 * in the backoff code. We need to fix up the fb refcount tracking the
1535 plane
->old_fb
= plane
->fb
;
1539 EXPORT_SYMBOL(drm_atomic_helper_disable_plane
);
1541 static int update_output_state(struct drm_atomic_state
*state
,
1542 struct drm_mode_set
*set
)
1544 struct drm_device
*dev
= set
->crtc
->dev
;
1545 struct drm_crtc
*crtc
;
1546 struct drm_crtc_state
*crtc_state
;
1547 struct drm_connector
*connector
;
1548 struct drm_connector_state
*conn_state
;
1551 ret
= drm_modeset_lock(&dev
->mode_config
.connection_mutex
,
1552 state
->acquire_ctx
);
1556 /* First grab all affected connector/crtc states. */
1557 for (i
= 0; i
< set
->num_connectors
; i
++) {
1558 conn_state
= drm_atomic_get_connector_state(state
,
1559 set
->connectors
[i
]);
1560 if (IS_ERR(conn_state
))
1561 return PTR_ERR(conn_state
);
1564 for_each_crtc_in_state(state
, crtc
, crtc_state
, i
) {
1565 ret
= drm_atomic_add_affected_connectors(state
, crtc
);
1570 /* Then recompute connector->crtc links and crtc enabling state. */
1571 for_each_connector_in_state(state
, connector
, conn_state
, i
) {
1572 if (conn_state
->crtc
== set
->crtc
) {
1573 ret
= drm_atomic_set_crtc_for_connector(conn_state
,
1579 for (j
= 0; j
< set
->num_connectors
; j
++) {
1580 if (set
->connectors
[j
] == connector
) {
1581 ret
= drm_atomic_set_crtc_for_connector(conn_state
,
1590 for_each_crtc_in_state(state
, crtc
, crtc_state
, i
) {
1591 /* Don't update ->enable for the CRTC in the set_config request,
1592 * since a mismatch would indicate a bug in the upper layers.
1593 * The actual modeset code later on will catch any
1594 * inconsistencies here. */
1595 if (crtc
== set
->crtc
)
1598 if (!drm_atomic_connectors_for_crtc(state
, crtc
)) {
1599 ret
= drm_atomic_set_mode_prop_for_crtc(crtc_state
,
1604 crtc_state
->active
= false;
1612 * drm_atomic_helper_set_config - set a new config from userspace
1613 * @set: mode set configuration
1615 * Provides a default crtc set_config handler using the atomic driver interface.
1618 * Returns 0 on success, negative errno numbers on failure.
1620 int drm_atomic_helper_set_config(struct drm_mode_set
*set
)
1622 struct drm_atomic_state
*state
;
1623 struct drm_crtc
*crtc
= set
->crtc
;
1624 struct drm_crtc_state
*crtc_state
;
1625 struct drm_plane_state
*primary_state
;
1628 state
= drm_atomic_state_alloc(crtc
->dev
);
1632 state
->acquire_ctx
= drm_modeset_legacy_acquire_ctx(crtc
);
1634 crtc_state
= drm_atomic_get_crtc_state(state
, crtc
);
1635 if (IS_ERR(crtc_state
)) {
1636 ret
= PTR_ERR(crtc_state
);
1640 primary_state
= drm_atomic_get_plane_state(state
, crtc
->primary
);
1641 if (IS_ERR(primary_state
)) {
1642 ret
= PTR_ERR(primary_state
);
1648 WARN_ON(set
->num_connectors
);
1650 ret
= drm_atomic_set_mode_for_crtc(crtc_state
, NULL
);
1654 crtc_state
->active
= false;
1656 ret
= drm_atomic_set_crtc_for_plane(primary_state
, NULL
);
1660 drm_atomic_set_fb_for_plane(primary_state
, NULL
);
1666 WARN_ON(!set
->num_connectors
);
1668 ret
= drm_atomic_set_mode_for_crtc(crtc_state
, set
->mode
);
1672 crtc_state
->active
= true;
1674 ret
= drm_atomic_set_crtc_for_plane(primary_state
, crtc
);
1677 drm_atomic_set_fb_for_plane(primary_state
, set
->fb
);
1678 primary_state
->crtc_x
= 0;
1679 primary_state
->crtc_y
= 0;
1680 primary_state
->crtc_h
= set
->mode
->vdisplay
;
1681 primary_state
->crtc_w
= set
->mode
->hdisplay
;
1682 primary_state
->src_x
= set
->x
<< 16;
1683 primary_state
->src_y
= set
->y
<< 16;
1684 primary_state
->src_h
= set
->mode
->vdisplay
<< 16;
1685 primary_state
->src_w
= set
->mode
->hdisplay
<< 16;
1688 ret
= update_output_state(state
, set
);
1692 ret
= drm_atomic_commit(state
);
1696 /* Driver takes ownership of state on successful commit. */
1699 if (ret
== -EDEADLK
)
1702 drm_atomic_state_free(state
);
1706 drm_atomic_state_clear(state
);
1707 drm_atomic_legacy_backoff(state
);
1710 * Someone might have exchanged the framebuffer while we dropped locks
1711 * in the backoff code. We need to fix up the fb refcount tracking the
1714 crtc
->primary
->old_fb
= crtc
->primary
->fb
;
1718 EXPORT_SYMBOL(drm_atomic_helper_set_config
);
1721 * drm_atomic_helper_crtc_set_property - helper for crtc properties
1723 * @property: DRM property
1724 * @val: value of property
1726 * Provides a default crtc set_property handler using the atomic driver
1730 * Zero on success, error code on failure
1733 drm_atomic_helper_crtc_set_property(struct drm_crtc
*crtc
,
1734 struct drm_property
*property
,
1737 struct drm_atomic_state
*state
;
1738 struct drm_crtc_state
*crtc_state
;
1741 state
= drm_atomic_state_alloc(crtc
->dev
);
1745 /* ->set_property is always called with all locks held. */
1746 state
->acquire_ctx
= crtc
->dev
->mode_config
.acquire_ctx
;
1748 crtc_state
= drm_atomic_get_crtc_state(state
, crtc
);
1749 if (IS_ERR(crtc_state
)) {
1750 ret
= PTR_ERR(crtc_state
);
1754 ret
= drm_atomic_crtc_set_property(crtc
, crtc_state
,
1759 ret
= drm_atomic_commit(state
);
1763 /* Driver takes ownership of state on successful commit. */
1766 if (ret
== -EDEADLK
)
1769 drm_atomic_state_free(state
);
1773 drm_atomic_state_clear(state
);
1774 drm_atomic_legacy_backoff(state
);
1778 EXPORT_SYMBOL(drm_atomic_helper_crtc_set_property
);
1781 * drm_atomic_helper_plane_set_property - helper for plane properties
1783 * @property: DRM property
1784 * @val: value of property
1786 * Provides a default plane set_property handler using the atomic driver
1790 * Zero on success, error code on failure
1793 drm_atomic_helper_plane_set_property(struct drm_plane
*plane
,
1794 struct drm_property
*property
,
1797 struct drm_atomic_state
*state
;
1798 struct drm_plane_state
*plane_state
;
1801 state
= drm_atomic_state_alloc(plane
->dev
);
1805 /* ->set_property is always called with all locks held. */
1806 state
->acquire_ctx
= plane
->dev
->mode_config
.acquire_ctx
;
1808 plane_state
= drm_atomic_get_plane_state(state
, plane
);
1809 if (IS_ERR(plane_state
)) {
1810 ret
= PTR_ERR(plane_state
);
1814 ret
= drm_atomic_plane_set_property(plane
, plane_state
,
1819 ret
= drm_atomic_commit(state
);
1823 /* Driver takes ownership of state on successful commit. */
1826 if (ret
== -EDEADLK
)
1829 drm_atomic_state_free(state
);
1833 drm_atomic_state_clear(state
);
1834 drm_atomic_legacy_backoff(state
);
1838 EXPORT_SYMBOL(drm_atomic_helper_plane_set_property
);
1841 * drm_atomic_helper_connector_set_property - helper for connector properties
1842 * @connector: DRM connector
1843 * @property: DRM property
1844 * @val: value of property
1846 * Provides a default connector set_property handler using the atomic driver
1850 * Zero on success, error code on failure
1853 drm_atomic_helper_connector_set_property(struct drm_connector
*connector
,
1854 struct drm_property
*property
,
1857 struct drm_atomic_state
*state
;
1858 struct drm_connector_state
*connector_state
;
1861 state
= drm_atomic_state_alloc(connector
->dev
);
1865 /* ->set_property is always called with all locks held. */
1866 state
->acquire_ctx
= connector
->dev
->mode_config
.acquire_ctx
;
1868 connector_state
= drm_atomic_get_connector_state(state
, connector
);
1869 if (IS_ERR(connector_state
)) {
1870 ret
= PTR_ERR(connector_state
);
1874 ret
= drm_atomic_connector_set_property(connector
, connector_state
,
1879 ret
= drm_atomic_commit(state
);
1883 /* Driver takes ownership of state on successful commit. */
1886 if (ret
== -EDEADLK
)
1889 drm_atomic_state_free(state
);
1893 drm_atomic_state_clear(state
);
1894 drm_atomic_legacy_backoff(state
);
1898 EXPORT_SYMBOL(drm_atomic_helper_connector_set_property
);
1901 * drm_atomic_helper_page_flip - execute a legacy page flip
1903 * @fb: DRM framebuffer
1904 * @event: optional DRM event to signal upon completion
1905 * @flags: flip flags for non-vblank sync'ed updates
1907 * Provides a default page flip implementation using the atomic driver interface.
1909 * Note that for now so called async page flips (i.e. updates which are not
1910 * synchronized to vblank) are not supported, since the atomic interfaces have
1911 * no provisions for this yet.
1914 * Returns 0 on success, negative errno numbers on failure.
1916 int drm_atomic_helper_page_flip(struct drm_crtc
*crtc
,
1917 struct drm_framebuffer
*fb
,
1918 struct drm_pending_vblank_event
*event
,
1921 struct drm_plane
*plane
= crtc
->primary
;
1922 struct drm_atomic_state
*state
;
1923 struct drm_plane_state
*plane_state
;
1924 struct drm_crtc_state
*crtc_state
;
1927 if (flags
& DRM_MODE_PAGE_FLIP_ASYNC
)
1930 state
= drm_atomic_state_alloc(plane
->dev
);
1934 state
->acquire_ctx
= drm_modeset_legacy_acquire_ctx(crtc
);
1936 crtc_state
= drm_atomic_get_crtc_state(state
, crtc
);
1937 if (IS_ERR(crtc_state
)) {
1938 ret
= PTR_ERR(crtc_state
);
1941 crtc_state
->event
= event
;
1943 plane_state
= drm_atomic_get_plane_state(state
, plane
);
1944 if (IS_ERR(plane_state
)) {
1945 ret
= PTR_ERR(plane_state
);
1949 ret
= drm_atomic_set_crtc_for_plane(plane_state
, crtc
);
1952 drm_atomic_set_fb_for_plane(plane_state
, fb
);
1954 ret
= drm_atomic_async_commit(state
);
1958 /* Driver takes ownership of state on successful async commit. */
1961 if (ret
== -EDEADLK
)
1964 drm_atomic_state_free(state
);
1968 drm_atomic_state_clear(state
);
1969 drm_atomic_legacy_backoff(state
);
1972 * Someone might have exchanged the framebuffer while we dropped locks
1973 * in the backoff code. We need to fix up the fb refcount tracking the
1976 plane
->old_fb
= plane
->fb
;
1980 EXPORT_SYMBOL(drm_atomic_helper_page_flip
);
1983 * drm_atomic_helper_connector_dpms() - connector dpms helper implementation
1984 * @connector: affected connector
1987 * This is the main helper function provided by the atomic helper framework for
1988 * implementing the legacy DPMS connector interface. It computes the new desired
1989 * ->active state for the corresponding CRTC (if the connector is enabled) and
1993 * Returns 0 on success, negative errno numbers on failure.
1995 int drm_atomic_helper_connector_dpms(struct drm_connector
*connector
,
1998 struct drm_mode_config
*config
= &connector
->dev
->mode_config
;
1999 struct drm_atomic_state
*state
;
2000 struct drm_crtc_state
*crtc_state
;
2001 struct drm_crtc
*crtc
;
2002 struct drm_connector
*tmp_connector
;
2004 bool active
= false;
2005 int old_mode
= connector
->dpms
;
2007 if (mode
!= DRM_MODE_DPMS_ON
)
2008 mode
= DRM_MODE_DPMS_OFF
;
2010 connector
->dpms
= mode
;
2011 crtc
= connector
->state
->crtc
;
2016 state
= drm_atomic_state_alloc(connector
->dev
);
2020 state
->acquire_ctx
= drm_modeset_legacy_acquire_ctx(crtc
);
2022 crtc_state
= drm_atomic_get_crtc_state(state
, crtc
);
2023 if (IS_ERR(crtc_state
)) {
2024 ret
= PTR_ERR(crtc_state
);
2028 WARN_ON(!drm_modeset_is_locked(&config
->connection_mutex
));
2030 drm_for_each_connector(tmp_connector
, connector
->dev
) {
2031 if (tmp_connector
->state
->crtc
!= crtc
)
2034 if (tmp_connector
->dpms
== DRM_MODE_DPMS_ON
) {
2039 crtc_state
->active
= active
;
2041 ret
= drm_atomic_commit(state
);
2045 /* Driver takes ownership of state on successful commit. */
2048 if (ret
== -EDEADLK
)
2051 connector
->dpms
= old_mode
;
2052 drm_atomic_state_free(state
);
2056 drm_atomic_state_clear(state
);
2057 drm_atomic_legacy_backoff(state
);
2061 EXPORT_SYMBOL(drm_atomic_helper_connector_dpms
);
2064 * DOC: atomic state reset and initialization
2066 * Both the drm core and the atomic helpers assume that there is always the full
2067 * and correct atomic software state for all connectors, CRTCs and planes
2068 * available. Which is a bit a problem on driver load and also after system
2069 * suspend. One way to solve this is to have a hardware state read-out
2070 * infrastructure which reconstructs the full software state (e.g. the i915
2073 * The simpler solution is to just reset the software state to everything off,
2074 * which is easiest to do by calling drm_mode_config_reset(). To facilitate this
2075 * the atomic helpers provide default reset implementations for all hooks.
2079 * drm_atomic_helper_crtc_reset - default ->reset hook for CRTCs
2082 * Resets the atomic state for @crtc by freeing the state pointer (which might
2083 * be NULL, e.g. at driver load time) and allocating a new empty state object.
2085 void drm_atomic_helper_crtc_reset(struct drm_crtc
*crtc
)
2087 if (crtc
->state
&& crtc
->state
->mode_blob
)
2088 drm_property_unreference_blob(crtc
->state
->mode_blob
);
2090 crtc
->state
= kzalloc(sizeof(*crtc
->state
), GFP_KERNEL
);
2093 crtc
->state
->crtc
= crtc
;
2095 EXPORT_SYMBOL(drm_atomic_helper_crtc_reset
);
2098 * __drm_atomic_helper_crtc_duplicate_state - copy atomic CRTC state
2099 * @crtc: CRTC object
2100 * @state: atomic CRTC state
2102 * Copies atomic state from a CRTC's current state and resets inferred values.
2103 * This is useful for drivers that subclass the CRTC state.
2105 void __drm_atomic_helper_crtc_duplicate_state(struct drm_crtc
*crtc
,
2106 struct drm_crtc_state
*state
)
2108 memcpy(state
, crtc
->state
, sizeof(*state
));
2110 if (state
->mode_blob
)
2111 drm_property_reference_blob(state
->mode_blob
);
2112 state
->mode_changed
= false;
2113 state
->active_changed
= false;
2114 state
->planes_changed
= false;
2115 state
->connectors_changed
= false;
2116 state
->event
= NULL
;
2118 EXPORT_SYMBOL(__drm_atomic_helper_crtc_duplicate_state
);
2121 * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
2124 * Default CRTC state duplicate hook for drivers which don't have their own
2125 * subclassed CRTC state structure.
2127 struct drm_crtc_state
*
2128 drm_atomic_helper_crtc_duplicate_state(struct drm_crtc
*crtc
)
2130 struct drm_crtc_state
*state
;
2132 if (WARN_ON(!crtc
->state
))
2135 state
= kmalloc(sizeof(*state
), GFP_KERNEL
);
2137 __drm_atomic_helper_crtc_duplicate_state(crtc
, state
);
2141 EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state
);
2144 * __drm_atomic_helper_crtc_destroy_state - release CRTC state
2145 * @crtc: CRTC object
2146 * @state: CRTC state object to release
2148 * Releases all resources stored in the CRTC state without actually freeing
2149 * the memory of the CRTC state. This is useful for drivers that subclass the
2152 void __drm_atomic_helper_crtc_destroy_state(struct drm_crtc
*crtc
,
2153 struct drm_crtc_state
*state
)
2155 if (state
->mode_blob
)
2156 drm_property_unreference_blob(state
->mode_blob
);
2158 EXPORT_SYMBOL(__drm_atomic_helper_crtc_destroy_state
);
2161 * drm_atomic_helper_crtc_destroy_state - default state destroy hook
2163 * @state: CRTC state object to release
2165 * Default CRTC state destroy hook for drivers which don't have their own
2166 * subclassed CRTC state structure.
2168 void drm_atomic_helper_crtc_destroy_state(struct drm_crtc
*crtc
,
2169 struct drm_crtc_state
*state
)
2171 __drm_atomic_helper_crtc_destroy_state(crtc
, state
);
2174 EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state
);
2177 * drm_atomic_helper_plane_reset - default ->reset hook for planes
2180 * Resets the atomic state for @plane by freeing the state pointer (which might
2181 * be NULL, e.g. at driver load time) and allocating a new empty state object.
2183 void drm_atomic_helper_plane_reset(struct drm_plane
*plane
)
2185 if (plane
->state
&& plane
->state
->fb
)
2186 drm_framebuffer_unreference(plane
->state
->fb
);
2188 kfree(plane
->state
);
2189 plane
->state
= kzalloc(sizeof(*plane
->state
), GFP_KERNEL
);
2192 plane
->state
->plane
= plane
;
2194 EXPORT_SYMBOL(drm_atomic_helper_plane_reset
);
2197 * __drm_atomic_helper_plane_duplicate_state - copy atomic plane state
2198 * @plane: plane object
2199 * @state: atomic plane state
2201 * Copies atomic state from a plane's current state. This is useful for
2202 * drivers that subclass the plane state.
2204 void __drm_atomic_helper_plane_duplicate_state(struct drm_plane
*plane
,
2205 struct drm_plane_state
*state
)
2207 memcpy(state
, plane
->state
, sizeof(*state
));
2210 drm_framebuffer_reference(state
->fb
);
2212 EXPORT_SYMBOL(__drm_atomic_helper_plane_duplicate_state
);
2215 * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
2218 * Default plane state duplicate hook for drivers which don't have their own
2219 * subclassed plane state structure.
2221 struct drm_plane_state
*
2222 drm_atomic_helper_plane_duplicate_state(struct drm_plane
*plane
)
2224 struct drm_plane_state
*state
;
2226 if (WARN_ON(!plane
->state
))
2229 state
= kmalloc(sizeof(*state
), GFP_KERNEL
);
2231 __drm_atomic_helper_plane_duplicate_state(plane
, state
);
2235 EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state
);
2238 * __drm_atomic_helper_plane_destroy_state - release plane state
2239 * @plane: plane object
2240 * @state: plane state object to release
2242 * Releases all resources stored in the plane state without actually freeing
2243 * the memory of the plane state. This is useful for drivers that subclass the
2246 void __drm_atomic_helper_plane_destroy_state(struct drm_plane
*plane
,
2247 struct drm_plane_state
*state
)
2250 drm_framebuffer_unreference(state
->fb
);
2252 EXPORT_SYMBOL(__drm_atomic_helper_plane_destroy_state
);
2255 * drm_atomic_helper_plane_destroy_state - default state destroy hook
2257 * @state: plane state object to release
2259 * Default plane state destroy hook for drivers which don't have their own
2260 * subclassed plane state structure.
2262 void drm_atomic_helper_plane_destroy_state(struct drm_plane
*plane
,
2263 struct drm_plane_state
*state
)
2265 __drm_atomic_helper_plane_destroy_state(plane
, state
);
2268 EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state
);
2271 * drm_atomic_helper_connector_reset - default ->reset hook for connectors
2272 * @connector: drm connector
2274 * Resets the atomic state for @connector by freeing the state pointer (which
2275 * might be NULL, e.g. at driver load time) and allocating a new empty state
2278 void drm_atomic_helper_connector_reset(struct drm_connector
*connector
)
2280 kfree(connector
->state
);
2281 connector
->state
= kzalloc(sizeof(*connector
->state
), GFP_KERNEL
);
2283 if (connector
->state
)
2284 connector
->state
->connector
= connector
;
2286 EXPORT_SYMBOL(drm_atomic_helper_connector_reset
);
2289 * __drm_atomic_helper_connector_duplicate_state - copy atomic connector state
2290 * @connector: connector object
2291 * @state: atomic connector state
2293 * Copies atomic state from a connector's current state. This is useful for
2294 * drivers that subclass the connector state.
2297 __drm_atomic_helper_connector_duplicate_state(struct drm_connector
*connector
,
2298 struct drm_connector_state
*state
)
2300 memcpy(state
, connector
->state
, sizeof(*state
));
2302 EXPORT_SYMBOL(__drm_atomic_helper_connector_duplicate_state
);
2305 * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
2306 * @connector: drm connector
2308 * Default connector state duplicate hook for drivers which don't have their own
2309 * subclassed connector state structure.
2311 struct drm_connector_state
*
2312 drm_atomic_helper_connector_duplicate_state(struct drm_connector
*connector
)
2314 struct drm_connector_state
*state
;
2316 if (WARN_ON(!connector
->state
))
2319 state
= kmalloc(sizeof(*state
), GFP_KERNEL
);
2321 __drm_atomic_helper_connector_duplicate_state(connector
, state
);
2325 EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state
);
2328 * __drm_atomic_helper_connector_destroy_state - release connector state
2329 * @connector: connector object
2330 * @state: connector state object to release
2332 * Releases all resources stored in the connector state without actually
2333 * freeing the memory of the connector state. This is useful for drivers that
2334 * subclass the connector state.
2337 __drm_atomic_helper_connector_destroy_state(struct drm_connector
*connector
,
2338 struct drm_connector_state
*state
)
2341 * This is currently a placeholder so that drivers that subclass the
2342 * state will automatically do the right thing if code is ever added
2346 EXPORT_SYMBOL(__drm_atomic_helper_connector_destroy_state
);
2349 * drm_atomic_helper_connector_destroy_state - default state destroy hook
2350 * @connector: drm connector
2351 * @state: connector state object to release
2353 * Default connector state destroy hook for drivers which don't have their own
2354 * subclassed connector state structure.
2356 void drm_atomic_helper_connector_destroy_state(struct drm_connector
*connector
,
2357 struct drm_connector_state
*state
)
2359 __drm_atomic_helper_connector_destroy_state(connector
, state
);
2362 EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state
);