2 * Copyright (C) 2014 Red Hat
3 * Copyright (C) 2014 Intel Corp.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
24 * Rob Clark <robdclark@gmail.com>
25 * Daniel Vetter <daniel.vetter@ffwll.ch>
29 #include <drm/drm_atomic.h>
30 #include <drm/drm_plane_helper.h>
31 #include <drm/drm_crtc_helper.h>
32 #include <drm/drm_atomic_helper.h>
33 #include <linux/dma-fence.h>
35 #include "drm_crtc_internal.h"
40 * This helper library provides implementations of check and commit functions on
41 * top of the CRTC modeset helper callbacks and the plane helper callbacks. It
42 * also provides convenience implementations for the atomic state handling
43 * callbacks for drivers which don't need to subclass the drm core structures to
44 * add their own additional internal state.
46 * This library also provides default implementations for the check callback in
47 * drm_atomic_helper_check() and for the commit callback with
48 * drm_atomic_helper_commit(). But the individual stages and callbacks are
49 * exposed to allow drivers to mix and match and e.g. use the plane helpers only
50 * together with a driver private modeset implementation.
52 * This library also provides implementations for all the legacy driver
53 * interfaces on top of the atomic interface. See drm_atomic_helper_set_config(),
54 * drm_atomic_helper_disable_plane(), drm_atomic_helper_disable_plane() and the
55 * various functions to implement set_property callbacks. New drivers must not
56 * implement these functions themselves but must use the provided helpers.
58 * The atomic helper uses the same function table structures as all other
59 * modesetting helpers. See the documentation for &struct drm_crtc_helper_funcs,
60 * struct &drm_encoder_helper_funcs and &struct drm_connector_helper_funcs. It
61 * also shares the &struct drm_plane_helper_funcs function table with the plane
65 drm_atomic_helper_plane_changed(struct drm_atomic_state *state,
66 struct drm_plane_state *old_plane_state,
67 struct drm_plane_state *plane_state,
68 struct drm_plane *plane)
70 struct drm_crtc_state *crtc_state;
72 if (old_plane_state->crtc) {
73 crtc_state = drm_atomic_get_existing_crtc_state(state,
74 old_plane_state->crtc);
76 if (WARN_ON(!crtc_state))
79 crtc_state->planes_changed = true;
82 if (plane_state->crtc) {
83 crtc_state = drm_atomic_get_existing_crtc_state(state,
86 if (WARN_ON(!crtc_state))
89 crtc_state->planes_changed = true;
93 static int handle_conflicting_encoders(struct drm_atomic_state *state,
94 bool disable_conflicting_encoders)
96 struct drm_connector_state *new_conn_state;
97 struct drm_connector *connector;
98 struct drm_connector_list_iter conn_iter;
99 struct drm_encoder *encoder;
100 unsigned encoder_mask = 0;
104 * First loop, find all newly assigned encoders from the connectors
105 * part of the state. If the same encoder is assigned to multiple
106 * connectors bail out.
108 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
109 const struct drm_connector_helper_funcs *funcs = connector->helper_private;
110 struct drm_encoder *new_encoder;
112 if (!new_conn_state->crtc)
115 if (funcs->atomic_best_encoder)
116 new_encoder = funcs->atomic_best_encoder(connector, new_conn_state);
117 else if (funcs->best_encoder)
118 new_encoder = funcs->best_encoder(connector);
120 new_encoder = drm_atomic_helper_best_encoder(connector);
123 if (encoder_mask & (1 << drm_encoder_index(new_encoder))) {
124 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] on [CONNECTOR:%d:%s] already assigned\n",
125 new_encoder->base.id, new_encoder->name,
126 connector->base.id, connector->name);
131 encoder_mask |= 1 << drm_encoder_index(new_encoder);
139 * Second loop, iterate over all connectors not part of the state.
141 * If a conflicting encoder is found and disable_conflicting_encoders
142 * is not set, an error is returned. Userspace can provide a solution
143 * through the atomic ioctl.
145 * If the flag is set conflicting connectors are removed from the crtc
146 * and the crtc is disabled if no encoder is left. This preserves
147 * compatibility with the legacy set_config behavior.
149 drm_connector_list_iter_begin(state->dev, &conn_iter);
150 drm_for_each_connector_iter(connector, &conn_iter) {
151 struct drm_crtc_state *crtc_state;
153 if (drm_atomic_get_existing_connector_state(state, connector))
156 encoder = connector->state->best_encoder;
157 if (!encoder || !(encoder_mask & (1 << drm_encoder_index(encoder))))
160 if (!disable_conflicting_encoders) {
161 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s] by [CONNECTOR:%d:%s]\n",
162 encoder->base.id, encoder->name,
163 connector->state->crtc->base.id,
164 connector->state->crtc->name,
165 connector->base.id, connector->name);
170 new_conn_state = drm_atomic_get_connector_state(state, connector);
171 if (IS_ERR(new_conn_state)) {
172 ret = PTR_ERR(new_conn_state);
176 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s], disabling [CONNECTOR:%d:%s]\n",
177 encoder->base.id, encoder->name,
178 new_conn_state->crtc->base.id, new_conn_state->crtc->name,
179 connector->base.id, connector->name);
181 crtc_state = drm_atomic_get_existing_crtc_state(state, new_conn_state->crtc);
183 ret = drm_atomic_set_crtc_for_connector(new_conn_state, NULL);
187 if (!crtc_state->connector_mask) {
188 ret = drm_atomic_set_mode_prop_for_crtc(crtc_state,
193 crtc_state->active = false;
197 drm_connector_list_iter_end(&conn_iter);
203 set_best_encoder(struct drm_atomic_state *state,
204 struct drm_connector_state *conn_state,
205 struct drm_encoder *encoder)
207 struct drm_crtc_state *crtc_state;
208 struct drm_crtc *crtc;
210 if (conn_state->best_encoder) {
211 /* Unset the encoder_mask in the old crtc state. */
212 crtc = conn_state->connector->state->crtc;
214 /* A NULL crtc is an error here because we should have
215 * duplicated a NULL best_encoder when crtc was NULL.
216 * As an exception restoring duplicated atomic state
217 * during resume is allowed, so don't warn when
218 * best_encoder is equal to encoder we intend to set.
220 WARN_ON(!crtc && encoder != conn_state->best_encoder);
222 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
224 crtc_state->encoder_mask &=
225 ~(1 << drm_encoder_index(conn_state->best_encoder));
230 crtc = conn_state->crtc;
233 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
235 crtc_state->encoder_mask |=
236 1 << drm_encoder_index(encoder);
240 conn_state->best_encoder = encoder;
244 steal_encoder(struct drm_atomic_state *state,
245 struct drm_encoder *encoder)
247 struct drm_crtc_state *crtc_state;
248 struct drm_connector *connector;
249 struct drm_connector_state *old_connector_state, *new_connector_state;
252 for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
253 struct drm_crtc *encoder_crtc;
255 if (new_connector_state->best_encoder != encoder)
258 encoder_crtc = old_connector_state->crtc;
260 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s], stealing it\n",
261 encoder->base.id, encoder->name,
262 encoder_crtc->base.id, encoder_crtc->name);
264 set_best_encoder(state, new_connector_state, NULL);
266 crtc_state = drm_atomic_get_existing_crtc_state(state, encoder_crtc);
267 crtc_state->connectors_changed = true;
274 update_connector_routing(struct drm_atomic_state *state,
275 struct drm_connector *connector,
276 struct drm_connector_state *old_connector_state,
277 struct drm_connector_state *new_connector_state)
279 const struct drm_connector_helper_funcs *funcs;
280 struct drm_encoder *new_encoder;
281 struct drm_crtc_state *crtc_state;
283 DRM_DEBUG_ATOMIC("Updating routing for [CONNECTOR:%d:%s]\n",
287 if (old_connector_state->crtc != new_connector_state->crtc) {
288 if (old_connector_state->crtc) {
289 crtc_state = drm_atomic_get_existing_crtc_state(state, old_connector_state->crtc);
290 crtc_state->connectors_changed = true;
293 if (new_connector_state->crtc) {
294 crtc_state = drm_atomic_get_existing_crtc_state(state, new_connector_state->crtc);
295 crtc_state->connectors_changed = true;
299 if (!new_connector_state->crtc) {
300 DRM_DEBUG_ATOMIC("Disabling [CONNECTOR:%d:%s]\n",
304 set_best_encoder(state, new_connector_state, NULL);
309 funcs = connector->helper_private;
311 if (funcs->atomic_best_encoder)
312 new_encoder = funcs->atomic_best_encoder(connector,
313 new_connector_state);
314 else if (funcs->best_encoder)
315 new_encoder = funcs->best_encoder(connector);
317 new_encoder = drm_atomic_helper_best_encoder(connector);
320 DRM_DEBUG_ATOMIC("No suitable encoder found for [CONNECTOR:%d:%s]\n",
326 if (!drm_encoder_crtc_ok(new_encoder, new_connector_state->crtc)) {
327 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] incompatible with [CRTC:%d:%s]\n",
328 new_encoder->base.id,
330 new_connector_state->crtc->base.id,
331 new_connector_state->crtc->name);
335 if (new_encoder == new_connector_state->best_encoder) {
336 set_best_encoder(state, new_connector_state, new_encoder);
338 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] keeps [ENCODER:%d:%s], now on [CRTC:%d:%s]\n",
341 new_encoder->base.id,
343 new_connector_state->crtc->base.id,
344 new_connector_state->crtc->name);
349 steal_encoder(state, new_encoder);
351 set_best_encoder(state, new_connector_state, new_encoder);
353 crtc_state = drm_atomic_get_existing_crtc_state(state, new_connector_state->crtc);
354 crtc_state->connectors_changed = true;
356 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] using [ENCODER:%d:%s] on [CRTC:%d:%s]\n",
359 new_encoder->base.id,
361 new_connector_state->crtc->base.id,
362 new_connector_state->crtc->name);
368 mode_fixup(struct drm_atomic_state *state)
370 struct drm_crtc *crtc;
371 struct drm_crtc_state *new_crtc_state;
372 struct drm_connector *connector;
373 struct drm_connector_state *new_conn_state;
377 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
378 if (!new_crtc_state->mode_changed &&
379 !new_crtc_state->connectors_changed)
382 drm_mode_copy(&new_crtc_state->adjusted_mode, &new_crtc_state->mode);
385 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
386 const struct drm_encoder_helper_funcs *funcs;
387 struct drm_encoder *encoder;
389 WARN_ON(!!new_conn_state->best_encoder != !!new_conn_state->crtc);
391 if (!new_conn_state->crtc || !new_conn_state->best_encoder)
395 drm_atomic_get_existing_crtc_state(state, new_conn_state->crtc);
398 * Each encoder has at most one connector (since we always steal
399 * it away), so we won't call ->mode_fixup twice.
401 encoder = new_conn_state->best_encoder;
402 funcs = encoder->helper_private;
404 ret = drm_bridge_mode_fixup(encoder->bridge, &new_crtc_state->mode,
405 &new_crtc_state->adjusted_mode);
407 DRM_DEBUG_ATOMIC("Bridge fixup failed\n");
411 if (funcs && funcs->atomic_check) {
412 ret = funcs->atomic_check(encoder, new_crtc_state,
415 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] check failed\n",
416 encoder->base.id, encoder->name);
419 } else if (funcs && funcs->mode_fixup) {
420 ret = funcs->mode_fixup(encoder, &new_crtc_state->mode,
421 &new_crtc_state->adjusted_mode);
423 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] fixup failed\n",
424 encoder->base.id, encoder->name);
430 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
431 const struct drm_crtc_helper_funcs *funcs;
433 if (!new_crtc_state->enable)
436 if (!new_crtc_state->mode_changed &&
437 !new_crtc_state->connectors_changed)
440 funcs = crtc->helper_private;
441 if (!funcs->mode_fixup)
444 ret = funcs->mode_fixup(crtc, &new_crtc_state->mode,
445 &new_crtc_state->adjusted_mode);
447 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] fixup failed\n",
448 crtc->base.id, crtc->name);
457 * drm_atomic_helper_check_modeset - validate state object for modeset changes
459 * @state: the driver state object
461 * Check the state object to see if the requested state is physically possible.
462 * This does all the crtc and connector related computations for an atomic
463 * update and adds any additional connectors needed for full modesets and calls
464 * down into &drm_crtc_helper_funcs.mode_fixup and
465 * &drm_encoder_helper_funcs.mode_fixup or
466 * &drm_encoder_helper_funcs.atomic_check functions of the driver backend.
468 * &drm_crtc_state.mode_changed is set when the input mode is changed.
469 * &drm_crtc_state.connectors_changed is set when a connector is added or
470 * removed from the crtc. &drm_crtc_state.active_changed is set when
471 * &drm_crtc_state.active changes, which is used for DPMS.
472 * See also: drm_atomic_crtc_needs_modeset()
476 * Drivers which set &drm_crtc_state.mode_changed (e.g. in their
477 * &drm_plane_helper_funcs.atomic_check hooks if a plane update can't be done
478 * without a full modeset) _must_ call this function afterwards after that
479 * change. It is permitted to call this function multiple times for the same
480 * update, e.g. when the &drm_crtc_helper_funcs.atomic_check functions depend
481 * upon the adjusted dotclock for fifo space allocation and watermark
485 * Zero for success or -errno
488 drm_atomic_helper_check_modeset(struct drm_device *dev,
489 struct drm_atomic_state *state)
491 struct drm_crtc *crtc;
492 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
493 struct drm_connector *connector;
494 struct drm_connector_state *old_connector_state, *new_connector_state;
497 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
498 if (!drm_mode_equal(&old_crtc_state->mode, &new_crtc_state->mode)) {
499 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] mode changed\n",
500 crtc->base.id, crtc->name);
501 new_crtc_state->mode_changed = true;
504 if (old_crtc_state->enable != new_crtc_state->enable) {
505 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enable changed\n",
506 crtc->base.id, crtc->name);
509 * For clarity this assignment is done here, but
510 * enable == 0 is only true when there are no
511 * connectors and a NULL mode.
513 * The other way around is true as well. enable != 0
514 * iff connectors are attached and a mode is set.
516 new_crtc_state->mode_changed = true;
517 new_crtc_state->connectors_changed = true;
521 ret = handle_conflicting_encoders(state, state->legacy_set_config);
525 for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
527 * This only sets crtc->connectors_changed for routing changes,
528 * drivers must set crtc->connectors_changed themselves when
529 * connector properties need to be updated.
531 ret = update_connector_routing(state, connector,
533 new_connector_state);
536 if (old_connector_state->crtc) {
537 new_crtc_state = drm_atomic_get_existing_crtc_state(state,
538 old_connector_state->crtc);
539 if (old_connector_state->link_status !=
540 new_connector_state->link_status)
541 new_crtc_state->connectors_changed = true;
546 * After all the routing has been prepared we need to add in any
547 * connector which is itself unchanged, but who's crtc changes it's
548 * configuration. This must be done before calling mode_fixup in case a
549 * crtc only changed its mode but has the same set of connectors.
551 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
552 bool has_connectors =
553 !!new_crtc_state->connector_mask;
556 * We must set ->active_changed after walking connectors for
557 * otherwise an update that only changes active would result in
558 * a full modeset because update_connector_routing force that.
560 if (old_crtc_state->active != new_crtc_state->active) {
561 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active changed\n",
562 crtc->base.id, crtc->name);
563 new_crtc_state->active_changed = true;
566 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
569 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] needs all connectors, enable: %c, active: %c\n",
570 crtc->base.id, crtc->name,
571 new_crtc_state->enable ? 'y' : 'n',
572 new_crtc_state->active ? 'y' : 'n');
574 ret = drm_atomic_add_affected_connectors(state, crtc);
578 ret = drm_atomic_add_affected_planes(state, crtc);
582 if (new_crtc_state->enable != has_connectors) {
583 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled/connectors mismatch\n",
584 crtc->base.id, crtc->name);
590 return mode_fixup(state);
592 EXPORT_SYMBOL(drm_atomic_helper_check_modeset);
595 * drm_atomic_helper_check_planes - validate state object for planes changes
597 * @state: the driver state object
599 * Check the state object to see if the requested state is physically possible.
600 * This does all the plane update related checks using by calling into the
601 * &drm_crtc_helper_funcs.atomic_check and &drm_plane_helper_funcs.atomic_check
602 * hooks provided by the driver.
604 * It also sets &drm_crtc_state.planes_changed to indicate that a crtc has
608 * Zero for success or -errno
611 drm_atomic_helper_check_planes(struct drm_device *dev,
612 struct drm_atomic_state *state)
614 struct drm_crtc *crtc;
615 struct drm_crtc_state *new_crtc_state;
616 struct drm_plane *plane;
617 struct drm_plane_state *new_plane_state, *old_plane_state;
620 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
621 const struct drm_plane_helper_funcs *funcs;
623 funcs = plane->helper_private;
625 drm_atomic_helper_plane_changed(state, old_plane_state, new_plane_state, plane);
627 if (!funcs || !funcs->atomic_check)
630 ret = funcs->atomic_check(plane, new_plane_state);
632 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic driver check failed\n",
633 plane->base.id, plane->name);
638 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
639 const struct drm_crtc_helper_funcs *funcs;
641 funcs = crtc->helper_private;
643 if (!funcs || !funcs->atomic_check)
646 ret = funcs->atomic_check(crtc, new_crtc_state);
648 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic driver check failed\n",
649 crtc->base.id, crtc->name);
656 EXPORT_SYMBOL(drm_atomic_helper_check_planes);
659 * drm_atomic_helper_check - validate state object
661 * @state: the driver state object
663 * Check the state object to see if the requested state is physically possible.
664 * Only crtcs and planes have check callbacks, so for any additional (global)
665 * checking that a driver needs it can simply wrap that around this function.
666 * Drivers without such needs can directly use this as their
667 * &drm_mode_config_funcs.atomic_check callback.
669 * This just wraps the two parts of the state checking for planes and modeset
670 * state in the default order: First it calls drm_atomic_helper_check_modeset()
671 * and then drm_atomic_helper_check_planes(). The assumption is that the
672 * @drm_plane_helper_funcs.atomic_check and @drm_crtc_helper_funcs.atomic_check
673 * functions depend upon an updated adjusted_mode.clock to e.g. properly compute
677 * Zero for success or -errno
679 int drm_atomic_helper_check(struct drm_device *dev,
680 struct drm_atomic_state *state)
684 ret = drm_atomic_helper_check_modeset(dev, state);
688 ret = drm_atomic_helper_check_planes(dev, state);
694 EXPORT_SYMBOL(drm_atomic_helper_check);
697 disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state)
699 struct drm_connector *connector;
700 struct drm_connector_state *old_conn_state, *new_conn_state;
701 struct drm_crtc *crtc;
702 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
705 for_each_oldnew_connector_in_state(old_state, connector, old_conn_state, new_conn_state, i) {
706 const struct drm_encoder_helper_funcs *funcs;
707 struct drm_encoder *encoder;
709 /* Shut down everything that's in the changeset and currently
710 * still on. So need to check the old, saved state. */
711 if (!old_conn_state->crtc)
714 old_crtc_state = drm_atomic_get_existing_crtc_state(old_state,
715 old_conn_state->crtc);
717 if (!old_crtc_state->active ||
718 !drm_atomic_crtc_needs_modeset(old_conn_state->crtc->state))
721 encoder = old_conn_state->best_encoder;
723 /* We shouldn't get this far if we didn't previously have
724 * an encoder.. but WARN_ON() rather than explode.
726 if (WARN_ON(!encoder))
729 funcs = encoder->helper_private;
731 DRM_DEBUG_ATOMIC("disabling [ENCODER:%d:%s]\n",
732 encoder->base.id, encoder->name);
735 * Each encoder has at most one connector (since we always steal
736 * it away), so we won't call disable hooks twice.
738 drm_bridge_disable(encoder->bridge);
740 /* Right function depends upon target state. */
742 if (new_conn_state->crtc && funcs->prepare)
743 funcs->prepare(encoder);
744 else if (funcs->disable)
745 funcs->disable(encoder);
746 else if (funcs->dpms)
747 funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
750 drm_bridge_post_disable(encoder->bridge);
753 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
754 const struct drm_crtc_helper_funcs *funcs;
756 /* Shut down everything that needs a full modeset. */
757 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
760 if (!old_crtc_state->active)
763 funcs = crtc->helper_private;
765 DRM_DEBUG_ATOMIC("disabling [CRTC:%d:%s]\n",
766 crtc->base.id, crtc->name);
769 /* Right function depends upon target state. */
770 if (new_crtc_state->enable && funcs->prepare)
771 funcs->prepare(crtc);
772 else if (funcs->atomic_disable)
773 funcs->atomic_disable(crtc, old_crtc_state);
774 else if (funcs->disable)
775 funcs->disable(crtc);
777 funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
782 * drm_atomic_helper_update_legacy_modeset_state - update legacy modeset state
784 * @old_state: atomic state object with old state structures
786 * This function updates all the various legacy modeset state pointers in
787 * connectors, encoders and crtcs. It also updates the timestamping constants
788 * used for precise vblank timestamps by calling
789 * drm_calc_timestamping_constants().
791 * Drivers can use this for building their own atomic commit if they don't have
792 * a pure helper-based modeset implementation.
795 drm_atomic_helper_update_legacy_modeset_state(struct drm_device *dev,
796 struct drm_atomic_state *old_state)
798 struct drm_connector *connector;
799 struct drm_connector_state *old_conn_state, *new_conn_state;
800 struct drm_crtc *crtc;
801 struct drm_crtc_state *new_crtc_state;
804 /* clear out existing links and update dpms */
805 for_each_oldnew_connector_in_state(old_state, connector, old_conn_state, new_conn_state, i) {
806 if (connector->encoder) {
807 WARN_ON(!connector->encoder->crtc);
809 connector->encoder->crtc = NULL;
810 connector->encoder = NULL;
813 crtc = new_conn_state->crtc;
814 if ((!crtc && old_conn_state->crtc) ||
815 (crtc && drm_atomic_crtc_needs_modeset(crtc->state))) {
816 struct drm_property *dpms_prop =
817 dev->mode_config.dpms_property;
818 int mode = DRM_MODE_DPMS_OFF;
820 if (crtc && crtc->state->active)
821 mode = DRM_MODE_DPMS_ON;
823 connector->dpms = mode;
824 drm_object_property_set_value(&connector->base,
830 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
831 if (!new_conn_state->crtc)
834 if (WARN_ON(!new_conn_state->best_encoder))
837 connector->encoder = new_conn_state->best_encoder;
838 connector->encoder->crtc = new_conn_state->crtc;
841 /* set legacy state in the crtc structure */
842 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
843 struct drm_plane *primary = crtc->primary;
845 crtc->mode = new_crtc_state->mode;
846 crtc->enabled = new_crtc_state->enable;
848 if (drm_atomic_get_existing_plane_state(old_state, primary) &&
849 primary->state->crtc == crtc) {
850 crtc->x = primary->state->src_x >> 16;
851 crtc->y = primary->state->src_y >> 16;
854 if (new_crtc_state->enable)
855 drm_calc_timestamping_constants(crtc,
856 &new_crtc_state->adjusted_mode);
859 EXPORT_SYMBOL(drm_atomic_helper_update_legacy_modeset_state);
862 crtc_set_mode(struct drm_device *dev, struct drm_atomic_state *old_state)
864 struct drm_crtc *crtc;
865 struct drm_crtc_state *new_crtc_state;
866 struct drm_connector *connector;
867 struct drm_connector_state *new_conn_state;
870 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
871 const struct drm_crtc_helper_funcs *funcs;
873 if (!new_crtc_state->mode_changed)
876 funcs = crtc->helper_private;
878 if (new_crtc_state->enable && funcs->mode_set_nofb) {
879 DRM_DEBUG_ATOMIC("modeset on [CRTC:%d:%s]\n",
880 crtc->base.id, crtc->name);
882 funcs->mode_set_nofb(crtc);
886 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
887 const struct drm_encoder_helper_funcs *funcs;
888 struct drm_encoder *encoder;
889 struct drm_display_mode *mode, *adjusted_mode;
891 if (!new_conn_state->best_encoder)
894 encoder = new_conn_state->best_encoder;
895 funcs = encoder->helper_private;
896 new_crtc_state = new_conn_state->crtc->state;
897 mode = &new_crtc_state->mode;
898 adjusted_mode = &new_crtc_state->adjusted_mode;
900 if (!new_crtc_state->mode_changed)
903 DRM_DEBUG_ATOMIC("modeset on [ENCODER:%d:%s]\n",
904 encoder->base.id, encoder->name);
907 * Each encoder has at most one connector (since we always steal
908 * it away), so we won't call mode_set hooks twice.
910 if (funcs && funcs->atomic_mode_set) {
911 funcs->atomic_mode_set(encoder, new_crtc_state,
913 } else if (funcs && funcs->mode_set) {
914 funcs->mode_set(encoder, mode, adjusted_mode);
917 drm_bridge_mode_set(encoder->bridge, mode, adjusted_mode);
922 * drm_atomic_helper_commit_modeset_disables - modeset commit to disable outputs
924 * @old_state: atomic state object with old state structures
926 * This function shuts down all the outputs that need to be shut down and
927 * prepares them (if required) with the new mode.
929 * For compatibility with legacy crtc helpers this should be called before
930 * drm_atomic_helper_commit_planes(), which is what the default commit function
931 * does. But drivers with different needs can group the modeset commits together
932 * and do the plane commits at the end. This is useful for drivers doing runtime
933 * PM since planes updates then only happen when the CRTC is actually enabled.
935 void drm_atomic_helper_commit_modeset_disables(struct drm_device *dev,
936 struct drm_atomic_state *old_state)
938 disable_outputs(dev, old_state);
940 drm_atomic_helper_update_legacy_modeset_state(dev, old_state);
942 crtc_set_mode(dev, old_state);
944 EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_disables);
947 * drm_atomic_helper_commit_modeset_enables - modeset commit to enable outputs
949 * @old_state: atomic state object with old state structures
951 * This function enables all the outputs with the new configuration which had to
952 * be turned off for the update.
954 * For compatibility with legacy crtc helpers this should be called after
955 * drm_atomic_helper_commit_planes(), which is what the default commit function
956 * does. But drivers with different needs can group the modeset commits together
957 * and do the plane commits at the end. This is useful for drivers doing runtime
958 * PM since planes updates then only happen when the CRTC is actually enabled.
960 void drm_atomic_helper_commit_modeset_enables(struct drm_device *dev,
961 struct drm_atomic_state *old_state)
963 struct drm_crtc *crtc;
964 struct drm_crtc_state *new_crtc_state;
965 struct drm_connector *connector;
966 struct drm_connector_state *new_conn_state;
969 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
970 const struct drm_crtc_helper_funcs *funcs;
972 /* Need to filter out CRTCs where only planes change. */
973 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
976 if (!new_crtc_state->active)
979 funcs = crtc->helper_private;
981 if (new_crtc_state->enable) {
982 DRM_DEBUG_ATOMIC("enabling [CRTC:%d:%s]\n",
983 crtc->base.id, crtc->name);
992 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
993 const struct drm_encoder_helper_funcs *funcs;
994 struct drm_encoder *encoder;
996 if (!new_conn_state->best_encoder)
999 if (!new_conn_state->crtc->state->active ||
1000 !drm_atomic_crtc_needs_modeset(new_conn_state->crtc->state))
1003 encoder = new_conn_state->best_encoder;
1004 funcs = encoder->helper_private;
1006 DRM_DEBUG_ATOMIC("enabling [ENCODER:%d:%s]\n",
1007 encoder->base.id, encoder->name);
1010 * Each encoder has at most one connector (since we always steal
1011 * it away), so we won't call enable hooks twice.
1013 drm_bridge_pre_enable(encoder->bridge);
1017 funcs->enable(encoder);
1018 else if (funcs->commit)
1019 funcs->commit(encoder);
1022 drm_bridge_enable(encoder->bridge);
1025 EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_enables);
1028 * drm_atomic_helper_wait_for_fences - wait for fences stashed in plane state
1030 * @state: atomic state object with old state structures
1031 * @pre_swap: If true, do an interruptible wait, and @state is the new state.
1032 * Otherwise @state is the old state.
1034 * For implicit sync, driver should fish the exclusive fence out from the
1035 * incoming fb's and stash it in the drm_plane_state. This is called after
1036 * drm_atomic_helper_swap_state() so it uses the current plane state (and
1037 * just uses the atomic state to find the changed planes)
1039 * Note that @pre_swap is needed since the point where we block for fences moves
1040 * around depending upon whether an atomic commit is blocking or
1041 * non-blocking. For async commit all waiting needs to happen after
1042 * drm_atomic_helper_swap_state() is called, but for synchronous commits we want
1043 * to wait **before** we do anything that can't be easily rolled back. That is
1044 * before we call drm_atomic_helper_swap_state().
1046 * Returns zero if success or < 0 if dma_fence_wait() fails.
1048 int drm_atomic_helper_wait_for_fences(struct drm_device *dev,
1049 struct drm_atomic_state *state,
1052 struct drm_plane *plane;
1053 struct drm_plane_state *new_plane_state;
1056 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
1057 if (!new_plane_state->fence)
1060 WARN_ON(!new_plane_state->fb);
1063 * If waiting for fences pre-swap (ie: nonblock), userspace can
1064 * still interrupt the operation. Instead of blocking until the
1065 * timer expires, make the wait interruptible.
1067 ret = dma_fence_wait(new_plane_state->fence, pre_swap);
1071 dma_fence_put(new_plane_state->fence);
1072 new_plane_state->fence = NULL;
1077 EXPORT_SYMBOL(drm_atomic_helper_wait_for_fences);
1080 * drm_atomic_helper_wait_for_vblanks - wait for vblank on crtcs
1082 * @old_state: atomic state object with old state structures
1084 * Helper to, after atomic commit, wait for vblanks on all effected
1085 * crtcs (ie. before cleaning up old framebuffers using
1086 * drm_atomic_helper_cleanup_planes()). It will only wait on crtcs where the
1087 * framebuffers have actually changed to optimize for the legacy cursor and
1088 * plane update use-case.
1091 drm_atomic_helper_wait_for_vblanks(struct drm_device *dev,
1092 struct drm_atomic_state *old_state)
1094 struct drm_crtc *crtc;
1095 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
1097 unsigned crtc_mask = 0;
1100 * Legacy cursor ioctls are completely unsynced, and userspace
1101 * relies on that (by doing tons of cursor updates).
1103 if (old_state->legacy_cursor_update)
1106 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
1107 if (!new_crtc_state->active || !new_crtc_state->planes_changed)
1110 ret = drm_crtc_vblank_get(crtc);
1114 crtc_mask |= drm_crtc_mask(crtc);
1115 old_state->crtcs[i].last_vblank_count = drm_crtc_vblank_count(crtc);
1118 for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
1119 if (!(crtc_mask & drm_crtc_mask(crtc)))
1122 ret = wait_event_timeout(dev->vblank[i].queue,
1123 old_state->crtcs[i].last_vblank_count !=
1124 drm_crtc_vblank_count(crtc),
1125 msecs_to_jiffies(50));
1127 WARN(!ret, "[CRTC:%d:%s] vblank wait timed out\n",
1128 crtc->base.id, crtc->name);
1130 drm_crtc_vblank_put(crtc);
1133 EXPORT_SYMBOL(drm_atomic_helper_wait_for_vblanks);
1136 * drm_atomic_helper_commit_tail - commit atomic update to hardware
1137 * @old_state: atomic state object with old state structures
1139 * This is the default implementation for the
1140 * &drm_mode_config_helper_funcs.atomic_commit_tail hook.
1142 * Note that the default ordering of how the various stages are called is to
1143 * match the legacy modeset helper library closest. One peculiarity of that is
1144 * that it doesn't mesh well with runtime PM at all.
1146 * For drivers supporting runtime PM the recommended sequence is instead ::
1148 * drm_atomic_helper_commit_modeset_disables(dev, old_state);
1150 * drm_atomic_helper_commit_modeset_enables(dev, old_state);
1152 * drm_atomic_helper_commit_planes(dev, old_state,
1153 * DRM_PLANE_COMMIT_ACTIVE_ONLY);
1155 * for committing the atomic update to hardware. See the kerneldoc entries for
1156 * these three functions for more details.
1158 void drm_atomic_helper_commit_tail(struct drm_atomic_state *old_state)
1160 struct drm_device *dev = old_state->dev;
1162 drm_atomic_helper_commit_modeset_disables(dev, old_state);
1164 drm_atomic_helper_commit_planes(dev, old_state, 0);
1166 drm_atomic_helper_commit_modeset_enables(dev, old_state);
1168 drm_atomic_helper_commit_hw_done(old_state);
1170 drm_atomic_helper_wait_for_vblanks(dev, old_state);
1172 drm_atomic_helper_cleanup_planes(dev, old_state);
1174 EXPORT_SYMBOL(drm_atomic_helper_commit_tail);
1176 static void commit_tail(struct drm_atomic_state *old_state)
1178 struct drm_device *dev = old_state->dev;
1179 const struct drm_mode_config_helper_funcs *funcs;
1181 funcs = dev->mode_config.helper_private;
1183 drm_atomic_helper_wait_for_fences(dev, old_state, false);
1185 drm_atomic_helper_wait_for_dependencies(old_state);
1187 if (funcs && funcs->atomic_commit_tail)
1188 funcs->atomic_commit_tail(old_state);
1190 drm_atomic_helper_commit_tail(old_state);
1192 drm_atomic_helper_commit_cleanup_done(old_state);
1194 drm_atomic_state_put(old_state);
1197 static void commit_work(struct work_struct *work)
1199 struct drm_atomic_state *state = container_of(work,
1200 struct drm_atomic_state,
1206 * drm_atomic_helper_commit - commit validated state object
1208 * @state: the driver state object
1209 * @nonblock: whether nonblocking behavior is requested.
1211 * This function commits a with drm_atomic_helper_check() pre-validated state
1212 * object. This can still fail when e.g. the framebuffer reservation fails. This
1213 * function implements nonblocking commits, using
1214 * drm_atomic_helper_setup_commit() and related functions.
1216 * Committing the actual hardware state is done through the
1217 * &drm_mode_config_helper_funcs.atomic_commit_tail callback, or it's default
1218 * implementation drm_atomic_helper_commit_tail().
1221 * Zero for success or -errno.
1223 int drm_atomic_helper_commit(struct drm_device *dev,
1224 struct drm_atomic_state *state,
1229 ret = drm_atomic_helper_setup_commit(state, nonblock);
1233 INIT_WORK(&state->commit_work, commit_work);
1235 ret = drm_atomic_helper_prepare_planes(dev, state);
1240 ret = drm_atomic_helper_wait_for_fences(dev, state, true);
1242 drm_atomic_helper_cleanup_planes(dev, state);
1248 * This is the point of no return - everything below never fails except
1249 * when the hw goes bonghits. Which means we can commit the new state on
1250 * the software side now.
1253 drm_atomic_helper_swap_state(state, true);
1256 * Everything below can be run asynchronously without the need to grab
1257 * any modeset locks at all under one condition: It must be guaranteed
1258 * that the asynchronous work has either been cancelled (if the driver
1259 * supports it, which at least requires that the framebuffers get
1260 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
1261 * before the new state gets committed on the software side with
1262 * drm_atomic_helper_swap_state().
1264 * This scheme allows new atomic state updates to be prepared and
1265 * checked in parallel to the asynchronous completion of the previous
1266 * update. Which is important since compositors need to figure out the
1267 * composition of the next frame right after having submitted the
1270 * NOTE: Commit work has multiple phases, first hardware commit, then
1271 * cleanup. We want them to overlap, hence need system_unbound_wq to
1272 * make sure work items don't artifically stall on each another.
1275 drm_atomic_state_get(state);
1277 queue_work(system_unbound_wq, &state->commit_work);
1283 EXPORT_SYMBOL(drm_atomic_helper_commit);
1286 * DOC: implementing nonblocking commit
1288 * Nonblocking atomic commits have to be implemented in the following sequence:
1290 * 1. Run drm_atomic_helper_prepare_planes() first. This is the only function
1291 * which commit needs to call which can fail, so we want to run it first and
1294 * 2. Synchronize with any outstanding nonblocking commit worker threads which
1295 * might be affected the new state update. This can be done by either cancelling
1296 * or flushing the work items, depending upon whether the driver can deal with
1297 * cancelled updates. Note that it is important to ensure that the framebuffer
1298 * cleanup is still done when cancelling.
1300 * Asynchronous workers need to have sufficient parallelism to be able to run
1301 * different atomic commits on different CRTCs in parallel. The simplest way to
1302 * achive this is by running them on the &system_unbound_wq work queue. Note
1303 * that drivers are not required to split up atomic commits and run an
1304 * individual commit in parallel - userspace is supposed to do that if it cares.
1305 * But it might be beneficial to do that for modesets, since those necessarily
1306 * must be done as one global operation, and enabling or disabling a CRTC can
1307 * take a long time. But even that is not required.
1309 * 3. The software state is updated synchronously with
1310 * drm_atomic_helper_swap_state(). Doing this under the protection of all modeset
1311 * locks means concurrent callers never see inconsistent state. And doing this
1312 * while it's guaranteed that no relevant nonblocking worker runs means that
1313 * nonblocking workers do not need grab any locks. Actually they must not grab
1314 * locks, for otherwise the work flushing will deadlock.
1316 * 4. Schedule a work item to do all subsequent steps, using the split-out
1317 * commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and
1318 * then cleaning up the framebuffers after the old framebuffer is no longer
1321 * The above scheme is implemented in the atomic helper libraries in
1322 * drm_atomic_helper_commit() using a bunch of helper functions. See
1323 * drm_atomic_helper_setup_commit() for a starting point.
1326 static int stall_checks(struct drm_crtc *crtc, bool nonblock)
1328 struct drm_crtc_commit *commit, *stall_commit = NULL;
1329 bool completed = true;
1333 spin_lock(&crtc->commit_lock);
1335 list_for_each_entry(commit, &crtc->commit_list, commit_entry) {
1337 completed = try_wait_for_completion(&commit->flip_done);
1338 /* Userspace is not allowed to get ahead of the previous
1339 * commit with nonblocking ones. */
1340 if (!completed && nonblock) {
1341 spin_unlock(&crtc->commit_lock);
1344 } else if (i == 1) {
1345 stall_commit = commit;
1346 drm_crtc_commit_get(stall_commit);
1352 spin_unlock(&crtc->commit_lock);
1357 /* We don't want to let commits get ahead of cleanup work too much,
1358 * stalling on 2nd previous commit means triple-buffer won't ever stall.
1360 ret = wait_for_completion_interruptible_timeout(&stall_commit->cleanup_done,
1363 DRM_ERROR("[CRTC:%d:%s] cleanup_done timed out\n",
1364 crtc->base.id, crtc->name);
1366 drm_crtc_commit_put(stall_commit);
1368 return ret < 0 ? ret : 0;
1371 static void release_crtc_commit(struct completion *completion)
1373 struct drm_crtc_commit *commit = container_of(completion,
1377 drm_crtc_commit_put(commit);
1381 * drm_atomic_helper_setup_commit - setup possibly nonblocking commit
1382 * @state: new modeset state to be committed
1383 * @nonblock: whether nonblocking behavior is requested.
1385 * This function prepares @state to be used by the atomic helper's support for
1386 * nonblocking commits. Drivers using the nonblocking commit infrastructure
1387 * should always call this function from their
1388 * &drm_mode_config_funcs.atomic_commit hook.
1390 * To be able to use this support drivers need to use a few more helper
1391 * functions. drm_atomic_helper_wait_for_dependencies() must be called before
1392 * actually committing the hardware state, and for nonblocking commits this call
1393 * must be placed in the async worker. See also drm_atomic_helper_swap_state()
1394 * and it's stall parameter, for when a driver's commit hooks look at the
1395 * &drm_crtc.state, &drm_plane.state or &drm_connector.state pointer directly.
1397 * Completion of the hardware commit step must be signalled using
1398 * drm_atomic_helper_commit_hw_done(). After this step the driver is not allowed
1399 * to read or change any permanent software or hardware modeset state. The only
1400 * exception is state protected by other means than &drm_modeset_lock locks.
1401 * Only the free standing @state with pointers to the old state structures can
1402 * be inspected, e.g. to clean up old buffers using
1403 * drm_atomic_helper_cleanup_planes().
1405 * At the very end, before cleaning up @state drivers must call
1406 * drm_atomic_helper_commit_cleanup_done().
1408 * This is all implemented by in drm_atomic_helper_commit(), giving drivers a
1409 * complete and esay-to-use default implementation of the atomic_commit() hook.
1411 * The tracking of asynchronously executed and still pending commits is done
1412 * using the core structure &drm_crtc_commit.
1414 * By default there's no need to clean up resources allocated by this function
1415 * explicitly: drm_atomic_state_default_clear() will take care of that
1420 * 0 on success. -EBUSY when userspace schedules nonblocking commits too fast,
1421 * -ENOMEM on allocation failures and -EINTR when a signal is pending.
1423 int drm_atomic_helper_setup_commit(struct drm_atomic_state *state,
1426 struct drm_crtc *crtc;
1427 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
1428 struct drm_crtc_commit *commit;
1431 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
1432 commit = kzalloc(sizeof(*commit), GFP_KERNEL);
1436 init_completion(&commit->flip_done);
1437 init_completion(&commit->hw_done);
1438 init_completion(&commit->cleanup_done);
1439 INIT_LIST_HEAD(&commit->commit_entry);
1440 kref_init(&commit->ref);
1441 commit->crtc = crtc;
1443 state->crtcs[i].commit = commit;
1445 ret = stall_checks(crtc, nonblock);
1449 /* Drivers only send out events when at least either current or
1450 * new CRTC state is active. Complete right away if everything
1452 if (!old_crtc_state->active && !new_crtc_state->active) {
1453 complete_all(&commit->flip_done);
1457 /* Legacy cursor updates are fully unsynced. */
1458 if (state->legacy_cursor_update) {
1459 complete_all(&commit->flip_done);
1463 if (!new_crtc_state->event) {
1464 commit->event = kzalloc(sizeof(*commit->event),
1469 new_crtc_state->event = commit->event;
1472 new_crtc_state->event->base.completion = &commit->flip_done;
1473 new_crtc_state->event->base.completion_release = release_crtc_commit;
1474 drm_crtc_commit_get(commit);
1479 EXPORT_SYMBOL(drm_atomic_helper_setup_commit);
1482 static struct drm_crtc_commit *preceeding_commit(struct drm_crtc *crtc)
1484 struct drm_crtc_commit *commit;
1487 list_for_each_entry(commit, &crtc->commit_list, commit_entry) {
1488 /* skip the first entry, that's the current commit */
1498 * drm_atomic_helper_wait_for_dependencies - wait for required preceeding commits
1499 * @old_state: atomic state object with old state structures
1501 * This function waits for all preceeding commits that touch the same CRTC as
1502 * @old_state to both be committed to the hardware (as signalled by
1503 * drm_atomic_helper_commit_hw_done) and executed by the hardware (as signalled
1504 * by calling drm_crtc_vblank_send_event() on the &drm_crtc_state.event).
1506 * This is part of the atomic helper support for nonblocking commits, see
1507 * drm_atomic_helper_setup_commit() for an overview.
1509 void drm_atomic_helper_wait_for_dependencies(struct drm_atomic_state *old_state)
1511 struct drm_crtc *crtc;
1512 struct drm_crtc_state *new_crtc_state;
1513 struct drm_crtc_commit *commit;
1517 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
1518 spin_lock(&crtc->commit_lock);
1519 commit = preceeding_commit(crtc);
1521 drm_crtc_commit_get(commit);
1522 spin_unlock(&crtc->commit_lock);
1527 ret = wait_for_completion_timeout(&commit->hw_done,
1530 DRM_ERROR("[CRTC:%d:%s] hw_done timed out\n",
1531 crtc->base.id, crtc->name);
1533 /* Currently no support for overwriting flips, hence
1534 * stall for previous one to execute completely. */
1535 ret = wait_for_completion_timeout(&commit->flip_done,
1538 DRM_ERROR("[CRTC:%d:%s] flip_done timed out\n",
1539 crtc->base.id, crtc->name);
1541 drm_crtc_commit_put(commit);
1544 EXPORT_SYMBOL(drm_atomic_helper_wait_for_dependencies);
1547 * drm_atomic_helper_commit_hw_done - setup possible nonblocking commit
1548 * @old_state: atomic state object with old state structures
1550 * This function is used to signal completion of the hardware commit step. After
1551 * this step the driver is not allowed to read or change any permanent software
1552 * or hardware modeset state. The only exception is state protected by other
1553 * means than &drm_modeset_lock locks.
1555 * Drivers should try to postpone any expensive or delayed cleanup work after
1556 * this function is called.
1558 * This is part of the atomic helper support for nonblocking commits, see
1559 * drm_atomic_helper_setup_commit() for an overview.
1561 void drm_atomic_helper_commit_hw_done(struct drm_atomic_state *old_state)
1563 struct drm_crtc *crtc;
1564 struct drm_crtc_state *new_crtc_state;
1565 struct drm_crtc_commit *commit;
1568 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
1569 commit = old_state->crtcs[i].commit;
1573 /* backend must have consumed any event by now */
1574 WARN_ON(new_crtc_state->event);
1575 spin_lock(&crtc->commit_lock);
1576 complete_all(&commit->hw_done);
1577 spin_unlock(&crtc->commit_lock);
1580 EXPORT_SYMBOL(drm_atomic_helper_commit_hw_done);
1583 * drm_atomic_helper_commit_cleanup_done - signal completion of commit
1584 * @old_state: atomic state object with old state structures
1586 * This signals completion of the atomic update @old_state, including any
1587 * cleanup work. If used, it must be called right before calling
1588 * drm_atomic_state_put().
1590 * This is part of the atomic helper support for nonblocking commits, see
1591 * drm_atomic_helper_setup_commit() for an overview.
1593 void drm_atomic_helper_commit_cleanup_done(struct drm_atomic_state *old_state)
1595 struct drm_crtc *crtc;
1596 struct drm_crtc_state *new_crtc_state;
1597 struct drm_crtc_commit *commit;
1601 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
1602 commit = old_state->crtcs[i].commit;
1603 if (WARN_ON(!commit))
1606 spin_lock(&crtc->commit_lock);
1607 complete_all(&commit->cleanup_done);
1608 WARN_ON(!try_wait_for_completion(&commit->hw_done));
1610 /* commit_list borrows our reference, need to remove before we
1611 * clean up our drm_atomic_state. But only after it actually
1612 * completed, otherwise subsequent commits won't stall properly. */
1613 if (try_wait_for_completion(&commit->flip_done))
1616 spin_unlock(&crtc->commit_lock);
1618 /* We must wait for the vblank event to signal our completion
1619 * before releasing our reference, since the vblank work does
1620 * not hold a reference of its own. */
1621 ret = wait_for_completion_timeout(&commit->flip_done,
1624 DRM_ERROR("[CRTC:%d:%s] flip_done timed out\n",
1625 crtc->base.id, crtc->name);
1627 spin_lock(&crtc->commit_lock);
1629 list_del(&commit->commit_entry);
1630 spin_unlock(&crtc->commit_lock);
1633 EXPORT_SYMBOL(drm_atomic_helper_commit_cleanup_done);
1636 * drm_atomic_helper_prepare_planes - prepare plane resources before commit
1638 * @state: atomic state object with new state structures
1640 * This function prepares plane state, specifically framebuffers, for the new
1641 * configuration, by calling &drm_plane_helper_funcs.prepare_fb. If any failure
1642 * is encountered this function will call &drm_plane_helper_funcs.cleanup_fb on
1643 * any already successfully prepared framebuffer.
1646 * 0 on success, negative error code on failure.
1648 int drm_atomic_helper_prepare_planes(struct drm_device *dev,
1649 struct drm_atomic_state *state)
1651 struct drm_plane *plane;
1652 struct drm_plane_state *new_plane_state;
1655 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
1656 const struct drm_plane_helper_funcs *funcs;
1658 funcs = plane->helper_private;
1660 if (funcs->prepare_fb) {
1661 ret = funcs->prepare_fb(plane, new_plane_state);
1670 for_each_new_plane_in_state(state, plane, new_plane_state, j) {
1671 const struct drm_plane_helper_funcs *funcs;
1676 funcs = plane->helper_private;
1678 if (funcs->cleanup_fb)
1679 funcs->cleanup_fb(plane, new_plane_state);
1684 EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
1686 static bool plane_crtc_active(const struct drm_plane_state *state)
1688 return state->crtc && state->crtc->state->active;
1692 * drm_atomic_helper_commit_planes - commit plane state
1694 * @old_state: atomic state object with old state structures
1695 * @flags: flags for committing plane state
1697 * This function commits the new plane state using the plane and atomic helper
1698 * functions for planes and crtcs. It assumes that the atomic state has already
1699 * been pushed into the relevant object state pointers, since this step can no
1702 * It still requires the global state object @old_state to know which planes and
1703 * crtcs need to be updated though.
1705 * Note that this function does all plane updates across all CRTCs in one step.
1706 * If the hardware can't support this approach look at
1707 * drm_atomic_helper_commit_planes_on_crtc() instead.
1709 * Plane parameters can be updated by applications while the associated CRTC is
1710 * disabled. The DRM/KMS core will store the parameters in the plane state,
1711 * which will be available to the driver when the CRTC is turned on. As a result
1712 * most drivers don't need to be immediately notified of plane updates for a
1715 * Unless otherwise needed, drivers are advised to set the ACTIVE_ONLY flag in
1716 * @flags in order not to receive plane update notifications related to a
1717 * disabled CRTC. This avoids the need to manually ignore plane updates in
1718 * driver code when the driver and/or hardware can't or just don't need to deal
1719 * with updates on disabled CRTCs, for example when supporting runtime PM.
1721 * Drivers may set the NO_DISABLE_AFTER_MODESET flag in @flags if the relevant
1722 * display controllers require to disable a CRTC's planes when the CRTC is
1723 * disabled. This function would skip the &drm_plane_helper_funcs.atomic_disable
1724 * call for a plane if the CRTC of the old plane state needs a modesetting
1725 * operation. Of course, the drivers need to disable the planes in their CRTC
1726 * disable callbacks since no one else would do that.
1728 * The drm_atomic_helper_commit() default implementation doesn't set the
1729 * ACTIVE_ONLY flag to most closely match the behaviour of the legacy helpers.
1730 * This should not be copied blindly by drivers.
1732 void drm_atomic_helper_commit_planes(struct drm_device *dev,
1733 struct drm_atomic_state *old_state,
1736 struct drm_crtc *crtc;
1737 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
1738 struct drm_plane *plane;
1739 struct drm_plane_state *old_plane_state, *new_plane_state;
1741 bool active_only = flags & DRM_PLANE_COMMIT_ACTIVE_ONLY;
1742 bool no_disable = flags & DRM_PLANE_COMMIT_NO_DISABLE_AFTER_MODESET;
1744 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
1745 const struct drm_crtc_helper_funcs *funcs;
1747 funcs = crtc->helper_private;
1749 if (!funcs || !funcs->atomic_begin)
1752 if (active_only && !new_crtc_state->active)
1755 funcs->atomic_begin(crtc, old_crtc_state);
1758 for_each_oldnew_plane_in_state(old_state, plane, old_plane_state, new_plane_state, i) {
1759 const struct drm_plane_helper_funcs *funcs;
1762 funcs = plane->helper_private;
1767 disabling = drm_atomic_plane_disabling(old_plane_state,
1772 * Skip planes related to inactive CRTCs. If the plane
1773 * is enabled use the state of the current CRTC. If the
1774 * plane is being disabled use the state of the old
1775 * CRTC to avoid skipping planes being disabled on an
1778 if (!disabling && !plane_crtc_active(new_plane_state))
1780 if (disabling && !plane_crtc_active(old_plane_state))
1785 * Special-case disabling the plane if drivers support it.
1787 if (disabling && funcs->atomic_disable) {
1788 struct drm_crtc_state *crtc_state;
1790 crtc_state = old_plane_state->crtc->state;
1792 if (drm_atomic_crtc_needs_modeset(crtc_state) &&
1796 funcs->atomic_disable(plane, old_plane_state);
1797 } else if (new_plane_state->crtc || disabling) {
1798 funcs->atomic_update(plane, old_plane_state);
1802 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
1803 const struct drm_crtc_helper_funcs *funcs;
1805 funcs = crtc->helper_private;
1807 if (!funcs || !funcs->atomic_flush)
1810 if (active_only && !new_crtc_state->active)
1813 funcs->atomic_flush(crtc, old_crtc_state);
1816 EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
1819 * drm_atomic_helper_commit_planes_on_crtc - commit plane state for a crtc
1820 * @old_crtc_state: atomic state object with the old crtc state
1822 * This function commits the new plane state using the plane and atomic helper
1823 * functions for planes on the specific crtc. It assumes that the atomic state
1824 * has already been pushed into the relevant object state pointers, since this
1825 * step can no longer fail.
1827 * This function is useful when plane updates should be done crtc-by-crtc
1828 * instead of one global step like drm_atomic_helper_commit_planes() does.
1830 * This function can only be savely used when planes are not allowed to move
1831 * between different CRTCs because this function doesn't handle inter-CRTC
1832 * depencies. Callers need to ensure that either no such depencies exist,
1833 * resolve them through ordering of commit calls or through some other means.
1836 drm_atomic_helper_commit_planes_on_crtc(struct drm_crtc_state *old_crtc_state)
1838 const struct drm_crtc_helper_funcs *crtc_funcs;
1839 struct drm_crtc *crtc = old_crtc_state->crtc;
1840 struct drm_atomic_state *old_state = old_crtc_state->state;
1841 struct drm_plane *plane;
1842 unsigned plane_mask;
1844 plane_mask = old_crtc_state->plane_mask;
1845 plane_mask |= crtc->state->plane_mask;
1847 crtc_funcs = crtc->helper_private;
1848 if (crtc_funcs && crtc_funcs->atomic_begin)
1849 crtc_funcs->atomic_begin(crtc, old_crtc_state);
1851 drm_for_each_plane_mask(plane, crtc->dev, plane_mask) {
1852 struct drm_plane_state *old_plane_state =
1853 drm_atomic_get_existing_plane_state(old_state, plane);
1854 const struct drm_plane_helper_funcs *plane_funcs;
1856 plane_funcs = plane->helper_private;
1858 if (!old_plane_state || !plane_funcs)
1861 WARN_ON(plane->state->crtc && plane->state->crtc != crtc);
1863 if (drm_atomic_plane_disabling(old_plane_state, plane->state) &&
1864 plane_funcs->atomic_disable)
1865 plane_funcs->atomic_disable(plane, old_plane_state);
1866 else if (plane->state->crtc ||
1867 drm_atomic_plane_disabling(old_plane_state, plane->state))
1868 plane_funcs->atomic_update(plane, old_plane_state);
1871 if (crtc_funcs && crtc_funcs->atomic_flush)
1872 crtc_funcs->atomic_flush(crtc, old_crtc_state);
1874 EXPORT_SYMBOL(drm_atomic_helper_commit_planes_on_crtc);
1877 * drm_atomic_helper_disable_planes_on_crtc - helper to disable CRTC's planes
1878 * @old_crtc_state: atomic state object with the old CRTC state
1879 * @atomic: if set, synchronize with CRTC's atomic_begin/flush hooks
1881 * Disables all planes associated with the given CRTC. This can be
1882 * used for instance in the CRTC helper atomic_disable callback to disable
1885 * If the atomic-parameter is set the function calls the CRTC's
1886 * atomic_begin hook before and atomic_flush hook after disabling the
1889 * It is a bug to call this function without having implemented the
1890 * &drm_plane_helper_funcs.atomic_disable plane hook.
1893 drm_atomic_helper_disable_planes_on_crtc(struct drm_crtc_state *old_crtc_state,
1896 struct drm_crtc *crtc = old_crtc_state->crtc;
1897 const struct drm_crtc_helper_funcs *crtc_funcs =
1898 crtc->helper_private;
1899 struct drm_plane *plane;
1901 if (atomic && crtc_funcs && crtc_funcs->atomic_begin)
1902 crtc_funcs->atomic_begin(crtc, NULL);
1904 drm_atomic_crtc_state_for_each_plane(plane, old_crtc_state) {
1905 const struct drm_plane_helper_funcs *plane_funcs =
1906 plane->helper_private;
1911 WARN_ON(!plane_funcs->atomic_disable);
1912 if (plane_funcs->atomic_disable)
1913 plane_funcs->atomic_disable(plane, NULL);
1916 if (atomic && crtc_funcs && crtc_funcs->atomic_flush)
1917 crtc_funcs->atomic_flush(crtc, NULL);
1919 EXPORT_SYMBOL(drm_atomic_helper_disable_planes_on_crtc);
1922 * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
1924 * @old_state: atomic state object with old state structures
1926 * This function cleans up plane state, specifically framebuffers, from the old
1927 * configuration. Hence the old configuration must be perserved in @old_state to
1928 * be able to call this function.
1930 * This function must also be called on the new state when the atomic update
1931 * fails at any point after calling drm_atomic_helper_prepare_planes().
1933 void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
1934 struct drm_atomic_state *old_state)
1936 struct drm_plane *plane;
1937 struct drm_plane_state *old_plane_state, *new_plane_state;
1940 for_each_oldnew_plane_in_state(old_state, plane, old_plane_state, new_plane_state, i) {
1941 const struct drm_plane_helper_funcs *funcs;
1942 struct drm_plane_state *plane_state;
1945 * This might be called before swapping when commit is aborted,
1946 * in which case we have to cleanup the new state.
1948 if (old_plane_state == plane->state)
1949 plane_state = new_plane_state;
1951 plane_state = old_plane_state;
1953 funcs = plane->helper_private;
1955 if (funcs->cleanup_fb)
1956 funcs->cleanup_fb(plane, plane_state);
1959 EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
1962 * drm_atomic_helper_swap_state - store atomic state into current sw state
1963 * @state: atomic state
1964 * @stall: stall for proceeding commits
1966 * This function stores the atomic state into the current state pointers in all
1967 * driver objects. It should be called after all failing steps have been done
1968 * and succeeded, but before the actual hardware state is committed.
1970 * For cleanup and error recovery the current state for all changed objects will
1971 * be swaped into @state.
1973 * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
1975 * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
1977 * 2. Do any other steps that might fail.
1979 * 3. Put the staged state into the current state pointers with this function.
1981 * 4. Actually commit the hardware state.
1983 * 5. Call drm_atomic_helper_cleanup_planes() with @state, which since step 3
1984 * contains the old state. Also do any other cleanup required with that state.
1986 * @stall must be set when nonblocking commits for this driver directly access
1987 * the &drm_plane.state, &drm_crtc.state or &drm_connector.state pointer. With
1988 * the current atomic helpers this is almost always the case, since the helpers
1989 * don't pass the right state structures to the callbacks.
1991 void drm_atomic_helper_swap_state(struct drm_atomic_state *state,
1996 struct drm_connector *connector;
1997 struct drm_connector_state *old_conn_state, *new_conn_state;
1998 struct drm_crtc *crtc;
1999 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
2000 struct drm_plane *plane;
2001 struct drm_plane_state *old_plane_state, *new_plane_state;
2002 struct drm_crtc_commit *commit;
2005 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
2006 spin_lock(&crtc->commit_lock);
2007 commit = list_first_entry_or_null(&crtc->commit_list,
2008 struct drm_crtc_commit, commit_entry);
2010 drm_crtc_commit_get(commit);
2011 spin_unlock(&crtc->commit_lock);
2016 ret = wait_for_completion_timeout(&commit->hw_done,
2019 DRM_ERROR("[CRTC:%d:%s] hw_done timed out\n",
2020 crtc->base.id, crtc->name);
2021 drm_crtc_commit_put(commit);
2025 for_each_oldnew_connector_in_state(state, connector, old_conn_state, new_conn_state, i) {
2026 WARN_ON(connector->state != old_conn_state);
2028 old_conn_state->state = state;
2029 new_conn_state->state = NULL;
2031 state->connectors[i].state = old_conn_state;
2032 connector->state = new_conn_state;
2035 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
2036 WARN_ON(crtc->state != old_crtc_state);
2038 old_crtc_state->state = state;
2039 new_crtc_state->state = NULL;
2041 state->crtcs[i].state = old_crtc_state;
2042 crtc->state = new_crtc_state;
2044 if (state->crtcs[i].commit) {
2045 spin_lock(&crtc->commit_lock);
2046 list_add(&state->crtcs[i].commit->commit_entry,
2047 &crtc->commit_list);
2048 spin_unlock(&crtc->commit_lock);
2050 state->crtcs[i].commit->event = NULL;
2054 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
2055 WARN_ON(plane->state != old_plane_state);
2057 old_plane_state->state = state;
2058 new_plane_state->state = NULL;
2060 state->planes[i].state = old_plane_state;
2061 plane->state = new_plane_state;
2064 EXPORT_SYMBOL(drm_atomic_helper_swap_state);
2067 * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
2068 * @plane: plane object to update
2069 * @crtc: owning CRTC of owning plane
2070 * @fb: framebuffer to flip onto plane
2071 * @crtc_x: x offset of primary plane on crtc
2072 * @crtc_y: y offset of primary plane on crtc
2073 * @crtc_w: width of primary plane rectangle on crtc
2074 * @crtc_h: height of primary plane rectangle on crtc
2075 * @src_x: x offset of @fb for panning
2076 * @src_y: y offset of @fb for panning
2077 * @src_w: width of source rectangle in @fb
2078 * @src_h: height of source rectangle in @fb
2080 * Provides a default plane update handler using the atomic driver interface.
2083 * Zero on success, error code on failure
2085 int drm_atomic_helper_update_plane(struct drm_plane *plane,
2086 struct drm_crtc *crtc,
2087 struct drm_framebuffer *fb,
2088 int crtc_x, int crtc_y,
2089 unsigned int crtc_w, unsigned int crtc_h,
2090 uint32_t src_x, uint32_t src_y,
2091 uint32_t src_w, uint32_t src_h)
2093 struct drm_atomic_state *state;
2094 struct drm_plane_state *plane_state;
2097 state = drm_atomic_state_alloc(plane->dev);
2101 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2103 plane_state = drm_atomic_get_plane_state(state, plane);
2104 if (IS_ERR(plane_state)) {
2105 ret = PTR_ERR(plane_state);
2109 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
2112 drm_atomic_set_fb_for_plane(plane_state, fb);
2113 plane_state->crtc_x = crtc_x;
2114 plane_state->crtc_y = crtc_y;
2115 plane_state->crtc_w = crtc_w;
2116 plane_state->crtc_h = crtc_h;
2117 plane_state->src_x = src_x;
2118 plane_state->src_y = src_y;
2119 plane_state->src_w = src_w;
2120 plane_state->src_h = src_h;
2122 if (plane == crtc->cursor)
2123 state->legacy_cursor_update = true;
2125 ret = drm_atomic_commit(state);
2127 if (ret == -EDEADLK)
2130 drm_atomic_state_put(state);
2134 drm_atomic_state_clear(state);
2135 drm_atomic_legacy_backoff(state);
2138 * Someone might have exchanged the framebuffer while we dropped locks
2139 * in the backoff code. We need to fix up the fb refcount tracking the
2142 plane->old_fb = plane->fb;
2146 EXPORT_SYMBOL(drm_atomic_helper_update_plane);
2149 * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
2150 * @plane: plane to disable
2152 * Provides a default plane disable handler using the atomic driver interface.
2155 * Zero on success, error code on failure
2157 int drm_atomic_helper_disable_plane(struct drm_plane *plane)
2159 struct drm_atomic_state *state;
2160 struct drm_plane_state *plane_state;
2164 * FIXME: Without plane->crtc set we can't get at the implicit legacy
2165 * acquire context. The real fix will be to wire the acquire ctx through
2166 * everywhere we need it, but meanwhile prevent chaos by just skipping
2167 * this noop. The critical case is the cursor ioctls which a) only grab
2168 * crtc/cursor-plane locks (so we need the crtc to get at the right
2169 * acquire context) and b) can try to disable the plane multiple times.
2174 state = drm_atomic_state_alloc(plane->dev);
2178 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(plane->crtc);
2180 plane_state = drm_atomic_get_plane_state(state, plane);
2181 if (IS_ERR(plane_state)) {
2182 ret = PTR_ERR(plane_state);
2186 if (plane_state->crtc && (plane == plane->crtc->cursor))
2187 plane_state->state->legacy_cursor_update = true;
2189 ret = __drm_atomic_helper_disable_plane(plane, plane_state);
2193 ret = drm_atomic_commit(state);
2195 if (ret == -EDEADLK)
2198 drm_atomic_state_put(state);
2202 drm_atomic_state_clear(state);
2203 drm_atomic_legacy_backoff(state);
2206 * Someone might have exchanged the framebuffer while we dropped locks
2207 * in the backoff code. We need to fix up the fb refcount tracking the
2210 plane->old_fb = plane->fb;
2214 EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
2216 /* just used from fb-helper and atomic-helper: */
2217 int __drm_atomic_helper_disable_plane(struct drm_plane *plane,
2218 struct drm_plane_state *plane_state)
2222 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
2226 drm_atomic_set_fb_for_plane(plane_state, NULL);
2227 plane_state->crtc_x = 0;
2228 plane_state->crtc_y = 0;
2229 plane_state->crtc_w = 0;
2230 plane_state->crtc_h = 0;
2231 plane_state->src_x = 0;
2232 plane_state->src_y = 0;
2233 plane_state->src_w = 0;
2234 plane_state->src_h = 0;
2239 static int update_output_state(struct drm_atomic_state *state,
2240 struct drm_mode_set *set)
2242 struct drm_device *dev = set->crtc->dev;
2243 struct drm_crtc *crtc;
2244 struct drm_crtc_state *new_crtc_state;
2245 struct drm_connector *connector;
2246 struct drm_connector_state *new_conn_state;
2249 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
2250 state->acquire_ctx);
2254 /* First disable all connectors on the target crtc. */
2255 ret = drm_atomic_add_affected_connectors(state, set->crtc);
2259 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
2260 if (new_conn_state->crtc == set->crtc) {
2261 ret = drm_atomic_set_crtc_for_connector(new_conn_state,
2266 /* Make sure legacy setCrtc always re-trains */
2267 new_conn_state->link_status = DRM_LINK_STATUS_GOOD;
2271 /* Then set all connectors from set->connectors on the target crtc */
2272 for (i = 0; i < set->num_connectors; i++) {
2273 new_conn_state = drm_atomic_get_connector_state(state,
2274 set->connectors[i]);
2275 if (IS_ERR(new_conn_state))
2276 return PTR_ERR(new_conn_state);
2278 ret = drm_atomic_set_crtc_for_connector(new_conn_state,
2284 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
2285 /* Don't update ->enable for the CRTC in the set_config request,
2286 * since a mismatch would indicate a bug in the upper layers.
2287 * The actual modeset code later on will catch any
2288 * inconsistencies here. */
2289 if (crtc == set->crtc)
2292 if (!new_crtc_state->connector_mask) {
2293 ret = drm_atomic_set_mode_prop_for_crtc(new_crtc_state,
2298 new_crtc_state->active = false;
2306 * drm_atomic_helper_set_config - set a new config from userspace
2307 * @set: mode set configuration
2309 * Provides a default crtc set_config handler using the atomic driver interface.
2311 * NOTE: For backwards compatibility with old userspace this automatically
2312 * resets the "link-status" property to GOOD, to force any link
2313 * re-training. The SETCRTC ioctl does not define whether an update does
2314 * need a full modeset or just a plane update, hence we're allowed to do
2315 * that. See also drm_mode_connector_set_link_status_property().
2318 * Returns 0 on success, negative errno numbers on failure.
2320 int drm_atomic_helper_set_config(struct drm_mode_set *set)
2322 struct drm_atomic_state *state;
2323 struct drm_crtc *crtc = set->crtc;
2326 state = drm_atomic_state_alloc(crtc->dev);
2330 state->legacy_set_config = true;
2331 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2333 ret = __drm_atomic_helper_set_config(set, state);
2337 ret = drm_atomic_commit(state);
2339 if (ret == -EDEADLK)
2342 drm_atomic_state_put(state);
2346 drm_atomic_state_clear(state);
2347 drm_atomic_legacy_backoff(state);
2350 * Someone might have exchanged the framebuffer while we dropped locks
2351 * in the backoff code. We need to fix up the fb refcount tracking the
2354 crtc->primary->old_fb = crtc->primary->fb;
2358 EXPORT_SYMBOL(drm_atomic_helper_set_config);
2360 /* just used from fb-helper and atomic-helper: */
2361 int __drm_atomic_helper_set_config(struct drm_mode_set *set,
2362 struct drm_atomic_state *state)
2364 struct drm_crtc_state *crtc_state;
2365 struct drm_plane_state *primary_state;
2366 struct drm_crtc *crtc = set->crtc;
2367 int hdisplay, vdisplay;
2370 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2371 if (IS_ERR(crtc_state))
2372 return PTR_ERR(crtc_state);
2374 primary_state = drm_atomic_get_plane_state(state, crtc->primary);
2375 if (IS_ERR(primary_state))
2376 return PTR_ERR(primary_state);
2380 WARN_ON(set->num_connectors);
2382 ret = drm_atomic_set_mode_for_crtc(crtc_state, NULL);
2386 crtc_state->active = false;
2388 ret = drm_atomic_set_crtc_for_plane(primary_state, NULL);
2392 drm_atomic_set_fb_for_plane(primary_state, NULL);
2398 WARN_ON(!set->num_connectors);
2400 ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
2404 crtc_state->active = true;
2406 ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
2410 drm_mode_get_hv_timing(set->mode, &hdisplay, &vdisplay);
2412 drm_atomic_set_fb_for_plane(primary_state, set->fb);
2413 primary_state->crtc_x = 0;
2414 primary_state->crtc_y = 0;
2415 primary_state->crtc_w = hdisplay;
2416 primary_state->crtc_h = vdisplay;
2417 primary_state->src_x = set->x << 16;
2418 primary_state->src_y = set->y << 16;
2419 if (drm_rotation_90_or_270(primary_state->rotation)) {
2420 primary_state->src_w = vdisplay << 16;
2421 primary_state->src_h = hdisplay << 16;
2423 primary_state->src_w = hdisplay << 16;
2424 primary_state->src_h = vdisplay << 16;
2428 ret = update_output_state(state, set);
2436 * drm_atomic_helper_disable_all - disable all currently active outputs
2438 * @ctx: lock acquisition context
2440 * Loops through all connectors, finding those that aren't turned off and then
2441 * turns them off by setting their DPMS mode to OFF and deactivating the CRTC
2442 * that they are connected to.
2444 * This is used for example in suspend/resume to disable all currently active
2445 * functions when suspending.
2447 * Note that if callers haven't already acquired all modeset locks this might
2448 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
2451 * 0 on success or a negative error code on failure.
2454 * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
2456 int drm_atomic_helper_disable_all(struct drm_device *dev,
2457 struct drm_modeset_acquire_ctx *ctx)
2459 struct drm_atomic_state *state;
2460 struct drm_connector_state *conn_state;
2461 struct drm_connector *conn;
2462 struct drm_plane_state *plane_state;
2463 struct drm_plane *plane;
2464 struct drm_crtc_state *crtc_state;
2465 struct drm_crtc *crtc;
2468 state = drm_atomic_state_alloc(dev);
2472 state->acquire_ctx = ctx;
2474 drm_for_each_crtc(crtc, dev) {
2475 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2476 if (IS_ERR(crtc_state)) {
2477 ret = PTR_ERR(crtc_state);
2481 crtc_state->active = false;
2483 ret = drm_atomic_set_mode_prop_for_crtc(crtc_state, NULL);
2487 ret = drm_atomic_add_affected_planes(state, crtc);
2491 ret = drm_atomic_add_affected_connectors(state, crtc);
2496 for_each_connector_in_state(state, conn, conn_state, i) {
2497 ret = drm_atomic_set_crtc_for_connector(conn_state, NULL);
2502 for_each_plane_in_state(state, plane, plane_state, i) {
2503 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
2507 drm_atomic_set_fb_for_plane(plane_state, NULL);
2510 ret = drm_atomic_commit(state);
2512 drm_atomic_state_put(state);
2516 EXPORT_SYMBOL(drm_atomic_helper_disable_all);
2519 * drm_atomic_helper_suspend - subsystem-level suspend helper
2522 * Duplicates the current atomic state, disables all active outputs and then
2523 * returns a pointer to the original atomic state to the caller. Drivers can
2524 * pass this pointer to the drm_atomic_helper_resume() helper upon resume to
2525 * restore the output configuration that was active at the time the system
2528 * Note that it is potentially unsafe to use this. The atomic state object
2529 * returned by this function is assumed to be persistent. Drivers must ensure
2530 * that this holds true. Before calling this function, drivers must make sure
2531 * to suspend fbdev emulation so that nothing can be using the device.
2534 * A pointer to a copy of the state before suspend on success or an ERR_PTR()-
2535 * encoded error code on failure. Drivers should store the returned atomic
2536 * state object and pass it to the drm_atomic_helper_resume() helper upon
2540 * drm_atomic_helper_duplicate_state(), drm_atomic_helper_disable_all(),
2541 * drm_atomic_helper_resume(), drm_atomic_helper_commit_duplicated_state()
2543 struct drm_atomic_state *drm_atomic_helper_suspend(struct drm_device *dev)
2545 struct drm_modeset_acquire_ctx ctx;
2546 struct drm_atomic_state *state;
2549 drm_modeset_acquire_init(&ctx, 0);
2552 err = drm_modeset_lock_all_ctx(dev, &ctx);
2554 state = ERR_PTR(err);
2558 state = drm_atomic_helper_duplicate_state(dev, &ctx);
2562 err = drm_atomic_helper_disable_all(dev, &ctx);
2564 drm_atomic_state_put(state);
2565 state = ERR_PTR(err);
2570 if (PTR_ERR(state) == -EDEADLK) {
2571 drm_modeset_backoff(&ctx);
2575 drm_modeset_drop_locks(&ctx);
2576 drm_modeset_acquire_fini(&ctx);
2579 EXPORT_SYMBOL(drm_atomic_helper_suspend);
2582 * drm_atomic_helper_commit_duplicated_state - commit duplicated state
2583 * @state: duplicated atomic state to commit
2584 * @ctx: pointer to acquire_ctx to use for commit.
2586 * The state returned by drm_atomic_helper_duplicate_state() and
2587 * drm_atomic_helper_suspend() is partially invalid, and needs to
2588 * be fixed up before commit.
2591 * 0 on success or a negative error code on failure.
2594 * drm_atomic_helper_suspend()
2596 int drm_atomic_helper_commit_duplicated_state(struct drm_atomic_state *state,
2597 struct drm_modeset_acquire_ctx *ctx)
2600 struct drm_plane *plane;
2601 struct drm_plane_state *new_plane_state;
2602 struct drm_connector *connector;
2603 struct drm_connector_state *new_conn_state;
2604 struct drm_crtc *crtc;
2605 struct drm_crtc_state *new_crtc_state;
2607 state->acquire_ctx = ctx;
2609 for_each_new_plane_in_state(state, plane, new_plane_state, i)
2610 state->planes[i].old_state = plane->state;
2612 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i)
2613 state->crtcs[i].old_state = crtc->state;
2615 for_each_new_connector_in_state(state, connector, new_conn_state, i)
2616 state->connectors[i].old_state = connector->state;
2618 return drm_atomic_commit(state);
2620 EXPORT_SYMBOL(drm_atomic_helper_commit_duplicated_state);
2623 * drm_atomic_helper_resume - subsystem-level resume helper
2625 * @state: atomic state to resume to
2627 * Calls drm_mode_config_reset() to synchronize hardware and software states,
2628 * grabs all modeset locks and commits the atomic state object. This can be
2629 * used in conjunction with the drm_atomic_helper_suspend() helper to
2630 * implement suspend/resume for drivers that support atomic mode-setting.
2633 * 0 on success or a negative error code on failure.
2636 * drm_atomic_helper_suspend()
2638 int drm_atomic_helper_resume(struct drm_device *dev,
2639 struct drm_atomic_state *state)
2641 struct drm_mode_config *config = &dev->mode_config;
2644 drm_mode_config_reset(dev);
2646 drm_modeset_lock_all(dev);
2647 err = drm_atomic_helper_commit_duplicated_state(state, config->acquire_ctx);
2648 drm_modeset_unlock_all(dev);
2652 EXPORT_SYMBOL(drm_atomic_helper_resume);
2655 * drm_atomic_helper_crtc_set_property - helper for crtc properties
2657 * @property: DRM property
2658 * @val: value of property
2660 * Provides a default crtc set_property handler using the atomic driver
2664 * Zero on success, error code on failure
2667 drm_atomic_helper_crtc_set_property(struct drm_crtc *crtc,
2668 struct drm_property *property,
2671 struct drm_atomic_state *state;
2672 struct drm_crtc_state *crtc_state;
2675 state = drm_atomic_state_alloc(crtc->dev);
2679 /* ->set_property is always called with all locks held. */
2680 state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
2682 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2683 if (IS_ERR(crtc_state)) {
2684 ret = PTR_ERR(crtc_state);
2688 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
2693 ret = drm_atomic_commit(state);
2695 if (ret == -EDEADLK)
2698 drm_atomic_state_put(state);
2702 drm_atomic_state_clear(state);
2703 drm_atomic_legacy_backoff(state);
2707 EXPORT_SYMBOL(drm_atomic_helper_crtc_set_property);
2710 * drm_atomic_helper_plane_set_property - helper for plane properties
2712 * @property: DRM property
2713 * @val: value of property
2715 * Provides a default plane set_property handler using the atomic driver
2719 * Zero on success, error code on failure
2722 drm_atomic_helper_plane_set_property(struct drm_plane *plane,
2723 struct drm_property *property,
2726 struct drm_atomic_state *state;
2727 struct drm_plane_state *plane_state;
2730 state = drm_atomic_state_alloc(plane->dev);
2734 /* ->set_property is always called with all locks held. */
2735 state->acquire_ctx = plane->dev->mode_config.acquire_ctx;
2737 plane_state = drm_atomic_get_plane_state(state, plane);
2738 if (IS_ERR(plane_state)) {
2739 ret = PTR_ERR(plane_state);
2743 ret = drm_atomic_plane_set_property(plane, plane_state,
2748 ret = drm_atomic_commit(state);
2750 if (ret == -EDEADLK)
2753 drm_atomic_state_put(state);
2757 drm_atomic_state_clear(state);
2758 drm_atomic_legacy_backoff(state);
2762 EXPORT_SYMBOL(drm_atomic_helper_plane_set_property);
2765 * drm_atomic_helper_connector_set_property - helper for connector properties
2766 * @connector: DRM connector
2767 * @property: DRM property
2768 * @val: value of property
2770 * Provides a default connector set_property handler using the atomic driver
2774 * Zero on success, error code on failure
2777 drm_atomic_helper_connector_set_property(struct drm_connector *connector,
2778 struct drm_property *property,
2781 struct drm_atomic_state *state;
2782 struct drm_connector_state *connector_state;
2785 state = drm_atomic_state_alloc(connector->dev);
2789 /* ->set_property is always called with all locks held. */
2790 state->acquire_ctx = connector->dev->mode_config.acquire_ctx;
2792 connector_state = drm_atomic_get_connector_state(state, connector);
2793 if (IS_ERR(connector_state)) {
2794 ret = PTR_ERR(connector_state);
2798 ret = drm_atomic_connector_set_property(connector, connector_state,
2803 ret = drm_atomic_commit(state);
2805 if (ret == -EDEADLK)
2808 drm_atomic_state_put(state);
2812 drm_atomic_state_clear(state);
2813 drm_atomic_legacy_backoff(state);
2817 EXPORT_SYMBOL(drm_atomic_helper_connector_set_property);
2819 static int page_flip_common(
2820 struct drm_atomic_state *state,
2821 struct drm_crtc *crtc,
2822 struct drm_framebuffer *fb,
2823 struct drm_pending_vblank_event *event,
2826 struct drm_plane *plane = crtc->primary;
2827 struct drm_plane_state *plane_state;
2828 struct drm_crtc_state *crtc_state;
2831 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2832 if (IS_ERR(crtc_state))
2833 return PTR_ERR(crtc_state);
2835 crtc_state->event = event;
2836 crtc_state->pageflip_flags = flags;
2838 plane_state = drm_atomic_get_plane_state(state, plane);
2839 if (IS_ERR(plane_state))
2840 return PTR_ERR(plane_state);
2842 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
2845 drm_atomic_set_fb_for_plane(plane_state, fb);
2847 /* Make sure we don't accidentally do a full modeset. */
2848 state->allow_modeset = false;
2849 if (!crtc_state->active) {
2850 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] disabled, rejecting legacy flip\n",
2851 crtc->base.id, crtc->name);
2859 * drm_atomic_helper_page_flip - execute a legacy page flip
2861 * @fb: DRM framebuffer
2862 * @event: optional DRM event to signal upon completion
2863 * @flags: flip flags for non-vblank sync'ed updates
2865 * Provides a default &drm_crtc_funcs.page_flip implementation
2866 * using the atomic driver interface.
2869 * Returns 0 on success, negative errno numbers on failure.
2872 * drm_atomic_helper_page_flip_target()
2874 int drm_atomic_helper_page_flip(struct drm_crtc *crtc,
2875 struct drm_framebuffer *fb,
2876 struct drm_pending_vblank_event *event,
2879 struct drm_plane *plane = crtc->primary;
2880 struct drm_atomic_state *state;
2883 state = drm_atomic_state_alloc(plane->dev);
2887 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2890 ret = page_flip_common(state, crtc, fb, event, flags);
2894 ret = drm_atomic_nonblocking_commit(state);
2897 if (ret == -EDEADLK)
2900 drm_atomic_state_put(state);
2904 drm_atomic_state_clear(state);
2905 drm_atomic_legacy_backoff(state);
2908 * Someone might have exchanged the framebuffer while we dropped locks
2909 * in the backoff code. We need to fix up the fb refcount tracking the
2912 plane->old_fb = plane->fb;
2916 EXPORT_SYMBOL(drm_atomic_helper_page_flip);
2919 * drm_atomic_helper_page_flip_target - do page flip on target vblank period.
2921 * @fb: DRM framebuffer
2922 * @event: optional DRM event to signal upon completion
2923 * @flags: flip flags for non-vblank sync'ed updates
2924 * @target: specifying the target vblank period when the flip to take effect
2926 * Provides a default &drm_crtc_funcs.page_flip_target implementation.
2927 * Similar to drm_atomic_helper_page_flip() with extra parameter to specify
2928 * target vblank period to flip.
2931 * Returns 0 on success, negative errno numbers on failure.
2933 int drm_atomic_helper_page_flip_target(
2934 struct drm_crtc *crtc,
2935 struct drm_framebuffer *fb,
2936 struct drm_pending_vblank_event *event,
2940 struct drm_plane *plane = crtc->primary;
2941 struct drm_atomic_state *state;
2942 struct drm_crtc_state *crtc_state;
2945 state = drm_atomic_state_alloc(plane->dev);
2949 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2952 ret = page_flip_common(state, crtc, fb, event, flags);
2956 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
2957 if (WARN_ON(!crtc_state)) {
2961 crtc_state->target_vblank = target;
2963 ret = drm_atomic_nonblocking_commit(state);
2966 if (ret == -EDEADLK)
2969 drm_atomic_state_put(state);
2973 drm_atomic_state_clear(state);
2974 drm_atomic_legacy_backoff(state);
2977 * Someone might have exchanged the framebuffer while we dropped locks
2978 * in the backoff code. We need to fix up the fb refcount tracking the
2981 plane->old_fb = plane->fb;
2985 EXPORT_SYMBOL(drm_atomic_helper_page_flip_target);
2988 * drm_atomic_helper_connector_dpms() - connector dpms helper implementation
2989 * @connector: affected connector
2992 * This is the main helper function provided by the atomic helper framework for
2993 * implementing the legacy DPMS connector interface. It computes the new desired
2994 * &drm_crtc_state.active state for the corresponding CRTC (if the connector is
2995 * enabled) and updates it.
2998 * Returns 0 on success, negative errno numbers on failure.
3000 int drm_atomic_helper_connector_dpms(struct drm_connector *connector,
3003 struct drm_mode_config *config = &connector->dev->mode_config;
3004 struct drm_atomic_state *state;
3005 struct drm_crtc_state *crtc_state;
3006 struct drm_crtc *crtc;
3007 struct drm_connector *tmp_connector;
3008 struct drm_connector_list_iter conn_iter;
3010 bool active = false;
3011 int old_mode = connector->dpms;
3013 if (mode != DRM_MODE_DPMS_ON)
3014 mode = DRM_MODE_DPMS_OFF;
3016 connector->dpms = mode;
3017 crtc = connector->state->crtc;
3022 state = drm_atomic_state_alloc(connector->dev);
3026 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
3028 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3029 if (IS_ERR(crtc_state)) {
3030 ret = PTR_ERR(crtc_state);
3034 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
3036 drm_connector_list_iter_begin(connector->dev, &conn_iter);
3037 drm_for_each_connector_iter(tmp_connector, &conn_iter) {
3038 if (tmp_connector->state->crtc != crtc)
3041 if (tmp_connector->dpms == DRM_MODE_DPMS_ON) {
3046 drm_connector_list_iter_end(&conn_iter);
3047 crtc_state->active = active;
3049 ret = drm_atomic_commit(state);
3051 if (ret == -EDEADLK)
3054 connector->dpms = old_mode;
3055 drm_atomic_state_put(state);
3059 drm_atomic_state_clear(state);
3060 drm_atomic_legacy_backoff(state);
3064 EXPORT_SYMBOL(drm_atomic_helper_connector_dpms);
3067 * drm_atomic_helper_best_encoder - Helper for
3068 * &drm_connector_helper_funcs.best_encoder callback
3069 * @connector: Connector control structure
3071 * This is a &drm_connector_helper_funcs.best_encoder callback helper for
3072 * connectors that support exactly 1 encoder, statically determined at driver
3075 struct drm_encoder *
3076 drm_atomic_helper_best_encoder(struct drm_connector *connector)
3078 WARN_ON(connector->encoder_ids[1]);
3079 return drm_encoder_find(connector->dev, connector->encoder_ids[0]);
3081 EXPORT_SYMBOL(drm_atomic_helper_best_encoder);
3084 * DOC: atomic state reset and initialization
3086 * Both the drm core and the atomic helpers assume that there is always the full
3087 * and correct atomic software state for all connectors, CRTCs and planes
3088 * available. Which is a bit a problem on driver load and also after system
3089 * suspend. One way to solve this is to have a hardware state read-out
3090 * infrastructure which reconstructs the full software state (e.g. the i915
3093 * The simpler solution is to just reset the software state to everything off,
3094 * which is easiest to do by calling drm_mode_config_reset(). To facilitate this
3095 * the atomic helpers provide default reset implementations for all hooks.
3097 * On the upside the precise state tracking of atomic simplifies system suspend
3098 * and resume a lot. For drivers using drm_mode_config_reset() a complete recipe
3099 * is implemented in drm_atomic_helper_suspend() and drm_atomic_helper_resume().
3100 * For other drivers the building blocks are split out, see the documentation
3101 * for these functions.
3105 * drm_atomic_helper_crtc_reset - default &drm_crtc_funcs.reset hook for CRTCs
3108 * Resets the atomic state for @crtc by freeing the state pointer (which might
3109 * be NULL, e.g. at driver load time) and allocating a new empty state object.
3111 void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc)
3114 __drm_atomic_helper_crtc_destroy_state(crtc->state);
3117 crtc->state = kzalloc(sizeof(*crtc->state), GFP_KERNEL);
3120 crtc->state->crtc = crtc;
3122 EXPORT_SYMBOL(drm_atomic_helper_crtc_reset);
3125 * __drm_atomic_helper_crtc_duplicate_state - copy atomic CRTC state
3126 * @crtc: CRTC object
3127 * @state: atomic CRTC state
3129 * Copies atomic state from a CRTC's current state and resets inferred values.
3130 * This is useful for drivers that subclass the CRTC state.
3132 void __drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc,
3133 struct drm_crtc_state *state)
3135 memcpy(state, crtc->state, sizeof(*state));
3137 if (state->mode_blob)
3138 drm_property_blob_get(state->mode_blob);
3139 if (state->degamma_lut)
3140 drm_property_blob_get(state->degamma_lut);
3142 drm_property_blob_get(state->ctm);
3143 if (state->gamma_lut)
3144 drm_property_blob_get(state->gamma_lut);
3145 state->mode_changed = false;
3146 state->active_changed = false;
3147 state->planes_changed = false;
3148 state->connectors_changed = false;
3149 state->color_mgmt_changed = false;
3150 state->zpos_changed = false;
3151 state->event = NULL;
3152 state->pageflip_flags = 0;
3154 EXPORT_SYMBOL(__drm_atomic_helper_crtc_duplicate_state);
3157 * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
3160 * Default CRTC state duplicate hook for drivers which don't have their own
3161 * subclassed CRTC state structure.
3163 struct drm_crtc_state *
3164 drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc)
3166 struct drm_crtc_state *state;
3168 if (WARN_ON(!crtc->state))
3171 state = kmalloc(sizeof(*state), GFP_KERNEL);
3173 __drm_atomic_helper_crtc_duplicate_state(crtc, state);
3177 EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state);
3180 * __drm_atomic_helper_crtc_destroy_state - release CRTC state
3181 * @state: CRTC state object to release
3183 * Releases all resources stored in the CRTC state without actually freeing
3184 * the memory of the CRTC state. This is useful for drivers that subclass the
3187 void __drm_atomic_helper_crtc_destroy_state(struct drm_crtc_state *state)
3189 drm_property_blob_put(state->mode_blob);
3190 drm_property_blob_put(state->degamma_lut);
3191 drm_property_blob_put(state->ctm);
3192 drm_property_blob_put(state->gamma_lut);
3194 EXPORT_SYMBOL(__drm_atomic_helper_crtc_destroy_state);
3197 * drm_atomic_helper_crtc_destroy_state - default state destroy hook
3199 * @state: CRTC state object to release
3201 * Default CRTC state destroy hook for drivers which don't have their own
3202 * subclassed CRTC state structure.
3204 void drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
3205 struct drm_crtc_state *state)
3207 __drm_atomic_helper_crtc_destroy_state(state);
3210 EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state);
3213 * drm_atomic_helper_plane_reset - default &drm_plane_funcs.reset hook for planes
3216 * Resets the atomic state for @plane by freeing the state pointer (which might
3217 * be NULL, e.g. at driver load time) and allocating a new empty state object.
3219 void drm_atomic_helper_plane_reset(struct drm_plane *plane)
3222 __drm_atomic_helper_plane_destroy_state(plane->state);
3224 kfree(plane->state);
3225 plane->state = kzalloc(sizeof(*plane->state), GFP_KERNEL);
3228 plane->state->plane = plane;
3229 plane->state->rotation = DRM_ROTATE_0;
3232 EXPORT_SYMBOL(drm_atomic_helper_plane_reset);
3235 * __drm_atomic_helper_plane_duplicate_state - copy atomic plane state
3236 * @plane: plane object
3237 * @state: atomic plane state
3239 * Copies atomic state from a plane's current state. This is useful for
3240 * drivers that subclass the plane state.
3242 void __drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane,
3243 struct drm_plane_state *state)
3245 memcpy(state, plane->state, sizeof(*state));
3248 drm_framebuffer_get(state->fb);
3250 state->fence = NULL;
3252 EXPORT_SYMBOL(__drm_atomic_helper_plane_duplicate_state);
3255 * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
3258 * Default plane state duplicate hook for drivers which don't have their own
3259 * subclassed plane state structure.
3261 struct drm_plane_state *
3262 drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane)
3264 struct drm_plane_state *state;
3266 if (WARN_ON(!plane->state))
3269 state = kmalloc(sizeof(*state), GFP_KERNEL);
3271 __drm_atomic_helper_plane_duplicate_state(plane, state);
3275 EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state);
3278 * __drm_atomic_helper_plane_destroy_state - release plane state
3279 * @state: plane state object to release
3281 * Releases all resources stored in the plane state without actually freeing
3282 * the memory of the plane state. This is useful for drivers that subclass the
3285 void __drm_atomic_helper_plane_destroy_state(struct drm_plane_state *state)
3288 drm_framebuffer_put(state->fb);
3291 dma_fence_put(state->fence);
3293 EXPORT_SYMBOL(__drm_atomic_helper_plane_destroy_state);
3296 * drm_atomic_helper_plane_destroy_state - default state destroy hook
3298 * @state: plane state object to release
3300 * Default plane state destroy hook for drivers which don't have their own
3301 * subclassed plane state structure.
3303 void drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
3304 struct drm_plane_state *state)
3306 __drm_atomic_helper_plane_destroy_state(state);
3309 EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state);
3312 * __drm_atomic_helper_connector_reset - reset state on connector
3313 * @connector: drm connector
3314 * @conn_state: connector state to assign
3316 * Initializes the newly allocated @conn_state and assigns it to
3317 * the &drm_conector->state pointer of @connector, usually required when
3318 * initializing the drivers or when called from the &drm_connector_funcs.reset
3321 * This is useful for drivers that subclass the connector state.
3324 __drm_atomic_helper_connector_reset(struct drm_connector *connector,
3325 struct drm_connector_state *conn_state)
3328 conn_state->connector = connector;
3330 connector->state = conn_state;
3332 EXPORT_SYMBOL(__drm_atomic_helper_connector_reset);
3335 * drm_atomic_helper_connector_reset - default &drm_connector_funcs.reset hook for connectors
3336 * @connector: drm connector
3338 * Resets the atomic state for @connector by freeing the state pointer (which
3339 * might be NULL, e.g. at driver load time) and allocating a new empty state
3342 void drm_atomic_helper_connector_reset(struct drm_connector *connector)
3344 struct drm_connector_state *conn_state =
3345 kzalloc(sizeof(*conn_state), GFP_KERNEL);
3347 if (connector->state)
3348 __drm_atomic_helper_connector_destroy_state(connector->state);
3350 kfree(connector->state);
3351 __drm_atomic_helper_connector_reset(connector, conn_state);
3353 EXPORT_SYMBOL(drm_atomic_helper_connector_reset);
3356 * __drm_atomic_helper_connector_duplicate_state - copy atomic connector state
3357 * @connector: connector object
3358 * @state: atomic connector state
3360 * Copies atomic state from a connector's current state. This is useful for
3361 * drivers that subclass the connector state.
3364 __drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector,
3365 struct drm_connector_state *state)
3367 memcpy(state, connector->state, sizeof(*state));
3369 drm_connector_get(connector);
3371 EXPORT_SYMBOL(__drm_atomic_helper_connector_duplicate_state);
3374 * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
3375 * @connector: drm connector
3377 * Default connector state duplicate hook for drivers which don't have their own
3378 * subclassed connector state structure.
3380 struct drm_connector_state *
3381 drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
3383 struct drm_connector_state *state;
3385 if (WARN_ON(!connector->state))
3388 state = kmalloc(sizeof(*state), GFP_KERNEL);
3390 __drm_atomic_helper_connector_duplicate_state(connector, state);
3394 EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
3397 * drm_atomic_helper_duplicate_state - duplicate an atomic state object
3399 * @ctx: lock acquisition context
3401 * Makes a copy of the current atomic state by looping over all objects and
3402 * duplicating their respective states. This is used for example by suspend/
3403 * resume support code to save the state prior to suspend such that it can
3404 * be restored upon resume.
3406 * Note that this treats atomic state as persistent between save and restore.
3407 * Drivers must make sure that this is possible and won't result in confusion
3408 * or erroneous behaviour.
3410 * Note that if callers haven't already acquired all modeset locks this might
3411 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
3414 * A pointer to the copy of the atomic state object on success or an
3415 * ERR_PTR()-encoded error code on failure.
3418 * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
3420 struct drm_atomic_state *
3421 drm_atomic_helper_duplicate_state(struct drm_device *dev,
3422 struct drm_modeset_acquire_ctx *ctx)
3424 struct drm_atomic_state *state;
3425 struct drm_connector *conn;
3426 struct drm_connector_list_iter conn_iter;
3427 struct drm_plane *plane;
3428 struct drm_crtc *crtc;
3431 state = drm_atomic_state_alloc(dev);
3433 return ERR_PTR(-ENOMEM);
3435 state->acquire_ctx = ctx;
3437 drm_for_each_crtc(crtc, dev) {
3438 struct drm_crtc_state *crtc_state;
3440 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3441 if (IS_ERR(crtc_state)) {
3442 err = PTR_ERR(crtc_state);
3447 drm_for_each_plane(plane, dev) {
3448 struct drm_plane_state *plane_state;
3450 plane_state = drm_atomic_get_plane_state(state, plane);
3451 if (IS_ERR(plane_state)) {
3452 err = PTR_ERR(plane_state);
3457 drm_connector_list_iter_begin(dev, &conn_iter);
3458 drm_for_each_connector_iter(conn, &conn_iter) {
3459 struct drm_connector_state *conn_state;
3461 conn_state = drm_atomic_get_connector_state(state, conn);
3462 if (IS_ERR(conn_state)) {
3463 err = PTR_ERR(conn_state);
3464 drm_connector_list_iter_end(&conn_iter);
3468 drm_connector_list_iter_end(&conn_iter);
3470 /* clear the acquire context so that it isn't accidentally reused */
3471 state->acquire_ctx = NULL;
3475 drm_atomic_state_put(state);
3476 state = ERR_PTR(err);
3481 EXPORT_SYMBOL(drm_atomic_helper_duplicate_state);
3484 * __drm_atomic_helper_connector_destroy_state - release connector state
3485 * @state: connector state object to release
3487 * Releases all resources stored in the connector state without actually
3488 * freeing the memory of the connector state. This is useful for drivers that
3489 * subclass the connector state.
3492 __drm_atomic_helper_connector_destroy_state(struct drm_connector_state *state)
3495 drm_connector_put(state->connector);
3497 EXPORT_SYMBOL(__drm_atomic_helper_connector_destroy_state);
3500 * drm_atomic_helper_connector_destroy_state - default state destroy hook
3501 * @connector: drm connector
3502 * @state: connector state object to release
3504 * Default connector state destroy hook for drivers which don't have their own
3505 * subclassed connector state structure.
3507 void drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
3508 struct drm_connector_state *state)
3510 __drm_atomic_helper_connector_destroy_state(state);
3513 EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state);
3516 * drm_atomic_helper_legacy_gamma_set - set the legacy gamma correction table
3517 * @crtc: CRTC object
3518 * @red: red correction table
3519 * @green: green correction table
3520 * @blue: green correction table
3521 * @size: size of the tables
3523 * Implements support for legacy gamma correction table for drivers
3524 * that support color management through the DEGAMMA_LUT/GAMMA_LUT
3527 int drm_atomic_helper_legacy_gamma_set(struct drm_crtc *crtc,
3528 u16 *red, u16 *green, u16 *blue,
3531 struct drm_device *dev = crtc->dev;
3532 struct drm_mode_config *config = &dev->mode_config;
3533 struct drm_atomic_state *state;
3534 struct drm_crtc_state *crtc_state;
3535 struct drm_property_blob *blob = NULL;
3536 struct drm_color_lut *blob_data;
3539 state = drm_atomic_state_alloc(crtc->dev);
3543 blob = drm_property_create_blob(dev,
3544 sizeof(struct drm_color_lut) * size,
3547 ret = PTR_ERR(blob);
3552 /* Prepare GAMMA_LUT with the legacy values. */
3553 blob_data = (struct drm_color_lut *) blob->data;
3554 for (i = 0; i < size; i++) {
3555 blob_data[i].red = red[i];
3556 blob_data[i].green = green[i];
3557 blob_data[i].blue = blue[i];
3560 state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
3562 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3563 if (IS_ERR(crtc_state)) {
3564 ret = PTR_ERR(crtc_state);
3568 /* Reset DEGAMMA_LUT and CTM properties. */
3569 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
3570 config->degamma_lut_property, 0);
3574 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
3575 config->ctm_property, 0);
3579 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
3580 config->gamma_lut_property, blob->base.id);
3584 ret = drm_atomic_commit(state);
3586 if (ret == -EDEADLK)
3589 drm_atomic_state_put(state);
3590 drm_property_blob_put(blob);
3594 drm_atomic_state_clear(state);
3595 drm_atomic_legacy_backoff(state);
3599 EXPORT_SYMBOL(drm_atomic_helper_legacy_gamma_set);