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_new_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_new_crtc_state(state, plane_state->crtc);
85 if (WARN_ON(!crtc_state))
88 crtc_state->planes_changed = true;
92 static int handle_conflicting_encoders(struct drm_atomic_state *state,
93 bool disable_conflicting_encoders)
95 struct drm_connector_state *new_conn_state;
96 struct drm_connector *connector;
97 struct drm_connector_list_iter conn_iter;
98 struct drm_encoder *encoder;
99 unsigned encoder_mask = 0;
103 * First loop, find all newly assigned encoders from the connectors
104 * part of the state. If the same encoder is assigned to multiple
105 * connectors bail out.
107 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
108 const struct drm_connector_helper_funcs *funcs = connector->helper_private;
109 struct drm_encoder *new_encoder;
111 if (!new_conn_state->crtc)
114 if (funcs->atomic_best_encoder)
115 new_encoder = funcs->atomic_best_encoder(connector, new_conn_state);
116 else if (funcs->best_encoder)
117 new_encoder = funcs->best_encoder(connector);
119 new_encoder = drm_atomic_helper_best_encoder(connector);
122 if (encoder_mask & (1 << drm_encoder_index(new_encoder))) {
123 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] on [CONNECTOR:%d:%s] already assigned\n",
124 new_encoder->base.id, new_encoder->name,
125 connector->base.id, connector->name);
130 encoder_mask |= 1 << drm_encoder_index(new_encoder);
138 * Second loop, iterate over all connectors not part of the state.
140 * If a conflicting encoder is found and disable_conflicting_encoders
141 * is not set, an error is returned. Userspace can provide a solution
142 * through the atomic ioctl.
144 * If the flag is set conflicting connectors are removed from the crtc
145 * and the crtc is disabled if no encoder is left. This preserves
146 * compatibility with the legacy set_config behavior.
148 drm_connector_list_iter_begin(state->dev, &conn_iter);
149 drm_for_each_connector_iter(connector, &conn_iter) {
150 struct drm_crtc_state *crtc_state;
152 if (drm_atomic_get_new_connector_state(state, connector))
155 encoder = connector->state->best_encoder;
156 if (!encoder || !(encoder_mask & (1 << drm_encoder_index(encoder))))
159 if (!disable_conflicting_encoders) {
160 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s] by [CONNECTOR:%d:%s]\n",
161 encoder->base.id, encoder->name,
162 connector->state->crtc->base.id,
163 connector->state->crtc->name,
164 connector->base.id, connector->name);
169 new_conn_state = drm_atomic_get_connector_state(state, connector);
170 if (IS_ERR(new_conn_state)) {
171 ret = PTR_ERR(new_conn_state);
175 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s], disabling [CONNECTOR:%d:%s]\n",
176 encoder->base.id, encoder->name,
177 new_conn_state->crtc->base.id, new_conn_state->crtc->name,
178 connector->base.id, connector->name);
180 crtc_state = drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
182 ret = drm_atomic_set_crtc_for_connector(new_conn_state, NULL);
186 if (!crtc_state->connector_mask) {
187 ret = drm_atomic_set_mode_prop_for_crtc(crtc_state,
192 crtc_state->active = false;
196 drm_connector_list_iter_end(&conn_iter);
202 set_best_encoder(struct drm_atomic_state *state,
203 struct drm_connector_state *conn_state,
204 struct drm_encoder *encoder)
206 struct drm_crtc_state *crtc_state;
207 struct drm_crtc *crtc;
209 if (conn_state->best_encoder) {
210 /* Unset the encoder_mask in the old crtc state. */
211 crtc = conn_state->connector->state->crtc;
213 /* A NULL crtc is an error here because we should have
214 * duplicated a NULL best_encoder when crtc was NULL.
215 * As an exception restoring duplicated atomic state
216 * during resume is allowed, so don't warn when
217 * best_encoder is equal to encoder we intend to set.
219 WARN_ON(!crtc && encoder != conn_state->best_encoder);
221 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
223 crtc_state->encoder_mask &=
224 ~(1 << drm_encoder_index(conn_state->best_encoder));
229 crtc = conn_state->crtc;
232 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
234 crtc_state->encoder_mask |=
235 1 << drm_encoder_index(encoder);
239 conn_state->best_encoder = encoder;
243 steal_encoder(struct drm_atomic_state *state,
244 struct drm_encoder *encoder)
246 struct drm_crtc_state *crtc_state;
247 struct drm_connector *connector;
248 struct drm_connector_state *old_connector_state, *new_connector_state;
251 for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
252 struct drm_crtc *encoder_crtc;
254 if (new_connector_state->best_encoder != encoder)
257 encoder_crtc = old_connector_state->crtc;
259 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s], stealing it\n",
260 encoder->base.id, encoder->name,
261 encoder_crtc->base.id, encoder_crtc->name);
263 set_best_encoder(state, new_connector_state, NULL);
265 crtc_state = drm_atomic_get_new_crtc_state(state, encoder_crtc);
266 crtc_state->connectors_changed = true;
273 update_connector_routing(struct drm_atomic_state *state,
274 struct drm_connector *connector,
275 struct drm_connector_state *old_connector_state,
276 struct drm_connector_state *new_connector_state)
278 const struct drm_connector_helper_funcs *funcs;
279 struct drm_encoder *new_encoder;
280 struct drm_crtc_state *crtc_state;
282 DRM_DEBUG_ATOMIC("Updating routing for [CONNECTOR:%d:%s]\n",
286 if (old_connector_state->crtc != new_connector_state->crtc) {
287 if (old_connector_state->crtc) {
288 crtc_state = drm_atomic_get_new_crtc_state(state, old_connector_state->crtc);
289 crtc_state->connectors_changed = true;
292 if (new_connector_state->crtc) {
293 crtc_state = drm_atomic_get_new_crtc_state(state, new_connector_state->crtc);
294 crtc_state->connectors_changed = true;
298 if (!new_connector_state->crtc) {
299 DRM_DEBUG_ATOMIC("Disabling [CONNECTOR:%d:%s]\n",
303 set_best_encoder(state, new_connector_state, NULL);
308 funcs = connector->helper_private;
310 if (funcs->atomic_best_encoder)
311 new_encoder = funcs->atomic_best_encoder(connector,
312 new_connector_state);
313 else if (funcs->best_encoder)
314 new_encoder = funcs->best_encoder(connector);
316 new_encoder = drm_atomic_helper_best_encoder(connector);
319 DRM_DEBUG_ATOMIC("No suitable encoder found for [CONNECTOR:%d:%s]\n",
325 if (!drm_encoder_crtc_ok(new_encoder, new_connector_state->crtc)) {
326 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] incompatible with [CRTC:%d:%s]\n",
327 new_encoder->base.id,
329 new_connector_state->crtc->base.id,
330 new_connector_state->crtc->name);
334 if (new_encoder == new_connector_state->best_encoder) {
335 set_best_encoder(state, new_connector_state, new_encoder);
337 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] keeps [ENCODER:%d:%s], now on [CRTC:%d:%s]\n",
340 new_encoder->base.id,
342 new_connector_state->crtc->base.id,
343 new_connector_state->crtc->name);
348 steal_encoder(state, new_encoder);
350 set_best_encoder(state, new_connector_state, new_encoder);
352 crtc_state = drm_atomic_get_new_crtc_state(state, new_connector_state->crtc);
353 crtc_state->connectors_changed = true;
355 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] using [ENCODER:%d:%s] on [CRTC:%d:%s]\n",
358 new_encoder->base.id,
360 new_connector_state->crtc->base.id,
361 new_connector_state->crtc->name);
367 mode_fixup(struct drm_atomic_state *state)
369 struct drm_crtc *crtc;
370 struct drm_crtc_state *new_crtc_state;
371 struct drm_connector *connector;
372 struct drm_connector_state *new_conn_state;
376 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
377 if (!new_crtc_state->mode_changed &&
378 !new_crtc_state->connectors_changed)
381 drm_mode_copy(&new_crtc_state->adjusted_mode, &new_crtc_state->mode);
384 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
385 const struct drm_encoder_helper_funcs *funcs;
386 struct drm_encoder *encoder;
388 WARN_ON(!!new_conn_state->best_encoder != !!new_conn_state->crtc);
390 if (!new_conn_state->crtc || !new_conn_state->best_encoder)
394 drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
397 * Each encoder has at most one connector (since we always steal
398 * it away), so we won't call ->mode_fixup twice.
400 encoder = new_conn_state->best_encoder;
401 funcs = encoder->helper_private;
403 ret = drm_bridge_mode_fixup(encoder->bridge, &new_crtc_state->mode,
404 &new_crtc_state->adjusted_mode);
406 DRM_DEBUG_ATOMIC("Bridge fixup failed\n");
410 if (funcs && funcs->atomic_check) {
411 ret = funcs->atomic_check(encoder, new_crtc_state,
414 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] check failed\n",
415 encoder->base.id, encoder->name);
418 } else if (funcs && funcs->mode_fixup) {
419 ret = funcs->mode_fixup(encoder, &new_crtc_state->mode,
420 &new_crtc_state->adjusted_mode);
422 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] fixup failed\n",
423 encoder->base.id, encoder->name);
429 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
430 const struct drm_crtc_helper_funcs *funcs;
432 if (!new_crtc_state->enable)
435 if (!new_crtc_state->mode_changed &&
436 !new_crtc_state->connectors_changed)
439 funcs = crtc->helper_private;
440 if (!funcs->mode_fixup)
443 ret = funcs->mode_fixup(crtc, &new_crtc_state->mode,
444 &new_crtc_state->adjusted_mode);
446 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] fixup failed\n",
447 crtc->base.id, crtc->name);
456 * drm_atomic_helper_check_modeset - validate state object for modeset changes
458 * @state: the driver state object
460 * Check the state object to see if the requested state is physically possible.
461 * This does all the crtc and connector related computations for an atomic
462 * update and adds any additional connectors needed for full modesets. It calls
463 * the various per-object callbacks in the follow order:
465 * 1. &drm_connector_helper_funcs.atomic_best_encoder for determining the new encoder.
466 * 2. &drm_connector_helper_funcs.atomic_check to validate the connector state.
467 * 3. If it's determined a modeset is needed then all connectors on the affected crtc
468 * crtc are added and &drm_connector_helper_funcs.atomic_check is run on them.
469 * 4. &drm_bridge_funcs.mode_fixup is called on all encoder bridges.
470 * 5. &drm_encoder_helper_funcs.atomic_check is called to validate any encoder state.
471 * This function is only called when the encoder will be part of a configured crtc,
472 * it must not be used for implementing connector property validation.
473 * If this function is NULL, &drm_atomic_encoder_helper_funcs.mode_fixup is called
475 * 6. &drm_crtc_helper_funcs.mode_fixup is called last, to fix up the mode with crtc constraints.
477 * &drm_crtc_state.mode_changed is set when the input mode is changed.
478 * &drm_crtc_state.connectors_changed is set when a connector is added or
479 * removed from the crtc. &drm_crtc_state.active_changed is set when
480 * &drm_crtc_state.active changes, which is used for DPMS.
481 * See also: drm_atomic_crtc_needs_modeset()
485 * Drivers which set &drm_crtc_state.mode_changed (e.g. in their
486 * &drm_plane_helper_funcs.atomic_check hooks if a plane update can't be done
487 * without a full modeset) _must_ call this function afterwards after that
488 * change. It is permitted to call this function multiple times for the same
489 * update, e.g. when the &drm_crtc_helper_funcs.atomic_check functions depend
490 * upon the adjusted dotclock for fifo space allocation and watermark
494 * Zero for success or -errno
497 drm_atomic_helper_check_modeset(struct drm_device *dev,
498 struct drm_atomic_state *state)
500 struct drm_crtc *crtc;
501 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
502 struct drm_connector *connector;
503 struct drm_connector_state *old_connector_state, *new_connector_state;
505 unsigned connectors_mask = 0;
507 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
508 bool has_connectors =
509 !!new_crtc_state->connector_mask;
511 if (!drm_mode_equal(&old_crtc_state->mode, &new_crtc_state->mode)) {
512 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] mode changed\n",
513 crtc->base.id, crtc->name);
514 new_crtc_state->mode_changed = true;
517 if (old_crtc_state->enable != new_crtc_state->enable) {
518 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enable changed\n",
519 crtc->base.id, crtc->name);
522 * For clarity this assignment is done here, but
523 * enable == 0 is only true when there are no
524 * connectors and a NULL mode.
526 * The other way around is true as well. enable != 0
527 * iff connectors are attached and a mode is set.
529 new_crtc_state->mode_changed = true;
530 new_crtc_state->connectors_changed = true;
533 if (old_crtc_state->active != new_crtc_state->active) {
534 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active changed\n",
535 crtc->base.id, crtc->name);
536 new_crtc_state->active_changed = true;
539 if (new_crtc_state->enable != has_connectors) {
540 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled/connectors mismatch\n",
541 crtc->base.id, crtc->name);
547 ret = handle_conflicting_encoders(state, false);
551 for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
552 const struct drm_connector_helper_funcs *funcs = connector->helper_private;
555 * This only sets crtc->connectors_changed for routing changes,
556 * drivers must set crtc->connectors_changed themselves when
557 * connector properties need to be updated.
559 ret = update_connector_routing(state, connector,
561 new_connector_state);
564 if (old_connector_state->crtc) {
565 new_crtc_state = drm_atomic_get_new_crtc_state(state,
566 old_connector_state->crtc);
567 if (old_connector_state->link_status !=
568 new_connector_state->link_status)
569 new_crtc_state->connectors_changed = true;
572 if (funcs->atomic_check)
573 ret = funcs->atomic_check(connector, new_connector_state);
577 connectors_mask += BIT(i);
581 * After all the routing has been prepared we need to add in any
582 * connector which is itself unchanged, but who's crtc changes it's
583 * configuration. This must be done before calling mode_fixup in case a
584 * crtc only changed its mode but has the same set of connectors.
586 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
587 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
590 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] needs all connectors, enable: %c, active: %c\n",
591 crtc->base.id, crtc->name,
592 new_crtc_state->enable ? 'y' : 'n',
593 new_crtc_state->active ? 'y' : 'n');
595 ret = drm_atomic_add_affected_connectors(state, crtc);
599 ret = drm_atomic_add_affected_planes(state, crtc);
605 * Iterate over all connectors again, to make sure atomic_check()
606 * has been called on them when a modeset is forced.
608 for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
609 const struct drm_connector_helper_funcs *funcs = connector->helper_private;
611 if (connectors_mask & BIT(i))
614 if (funcs->atomic_check)
615 ret = funcs->atomic_check(connector, new_connector_state);
620 return mode_fixup(state);
622 EXPORT_SYMBOL(drm_atomic_helper_check_modeset);
625 * drm_atomic_helper_check_planes - validate state object for planes changes
627 * @state: the driver state object
629 * Check the state object to see if the requested state is physically possible.
630 * This does all the plane update related checks using by calling into the
631 * &drm_crtc_helper_funcs.atomic_check and &drm_plane_helper_funcs.atomic_check
632 * hooks provided by the driver.
634 * It also sets &drm_crtc_state.planes_changed to indicate that a crtc has
638 * Zero for success or -errno
641 drm_atomic_helper_check_planes(struct drm_device *dev,
642 struct drm_atomic_state *state)
644 struct drm_crtc *crtc;
645 struct drm_crtc_state *new_crtc_state;
646 struct drm_plane *plane;
647 struct drm_plane_state *new_plane_state, *old_plane_state;
650 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
651 const struct drm_plane_helper_funcs *funcs;
653 funcs = plane->helper_private;
655 drm_atomic_helper_plane_changed(state, old_plane_state, new_plane_state, plane);
657 if (!funcs || !funcs->atomic_check)
660 ret = funcs->atomic_check(plane, new_plane_state);
662 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic driver check failed\n",
663 plane->base.id, plane->name);
668 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
669 const struct drm_crtc_helper_funcs *funcs;
671 funcs = crtc->helper_private;
673 if (!funcs || !funcs->atomic_check)
676 ret = funcs->atomic_check(crtc, new_crtc_state);
678 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic driver check failed\n",
679 crtc->base.id, crtc->name);
686 EXPORT_SYMBOL(drm_atomic_helper_check_planes);
689 * drm_atomic_helper_check - validate state object
691 * @state: the driver state object
693 * Check the state object to see if the requested state is physically possible.
694 * Only crtcs and planes have check callbacks, so for any additional (global)
695 * checking that a driver needs it can simply wrap that around this function.
696 * Drivers without such needs can directly use this as their
697 * &drm_mode_config_funcs.atomic_check callback.
699 * This just wraps the two parts of the state checking for planes and modeset
700 * state in the default order: First it calls drm_atomic_helper_check_modeset()
701 * and then drm_atomic_helper_check_planes(). The assumption is that the
702 * @drm_plane_helper_funcs.atomic_check and @drm_crtc_helper_funcs.atomic_check
703 * functions depend upon an updated adjusted_mode.clock to e.g. properly compute
707 * Zero for success or -errno
709 int drm_atomic_helper_check(struct drm_device *dev,
710 struct drm_atomic_state *state)
714 ret = drm_atomic_helper_check_modeset(dev, state);
718 ret = drm_atomic_helper_check_planes(dev, state);
724 EXPORT_SYMBOL(drm_atomic_helper_check);
727 disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state)
729 struct drm_connector *connector;
730 struct drm_connector_state *old_conn_state, *new_conn_state;
731 struct drm_crtc *crtc;
732 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
735 for_each_oldnew_connector_in_state(old_state, connector, old_conn_state, new_conn_state, i) {
736 const struct drm_encoder_helper_funcs *funcs;
737 struct drm_encoder *encoder;
739 /* Shut down everything that's in the changeset and currently
740 * still on. So need to check the old, saved state. */
741 if (!old_conn_state->crtc)
744 old_crtc_state = drm_atomic_get_old_crtc_state(old_state, old_conn_state->crtc);
746 if (!old_crtc_state->active ||
747 !drm_atomic_crtc_needs_modeset(old_conn_state->crtc->state))
750 encoder = old_conn_state->best_encoder;
752 /* We shouldn't get this far if we didn't previously have
753 * an encoder.. but WARN_ON() rather than explode.
755 if (WARN_ON(!encoder))
758 funcs = encoder->helper_private;
760 DRM_DEBUG_ATOMIC("disabling [ENCODER:%d:%s]\n",
761 encoder->base.id, encoder->name);
764 * Each encoder has at most one connector (since we always steal
765 * it away), so we won't call disable hooks twice.
767 drm_bridge_disable(encoder->bridge);
769 /* Right function depends upon target state. */
771 if (new_conn_state->crtc && funcs->prepare)
772 funcs->prepare(encoder);
773 else if (funcs->disable)
774 funcs->disable(encoder);
775 else if (funcs->dpms)
776 funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
779 drm_bridge_post_disable(encoder->bridge);
782 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
783 const struct drm_crtc_helper_funcs *funcs;
785 /* Shut down everything that needs a full modeset. */
786 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
789 if (!old_crtc_state->active)
792 funcs = crtc->helper_private;
794 DRM_DEBUG_ATOMIC("disabling [CRTC:%d:%s]\n",
795 crtc->base.id, crtc->name);
798 /* Right function depends upon target state. */
799 if (new_crtc_state->enable && funcs->prepare)
800 funcs->prepare(crtc);
801 else if (funcs->atomic_disable)
802 funcs->atomic_disable(crtc, old_crtc_state);
803 else if (funcs->disable)
804 funcs->disable(crtc);
806 funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
811 * drm_atomic_helper_update_legacy_modeset_state - update legacy modeset state
813 * @old_state: atomic state object with old state structures
815 * This function updates all the various legacy modeset state pointers in
816 * connectors, encoders and crtcs. It also updates the timestamping constants
817 * used for precise vblank timestamps by calling
818 * drm_calc_timestamping_constants().
820 * Drivers can use this for building their own atomic commit if they don't have
821 * a pure helper-based modeset implementation.
824 drm_atomic_helper_update_legacy_modeset_state(struct drm_device *dev,
825 struct drm_atomic_state *old_state)
827 struct drm_connector *connector;
828 struct drm_connector_state *old_conn_state, *new_conn_state;
829 struct drm_crtc *crtc;
830 struct drm_crtc_state *new_crtc_state;
833 /* clear out existing links and update dpms */
834 for_each_oldnew_connector_in_state(old_state, connector, old_conn_state, new_conn_state, i) {
835 if (connector->encoder) {
836 WARN_ON(!connector->encoder->crtc);
838 connector->encoder->crtc = NULL;
839 connector->encoder = NULL;
842 crtc = new_conn_state->crtc;
843 if ((!crtc && old_conn_state->crtc) ||
844 (crtc && drm_atomic_crtc_needs_modeset(crtc->state))) {
845 struct drm_property *dpms_prop =
846 dev->mode_config.dpms_property;
847 int mode = DRM_MODE_DPMS_OFF;
849 if (crtc && crtc->state->active)
850 mode = DRM_MODE_DPMS_ON;
852 connector->dpms = mode;
853 drm_object_property_set_value(&connector->base,
859 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
860 if (!new_conn_state->crtc)
863 if (WARN_ON(!new_conn_state->best_encoder))
866 connector->encoder = new_conn_state->best_encoder;
867 connector->encoder->crtc = new_conn_state->crtc;
870 /* set legacy state in the crtc structure */
871 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
872 struct drm_plane *primary = crtc->primary;
873 struct drm_plane_state *new_plane_state;
875 crtc->mode = new_crtc_state->mode;
876 crtc->enabled = new_crtc_state->enable;
879 drm_atomic_get_new_plane_state(old_state, primary);
881 if (new_plane_state && new_plane_state->crtc == crtc) {
882 crtc->x = new_plane_state->src_x >> 16;
883 crtc->y = new_plane_state->src_y >> 16;
886 if (new_crtc_state->enable)
887 drm_calc_timestamping_constants(crtc,
888 &new_crtc_state->adjusted_mode);
891 EXPORT_SYMBOL(drm_atomic_helper_update_legacy_modeset_state);
894 crtc_set_mode(struct drm_device *dev, struct drm_atomic_state *old_state)
896 struct drm_crtc *crtc;
897 struct drm_crtc_state *new_crtc_state;
898 struct drm_connector *connector;
899 struct drm_connector_state *new_conn_state;
902 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
903 const struct drm_crtc_helper_funcs *funcs;
905 if (!new_crtc_state->mode_changed)
908 funcs = crtc->helper_private;
910 if (new_crtc_state->enable && funcs->mode_set_nofb) {
911 DRM_DEBUG_ATOMIC("modeset on [CRTC:%d:%s]\n",
912 crtc->base.id, crtc->name);
914 funcs->mode_set_nofb(crtc);
918 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
919 const struct drm_encoder_helper_funcs *funcs;
920 struct drm_encoder *encoder;
921 struct drm_display_mode *mode, *adjusted_mode;
923 if (!new_conn_state->best_encoder)
926 encoder = new_conn_state->best_encoder;
927 funcs = encoder->helper_private;
928 new_crtc_state = new_conn_state->crtc->state;
929 mode = &new_crtc_state->mode;
930 adjusted_mode = &new_crtc_state->adjusted_mode;
932 if (!new_crtc_state->mode_changed)
935 DRM_DEBUG_ATOMIC("modeset on [ENCODER:%d:%s]\n",
936 encoder->base.id, encoder->name);
939 * Each encoder has at most one connector (since we always steal
940 * it away), so we won't call mode_set hooks twice.
942 if (funcs && funcs->atomic_mode_set) {
943 funcs->atomic_mode_set(encoder, new_crtc_state,
945 } else if (funcs && funcs->mode_set) {
946 funcs->mode_set(encoder, mode, adjusted_mode);
949 drm_bridge_mode_set(encoder->bridge, mode, adjusted_mode);
954 * drm_atomic_helper_commit_modeset_disables - modeset commit to disable outputs
956 * @old_state: atomic state object with old state structures
958 * This function shuts down all the outputs that need to be shut down and
959 * prepares them (if required) with the new mode.
961 * For compatibility with legacy crtc helpers this should be called before
962 * drm_atomic_helper_commit_planes(), which is what the default commit function
963 * does. But drivers with different needs can group the modeset commits together
964 * and do the plane commits at the end. This is useful for drivers doing runtime
965 * PM since planes updates then only happen when the CRTC is actually enabled.
967 void drm_atomic_helper_commit_modeset_disables(struct drm_device *dev,
968 struct drm_atomic_state *old_state)
970 disable_outputs(dev, old_state);
972 drm_atomic_helper_update_legacy_modeset_state(dev, old_state);
974 crtc_set_mode(dev, old_state);
976 EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_disables);
979 * drm_atomic_helper_commit_modeset_enables - modeset commit to enable outputs
981 * @old_state: atomic state object with old state structures
983 * This function enables all the outputs with the new configuration which had to
984 * be turned off for the update.
986 * For compatibility with legacy crtc helpers this should be called after
987 * drm_atomic_helper_commit_planes(), which is what the default commit function
988 * does. But drivers with different needs can group the modeset commits together
989 * and do the plane commits at the end. This is useful for drivers doing runtime
990 * PM since planes updates then only happen when the CRTC is actually enabled.
992 void drm_atomic_helper_commit_modeset_enables(struct drm_device *dev,
993 struct drm_atomic_state *old_state)
995 struct drm_crtc *crtc;
996 struct drm_crtc_state *new_crtc_state;
997 struct drm_connector *connector;
998 struct drm_connector_state *new_conn_state;
1001 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
1002 const struct drm_crtc_helper_funcs *funcs;
1004 /* Need to filter out CRTCs where only planes change. */
1005 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
1008 if (!new_crtc_state->active)
1011 funcs = crtc->helper_private;
1013 if (new_crtc_state->enable) {
1014 DRM_DEBUG_ATOMIC("enabling [CRTC:%d:%s]\n",
1015 crtc->base.id, crtc->name);
1018 funcs->enable(crtc);
1020 funcs->commit(crtc);
1024 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
1025 const struct drm_encoder_helper_funcs *funcs;
1026 struct drm_encoder *encoder;
1028 if (!new_conn_state->best_encoder)
1031 if (!new_conn_state->crtc->state->active ||
1032 !drm_atomic_crtc_needs_modeset(new_conn_state->crtc->state))
1035 encoder = new_conn_state->best_encoder;
1036 funcs = encoder->helper_private;
1038 DRM_DEBUG_ATOMIC("enabling [ENCODER:%d:%s]\n",
1039 encoder->base.id, encoder->name);
1042 * Each encoder has at most one connector (since we always steal
1043 * it away), so we won't call enable hooks twice.
1045 drm_bridge_pre_enable(encoder->bridge);
1049 funcs->enable(encoder);
1050 else if (funcs->commit)
1051 funcs->commit(encoder);
1054 drm_bridge_enable(encoder->bridge);
1057 EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_enables);
1060 * drm_atomic_helper_wait_for_fences - wait for fences stashed in plane state
1062 * @state: atomic state object with old state structures
1063 * @pre_swap: If true, do an interruptible wait, and @state is the new state.
1064 * Otherwise @state is the old state.
1066 * For implicit sync, driver should fish the exclusive fence out from the
1067 * incoming fb's and stash it in the drm_plane_state. This is called after
1068 * drm_atomic_helper_swap_state() so it uses the current plane state (and
1069 * just uses the atomic state to find the changed planes)
1071 * Note that @pre_swap is needed since the point where we block for fences moves
1072 * around depending upon whether an atomic commit is blocking or
1073 * non-blocking. For non-blocking commit all waiting needs to happen after
1074 * drm_atomic_helper_swap_state() is called, but for blocking commits we want
1075 * to wait **before** we do anything that can't be easily rolled back. That is
1076 * before we call drm_atomic_helper_swap_state().
1078 * Returns zero if success or < 0 if dma_fence_wait() fails.
1080 int drm_atomic_helper_wait_for_fences(struct drm_device *dev,
1081 struct drm_atomic_state *state,
1084 struct drm_plane *plane;
1085 struct drm_plane_state *new_plane_state;
1088 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
1089 if (!new_plane_state->fence)
1092 WARN_ON(!new_plane_state->fb);
1095 * If waiting for fences pre-swap (ie: nonblock), userspace can
1096 * still interrupt the operation. Instead of blocking until the
1097 * timer expires, make the wait interruptible.
1099 ret = dma_fence_wait(new_plane_state->fence, pre_swap);
1103 dma_fence_put(new_plane_state->fence);
1104 new_plane_state->fence = NULL;
1109 EXPORT_SYMBOL(drm_atomic_helper_wait_for_fences);
1112 * drm_atomic_helper_wait_for_vblanks - wait for vblank on crtcs
1114 * @old_state: atomic state object with old state structures
1116 * Helper to, after atomic commit, wait for vblanks on all effected
1117 * crtcs (ie. before cleaning up old framebuffers using
1118 * drm_atomic_helper_cleanup_planes()). It will only wait on crtcs where the
1119 * framebuffers have actually changed to optimize for the legacy cursor and
1120 * plane update use-case.
1123 drm_atomic_helper_wait_for_vblanks(struct drm_device *dev,
1124 struct drm_atomic_state *old_state)
1126 struct drm_crtc *crtc;
1127 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
1129 unsigned crtc_mask = 0;
1132 * Legacy cursor ioctls are completely unsynced, and userspace
1133 * relies on that (by doing tons of cursor updates).
1135 if (old_state->legacy_cursor_update)
1138 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
1139 if (!new_crtc_state->active || !new_crtc_state->planes_changed)
1142 ret = drm_crtc_vblank_get(crtc);
1146 crtc_mask |= drm_crtc_mask(crtc);
1147 old_state->crtcs[i].last_vblank_count = drm_crtc_vblank_count(crtc);
1150 for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
1151 if (!(crtc_mask & drm_crtc_mask(crtc)))
1154 ret = wait_event_timeout(dev->vblank[i].queue,
1155 old_state->crtcs[i].last_vblank_count !=
1156 drm_crtc_vblank_count(crtc),
1157 msecs_to_jiffies(50));
1159 WARN(!ret, "[CRTC:%d:%s] vblank wait timed out\n",
1160 crtc->base.id, crtc->name);
1162 drm_crtc_vblank_put(crtc);
1165 EXPORT_SYMBOL(drm_atomic_helper_wait_for_vblanks);
1168 * drm_atomic_helper_commit_tail - commit atomic update to hardware
1169 * @old_state: atomic state object with old state structures
1171 * This is the default implementation for the
1172 * &drm_mode_config_helper_funcs.atomic_commit_tail hook.
1174 * Note that the default ordering of how the various stages are called is to
1175 * match the legacy modeset helper library closest. One peculiarity of that is
1176 * that it doesn't mesh well with runtime PM at all.
1178 * For drivers supporting runtime PM the recommended sequence is instead ::
1180 * drm_atomic_helper_commit_modeset_disables(dev, old_state);
1182 * drm_atomic_helper_commit_modeset_enables(dev, old_state);
1184 * drm_atomic_helper_commit_planes(dev, old_state,
1185 * DRM_PLANE_COMMIT_ACTIVE_ONLY);
1187 * for committing the atomic update to hardware. See the kerneldoc entries for
1188 * these three functions for more details.
1190 void drm_atomic_helper_commit_tail(struct drm_atomic_state *old_state)
1192 struct drm_device *dev = old_state->dev;
1194 drm_atomic_helper_commit_modeset_disables(dev, old_state);
1196 drm_atomic_helper_commit_planes(dev, old_state, 0);
1198 drm_atomic_helper_commit_modeset_enables(dev, old_state);
1200 drm_atomic_helper_commit_hw_done(old_state);
1202 drm_atomic_helper_wait_for_vblanks(dev, old_state);
1204 drm_atomic_helper_cleanup_planes(dev, old_state);
1206 EXPORT_SYMBOL(drm_atomic_helper_commit_tail);
1208 static void commit_tail(struct drm_atomic_state *old_state)
1210 struct drm_device *dev = old_state->dev;
1211 const struct drm_mode_config_helper_funcs *funcs;
1213 funcs = dev->mode_config.helper_private;
1215 drm_atomic_helper_wait_for_fences(dev, old_state, false);
1217 drm_atomic_helper_wait_for_dependencies(old_state);
1219 if (funcs && funcs->atomic_commit_tail)
1220 funcs->atomic_commit_tail(old_state);
1222 drm_atomic_helper_commit_tail(old_state);
1224 drm_atomic_helper_commit_cleanup_done(old_state);
1226 drm_atomic_state_put(old_state);
1229 static void commit_work(struct work_struct *work)
1231 struct drm_atomic_state *state = container_of(work,
1232 struct drm_atomic_state,
1238 * drm_atomic_helper_commit - commit validated state object
1240 * @state: the driver state object
1241 * @nonblock: whether nonblocking behavior is requested.
1243 * This function commits a with drm_atomic_helper_check() pre-validated state
1244 * object. This can still fail when e.g. the framebuffer reservation fails. This
1245 * function implements nonblocking commits, using
1246 * drm_atomic_helper_setup_commit() and related functions.
1248 * Committing the actual hardware state is done through the
1249 * &drm_mode_config_helper_funcs.atomic_commit_tail callback, or it's default
1250 * implementation drm_atomic_helper_commit_tail().
1253 * Zero for success or -errno.
1255 int drm_atomic_helper_commit(struct drm_device *dev,
1256 struct drm_atomic_state *state,
1261 ret = drm_atomic_helper_setup_commit(state, nonblock);
1265 INIT_WORK(&state->commit_work, commit_work);
1267 ret = drm_atomic_helper_prepare_planes(dev, state);
1272 ret = drm_atomic_helper_wait_for_fences(dev, state, true);
1274 drm_atomic_helper_cleanup_planes(dev, state);
1280 * This is the point of no return - everything below never fails except
1281 * when the hw goes bonghits. Which means we can commit the new state on
1282 * the software side now.
1285 drm_atomic_helper_swap_state(state, true);
1288 * Everything below can be run asynchronously without the need to grab
1289 * any modeset locks at all under one condition: It must be guaranteed
1290 * that the asynchronous work has either been cancelled (if the driver
1291 * supports it, which at least requires that the framebuffers get
1292 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
1293 * before the new state gets committed on the software side with
1294 * drm_atomic_helper_swap_state().
1296 * This scheme allows new atomic state updates to be prepared and
1297 * checked in parallel to the asynchronous completion of the previous
1298 * update. Which is important since compositors need to figure out the
1299 * composition of the next frame right after having submitted the
1302 * NOTE: Commit work has multiple phases, first hardware commit, then
1303 * cleanup. We want them to overlap, hence need system_unbound_wq to
1304 * make sure work items don't artifically stall on each another.
1307 drm_atomic_state_get(state);
1309 queue_work(system_unbound_wq, &state->commit_work);
1315 EXPORT_SYMBOL(drm_atomic_helper_commit);
1318 * DOC: implementing nonblocking commit
1320 * Nonblocking atomic commits have to be implemented in the following sequence:
1322 * 1. Run drm_atomic_helper_prepare_planes() first. This is the only function
1323 * which commit needs to call which can fail, so we want to run it first and
1326 * 2. Synchronize with any outstanding nonblocking commit worker threads which
1327 * might be affected the new state update. This can be done by either cancelling
1328 * or flushing the work items, depending upon whether the driver can deal with
1329 * cancelled updates. Note that it is important to ensure that the framebuffer
1330 * cleanup is still done when cancelling.
1332 * Asynchronous workers need to have sufficient parallelism to be able to run
1333 * different atomic commits on different CRTCs in parallel. The simplest way to
1334 * achive this is by running them on the &system_unbound_wq work queue. Note
1335 * that drivers are not required to split up atomic commits and run an
1336 * individual commit in parallel - userspace is supposed to do that if it cares.
1337 * But it might be beneficial to do that for modesets, since those necessarily
1338 * must be done as one global operation, and enabling or disabling a CRTC can
1339 * take a long time. But even that is not required.
1341 * 3. The software state is updated synchronously with
1342 * drm_atomic_helper_swap_state(). Doing this under the protection of all modeset
1343 * locks means concurrent callers never see inconsistent state. And doing this
1344 * while it's guaranteed that no relevant nonblocking worker runs means that
1345 * nonblocking workers do not need grab any locks. Actually they must not grab
1346 * locks, for otherwise the work flushing will deadlock.
1348 * 4. Schedule a work item to do all subsequent steps, using the split-out
1349 * commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and
1350 * then cleaning up the framebuffers after the old framebuffer is no longer
1353 * The above scheme is implemented in the atomic helper libraries in
1354 * drm_atomic_helper_commit() using a bunch of helper functions. See
1355 * drm_atomic_helper_setup_commit() for a starting point.
1358 static int stall_checks(struct drm_crtc *crtc, bool nonblock)
1360 struct drm_crtc_commit *commit, *stall_commit = NULL;
1361 bool completed = true;
1365 spin_lock(&crtc->commit_lock);
1367 list_for_each_entry(commit, &crtc->commit_list, commit_entry) {
1369 completed = try_wait_for_completion(&commit->flip_done);
1370 /* Userspace is not allowed to get ahead of the previous
1371 * commit with nonblocking ones. */
1372 if (!completed && nonblock) {
1373 spin_unlock(&crtc->commit_lock);
1376 } else if (i == 1) {
1377 stall_commit = commit;
1378 drm_crtc_commit_get(stall_commit);
1384 spin_unlock(&crtc->commit_lock);
1389 /* We don't want to let commits get ahead of cleanup work too much,
1390 * stalling on 2nd previous commit means triple-buffer won't ever stall.
1392 ret = wait_for_completion_interruptible_timeout(&stall_commit->cleanup_done,
1395 DRM_ERROR("[CRTC:%d:%s] cleanup_done timed out\n",
1396 crtc->base.id, crtc->name);
1398 drm_crtc_commit_put(stall_commit);
1400 return ret < 0 ? ret : 0;
1403 static void release_crtc_commit(struct completion *completion)
1405 struct drm_crtc_commit *commit = container_of(completion,
1409 drm_crtc_commit_put(commit);
1413 * drm_atomic_helper_setup_commit - setup possibly nonblocking commit
1414 * @state: new modeset state to be committed
1415 * @nonblock: whether nonblocking behavior is requested.
1417 * This function prepares @state to be used by the atomic helper's support for
1418 * nonblocking commits. Drivers using the nonblocking commit infrastructure
1419 * should always call this function from their
1420 * &drm_mode_config_funcs.atomic_commit hook.
1422 * To be able to use this support drivers need to use a few more helper
1423 * functions. drm_atomic_helper_wait_for_dependencies() must be called before
1424 * actually committing the hardware state, and for nonblocking commits this call
1425 * must be placed in the async worker. See also drm_atomic_helper_swap_state()
1426 * and it's stall parameter, for when a driver's commit hooks look at the
1427 * &drm_crtc.state, &drm_plane.state or &drm_connector.state pointer directly.
1429 * Completion of the hardware commit step must be signalled using
1430 * drm_atomic_helper_commit_hw_done(). After this step the driver is not allowed
1431 * to read or change any permanent software or hardware modeset state. The only
1432 * exception is state protected by other means than &drm_modeset_lock locks.
1433 * Only the free standing @state with pointers to the old state structures can
1434 * be inspected, e.g. to clean up old buffers using
1435 * drm_atomic_helper_cleanup_planes().
1437 * At the very end, before cleaning up @state drivers must call
1438 * drm_atomic_helper_commit_cleanup_done().
1440 * This is all implemented by in drm_atomic_helper_commit(), giving drivers a
1441 * complete and esay-to-use default implementation of the atomic_commit() hook.
1443 * The tracking of asynchronously executed and still pending commits is done
1444 * using the core structure &drm_crtc_commit.
1446 * By default there's no need to clean up resources allocated by this function
1447 * explicitly: drm_atomic_state_default_clear() will take care of that
1452 * 0 on success. -EBUSY when userspace schedules nonblocking commits too fast,
1453 * -ENOMEM on allocation failures and -EINTR when a signal is pending.
1455 int drm_atomic_helper_setup_commit(struct drm_atomic_state *state,
1458 struct drm_crtc *crtc;
1459 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
1460 struct drm_crtc_commit *commit;
1463 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
1464 commit = kzalloc(sizeof(*commit), GFP_KERNEL);
1468 init_completion(&commit->flip_done);
1469 init_completion(&commit->hw_done);
1470 init_completion(&commit->cleanup_done);
1471 INIT_LIST_HEAD(&commit->commit_entry);
1472 kref_init(&commit->ref);
1473 commit->crtc = crtc;
1475 state->crtcs[i].commit = commit;
1477 ret = stall_checks(crtc, nonblock);
1481 /* Drivers only send out events when at least either current or
1482 * new CRTC state is active. Complete right away if everything
1484 if (!old_crtc_state->active && !new_crtc_state->active) {
1485 complete_all(&commit->flip_done);
1489 /* Legacy cursor updates are fully unsynced. */
1490 if (state->legacy_cursor_update) {
1491 complete_all(&commit->flip_done);
1495 if (!new_crtc_state->event) {
1496 commit->event = kzalloc(sizeof(*commit->event),
1501 new_crtc_state->event = commit->event;
1504 new_crtc_state->event->base.completion = &commit->flip_done;
1505 new_crtc_state->event->base.completion_release = release_crtc_commit;
1506 drm_crtc_commit_get(commit);
1511 EXPORT_SYMBOL(drm_atomic_helper_setup_commit);
1514 static struct drm_crtc_commit *preceeding_commit(struct drm_crtc *crtc)
1516 struct drm_crtc_commit *commit;
1519 list_for_each_entry(commit, &crtc->commit_list, commit_entry) {
1520 /* skip the first entry, that's the current commit */
1530 * drm_atomic_helper_wait_for_dependencies - wait for required preceeding commits
1531 * @old_state: atomic state object with old state structures
1533 * This function waits for all preceeding commits that touch the same CRTC as
1534 * @old_state to both be committed to the hardware (as signalled by
1535 * drm_atomic_helper_commit_hw_done) and executed by the hardware (as signalled
1536 * by calling drm_crtc_vblank_send_event() on the &drm_crtc_state.event).
1538 * This is part of the atomic helper support for nonblocking commits, see
1539 * drm_atomic_helper_setup_commit() for an overview.
1541 void drm_atomic_helper_wait_for_dependencies(struct drm_atomic_state *old_state)
1543 struct drm_crtc *crtc;
1544 struct drm_crtc_state *new_crtc_state;
1545 struct drm_crtc_commit *commit;
1549 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
1550 spin_lock(&crtc->commit_lock);
1551 commit = preceeding_commit(crtc);
1553 drm_crtc_commit_get(commit);
1554 spin_unlock(&crtc->commit_lock);
1559 ret = wait_for_completion_timeout(&commit->hw_done,
1562 DRM_ERROR("[CRTC:%d:%s] hw_done timed out\n",
1563 crtc->base.id, crtc->name);
1565 /* Currently no support for overwriting flips, hence
1566 * stall for previous one to execute completely. */
1567 ret = wait_for_completion_timeout(&commit->flip_done,
1570 DRM_ERROR("[CRTC:%d:%s] flip_done timed out\n",
1571 crtc->base.id, crtc->name);
1573 drm_crtc_commit_put(commit);
1576 EXPORT_SYMBOL(drm_atomic_helper_wait_for_dependencies);
1579 * drm_atomic_helper_commit_hw_done - setup possible nonblocking commit
1580 * @old_state: atomic state object with old state structures
1582 * This function is used to signal completion of the hardware commit step. After
1583 * this step the driver is not allowed to read or change any permanent software
1584 * or hardware modeset state. The only exception is state protected by other
1585 * means than &drm_modeset_lock locks.
1587 * Drivers should try to postpone any expensive or delayed cleanup work after
1588 * this function is called.
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_hw_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;
1600 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
1601 commit = old_state->crtcs[i].commit;
1605 /* backend must have consumed any event by now */
1606 WARN_ON(new_crtc_state->event);
1607 spin_lock(&crtc->commit_lock);
1608 complete_all(&commit->hw_done);
1609 spin_unlock(&crtc->commit_lock);
1612 EXPORT_SYMBOL(drm_atomic_helper_commit_hw_done);
1615 * drm_atomic_helper_commit_cleanup_done - signal completion of commit
1616 * @old_state: atomic state object with old state structures
1618 * This signals completion of the atomic update @old_state, including any
1619 * cleanup work. If used, it must be called right before calling
1620 * drm_atomic_state_put().
1622 * This is part of the atomic helper support for nonblocking commits, see
1623 * drm_atomic_helper_setup_commit() for an overview.
1625 void drm_atomic_helper_commit_cleanup_done(struct drm_atomic_state *old_state)
1627 struct drm_crtc *crtc;
1628 struct drm_crtc_state *new_crtc_state;
1629 struct drm_crtc_commit *commit;
1633 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
1634 commit = old_state->crtcs[i].commit;
1635 if (WARN_ON(!commit))
1638 spin_lock(&crtc->commit_lock);
1639 complete_all(&commit->cleanup_done);
1640 WARN_ON(!try_wait_for_completion(&commit->hw_done));
1642 /* commit_list borrows our reference, need to remove before we
1643 * clean up our drm_atomic_state. But only after it actually
1644 * completed, otherwise subsequent commits won't stall properly. */
1645 if (try_wait_for_completion(&commit->flip_done))
1648 spin_unlock(&crtc->commit_lock);
1650 /* We must wait for the vblank event to signal our completion
1651 * before releasing our reference, since the vblank work does
1652 * not hold a reference of its own. */
1653 ret = wait_for_completion_timeout(&commit->flip_done,
1656 DRM_ERROR("[CRTC:%d:%s] flip_done timed out\n",
1657 crtc->base.id, crtc->name);
1659 spin_lock(&crtc->commit_lock);
1661 list_del(&commit->commit_entry);
1662 spin_unlock(&crtc->commit_lock);
1665 EXPORT_SYMBOL(drm_atomic_helper_commit_cleanup_done);
1668 * drm_atomic_helper_prepare_planes - prepare plane resources before commit
1670 * @state: atomic state object with new state structures
1672 * This function prepares plane state, specifically framebuffers, for the new
1673 * configuration, by calling &drm_plane_helper_funcs.prepare_fb. If any failure
1674 * is encountered this function will call &drm_plane_helper_funcs.cleanup_fb on
1675 * any already successfully prepared framebuffer.
1678 * 0 on success, negative error code on failure.
1680 int drm_atomic_helper_prepare_planes(struct drm_device *dev,
1681 struct drm_atomic_state *state)
1683 struct drm_plane *plane;
1684 struct drm_plane_state *new_plane_state;
1687 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
1688 const struct drm_plane_helper_funcs *funcs;
1690 funcs = plane->helper_private;
1692 if (funcs->prepare_fb) {
1693 ret = funcs->prepare_fb(plane, new_plane_state);
1702 for_each_new_plane_in_state(state, plane, new_plane_state, j) {
1703 const struct drm_plane_helper_funcs *funcs;
1708 funcs = plane->helper_private;
1710 if (funcs->cleanup_fb)
1711 funcs->cleanup_fb(plane, new_plane_state);
1716 EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
1718 static bool plane_crtc_active(const struct drm_plane_state *state)
1720 return state->crtc && state->crtc->state->active;
1724 * drm_atomic_helper_commit_planes - commit plane state
1726 * @old_state: atomic state object with old state structures
1727 * @flags: flags for committing plane state
1729 * This function commits the new plane state using the plane and atomic helper
1730 * functions for planes and crtcs. It assumes that the atomic state has already
1731 * been pushed into the relevant object state pointers, since this step can no
1734 * It still requires the global state object @old_state to know which planes and
1735 * crtcs need to be updated though.
1737 * Note that this function does all plane updates across all CRTCs in one step.
1738 * If the hardware can't support this approach look at
1739 * drm_atomic_helper_commit_planes_on_crtc() instead.
1741 * Plane parameters can be updated by applications while the associated CRTC is
1742 * disabled. The DRM/KMS core will store the parameters in the plane state,
1743 * which will be available to the driver when the CRTC is turned on. As a result
1744 * most drivers don't need to be immediately notified of plane updates for a
1747 * Unless otherwise needed, drivers are advised to set the ACTIVE_ONLY flag in
1748 * @flags in order not to receive plane update notifications related to a
1749 * disabled CRTC. This avoids the need to manually ignore plane updates in
1750 * driver code when the driver and/or hardware can't or just don't need to deal
1751 * with updates on disabled CRTCs, for example when supporting runtime PM.
1753 * Drivers may set the NO_DISABLE_AFTER_MODESET flag in @flags if the relevant
1754 * display controllers require to disable a CRTC's planes when the CRTC is
1755 * disabled. This function would skip the &drm_plane_helper_funcs.atomic_disable
1756 * call for a plane if the CRTC of the old plane state needs a modesetting
1757 * operation. Of course, the drivers need to disable the planes in their CRTC
1758 * disable callbacks since no one else would do that.
1760 * The drm_atomic_helper_commit() default implementation doesn't set the
1761 * ACTIVE_ONLY flag to most closely match the behaviour of the legacy helpers.
1762 * This should not be copied blindly by drivers.
1764 void drm_atomic_helper_commit_planes(struct drm_device *dev,
1765 struct drm_atomic_state *old_state,
1768 struct drm_crtc *crtc;
1769 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
1770 struct drm_plane *plane;
1771 struct drm_plane_state *old_plane_state, *new_plane_state;
1773 bool active_only = flags & DRM_PLANE_COMMIT_ACTIVE_ONLY;
1774 bool no_disable = flags & DRM_PLANE_COMMIT_NO_DISABLE_AFTER_MODESET;
1776 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
1777 const struct drm_crtc_helper_funcs *funcs;
1779 funcs = crtc->helper_private;
1781 if (!funcs || !funcs->atomic_begin)
1784 if (active_only && !new_crtc_state->active)
1787 funcs->atomic_begin(crtc, old_crtc_state);
1790 for_each_oldnew_plane_in_state(old_state, plane, old_plane_state, new_plane_state, i) {
1791 const struct drm_plane_helper_funcs *funcs;
1794 funcs = plane->helper_private;
1799 disabling = drm_atomic_plane_disabling(old_plane_state,
1804 * Skip planes related to inactive CRTCs. If the plane
1805 * is enabled use the state of the current CRTC. If the
1806 * plane is being disabled use the state of the old
1807 * CRTC to avoid skipping planes being disabled on an
1810 if (!disabling && !plane_crtc_active(new_plane_state))
1812 if (disabling && !plane_crtc_active(old_plane_state))
1817 * Special-case disabling the plane if drivers support it.
1819 if (disabling && funcs->atomic_disable) {
1820 struct drm_crtc_state *crtc_state;
1822 crtc_state = old_plane_state->crtc->state;
1824 if (drm_atomic_crtc_needs_modeset(crtc_state) &&
1828 funcs->atomic_disable(plane, old_plane_state);
1829 } else if (new_plane_state->crtc || disabling) {
1830 funcs->atomic_update(plane, old_plane_state);
1834 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
1835 const struct drm_crtc_helper_funcs *funcs;
1837 funcs = crtc->helper_private;
1839 if (!funcs || !funcs->atomic_flush)
1842 if (active_only && !new_crtc_state->active)
1845 funcs->atomic_flush(crtc, old_crtc_state);
1848 EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
1851 * drm_atomic_helper_commit_planes_on_crtc - commit plane state for a crtc
1852 * @old_crtc_state: atomic state object with the old crtc state
1854 * This function commits the new plane state using the plane and atomic helper
1855 * functions for planes on the specific crtc. It assumes that the atomic state
1856 * has already been pushed into the relevant object state pointers, since this
1857 * step can no longer fail.
1859 * This function is useful when plane updates should be done crtc-by-crtc
1860 * instead of one global step like drm_atomic_helper_commit_planes() does.
1862 * This function can only be savely used when planes are not allowed to move
1863 * between different CRTCs because this function doesn't handle inter-CRTC
1864 * depencies. Callers need to ensure that either no such depencies exist,
1865 * resolve them through ordering of commit calls or through some other means.
1868 drm_atomic_helper_commit_planes_on_crtc(struct drm_crtc_state *old_crtc_state)
1870 const struct drm_crtc_helper_funcs *crtc_funcs;
1871 struct drm_crtc *crtc = old_crtc_state->crtc;
1872 struct drm_atomic_state *old_state = old_crtc_state->state;
1873 struct drm_plane *plane;
1874 unsigned plane_mask;
1876 plane_mask = old_crtc_state->plane_mask;
1877 plane_mask |= crtc->state->plane_mask;
1879 crtc_funcs = crtc->helper_private;
1880 if (crtc_funcs && crtc_funcs->atomic_begin)
1881 crtc_funcs->atomic_begin(crtc, old_crtc_state);
1883 drm_for_each_plane_mask(plane, crtc->dev, plane_mask) {
1884 struct drm_plane_state *old_plane_state =
1885 drm_atomic_get_old_plane_state(old_state, plane);
1886 const struct drm_plane_helper_funcs *plane_funcs;
1888 plane_funcs = plane->helper_private;
1890 if (!old_plane_state || !plane_funcs)
1893 WARN_ON(plane->state->crtc && plane->state->crtc != crtc);
1895 if (drm_atomic_plane_disabling(old_plane_state, plane->state) &&
1896 plane_funcs->atomic_disable)
1897 plane_funcs->atomic_disable(plane, old_plane_state);
1898 else if (plane->state->crtc ||
1899 drm_atomic_plane_disabling(old_plane_state, plane->state))
1900 plane_funcs->atomic_update(plane, old_plane_state);
1903 if (crtc_funcs && crtc_funcs->atomic_flush)
1904 crtc_funcs->atomic_flush(crtc, old_crtc_state);
1906 EXPORT_SYMBOL(drm_atomic_helper_commit_planes_on_crtc);
1909 * drm_atomic_helper_disable_planes_on_crtc - helper to disable CRTC's planes
1910 * @old_crtc_state: atomic state object with the old CRTC state
1911 * @atomic: if set, synchronize with CRTC's atomic_begin/flush hooks
1913 * Disables all planes associated with the given CRTC. This can be
1914 * used for instance in the CRTC helper atomic_disable callback to disable
1917 * If the atomic-parameter is set the function calls the CRTC's
1918 * atomic_begin hook before and atomic_flush hook after disabling the
1921 * It is a bug to call this function without having implemented the
1922 * &drm_plane_helper_funcs.atomic_disable plane hook.
1925 drm_atomic_helper_disable_planes_on_crtc(struct drm_crtc_state *old_crtc_state,
1928 struct drm_crtc *crtc = old_crtc_state->crtc;
1929 const struct drm_crtc_helper_funcs *crtc_funcs =
1930 crtc->helper_private;
1931 struct drm_plane *plane;
1933 if (atomic && crtc_funcs && crtc_funcs->atomic_begin)
1934 crtc_funcs->atomic_begin(crtc, NULL);
1936 drm_atomic_crtc_state_for_each_plane(plane, old_crtc_state) {
1937 const struct drm_plane_helper_funcs *plane_funcs =
1938 plane->helper_private;
1943 WARN_ON(!plane_funcs->atomic_disable);
1944 if (plane_funcs->atomic_disable)
1945 plane_funcs->atomic_disable(plane, NULL);
1948 if (atomic && crtc_funcs && crtc_funcs->atomic_flush)
1949 crtc_funcs->atomic_flush(crtc, NULL);
1951 EXPORT_SYMBOL(drm_atomic_helper_disable_planes_on_crtc);
1954 * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
1956 * @old_state: atomic state object with old state structures
1958 * This function cleans up plane state, specifically framebuffers, from the old
1959 * configuration. Hence the old configuration must be perserved in @old_state to
1960 * be able to call this function.
1962 * This function must also be called on the new state when the atomic update
1963 * fails at any point after calling drm_atomic_helper_prepare_planes().
1965 void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
1966 struct drm_atomic_state *old_state)
1968 struct drm_plane *plane;
1969 struct drm_plane_state *old_plane_state, *new_plane_state;
1972 for_each_oldnew_plane_in_state(old_state, plane, old_plane_state, new_plane_state, i) {
1973 const struct drm_plane_helper_funcs *funcs;
1974 struct drm_plane_state *plane_state;
1977 * This might be called before swapping when commit is aborted,
1978 * in which case we have to cleanup the new state.
1980 if (old_plane_state == plane->state)
1981 plane_state = new_plane_state;
1983 plane_state = old_plane_state;
1985 funcs = plane->helper_private;
1987 if (funcs->cleanup_fb)
1988 funcs->cleanup_fb(plane, plane_state);
1991 EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
1994 * drm_atomic_helper_swap_state - store atomic state into current sw state
1995 * @state: atomic state
1996 * @stall: stall for proceeding commits
1998 * This function stores the atomic state into the current state pointers in all
1999 * driver objects. It should be called after all failing steps have been done
2000 * and succeeded, but before the actual hardware state is committed.
2002 * For cleanup and error recovery the current state for all changed objects will
2003 * be swaped into @state.
2005 * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
2007 * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
2009 * 2. Do any other steps that might fail.
2011 * 3. Put the staged state into the current state pointers with this function.
2013 * 4. Actually commit the hardware state.
2015 * 5. Call drm_atomic_helper_cleanup_planes() with @state, which since step 3
2016 * contains the old state. Also do any other cleanup required with that state.
2018 * @stall must be set when nonblocking commits for this driver directly access
2019 * the &drm_plane.state, &drm_crtc.state or &drm_connector.state pointer. With
2020 * the current atomic helpers this is almost always the case, since the helpers
2021 * don't pass the right state structures to the callbacks.
2023 void drm_atomic_helper_swap_state(struct drm_atomic_state *state,
2028 struct drm_connector *connector;
2029 struct drm_connector_state *old_conn_state, *new_conn_state;
2030 struct drm_crtc *crtc;
2031 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
2032 struct drm_plane *plane;
2033 struct drm_plane_state *old_plane_state, *new_plane_state;
2034 struct drm_crtc_commit *commit;
2035 void *obj, *obj_state;
2036 const struct drm_private_state_funcs *funcs;
2039 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
2040 spin_lock(&crtc->commit_lock);
2041 commit = list_first_entry_or_null(&crtc->commit_list,
2042 struct drm_crtc_commit, commit_entry);
2044 drm_crtc_commit_get(commit);
2045 spin_unlock(&crtc->commit_lock);
2050 ret = wait_for_completion_timeout(&commit->hw_done,
2053 DRM_ERROR("[CRTC:%d:%s] hw_done timed out\n",
2054 crtc->base.id, crtc->name);
2055 drm_crtc_commit_put(commit);
2059 for_each_oldnew_connector_in_state(state, connector, old_conn_state, new_conn_state, i) {
2060 WARN_ON(connector->state != old_conn_state);
2062 old_conn_state->state = state;
2063 new_conn_state->state = NULL;
2065 state->connectors[i].state = old_conn_state;
2066 connector->state = new_conn_state;
2069 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
2070 WARN_ON(crtc->state != old_crtc_state);
2072 old_crtc_state->state = state;
2073 new_crtc_state->state = NULL;
2075 state->crtcs[i].state = old_crtc_state;
2076 crtc->state = new_crtc_state;
2078 if (state->crtcs[i].commit) {
2079 spin_lock(&crtc->commit_lock);
2080 list_add(&state->crtcs[i].commit->commit_entry,
2081 &crtc->commit_list);
2082 spin_unlock(&crtc->commit_lock);
2084 state->crtcs[i].commit->event = NULL;
2088 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
2089 WARN_ON(plane->state != old_plane_state);
2091 old_plane_state->state = state;
2092 new_plane_state->state = NULL;
2094 state->planes[i].state = old_plane_state;
2095 plane->state = new_plane_state;
2098 __for_each_private_obj(state, obj, obj_state, i, funcs)
2099 funcs->swap_state(obj, &state->private_objs[i].obj_state);
2101 EXPORT_SYMBOL(drm_atomic_helper_swap_state);
2104 * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
2105 * @plane: plane object to update
2106 * @crtc: owning CRTC of owning plane
2107 * @fb: framebuffer to flip onto plane
2108 * @crtc_x: x offset of primary plane on crtc
2109 * @crtc_y: y offset of primary plane on crtc
2110 * @crtc_w: width of primary plane rectangle on crtc
2111 * @crtc_h: height of primary plane rectangle on crtc
2112 * @src_x: x offset of @fb for panning
2113 * @src_y: y offset of @fb for panning
2114 * @src_w: width of source rectangle in @fb
2115 * @src_h: height of source rectangle in @fb
2116 * @ctx: lock acquire context
2118 * Provides a default plane update handler using the atomic driver interface.
2121 * Zero on success, error code on failure
2123 int drm_atomic_helper_update_plane(struct drm_plane *plane,
2124 struct drm_crtc *crtc,
2125 struct drm_framebuffer *fb,
2126 int crtc_x, int crtc_y,
2127 unsigned int crtc_w, unsigned int crtc_h,
2128 uint32_t src_x, uint32_t src_y,
2129 uint32_t src_w, uint32_t src_h,
2130 struct drm_modeset_acquire_ctx *ctx)
2132 struct drm_atomic_state *state;
2133 struct drm_plane_state *plane_state;
2136 state = drm_atomic_state_alloc(plane->dev);
2140 state->acquire_ctx = ctx;
2141 plane_state = drm_atomic_get_plane_state(state, plane);
2142 if (IS_ERR(plane_state)) {
2143 ret = PTR_ERR(plane_state);
2147 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
2150 drm_atomic_set_fb_for_plane(plane_state, fb);
2151 plane_state->crtc_x = crtc_x;
2152 plane_state->crtc_y = crtc_y;
2153 plane_state->crtc_w = crtc_w;
2154 plane_state->crtc_h = crtc_h;
2155 plane_state->src_x = src_x;
2156 plane_state->src_y = src_y;
2157 plane_state->src_w = src_w;
2158 plane_state->src_h = src_h;
2160 if (plane == crtc->cursor)
2161 state->legacy_cursor_update = true;
2163 ret = drm_atomic_commit(state);
2165 drm_atomic_state_put(state);
2168 EXPORT_SYMBOL(drm_atomic_helper_update_plane);
2171 * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
2172 * @plane: plane to disable
2173 * @ctx: lock acquire context
2175 * Provides a default plane disable handler using the atomic driver interface.
2178 * Zero on success, error code on failure
2180 int drm_atomic_helper_disable_plane(struct drm_plane *plane,
2181 struct drm_modeset_acquire_ctx *ctx)
2183 struct drm_atomic_state *state;
2184 struct drm_plane_state *plane_state;
2187 state = drm_atomic_state_alloc(plane->dev);
2191 state->acquire_ctx = ctx;
2192 plane_state = drm_atomic_get_plane_state(state, plane);
2193 if (IS_ERR(plane_state)) {
2194 ret = PTR_ERR(plane_state);
2198 if (plane_state->crtc && (plane == plane->crtc->cursor))
2199 plane_state->state->legacy_cursor_update = true;
2201 ret = __drm_atomic_helper_disable_plane(plane, plane_state);
2205 ret = drm_atomic_commit(state);
2207 drm_atomic_state_put(state);
2210 EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
2212 /* just used from fb-helper and atomic-helper: */
2213 int __drm_atomic_helper_disable_plane(struct drm_plane *plane,
2214 struct drm_plane_state *plane_state)
2218 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
2222 drm_atomic_set_fb_for_plane(plane_state, NULL);
2223 plane_state->crtc_x = 0;
2224 plane_state->crtc_y = 0;
2225 plane_state->crtc_w = 0;
2226 plane_state->crtc_h = 0;
2227 plane_state->src_x = 0;
2228 plane_state->src_y = 0;
2229 plane_state->src_w = 0;
2230 plane_state->src_h = 0;
2235 static int update_output_state(struct drm_atomic_state *state,
2236 struct drm_mode_set *set)
2238 struct drm_device *dev = set->crtc->dev;
2239 struct drm_crtc *crtc;
2240 struct drm_crtc_state *new_crtc_state;
2241 struct drm_connector *connector;
2242 struct drm_connector_state *new_conn_state;
2245 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
2246 state->acquire_ctx);
2250 /* First disable all connectors on the target crtc. */
2251 ret = drm_atomic_add_affected_connectors(state, set->crtc);
2255 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
2256 if (new_conn_state->crtc == set->crtc) {
2257 ret = drm_atomic_set_crtc_for_connector(new_conn_state,
2262 /* Make sure legacy setCrtc always re-trains */
2263 new_conn_state->link_status = DRM_LINK_STATUS_GOOD;
2267 /* Then set all connectors from set->connectors on the target crtc */
2268 for (i = 0; i < set->num_connectors; i++) {
2269 new_conn_state = drm_atomic_get_connector_state(state,
2270 set->connectors[i]);
2271 if (IS_ERR(new_conn_state))
2272 return PTR_ERR(new_conn_state);
2274 ret = drm_atomic_set_crtc_for_connector(new_conn_state,
2280 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
2281 /* Don't update ->enable for the CRTC in the set_config request,
2282 * since a mismatch would indicate a bug in the upper layers.
2283 * The actual modeset code later on will catch any
2284 * inconsistencies here. */
2285 if (crtc == set->crtc)
2288 if (!new_crtc_state->connector_mask) {
2289 ret = drm_atomic_set_mode_prop_for_crtc(new_crtc_state,
2294 new_crtc_state->active = false;
2302 * drm_atomic_helper_set_config - set a new config from userspace
2303 * @set: mode set configuration
2304 * @ctx: lock acquisition context
2306 * Provides a default crtc set_config handler using the atomic driver interface.
2308 * NOTE: For backwards compatibility with old userspace this automatically
2309 * resets the "link-status" property to GOOD, to force any link
2310 * re-training. The SETCRTC ioctl does not define whether an update does
2311 * need a full modeset or just a plane update, hence we're allowed to do
2312 * that. See also drm_mode_connector_set_link_status_property().
2315 * Returns 0 on success, negative errno numbers on failure.
2317 int drm_atomic_helper_set_config(struct drm_mode_set *set,
2318 struct drm_modeset_acquire_ctx *ctx)
2320 struct drm_atomic_state *state;
2321 struct drm_crtc *crtc = set->crtc;
2324 state = drm_atomic_state_alloc(crtc->dev);
2328 state->acquire_ctx = ctx;
2329 ret = __drm_atomic_helper_set_config(set, state);
2333 ret = handle_conflicting_encoders(state, true);
2337 ret = drm_atomic_commit(state);
2340 drm_atomic_state_put(state);
2343 EXPORT_SYMBOL(drm_atomic_helper_set_config);
2345 /* just used from fb-helper and atomic-helper: */
2346 int __drm_atomic_helper_set_config(struct drm_mode_set *set,
2347 struct drm_atomic_state *state)
2349 struct drm_crtc_state *crtc_state;
2350 struct drm_plane_state *primary_state;
2351 struct drm_crtc *crtc = set->crtc;
2352 int hdisplay, vdisplay;
2355 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2356 if (IS_ERR(crtc_state))
2357 return PTR_ERR(crtc_state);
2359 primary_state = drm_atomic_get_plane_state(state, crtc->primary);
2360 if (IS_ERR(primary_state))
2361 return PTR_ERR(primary_state);
2365 WARN_ON(set->num_connectors);
2367 ret = drm_atomic_set_mode_for_crtc(crtc_state, NULL);
2371 crtc_state->active = false;
2373 ret = drm_atomic_set_crtc_for_plane(primary_state, NULL);
2377 drm_atomic_set_fb_for_plane(primary_state, NULL);
2383 WARN_ON(!set->num_connectors);
2385 ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
2389 crtc_state->active = true;
2391 ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
2395 drm_mode_get_hv_timing(set->mode, &hdisplay, &vdisplay);
2397 drm_atomic_set_fb_for_plane(primary_state, set->fb);
2398 primary_state->crtc_x = 0;
2399 primary_state->crtc_y = 0;
2400 primary_state->crtc_w = hdisplay;
2401 primary_state->crtc_h = vdisplay;
2402 primary_state->src_x = set->x << 16;
2403 primary_state->src_y = set->y << 16;
2404 if (drm_rotation_90_or_270(primary_state->rotation)) {
2405 primary_state->src_w = vdisplay << 16;
2406 primary_state->src_h = hdisplay << 16;
2408 primary_state->src_w = hdisplay << 16;
2409 primary_state->src_h = vdisplay << 16;
2413 ret = update_output_state(state, set);
2421 * drm_atomic_helper_disable_all - disable all currently active outputs
2423 * @ctx: lock acquisition context
2425 * Loops through all connectors, finding those that aren't turned off and then
2426 * turns them off by setting their DPMS mode to OFF and deactivating the CRTC
2427 * that they are connected to.
2429 * This is used for example in suspend/resume to disable all currently active
2430 * functions when suspending. If you just want to shut down everything at e.g.
2431 * driver unload, look at drm_atomic_helper_shutdown().
2433 * Note that if callers haven't already acquired all modeset locks this might
2434 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
2437 * 0 on success or a negative error code on failure.
2440 * drm_atomic_helper_suspend(), drm_atomic_helper_resume() and
2441 * drm_atomic_helper_shutdown().
2443 int drm_atomic_helper_disable_all(struct drm_device *dev,
2444 struct drm_modeset_acquire_ctx *ctx)
2446 struct drm_atomic_state *state;
2447 struct drm_connector_state *conn_state;
2448 struct drm_connector *conn;
2449 struct drm_plane_state *plane_state;
2450 struct drm_plane *plane;
2451 struct drm_crtc_state *crtc_state;
2452 struct drm_crtc *crtc;
2455 state = drm_atomic_state_alloc(dev);
2459 state->acquire_ctx = ctx;
2461 drm_for_each_crtc(crtc, dev) {
2462 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2463 if (IS_ERR(crtc_state)) {
2464 ret = PTR_ERR(crtc_state);
2468 crtc_state->active = false;
2470 ret = drm_atomic_set_mode_prop_for_crtc(crtc_state, NULL);
2474 ret = drm_atomic_add_affected_planes(state, crtc);
2478 ret = drm_atomic_add_affected_connectors(state, crtc);
2483 for_each_connector_in_state(state, conn, conn_state, i) {
2484 ret = drm_atomic_set_crtc_for_connector(conn_state, NULL);
2489 for_each_plane_in_state(state, plane, plane_state, i) {
2490 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
2494 drm_atomic_set_fb_for_plane(plane_state, NULL);
2497 ret = drm_atomic_commit(state);
2499 drm_atomic_state_put(state);
2503 EXPORT_SYMBOL(drm_atomic_helper_disable_all);
2506 * drm_atomic_helper_shutdown - shutdown all CRTC
2509 * This shuts down all CRTC, which is useful for driver unloading. Shutdown on
2510 * suspend should instead be handled with drm_atomic_helper_suspend(), since
2511 * that also takes a snapshot of the modeset state to be restored on resume.
2513 * This is just a convenience wrapper around drm_atomic_helper_disable_all(),
2514 * and it is the atomic version of drm_crtc_force_disable_all().
2516 void drm_atomic_helper_shutdown(struct drm_device *dev)
2518 struct drm_modeset_acquire_ctx ctx;
2521 drm_modeset_acquire_init(&ctx, 0);
2523 ret = drm_modeset_lock_all_ctx(dev, &ctx);
2525 ret = drm_atomic_helper_disable_all(dev, &ctx);
2527 if (ret != -EDEADLK)
2530 drm_modeset_backoff(&ctx);
2534 DRM_ERROR("Disabling all crtc's during unload failed with %i\n", ret);
2536 drm_modeset_drop_locks(&ctx);
2537 drm_modeset_acquire_fini(&ctx);
2539 EXPORT_SYMBOL(drm_atomic_helper_shutdown);
2542 * drm_atomic_helper_suspend - subsystem-level suspend helper
2545 * Duplicates the current atomic state, disables all active outputs and then
2546 * returns a pointer to the original atomic state to the caller. Drivers can
2547 * pass this pointer to the drm_atomic_helper_resume() helper upon resume to
2548 * restore the output configuration that was active at the time the system
2551 * Note that it is potentially unsafe to use this. The atomic state object
2552 * returned by this function is assumed to be persistent. Drivers must ensure
2553 * that this holds true. Before calling this function, drivers must make sure
2554 * to suspend fbdev emulation so that nothing can be using the device.
2557 * A pointer to a copy of the state before suspend on success or an ERR_PTR()-
2558 * encoded error code on failure. Drivers should store the returned atomic
2559 * state object and pass it to the drm_atomic_helper_resume() helper upon
2563 * drm_atomic_helper_duplicate_state(), drm_atomic_helper_disable_all(),
2564 * drm_atomic_helper_resume(), drm_atomic_helper_commit_duplicated_state()
2566 struct drm_atomic_state *drm_atomic_helper_suspend(struct drm_device *dev)
2568 struct drm_modeset_acquire_ctx ctx;
2569 struct drm_atomic_state *state;
2572 drm_modeset_acquire_init(&ctx, 0);
2575 err = drm_modeset_lock_all_ctx(dev, &ctx);
2577 state = ERR_PTR(err);
2581 state = drm_atomic_helper_duplicate_state(dev, &ctx);
2585 err = drm_atomic_helper_disable_all(dev, &ctx);
2587 drm_atomic_state_put(state);
2588 state = ERR_PTR(err);
2593 if (PTR_ERR(state) == -EDEADLK) {
2594 drm_modeset_backoff(&ctx);
2598 drm_modeset_drop_locks(&ctx);
2599 drm_modeset_acquire_fini(&ctx);
2602 EXPORT_SYMBOL(drm_atomic_helper_suspend);
2605 * drm_atomic_helper_commit_duplicated_state - commit duplicated state
2606 * @state: duplicated atomic state to commit
2607 * @ctx: pointer to acquire_ctx to use for commit.
2609 * The state returned by drm_atomic_helper_duplicate_state() and
2610 * drm_atomic_helper_suspend() is partially invalid, and needs to
2611 * be fixed up before commit.
2614 * 0 on success or a negative error code on failure.
2617 * drm_atomic_helper_suspend()
2619 int drm_atomic_helper_commit_duplicated_state(struct drm_atomic_state *state,
2620 struct drm_modeset_acquire_ctx *ctx)
2623 struct drm_plane *plane;
2624 struct drm_plane_state *new_plane_state;
2625 struct drm_connector *connector;
2626 struct drm_connector_state *new_conn_state;
2627 struct drm_crtc *crtc;
2628 struct drm_crtc_state *new_crtc_state;
2630 state->acquire_ctx = ctx;
2632 for_each_new_plane_in_state(state, plane, new_plane_state, i)
2633 state->planes[i].old_state = plane->state;
2635 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i)
2636 state->crtcs[i].old_state = crtc->state;
2638 for_each_new_connector_in_state(state, connector, new_conn_state, i)
2639 state->connectors[i].old_state = connector->state;
2641 return drm_atomic_commit(state);
2643 EXPORT_SYMBOL(drm_atomic_helper_commit_duplicated_state);
2646 * drm_atomic_helper_resume - subsystem-level resume helper
2648 * @state: atomic state to resume to
2650 * Calls drm_mode_config_reset() to synchronize hardware and software states,
2651 * grabs all modeset locks and commits the atomic state object. This can be
2652 * used in conjunction with the drm_atomic_helper_suspend() helper to
2653 * implement suspend/resume for drivers that support atomic mode-setting.
2656 * 0 on success or a negative error code on failure.
2659 * drm_atomic_helper_suspend()
2661 int drm_atomic_helper_resume(struct drm_device *dev,
2662 struct drm_atomic_state *state)
2664 struct drm_modeset_acquire_ctx ctx;
2667 drm_mode_config_reset(dev);
2669 drm_modeset_acquire_init(&ctx, 0);
2671 err = drm_atomic_helper_commit_duplicated_state(state, &ctx);
2672 if (err != -EDEADLK)
2675 drm_modeset_backoff(&ctx);
2678 drm_modeset_drop_locks(&ctx);
2679 drm_modeset_acquire_fini(&ctx);
2683 EXPORT_SYMBOL(drm_atomic_helper_resume);
2686 * drm_atomic_helper_crtc_set_property - helper for crtc properties
2688 * @property: DRM property
2689 * @val: value of property
2691 * Provides a default crtc set_property handler using the atomic driver
2695 * Zero on success, error code on failure
2698 drm_atomic_helper_crtc_set_property(struct drm_crtc *crtc,
2699 struct drm_property *property,
2702 struct drm_atomic_state *state;
2703 struct drm_crtc_state *crtc_state;
2706 state = drm_atomic_state_alloc(crtc->dev);
2710 /* ->set_property is always called with all locks held. */
2711 state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
2713 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2714 if (IS_ERR(crtc_state)) {
2715 ret = PTR_ERR(crtc_state);
2719 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
2724 ret = drm_atomic_commit(state);
2726 if (ret == -EDEADLK)
2729 drm_atomic_state_put(state);
2733 drm_atomic_state_clear(state);
2734 drm_atomic_legacy_backoff(state);
2738 EXPORT_SYMBOL(drm_atomic_helper_crtc_set_property);
2741 * drm_atomic_helper_plane_set_property - helper for plane properties
2743 * @property: DRM property
2744 * @val: value of property
2746 * Provides a default plane set_property handler using the atomic driver
2750 * Zero on success, error code on failure
2753 drm_atomic_helper_plane_set_property(struct drm_plane *plane,
2754 struct drm_property *property,
2757 struct drm_atomic_state *state;
2758 struct drm_plane_state *plane_state;
2761 state = drm_atomic_state_alloc(plane->dev);
2765 /* ->set_property is always called with all locks held. */
2766 state->acquire_ctx = plane->dev->mode_config.acquire_ctx;
2768 plane_state = drm_atomic_get_plane_state(state, plane);
2769 if (IS_ERR(plane_state)) {
2770 ret = PTR_ERR(plane_state);
2774 ret = drm_atomic_plane_set_property(plane, plane_state,
2779 ret = drm_atomic_commit(state);
2781 if (ret == -EDEADLK)
2784 drm_atomic_state_put(state);
2788 drm_atomic_state_clear(state);
2789 drm_atomic_legacy_backoff(state);
2793 EXPORT_SYMBOL(drm_atomic_helper_plane_set_property);
2796 * drm_atomic_helper_connector_set_property - helper for connector properties
2797 * @connector: DRM connector
2798 * @property: DRM property
2799 * @val: value of property
2801 * Provides a default connector set_property handler using the atomic driver
2805 * Zero on success, error code on failure
2808 drm_atomic_helper_connector_set_property(struct drm_connector *connector,
2809 struct drm_property *property,
2812 struct drm_atomic_state *state;
2813 struct drm_connector_state *connector_state;
2816 state = drm_atomic_state_alloc(connector->dev);
2820 /* ->set_property is always called with all locks held. */
2821 state->acquire_ctx = connector->dev->mode_config.acquire_ctx;
2823 connector_state = drm_atomic_get_connector_state(state, connector);
2824 if (IS_ERR(connector_state)) {
2825 ret = PTR_ERR(connector_state);
2829 ret = drm_atomic_connector_set_property(connector, connector_state,
2834 ret = drm_atomic_commit(state);
2836 if (ret == -EDEADLK)
2839 drm_atomic_state_put(state);
2843 drm_atomic_state_clear(state);
2844 drm_atomic_legacy_backoff(state);
2848 EXPORT_SYMBOL(drm_atomic_helper_connector_set_property);
2850 static int page_flip_common(
2851 struct drm_atomic_state *state,
2852 struct drm_crtc *crtc,
2853 struct drm_framebuffer *fb,
2854 struct drm_pending_vblank_event *event,
2857 struct drm_plane *plane = crtc->primary;
2858 struct drm_plane_state *plane_state;
2859 struct drm_crtc_state *crtc_state;
2862 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2863 if (IS_ERR(crtc_state))
2864 return PTR_ERR(crtc_state);
2866 crtc_state->event = event;
2867 crtc_state->pageflip_flags = flags;
2869 plane_state = drm_atomic_get_plane_state(state, plane);
2870 if (IS_ERR(plane_state))
2871 return PTR_ERR(plane_state);
2873 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
2876 drm_atomic_set_fb_for_plane(plane_state, fb);
2878 /* Make sure we don't accidentally do a full modeset. */
2879 state->allow_modeset = false;
2880 if (!crtc_state->active) {
2881 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] disabled, rejecting legacy flip\n",
2882 crtc->base.id, crtc->name);
2890 * drm_atomic_helper_page_flip - execute a legacy page flip
2892 * @fb: DRM framebuffer
2893 * @event: optional DRM event to signal upon completion
2894 * @flags: flip flags for non-vblank sync'ed updates
2895 * @ctx: lock acquisition context
2897 * Provides a default &drm_crtc_funcs.page_flip implementation
2898 * using the atomic driver interface.
2901 * Returns 0 on success, negative errno numbers on failure.
2904 * drm_atomic_helper_page_flip_target()
2906 int drm_atomic_helper_page_flip(struct drm_crtc *crtc,
2907 struct drm_framebuffer *fb,
2908 struct drm_pending_vblank_event *event,
2910 struct drm_modeset_acquire_ctx *ctx)
2912 struct drm_plane *plane = crtc->primary;
2913 struct drm_atomic_state *state;
2916 state = drm_atomic_state_alloc(plane->dev);
2920 state->acquire_ctx = ctx;
2922 ret = page_flip_common(state, crtc, fb, event, flags);
2926 ret = drm_atomic_nonblocking_commit(state);
2928 drm_atomic_state_put(state);
2931 EXPORT_SYMBOL(drm_atomic_helper_page_flip);
2934 * drm_atomic_helper_page_flip_target - do page flip on target vblank period.
2936 * @fb: DRM framebuffer
2937 * @event: optional DRM event to signal upon completion
2938 * @flags: flip flags for non-vblank sync'ed updates
2939 * @target: specifying the target vblank period when the flip to take effect
2940 * @ctx: lock acquisition context
2942 * Provides a default &drm_crtc_funcs.page_flip_target implementation.
2943 * Similar to drm_atomic_helper_page_flip() with extra parameter to specify
2944 * target vblank period to flip.
2947 * Returns 0 on success, negative errno numbers on failure.
2949 int drm_atomic_helper_page_flip_target(
2950 struct drm_crtc *crtc,
2951 struct drm_framebuffer *fb,
2952 struct drm_pending_vblank_event *event,
2955 struct drm_modeset_acquire_ctx *ctx)
2957 struct drm_plane *plane = crtc->primary;
2958 struct drm_atomic_state *state;
2959 struct drm_crtc_state *crtc_state;
2962 state = drm_atomic_state_alloc(plane->dev);
2966 state->acquire_ctx = ctx;
2968 ret = page_flip_common(state, crtc, fb, event, flags);
2972 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
2973 if (WARN_ON(!crtc_state)) {
2977 crtc_state->target_vblank = target;
2979 ret = drm_atomic_nonblocking_commit(state);
2981 drm_atomic_state_put(state);
2984 EXPORT_SYMBOL(drm_atomic_helper_page_flip_target);
2987 * drm_atomic_helper_connector_dpms() - connector dpms helper implementation
2988 * @connector: affected connector
2991 * This is the main helper function provided by the atomic helper framework for
2992 * implementing the legacy DPMS connector interface. It computes the new desired
2993 * &drm_crtc_state.active state for the corresponding CRTC (if the connector is
2994 * enabled) and updates it.
2997 * Returns 0 on success, negative errno numbers on failure.
2999 int drm_atomic_helper_connector_dpms(struct drm_connector *connector,
3002 struct drm_mode_config *config = &connector->dev->mode_config;
3003 struct drm_atomic_state *state;
3004 struct drm_crtc_state *crtc_state;
3005 struct drm_crtc *crtc;
3006 struct drm_connector *tmp_connector;
3007 struct drm_connector_list_iter conn_iter;
3009 bool active = false;
3010 int old_mode = connector->dpms;
3012 if (mode != DRM_MODE_DPMS_ON)
3013 mode = DRM_MODE_DPMS_OFF;
3015 connector->dpms = mode;
3016 crtc = connector->state->crtc;
3021 state = drm_atomic_state_alloc(connector->dev);
3025 state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
3027 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3028 if (IS_ERR(crtc_state)) {
3029 ret = PTR_ERR(crtc_state);
3033 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
3035 drm_connector_list_iter_begin(connector->dev, &conn_iter);
3036 drm_for_each_connector_iter(tmp_connector, &conn_iter) {
3037 if (tmp_connector->state->crtc != crtc)
3040 if (tmp_connector->dpms == DRM_MODE_DPMS_ON) {
3045 drm_connector_list_iter_end(&conn_iter);
3046 crtc_state->active = active;
3048 ret = drm_atomic_commit(state);
3050 if (ret == -EDEADLK)
3053 connector->dpms = old_mode;
3054 drm_atomic_state_put(state);
3058 drm_atomic_state_clear(state);
3059 drm_atomic_legacy_backoff(state);
3063 EXPORT_SYMBOL(drm_atomic_helper_connector_dpms);
3066 * drm_atomic_helper_best_encoder - Helper for
3067 * &drm_connector_helper_funcs.best_encoder callback
3068 * @connector: Connector control structure
3070 * This is a &drm_connector_helper_funcs.best_encoder callback helper for
3071 * connectors that support exactly 1 encoder, statically determined at driver
3074 struct drm_encoder *
3075 drm_atomic_helper_best_encoder(struct drm_connector *connector)
3077 WARN_ON(connector->encoder_ids[1]);
3078 return drm_encoder_find(connector->dev, connector->encoder_ids[0]);
3080 EXPORT_SYMBOL(drm_atomic_helper_best_encoder);
3083 * DOC: atomic state reset and initialization
3085 * Both the drm core and the atomic helpers assume that there is always the full
3086 * and correct atomic software state for all connectors, CRTCs and planes
3087 * available. Which is a bit a problem on driver load and also after system
3088 * suspend. One way to solve this is to have a hardware state read-out
3089 * infrastructure which reconstructs the full software state (e.g. the i915
3092 * The simpler solution is to just reset the software state to everything off,
3093 * which is easiest to do by calling drm_mode_config_reset(). To facilitate this
3094 * the atomic helpers provide default reset implementations for all hooks.
3096 * On the upside the precise state tracking of atomic simplifies system suspend
3097 * and resume a lot. For drivers using drm_mode_config_reset() a complete recipe
3098 * is implemented in drm_atomic_helper_suspend() and drm_atomic_helper_resume().
3099 * For other drivers the building blocks are split out, see the documentation
3100 * for these functions.
3104 * drm_atomic_helper_crtc_reset - default &drm_crtc_funcs.reset hook for CRTCs
3107 * Resets the atomic state for @crtc by freeing the state pointer (which might
3108 * be NULL, e.g. at driver load time) and allocating a new empty state object.
3110 void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc)
3113 __drm_atomic_helper_crtc_destroy_state(crtc->state);
3116 crtc->state = kzalloc(sizeof(*crtc->state), GFP_KERNEL);
3119 crtc->state->crtc = crtc;
3121 EXPORT_SYMBOL(drm_atomic_helper_crtc_reset);
3124 * __drm_atomic_helper_crtc_duplicate_state - copy atomic CRTC state
3125 * @crtc: CRTC object
3126 * @state: atomic CRTC state
3128 * Copies atomic state from a CRTC's current state and resets inferred values.
3129 * This is useful for drivers that subclass the CRTC state.
3131 void __drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc,
3132 struct drm_crtc_state *state)
3134 memcpy(state, crtc->state, sizeof(*state));
3136 if (state->mode_blob)
3137 drm_property_blob_get(state->mode_blob);
3138 if (state->degamma_lut)
3139 drm_property_blob_get(state->degamma_lut);
3141 drm_property_blob_get(state->ctm);
3142 if (state->gamma_lut)
3143 drm_property_blob_get(state->gamma_lut);
3144 state->mode_changed = false;
3145 state->active_changed = false;
3146 state->planes_changed = false;
3147 state->connectors_changed = false;
3148 state->color_mgmt_changed = false;
3149 state->zpos_changed = false;
3150 state->event = NULL;
3151 state->pageflip_flags = 0;
3153 EXPORT_SYMBOL(__drm_atomic_helper_crtc_duplicate_state);
3156 * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
3159 * Default CRTC state duplicate hook for drivers which don't have their own
3160 * subclassed CRTC state structure.
3162 struct drm_crtc_state *
3163 drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc)
3165 struct drm_crtc_state *state;
3167 if (WARN_ON(!crtc->state))
3170 state = kmalloc(sizeof(*state), GFP_KERNEL);
3172 __drm_atomic_helper_crtc_duplicate_state(crtc, state);
3176 EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state);
3179 * __drm_atomic_helper_crtc_destroy_state - release CRTC state
3180 * @state: CRTC state object to release
3182 * Releases all resources stored in the CRTC state without actually freeing
3183 * the memory of the CRTC state. This is useful for drivers that subclass the
3186 void __drm_atomic_helper_crtc_destroy_state(struct drm_crtc_state *state)
3188 drm_property_blob_put(state->mode_blob);
3189 drm_property_blob_put(state->degamma_lut);
3190 drm_property_blob_put(state->ctm);
3191 drm_property_blob_put(state->gamma_lut);
3193 EXPORT_SYMBOL(__drm_atomic_helper_crtc_destroy_state);
3196 * drm_atomic_helper_crtc_destroy_state - default state destroy hook
3198 * @state: CRTC state object to release
3200 * Default CRTC state destroy hook for drivers which don't have their own
3201 * subclassed CRTC state structure.
3203 void drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
3204 struct drm_crtc_state *state)
3206 __drm_atomic_helper_crtc_destroy_state(state);
3209 EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state);
3212 * drm_atomic_helper_plane_reset - default &drm_plane_funcs.reset hook for planes
3215 * Resets the atomic state for @plane by freeing the state pointer (which might
3216 * be NULL, e.g. at driver load time) and allocating a new empty state object.
3218 void drm_atomic_helper_plane_reset(struct drm_plane *plane)
3221 __drm_atomic_helper_plane_destroy_state(plane->state);
3223 kfree(plane->state);
3224 plane->state = kzalloc(sizeof(*plane->state), GFP_KERNEL);
3227 plane->state->plane = plane;
3228 plane->state->rotation = DRM_MODE_ROTATE_0;
3231 EXPORT_SYMBOL(drm_atomic_helper_plane_reset);
3234 * __drm_atomic_helper_plane_duplicate_state - copy atomic plane state
3235 * @plane: plane object
3236 * @state: atomic plane state
3238 * Copies atomic state from a plane's current state. This is useful for
3239 * drivers that subclass the plane state.
3241 void __drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane,
3242 struct drm_plane_state *state)
3244 memcpy(state, plane->state, sizeof(*state));
3247 drm_framebuffer_get(state->fb);
3249 state->fence = NULL;
3251 EXPORT_SYMBOL(__drm_atomic_helper_plane_duplicate_state);
3254 * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
3257 * Default plane state duplicate hook for drivers which don't have their own
3258 * subclassed plane state structure.
3260 struct drm_plane_state *
3261 drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane)
3263 struct drm_plane_state *state;
3265 if (WARN_ON(!plane->state))
3268 state = kmalloc(sizeof(*state), GFP_KERNEL);
3270 __drm_atomic_helper_plane_duplicate_state(plane, state);
3274 EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state);
3277 * __drm_atomic_helper_plane_destroy_state - release plane state
3278 * @state: plane state object to release
3280 * Releases all resources stored in the plane state without actually freeing
3281 * the memory of the plane state. This is useful for drivers that subclass the
3284 void __drm_atomic_helper_plane_destroy_state(struct drm_plane_state *state)
3287 drm_framebuffer_put(state->fb);
3290 dma_fence_put(state->fence);
3292 EXPORT_SYMBOL(__drm_atomic_helper_plane_destroy_state);
3295 * drm_atomic_helper_plane_destroy_state - default state destroy hook
3297 * @state: plane state object to release
3299 * Default plane state destroy hook for drivers which don't have their own
3300 * subclassed plane state structure.
3302 void drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
3303 struct drm_plane_state *state)
3305 __drm_atomic_helper_plane_destroy_state(state);
3308 EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state);
3311 * __drm_atomic_helper_connector_reset - reset state on connector
3312 * @connector: drm connector
3313 * @conn_state: connector state to assign
3315 * Initializes the newly allocated @conn_state and assigns it to
3316 * the &drm_conector->state pointer of @connector, usually required when
3317 * initializing the drivers or when called from the &drm_connector_funcs.reset
3320 * This is useful for drivers that subclass the connector state.
3323 __drm_atomic_helper_connector_reset(struct drm_connector *connector,
3324 struct drm_connector_state *conn_state)
3327 conn_state->connector = connector;
3329 connector->state = conn_state;
3331 EXPORT_SYMBOL(__drm_atomic_helper_connector_reset);
3334 * drm_atomic_helper_connector_reset - default &drm_connector_funcs.reset hook for connectors
3335 * @connector: drm connector
3337 * Resets the atomic state for @connector by freeing the state pointer (which
3338 * might be NULL, e.g. at driver load time) and allocating a new empty state
3341 void drm_atomic_helper_connector_reset(struct drm_connector *connector)
3343 struct drm_connector_state *conn_state =
3344 kzalloc(sizeof(*conn_state), GFP_KERNEL);
3346 if (connector->state)
3347 __drm_atomic_helper_connector_destroy_state(connector->state);
3349 kfree(connector->state);
3350 __drm_atomic_helper_connector_reset(connector, conn_state);
3352 EXPORT_SYMBOL(drm_atomic_helper_connector_reset);
3355 * __drm_atomic_helper_connector_duplicate_state - copy atomic connector state
3356 * @connector: connector object
3357 * @state: atomic connector state
3359 * Copies atomic state from a connector's current state. This is useful for
3360 * drivers that subclass the connector state.
3363 __drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector,
3364 struct drm_connector_state *state)
3366 memcpy(state, connector->state, sizeof(*state));
3368 drm_connector_get(connector);
3370 EXPORT_SYMBOL(__drm_atomic_helper_connector_duplicate_state);
3373 * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
3374 * @connector: drm connector
3376 * Default connector state duplicate hook for drivers which don't have their own
3377 * subclassed connector state structure.
3379 struct drm_connector_state *
3380 drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
3382 struct drm_connector_state *state;
3384 if (WARN_ON(!connector->state))
3387 state = kmalloc(sizeof(*state), GFP_KERNEL);
3389 __drm_atomic_helper_connector_duplicate_state(connector, state);
3393 EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
3396 * drm_atomic_helper_duplicate_state - duplicate an atomic state object
3398 * @ctx: lock acquisition context
3400 * Makes a copy of the current atomic state by looping over all objects and
3401 * duplicating their respective states. This is used for example by suspend/
3402 * resume support code to save the state prior to suspend such that it can
3403 * be restored upon resume.
3405 * Note that this treats atomic state as persistent between save and restore.
3406 * Drivers must make sure that this is possible and won't result in confusion
3407 * or erroneous behaviour.
3409 * Note that if callers haven't already acquired all modeset locks this might
3410 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
3413 * A pointer to the copy of the atomic state object on success or an
3414 * ERR_PTR()-encoded error code on failure.
3417 * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
3419 struct drm_atomic_state *
3420 drm_atomic_helper_duplicate_state(struct drm_device *dev,
3421 struct drm_modeset_acquire_ctx *ctx)
3423 struct drm_atomic_state *state;
3424 struct drm_connector *conn;
3425 struct drm_connector_list_iter conn_iter;
3426 struct drm_plane *plane;
3427 struct drm_crtc *crtc;
3430 state = drm_atomic_state_alloc(dev);
3432 return ERR_PTR(-ENOMEM);
3434 state->acquire_ctx = ctx;
3436 drm_for_each_crtc(crtc, dev) {
3437 struct drm_crtc_state *crtc_state;
3439 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3440 if (IS_ERR(crtc_state)) {
3441 err = PTR_ERR(crtc_state);
3446 drm_for_each_plane(plane, dev) {
3447 struct drm_plane_state *plane_state;
3449 plane_state = drm_atomic_get_plane_state(state, plane);
3450 if (IS_ERR(plane_state)) {
3451 err = PTR_ERR(plane_state);
3456 drm_connector_list_iter_begin(dev, &conn_iter);
3457 drm_for_each_connector_iter(conn, &conn_iter) {
3458 struct drm_connector_state *conn_state;
3460 conn_state = drm_atomic_get_connector_state(state, conn);
3461 if (IS_ERR(conn_state)) {
3462 err = PTR_ERR(conn_state);
3463 drm_connector_list_iter_end(&conn_iter);
3467 drm_connector_list_iter_end(&conn_iter);
3469 /* clear the acquire context so that it isn't accidentally reused */
3470 state->acquire_ctx = NULL;
3474 drm_atomic_state_put(state);
3475 state = ERR_PTR(err);
3480 EXPORT_SYMBOL(drm_atomic_helper_duplicate_state);
3483 * __drm_atomic_helper_connector_destroy_state - release connector state
3484 * @state: connector state object to release
3486 * Releases all resources stored in the connector state without actually
3487 * freeing the memory of the connector state. This is useful for drivers that
3488 * subclass the connector state.
3491 __drm_atomic_helper_connector_destroy_state(struct drm_connector_state *state)
3494 drm_connector_put(state->connector);
3496 EXPORT_SYMBOL(__drm_atomic_helper_connector_destroy_state);
3499 * drm_atomic_helper_connector_destroy_state - default state destroy hook
3500 * @connector: drm connector
3501 * @state: connector state object to release
3503 * Default connector state destroy hook for drivers which don't have their own
3504 * subclassed connector state structure.
3506 void drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
3507 struct drm_connector_state *state)
3509 __drm_atomic_helper_connector_destroy_state(state);
3512 EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state);
3515 * drm_atomic_helper_legacy_gamma_set - set the legacy gamma correction table
3516 * @crtc: CRTC object
3517 * @red: red correction table
3518 * @green: green correction table
3519 * @blue: green correction table
3520 * @size: size of the tables
3521 * @ctx: lock acquire context
3523 * Implements support for legacy gamma correction table for drivers
3524 * that support color management through the DEGAMMA_LUT/GAMMA_LUT
3525 * properties. See drm_crtc_enable_color_mgmt() and the containing chapter for
3526 * how the atomic color management and gamma tables work.
3528 int drm_atomic_helper_legacy_gamma_set(struct drm_crtc *crtc,
3529 u16 *red, u16 *green, u16 *blue,
3531 struct drm_modeset_acquire_ctx *ctx)
3533 struct drm_device *dev = crtc->dev;
3534 struct drm_mode_config *config = &dev->mode_config;
3535 struct drm_atomic_state *state;
3536 struct drm_crtc_state *crtc_state;
3537 struct drm_property_blob *blob = NULL;
3538 struct drm_color_lut *blob_data;
3541 state = drm_atomic_state_alloc(crtc->dev);
3545 blob = drm_property_create_blob(dev,
3546 sizeof(struct drm_color_lut) * size,
3549 ret = PTR_ERR(blob);
3554 /* Prepare GAMMA_LUT with the legacy values. */
3555 blob_data = (struct drm_color_lut *) blob->data;
3556 for (i = 0; i < size; i++) {
3557 blob_data[i].red = red[i];
3558 blob_data[i].green = green[i];
3559 blob_data[i].blue = blue[i];
3562 state->acquire_ctx = ctx;
3563 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3564 if (IS_ERR(crtc_state)) {
3565 ret = PTR_ERR(crtc_state);
3569 /* Reset DEGAMMA_LUT and CTM properties. */
3570 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
3571 config->degamma_lut_property, 0);
3575 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
3576 config->ctm_property, 0);
3580 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
3581 config->gamma_lut_property, blob->base.id);
3585 ret = drm_atomic_commit(state);
3588 drm_atomic_state_put(state);
3589 drm_property_blob_put(blob);
3592 EXPORT_SYMBOL(drm_atomic_helper_legacy_gamma_set);