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 expose
47 * 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 list_for_each_entry(connector, &config->connector_list, head) {
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;
114 * We can only steal an encoder coming from a connector, which means we
115 * must already hold the connection_mutex.
117 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
119 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d], stealing it\n",
120 encoder->base.id, encoder->name,
121 encoder_crtc->base.id);
123 crtc_state = drm_atomic_get_crtc_state(state, encoder_crtc);
124 if (IS_ERR(crtc_state))
125 return PTR_ERR(crtc_state);
127 crtc_state->mode_changed = true;
129 list_for_each_entry(connector, &config->connector_list, head) {
130 if (connector->state->best_encoder != encoder)
133 DRM_DEBUG_ATOMIC("Stealing encoder from [CONNECTOR:%d:%s]\n",
137 connector_state = drm_atomic_get_connector_state(state,
139 if (IS_ERR(connector_state))
140 return PTR_ERR(connector_state);
142 ret = drm_atomic_set_crtc_for_connector(connector_state, NULL);
145 connector_state->best_encoder = NULL;
152 update_connector_routing(struct drm_atomic_state *state, int conn_idx)
154 const struct drm_connector_helper_funcs *funcs;
155 struct drm_encoder *new_encoder;
156 struct drm_crtc *encoder_crtc;
157 struct drm_connector *connector;
158 struct drm_connector_state *connector_state;
159 struct drm_crtc_state *crtc_state;
162 connector = state->connectors[conn_idx];
163 connector_state = state->connector_states[conn_idx];
168 DRM_DEBUG_ATOMIC("Updating routing for [CONNECTOR:%d:%s]\n",
172 if (connector->state->crtc != connector_state->crtc) {
173 if (connector->state->crtc) {
174 idx = drm_crtc_index(connector->state->crtc);
176 crtc_state = state->crtc_states[idx];
177 crtc_state->mode_changed = true;
180 if (connector_state->crtc) {
181 idx = drm_crtc_index(connector_state->crtc);
183 crtc_state = state->crtc_states[idx];
184 crtc_state->mode_changed = true;
188 if (!connector_state->crtc) {
189 DRM_DEBUG_ATOMIC("Disabling [CONNECTOR:%d:%s]\n",
193 connector_state->best_encoder = NULL;
198 funcs = connector->helper_private;
200 if (funcs->atomic_best_encoder)
201 new_encoder = funcs->atomic_best_encoder(connector,
204 new_encoder = funcs->best_encoder(connector);
207 DRM_DEBUG_ATOMIC("No suitable encoder found for [CONNECTOR:%d:%s]\n",
213 if (new_encoder == connector_state->best_encoder) {
214 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] keeps [ENCODER:%d:%s], now on [CRTC:%d]\n",
217 new_encoder->base.id,
219 connector_state->crtc->base.id);
224 encoder_crtc = get_current_crtc_for_encoder(state->dev,
228 ret = steal_encoder(state, new_encoder, encoder_crtc);
230 DRM_DEBUG_ATOMIC("Encoder stealing failed for [CONNECTOR:%d:%s]\n",
237 connector_state->best_encoder = new_encoder;
238 if (connector_state->crtc) {
239 idx = drm_crtc_index(connector_state->crtc);
241 crtc_state = state->crtc_states[idx];
242 crtc_state->mode_changed = true;
245 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] using [ENCODER:%d:%s] on [CRTC:%d]\n",
248 new_encoder->base.id,
250 connector_state->crtc->base.id);
256 mode_fixup(struct drm_atomic_state *state)
258 struct drm_crtc *crtc;
259 struct drm_crtc_state *crtc_state;
260 struct drm_connector *connector;
261 struct drm_connector_state *conn_state;
265 for_each_crtc_in_state(state, crtc, crtc_state, i) {
266 if (!crtc_state->mode_changed)
269 drm_mode_copy(&crtc_state->adjusted_mode, &crtc_state->mode);
272 for_each_connector_in_state(state, connector, conn_state, i) {
273 const struct drm_encoder_helper_funcs *funcs;
274 struct drm_encoder *encoder;
276 WARN_ON(!!conn_state->best_encoder != !!conn_state->crtc);
278 if (!conn_state->crtc || !conn_state->best_encoder)
282 state->crtc_states[drm_crtc_index(conn_state->crtc)];
285 * Each encoder has at most one connector (since we always steal
286 * it away), so we won't call ->mode_fixup twice.
288 encoder = conn_state->best_encoder;
289 funcs = encoder->helper_private;
293 ret = drm_bridge_mode_fixup(encoder->bridge, &crtc_state->mode,
294 &crtc_state->adjusted_mode);
296 DRM_DEBUG_ATOMIC("Bridge fixup failed\n");
300 if (funcs->atomic_check) {
301 ret = funcs->atomic_check(encoder, crtc_state,
304 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] check failed\n",
305 encoder->base.id, encoder->name);
309 ret = funcs->mode_fixup(encoder, &crtc_state->mode,
310 &crtc_state->adjusted_mode);
312 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] fixup failed\n",
313 encoder->base.id, encoder->name);
319 for_each_crtc_in_state(state, crtc, crtc_state, i) {
320 const struct drm_crtc_helper_funcs *funcs;
322 if (!crtc_state->mode_changed)
325 funcs = crtc->helper_private;
326 if (!funcs->mode_fixup)
329 ret = funcs->mode_fixup(crtc, &crtc_state->mode,
330 &crtc_state->adjusted_mode);
332 DRM_DEBUG_ATOMIC("[CRTC:%d] fixup failed\n",
342 * drm_atomic_helper_check_modeset - validate state object for modeset changes
344 * @state: the driver state object
346 * Check the state object to see if the requested state is physically possible.
347 * This does all the crtc and connector related computations for an atomic
348 * update. It computes and updates crtc_state->mode_changed, adds any additional
349 * connectors needed for full modesets and calls down into ->mode_fixup
350 * functions of the driver backend.
354 * Drivers which update ->mode_changed (e.g. in their ->atomic_check hooks if a
355 * plane update can't be done without a full modeset) _must_ call this function
356 * afterwards after that change. It is permitted to call this function multiple
357 * times for the same update, e.g. when the ->atomic_check functions depend upon
358 * the adjusted dotclock for fifo space allocation and watermark computation.
361 * Zero for success or -errno
364 drm_atomic_helper_check_modeset(struct drm_device *dev,
365 struct drm_atomic_state *state)
367 struct drm_crtc *crtc;
368 struct drm_crtc_state *crtc_state;
369 struct drm_connector *connector;
370 struct drm_connector_state *connector_state;
373 for_each_crtc_in_state(state, crtc, crtc_state, i) {
374 if (!drm_mode_equal(&crtc->state->mode, &crtc_state->mode)) {
375 DRM_DEBUG_ATOMIC("[CRTC:%d] mode changed\n",
377 crtc_state->mode_changed = true;
380 if (crtc->state->enable != crtc_state->enable) {
381 DRM_DEBUG_ATOMIC("[CRTC:%d] enable changed\n",
383 crtc_state->mode_changed = true;
387 for_each_connector_in_state(state, connector, connector_state, i) {
389 * This only sets crtc->mode_changed for routing changes,
390 * drivers must set crtc->mode_changed themselves when connector
391 * properties need to be updated.
393 ret = update_connector_routing(state, i);
399 * After all the routing has been prepared we need to add in any
400 * connector which is itself unchanged, but who's crtc changes it's
401 * configuration. This must be done before calling mode_fixup in case a
402 * crtc only changed its mode but has the same set of connectors.
404 for_each_crtc_in_state(state, crtc, crtc_state, i) {
408 * We must set ->active_changed after walking connectors for
409 * otherwise an update that only changes active would result in
410 * a full modeset because update_connector_routing force that.
412 if (crtc->state->active != crtc_state->active) {
413 DRM_DEBUG_ATOMIC("[CRTC:%d] active changed\n",
415 crtc_state->active_changed = true;
418 if (!drm_atomic_crtc_needs_modeset(crtc_state))
421 DRM_DEBUG_ATOMIC("[CRTC:%d] needs all connectors, enable: %c, active: %c\n",
423 crtc_state->enable ? 'y' : 'n',
424 crtc_state->active ? 'y' : 'n');
426 ret = drm_atomic_add_affected_connectors(state, crtc);
430 ret = drm_atomic_add_affected_planes(state, crtc);
434 num_connectors = drm_atomic_connectors_for_crtc(state,
437 if (crtc_state->enable != !!num_connectors) {
438 DRM_DEBUG_ATOMIC("[CRTC:%d] enabled/connectors mismatch\n",
445 return mode_fixup(state);
447 EXPORT_SYMBOL(drm_atomic_helper_check_modeset);
450 * drm_atomic_helper_check_planes - validate state object for planes changes
452 * @state: the driver state object
454 * Check the state object to see if the requested state is physically possible.
455 * This does all the plane update related checks using by calling into the
456 * ->atomic_check hooks provided by the driver.
459 * Zero for success or -errno
462 drm_atomic_helper_check_planes(struct drm_device *dev,
463 struct drm_atomic_state *state)
465 struct drm_crtc *crtc;
466 struct drm_crtc_state *crtc_state;
467 struct drm_plane *plane;
468 struct drm_plane_state *plane_state;
471 for_each_plane_in_state(state, plane, plane_state, i) {
472 const struct drm_plane_helper_funcs *funcs;
474 funcs = plane->helper_private;
476 drm_atomic_helper_plane_changed(state, plane_state, plane);
478 if (!funcs || !funcs->atomic_check)
481 ret = funcs->atomic_check(plane, plane_state);
483 DRM_DEBUG_ATOMIC("[PLANE:%d] atomic driver check failed\n",
489 for_each_crtc_in_state(state, crtc, crtc_state, i) {
490 const struct drm_crtc_helper_funcs *funcs;
492 funcs = crtc->helper_private;
494 if (!funcs || !funcs->atomic_check)
497 ret = funcs->atomic_check(crtc, state->crtc_states[i]);
499 DRM_DEBUG_ATOMIC("[CRTC:%d] atomic driver check failed\n",
507 EXPORT_SYMBOL(drm_atomic_helper_check_planes);
510 * drm_atomic_helper_check - validate state object
512 * @state: the driver state object
514 * Check the state object to see if the requested state is physically possible.
515 * Only crtcs and planes have check callbacks, so for any additional (global)
516 * checking that a driver needs it can simply wrap that around this function.
517 * Drivers without such needs can directly use this as their ->atomic_check()
520 * This just wraps the two parts of the state checking for planes and modeset
521 * state in the default order: First it calls drm_atomic_helper_check_modeset()
522 * and then drm_atomic_helper_check_planes(). The assumption is that the
523 * ->atomic_check functions depend upon an updated adjusted_mode.clock to
524 * e.g. properly compute watermarks.
527 * Zero for success or -errno
529 int drm_atomic_helper_check(struct drm_device *dev,
530 struct drm_atomic_state *state)
534 ret = drm_atomic_helper_check_modeset(dev, state);
538 ret = drm_atomic_helper_check_planes(dev, state);
544 EXPORT_SYMBOL(drm_atomic_helper_check);
547 disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state)
549 struct drm_connector *connector;
550 struct drm_connector_state *old_conn_state;
551 struct drm_crtc *crtc;
552 struct drm_crtc_state *old_crtc_state;
555 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
556 const struct drm_encoder_helper_funcs *funcs;
557 struct drm_encoder *encoder;
558 struct drm_crtc_state *old_crtc_state;
560 /* Shut down everything that's in the changeset and currently
561 * still on. So need to check the old, saved state. */
562 if (!old_conn_state->crtc)
565 old_crtc_state = old_state->crtc_states[drm_crtc_index(old_conn_state->crtc)];
567 if (!old_crtc_state->active ||
568 !drm_atomic_crtc_needs_modeset(old_conn_state->crtc->state))
571 encoder = old_conn_state->best_encoder;
573 /* We shouldn't get this far if we didn't previously have
574 * an encoder.. but WARN_ON() rather than explode.
576 if (WARN_ON(!encoder))
579 funcs = encoder->helper_private;
581 DRM_DEBUG_ATOMIC("disabling [ENCODER:%d:%s]\n",
582 encoder->base.id, encoder->name);
585 * Each encoder has at most one connector (since we always steal
586 * it away), so we won't call disable hooks twice.
588 drm_bridge_disable(encoder->bridge);
590 /* Right function depends upon target state. */
591 if (connector->state->crtc && funcs->prepare)
592 funcs->prepare(encoder);
593 else if (funcs->disable)
594 funcs->disable(encoder);
596 funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
598 drm_bridge_post_disable(encoder->bridge);
601 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
602 const struct drm_crtc_helper_funcs *funcs;
604 /* Shut down everything that needs a full modeset. */
605 if (!drm_atomic_crtc_needs_modeset(crtc->state))
608 if (!old_crtc_state->active)
611 funcs = crtc->helper_private;
613 DRM_DEBUG_ATOMIC("disabling [CRTC:%d]\n",
617 /* Right function depends upon target state. */
618 if (crtc->state->enable && funcs->prepare)
619 funcs->prepare(crtc);
620 else if (funcs->disable)
621 funcs->disable(crtc);
623 funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
628 * drm_atomic_helper_update_legacy_modeset_state - update legacy modeset state
630 * @old_state: atomic state object with old state structures
632 * This function updates all the various legacy modeset state pointers in
633 * connectors, encoders and crtcs. It also updates the timestamping constants
634 * used for precise vblank timestamps by calling
635 * drm_calc_timestamping_constants().
637 * Drivers can use this for building their own atomic commit if they don't have
638 * a pure helper-based modeset implementation.
641 drm_atomic_helper_update_legacy_modeset_state(struct drm_device *dev,
642 struct drm_atomic_state *old_state)
644 struct drm_connector *connector;
645 struct drm_connector_state *old_conn_state;
646 struct drm_crtc *crtc;
647 struct drm_crtc_state *old_crtc_state;
650 /* clear out existing links */
651 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
652 if (!connector->encoder)
655 WARN_ON(!connector->encoder->crtc);
657 connector->encoder->crtc = NULL;
658 connector->encoder = NULL;
662 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
663 if (!connector->state->crtc)
666 if (WARN_ON(!connector->state->best_encoder))
669 connector->encoder = connector->state->best_encoder;
670 connector->encoder->crtc = connector->state->crtc;
673 /* set legacy state in the crtc structure */
674 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
675 crtc->mode = crtc->state->mode;
676 crtc->enabled = crtc->state->enable;
677 crtc->x = crtc->primary->state->src_x >> 16;
678 crtc->y = crtc->primary->state->src_y >> 16;
680 if (crtc->state->enable)
681 drm_calc_timestamping_constants(crtc,
682 &crtc->state->adjusted_mode);
685 EXPORT_SYMBOL(drm_atomic_helper_update_legacy_modeset_state);
688 crtc_set_mode(struct drm_device *dev, struct drm_atomic_state *old_state)
690 struct drm_crtc *crtc;
691 struct drm_crtc_state *old_crtc_state;
692 struct drm_connector *connector;
693 struct drm_connector_state *old_conn_state;
696 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
697 const struct drm_crtc_helper_funcs *funcs;
699 if (!crtc->state->mode_changed)
702 funcs = crtc->helper_private;
704 if (crtc->state->enable && funcs->mode_set_nofb) {
705 DRM_DEBUG_ATOMIC("modeset on [CRTC:%d]\n",
708 funcs->mode_set_nofb(crtc);
712 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
713 const struct drm_encoder_helper_funcs *funcs;
714 struct drm_crtc_state *new_crtc_state;
715 struct drm_encoder *encoder;
716 struct drm_display_mode *mode, *adjusted_mode;
718 if (!connector->state->best_encoder)
721 encoder = connector->state->best_encoder;
722 funcs = encoder->helper_private;
723 new_crtc_state = connector->state->crtc->state;
724 mode = &new_crtc_state->mode;
725 adjusted_mode = &new_crtc_state->adjusted_mode;
727 if (!new_crtc_state->mode_changed)
730 DRM_DEBUG_ATOMIC("modeset on [ENCODER:%d:%s]\n",
731 encoder->base.id, encoder->name);
734 * Each encoder has at most one connector (since we always steal
735 * it away), so we won't call mode_set hooks twice.
738 funcs->mode_set(encoder, mode, adjusted_mode);
740 drm_bridge_mode_set(encoder->bridge, mode, adjusted_mode);
745 * drm_atomic_helper_commit_modeset_disables - modeset commit to disable outputs
747 * @old_state: atomic state object with old state structures
749 * This function shuts down all the outputs that need to be shut down and
750 * prepares them (if required) with the new mode.
752 * For compatability with legacy crtc helpers this should be called before
753 * drm_atomic_helper_commit_planes(), which is what the default commit function
754 * does. But drivers with different needs can group the modeset commits together
755 * and do the plane commits at the end. This is useful for drivers doing runtime
756 * PM since planes updates then only happen when the CRTC is actually enabled.
758 void drm_atomic_helper_commit_modeset_disables(struct drm_device *dev,
759 struct drm_atomic_state *old_state)
761 disable_outputs(dev, old_state);
763 drm_atomic_helper_update_legacy_modeset_state(dev, old_state);
765 crtc_set_mode(dev, old_state);
767 EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_disables);
770 * drm_atomic_helper_commit_modeset_enables - modeset commit to enable outputs
772 * @old_state: atomic state object with old state structures
774 * This function enables all the outputs with the new configuration which had to
775 * be turned off for the update.
777 * For compatability with legacy crtc helpers this should be called after
778 * drm_atomic_helper_commit_planes(), which is what the default commit function
779 * does. But drivers with different needs can group the modeset commits together
780 * and do the plane commits at the end. This is useful for drivers doing runtime
781 * PM since planes updates then only happen when the CRTC is actually enabled.
783 void drm_atomic_helper_commit_modeset_enables(struct drm_device *dev,
784 struct drm_atomic_state *old_state)
786 struct drm_crtc *crtc;
787 struct drm_crtc_state *old_crtc_state;
788 struct drm_connector *connector;
789 struct drm_connector_state *old_conn_state;
792 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
793 const struct drm_crtc_helper_funcs *funcs;
795 /* Need to filter out CRTCs where only planes change. */
796 if (!drm_atomic_crtc_needs_modeset(crtc->state))
799 if (!crtc->state->active)
802 funcs = crtc->helper_private;
804 if (crtc->state->enable) {
805 DRM_DEBUG_ATOMIC("enabling [CRTC:%d]\n",
815 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
816 const struct drm_encoder_helper_funcs *funcs;
817 struct drm_encoder *encoder;
819 if (!connector->state->best_encoder)
822 if (!connector->state->crtc->state->active ||
823 !drm_atomic_crtc_needs_modeset(connector->state->crtc->state))
826 encoder = connector->state->best_encoder;
827 funcs = encoder->helper_private;
829 DRM_DEBUG_ATOMIC("enabling [ENCODER:%d:%s]\n",
830 encoder->base.id, encoder->name);
833 * Each encoder has at most one connector (since we always steal
834 * it away), so we won't call enable hooks twice.
836 drm_bridge_pre_enable(encoder->bridge);
839 funcs->enable(encoder);
841 funcs->commit(encoder);
843 drm_bridge_enable(encoder->bridge);
846 EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_enables);
848 static void wait_for_fences(struct drm_device *dev,
849 struct drm_atomic_state *state)
851 struct drm_plane *plane;
852 struct drm_plane_state *plane_state;
855 for_each_plane_in_state(state, plane, plane_state, i) {
856 if (!plane->state->fence)
859 WARN_ON(!plane->state->fb);
861 fence_wait(plane->state->fence, false);
862 fence_put(plane->state->fence);
863 plane->state->fence = NULL;
867 static bool framebuffer_changed(struct drm_device *dev,
868 struct drm_atomic_state *old_state,
869 struct drm_crtc *crtc)
871 struct drm_plane *plane;
872 struct drm_plane_state *old_plane_state;
875 for_each_plane_in_state(old_state, plane, old_plane_state, i) {
876 if (plane->state->crtc != crtc &&
877 old_plane_state->crtc != crtc)
880 if (plane->state->fb != old_plane_state->fb)
888 * drm_atomic_helper_wait_for_vblanks - wait for vblank on crtcs
890 * @old_state: atomic state object with old state structures
892 * Helper to, after atomic commit, wait for vblanks on all effected
893 * crtcs (ie. before cleaning up old framebuffers using
894 * drm_atomic_helper_cleanup_planes()). It will only wait on crtcs where the
895 * framebuffers have actually changed to optimize for the legacy cursor and
896 * plane update use-case.
899 drm_atomic_helper_wait_for_vblanks(struct drm_device *dev,
900 struct drm_atomic_state *old_state)
902 struct drm_crtc *crtc;
903 struct drm_crtc_state *old_crtc_state;
906 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
907 /* No one cares about the old state, so abuse it for tracking
908 * and store whether we hold a vblank reference (and should do a
909 * vblank wait) in the ->enable boolean. */
910 old_crtc_state->enable = false;
912 if (!crtc->state->enable)
915 /* Legacy cursor ioctls are completely unsynced, and userspace
916 * relies on that (by doing tons of cursor updates). */
917 if (old_state->legacy_cursor_update)
920 if (!framebuffer_changed(dev, old_state, crtc))
923 ret = drm_crtc_vblank_get(crtc);
927 old_crtc_state->enable = true;
928 old_crtc_state->last_vblank_count = drm_vblank_count(dev, i);
931 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
932 if (!old_crtc_state->enable)
935 ret = wait_event_timeout(dev->vblank[i].queue,
936 old_crtc_state->last_vblank_count !=
937 drm_vblank_count(dev, i),
938 msecs_to_jiffies(50));
940 drm_crtc_vblank_put(crtc);
943 EXPORT_SYMBOL(drm_atomic_helper_wait_for_vblanks);
946 * drm_atomic_helper_commit - commit validated state object
948 * @state: the driver state object
949 * @async: asynchronous commit
951 * This function commits a with drm_atomic_helper_check() pre-validated state
952 * object. This can still fail when e.g. the framebuffer reservation fails. For
953 * now this doesn't implement asynchronous commits.
956 * Zero for success or -errno.
958 int drm_atomic_helper_commit(struct drm_device *dev,
959 struct drm_atomic_state *state,
967 ret = drm_atomic_helper_prepare_planes(dev, state);
972 * This is the point of no return - everything below never fails except
973 * when the hw goes bonghits. Which means we can commit the new state on
974 * the software side now.
977 drm_atomic_helper_swap_state(dev, state);
980 * Everything below can be run asynchronously without the need to grab
981 * any modeset locks at all under one condition: It must be guaranteed
982 * that the asynchronous work has either been cancelled (if the driver
983 * supports it, which at least requires that the framebuffers get
984 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
985 * before the new state gets committed on the software side with
986 * drm_atomic_helper_swap_state().
988 * This scheme allows new atomic state updates to be prepared and
989 * checked in parallel to the asynchronous completion of the previous
990 * update. Which is important since compositors need to figure out the
991 * composition of the next frame right after having submitted the
995 wait_for_fences(dev, state);
997 drm_atomic_helper_commit_modeset_disables(dev, state);
999 drm_atomic_helper_commit_planes(dev, state);
1001 drm_atomic_helper_commit_modeset_enables(dev, state);
1003 drm_atomic_helper_wait_for_vblanks(dev, state);
1005 drm_atomic_helper_cleanup_planes(dev, state);
1007 drm_atomic_state_free(state);
1011 EXPORT_SYMBOL(drm_atomic_helper_commit);
1014 * DOC: implementing async commit
1016 * For now the atomic helpers don't support async commit directly. If there is
1017 * real need it could be added though, using the dma-buf fence infrastructure
1018 * for generic synchronization with outstanding rendering.
1020 * For now drivers have to implement async commit themselves, with the following
1021 * sequence being the recommended one:
1023 * 1. Run drm_atomic_helper_prepare_planes() first. This is the only function
1024 * which commit needs to call which can fail, so we want to run it first and
1027 * 2. Synchronize with any outstanding asynchronous commit worker threads which
1028 * might be affected the new state update. This can be done by either cancelling
1029 * or flushing the work items, depending upon whether the driver can deal with
1030 * cancelled updates. Note that it is important to ensure that the framebuffer
1031 * cleanup is still done when cancelling.
1033 * For sufficient parallelism it is recommended to have a work item per crtc
1034 * (for updates which don't touch global state) and a global one. Then we only
1035 * need to synchronize with the crtc work items for changed crtcs and the global
1036 * work item, which allows nice concurrent updates on disjoint sets of crtcs.
1038 * 3. The software state is updated synchronously with
1039 * drm_atomic_helper_swap_state. Doing this under the protection of all modeset
1040 * locks means concurrent callers never see inconsistent state. And doing this
1041 * while it's guaranteed that no relevant async worker runs means that async
1042 * workers do not need grab any locks. Actually they must not grab locks, for
1043 * otherwise the work flushing will deadlock.
1045 * 4. Schedule a work item to do all subsequent steps, using the split-out
1046 * commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and
1047 * then cleaning up the framebuffers after the old framebuffer is no longer
1052 * drm_atomic_helper_prepare_planes - prepare plane resources before commit
1054 * @state: atomic state object with new state structures
1056 * This function prepares plane state, specifically framebuffers, for the new
1057 * configuration. If any failure is encountered this function will call
1058 * ->cleanup_fb on any already successfully prepared framebuffer.
1061 * 0 on success, negative error code on failure.
1063 int drm_atomic_helper_prepare_planes(struct drm_device *dev,
1064 struct drm_atomic_state *state)
1066 int nplanes = dev->mode_config.num_total_plane;
1069 for (i = 0; i < nplanes; i++) {
1070 const struct drm_plane_helper_funcs *funcs;
1071 struct drm_plane *plane = state->planes[i];
1072 struct drm_plane_state *plane_state = state->plane_states[i];
1073 struct drm_framebuffer *fb;
1078 funcs = plane->helper_private;
1080 fb = plane_state->fb;
1082 if (fb && funcs->prepare_fb) {
1083 ret = funcs->prepare_fb(plane, fb, plane_state);
1092 for (i--; i >= 0; i--) {
1093 const struct drm_plane_helper_funcs *funcs;
1094 struct drm_plane *plane = state->planes[i];
1095 struct drm_plane_state *plane_state = state->plane_states[i];
1096 struct drm_framebuffer *fb;
1101 funcs = plane->helper_private;
1103 fb = state->plane_states[i]->fb;
1105 if (fb && funcs->cleanup_fb)
1106 funcs->cleanup_fb(plane, fb, plane_state);
1112 EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
1115 * drm_atomic_helper_commit_planes - commit plane state
1117 * @old_state: atomic state object with old state structures
1119 * This function commits the new plane state using the plane and atomic helper
1120 * functions for planes and crtcs. It assumes that the atomic state has already
1121 * been pushed into the relevant object state pointers, since this step can no
1124 * It still requires the global state object @old_state to know which planes and
1125 * crtcs need to be updated though.
1127 * Note that this function does all plane updates across all CRTCs in one step.
1128 * If the hardware can't support this approach look at
1129 * drm_atomic_helper_commit_planes_on_crtc() instead.
1131 void drm_atomic_helper_commit_planes(struct drm_device *dev,
1132 struct drm_atomic_state *old_state)
1134 struct drm_crtc *crtc;
1135 struct drm_crtc_state *old_crtc_state;
1136 struct drm_plane *plane;
1137 struct drm_plane_state *old_plane_state;
1140 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
1141 const struct drm_crtc_helper_funcs *funcs;
1143 funcs = crtc->helper_private;
1145 if (!funcs || !funcs->atomic_begin)
1148 funcs->atomic_begin(crtc);
1151 for_each_plane_in_state(old_state, plane, old_plane_state, i) {
1152 const struct drm_plane_helper_funcs *funcs;
1154 funcs = plane->helper_private;
1160 * Special-case disabling the plane if drivers support it.
1162 if (drm_atomic_plane_disabling(plane, old_plane_state) &&
1163 funcs->atomic_disable)
1164 funcs->atomic_disable(plane, old_plane_state);
1165 else if (plane->state->crtc ||
1166 drm_atomic_plane_disabling(plane, old_plane_state))
1167 funcs->atomic_update(plane, old_plane_state);
1170 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
1171 const struct drm_crtc_helper_funcs *funcs;
1173 funcs = crtc->helper_private;
1175 if (!funcs || !funcs->atomic_flush)
1178 funcs->atomic_flush(crtc);
1181 EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
1184 * drm_atomic_helper_commit_planes_on_crtc - commit plane state for a crtc
1185 * @old_crtc_state: atomic state object with the old crtc state
1187 * This function commits the new plane state using the plane and atomic helper
1188 * functions for planes on the specific crtc. It assumes that the atomic state
1189 * has already been pushed into the relevant object state pointers, since this
1190 * step can no longer fail.
1192 * This function is useful when plane updates should be done crtc-by-crtc
1193 * instead of one global step like drm_atomic_helper_commit_planes() does.
1195 * This function can only be savely used when planes are not allowed to move
1196 * between different CRTCs because this function doesn't handle inter-CRTC
1197 * depencies. Callers need to ensure that either no such depencies exist,
1198 * resolve them through ordering of commit calls or through some other means.
1201 drm_atomic_helper_commit_planes_on_crtc(struct drm_crtc_state *old_crtc_state)
1203 const struct drm_crtc_helper_funcs *crtc_funcs;
1204 struct drm_crtc *crtc = old_crtc_state->crtc;
1205 struct drm_atomic_state *old_state = old_crtc_state->state;
1206 struct drm_plane *plane;
1207 unsigned plane_mask;
1209 plane_mask = old_crtc_state->plane_mask;
1210 plane_mask |= crtc->state->plane_mask;
1212 crtc_funcs = crtc->helper_private;
1213 if (crtc_funcs && crtc_funcs->atomic_begin)
1214 crtc_funcs->atomic_begin(crtc);
1216 drm_for_each_plane_mask(plane, crtc->dev, plane_mask) {
1217 struct drm_plane_state *old_plane_state =
1218 drm_atomic_get_existing_plane_state(old_state, plane);
1219 const struct drm_plane_helper_funcs *plane_funcs;
1221 plane_funcs = plane->helper_private;
1223 if (!old_plane_state || !plane_funcs)
1226 WARN_ON(plane->state->crtc && plane->state->crtc != crtc);
1228 if (drm_atomic_plane_disabling(plane, old_plane_state) &&
1229 plane_funcs->atomic_disable)
1230 plane_funcs->atomic_disable(plane, old_plane_state);
1231 else if (plane->state->crtc ||
1232 drm_atomic_plane_disabling(plane, old_plane_state))
1233 plane_funcs->atomic_update(plane, old_plane_state);
1236 if (crtc_funcs && crtc_funcs->atomic_flush)
1237 crtc_funcs->atomic_flush(crtc);
1239 EXPORT_SYMBOL(drm_atomic_helper_commit_planes_on_crtc);
1242 * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
1244 * @old_state: atomic state object with old state structures
1246 * This function cleans up plane state, specifically framebuffers, from the old
1247 * configuration. Hence the old configuration must be perserved in @old_state to
1248 * be able to call this function.
1250 * This function must also be called on the new state when the atomic update
1251 * fails at any point after calling drm_atomic_helper_prepare_planes().
1253 void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
1254 struct drm_atomic_state *old_state)
1256 struct drm_plane *plane;
1257 struct drm_plane_state *plane_state;
1260 for_each_plane_in_state(old_state, plane, plane_state, i) {
1261 const struct drm_plane_helper_funcs *funcs;
1262 struct drm_framebuffer *old_fb;
1264 funcs = plane->helper_private;
1266 old_fb = plane_state->fb;
1268 if (old_fb && funcs->cleanup_fb)
1269 funcs->cleanup_fb(plane, old_fb, plane_state);
1272 EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
1275 * drm_atomic_helper_swap_state - store atomic state into current sw state
1277 * @state: atomic state
1279 * This function stores the atomic state into the current state pointers in all
1280 * driver objects. It should be called after all failing steps have been done
1281 * and succeeded, but before the actual hardware state is committed.
1283 * For cleanup and error recovery the current state for all changed objects will
1284 * be swaped into @state.
1286 * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
1288 * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
1290 * 2. Do any other steps that might fail.
1292 * 3. Put the staged state into the current state pointers with this function.
1294 * 4. Actually commit the hardware state.
1296 * 5. Call drm_atomic_helper_cleanup_planes with @state, which since step 3
1297 * contains the old state. Also do any other cleanup required with that state.
1299 void drm_atomic_helper_swap_state(struct drm_device *dev,
1300 struct drm_atomic_state *state)
1304 for (i = 0; i < dev->mode_config.num_connector; i++) {
1305 struct drm_connector *connector = state->connectors[i];
1310 connector->state->state = state;
1311 swap(state->connector_states[i], connector->state);
1312 connector->state->state = NULL;
1315 for (i = 0; i < dev->mode_config.num_crtc; i++) {
1316 struct drm_crtc *crtc = state->crtcs[i];
1321 crtc->state->state = state;
1322 swap(state->crtc_states[i], crtc->state);
1323 crtc->state->state = NULL;
1326 for (i = 0; i < dev->mode_config.num_total_plane; i++) {
1327 struct drm_plane *plane = state->planes[i];
1332 plane->state->state = state;
1333 swap(state->plane_states[i], plane->state);
1334 plane->state->state = NULL;
1337 EXPORT_SYMBOL(drm_atomic_helper_swap_state);
1340 * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
1341 * @plane: plane object to update
1342 * @crtc: owning CRTC of owning plane
1343 * @fb: framebuffer to flip onto plane
1344 * @crtc_x: x offset of primary plane on crtc
1345 * @crtc_y: y offset of primary plane on crtc
1346 * @crtc_w: width of primary plane rectangle on crtc
1347 * @crtc_h: height of primary plane rectangle on crtc
1348 * @src_x: x offset of @fb for panning
1349 * @src_y: y offset of @fb for panning
1350 * @src_w: width of source rectangle in @fb
1351 * @src_h: height of source rectangle in @fb
1353 * Provides a default plane update handler using the atomic driver interface.
1356 * Zero on success, error code on failure
1358 int drm_atomic_helper_update_plane(struct drm_plane *plane,
1359 struct drm_crtc *crtc,
1360 struct drm_framebuffer *fb,
1361 int crtc_x, int crtc_y,
1362 unsigned int crtc_w, unsigned int crtc_h,
1363 uint32_t src_x, uint32_t src_y,
1364 uint32_t src_w, uint32_t src_h)
1366 struct drm_atomic_state *state;
1367 struct drm_plane_state *plane_state;
1370 state = drm_atomic_state_alloc(plane->dev);
1374 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1376 plane_state = drm_atomic_get_plane_state(state, plane);
1377 if (IS_ERR(plane_state)) {
1378 ret = PTR_ERR(plane_state);
1382 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
1385 drm_atomic_set_fb_for_plane(plane_state, fb);
1386 plane_state->crtc_x = crtc_x;
1387 plane_state->crtc_y = crtc_y;
1388 plane_state->crtc_h = crtc_h;
1389 plane_state->crtc_w = crtc_w;
1390 plane_state->src_x = src_x;
1391 plane_state->src_y = src_y;
1392 plane_state->src_h = src_h;
1393 plane_state->src_w = src_w;
1395 if (plane == crtc->cursor)
1396 state->legacy_cursor_update = true;
1398 ret = drm_atomic_commit(state);
1402 /* Driver takes ownership of state on successful commit. */
1405 if (ret == -EDEADLK)
1408 drm_atomic_state_free(state);
1412 drm_atomic_state_clear(state);
1413 drm_atomic_legacy_backoff(state);
1416 * Someone might have exchanged the framebuffer while we dropped locks
1417 * in the backoff code. We need to fix up the fb refcount tracking the
1420 plane->old_fb = plane->fb;
1424 EXPORT_SYMBOL(drm_atomic_helper_update_plane);
1427 * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
1428 * @plane: plane to disable
1430 * Provides a default plane disable handler using the atomic driver interface.
1433 * Zero on success, error code on failure
1435 int drm_atomic_helper_disable_plane(struct drm_plane *plane)
1437 struct drm_atomic_state *state;
1438 struct drm_plane_state *plane_state;
1442 * FIXME: Without plane->crtc set we can't get at the implicit legacy
1443 * acquire context. The real fix will be to wire the acquire ctx through
1444 * everywhere we need it, but meanwhile prevent chaos by just skipping
1445 * this noop. The critical case is the cursor ioctls which a) only grab
1446 * crtc/cursor-plane locks (so we need the crtc to get at the right
1447 * acquire context) and b) can try to disable the plane multiple times.
1452 state = drm_atomic_state_alloc(plane->dev);
1456 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(plane->crtc);
1458 plane_state = drm_atomic_get_plane_state(state, plane);
1459 if (IS_ERR(plane_state)) {
1460 ret = PTR_ERR(plane_state);
1464 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
1467 drm_atomic_set_fb_for_plane(plane_state, NULL);
1468 plane_state->crtc_x = 0;
1469 plane_state->crtc_y = 0;
1470 plane_state->crtc_h = 0;
1471 plane_state->crtc_w = 0;
1472 plane_state->src_x = 0;
1473 plane_state->src_y = 0;
1474 plane_state->src_h = 0;
1475 plane_state->src_w = 0;
1477 if (plane == plane->crtc->cursor)
1478 state->legacy_cursor_update = true;
1480 ret = drm_atomic_commit(state);
1484 /* Driver takes ownership of state on successful commit. */
1487 if (ret == -EDEADLK)
1490 drm_atomic_state_free(state);
1494 drm_atomic_state_clear(state);
1495 drm_atomic_legacy_backoff(state);
1498 * Someone might have exchanged the framebuffer while we dropped locks
1499 * in the backoff code. We need to fix up the fb refcount tracking the
1502 plane->old_fb = plane->fb;
1506 EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
1508 static int update_output_state(struct drm_atomic_state *state,
1509 struct drm_mode_set *set)
1511 struct drm_device *dev = set->crtc->dev;
1512 struct drm_crtc *crtc;
1513 struct drm_crtc_state *crtc_state;
1514 struct drm_connector *connector;
1515 struct drm_connector_state *conn_state;
1518 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
1519 state->acquire_ctx);
1523 /* First grab all affected connector/crtc states. */
1524 for (i = 0; i < set->num_connectors; i++) {
1525 conn_state = drm_atomic_get_connector_state(state,
1526 set->connectors[i]);
1527 if (IS_ERR(conn_state))
1528 return PTR_ERR(conn_state);
1531 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1532 ret = drm_atomic_add_affected_connectors(state, crtc);
1537 /* Then recompute connector->crtc links and crtc enabling state. */
1538 for_each_connector_in_state(state, connector, conn_state, i) {
1539 if (conn_state->crtc == set->crtc) {
1540 ret = drm_atomic_set_crtc_for_connector(conn_state,
1546 for (j = 0; j < set->num_connectors; j++) {
1547 if (set->connectors[j] == connector) {
1548 ret = drm_atomic_set_crtc_for_connector(conn_state,
1557 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1558 /* Don't update ->enable for the CRTC in the set_config request,
1559 * since a mismatch would indicate a bug in the upper layers.
1560 * The actual modeset code later on will catch any
1561 * inconsistencies here. */
1562 if (crtc == set->crtc)
1565 if (!drm_atomic_connectors_for_crtc(state, crtc)) {
1566 ret = drm_atomic_set_mode_prop_for_crtc(crtc_state,
1571 crtc_state->active = false;
1579 * drm_atomic_helper_set_config - set a new config from userspace
1580 * @set: mode set configuration
1582 * Provides a default crtc set_config handler using the atomic driver interface.
1585 * Returns 0 on success, negative errno numbers on failure.
1587 int drm_atomic_helper_set_config(struct drm_mode_set *set)
1589 struct drm_atomic_state *state;
1590 struct drm_crtc *crtc = set->crtc;
1591 struct drm_crtc_state *crtc_state;
1592 struct drm_plane_state *primary_state;
1595 state = drm_atomic_state_alloc(crtc->dev);
1599 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1601 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1602 if (IS_ERR(crtc_state)) {
1603 ret = PTR_ERR(crtc_state);
1607 primary_state = drm_atomic_get_plane_state(state, crtc->primary);
1608 if (IS_ERR(primary_state)) {
1609 ret = PTR_ERR(primary_state);
1615 WARN_ON(set->num_connectors);
1617 ret = drm_atomic_set_mode_for_crtc(crtc_state, NULL);
1621 crtc_state->active = false;
1623 ret = drm_atomic_set_crtc_for_plane(primary_state, NULL);
1627 drm_atomic_set_fb_for_plane(primary_state, NULL);
1633 WARN_ON(!set->num_connectors);
1635 ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
1639 crtc_state->active = true;
1641 ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
1644 drm_atomic_set_fb_for_plane(primary_state, set->fb);
1645 primary_state->crtc_x = 0;
1646 primary_state->crtc_y = 0;
1647 primary_state->crtc_h = set->mode->vdisplay;
1648 primary_state->crtc_w = set->mode->hdisplay;
1649 primary_state->src_x = set->x << 16;
1650 primary_state->src_y = set->y << 16;
1651 primary_state->src_h = set->mode->vdisplay << 16;
1652 primary_state->src_w = set->mode->hdisplay << 16;
1655 ret = update_output_state(state, set);
1659 ret = drm_atomic_commit(state);
1663 /* Driver takes ownership of state on successful commit. */
1666 if (ret == -EDEADLK)
1669 drm_atomic_state_free(state);
1673 drm_atomic_state_clear(state);
1674 drm_atomic_legacy_backoff(state);
1677 * Someone might have exchanged the framebuffer while we dropped locks
1678 * in the backoff code. We need to fix up the fb refcount tracking the
1681 crtc->primary->old_fb = crtc->primary->fb;
1685 EXPORT_SYMBOL(drm_atomic_helper_set_config);
1688 * drm_atomic_helper_crtc_set_property - helper for crtc properties
1690 * @property: DRM property
1691 * @val: value of property
1693 * Provides a default crtc set_property handler using the atomic driver
1697 * Zero on success, error code on failure
1700 drm_atomic_helper_crtc_set_property(struct drm_crtc *crtc,
1701 struct drm_property *property,
1704 struct drm_atomic_state *state;
1705 struct drm_crtc_state *crtc_state;
1708 state = drm_atomic_state_alloc(crtc->dev);
1712 /* ->set_property is always called with all locks held. */
1713 state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
1715 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1716 if (IS_ERR(crtc_state)) {
1717 ret = PTR_ERR(crtc_state);
1721 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
1726 ret = drm_atomic_commit(state);
1730 /* Driver takes ownership of state on successful commit. */
1733 if (ret == -EDEADLK)
1736 drm_atomic_state_free(state);
1740 drm_atomic_state_clear(state);
1741 drm_atomic_legacy_backoff(state);
1745 EXPORT_SYMBOL(drm_atomic_helper_crtc_set_property);
1748 * drm_atomic_helper_plane_set_property - helper for plane properties
1750 * @property: DRM property
1751 * @val: value of property
1753 * Provides a default plane set_property handler using the atomic driver
1757 * Zero on success, error code on failure
1760 drm_atomic_helper_plane_set_property(struct drm_plane *plane,
1761 struct drm_property *property,
1764 struct drm_atomic_state *state;
1765 struct drm_plane_state *plane_state;
1768 state = drm_atomic_state_alloc(plane->dev);
1772 /* ->set_property is always called with all locks held. */
1773 state->acquire_ctx = plane->dev->mode_config.acquire_ctx;
1775 plane_state = drm_atomic_get_plane_state(state, plane);
1776 if (IS_ERR(plane_state)) {
1777 ret = PTR_ERR(plane_state);
1781 ret = drm_atomic_plane_set_property(plane, plane_state,
1786 ret = drm_atomic_commit(state);
1790 /* Driver takes ownership of state on successful commit. */
1793 if (ret == -EDEADLK)
1796 drm_atomic_state_free(state);
1800 drm_atomic_state_clear(state);
1801 drm_atomic_legacy_backoff(state);
1805 EXPORT_SYMBOL(drm_atomic_helper_plane_set_property);
1808 * drm_atomic_helper_connector_set_property - helper for connector properties
1809 * @connector: DRM connector
1810 * @property: DRM property
1811 * @val: value of property
1813 * Provides a default connector set_property handler using the atomic driver
1817 * Zero on success, error code on failure
1820 drm_atomic_helper_connector_set_property(struct drm_connector *connector,
1821 struct drm_property *property,
1824 struct drm_atomic_state *state;
1825 struct drm_connector_state *connector_state;
1828 state = drm_atomic_state_alloc(connector->dev);
1832 /* ->set_property is always called with all locks held. */
1833 state->acquire_ctx = connector->dev->mode_config.acquire_ctx;
1835 connector_state = drm_atomic_get_connector_state(state, connector);
1836 if (IS_ERR(connector_state)) {
1837 ret = PTR_ERR(connector_state);
1841 ret = drm_atomic_connector_set_property(connector, connector_state,
1846 ret = drm_atomic_commit(state);
1850 /* Driver takes ownership of state on successful commit. */
1853 if (ret == -EDEADLK)
1856 drm_atomic_state_free(state);
1860 drm_atomic_state_clear(state);
1861 drm_atomic_legacy_backoff(state);
1865 EXPORT_SYMBOL(drm_atomic_helper_connector_set_property);
1868 * drm_atomic_helper_page_flip - execute a legacy page flip
1870 * @fb: DRM framebuffer
1871 * @event: optional DRM event to signal upon completion
1872 * @flags: flip flags for non-vblank sync'ed updates
1874 * Provides a default page flip implementation using the atomic driver interface.
1876 * Note that for now so called async page flips (i.e. updates which are not
1877 * synchronized to vblank) are not supported, since the atomic interfaces have
1878 * no provisions for this yet.
1881 * Returns 0 on success, negative errno numbers on failure.
1883 int drm_atomic_helper_page_flip(struct drm_crtc *crtc,
1884 struct drm_framebuffer *fb,
1885 struct drm_pending_vblank_event *event,
1888 struct drm_plane *plane = crtc->primary;
1889 struct drm_atomic_state *state;
1890 struct drm_plane_state *plane_state;
1891 struct drm_crtc_state *crtc_state;
1894 if (flags & DRM_MODE_PAGE_FLIP_ASYNC)
1897 state = drm_atomic_state_alloc(plane->dev);
1901 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1903 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1904 if (IS_ERR(crtc_state)) {
1905 ret = PTR_ERR(crtc_state);
1908 crtc_state->event = event;
1910 plane_state = drm_atomic_get_plane_state(state, plane);
1911 if (IS_ERR(plane_state)) {
1912 ret = PTR_ERR(plane_state);
1916 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
1919 drm_atomic_set_fb_for_plane(plane_state, fb);
1921 ret = drm_atomic_async_commit(state);
1925 /* TODO: ->page_flip is the only driver callback where the core
1926 * doesn't update plane->fb. For now patch it up here. */
1927 plane->fb = plane->state->fb;
1929 /* Driver takes ownership of state on successful async commit. */
1932 if (ret == -EDEADLK)
1935 drm_atomic_state_free(state);
1939 drm_atomic_state_clear(state);
1940 drm_atomic_legacy_backoff(state);
1943 * Someone might have exchanged the framebuffer while we dropped locks
1944 * in the backoff code. We need to fix up the fb refcount tracking the
1947 plane->old_fb = plane->fb;
1951 EXPORT_SYMBOL(drm_atomic_helper_page_flip);
1954 * drm_atomic_helper_connector_dpms() - connector dpms helper implementation
1955 * @connector: affected connector
1958 * This is the main helper function provided by the atomic helper framework for
1959 * implementing the legacy DPMS connector interface. It computes the new desired
1960 * ->active state for the corresponding CRTC (if the connector is enabled) and
1963 void drm_atomic_helper_connector_dpms(struct drm_connector *connector,
1966 struct drm_mode_config *config = &connector->dev->mode_config;
1967 struct drm_atomic_state *state;
1968 struct drm_crtc_state *crtc_state;
1969 struct drm_crtc *crtc;
1970 struct drm_connector *tmp_connector;
1972 bool active = false;
1974 if (mode != DRM_MODE_DPMS_ON)
1975 mode = DRM_MODE_DPMS_OFF;
1977 connector->dpms = mode;
1978 crtc = connector->state->crtc;
1983 /* FIXME: ->dpms has no return value so can't forward the -ENOMEM. */
1984 state = drm_atomic_state_alloc(connector->dev);
1988 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1990 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1991 if (IS_ERR(crtc_state))
1994 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
1996 list_for_each_entry(tmp_connector, &config->connector_list, head) {
1997 if (tmp_connector->state->crtc != crtc)
2000 if (tmp_connector->dpms == DRM_MODE_DPMS_ON) {
2005 crtc_state->active = active;
2007 ret = drm_atomic_commit(state);
2011 /* Driver takes ownership of state on successful async commit. */
2014 if (ret == -EDEADLK)
2017 drm_atomic_state_free(state);
2019 WARN(1, "Driver bug: Changing ->active failed with ret=%i\n", ret);
2023 drm_atomic_state_clear(state);
2024 drm_atomic_legacy_backoff(state);
2028 EXPORT_SYMBOL(drm_atomic_helper_connector_dpms);
2031 * DOC: atomic state reset and initialization
2033 * Both the drm core and the atomic helpers assume that there is always the full
2034 * and correct atomic software state for all connectors, CRTCs and planes
2035 * available. Which is a bit a problem on driver load and also after system
2036 * suspend. One way to solve this is to have a hardware state read-out
2037 * infrastructure which reconstructs the full software state (e.g. the i915
2040 * The simpler solution is to just reset the software state to everything off,
2041 * which is easiest to do by calling drm_mode_config_reset(). To facilitate this
2042 * the atomic helpers provide default reset implementations for all hooks.
2046 * drm_atomic_helper_crtc_reset - default ->reset hook for CRTCs
2049 * Resets the atomic state for @crtc by freeing the state pointer (which might
2050 * be NULL, e.g. at driver load time) and allocating a new empty state object.
2052 void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc)
2054 if (crtc->state && crtc->state->mode_blob)
2055 drm_property_unreference_blob(crtc->state->mode_blob);
2057 crtc->state = kzalloc(sizeof(*crtc->state), GFP_KERNEL);
2060 crtc->state->crtc = crtc;
2062 EXPORT_SYMBOL(drm_atomic_helper_crtc_reset);
2065 * __drm_atomic_helper_crtc_duplicate_state - copy atomic CRTC state
2066 * @crtc: CRTC object
2067 * @state: atomic CRTC state
2069 * Copies atomic state from a CRTC's current state and resets inferred values.
2070 * This is useful for drivers that subclass the CRTC state.
2072 void __drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc,
2073 struct drm_crtc_state *state)
2075 memcpy(state, crtc->state, sizeof(*state));
2077 if (state->mode_blob)
2078 drm_property_reference_blob(state->mode_blob);
2079 state->mode_changed = false;
2080 state->active_changed = false;
2081 state->planes_changed = false;
2082 state->event = NULL;
2084 EXPORT_SYMBOL(__drm_atomic_helper_crtc_duplicate_state);
2087 * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
2090 * Default CRTC state duplicate hook for drivers which don't have their own
2091 * subclassed CRTC state structure.
2093 struct drm_crtc_state *
2094 drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc)
2096 struct drm_crtc_state *state;
2098 if (WARN_ON(!crtc->state))
2101 state = kmalloc(sizeof(*state), GFP_KERNEL);
2103 __drm_atomic_helper_crtc_duplicate_state(crtc, state);
2107 EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state);
2110 * __drm_atomic_helper_crtc_destroy_state - release CRTC state
2111 * @crtc: CRTC object
2112 * @state: CRTC state object to release
2114 * Releases all resources stored in the CRTC state without actually freeing
2115 * the memory of the CRTC state. This is useful for drivers that subclass the
2118 void __drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
2119 struct drm_crtc_state *state)
2121 if (state->mode_blob)
2122 drm_property_unreference_blob(state->mode_blob);
2124 EXPORT_SYMBOL(__drm_atomic_helper_crtc_destroy_state);
2127 * drm_atomic_helper_crtc_destroy_state - default state destroy hook
2129 * @state: CRTC state object to release
2131 * Default CRTC state destroy hook for drivers which don't have their own
2132 * subclassed CRTC state structure.
2134 void drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
2135 struct drm_crtc_state *state)
2137 __drm_atomic_helper_crtc_destroy_state(crtc, state);
2140 EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state);
2143 * drm_atomic_helper_plane_reset - default ->reset hook for planes
2146 * Resets the atomic state for @plane by freeing the state pointer (which might
2147 * be NULL, e.g. at driver load time) and allocating a new empty state object.
2149 void drm_atomic_helper_plane_reset(struct drm_plane *plane)
2151 if (plane->state && plane->state->fb)
2152 drm_framebuffer_unreference(plane->state->fb);
2154 kfree(plane->state);
2155 plane->state = kzalloc(sizeof(*plane->state), GFP_KERNEL);
2158 plane->state->plane = plane;
2160 EXPORT_SYMBOL(drm_atomic_helper_plane_reset);
2163 * __drm_atomic_helper_plane_duplicate_state - copy atomic plane state
2164 * @plane: plane object
2165 * @state: atomic plane state
2167 * Copies atomic state from a plane's current state. This is useful for
2168 * drivers that subclass the plane state.
2170 void __drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane,
2171 struct drm_plane_state *state)
2173 memcpy(state, plane->state, sizeof(*state));
2176 drm_framebuffer_reference(state->fb);
2178 EXPORT_SYMBOL(__drm_atomic_helper_plane_duplicate_state);
2181 * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
2184 * Default plane state duplicate hook for drivers which don't have their own
2185 * subclassed plane state structure.
2187 struct drm_plane_state *
2188 drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane)
2190 struct drm_plane_state *state;
2192 if (WARN_ON(!plane->state))
2195 state = kmalloc(sizeof(*state), GFP_KERNEL);
2197 __drm_atomic_helper_plane_duplicate_state(plane, state);
2201 EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state);
2204 * __drm_atomic_helper_plane_destroy_state - release plane state
2205 * @plane: plane object
2206 * @state: plane state object to release
2208 * Releases all resources stored in the plane state without actually freeing
2209 * the memory of the plane state. This is useful for drivers that subclass the
2212 void __drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
2213 struct drm_plane_state *state)
2216 drm_framebuffer_unreference(state->fb);
2218 EXPORT_SYMBOL(__drm_atomic_helper_plane_destroy_state);
2221 * drm_atomic_helper_plane_destroy_state - default state destroy hook
2223 * @state: plane state object to release
2225 * Default plane state destroy hook for drivers which don't have their own
2226 * subclassed plane state structure.
2228 void drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
2229 struct drm_plane_state *state)
2231 __drm_atomic_helper_plane_destroy_state(plane, state);
2234 EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state);
2237 * drm_atomic_helper_connector_reset - default ->reset hook for connectors
2238 * @connector: drm connector
2240 * Resets the atomic state for @connector by freeing the state pointer (which
2241 * might be NULL, e.g. at driver load time) and allocating a new empty state
2244 void drm_atomic_helper_connector_reset(struct drm_connector *connector)
2246 kfree(connector->state);
2247 connector->state = kzalloc(sizeof(*connector->state), GFP_KERNEL);
2249 if (connector->state)
2250 connector->state->connector = connector;
2252 EXPORT_SYMBOL(drm_atomic_helper_connector_reset);
2255 * __drm_atomic_helper_connector_duplicate_state - copy atomic connector state
2256 * @connector: connector object
2257 * @state: atomic connector state
2259 * Copies atomic state from a connector's current state. This is useful for
2260 * drivers that subclass the connector state.
2263 __drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector,
2264 struct drm_connector_state *state)
2266 memcpy(state, connector->state, sizeof(*state));
2268 EXPORT_SYMBOL(__drm_atomic_helper_connector_duplicate_state);
2271 * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
2272 * @connector: drm connector
2274 * Default connector state duplicate hook for drivers which don't have their own
2275 * subclassed connector state structure.
2277 struct drm_connector_state *
2278 drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
2280 struct drm_connector_state *state;
2282 if (WARN_ON(!connector->state))
2285 state = kmalloc(sizeof(*state), GFP_KERNEL);
2287 __drm_atomic_helper_connector_duplicate_state(connector, state);
2291 EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
2294 * __drm_atomic_helper_connector_destroy_state - release connector state
2295 * @connector: connector object
2296 * @state: connector state object to release
2298 * Releases all resources stored in the connector state without actually
2299 * freeing the memory of the connector state. This is useful for drivers that
2300 * subclass the connector state.
2303 __drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
2304 struct drm_connector_state *state)
2307 * This is currently a placeholder so that drivers that subclass the
2308 * state will automatically do the right thing if code is ever added
2312 EXPORT_SYMBOL(__drm_atomic_helper_connector_destroy_state);
2315 * drm_atomic_helper_connector_destroy_state - default state destroy hook
2316 * @connector: drm connector
2317 * @state: connector state object to release
2319 * Default connector state destroy hook for drivers which don't have their own
2320 * subclassed connector state structure.
2322 void drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
2323 struct drm_connector_state *state)
2325 __drm_atomic_helper_connector_destroy_state(connector, state);
2328 EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state);