2 * Copyright (C) 2014 Red Hat
3 * Copyright (C) 2014 Intel Corp.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
29 #include <drm/drm_atomic.h>
30 #include <drm/drm_atomic_uapi.h>
31 #include <drm/drm_plane_helper.h>
32 #include <drm/drm_crtc_helper.h>
33 #include <drm/drm_atomic_helper.h>
34 #include <drm/drm_writeback.h>
35 #include <linux/dma-fence.h>
37 #include "drm_crtc_helper_internal.h"
38 #include "drm_crtc_internal.h"
43 * This helper library provides implementations of check and commit functions on
44 * top of the CRTC modeset helper callbacks and the plane helper callbacks. It
45 * also provides convenience implementations for the atomic state handling
46 * callbacks for drivers which don't need to subclass the drm core structures to
47 * add their own additional internal state.
49 * This library also provides default implementations for the check callback in
50 * drm_atomic_helper_check() and for the commit callback with
51 * drm_atomic_helper_commit(). But the individual stages and callbacks are
52 * exposed to allow drivers to mix and match and e.g. use the plane helpers only
53 * together with a driver private modeset implementation.
55 * This library also provides implementations for all the legacy driver
56 * interfaces on top of the atomic interface. See drm_atomic_helper_set_config(),
57 * drm_atomic_helper_disable_plane(), drm_atomic_helper_disable_plane() and the
58 * various functions to implement set_property callbacks. New drivers must not
59 * implement these functions themselves but must use the provided helpers.
61 * The atomic helper uses the same function table structures as all other
62 * modesetting helpers. See the documentation for &struct drm_crtc_helper_funcs,
63 * struct &drm_encoder_helper_funcs and &struct drm_connector_helper_funcs. It
64 * also shares the &struct drm_plane_helper_funcs function table with the plane
68 drm_atomic_helper_plane_changed(struct drm_atomic_state *state,
69 struct drm_plane_state *old_plane_state,
70 struct drm_plane_state *plane_state,
71 struct drm_plane *plane)
73 struct drm_crtc_state *crtc_state;
75 if (old_plane_state->crtc) {
76 crtc_state = drm_atomic_get_new_crtc_state(state,
77 old_plane_state->crtc);
79 if (WARN_ON(!crtc_state))
82 crtc_state->planes_changed = true;
85 if (plane_state->crtc) {
86 crtc_state = drm_atomic_get_new_crtc_state(state, plane_state->crtc);
88 if (WARN_ON(!crtc_state))
91 crtc_state->planes_changed = true;
96 * For connectors that support multiple encoders, either the
97 * .atomic_best_encoder() or .best_encoder() operation must be implemented.
99 static struct drm_encoder *
100 pick_single_encoder_for_connector(struct drm_connector *connector)
102 WARN_ON(connector->encoder_ids[1]);
103 return drm_encoder_find(connector->dev, NULL, connector->encoder_ids[0]);
106 static int handle_conflicting_encoders(struct drm_atomic_state *state,
107 bool disable_conflicting_encoders)
109 struct drm_connector_state *new_conn_state;
110 struct drm_connector *connector;
111 struct drm_connector_list_iter conn_iter;
112 struct drm_encoder *encoder;
113 unsigned encoder_mask = 0;
117 * First loop, find all newly assigned encoders from the connectors
118 * part of the state. If the same encoder is assigned to multiple
119 * connectors bail out.
121 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
122 const struct drm_connector_helper_funcs *funcs = connector->helper_private;
123 struct drm_encoder *new_encoder;
125 if (!new_conn_state->crtc)
128 if (funcs->atomic_best_encoder)
129 new_encoder = funcs->atomic_best_encoder(connector, new_conn_state);
130 else if (funcs->best_encoder)
131 new_encoder = funcs->best_encoder(connector);
133 new_encoder = pick_single_encoder_for_connector(connector);
136 if (encoder_mask & drm_encoder_mask(new_encoder)) {
137 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] on [CONNECTOR:%d:%s] already assigned\n",
138 new_encoder->base.id, new_encoder->name,
139 connector->base.id, connector->name);
144 encoder_mask |= drm_encoder_mask(new_encoder);
152 * Second loop, iterate over all connectors not part of the state.
154 * If a conflicting encoder is found and disable_conflicting_encoders
155 * is not set, an error is returned. Userspace can provide a solution
156 * through the atomic ioctl.
158 * If the flag is set conflicting connectors are removed from the crtc
159 * and the crtc is disabled if no encoder is left. This preserves
160 * compatibility with the legacy set_config behavior.
162 drm_connector_list_iter_begin(state->dev, &conn_iter);
163 drm_for_each_connector_iter(connector, &conn_iter) {
164 struct drm_crtc_state *crtc_state;
166 if (drm_atomic_get_new_connector_state(state, connector))
169 encoder = connector->state->best_encoder;
170 if (!encoder || !(encoder_mask & drm_encoder_mask(encoder)))
173 if (!disable_conflicting_encoders) {
174 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s] by [CONNECTOR:%d:%s]\n",
175 encoder->base.id, encoder->name,
176 connector->state->crtc->base.id,
177 connector->state->crtc->name,
178 connector->base.id, connector->name);
183 new_conn_state = drm_atomic_get_connector_state(state, connector);
184 if (IS_ERR(new_conn_state)) {
185 ret = PTR_ERR(new_conn_state);
189 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s], disabling [CONNECTOR:%d:%s]\n",
190 encoder->base.id, encoder->name,
191 new_conn_state->crtc->base.id, new_conn_state->crtc->name,
192 connector->base.id, connector->name);
194 crtc_state = drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
196 ret = drm_atomic_set_crtc_for_connector(new_conn_state, NULL);
200 if (!crtc_state->connector_mask) {
201 ret = drm_atomic_set_mode_prop_for_crtc(crtc_state,
206 crtc_state->active = false;
210 drm_connector_list_iter_end(&conn_iter);
216 set_best_encoder(struct drm_atomic_state *state,
217 struct drm_connector_state *conn_state,
218 struct drm_encoder *encoder)
220 struct drm_crtc_state *crtc_state;
221 struct drm_crtc *crtc;
223 if (conn_state->best_encoder) {
224 /* Unset the encoder_mask in the old crtc state. */
225 crtc = conn_state->connector->state->crtc;
227 /* A NULL crtc is an error here because we should have
228 * duplicated a NULL best_encoder when crtc was NULL.
229 * As an exception restoring duplicated atomic state
230 * during resume is allowed, so don't warn when
231 * best_encoder is equal to encoder we intend to set.
233 WARN_ON(!crtc && encoder != conn_state->best_encoder);
235 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
237 crtc_state->encoder_mask &=
238 ~drm_encoder_mask(conn_state->best_encoder);
243 crtc = conn_state->crtc;
246 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
248 crtc_state->encoder_mask |=
249 drm_encoder_mask(encoder);
253 conn_state->best_encoder = encoder;
257 steal_encoder(struct drm_atomic_state *state,
258 struct drm_encoder *encoder)
260 struct drm_crtc_state *crtc_state;
261 struct drm_connector *connector;
262 struct drm_connector_state *old_connector_state, *new_connector_state;
265 for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
266 struct drm_crtc *encoder_crtc;
268 if (new_connector_state->best_encoder != encoder)
271 encoder_crtc = old_connector_state->crtc;
273 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s], stealing it\n",
274 encoder->base.id, encoder->name,
275 encoder_crtc->base.id, encoder_crtc->name);
277 set_best_encoder(state, new_connector_state, NULL);
279 crtc_state = drm_atomic_get_new_crtc_state(state, encoder_crtc);
280 crtc_state->connectors_changed = true;
287 update_connector_routing(struct drm_atomic_state *state,
288 struct drm_connector *connector,
289 struct drm_connector_state *old_connector_state,
290 struct drm_connector_state *new_connector_state)
292 const struct drm_connector_helper_funcs *funcs;
293 struct drm_encoder *new_encoder;
294 struct drm_crtc_state *crtc_state;
296 DRM_DEBUG_ATOMIC("Updating routing for [CONNECTOR:%d:%s]\n",
300 if (old_connector_state->crtc != new_connector_state->crtc) {
301 if (old_connector_state->crtc) {
302 crtc_state = drm_atomic_get_new_crtc_state(state, old_connector_state->crtc);
303 crtc_state->connectors_changed = true;
306 if (new_connector_state->crtc) {
307 crtc_state = drm_atomic_get_new_crtc_state(state, new_connector_state->crtc);
308 crtc_state->connectors_changed = true;
312 if (!new_connector_state->crtc) {
313 DRM_DEBUG_ATOMIC("Disabling [CONNECTOR:%d:%s]\n",
317 set_best_encoder(state, new_connector_state, NULL);
322 funcs = connector->helper_private;
324 if (funcs->atomic_best_encoder)
325 new_encoder = funcs->atomic_best_encoder(connector,
326 new_connector_state);
327 else if (funcs->best_encoder)
328 new_encoder = funcs->best_encoder(connector);
330 new_encoder = pick_single_encoder_for_connector(connector);
333 DRM_DEBUG_ATOMIC("No suitable encoder found for [CONNECTOR:%d:%s]\n",
339 if (!drm_encoder_crtc_ok(new_encoder, new_connector_state->crtc)) {
340 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] incompatible with [CRTC:%d:%s]\n",
341 new_encoder->base.id,
343 new_connector_state->crtc->base.id,
344 new_connector_state->crtc->name);
348 if (new_encoder == new_connector_state->best_encoder) {
349 set_best_encoder(state, new_connector_state, new_encoder);
351 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] keeps [ENCODER:%d:%s], now on [CRTC:%d:%s]\n",
354 new_encoder->base.id,
356 new_connector_state->crtc->base.id,
357 new_connector_state->crtc->name);
362 steal_encoder(state, new_encoder);
364 set_best_encoder(state, new_connector_state, new_encoder);
366 crtc_state = drm_atomic_get_new_crtc_state(state, new_connector_state->crtc);
367 crtc_state->connectors_changed = true;
369 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] using [ENCODER:%d:%s] on [CRTC:%d:%s]\n",
372 new_encoder->base.id,
374 new_connector_state->crtc->base.id,
375 new_connector_state->crtc->name);
381 mode_fixup(struct drm_atomic_state *state)
383 struct drm_crtc *crtc;
384 struct drm_crtc_state *new_crtc_state;
385 struct drm_connector *connector;
386 struct drm_connector_state *new_conn_state;
390 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
391 if (!new_crtc_state->mode_changed &&
392 !new_crtc_state->connectors_changed)
395 drm_mode_copy(&new_crtc_state->adjusted_mode, &new_crtc_state->mode);
398 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
399 const struct drm_encoder_helper_funcs *funcs;
400 struct drm_encoder *encoder;
402 WARN_ON(!!new_conn_state->best_encoder != !!new_conn_state->crtc);
404 if (!new_conn_state->crtc || !new_conn_state->best_encoder)
408 drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
411 * Each encoder has at most one connector (since we always steal
412 * it away), so we won't call ->mode_fixup twice.
414 encoder = new_conn_state->best_encoder;
415 funcs = encoder->helper_private;
417 ret = drm_bridge_mode_fixup(encoder->bridge, &new_crtc_state->mode,
418 &new_crtc_state->adjusted_mode);
420 DRM_DEBUG_ATOMIC("Bridge fixup failed\n");
424 if (funcs && funcs->atomic_check) {
425 ret = funcs->atomic_check(encoder, new_crtc_state,
428 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] check failed\n",
429 encoder->base.id, encoder->name);
432 } else if (funcs && funcs->mode_fixup) {
433 ret = funcs->mode_fixup(encoder, &new_crtc_state->mode,
434 &new_crtc_state->adjusted_mode);
436 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] fixup failed\n",
437 encoder->base.id, encoder->name);
443 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
444 const struct drm_crtc_helper_funcs *funcs;
446 if (!new_crtc_state->enable)
449 if (!new_crtc_state->mode_changed &&
450 !new_crtc_state->connectors_changed)
453 funcs = crtc->helper_private;
454 if (!funcs->mode_fixup)
457 ret = funcs->mode_fixup(crtc, &new_crtc_state->mode,
458 &new_crtc_state->adjusted_mode);
460 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] fixup failed\n",
461 crtc->base.id, crtc->name);
469 static enum drm_mode_status mode_valid_path(struct drm_connector *connector,
470 struct drm_encoder *encoder,
471 struct drm_crtc *crtc,
472 struct drm_display_mode *mode)
474 enum drm_mode_status ret;
476 ret = drm_encoder_mode_valid(encoder, mode);
477 if (ret != MODE_OK) {
478 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] mode_valid() failed\n",
479 encoder->base.id, encoder->name);
483 ret = drm_bridge_mode_valid(encoder->bridge, mode);
484 if (ret != MODE_OK) {
485 DRM_DEBUG_ATOMIC("[BRIDGE] mode_valid() failed\n");
489 ret = drm_crtc_mode_valid(crtc, mode);
490 if (ret != MODE_OK) {
491 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] mode_valid() failed\n",
492 crtc->base.id, crtc->name);
500 mode_valid(struct drm_atomic_state *state)
502 struct drm_connector_state *conn_state;
503 struct drm_connector *connector;
506 for_each_new_connector_in_state(state, connector, conn_state, i) {
507 struct drm_encoder *encoder = conn_state->best_encoder;
508 struct drm_crtc *crtc = conn_state->crtc;
509 struct drm_crtc_state *crtc_state;
510 enum drm_mode_status mode_status;
511 struct drm_display_mode *mode;
513 if (!crtc || !encoder)
516 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
519 if (!crtc_state->mode_changed && !crtc_state->connectors_changed)
522 mode = &crtc_state->mode;
524 mode_status = mode_valid_path(connector, encoder, crtc, mode);
525 if (mode_status != MODE_OK)
533 * drm_atomic_helper_check_modeset - validate state object for modeset changes
535 * @state: the driver state object
537 * Check the state object to see if the requested state is physically possible.
538 * This does all the crtc and connector related computations for an atomic
539 * update and adds any additional connectors needed for full modesets. It calls
540 * the various per-object callbacks in the follow order:
542 * 1. &drm_connector_helper_funcs.atomic_best_encoder for determining the new encoder.
543 * 2. &drm_connector_helper_funcs.atomic_check to validate the connector state.
544 * 3. If it's determined a modeset is needed then all connectors on the affected crtc
545 * crtc are added and &drm_connector_helper_funcs.atomic_check is run on them.
546 * 4. &drm_encoder_helper_funcs.mode_valid, &drm_bridge_funcs.mode_valid and
547 * &drm_crtc_helper_funcs.mode_valid are called on the affected components.
548 * 5. &drm_bridge_funcs.mode_fixup is called on all encoder bridges.
549 * 6. &drm_encoder_helper_funcs.atomic_check is called to validate any encoder state.
550 * This function is only called when the encoder will be part of a configured crtc,
551 * it must not be used for implementing connector property validation.
552 * If this function is NULL, &drm_atomic_encoder_helper_funcs.mode_fixup is called
554 * 7. &drm_crtc_helper_funcs.mode_fixup is called last, to fix up the mode with crtc constraints.
556 * &drm_crtc_state.mode_changed is set when the input mode is changed.
557 * &drm_crtc_state.connectors_changed is set when a connector is added or
558 * removed from the crtc. &drm_crtc_state.active_changed is set when
559 * &drm_crtc_state.active changes, which is used for DPMS.
560 * See also: drm_atomic_crtc_needs_modeset()
564 * Drivers which set &drm_crtc_state.mode_changed (e.g. in their
565 * &drm_plane_helper_funcs.atomic_check hooks if a plane update can't be done
566 * without a full modeset) _must_ call this function afterwards after that
567 * change. It is permitted to call this function multiple times for the same
568 * update, e.g. when the &drm_crtc_helper_funcs.atomic_check functions depend
569 * upon the adjusted dotclock for fifo space allocation and watermark
573 * Zero for success or -errno
576 drm_atomic_helper_check_modeset(struct drm_device *dev,
577 struct drm_atomic_state *state)
579 struct drm_crtc *crtc;
580 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
581 struct drm_connector *connector;
582 struct drm_connector_state *old_connector_state, *new_connector_state;
584 unsigned connectors_mask = 0;
586 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
587 bool has_connectors =
588 !!new_crtc_state->connector_mask;
590 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
592 if (!drm_mode_equal(&old_crtc_state->mode, &new_crtc_state->mode)) {
593 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] mode changed\n",
594 crtc->base.id, crtc->name);
595 new_crtc_state->mode_changed = true;
598 if (old_crtc_state->enable != new_crtc_state->enable) {
599 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enable changed\n",
600 crtc->base.id, crtc->name);
603 * For clarity this assignment is done here, but
604 * enable == 0 is only true when there are no
605 * connectors and a NULL mode.
607 * The other way around is true as well. enable != 0
608 * iff connectors are attached and a mode is set.
610 new_crtc_state->mode_changed = true;
611 new_crtc_state->connectors_changed = true;
614 if (old_crtc_state->active != new_crtc_state->active) {
615 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active changed\n",
616 crtc->base.id, crtc->name);
617 new_crtc_state->active_changed = true;
620 if (new_crtc_state->enable != has_connectors) {
621 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled/connectors mismatch\n",
622 crtc->base.id, crtc->name);
628 ret = handle_conflicting_encoders(state, false);
632 for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
633 const struct drm_connector_helper_funcs *funcs = connector->helper_private;
635 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
638 * This only sets crtc->connectors_changed for routing changes,
639 * drivers must set crtc->connectors_changed themselves when
640 * connector properties need to be updated.
642 ret = update_connector_routing(state, connector,
644 new_connector_state);
647 if (old_connector_state->crtc) {
648 new_crtc_state = drm_atomic_get_new_crtc_state(state,
649 old_connector_state->crtc);
650 if (old_connector_state->link_status !=
651 new_connector_state->link_status)
652 new_crtc_state->connectors_changed = true;
655 if (funcs->atomic_check)
656 ret = funcs->atomic_check(connector, new_connector_state);
660 connectors_mask |= BIT(i);
664 * After all the routing has been prepared we need to add in any
665 * connector which is itself unchanged, but who's crtc changes it's
666 * configuration. This must be done before calling mode_fixup in case a
667 * crtc only changed its mode but has the same set of connectors.
669 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
670 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
673 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] needs all connectors, enable: %c, active: %c\n",
674 crtc->base.id, crtc->name,
675 new_crtc_state->enable ? 'y' : 'n',
676 new_crtc_state->active ? 'y' : 'n');
678 ret = drm_atomic_add_affected_connectors(state, crtc);
682 ret = drm_atomic_add_affected_planes(state, crtc);
688 * Iterate over all connectors again, to make sure atomic_check()
689 * has been called on them when a modeset is forced.
691 for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
692 const struct drm_connector_helper_funcs *funcs = connector->helper_private;
694 if (connectors_mask & BIT(i))
697 if (funcs->atomic_check)
698 ret = funcs->atomic_check(connector, new_connector_state);
703 ret = mode_valid(state);
707 return mode_fixup(state);
709 EXPORT_SYMBOL(drm_atomic_helper_check_modeset);
712 * drm_atomic_helper_check_plane_state() - Check plane state for validity
713 * @plane_state: plane state to check
714 * @crtc_state: crtc state to check
715 * @min_scale: minimum @src:@dest scaling factor in 16.16 fixed point
716 * @max_scale: maximum @src:@dest scaling factor in 16.16 fixed point
717 * @can_position: is it legal to position the plane such that it
718 * doesn't cover the entire crtc? This will generally
719 * only be false for primary planes.
720 * @can_update_disabled: can the plane be updated while the crtc
723 * Checks that a desired plane update is valid, and updates various
724 * bits of derived state (clipped coordinates etc.). Drivers that provide
725 * their own plane handling rather than helper-provided implementations may
726 * still wish to call this function to avoid duplication of error checking
730 * Zero if update appears valid, error code on failure
732 int drm_atomic_helper_check_plane_state(struct drm_plane_state *plane_state,
733 const struct drm_crtc_state *crtc_state,
737 bool can_update_disabled)
739 struct drm_framebuffer *fb = plane_state->fb;
740 struct drm_rect *src = &plane_state->src;
741 struct drm_rect *dst = &plane_state->dst;
742 unsigned int rotation = plane_state->rotation;
743 struct drm_rect clip = {};
746 WARN_ON(plane_state->crtc && plane_state->crtc != crtc_state->crtc);
748 *src = drm_plane_state_src(plane_state);
749 *dst = drm_plane_state_dest(plane_state);
752 plane_state->visible = false;
756 /* crtc should only be NULL when disabling (i.e., !fb) */
757 if (WARN_ON(!plane_state->crtc)) {
758 plane_state->visible = false;
762 if (!crtc_state->enable && !can_update_disabled) {
763 DRM_DEBUG_KMS("Cannot update plane of a disabled CRTC.\n");
767 drm_rect_rotate(src, fb->width << 16, fb->height << 16, rotation);
770 hscale = drm_rect_calc_hscale(src, dst, min_scale, max_scale);
771 vscale = drm_rect_calc_vscale(src, dst, min_scale, max_scale);
772 if (hscale < 0 || vscale < 0) {
773 DRM_DEBUG_KMS("Invalid scaling of plane\n");
774 drm_rect_debug_print("src: ", &plane_state->src, true);
775 drm_rect_debug_print("dst: ", &plane_state->dst, false);
779 if (crtc_state->enable)
780 drm_mode_get_hv_timing(&crtc_state->mode, &clip.x2, &clip.y2);
782 plane_state->visible = drm_rect_clip_scaled(src, dst, &clip);
784 drm_rect_rotate_inv(src, fb->width << 16, fb->height << 16, rotation);
786 if (!plane_state->visible)
788 * Plane isn't visible; some drivers can handle this
789 * so we just return success here. Drivers that can't
790 * (including those that use the primary plane helper's
791 * update function) will return an error from their
792 * update_plane handler.
796 if (!can_position && !drm_rect_equals(dst, &clip)) {
797 DRM_DEBUG_KMS("Plane must cover entire CRTC\n");
798 drm_rect_debug_print("dst: ", dst, false);
799 drm_rect_debug_print("clip: ", &clip, false);
805 EXPORT_SYMBOL(drm_atomic_helper_check_plane_state);
808 * drm_atomic_helper_check_planes - validate state object for planes changes
810 * @state: the driver state object
812 * Check the state object to see if the requested state is physically possible.
813 * This does all the plane update related checks using by calling into the
814 * &drm_crtc_helper_funcs.atomic_check and &drm_plane_helper_funcs.atomic_check
815 * hooks provided by the driver.
817 * It also sets &drm_crtc_state.planes_changed to indicate that a crtc has
821 * Zero for success or -errno
824 drm_atomic_helper_check_planes(struct drm_device *dev,
825 struct drm_atomic_state *state)
827 struct drm_crtc *crtc;
828 struct drm_crtc_state *new_crtc_state;
829 struct drm_plane *plane;
830 struct drm_plane_state *new_plane_state, *old_plane_state;
833 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
834 const struct drm_plane_helper_funcs *funcs;
836 WARN_ON(!drm_modeset_is_locked(&plane->mutex));
838 funcs = plane->helper_private;
840 drm_atomic_helper_plane_changed(state, old_plane_state, new_plane_state, plane);
842 if (!funcs || !funcs->atomic_check)
845 ret = funcs->atomic_check(plane, new_plane_state);
847 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic driver check failed\n",
848 plane->base.id, plane->name);
853 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
854 const struct drm_crtc_helper_funcs *funcs;
856 funcs = crtc->helper_private;
858 if (!funcs || !funcs->atomic_check)
861 ret = funcs->atomic_check(crtc, new_crtc_state);
863 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic driver check failed\n",
864 crtc->base.id, crtc->name);
871 EXPORT_SYMBOL(drm_atomic_helper_check_planes);
874 * drm_atomic_helper_check - validate state object
876 * @state: the driver state object
878 * Check the state object to see if the requested state is physically possible.
879 * Only crtcs and planes have check callbacks, so for any additional (global)
880 * checking that a driver needs it can simply wrap that around this function.
881 * Drivers without such needs can directly use this as their
882 * &drm_mode_config_funcs.atomic_check callback.
884 * This just wraps the two parts of the state checking for planes and modeset
885 * state in the default order: First it calls drm_atomic_helper_check_modeset()
886 * and then drm_atomic_helper_check_planes(). The assumption is that the
887 * @drm_plane_helper_funcs.atomic_check and @drm_crtc_helper_funcs.atomic_check
888 * functions depend upon an updated adjusted_mode.clock to e.g. properly compute
891 * Note that zpos normalization will add all enable planes to the state which
892 * might not desired for some drivers.
893 * For example enable/disable of a cursor plane which have fixed zpos value
894 * would trigger all other enabled planes to be forced to the state change.
897 * Zero for success or -errno
899 int drm_atomic_helper_check(struct drm_device *dev,
900 struct drm_atomic_state *state)
904 ret = drm_atomic_helper_check_modeset(dev, state);
908 if (dev->mode_config.normalize_zpos) {
909 ret = drm_atomic_normalize_zpos(dev, state);
914 ret = drm_atomic_helper_check_planes(dev, state);
918 if (state->legacy_cursor_update)
919 state->async_update = !drm_atomic_helper_async_check(dev, state);
923 EXPORT_SYMBOL(drm_atomic_helper_check);
926 disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state)
928 struct drm_connector *connector;
929 struct drm_connector_state *old_conn_state, *new_conn_state;
930 struct drm_crtc *crtc;
931 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
934 for_each_oldnew_connector_in_state(old_state, connector, old_conn_state, new_conn_state, i) {
935 const struct drm_encoder_helper_funcs *funcs;
936 struct drm_encoder *encoder;
938 /* Shut down everything that's in the changeset and currently
939 * still on. So need to check the old, saved state. */
940 if (!old_conn_state->crtc)
943 old_crtc_state = drm_atomic_get_old_crtc_state(old_state, old_conn_state->crtc);
945 if (!old_crtc_state->active ||
946 !drm_atomic_crtc_needs_modeset(old_conn_state->crtc->state))
949 encoder = old_conn_state->best_encoder;
951 /* We shouldn't get this far if we didn't previously have
952 * an encoder.. but WARN_ON() rather than explode.
954 if (WARN_ON(!encoder))
957 funcs = encoder->helper_private;
959 DRM_DEBUG_ATOMIC("disabling [ENCODER:%d:%s]\n",
960 encoder->base.id, encoder->name);
963 * Each encoder has at most one connector (since we always steal
964 * it away), so we won't call disable hooks twice.
966 drm_bridge_disable(encoder->bridge);
968 /* Right function depends upon target state. */
970 if (new_conn_state->crtc && funcs->prepare)
971 funcs->prepare(encoder);
972 else if (funcs->disable)
973 funcs->disable(encoder);
974 else if (funcs->dpms)
975 funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
978 drm_bridge_post_disable(encoder->bridge);
981 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
982 const struct drm_crtc_helper_funcs *funcs;
985 /* Shut down everything that needs a full modeset. */
986 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
989 if (!old_crtc_state->active)
992 funcs = crtc->helper_private;
994 DRM_DEBUG_ATOMIC("disabling [CRTC:%d:%s]\n",
995 crtc->base.id, crtc->name);
998 /* Right function depends upon target state. */
999 if (new_crtc_state->enable && funcs->prepare)
1000 funcs->prepare(crtc);
1001 else if (funcs->atomic_disable)
1002 funcs->atomic_disable(crtc, old_crtc_state);
1003 else if (funcs->disable)
1004 funcs->disable(crtc);
1006 funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
1008 if (!(dev->irq_enabled && dev->num_crtcs))
1011 ret = drm_crtc_vblank_get(crtc);
1012 WARN_ONCE(ret != -EINVAL, "driver forgot to call drm_crtc_vblank_off()\n");
1014 drm_crtc_vblank_put(crtc);
1019 * drm_atomic_helper_update_legacy_modeset_state - update legacy modeset state
1021 * @old_state: atomic state object with old state structures
1023 * This function updates all the various legacy modeset state pointers in
1024 * connectors, encoders and crtcs. It also updates the timestamping constants
1025 * used for precise vblank timestamps by calling
1026 * drm_calc_timestamping_constants().
1028 * Drivers can use this for building their own atomic commit if they don't have
1029 * a pure helper-based modeset implementation.
1031 * Since these updates are not synchronized with lockings, only code paths
1032 * called from &drm_mode_config_helper_funcs.atomic_commit_tail can look at the
1033 * legacy state filled out by this helper. Defacto this means this helper and
1034 * the legacy state pointers are only really useful for transitioning an
1035 * existing driver to the atomic world.
1038 drm_atomic_helper_update_legacy_modeset_state(struct drm_device *dev,
1039 struct drm_atomic_state *old_state)
1041 struct drm_connector *connector;
1042 struct drm_connector_state *old_conn_state, *new_conn_state;
1043 struct drm_crtc *crtc;
1044 struct drm_crtc_state *new_crtc_state;
1047 /* clear out existing links and update dpms */
1048 for_each_oldnew_connector_in_state(old_state, connector, old_conn_state, new_conn_state, i) {
1049 if (connector->encoder) {
1050 WARN_ON(!connector->encoder->crtc);
1052 connector->encoder->crtc = NULL;
1053 connector->encoder = NULL;
1056 crtc = new_conn_state->crtc;
1057 if ((!crtc && old_conn_state->crtc) ||
1058 (crtc && drm_atomic_crtc_needs_modeset(crtc->state))) {
1059 int mode = DRM_MODE_DPMS_OFF;
1061 if (crtc && crtc->state->active)
1062 mode = DRM_MODE_DPMS_ON;
1064 connector->dpms = mode;
1069 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
1070 if (!new_conn_state->crtc)
1073 if (WARN_ON(!new_conn_state->best_encoder))
1076 connector->encoder = new_conn_state->best_encoder;
1077 connector->encoder->crtc = new_conn_state->crtc;
1080 /* set legacy state in the crtc structure */
1081 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
1082 struct drm_plane *primary = crtc->primary;
1083 struct drm_plane_state *new_plane_state;
1085 crtc->mode = new_crtc_state->mode;
1086 crtc->enabled = new_crtc_state->enable;
1089 drm_atomic_get_new_plane_state(old_state, primary);
1091 if (new_plane_state && new_plane_state->crtc == crtc) {
1092 crtc->x = new_plane_state->src_x >> 16;
1093 crtc->y = new_plane_state->src_y >> 16;
1096 if (new_crtc_state->enable)
1097 drm_calc_timestamping_constants(crtc,
1098 &new_crtc_state->adjusted_mode);
1101 EXPORT_SYMBOL(drm_atomic_helper_update_legacy_modeset_state);
1104 crtc_set_mode(struct drm_device *dev, struct drm_atomic_state *old_state)
1106 struct drm_crtc *crtc;
1107 struct drm_crtc_state *new_crtc_state;
1108 struct drm_connector *connector;
1109 struct drm_connector_state *new_conn_state;
1112 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
1113 const struct drm_crtc_helper_funcs *funcs;
1115 if (!new_crtc_state->mode_changed)
1118 funcs = crtc->helper_private;
1120 if (new_crtc_state->enable && funcs->mode_set_nofb) {
1121 DRM_DEBUG_ATOMIC("modeset on [CRTC:%d:%s]\n",
1122 crtc->base.id, crtc->name);
1124 funcs->mode_set_nofb(crtc);
1128 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
1129 const struct drm_encoder_helper_funcs *funcs;
1130 struct drm_encoder *encoder;
1131 struct drm_display_mode *mode, *adjusted_mode;
1133 if (!new_conn_state->best_encoder)
1136 encoder = new_conn_state->best_encoder;
1137 funcs = encoder->helper_private;
1138 new_crtc_state = new_conn_state->crtc->state;
1139 mode = &new_crtc_state->mode;
1140 adjusted_mode = &new_crtc_state->adjusted_mode;
1142 if (!new_crtc_state->mode_changed)
1145 DRM_DEBUG_ATOMIC("modeset on [ENCODER:%d:%s]\n",
1146 encoder->base.id, encoder->name);
1149 * Each encoder has at most one connector (since we always steal
1150 * it away), so we won't call mode_set hooks twice.
1152 if (funcs && funcs->atomic_mode_set) {
1153 funcs->atomic_mode_set(encoder, new_crtc_state,
1155 } else if (funcs && funcs->mode_set) {
1156 funcs->mode_set(encoder, mode, adjusted_mode);
1159 drm_bridge_mode_set(encoder->bridge, mode, adjusted_mode);
1164 * drm_atomic_helper_commit_modeset_disables - modeset commit to disable outputs
1166 * @old_state: atomic state object with old state structures
1168 * This function shuts down all the outputs that need to be shut down and
1169 * prepares them (if required) with the new mode.
1171 * For compatibility with legacy crtc helpers this should be called before
1172 * drm_atomic_helper_commit_planes(), which is what the default commit function
1173 * does. But drivers with different needs can group the modeset commits together
1174 * and do the plane commits at the end. This is useful for drivers doing runtime
1175 * PM since planes updates then only happen when the CRTC is actually enabled.
1177 void drm_atomic_helper_commit_modeset_disables(struct drm_device *dev,
1178 struct drm_atomic_state *old_state)
1180 disable_outputs(dev, old_state);
1182 drm_atomic_helper_update_legacy_modeset_state(dev, old_state);
1184 crtc_set_mode(dev, old_state);
1186 EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_disables);
1188 static void drm_atomic_helper_commit_writebacks(struct drm_device *dev,
1189 struct drm_atomic_state *old_state)
1191 struct drm_connector *connector;
1192 struct drm_connector_state *new_conn_state;
1195 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
1196 const struct drm_connector_helper_funcs *funcs;
1198 funcs = connector->helper_private;
1199 if (!funcs->atomic_commit)
1202 if (new_conn_state->writeback_job && new_conn_state->writeback_job->fb) {
1203 WARN_ON(connector->connector_type != DRM_MODE_CONNECTOR_WRITEBACK);
1204 funcs->atomic_commit(connector, new_conn_state);
1210 * drm_atomic_helper_commit_modeset_enables - modeset commit to enable outputs
1212 * @old_state: atomic state object with old state structures
1214 * This function enables all the outputs with the new configuration which had to
1215 * be turned off for the update.
1217 * For compatibility with legacy crtc helpers this should be called after
1218 * drm_atomic_helper_commit_planes(), which is what the default commit function
1219 * does. But drivers with different needs can group the modeset commits together
1220 * and do the plane commits at the end. This is useful for drivers doing runtime
1221 * PM since planes updates then only happen when the CRTC is actually enabled.
1223 void drm_atomic_helper_commit_modeset_enables(struct drm_device *dev,
1224 struct drm_atomic_state *old_state)
1226 struct drm_crtc *crtc;
1227 struct drm_crtc_state *old_crtc_state;
1228 struct drm_crtc_state *new_crtc_state;
1229 struct drm_connector *connector;
1230 struct drm_connector_state *new_conn_state;
1233 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
1234 const struct drm_crtc_helper_funcs *funcs;
1236 /* Need to filter out CRTCs where only planes change. */
1237 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
1240 if (!new_crtc_state->active)
1243 funcs = crtc->helper_private;
1245 if (new_crtc_state->enable) {
1246 DRM_DEBUG_ATOMIC("enabling [CRTC:%d:%s]\n",
1247 crtc->base.id, crtc->name);
1249 if (funcs->atomic_enable)
1250 funcs->atomic_enable(crtc, old_crtc_state);
1252 funcs->commit(crtc);
1256 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
1257 const struct drm_encoder_helper_funcs *funcs;
1258 struct drm_encoder *encoder;
1260 if (!new_conn_state->best_encoder)
1263 if (!new_conn_state->crtc->state->active ||
1264 !drm_atomic_crtc_needs_modeset(new_conn_state->crtc->state))
1267 encoder = new_conn_state->best_encoder;
1268 funcs = encoder->helper_private;
1270 DRM_DEBUG_ATOMIC("enabling [ENCODER:%d:%s]\n",
1271 encoder->base.id, encoder->name);
1274 * Each encoder has at most one connector (since we always steal
1275 * it away), so we won't call enable hooks twice.
1277 drm_bridge_pre_enable(encoder->bridge);
1281 funcs->enable(encoder);
1282 else if (funcs->commit)
1283 funcs->commit(encoder);
1286 drm_bridge_enable(encoder->bridge);
1289 drm_atomic_helper_commit_writebacks(dev, old_state);
1291 EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_enables);
1294 * drm_atomic_helper_wait_for_fences - wait for fences stashed in plane state
1296 * @state: atomic state object with old state structures
1297 * @pre_swap: If true, do an interruptible wait, and @state is the new state.
1298 * Otherwise @state is the old state.
1300 * For implicit sync, driver should fish the exclusive fence out from the
1301 * incoming fb's and stash it in the drm_plane_state. This is called after
1302 * drm_atomic_helper_swap_state() so it uses the current plane state (and
1303 * just uses the atomic state to find the changed planes)
1305 * Note that @pre_swap is needed since the point where we block for fences moves
1306 * around depending upon whether an atomic commit is blocking or
1307 * non-blocking. For non-blocking commit all waiting needs to happen after
1308 * drm_atomic_helper_swap_state() is called, but for blocking commits we want
1309 * to wait **before** we do anything that can't be easily rolled back. That is
1310 * before we call drm_atomic_helper_swap_state().
1312 * Returns zero if success or < 0 if dma_fence_wait() fails.
1314 int drm_atomic_helper_wait_for_fences(struct drm_device *dev,
1315 struct drm_atomic_state *state,
1318 struct drm_plane *plane;
1319 struct drm_plane_state *new_plane_state;
1322 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
1323 if (!new_plane_state->fence)
1326 WARN_ON(!new_plane_state->fb);
1329 * If waiting for fences pre-swap (ie: nonblock), userspace can
1330 * still interrupt the operation. Instead of blocking until the
1331 * timer expires, make the wait interruptible.
1333 ret = dma_fence_wait(new_plane_state->fence, pre_swap);
1337 dma_fence_put(new_plane_state->fence);
1338 new_plane_state->fence = NULL;
1343 EXPORT_SYMBOL(drm_atomic_helper_wait_for_fences);
1346 * drm_atomic_helper_wait_for_vblanks - wait for vblank on crtcs
1348 * @old_state: atomic state object with old state structures
1350 * Helper to, after atomic commit, wait for vblanks on all effected
1351 * crtcs (ie. before cleaning up old framebuffers using
1352 * drm_atomic_helper_cleanup_planes()). It will only wait on CRTCs where the
1353 * framebuffers have actually changed to optimize for the legacy cursor and
1354 * plane update use-case.
1356 * Drivers using the nonblocking commit tracking support initialized by calling
1357 * drm_atomic_helper_setup_commit() should look at
1358 * drm_atomic_helper_wait_for_flip_done() as an alternative.
1361 drm_atomic_helper_wait_for_vblanks(struct drm_device *dev,
1362 struct drm_atomic_state *old_state)
1364 struct drm_crtc *crtc;
1365 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
1367 unsigned crtc_mask = 0;
1370 * Legacy cursor ioctls are completely unsynced, and userspace
1371 * relies on that (by doing tons of cursor updates).
1373 if (old_state->legacy_cursor_update)
1376 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
1377 if (!new_crtc_state->active)
1380 ret = drm_crtc_vblank_get(crtc);
1384 crtc_mask |= drm_crtc_mask(crtc);
1385 old_state->crtcs[i].last_vblank_count = drm_crtc_vblank_count(crtc);
1388 for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
1389 if (!(crtc_mask & drm_crtc_mask(crtc)))
1392 ret = wait_event_timeout(dev->vblank[i].queue,
1393 old_state->crtcs[i].last_vblank_count !=
1394 drm_crtc_vblank_count(crtc),
1395 msecs_to_jiffies(50));
1397 WARN(!ret, "[CRTC:%d:%s] vblank wait timed out\n",
1398 crtc->base.id, crtc->name);
1400 drm_crtc_vblank_put(crtc);
1403 EXPORT_SYMBOL(drm_atomic_helper_wait_for_vblanks);
1406 * drm_atomic_helper_wait_for_flip_done - wait for all page flips to be done
1408 * @old_state: atomic state object with old state structures
1410 * Helper to, after atomic commit, wait for page flips on all effected
1411 * crtcs (ie. before cleaning up old framebuffers using
1412 * drm_atomic_helper_cleanup_planes()). Compared to
1413 * drm_atomic_helper_wait_for_vblanks() this waits for the completion of on all
1414 * CRTCs, assuming that cursors-only updates are signalling their completion
1415 * immediately (or using a different path).
1417 * This requires that drivers use the nonblocking commit tracking support
1418 * initialized using drm_atomic_helper_setup_commit().
1420 void drm_atomic_helper_wait_for_flip_done(struct drm_device *dev,
1421 struct drm_atomic_state *old_state)
1423 struct drm_crtc_state *new_crtc_state;
1424 struct drm_crtc *crtc;
1427 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
1428 struct drm_crtc_commit *commit = new_crtc_state->commit;
1434 ret = wait_for_completion_timeout(&commit->flip_done, 10 * HZ);
1436 DRM_ERROR("[CRTC:%d:%s] flip_done timed out\n",
1437 crtc->base.id, crtc->name);
1440 EXPORT_SYMBOL(drm_atomic_helper_wait_for_flip_done);
1443 * drm_atomic_helper_commit_tail - commit atomic update to hardware
1444 * @old_state: atomic state object with old state structures
1446 * This is the default implementation for the
1447 * &drm_mode_config_helper_funcs.atomic_commit_tail hook, for drivers
1448 * that do not support runtime_pm or do not need the CRTC to be
1449 * enabled to perform a commit. Otherwise, see
1450 * drm_atomic_helper_commit_tail_rpm().
1452 * Note that the default ordering of how the various stages are called is to
1453 * match the legacy modeset helper library closest.
1455 void drm_atomic_helper_commit_tail(struct drm_atomic_state *old_state)
1457 struct drm_device *dev = old_state->dev;
1459 drm_atomic_helper_commit_modeset_disables(dev, old_state);
1461 drm_atomic_helper_commit_planes(dev, old_state, 0);
1463 drm_atomic_helper_commit_modeset_enables(dev, old_state);
1465 drm_atomic_helper_fake_vblank(old_state);
1467 drm_atomic_helper_commit_hw_done(old_state);
1469 drm_atomic_helper_wait_for_vblanks(dev, old_state);
1471 drm_atomic_helper_cleanup_planes(dev, old_state);
1473 EXPORT_SYMBOL(drm_atomic_helper_commit_tail);
1476 * drm_atomic_helper_commit_tail_rpm - commit atomic update to hardware
1477 * @old_state: new modeset state to be committed
1479 * This is an alternative implementation for the
1480 * &drm_mode_config_helper_funcs.atomic_commit_tail hook, for drivers
1481 * that support runtime_pm or need the CRTC to be enabled to perform a
1482 * commit. Otherwise, one should use the default implementation
1483 * drm_atomic_helper_commit_tail().
1485 void drm_atomic_helper_commit_tail_rpm(struct drm_atomic_state *old_state)
1487 struct drm_device *dev = old_state->dev;
1489 drm_atomic_helper_commit_modeset_disables(dev, old_state);
1491 drm_atomic_helper_commit_modeset_enables(dev, old_state);
1493 drm_atomic_helper_commit_planes(dev, old_state,
1494 DRM_PLANE_COMMIT_ACTIVE_ONLY);
1496 drm_atomic_helper_fake_vblank(old_state);
1498 drm_atomic_helper_commit_hw_done(old_state);
1500 drm_atomic_helper_wait_for_vblanks(dev, old_state);
1502 drm_atomic_helper_cleanup_planes(dev, old_state);
1504 EXPORT_SYMBOL(drm_atomic_helper_commit_tail_rpm);
1506 static void commit_tail(struct drm_atomic_state *old_state)
1508 struct drm_device *dev = old_state->dev;
1509 const struct drm_mode_config_helper_funcs *funcs;
1511 funcs = dev->mode_config.helper_private;
1513 drm_atomic_helper_wait_for_fences(dev, old_state, false);
1515 drm_atomic_helper_wait_for_dependencies(old_state);
1517 if (funcs && funcs->atomic_commit_tail)
1518 funcs->atomic_commit_tail(old_state);
1520 drm_atomic_helper_commit_tail(old_state);
1522 drm_atomic_helper_commit_cleanup_done(old_state);
1524 drm_atomic_state_put(old_state);
1527 static void commit_work(struct work_struct *work)
1529 struct drm_atomic_state *state = container_of(work,
1530 struct drm_atomic_state,
1536 * drm_atomic_helper_async_check - check if state can be commited asynchronously
1538 * @state: the driver state object
1540 * This helper will check if it is possible to commit the state asynchronously.
1541 * Async commits are not supposed to swap the states like normal sync commits
1542 * but just do in-place changes on the current state.
1544 * It will return 0 if the commit can happen in an asynchronous fashion or error
1545 * if not. Note that error just mean it can't be commited asynchronously, if it
1546 * fails the commit should be treated like a normal synchronous commit.
1548 int drm_atomic_helper_async_check(struct drm_device *dev,
1549 struct drm_atomic_state *state)
1551 struct drm_crtc *crtc;
1552 struct drm_crtc_state *crtc_state;
1553 struct drm_plane *plane = NULL;
1554 struct drm_plane_state *old_plane_state = NULL;
1555 struct drm_plane_state *new_plane_state = NULL;
1556 const struct drm_plane_helper_funcs *funcs;
1557 int i, n_planes = 0;
1559 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
1560 if (drm_atomic_crtc_needs_modeset(crtc_state))
1564 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i)
1567 /* FIXME: we support only single plane updates for now */
1571 if (!new_plane_state->crtc ||
1572 old_plane_state->crtc != new_plane_state->crtc)
1575 funcs = plane->helper_private;
1576 if (!funcs->atomic_async_update)
1579 if (new_plane_state->fence)
1583 * Don't do an async update if there is an outstanding commit modifying
1584 * the plane. This prevents our async update's changes from getting
1585 * overridden by a previous synchronous update's state.
1587 if (old_plane_state->commit &&
1588 !try_wait_for_completion(&old_plane_state->commit->hw_done))
1591 return funcs->atomic_async_check(plane, new_plane_state);
1593 EXPORT_SYMBOL(drm_atomic_helper_async_check);
1596 * drm_atomic_helper_async_commit - commit state asynchronously
1598 * @state: the driver state object
1600 * This function commits a state asynchronously, i.e., not vblank
1601 * synchronized. It should be used on a state only when
1602 * drm_atomic_async_check() succeeds. Async commits are not supposed to swap
1603 * the states like normal sync commits, but just do in-place changes on the
1606 void drm_atomic_helper_async_commit(struct drm_device *dev,
1607 struct drm_atomic_state *state)
1609 struct drm_plane *plane;
1610 struct drm_plane_state *plane_state;
1611 const struct drm_plane_helper_funcs *funcs;
1614 for_each_new_plane_in_state(state, plane, plane_state, i) {
1615 funcs = plane->helper_private;
1616 funcs->atomic_async_update(plane, plane_state);
1619 * ->atomic_async_update() is supposed to update the
1620 * plane->state in-place, make sure at least common
1621 * properties have been properly updated.
1623 WARN_ON_ONCE(plane->state->fb != plane_state->fb);
1624 WARN_ON_ONCE(plane->state->crtc_x != plane_state->crtc_x);
1625 WARN_ON_ONCE(plane->state->crtc_y != plane_state->crtc_y);
1626 WARN_ON_ONCE(plane->state->src_x != plane_state->src_x);
1627 WARN_ON_ONCE(plane->state->src_y != plane_state->src_y);
1630 EXPORT_SYMBOL(drm_atomic_helper_async_commit);
1633 * drm_atomic_helper_commit - commit validated state object
1635 * @state: the driver state object
1636 * @nonblock: whether nonblocking behavior is requested.
1638 * This function commits a with drm_atomic_helper_check() pre-validated state
1639 * object. This can still fail when e.g. the framebuffer reservation fails. This
1640 * function implements nonblocking commits, using
1641 * drm_atomic_helper_setup_commit() and related functions.
1643 * Committing the actual hardware state is done through the
1644 * &drm_mode_config_helper_funcs.atomic_commit_tail callback, or it's default
1645 * implementation drm_atomic_helper_commit_tail().
1648 * Zero for success or -errno.
1650 int drm_atomic_helper_commit(struct drm_device *dev,
1651 struct drm_atomic_state *state,
1656 if (state->async_update) {
1657 ret = drm_atomic_helper_prepare_planes(dev, state);
1661 drm_atomic_helper_async_commit(dev, state);
1662 drm_atomic_helper_cleanup_planes(dev, state);
1667 ret = drm_atomic_helper_setup_commit(state, nonblock);
1671 INIT_WORK(&state->commit_work, commit_work);
1673 ret = drm_atomic_helper_prepare_planes(dev, state);
1678 ret = drm_atomic_helper_wait_for_fences(dev, state, true);
1684 * This is the point of no return - everything below never fails except
1685 * when the hw goes bonghits. Which means we can commit the new state on
1686 * the software side now.
1689 ret = drm_atomic_helper_swap_state(state, true);
1694 * Everything below can be run asynchronously without the need to grab
1695 * any modeset locks at all under one condition: It must be guaranteed
1696 * that the asynchronous work has either been cancelled (if the driver
1697 * supports it, which at least requires that the framebuffers get
1698 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
1699 * before the new state gets committed on the software side with
1700 * drm_atomic_helper_swap_state().
1702 * This scheme allows new atomic state updates to be prepared and
1703 * checked in parallel to the asynchronous completion of the previous
1704 * update. Which is important since compositors need to figure out the
1705 * composition of the next frame right after having submitted the
1708 * NOTE: Commit work has multiple phases, first hardware commit, then
1709 * cleanup. We want them to overlap, hence need system_unbound_wq to
1710 * make sure work items don't artifically stall on each another.
1713 drm_atomic_state_get(state);
1715 queue_work(system_unbound_wq, &state->commit_work);
1722 drm_atomic_helper_cleanup_planes(dev, state);
1725 EXPORT_SYMBOL(drm_atomic_helper_commit);
1728 * DOC: implementing nonblocking commit
1730 * Nonblocking atomic commits have to be implemented in the following sequence:
1732 * 1. Run drm_atomic_helper_prepare_planes() first. This is the only function
1733 * which commit needs to call which can fail, so we want to run it first and
1736 * 2. Synchronize with any outstanding nonblocking commit worker threads which
1737 * might be affected the new state update. This can be done by either cancelling
1738 * or flushing the work items, depending upon whether the driver can deal with
1739 * cancelled updates. Note that it is important to ensure that the framebuffer
1740 * cleanup is still done when cancelling.
1742 * Asynchronous workers need to have sufficient parallelism to be able to run
1743 * different atomic commits on different CRTCs in parallel. The simplest way to
1744 * achive this is by running them on the &system_unbound_wq work queue. Note
1745 * that drivers are not required to split up atomic commits and run an
1746 * individual commit in parallel - userspace is supposed to do that if it cares.
1747 * But it might be beneficial to do that for modesets, since those necessarily
1748 * must be done as one global operation, and enabling or disabling a CRTC can
1749 * take a long time. But even that is not required.
1751 * 3. The software state is updated synchronously with
1752 * drm_atomic_helper_swap_state(). Doing this under the protection of all modeset
1753 * locks means concurrent callers never see inconsistent state. And doing this
1754 * while it's guaranteed that no relevant nonblocking worker runs means that
1755 * nonblocking workers do not need grab any locks. Actually they must not grab
1756 * locks, for otherwise the work flushing will deadlock.
1758 * 4. Schedule a work item to do all subsequent steps, using the split-out
1759 * commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and
1760 * then cleaning up the framebuffers after the old framebuffer is no longer
1763 * The above scheme is implemented in the atomic helper libraries in
1764 * drm_atomic_helper_commit() using a bunch of helper functions. See
1765 * drm_atomic_helper_setup_commit() for a starting point.
1768 static int stall_checks(struct drm_crtc *crtc, bool nonblock)
1770 struct drm_crtc_commit *commit, *stall_commit = NULL;
1771 bool completed = true;
1775 spin_lock(&crtc->commit_lock);
1777 list_for_each_entry(commit, &crtc->commit_list, commit_entry) {
1779 completed = try_wait_for_completion(&commit->flip_done);
1780 /* Userspace is not allowed to get ahead of the previous
1781 * commit with nonblocking ones. */
1782 if (!completed && nonblock) {
1783 spin_unlock(&crtc->commit_lock);
1786 } else if (i == 1) {
1787 stall_commit = drm_crtc_commit_get(commit);
1793 spin_unlock(&crtc->commit_lock);
1798 /* We don't want to let commits get ahead of cleanup work too much,
1799 * stalling on 2nd previous commit means triple-buffer won't ever stall.
1801 ret = wait_for_completion_interruptible_timeout(&stall_commit->cleanup_done,
1804 DRM_ERROR("[CRTC:%d:%s] cleanup_done timed out\n",
1805 crtc->base.id, crtc->name);
1807 drm_crtc_commit_put(stall_commit);
1809 return ret < 0 ? ret : 0;
1812 static void release_crtc_commit(struct completion *completion)
1814 struct drm_crtc_commit *commit = container_of(completion,
1818 drm_crtc_commit_put(commit);
1821 static void init_commit(struct drm_crtc_commit *commit, struct drm_crtc *crtc)
1823 init_completion(&commit->flip_done);
1824 init_completion(&commit->hw_done);
1825 init_completion(&commit->cleanup_done);
1826 INIT_LIST_HEAD(&commit->commit_entry);
1827 kref_init(&commit->ref);
1828 commit->crtc = crtc;
1831 static struct drm_crtc_commit *
1832 crtc_or_fake_commit(struct drm_atomic_state *state, struct drm_crtc *crtc)
1835 struct drm_crtc_state *new_crtc_state;
1837 new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
1839 return new_crtc_state->commit;
1842 if (!state->fake_commit) {
1843 state->fake_commit = kzalloc(sizeof(*state->fake_commit), GFP_KERNEL);
1844 if (!state->fake_commit)
1847 init_commit(state->fake_commit, NULL);
1850 return state->fake_commit;
1854 * drm_atomic_helper_setup_commit - setup possibly nonblocking commit
1855 * @state: new modeset state to be committed
1856 * @nonblock: whether nonblocking behavior is requested.
1858 * This function prepares @state to be used by the atomic helper's support for
1859 * nonblocking commits. Drivers using the nonblocking commit infrastructure
1860 * should always call this function from their
1861 * &drm_mode_config_funcs.atomic_commit hook.
1863 * To be able to use this support drivers need to use a few more helper
1864 * functions. drm_atomic_helper_wait_for_dependencies() must be called before
1865 * actually committing the hardware state, and for nonblocking commits this call
1866 * must be placed in the async worker. See also drm_atomic_helper_swap_state()
1867 * and it's stall parameter, for when a driver's commit hooks look at the
1868 * &drm_crtc.state, &drm_plane.state or &drm_connector.state pointer directly.
1870 * Completion of the hardware commit step must be signalled using
1871 * drm_atomic_helper_commit_hw_done(). After this step the driver is not allowed
1872 * to read or change any permanent software or hardware modeset state. The only
1873 * exception is state protected by other means than &drm_modeset_lock locks.
1874 * Only the free standing @state with pointers to the old state structures can
1875 * be inspected, e.g. to clean up old buffers using
1876 * drm_atomic_helper_cleanup_planes().
1878 * At the very end, before cleaning up @state drivers must call
1879 * drm_atomic_helper_commit_cleanup_done().
1881 * This is all implemented by in drm_atomic_helper_commit(), giving drivers a
1882 * complete and easy-to-use default implementation of the atomic_commit() hook.
1884 * The tracking of asynchronously executed and still pending commits is done
1885 * using the core structure &drm_crtc_commit.
1887 * By default there's no need to clean up resources allocated by this function
1888 * explicitly: drm_atomic_state_default_clear() will take care of that
1893 * 0 on success. -EBUSY when userspace schedules nonblocking commits too fast,
1894 * -ENOMEM on allocation failures and -EINTR when a signal is pending.
1896 int drm_atomic_helper_setup_commit(struct drm_atomic_state *state,
1899 struct drm_crtc *crtc;
1900 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
1901 struct drm_connector *conn;
1902 struct drm_connector_state *old_conn_state, *new_conn_state;
1903 struct drm_plane *plane;
1904 struct drm_plane_state *old_plane_state, *new_plane_state;
1905 struct drm_crtc_commit *commit;
1908 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
1909 commit = kzalloc(sizeof(*commit), GFP_KERNEL);
1913 init_commit(commit, crtc);
1915 new_crtc_state->commit = commit;
1917 ret = stall_checks(crtc, nonblock);
1921 /* Drivers only send out events when at least either current or
1922 * new CRTC state is active. Complete right away if everything
1924 if (!old_crtc_state->active && !new_crtc_state->active) {
1925 complete_all(&commit->flip_done);
1929 /* Legacy cursor updates are fully unsynced. */
1930 if (state->legacy_cursor_update) {
1931 complete_all(&commit->flip_done);
1935 if (!new_crtc_state->event) {
1936 commit->event = kzalloc(sizeof(*commit->event),
1941 new_crtc_state->event = commit->event;
1944 new_crtc_state->event->base.completion = &commit->flip_done;
1945 new_crtc_state->event->base.completion_release = release_crtc_commit;
1946 drm_crtc_commit_get(commit);
1948 commit->abort_completion = true;
1951 for_each_oldnew_connector_in_state(state, conn, old_conn_state, new_conn_state, i) {
1952 /* Userspace is not allowed to get ahead of the previous
1953 * commit with nonblocking ones. */
1954 if (nonblock && old_conn_state->commit &&
1955 !try_wait_for_completion(&old_conn_state->commit->flip_done))
1958 /* Always track connectors explicitly for e.g. link retraining. */
1959 commit = crtc_or_fake_commit(state, new_conn_state->crtc ?: old_conn_state->crtc);
1963 new_conn_state->commit = drm_crtc_commit_get(commit);
1966 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
1967 /* Userspace is not allowed to get ahead of the previous
1968 * commit with nonblocking ones. */
1969 if (nonblock && old_plane_state->commit &&
1970 !try_wait_for_completion(&old_plane_state->commit->flip_done))
1973 /* Always track planes explicitly for async pageflip support. */
1974 commit = crtc_or_fake_commit(state, new_plane_state->crtc ?: old_plane_state->crtc);
1978 new_plane_state->commit = drm_crtc_commit_get(commit);
1983 EXPORT_SYMBOL(drm_atomic_helper_setup_commit);
1986 * drm_atomic_helper_wait_for_dependencies - wait for required preceeding commits
1987 * @old_state: atomic state object with old state structures
1989 * This function waits for all preceeding commits that touch the same CRTC as
1990 * @old_state to both be committed to the hardware (as signalled by
1991 * drm_atomic_helper_commit_hw_done) and executed by the hardware (as signalled
1992 * by calling drm_crtc_send_vblank_event() on the &drm_crtc_state.event).
1994 * This is part of the atomic helper support for nonblocking commits, see
1995 * drm_atomic_helper_setup_commit() for an overview.
1997 void drm_atomic_helper_wait_for_dependencies(struct drm_atomic_state *old_state)
1999 struct drm_crtc *crtc;
2000 struct drm_crtc_state *old_crtc_state;
2001 struct drm_plane *plane;
2002 struct drm_plane_state *old_plane_state;
2003 struct drm_connector *conn;
2004 struct drm_connector_state *old_conn_state;
2005 struct drm_crtc_commit *commit;
2009 for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
2010 commit = old_crtc_state->commit;
2015 ret = wait_for_completion_timeout(&commit->hw_done,
2018 DRM_ERROR("[CRTC:%d:%s] hw_done timed out\n",
2019 crtc->base.id, crtc->name);
2021 /* Currently no support for overwriting flips, hence
2022 * stall for previous one to execute completely. */
2023 ret = wait_for_completion_timeout(&commit->flip_done,
2026 DRM_ERROR("[CRTC:%d:%s] flip_done timed out\n",
2027 crtc->base.id, crtc->name);
2030 for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
2031 commit = old_conn_state->commit;
2036 ret = wait_for_completion_timeout(&commit->hw_done,
2039 DRM_ERROR("[CONNECTOR:%d:%s] hw_done timed out\n",
2040 conn->base.id, conn->name);
2042 /* Currently no support for overwriting flips, hence
2043 * stall for previous one to execute completely. */
2044 ret = wait_for_completion_timeout(&commit->flip_done,
2047 DRM_ERROR("[CONNECTOR:%d:%s] flip_done timed out\n",
2048 conn->base.id, conn->name);
2051 for_each_old_plane_in_state(old_state, plane, old_plane_state, i) {
2052 commit = old_plane_state->commit;
2057 ret = wait_for_completion_timeout(&commit->hw_done,
2060 DRM_ERROR("[PLANE:%d:%s] hw_done timed out\n",
2061 plane->base.id, plane->name);
2063 /* Currently no support for overwriting flips, hence
2064 * stall for previous one to execute completely. */
2065 ret = wait_for_completion_timeout(&commit->flip_done,
2068 DRM_ERROR("[PLANE:%d:%s] flip_done timed out\n",
2069 plane->base.id, plane->name);
2072 EXPORT_SYMBOL(drm_atomic_helper_wait_for_dependencies);
2075 * drm_atomic_helper_fake_vblank - fake VBLANK events if needed
2076 * @old_state: atomic state object with old state structures
2078 * This function walks all CRTCs and fake VBLANK events on those with
2079 * &drm_crtc_state.no_vblank set to true and &drm_crtc_state.event != NULL.
2080 * The primary use of this function is writeback connectors working in oneshot
2081 * mode and faking VBLANK events. In this case they only fake the VBLANK event
2082 * when a job is queued, and any change to the pipeline that does not touch the
2083 * connector is leading to timeouts when calling
2084 * drm_atomic_helper_wait_for_vblanks() or
2085 * drm_atomic_helper_wait_for_flip_done().
2087 * This is part of the atomic helper support for nonblocking commits, see
2088 * drm_atomic_helper_setup_commit() for an overview.
2090 void drm_atomic_helper_fake_vblank(struct drm_atomic_state *old_state)
2092 struct drm_crtc_state *new_crtc_state;
2093 struct drm_crtc *crtc;
2096 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
2097 unsigned long flags;
2099 if (!new_crtc_state->no_vblank)
2102 spin_lock_irqsave(&old_state->dev->event_lock, flags);
2103 if (new_crtc_state->event) {
2104 drm_crtc_send_vblank_event(crtc,
2105 new_crtc_state->event);
2106 new_crtc_state->event = NULL;
2108 spin_unlock_irqrestore(&old_state->dev->event_lock, flags);
2111 EXPORT_SYMBOL(drm_atomic_helper_fake_vblank);
2114 * drm_atomic_helper_commit_hw_done - setup possible nonblocking commit
2115 * @old_state: atomic state object with old state structures
2117 * This function is used to signal completion of the hardware commit step. After
2118 * this step the driver is not allowed to read or change any permanent software
2119 * or hardware modeset state. The only exception is state protected by other
2120 * means than &drm_modeset_lock locks.
2122 * Drivers should try to postpone any expensive or delayed cleanup work after
2123 * this function is called.
2125 * This is part of the atomic helper support for nonblocking commits, see
2126 * drm_atomic_helper_setup_commit() for an overview.
2128 void drm_atomic_helper_commit_hw_done(struct drm_atomic_state *old_state)
2130 struct drm_crtc *crtc;
2131 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
2132 struct drm_crtc_commit *commit;
2135 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
2136 commit = new_crtc_state->commit;
2141 * copy new_crtc_state->commit to old_crtc_state->commit,
2142 * it's unsafe to touch new_crtc_state after hw_done,
2143 * but we still need to do so in cleanup_done().
2145 if (old_crtc_state->commit)
2146 drm_crtc_commit_put(old_crtc_state->commit);
2148 old_crtc_state->commit = drm_crtc_commit_get(commit);
2150 /* backend must have consumed any event by now */
2151 WARN_ON(new_crtc_state->event);
2152 complete_all(&commit->hw_done);
2155 if (old_state->fake_commit) {
2156 complete_all(&old_state->fake_commit->hw_done);
2157 complete_all(&old_state->fake_commit->flip_done);
2160 EXPORT_SYMBOL(drm_atomic_helper_commit_hw_done);
2163 * drm_atomic_helper_commit_cleanup_done - signal completion of commit
2164 * @old_state: atomic state object with old state structures
2166 * This signals completion of the atomic update @old_state, including any
2167 * cleanup work. If used, it must be called right before calling
2168 * drm_atomic_state_put().
2170 * This is part of the atomic helper support for nonblocking commits, see
2171 * drm_atomic_helper_setup_commit() for an overview.
2173 void drm_atomic_helper_commit_cleanup_done(struct drm_atomic_state *old_state)
2175 struct drm_crtc *crtc;
2176 struct drm_crtc_state *old_crtc_state;
2177 struct drm_crtc_commit *commit;
2180 for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
2181 commit = old_crtc_state->commit;
2182 if (WARN_ON(!commit))
2185 complete_all(&commit->cleanup_done);
2186 WARN_ON(!try_wait_for_completion(&commit->hw_done));
2188 spin_lock(&crtc->commit_lock);
2189 list_del(&commit->commit_entry);
2190 spin_unlock(&crtc->commit_lock);
2193 if (old_state->fake_commit)
2194 complete_all(&old_state->fake_commit->cleanup_done);
2196 EXPORT_SYMBOL(drm_atomic_helper_commit_cleanup_done);
2199 * drm_atomic_helper_prepare_planes - prepare plane resources before commit
2201 * @state: atomic state object with new state structures
2203 * This function prepares plane state, specifically framebuffers, for the new
2204 * configuration, by calling &drm_plane_helper_funcs.prepare_fb. If any failure
2205 * is encountered this function will call &drm_plane_helper_funcs.cleanup_fb on
2206 * any already successfully prepared framebuffer.
2209 * 0 on success, negative error code on failure.
2211 int drm_atomic_helper_prepare_planes(struct drm_device *dev,
2212 struct drm_atomic_state *state)
2214 struct drm_plane *plane;
2215 struct drm_plane_state *new_plane_state;
2218 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
2219 const struct drm_plane_helper_funcs *funcs;
2221 funcs = plane->helper_private;
2223 if (funcs->prepare_fb) {
2224 ret = funcs->prepare_fb(plane, new_plane_state);
2233 for_each_new_plane_in_state(state, plane, new_plane_state, j) {
2234 const struct drm_plane_helper_funcs *funcs;
2239 funcs = plane->helper_private;
2241 if (funcs->cleanup_fb)
2242 funcs->cleanup_fb(plane, new_plane_state);
2247 EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
2249 static bool plane_crtc_active(const struct drm_plane_state *state)
2251 return state->crtc && state->crtc->state->active;
2255 * drm_atomic_helper_commit_planes - commit plane state
2257 * @old_state: atomic state object with old state structures
2258 * @flags: flags for committing plane state
2260 * This function commits the new plane state using the plane and atomic helper
2261 * functions for planes and crtcs. It assumes that the atomic state has already
2262 * been pushed into the relevant object state pointers, since this step can no
2265 * It still requires the global state object @old_state to know which planes and
2266 * crtcs need to be updated though.
2268 * Note that this function does all plane updates across all CRTCs in one step.
2269 * If the hardware can't support this approach look at
2270 * drm_atomic_helper_commit_planes_on_crtc() instead.
2272 * Plane parameters can be updated by applications while the associated CRTC is
2273 * disabled. The DRM/KMS core will store the parameters in the plane state,
2274 * which will be available to the driver when the CRTC is turned on. As a result
2275 * most drivers don't need to be immediately notified of plane updates for a
2278 * Unless otherwise needed, drivers are advised to set the ACTIVE_ONLY flag in
2279 * @flags in order not to receive plane update notifications related to a
2280 * disabled CRTC. This avoids the need to manually ignore plane updates in
2281 * driver code when the driver and/or hardware can't or just don't need to deal
2282 * with updates on disabled CRTCs, for example when supporting runtime PM.
2284 * Drivers may set the NO_DISABLE_AFTER_MODESET flag in @flags if the relevant
2285 * display controllers require to disable a CRTC's planes when the CRTC is
2286 * disabled. This function would skip the &drm_plane_helper_funcs.atomic_disable
2287 * call for a plane if the CRTC of the old plane state needs a modesetting
2288 * operation. Of course, the drivers need to disable the planes in their CRTC
2289 * disable callbacks since no one else would do that.
2291 * The drm_atomic_helper_commit() default implementation doesn't set the
2292 * ACTIVE_ONLY flag to most closely match the behaviour of the legacy helpers.
2293 * This should not be copied blindly by drivers.
2295 void drm_atomic_helper_commit_planes(struct drm_device *dev,
2296 struct drm_atomic_state *old_state,
2299 struct drm_crtc *crtc;
2300 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
2301 struct drm_plane *plane;
2302 struct drm_plane_state *old_plane_state, *new_plane_state;
2304 bool active_only = flags & DRM_PLANE_COMMIT_ACTIVE_ONLY;
2305 bool no_disable = flags & DRM_PLANE_COMMIT_NO_DISABLE_AFTER_MODESET;
2307 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
2308 const struct drm_crtc_helper_funcs *funcs;
2310 funcs = crtc->helper_private;
2312 if (!funcs || !funcs->atomic_begin)
2315 if (active_only && !new_crtc_state->active)
2318 funcs->atomic_begin(crtc, old_crtc_state);
2321 for_each_oldnew_plane_in_state(old_state, plane, old_plane_state, new_plane_state, i) {
2322 const struct drm_plane_helper_funcs *funcs;
2325 funcs = plane->helper_private;
2330 disabling = drm_atomic_plane_disabling(old_plane_state,
2335 * Skip planes related to inactive CRTCs. If the plane
2336 * is enabled use the state of the current CRTC. If the
2337 * plane is being disabled use the state of the old
2338 * CRTC to avoid skipping planes being disabled on an
2341 if (!disabling && !plane_crtc_active(new_plane_state))
2343 if (disabling && !plane_crtc_active(old_plane_state))
2348 * Special-case disabling the plane if drivers support it.
2350 if (disabling && funcs->atomic_disable) {
2351 struct drm_crtc_state *crtc_state;
2353 crtc_state = old_plane_state->crtc->state;
2355 if (drm_atomic_crtc_needs_modeset(crtc_state) &&
2359 funcs->atomic_disable(plane, old_plane_state);
2360 } else if (new_plane_state->crtc || disabling) {
2361 funcs->atomic_update(plane, old_plane_state);
2365 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
2366 const struct drm_crtc_helper_funcs *funcs;
2368 funcs = crtc->helper_private;
2370 if (!funcs || !funcs->atomic_flush)
2373 if (active_only && !new_crtc_state->active)
2376 funcs->atomic_flush(crtc, old_crtc_state);
2379 EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
2382 * drm_atomic_helper_commit_planes_on_crtc - commit plane state for a crtc
2383 * @old_crtc_state: atomic state object with the old crtc state
2385 * This function commits the new plane state using the plane and atomic helper
2386 * functions for planes on the specific crtc. It assumes that the atomic state
2387 * has already been pushed into the relevant object state pointers, since this
2388 * step can no longer fail.
2390 * This function is useful when plane updates should be done crtc-by-crtc
2391 * instead of one global step like drm_atomic_helper_commit_planes() does.
2393 * This function can only be savely used when planes are not allowed to move
2394 * between different CRTCs because this function doesn't handle inter-CRTC
2395 * depencies. Callers need to ensure that either no such depencies exist,
2396 * resolve them through ordering of commit calls or through some other means.
2399 drm_atomic_helper_commit_planes_on_crtc(struct drm_crtc_state *old_crtc_state)
2401 const struct drm_crtc_helper_funcs *crtc_funcs;
2402 struct drm_crtc *crtc = old_crtc_state->crtc;
2403 struct drm_atomic_state *old_state = old_crtc_state->state;
2404 struct drm_crtc_state *new_crtc_state =
2405 drm_atomic_get_new_crtc_state(old_state, crtc);
2406 struct drm_plane *plane;
2407 unsigned plane_mask;
2409 plane_mask = old_crtc_state->plane_mask;
2410 plane_mask |= new_crtc_state->plane_mask;
2412 crtc_funcs = crtc->helper_private;
2413 if (crtc_funcs && crtc_funcs->atomic_begin)
2414 crtc_funcs->atomic_begin(crtc, old_crtc_state);
2416 drm_for_each_plane_mask(plane, crtc->dev, plane_mask) {
2417 struct drm_plane_state *old_plane_state =
2418 drm_atomic_get_old_plane_state(old_state, plane);
2419 struct drm_plane_state *new_plane_state =
2420 drm_atomic_get_new_plane_state(old_state, plane);
2421 const struct drm_plane_helper_funcs *plane_funcs;
2423 plane_funcs = plane->helper_private;
2425 if (!old_plane_state || !plane_funcs)
2428 WARN_ON(new_plane_state->crtc &&
2429 new_plane_state->crtc != crtc);
2431 if (drm_atomic_plane_disabling(old_plane_state, new_plane_state) &&
2432 plane_funcs->atomic_disable)
2433 plane_funcs->atomic_disable(plane, old_plane_state);
2434 else if (new_plane_state->crtc ||
2435 drm_atomic_plane_disabling(old_plane_state, new_plane_state))
2436 plane_funcs->atomic_update(plane, old_plane_state);
2439 if (crtc_funcs && crtc_funcs->atomic_flush)
2440 crtc_funcs->atomic_flush(crtc, old_crtc_state);
2442 EXPORT_SYMBOL(drm_atomic_helper_commit_planes_on_crtc);
2445 * drm_atomic_helper_disable_planes_on_crtc - helper to disable CRTC's planes
2446 * @old_crtc_state: atomic state object with the old CRTC state
2447 * @atomic: if set, synchronize with CRTC's atomic_begin/flush hooks
2449 * Disables all planes associated with the given CRTC. This can be
2450 * used for instance in the CRTC helper atomic_disable callback to disable
2453 * If the atomic-parameter is set the function calls the CRTC's
2454 * atomic_begin hook before and atomic_flush hook after disabling the
2457 * It is a bug to call this function without having implemented the
2458 * &drm_plane_helper_funcs.atomic_disable plane hook.
2461 drm_atomic_helper_disable_planes_on_crtc(struct drm_crtc_state *old_crtc_state,
2464 struct drm_crtc *crtc = old_crtc_state->crtc;
2465 const struct drm_crtc_helper_funcs *crtc_funcs =
2466 crtc->helper_private;
2467 struct drm_plane *plane;
2469 if (atomic && crtc_funcs && crtc_funcs->atomic_begin)
2470 crtc_funcs->atomic_begin(crtc, NULL);
2472 drm_atomic_crtc_state_for_each_plane(plane, old_crtc_state) {
2473 const struct drm_plane_helper_funcs *plane_funcs =
2474 plane->helper_private;
2479 WARN_ON(!plane_funcs->atomic_disable);
2480 if (plane_funcs->atomic_disable)
2481 plane_funcs->atomic_disable(plane, NULL);
2484 if (atomic && crtc_funcs && crtc_funcs->atomic_flush)
2485 crtc_funcs->atomic_flush(crtc, NULL);
2487 EXPORT_SYMBOL(drm_atomic_helper_disable_planes_on_crtc);
2490 * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
2492 * @old_state: atomic state object with old state structures
2494 * This function cleans up plane state, specifically framebuffers, from the old
2495 * configuration. Hence the old configuration must be perserved in @old_state to
2496 * be able to call this function.
2498 * This function must also be called on the new state when the atomic update
2499 * fails at any point after calling drm_atomic_helper_prepare_planes().
2501 void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
2502 struct drm_atomic_state *old_state)
2504 struct drm_plane *plane;
2505 struct drm_plane_state *old_plane_state, *new_plane_state;
2508 for_each_oldnew_plane_in_state(old_state, plane, old_plane_state, new_plane_state, i) {
2509 const struct drm_plane_helper_funcs *funcs;
2510 struct drm_plane_state *plane_state;
2513 * This might be called before swapping when commit is aborted,
2514 * in which case we have to cleanup the new state.
2516 if (old_plane_state == plane->state)
2517 plane_state = new_plane_state;
2519 plane_state = old_plane_state;
2521 funcs = plane->helper_private;
2523 if (funcs->cleanup_fb)
2524 funcs->cleanup_fb(plane, plane_state);
2527 EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
2530 * drm_atomic_helper_swap_state - store atomic state into current sw state
2531 * @state: atomic state
2532 * @stall: stall for preceeding commits
2534 * This function stores the atomic state into the current state pointers in all
2535 * driver objects. It should be called after all failing steps have been done
2536 * and succeeded, but before the actual hardware state is committed.
2538 * For cleanup and error recovery the current state for all changed objects will
2539 * be swapped into @state.
2541 * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
2543 * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
2545 * 2. Do any other steps that might fail.
2547 * 3. Put the staged state into the current state pointers with this function.
2549 * 4. Actually commit the hardware state.
2551 * 5. Call drm_atomic_helper_cleanup_planes() with @state, which since step 3
2552 * contains the old state. Also do any other cleanup required with that state.
2554 * @stall must be set when nonblocking commits for this driver directly access
2555 * the &drm_plane.state, &drm_crtc.state or &drm_connector.state pointer. With
2556 * the current atomic helpers this is almost always the case, since the helpers
2557 * don't pass the right state structures to the callbacks.
2561 * Returns 0 on success. Can return -ERESTARTSYS when @stall is true and the
2562 * waiting for the previous commits has been interrupted.
2564 int drm_atomic_helper_swap_state(struct drm_atomic_state *state,
2568 struct drm_connector *connector;
2569 struct drm_connector_state *old_conn_state, *new_conn_state;
2570 struct drm_crtc *crtc;
2571 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
2572 struct drm_plane *plane;
2573 struct drm_plane_state *old_plane_state, *new_plane_state;
2574 struct drm_crtc_commit *commit;
2575 struct drm_private_obj *obj;
2576 struct drm_private_state *old_obj_state, *new_obj_state;
2580 * We have to stall for hw_done here before
2581 * drm_atomic_helper_wait_for_dependencies() because flip
2582 * depth > 1 is not yet supported by all drivers. As long as
2583 * obj->state is directly dereferenced anywhere in the drivers
2584 * atomic_commit_tail function, then it's unsafe to swap state
2585 * before drm_atomic_helper_commit_hw_done() is called.
2588 for_each_old_crtc_in_state(state, crtc, old_crtc_state, i) {
2589 commit = old_crtc_state->commit;
2594 ret = wait_for_completion_interruptible(&commit->hw_done);
2599 for_each_old_connector_in_state(state, connector, old_conn_state, i) {
2600 commit = old_conn_state->commit;
2605 ret = wait_for_completion_interruptible(&commit->hw_done);
2610 for_each_old_plane_in_state(state, plane, old_plane_state, i) {
2611 commit = old_plane_state->commit;
2616 ret = wait_for_completion_interruptible(&commit->hw_done);
2622 for_each_oldnew_connector_in_state(state, connector, old_conn_state, new_conn_state, i) {
2623 WARN_ON(connector->state != old_conn_state);
2625 old_conn_state->state = state;
2626 new_conn_state->state = NULL;
2628 state->connectors[i].state = old_conn_state;
2629 connector->state = new_conn_state;
2632 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
2633 WARN_ON(crtc->state != old_crtc_state);
2635 old_crtc_state->state = state;
2636 new_crtc_state->state = NULL;
2638 state->crtcs[i].state = old_crtc_state;
2639 crtc->state = new_crtc_state;
2641 if (new_crtc_state->commit) {
2642 spin_lock(&crtc->commit_lock);
2643 list_add(&new_crtc_state->commit->commit_entry,
2644 &crtc->commit_list);
2645 spin_unlock(&crtc->commit_lock);
2647 new_crtc_state->commit->event = NULL;
2651 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
2652 WARN_ON(plane->state != old_plane_state);
2654 old_plane_state->state = state;
2655 new_plane_state->state = NULL;
2657 state->planes[i].state = old_plane_state;
2658 plane->state = new_plane_state;
2661 for_each_oldnew_private_obj_in_state(state, obj, old_obj_state, new_obj_state, i) {
2662 WARN_ON(obj->state != old_obj_state);
2664 old_obj_state->state = state;
2665 new_obj_state->state = NULL;
2667 state->private_objs[i].state = old_obj_state;
2668 obj->state = new_obj_state;
2673 EXPORT_SYMBOL(drm_atomic_helper_swap_state);
2676 * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
2677 * @plane: plane object to update
2678 * @crtc: owning CRTC of owning plane
2679 * @fb: framebuffer to flip onto plane
2680 * @crtc_x: x offset of primary plane on crtc
2681 * @crtc_y: y offset of primary plane on crtc
2682 * @crtc_w: width of primary plane rectangle on crtc
2683 * @crtc_h: height of primary plane rectangle on crtc
2684 * @src_x: x offset of @fb for panning
2685 * @src_y: y offset of @fb for panning
2686 * @src_w: width of source rectangle in @fb
2687 * @src_h: height of source rectangle in @fb
2688 * @ctx: lock acquire context
2690 * Provides a default plane update handler using the atomic driver interface.
2693 * Zero on success, error code on failure
2695 int drm_atomic_helper_update_plane(struct drm_plane *plane,
2696 struct drm_crtc *crtc,
2697 struct drm_framebuffer *fb,
2698 int crtc_x, int crtc_y,
2699 unsigned int crtc_w, unsigned int crtc_h,
2700 uint32_t src_x, uint32_t src_y,
2701 uint32_t src_w, uint32_t src_h,
2702 struct drm_modeset_acquire_ctx *ctx)
2704 struct drm_atomic_state *state;
2705 struct drm_plane_state *plane_state;
2708 state = drm_atomic_state_alloc(plane->dev);
2712 state->acquire_ctx = ctx;
2713 plane_state = drm_atomic_get_plane_state(state, plane);
2714 if (IS_ERR(plane_state)) {
2715 ret = PTR_ERR(plane_state);
2719 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
2722 drm_atomic_set_fb_for_plane(plane_state, fb);
2723 plane_state->crtc_x = crtc_x;
2724 plane_state->crtc_y = crtc_y;
2725 plane_state->crtc_w = crtc_w;
2726 plane_state->crtc_h = crtc_h;
2727 plane_state->src_x = src_x;
2728 plane_state->src_y = src_y;
2729 plane_state->src_w = src_w;
2730 plane_state->src_h = src_h;
2732 if (plane == crtc->cursor)
2733 state->legacy_cursor_update = true;
2735 ret = drm_atomic_commit(state);
2737 drm_atomic_state_put(state);
2740 EXPORT_SYMBOL(drm_atomic_helper_update_plane);
2743 * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
2744 * @plane: plane to disable
2745 * @ctx: lock acquire context
2747 * Provides a default plane disable handler using the atomic driver interface.
2750 * Zero on success, error code on failure
2752 int drm_atomic_helper_disable_plane(struct drm_plane *plane,
2753 struct drm_modeset_acquire_ctx *ctx)
2755 struct drm_atomic_state *state;
2756 struct drm_plane_state *plane_state;
2759 state = drm_atomic_state_alloc(plane->dev);
2763 state->acquire_ctx = ctx;
2764 plane_state = drm_atomic_get_plane_state(state, plane);
2765 if (IS_ERR(plane_state)) {
2766 ret = PTR_ERR(plane_state);
2770 if (plane_state->crtc && plane_state->crtc->cursor == plane)
2771 plane_state->state->legacy_cursor_update = true;
2773 ret = __drm_atomic_helper_disable_plane(plane, plane_state);
2777 ret = drm_atomic_commit(state);
2779 drm_atomic_state_put(state);
2782 EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
2784 /* just used from fb-helper and atomic-helper: */
2785 int __drm_atomic_helper_disable_plane(struct drm_plane *plane,
2786 struct drm_plane_state *plane_state)
2790 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
2794 drm_atomic_set_fb_for_plane(plane_state, NULL);
2795 plane_state->crtc_x = 0;
2796 plane_state->crtc_y = 0;
2797 plane_state->crtc_w = 0;
2798 plane_state->crtc_h = 0;
2799 plane_state->src_x = 0;
2800 plane_state->src_y = 0;
2801 plane_state->src_w = 0;
2802 plane_state->src_h = 0;
2807 static int update_output_state(struct drm_atomic_state *state,
2808 struct drm_mode_set *set)
2810 struct drm_device *dev = set->crtc->dev;
2811 struct drm_crtc *crtc;
2812 struct drm_crtc_state *new_crtc_state;
2813 struct drm_connector *connector;
2814 struct drm_connector_state *new_conn_state;
2817 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
2818 state->acquire_ctx);
2822 /* First disable all connectors on the target crtc. */
2823 ret = drm_atomic_add_affected_connectors(state, set->crtc);
2827 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
2828 if (new_conn_state->crtc == set->crtc) {
2829 ret = drm_atomic_set_crtc_for_connector(new_conn_state,
2834 /* Make sure legacy setCrtc always re-trains */
2835 new_conn_state->link_status = DRM_LINK_STATUS_GOOD;
2839 /* Then set all connectors from set->connectors on the target crtc */
2840 for (i = 0; i < set->num_connectors; i++) {
2841 new_conn_state = drm_atomic_get_connector_state(state,
2842 set->connectors[i]);
2843 if (IS_ERR(new_conn_state))
2844 return PTR_ERR(new_conn_state);
2846 ret = drm_atomic_set_crtc_for_connector(new_conn_state,
2852 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
2853 /* Don't update ->enable for the CRTC in the set_config request,
2854 * since a mismatch would indicate a bug in the upper layers.
2855 * The actual modeset code later on will catch any
2856 * inconsistencies here. */
2857 if (crtc == set->crtc)
2860 if (!new_crtc_state->connector_mask) {
2861 ret = drm_atomic_set_mode_prop_for_crtc(new_crtc_state,
2866 new_crtc_state->active = false;
2874 * drm_atomic_helper_set_config - set a new config from userspace
2875 * @set: mode set configuration
2876 * @ctx: lock acquisition context
2878 * Provides a default crtc set_config handler using the atomic driver interface.
2880 * NOTE: For backwards compatibility with old userspace this automatically
2881 * resets the "link-status" property to GOOD, to force any link
2882 * re-training. The SETCRTC ioctl does not define whether an update does
2883 * need a full modeset or just a plane update, hence we're allowed to do
2884 * that. See also drm_connector_set_link_status_property().
2887 * Returns 0 on success, negative errno numbers on failure.
2889 int drm_atomic_helper_set_config(struct drm_mode_set *set,
2890 struct drm_modeset_acquire_ctx *ctx)
2892 struct drm_atomic_state *state;
2893 struct drm_crtc *crtc = set->crtc;
2896 state = drm_atomic_state_alloc(crtc->dev);
2900 state->acquire_ctx = ctx;
2901 ret = __drm_atomic_helper_set_config(set, state);
2905 ret = handle_conflicting_encoders(state, true);
2909 ret = drm_atomic_commit(state);
2912 drm_atomic_state_put(state);
2915 EXPORT_SYMBOL(drm_atomic_helper_set_config);
2917 /* just used from fb-helper and atomic-helper: */
2918 int __drm_atomic_helper_set_config(struct drm_mode_set *set,
2919 struct drm_atomic_state *state)
2921 struct drm_crtc_state *crtc_state;
2922 struct drm_plane_state *primary_state;
2923 struct drm_crtc *crtc = set->crtc;
2924 int hdisplay, vdisplay;
2927 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2928 if (IS_ERR(crtc_state))
2929 return PTR_ERR(crtc_state);
2931 primary_state = drm_atomic_get_plane_state(state, crtc->primary);
2932 if (IS_ERR(primary_state))
2933 return PTR_ERR(primary_state);
2937 WARN_ON(set->num_connectors);
2939 ret = drm_atomic_set_mode_for_crtc(crtc_state, NULL);
2943 crtc_state->active = false;
2945 ret = drm_atomic_set_crtc_for_plane(primary_state, NULL);
2949 drm_atomic_set_fb_for_plane(primary_state, NULL);
2955 WARN_ON(!set->num_connectors);
2957 ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
2961 crtc_state->active = true;
2963 ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
2967 drm_mode_get_hv_timing(set->mode, &hdisplay, &vdisplay);
2969 drm_atomic_set_fb_for_plane(primary_state, set->fb);
2970 primary_state->crtc_x = 0;
2971 primary_state->crtc_y = 0;
2972 primary_state->crtc_w = hdisplay;
2973 primary_state->crtc_h = vdisplay;
2974 primary_state->src_x = set->x << 16;
2975 primary_state->src_y = set->y << 16;
2976 if (drm_rotation_90_or_270(primary_state->rotation)) {
2977 primary_state->src_w = vdisplay << 16;
2978 primary_state->src_h = hdisplay << 16;
2980 primary_state->src_w = hdisplay << 16;
2981 primary_state->src_h = vdisplay << 16;
2985 ret = update_output_state(state, set);
2992 static int __drm_atomic_helper_disable_all(struct drm_device *dev,
2993 struct drm_modeset_acquire_ctx *ctx,
2996 struct drm_atomic_state *state;
2997 struct drm_connector_state *conn_state;
2998 struct drm_connector *conn;
2999 struct drm_plane_state *plane_state;
3000 struct drm_plane *plane;
3001 struct drm_crtc_state *crtc_state;
3002 struct drm_crtc *crtc;
3005 state = drm_atomic_state_alloc(dev);
3009 state->acquire_ctx = ctx;
3011 drm_for_each_crtc(crtc, dev) {
3012 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3013 if (IS_ERR(crtc_state)) {
3014 ret = PTR_ERR(crtc_state);
3018 crtc_state->active = false;
3020 ret = drm_atomic_set_mode_prop_for_crtc(crtc_state, NULL);
3024 ret = drm_atomic_add_affected_planes(state, crtc);
3028 ret = drm_atomic_add_affected_connectors(state, crtc);
3033 for_each_new_connector_in_state(state, conn, conn_state, i) {
3034 ret = drm_atomic_set_crtc_for_connector(conn_state, NULL);
3039 for_each_new_plane_in_state(state, plane, plane_state, i) {
3040 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
3044 drm_atomic_set_fb_for_plane(plane_state, NULL);
3047 ret = drm_atomic_commit(state);
3049 drm_atomic_state_put(state);
3054 * drm_atomic_helper_disable_all - disable all currently active outputs
3056 * @ctx: lock acquisition context
3058 * Loops through all connectors, finding those that aren't turned off and then
3059 * turns them off by setting their DPMS mode to OFF and deactivating the CRTC
3060 * that they are connected to.
3062 * This is used for example in suspend/resume to disable all currently active
3063 * functions when suspending. If you just want to shut down everything at e.g.
3064 * driver unload, look at drm_atomic_helper_shutdown().
3066 * Note that if callers haven't already acquired all modeset locks this might
3067 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
3070 * 0 on success or a negative error code on failure.
3073 * drm_atomic_helper_suspend(), drm_atomic_helper_resume() and
3074 * drm_atomic_helper_shutdown().
3076 int drm_atomic_helper_disable_all(struct drm_device *dev,
3077 struct drm_modeset_acquire_ctx *ctx)
3079 return __drm_atomic_helper_disable_all(dev, ctx, false);
3081 EXPORT_SYMBOL(drm_atomic_helper_disable_all);
3084 * drm_atomic_helper_shutdown - shutdown all CRTC
3087 * This shuts down all CRTC, which is useful for driver unloading. Shutdown on
3088 * suspend should instead be handled with drm_atomic_helper_suspend(), since
3089 * that also takes a snapshot of the modeset state to be restored on resume.
3091 * This is just a convenience wrapper around drm_atomic_helper_disable_all(),
3092 * and it is the atomic version of drm_crtc_force_disable_all().
3094 void drm_atomic_helper_shutdown(struct drm_device *dev)
3096 struct drm_modeset_acquire_ctx ctx;
3099 drm_modeset_acquire_init(&ctx, 0);
3101 ret = drm_modeset_lock_all_ctx(dev, &ctx);
3103 ret = __drm_atomic_helper_disable_all(dev, &ctx, true);
3105 if (ret != -EDEADLK)
3108 drm_modeset_backoff(&ctx);
3112 DRM_ERROR("Disabling all crtc's during unload failed with %i\n", ret);
3114 drm_modeset_drop_locks(&ctx);
3115 drm_modeset_acquire_fini(&ctx);
3117 EXPORT_SYMBOL(drm_atomic_helper_shutdown);
3120 * drm_atomic_helper_suspend - subsystem-level suspend helper
3123 * Duplicates the current atomic state, disables all active outputs and then
3124 * returns a pointer to the original atomic state to the caller. Drivers can
3125 * pass this pointer to the drm_atomic_helper_resume() helper upon resume to
3126 * restore the output configuration that was active at the time the system
3129 * Note that it is potentially unsafe to use this. The atomic state object
3130 * returned by this function is assumed to be persistent. Drivers must ensure
3131 * that this holds true. Before calling this function, drivers must make sure
3132 * to suspend fbdev emulation so that nothing can be using the device.
3135 * A pointer to a copy of the state before suspend on success or an ERR_PTR()-
3136 * encoded error code on failure. Drivers should store the returned atomic
3137 * state object and pass it to the drm_atomic_helper_resume() helper upon
3141 * drm_atomic_helper_duplicate_state(), drm_atomic_helper_disable_all(),
3142 * drm_atomic_helper_resume(), drm_atomic_helper_commit_duplicated_state()
3144 struct drm_atomic_state *drm_atomic_helper_suspend(struct drm_device *dev)
3146 struct drm_modeset_acquire_ctx ctx;
3147 struct drm_atomic_state *state;
3150 drm_modeset_acquire_init(&ctx, 0);
3153 err = drm_modeset_lock_all_ctx(dev, &ctx);
3155 state = ERR_PTR(err);
3159 state = drm_atomic_helper_duplicate_state(dev, &ctx);
3163 err = drm_atomic_helper_disable_all(dev, &ctx);
3165 drm_atomic_state_put(state);
3166 state = ERR_PTR(err);
3171 if (PTR_ERR(state) == -EDEADLK) {
3172 drm_modeset_backoff(&ctx);
3176 drm_modeset_drop_locks(&ctx);
3177 drm_modeset_acquire_fini(&ctx);
3180 EXPORT_SYMBOL(drm_atomic_helper_suspend);
3183 * drm_atomic_helper_commit_duplicated_state - commit duplicated state
3184 * @state: duplicated atomic state to commit
3185 * @ctx: pointer to acquire_ctx to use for commit.
3187 * The state returned by drm_atomic_helper_duplicate_state() and
3188 * drm_atomic_helper_suspend() is partially invalid, and needs to
3189 * be fixed up before commit.
3192 * 0 on success or a negative error code on failure.
3195 * drm_atomic_helper_suspend()
3197 int drm_atomic_helper_commit_duplicated_state(struct drm_atomic_state *state,
3198 struct drm_modeset_acquire_ctx *ctx)
3201 struct drm_plane *plane;
3202 struct drm_plane_state *new_plane_state;
3203 struct drm_connector *connector;
3204 struct drm_connector_state *new_conn_state;
3205 struct drm_crtc *crtc;
3206 struct drm_crtc_state *new_crtc_state;
3208 state->acquire_ctx = ctx;
3210 for_each_new_plane_in_state(state, plane, new_plane_state, i)
3211 state->planes[i].old_state = plane->state;
3213 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i)
3214 state->crtcs[i].old_state = crtc->state;
3216 for_each_new_connector_in_state(state, connector, new_conn_state, i)
3217 state->connectors[i].old_state = connector->state;
3219 return drm_atomic_commit(state);
3221 EXPORT_SYMBOL(drm_atomic_helper_commit_duplicated_state);
3224 * drm_atomic_helper_resume - subsystem-level resume helper
3226 * @state: atomic state to resume to
3228 * Calls drm_mode_config_reset() to synchronize hardware and software states,
3229 * grabs all modeset locks and commits the atomic state object. This can be
3230 * used in conjunction with the drm_atomic_helper_suspend() helper to
3231 * implement suspend/resume for drivers that support atomic mode-setting.
3234 * 0 on success or a negative error code on failure.
3237 * drm_atomic_helper_suspend()
3239 int drm_atomic_helper_resume(struct drm_device *dev,
3240 struct drm_atomic_state *state)
3242 struct drm_modeset_acquire_ctx ctx;
3245 drm_mode_config_reset(dev);
3247 drm_modeset_acquire_init(&ctx, 0);
3249 err = drm_modeset_lock_all_ctx(dev, &ctx);
3253 err = drm_atomic_helper_commit_duplicated_state(state, &ctx);
3255 if (err != -EDEADLK)
3258 drm_modeset_backoff(&ctx);
3261 drm_atomic_state_put(state);
3262 drm_modeset_drop_locks(&ctx);
3263 drm_modeset_acquire_fini(&ctx);
3267 EXPORT_SYMBOL(drm_atomic_helper_resume);
3269 static int page_flip_common(struct drm_atomic_state *state,
3270 struct drm_crtc *crtc,
3271 struct drm_framebuffer *fb,
3272 struct drm_pending_vblank_event *event,
3275 struct drm_plane *plane = crtc->primary;
3276 struct drm_plane_state *plane_state;
3277 struct drm_crtc_state *crtc_state;
3280 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3281 if (IS_ERR(crtc_state))
3282 return PTR_ERR(crtc_state);
3284 crtc_state->event = event;
3285 crtc_state->pageflip_flags = flags;
3287 plane_state = drm_atomic_get_plane_state(state, plane);
3288 if (IS_ERR(plane_state))
3289 return PTR_ERR(plane_state);
3291 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
3294 drm_atomic_set_fb_for_plane(plane_state, fb);
3296 /* Make sure we don't accidentally do a full modeset. */
3297 state->allow_modeset = false;
3298 if (!crtc_state->active) {
3299 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] disabled, rejecting legacy flip\n",
3300 crtc->base.id, crtc->name);
3308 * drm_atomic_helper_page_flip - execute a legacy page flip
3310 * @fb: DRM framebuffer
3311 * @event: optional DRM event to signal upon completion
3312 * @flags: flip flags for non-vblank sync'ed updates
3313 * @ctx: lock acquisition context
3315 * Provides a default &drm_crtc_funcs.page_flip implementation
3316 * using the atomic driver interface.
3319 * Returns 0 on success, negative errno numbers on failure.
3322 * drm_atomic_helper_page_flip_target()
3324 int drm_atomic_helper_page_flip(struct drm_crtc *crtc,
3325 struct drm_framebuffer *fb,
3326 struct drm_pending_vblank_event *event,
3328 struct drm_modeset_acquire_ctx *ctx)
3330 struct drm_plane *plane = crtc->primary;
3331 struct drm_atomic_state *state;
3334 state = drm_atomic_state_alloc(plane->dev);
3338 state->acquire_ctx = ctx;
3340 ret = page_flip_common(state, crtc, fb, event, flags);
3344 ret = drm_atomic_nonblocking_commit(state);
3346 drm_atomic_state_put(state);
3349 EXPORT_SYMBOL(drm_atomic_helper_page_flip);
3352 * drm_atomic_helper_page_flip_target - do page flip on target vblank period.
3354 * @fb: DRM framebuffer
3355 * @event: optional DRM event to signal upon completion
3356 * @flags: flip flags for non-vblank sync'ed updates
3357 * @target: specifying the target vblank period when the flip to take effect
3358 * @ctx: lock acquisition context
3360 * Provides a default &drm_crtc_funcs.page_flip_target implementation.
3361 * Similar to drm_atomic_helper_page_flip() with extra parameter to specify
3362 * target vblank period to flip.
3365 * Returns 0 on success, negative errno numbers on failure.
3367 int drm_atomic_helper_page_flip_target(struct drm_crtc *crtc,
3368 struct drm_framebuffer *fb,
3369 struct drm_pending_vblank_event *event,
3372 struct drm_modeset_acquire_ctx *ctx)
3374 struct drm_plane *plane = crtc->primary;
3375 struct drm_atomic_state *state;
3376 struct drm_crtc_state *crtc_state;
3379 state = drm_atomic_state_alloc(plane->dev);
3383 state->acquire_ctx = ctx;
3385 ret = page_flip_common(state, crtc, fb, event, flags);
3389 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
3390 if (WARN_ON(!crtc_state)) {
3394 crtc_state->target_vblank = target;
3396 ret = drm_atomic_nonblocking_commit(state);
3398 drm_atomic_state_put(state);
3401 EXPORT_SYMBOL(drm_atomic_helper_page_flip_target);