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 struct drm_encoder *encoder;
583 struct drm_crtc_state *old_crtc_state;
585 /* Shut down everything that's in the changeset and currently
586 * still on. So need to check the old, saved state. */
587 if (!old_conn_state->crtc)
590 old_crtc_state = old_state->crtc_states[drm_crtc_index(old_conn_state->crtc)];
592 if (!old_crtc_state->active ||
593 !drm_atomic_crtc_needs_modeset(old_conn_state->crtc->state))
596 encoder = old_conn_state->best_encoder;
598 /* We shouldn't get this far if we didn't previously have
599 * an encoder.. but WARN_ON() rather than explode.
601 if (WARN_ON(!encoder))
604 funcs = encoder->helper_private;
606 DRM_DEBUG_ATOMIC("disabling [ENCODER:%d:%s]\n",
607 encoder->base.id, encoder->name);
610 * Each encoder has at most one connector (since we always steal
611 * it away), so we won't call disable hooks twice.
613 drm_bridge_disable(encoder->bridge);
615 /* Right function depends upon target state. */
616 if (connector->state->crtc && funcs->prepare)
617 funcs->prepare(encoder);
618 else if (funcs->disable)
619 funcs->disable(encoder);
621 funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
623 drm_bridge_post_disable(encoder->bridge);
626 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
627 const struct drm_crtc_helper_funcs *funcs;
629 /* Shut down everything that needs a full modeset. */
630 if (!drm_atomic_crtc_needs_modeset(crtc->state))
633 if (!old_crtc_state->active)
636 funcs = crtc->helper_private;
638 DRM_DEBUG_ATOMIC("disabling [CRTC:%d]\n",
642 /* Right function depends upon target state. */
643 if (crtc->state->enable && funcs->prepare)
644 funcs->prepare(crtc);
645 else if (funcs->disable)
646 funcs->disable(crtc);
648 funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
653 * drm_atomic_helper_update_legacy_modeset_state - update legacy modeset state
655 * @old_state: atomic state object with old state structures
657 * This function updates all the various legacy modeset state pointers in
658 * connectors, encoders and crtcs. It also updates the timestamping constants
659 * used for precise vblank timestamps by calling
660 * drm_calc_timestamping_constants().
662 * Drivers can use this for building their own atomic commit if they don't have
663 * a pure helper-based modeset implementation.
666 drm_atomic_helper_update_legacy_modeset_state(struct drm_device *dev,
667 struct drm_atomic_state *old_state)
669 struct drm_connector *connector;
670 struct drm_connector_state *old_conn_state;
671 struct drm_crtc *crtc;
672 struct drm_crtc_state *old_crtc_state;
675 /* clear out existing links and update dpms */
676 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
677 if (connector->encoder) {
678 WARN_ON(!connector->encoder->crtc);
680 connector->encoder->crtc = NULL;
681 connector->encoder = NULL;
684 crtc = connector->state->crtc;
685 if ((!crtc && old_conn_state->crtc) ||
686 (crtc && drm_atomic_crtc_needs_modeset(crtc->state))) {
687 struct drm_property *dpms_prop =
688 dev->mode_config.dpms_property;
689 int mode = DRM_MODE_DPMS_OFF;
691 if (crtc && crtc->state->active)
692 mode = DRM_MODE_DPMS_ON;
694 connector->dpms = mode;
695 drm_object_property_set_value(&connector->base,
701 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
702 if (!connector->state->crtc)
705 if (WARN_ON(!connector->state->best_encoder))
708 connector->encoder = connector->state->best_encoder;
709 connector->encoder->crtc = connector->state->crtc;
712 /* set legacy state in the crtc structure */
713 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
714 struct drm_plane *primary = crtc->primary;
716 crtc->mode = crtc->state->mode;
717 crtc->enabled = crtc->state->enable;
719 if (drm_atomic_get_existing_plane_state(old_state, primary) &&
720 primary->state->crtc == crtc) {
721 crtc->x = primary->state->src_x >> 16;
722 crtc->y = primary->state->src_y >> 16;
725 if (crtc->state->enable)
726 drm_calc_timestamping_constants(crtc,
727 &crtc->state->adjusted_mode);
730 EXPORT_SYMBOL(drm_atomic_helper_update_legacy_modeset_state);
733 crtc_set_mode(struct drm_device *dev, struct drm_atomic_state *old_state)
735 struct drm_crtc *crtc;
736 struct drm_crtc_state *old_crtc_state;
737 struct drm_connector *connector;
738 struct drm_connector_state *old_conn_state;
741 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
742 const struct drm_crtc_helper_funcs *funcs;
744 if (!crtc->state->mode_changed)
747 funcs = crtc->helper_private;
749 if (crtc->state->enable && funcs->mode_set_nofb) {
750 DRM_DEBUG_ATOMIC("modeset on [CRTC:%d]\n",
753 funcs->mode_set_nofb(crtc);
757 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
758 const struct drm_encoder_helper_funcs *funcs;
759 struct drm_crtc_state *new_crtc_state;
760 struct drm_encoder *encoder;
761 struct drm_display_mode *mode, *adjusted_mode;
763 if (!connector->state->best_encoder)
766 encoder = connector->state->best_encoder;
767 funcs = encoder->helper_private;
768 new_crtc_state = connector->state->crtc->state;
769 mode = &new_crtc_state->mode;
770 adjusted_mode = &new_crtc_state->adjusted_mode;
772 if (!new_crtc_state->mode_changed)
775 DRM_DEBUG_ATOMIC("modeset on [ENCODER:%d:%s]\n",
776 encoder->base.id, encoder->name);
779 * Each encoder has at most one connector (since we always steal
780 * it away), so we won't call mode_set hooks twice.
783 funcs->mode_set(encoder, mode, adjusted_mode);
785 drm_bridge_mode_set(encoder->bridge, mode, adjusted_mode);
790 * drm_atomic_helper_commit_modeset_disables - modeset commit to disable outputs
792 * @old_state: atomic state object with old state structures
794 * This function shuts down all the outputs that need to be shut down and
795 * prepares them (if required) with the new mode.
797 * For compatibility with legacy crtc helpers this should be called before
798 * drm_atomic_helper_commit_planes(), which is what the default commit function
799 * does. But drivers with different needs can group the modeset commits together
800 * and do the plane commits at the end. This is useful for drivers doing runtime
801 * PM since planes updates then only happen when the CRTC is actually enabled.
803 void drm_atomic_helper_commit_modeset_disables(struct drm_device *dev,
804 struct drm_atomic_state *old_state)
806 disable_outputs(dev, old_state);
808 drm_atomic_helper_update_legacy_modeset_state(dev, old_state);
810 crtc_set_mode(dev, old_state);
812 EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_disables);
815 * drm_atomic_helper_commit_modeset_enables - modeset commit to enable outputs
817 * @old_state: atomic state object with old state structures
819 * This function enables all the outputs with the new configuration which had to
820 * be turned off for the update.
822 * For compatibility with legacy crtc helpers this should be called after
823 * drm_atomic_helper_commit_planes(), which is what the default commit function
824 * does. But drivers with different needs can group the modeset commits together
825 * and do the plane commits at the end. This is useful for drivers doing runtime
826 * PM since planes updates then only happen when the CRTC is actually enabled.
828 void drm_atomic_helper_commit_modeset_enables(struct drm_device *dev,
829 struct drm_atomic_state *old_state)
831 struct drm_crtc *crtc;
832 struct drm_crtc_state *old_crtc_state;
833 struct drm_connector *connector;
834 struct drm_connector_state *old_conn_state;
837 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
838 const struct drm_crtc_helper_funcs *funcs;
840 /* Need to filter out CRTCs where only planes change. */
841 if (!drm_atomic_crtc_needs_modeset(crtc->state))
844 if (!crtc->state->active)
847 funcs = crtc->helper_private;
849 if (crtc->state->enable) {
850 DRM_DEBUG_ATOMIC("enabling [CRTC:%d]\n",
860 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
861 const struct drm_encoder_helper_funcs *funcs;
862 struct drm_encoder *encoder;
864 if (!connector->state->best_encoder)
867 if (!connector->state->crtc->state->active ||
868 !drm_atomic_crtc_needs_modeset(connector->state->crtc->state))
871 encoder = connector->state->best_encoder;
872 funcs = encoder->helper_private;
874 DRM_DEBUG_ATOMIC("enabling [ENCODER:%d:%s]\n",
875 encoder->base.id, encoder->name);
878 * Each encoder has at most one connector (since we always steal
879 * it away), so we won't call enable hooks twice.
881 drm_bridge_pre_enable(encoder->bridge);
884 funcs->enable(encoder);
886 funcs->commit(encoder);
888 drm_bridge_enable(encoder->bridge);
891 EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_enables);
893 static void wait_for_fences(struct drm_device *dev,
894 struct drm_atomic_state *state)
896 struct drm_plane *plane;
897 struct drm_plane_state *plane_state;
900 for_each_plane_in_state(state, plane, plane_state, i) {
901 if (!plane->state->fence)
904 WARN_ON(!plane->state->fb);
906 fence_wait(plane->state->fence, false);
907 fence_put(plane->state->fence);
908 plane->state->fence = NULL;
913 * drm_atomic_helper_framebuffer_changed - check if framebuffer has changed
915 * @old_state: atomic state object with old state structures
918 * Checks whether the framebuffer used for this CRTC changes as a result of
919 * the atomic update. This is useful for drivers which cannot use
920 * drm_atomic_helper_wait_for_vblanks() and need to reimplement its
924 * true if the framebuffer changed.
926 bool drm_atomic_helper_framebuffer_changed(struct drm_device *dev,
927 struct drm_atomic_state *old_state,
928 struct drm_crtc *crtc)
930 struct drm_plane *plane;
931 struct drm_plane_state *old_plane_state;
934 for_each_plane_in_state(old_state, plane, old_plane_state, i) {
935 if (plane->state->crtc != crtc &&
936 old_plane_state->crtc != crtc)
939 if (plane->state->fb != old_plane_state->fb)
945 EXPORT_SYMBOL(drm_atomic_helper_framebuffer_changed);
948 * drm_atomic_helper_wait_for_vblanks - wait for vblank on crtcs
950 * @old_state: atomic state object with old state structures
952 * Helper to, after atomic commit, wait for vblanks on all effected
953 * crtcs (ie. before cleaning up old framebuffers using
954 * drm_atomic_helper_cleanup_planes()). It will only wait on crtcs where the
955 * framebuffers have actually changed to optimize for the legacy cursor and
956 * plane update use-case.
959 drm_atomic_helper_wait_for_vblanks(struct drm_device *dev,
960 struct drm_atomic_state *old_state)
962 struct drm_crtc *crtc;
963 struct drm_crtc_state *old_crtc_state;
966 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
967 /* No one cares about the old state, so abuse it for tracking
968 * and store whether we hold a vblank reference (and should do a
969 * vblank wait) in the ->enable boolean. */
970 old_crtc_state->enable = false;
972 if (!crtc->state->enable)
975 /* Legacy cursor ioctls are completely unsynced, and userspace
976 * relies on that (by doing tons of cursor updates). */
977 if (old_state->legacy_cursor_update)
980 if (!drm_atomic_helper_framebuffer_changed(dev,
984 ret = drm_crtc_vblank_get(crtc);
988 old_crtc_state->enable = true;
989 old_crtc_state->last_vblank_count = drm_crtc_vblank_count(crtc);
992 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
993 if (!old_crtc_state->enable)
996 ret = wait_event_timeout(dev->vblank[i].queue,
997 old_crtc_state->last_vblank_count !=
998 drm_crtc_vblank_count(crtc),
999 msecs_to_jiffies(50));
1001 drm_crtc_vblank_put(crtc);
1004 EXPORT_SYMBOL(drm_atomic_helper_wait_for_vblanks);
1007 * drm_atomic_helper_commit - commit validated state object
1009 * @state: the driver state object
1010 * @async: asynchronous commit
1012 * This function commits a with drm_atomic_helper_check() pre-validated state
1013 * object. This can still fail when e.g. the framebuffer reservation fails. For
1014 * now this doesn't implement asynchronous commits.
1016 * Note that right now this function does not support async commits, and hence
1017 * driver writers must implement their own version for now. Also note that the
1018 * default ordering of how the various stages are called is to match the legacy
1019 * modeset helper library closest. One peculiarity of that is that it doesn't
1020 * mesh well with runtime PM at all.
1022 * For drivers supporting runtime PM the recommended sequence is
1024 * drm_atomic_helper_commit_modeset_disables(dev, state);
1026 * drm_atomic_helper_commit_modeset_enables(dev, state);
1028 * drm_atomic_helper_commit_planes(dev, state, true);
1030 * See the kerneldoc entries for these three functions for more details.
1033 * Zero for success or -errno.
1035 int drm_atomic_helper_commit(struct drm_device *dev,
1036 struct drm_atomic_state *state,
1044 ret = drm_atomic_helper_prepare_planes(dev, state);
1049 * This is the point of no return - everything below never fails except
1050 * when the hw goes bonghits. Which means we can commit the new state on
1051 * the software side now.
1054 drm_atomic_helper_swap_state(dev, state);
1057 * Everything below can be run asynchronously without the need to grab
1058 * any modeset locks at all under one condition: It must be guaranteed
1059 * that the asynchronous work has either been cancelled (if the driver
1060 * supports it, which at least requires that the framebuffers get
1061 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
1062 * before the new state gets committed on the software side with
1063 * drm_atomic_helper_swap_state().
1065 * This scheme allows new atomic state updates to be prepared and
1066 * checked in parallel to the asynchronous completion of the previous
1067 * update. Which is important since compositors need to figure out the
1068 * composition of the next frame right after having submitted the
1072 wait_for_fences(dev, state);
1074 drm_atomic_helper_commit_modeset_disables(dev, state);
1076 drm_atomic_helper_commit_planes(dev, state, false);
1078 drm_atomic_helper_commit_modeset_enables(dev, state);
1080 drm_atomic_helper_wait_for_vblanks(dev, state);
1082 drm_atomic_helper_cleanup_planes(dev, state);
1084 drm_atomic_state_free(state);
1088 EXPORT_SYMBOL(drm_atomic_helper_commit);
1091 * DOC: implementing async commit
1093 * For now the atomic helpers don't support async commit directly. If there is
1094 * real need it could be added though, using the dma-buf fence infrastructure
1095 * for generic synchronization with outstanding rendering.
1097 * For now drivers have to implement async commit themselves, with the following
1098 * sequence being the recommended one:
1100 * 1. Run drm_atomic_helper_prepare_planes() first. This is the only function
1101 * which commit needs to call which can fail, so we want to run it first and
1104 * 2. Synchronize with any outstanding asynchronous commit worker threads which
1105 * might be affected the new state update. This can be done by either cancelling
1106 * or flushing the work items, depending upon whether the driver can deal with
1107 * cancelled updates. Note that it is important to ensure that the framebuffer
1108 * cleanup is still done when cancelling.
1110 * For sufficient parallelism it is recommended to have a work item per crtc
1111 * (for updates which don't touch global state) and a global one. Then we only
1112 * need to synchronize with the crtc work items for changed crtcs and the global
1113 * work item, which allows nice concurrent updates on disjoint sets of crtcs.
1115 * 3. The software state is updated synchronously with
1116 * drm_atomic_helper_swap_state(). Doing this under the protection of all modeset
1117 * locks means concurrent callers never see inconsistent state. And doing this
1118 * while it's guaranteed that no relevant async worker runs means that async
1119 * workers do not need grab any locks. Actually they must not grab locks, for
1120 * otherwise the work flushing will deadlock.
1122 * 4. Schedule a work item to do all subsequent steps, using the split-out
1123 * commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and
1124 * then cleaning up the framebuffers after the old framebuffer is no longer
1129 * drm_atomic_helper_prepare_planes - prepare plane resources before commit
1131 * @state: atomic state object with new state structures
1133 * This function prepares plane state, specifically framebuffers, for the new
1134 * configuration. If any failure is encountered this function will call
1135 * ->cleanup_fb on any already successfully prepared framebuffer.
1138 * 0 on success, negative error code on failure.
1140 int drm_atomic_helper_prepare_planes(struct drm_device *dev,
1141 struct drm_atomic_state *state)
1143 int nplanes = dev->mode_config.num_total_plane;
1146 for (i = 0; i < nplanes; i++) {
1147 const struct drm_plane_helper_funcs *funcs;
1148 struct drm_plane *plane = state->planes[i];
1149 struct drm_plane_state *plane_state = state->plane_states[i];
1154 funcs = plane->helper_private;
1156 if (funcs->prepare_fb) {
1157 ret = funcs->prepare_fb(plane, plane_state);
1166 for (i--; i >= 0; i--) {
1167 const struct drm_plane_helper_funcs *funcs;
1168 struct drm_plane *plane = state->planes[i];
1169 struct drm_plane_state *plane_state = state->plane_states[i];
1174 funcs = plane->helper_private;
1176 if (funcs->cleanup_fb)
1177 funcs->cleanup_fb(plane, plane_state);
1183 EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
1185 bool plane_crtc_active(struct drm_plane_state *state)
1187 return state->crtc && state->crtc->state->active;
1191 * drm_atomic_helper_commit_planes - commit plane state
1193 * @old_state: atomic state object with old state structures
1194 * @active_only: Only commit on active CRTC if set
1196 * This function commits the new plane state using the plane and atomic helper
1197 * functions for planes and crtcs. It assumes that the atomic state has already
1198 * been pushed into the relevant object state pointers, since this step can no
1201 * It still requires the global state object @old_state to know which planes and
1202 * crtcs need to be updated though.
1204 * Note that this function does all plane updates across all CRTCs in one step.
1205 * If the hardware can't support this approach look at
1206 * drm_atomic_helper_commit_planes_on_crtc() instead.
1208 * Plane parameters can be updated by applications while the associated CRTC is
1209 * disabled. The DRM/KMS core will store the parameters in the plane state,
1210 * which will be available to the driver when the CRTC is turned on. As a result
1211 * most drivers don't need to be immediately notified of plane updates for a
1214 * Unless otherwise needed, drivers are advised to set the @active_only
1215 * parameters to true in order not to receive plane update notifications related
1216 * to a disabled CRTC. This avoids the need to manually ignore plane updates in
1217 * driver code when the driver and/or hardware can't or just don't need to deal
1218 * with updates on disabled CRTCs, for example when supporting runtime PM.
1220 * The drm_atomic_helper_commit() default implementation only sets @active_only
1221 * to false to most closely match the behaviour of the legacy helpers. This should
1222 * not be copied blindly by drivers.
1224 void drm_atomic_helper_commit_planes(struct drm_device *dev,
1225 struct drm_atomic_state *old_state,
1228 struct drm_crtc *crtc;
1229 struct drm_crtc_state *old_crtc_state;
1230 struct drm_plane *plane;
1231 struct drm_plane_state *old_plane_state;
1234 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
1235 const struct drm_crtc_helper_funcs *funcs;
1237 funcs = crtc->helper_private;
1239 if (!funcs || !funcs->atomic_begin)
1242 if (active_only && !crtc->state->active)
1245 funcs->atomic_begin(crtc, old_crtc_state);
1248 for_each_plane_in_state(old_state, plane, old_plane_state, i) {
1249 const struct drm_plane_helper_funcs *funcs;
1252 funcs = plane->helper_private;
1257 disabling = drm_atomic_plane_disabling(plane, old_plane_state);
1261 * Skip planes related to inactive CRTCs. If the plane
1262 * is enabled use the state of the current CRTC. If the
1263 * plane is being disabled use the state of the old
1264 * CRTC to avoid skipping planes being disabled on an
1267 if (!disabling && !plane_crtc_active(plane->state))
1269 if (disabling && !plane_crtc_active(old_plane_state))
1274 * Special-case disabling the plane if drivers support it.
1276 if (disabling && funcs->atomic_disable)
1277 funcs->atomic_disable(plane, old_plane_state);
1278 else if (plane->state->crtc || disabling)
1279 funcs->atomic_update(plane, old_plane_state);
1282 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
1283 const struct drm_crtc_helper_funcs *funcs;
1285 funcs = crtc->helper_private;
1287 if (!funcs || !funcs->atomic_flush)
1290 if (active_only && !crtc->state->active)
1293 funcs->atomic_flush(crtc, old_crtc_state);
1296 EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
1299 * drm_atomic_helper_commit_planes_on_crtc - commit plane state for a crtc
1300 * @old_crtc_state: atomic state object with the old crtc state
1302 * This function commits the new plane state using the plane and atomic helper
1303 * functions for planes on the specific crtc. It assumes that the atomic state
1304 * has already been pushed into the relevant object state pointers, since this
1305 * step can no longer fail.
1307 * This function is useful when plane updates should be done crtc-by-crtc
1308 * instead of one global step like drm_atomic_helper_commit_planes() does.
1310 * This function can only be savely used when planes are not allowed to move
1311 * between different CRTCs because this function doesn't handle inter-CRTC
1312 * depencies. Callers need to ensure that either no such depencies exist,
1313 * resolve them through ordering of commit calls or through some other means.
1316 drm_atomic_helper_commit_planes_on_crtc(struct drm_crtc_state *old_crtc_state)
1318 const struct drm_crtc_helper_funcs *crtc_funcs;
1319 struct drm_crtc *crtc = old_crtc_state->crtc;
1320 struct drm_atomic_state *old_state = old_crtc_state->state;
1321 struct drm_plane *plane;
1322 unsigned plane_mask;
1324 plane_mask = old_crtc_state->plane_mask;
1325 plane_mask |= crtc->state->plane_mask;
1327 crtc_funcs = crtc->helper_private;
1328 if (crtc_funcs && crtc_funcs->atomic_begin)
1329 crtc_funcs->atomic_begin(crtc, old_crtc_state);
1331 drm_for_each_plane_mask(plane, crtc->dev, plane_mask) {
1332 struct drm_plane_state *old_plane_state =
1333 drm_atomic_get_existing_plane_state(old_state, plane);
1334 const struct drm_plane_helper_funcs *plane_funcs;
1336 plane_funcs = plane->helper_private;
1338 if (!old_plane_state || !plane_funcs)
1341 WARN_ON(plane->state->crtc && plane->state->crtc != crtc);
1343 if (drm_atomic_plane_disabling(plane, old_plane_state) &&
1344 plane_funcs->atomic_disable)
1345 plane_funcs->atomic_disable(plane, old_plane_state);
1346 else if (plane->state->crtc ||
1347 drm_atomic_plane_disabling(plane, old_plane_state))
1348 plane_funcs->atomic_update(plane, old_plane_state);
1351 if (crtc_funcs && crtc_funcs->atomic_flush)
1352 crtc_funcs->atomic_flush(crtc, old_crtc_state);
1354 EXPORT_SYMBOL(drm_atomic_helper_commit_planes_on_crtc);
1357 * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
1359 * @old_state: atomic state object with old state structures
1361 * This function cleans up plane state, specifically framebuffers, from the old
1362 * configuration. Hence the old configuration must be perserved in @old_state to
1363 * be able to call this function.
1365 * This function must also be called on the new state when the atomic update
1366 * fails at any point after calling drm_atomic_helper_prepare_planes().
1368 void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
1369 struct drm_atomic_state *old_state)
1371 struct drm_plane *plane;
1372 struct drm_plane_state *plane_state;
1375 for_each_plane_in_state(old_state, plane, plane_state, i) {
1376 const struct drm_plane_helper_funcs *funcs;
1378 funcs = plane->helper_private;
1380 if (funcs->cleanup_fb)
1381 funcs->cleanup_fb(plane, plane_state);
1384 EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
1387 * drm_atomic_helper_swap_state - store atomic state into current sw state
1389 * @state: atomic state
1391 * This function stores the atomic state into the current state pointers in all
1392 * driver objects. It should be called after all failing steps have been done
1393 * and succeeded, but before the actual hardware state is committed.
1395 * For cleanup and error recovery the current state for all changed objects will
1396 * be swaped into @state.
1398 * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
1400 * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
1402 * 2. Do any other steps that might fail.
1404 * 3. Put the staged state into the current state pointers with this function.
1406 * 4. Actually commit the hardware state.
1408 * 5. Call drm_atomic_helper_cleanup_planes() with @state, which since step 3
1409 * contains the old state. Also do any other cleanup required with that state.
1411 void drm_atomic_helper_swap_state(struct drm_device *dev,
1412 struct drm_atomic_state *state)
1416 for (i = 0; i < dev->mode_config.num_connector; i++) {
1417 struct drm_connector *connector = state->connectors[i];
1422 connector->state->state = state;
1423 swap(state->connector_states[i], connector->state);
1424 connector->state->state = NULL;
1427 for (i = 0; i < dev->mode_config.num_crtc; i++) {
1428 struct drm_crtc *crtc = state->crtcs[i];
1433 crtc->state->state = state;
1434 swap(state->crtc_states[i], crtc->state);
1435 crtc->state->state = NULL;
1438 for (i = 0; i < dev->mode_config.num_total_plane; i++) {
1439 struct drm_plane *plane = state->planes[i];
1444 plane->state->state = state;
1445 swap(state->plane_states[i], plane->state);
1446 plane->state->state = NULL;
1449 EXPORT_SYMBOL(drm_atomic_helper_swap_state);
1452 * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
1453 * @plane: plane object to update
1454 * @crtc: owning CRTC of owning plane
1455 * @fb: framebuffer to flip onto plane
1456 * @crtc_x: x offset of primary plane on crtc
1457 * @crtc_y: y offset of primary plane on crtc
1458 * @crtc_w: width of primary plane rectangle on crtc
1459 * @crtc_h: height of primary plane rectangle on crtc
1460 * @src_x: x offset of @fb for panning
1461 * @src_y: y offset of @fb for panning
1462 * @src_w: width of source rectangle in @fb
1463 * @src_h: height of source rectangle in @fb
1465 * Provides a default plane update handler using the atomic driver interface.
1468 * Zero on success, error code on failure
1470 int drm_atomic_helper_update_plane(struct drm_plane *plane,
1471 struct drm_crtc *crtc,
1472 struct drm_framebuffer *fb,
1473 int crtc_x, int crtc_y,
1474 unsigned int crtc_w, unsigned int crtc_h,
1475 uint32_t src_x, uint32_t src_y,
1476 uint32_t src_w, uint32_t src_h)
1478 struct drm_atomic_state *state;
1479 struct drm_plane_state *plane_state;
1482 state = drm_atomic_state_alloc(plane->dev);
1486 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1488 plane_state = drm_atomic_get_plane_state(state, plane);
1489 if (IS_ERR(plane_state)) {
1490 ret = PTR_ERR(plane_state);
1494 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
1497 drm_atomic_set_fb_for_plane(plane_state, fb);
1498 plane_state->crtc_x = crtc_x;
1499 plane_state->crtc_y = crtc_y;
1500 plane_state->crtc_h = crtc_h;
1501 plane_state->crtc_w = crtc_w;
1502 plane_state->src_x = src_x;
1503 plane_state->src_y = src_y;
1504 plane_state->src_h = src_h;
1505 plane_state->src_w = src_w;
1507 if (plane == crtc->cursor)
1508 state->legacy_cursor_update = true;
1510 ret = drm_atomic_commit(state);
1514 /* Driver takes ownership of state on successful commit. */
1517 if (ret == -EDEADLK)
1520 drm_atomic_state_free(state);
1524 drm_atomic_state_clear(state);
1525 drm_atomic_legacy_backoff(state);
1528 * Someone might have exchanged the framebuffer while we dropped locks
1529 * in the backoff code. We need to fix up the fb refcount tracking the
1532 plane->old_fb = plane->fb;
1536 EXPORT_SYMBOL(drm_atomic_helper_update_plane);
1539 * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
1540 * @plane: plane to disable
1542 * Provides a default plane disable handler using the atomic driver interface.
1545 * Zero on success, error code on failure
1547 int drm_atomic_helper_disable_plane(struct drm_plane *plane)
1549 struct drm_atomic_state *state;
1550 struct drm_plane_state *plane_state;
1554 * FIXME: Without plane->crtc set we can't get at the implicit legacy
1555 * acquire context. The real fix will be to wire the acquire ctx through
1556 * everywhere we need it, but meanwhile prevent chaos by just skipping
1557 * this noop. The critical case is the cursor ioctls which a) only grab
1558 * crtc/cursor-plane locks (so we need the crtc to get at the right
1559 * acquire context) and b) can try to disable the plane multiple times.
1564 state = drm_atomic_state_alloc(plane->dev);
1568 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(plane->crtc);
1570 plane_state = drm_atomic_get_plane_state(state, plane);
1571 if (IS_ERR(plane_state)) {
1572 ret = PTR_ERR(plane_state);
1576 if (plane_state->crtc && (plane == plane->crtc->cursor))
1577 plane_state->state->legacy_cursor_update = true;
1579 ret = __drm_atomic_helper_disable_plane(plane, plane_state);
1583 ret = drm_atomic_commit(state);
1587 /* Driver takes ownership of state on successful commit. */
1590 if (ret == -EDEADLK)
1593 drm_atomic_state_free(state);
1597 drm_atomic_state_clear(state);
1598 drm_atomic_legacy_backoff(state);
1601 * Someone might have exchanged the framebuffer while we dropped locks
1602 * in the backoff code. We need to fix up the fb refcount tracking the
1605 plane->old_fb = plane->fb;
1609 EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
1611 /* just used from fb-helper and atomic-helper: */
1612 int __drm_atomic_helper_disable_plane(struct drm_plane *plane,
1613 struct drm_plane_state *plane_state)
1617 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
1621 drm_atomic_set_fb_for_plane(plane_state, NULL);
1622 plane_state->crtc_x = 0;
1623 plane_state->crtc_y = 0;
1624 plane_state->crtc_h = 0;
1625 plane_state->crtc_w = 0;
1626 plane_state->src_x = 0;
1627 plane_state->src_y = 0;
1628 plane_state->src_h = 0;
1629 plane_state->src_w = 0;
1634 static int update_output_state(struct drm_atomic_state *state,
1635 struct drm_mode_set *set)
1637 struct drm_device *dev = set->crtc->dev;
1638 struct drm_crtc *crtc;
1639 struct drm_crtc_state *crtc_state;
1640 struct drm_connector *connector;
1641 struct drm_connector_state *conn_state;
1644 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
1645 state->acquire_ctx);
1649 /* First grab all affected connector/crtc states. */
1650 for (i = 0; i < set->num_connectors; i++) {
1651 conn_state = drm_atomic_get_connector_state(state,
1652 set->connectors[i]);
1653 if (IS_ERR(conn_state))
1654 return PTR_ERR(conn_state);
1657 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1658 ret = drm_atomic_add_affected_connectors(state, crtc);
1663 /* Then recompute connector->crtc links and crtc enabling state. */
1664 for_each_connector_in_state(state, connector, conn_state, i) {
1665 if (conn_state->crtc == set->crtc) {
1666 ret = drm_atomic_set_crtc_for_connector(conn_state,
1672 for (j = 0; j < set->num_connectors; j++) {
1673 if (set->connectors[j] == connector) {
1674 ret = drm_atomic_set_crtc_for_connector(conn_state,
1683 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1684 /* Don't update ->enable for the CRTC in the set_config request,
1685 * since a mismatch would indicate a bug in the upper layers.
1686 * The actual modeset code later on will catch any
1687 * inconsistencies here. */
1688 if (crtc == set->crtc)
1691 if (!drm_atomic_connectors_for_crtc(state, crtc)) {
1692 ret = drm_atomic_set_mode_prop_for_crtc(crtc_state,
1697 crtc_state->active = false;
1705 * drm_atomic_helper_set_config - set a new config from userspace
1706 * @set: mode set configuration
1708 * Provides a default crtc set_config handler using the atomic driver interface.
1711 * Returns 0 on success, negative errno numbers on failure.
1713 int drm_atomic_helper_set_config(struct drm_mode_set *set)
1715 struct drm_atomic_state *state;
1716 struct drm_crtc *crtc = set->crtc;
1719 state = drm_atomic_state_alloc(crtc->dev);
1723 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1725 ret = __drm_atomic_helper_set_config(set, state);
1729 ret = drm_atomic_commit(state);
1733 /* Driver takes ownership of state on successful commit. */
1736 if (ret == -EDEADLK)
1739 drm_atomic_state_free(state);
1743 drm_atomic_state_clear(state);
1744 drm_atomic_legacy_backoff(state);
1747 * Someone might have exchanged the framebuffer while we dropped locks
1748 * in the backoff code. We need to fix up the fb refcount tracking the
1751 crtc->primary->old_fb = crtc->primary->fb;
1755 EXPORT_SYMBOL(drm_atomic_helper_set_config);
1757 /* just used from fb-helper and atomic-helper: */
1758 int __drm_atomic_helper_set_config(struct drm_mode_set *set,
1759 struct drm_atomic_state *state)
1761 struct drm_crtc_state *crtc_state;
1762 struct drm_plane_state *primary_state;
1763 struct drm_crtc *crtc = set->crtc;
1764 int hdisplay, vdisplay;
1767 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1768 if (IS_ERR(crtc_state))
1769 return PTR_ERR(crtc_state);
1771 primary_state = drm_atomic_get_plane_state(state, crtc->primary);
1772 if (IS_ERR(primary_state))
1773 return PTR_ERR(primary_state);
1777 WARN_ON(set->num_connectors);
1779 ret = drm_atomic_set_mode_for_crtc(crtc_state, NULL);
1783 crtc_state->active = false;
1785 ret = drm_atomic_set_crtc_for_plane(primary_state, NULL);
1789 drm_atomic_set_fb_for_plane(primary_state, NULL);
1795 WARN_ON(!set->num_connectors);
1797 ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
1801 crtc_state->active = true;
1803 ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
1807 drm_crtc_get_hv_timing(set->mode, &hdisplay, &vdisplay);
1809 drm_atomic_set_fb_for_plane(primary_state, set->fb);
1810 primary_state->crtc_x = 0;
1811 primary_state->crtc_y = 0;
1812 primary_state->crtc_h = vdisplay;
1813 primary_state->crtc_w = hdisplay;
1814 primary_state->src_x = set->x << 16;
1815 primary_state->src_y = set->y << 16;
1816 if (primary_state->rotation & (BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270))) {
1817 primary_state->src_h = hdisplay << 16;
1818 primary_state->src_w = vdisplay << 16;
1820 primary_state->src_h = vdisplay << 16;
1821 primary_state->src_w = hdisplay << 16;
1825 ret = update_output_state(state, set);
1833 * drm_atomic_helper_disable_all - disable all currently active outputs
1835 * @ctx: lock acquisition context
1837 * Loops through all connectors, finding those that aren't turned off and then
1838 * turns them off by setting their DPMS mode to OFF and deactivating the CRTC
1839 * that they are connected to.
1841 * This is used for example in suspend/resume to disable all currently active
1842 * functions when suspending.
1844 * Note that if callers haven't already acquired all modeset locks this might
1845 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
1848 * 0 on success or a negative error code on failure.
1851 * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
1853 int drm_atomic_helper_disable_all(struct drm_device *dev,
1854 struct drm_modeset_acquire_ctx *ctx)
1856 struct drm_atomic_state *state;
1857 struct drm_connector *conn;
1860 state = drm_atomic_state_alloc(dev);
1864 state->acquire_ctx = ctx;
1866 drm_for_each_connector(conn, dev) {
1867 struct drm_crtc *crtc = conn->state->crtc;
1868 struct drm_crtc_state *crtc_state;
1870 if (!crtc || conn->dpms != DRM_MODE_DPMS_ON)
1873 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1874 if (IS_ERR(crtc_state)) {
1875 err = PTR_ERR(crtc_state);
1879 crtc_state->active = false;
1882 err = drm_atomic_commit(state);
1886 drm_atomic_state_free(state);
1890 EXPORT_SYMBOL(drm_atomic_helper_disable_all);
1893 * drm_atomic_helper_suspend - subsystem-level suspend helper
1896 * Duplicates the current atomic state, disables all active outputs and then
1897 * returns a pointer to the original atomic state to the caller. Drivers can
1898 * pass this pointer to the drm_atomic_helper_resume() helper upon resume to
1899 * restore the output configuration that was active at the time the system
1902 * Note that it is potentially unsafe to use this. The atomic state object
1903 * returned by this function is assumed to be persistent. Drivers must ensure
1904 * that this holds true. Before calling this function, drivers must make sure
1905 * to suspend fbdev emulation so that nothing can be using the device.
1908 * A pointer to a copy of the state before suspend on success or an ERR_PTR()-
1909 * encoded error code on failure. Drivers should store the returned atomic
1910 * state object and pass it to the drm_atomic_helper_resume() helper upon
1914 * drm_atomic_helper_duplicate_state(), drm_atomic_helper_disable_all(),
1915 * drm_atomic_helper_resume()
1917 struct drm_atomic_state *drm_atomic_helper_suspend(struct drm_device *dev)
1919 struct drm_modeset_acquire_ctx ctx;
1920 struct drm_atomic_state *state;
1923 drm_modeset_acquire_init(&ctx, 0);
1926 err = drm_modeset_lock_all_ctx(dev, &ctx);
1928 state = ERR_PTR(err);
1932 state = drm_atomic_helper_duplicate_state(dev, &ctx);
1936 err = drm_atomic_helper_disable_all(dev, &ctx);
1938 drm_atomic_state_free(state);
1939 state = ERR_PTR(err);
1944 if (PTR_ERR(state) == -EDEADLK) {
1945 drm_modeset_backoff(&ctx);
1949 drm_modeset_drop_locks(&ctx);
1950 drm_modeset_acquire_fini(&ctx);
1953 EXPORT_SYMBOL(drm_atomic_helper_suspend);
1956 * drm_atomic_helper_resume - subsystem-level resume helper
1958 * @state: atomic state to resume to
1960 * Calls drm_mode_config_reset() to synchronize hardware and software states,
1961 * grabs all modeset locks and commits the atomic state object. This can be
1962 * used in conjunction with the drm_atomic_helper_suspend() helper to
1963 * implement suspend/resume for drivers that support atomic mode-setting.
1966 * 0 on success or a negative error code on failure.
1969 * drm_atomic_helper_suspend()
1971 int drm_atomic_helper_resume(struct drm_device *dev,
1972 struct drm_atomic_state *state)
1974 struct drm_mode_config *config = &dev->mode_config;
1977 drm_mode_config_reset(dev);
1978 drm_modeset_lock_all(dev);
1979 state->acquire_ctx = config->acquire_ctx;
1980 err = drm_atomic_commit(state);
1981 drm_modeset_unlock_all(dev);
1985 EXPORT_SYMBOL(drm_atomic_helper_resume);
1988 * drm_atomic_helper_crtc_set_property - helper for crtc properties
1990 * @property: DRM property
1991 * @val: value of property
1993 * Provides a default crtc set_property handler using the atomic driver
1997 * Zero on success, error code on failure
2000 drm_atomic_helper_crtc_set_property(struct drm_crtc *crtc,
2001 struct drm_property *property,
2004 struct drm_atomic_state *state;
2005 struct drm_crtc_state *crtc_state;
2008 state = drm_atomic_state_alloc(crtc->dev);
2012 /* ->set_property is always called with all locks held. */
2013 state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
2015 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2016 if (IS_ERR(crtc_state)) {
2017 ret = PTR_ERR(crtc_state);
2021 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
2026 ret = drm_atomic_commit(state);
2030 /* Driver takes ownership of state on successful commit. */
2033 if (ret == -EDEADLK)
2036 drm_atomic_state_free(state);
2040 drm_atomic_state_clear(state);
2041 drm_atomic_legacy_backoff(state);
2045 EXPORT_SYMBOL(drm_atomic_helper_crtc_set_property);
2048 * drm_atomic_helper_plane_set_property - helper for plane properties
2050 * @property: DRM property
2051 * @val: value of property
2053 * Provides a default plane set_property handler using the atomic driver
2057 * Zero on success, error code on failure
2060 drm_atomic_helper_plane_set_property(struct drm_plane *plane,
2061 struct drm_property *property,
2064 struct drm_atomic_state *state;
2065 struct drm_plane_state *plane_state;
2068 state = drm_atomic_state_alloc(plane->dev);
2072 /* ->set_property is always called with all locks held. */
2073 state->acquire_ctx = plane->dev->mode_config.acquire_ctx;
2075 plane_state = drm_atomic_get_plane_state(state, plane);
2076 if (IS_ERR(plane_state)) {
2077 ret = PTR_ERR(plane_state);
2081 ret = drm_atomic_plane_set_property(plane, plane_state,
2086 ret = drm_atomic_commit(state);
2090 /* Driver takes ownership of state on successful commit. */
2093 if (ret == -EDEADLK)
2096 drm_atomic_state_free(state);
2100 drm_atomic_state_clear(state);
2101 drm_atomic_legacy_backoff(state);
2105 EXPORT_SYMBOL(drm_atomic_helper_plane_set_property);
2108 * drm_atomic_helper_connector_set_property - helper for connector properties
2109 * @connector: DRM connector
2110 * @property: DRM property
2111 * @val: value of property
2113 * Provides a default connector set_property handler using the atomic driver
2117 * Zero on success, error code on failure
2120 drm_atomic_helper_connector_set_property(struct drm_connector *connector,
2121 struct drm_property *property,
2124 struct drm_atomic_state *state;
2125 struct drm_connector_state *connector_state;
2128 state = drm_atomic_state_alloc(connector->dev);
2132 /* ->set_property is always called with all locks held. */
2133 state->acquire_ctx = connector->dev->mode_config.acquire_ctx;
2135 connector_state = drm_atomic_get_connector_state(state, connector);
2136 if (IS_ERR(connector_state)) {
2137 ret = PTR_ERR(connector_state);
2141 ret = drm_atomic_connector_set_property(connector, connector_state,
2146 ret = drm_atomic_commit(state);
2150 /* Driver takes ownership of state on successful commit. */
2153 if (ret == -EDEADLK)
2156 drm_atomic_state_free(state);
2160 drm_atomic_state_clear(state);
2161 drm_atomic_legacy_backoff(state);
2165 EXPORT_SYMBOL(drm_atomic_helper_connector_set_property);
2168 * drm_atomic_helper_page_flip - execute a legacy page flip
2170 * @fb: DRM framebuffer
2171 * @event: optional DRM event to signal upon completion
2172 * @flags: flip flags for non-vblank sync'ed updates
2174 * Provides a default page flip implementation using the atomic driver interface.
2176 * Note that for now so called async page flips (i.e. updates which are not
2177 * synchronized to vblank) are not supported, since the atomic interfaces have
2178 * no provisions for this yet.
2181 * Returns 0 on success, negative errno numbers on failure.
2183 int drm_atomic_helper_page_flip(struct drm_crtc *crtc,
2184 struct drm_framebuffer *fb,
2185 struct drm_pending_vblank_event *event,
2188 struct drm_plane *plane = crtc->primary;
2189 struct drm_atomic_state *state;
2190 struct drm_plane_state *plane_state;
2191 struct drm_crtc_state *crtc_state;
2194 if (flags & DRM_MODE_PAGE_FLIP_ASYNC)
2197 state = drm_atomic_state_alloc(plane->dev);
2201 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2203 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2204 if (IS_ERR(crtc_state)) {
2205 ret = PTR_ERR(crtc_state);
2208 crtc_state->event = event;
2210 plane_state = drm_atomic_get_plane_state(state, plane);
2211 if (IS_ERR(plane_state)) {
2212 ret = PTR_ERR(plane_state);
2216 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
2219 drm_atomic_set_fb_for_plane(plane_state, fb);
2221 ret = drm_atomic_async_commit(state);
2225 /* Driver takes ownership of state on successful async commit. */
2228 if (ret == -EDEADLK)
2231 drm_atomic_state_free(state);
2235 drm_atomic_state_clear(state);
2236 drm_atomic_legacy_backoff(state);
2239 * Someone might have exchanged the framebuffer while we dropped locks
2240 * in the backoff code. We need to fix up the fb refcount tracking the
2243 plane->old_fb = plane->fb;
2247 EXPORT_SYMBOL(drm_atomic_helper_page_flip);
2250 * drm_atomic_helper_connector_dpms() - connector dpms helper implementation
2251 * @connector: affected connector
2254 * This is the main helper function provided by the atomic helper framework for
2255 * implementing the legacy DPMS connector interface. It computes the new desired
2256 * ->active state for the corresponding CRTC (if the connector is enabled) and
2260 * Returns 0 on success, negative errno numbers on failure.
2262 int drm_atomic_helper_connector_dpms(struct drm_connector *connector,
2265 struct drm_mode_config *config = &connector->dev->mode_config;
2266 struct drm_atomic_state *state;
2267 struct drm_crtc_state *crtc_state;
2268 struct drm_crtc *crtc;
2269 struct drm_connector *tmp_connector;
2271 bool active = false;
2272 int old_mode = connector->dpms;
2274 if (mode != DRM_MODE_DPMS_ON)
2275 mode = DRM_MODE_DPMS_OFF;
2277 connector->dpms = mode;
2278 crtc = connector->state->crtc;
2283 state = drm_atomic_state_alloc(connector->dev);
2287 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2289 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2290 if (IS_ERR(crtc_state)) {
2291 ret = PTR_ERR(crtc_state);
2295 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
2297 drm_for_each_connector(tmp_connector, connector->dev) {
2298 if (tmp_connector->state->crtc != crtc)
2301 if (tmp_connector->dpms == DRM_MODE_DPMS_ON) {
2306 crtc_state->active = active;
2308 ret = drm_atomic_commit(state);
2312 /* Driver takes ownership of state on successful commit. */
2315 if (ret == -EDEADLK)
2318 connector->dpms = old_mode;
2319 drm_atomic_state_free(state);
2323 drm_atomic_state_clear(state);
2324 drm_atomic_legacy_backoff(state);
2328 EXPORT_SYMBOL(drm_atomic_helper_connector_dpms);
2331 * DOC: atomic state reset and initialization
2333 * Both the drm core and the atomic helpers assume that there is always the full
2334 * and correct atomic software state for all connectors, CRTCs and planes
2335 * available. Which is a bit a problem on driver load and also after system
2336 * suspend. One way to solve this is to have a hardware state read-out
2337 * infrastructure which reconstructs the full software state (e.g. the i915
2340 * The simpler solution is to just reset the software state to everything off,
2341 * which is easiest to do by calling drm_mode_config_reset(). To facilitate this
2342 * the atomic helpers provide default reset implementations for all hooks.
2346 * drm_atomic_helper_crtc_reset - default ->reset hook for CRTCs
2349 * Resets the atomic state for @crtc by freeing the state pointer (which might
2350 * be NULL, e.g. at driver load time) and allocating a new empty state object.
2352 void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc)
2354 if (crtc->state && crtc->state->mode_blob)
2355 drm_property_unreference_blob(crtc->state->mode_blob);
2357 crtc->state = kzalloc(sizeof(*crtc->state), GFP_KERNEL);
2360 crtc->state->crtc = crtc;
2362 EXPORT_SYMBOL(drm_atomic_helper_crtc_reset);
2365 * __drm_atomic_helper_crtc_duplicate_state - copy atomic CRTC state
2366 * @crtc: CRTC object
2367 * @state: atomic CRTC state
2369 * Copies atomic state from a CRTC's current state and resets inferred values.
2370 * This is useful for drivers that subclass the CRTC state.
2372 void __drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc,
2373 struct drm_crtc_state *state)
2375 memcpy(state, crtc->state, sizeof(*state));
2377 if (state->mode_blob)
2378 drm_property_reference_blob(state->mode_blob);
2379 state->mode_changed = false;
2380 state->active_changed = false;
2381 state->planes_changed = false;
2382 state->connectors_changed = false;
2383 state->event = NULL;
2385 EXPORT_SYMBOL(__drm_atomic_helper_crtc_duplicate_state);
2388 * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
2391 * Default CRTC state duplicate hook for drivers which don't have their own
2392 * subclassed CRTC state structure.
2394 struct drm_crtc_state *
2395 drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc)
2397 struct drm_crtc_state *state;
2399 if (WARN_ON(!crtc->state))
2402 state = kmalloc(sizeof(*state), GFP_KERNEL);
2404 __drm_atomic_helper_crtc_duplicate_state(crtc, state);
2408 EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state);
2411 * __drm_atomic_helper_crtc_destroy_state - release CRTC state
2412 * @crtc: CRTC object
2413 * @state: CRTC state object to release
2415 * Releases all resources stored in the CRTC state without actually freeing
2416 * the memory of the CRTC state. This is useful for drivers that subclass the
2419 void __drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
2420 struct drm_crtc_state *state)
2422 if (state->mode_blob)
2423 drm_property_unreference_blob(state->mode_blob);
2425 EXPORT_SYMBOL(__drm_atomic_helper_crtc_destroy_state);
2428 * drm_atomic_helper_crtc_destroy_state - default state destroy hook
2430 * @state: CRTC state object to release
2432 * Default CRTC state destroy hook for drivers which don't have their own
2433 * subclassed CRTC state structure.
2435 void drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
2436 struct drm_crtc_state *state)
2438 __drm_atomic_helper_crtc_destroy_state(crtc, state);
2441 EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state);
2444 * drm_atomic_helper_plane_reset - default ->reset hook for planes
2447 * Resets the atomic state for @plane by freeing the state pointer (which might
2448 * be NULL, e.g. at driver load time) and allocating a new empty state object.
2450 void drm_atomic_helper_plane_reset(struct drm_plane *plane)
2452 if (plane->state && plane->state->fb)
2453 drm_framebuffer_unreference(plane->state->fb);
2455 kfree(plane->state);
2456 plane->state = kzalloc(sizeof(*plane->state), GFP_KERNEL);
2459 plane->state->plane = plane;
2461 EXPORT_SYMBOL(drm_atomic_helper_plane_reset);
2464 * __drm_atomic_helper_plane_duplicate_state - copy atomic plane state
2465 * @plane: plane object
2466 * @state: atomic plane state
2468 * Copies atomic state from a plane's current state. This is useful for
2469 * drivers that subclass the plane state.
2471 void __drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane,
2472 struct drm_plane_state *state)
2474 memcpy(state, plane->state, sizeof(*state));
2477 drm_framebuffer_reference(state->fb);
2479 EXPORT_SYMBOL(__drm_atomic_helper_plane_duplicate_state);
2482 * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
2485 * Default plane state duplicate hook for drivers which don't have their own
2486 * subclassed plane state structure.
2488 struct drm_plane_state *
2489 drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane)
2491 struct drm_plane_state *state;
2493 if (WARN_ON(!plane->state))
2496 state = kmalloc(sizeof(*state), GFP_KERNEL);
2498 __drm_atomic_helper_plane_duplicate_state(plane, state);
2502 EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state);
2505 * __drm_atomic_helper_plane_destroy_state - release plane state
2506 * @plane: plane object
2507 * @state: plane state object to release
2509 * Releases all resources stored in the plane state without actually freeing
2510 * the memory of the plane state. This is useful for drivers that subclass the
2513 void __drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
2514 struct drm_plane_state *state)
2517 drm_framebuffer_unreference(state->fb);
2519 EXPORT_SYMBOL(__drm_atomic_helper_plane_destroy_state);
2522 * drm_atomic_helper_plane_destroy_state - default state destroy hook
2524 * @state: plane state object to release
2526 * Default plane state destroy hook for drivers which don't have their own
2527 * subclassed plane state structure.
2529 void drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
2530 struct drm_plane_state *state)
2532 __drm_atomic_helper_plane_destroy_state(plane, state);
2535 EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state);
2538 * drm_atomic_helper_connector_reset - default ->reset hook for connectors
2539 * @connector: drm connector
2541 * Resets the atomic state for @connector by freeing the state pointer (which
2542 * might be NULL, e.g. at driver load time) and allocating a new empty state
2545 void drm_atomic_helper_connector_reset(struct drm_connector *connector)
2547 kfree(connector->state);
2548 connector->state = kzalloc(sizeof(*connector->state), GFP_KERNEL);
2550 if (connector->state)
2551 connector->state->connector = connector;
2553 EXPORT_SYMBOL(drm_atomic_helper_connector_reset);
2556 * __drm_atomic_helper_connector_duplicate_state - copy atomic connector state
2557 * @connector: connector object
2558 * @state: atomic connector state
2560 * Copies atomic state from a connector's current state. This is useful for
2561 * drivers that subclass the connector state.
2564 __drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector,
2565 struct drm_connector_state *state)
2567 memcpy(state, connector->state, sizeof(*state));
2569 EXPORT_SYMBOL(__drm_atomic_helper_connector_duplicate_state);
2572 * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
2573 * @connector: drm connector
2575 * Default connector state duplicate hook for drivers which don't have their own
2576 * subclassed connector state structure.
2578 struct drm_connector_state *
2579 drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
2581 struct drm_connector_state *state;
2583 if (WARN_ON(!connector->state))
2586 state = kmalloc(sizeof(*state), GFP_KERNEL);
2588 __drm_atomic_helper_connector_duplicate_state(connector, state);
2592 EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
2595 * drm_atomic_helper_duplicate_state - duplicate an atomic state object
2597 * @ctx: lock acquisition context
2599 * Makes a copy of the current atomic state by looping over all objects and
2600 * duplicating their respective states. This is used for example by suspend/
2601 * resume support code to save the state prior to suspend such that it can
2602 * be restored upon resume.
2604 * Note that this treats atomic state as persistent between save and restore.
2605 * Drivers must make sure that this is possible and won't result in confusion
2606 * or erroneous behaviour.
2608 * Note that if callers haven't already acquired all modeset locks this might
2609 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
2612 * A pointer to the copy of the atomic state object on success or an
2613 * ERR_PTR()-encoded error code on failure.
2616 * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
2618 struct drm_atomic_state *
2619 drm_atomic_helper_duplicate_state(struct drm_device *dev,
2620 struct drm_modeset_acquire_ctx *ctx)
2622 struct drm_atomic_state *state;
2623 struct drm_connector *conn;
2624 struct drm_plane *plane;
2625 struct drm_crtc *crtc;
2628 state = drm_atomic_state_alloc(dev);
2630 return ERR_PTR(-ENOMEM);
2632 state->acquire_ctx = ctx;
2634 drm_for_each_crtc(crtc, dev) {
2635 struct drm_crtc_state *crtc_state;
2637 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2638 if (IS_ERR(crtc_state)) {
2639 err = PTR_ERR(crtc_state);
2644 drm_for_each_plane(plane, dev) {
2645 struct drm_plane_state *plane_state;
2647 plane_state = drm_atomic_get_plane_state(state, plane);
2648 if (IS_ERR(plane_state)) {
2649 err = PTR_ERR(plane_state);
2654 drm_for_each_connector(conn, dev) {
2655 struct drm_connector_state *conn_state;
2657 conn_state = drm_atomic_get_connector_state(state, conn);
2658 if (IS_ERR(conn_state)) {
2659 err = PTR_ERR(conn_state);
2664 /* clear the acquire context so that it isn't accidentally reused */
2665 state->acquire_ctx = NULL;
2669 drm_atomic_state_free(state);
2670 state = ERR_PTR(err);
2675 EXPORT_SYMBOL(drm_atomic_helper_duplicate_state);
2678 * __drm_atomic_helper_connector_destroy_state - release connector state
2679 * @connector: connector object
2680 * @state: connector state object to release
2682 * Releases all resources stored in the connector state without actually
2683 * freeing the memory of the connector state. This is useful for drivers that
2684 * subclass the connector state.
2687 __drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
2688 struct drm_connector_state *state)
2691 * This is currently a placeholder so that drivers that subclass the
2692 * state will automatically do the right thing if code is ever added
2696 EXPORT_SYMBOL(__drm_atomic_helper_connector_destroy_state);
2699 * drm_atomic_helper_connector_destroy_state - default state destroy hook
2700 * @connector: drm connector
2701 * @state: connector state object to release
2703 * Default connector state destroy hook for drivers which don't have their own
2704 * subclassed connector state structure.
2706 void drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
2707 struct drm_connector_state *state)
2709 __drm_atomic_helper_connector_destroy_state(connector, state);
2712 EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state);