2 * Copyright (C) 2014 Red Hat
3 * Copyright (C) 2014 Intel Corp.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
24 * Rob Clark <robdclark@gmail.com>
25 * Daniel Vetter <daniel.vetter@ffwll.ch>
29 #include <drm/drm_atomic.h>
30 #include <drm/drm_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)
1606 * FIXME: Since prepare_fb and cleanup_fb are always called on
1607 * the new_plane_state for async updates we need to block framebuffer
1608 * changes. This prevents use of a fb that's been cleaned up and
1609 * double cleanups from occuring.
1611 if (old_plane_state->fb != new_plane_state->fb)
1614 funcs = plane->helper_private;
1615 if (!funcs->atomic_async_update)
1618 if (new_plane_state->fence)
1622 * Don't do an async update if there is an outstanding commit modifying
1623 * the plane. This prevents our async update's changes from getting
1624 * overridden by a previous synchronous update's state.
1626 if (old_plane_state->commit &&
1627 !try_wait_for_completion(&old_plane_state->commit->hw_done))
1630 return funcs->atomic_async_check(plane, new_plane_state);
1632 EXPORT_SYMBOL(drm_atomic_helper_async_check);
1635 * drm_atomic_helper_async_commit - commit state asynchronously
1637 * @state: the driver state object
1639 * This function commits a state asynchronously, i.e., not vblank
1640 * synchronized. It should be used on a state only when
1641 * drm_atomic_async_check() succeeds. Async commits are not supposed to swap
1642 * the states like normal sync commits, but just do in-place changes on the
1645 void drm_atomic_helper_async_commit(struct drm_device *dev,
1646 struct drm_atomic_state *state)
1648 struct drm_plane *plane;
1649 struct drm_plane_state *plane_state;
1650 const struct drm_plane_helper_funcs *funcs;
1653 for_each_new_plane_in_state(state, plane, plane_state, i) {
1654 funcs = plane->helper_private;
1655 funcs->atomic_async_update(plane, plane_state);
1658 * ->atomic_async_update() is supposed to update the
1659 * plane->state in-place, make sure at least common
1660 * properties have been properly updated.
1662 WARN_ON_ONCE(plane->state->fb != plane_state->fb);
1663 WARN_ON_ONCE(plane->state->crtc_x != plane_state->crtc_x);
1664 WARN_ON_ONCE(plane->state->crtc_y != plane_state->crtc_y);
1665 WARN_ON_ONCE(plane->state->src_x != plane_state->src_x);
1666 WARN_ON_ONCE(plane->state->src_y != plane_state->src_y);
1669 EXPORT_SYMBOL(drm_atomic_helper_async_commit);
1672 * drm_atomic_helper_commit - commit validated state object
1674 * @state: the driver state object
1675 * @nonblock: whether nonblocking behavior is requested.
1677 * This function commits a with drm_atomic_helper_check() pre-validated state
1678 * object. This can still fail when e.g. the framebuffer reservation fails. This
1679 * function implements nonblocking commits, using
1680 * drm_atomic_helper_setup_commit() and related functions.
1682 * Committing the actual hardware state is done through the
1683 * &drm_mode_config_helper_funcs.atomic_commit_tail callback, or it's default
1684 * implementation drm_atomic_helper_commit_tail().
1687 * Zero for success or -errno.
1689 int drm_atomic_helper_commit(struct drm_device *dev,
1690 struct drm_atomic_state *state,
1695 if (state->async_update) {
1696 ret = drm_atomic_helper_prepare_planes(dev, state);
1700 drm_atomic_helper_async_commit(dev, state);
1701 drm_atomic_helper_cleanup_planes(dev, state);
1706 ret = drm_atomic_helper_setup_commit(state, nonblock);
1710 INIT_WORK(&state->commit_work, commit_work);
1712 ret = drm_atomic_helper_prepare_planes(dev, state);
1717 ret = drm_atomic_helper_wait_for_fences(dev, state, true);
1723 * This is the point of no return - everything below never fails except
1724 * when the hw goes bonghits. Which means we can commit the new state on
1725 * the software side now.
1728 ret = drm_atomic_helper_swap_state(state, true);
1733 * Everything below can be run asynchronously without the need to grab
1734 * any modeset locks at all under one condition: It must be guaranteed
1735 * that the asynchronous work has either been cancelled (if the driver
1736 * supports it, which at least requires that the framebuffers get
1737 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
1738 * before the new state gets committed on the software side with
1739 * drm_atomic_helper_swap_state().
1741 * This scheme allows new atomic state updates to be prepared and
1742 * checked in parallel to the asynchronous completion of the previous
1743 * update. Which is important since compositors need to figure out the
1744 * composition of the next frame right after having submitted the
1747 * NOTE: Commit work has multiple phases, first hardware commit, then
1748 * cleanup. We want them to overlap, hence need system_unbound_wq to
1749 * make sure work items don't artifically stall on each another.
1752 drm_atomic_state_get(state);
1754 queue_work(system_unbound_wq, &state->commit_work);
1761 drm_atomic_helper_cleanup_planes(dev, state);
1764 EXPORT_SYMBOL(drm_atomic_helper_commit);
1767 * DOC: implementing nonblocking commit
1769 * Nonblocking atomic commits have to be implemented in the following sequence:
1771 * 1. Run drm_atomic_helper_prepare_planes() first. This is the only function
1772 * which commit needs to call which can fail, so we want to run it first and
1775 * 2. Synchronize with any outstanding nonblocking commit worker threads which
1776 * might be affected the new state update. This can be done by either cancelling
1777 * or flushing the work items, depending upon whether the driver can deal with
1778 * cancelled updates. Note that it is important to ensure that the framebuffer
1779 * cleanup is still done when cancelling.
1781 * Asynchronous workers need to have sufficient parallelism to be able to run
1782 * different atomic commits on different CRTCs in parallel. The simplest way to
1783 * achive this is by running them on the &system_unbound_wq work queue. Note
1784 * that drivers are not required to split up atomic commits and run an
1785 * individual commit in parallel - userspace is supposed to do that if it cares.
1786 * But it might be beneficial to do that for modesets, since those necessarily
1787 * must be done as one global operation, and enabling or disabling a CRTC can
1788 * take a long time. But even that is not required.
1790 * 3. The software state is updated synchronously with
1791 * drm_atomic_helper_swap_state(). Doing this under the protection of all modeset
1792 * locks means concurrent callers never see inconsistent state. And doing this
1793 * while it's guaranteed that no relevant nonblocking worker runs means that
1794 * nonblocking workers do not need grab any locks. Actually they must not grab
1795 * locks, for otherwise the work flushing will deadlock.
1797 * 4. Schedule a work item to do all subsequent steps, using the split-out
1798 * commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and
1799 * then cleaning up the framebuffers after the old framebuffer is no longer
1802 * The above scheme is implemented in the atomic helper libraries in
1803 * drm_atomic_helper_commit() using a bunch of helper functions. See
1804 * drm_atomic_helper_setup_commit() for a starting point.
1807 static int stall_checks(struct drm_crtc *crtc, bool nonblock)
1809 struct drm_crtc_commit *commit, *stall_commit = NULL;
1810 bool completed = true;
1814 spin_lock(&crtc->commit_lock);
1816 list_for_each_entry(commit, &crtc->commit_list, commit_entry) {
1818 completed = try_wait_for_completion(&commit->flip_done);
1819 /* Userspace is not allowed to get ahead of the previous
1820 * commit with nonblocking ones. */
1821 if (!completed && nonblock) {
1822 spin_unlock(&crtc->commit_lock);
1825 } else if (i == 1) {
1826 stall_commit = drm_crtc_commit_get(commit);
1832 spin_unlock(&crtc->commit_lock);
1837 /* We don't want to let commits get ahead of cleanup work too much,
1838 * stalling on 2nd previous commit means triple-buffer won't ever stall.
1840 ret = wait_for_completion_interruptible_timeout(&stall_commit->cleanup_done,
1843 DRM_ERROR("[CRTC:%d:%s] cleanup_done timed out\n",
1844 crtc->base.id, crtc->name);
1846 drm_crtc_commit_put(stall_commit);
1848 return ret < 0 ? ret : 0;
1851 static void release_crtc_commit(struct completion *completion)
1853 struct drm_crtc_commit *commit = container_of(completion,
1857 drm_crtc_commit_put(commit);
1860 static void init_commit(struct drm_crtc_commit *commit, struct drm_crtc *crtc)
1862 init_completion(&commit->flip_done);
1863 init_completion(&commit->hw_done);
1864 init_completion(&commit->cleanup_done);
1865 INIT_LIST_HEAD(&commit->commit_entry);
1866 kref_init(&commit->ref);
1867 commit->crtc = crtc;
1870 static struct drm_crtc_commit *
1871 crtc_or_fake_commit(struct drm_atomic_state *state, struct drm_crtc *crtc)
1874 struct drm_crtc_state *new_crtc_state;
1876 new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
1878 return new_crtc_state->commit;
1881 if (!state->fake_commit) {
1882 state->fake_commit = kzalloc(sizeof(*state->fake_commit), GFP_KERNEL);
1883 if (!state->fake_commit)
1886 init_commit(state->fake_commit, NULL);
1889 return state->fake_commit;
1893 * drm_atomic_helper_setup_commit - setup possibly nonblocking commit
1894 * @state: new modeset state to be committed
1895 * @nonblock: whether nonblocking behavior is requested.
1897 * This function prepares @state to be used by the atomic helper's support for
1898 * nonblocking commits. Drivers using the nonblocking commit infrastructure
1899 * should always call this function from their
1900 * &drm_mode_config_funcs.atomic_commit hook.
1902 * To be able to use this support drivers need to use a few more helper
1903 * functions. drm_atomic_helper_wait_for_dependencies() must be called before
1904 * actually committing the hardware state, and for nonblocking commits this call
1905 * must be placed in the async worker. See also drm_atomic_helper_swap_state()
1906 * and it's stall parameter, for when a driver's commit hooks look at the
1907 * &drm_crtc.state, &drm_plane.state or &drm_connector.state pointer directly.
1909 * Completion of the hardware commit step must be signalled using
1910 * drm_atomic_helper_commit_hw_done(). After this step the driver is not allowed
1911 * to read or change any permanent software or hardware modeset state. The only
1912 * exception is state protected by other means than &drm_modeset_lock locks.
1913 * Only the free standing @state with pointers to the old state structures can
1914 * be inspected, e.g. to clean up old buffers using
1915 * drm_atomic_helper_cleanup_planes().
1917 * At the very end, before cleaning up @state drivers must call
1918 * drm_atomic_helper_commit_cleanup_done().
1920 * This is all implemented by in drm_atomic_helper_commit(), giving drivers a
1921 * complete and easy-to-use default implementation of the atomic_commit() hook.
1923 * The tracking of asynchronously executed and still pending commits is done
1924 * using the core structure &drm_crtc_commit.
1926 * By default there's no need to clean up resources allocated by this function
1927 * explicitly: drm_atomic_state_default_clear() will take care of that
1932 * 0 on success. -EBUSY when userspace schedules nonblocking commits too fast,
1933 * -ENOMEM on allocation failures and -EINTR when a signal is pending.
1935 int drm_atomic_helper_setup_commit(struct drm_atomic_state *state,
1938 struct drm_crtc *crtc;
1939 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
1940 struct drm_connector *conn;
1941 struct drm_connector_state *old_conn_state, *new_conn_state;
1942 struct drm_plane *plane;
1943 struct drm_plane_state *old_plane_state, *new_plane_state;
1944 struct drm_crtc_commit *commit;
1947 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
1948 commit = kzalloc(sizeof(*commit), GFP_KERNEL);
1952 init_commit(commit, crtc);
1954 new_crtc_state->commit = commit;
1956 ret = stall_checks(crtc, nonblock);
1960 /* Drivers only send out events when at least either current or
1961 * new CRTC state is active. Complete right away if everything
1963 if (!old_crtc_state->active && !new_crtc_state->active) {
1964 complete_all(&commit->flip_done);
1968 /* Legacy cursor updates are fully unsynced. */
1969 if (state->legacy_cursor_update) {
1970 complete_all(&commit->flip_done);
1974 if (!new_crtc_state->event) {
1975 commit->event = kzalloc(sizeof(*commit->event),
1980 new_crtc_state->event = commit->event;
1983 new_crtc_state->event->base.completion = &commit->flip_done;
1984 new_crtc_state->event->base.completion_release = release_crtc_commit;
1985 drm_crtc_commit_get(commit);
1987 commit->abort_completion = true;
1989 state->crtcs[i].commit = commit;
1990 drm_crtc_commit_get(commit);
1993 for_each_oldnew_connector_in_state(state, conn, old_conn_state, new_conn_state, i) {
1994 /* Userspace is not allowed to get ahead of the previous
1995 * commit with nonblocking ones. */
1996 if (nonblock && old_conn_state->commit &&
1997 !try_wait_for_completion(&old_conn_state->commit->flip_done))
2000 /* Always track connectors explicitly for e.g. link retraining. */
2001 commit = crtc_or_fake_commit(state, new_conn_state->crtc ?: old_conn_state->crtc);
2005 new_conn_state->commit = drm_crtc_commit_get(commit);
2008 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
2009 /* Userspace is not allowed to get ahead of the previous
2010 * commit with nonblocking ones. */
2011 if (nonblock && old_plane_state->commit &&
2012 !try_wait_for_completion(&old_plane_state->commit->flip_done))
2015 /* Always track planes explicitly for async pageflip support. */
2016 commit = crtc_or_fake_commit(state, new_plane_state->crtc ?: old_plane_state->crtc);
2020 new_plane_state->commit = drm_crtc_commit_get(commit);
2025 EXPORT_SYMBOL(drm_atomic_helper_setup_commit);
2028 * drm_atomic_helper_wait_for_dependencies - wait for required preceeding commits
2029 * @old_state: atomic state object with old state structures
2031 * This function waits for all preceeding commits that touch the same CRTC as
2032 * @old_state to both be committed to the hardware (as signalled by
2033 * drm_atomic_helper_commit_hw_done) and executed by the hardware (as signalled
2034 * by calling drm_crtc_send_vblank_event() on the &drm_crtc_state.event).
2036 * This is part of the atomic helper support for nonblocking commits, see
2037 * drm_atomic_helper_setup_commit() for an overview.
2039 void drm_atomic_helper_wait_for_dependencies(struct drm_atomic_state *old_state)
2041 struct drm_crtc *crtc;
2042 struct drm_crtc_state *old_crtc_state;
2043 struct drm_plane *plane;
2044 struct drm_plane_state *old_plane_state;
2045 struct drm_connector *conn;
2046 struct drm_connector_state *old_conn_state;
2047 struct drm_crtc_commit *commit;
2051 for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
2052 commit = old_crtc_state->commit;
2057 ret = wait_for_completion_timeout(&commit->hw_done,
2060 DRM_ERROR("[CRTC:%d:%s] hw_done timed out\n",
2061 crtc->base.id, crtc->name);
2063 /* Currently no support for overwriting flips, hence
2064 * stall for previous one to execute completely. */
2065 ret = wait_for_completion_timeout(&commit->flip_done,
2068 DRM_ERROR("[CRTC:%d:%s] flip_done timed out\n",
2069 crtc->base.id, crtc->name);
2072 for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
2073 commit = old_conn_state->commit;
2078 ret = wait_for_completion_timeout(&commit->hw_done,
2081 DRM_ERROR("[CONNECTOR:%d:%s] hw_done timed out\n",
2082 conn->base.id, conn->name);
2084 /* Currently no support for overwriting flips, hence
2085 * stall for previous one to execute completely. */
2086 ret = wait_for_completion_timeout(&commit->flip_done,
2089 DRM_ERROR("[CONNECTOR:%d:%s] flip_done timed out\n",
2090 conn->base.id, conn->name);
2093 for_each_old_plane_in_state(old_state, plane, old_plane_state, i) {
2094 commit = old_plane_state->commit;
2099 ret = wait_for_completion_timeout(&commit->hw_done,
2102 DRM_ERROR("[PLANE:%d:%s] hw_done timed out\n",
2103 plane->base.id, plane->name);
2105 /* Currently no support for overwriting flips, hence
2106 * stall for previous one to execute completely. */
2107 ret = wait_for_completion_timeout(&commit->flip_done,
2110 DRM_ERROR("[PLANE:%d:%s] flip_done timed out\n",
2111 plane->base.id, plane->name);
2114 EXPORT_SYMBOL(drm_atomic_helper_wait_for_dependencies);
2117 * drm_atomic_helper_fake_vblank - fake VBLANK events if needed
2118 * @old_state: atomic state object with old state structures
2120 * This function walks all CRTCs and fake VBLANK events on those with
2121 * &drm_crtc_state.no_vblank set to true and &drm_crtc_state.event != NULL.
2122 * The primary use of this function is writeback connectors working in oneshot
2123 * mode and faking VBLANK events. In this case they only fake the VBLANK event
2124 * when a job is queued, and any change to the pipeline that does not touch the
2125 * connector is leading to timeouts when calling
2126 * drm_atomic_helper_wait_for_vblanks() or
2127 * drm_atomic_helper_wait_for_flip_done().
2129 * This is part of the atomic helper support for nonblocking commits, see
2130 * drm_atomic_helper_setup_commit() for an overview.
2132 void drm_atomic_helper_fake_vblank(struct drm_atomic_state *old_state)
2134 struct drm_crtc_state *new_crtc_state;
2135 struct drm_crtc *crtc;
2138 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
2139 unsigned long flags;
2141 if (!new_crtc_state->no_vblank)
2144 spin_lock_irqsave(&old_state->dev->event_lock, flags);
2145 if (new_crtc_state->event) {
2146 drm_crtc_send_vblank_event(crtc,
2147 new_crtc_state->event);
2148 new_crtc_state->event = NULL;
2150 spin_unlock_irqrestore(&old_state->dev->event_lock, flags);
2153 EXPORT_SYMBOL(drm_atomic_helper_fake_vblank);
2156 * drm_atomic_helper_commit_hw_done - setup possible nonblocking commit
2157 * @old_state: atomic state object with old state structures
2159 * This function is used to signal completion of the hardware commit step. After
2160 * this step the driver is not allowed to read or change any permanent software
2161 * or hardware modeset state. The only exception is state protected by other
2162 * means than &drm_modeset_lock locks.
2164 * Drivers should try to postpone any expensive or delayed cleanup work after
2165 * this function is called.
2167 * This is part of the atomic helper support for nonblocking commits, see
2168 * drm_atomic_helper_setup_commit() for an overview.
2170 void drm_atomic_helper_commit_hw_done(struct drm_atomic_state *old_state)
2172 struct drm_crtc *crtc;
2173 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
2174 struct drm_crtc_commit *commit;
2177 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
2178 commit = new_crtc_state->commit;
2183 * copy new_crtc_state->commit to old_crtc_state->commit,
2184 * it's unsafe to touch new_crtc_state after hw_done,
2185 * but we still need to do so in cleanup_done().
2187 if (old_crtc_state->commit)
2188 drm_crtc_commit_put(old_crtc_state->commit);
2190 old_crtc_state->commit = drm_crtc_commit_get(commit);
2192 /* backend must have consumed any event by now */
2193 WARN_ON(new_crtc_state->event);
2194 complete_all(&commit->hw_done);
2197 if (old_state->fake_commit) {
2198 complete_all(&old_state->fake_commit->hw_done);
2199 complete_all(&old_state->fake_commit->flip_done);
2202 EXPORT_SYMBOL(drm_atomic_helper_commit_hw_done);
2205 * drm_atomic_helper_commit_cleanup_done - signal completion of commit
2206 * @old_state: atomic state object with old state structures
2208 * This signals completion of the atomic update @old_state, including any
2209 * cleanup work. If used, it must be called right before calling
2210 * drm_atomic_state_put().
2212 * This is part of the atomic helper support for nonblocking commits, see
2213 * drm_atomic_helper_setup_commit() for an overview.
2215 void drm_atomic_helper_commit_cleanup_done(struct drm_atomic_state *old_state)
2217 struct drm_crtc *crtc;
2218 struct drm_crtc_state *old_crtc_state;
2219 struct drm_crtc_commit *commit;
2222 for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
2223 commit = old_crtc_state->commit;
2224 if (WARN_ON(!commit))
2227 complete_all(&commit->cleanup_done);
2228 WARN_ON(!try_wait_for_completion(&commit->hw_done));
2230 spin_lock(&crtc->commit_lock);
2231 list_del(&commit->commit_entry);
2232 spin_unlock(&crtc->commit_lock);
2235 if (old_state->fake_commit) {
2236 complete_all(&old_state->fake_commit->cleanup_done);
2237 WARN_ON(!try_wait_for_completion(&old_state->fake_commit->hw_done));
2240 EXPORT_SYMBOL(drm_atomic_helper_commit_cleanup_done);
2243 * drm_atomic_helper_prepare_planes - prepare plane resources before commit
2245 * @state: atomic state object with new state structures
2247 * This function prepares plane state, specifically framebuffers, for the new
2248 * configuration, by calling &drm_plane_helper_funcs.prepare_fb. If any failure
2249 * is encountered this function will call &drm_plane_helper_funcs.cleanup_fb on
2250 * any already successfully prepared framebuffer.
2253 * 0 on success, negative error code on failure.
2255 int drm_atomic_helper_prepare_planes(struct drm_device *dev,
2256 struct drm_atomic_state *state)
2258 struct drm_plane *plane;
2259 struct drm_plane_state *new_plane_state;
2262 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
2263 const struct drm_plane_helper_funcs *funcs;
2265 funcs = plane->helper_private;
2267 if (funcs->prepare_fb) {
2268 ret = funcs->prepare_fb(plane, new_plane_state);
2277 for_each_new_plane_in_state(state, plane, new_plane_state, j) {
2278 const struct drm_plane_helper_funcs *funcs;
2283 funcs = plane->helper_private;
2285 if (funcs->cleanup_fb)
2286 funcs->cleanup_fb(plane, new_plane_state);
2291 EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
2293 static bool plane_crtc_active(const struct drm_plane_state *state)
2295 return state->crtc && state->crtc->state->active;
2299 * drm_atomic_helper_commit_planes - commit plane state
2301 * @old_state: atomic state object with old state structures
2302 * @flags: flags for committing plane state
2304 * This function commits the new plane state using the plane and atomic helper
2305 * functions for planes and crtcs. It assumes that the atomic state has already
2306 * been pushed into the relevant object state pointers, since this step can no
2309 * It still requires the global state object @old_state to know which planes and
2310 * crtcs need to be updated though.
2312 * Note that this function does all plane updates across all CRTCs in one step.
2313 * If the hardware can't support this approach look at
2314 * drm_atomic_helper_commit_planes_on_crtc() instead.
2316 * Plane parameters can be updated by applications while the associated CRTC is
2317 * disabled. The DRM/KMS core will store the parameters in the plane state,
2318 * which will be available to the driver when the CRTC is turned on. As a result
2319 * most drivers don't need to be immediately notified of plane updates for a
2322 * Unless otherwise needed, drivers are advised to set the ACTIVE_ONLY flag in
2323 * @flags in order not to receive plane update notifications related to a
2324 * disabled CRTC. This avoids the need to manually ignore plane updates in
2325 * driver code when the driver and/or hardware can't or just don't need to deal
2326 * with updates on disabled CRTCs, for example when supporting runtime PM.
2328 * Drivers may set the NO_DISABLE_AFTER_MODESET flag in @flags if the relevant
2329 * display controllers require to disable a CRTC's planes when the CRTC is
2330 * disabled. This function would skip the &drm_plane_helper_funcs.atomic_disable
2331 * call for a plane if the CRTC of the old plane state needs a modesetting
2332 * operation. Of course, the drivers need to disable the planes in their CRTC
2333 * disable callbacks since no one else would do that.
2335 * The drm_atomic_helper_commit() default implementation doesn't set the
2336 * ACTIVE_ONLY flag to most closely match the behaviour of the legacy helpers.
2337 * This should not be copied blindly by drivers.
2339 void drm_atomic_helper_commit_planes(struct drm_device *dev,
2340 struct drm_atomic_state *old_state,
2343 struct drm_crtc *crtc;
2344 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
2345 struct drm_plane *plane;
2346 struct drm_plane_state *old_plane_state, *new_plane_state;
2348 bool active_only = flags & DRM_PLANE_COMMIT_ACTIVE_ONLY;
2349 bool no_disable = flags & DRM_PLANE_COMMIT_NO_DISABLE_AFTER_MODESET;
2351 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
2352 const struct drm_crtc_helper_funcs *funcs;
2354 funcs = crtc->helper_private;
2356 if (!funcs || !funcs->atomic_begin)
2359 if (active_only && !new_crtc_state->active)
2362 funcs->atomic_begin(crtc, old_crtc_state);
2365 for_each_oldnew_plane_in_state(old_state, plane, old_plane_state, new_plane_state, i) {
2366 const struct drm_plane_helper_funcs *funcs;
2369 funcs = plane->helper_private;
2374 disabling = drm_atomic_plane_disabling(old_plane_state,
2379 * Skip planes related to inactive CRTCs. If the plane
2380 * is enabled use the state of the current CRTC. If the
2381 * plane is being disabled use the state of the old
2382 * CRTC to avoid skipping planes being disabled on an
2385 if (!disabling && !plane_crtc_active(new_plane_state))
2387 if (disabling && !plane_crtc_active(old_plane_state))
2392 * Special-case disabling the plane if drivers support it.
2394 if (disabling && funcs->atomic_disable) {
2395 struct drm_crtc_state *crtc_state;
2397 crtc_state = old_plane_state->crtc->state;
2399 if (drm_atomic_crtc_needs_modeset(crtc_state) &&
2403 funcs->atomic_disable(plane, old_plane_state);
2404 } else if (new_plane_state->crtc || disabling) {
2405 funcs->atomic_update(plane, old_plane_state);
2409 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
2410 const struct drm_crtc_helper_funcs *funcs;
2412 funcs = crtc->helper_private;
2414 if (!funcs || !funcs->atomic_flush)
2417 if (active_only && !new_crtc_state->active)
2420 funcs->atomic_flush(crtc, old_crtc_state);
2423 EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
2426 * drm_atomic_helper_commit_planes_on_crtc - commit plane state for a crtc
2427 * @old_crtc_state: atomic state object with the old crtc state
2429 * This function commits the new plane state using the plane and atomic helper
2430 * functions for planes on the specific crtc. It assumes that the atomic state
2431 * has already been pushed into the relevant object state pointers, since this
2432 * step can no longer fail.
2434 * This function is useful when plane updates should be done crtc-by-crtc
2435 * instead of one global step like drm_atomic_helper_commit_planes() does.
2437 * This function can only be savely used when planes are not allowed to move
2438 * between different CRTCs because this function doesn't handle inter-CRTC
2439 * depencies. Callers need to ensure that either no such depencies exist,
2440 * resolve them through ordering of commit calls or through some other means.
2443 drm_atomic_helper_commit_planes_on_crtc(struct drm_crtc_state *old_crtc_state)
2445 const struct drm_crtc_helper_funcs *crtc_funcs;
2446 struct drm_crtc *crtc = old_crtc_state->crtc;
2447 struct drm_atomic_state *old_state = old_crtc_state->state;
2448 struct drm_crtc_state *new_crtc_state =
2449 drm_atomic_get_new_crtc_state(old_state, crtc);
2450 struct drm_plane *plane;
2451 unsigned plane_mask;
2453 plane_mask = old_crtc_state->plane_mask;
2454 plane_mask |= new_crtc_state->plane_mask;
2456 crtc_funcs = crtc->helper_private;
2457 if (crtc_funcs && crtc_funcs->atomic_begin)
2458 crtc_funcs->atomic_begin(crtc, old_crtc_state);
2460 drm_for_each_plane_mask(plane, crtc->dev, plane_mask) {
2461 struct drm_plane_state *old_plane_state =
2462 drm_atomic_get_old_plane_state(old_state, plane);
2463 struct drm_plane_state *new_plane_state =
2464 drm_atomic_get_new_plane_state(old_state, plane);
2465 const struct drm_plane_helper_funcs *plane_funcs;
2467 plane_funcs = plane->helper_private;
2469 if (!old_plane_state || !plane_funcs)
2472 WARN_ON(new_plane_state->crtc &&
2473 new_plane_state->crtc != crtc);
2475 if (drm_atomic_plane_disabling(old_plane_state, new_plane_state) &&
2476 plane_funcs->atomic_disable)
2477 plane_funcs->atomic_disable(plane, old_plane_state);
2478 else if (new_plane_state->crtc ||
2479 drm_atomic_plane_disabling(old_plane_state, new_plane_state))
2480 plane_funcs->atomic_update(plane, old_plane_state);
2483 if (crtc_funcs && crtc_funcs->atomic_flush)
2484 crtc_funcs->atomic_flush(crtc, old_crtc_state);
2486 EXPORT_SYMBOL(drm_atomic_helper_commit_planes_on_crtc);
2489 * drm_atomic_helper_disable_planes_on_crtc - helper to disable CRTC's planes
2490 * @old_crtc_state: atomic state object with the old CRTC state
2491 * @atomic: if set, synchronize with CRTC's atomic_begin/flush hooks
2493 * Disables all planes associated with the given CRTC. This can be
2494 * used for instance in the CRTC helper atomic_disable callback to disable
2497 * If the atomic-parameter is set the function calls the CRTC's
2498 * atomic_begin hook before and atomic_flush hook after disabling the
2501 * It is a bug to call this function without having implemented the
2502 * &drm_plane_helper_funcs.atomic_disable plane hook.
2505 drm_atomic_helper_disable_planes_on_crtc(struct drm_crtc_state *old_crtc_state,
2508 struct drm_crtc *crtc = old_crtc_state->crtc;
2509 const struct drm_crtc_helper_funcs *crtc_funcs =
2510 crtc->helper_private;
2511 struct drm_plane *plane;
2513 if (atomic && crtc_funcs && crtc_funcs->atomic_begin)
2514 crtc_funcs->atomic_begin(crtc, NULL);
2516 drm_atomic_crtc_state_for_each_plane(plane, old_crtc_state) {
2517 const struct drm_plane_helper_funcs *plane_funcs =
2518 plane->helper_private;
2523 WARN_ON(!plane_funcs->atomic_disable);
2524 if (plane_funcs->atomic_disable)
2525 plane_funcs->atomic_disable(plane, NULL);
2528 if (atomic && crtc_funcs && crtc_funcs->atomic_flush)
2529 crtc_funcs->atomic_flush(crtc, NULL);
2531 EXPORT_SYMBOL(drm_atomic_helper_disable_planes_on_crtc);
2534 * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
2536 * @old_state: atomic state object with old state structures
2538 * This function cleans up plane state, specifically framebuffers, from the old
2539 * configuration. Hence the old configuration must be perserved in @old_state to
2540 * be able to call this function.
2542 * This function must also be called on the new state when the atomic update
2543 * fails at any point after calling drm_atomic_helper_prepare_planes().
2545 void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
2546 struct drm_atomic_state *old_state)
2548 struct drm_plane *plane;
2549 struct drm_plane_state *old_plane_state, *new_plane_state;
2552 for_each_oldnew_plane_in_state(old_state, plane, old_plane_state, new_plane_state, i) {
2553 const struct drm_plane_helper_funcs *funcs;
2554 struct drm_plane_state *plane_state;
2557 * This might be called before swapping when commit is aborted,
2558 * in which case we have to cleanup the new state.
2560 if (old_plane_state == plane->state)
2561 plane_state = new_plane_state;
2563 plane_state = old_plane_state;
2565 funcs = plane->helper_private;
2567 if (funcs->cleanup_fb)
2568 funcs->cleanup_fb(plane, plane_state);
2571 EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
2574 * drm_atomic_helper_swap_state - store atomic state into current sw state
2575 * @state: atomic state
2576 * @stall: stall for preceeding commits
2578 * This function stores the atomic state into the current state pointers in all
2579 * driver objects. It should be called after all failing steps have been done
2580 * and succeeded, but before the actual hardware state is committed.
2582 * For cleanup and error recovery the current state for all changed objects will
2583 * be swapped into @state.
2585 * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
2587 * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
2589 * 2. Do any other steps that might fail.
2591 * 3. Put the staged state into the current state pointers with this function.
2593 * 4. Actually commit the hardware state.
2595 * 5. Call drm_atomic_helper_cleanup_planes() with @state, which since step 3
2596 * contains the old state. Also do any other cleanup required with that state.
2598 * @stall must be set when nonblocking commits for this driver directly access
2599 * the &drm_plane.state, &drm_crtc.state or &drm_connector.state pointer. With
2600 * the current atomic helpers this is almost always the case, since the helpers
2601 * don't pass the right state structures to the callbacks.
2605 * Returns 0 on success. Can return -ERESTARTSYS when @stall is true and the
2606 * waiting for the previous commits has been interrupted.
2608 int drm_atomic_helper_swap_state(struct drm_atomic_state *state,
2612 struct drm_connector *connector;
2613 struct drm_connector_state *old_conn_state, *new_conn_state;
2614 struct drm_crtc *crtc;
2615 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
2616 struct drm_plane *plane;
2617 struct drm_plane_state *old_plane_state, *new_plane_state;
2618 struct drm_crtc_commit *commit;
2619 struct drm_private_obj *obj;
2620 struct drm_private_state *old_obj_state, *new_obj_state;
2624 * We have to stall for hw_done here before
2625 * drm_atomic_helper_wait_for_dependencies() because flip
2626 * depth > 1 is not yet supported by all drivers. As long as
2627 * obj->state is directly dereferenced anywhere in the drivers
2628 * atomic_commit_tail function, then it's unsafe to swap state
2629 * before drm_atomic_helper_commit_hw_done() is called.
2632 for_each_old_crtc_in_state(state, crtc, old_crtc_state, i) {
2633 commit = old_crtc_state->commit;
2638 ret = wait_for_completion_interruptible(&commit->hw_done);
2643 for_each_old_connector_in_state(state, connector, old_conn_state, i) {
2644 commit = old_conn_state->commit;
2649 ret = wait_for_completion_interruptible(&commit->hw_done);
2654 for_each_old_plane_in_state(state, plane, old_plane_state, i) {
2655 commit = old_plane_state->commit;
2660 ret = wait_for_completion_interruptible(&commit->hw_done);
2666 for_each_oldnew_connector_in_state(state, connector, old_conn_state, new_conn_state, i) {
2667 WARN_ON(connector->state != old_conn_state);
2669 old_conn_state->state = state;
2670 new_conn_state->state = NULL;
2672 state->connectors[i].state = old_conn_state;
2673 connector->state = new_conn_state;
2676 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
2677 WARN_ON(crtc->state != old_crtc_state);
2679 old_crtc_state->state = state;
2680 new_crtc_state->state = NULL;
2682 state->crtcs[i].state = old_crtc_state;
2683 crtc->state = new_crtc_state;
2685 if (new_crtc_state->commit) {
2686 spin_lock(&crtc->commit_lock);
2687 list_add(&new_crtc_state->commit->commit_entry,
2688 &crtc->commit_list);
2689 spin_unlock(&crtc->commit_lock);
2691 new_crtc_state->commit->event = NULL;
2695 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
2696 WARN_ON(plane->state != old_plane_state);
2698 old_plane_state->state = state;
2699 new_plane_state->state = NULL;
2701 state->planes[i].state = old_plane_state;
2702 plane->state = new_plane_state;
2705 for_each_oldnew_private_obj_in_state(state, obj, old_obj_state, new_obj_state, i) {
2706 WARN_ON(obj->state != old_obj_state);
2708 old_obj_state->state = state;
2709 new_obj_state->state = NULL;
2711 state->private_objs[i].state = old_obj_state;
2712 obj->state = new_obj_state;
2717 EXPORT_SYMBOL(drm_atomic_helper_swap_state);
2720 * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
2721 * @plane: plane object to update
2722 * @crtc: owning CRTC of owning plane
2723 * @fb: framebuffer to flip onto plane
2724 * @crtc_x: x offset of primary plane on crtc
2725 * @crtc_y: y offset of primary plane on crtc
2726 * @crtc_w: width of primary plane rectangle on crtc
2727 * @crtc_h: height of primary plane rectangle on crtc
2728 * @src_x: x offset of @fb for panning
2729 * @src_y: y offset of @fb for panning
2730 * @src_w: width of source rectangle in @fb
2731 * @src_h: height of source rectangle in @fb
2732 * @ctx: lock acquire context
2734 * Provides a default plane update handler using the atomic driver interface.
2737 * Zero on success, error code on failure
2739 int drm_atomic_helper_update_plane(struct drm_plane *plane,
2740 struct drm_crtc *crtc,
2741 struct drm_framebuffer *fb,
2742 int crtc_x, int crtc_y,
2743 unsigned int crtc_w, unsigned int crtc_h,
2744 uint32_t src_x, uint32_t src_y,
2745 uint32_t src_w, uint32_t src_h,
2746 struct drm_modeset_acquire_ctx *ctx)
2748 struct drm_atomic_state *state;
2749 struct drm_plane_state *plane_state;
2752 state = drm_atomic_state_alloc(plane->dev);
2756 state->acquire_ctx = ctx;
2757 plane_state = drm_atomic_get_plane_state(state, plane);
2758 if (IS_ERR(plane_state)) {
2759 ret = PTR_ERR(plane_state);
2763 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
2766 drm_atomic_set_fb_for_plane(plane_state, fb);
2767 plane_state->crtc_x = crtc_x;
2768 plane_state->crtc_y = crtc_y;
2769 plane_state->crtc_w = crtc_w;
2770 plane_state->crtc_h = crtc_h;
2771 plane_state->src_x = src_x;
2772 plane_state->src_y = src_y;
2773 plane_state->src_w = src_w;
2774 plane_state->src_h = src_h;
2776 if (plane == crtc->cursor)
2777 state->legacy_cursor_update = true;
2779 ret = drm_atomic_commit(state);
2781 drm_atomic_state_put(state);
2784 EXPORT_SYMBOL(drm_atomic_helper_update_plane);
2787 * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
2788 * @plane: plane to disable
2789 * @ctx: lock acquire context
2791 * Provides a default plane disable handler using the atomic driver interface.
2794 * Zero on success, error code on failure
2796 int drm_atomic_helper_disable_plane(struct drm_plane *plane,
2797 struct drm_modeset_acquire_ctx *ctx)
2799 struct drm_atomic_state *state;
2800 struct drm_plane_state *plane_state;
2803 state = drm_atomic_state_alloc(plane->dev);
2807 state->acquire_ctx = ctx;
2808 plane_state = drm_atomic_get_plane_state(state, plane);
2809 if (IS_ERR(plane_state)) {
2810 ret = PTR_ERR(plane_state);
2814 if (plane_state->crtc && plane_state->crtc->cursor == plane)
2815 plane_state->state->legacy_cursor_update = true;
2817 ret = __drm_atomic_helper_disable_plane(plane, plane_state);
2821 ret = drm_atomic_commit(state);
2823 drm_atomic_state_put(state);
2826 EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
2828 /* just used from fb-helper and atomic-helper: */
2829 int __drm_atomic_helper_disable_plane(struct drm_plane *plane,
2830 struct drm_plane_state *plane_state)
2834 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
2838 drm_atomic_set_fb_for_plane(plane_state, NULL);
2839 plane_state->crtc_x = 0;
2840 plane_state->crtc_y = 0;
2841 plane_state->crtc_w = 0;
2842 plane_state->crtc_h = 0;
2843 plane_state->src_x = 0;
2844 plane_state->src_y = 0;
2845 plane_state->src_w = 0;
2846 plane_state->src_h = 0;
2851 static int update_output_state(struct drm_atomic_state *state,
2852 struct drm_mode_set *set)
2854 struct drm_device *dev = set->crtc->dev;
2855 struct drm_crtc *crtc;
2856 struct drm_crtc_state *new_crtc_state;
2857 struct drm_connector *connector;
2858 struct drm_connector_state *new_conn_state;
2861 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
2862 state->acquire_ctx);
2866 /* First disable all connectors on the target crtc. */
2867 ret = drm_atomic_add_affected_connectors(state, set->crtc);
2871 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
2872 if (new_conn_state->crtc == set->crtc) {
2873 ret = drm_atomic_set_crtc_for_connector(new_conn_state,
2878 /* Make sure legacy setCrtc always re-trains */
2879 new_conn_state->link_status = DRM_LINK_STATUS_GOOD;
2883 /* Then set all connectors from set->connectors on the target crtc */
2884 for (i = 0; i < set->num_connectors; i++) {
2885 new_conn_state = drm_atomic_get_connector_state(state,
2886 set->connectors[i]);
2887 if (IS_ERR(new_conn_state))
2888 return PTR_ERR(new_conn_state);
2890 ret = drm_atomic_set_crtc_for_connector(new_conn_state,
2896 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
2897 /* Don't update ->enable for the CRTC in the set_config request,
2898 * since a mismatch would indicate a bug in the upper layers.
2899 * The actual modeset code later on will catch any
2900 * inconsistencies here. */
2901 if (crtc == set->crtc)
2904 if (!new_crtc_state->connector_mask) {
2905 ret = drm_atomic_set_mode_prop_for_crtc(new_crtc_state,
2910 new_crtc_state->active = false;
2918 * drm_atomic_helper_set_config - set a new config from userspace
2919 * @set: mode set configuration
2920 * @ctx: lock acquisition context
2922 * Provides a default crtc set_config handler using the atomic driver interface.
2924 * NOTE: For backwards compatibility with old userspace this automatically
2925 * resets the "link-status" property to GOOD, to force any link
2926 * re-training. The SETCRTC ioctl does not define whether an update does
2927 * need a full modeset or just a plane update, hence we're allowed to do
2928 * that. See also drm_connector_set_link_status_property().
2931 * Returns 0 on success, negative errno numbers on failure.
2933 int drm_atomic_helper_set_config(struct drm_mode_set *set,
2934 struct drm_modeset_acquire_ctx *ctx)
2936 struct drm_atomic_state *state;
2937 struct drm_crtc *crtc = set->crtc;
2940 state = drm_atomic_state_alloc(crtc->dev);
2944 state->acquire_ctx = ctx;
2945 ret = __drm_atomic_helper_set_config(set, state);
2949 ret = handle_conflicting_encoders(state, true);
2953 ret = drm_atomic_commit(state);
2956 drm_atomic_state_put(state);
2959 EXPORT_SYMBOL(drm_atomic_helper_set_config);
2961 /* just used from fb-helper and atomic-helper: */
2962 int __drm_atomic_helper_set_config(struct drm_mode_set *set,
2963 struct drm_atomic_state *state)
2965 struct drm_crtc_state *crtc_state;
2966 struct drm_plane_state *primary_state;
2967 struct drm_crtc *crtc = set->crtc;
2968 int hdisplay, vdisplay;
2971 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2972 if (IS_ERR(crtc_state))
2973 return PTR_ERR(crtc_state);
2975 primary_state = drm_atomic_get_plane_state(state, crtc->primary);
2976 if (IS_ERR(primary_state))
2977 return PTR_ERR(primary_state);
2981 WARN_ON(set->num_connectors);
2983 ret = drm_atomic_set_mode_for_crtc(crtc_state, NULL);
2987 crtc_state->active = false;
2989 ret = drm_atomic_set_crtc_for_plane(primary_state, NULL);
2993 drm_atomic_set_fb_for_plane(primary_state, NULL);
2999 WARN_ON(!set->num_connectors);
3001 ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
3005 crtc_state->active = true;
3007 ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
3011 drm_mode_get_hv_timing(set->mode, &hdisplay, &vdisplay);
3013 drm_atomic_set_fb_for_plane(primary_state, set->fb);
3014 primary_state->crtc_x = 0;
3015 primary_state->crtc_y = 0;
3016 primary_state->crtc_w = hdisplay;
3017 primary_state->crtc_h = vdisplay;
3018 primary_state->src_x = set->x << 16;
3019 primary_state->src_y = set->y << 16;
3020 if (drm_rotation_90_or_270(primary_state->rotation)) {
3021 primary_state->src_w = vdisplay << 16;
3022 primary_state->src_h = hdisplay << 16;
3024 primary_state->src_w = hdisplay << 16;
3025 primary_state->src_h = vdisplay << 16;
3029 ret = update_output_state(state, set);
3036 static int __drm_atomic_helper_disable_all(struct drm_device *dev,
3037 struct drm_modeset_acquire_ctx *ctx,
3040 struct drm_atomic_state *state;
3041 struct drm_connector_state *conn_state;
3042 struct drm_connector *conn;
3043 struct drm_plane_state *plane_state;
3044 struct drm_plane *plane;
3045 struct drm_crtc_state *crtc_state;
3046 struct drm_crtc *crtc;
3049 state = drm_atomic_state_alloc(dev);
3053 state->acquire_ctx = ctx;
3055 drm_for_each_crtc(crtc, dev) {
3056 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3057 if (IS_ERR(crtc_state)) {
3058 ret = PTR_ERR(crtc_state);
3062 crtc_state->active = false;
3064 ret = drm_atomic_set_mode_prop_for_crtc(crtc_state, NULL);
3068 ret = drm_atomic_add_affected_planes(state, crtc);
3072 ret = drm_atomic_add_affected_connectors(state, crtc);
3077 for_each_new_connector_in_state(state, conn, conn_state, i) {
3078 ret = drm_atomic_set_crtc_for_connector(conn_state, NULL);
3083 for_each_new_plane_in_state(state, plane, plane_state, i) {
3084 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
3088 drm_atomic_set_fb_for_plane(plane_state, NULL);
3091 ret = drm_atomic_commit(state);
3093 drm_atomic_state_put(state);
3098 * drm_atomic_helper_disable_all - disable all currently active outputs
3100 * @ctx: lock acquisition context
3102 * Loops through all connectors, finding those that aren't turned off and then
3103 * turns them off by setting their DPMS mode to OFF and deactivating the CRTC
3104 * that they are connected to.
3106 * This is used for example in suspend/resume to disable all currently active
3107 * functions when suspending. If you just want to shut down everything at e.g.
3108 * driver unload, look at drm_atomic_helper_shutdown().
3110 * Note that if callers haven't already acquired all modeset locks this might
3111 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
3114 * 0 on success or a negative error code on failure.
3117 * drm_atomic_helper_suspend(), drm_atomic_helper_resume() and
3118 * drm_atomic_helper_shutdown().
3120 int drm_atomic_helper_disable_all(struct drm_device *dev,
3121 struct drm_modeset_acquire_ctx *ctx)
3123 return __drm_atomic_helper_disable_all(dev, ctx, false);
3125 EXPORT_SYMBOL(drm_atomic_helper_disable_all);
3128 * drm_atomic_helper_shutdown - shutdown all CRTC
3131 * This shuts down all CRTC, which is useful for driver unloading. Shutdown on
3132 * suspend should instead be handled with drm_atomic_helper_suspend(), since
3133 * that also takes a snapshot of the modeset state to be restored on resume.
3135 * This is just a convenience wrapper around drm_atomic_helper_disable_all(),
3136 * and it is the atomic version of drm_crtc_force_disable_all().
3138 void drm_atomic_helper_shutdown(struct drm_device *dev)
3140 struct drm_modeset_acquire_ctx ctx;
3143 DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx, 0, ret);
3145 ret = __drm_atomic_helper_disable_all(dev, &ctx, true);
3147 DRM_ERROR("Disabling all crtc's during unload failed with %i\n", ret);
3149 DRM_MODESET_LOCK_ALL_END(ctx, ret);
3151 EXPORT_SYMBOL(drm_atomic_helper_shutdown);
3154 * drm_atomic_helper_duplicate_state - duplicate an atomic state object
3156 * @ctx: lock acquisition context
3158 * Makes a copy of the current atomic state by looping over all objects and
3159 * duplicating their respective states. This is used for example by suspend/
3160 * resume support code to save the state prior to suspend such that it can
3161 * be restored upon resume.
3163 * Note that this treats atomic state as persistent between save and restore.
3164 * Drivers must make sure that this is possible and won't result in confusion
3165 * or erroneous behaviour.
3167 * Note that if callers haven't already acquired all modeset locks this might
3168 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
3171 * A pointer to the copy of the atomic state object on success or an
3172 * ERR_PTR()-encoded error code on failure.
3175 * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
3177 struct drm_atomic_state *
3178 drm_atomic_helper_duplicate_state(struct drm_device *dev,
3179 struct drm_modeset_acquire_ctx *ctx)
3181 struct drm_atomic_state *state;
3182 struct drm_connector *conn;
3183 struct drm_connector_list_iter conn_iter;
3184 struct drm_plane *plane;
3185 struct drm_crtc *crtc;
3188 state = drm_atomic_state_alloc(dev);
3190 return ERR_PTR(-ENOMEM);
3192 state->acquire_ctx = ctx;
3194 drm_for_each_crtc(crtc, dev) {
3195 struct drm_crtc_state *crtc_state;
3197 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3198 if (IS_ERR(crtc_state)) {
3199 err = PTR_ERR(crtc_state);
3204 drm_for_each_plane(plane, dev) {
3205 struct drm_plane_state *plane_state;
3207 plane_state = drm_atomic_get_plane_state(state, plane);
3208 if (IS_ERR(plane_state)) {
3209 err = PTR_ERR(plane_state);
3214 drm_connector_list_iter_begin(dev, &conn_iter);
3215 drm_for_each_connector_iter(conn, &conn_iter) {
3216 struct drm_connector_state *conn_state;
3218 conn_state = drm_atomic_get_connector_state(state, conn);
3219 if (IS_ERR(conn_state)) {
3220 err = PTR_ERR(conn_state);
3221 drm_connector_list_iter_end(&conn_iter);
3225 drm_connector_list_iter_end(&conn_iter);
3227 /* clear the acquire context so that it isn't accidentally reused */
3228 state->acquire_ctx = NULL;
3232 drm_atomic_state_put(state);
3233 state = ERR_PTR(err);
3238 EXPORT_SYMBOL(drm_atomic_helper_duplicate_state);
3241 * drm_atomic_helper_suspend - subsystem-level suspend helper
3244 * Duplicates the current atomic state, disables all active outputs and then
3245 * returns a pointer to the original atomic state to the caller. Drivers can
3246 * pass this pointer to the drm_atomic_helper_resume() helper upon resume to
3247 * restore the output configuration that was active at the time the system
3250 * Note that it is potentially unsafe to use this. The atomic state object
3251 * returned by this function is assumed to be persistent. Drivers must ensure
3252 * that this holds true. Before calling this function, drivers must make sure
3253 * to suspend fbdev emulation so that nothing can be using the device.
3256 * A pointer to a copy of the state before suspend on success or an ERR_PTR()-
3257 * encoded error code on failure. Drivers should store the returned atomic
3258 * state object and pass it to the drm_atomic_helper_resume() helper upon
3262 * drm_atomic_helper_duplicate_state(), drm_atomic_helper_disable_all(),
3263 * drm_atomic_helper_resume(), drm_atomic_helper_commit_duplicated_state()
3265 struct drm_atomic_state *drm_atomic_helper_suspend(struct drm_device *dev)
3267 struct drm_modeset_acquire_ctx ctx;
3268 struct drm_atomic_state *state;
3271 /* This can never be returned, but it makes the compiler happy */
3272 state = ERR_PTR(-EINVAL);
3274 DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx, 0, err);
3276 state = drm_atomic_helper_duplicate_state(dev, &ctx);
3280 err = drm_atomic_helper_disable_all(dev, &ctx);
3282 drm_atomic_state_put(state);
3283 state = ERR_PTR(err);
3288 DRM_MODESET_LOCK_ALL_END(ctx, err);
3290 return ERR_PTR(err);
3294 EXPORT_SYMBOL(drm_atomic_helper_suspend);
3297 * drm_atomic_helper_commit_duplicated_state - commit duplicated state
3298 * @state: duplicated atomic state to commit
3299 * @ctx: pointer to acquire_ctx to use for commit.
3301 * The state returned by drm_atomic_helper_duplicate_state() and
3302 * drm_atomic_helper_suspend() is partially invalid, and needs to
3303 * be fixed up before commit.
3306 * 0 on success or a negative error code on failure.
3309 * drm_atomic_helper_suspend()
3311 int drm_atomic_helper_commit_duplicated_state(struct drm_atomic_state *state,
3312 struct drm_modeset_acquire_ctx *ctx)
3315 struct drm_plane *plane;
3316 struct drm_plane_state *new_plane_state;
3317 struct drm_connector *connector;
3318 struct drm_connector_state *new_conn_state;
3319 struct drm_crtc *crtc;
3320 struct drm_crtc_state *new_crtc_state;
3322 state->acquire_ctx = ctx;
3324 for_each_new_plane_in_state(state, plane, new_plane_state, i)
3325 state->planes[i].old_state = plane->state;
3327 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i)
3328 state->crtcs[i].old_state = crtc->state;
3330 for_each_new_connector_in_state(state, connector, new_conn_state, i)
3331 state->connectors[i].old_state = connector->state;
3333 ret = drm_atomic_commit(state);
3335 state->acquire_ctx = NULL;
3339 EXPORT_SYMBOL(drm_atomic_helper_commit_duplicated_state);
3342 * drm_atomic_helper_resume - subsystem-level resume helper
3344 * @state: atomic state to resume to
3346 * Calls drm_mode_config_reset() to synchronize hardware and software states,
3347 * grabs all modeset locks and commits the atomic state object. This can be
3348 * used in conjunction with the drm_atomic_helper_suspend() helper to
3349 * implement suspend/resume for drivers that support atomic mode-setting.
3352 * 0 on success or a negative error code on failure.
3355 * drm_atomic_helper_suspend()
3357 int drm_atomic_helper_resume(struct drm_device *dev,
3358 struct drm_atomic_state *state)
3360 struct drm_modeset_acquire_ctx ctx;
3363 drm_mode_config_reset(dev);
3365 DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx, 0, err);
3367 err = drm_atomic_helper_commit_duplicated_state(state, &ctx);
3369 DRM_MODESET_LOCK_ALL_END(ctx, err);
3370 drm_atomic_state_put(state);
3374 EXPORT_SYMBOL(drm_atomic_helper_resume);
3376 static int page_flip_common(struct drm_atomic_state *state,
3377 struct drm_crtc *crtc,
3378 struct drm_framebuffer *fb,
3379 struct drm_pending_vblank_event *event,
3382 struct drm_plane *plane = crtc->primary;
3383 struct drm_plane_state *plane_state;
3384 struct drm_crtc_state *crtc_state;
3387 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3388 if (IS_ERR(crtc_state))
3389 return PTR_ERR(crtc_state);
3391 crtc_state->event = event;
3392 crtc_state->pageflip_flags = flags;
3394 plane_state = drm_atomic_get_plane_state(state, plane);
3395 if (IS_ERR(plane_state))
3396 return PTR_ERR(plane_state);
3398 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
3401 drm_atomic_set_fb_for_plane(plane_state, fb);
3403 /* Make sure we don't accidentally do a full modeset. */
3404 state->allow_modeset = false;
3405 if (!crtc_state->active) {
3406 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] disabled, rejecting legacy flip\n",
3407 crtc->base.id, crtc->name);
3415 * drm_atomic_helper_page_flip - execute a legacy page flip
3417 * @fb: DRM framebuffer
3418 * @event: optional DRM event to signal upon completion
3419 * @flags: flip flags for non-vblank sync'ed updates
3420 * @ctx: lock acquisition context
3422 * Provides a default &drm_crtc_funcs.page_flip implementation
3423 * using the atomic driver interface.
3426 * Returns 0 on success, negative errno numbers on failure.
3429 * drm_atomic_helper_page_flip_target()
3431 int drm_atomic_helper_page_flip(struct drm_crtc *crtc,
3432 struct drm_framebuffer *fb,
3433 struct drm_pending_vblank_event *event,
3435 struct drm_modeset_acquire_ctx *ctx)
3437 struct drm_plane *plane = crtc->primary;
3438 struct drm_atomic_state *state;
3441 state = drm_atomic_state_alloc(plane->dev);
3445 state->acquire_ctx = ctx;
3447 ret = page_flip_common(state, crtc, fb, event, flags);
3451 ret = drm_atomic_nonblocking_commit(state);
3453 drm_atomic_state_put(state);
3456 EXPORT_SYMBOL(drm_atomic_helper_page_flip);
3459 * drm_atomic_helper_page_flip_target - do page flip on target vblank period.
3461 * @fb: DRM framebuffer
3462 * @event: optional DRM event to signal upon completion
3463 * @flags: flip flags for non-vblank sync'ed updates
3464 * @target: specifying the target vblank period when the flip to take effect
3465 * @ctx: lock acquisition context
3467 * Provides a default &drm_crtc_funcs.page_flip_target implementation.
3468 * Similar to drm_atomic_helper_page_flip() with extra parameter to specify
3469 * target vblank period to flip.
3472 * Returns 0 on success, negative errno numbers on failure.
3474 int drm_atomic_helper_page_flip_target(struct drm_crtc *crtc,
3475 struct drm_framebuffer *fb,
3476 struct drm_pending_vblank_event *event,
3479 struct drm_modeset_acquire_ctx *ctx)
3481 struct drm_plane *plane = crtc->primary;
3482 struct drm_atomic_state *state;
3483 struct drm_crtc_state *crtc_state;
3486 state = drm_atomic_state_alloc(plane->dev);
3490 state->acquire_ctx = ctx;
3492 ret = page_flip_common(state, crtc, fb, event, flags);
3496 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
3497 if (WARN_ON(!crtc_state)) {
3501 crtc_state->target_vblank = target;
3503 ret = drm_atomic_nonblocking_commit(state);
3505 drm_atomic_state_put(state);
3508 EXPORT_SYMBOL(drm_atomic_helper_page_flip_target);
3511 * drm_atomic_helper_legacy_gamma_set - set the legacy gamma correction table
3512 * @crtc: CRTC object
3513 * @red: red correction table
3514 * @green: green correction table
3515 * @blue: green correction table
3516 * @size: size of the tables
3517 * @ctx: lock acquire context
3519 * Implements support for legacy gamma correction table for drivers
3520 * that support color management through the DEGAMMA_LUT/GAMMA_LUT
3521 * properties. See drm_crtc_enable_color_mgmt() and the containing chapter for
3522 * how the atomic color management and gamma tables work.
3524 int drm_atomic_helper_legacy_gamma_set(struct drm_crtc *crtc,
3525 u16 *red, u16 *green, u16 *blue,
3527 struct drm_modeset_acquire_ctx *ctx)
3529 struct drm_device *dev = crtc->dev;
3530 struct drm_atomic_state *state;
3531 struct drm_crtc_state *crtc_state;
3532 struct drm_property_blob *blob = NULL;
3533 struct drm_color_lut *blob_data;
3537 state = drm_atomic_state_alloc(crtc->dev);
3541 blob = drm_property_create_blob(dev,
3542 sizeof(struct drm_color_lut) * size,
3545 ret = PTR_ERR(blob);
3550 /* Prepare GAMMA_LUT with the legacy values. */
3551 blob_data = blob->data;
3552 for (i = 0; i < size; i++) {
3553 blob_data[i].red = red[i];
3554 blob_data[i].green = green[i];
3555 blob_data[i].blue = blue[i];
3558 state->acquire_ctx = ctx;
3559 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3560 if (IS_ERR(crtc_state)) {
3561 ret = PTR_ERR(crtc_state);
3565 /* Reset DEGAMMA_LUT and CTM properties. */
3566 replaced = drm_property_replace_blob(&crtc_state->degamma_lut, NULL);
3567 replaced |= drm_property_replace_blob(&crtc_state->ctm, NULL);
3568 replaced |= drm_property_replace_blob(&crtc_state->gamma_lut, blob);
3569 crtc_state->color_mgmt_changed |= replaced;
3571 ret = drm_atomic_commit(state);
3574 drm_atomic_state_put(state);
3575 drm_property_blob_put(blob);
3578 EXPORT_SYMBOL(drm_atomic_helper_legacy_gamma_set);