2 * Copyright (C) 2014 Red Hat
3 * Copyright (C) 2014 Intel Corp.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
24 * Rob Clark <robdclark@gmail.com>
25 * Daniel Vetter <daniel.vetter@ffwll.ch>
29 #include <drm/drm_atomic.h>
30 #include <drm/drm_plane_helper.h>
31 #include <drm/drm_crtc_helper.h>
32 #include <drm/drm_atomic_helper.h>
33 #include <drm/drm_writeback.h>
34 #include <linux/dma-fence.h>
36 #include "drm_crtc_helper_internal.h"
37 #include "drm_crtc_internal.h"
42 * This helper library provides implementations of check and commit functions on
43 * top of the CRTC modeset helper callbacks and the plane helper callbacks. It
44 * also provides convenience implementations for the atomic state handling
45 * callbacks for drivers which don't need to subclass the drm core structures to
46 * add their own additional internal state.
48 * This library also provides default implementations for the check callback in
49 * drm_atomic_helper_check() and for the commit callback with
50 * drm_atomic_helper_commit(). But the individual stages and callbacks are
51 * exposed to allow drivers to mix and match and e.g. use the plane helpers only
52 * together with a driver private modeset implementation.
54 * This library also provides implementations for all the legacy driver
55 * interfaces on top of the atomic interface. See drm_atomic_helper_set_config(),
56 * drm_atomic_helper_disable_plane(), drm_atomic_helper_disable_plane() and the
57 * various functions to implement set_property callbacks. New drivers must not
58 * implement these functions themselves but must use the provided helpers.
60 * The atomic helper uses the same function table structures as all other
61 * modesetting helpers. See the documentation for &struct drm_crtc_helper_funcs,
62 * struct &drm_encoder_helper_funcs and &struct drm_connector_helper_funcs. It
63 * also shares the &struct drm_plane_helper_funcs function table with the plane
67 drm_atomic_helper_plane_changed(struct drm_atomic_state *state,
68 struct drm_plane_state *old_plane_state,
69 struct drm_plane_state *plane_state,
70 struct drm_plane *plane)
72 struct drm_crtc_state *crtc_state;
74 if (old_plane_state->crtc) {
75 crtc_state = drm_atomic_get_new_crtc_state(state,
76 old_plane_state->crtc);
78 if (WARN_ON(!crtc_state))
81 crtc_state->planes_changed = true;
84 if (plane_state->crtc) {
85 crtc_state = drm_atomic_get_new_crtc_state(state, plane_state->crtc);
87 if (WARN_ON(!crtc_state))
90 crtc_state->planes_changed = true;
94 static int handle_conflicting_encoders(struct drm_atomic_state *state,
95 bool disable_conflicting_encoders)
97 struct drm_connector_state *new_conn_state;
98 struct drm_connector *connector;
99 struct drm_connector_list_iter conn_iter;
100 struct drm_encoder *encoder;
101 unsigned encoder_mask = 0;
105 * First loop, find all newly assigned encoders from the connectors
106 * part of the state. If the same encoder is assigned to multiple
107 * connectors bail out.
109 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
110 const struct drm_connector_helper_funcs *funcs = connector->helper_private;
111 struct drm_encoder *new_encoder;
113 if (!new_conn_state->crtc)
116 if (funcs->atomic_best_encoder)
117 new_encoder = funcs->atomic_best_encoder(connector, new_conn_state);
118 else if (funcs->best_encoder)
119 new_encoder = funcs->best_encoder(connector);
121 new_encoder = drm_atomic_helper_best_encoder(connector);
124 if (encoder_mask & drm_encoder_mask(new_encoder)) {
125 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] on [CONNECTOR:%d:%s] already assigned\n",
126 new_encoder->base.id, new_encoder->name,
127 connector->base.id, connector->name);
132 encoder_mask |= drm_encoder_mask(new_encoder);
140 * Second loop, iterate over all connectors not part of the state.
142 * If a conflicting encoder is found and disable_conflicting_encoders
143 * is not set, an error is returned. Userspace can provide a solution
144 * through the atomic ioctl.
146 * If the flag is set conflicting connectors are removed from the crtc
147 * and the crtc is disabled if no encoder is left. This preserves
148 * compatibility with the legacy set_config behavior.
150 drm_connector_list_iter_begin(state->dev, &conn_iter);
151 drm_for_each_connector_iter(connector, &conn_iter) {
152 struct drm_crtc_state *crtc_state;
154 if (drm_atomic_get_new_connector_state(state, connector))
157 encoder = connector->state->best_encoder;
158 if (!encoder || !(encoder_mask & drm_encoder_mask(encoder)))
161 if (!disable_conflicting_encoders) {
162 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s] by [CONNECTOR:%d:%s]\n",
163 encoder->base.id, encoder->name,
164 connector->state->crtc->base.id,
165 connector->state->crtc->name,
166 connector->base.id, connector->name);
171 new_conn_state = drm_atomic_get_connector_state(state, connector);
172 if (IS_ERR(new_conn_state)) {
173 ret = PTR_ERR(new_conn_state);
177 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s], disabling [CONNECTOR:%d:%s]\n",
178 encoder->base.id, encoder->name,
179 new_conn_state->crtc->base.id, new_conn_state->crtc->name,
180 connector->base.id, connector->name);
182 crtc_state = drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
184 ret = drm_atomic_set_crtc_for_connector(new_conn_state, NULL);
188 if (!crtc_state->connector_mask) {
189 ret = drm_atomic_set_mode_prop_for_crtc(crtc_state,
194 crtc_state->active = false;
198 drm_connector_list_iter_end(&conn_iter);
204 set_best_encoder(struct drm_atomic_state *state,
205 struct drm_connector_state *conn_state,
206 struct drm_encoder *encoder)
208 struct drm_crtc_state *crtc_state;
209 struct drm_crtc *crtc;
211 if (conn_state->best_encoder) {
212 /* Unset the encoder_mask in the old crtc state. */
213 crtc = conn_state->connector->state->crtc;
215 /* A NULL crtc is an error here because we should have
216 * duplicated a NULL best_encoder when crtc was NULL.
217 * As an exception restoring duplicated atomic state
218 * during resume is allowed, so don't warn when
219 * best_encoder is equal to encoder we intend to set.
221 WARN_ON(!crtc && encoder != conn_state->best_encoder);
223 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
225 crtc_state->encoder_mask &=
226 ~drm_encoder_mask(conn_state->best_encoder);
231 crtc = conn_state->crtc;
234 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
236 crtc_state->encoder_mask |=
237 drm_encoder_mask(encoder);
241 conn_state->best_encoder = encoder;
245 steal_encoder(struct drm_atomic_state *state,
246 struct drm_encoder *encoder)
248 struct drm_crtc_state *crtc_state;
249 struct drm_connector *connector;
250 struct drm_connector_state *old_connector_state, *new_connector_state;
253 for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
254 struct drm_crtc *encoder_crtc;
256 if (new_connector_state->best_encoder != encoder)
259 encoder_crtc = old_connector_state->crtc;
261 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s], stealing it\n",
262 encoder->base.id, encoder->name,
263 encoder_crtc->base.id, encoder_crtc->name);
265 set_best_encoder(state, new_connector_state, NULL);
267 crtc_state = drm_atomic_get_new_crtc_state(state, encoder_crtc);
268 crtc_state->connectors_changed = true;
275 update_connector_routing(struct drm_atomic_state *state,
276 struct drm_connector *connector,
277 struct drm_connector_state *old_connector_state,
278 struct drm_connector_state *new_connector_state)
280 const struct drm_connector_helper_funcs *funcs;
281 struct drm_encoder *new_encoder;
282 struct drm_crtc_state *crtc_state;
284 DRM_DEBUG_ATOMIC("Updating routing for [CONNECTOR:%d:%s]\n",
288 if (old_connector_state->crtc != new_connector_state->crtc) {
289 if (old_connector_state->crtc) {
290 crtc_state = drm_atomic_get_new_crtc_state(state, old_connector_state->crtc);
291 crtc_state->connectors_changed = true;
294 if (new_connector_state->crtc) {
295 crtc_state = drm_atomic_get_new_crtc_state(state, new_connector_state->crtc);
296 crtc_state->connectors_changed = true;
300 if (!new_connector_state->crtc) {
301 DRM_DEBUG_ATOMIC("Disabling [CONNECTOR:%d:%s]\n",
305 set_best_encoder(state, new_connector_state, NULL);
310 crtc_state = drm_atomic_get_new_crtc_state(state,
311 new_connector_state->crtc);
313 * For compatibility with legacy users, we want to make sure that
314 * we allow DPMS On->Off modesets on unregistered connectors. Modesets
315 * which would result in anything else must be considered invalid, to
316 * avoid turning on new displays on dead connectors.
318 * Since the connector can be unregistered at any point during an
319 * atomic check or commit, this is racy. But that's OK: all we care
320 * about is ensuring that userspace can't do anything but shut off the
321 * display on a connector that was destroyed after its been notified,
324 if (drm_connector_is_unregistered(connector) && crtc_state->active) {
325 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] is not registered\n",
326 connector->base.id, connector->name);
330 funcs = connector->helper_private;
332 if (funcs->atomic_best_encoder)
333 new_encoder = funcs->atomic_best_encoder(connector,
334 new_connector_state);
335 else if (funcs->best_encoder)
336 new_encoder = funcs->best_encoder(connector);
338 new_encoder = drm_atomic_helper_best_encoder(connector);
341 DRM_DEBUG_ATOMIC("No suitable encoder found for [CONNECTOR:%d:%s]\n",
347 if (!drm_encoder_crtc_ok(new_encoder, new_connector_state->crtc)) {
348 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] incompatible with [CRTC:%d:%s]\n",
349 new_encoder->base.id,
351 new_connector_state->crtc->base.id,
352 new_connector_state->crtc->name);
356 if (new_encoder == new_connector_state->best_encoder) {
357 set_best_encoder(state, new_connector_state, new_encoder);
359 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] keeps [ENCODER:%d:%s], now on [CRTC:%d:%s]\n",
362 new_encoder->base.id,
364 new_connector_state->crtc->base.id,
365 new_connector_state->crtc->name);
370 steal_encoder(state, new_encoder);
372 set_best_encoder(state, new_connector_state, new_encoder);
374 crtc_state->connectors_changed = true;
376 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] using [ENCODER:%d:%s] on [CRTC:%d:%s]\n",
379 new_encoder->base.id,
381 new_connector_state->crtc->base.id,
382 new_connector_state->crtc->name);
388 mode_fixup(struct drm_atomic_state *state)
390 struct drm_crtc *crtc;
391 struct drm_crtc_state *new_crtc_state;
392 struct drm_connector *connector;
393 struct drm_connector_state *new_conn_state;
397 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
398 if (!new_crtc_state->mode_changed &&
399 !new_crtc_state->connectors_changed)
402 drm_mode_copy(&new_crtc_state->adjusted_mode, &new_crtc_state->mode);
405 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
406 const struct drm_encoder_helper_funcs *funcs;
407 struct drm_encoder *encoder;
409 WARN_ON(!!new_conn_state->best_encoder != !!new_conn_state->crtc);
411 if (!new_conn_state->crtc || !new_conn_state->best_encoder)
415 drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
418 * Each encoder has at most one connector (since we always steal
419 * it away), so we won't call ->mode_fixup twice.
421 encoder = new_conn_state->best_encoder;
422 funcs = encoder->helper_private;
424 ret = drm_bridge_mode_fixup(encoder->bridge, &new_crtc_state->mode,
425 &new_crtc_state->adjusted_mode);
427 DRM_DEBUG_ATOMIC("Bridge fixup failed\n");
431 if (funcs && funcs->atomic_check) {
432 ret = funcs->atomic_check(encoder, new_crtc_state,
435 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] check failed\n",
436 encoder->base.id, encoder->name);
439 } else if (funcs && funcs->mode_fixup) {
440 ret = funcs->mode_fixup(encoder, &new_crtc_state->mode,
441 &new_crtc_state->adjusted_mode);
443 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] fixup failed\n",
444 encoder->base.id, encoder->name);
450 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
451 const struct drm_crtc_helper_funcs *funcs;
453 if (!new_crtc_state->enable)
456 if (!new_crtc_state->mode_changed &&
457 !new_crtc_state->connectors_changed)
460 funcs = crtc->helper_private;
461 if (!funcs->mode_fixup)
464 ret = funcs->mode_fixup(crtc, &new_crtc_state->mode,
465 &new_crtc_state->adjusted_mode);
467 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] fixup failed\n",
468 crtc->base.id, crtc->name);
476 static enum drm_mode_status mode_valid_path(struct drm_connector *connector,
477 struct drm_encoder *encoder,
478 struct drm_crtc *crtc,
479 struct drm_display_mode *mode)
481 enum drm_mode_status ret;
483 ret = drm_encoder_mode_valid(encoder, mode);
484 if (ret != MODE_OK) {
485 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] mode_valid() failed\n",
486 encoder->base.id, encoder->name);
490 ret = drm_bridge_mode_valid(encoder->bridge, mode);
491 if (ret != MODE_OK) {
492 DRM_DEBUG_ATOMIC("[BRIDGE] mode_valid() failed\n");
496 ret = drm_crtc_mode_valid(crtc, mode);
497 if (ret != MODE_OK) {
498 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] mode_valid() failed\n",
499 crtc->base.id, crtc->name);
507 mode_valid(struct drm_atomic_state *state)
509 struct drm_connector_state *conn_state;
510 struct drm_connector *connector;
513 for_each_new_connector_in_state(state, connector, conn_state, i) {
514 struct drm_encoder *encoder = conn_state->best_encoder;
515 struct drm_crtc *crtc = conn_state->crtc;
516 struct drm_crtc_state *crtc_state;
517 enum drm_mode_status mode_status;
518 struct drm_display_mode *mode;
520 if (!crtc || !encoder)
523 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
526 if (!crtc_state->mode_changed && !crtc_state->connectors_changed)
529 mode = &crtc_state->mode;
531 mode_status = mode_valid_path(connector, encoder, crtc, mode);
532 if (mode_status != MODE_OK)
540 * drm_atomic_helper_check_modeset - validate state object for modeset changes
542 * @state: the driver state object
544 * Check the state object to see if the requested state is physically possible.
545 * This does all the crtc and connector related computations for an atomic
546 * update and adds any additional connectors needed for full modesets. It calls
547 * the various per-object callbacks in the follow order:
549 * 1. &drm_connector_helper_funcs.atomic_best_encoder for determining the new encoder.
550 * 2. &drm_connector_helper_funcs.atomic_check to validate the connector state.
551 * 3. If it's determined a modeset is needed then all connectors on the affected crtc
552 * crtc are added and &drm_connector_helper_funcs.atomic_check is run on them.
553 * 4. &drm_encoder_helper_funcs.mode_valid, &drm_bridge_funcs.mode_valid and
554 * &drm_crtc_helper_funcs.mode_valid are called on the affected components.
555 * 5. &drm_bridge_funcs.mode_fixup is called on all encoder bridges.
556 * 6. &drm_encoder_helper_funcs.atomic_check is called to validate any encoder state.
557 * This function is only called when the encoder will be part of a configured crtc,
558 * it must not be used for implementing connector property validation.
559 * If this function is NULL, &drm_atomic_encoder_helper_funcs.mode_fixup is called
561 * 7. &drm_crtc_helper_funcs.mode_fixup is called last, to fix up the mode with crtc constraints.
563 * &drm_crtc_state.mode_changed is set when the input mode is changed.
564 * &drm_crtc_state.connectors_changed is set when a connector is added or
565 * removed from the crtc. &drm_crtc_state.active_changed is set when
566 * &drm_crtc_state.active changes, which is used for DPMS.
567 * See also: drm_atomic_crtc_needs_modeset()
571 * Drivers which set &drm_crtc_state.mode_changed (e.g. in their
572 * &drm_plane_helper_funcs.atomic_check hooks if a plane update can't be done
573 * without a full modeset) _must_ call this function afterwards after that
574 * change. It is permitted to call this function multiple times for the same
575 * update, e.g. when the &drm_crtc_helper_funcs.atomic_check functions depend
576 * upon the adjusted dotclock for fifo space allocation and watermark
580 * Zero for success or -errno
583 drm_atomic_helper_check_modeset(struct drm_device *dev,
584 struct drm_atomic_state *state)
586 struct drm_crtc *crtc;
587 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
588 struct drm_connector *connector;
589 struct drm_connector_state *old_connector_state, *new_connector_state;
591 unsigned connectors_mask = 0;
593 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
594 bool has_connectors =
595 !!new_crtc_state->connector_mask;
597 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
599 if (!drm_mode_equal(&old_crtc_state->mode, &new_crtc_state->mode)) {
600 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] mode changed\n",
601 crtc->base.id, crtc->name);
602 new_crtc_state->mode_changed = true;
605 if (old_crtc_state->enable != new_crtc_state->enable) {
606 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enable changed\n",
607 crtc->base.id, crtc->name);
610 * For clarity this assignment is done here, but
611 * enable == 0 is only true when there are no
612 * connectors and a NULL mode.
614 * The other way around is true as well. enable != 0
615 * iff connectors are attached and a mode is set.
617 new_crtc_state->mode_changed = true;
618 new_crtc_state->connectors_changed = true;
621 if (old_crtc_state->active != new_crtc_state->active) {
622 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active changed\n",
623 crtc->base.id, crtc->name);
624 new_crtc_state->active_changed = true;
627 if (new_crtc_state->enable != has_connectors) {
628 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled/connectors mismatch\n",
629 crtc->base.id, crtc->name);
635 ret = handle_conflicting_encoders(state, false);
639 for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
640 const struct drm_connector_helper_funcs *funcs = connector->helper_private;
642 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
645 * This only sets crtc->connectors_changed for routing changes,
646 * drivers must set crtc->connectors_changed themselves when
647 * connector properties need to be updated.
649 ret = update_connector_routing(state, connector,
651 new_connector_state);
654 if (old_connector_state->crtc) {
655 new_crtc_state = drm_atomic_get_new_crtc_state(state,
656 old_connector_state->crtc);
657 if (old_connector_state->link_status !=
658 new_connector_state->link_status)
659 new_crtc_state->connectors_changed = true;
662 if (funcs->atomic_check)
663 ret = funcs->atomic_check(connector, new_connector_state);
667 connectors_mask |= BIT(i);
671 * After all the routing has been prepared we need to add in any
672 * connector which is itself unchanged, but who's crtc changes it's
673 * configuration. This must be done before calling mode_fixup in case a
674 * crtc only changed its mode but has the same set of connectors.
676 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
677 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
680 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] needs all connectors, enable: %c, active: %c\n",
681 crtc->base.id, crtc->name,
682 new_crtc_state->enable ? 'y' : 'n',
683 new_crtc_state->active ? 'y' : 'n');
685 ret = drm_atomic_add_affected_connectors(state, crtc);
689 ret = drm_atomic_add_affected_planes(state, crtc);
695 * Iterate over all connectors again, to make sure atomic_check()
696 * has been called on them when a modeset is forced.
698 for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
699 const struct drm_connector_helper_funcs *funcs = connector->helper_private;
701 if (connectors_mask & BIT(i))
704 if (funcs->atomic_check)
705 ret = funcs->atomic_check(connector, new_connector_state);
710 ret = mode_valid(state);
714 return mode_fixup(state);
716 EXPORT_SYMBOL(drm_atomic_helper_check_modeset);
719 * drm_atomic_helper_check_plane_state() - Check plane state for validity
720 * @plane_state: plane state to check
721 * @crtc_state: crtc state to check
722 * @min_scale: minimum @src:@dest scaling factor in 16.16 fixed point
723 * @max_scale: maximum @src:@dest scaling factor in 16.16 fixed point
724 * @can_position: is it legal to position the plane such that it
725 * doesn't cover the entire crtc? This will generally
726 * only be false for primary planes.
727 * @can_update_disabled: can the plane be updated while the crtc
730 * Checks that a desired plane update is valid, and updates various
731 * bits of derived state (clipped coordinates etc.). Drivers that provide
732 * their own plane handling rather than helper-provided implementations may
733 * still wish to call this function to avoid duplication of error checking
737 * Zero if update appears valid, error code on failure
739 int drm_atomic_helper_check_plane_state(struct drm_plane_state *plane_state,
740 const struct drm_crtc_state *crtc_state,
744 bool can_update_disabled)
746 struct drm_framebuffer *fb = plane_state->fb;
747 struct drm_rect *src = &plane_state->src;
748 struct drm_rect *dst = &plane_state->dst;
749 unsigned int rotation = plane_state->rotation;
750 struct drm_rect clip = {};
753 WARN_ON(plane_state->crtc && plane_state->crtc != crtc_state->crtc);
755 *src = drm_plane_state_src(plane_state);
756 *dst = drm_plane_state_dest(plane_state);
759 plane_state->visible = false;
763 /* crtc should only be NULL when disabling (i.e., !fb) */
764 if (WARN_ON(!plane_state->crtc)) {
765 plane_state->visible = false;
769 if (!crtc_state->enable && !can_update_disabled) {
770 DRM_DEBUG_KMS("Cannot update plane of a disabled CRTC.\n");
774 drm_rect_rotate(src, fb->width << 16, fb->height << 16, rotation);
777 hscale = drm_rect_calc_hscale(src, dst, min_scale, max_scale);
778 vscale = drm_rect_calc_vscale(src, dst, min_scale, max_scale);
779 if (hscale < 0 || vscale < 0) {
780 DRM_DEBUG_KMS("Invalid scaling of plane\n");
781 drm_rect_debug_print("src: ", &plane_state->src, true);
782 drm_rect_debug_print("dst: ", &plane_state->dst, false);
786 if (crtc_state->enable)
787 drm_mode_get_hv_timing(&crtc_state->mode, &clip.x2, &clip.y2);
789 plane_state->visible = drm_rect_clip_scaled(src, dst, &clip);
791 drm_rect_rotate_inv(src, fb->width << 16, fb->height << 16, rotation);
793 if (!plane_state->visible)
795 * Plane isn't visible; some drivers can handle this
796 * so we just return success here. Drivers that can't
797 * (including those that use the primary plane helper's
798 * update function) will return an error from their
799 * update_plane handler.
803 if (!can_position && !drm_rect_equals(dst, &clip)) {
804 DRM_DEBUG_KMS("Plane must cover entire CRTC\n");
805 drm_rect_debug_print("dst: ", dst, false);
806 drm_rect_debug_print("clip: ", &clip, false);
812 EXPORT_SYMBOL(drm_atomic_helper_check_plane_state);
815 * drm_atomic_helper_check_planes - validate state object for planes changes
817 * @state: the driver state object
819 * Check the state object to see if the requested state is physically possible.
820 * This does all the plane update related checks using by calling into the
821 * &drm_crtc_helper_funcs.atomic_check and &drm_plane_helper_funcs.atomic_check
822 * hooks provided by the driver.
824 * It also sets &drm_crtc_state.planes_changed to indicate that a crtc has
828 * Zero for success or -errno
831 drm_atomic_helper_check_planes(struct drm_device *dev,
832 struct drm_atomic_state *state)
834 struct drm_crtc *crtc;
835 struct drm_crtc_state *new_crtc_state;
836 struct drm_plane *plane;
837 struct drm_plane_state *new_plane_state, *old_plane_state;
840 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
841 const struct drm_plane_helper_funcs *funcs;
843 WARN_ON(!drm_modeset_is_locked(&plane->mutex));
845 funcs = plane->helper_private;
847 drm_atomic_helper_plane_changed(state, old_plane_state, new_plane_state, plane);
849 if (!funcs || !funcs->atomic_check)
852 ret = funcs->atomic_check(plane, new_plane_state);
854 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic driver check failed\n",
855 plane->base.id, plane->name);
860 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
861 const struct drm_crtc_helper_funcs *funcs;
863 funcs = crtc->helper_private;
865 if (!funcs || !funcs->atomic_check)
868 ret = funcs->atomic_check(crtc, new_crtc_state);
870 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic driver check failed\n",
871 crtc->base.id, crtc->name);
878 EXPORT_SYMBOL(drm_atomic_helper_check_planes);
881 * drm_atomic_helper_check - validate state object
883 * @state: the driver state object
885 * Check the state object to see if the requested state is physically possible.
886 * Only crtcs and planes have check callbacks, so for any additional (global)
887 * checking that a driver needs it can simply wrap that around this function.
888 * Drivers without such needs can directly use this as their
889 * &drm_mode_config_funcs.atomic_check callback.
891 * This just wraps the two parts of the state checking for planes and modeset
892 * state in the default order: First it calls drm_atomic_helper_check_modeset()
893 * and then drm_atomic_helper_check_planes(). The assumption is that the
894 * @drm_plane_helper_funcs.atomic_check and @drm_crtc_helper_funcs.atomic_check
895 * functions depend upon an updated adjusted_mode.clock to e.g. properly compute
898 * Note that zpos normalization will add all enable planes to the state which
899 * might not desired for some drivers.
900 * For example enable/disable of a cursor plane which have fixed zpos value
901 * would trigger all other enabled planes to be forced to the state change.
904 * Zero for success or -errno
906 int drm_atomic_helper_check(struct drm_device *dev,
907 struct drm_atomic_state *state)
911 ret = drm_atomic_helper_check_modeset(dev, state);
915 if (dev->mode_config.normalize_zpos) {
916 ret = drm_atomic_normalize_zpos(dev, state);
921 ret = drm_atomic_helper_check_planes(dev, state);
925 if (state->legacy_cursor_update)
926 state->async_update = !drm_atomic_helper_async_check(dev, state);
930 EXPORT_SYMBOL(drm_atomic_helper_check);
933 disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state)
935 struct drm_connector *connector;
936 struct drm_connector_state *old_conn_state, *new_conn_state;
937 struct drm_crtc *crtc;
938 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
941 for_each_oldnew_connector_in_state(old_state, connector, old_conn_state, new_conn_state, i) {
942 const struct drm_encoder_helper_funcs *funcs;
943 struct drm_encoder *encoder;
945 /* Shut down everything that's in the changeset and currently
946 * still on. So need to check the old, saved state. */
947 if (!old_conn_state->crtc)
950 old_crtc_state = drm_atomic_get_old_crtc_state(old_state, old_conn_state->crtc);
952 if (!old_crtc_state->active ||
953 !drm_atomic_crtc_needs_modeset(old_conn_state->crtc->state))
956 encoder = old_conn_state->best_encoder;
958 /* We shouldn't get this far if we didn't previously have
959 * an encoder.. but WARN_ON() rather than explode.
961 if (WARN_ON(!encoder))
964 funcs = encoder->helper_private;
966 DRM_DEBUG_ATOMIC("disabling [ENCODER:%d:%s]\n",
967 encoder->base.id, encoder->name);
970 * Each encoder has at most one connector (since we always steal
971 * it away), so we won't call disable hooks twice.
973 drm_bridge_disable(encoder->bridge);
975 /* Right function depends upon target state. */
977 if (new_conn_state->crtc && funcs->prepare)
978 funcs->prepare(encoder);
979 else if (funcs->disable)
980 funcs->disable(encoder);
981 else if (funcs->dpms)
982 funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
985 drm_bridge_post_disable(encoder->bridge);
988 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
989 const struct drm_crtc_helper_funcs *funcs;
992 /* Shut down everything that needs a full modeset. */
993 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
996 if (!old_crtc_state->active)
999 funcs = crtc->helper_private;
1001 DRM_DEBUG_ATOMIC("disabling [CRTC:%d:%s]\n",
1002 crtc->base.id, crtc->name);
1005 /* Right function depends upon target state. */
1006 if (new_crtc_state->enable && funcs->prepare)
1007 funcs->prepare(crtc);
1008 else if (funcs->atomic_disable)
1009 funcs->atomic_disable(crtc, old_crtc_state);
1010 else if (funcs->disable)
1011 funcs->disable(crtc);
1013 funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
1015 if (!(dev->irq_enabled && dev->num_crtcs))
1018 ret = drm_crtc_vblank_get(crtc);
1019 WARN_ONCE(ret != -EINVAL, "driver forgot to call drm_crtc_vblank_off()\n");
1021 drm_crtc_vblank_put(crtc);
1026 * drm_atomic_helper_update_legacy_modeset_state - update legacy modeset state
1028 * @old_state: atomic state object with old state structures
1030 * This function updates all the various legacy modeset state pointers in
1031 * connectors, encoders and crtcs. It also updates the timestamping constants
1032 * used for precise vblank timestamps by calling
1033 * drm_calc_timestamping_constants().
1035 * Drivers can use this for building their own atomic commit if they don't have
1036 * a pure helper-based modeset implementation.
1038 * Since these updates are not synchronized with lockings, only code paths
1039 * called from &drm_mode_config_helper_funcs.atomic_commit_tail can look at the
1040 * legacy state filled out by this helper. Defacto this means this helper and
1041 * the legacy state pointers are only really useful for transitioning an
1042 * existing driver to the atomic world.
1045 drm_atomic_helper_update_legacy_modeset_state(struct drm_device *dev,
1046 struct drm_atomic_state *old_state)
1048 struct drm_connector *connector;
1049 struct drm_connector_state *old_conn_state, *new_conn_state;
1050 struct drm_crtc *crtc;
1051 struct drm_crtc_state *new_crtc_state;
1054 /* clear out existing links and update dpms */
1055 for_each_oldnew_connector_in_state(old_state, connector, old_conn_state, new_conn_state, i) {
1056 if (connector->encoder) {
1057 WARN_ON(!connector->encoder->crtc);
1059 connector->encoder->crtc = NULL;
1060 connector->encoder = NULL;
1063 crtc = new_conn_state->crtc;
1064 if ((!crtc && old_conn_state->crtc) ||
1065 (crtc && drm_atomic_crtc_needs_modeset(crtc->state))) {
1066 int mode = DRM_MODE_DPMS_OFF;
1068 if (crtc && crtc->state->active)
1069 mode = DRM_MODE_DPMS_ON;
1071 connector->dpms = mode;
1076 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
1077 if (!new_conn_state->crtc)
1080 if (WARN_ON(!new_conn_state->best_encoder))
1083 connector->encoder = new_conn_state->best_encoder;
1084 connector->encoder->crtc = new_conn_state->crtc;
1087 /* set legacy state in the crtc structure */
1088 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
1089 struct drm_plane *primary = crtc->primary;
1090 struct drm_plane_state *new_plane_state;
1092 crtc->mode = new_crtc_state->mode;
1093 crtc->enabled = new_crtc_state->enable;
1096 drm_atomic_get_new_plane_state(old_state, primary);
1098 if (new_plane_state && new_plane_state->crtc == crtc) {
1099 crtc->x = new_plane_state->src_x >> 16;
1100 crtc->y = new_plane_state->src_y >> 16;
1103 if (new_crtc_state->enable)
1104 drm_calc_timestamping_constants(crtc,
1105 &new_crtc_state->adjusted_mode);
1108 EXPORT_SYMBOL(drm_atomic_helper_update_legacy_modeset_state);
1111 crtc_set_mode(struct drm_device *dev, struct drm_atomic_state *old_state)
1113 struct drm_crtc *crtc;
1114 struct drm_crtc_state *new_crtc_state;
1115 struct drm_connector *connector;
1116 struct drm_connector_state *new_conn_state;
1119 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
1120 const struct drm_crtc_helper_funcs *funcs;
1122 if (!new_crtc_state->mode_changed)
1125 funcs = crtc->helper_private;
1127 if (new_crtc_state->enable && funcs->mode_set_nofb) {
1128 DRM_DEBUG_ATOMIC("modeset on [CRTC:%d:%s]\n",
1129 crtc->base.id, crtc->name);
1131 funcs->mode_set_nofb(crtc);
1135 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
1136 const struct drm_encoder_helper_funcs *funcs;
1137 struct drm_encoder *encoder;
1138 struct drm_display_mode *mode, *adjusted_mode;
1140 if (!new_conn_state->best_encoder)
1143 encoder = new_conn_state->best_encoder;
1144 funcs = encoder->helper_private;
1145 new_crtc_state = new_conn_state->crtc->state;
1146 mode = &new_crtc_state->mode;
1147 adjusted_mode = &new_crtc_state->adjusted_mode;
1149 if (!new_crtc_state->mode_changed)
1152 DRM_DEBUG_ATOMIC("modeset on [ENCODER:%d:%s]\n",
1153 encoder->base.id, encoder->name);
1156 * Each encoder has at most one connector (since we always steal
1157 * it away), so we won't call mode_set hooks twice.
1159 if (funcs && funcs->atomic_mode_set) {
1160 funcs->atomic_mode_set(encoder, new_crtc_state,
1162 } else if (funcs && funcs->mode_set) {
1163 funcs->mode_set(encoder, mode, adjusted_mode);
1166 drm_bridge_mode_set(encoder->bridge, mode, adjusted_mode);
1171 * drm_atomic_helper_commit_modeset_disables - modeset commit to disable outputs
1173 * @old_state: atomic state object with old state structures
1175 * This function shuts down all the outputs that need to be shut down and
1176 * prepares them (if required) with the new mode.
1178 * For compatibility with legacy crtc helpers this should be called before
1179 * drm_atomic_helper_commit_planes(), which is what the default commit function
1180 * does. But drivers with different needs can group the modeset commits together
1181 * and do the plane commits at the end. This is useful for drivers doing runtime
1182 * PM since planes updates then only happen when the CRTC is actually enabled.
1184 void drm_atomic_helper_commit_modeset_disables(struct drm_device *dev,
1185 struct drm_atomic_state *old_state)
1187 disable_outputs(dev, old_state);
1189 drm_atomic_helper_update_legacy_modeset_state(dev, old_state);
1191 crtc_set_mode(dev, old_state);
1193 EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_disables);
1195 static void drm_atomic_helper_commit_writebacks(struct drm_device *dev,
1196 struct drm_atomic_state *old_state)
1198 struct drm_connector *connector;
1199 struct drm_connector_state *new_conn_state;
1202 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
1203 const struct drm_connector_helper_funcs *funcs;
1205 funcs = connector->helper_private;
1206 if (!funcs->atomic_commit)
1209 if (new_conn_state->writeback_job && new_conn_state->writeback_job->fb) {
1210 WARN_ON(connector->connector_type != DRM_MODE_CONNECTOR_WRITEBACK);
1211 funcs->atomic_commit(connector, new_conn_state);
1217 * drm_atomic_helper_commit_modeset_enables - modeset commit to enable outputs
1219 * @old_state: atomic state object with old state structures
1221 * This function enables all the outputs with the new configuration which had to
1222 * be turned off for the update.
1224 * For compatibility with legacy crtc helpers this should be called after
1225 * drm_atomic_helper_commit_planes(), which is what the default commit function
1226 * does. But drivers with different needs can group the modeset commits together
1227 * and do the plane commits at the end. This is useful for drivers doing runtime
1228 * PM since planes updates then only happen when the CRTC is actually enabled.
1230 void drm_atomic_helper_commit_modeset_enables(struct drm_device *dev,
1231 struct drm_atomic_state *old_state)
1233 struct drm_crtc *crtc;
1234 struct drm_crtc_state *old_crtc_state;
1235 struct drm_crtc_state *new_crtc_state;
1236 struct drm_connector *connector;
1237 struct drm_connector_state *new_conn_state;
1240 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
1241 const struct drm_crtc_helper_funcs *funcs;
1243 /* Need to filter out CRTCs where only planes change. */
1244 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
1247 if (!new_crtc_state->active)
1250 funcs = crtc->helper_private;
1252 if (new_crtc_state->enable) {
1253 DRM_DEBUG_ATOMIC("enabling [CRTC:%d:%s]\n",
1254 crtc->base.id, crtc->name);
1256 if (funcs->atomic_enable)
1257 funcs->atomic_enable(crtc, old_crtc_state);
1259 funcs->commit(crtc);
1263 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
1264 const struct drm_encoder_helper_funcs *funcs;
1265 struct drm_encoder *encoder;
1267 if (!new_conn_state->best_encoder)
1270 if (!new_conn_state->crtc->state->active ||
1271 !drm_atomic_crtc_needs_modeset(new_conn_state->crtc->state))
1274 encoder = new_conn_state->best_encoder;
1275 funcs = encoder->helper_private;
1277 DRM_DEBUG_ATOMIC("enabling [ENCODER:%d:%s]\n",
1278 encoder->base.id, encoder->name);
1281 * Each encoder has at most one connector (since we always steal
1282 * it away), so we won't call enable hooks twice.
1284 drm_bridge_pre_enable(encoder->bridge);
1288 funcs->enable(encoder);
1289 else if (funcs->commit)
1290 funcs->commit(encoder);
1293 drm_bridge_enable(encoder->bridge);
1296 drm_atomic_helper_commit_writebacks(dev, old_state);
1298 EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_enables);
1301 * drm_atomic_helper_wait_for_fences - wait for fences stashed in plane state
1303 * @state: atomic state object with old state structures
1304 * @pre_swap: If true, do an interruptible wait, and @state is the new state.
1305 * Otherwise @state is the old state.
1307 * For implicit sync, driver should fish the exclusive fence out from the
1308 * incoming fb's and stash it in the drm_plane_state. This is called after
1309 * drm_atomic_helper_swap_state() so it uses the current plane state (and
1310 * just uses the atomic state to find the changed planes)
1312 * Note that @pre_swap is needed since the point where we block for fences moves
1313 * around depending upon whether an atomic commit is blocking or
1314 * non-blocking. For non-blocking commit all waiting needs to happen after
1315 * drm_atomic_helper_swap_state() is called, but for blocking commits we want
1316 * to wait **before** we do anything that can't be easily rolled back. That is
1317 * before we call drm_atomic_helper_swap_state().
1319 * Returns zero if success or < 0 if dma_fence_wait() fails.
1321 int drm_atomic_helper_wait_for_fences(struct drm_device *dev,
1322 struct drm_atomic_state *state,
1325 struct drm_plane *plane;
1326 struct drm_plane_state *new_plane_state;
1329 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
1330 if (!new_plane_state->fence)
1333 WARN_ON(!new_plane_state->fb);
1336 * If waiting for fences pre-swap (ie: nonblock), userspace can
1337 * still interrupt the operation. Instead of blocking until the
1338 * timer expires, make the wait interruptible.
1340 ret = dma_fence_wait(new_plane_state->fence, pre_swap);
1344 dma_fence_put(new_plane_state->fence);
1345 new_plane_state->fence = NULL;
1350 EXPORT_SYMBOL(drm_atomic_helper_wait_for_fences);
1353 * drm_atomic_helper_wait_for_vblanks - wait for vblank on crtcs
1355 * @old_state: atomic state object with old state structures
1357 * Helper to, after atomic commit, wait for vblanks on all effected
1358 * crtcs (ie. before cleaning up old framebuffers using
1359 * drm_atomic_helper_cleanup_planes()). It will only wait on CRTCs where the
1360 * framebuffers have actually changed to optimize for the legacy cursor and
1361 * plane update use-case.
1363 * Drivers using the nonblocking commit tracking support initialized by calling
1364 * drm_atomic_helper_setup_commit() should look at
1365 * drm_atomic_helper_wait_for_flip_done() as an alternative.
1368 drm_atomic_helper_wait_for_vblanks(struct drm_device *dev,
1369 struct drm_atomic_state *old_state)
1371 struct drm_crtc *crtc;
1372 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
1374 unsigned crtc_mask = 0;
1377 * Legacy cursor ioctls are completely unsynced, and userspace
1378 * relies on that (by doing tons of cursor updates).
1380 if (old_state->legacy_cursor_update)
1383 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
1384 if (!new_crtc_state->active)
1387 ret = drm_crtc_vblank_get(crtc);
1391 crtc_mask |= drm_crtc_mask(crtc);
1392 old_state->crtcs[i].last_vblank_count = drm_crtc_vblank_count(crtc);
1395 for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
1396 if (!(crtc_mask & drm_crtc_mask(crtc)))
1399 ret = wait_event_timeout(dev->vblank[i].queue,
1400 old_state->crtcs[i].last_vblank_count !=
1401 drm_crtc_vblank_count(crtc),
1402 msecs_to_jiffies(50));
1404 WARN(!ret, "[CRTC:%d:%s] vblank wait timed out\n",
1405 crtc->base.id, crtc->name);
1407 drm_crtc_vblank_put(crtc);
1410 EXPORT_SYMBOL(drm_atomic_helper_wait_for_vblanks);
1413 * drm_atomic_helper_wait_for_flip_done - wait for all page flips to be done
1415 * @old_state: atomic state object with old state structures
1417 * Helper to, after atomic commit, wait for page flips on all effected
1418 * crtcs (ie. before cleaning up old framebuffers using
1419 * drm_atomic_helper_cleanup_planes()). Compared to
1420 * drm_atomic_helper_wait_for_vblanks() this waits for the completion of on all
1421 * CRTCs, assuming that cursors-only updates are signalling their completion
1422 * immediately (or using a different path).
1424 * This requires that drivers use the nonblocking commit tracking support
1425 * initialized using drm_atomic_helper_setup_commit().
1427 void drm_atomic_helper_wait_for_flip_done(struct drm_device *dev,
1428 struct drm_atomic_state *old_state)
1430 struct drm_crtc *crtc;
1433 for (i = 0; i < dev->mode_config.num_crtc; i++) {
1434 struct drm_crtc_commit *commit = old_state->crtcs[i].commit;
1437 crtc = old_state->crtcs[i].ptr;
1439 if (!crtc || !commit)
1442 ret = wait_for_completion_timeout(&commit->flip_done, 10 * HZ);
1444 DRM_ERROR("[CRTC:%d:%s] flip_done timed out\n",
1445 crtc->base.id, crtc->name);
1448 if (old_state->fake_commit)
1449 complete_all(&old_state->fake_commit->flip_done);
1451 EXPORT_SYMBOL(drm_atomic_helper_wait_for_flip_done);
1454 * drm_atomic_helper_commit_tail - commit atomic update to hardware
1455 * @old_state: atomic state object with old state structures
1457 * This is the default implementation for the
1458 * &drm_mode_config_helper_funcs.atomic_commit_tail hook, for drivers
1459 * that do not support runtime_pm or do not need the CRTC to be
1460 * enabled to perform a commit. Otherwise, see
1461 * drm_atomic_helper_commit_tail_rpm().
1463 * Note that the default ordering of how the various stages are called is to
1464 * match the legacy modeset helper library closest.
1466 void drm_atomic_helper_commit_tail(struct drm_atomic_state *old_state)
1468 struct drm_device *dev = old_state->dev;
1470 drm_atomic_helper_commit_modeset_disables(dev, old_state);
1472 drm_atomic_helper_commit_planes(dev, old_state, 0);
1474 drm_atomic_helper_commit_modeset_enables(dev, old_state);
1476 drm_atomic_helper_fake_vblank(old_state);
1478 drm_atomic_helper_commit_hw_done(old_state);
1480 drm_atomic_helper_wait_for_vblanks(dev, old_state);
1482 drm_atomic_helper_cleanup_planes(dev, old_state);
1484 EXPORT_SYMBOL(drm_atomic_helper_commit_tail);
1487 * drm_atomic_helper_commit_tail_rpm - commit atomic update to hardware
1488 * @old_state: new modeset state to be committed
1490 * This is an alternative implementation for the
1491 * &drm_mode_config_helper_funcs.atomic_commit_tail hook, for drivers
1492 * that support runtime_pm or need the CRTC to be enabled to perform a
1493 * commit. Otherwise, one should use the default implementation
1494 * drm_atomic_helper_commit_tail().
1496 void drm_atomic_helper_commit_tail_rpm(struct drm_atomic_state *old_state)
1498 struct drm_device *dev = old_state->dev;
1500 drm_atomic_helper_commit_modeset_disables(dev, old_state);
1502 drm_atomic_helper_commit_modeset_enables(dev, old_state);
1504 drm_atomic_helper_commit_planes(dev, old_state,
1505 DRM_PLANE_COMMIT_ACTIVE_ONLY);
1507 drm_atomic_helper_fake_vblank(old_state);
1509 drm_atomic_helper_commit_hw_done(old_state);
1511 drm_atomic_helper_wait_for_vblanks(dev, old_state);
1513 drm_atomic_helper_cleanup_planes(dev, old_state);
1515 EXPORT_SYMBOL(drm_atomic_helper_commit_tail_rpm);
1517 static void commit_tail(struct drm_atomic_state *old_state)
1519 struct drm_device *dev = old_state->dev;
1520 const struct drm_mode_config_helper_funcs *funcs;
1522 funcs = dev->mode_config.helper_private;
1524 drm_atomic_helper_wait_for_fences(dev, old_state, false);
1526 drm_atomic_helper_wait_for_dependencies(old_state);
1528 if (funcs && funcs->atomic_commit_tail)
1529 funcs->atomic_commit_tail(old_state);
1531 drm_atomic_helper_commit_tail(old_state);
1533 drm_atomic_helper_commit_cleanup_done(old_state);
1535 drm_atomic_state_put(old_state);
1538 static void commit_work(struct work_struct *work)
1540 struct drm_atomic_state *state = container_of(work,
1541 struct drm_atomic_state,
1547 * drm_atomic_helper_async_check - check if state can be commited asynchronously
1549 * @state: the driver state object
1551 * This helper will check if it is possible to commit the state asynchronously.
1552 * Async commits are not supposed to swap the states like normal sync commits
1553 * but just do in-place changes on the current state.
1555 * It will return 0 if the commit can happen in an asynchronous fashion or error
1556 * if not. Note that error just mean it can't be commited asynchronously, if it
1557 * fails the commit should be treated like a normal synchronous commit.
1559 int drm_atomic_helper_async_check(struct drm_device *dev,
1560 struct drm_atomic_state *state)
1562 struct drm_crtc *crtc;
1563 struct drm_crtc_state *crtc_state;
1564 struct drm_plane *plane = NULL;
1565 struct drm_plane_state *old_plane_state = NULL;
1566 struct drm_plane_state *new_plane_state = NULL;
1567 const struct drm_plane_helper_funcs *funcs;
1568 int i, n_planes = 0;
1570 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
1571 if (drm_atomic_crtc_needs_modeset(crtc_state))
1575 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i)
1578 /* FIXME: we support only single plane updates for now */
1582 if (!new_plane_state->crtc ||
1583 old_plane_state->crtc != new_plane_state->crtc)
1587 * FIXME: Since prepare_fb and cleanup_fb are always called on
1588 * the new_plane_state for async updates we need to block framebuffer
1589 * changes. This prevents use of a fb that's been cleaned up and
1590 * double cleanups from occuring.
1592 if (old_plane_state->fb != new_plane_state->fb)
1595 funcs = plane->helper_private;
1596 if (!funcs->atomic_async_update)
1599 if (new_plane_state->fence)
1603 * Don't do an async update if there is an outstanding commit modifying
1604 * the plane. This prevents our async update's changes from getting
1605 * overridden by a previous synchronous update's state.
1607 if (old_plane_state->commit &&
1608 !try_wait_for_completion(&old_plane_state->commit->hw_done))
1611 return funcs->atomic_async_check(plane, new_plane_state);
1613 EXPORT_SYMBOL(drm_atomic_helper_async_check);
1616 * drm_atomic_helper_async_commit - commit state asynchronously
1618 * @state: the driver state object
1620 * This function commits a state asynchronously, i.e., not vblank
1621 * synchronized. It should be used on a state only when
1622 * drm_atomic_async_check() succeeds. Async commits are not supposed to swap
1623 * the states like normal sync commits, but just do in-place changes on the
1626 * TODO: Implement full swap instead of doing in-place changes.
1628 void drm_atomic_helper_async_commit(struct drm_device *dev,
1629 struct drm_atomic_state *state)
1631 struct drm_plane *plane;
1632 struct drm_plane_state *plane_state;
1633 const struct drm_plane_helper_funcs *funcs;
1636 for_each_new_plane_in_state(state, plane, plane_state, i) {
1637 struct drm_framebuffer *new_fb = plane_state->fb;
1638 struct drm_framebuffer *old_fb = plane->state->fb;
1640 funcs = plane->helper_private;
1641 funcs->atomic_async_update(plane, plane_state);
1644 * ->atomic_async_update() is supposed to update the
1645 * plane->state in-place, make sure at least common
1646 * properties have been properly updated.
1648 WARN_ON_ONCE(plane->state->fb != new_fb);
1649 WARN_ON_ONCE(plane->state->crtc_x != plane_state->crtc_x);
1650 WARN_ON_ONCE(plane->state->crtc_y != plane_state->crtc_y);
1651 WARN_ON_ONCE(plane->state->src_x != plane_state->src_x);
1652 WARN_ON_ONCE(plane->state->src_y != plane_state->src_y);
1655 * Make sure the FBs have been swapped so that cleanups in the
1656 * new_state performs a cleanup in the old FB.
1658 WARN_ON_ONCE(plane_state->fb != old_fb);
1661 EXPORT_SYMBOL(drm_atomic_helper_async_commit);
1664 * drm_atomic_helper_commit - commit validated state object
1666 * @state: the driver state object
1667 * @nonblock: whether nonblocking behavior is requested.
1669 * This function commits a with drm_atomic_helper_check() pre-validated state
1670 * object. This can still fail when e.g. the framebuffer reservation fails. This
1671 * function implements nonblocking commits, using
1672 * drm_atomic_helper_setup_commit() and related functions.
1674 * Committing the actual hardware state is done through the
1675 * &drm_mode_config_helper_funcs.atomic_commit_tail callback, or it's default
1676 * implementation drm_atomic_helper_commit_tail().
1679 * Zero for success or -errno.
1681 int drm_atomic_helper_commit(struct drm_device *dev,
1682 struct drm_atomic_state *state,
1687 if (state->async_update) {
1688 ret = drm_atomic_helper_prepare_planes(dev, state);
1692 drm_atomic_helper_async_commit(dev, state);
1693 drm_atomic_helper_cleanup_planes(dev, state);
1698 ret = drm_atomic_helper_setup_commit(state, nonblock);
1702 INIT_WORK(&state->commit_work, commit_work);
1704 ret = drm_atomic_helper_prepare_planes(dev, state);
1709 ret = drm_atomic_helper_wait_for_fences(dev, state, true);
1715 * This is the point of no return - everything below never fails except
1716 * when the hw goes bonghits. Which means we can commit the new state on
1717 * the software side now.
1720 ret = drm_atomic_helper_swap_state(state, true);
1725 * Everything below can be run asynchronously without the need to grab
1726 * any modeset locks at all under one condition: It must be guaranteed
1727 * that the asynchronous work has either been cancelled (if the driver
1728 * supports it, which at least requires that the framebuffers get
1729 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
1730 * before the new state gets committed on the software side with
1731 * drm_atomic_helper_swap_state().
1733 * This scheme allows new atomic state updates to be prepared and
1734 * checked in parallel to the asynchronous completion of the previous
1735 * update. Which is important since compositors need to figure out the
1736 * composition of the next frame right after having submitted the
1739 * NOTE: Commit work has multiple phases, first hardware commit, then
1740 * cleanup. We want them to overlap, hence need system_unbound_wq to
1741 * make sure work items don't artifically stall on each another.
1744 drm_atomic_state_get(state);
1746 queue_work(system_unbound_wq, &state->commit_work);
1753 drm_atomic_helper_cleanup_planes(dev, state);
1756 EXPORT_SYMBOL(drm_atomic_helper_commit);
1759 * DOC: implementing nonblocking commit
1761 * Nonblocking atomic commits have to be implemented in the following sequence:
1763 * 1. Run drm_atomic_helper_prepare_planes() first. This is the only function
1764 * which commit needs to call which can fail, so we want to run it first and
1767 * 2. Synchronize with any outstanding nonblocking commit worker threads which
1768 * might be affected the new state update. This can be done by either cancelling
1769 * or flushing the work items, depending upon whether the driver can deal with
1770 * cancelled updates. Note that it is important to ensure that the framebuffer
1771 * cleanup is still done when cancelling.
1773 * Asynchronous workers need to have sufficient parallelism to be able to run
1774 * different atomic commits on different CRTCs in parallel. The simplest way to
1775 * achive this is by running them on the &system_unbound_wq work queue. Note
1776 * that drivers are not required to split up atomic commits and run an
1777 * individual commit in parallel - userspace is supposed to do that if it cares.
1778 * But it might be beneficial to do that for modesets, since those necessarily
1779 * must be done as one global operation, and enabling or disabling a CRTC can
1780 * take a long time. But even that is not required.
1782 * 3. The software state is updated synchronously with
1783 * drm_atomic_helper_swap_state(). Doing this under the protection of all modeset
1784 * locks means concurrent callers never see inconsistent state. And doing this
1785 * while it's guaranteed that no relevant nonblocking worker runs means that
1786 * nonblocking workers do not need grab any locks. Actually they must not grab
1787 * locks, for otherwise the work flushing will deadlock.
1789 * 4. Schedule a work item to do all subsequent steps, using the split-out
1790 * commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and
1791 * then cleaning up the framebuffers after the old framebuffer is no longer
1794 * The above scheme is implemented in the atomic helper libraries in
1795 * drm_atomic_helper_commit() using a bunch of helper functions. See
1796 * drm_atomic_helper_setup_commit() for a starting point.
1799 static int stall_checks(struct drm_crtc *crtc, bool nonblock)
1801 struct drm_crtc_commit *commit, *stall_commit = NULL;
1802 bool completed = true;
1806 spin_lock(&crtc->commit_lock);
1808 list_for_each_entry(commit, &crtc->commit_list, commit_entry) {
1810 completed = try_wait_for_completion(&commit->flip_done);
1811 /* Userspace is not allowed to get ahead of the previous
1812 * commit with nonblocking ones. */
1813 if (!completed && nonblock) {
1814 spin_unlock(&crtc->commit_lock);
1817 } else if (i == 1) {
1818 stall_commit = drm_crtc_commit_get(commit);
1824 spin_unlock(&crtc->commit_lock);
1829 /* We don't want to let commits get ahead of cleanup work too much,
1830 * stalling on 2nd previous commit means triple-buffer won't ever stall.
1832 ret = wait_for_completion_interruptible_timeout(&stall_commit->cleanup_done,
1835 DRM_ERROR("[CRTC:%d:%s] cleanup_done timed out\n",
1836 crtc->base.id, crtc->name);
1838 drm_crtc_commit_put(stall_commit);
1840 return ret < 0 ? ret : 0;
1843 static void release_crtc_commit(struct completion *completion)
1845 struct drm_crtc_commit *commit = container_of(completion,
1849 drm_crtc_commit_put(commit);
1852 static void init_commit(struct drm_crtc_commit *commit, struct drm_crtc *crtc)
1854 init_completion(&commit->flip_done);
1855 init_completion(&commit->hw_done);
1856 init_completion(&commit->cleanup_done);
1857 INIT_LIST_HEAD(&commit->commit_entry);
1858 kref_init(&commit->ref);
1859 commit->crtc = crtc;
1862 static struct drm_crtc_commit *
1863 crtc_or_fake_commit(struct drm_atomic_state *state, struct drm_crtc *crtc)
1866 struct drm_crtc_state *new_crtc_state;
1868 new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
1870 return new_crtc_state->commit;
1873 if (!state->fake_commit) {
1874 state->fake_commit = kzalloc(sizeof(*state->fake_commit), GFP_KERNEL);
1875 if (!state->fake_commit)
1878 init_commit(state->fake_commit, NULL);
1881 return state->fake_commit;
1885 * drm_atomic_helper_setup_commit - setup possibly nonblocking commit
1886 * @state: new modeset state to be committed
1887 * @nonblock: whether nonblocking behavior is requested.
1889 * This function prepares @state to be used by the atomic helper's support for
1890 * nonblocking commits. Drivers using the nonblocking commit infrastructure
1891 * should always call this function from their
1892 * &drm_mode_config_funcs.atomic_commit hook.
1894 * To be able to use this support drivers need to use a few more helper
1895 * functions. drm_atomic_helper_wait_for_dependencies() must be called before
1896 * actually committing the hardware state, and for nonblocking commits this call
1897 * must be placed in the async worker. See also drm_atomic_helper_swap_state()
1898 * and it's stall parameter, for when a driver's commit hooks look at the
1899 * &drm_crtc.state, &drm_plane.state or &drm_connector.state pointer directly.
1901 * Completion of the hardware commit step must be signalled using
1902 * drm_atomic_helper_commit_hw_done(). After this step the driver is not allowed
1903 * to read or change any permanent software or hardware modeset state. The only
1904 * exception is state protected by other means than &drm_modeset_lock locks.
1905 * Only the free standing @state with pointers to the old state structures can
1906 * be inspected, e.g. to clean up old buffers using
1907 * drm_atomic_helper_cleanup_planes().
1909 * At the very end, before cleaning up @state drivers must call
1910 * drm_atomic_helper_commit_cleanup_done().
1912 * This is all implemented by in drm_atomic_helper_commit(), giving drivers a
1913 * complete and easy-to-use default implementation of the atomic_commit() hook.
1915 * The tracking of asynchronously executed and still pending commits is done
1916 * using the core structure &drm_crtc_commit.
1918 * By default there's no need to clean up resources allocated by this function
1919 * explicitly: drm_atomic_state_default_clear() will take care of that
1924 * 0 on success. -EBUSY when userspace schedules nonblocking commits too fast,
1925 * -ENOMEM on allocation failures and -EINTR when a signal is pending.
1927 int drm_atomic_helper_setup_commit(struct drm_atomic_state *state,
1930 struct drm_crtc *crtc;
1931 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
1932 struct drm_connector *conn;
1933 struct drm_connector_state *old_conn_state, *new_conn_state;
1934 struct drm_plane *plane;
1935 struct drm_plane_state *old_plane_state, *new_plane_state;
1936 struct drm_crtc_commit *commit;
1939 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
1940 commit = kzalloc(sizeof(*commit), GFP_KERNEL);
1944 init_commit(commit, crtc);
1946 new_crtc_state->commit = commit;
1948 ret = stall_checks(crtc, nonblock);
1952 /* Drivers only send out events when at least either current or
1953 * new CRTC state is active. Complete right away if everything
1955 if (!old_crtc_state->active && !new_crtc_state->active) {
1956 complete_all(&commit->flip_done);
1960 /* Legacy cursor updates are fully unsynced. */
1961 if (state->legacy_cursor_update) {
1962 complete_all(&commit->flip_done);
1966 if (!new_crtc_state->event) {
1967 commit->event = kzalloc(sizeof(*commit->event),
1972 new_crtc_state->event = commit->event;
1975 new_crtc_state->event->base.completion = &commit->flip_done;
1976 new_crtc_state->event->base.completion_release = release_crtc_commit;
1977 drm_crtc_commit_get(commit);
1979 commit->abort_completion = true;
1981 state->crtcs[i].commit = commit;
1982 drm_crtc_commit_get(commit);
1985 for_each_oldnew_connector_in_state(state, conn, old_conn_state, new_conn_state, i) {
1986 /* Userspace is not allowed to get ahead of the previous
1987 * commit with nonblocking ones. */
1988 if (nonblock && old_conn_state->commit &&
1989 !try_wait_for_completion(&old_conn_state->commit->flip_done))
1992 /* Always track connectors explicitly for e.g. link retraining. */
1993 commit = crtc_or_fake_commit(state, new_conn_state->crtc ?: old_conn_state->crtc);
1997 new_conn_state->commit = drm_crtc_commit_get(commit);
2000 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
2001 /* Userspace is not allowed to get ahead of the previous
2002 * commit with nonblocking ones. */
2003 if (nonblock && old_plane_state->commit &&
2004 !try_wait_for_completion(&old_plane_state->commit->flip_done))
2007 /* Always track planes explicitly for async pageflip support. */
2008 commit = crtc_or_fake_commit(state, new_plane_state->crtc ?: old_plane_state->crtc);
2012 new_plane_state->commit = drm_crtc_commit_get(commit);
2017 EXPORT_SYMBOL(drm_atomic_helper_setup_commit);
2020 * drm_atomic_helper_wait_for_dependencies - wait for required preceeding commits
2021 * @old_state: atomic state object with old state structures
2023 * This function waits for all preceeding commits that touch the same CRTC as
2024 * @old_state to both be committed to the hardware (as signalled by
2025 * drm_atomic_helper_commit_hw_done) and executed by the hardware (as signalled
2026 * by calling drm_crtc_send_vblank_event() on the &drm_crtc_state.event).
2028 * This is part of the atomic helper support for nonblocking commits, see
2029 * drm_atomic_helper_setup_commit() for an overview.
2031 void drm_atomic_helper_wait_for_dependencies(struct drm_atomic_state *old_state)
2033 struct drm_crtc *crtc;
2034 struct drm_crtc_state *old_crtc_state;
2035 struct drm_plane *plane;
2036 struct drm_plane_state *old_plane_state;
2037 struct drm_connector *conn;
2038 struct drm_connector_state *old_conn_state;
2039 struct drm_crtc_commit *commit;
2043 for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
2044 commit = old_crtc_state->commit;
2049 ret = wait_for_completion_timeout(&commit->hw_done,
2052 DRM_ERROR("[CRTC:%d:%s] hw_done timed out\n",
2053 crtc->base.id, crtc->name);
2055 /* Currently no support for overwriting flips, hence
2056 * stall for previous one to execute completely. */
2057 ret = wait_for_completion_timeout(&commit->flip_done,
2060 DRM_ERROR("[CRTC:%d:%s] flip_done timed out\n",
2061 crtc->base.id, crtc->name);
2064 for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
2065 commit = old_conn_state->commit;
2070 ret = wait_for_completion_timeout(&commit->hw_done,
2073 DRM_ERROR("[CONNECTOR:%d:%s] hw_done timed out\n",
2074 conn->base.id, conn->name);
2076 /* Currently no support for overwriting flips, hence
2077 * stall for previous one to execute completely. */
2078 ret = wait_for_completion_timeout(&commit->flip_done,
2081 DRM_ERROR("[CONNECTOR:%d:%s] flip_done timed out\n",
2082 conn->base.id, conn->name);
2085 for_each_old_plane_in_state(old_state, plane, old_plane_state, i) {
2086 commit = old_plane_state->commit;
2091 ret = wait_for_completion_timeout(&commit->hw_done,
2094 DRM_ERROR("[PLANE:%d:%s] hw_done timed out\n",
2095 plane->base.id, plane->name);
2097 /* Currently no support for overwriting flips, hence
2098 * stall for previous one to execute completely. */
2099 ret = wait_for_completion_timeout(&commit->flip_done,
2102 DRM_ERROR("[PLANE:%d:%s] flip_done timed out\n",
2103 plane->base.id, plane->name);
2106 EXPORT_SYMBOL(drm_atomic_helper_wait_for_dependencies);
2109 * drm_atomic_helper_fake_vblank - fake VBLANK events if needed
2110 * @old_state: atomic state object with old state structures
2112 * This function walks all CRTCs and fake VBLANK events on those with
2113 * &drm_crtc_state.no_vblank set to true and &drm_crtc_state.event != NULL.
2114 * The primary use of this function is writeback connectors working in oneshot
2115 * mode and faking VBLANK events. In this case they only fake the VBLANK event
2116 * when a job is queued, and any change to the pipeline that does not touch the
2117 * connector is leading to timeouts when calling
2118 * drm_atomic_helper_wait_for_vblanks() or
2119 * drm_atomic_helper_wait_for_flip_done().
2121 * This is part of the atomic helper support for nonblocking commits, see
2122 * drm_atomic_helper_setup_commit() for an overview.
2124 void drm_atomic_helper_fake_vblank(struct drm_atomic_state *old_state)
2126 struct drm_crtc_state *new_crtc_state;
2127 struct drm_crtc *crtc;
2130 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
2131 unsigned long flags;
2133 if (!new_crtc_state->no_vblank)
2136 spin_lock_irqsave(&old_state->dev->event_lock, flags);
2137 if (new_crtc_state->event) {
2138 drm_crtc_send_vblank_event(crtc,
2139 new_crtc_state->event);
2140 new_crtc_state->event = NULL;
2142 spin_unlock_irqrestore(&old_state->dev->event_lock, flags);
2145 EXPORT_SYMBOL(drm_atomic_helper_fake_vblank);
2148 * drm_atomic_helper_commit_hw_done - setup possible nonblocking commit
2149 * @old_state: atomic state object with old state structures
2151 * This function is used to signal completion of the hardware commit step. After
2152 * this step the driver is not allowed to read or change any permanent software
2153 * or hardware modeset state. The only exception is state protected by other
2154 * means than &drm_modeset_lock locks.
2156 * Drivers should try to postpone any expensive or delayed cleanup work after
2157 * this function is called.
2159 * This is part of the atomic helper support for nonblocking commits, see
2160 * drm_atomic_helper_setup_commit() for an overview.
2162 void drm_atomic_helper_commit_hw_done(struct drm_atomic_state *old_state)
2164 struct drm_crtc *crtc;
2165 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
2166 struct drm_crtc_commit *commit;
2169 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
2170 commit = new_crtc_state->commit;
2175 * copy new_crtc_state->commit to old_crtc_state->commit,
2176 * it's unsafe to touch new_crtc_state after hw_done,
2177 * but we still need to do so in cleanup_done().
2179 if (old_crtc_state->commit)
2180 drm_crtc_commit_put(old_crtc_state->commit);
2182 old_crtc_state->commit = drm_crtc_commit_get(commit);
2184 /* backend must have consumed any event by now */
2185 WARN_ON(new_crtc_state->event);
2186 complete_all(&commit->hw_done);
2189 if (old_state->fake_commit) {
2190 complete_all(&old_state->fake_commit->hw_done);
2191 complete_all(&old_state->fake_commit->flip_done);
2194 EXPORT_SYMBOL(drm_atomic_helper_commit_hw_done);
2197 * drm_atomic_helper_commit_cleanup_done - signal completion of commit
2198 * @old_state: atomic state object with old state structures
2200 * This signals completion of the atomic update @old_state, including any
2201 * cleanup work. If used, it must be called right before calling
2202 * drm_atomic_state_put().
2204 * This is part of the atomic helper support for nonblocking commits, see
2205 * drm_atomic_helper_setup_commit() for an overview.
2207 void drm_atomic_helper_commit_cleanup_done(struct drm_atomic_state *old_state)
2209 struct drm_crtc *crtc;
2210 struct drm_crtc_state *old_crtc_state;
2211 struct drm_crtc_commit *commit;
2214 for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
2215 commit = old_crtc_state->commit;
2216 if (WARN_ON(!commit))
2219 complete_all(&commit->cleanup_done);
2220 WARN_ON(!try_wait_for_completion(&commit->hw_done));
2222 spin_lock(&crtc->commit_lock);
2223 list_del(&commit->commit_entry);
2224 spin_unlock(&crtc->commit_lock);
2227 if (old_state->fake_commit)
2228 complete_all(&old_state->fake_commit->cleanup_done);
2230 EXPORT_SYMBOL(drm_atomic_helper_commit_cleanup_done);
2233 * drm_atomic_helper_prepare_planes - prepare plane resources before commit
2235 * @state: atomic state object with new state structures
2237 * This function prepares plane state, specifically framebuffers, for the new
2238 * configuration, by calling &drm_plane_helper_funcs.prepare_fb. If any failure
2239 * is encountered this function will call &drm_plane_helper_funcs.cleanup_fb on
2240 * any already successfully prepared framebuffer.
2243 * 0 on success, negative error code on failure.
2245 int drm_atomic_helper_prepare_planes(struct drm_device *dev,
2246 struct drm_atomic_state *state)
2248 struct drm_plane *plane;
2249 struct drm_plane_state *new_plane_state;
2252 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
2253 const struct drm_plane_helper_funcs *funcs;
2255 funcs = plane->helper_private;
2257 if (funcs->prepare_fb) {
2258 ret = funcs->prepare_fb(plane, new_plane_state);
2267 for_each_new_plane_in_state(state, plane, new_plane_state, j) {
2268 const struct drm_plane_helper_funcs *funcs;
2273 funcs = plane->helper_private;
2275 if (funcs->cleanup_fb)
2276 funcs->cleanup_fb(plane, new_plane_state);
2281 EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
2283 static bool plane_crtc_active(const struct drm_plane_state *state)
2285 return state->crtc && state->crtc->state->active;
2289 * drm_atomic_helper_commit_planes - commit plane state
2291 * @old_state: atomic state object with old state structures
2292 * @flags: flags for committing plane state
2294 * This function commits the new plane state using the plane and atomic helper
2295 * functions for planes and crtcs. It assumes that the atomic state has already
2296 * been pushed into the relevant object state pointers, since this step can no
2299 * It still requires the global state object @old_state to know which planes and
2300 * crtcs need to be updated though.
2302 * Note that this function does all plane updates across all CRTCs in one step.
2303 * If the hardware can't support this approach look at
2304 * drm_atomic_helper_commit_planes_on_crtc() instead.
2306 * Plane parameters can be updated by applications while the associated CRTC is
2307 * disabled. The DRM/KMS core will store the parameters in the plane state,
2308 * which will be available to the driver when the CRTC is turned on. As a result
2309 * most drivers don't need to be immediately notified of plane updates for a
2312 * Unless otherwise needed, drivers are advised to set the ACTIVE_ONLY flag in
2313 * @flags in order not to receive plane update notifications related to a
2314 * disabled CRTC. This avoids the need to manually ignore plane updates in
2315 * driver code when the driver and/or hardware can't or just don't need to deal
2316 * with updates on disabled CRTCs, for example when supporting runtime PM.
2318 * Drivers may set the NO_DISABLE_AFTER_MODESET flag in @flags if the relevant
2319 * display controllers require to disable a CRTC's planes when the CRTC is
2320 * disabled. This function would skip the &drm_plane_helper_funcs.atomic_disable
2321 * call for a plane if the CRTC of the old plane state needs a modesetting
2322 * operation. Of course, the drivers need to disable the planes in their CRTC
2323 * disable callbacks since no one else would do that.
2325 * The drm_atomic_helper_commit() default implementation doesn't set the
2326 * ACTIVE_ONLY flag to most closely match the behaviour of the legacy helpers.
2327 * This should not be copied blindly by drivers.
2329 void drm_atomic_helper_commit_planes(struct drm_device *dev,
2330 struct drm_atomic_state *old_state,
2333 struct drm_crtc *crtc;
2334 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
2335 struct drm_plane *plane;
2336 struct drm_plane_state *old_plane_state, *new_plane_state;
2338 bool active_only = flags & DRM_PLANE_COMMIT_ACTIVE_ONLY;
2339 bool no_disable = flags & DRM_PLANE_COMMIT_NO_DISABLE_AFTER_MODESET;
2341 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
2342 const struct drm_crtc_helper_funcs *funcs;
2344 funcs = crtc->helper_private;
2346 if (!funcs || !funcs->atomic_begin)
2349 if (active_only && !new_crtc_state->active)
2352 funcs->atomic_begin(crtc, old_crtc_state);
2355 for_each_oldnew_plane_in_state(old_state, plane, old_plane_state, new_plane_state, i) {
2356 const struct drm_plane_helper_funcs *funcs;
2359 funcs = plane->helper_private;
2364 disabling = drm_atomic_plane_disabling(old_plane_state,
2369 * Skip planes related to inactive CRTCs. If the plane
2370 * is enabled use the state of the current CRTC. If the
2371 * plane is being disabled use the state of the old
2372 * CRTC to avoid skipping planes being disabled on an
2375 if (!disabling && !plane_crtc_active(new_plane_state))
2377 if (disabling && !plane_crtc_active(old_plane_state))
2382 * Special-case disabling the plane if drivers support it.
2384 if (disabling && funcs->atomic_disable) {
2385 struct drm_crtc_state *crtc_state;
2387 crtc_state = old_plane_state->crtc->state;
2389 if (drm_atomic_crtc_needs_modeset(crtc_state) &&
2393 funcs->atomic_disable(plane, old_plane_state);
2394 } else if (new_plane_state->crtc || disabling) {
2395 funcs->atomic_update(plane, old_plane_state);
2399 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
2400 const struct drm_crtc_helper_funcs *funcs;
2402 funcs = crtc->helper_private;
2404 if (!funcs || !funcs->atomic_flush)
2407 if (active_only && !new_crtc_state->active)
2410 funcs->atomic_flush(crtc, old_crtc_state);
2413 EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
2416 * drm_atomic_helper_commit_planes_on_crtc - commit plane state for a crtc
2417 * @old_crtc_state: atomic state object with the old crtc state
2419 * This function commits the new plane state using the plane and atomic helper
2420 * functions for planes on the specific crtc. It assumes that the atomic state
2421 * has already been pushed into the relevant object state pointers, since this
2422 * step can no longer fail.
2424 * This function is useful when plane updates should be done crtc-by-crtc
2425 * instead of one global step like drm_atomic_helper_commit_planes() does.
2427 * This function can only be savely used when planes are not allowed to move
2428 * between different CRTCs because this function doesn't handle inter-CRTC
2429 * depencies. Callers need to ensure that either no such depencies exist,
2430 * resolve them through ordering of commit calls or through some other means.
2433 drm_atomic_helper_commit_planes_on_crtc(struct drm_crtc_state *old_crtc_state)
2435 const struct drm_crtc_helper_funcs *crtc_funcs;
2436 struct drm_crtc *crtc = old_crtc_state->crtc;
2437 struct drm_atomic_state *old_state = old_crtc_state->state;
2438 struct drm_crtc_state *new_crtc_state =
2439 drm_atomic_get_new_crtc_state(old_state, crtc);
2440 struct drm_plane *plane;
2441 unsigned plane_mask;
2443 plane_mask = old_crtc_state->plane_mask;
2444 plane_mask |= new_crtc_state->plane_mask;
2446 crtc_funcs = crtc->helper_private;
2447 if (crtc_funcs && crtc_funcs->atomic_begin)
2448 crtc_funcs->atomic_begin(crtc, old_crtc_state);
2450 drm_for_each_plane_mask(plane, crtc->dev, plane_mask) {
2451 struct drm_plane_state *old_plane_state =
2452 drm_atomic_get_old_plane_state(old_state, plane);
2453 struct drm_plane_state *new_plane_state =
2454 drm_atomic_get_new_plane_state(old_state, plane);
2455 const struct drm_plane_helper_funcs *plane_funcs;
2457 plane_funcs = plane->helper_private;
2459 if (!old_plane_state || !plane_funcs)
2462 WARN_ON(new_plane_state->crtc &&
2463 new_plane_state->crtc != crtc);
2465 if (drm_atomic_plane_disabling(old_plane_state, new_plane_state) &&
2466 plane_funcs->atomic_disable)
2467 plane_funcs->atomic_disable(plane, old_plane_state);
2468 else if (new_plane_state->crtc ||
2469 drm_atomic_plane_disabling(old_plane_state, new_plane_state))
2470 plane_funcs->atomic_update(plane, old_plane_state);
2473 if (crtc_funcs && crtc_funcs->atomic_flush)
2474 crtc_funcs->atomic_flush(crtc, old_crtc_state);
2476 EXPORT_SYMBOL(drm_atomic_helper_commit_planes_on_crtc);
2479 * drm_atomic_helper_disable_planes_on_crtc - helper to disable CRTC's planes
2480 * @old_crtc_state: atomic state object with the old CRTC state
2481 * @atomic: if set, synchronize with CRTC's atomic_begin/flush hooks
2483 * Disables all planes associated with the given CRTC. This can be
2484 * used for instance in the CRTC helper atomic_disable callback to disable
2487 * If the atomic-parameter is set the function calls the CRTC's
2488 * atomic_begin hook before and atomic_flush hook after disabling the
2491 * It is a bug to call this function without having implemented the
2492 * &drm_plane_helper_funcs.atomic_disable plane hook.
2495 drm_atomic_helper_disable_planes_on_crtc(struct drm_crtc_state *old_crtc_state,
2498 struct drm_crtc *crtc = old_crtc_state->crtc;
2499 const struct drm_crtc_helper_funcs *crtc_funcs =
2500 crtc->helper_private;
2501 struct drm_plane *plane;
2503 if (atomic && crtc_funcs && crtc_funcs->atomic_begin)
2504 crtc_funcs->atomic_begin(crtc, NULL);
2506 drm_atomic_crtc_state_for_each_plane(plane, old_crtc_state) {
2507 const struct drm_plane_helper_funcs *plane_funcs =
2508 plane->helper_private;
2513 WARN_ON(!plane_funcs->atomic_disable);
2514 if (plane_funcs->atomic_disable)
2515 plane_funcs->atomic_disable(plane, NULL);
2518 if (atomic && crtc_funcs && crtc_funcs->atomic_flush)
2519 crtc_funcs->atomic_flush(crtc, NULL);
2521 EXPORT_SYMBOL(drm_atomic_helper_disable_planes_on_crtc);
2524 * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
2526 * @old_state: atomic state object with old state structures
2528 * This function cleans up plane state, specifically framebuffers, from the old
2529 * configuration. Hence the old configuration must be perserved in @old_state to
2530 * be able to call this function.
2532 * This function must also be called on the new state when the atomic update
2533 * fails at any point after calling drm_atomic_helper_prepare_planes().
2535 void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
2536 struct drm_atomic_state *old_state)
2538 struct drm_plane *plane;
2539 struct drm_plane_state *old_plane_state, *new_plane_state;
2542 for_each_oldnew_plane_in_state(old_state, plane, old_plane_state, new_plane_state, i) {
2543 const struct drm_plane_helper_funcs *funcs;
2544 struct drm_plane_state *plane_state;
2547 * This might be called before swapping when commit is aborted,
2548 * in which case we have to cleanup the new state.
2550 if (old_plane_state == plane->state)
2551 plane_state = new_plane_state;
2553 plane_state = old_plane_state;
2555 funcs = plane->helper_private;
2557 if (funcs->cleanup_fb)
2558 funcs->cleanup_fb(plane, plane_state);
2561 EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
2564 * drm_atomic_helper_swap_state - store atomic state into current sw state
2565 * @state: atomic state
2566 * @stall: stall for preceeding commits
2568 * This function stores the atomic state into the current state pointers in all
2569 * driver objects. It should be called after all failing steps have been done
2570 * and succeeded, but before the actual hardware state is committed.
2572 * For cleanup and error recovery the current state for all changed objects will
2573 * be swapped into @state.
2575 * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
2577 * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
2579 * 2. Do any other steps that might fail.
2581 * 3. Put the staged state into the current state pointers with this function.
2583 * 4. Actually commit the hardware state.
2585 * 5. Call drm_atomic_helper_cleanup_planes() with @state, which since step 3
2586 * contains the old state. Also do any other cleanup required with that state.
2588 * @stall must be set when nonblocking commits for this driver directly access
2589 * the &drm_plane.state, &drm_crtc.state or &drm_connector.state pointer. With
2590 * the current atomic helpers this is almost always the case, since the helpers
2591 * don't pass the right state structures to the callbacks.
2595 * Returns 0 on success. Can return -ERESTARTSYS when @stall is true and the
2596 * waiting for the previous commits has been interrupted.
2598 int drm_atomic_helper_swap_state(struct drm_atomic_state *state,
2602 struct drm_connector *connector;
2603 struct drm_connector_state *old_conn_state, *new_conn_state;
2604 struct drm_crtc *crtc;
2605 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
2606 struct drm_plane *plane;
2607 struct drm_plane_state *old_plane_state, *new_plane_state;
2608 struct drm_crtc_commit *commit;
2609 struct drm_private_obj *obj;
2610 struct drm_private_state *old_obj_state, *new_obj_state;
2614 * We have to stall for hw_done here before
2615 * drm_atomic_helper_wait_for_dependencies() because flip
2616 * depth > 1 is not yet supported by all drivers. As long as
2617 * obj->state is directly dereferenced anywhere in the drivers
2618 * atomic_commit_tail function, then it's unsafe to swap state
2619 * before drm_atomic_helper_commit_hw_done() is called.
2622 for_each_old_crtc_in_state(state, crtc, old_crtc_state, i) {
2623 commit = old_crtc_state->commit;
2628 ret = wait_for_completion_interruptible(&commit->hw_done);
2633 for_each_old_connector_in_state(state, connector, old_conn_state, i) {
2634 commit = old_conn_state->commit;
2639 ret = wait_for_completion_interruptible(&commit->hw_done);
2644 for_each_old_plane_in_state(state, plane, old_plane_state, i) {
2645 commit = old_plane_state->commit;
2650 ret = wait_for_completion_interruptible(&commit->hw_done);
2656 for_each_oldnew_connector_in_state(state, connector, old_conn_state, new_conn_state, i) {
2657 WARN_ON(connector->state != old_conn_state);
2659 old_conn_state->state = state;
2660 new_conn_state->state = NULL;
2662 state->connectors[i].state = old_conn_state;
2663 connector->state = new_conn_state;
2666 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
2667 WARN_ON(crtc->state != old_crtc_state);
2669 old_crtc_state->state = state;
2670 new_crtc_state->state = NULL;
2672 state->crtcs[i].state = old_crtc_state;
2673 crtc->state = new_crtc_state;
2675 if (new_crtc_state->commit) {
2676 spin_lock(&crtc->commit_lock);
2677 list_add(&new_crtc_state->commit->commit_entry,
2678 &crtc->commit_list);
2679 spin_unlock(&crtc->commit_lock);
2681 new_crtc_state->commit->event = NULL;
2685 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
2686 WARN_ON(plane->state != old_plane_state);
2688 old_plane_state->state = state;
2689 new_plane_state->state = NULL;
2691 state->planes[i].state = old_plane_state;
2692 plane->state = new_plane_state;
2695 for_each_oldnew_private_obj_in_state(state, obj, old_obj_state, new_obj_state, i) {
2696 WARN_ON(obj->state != old_obj_state);
2698 old_obj_state->state = state;
2699 new_obj_state->state = NULL;
2701 state->private_objs[i].state = old_obj_state;
2702 obj->state = new_obj_state;
2707 EXPORT_SYMBOL(drm_atomic_helper_swap_state);
2710 * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
2711 * @plane: plane object to update
2712 * @crtc: owning CRTC of owning plane
2713 * @fb: framebuffer to flip onto plane
2714 * @crtc_x: x offset of primary plane on crtc
2715 * @crtc_y: y offset of primary plane on crtc
2716 * @crtc_w: width of primary plane rectangle on crtc
2717 * @crtc_h: height of primary plane rectangle on crtc
2718 * @src_x: x offset of @fb for panning
2719 * @src_y: y offset of @fb for panning
2720 * @src_w: width of source rectangle in @fb
2721 * @src_h: height of source rectangle in @fb
2722 * @ctx: lock acquire context
2724 * Provides a default plane update handler using the atomic driver interface.
2727 * Zero on success, error code on failure
2729 int drm_atomic_helper_update_plane(struct drm_plane *plane,
2730 struct drm_crtc *crtc,
2731 struct drm_framebuffer *fb,
2732 int crtc_x, int crtc_y,
2733 unsigned int crtc_w, unsigned int crtc_h,
2734 uint32_t src_x, uint32_t src_y,
2735 uint32_t src_w, uint32_t src_h,
2736 struct drm_modeset_acquire_ctx *ctx)
2738 struct drm_atomic_state *state;
2739 struct drm_plane_state *plane_state;
2742 state = drm_atomic_state_alloc(plane->dev);
2746 state->acquire_ctx = ctx;
2747 plane_state = drm_atomic_get_plane_state(state, plane);
2748 if (IS_ERR(plane_state)) {
2749 ret = PTR_ERR(plane_state);
2753 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
2756 drm_atomic_set_fb_for_plane(plane_state, fb);
2757 plane_state->crtc_x = crtc_x;
2758 plane_state->crtc_y = crtc_y;
2759 plane_state->crtc_w = crtc_w;
2760 plane_state->crtc_h = crtc_h;
2761 plane_state->src_x = src_x;
2762 plane_state->src_y = src_y;
2763 plane_state->src_w = src_w;
2764 plane_state->src_h = src_h;
2766 if (plane == crtc->cursor)
2767 state->legacy_cursor_update = true;
2769 ret = drm_atomic_commit(state);
2771 drm_atomic_state_put(state);
2774 EXPORT_SYMBOL(drm_atomic_helper_update_plane);
2777 * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
2778 * @plane: plane to disable
2779 * @ctx: lock acquire context
2781 * Provides a default plane disable handler using the atomic driver interface.
2784 * Zero on success, error code on failure
2786 int drm_atomic_helper_disable_plane(struct drm_plane *plane,
2787 struct drm_modeset_acquire_ctx *ctx)
2789 struct drm_atomic_state *state;
2790 struct drm_plane_state *plane_state;
2793 state = drm_atomic_state_alloc(plane->dev);
2797 state->acquire_ctx = ctx;
2798 plane_state = drm_atomic_get_plane_state(state, plane);
2799 if (IS_ERR(plane_state)) {
2800 ret = PTR_ERR(plane_state);
2804 if (plane_state->crtc && plane_state->crtc->cursor == plane)
2805 plane_state->state->legacy_cursor_update = true;
2807 ret = __drm_atomic_helper_disable_plane(plane, plane_state);
2811 ret = drm_atomic_commit(state);
2813 drm_atomic_state_put(state);
2816 EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
2818 /* just used from fb-helper and atomic-helper: */
2819 int __drm_atomic_helper_disable_plane(struct drm_plane *plane,
2820 struct drm_plane_state *plane_state)
2824 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
2828 drm_atomic_set_fb_for_plane(plane_state, NULL);
2829 plane_state->crtc_x = 0;
2830 plane_state->crtc_y = 0;
2831 plane_state->crtc_w = 0;
2832 plane_state->crtc_h = 0;
2833 plane_state->src_x = 0;
2834 plane_state->src_y = 0;
2835 plane_state->src_w = 0;
2836 plane_state->src_h = 0;
2841 static int update_output_state(struct drm_atomic_state *state,
2842 struct drm_mode_set *set)
2844 struct drm_device *dev = set->crtc->dev;
2845 struct drm_crtc *crtc;
2846 struct drm_crtc_state *new_crtc_state;
2847 struct drm_connector *connector;
2848 struct drm_connector_state *new_conn_state;
2851 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
2852 state->acquire_ctx);
2856 /* First disable all connectors on the target crtc. */
2857 ret = drm_atomic_add_affected_connectors(state, set->crtc);
2861 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
2862 if (new_conn_state->crtc == set->crtc) {
2863 ret = drm_atomic_set_crtc_for_connector(new_conn_state,
2868 /* Make sure legacy setCrtc always re-trains */
2869 new_conn_state->link_status = DRM_LINK_STATUS_GOOD;
2873 /* Then set all connectors from set->connectors on the target crtc */
2874 for (i = 0; i < set->num_connectors; i++) {
2875 new_conn_state = drm_atomic_get_connector_state(state,
2876 set->connectors[i]);
2877 if (IS_ERR(new_conn_state))
2878 return PTR_ERR(new_conn_state);
2880 ret = drm_atomic_set_crtc_for_connector(new_conn_state,
2886 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
2887 /* Don't update ->enable for the CRTC in the set_config request,
2888 * since a mismatch would indicate a bug in the upper layers.
2889 * The actual modeset code later on will catch any
2890 * inconsistencies here. */
2891 if (crtc == set->crtc)
2894 if (!new_crtc_state->connector_mask) {
2895 ret = drm_atomic_set_mode_prop_for_crtc(new_crtc_state,
2900 new_crtc_state->active = false;
2908 * drm_atomic_helper_set_config - set a new config from userspace
2909 * @set: mode set configuration
2910 * @ctx: lock acquisition context
2912 * Provides a default crtc set_config handler using the atomic driver interface.
2914 * NOTE: For backwards compatibility with old userspace this automatically
2915 * resets the "link-status" property to GOOD, to force any link
2916 * re-training. The SETCRTC ioctl does not define whether an update does
2917 * need a full modeset or just a plane update, hence we're allowed to do
2918 * that. See also drm_connector_set_link_status_property().
2921 * Returns 0 on success, negative errno numbers on failure.
2923 int drm_atomic_helper_set_config(struct drm_mode_set *set,
2924 struct drm_modeset_acquire_ctx *ctx)
2926 struct drm_atomic_state *state;
2927 struct drm_crtc *crtc = set->crtc;
2930 state = drm_atomic_state_alloc(crtc->dev);
2934 state->acquire_ctx = ctx;
2935 ret = __drm_atomic_helper_set_config(set, state);
2939 ret = handle_conflicting_encoders(state, true);
2943 ret = drm_atomic_commit(state);
2946 drm_atomic_state_put(state);
2949 EXPORT_SYMBOL(drm_atomic_helper_set_config);
2951 /* just used from fb-helper and atomic-helper: */
2952 int __drm_atomic_helper_set_config(struct drm_mode_set *set,
2953 struct drm_atomic_state *state)
2955 struct drm_crtc_state *crtc_state;
2956 struct drm_plane_state *primary_state;
2957 struct drm_crtc *crtc = set->crtc;
2958 int hdisplay, vdisplay;
2961 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2962 if (IS_ERR(crtc_state))
2963 return PTR_ERR(crtc_state);
2965 primary_state = drm_atomic_get_plane_state(state, crtc->primary);
2966 if (IS_ERR(primary_state))
2967 return PTR_ERR(primary_state);
2971 WARN_ON(set->num_connectors);
2973 ret = drm_atomic_set_mode_for_crtc(crtc_state, NULL);
2977 crtc_state->active = false;
2979 ret = drm_atomic_set_crtc_for_plane(primary_state, NULL);
2983 drm_atomic_set_fb_for_plane(primary_state, NULL);
2989 WARN_ON(!set->num_connectors);
2991 ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
2995 crtc_state->active = true;
2997 ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
3001 drm_mode_get_hv_timing(set->mode, &hdisplay, &vdisplay);
3003 drm_atomic_set_fb_for_plane(primary_state, set->fb);
3004 primary_state->crtc_x = 0;
3005 primary_state->crtc_y = 0;
3006 primary_state->crtc_w = hdisplay;
3007 primary_state->crtc_h = vdisplay;
3008 primary_state->src_x = set->x << 16;
3009 primary_state->src_y = set->y << 16;
3010 if (drm_rotation_90_or_270(primary_state->rotation)) {
3011 primary_state->src_w = vdisplay << 16;
3012 primary_state->src_h = hdisplay << 16;
3014 primary_state->src_w = hdisplay << 16;
3015 primary_state->src_h = vdisplay << 16;
3019 ret = update_output_state(state, set);
3026 static int __drm_atomic_helper_disable_all(struct drm_device *dev,
3027 struct drm_modeset_acquire_ctx *ctx,
3030 struct drm_atomic_state *state;
3031 struct drm_connector_state *conn_state;
3032 struct drm_connector *conn;
3033 struct drm_plane_state *plane_state;
3034 struct drm_plane *plane;
3035 struct drm_crtc_state *crtc_state;
3036 struct drm_crtc *crtc;
3039 state = drm_atomic_state_alloc(dev);
3043 state->acquire_ctx = ctx;
3045 drm_for_each_crtc(crtc, dev) {
3046 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3047 if (IS_ERR(crtc_state)) {
3048 ret = PTR_ERR(crtc_state);
3052 crtc_state->active = false;
3054 ret = drm_atomic_set_mode_prop_for_crtc(crtc_state, NULL);
3058 ret = drm_atomic_add_affected_planes(state, crtc);
3062 ret = drm_atomic_add_affected_connectors(state, crtc);
3067 for_each_new_connector_in_state(state, conn, conn_state, i) {
3068 ret = drm_atomic_set_crtc_for_connector(conn_state, NULL);
3073 for_each_new_plane_in_state(state, plane, plane_state, i) {
3074 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
3078 drm_atomic_set_fb_for_plane(plane_state, NULL);
3081 ret = drm_atomic_commit(state);
3083 drm_atomic_state_put(state);
3088 * drm_atomic_helper_disable_all - disable all currently active outputs
3090 * @ctx: lock acquisition context
3092 * Loops through all connectors, finding those that aren't turned off and then
3093 * turns them off by setting their DPMS mode to OFF and deactivating the CRTC
3094 * that they are connected to.
3096 * This is used for example in suspend/resume to disable all currently active
3097 * functions when suspending. If you just want to shut down everything at e.g.
3098 * driver unload, look at drm_atomic_helper_shutdown().
3100 * Note that if callers haven't already acquired all modeset locks this might
3101 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
3104 * 0 on success or a negative error code on failure.
3107 * drm_atomic_helper_suspend(), drm_atomic_helper_resume() and
3108 * drm_atomic_helper_shutdown().
3110 int drm_atomic_helper_disable_all(struct drm_device *dev,
3111 struct drm_modeset_acquire_ctx *ctx)
3113 return __drm_atomic_helper_disable_all(dev, ctx, false);
3115 EXPORT_SYMBOL(drm_atomic_helper_disable_all);
3118 * drm_atomic_helper_shutdown - shutdown all CRTC
3121 * This shuts down all CRTC, which is useful for driver unloading. Shutdown on
3122 * suspend should instead be handled with drm_atomic_helper_suspend(), since
3123 * that also takes a snapshot of the modeset state to be restored on resume.
3125 * This is just a convenience wrapper around drm_atomic_helper_disable_all(),
3126 * and it is the atomic version of drm_crtc_force_disable_all().
3128 void drm_atomic_helper_shutdown(struct drm_device *dev)
3130 struct drm_modeset_acquire_ctx ctx;
3133 drm_modeset_acquire_init(&ctx, 0);
3135 ret = drm_modeset_lock_all_ctx(dev, &ctx);
3137 ret = __drm_atomic_helper_disable_all(dev, &ctx, true);
3139 if (ret != -EDEADLK)
3142 drm_modeset_backoff(&ctx);
3146 DRM_ERROR("Disabling all crtc's during unload failed with %i\n", ret);
3148 drm_modeset_drop_locks(&ctx);
3149 drm_modeset_acquire_fini(&ctx);
3151 EXPORT_SYMBOL(drm_atomic_helper_shutdown);
3154 * drm_atomic_helper_suspend - subsystem-level suspend helper
3157 * Duplicates the current atomic state, disables all active outputs and then
3158 * returns a pointer to the original atomic state to the caller. Drivers can
3159 * pass this pointer to the drm_atomic_helper_resume() helper upon resume to
3160 * restore the output configuration that was active at the time the system
3163 * Note that it is potentially unsafe to use this. The atomic state object
3164 * returned by this function is assumed to be persistent. Drivers must ensure
3165 * that this holds true. Before calling this function, drivers must make sure
3166 * to suspend fbdev emulation so that nothing can be using the device.
3169 * A pointer to a copy of the state before suspend on success or an ERR_PTR()-
3170 * encoded error code on failure. Drivers should store the returned atomic
3171 * state object and pass it to the drm_atomic_helper_resume() helper upon
3175 * drm_atomic_helper_duplicate_state(), drm_atomic_helper_disable_all(),
3176 * drm_atomic_helper_resume(), drm_atomic_helper_commit_duplicated_state()
3178 struct drm_atomic_state *drm_atomic_helper_suspend(struct drm_device *dev)
3180 struct drm_modeset_acquire_ctx ctx;
3181 struct drm_atomic_state *state;
3184 drm_modeset_acquire_init(&ctx, 0);
3187 err = drm_modeset_lock_all_ctx(dev, &ctx);
3189 state = ERR_PTR(err);
3193 state = drm_atomic_helper_duplicate_state(dev, &ctx);
3197 err = drm_atomic_helper_disable_all(dev, &ctx);
3199 drm_atomic_state_put(state);
3200 state = ERR_PTR(err);
3205 if (PTR_ERR(state) == -EDEADLK) {
3206 drm_modeset_backoff(&ctx);
3210 drm_modeset_drop_locks(&ctx);
3211 drm_modeset_acquire_fini(&ctx);
3214 EXPORT_SYMBOL(drm_atomic_helper_suspend);
3217 * drm_atomic_helper_commit_duplicated_state - commit duplicated state
3218 * @state: duplicated atomic state to commit
3219 * @ctx: pointer to acquire_ctx to use for commit.
3221 * The state returned by drm_atomic_helper_duplicate_state() and
3222 * drm_atomic_helper_suspend() is partially invalid, and needs to
3223 * be fixed up before commit.
3226 * 0 on success or a negative error code on failure.
3229 * drm_atomic_helper_suspend()
3231 int drm_atomic_helper_commit_duplicated_state(struct drm_atomic_state *state,
3232 struct drm_modeset_acquire_ctx *ctx)
3235 struct drm_plane *plane;
3236 struct drm_plane_state *new_plane_state;
3237 struct drm_connector *connector;
3238 struct drm_connector_state *new_conn_state;
3239 struct drm_crtc *crtc;
3240 struct drm_crtc_state *new_crtc_state;
3242 state->acquire_ctx = ctx;
3244 for_each_new_plane_in_state(state, plane, new_plane_state, i)
3245 state->planes[i].old_state = plane->state;
3247 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i)
3248 state->crtcs[i].old_state = crtc->state;
3250 for_each_new_connector_in_state(state, connector, new_conn_state, i)
3251 state->connectors[i].old_state = connector->state;
3253 ret = drm_atomic_commit(state);
3255 state->acquire_ctx = NULL;
3259 EXPORT_SYMBOL(drm_atomic_helper_commit_duplicated_state);
3262 * drm_atomic_helper_resume - subsystem-level resume helper
3264 * @state: atomic state to resume to
3266 * Calls drm_mode_config_reset() to synchronize hardware and software states,
3267 * grabs all modeset locks and commits the atomic state object. This can be
3268 * used in conjunction with the drm_atomic_helper_suspend() helper to
3269 * implement suspend/resume for drivers that support atomic mode-setting.
3272 * 0 on success or a negative error code on failure.
3275 * drm_atomic_helper_suspend()
3277 int drm_atomic_helper_resume(struct drm_device *dev,
3278 struct drm_atomic_state *state)
3280 struct drm_modeset_acquire_ctx ctx;
3283 drm_mode_config_reset(dev);
3285 drm_modeset_acquire_init(&ctx, 0);
3287 err = drm_modeset_lock_all_ctx(dev, &ctx);
3291 err = drm_atomic_helper_commit_duplicated_state(state, &ctx);
3293 if (err != -EDEADLK)
3296 drm_modeset_backoff(&ctx);
3299 drm_atomic_state_put(state);
3300 drm_modeset_drop_locks(&ctx);
3301 drm_modeset_acquire_fini(&ctx);
3305 EXPORT_SYMBOL(drm_atomic_helper_resume);
3307 static int page_flip_common(struct drm_atomic_state *state,
3308 struct drm_crtc *crtc,
3309 struct drm_framebuffer *fb,
3310 struct drm_pending_vblank_event *event,
3313 struct drm_plane *plane = crtc->primary;
3314 struct drm_plane_state *plane_state;
3315 struct drm_crtc_state *crtc_state;
3318 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3319 if (IS_ERR(crtc_state))
3320 return PTR_ERR(crtc_state);
3322 crtc_state->event = event;
3323 crtc_state->pageflip_flags = flags;
3325 plane_state = drm_atomic_get_plane_state(state, plane);
3326 if (IS_ERR(plane_state))
3327 return PTR_ERR(plane_state);
3329 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
3332 drm_atomic_set_fb_for_plane(plane_state, fb);
3334 /* Make sure we don't accidentally do a full modeset. */
3335 state->allow_modeset = false;
3336 if (!crtc_state->active) {
3337 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] disabled, rejecting legacy flip\n",
3338 crtc->base.id, crtc->name);
3346 * drm_atomic_helper_page_flip - execute a legacy page flip
3348 * @fb: DRM framebuffer
3349 * @event: optional DRM event to signal upon completion
3350 * @flags: flip flags for non-vblank sync'ed updates
3351 * @ctx: lock acquisition context
3353 * Provides a default &drm_crtc_funcs.page_flip implementation
3354 * using the atomic driver interface.
3357 * Returns 0 on success, negative errno numbers on failure.
3360 * drm_atomic_helper_page_flip_target()
3362 int drm_atomic_helper_page_flip(struct drm_crtc *crtc,
3363 struct drm_framebuffer *fb,
3364 struct drm_pending_vblank_event *event,
3366 struct drm_modeset_acquire_ctx *ctx)
3368 struct drm_plane *plane = crtc->primary;
3369 struct drm_atomic_state *state;
3372 state = drm_atomic_state_alloc(plane->dev);
3376 state->acquire_ctx = ctx;
3378 ret = page_flip_common(state, crtc, fb, event, flags);
3382 ret = drm_atomic_nonblocking_commit(state);
3384 drm_atomic_state_put(state);
3387 EXPORT_SYMBOL(drm_atomic_helper_page_flip);
3390 * drm_atomic_helper_page_flip_target - do page flip on target vblank period.
3392 * @fb: DRM framebuffer
3393 * @event: optional DRM event to signal upon completion
3394 * @flags: flip flags for non-vblank sync'ed updates
3395 * @target: specifying the target vblank period when the flip to take effect
3396 * @ctx: lock acquisition context
3398 * Provides a default &drm_crtc_funcs.page_flip_target implementation.
3399 * Similar to drm_atomic_helper_page_flip() with extra parameter to specify
3400 * target vblank period to flip.
3403 * Returns 0 on success, negative errno numbers on failure.
3405 int drm_atomic_helper_page_flip_target(struct drm_crtc *crtc,
3406 struct drm_framebuffer *fb,
3407 struct drm_pending_vblank_event *event,
3410 struct drm_modeset_acquire_ctx *ctx)
3412 struct drm_plane *plane = crtc->primary;
3413 struct drm_atomic_state *state;
3414 struct drm_crtc_state *crtc_state;
3417 state = drm_atomic_state_alloc(plane->dev);
3421 state->acquire_ctx = ctx;
3423 ret = page_flip_common(state, crtc, fb, event, flags);
3427 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
3428 if (WARN_ON(!crtc_state)) {
3432 crtc_state->target_vblank = target;
3434 ret = drm_atomic_nonblocking_commit(state);
3436 drm_atomic_state_put(state);
3439 EXPORT_SYMBOL(drm_atomic_helper_page_flip_target);
3442 * drm_atomic_helper_best_encoder - Helper for
3443 * &drm_connector_helper_funcs.best_encoder callback
3444 * @connector: Connector control structure
3446 * This is a &drm_connector_helper_funcs.best_encoder callback helper for
3447 * connectors that support exactly 1 encoder, statically determined at driver
3450 struct drm_encoder *
3451 drm_atomic_helper_best_encoder(struct drm_connector *connector)
3453 WARN_ON(connector->encoder_ids[1]);
3454 return drm_encoder_find(connector->dev, NULL, connector->encoder_ids[0]);
3456 EXPORT_SYMBOL(drm_atomic_helper_best_encoder);
3459 * DOC: atomic state reset and initialization
3461 * Both the drm core and the atomic helpers assume that there is always the full
3462 * and correct atomic software state for all connectors, CRTCs and planes
3463 * available. Which is a bit a problem on driver load and also after system
3464 * suspend. One way to solve this is to have a hardware state read-out
3465 * infrastructure which reconstructs the full software state (e.g. the i915
3468 * The simpler solution is to just reset the software state to everything off,
3469 * which is easiest to do by calling drm_mode_config_reset(). To facilitate this
3470 * the atomic helpers provide default reset implementations for all hooks.
3472 * On the upside the precise state tracking of atomic simplifies system suspend
3473 * and resume a lot. For drivers using drm_mode_config_reset() a complete recipe
3474 * is implemented in drm_atomic_helper_suspend() and drm_atomic_helper_resume().
3475 * For other drivers the building blocks are split out, see the documentation
3476 * for these functions.
3480 * drm_atomic_helper_crtc_reset - default &drm_crtc_funcs.reset hook for CRTCs
3483 * Resets the atomic state for @crtc by freeing the state pointer (which might
3484 * be NULL, e.g. at driver load time) and allocating a new empty state object.
3486 void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc)
3489 __drm_atomic_helper_crtc_destroy_state(crtc->state);
3492 crtc->state = kzalloc(sizeof(*crtc->state), GFP_KERNEL);
3495 crtc->state->crtc = crtc;
3497 EXPORT_SYMBOL(drm_atomic_helper_crtc_reset);
3500 * __drm_atomic_helper_crtc_duplicate_state - copy atomic CRTC state
3501 * @crtc: CRTC object
3502 * @state: atomic CRTC state
3504 * Copies atomic state from a CRTC's current state and resets inferred values.
3505 * This is useful for drivers that subclass the CRTC state.
3507 void __drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc,
3508 struct drm_crtc_state *state)
3510 memcpy(state, crtc->state, sizeof(*state));
3512 if (state->mode_blob)
3513 drm_property_blob_get(state->mode_blob);
3514 if (state->degamma_lut)
3515 drm_property_blob_get(state->degamma_lut);
3517 drm_property_blob_get(state->ctm);
3518 if (state->gamma_lut)
3519 drm_property_blob_get(state->gamma_lut);
3520 state->mode_changed = false;
3521 state->active_changed = false;
3522 state->planes_changed = false;
3523 state->connectors_changed = false;
3524 state->color_mgmt_changed = false;
3525 state->zpos_changed = false;
3526 state->commit = NULL;
3527 state->event = NULL;
3528 state->pageflip_flags = 0;
3530 EXPORT_SYMBOL(__drm_atomic_helper_crtc_duplicate_state);
3533 * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
3536 * Default CRTC state duplicate hook for drivers which don't have their own
3537 * subclassed CRTC state structure.
3539 struct drm_crtc_state *
3540 drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc)
3542 struct drm_crtc_state *state;
3544 if (WARN_ON(!crtc->state))
3547 state = kmalloc(sizeof(*state), GFP_KERNEL);
3549 __drm_atomic_helper_crtc_duplicate_state(crtc, state);
3553 EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state);
3556 * __drm_atomic_helper_crtc_destroy_state - release CRTC state
3557 * @state: CRTC state object to release
3559 * Releases all resources stored in the CRTC state without actually freeing
3560 * the memory of the CRTC state. This is useful for drivers that subclass the
3563 void __drm_atomic_helper_crtc_destroy_state(struct drm_crtc_state *state)
3565 if (state->commit) {
3567 * In the event that a non-blocking commit returns
3568 * -ERESTARTSYS before the commit_tail work is queued, we will
3569 * have an extra reference to the commit object. Release it, if
3570 * the event has not been consumed by the worker.
3572 * state->event may be freed, so we can't directly look at
3573 * state->event->base.completion.
3575 if (state->event && state->commit->abort_completion)
3576 drm_crtc_commit_put(state->commit);
3578 kfree(state->commit->event);
3579 state->commit->event = NULL;
3581 drm_crtc_commit_put(state->commit);
3584 drm_property_blob_put(state->mode_blob);
3585 drm_property_blob_put(state->degamma_lut);
3586 drm_property_blob_put(state->ctm);
3587 drm_property_blob_put(state->gamma_lut);
3589 EXPORT_SYMBOL(__drm_atomic_helper_crtc_destroy_state);
3592 * drm_atomic_helper_crtc_destroy_state - default state destroy hook
3594 * @state: CRTC state object to release
3596 * Default CRTC state destroy hook for drivers which don't have their own
3597 * subclassed CRTC state structure.
3599 void drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
3600 struct drm_crtc_state *state)
3602 __drm_atomic_helper_crtc_destroy_state(state);
3605 EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state);
3608 * drm_atomic_helper_plane_reset - default &drm_plane_funcs.reset hook for planes
3611 * Resets the atomic state for @plane by freeing the state pointer (which might
3612 * be NULL, e.g. at driver load time) and allocating a new empty state object.
3614 void drm_atomic_helper_plane_reset(struct drm_plane *plane)
3617 __drm_atomic_helper_plane_destroy_state(plane->state);
3619 kfree(plane->state);
3620 plane->state = kzalloc(sizeof(*plane->state), GFP_KERNEL);
3623 plane->state->plane = plane;
3624 plane->state->rotation = DRM_MODE_ROTATE_0;
3626 /* Reset the alpha value to fully opaque if it matters */
3627 if (plane->alpha_property)
3628 plane->state->alpha = plane->alpha_property->values[1];
3631 EXPORT_SYMBOL(drm_atomic_helper_plane_reset);
3634 * __drm_atomic_helper_plane_duplicate_state - copy atomic plane state
3635 * @plane: plane object
3636 * @state: atomic plane state
3638 * Copies atomic state from a plane's current state. This is useful for
3639 * drivers that subclass the plane state.
3641 void __drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane,
3642 struct drm_plane_state *state)
3644 memcpy(state, plane->state, sizeof(*state));
3647 drm_framebuffer_get(state->fb);
3649 state->fence = NULL;
3650 state->commit = NULL;
3652 EXPORT_SYMBOL(__drm_atomic_helper_plane_duplicate_state);
3655 * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
3658 * Default plane state duplicate hook for drivers which don't have their own
3659 * subclassed plane state structure.
3661 struct drm_plane_state *
3662 drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane)
3664 struct drm_plane_state *state;
3666 if (WARN_ON(!plane->state))
3669 state = kmalloc(sizeof(*state), GFP_KERNEL);
3671 __drm_atomic_helper_plane_duplicate_state(plane, state);
3675 EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state);
3678 * __drm_atomic_helper_plane_destroy_state - release plane state
3679 * @state: plane state object to release
3681 * Releases all resources stored in the plane state without actually freeing
3682 * the memory of the plane state. This is useful for drivers that subclass the
3685 void __drm_atomic_helper_plane_destroy_state(struct drm_plane_state *state)
3688 drm_framebuffer_put(state->fb);
3691 dma_fence_put(state->fence);
3694 drm_crtc_commit_put(state->commit);
3696 EXPORT_SYMBOL(__drm_atomic_helper_plane_destroy_state);
3699 * drm_atomic_helper_plane_destroy_state - default state destroy hook
3701 * @state: plane state object to release
3703 * Default plane state destroy hook for drivers which don't have their own
3704 * subclassed plane state structure.
3706 void drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
3707 struct drm_plane_state *state)
3709 __drm_atomic_helper_plane_destroy_state(state);
3712 EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state);
3715 * __drm_atomic_helper_connector_reset - reset state on connector
3716 * @connector: drm connector
3717 * @conn_state: connector state to assign
3719 * Initializes the newly allocated @conn_state and assigns it to
3720 * the &drm_conector->state pointer of @connector, usually required when
3721 * initializing the drivers or when called from the &drm_connector_funcs.reset
3724 * This is useful for drivers that subclass the connector state.
3727 __drm_atomic_helper_connector_reset(struct drm_connector *connector,
3728 struct drm_connector_state *conn_state)
3731 conn_state->connector = connector;
3733 connector->state = conn_state;
3735 EXPORT_SYMBOL(__drm_atomic_helper_connector_reset);
3738 * drm_atomic_helper_connector_reset - default &drm_connector_funcs.reset hook for connectors
3739 * @connector: drm connector
3741 * Resets the atomic state for @connector by freeing the state pointer (which
3742 * might be NULL, e.g. at driver load time) and allocating a new empty state
3745 void drm_atomic_helper_connector_reset(struct drm_connector *connector)
3747 struct drm_connector_state *conn_state =
3748 kzalloc(sizeof(*conn_state), GFP_KERNEL);
3750 if (connector->state)
3751 __drm_atomic_helper_connector_destroy_state(connector->state);
3753 kfree(connector->state);
3754 __drm_atomic_helper_connector_reset(connector, conn_state);
3756 EXPORT_SYMBOL(drm_atomic_helper_connector_reset);
3759 * __drm_atomic_helper_connector_duplicate_state - copy atomic connector state
3760 * @connector: connector object
3761 * @state: atomic connector state
3763 * Copies atomic state from a connector's current state. This is useful for
3764 * drivers that subclass the connector state.
3767 __drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector,
3768 struct drm_connector_state *state)
3770 memcpy(state, connector->state, sizeof(*state));
3772 drm_connector_get(connector);
3773 state->commit = NULL;
3775 /* Don't copy over a writeback job, they are used only once */
3776 state->writeback_job = NULL;
3778 EXPORT_SYMBOL(__drm_atomic_helper_connector_duplicate_state);
3781 * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
3782 * @connector: drm connector
3784 * Default connector state duplicate hook for drivers which don't have their own
3785 * subclassed connector state structure.
3787 struct drm_connector_state *
3788 drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
3790 struct drm_connector_state *state;
3792 if (WARN_ON(!connector->state))
3795 state = kmalloc(sizeof(*state), GFP_KERNEL);
3797 __drm_atomic_helper_connector_duplicate_state(connector, state);
3801 EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
3804 * drm_atomic_helper_duplicate_state - duplicate an atomic state object
3806 * @ctx: lock acquisition context
3808 * Makes a copy of the current atomic state by looping over all objects and
3809 * duplicating their respective states. This is used for example by suspend/
3810 * resume support code to save the state prior to suspend such that it can
3811 * be restored upon resume.
3813 * Note that this treats atomic state as persistent between save and restore.
3814 * Drivers must make sure that this is possible and won't result in confusion
3815 * or erroneous behaviour.
3817 * Note that if callers haven't already acquired all modeset locks this might
3818 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
3821 * A pointer to the copy of the atomic state object on success or an
3822 * ERR_PTR()-encoded error code on failure.
3825 * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
3827 struct drm_atomic_state *
3828 drm_atomic_helper_duplicate_state(struct drm_device *dev,
3829 struct drm_modeset_acquire_ctx *ctx)
3831 struct drm_atomic_state *state;
3832 struct drm_connector *conn;
3833 struct drm_connector_list_iter conn_iter;
3834 struct drm_plane *plane;
3835 struct drm_crtc *crtc;
3838 state = drm_atomic_state_alloc(dev);
3840 return ERR_PTR(-ENOMEM);
3842 state->acquire_ctx = ctx;
3844 drm_for_each_crtc(crtc, dev) {
3845 struct drm_crtc_state *crtc_state;
3847 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3848 if (IS_ERR(crtc_state)) {
3849 err = PTR_ERR(crtc_state);
3854 drm_for_each_plane(plane, dev) {
3855 struct drm_plane_state *plane_state;
3857 plane_state = drm_atomic_get_plane_state(state, plane);
3858 if (IS_ERR(plane_state)) {
3859 err = PTR_ERR(plane_state);
3864 drm_connector_list_iter_begin(dev, &conn_iter);
3865 drm_for_each_connector_iter(conn, &conn_iter) {
3866 struct drm_connector_state *conn_state;
3868 conn_state = drm_atomic_get_connector_state(state, conn);
3869 if (IS_ERR(conn_state)) {
3870 err = PTR_ERR(conn_state);
3871 drm_connector_list_iter_end(&conn_iter);
3875 drm_connector_list_iter_end(&conn_iter);
3877 /* clear the acquire context so that it isn't accidentally reused */
3878 state->acquire_ctx = NULL;
3882 drm_atomic_state_put(state);
3883 state = ERR_PTR(err);
3888 EXPORT_SYMBOL(drm_atomic_helper_duplicate_state);
3891 * __drm_atomic_helper_connector_destroy_state - release connector state
3892 * @state: connector state object to release
3894 * Releases all resources stored in the connector state without actually
3895 * freeing the memory of the connector state. This is useful for drivers that
3896 * subclass the connector state.
3899 __drm_atomic_helper_connector_destroy_state(struct drm_connector_state *state)
3902 drm_connector_put(state->connector);
3905 drm_crtc_commit_put(state->commit);
3907 EXPORT_SYMBOL(__drm_atomic_helper_connector_destroy_state);
3910 * drm_atomic_helper_connector_destroy_state - default state destroy hook
3911 * @connector: drm connector
3912 * @state: connector state object to release
3914 * Default connector state destroy hook for drivers which don't have their own
3915 * subclassed connector state structure.
3917 void drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
3918 struct drm_connector_state *state)
3920 __drm_atomic_helper_connector_destroy_state(state);
3923 EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state);
3926 * drm_atomic_helper_legacy_gamma_set - set the legacy gamma correction table
3927 * @crtc: CRTC object
3928 * @red: red correction table
3929 * @green: green correction table
3930 * @blue: green correction table
3931 * @size: size of the tables
3932 * @ctx: lock acquire context
3934 * Implements support for legacy gamma correction table for drivers
3935 * that support color management through the DEGAMMA_LUT/GAMMA_LUT
3936 * properties. See drm_crtc_enable_color_mgmt() and the containing chapter for
3937 * how the atomic color management and gamma tables work.
3939 int drm_atomic_helper_legacy_gamma_set(struct drm_crtc *crtc,
3940 u16 *red, u16 *green, u16 *blue,
3942 struct drm_modeset_acquire_ctx *ctx)
3944 struct drm_device *dev = crtc->dev;
3945 struct drm_atomic_state *state;
3946 struct drm_crtc_state *crtc_state;
3947 struct drm_property_blob *blob = NULL;
3948 struct drm_color_lut *blob_data;
3952 state = drm_atomic_state_alloc(crtc->dev);
3956 blob = drm_property_create_blob(dev,
3957 sizeof(struct drm_color_lut) * size,
3960 ret = PTR_ERR(blob);
3965 /* Prepare GAMMA_LUT with the legacy values. */
3966 blob_data = blob->data;
3967 for (i = 0; i < size; i++) {
3968 blob_data[i].red = red[i];
3969 blob_data[i].green = green[i];
3970 blob_data[i].blue = blue[i];
3973 state->acquire_ctx = ctx;
3974 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3975 if (IS_ERR(crtc_state)) {
3976 ret = PTR_ERR(crtc_state);
3980 /* Reset DEGAMMA_LUT and CTM properties. */
3981 replaced = drm_property_replace_blob(&crtc_state->degamma_lut, NULL);
3982 replaced |= drm_property_replace_blob(&crtc_state->ctm, NULL);
3983 replaced |= drm_property_replace_blob(&crtc_state->gamma_lut, blob);
3984 crtc_state->color_mgmt_changed |= replaced;
3986 ret = drm_atomic_commit(state);
3989 drm_atomic_state_put(state);
3990 drm_property_blob_put(blob);
3993 EXPORT_SYMBOL(drm_atomic_helper_legacy_gamma_set);
3996 * __drm_atomic_helper_private_duplicate_state - copy atomic private state
3998 * @state: new private object state
4000 * Copies atomic state from a private objects's current state and resets inferred values.
4001 * This is useful for drivers that subclass the private state.
4003 void __drm_atomic_helper_private_obj_duplicate_state(struct drm_private_obj *obj,
4004 struct drm_private_state *state)
4006 memcpy(state, obj->state, sizeof(*state));
4008 EXPORT_SYMBOL(__drm_atomic_helper_private_obj_duplicate_state);