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/fence.h>
38 * This helper library provides implementations of check and commit functions on
39 * top of the CRTC modeset helper callbacks and the plane helper callbacks. It
40 * also provides convenience implementations for the atomic state handling
41 * callbacks for drivers which don't need to subclass the drm core structures to
42 * add their own additional internal state.
44 * This library also provides default implementations for the check callback in
45 * drm_atomic_helper_check() and for the commit callback with
46 * drm_atomic_helper_commit(). But the individual stages and callbacks are
47 * exposed to allow drivers to mix and match and e.g. use the plane helpers only
48 * together with a driver private modeset implementation.
50 * This library also provides implementations for all the legacy driver
51 * interfaces on top of the atomic interface. See drm_atomic_helper_set_config(),
52 * drm_atomic_helper_disable_plane(), drm_atomic_helper_disable_plane() and the
53 * various functions to implement set_property callbacks. New drivers must not
54 * implement these functions themselves but must use the provided helpers.
56 * The atomic helper uses the same function table structures as all other
57 * modesetting helpers. See the documentation for struct &drm_crtc_helper_funcs,
58 * struct &drm_encoder_helper_funcs and struct &drm_connector_helper_funcs. It
59 * also shares the struct &drm_plane_helper_funcs function table with the plane
63 drm_atomic_helper_plane_changed(struct drm_atomic_state *state,
64 struct drm_plane_state *plane_state,
65 struct drm_plane *plane)
67 struct drm_crtc_state *crtc_state;
69 if (plane->state->crtc) {
70 crtc_state = state->crtc_states[drm_crtc_index(plane->state->crtc)];
72 if (WARN_ON(!crtc_state))
75 crtc_state->planes_changed = true;
78 if (plane_state->crtc) {
80 state->crtc_states[drm_crtc_index(plane_state->crtc)];
82 if (WARN_ON(!crtc_state))
85 crtc_state->planes_changed = true;
89 static int handle_conflicting_encoders(struct drm_atomic_state *state,
90 bool disable_conflicting_encoders)
92 struct drm_connector_state *conn_state;
93 struct drm_connector *connector;
94 struct drm_encoder *encoder;
95 unsigned encoder_mask = 0;
99 * First loop, find all newly assigned encoders from the connectors
100 * part of the state. If the same encoder is assigned to multiple
101 * connectors bail out.
103 for_each_connector_in_state(state, connector, conn_state, i) {
104 const struct drm_connector_helper_funcs *funcs = connector->helper_private;
105 struct drm_encoder *new_encoder;
107 if (!conn_state->crtc)
110 if (funcs->atomic_best_encoder)
111 new_encoder = funcs->atomic_best_encoder(connector, conn_state);
113 new_encoder = funcs->best_encoder(connector);
116 if (encoder_mask & (1 << drm_encoder_index(new_encoder))) {
117 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] on [CONNECTOR:%d:%s] already assigned\n",
118 new_encoder->base.id, new_encoder->name,
119 connector->base.id, connector->name);
124 encoder_mask |= 1 << drm_encoder_index(new_encoder);
132 * Second loop, iterate over all connectors not part of the state.
134 * If a conflicting encoder is found and disable_conflicting_encoders
135 * is not set, an error is returned. Userspace can provide a solution
136 * through the atomic ioctl.
138 * If the flag is set conflicting connectors are removed from the crtc
139 * and the crtc is disabled if no encoder is left. This preserves
140 * compatibility with the legacy set_config behavior.
142 drm_for_each_connector(connector, state->dev) {
143 struct drm_crtc_state *crtc_state;
145 if (drm_atomic_get_existing_connector_state(state, connector))
148 encoder = connector->state->best_encoder;
149 if (!encoder || !(encoder_mask & (1 << drm_encoder_index(encoder))))
152 if (!disable_conflicting_encoders) {
153 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s] by [CONNECTOR:%d:%s]\n",
154 encoder->base.id, encoder->name,
155 connector->state->crtc->base.id,
156 connector->state->crtc->name,
157 connector->base.id, connector->name);
161 conn_state = drm_atomic_get_connector_state(state, connector);
162 if (IS_ERR(conn_state))
163 return PTR_ERR(conn_state);
165 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s], disabling [CONNECTOR:%d:%s]\n",
166 encoder->base.id, encoder->name,
167 conn_state->crtc->base.id, conn_state->crtc->name,
168 connector->base.id, connector->name);
170 crtc_state = drm_atomic_get_existing_crtc_state(state, conn_state->crtc);
172 ret = drm_atomic_set_crtc_for_connector(conn_state, NULL);
176 if (!crtc_state->connector_mask) {
177 ret = drm_atomic_set_mode_prop_for_crtc(crtc_state,
182 crtc_state->active = false;
190 set_best_encoder(struct drm_atomic_state *state,
191 struct drm_connector_state *conn_state,
192 struct drm_encoder *encoder)
194 struct drm_crtc_state *crtc_state;
195 struct drm_crtc *crtc;
197 if (conn_state->best_encoder) {
198 /* Unset the encoder_mask in the old crtc state. */
199 crtc = conn_state->connector->state->crtc;
201 /* A NULL crtc is an error here because we should have
202 * duplicated a NULL best_encoder when crtc was NULL.
203 * As an exception restoring duplicated atomic state
204 * during resume is allowed, so don't warn when
205 * best_encoder is equal to encoder we intend to set.
207 WARN_ON(!crtc && encoder != conn_state->best_encoder);
209 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
211 crtc_state->encoder_mask &=
212 ~(1 << drm_encoder_index(conn_state->best_encoder));
217 crtc = conn_state->crtc;
220 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
222 crtc_state->encoder_mask |=
223 1 << drm_encoder_index(encoder);
227 conn_state->best_encoder = encoder;
231 steal_encoder(struct drm_atomic_state *state,
232 struct drm_encoder *encoder)
234 struct drm_crtc_state *crtc_state;
235 struct drm_connector *connector;
236 struct drm_connector_state *connector_state;
239 for_each_connector_in_state(state, connector, connector_state, i) {
240 struct drm_crtc *encoder_crtc;
242 if (connector_state->best_encoder != encoder)
245 encoder_crtc = connector->state->crtc;
247 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s], stealing it\n",
248 encoder->base.id, encoder->name,
249 encoder_crtc->base.id, encoder_crtc->name);
251 set_best_encoder(state, connector_state, NULL);
253 crtc_state = drm_atomic_get_existing_crtc_state(state, encoder_crtc);
254 crtc_state->connectors_changed = true;
261 update_connector_routing(struct drm_atomic_state *state,
262 struct drm_connector *connector,
263 struct drm_connector_state *connector_state)
265 const struct drm_connector_helper_funcs *funcs;
266 struct drm_encoder *new_encoder;
267 struct drm_crtc_state *crtc_state;
269 DRM_DEBUG_ATOMIC("Updating routing for [CONNECTOR:%d:%s]\n",
273 if (connector->state->crtc != connector_state->crtc) {
274 if (connector->state->crtc) {
275 crtc_state = drm_atomic_get_existing_crtc_state(state, connector->state->crtc);
276 crtc_state->connectors_changed = true;
279 if (connector_state->crtc) {
280 crtc_state = drm_atomic_get_existing_crtc_state(state, connector_state->crtc);
281 crtc_state->connectors_changed = true;
285 if (!connector_state->crtc) {
286 DRM_DEBUG_ATOMIC("Disabling [CONNECTOR:%d:%s]\n",
290 set_best_encoder(state, connector_state, NULL);
295 funcs = connector->helper_private;
297 if (funcs->atomic_best_encoder)
298 new_encoder = funcs->atomic_best_encoder(connector,
301 new_encoder = funcs->best_encoder(connector);
304 DRM_DEBUG_ATOMIC("No suitable encoder found for [CONNECTOR:%d:%s]\n",
310 if (!drm_encoder_crtc_ok(new_encoder, connector_state->crtc)) {
311 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] incompatible with [CRTC:%d]\n",
312 new_encoder->base.id,
314 connector_state->crtc->base.id);
318 if (new_encoder == connector_state->best_encoder) {
319 set_best_encoder(state, connector_state, new_encoder);
321 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] keeps [ENCODER:%d:%s], now on [CRTC:%d:%s]\n",
324 new_encoder->base.id,
326 connector_state->crtc->base.id,
327 connector_state->crtc->name);
332 steal_encoder(state, new_encoder);
334 set_best_encoder(state, connector_state, new_encoder);
336 crtc_state = drm_atomic_get_existing_crtc_state(state, connector_state->crtc);
337 crtc_state->connectors_changed = true;
339 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] using [ENCODER:%d:%s] on [CRTC:%d:%s]\n",
342 new_encoder->base.id,
344 connector_state->crtc->base.id,
345 connector_state->crtc->name);
351 mode_fixup(struct drm_atomic_state *state)
353 struct drm_crtc *crtc;
354 struct drm_crtc_state *crtc_state;
355 struct drm_connector *connector;
356 struct drm_connector_state *conn_state;
360 for_each_crtc_in_state(state, crtc, crtc_state, i) {
361 if (!crtc_state->mode_changed &&
362 !crtc_state->connectors_changed)
365 drm_mode_copy(&crtc_state->adjusted_mode, &crtc_state->mode);
368 for_each_connector_in_state(state, connector, conn_state, i) {
369 const struct drm_encoder_helper_funcs *funcs;
370 struct drm_encoder *encoder;
372 WARN_ON(!!conn_state->best_encoder != !!conn_state->crtc);
374 if (!conn_state->crtc || !conn_state->best_encoder)
378 state->crtc_states[drm_crtc_index(conn_state->crtc)];
381 * Each encoder has at most one connector (since we always steal
382 * it away), so we won't call ->mode_fixup twice.
384 encoder = conn_state->best_encoder;
385 funcs = encoder->helper_private;
389 ret = drm_bridge_mode_fixup(encoder->bridge, &crtc_state->mode,
390 &crtc_state->adjusted_mode);
392 DRM_DEBUG_ATOMIC("Bridge fixup failed\n");
396 if (funcs->atomic_check) {
397 ret = funcs->atomic_check(encoder, crtc_state,
400 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] check failed\n",
401 encoder->base.id, encoder->name);
404 } else if (funcs->mode_fixup) {
405 ret = funcs->mode_fixup(encoder, &crtc_state->mode,
406 &crtc_state->adjusted_mode);
408 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] fixup failed\n",
409 encoder->base.id, encoder->name);
415 for_each_crtc_in_state(state, crtc, crtc_state, i) {
416 const struct drm_crtc_helper_funcs *funcs;
418 if (!crtc_state->mode_changed &&
419 !crtc_state->connectors_changed)
422 funcs = crtc->helper_private;
423 if (!funcs->mode_fixup)
426 ret = funcs->mode_fixup(crtc, &crtc_state->mode,
427 &crtc_state->adjusted_mode);
429 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] fixup failed\n",
430 crtc->base.id, crtc->name);
439 * drm_atomic_helper_check_modeset - validate state object for modeset changes
441 * @state: the driver state object
443 * Check the state object to see if the requested state is physically possible.
444 * This does all the crtc and connector related computations for an atomic
445 * update and adds any additional connectors needed for full modesets and calls
446 * down into ->mode_fixup functions of the driver backend.
448 * crtc_state->mode_changed is set when the input mode is changed.
449 * crtc_state->connectors_changed is set when a connector is added or
450 * removed from the crtc.
451 * crtc_state->active_changed is set when crtc_state->active changes,
452 * which is used for dpms.
456 * Drivers which update ->mode_changed (e.g. in their ->atomic_check hooks if a
457 * plane update can't be done without a full modeset) _must_ call this function
458 * afterwards after that change. It is permitted to call this function multiple
459 * times for the same update, e.g. when the ->atomic_check functions depend upon
460 * the adjusted dotclock for fifo space allocation and watermark computation.
463 * Zero for success or -errno
466 drm_atomic_helper_check_modeset(struct drm_device *dev,
467 struct drm_atomic_state *state)
469 struct drm_crtc *crtc;
470 struct drm_crtc_state *crtc_state;
471 struct drm_connector *connector;
472 struct drm_connector_state *connector_state;
475 for_each_crtc_in_state(state, crtc, crtc_state, i) {
476 if (!drm_mode_equal(&crtc->state->mode, &crtc_state->mode)) {
477 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] mode changed\n",
478 crtc->base.id, crtc->name);
479 crtc_state->mode_changed = true;
482 if (crtc->state->enable != crtc_state->enable) {
483 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enable changed\n",
484 crtc->base.id, crtc->name);
487 * For clarity this assignment is done here, but
488 * enable == 0 is only true when there are no
489 * connectors and a NULL mode.
491 * The other way around is true as well. enable != 0
492 * iff connectors are attached and a mode is set.
494 crtc_state->mode_changed = true;
495 crtc_state->connectors_changed = true;
499 ret = handle_conflicting_encoders(state, state->legacy_set_config);
503 for_each_connector_in_state(state, connector, connector_state, i) {
505 * This only sets crtc->mode_changed for routing changes,
506 * drivers must set crtc->mode_changed themselves when connector
507 * properties need to be updated.
509 ret = update_connector_routing(state, connector,
516 * After all the routing has been prepared we need to add in any
517 * connector which is itself unchanged, but who's crtc changes it's
518 * configuration. This must be done before calling mode_fixup in case a
519 * crtc only changed its mode but has the same set of connectors.
521 for_each_crtc_in_state(state, crtc, crtc_state, i) {
522 bool has_connectors =
523 !!crtc_state->connector_mask;
526 * We must set ->active_changed after walking connectors for
527 * otherwise an update that only changes active would result in
528 * a full modeset because update_connector_routing force that.
530 if (crtc->state->active != crtc_state->active) {
531 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active changed\n",
532 crtc->base.id, crtc->name);
533 crtc_state->active_changed = true;
536 if (!drm_atomic_crtc_needs_modeset(crtc_state))
539 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] needs all connectors, enable: %c, active: %c\n",
540 crtc->base.id, crtc->name,
541 crtc_state->enable ? 'y' : 'n',
542 crtc_state->active ? 'y' : 'n');
544 ret = drm_atomic_add_affected_connectors(state, crtc);
548 ret = drm_atomic_add_affected_planes(state, crtc);
552 if (crtc_state->enable != has_connectors) {
553 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled/connectors mismatch\n",
554 crtc->base.id, crtc->name);
560 return mode_fixup(state);
562 EXPORT_SYMBOL(drm_atomic_helper_check_modeset);
565 * drm_atomic_helper_check_planes - validate state object for planes changes
567 * @state: the driver state object
569 * Check the state object to see if the requested state is physically possible.
570 * This does all the plane update related checks using by calling into the
571 * ->atomic_check hooks provided by the driver.
573 * It also sets crtc_state->planes_changed to indicate that a crtc has
577 * Zero for success or -errno
580 drm_atomic_helper_check_planes(struct drm_device *dev,
581 struct drm_atomic_state *state)
583 struct drm_crtc *crtc;
584 struct drm_crtc_state *crtc_state;
585 struct drm_plane *plane;
586 struct drm_plane_state *plane_state;
589 for_each_plane_in_state(state, plane, plane_state, i) {
590 const struct drm_plane_helper_funcs *funcs;
592 funcs = plane->helper_private;
594 drm_atomic_helper_plane_changed(state, plane_state, plane);
596 if (!funcs || !funcs->atomic_check)
599 ret = funcs->atomic_check(plane, plane_state);
601 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic driver check failed\n",
602 plane->base.id, plane->name);
607 for_each_crtc_in_state(state, crtc, crtc_state, i) {
608 const struct drm_crtc_helper_funcs *funcs;
610 funcs = crtc->helper_private;
612 if (!funcs || !funcs->atomic_check)
615 ret = funcs->atomic_check(crtc, state->crtc_states[i]);
617 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic driver check failed\n",
618 crtc->base.id, crtc->name);
625 EXPORT_SYMBOL(drm_atomic_helper_check_planes);
628 * drm_atomic_helper_check - validate state object
630 * @state: the driver state object
632 * Check the state object to see if the requested state is physically possible.
633 * Only crtcs and planes have check callbacks, so for any additional (global)
634 * checking that a driver needs it can simply wrap that around this function.
635 * Drivers without such needs can directly use this as their ->atomic_check()
638 * This just wraps the two parts of the state checking for planes and modeset
639 * state in the default order: First it calls drm_atomic_helper_check_modeset()
640 * and then drm_atomic_helper_check_planes(). The assumption is that the
641 * ->atomic_check functions depend upon an updated adjusted_mode.clock to
642 * e.g. properly compute watermarks.
645 * Zero for success or -errno
647 int drm_atomic_helper_check(struct drm_device *dev,
648 struct drm_atomic_state *state)
652 ret = drm_atomic_helper_check_modeset(dev, state);
656 ret = drm_atomic_helper_check_planes(dev, state);
662 EXPORT_SYMBOL(drm_atomic_helper_check);
665 disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state)
667 struct drm_connector *connector;
668 struct drm_connector_state *old_conn_state;
669 struct drm_crtc *crtc;
670 struct drm_crtc_state *old_crtc_state;
673 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
674 const struct drm_encoder_helper_funcs *funcs;
675 struct drm_encoder *encoder;
677 /* Shut down everything that's in the changeset and currently
678 * still on. So need to check the old, saved state. */
679 if (!old_conn_state->crtc)
682 old_crtc_state = old_state->crtc_states[drm_crtc_index(old_conn_state->crtc)];
684 if (!old_crtc_state->active ||
685 !drm_atomic_crtc_needs_modeset(old_conn_state->crtc->state))
688 encoder = old_conn_state->best_encoder;
690 /* We shouldn't get this far if we didn't previously have
691 * an encoder.. but WARN_ON() rather than explode.
693 if (WARN_ON(!encoder))
696 funcs = encoder->helper_private;
698 DRM_DEBUG_ATOMIC("disabling [ENCODER:%d:%s]\n",
699 encoder->base.id, encoder->name);
702 * Each encoder has at most one connector (since we always steal
703 * it away), so we won't call disable hooks twice.
705 drm_bridge_disable(encoder->bridge);
707 /* Right function depends upon target state. */
708 if (connector->state->crtc && funcs->prepare)
709 funcs->prepare(encoder);
710 else if (funcs->disable)
711 funcs->disable(encoder);
713 funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
715 drm_bridge_post_disable(encoder->bridge);
718 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
719 const struct drm_crtc_helper_funcs *funcs;
721 /* Shut down everything that needs a full modeset. */
722 if (!drm_atomic_crtc_needs_modeset(crtc->state))
725 if (!old_crtc_state->active)
728 funcs = crtc->helper_private;
730 DRM_DEBUG_ATOMIC("disabling [CRTC:%d:%s]\n",
731 crtc->base.id, crtc->name);
734 /* Right function depends upon target state. */
735 if (crtc->state->enable && funcs->prepare)
736 funcs->prepare(crtc);
737 else if (funcs->disable)
738 funcs->disable(crtc);
740 funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
745 * drm_atomic_helper_update_legacy_modeset_state - update legacy modeset state
747 * @old_state: atomic state object with old state structures
749 * This function updates all the various legacy modeset state pointers in
750 * connectors, encoders and crtcs. It also updates the timestamping constants
751 * used for precise vblank timestamps by calling
752 * drm_calc_timestamping_constants().
754 * Drivers can use this for building their own atomic commit if they don't have
755 * a pure helper-based modeset implementation.
758 drm_atomic_helper_update_legacy_modeset_state(struct drm_device *dev,
759 struct drm_atomic_state *old_state)
761 struct drm_connector *connector;
762 struct drm_connector_state *old_conn_state;
763 struct drm_crtc *crtc;
764 struct drm_crtc_state *old_crtc_state;
767 /* clear out existing links and update dpms */
768 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
769 if (connector->encoder) {
770 WARN_ON(!connector->encoder->crtc);
772 connector->encoder->crtc = NULL;
773 connector->encoder = NULL;
776 crtc = connector->state->crtc;
777 if ((!crtc && old_conn_state->crtc) ||
778 (crtc && drm_atomic_crtc_needs_modeset(crtc->state))) {
779 struct drm_property *dpms_prop =
780 dev->mode_config.dpms_property;
781 int mode = DRM_MODE_DPMS_OFF;
783 if (crtc && crtc->state->active)
784 mode = DRM_MODE_DPMS_ON;
786 connector->dpms = mode;
787 drm_object_property_set_value(&connector->base,
793 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
794 if (!connector->state->crtc)
797 if (WARN_ON(!connector->state->best_encoder))
800 connector->encoder = connector->state->best_encoder;
801 connector->encoder->crtc = connector->state->crtc;
804 /* set legacy state in the crtc structure */
805 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
806 struct drm_plane *primary = crtc->primary;
808 crtc->mode = crtc->state->mode;
809 crtc->enabled = crtc->state->enable;
811 if (drm_atomic_get_existing_plane_state(old_state, primary) &&
812 primary->state->crtc == crtc) {
813 crtc->x = primary->state->src_x >> 16;
814 crtc->y = primary->state->src_y >> 16;
817 if (crtc->state->enable)
818 drm_calc_timestamping_constants(crtc,
819 &crtc->state->adjusted_mode);
822 EXPORT_SYMBOL(drm_atomic_helper_update_legacy_modeset_state);
825 crtc_set_mode(struct drm_device *dev, struct drm_atomic_state *old_state)
827 struct drm_crtc *crtc;
828 struct drm_crtc_state *old_crtc_state;
829 struct drm_connector *connector;
830 struct drm_connector_state *old_conn_state;
833 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
834 const struct drm_crtc_helper_funcs *funcs;
836 if (!crtc->state->mode_changed)
839 funcs = crtc->helper_private;
841 if (crtc->state->enable && funcs->mode_set_nofb) {
842 DRM_DEBUG_ATOMIC("modeset on [CRTC:%d:%s]\n",
843 crtc->base.id, crtc->name);
845 funcs->mode_set_nofb(crtc);
849 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
850 const struct drm_encoder_helper_funcs *funcs;
851 struct drm_crtc_state *new_crtc_state;
852 struct drm_encoder *encoder;
853 struct drm_display_mode *mode, *adjusted_mode;
855 if (!connector->state->best_encoder)
858 encoder = connector->state->best_encoder;
859 funcs = encoder->helper_private;
860 new_crtc_state = connector->state->crtc->state;
861 mode = &new_crtc_state->mode;
862 adjusted_mode = &new_crtc_state->adjusted_mode;
864 if (!new_crtc_state->mode_changed)
867 DRM_DEBUG_ATOMIC("modeset on [ENCODER:%d:%s]\n",
868 encoder->base.id, encoder->name);
871 * Each encoder has at most one connector (since we always steal
872 * it away), so we won't call mode_set hooks twice.
875 funcs->mode_set(encoder, mode, adjusted_mode);
877 drm_bridge_mode_set(encoder->bridge, mode, adjusted_mode);
882 * drm_atomic_helper_commit_modeset_disables - modeset commit to disable outputs
884 * @old_state: atomic state object with old state structures
886 * This function shuts down all the outputs that need to be shut down and
887 * prepares them (if required) with the new mode.
889 * For compatibility with legacy crtc helpers this should be called before
890 * drm_atomic_helper_commit_planes(), which is what the default commit function
891 * does. But drivers with different needs can group the modeset commits together
892 * and do the plane commits at the end. This is useful for drivers doing runtime
893 * PM since planes updates then only happen when the CRTC is actually enabled.
895 void drm_atomic_helper_commit_modeset_disables(struct drm_device *dev,
896 struct drm_atomic_state *old_state)
898 disable_outputs(dev, old_state);
900 drm_atomic_helper_update_legacy_modeset_state(dev, old_state);
902 crtc_set_mode(dev, old_state);
904 EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_disables);
907 * drm_atomic_helper_commit_modeset_enables - modeset commit to enable outputs
909 * @old_state: atomic state object with old state structures
911 * This function enables all the outputs with the new configuration which had to
912 * be turned off for the update.
914 * For compatibility with legacy crtc helpers this should be called after
915 * drm_atomic_helper_commit_planes(), which is what the default commit function
916 * does. But drivers with different needs can group the modeset commits together
917 * and do the plane commits at the end. This is useful for drivers doing runtime
918 * PM since planes updates then only happen when the CRTC is actually enabled.
920 void drm_atomic_helper_commit_modeset_enables(struct drm_device *dev,
921 struct drm_atomic_state *old_state)
923 struct drm_crtc *crtc;
924 struct drm_crtc_state *old_crtc_state;
925 struct drm_connector *connector;
926 struct drm_connector_state *old_conn_state;
929 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
930 const struct drm_crtc_helper_funcs *funcs;
932 /* Need to filter out CRTCs where only planes change. */
933 if (!drm_atomic_crtc_needs_modeset(crtc->state))
936 if (!crtc->state->active)
939 funcs = crtc->helper_private;
941 if (crtc->state->enable) {
942 DRM_DEBUG_ATOMIC("enabling [CRTC:%d:%s]\n",
943 crtc->base.id, crtc->name);
952 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
953 const struct drm_encoder_helper_funcs *funcs;
954 struct drm_encoder *encoder;
956 if (!connector->state->best_encoder)
959 if (!connector->state->crtc->state->active ||
960 !drm_atomic_crtc_needs_modeset(connector->state->crtc->state))
963 encoder = connector->state->best_encoder;
964 funcs = encoder->helper_private;
966 DRM_DEBUG_ATOMIC("enabling [ENCODER:%d:%s]\n",
967 encoder->base.id, encoder->name);
970 * Each encoder has at most one connector (since we always steal
971 * it away), so we won't call enable hooks twice.
973 drm_bridge_pre_enable(encoder->bridge);
976 funcs->enable(encoder);
978 funcs->commit(encoder);
980 drm_bridge_enable(encoder->bridge);
983 EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_enables);
985 static void wait_for_fences(struct drm_device *dev,
986 struct drm_atomic_state *state)
988 struct drm_plane *plane;
989 struct drm_plane_state *plane_state;
992 for_each_plane_in_state(state, plane, plane_state, i) {
993 if (!plane->state->fence)
996 WARN_ON(!plane->state->fb);
998 fence_wait(plane->state->fence, false);
999 fence_put(plane->state->fence);
1000 plane->state->fence = NULL;
1005 * drm_atomic_helper_framebuffer_changed - check if framebuffer has changed
1007 * @old_state: atomic state object with old state structures
1010 * Checks whether the framebuffer used for this CRTC changes as a result of
1011 * the atomic update. This is useful for drivers which cannot use
1012 * drm_atomic_helper_wait_for_vblanks() and need to reimplement its
1016 * true if the framebuffer changed.
1018 bool drm_atomic_helper_framebuffer_changed(struct drm_device *dev,
1019 struct drm_atomic_state *old_state,
1020 struct drm_crtc *crtc)
1022 struct drm_plane *plane;
1023 struct drm_plane_state *old_plane_state;
1026 for_each_plane_in_state(old_state, plane, old_plane_state, i) {
1027 if (plane->state->crtc != crtc &&
1028 old_plane_state->crtc != crtc)
1031 if (plane->state->fb != old_plane_state->fb)
1037 EXPORT_SYMBOL(drm_atomic_helper_framebuffer_changed);
1040 * drm_atomic_helper_wait_for_vblanks - wait for vblank on crtcs
1042 * @old_state: atomic state object with old state structures
1044 * Helper to, after atomic commit, wait for vblanks on all effected
1045 * crtcs (ie. before cleaning up old framebuffers using
1046 * drm_atomic_helper_cleanup_planes()). It will only wait on crtcs where the
1047 * framebuffers have actually changed to optimize for the legacy cursor and
1048 * plane update use-case.
1051 drm_atomic_helper_wait_for_vblanks(struct drm_device *dev,
1052 struct drm_atomic_state *old_state)
1054 struct drm_crtc *crtc;
1055 struct drm_crtc_state *old_crtc_state;
1058 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
1059 /* No one cares about the old state, so abuse it for tracking
1060 * and store whether we hold a vblank reference (and should do a
1061 * vblank wait) in the ->enable boolean. */
1062 old_crtc_state->enable = false;
1064 if (!crtc->state->enable)
1067 /* Legacy cursor ioctls are completely unsynced, and userspace
1068 * relies on that (by doing tons of cursor updates). */
1069 if (old_state->legacy_cursor_update)
1072 if (!drm_atomic_helper_framebuffer_changed(dev,
1076 ret = drm_crtc_vblank_get(crtc);
1080 old_crtc_state->enable = true;
1081 old_crtc_state->last_vblank_count = drm_crtc_vblank_count(crtc);
1084 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
1085 if (!old_crtc_state->enable)
1088 ret = wait_event_timeout(dev->vblank[i].queue,
1089 old_crtc_state->last_vblank_count !=
1090 drm_crtc_vblank_count(crtc),
1091 msecs_to_jiffies(50));
1093 drm_crtc_vblank_put(crtc);
1096 EXPORT_SYMBOL(drm_atomic_helper_wait_for_vblanks);
1099 * drm_atomic_helper_commit - commit validated state object
1101 * @state: the driver state object
1102 * @async: asynchronous commit
1104 * This function commits a with drm_atomic_helper_check() pre-validated state
1105 * object. This can still fail when e.g. the framebuffer reservation fails. For
1106 * now this doesn't implement asynchronous commits.
1108 * Note that right now this function does not support async commits, and hence
1109 * driver writers must implement their own version for now. Also note that the
1110 * default ordering of how the various stages are called is to match the legacy
1111 * modeset helper library closest. One peculiarity of that is that it doesn't
1112 * mesh well with runtime PM at all.
1114 * For drivers supporting runtime PM the recommended sequence is
1116 * drm_atomic_helper_commit_modeset_disables(dev, state);
1118 * drm_atomic_helper_commit_modeset_enables(dev, state);
1120 * drm_atomic_helper_commit_planes(dev, state, true);
1122 * See the kerneldoc entries for these three functions for more details.
1125 * Zero for success or -errno.
1127 int drm_atomic_helper_commit(struct drm_device *dev,
1128 struct drm_atomic_state *state,
1136 ret = drm_atomic_helper_prepare_planes(dev, state);
1141 * This is the point of no return - everything below never fails except
1142 * when the hw goes bonghits. Which means we can commit the new state on
1143 * the software side now.
1146 drm_atomic_helper_swap_state(dev, state);
1149 * Everything below can be run asynchronously without the need to grab
1150 * any modeset locks at all under one condition: It must be guaranteed
1151 * that the asynchronous work has either been cancelled (if the driver
1152 * supports it, which at least requires that the framebuffers get
1153 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
1154 * before the new state gets committed on the software side with
1155 * drm_atomic_helper_swap_state().
1157 * This scheme allows new atomic state updates to be prepared and
1158 * checked in parallel to the asynchronous completion of the previous
1159 * update. Which is important since compositors need to figure out the
1160 * composition of the next frame right after having submitted the
1164 wait_for_fences(dev, state);
1166 drm_atomic_helper_commit_modeset_disables(dev, state);
1168 drm_atomic_helper_commit_planes(dev, state, false);
1170 drm_atomic_helper_commit_modeset_enables(dev, state);
1172 drm_atomic_helper_wait_for_vblanks(dev, state);
1174 drm_atomic_helper_cleanup_planes(dev, state);
1176 drm_atomic_state_free(state);
1180 EXPORT_SYMBOL(drm_atomic_helper_commit);
1183 * DOC: implementing async commit
1185 * For now the atomic helpers don't support async commit directly. If there is
1186 * real need it could be added though, using the dma-buf fence infrastructure
1187 * for generic synchronization with outstanding rendering.
1189 * For now drivers have to implement async commit themselves, with the following
1190 * sequence being the recommended one:
1192 * 1. Run drm_atomic_helper_prepare_planes() first. This is the only function
1193 * which commit needs to call which can fail, so we want to run it first and
1196 * 2. Synchronize with any outstanding asynchronous commit worker threads which
1197 * might be affected the new state update. This can be done by either cancelling
1198 * or flushing the work items, depending upon whether the driver can deal with
1199 * cancelled updates. Note that it is important to ensure that the framebuffer
1200 * cleanup is still done when cancelling.
1202 * For sufficient parallelism it is recommended to have a work item per crtc
1203 * (for updates which don't touch global state) and a global one. Then we only
1204 * need to synchronize with the crtc work items for changed crtcs and the global
1205 * work item, which allows nice concurrent updates on disjoint sets of crtcs.
1207 * 3. The software state is updated synchronously with
1208 * drm_atomic_helper_swap_state(). Doing this under the protection of all modeset
1209 * locks means concurrent callers never see inconsistent state. And doing this
1210 * while it's guaranteed that no relevant async worker runs means that async
1211 * workers do not need grab any locks. Actually they must not grab locks, for
1212 * otherwise the work flushing will deadlock.
1214 * 4. Schedule a work item to do all subsequent steps, using the split-out
1215 * commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and
1216 * then cleaning up the framebuffers after the old framebuffer is no longer
1221 * drm_atomic_helper_prepare_planes - prepare plane resources before commit
1223 * @state: atomic state object with new state structures
1225 * This function prepares plane state, specifically framebuffers, for the new
1226 * configuration. If any failure is encountered this function will call
1227 * ->cleanup_fb on any already successfully prepared framebuffer.
1230 * 0 on success, negative error code on failure.
1232 int drm_atomic_helper_prepare_planes(struct drm_device *dev,
1233 struct drm_atomic_state *state)
1235 int nplanes = dev->mode_config.num_total_plane;
1238 for (i = 0; i < nplanes; i++) {
1239 const struct drm_plane_helper_funcs *funcs;
1240 struct drm_plane *plane = state->planes[i];
1241 struct drm_plane_state *plane_state = state->plane_states[i];
1246 funcs = plane->helper_private;
1248 if (funcs->prepare_fb) {
1249 ret = funcs->prepare_fb(plane, plane_state);
1258 for (i--; i >= 0; i--) {
1259 const struct drm_plane_helper_funcs *funcs;
1260 struct drm_plane *plane = state->planes[i];
1261 struct drm_plane_state *plane_state = state->plane_states[i];
1266 funcs = plane->helper_private;
1268 if (funcs->cleanup_fb)
1269 funcs->cleanup_fb(plane, plane_state);
1275 EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
1277 bool plane_crtc_active(struct drm_plane_state *state)
1279 return state->crtc && state->crtc->state->active;
1283 * drm_atomic_helper_commit_planes - commit plane state
1285 * @old_state: atomic state object with old state structures
1286 * @active_only: Only commit on active CRTC if set
1288 * This function commits the new plane state using the plane and atomic helper
1289 * functions for planes and crtcs. It assumes that the atomic state has already
1290 * been pushed into the relevant object state pointers, since this step can no
1293 * It still requires the global state object @old_state to know which planes and
1294 * crtcs need to be updated though.
1296 * Note that this function does all plane updates across all CRTCs in one step.
1297 * If the hardware can't support this approach look at
1298 * drm_atomic_helper_commit_planes_on_crtc() instead.
1300 * Plane parameters can be updated by applications while the associated CRTC is
1301 * disabled. The DRM/KMS core will store the parameters in the plane state,
1302 * which will be available to the driver when the CRTC is turned on. As a result
1303 * most drivers don't need to be immediately notified of plane updates for a
1306 * Unless otherwise needed, drivers are advised to set the @active_only
1307 * parameters to true in order not to receive plane update notifications related
1308 * to a disabled CRTC. This avoids the need to manually ignore plane updates in
1309 * driver code when the driver and/or hardware can't or just don't need to deal
1310 * with updates on disabled CRTCs, for example when supporting runtime PM.
1312 * The drm_atomic_helper_commit() default implementation only sets @active_only
1313 * to false to most closely match the behaviour of the legacy helpers. This should
1314 * not be copied blindly by drivers.
1316 void drm_atomic_helper_commit_planes(struct drm_device *dev,
1317 struct drm_atomic_state *old_state,
1320 struct drm_crtc *crtc;
1321 struct drm_crtc_state *old_crtc_state;
1322 struct drm_plane *plane;
1323 struct drm_plane_state *old_plane_state;
1326 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
1327 const struct drm_crtc_helper_funcs *funcs;
1329 funcs = crtc->helper_private;
1331 if (!funcs || !funcs->atomic_begin)
1334 if (active_only && !crtc->state->active)
1337 funcs->atomic_begin(crtc, old_crtc_state);
1340 for_each_plane_in_state(old_state, plane, old_plane_state, i) {
1341 const struct drm_plane_helper_funcs *funcs;
1344 funcs = plane->helper_private;
1349 disabling = drm_atomic_plane_disabling(plane, old_plane_state);
1353 * Skip planes related to inactive CRTCs. If the plane
1354 * is enabled use the state of the current CRTC. If the
1355 * plane is being disabled use the state of the old
1356 * CRTC to avoid skipping planes being disabled on an
1359 if (!disabling && !plane_crtc_active(plane->state))
1361 if (disabling && !plane_crtc_active(old_plane_state))
1366 * Special-case disabling the plane if drivers support it.
1368 if (disabling && funcs->atomic_disable)
1369 funcs->atomic_disable(plane, old_plane_state);
1370 else if (plane->state->crtc || disabling)
1371 funcs->atomic_update(plane, old_plane_state);
1374 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
1375 const struct drm_crtc_helper_funcs *funcs;
1377 funcs = crtc->helper_private;
1379 if (!funcs || !funcs->atomic_flush)
1382 if (active_only && !crtc->state->active)
1385 funcs->atomic_flush(crtc, old_crtc_state);
1388 EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
1391 * drm_atomic_helper_commit_planes_on_crtc - commit plane state for a crtc
1392 * @old_crtc_state: atomic state object with the old crtc state
1394 * This function commits the new plane state using the plane and atomic helper
1395 * functions for planes on the specific crtc. It assumes that the atomic state
1396 * has already been pushed into the relevant object state pointers, since this
1397 * step can no longer fail.
1399 * This function is useful when plane updates should be done crtc-by-crtc
1400 * instead of one global step like drm_atomic_helper_commit_planes() does.
1402 * This function can only be savely used when planes are not allowed to move
1403 * between different CRTCs because this function doesn't handle inter-CRTC
1404 * depencies. Callers need to ensure that either no such depencies exist,
1405 * resolve them through ordering of commit calls or through some other means.
1408 drm_atomic_helper_commit_planes_on_crtc(struct drm_crtc_state *old_crtc_state)
1410 const struct drm_crtc_helper_funcs *crtc_funcs;
1411 struct drm_crtc *crtc = old_crtc_state->crtc;
1412 struct drm_atomic_state *old_state = old_crtc_state->state;
1413 struct drm_plane *plane;
1414 unsigned plane_mask;
1416 plane_mask = old_crtc_state->plane_mask;
1417 plane_mask |= crtc->state->plane_mask;
1419 crtc_funcs = crtc->helper_private;
1420 if (crtc_funcs && crtc_funcs->atomic_begin)
1421 crtc_funcs->atomic_begin(crtc, old_crtc_state);
1423 drm_for_each_plane_mask(plane, crtc->dev, plane_mask) {
1424 struct drm_plane_state *old_plane_state =
1425 drm_atomic_get_existing_plane_state(old_state, plane);
1426 const struct drm_plane_helper_funcs *plane_funcs;
1428 plane_funcs = plane->helper_private;
1430 if (!old_plane_state || !plane_funcs)
1433 WARN_ON(plane->state->crtc && plane->state->crtc != crtc);
1435 if (drm_atomic_plane_disabling(plane, old_plane_state) &&
1436 plane_funcs->atomic_disable)
1437 plane_funcs->atomic_disable(plane, old_plane_state);
1438 else if (plane->state->crtc ||
1439 drm_atomic_plane_disabling(plane, old_plane_state))
1440 plane_funcs->atomic_update(plane, old_plane_state);
1443 if (crtc_funcs && crtc_funcs->atomic_flush)
1444 crtc_funcs->atomic_flush(crtc, old_crtc_state);
1446 EXPORT_SYMBOL(drm_atomic_helper_commit_planes_on_crtc);
1449 * drm_atomic_helper_disable_planes_on_crtc - helper to disable CRTC's planes
1451 * @atomic: if set, synchronize with CRTC's atomic_begin/flush hooks
1453 * Disables all planes associated with the given CRTC. This can be
1454 * used for instance in the CRTC helper disable callback to disable
1455 * all planes before shutting down the display pipeline.
1457 * If the atomic-parameter is set the function calls the CRTC's
1458 * atomic_begin hook before and atomic_flush hook after disabling the
1461 * It is a bug to call this function without having implemented the
1462 * ->atomic_disable() plane hook.
1464 void drm_atomic_helper_disable_planes_on_crtc(struct drm_crtc *crtc,
1467 const struct drm_crtc_helper_funcs *crtc_funcs =
1468 crtc->helper_private;
1469 struct drm_plane *plane;
1471 if (atomic && crtc_funcs && crtc_funcs->atomic_begin)
1472 crtc_funcs->atomic_begin(crtc, NULL);
1474 drm_for_each_plane(plane, crtc->dev) {
1475 const struct drm_plane_helper_funcs *plane_funcs =
1476 plane->helper_private;
1478 if (plane->state->crtc != crtc || !plane_funcs)
1481 WARN_ON(!plane_funcs->atomic_disable);
1482 if (plane_funcs->atomic_disable)
1483 plane_funcs->atomic_disable(plane, NULL);
1486 if (atomic && crtc_funcs && crtc_funcs->atomic_flush)
1487 crtc_funcs->atomic_flush(crtc, NULL);
1489 EXPORT_SYMBOL(drm_atomic_helper_disable_planes_on_crtc);
1492 * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
1494 * @old_state: atomic state object with old state structures
1496 * This function cleans up plane state, specifically framebuffers, from the old
1497 * configuration. Hence the old configuration must be perserved in @old_state to
1498 * be able to call this function.
1500 * This function must also be called on the new state when the atomic update
1501 * fails at any point after calling drm_atomic_helper_prepare_planes().
1503 void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
1504 struct drm_atomic_state *old_state)
1506 struct drm_plane *plane;
1507 struct drm_plane_state *plane_state;
1510 for_each_plane_in_state(old_state, plane, plane_state, i) {
1511 const struct drm_plane_helper_funcs *funcs;
1513 funcs = plane->helper_private;
1515 if (funcs->cleanup_fb)
1516 funcs->cleanup_fb(plane, plane_state);
1519 EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
1522 * drm_atomic_helper_swap_state - store atomic state into current sw state
1524 * @state: atomic state
1526 * This function stores the atomic state into the current state pointers in all
1527 * driver objects. It should be called after all failing steps have been done
1528 * and succeeded, but before the actual hardware state is committed.
1530 * For cleanup and error recovery the current state for all changed objects will
1531 * be swaped into @state.
1533 * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
1535 * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
1537 * 2. Do any other steps that might fail.
1539 * 3. Put the staged state into the current state pointers with this function.
1541 * 4. Actually commit the hardware state.
1543 * 5. Call drm_atomic_helper_cleanup_planes() with @state, which since step 3
1544 * contains the old state. Also do any other cleanup required with that state.
1546 void drm_atomic_helper_swap_state(struct drm_device *dev,
1547 struct drm_atomic_state *state)
1551 for (i = 0; i < state->num_connector; i++) {
1552 struct drm_connector *connector = state->connectors[i];
1557 connector->state->state = state;
1558 swap(state->connector_states[i], connector->state);
1559 connector->state->state = NULL;
1562 for (i = 0; i < dev->mode_config.num_crtc; i++) {
1563 struct drm_crtc *crtc = state->crtcs[i];
1568 crtc->state->state = state;
1569 swap(state->crtc_states[i], crtc->state);
1570 crtc->state->state = NULL;
1573 for (i = 0; i < dev->mode_config.num_total_plane; i++) {
1574 struct drm_plane *plane = state->planes[i];
1579 plane->state->state = state;
1580 swap(state->plane_states[i], plane->state);
1581 plane->state->state = NULL;
1584 EXPORT_SYMBOL(drm_atomic_helper_swap_state);
1587 * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
1588 * @plane: plane object to update
1589 * @crtc: owning CRTC of owning plane
1590 * @fb: framebuffer to flip onto plane
1591 * @crtc_x: x offset of primary plane on crtc
1592 * @crtc_y: y offset of primary plane on crtc
1593 * @crtc_w: width of primary plane rectangle on crtc
1594 * @crtc_h: height of primary plane rectangle on crtc
1595 * @src_x: x offset of @fb for panning
1596 * @src_y: y offset of @fb for panning
1597 * @src_w: width of source rectangle in @fb
1598 * @src_h: height of source rectangle in @fb
1600 * Provides a default plane update handler using the atomic driver interface.
1603 * Zero on success, error code on failure
1605 int drm_atomic_helper_update_plane(struct drm_plane *plane,
1606 struct drm_crtc *crtc,
1607 struct drm_framebuffer *fb,
1608 int crtc_x, int crtc_y,
1609 unsigned int crtc_w, unsigned int crtc_h,
1610 uint32_t src_x, uint32_t src_y,
1611 uint32_t src_w, uint32_t src_h)
1613 struct drm_atomic_state *state;
1614 struct drm_plane_state *plane_state;
1617 state = drm_atomic_state_alloc(plane->dev);
1621 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1623 plane_state = drm_atomic_get_plane_state(state, plane);
1624 if (IS_ERR(plane_state)) {
1625 ret = PTR_ERR(plane_state);
1629 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
1632 drm_atomic_set_fb_for_plane(plane_state, fb);
1633 plane_state->crtc_x = crtc_x;
1634 plane_state->crtc_y = crtc_y;
1635 plane_state->crtc_w = crtc_w;
1636 plane_state->crtc_h = crtc_h;
1637 plane_state->src_x = src_x;
1638 plane_state->src_y = src_y;
1639 plane_state->src_w = src_w;
1640 plane_state->src_h = src_h;
1642 if (plane == crtc->cursor)
1643 state->legacy_cursor_update = true;
1645 ret = drm_atomic_commit(state);
1649 /* Driver takes ownership of state on successful commit. */
1652 if (ret == -EDEADLK)
1655 drm_atomic_state_free(state);
1659 drm_atomic_state_clear(state);
1660 drm_atomic_legacy_backoff(state);
1663 * Someone might have exchanged the framebuffer while we dropped locks
1664 * in the backoff code. We need to fix up the fb refcount tracking the
1667 plane->old_fb = plane->fb;
1671 EXPORT_SYMBOL(drm_atomic_helper_update_plane);
1674 * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
1675 * @plane: plane to disable
1677 * Provides a default plane disable handler using the atomic driver interface.
1680 * Zero on success, error code on failure
1682 int drm_atomic_helper_disable_plane(struct drm_plane *plane)
1684 struct drm_atomic_state *state;
1685 struct drm_plane_state *plane_state;
1689 * FIXME: Without plane->crtc set we can't get at the implicit legacy
1690 * acquire context. The real fix will be to wire the acquire ctx through
1691 * everywhere we need it, but meanwhile prevent chaos by just skipping
1692 * this noop. The critical case is the cursor ioctls which a) only grab
1693 * crtc/cursor-plane locks (so we need the crtc to get at the right
1694 * acquire context) and b) can try to disable the plane multiple times.
1699 state = drm_atomic_state_alloc(plane->dev);
1703 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(plane->crtc);
1705 plane_state = drm_atomic_get_plane_state(state, plane);
1706 if (IS_ERR(plane_state)) {
1707 ret = PTR_ERR(plane_state);
1711 if (plane_state->crtc && (plane == plane->crtc->cursor))
1712 plane_state->state->legacy_cursor_update = true;
1714 ret = __drm_atomic_helper_disable_plane(plane, plane_state);
1718 ret = drm_atomic_commit(state);
1722 /* Driver takes ownership of state on successful commit. */
1725 if (ret == -EDEADLK)
1728 drm_atomic_state_free(state);
1732 drm_atomic_state_clear(state);
1733 drm_atomic_legacy_backoff(state);
1736 * Someone might have exchanged the framebuffer while we dropped locks
1737 * in the backoff code. We need to fix up the fb refcount tracking the
1740 plane->old_fb = plane->fb;
1744 EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
1746 /* just used from fb-helper and atomic-helper: */
1747 int __drm_atomic_helper_disable_plane(struct drm_plane *plane,
1748 struct drm_plane_state *plane_state)
1752 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
1756 drm_atomic_set_fb_for_plane(plane_state, NULL);
1757 plane_state->crtc_x = 0;
1758 plane_state->crtc_y = 0;
1759 plane_state->crtc_w = 0;
1760 plane_state->crtc_h = 0;
1761 plane_state->src_x = 0;
1762 plane_state->src_y = 0;
1763 plane_state->src_w = 0;
1764 plane_state->src_h = 0;
1769 static int update_output_state(struct drm_atomic_state *state,
1770 struct drm_mode_set *set)
1772 struct drm_device *dev = set->crtc->dev;
1773 struct drm_crtc *crtc;
1774 struct drm_crtc_state *crtc_state;
1775 struct drm_connector *connector;
1776 struct drm_connector_state *conn_state;
1779 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
1780 state->acquire_ctx);
1784 /* First disable all connectors on the target crtc. */
1785 ret = drm_atomic_add_affected_connectors(state, set->crtc);
1789 for_each_connector_in_state(state, connector, conn_state, i) {
1790 if (conn_state->crtc == set->crtc) {
1791 ret = drm_atomic_set_crtc_for_connector(conn_state,
1798 /* Then set all connectors from set->connectors on the target crtc */
1799 for (i = 0; i < set->num_connectors; i++) {
1800 conn_state = drm_atomic_get_connector_state(state,
1801 set->connectors[i]);
1802 if (IS_ERR(conn_state))
1803 return PTR_ERR(conn_state);
1805 ret = drm_atomic_set_crtc_for_connector(conn_state,
1811 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1812 /* Don't update ->enable for the CRTC in the set_config request,
1813 * since a mismatch would indicate a bug in the upper layers.
1814 * The actual modeset code later on will catch any
1815 * inconsistencies here. */
1816 if (crtc == set->crtc)
1819 if (!crtc_state->connector_mask) {
1820 ret = drm_atomic_set_mode_prop_for_crtc(crtc_state,
1825 crtc_state->active = false;
1833 * drm_atomic_helper_set_config - set a new config from userspace
1834 * @set: mode set configuration
1836 * Provides a default crtc set_config handler using the atomic driver interface.
1839 * Returns 0 on success, negative errno numbers on failure.
1841 int drm_atomic_helper_set_config(struct drm_mode_set *set)
1843 struct drm_atomic_state *state;
1844 struct drm_crtc *crtc = set->crtc;
1847 state = drm_atomic_state_alloc(crtc->dev);
1851 state->legacy_set_config = true;
1852 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1854 ret = __drm_atomic_helper_set_config(set, state);
1858 ret = drm_atomic_commit(state);
1862 /* Driver takes ownership of state on successful commit. */
1865 if (ret == -EDEADLK)
1868 drm_atomic_state_free(state);
1872 drm_atomic_state_clear(state);
1873 drm_atomic_legacy_backoff(state);
1876 * Someone might have exchanged the framebuffer while we dropped locks
1877 * in the backoff code. We need to fix up the fb refcount tracking the
1880 crtc->primary->old_fb = crtc->primary->fb;
1884 EXPORT_SYMBOL(drm_atomic_helper_set_config);
1886 /* just used from fb-helper and atomic-helper: */
1887 int __drm_atomic_helper_set_config(struct drm_mode_set *set,
1888 struct drm_atomic_state *state)
1890 struct drm_crtc_state *crtc_state;
1891 struct drm_plane_state *primary_state;
1892 struct drm_crtc *crtc = set->crtc;
1893 int hdisplay, vdisplay;
1896 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1897 if (IS_ERR(crtc_state))
1898 return PTR_ERR(crtc_state);
1900 primary_state = drm_atomic_get_plane_state(state, crtc->primary);
1901 if (IS_ERR(primary_state))
1902 return PTR_ERR(primary_state);
1906 WARN_ON(set->num_connectors);
1908 ret = drm_atomic_set_mode_for_crtc(crtc_state, NULL);
1912 crtc_state->active = false;
1914 ret = drm_atomic_set_crtc_for_plane(primary_state, NULL);
1918 drm_atomic_set_fb_for_plane(primary_state, NULL);
1924 WARN_ON(!set->num_connectors);
1926 ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
1930 crtc_state->active = true;
1932 ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
1936 drm_crtc_get_hv_timing(set->mode, &hdisplay, &vdisplay);
1938 drm_atomic_set_fb_for_plane(primary_state, set->fb);
1939 primary_state->crtc_x = 0;
1940 primary_state->crtc_y = 0;
1941 primary_state->crtc_w = hdisplay;
1942 primary_state->crtc_h = vdisplay;
1943 primary_state->src_x = set->x << 16;
1944 primary_state->src_y = set->y << 16;
1945 if (primary_state->rotation & (BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270))) {
1946 primary_state->src_w = vdisplay << 16;
1947 primary_state->src_h = hdisplay << 16;
1949 primary_state->src_w = hdisplay << 16;
1950 primary_state->src_h = vdisplay << 16;
1954 ret = update_output_state(state, set);
1962 * drm_atomic_helper_disable_all - disable all currently active outputs
1964 * @ctx: lock acquisition context
1966 * Loops through all connectors, finding those that aren't turned off and then
1967 * turns them off by setting their DPMS mode to OFF and deactivating the CRTC
1968 * that they are connected to.
1970 * This is used for example in suspend/resume to disable all currently active
1971 * functions when suspending.
1973 * Note that if callers haven't already acquired all modeset locks this might
1974 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
1977 * 0 on success or a negative error code on failure.
1980 * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
1982 int drm_atomic_helper_disable_all(struct drm_device *dev,
1983 struct drm_modeset_acquire_ctx *ctx)
1985 struct drm_atomic_state *state;
1986 struct drm_connector *conn;
1989 state = drm_atomic_state_alloc(dev);
1993 state->acquire_ctx = ctx;
1995 drm_for_each_connector(conn, dev) {
1996 struct drm_crtc *crtc = conn->state->crtc;
1997 struct drm_crtc_state *crtc_state;
1999 if (!crtc || conn->dpms != DRM_MODE_DPMS_ON)
2002 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2003 if (IS_ERR(crtc_state)) {
2004 err = PTR_ERR(crtc_state);
2008 crtc_state->active = false;
2011 err = drm_atomic_commit(state);
2015 drm_atomic_state_free(state);
2019 EXPORT_SYMBOL(drm_atomic_helper_disable_all);
2022 * drm_atomic_helper_suspend - subsystem-level suspend helper
2025 * Duplicates the current atomic state, disables all active outputs and then
2026 * returns a pointer to the original atomic state to the caller. Drivers can
2027 * pass this pointer to the drm_atomic_helper_resume() helper upon resume to
2028 * restore the output configuration that was active at the time the system
2031 * Note that it is potentially unsafe to use this. The atomic state object
2032 * returned by this function is assumed to be persistent. Drivers must ensure
2033 * that this holds true. Before calling this function, drivers must make sure
2034 * to suspend fbdev emulation so that nothing can be using the device.
2037 * A pointer to a copy of the state before suspend on success or an ERR_PTR()-
2038 * encoded error code on failure. Drivers should store the returned atomic
2039 * state object and pass it to the drm_atomic_helper_resume() helper upon
2043 * drm_atomic_helper_duplicate_state(), drm_atomic_helper_disable_all(),
2044 * drm_atomic_helper_resume()
2046 struct drm_atomic_state *drm_atomic_helper_suspend(struct drm_device *dev)
2048 struct drm_modeset_acquire_ctx ctx;
2049 struct drm_atomic_state *state;
2052 drm_modeset_acquire_init(&ctx, 0);
2055 err = drm_modeset_lock_all_ctx(dev, &ctx);
2057 state = ERR_PTR(err);
2061 state = drm_atomic_helper_duplicate_state(dev, &ctx);
2065 err = drm_atomic_helper_disable_all(dev, &ctx);
2067 drm_atomic_state_free(state);
2068 state = ERR_PTR(err);
2073 if (PTR_ERR(state) == -EDEADLK) {
2074 drm_modeset_backoff(&ctx);
2078 drm_modeset_drop_locks(&ctx);
2079 drm_modeset_acquire_fini(&ctx);
2082 EXPORT_SYMBOL(drm_atomic_helper_suspend);
2085 * drm_atomic_helper_resume - subsystem-level resume helper
2087 * @state: atomic state to resume to
2089 * Calls drm_mode_config_reset() to synchronize hardware and software states,
2090 * grabs all modeset locks and commits the atomic state object. This can be
2091 * used in conjunction with the drm_atomic_helper_suspend() helper to
2092 * implement suspend/resume for drivers that support atomic mode-setting.
2095 * 0 on success or a negative error code on failure.
2098 * drm_atomic_helper_suspend()
2100 int drm_atomic_helper_resume(struct drm_device *dev,
2101 struct drm_atomic_state *state)
2103 struct drm_mode_config *config = &dev->mode_config;
2106 drm_mode_config_reset(dev);
2107 drm_modeset_lock_all(dev);
2108 state->acquire_ctx = config->acquire_ctx;
2109 err = drm_atomic_commit(state);
2110 drm_modeset_unlock_all(dev);
2114 EXPORT_SYMBOL(drm_atomic_helper_resume);
2117 * drm_atomic_helper_crtc_set_property - helper for crtc properties
2119 * @property: DRM property
2120 * @val: value of property
2122 * Provides a default crtc set_property handler using the atomic driver
2126 * Zero on success, error code on failure
2129 drm_atomic_helper_crtc_set_property(struct drm_crtc *crtc,
2130 struct drm_property *property,
2133 struct drm_atomic_state *state;
2134 struct drm_crtc_state *crtc_state;
2137 state = drm_atomic_state_alloc(crtc->dev);
2141 /* ->set_property is always called with all locks held. */
2142 state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
2144 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2145 if (IS_ERR(crtc_state)) {
2146 ret = PTR_ERR(crtc_state);
2150 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
2155 ret = drm_atomic_commit(state);
2159 /* Driver takes ownership of state on successful commit. */
2162 if (ret == -EDEADLK)
2165 drm_atomic_state_free(state);
2169 drm_atomic_state_clear(state);
2170 drm_atomic_legacy_backoff(state);
2174 EXPORT_SYMBOL(drm_atomic_helper_crtc_set_property);
2177 * drm_atomic_helper_plane_set_property - helper for plane properties
2179 * @property: DRM property
2180 * @val: value of property
2182 * Provides a default plane set_property handler using the atomic driver
2186 * Zero on success, error code on failure
2189 drm_atomic_helper_plane_set_property(struct drm_plane *plane,
2190 struct drm_property *property,
2193 struct drm_atomic_state *state;
2194 struct drm_plane_state *plane_state;
2197 state = drm_atomic_state_alloc(plane->dev);
2201 /* ->set_property is always called with all locks held. */
2202 state->acquire_ctx = plane->dev->mode_config.acquire_ctx;
2204 plane_state = drm_atomic_get_plane_state(state, plane);
2205 if (IS_ERR(plane_state)) {
2206 ret = PTR_ERR(plane_state);
2210 ret = drm_atomic_plane_set_property(plane, plane_state,
2215 ret = drm_atomic_commit(state);
2219 /* Driver takes ownership of state on successful commit. */
2222 if (ret == -EDEADLK)
2225 drm_atomic_state_free(state);
2229 drm_atomic_state_clear(state);
2230 drm_atomic_legacy_backoff(state);
2234 EXPORT_SYMBOL(drm_atomic_helper_plane_set_property);
2237 * drm_atomic_helper_connector_set_property - helper for connector properties
2238 * @connector: DRM connector
2239 * @property: DRM property
2240 * @val: value of property
2242 * Provides a default connector set_property handler using the atomic driver
2246 * Zero on success, error code on failure
2249 drm_atomic_helper_connector_set_property(struct drm_connector *connector,
2250 struct drm_property *property,
2253 struct drm_atomic_state *state;
2254 struct drm_connector_state *connector_state;
2257 state = drm_atomic_state_alloc(connector->dev);
2261 /* ->set_property is always called with all locks held. */
2262 state->acquire_ctx = connector->dev->mode_config.acquire_ctx;
2264 connector_state = drm_atomic_get_connector_state(state, connector);
2265 if (IS_ERR(connector_state)) {
2266 ret = PTR_ERR(connector_state);
2270 ret = drm_atomic_connector_set_property(connector, connector_state,
2275 ret = drm_atomic_commit(state);
2279 /* Driver takes ownership of state on successful commit. */
2282 if (ret == -EDEADLK)
2285 drm_atomic_state_free(state);
2289 drm_atomic_state_clear(state);
2290 drm_atomic_legacy_backoff(state);
2294 EXPORT_SYMBOL(drm_atomic_helper_connector_set_property);
2297 * drm_atomic_helper_page_flip - execute a legacy page flip
2299 * @fb: DRM framebuffer
2300 * @event: optional DRM event to signal upon completion
2301 * @flags: flip flags for non-vblank sync'ed updates
2303 * Provides a default page flip implementation using the atomic driver interface.
2305 * Note that for now so called async page flips (i.e. updates which are not
2306 * synchronized to vblank) are not supported, since the atomic interfaces have
2307 * no provisions for this yet.
2310 * Returns 0 on success, negative errno numbers on failure.
2312 int drm_atomic_helper_page_flip(struct drm_crtc *crtc,
2313 struct drm_framebuffer *fb,
2314 struct drm_pending_vblank_event *event,
2317 struct drm_plane *plane = crtc->primary;
2318 struct drm_atomic_state *state;
2319 struct drm_plane_state *plane_state;
2320 struct drm_crtc_state *crtc_state;
2323 if (flags & DRM_MODE_PAGE_FLIP_ASYNC)
2326 state = drm_atomic_state_alloc(plane->dev);
2330 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2332 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2333 if (IS_ERR(crtc_state)) {
2334 ret = PTR_ERR(crtc_state);
2337 crtc_state->event = event;
2339 plane_state = drm_atomic_get_plane_state(state, plane);
2340 if (IS_ERR(plane_state)) {
2341 ret = PTR_ERR(plane_state);
2345 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
2348 drm_atomic_set_fb_for_plane(plane_state, fb);
2350 /* Make sure we don't accidentally do a full modeset. */
2351 state->allow_modeset = false;
2352 if (!crtc_state->active) {
2353 DRM_DEBUG_ATOMIC("[CRTC:%d] disabled, rejecting legacy flip\n",
2359 ret = drm_atomic_async_commit(state);
2363 /* Driver takes ownership of state on successful async commit. */
2366 if (ret == -EDEADLK)
2369 drm_atomic_state_free(state);
2373 drm_atomic_state_clear(state);
2374 drm_atomic_legacy_backoff(state);
2377 * Someone might have exchanged the framebuffer while we dropped locks
2378 * in the backoff code. We need to fix up the fb refcount tracking the
2381 plane->old_fb = plane->fb;
2385 EXPORT_SYMBOL(drm_atomic_helper_page_flip);
2388 * drm_atomic_helper_connector_dpms() - connector dpms helper implementation
2389 * @connector: affected connector
2392 * This is the main helper function provided by the atomic helper framework for
2393 * implementing the legacy DPMS connector interface. It computes the new desired
2394 * ->active state for the corresponding CRTC (if the connector is enabled) and
2398 * Returns 0 on success, negative errno numbers on failure.
2400 int drm_atomic_helper_connector_dpms(struct drm_connector *connector,
2403 struct drm_mode_config *config = &connector->dev->mode_config;
2404 struct drm_atomic_state *state;
2405 struct drm_crtc_state *crtc_state;
2406 struct drm_crtc *crtc;
2407 struct drm_connector *tmp_connector;
2409 bool active = false;
2410 int old_mode = connector->dpms;
2412 if (mode != DRM_MODE_DPMS_ON)
2413 mode = DRM_MODE_DPMS_OFF;
2415 connector->dpms = mode;
2416 crtc = connector->state->crtc;
2421 state = drm_atomic_state_alloc(connector->dev);
2425 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2427 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2428 if (IS_ERR(crtc_state)) {
2429 ret = PTR_ERR(crtc_state);
2433 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
2435 drm_for_each_connector(tmp_connector, connector->dev) {
2436 if (tmp_connector->state->crtc != crtc)
2439 if (tmp_connector->dpms == DRM_MODE_DPMS_ON) {
2444 crtc_state->active = active;
2446 ret = drm_atomic_commit(state);
2450 /* Driver takes ownership of state on successful commit. */
2453 if (ret == -EDEADLK)
2456 connector->dpms = old_mode;
2457 drm_atomic_state_free(state);
2461 drm_atomic_state_clear(state);
2462 drm_atomic_legacy_backoff(state);
2466 EXPORT_SYMBOL(drm_atomic_helper_connector_dpms);
2469 * DOC: atomic state reset and initialization
2471 * Both the drm core and the atomic helpers assume that there is always the full
2472 * and correct atomic software state for all connectors, CRTCs and planes
2473 * available. Which is a bit a problem on driver load and also after system
2474 * suspend. One way to solve this is to have a hardware state read-out
2475 * infrastructure which reconstructs the full software state (e.g. the i915
2478 * The simpler solution is to just reset the software state to everything off,
2479 * which is easiest to do by calling drm_mode_config_reset(). To facilitate this
2480 * the atomic helpers provide default reset implementations for all hooks.
2482 * On the upside the precise state tracking of atomic simplifies system suspend
2483 * and resume a lot. For drivers using drm_mode_config_reset() a complete recipe
2484 * is implemented in drm_atomic_helper_suspend() and drm_atomic_helper_resume().
2485 * For other drivers the building blocks are split out, see the documentation
2486 * for these functions.
2490 * drm_atomic_helper_crtc_reset - default ->reset hook for CRTCs
2493 * Resets the atomic state for @crtc by freeing the state pointer (which might
2494 * be NULL, e.g. at driver load time) and allocating a new empty state object.
2496 void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc)
2499 drm_property_unreference_blob(crtc->state->mode_blob);
2500 drm_property_unreference_blob(crtc->state->degamma_lut);
2501 drm_property_unreference_blob(crtc->state->ctm);
2502 drm_property_unreference_blob(crtc->state->gamma_lut);
2505 crtc->state = kzalloc(sizeof(*crtc->state), GFP_KERNEL);
2508 crtc->state->crtc = crtc;
2510 EXPORT_SYMBOL(drm_atomic_helper_crtc_reset);
2513 * __drm_atomic_helper_crtc_duplicate_state - copy atomic CRTC state
2514 * @crtc: CRTC object
2515 * @state: atomic CRTC state
2517 * Copies atomic state from a CRTC's current state and resets inferred values.
2518 * This is useful for drivers that subclass the CRTC state.
2520 void __drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc,
2521 struct drm_crtc_state *state)
2523 memcpy(state, crtc->state, sizeof(*state));
2525 if (state->mode_blob)
2526 drm_property_reference_blob(state->mode_blob);
2527 if (state->degamma_lut)
2528 drm_property_reference_blob(state->degamma_lut);
2530 drm_property_reference_blob(state->ctm);
2531 if (state->gamma_lut)
2532 drm_property_reference_blob(state->gamma_lut);
2533 state->mode_changed = false;
2534 state->active_changed = false;
2535 state->planes_changed = false;
2536 state->connectors_changed = false;
2537 state->color_mgmt_changed = false;
2538 state->event = NULL;
2540 EXPORT_SYMBOL(__drm_atomic_helper_crtc_duplicate_state);
2543 * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
2546 * Default CRTC state duplicate hook for drivers which don't have their own
2547 * subclassed CRTC state structure.
2549 struct drm_crtc_state *
2550 drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc)
2552 struct drm_crtc_state *state;
2554 if (WARN_ON(!crtc->state))
2557 state = kmalloc(sizeof(*state), GFP_KERNEL);
2559 __drm_atomic_helper_crtc_duplicate_state(crtc, state);
2563 EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state);
2566 * __drm_atomic_helper_crtc_destroy_state - release CRTC state
2567 * @crtc: CRTC object
2568 * @state: CRTC state object to release
2570 * Releases all resources stored in the CRTC state without actually freeing
2571 * the memory of the CRTC state. This is useful for drivers that subclass the
2574 void __drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
2575 struct drm_crtc_state *state)
2577 drm_property_unreference_blob(state->mode_blob);
2578 drm_property_unreference_blob(state->degamma_lut);
2579 drm_property_unreference_blob(state->ctm);
2580 drm_property_unreference_blob(state->gamma_lut);
2582 EXPORT_SYMBOL(__drm_atomic_helper_crtc_destroy_state);
2585 * drm_atomic_helper_crtc_destroy_state - default state destroy hook
2587 * @state: CRTC state object to release
2589 * Default CRTC state destroy hook for drivers which don't have their own
2590 * subclassed CRTC state structure.
2592 void drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
2593 struct drm_crtc_state *state)
2595 __drm_atomic_helper_crtc_destroy_state(crtc, state);
2598 EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state);
2601 * drm_atomic_helper_plane_reset - default ->reset hook for planes
2604 * Resets the atomic state for @plane by freeing the state pointer (which might
2605 * be NULL, e.g. at driver load time) and allocating a new empty state object.
2607 void drm_atomic_helper_plane_reset(struct drm_plane *plane)
2609 if (plane->state && plane->state->fb)
2610 drm_framebuffer_unreference(plane->state->fb);
2612 kfree(plane->state);
2613 plane->state = kzalloc(sizeof(*plane->state), GFP_KERNEL);
2616 plane->state->plane = plane;
2617 plane->state->rotation = BIT(DRM_ROTATE_0);
2620 EXPORT_SYMBOL(drm_atomic_helper_plane_reset);
2623 * __drm_atomic_helper_plane_duplicate_state - copy atomic plane state
2624 * @plane: plane object
2625 * @state: atomic plane state
2627 * Copies atomic state from a plane's current state. This is useful for
2628 * drivers that subclass the plane state.
2630 void __drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane,
2631 struct drm_plane_state *state)
2633 memcpy(state, plane->state, sizeof(*state));
2636 drm_framebuffer_reference(state->fb);
2638 EXPORT_SYMBOL(__drm_atomic_helper_plane_duplicate_state);
2641 * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
2644 * Default plane state duplicate hook for drivers which don't have their own
2645 * subclassed plane state structure.
2647 struct drm_plane_state *
2648 drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane)
2650 struct drm_plane_state *state;
2652 if (WARN_ON(!plane->state))
2655 state = kmalloc(sizeof(*state), GFP_KERNEL);
2657 __drm_atomic_helper_plane_duplicate_state(plane, state);
2661 EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state);
2664 * __drm_atomic_helper_plane_destroy_state - release plane state
2665 * @plane: plane object
2666 * @state: plane state object to release
2668 * Releases all resources stored in the plane state without actually freeing
2669 * the memory of the plane state. This is useful for drivers that subclass the
2672 void __drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
2673 struct drm_plane_state *state)
2676 drm_framebuffer_unreference(state->fb);
2678 EXPORT_SYMBOL(__drm_atomic_helper_plane_destroy_state);
2681 * drm_atomic_helper_plane_destroy_state - default state destroy hook
2683 * @state: plane state object to release
2685 * Default plane state destroy hook for drivers which don't have their own
2686 * subclassed plane state structure.
2688 void drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
2689 struct drm_plane_state *state)
2691 __drm_atomic_helper_plane_destroy_state(plane, state);
2694 EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state);
2697 * __drm_atomic_helper_connector_reset - reset state on connector
2698 * @connector: drm connector
2699 * @conn_state: connector state to assign
2701 * Initializes the newly allocated @conn_state and assigns it to
2702 * #connector ->state, usually required when initializing the drivers
2703 * or when called from the ->reset hook.
2705 * This is useful for drivers that subclass the connector state.
2708 __drm_atomic_helper_connector_reset(struct drm_connector *connector,
2709 struct drm_connector_state *conn_state)
2712 conn_state->connector = connector;
2714 connector->state = conn_state;
2716 EXPORT_SYMBOL(__drm_atomic_helper_connector_reset);
2719 * drm_atomic_helper_connector_reset - default ->reset hook for connectors
2720 * @connector: drm connector
2722 * Resets the atomic state for @connector by freeing the state pointer (which
2723 * might be NULL, e.g. at driver load time) and allocating a new empty state
2726 void drm_atomic_helper_connector_reset(struct drm_connector *connector)
2728 struct drm_connector_state *conn_state =
2729 kzalloc(sizeof(*conn_state), GFP_KERNEL);
2731 kfree(connector->state);
2732 __drm_atomic_helper_connector_reset(connector, conn_state);
2734 EXPORT_SYMBOL(drm_atomic_helper_connector_reset);
2737 * __drm_atomic_helper_connector_duplicate_state - copy atomic connector state
2738 * @connector: connector object
2739 * @state: atomic connector state
2741 * Copies atomic state from a connector's current state. This is useful for
2742 * drivers that subclass the connector state.
2745 __drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector,
2746 struct drm_connector_state *state)
2748 memcpy(state, connector->state, sizeof(*state));
2750 EXPORT_SYMBOL(__drm_atomic_helper_connector_duplicate_state);
2753 * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
2754 * @connector: drm connector
2756 * Default connector state duplicate hook for drivers which don't have their own
2757 * subclassed connector state structure.
2759 struct drm_connector_state *
2760 drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
2762 struct drm_connector_state *state;
2764 if (WARN_ON(!connector->state))
2767 state = kmalloc(sizeof(*state), GFP_KERNEL);
2769 __drm_atomic_helper_connector_duplicate_state(connector, state);
2773 EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
2776 * drm_atomic_helper_duplicate_state - duplicate an atomic state object
2778 * @ctx: lock acquisition context
2780 * Makes a copy of the current atomic state by looping over all objects and
2781 * duplicating their respective states. This is used for example by suspend/
2782 * resume support code to save the state prior to suspend such that it can
2783 * be restored upon resume.
2785 * Note that this treats atomic state as persistent between save and restore.
2786 * Drivers must make sure that this is possible and won't result in confusion
2787 * or erroneous behaviour.
2789 * Note that if callers haven't already acquired all modeset locks this might
2790 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
2793 * A pointer to the copy of the atomic state object on success or an
2794 * ERR_PTR()-encoded error code on failure.
2797 * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
2799 struct drm_atomic_state *
2800 drm_atomic_helper_duplicate_state(struct drm_device *dev,
2801 struct drm_modeset_acquire_ctx *ctx)
2803 struct drm_atomic_state *state;
2804 struct drm_connector *conn;
2805 struct drm_plane *plane;
2806 struct drm_crtc *crtc;
2809 state = drm_atomic_state_alloc(dev);
2811 return ERR_PTR(-ENOMEM);
2813 state->acquire_ctx = ctx;
2815 drm_for_each_crtc(crtc, dev) {
2816 struct drm_crtc_state *crtc_state;
2818 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2819 if (IS_ERR(crtc_state)) {
2820 err = PTR_ERR(crtc_state);
2825 drm_for_each_plane(plane, dev) {
2826 struct drm_plane_state *plane_state;
2828 plane_state = drm_atomic_get_plane_state(state, plane);
2829 if (IS_ERR(plane_state)) {
2830 err = PTR_ERR(plane_state);
2835 drm_for_each_connector(conn, dev) {
2836 struct drm_connector_state *conn_state;
2838 conn_state = drm_atomic_get_connector_state(state, conn);
2839 if (IS_ERR(conn_state)) {
2840 err = PTR_ERR(conn_state);
2845 /* clear the acquire context so that it isn't accidentally reused */
2846 state->acquire_ctx = NULL;
2850 drm_atomic_state_free(state);
2851 state = ERR_PTR(err);
2856 EXPORT_SYMBOL(drm_atomic_helper_duplicate_state);
2859 * __drm_atomic_helper_connector_destroy_state - release connector state
2860 * @connector: connector object
2861 * @state: connector state object to release
2863 * Releases all resources stored in the connector state without actually
2864 * freeing the memory of the connector state. This is useful for drivers that
2865 * subclass the connector state.
2868 __drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
2869 struct drm_connector_state *state)
2872 * This is currently a placeholder so that drivers that subclass the
2873 * state will automatically do the right thing if code is ever added
2877 EXPORT_SYMBOL(__drm_atomic_helper_connector_destroy_state);
2880 * drm_atomic_helper_connector_destroy_state - default state destroy hook
2881 * @connector: drm connector
2882 * @state: connector state object to release
2884 * Default connector state destroy hook for drivers which don't have their own
2885 * subclassed connector state structure.
2887 void drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
2888 struct drm_connector_state *state)
2890 __drm_atomic_helper_connector_destroy_state(connector, state);
2893 EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state);
2896 * drm_atomic_helper_legacy_gamma_set - set the legacy gamma correction table
2897 * @crtc: CRTC object
2898 * @red: red correction table
2899 * @green: green correction table
2900 * @blue: green correction table
2902 * @size: size of the tables
2904 * Implements support for legacy gamma correction table for drivers
2905 * that support color management through the DEGAMMA_LUT/GAMMA_LUT
2908 void drm_atomic_helper_legacy_gamma_set(struct drm_crtc *crtc,
2909 u16 *red, u16 *green, u16 *blue,
2910 uint32_t start, uint32_t size)
2912 struct drm_device *dev = crtc->dev;
2913 struct drm_mode_config *config = &dev->mode_config;
2914 struct drm_atomic_state *state;
2915 struct drm_crtc_state *crtc_state;
2916 struct drm_property_blob *blob = NULL;
2917 struct drm_color_lut *blob_data;
2920 state = drm_atomic_state_alloc(crtc->dev);
2924 blob = drm_property_create_blob(dev,
2925 sizeof(struct drm_color_lut) * size,
2928 ret = PTR_ERR(blob);
2933 /* Prepare GAMMA_LUT with the legacy values. */
2934 blob_data = (struct drm_color_lut *) blob->data;
2935 for (i = 0; i < size; i++) {
2936 blob_data[i].red = red[i];
2937 blob_data[i].green = green[i];
2938 blob_data[i].blue = blue[i];
2941 state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
2943 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2944 if (IS_ERR(crtc_state)) {
2945 ret = PTR_ERR(crtc_state);
2949 /* Reset DEGAMMA_LUT and CTM properties. */
2950 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
2951 config->degamma_lut_property, 0);
2955 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
2956 config->ctm_property, 0);
2960 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
2961 config->gamma_lut_property, blob->base.id);
2965 ret = drm_atomic_commit(state);
2969 /* Driver takes ownership of state on successful commit. */
2971 drm_property_unreference_blob(blob);
2975 if (ret == -EDEADLK)
2978 drm_atomic_state_free(state);
2979 drm_property_unreference_blob(blob);
2983 drm_atomic_state_clear(state);
2984 drm_atomic_legacy_backoff(state);
2988 EXPORT_SYMBOL(drm_atomic_helper_legacy_gamma_set);