2 * Copyright (C) 2014 Red Hat
3 * Copyright (C) 2014 Intel Corp.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
29 #include <drm/drm_atomic.h>
30 #include <drm/drm_plane_helper.h>
31 #include <drm/drm_crtc_helper.h>
32 #include <drm/drm_atomic_helper.h>
33 #include <linux/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 &drm_crtc_helper_funcs.mode_fixup and
462 * &drm_encoder_helper_funcs.mode_fixup or
463 * &drm_encoder_helper_funcs.atomic_check functions of the driver backend.
465 * &drm_crtc_state.mode_changed is set when the input mode is changed.
466 * &drm_crtc_state.connectors_changed is set when a connector is added or
467 * removed from the crtc. &drm_crtc_state.active_changed is set when
468 * &drm_crtc_state.active changes, which is used for DPMS.
469 * See also: drm_atomic_crtc_needs_modeset()
473 * Drivers which set &drm_crtc_state.mode_changed (e.g. in their
474 * &drm_plane_helper_funcs.atomic_check hooks if a plane update can't be done
475 * without a full modeset) _must_ call this function afterwards after that
476 * change. It is permitted to call this function multiple times for the same
477 * update, e.g. when the &drm_crtc_helper_funcs.atomic_check functions depend
478 * upon the adjusted dotclock for fifo space allocation and watermark
482 * Zero for success or -errno
485 drm_atomic_helper_check_modeset(struct drm_device *dev,
486 struct drm_atomic_state *state)
488 struct drm_crtc *crtc;
489 struct drm_crtc_state *crtc_state;
490 struct drm_connector *connector;
491 struct drm_connector_state *connector_state;
494 for_each_crtc_in_state(state, crtc, crtc_state, i) {
495 if (!drm_mode_equal(&crtc->state->mode, &crtc_state->mode)) {
496 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] mode changed\n",
497 crtc->base.id, crtc->name);
498 crtc_state->mode_changed = true;
501 if (crtc->state->enable != crtc_state->enable) {
502 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enable changed\n",
503 crtc->base.id, crtc->name);
506 * For clarity this assignment is done here, but
507 * enable == 0 is only true when there are no
508 * connectors and a NULL mode.
510 * The other way around is true as well. enable != 0
511 * iff connectors are attached and a mode is set.
513 crtc_state->mode_changed = true;
514 crtc_state->connectors_changed = true;
518 ret = handle_conflicting_encoders(state, state->legacy_set_config);
522 for_each_connector_in_state(state, connector, connector_state, i) {
524 * This only sets crtc->connectors_changed for routing changes,
525 * drivers must set crtc->connectors_changed themselves when
526 * connector properties need to be updated.
528 ret = update_connector_routing(state, connector,
535 * After all the routing has been prepared we need to add in any
536 * connector which is itself unchanged, but who's crtc changes it's
537 * configuration. This must be done before calling mode_fixup in case a
538 * crtc only changed its mode but has the same set of connectors.
540 for_each_crtc_in_state(state, crtc, crtc_state, i) {
541 bool has_connectors =
542 !!crtc_state->connector_mask;
545 * We must set ->active_changed after walking connectors for
546 * otherwise an update that only changes active would result in
547 * a full modeset because update_connector_routing force that.
549 if (crtc->state->active != crtc_state->active) {
550 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active changed\n",
551 crtc->base.id, crtc->name);
552 crtc_state->active_changed = true;
555 if (!drm_atomic_crtc_needs_modeset(crtc_state))
558 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] needs all connectors, enable: %c, active: %c\n",
559 crtc->base.id, crtc->name,
560 crtc_state->enable ? 'y' : 'n',
561 crtc_state->active ? 'y' : 'n');
563 ret = drm_atomic_add_affected_connectors(state, crtc);
567 ret = drm_atomic_add_affected_planes(state, crtc);
571 if (crtc_state->enable != has_connectors) {
572 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled/connectors mismatch\n",
573 crtc->base.id, crtc->name);
579 return mode_fixup(state);
581 EXPORT_SYMBOL(drm_atomic_helper_check_modeset);
584 * drm_atomic_helper_check_planes - validate state object for planes changes
586 * @state: the driver state object
588 * Check the state object to see if the requested state is physically possible.
589 * This does all the plane update related checks using by calling into the
590 * &drm_crtc_helper_funcs.atomic_check and &drm_plane_helper_funcs.atomic_check
591 * hooks provided by the driver.
593 * It also sets &drm_crtc_state.planes_changed to indicate that a crtc has
597 * Zero for success or -errno
600 drm_atomic_helper_check_planes(struct drm_device *dev,
601 struct drm_atomic_state *state)
603 struct drm_crtc *crtc;
604 struct drm_crtc_state *crtc_state;
605 struct drm_plane *plane;
606 struct drm_plane_state *plane_state;
609 for_each_plane_in_state(state, plane, plane_state, i) {
610 const struct drm_plane_helper_funcs *funcs;
612 funcs = plane->helper_private;
614 drm_atomic_helper_plane_changed(state, plane_state, plane);
616 if (!funcs || !funcs->atomic_check)
619 ret = funcs->atomic_check(plane, plane_state);
621 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic driver check failed\n",
622 plane->base.id, plane->name);
627 for_each_crtc_in_state(state, crtc, crtc_state, i) {
628 const struct drm_crtc_helper_funcs *funcs;
630 funcs = crtc->helper_private;
632 if (!funcs || !funcs->atomic_check)
635 ret = funcs->atomic_check(crtc, crtc_state);
637 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic driver check failed\n",
638 crtc->base.id, crtc->name);
645 EXPORT_SYMBOL(drm_atomic_helper_check_planes);
648 * drm_atomic_helper_check - validate state object
650 * @state: the driver state object
652 * Check the state object to see if the requested state is physically possible.
653 * Only crtcs and planes have check callbacks, so for any additional (global)
654 * checking that a driver needs it can simply wrap that around this function.
655 * Drivers without such needs can directly use this as their
656 * &drm_mode_config_funcs.atomic_check callback.
658 * This just wraps the two parts of the state checking for planes and modeset
659 * state in the default order: First it calls drm_atomic_helper_check_modeset()
660 * and then drm_atomic_helper_check_planes(). The assumption is that the
661 * @drm_plane_helper_funcs.atomic_check and @drm_crtc_helper_funcs.atomic_check
662 * functions depend upon an updated adjusted_mode.clock to e.g. properly compute
666 * Zero for success or -errno
668 int drm_atomic_helper_check(struct drm_device *dev,
669 struct drm_atomic_state *state)
673 ret = drm_atomic_helper_check_modeset(dev, state);
677 ret = drm_atomic_helper_check_planes(dev, state);
683 EXPORT_SYMBOL(drm_atomic_helper_check);
686 disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state)
688 struct drm_connector *connector;
689 struct drm_connector_state *old_conn_state;
690 struct drm_crtc *crtc;
691 struct drm_crtc_state *old_crtc_state;
694 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
695 const struct drm_encoder_helper_funcs *funcs;
696 struct drm_encoder *encoder;
698 /* Shut down everything that's in the changeset and currently
699 * still on. So need to check the old, saved state. */
700 if (!old_conn_state->crtc)
703 old_crtc_state = drm_atomic_get_existing_crtc_state(old_state,
704 old_conn_state->crtc);
706 if (!old_crtc_state->active ||
707 !drm_atomic_crtc_needs_modeset(old_conn_state->crtc->state))
710 encoder = old_conn_state->best_encoder;
712 /* We shouldn't get this far if we didn't previously have
713 * an encoder.. but WARN_ON() rather than explode.
715 if (WARN_ON(!encoder))
718 funcs = encoder->helper_private;
720 DRM_DEBUG_ATOMIC("disabling [ENCODER:%d:%s]\n",
721 encoder->base.id, encoder->name);
724 * Each encoder has at most one connector (since we always steal
725 * it away), so we won't call disable hooks twice.
727 drm_bridge_disable(encoder->bridge);
729 /* Right function depends upon target state. */
731 if (connector->state->crtc && funcs->prepare)
732 funcs->prepare(encoder);
733 else if (funcs->disable)
734 funcs->disable(encoder);
735 else if (funcs->dpms)
736 funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
739 drm_bridge_post_disable(encoder->bridge);
742 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
743 const struct drm_crtc_helper_funcs *funcs;
745 /* Shut down everything that needs a full modeset. */
746 if (!drm_atomic_crtc_needs_modeset(crtc->state))
749 if (!old_crtc_state->active)
752 funcs = crtc->helper_private;
754 DRM_DEBUG_ATOMIC("disabling [CRTC:%d:%s]\n",
755 crtc->base.id, crtc->name);
758 /* Right function depends upon target state. */
759 if (crtc->state->enable && funcs->prepare)
760 funcs->prepare(crtc);
761 else if (funcs->atomic_disable)
762 funcs->atomic_disable(crtc, old_crtc_state);
763 else if (funcs->disable)
764 funcs->disable(crtc);
766 funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
771 * drm_atomic_helper_update_legacy_modeset_state - update legacy modeset state
773 * @old_state: atomic state object with old state structures
775 * This function updates all the various legacy modeset state pointers in
776 * connectors, encoders and crtcs. It also updates the timestamping constants
777 * used for precise vblank timestamps by calling
778 * drm_calc_timestamping_constants().
780 * Drivers can use this for building their own atomic commit if they don't have
781 * a pure helper-based modeset implementation.
784 drm_atomic_helper_update_legacy_modeset_state(struct drm_device *dev,
785 struct drm_atomic_state *old_state)
787 struct drm_connector *connector;
788 struct drm_connector_state *old_conn_state;
789 struct drm_crtc *crtc;
790 struct drm_crtc_state *old_crtc_state;
793 /* clear out existing links and update dpms */
794 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
795 if (connector->encoder) {
796 WARN_ON(!connector->encoder->crtc);
798 connector->encoder->crtc = NULL;
799 connector->encoder = NULL;
802 crtc = connector->state->crtc;
803 if ((!crtc && old_conn_state->crtc) ||
804 (crtc && drm_atomic_crtc_needs_modeset(crtc->state))) {
805 struct drm_property *dpms_prop =
806 dev->mode_config.dpms_property;
807 int mode = DRM_MODE_DPMS_OFF;
809 if (crtc && crtc->state->active)
810 mode = DRM_MODE_DPMS_ON;
812 connector->dpms = mode;
813 drm_object_property_set_value(&connector->base,
819 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
820 if (!connector->state->crtc)
823 if (WARN_ON(!connector->state->best_encoder))
826 connector->encoder = connector->state->best_encoder;
827 connector->encoder->crtc = connector->state->crtc;
830 /* set legacy state in the crtc structure */
831 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
832 struct drm_plane *primary = crtc->primary;
834 crtc->mode = crtc->state->mode;
835 crtc->enabled = crtc->state->enable;
837 if (drm_atomic_get_existing_plane_state(old_state, primary) &&
838 primary->state->crtc == crtc) {
839 crtc->x = primary->state->src_x >> 16;
840 crtc->y = primary->state->src_y >> 16;
843 if (crtc->state->enable)
844 drm_calc_timestamping_constants(crtc,
845 &crtc->state->adjusted_mode);
848 EXPORT_SYMBOL(drm_atomic_helper_update_legacy_modeset_state);
851 crtc_set_mode(struct drm_device *dev, struct drm_atomic_state *old_state)
853 struct drm_crtc *crtc;
854 struct drm_crtc_state *old_crtc_state;
855 struct drm_connector *connector;
856 struct drm_connector_state *old_conn_state;
859 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
860 const struct drm_crtc_helper_funcs *funcs;
862 if (!crtc->state->mode_changed)
865 funcs = crtc->helper_private;
867 if (crtc->state->enable && funcs->mode_set_nofb) {
868 DRM_DEBUG_ATOMIC("modeset on [CRTC:%d:%s]\n",
869 crtc->base.id, crtc->name);
871 funcs->mode_set_nofb(crtc);
875 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
876 const struct drm_encoder_helper_funcs *funcs;
877 struct drm_crtc_state *new_crtc_state;
878 struct drm_encoder *encoder;
879 struct drm_display_mode *mode, *adjusted_mode;
881 if (!connector->state->best_encoder)
884 encoder = connector->state->best_encoder;
885 funcs = encoder->helper_private;
886 new_crtc_state = connector->state->crtc->state;
887 mode = &new_crtc_state->mode;
888 adjusted_mode = &new_crtc_state->adjusted_mode;
890 if (!new_crtc_state->mode_changed)
893 DRM_DEBUG_ATOMIC("modeset on [ENCODER:%d:%s]\n",
894 encoder->base.id, encoder->name);
897 * Each encoder has at most one connector (since we always steal
898 * it away), so we won't call mode_set hooks twice.
900 if (funcs && funcs->atomic_mode_set) {
901 funcs->atomic_mode_set(encoder, new_crtc_state,
903 } else if (funcs && funcs->mode_set) {
904 funcs->mode_set(encoder, mode, adjusted_mode);
907 drm_bridge_mode_set(encoder->bridge, mode, adjusted_mode);
912 * drm_atomic_helper_commit_modeset_disables - modeset commit to disable outputs
914 * @old_state: atomic state object with old state structures
916 * This function shuts down all the outputs that need to be shut down and
917 * prepares them (if required) with the new mode.
919 * For compatibility with legacy crtc helpers this should be called before
920 * drm_atomic_helper_commit_planes(), which is what the default commit function
921 * does. But drivers with different needs can group the modeset commits together
922 * and do the plane commits at the end. This is useful for drivers doing runtime
923 * PM since planes updates then only happen when the CRTC is actually enabled.
925 void drm_atomic_helper_commit_modeset_disables(struct drm_device *dev,
926 struct drm_atomic_state *old_state)
928 disable_outputs(dev, old_state);
930 drm_atomic_helper_update_legacy_modeset_state(dev, old_state);
932 crtc_set_mode(dev, old_state);
934 EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_disables);
937 * drm_atomic_helper_commit_modeset_enables - modeset commit to enable outputs
939 * @old_state: atomic state object with old state structures
941 * This function enables all the outputs with the new configuration which had to
942 * be turned off for the update.
944 * For compatibility with legacy crtc helpers this should be called after
945 * drm_atomic_helper_commit_planes(), which is what the default commit function
946 * does. But drivers with different needs can group the modeset commits together
947 * and do the plane commits at the end. This is useful for drivers doing runtime
948 * PM since planes updates then only happen when the CRTC is actually enabled.
950 void drm_atomic_helper_commit_modeset_enables(struct drm_device *dev,
951 struct drm_atomic_state *old_state)
953 struct drm_crtc *crtc;
954 struct drm_crtc_state *old_crtc_state;
955 struct drm_connector *connector;
956 struct drm_connector_state *old_conn_state;
959 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
960 const struct drm_crtc_helper_funcs *funcs;
962 /* Need to filter out CRTCs where only planes change. */
963 if (!drm_atomic_crtc_needs_modeset(crtc->state))
966 if (!crtc->state->active)
969 funcs = crtc->helper_private;
971 if (crtc->state->enable) {
972 DRM_DEBUG_ATOMIC("enabling [CRTC:%d:%s]\n",
973 crtc->base.id, crtc->name);
982 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
983 const struct drm_encoder_helper_funcs *funcs;
984 struct drm_encoder *encoder;
986 if (!connector->state->best_encoder)
989 if (!connector->state->crtc->state->active ||
990 !drm_atomic_crtc_needs_modeset(connector->state->crtc->state))
993 encoder = connector->state->best_encoder;
994 funcs = encoder->helper_private;
996 DRM_DEBUG_ATOMIC("enabling [ENCODER:%d:%s]\n",
997 encoder->base.id, encoder->name);
1000 * Each encoder has at most one connector (since we always steal
1001 * it away), so we won't call enable hooks twice.
1003 drm_bridge_pre_enable(encoder->bridge);
1007 funcs->enable(encoder);
1008 else if (funcs->commit)
1009 funcs->commit(encoder);
1012 drm_bridge_enable(encoder->bridge);
1015 EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_enables);
1018 * drm_atomic_helper_wait_for_fences - wait for fences stashed in plane state
1020 * @state: atomic state object with old state structures
1021 * @pre_swap: If true, do an interruptible wait, and @state is the new state.
1022 * Otherwise @state is the old state.
1024 * For implicit sync, driver should fish the exclusive fence out from the
1025 * incoming fb's and stash it in the drm_plane_state. This is called after
1026 * drm_atomic_helper_swap_state() so it uses the current plane state (and
1027 * just uses the atomic state to find the changed planes)
1029 * Note that @pre_swap is needed since the point where we block for fences moves
1030 * around depending upon whether an atomic commit is blocking or
1031 * non-blocking. For async commit all waiting needs to happen after
1032 * drm_atomic_helper_swap_state() is called, but for synchronous commits we want
1033 * to wait **before** we do anything that can't be easily rolled back. That is
1034 * before we call drm_atomic_helper_swap_state().
1036 * Returns zero if success or < 0 if dma_fence_wait() fails.
1038 int drm_atomic_helper_wait_for_fences(struct drm_device *dev,
1039 struct drm_atomic_state *state,
1042 struct drm_plane *plane;
1043 struct drm_plane_state *plane_state;
1046 for_each_plane_in_state(state, plane, plane_state, i) {
1048 plane_state = plane->state;
1050 if (!plane_state->fence)
1053 WARN_ON(!plane_state->fb);
1056 * If waiting for fences pre-swap (ie: nonblock), userspace can
1057 * still interrupt the operation. Instead of blocking until the
1058 * timer expires, make the wait interruptible.
1060 ret = dma_fence_wait(plane_state->fence, pre_swap);
1064 dma_fence_put(plane_state->fence);
1065 plane_state->fence = NULL;
1070 EXPORT_SYMBOL(drm_atomic_helper_wait_for_fences);
1073 * drm_atomic_helper_wait_for_vblanks - wait for vblank on crtcs
1075 * @old_state: atomic state object with old state structures
1077 * Helper to, after atomic commit, wait for vblanks on all effected
1078 * crtcs (ie. before cleaning up old framebuffers using
1079 * drm_atomic_helper_cleanup_planes()). It will only wait on crtcs where the
1080 * framebuffers have actually changed to optimize for the legacy cursor and
1081 * plane update use-case.
1084 drm_atomic_helper_wait_for_vblanks(struct drm_device *dev,
1085 struct drm_atomic_state *old_state)
1087 struct drm_crtc *crtc;
1088 struct drm_crtc_state *old_crtc_state;
1090 unsigned crtc_mask = 0;
1093 * Legacy cursor ioctls are completely unsynced, and userspace
1094 * relies on that (by doing tons of cursor updates).
1096 if (old_state->legacy_cursor_update)
1099 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
1100 struct drm_crtc_state *new_crtc_state = crtc->state;
1102 if (!new_crtc_state->active || !new_crtc_state->planes_changed)
1105 ret = drm_crtc_vblank_get(crtc);
1109 crtc_mask |= drm_crtc_mask(crtc);
1110 old_state->crtcs[i].last_vblank_count = drm_crtc_vblank_count(crtc);
1113 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
1114 if (!(crtc_mask & drm_crtc_mask(crtc)))
1117 ret = wait_event_timeout(dev->vblank[i].queue,
1118 old_state->crtcs[i].last_vblank_count !=
1119 drm_crtc_vblank_count(crtc),
1120 msecs_to_jiffies(50));
1122 WARN(!ret, "[CRTC:%d] vblank wait timed out\n", crtc->base.id);
1124 drm_crtc_vblank_put(crtc);
1127 EXPORT_SYMBOL(drm_atomic_helper_wait_for_vblanks);
1130 * drm_atomic_helper_commit_tail - commit atomic update to hardware
1131 * @old_state: atomic state object with old state structures
1133 * This is the default implementation for the
1134 * &drm_mode_config_helper_funcs.atomic_commit_tail hook.
1136 * Note that the default ordering of how the various stages are called is to
1137 * match the legacy modeset helper library closest. One peculiarity of that is
1138 * that it doesn't mesh well with runtime PM at all.
1140 * For drivers supporting runtime PM the recommended sequence is instead ::
1142 * drm_atomic_helper_commit_modeset_disables(dev, old_state);
1144 * drm_atomic_helper_commit_modeset_enables(dev, old_state);
1146 * drm_atomic_helper_commit_planes(dev, old_state,
1147 * DRM_PLANE_COMMIT_ACTIVE_ONLY);
1149 * for committing the atomic update to hardware. See the kerneldoc entries for
1150 * these three functions for more details.
1152 void drm_atomic_helper_commit_tail(struct drm_atomic_state *old_state)
1154 struct drm_device *dev = old_state->dev;
1156 drm_atomic_helper_commit_modeset_disables(dev, old_state);
1158 drm_atomic_helper_commit_planes(dev, old_state, 0);
1160 drm_atomic_helper_commit_modeset_enables(dev, old_state);
1162 drm_atomic_helper_commit_hw_done(old_state);
1164 drm_atomic_helper_wait_for_vblanks(dev, old_state);
1166 drm_atomic_helper_cleanup_planes(dev, old_state);
1168 EXPORT_SYMBOL(drm_atomic_helper_commit_tail);
1170 static void commit_tail(struct drm_atomic_state *old_state)
1172 struct drm_device *dev = old_state->dev;
1173 struct drm_mode_config_helper_funcs *funcs;
1175 funcs = dev->mode_config.helper_private;
1177 drm_atomic_helper_wait_for_fences(dev, old_state, false);
1179 drm_atomic_helper_wait_for_dependencies(old_state);
1181 if (funcs && funcs->atomic_commit_tail)
1182 funcs->atomic_commit_tail(old_state);
1184 drm_atomic_helper_commit_tail(old_state);
1186 drm_atomic_helper_commit_cleanup_done(old_state);
1188 drm_atomic_state_put(old_state);
1191 static void commit_work(struct work_struct *work)
1193 struct drm_atomic_state *state = container_of(work,
1194 struct drm_atomic_state,
1200 * drm_atomic_helper_commit - commit validated state object
1202 * @state: the driver state object
1203 * @nonblock: whether nonblocking behavior is requested.
1205 * This function commits a with drm_atomic_helper_check() pre-validated state
1206 * object. This can still fail when e.g. the framebuffer reservation fails. This
1207 * function implements nonblocking commits, using
1208 * drm_atomic_helper_setup_commit() and related functions.
1210 * Committing the actual hardware state is done through the
1211 * &drm_mode_config_helper_funcs.atomic_commit_tail callback, or it's default
1212 * implementation drm_atomic_helper_commit_tail().
1215 * Zero for success or -errno.
1217 int drm_atomic_helper_commit(struct drm_device *dev,
1218 struct drm_atomic_state *state,
1223 ret = drm_atomic_helper_setup_commit(state, nonblock);
1227 INIT_WORK(&state->commit_work, commit_work);
1229 ret = drm_atomic_helper_prepare_planes(dev, state);
1234 ret = drm_atomic_helper_wait_for_fences(dev, state, true);
1236 drm_atomic_helper_cleanup_planes(dev, state);
1242 * This is the point of no return - everything below never fails except
1243 * when the hw goes bonghits. Which means we can commit the new state on
1244 * the software side now.
1247 drm_atomic_helper_swap_state(state, true);
1250 * Everything below can be run asynchronously without the need to grab
1251 * any modeset locks at all under one condition: It must be guaranteed
1252 * that the asynchronous work has either been cancelled (if the driver
1253 * supports it, which at least requires that the framebuffers get
1254 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
1255 * before the new state gets committed on the software side with
1256 * drm_atomic_helper_swap_state().
1258 * This scheme allows new atomic state updates to be prepared and
1259 * checked in parallel to the asynchronous completion of the previous
1260 * update. Which is important since compositors need to figure out the
1261 * composition of the next frame right after having submitted the
1264 * NOTE: Commit work has multiple phases, first hardware commit, then
1265 * cleanup. We want them to overlap, hence need system_unbound_wq to
1266 * make sure work items don't artifically stall on each another.
1269 drm_atomic_state_get(state);
1271 queue_work(system_unbound_wq, &state->commit_work);
1277 EXPORT_SYMBOL(drm_atomic_helper_commit);
1280 * DOC: implementing nonblocking commit
1282 * Nonblocking atomic commits have to be implemented in the following sequence:
1284 * 1. Run drm_atomic_helper_prepare_planes() first. This is the only function
1285 * which commit needs to call which can fail, so we want to run it first and
1288 * 2. Synchronize with any outstanding nonblocking commit worker threads which
1289 * might be affected the new state update. This can be done by either cancelling
1290 * or flushing the work items, depending upon whether the driver can deal with
1291 * cancelled updates. Note that it is important to ensure that the framebuffer
1292 * cleanup is still done when cancelling.
1294 * Asynchronous workers need to have sufficient parallelism to be able to run
1295 * different atomic commits on different CRTCs in parallel. The simplest way to
1296 * achive this is by running them on the &system_unbound_wq work queue. Note
1297 * that drivers are not required to split up atomic commits and run an
1298 * individual commit in parallel - userspace is supposed to do that if it cares.
1299 * But it might be beneficial to do that for modesets, since those necessarily
1300 * must be done as one global operation, and enabling or disabling a CRTC can
1301 * take a long time. But even that is not required.
1303 * 3. The software state is updated synchronously with
1304 * drm_atomic_helper_swap_state(). Doing this under the protection of all modeset
1305 * locks means concurrent callers never see inconsistent state. And doing this
1306 * while it's guaranteed that no relevant nonblocking worker runs means that
1307 * nonblocking workers do not need grab any locks. Actually they must not grab
1308 * locks, for otherwise the work flushing will deadlock.
1310 * 4. Schedule a work item to do all subsequent steps, using the split-out
1311 * commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and
1312 * then cleaning up the framebuffers after the old framebuffer is no longer
1315 * The above scheme is implemented in the atomic helper libraries in
1316 * drm_atomic_helper_commit() using a bunch of helper functions. See
1317 * drm_atomic_helper_setup_commit() for a starting point.
1320 static int stall_checks(struct drm_crtc *crtc, bool nonblock)
1322 struct drm_crtc_commit *commit, *stall_commit = NULL;
1323 bool completed = true;
1327 spin_lock(&crtc->commit_lock);
1329 list_for_each_entry(commit, &crtc->commit_list, commit_entry) {
1331 completed = try_wait_for_completion(&commit->flip_done);
1332 /* Userspace is not allowed to get ahead of the previous
1333 * commit with nonblocking ones. */
1334 if (!completed && nonblock) {
1335 spin_unlock(&crtc->commit_lock);
1338 } else if (i == 1) {
1339 stall_commit = commit;
1340 drm_crtc_commit_get(stall_commit);
1346 spin_unlock(&crtc->commit_lock);
1351 /* We don't want to let commits get ahead of cleanup work too much,
1352 * stalling on 2nd previous commit means triple-buffer won't ever stall.
1354 ret = wait_for_completion_interruptible_timeout(&stall_commit->cleanup_done,
1357 DRM_ERROR("[CRTC:%d:%s] cleanup_done timed out\n",
1358 crtc->base.id, crtc->name);
1360 drm_crtc_commit_put(stall_commit);
1362 return ret < 0 ? ret : 0;
1365 static void release_crtc_commit(struct completion *completion)
1367 struct drm_crtc_commit *commit = container_of(completion,
1371 drm_crtc_commit_put(commit);
1375 * drm_atomic_helper_setup_commit - setup possibly nonblocking commit
1376 * @state: new modeset state to be committed
1377 * @nonblock: whether nonblocking behavior is requested.
1379 * This function prepares @state to be used by the atomic helper's support for
1380 * nonblocking commits. Drivers using the nonblocking commit infrastructure
1381 * should always call this function from their
1382 * &drm_mode_config_funcs.atomic_commit hook.
1384 * To be able to use this support drivers need to use a few more helper
1385 * functions. drm_atomic_helper_wait_for_dependencies() must be called before
1386 * actually committing the hardware state, and for nonblocking commits this call
1387 * must be placed in the async worker. See also drm_atomic_helper_swap_state()
1388 * and it's stall parameter, for when a driver's commit hooks look at the
1389 * &drm_crtc.state, &drm_plane.state or &drm_connector.state pointer directly.
1391 * Completion of the hardware commit step must be signalled using
1392 * drm_atomic_helper_commit_hw_done(). After this step the driver is not allowed
1393 * to read or change any permanent software or hardware modeset state. The only
1394 * exception is state protected by other means than &drm_modeset_lock locks.
1395 * Only the free standing @state with pointers to the old state structures can
1396 * be inspected, e.g. to clean up old buffers using
1397 * drm_atomic_helper_cleanup_planes().
1399 * At the very end, before cleaning up @state drivers must call
1400 * drm_atomic_helper_commit_cleanup_done().
1402 * This is all implemented by in drm_atomic_helper_commit(), giving drivers a
1403 * complete and esay-to-use default implementation of the atomic_commit() hook.
1405 * The tracking of asynchronously executed and still pending commits is done
1406 * using the core structure &drm_crtc_commit.
1408 * By default there's no need to clean up resources allocated by this function
1409 * explicitly: drm_atomic_state_default_clear() will take care of that
1414 * 0 on success. -EBUSY when userspace schedules nonblocking commits too fast,
1415 * -ENOMEM on allocation failures and -EINTR when a signal is pending.
1417 int drm_atomic_helper_setup_commit(struct drm_atomic_state *state,
1420 struct drm_crtc *crtc;
1421 struct drm_crtc_state *crtc_state;
1422 struct drm_crtc_commit *commit;
1425 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1426 commit = kzalloc(sizeof(*commit), GFP_KERNEL);
1430 init_completion(&commit->flip_done);
1431 init_completion(&commit->hw_done);
1432 init_completion(&commit->cleanup_done);
1433 INIT_LIST_HEAD(&commit->commit_entry);
1434 kref_init(&commit->ref);
1435 commit->crtc = crtc;
1437 state->crtcs[i].commit = commit;
1439 ret = stall_checks(crtc, nonblock);
1443 /* Drivers only send out events when at least either current or
1444 * new CRTC state is active. Complete right away if everything
1446 if (!crtc->state->active && !crtc_state->active) {
1447 complete_all(&commit->flip_done);
1451 /* Legacy cursor updates are fully unsynced. */
1452 if (state->legacy_cursor_update) {
1453 complete_all(&commit->flip_done);
1457 if (!crtc_state->event) {
1458 commit->event = kzalloc(sizeof(*commit->event),
1463 crtc_state->event = commit->event;
1466 crtc_state->event->base.completion = &commit->flip_done;
1467 crtc_state->event->base.completion_release = release_crtc_commit;
1468 drm_crtc_commit_get(commit);
1473 EXPORT_SYMBOL(drm_atomic_helper_setup_commit);
1476 static struct drm_crtc_commit *preceeding_commit(struct drm_crtc *crtc)
1478 struct drm_crtc_commit *commit;
1481 list_for_each_entry(commit, &crtc->commit_list, commit_entry) {
1482 /* skip the first entry, that's the current commit */
1492 * drm_atomic_helper_wait_for_dependencies - wait for required preceeding commits
1493 * @old_state: atomic state object with old state structures
1495 * This function waits for all preceeding commits that touch the same CRTC as
1496 * @old_state to both be committed to the hardware (as signalled by
1497 * drm_atomic_helper_commit_hw_done) and executed by the hardware (as signalled
1498 * by calling drm_crtc_vblank_send_event() on the &drm_crtc_state.event).
1500 * This is part of the atomic helper support for nonblocking commits, see
1501 * drm_atomic_helper_setup_commit() for an overview.
1503 void drm_atomic_helper_wait_for_dependencies(struct drm_atomic_state *old_state)
1505 struct drm_crtc *crtc;
1506 struct drm_crtc_state *crtc_state;
1507 struct drm_crtc_commit *commit;
1511 for_each_crtc_in_state(old_state, crtc, crtc_state, i) {
1512 spin_lock(&crtc->commit_lock);
1513 commit = preceeding_commit(crtc);
1515 drm_crtc_commit_get(commit);
1516 spin_unlock(&crtc->commit_lock);
1521 ret = wait_for_completion_timeout(&commit->hw_done,
1524 DRM_ERROR("[CRTC:%d:%s] hw_done timed out\n",
1525 crtc->base.id, crtc->name);
1527 /* Currently no support for overwriting flips, hence
1528 * stall for previous one to execute completely. */
1529 ret = wait_for_completion_timeout(&commit->flip_done,
1532 DRM_ERROR("[CRTC:%d:%s] flip_done timed out\n",
1533 crtc->base.id, crtc->name);
1535 drm_crtc_commit_put(commit);
1538 EXPORT_SYMBOL(drm_atomic_helper_wait_for_dependencies);
1541 * drm_atomic_helper_commit_hw_done - setup possible nonblocking commit
1542 * @old_state: atomic state object with old state structures
1544 * This function is used to signal completion of the hardware commit step. After
1545 * this step the driver is not allowed to read or change any permanent software
1546 * or hardware modeset state. The only exception is state protected by other
1547 * means than &drm_modeset_lock locks.
1549 * Drivers should try to postpone any expensive or delayed cleanup work after
1550 * this function is called.
1552 * This is part of the atomic helper support for nonblocking commits, see
1553 * drm_atomic_helper_setup_commit() for an overview.
1555 void drm_atomic_helper_commit_hw_done(struct drm_atomic_state *old_state)
1557 struct drm_crtc *crtc;
1558 struct drm_crtc_state *crtc_state;
1559 struct drm_crtc_commit *commit;
1562 for_each_crtc_in_state(old_state, crtc, crtc_state, i) {
1563 commit = old_state->crtcs[i].commit;
1567 /* backend must have consumed any event by now */
1568 WARN_ON(crtc->state->event);
1569 spin_lock(&crtc->commit_lock);
1570 complete_all(&commit->hw_done);
1571 spin_unlock(&crtc->commit_lock);
1574 EXPORT_SYMBOL(drm_atomic_helper_commit_hw_done);
1577 * drm_atomic_helper_commit_cleanup_done - signal completion of commit
1578 * @old_state: atomic state object with old state structures
1580 * This signals completion of the atomic update @old_state, including any
1581 * cleanup work. If used, it must be called right before calling
1582 * drm_atomic_state_put().
1584 * This is part of the atomic helper support for nonblocking commits, see
1585 * drm_atomic_helper_setup_commit() for an overview.
1587 void drm_atomic_helper_commit_cleanup_done(struct drm_atomic_state *old_state)
1589 struct drm_crtc *crtc;
1590 struct drm_crtc_state *crtc_state;
1591 struct drm_crtc_commit *commit;
1595 for_each_crtc_in_state(old_state, crtc, crtc_state, i) {
1596 commit = old_state->crtcs[i].commit;
1597 if (WARN_ON(!commit))
1600 spin_lock(&crtc->commit_lock);
1601 complete_all(&commit->cleanup_done);
1602 WARN_ON(!try_wait_for_completion(&commit->hw_done));
1604 /* commit_list borrows our reference, need to remove before we
1605 * clean up our drm_atomic_state. But only after it actually
1606 * completed, otherwise subsequent commits won't stall properly. */
1607 if (try_wait_for_completion(&commit->flip_done))
1610 spin_unlock(&crtc->commit_lock);
1612 /* We must wait for the vblank event to signal our completion
1613 * before releasing our reference, since the vblank work does
1614 * not hold a reference of its own. */
1615 ret = wait_for_completion_timeout(&commit->flip_done,
1618 DRM_ERROR("[CRTC:%d:%s] flip_done timed out\n",
1619 crtc->base.id, crtc->name);
1621 spin_lock(&crtc->commit_lock);
1623 list_del(&commit->commit_entry);
1624 spin_unlock(&crtc->commit_lock);
1627 EXPORT_SYMBOL(drm_atomic_helper_commit_cleanup_done);
1630 * drm_atomic_helper_prepare_planes - prepare plane resources before commit
1632 * @state: atomic state object with new state structures
1634 * This function prepares plane state, specifically framebuffers, for the new
1635 * configuration, by calling &drm_plane_helper_funcs.prepare_fb. If any failure
1636 * is encountered this function will call &drm_plane_helper_funcs.cleanup_fb on
1637 * any already successfully prepared framebuffer.
1640 * 0 on success, negative error code on failure.
1642 int drm_atomic_helper_prepare_planes(struct drm_device *dev,
1643 struct drm_atomic_state *state)
1645 struct drm_plane *plane;
1646 struct drm_plane_state *plane_state;
1649 for_each_plane_in_state(state, plane, plane_state, i) {
1650 const struct drm_plane_helper_funcs *funcs;
1652 funcs = plane->helper_private;
1654 if (funcs->prepare_fb) {
1655 ret = funcs->prepare_fb(plane, plane_state);
1664 for_each_plane_in_state(state, plane, plane_state, j) {
1665 const struct drm_plane_helper_funcs *funcs;
1670 funcs = plane->helper_private;
1672 if (funcs->cleanup_fb)
1673 funcs->cleanup_fb(plane, plane_state);
1678 EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
1680 static bool plane_crtc_active(const struct drm_plane_state *state)
1682 return state->crtc && state->crtc->state->active;
1686 * drm_atomic_helper_commit_planes - commit plane state
1688 * @old_state: atomic state object with old state structures
1689 * @flags: flags for committing plane state
1691 * This function commits the new plane state using the plane and atomic helper
1692 * functions for planes and crtcs. It assumes that the atomic state has already
1693 * been pushed into the relevant object state pointers, since this step can no
1696 * It still requires the global state object @old_state to know which planes and
1697 * crtcs need to be updated though.
1699 * Note that this function does all plane updates across all CRTCs in one step.
1700 * If the hardware can't support this approach look at
1701 * drm_atomic_helper_commit_planes_on_crtc() instead.
1703 * Plane parameters can be updated by applications while the associated CRTC is
1704 * disabled. The DRM/KMS core will store the parameters in the plane state,
1705 * which will be available to the driver when the CRTC is turned on. As a result
1706 * most drivers don't need to be immediately notified of plane updates for a
1709 * Unless otherwise needed, drivers are advised to set the ACTIVE_ONLY flag in
1710 * @flags in order not to receive plane update notifications related to a
1711 * disabled CRTC. This avoids the need to manually ignore plane updates in
1712 * driver code when the driver and/or hardware can't or just don't need to deal
1713 * with updates on disabled CRTCs, for example when supporting runtime PM.
1715 * Drivers may set the NO_DISABLE_AFTER_MODESET flag in @flags if the relevant
1716 * display controllers require to disable a CRTC's planes when the CRTC is
1717 * disabled. This function would skip the &drm_plane_helper_funcs.atomic_disable
1718 * call for a plane if the CRTC of the old plane state needs a modesetting
1719 * operation. Of course, the drivers need to disable the planes in their CRTC
1720 * disable callbacks since no one else would do that.
1722 * The drm_atomic_helper_commit() default implementation doesn't set the
1723 * ACTIVE_ONLY flag to most closely match the behaviour of the legacy helpers.
1724 * This should not be copied blindly by drivers.
1726 void drm_atomic_helper_commit_planes(struct drm_device *dev,
1727 struct drm_atomic_state *old_state,
1730 struct drm_crtc *crtc;
1731 struct drm_crtc_state *old_crtc_state;
1732 struct drm_plane *plane;
1733 struct drm_plane_state *old_plane_state;
1735 bool active_only = flags & DRM_PLANE_COMMIT_ACTIVE_ONLY;
1736 bool no_disable = flags & DRM_PLANE_COMMIT_NO_DISABLE_AFTER_MODESET;
1738 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
1739 const struct drm_crtc_helper_funcs *funcs;
1741 funcs = crtc->helper_private;
1743 if (!funcs || !funcs->atomic_begin)
1746 if (active_only && !crtc->state->active)
1749 funcs->atomic_begin(crtc, old_crtc_state);
1752 for_each_plane_in_state(old_state, plane, old_plane_state, i) {
1753 const struct drm_plane_helper_funcs *funcs;
1756 funcs = plane->helper_private;
1761 disabling = drm_atomic_plane_disabling(plane, old_plane_state);
1765 * Skip planes related to inactive CRTCs. If the plane
1766 * is enabled use the state of the current CRTC. If the
1767 * plane is being disabled use the state of the old
1768 * CRTC to avoid skipping planes being disabled on an
1771 if (!disabling && !plane_crtc_active(plane->state))
1773 if (disabling && !plane_crtc_active(old_plane_state))
1778 * Special-case disabling the plane if drivers support it.
1780 if (disabling && funcs->atomic_disable) {
1781 struct drm_crtc_state *crtc_state;
1783 crtc_state = old_plane_state->crtc->state;
1785 if (drm_atomic_crtc_needs_modeset(crtc_state) &&
1789 funcs->atomic_disable(plane, old_plane_state);
1790 } else if (plane->state->crtc || disabling) {
1791 funcs->atomic_update(plane, old_plane_state);
1795 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
1796 const struct drm_crtc_helper_funcs *funcs;
1798 funcs = crtc->helper_private;
1800 if (!funcs || !funcs->atomic_flush)
1803 if (active_only && !crtc->state->active)
1806 funcs->atomic_flush(crtc, old_crtc_state);
1809 EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
1812 * drm_atomic_helper_commit_planes_on_crtc - commit plane state for a crtc
1813 * @old_crtc_state: atomic state object with the old crtc state
1815 * This function commits the new plane state using the plane and atomic helper
1816 * functions for planes on the specific crtc. It assumes that the atomic state
1817 * has already been pushed into the relevant object state pointers, since this
1818 * step can no longer fail.
1820 * This function is useful when plane updates should be done crtc-by-crtc
1821 * instead of one global step like drm_atomic_helper_commit_planes() does.
1823 * This function can only be savely used when planes are not allowed to move
1824 * between different CRTCs because this function doesn't handle inter-CRTC
1825 * depencies. Callers need to ensure that either no such depencies exist,
1826 * resolve them through ordering of commit calls or through some other means.
1829 drm_atomic_helper_commit_planes_on_crtc(struct drm_crtc_state *old_crtc_state)
1831 const struct drm_crtc_helper_funcs *crtc_funcs;
1832 struct drm_crtc *crtc = old_crtc_state->crtc;
1833 struct drm_atomic_state *old_state = old_crtc_state->state;
1834 struct drm_plane *plane;
1835 unsigned plane_mask;
1837 plane_mask = old_crtc_state->plane_mask;
1838 plane_mask |= crtc->state->plane_mask;
1840 crtc_funcs = crtc->helper_private;
1841 if (crtc_funcs && crtc_funcs->atomic_begin)
1842 crtc_funcs->atomic_begin(crtc, old_crtc_state);
1844 drm_for_each_plane_mask(plane, crtc->dev, plane_mask) {
1845 struct drm_plane_state *old_plane_state =
1846 drm_atomic_get_existing_plane_state(old_state, plane);
1847 const struct drm_plane_helper_funcs *plane_funcs;
1849 plane_funcs = plane->helper_private;
1851 if (!old_plane_state || !plane_funcs)
1854 WARN_ON(plane->state->crtc && plane->state->crtc != crtc);
1856 if (drm_atomic_plane_disabling(plane, old_plane_state) &&
1857 plane_funcs->atomic_disable)
1858 plane_funcs->atomic_disable(plane, old_plane_state);
1859 else if (plane->state->crtc ||
1860 drm_atomic_plane_disabling(plane, old_plane_state))
1861 plane_funcs->atomic_update(plane, old_plane_state);
1864 if (crtc_funcs && crtc_funcs->atomic_flush)
1865 crtc_funcs->atomic_flush(crtc, old_crtc_state);
1867 EXPORT_SYMBOL(drm_atomic_helper_commit_planes_on_crtc);
1870 * drm_atomic_helper_disable_planes_on_crtc - helper to disable CRTC's planes
1871 * @old_crtc_state: atomic state object with the old CRTC state
1872 * @atomic: if set, synchronize with CRTC's atomic_begin/flush hooks
1874 * Disables all planes associated with the given CRTC. This can be
1875 * used for instance in the CRTC helper atomic_disable callback to disable
1878 * If the atomic-parameter is set the function calls the CRTC's
1879 * atomic_begin hook before and atomic_flush hook after disabling the
1882 * It is a bug to call this function without having implemented the
1883 * &drm_plane_helper_funcs.atomic_disable plane hook.
1886 drm_atomic_helper_disable_planes_on_crtc(struct drm_crtc_state *old_crtc_state,
1889 struct drm_crtc *crtc = old_crtc_state->crtc;
1890 const struct drm_crtc_helper_funcs *crtc_funcs =
1891 crtc->helper_private;
1892 struct drm_plane *plane;
1894 if (atomic && crtc_funcs && crtc_funcs->atomic_begin)
1895 crtc_funcs->atomic_begin(crtc, NULL);
1897 drm_atomic_crtc_state_for_each_plane(plane, old_crtc_state) {
1898 const struct drm_plane_helper_funcs *plane_funcs =
1899 plane->helper_private;
1904 WARN_ON(!plane_funcs->atomic_disable);
1905 if (plane_funcs->atomic_disable)
1906 plane_funcs->atomic_disable(plane, NULL);
1909 if (atomic && crtc_funcs && crtc_funcs->atomic_flush)
1910 crtc_funcs->atomic_flush(crtc, NULL);
1912 EXPORT_SYMBOL(drm_atomic_helper_disable_planes_on_crtc);
1915 * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
1917 * @old_state: atomic state object with old state structures
1919 * This function cleans up plane state, specifically framebuffers, from the old
1920 * configuration. Hence the old configuration must be perserved in @old_state to
1921 * be able to call this function.
1923 * This function must also be called on the new state when the atomic update
1924 * fails at any point after calling drm_atomic_helper_prepare_planes().
1926 void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
1927 struct drm_atomic_state *old_state)
1929 struct drm_plane *plane;
1930 struct drm_plane_state *plane_state;
1933 for_each_plane_in_state(old_state, plane, plane_state, i) {
1934 const struct drm_plane_helper_funcs *funcs;
1936 funcs = plane->helper_private;
1938 if (funcs->cleanup_fb)
1939 funcs->cleanup_fb(plane, plane_state);
1942 EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
1945 * drm_atomic_helper_swap_state - store atomic state into current sw state
1946 * @state: atomic state
1947 * @stall: stall for proceeding commits
1949 * This function stores the atomic state into the current state pointers in all
1950 * driver objects. It should be called after all failing steps have been done
1951 * and succeeded, but before the actual hardware state is committed.
1953 * For cleanup and error recovery the current state for all changed objects will
1954 * be swaped into @state.
1956 * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
1958 * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
1960 * 2. Do any other steps that might fail.
1962 * 3. Put the staged state into the current state pointers with this function.
1964 * 4. Actually commit the hardware state.
1966 * 5. Call drm_atomic_helper_cleanup_planes() with @state, which since step 3
1967 * contains the old state. Also do any other cleanup required with that state.
1969 * @stall must be set when nonblocking commits for this driver directly access
1970 * the &drm_plane.state, &drm_crtc.state or &drm_connector.state pointer. With
1971 * the current atomic helpers this is almost always the case, since the helpers
1972 * don't pass the right state structures to the callbacks.
1974 void drm_atomic_helper_swap_state(struct drm_atomic_state *state,
1979 struct drm_connector *connector;
1980 struct drm_connector_state *conn_state;
1981 struct drm_crtc *crtc;
1982 struct drm_crtc_state *crtc_state;
1983 struct drm_plane *plane;
1984 struct drm_plane_state *plane_state;
1985 struct drm_crtc_commit *commit;
1988 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1989 spin_lock(&crtc->commit_lock);
1990 commit = list_first_entry_or_null(&crtc->commit_list,
1991 struct drm_crtc_commit, commit_entry);
1993 drm_crtc_commit_get(commit);
1994 spin_unlock(&crtc->commit_lock);
1999 ret = wait_for_completion_timeout(&commit->hw_done,
2002 DRM_ERROR("[CRTC:%d:%s] hw_done timed out\n",
2003 crtc->base.id, crtc->name);
2004 drm_crtc_commit_put(commit);
2008 for_each_connector_in_state(state, connector, conn_state, i) {
2009 connector->state->state = state;
2010 swap(state->connectors[i].state, connector->state);
2011 connector->state->state = NULL;
2014 for_each_crtc_in_state(state, crtc, crtc_state, i) {
2015 crtc->state->state = state;
2016 swap(state->crtcs[i].state, crtc->state);
2017 crtc->state->state = NULL;
2019 if (state->crtcs[i].commit) {
2020 spin_lock(&crtc->commit_lock);
2021 list_add(&state->crtcs[i].commit->commit_entry,
2022 &crtc->commit_list);
2023 spin_unlock(&crtc->commit_lock);
2025 state->crtcs[i].commit->event = NULL;
2029 for_each_plane_in_state(state, plane, plane_state, i) {
2030 plane->state->state = state;
2031 swap(state->planes[i].state, plane->state);
2032 plane->state->state = NULL;
2035 EXPORT_SYMBOL(drm_atomic_helper_swap_state);
2038 * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
2039 * @plane: plane object to update
2040 * @crtc: owning CRTC of owning plane
2041 * @fb: framebuffer to flip onto plane
2042 * @crtc_x: x offset of primary plane on crtc
2043 * @crtc_y: y offset of primary plane on crtc
2044 * @crtc_w: width of primary plane rectangle on crtc
2045 * @crtc_h: height of primary plane rectangle on crtc
2046 * @src_x: x offset of @fb for panning
2047 * @src_y: y offset of @fb for panning
2048 * @src_w: width of source rectangle in @fb
2049 * @src_h: height of source rectangle in @fb
2051 * Provides a default plane update handler using the atomic driver interface.
2054 * Zero on success, error code on failure
2056 int drm_atomic_helper_update_plane(struct drm_plane *plane,
2057 struct drm_crtc *crtc,
2058 struct drm_framebuffer *fb,
2059 int crtc_x, int crtc_y,
2060 unsigned int crtc_w, unsigned int crtc_h,
2061 uint32_t src_x, uint32_t src_y,
2062 uint32_t src_w, uint32_t src_h)
2064 struct drm_atomic_state *state;
2065 struct drm_plane_state *plane_state;
2068 state = drm_atomic_state_alloc(plane->dev);
2072 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2074 plane_state = drm_atomic_get_plane_state(state, plane);
2075 if (IS_ERR(plane_state)) {
2076 ret = PTR_ERR(plane_state);
2080 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
2083 drm_atomic_set_fb_for_plane(plane_state, fb);
2084 plane_state->crtc_x = crtc_x;
2085 plane_state->crtc_y = crtc_y;
2086 plane_state->crtc_w = crtc_w;
2087 plane_state->crtc_h = crtc_h;
2088 plane_state->src_x = src_x;
2089 plane_state->src_y = src_y;
2090 plane_state->src_w = src_w;
2091 plane_state->src_h = src_h;
2093 if (plane == crtc->cursor)
2094 state->legacy_cursor_update = true;
2096 ret = drm_atomic_commit(state);
2098 if (ret == -EDEADLK)
2101 drm_atomic_state_put(state);
2105 drm_atomic_state_clear(state);
2106 drm_atomic_legacy_backoff(state);
2109 * Someone might have exchanged the framebuffer while we dropped locks
2110 * in the backoff code. We need to fix up the fb refcount tracking the
2113 plane->old_fb = plane->fb;
2117 EXPORT_SYMBOL(drm_atomic_helper_update_plane);
2120 * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
2121 * @plane: plane to disable
2123 * Provides a default plane disable handler using the atomic driver interface.
2126 * Zero on success, error code on failure
2128 int drm_atomic_helper_disable_plane(struct drm_plane *plane)
2130 struct drm_atomic_state *state;
2131 struct drm_plane_state *plane_state;
2135 * FIXME: Without plane->crtc set we can't get at the implicit legacy
2136 * acquire context. The real fix will be to wire the acquire ctx through
2137 * everywhere we need it, but meanwhile prevent chaos by just skipping
2138 * this noop. The critical case is the cursor ioctls which a) only grab
2139 * crtc/cursor-plane locks (so we need the crtc to get at the right
2140 * acquire context) and b) can try to disable the plane multiple times.
2145 state = drm_atomic_state_alloc(plane->dev);
2149 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(plane->crtc);
2151 plane_state = drm_atomic_get_plane_state(state, plane);
2152 if (IS_ERR(plane_state)) {
2153 ret = PTR_ERR(plane_state);
2157 if (plane_state->crtc && (plane == plane->crtc->cursor))
2158 plane_state->state->legacy_cursor_update = true;
2160 ret = __drm_atomic_helper_disable_plane(plane, plane_state);
2164 ret = drm_atomic_commit(state);
2166 if (ret == -EDEADLK)
2169 drm_atomic_state_put(state);
2173 drm_atomic_state_clear(state);
2174 drm_atomic_legacy_backoff(state);
2177 * Someone might have exchanged the framebuffer while we dropped locks
2178 * in the backoff code. We need to fix up the fb refcount tracking the
2181 plane->old_fb = plane->fb;
2185 EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
2187 /* just used from fb-helper and atomic-helper: */
2188 int __drm_atomic_helper_disable_plane(struct drm_plane *plane,
2189 struct drm_plane_state *plane_state)
2193 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
2197 drm_atomic_set_fb_for_plane(plane_state, NULL);
2198 plane_state->crtc_x = 0;
2199 plane_state->crtc_y = 0;
2200 plane_state->crtc_w = 0;
2201 plane_state->crtc_h = 0;
2202 plane_state->src_x = 0;
2203 plane_state->src_y = 0;
2204 plane_state->src_w = 0;
2205 plane_state->src_h = 0;
2210 static int update_output_state(struct drm_atomic_state *state,
2211 struct drm_mode_set *set)
2213 struct drm_device *dev = set->crtc->dev;
2214 struct drm_crtc *crtc;
2215 struct drm_crtc_state *crtc_state;
2216 struct drm_connector *connector;
2217 struct drm_connector_state *conn_state;
2220 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
2221 state->acquire_ctx);
2225 /* First disable all connectors on the target crtc. */
2226 ret = drm_atomic_add_affected_connectors(state, set->crtc);
2230 for_each_connector_in_state(state, connector, conn_state, i) {
2231 if (conn_state->crtc == set->crtc) {
2232 ret = drm_atomic_set_crtc_for_connector(conn_state,
2239 /* Then set all connectors from set->connectors on the target crtc */
2240 for (i = 0; i < set->num_connectors; i++) {
2241 conn_state = drm_atomic_get_connector_state(state,
2242 set->connectors[i]);
2243 if (IS_ERR(conn_state))
2244 return PTR_ERR(conn_state);
2246 ret = drm_atomic_set_crtc_for_connector(conn_state,
2252 for_each_crtc_in_state(state, crtc, crtc_state, i) {
2253 /* Don't update ->enable for the CRTC in the set_config request,
2254 * since a mismatch would indicate a bug in the upper layers.
2255 * The actual modeset code later on will catch any
2256 * inconsistencies here. */
2257 if (crtc == set->crtc)
2260 if (!crtc_state->connector_mask) {
2261 ret = drm_atomic_set_mode_prop_for_crtc(crtc_state,
2266 crtc_state->active = false;
2274 * drm_atomic_helper_set_config - set a new config from userspace
2275 * @set: mode set configuration
2277 * Provides a default crtc set_config handler using the atomic driver interface.
2280 * Returns 0 on success, negative errno numbers on failure.
2282 int drm_atomic_helper_set_config(struct drm_mode_set *set)
2284 struct drm_atomic_state *state;
2285 struct drm_crtc *crtc = set->crtc;
2288 state = drm_atomic_state_alloc(crtc->dev);
2292 state->legacy_set_config = true;
2293 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2295 ret = __drm_atomic_helper_set_config(set, state);
2299 ret = drm_atomic_commit(state);
2301 if (ret == -EDEADLK)
2304 drm_atomic_state_put(state);
2308 drm_atomic_state_clear(state);
2309 drm_atomic_legacy_backoff(state);
2312 * Someone might have exchanged the framebuffer while we dropped locks
2313 * in the backoff code. We need to fix up the fb refcount tracking the
2316 crtc->primary->old_fb = crtc->primary->fb;
2320 EXPORT_SYMBOL(drm_atomic_helper_set_config);
2322 /* just used from fb-helper and atomic-helper: */
2323 int __drm_atomic_helper_set_config(struct drm_mode_set *set,
2324 struct drm_atomic_state *state)
2326 struct drm_crtc_state *crtc_state;
2327 struct drm_plane_state *primary_state;
2328 struct drm_crtc *crtc = set->crtc;
2329 int hdisplay, vdisplay;
2332 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2333 if (IS_ERR(crtc_state))
2334 return PTR_ERR(crtc_state);
2336 primary_state = drm_atomic_get_plane_state(state, crtc->primary);
2337 if (IS_ERR(primary_state))
2338 return PTR_ERR(primary_state);
2342 WARN_ON(set->num_connectors);
2344 ret = drm_atomic_set_mode_for_crtc(crtc_state, NULL);
2348 crtc_state->active = false;
2350 ret = drm_atomic_set_crtc_for_plane(primary_state, NULL);
2354 drm_atomic_set_fb_for_plane(primary_state, NULL);
2360 WARN_ON(!set->num_connectors);
2362 ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
2366 crtc_state->active = true;
2368 ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
2372 drm_mode_get_hv_timing(set->mode, &hdisplay, &vdisplay);
2374 drm_atomic_set_fb_for_plane(primary_state, set->fb);
2375 primary_state->crtc_x = 0;
2376 primary_state->crtc_y = 0;
2377 primary_state->crtc_w = hdisplay;
2378 primary_state->crtc_h = vdisplay;
2379 primary_state->src_x = set->x << 16;
2380 primary_state->src_y = set->y << 16;
2381 if (drm_rotation_90_or_270(primary_state->rotation)) {
2382 primary_state->src_w = vdisplay << 16;
2383 primary_state->src_h = hdisplay << 16;
2385 primary_state->src_w = hdisplay << 16;
2386 primary_state->src_h = vdisplay << 16;
2390 ret = update_output_state(state, set);
2398 * drm_atomic_helper_disable_all - disable all currently active outputs
2400 * @ctx: lock acquisition context
2402 * Loops through all connectors, finding those that aren't turned off and then
2403 * turns them off by setting their DPMS mode to OFF and deactivating the CRTC
2404 * that they are connected to.
2406 * This is used for example in suspend/resume to disable all currently active
2407 * functions when suspending.
2409 * Note that if callers haven't already acquired all modeset locks this might
2410 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
2413 * 0 on success or a negative error code on failure.
2416 * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
2418 int drm_atomic_helper_disable_all(struct drm_device *dev,
2419 struct drm_modeset_acquire_ctx *ctx)
2421 struct drm_atomic_state *state;
2422 struct drm_connector *conn;
2423 struct drm_connector_list_iter conn_iter;
2426 state = drm_atomic_state_alloc(dev);
2430 state->acquire_ctx = ctx;
2432 drm_connector_list_iter_get(dev, &conn_iter);
2433 drm_for_each_connector_iter(conn, &conn_iter) {
2434 struct drm_crtc *crtc = conn->state->crtc;
2435 struct drm_crtc_state *crtc_state;
2437 if (!crtc || conn->dpms != DRM_MODE_DPMS_ON)
2440 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2441 if (IS_ERR(crtc_state)) {
2442 err = PTR_ERR(crtc_state);
2446 crtc_state->active = false;
2449 err = drm_atomic_commit(state);
2451 drm_connector_list_iter_put(&conn_iter);
2452 drm_atomic_state_put(state);
2455 EXPORT_SYMBOL(drm_atomic_helper_disable_all);
2458 * drm_atomic_helper_suspend - subsystem-level suspend helper
2461 * Duplicates the current atomic state, disables all active outputs and then
2462 * returns a pointer to the original atomic state to the caller. Drivers can
2463 * pass this pointer to the drm_atomic_helper_resume() helper upon resume to
2464 * restore the output configuration that was active at the time the system
2467 * Note that it is potentially unsafe to use this. The atomic state object
2468 * returned by this function is assumed to be persistent. Drivers must ensure
2469 * that this holds true. Before calling this function, drivers must make sure
2470 * to suspend fbdev emulation so that nothing can be using the device.
2473 * A pointer to a copy of the state before suspend on success or an ERR_PTR()-
2474 * encoded error code on failure. Drivers should store the returned atomic
2475 * state object and pass it to the drm_atomic_helper_resume() helper upon
2479 * drm_atomic_helper_duplicate_state(), drm_atomic_helper_disable_all(),
2480 * drm_atomic_helper_resume()
2482 struct drm_atomic_state *drm_atomic_helper_suspend(struct drm_device *dev)
2484 struct drm_modeset_acquire_ctx ctx;
2485 struct drm_atomic_state *state;
2488 drm_modeset_acquire_init(&ctx, 0);
2491 err = drm_modeset_lock_all_ctx(dev, &ctx);
2493 state = ERR_PTR(err);
2497 state = drm_atomic_helper_duplicate_state(dev, &ctx);
2501 err = drm_atomic_helper_disable_all(dev, &ctx);
2503 drm_atomic_state_put(state);
2504 state = ERR_PTR(err);
2509 if (PTR_ERR(state) == -EDEADLK) {
2510 drm_modeset_backoff(&ctx);
2514 drm_modeset_drop_locks(&ctx);
2515 drm_modeset_acquire_fini(&ctx);
2518 EXPORT_SYMBOL(drm_atomic_helper_suspend);
2521 * drm_atomic_helper_resume - subsystem-level resume helper
2523 * @state: atomic state to resume to
2525 * Calls drm_mode_config_reset() to synchronize hardware and software states,
2526 * grabs all modeset locks and commits the atomic state object. This can be
2527 * used in conjunction with the drm_atomic_helper_suspend() helper to
2528 * implement suspend/resume for drivers that support atomic mode-setting.
2531 * 0 on success or a negative error code on failure.
2534 * drm_atomic_helper_suspend()
2536 int drm_atomic_helper_resume(struct drm_device *dev,
2537 struct drm_atomic_state *state)
2539 struct drm_mode_config *config = &dev->mode_config;
2542 drm_mode_config_reset(dev);
2543 drm_modeset_lock_all(dev);
2544 state->acquire_ctx = config->acquire_ctx;
2545 err = drm_atomic_commit(state);
2546 drm_modeset_unlock_all(dev);
2550 EXPORT_SYMBOL(drm_atomic_helper_resume);
2553 * drm_atomic_helper_crtc_set_property - helper for crtc properties
2555 * @property: DRM property
2556 * @val: value of property
2558 * Provides a default crtc set_property handler using the atomic driver
2562 * Zero on success, error code on failure
2565 drm_atomic_helper_crtc_set_property(struct drm_crtc *crtc,
2566 struct drm_property *property,
2569 struct drm_atomic_state *state;
2570 struct drm_crtc_state *crtc_state;
2573 state = drm_atomic_state_alloc(crtc->dev);
2577 /* ->set_property is always called with all locks held. */
2578 state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
2580 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2581 if (IS_ERR(crtc_state)) {
2582 ret = PTR_ERR(crtc_state);
2586 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
2591 ret = drm_atomic_commit(state);
2593 if (ret == -EDEADLK)
2596 drm_atomic_state_put(state);
2600 drm_atomic_state_clear(state);
2601 drm_atomic_legacy_backoff(state);
2605 EXPORT_SYMBOL(drm_atomic_helper_crtc_set_property);
2608 * drm_atomic_helper_plane_set_property - helper for plane properties
2610 * @property: DRM property
2611 * @val: value of property
2613 * Provides a default plane set_property handler using the atomic driver
2617 * Zero on success, error code on failure
2620 drm_atomic_helper_plane_set_property(struct drm_plane *plane,
2621 struct drm_property *property,
2624 struct drm_atomic_state *state;
2625 struct drm_plane_state *plane_state;
2628 state = drm_atomic_state_alloc(plane->dev);
2632 /* ->set_property is always called with all locks held. */
2633 state->acquire_ctx = plane->dev->mode_config.acquire_ctx;
2635 plane_state = drm_atomic_get_plane_state(state, plane);
2636 if (IS_ERR(plane_state)) {
2637 ret = PTR_ERR(plane_state);
2641 ret = drm_atomic_plane_set_property(plane, plane_state,
2646 ret = drm_atomic_commit(state);
2648 if (ret == -EDEADLK)
2651 drm_atomic_state_put(state);
2655 drm_atomic_state_clear(state);
2656 drm_atomic_legacy_backoff(state);
2660 EXPORT_SYMBOL(drm_atomic_helper_plane_set_property);
2663 * drm_atomic_helper_connector_set_property - helper for connector properties
2664 * @connector: DRM connector
2665 * @property: DRM property
2666 * @val: value of property
2668 * Provides a default connector set_property handler using the atomic driver
2672 * Zero on success, error code on failure
2675 drm_atomic_helper_connector_set_property(struct drm_connector *connector,
2676 struct drm_property *property,
2679 struct drm_atomic_state *state;
2680 struct drm_connector_state *connector_state;
2683 state = drm_atomic_state_alloc(connector->dev);
2687 /* ->set_property is always called with all locks held. */
2688 state->acquire_ctx = connector->dev->mode_config.acquire_ctx;
2690 connector_state = drm_atomic_get_connector_state(state, connector);
2691 if (IS_ERR(connector_state)) {
2692 ret = PTR_ERR(connector_state);
2696 ret = drm_atomic_connector_set_property(connector, connector_state,
2701 ret = drm_atomic_commit(state);
2703 if (ret == -EDEADLK)
2706 drm_atomic_state_put(state);
2710 drm_atomic_state_clear(state);
2711 drm_atomic_legacy_backoff(state);
2715 EXPORT_SYMBOL(drm_atomic_helper_connector_set_property);
2717 static int page_flip_common(
2718 struct drm_atomic_state *state,
2719 struct drm_crtc *crtc,
2720 struct drm_framebuffer *fb,
2721 struct drm_pending_vblank_event *event)
2723 struct drm_plane *plane = crtc->primary;
2724 struct drm_plane_state *plane_state;
2725 struct drm_crtc_state *crtc_state;
2728 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2729 if (IS_ERR(crtc_state))
2730 return PTR_ERR(crtc_state);
2732 crtc_state->event = event;
2734 plane_state = drm_atomic_get_plane_state(state, plane);
2735 if (IS_ERR(plane_state))
2736 return PTR_ERR(plane_state);
2739 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
2742 drm_atomic_set_fb_for_plane(plane_state, fb);
2744 /* Make sure we don't accidentally do a full modeset. */
2745 state->allow_modeset = false;
2746 if (!crtc_state->active) {
2747 DRM_DEBUG_ATOMIC("[CRTC:%d] disabled, rejecting legacy flip\n",
2756 * drm_atomic_helper_page_flip - execute a legacy page flip
2758 * @fb: DRM framebuffer
2759 * @event: optional DRM event to signal upon completion
2760 * @flags: flip flags for non-vblank sync'ed updates
2762 * Provides a default &drm_crtc_funcs.page_flip implementation
2763 * using the atomic driver interface.
2765 * Note that for now so called async page flips (i.e. updates which are not
2766 * synchronized to vblank) are not supported, since the atomic interfaces have
2767 * no provisions for this yet.
2770 * Returns 0 on success, negative errno numbers on failure.
2773 * drm_atomic_helper_page_flip_target()
2775 int drm_atomic_helper_page_flip(struct drm_crtc *crtc,
2776 struct drm_framebuffer *fb,
2777 struct drm_pending_vblank_event *event,
2780 struct drm_plane *plane = crtc->primary;
2781 struct drm_atomic_state *state;
2784 if (flags & DRM_MODE_PAGE_FLIP_ASYNC)
2787 state = drm_atomic_state_alloc(plane->dev);
2791 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2794 ret = page_flip_common(state, crtc, fb, event);
2798 ret = drm_atomic_nonblocking_commit(state);
2801 if (ret == -EDEADLK)
2804 drm_atomic_state_put(state);
2808 drm_atomic_state_clear(state);
2809 drm_atomic_legacy_backoff(state);
2812 * Someone might have exchanged the framebuffer while we dropped locks
2813 * in the backoff code. We need to fix up the fb refcount tracking the
2816 plane->old_fb = plane->fb;
2820 EXPORT_SYMBOL(drm_atomic_helper_page_flip);
2823 * drm_atomic_helper_page_flip_target - do page flip on target vblank period.
2825 * @fb: DRM framebuffer
2826 * @event: optional DRM event to signal upon completion
2827 * @flags: flip flags for non-vblank sync'ed updates
2828 * @target: specifying the target vblank period when the flip to take effect
2830 * Provides a default &drm_crtc_funcs.page_flip_target implementation.
2831 * Similar to drm_atomic_helper_page_flip() with extra parameter to specify
2832 * target vblank period to flip.
2835 * Returns 0 on success, negative errno numbers on failure.
2837 int drm_atomic_helper_page_flip_target(
2838 struct drm_crtc *crtc,
2839 struct drm_framebuffer *fb,
2840 struct drm_pending_vblank_event *event,
2844 struct drm_plane *plane = crtc->primary;
2845 struct drm_atomic_state *state;
2846 struct drm_crtc_state *crtc_state;
2849 if (flags & DRM_MODE_PAGE_FLIP_ASYNC)
2852 state = drm_atomic_state_alloc(plane->dev);
2856 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2859 ret = page_flip_common(state, crtc, fb, event);
2863 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
2864 if (WARN_ON(!crtc_state)) {
2868 crtc_state->target_vblank = target;
2870 ret = drm_atomic_nonblocking_commit(state);
2873 if (ret == -EDEADLK)
2876 drm_atomic_state_put(state);
2880 drm_atomic_state_clear(state);
2881 drm_atomic_legacy_backoff(state);
2884 * Someone might have exchanged the framebuffer while we dropped locks
2885 * in the backoff code. We need to fix up the fb refcount tracking the
2888 plane->old_fb = plane->fb;
2892 EXPORT_SYMBOL(drm_atomic_helper_page_flip_target);
2895 * drm_atomic_helper_connector_dpms() - connector dpms helper implementation
2896 * @connector: affected connector
2899 * This is the main helper function provided by the atomic helper framework for
2900 * implementing the legacy DPMS connector interface. It computes the new desired
2901 * &drm_crtc_state.active state for the corresponding CRTC (if the connector is
2902 * enabled) and updates it.
2905 * Returns 0 on success, negative errno numbers on failure.
2907 int drm_atomic_helper_connector_dpms(struct drm_connector *connector,
2910 struct drm_mode_config *config = &connector->dev->mode_config;
2911 struct drm_atomic_state *state;
2912 struct drm_crtc_state *crtc_state;
2913 struct drm_crtc *crtc;
2914 struct drm_connector *tmp_connector;
2915 struct drm_connector_list_iter conn_iter;
2917 bool active = false;
2918 int old_mode = connector->dpms;
2920 if (mode != DRM_MODE_DPMS_ON)
2921 mode = DRM_MODE_DPMS_OFF;
2923 connector->dpms = mode;
2924 crtc = connector->state->crtc;
2929 state = drm_atomic_state_alloc(connector->dev);
2933 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2935 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2936 if (IS_ERR(crtc_state)) {
2937 ret = PTR_ERR(crtc_state);
2941 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
2943 drm_connector_list_iter_get(connector->dev, &conn_iter);
2944 drm_for_each_connector_iter(tmp_connector, &conn_iter) {
2945 if (tmp_connector->state->crtc != crtc)
2948 if (tmp_connector->dpms == DRM_MODE_DPMS_ON) {
2953 drm_connector_list_iter_put(&conn_iter);
2954 crtc_state->active = active;
2956 ret = drm_atomic_commit(state);
2958 if (ret == -EDEADLK)
2961 connector->dpms = old_mode;
2962 drm_atomic_state_put(state);
2966 drm_atomic_state_clear(state);
2967 drm_atomic_legacy_backoff(state);
2971 EXPORT_SYMBOL(drm_atomic_helper_connector_dpms);
2974 * drm_atomic_helper_best_encoder - Helper for
2975 * &drm_connector_helper_funcs.best_encoder callback
2976 * @connector: Connector control structure
2978 * This is a &drm_connector_helper_funcs.best_encoder callback helper for
2979 * connectors that support exactly 1 encoder, statically determined at driver
2982 struct drm_encoder *
2983 drm_atomic_helper_best_encoder(struct drm_connector *connector)
2985 WARN_ON(connector->encoder_ids[1]);
2986 return drm_encoder_find(connector->dev, connector->encoder_ids[0]);
2988 EXPORT_SYMBOL(drm_atomic_helper_best_encoder);
2991 * DOC: atomic state reset and initialization
2993 * Both the drm core and the atomic helpers assume that there is always the full
2994 * and correct atomic software state for all connectors, CRTCs and planes
2995 * available. Which is a bit a problem on driver load and also after system
2996 * suspend. One way to solve this is to have a hardware state read-out
2997 * infrastructure which reconstructs the full software state (e.g. the i915
3000 * The simpler solution is to just reset the software state to everything off,
3001 * which is easiest to do by calling drm_mode_config_reset(). To facilitate this
3002 * the atomic helpers provide default reset implementations for all hooks.
3004 * On the upside the precise state tracking of atomic simplifies system suspend
3005 * and resume a lot. For drivers using drm_mode_config_reset() a complete recipe
3006 * is implemented in drm_atomic_helper_suspend() and drm_atomic_helper_resume().
3007 * For other drivers the building blocks are split out, see the documentation
3008 * for these functions.
3012 * drm_atomic_helper_crtc_reset - default &drm_crtc_funcs.reset hook for CRTCs
3015 * Resets the atomic state for @crtc by freeing the state pointer (which might
3016 * be NULL, e.g. at driver load time) and allocating a new empty state object.
3018 void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc)
3021 __drm_atomic_helper_crtc_destroy_state(crtc->state);
3024 crtc->state = kzalloc(sizeof(*crtc->state), GFP_KERNEL);
3027 crtc->state->crtc = crtc;
3029 EXPORT_SYMBOL(drm_atomic_helper_crtc_reset);
3032 * __drm_atomic_helper_crtc_duplicate_state - copy atomic CRTC state
3033 * @crtc: CRTC object
3034 * @state: atomic CRTC state
3036 * Copies atomic state from a CRTC's current state and resets inferred values.
3037 * This is useful for drivers that subclass the CRTC state.
3039 void __drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc,
3040 struct drm_crtc_state *state)
3042 memcpy(state, crtc->state, sizeof(*state));
3044 if (state->mode_blob)
3045 drm_property_reference_blob(state->mode_blob);
3046 if (state->degamma_lut)
3047 drm_property_reference_blob(state->degamma_lut);
3049 drm_property_reference_blob(state->ctm);
3050 if (state->gamma_lut)
3051 drm_property_reference_blob(state->gamma_lut);
3052 state->mode_changed = false;
3053 state->active_changed = false;
3054 state->planes_changed = false;
3055 state->connectors_changed = false;
3056 state->color_mgmt_changed = false;
3057 state->zpos_changed = false;
3058 state->event = NULL;
3060 EXPORT_SYMBOL(__drm_atomic_helper_crtc_duplicate_state);
3063 * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
3066 * Default CRTC state duplicate hook for drivers which don't have their own
3067 * subclassed CRTC state structure.
3069 struct drm_crtc_state *
3070 drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc)
3072 struct drm_crtc_state *state;
3074 if (WARN_ON(!crtc->state))
3077 state = kmalloc(sizeof(*state), GFP_KERNEL);
3079 __drm_atomic_helper_crtc_duplicate_state(crtc, state);
3083 EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state);
3086 * __drm_atomic_helper_crtc_destroy_state - release CRTC state
3087 * @state: CRTC state object to release
3089 * Releases all resources stored in the CRTC state without actually freeing
3090 * the memory of the CRTC state. This is useful for drivers that subclass the
3093 void __drm_atomic_helper_crtc_destroy_state(struct drm_crtc_state *state)
3095 drm_property_unreference_blob(state->mode_blob);
3096 drm_property_unreference_blob(state->degamma_lut);
3097 drm_property_unreference_blob(state->ctm);
3098 drm_property_unreference_blob(state->gamma_lut);
3100 EXPORT_SYMBOL(__drm_atomic_helper_crtc_destroy_state);
3103 * drm_atomic_helper_crtc_destroy_state - default state destroy hook
3105 * @state: CRTC state object to release
3107 * Default CRTC state destroy hook for drivers which don't have their own
3108 * subclassed CRTC state structure.
3110 void drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
3111 struct drm_crtc_state *state)
3113 __drm_atomic_helper_crtc_destroy_state(state);
3116 EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state);
3119 * drm_atomic_helper_plane_reset - default &drm_plane_funcs.reset hook for planes
3122 * Resets the atomic state for @plane by freeing the state pointer (which might
3123 * be NULL, e.g. at driver load time) and allocating a new empty state object.
3125 void drm_atomic_helper_plane_reset(struct drm_plane *plane)
3128 __drm_atomic_helper_plane_destroy_state(plane->state);
3130 kfree(plane->state);
3131 plane->state = kzalloc(sizeof(*plane->state), GFP_KERNEL);
3134 plane->state->plane = plane;
3135 plane->state->rotation = DRM_ROTATE_0;
3138 EXPORT_SYMBOL(drm_atomic_helper_plane_reset);
3141 * __drm_atomic_helper_plane_duplicate_state - copy atomic plane state
3142 * @plane: plane object
3143 * @state: atomic plane state
3145 * Copies atomic state from a plane's current state. This is useful for
3146 * drivers that subclass the plane state.
3148 void __drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane,
3149 struct drm_plane_state *state)
3151 memcpy(state, plane->state, sizeof(*state));
3154 drm_framebuffer_reference(state->fb);
3156 state->fence = NULL;
3158 EXPORT_SYMBOL(__drm_atomic_helper_plane_duplicate_state);
3161 * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
3164 * Default plane state duplicate hook for drivers which don't have their own
3165 * subclassed plane state structure.
3167 struct drm_plane_state *
3168 drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane)
3170 struct drm_plane_state *state;
3172 if (WARN_ON(!plane->state))
3175 state = kmalloc(sizeof(*state), GFP_KERNEL);
3177 __drm_atomic_helper_plane_duplicate_state(plane, state);
3181 EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state);
3184 * __drm_atomic_helper_plane_destroy_state - release plane state
3185 * @state: plane state object to release
3187 * Releases all resources stored in the plane state without actually freeing
3188 * the memory of the plane state. This is useful for drivers that subclass the
3191 void __drm_atomic_helper_plane_destroy_state(struct drm_plane_state *state)
3194 drm_framebuffer_unreference(state->fb);
3197 dma_fence_put(state->fence);
3199 EXPORT_SYMBOL(__drm_atomic_helper_plane_destroy_state);
3202 * drm_atomic_helper_plane_destroy_state - default state destroy hook
3204 * @state: plane state object to release
3206 * Default plane state destroy hook for drivers which don't have their own
3207 * subclassed plane state structure.
3209 void drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
3210 struct drm_plane_state *state)
3212 __drm_atomic_helper_plane_destroy_state(state);
3215 EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state);
3218 * __drm_atomic_helper_connector_reset - reset state on connector
3219 * @connector: drm connector
3220 * @conn_state: connector state to assign
3222 * Initializes the newly allocated @conn_state and assigns it to
3223 * the &drm_conector->state pointer of @connector, usually required when
3224 * initializing the drivers or when called from the &drm_connector_funcs.reset
3227 * This is useful for drivers that subclass the connector state.
3230 __drm_atomic_helper_connector_reset(struct drm_connector *connector,
3231 struct drm_connector_state *conn_state)
3234 conn_state->connector = connector;
3236 connector->state = conn_state;
3238 EXPORT_SYMBOL(__drm_atomic_helper_connector_reset);
3241 * drm_atomic_helper_connector_reset - default &drm_connector_funcs.reset hook for connectors
3242 * @connector: drm connector
3244 * Resets the atomic state for @connector by freeing the state pointer (which
3245 * might be NULL, e.g. at driver load time) and allocating a new empty state
3248 void drm_atomic_helper_connector_reset(struct drm_connector *connector)
3250 struct drm_connector_state *conn_state =
3251 kzalloc(sizeof(*conn_state), GFP_KERNEL);
3253 if (connector->state)
3254 __drm_atomic_helper_connector_destroy_state(connector->state);
3256 kfree(connector->state);
3257 __drm_atomic_helper_connector_reset(connector, conn_state);
3259 EXPORT_SYMBOL(drm_atomic_helper_connector_reset);
3262 * __drm_atomic_helper_connector_duplicate_state - copy atomic connector state
3263 * @connector: connector object
3264 * @state: atomic connector state
3266 * Copies atomic state from a connector's current state. This is useful for
3267 * drivers that subclass the connector state.
3270 __drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector,
3271 struct drm_connector_state *state)
3273 memcpy(state, connector->state, sizeof(*state));
3275 drm_connector_reference(connector);
3277 EXPORT_SYMBOL(__drm_atomic_helper_connector_duplicate_state);
3280 * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
3281 * @connector: drm connector
3283 * Default connector state duplicate hook for drivers which don't have their own
3284 * subclassed connector state structure.
3286 struct drm_connector_state *
3287 drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
3289 struct drm_connector_state *state;
3291 if (WARN_ON(!connector->state))
3294 state = kmalloc(sizeof(*state), GFP_KERNEL);
3296 __drm_atomic_helper_connector_duplicate_state(connector, state);
3300 EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
3303 * drm_atomic_helper_duplicate_state - duplicate an atomic state object
3305 * @ctx: lock acquisition context
3307 * Makes a copy of the current atomic state by looping over all objects and
3308 * duplicating their respective states. This is used for example by suspend/
3309 * resume support code to save the state prior to suspend such that it can
3310 * be restored upon resume.
3312 * Note that this treats atomic state as persistent between save and restore.
3313 * Drivers must make sure that this is possible and won't result in confusion
3314 * or erroneous behaviour.
3316 * Note that if callers haven't already acquired all modeset locks this might
3317 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
3320 * A pointer to the copy of the atomic state object on success or an
3321 * ERR_PTR()-encoded error code on failure.
3324 * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
3326 struct drm_atomic_state *
3327 drm_atomic_helper_duplicate_state(struct drm_device *dev,
3328 struct drm_modeset_acquire_ctx *ctx)
3330 struct drm_atomic_state *state;
3331 struct drm_connector *conn;
3332 struct drm_connector_list_iter conn_iter;
3333 struct drm_plane *plane;
3334 struct drm_crtc *crtc;
3337 state = drm_atomic_state_alloc(dev);
3339 return ERR_PTR(-ENOMEM);
3341 state->acquire_ctx = ctx;
3343 drm_for_each_crtc(crtc, dev) {
3344 struct drm_crtc_state *crtc_state;
3346 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3347 if (IS_ERR(crtc_state)) {
3348 err = PTR_ERR(crtc_state);
3353 drm_for_each_plane(plane, dev) {
3354 struct drm_plane_state *plane_state;
3356 plane_state = drm_atomic_get_plane_state(state, plane);
3357 if (IS_ERR(plane_state)) {
3358 err = PTR_ERR(plane_state);
3363 drm_connector_list_iter_get(dev, &conn_iter);
3364 drm_for_each_connector_iter(conn, &conn_iter) {
3365 struct drm_connector_state *conn_state;
3367 conn_state = drm_atomic_get_connector_state(state, conn);
3368 if (IS_ERR(conn_state)) {
3369 err = PTR_ERR(conn_state);
3370 drm_connector_list_iter_put(&conn_iter);
3374 drm_connector_list_iter_put(&conn_iter);
3376 /* clear the acquire context so that it isn't accidentally reused */
3377 state->acquire_ctx = NULL;
3381 drm_atomic_state_put(state);
3382 state = ERR_PTR(err);
3387 EXPORT_SYMBOL(drm_atomic_helper_duplicate_state);
3390 * __drm_atomic_helper_connector_destroy_state - release connector state
3391 * @state: connector state object to release
3393 * Releases all resources stored in the connector state without actually
3394 * freeing the memory of the connector state. This is useful for drivers that
3395 * subclass the connector state.
3398 __drm_atomic_helper_connector_destroy_state(struct drm_connector_state *state)
3401 drm_connector_unreference(state->connector);
3403 EXPORT_SYMBOL(__drm_atomic_helper_connector_destroy_state);
3406 * drm_atomic_helper_connector_destroy_state - default state destroy hook
3407 * @connector: drm connector
3408 * @state: connector state object to release
3410 * Default connector state destroy hook for drivers which don't have their own
3411 * subclassed connector state structure.
3413 void drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
3414 struct drm_connector_state *state)
3416 __drm_atomic_helper_connector_destroy_state(state);
3419 EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state);
3422 * drm_atomic_helper_legacy_gamma_set - set the legacy gamma correction table
3423 * @crtc: CRTC object
3424 * @red: red correction table
3425 * @green: green correction table
3426 * @blue: green correction table
3427 * @size: size of the tables
3429 * Implements support for legacy gamma correction table for drivers
3430 * that support color management through the DEGAMMA_LUT/GAMMA_LUT
3433 int drm_atomic_helper_legacy_gamma_set(struct drm_crtc *crtc,
3434 u16 *red, u16 *green, u16 *blue,
3437 struct drm_device *dev = crtc->dev;
3438 struct drm_mode_config *config = &dev->mode_config;
3439 struct drm_atomic_state *state;
3440 struct drm_crtc_state *crtc_state;
3441 struct drm_property_blob *blob = NULL;
3442 struct drm_color_lut *blob_data;
3445 state = drm_atomic_state_alloc(crtc->dev);
3449 blob = drm_property_create_blob(dev,
3450 sizeof(struct drm_color_lut) * size,
3453 ret = PTR_ERR(blob);
3458 /* Prepare GAMMA_LUT with the legacy values. */
3459 blob_data = (struct drm_color_lut *) blob->data;
3460 for (i = 0; i < size; i++) {
3461 blob_data[i].red = red[i];
3462 blob_data[i].green = green[i];
3463 blob_data[i].blue = blue[i];
3466 state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
3468 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3469 if (IS_ERR(crtc_state)) {
3470 ret = PTR_ERR(crtc_state);
3474 /* Reset DEGAMMA_LUT and CTM properties. */
3475 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
3476 config->degamma_lut_property, 0);
3480 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
3481 config->ctm_property, 0);
3485 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
3486 config->gamma_lut_property, blob->base.id);
3490 ret = drm_atomic_commit(state);
3492 if (ret == -EDEADLK)
3495 drm_atomic_state_put(state);
3496 drm_property_unreference_blob(blob);
3500 drm_atomic_state_clear(state);
3501 drm_atomic_legacy_backoff(state);
3505 EXPORT_SYMBOL(drm_atomic_helper_legacy_gamma_set);