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
47 * exposed 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.
56 * The atomic helper uses the same function table structures as all other
57 * modesetting helpers. See the documentation for struct &drm_crtc_helper_funcs,
58 * struct &drm_encoder_helper_funcs and struct &drm_connector_helper_funcs. It
59 * also shares the struct &drm_plane_helper_funcs function table with the plane
63 drm_atomic_helper_plane_changed(struct drm_atomic_state
*state
,
64 struct drm_plane_state
*plane_state
,
65 struct drm_plane
*plane
)
67 struct drm_crtc_state
*crtc_state
;
69 if (plane
->state
->crtc
) {
70 crtc_state
= state
->crtc_states
[drm_crtc_index(plane
->state
->crtc
)];
72 if (WARN_ON(!crtc_state
))
75 crtc_state
->planes_changed
= true;
78 if (plane_state
->crtc
) {
80 state
->crtc_states
[drm_crtc_index(plane_state
->crtc
)];
82 if (WARN_ON(!crtc_state
))
85 crtc_state
->planes_changed
= true;
90 check_pending_encoder_assignment(struct drm_atomic_state
*state
,
91 struct drm_encoder
*new_encoder
,
92 struct drm_connector
*new_connector
)
94 struct drm_connector
*connector
;
95 struct drm_connector_state
*conn_state
;
98 for_each_connector_in_state(state
, connector
, conn_state
, i
) {
99 if (conn_state
->best_encoder
!= new_encoder
)
102 /* encoder already assigned and we're trying to re-steal it! */
103 if (connector
->state
->best_encoder
!= conn_state
->best_encoder
)
110 static struct drm_crtc
*
111 get_current_crtc_for_encoder(struct drm_device
*dev
,
112 struct drm_encoder
*encoder
)
114 struct drm_mode_config
*config
= &dev
->mode_config
;
115 struct drm_connector
*connector
;
117 WARN_ON(!drm_modeset_is_locked(&config
->connection_mutex
));
119 drm_for_each_connector(connector
, dev
) {
120 if (connector
->state
->best_encoder
!= encoder
)
123 return connector
->state
->crtc
;
130 steal_encoder(struct drm_atomic_state
*state
,
131 struct drm_encoder
*encoder
,
132 struct drm_crtc
*encoder_crtc
)
134 struct drm_mode_config
*config
= &state
->dev
->mode_config
;
135 struct drm_crtc_state
*crtc_state
;
136 struct drm_connector
*connector
;
137 struct drm_connector_state
*connector_state
;
141 * We can only steal an encoder coming from a connector, which means we
142 * must already hold the connection_mutex.
144 WARN_ON(!drm_modeset_is_locked(&config
->connection_mutex
));
146 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d], stealing it\n",
147 encoder
->base
.id
, encoder
->name
,
148 encoder_crtc
->base
.id
);
150 crtc_state
= drm_atomic_get_crtc_state(state
, encoder_crtc
);
151 if (IS_ERR(crtc_state
))
152 return PTR_ERR(crtc_state
);
154 crtc_state
->connectors_changed
= true;
156 list_for_each_entry(connector
, &config
->connector_list
, head
) {
157 if (connector
->state
->best_encoder
!= encoder
)
160 DRM_DEBUG_ATOMIC("Stealing encoder from [CONNECTOR:%d:%s]\n",
164 connector_state
= drm_atomic_get_connector_state(state
,
166 if (IS_ERR(connector_state
))
167 return PTR_ERR(connector_state
);
169 ret
= drm_atomic_set_crtc_for_connector(connector_state
, NULL
);
172 connector_state
->best_encoder
= NULL
;
179 update_connector_routing(struct drm_atomic_state
*state
, int conn_idx
)
181 const struct drm_connector_helper_funcs
*funcs
;
182 struct drm_encoder
*new_encoder
;
183 struct drm_crtc
*encoder_crtc
;
184 struct drm_connector
*connector
;
185 struct drm_connector_state
*connector_state
;
186 struct drm_crtc_state
*crtc_state
;
189 connector
= state
->connectors
[conn_idx
];
190 connector_state
= state
->connector_states
[conn_idx
];
195 DRM_DEBUG_ATOMIC("Updating routing for [CONNECTOR:%d:%s]\n",
199 if (connector
->state
->crtc
!= connector_state
->crtc
) {
200 if (connector
->state
->crtc
) {
201 idx
= drm_crtc_index(connector
->state
->crtc
);
203 crtc_state
= state
->crtc_states
[idx
];
204 crtc_state
->connectors_changed
= true;
207 if (connector_state
->crtc
) {
208 idx
= drm_crtc_index(connector_state
->crtc
);
210 crtc_state
= state
->crtc_states
[idx
];
211 crtc_state
->connectors_changed
= true;
215 if (!connector_state
->crtc
) {
216 DRM_DEBUG_ATOMIC("Disabling [CONNECTOR:%d:%s]\n",
220 connector_state
->best_encoder
= NULL
;
225 funcs
= connector
->helper_private
;
227 if (funcs
->atomic_best_encoder
)
228 new_encoder
= funcs
->atomic_best_encoder(connector
,
231 new_encoder
= funcs
->best_encoder(connector
);
234 DRM_DEBUG_ATOMIC("No suitable encoder found for [CONNECTOR:%d:%s]\n",
240 if (!drm_encoder_crtc_ok(new_encoder
, connector_state
->crtc
)) {
241 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] incompatible with [CRTC:%d]\n",
242 new_encoder
->base
.id
,
244 connector_state
->crtc
->base
.id
);
248 if (new_encoder
== connector_state
->best_encoder
) {
249 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] keeps [ENCODER:%d:%s], now on [CRTC:%d]\n",
252 new_encoder
->base
.id
,
254 connector_state
->crtc
->base
.id
);
259 if (!check_pending_encoder_assignment(state
, new_encoder
, connector
)) {
260 DRM_DEBUG_ATOMIC("Encoder for [CONNECTOR:%d:%s] already assigned\n",
266 encoder_crtc
= get_current_crtc_for_encoder(state
->dev
,
270 ret
= steal_encoder(state
, new_encoder
, encoder_crtc
);
272 DRM_DEBUG_ATOMIC("Encoder stealing failed for [CONNECTOR:%d:%s]\n",
279 if (WARN_ON(!connector_state
->crtc
))
282 connector_state
->best_encoder
= new_encoder
;
283 idx
= drm_crtc_index(connector_state
->crtc
);
285 crtc_state
= state
->crtc_states
[idx
];
286 crtc_state
->connectors_changed
= true;
288 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] using [ENCODER:%d:%s] on [CRTC:%d]\n",
291 new_encoder
->base
.id
,
293 connector_state
->crtc
->base
.id
);
299 mode_fixup(struct drm_atomic_state
*state
)
301 struct drm_crtc
*crtc
;
302 struct drm_crtc_state
*crtc_state
;
303 struct drm_connector
*connector
;
304 struct drm_connector_state
*conn_state
;
308 for_each_crtc_in_state(state
, crtc
, crtc_state
, i
) {
309 if (!crtc_state
->mode_changed
&&
310 !crtc_state
->connectors_changed
)
313 drm_mode_copy(&crtc_state
->adjusted_mode
, &crtc_state
->mode
);
316 for_each_connector_in_state(state
, connector
, conn_state
, i
) {
317 const struct drm_encoder_helper_funcs
*funcs
;
318 struct drm_encoder
*encoder
;
320 WARN_ON(!!conn_state
->best_encoder
!= !!conn_state
->crtc
);
322 if (!conn_state
->crtc
|| !conn_state
->best_encoder
)
326 state
->crtc_states
[drm_crtc_index(conn_state
->crtc
)];
329 * Each encoder has at most one connector (since we always steal
330 * it away), so we won't call ->mode_fixup twice.
332 encoder
= conn_state
->best_encoder
;
333 funcs
= encoder
->helper_private
;
337 ret
= drm_bridge_mode_fixup(encoder
->bridge
, &crtc_state
->mode
,
338 &crtc_state
->adjusted_mode
);
340 DRM_DEBUG_ATOMIC("Bridge fixup failed\n");
344 if (funcs
->atomic_check
) {
345 ret
= funcs
->atomic_check(encoder
, crtc_state
,
348 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] check failed\n",
349 encoder
->base
.id
, encoder
->name
);
352 } else if (funcs
->mode_fixup
) {
353 ret
= funcs
->mode_fixup(encoder
, &crtc_state
->mode
,
354 &crtc_state
->adjusted_mode
);
356 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] fixup failed\n",
357 encoder
->base
.id
, encoder
->name
);
363 for_each_crtc_in_state(state
, crtc
, crtc_state
, i
) {
364 const struct drm_crtc_helper_funcs
*funcs
;
366 if (!crtc_state
->mode_changed
&&
367 !crtc_state
->connectors_changed
)
370 funcs
= crtc
->helper_private
;
371 if (!funcs
->mode_fixup
)
374 ret
= funcs
->mode_fixup(crtc
, &crtc_state
->mode
,
375 &crtc_state
->adjusted_mode
);
377 DRM_DEBUG_ATOMIC("[CRTC:%d] fixup failed\n",
387 * drm_atomic_helper_check_modeset - validate state object for modeset changes
389 * @state: the driver state object
391 * Check the state object to see if the requested state is physically possible.
392 * This does all the crtc and connector related computations for an atomic
393 * update and adds any additional connectors needed for full modesets and calls
394 * down into ->mode_fixup functions of the driver backend.
396 * crtc_state->mode_changed is set when the input mode is changed.
397 * crtc_state->connectors_changed is set when a connector is added or
398 * removed from the crtc.
399 * crtc_state->active_changed is set when crtc_state->active changes,
400 * which is used for dpms.
404 * Drivers which update ->mode_changed (e.g. in their ->atomic_check hooks if a
405 * plane update can't be done without a full modeset) _must_ call this function
406 * afterwards after that change. It is permitted to call this function multiple
407 * times for the same update, e.g. when the ->atomic_check functions depend upon
408 * the adjusted dotclock for fifo space allocation and watermark computation.
411 * Zero for success or -errno
414 drm_atomic_helper_check_modeset(struct drm_device
*dev
,
415 struct drm_atomic_state
*state
)
417 struct drm_crtc
*crtc
;
418 struct drm_crtc_state
*crtc_state
;
419 struct drm_connector
*connector
;
420 struct drm_connector_state
*connector_state
;
423 for_each_crtc_in_state(state
, crtc
, crtc_state
, i
) {
424 if (!drm_mode_equal(&crtc
->state
->mode
, &crtc_state
->mode
)) {
425 DRM_DEBUG_ATOMIC("[CRTC:%d] mode changed\n",
427 crtc_state
->mode_changed
= true;
430 if (crtc
->state
->enable
!= crtc_state
->enable
) {
431 DRM_DEBUG_ATOMIC("[CRTC:%d] enable changed\n",
435 * For clarity this assignment is done here, but
436 * enable == 0 is only true when there are no
437 * connectors and a NULL mode.
439 * The other way around is true as well. enable != 0
440 * iff connectors are attached and a mode is set.
442 crtc_state
->mode_changed
= true;
443 crtc_state
->connectors_changed
= true;
447 for_each_connector_in_state(state
, connector
, connector_state
, i
) {
449 * This only sets crtc->mode_changed for routing changes,
450 * drivers must set crtc->mode_changed themselves when connector
451 * properties need to be updated.
453 ret
= update_connector_routing(state
, i
);
459 * After all the routing has been prepared we need to add in any
460 * connector which is itself unchanged, but who's crtc changes it's
461 * configuration. This must be done before calling mode_fixup in case a
462 * crtc only changed its mode but has the same set of connectors.
464 for_each_crtc_in_state(state
, crtc
, crtc_state
, i
) {
468 * We must set ->active_changed after walking connectors for
469 * otherwise an update that only changes active would result in
470 * a full modeset because update_connector_routing force that.
472 if (crtc
->state
->active
!= crtc_state
->active
) {
473 DRM_DEBUG_ATOMIC("[CRTC:%d] active changed\n",
475 crtc_state
->active_changed
= true;
478 if (!drm_atomic_crtc_needs_modeset(crtc_state
))
481 DRM_DEBUG_ATOMIC("[CRTC:%d] needs all connectors, enable: %c, active: %c\n",
483 crtc_state
->enable
? 'y' : 'n',
484 crtc_state
->active
? 'y' : 'n');
486 ret
= drm_atomic_add_affected_connectors(state
, crtc
);
490 ret
= drm_atomic_add_affected_planes(state
, crtc
);
494 num_connectors
= drm_atomic_connectors_for_crtc(state
,
497 if (crtc_state
->enable
!= !!num_connectors
) {
498 DRM_DEBUG_ATOMIC("[CRTC:%d] enabled/connectors mismatch\n",
505 return mode_fixup(state
);
507 EXPORT_SYMBOL(drm_atomic_helper_check_modeset
);
510 * drm_atomic_helper_check_planes - validate state object for planes changes
512 * @state: the driver state object
514 * Check the state object to see if the requested state is physically possible.
515 * This does all the plane update related checks using by calling into the
516 * ->atomic_check hooks provided by the driver.
518 * It also sets crtc_state->planes_changed to indicate that a crtc has
522 * Zero for success or -errno
525 drm_atomic_helper_check_planes(struct drm_device
*dev
,
526 struct drm_atomic_state
*state
)
528 struct drm_crtc
*crtc
;
529 struct drm_crtc_state
*crtc_state
;
530 struct drm_plane
*plane
;
531 struct drm_plane_state
*plane_state
;
534 for_each_plane_in_state(state
, plane
, plane_state
, i
) {
535 const struct drm_plane_helper_funcs
*funcs
;
537 funcs
= plane
->helper_private
;
539 drm_atomic_helper_plane_changed(state
, plane_state
, plane
);
541 if (!funcs
|| !funcs
->atomic_check
)
544 ret
= funcs
->atomic_check(plane
, plane_state
);
546 DRM_DEBUG_ATOMIC("[PLANE:%d] atomic driver check failed\n",
552 for_each_crtc_in_state(state
, crtc
, crtc_state
, i
) {
553 const struct drm_crtc_helper_funcs
*funcs
;
555 funcs
= crtc
->helper_private
;
557 if (!funcs
|| !funcs
->atomic_check
)
560 ret
= funcs
->atomic_check(crtc
, state
->crtc_states
[i
]);
562 DRM_DEBUG_ATOMIC("[CRTC:%d] atomic driver check failed\n",
570 EXPORT_SYMBOL(drm_atomic_helper_check_planes
);
573 * drm_atomic_helper_check - validate state object
575 * @state: the driver state object
577 * Check the state object to see if the requested state is physically possible.
578 * Only crtcs and planes have check callbacks, so for any additional (global)
579 * checking that a driver needs it can simply wrap that around this function.
580 * Drivers without such needs can directly use this as their ->atomic_check()
583 * This just wraps the two parts of the state checking for planes and modeset
584 * state in the default order: First it calls drm_atomic_helper_check_modeset()
585 * and then drm_atomic_helper_check_planes(). The assumption is that the
586 * ->atomic_check functions depend upon an updated adjusted_mode.clock to
587 * e.g. properly compute watermarks.
590 * Zero for success or -errno
592 int drm_atomic_helper_check(struct drm_device
*dev
,
593 struct drm_atomic_state
*state
)
597 ret
= drm_atomic_helper_check_modeset(dev
, state
);
601 ret
= drm_atomic_helper_check_planes(dev
, state
);
607 EXPORT_SYMBOL(drm_atomic_helper_check
);
610 disable_outputs(struct drm_device
*dev
, struct drm_atomic_state
*old_state
)
612 struct drm_connector
*connector
;
613 struct drm_connector_state
*old_conn_state
;
614 struct drm_crtc
*crtc
;
615 struct drm_crtc_state
*old_crtc_state
;
618 for_each_connector_in_state(old_state
, connector
, old_conn_state
, i
) {
619 const struct drm_encoder_helper_funcs
*funcs
;
620 struct drm_encoder
*encoder
;
621 struct drm_crtc_state
*old_crtc_state
;
623 /* Shut down everything that's in the changeset and currently
624 * still on. So need to check the old, saved state. */
625 if (!old_conn_state
->crtc
)
628 old_crtc_state
= old_state
->crtc_states
[drm_crtc_index(old_conn_state
->crtc
)];
630 if (!old_crtc_state
->active
||
631 !drm_atomic_crtc_needs_modeset(old_conn_state
->crtc
->state
))
634 encoder
= old_conn_state
->best_encoder
;
636 /* We shouldn't get this far if we didn't previously have
637 * an encoder.. but WARN_ON() rather than explode.
639 if (WARN_ON(!encoder
))
642 funcs
= encoder
->helper_private
;
644 DRM_DEBUG_ATOMIC("disabling [ENCODER:%d:%s]\n",
645 encoder
->base
.id
, encoder
->name
);
648 * Each encoder has at most one connector (since we always steal
649 * it away), so we won't call disable hooks twice.
651 drm_bridge_disable(encoder
->bridge
);
653 /* Right function depends upon target state. */
654 if (connector
->state
->crtc
&& funcs
->prepare
)
655 funcs
->prepare(encoder
);
656 else if (funcs
->disable
)
657 funcs
->disable(encoder
);
659 funcs
->dpms(encoder
, DRM_MODE_DPMS_OFF
);
661 drm_bridge_post_disable(encoder
->bridge
);
664 for_each_crtc_in_state(old_state
, crtc
, old_crtc_state
, i
) {
665 const struct drm_crtc_helper_funcs
*funcs
;
667 /* Shut down everything that needs a full modeset. */
668 if (!drm_atomic_crtc_needs_modeset(crtc
->state
))
671 if (!old_crtc_state
->active
)
674 funcs
= crtc
->helper_private
;
676 DRM_DEBUG_ATOMIC("disabling [CRTC:%d]\n",
680 /* Right function depends upon target state. */
681 if (crtc
->state
->enable
&& funcs
->prepare
)
682 funcs
->prepare(crtc
);
683 else if (funcs
->disable
)
684 funcs
->disable(crtc
);
686 funcs
->dpms(crtc
, DRM_MODE_DPMS_OFF
);
691 * drm_atomic_helper_update_legacy_modeset_state - update legacy modeset state
693 * @old_state: atomic state object with old state structures
695 * This function updates all the various legacy modeset state pointers in
696 * connectors, encoders and crtcs. It also updates the timestamping constants
697 * used for precise vblank timestamps by calling
698 * drm_calc_timestamping_constants().
700 * Drivers can use this for building their own atomic commit if they don't have
701 * a pure helper-based modeset implementation.
704 drm_atomic_helper_update_legacy_modeset_state(struct drm_device
*dev
,
705 struct drm_atomic_state
*old_state
)
707 struct drm_connector
*connector
;
708 struct drm_connector_state
*old_conn_state
;
709 struct drm_crtc
*crtc
;
710 struct drm_crtc_state
*old_crtc_state
;
713 /* clear out existing links and update dpms */
714 for_each_connector_in_state(old_state
, connector
, old_conn_state
, i
) {
715 if (connector
->encoder
) {
716 WARN_ON(!connector
->encoder
->crtc
);
718 connector
->encoder
->crtc
= NULL
;
719 connector
->encoder
= NULL
;
722 crtc
= connector
->state
->crtc
;
723 if ((!crtc
&& old_conn_state
->crtc
) ||
724 (crtc
&& drm_atomic_crtc_needs_modeset(crtc
->state
))) {
725 struct drm_property
*dpms_prop
=
726 dev
->mode_config
.dpms_property
;
727 int mode
= DRM_MODE_DPMS_OFF
;
729 if (crtc
&& crtc
->state
->active
)
730 mode
= DRM_MODE_DPMS_ON
;
732 connector
->dpms
= mode
;
733 drm_object_property_set_value(&connector
->base
,
739 for_each_connector_in_state(old_state
, connector
, old_conn_state
, i
) {
740 if (!connector
->state
->crtc
)
743 if (WARN_ON(!connector
->state
->best_encoder
))
746 connector
->encoder
= connector
->state
->best_encoder
;
747 connector
->encoder
->crtc
= connector
->state
->crtc
;
750 /* set legacy state in the crtc structure */
751 for_each_crtc_in_state(old_state
, crtc
, old_crtc_state
, i
) {
752 struct drm_plane
*primary
= crtc
->primary
;
754 crtc
->mode
= crtc
->state
->mode
;
755 crtc
->enabled
= crtc
->state
->enable
;
757 if (drm_atomic_get_existing_plane_state(old_state
, primary
) &&
758 primary
->state
->crtc
== crtc
) {
759 crtc
->x
= primary
->state
->src_x
>> 16;
760 crtc
->y
= primary
->state
->src_y
>> 16;
763 if (crtc
->state
->enable
)
764 drm_calc_timestamping_constants(crtc
,
765 &crtc
->state
->adjusted_mode
);
768 EXPORT_SYMBOL(drm_atomic_helper_update_legacy_modeset_state
);
771 crtc_set_mode(struct drm_device
*dev
, struct drm_atomic_state
*old_state
)
773 struct drm_crtc
*crtc
;
774 struct drm_crtc_state
*old_crtc_state
;
775 struct drm_connector
*connector
;
776 struct drm_connector_state
*old_conn_state
;
779 for_each_crtc_in_state(old_state
, crtc
, old_crtc_state
, i
) {
780 const struct drm_crtc_helper_funcs
*funcs
;
782 if (!crtc
->state
->mode_changed
)
785 funcs
= crtc
->helper_private
;
787 if (crtc
->state
->enable
&& funcs
->mode_set_nofb
) {
788 DRM_DEBUG_ATOMIC("modeset on [CRTC:%d]\n",
791 funcs
->mode_set_nofb(crtc
);
795 for_each_connector_in_state(old_state
, connector
, old_conn_state
, i
) {
796 const struct drm_encoder_helper_funcs
*funcs
;
797 struct drm_crtc_state
*new_crtc_state
;
798 struct drm_encoder
*encoder
;
799 struct drm_display_mode
*mode
, *adjusted_mode
;
801 if (!connector
->state
->best_encoder
)
804 encoder
= connector
->state
->best_encoder
;
805 funcs
= encoder
->helper_private
;
806 new_crtc_state
= connector
->state
->crtc
->state
;
807 mode
= &new_crtc_state
->mode
;
808 adjusted_mode
= &new_crtc_state
->adjusted_mode
;
810 if (!new_crtc_state
->mode_changed
)
813 DRM_DEBUG_ATOMIC("modeset on [ENCODER:%d:%s]\n",
814 encoder
->base
.id
, encoder
->name
);
817 * Each encoder has at most one connector (since we always steal
818 * it away), so we won't call mode_set hooks twice.
821 funcs
->mode_set(encoder
, mode
, adjusted_mode
);
823 drm_bridge_mode_set(encoder
->bridge
, mode
, adjusted_mode
);
828 * drm_atomic_helper_commit_modeset_disables - modeset commit to disable outputs
830 * @old_state: atomic state object with old state structures
832 * This function shuts down all the outputs that need to be shut down and
833 * prepares them (if required) with the new mode.
835 * For compatibility with legacy crtc helpers this should be called before
836 * drm_atomic_helper_commit_planes(), which is what the default commit function
837 * does. But drivers with different needs can group the modeset commits together
838 * and do the plane commits at the end. This is useful for drivers doing runtime
839 * PM since planes updates then only happen when the CRTC is actually enabled.
841 void drm_atomic_helper_commit_modeset_disables(struct drm_device
*dev
,
842 struct drm_atomic_state
*old_state
)
844 disable_outputs(dev
, old_state
);
846 drm_atomic_helper_update_legacy_modeset_state(dev
, old_state
);
848 crtc_set_mode(dev
, old_state
);
850 EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_disables
);
853 * drm_atomic_helper_commit_modeset_enables - modeset commit to enable outputs
855 * @old_state: atomic state object with old state structures
857 * This function enables all the outputs with the new configuration which had to
858 * be turned off for the update.
860 * For compatibility with legacy crtc helpers this should be called after
861 * drm_atomic_helper_commit_planes(), which is what the default commit function
862 * does. But drivers with different needs can group the modeset commits together
863 * and do the plane commits at the end. This is useful for drivers doing runtime
864 * PM since planes updates then only happen when the CRTC is actually enabled.
866 void drm_atomic_helper_commit_modeset_enables(struct drm_device
*dev
,
867 struct drm_atomic_state
*old_state
)
869 struct drm_crtc
*crtc
;
870 struct drm_crtc_state
*old_crtc_state
;
871 struct drm_connector
*connector
;
872 struct drm_connector_state
*old_conn_state
;
875 for_each_crtc_in_state(old_state
, crtc
, old_crtc_state
, i
) {
876 const struct drm_crtc_helper_funcs
*funcs
;
878 /* Need to filter out CRTCs where only planes change. */
879 if (!drm_atomic_crtc_needs_modeset(crtc
->state
))
882 if (!crtc
->state
->active
)
885 funcs
= crtc
->helper_private
;
887 if (crtc
->state
->enable
) {
888 DRM_DEBUG_ATOMIC("enabling [CRTC:%d]\n",
898 for_each_connector_in_state(old_state
, connector
, old_conn_state
, i
) {
899 const struct drm_encoder_helper_funcs
*funcs
;
900 struct drm_encoder
*encoder
;
902 if (!connector
->state
->best_encoder
)
905 if (!connector
->state
->crtc
->state
->active
||
906 !drm_atomic_crtc_needs_modeset(connector
->state
->crtc
->state
))
909 encoder
= connector
->state
->best_encoder
;
910 funcs
= encoder
->helper_private
;
912 DRM_DEBUG_ATOMIC("enabling [ENCODER:%d:%s]\n",
913 encoder
->base
.id
, encoder
->name
);
916 * Each encoder has at most one connector (since we always steal
917 * it away), so we won't call enable hooks twice.
919 drm_bridge_pre_enable(encoder
->bridge
);
922 funcs
->enable(encoder
);
924 funcs
->commit(encoder
);
926 drm_bridge_enable(encoder
->bridge
);
929 EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_enables
);
931 static void wait_for_fences(struct drm_device
*dev
,
932 struct drm_atomic_state
*state
)
934 struct drm_plane
*plane
;
935 struct drm_plane_state
*plane_state
;
938 for_each_plane_in_state(state
, plane
, plane_state
, i
) {
939 if (!plane
->state
->fence
)
942 WARN_ON(!plane
->state
->fb
);
944 fence_wait(plane
->state
->fence
, false);
945 fence_put(plane
->state
->fence
);
946 plane
->state
->fence
= NULL
;
950 static bool framebuffer_changed(struct drm_device
*dev
,
951 struct drm_atomic_state
*old_state
,
952 struct drm_crtc
*crtc
)
954 struct drm_plane
*plane
;
955 struct drm_plane_state
*old_plane_state
;
958 for_each_plane_in_state(old_state
, plane
, old_plane_state
, i
) {
959 if (plane
->state
->crtc
!= crtc
&&
960 old_plane_state
->crtc
!= crtc
)
963 if (plane
->state
->fb
!= old_plane_state
->fb
)
971 * drm_atomic_helper_wait_for_vblanks - wait for vblank on crtcs
973 * @old_state: atomic state object with old state structures
975 * Helper to, after atomic commit, wait for vblanks on all effected
976 * crtcs (ie. before cleaning up old framebuffers using
977 * drm_atomic_helper_cleanup_planes()). It will only wait on crtcs where the
978 * framebuffers have actually changed to optimize for the legacy cursor and
979 * plane update use-case.
982 drm_atomic_helper_wait_for_vblanks(struct drm_device
*dev
,
983 struct drm_atomic_state
*old_state
)
985 struct drm_crtc
*crtc
;
986 struct drm_crtc_state
*old_crtc_state
;
989 for_each_crtc_in_state(old_state
, crtc
, old_crtc_state
, i
) {
990 /* No one cares about the old state, so abuse it for tracking
991 * and store whether we hold a vblank reference (and should do a
992 * vblank wait) in the ->enable boolean. */
993 old_crtc_state
->enable
= false;
995 if (!crtc
->state
->enable
)
998 /* Legacy cursor ioctls are completely unsynced, and userspace
999 * relies on that (by doing tons of cursor updates). */
1000 if (old_state
->legacy_cursor_update
)
1003 if (!framebuffer_changed(dev
, old_state
, crtc
))
1006 ret
= drm_crtc_vblank_get(crtc
);
1010 old_crtc_state
->enable
= true;
1011 old_crtc_state
->last_vblank_count
= drm_crtc_vblank_count(crtc
);
1014 for_each_crtc_in_state(old_state
, crtc
, old_crtc_state
, i
) {
1015 if (!old_crtc_state
->enable
)
1018 ret
= wait_event_timeout(dev
->vblank
[i
].queue
,
1019 old_crtc_state
->last_vblank_count
!=
1020 drm_crtc_vblank_count(crtc
),
1021 msecs_to_jiffies(50));
1023 drm_crtc_vblank_put(crtc
);
1026 EXPORT_SYMBOL(drm_atomic_helper_wait_for_vblanks
);
1029 * drm_atomic_helper_commit - commit validated state object
1031 * @state: the driver state object
1032 * @async: asynchronous commit
1034 * This function commits a with drm_atomic_helper_check() pre-validated state
1035 * object. This can still fail when e.g. the framebuffer reservation fails. For
1036 * now this doesn't implement asynchronous commits.
1038 * Note that right now this function does not support async commits, and hence
1039 * driver writers must implement their own version for now. Also note that the
1040 * default ordering of how the various stages are called is to match the legacy
1041 * modeset helper library closest. One peculiarity of that is that it doesn't
1042 * mesh well with runtime PM at all.
1044 * For drivers supporting runtime PM the recommended sequence is
1046 * drm_atomic_helper_commit_modeset_disables(dev, state);
1048 * drm_atomic_helper_commit_modeset_enables(dev, state);
1050 * drm_atomic_helper_commit_planes(dev, state, true);
1052 * See the kerneldoc entries for these three functions for more details.
1055 * Zero for success or -errno.
1057 int drm_atomic_helper_commit(struct drm_device
*dev
,
1058 struct drm_atomic_state
*state
,
1066 ret
= drm_atomic_helper_prepare_planes(dev
, state
);
1071 * This is the point of no return - everything below never fails except
1072 * when the hw goes bonghits. Which means we can commit the new state on
1073 * the software side now.
1076 drm_atomic_helper_swap_state(dev
, state
);
1079 * Everything below can be run asynchronously without the need to grab
1080 * any modeset locks at all under one condition: It must be guaranteed
1081 * that the asynchronous work has either been cancelled (if the driver
1082 * supports it, which at least requires that the framebuffers get
1083 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
1084 * before the new state gets committed on the software side with
1085 * drm_atomic_helper_swap_state().
1087 * This scheme allows new atomic state updates to be prepared and
1088 * checked in parallel to the asynchronous completion of the previous
1089 * update. Which is important since compositors need to figure out the
1090 * composition of the next frame right after having submitted the
1094 wait_for_fences(dev
, state
);
1096 drm_atomic_helper_commit_modeset_disables(dev
, state
);
1098 drm_atomic_helper_commit_planes(dev
, state
, false);
1100 drm_atomic_helper_commit_modeset_enables(dev
, state
);
1102 drm_atomic_helper_wait_for_vblanks(dev
, state
);
1104 drm_atomic_helper_cleanup_planes(dev
, state
);
1106 drm_atomic_state_free(state
);
1110 EXPORT_SYMBOL(drm_atomic_helper_commit
);
1113 * DOC: implementing async commit
1115 * For now the atomic helpers don't support async commit directly. If there is
1116 * real need it could be added though, using the dma-buf fence infrastructure
1117 * for generic synchronization with outstanding rendering.
1119 * For now drivers have to implement async commit themselves, with the following
1120 * sequence being the recommended one:
1122 * 1. Run drm_atomic_helper_prepare_planes() first. This is the only function
1123 * which commit needs to call which can fail, so we want to run it first and
1126 * 2. Synchronize with any outstanding asynchronous commit worker threads which
1127 * might be affected the new state update. This can be done by either cancelling
1128 * or flushing the work items, depending upon whether the driver can deal with
1129 * cancelled updates. Note that it is important to ensure that the framebuffer
1130 * cleanup is still done when cancelling.
1132 * For sufficient parallelism it is recommended to have a work item per crtc
1133 * (for updates which don't touch global state) and a global one. Then we only
1134 * need to synchronize with the crtc work items for changed crtcs and the global
1135 * work item, which allows nice concurrent updates on disjoint sets of crtcs.
1137 * 3. The software state is updated synchronously with
1138 * drm_atomic_helper_swap_state(). Doing this under the protection of all modeset
1139 * locks means concurrent callers never see inconsistent state. And doing this
1140 * while it's guaranteed that no relevant async worker runs means that async
1141 * workers do not need grab any locks. Actually they must not grab locks, for
1142 * otherwise the work flushing will deadlock.
1144 * 4. Schedule a work item to do all subsequent steps, using the split-out
1145 * commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and
1146 * then cleaning up the framebuffers after the old framebuffer is no longer
1151 * drm_atomic_helper_prepare_planes - prepare plane resources before commit
1153 * @state: atomic state object with new state structures
1155 * This function prepares plane state, specifically framebuffers, for the new
1156 * configuration. If any failure is encountered this function will call
1157 * ->cleanup_fb on any already successfully prepared framebuffer.
1160 * 0 on success, negative error code on failure.
1162 int drm_atomic_helper_prepare_planes(struct drm_device
*dev
,
1163 struct drm_atomic_state
*state
)
1165 int nplanes
= dev
->mode_config
.num_total_plane
;
1168 for (i
= 0; i
< nplanes
; i
++) {
1169 const struct drm_plane_helper_funcs
*funcs
;
1170 struct drm_plane
*plane
= state
->planes
[i
];
1171 struct drm_plane_state
*plane_state
= state
->plane_states
[i
];
1176 funcs
= plane
->helper_private
;
1178 if (funcs
->prepare_fb
) {
1179 ret
= funcs
->prepare_fb(plane
, plane_state
);
1188 for (i
--; i
>= 0; i
--) {
1189 const struct drm_plane_helper_funcs
*funcs
;
1190 struct drm_plane
*plane
= state
->planes
[i
];
1191 struct drm_plane_state
*plane_state
= state
->plane_states
[i
];
1196 funcs
= plane
->helper_private
;
1198 if (funcs
->cleanup_fb
)
1199 funcs
->cleanup_fb(plane
, plane_state
);
1205 EXPORT_SYMBOL(drm_atomic_helper_prepare_planes
);
1207 bool plane_crtc_active(struct drm_plane_state
*state
)
1209 return state
->crtc
&& state
->crtc
->state
->active
;
1213 * drm_atomic_helper_commit_planes - commit plane state
1215 * @old_state: atomic state object with old state structures
1216 * @active_only: Only commit on active CRTC if set
1218 * This function commits the new plane state using the plane and atomic helper
1219 * functions for planes and crtcs. It assumes that the atomic state has already
1220 * been pushed into the relevant object state pointers, since this step can no
1223 * It still requires the global state object @old_state to know which planes and
1224 * crtcs need to be updated though.
1226 * Note that this function does all plane updates across all CRTCs in one step.
1227 * If the hardware can't support this approach look at
1228 * drm_atomic_helper_commit_planes_on_crtc() instead.
1230 * Plane parameters can be updated by applications while the associated CRTC is
1231 * disabled. The DRM/KMS core will store the parameters in the plane state,
1232 * which will be available to the driver when the CRTC is turned on. As a result
1233 * most drivers don't need to be immediately notified of plane updates for a
1236 * Unless otherwise needed, drivers are advised to set the @active_only
1237 * parameters to true in order not to receive plane update notifications related
1238 * to a disabled CRTC. This avoids the need to manually ignore plane updates in
1239 * driver code when the driver and/or hardware can't or just don't need to deal
1240 * with updates on disabled CRTCs, for example when supporting runtime PM.
1242 * The drm_atomic_helper_commit() default implementation only sets @active_only
1243 * to false to most closely match the behaviour of the legacy helpers. This should
1244 * not be copied blindly by drivers.
1246 void drm_atomic_helper_commit_planes(struct drm_device
*dev
,
1247 struct drm_atomic_state
*old_state
,
1250 struct drm_crtc
*crtc
;
1251 struct drm_crtc_state
*old_crtc_state
;
1252 struct drm_plane
*plane
;
1253 struct drm_plane_state
*old_plane_state
;
1256 for_each_crtc_in_state(old_state
, crtc
, old_crtc_state
, i
) {
1257 const struct drm_crtc_helper_funcs
*funcs
;
1259 funcs
= crtc
->helper_private
;
1261 if (!funcs
|| !funcs
->atomic_begin
)
1264 if (active_only
&& !crtc
->state
->active
)
1267 funcs
->atomic_begin(crtc
, old_crtc_state
);
1270 for_each_plane_in_state(old_state
, plane
, old_plane_state
, i
) {
1271 const struct drm_plane_helper_funcs
*funcs
;
1274 funcs
= plane
->helper_private
;
1279 disabling
= drm_atomic_plane_disabling(plane
, old_plane_state
);
1283 * Skip planes related to inactive CRTCs. If the plane
1284 * is enabled use the state of the current CRTC. If the
1285 * plane is being disabled use the state of the old
1286 * CRTC to avoid skipping planes being disabled on an
1289 if (!disabling
&& !plane_crtc_active(plane
->state
))
1291 if (disabling
&& !plane_crtc_active(old_plane_state
))
1296 * Special-case disabling the plane if drivers support it.
1298 if (disabling
&& funcs
->atomic_disable
)
1299 funcs
->atomic_disable(plane
, old_plane_state
);
1300 else if (plane
->state
->crtc
|| disabling
)
1301 funcs
->atomic_update(plane
, old_plane_state
);
1304 for_each_crtc_in_state(old_state
, crtc
, old_crtc_state
, i
) {
1305 const struct drm_crtc_helper_funcs
*funcs
;
1307 funcs
= crtc
->helper_private
;
1309 if (!funcs
|| !funcs
->atomic_flush
)
1312 if (active_only
&& !crtc
->state
->active
)
1315 funcs
->atomic_flush(crtc
, old_crtc_state
);
1318 EXPORT_SYMBOL(drm_atomic_helper_commit_planes
);
1321 * drm_atomic_helper_commit_planes_on_crtc - commit plane state for a crtc
1322 * @old_crtc_state: atomic state object with the old crtc state
1324 * This function commits the new plane state using the plane and atomic helper
1325 * functions for planes on the specific crtc. It assumes that the atomic state
1326 * has already been pushed into the relevant object state pointers, since this
1327 * step can no longer fail.
1329 * This function is useful when plane updates should be done crtc-by-crtc
1330 * instead of one global step like drm_atomic_helper_commit_planes() does.
1332 * This function can only be savely used when planes are not allowed to move
1333 * between different CRTCs because this function doesn't handle inter-CRTC
1334 * depencies. Callers need to ensure that either no such depencies exist,
1335 * resolve them through ordering of commit calls or through some other means.
1338 drm_atomic_helper_commit_planes_on_crtc(struct drm_crtc_state
*old_crtc_state
)
1340 const struct drm_crtc_helper_funcs
*crtc_funcs
;
1341 struct drm_crtc
*crtc
= old_crtc_state
->crtc
;
1342 struct drm_atomic_state
*old_state
= old_crtc_state
->state
;
1343 struct drm_plane
*plane
;
1344 unsigned plane_mask
;
1346 plane_mask
= old_crtc_state
->plane_mask
;
1347 plane_mask
|= crtc
->state
->plane_mask
;
1349 crtc_funcs
= crtc
->helper_private
;
1350 if (crtc_funcs
&& crtc_funcs
->atomic_begin
)
1351 crtc_funcs
->atomic_begin(crtc
, old_crtc_state
);
1353 drm_for_each_plane_mask(plane
, crtc
->dev
, plane_mask
) {
1354 struct drm_plane_state
*old_plane_state
=
1355 drm_atomic_get_existing_plane_state(old_state
, plane
);
1356 const struct drm_plane_helper_funcs
*plane_funcs
;
1358 plane_funcs
= plane
->helper_private
;
1360 if (!old_plane_state
|| !plane_funcs
)
1363 WARN_ON(plane
->state
->crtc
&& plane
->state
->crtc
!= crtc
);
1365 if (drm_atomic_plane_disabling(plane
, old_plane_state
) &&
1366 plane_funcs
->atomic_disable
)
1367 plane_funcs
->atomic_disable(plane
, old_plane_state
);
1368 else if (plane
->state
->crtc
||
1369 drm_atomic_plane_disabling(plane
, old_plane_state
))
1370 plane_funcs
->atomic_update(plane
, old_plane_state
);
1373 if (crtc_funcs
&& crtc_funcs
->atomic_flush
)
1374 crtc_funcs
->atomic_flush(crtc
, old_crtc_state
);
1376 EXPORT_SYMBOL(drm_atomic_helper_commit_planes_on_crtc
);
1379 * drm_atomic_helper_disable_planes_on_crtc - helper to disable CRTC's planes
1381 * @atomic: if set, synchronize with CRTC's atomic_begin/flush hooks
1383 * Disables all planes associated with the given CRTC. This can be
1384 * used for instance in the CRTC helper disable callback to disable
1385 * all planes before shutting down the display pipeline.
1387 * If the atomic-parameter is set the function calls the CRTC's
1388 * atomic_begin hook before and atomic_flush hook after disabling the
1391 * It is a bug to call this function without having implemented the
1392 * ->atomic_disable() plane hook.
1394 void drm_atomic_helper_disable_planes_on_crtc(struct drm_crtc
*crtc
,
1397 const struct drm_crtc_helper_funcs
*crtc_funcs
=
1398 crtc
->helper_private
;
1399 struct drm_plane
*plane
;
1401 if (atomic
&& crtc_funcs
&& crtc_funcs
->atomic_begin
)
1402 crtc_funcs
->atomic_begin(crtc
, NULL
);
1404 drm_for_each_plane(plane
, crtc
->dev
) {
1405 const struct drm_plane_helper_funcs
*plane_funcs
=
1406 plane
->helper_private
;
1408 if (plane
->state
->crtc
!= crtc
|| !plane_funcs
)
1411 WARN_ON(!plane_funcs
->atomic_disable
);
1412 if (plane_funcs
->atomic_disable
)
1413 plane_funcs
->atomic_disable(plane
, NULL
);
1416 if (atomic
&& crtc_funcs
&& crtc_funcs
->atomic_flush
)
1417 crtc_funcs
->atomic_flush(crtc
, NULL
);
1419 EXPORT_SYMBOL(drm_atomic_helper_disable_planes_on_crtc
);
1422 * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
1424 * @old_state: atomic state object with old state structures
1426 * This function cleans up plane state, specifically framebuffers, from the old
1427 * configuration. Hence the old configuration must be perserved in @old_state to
1428 * be able to call this function.
1430 * This function must also be called on the new state when the atomic update
1431 * fails at any point after calling drm_atomic_helper_prepare_planes().
1433 void drm_atomic_helper_cleanup_planes(struct drm_device
*dev
,
1434 struct drm_atomic_state
*old_state
)
1436 struct drm_plane
*plane
;
1437 struct drm_plane_state
*plane_state
;
1440 for_each_plane_in_state(old_state
, plane
, plane_state
, i
) {
1441 const struct drm_plane_helper_funcs
*funcs
;
1443 funcs
= plane
->helper_private
;
1445 if (funcs
->cleanup_fb
)
1446 funcs
->cleanup_fb(plane
, plane_state
);
1449 EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes
);
1452 * drm_atomic_helper_swap_state - store atomic state into current sw state
1454 * @state: atomic state
1456 * This function stores the atomic state into the current state pointers in all
1457 * driver objects. It should be called after all failing steps have been done
1458 * and succeeded, but before the actual hardware state is committed.
1460 * For cleanup and error recovery the current state for all changed objects will
1461 * be swaped into @state.
1463 * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
1465 * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
1467 * 2. Do any other steps that might fail.
1469 * 3. Put the staged state into the current state pointers with this function.
1471 * 4. Actually commit the hardware state.
1473 * 5. Call drm_atomic_helper_cleanup_planes() with @state, which since step 3
1474 * contains the old state. Also do any other cleanup required with that state.
1476 void drm_atomic_helper_swap_state(struct drm_device
*dev
,
1477 struct drm_atomic_state
*state
)
1481 for (i
= 0; i
< dev
->mode_config
.num_connector
; i
++) {
1482 struct drm_connector
*connector
= state
->connectors
[i
];
1487 connector
->state
->state
= state
;
1488 swap(state
->connector_states
[i
], connector
->state
);
1489 connector
->state
->state
= NULL
;
1492 for (i
= 0; i
< dev
->mode_config
.num_crtc
; i
++) {
1493 struct drm_crtc
*crtc
= state
->crtcs
[i
];
1498 crtc
->state
->state
= state
;
1499 swap(state
->crtc_states
[i
], crtc
->state
);
1500 crtc
->state
->state
= NULL
;
1503 for (i
= 0; i
< dev
->mode_config
.num_total_plane
; i
++) {
1504 struct drm_plane
*plane
= state
->planes
[i
];
1509 plane
->state
->state
= state
;
1510 swap(state
->plane_states
[i
], plane
->state
);
1511 plane
->state
->state
= NULL
;
1514 EXPORT_SYMBOL(drm_atomic_helper_swap_state
);
1517 * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
1518 * @plane: plane object to update
1519 * @crtc: owning CRTC of owning plane
1520 * @fb: framebuffer to flip onto plane
1521 * @crtc_x: x offset of primary plane on crtc
1522 * @crtc_y: y offset of primary plane on crtc
1523 * @crtc_w: width of primary plane rectangle on crtc
1524 * @crtc_h: height of primary plane rectangle on crtc
1525 * @src_x: x offset of @fb for panning
1526 * @src_y: y offset of @fb for panning
1527 * @src_w: width of source rectangle in @fb
1528 * @src_h: height of source rectangle in @fb
1530 * Provides a default plane update handler using the atomic driver interface.
1533 * Zero on success, error code on failure
1535 int drm_atomic_helper_update_plane(struct drm_plane
*plane
,
1536 struct drm_crtc
*crtc
,
1537 struct drm_framebuffer
*fb
,
1538 int crtc_x
, int crtc_y
,
1539 unsigned int crtc_w
, unsigned int crtc_h
,
1540 uint32_t src_x
, uint32_t src_y
,
1541 uint32_t src_w
, uint32_t src_h
)
1543 struct drm_atomic_state
*state
;
1544 struct drm_plane_state
*plane_state
;
1547 state
= drm_atomic_state_alloc(plane
->dev
);
1551 state
->acquire_ctx
= drm_modeset_legacy_acquire_ctx(crtc
);
1553 plane_state
= drm_atomic_get_plane_state(state
, plane
);
1554 if (IS_ERR(plane_state
)) {
1555 ret
= PTR_ERR(plane_state
);
1559 ret
= drm_atomic_set_crtc_for_plane(plane_state
, crtc
);
1562 drm_atomic_set_fb_for_plane(plane_state
, fb
);
1563 plane_state
->crtc_x
= crtc_x
;
1564 plane_state
->crtc_y
= crtc_y
;
1565 plane_state
->crtc_w
= crtc_w
;
1566 plane_state
->crtc_h
= crtc_h
;
1567 plane_state
->src_x
= src_x
;
1568 plane_state
->src_y
= src_y
;
1569 plane_state
->src_w
= src_w
;
1570 plane_state
->src_h
= src_h
;
1572 if (plane
== crtc
->cursor
)
1573 state
->legacy_cursor_update
= true;
1575 ret
= drm_atomic_commit(state
);
1579 /* Driver takes ownership of state on successful commit. */
1582 if (ret
== -EDEADLK
)
1585 drm_atomic_state_free(state
);
1589 drm_atomic_state_clear(state
);
1590 drm_atomic_legacy_backoff(state
);
1593 * Someone might have exchanged the framebuffer while we dropped locks
1594 * in the backoff code. We need to fix up the fb refcount tracking the
1597 plane
->old_fb
= plane
->fb
;
1601 EXPORT_SYMBOL(drm_atomic_helper_update_plane
);
1604 * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
1605 * @plane: plane to disable
1607 * Provides a default plane disable handler using the atomic driver interface.
1610 * Zero on success, error code on failure
1612 int drm_atomic_helper_disable_plane(struct drm_plane
*plane
)
1614 struct drm_atomic_state
*state
;
1615 struct drm_plane_state
*plane_state
;
1619 * FIXME: Without plane->crtc set we can't get at the implicit legacy
1620 * acquire context. The real fix will be to wire the acquire ctx through
1621 * everywhere we need it, but meanwhile prevent chaos by just skipping
1622 * this noop. The critical case is the cursor ioctls which a) only grab
1623 * crtc/cursor-plane locks (so we need the crtc to get at the right
1624 * acquire context) and b) can try to disable the plane multiple times.
1629 state
= drm_atomic_state_alloc(plane
->dev
);
1633 state
->acquire_ctx
= drm_modeset_legacy_acquire_ctx(plane
->crtc
);
1635 plane_state
= drm_atomic_get_plane_state(state
, plane
);
1636 if (IS_ERR(plane_state
)) {
1637 ret
= PTR_ERR(plane_state
);
1641 if (plane_state
->crtc
&& (plane
== plane
->crtc
->cursor
))
1642 plane_state
->state
->legacy_cursor_update
= true;
1644 ret
= __drm_atomic_helper_disable_plane(plane
, plane_state
);
1648 ret
= drm_atomic_commit(state
);
1652 /* Driver takes ownership of state on successful commit. */
1655 if (ret
== -EDEADLK
)
1658 drm_atomic_state_free(state
);
1662 drm_atomic_state_clear(state
);
1663 drm_atomic_legacy_backoff(state
);
1666 * Someone might have exchanged the framebuffer while we dropped locks
1667 * in the backoff code. We need to fix up the fb refcount tracking the
1670 plane
->old_fb
= plane
->fb
;
1674 EXPORT_SYMBOL(drm_atomic_helper_disable_plane
);
1676 /* just used from fb-helper and atomic-helper: */
1677 int __drm_atomic_helper_disable_plane(struct drm_plane
*plane
,
1678 struct drm_plane_state
*plane_state
)
1682 ret
= drm_atomic_set_crtc_for_plane(plane_state
, NULL
);
1686 drm_atomic_set_fb_for_plane(plane_state
, NULL
);
1687 plane_state
->crtc_x
= 0;
1688 plane_state
->crtc_y
= 0;
1689 plane_state
->crtc_w
= 0;
1690 plane_state
->crtc_h
= 0;
1691 plane_state
->src_x
= 0;
1692 plane_state
->src_y
= 0;
1693 plane_state
->src_w
= 0;
1694 plane_state
->src_h
= 0;
1699 static int update_output_state(struct drm_atomic_state
*state
,
1700 struct drm_mode_set
*set
)
1702 struct drm_device
*dev
= set
->crtc
->dev
;
1703 struct drm_crtc
*crtc
;
1704 struct drm_crtc_state
*crtc_state
;
1705 struct drm_connector
*connector
;
1706 struct drm_connector_state
*conn_state
;
1709 ret
= drm_modeset_lock(&dev
->mode_config
.connection_mutex
,
1710 state
->acquire_ctx
);
1714 /* First grab all affected connector/crtc states. */
1715 for (i
= 0; i
< set
->num_connectors
; i
++) {
1716 conn_state
= drm_atomic_get_connector_state(state
,
1717 set
->connectors
[i
]);
1718 if (IS_ERR(conn_state
))
1719 return PTR_ERR(conn_state
);
1722 for_each_crtc_in_state(state
, crtc
, crtc_state
, i
) {
1723 ret
= drm_atomic_add_affected_connectors(state
, crtc
);
1728 /* Then recompute connector->crtc links and crtc enabling state. */
1729 for_each_connector_in_state(state
, connector
, conn_state
, i
) {
1730 if (conn_state
->crtc
== set
->crtc
) {
1731 ret
= drm_atomic_set_crtc_for_connector(conn_state
,
1737 for (j
= 0; j
< set
->num_connectors
; j
++) {
1738 if (set
->connectors
[j
] == connector
) {
1739 ret
= drm_atomic_set_crtc_for_connector(conn_state
,
1748 for_each_crtc_in_state(state
, crtc
, crtc_state
, i
) {
1749 /* Don't update ->enable for the CRTC in the set_config request,
1750 * since a mismatch would indicate a bug in the upper layers.
1751 * The actual modeset code later on will catch any
1752 * inconsistencies here. */
1753 if (crtc
== set
->crtc
)
1756 if (!drm_atomic_connectors_for_crtc(state
, crtc
)) {
1757 ret
= drm_atomic_set_mode_prop_for_crtc(crtc_state
,
1762 crtc_state
->active
= false;
1770 * drm_atomic_helper_set_config - set a new config from userspace
1771 * @set: mode set configuration
1773 * Provides a default crtc set_config handler using the atomic driver interface.
1776 * Returns 0 on success, negative errno numbers on failure.
1778 int drm_atomic_helper_set_config(struct drm_mode_set
*set
)
1780 struct drm_atomic_state
*state
;
1781 struct drm_crtc
*crtc
= set
->crtc
;
1784 state
= drm_atomic_state_alloc(crtc
->dev
);
1788 state
->acquire_ctx
= drm_modeset_legacy_acquire_ctx(crtc
);
1790 ret
= __drm_atomic_helper_set_config(set
, state
);
1794 ret
= drm_atomic_commit(state
);
1798 /* Driver takes ownership of state on successful commit. */
1801 if (ret
== -EDEADLK
)
1804 drm_atomic_state_free(state
);
1808 drm_atomic_state_clear(state
);
1809 drm_atomic_legacy_backoff(state
);
1812 * Someone might have exchanged the framebuffer while we dropped locks
1813 * in the backoff code. We need to fix up the fb refcount tracking the
1816 crtc
->primary
->old_fb
= crtc
->primary
->fb
;
1820 EXPORT_SYMBOL(drm_atomic_helper_set_config
);
1822 /* just used from fb-helper and atomic-helper: */
1823 int __drm_atomic_helper_set_config(struct drm_mode_set
*set
,
1824 struct drm_atomic_state
*state
)
1826 struct drm_crtc_state
*crtc_state
;
1827 struct drm_plane_state
*primary_state
;
1828 struct drm_crtc
*crtc
= set
->crtc
;
1829 int hdisplay
, vdisplay
;
1832 crtc_state
= drm_atomic_get_crtc_state(state
, crtc
);
1833 if (IS_ERR(crtc_state
))
1834 return PTR_ERR(crtc_state
);
1836 primary_state
= drm_atomic_get_plane_state(state
, crtc
->primary
);
1837 if (IS_ERR(primary_state
))
1838 return PTR_ERR(primary_state
);
1842 WARN_ON(set
->num_connectors
);
1844 ret
= drm_atomic_set_mode_for_crtc(crtc_state
, NULL
);
1848 crtc_state
->active
= false;
1850 ret
= drm_atomic_set_crtc_for_plane(primary_state
, NULL
);
1854 drm_atomic_set_fb_for_plane(primary_state
, NULL
);
1860 WARN_ON(!set
->num_connectors
);
1862 ret
= drm_atomic_set_mode_for_crtc(crtc_state
, set
->mode
);
1866 crtc_state
->active
= true;
1868 ret
= drm_atomic_set_crtc_for_plane(primary_state
, crtc
);
1872 drm_crtc_get_hv_timing(set
->mode
, &hdisplay
, &vdisplay
);
1874 drm_atomic_set_fb_for_plane(primary_state
, set
->fb
);
1875 primary_state
->crtc_x
= 0;
1876 primary_state
->crtc_y
= 0;
1877 primary_state
->crtc_w
= hdisplay
;
1878 primary_state
->crtc_h
= vdisplay
;
1879 primary_state
->src_x
= set
->x
<< 16;
1880 primary_state
->src_y
= set
->y
<< 16;
1881 if (primary_state
->rotation
& (BIT(DRM_ROTATE_90
) | BIT(DRM_ROTATE_270
))) {
1882 primary_state
->src_w
= vdisplay
<< 16;
1883 primary_state
->src_h
= hdisplay
<< 16;
1885 primary_state
->src_w
= hdisplay
<< 16;
1886 primary_state
->src_h
= vdisplay
<< 16;
1890 ret
= update_output_state(state
, set
);
1898 * drm_atomic_helper_disable_all - disable all currently active outputs
1900 * @ctx: lock acquisition context
1902 * Loops through all connectors, finding those that aren't turned off and then
1903 * turns them off by setting their DPMS mode to OFF and deactivating the CRTC
1904 * that they are connected to.
1906 * This is used for example in suspend/resume to disable all currently active
1907 * functions when suspending.
1909 * Note that if callers haven't already acquired all modeset locks this might
1910 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
1913 * 0 on success or a negative error code on failure.
1916 * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
1918 int drm_atomic_helper_disable_all(struct drm_device
*dev
,
1919 struct drm_modeset_acquire_ctx
*ctx
)
1921 struct drm_atomic_state
*state
;
1922 struct drm_connector
*conn
;
1925 state
= drm_atomic_state_alloc(dev
);
1929 state
->acquire_ctx
= ctx
;
1931 drm_for_each_connector(conn
, dev
) {
1932 struct drm_crtc
*crtc
= conn
->state
->crtc
;
1933 struct drm_crtc_state
*crtc_state
;
1935 if (!crtc
|| conn
->dpms
!= DRM_MODE_DPMS_ON
)
1938 crtc_state
= drm_atomic_get_crtc_state(state
, crtc
);
1939 if (IS_ERR(crtc_state
)) {
1940 err
= PTR_ERR(crtc_state
);
1944 crtc_state
->active
= false;
1947 err
= drm_atomic_commit(state
);
1951 drm_atomic_state_free(state
);
1955 EXPORT_SYMBOL(drm_atomic_helper_disable_all
);
1958 * drm_atomic_helper_suspend - subsystem-level suspend helper
1961 * Duplicates the current atomic state, disables all active outputs and then
1962 * returns a pointer to the original atomic state to the caller. Drivers can
1963 * pass this pointer to the drm_atomic_helper_resume() helper upon resume to
1964 * restore the output configuration that was active at the time the system
1967 * Note that it is potentially unsafe to use this. The atomic state object
1968 * returned by this function is assumed to be persistent. Drivers must ensure
1969 * that this holds true. Before calling this function, drivers must make sure
1970 * to suspend fbdev emulation so that nothing can be using the device.
1973 * A pointer to a copy of the state before suspend on success or an ERR_PTR()-
1974 * encoded error code on failure. Drivers should store the returned atomic
1975 * state object and pass it to the drm_atomic_helper_resume() helper upon
1979 * drm_atomic_helper_duplicate_state(), drm_atomic_helper_disable_all(),
1980 * drm_atomic_helper_resume()
1982 struct drm_atomic_state
*drm_atomic_helper_suspend(struct drm_device
*dev
)
1984 struct drm_modeset_acquire_ctx ctx
;
1985 struct drm_atomic_state
*state
;
1988 drm_modeset_acquire_init(&ctx
, 0);
1991 err
= drm_modeset_lock_all_ctx(dev
, &ctx
);
1993 state
= ERR_PTR(err
);
1997 state
= drm_atomic_helper_duplicate_state(dev
, &ctx
);
2001 err
= drm_atomic_helper_disable_all(dev
, &ctx
);
2003 drm_atomic_state_free(state
);
2004 state
= ERR_PTR(err
);
2009 if (PTR_ERR(state
) == -EDEADLK
) {
2010 drm_modeset_backoff(&ctx
);
2014 drm_modeset_drop_locks(&ctx
);
2015 drm_modeset_acquire_fini(&ctx
);
2018 EXPORT_SYMBOL(drm_atomic_helper_suspend
);
2021 * drm_atomic_helper_resume - subsystem-level resume helper
2023 * @state: atomic state to resume to
2025 * Calls drm_mode_config_reset() to synchronize hardware and software states,
2026 * grabs all modeset locks and commits the atomic state object. This can be
2027 * used in conjunction with the drm_atomic_helper_suspend() helper to
2028 * implement suspend/resume for drivers that support atomic mode-setting.
2031 * 0 on success or a negative error code on failure.
2034 * drm_atomic_helper_suspend()
2036 int drm_atomic_helper_resume(struct drm_device
*dev
,
2037 struct drm_atomic_state
*state
)
2039 struct drm_mode_config
*config
= &dev
->mode_config
;
2042 drm_mode_config_reset(dev
);
2043 drm_modeset_lock_all(dev
);
2044 state
->acquire_ctx
= config
->acquire_ctx
;
2045 err
= drm_atomic_commit(state
);
2046 drm_modeset_unlock_all(dev
);
2050 EXPORT_SYMBOL(drm_atomic_helper_resume
);
2053 * drm_atomic_helper_crtc_set_property - helper for crtc properties
2055 * @property: DRM property
2056 * @val: value of property
2058 * Provides a default crtc set_property handler using the atomic driver
2062 * Zero on success, error code on failure
2065 drm_atomic_helper_crtc_set_property(struct drm_crtc
*crtc
,
2066 struct drm_property
*property
,
2069 struct drm_atomic_state
*state
;
2070 struct drm_crtc_state
*crtc_state
;
2073 state
= drm_atomic_state_alloc(crtc
->dev
);
2077 /* ->set_property is always called with all locks held. */
2078 state
->acquire_ctx
= crtc
->dev
->mode_config
.acquire_ctx
;
2080 crtc_state
= drm_atomic_get_crtc_state(state
, crtc
);
2081 if (IS_ERR(crtc_state
)) {
2082 ret
= PTR_ERR(crtc_state
);
2086 ret
= drm_atomic_crtc_set_property(crtc
, crtc_state
,
2091 ret
= drm_atomic_commit(state
);
2095 /* Driver takes ownership of state on successful commit. */
2098 if (ret
== -EDEADLK
)
2101 drm_atomic_state_free(state
);
2105 drm_atomic_state_clear(state
);
2106 drm_atomic_legacy_backoff(state
);
2110 EXPORT_SYMBOL(drm_atomic_helper_crtc_set_property
);
2113 * drm_atomic_helper_plane_set_property - helper for plane properties
2115 * @property: DRM property
2116 * @val: value of property
2118 * Provides a default plane set_property handler using the atomic driver
2122 * Zero on success, error code on failure
2125 drm_atomic_helper_plane_set_property(struct drm_plane
*plane
,
2126 struct drm_property
*property
,
2129 struct drm_atomic_state
*state
;
2130 struct drm_plane_state
*plane_state
;
2133 state
= drm_atomic_state_alloc(plane
->dev
);
2137 /* ->set_property is always called with all locks held. */
2138 state
->acquire_ctx
= plane
->dev
->mode_config
.acquire_ctx
;
2140 plane_state
= drm_atomic_get_plane_state(state
, plane
);
2141 if (IS_ERR(plane_state
)) {
2142 ret
= PTR_ERR(plane_state
);
2146 ret
= drm_atomic_plane_set_property(plane
, plane_state
,
2151 ret
= drm_atomic_commit(state
);
2155 /* Driver takes ownership of state on successful commit. */
2158 if (ret
== -EDEADLK
)
2161 drm_atomic_state_free(state
);
2165 drm_atomic_state_clear(state
);
2166 drm_atomic_legacy_backoff(state
);
2170 EXPORT_SYMBOL(drm_atomic_helper_plane_set_property
);
2173 * drm_atomic_helper_connector_set_property - helper for connector properties
2174 * @connector: DRM connector
2175 * @property: DRM property
2176 * @val: value of property
2178 * Provides a default connector set_property handler using the atomic driver
2182 * Zero on success, error code on failure
2185 drm_atomic_helper_connector_set_property(struct drm_connector
*connector
,
2186 struct drm_property
*property
,
2189 struct drm_atomic_state
*state
;
2190 struct drm_connector_state
*connector_state
;
2193 state
= drm_atomic_state_alloc(connector
->dev
);
2197 /* ->set_property is always called with all locks held. */
2198 state
->acquire_ctx
= connector
->dev
->mode_config
.acquire_ctx
;
2200 connector_state
= drm_atomic_get_connector_state(state
, connector
);
2201 if (IS_ERR(connector_state
)) {
2202 ret
= PTR_ERR(connector_state
);
2206 ret
= drm_atomic_connector_set_property(connector
, connector_state
,
2211 ret
= drm_atomic_commit(state
);
2215 /* Driver takes ownership of state on successful commit. */
2218 if (ret
== -EDEADLK
)
2221 drm_atomic_state_free(state
);
2225 drm_atomic_state_clear(state
);
2226 drm_atomic_legacy_backoff(state
);
2230 EXPORT_SYMBOL(drm_atomic_helper_connector_set_property
);
2233 * drm_atomic_helper_page_flip - execute a legacy page flip
2235 * @fb: DRM framebuffer
2236 * @event: optional DRM event to signal upon completion
2237 * @flags: flip flags for non-vblank sync'ed updates
2239 * Provides a default page flip implementation using the atomic driver interface.
2241 * Note that for now so called async page flips (i.e. updates which are not
2242 * synchronized to vblank) are not supported, since the atomic interfaces have
2243 * no provisions for this yet.
2246 * Returns 0 on success, negative errno numbers on failure.
2248 int drm_atomic_helper_page_flip(struct drm_crtc
*crtc
,
2249 struct drm_framebuffer
*fb
,
2250 struct drm_pending_vblank_event
*event
,
2253 struct drm_plane
*plane
= crtc
->primary
;
2254 struct drm_atomic_state
*state
;
2255 struct drm_plane_state
*plane_state
;
2256 struct drm_crtc_state
*crtc_state
;
2259 if (flags
& DRM_MODE_PAGE_FLIP_ASYNC
)
2262 state
= drm_atomic_state_alloc(plane
->dev
);
2266 state
->acquire_ctx
= drm_modeset_legacy_acquire_ctx(crtc
);
2268 crtc_state
= drm_atomic_get_crtc_state(state
, crtc
);
2269 if (IS_ERR(crtc_state
)) {
2270 ret
= PTR_ERR(crtc_state
);
2273 crtc_state
->event
= event
;
2275 plane_state
= drm_atomic_get_plane_state(state
, plane
);
2276 if (IS_ERR(plane_state
)) {
2277 ret
= PTR_ERR(plane_state
);
2281 ret
= drm_atomic_set_crtc_for_plane(plane_state
, crtc
);
2284 drm_atomic_set_fb_for_plane(plane_state
, fb
);
2286 ret
= drm_atomic_async_commit(state
);
2290 /* Driver takes ownership of state on successful async commit. */
2293 if (ret
== -EDEADLK
)
2296 drm_atomic_state_free(state
);
2300 drm_atomic_state_clear(state
);
2301 drm_atomic_legacy_backoff(state
);
2304 * Someone might have exchanged the framebuffer while we dropped locks
2305 * in the backoff code. We need to fix up the fb refcount tracking the
2308 plane
->old_fb
= plane
->fb
;
2312 EXPORT_SYMBOL(drm_atomic_helper_page_flip
);
2315 * drm_atomic_helper_connector_dpms() - connector dpms helper implementation
2316 * @connector: affected connector
2319 * This is the main helper function provided by the atomic helper framework for
2320 * implementing the legacy DPMS connector interface. It computes the new desired
2321 * ->active state for the corresponding CRTC (if the connector is enabled) and
2325 * Returns 0 on success, negative errno numbers on failure.
2327 int drm_atomic_helper_connector_dpms(struct drm_connector
*connector
,
2330 struct drm_mode_config
*config
= &connector
->dev
->mode_config
;
2331 struct drm_atomic_state
*state
;
2332 struct drm_crtc_state
*crtc_state
;
2333 struct drm_crtc
*crtc
;
2334 struct drm_connector
*tmp_connector
;
2336 bool active
= false;
2337 int old_mode
= connector
->dpms
;
2339 if (mode
!= DRM_MODE_DPMS_ON
)
2340 mode
= DRM_MODE_DPMS_OFF
;
2342 connector
->dpms
= mode
;
2343 crtc
= connector
->state
->crtc
;
2348 state
= drm_atomic_state_alloc(connector
->dev
);
2352 state
->acquire_ctx
= drm_modeset_legacy_acquire_ctx(crtc
);
2354 crtc_state
= drm_atomic_get_crtc_state(state
, crtc
);
2355 if (IS_ERR(crtc_state
)) {
2356 ret
= PTR_ERR(crtc_state
);
2360 WARN_ON(!drm_modeset_is_locked(&config
->connection_mutex
));
2362 drm_for_each_connector(tmp_connector
, connector
->dev
) {
2363 if (tmp_connector
->state
->crtc
!= crtc
)
2366 if (tmp_connector
->dpms
== DRM_MODE_DPMS_ON
) {
2371 crtc_state
->active
= active
;
2373 ret
= drm_atomic_commit(state
);
2377 /* Driver takes ownership of state on successful commit. */
2380 if (ret
== -EDEADLK
)
2383 connector
->dpms
= old_mode
;
2384 drm_atomic_state_free(state
);
2388 drm_atomic_state_clear(state
);
2389 drm_atomic_legacy_backoff(state
);
2393 EXPORT_SYMBOL(drm_atomic_helper_connector_dpms
);
2396 * DOC: atomic state reset and initialization
2398 * Both the drm core and the atomic helpers assume that there is always the full
2399 * and correct atomic software state for all connectors, CRTCs and planes
2400 * available. Which is a bit a problem on driver load and also after system
2401 * suspend. One way to solve this is to have a hardware state read-out
2402 * infrastructure which reconstructs the full software state (e.g. the i915
2405 * The simpler solution is to just reset the software state to everything off,
2406 * which is easiest to do by calling drm_mode_config_reset(). To facilitate this
2407 * the atomic helpers provide default reset implementations for all hooks.
2409 * On the upside the precise state tracking of atomic simplifies system suspend
2410 * and resume a lot. For drivers using drm_mode_config_reset() a complete recipe
2411 * is implemented in drm_atomic_helper_suspend() and drm_atomic_helper_resume().
2412 * For other drivers the building blocks are split out, see the documentation
2413 * for these functions.
2417 * drm_atomic_helper_crtc_reset - default ->reset hook for CRTCs
2420 * Resets the atomic state for @crtc by freeing the state pointer (which might
2421 * be NULL, e.g. at driver load time) and allocating a new empty state object.
2423 void drm_atomic_helper_crtc_reset(struct drm_crtc
*crtc
)
2426 drm_property_unreference_blob(crtc
->state
->mode_blob
);
2428 crtc
->state
= kzalloc(sizeof(*crtc
->state
), GFP_KERNEL
);
2431 crtc
->state
->crtc
= crtc
;
2433 EXPORT_SYMBOL(drm_atomic_helper_crtc_reset
);
2436 * __drm_atomic_helper_crtc_duplicate_state - copy atomic CRTC state
2437 * @crtc: CRTC object
2438 * @state: atomic CRTC state
2440 * Copies atomic state from a CRTC's current state and resets inferred values.
2441 * This is useful for drivers that subclass the CRTC state.
2443 void __drm_atomic_helper_crtc_duplicate_state(struct drm_crtc
*crtc
,
2444 struct drm_crtc_state
*state
)
2446 memcpy(state
, crtc
->state
, sizeof(*state
));
2448 if (state
->mode_blob
)
2449 drm_property_reference_blob(state
->mode_blob
);
2450 state
->mode_changed
= false;
2451 state
->active_changed
= false;
2452 state
->planes_changed
= false;
2453 state
->connectors_changed
= false;
2454 state
->event
= NULL
;
2456 EXPORT_SYMBOL(__drm_atomic_helper_crtc_duplicate_state
);
2459 * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
2462 * Default CRTC state duplicate hook for drivers which don't have their own
2463 * subclassed CRTC state structure.
2465 struct drm_crtc_state
*
2466 drm_atomic_helper_crtc_duplicate_state(struct drm_crtc
*crtc
)
2468 struct drm_crtc_state
*state
;
2470 if (WARN_ON(!crtc
->state
))
2473 state
= kmalloc(sizeof(*state
), GFP_KERNEL
);
2475 __drm_atomic_helper_crtc_duplicate_state(crtc
, state
);
2479 EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state
);
2482 * __drm_atomic_helper_crtc_destroy_state - release CRTC state
2483 * @crtc: CRTC object
2484 * @state: CRTC state object to release
2486 * Releases all resources stored in the CRTC state without actually freeing
2487 * the memory of the CRTC state. This is useful for drivers that subclass the
2490 void __drm_atomic_helper_crtc_destroy_state(struct drm_crtc
*crtc
,
2491 struct drm_crtc_state
*state
)
2493 drm_property_unreference_blob(state
->mode_blob
);
2495 EXPORT_SYMBOL(__drm_atomic_helper_crtc_destroy_state
);
2498 * drm_atomic_helper_crtc_destroy_state - default state destroy hook
2500 * @state: CRTC state object to release
2502 * Default CRTC state destroy hook for drivers which don't have their own
2503 * subclassed CRTC state structure.
2505 void drm_atomic_helper_crtc_destroy_state(struct drm_crtc
*crtc
,
2506 struct drm_crtc_state
*state
)
2508 __drm_atomic_helper_crtc_destroy_state(crtc
, state
);
2511 EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state
);
2514 * drm_atomic_helper_plane_reset - default ->reset hook for planes
2517 * Resets the atomic state for @plane by freeing the state pointer (which might
2518 * be NULL, e.g. at driver load time) and allocating a new empty state object.
2520 void drm_atomic_helper_plane_reset(struct drm_plane
*plane
)
2522 if (plane
->state
&& plane
->state
->fb
)
2523 drm_framebuffer_unreference(plane
->state
->fb
);
2525 kfree(plane
->state
);
2526 plane
->state
= kzalloc(sizeof(*plane
->state
), GFP_KERNEL
);
2529 plane
->state
->plane
= plane
;
2531 EXPORT_SYMBOL(drm_atomic_helper_plane_reset
);
2534 * __drm_atomic_helper_plane_duplicate_state - copy atomic plane state
2535 * @plane: plane object
2536 * @state: atomic plane state
2538 * Copies atomic state from a plane's current state. This is useful for
2539 * drivers that subclass the plane state.
2541 void __drm_atomic_helper_plane_duplicate_state(struct drm_plane
*plane
,
2542 struct drm_plane_state
*state
)
2544 memcpy(state
, plane
->state
, sizeof(*state
));
2547 drm_framebuffer_reference(state
->fb
);
2549 EXPORT_SYMBOL(__drm_atomic_helper_plane_duplicate_state
);
2552 * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
2555 * Default plane state duplicate hook for drivers which don't have their own
2556 * subclassed plane state structure.
2558 struct drm_plane_state
*
2559 drm_atomic_helper_plane_duplicate_state(struct drm_plane
*plane
)
2561 struct drm_plane_state
*state
;
2563 if (WARN_ON(!plane
->state
))
2566 state
= kmalloc(sizeof(*state
), GFP_KERNEL
);
2568 __drm_atomic_helper_plane_duplicate_state(plane
, state
);
2572 EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state
);
2575 * __drm_atomic_helper_plane_destroy_state - release plane state
2576 * @plane: plane object
2577 * @state: plane state object to release
2579 * Releases all resources stored in the plane state without actually freeing
2580 * the memory of the plane state. This is useful for drivers that subclass the
2583 void __drm_atomic_helper_plane_destroy_state(struct drm_plane
*plane
,
2584 struct drm_plane_state
*state
)
2587 drm_framebuffer_unreference(state
->fb
);
2589 EXPORT_SYMBOL(__drm_atomic_helper_plane_destroy_state
);
2592 * drm_atomic_helper_plane_destroy_state - default state destroy hook
2594 * @state: plane state object to release
2596 * Default plane state destroy hook for drivers which don't have their own
2597 * subclassed plane state structure.
2599 void drm_atomic_helper_plane_destroy_state(struct drm_plane
*plane
,
2600 struct drm_plane_state
*state
)
2602 __drm_atomic_helper_plane_destroy_state(plane
, state
);
2605 EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state
);
2608 * drm_atomic_helper_connector_reset - default ->reset hook for connectors
2609 * @connector: drm connector
2611 * Resets the atomic state for @connector by freeing the state pointer (which
2612 * might be NULL, e.g. at driver load time) and allocating a new empty state
2615 void drm_atomic_helper_connector_reset(struct drm_connector
*connector
)
2617 kfree(connector
->state
);
2618 connector
->state
= kzalloc(sizeof(*connector
->state
), GFP_KERNEL
);
2620 if (connector
->state
)
2621 connector
->state
->connector
= connector
;
2623 EXPORT_SYMBOL(drm_atomic_helper_connector_reset
);
2626 * __drm_atomic_helper_connector_duplicate_state - copy atomic connector state
2627 * @connector: connector object
2628 * @state: atomic connector state
2630 * Copies atomic state from a connector's current state. This is useful for
2631 * drivers that subclass the connector state.
2634 __drm_atomic_helper_connector_duplicate_state(struct drm_connector
*connector
,
2635 struct drm_connector_state
*state
)
2637 memcpy(state
, connector
->state
, sizeof(*state
));
2639 EXPORT_SYMBOL(__drm_atomic_helper_connector_duplicate_state
);
2642 * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
2643 * @connector: drm connector
2645 * Default connector state duplicate hook for drivers which don't have their own
2646 * subclassed connector state structure.
2648 struct drm_connector_state
*
2649 drm_atomic_helper_connector_duplicate_state(struct drm_connector
*connector
)
2651 struct drm_connector_state
*state
;
2653 if (WARN_ON(!connector
->state
))
2656 state
= kmalloc(sizeof(*state
), GFP_KERNEL
);
2658 __drm_atomic_helper_connector_duplicate_state(connector
, state
);
2662 EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state
);
2665 * drm_atomic_helper_duplicate_state - duplicate an atomic state object
2667 * @ctx: lock acquisition context
2669 * Makes a copy of the current atomic state by looping over all objects and
2670 * duplicating their respective states. This is used for example by suspend/
2671 * resume support code to save the state prior to suspend such that it can
2672 * be restored upon resume.
2674 * Note that this treats atomic state as persistent between save and restore.
2675 * Drivers must make sure that this is possible and won't result in confusion
2676 * or erroneous behaviour.
2678 * Note that if callers haven't already acquired all modeset locks this might
2679 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
2682 * A pointer to the copy of the atomic state object on success or an
2683 * ERR_PTR()-encoded error code on failure.
2686 * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
2688 struct drm_atomic_state
*
2689 drm_atomic_helper_duplicate_state(struct drm_device
*dev
,
2690 struct drm_modeset_acquire_ctx
*ctx
)
2692 struct drm_atomic_state
*state
;
2693 struct drm_connector
*conn
;
2694 struct drm_plane
*plane
;
2695 struct drm_crtc
*crtc
;
2698 state
= drm_atomic_state_alloc(dev
);
2700 return ERR_PTR(-ENOMEM
);
2702 state
->acquire_ctx
= ctx
;
2704 drm_for_each_crtc(crtc
, dev
) {
2705 struct drm_crtc_state
*crtc_state
;
2707 crtc_state
= drm_atomic_get_crtc_state(state
, crtc
);
2708 if (IS_ERR(crtc_state
)) {
2709 err
= PTR_ERR(crtc_state
);
2714 drm_for_each_plane(plane
, dev
) {
2715 struct drm_plane_state
*plane_state
;
2717 plane_state
= drm_atomic_get_plane_state(state
, plane
);
2718 if (IS_ERR(plane_state
)) {
2719 err
= PTR_ERR(plane_state
);
2724 drm_for_each_connector(conn
, dev
) {
2725 struct drm_connector_state
*conn_state
;
2727 conn_state
= drm_atomic_get_connector_state(state
, conn
);
2728 if (IS_ERR(conn_state
)) {
2729 err
= PTR_ERR(conn_state
);
2734 /* clear the acquire context so that it isn't accidentally reused */
2735 state
->acquire_ctx
= NULL
;
2739 drm_atomic_state_free(state
);
2740 state
= ERR_PTR(err
);
2745 EXPORT_SYMBOL(drm_atomic_helper_duplicate_state
);
2748 * __drm_atomic_helper_connector_destroy_state - release connector state
2749 * @connector: connector object
2750 * @state: connector state object to release
2752 * Releases all resources stored in the connector state without actually
2753 * freeing the memory of the connector state. This is useful for drivers that
2754 * subclass the connector state.
2757 __drm_atomic_helper_connector_destroy_state(struct drm_connector
*connector
,
2758 struct drm_connector_state
*state
)
2761 * This is currently a placeholder so that drivers that subclass the
2762 * state will automatically do the right thing if code is ever added
2766 EXPORT_SYMBOL(__drm_atomic_helper_connector_destroy_state
);
2769 * drm_atomic_helper_connector_destroy_state - default state destroy hook
2770 * @connector: drm connector
2771 * @state: connector state object to release
2773 * Default connector state destroy hook for drivers which don't have their own
2774 * subclassed connector state structure.
2776 void drm_atomic_helper_connector_destroy_state(struct drm_connector
*connector
,
2777 struct drm_connector_state
*state
)
2779 __drm_atomic_helper_connector_destroy_state(connector
, state
);
2782 EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state
);