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/dma-fence.h>
35 #include "drm_crtc_internal.h"
40 * This helper library provides implementations of check and commit functions on
41 * top of the CRTC modeset helper callbacks and the plane helper callbacks. It
42 * also provides convenience implementations for the atomic state handling
43 * callbacks for drivers which don't need to subclass the drm core structures to
44 * add their own additional internal state.
46 * This library also provides default implementations for the check callback in
47 * drm_atomic_helper_check() and for the commit callback with
48 * drm_atomic_helper_commit(). But the individual stages and callbacks are
49 * exposed to allow drivers to mix and match and e.g. use the plane helpers only
50 * together with a driver private modeset implementation.
52 * This library also provides implementations for all the legacy driver
53 * interfaces on top of the atomic interface. See drm_atomic_helper_set_config(),
54 * drm_atomic_helper_disable_plane(), drm_atomic_helper_disable_plane() and the
55 * various functions to implement set_property callbacks. New drivers must not
56 * implement these functions themselves but must use the provided helpers.
58 * The atomic helper uses the same function table structures as all other
59 * modesetting helpers. See the documentation for &struct drm_crtc_helper_funcs,
60 * struct &drm_encoder_helper_funcs and &struct drm_connector_helper_funcs. It
61 * also shares the &struct drm_plane_helper_funcs function table with the plane
65 drm_atomic_helper_plane_changed(struct drm_atomic_state *state,
66 struct drm_plane_state *plane_state,
67 struct drm_plane *plane)
69 struct drm_crtc_state *crtc_state;
71 if (plane->state->crtc) {
72 crtc_state = drm_atomic_get_existing_crtc_state(state,
75 if (WARN_ON(!crtc_state))
78 crtc_state->planes_changed = true;
81 if (plane_state->crtc) {
82 crtc_state = drm_atomic_get_existing_crtc_state(state,
85 if (WARN_ON(!crtc_state))
88 crtc_state->planes_changed = true;
92 static int handle_conflicting_encoders(struct drm_atomic_state *state,
93 bool disable_conflicting_encoders)
95 struct drm_connector_state *conn_state;
96 struct drm_connector *connector;
97 struct drm_connector_list_iter conn_iter;
98 struct drm_encoder *encoder;
99 unsigned encoder_mask = 0;
103 * First loop, find all newly assigned encoders from the connectors
104 * part of the state. If the same encoder is assigned to multiple
105 * connectors bail out.
107 for_each_connector_in_state(state, connector, conn_state, i) {
108 const struct drm_connector_helper_funcs *funcs = connector->helper_private;
109 struct drm_encoder *new_encoder;
111 if (!conn_state->crtc)
114 if (funcs->atomic_best_encoder)
115 new_encoder = funcs->atomic_best_encoder(connector, conn_state);
116 else if (funcs->best_encoder)
117 new_encoder = funcs->best_encoder(connector);
119 new_encoder = drm_atomic_helper_best_encoder(connector);
122 if (encoder_mask & (1 << drm_encoder_index(new_encoder))) {
123 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] on [CONNECTOR:%d:%s] already assigned\n",
124 new_encoder->base.id, new_encoder->name,
125 connector->base.id, connector->name);
130 encoder_mask |= 1 << drm_encoder_index(new_encoder);
138 * Second loop, iterate over all connectors not part of the state.
140 * If a conflicting encoder is found and disable_conflicting_encoders
141 * is not set, an error is returned. Userspace can provide a solution
142 * through the atomic ioctl.
144 * If the flag is set conflicting connectors are removed from the crtc
145 * and the crtc is disabled if no encoder is left. This preserves
146 * compatibility with the legacy set_config behavior.
148 drm_connector_list_iter_get(state->dev, &conn_iter);
149 drm_for_each_connector_iter(connector, &conn_iter) {
150 struct drm_crtc_state *crtc_state;
152 if (drm_atomic_get_existing_connector_state(state, connector))
155 encoder = connector->state->best_encoder;
156 if (!encoder || !(encoder_mask & (1 << drm_encoder_index(encoder))))
159 if (!disable_conflicting_encoders) {
160 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s] by [CONNECTOR:%d:%s]\n",
161 encoder->base.id, encoder->name,
162 connector->state->crtc->base.id,
163 connector->state->crtc->name,
164 connector->base.id, connector->name);
169 conn_state = drm_atomic_get_connector_state(state, connector);
170 if (IS_ERR(conn_state)) {
171 ret = PTR_ERR(conn_state);
175 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s], disabling [CONNECTOR:%d:%s]\n",
176 encoder->base.id, encoder->name,
177 conn_state->crtc->base.id, conn_state->crtc->name,
178 connector->base.id, connector->name);
180 crtc_state = drm_atomic_get_existing_crtc_state(state, conn_state->crtc);
182 ret = drm_atomic_set_crtc_for_connector(conn_state, NULL);
186 if (!crtc_state->connector_mask) {
187 ret = drm_atomic_set_mode_prop_for_crtc(crtc_state,
192 crtc_state->active = false;
196 drm_connector_list_iter_put(&conn_iter);
202 set_best_encoder(struct drm_atomic_state *state,
203 struct drm_connector_state *conn_state,
204 struct drm_encoder *encoder)
206 struct drm_crtc_state *crtc_state;
207 struct drm_crtc *crtc;
209 if (conn_state->best_encoder) {
210 /* Unset the encoder_mask in the old crtc state. */
211 crtc = conn_state->connector->state->crtc;
213 /* A NULL crtc is an error here because we should have
214 * duplicated a NULL best_encoder when crtc was NULL.
215 * As an exception restoring duplicated atomic state
216 * during resume is allowed, so don't warn when
217 * best_encoder is equal to encoder we intend to set.
219 WARN_ON(!crtc && encoder != conn_state->best_encoder);
221 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
223 crtc_state->encoder_mask &=
224 ~(1 << drm_encoder_index(conn_state->best_encoder));
229 crtc = conn_state->crtc;
232 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
234 crtc_state->encoder_mask |=
235 1 << drm_encoder_index(encoder);
239 conn_state->best_encoder = encoder;
243 steal_encoder(struct drm_atomic_state *state,
244 struct drm_encoder *encoder)
246 struct drm_crtc_state *crtc_state;
247 struct drm_connector *connector;
248 struct drm_connector_state *connector_state;
251 for_each_connector_in_state(state, connector, connector_state, i) {
252 struct drm_crtc *encoder_crtc;
254 if (connector_state->best_encoder != encoder)
257 encoder_crtc = connector->state->crtc;
259 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s], stealing it\n",
260 encoder->base.id, encoder->name,
261 encoder_crtc->base.id, encoder_crtc->name);
263 set_best_encoder(state, connector_state, NULL);
265 crtc_state = drm_atomic_get_existing_crtc_state(state, encoder_crtc);
266 crtc_state->connectors_changed = true;
273 update_connector_routing(struct drm_atomic_state *state,
274 struct drm_connector *connector,
275 struct drm_connector_state *connector_state)
277 const struct drm_connector_helper_funcs *funcs;
278 struct drm_encoder *new_encoder;
279 struct drm_crtc_state *crtc_state;
281 DRM_DEBUG_ATOMIC("Updating routing for [CONNECTOR:%d:%s]\n",
285 if (connector->state->crtc != connector_state->crtc) {
286 if (connector->state->crtc) {
287 crtc_state = drm_atomic_get_existing_crtc_state(state, connector->state->crtc);
288 crtc_state->connectors_changed = true;
291 if (connector_state->crtc) {
292 crtc_state = drm_atomic_get_existing_crtc_state(state, connector_state->crtc);
293 crtc_state->connectors_changed = true;
297 if (!connector_state->crtc) {
298 DRM_DEBUG_ATOMIC("Disabling [CONNECTOR:%d:%s]\n",
302 set_best_encoder(state, connector_state, NULL);
307 funcs = connector->helper_private;
309 if (funcs->atomic_best_encoder)
310 new_encoder = funcs->atomic_best_encoder(connector,
312 else if (funcs->best_encoder)
313 new_encoder = funcs->best_encoder(connector);
315 new_encoder = drm_atomic_helper_best_encoder(connector);
318 DRM_DEBUG_ATOMIC("No suitable encoder found for [CONNECTOR:%d:%s]\n",
324 if (!drm_encoder_crtc_ok(new_encoder, connector_state->crtc)) {
325 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] incompatible with [CRTC:%d]\n",
326 new_encoder->base.id,
328 connector_state->crtc->base.id);
332 if (new_encoder == connector_state->best_encoder) {
333 set_best_encoder(state, connector_state, new_encoder);
335 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] keeps [ENCODER:%d:%s], now on [CRTC:%d:%s]\n",
338 new_encoder->base.id,
340 connector_state->crtc->base.id,
341 connector_state->crtc->name);
346 steal_encoder(state, new_encoder);
348 set_best_encoder(state, connector_state, new_encoder);
350 crtc_state = drm_atomic_get_existing_crtc_state(state, connector_state->crtc);
351 crtc_state->connectors_changed = true;
353 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] using [ENCODER:%d:%s] on [CRTC:%d:%s]\n",
356 new_encoder->base.id,
358 connector_state->crtc->base.id,
359 connector_state->crtc->name);
365 mode_fixup(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 *conn_state;
374 for_each_crtc_in_state(state, crtc, crtc_state, i) {
375 if (!crtc_state->mode_changed &&
376 !crtc_state->connectors_changed)
379 drm_mode_copy(&crtc_state->adjusted_mode, &crtc_state->mode);
382 for_each_connector_in_state(state, connector, conn_state, i) {
383 const struct drm_encoder_helper_funcs *funcs;
384 struct drm_encoder *encoder;
386 WARN_ON(!!conn_state->best_encoder != !!conn_state->crtc);
388 if (!conn_state->crtc || !conn_state->best_encoder)
391 crtc_state = drm_atomic_get_existing_crtc_state(state,
395 * Each encoder has at most one connector (since we always steal
396 * it away), so we won't call ->mode_fixup twice.
398 encoder = conn_state->best_encoder;
399 funcs = encoder->helper_private;
401 ret = drm_bridge_mode_fixup(encoder->bridge, &crtc_state->mode,
402 &crtc_state->adjusted_mode);
404 DRM_DEBUG_ATOMIC("Bridge fixup failed\n");
408 if (funcs && funcs->atomic_check) {
409 ret = funcs->atomic_check(encoder, crtc_state,
412 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] check failed\n",
413 encoder->base.id, encoder->name);
416 } else if (funcs && funcs->mode_fixup) {
417 ret = funcs->mode_fixup(encoder, &crtc_state->mode,
418 &crtc_state->adjusted_mode);
420 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] fixup failed\n",
421 encoder->base.id, encoder->name);
427 for_each_crtc_in_state(state, crtc, crtc_state, i) {
428 const struct drm_crtc_helper_funcs *funcs;
430 if (!crtc_state->enable)
433 if (!crtc_state->mode_changed &&
434 !crtc_state->connectors_changed)
437 funcs = crtc->helper_private;
438 if (!funcs->mode_fixup)
441 ret = funcs->mode_fixup(crtc, &crtc_state->mode,
442 &crtc_state->adjusted_mode);
444 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] fixup failed\n",
445 crtc->base.id, crtc->name);
454 * drm_atomic_helper_check_modeset - validate state object for modeset changes
456 * @state: the driver state object
458 * Check the state object to see if the requested state is physically possible.
459 * This does all the crtc and connector related computations for an atomic
460 * update and adds any additional connectors needed for full modesets and calls
461 * down into ->mode_fixup functions of the driver backend.
463 * crtc_state->mode_changed is set when the input mode is changed.
464 * crtc_state->connectors_changed is set when a connector is added or
465 * removed from the crtc.
466 * crtc_state->active_changed is set when crtc_state->active changes,
467 * which is used for dpms.
468 * See also: drm_atomic_crtc_needs_modeset()
472 * Drivers which set ->mode_changed (e.g. in their ->atomic_check hooks if a
473 * plane update can't be done without a full modeset) _must_ call this function
474 * afterwards after that change. It is permitted to call this function multiple
475 * times for the same update, e.g. when the ->atomic_check functions depend upon
476 * the adjusted dotclock for fifo space allocation and watermark computation.
479 * Zero for success or -errno
482 drm_atomic_helper_check_modeset(struct drm_device *dev,
483 struct drm_atomic_state *state)
485 struct drm_crtc *crtc;
486 struct drm_crtc_state *crtc_state;
487 struct drm_connector *connector;
488 struct drm_connector_state *connector_state;
491 for_each_crtc_in_state(state, crtc, crtc_state, i) {
492 if (!drm_mode_equal(&crtc->state->mode, &crtc_state->mode)) {
493 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] mode changed\n",
494 crtc->base.id, crtc->name);
495 crtc_state->mode_changed = true;
498 if (crtc->state->enable != crtc_state->enable) {
499 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enable changed\n",
500 crtc->base.id, crtc->name);
503 * For clarity this assignment is done here, but
504 * enable == 0 is only true when there are no
505 * connectors and a NULL mode.
507 * The other way around is true as well. enable != 0
508 * iff connectors are attached and a mode is set.
510 crtc_state->mode_changed = true;
511 crtc_state->connectors_changed = true;
515 ret = handle_conflicting_encoders(state, state->legacy_set_config);
519 for_each_connector_in_state(state, connector, connector_state, i) {
521 * This only sets crtc->connectors_changed for routing changes,
522 * drivers must set crtc->connectors_changed themselves when
523 * connector properties need to be updated.
525 ret = update_connector_routing(state, connector,
532 * After all the routing has been prepared we need to add in any
533 * connector which is itself unchanged, but who's crtc changes it's
534 * configuration. This must be done before calling mode_fixup in case a
535 * crtc only changed its mode but has the same set of connectors.
537 for_each_crtc_in_state(state, crtc, crtc_state, i) {
538 bool has_connectors =
539 !!crtc_state->connector_mask;
542 * We must set ->active_changed after walking connectors for
543 * otherwise an update that only changes active would result in
544 * a full modeset because update_connector_routing force that.
546 if (crtc->state->active != crtc_state->active) {
547 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active changed\n",
548 crtc->base.id, crtc->name);
549 crtc_state->active_changed = true;
552 if (!drm_atomic_crtc_needs_modeset(crtc_state))
555 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] needs all connectors, enable: %c, active: %c\n",
556 crtc->base.id, crtc->name,
557 crtc_state->enable ? 'y' : 'n',
558 crtc_state->active ? 'y' : 'n');
560 ret = drm_atomic_add_affected_connectors(state, crtc);
564 ret = drm_atomic_add_affected_planes(state, crtc);
568 if (crtc_state->enable != has_connectors) {
569 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled/connectors mismatch\n",
570 crtc->base.id, crtc->name);
576 return mode_fixup(state);
578 EXPORT_SYMBOL(drm_atomic_helper_check_modeset);
581 * drm_atomic_helper_check_planes - validate state object for planes changes
583 * @state: the driver state object
585 * Check the state object to see if the requested state is physically possible.
586 * This does all the plane update related checks using by calling into the
587 * ->atomic_check hooks provided by the driver.
589 * It also sets crtc_state->planes_changed to indicate that a crtc has
593 * Zero for success or -errno
596 drm_atomic_helper_check_planes(struct drm_device *dev,
597 struct drm_atomic_state *state)
599 struct drm_crtc *crtc;
600 struct drm_crtc_state *crtc_state;
601 struct drm_plane *plane;
602 struct drm_plane_state *plane_state;
605 for_each_plane_in_state(state, plane, plane_state, i) {
606 const struct drm_plane_helper_funcs *funcs;
608 funcs = plane->helper_private;
610 drm_atomic_helper_plane_changed(state, plane_state, plane);
612 if (!funcs || !funcs->atomic_check)
615 ret = funcs->atomic_check(plane, plane_state);
617 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic driver check failed\n",
618 plane->base.id, plane->name);
623 for_each_crtc_in_state(state, crtc, crtc_state, i) {
624 const struct drm_crtc_helper_funcs *funcs;
626 funcs = crtc->helper_private;
628 if (!funcs || !funcs->atomic_check)
631 ret = funcs->atomic_check(crtc, crtc_state);
633 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic driver check failed\n",
634 crtc->base.id, crtc->name);
641 EXPORT_SYMBOL(drm_atomic_helper_check_planes);
644 * drm_atomic_helper_check - validate state object
646 * @state: the driver state object
648 * Check the state object to see if the requested state is physically possible.
649 * Only crtcs and planes have check callbacks, so for any additional (global)
650 * checking that a driver needs it can simply wrap that around this function.
651 * Drivers without such needs can directly use this as their ->atomic_check()
654 * This just wraps the two parts of the state checking for planes and modeset
655 * state in the default order: First it calls drm_atomic_helper_check_modeset()
656 * and then drm_atomic_helper_check_planes(). The assumption is that the
657 * ->atomic_check functions depend upon an updated adjusted_mode.clock to
658 * e.g. properly compute watermarks.
661 * Zero for success or -errno
663 int drm_atomic_helper_check(struct drm_device *dev,
664 struct drm_atomic_state *state)
668 ret = drm_atomic_helper_check_modeset(dev, state);
672 ret = drm_atomic_helper_check_planes(dev, state);
678 EXPORT_SYMBOL(drm_atomic_helper_check);
681 disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state)
683 struct drm_connector *connector;
684 struct drm_connector_state *old_conn_state;
685 struct drm_crtc *crtc;
686 struct drm_crtc_state *old_crtc_state;
689 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
690 const struct drm_encoder_helper_funcs *funcs;
691 struct drm_encoder *encoder;
693 /* Shut down everything that's in the changeset and currently
694 * still on. So need to check the old, saved state. */
695 if (!old_conn_state->crtc)
698 old_crtc_state = drm_atomic_get_existing_crtc_state(old_state,
699 old_conn_state->crtc);
701 if (!old_crtc_state->active ||
702 !drm_atomic_crtc_needs_modeset(old_conn_state->crtc->state))
705 encoder = old_conn_state->best_encoder;
707 /* We shouldn't get this far if we didn't previously have
708 * an encoder.. but WARN_ON() rather than explode.
710 if (WARN_ON(!encoder))
713 funcs = encoder->helper_private;
715 DRM_DEBUG_ATOMIC("disabling [ENCODER:%d:%s]\n",
716 encoder->base.id, encoder->name);
719 * Each encoder has at most one connector (since we always steal
720 * it away), so we won't call disable hooks twice.
722 drm_bridge_disable(encoder->bridge);
724 /* Right function depends upon target state. */
726 if (connector->state->crtc && funcs->prepare)
727 funcs->prepare(encoder);
728 else if (funcs->disable)
729 funcs->disable(encoder);
730 else if (funcs->dpms)
731 funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
734 drm_bridge_post_disable(encoder->bridge);
737 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
738 const struct drm_crtc_helper_funcs *funcs;
740 /* Shut down everything that needs a full modeset. */
741 if (!drm_atomic_crtc_needs_modeset(crtc->state))
744 if (!old_crtc_state->active)
747 funcs = crtc->helper_private;
749 DRM_DEBUG_ATOMIC("disabling [CRTC:%d:%s]\n",
750 crtc->base.id, crtc->name);
753 /* Right function depends upon target state. */
754 if (crtc->state->enable && funcs->prepare)
755 funcs->prepare(crtc);
756 else if (funcs->atomic_disable)
757 funcs->atomic_disable(crtc, old_crtc_state);
758 else if (funcs->disable)
759 funcs->disable(crtc);
761 funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
766 * drm_atomic_helper_update_legacy_modeset_state - update legacy modeset state
768 * @old_state: atomic state object with old state structures
770 * This function updates all the various legacy modeset state pointers in
771 * connectors, encoders and crtcs. It also updates the timestamping constants
772 * used for precise vblank timestamps by calling
773 * drm_calc_timestamping_constants().
775 * Drivers can use this for building their own atomic commit if they don't have
776 * a pure helper-based modeset implementation.
779 drm_atomic_helper_update_legacy_modeset_state(struct drm_device *dev,
780 struct drm_atomic_state *old_state)
782 struct drm_connector *connector;
783 struct drm_connector_state *old_conn_state;
784 struct drm_crtc *crtc;
785 struct drm_crtc_state *old_crtc_state;
788 /* clear out existing links and update dpms */
789 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
790 if (connector->encoder) {
791 WARN_ON(!connector->encoder->crtc);
793 connector->encoder->crtc = NULL;
794 connector->encoder = NULL;
797 crtc = connector->state->crtc;
798 if ((!crtc && old_conn_state->crtc) ||
799 (crtc && drm_atomic_crtc_needs_modeset(crtc->state))) {
800 struct drm_property *dpms_prop =
801 dev->mode_config.dpms_property;
802 int mode = DRM_MODE_DPMS_OFF;
804 if (crtc && crtc->state->active)
805 mode = DRM_MODE_DPMS_ON;
807 connector->dpms = mode;
808 drm_object_property_set_value(&connector->base,
814 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
815 if (!connector->state->crtc)
818 if (WARN_ON(!connector->state->best_encoder))
821 connector->encoder = connector->state->best_encoder;
822 connector->encoder->crtc = connector->state->crtc;
825 /* set legacy state in the crtc structure */
826 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
827 struct drm_plane *primary = crtc->primary;
829 crtc->mode = crtc->state->mode;
830 crtc->enabled = crtc->state->enable;
832 if (drm_atomic_get_existing_plane_state(old_state, primary) &&
833 primary->state->crtc == crtc) {
834 crtc->x = primary->state->src_x >> 16;
835 crtc->y = primary->state->src_y >> 16;
838 if (crtc->state->enable)
839 drm_calc_timestamping_constants(crtc,
840 &crtc->state->adjusted_mode);
843 EXPORT_SYMBOL(drm_atomic_helper_update_legacy_modeset_state);
846 crtc_set_mode(struct drm_device *dev, struct drm_atomic_state *old_state)
848 struct drm_crtc *crtc;
849 struct drm_crtc_state *old_crtc_state;
850 struct drm_connector *connector;
851 struct drm_connector_state *old_conn_state;
854 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
855 const struct drm_crtc_helper_funcs *funcs;
857 if (!crtc->state->mode_changed)
860 funcs = crtc->helper_private;
862 if (crtc->state->enable && funcs->mode_set_nofb) {
863 DRM_DEBUG_ATOMIC("modeset on [CRTC:%d:%s]\n",
864 crtc->base.id, crtc->name);
866 funcs->mode_set_nofb(crtc);
870 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
871 const struct drm_encoder_helper_funcs *funcs;
872 struct drm_crtc_state *new_crtc_state;
873 struct drm_encoder *encoder;
874 struct drm_display_mode *mode, *adjusted_mode;
876 if (!connector->state->best_encoder)
879 encoder = connector->state->best_encoder;
880 funcs = encoder->helper_private;
881 new_crtc_state = connector->state->crtc->state;
882 mode = &new_crtc_state->mode;
883 adjusted_mode = &new_crtc_state->adjusted_mode;
885 if (!new_crtc_state->mode_changed)
888 DRM_DEBUG_ATOMIC("modeset on [ENCODER:%d:%s]\n",
889 encoder->base.id, encoder->name);
892 * Each encoder has at most one connector (since we always steal
893 * it away), so we won't call mode_set hooks twice.
895 if (funcs && funcs->atomic_mode_set) {
896 funcs->atomic_mode_set(encoder, new_crtc_state,
898 } else if (funcs && funcs->mode_set) {
899 funcs->mode_set(encoder, mode, adjusted_mode);
902 drm_bridge_mode_set(encoder->bridge, mode, adjusted_mode);
907 * drm_atomic_helper_commit_modeset_disables - modeset commit to disable outputs
909 * @old_state: atomic state object with old state structures
911 * This function shuts down all the outputs that need to be shut down and
912 * prepares them (if required) with the new mode.
914 * For compatibility with legacy crtc helpers this should be called before
915 * drm_atomic_helper_commit_planes(), which is what the default commit function
916 * does. But drivers with different needs can group the modeset commits together
917 * and do the plane commits at the end. This is useful for drivers doing runtime
918 * PM since planes updates then only happen when the CRTC is actually enabled.
920 void drm_atomic_helper_commit_modeset_disables(struct drm_device *dev,
921 struct drm_atomic_state *old_state)
923 disable_outputs(dev, old_state);
925 drm_atomic_helper_update_legacy_modeset_state(dev, old_state);
927 crtc_set_mode(dev, old_state);
929 EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_disables);
932 * drm_atomic_helper_commit_modeset_enables - modeset commit to enable outputs
934 * @old_state: atomic state object with old state structures
936 * This function enables all the outputs with the new configuration which had to
937 * be turned off for the update.
939 * For compatibility with legacy crtc helpers this should be called after
940 * drm_atomic_helper_commit_planes(), which is what the default commit function
941 * does. But drivers with different needs can group the modeset commits together
942 * and do the plane commits at the end. This is useful for drivers doing runtime
943 * PM since planes updates then only happen when the CRTC is actually enabled.
945 void drm_atomic_helper_commit_modeset_enables(struct drm_device *dev,
946 struct drm_atomic_state *old_state)
948 struct drm_crtc *crtc;
949 struct drm_crtc_state *old_crtc_state;
950 struct drm_connector *connector;
951 struct drm_connector_state *old_conn_state;
954 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
955 const struct drm_crtc_helper_funcs *funcs;
957 /* Need to filter out CRTCs where only planes change. */
958 if (!drm_atomic_crtc_needs_modeset(crtc->state))
961 if (!crtc->state->active)
964 funcs = crtc->helper_private;
966 if (crtc->state->enable) {
967 DRM_DEBUG_ATOMIC("enabling [CRTC:%d:%s]\n",
968 crtc->base.id, crtc->name);
977 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
978 const struct drm_encoder_helper_funcs *funcs;
979 struct drm_encoder *encoder;
981 if (!connector->state->best_encoder)
984 if (!connector->state->crtc->state->active ||
985 !drm_atomic_crtc_needs_modeset(connector->state->crtc->state))
988 encoder = connector->state->best_encoder;
989 funcs = encoder->helper_private;
991 DRM_DEBUG_ATOMIC("enabling [ENCODER:%d:%s]\n",
992 encoder->base.id, encoder->name);
995 * Each encoder has at most one connector (since we always steal
996 * it away), so we won't call enable hooks twice.
998 drm_bridge_pre_enable(encoder->bridge);
1002 funcs->enable(encoder);
1003 else if (funcs->commit)
1004 funcs->commit(encoder);
1007 drm_bridge_enable(encoder->bridge);
1010 EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_enables);
1013 * drm_atomic_helper_wait_for_fences - wait for fences stashed in plane state
1015 * @state: atomic state object with old state structures
1016 * @pre_swap: If true, do an interruptible wait, and @state is the new state.
1017 * Otherwise @state is the old state.
1019 * For implicit sync, driver should fish the exclusive fence out from the
1020 * incoming fb's and stash it in the drm_plane_state. This is called after
1021 * drm_atomic_helper_swap_state() so it uses the current plane state (and
1022 * just uses the atomic state to find the changed planes)
1024 * Note that @pre_swap is needed since the point where we block for fences moves
1025 * around depending upon whether an atomic commit is blocking or
1026 * non-blocking. For async commit all waiting needs to happen after
1027 * drm_atomic_helper_swap_state() is called, but for synchronous commits we want
1028 * to wait **before** we do anything that can't be easily rolled back. That is
1029 * before we call drm_atomic_helper_swap_state().
1031 * Returns zero if success or < 0 if dma_fence_wait() fails.
1033 int drm_atomic_helper_wait_for_fences(struct drm_device *dev,
1034 struct drm_atomic_state *state,
1037 struct drm_plane *plane;
1038 struct drm_plane_state *plane_state;
1041 for_each_plane_in_state(state, plane, plane_state, i) {
1043 plane_state = plane->state;
1045 if (!plane_state->fence)
1048 WARN_ON(!plane_state->fb);
1051 * If waiting for fences pre-swap (ie: nonblock), userspace can
1052 * still interrupt the operation. Instead of blocking until the
1053 * timer expires, make the wait interruptible.
1055 ret = dma_fence_wait(plane_state->fence, pre_swap);
1059 dma_fence_put(plane_state->fence);
1060 plane_state->fence = NULL;
1065 EXPORT_SYMBOL(drm_atomic_helper_wait_for_fences);
1068 * drm_atomic_helper_wait_for_vblanks - wait for vblank on crtcs
1070 * @old_state: atomic state object with old state structures
1072 * Helper to, after atomic commit, wait for vblanks on all effected
1073 * crtcs (ie. before cleaning up old framebuffers using
1074 * drm_atomic_helper_cleanup_planes()). It will only wait on crtcs where the
1075 * framebuffers have actually changed to optimize for the legacy cursor and
1076 * plane update use-case.
1079 drm_atomic_helper_wait_for_vblanks(struct drm_device *dev,
1080 struct drm_atomic_state *old_state)
1082 struct drm_crtc *crtc;
1083 struct drm_crtc_state *old_crtc_state;
1085 unsigned crtc_mask = 0;
1088 * Legacy cursor ioctls are completely unsynced, and userspace
1089 * relies on that (by doing tons of cursor updates).
1091 if (old_state->legacy_cursor_update)
1094 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
1095 struct drm_crtc_state *new_crtc_state = crtc->state;
1097 if (!new_crtc_state->active || !new_crtc_state->planes_changed)
1100 ret = drm_crtc_vblank_get(crtc);
1104 crtc_mask |= drm_crtc_mask(crtc);
1105 old_state->crtcs[i].last_vblank_count = drm_crtc_vblank_count(crtc);
1108 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
1109 if (!(crtc_mask & drm_crtc_mask(crtc)))
1112 ret = wait_event_timeout(dev->vblank[i].queue,
1113 old_state->crtcs[i].last_vblank_count !=
1114 drm_crtc_vblank_count(crtc),
1115 msecs_to_jiffies(50));
1117 WARN(!ret, "[CRTC:%d] vblank wait timed out\n", crtc->base.id);
1119 drm_crtc_vblank_put(crtc);
1122 EXPORT_SYMBOL(drm_atomic_helper_wait_for_vblanks);
1125 * drm_atomic_helper_commit_tail - commit atomic update to hardware
1126 * @old_state: atomic state object with old state structures
1128 * This is the default implemenation for the ->atomic_commit_tail() hook of the
1129 * &drm_mode_config_helper_funcs vtable.
1131 * Note that the default ordering of how the various stages are called is to
1132 * match the legacy modeset helper library closest. One peculiarity of that is
1133 * that it doesn't mesh well with runtime PM at all.
1135 * For drivers supporting runtime PM the recommended sequence is instead ::
1137 * drm_atomic_helper_commit_modeset_disables(dev, old_state);
1139 * drm_atomic_helper_commit_modeset_enables(dev, old_state);
1141 * drm_atomic_helper_commit_planes(dev, old_state,
1142 * DRM_PLANE_COMMIT_ACTIVE_ONLY);
1144 * for committing the atomic update to hardware. See the kerneldoc entries for
1145 * these three functions for more details.
1147 void drm_atomic_helper_commit_tail(struct drm_atomic_state *old_state)
1149 struct drm_device *dev = old_state->dev;
1151 drm_atomic_helper_commit_modeset_disables(dev, old_state);
1153 drm_atomic_helper_commit_planes(dev, old_state, 0);
1155 drm_atomic_helper_commit_modeset_enables(dev, old_state);
1157 drm_atomic_helper_commit_hw_done(old_state);
1159 drm_atomic_helper_wait_for_vblanks(dev, old_state);
1161 drm_atomic_helper_cleanup_planes(dev, old_state);
1163 EXPORT_SYMBOL(drm_atomic_helper_commit_tail);
1165 static void commit_tail(struct drm_atomic_state *old_state)
1167 struct drm_device *dev = old_state->dev;
1168 struct drm_mode_config_helper_funcs *funcs;
1170 funcs = dev->mode_config.helper_private;
1172 drm_atomic_helper_wait_for_fences(dev, old_state, false);
1174 drm_atomic_helper_wait_for_dependencies(old_state);
1176 if (funcs && funcs->atomic_commit_tail)
1177 funcs->atomic_commit_tail(old_state);
1179 drm_atomic_helper_commit_tail(old_state);
1181 drm_atomic_helper_commit_cleanup_done(old_state);
1183 drm_atomic_state_put(old_state);
1186 static void commit_work(struct work_struct *work)
1188 struct drm_atomic_state *state = container_of(work,
1189 struct drm_atomic_state,
1195 * drm_atomic_helper_commit - commit validated state object
1197 * @state: the driver state object
1198 * @nonblock: whether nonblocking behavior is requested.
1200 * This function commits a with drm_atomic_helper_check() pre-validated state
1201 * object. This can still fail when e.g. the framebuffer reservation fails. This
1202 * function implements nonblocking commits, using
1203 * drm_atomic_helper_setup_commit() and related functions.
1205 * Committing the actual hardware state is done through the
1206 * ->atomic_commit_tail() callback of the &drm_mode_config_helper_funcs vtable,
1207 * or it's default implementation drm_atomic_helper_commit_tail().
1210 * Zero for success or -errno.
1212 int drm_atomic_helper_commit(struct drm_device *dev,
1213 struct drm_atomic_state *state,
1218 ret = drm_atomic_helper_setup_commit(state, nonblock);
1222 INIT_WORK(&state->commit_work, commit_work);
1224 ret = drm_atomic_helper_prepare_planes(dev, state);
1229 ret = drm_atomic_helper_wait_for_fences(dev, state, true);
1235 * This is the point of no return - everything below never fails except
1236 * when the hw goes bonghits. Which means we can commit the new state on
1237 * the software side now.
1240 drm_atomic_helper_swap_state(state, true);
1243 * Everything below can be run asynchronously without the need to grab
1244 * any modeset locks at all under one condition: It must be guaranteed
1245 * that the asynchronous work has either been cancelled (if the driver
1246 * supports it, which at least requires that the framebuffers get
1247 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
1248 * before the new state gets committed on the software side with
1249 * drm_atomic_helper_swap_state().
1251 * This scheme allows new atomic state updates to be prepared and
1252 * checked in parallel to the asynchronous completion of the previous
1253 * update. Which is important since compositors need to figure out the
1254 * composition of the next frame right after having submitted the
1257 * NOTE: Commit work has multiple phases, first hardware commit, then
1258 * cleanup. We want them to overlap, hence need system_unbound_wq to
1259 * make sure work items don't artifically stall on each another.
1262 drm_atomic_state_get(state);
1264 queue_work(system_unbound_wq, &state->commit_work);
1270 EXPORT_SYMBOL(drm_atomic_helper_commit);
1273 * DOC: implementing nonblocking commit
1275 * Nonblocking atomic commits have to be implemented in the following sequence:
1277 * 1. Run drm_atomic_helper_prepare_planes() first. This is the only function
1278 * which commit needs to call which can fail, so we want to run it first and
1281 * 2. Synchronize with any outstanding nonblocking commit worker threads which
1282 * might be affected the new state update. This can be done by either cancelling
1283 * or flushing the work items, depending upon whether the driver can deal with
1284 * cancelled updates. Note that it is important to ensure that the framebuffer
1285 * cleanup is still done when cancelling.
1287 * Asynchronous workers need to have sufficient parallelism to be able to run
1288 * different atomic commits on different CRTCs in parallel. The simplest way to
1289 * achive this is by running them on the &system_unbound_wq work queue. Note
1290 * that drivers are not required to split up atomic commits and run an
1291 * individual commit in parallel - userspace is supposed to do that if it cares.
1292 * But it might be beneficial to do that for modesets, since those necessarily
1293 * must be done as one global operation, and enabling or disabling a CRTC can
1294 * take a long time. But even that is not required.
1296 * 3. The software state is updated synchronously with
1297 * drm_atomic_helper_swap_state(). Doing this under the protection of all modeset
1298 * locks means concurrent callers never see inconsistent state. And doing this
1299 * while it's guaranteed that no relevant nonblocking worker runs means that
1300 * nonblocking workers do not need grab any locks. Actually they must not grab
1301 * locks, for otherwise the work flushing will deadlock.
1303 * 4. Schedule a work item to do all subsequent steps, using the split-out
1304 * commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and
1305 * then cleaning up the framebuffers after the old framebuffer is no longer
1308 * The above scheme is implemented in the atomic helper libraries in
1309 * drm_atomic_helper_commit() using a bunch of helper functions. See
1310 * drm_atomic_helper_setup_commit() for a starting point.
1313 static int stall_checks(struct drm_crtc *crtc, bool nonblock)
1315 struct drm_crtc_commit *commit, *stall_commit = NULL;
1316 bool completed = true;
1320 spin_lock(&crtc->commit_lock);
1322 list_for_each_entry(commit, &crtc->commit_list, commit_entry) {
1324 completed = try_wait_for_completion(&commit->flip_done);
1325 /* Userspace is not allowed to get ahead of the previous
1326 * commit with nonblocking ones. */
1327 if (!completed && nonblock) {
1328 spin_unlock(&crtc->commit_lock);
1331 } else if (i == 1) {
1332 stall_commit = commit;
1333 drm_crtc_commit_get(stall_commit);
1339 spin_unlock(&crtc->commit_lock);
1344 /* We don't want to let commits get ahead of cleanup work too much,
1345 * stalling on 2nd previous commit means triple-buffer won't ever stall.
1347 ret = wait_for_completion_interruptible_timeout(&stall_commit->cleanup_done,
1350 DRM_ERROR("[CRTC:%d:%s] cleanup_done timed out\n",
1351 crtc->base.id, crtc->name);
1353 drm_crtc_commit_put(stall_commit);
1355 return ret < 0 ? ret : 0;
1358 static void release_crtc_commit(struct completion *completion)
1360 struct drm_crtc_commit *commit = container_of(completion,
1364 drm_crtc_commit_put(commit);
1368 * drm_atomic_helper_setup_commit - setup possibly nonblocking commit
1369 * @state: new modeset state to be committed
1370 * @nonblock: whether nonblocking behavior is requested.
1372 * This function prepares @state to be used by the atomic helper's support for
1373 * nonblocking commits. Drivers using the nonblocking commit infrastructure
1374 * should always call this function from their ->atomic_commit hook.
1376 * To be able to use this support drivers need to use a few more helper
1377 * functions. drm_atomic_helper_wait_for_dependencies() must be called before
1378 * actually committing the hardware state, and for nonblocking commits this call
1379 * must be placed in the async worker. See also drm_atomic_helper_swap_state()
1380 * and it's stall parameter, for when a driver's commit hooks look at the
1381 * ->state pointers of &struct drm_crtc, &drm_plane or &drm_connector directly.
1383 * Completion of the hardware commit step must be signalled using
1384 * drm_atomic_helper_commit_hw_done(). After this step the driver is not allowed
1385 * to read or change any permanent software or hardware modeset state. The only
1386 * exception is state protected by other means than &drm_modeset_lock locks.
1387 * Only the free standing @state with pointers to the old state structures can
1388 * be inspected, e.g. to clean up old buffers using
1389 * drm_atomic_helper_cleanup_planes().
1391 * At the very end, before cleaning up @state drivers must call
1392 * drm_atomic_helper_commit_cleanup_done().
1394 * This is all implemented by in drm_atomic_helper_commit(), giving drivers a
1395 * complete and esay-to-use default implementation of the atomic_commit() hook.
1397 * The tracking of asynchronously executed and still pending commits is done
1398 * using the core structure &drm_crtc_commit.
1400 * By default there's no need to clean up resources allocated by this function
1401 * explicitly: drm_atomic_state_default_clear() will take care of that
1406 * 0 on success. -EBUSY when userspace schedules nonblocking commits too fast,
1407 * -ENOMEM on allocation failures and -EINTR when a signal is pending.
1409 int drm_atomic_helper_setup_commit(struct drm_atomic_state *state,
1412 struct drm_crtc *crtc;
1413 struct drm_crtc_state *crtc_state;
1414 struct drm_crtc_commit *commit;
1417 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1418 commit = kzalloc(sizeof(*commit), GFP_KERNEL);
1422 init_completion(&commit->flip_done);
1423 init_completion(&commit->hw_done);
1424 init_completion(&commit->cleanup_done);
1425 INIT_LIST_HEAD(&commit->commit_entry);
1426 kref_init(&commit->ref);
1427 commit->crtc = crtc;
1429 state->crtcs[i].commit = commit;
1431 ret = stall_checks(crtc, nonblock);
1435 /* Drivers only send out events when at least either current or
1436 * new CRTC state is active. Complete right away if everything
1438 if (!crtc->state->active && !crtc_state->active) {
1439 complete_all(&commit->flip_done);
1443 /* Legacy cursor updates are fully unsynced. */
1444 if (state->legacy_cursor_update) {
1445 complete_all(&commit->flip_done);
1449 if (!crtc_state->event) {
1450 commit->event = kzalloc(sizeof(*commit->event),
1455 crtc_state->event = commit->event;
1458 crtc_state->event->base.completion = &commit->flip_done;
1459 crtc_state->event->base.completion_release = release_crtc_commit;
1460 drm_crtc_commit_get(commit);
1465 EXPORT_SYMBOL(drm_atomic_helper_setup_commit);
1468 static struct drm_crtc_commit *preceeding_commit(struct drm_crtc *crtc)
1470 struct drm_crtc_commit *commit;
1473 list_for_each_entry(commit, &crtc->commit_list, commit_entry) {
1474 /* skip the first entry, that's the current commit */
1484 * drm_atomic_helper_wait_for_dependencies - wait for required preceeding commits
1485 * @old_state: atomic state object with old state structures
1487 * This function waits for all preceeding commits that touch the same CRTC as
1488 * @old_state to both be committed to the hardware (as signalled by
1489 * drm_atomic_helper_commit_hw_done) and executed by the hardware (as signalled
1490 * by calling drm_crtc_vblank_send_event on the event member of
1493 * This is part of the atomic helper support for nonblocking commits, see
1494 * drm_atomic_helper_setup_commit() for an overview.
1496 void drm_atomic_helper_wait_for_dependencies(struct drm_atomic_state *old_state)
1498 struct drm_crtc *crtc;
1499 struct drm_crtc_state *crtc_state;
1500 struct drm_crtc_commit *commit;
1504 for_each_crtc_in_state(old_state, crtc, crtc_state, i) {
1505 spin_lock(&crtc->commit_lock);
1506 commit = preceeding_commit(crtc);
1508 drm_crtc_commit_get(commit);
1509 spin_unlock(&crtc->commit_lock);
1514 ret = wait_for_completion_timeout(&commit->hw_done,
1517 DRM_ERROR("[CRTC:%d:%s] hw_done timed out\n",
1518 crtc->base.id, crtc->name);
1520 /* Currently no support for overwriting flips, hence
1521 * stall for previous one to execute completely. */
1522 ret = wait_for_completion_timeout(&commit->flip_done,
1525 DRM_ERROR("[CRTC:%d:%s] flip_done timed out\n",
1526 crtc->base.id, crtc->name);
1528 drm_crtc_commit_put(commit);
1531 EXPORT_SYMBOL(drm_atomic_helper_wait_for_dependencies);
1534 * drm_atomic_helper_commit_hw_done - setup possible nonblocking commit
1535 * @old_state: atomic state object with old state structures
1537 * This function is used to signal completion of the hardware commit step. After
1538 * this step the driver is not allowed to read or change any permanent software
1539 * or hardware modeset state. The only exception is state protected by other
1540 * means than &drm_modeset_lock locks.
1542 * Drivers should try to postpone any expensive or delayed cleanup work after
1543 * this function is called.
1545 * This is part of the atomic helper support for nonblocking commits, see
1546 * drm_atomic_helper_setup_commit() for an overview.
1548 void drm_atomic_helper_commit_hw_done(struct drm_atomic_state *old_state)
1550 struct drm_crtc *crtc;
1551 struct drm_crtc_state *crtc_state;
1552 struct drm_crtc_commit *commit;
1555 for_each_crtc_in_state(old_state, crtc, crtc_state, i) {
1556 commit = old_state->crtcs[i].commit;
1560 /* backend must have consumed any event by now */
1561 WARN_ON(crtc->state->event);
1562 spin_lock(&crtc->commit_lock);
1563 complete_all(&commit->hw_done);
1564 spin_unlock(&crtc->commit_lock);
1567 EXPORT_SYMBOL(drm_atomic_helper_commit_hw_done);
1570 * drm_atomic_helper_commit_cleanup_done - signal completion of commit
1571 * @old_state: atomic state object with old state structures
1573 * This signals completion of the atomic update @old_state, including any
1574 * cleanup work. If used, it must be called right before calling
1575 * drm_atomic_state_put().
1577 * This is part of the atomic helper support for nonblocking commits, see
1578 * drm_atomic_helper_setup_commit() for an overview.
1580 void drm_atomic_helper_commit_cleanup_done(struct drm_atomic_state *old_state)
1582 struct drm_crtc *crtc;
1583 struct drm_crtc_state *crtc_state;
1584 struct drm_crtc_commit *commit;
1588 for_each_crtc_in_state(old_state, crtc, crtc_state, i) {
1589 commit = old_state->crtcs[i].commit;
1590 if (WARN_ON(!commit))
1593 spin_lock(&crtc->commit_lock);
1594 complete_all(&commit->cleanup_done);
1595 WARN_ON(!try_wait_for_completion(&commit->hw_done));
1597 /* commit_list borrows our reference, need to remove before we
1598 * clean up our drm_atomic_state. But only after it actually
1599 * completed, otherwise subsequent commits won't stall properly. */
1600 if (try_wait_for_completion(&commit->flip_done))
1603 spin_unlock(&crtc->commit_lock);
1605 /* We must wait for the vblank event to signal our completion
1606 * before releasing our reference, since the vblank work does
1607 * not hold a reference of its own. */
1608 ret = wait_for_completion_timeout(&commit->flip_done,
1611 DRM_ERROR("[CRTC:%d:%s] flip_done timed out\n",
1612 crtc->base.id, crtc->name);
1614 spin_lock(&crtc->commit_lock);
1616 list_del(&commit->commit_entry);
1617 spin_unlock(&crtc->commit_lock);
1620 EXPORT_SYMBOL(drm_atomic_helper_commit_cleanup_done);
1623 * drm_atomic_helper_prepare_planes - prepare plane resources before commit
1625 * @state: atomic state object with new state structures
1627 * This function prepares plane state, specifically framebuffers, for the new
1628 * configuration. If any failure is encountered this function will call
1629 * ->cleanup_fb on any already successfully prepared framebuffer.
1632 * 0 on success, negative error code on failure.
1634 int drm_atomic_helper_prepare_planes(struct drm_device *dev,
1635 struct drm_atomic_state *state)
1637 struct drm_plane *plane;
1638 struct drm_plane_state *plane_state;
1641 for_each_plane_in_state(state, plane, plane_state, i) {
1642 const struct drm_plane_helper_funcs *funcs;
1644 funcs = plane->helper_private;
1646 if (funcs->prepare_fb) {
1647 ret = funcs->prepare_fb(plane, plane_state);
1656 for_each_plane_in_state(state, plane, plane_state, j) {
1657 const struct drm_plane_helper_funcs *funcs;
1662 funcs = plane->helper_private;
1664 if (funcs->cleanup_fb)
1665 funcs->cleanup_fb(plane, plane_state);
1670 EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
1672 static bool plane_crtc_active(const struct drm_plane_state *state)
1674 return state->crtc && state->crtc->state->active;
1678 * drm_atomic_helper_commit_planes - commit plane state
1680 * @old_state: atomic state object with old state structures
1681 * @flags: flags for committing plane state
1683 * This function commits the new plane state using the plane and atomic helper
1684 * functions for planes and crtcs. It assumes that the atomic state has already
1685 * been pushed into the relevant object state pointers, since this step can no
1688 * It still requires the global state object @old_state to know which planes and
1689 * crtcs need to be updated though.
1691 * Note that this function does all plane updates across all CRTCs in one step.
1692 * If the hardware can't support this approach look at
1693 * drm_atomic_helper_commit_planes_on_crtc() instead.
1695 * Plane parameters can be updated by applications while the associated CRTC is
1696 * disabled. The DRM/KMS core will store the parameters in the plane state,
1697 * which will be available to the driver when the CRTC is turned on. As a result
1698 * most drivers don't need to be immediately notified of plane updates for a
1701 * Unless otherwise needed, drivers are advised to set the ACTIVE_ONLY flag in
1702 * @flags in order not to receive plane update notifications related to a
1703 * disabled CRTC. This avoids the need to manually ignore plane updates in
1704 * driver code when the driver and/or hardware can't or just don't need to deal
1705 * with updates on disabled CRTCs, for example when supporting runtime PM.
1707 * Drivers may set the NO_DISABLE_AFTER_MODESET flag in @flags if the relevant
1708 * display controllers require to disable a CRTC's planes when the CRTC is
1709 * disabled. This function would skip the ->atomic_disable call for a plane if
1710 * the CRTC of the old plane state needs a modesetting operation. Of course,
1711 * the drivers need to disable the planes in their CRTC disable callbacks
1712 * since no one else would do that.
1714 * The drm_atomic_helper_commit() default implementation doesn't set the
1715 * ACTIVE_ONLY flag to most closely match the behaviour of the legacy helpers.
1716 * This should not be copied blindly by drivers.
1718 void drm_atomic_helper_commit_planes(struct drm_device *dev,
1719 struct drm_atomic_state *old_state,
1722 struct drm_crtc *crtc;
1723 struct drm_crtc_state *old_crtc_state;
1724 struct drm_plane *plane;
1725 struct drm_plane_state *old_plane_state;
1727 bool active_only = flags & DRM_PLANE_COMMIT_ACTIVE_ONLY;
1728 bool no_disable = flags & DRM_PLANE_COMMIT_NO_DISABLE_AFTER_MODESET;
1730 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
1731 const struct drm_crtc_helper_funcs *funcs;
1733 funcs = crtc->helper_private;
1735 if (!funcs || !funcs->atomic_begin)
1738 if (active_only && !crtc->state->active)
1741 funcs->atomic_begin(crtc, old_crtc_state);
1744 for_each_plane_in_state(old_state, plane, old_plane_state, i) {
1745 const struct drm_plane_helper_funcs *funcs;
1748 funcs = plane->helper_private;
1753 disabling = drm_atomic_plane_disabling(plane, old_plane_state);
1757 * Skip planes related to inactive CRTCs. If the plane
1758 * is enabled use the state of the current CRTC. If the
1759 * plane is being disabled use the state of the old
1760 * CRTC to avoid skipping planes being disabled on an
1763 if (!disabling && !plane_crtc_active(plane->state))
1765 if (disabling && !plane_crtc_active(old_plane_state))
1770 * Special-case disabling the plane if drivers support it.
1772 if (disabling && funcs->atomic_disable) {
1773 struct drm_crtc_state *crtc_state;
1775 crtc_state = old_plane_state->crtc->state;
1777 if (drm_atomic_crtc_needs_modeset(crtc_state) &&
1781 funcs->atomic_disable(plane, old_plane_state);
1782 } else if (plane->state->crtc || disabling) {
1783 funcs->atomic_update(plane, old_plane_state);
1787 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
1788 const struct drm_crtc_helper_funcs *funcs;
1790 funcs = crtc->helper_private;
1792 if (!funcs || !funcs->atomic_flush)
1795 if (active_only && !crtc->state->active)
1798 funcs->atomic_flush(crtc, old_crtc_state);
1801 EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
1804 * drm_atomic_helper_commit_planes_on_crtc - commit plane state for a crtc
1805 * @old_crtc_state: atomic state object with the old crtc state
1807 * This function commits the new plane state using the plane and atomic helper
1808 * functions for planes on the specific crtc. It assumes that the atomic state
1809 * has already been pushed into the relevant object state pointers, since this
1810 * step can no longer fail.
1812 * This function is useful when plane updates should be done crtc-by-crtc
1813 * instead of one global step like drm_atomic_helper_commit_planes() does.
1815 * This function can only be savely used when planes are not allowed to move
1816 * between different CRTCs because this function doesn't handle inter-CRTC
1817 * depencies. Callers need to ensure that either no such depencies exist,
1818 * resolve them through ordering of commit calls or through some other means.
1821 drm_atomic_helper_commit_planes_on_crtc(struct drm_crtc_state *old_crtc_state)
1823 const struct drm_crtc_helper_funcs *crtc_funcs;
1824 struct drm_crtc *crtc = old_crtc_state->crtc;
1825 struct drm_atomic_state *old_state = old_crtc_state->state;
1826 struct drm_plane *plane;
1827 unsigned plane_mask;
1829 plane_mask = old_crtc_state->plane_mask;
1830 plane_mask |= crtc->state->plane_mask;
1832 crtc_funcs = crtc->helper_private;
1833 if (crtc_funcs && crtc_funcs->atomic_begin)
1834 crtc_funcs->atomic_begin(crtc, old_crtc_state);
1836 drm_for_each_plane_mask(plane, crtc->dev, plane_mask) {
1837 struct drm_plane_state *old_plane_state =
1838 drm_atomic_get_existing_plane_state(old_state, plane);
1839 const struct drm_plane_helper_funcs *plane_funcs;
1841 plane_funcs = plane->helper_private;
1843 if (!old_plane_state || !plane_funcs)
1846 WARN_ON(plane->state->crtc && plane->state->crtc != crtc);
1848 if (drm_atomic_plane_disabling(plane, old_plane_state) &&
1849 plane_funcs->atomic_disable)
1850 plane_funcs->atomic_disable(plane, old_plane_state);
1851 else if (plane->state->crtc ||
1852 drm_atomic_plane_disabling(plane, old_plane_state))
1853 plane_funcs->atomic_update(plane, old_plane_state);
1856 if (crtc_funcs && crtc_funcs->atomic_flush)
1857 crtc_funcs->atomic_flush(crtc, old_crtc_state);
1859 EXPORT_SYMBOL(drm_atomic_helper_commit_planes_on_crtc);
1862 * drm_atomic_helper_disable_planes_on_crtc - helper to disable CRTC's planes
1863 * @old_crtc_state: atomic state object with the old CRTC state
1864 * @atomic: if set, synchronize with CRTC's atomic_begin/flush hooks
1866 * Disables all planes associated with the given CRTC. This can be
1867 * used for instance in the CRTC helper atomic_disable callback to disable
1870 * If the atomic-parameter is set the function calls the CRTC's
1871 * atomic_begin hook before and atomic_flush hook after disabling the
1874 * It is a bug to call this function without having implemented the
1875 * ->atomic_disable() plane hook.
1878 drm_atomic_helper_disable_planes_on_crtc(struct drm_crtc_state *old_crtc_state,
1881 struct drm_crtc *crtc = old_crtc_state->crtc;
1882 const struct drm_crtc_helper_funcs *crtc_funcs =
1883 crtc->helper_private;
1884 struct drm_plane *plane;
1886 if (atomic && crtc_funcs && crtc_funcs->atomic_begin)
1887 crtc_funcs->atomic_begin(crtc, NULL);
1889 drm_atomic_crtc_state_for_each_plane(plane, old_crtc_state) {
1890 const struct drm_plane_helper_funcs *plane_funcs =
1891 plane->helper_private;
1896 WARN_ON(!plane_funcs->atomic_disable);
1897 if (plane_funcs->atomic_disable)
1898 plane_funcs->atomic_disable(plane, NULL);
1901 if (atomic && crtc_funcs && crtc_funcs->atomic_flush)
1902 crtc_funcs->atomic_flush(crtc, NULL);
1904 EXPORT_SYMBOL(drm_atomic_helper_disable_planes_on_crtc);
1907 * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
1909 * @old_state: atomic state object with old state structures
1911 * This function cleans up plane state, specifically framebuffers, from the old
1912 * configuration. Hence the old configuration must be perserved in @old_state to
1913 * be able to call this function.
1915 * This function must also be called on the new state when the atomic update
1916 * fails at any point after calling drm_atomic_helper_prepare_planes().
1918 void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
1919 struct drm_atomic_state *old_state)
1921 struct drm_plane *plane;
1922 struct drm_plane_state *plane_state;
1925 for_each_plane_in_state(old_state, plane, plane_state, i) {
1926 const struct drm_plane_helper_funcs *funcs;
1928 funcs = plane->helper_private;
1930 if (funcs->cleanup_fb)
1931 funcs->cleanup_fb(plane, plane_state);
1934 EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
1937 * drm_atomic_helper_swap_state - store atomic state into current sw state
1938 * @state: atomic state
1939 * @stall: stall for proceeding commits
1941 * This function stores the atomic state into the current state pointers in all
1942 * driver objects. It should be called after all failing steps have been done
1943 * and succeeded, but before the actual hardware state is committed.
1945 * For cleanup and error recovery the current state for all changed objects will
1946 * be swaped into @state.
1948 * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
1950 * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
1952 * 2. Do any other steps that might fail.
1954 * 3. Put the staged state into the current state pointers with this function.
1956 * 4. Actually commit the hardware state.
1958 * 5. Call drm_atomic_helper_cleanup_planes() with @state, which since step 3
1959 * contains the old state. Also do any other cleanup required with that state.
1961 * @stall must be set when nonblocking commits for this driver directly access
1962 * the ->state pointer of &drm_plane, &drm_crtc or &drm_connector. With the
1963 * current atomic helpers this is almost always the case, since the helpers
1964 * don't pass the right state structures to the callbacks.
1966 void drm_atomic_helper_swap_state(struct drm_atomic_state *state,
1971 struct drm_connector *connector;
1972 struct drm_connector_state *conn_state;
1973 struct drm_crtc *crtc;
1974 struct drm_crtc_state *crtc_state;
1975 struct drm_plane *plane;
1976 struct drm_plane_state *plane_state;
1977 struct drm_crtc_commit *commit;
1980 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1981 spin_lock(&crtc->commit_lock);
1982 commit = list_first_entry_or_null(&crtc->commit_list,
1983 struct drm_crtc_commit, commit_entry);
1985 drm_crtc_commit_get(commit);
1986 spin_unlock(&crtc->commit_lock);
1991 ret = wait_for_completion_timeout(&commit->hw_done,
1994 DRM_ERROR("[CRTC:%d:%s] hw_done timed out\n",
1995 crtc->base.id, crtc->name);
1996 drm_crtc_commit_put(commit);
2000 for_each_connector_in_state(state, connector, conn_state, i) {
2001 connector->state->state = state;
2002 swap(state->connectors[i].state, connector->state);
2003 connector->state->state = NULL;
2006 for_each_crtc_in_state(state, crtc, crtc_state, i) {
2007 crtc->state->state = state;
2008 swap(state->crtcs[i].state, crtc->state);
2009 crtc->state->state = NULL;
2011 if (state->crtcs[i].commit) {
2012 spin_lock(&crtc->commit_lock);
2013 list_add(&state->crtcs[i].commit->commit_entry,
2014 &crtc->commit_list);
2015 spin_unlock(&crtc->commit_lock);
2017 state->crtcs[i].commit->event = NULL;
2021 for_each_plane_in_state(state, plane, plane_state, i) {
2022 plane->state->state = state;
2023 swap(state->planes[i].state, plane->state);
2024 plane->state->state = NULL;
2027 EXPORT_SYMBOL(drm_atomic_helper_swap_state);
2030 * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
2031 * @plane: plane object to update
2032 * @crtc: owning CRTC of owning plane
2033 * @fb: framebuffer to flip onto plane
2034 * @crtc_x: x offset of primary plane on crtc
2035 * @crtc_y: y offset of primary plane on crtc
2036 * @crtc_w: width of primary plane rectangle on crtc
2037 * @crtc_h: height of primary plane rectangle on crtc
2038 * @src_x: x offset of @fb for panning
2039 * @src_y: y offset of @fb for panning
2040 * @src_w: width of source rectangle in @fb
2041 * @src_h: height of source rectangle in @fb
2043 * Provides a default plane update handler using the atomic driver interface.
2046 * Zero on success, error code on failure
2048 int drm_atomic_helper_update_plane(struct drm_plane *plane,
2049 struct drm_crtc *crtc,
2050 struct drm_framebuffer *fb,
2051 int crtc_x, int crtc_y,
2052 unsigned int crtc_w, unsigned int crtc_h,
2053 uint32_t src_x, uint32_t src_y,
2054 uint32_t src_w, uint32_t src_h)
2056 struct drm_atomic_state *state;
2057 struct drm_plane_state *plane_state;
2060 state = drm_atomic_state_alloc(plane->dev);
2064 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2066 plane_state = drm_atomic_get_plane_state(state, plane);
2067 if (IS_ERR(plane_state)) {
2068 ret = PTR_ERR(plane_state);
2072 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
2075 drm_atomic_set_fb_for_plane(plane_state, fb);
2076 plane_state->crtc_x = crtc_x;
2077 plane_state->crtc_y = crtc_y;
2078 plane_state->crtc_w = crtc_w;
2079 plane_state->crtc_h = crtc_h;
2080 plane_state->src_x = src_x;
2081 plane_state->src_y = src_y;
2082 plane_state->src_w = src_w;
2083 plane_state->src_h = src_h;
2085 if (plane == crtc->cursor)
2086 state->legacy_cursor_update = true;
2088 ret = drm_atomic_commit(state);
2090 if (ret == -EDEADLK)
2093 drm_atomic_state_put(state);
2097 drm_atomic_state_clear(state);
2098 drm_atomic_legacy_backoff(state);
2101 * Someone might have exchanged the framebuffer while we dropped locks
2102 * in the backoff code. We need to fix up the fb refcount tracking the
2105 plane->old_fb = plane->fb;
2109 EXPORT_SYMBOL(drm_atomic_helper_update_plane);
2112 * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
2113 * @plane: plane to disable
2115 * Provides a default plane disable handler using the atomic driver interface.
2118 * Zero on success, error code on failure
2120 int drm_atomic_helper_disable_plane(struct drm_plane *plane)
2122 struct drm_atomic_state *state;
2123 struct drm_plane_state *plane_state;
2127 * FIXME: Without plane->crtc set we can't get at the implicit legacy
2128 * acquire context. The real fix will be to wire the acquire ctx through
2129 * everywhere we need it, but meanwhile prevent chaos by just skipping
2130 * this noop. The critical case is the cursor ioctls which a) only grab
2131 * crtc/cursor-plane locks (so we need the crtc to get at the right
2132 * acquire context) and b) can try to disable the plane multiple times.
2137 state = drm_atomic_state_alloc(plane->dev);
2141 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(plane->crtc);
2143 plane_state = drm_atomic_get_plane_state(state, plane);
2144 if (IS_ERR(plane_state)) {
2145 ret = PTR_ERR(plane_state);
2149 if (plane_state->crtc && (plane == plane->crtc->cursor))
2150 plane_state->state->legacy_cursor_update = true;
2152 ret = __drm_atomic_helper_disable_plane(plane, plane_state);
2156 ret = drm_atomic_commit(state);
2158 if (ret == -EDEADLK)
2161 drm_atomic_state_put(state);
2165 drm_atomic_state_clear(state);
2166 drm_atomic_legacy_backoff(state);
2169 * Someone might have exchanged the framebuffer while we dropped locks
2170 * in the backoff code. We need to fix up the fb refcount tracking the
2173 plane->old_fb = plane->fb;
2177 EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
2179 /* just used from fb-helper and atomic-helper: */
2180 int __drm_atomic_helper_disable_plane(struct drm_plane *plane,
2181 struct drm_plane_state *plane_state)
2185 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
2189 drm_atomic_set_fb_for_plane(plane_state, NULL);
2190 plane_state->crtc_x = 0;
2191 plane_state->crtc_y = 0;
2192 plane_state->crtc_w = 0;
2193 plane_state->crtc_h = 0;
2194 plane_state->src_x = 0;
2195 plane_state->src_y = 0;
2196 plane_state->src_w = 0;
2197 plane_state->src_h = 0;
2202 static int update_output_state(struct drm_atomic_state *state,
2203 struct drm_mode_set *set)
2205 struct drm_device *dev = set->crtc->dev;
2206 struct drm_crtc *crtc;
2207 struct drm_crtc_state *crtc_state;
2208 struct drm_connector *connector;
2209 struct drm_connector_state *conn_state;
2212 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
2213 state->acquire_ctx);
2217 /* First disable all connectors on the target crtc. */
2218 ret = drm_atomic_add_affected_connectors(state, set->crtc);
2222 for_each_connector_in_state(state, connector, conn_state, i) {
2223 if (conn_state->crtc == set->crtc) {
2224 ret = drm_atomic_set_crtc_for_connector(conn_state,
2231 /* Then set all connectors from set->connectors on the target crtc */
2232 for (i = 0; i < set->num_connectors; i++) {
2233 conn_state = drm_atomic_get_connector_state(state,
2234 set->connectors[i]);
2235 if (IS_ERR(conn_state))
2236 return PTR_ERR(conn_state);
2238 ret = drm_atomic_set_crtc_for_connector(conn_state,
2244 for_each_crtc_in_state(state, crtc, crtc_state, i) {
2245 /* Don't update ->enable for the CRTC in the set_config request,
2246 * since a mismatch would indicate a bug in the upper layers.
2247 * The actual modeset code later on will catch any
2248 * inconsistencies here. */
2249 if (crtc == set->crtc)
2252 if (!crtc_state->connector_mask) {
2253 ret = drm_atomic_set_mode_prop_for_crtc(crtc_state,
2258 crtc_state->active = false;
2266 * drm_atomic_helper_set_config - set a new config from userspace
2267 * @set: mode set configuration
2269 * Provides a default crtc set_config handler using the atomic driver interface.
2272 * Returns 0 on success, negative errno numbers on failure.
2274 int drm_atomic_helper_set_config(struct drm_mode_set *set)
2276 struct drm_atomic_state *state;
2277 struct drm_crtc *crtc = set->crtc;
2280 state = drm_atomic_state_alloc(crtc->dev);
2284 state->legacy_set_config = true;
2285 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2287 ret = __drm_atomic_helper_set_config(set, state);
2291 ret = drm_atomic_commit(state);
2293 if (ret == -EDEADLK)
2296 drm_atomic_state_put(state);
2300 drm_atomic_state_clear(state);
2301 drm_atomic_legacy_backoff(state);
2304 * Someone might have exchanged the framebuffer while we dropped locks
2305 * in the backoff code. We need to fix up the fb refcount tracking the
2308 crtc->primary->old_fb = crtc->primary->fb;
2312 EXPORT_SYMBOL(drm_atomic_helper_set_config);
2314 /* just used from fb-helper and atomic-helper: */
2315 int __drm_atomic_helper_set_config(struct drm_mode_set *set,
2316 struct drm_atomic_state *state)
2318 struct drm_crtc_state *crtc_state;
2319 struct drm_plane_state *primary_state;
2320 struct drm_crtc *crtc = set->crtc;
2321 int hdisplay, vdisplay;
2324 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2325 if (IS_ERR(crtc_state))
2326 return PTR_ERR(crtc_state);
2328 primary_state = drm_atomic_get_plane_state(state, crtc->primary);
2329 if (IS_ERR(primary_state))
2330 return PTR_ERR(primary_state);
2334 WARN_ON(set->num_connectors);
2336 ret = drm_atomic_set_mode_for_crtc(crtc_state, NULL);
2340 crtc_state->active = false;
2342 ret = drm_atomic_set_crtc_for_plane(primary_state, NULL);
2346 drm_atomic_set_fb_for_plane(primary_state, NULL);
2352 WARN_ON(!set->num_connectors);
2354 ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
2358 crtc_state->active = true;
2360 ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
2364 drm_crtc_get_hv_timing(set->mode, &hdisplay, &vdisplay);
2366 drm_atomic_set_fb_for_plane(primary_state, set->fb);
2367 primary_state->crtc_x = 0;
2368 primary_state->crtc_y = 0;
2369 primary_state->crtc_w = hdisplay;
2370 primary_state->crtc_h = vdisplay;
2371 primary_state->src_x = set->x << 16;
2372 primary_state->src_y = set->y << 16;
2373 if (drm_rotation_90_or_270(primary_state->rotation)) {
2374 primary_state->src_w = vdisplay << 16;
2375 primary_state->src_h = hdisplay << 16;
2377 primary_state->src_w = hdisplay << 16;
2378 primary_state->src_h = vdisplay << 16;
2382 ret = update_output_state(state, set);
2390 * drm_atomic_helper_disable_all - disable all currently active outputs
2392 * @ctx: lock acquisition context
2394 * Loops through all connectors, finding those that aren't turned off and then
2395 * turns them off by setting their DPMS mode to OFF and deactivating the CRTC
2396 * that they are connected to.
2398 * This is used for example in suspend/resume to disable all currently active
2399 * functions when suspending.
2401 * Note that if callers haven't already acquired all modeset locks this might
2402 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
2405 * 0 on success or a negative error code on failure.
2408 * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
2410 int drm_atomic_helper_disable_all(struct drm_device *dev,
2411 struct drm_modeset_acquire_ctx *ctx)
2413 struct drm_atomic_state *state;
2414 struct drm_connector *conn;
2415 struct drm_connector_list_iter conn_iter;
2418 state = drm_atomic_state_alloc(dev);
2422 state->acquire_ctx = ctx;
2424 drm_connector_list_iter_get(dev, &conn_iter);
2425 drm_for_each_connector_iter(conn, &conn_iter) {
2426 struct drm_crtc *crtc = conn->state->crtc;
2427 struct drm_crtc_state *crtc_state;
2429 if (!crtc || conn->dpms != DRM_MODE_DPMS_ON)
2432 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2433 if (IS_ERR(crtc_state)) {
2434 err = PTR_ERR(crtc_state);
2438 crtc_state->active = false;
2441 err = drm_atomic_commit(state);
2443 drm_connector_list_iter_put(&conn_iter);
2444 drm_atomic_state_put(state);
2447 EXPORT_SYMBOL(drm_atomic_helper_disable_all);
2450 * drm_atomic_helper_suspend - subsystem-level suspend helper
2453 * Duplicates the current atomic state, disables all active outputs and then
2454 * returns a pointer to the original atomic state to the caller. Drivers can
2455 * pass this pointer to the drm_atomic_helper_resume() helper upon resume to
2456 * restore the output configuration that was active at the time the system
2459 * Note that it is potentially unsafe to use this. The atomic state object
2460 * returned by this function is assumed to be persistent. Drivers must ensure
2461 * that this holds true. Before calling this function, drivers must make sure
2462 * to suspend fbdev emulation so that nothing can be using the device.
2465 * A pointer to a copy of the state before suspend on success or an ERR_PTR()-
2466 * encoded error code on failure. Drivers should store the returned atomic
2467 * state object and pass it to the drm_atomic_helper_resume() helper upon
2471 * drm_atomic_helper_duplicate_state(), drm_atomic_helper_disable_all(),
2472 * drm_atomic_helper_resume()
2474 struct drm_atomic_state *drm_atomic_helper_suspend(struct drm_device *dev)
2476 struct drm_modeset_acquire_ctx ctx;
2477 struct drm_atomic_state *state;
2480 drm_modeset_acquire_init(&ctx, 0);
2483 err = drm_modeset_lock_all_ctx(dev, &ctx);
2485 state = ERR_PTR(err);
2489 state = drm_atomic_helper_duplicate_state(dev, &ctx);
2493 err = drm_atomic_helper_disable_all(dev, &ctx);
2495 drm_atomic_state_put(state);
2496 state = ERR_PTR(err);
2501 if (PTR_ERR(state) == -EDEADLK) {
2502 drm_modeset_backoff(&ctx);
2506 drm_modeset_drop_locks(&ctx);
2507 drm_modeset_acquire_fini(&ctx);
2510 EXPORT_SYMBOL(drm_atomic_helper_suspend);
2513 * drm_atomic_helper_resume - subsystem-level resume helper
2515 * @state: atomic state to resume to
2517 * Calls drm_mode_config_reset() to synchronize hardware and software states,
2518 * grabs all modeset locks and commits the atomic state object. This can be
2519 * used in conjunction with the drm_atomic_helper_suspend() helper to
2520 * implement suspend/resume for drivers that support atomic mode-setting.
2523 * 0 on success or a negative error code on failure.
2526 * drm_atomic_helper_suspend()
2528 int drm_atomic_helper_resume(struct drm_device *dev,
2529 struct drm_atomic_state *state)
2531 struct drm_mode_config *config = &dev->mode_config;
2534 drm_mode_config_reset(dev);
2535 drm_modeset_lock_all(dev);
2536 state->acquire_ctx = config->acquire_ctx;
2537 err = drm_atomic_commit(state);
2538 drm_modeset_unlock_all(dev);
2542 EXPORT_SYMBOL(drm_atomic_helper_resume);
2545 * drm_atomic_helper_crtc_set_property - helper for crtc properties
2547 * @property: DRM property
2548 * @val: value of property
2550 * Provides a default crtc set_property handler using the atomic driver
2554 * Zero on success, error code on failure
2557 drm_atomic_helper_crtc_set_property(struct drm_crtc *crtc,
2558 struct drm_property *property,
2561 struct drm_atomic_state *state;
2562 struct drm_crtc_state *crtc_state;
2565 state = drm_atomic_state_alloc(crtc->dev);
2569 /* ->set_property is always called with all locks held. */
2570 state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
2572 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2573 if (IS_ERR(crtc_state)) {
2574 ret = PTR_ERR(crtc_state);
2578 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
2583 ret = drm_atomic_commit(state);
2585 if (ret == -EDEADLK)
2588 drm_atomic_state_put(state);
2592 drm_atomic_state_clear(state);
2593 drm_atomic_legacy_backoff(state);
2597 EXPORT_SYMBOL(drm_atomic_helper_crtc_set_property);
2600 * drm_atomic_helper_plane_set_property - helper for plane properties
2602 * @property: DRM property
2603 * @val: value of property
2605 * Provides a default plane set_property handler using the atomic driver
2609 * Zero on success, error code on failure
2612 drm_atomic_helper_plane_set_property(struct drm_plane *plane,
2613 struct drm_property *property,
2616 struct drm_atomic_state *state;
2617 struct drm_plane_state *plane_state;
2620 state = drm_atomic_state_alloc(plane->dev);
2624 /* ->set_property is always called with all locks held. */
2625 state->acquire_ctx = plane->dev->mode_config.acquire_ctx;
2627 plane_state = drm_atomic_get_plane_state(state, plane);
2628 if (IS_ERR(plane_state)) {
2629 ret = PTR_ERR(plane_state);
2633 ret = drm_atomic_plane_set_property(plane, plane_state,
2638 ret = drm_atomic_commit(state);
2640 if (ret == -EDEADLK)
2643 drm_atomic_state_put(state);
2647 drm_atomic_state_clear(state);
2648 drm_atomic_legacy_backoff(state);
2652 EXPORT_SYMBOL(drm_atomic_helper_plane_set_property);
2655 * drm_atomic_helper_connector_set_property - helper for connector properties
2656 * @connector: DRM connector
2657 * @property: DRM property
2658 * @val: value of property
2660 * Provides a default connector set_property handler using the atomic driver
2664 * Zero on success, error code on failure
2667 drm_atomic_helper_connector_set_property(struct drm_connector *connector,
2668 struct drm_property *property,
2671 struct drm_atomic_state *state;
2672 struct drm_connector_state *connector_state;
2675 state = drm_atomic_state_alloc(connector->dev);
2679 /* ->set_property is always called with all locks held. */
2680 state->acquire_ctx = connector->dev->mode_config.acquire_ctx;
2682 connector_state = drm_atomic_get_connector_state(state, connector);
2683 if (IS_ERR(connector_state)) {
2684 ret = PTR_ERR(connector_state);
2688 ret = drm_atomic_connector_set_property(connector, connector_state,
2693 ret = drm_atomic_commit(state);
2695 if (ret == -EDEADLK)
2698 drm_atomic_state_put(state);
2702 drm_atomic_state_clear(state);
2703 drm_atomic_legacy_backoff(state);
2707 EXPORT_SYMBOL(drm_atomic_helper_connector_set_property);
2709 static int page_flip_common(
2710 struct drm_atomic_state *state,
2711 struct drm_crtc *crtc,
2712 struct drm_framebuffer *fb,
2713 struct drm_pending_vblank_event *event)
2715 struct drm_plane *plane = crtc->primary;
2716 struct drm_plane_state *plane_state;
2717 struct drm_crtc_state *crtc_state;
2720 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2721 if (IS_ERR(crtc_state))
2722 return PTR_ERR(crtc_state);
2724 crtc_state->event = event;
2726 plane_state = drm_atomic_get_plane_state(state, plane);
2727 if (IS_ERR(plane_state))
2728 return PTR_ERR(plane_state);
2731 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
2734 drm_atomic_set_fb_for_plane(plane_state, fb);
2736 /* Make sure we don't accidentally do a full modeset. */
2737 state->allow_modeset = false;
2738 if (!crtc_state->active) {
2739 DRM_DEBUG_ATOMIC("[CRTC:%d] disabled, rejecting legacy flip\n",
2748 * drm_atomic_helper_page_flip - execute a legacy page flip
2750 * @fb: DRM framebuffer
2751 * @event: optional DRM event to signal upon completion
2752 * @flags: flip flags for non-vblank sync'ed updates
2754 * Provides a default &drm_crtc_funcs.page_flip implementation
2755 * using the atomic driver interface.
2757 * Note that for now so called async page flips (i.e. updates which are not
2758 * synchronized to vblank) are not supported, since the atomic interfaces have
2759 * no provisions for this yet.
2762 * Returns 0 on success, negative errno numbers on failure.
2765 * drm_atomic_helper_page_flip_target()
2767 int drm_atomic_helper_page_flip(struct drm_crtc *crtc,
2768 struct drm_framebuffer *fb,
2769 struct drm_pending_vblank_event *event,
2772 struct drm_plane *plane = crtc->primary;
2773 struct drm_atomic_state *state;
2776 if (flags & DRM_MODE_PAGE_FLIP_ASYNC)
2779 state = drm_atomic_state_alloc(plane->dev);
2783 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2786 ret = page_flip_common(state, crtc, fb, event);
2790 ret = drm_atomic_nonblocking_commit(state);
2793 if (ret == -EDEADLK)
2796 drm_atomic_state_put(state);
2800 drm_atomic_state_clear(state);
2801 drm_atomic_legacy_backoff(state);
2804 * Someone might have exchanged the framebuffer while we dropped locks
2805 * in the backoff code. We need to fix up the fb refcount tracking the
2808 plane->old_fb = plane->fb;
2812 EXPORT_SYMBOL(drm_atomic_helper_page_flip);
2815 * drm_atomic_helper_page_flip_target - do page flip on target vblank period.
2817 * @fb: DRM framebuffer
2818 * @event: optional DRM event to signal upon completion
2819 * @flags: flip flags for non-vblank sync'ed updates
2820 * @target: specifying the target vblank period when the flip to take effect
2822 * Provides a default &drm_crtc_funcs.page_flip_target implementation.
2823 * Similar to drm_atomic_helper_page_flip() with extra parameter to specify
2824 * target vblank period to flip.
2827 * Returns 0 on success, negative errno numbers on failure.
2829 int drm_atomic_helper_page_flip_target(
2830 struct drm_crtc *crtc,
2831 struct drm_framebuffer *fb,
2832 struct drm_pending_vblank_event *event,
2836 struct drm_plane *plane = crtc->primary;
2837 struct drm_atomic_state *state;
2838 struct drm_crtc_state *crtc_state;
2841 if (flags & DRM_MODE_PAGE_FLIP_ASYNC)
2844 state = drm_atomic_state_alloc(plane->dev);
2848 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2851 ret = page_flip_common(state, crtc, fb, event);
2855 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
2856 if (WARN_ON(!crtc_state)) {
2860 crtc_state->target_vblank = target;
2862 ret = drm_atomic_nonblocking_commit(state);
2865 if (ret == -EDEADLK)
2868 drm_atomic_state_put(state);
2872 drm_atomic_state_clear(state);
2873 drm_atomic_legacy_backoff(state);
2876 * Someone might have exchanged the framebuffer while we dropped locks
2877 * in the backoff code. We need to fix up the fb refcount tracking the
2880 plane->old_fb = plane->fb;
2884 EXPORT_SYMBOL(drm_atomic_helper_page_flip_target);
2887 * drm_atomic_helper_connector_dpms() - connector dpms helper implementation
2888 * @connector: affected connector
2891 * This is the main helper function provided by the atomic helper framework for
2892 * implementing the legacy DPMS connector interface. It computes the new desired
2893 * ->active state for the corresponding CRTC (if the connector is enabled) and
2897 * Returns 0 on success, negative errno numbers on failure.
2899 int drm_atomic_helper_connector_dpms(struct drm_connector *connector,
2902 struct drm_mode_config *config = &connector->dev->mode_config;
2903 struct drm_atomic_state *state;
2904 struct drm_crtc_state *crtc_state;
2905 struct drm_crtc *crtc;
2906 struct drm_connector *tmp_connector;
2907 struct drm_connector_list_iter conn_iter;
2909 bool active = false;
2910 int old_mode = connector->dpms;
2912 if (mode != DRM_MODE_DPMS_ON)
2913 mode = DRM_MODE_DPMS_OFF;
2915 connector->dpms = mode;
2916 crtc = connector->state->crtc;
2921 state = drm_atomic_state_alloc(connector->dev);
2925 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2927 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2928 if (IS_ERR(crtc_state)) {
2929 ret = PTR_ERR(crtc_state);
2933 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
2935 drm_connector_list_iter_get(connector->dev, &conn_iter);
2936 drm_for_each_connector_iter(tmp_connector, &conn_iter) {
2937 if (tmp_connector->state->crtc != crtc)
2940 if (tmp_connector->dpms == DRM_MODE_DPMS_ON) {
2945 drm_connector_list_iter_put(&conn_iter);
2946 crtc_state->active = active;
2948 ret = drm_atomic_commit(state);
2950 if (ret == -EDEADLK)
2953 connector->dpms = old_mode;
2954 drm_atomic_state_put(state);
2958 drm_atomic_state_clear(state);
2959 drm_atomic_legacy_backoff(state);
2963 EXPORT_SYMBOL(drm_atomic_helper_connector_dpms);
2966 * drm_atomic_helper_best_encoder - Helper for &drm_connector_helper_funcs
2967 * ->best_encoder callback
2968 * @connector: Connector control structure
2970 * This is a &drm_connector_helper_funcs ->best_encoder callback helper for
2971 * connectors that support exactly 1 encoder, statically determined at driver
2974 struct drm_encoder *
2975 drm_atomic_helper_best_encoder(struct drm_connector *connector)
2977 WARN_ON(connector->encoder_ids[1]);
2978 return drm_encoder_find(connector->dev, connector->encoder_ids[0]);
2980 EXPORT_SYMBOL(drm_atomic_helper_best_encoder);
2983 * DOC: atomic state reset and initialization
2985 * Both the drm core and the atomic helpers assume that there is always the full
2986 * and correct atomic software state for all connectors, CRTCs and planes
2987 * available. Which is a bit a problem on driver load and also after system
2988 * suspend. One way to solve this is to have a hardware state read-out
2989 * infrastructure which reconstructs the full software state (e.g. the i915
2992 * The simpler solution is to just reset the software state to everything off,
2993 * which is easiest to do by calling drm_mode_config_reset(). To facilitate this
2994 * the atomic helpers provide default reset implementations for all hooks.
2996 * On the upside the precise state tracking of atomic simplifies system suspend
2997 * and resume a lot. For drivers using drm_mode_config_reset() a complete recipe
2998 * is implemented in drm_atomic_helper_suspend() and drm_atomic_helper_resume().
2999 * For other drivers the building blocks are split out, see the documentation
3000 * for these functions.
3004 * drm_atomic_helper_crtc_reset - default ->reset hook for CRTCs
3007 * Resets the atomic state for @crtc by freeing the state pointer (which might
3008 * be NULL, e.g. at driver load time) and allocating a new empty state object.
3010 void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc)
3013 __drm_atomic_helper_crtc_destroy_state(crtc->state);
3016 crtc->state = kzalloc(sizeof(*crtc->state), GFP_KERNEL);
3019 crtc->state->crtc = crtc;
3021 EXPORT_SYMBOL(drm_atomic_helper_crtc_reset);
3024 * __drm_atomic_helper_crtc_duplicate_state - copy atomic CRTC state
3025 * @crtc: CRTC object
3026 * @state: atomic CRTC state
3028 * Copies atomic state from a CRTC's current state and resets inferred values.
3029 * This is useful for drivers that subclass the CRTC state.
3031 void __drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc,
3032 struct drm_crtc_state *state)
3034 memcpy(state, crtc->state, sizeof(*state));
3036 if (state->mode_blob)
3037 drm_property_reference_blob(state->mode_blob);
3038 if (state->degamma_lut)
3039 drm_property_reference_blob(state->degamma_lut);
3041 drm_property_reference_blob(state->ctm);
3042 if (state->gamma_lut)
3043 drm_property_reference_blob(state->gamma_lut);
3044 state->mode_changed = false;
3045 state->active_changed = false;
3046 state->planes_changed = false;
3047 state->connectors_changed = false;
3048 state->color_mgmt_changed = false;
3049 state->zpos_changed = false;
3050 state->event = NULL;
3052 EXPORT_SYMBOL(__drm_atomic_helper_crtc_duplicate_state);
3055 * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
3058 * Default CRTC state duplicate hook for drivers which don't have their own
3059 * subclassed CRTC state structure.
3061 struct drm_crtc_state *
3062 drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc)
3064 struct drm_crtc_state *state;
3066 if (WARN_ON(!crtc->state))
3069 state = kmalloc(sizeof(*state), GFP_KERNEL);
3071 __drm_atomic_helper_crtc_duplicate_state(crtc, state);
3075 EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state);
3078 * __drm_atomic_helper_crtc_destroy_state - release CRTC state
3079 * @state: CRTC state object to release
3081 * Releases all resources stored in the CRTC state without actually freeing
3082 * the memory of the CRTC state. This is useful for drivers that subclass the
3085 void __drm_atomic_helper_crtc_destroy_state(struct drm_crtc_state *state)
3087 drm_property_unreference_blob(state->mode_blob);
3088 drm_property_unreference_blob(state->degamma_lut);
3089 drm_property_unreference_blob(state->ctm);
3090 drm_property_unreference_blob(state->gamma_lut);
3092 EXPORT_SYMBOL(__drm_atomic_helper_crtc_destroy_state);
3095 * drm_atomic_helper_crtc_destroy_state - default state destroy hook
3097 * @state: CRTC state object to release
3099 * Default CRTC state destroy hook for drivers which don't have their own
3100 * subclassed CRTC state structure.
3102 void drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
3103 struct drm_crtc_state *state)
3105 __drm_atomic_helper_crtc_destroy_state(state);
3108 EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state);
3111 * drm_atomic_helper_plane_reset - default ->reset hook for planes
3114 * Resets the atomic state for @plane by freeing the state pointer (which might
3115 * be NULL, e.g. at driver load time) and allocating a new empty state object.
3117 void drm_atomic_helper_plane_reset(struct drm_plane *plane)
3120 __drm_atomic_helper_plane_destroy_state(plane->state);
3122 kfree(plane->state);
3123 plane->state = kzalloc(sizeof(*plane->state), GFP_KERNEL);
3126 plane->state->plane = plane;
3127 plane->state->rotation = DRM_ROTATE_0;
3130 EXPORT_SYMBOL(drm_atomic_helper_plane_reset);
3133 * __drm_atomic_helper_plane_duplicate_state - copy atomic plane state
3134 * @plane: plane object
3135 * @state: atomic plane state
3137 * Copies atomic state from a plane's current state. This is useful for
3138 * drivers that subclass the plane state.
3140 void __drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane,
3141 struct drm_plane_state *state)
3143 memcpy(state, plane->state, sizeof(*state));
3146 drm_framebuffer_reference(state->fb);
3148 state->fence = NULL;
3150 EXPORT_SYMBOL(__drm_atomic_helper_plane_duplicate_state);
3153 * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
3156 * Default plane state duplicate hook for drivers which don't have their own
3157 * subclassed plane state structure.
3159 struct drm_plane_state *
3160 drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane)
3162 struct drm_plane_state *state;
3164 if (WARN_ON(!plane->state))
3167 state = kmalloc(sizeof(*state), GFP_KERNEL);
3169 __drm_atomic_helper_plane_duplicate_state(plane, state);
3173 EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state);
3176 * __drm_atomic_helper_plane_destroy_state - release plane state
3177 * @state: plane state object to release
3179 * Releases all resources stored in the plane state without actually freeing
3180 * the memory of the plane state. This is useful for drivers that subclass the
3183 void __drm_atomic_helper_plane_destroy_state(struct drm_plane_state *state)
3186 drm_framebuffer_unreference(state->fb);
3189 dma_fence_put(state->fence);
3191 EXPORT_SYMBOL(__drm_atomic_helper_plane_destroy_state);
3194 * drm_atomic_helper_plane_destroy_state - default state destroy hook
3196 * @state: plane state object to release
3198 * Default plane state destroy hook for drivers which don't have their own
3199 * subclassed plane state structure.
3201 void drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
3202 struct drm_plane_state *state)
3204 __drm_atomic_helper_plane_destroy_state(state);
3207 EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state);
3210 * __drm_atomic_helper_connector_reset - reset state on connector
3211 * @connector: drm connector
3212 * @conn_state: connector state to assign
3214 * Initializes the newly allocated @conn_state and assigns it to
3215 * #connector ->state, usually required when initializing the drivers
3216 * or when called from the ->reset hook.
3218 * This is useful for drivers that subclass the connector state.
3221 __drm_atomic_helper_connector_reset(struct drm_connector *connector,
3222 struct drm_connector_state *conn_state)
3225 conn_state->connector = connector;
3227 connector->state = conn_state;
3229 EXPORT_SYMBOL(__drm_atomic_helper_connector_reset);
3232 * drm_atomic_helper_connector_reset - default ->reset hook for connectors
3233 * @connector: drm connector
3235 * Resets the atomic state for @connector by freeing the state pointer (which
3236 * might be NULL, e.g. at driver load time) and allocating a new empty state
3239 void drm_atomic_helper_connector_reset(struct drm_connector *connector)
3241 struct drm_connector_state *conn_state =
3242 kzalloc(sizeof(*conn_state), GFP_KERNEL);
3244 if (connector->state)
3245 __drm_atomic_helper_connector_destroy_state(connector->state);
3247 kfree(connector->state);
3248 __drm_atomic_helper_connector_reset(connector, conn_state);
3250 EXPORT_SYMBOL(drm_atomic_helper_connector_reset);
3253 * __drm_atomic_helper_connector_duplicate_state - copy atomic connector state
3254 * @connector: connector object
3255 * @state: atomic connector state
3257 * Copies atomic state from a connector's current state. This is useful for
3258 * drivers that subclass the connector state.
3261 __drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector,
3262 struct drm_connector_state *state)
3264 memcpy(state, connector->state, sizeof(*state));
3266 drm_connector_reference(connector);
3268 EXPORT_SYMBOL(__drm_atomic_helper_connector_duplicate_state);
3271 * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
3272 * @connector: drm connector
3274 * Default connector state duplicate hook for drivers which don't have their own
3275 * subclassed connector state structure.
3277 struct drm_connector_state *
3278 drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
3280 struct drm_connector_state *state;
3282 if (WARN_ON(!connector->state))
3285 state = kmalloc(sizeof(*state), GFP_KERNEL);
3287 __drm_atomic_helper_connector_duplicate_state(connector, state);
3291 EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
3294 * drm_atomic_helper_duplicate_state - duplicate an atomic state object
3296 * @ctx: lock acquisition context
3298 * Makes a copy of the current atomic state by looping over all objects and
3299 * duplicating their respective states. This is used for example by suspend/
3300 * resume support code to save the state prior to suspend such that it can
3301 * be restored upon resume.
3303 * Note that this treats atomic state as persistent between save and restore.
3304 * Drivers must make sure that this is possible and won't result in confusion
3305 * or erroneous behaviour.
3307 * Note that if callers haven't already acquired all modeset locks this might
3308 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
3311 * A pointer to the copy of the atomic state object on success or an
3312 * ERR_PTR()-encoded error code on failure.
3315 * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
3317 struct drm_atomic_state *
3318 drm_atomic_helper_duplicate_state(struct drm_device *dev,
3319 struct drm_modeset_acquire_ctx *ctx)
3321 struct drm_atomic_state *state;
3322 struct drm_connector *conn;
3323 struct drm_connector_list_iter conn_iter;
3324 struct drm_plane *plane;
3325 struct drm_crtc *crtc;
3328 state = drm_atomic_state_alloc(dev);
3330 return ERR_PTR(-ENOMEM);
3332 state->acquire_ctx = ctx;
3334 drm_for_each_crtc(crtc, dev) {
3335 struct drm_crtc_state *crtc_state;
3337 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3338 if (IS_ERR(crtc_state)) {
3339 err = PTR_ERR(crtc_state);
3344 drm_for_each_plane(plane, dev) {
3345 struct drm_plane_state *plane_state;
3347 plane_state = drm_atomic_get_plane_state(state, plane);
3348 if (IS_ERR(plane_state)) {
3349 err = PTR_ERR(plane_state);
3354 drm_connector_list_iter_get(dev, &conn_iter);
3355 drm_for_each_connector_iter(conn, &conn_iter) {
3356 struct drm_connector_state *conn_state;
3358 conn_state = drm_atomic_get_connector_state(state, conn);
3359 if (IS_ERR(conn_state)) {
3360 err = PTR_ERR(conn_state);
3361 drm_connector_list_iter_put(&conn_iter);
3365 drm_connector_list_iter_put(&conn_iter);
3367 /* clear the acquire context so that it isn't accidentally reused */
3368 state->acquire_ctx = NULL;
3372 drm_atomic_state_put(state);
3373 state = ERR_PTR(err);
3378 EXPORT_SYMBOL(drm_atomic_helper_duplicate_state);
3381 * __drm_atomic_helper_connector_destroy_state - release connector state
3382 * @state: connector state object to release
3384 * Releases all resources stored in the connector state without actually
3385 * freeing the memory of the connector state. This is useful for drivers that
3386 * subclass the connector state.
3389 __drm_atomic_helper_connector_destroy_state(struct drm_connector_state *state)
3392 drm_connector_unreference(state->connector);
3394 EXPORT_SYMBOL(__drm_atomic_helper_connector_destroy_state);
3397 * drm_atomic_helper_connector_destroy_state - default state destroy hook
3398 * @connector: drm connector
3399 * @state: connector state object to release
3401 * Default connector state destroy hook for drivers which don't have their own
3402 * subclassed connector state structure.
3404 void drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
3405 struct drm_connector_state *state)
3407 __drm_atomic_helper_connector_destroy_state(state);
3410 EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state);
3413 * drm_atomic_helper_legacy_gamma_set - set the legacy gamma correction table
3414 * @crtc: CRTC object
3415 * @red: red correction table
3416 * @green: green correction table
3417 * @blue: green correction table
3418 * @size: size of the tables
3420 * Implements support for legacy gamma correction table for drivers
3421 * that support color management through the DEGAMMA_LUT/GAMMA_LUT
3424 int drm_atomic_helper_legacy_gamma_set(struct drm_crtc *crtc,
3425 u16 *red, u16 *green, u16 *blue,
3428 struct drm_device *dev = crtc->dev;
3429 struct drm_mode_config *config = &dev->mode_config;
3430 struct drm_atomic_state *state;
3431 struct drm_crtc_state *crtc_state;
3432 struct drm_property_blob *blob = NULL;
3433 struct drm_color_lut *blob_data;
3436 state = drm_atomic_state_alloc(crtc->dev);
3440 blob = drm_property_create_blob(dev,
3441 sizeof(struct drm_color_lut) * size,
3444 ret = PTR_ERR(blob);
3449 /* Prepare GAMMA_LUT with the legacy values. */
3450 blob_data = (struct drm_color_lut *) blob->data;
3451 for (i = 0; i < size; i++) {
3452 blob_data[i].red = red[i];
3453 blob_data[i].green = green[i];
3454 blob_data[i].blue = blue[i];
3457 state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
3459 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3460 if (IS_ERR(crtc_state)) {
3461 ret = PTR_ERR(crtc_state);
3465 /* Reset DEGAMMA_LUT and CTM properties. */
3466 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
3467 config->degamma_lut_property, 0);
3471 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
3472 config->ctm_property, 0);
3476 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
3477 config->gamma_lut_property, blob->base.id);
3481 ret = drm_atomic_commit(state);
3483 if (ret == -EDEADLK)
3486 drm_atomic_state_put(state);
3487 drm_property_unreference_blob(blob);
3491 drm_atomic_state_clear(state);
3492 drm_atomic_legacy_backoff(state);
3496 EXPORT_SYMBOL(drm_atomic_helper_legacy_gamma_set);