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.
56 * The atomic helper uses the same function table structures as all other
57 * modesetting helpers. See the documentation for struct &drm_crtc_helper_funcs,
58 * struct &drm_encoder_helper_funcs and struct &drm_connector_helper_funcs. It
59 * also shares the struct &drm_plane_helper_funcs function table with the plane
63 drm_atomic_helper_plane_changed(struct drm_atomic_state *state,
64 struct drm_plane_state *plane_state,
65 struct drm_plane *plane)
67 struct drm_crtc_state *crtc_state;
69 if (plane->state->crtc) {
70 crtc_state = state->crtc_states[drm_crtc_index(plane->state->crtc)];
72 if (WARN_ON(!crtc_state))
75 crtc_state->planes_changed = true;
78 if (plane_state->crtc) {
80 state->crtc_states[drm_crtc_index(plane_state->crtc)];
82 if (WARN_ON(!crtc_state))
85 crtc_state->planes_changed = true;
90 check_pending_encoder_assignment(struct drm_atomic_state *state,
91 struct drm_encoder *new_encoder,
92 struct drm_connector *new_connector)
94 struct drm_connector *connector;
95 struct drm_connector_state *conn_state;
98 for_each_connector_in_state(state, connector, conn_state, i) {
99 if (conn_state->best_encoder != new_encoder)
102 /* encoder already assigned and we're trying to re-steal it! */
103 if (connector->state->best_encoder != conn_state->best_encoder)
110 static struct drm_crtc *
111 get_current_crtc_for_encoder(struct drm_device *dev,
112 struct drm_encoder *encoder)
114 struct drm_mode_config *config = &dev->mode_config;
115 struct drm_connector *connector;
117 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
119 drm_for_each_connector(connector, dev) {
120 if (connector->state->best_encoder != encoder)
123 return connector->state->crtc;
130 steal_encoder(struct drm_atomic_state *state,
131 struct drm_encoder *encoder,
132 struct drm_crtc *encoder_crtc)
134 struct drm_mode_config *config = &state->dev->mode_config;
135 struct drm_crtc_state *crtc_state;
136 struct drm_connector *connector;
137 struct drm_connector_state *connector_state;
141 * We can only steal an encoder coming from a connector, which means we
142 * must already hold the connection_mutex.
144 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
146 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s], stealing it\n",
147 encoder->base.id, encoder->name,
148 encoder_crtc->base.id, encoder_crtc->name);
150 crtc_state = drm_atomic_get_crtc_state(state, encoder_crtc);
151 if (IS_ERR(crtc_state))
152 return PTR_ERR(crtc_state);
154 crtc_state->connectors_changed = true;
156 list_for_each_entry(connector, &config->connector_list, head) {
157 if (connector->state->best_encoder != encoder)
160 DRM_DEBUG_ATOMIC("Stealing encoder from [CONNECTOR:%d:%s]\n",
164 connector_state = drm_atomic_get_connector_state(state,
166 if (IS_ERR(connector_state))
167 return PTR_ERR(connector_state);
169 ret = drm_atomic_set_crtc_for_connector(connector_state, NULL);
172 connector_state->best_encoder = NULL;
179 update_connector_routing(struct drm_atomic_state *state, int conn_idx)
181 const struct drm_connector_helper_funcs *funcs;
182 struct drm_encoder *new_encoder;
183 struct drm_crtc *encoder_crtc;
184 struct drm_connector *connector;
185 struct drm_connector_state *connector_state;
186 struct drm_crtc_state *crtc_state;
189 connector = state->connectors[conn_idx];
190 connector_state = state->connector_states[conn_idx];
195 DRM_DEBUG_ATOMIC("Updating routing for [CONNECTOR:%d:%s]\n",
199 if (connector->state->crtc != connector_state->crtc) {
200 if (connector->state->crtc) {
201 idx = drm_crtc_index(connector->state->crtc);
203 crtc_state = state->crtc_states[idx];
204 crtc_state->connectors_changed = true;
207 if (connector_state->crtc) {
208 idx = drm_crtc_index(connector_state->crtc);
210 crtc_state = state->crtc_states[idx];
211 crtc_state->connectors_changed = true;
215 if (!connector_state->crtc) {
216 DRM_DEBUG_ATOMIC("Disabling [CONNECTOR:%d:%s]\n",
220 connector_state->best_encoder = NULL;
225 funcs = connector->helper_private;
227 if (funcs->atomic_best_encoder)
228 new_encoder = funcs->atomic_best_encoder(connector,
231 new_encoder = funcs->best_encoder(connector);
234 DRM_DEBUG_ATOMIC("No suitable encoder found for [CONNECTOR:%d:%s]\n",
240 if (!drm_encoder_crtc_ok(new_encoder, connector_state->crtc)) {
241 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] incompatible with [CRTC:%d]\n",
242 new_encoder->base.id,
244 connector_state->crtc->base.id);
248 if (new_encoder == connector_state->best_encoder) {
249 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] keeps [ENCODER:%d:%s], now on [CRTC:%d:%s]\n",
252 new_encoder->base.id,
254 connector_state->crtc->base.id,
255 connector_state->crtc->name);
260 if (!check_pending_encoder_assignment(state, new_encoder, connector)) {
261 DRM_DEBUG_ATOMIC("Encoder for [CONNECTOR:%d:%s] already assigned\n",
267 encoder_crtc = get_current_crtc_for_encoder(state->dev,
271 ret = steal_encoder(state, new_encoder, encoder_crtc);
273 DRM_DEBUG_ATOMIC("Encoder stealing failed for [CONNECTOR:%d:%s]\n",
280 if (WARN_ON(!connector_state->crtc))
283 connector_state->best_encoder = new_encoder;
284 idx = drm_crtc_index(connector_state->crtc);
286 crtc_state = state->crtc_states[idx];
287 crtc_state->connectors_changed = true;
289 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] using [ENCODER:%d:%s] on [CRTC:%d:%s]\n",
292 new_encoder->base.id,
294 connector_state->crtc->base.id,
295 connector_state->crtc->name);
301 mode_fixup(struct drm_atomic_state *state)
303 struct drm_crtc *crtc;
304 struct drm_crtc_state *crtc_state;
305 struct drm_connector *connector;
306 struct drm_connector_state *conn_state;
310 for_each_crtc_in_state(state, crtc, crtc_state, i) {
311 if (!crtc_state->mode_changed &&
312 !crtc_state->connectors_changed)
315 drm_mode_copy(&crtc_state->adjusted_mode, &crtc_state->mode);
318 for_each_connector_in_state(state, connector, conn_state, i) {
319 const struct drm_encoder_helper_funcs *funcs;
320 struct drm_encoder *encoder;
322 WARN_ON(!!conn_state->best_encoder != !!conn_state->crtc);
324 if (!conn_state->crtc || !conn_state->best_encoder)
328 state->crtc_states[drm_crtc_index(conn_state->crtc)];
331 * Each encoder has at most one connector (since we always steal
332 * it away), so we won't call ->mode_fixup twice.
334 encoder = conn_state->best_encoder;
335 funcs = encoder->helper_private;
339 ret = drm_bridge_mode_fixup(encoder->bridge, &crtc_state->mode,
340 &crtc_state->adjusted_mode);
342 DRM_DEBUG_ATOMIC("Bridge fixup failed\n");
346 if (funcs->atomic_check) {
347 ret = funcs->atomic_check(encoder, crtc_state,
350 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] check failed\n",
351 encoder->base.id, encoder->name);
354 } else if (funcs->mode_fixup) {
355 ret = funcs->mode_fixup(encoder, &crtc_state->mode,
356 &crtc_state->adjusted_mode);
358 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] fixup failed\n",
359 encoder->base.id, encoder->name);
365 for_each_crtc_in_state(state, crtc, crtc_state, i) {
366 const struct drm_crtc_helper_funcs *funcs;
368 if (!crtc_state->mode_changed &&
369 !crtc_state->connectors_changed)
372 funcs = crtc->helper_private;
373 if (!funcs->mode_fixup)
376 ret = funcs->mode_fixup(crtc, &crtc_state->mode,
377 &crtc_state->adjusted_mode);
379 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] fixup failed\n",
380 crtc->base.id, crtc->name);
389 * drm_atomic_helper_check_modeset - validate state object for modeset changes
391 * @state: the driver state object
393 * Check the state object to see if the requested state is physically possible.
394 * This does all the crtc and connector related computations for an atomic
395 * update and adds any additional connectors needed for full modesets and calls
396 * down into ->mode_fixup functions of the driver backend.
398 * crtc_state->mode_changed is set when the input mode is changed.
399 * crtc_state->connectors_changed is set when a connector is added or
400 * removed from the crtc.
401 * crtc_state->active_changed is set when crtc_state->active changes,
402 * which is used for dpms.
406 * Drivers which update ->mode_changed (e.g. in their ->atomic_check hooks if a
407 * plane update can't be done without a full modeset) _must_ call this function
408 * afterwards after that change. It is permitted to call this function multiple
409 * times for the same update, e.g. when the ->atomic_check functions depend upon
410 * the adjusted dotclock for fifo space allocation and watermark computation.
413 * Zero for success or -errno
416 drm_atomic_helper_check_modeset(struct drm_device *dev,
417 struct drm_atomic_state *state)
419 struct drm_crtc *crtc;
420 struct drm_crtc_state *crtc_state;
421 struct drm_connector *connector;
422 struct drm_connector_state *connector_state;
425 for_each_crtc_in_state(state, crtc, crtc_state, i) {
426 if (!drm_mode_equal(&crtc->state->mode, &crtc_state->mode)) {
427 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] mode changed\n",
428 crtc->base.id, crtc->name);
429 crtc_state->mode_changed = true;
432 if (crtc->state->enable != crtc_state->enable) {
433 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enable changed\n",
434 crtc->base.id, crtc->name);
437 * For clarity this assignment is done here, but
438 * enable == 0 is only true when there are no
439 * connectors and a NULL mode.
441 * The other way around is true as well. enable != 0
442 * iff connectors are attached and a mode is set.
444 crtc_state->mode_changed = true;
445 crtc_state->connectors_changed = true;
449 for_each_connector_in_state(state, connector, connector_state, i) {
451 * This only sets crtc->mode_changed for routing changes,
452 * drivers must set crtc->mode_changed themselves when connector
453 * properties need to be updated.
455 ret = update_connector_routing(state, i);
461 * After all the routing has been prepared we need to add in any
462 * connector which is itself unchanged, but who's crtc changes it's
463 * configuration. This must be done before calling mode_fixup in case a
464 * crtc only changed its mode but has the same set of connectors.
466 for_each_crtc_in_state(state, crtc, crtc_state, i) {
470 * We must set ->active_changed after walking connectors for
471 * otherwise an update that only changes active would result in
472 * a full modeset because update_connector_routing force that.
474 if (crtc->state->active != crtc_state->active) {
475 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active changed\n",
476 crtc->base.id, crtc->name);
477 crtc_state->active_changed = true;
480 if (!drm_atomic_crtc_needs_modeset(crtc_state))
483 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] needs all connectors, enable: %c, active: %c\n",
484 crtc->base.id, crtc->name,
485 crtc_state->enable ? 'y' : 'n',
486 crtc_state->active ? 'y' : 'n');
488 ret = drm_atomic_add_affected_connectors(state, crtc);
492 ret = drm_atomic_add_affected_planes(state, crtc);
496 num_connectors = drm_atomic_connectors_for_crtc(state,
499 if (crtc_state->enable != !!num_connectors) {
500 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled/connectors mismatch\n",
501 crtc->base.id, crtc->name);
507 return mode_fixup(state);
509 EXPORT_SYMBOL(drm_atomic_helper_check_modeset);
512 * drm_atomic_helper_check_planes - validate state object for planes changes
514 * @state: the driver state object
516 * Check the state object to see if the requested state is physically possible.
517 * This does all the plane update related checks using by calling into the
518 * ->atomic_check hooks provided by the driver.
520 * It also sets crtc_state->planes_changed to indicate that a crtc has
524 * Zero for success or -errno
527 drm_atomic_helper_check_planes(struct drm_device *dev,
528 struct drm_atomic_state *state)
530 struct drm_crtc *crtc;
531 struct drm_crtc_state *crtc_state;
532 struct drm_plane *plane;
533 struct drm_plane_state *plane_state;
536 for_each_plane_in_state(state, plane, plane_state, i) {
537 const struct drm_plane_helper_funcs *funcs;
539 funcs = plane->helper_private;
541 drm_atomic_helper_plane_changed(state, plane_state, plane);
543 if (!funcs || !funcs->atomic_check)
546 ret = funcs->atomic_check(plane, plane_state);
548 DRM_DEBUG_ATOMIC("[PLANE:%d] atomic driver check failed\n",
554 for_each_crtc_in_state(state, crtc, crtc_state, i) {
555 const struct drm_crtc_helper_funcs *funcs;
557 funcs = crtc->helper_private;
559 if (!funcs || !funcs->atomic_check)
562 ret = funcs->atomic_check(crtc, state->crtc_states[i]);
564 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic driver check failed\n",
565 crtc->base.id, crtc->name);
572 EXPORT_SYMBOL(drm_atomic_helper_check_planes);
575 * drm_atomic_helper_check - validate state object
577 * @state: the driver state object
579 * Check the state object to see if the requested state is physically possible.
580 * Only crtcs and planes have check callbacks, so for any additional (global)
581 * checking that a driver needs it can simply wrap that around this function.
582 * Drivers without such needs can directly use this as their ->atomic_check()
585 * This just wraps the two parts of the state checking for planes and modeset
586 * state in the default order: First it calls drm_atomic_helper_check_modeset()
587 * and then drm_atomic_helper_check_planes(). The assumption is that the
588 * ->atomic_check functions depend upon an updated adjusted_mode.clock to
589 * e.g. properly compute watermarks.
592 * Zero for success or -errno
594 int drm_atomic_helper_check(struct drm_device *dev,
595 struct drm_atomic_state *state)
599 ret = drm_atomic_helper_check_modeset(dev, state);
603 ret = drm_atomic_helper_check_planes(dev, state);
609 EXPORT_SYMBOL(drm_atomic_helper_check);
612 disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state)
614 struct drm_connector *connector;
615 struct drm_connector_state *old_conn_state;
616 struct drm_crtc *crtc;
617 struct drm_crtc_state *old_crtc_state;
620 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
621 const struct drm_encoder_helper_funcs *funcs;
622 struct drm_encoder *encoder;
623 struct drm_crtc_state *old_crtc_state;
625 /* Shut down everything that's in the changeset and currently
626 * still on. So need to check the old, saved state. */
627 if (!old_conn_state->crtc)
630 old_crtc_state = old_state->crtc_states[drm_crtc_index(old_conn_state->crtc)];
632 if (!old_crtc_state->active ||
633 !drm_atomic_crtc_needs_modeset(old_conn_state->crtc->state))
636 encoder = old_conn_state->best_encoder;
638 /* We shouldn't get this far if we didn't previously have
639 * an encoder.. but WARN_ON() rather than explode.
641 if (WARN_ON(!encoder))
644 funcs = encoder->helper_private;
646 DRM_DEBUG_ATOMIC("disabling [ENCODER:%d:%s]\n",
647 encoder->base.id, encoder->name);
650 * Each encoder has at most one connector (since we always steal
651 * it away), so we won't call disable hooks twice.
653 drm_bridge_disable(encoder->bridge);
655 /* Right function depends upon target state. */
656 if (connector->state->crtc && funcs->prepare)
657 funcs->prepare(encoder);
658 else if (funcs->disable)
659 funcs->disable(encoder);
661 funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
663 drm_bridge_post_disable(encoder->bridge);
666 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
667 const struct drm_crtc_helper_funcs *funcs;
669 /* Shut down everything that needs a full modeset. */
670 if (!drm_atomic_crtc_needs_modeset(crtc->state))
673 if (!old_crtc_state->active)
676 funcs = crtc->helper_private;
678 DRM_DEBUG_ATOMIC("disabling [CRTC:%d:%s]\n",
679 crtc->base.id, crtc->name);
682 /* Right function depends upon target state. */
683 if (crtc->state->enable && funcs->prepare)
684 funcs->prepare(crtc);
685 else if (funcs->disable)
686 funcs->disable(crtc);
688 funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
693 * drm_atomic_helper_update_legacy_modeset_state - update legacy modeset state
695 * @old_state: atomic state object with old state structures
697 * This function updates all the various legacy modeset state pointers in
698 * connectors, encoders and crtcs. It also updates the timestamping constants
699 * used for precise vblank timestamps by calling
700 * drm_calc_timestamping_constants().
702 * Drivers can use this for building their own atomic commit if they don't have
703 * a pure helper-based modeset implementation.
706 drm_atomic_helper_update_legacy_modeset_state(struct drm_device *dev,
707 struct drm_atomic_state *old_state)
709 struct drm_connector *connector;
710 struct drm_connector_state *old_conn_state;
711 struct drm_crtc *crtc;
712 struct drm_crtc_state *old_crtc_state;
715 /* clear out existing links and update dpms */
716 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
717 if (connector->encoder) {
718 WARN_ON(!connector->encoder->crtc);
720 connector->encoder->crtc = NULL;
721 connector->encoder = NULL;
724 crtc = connector->state->crtc;
725 if ((!crtc && old_conn_state->crtc) ||
726 (crtc && drm_atomic_crtc_needs_modeset(crtc->state))) {
727 struct drm_property *dpms_prop =
728 dev->mode_config.dpms_property;
729 int mode = DRM_MODE_DPMS_OFF;
731 if (crtc && crtc->state->active)
732 mode = DRM_MODE_DPMS_ON;
734 connector->dpms = mode;
735 drm_object_property_set_value(&connector->base,
741 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
742 if (!connector->state->crtc)
745 if (WARN_ON(!connector->state->best_encoder))
748 connector->encoder = connector->state->best_encoder;
749 connector->encoder->crtc = connector->state->crtc;
752 /* set legacy state in the crtc structure */
753 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
754 struct drm_plane *primary = crtc->primary;
756 crtc->mode = crtc->state->mode;
757 crtc->enabled = crtc->state->enable;
759 if (drm_atomic_get_existing_plane_state(old_state, primary) &&
760 primary->state->crtc == crtc) {
761 crtc->x = primary->state->src_x >> 16;
762 crtc->y = primary->state->src_y >> 16;
765 if (crtc->state->enable)
766 drm_calc_timestamping_constants(crtc,
767 &crtc->state->adjusted_mode);
770 EXPORT_SYMBOL(drm_atomic_helper_update_legacy_modeset_state);
773 crtc_set_mode(struct drm_device *dev, struct drm_atomic_state *old_state)
775 struct drm_crtc *crtc;
776 struct drm_crtc_state *old_crtc_state;
777 struct drm_connector *connector;
778 struct drm_connector_state *old_conn_state;
781 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
782 const struct drm_crtc_helper_funcs *funcs;
784 if (!crtc->state->mode_changed)
787 funcs = crtc->helper_private;
789 if (crtc->state->enable && funcs->mode_set_nofb) {
790 DRM_DEBUG_ATOMIC("modeset on [CRTC:%d:%s]\n",
791 crtc->base.id, crtc->name);
793 funcs->mode_set_nofb(crtc);
797 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
798 const struct drm_encoder_helper_funcs *funcs;
799 struct drm_crtc_state *new_crtc_state;
800 struct drm_encoder *encoder;
801 struct drm_display_mode *mode, *adjusted_mode;
803 if (!connector->state->best_encoder)
806 encoder = connector->state->best_encoder;
807 funcs = encoder->helper_private;
808 new_crtc_state = connector->state->crtc->state;
809 mode = &new_crtc_state->mode;
810 adjusted_mode = &new_crtc_state->adjusted_mode;
812 if (!new_crtc_state->mode_changed)
815 DRM_DEBUG_ATOMIC("modeset on [ENCODER:%d:%s]\n",
816 encoder->base.id, encoder->name);
819 * Each encoder has at most one connector (since we always steal
820 * it away), so we won't call mode_set hooks twice.
823 funcs->mode_set(encoder, mode, adjusted_mode);
825 drm_bridge_mode_set(encoder->bridge, mode, adjusted_mode);
830 * drm_atomic_helper_commit_modeset_disables - modeset commit to disable outputs
832 * @old_state: atomic state object with old state structures
834 * This function shuts down all the outputs that need to be shut down and
835 * prepares them (if required) with the new mode.
837 * For compatibility with legacy crtc helpers this should be called before
838 * drm_atomic_helper_commit_planes(), which is what the default commit function
839 * does. But drivers with different needs can group the modeset commits together
840 * and do the plane commits at the end. This is useful for drivers doing runtime
841 * PM since planes updates then only happen when the CRTC is actually enabled.
843 void drm_atomic_helper_commit_modeset_disables(struct drm_device *dev,
844 struct drm_atomic_state *old_state)
846 disable_outputs(dev, old_state);
848 drm_atomic_helper_update_legacy_modeset_state(dev, old_state);
850 crtc_set_mode(dev, old_state);
852 EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_disables);
855 * drm_atomic_helper_commit_modeset_enables - modeset commit to enable outputs
857 * @old_state: atomic state object with old state structures
859 * This function enables all the outputs with the new configuration which had to
860 * be turned off for the update.
862 * For compatibility with legacy crtc helpers this should be called after
863 * drm_atomic_helper_commit_planes(), which is what the default commit function
864 * does. But drivers with different needs can group the modeset commits together
865 * and do the plane commits at the end. This is useful for drivers doing runtime
866 * PM since planes updates then only happen when the CRTC is actually enabled.
868 void drm_atomic_helper_commit_modeset_enables(struct drm_device *dev,
869 struct drm_atomic_state *old_state)
871 struct drm_crtc *crtc;
872 struct drm_crtc_state *old_crtc_state;
873 struct drm_connector *connector;
874 struct drm_connector_state *old_conn_state;
877 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
878 const struct drm_crtc_helper_funcs *funcs;
880 /* Need to filter out CRTCs where only planes change. */
881 if (!drm_atomic_crtc_needs_modeset(crtc->state))
884 if (!crtc->state->active)
887 funcs = crtc->helper_private;
889 if (crtc->state->enable) {
890 DRM_DEBUG_ATOMIC("enabling [CRTC:%d:%s]\n",
891 crtc->base.id, crtc->name);
900 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
901 const struct drm_encoder_helper_funcs *funcs;
902 struct drm_encoder *encoder;
904 if (!connector->state->best_encoder)
907 if (!connector->state->crtc->state->active ||
908 !drm_atomic_crtc_needs_modeset(connector->state->crtc->state))
911 encoder = connector->state->best_encoder;
912 funcs = encoder->helper_private;
914 DRM_DEBUG_ATOMIC("enabling [ENCODER:%d:%s]\n",
915 encoder->base.id, encoder->name);
918 * Each encoder has at most one connector (since we always steal
919 * it away), so we won't call enable hooks twice.
921 drm_bridge_pre_enable(encoder->bridge);
924 funcs->enable(encoder);
926 funcs->commit(encoder);
928 drm_bridge_enable(encoder->bridge);
931 EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_enables);
933 static void wait_for_fences(struct drm_device *dev,
934 struct drm_atomic_state *state)
936 struct drm_plane *plane;
937 struct drm_plane_state *plane_state;
940 for_each_plane_in_state(state, plane, plane_state, i) {
941 if (!plane->state->fence)
944 WARN_ON(!plane->state->fb);
946 fence_wait(plane->state->fence, false);
947 fence_put(plane->state->fence);
948 plane->state->fence = NULL;
952 static bool framebuffer_changed(struct drm_device *dev,
953 struct drm_atomic_state *old_state,
954 struct drm_crtc *crtc)
956 struct drm_plane *plane;
957 struct drm_plane_state *old_plane_state;
960 for_each_plane_in_state(old_state, plane, old_plane_state, i) {
961 if (plane->state->crtc != crtc &&
962 old_plane_state->crtc != crtc)
965 if (plane->state->fb != old_plane_state->fb)
973 * drm_atomic_helper_wait_for_vblanks - wait for vblank on crtcs
975 * @old_state: atomic state object with old state structures
977 * Helper to, after atomic commit, wait for vblanks on all effected
978 * crtcs (ie. before cleaning up old framebuffers using
979 * drm_atomic_helper_cleanup_planes()). It will only wait on crtcs where the
980 * framebuffers have actually changed to optimize for the legacy cursor and
981 * plane update use-case.
984 drm_atomic_helper_wait_for_vblanks(struct drm_device *dev,
985 struct drm_atomic_state *old_state)
987 struct drm_crtc *crtc;
988 struct drm_crtc_state *old_crtc_state;
991 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
992 /* No one cares about the old state, so abuse it for tracking
993 * and store whether we hold a vblank reference (and should do a
994 * vblank wait) in the ->enable boolean. */
995 old_crtc_state->enable = false;
997 if (!crtc->state->enable)
1000 /* Legacy cursor ioctls are completely unsynced, and userspace
1001 * relies on that (by doing tons of cursor updates). */
1002 if (old_state->legacy_cursor_update)
1005 if (!framebuffer_changed(dev, old_state, crtc))
1008 ret = drm_crtc_vblank_get(crtc);
1012 old_crtc_state->enable = true;
1013 old_crtc_state->last_vblank_count = drm_crtc_vblank_count(crtc);
1016 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
1017 if (!old_crtc_state->enable)
1020 ret = wait_event_timeout(dev->vblank[i].queue,
1021 old_crtc_state->last_vblank_count !=
1022 drm_crtc_vblank_count(crtc),
1023 msecs_to_jiffies(50));
1025 drm_crtc_vblank_put(crtc);
1028 EXPORT_SYMBOL(drm_atomic_helper_wait_for_vblanks);
1031 * drm_atomic_helper_commit - commit validated state object
1033 * @state: the driver state object
1034 * @async: asynchronous commit
1036 * This function commits a with drm_atomic_helper_check() pre-validated state
1037 * object. This can still fail when e.g. the framebuffer reservation fails. For
1038 * now this doesn't implement asynchronous commits.
1040 * Note that right now this function does not support async commits, and hence
1041 * driver writers must implement their own version for now. Also note that the
1042 * default ordering of how the various stages are called is to match the legacy
1043 * modeset helper library closest. One peculiarity of that is that it doesn't
1044 * mesh well with runtime PM at all.
1046 * For drivers supporting runtime PM the recommended sequence is
1048 * drm_atomic_helper_commit_modeset_disables(dev, state);
1050 * drm_atomic_helper_commit_modeset_enables(dev, state);
1052 * drm_atomic_helper_commit_planes(dev, state, true);
1054 * See the kerneldoc entries for these three functions for more details.
1057 * Zero for success or -errno.
1059 int drm_atomic_helper_commit(struct drm_device *dev,
1060 struct drm_atomic_state *state,
1068 ret = drm_atomic_helper_prepare_planes(dev, state);
1073 * This is the point of no return - everything below never fails except
1074 * when the hw goes bonghits. Which means we can commit the new state on
1075 * the software side now.
1078 drm_atomic_helper_swap_state(dev, state);
1081 * Everything below can be run asynchronously without the need to grab
1082 * any modeset locks at all under one condition: It must be guaranteed
1083 * that the asynchronous work has either been cancelled (if the driver
1084 * supports it, which at least requires that the framebuffers get
1085 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
1086 * before the new state gets committed on the software side with
1087 * drm_atomic_helper_swap_state().
1089 * This scheme allows new atomic state updates to be prepared and
1090 * checked in parallel to the asynchronous completion of the previous
1091 * update. Which is important since compositors need to figure out the
1092 * composition of the next frame right after having submitted the
1096 wait_for_fences(dev, state);
1098 drm_atomic_helper_commit_modeset_disables(dev, state);
1100 drm_atomic_helper_commit_planes(dev, state, false);
1102 drm_atomic_helper_commit_modeset_enables(dev, state);
1104 drm_atomic_helper_wait_for_vblanks(dev, state);
1106 drm_atomic_helper_cleanup_planes(dev, state);
1108 drm_atomic_state_free(state);
1112 EXPORT_SYMBOL(drm_atomic_helper_commit);
1115 * DOC: implementing async commit
1117 * For now the atomic helpers don't support async commit directly. If there is
1118 * real need it could be added though, using the dma-buf fence infrastructure
1119 * for generic synchronization with outstanding rendering.
1121 * For now drivers have to implement async commit themselves, with the following
1122 * sequence being the recommended one:
1124 * 1. Run drm_atomic_helper_prepare_planes() first. This is the only function
1125 * which commit needs to call which can fail, so we want to run it first and
1128 * 2. Synchronize with any outstanding asynchronous commit worker threads which
1129 * might be affected the new state update. This can be done by either cancelling
1130 * or flushing the work items, depending upon whether the driver can deal with
1131 * cancelled updates. Note that it is important to ensure that the framebuffer
1132 * cleanup is still done when cancelling.
1134 * For sufficient parallelism it is recommended to have a work item per crtc
1135 * (for updates which don't touch global state) and a global one. Then we only
1136 * need to synchronize with the crtc work items for changed crtcs and the global
1137 * work item, which allows nice concurrent updates on disjoint sets of crtcs.
1139 * 3. The software state is updated synchronously with
1140 * drm_atomic_helper_swap_state(). Doing this under the protection of all modeset
1141 * locks means concurrent callers never see inconsistent state. And doing this
1142 * while it's guaranteed that no relevant async worker runs means that async
1143 * workers do not need grab any locks. Actually they must not grab locks, for
1144 * otherwise the work flushing will deadlock.
1146 * 4. Schedule a work item to do all subsequent steps, using the split-out
1147 * commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and
1148 * then cleaning up the framebuffers after the old framebuffer is no longer
1153 * drm_atomic_helper_prepare_planes - prepare plane resources before commit
1155 * @state: atomic state object with new state structures
1157 * This function prepares plane state, specifically framebuffers, for the new
1158 * configuration. If any failure is encountered this function will call
1159 * ->cleanup_fb on any already successfully prepared framebuffer.
1162 * 0 on success, negative error code on failure.
1164 int drm_atomic_helper_prepare_planes(struct drm_device *dev,
1165 struct drm_atomic_state *state)
1167 int nplanes = dev->mode_config.num_total_plane;
1170 for (i = 0; i < nplanes; i++) {
1171 const struct drm_plane_helper_funcs *funcs;
1172 struct drm_plane *plane = state->planes[i];
1173 struct drm_plane_state *plane_state = state->plane_states[i];
1178 funcs = plane->helper_private;
1180 if (funcs->prepare_fb) {
1181 ret = funcs->prepare_fb(plane, plane_state);
1190 for (i--; i >= 0; i--) {
1191 const struct drm_plane_helper_funcs *funcs;
1192 struct drm_plane *plane = state->planes[i];
1193 struct drm_plane_state *plane_state = state->plane_states[i];
1198 funcs = plane->helper_private;
1200 if (funcs->cleanup_fb)
1201 funcs->cleanup_fb(plane, plane_state);
1207 EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
1209 bool plane_crtc_active(struct drm_plane_state *state)
1211 return state->crtc && state->crtc->state->active;
1215 * drm_atomic_helper_commit_planes - commit plane state
1217 * @old_state: atomic state object with old state structures
1218 * @active_only: Only commit on active CRTC if set
1220 * This function commits the new plane state using the plane and atomic helper
1221 * functions for planes and crtcs. It assumes that the atomic state has already
1222 * been pushed into the relevant object state pointers, since this step can no
1225 * It still requires the global state object @old_state to know which planes and
1226 * crtcs need to be updated though.
1228 * Note that this function does all plane updates across all CRTCs in one step.
1229 * If the hardware can't support this approach look at
1230 * drm_atomic_helper_commit_planes_on_crtc() instead.
1232 * Plane parameters can be updated by applications while the associated CRTC is
1233 * disabled. The DRM/KMS core will store the parameters in the plane state,
1234 * which will be available to the driver when the CRTC is turned on. As a result
1235 * most drivers don't need to be immediately notified of plane updates for a
1238 * Unless otherwise needed, drivers are advised to set the @active_only
1239 * parameters to true in order not to receive plane update notifications related
1240 * to a disabled CRTC. This avoids the need to manually ignore plane updates in
1241 * driver code when the driver and/or hardware can't or just don't need to deal
1242 * with updates on disabled CRTCs, for example when supporting runtime PM.
1244 * The drm_atomic_helper_commit() default implementation only sets @active_only
1245 * to false to most closely match the behaviour of the legacy helpers. This should
1246 * not be copied blindly by drivers.
1248 void drm_atomic_helper_commit_planes(struct drm_device *dev,
1249 struct drm_atomic_state *old_state,
1252 struct drm_crtc *crtc;
1253 struct drm_crtc_state *old_crtc_state;
1254 struct drm_plane *plane;
1255 struct drm_plane_state *old_plane_state;
1258 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
1259 const struct drm_crtc_helper_funcs *funcs;
1261 funcs = crtc->helper_private;
1263 if (!funcs || !funcs->atomic_begin)
1266 if (active_only && !crtc->state->active)
1269 funcs->atomic_begin(crtc, old_crtc_state);
1272 for_each_plane_in_state(old_state, plane, old_plane_state, i) {
1273 const struct drm_plane_helper_funcs *funcs;
1276 funcs = plane->helper_private;
1281 disabling = drm_atomic_plane_disabling(plane, old_plane_state);
1285 * Skip planes related to inactive CRTCs. If the plane
1286 * is enabled use the state of the current CRTC. If the
1287 * plane is being disabled use the state of the old
1288 * CRTC to avoid skipping planes being disabled on an
1291 if (!disabling && !plane_crtc_active(plane->state))
1293 if (disabling && !plane_crtc_active(old_plane_state))
1298 * Special-case disabling the plane if drivers support it.
1300 if (disabling && funcs->atomic_disable)
1301 funcs->atomic_disable(plane, old_plane_state);
1302 else if (plane->state->crtc || disabling)
1303 funcs->atomic_update(plane, old_plane_state);
1306 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
1307 const struct drm_crtc_helper_funcs *funcs;
1309 funcs = crtc->helper_private;
1311 if (!funcs || !funcs->atomic_flush)
1314 if (active_only && !crtc->state->active)
1317 funcs->atomic_flush(crtc, old_crtc_state);
1320 EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
1323 * drm_atomic_helper_commit_planes_on_crtc - commit plane state for a crtc
1324 * @old_crtc_state: atomic state object with the old crtc state
1326 * This function commits the new plane state using the plane and atomic helper
1327 * functions for planes on the specific crtc. It assumes that the atomic state
1328 * has already been pushed into the relevant object state pointers, since this
1329 * step can no longer fail.
1331 * This function is useful when plane updates should be done crtc-by-crtc
1332 * instead of one global step like drm_atomic_helper_commit_planes() does.
1334 * This function can only be savely used when planes are not allowed to move
1335 * between different CRTCs because this function doesn't handle inter-CRTC
1336 * depencies. Callers need to ensure that either no such depencies exist,
1337 * resolve them through ordering of commit calls or through some other means.
1340 drm_atomic_helper_commit_planes_on_crtc(struct drm_crtc_state *old_crtc_state)
1342 const struct drm_crtc_helper_funcs *crtc_funcs;
1343 struct drm_crtc *crtc = old_crtc_state->crtc;
1344 struct drm_atomic_state *old_state = old_crtc_state->state;
1345 struct drm_plane *plane;
1346 unsigned plane_mask;
1348 plane_mask = old_crtc_state->plane_mask;
1349 plane_mask |= crtc->state->plane_mask;
1351 crtc_funcs = crtc->helper_private;
1352 if (crtc_funcs && crtc_funcs->atomic_begin)
1353 crtc_funcs->atomic_begin(crtc, old_crtc_state);
1355 drm_for_each_plane_mask(plane, crtc->dev, plane_mask) {
1356 struct drm_plane_state *old_plane_state =
1357 drm_atomic_get_existing_plane_state(old_state, plane);
1358 const struct drm_plane_helper_funcs *plane_funcs;
1360 plane_funcs = plane->helper_private;
1362 if (!old_plane_state || !plane_funcs)
1365 WARN_ON(plane->state->crtc && plane->state->crtc != crtc);
1367 if (drm_atomic_plane_disabling(plane, old_plane_state) &&
1368 plane_funcs->atomic_disable)
1369 plane_funcs->atomic_disable(plane, old_plane_state);
1370 else if (plane->state->crtc ||
1371 drm_atomic_plane_disabling(plane, old_plane_state))
1372 plane_funcs->atomic_update(plane, old_plane_state);
1375 if (crtc_funcs && crtc_funcs->atomic_flush)
1376 crtc_funcs->atomic_flush(crtc, old_crtc_state);
1378 EXPORT_SYMBOL(drm_atomic_helper_commit_planes_on_crtc);
1381 * drm_atomic_helper_disable_planes_on_crtc - helper to disable CRTC's planes
1383 * @atomic: if set, synchronize with CRTC's atomic_begin/flush hooks
1385 * Disables all planes associated with the given CRTC. This can be
1386 * used for instance in the CRTC helper disable callback to disable
1387 * all planes before shutting down the display pipeline.
1389 * If the atomic-parameter is set the function calls the CRTC's
1390 * atomic_begin hook before and atomic_flush hook after disabling the
1393 * It is a bug to call this function without having implemented the
1394 * ->atomic_disable() plane hook.
1396 void drm_atomic_helper_disable_planes_on_crtc(struct drm_crtc *crtc,
1399 const struct drm_crtc_helper_funcs *crtc_funcs =
1400 crtc->helper_private;
1401 struct drm_plane *plane;
1403 if (atomic && crtc_funcs && crtc_funcs->atomic_begin)
1404 crtc_funcs->atomic_begin(crtc, NULL);
1406 drm_for_each_plane(plane, crtc->dev) {
1407 const struct drm_plane_helper_funcs *plane_funcs =
1408 plane->helper_private;
1410 if (plane->state->crtc != crtc || !plane_funcs)
1413 WARN_ON(!plane_funcs->atomic_disable);
1414 if (plane_funcs->atomic_disable)
1415 plane_funcs->atomic_disable(plane, NULL);
1418 if (atomic && crtc_funcs && crtc_funcs->atomic_flush)
1419 crtc_funcs->atomic_flush(crtc, NULL);
1421 EXPORT_SYMBOL(drm_atomic_helper_disable_planes_on_crtc);
1424 * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
1426 * @old_state: atomic state object with old state structures
1428 * This function cleans up plane state, specifically framebuffers, from the old
1429 * configuration. Hence the old configuration must be perserved in @old_state to
1430 * be able to call this function.
1432 * This function must also be called on the new state when the atomic update
1433 * fails at any point after calling drm_atomic_helper_prepare_planes().
1435 void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
1436 struct drm_atomic_state *old_state)
1438 struct drm_plane *plane;
1439 struct drm_plane_state *plane_state;
1442 for_each_plane_in_state(old_state, plane, plane_state, i) {
1443 const struct drm_plane_helper_funcs *funcs;
1445 funcs = plane->helper_private;
1447 if (funcs->cleanup_fb)
1448 funcs->cleanup_fb(plane, plane_state);
1451 EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
1454 * drm_atomic_helper_swap_state - store atomic state into current sw state
1456 * @state: atomic state
1458 * This function stores the atomic state into the current state pointers in all
1459 * driver objects. It should be called after all failing steps have been done
1460 * and succeeded, but before the actual hardware state is committed.
1462 * For cleanup and error recovery the current state for all changed objects will
1463 * be swaped into @state.
1465 * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
1467 * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
1469 * 2. Do any other steps that might fail.
1471 * 3. Put the staged state into the current state pointers with this function.
1473 * 4. Actually commit the hardware state.
1475 * 5. Call drm_atomic_helper_cleanup_planes() with @state, which since step 3
1476 * contains the old state. Also do any other cleanup required with that state.
1478 void drm_atomic_helper_swap_state(struct drm_device *dev,
1479 struct drm_atomic_state *state)
1483 for (i = 0; i < dev->mode_config.num_connector; i++) {
1484 struct drm_connector *connector = state->connectors[i];
1489 connector->state->state = state;
1490 swap(state->connector_states[i], connector->state);
1491 connector->state->state = NULL;
1494 for (i = 0; i < dev->mode_config.num_crtc; i++) {
1495 struct drm_crtc *crtc = state->crtcs[i];
1500 crtc->state->state = state;
1501 swap(state->crtc_states[i], crtc->state);
1502 crtc->state->state = NULL;
1505 for (i = 0; i < dev->mode_config.num_total_plane; i++) {
1506 struct drm_plane *plane = state->planes[i];
1511 plane->state->state = state;
1512 swap(state->plane_states[i], plane->state);
1513 plane->state->state = NULL;
1516 EXPORT_SYMBOL(drm_atomic_helper_swap_state);
1519 * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
1520 * @plane: plane object to update
1521 * @crtc: owning CRTC of owning plane
1522 * @fb: framebuffer to flip onto plane
1523 * @crtc_x: x offset of primary plane on crtc
1524 * @crtc_y: y offset of primary plane on crtc
1525 * @crtc_w: width of primary plane rectangle on crtc
1526 * @crtc_h: height of primary plane rectangle on crtc
1527 * @src_x: x offset of @fb for panning
1528 * @src_y: y offset of @fb for panning
1529 * @src_w: width of source rectangle in @fb
1530 * @src_h: height of source rectangle in @fb
1532 * Provides a default plane update handler using the atomic driver interface.
1535 * Zero on success, error code on failure
1537 int drm_atomic_helper_update_plane(struct drm_plane *plane,
1538 struct drm_crtc *crtc,
1539 struct drm_framebuffer *fb,
1540 int crtc_x, int crtc_y,
1541 unsigned int crtc_w, unsigned int crtc_h,
1542 uint32_t src_x, uint32_t src_y,
1543 uint32_t src_w, uint32_t src_h)
1545 struct drm_atomic_state *state;
1546 struct drm_plane_state *plane_state;
1549 state = drm_atomic_state_alloc(plane->dev);
1553 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1555 plane_state = drm_atomic_get_plane_state(state, plane);
1556 if (IS_ERR(plane_state)) {
1557 ret = PTR_ERR(plane_state);
1561 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
1564 drm_atomic_set_fb_for_plane(plane_state, fb);
1565 plane_state->crtc_x = crtc_x;
1566 plane_state->crtc_y = crtc_y;
1567 plane_state->crtc_w = crtc_w;
1568 plane_state->crtc_h = crtc_h;
1569 plane_state->src_x = src_x;
1570 plane_state->src_y = src_y;
1571 plane_state->src_w = src_w;
1572 plane_state->src_h = src_h;
1574 if (plane == crtc->cursor)
1575 state->legacy_cursor_update = true;
1577 ret = drm_atomic_commit(state);
1581 /* Driver takes ownership of state on successful commit. */
1584 if (ret == -EDEADLK)
1587 drm_atomic_state_free(state);
1591 drm_atomic_state_clear(state);
1592 drm_atomic_legacy_backoff(state);
1595 * Someone might have exchanged the framebuffer while we dropped locks
1596 * in the backoff code. We need to fix up the fb refcount tracking the
1599 plane->old_fb = plane->fb;
1603 EXPORT_SYMBOL(drm_atomic_helper_update_plane);
1606 * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
1607 * @plane: plane to disable
1609 * Provides a default plane disable handler using the atomic driver interface.
1612 * Zero on success, error code on failure
1614 int drm_atomic_helper_disable_plane(struct drm_plane *plane)
1616 struct drm_atomic_state *state;
1617 struct drm_plane_state *plane_state;
1621 * FIXME: Without plane->crtc set we can't get at the implicit legacy
1622 * acquire context. The real fix will be to wire the acquire ctx through
1623 * everywhere we need it, but meanwhile prevent chaos by just skipping
1624 * this noop. The critical case is the cursor ioctls which a) only grab
1625 * crtc/cursor-plane locks (so we need the crtc to get at the right
1626 * acquire context) and b) can try to disable the plane multiple times.
1631 state = drm_atomic_state_alloc(plane->dev);
1635 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(plane->crtc);
1637 plane_state = drm_atomic_get_plane_state(state, plane);
1638 if (IS_ERR(plane_state)) {
1639 ret = PTR_ERR(plane_state);
1643 if (plane_state->crtc && (plane == plane->crtc->cursor))
1644 plane_state->state->legacy_cursor_update = true;
1646 ret = __drm_atomic_helper_disable_plane(plane, plane_state);
1650 ret = drm_atomic_commit(state);
1654 /* Driver takes ownership of state on successful commit. */
1657 if (ret == -EDEADLK)
1660 drm_atomic_state_free(state);
1664 drm_atomic_state_clear(state);
1665 drm_atomic_legacy_backoff(state);
1668 * Someone might have exchanged the framebuffer while we dropped locks
1669 * in the backoff code. We need to fix up the fb refcount tracking the
1672 plane->old_fb = plane->fb;
1676 EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
1678 /* just used from fb-helper and atomic-helper: */
1679 int __drm_atomic_helper_disable_plane(struct drm_plane *plane,
1680 struct drm_plane_state *plane_state)
1684 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
1688 drm_atomic_set_fb_for_plane(plane_state, NULL);
1689 plane_state->crtc_x = 0;
1690 plane_state->crtc_y = 0;
1691 plane_state->crtc_w = 0;
1692 plane_state->crtc_h = 0;
1693 plane_state->src_x = 0;
1694 plane_state->src_y = 0;
1695 plane_state->src_w = 0;
1696 plane_state->src_h = 0;
1701 static int update_output_state(struct drm_atomic_state *state,
1702 struct drm_mode_set *set)
1704 struct drm_device *dev = set->crtc->dev;
1705 struct drm_crtc *crtc;
1706 struct drm_crtc_state *crtc_state;
1707 struct drm_connector *connector;
1708 struct drm_connector_state *conn_state;
1711 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
1712 state->acquire_ctx);
1716 /* First grab all affected connector/crtc states. */
1717 for (i = 0; i < set->num_connectors; i++) {
1718 conn_state = drm_atomic_get_connector_state(state,
1719 set->connectors[i]);
1720 if (IS_ERR(conn_state))
1721 return PTR_ERR(conn_state);
1724 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1725 ret = drm_atomic_add_affected_connectors(state, crtc);
1730 /* Then recompute connector->crtc links and crtc enabling state. */
1731 for_each_connector_in_state(state, connector, conn_state, i) {
1732 if (conn_state->crtc == set->crtc) {
1733 ret = drm_atomic_set_crtc_for_connector(conn_state,
1739 for (j = 0; j < set->num_connectors; j++) {
1740 if (set->connectors[j] == connector) {
1741 ret = drm_atomic_set_crtc_for_connector(conn_state,
1750 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1751 /* Don't update ->enable for the CRTC in the set_config request,
1752 * since a mismatch would indicate a bug in the upper layers.
1753 * The actual modeset code later on will catch any
1754 * inconsistencies here. */
1755 if (crtc == set->crtc)
1758 if (!drm_atomic_connectors_for_crtc(state, crtc)) {
1759 ret = drm_atomic_set_mode_prop_for_crtc(crtc_state,
1764 crtc_state->active = false;
1772 * drm_atomic_helper_set_config - set a new config from userspace
1773 * @set: mode set configuration
1775 * Provides a default crtc set_config handler using the atomic driver interface.
1778 * Returns 0 on success, negative errno numbers on failure.
1780 int drm_atomic_helper_set_config(struct drm_mode_set *set)
1782 struct drm_atomic_state *state;
1783 struct drm_crtc *crtc = set->crtc;
1786 state = drm_atomic_state_alloc(crtc->dev);
1790 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1792 ret = __drm_atomic_helper_set_config(set, state);
1796 ret = drm_atomic_commit(state);
1800 /* Driver takes ownership of state on successful commit. */
1803 if (ret == -EDEADLK)
1806 drm_atomic_state_free(state);
1810 drm_atomic_state_clear(state);
1811 drm_atomic_legacy_backoff(state);
1814 * Someone might have exchanged the framebuffer while we dropped locks
1815 * in the backoff code. We need to fix up the fb refcount tracking the
1818 crtc->primary->old_fb = crtc->primary->fb;
1822 EXPORT_SYMBOL(drm_atomic_helper_set_config);
1824 /* just used from fb-helper and atomic-helper: */
1825 int __drm_atomic_helper_set_config(struct drm_mode_set *set,
1826 struct drm_atomic_state *state)
1828 struct drm_crtc_state *crtc_state;
1829 struct drm_plane_state *primary_state;
1830 struct drm_crtc *crtc = set->crtc;
1831 int hdisplay, vdisplay;
1834 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1835 if (IS_ERR(crtc_state))
1836 return PTR_ERR(crtc_state);
1838 primary_state = drm_atomic_get_plane_state(state, crtc->primary);
1839 if (IS_ERR(primary_state))
1840 return PTR_ERR(primary_state);
1844 WARN_ON(set->num_connectors);
1846 ret = drm_atomic_set_mode_for_crtc(crtc_state, NULL);
1850 crtc_state->active = false;
1852 ret = drm_atomic_set_crtc_for_plane(primary_state, NULL);
1856 drm_atomic_set_fb_for_plane(primary_state, NULL);
1862 WARN_ON(!set->num_connectors);
1864 ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
1868 crtc_state->active = true;
1870 ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
1874 drm_crtc_get_hv_timing(set->mode, &hdisplay, &vdisplay);
1876 drm_atomic_set_fb_for_plane(primary_state, set->fb);
1877 primary_state->crtc_x = 0;
1878 primary_state->crtc_y = 0;
1879 primary_state->crtc_w = hdisplay;
1880 primary_state->crtc_h = vdisplay;
1881 primary_state->src_x = set->x << 16;
1882 primary_state->src_y = set->y << 16;
1883 if (primary_state->rotation & (BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270))) {
1884 primary_state->src_w = vdisplay << 16;
1885 primary_state->src_h = hdisplay << 16;
1887 primary_state->src_w = hdisplay << 16;
1888 primary_state->src_h = vdisplay << 16;
1892 ret = update_output_state(state, set);
1900 * drm_atomic_helper_disable_all - disable all currently active outputs
1902 * @ctx: lock acquisition context
1904 * Loops through all connectors, finding those that aren't turned off and then
1905 * turns them off by setting their DPMS mode to OFF and deactivating the CRTC
1906 * that they are connected to.
1908 * This is used for example in suspend/resume to disable all currently active
1909 * functions when suspending.
1911 * Note that if callers haven't already acquired all modeset locks this might
1912 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
1915 * 0 on success or a negative error code on failure.
1918 * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
1920 int drm_atomic_helper_disable_all(struct drm_device *dev,
1921 struct drm_modeset_acquire_ctx *ctx)
1923 struct drm_atomic_state *state;
1924 struct drm_connector *conn;
1927 state = drm_atomic_state_alloc(dev);
1931 state->acquire_ctx = ctx;
1933 drm_for_each_connector(conn, dev) {
1934 struct drm_crtc *crtc = conn->state->crtc;
1935 struct drm_crtc_state *crtc_state;
1937 if (!crtc || conn->dpms != DRM_MODE_DPMS_ON)
1940 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1941 if (IS_ERR(crtc_state)) {
1942 err = PTR_ERR(crtc_state);
1946 crtc_state->active = false;
1949 err = drm_atomic_commit(state);
1953 drm_atomic_state_free(state);
1957 EXPORT_SYMBOL(drm_atomic_helper_disable_all);
1960 * drm_atomic_helper_suspend - subsystem-level suspend helper
1963 * Duplicates the current atomic state, disables all active outputs and then
1964 * returns a pointer to the original atomic state to the caller. Drivers can
1965 * pass this pointer to the drm_atomic_helper_resume() helper upon resume to
1966 * restore the output configuration that was active at the time the system
1969 * Note that it is potentially unsafe to use this. The atomic state object
1970 * returned by this function is assumed to be persistent. Drivers must ensure
1971 * that this holds true. Before calling this function, drivers must make sure
1972 * to suspend fbdev emulation so that nothing can be using the device.
1975 * A pointer to a copy of the state before suspend on success or an ERR_PTR()-
1976 * encoded error code on failure. Drivers should store the returned atomic
1977 * state object and pass it to the drm_atomic_helper_resume() helper upon
1981 * drm_atomic_helper_duplicate_state(), drm_atomic_helper_disable_all(),
1982 * drm_atomic_helper_resume()
1984 struct drm_atomic_state *drm_atomic_helper_suspend(struct drm_device *dev)
1986 struct drm_modeset_acquire_ctx ctx;
1987 struct drm_atomic_state *state;
1990 drm_modeset_acquire_init(&ctx, 0);
1993 err = drm_modeset_lock_all_ctx(dev, &ctx);
1995 state = ERR_PTR(err);
1999 state = drm_atomic_helper_duplicate_state(dev, &ctx);
2003 err = drm_atomic_helper_disable_all(dev, &ctx);
2005 drm_atomic_state_free(state);
2006 state = ERR_PTR(err);
2011 if (PTR_ERR(state) == -EDEADLK) {
2012 drm_modeset_backoff(&ctx);
2016 drm_modeset_drop_locks(&ctx);
2017 drm_modeset_acquire_fini(&ctx);
2020 EXPORT_SYMBOL(drm_atomic_helper_suspend);
2023 * drm_atomic_helper_resume - subsystem-level resume helper
2025 * @state: atomic state to resume to
2027 * Calls drm_mode_config_reset() to synchronize hardware and software states,
2028 * grabs all modeset locks and commits the atomic state object. This can be
2029 * used in conjunction with the drm_atomic_helper_suspend() helper to
2030 * implement suspend/resume for drivers that support atomic mode-setting.
2033 * 0 on success or a negative error code on failure.
2036 * drm_atomic_helper_suspend()
2038 int drm_atomic_helper_resume(struct drm_device *dev,
2039 struct drm_atomic_state *state)
2041 struct drm_mode_config *config = &dev->mode_config;
2044 drm_mode_config_reset(dev);
2045 drm_modeset_lock_all(dev);
2046 state->acquire_ctx = config->acquire_ctx;
2047 err = drm_atomic_commit(state);
2048 drm_modeset_unlock_all(dev);
2052 EXPORT_SYMBOL(drm_atomic_helper_resume);
2055 * drm_atomic_helper_crtc_set_property - helper for crtc properties
2057 * @property: DRM property
2058 * @val: value of property
2060 * Provides a default crtc set_property handler using the atomic driver
2064 * Zero on success, error code on failure
2067 drm_atomic_helper_crtc_set_property(struct drm_crtc *crtc,
2068 struct drm_property *property,
2071 struct drm_atomic_state *state;
2072 struct drm_crtc_state *crtc_state;
2075 state = drm_atomic_state_alloc(crtc->dev);
2079 /* ->set_property is always called with all locks held. */
2080 state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
2082 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2083 if (IS_ERR(crtc_state)) {
2084 ret = PTR_ERR(crtc_state);
2088 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
2093 ret = drm_atomic_commit(state);
2097 /* Driver takes ownership of state on successful commit. */
2100 if (ret == -EDEADLK)
2103 drm_atomic_state_free(state);
2107 drm_atomic_state_clear(state);
2108 drm_atomic_legacy_backoff(state);
2112 EXPORT_SYMBOL(drm_atomic_helper_crtc_set_property);
2115 * drm_atomic_helper_plane_set_property - helper for plane properties
2117 * @property: DRM property
2118 * @val: value of property
2120 * Provides a default plane set_property handler using the atomic driver
2124 * Zero on success, error code on failure
2127 drm_atomic_helper_plane_set_property(struct drm_plane *plane,
2128 struct drm_property *property,
2131 struct drm_atomic_state *state;
2132 struct drm_plane_state *plane_state;
2135 state = drm_atomic_state_alloc(plane->dev);
2139 /* ->set_property is always called with all locks held. */
2140 state->acquire_ctx = plane->dev->mode_config.acquire_ctx;
2142 plane_state = drm_atomic_get_plane_state(state, plane);
2143 if (IS_ERR(plane_state)) {
2144 ret = PTR_ERR(plane_state);
2148 ret = drm_atomic_plane_set_property(plane, plane_state,
2153 ret = drm_atomic_commit(state);
2157 /* Driver takes ownership of state on successful commit. */
2160 if (ret == -EDEADLK)
2163 drm_atomic_state_free(state);
2167 drm_atomic_state_clear(state);
2168 drm_atomic_legacy_backoff(state);
2172 EXPORT_SYMBOL(drm_atomic_helper_plane_set_property);
2175 * drm_atomic_helper_connector_set_property - helper for connector properties
2176 * @connector: DRM connector
2177 * @property: DRM property
2178 * @val: value of property
2180 * Provides a default connector set_property handler using the atomic driver
2184 * Zero on success, error code on failure
2187 drm_atomic_helper_connector_set_property(struct drm_connector *connector,
2188 struct drm_property *property,
2191 struct drm_atomic_state *state;
2192 struct drm_connector_state *connector_state;
2195 state = drm_atomic_state_alloc(connector->dev);
2199 /* ->set_property is always called with all locks held. */
2200 state->acquire_ctx = connector->dev->mode_config.acquire_ctx;
2202 connector_state = drm_atomic_get_connector_state(state, connector);
2203 if (IS_ERR(connector_state)) {
2204 ret = PTR_ERR(connector_state);
2208 ret = drm_atomic_connector_set_property(connector, connector_state,
2213 ret = drm_atomic_commit(state);
2217 /* Driver takes ownership of state on successful commit. */
2220 if (ret == -EDEADLK)
2223 drm_atomic_state_free(state);
2227 drm_atomic_state_clear(state);
2228 drm_atomic_legacy_backoff(state);
2232 EXPORT_SYMBOL(drm_atomic_helper_connector_set_property);
2235 * drm_atomic_helper_page_flip - execute a legacy page flip
2237 * @fb: DRM framebuffer
2238 * @event: optional DRM event to signal upon completion
2239 * @flags: flip flags for non-vblank sync'ed updates
2241 * Provides a default page flip implementation using the atomic driver interface.
2243 * Note that for now so called async page flips (i.e. updates which are not
2244 * synchronized to vblank) are not supported, since the atomic interfaces have
2245 * no provisions for this yet.
2248 * Returns 0 on success, negative errno numbers on failure.
2250 int drm_atomic_helper_page_flip(struct drm_crtc *crtc,
2251 struct drm_framebuffer *fb,
2252 struct drm_pending_vblank_event *event,
2255 struct drm_plane *plane = crtc->primary;
2256 struct drm_atomic_state *state;
2257 struct drm_plane_state *plane_state;
2258 struct drm_crtc_state *crtc_state;
2261 if (flags & DRM_MODE_PAGE_FLIP_ASYNC)
2264 state = drm_atomic_state_alloc(plane->dev);
2268 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2270 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2271 if (IS_ERR(crtc_state)) {
2272 ret = PTR_ERR(crtc_state);
2275 crtc_state->event = event;
2277 plane_state = drm_atomic_get_plane_state(state, plane);
2278 if (IS_ERR(plane_state)) {
2279 ret = PTR_ERR(plane_state);
2283 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
2286 drm_atomic_set_fb_for_plane(plane_state, fb);
2288 ret = drm_atomic_async_commit(state);
2292 /* Driver takes ownership of state on successful async commit. */
2295 if (ret == -EDEADLK)
2298 drm_atomic_state_free(state);
2302 drm_atomic_state_clear(state);
2303 drm_atomic_legacy_backoff(state);
2306 * Someone might have exchanged the framebuffer while we dropped locks
2307 * in the backoff code. We need to fix up the fb refcount tracking the
2310 plane->old_fb = plane->fb;
2314 EXPORT_SYMBOL(drm_atomic_helper_page_flip);
2317 * drm_atomic_helper_connector_dpms() - connector dpms helper implementation
2318 * @connector: affected connector
2321 * This is the main helper function provided by the atomic helper framework for
2322 * implementing the legacy DPMS connector interface. It computes the new desired
2323 * ->active state for the corresponding CRTC (if the connector is enabled) and
2327 * Returns 0 on success, negative errno numbers on failure.
2329 int drm_atomic_helper_connector_dpms(struct drm_connector *connector,
2332 struct drm_mode_config *config = &connector->dev->mode_config;
2333 struct drm_atomic_state *state;
2334 struct drm_crtc_state *crtc_state;
2335 struct drm_crtc *crtc;
2336 struct drm_connector *tmp_connector;
2338 bool active = false;
2339 int old_mode = connector->dpms;
2341 if (mode != DRM_MODE_DPMS_ON)
2342 mode = DRM_MODE_DPMS_OFF;
2344 connector->dpms = mode;
2345 crtc = connector->state->crtc;
2350 state = drm_atomic_state_alloc(connector->dev);
2354 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2356 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2357 if (IS_ERR(crtc_state)) {
2358 ret = PTR_ERR(crtc_state);
2362 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
2364 drm_for_each_connector(tmp_connector, connector->dev) {
2365 if (tmp_connector->state->crtc != crtc)
2368 if (tmp_connector->dpms == DRM_MODE_DPMS_ON) {
2373 crtc_state->active = active;
2375 ret = drm_atomic_commit(state);
2379 /* Driver takes ownership of state on successful commit. */
2382 if (ret == -EDEADLK)
2385 connector->dpms = old_mode;
2386 drm_atomic_state_free(state);
2390 drm_atomic_state_clear(state);
2391 drm_atomic_legacy_backoff(state);
2395 EXPORT_SYMBOL(drm_atomic_helper_connector_dpms);
2398 * DOC: atomic state reset and initialization
2400 * Both the drm core and the atomic helpers assume that there is always the full
2401 * and correct atomic software state for all connectors, CRTCs and planes
2402 * available. Which is a bit a problem on driver load and also after system
2403 * suspend. One way to solve this is to have a hardware state read-out
2404 * infrastructure which reconstructs the full software state (e.g. the i915
2407 * The simpler solution is to just reset the software state to everything off,
2408 * which is easiest to do by calling drm_mode_config_reset(). To facilitate this
2409 * the atomic helpers provide default reset implementations for all hooks.
2411 * On the upside the precise state tracking of atomic simplifies system suspend
2412 * and resume a lot. For drivers using drm_mode_config_reset() a complete recipe
2413 * is implemented in drm_atomic_helper_suspend() and drm_atomic_helper_resume().
2414 * For other drivers the building blocks are split out, see the documentation
2415 * for these functions.
2419 * drm_atomic_helper_crtc_reset - default ->reset hook for CRTCs
2422 * Resets the atomic state for @crtc by freeing the state pointer (which might
2423 * be NULL, e.g. at driver load time) and allocating a new empty state object.
2425 void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc)
2428 drm_property_unreference_blob(crtc->state->mode_blob);
2430 crtc->state = kzalloc(sizeof(*crtc->state), GFP_KERNEL);
2433 crtc->state->crtc = crtc;
2435 EXPORT_SYMBOL(drm_atomic_helper_crtc_reset);
2438 * __drm_atomic_helper_crtc_duplicate_state - copy atomic CRTC state
2439 * @crtc: CRTC object
2440 * @state: atomic CRTC state
2442 * Copies atomic state from a CRTC's current state and resets inferred values.
2443 * This is useful for drivers that subclass the CRTC state.
2445 void __drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc,
2446 struct drm_crtc_state *state)
2448 memcpy(state, crtc->state, sizeof(*state));
2450 if (state->mode_blob)
2451 drm_property_reference_blob(state->mode_blob);
2452 state->mode_changed = false;
2453 state->active_changed = false;
2454 state->planes_changed = false;
2455 state->connectors_changed = false;
2456 state->event = NULL;
2458 EXPORT_SYMBOL(__drm_atomic_helper_crtc_duplicate_state);
2461 * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
2464 * Default CRTC state duplicate hook for drivers which don't have their own
2465 * subclassed CRTC state structure.
2467 struct drm_crtc_state *
2468 drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc)
2470 struct drm_crtc_state *state;
2472 if (WARN_ON(!crtc->state))
2475 state = kmalloc(sizeof(*state), GFP_KERNEL);
2477 __drm_atomic_helper_crtc_duplicate_state(crtc, state);
2481 EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state);
2484 * __drm_atomic_helper_crtc_destroy_state - release CRTC state
2485 * @crtc: CRTC object
2486 * @state: CRTC state object to release
2488 * Releases all resources stored in the CRTC state without actually freeing
2489 * the memory of the CRTC state. This is useful for drivers that subclass the
2492 void __drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
2493 struct drm_crtc_state *state)
2495 drm_property_unreference_blob(state->mode_blob);
2497 EXPORT_SYMBOL(__drm_atomic_helper_crtc_destroy_state);
2500 * drm_atomic_helper_crtc_destroy_state - default state destroy hook
2502 * @state: CRTC state object to release
2504 * Default CRTC state destroy hook for drivers which don't have their own
2505 * subclassed CRTC state structure.
2507 void drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
2508 struct drm_crtc_state *state)
2510 __drm_atomic_helper_crtc_destroy_state(crtc, state);
2513 EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state);
2516 * drm_atomic_helper_plane_reset - default ->reset hook for planes
2519 * Resets the atomic state for @plane by freeing the state pointer (which might
2520 * be NULL, e.g. at driver load time) and allocating a new empty state object.
2522 void drm_atomic_helper_plane_reset(struct drm_plane *plane)
2524 if (plane->state && plane->state->fb)
2525 drm_framebuffer_unreference(plane->state->fb);
2527 kfree(plane->state);
2528 plane->state = kzalloc(sizeof(*plane->state), GFP_KERNEL);
2531 plane->state->plane = plane;
2533 EXPORT_SYMBOL(drm_atomic_helper_plane_reset);
2536 * __drm_atomic_helper_plane_duplicate_state - copy atomic plane state
2537 * @plane: plane object
2538 * @state: atomic plane state
2540 * Copies atomic state from a plane's current state. This is useful for
2541 * drivers that subclass the plane state.
2543 void __drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane,
2544 struct drm_plane_state *state)
2546 memcpy(state, plane->state, sizeof(*state));
2549 drm_framebuffer_reference(state->fb);
2551 EXPORT_SYMBOL(__drm_atomic_helper_plane_duplicate_state);
2554 * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
2557 * Default plane state duplicate hook for drivers which don't have their own
2558 * subclassed plane state structure.
2560 struct drm_plane_state *
2561 drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane)
2563 struct drm_plane_state *state;
2565 if (WARN_ON(!plane->state))
2568 state = kmalloc(sizeof(*state), GFP_KERNEL);
2570 __drm_atomic_helper_plane_duplicate_state(plane, state);
2574 EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state);
2577 * __drm_atomic_helper_plane_destroy_state - release plane state
2578 * @plane: plane object
2579 * @state: plane state object to release
2581 * Releases all resources stored in the plane state without actually freeing
2582 * the memory of the plane state. This is useful for drivers that subclass the
2585 void __drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
2586 struct drm_plane_state *state)
2589 drm_framebuffer_unreference(state->fb);
2591 EXPORT_SYMBOL(__drm_atomic_helper_plane_destroy_state);
2594 * drm_atomic_helper_plane_destroy_state - default state destroy hook
2596 * @state: plane state object to release
2598 * Default plane state destroy hook for drivers which don't have their own
2599 * subclassed plane state structure.
2601 void drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
2602 struct drm_plane_state *state)
2604 __drm_atomic_helper_plane_destroy_state(plane, state);
2607 EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state);
2610 * drm_atomic_helper_connector_reset - default ->reset hook for connectors
2611 * @connector: drm connector
2613 * Resets the atomic state for @connector by freeing the state pointer (which
2614 * might be NULL, e.g. at driver load time) and allocating a new empty state
2617 void drm_atomic_helper_connector_reset(struct drm_connector *connector)
2619 kfree(connector->state);
2620 connector->state = kzalloc(sizeof(*connector->state), GFP_KERNEL);
2622 if (connector->state)
2623 connector->state->connector = connector;
2625 EXPORT_SYMBOL(drm_atomic_helper_connector_reset);
2628 * __drm_atomic_helper_connector_duplicate_state - copy atomic connector state
2629 * @connector: connector object
2630 * @state: atomic connector state
2632 * Copies atomic state from a connector's current state. This is useful for
2633 * drivers that subclass the connector state.
2636 __drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector,
2637 struct drm_connector_state *state)
2639 memcpy(state, connector->state, sizeof(*state));
2641 EXPORT_SYMBOL(__drm_atomic_helper_connector_duplicate_state);
2644 * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
2645 * @connector: drm connector
2647 * Default connector state duplicate hook for drivers which don't have their own
2648 * subclassed connector state structure.
2650 struct drm_connector_state *
2651 drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
2653 struct drm_connector_state *state;
2655 if (WARN_ON(!connector->state))
2658 state = kmalloc(sizeof(*state), GFP_KERNEL);
2660 __drm_atomic_helper_connector_duplicate_state(connector, state);
2664 EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
2667 * drm_atomic_helper_duplicate_state - duplicate an atomic state object
2669 * @ctx: lock acquisition context
2671 * Makes a copy of the current atomic state by looping over all objects and
2672 * duplicating their respective states. This is used for example by suspend/
2673 * resume support code to save the state prior to suspend such that it can
2674 * be restored upon resume.
2676 * Note that this treats atomic state as persistent between save and restore.
2677 * Drivers must make sure that this is possible and won't result in confusion
2678 * or erroneous behaviour.
2680 * Note that if callers haven't already acquired all modeset locks this might
2681 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
2684 * A pointer to the copy of the atomic state object on success or an
2685 * ERR_PTR()-encoded error code on failure.
2688 * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
2690 struct drm_atomic_state *
2691 drm_atomic_helper_duplicate_state(struct drm_device *dev,
2692 struct drm_modeset_acquire_ctx *ctx)
2694 struct drm_atomic_state *state;
2695 struct drm_connector *conn;
2696 struct drm_plane *plane;
2697 struct drm_crtc *crtc;
2700 state = drm_atomic_state_alloc(dev);
2702 return ERR_PTR(-ENOMEM);
2704 state->acquire_ctx = ctx;
2706 drm_for_each_crtc(crtc, dev) {
2707 struct drm_crtc_state *crtc_state;
2709 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2710 if (IS_ERR(crtc_state)) {
2711 err = PTR_ERR(crtc_state);
2716 drm_for_each_plane(plane, dev) {
2717 struct drm_plane_state *plane_state;
2719 plane_state = drm_atomic_get_plane_state(state, plane);
2720 if (IS_ERR(plane_state)) {
2721 err = PTR_ERR(plane_state);
2726 drm_for_each_connector(conn, dev) {
2727 struct drm_connector_state *conn_state;
2729 conn_state = drm_atomic_get_connector_state(state, conn);
2730 if (IS_ERR(conn_state)) {
2731 err = PTR_ERR(conn_state);
2736 /* clear the acquire context so that it isn't accidentally reused */
2737 state->acquire_ctx = NULL;
2741 drm_atomic_state_free(state);
2742 state = ERR_PTR(err);
2747 EXPORT_SYMBOL(drm_atomic_helper_duplicate_state);
2750 * __drm_atomic_helper_connector_destroy_state - release connector state
2751 * @connector: connector object
2752 * @state: connector state object to release
2754 * Releases all resources stored in the connector state without actually
2755 * freeing the memory of the connector state. This is useful for drivers that
2756 * subclass the connector state.
2759 __drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
2760 struct drm_connector_state *state)
2763 * This is currently a placeholder so that drivers that subclass the
2764 * state will automatically do the right thing if code is ever added
2768 EXPORT_SYMBOL(__drm_atomic_helper_connector_destroy_state);
2771 * drm_atomic_helper_connector_destroy_state - default state destroy hook
2772 * @connector: drm connector
2773 * @state: connector state object to release
2775 * Default connector state destroy hook for drivers which don't have their own
2776 * subclassed connector state structure.
2778 void drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
2779 struct drm_connector_state *state)
2781 __drm_atomic_helper_connector_destroy_state(connector, state);
2784 EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state);