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.
29 #include <drm/drm_atomic.h>
30 #include <drm/drm_plane_helper.h>
31 #include <drm/drm_crtc_helper.h>
32 #include <drm/drm_atomic_helper.h>
33 #include <linux/fence.h>
38 * This helper library provides implementations of check and commit functions on
39 * top of the CRTC modeset helper callbacks and the plane helper callbacks. It
40 * also provides convenience implementations for the atomic state handling
41 * callbacks for drivers which don't need to subclass the drm core structures to
42 * add their own additional internal state.
44 * This library also provides default implementations for the check callback in
45 * drm_atomic_helper_check() and for the commit callback with
46 * drm_atomic_helper_commit(). But the individual stages and callbacks are
47 * exposed to allow drivers to mix and match and e.g. use the plane helpers only
48 * together with a driver private modeset implementation.
50 * This library also provides implementations for all the legacy driver
51 * interfaces on top of the atomic interface. See drm_atomic_helper_set_config(),
52 * drm_atomic_helper_disable_plane(), drm_atomic_helper_disable_plane() and the
53 * various functions to implement set_property callbacks. New drivers must not
54 * implement these functions themselves but must use the provided helpers.
56 * The atomic helper uses the same function table structures as all other
57 * modesetting helpers. See the documentation for struct &drm_crtc_helper_funcs,
58 * struct &drm_encoder_helper_funcs and struct &drm_connector_helper_funcs. It
59 * also shares the struct &drm_plane_helper_funcs function table with the plane
63 drm_atomic_helper_plane_changed(struct drm_atomic_state *state,
64 struct drm_plane_state *plane_state,
65 struct drm_plane *plane)
67 struct drm_crtc_state *crtc_state;
69 if (plane->state->crtc) {
70 crtc_state = state->crtc_states[drm_crtc_index(plane->state->crtc)];
72 if (WARN_ON(!crtc_state))
75 crtc_state->planes_changed = true;
78 if (plane_state->crtc) {
80 state->crtc_states[drm_crtc_index(plane_state->crtc)];
82 if (WARN_ON(!crtc_state))
85 crtc_state->planes_changed = true;
90 check_pending_encoder_assignment(struct drm_atomic_state *state,
91 struct drm_encoder *new_encoder)
93 struct drm_connector *connector;
94 struct drm_connector_state *conn_state;
97 for_each_connector_in_state(state, connector, conn_state, i) {
98 if (conn_state->best_encoder != new_encoder)
101 /* encoder already assigned and we're trying to re-steal it! */
102 if (connector->state->best_encoder != conn_state->best_encoder)
109 static struct drm_crtc *
110 get_current_crtc_for_encoder(struct drm_device *dev,
111 struct drm_encoder *encoder)
113 struct drm_mode_config *config = &dev->mode_config;
114 struct drm_connector *connector;
116 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
118 drm_for_each_connector(connector, dev) {
119 if (connector->state->best_encoder != encoder)
122 return connector->state->crtc;
129 set_best_encoder(struct drm_atomic_state *state,
130 struct drm_connector_state *conn_state,
131 struct drm_encoder *encoder)
133 struct drm_crtc_state *crtc_state;
134 struct drm_crtc *crtc;
136 if (conn_state->best_encoder) {
137 /* Unset the encoder_mask in the old crtc state. */
138 crtc = conn_state->connector->state->crtc;
140 /* A NULL crtc is an error here because we should have
141 * duplicated a NULL best_encoder when crtc was NULL.
142 * As an exception restoring duplicated atomic state
143 * during resume is allowed, so don't warn when
144 * best_encoder is equal to encoder we intend to set.
146 WARN_ON(!crtc && encoder != conn_state->best_encoder);
148 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
150 crtc_state->encoder_mask &=
151 ~(1 << drm_encoder_index(conn_state->best_encoder));
156 crtc = conn_state->crtc;
159 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
161 crtc_state->encoder_mask |=
162 1 << drm_encoder_index(encoder);
166 conn_state->best_encoder = encoder;
170 steal_encoder(struct drm_atomic_state *state,
171 struct drm_encoder *encoder,
172 struct drm_crtc *encoder_crtc)
174 struct drm_mode_config *config = &state->dev->mode_config;
175 struct drm_crtc_state *crtc_state;
176 struct drm_connector *connector;
177 struct drm_connector_state *connector_state;
180 * We can only steal an encoder coming from a connector, which means we
181 * must already hold the connection_mutex.
183 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
185 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s], stealing it\n",
186 encoder->base.id, encoder->name,
187 encoder_crtc->base.id, encoder_crtc->name);
189 crtc_state = drm_atomic_get_crtc_state(state, encoder_crtc);
190 if (IS_ERR(crtc_state))
191 return PTR_ERR(crtc_state);
193 crtc_state->connectors_changed = true;
195 list_for_each_entry(connector, &config->connector_list, head) {
196 if (connector->state->best_encoder != encoder)
199 DRM_DEBUG_ATOMIC("Stealing encoder from [CONNECTOR:%d:%s]\n",
203 connector_state = drm_atomic_get_connector_state(state,
205 if (IS_ERR(connector_state))
206 return PTR_ERR(connector_state);
208 if (connector_state->best_encoder != encoder)
211 set_best_encoder(state, connector_state, NULL);
218 update_connector_routing(struct drm_atomic_state *state, int conn_idx)
220 const struct drm_connector_helper_funcs *funcs;
221 struct drm_encoder *new_encoder;
222 struct drm_crtc *encoder_crtc;
223 struct drm_connector *connector;
224 struct drm_connector_state *connector_state;
225 struct drm_crtc_state *crtc_state;
228 connector = state->connectors[conn_idx];
229 connector_state = state->connector_states[conn_idx];
234 DRM_DEBUG_ATOMIC("Updating routing for [CONNECTOR:%d:%s]\n",
238 if (connector->state->crtc != connector_state->crtc) {
239 if (connector->state->crtc) {
240 idx = drm_crtc_index(connector->state->crtc);
242 crtc_state = state->crtc_states[idx];
243 crtc_state->connectors_changed = true;
246 if (connector_state->crtc) {
247 idx = drm_crtc_index(connector_state->crtc);
249 crtc_state = state->crtc_states[idx];
250 crtc_state->connectors_changed = true;
254 if (!connector_state->crtc) {
255 DRM_DEBUG_ATOMIC("Disabling [CONNECTOR:%d:%s]\n",
259 set_best_encoder(state, connector_state, NULL);
264 funcs = connector->helper_private;
266 if (funcs->atomic_best_encoder)
267 new_encoder = funcs->atomic_best_encoder(connector,
270 new_encoder = funcs->best_encoder(connector);
273 DRM_DEBUG_ATOMIC("No suitable encoder found for [CONNECTOR:%d:%s]\n",
279 if (!drm_encoder_crtc_ok(new_encoder, connector_state->crtc)) {
280 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] incompatible with [CRTC:%d]\n",
281 new_encoder->base.id,
283 connector_state->crtc->base.id);
287 if (new_encoder == connector_state->best_encoder) {
288 set_best_encoder(state, connector_state, new_encoder);
290 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] keeps [ENCODER:%d:%s], now on [CRTC:%d:%s]\n",
293 new_encoder->base.id,
295 connector_state->crtc->base.id,
296 connector_state->crtc->name);
301 if (!check_pending_encoder_assignment(state, new_encoder)) {
302 DRM_DEBUG_ATOMIC("Encoder for [CONNECTOR:%d:%s] already assigned\n",
308 encoder_crtc = get_current_crtc_for_encoder(state->dev,
312 ret = steal_encoder(state, new_encoder, encoder_crtc);
314 DRM_DEBUG_ATOMIC("Encoder stealing failed for [CONNECTOR:%d:%s]\n",
321 if (WARN_ON(!connector_state->crtc))
324 set_best_encoder(state, connector_state, new_encoder);
326 idx = drm_crtc_index(connector_state->crtc);
328 crtc_state = state->crtc_states[idx];
329 crtc_state->connectors_changed = true;
331 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] using [ENCODER:%d:%s] on [CRTC:%d:%s]\n",
334 new_encoder->base.id,
336 connector_state->crtc->base.id,
337 connector_state->crtc->name);
343 mode_fixup(struct drm_atomic_state *state)
345 struct drm_crtc *crtc;
346 struct drm_crtc_state *crtc_state;
347 struct drm_connector *connector;
348 struct drm_connector_state *conn_state;
352 for_each_crtc_in_state(state, crtc, crtc_state, i) {
353 if (!crtc_state->mode_changed &&
354 !crtc_state->connectors_changed)
357 drm_mode_copy(&crtc_state->adjusted_mode, &crtc_state->mode);
360 for_each_connector_in_state(state, connector, conn_state, i) {
361 const struct drm_encoder_helper_funcs *funcs;
362 struct drm_encoder *encoder;
364 WARN_ON(!!conn_state->best_encoder != !!conn_state->crtc);
366 if (!conn_state->crtc || !conn_state->best_encoder)
370 state->crtc_states[drm_crtc_index(conn_state->crtc)];
373 * Each encoder has at most one connector (since we always steal
374 * it away), so we won't call ->mode_fixup twice.
376 encoder = conn_state->best_encoder;
377 funcs = encoder->helper_private;
381 ret = drm_bridge_mode_fixup(encoder->bridge, &crtc_state->mode,
382 &crtc_state->adjusted_mode);
384 DRM_DEBUG_ATOMIC("Bridge fixup failed\n");
388 if (funcs->atomic_check) {
389 ret = funcs->atomic_check(encoder, crtc_state,
392 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] check failed\n",
393 encoder->base.id, encoder->name);
396 } else if (funcs->mode_fixup) {
397 ret = funcs->mode_fixup(encoder, &crtc_state->mode,
398 &crtc_state->adjusted_mode);
400 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] fixup failed\n",
401 encoder->base.id, encoder->name);
407 for_each_crtc_in_state(state, crtc, crtc_state, i) {
408 const struct drm_crtc_helper_funcs *funcs;
410 if (!crtc_state->mode_changed &&
411 !crtc_state->connectors_changed)
414 funcs = crtc->helper_private;
415 if (!funcs->mode_fixup)
418 ret = funcs->mode_fixup(crtc, &crtc_state->mode,
419 &crtc_state->adjusted_mode);
421 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] fixup failed\n",
422 crtc->base.id, crtc->name);
431 * drm_atomic_helper_check_modeset - validate state object for modeset changes
433 * @state: the driver state object
435 * Check the state object to see if the requested state is physically possible.
436 * This does all the crtc and connector related computations for an atomic
437 * update and adds any additional connectors needed for full modesets and calls
438 * down into ->mode_fixup functions of the driver backend.
440 * crtc_state->mode_changed is set when the input mode is changed.
441 * crtc_state->connectors_changed is set when a connector is added or
442 * removed from the crtc.
443 * crtc_state->active_changed is set when crtc_state->active changes,
444 * which is used for dpms.
448 * Drivers which update ->mode_changed (e.g. in their ->atomic_check hooks if a
449 * plane update can't be done without a full modeset) _must_ call this function
450 * afterwards after that change. It is permitted to call this function multiple
451 * times for the same update, e.g. when the ->atomic_check functions depend upon
452 * the adjusted dotclock for fifo space allocation and watermark computation.
455 * Zero for success or -errno
458 drm_atomic_helper_check_modeset(struct drm_device *dev,
459 struct drm_atomic_state *state)
461 struct drm_crtc *crtc;
462 struct drm_crtc_state *crtc_state;
463 struct drm_connector *connector;
464 struct drm_connector_state *connector_state;
467 for_each_crtc_in_state(state, crtc, crtc_state, i) {
468 if (!drm_mode_equal(&crtc->state->mode, &crtc_state->mode)) {
469 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] mode changed\n",
470 crtc->base.id, crtc->name);
471 crtc_state->mode_changed = true;
474 if (crtc->state->enable != crtc_state->enable) {
475 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enable changed\n",
476 crtc->base.id, crtc->name);
479 * For clarity this assignment is done here, but
480 * enable == 0 is only true when there are no
481 * connectors and a NULL mode.
483 * The other way around is true as well. enable != 0
484 * iff connectors are attached and a mode is set.
486 crtc_state->mode_changed = true;
487 crtc_state->connectors_changed = true;
491 for_each_connector_in_state(state, connector, connector_state, i) {
493 * This only sets crtc->mode_changed for routing changes,
494 * drivers must set crtc->mode_changed themselves when connector
495 * properties need to be updated.
497 ret = update_connector_routing(state, i);
503 * After all the routing has been prepared we need to add in any
504 * connector which is itself unchanged, but who's crtc changes it's
505 * configuration. This must be done before calling mode_fixup in case a
506 * crtc only changed its mode but has the same set of connectors.
508 for_each_crtc_in_state(state, crtc, crtc_state, i) {
509 bool has_connectors =
510 !!crtc_state->connector_mask;
513 * We must set ->active_changed after walking connectors for
514 * otherwise an update that only changes active would result in
515 * a full modeset because update_connector_routing force that.
517 if (crtc->state->active != crtc_state->active) {
518 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active changed\n",
519 crtc->base.id, crtc->name);
520 crtc_state->active_changed = true;
523 if (!drm_atomic_crtc_needs_modeset(crtc_state))
526 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] needs all connectors, enable: %c, active: %c\n",
527 crtc->base.id, crtc->name,
528 crtc_state->enable ? 'y' : 'n',
529 crtc_state->active ? 'y' : 'n');
531 ret = drm_atomic_add_affected_connectors(state, crtc);
535 ret = drm_atomic_add_affected_planes(state, crtc);
539 if (crtc_state->enable != has_connectors) {
540 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled/connectors mismatch\n",
541 crtc->base.id, crtc->name);
547 return mode_fixup(state);
549 EXPORT_SYMBOL(drm_atomic_helper_check_modeset);
552 * drm_atomic_helper_check_planes - validate state object for planes changes
554 * @state: the driver state object
556 * Check the state object to see if the requested state is physically possible.
557 * This does all the plane update related checks using by calling into the
558 * ->atomic_check hooks provided by the driver.
560 * It also sets crtc_state->planes_changed to indicate that a crtc has
564 * Zero for success or -errno
567 drm_atomic_helper_check_planes(struct drm_device *dev,
568 struct drm_atomic_state *state)
570 struct drm_crtc *crtc;
571 struct drm_crtc_state *crtc_state;
572 struct drm_plane *plane;
573 struct drm_plane_state *plane_state;
576 for_each_plane_in_state(state, plane, plane_state, i) {
577 const struct drm_plane_helper_funcs *funcs;
579 funcs = plane->helper_private;
581 drm_atomic_helper_plane_changed(state, plane_state, plane);
583 if (!funcs || !funcs->atomic_check)
586 ret = funcs->atomic_check(plane, plane_state);
588 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic driver check failed\n",
589 plane->base.id, plane->name);
594 for_each_crtc_in_state(state, crtc, crtc_state, i) {
595 const struct drm_crtc_helper_funcs *funcs;
597 funcs = crtc->helper_private;
599 if (!funcs || !funcs->atomic_check)
602 ret = funcs->atomic_check(crtc, state->crtc_states[i]);
604 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic driver check failed\n",
605 crtc->base.id, crtc->name);
612 EXPORT_SYMBOL(drm_atomic_helper_check_planes);
615 * drm_atomic_helper_check - validate state object
617 * @state: the driver state object
619 * Check the state object to see if the requested state is physically possible.
620 * Only crtcs and planes have check callbacks, so for any additional (global)
621 * checking that a driver needs it can simply wrap that around this function.
622 * Drivers without such needs can directly use this as their ->atomic_check()
625 * This just wraps the two parts of the state checking for planes and modeset
626 * state in the default order: First it calls drm_atomic_helper_check_modeset()
627 * and then drm_atomic_helper_check_planes(). The assumption is that the
628 * ->atomic_check functions depend upon an updated adjusted_mode.clock to
629 * e.g. properly compute watermarks.
632 * Zero for success or -errno
634 int drm_atomic_helper_check(struct drm_device *dev,
635 struct drm_atomic_state *state)
639 ret = drm_atomic_helper_check_modeset(dev, state);
643 ret = drm_atomic_helper_check_planes(dev, state);
649 EXPORT_SYMBOL(drm_atomic_helper_check);
652 disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state)
654 struct drm_connector *connector;
655 struct drm_connector_state *old_conn_state;
656 struct drm_crtc *crtc;
657 struct drm_crtc_state *old_crtc_state;
660 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
661 const struct drm_encoder_helper_funcs *funcs;
662 struct drm_encoder *encoder;
664 /* Shut down everything that's in the changeset and currently
665 * still on. So need to check the old, saved state. */
666 if (!old_conn_state->crtc)
669 old_crtc_state = old_state->crtc_states[drm_crtc_index(old_conn_state->crtc)];
671 if (!old_crtc_state->active ||
672 !drm_atomic_crtc_needs_modeset(old_conn_state->crtc->state))
675 encoder = old_conn_state->best_encoder;
677 /* We shouldn't get this far if we didn't previously have
678 * an encoder.. but WARN_ON() rather than explode.
680 if (WARN_ON(!encoder))
683 funcs = encoder->helper_private;
685 DRM_DEBUG_ATOMIC("disabling [ENCODER:%d:%s]\n",
686 encoder->base.id, encoder->name);
689 * Each encoder has at most one connector (since we always steal
690 * it away), so we won't call disable hooks twice.
692 drm_bridge_disable(encoder->bridge);
694 /* Right function depends upon target state. */
695 if (connector->state->crtc && funcs->prepare)
696 funcs->prepare(encoder);
697 else if (funcs->disable)
698 funcs->disable(encoder);
700 funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
702 drm_bridge_post_disable(encoder->bridge);
705 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
706 const struct drm_crtc_helper_funcs *funcs;
708 /* Shut down everything that needs a full modeset. */
709 if (!drm_atomic_crtc_needs_modeset(crtc->state))
712 if (!old_crtc_state->active)
715 funcs = crtc->helper_private;
717 DRM_DEBUG_ATOMIC("disabling [CRTC:%d:%s]\n",
718 crtc->base.id, crtc->name);
721 /* Right function depends upon target state. */
722 if (crtc->state->enable && funcs->prepare)
723 funcs->prepare(crtc);
724 else if (funcs->disable)
725 funcs->disable(crtc);
727 funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
732 * drm_atomic_helper_update_legacy_modeset_state - update legacy modeset state
734 * @old_state: atomic state object with old state structures
736 * This function updates all the various legacy modeset state pointers in
737 * connectors, encoders and crtcs. It also updates the timestamping constants
738 * used for precise vblank timestamps by calling
739 * drm_calc_timestamping_constants().
741 * Drivers can use this for building their own atomic commit if they don't have
742 * a pure helper-based modeset implementation.
745 drm_atomic_helper_update_legacy_modeset_state(struct drm_device *dev,
746 struct drm_atomic_state *old_state)
748 struct drm_connector *connector;
749 struct drm_connector_state *old_conn_state;
750 struct drm_crtc *crtc;
751 struct drm_crtc_state *old_crtc_state;
754 /* clear out existing links and update dpms */
755 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
756 if (connector->encoder) {
757 WARN_ON(!connector->encoder->crtc);
759 connector->encoder->crtc = NULL;
760 connector->encoder = NULL;
763 crtc = connector->state->crtc;
764 if ((!crtc && old_conn_state->crtc) ||
765 (crtc && drm_atomic_crtc_needs_modeset(crtc->state))) {
766 struct drm_property *dpms_prop =
767 dev->mode_config.dpms_property;
768 int mode = DRM_MODE_DPMS_OFF;
770 if (crtc && crtc->state->active)
771 mode = DRM_MODE_DPMS_ON;
773 connector->dpms = mode;
774 drm_object_property_set_value(&connector->base,
780 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
781 if (!connector->state->crtc)
784 if (WARN_ON(!connector->state->best_encoder))
787 connector->encoder = connector->state->best_encoder;
788 connector->encoder->crtc = connector->state->crtc;
791 /* set legacy state in the crtc structure */
792 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
793 struct drm_plane *primary = crtc->primary;
795 crtc->mode = crtc->state->mode;
796 crtc->enabled = crtc->state->enable;
798 if (drm_atomic_get_existing_plane_state(old_state, primary) &&
799 primary->state->crtc == crtc) {
800 crtc->x = primary->state->src_x >> 16;
801 crtc->y = primary->state->src_y >> 16;
804 if (crtc->state->enable)
805 drm_calc_timestamping_constants(crtc,
806 &crtc->state->adjusted_mode);
809 EXPORT_SYMBOL(drm_atomic_helper_update_legacy_modeset_state);
812 crtc_set_mode(struct drm_device *dev, struct drm_atomic_state *old_state)
814 struct drm_crtc *crtc;
815 struct drm_crtc_state *old_crtc_state;
816 struct drm_connector *connector;
817 struct drm_connector_state *old_conn_state;
820 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
821 const struct drm_crtc_helper_funcs *funcs;
823 if (!crtc->state->mode_changed)
826 funcs = crtc->helper_private;
828 if (crtc->state->enable && funcs->mode_set_nofb) {
829 DRM_DEBUG_ATOMIC("modeset on [CRTC:%d:%s]\n",
830 crtc->base.id, crtc->name);
832 funcs->mode_set_nofb(crtc);
836 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
837 const struct drm_encoder_helper_funcs *funcs;
838 struct drm_crtc_state *new_crtc_state;
839 struct drm_encoder *encoder;
840 struct drm_display_mode *mode, *adjusted_mode;
842 if (!connector->state->best_encoder)
845 encoder = connector->state->best_encoder;
846 funcs = encoder->helper_private;
847 new_crtc_state = connector->state->crtc->state;
848 mode = &new_crtc_state->mode;
849 adjusted_mode = &new_crtc_state->adjusted_mode;
851 if (!new_crtc_state->mode_changed)
854 DRM_DEBUG_ATOMIC("modeset on [ENCODER:%d:%s]\n",
855 encoder->base.id, encoder->name);
858 * Each encoder has at most one connector (since we always steal
859 * it away), so we won't call mode_set hooks twice.
862 funcs->mode_set(encoder, mode, adjusted_mode);
864 drm_bridge_mode_set(encoder->bridge, mode, adjusted_mode);
869 * drm_atomic_helper_commit_modeset_disables - modeset commit to disable outputs
871 * @old_state: atomic state object with old state structures
873 * This function shuts down all the outputs that need to be shut down and
874 * prepares them (if required) with the new mode.
876 * For compatibility with legacy crtc helpers this should be called before
877 * drm_atomic_helper_commit_planes(), which is what the default commit function
878 * does. But drivers with different needs can group the modeset commits together
879 * and do the plane commits at the end. This is useful for drivers doing runtime
880 * PM since planes updates then only happen when the CRTC is actually enabled.
882 void drm_atomic_helper_commit_modeset_disables(struct drm_device *dev,
883 struct drm_atomic_state *old_state)
885 disable_outputs(dev, old_state);
887 drm_atomic_helper_update_legacy_modeset_state(dev, old_state);
889 crtc_set_mode(dev, old_state);
891 EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_disables);
894 * drm_atomic_helper_commit_modeset_enables - modeset commit to enable outputs
896 * @old_state: atomic state object with old state structures
898 * This function enables all the outputs with the new configuration which had to
899 * be turned off for the update.
901 * For compatibility with legacy crtc helpers this should be called after
902 * drm_atomic_helper_commit_planes(), which is what the default commit function
903 * does. But drivers with different needs can group the modeset commits together
904 * and do the plane commits at the end. This is useful for drivers doing runtime
905 * PM since planes updates then only happen when the CRTC is actually enabled.
907 void drm_atomic_helper_commit_modeset_enables(struct drm_device *dev,
908 struct drm_atomic_state *old_state)
910 struct drm_crtc *crtc;
911 struct drm_crtc_state *old_crtc_state;
912 struct drm_connector *connector;
913 struct drm_connector_state *old_conn_state;
916 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
917 const struct drm_crtc_helper_funcs *funcs;
919 /* Need to filter out CRTCs where only planes change. */
920 if (!drm_atomic_crtc_needs_modeset(crtc->state))
923 if (!crtc->state->active)
926 funcs = crtc->helper_private;
928 if (crtc->state->enable) {
929 DRM_DEBUG_ATOMIC("enabling [CRTC:%d:%s]\n",
930 crtc->base.id, crtc->name);
939 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
940 const struct drm_encoder_helper_funcs *funcs;
941 struct drm_encoder *encoder;
943 if (!connector->state->best_encoder)
946 if (!connector->state->crtc->state->active ||
947 !drm_atomic_crtc_needs_modeset(connector->state->crtc->state))
950 encoder = connector->state->best_encoder;
951 funcs = encoder->helper_private;
953 DRM_DEBUG_ATOMIC("enabling [ENCODER:%d:%s]\n",
954 encoder->base.id, encoder->name);
957 * Each encoder has at most one connector (since we always steal
958 * it away), so we won't call enable hooks twice.
960 drm_bridge_pre_enable(encoder->bridge);
963 funcs->enable(encoder);
965 funcs->commit(encoder);
967 drm_bridge_enable(encoder->bridge);
970 EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_enables);
972 static void wait_for_fences(struct drm_device *dev,
973 struct drm_atomic_state *state)
975 struct drm_plane *plane;
976 struct drm_plane_state *plane_state;
979 for_each_plane_in_state(state, plane, plane_state, i) {
980 if (!plane->state->fence)
983 WARN_ON(!plane->state->fb);
985 fence_wait(plane->state->fence, false);
986 fence_put(plane->state->fence);
987 plane->state->fence = NULL;
992 * drm_atomic_helper_framebuffer_changed - check if framebuffer has changed
994 * @old_state: atomic state object with old state structures
997 * Checks whether the framebuffer used for this CRTC changes as a result of
998 * the atomic update. This is useful for drivers which cannot use
999 * drm_atomic_helper_wait_for_vblanks() and need to reimplement its
1003 * true if the framebuffer changed.
1005 bool drm_atomic_helper_framebuffer_changed(struct drm_device *dev,
1006 struct drm_atomic_state *old_state,
1007 struct drm_crtc *crtc)
1009 struct drm_plane *plane;
1010 struct drm_plane_state *old_plane_state;
1013 for_each_plane_in_state(old_state, plane, old_plane_state, i) {
1014 if (plane->state->crtc != crtc &&
1015 old_plane_state->crtc != crtc)
1018 if (plane->state->fb != old_plane_state->fb)
1024 EXPORT_SYMBOL(drm_atomic_helper_framebuffer_changed);
1027 * drm_atomic_helper_wait_for_vblanks - wait for vblank on crtcs
1029 * @old_state: atomic state object with old state structures
1031 * Helper to, after atomic commit, wait for vblanks on all effected
1032 * crtcs (ie. before cleaning up old framebuffers using
1033 * drm_atomic_helper_cleanup_planes()). It will only wait on crtcs where the
1034 * framebuffers have actually changed to optimize for the legacy cursor and
1035 * plane update use-case.
1038 drm_atomic_helper_wait_for_vblanks(struct drm_device *dev,
1039 struct drm_atomic_state *old_state)
1041 struct drm_crtc *crtc;
1042 struct drm_crtc_state *old_crtc_state;
1045 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
1046 /* No one cares about the old state, so abuse it for tracking
1047 * and store whether we hold a vblank reference (and should do a
1048 * vblank wait) in the ->enable boolean. */
1049 old_crtc_state->enable = false;
1051 if (!crtc->state->enable)
1054 /* Legacy cursor ioctls are completely unsynced, and userspace
1055 * relies on that (by doing tons of cursor updates). */
1056 if (old_state->legacy_cursor_update)
1059 if (!drm_atomic_helper_framebuffer_changed(dev,
1063 ret = drm_crtc_vblank_get(crtc);
1067 old_crtc_state->enable = true;
1068 old_crtc_state->last_vblank_count = drm_crtc_vblank_count(crtc);
1071 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
1072 if (!old_crtc_state->enable)
1075 ret = wait_event_timeout(dev->vblank[i].queue,
1076 old_crtc_state->last_vblank_count !=
1077 drm_crtc_vblank_count(crtc),
1078 msecs_to_jiffies(50));
1080 drm_crtc_vblank_put(crtc);
1083 EXPORT_SYMBOL(drm_atomic_helper_wait_for_vblanks);
1086 * drm_atomic_helper_commit - commit validated state object
1088 * @state: the driver state object
1089 * @async: asynchronous commit
1091 * This function commits a with drm_atomic_helper_check() pre-validated state
1092 * object. This can still fail when e.g. the framebuffer reservation fails. For
1093 * now this doesn't implement asynchronous commits.
1095 * Note that right now this function does not support async commits, and hence
1096 * driver writers must implement their own version for now. Also note that the
1097 * default ordering of how the various stages are called is to match the legacy
1098 * modeset helper library closest. One peculiarity of that is that it doesn't
1099 * mesh well with runtime PM at all.
1101 * For drivers supporting runtime PM the recommended sequence is
1103 * drm_atomic_helper_commit_modeset_disables(dev, state);
1105 * drm_atomic_helper_commit_modeset_enables(dev, state);
1107 * drm_atomic_helper_commit_planes(dev, state, true);
1109 * See the kerneldoc entries for these three functions for more details.
1112 * Zero for success or -errno.
1114 int drm_atomic_helper_commit(struct drm_device *dev,
1115 struct drm_atomic_state *state,
1123 ret = drm_atomic_helper_prepare_planes(dev, state);
1128 * This is the point of no return - everything below never fails except
1129 * when the hw goes bonghits. Which means we can commit the new state on
1130 * the software side now.
1133 drm_atomic_helper_swap_state(dev, state);
1136 * Everything below can be run asynchronously without the need to grab
1137 * any modeset locks at all under one condition: It must be guaranteed
1138 * that the asynchronous work has either been cancelled (if the driver
1139 * supports it, which at least requires that the framebuffers get
1140 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
1141 * before the new state gets committed on the software side with
1142 * drm_atomic_helper_swap_state().
1144 * This scheme allows new atomic state updates to be prepared and
1145 * checked in parallel to the asynchronous completion of the previous
1146 * update. Which is important since compositors need to figure out the
1147 * composition of the next frame right after having submitted the
1151 wait_for_fences(dev, state);
1153 drm_atomic_helper_commit_modeset_disables(dev, state);
1155 drm_atomic_helper_commit_planes(dev, state, false);
1157 drm_atomic_helper_commit_modeset_enables(dev, state);
1159 drm_atomic_helper_wait_for_vblanks(dev, state);
1161 drm_atomic_helper_cleanup_planes(dev, state);
1163 drm_atomic_state_free(state);
1167 EXPORT_SYMBOL(drm_atomic_helper_commit);
1170 * DOC: implementing async commit
1172 * For now the atomic helpers don't support async commit directly. If there is
1173 * real need it could be added though, using the dma-buf fence infrastructure
1174 * for generic synchronization with outstanding rendering.
1176 * For now drivers have to implement async commit themselves, with the following
1177 * sequence being the recommended one:
1179 * 1. Run drm_atomic_helper_prepare_planes() first. This is the only function
1180 * which commit needs to call which can fail, so we want to run it first and
1183 * 2. Synchronize with any outstanding asynchronous commit worker threads which
1184 * might be affected the new state update. This can be done by either cancelling
1185 * or flushing the work items, depending upon whether the driver can deal with
1186 * cancelled updates. Note that it is important to ensure that the framebuffer
1187 * cleanup is still done when cancelling.
1189 * For sufficient parallelism it is recommended to have a work item per crtc
1190 * (for updates which don't touch global state) and a global one. Then we only
1191 * need to synchronize with the crtc work items for changed crtcs and the global
1192 * work item, which allows nice concurrent updates on disjoint sets of crtcs.
1194 * 3. The software state is updated synchronously with
1195 * drm_atomic_helper_swap_state(). Doing this under the protection of all modeset
1196 * locks means concurrent callers never see inconsistent state. And doing this
1197 * while it's guaranteed that no relevant async worker runs means that async
1198 * workers do not need grab any locks. Actually they must not grab locks, for
1199 * otherwise the work flushing will deadlock.
1201 * 4. Schedule a work item to do all subsequent steps, using the split-out
1202 * commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and
1203 * then cleaning up the framebuffers after the old framebuffer is no longer
1208 * drm_atomic_helper_prepare_planes - prepare plane resources before commit
1210 * @state: atomic state object with new state structures
1212 * This function prepares plane state, specifically framebuffers, for the new
1213 * configuration. If any failure is encountered this function will call
1214 * ->cleanup_fb on any already successfully prepared framebuffer.
1217 * 0 on success, negative error code on failure.
1219 int drm_atomic_helper_prepare_planes(struct drm_device *dev,
1220 struct drm_atomic_state *state)
1222 int nplanes = dev->mode_config.num_total_plane;
1225 for (i = 0; i < nplanes; i++) {
1226 const struct drm_plane_helper_funcs *funcs;
1227 struct drm_plane *plane = state->planes[i];
1228 struct drm_plane_state *plane_state = state->plane_states[i];
1233 funcs = plane->helper_private;
1235 if (funcs->prepare_fb) {
1236 ret = funcs->prepare_fb(plane, plane_state);
1245 for (i--; i >= 0; i--) {
1246 const struct drm_plane_helper_funcs *funcs;
1247 struct drm_plane *plane = state->planes[i];
1248 struct drm_plane_state *plane_state = state->plane_states[i];
1253 funcs = plane->helper_private;
1255 if (funcs->cleanup_fb)
1256 funcs->cleanup_fb(plane, plane_state);
1262 EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
1264 bool plane_crtc_active(struct drm_plane_state *state)
1266 return state->crtc && state->crtc->state->active;
1270 * drm_atomic_helper_commit_planes - commit plane state
1272 * @old_state: atomic state object with old state structures
1273 * @active_only: Only commit on active CRTC if set
1275 * This function commits the new plane state using the plane and atomic helper
1276 * functions for planes and crtcs. It assumes that the atomic state has already
1277 * been pushed into the relevant object state pointers, since this step can no
1280 * It still requires the global state object @old_state to know which planes and
1281 * crtcs need to be updated though.
1283 * Note that this function does all plane updates across all CRTCs in one step.
1284 * If the hardware can't support this approach look at
1285 * drm_atomic_helper_commit_planes_on_crtc() instead.
1287 * Plane parameters can be updated by applications while the associated CRTC is
1288 * disabled. The DRM/KMS core will store the parameters in the plane state,
1289 * which will be available to the driver when the CRTC is turned on. As a result
1290 * most drivers don't need to be immediately notified of plane updates for a
1293 * Unless otherwise needed, drivers are advised to set the @active_only
1294 * parameters to true in order not to receive plane update notifications related
1295 * to a disabled CRTC. This avoids the need to manually ignore plane updates in
1296 * driver code when the driver and/or hardware can't or just don't need to deal
1297 * with updates on disabled CRTCs, for example when supporting runtime PM.
1299 * The drm_atomic_helper_commit() default implementation only sets @active_only
1300 * to false to most closely match the behaviour of the legacy helpers. This should
1301 * not be copied blindly by drivers.
1303 void drm_atomic_helper_commit_planes(struct drm_device *dev,
1304 struct drm_atomic_state *old_state,
1307 struct drm_crtc *crtc;
1308 struct drm_crtc_state *old_crtc_state;
1309 struct drm_plane *plane;
1310 struct drm_plane_state *old_plane_state;
1313 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
1314 const struct drm_crtc_helper_funcs *funcs;
1316 funcs = crtc->helper_private;
1318 if (!funcs || !funcs->atomic_begin)
1321 if (active_only && !crtc->state->active)
1324 funcs->atomic_begin(crtc, old_crtc_state);
1327 for_each_plane_in_state(old_state, plane, old_plane_state, i) {
1328 const struct drm_plane_helper_funcs *funcs;
1331 funcs = plane->helper_private;
1336 disabling = drm_atomic_plane_disabling(plane, old_plane_state);
1340 * Skip planes related to inactive CRTCs. If the plane
1341 * is enabled use the state of the current CRTC. If the
1342 * plane is being disabled use the state of the old
1343 * CRTC to avoid skipping planes being disabled on an
1346 if (!disabling && !plane_crtc_active(plane->state))
1348 if (disabling && !plane_crtc_active(old_plane_state))
1353 * Special-case disabling the plane if drivers support it.
1355 if (disabling && funcs->atomic_disable)
1356 funcs->atomic_disable(plane, old_plane_state);
1357 else if (plane->state->crtc || disabling)
1358 funcs->atomic_update(plane, old_plane_state);
1361 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
1362 const struct drm_crtc_helper_funcs *funcs;
1364 funcs = crtc->helper_private;
1366 if (!funcs || !funcs->atomic_flush)
1369 if (active_only && !crtc->state->active)
1372 funcs->atomic_flush(crtc, old_crtc_state);
1375 EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
1378 * drm_atomic_helper_commit_planes_on_crtc - commit plane state for a crtc
1379 * @old_crtc_state: atomic state object with the old crtc state
1381 * This function commits the new plane state using the plane and atomic helper
1382 * functions for planes on the specific crtc. It assumes that the atomic state
1383 * has already been pushed into the relevant object state pointers, since this
1384 * step can no longer fail.
1386 * This function is useful when plane updates should be done crtc-by-crtc
1387 * instead of one global step like drm_atomic_helper_commit_planes() does.
1389 * This function can only be savely used when planes are not allowed to move
1390 * between different CRTCs because this function doesn't handle inter-CRTC
1391 * depencies. Callers need to ensure that either no such depencies exist,
1392 * resolve them through ordering of commit calls or through some other means.
1395 drm_atomic_helper_commit_planes_on_crtc(struct drm_crtc_state *old_crtc_state)
1397 const struct drm_crtc_helper_funcs *crtc_funcs;
1398 struct drm_crtc *crtc = old_crtc_state->crtc;
1399 struct drm_atomic_state *old_state = old_crtc_state->state;
1400 struct drm_plane *plane;
1401 unsigned plane_mask;
1403 plane_mask = old_crtc_state->plane_mask;
1404 plane_mask |= crtc->state->plane_mask;
1406 crtc_funcs = crtc->helper_private;
1407 if (crtc_funcs && crtc_funcs->atomic_begin)
1408 crtc_funcs->atomic_begin(crtc, old_crtc_state);
1410 drm_for_each_plane_mask(plane, crtc->dev, plane_mask) {
1411 struct drm_plane_state *old_plane_state =
1412 drm_atomic_get_existing_plane_state(old_state, plane);
1413 const struct drm_plane_helper_funcs *plane_funcs;
1415 plane_funcs = plane->helper_private;
1417 if (!old_plane_state || !plane_funcs)
1420 WARN_ON(plane->state->crtc && plane->state->crtc != crtc);
1422 if (drm_atomic_plane_disabling(plane, old_plane_state) &&
1423 plane_funcs->atomic_disable)
1424 plane_funcs->atomic_disable(plane, old_plane_state);
1425 else if (plane->state->crtc ||
1426 drm_atomic_plane_disabling(plane, old_plane_state))
1427 plane_funcs->atomic_update(plane, old_plane_state);
1430 if (crtc_funcs && crtc_funcs->atomic_flush)
1431 crtc_funcs->atomic_flush(crtc, old_crtc_state);
1433 EXPORT_SYMBOL(drm_atomic_helper_commit_planes_on_crtc);
1436 * drm_atomic_helper_disable_planes_on_crtc - helper to disable CRTC's planes
1438 * @atomic: if set, synchronize with CRTC's atomic_begin/flush hooks
1440 * Disables all planes associated with the given CRTC. This can be
1441 * used for instance in the CRTC helper disable callback to disable
1442 * all planes before shutting down the display pipeline.
1444 * If the atomic-parameter is set the function calls the CRTC's
1445 * atomic_begin hook before and atomic_flush hook after disabling the
1448 * It is a bug to call this function without having implemented the
1449 * ->atomic_disable() plane hook.
1451 void drm_atomic_helper_disable_planes_on_crtc(struct drm_crtc *crtc,
1454 const struct drm_crtc_helper_funcs *crtc_funcs =
1455 crtc->helper_private;
1456 struct drm_plane *plane;
1458 if (atomic && crtc_funcs && crtc_funcs->atomic_begin)
1459 crtc_funcs->atomic_begin(crtc, NULL);
1461 drm_for_each_plane(plane, crtc->dev) {
1462 const struct drm_plane_helper_funcs *plane_funcs =
1463 plane->helper_private;
1465 if (plane->state->crtc != crtc || !plane_funcs)
1468 WARN_ON(!plane_funcs->atomic_disable);
1469 if (plane_funcs->atomic_disable)
1470 plane_funcs->atomic_disable(plane, NULL);
1473 if (atomic && crtc_funcs && crtc_funcs->atomic_flush)
1474 crtc_funcs->atomic_flush(crtc, NULL);
1476 EXPORT_SYMBOL(drm_atomic_helper_disable_planes_on_crtc);
1479 * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
1481 * @old_state: atomic state object with old state structures
1483 * This function cleans up plane state, specifically framebuffers, from the old
1484 * configuration. Hence the old configuration must be perserved in @old_state to
1485 * be able to call this function.
1487 * This function must also be called on the new state when the atomic update
1488 * fails at any point after calling drm_atomic_helper_prepare_planes().
1490 void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
1491 struct drm_atomic_state *old_state)
1493 struct drm_plane *plane;
1494 struct drm_plane_state *plane_state;
1497 for_each_plane_in_state(old_state, plane, plane_state, i) {
1498 const struct drm_plane_helper_funcs *funcs;
1500 funcs = plane->helper_private;
1502 if (funcs->cleanup_fb)
1503 funcs->cleanup_fb(plane, plane_state);
1506 EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
1509 * drm_atomic_helper_swap_state - store atomic state into current sw state
1511 * @state: atomic state
1513 * This function stores the atomic state into the current state pointers in all
1514 * driver objects. It should be called after all failing steps have been done
1515 * and succeeded, but before the actual hardware state is committed.
1517 * For cleanup and error recovery the current state for all changed objects will
1518 * be swaped into @state.
1520 * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
1522 * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
1524 * 2. Do any other steps that might fail.
1526 * 3. Put the staged state into the current state pointers with this function.
1528 * 4. Actually commit the hardware state.
1530 * 5. Call drm_atomic_helper_cleanup_planes() with @state, which since step 3
1531 * contains the old state. Also do any other cleanup required with that state.
1533 void drm_atomic_helper_swap_state(struct drm_device *dev,
1534 struct drm_atomic_state *state)
1538 for (i = 0; i < state->num_connector; i++) {
1539 struct drm_connector *connector = state->connectors[i];
1544 connector->state->state = state;
1545 swap(state->connector_states[i], connector->state);
1546 connector->state->state = NULL;
1549 for (i = 0; i < dev->mode_config.num_crtc; i++) {
1550 struct drm_crtc *crtc = state->crtcs[i];
1555 crtc->state->state = state;
1556 swap(state->crtc_states[i], crtc->state);
1557 crtc->state->state = NULL;
1560 for (i = 0; i < dev->mode_config.num_total_plane; i++) {
1561 struct drm_plane *plane = state->planes[i];
1566 plane->state->state = state;
1567 swap(state->plane_states[i], plane->state);
1568 plane->state->state = NULL;
1571 EXPORT_SYMBOL(drm_atomic_helper_swap_state);
1574 * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
1575 * @plane: plane object to update
1576 * @crtc: owning CRTC of owning plane
1577 * @fb: framebuffer to flip onto plane
1578 * @crtc_x: x offset of primary plane on crtc
1579 * @crtc_y: y offset of primary plane on crtc
1580 * @crtc_w: width of primary plane rectangle on crtc
1581 * @crtc_h: height of primary plane rectangle on crtc
1582 * @src_x: x offset of @fb for panning
1583 * @src_y: y offset of @fb for panning
1584 * @src_w: width of source rectangle in @fb
1585 * @src_h: height of source rectangle in @fb
1587 * Provides a default plane update handler using the atomic driver interface.
1590 * Zero on success, error code on failure
1592 int drm_atomic_helper_update_plane(struct drm_plane *plane,
1593 struct drm_crtc *crtc,
1594 struct drm_framebuffer *fb,
1595 int crtc_x, int crtc_y,
1596 unsigned int crtc_w, unsigned int crtc_h,
1597 uint32_t src_x, uint32_t src_y,
1598 uint32_t src_w, uint32_t src_h)
1600 struct drm_atomic_state *state;
1601 struct drm_plane_state *plane_state;
1604 state = drm_atomic_state_alloc(plane->dev);
1608 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1610 plane_state = drm_atomic_get_plane_state(state, plane);
1611 if (IS_ERR(plane_state)) {
1612 ret = PTR_ERR(plane_state);
1616 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
1619 drm_atomic_set_fb_for_plane(plane_state, fb);
1620 plane_state->crtc_x = crtc_x;
1621 plane_state->crtc_y = crtc_y;
1622 plane_state->crtc_w = crtc_w;
1623 plane_state->crtc_h = crtc_h;
1624 plane_state->src_x = src_x;
1625 plane_state->src_y = src_y;
1626 plane_state->src_w = src_w;
1627 plane_state->src_h = src_h;
1629 if (plane == crtc->cursor)
1630 state->legacy_cursor_update = true;
1632 ret = drm_atomic_commit(state);
1636 /* Driver takes ownership of state on successful commit. */
1639 if (ret == -EDEADLK)
1642 drm_atomic_state_free(state);
1646 drm_atomic_state_clear(state);
1647 drm_atomic_legacy_backoff(state);
1650 * Someone might have exchanged the framebuffer while we dropped locks
1651 * in the backoff code. We need to fix up the fb refcount tracking the
1654 plane->old_fb = plane->fb;
1658 EXPORT_SYMBOL(drm_atomic_helper_update_plane);
1661 * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
1662 * @plane: plane to disable
1664 * Provides a default plane disable handler using the atomic driver interface.
1667 * Zero on success, error code on failure
1669 int drm_atomic_helper_disable_plane(struct drm_plane *plane)
1671 struct drm_atomic_state *state;
1672 struct drm_plane_state *plane_state;
1676 * FIXME: Without plane->crtc set we can't get at the implicit legacy
1677 * acquire context. The real fix will be to wire the acquire ctx through
1678 * everywhere we need it, but meanwhile prevent chaos by just skipping
1679 * this noop. The critical case is the cursor ioctls which a) only grab
1680 * crtc/cursor-plane locks (so we need the crtc to get at the right
1681 * acquire context) and b) can try to disable the plane multiple times.
1686 state = drm_atomic_state_alloc(plane->dev);
1690 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(plane->crtc);
1692 plane_state = drm_atomic_get_plane_state(state, plane);
1693 if (IS_ERR(plane_state)) {
1694 ret = PTR_ERR(plane_state);
1698 if (plane_state->crtc && (plane == plane->crtc->cursor))
1699 plane_state->state->legacy_cursor_update = true;
1701 ret = __drm_atomic_helper_disable_plane(plane, plane_state);
1705 ret = drm_atomic_commit(state);
1709 /* Driver takes ownership of state on successful commit. */
1712 if (ret == -EDEADLK)
1715 drm_atomic_state_free(state);
1719 drm_atomic_state_clear(state);
1720 drm_atomic_legacy_backoff(state);
1723 * Someone might have exchanged the framebuffer while we dropped locks
1724 * in the backoff code. We need to fix up the fb refcount tracking the
1727 plane->old_fb = plane->fb;
1731 EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
1733 /* just used from fb-helper and atomic-helper: */
1734 int __drm_atomic_helper_disable_plane(struct drm_plane *plane,
1735 struct drm_plane_state *plane_state)
1739 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
1743 drm_atomic_set_fb_for_plane(plane_state, NULL);
1744 plane_state->crtc_x = 0;
1745 plane_state->crtc_y = 0;
1746 plane_state->crtc_w = 0;
1747 plane_state->crtc_h = 0;
1748 plane_state->src_x = 0;
1749 plane_state->src_y = 0;
1750 plane_state->src_w = 0;
1751 plane_state->src_h = 0;
1756 static int update_output_state(struct drm_atomic_state *state,
1757 struct drm_mode_set *set)
1759 struct drm_device *dev = set->crtc->dev;
1760 struct drm_crtc *crtc;
1761 struct drm_crtc_state *crtc_state;
1762 struct drm_connector *connector;
1763 struct drm_connector_state *conn_state;
1766 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
1767 state->acquire_ctx);
1771 /* First grab all affected connector/crtc states. */
1772 for (i = 0; i < set->num_connectors; i++) {
1773 conn_state = drm_atomic_get_connector_state(state,
1774 set->connectors[i]);
1775 if (IS_ERR(conn_state))
1776 return PTR_ERR(conn_state);
1779 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1780 ret = drm_atomic_add_affected_connectors(state, crtc);
1785 /* Then recompute connector->crtc links and crtc enabling state. */
1786 for_each_connector_in_state(state, connector, conn_state, i) {
1787 if (conn_state->crtc == set->crtc) {
1788 ret = drm_atomic_set_crtc_for_connector(conn_state,
1794 for (j = 0; j < set->num_connectors; j++) {
1795 if (set->connectors[j] == connector) {
1796 ret = drm_atomic_set_crtc_for_connector(conn_state,
1805 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1806 /* Don't update ->enable for the CRTC in the set_config request,
1807 * since a mismatch would indicate a bug in the upper layers.
1808 * The actual modeset code later on will catch any
1809 * inconsistencies here. */
1810 if (crtc == set->crtc)
1813 if (!crtc_state->connector_mask) {
1814 ret = drm_atomic_set_mode_prop_for_crtc(crtc_state,
1819 crtc_state->active = false;
1827 * drm_atomic_helper_set_config - set a new config from userspace
1828 * @set: mode set configuration
1830 * Provides a default crtc set_config handler using the atomic driver interface.
1833 * Returns 0 on success, negative errno numbers on failure.
1835 int drm_atomic_helper_set_config(struct drm_mode_set *set)
1837 struct drm_atomic_state *state;
1838 struct drm_crtc *crtc = set->crtc;
1841 state = drm_atomic_state_alloc(crtc->dev);
1845 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1847 ret = __drm_atomic_helper_set_config(set, state);
1851 ret = drm_atomic_commit(state);
1855 /* Driver takes ownership of state on successful commit. */
1858 if (ret == -EDEADLK)
1861 drm_atomic_state_free(state);
1865 drm_atomic_state_clear(state);
1866 drm_atomic_legacy_backoff(state);
1869 * Someone might have exchanged the framebuffer while we dropped locks
1870 * in the backoff code. We need to fix up the fb refcount tracking the
1873 crtc->primary->old_fb = crtc->primary->fb;
1877 EXPORT_SYMBOL(drm_atomic_helper_set_config);
1879 /* just used from fb-helper and atomic-helper: */
1880 int __drm_atomic_helper_set_config(struct drm_mode_set *set,
1881 struct drm_atomic_state *state)
1883 struct drm_crtc_state *crtc_state;
1884 struct drm_plane_state *primary_state;
1885 struct drm_crtc *crtc = set->crtc;
1886 int hdisplay, vdisplay;
1889 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1890 if (IS_ERR(crtc_state))
1891 return PTR_ERR(crtc_state);
1893 primary_state = drm_atomic_get_plane_state(state, crtc->primary);
1894 if (IS_ERR(primary_state))
1895 return PTR_ERR(primary_state);
1899 WARN_ON(set->num_connectors);
1901 ret = drm_atomic_set_mode_for_crtc(crtc_state, NULL);
1905 crtc_state->active = false;
1907 ret = drm_atomic_set_crtc_for_plane(primary_state, NULL);
1911 drm_atomic_set_fb_for_plane(primary_state, NULL);
1917 WARN_ON(!set->num_connectors);
1919 ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
1923 crtc_state->active = true;
1925 ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
1929 drm_crtc_get_hv_timing(set->mode, &hdisplay, &vdisplay);
1931 drm_atomic_set_fb_for_plane(primary_state, set->fb);
1932 primary_state->crtc_x = 0;
1933 primary_state->crtc_y = 0;
1934 primary_state->crtc_w = hdisplay;
1935 primary_state->crtc_h = vdisplay;
1936 primary_state->src_x = set->x << 16;
1937 primary_state->src_y = set->y << 16;
1938 if (primary_state->rotation & (BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270))) {
1939 primary_state->src_w = vdisplay << 16;
1940 primary_state->src_h = hdisplay << 16;
1942 primary_state->src_w = hdisplay << 16;
1943 primary_state->src_h = vdisplay << 16;
1947 ret = update_output_state(state, set);
1955 * drm_atomic_helper_disable_all - disable all currently active outputs
1957 * @ctx: lock acquisition context
1959 * Loops through all connectors, finding those that aren't turned off and then
1960 * turns them off by setting their DPMS mode to OFF and deactivating the CRTC
1961 * that they are connected to.
1963 * This is used for example in suspend/resume to disable all currently active
1964 * functions when suspending.
1966 * Note that if callers haven't already acquired all modeset locks this might
1967 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
1970 * 0 on success or a negative error code on failure.
1973 * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
1975 int drm_atomic_helper_disable_all(struct drm_device *dev,
1976 struct drm_modeset_acquire_ctx *ctx)
1978 struct drm_atomic_state *state;
1979 struct drm_connector *conn;
1982 state = drm_atomic_state_alloc(dev);
1986 state->acquire_ctx = ctx;
1988 drm_for_each_connector(conn, dev) {
1989 struct drm_crtc *crtc = conn->state->crtc;
1990 struct drm_crtc_state *crtc_state;
1992 if (!crtc || conn->dpms != DRM_MODE_DPMS_ON)
1995 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1996 if (IS_ERR(crtc_state)) {
1997 err = PTR_ERR(crtc_state);
2001 crtc_state->active = false;
2004 err = drm_atomic_commit(state);
2008 drm_atomic_state_free(state);
2012 EXPORT_SYMBOL(drm_atomic_helper_disable_all);
2015 * drm_atomic_helper_suspend - subsystem-level suspend helper
2018 * Duplicates the current atomic state, disables all active outputs and then
2019 * returns a pointer to the original atomic state to the caller. Drivers can
2020 * pass this pointer to the drm_atomic_helper_resume() helper upon resume to
2021 * restore the output configuration that was active at the time the system
2024 * Note that it is potentially unsafe to use this. The atomic state object
2025 * returned by this function is assumed to be persistent. Drivers must ensure
2026 * that this holds true. Before calling this function, drivers must make sure
2027 * to suspend fbdev emulation so that nothing can be using the device.
2030 * A pointer to a copy of the state before suspend on success or an ERR_PTR()-
2031 * encoded error code on failure. Drivers should store the returned atomic
2032 * state object and pass it to the drm_atomic_helper_resume() helper upon
2036 * drm_atomic_helper_duplicate_state(), drm_atomic_helper_disable_all(),
2037 * drm_atomic_helper_resume()
2039 struct drm_atomic_state *drm_atomic_helper_suspend(struct drm_device *dev)
2041 struct drm_modeset_acquire_ctx ctx;
2042 struct drm_atomic_state *state;
2045 drm_modeset_acquire_init(&ctx, 0);
2048 err = drm_modeset_lock_all_ctx(dev, &ctx);
2050 state = ERR_PTR(err);
2054 state = drm_atomic_helper_duplicate_state(dev, &ctx);
2058 err = drm_atomic_helper_disable_all(dev, &ctx);
2060 drm_atomic_state_free(state);
2061 state = ERR_PTR(err);
2066 if (PTR_ERR(state) == -EDEADLK) {
2067 drm_modeset_backoff(&ctx);
2071 drm_modeset_drop_locks(&ctx);
2072 drm_modeset_acquire_fini(&ctx);
2075 EXPORT_SYMBOL(drm_atomic_helper_suspend);
2078 * drm_atomic_helper_resume - subsystem-level resume helper
2080 * @state: atomic state to resume to
2082 * Calls drm_mode_config_reset() to synchronize hardware and software states,
2083 * grabs all modeset locks and commits the atomic state object. This can be
2084 * used in conjunction with the drm_atomic_helper_suspend() helper to
2085 * implement suspend/resume for drivers that support atomic mode-setting.
2088 * 0 on success or a negative error code on failure.
2091 * drm_atomic_helper_suspend()
2093 int drm_atomic_helper_resume(struct drm_device *dev,
2094 struct drm_atomic_state *state)
2096 struct drm_mode_config *config = &dev->mode_config;
2099 drm_mode_config_reset(dev);
2100 drm_modeset_lock_all(dev);
2101 state->acquire_ctx = config->acquire_ctx;
2102 err = drm_atomic_commit(state);
2103 drm_modeset_unlock_all(dev);
2107 EXPORT_SYMBOL(drm_atomic_helper_resume);
2110 * drm_atomic_helper_crtc_set_property - helper for crtc properties
2112 * @property: DRM property
2113 * @val: value of property
2115 * Provides a default crtc set_property handler using the atomic driver
2119 * Zero on success, error code on failure
2122 drm_atomic_helper_crtc_set_property(struct drm_crtc *crtc,
2123 struct drm_property *property,
2126 struct drm_atomic_state *state;
2127 struct drm_crtc_state *crtc_state;
2130 state = drm_atomic_state_alloc(crtc->dev);
2134 /* ->set_property is always called with all locks held. */
2135 state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
2137 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2138 if (IS_ERR(crtc_state)) {
2139 ret = PTR_ERR(crtc_state);
2143 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
2148 ret = drm_atomic_commit(state);
2152 /* Driver takes ownership of state on successful commit. */
2155 if (ret == -EDEADLK)
2158 drm_atomic_state_free(state);
2162 drm_atomic_state_clear(state);
2163 drm_atomic_legacy_backoff(state);
2167 EXPORT_SYMBOL(drm_atomic_helper_crtc_set_property);
2170 * drm_atomic_helper_plane_set_property - helper for plane properties
2172 * @property: DRM property
2173 * @val: value of property
2175 * Provides a default plane set_property handler using the atomic driver
2179 * Zero on success, error code on failure
2182 drm_atomic_helper_plane_set_property(struct drm_plane *plane,
2183 struct drm_property *property,
2186 struct drm_atomic_state *state;
2187 struct drm_plane_state *plane_state;
2190 state = drm_atomic_state_alloc(plane->dev);
2194 /* ->set_property is always called with all locks held. */
2195 state->acquire_ctx = plane->dev->mode_config.acquire_ctx;
2197 plane_state = drm_atomic_get_plane_state(state, plane);
2198 if (IS_ERR(plane_state)) {
2199 ret = PTR_ERR(plane_state);
2203 ret = drm_atomic_plane_set_property(plane, plane_state,
2208 ret = drm_atomic_commit(state);
2212 /* Driver takes ownership of state on successful commit. */
2215 if (ret == -EDEADLK)
2218 drm_atomic_state_free(state);
2222 drm_atomic_state_clear(state);
2223 drm_atomic_legacy_backoff(state);
2227 EXPORT_SYMBOL(drm_atomic_helper_plane_set_property);
2230 * drm_atomic_helper_connector_set_property - helper for connector properties
2231 * @connector: DRM connector
2232 * @property: DRM property
2233 * @val: value of property
2235 * Provides a default connector set_property handler using the atomic driver
2239 * Zero on success, error code on failure
2242 drm_atomic_helper_connector_set_property(struct drm_connector *connector,
2243 struct drm_property *property,
2246 struct drm_atomic_state *state;
2247 struct drm_connector_state *connector_state;
2250 state = drm_atomic_state_alloc(connector->dev);
2254 /* ->set_property is always called with all locks held. */
2255 state->acquire_ctx = connector->dev->mode_config.acquire_ctx;
2257 connector_state = drm_atomic_get_connector_state(state, connector);
2258 if (IS_ERR(connector_state)) {
2259 ret = PTR_ERR(connector_state);
2263 ret = drm_atomic_connector_set_property(connector, connector_state,
2268 ret = drm_atomic_commit(state);
2272 /* Driver takes ownership of state on successful commit. */
2275 if (ret == -EDEADLK)
2278 drm_atomic_state_free(state);
2282 drm_atomic_state_clear(state);
2283 drm_atomic_legacy_backoff(state);
2287 EXPORT_SYMBOL(drm_atomic_helper_connector_set_property);
2290 * drm_atomic_helper_page_flip - execute a legacy page flip
2292 * @fb: DRM framebuffer
2293 * @event: optional DRM event to signal upon completion
2294 * @flags: flip flags for non-vblank sync'ed updates
2296 * Provides a default page flip implementation using the atomic driver interface.
2298 * Note that for now so called async page flips (i.e. updates which are not
2299 * synchronized to vblank) are not supported, since the atomic interfaces have
2300 * no provisions for this yet.
2303 * Returns 0 on success, negative errno numbers on failure.
2305 int drm_atomic_helper_page_flip(struct drm_crtc *crtc,
2306 struct drm_framebuffer *fb,
2307 struct drm_pending_vblank_event *event,
2310 struct drm_plane *plane = crtc->primary;
2311 struct drm_atomic_state *state;
2312 struct drm_plane_state *plane_state;
2313 struct drm_crtc_state *crtc_state;
2316 if (flags & DRM_MODE_PAGE_FLIP_ASYNC)
2319 state = drm_atomic_state_alloc(plane->dev);
2323 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2325 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2326 if (IS_ERR(crtc_state)) {
2327 ret = PTR_ERR(crtc_state);
2330 crtc_state->event = event;
2332 plane_state = drm_atomic_get_plane_state(state, plane);
2333 if (IS_ERR(plane_state)) {
2334 ret = PTR_ERR(plane_state);
2338 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
2341 drm_atomic_set_fb_for_plane(plane_state, fb);
2343 /* Make sure we don't accidentally do a full modeset. */
2344 state->allow_modeset = false;
2345 if (!crtc_state->active) {
2346 DRM_DEBUG_ATOMIC("[CRTC:%d] disabled, rejecting legacy flip\n",
2352 ret = drm_atomic_async_commit(state);
2356 /* Driver takes ownership of state on successful async commit. */
2359 if (ret == -EDEADLK)
2362 drm_atomic_state_free(state);
2366 drm_atomic_state_clear(state);
2367 drm_atomic_legacy_backoff(state);
2370 * Someone might have exchanged the framebuffer while we dropped locks
2371 * in the backoff code. We need to fix up the fb refcount tracking the
2374 plane->old_fb = plane->fb;
2378 EXPORT_SYMBOL(drm_atomic_helper_page_flip);
2381 * drm_atomic_helper_connector_dpms() - connector dpms helper implementation
2382 * @connector: affected connector
2385 * This is the main helper function provided by the atomic helper framework for
2386 * implementing the legacy DPMS connector interface. It computes the new desired
2387 * ->active state for the corresponding CRTC (if the connector is enabled) and
2391 * Returns 0 on success, negative errno numbers on failure.
2393 int drm_atomic_helper_connector_dpms(struct drm_connector *connector,
2396 struct drm_mode_config *config = &connector->dev->mode_config;
2397 struct drm_atomic_state *state;
2398 struct drm_crtc_state *crtc_state;
2399 struct drm_crtc *crtc;
2400 struct drm_connector *tmp_connector;
2402 bool active = false;
2403 int old_mode = connector->dpms;
2405 if (mode != DRM_MODE_DPMS_ON)
2406 mode = DRM_MODE_DPMS_OFF;
2408 connector->dpms = mode;
2409 crtc = connector->state->crtc;
2414 state = drm_atomic_state_alloc(connector->dev);
2418 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2420 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2421 if (IS_ERR(crtc_state)) {
2422 ret = PTR_ERR(crtc_state);
2426 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
2428 drm_for_each_connector(tmp_connector, connector->dev) {
2429 if (tmp_connector->state->crtc != crtc)
2432 if (tmp_connector->dpms == DRM_MODE_DPMS_ON) {
2437 crtc_state->active = active;
2439 ret = drm_atomic_commit(state);
2443 /* Driver takes ownership of state on successful commit. */
2446 if (ret == -EDEADLK)
2449 connector->dpms = old_mode;
2450 drm_atomic_state_free(state);
2454 drm_atomic_state_clear(state);
2455 drm_atomic_legacy_backoff(state);
2459 EXPORT_SYMBOL(drm_atomic_helper_connector_dpms);
2462 * DOC: atomic state reset and initialization
2464 * Both the drm core and the atomic helpers assume that there is always the full
2465 * and correct atomic software state for all connectors, CRTCs and planes
2466 * available. Which is a bit a problem on driver load and also after system
2467 * suspend. One way to solve this is to have a hardware state read-out
2468 * infrastructure which reconstructs the full software state (e.g. the i915
2471 * The simpler solution is to just reset the software state to everything off,
2472 * which is easiest to do by calling drm_mode_config_reset(). To facilitate this
2473 * the atomic helpers provide default reset implementations for all hooks.
2475 * On the upside the precise state tracking of atomic simplifies system suspend
2476 * and resume a lot. For drivers using drm_mode_config_reset() a complete recipe
2477 * is implemented in drm_atomic_helper_suspend() and drm_atomic_helper_resume().
2478 * For other drivers the building blocks are split out, see the documentation
2479 * for these functions.
2483 * drm_atomic_helper_crtc_reset - default ->reset hook for CRTCs
2486 * Resets the atomic state for @crtc by freeing the state pointer (which might
2487 * be NULL, e.g. at driver load time) and allocating a new empty state object.
2489 void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc)
2492 drm_property_unreference_blob(crtc->state->mode_blob);
2494 crtc->state = kzalloc(sizeof(*crtc->state), GFP_KERNEL);
2497 crtc->state->crtc = crtc;
2499 EXPORT_SYMBOL(drm_atomic_helper_crtc_reset);
2502 * __drm_atomic_helper_crtc_duplicate_state - copy atomic CRTC state
2503 * @crtc: CRTC object
2504 * @state: atomic CRTC state
2506 * Copies atomic state from a CRTC's current state and resets inferred values.
2507 * This is useful for drivers that subclass the CRTC state.
2509 void __drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc,
2510 struct drm_crtc_state *state)
2512 memcpy(state, crtc->state, sizeof(*state));
2514 if (state->mode_blob)
2515 drm_property_reference_blob(state->mode_blob);
2516 state->mode_changed = false;
2517 state->active_changed = false;
2518 state->planes_changed = false;
2519 state->connectors_changed = false;
2520 state->event = NULL;
2522 EXPORT_SYMBOL(__drm_atomic_helper_crtc_duplicate_state);
2525 * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
2528 * Default CRTC state duplicate hook for drivers which don't have their own
2529 * subclassed CRTC state structure.
2531 struct drm_crtc_state *
2532 drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc)
2534 struct drm_crtc_state *state;
2536 if (WARN_ON(!crtc->state))
2539 state = kmalloc(sizeof(*state), GFP_KERNEL);
2541 __drm_atomic_helper_crtc_duplicate_state(crtc, state);
2545 EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state);
2548 * __drm_atomic_helper_crtc_destroy_state - release CRTC state
2549 * @crtc: CRTC object
2550 * @state: CRTC state object to release
2552 * Releases all resources stored in the CRTC state without actually freeing
2553 * the memory of the CRTC state. This is useful for drivers that subclass the
2556 void __drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
2557 struct drm_crtc_state *state)
2559 drm_property_unreference_blob(state->mode_blob);
2561 EXPORT_SYMBOL(__drm_atomic_helper_crtc_destroy_state);
2564 * drm_atomic_helper_crtc_destroy_state - default state destroy hook
2566 * @state: CRTC state object to release
2568 * Default CRTC state destroy hook for drivers which don't have their own
2569 * subclassed CRTC state structure.
2571 void drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
2572 struct drm_crtc_state *state)
2574 __drm_atomic_helper_crtc_destroy_state(crtc, state);
2577 EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state);
2580 * drm_atomic_helper_plane_reset - default ->reset hook for planes
2583 * Resets the atomic state for @plane by freeing the state pointer (which might
2584 * be NULL, e.g. at driver load time) and allocating a new empty state object.
2586 void drm_atomic_helper_plane_reset(struct drm_plane *plane)
2588 if (plane->state && plane->state->fb)
2589 drm_framebuffer_unreference(plane->state->fb);
2591 kfree(plane->state);
2592 plane->state = kzalloc(sizeof(*plane->state), GFP_KERNEL);
2595 plane->state->plane = plane;
2596 plane->state->rotation = BIT(DRM_ROTATE_0);
2599 EXPORT_SYMBOL(drm_atomic_helper_plane_reset);
2602 * __drm_atomic_helper_plane_duplicate_state - copy atomic plane state
2603 * @plane: plane object
2604 * @state: atomic plane state
2606 * Copies atomic state from a plane's current state. This is useful for
2607 * drivers that subclass the plane state.
2609 void __drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane,
2610 struct drm_plane_state *state)
2612 memcpy(state, plane->state, sizeof(*state));
2615 drm_framebuffer_reference(state->fb);
2617 EXPORT_SYMBOL(__drm_atomic_helper_plane_duplicate_state);
2620 * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
2623 * Default plane state duplicate hook for drivers which don't have their own
2624 * subclassed plane state structure.
2626 struct drm_plane_state *
2627 drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane)
2629 struct drm_plane_state *state;
2631 if (WARN_ON(!plane->state))
2634 state = kmalloc(sizeof(*state), GFP_KERNEL);
2636 __drm_atomic_helper_plane_duplicate_state(plane, state);
2640 EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state);
2643 * __drm_atomic_helper_plane_destroy_state - release plane state
2644 * @plane: plane object
2645 * @state: plane state object to release
2647 * Releases all resources stored in the plane state without actually freeing
2648 * the memory of the plane state. This is useful for drivers that subclass the
2651 void __drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
2652 struct drm_plane_state *state)
2655 drm_framebuffer_unreference(state->fb);
2657 EXPORT_SYMBOL(__drm_atomic_helper_plane_destroy_state);
2660 * drm_atomic_helper_plane_destroy_state - default state destroy hook
2662 * @state: plane state object to release
2664 * Default plane state destroy hook for drivers which don't have their own
2665 * subclassed plane state structure.
2667 void drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
2668 struct drm_plane_state *state)
2670 __drm_atomic_helper_plane_destroy_state(plane, state);
2673 EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state);
2676 * __drm_atomic_helper_connector_reset - reset state on connector
2677 * @connector: drm connector
2678 * @conn_state: connector state to assign
2680 * Initializes the newly allocated @conn_state and assigns it to
2681 * #connector ->state, usually required when initializing the drivers
2682 * or when called from the ->reset hook.
2684 * This is useful for drivers that subclass the connector state.
2687 __drm_atomic_helper_connector_reset(struct drm_connector *connector,
2688 struct drm_connector_state *conn_state)
2691 conn_state->connector = connector;
2693 connector->state = conn_state;
2695 EXPORT_SYMBOL(__drm_atomic_helper_connector_reset);
2698 * drm_atomic_helper_connector_reset - default ->reset hook for connectors
2699 * @connector: drm connector
2701 * Resets the atomic state for @connector by freeing the state pointer (which
2702 * might be NULL, e.g. at driver load time) and allocating a new empty state
2705 void drm_atomic_helper_connector_reset(struct drm_connector *connector)
2707 struct drm_connector_state *conn_state =
2708 kzalloc(sizeof(*conn_state), GFP_KERNEL);
2710 kfree(connector->state);
2711 __drm_atomic_helper_connector_reset(connector, conn_state);
2713 EXPORT_SYMBOL(drm_atomic_helper_connector_reset);
2716 * __drm_atomic_helper_connector_duplicate_state - copy atomic connector state
2717 * @connector: connector object
2718 * @state: atomic connector state
2720 * Copies atomic state from a connector's current state. This is useful for
2721 * drivers that subclass the connector state.
2724 __drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector,
2725 struct drm_connector_state *state)
2727 memcpy(state, connector->state, sizeof(*state));
2729 EXPORT_SYMBOL(__drm_atomic_helper_connector_duplicate_state);
2732 * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
2733 * @connector: drm connector
2735 * Default connector state duplicate hook for drivers which don't have their own
2736 * subclassed connector state structure.
2738 struct drm_connector_state *
2739 drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
2741 struct drm_connector_state *state;
2743 if (WARN_ON(!connector->state))
2746 state = kmalloc(sizeof(*state), GFP_KERNEL);
2748 __drm_atomic_helper_connector_duplicate_state(connector, state);
2752 EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
2755 * drm_atomic_helper_duplicate_state - duplicate an atomic state object
2757 * @ctx: lock acquisition context
2759 * Makes a copy of the current atomic state by looping over all objects and
2760 * duplicating their respective states. This is used for example by suspend/
2761 * resume support code to save the state prior to suspend such that it can
2762 * be restored upon resume.
2764 * Note that this treats atomic state as persistent between save and restore.
2765 * Drivers must make sure that this is possible and won't result in confusion
2766 * or erroneous behaviour.
2768 * Note that if callers haven't already acquired all modeset locks this might
2769 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
2772 * A pointer to the copy of the atomic state object on success or an
2773 * ERR_PTR()-encoded error code on failure.
2776 * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
2778 struct drm_atomic_state *
2779 drm_atomic_helper_duplicate_state(struct drm_device *dev,
2780 struct drm_modeset_acquire_ctx *ctx)
2782 struct drm_atomic_state *state;
2783 struct drm_connector *conn;
2784 struct drm_plane *plane;
2785 struct drm_crtc *crtc;
2788 state = drm_atomic_state_alloc(dev);
2790 return ERR_PTR(-ENOMEM);
2792 state->acquire_ctx = ctx;
2794 drm_for_each_crtc(crtc, dev) {
2795 struct drm_crtc_state *crtc_state;
2797 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2798 if (IS_ERR(crtc_state)) {
2799 err = PTR_ERR(crtc_state);
2804 drm_for_each_plane(plane, dev) {
2805 struct drm_plane_state *plane_state;
2807 plane_state = drm_atomic_get_plane_state(state, plane);
2808 if (IS_ERR(plane_state)) {
2809 err = PTR_ERR(plane_state);
2814 drm_for_each_connector(conn, dev) {
2815 struct drm_connector_state *conn_state;
2817 conn_state = drm_atomic_get_connector_state(state, conn);
2818 if (IS_ERR(conn_state)) {
2819 err = PTR_ERR(conn_state);
2824 /* clear the acquire context so that it isn't accidentally reused */
2825 state->acquire_ctx = NULL;
2829 drm_atomic_state_free(state);
2830 state = ERR_PTR(err);
2835 EXPORT_SYMBOL(drm_atomic_helper_duplicate_state);
2838 * __drm_atomic_helper_connector_destroy_state - release connector state
2839 * @connector: connector object
2840 * @state: connector state object to release
2842 * Releases all resources stored in the connector state without actually
2843 * freeing the memory of the connector state. This is useful for drivers that
2844 * subclass the connector state.
2847 __drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
2848 struct drm_connector_state *state)
2851 * This is currently a placeholder so that drivers that subclass the
2852 * state will automatically do the right thing if code is ever added
2856 EXPORT_SYMBOL(__drm_atomic_helper_connector_destroy_state);
2859 * drm_atomic_helper_connector_destroy_state - default state destroy hook
2860 * @connector: drm connector
2861 * @state: connector state object to release
2863 * Default connector state destroy hook for drivers which don't have their own
2864 * subclassed connector state structure.
2866 void drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
2867 struct drm_connector_state *state)
2869 __drm_atomic_helper_connector_destroy_state(connector, state);
2872 EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state);