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.
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;
113 * We can only steal an encoder coming from a connector, which means we
114 * must already hold the connection_mutex.
116 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
118 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d], stealing it\n",
119 encoder->base.id, encoder->name,
120 encoder_crtc->base.id);
122 crtc_state = drm_atomic_get_crtc_state(state, encoder_crtc);
123 if (IS_ERR(crtc_state))
124 return PTR_ERR(crtc_state);
126 crtc_state->connectors_changed = true;
128 list_for_each_entry(connector, &config->connector_list, head) {
129 if (connector->state->best_encoder != encoder)
132 DRM_DEBUG_ATOMIC("Stealing encoder from [CONNECTOR:%d:%s]\n",
136 connector_state = drm_atomic_get_connector_state(state,
138 if (IS_ERR(connector_state))
139 return PTR_ERR(connector_state);
141 connector_state->best_encoder = NULL;
148 update_connector_routing(struct drm_atomic_state *state, int conn_idx)
150 const struct drm_connector_helper_funcs *funcs;
151 struct drm_encoder *new_encoder;
152 struct drm_crtc *encoder_crtc;
153 struct drm_connector *connector;
154 struct drm_connector_state *connector_state;
155 struct drm_crtc_state *crtc_state;
158 connector = state->connectors[conn_idx];
159 connector_state = state->connector_states[conn_idx];
164 DRM_DEBUG_ATOMIC("Updating routing for [CONNECTOR:%d:%s]\n",
168 if (connector->state->crtc != connector_state->crtc) {
169 if (connector->state->crtc) {
170 idx = drm_crtc_index(connector->state->crtc);
172 crtc_state = state->crtc_states[idx];
173 crtc_state->connectors_changed = true;
176 if (connector_state->crtc) {
177 idx = drm_crtc_index(connector_state->crtc);
179 crtc_state = state->crtc_states[idx];
180 crtc_state->connectors_changed = true;
184 if (!connector_state->crtc) {
185 DRM_DEBUG_ATOMIC("Disabling [CONNECTOR:%d:%s]\n",
189 connector_state->best_encoder = NULL;
194 funcs = connector->helper_private;
196 if (funcs->atomic_best_encoder)
197 new_encoder = funcs->atomic_best_encoder(connector,
200 new_encoder = funcs->best_encoder(connector);
203 DRM_DEBUG_ATOMIC("No suitable encoder found for [CONNECTOR:%d:%s]\n",
209 if (!drm_encoder_crtc_ok(new_encoder, connector_state->crtc)) {
210 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] incompatible with [CRTC:%d]\n",
211 new_encoder->base.id,
213 connector_state->crtc->base.id);
217 if (new_encoder == connector_state->best_encoder) {
218 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] keeps [ENCODER:%d:%s], now on [CRTC:%d]\n",
221 new_encoder->base.id,
223 connector_state->crtc->base.id);
228 encoder_crtc = get_current_crtc_for_encoder(state->dev,
232 ret = steal_encoder(state, new_encoder, encoder_crtc);
234 DRM_DEBUG_ATOMIC("Encoder stealing failed for [CONNECTOR:%d:%s]\n",
241 if (WARN_ON(!connector_state->crtc))
244 connector_state->best_encoder = new_encoder;
245 idx = drm_crtc_index(connector_state->crtc);
247 crtc_state = state->crtc_states[idx];
248 crtc_state->connectors_changed = true;
250 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] using [ENCODER:%d:%s] on [CRTC:%d]\n",
253 new_encoder->base.id,
255 connector_state->crtc->base.id);
261 mode_fixup(struct drm_atomic_state *state)
263 struct drm_crtc *crtc;
264 struct drm_crtc_state *crtc_state;
265 struct drm_connector *connector;
266 struct drm_connector_state *conn_state;
270 for_each_crtc_in_state(state, crtc, crtc_state, i) {
271 if (!crtc_state->mode_changed &&
272 !crtc_state->connectors_changed)
275 drm_mode_copy(&crtc_state->adjusted_mode, &crtc_state->mode);
278 for_each_connector_in_state(state, connector, conn_state, i) {
279 const struct drm_encoder_helper_funcs *funcs;
280 struct drm_encoder *encoder;
282 WARN_ON(!!conn_state->best_encoder != !!conn_state->crtc);
284 if (!conn_state->crtc || !conn_state->best_encoder)
288 state->crtc_states[drm_crtc_index(conn_state->crtc)];
291 * Each encoder has at most one connector (since we always steal
292 * it away), so we won't call ->mode_fixup twice.
294 encoder = conn_state->best_encoder;
295 funcs = encoder->helper_private;
299 ret = drm_bridge_mode_fixup(encoder->bridge, &crtc_state->mode,
300 &crtc_state->adjusted_mode);
302 DRM_DEBUG_ATOMIC("Bridge fixup failed\n");
306 if (funcs->atomic_check) {
307 ret = funcs->atomic_check(encoder, crtc_state,
310 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] check failed\n",
311 encoder->base.id, encoder->name);
314 } else if (funcs->mode_fixup) {
315 ret = funcs->mode_fixup(encoder, &crtc_state->mode,
316 &crtc_state->adjusted_mode);
318 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] fixup failed\n",
319 encoder->base.id, encoder->name);
325 for_each_crtc_in_state(state, crtc, crtc_state, i) {
326 const struct drm_crtc_helper_funcs *funcs;
328 if (!crtc_state->mode_changed &&
329 !crtc_state->connectors_changed)
332 funcs = crtc->helper_private;
333 if (!funcs->mode_fixup)
336 ret = funcs->mode_fixup(crtc, &crtc_state->mode,
337 &crtc_state->adjusted_mode);
339 DRM_DEBUG_ATOMIC("[CRTC:%d] fixup failed\n",
349 * drm_atomic_helper_check_modeset - validate state object for modeset changes
351 * @state: the driver state object
353 * Check the state object to see if the requested state is physically possible.
354 * This does all the crtc and connector related computations for an atomic
355 * update and adds any additional connectors needed for full modesets and calls
356 * down into ->mode_fixup functions of the driver backend.
358 * crtc_state->mode_changed is set when the input mode is changed.
359 * crtc_state->connectors_changed is set when a connector is added or
360 * removed from the crtc.
361 * crtc_state->active_changed is set when crtc_state->active changes,
362 * which is used for dpms.
366 * Drivers which update ->mode_changed (e.g. in their ->atomic_check hooks if a
367 * plane update can't be done without a full modeset) _must_ call this function
368 * afterwards after that change. It is permitted to call this function multiple
369 * times for the same update, e.g. when the ->atomic_check functions depend upon
370 * the adjusted dotclock for fifo space allocation and watermark computation.
373 * Zero for success or -errno
376 drm_atomic_helper_check_modeset(struct drm_device *dev,
377 struct drm_atomic_state *state)
379 struct drm_crtc *crtc;
380 struct drm_crtc_state *crtc_state;
381 struct drm_connector *connector;
382 struct drm_connector_state *connector_state;
385 for_each_crtc_in_state(state, crtc, crtc_state, i) {
386 if (!drm_mode_equal(&crtc->state->mode, &crtc_state->mode)) {
387 DRM_DEBUG_ATOMIC("[CRTC:%d] mode changed\n",
389 crtc_state->mode_changed = true;
392 if (crtc->state->enable != crtc_state->enable) {
393 DRM_DEBUG_ATOMIC("[CRTC:%d] enable changed\n",
397 * For clarity this assignment is done here, but
398 * enable == 0 is only true when there are no
399 * connectors and a NULL mode.
401 * The other way around is true as well. enable != 0
402 * iff connectors are attached and a mode is set.
404 crtc_state->mode_changed = true;
405 crtc_state->connectors_changed = true;
409 for_each_connector_in_state(state, connector, connector_state, i) {
411 * This only sets crtc->mode_changed for routing changes,
412 * drivers must set crtc->mode_changed themselves when connector
413 * properties need to be updated.
415 ret = update_connector_routing(state, i);
421 * After all the routing has been prepared we need to add in any
422 * connector which is itself unchanged, but who's crtc changes it's
423 * configuration. This must be done before calling mode_fixup in case a
424 * crtc only changed its mode but has the same set of connectors.
426 for_each_crtc_in_state(state, crtc, crtc_state, i) {
430 * We must set ->active_changed after walking connectors for
431 * otherwise an update that only changes active would result in
432 * a full modeset because update_connector_routing force that.
434 if (crtc->state->active != crtc_state->active) {
435 DRM_DEBUG_ATOMIC("[CRTC:%d] active changed\n",
437 crtc_state->active_changed = true;
440 if (!drm_atomic_crtc_needs_modeset(crtc_state))
443 DRM_DEBUG_ATOMIC("[CRTC:%d] needs all connectors, enable: %c, active: %c\n",
445 crtc_state->enable ? 'y' : 'n',
446 crtc_state->active ? 'y' : 'n');
448 ret = drm_atomic_add_affected_connectors(state, crtc);
452 ret = drm_atomic_add_affected_planes(state, crtc);
456 num_connectors = drm_atomic_connectors_for_crtc(state,
459 if (crtc_state->enable != !!num_connectors) {
460 DRM_DEBUG_ATOMIC("[CRTC:%d] enabled/connectors mismatch\n",
467 return mode_fixup(state);
469 EXPORT_SYMBOL(drm_atomic_helper_check_modeset);
472 * drm_atomic_helper_check_planes - validate state object for planes changes
474 * @state: the driver state object
476 * Check the state object to see if the requested state is physically possible.
477 * This does all the plane update related checks using by calling into the
478 * ->atomic_check hooks provided by the driver.
480 * It also sets crtc_state->planes_changed to indicate that a crtc has
484 * Zero for success or -errno
487 drm_atomic_helper_check_planes(struct drm_device *dev,
488 struct drm_atomic_state *state)
490 struct drm_crtc *crtc;
491 struct drm_crtc_state *crtc_state;
492 struct drm_plane *plane;
493 struct drm_plane_state *plane_state;
496 for_each_plane_in_state(state, plane, plane_state, i) {
497 const struct drm_plane_helper_funcs *funcs;
499 funcs = plane->helper_private;
501 drm_atomic_helper_plane_changed(state, plane_state, plane);
503 if (!funcs || !funcs->atomic_check)
506 ret = funcs->atomic_check(plane, plane_state);
508 DRM_DEBUG_ATOMIC("[PLANE:%d] atomic driver check failed\n",
514 for_each_crtc_in_state(state, crtc, crtc_state, i) {
515 const struct drm_crtc_helper_funcs *funcs;
517 funcs = crtc->helper_private;
519 if (!funcs || !funcs->atomic_check)
522 ret = funcs->atomic_check(crtc, state->crtc_states[i]);
524 DRM_DEBUG_ATOMIC("[CRTC:%d] atomic driver check failed\n",
532 EXPORT_SYMBOL(drm_atomic_helper_check_planes);
535 * drm_atomic_helper_check - validate state object
537 * @state: the driver state object
539 * Check the state object to see if the requested state is physically possible.
540 * Only crtcs and planes have check callbacks, so for any additional (global)
541 * checking that a driver needs it can simply wrap that around this function.
542 * Drivers without such needs can directly use this as their ->atomic_check()
545 * This just wraps the two parts of the state checking for planes and modeset
546 * state in the default order: First it calls drm_atomic_helper_check_modeset()
547 * and then drm_atomic_helper_check_planes(). The assumption is that the
548 * ->atomic_check functions depend upon an updated adjusted_mode.clock to
549 * e.g. properly compute watermarks.
552 * Zero for success or -errno
554 int drm_atomic_helper_check(struct drm_device *dev,
555 struct drm_atomic_state *state)
559 ret = drm_atomic_helper_check_modeset(dev, state);
563 ret = drm_atomic_helper_check_planes(dev, state);
569 EXPORT_SYMBOL(drm_atomic_helper_check);
572 disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state)
574 struct drm_connector *connector;
575 struct drm_connector_state *old_conn_state;
576 struct drm_crtc *crtc;
577 struct drm_crtc_state *old_crtc_state;
580 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
581 const struct drm_encoder_helper_funcs *funcs;
582 const struct drm_connector_helper_funcs *conn_funcs;
583 struct drm_encoder *encoder;
584 struct drm_crtc_state *old_crtc_state;
586 /* Shut down everything that's in the changeset and currently
587 * still on. So need to check the old, saved state. */
588 if (!old_conn_state->crtc)
591 old_crtc_state = old_state->crtc_states[drm_crtc_index(old_conn_state->crtc)];
593 if (!old_crtc_state->active ||
594 !drm_atomic_crtc_needs_modeset(old_conn_state->crtc->state))
597 encoder = old_conn_state->best_encoder;
599 /* We shouldn't get this far if we didn't previously have
600 * an encoder.. but WARN_ON() rather than explode.
602 if (WARN_ON(!encoder))
605 funcs = encoder->helper_private;
607 DRM_DEBUG_ATOMIC("disabling [ENCODER:%d:%s]\n",
608 encoder->base.id, encoder->name);
610 conn_funcs = connector->helper_private;
611 if (connector->loader_protect) {
612 drm_bridge_pre_enable(encoder->bridge);
615 funcs->enable(encoder);
617 funcs->commit(encoder);
619 drm_bridge_enable(encoder->bridge);
621 if (conn_funcs->loader_protect)
622 conn_funcs->loader_protect(connector, false);
623 connector->loader_protect = false;
626 * Each encoder has at most one connector (since we always steal
627 * it away), so we won't call disable hooks twice.
629 drm_bridge_disable(encoder->bridge);
631 /* Right function depends upon target state. */
632 if (connector->state->crtc && funcs->prepare)
633 funcs->prepare(encoder);
634 else if (funcs->disable)
635 funcs->disable(encoder);
637 funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
639 drm_bridge_post_disable(encoder->bridge);
642 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
643 const struct drm_crtc_helper_funcs *funcs;
645 /* Shut down everything that needs a full modeset. */
646 if (!drm_atomic_crtc_needs_modeset(crtc->state))
649 if (!old_crtc_state->active)
652 funcs = crtc->helper_private;
654 DRM_DEBUG_ATOMIC("disabling [CRTC:%d]\n",
658 /* Right function depends upon target state. */
659 if (crtc->state->enable && funcs->prepare)
660 funcs->prepare(crtc);
661 else if (funcs->disable)
662 funcs->disable(crtc);
664 funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
669 * drm_atomic_helper_update_legacy_modeset_state - update legacy modeset state
671 * @old_state: atomic state object with old state structures
673 * This function updates all the various legacy modeset state pointers in
674 * connectors, encoders and crtcs. It also updates the timestamping constants
675 * used for precise vblank timestamps by calling
676 * drm_calc_timestamping_constants().
678 * Drivers can use this for building their own atomic commit if they don't have
679 * a pure helper-based modeset implementation.
682 drm_atomic_helper_update_legacy_modeset_state(struct drm_device *dev,
683 struct drm_atomic_state *old_state)
685 struct drm_connector *connector;
686 struct drm_connector_state *old_conn_state;
687 struct drm_crtc *crtc;
688 struct drm_crtc_state *old_crtc_state;
691 /* clear out existing links and update dpms */
692 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
693 if (connector->encoder) {
694 WARN_ON(!connector->encoder->crtc);
696 connector->encoder->crtc = NULL;
697 connector->encoder = NULL;
700 crtc = connector->state->crtc;
701 if ((!crtc && old_conn_state->crtc) ||
702 (crtc && drm_atomic_crtc_needs_modeset(crtc->state))) {
703 struct drm_property *dpms_prop =
704 dev->mode_config.dpms_property;
705 int mode = DRM_MODE_DPMS_OFF;
707 if (crtc && crtc->state->active)
708 mode = DRM_MODE_DPMS_ON;
710 connector->dpms = mode;
711 drm_object_property_set_value(&connector->base,
717 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
718 if (!connector->state->crtc)
721 if (WARN_ON(!connector->state->best_encoder))
724 connector->encoder = connector->state->best_encoder;
725 connector->encoder->crtc = connector->state->crtc;
728 /* set legacy state in the crtc structure */
729 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
730 struct drm_plane *primary = crtc->primary;
732 crtc->mode = crtc->state->mode;
733 crtc->enabled = crtc->state->enable;
735 if (drm_atomic_get_existing_plane_state(old_state, primary) &&
736 primary->state->crtc == crtc) {
737 crtc->x = primary->state->src_x >> 16;
738 crtc->y = primary->state->src_y >> 16;
741 if (crtc->state->enable)
742 drm_calc_timestamping_constants(crtc,
743 &crtc->state->adjusted_mode);
746 EXPORT_SYMBOL(drm_atomic_helper_update_legacy_modeset_state);
749 crtc_set_mode(struct drm_device *dev, struct drm_atomic_state *old_state)
751 struct drm_crtc *crtc;
752 struct drm_crtc_state *old_crtc_state;
753 struct drm_connector *connector;
754 struct drm_connector_state *old_conn_state;
757 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
758 const struct drm_crtc_helper_funcs *funcs;
760 if (!crtc->state->mode_changed)
763 funcs = crtc->helper_private;
765 if (crtc->state->enable && funcs->mode_set_nofb) {
766 DRM_DEBUG_ATOMIC("modeset on [CRTC:%d]\n",
769 funcs->mode_set_nofb(crtc);
773 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
774 const struct drm_encoder_helper_funcs *funcs;
775 struct drm_crtc_state *new_crtc_state;
776 struct drm_encoder *encoder;
777 struct drm_display_mode *mode, *adjusted_mode;
779 if (!connector->state->best_encoder)
782 encoder = connector->state->best_encoder;
783 funcs = encoder->helper_private;
784 new_crtc_state = connector->state->crtc->state;
785 mode = &new_crtc_state->mode;
786 adjusted_mode = &new_crtc_state->adjusted_mode;
788 if (!new_crtc_state->mode_changed)
791 DRM_DEBUG_ATOMIC("modeset on [ENCODER:%d:%s]\n",
792 encoder->base.id, encoder->name);
795 * Each encoder has at most one connector (since we always steal
796 * it away), so we won't call mode_set hooks twice.
799 funcs->mode_set(encoder, mode, adjusted_mode);
801 drm_bridge_mode_set(encoder->bridge, mode, adjusted_mode);
806 * drm_atomic_helper_commit_modeset_disables - modeset commit to disable outputs
808 * @old_state: atomic state object with old state structures
810 * This function shuts down all the outputs that need to be shut down and
811 * prepares them (if required) with the new mode.
813 * For compatibility with legacy crtc helpers this should be called before
814 * drm_atomic_helper_commit_planes(), which is what the default commit function
815 * does. But drivers with different needs can group the modeset commits together
816 * and do the plane commits at the end. This is useful for drivers doing runtime
817 * PM since planes updates then only happen when the CRTC is actually enabled.
819 void drm_atomic_helper_commit_modeset_disables(struct drm_device *dev,
820 struct drm_atomic_state *old_state)
822 disable_outputs(dev, old_state);
824 drm_atomic_helper_update_legacy_modeset_state(dev, old_state);
826 crtc_set_mode(dev, old_state);
828 EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_disables);
831 * drm_atomic_helper_commit_modeset_enables - modeset commit to enable outputs
833 * @old_state: atomic state object with old state structures
835 * This function enables all the outputs with the new configuration which had to
836 * be turned off for the update.
838 * For compatibility with legacy crtc helpers this should be called after
839 * drm_atomic_helper_commit_planes(), which is what the default commit function
840 * does. But drivers with different needs can group the modeset commits together
841 * and do the plane commits at the end. This is useful for drivers doing runtime
842 * PM since planes updates then only happen when the CRTC is actually enabled.
844 void drm_atomic_helper_commit_modeset_enables(struct drm_device *dev,
845 struct drm_atomic_state *old_state)
847 struct drm_crtc *crtc;
848 struct drm_crtc_state *old_crtc_state;
849 struct drm_connector *connector;
850 struct drm_connector_state *old_conn_state;
853 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
854 const struct drm_crtc_helper_funcs *funcs;
856 /* Need to filter out CRTCs where only planes change. */
857 if (!drm_atomic_crtc_needs_modeset(crtc->state))
860 if (!crtc->state->active)
863 funcs = crtc->helper_private;
865 if (crtc->state->enable) {
866 DRM_DEBUG_ATOMIC("enabling [CRTC:%d]\n",
876 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
877 const struct drm_encoder_helper_funcs *funcs;
878 struct drm_encoder *encoder;
880 if (!connector->state->best_encoder)
883 if (!connector->state->crtc->state->active ||
884 !drm_atomic_crtc_needs_modeset(connector->state->crtc->state))
887 encoder = connector->state->best_encoder;
888 funcs = encoder->helper_private;
890 DRM_DEBUG_ATOMIC("enabling [ENCODER:%d:%s]\n",
891 encoder->base.id, encoder->name);
894 * Each encoder has at most one connector (since we always steal
895 * it away), so we won't call enable hooks twice.
897 drm_bridge_pre_enable(encoder->bridge);
900 funcs->enable(encoder);
902 funcs->commit(encoder);
904 drm_bridge_enable(encoder->bridge);
907 EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_enables);
909 static void wait_for_fences(struct drm_device *dev,
910 struct drm_atomic_state *state)
912 struct drm_plane *plane;
913 struct drm_plane_state *plane_state;
916 for_each_plane_in_state(state, plane, plane_state, i) {
917 if (!plane->state->fence)
920 WARN_ON(!plane->state->fb);
922 fence_wait(plane->state->fence, false);
923 fence_put(plane->state->fence);
924 plane->state->fence = NULL;
929 * drm_atomic_helper_framebuffer_changed - check if framebuffer has changed
931 * @old_state: atomic state object with old state structures
934 * Checks whether the framebuffer used for this CRTC changes as a result of
935 * the atomic update. This is useful for drivers which cannot use
936 * drm_atomic_helper_wait_for_vblanks() and need to reimplement its
940 * true if the framebuffer changed.
942 bool drm_atomic_helper_framebuffer_changed(struct drm_device *dev,
943 struct drm_atomic_state *old_state,
944 struct drm_crtc *crtc)
946 struct drm_plane *plane;
947 struct drm_plane_state *old_plane_state;
950 for_each_plane_in_state(old_state, plane, old_plane_state, i) {
951 if (plane->state->crtc != crtc &&
952 old_plane_state->crtc != crtc)
955 if (plane->state->fb != old_plane_state->fb)
961 EXPORT_SYMBOL(drm_atomic_helper_framebuffer_changed);
964 * drm_atomic_helper_wait_for_vblanks - wait for vblank on crtcs
966 * @old_state: atomic state object with old state structures
968 * Helper to, after atomic commit, wait for vblanks on all effected
969 * crtcs (ie. before cleaning up old framebuffers using
970 * drm_atomic_helper_cleanup_planes()). It will only wait on crtcs where the
971 * framebuffers have actually changed to optimize for the legacy cursor and
972 * plane update use-case.
975 drm_atomic_helper_wait_for_vblanks(struct drm_device *dev,
976 struct drm_atomic_state *old_state)
978 struct drm_crtc *crtc;
979 struct drm_crtc_state *old_crtc_state;
982 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
983 /* No one cares about the old state, so abuse it for tracking
984 * and store whether we hold a vblank reference (and should do a
985 * vblank wait) in the ->enable boolean. */
986 old_crtc_state->enable = false;
988 if (!crtc->state->enable)
991 /* Legacy cursor ioctls are completely unsynced, and userspace
992 * relies on that (by doing tons of cursor updates). */
993 if (old_state->legacy_cursor_update)
996 if (!drm_atomic_helper_framebuffer_changed(dev,
1000 ret = drm_crtc_vblank_get(crtc);
1004 old_crtc_state->enable = true;
1005 old_crtc_state->last_vblank_count = drm_crtc_vblank_count(crtc);
1008 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
1009 if (!old_crtc_state->enable)
1012 ret = wait_event_timeout(dev->vblank[i].queue,
1013 old_crtc_state->last_vblank_count !=
1014 drm_crtc_vblank_count(crtc),
1015 msecs_to_jiffies(50));
1017 drm_crtc_vblank_put(crtc);
1020 EXPORT_SYMBOL(drm_atomic_helper_wait_for_vblanks);
1023 * drm_atomic_helper_commit - commit validated state object
1025 * @state: the driver state object
1026 * @async: asynchronous commit
1028 * This function commits a with drm_atomic_helper_check() pre-validated state
1029 * object. This can still fail when e.g. the framebuffer reservation fails. For
1030 * now this doesn't implement asynchronous commits.
1032 * Note that right now this function does not support async commits, and hence
1033 * driver writers must implement their own version for now. Also note that the
1034 * default ordering of how the various stages are called is to match the legacy
1035 * modeset helper library closest. One peculiarity of that is that it doesn't
1036 * mesh well with runtime PM at all.
1038 * For drivers supporting runtime PM the recommended sequence is
1040 * drm_atomic_helper_commit_modeset_disables(dev, state);
1042 * drm_atomic_helper_commit_modeset_enables(dev, state);
1044 * drm_atomic_helper_commit_planes(dev, state, true);
1046 * See the kerneldoc entries for these three functions for more details.
1049 * Zero for success or -errno.
1051 int drm_atomic_helper_commit(struct drm_device *dev,
1052 struct drm_atomic_state *state,
1060 ret = drm_atomic_helper_prepare_planes(dev, state);
1065 * This is the point of no return - everything below never fails except
1066 * when the hw goes bonghits. Which means we can commit the new state on
1067 * the software side now.
1070 drm_atomic_helper_swap_state(dev, state);
1073 * Everything below can be run asynchronously without the need to grab
1074 * any modeset locks at all under one condition: It must be guaranteed
1075 * that the asynchronous work has either been cancelled (if the driver
1076 * supports it, which at least requires that the framebuffers get
1077 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
1078 * before the new state gets committed on the software side with
1079 * drm_atomic_helper_swap_state().
1081 * This scheme allows new atomic state updates to be prepared and
1082 * checked in parallel to the asynchronous completion of the previous
1083 * update. Which is important since compositors need to figure out the
1084 * composition of the next frame right after having submitted the
1088 wait_for_fences(dev, state);
1090 drm_atomic_helper_commit_modeset_disables(dev, state);
1092 drm_atomic_helper_commit_planes(dev, state, false);
1094 drm_atomic_helper_commit_modeset_enables(dev, state);
1096 drm_atomic_helper_wait_for_vblanks(dev, state);
1098 drm_atomic_helper_cleanup_planes(dev, state);
1100 drm_atomic_state_free(state);
1104 EXPORT_SYMBOL(drm_atomic_helper_commit);
1107 * DOC: implementing async commit
1109 * For now the atomic helpers don't support async commit directly. If there is
1110 * real need it could be added though, using the dma-buf fence infrastructure
1111 * for generic synchronization with outstanding rendering.
1113 * For now drivers have to implement async commit themselves, with the following
1114 * sequence being the recommended one:
1116 * 1. Run drm_atomic_helper_prepare_planes() first. This is the only function
1117 * which commit needs to call which can fail, so we want to run it first and
1120 * 2. Synchronize with any outstanding asynchronous commit worker threads which
1121 * might be affected the new state update. This can be done by either cancelling
1122 * or flushing the work items, depending upon whether the driver can deal with
1123 * cancelled updates. Note that it is important to ensure that the framebuffer
1124 * cleanup is still done when cancelling.
1126 * For sufficient parallelism it is recommended to have a work item per crtc
1127 * (for updates which don't touch global state) and a global one. Then we only
1128 * need to synchronize with the crtc work items for changed crtcs and the global
1129 * work item, which allows nice concurrent updates on disjoint sets of crtcs.
1131 * 3. The software state is updated synchronously with
1132 * drm_atomic_helper_swap_state(). Doing this under the protection of all modeset
1133 * locks means concurrent callers never see inconsistent state. And doing this
1134 * while it's guaranteed that no relevant async worker runs means that async
1135 * workers do not need grab any locks. Actually they must not grab locks, for
1136 * otherwise the work flushing will deadlock.
1138 * 4. Schedule a work item to do all subsequent steps, using the split-out
1139 * commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and
1140 * then cleaning up the framebuffers after the old framebuffer is no longer
1145 * drm_atomic_helper_prepare_planes - prepare plane resources before commit
1147 * @state: atomic state object with new state structures
1149 * This function prepares plane state, specifically framebuffers, for the new
1150 * configuration. If any failure is encountered this function will call
1151 * ->cleanup_fb on any already successfully prepared framebuffer.
1154 * 0 on success, negative error code on failure.
1156 int drm_atomic_helper_prepare_planes(struct drm_device *dev,
1157 struct drm_atomic_state *state)
1159 int nplanes = dev->mode_config.num_total_plane;
1162 for (i = 0; i < nplanes; i++) {
1163 const struct drm_plane_helper_funcs *funcs;
1164 struct drm_plane *plane = state->planes[i];
1165 struct drm_plane_state *plane_state = state->plane_states[i];
1170 funcs = plane->helper_private;
1172 if (funcs->prepare_fb) {
1173 ret = funcs->prepare_fb(plane, plane_state);
1182 for (i--; i >= 0; i--) {
1183 const struct drm_plane_helper_funcs *funcs;
1184 struct drm_plane *plane = state->planes[i];
1185 struct drm_plane_state *plane_state = state->plane_states[i];
1190 funcs = plane->helper_private;
1192 if (funcs->cleanup_fb)
1193 funcs->cleanup_fb(plane, plane_state);
1199 EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
1201 bool plane_crtc_active(struct drm_plane_state *state)
1203 return state->crtc && state->crtc->state->active;
1207 * drm_atomic_helper_commit_planes - commit plane state
1209 * @old_state: atomic state object with old state structures
1210 * @active_only: Only commit on active CRTC if set
1212 * This function commits the new plane state using the plane and atomic helper
1213 * functions for planes and crtcs. It assumes that the atomic state has already
1214 * been pushed into the relevant object state pointers, since this step can no
1217 * It still requires the global state object @old_state to know which planes and
1218 * crtcs need to be updated though.
1220 * Note that this function does all plane updates across all CRTCs in one step.
1221 * If the hardware can't support this approach look at
1222 * drm_atomic_helper_commit_planes_on_crtc() instead.
1224 * Plane parameters can be updated by applications while the associated CRTC is
1225 * disabled. The DRM/KMS core will store the parameters in the plane state,
1226 * which will be available to the driver when the CRTC is turned on. As a result
1227 * most drivers don't need to be immediately notified of plane updates for a
1230 * Unless otherwise needed, drivers are advised to set the @active_only
1231 * parameters to true in order not to receive plane update notifications related
1232 * to a disabled CRTC. This avoids the need to manually ignore plane updates in
1233 * driver code when the driver and/or hardware can't or just don't need to deal
1234 * with updates on disabled CRTCs, for example when supporting runtime PM.
1236 * The drm_atomic_helper_commit() default implementation only sets @active_only
1237 * to false to most closely match the behaviour of the legacy helpers. This should
1238 * not be copied blindly by drivers.
1240 void drm_atomic_helper_commit_planes(struct drm_device *dev,
1241 struct drm_atomic_state *old_state,
1244 struct drm_crtc *crtc;
1245 struct drm_crtc_state *old_crtc_state;
1246 struct drm_plane *plane;
1247 struct drm_plane_state *old_plane_state;
1250 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
1251 const struct drm_crtc_helper_funcs *funcs;
1253 funcs = crtc->helper_private;
1255 if (!funcs || !funcs->atomic_begin)
1258 if (active_only && !crtc->state->active)
1261 funcs->atomic_begin(crtc, old_crtc_state);
1264 for_each_plane_in_state(old_state, plane, old_plane_state, i) {
1265 const struct drm_plane_helper_funcs *funcs;
1268 funcs = plane->helper_private;
1273 disabling = drm_atomic_plane_disabling(plane, old_plane_state);
1277 * Skip planes related to inactive CRTCs. If the plane
1278 * is enabled use the state of the current CRTC. If the
1279 * plane is being disabled use the state of the old
1280 * CRTC to avoid skipping planes being disabled on an
1283 if (!disabling && !plane_crtc_active(plane->state))
1285 if (disabling && !plane_crtc_active(old_plane_state))
1290 * Special-case disabling the plane if drivers support it.
1292 if (disabling && funcs->atomic_disable)
1293 funcs->atomic_disable(plane, old_plane_state);
1294 else if (plane->state->crtc || disabling)
1295 funcs->atomic_update(plane, old_plane_state);
1298 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
1299 const struct drm_crtc_helper_funcs *funcs;
1301 funcs = crtc->helper_private;
1303 if (!funcs || !funcs->atomic_flush)
1306 if (active_only && !crtc->state->active)
1309 funcs->atomic_flush(crtc, old_crtc_state);
1312 EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
1315 * drm_atomic_helper_commit_planes_on_crtc - commit plane state for a crtc
1316 * @old_crtc_state: atomic state object with the old crtc state
1318 * This function commits the new plane state using the plane and atomic helper
1319 * functions for planes on the specific crtc. It assumes that the atomic state
1320 * has already been pushed into the relevant object state pointers, since this
1321 * step can no longer fail.
1323 * This function is useful when plane updates should be done crtc-by-crtc
1324 * instead of one global step like drm_atomic_helper_commit_planes() does.
1326 * This function can only be savely used when planes are not allowed to move
1327 * between different CRTCs because this function doesn't handle inter-CRTC
1328 * depencies. Callers need to ensure that either no such depencies exist,
1329 * resolve them through ordering of commit calls or through some other means.
1332 drm_atomic_helper_commit_planes_on_crtc(struct drm_crtc_state *old_crtc_state)
1334 const struct drm_crtc_helper_funcs *crtc_funcs;
1335 struct drm_crtc *crtc = old_crtc_state->crtc;
1336 struct drm_atomic_state *old_state = old_crtc_state->state;
1337 struct drm_plane *plane;
1338 unsigned plane_mask;
1340 plane_mask = old_crtc_state->plane_mask;
1341 plane_mask |= crtc->state->plane_mask;
1343 crtc_funcs = crtc->helper_private;
1344 if (crtc_funcs && crtc_funcs->atomic_begin)
1345 crtc_funcs->atomic_begin(crtc, old_crtc_state);
1347 drm_for_each_plane_mask(plane, crtc->dev, plane_mask) {
1348 struct drm_plane_state *old_plane_state =
1349 drm_atomic_get_existing_plane_state(old_state, plane);
1350 const struct drm_plane_helper_funcs *plane_funcs;
1352 plane_funcs = plane->helper_private;
1354 if (!old_plane_state || !plane_funcs)
1357 WARN_ON(plane->state->crtc && plane->state->crtc != crtc);
1359 if (drm_atomic_plane_disabling(plane, old_plane_state) &&
1360 plane_funcs->atomic_disable)
1361 plane_funcs->atomic_disable(plane, old_plane_state);
1362 else if (plane->state->crtc ||
1363 drm_atomic_plane_disabling(plane, old_plane_state))
1364 plane_funcs->atomic_update(plane, old_plane_state);
1367 if (crtc_funcs && crtc_funcs->atomic_flush)
1368 crtc_funcs->atomic_flush(crtc, old_crtc_state);
1370 EXPORT_SYMBOL(drm_atomic_helper_commit_planes_on_crtc);
1373 * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
1375 * @old_state: atomic state object with old state structures
1377 * This function cleans up plane state, specifically framebuffers, from the old
1378 * configuration. Hence the old configuration must be perserved in @old_state to
1379 * be able to call this function.
1381 * This function must also be called on the new state when the atomic update
1382 * fails at any point after calling drm_atomic_helper_prepare_planes().
1384 void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
1385 struct drm_atomic_state *old_state)
1387 struct drm_plane *plane;
1388 struct drm_plane_state *plane_state;
1391 for_each_plane_in_state(old_state, plane, plane_state, i) {
1392 const struct drm_plane_helper_funcs *funcs;
1394 funcs = plane->helper_private;
1396 if (funcs->cleanup_fb)
1397 funcs->cleanup_fb(plane, plane_state);
1400 EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
1403 * drm_atomic_helper_swap_state - store atomic state into current sw state
1405 * @state: atomic state
1407 * This function stores the atomic state into the current state pointers in all
1408 * driver objects. It should be called after all failing steps have been done
1409 * and succeeded, but before the actual hardware state is committed.
1411 * For cleanup and error recovery the current state for all changed objects will
1412 * be swaped into @state.
1414 * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
1416 * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
1418 * 2. Do any other steps that might fail.
1420 * 3. Put the staged state into the current state pointers with this function.
1422 * 4. Actually commit the hardware state.
1424 * 5. Call drm_atomic_helper_cleanup_planes() with @state, which since step 3
1425 * contains the old state. Also do any other cleanup required with that state.
1427 void drm_atomic_helper_swap_state(struct drm_device *dev,
1428 struct drm_atomic_state *state)
1432 for (i = 0; i < dev->mode_config.num_connector; i++) {
1433 struct drm_connector *connector = state->connectors[i];
1438 connector->state->state = state;
1439 swap(state->connector_states[i], connector->state);
1440 connector->state->state = NULL;
1443 for (i = 0; i < dev->mode_config.num_crtc; i++) {
1444 struct drm_crtc *crtc = state->crtcs[i];
1449 crtc->state->state = state;
1450 swap(state->crtc_states[i], crtc->state);
1451 crtc->state->state = NULL;
1454 for (i = 0; i < dev->mode_config.num_total_plane; i++) {
1455 struct drm_plane *plane = state->planes[i];
1460 plane->state->state = state;
1461 swap(state->plane_states[i], plane->state);
1462 plane->state->state = NULL;
1465 EXPORT_SYMBOL(drm_atomic_helper_swap_state);
1468 * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
1469 * @plane: plane object to update
1470 * @crtc: owning CRTC of owning plane
1471 * @fb: framebuffer to flip onto plane
1472 * @crtc_x: x offset of primary plane on crtc
1473 * @crtc_y: y offset of primary plane on crtc
1474 * @crtc_w: width of primary plane rectangle on crtc
1475 * @crtc_h: height of primary plane rectangle on crtc
1476 * @src_x: x offset of @fb for panning
1477 * @src_y: y offset of @fb for panning
1478 * @src_w: width of source rectangle in @fb
1479 * @src_h: height of source rectangle in @fb
1481 * Provides a default plane update handler using the atomic driver interface.
1484 * Zero on success, error code on failure
1486 int drm_atomic_helper_update_plane(struct drm_plane *plane,
1487 struct drm_crtc *crtc,
1488 struct drm_framebuffer *fb,
1489 int crtc_x, int crtc_y,
1490 unsigned int crtc_w, unsigned int crtc_h,
1491 uint32_t src_x, uint32_t src_y,
1492 uint32_t src_w, uint32_t src_h)
1494 struct drm_atomic_state *state;
1495 struct drm_plane_state *plane_state;
1498 state = drm_atomic_state_alloc(plane->dev);
1502 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1504 plane_state = drm_atomic_get_plane_state(state, plane);
1505 if (IS_ERR(plane_state)) {
1506 ret = PTR_ERR(plane_state);
1510 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
1513 drm_atomic_set_fb_for_plane(plane_state, fb);
1514 plane_state->crtc_x = crtc_x;
1515 plane_state->crtc_y = crtc_y;
1516 plane_state->crtc_h = crtc_h;
1517 plane_state->crtc_w = crtc_w;
1518 plane_state->src_x = src_x;
1519 plane_state->src_y = src_y;
1520 plane_state->src_h = src_h;
1521 plane_state->src_w = src_w;
1523 if (plane == crtc->cursor)
1524 state->legacy_cursor_update = true;
1526 ret = drm_atomic_commit(state);
1530 /* Driver takes ownership of state on successful commit. */
1533 if (ret == -EDEADLK)
1536 drm_atomic_state_free(state);
1540 drm_atomic_state_clear(state);
1541 drm_atomic_legacy_backoff(state);
1544 * Someone might have exchanged the framebuffer while we dropped locks
1545 * in the backoff code. We need to fix up the fb refcount tracking the
1548 plane->old_fb = plane->fb;
1552 EXPORT_SYMBOL(drm_atomic_helper_update_plane);
1555 * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
1556 * @plane: plane to disable
1558 * Provides a default plane disable handler using the atomic driver interface.
1561 * Zero on success, error code on failure
1563 int drm_atomic_helper_disable_plane(struct drm_plane *plane)
1565 struct drm_atomic_state *state;
1566 struct drm_plane_state *plane_state;
1570 * FIXME: Without plane->crtc set we can't get at the implicit legacy
1571 * acquire context. The real fix will be to wire the acquire ctx through
1572 * everywhere we need it, but meanwhile prevent chaos by just skipping
1573 * this noop. The critical case is the cursor ioctls which a) only grab
1574 * crtc/cursor-plane locks (so we need the crtc to get at the right
1575 * acquire context) and b) can try to disable the plane multiple times.
1580 state = drm_atomic_state_alloc(plane->dev);
1584 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(plane->crtc);
1586 plane_state = drm_atomic_get_plane_state(state, plane);
1587 if (IS_ERR(plane_state)) {
1588 ret = PTR_ERR(plane_state);
1592 if (plane_state->crtc && (plane == plane->crtc->cursor))
1593 plane_state->state->legacy_cursor_update = true;
1595 ret = __drm_atomic_helper_disable_plane(plane, plane_state);
1599 ret = drm_atomic_commit(state);
1603 /* Driver takes ownership of state on successful commit. */
1606 if (ret == -EDEADLK)
1609 drm_atomic_state_free(state);
1613 drm_atomic_state_clear(state);
1614 drm_atomic_legacy_backoff(state);
1617 * Someone might have exchanged the framebuffer while we dropped locks
1618 * in the backoff code. We need to fix up the fb refcount tracking the
1621 plane->old_fb = plane->fb;
1625 EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
1627 /* just used from fb-helper and atomic-helper: */
1628 int __drm_atomic_helper_disable_plane(struct drm_plane *plane,
1629 struct drm_plane_state *plane_state)
1633 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
1637 drm_atomic_set_fb_for_plane(plane_state, NULL);
1638 plane_state->crtc_x = 0;
1639 plane_state->crtc_y = 0;
1640 plane_state->crtc_h = 0;
1641 plane_state->crtc_w = 0;
1642 plane_state->src_x = 0;
1643 plane_state->src_y = 0;
1644 plane_state->src_h = 0;
1645 plane_state->src_w = 0;
1650 static int update_output_state(struct drm_atomic_state *state,
1651 struct drm_mode_set *set)
1653 struct drm_device *dev = set->crtc->dev;
1654 struct drm_crtc *crtc;
1655 struct drm_crtc_state *crtc_state;
1656 struct drm_connector *connector;
1657 struct drm_connector_state *conn_state;
1660 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
1661 state->acquire_ctx);
1665 /* First grab all affected connector/crtc states. */
1666 for (i = 0; i < set->num_connectors; i++) {
1667 conn_state = drm_atomic_get_connector_state(state,
1668 set->connectors[i]);
1669 if (IS_ERR(conn_state))
1670 return PTR_ERR(conn_state);
1673 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1674 ret = drm_atomic_add_affected_connectors(state, crtc);
1679 /* Then recompute connector->crtc links and crtc enabling state. */
1680 for_each_connector_in_state(state, connector, conn_state, i) {
1681 if (conn_state->crtc == set->crtc) {
1682 ret = drm_atomic_set_crtc_for_connector(conn_state,
1688 for (j = 0; j < set->num_connectors; j++) {
1689 if (set->connectors[j] == connector) {
1690 ret = drm_atomic_set_crtc_for_connector(conn_state,
1699 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1700 /* Don't update ->enable for the CRTC in the set_config request,
1701 * since a mismatch would indicate a bug in the upper layers.
1702 * The actual modeset code later on will catch any
1703 * inconsistencies here. */
1704 if (crtc == set->crtc)
1707 if (!drm_atomic_connectors_for_crtc(state, crtc)) {
1708 ret = drm_atomic_set_mode_prop_for_crtc(crtc_state,
1713 crtc_state->active = false;
1721 * drm_atomic_helper_set_config - set a new config from userspace
1722 * @set: mode set configuration
1724 * Provides a default crtc set_config handler using the atomic driver interface.
1727 * Returns 0 on success, negative errno numbers on failure.
1729 int drm_atomic_helper_set_config(struct drm_mode_set *set)
1731 struct drm_atomic_state *state;
1732 struct drm_crtc *crtc = set->crtc;
1735 state = drm_atomic_state_alloc(crtc->dev);
1739 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1741 ret = __drm_atomic_helper_set_config(set, state);
1745 ret = drm_atomic_commit(state);
1749 /* Driver takes ownership of state on successful commit. */
1752 if (ret == -EDEADLK)
1755 drm_atomic_state_free(state);
1759 drm_atomic_state_clear(state);
1760 drm_atomic_legacy_backoff(state);
1763 * Someone might have exchanged the framebuffer while we dropped locks
1764 * in the backoff code. We need to fix up the fb refcount tracking the
1767 crtc->primary->old_fb = crtc->primary->fb;
1771 EXPORT_SYMBOL(drm_atomic_helper_set_config);
1773 /* just used from fb-helper and atomic-helper: */
1774 int __drm_atomic_helper_set_config(struct drm_mode_set *set,
1775 struct drm_atomic_state *state)
1777 struct drm_crtc_state *crtc_state;
1778 struct drm_plane_state *primary_state;
1779 struct drm_crtc *crtc = set->crtc;
1780 int hdisplay, vdisplay;
1783 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1784 if (IS_ERR(crtc_state))
1785 return PTR_ERR(crtc_state);
1787 primary_state = drm_atomic_get_plane_state(state, crtc->primary);
1788 if (IS_ERR(primary_state))
1789 return PTR_ERR(primary_state);
1793 WARN_ON(set->num_connectors);
1795 ret = drm_atomic_set_mode_for_crtc(crtc_state, NULL);
1799 crtc_state->active = false;
1801 ret = drm_atomic_set_crtc_for_plane(primary_state, NULL);
1805 drm_atomic_set_fb_for_plane(primary_state, NULL);
1811 WARN_ON(!set->num_connectors);
1813 ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
1817 crtc_state->active = true;
1819 ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
1823 drm_crtc_get_hv_timing(set->mode, &hdisplay, &vdisplay);
1825 drm_atomic_set_fb_for_plane(primary_state, set->fb);
1826 primary_state->crtc_x = 0;
1827 primary_state->crtc_y = 0;
1828 primary_state->crtc_h = vdisplay;
1829 primary_state->crtc_w = hdisplay;
1830 primary_state->src_x = set->x << 16;
1831 primary_state->src_y = set->y << 16;
1832 if (primary_state->rotation & (BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270))) {
1833 primary_state->src_h = hdisplay << 16;
1834 primary_state->src_w = vdisplay << 16;
1836 primary_state->src_h = vdisplay << 16;
1837 primary_state->src_w = hdisplay << 16;
1841 ret = update_output_state(state, set);
1849 * drm_atomic_helper_disable_all - disable all currently active outputs
1851 * @ctx: lock acquisition context
1853 * Loops through all connectors, finding those that aren't turned off and then
1854 * turns them off by setting their DPMS mode to OFF and deactivating the CRTC
1855 * that they are connected to.
1857 * This is used for example in suspend/resume to disable all currently active
1858 * functions when suspending.
1860 * Note that if callers haven't already acquired all modeset locks this might
1861 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
1864 * 0 on success or a negative error code on failure.
1867 * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
1869 int drm_atomic_helper_disable_all(struct drm_device *dev,
1870 struct drm_modeset_acquire_ctx *ctx)
1872 struct drm_atomic_state *state;
1873 struct drm_connector *conn;
1876 state = drm_atomic_state_alloc(dev);
1880 state->acquire_ctx = ctx;
1882 drm_for_each_connector(conn, dev) {
1883 struct drm_crtc *crtc = conn->state->crtc;
1884 struct drm_crtc_state *crtc_state;
1886 if (!crtc || conn->dpms != DRM_MODE_DPMS_ON)
1889 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1890 if (IS_ERR(crtc_state)) {
1891 err = PTR_ERR(crtc_state);
1895 crtc_state->active = false;
1898 err = drm_atomic_commit(state);
1902 drm_atomic_state_free(state);
1906 EXPORT_SYMBOL(drm_atomic_helper_disable_all);
1909 * drm_atomic_helper_suspend - subsystem-level suspend helper
1912 * Duplicates the current atomic state, disables all active outputs and then
1913 * returns a pointer to the original atomic state to the caller. Drivers can
1914 * pass this pointer to the drm_atomic_helper_resume() helper upon resume to
1915 * restore the output configuration that was active at the time the system
1918 * Note that it is potentially unsafe to use this. The atomic state object
1919 * returned by this function is assumed to be persistent. Drivers must ensure
1920 * that this holds true. Before calling this function, drivers must make sure
1921 * to suspend fbdev emulation so that nothing can be using the device.
1924 * A pointer to a copy of the state before suspend on success or an ERR_PTR()-
1925 * encoded error code on failure. Drivers should store the returned atomic
1926 * state object and pass it to the drm_atomic_helper_resume() helper upon
1930 * drm_atomic_helper_duplicate_state(), drm_atomic_helper_disable_all(),
1931 * drm_atomic_helper_resume()
1933 struct drm_atomic_state *drm_atomic_helper_suspend(struct drm_device *dev)
1935 struct drm_modeset_acquire_ctx ctx;
1936 struct drm_atomic_state *state;
1939 drm_modeset_acquire_init(&ctx, 0);
1942 err = drm_modeset_lock_all_ctx(dev, &ctx);
1944 state = ERR_PTR(err);
1948 state = drm_atomic_helper_duplicate_state(dev, &ctx);
1952 err = drm_atomic_helper_disable_all(dev, &ctx);
1954 drm_atomic_state_free(state);
1955 state = ERR_PTR(err);
1960 if (PTR_ERR(state) == -EDEADLK) {
1961 drm_modeset_backoff(&ctx);
1965 drm_modeset_drop_locks(&ctx);
1966 drm_modeset_acquire_fini(&ctx);
1969 EXPORT_SYMBOL(drm_atomic_helper_suspend);
1972 * drm_atomic_helper_resume - subsystem-level resume helper
1974 * @state: atomic state to resume to
1976 * Calls drm_mode_config_reset() to synchronize hardware and software states,
1977 * grabs all modeset locks and commits the atomic state object. This can be
1978 * used in conjunction with the drm_atomic_helper_suspend() helper to
1979 * implement suspend/resume for drivers that support atomic mode-setting.
1982 * 0 on success or a negative error code on failure.
1985 * drm_atomic_helper_suspend()
1987 int drm_atomic_helper_resume(struct drm_device *dev,
1988 struct drm_atomic_state *state)
1990 struct drm_mode_config *config = &dev->mode_config;
1993 drm_mode_config_reset(dev);
1994 drm_modeset_lock_all(dev);
1995 state->acquire_ctx = config->acquire_ctx;
1996 err = drm_atomic_commit(state);
1997 drm_modeset_unlock_all(dev);
2001 EXPORT_SYMBOL(drm_atomic_helper_resume);
2004 * drm_atomic_helper_crtc_set_property - helper for crtc properties
2006 * @property: DRM property
2007 * @val: value of property
2009 * Provides a default crtc set_property handler using the atomic driver
2013 * Zero on success, error code on failure
2016 drm_atomic_helper_crtc_set_property(struct drm_crtc *crtc,
2017 struct drm_property *property,
2020 struct drm_atomic_state *state;
2021 struct drm_crtc_state *crtc_state;
2024 state = drm_atomic_state_alloc(crtc->dev);
2028 /* ->set_property is always called with all locks held. */
2029 state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
2031 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2032 if (IS_ERR(crtc_state)) {
2033 ret = PTR_ERR(crtc_state);
2037 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
2042 ret = drm_atomic_commit(state);
2046 /* Driver takes ownership of state on successful commit. */
2049 if (ret == -EDEADLK)
2052 drm_atomic_state_free(state);
2056 drm_atomic_state_clear(state);
2057 drm_atomic_legacy_backoff(state);
2061 EXPORT_SYMBOL(drm_atomic_helper_crtc_set_property);
2064 * drm_atomic_helper_plane_set_property - helper for plane properties
2066 * @property: DRM property
2067 * @val: value of property
2069 * Provides a default plane set_property handler using the atomic driver
2073 * Zero on success, error code on failure
2076 drm_atomic_helper_plane_set_property(struct drm_plane *plane,
2077 struct drm_property *property,
2080 struct drm_atomic_state *state;
2081 struct drm_plane_state *plane_state;
2084 state = drm_atomic_state_alloc(plane->dev);
2088 /* ->set_property is always called with all locks held. */
2089 state->acquire_ctx = plane->dev->mode_config.acquire_ctx;
2091 plane_state = drm_atomic_get_plane_state(state, plane);
2092 if (IS_ERR(plane_state)) {
2093 ret = PTR_ERR(plane_state);
2097 ret = drm_atomic_plane_set_property(plane, plane_state,
2102 ret = drm_atomic_commit(state);
2106 /* Driver takes ownership of state on successful commit. */
2109 if (ret == -EDEADLK)
2112 drm_atomic_state_free(state);
2116 drm_atomic_state_clear(state);
2117 drm_atomic_legacy_backoff(state);
2121 EXPORT_SYMBOL(drm_atomic_helper_plane_set_property);
2124 * drm_atomic_helper_connector_set_property - helper for connector properties
2125 * @connector: DRM connector
2126 * @property: DRM property
2127 * @val: value of property
2129 * Provides a default connector set_property handler using the atomic driver
2133 * Zero on success, error code on failure
2136 drm_atomic_helper_connector_set_property(struct drm_connector *connector,
2137 struct drm_property *property,
2140 struct drm_atomic_state *state;
2141 struct drm_connector_state *connector_state;
2144 state = drm_atomic_state_alloc(connector->dev);
2148 /* ->set_property is always called with all locks held. */
2149 state->acquire_ctx = connector->dev->mode_config.acquire_ctx;
2151 connector_state = drm_atomic_get_connector_state(state, connector);
2152 if (IS_ERR(connector_state)) {
2153 ret = PTR_ERR(connector_state);
2157 ret = drm_atomic_connector_set_property(connector, connector_state,
2162 ret = drm_atomic_commit(state);
2166 /* Driver takes ownership of state on successful commit. */
2169 if (ret == -EDEADLK)
2172 drm_atomic_state_free(state);
2176 drm_atomic_state_clear(state);
2177 drm_atomic_legacy_backoff(state);
2181 EXPORT_SYMBOL(drm_atomic_helper_connector_set_property);
2184 * drm_atomic_helper_page_flip - execute a legacy page flip
2186 * @fb: DRM framebuffer
2187 * @event: optional DRM event to signal upon completion
2188 * @flags: flip flags for non-vblank sync'ed updates
2190 * Provides a default page flip implementation using the atomic driver interface.
2192 * Note that for now so called async page flips (i.e. updates which are not
2193 * synchronized to vblank) are not supported, since the atomic interfaces have
2194 * no provisions for this yet.
2197 * Returns 0 on success, negative errno numbers on failure.
2199 int drm_atomic_helper_page_flip(struct drm_crtc *crtc,
2200 struct drm_framebuffer *fb,
2201 struct drm_pending_vblank_event *event,
2204 struct drm_plane *plane = crtc->primary;
2205 struct drm_atomic_state *state;
2206 struct drm_plane_state *plane_state;
2207 struct drm_crtc_state *crtc_state;
2210 if (flags & DRM_MODE_PAGE_FLIP_ASYNC)
2213 state = drm_atomic_state_alloc(plane->dev);
2217 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2219 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2220 if (IS_ERR(crtc_state)) {
2221 ret = PTR_ERR(crtc_state);
2224 crtc_state->event = event;
2226 plane_state = drm_atomic_get_plane_state(state, plane);
2227 if (IS_ERR(plane_state)) {
2228 ret = PTR_ERR(plane_state);
2232 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
2235 drm_atomic_set_fb_for_plane(plane_state, fb);
2237 ret = drm_atomic_async_commit(state);
2241 /* Driver takes ownership of state on successful async commit. */
2244 if (ret == -EDEADLK)
2247 drm_atomic_state_free(state);
2251 drm_atomic_state_clear(state);
2252 drm_atomic_legacy_backoff(state);
2255 * Someone might have exchanged the framebuffer while we dropped locks
2256 * in the backoff code. We need to fix up the fb refcount tracking the
2259 plane->old_fb = plane->fb;
2263 EXPORT_SYMBOL(drm_atomic_helper_page_flip);
2266 * drm_atomic_helper_connector_dpms() - connector dpms helper implementation
2267 * @connector: affected connector
2270 * This is the main helper function provided by the atomic helper framework for
2271 * implementing the legacy DPMS connector interface. It computes the new desired
2272 * ->active state for the corresponding CRTC (if the connector is enabled) and
2276 * Returns 0 on success, negative errno numbers on failure.
2278 int drm_atomic_helper_connector_dpms(struct drm_connector *connector,
2281 struct drm_mode_config *config = &connector->dev->mode_config;
2282 struct drm_atomic_state *state;
2283 struct drm_crtc_state *crtc_state;
2284 struct drm_crtc *crtc;
2285 struct drm_connector *tmp_connector;
2287 bool active = false;
2288 int old_mode = connector->dpms;
2290 if (mode != DRM_MODE_DPMS_ON)
2291 mode = DRM_MODE_DPMS_OFF;
2293 connector->dpms = mode;
2294 crtc = connector->state->crtc;
2299 state = drm_atomic_state_alloc(connector->dev);
2303 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2305 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2306 if (IS_ERR(crtc_state)) {
2307 ret = PTR_ERR(crtc_state);
2311 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
2313 drm_for_each_connector(tmp_connector, connector->dev) {
2314 if (tmp_connector->state->crtc != crtc)
2317 if (tmp_connector->dpms == DRM_MODE_DPMS_ON) {
2322 crtc_state->active = active;
2324 ret = drm_atomic_commit(state);
2328 /* Driver takes ownership of state on successful commit. */
2331 if (ret == -EDEADLK)
2334 connector->dpms = old_mode;
2335 drm_atomic_state_free(state);
2339 drm_atomic_state_clear(state);
2340 drm_atomic_legacy_backoff(state);
2344 EXPORT_SYMBOL(drm_atomic_helper_connector_dpms);
2347 * DOC: atomic state reset and initialization
2349 * Both the drm core and the atomic helpers assume that there is always the full
2350 * and correct atomic software state for all connectors, CRTCs and planes
2351 * available. Which is a bit a problem on driver load and also after system
2352 * suspend. One way to solve this is to have a hardware state read-out
2353 * infrastructure which reconstructs the full software state (e.g. the i915
2356 * The simpler solution is to just reset the software state to everything off,
2357 * which is easiest to do by calling drm_mode_config_reset(). To facilitate this
2358 * the atomic helpers provide default reset implementations for all hooks.
2362 * drm_atomic_helper_crtc_reset - default ->reset hook for CRTCs
2365 * Resets the atomic state for @crtc by freeing the state pointer (which might
2366 * be NULL, e.g. at driver load time) and allocating a new empty state object.
2368 void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc)
2370 if (crtc->state && crtc->state->mode_blob)
2371 drm_property_unreference_blob(crtc->state->mode_blob);
2373 crtc->state = kzalloc(sizeof(*crtc->state), GFP_KERNEL);
2376 crtc->state->crtc = crtc;
2378 EXPORT_SYMBOL(drm_atomic_helper_crtc_reset);
2381 * __drm_atomic_helper_crtc_duplicate_state - copy atomic CRTC state
2382 * @crtc: CRTC object
2383 * @state: atomic CRTC state
2385 * Copies atomic state from a CRTC's current state and resets inferred values.
2386 * This is useful for drivers that subclass the CRTC state.
2388 void __drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc,
2389 struct drm_crtc_state *state)
2391 memcpy(state, crtc->state, sizeof(*state));
2393 if (state->mode_blob)
2394 drm_property_reference_blob(state->mode_blob);
2395 state->mode_changed = false;
2396 state->active_changed = false;
2397 state->planes_changed = false;
2398 state->connectors_changed = false;
2399 state->event = NULL;
2401 EXPORT_SYMBOL(__drm_atomic_helper_crtc_duplicate_state);
2404 * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
2407 * Default CRTC state duplicate hook for drivers which don't have their own
2408 * subclassed CRTC state structure.
2410 struct drm_crtc_state *
2411 drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc)
2413 struct drm_crtc_state *state;
2415 if (WARN_ON(!crtc->state))
2418 state = kmalloc(sizeof(*state), GFP_KERNEL);
2420 __drm_atomic_helper_crtc_duplicate_state(crtc, state);
2424 EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state);
2427 * __drm_atomic_helper_crtc_destroy_state - release CRTC state
2428 * @crtc: CRTC object
2429 * @state: CRTC state object to release
2431 * Releases all resources stored in the CRTC state without actually freeing
2432 * the memory of the CRTC state. This is useful for drivers that subclass the
2435 void __drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
2436 struct drm_crtc_state *state)
2438 if (state->mode_blob)
2439 drm_property_unreference_blob(state->mode_blob);
2441 EXPORT_SYMBOL(__drm_atomic_helper_crtc_destroy_state);
2444 * drm_atomic_helper_crtc_destroy_state - default state destroy hook
2446 * @state: CRTC state object to release
2448 * Default CRTC state destroy hook for drivers which don't have their own
2449 * subclassed CRTC state structure.
2451 void drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
2452 struct drm_crtc_state *state)
2454 __drm_atomic_helper_crtc_destroy_state(crtc, state);
2457 EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state);
2460 * drm_atomic_helper_plane_reset - default ->reset hook for planes
2463 * Resets the atomic state for @plane by freeing the state pointer (which might
2464 * be NULL, e.g. at driver load time) and allocating a new empty state object.
2466 void drm_atomic_helper_plane_reset(struct drm_plane *plane)
2468 if (plane->state && plane->state->fb)
2469 drm_framebuffer_unreference(plane->state->fb);
2471 kfree(plane->state);
2472 plane->state = kzalloc(sizeof(*plane->state), GFP_KERNEL);
2475 plane->state->plane = plane;
2477 EXPORT_SYMBOL(drm_atomic_helper_plane_reset);
2480 * __drm_atomic_helper_plane_duplicate_state - copy atomic plane state
2481 * @plane: plane object
2482 * @state: atomic plane state
2484 * Copies atomic state from a plane's current state. This is useful for
2485 * drivers that subclass the plane state.
2487 void __drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane,
2488 struct drm_plane_state *state)
2490 memcpy(state, plane->state, sizeof(*state));
2493 drm_framebuffer_reference(state->fb);
2495 EXPORT_SYMBOL(__drm_atomic_helper_plane_duplicate_state);
2498 * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
2501 * Default plane state duplicate hook for drivers which don't have their own
2502 * subclassed plane state structure.
2504 struct drm_plane_state *
2505 drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane)
2507 struct drm_plane_state *state;
2509 if (WARN_ON(!plane->state))
2512 state = kmalloc(sizeof(*state), GFP_KERNEL);
2514 __drm_atomic_helper_plane_duplicate_state(plane, state);
2518 EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state);
2521 * __drm_atomic_helper_plane_destroy_state - release plane state
2522 * @plane: plane object
2523 * @state: plane state object to release
2525 * Releases all resources stored in the plane state without actually freeing
2526 * the memory of the plane state. This is useful for drivers that subclass the
2529 void __drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
2530 struct drm_plane_state *state)
2533 drm_framebuffer_unreference(state->fb);
2535 EXPORT_SYMBOL(__drm_atomic_helper_plane_destroy_state);
2538 * drm_atomic_helper_plane_destroy_state - default state destroy hook
2540 * @state: plane state object to release
2542 * Default plane state destroy hook for drivers which don't have their own
2543 * subclassed plane state structure.
2545 void drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
2546 struct drm_plane_state *state)
2548 __drm_atomic_helper_plane_destroy_state(plane, state);
2551 EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state);
2554 * drm_atomic_helper_connector_reset - default ->reset hook for connectors
2555 * @connector: drm connector
2557 * Resets the atomic state for @connector by freeing the state pointer (which
2558 * might be NULL, e.g. at driver load time) and allocating a new empty state
2561 void drm_atomic_helper_connector_reset(struct drm_connector *connector)
2563 kfree(connector->state);
2564 connector->state = kzalloc(sizeof(*connector->state), GFP_KERNEL);
2566 if (connector->state)
2567 connector->state->connector = connector;
2569 EXPORT_SYMBOL(drm_atomic_helper_connector_reset);
2572 * __drm_atomic_helper_connector_duplicate_state - copy atomic connector state
2573 * @connector: connector object
2574 * @state: atomic connector state
2576 * Copies atomic state from a connector's current state. This is useful for
2577 * drivers that subclass the connector state.
2580 __drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector,
2581 struct drm_connector_state *state)
2583 memcpy(state, connector->state, sizeof(*state));
2585 EXPORT_SYMBOL(__drm_atomic_helper_connector_duplicate_state);
2588 * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
2589 * @connector: drm connector
2591 * Default connector state duplicate hook for drivers which don't have their own
2592 * subclassed connector state structure.
2594 struct drm_connector_state *
2595 drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
2597 struct drm_connector_state *state;
2599 if (WARN_ON(!connector->state))
2602 state = kmalloc(sizeof(*state), GFP_KERNEL);
2604 __drm_atomic_helper_connector_duplicate_state(connector, state);
2608 EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
2611 * drm_atomic_helper_duplicate_state - duplicate an atomic state object
2613 * @ctx: lock acquisition context
2615 * Makes a copy of the current atomic state by looping over all objects and
2616 * duplicating their respective states. This is used for example by suspend/
2617 * resume support code to save the state prior to suspend such that it can
2618 * be restored upon resume.
2620 * Note that this treats atomic state as persistent between save and restore.
2621 * Drivers must make sure that this is possible and won't result in confusion
2622 * or erroneous behaviour.
2624 * Note that if callers haven't already acquired all modeset locks this might
2625 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
2628 * A pointer to the copy of the atomic state object on success or an
2629 * ERR_PTR()-encoded error code on failure.
2632 * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
2634 struct drm_atomic_state *
2635 drm_atomic_helper_duplicate_state(struct drm_device *dev,
2636 struct drm_modeset_acquire_ctx *ctx)
2638 struct drm_atomic_state *state;
2639 struct drm_connector *conn;
2640 struct drm_plane *plane;
2641 struct drm_crtc *crtc;
2644 state = drm_atomic_state_alloc(dev);
2646 return ERR_PTR(-ENOMEM);
2648 state->acquire_ctx = ctx;
2650 drm_for_each_crtc(crtc, dev) {
2651 struct drm_crtc_state *crtc_state;
2653 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2654 if (IS_ERR(crtc_state)) {
2655 err = PTR_ERR(crtc_state);
2660 drm_for_each_plane(plane, dev) {
2661 struct drm_plane_state *plane_state;
2663 plane_state = drm_atomic_get_plane_state(state, plane);
2664 if (IS_ERR(plane_state)) {
2665 err = PTR_ERR(plane_state);
2670 drm_for_each_connector(conn, dev) {
2671 struct drm_connector_state *conn_state;
2673 conn_state = drm_atomic_get_connector_state(state, conn);
2674 if (IS_ERR(conn_state)) {
2675 err = PTR_ERR(conn_state);
2680 /* clear the acquire context so that it isn't accidentally reused */
2681 state->acquire_ctx = NULL;
2685 drm_atomic_state_free(state);
2686 state = ERR_PTR(err);
2691 EXPORT_SYMBOL(drm_atomic_helper_duplicate_state);
2694 * __drm_atomic_helper_connector_destroy_state - release connector state
2695 * @connector: connector object
2696 * @state: connector state object to release
2698 * Releases all resources stored in the connector state without actually
2699 * freeing the memory of the connector state. This is useful for drivers that
2700 * subclass the connector state.
2703 __drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
2704 struct drm_connector_state *state)
2707 * This is currently a placeholder so that drivers that subclass the
2708 * state will automatically do the right thing if code is ever added
2712 EXPORT_SYMBOL(__drm_atomic_helper_connector_destroy_state);
2715 * drm_atomic_helper_connector_destroy_state - default state destroy hook
2716 * @connector: drm connector
2717 * @state: connector state object to release
2719 * Default connector state destroy hook for drivers which don't have their own
2720 * subclassed connector state structure.
2722 void drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
2723 struct drm_connector_state *state)
2725 __drm_atomic_helper_connector_destroy_state(connector, state);
2728 EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state);