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;
912 static bool framebuffer_changed(struct drm_device *dev,
913 struct drm_atomic_state *old_state,
914 struct drm_crtc *crtc)
916 struct drm_plane *plane;
917 struct drm_plane_state *old_plane_state;
920 for_each_plane_in_state(old_state, plane, old_plane_state, i) {
921 if (plane->state->crtc != crtc &&
922 old_plane_state->crtc != crtc)
925 if (plane->state->fb != old_plane_state->fb)
933 * drm_atomic_helper_wait_for_vblanks - wait for vblank on crtcs
935 * @old_state: atomic state object with old state structures
937 * Helper to, after atomic commit, wait for vblanks on all effected
938 * crtcs (ie. before cleaning up old framebuffers using
939 * drm_atomic_helper_cleanup_planes()). It will only wait on crtcs where the
940 * framebuffers have actually changed to optimize for the legacy cursor and
941 * plane update use-case.
944 drm_atomic_helper_wait_for_vblanks(struct drm_device *dev,
945 struct drm_atomic_state *old_state)
947 struct drm_crtc *crtc;
948 struct drm_crtc_state *old_crtc_state;
951 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
952 /* No one cares about the old state, so abuse it for tracking
953 * and store whether we hold a vblank reference (and should do a
954 * vblank wait) in the ->enable boolean. */
955 old_crtc_state->enable = false;
957 if (!crtc->state->enable)
960 /* Legacy cursor ioctls are completely unsynced, and userspace
961 * relies on that (by doing tons of cursor updates). */
962 if (old_state->legacy_cursor_update)
965 if (!framebuffer_changed(dev, old_state, crtc))
968 ret = drm_crtc_vblank_get(crtc);
972 old_crtc_state->enable = true;
973 old_crtc_state->last_vblank_count = drm_crtc_vblank_count(crtc);
976 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
977 if (!old_crtc_state->enable)
980 ret = wait_event_timeout(dev->vblank[i].queue,
981 old_crtc_state->last_vblank_count !=
982 drm_crtc_vblank_count(crtc),
983 msecs_to_jiffies(50));
985 drm_crtc_vblank_put(crtc);
988 EXPORT_SYMBOL(drm_atomic_helper_wait_for_vblanks);
991 * drm_atomic_helper_commit - commit validated state object
993 * @state: the driver state object
994 * @async: asynchronous commit
996 * This function commits a with drm_atomic_helper_check() pre-validated state
997 * object. This can still fail when e.g. the framebuffer reservation fails. For
998 * now this doesn't implement asynchronous commits.
1000 * Note that right now this function does not support async commits, and hence
1001 * driver writers must implement their own version for now. Also note that the
1002 * default ordering of how the various stages are called is to match the legacy
1003 * modeset helper library closest. One peculiarity of that is that it doesn't
1004 * mesh well with runtime PM at all.
1006 * For drivers supporting runtime PM the recommended sequence is
1008 * drm_atomic_helper_commit_modeset_disables(dev, state);
1010 * drm_atomic_helper_commit_modeset_enables(dev, state);
1012 * drm_atomic_helper_commit_planes(dev, state, true);
1014 * See the kerneldoc entries for these three functions for more details.
1017 * Zero for success or -errno.
1019 int drm_atomic_helper_commit(struct drm_device *dev,
1020 struct drm_atomic_state *state,
1028 ret = drm_atomic_helper_prepare_planes(dev, state);
1033 * This is the point of no return - everything below never fails except
1034 * when the hw goes bonghits. Which means we can commit the new state on
1035 * the software side now.
1038 drm_atomic_helper_swap_state(dev, state);
1041 * Everything below can be run asynchronously without the need to grab
1042 * any modeset locks at all under one condition: It must be guaranteed
1043 * that the asynchronous work has either been cancelled (if the driver
1044 * supports it, which at least requires that the framebuffers get
1045 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
1046 * before the new state gets committed on the software side with
1047 * drm_atomic_helper_swap_state().
1049 * This scheme allows new atomic state updates to be prepared and
1050 * checked in parallel to the asynchronous completion of the previous
1051 * update. Which is important since compositors need to figure out the
1052 * composition of the next frame right after having submitted the
1056 wait_for_fences(dev, state);
1058 drm_atomic_helper_commit_modeset_disables(dev, state);
1060 drm_atomic_helper_commit_planes(dev, state, false);
1062 drm_atomic_helper_commit_modeset_enables(dev, state);
1064 drm_atomic_helper_wait_for_vblanks(dev, state);
1066 drm_atomic_helper_cleanup_planes(dev, state);
1068 drm_atomic_state_free(state);
1072 EXPORT_SYMBOL(drm_atomic_helper_commit);
1075 * DOC: implementing async commit
1077 * For now the atomic helpers don't support async commit directly. If there is
1078 * real need it could be added though, using the dma-buf fence infrastructure
1079 * for generic synchronization with outstanding rendering.
1081 * For now drivers have to implement async commit themselves, with the following
1082 * sequence being the recommended one:
1084 * 1. Run drm_atomic_helper_prepare_planes() first. This is the only function
1085 * which commit needs to call which can fail, so we want to run it first and
1088 * 2. Synchronize with any outstanding asynchronous commit worker threads which
1089 * might be affected the new state update. This can be done by either cancelling
1090 * or flushing the work items, depending upon whether the driver can deal with
1091 * cancelled updates. Note that it is important to ensure that the framebuffer
1092 * cleanup is still done when cancelling.
1094 * For sufficient parallelism it is recommended to have a work item per crtc
1095 * (for updates which don't touch global state) and a global one. Then we only
1096 * need to synchronize with the crtc work items for changed crtcs and the global
1097 * work item, which allows nice concurrent updates on disjoint sets of crtcs.
1099 * 3. The software state is updated synchronously with
1100 * drm_atomic_helper_swap_state(). Doing this under the protection of all modeset
1101 * locks means concurrent callers never see inconsistent state. And doing this
1102 * while it's guaranteed that no relevant async worker runs means that async
1103 * workers do not need grab any locks. Actually they must not grab locks, for
1104 * otherwise the work flushing will deadlock.
1106 * 4. Schedule a work item to do all subsequent steps, using the split-out
1107 * commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and
1108 * then cleaning up the framebuffers after the old framebuffer is no longer
1113 * drm_atomic_helper_prepare_planes - prepare plane resources before commit
1115 * @state: atomic state object with new state structures
1117 * This function prepares plane state, specifically framebuffers, for the new
1118 * configuration. If any failure is encountered this function will call
1119 * ->cleanup_fb on any already successfully prepared framebuffer.
1122 * 0 on success, negative error code on failure.
1124 int drm_atomic_helper_prepare_planes(struct drm_device *dev,
1125 struct drm_atomic_state *state)
1127 int nplanes = dev->mode_config.num_total_plane;
1130 for (i = 0; i < nplanes; i++) {
1131 const struct drm_plane_helper_funcs *funcs;
1132 struct drm_plane *plane = state->planes[i];
1133 struct drm_plane_state *plane_state = state->plane_states[i];
1138 funcs = plane->helper_private;
1140 if (funcs->prepare_fb) {
1141 ret = funcs->prepare_fb(plane, plane_state);
1150 for (i--; i >= 0; i--) {
1151 const struct drm_plane_helper_funcs *funcs;
1152 struct drm_plane *plane = state->planes[i];
1153 struct drm_plane_state *plane_state = state->plane_states[i];
1158 funcs = plane->helper_private;
1160 if (funcs->cleanup_fb)
1161 funcs->cleanup_fb(plane, plane_state);
1167 EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
1169 bool plane_crtc_active(struct drm_plane_state *state)
1171 return state->crtc && state->crtc->state->active;
1175 * drm_atomic_helper_commit_planes - commit plane state
1177 * @old_state: atomic state object with old state structures
1178 * @active_only: Only commit on active CRTC if set
1180 * This function commits the new plane state using the plane and atomic helper
1181 * functions for planes and crtcs. It assumes that the atomic state has already
1182 * been pushed into the relevant object state pointers, since this step can no
1185 * It still requires the global state object @old_state to know which planes and
1186 * crtcs need to be updated though.
1188 * Note that this function does all plane updates across all CRTCs in one step.
1189 * If the hardware can't support this approach look at
1190 * drm_atomic_helper_commit_planes_on_crtc() instead.
1192 * Plane parameters can be updated by applications while the associated CRTC is
1193 * disabled. The DRM/KMS core will store the parameters in the plane state,
1194 * which will be available to the driver when the CRTC is turned on. As a result
1195 * most drivers don't need to be immediately notified of plane updates for a
1198 * Unless otherwise needed, drivers are advised to set the @active_only
1199 * parameters to true in order not to receive plane update notifications related
1200 * to a disabled CRTC. This avoids the need to manually ignore plane updates in
1201 * driver code when the driver and/or hardware can't or just don't need to deal
1202 * with updates on disabled CRTCs, for example when supporting runtime PM.
1204 * The drm_atomic_helper_commit() default implementation only sets @active_only
1205 * to false to most closely match the behaviour of the legacy helpers. This should
1206 * not be copied blindly by drivers.
1208 void drm_atomic_helper_commit_planes(struct drm_device *dev,
1209 struct drm_atomic_state *old_state,
1212 struct drm_crtc *crtc;
1213 struct drm_crtc_state *old_crtc_state;
1214 struct drm_plane *plane;
1215 struct drm_plane_state *old_plane_state;
1218 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
1219 const struct drm_crtc_helper_funcs *funcs;
1221 funcs = crtc->helper_private;
1223 if (!funcs || !funcs->atomic_begin)
1226 if (active_only && !crtc->state->active)
1229 funcs->atomic_begin(crtc, old_crtc_state);
1232 for_each_plane_in_state(old_state, plane, old_plane_state, i) {
1233 const struct drm_plane_helper_funcs *funcs;
1236 funcs = plane->helper_private;
1241 disabling = drm_atomic_plane_disabling(plane, old_plane_state);
1245 * Skip planes related to inactive CRTCs. If the plane
1246 * is enabled use the state of the current CRTC. If the
1247 * plane is being disabled use the state of the old
1248 * CRTC to avoid skipping planes being disabled on an
1251 if (!disabling && !plane_crtc_active(plane->state))
1253 if (disabling && !plane_crtc_active(old_plane_state))
1258 * Special-case disabling the plane if drivers support it.
1260 if (disabling && funcs->atomic_disable)
1261 funcs->atomic_disable(plane, old_plane_state);
1262 else if (plane->state->crtc || disabling)
1263 funcs->atomic_update(plane, old_plane_state);
1266 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
1267 const struct drm_crtc_helper_funcs *funcs;
1269 funcs = crtc->helper_private;
1271 if (!funcs || !funcs->atomic_flush)
1274 if (active_only && !crtc->state->active)
1277 funcs->atomic_flush(crtc, old_crtc_state);
1280 EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
1283 * drm_atomic_helper_commit_planes_on_crtc - commit plane state for a crtc
1284 * @old_crtc_state: atomic state object with the old crtc state
1286 * This function commits the new plane state using the plane and atomic helper
1287 * functions for planes on the specific crtc. It assumes that the atomic state
1288 * has already been pushed into the relevant object state pointers, since this
1289 * step can no longer fail.
1291 * This function is useful when plane updates should be done crtc-by-crtc
1292 * instead of one global step like drm_atomic_helper_commit_planes() does.
1294 * This function can only be savely used when planes are not allowed to move
1295 * between different CRTCs because this function doesn't handle inter-CRTC
1296 * depencies. Callers need to ensure that either no such depencies exist,
1297 * resolve them through ordering of commit calls or through some other means.
1300 drm_atomic_helper_commit_planes_on_crtc(struct drm_crtc_state *old_crtc_state)
1302 const struct drm_crtc_helper_funcs *crtc_funcs;
1303 struct drm_crtc *crtc = old_crtc_state->crtc;
1304 struct drm_atomic_state *old_state = old_crtc_state->state;
1305 struct drm_plane *plane;
1306 unsigned plane_mask;
1308 plane_mask = old_crtc_state->plane_mask;
1309 plane_mask |= crtc->state->plane_mask;
1311 crtc_funcs = crtc->helper_private;
1312 if (crtc_funcs && crtc_funcs->atomic_begin)
1313 crtc_funcs->atomic_begin(crtc, old_crtc_state);
1315 drm_for_each_plane_mask(plane, crtc->dev, plane_mask) {
1316 struct drm_plane_state *old_plane_state =
1317 drm_atomic_get_existing_plane_state(old_state, plane);
1318 const struct drm_plane_helper_funcs *plane_funcs;
1320 plane_funcs = plane->helper_private;
1322 if (!old_plane_state || !plane_funcs)
1325 WARN_ON(plane->state->crtc && plane->state->crtc != crtc);
1327 if (drm_atomic_plane_disabling(plane, old_plane_state) &&
1328 plane_funcs->atomic_disable)
1329 plane_funcs->atomic_disable(plane, old_plane_state);
1330 else if (plane->state->crtc ||
1331 drm_atomic_plane_disabling(plane, old_plane_state))
1332 plane_funcs->atomic_update(plane, old_plane_state);
1335 if (crtc_funcs && crtc_funcs->atomic_flush)
1336 crtc_funcs->atomic_flush(crtc, old_crtc_state);
1338 EXPORT_SYMBOL(drm_atomic_helper_commit_planes_on_crtc);
1341 * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
1343 * @old_state: atomic state object with old state structures
1345 * This function cleans up plane state, specifically framebuffers, from the old
1346 * configuration. Hence the old configuration must be perserved in @old_state to
1347 * be able to call this function.
1349 * This function must also be called on the new state when the atomic update
1350 * fails at any point after calling drm_atomic_helper_prepare_planes().
1352 void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
1353 struct drm_atomic_state *old_state)
1355 struct drm_plane *plane;
1356 struct drm_plane_state *plane_state;
1359 for_each_plane_in_state(old_state, plane, plane_state, i) {
1360 const struct drm_plane_helper_funcs *funcs;
1362 funcs = plane->helper_private;
1364 if (funcs->cleanup_fb)
1365 funcs->cleanup_fb(plane, plane_state);
1368 EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
1371 * drm_atomic_helper_swap_state - store atomic state into current sw state
1373 * @state: atomic state
1375 * This function stores the atomic state into the current state pointers in all
1376 * driver objects. It should be called after all failing steps have been done
1377 * and succeeded, but before the actual hardware state is committed.
1379 * For cleanup and error recovery the current state for all changed objects will
1380 * be swaped into @state.
1382 * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
1384 * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
1386 * 2. Do any other steps that might fail.
1388 * 3. Put the staged state into the current state pointers with this function.
1390 * 4. Actually commit the hardware state.
1392 * 5. Call drm_atomic_helper_cleanup_planes() with @state, which since step 3
1393 * contains the old state. Also do any other cleanup required with that state.
1395 void drm_atomic_helper_swap_state(struct drm_device *dev,
1396 struct drm_atomic_state *state)
1400 for (i = 0; i < dev->mode_config.num_connector; i++) {
1401 struct drm_connector *connector = state->connectors[i];
1406 connector->state->state = state;
1407 swap(state->connector_states[i], connector->state);
1408 connector->state->state = NULL;
1411 for (i = 0; i < dev->mode_config.num_crtc; i++) {
1412 struct drm_crtc *crtc = state->crtcs[i];
1417 crtc->state->state = state;
1418 swap(state->crtc_states[i], crtc->state);
1419 crtc->state->state = NULL;
1422 for (i = 0; i < dev->mode_config.num_total_plane; i++) {
1423 struct drm_plane *plane = state->planes[i];
1428 plane->state->state = state;
1429 swap(state->plane_states[i], plane->state);
1430 plane->state->state = NULL;
1433 EXPORT_SYMBOL(drm_atomic_helper_swap_state);
1436 * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
1437 * @plane: plane object to update
1438 * @crtc: owning CRTC of owning plane
1439 * @fb: framebuffer to flip onto plane
1440 * @crtc_x: x offset of primary plane on crtc
1441 * @crtc_y: y offset of primary plane on crtc
1442 * @crtc_w: width of primary plane rectangle on crtc
1443 * @crtc_h: height of primary plane rectangle on crtc
1444 * @src_x: x offset of @fb for panning
1445 * @src_y: y offset of @fb for panning
1446 * @src_w: width of source rectangle in @fb
1447 * @src_h: height of source rectangle in @fb
1449 * Provides a default plane update handler using the atomic driver interface.
1452 * Zero on success, error code on failure
1454 int drm_atomic_helper_update_plane(struct drm_plane *plane,
1455 struct drm_crtc *crtc,
1456 struct drm_framebuffer *fb,
1457 int crtc_x, int crtc_y,
1458 unsigned int crtc_w, unsigned int crtc_h,
1459 uint32_t src_x, uint32_t src_y,
1460 uint32_t src_w, uint32_t src_h)
1462 struct drm_atomic_state *state;
1463 struct drm_plane_state *plane_state;
1466 state = drm_atomic_state_alloc(plane->dev);
1470 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1472 plane_state = drm_atomic_get_plane_state(state, plane);
1473 if (IS_ERR(plane_state)) {
1474 ret = PTR_ERR(plane_state);
1478 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
1481 drm_atomic_set_fb_for_plane(plane_state, fb);
1482 plane_state->crtc_x = crtc_x;
1483 plane_state->crtc_y = crtc_y;
1484 plane_state->crtc_h = crtc_h;
1485 plane_state->crtc_w = crtc_w;
1486 plane_state->src_x = src_x;
1487 plane_state->src_y = src_y;
1488 plane_state->src_h = src_h;
1489 plane_state->src_w = src_w;
1491 if (plane == crtc->cursor)
1492 state->legacy_cursor_update = true;
1494 ret = drm_atomic_commit(state);
1498 /* Driver takes ownership of state on successful commit. */
1501 if (ret == -EDEADLK)
1504 drm_atomic_state_free(state);
1508 drm_atomic_state_clear(state);
1509 drm_atomic_legacy_backoff(state);
1512 * Someone might have exchanged the framebuffer while we dropped locks
1513 * in the backoff code. We need to fix up the fb refcount tracking the
1516 plane->old_fb = plane->fb;
1520 EXPORT_SYMBOL(drm_atomic_helper_update_plane);
1523 * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
1524 * @plane: plane to disable
1526 * Provides a default plane disable handler using the atomic driver interface.
1529 * Zero on success, error code on failure
1531 int drm_atomic_helper_disable_plane(struct drm_plane *plane)
1533 struct drm_atomic_state *state;
1534 struct drm_plane_state *plane_state;
1538 * FIXME: Without plane->crtc set we can't get at the implicit legacy
1539 * acquire context. The real fix will be to wire the acquire ctx through
1540 * everywhere we need it, but meanwhile prevent chaos by just skipping
1541 * this noop. The critical case is the cursor ioctls which a) only grab
1542 * crtc/cursor-plane locks (so we need the crtc to get at the right
1543 * acquire context) and b) can try to disable the plane multiple times.
1548 state = drm_atomic_state_alloc(plane->dev);
1552 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(plane->crtc);
1554 plane_state = drm_atomic_get_plane_state(state, plane);
1555 if (IS_ERR(plane_state)) {
1556 ret = PTR_ERR(plane_state);
1560 if (plane_state->crtc && (plane == plane->crtc->cursor))
1561 plane_state->state->legacy_cursor_update = true;
1563 ret = __drm_atomic_helper_disable_plane(plane, plane_state);
1567 ret = drm_atomic_commit(state);
1571 /* Driver takes ownership of state on successful commit. */
1574 if (ret == -EDEADLK)
1577 drm_atomic_state_free(state);
1581 drm_atomic_state_clear(state);
1582 drm_atomic_legacy_backoff(state);
1585 * Someone might have exchanged the framebuffer while we dropped locks
1586 * in the backoff code. We need to fix up the fb refcount tracking the
1589 plane->old_fb = plane->fb;
1593 EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
1595 /* just used from fb-helper and atomic-helper: */
1596 int __drm_atomic_helper_disable_plane(struct drm_plane *plane,
1597 struct drm_plane_state *plane_state)
1601 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
1605 drm_atomic_set_fb_for_plane(plane_state, NULL);
1606 plane_state->crtc_x = 0;
1607 plane_state->crtc_y = 0;
1608 plane_state->crtc_h = 0;
1609 plane_state->crtc_w = 0;
1610 plane_state->src_x = 0;
1611 plane_state->src_y = 0;
1612 plane_state->src_h = 0;
1613 plane_state->src_w = 0;
1618 static int update_output_state(struct drm_atomic_state *state,
1619 struct drm_mode_set *set)
1621 struct drm_device *dev = set->crtc->dev;
1622 struct drm_crtc *crtc;
1623 struct drm_crtc_state *crtc_state;
1624 struct drm_connector *connector;
1625 struct drm_connector_state *conn_state;
1628 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
1629 state->acquire_ctx);
1633 /* First grab all affected connector/crtc states. */
1634 for (i = 0; i < set->num_connectors; i++) {
1635 conn_state = drm_atomic_get_connector_state(state,
1636 set->connectors[i]);
1637 if (IS_ERR(conn_state))
1638 return PTR_ERR(conn_state);
1641 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1642 ret = drm_atomic_add_affected_connectors(state, crtc);
1647 /* Then recompute connector->crtc links and crtc enabling state. */
1648 for_each_connector_in_state(state, connector, conn_state, i) {
1649 if (conn_state->crtc == set->crtc) {
1650 ret = drm_atomic_set_crtc_for_connector(conn_state,
1656 for (j = 0; j < set->num_connectors; j++) {
1657 if (set->connectors[j] == connector) {
1658 ret = drm_atomic_set_crtc_for_connector(conn_state,
1667 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1668 /* Don't update ->enable for the CRTC in the set_config request,
1669 * since a mismatch would indicate a bug in the upper layers.
1670 * The actual modeset code later on will catch any
1671 * inconsistencies here. */
1672 if (crtc == set->crtc)
1675 if (!drm_atomic_connectors_for_crtc(state, crtc)) {
1676 ret = drm_atomic_set_mode_prop_for_crtc(crtc_state,
1681 crtc_state->active = false;
1689 * drm_atomic_helper_set_config - set a new config from userspace
1690 * @set: mode set configuration
1692 * Provides a default crtc set_config handler using the atomic driver interface.
1695 * Returns 0 on success, negative errno numbers on failure.
1697 int drm_atomic_helper_set_config(struct drm_mode_set *set)
1699 struct drm_atomic_state *state;
1700 struct drm_crtc *crtc = set->crtc;
1703 state = drm_atomic_state_alloc(crtc->dev);
1707 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1709 ret = __drm_atomic_helper_set_config(set, state);
1713 ret = drm_atomic_commit(state);
1717 /* Driver takes ownership of state on successful commit. */
1720 if (ret == -EDEADLK)
1723 drm_atomic_state_free(state);
1727 drm_atomic_state_clear(state);
1728 drm_atomic_legacy_backoff(state);
1731 * Someone might have exchanged the framebuffer while we dropped locks
1732 * in the backoff code. We need to fix up the fb refcount tracking the
1735 crtc->primary->old_fb = crtc->primary->fb;
1739 EXPORT_SYMBOL(drm_atomic_helper_set_config);
1741 /* just used from fb-helper and atomic-helper: */
1742 int __drm_atomic_helper_set_config(struct drm_mode_set *set,
1743 struct drm_atomic_state *state)
1745 struct drm_crtc_state *crtc_state;
1746 struct drm_plane_state *primary_state;
1747 struct drm_crtc *crtc = set->crtc;
1748 int hdisplay, vdisplay;
1751 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1752 if (IS_ERR(crtc_state))
1753 return PTR_ERR(crtc_state);
1755 primary_state = drm_atomic_get_plane_state(state, crtc->primary);
1756 if (IS_ERR(primary_state))
1757 return PTR_ERR(primary_state);
1761 WARN_ON(set->num_connectors);
1763 ret = drm_atomic_set_mode_for_crtc(crtc_state, NULL);
1767 crtc_state->active = false;
1769 ret = drm_atomic_set_crtc_for_plane(primary_state, NULL);
1773 drm_atomic_set_fb_for_plane(primary_state, NULL);
1779 WARN_ON(!set->num_connectors);
1781 ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
1785 crtc_state->active = true;
1787 ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
1791 drm_crtc_get_hv_timing(set->mode, &hdisplay, &vdisplay);
1793 drm_atomic_set_fb_for_plane(primary_state, set->fb);
1794 primary_state->crtc_x = 0;
1795 primary_state->crtc_y = 0;
1796 primary_state->crtc_h = vdisplay;
1797 primary_state->crtc_w = hdisplay;
1798 primary_state->src_x = set->x << 16;
1799 primary_state->src_y = set->y << 16;
1800 if (primary_state->rotation & (BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270))) {
1801 primary_state->src_h = hdisplay << 16;
1802 primary_state->src_w = vdisplay << 16;
1804 primary_state->src_h = vdisplay << 16;
1805 primary_state->src_w = hdisplay << 16;
1809 ret = update_output_state(state, set);
1817 * drm_atomic_helper_crtc_set_property - helper for crtc properties
1819 * @property: DRM property
1820 * @val: value of property
1822 * Provides a default crtc set_property handler using the atomic driver
1826 * Zero on success, error code on failure
1829 drm_atomic_helper_crtc_set_property(struct drm_crtc *crtc,
1830 struct drm_property *property,
1833 struct drm_atomic_state *state;
1834 struct drm_crtc_state *crtc_state;
1837 state = drm_atomic_state_alloc(crtc->dev);
1841 /* ->set_property is always called with all locks held. */
1842 state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
1844 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1845 if (IS_ERR(crtc_state)) {
1846 ret = PTR_ERR(crtc_state);
1850 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
1855 ret = drm_atomic_commit(state);
1859 /* Driver takes ownership of state on successful commit. */
1862 if (ret == -EDEADLK)
1865 drm_atomic_state_free(state);
1869 drm_atomic_state_clear(state);
1870 drm_atomic_legacy_backoff(state);
1874 EXPORT_SYMBOL(drm_atomic_helper_crtc_set_property);
1877 * drm_atomic_helper_plane_set_property - helper for plane properties
1879 * @property: DRM property
1880 * @val: value of property
1882 * Provides a default plane set_property handler using the atomic driver
1886 * Zero on success, error code on failure
1889 drm_atomic_helper_plane_set_property(struct drm_plane *plane,
1890 struct drm_property *property,
1893 struct drm_atomic_state *state;
1894 struct drm_plane_state *plane_state;
1897 state = drm_atomic_state_alloc(plane->dev);
1901 /* ->set_property is always called with all locks held. */
1902 state->acquire_ctx = plane->dev->mode_config.acquire_ctx;
1904 plane_state = drm_atomic_get_plane_state(state, plane);
1905 if (IS_ERR(plane_state)) {
1906 ret = PTR_ERR(plane_state);
1910 ret = drm_atomic_plane_set_property(plane, plane_state,
1915 ret = drm_atomic_commit(state);
1919 /* Driver takes ownership of state on successful commit. */
1922 if (ret == -EDEADLK)
1925 drm_atomic_state_free(state);
1929 drm_atomic_state_clear(state);
1930 drm_atomic_legacy_backoff(state);
1934 EXPORT_SYMBOL(drm_atomic_helper_plane_set_property);
1937 * drm_atomic_helper_connector_set_property - helper for connector properties
1938 * @connector: DRM connector
1939 * @property: DRM property
1940 * @val: value of property
1942 * Provides a default connector set_property handler using the atomic driver
1946 * Zero on success, error code on failure
1949 drm_atomic_helper_connector_set_property(struct drm_connector *connector,
1950 struct drm_property *property,
1953 struct drm_atomic_state *state;
1954 struct drm_connector_state *connector_state;
1957 state = drm_atomic_state_alloc(connector->dev);
1961 /* ->set_property is always called with all locks held. */
1962 state->acquire_ctx = connector->dev->mode_config.acquire_ctx;
1964 connector_state = drm_atomic_get_connector_state(state, connector);
1965 if (IS_ERR(connector_state)) {
1966 ret = PTR_ERR(connector_state);
1970 ret = drm_atomic_connector_set_property(connector, connector_state,
1975 ret = drm_atomic_commit(state);
1979 /* Driver takes ownership of state on successful commit. */
1982 if (ret == -EDEADLK)
1985 drm_atomic_state_free(state);
1989 drm_atomic_state_clear(state);
1990 drm_atomic_legacy_backoff(state);
1994 EXPORT_SYMBOL(drm_atomic_helper_connector_set_property);
1997 * drm_atomic_helper_page_flip - execute a legacy page flip
1999 * @fb: DRM framebuffer
2000 * @event: optional DRM event to signal upon completion
2001 * @flags: flip flags for non-vblank sync'ed updates
2003 * Provides a default page flip implementation using the atomic driver interface.
2005 * Note that for now so called async page flips (i.e. updates which are not
2006 * synchronized to vblank) are not supported, since the atomic interfaces have
2007 * no provisions for this yet.
2010 * Returns 0 on success, negative errno numbers on failure.
2012 int drm_atomic_helper_page_flip(struct drm_crtc *crtc,
2013 struct drm_framebuffer *fb,
2014 struct drm_pending_vblank_event *event,
2017 struct drm_plane *plane = crtc->primary;
2018 struct drm_atomic_state *state;
2019 struct drm_plane_state *plane_state;
2020 struct drm_crtc_state *crtc_state;
2023 if (flags & DRM_MODE_PAGE_FLIP_ASYNC)
2026 state = drm_atomic_state_alloc(plane->dev);
2030 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2032 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2033 if (IS_ERR(crtc_state)) {
2034 ret = PTR_ERR(crtc_state);
2037 crtc_state->event = event;
2039 plane_state = drm_atomic_get_plane_state(state, plane);
2040 if (IS_ERR(plane_state)) {
2041 ret = PTR_ERR(plane_state);
2045 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
2048 drm_atomic_set_fb_for_plane(plane_state, fb);
2050 ret = drm_atomic_async_commit(state);
2054 /* Driver takes ownership of state on successful async commit. */
2057 if (ret == -EDEADLK)
2060 drm_atomic_state_free(state);
2064 drm_atomic_state_clear(state);
2065 drm_atomic_legacy_backoff(state);
2068 * Someone might have exchanged the framebuffer while we dropped locks
2069 * in the backoff code. We need to fix up the fb refcount tracking the
2072 plane->old_fb = plane->fb;
2076 EXPORT_SYMBOL(drm_atomic_helper_page_flip);
2079 * drm_atomic_helper_connector_dpms() - connector dpms helper implementation
2080 * @connector: affected connector
2083 * This is the main helper function provided by the atomic helper framework for
2084 * implementing the legacy DPMS connector interface. It computes the new desired
2085 * ->active state for the corresponding CRTC (if the connector is enabled) and
2089 * Returns 0 on success, negative errno numbers on failure.
2091 int drm_atomic_helper_connector_dpms(struct drm_connector *connector,
2094 struct drm_mode_config *config = &connector->dev->mode_config;
2095 struct drm_atomic_state *state;
2096 struct drm_crtc_state *crtc_state;
2097 struct drm_crtc *crtc;
2098 struct drm_connector *tmp_connector;
2100 bool active = false;
2101 int old_mode = connector->dpms;
2103 if (mode != DRM_MODE_DPMS_ON)
2104 mode = DRM_MODE_DPMS_OFF;
2106 connector->dpms = mode;
2107 crtc = connector->state->crtc;
2112 state = drm_atomic_state_alloc(connector->dev);
2116 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2118 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2119 if (IS_ERR(crtc_state)) {
2120 ret = PTR_ERR(crtc_state);
2124 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
2126 drm_for_each_connector(tmp_connector, connector->dev) {
2127 if (tmp_connector->state->crtc != crtc)
2130 if (tmp_connector->dpms == DRM_MODE_DPMS_ON) {
2135 crtc_state->active = active;
2137 ret = drm_atomic_commit(state);
2141 /* Driver takes ownership of state on successful commit. */
2144 if (ret == -EDEADLK)
2147 connector->dpms = old_mode;
2148 drm_atomic_state_free(state);
2152 drm_atomic_state_clear(state);
2153 drm_atomic_legacy_backoff(state);
2157 EXPORT_SYMBOL(drm_atomic_helper_connector_dpms);
2160 * DOC: atomic state reset and initialization
2162 * Both the drm core and the atomic helpers assume that there is always the full
2163 * and correct atomic software state for all connectors, CRTCs and planes
2164 * available. Which is a bit a problem on driver load and also after system
2165 * suspend. One way to solve this is to have a hardware state read-out
2166 * infrastructure which reconstructs the full software state (e.g. the i915
2169 * The simpler solution is to just reset the software state to everything off,
2170 * which is easiest to do by calling drm_mode_config_reset(). To facilitate this
2171 * the atomic helpers provide default reset implementations for all hooks.
2175 * drm_atomic_helper_crtc_reset - default ->reset hook for CRTCs
2178 * Resets the atomic state for @crtc by freeing the state pointer (which might
2179 * be NULL, e.g. at driver load time) and allocating a new empty state object.
2181 void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc)
2183 if (crtc->state && crtc->state->mode_blob)
2184 drm_property_unreference_blob(crtc->state->mode_blob);
2186 crtc->state = kzalloc(sizeof(*crtc->state), GFP_KERNEL);
2189 crtc->state->crtc = crtc;
2191 EXPORT_SYMBOL(drm_atomic_helper_crtc_reset);
2194 * __drm_atomic_helper_crtc_duplicate_state - copy atomic CRTC state
2195 * @crtc: CRTC object
2196 * @state: atomic CRTC state
2198 * Copies atomic state from a CRTC's current state and resets inferred values.
2199 * This is useful for drivers that subclass the CRTC state.
2201 void __drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc,
2202 struct drm_crtc_state *state)
2204 memcpy(state, crtc->state, sizeof(*state));
2206 if (state->mode_blob)
2207 drm_property_reference_blob(state->mode_blob);
2208 state->mode_changed = false;
2209 state->active_changed = false;
2210 state->planes_changed = false;
2211 state->connectors_changed = false;
2212 state->event = NULL;
2214 EXPORT_SYMBOL(__drm_atomic_helper_crtc_duplicate_state);
2217 * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
2220 * Default CRTC state duplicate hook for drivers which don't have their own
2221 * subclassed CRTC state structure.
2223 struct drm_crtc_state *
2224 drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc)
2226 struct drm_crtc_state *state;
2228 if (WARN_ON(!crtc->state))
2231 state = kmalloc(sizeof(*state), GFP_KERNEL);
2233 __drm_atomic_helper_crtc_duplicate_state(crtc, state);
2237 EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state);
2240 * __drm_atomic_helper_crtc_destroy_state - release CRTC state
2241 * @crtc: CRTC object
2242 * @state: CRTC state object to release
2244 * Releases all resources stored in the CRTC state without actually freeing
2245 * the memory of the CRTC state. This is useful for drivers that subclass the
2248 void __drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
2249 struct drm_crtc_state *state)
2251 if (state->mode_blob)
2252 drm_property_unreference_blob(state->mode_blob);
2254 EXPORT_SYMBOL(__drm_atomic_helper_crtc_destroy_state);
2257 * drm_atomic_helper_crtc_destroy_state - default state destroy hook
2259 * @state: CRTC state object to release
2261 * Default CRTC state destroy hook for drivers which don't have their own
2262 * subclassed CRTC state structure.
2264 void drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
2265 struct drm_crtc_state *state)
2267 __drm_atomic_helper_crtc_destroy_state(crtc, state);
2270 EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state);
2273 * drm_atomic_helper_plane_reset - default ->reset hook for planes
2276 * Resets the atomic state for @plane by freeing the state pointer (which might
2277 * be NULL, e.g. at driver load time) and allocating a new empty state object.
2279 void drm_atomic_helper_plane_reset(struct drm_plane *plane)
2281 if (plane->state && plane->state->fb)
2282 drm_framebuffer_unreference(plane->state->fb);
2284 kfree(plane->state);
2285 plane->state = kzalloc(sizeof(*plane->state), GFP_KERNEL);
2288 plane->state->plane = plane;
2290 EXPORT_SYMBOL(drm_atomic_helper_plane_reset);
2293 * __drm_atomic_helper_plane_duplicate_state - copy atomic plane state
2294 * @plane: plane object
2295 * @state: atomic plane state
2297 * Copies atomic state from a plane's current state. This is useful for
2298 * drivers that subclass the plane state.
2300 void __drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane,
2301 struct drm_plane_state *state)
2303 memcpy(state, plane->state, sizeof(*state));
2306 drm_framebuffer_reference(state->fb);
2308 EXPORT_SYMBOL(__drm_atomic_helper_plane_duplicate_state);
2311 * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
2314 * Default plane state duplicate hook for drivers which don't have their own
2315 * subclassed plane state structure.
2317 struct drm_plane_state *
2318 drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane)
2320 struct drm_plane_state *state;
2322 if (WARN_ON(!plane->state))
2325 state = kmalloc(sizeof(*state), GFP_KERNEL);
2327 __drm_atomic_helper_plane_duplicate_state(plane, state);
2331 EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state);
2334 * __drm_atomic_helper_plane_destroy_state - release plane state
2335 * @plane: plane object
2336 * @state: plane state object to release
2338 * Releases all resources stored in the plane state without actually freeing
2339 * the memory of the plane state. This is useful for drivers that subclass the
2342 void __drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
2343 struct drm_plane_state *state)
2346 drm_framebuffer_unreference(state->fb);
2348 EXPORT_SYMBOL(__drm_atomic_helper_plane_destroy_state);
2351 * drm_atomic_helper_plane_destroy_state - default state destroy hook
2353 * @state: plane state object to release
2355 * Default plane state destroy hook for drivers which don't have their own
2356 * subclassed plane state structure.
2358 void drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
2359 struct drm_plane_state *state)
2361 __drm_atomic_helper_plane_destroy_state(plane, state);
2364 EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state);
2367 * drm_atomic_helper_connector_reset - default ->reset hook for connectors
2368 * @connector: drm connector
2370 * Resets the atomic state for @connector by freeing the state pointer (which
2371 * might be NULL, e.g. at driver load time) and allocating a new empty state
2374 void drm_atomic_helper_connector_reset(struct drm_connector *connector)
2376 kfree(connector->state);
2377 connector->state = kzalloc(sizeof(*connector->state), GFP_KERNEL);
2379 if (connector->state)
2380 connector->state->connector = connector;
2382 EXPORT_SYMBOL(drm_atomic_helper_connector_reset);
2385 * __drm_atomic_helper_connector_duplicate_state - copy atomic connector state
2386 * @connector: connector object
2387 * @state: atomic connector state
2389 * Copies atomic state from a connector's current state. This is useful for
2390 * drivers that subclass the connector state.
2393 __drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector,
2394 struct drm_connector_state *state)
2396 memcpy(state, connector->state, sizeof(*state));
2398 EXPORT_SYMBOL(__drm_atomic_helper_connector_duplicate_state);
2401 * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
2402 * @connector: drm connector
2404 * Default connector state duplicate hook for drivers which don't have their own
2405 * subclassed connector state structure.
2407 struct drm_connector_state *
2408 drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
2410 struct drm_connector_state *state;
2412 if (WARN_ON(!connector->state))
2415 state = kmalloc(sizeof(*state), GFP_KERNEL);
2417 __drm_atomic_helper_connector_duplicate_state(connector, state);
2421 EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
2424 * drm_atomic_helper_duplicate_state - duplicate an atomic state object
2426 * @ctx: lock acquisition context
2428 * Makes a copy of the current atomic state by looping over all objects and
2429 * duplicating their respective states.
2431 * Note that this treats atomic state as persistent between save and restore.
2432 * Drivers must make sure that this is possible and won't result in confusion
2433 * or erroneous behaviour.
2435 * Note that if callers haven't already acquired all modeset locks this might
2436 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
2439 * A pointer to the copy of the atomic state object on success or an
2440 * ERR_PTR()-encoded error code on failure.
2442 struct drm_atomic_state *
2443 drm_atomic_helper_duplicate_state(struct drm_device *dev,
2444 struct drm_modeset_acquire_ctx *ctx)
2446 struct drm_atomic_state *state;
2447 struct drm_connector *conn;
2448 struct drm_plane *plane;
2449 struct drm_crtc *crtc;
2452 state = drm_atomic_state_alloc(dev);
2454 return ERR_PTR(-ENOMEM);
2456 state->acquire_ctx = ctx;
2458 drm_for_each_crtc(crtc, dev) {
2459 struct drm_crtc_state *crtc_state;
2461 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2462 if (IS_ERR(crtc_state)) {
2463 err = PTR_ERR(crtc_state);
2468 drm_for_each_plane(plane, dev) {
2469 struct drm_plane_state *plane_state;
2471 plane_state = drm_atomic_get_plane_state(state, plane);
2472 if (IS_ERR(plane_state)) {
2473 err = PTR_ERR(plane_state);
2478 drm_for_each_connector(conn, dev) {
2479 struct drm_connector_state *conn_state;
2481 conn_state = drm_atomic_get_connector_state(state, conn);
2482 if (IS_ERR(conn_state)) {
2483 err = PTR_ERR(conn_state);
2488 /* clear the acquire context so that it isn't accidentally reused */
2489 state->acquire_ctx = NULL;
2493 drm_atomic_state_free(state);
2494 state = ERR_PTR(err);
2499 EXPORT_SYMBOL(drm_atomic_helper_duplicate_state);
2502 * __drm_atomic_helper_connector_destroy_state - release connector state
2503 * @connector: connector object
2504 * @state: connector state object to release
2506 * Releases all resources stored in the connector state without actually
2507 * freeing the memory of the connector state. This is useful for drivers that
2508 * subclass the connector state.
2511 __drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
2512 struct drm_connector_state *state)
2515 * This is currently a placeholder so that drivers that subclass the
2516 * state will automatically do the right thing if code is ever added
2520 EXPORT_SYMBOL(__drm_atomic_helper_connector_destroy_state);
2523 * drm_atomic_helper_connector_destroy_state - default state destroy hook
2524 * @connector: drm connector
2525 * @state: connector state object to release
2527 * Default connector state destroy hook for drivers which don't have their own
2528 * subclassed connector state structure.
2530 void drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
2531 struct drm_connector_state *state)
2533 __drm_atomic_helper_connector_destroy_state(connector, state);
2536 EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state);