2 * Copyright (C) 2014 Red Hat
3 * Copyright (C) 2014 Intel Corp.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
29 #include <drm/drm_atomic.h>
30 #include <drm/drm_atomic_uapi.h>
31 #include <drm/drm_plane_helper.h>
32 #include <drm/drm_crtc_helper.h>
33 #include <drm/drm_atomic_helper.h>
34 #include <drm/drm_writeback.h>
35 #include <drm/drm_damage_helper.h>
36 #include <linux/dma-fence.h>
38 #include "drm_crtc_helper_internal.h"
39 #include "drm_crtc_internal.h"
44 * This helper library provides implementations of check and commit functions on
45 * top of the CRTC modeset helper callbacks and the plane helper callbacks. It
46 * also provides convenience implementations for the atomic state handling
47 * callbacks for drivers which don't need to subclass the drm core structures to
48 * add their own additional internal state.
50 * This library also provides default implementations for the check callback in
51 * drm_atomic_helper_check() and for the commit callback with
52 * drm_atomic_helper_commit(). But the individual stages and callbacks are
53 * exposed to allow drivers to mix and match and e.g. use the plane helpers only
54 * together with a driver private modeset implementation.
56 * This library also provides implementations for all the legacy driver
57 * interfaces on top of the atomic interface. See drm_atomic_helper_set_config(),
58 * drm_atomic_helper_disable_plane(), drm_atomic_helper_disable_plane() and the
59 * various functions to implement set_property callbacks. New drivers must not
60 * implement these functions themselves but must use the provided helpers.
62 * The atomic helper uses the same function table structures as all other
63 * modesetting helpers. See the documentation for &struct drm_crtc_helper_funcs,
64 * struct &drm_encoder_helper_funcs and &struct drm_connector_helper_funcs. It
65 * also shares the &struct drm_plane_helper_funcs function table with the plane
69 drm_atomic_helper_plane_changed(struct drm_atomic_state *state,
70 struct drm_plane_state *old_plane_state,
71 struct drm_plane_state *plane_state,
72 struct drm_plane *plane)
74 struct drm_crtc_state *crtc_state;
76 if (old_plane_state->crtc) {
77 crtc_state = drm_atomic_get_new_crtc_state(state,
78 old_plane_state->crtc);
80 if (WARN_ON(!crtc_state))
83 crtc_state->planes_changed = true;
86 if (plane_state->crtc) {
87 crtc_state = drm_atomic_get_new_crtc_state(state, plane_state->crtc);
89 if (WARN_ON(!crtc_state))
92 crtc_state->planes_changed = true;
97 * For connectors that support multiple encoders, either the
98 * .atomic_best_encoder() or .best_encoder() operation must be implemented.
100 static struct drm_encoder *
101 pick_single_encoder_for_connector(struct drm_connector *connector)
103 WARN_ON(connector->encoder_ids[1]);
104 return drm_encoder_find(connector->dev, NULL, connector->encoder_ids[0]);
107 static int handle_conflicting_encoders(struct drm_atomic_state *state,
108 bool disable_conflicting_encoders)
110 struct drm_connector_state *new_conn_state;
111 struct drm_connector *connector;
112 struct drm_connector_list_iter conn_iter;
113 struct drm_encoder *encoder;
114 unsigned encoder_mask = 0;
118 * First loop, find all newly assigned encoders from the connectors
119 * part of the state. If the same encoder is assigned to multiple
120 * connectors bail out.
122 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
123 const struct drm_connector_helper_funcs *funcs = connector->helper_private;
124 struct drm_encoder *new_encoder;
126 if (!new_conn_state->crtc)
129 if (funcs->atomic_best_encoder)
130 new_encoder = funcs->atomic_best_encoder(connector, new_conn_state);
131 else if (funcs->best_encoder)
132 new_encoder = funcs->best_encoder(connector);
134 new_encoder = pick_single_encoder_for_connector(connector);
137 if (encoder_mask & drm_encoder_mask(new_encoder)) {
138 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] on [CONNECTOR:%d:%s] already assigned\n",
139 new_encoder->base.id, new_encoder->name,
140 connector->base.id, connector->name);
145 encoder_mask |= drm_encoder_mask(new_encoder);
153 * Second loop, iterate over all connectors not part of the state.
155 * If a conflicting encoder is found and disable_conflicting_encoders
156 * is not set, an error is returned. Userspace can provide a solution
157 * through the atomic ioctl.
159 * If the flag is set conflicting connectors are removed from the crtc
160 * and the crtc is disabled if no encoder is left. This preserves
161 * compatibility with the legacy set_config behavior.
163 drm_connector_list_iter_begin(state->dev, &conn_iter);
164 drm_for_each_connector_iter(connector, &conn_iter) {
165 struct drm_crtc_state *crtc_state;
167 if (drm_atomic_get_new_connector_state(state, connector))
170 encoder = connector->state->best_encoder;
171 if (!encoder || !(encoder_mask & drm_encoder_mask(encoder)))
174 if (!disable_conflicting_encoders) {
175 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s] by [CONNECTOR:%d:%s]\n",
176 encoder->base.id, encoder->name,
177 connector->state->crtc->base.id,
178 connector->state->crtc->name,
179 connector->base.id, connector->name);
184 new_conn_state = drm_atomic_get_connector_state(state, connector);
185 if (IS_ERR(new_conn_state)) {
186 ret = PTR_ERR(new_conn_state);
190 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s], disabling [CONNECTOR:%d:%s]\n",
191 encoder->base.id, encoder->name,
192 new_conn_state->crtc->base.id, new_conn_state->crtc->name,
193 connector->base.id, connector->name);
195 crtc_state = drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
197 ret = drm_atomic_set_crtc_for_connector(new_conn_state, NULL);
201 if (!crtc_state->connector_mask) {
202 ret = drm_atomic_set_mode_prop_for_crtc(crtc_state,
207 crtc_state->active = false;
211 drm_connector_list_iter_end(&conn_iter);
217 set_best_encoder(struct drm_atomic_state *state,
218 struct drm_connector_state *conn_state,
219 struct drm_encoder *encoder)
221 struct drm_crtc_state *crtc_state;
222 struct drm_crtc *crtc;
224 if (conn_state->best_encoder) {
225 /* Unset the encoder_mask in the old crtc state. */
226 crtc = conn_state->connector->state->crtc;
228 /* A NULL crtc is an error here because we should have
229 * duplicated a NULL best_encoder when crtc was NULL.
230 * As an exception restoring duplicated atomic state
231 * during resume is allowed, so don't warn when
232 * best_encoder is equal to encoder we intend to set.
234 WARN_ON(!crtc && encoder != conn_state->best_encoder);
236 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
238 crtc_state->encoder_mask &=
239 ~drm_encoder_mask(conn_state->best_encoder);
244 crtc = conn_state->crtc;
247 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
249 crtc_state->encoder_mask |=
250 drm_encoder_mask(encoder);
254 conn_state->best_encoder = encoder;
258 steal_encoder(struct drm_atomic_state *state,
259 struct drm_encoder *encoder)
261 struct drm_crtc_state *crtc_state;
262 struct drm_connector *connector;
263 struct drm_connector_state *old_connector_state, *new_connector_state;
266 for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
267 struct drm_crtc *encoder_crtc;
269 if (new_connector_state->best_encoder != encoder)
272 encoder_crtc = old_connector_state->crtc;
274 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s], stealing it\n",
275 encoder->base.id, encoder->name,
276 encoder_crtc->base.id, encoder_crtc->name);
278 set_best_encoder(state, new_connector_state, NULL);
280 crtc_state = drm_atomic_get_new_crtc_state(state, encoder_crtc);
281 crtc_state->connectors_changed = true;
288 update_connector_routing(struct drm_atomic_state *state,
289 struct drm_connector *connector,
290 struct drm_connector_state *old_connector_state,
291 struct drm_connector_state *new_connector_state)
293 const struct drm_connector_helper_funcs *funcs;
294 struct drm_encoder *new_encoder;
295 struct drm_crtc_state *crtc_state;
297 DRM_DEBUG_ATOMIC("Updating routing for [CONNECTOR:%d:%s]\n",
301 if (old_connector_state->crtc != new_connector_state->crtc) {
302 if (old_connector_state->crtc) {
303 crtc_state = drm_atomic_get_new_crtc_state(state, old_connector_state->crtc);
304 crtc_state->connectors_changed = true;
307 if (new_connector_state->crtc) {
308 crtc_state = drm_atomic_get_new_crtc_state(state, new_connector_state->crtc);
309 crtc_state->connectors_changed = true;
313 if (!new_connector_state->crtc) {
314 DRM_DEBUG_ATOMIC("Disabling [CONNECTOR:%d:%s]\n",
318 set_best_encoder(state, new_connector_state, NULL);
323 crtc_state = drm_atomic_get_new_crtc_state(state,
324 new_connector_state->crtc);
326 * For compatibility with legacy users, we want to make sure that
327 * we allow DPMS On->Off modesets on unregistered connectors. Modesets
328 * which would result in anything else must be considered invalid, to
329 * avoid turning on new displays on dead connectors.
331 * Since the connector can be unregistered at any point during an
332 * atomic check or commit, this is racy. But that's OK: all we care
333 * about is ensuring that userspace can't do anything but shut off the
334 * display on a connector that was destroyed after its been notified,
337 if (drm_connector_is_unregistered(connector) && crtc_state->active) {
338 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] is not registered\n",
339 connector->base.id, connector->name);
343 funcs = connector->helper_private;
345 if (funcs->atomic_best_encoder)
346 new_encoder = funcs->atomic_best_encoder(connector,
347 new_connector_state);
348 else if (funcs->best_encoder)
349 new_encoder = funcs->best_encoder(connector);
351 new_encoder = pick_single_encoder_for_connector(connector);
354 DRM_DEBUG_ATOMIC("No suitable encoder found for [CONNECTOR:%d:%s]\n",
360 if (!drm_encoder_crtc_ok(new_encoder, new_connector_state->crtc)) {
361 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] incompatible with [CRTC:%d:%s]\n",
362 new_encoder->base.id,
364 new_connector_state->crtc->base.id,
365 new_connector_state->crtc->name);
369 if (new_encoder == new_connector_state->best_encoder) {
370 set_best_encoder(state, new_connector_state, new_encoder);
372 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] keeps [ENCODER:%d:%s], now on [CRTC:%d:%s]\n",
375 new_encoder->base.id,
377 new_connector_state->crtc->base.id,
378 new_connector_state->crtc->name);
383 steal_encoder(state, new_encoder);
385 set_best_encoder(state, new_connector_state, new_encoder);
387 crtc_state->connectors_changed = true;
389 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] using [ENCODER:%d:%s] on [CRTC:%d:%s]\n",
392 new_encoder->base.id,
394 new_connector_state->crtc->base.id,
395 new_connector_state->crtc->name);
401 mode_fixup(struct drm_atomic_state *state)
403 struct drm_crtc *crtc;
404 struct drm_crtc_state *new_crtc_state;
405 struct drm_connector *connector;
406 struct drm_connector_state *new_conn_state;
410 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
411 if (!new_crtc_state->mode_changed &&
412 !new_crtc_state->connectors_changed)
415 drm_mode_copy(&new_crtc_state->adjusted_mode, &new_crtc_state->mode);
418 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
419 const struct drm_encoder_helper_funcs *funcs;
420 struct drm_encoder *encoder;
422 WARN_ON(!!new_conn_state->best_encoder != !!new_conn_state->crtc);
424 if (!new_conn_state->crtc || !new_conn_state->best_encoder)
428 drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
431 * Each encoder has at most one connector (since we always steal
432 * it away), so we won't call ->mode_fixup twice.
434 encoder = new_conn_state->best_encoder;
435 funcs = encoder->helper_private;
437 ret = drm_bridge_mode_fixup(encoder->bridge, &new_crtc_state->mode,
438 &new_crtc_state->adjusted_mode);
440 DRM_DEBUG_ATOMIC("Bridge fixup failed\n");
444 if (funcs && funcs->atomic_check) {
445 ret = funcs->atomic_check(encoder, new_crtc_state,
448 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] check failed\n",
449 encoder->base.id, encoder->name);
452 } else if (funcs && funcs->mode_fixup) {
453 ret = funcs->mode_fixup(encoder, &new_crtc_state->mode,
454 &new_crtc_state->adjusted_mode);
456 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] fixup failed\n",
457 encoder->base.id, encoder->name);
463 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
464 const struct drm_crtc_helper_funcs *funcs;
466 if (!new_crtc_state->enable)
469 if (!new_crtc_state->mode_changed &&
470 !new_crtc_state->connectors_changed)
473 funcs = crtc->helper_private;
474 if (!funcs->mode_fixup)
477 ret = funcs->mode_fixup(crtc, &new_crtc_state->mode,
478 &new_crtc_state->adjusted_mode);
480 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] fixup failed\n",
481 crtc->base.id, crtc->name);
489 static enum drm_mode_status mode_valid_path(struct drm_connector *connector,
490 struct drm_encoder *encoder,
491 struct drm_crtc *crtc,
492 struct drm_display_mode *mode)
494 enum drm_mode_status ret;
496 ret = drm_encoder_mode_valid(encoder, mode);
497 if (ret != MODE_OK) {
498 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] mode_valid() failed\n",
499 encoder->base.id, encoder->name);
503 ret = drm_bridge_mode_valid(encoder->bridge, mode);
504 if (ret != MODE_OK) {
505 DRM_DEBUG_ATOMIC("[BRIDGE] mode_valid() failed\n");
509 ret = drm_crtc_mode_valid(crtc, mode);
510 if (ret != MODE_OK) {
511 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] mode_valid() failed\n",
512 crtc->base.id, crtc->name);
520 mode_valid(struct drm_atomic_state *state)
522 struct drm_connector_state *conn_state;
523 struct drm_connector *connector;
526 for_each_new_connector_in_state(state, connector, conn_state, i) {
527 struct drm_encoder *encoder = conn_state->best_encoder;
528 struct drm_crtc *crtc = conn_state->crtc;
529 struct drm_crtc_state *crtc_state;
530 enum drm_mode_status mode_status;
531 struct drm_display_mode *mode;
533 if (!crtc || !encoder)
536 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
539 if (!crtc_state->mode_changed && !crtc_state->connectors_changed)
542 mode = &crtc_state->mode;
544 mode_status = mode_valid_path(connector, encoder, crtc, mode);
545 if (mode_status != MODE_OK)
553 * drm_atomic_helper_check_modeset - validate state object for modeset changes
555 * @state: the driver state object
557 * Check the state object to see if the requested state is physically possible.
558 * This does all the crtc and connector related computations for an atomic
559 * update and adds any additional connectors needed for full modesets. It calls
560 * the various per-object callbacks in the follow order:
562 * 1. &drm_connector_helper_funcs.atomic_best_encoder for determining the new encoder.
563 * 2. &drm_connector_helper_funcs.atomic_check to validate the connector state.
564 * 3. If it's determined a modeset is needed then all connectors on the affected crtc
565 * crtc are added and &drm_connector_helper_funcs.atomic_check is run on them.
566 * 4. &drm_encoder_helper_funcs.mode_valid, &drm_bridge_funcs.mode_valid and
567 * &drm_crtc_helper_funcs.mode_valid are called on the affected components.
568 * 5. &drm_bridge_funcs.mode_fixup is called on all encoder bridges.
569 * 6. &drm_encoder_helper_funcs.atomic_check is called to validate any encoder state.
570 * This function is only called when the encoder will be part of a configured crtc,
571 * it must not be used for implementing connector property validation.
572 * If this function is NULL, &drm_atomic_encoder_helper_funcs.mode_fixup is called
574 * 7. &drm_crtc_helper_funcs.mode_fixup is called last, to fix up the mode with crtc constraints.
576 * &drm_crtc_state.mode_changed is set when the input mode is changed.
577 * &drm_crtc_state.connectors_changed is set when a connector is added or
578 * removed from the crtc. &drm_crtc_state.active_changed is set when
579 * &drm_crtc_state.active changes, which is used for DPMS.
580 * See also: drm_atomic_crtc_needs_modeset()
584 * Drivers which set &drm_crtc_state.mode_changed (e.g. in their
585 * &drm_plane_helper_funcs.atomic_check hooks if a plane update can't be done
586 * without a full modeset) _must_ call this function afterwards after that
587 * change. It is permitted to call this function multiple times for the same
588 * update, e.g. when the &drm_crtc_helper_funcs.atomic_check functions depend
589 * upon the adjusted dotclock for fifo space allocation and watermark
593 * Zero for success or -errno
596 drm_atomic_helper_check_modeset(struct drm_device *dev,
597 struct drm_atomic_state *state)
599 struct drm_crtc *crtc;
600 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
601 struct drm_connector *connector;
602 struct drm_connector_state *old_connector_state, *new_connector_state;
604 unsigned connectors_mask = 0;
606 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
607 bool has_connectors =
608 !!new_crtc_state->connector_mask;
610 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
612 if (!drm_mode_equal(&old_crtc_state->mode, &new_crtc_state->mode)) {
613 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] mode changed\n",
614 crtc->base.id, crtc->name);
615 new_crtc_state->mode_changed = true;
618 if (old_crtc_state->enable != new_crtc_state->enable) {
619 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enable changed\n",
620 crtc->base.id, crtc->name);
623 * For clarity this assignment is done here, but
624 * enable == 0 is only true when there are no
625 * connectors and a NULL mode.
627 * The other way around is true as well. enable != 0
628 * iff connectors are attached and a mode is set.
630 new_crtc_state->mode_changed = true;
631 new_crtc_state->connectors_changed = true;
634 if (old_crtc_state->active != new_crtc_state->active) {
635 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active changed\n",
636 crtc->base.id, crtc->name);
637 new_crtc_state->active_changed = true;
640 if (new_crtc_state->enable != has_connectors) {
641 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled/connectors mismatch\n",
642 crtc->base.id, crtc->name);
648 ret = handle_conflicting_encoders(state, false);
652 for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
653 const struct drm_connector_helper_funcs *funcs = connector->helper_private;
655 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
658 * This only sets crtc->connectors_changed for routing changes,
659 * drivers must set crtc->connectors_changed themselves when
660 * connector properties need to be updated.
662 ret = update_connector_routing(state, connector,
664 new_connector_state);
667 if (old_connector_state->crtc) {
668 new_crtc_state = drm_atomic_get_new_crtc_state(state,
669 old_connector_state->crtc);
670 if (old_connector_state->link_status !=
671 new_connector_state->link_status)
672 new_crtc_state->connectors_changed = true;
674 if (old_connector_state->max_requested_bpc !=
675 new_connector_state->max_requested_bpc)
676 new_crtc_state->connectors_changed = true;
679 if (funcs->atomic_check)
680 ret = funcs->atomic_check(connector, new_connector_state);
684 connectors_mask |= BIT(i);
688 * After all the routing has been prepared we need to add in any
689 * connector which is itself unchanged, but who's crtc changes it's
690 * configuration. This must be done before calling mode_fixup in case a
691 * crtc only changed its mode but has the same set of connectors.
693 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
694 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
697 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] needs all connectors, enable: %c, active: %c\n",
698 crtc->base.id, crtc->name,
699 new_crtc_state->enable ? 'y' : 'n',
700 new_crtc_state->active ? 'y' : 'n');
702 ret = drm_atomic_add_affected_connectors(state, crtc);
706 ret = drm_atomic_add_affected_planes(state, crtc);
712 * Iterate over all connectors again, to make sure atomic_check()
713 * has been called on them when a modeset is forced.
715 for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
716 const struct drm_connector_helper_funcs *funcs = connector->helper_private;
718 if (connectors_mask & BIT(i))
721 if (funcs->atomic_check)
722 ret = funcs->atomic_check(connector, new_connector_state);
727 ret = mode_valid(state);
731 return mode_fixup(state);
733 EXPORT_SYMBOL(drm_atomic_helper_check_modeset);
736 * drm_atomic_helper_check_plane_state() - Check plane state for validity
737 * @plane_state: plane state to check
738 * @crtc_state: crtc state to check
739 * @min_scale: minimum @src:@dest scaling factor in 16.16 fixed point
740 * @max_scale: maximum @src:@dest scaling factor in 16.16 fixed point
741 * @can_position: is it legal to position the plane such that it
742 * doesn't cover the entire crtc? This will generally
743 * only be false for primary planes.
744 * @can_update_disabled: can the plane be updated while the crtc
747 * Checks that a desired plane update is valid, and updates various
748 * bits of derived state (clipped coordinates etc.). Drivers that provide
749 * their own plane handling rather than helper-provided implementations may
750 * still wish to call this function to avoid duplication of error checking
754 * Zero if update appears valid, error code on failure
756 int drm_atomic_helper_check_plane_state(struct drm_plane_state *plane_state,
757 const struct drm_crtc_state *crtc_state,
761 bool can_update_disabled)
763 struct drm_framebuffer *fb = plane_state->fb;
764 struct drm_rect *src = &plane_state->src;
765 struct drm_rect *dst = &plane_state->dst;
766 unsigned int rotation = plane_state->rotation;
767 struct drm_rect clip = {};
770 WARN_ON(plane_state->crtc && plane_state->crtc != crtc_state->crtc);
772 *src = drm_plane_state_src(plane_state);
773 *dst = drm_plane_state_dest(plane_state);
776 plane_state->visible = false;
780 /* crtc should only be NULL when disabling (i.e., !fb) */
781 if (WARN_ON(!plane_state->crtc)) {
782 plane_state->visible = false;
786 if (!crtc_state->enable && !can_update_disabled) {
787 DRM_DEBUG_KMS("Cannot update plane of a disabled CRTC.\n");
791 drm_rect_rotate(src, fb->width << 16, fb->height << 16, rotation);
794 hscale = drm_rect_calc_hscale(src, dst, min_scale, max_scale);
795 vscale = drm_rect_calc_vscale(src, dst, min_scale, max_scale);
796 if (hscale < 0 || vscale < 0) {
797 DRM_DEBUG_KMS("Invalid scaling of plane\n");
798 drm_rect_debug_print("src: ", &plane_state->src, true);
799 drm_rect_debug_print("dst: ", &plane_state->dst, false);
803 if (crtc_state->enable)
804 drm_mode_get_hv_timing(&crtc_state->mode, &clip.x2, &clip.y2);
806 plane_state->visible = drm_rect_clip_scaled(src, dst, &clip);
808 drm_rect_rotate_inv(src, fb->width << 16, fb->height << 16, rotation);
810 if (!plane_state->visible)
812 * Plane isn't visible; some drivers can handle this
813 * so we just return success here. Drivers that can't
814 * (including those that use the primary plane helper's
815 * update function) will return an error from their
816 * update_plane handler.
820 if (!can_position && !drm_rect_equals(dst, &clip)) {
821 DRM_DEBUG_KMS("Plane must cover entire CRTC\n");
822 drm_rect_debug_print("dst: ", dst, false);
823 drm_rect_debug_print("clip: ", &clip, false);
829 EXPORT_SYMBOL(drm_atomic_helper_check_plane_state);
832 * drm_atomic_helper_check_planes - validate state object for planes changes
834 * @state: the driver state object
836 * Check the state object to see if the requested state is physically possible.
837 * This does all the plane update related checks using by calling into the
838 * &drm_crtc_helper_funcs.atomic_check and &drm_plane_helper_funcs.atomic_check
839 * hooks provided by the driver.
841 * It also sets &drm_crtc_state.planes_changed to indicate that a crtc has
845 * Zero for success or -errno
848 drm_atomic_helper_check_planes(struct drm_device *dev,
849 struct drm_atomic_state *state)
851 struct drm_crtc *crtc;
852 struct drm_crtc_state *new_crtc_state;
853 struct drm_plane *plane;
854 struct drm_plane_state *new_plane_state, *old_plane_state;
857 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
858 const struct drm_plane_helper_funcs *funcs;
860 WARN_ON(!drm_modeset_is_locked(&plane->mutex));
862 funcs = plane->helper_private;
864 drm_atomic_helper_plane_changed(state, old_plane_state, new_plane_state, plane);
866 drm_atomic_helper_check_plane_damage(state, new_plane_state);
868 if (!funcs || !funcs->atomic_check)
871 ret = funcs->atomic_check(plane, new_plane_state);
873 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic driver check failed\n",
874 plane->base.id, plane->name);
879 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
880 const struct drm_crtc_helper_funcs *funcs;
882 funcs = crtc->helper_private;
884 if (!funcs || !funcs->atomic_check)
887 ret = funcs->atomic_check(crtc, new_crtc_state);
889 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic driver check failed\n",
890 crtc->base.id, crtc->name);
897 EXPORT_SYMBOL(drm_atomic_helper_check_planes);
900 * drm_atomic_helper_check - validate state object
902 * @state: the driver state object
904 * Check the state object to see if the requested state is physically possible.
905 * Only crtcs and planes have check callbacks, so for any additional (global)
906 * checking that a driver needs it can simply wrap that around this function.
907 * Drivers without such needs can directly use this as their
908 * &drm_mode_config_funcs.atomic_check callback.
910 * This just wraps the two parts of the state checking for planes and modeset
911 * state in the default order: First it calls drm_atomic_helper_check_modeset()
912 * and then drm_atomic_helper_check_planes(). The assumption is that the
913 * @drm_plane_helper_funcs.atomic_check and @drm_crtc_helper_funcs.atomic_check
914 * functions depend upon an updated adjusted_mode.clock to e.g. properly compute
917 * Note that zpos normalization will add all enable planes to the state which
918 * might not desired for some drivers.
919 * For example enable/disable of a cursor plane which have fixed zpos value
920 * would trigger all other enabled planes to be forced to the state change.
923 * Zero for success or -errno
925 int drm_atomic_helper_check(struct drm_device *dev,
926 struct drm_atomic_state *state)
930 ret = drm_atomic_helper_check_modeset(dev, state);
934 if (dev->mode_config.normalize_zpos) {
935 ret = drm_atomic_normalize_zpos(dev, state);
940 ret = drm_atomic_helper_check_planes(dev, state);
944 if (state->legacy_cursor_update)
945 state->async_update = !drm_atomic_helper_async_check(dev, state);
949 EXPORT_SYMBOL(drm_atomic_helper_check);
952 disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state)
954 struct drm_connector *connector;
955 struct drm_connector_state *old_conn_state, *new_conn_state;
956 struct drm_crtc *crtc;
957 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
960 for_each_oldnew_connector_in_state(old_state, connector, old_conn_state, new_conn_state, i) {
961 const struct drm_encoder_helper_funcs *funcs;
962 struct drm_encoder *encoder;
964 /* Shut down everything that's in the changeset and currently
965 * still on. So need to check the old, saved state. */
966 if (!old_conn_state->crtc)
969 old_crtc_state = drm_atomic_get_old_crtc_state(old_state, old_conn_state->crtc);
971 if (!old_crtc_state->active ||
972 !drm_atomic_crtc_needs_modeset(old_conn_state->crtc->state))
975 encoder = old_conn_state->best_encoder;
977 /* We shouldn't get this far if we didn't previously have
978 * an encoder.. but WARN_ON() rather than explode.
980 if (WARN_ON(!encoder))
983 funcs = encoder->helper_private;
985 DRM_DEBUG_ATOMIC("disabling [ENCODER:%d:%s]\n",
986 encoder->base.id, encoder->name);
989 * Each encoder has at most one connector (since we always steal
990 * it away), so we won't call disable hooks twice.
992 drm_bridge_disable(encoder->bridge);
994 /* Right function depends upon target state. */
996 if (new_conn_state->crtc && funcs->prepare)
997 funcs->prepare(encoder);
998 else if (funcs->disable)
999 funcs->disable(encoder);
1000 else if (funcs->dpms)
1001 funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
1004 drm_bridge_post_disable(encoder->bridge);
1007 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
1008 const struct drm_crtc_helper_funcs *funcs;
1011 /* Shut down everything that needs a full modeset. */
1012 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
1015 if (!old_crtc_state->active)
1018 funcs = crtc->helper_private;
1020 DRM_DEBUG_ATOMIC("disabling [CRTC:%d:%s]\n",
1021 crtc->base.id, crtc->name);
1024 /* Right function depends upon target state. */
1025 if (new_crtc_state->enable && funcs->prepare)
1026 funcs->prepare(crtc);
1027 else if (funcs->atomic_disable)
1028 funcs->atomic_disable(crtc, old_crtc_state);
1029 else if (funcs->disable)
1030 funcs->disable(crtc);
1032 funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
1034 if (!(dev->irq_enabled && dev->num_crtcs))
1037 ret = drm_crtc_vblank_get(crtc);
1038 WARN_ONCE(ret != -EINVAL, "driver forgot to call drm_crtc_vblank_off()\n");
1040 drm_crtc_vblank_put(crtc);
1045 * drm_atomic_helper_update_legacy_modeset_state - update legacy modeset state
1047 * @old_state: atomic state object with old state structures
1049 * This function updates all the various legacy modeset state pointers in
1050 * connectors, encoders and crtcs. It also updates the timestamping constants
1051 * used for precise vblank timestamps by calling
1052 * drm_calc_timestamping_constants().
1054 * Drivers can use this for building their own atomic commit if they don't have
1055 * a pure helper-based modeset implementation.
1057 * Since these updates are not synchronized with lockings, only code paths
1058 * called from &drm_mode_config_helper_funcs.atomic_commit_tail can look at the
1059 * legacy state filled out by this helper. Defacto this means this helper and
1060 * the legacy state pointers are only really useful for transitioning an
1061 * existing driver to the atomic world.
1064 drm_atomic_helper_update_legacy_modeset_state(struct drm_device *dev,
1065 struct drm_atomic_state *old_state)
1067 struct drm_connector *connector;
1068 struct drm_connector_state *old_conn_state, *new_conn_state;
1069 struct drm_crtc *crtc;
1070 struct drm_crtc_state *new_crtc_state;
1073 /* clear out existing links and update dpms */
1074 for_each_oldnew_connector_in_state(old_state, connector, old_conn_state, new_conn_state, i) {
1075 if (connector->encoder) {
1076 WARN_ON(!connector->encoder->crtc);
1078 connector->encoder->crtc = NULL;
1079 connector->encoder = NULL;
1082 crtc = new_conn_state->crtc;
1083 if ((!crtc && old_conn_state->crtc) ||
1084 (crtc && drm_atomic_crtc_needs_modeset(crtc->state))) {
1085 int mode = DRM_MODE_DPMS_OFF;
1087 if (crtc && crtc->state->active)
1088 mode = DRM_MODE_DPMS_ON;
1090 connector->dpms = mode;
1095 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
1096 if (!new_conn_state->crtc)
1099 if (WARN_ON(!new_conn_state->best_encoder))
1102 connector->encoder = new_conn_state->best_encoder;
1103 connector->encoder->crtc = new_conn_state->crtc;
1106 /* set legacy state in the crtc structure */
1107 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
1108 struct drm_plane *primary = crtc->primary;
1109 struct drm_plane_state *new_plane_state;
1111 crtc->mode = new_crtc_state->mode;
1112 crtc->enabled = new_crtc_state->enable;
1115 drm_atomic_get_new_plane_state(old_state, primary);
1117 if (new_plane_state && new_plane_state->crtc == crtc) {
1118 crtc->x = new_plane_state->src_x >> 16;
1119 crtc->y = new_plane_state->src_y >> 16;
1122 if (new_crtc_state->enable)
1123 drm_calc_timestamping_constants(crtc,
1124 &new_crtc_state->adjusted_mode);
1127 EXPORT_SYMBOL(drm_atomic_helper_update_legacy_modeset_state);
1130 crtc_set_mode(struct drm_device *dev, struct drm_atomic_state *old_state)
1132 struct drm_crtc *crtc;
1133 struct drm_crtc_state *new_crtc_state;
1134 struct drm_connector *connector;
1135 struct drm_connector_state *new_conn_state;
1138 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
1139 const struct drm_crtc_helper_funcs *funcs;
1141 if (!new_crtc_state->mode_changed)
1144 funcs = crtc->helper_private;
1146 if (new_crtc_state->enable && funcs->mode_set_nofb) {
1147 DRM_DEBUG_ATOMIC("modeset on [CRTC:%d:%s]\n",
1148 crtc->base.id, crtc->name);
1150 funcs->mode_set_nofb(crtc);
1154 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
1155 const struct drm_encoder_helper_funcs *funcs;
1156 struct drm_encoder *encoder;
1157 struct drm_display_mode *mode, *adjusted_mode;
1159 if (!new_conn_state->best_encoder)
1162 encoder = new_conn_state->best_encoder;
1163 funcs = encoder->helper_private;
1164 new_crtc_state = new_conn_state->crtc->state;
1165 mode = &new_crtc_state->mode;
1166 adjusted_mode = &new_crtc_state->adjusted_mode;
1168 if (!new_crtc_state->mode_changed)
1171 DRM_DEBUG_ATOMIC("modeset on [ENCODER:%d:%s]\n",
1172 encoder->base.id, encoder->name);
1175 * Each encoder has at most one connector (since we always steal
1176 * it away), so we won't call mode_set hooks twice.
1178 if (funcs && funcs->atomic_mode_set) {
1179 funcs->atomic_mode_set(encoder, new_crtc_state,
1181 } else if (funcs && funcs->mode_set) {
1182 funcs->mode_set(encoder, mode, adjusted_mode);
1185 drm_bridge_mode_set(encoder->bridge, mode, adjusted_mode);
1190 * drm_atomic_helper_commit_modeset_disables - modeset commit to disable outputs
1192 * @old_state: atomic state object with old state structures
1194 * This function shuts down all the outputs that need to be shut down and
1195 * prepares them (if required) with the new mode.
1197 * For compatibility with legacy crtc helpers this should be called before
1198 * drm_atomic_helper_commit_planes(), which is what the default commit function
1199 * does. But drivers with different needs can group the modeset commits together
1200 * and do the plane commits at the end. This is useful for drivers doing runtime
1201 * PM since planes updates then only happen when the CRTC is actually enabled.
1203 void drm_atomic_helper_commit_modeset_disables(struct drm_device *dev,
1204 struct drm_atomic_state *old_state)
1206 disable_outputs(dev, old_state);
1208 drm_atomic_helper_update_legacy_modeset_state(dev, old_state);
1210 crtc_set_mode(dev, old_state);
1212 EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_disables);
1214 static void drm_atomic_helper_commit_writebacks(struct drm_device *dev,
1215 struct drm_atomic_state *old_state)
1217 struct drm_connector *connector;
1218 struct drm_connector_state *new_conn_state;
1221 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
1222 const struct drm_connector_helper_funcs *funcs;
1224 funcs = connector->helper_private;
1225 if (!funcs->atomic_commit)
1228 if (new_conn_state->writeback_job && new_conn_state->writeback_job->fb) {
1229 WARN_ON(connector->connector_type != DRM_MODE_CONNECTOR_WRITEBACK);
1230 funcs->atomic_commit(connector, new_conn_state);
1236 * drm_atomic_helper_commit_modeset_enables - modeset commit to enable outputs
1238 * @old_state: atomic state object with old state structures
1240 * This function enables all the outputs with the new configuration which had to
1241 * be turned off for the update.
1243 * For compatibility with legacy crtc helpers this should be called after
1244 * drm_atomic_helper_commit_planes(), which is what the default commit function
1245 * does. But drivers with different needs can group the modeset commits together
1246 * and do the plane commits at the end. This is useful for drivers doing runtime
1247 * PM since planes updates then only happen when the CRTC is actually enabled.
1249 void drm_atomic_helper_commit_modeset_enables(struct drm_device *dev,
1250 struct drm_atomic_state *old_state)
1252 struct drm_crtc *crtc;
1253 struct drm_crtc_state *old_crtc_state;
1254 struct drm_crtc_state *new_crtc_state;
1255 struct drm_connector *connector;
1256 struct drm_connector_state *new_conn_state;
1259 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
1260 const struct drm_crtc_helper_funcs *funcs;
1262 /* Need to filter out CRTCs where only planes change. */
1263 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
1266 if (!new_crtc_state->active)
1269 funcs = crtc->helper_private;
1271 if (new_crtc_state->enable) {
1272 DRM_DEBUG_ATOMIC("enabling [CRTC:%d:%s]\n",
1273 crtc->base.id, crtc->name);
1275 if (funcs->atomic_enable)
1276 funcs->atomic_enable(crtc, old_crtc_state);
1278 funcs->commit(crtc);
1282 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
1283 const struct drm_encoder_helper_funcs *funcs;
1284 struct drm_encoder *encoder;
1286 if (!new_conn_state->best_encoder)
1289 if (!new_conn_state->crtc->state->active ||
1290 !drm_atomic_crtc_needs_modeset(new_conn_state->crtc->state))
1293 encoder = new_conn_state->best_encoder;
1294 funcs = encoder->helper_private;
1296 DRM_DEBUG_ATOMIC("enabling [ENCODER:%d:%s]\n",
1297 encoder->base.id, encoder->name);
1300 * Each encoder has at most one connector (since we always steal
1301 * it away), so we won't call enable hooks twice.
1303 drm_bridge_pre_enable(encoder->bridge);
1307 funcs->enable(encoder);
1308 else if (funcs->commit)
1309 funcs->commit(encoder);
1312 drm_bridge_enable(encoder->bridge);
1315 drm_atomic_helper_commit_writebacks(dev, old_state);
1317 EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_enables);
1320 * drm_atomic_helper_wait_for_fences - wait for fences stashed in plane state
1322 * @state: atomic state object with old state structures
1323 * @pre_swap: If true, do an interruptible wait, and @state is the new state.
1324 * Otherwise @state is the old state.
1326 * For implicit sync, driver should fish the exclusive fence out from the
1327 * incoming fb's and stash it in the drm_plane_state. This is called after
1328 * drm_atomic_helper_swap_state() so it uses the current plane state (and
1329 * just uses the atomic state to find the changed planes)
1331 * Note that @pre_swap is needed since the point where we block for fences moves
1332 * around depending upon whether an atomic commit is blocking or
1333 * non-blocking. For non-blocking commit all waiting needs to happen after
1334 * drm_atomic_helper_swap_state() is called, but for blocking commits we want
1335 * to wait **before** we do anything that can't be easily rolled back. That is
1336 * before we call drm_atomic_helper_swap_state().
1338 * Returns zero if success or < 0 if dma_fence_wait() fails.
1340 int drm_atomic_helper_wait_for_fences(struct drm_device *dev,
1341 struct drm_atomic_state *state,
1344 struct drm_plane *plane;
1345 struct drm_plane_state *new_plane_state;
1348 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
1349 if (!new_plane_state->fence)
1352 WARN_ON(!new_plane_state->fb);
1355 * If waiting for fences pre-swap (ie: nonblock), userspace can
1356 * still interrupt the operation. Instead of blocking until the
1357 * timer expires, make the wait interruptible.
1359 ret = dma_fence_wait(new_plane_state->fence, pre_swap);
1363 dma_fence_put(new_plane_state->fence);
1364 new_plane_state->fence = NULL;
1369 EXPORT_SYMBOL(drm_atomic_helper_wait_for_fences);
1372 * drm_atomic_helper_wait_for_vblanks - wait for vblank on crtcs
1374 * @old_state: atomic state object with old state structures
1376 * Helper to, after atomic commit, wait for vblanks on all effected
1377 * crtcs (ie. before cleaning up old framebuffers using
1378 * drm_atomic_helper_cleanup_planes()). It will only wait on CRTCs where the
1379 * framebuffers have actually changed to optimize for the legacy cursor and
1380 * plane update use-case.
1382 * Drivers using the nonblocking commit tracking support initialized by calling
1383 * drm_atomic_helper_setup_commit() should look at
1384 * drm_atomic_helper_wait_for_flip_done() as an alternative.
1387 drm_atomic_helper_wait_for_vblanks(struct drm_device *dev,
1388 struct drm_atomic_state *old_state)
1390 struct drm_crtc *crtc;
1391 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
1393 unsigned crtc_mask = 0;
1396 * Legacy cursor ioctls are completely unsynced, and userspace
1397 * relies on that (by doing tons of cursor updates).
1399 if (old_state->legacy_cursor_update)
1402 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
1403 if (!new_crtc_state->active)
1406 ret = drm_crtc_vblank_get(crtc);
1410 crtc_mask |= drm_crtc_mask(crtc);
1411 old_state->crtcs[i].last_vblank_count = drm_crtc_vblank_count(crtc);
1414 for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
1415 if (!(crtc_mask & drm_crtc_mask(crtc)))
1418 ret = wait_event_timeout(dev->vblank[i].queue,
1419 old_state->crtcs[i].last_vblank_count !=
1420 drm_crtc_vblank_count(crtc),
1421 msecs_to_jiffies(50));
1423 WARN(!ret, "[CRTC:%d:%s] vblank wait timed out\n",
1424 crtc->base.id, crtc->name);
1426 drm_crtc_vblank_put(crtc);
1429 EXPORT_SYMBOL(drm_atomic_helper_wait_for_vblanks);
1432 * drm_atomic_helper_wait_for_flip_done - wait for all page flips to be done
1434 * @old_state: atomic state object with old state structures
1436 * Helper to, after atomic commit, wait for page flips on all effected
1437 * crtcs (ie. before cleaning up old framebuffers using
1438 * drm_atomic_helper_cleanup_planes()). Compared to
1439 * drm_atomic_helper_wait_for_vblanks() this waits for the completion of on all
1440 * CRTCs, assuming that cursors-only updates are signalling their completion
1441 * immediately (or using a different path).
1443 * This requires that drivers use the nonblocking commit tracking support
1444 * initialized using drm_atomic_helper_setup_commit().
1446 void drm_atomic_helper_wait_for_flip_done(struct drm_device *dev,
1447 struct drm_atomic_state *old_state)
1449 struct drm_crtc *crtc;
1452 for (i = 0; i < dev->mode_config.num_crtc; i++) {
1453 struct drm_crtc_commit *commit = old_state->crtcs[i].commit;
1456 crtc = old_state->crtcs[i].ptr;
1458 if (!crtc || !commit)
1461 ret = wait_for_completion_timeout(&commit->flip_done, 10 * HZ);
1463 DRM_ERROR("[CRTC:%d:%s] flip_done timed out\n",
1464 crtc->base.id, crtc->name);
1467 if (old_state->fake_commit)
1468 complete_all(&old_state->fake_commit->flip_done);
1470 EXPORT_SYMBOL(drm_atomic_helper_wait_for_flip_done);
1473 * drm_atomic_helper_commit_tail - commit atomic update to hardware
1474 * @old_state: atomic state object with old state structures
1476 * This is the default implementation for the
1477 * &drm_mode_config_helper_funcs.atomic_commit_tail hook, for drivers
1478 * that do not support runtime_pm or do not need the CRTC to be
1479 * enabled to perform a commit. Otherwise, see
1480 * drm_atomic_helper_commit_tail_rpm().
1482 * Note that the default ordering of how the various stages are called is to
1483 * match the legacy modeset helper library closest.
1485 void drm_atomic_helper_commit_tail(struct drm_atomic_state *old_state)
1487 struct drm_device *dev = old_state->dev;
1489 drm_atomic_helper_commit_modeset_disables(dev, old_state);
1491 drm_atomic_helper_commit_planes(dev, old_state, 0);
1493 drm_atomic_helper_commit_modeset_enables(dev, old_state);
1495 drm_atomic_helper_fake_vblank(old_state);
1497 drm_atomic_helper_commit_hw_done(old_state);
1499 drm_atomic_helper_wait_for_vblanks(dev, old_state);
1501 drm_atomic_helper_cleanup_planes(dev, old_state);
1503 EXPORT_SYMBOL(drm_atomic_helper_commit_tail);
1506 * drm_atomic_helper_commit_tail_rpm - commit atomic update to hardware
1507 * @old_state: new modeset state to be committed
1509 * This is an alternative implementation for the
1510 * &drm_mode_config_helper_funcs.atomic_commit_tail hook, for drivers
1511 * that support runtime_pm or need the CRTC to be enabled to perform a
1512 * commit. Otherwise, one should use the default implementation
1513 * drm_atomic_helper_commit_tail().
1515 void drm_atomic_helper_commit_tail_rpm(struct drm_atomic_state *old_state)
1517 struct drm_device *dev = old_state->dev;
1519 drm_atomic_helper_commit_modeset_disables(dev, old_state);
1521 drm_atomic_helper_commit_modeset_enables(dev, old_state);
1523 drm_atomic_helper_commit_planes(dev, old_state,
1524 DRM_PLANE_COMMIT_ACTIVE_ONLY);
1526 drm_atomic_helper_fake_vblank(old_state);
1528 drm_atomic_helper_commit_hw_done(old_state);
1530 drm_atomic_helper_wait_for_vblanks(dev, old_state);
1532 drm_atomic_helper_cleanup_planes(dev, old_state);
1534 EXPORT_SYMBOL(drm_atomic_helper_commit_tail_rpm);
1536 static void commit_tail(struct drm_atomic_state *old_state)
1538 struct drm_device *dev = old_state->dev;
1539 const struct drm_mode_config_helper_funcs *funcs;
1541 funcs = dev->mode_config.helper_private;
1543 drm_atomic_helper_wait_for_fences(dev, old_state, false);
1545 drm_atomic_helper_wait_for_dependencies(old_state);
1547 if (funcs && funcs->atomic_commit_tail)
1548 funcs->atomic_commit_tail(old_state);
1550 drm_atomic_helper_commit_tail(old_state);
1552 drm_atomic_helper_commit_cleanup_done(old_state);
1554 drm_atomic_state_put(old_state);
1557 static void commit_work(struct work_struct *work)
1559 struct drm_atomic_state *state = container_of(work,
1560 struct drm_atomic_state,
1566 * drm_atomic_helper_async_check - check if state can be commited asynchronously
1568 * @state: the driver state object
1570 * This helper will check if it is possible to commit the state asynchronously.
1571 * Async commits are not supposed to swap the states like normal sync commits
1572 * but just do in-place changes on the current state.
1574 * It will return 0 if the commit can happen in an asynchronous fashion or error
1575 * if not. Note that error just mean it can't be commited asynchronously, if it
1576 * fails the commit should be treated like a normal synchronous commit.
1578 int drm_atomic_helper_async_check(struct drm_device *dev,
1579 struct drm_atomic_state *state)
1581 struct drm_crtc *crtc;
1582 struct drm_crtc_state *crtc_state;
1583 struct drm_plane *plane = NULL;
1584 struct drm_plane_state *old_plane_state = NULL;
1585 struct drm_plane_state *new_plane_state = NULL;
1586 const struct drm_plane_helper_funcs *funcs;
1587 int i, n_planes = 0;
1589 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
1590 if (drm_atomic_crtc_needs_modeset(crtc_state))
1594 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i)
1597 /* FIXME: we support only single plane updates for now */
1601 if (!new_plane_state->crtc ||
1602 old_plane_state->crtc != new_plane_state->crtc)
1605 funcs = plane->helper_private;
1606 if (!funcs->atomic_async_update)
1609 if (new_plane_state->fence)
1613 * Don't do an async update if there is an outstanding commit modifying
1614 * the plane. This prevents our async update's changes from getting
1615 * overridden by a previous synchronous update's state.
1617 if (old_plane_state->commit &&
1618 !try_wait_for_completion(&old_plane_state->commit->hw_done))
1621 return funcs->atomic_async_check(plane, new_plane_state);
1623 EXPORT_SYMBOL(drm_atomic_helper_async_check);
1626 * drm_atomic_helper_async_commit - commit state asynchronously
1628 * @state: the driver state object
1630 * This function commits a state asynchronously, i.e., not vblank
1631 * synchronized. It should be used on a state only when
1632 * drm_atomic_async_check() succeeds. Async commits are not supposed to swap
1633 * the states like normal sync commits, but just do in-place changes on the
1636 void drm_atomic_helper_async_commit(struct drm_device *dev,
1637 struct drm_atomic_state *state)
1639 struct drm_plane *plane;
1640 struct drm_plane_state *plane_state;
1641 const struct drm_plane_helper_funcs *funcs;
1644 for_each_new_plane_in_state(state, plane, plane_state, i) {
1645 funcs = plane->helper_private;
1646 funcs->atomic_async_update(plane, plane_state);
1649 * ->atomic_async_update() is supposed to update the
1650 * plane->state in-place, make sure at least common
1651 * properties have been properly updated.
1653 WARN_ON_ONCE(plane->state->fb != plane_state->fb);
1654 WARN_ON_ONCE(plane->state->crtc_x != plane_state->crtc_x);
1655 WARN_ON_ONCE(plane->state->crtc_y != plane_state->crtc_y);
1656 WARN_ON_ONCE(plane->state->src_x != plane_state->src_x);
1657 WARN_ON_ONCE(plane->state->src_y != plane_state->src_y);
1660 EXPORT_SYMBOL(drm_atomic_helper_async_commit);
1663 * drm_atomic_helper_commit - commit validated state object
1665 * @state: the driver state object
1666 * @nonblock: whether nonblocking behavior is requested.
1668 * This function commits a with drm_atomic_helper_check() pre-validated state
1669 * object. This can still fail when e.g. the framebuffer reservation fails. This
1670 * function implements nonblocking commits, using
1671 * drm_atomic_helper_setup_commit() and related functions.
1673 * Committing the actual hardware state is done through the
1674 * &drm_mode_config_helper_funcs.atomic_commit_tail callback, or it's default
1675 * implementation drm_atomic_helper_commit_tail().
1678 * Zero for success or -errno.
1680 int drm_atomic_helper_commit(struct drm_device *dev,
1681 struct drm_atomic_state *state,
1686 if (state->async_update) {
1687 ret = drm_atomic_helper_prepare_planes(dev, state);
1691 drm_atomic_helper_async_commit(dev, state);
1692 drm_atomic_helper_cleanup_planes(dev, state);
1697 ret = drm_atomic_helper_setup_commit(state, nonblock);
1701 INIT_WORK(&state->commit_work, commit_work);
1703 ret = drm_atomic_helper_prepare_planes(dev, state);
1708 ret = drm_atomic_helper_wait_for_fences(dev, state, true);
1714 * This is the point of no return - everything below never fails except
1715 * when the hw goes bonghits. Which means we can commit the new state on
1716 * the software side now.
1719 ret = drm_atomic_helper_swap_state(state, true);
1724 * Everything below can be run asynchronously without the need to grab
1725 * any modeset locks at all under one condition: It must be guaranteed
1726 * that the asynchronous work has either been cancelled (if the driver
1727 * supports it, which at least requires that the framebuffers get
1728 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
1729 * before the new state gets committed on the software side with
1730 * drm_atomic_helper_swap_state().
1732 * This scheme allows new atomic state updates to be prepared and
1733 * checked in parallel to the asynchronous completion of the previous
1734 * update. Which is important since compositors need to figure out the
1735 * composition of the next frame right after having submitted the
1738 * NOTE: Commit work has multiple phases, first hardware commit, then
1739 * cleanup. We want them to overlap, hence need system_unbound_wq to
1740 * make sure work items don't artifically stall on each another.
1743 drm_atomic_state_get(state);
1745 queue_work(system_unbound_wq, &state->commit_work);
1752 drm_atomic_helper_cleanup_planes(dev, state);
1755 EXPORT_SYMBOL(drm_atomic_helper_commit);
1758 * DOC: implementing nonblocking commit
1760 * Nonblocking atomic commits have to be implemented in the following sequence:
1762 * 1. Run drm_atomic_helper_prepare_planes() first. This is the only function
1763 * which commit needs to call which can fail, so we want to run it first and
1766 * 2. Synchronize with any outstanding nonblocking commit worker threads which
1767 * might be affected the new state update. This can be done by either cancelling
1768 * or flushing the work items, depending upon whether the driver can deal with
1769 * cancelled updates. Note that it is important to ensure that the framebuffer
1770 * cleanup is still done when cancelling.
1772 * Asynchronous workers need to have sufficient parallelism to be able to run
1773 * different atomic commits on different CRTCs in parallel. The simplest way to
1774 * achive this is by running them on the &system_unbound_wq work queue. Note
1775 * that drivers are not required to split up atomic commits and run an
1776 * individual commit in parallel - userspace is supposed to do that if it cares.
1777 * But it might be beneficial to do that for modesets, since those necessarily
1778 * must be done as one global operation, and enabling or disabling a CRTC can
1779 * take a long time. But even that is not required.
1781 * 3. The software state is updated synchronously with
1782 * drm_atomic_helper_swap_state(). Doing this under the protection of all modeset
1783 * locks means concurrent callers never see inconsistent state. And doing this
1784 * while it's guaranteed that no relevant nonblocking worker runs means that
1785 * nonblocking workers do not need grab any locks. Actually they must not grab
1786 * locks, for otherwise the work flushing will deadlock.
1788 * 4. Schedule a work item to do all subsequent steps, using the split-out
1789 * commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and
1790 * then cleaning up the framebuffers after the old framebuffer is no longer
1793 * The above scheme is implemented in the atomic helper libraries in
1794 * drm_atomic_helper_commit() using a bunch of helper functions. See
1795 * drm_atomic_helper_setup_commit() for a starting point.
1798 static int stall_checks(struct drm_crtc *crtc, bool nonblock)
1800 struct drm_crtc_commit *commit, *stall_commit = NULL;
1801 bool completed = true;
1805 spin_lock(&crtc->commit_lock);
1807 list_for_each_entry(commit, &crtc->commit_list, commit_entry) {
1809 completed = try_wait_for_completion(&commit->flip_done);
1810 /* Userspace is not allowed to get ahead of the previous
1811 * commit with nonblocking ones. */
1812 if (!completed && nonblock) {
1813 spin_unlock(&crtc->commit_lock);
1816 } else if (i == 1) {
1817 stall_commit = drm_crtc_commit_get(commit);
1823 spin_unlock(&crtc->commit_lock);
1828 /* We don't want to let commits get ahead of cleanup work too much,
1829 * stalling on 2nd previous commit means triple-buffer won't ever stall.
1831 ret = wait_for_completion_interruptible_timeout(&stall_commit->cleanup_done,
1834 DRM_ERROR("[CRTC:%d:%s] cleanup_done timed out\n",
1835 crtc->base.id, crtc->name);
1837 drm_crtc_commit_put(stall_commit);
1839 return ret < 0 ? ret : 0;
1842 static void release_crtc_commit(struct completion *completion)
1844 struct drm_crtc_commit *commit = container_of(completion,
1848 drm_crtc_commit_put(commit);
1851 static void init_commit(struct drm_crtc_commit *commit, struct drm_crtc *crtc)
1853 init_completion(&commit->flip_done);
1854 init_completion(&commit->hw_done);
1855 init_completion(&commit->cleanup_done);
1856 INIT_LIST_HEAD(&commit->commit_entry);
1857 kref_init(&commit->ref);
1858 commit->crtc = crtc;
1861 static struct drm_crtc_commit *
1862 crtc_or_fake_commit(struct drm_atomic_state *state, struct drm_crtc *crtc)
1865 struct drm_crtc_state *new_crtc_state;
1867 new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
1869 return new_crtc_state->commit;
1872 if (!state->fake_commit) {
1873 state->fake_commit = kzalloc(sizeof(*state->fake_commit), GFP_KERNEL);
1874 if (!state->fake_commit)
1877 init_commit(state->fake_commit, NULL);
1880 return state->fake_commit;
1884 * drm_atomic_helper_setup_commit - setup possibly nonblocking commit
1885 * @state: new modeset state to be committed
1886 * @nonblock: whether nonblocking behavior is requested.
1888 * This function prepares @state to be used by the atomic helper's support for
1889 * nonblocking commits. Drivers using the nonblocking commit infrastructure
1890 * should always call this function from their
1891 * &drm_mode_config_funcs.atomic_commit hook.
1893 * To be able to use this support drivers need to use a few more helper
1894 * functions. drm_atomic_helper_wait_for_dependencies() must be called before
1895 * actually committing the hardware state, and for nonblocking commits this call
1896 * must be placed in the async worker. See also drm_atomic_helper_swap_state()
1897 * and it's stall parameter, for when a driver's commit hooks look at the
1898 * &drm_crtc.state, &drm_plane.state or &drm_connector.state pointer directly.
1900 * Completion of the hardware commit step must be signalled using
1901 * drm_atomic_helper_commit_hw_done(). After this step the driver is not allowed
1902 * to read or change any permanent software or hardware modeset state. The only
1903 * exception is state protected by other means than &drm_modeset_lock locks.
1904 * Only the free standing @state with pointers to the old state structures can
1905 * be inspected, e.g. to clean up old buffers using
1906 * drm_atomic_helper_cleanup_planes().
1908 * At the very end, before cleaning up @state drivers must call
1909 * drm_atomic_helper_commit_cleanup_done().
1911 * This is all implemented by in drm_atomic_helper_commit(), giving drivers a
1912 * complete and easy-to-use default implementation of the atomic_commit() hook.
1914 * The tracking of asynchronously executed and still pending commits is done
1915 * using the core structure &drm_crtc_commit.
1917 * By default there's no need to clean up resources allocated by this function
1918 * explicitly: drm_atomic_state_default_clear() will take care of that
1923 * 0 on success. -EBUSY when userspace schedules nonblocking commits too fast,
1924 * -ENOMEM on allocation failures and -EINTR when a signal is pending.
1926 int drm_atomic_helper_setup_commit(struct drm_atomic_state *state,
1929 struct drm_crtc *crtc;
1930 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
1931 struct drm_connector *conn;
1932 struct drm_connector_state *old_conn_state, *new_conn_state;
1933 struct drm_plane *plane;
1934 struct drm_plane_state *old_plane_state, *new_plane_state;
1935 struct drm_crtc_commit *commit;
1938 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
1939 commit = kzalloc(sizeof(*commit), GFP_KERNEL);
1943 init_commit(commit, crtc);
1945 new_crtc_state->commit = commit;
1947 ret = stall_checks(crtc, nonblock);
1951 /* Drivers only send out events when at least either current or
1952 * new CRTC state is active. Complete right away if everything
1954 if (!old_crtc_state->active && !new_crtc_state->active) {
1955 complete_all(&commit->flip_done);
1959 /* Legacy cursor updates are fully unsynced. */
1960 if (state->legacy_cursor_update) {
1961 complete_all(&commit->flip_done);
1965 if (!new_crtc_state->event) {
1966 commit->event = kzalloc(sizeof(*commit->event),
1971 new_crtc_state->event = commit->event;
1974 new_crtc_state->event->base.completion = &commit->flip_done;
1975 new_crtc_state->event->base.completion_release = release_crtc_commit;
1976 drm_crtc_commit_get(commit);
1978 commit->abort_completion = true;
1980 state->crtcs[i].commit = commit;
1981 drm_crtc_commit_get(commit);
1984 for_each_oldnew_connector_in_state(state, conn, old_conn_state, new_conn_state, i) {
1985 /* Userspace is not allowed to get ahead of the previous
1986 * commit with nonblocking ones. */
1987 if (nonblock && old_conn_state->commit &&
1988 !try_wait_for_completion(&old_conn_state->commit->flip_done))
1991 /* Always track connectors explicitly for e.g. link retraining. */
1992 commit = crtc_or_fake_commit(state, new_conn_state->crtc ?: old_conn_state->crtc);
1996 new_conn_state->commit = drm_crtc_commit_get(commit);
1999 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
2000 /* Userspace is not allowed to get ahead of the previous
2001 * commit with nonblocking ones. */
2002 if (nonblock && old_plane_state->commit &&
2003 !try_wait_for_completion(&old_plane_state->commit->flip_done))
2006 /* Always track planes explicitly for async pageflip support. */
2007 commit = crtc_or_fake_commit(state, new_plane_state->crtc ?: old_plane_state->crtc);
2011 new_plane_state->commit = drm_crtc_commit_get(commit);
2016 EXPORT_SYMBOL(drm_atomic_helper_setup_commit);
2019 * drm_atomic_helper_wait_for_dependencies - wait for required preceeding commits
2020 * @old_state: atomic state object with old state structures
2022 * This function waits for all preceeding commits that touch the same CRTC as
2023 * @old_state to both be committed to the hardware (as signalled by
2024 * drm_atomic_helper_commit_hw_done) and executed by the hardware (as signalled
2025 * by calling drm_crtc_send_vblank_event() on the &drm_crtc_state.event).
2027 * This is part of the atomic helper support for nonblocking commits, see
2028 * drm_atomic_helper_setup_commit() for an overview.
2030 void drm_atomic_helper_wait_for_dependencies(struct drm_atomic_state *old_state)
2032 struct drm_crtc *crtc;
2033 struct drm_crtc_state *old_crtc_state;
2034 struct drm_plane *plane;
2035 struct drm_plane_state *old_plane_state;
2036 struct drm_connector *conn;
2037 struct drm_connector_state *old_conn_state;
2038 struct drm_crtc_commit *commit;
2042 for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
2043 commit = old_crtc_state->commit;
2048 ret = wait_for_completion_timeout(&commit->hw_done,
2051 DRM_ERROR("[CRTC:%d:%s] hw_done timed out\n",
2052 crtc->base.id, crtc->name);
2054 /* Currently no support for overwriting flips, hence
2055 * stall for previous one to execute completely. */
2056 ret = wait_for_completion_timeout(&commit->flip_done,
2059 DRM_ERROR("[CRTC:%d:%s] flip_done timed out\n",
2060 crtc->base.id, crtc->name);
2063 for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
2064 commit = old_conn_state->commit;
2069 ret = wait_for_completion_timeout(&commit->hw_done,
2072 DRM_ERROR("[CONNECTOR:%d:%s] hw_done timed out\n",
2073 conn->base.id, conn->name);
2075 /* Currently no support for overwriting flips, hence
2076 * stall for previous one to execute completely. */
2077 ret = wait_for_completion_timeout(&commit->flip_done,
2080 DRM_ERROR("[CONNECTOR:%d:%s] flip_done timed out\n",
2081 conn->base.id, conn->name);
2084 for_each_old_plane_in_state(old_state, plane, old_plane_state, i) {
2085 commit = old_plane_state->commit;
2090 ret = wait_for_completion_timeout(&commit->hw_done,
2093 DRM_ERROR("[PLANE:%d:%s] hw_done timed out\n",
2094 plane->base.id, plane->name);
2096 /* Currently no support for overwriting flips, hence
2097 * stall for previous one to execute completely. */
2098 ret = wait_for_completion_timeout(&commit->flip_done,
2101 DRM_ERROR("[PLANE:%d:%s] flip_done timed out\n",
2102 plane->base.id, plane->name);
2105 EXPORT_SYMBOL(drm_atomic_helper_wait_for_dependencies);
2108 * drm_atomic_helper_fake_vblank - fake VBLANK events if needed
2109 * @old_state: atomic state object with old state structures
2111 * This function walks all CRTCs and fake VBLANK events on those with
2112 * &drm_crtc_state.no_vblank set to true and &drm_crtc_state.event != NULL.
2113 * The primary use of this function is writeback connectors working in oneshot
2114 * mode and faking VBLANK events. In this case they only fake the VBLANK event
2115 * when a job is queued, and any change to the pipeline that does not touch the
2116 * connector is leading to timeouts when calling
2117 * drm_atomic_helper_wait_for_vblanks() or
2118 * drm_atomic_helper_wait_for_flip_done().
2120 * This is part of the atomic helper support for nonblocking commits, see
2121 * drm_atomic_helper_setup_commit() for an overview.
2123 void drm_atomic_helper_fake_vblank(struct drm_atomic_state *old_state)
2125 struct drm_crtc_state *new_crtc_state;
2126 struct drm_crtc *crtc;
2129 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
2130 unsigned long flags;
2132 if (!new_crtc_state->no_vblank)
2135 spin_lock_irqsave(&old_state->dev->event_lock, flags);
2136 if (new_crtc_state->event) {
2137 drm_crtc_send_vblank_event(crtc,
2138 new_crtc_state->event);
2139 new_crtc_state->event = NULL;
2141 spin_unlock_irqrestore(&old_state->dev->event_lock, flags);
2144 EXPORT_SYMBOL(drm_atomic_helper_fake_vblank);
2147 * drm_atomic_helper_commit_hw_done - setup possible nonblocking commit
2148 * @old_state: atomic state object with old state structures
2150 * This function is used to signal completion of the hardware commit step. After
2151 * this step the driver is not allowed to read or change any permanent software
2152 * or hardware modeset state. The only exception is state protected by other
2153 * means than &drm_modeset_lock locks.
2155 * Drivers should try to postpone any expensive or delayed cleanup work after
2156 * this function is called.
2158 * This is part of the atomic helper support for nonblocking commits, see
2159 * drm_atomic_helper_setup_commit() for an overview.
2161 void drm_atomic_helper_commit_hw_done(struct drm_atomic_state *old_state)
2163 struct drm_crtc *crtc;
2164 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
2165 struct drm_crtc_commit *commit;
2168 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
2169 commit = new_crtc_state->commit;
2174 * copy new_crtc_state->commit to old_crtc_state->commit,
2175 * it's unsafe to touch new_crtc_state after hw_done,
2176 * but we still need to do so in cleanup_done().
2178 if (old_crtc_state->commit)
2179 drm_crtc_commit_put(old_crtc_state->commit);
2181 old_crtc_state->commit = drm_crtc_commit_get(commit);
2183 /* backend must have consumed any event by now */
2184 WARN_ON(new_crtc_state->event);
2185 complete_all(&commit->hw_done);
2188 if (old_state->fake_commit) {
2189 complete_all(&old_state->fake_commit->hw_done);
2190 complete_all(&old_state->fake_commit->flip_done);
2193 EXPORT_SYMBOL(drm_atomic_helper_commit_hw_done);
2196 * drm_atomic_helper_commit_cleanup_done - signal completion of commit
2197 * @old_state: atomic state object with old state structures
2199 * This signals completion of the atomic update @old_state, including any
2200 * cleanup work. If used, it must be called right before calling
2201 * drm_atomic_state_put().
2203 * This is part of the atomic helper support for nonblocking commits, see
2204 * drm_atomic_helper_setup_commit() for an overview.
2206 void drm_atomic_helper_commit_cleanup_done(struct drm_atomic_state *old_state)
2208 struct drm_crtc *crtc;
2209 struct drm_crtc_state *old_crtc_state;
2210 struct drm_crtc_commit *commit;
2213 for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
2214 commit = old_crtc_state->commit;
2215 if (WARN_ON(!commit))
2218 complete_all(&commit->cleanup_done);
2219 WARN_ON(!try_wait_for_completion(&commit->hw_done));
2221 spin_lock(&crtc->commit_lock);
2222 list_del(&commit->commit_entry);
2223 spin_unlock(&crtc->commit_lock);
2226 if (old_state->fake_commit) {
2227 complete_all(&old_state->fake_commit->cleanup_done);
2228 WARN_ON(!try_wait_for_completion(&old_state->fake_commit->hw_done));
2231 EXPORT_SYMBOL(drm_atomic_helper_commit_cleanup_done);
2234 * drm_atomic_helper_prepare_planes - prepare plane resources before commit
2236 * @state: atomic state object with new state structures
2238 * This function prepares plane state, specifically framebuffers, for the new
2239 * configuration, by calling &drm_plane_helper_funcs.prepare_fb. If any failure
2240 * is encountered this function will call &drm_plane_helper_funcs.cleanup_fb on
2241 * any already successfully prepared framebuffer.
2244 * 0 on success, negative error code on failure.
2246 int drm_atomic_helper_prepare_planes(struct drm_device *dev,
2247 struct drm_atomic_state *state)
2249 struct drm_plane *plane;
2250 struct drm_plane_state *new_plane_state;
2253 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
2254 const struct drm_plane_helper_funcs *funcs;
2256 funcs = plane->helper_private;
2258 if (funcs->prepare_fb) {
2259 ret = funcs->prepare_fb(plane, new_plane_state);
2268 for_each_new_plane_in_state(state, plane, new_plane_state, j) {
2269 const struct drm_plane_helper_funcs *funcs;
2274 funcs = plane->helper_private;
2276 if (funcs->cleanup_fb)
2277 funcs->cleanup_fb(plane, new_plane_state);
2282 EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
2284 static bool plane_crtc_active(const struct drm_plane_state *state)
2286 return state->crtc && state->crtc->state->active;
2290 * drm_atomic_helper_commit_planes - commit plane state
2292 * @old_state: atomic state object with old state structures
2293 * @flags: flags for committing plane state
2295 * This function commits the new plane state using the plane and atomic helper
2296 * functions for planes and crtcs. It assumes that the atomic state has already
2297 * been pushed into the relevant object state pointers, since this step can no
2300 * It still requires the global state object @old_state to know which planes and
2301 * crtcs need to be updated though.
2303 * Note that this function does all plane updates across all CRTCs in one step.
2304 * If the hardware can't support this approach look at
2305 * drm_atomic_helper_commit_planes_on_crtc() instead.
2307 * Plane parameters can be updated by applications while the associated CRTC is
2308 * disabled. The DRM/KMS core will store the parameters in the plane state,
2309 * which will be available to the driver when the CRTC is turned on. As a result
2310 * most drivers don't need to be immediately notified of plane updates for a
2313 * Unless otherwise needed, drivers are advised to set the ACTIVE_ONLY flag in
2314 * @flags in order not to receive plane update notifications related to a
2315 * disabled CRTC. This avoids the need to manually ignore plane updates in
2316 * driver code when the driver and/or hardware can't or just don't need to deal
2317 * with updates on disabled CRTCs, for example when supporting runtime PM.
2319 * Drivers may set the NO_DISABLE_AFTER_MODESET flag in @flags if the relevant
2320 * display controllers require to disable a CRTC's planes when the CRTC is
2321 * disabled. This function would skip the &drm_plane_helper_funcs.atomic_disable
2322 * call for a plane if the CRTC of the old plane state needs a modesetting
2323 * operation. Of course, the drivers need to disable the planes in their CRTC
2324 * disable callbacks since no one else would do that.
2326 * The drm_atomic_helper_commit() default implementation doesn't set the
2327 * ACTIVE_ONLY flag to most closely match the behaviour of the legacy helpers.
2328 * This should not be copied blindly by drivers.
2330 void drm_atomic_helper_commit_planes(struct drm_device *dev,
2331 struct drm_atomic_state *old_state,
2334 struct drm_crtc *crtc;
2335 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
2336 struct drm_plane *plane;
2337 struct drm_plane_state *old_plane_state, *new_plane_state;
2339 bool active_only = flags & DRM_PLANE_COMMIT_ACTIVE_ONLY;
2340 bool no_disable = flags & DRM_PLANE_COMMIT_NO_DISABLE_AFTER_MODESET;
2342 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
2343 const struct drm_crtc_helper_funcs *funcs;
2345 funcs = crtc->helper_private;
2347 if (!funcs || !funcs->atomic_begin)
2350 if (active_only && !new_crtc_state->active)
2353 funcs->atomic_begin(crtc, old_crtc_state);
2356 for_each_oldnew_plane_in_state(old_state, plane, old_plane_state, new_plane_state, i) {
2357 const struct drm_plane_helper_funcs *funcs;
2360 funcs = plane->helper_private;
2365 disabling = drm_atomic_plane_disabling(old_plane_state,
2370 * Skip planes related to inactive CRTCs. If the plane
2371 * is enabled use the state of the current CRTC. If the
2372 * plane is being disabled use the state of the old
2373 * CRTC to avoid skipping planes being disabled on an
2376 if (!disabling && !plane_crtc_active(new_plane_state))
2378 if (disabling && !plane_crtc_active(old_plane_state))
2383 * Special-case disabling the plane if drivers support it.
2385 if (disabling && funcs->atomic_disable) {
2386 struct drm_crtc_state *crtc_state;
2388 crtc_state = old_plane_state->crtc->state;
2390 if (drm_atomic_crtc_needs_modeset(crtc_state) &&
2394 funcs->atomic_disable(plane, old_plane_state);
2395 } else if (new_plane_state->crtc || disabling) {
2396 funcs->atomic_update(plane, old_plane_state);
2400 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
2401 const struct drm_crtc_helper_funcs *funcs;
2403 funcs = crtc->helper_private;
2405 if (!funcs || !funcs->atomic_flush)
2408 if (active_only && !new_crtc_state->active)
2411 funcs->atomic_flush(crtc, old_crtc_state);
2414 EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
2417 * drm_atomic_helper_commit_planes_on_crtc - commit plane state for a crtc
2418 * @old_crtc_state: atomic state object with the old crtc state
2420 * This function commits the new plane state using the plane and atomic helper
2421 * functions for planes on the specific crtc. It assumes that the atomic state
2422 * has already been pushed into the relevant object state pointers, since this
2423 * step can no longer fail.
2425 * This function is useful when plane updates should be done crtc-by-crtc
2426 * instead of one global step like drm_atomic_helper_commit_planes() does.
2428 * This function can only be savely used when planes are not allowed to move
2429 * between different CRTCs because this function doesn't handle inter-CRTC
2430 * depencies. Callers need to ensure that either no such depencies exist,
2431 * resolve them through ordering of commit calls or through some other means.
2434 drm_atomic_helper_commit_planes_on_crtc(struct drm_crtc_state *old_crtc_state)
2436 const struct drm_crtc_helper_funcs *crtc_funcs;
2437 struct drm_crtc *crtc = old_crtc_state->crtc;
2438 struct drm_atomic_state *old_state = old_crtc_state->state;
2439 struct drm_crtc_state *new_crtc_state =
2440 drm_atomic_get_new_crtc_state(old_state, crtc);
2441 struct drm_plane *plane;
2442 unsigned plane_mask;
2444 plane_mask = old_crtc_state->plane_mask;
2445 plane_mask |= new_crtc_state->plane_mask;
2447 crtc_funcs = crtc->helper_private;
2448 if (crtc_funcs && crtc_funcs->atomic_begin)
2449 crtc_funcs->atomic_begin(crtc, old_crtc_state);
2451 drm_for_each_plane_mask(plane, crtc->dev, plane_mask) {
2452 struct drm_plane_state *old_plane_state =
2453 drm_atomic_get_old_plane_state(old_state, plane);
2454 struct drm_plane_state *new_plane_state =
2455 drm_atomic_get_new_plane_state(old_state, plane);
2456 const struct drm_plane_helper_funcs *plane_funcs;
2458 plane_funcs = plane->helper_private;
2460 if (!old_plane_state || !plane_funcs)
2463 WARN_ON(new_plane_state->crtc &&
2464 new_plane_state->crtc != crtc);
2466 if (drm_atomic_plane_disabling(old_plane_state, new_plane_state) &&
2467 plane_funcs->atomic_disable)
2468 plane_funcs->atomic_disable(plane, old_plane_state);
2469 else if (new_plane_state->crtc ||
2470 drm_atomic_plane_disabling(old_plane_state, new_plane_state))
2471 plane_funcs->atomic_update(plane, old_plane_state);
2474 if (crtc_funcs && crtc_funcs->atomic_flush)
2475 crtc_funcs->atomic_flush(crtc, old_crtc_state);
2477 EXPORT_SYMBOL(drm_atomic_helper_commit_planes_on_crtc);
2480 * drm_atomic_helper_disable_planes_on_crtc - helper to disable CRTC's planes
2481 * @old_crtc_state: atomic state object with the old CRTC state
2482 * @atomic: if set, synchronize with CRTC's atomic_begin/flush hooks
2484 * Disables all planes associated with the given CRTC. This can be
2485 * used for instance in the CRTC helper atomic_disable callback to disable
2488 * If the atomic-parameter is set the function calls the CRTC's
2489 * atomic_begin hook before and atomic_flush hook after disabling the
2492 * It is a bug to call this function without having implemented the
2493 * &drm_plane_helper_funcs.atomic_disable plane hook.
2496 drm_atomic_helper_disable_planes_on_crtc(struct drm_crtc_state *old_crtc_state,
2499 struct drm_crtc *crtc = old_crtc_state->crtc;
2500 const struct drm_crtc_helper_funcs *crtc_funcs =
2501 crtc->helper_private;
2502 struct drm_plane *plane;
2504 if (atomic && crtc_funcs && crtc_funcs->atomic_begin)
2505 crtc_funcs->atomic_begin(crtc, NULL);
2507 drm_atomic_crtc_state_for_each_plane(plane, old_crtc_state) {
2508 const struct drm_plane_helper_funcs *plane_funcs =
2509 plane->helper_private;
2514 WARN_ON(!plane_funcs->atomic_disable);
2515 if (plane_funcs->atomic_disable)
2516 plane_funcs->atomic_disable(plane, NULL);
2519 if (atomic && crtc_funcs && crtc_funcs->atomic_flush)
2520 crtc_funcs->atomic_flush(crtc, NULL);
2522 EXPORT_SYMBOL(drm_atomic_helper_disable_planes_on_crtc);
2525 * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
2527 * @old_state: atomic state object with old state structures
2529 * This function cleans up plane state, specifically framebuffers, from the old
2530 * configuration. Hence the old configuration must be perserved in @old_state to
2531 * be able to call this function.
2533 * This function must also be called on the new state when the atomic update
2534 * fails at any point after calling drm_atomic_helper_prepare_planes().
2536 void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
2537 struct drm_atomic_state *old_state)
2539 struct drm_plane *plane;
2540 struct drm_plane_state *old_plane_state, *new_plane_state;
2543 for_each_oldnew_plane_in_state(old_state, plane, old_plane_state, new_plane_state, i) {
2544 const struct drm_plane_helper_funcs *funcs;
2545 struct drm_plane_state *plane_state;
2548 * This might be called before swapping when commit is aborted,
2549 * in which case we have to cleanup the new state.
2551 if (old_plane_state == plane->state)
2552 plane_state = new_plane_state;
2554 plane_state = old_plane_state;
2556 funcs = plane->helper_private;
2558 if (funcs->cleanup_fb)
2559 funcs->cleanup_fb(plane, plane_state);
2562 EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
2565 * drm_atomic_helper_swap_state - store atomic state into current sw state
2566 * @state: atomic state
2567 * @stall: stall for preceeding commits
2569 * This function stores the atomic state into the current state pointers in all
2570 * driver objects. It should be called after all failing steps have been done
2571 * and succeeded, but before the actual hardware state is committed.
2573 * For cleanup and error recovery the current state for all changed objects will
2574 * be swapped into @state.
2576 * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
2578 * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
2580 * 2. Do any other steps that might fail.
2582 * 3. Put the staged state into the current state pointers with this function.
2584 * 4. Actually commit the hardware state.
2586 * 5. Call drm_atomic_helper_cleanup_planes() with @state, which since step 3
2587 * contains the old state. Also do any other cleanup required with that state.
2589 * @stall must be set when nonblocking commits for this driver directly access
2590 * the &drm_plane.state, &drm_crtc.state or &drm_connector.state pointer. With
2591 * the current atomic helpers this is almost always the case, since the helpers
2592 * don't pass the right state structures to the callbacks.
2596 * Returns 0 on success. Can return -ERESTARTSYS when @stall is true and the
2597 * waiting for the previous commits has been interrupted.
2599 int drm_atomic_helper_swap_state(struct drm_atomic_state *state,
2603 struct drm_connector *connector;
2604 struct drm_connector_state *old_conn_state, *new_conn_state;
2605 struct drm_crtc *crtc;
2606 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
2607 struct drm_plane *plane;
2608 struct drm_plane_state *old_plane_state, *new_plane_state;
2609 struct drm_crtc_commit *commit;
2610 struct drm_private_obj *obj;
2611 struct drm_private_state *old_obj_state, *new_obj_state;
2615 * We have to stall for hw_done here before
2616 * drm_atomic_helper_wait_for_dependencies() because flip
2617 * depth > 1 is not yet supported by all drivers. As long as
2618 * obj->state is directly dereferenced anywhere in the drivers
2619 * atomic_commit_tail function, then it's unsafe to swap state
2620 * before drm_atomic_helper_commit_hw_done() is called.
2623 for_each_old_crtc_in_state(state, crtc, old_crtc_state, i) {
2624 commit = old_crtc_state->commit;
2629 ret = wait_for_completion_interruptible(&commit->hw_done);
2634 for_each_old_connector_in_state(state, connector, old_conn_state, i) {
2635 commit = old_conn_state->commit;
2640 ret = wait_for_completion_interruptible(&commit->hw_done);
2645 for_each_old_plane_in_state(state, plane, old_plane_state, i) {
2646 commit = old_plane_state->commit;
2651 ret = wait_for_completion_interruptible(&commit->hw_done);
2657 for_each_oldnew_connector_in_state(state, connector, old_conn_state, new_conn_state, i) {
2658 WARN_ON(connector->state != old_conn_state);
2660 old_conn_state->state = state;
2661 new_conn_state->state = NULL;
2663 state->connectors[i].state = old_conn_state;
2664 connector->state = new_conn_state;
2667 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
2668 WARN_ON(crtc->state != old_crtc_state);
2670 old_crtc_state->state = state;
2671 new_crtc_state->state = NULL;
2673 state->crtcs[i].state = old_crtc_state;
2674 crtc->state = new_crtc_state;
2676 if (new_crtc_state->commit) {
2677 spin_lock(&crtc->commit_lock);
2678 list_add(&new_crtc_state->commit->commit_entry,
2679 &crtc->commit_list);
2680 spin_unlock(&crtc->commit_lock);
2682 new_crtc_state->commit->event = NULL;
2686 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
2687 WARN_ON(plane->state != old_plane_state);
2689 old_plane_state->state = state;
2690 new_plane_state->state = NULL;
2692 state->planes[i].state = old_plane_state;
2693 plane->state = new_plane_state;
2696 for_each_oldnew_private_obj_in_state(state, obj, old_obj_state, new_obj_state, i) {
2697 WARN_ON(obj->state != old_obj_state);
2699 old_obj_state->state = state;
2700 new_obj_state->state = NULL;
2702 state->private_objs[i].state = old_obj_state;
2703 obj->state = new_obj_state;
2708 EXPORT_SYMBOL(drm_atomic_helper_swap_state);
2711 * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
2712 * @plane: plane object to update
2713 * @crtc: owning CRTC of owning plane
2714 * @fb: framebuffer to flip onto plane
2715 * @crtc_x: x offset of primary plane on crtc
2716 * @crtc_y: y offset of primary plane on crtc
2717 * @crtc_w: width of primary plane rectangle on crtc
2718 * @crtc_h: height of primary plane rectangle on crtc
2719 * @src_x: x offset of @fb for panning
2720 * @src_y: y offset of @fb for panning
2721 * @src_w: width of source rectangle in @fb
2722 * @src_h: height of source rectangle in @fb
2723 * @ctx: lock acquire context
2725 * Provides a default plane update handler using the atomic driver interface.
2728 * Zero on success, error code on failure
2730 int drm_atomic_helper_update_plane(struct drm_plane *plane,
2731 struct drm_crtc *crtc,
2732 struct drm_framebuffer *fb,
2733 int crtc_x, int crtc_y,
2734 unsigned int crtc_w, unsigned int crtc_h,
2735 uint32_t src_x, uint32_t src_y,
2736 uint32_t src_w, uint32_t src_h,
2737 struct drm_modeset_acquire_ctx *ctx)
2739 struct drm_atomic_state *state;
2740 struct drm_plane_state *plane_state;
2743 state = drm_atomic_state_alloc(plane->dev);
2747 state->acquire_ctx = ctx;
2748 plane_state = drm_atomic_get_plane_state(state, plane);
2749 if (IS_ERR(plane_state)) {
2750 ret = PTR_ERR(plane_state);
2754 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
2757 drm_atomic_set_fb_for_plane(plane_state, fb);
2758 plane_state->crtc_x = crtc_x;
2759 plane_state->crtc_y = crtc_y;
2760 plane_state->crtc_w = crtc_w;
2761 plane_state->crtc_h = crtc_h;
2762 plane_state->src_x = src_x;
2763 plane_state->src_y = src_y;
2764 plane_state->src_w = src_w;
2765 plane_state->src_h = src_h;
2767 if (plane == crtc->cursor)
2768 state->legacy_cursor_update = true;
2770 ret = drm_atomic_commit(state);
2772 drm_atomic_state_put(state);
2775 EXPORT_SYMBOL(drm_atomic_helper_update_plane);
2778 * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
2779 * @plane: plane to disable
2780 * @ctx: lock acquire context
2782 * Provides a default plane disable handler using the atomic driver interface.
2785 * Zero on success, error code on failure
2787 int drm_atomic_helper_disable_plane(struct drm_plane *plane,
2788 struct drm_modeset_acquire_ctx *ctx)
2790 struct drm_atomic_state *state;
2791 struct drm_plane_state *plane_state;
2794 state = drm_atomic_state_alloc(plane->dev);
2798 state->acquire_ctx = ctx;
2799 plane_state = drm_atomic_get_plane_state(state, plane);
2800 if (IS_ERR(plane_state)) {
2801 ret = PTR_ERR(plane_state);
2805 if (plane_state->crtc && plane_state->crtc->cursor == plane)
2806 plane_state->state->legacy_cursor_update = true;
2808 ret = __drm_atomic_helper_disable_plane(plane, plane_state);
2812 ret = drm_atomic_commit(state);
2814 drm_atomic_state_put(state);
2817 EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
2819 /* just used from fb-helper and atomic-helper: */
2820 int __drm_atomic_helper_disable_plane(struct drm_plane *plane,
2821 struct drm_plane_state *plane_state)
2825 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
2829 drm_atomic_set_fb_for_plane(plane_state, NULL);
2830 plane_state->crtc_x = 0;
2831 plane_state->crtc_y = 0;
2832 plane_state->crtc_w = 0;
2833 plane_state->crtc_h = 0;
2834 plane_state->src_x = 0;
2835 plane_state->src_y = 0;
2836 plane_state->src_w = 0;
2837 plane_state->src_h = 0;
2842 static int update_output_state(struct drm_atomic_state *state,
2843 struct drm_mode_set *set)
2845 struct drm_device *dev = set->crtc->dev;
2846 struct drm_crtc *crtc;
2847 struct drm_crtc_state *new_crtc_state;
2848 struct drm_connector *connector;
2849 struct drm_connector_state *new_conn_state;
2852 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
2853 state->acquire_ctx);
2857 /* First disable all connectors on the target crtc. */
2858 ret = drm_atomic_add_affected_connectors(state, set->crtc);
2862 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
2863 if (new_conn_state->crtc == set->crtc) {
2864 ret = drm_atomic_set_crtc_for_connector(new_conn_state,
2869 /* Make sure legacy setCrtc always re-trains */
2870 new_conn_state->link_status = DRM_LINK_STATUS_GOOD;
2874 /* Then set all connectors from set->connectors on the target crtc */
2875 for (i = 0; i < set->num_connectors; i++) {
2876 new_conn_state = drm_atomic_get_connector_state(state,
2877 set->connectors[i]);
2878 if (IS_ERR(new_conn_state))
2879 return PTR_ERR(new_conn_state);
2881 ret = drm_atomic_set_crtc_for_connector(new_conn_state,
2887 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
2888 /* Don't update ->enable for the CRTC in the set_config request,
2889 * since a mismatch would indicate a bug in the upper layers.
2890 * The actual modeset code later on will catch any
2891 * inconsistencies here. */
2892 if (crtc == set->crtc)
2895 if (!new_crtc_state->connector_mask) {
2896 ret = drm_atomic_set_mode_prop_for_crtc(new_crtc_state,
2901 new_crtc_state->active = false;
2909 * drm_atomic_helper_set_config - set a new config from userspace
2910 * @set: mode set configuration
2911 * @ctx: lock acquisition context
2913 * Provides a default crtc set_config handler using the atomic driver interface.
2915 * NOTE: For backwards compatibility with old userspace this automatically
2916 * resets the "link-status" property to GOOD, to force any link
2917 * re-training. The SETCRTC ioctl does not define whether an update does
2918 * need a full modeset or just a plane update, hence we're allowed to do
2919 * that. See also drm_connector_set_link_status_property().
2922 * Returns 0 on success, negative errno numbers on failure.
2924 int drm_atomic_helper_set_config(struct drm_mode_set *set,
2925 struct drm_modeset_acquire_ctx *ctx)
2927 struct drm_atomic_state *state;
2928 struct drm_crtc *crtc = set->crtc;
2931 state = drm_atomic_state_alloc(crtc->dev);
2935 state->acquire_ctx = ctx;
2936 ret = __drm_atomic_helper_set_config(set, state);
2940 ret = handle_conflicting_encoders(state, true);
2944 ret = drm_atomic_commit(state);
2947 drm_atomic_state_put(state);
2950 EXPORT_SYMBOL(drm_atomic_helper_set_config);
2952 /* just used from fb-helper and atomic-helper: */
2953 int __drm_atomic_helper_set_config(struct drm_mode_set *set,
2954 struct drm_atomic_state *state)
2956 struct drm_crtc_state *crtc_state;
2957 struct drm_plane_state *primary_state;
2958 struct drm_crtc *crtc = set->crtc;
2959 int hdisplay, vdisplay;
2962 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2963 if (IS_ERR(crtc_state))
2964 return PTR_ERR(crtc_state);
2966 primary_state = drm_atomic_get_plane_state(state, crtc->primary);
2967 if (IS_ERR(primary_state))
2968 return PTR_ERR(primary_state);
2972 WARN_ON(set->num_connectors);
2974 ret = drm_atomic_set_mode_for_crtc(crtc_state, NULL);
2978 crtc_state->active = false;
2980 ret = drm_atomic_set_crtc_for_plane(primary_state, NULL);
2984 drm_atomic_set_fb_for_plane(primary_state, NULL);
2990 WARN_ON(!set->num_connectors);
2992 ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
2996 crtc_state->active = true;
2998 ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
3002 drm_mode_get_hv_timing(set->mode, &hdisplay, &vdisplay);
3004 drm_atomic_set_fb_for_plane(primary_state, set->fb);
3005 primary_state->crtc_x = 0;
3006 primary_state->crtc_y = 0;
3007 primary_state->crtc_w = hdisplay;
3008 primary_state->crtc_h = vdisplay;
3009 primary_state->src_x = set->x << 16;
3010 primary_state->src_y = set->y << 16;
3011 if (drm_rotation_90_or_270(primary_state->rotation)) {
3012 primary_state->src_w = vdisplay << 16;
3013 primary_state->src_h = hdisplay << 16;
3015 primary_state->src_w = hdisplay << 16;
3016 primary_state->src_h = vdisplay << 16;
3020 ret = update_output_state(state, set);
3027 static int __drm_atomic_helper_disable_all(struct drm_device *dev,
3028 struct drm_modeset_acquire_ctx *ctx,
3031 struct drm_atomic_state *state;
3032 struct drm_connector_state *conn_state;
3033 struct drm_connector *conn;
3034 struct drm_plane_state *plane_state;
3035 struct drm_plane *plane;
3036 struct drm_crtc_state *crtc_state;
3037 struct drm_crtc *crtc;
3040 state = drm_atomic_state_alloc(dev);
3044 state->acquire_ctx = ctx;
3046 drm_for_each_crtc(crtc, dev) {
3047 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3048 if (IS_ERR(crtc_state)) {
3049 ret = PTR_ERR(crtc_state);
3053 crtc_state->active = false;
3055 ret = drm_atomic_set_mode_prop_for_crtc(crtc_state, NULL);
3059 ret = drm_atomic_add_affected_planes(state, crtc);
3063 ret = drm_atomic_add_affected_connectors(state, crtc);
3068 for_each_new_connector_in_state(state, conn, conn_state, i) {
3069 ret = drm_atomic_set_crtc_for_connector(conn_state, NULL);
3074 for_each_new_plane_in_state(state, plane, plane_state, i) {
3075 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
3079 drm_atomic_set_fb_for_plane(plane_state, NULL);
3082 ret = drm_atomic_commit(state);
3084 drm_atomic_state_put(state);
3089 * drm_atomic_helper_disable_all - disable all currently active outputs
3091 * @ctx: lock acquisition context
3093 * Loops through all connectors, finding those that aren't turned off and then
3094 * turns them off by setting their DPMS mode to OFF and deactivating the CRTC
3095 * that they are connected to.
3097 * This is used for example in suspend/resume to disable all currently active
3098 * functions when suspending. If you just want to shut down everything at e.g.
3099 * driver unload, look at drm_atomic_helper_shutdown().
3101 * Note that if callers haven't already acquired all modeset locks this might
3102 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
3105 * 0 on success or a negative error code on failure.
3108 * drm_atomic_helper_suspend(), drm_atomic_helper_resume() and
3109 * drm_atomic_helper_shutdown().
3111 int drm_atomic_helper_disable_all(struct drm_device *dev,
3112 struct drm_modeset_acquire_ctx *ctx)
3114 return __drm_atomic_helper_disable_all(dev, ctx, false);
3116 EXPORT_SYMBOL(drm_atomic_helper_disable_all);
3119 * drm_atomic_helper_shutdown - shutdown all CRTC
3122 * This shuts down all CRTC, which is useful for driver unloading. Shutdown on
3123 * suspend should instead be handled with drm_atomic_helper_suspend(), since
3124 * that also takes a snapshot of the modeset state to be restored on resume.
3126 * This is just a convenience wrapper around drm_atomic_helper_disable_all(),
3127 * and it is the atomic version of drm_crtc_force_disable_all().
3129 void drm_atomic_helper_shutdown(struct drm_device *dev)
3131 struct drm_modeset_acquire_ctx ctx;
3134 DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx, 0, ret);
3136 ret = __drm_atomic_helper_disable_all(dev, &ctx, true);
3138 DRM_ERROR("Disabling all crtc's during unload failed with %i\n", ret);
3140 DRM_MODESET_LOCK_ALL_END(ctx, ret);
3142 EXPORT_SYMBOL(drm_atomic_helper_shutdown);
3145 * drm_atomic_helper_duplicate_state - duplicate an atomic state object
3147 * @ctx: lock acquisition context
3149 * Makes a copy of the current atomic state by looping over all objects and
3150 * duplicating their respective states. This is used for example by suspend/
3151 * resume support code to save the state prior to suspend such that it can
3152 * be restored upon resume.
3154 * Note that this treats atomic state as persistent between save and restore.
3155 * Drivers must make sure that this is possible and won't result in confusion
3156 * or erroneous behaviour.
3158 * Note that if callers haven't already acquired all modeset locks this might
3159 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
3162 * A pointer to the copy of the atomic state object on success or an
3163 * ERR_PTR()-encoded error code on failure.
3166 * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
3168 struct drm_atomic_state *
3169 drm_atomic_helper_duplicate_state(struct drm_device *dev,
3170 struct drm_modeset_acquire_ctx *ctx)
3172 struct drm_atomic_state *state;
3173 struct drm_connector *conn;
3174 struct drm_connector_list_iter conn_iter;
3175 struct drm_plane *plane;
3176 struct drm_crtc *crtc;
3179 state = drm_atomic_state_alloc(dev);
3181 return ERR_PTR(-ENOMEM);
3183 state->acquire_ctx = ctx;
3185 drm_for_each_crtc(crtc, dev) {
3186 struct drm_crtc_state *crtc_state;
3188 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3189 if (IS_ERR(crtc_state)) {
3190 err = PTR_ERR(crtc_state);
3195 drm_for_each_plane(plane, dev) {
3196 struct drm_plane_state *plane_state;
3198 plane_state = drm_atomic_get_plane_state(state, plane);
3199 if (IS_ERR(plane_state)) {
3200 err = PTR_ERR(plane_state);
3205 drm_connector_list_iter_begin(dev, &conn_iter);
3206 drm_for_each_connector_iter(conn, &conn_iter) {
3207 struct drm_connector_state *conn_state;
3209 conn_state = drm_atomic_get_connector_state(state, conn);
3210 if (IS_ERR(conn_state)) {
3211 err = PTR_ERR(conn_state);
3212 drm_connector_list_iter_end(&conn_iter);
3216 drm_connector_list_iter_end(&conn_iter);
3218 /* clear the acquire context so that it isn't accidentally reused */
3219 state->acquire_ctx = NULL;
3223 drm_atomic_state_put(state);
3224 state = ERR_PTR(err);
3229 EXPORT_SYMBOL(drm_atomic_helper_duplicate_state);
3232 * drm_atomic_helper_suspend - subsystem-level suspend helper
3235 * Duplicates the current atomic state, disables all active outputs and then
3236 * returns a pointer to the original atomic state to the caller. Drivers can
3237 * pass this pointer to the drm_atomic_helper_resume() helper upon resume to
3238 * restore the output configuration that was active at the time the system
3241 * Note that it is potentially unsafe to use this. The atomic state object
3242 * returned by this function is assumed to be persistent. Drivers must ensure
3243 * that this holds true. Before calling this function, drivers must make sure
3244 * to suspend fbdev emulation so that nothing can be using the device.
3247 * A pointer to a copy of the state before suspend on success or an ERR_PTR()-
3248 * encoded error code on failure. Drivers should store the returned atomic
3249 * state object and pass it to the drm_atomic_helper_resume() helper upon
3253 * drm_atomic_helper_duplicate_state(), drm_atomic_helper_disable_all(),
3254 * drm_atomic_helper_resume(), drm_atomic_helper_commit_duplicated_state()
3256 struct drm_atomic_state *drm_atomic_helper_suspend(struct drm_device *dev)
3258 struct drm_modeset_acquire_ctx ctx;
3259 struct drm_atomic_state *state;
3262 /* This can never be returned, but it makes the compiler happy */
3263 state = ERR_PTR(-EINVAL);
3265 DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx, 0, err);
3267 state = drm_atomic_helper_duplicate_state(dev, &ctx);
3271 err = drm_atomic_helper_disable_all(dev, &ctx);
3273 drm_atomic_state_put(state);
3274 state = ERR_PTR(err);
3279 DRM_MODESET_LOCK_ALL_END(ctx, err);
3281 return ERR_PTR(err);
3285 EXPORT_SYMBOL(drm_atomic_helper_suspend);
3288 * drm_atomic_helper_commit_duplicated_state - commit duplicated state
3289 * @state: duplicated atomic state to commit
3290 * @ctx: pointer to acquire_ctx to use for commit.
3292 * The state returned by drm_atomic_helper_duplicate_state() and
3293 * drm_atomic_helper_suspend() is partially invalid, and needs to
3294 * be fixed up before commit.
3297 * 0 on success or a negative error code on failure.
3300 * drm_atomic_helper_suspend()
3302 int drm_atomic_helper_commit_duplicated_state(struct drm_atomic_state *state,
3303 struct drm_modeset_acquire_ctx *ctx)
3306 struct drm_plane *plane;
3307 struct drm_plane_state *new_plane_state;
3308 struct drm_connector *connector;
3309 struct drm_connector_state *new_conn_state;
3310 struct drm_crtc *crtc;
3311 struct drm_crtc_state *new_crtc_state;
3313 state->acquire_ctx = ctx;
3315 for_each_new_plane_in_state(state, plane, new_plane_state, i)
3316 state->planes[i].old_state = plane->state;
3318 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i)
3319 state->crtcs[i].old_state = crtc->state;
3321 for_each_new_connector_in_state(state, connector, new_conn_state, i)
3322 state->connectors[i].old_state = connector->state;
3324 ret = drm_atomic_commit(state);
3326 state->acquire_ctx = NULL;
3330 EXPORT_SYMBOL(drm_atomic_helper_commit_duplicated_state);
3333 * drm_atomic_helper_resume - subsystem-level resume helper
3335 * @state: atomic state to resume to
3337 * Calls drm_mode_config_reset() to synchronize hardware and software states,
3338 * grabs all modeset locks and commits the atomic state object. This can be
3339 * used in conjunction with the drm_atomic_helper_suspend() helper to
3340 * implement suspend/resume for drivers that support atomic mode-setting.
3343 * 0 on success or a negative error code on failure.
3346 * drm_atomic_helper_suspend()
3348 int drm_atomic_helper_resume(struct drm_device *dev,
3349 struct drm_atomic_state *state)
3351 struct drm_modeset_acquire_ctx ctx;
3354 drm_mode_config_reset(dev);
3356 DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx, 0, err);
3358 err = drm_atomic_helper_commit_duplicated_state(state, &ctx);
3360 DRM_MODESET_LOCK_ALL_END(ctx, err);
3361 drm_atomic_state_put(state);
3365 EXPORT_SYMBOL(drm_atomic_helper_resume);
3367 static int page_flip_common(struct drm_atomic_state *state,
3368 struct drm_crtc *crtc,
3369 struct drm_framebuffer *fb,
3370 struct drm_pending_vblank_event *event,
3373 struct drm_plane *plane = crtc->primary;
3374 struct drm_plane_state *plane_state;
3375 struct drm_crtc_state *crtc_state;
3378 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3379 if (IS_ERR(crtc_state))
3380 return PTR_ERR(crtc_state);
3382 crtc_state->event = event;
3383 crtc_state->pageflip_flags = flags;
3385 plane_state = drm_atomic_get_plane_state(state, plane);
3386 if (IS_ERR(plane_state))
3387 return PTR_ERR(plane_state);
3389 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
3392 drm_atomic_set_fb_for_plane(plane_state, fb);
3394 /* Make sure we don't accidentally do a full modeset. */
3395 state->allow_modeset = false;
3396 if (!crtc_state->active) {
3397 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] disabled, rejecting legacy flip\n",
3398 crtc->base.id, crtc->name);
3406 * drm_atomic_helper_page_flip - execute a legacy page flip
3408 * @fb: DRM framebuffer
3409 * @event: optional DRM event to signal upon completion
3410 * @flags: flip flags for non-vblank sync'ed updates
3411 * @ctx: lock acquisition context
3413 * Provides a default &drm_crtc_funcs.page_flip implementation
3414 * using the atomic driver interface.
3417 * Returns 0 on success, negative errno numbers on failure.
3420 * drm_atomic_helper_page_flip_target()
3422 int drm_atomic_helper_page_flip(struct drm_crtc *crtc,
3423 struct drm_framebuffer *fb,
3424 struct drm_pending_vblank_event *event,
3426 struct drm_modeset_acquire_ctx *ctx)
3428 struct drm_plane *plane = crtc->primary;
3429 struct drm_atomic_state *state;
3432 state = drm_atomic_state_alloc(plane->dev);
3436 state->acquire_ctx = ctx;
3438 ret = page_flip_common(state, crtc, fb, event, flags);
3442 ret = drm_atomic_nonblocking_commit(state);
3444 drm_atomic_state_put(state);
3447 EXPORT_SYMBOL(drm_atomic_helper_page_flip);
3450 * drm_atomic_helper_page_flip_target - do page flip on target vblank period.
3452 * @fb: DRM framebuffer
3453 * @event: optional DRM event to signal upon completion
3454 * @flags: flip flags for non-vblank sync'ed updates
3455 * @target: specifying the target vblank period when the flip to take effect
3456 * @ctx: lock acquisition context
3458 * Provides a default &drm_crtc_funcs.page_flip_target implementation.
3459 * Similar to drm_atomic_helper_page_flip() with extra parameter to specify
3460 * target vblank period to flip.
3463 * Returns 0 on success, negative errno numbers on failure.
3465 int drm_atomic_helper_page_flip_target(struct drm_crtc *crtc,
3466 struct drm_framebuffer *fb,
3467 struct drm_pending_vblank_event *event,
3470 struct drm_modeset_acquire_ctx *ctx)
3472 struct drm_plane *plane = crtc->primary;
3473 struct drm_atomic_state *state;
3474 struct drm_crtc_state *crtc_state;
3477 state = drm_atomic_state_alloc(plane->dev);
3481 state->acquire_ctx = ctx;
3483 ret = page_flip_common(state, crtc, fb, event, flags);
3487 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
3488 if (WARN_ON(!crtc_state)) {
3492 crtc_state->target_vblank = target;
3494 ret = drm_atomic_nonblocking_commit(state);
3496 drm_atomic_state_put(state);
3499 EXPORT_SYMBOL(drm_atomic_helper_page_flip_target);
3502 * drm_atomic_helper_legacy_gamma_set - set the legacy gamma correction table
3503 * @crtc: CRTC object
3504 * @red: red correction table
3505 * @green: green correction table
3506 * @blue: green correction table
3507 * @size: size of the tables
3508 * @ctx: lock acquire context
3510 * Implements support for legacy gamma correction table for drivers
3511 * that support color management through the DEGAMMA_LUT/GAMMA_LUT
3512 * properties. See drm_crtc_enable_color_mgmt() and the containing chapter for
3513 * how the atomic color management and gamma tables work.
3515 int drm_atomic_helper_legacy_gamma_set(struct drm_crtc *crtc,
3516 u16 *red, u16 *green, u16 *blue,
3518 struct drm_modeset_acquire_ctx *ctx)
3520 struct drm_device *dev = crtc->dev;
3521 struct drm_atomic_state *state;
3522 struct drm_crtc_state *crtc_state;
3523 struct drm_property_blob *blob = NULL;
3524 struct drm_color_lut *blob_data;
3528 state = drm_atomic_state_alloc(crtc->dev);
3532 blob = drm_property_create_blob(dev,
3533 sizeof(struct drm_color_lut) * size,
3536 ret = PTR_ERR(blob);
3541 /* Prepare GAMMA_LUT with the legacy values. */
3542 blob_data = blob->data;
3543 for (i = 0; i < size; i++) {
3544 blob_data[i].red = red[i];
3545 blob_data[i].green = green[i];
3546 blob_data[i].blue = blue[i];
3549 state->acquire_ctx = ctx;
3550 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3551 if (IS_ERR(crtc_state)) {
3552 ret = PTR_ERR(crtc_state);
3556 /* Reset DEGAMMA_LUT and CTM properties. */
3557 replaced = drm_property_replace_blob(&crtc_state->degamma_lut, NULL);
3558 replaced |= drm_property_replace_blob(&crtc_state->ctm, NULL);
3559 replaced |= drm_property_replace_blob(&crtc_state->gamma_lut, blob);
3560 crtc_state->color_mgmt_changed |= replaced;
3562 ret = drm_atomic_commit(state);
3565 drm_atomic_state_put(state);
3566 drm_property_blob_put(blob);
3569 EXPORT_SYMBOL(drm_atomic_helper_legacy_gamma_set);