2 * Copyright (C) 2014 Red Hat
3 * Copyright (C) 2014 Intel Corp.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
24 * Rob Clark <robdclark@gmail.com>
25 * Daniel Vetter <daniel.vetter@ffwll.ch>
28 #include <linux/dma-fence.h>
29 #include <linux/ktime.h>
31 #include <drm/drm_atomic.h>
32 #include <drm/drm_atomic_helper.h>
33 #include <drm/drm_atomic_uapi.h>
34 #include <drm/drm_blend.h>
35 #include <drm/drm_bridge.h>
36 #include <drm/drm_damage_helper.h>
37 #include <drm/drm_device.h>
38 #include <drm/drm_drv.h>
39 #include <drm/drm_framebuffer.h>
40 #include <drm/drm_gem_atomic_helper.h>
41 #include <drm/drm_print.h>
42 #include <drm/drm_self_refresh_helper.h>
43 #include <drm/drm_vblank.h>
44 #include <drm/drm_writeback.h>
46 #include "drm_crtc_helper_internal.h"
47 #include "drm_crtc_internal.h"
52 * This helper library provides implementations of check and commit functions on
53 * top of the CRTC modeset helper callbacks and the plane helper callbacks. It
54 * also provides convenience implementations for the atomic state handling
55 * callbacks for drivers which don't need to subclass the drm core structures to
56 * add their own additional internal state.
58 * This library also provides default implementations for the check callback in
59 * drm_atomic_helper_check() and for the commit callback with
60 * drm_atomic_helper_commit(). But the individual stages and callbacks are
61 * exposed to allow drivers to mix and match and e.g. use the plane helpers only
62 * together with a driver private modeset implementation.
64 * This library also provides implementations for all the legacy driver
65 * interfaces on top of the atomic interface. See drm_atomic_helper_set_config(),
66 * drm_atomic_helper_disable_plane(), and the various functions to implement
67 * set_property callbacks. New drivers must not implement these functions
68 * themselves but must use the provided helpers.
70 * The atomic helper uses the same function table structures as all other
71 * modesetting helpers. See the documentation for &struct drm_crtc_helper_funcs,
72 * struct &drm_encoder_helper_funcs and &struct drm_connector_helper_funcs. It
73 * also shares the &struct drm_plane_helper_funcs function table with the plane
77 drm_atomic_helper_plane_changed(struct drm_atomic_state *state,
78 struct drm_plane_state *old_plane_state,
79 struct drm_plane_state *plane_state,
80 struct drm_plane *plane)
82 struct drm_crtc_state *crtc_state;
84 if (old_plane_state->crtc) {
85 crtc_state = drm_atomic_get_new_crtc_state(state,
86 old_plane_state->crtc);
88 if (WARN_ON(!crtc_state))
91 crtc_state->planes_changed = true;
94 if (plane_state->crtc) {
95 crtc_state = drm_atomic_get_new_crtc_state(state, plane_state->crtc);
97 if (WARN_ON(!crtc_state))
100 crtc_state->planes_changed = true;
104 static int handle_conflicting_encoders(struct drm_atomic_state *state,
105 bool disable_conflicting_encoders)
107 struct drm_connector_state *new_conn_state;
108 struct drm_connector *connector;
109 struct drm_connector_list_iter conn_iter;
110 struct drm_encoder *encoder;
111 unsigned int encoder_mask = 0;
115 * First loop, find all newly assigned encoders from the connectors
116 * part of the state. If the same encoder is assigned to multiple
117 * connectors bail out.
119 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
120 const struct drm_connector_helper_funcs *funcs = connector->helper_private;
121 struct drm_encoder *new_encoder;
123 if (!new_conn_state->crtc)
126 if (funcs->atomic_best_encoder)
127 new_encoder = funcs->atomic_best_encoder(connector,
129 else if (funcs->best_encoder)
130 new_encoder = funcs->best_encoder(connector);
132 new_encoder = drm_connector_get_single_encoder(connector);
135 if (encoder_mask & drm_encoder_mask(new_encoder)) {
136 drm_dbg_atomic(connector->dev,
137 "[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_dbg_atomic(connector->dev,
175 "[ENCODER:%d:%s] in use on [CRTC:%d:%s] by [CONNECTOR:%d:%s]\n",
176 encoder->base.id, encoder->name,
177 connector->state->crtc->base.id,
178 connector->state->crtc->name,
179 connector->base.id, connector->name);
184 new_conn_state = drm_atomic_get_connector_state(state, connector);
185 if (IS_ERR(new_conn_state)) {
186 ret = PTR_ERR(new_conn_state);
190 drm_dbg_atomic(connector->dev,
191 "[ENCODER:%d:%s] in use on [CRTC:%d:%s], disabling [CONNECTOR:%d:%s]\n",
192 encoder->base.id, encoder->name,
193 new_conn_state->crtc->base.id, new_conn_state->crtc->name,
194 connector->base.id, connector->name);
196 crtc_state = drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
198 ret = drm_atomic_set_crtc_for_connector(new_conn_state, NULL);
202 if (!crtc_state->connector_mask) {
203 ret = drm_atomic_set_mode_prop_for_crtc(crtc_state,
208 crtc_state->active = false;
212 drm_connector_list_iter_end(&conn_iter);
218 set_best_encoder(struct drm_atomic_state *state,
219 struct drm_connector_state *conn_state,
220 struct drm_encoder *encoder)
222 struct drm_crtc_state *crtc_state;
223 struct drm_crtc *crtc;
225 if (conn_state->best_encoder) {
226 /* Unset the encoder_mask in the old crtc state. */
227 crtc = conn_state->connector->state->crtc;
229 /* A NULL crtc is an error here because we should have
230 * duplicated a NULL best_encoder when crtc was NULL.
231 * As an exception restoring duplicated atomic state
232 * during resume is allowed, so don't warn when
233 * best_encoder is equal to encoder we intend to set.
235 WARN_ON(!crtc && encoder != conn_state->best_encoder);
237 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
239 crtc_state->encoder_mask &=
240 ~drm_encoder_mask(conn_state->best_encoder);
245 crtc = conn_state->crtc;
248 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
250 crtc_state->encoder_mask |=
251 drm_encoder_mask(encoder);
255 conn_state->best_encoder = encoder;
259 steal_encoder(struct drm_atomic_state *state,
260 struct drm_encoder *encoder)
262 struct drm_crtc_state *crtc_state;
263 struct drm_connector *connector;
264 struct drm_connector_state *old_connector_state, *new_connector_state;
267 for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
268 struct drm_crtc *encoder_crtc;
270 if (new_connector_state->best_encoder != encoder)
273 encoder_crtc = old_connector_state->crtc;
275 drm_dbg_atomic(encoder->dev,
276 "[ENCODER:%d:%s] in use on [CRTC:%d:%s], stealing it\n",
277 encoder->base.id, encoder->name,
278 encoder_crtc->base.id, encoder_crtc->name);
280 set_best_encoder(state, new_connector_state, NULL);
282 crtc_state = drm_atomic_get_new_crtc_state(state, encoder_crtc);
283 crtc_state->connectors_changed = true;
290 update_connector_routing(struct drm_atomic_state *state,
291 struct drm_connector *connector,
292 struct drm_connector_state *old_connector_state,
293 struct drm_connector_state *new_connector_state,
296 const struct drm_connector_helper_funcs *funcs;
297 struct drm_encoder *new_encoder;
298 struct drm_crtc_state *crtc_state;
300 drm_dbg_atomic(connector->dev, "Updating routing for [CONNECTOR:%d:%s]\n",
301 connector->base.id, connector->name);
303 if (old_connector_state->crtc != new_connector_state->crtc) {
304 if (old_connector_state->crtc) {
305 crtc_state = drm_atomic_get_new_crtc_state(state, old_connector_state->crtc);
306 crtc_state->connectors_changed = true;
309 if (new_connector_state->crtc) {
310 crtc_state = drm_atomic_get_new_crtc_state(state, new_connector_state->crtc);
311 crtc_state->connectors_changed = true;
315 if (!new_connector_state->crtc) {
316 drm_dbg_atomic(connector->dev, "Disabling [CONNECTOR:%d:%s]\n",
317 connector->base.id, connector->name);
319 set_best_encoder(state, new_connector_state, NULL);
324 crtc_state = drm_atomic_get_new_crtc_state(state,
325 new_connector_state->crtc);
327 * For compatibility with legacy users, we want to make sure that
328 * we allow DPMS On->Off modesets on unregistered connectors. Modesets
329 * which would result in anything else must be considered invalid, to
330 * avoid turning on new displays on dead connectors.
332 * Since the connector can be unregistered at any point during an
333 * atomic check or commit, this is racy. But that's OK: all we care
334 * about is ensuring that userspace can't do anything but shut off the
335 * display on a connector that was destroyed after it's been notified,
338 * Additionally, we also want to ignore connector registration when
339 * we're trying to restore an atomic state during system resume since
340 * there's a chance the connector may have been destroyed during the
341 * process, but it's better to ignore that then cause
342 * drm_atomic_helper_resume() to fail.
344 * Last, we want to ignore connector registration when the connector
345 * was not pulled in the atomic state by user-space (ie, was pulled
346 * in by the driver, e.g. when updating a DP-MST stream).
348 if (!state->duplicated && drm_connector_is_unregistered(connector) &&
349 added_by_user && crtc_state->active) {
350 drm_dbg_atomic(connector->dev,
351 "[CONNECTOR:%d:%s] is not registered\n",
352 connector->base.id, connector->name);
356 funcs = connector->helper_private;
358 if (funcs->atomic_best_encoder)
359 new_encoder = funcs->atomic_best_encoder(connector, state);
360 else if (funcs->best_encoder)
361 new_encoder = funcs->best_encoder(connector);
363 new_encoder = drm_connector_get_single_encoder(connector);
366 drm_dbg_atomic(connector->dev,
367 "No suitable encoder found for [CONNECTOR:%d:%s]\n",
368 connector->base.id, connector->name);
372 if (!drm_encoder_crtc_ok(new_encoder, new_connector_state->crtc)) {
373 drm_dbg_atomic(connector->dev,
374 "[ENCODER:%d:%s] incompatible with [CRTC:%d:%s]\n",
375 new_encoder->base.id,
377 new_connector_state->crtc->base.id,
378 new_connector_state->crtc->name);
382 if (new_encoder == new_connector_state->best_encoder) {
383 set_best_encoder(state, new_connector_state, new_encoder);
385 drm_dbg_atomic(connector->dev,
386 "[CONNECTOR:%d:%s] keeps [ENCODER:%d:%s], now on [CRTC:%d:%s]\n",
389 new_encoder->base.id,
391 new_connector_state->crtc->base.id,
392 new_connector_state->crtc->name);
397 steal_encoder(state, new_encoder);
399 set_best_encoder(state, new_connector_state, new_encoder);
401 crtc_state->connectors_changed = true;
403 drm_dbg_atomic(connector->dev,
404 "[CONNECTOR:%d:%s] using [ENCODER:%d:%s] on [CRTC:%d:%s]\n",
407 new_encoder->base.id,
409 new_connector_state->crtc->base.id,
410 new_connector_state->crtc->name);
416 mode_fixup(struct drm_atomic_state *state)
418 struct drm_crtc *crtc;
419 struct drm_crtc_state *new_crtc_state;
420 struct drm_connector *connector;
421 struct drm_connector_state *new_conn_state;
425 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
426 if (!new_crtc_state->mode_changed &&
427 !new_crtc_state->connectors_changed)
430 drm_mode_copy(&new_crtc_state->adjusted_mode, &new_crtc_state->mode);
433 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
434 const struct drm_encoder_helper_funcs *funcs;
435 struct drm_encoder *encoder;
436 struct drm_bridge *bridge;
438 WARN_ON(!!new_conn_state->best_encoder != !!new_conn_state->crtc);
440 if (!new_conn_state->crtc || !new_conn_state->best_encoder)
444 drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
446 if (!new_crtc_state->mode_changed &&
447 !new_crtc_state->connectors_changed) {
452 * Each encoder has at most one connector (since we always steal
453 * it away), so we won't call ->mode_fixup twice.
455 encoder = new_conn_state->best_encoder;
456 funcs = encoder->helper_private;
458 bridge = drm_bridge_chain_get_first_bridge(encoder);
459 ret = drm_atomic_bridge_chain_check(bridge,
463 drm_dbg_atomic(encoder->dev, "Bridge atomic check failed\n");
467 if (funcs && funcs->atomic_check) {
468 ret = funcs->atomic_check(encoder, new_crtc_state,
471 drm_dbg_atomic(encoder->dev,
472 "[ENCODER:%d:%s] check failed\n",
473 encoder->base.id, encoder->name);
476 } else if (funcs && funcs->mode_fixup) {
477 ret = funcs->mode_fixup(encoder, &new_crtc_state->mode,
478 &new_crtc_state->adjusted_mode);
480 drm_dbg_atomic(encoder->dev,
481 "[ENCODER:%d:%s] fixup failed\n",
482 encoder->base.id, encoder->name);
488 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
489 const struct drm_crtc_helper_funcs *funcs;
491 if (!new_crtc_state->enable)
494 if (!new_crtc_state->mode_changed &&
495 !new_crtc_state->connectors_changed)
498 funcs = crtc->helper_private;
499 if (!funcs || !funcs->mode_fixup)
502 ret = funcs->mode_fixup(crtc, &new_crtc_state->mode,
503 &new_crtc_state->adjusted_mode);
505 drm_dbg_atomic(crtc->dev, "[CRTC:%d:%s] fixup failed\n",
506 crtc->base.id, crtc->name);
514 static enum drm_mode_status mode_valid_path(struct drm_connector *connector,
515 struct drm_encoder *encoder,
516 struct drm_crtc *crtc,
517 const struct drm_display_mode *mode)
519 struct drm_bridge *bridge;
520 enum drm_mode_status ret;
522 ret = drm_encoder_mode_valid(encoder, mode);
523 if (ret != MODE_OK) {
524 drm_dbg_atomic(encoder->dev,
525 "[ENCODER:%d:%s] mode_valid() failed\n",
526 encoder->base.id, encoder->name);
530 bridge = drm_bridge_chain_get_first_bridge(encoder);
531 ret = drm_bridge_chain_mode_valid(bridge, &connector->display_info,
533 if (ret != MODE_OK) {
534 drm_dbg_atomic(encoder->dev, "[BRIDGE] mode_valid() failed\n");
538 ret = drm_crtc_mode_valid(crtc, mode);
539 if (ret != MODE_OK) {
540 drm_dbg_atomic(encoder->dev, "[CRTC:%d:%s] mode_valid() failed\n",
541 crtc->base.id, crtc->name);
549 mode_valid(struct drm_atomic_state *state)
551 struct drm_connector_state *conn_state;
552 struct drm_connector *connector;
555 for_each_new_connector_in_state(state, connector, conn_state, i) {
556 struct drm_encoder *encoder = conn_state->best_encoder;
557 struct drm_crtc *crtc = conn_state->crtc;
558 struct drm_crtc_state *crtc_state;
559 enum drm_mode_status mode_status;
560 const struct drm_display_mode *mode;
562 if (!crtc || !encoder)
565 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
568 if (!crtc_state->mode_changed && !crtc_state->connectors_changed)
571 mode = &crtc_state->mode;
573 mode_status = mode_valid_path(connector, encoder, crtc, mode);
574 if (mode_status != MODE_OK)
582 * drm_atomic_helper_check_modeset - validate state object for modeset changes
584 * @state: the driver state object
586 * Check the state object to see if the requested state is physically possible.
587 * This does all the CRTC and connector related computations for an atomic
588 * update and adds any additional connectors needed for full modesets. It calls
589 * the various per-object callbacks in the follow order:
591 * 1. &drm_connector_helper_funcs.atomic_best_encoder for determining the new encoder.
592 * 2. &drm_connector_helper_funcs.atomic_check to validate the connector state.
593 * 3. If it's determined a modeset is needed then all connectors on the affected
594 * CRTC are added and &drm_connector_helper_funcs.atomic_check is run on them.
595 * 4. &drm_encoder_helper_funcs.mode_valid, &drm_bridge_funcs.mode_valid and
596 * &drm_crtc_helper_funcs.mode_valid are called on the affected components.
597 * 5. &drm_bridge_funcs.mode_fixup is called on all encoder bridges.
598 * 6. &drm_encoder_helper_funcs.atomic_check is called to validate any encoder state.
599 * This function is only called when the encoder will be part of a configured CRTC,
600 * it must not be used for implementing connector property validation.
601 * If this function is NULL, &drm_atomic_encoder_helper_funcs.mode_fixup is called
603 * 7. &drm_crtc_helper_funcs.mode_fixup is called last, to fix up the mode with CRTC constraints.
605 * &drm_crtc_state.mode_changed is set when the input mode is changed.
606 * &drm_crtc_state.connectors_changed is set when a connector is added or
607 * removed from the CRTC. &drm_crtc_state.active_changed is set when
608 * &drm_crtc_state.active changes, which is used for DPMS.
609 * &drm_crtc_state.no_vblank is set from the result of drm_dev_has_vblank().
610 * See also: drm_atomic_crtc_needs_modeset()
614 * Drivers which set &drm_crtc_state.mode_changed (e.g. in their
615 * &drm_plane_helper_funcs.atomic_check hooks if a plane update can't be done
616 * without a full modeset) _must_ call this function after that change. It is
617 * permitted to call this function multiple times for the same update, e.g.
618 * when the &drm_crtc_helper_funcs.atomic_check functions depend upon the
619 * adjusted dotclock for fifo space allocation and watermark computation.
622 * Zero for success or -errno
625 drm_atomic_helper_check_modeset(struct drm_device *dev,
626 struct drm_atomic_state *state)
628 struct drm_crtc *crtc;
629 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
630 struct drm_connector *connector;
631 struct drm_connector_state *old_connector_state, *new_connector_state;
633 unsigned int connectors_mask = 0, user_connectors_mask = 0;
635 for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i)
636 user_connectors_mask |= BIT(i);
638 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
639 bool has_connectors =
640 !!new_crtc_state->connector_mask;
642 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
644 if (!drm_mode_equal(&old_crtc_state->mode, &new_crtc_state->mode)) {
645 drm_dbg_atomic(dev, "[CRTC:%d:%s] mode changed\n",
646 crtc->base.id, crtc->name);
647 new_crtc_state->mode_changed = true;
650 if (old_crtc_state->enable != new_crtc_state->enable) {
651 drm_dbg_atomic(dev, "[CRTC:%d:%s] enable changed\n",
652 crtc->base.id, crtc->name);
655 * For clarity this assignment is done here, but
656 * enable == 0 is only true when there are no
657 * connectors and a NULL mode.
659 * The other way around is true as well. enable != 0
660 * implies that connectors are attached and a mode is set.
662 new_crtc_state->mode_changed = true;
663 new_crtc_state->connectors_changed = true;
666 if (old_crtc_state->active != new_crtc_state->active) {
667 drm_dbg_atomic(dev, "[CRTC:%d:%s] active changed\n",
668 crtc->base.id, crtc->name);
669 new_crtc_state->active_changed = true;
672 if (new_crtc_state->enable != has_connectors) {
673 drm_dbg_atomic(dev, "[CRTC:%d:%s] enabled/connectors mismatch\n",
674 crtc->base.id, crtc->name);
679 if (drm_dev_has_vblank(dev))
680 new_crtc_state->no_vblank = false;
682 new_crtc_state->no_vblank = true;
685 ret = handle_conflicting_encoders(state, false);
689 for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
690 const struct drm_connector_helper_funcs *funcs = connector->helper_private;
692 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
695 * This only sets crtc->connectors_changed for routing changes,
696 * drivers must set crtc->connectors_changed themselves when
697 * connector properties need to be updated.
699 ret = update_connector_routing(state, connector,
702 BIT(i) & user_connectors_mask);
705 if (old_connector_state->crtc) {
706 new_crtc_state = drm_atomic_get_new_crtc_state(state,
707 old_connector_state->crtc);
708 if (old_connector_state->link_status !=
709 new_connector_state->link_status)
710 new_crtc_state->connectors_changed = true;
712 if (old_connector_state->max_requested_bpc !=
713 new_connector_state->max_requested_bpc)
714 new_crtc_state->connectors_changed = true;
717 if (funcs->atomic_check)
718 ret = funcs->atomic_check(connector, state);
721 "[CONNECTOR:%d:%s] driver check failed\n",
722 connector->base.id, connector->name);
726 connectors_mask |= BIT(i);
730 * After all the routing has been prepared we need to add in any
731 * connector which is itself unchanged, but whose CRTC changes its
732 * configuration. This must be done before calling mode_fixup in case a
733 * crtc only changed its mode but has the same set of connectors.
735 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
736 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
740 "[CRTC:%d:%s] needs all connectors, enable: %c, active: %c\n",
741 crtc->base.id, crtc->name,
742 new_crtc_state->enable ? 'y' : 'n',
743 new_crtc_state->active ? 'y' : 'n');
745 ret = drm_atomic_add_affected_connectors(state, crtc);
749 ret = drm_atomic_add_affected_planes(state, crtc);
755 * Iterate over all connectors again, to make sure atomic_check()
756 * has been called on them when a modeset is forced.
758 for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
759 const struct drm_connector_helper_funcs *funcs = connector->helper_private;
761 if (connectors_mask & BIT(i))
764 if (funcs->atomic_check)
765 ret = funcs->atomic_check(connector, state);
768 "[CONNECTOR:%d:%s] driver check failed\n",
769 connector->base.id, connector->name);
775 * Iterate over all connectors again, and add all affected bridges to
778 for_each_oldnew_connector_in_state(state, connector,
780 new_connector_state, i) {
781 struct drm_encoder *encoder;
783 encoder = old_connector_state->best_encoder;
784 ret = drm_atomic_add_encoder_bridges(state, encoder);
788 encoder = new_connector_state->best_encoder;
789 ret = drm_atomic_add_encoder_bridges(state, encoder);
794 ret = mode_valid(state);
798 return mode_fixup(state);
800 EXPORT_SYMBOL(drm_atomic_helper_check_modeset);
803 * drm_atomic_helper_check_wb_encoder_state() - Check writeback encoder state
804 * @encoder: encoder state to check
805 * @conn_state: connector state to check
807 * Checks if the writeback connector state is valid, and returns an error if it
811 * Zero for success or -errno
814 drm_atomic_helper_check_wb_encoder_state(struct drm_encoder *encoder,
815 struct drm_connector_state *conn_state)
817 struct drm_writeback_job *wb_job = conn_state->writeback_job;
818 struct drm_property_blob *pixel_format_blob;
819 struct drm_framebuffer *fb;
823 if (!wb_job || !wb_job->fb)
826 pixel_format_blob = wb_job->connector->pixel_formats_blob_ptr;
827 nformats = pixel_format_blob->length / sizeof(u32);
828 formats = pixel_format_blob->data;
831 for (i = 0; i < nformats; i++)
832 if (fb->format->format == formats[i])
835 drm_dbg_kms(encoder->dev, "Invalid pixel format %p4cc\n", &fb->format->format);
839 EXPORT_SYMBOL(drm_atomic_helper_check_wb_encoder_state);
842 * drm_atomic_helper_check_plane_state() - Check plane state for validity
843 * @plane_state: plane state to check
844 * @crtc_state: CRTC state to check
845 * @min_scale: minimum @src:@dest scaling factor in 16.16 fixed point
846 * @max_scale: maximum @src:@dest scaling factor in 16.16 fixed point
847 * @can_position: is it legal to position the plane such that it
848 * doesn't cover the entire CRTC? This will generally
849 * only be false for primary planes.
850 * @can_update_disabled: can the plane be updated while the CRTC
853 * Checks that a desired plane update is valid, and updates various
854 * bits of derived state (clipped coordinates etc.). Drivers that provide
855 * their own plane handling rather than helper-provided implementations may
856 * still wish to call this function to avoid duplication of error checking
860 * Zero if update appears valid, error code on failure
862 int drm_atomic_helper_check_plane_state(struct drm_plane_state *plane_state,
863 const struct drm_crtc_state *crtc_state,
867 bool can_update_disabled)
869 struct drm_framebuffer *fb = plane_state->fb;
870 struct drm_rect *src = &plane_state->src;
871 struct drm_rect *dst = &plane_state->dst;
872 unsigned int rotation = plane_state->rotation;
873 struct drm_rect clip = {};
876 WARN_ON(plane_state->crtc && plane_state->crtc != crtc_state->crtc);
878 *src = drm_plane_state_src(plane_state);
879 *dst = drm_plane_state_dest(plane_state);
882 plane_state->visible = false;
886 /* crtc should only be NULL when disabling (i.e., !fb) */
887 if (WARN_ON(!plane_state->crtc)) {
888 plane_state->visible = false;
892 if (!crtc_state->enable && !can_update_disabled) {
893 drm_dbg_kms(plane_state->plane->dev,
894 "Cannot update plane of a disabled CRTC.\n");
898 drm_rect_rotate(src, fb->width << 16, fb->height << 16, rotation);
901 hscale = drm_rect_calc_hscale(src, dst, min_scale, max_scale);
902 vscale = drm_rect_calc_vscale(src, dst, min_scale, max_scale);
903 if (hscale < 0 || vscale < 0) {
904 drm_dbg_kms(plane_state->plane->dev,
905 "Invalid scaling of plane\n");
906 drm_rect_debug_print("src: ", &plane_state->src, true);
907 drm_rect_debug_print("dst: ", &plane_state->dst, false);
911 if (crtc_state->enable)
912 drm_mode_get_hv_timing(&crtc_state->mode, &clip.x2, &clip.y2);
914 plane_state->visible = drm_rect_clip_scaled(src, dst, &clip);
916 drm_rect_rotate_inv(src, fb->width << 16, fb->height << 16, rotation);
918 if (!plane_state->visible)
920 * Plane isn't visible; some drivers can handle this
921 * so we just return success here. Drivers that can't
922 * (including those that use the primary plane helper's
923 * update function) will return an error from their
924 * update_plane handler.
928 if (!can_position && !drm_rect_equals(dst, &clip)) {
929 drm_dbg_kms(plane_state->plane->dev,
930 "Plane must cover entire CRTC\n");
931 drm_rect_debug_print("dst: ", dst, false);
932 drm_rect_debug_print("clip: ", &clip, false);
938 EXPORT_SYMBOL(drm_atomic_helper_check_plane_state);
941 * drm_atomic_helper_check_crtc_primary_plane() - Check CRTC state for primary plane
942 * @crtc_state: CRTC state to check
944 * Checks that a CRTC has at least one primary plane attached to it, which is
945 * a requirement on some hardware. Note that this only involves the CRTC side
946 * of the test. To test if the primary plane is visible or if it can be updated
947 * without the CRTC being enabled, use drm_atomic_helper_check_plane_state() in
948 * the plane's atomic check.
951 * 0 if a primary plane is attached to the CRTC, or an error code otherwise
953 int drm_atomic_helper_check_crtc_primary_plane(struct drm_crtc_state *crtc_state)
955 struct drm_crtc *crtc = crtc_state->crtc;
956 struct drm_device *dev = crtc->dev;
957 struct drm_plane *plane;
959 /* needs at least one primary plane to be enabled */
960 drm_for_each_plane_mask(plane, dev, crtc_state->plane_mask) {
961 if (plane->type == DRM_PLANE_TYPE_PRIMARY)
965 drm_dbg_atomic(dev, "[CRTC:%d:%s] primary plane missing\n", crtc->base.id, crtc->name);
969 EXPORT_SYMBOL(drm_atomic_helper_check_crtc_primary_plane);
972 * drm_atomic_helper_check_planes - validate state object for planes changes
974 * @state: the driver state object
976 * Check the state object to see if the requested state is physically possible.
977 * This does all the plane update related checks using by calling into the
978 * &drm_crtc_helper_funcs.atomic_check and &drm_plane_helper_funcs.atomic_check
979 * hooks provided by the driver.
981 * It also sets &drm_crtc_state.planes_changed to indicate that a CRTC has
985 * Zero for success or -errno
988 drm_atomic_helper_check_planes(struct drm_device *dev,
989 struct drm_atomic_state *state)
991 struct drm_crtc *crtc;
992 struct drm_crtc_state *new_crtc_state;
993 struct drm_plane *plane;
994 struct drm_plane_state *new_plane_state, *old_plane_state;
997 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
998 const struct drm_plane_helper_funcs *funcs;
1000 WARN_ON(!drm_modeset_is_locked(&plane->mutex));
1002 funcs = plane->helper_private;
1004 drm_atomic_helper_plane_changed(state, old_plane_state, new_plane_state, plane);
1006 drm_atomic_helper_check_plane_damage(state, new_plane_state);
1008 if (!funcs || !funcs->atomic_check)
1011 ret = funcs->atomic_check(plane, state);
1013 drm_dbg_atomic(plane->dev,
1014 "[PLANE:%d:%s] atomic driver check failed\n",
1015 plane->base.id, plane->name);
1020 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
1021 const struct drm_crtc_helper_funcs *funcs;
1023 funcs = crtc->helper_private;
1025 if (!funcs || !funcs->atomic_check)
1028 ret = funcs->atomic_check(crtc, state);
1030 drm_dbg_atomic(crtc->dev,
1031 "[CRTC:%d:%s] atomic driver check failed\n",
1032 crtc->base.id, crtc->name);
1039 EXPORT_SYMBOL(drm_atomic_helper_check_planes);
1042 * drm_atomic_helper_check - validate state object
1044 * @state: the driver state object
1046 * Check the state object to see if the requested state is physically possible.
1047 * Only CRTCs and planes have check callbacks, so for any additional (global)
1048 * checking that a driver needs it can simply wrap that around this function.
1049 * Drivers without such needs can directly use this as their
1050 * &drm_mode_config_funcs.atomic_check callback.
1052 * This just wraps the two parts of the state checking for planes and modeset
1053 * state in the default order: First it calls drm_atomic_helper_check_modeset()
1054 * and then drm_atomic_helper_check_planes(). The assumption is that the
1055 * @drm_plane_helper_funcs.atomic_check and @drm_crtc_helper_funcs.atomic_check
1056 * functions depend upon an updated adjusted_mode.clock to e.g. properly compute
1059 * Note that zpos normalization will add all enable planes to the state which
1060 * might not desired for some drivers.
1061 * For example enable/disable of a cursor plane which have fixed zpos value
1062 * would trigger all other enabled planes to be forced to the state change.
1065 * Zero for success or -errno
1067 int drm_atomic_helper_check(struct drm_device *dev,
1068 struct drm_atomic_state *state)
1072 ret = drm_atomic_helper_check_modeset(dev, state);
1076 if (dev->mode_config.normalize_zpos) {
1077 ret = drm_atomic_normalize_zpos(dev, state);
1082 ret = drm_atomic_helper_check_planes(dev, state);
1086 if (state->legacy_cursor_update)
1087 state->async_update = !drm_atomic_helper_async_check(dev, state);
1089 drm_self_refresh_helper_alter_state(state);
1093 EXPORT_SYMBOL(drm_atomic_helper_check);
1096 crtc_needs_disable(struct drm_crtc_state *old_state,
1097 struct drm_crtc_state *new_state)
1100 * No new_state means the CRTC is off, so the only criteria is whether
1101 * it's currently active or in self refresh mode.
1104 return drm_atomic_crtc_effectively_active(old_state);
1107 * We need to disable bridge(s) and CRTC if we're transitioning out of
1108 * self-refresh and changing CRTCs at the same time, because the
1109 * bridge tracks self-refresh status via CRTC state.
1111 if (old_state->self_refresh_active &&
1112 old_state->crtc != new_state->crtc)
1116 * We also need to run through the crtc_funcs->disable() function if
1117 * the CRTC is currently on, if it's transitioning to self refresh
1118 * mode, or if it's in self refresh mode and needs to be fully
1121 return old_state->active ||
1122 (old_state->self_refresh_active && !new_state->active) ||
1123 new_state->self_refresh_active;
1127 disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state)
1129 struct drm_connector *connector;
1130 struct drm_connector_state *old_conn_state, *new_conn_state;
1131 struct drm_crtc *crtc;
1132 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
1135 for_each_oldnew_connector_in_state(old_state, connector, old_conn_state, new_conn_state, i) {
1136 const struct drm_encoder_helper_funcs *funcs;
1137 struct drm_encoder *encoder;
1138 struct drm_bridge *bridge;
1141 * Shut down everything that's in the changeset and currently
1142 * still on. So need to check the old, saved state.
1144 if (!old_conn_state->crtc)
1147 old_crtc_state = drm_atomic_get_old_crtc_state(old_state, old_conn_state->crtc);
1149 if (new_conn_state->crtc)
1150 new_crtc_state = drm_atomic_get_new_crtc_state(
1152 new_conn_state->crtc);
1154 new_crtc_state = NULL;
1156 if (!crtc_needs_disable(old_crtc_state, new_crtc_state) ||
1157 !drm_atomic_crtc_needs_modeset(old_conn_state->crtc->state))
1160 encoder = old_conn_state->best_encoder;
1162 /* We shouldn't get this far if we didn't previously have
1163 * an encoder.. but WARN_ON() rather than explode.
1165 if (WARN_ON(!encoder))
1168 funcs = encoder->helper_private;
1170 drm_dbg_atomic(dev, "disabling [ENCODER:%d:%s]\n",
1171 encoder->base.id, encoder->name);
1174 * Each encoder has at most one connector (since we always steal
1175 * it away), so we won't call disable hooks twice.
1177 bridge = drm_bridge_chain_get_first_bridge(encoder);
1178 drm_atomic_bridge_chain_disable(bridge, old_state);
1180 /* Right function depends upon target state. */
1182 if (funcs->atomic_disable)
1183 funcs->atomic_disable(encoder, old_state);
1184 else if (new_conn_state->crtc && funcs->prepare)
1185 funcs->prepare(encoder);
1186 else if (funcs->disable)
1187 funcs->disable(encoder);
1188 else if (funcs->dpms)
1189 funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
1192 drm_atomic_bridge_chain_post_disable(bridge, old_state);
1195 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
1196 const struct drm_crtc_helper_funcs *funcs;
1199 /* Shut down everything that needs a full modeset. */
1200 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
1203 if (!crtc_needs_disable(old_crtc_state, new_crtc_state))
1206 funcs = crtc->helper_private;
1208 drm_dbg_atomic(dev, "disabling [CRTC:%d:%s]\n",
1209 crtc->base.id, crtc->name);
1212 /* Right function depends upon target state. */
1213 if (new_crtc_state->enable && funcs->prepare)
1214 funcs->prepare(crtc);
1215 else if (funcs->atomic_disable)
1216 funcs->atomic_disable(crtc, old_state);
1217 else if (funcs->disable)
1218 funcs->disable(crtc);
1219 else if (funcs->dpms)
1220 funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
1222 if (!drm_dev_has_vblank(dev))
1225 ret = drm_crtc_vblank_get(crtc);
1227 * Self-refresh is not a true "disable"; ensure vblank remains
1230 if (new_crtc_state->self_refresh_active)
1232 "driver disabled vblank in self-refresh\n");
1234 WARN_ONCE(ret != -EINVAL,
1235 "driver forgot to call drm_crtc_vblank_off()\n");
1237 drm_crtc_vblank_put(crtc);
1242 * drm_atomic_helper_update_legacy_modeset_state - update legacy modeset state
1244 * @old_state: atomic state object with old state structures
1246 * This function updates all the various legacy modeset state pointers in
1247 * connectors, encoders and CRTCs.
1249 * Drivers can use this for building their own atomic commit if they don't have
1250 * a pure helper-based modeset implementation.
1252 * Since these updates are not synchronized with lockings, only code paths
1253 * called from &drm_mode_config_helper_funcs.atomic_commit_tail can look at the
1254 * legacy state filled out by this helper. Defacto this means this helper and
1255 * the legacy state pointers are only really useful for transitioning an
1256 * existing driver to the atomic world.
1259 drm_atomic_helper_update_legacy_modeset_state(struct drm_device *dev,
1260 struct drm_atomic_state *old_state)
1262 struct drm_connector *connector;
1263 struct drm_connector_state *old_conn_state, *new_conn_state;
1264 struct drm_crtc *crtc;
1265 struct drm_crtc_state *new_crtc_state;
1268 /* clear out existing links and update dpms */
1269 for_each_oldnew_connector_in_state(old_state, connector, old_conn_state, new_conn_state, i) {
1270 if (connector->encoder) {
1271 WARN_ON(!connector->encoder->crtc);
1273 connector->encoder->crtc = NULL;
1274 connector->encoder = NULL;
1277 crtc = new_conn_state->crtc;
1278 if ((!crtc && old_conn_state->crtc) ||
1279 (crtc && drm_atomic_crtc_needs_modeset(crtc->state))) {
1280 int mode = DRM_MODE_DPMS_OFF;
1282 if (crtc && crtc->state->active)
1283 mode = DRM_MODE_DPMS_ON;
1285 connector->dpms = mode;
1290 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
1291 if (!new_conn_state->crtc)
1294 if (WARN_ON(!new_conn_state->best_encoder))
1297 connector->encoder = new_conn_state->best_encoder;
1298 connector->encoder->crtc = new_conn_state->crtc;
1301 /* set legacy state in the crtc structure */
1302 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
1303 struct drm_plane *primary = crtc->primary;
1304 struct drm_plane_state *new_plane_state;
1306 crtc->mode = new_crtc_state->mode;
1307 crtc->enabled = new_crtc_state->enable;
1310 drm_atomic_get_new_plane_state(old_state, primary);
1312 if (new_plane_state && new_plane_state->crtc == crtc) {
1313 crtc->x = new_plane_state->src_x >> 16;
1314 crtc->y = new_plane_state->src_y >> 16;
1318 EXPORT_SYMBOL(drm_atomic_helper_update_legacy_modeset_state);
1321 * drm_atomic_helper_calc_timestamping_constants - update vblank timestamping constants
1322 * @state: atomic state object
1324 * Updates the timestamping constants used for precise vblank timestamps
1325 * by calling drm_calc_timestamping_constants() for all enabled crtcs in @state.
1327 void drm_atomic_helper_calc_timestamping_constants(struct drm_atomic_state *state)
1329 struct drm_crtc_state *new_crtc_state;
1330 struct drm_crtc *crtc;
1333 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
1334 if (new_crtc_state->enable)
1335 drm_calc_timestamping_constants(crtc,
1336 &new_crtc_state->adjusted_mode);
1339 EXPORT_SYMBOL(drm_atomic_helper_calc_timestamping_constants);
1342 crtc_set_mode(struct drm_device *dev, struct drm_atomic_state *old_state)
1344 struct drm_crtc *crtc;
1345 struct drm_crtc_state *new_crtc_state;
1346 struct drm_connector *connector;
1347 struct drm_connector_state *new_conn_state;
1350 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
1351 const struct drm_crtc_helper_funcs *funcs;
1353 if (!new_crtc_state->mode_changed)
1356 funcs = crtc->helper_private;
1358 if (new_crtc_state->enable && funcs->mode_set_nofb) {
1359 drm_dbg_atomic(dev, "modeset on [CRTC:%d:%s]\n",
1360 crtc->base.id, crtc->name);
1362 funcs->mode_set_nofb(crtc);
1366 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
1367 const struct drm_encoder_helper_funcs *funcs;
1368 struct drm_encoder *encoder;
1369 struct drm_display_mode *mode, *adjusted_mode;
1370 struct drm_bridge *bridge;
1372 if (!new_conn_state->best_encoder)
1375 encoder = new_conn_state->best_encoder;
1376 funcs = encoder->helper_private;
1377 new_crtc_state = new_conn_state->crtc->state;
1378 mode = &new_crtc_state->mode;
1379 adjusted_mode = &new_crtc_state->adjusted_mode;
1381 if (!new_crtc_state->mode_changed)
1384 drm_dbg_atomic(dev, "modeset on [ENCODER:%d:%s]\n",
1385 encoder->base.id, encoder->name);
1388 * Each encoder has at most one connector (since we always steal
1389 * it away), so we won't call mode_set hooks twice.
1391 if (funcs && funcs->atomic_mode_set) {
1392 funcs->atomic_mode_set(encoder, new_crtc_state,
1394 } else if (funcs && funcs->mode_set) {
1395 funcs->mode_set(encoder, mode, adjusted_mode);
1398 bridge = drm_bridge_chain_get_first_bridge(encoder);
1399 drm_bridge_chain_mode_set(bridge, mode, adjusted_mode);
1404 * drm_atomic_helper_commit_modeset_disables - modeset commit to disable outputs
1406 * @old_state: atomic state object with old state structures
1408 * This function shuts down all the outputs that need to be shut down and
1409 * prepares them (if required) with the new mode.
1411 * For compatibility with legacy CRTC helpers this should be called before
1412 * drm_atomic_helper_commit_planes(), which is what the default commit function
1413 * does. But drivers with different needs can group the modeset commits together
1414 * and do the plane commits at the end. This is useful for drivers doing runtime
1415 * PM since planes updates then only happen when the CRTC is actually enabled.
1417 void drm_atomic_helper_commit_modeset_disables(struct drm_device *dev,
1418 struct drm_atomic_state *old_state)
1420 disable_outputs(dev, old_state);
1422 drm_atomic_helper_update_legacy_modeset_state(dev, old_state);
1423 drm_atomic_helper_calc_timestamping_constants(old_state);
1425 crtc_set_mode(dev, old_state);
1427 EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_disables);
1429 static void drm_atomic_helper_commit_writebacks(struct drm_device *dev,
1430 struct drm_atomic_state *old_state)
1432 struct drm_connector *connector;
1433 struct drm_connector_state *new_conn_state;
1436 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
1437 const struct drm_connector_helper_funcs *funcs;
1439 funcs = connector->helper_private;
1440 if (!funcs->atomic_commit)
1443 if (new_conn_state->writeback_job && new_conn_state->writeback_job->fb) {
1444 WARN_ON(connector->connector_type != DRM_MODE_CONNECTOR_WRITEBACK);
1445 funcs->atomic_commit(connector, old_state);
1451 * drm_atomic_helper_commit_modeset_enables - modeset commit to enable outputs
1453 * @old_state: atomic state object with old state structures
1455 * This function enables all the outputs with the new configuration which had to
1456 * be turned off for the update.
1458 * For compatibility with legacy CRTC helpers this should be called after
1459 * drm_atomic_helper_commit_planes(), which is what the default commit function
1460 * does. But drivers with different needs can group the modeset commits together
1461 * and do the plane commits at the end. This is useful for drivers doing runtime
1462 * PM since planes updates then only happen when the CRTC is actually enabled.
1464 void drm_atomic_helper_commit_modeset_enables(struct drm_device *dev,
1465 struct drm_atomic_state *old_state)
1467 struct drm_crtc *crtc;
1468 struct drm_crtc_state *old_crtc_state;
1469 struct drm_crtc_state *new_crtc_state;
1470 struct drm_connector *connector;
1471 struct drm_connector_state *new_conn_state;
1474 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
1475 const struct drm_crtc_helper_funcs *funcs;
1477 /* Need to filter out CRTCs where only planes change. */
1478 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
1481 if (!new_crtc_state->active)
1484 funcs = crtc->helper_private;
1486 if (new_crtc_state->enable) {
1487 drm_dbg_atomic(dev, "enabling [CRTC:%d:%s]\n",
1488 crtc->base.id, crtc->name);
1489 if (funcs->atomic_enable)
1490 funcs->atomic_enable(crtc, old_state);
1491 else if (funcs->commit)
1492 funcs->commit(crtc);
1496 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
1497 const struct drm_encoder_helper_funcs *funcs;
1498 struct drm_encoder *encoder;
1499 struct drm_bridge *bridge;
1501 if (!new_conn_state->best_encoder)
1504 if (!new_conn_state->crtc->state->active ||
1505 !drm_atomic_crtc_needs_modeset(new_conn_state->crtc->state))
1508 encoder = new_conn_state->best_encoder;
1509 funcs = encoder->helper_private;
1511 drm_dbg_atomic(dev, "enabling [ENCODER:%d:%s]\n",
1512 encoder->base.id, encoder->name);
1515 * Each encoder has at most one connector (since we always steal
1516 * it away), so we won't call enable hooks twice.
1518 bridge = drm_bridge_chain_get_first_bridge(encoder);
1519 drm_atomic_bridge_chain_pre_enable(bridge, old_state);
1522 if (funcs->atomic_enable)
1523 funcs->atomic_enable(encoder, old_state);
1524 else if (funcs->enable)
1525 funcs->enable(encoder);
1526 else if (funcs->commit)
1527 funcs->commit(encoder);
1530 drm_atomic_bridge_chain_enable(bridge, old_state);
1533 drm_atomic_helper_commit_writebacks(dev, old_state);
1535 EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_enables);
1538 * For atomic updates which touch just a single CRTC, calculate the time of the
1539 * next vblank, and inform all the fences of the deadline.
1541 static void set_fence_deadline(struct drm_device *dev,
1542 struct drm_atomic_state *state)
1544 struct drm_crtc *crtc;
1545 struct drm_crtc_state *new_crtc_state;
1546 struct drm_plane *plane;
1547 struct drm_plane_state *new_plane_state;
1548 ktime_t vbltime = 0;
1551 for_each_new_crtc_in_state (state, crtc, new_crtc_state, i) {
1554 if (drm_atomic_crtc_needs_modeset(new_crtc_state))
1557 if (!new_crtc_state->active)
1560 if (drm_crtc_next_vblank_start(crtc, &v))
1563 if (!vbltime || ktime_before(v, vbltime))
1567 /* If no CRTCs updated, then nothing to do: */
1571 for_each_new_plane_in_state (state, plane, new_plane_state, i) {
1572 if (!new_plane_state->fence)
1574 dma_fence_set_deadline(new_plane_state->fence, vbltime);
1579 * drm_atomic_helper_wait_for_fences - wait for fences stashed in plane state
1581 * @state: atomic state object with old state structures
1582 * @pre_swap: If true, do an interruptible wait, and @state is the new state.
1583 * Otherwise @state is the old state.
1585 * For implicit sync, driver should fish the exclusive fence out from the
1586 * incoming fb's and stash it in the drm_plane_state. This is called after
1587 * drm_atomic_helper_swap_state() so it uses the current plane state (and
1588 * just uses the atomic state to find the changed planes)
1590 * Note that @pre_swap is needed since the point where we block for fences moves
1591 * around depending upon whether an atomic commit is blocking or
1592 * non-blocking. For non-blocking commit all waiting needs to happen after
1593 * drm_atomic_helper_swap_state() is called, but for blocking commits we want
1594 * to wait **before** we do anything that can't be easily rolled back. That is
1595 * before we call drm_atomic_helper_swap_state().
1597 * Returns zero if success or < 0 if dma_fence_wait() fails.
1599 int drm_atomic_helper_wait_for_fences(struct drm_device *dev,
1600 struct drm_atomic_state *state,
1603 struct drm_plane *plane;
1604 struct drm_plane_state *new_plane_state;
1607 set_fence_deadline(dev, state);
1609 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
1610 if (!new_plane_state->fence)
1613 WARN_ON(!new_plane_state->fb);
1616 * If waiting for fences pre-swap (ie: nonblock), userspace can
1617 * still interrupt the operation. Instead of blocking until the
1618 * timer expires, make the wait interruptible.
1620 ret = dma_fence_wait(new_plane_state->fence, pre_swap);
1624 dma_fence_put(new_plane_state->fence);
1625 new_plane_state->fence = NULL;
1630 EXPORT_SYMBOL(drm_atomic_helper_wait_for_fences);
1633 * drm_atomic_helper_wait_for_vblanks - wait for vblank on CRTCs
1635 * @old_state: atomic state object with old state structures
1637 * Helper to, after atomic commit, wait for vblanks on all affected
1638 * CRTCs (ie. before cleaning up old framebuffers using
1639 * drm_atomic_helper_cleanup_planes()). It will only wait on CRTCs where the
1640 * framebuffers have actually changed to optimize for the legacy cursor and
1641 * plane update use-case.
1643 * Drivers using the nonblocking commit tracking support initialized by calling
1644 * drm_atomic_helper_setup_commit() should look at
1645 * drm_atomic_helper_wait_for_flip_done() as an alternative.
1648 drm_atomic_helper_wait_for_vblanks(struct drm_device *dev,
1649 struct drm_atomic_state *old_state)
1651 struct drm_crtc *crtc;
1652 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
1654 unsigned int crtc_mask = 0;
1656 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
1657 if (!new_crtc_state->active)
1660 ret = drm_crtc_vblank_get(crtc);
1664 crtc_mask |= drm_crtc_mask(crtc);
1665 old_state->crtcs[i].last_vblank_count = drm_crtc_vblank_count(crtc);
1668 for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
1669 if (!(crtc_mask & drm_crtc_mask(crtc)))
1672 ret = wait_event_timeout(dev->vblank[i].queue,
1673 old_state->crtcs[i].last_vblank_count !=
1674 drm_crtc_vblank_count(crtc),
1675 msecs_to_jiffies(100));
1677 WARN(!ret, "[CRTC:%d:%s] vblank wait timed out\n",
1678 crtc->base.id, crtc->name);
1680 drm_crtc_vblank_put(crtc);
1683 EXPORT_SYMBOL(drm_atomic_helper_wait_for_vblanks);
1686 * drm_atomic_helper_wait_for_flip_done - wait for all page flips to be done
1688 * @old_state: atomic state object with old state structures
1690 * Helper to, after atomic commit, wait for page flips on all affected
1691 * crtcs (ie. before cleaning up old framebuffers using
1692 * drm_atomic_helper_cleanup_planes()). Compared to
1693 * drm_atomic_helper_wait_for_vblanks() this waits for the completion on all
1694 * CRTCs, assuming that cursors-only updates are signalling their completion
1695 * immediately (or using a different path).
1697 * This requires that drivers use the nonblocking commit tracking support
1698 * initialized using drm_atomic_helper_setup_commit().
1700 void drm_atomic_helper_wait_for_flip_done(struct drm_device *dev,
1701 struct drm_atomic_state *old_state)
1703 struct drm_crtc *crtc;
1706 for (i = 0; i < dev->mode_config.num_crtc; i++) {
1707 struct drm_crtc_commit *commit = old_state->crtcs[i].commit;
1710 crtc = old_state->crtcs[i].ptr;
1712 if (!crtc || !commit)
1715 ret = wait_for_completion_timeout(&commit->flip_done, 10 * HZ);
1717 drm_err(dev, "[CRTC:%d:%s] flip_done timed out\n",
1718 crtc->base.id, crtc->name);
1721 if (old_state->fake_commit)
1722 complete_all(&old_state->fake_commit->flip_done);
1724 EXPORT_SYMBOL(drm_atomic_helper_wait_for_flip_done);
1727 * drm_atomic_helper_commit_tail - commit atomic update to hardware
1728 * @old_state: atomic state object with old state structures
1730 * This is the default implementation for the
1731 * &drm_mode_config_helper_funcs.atomic_commit_tail hook, for drivers
1732 * that do not support runtime_pm or do not need the CRTC to be
1733 * enabled to perform a commit. Otherwise, see
1734 * drm_atomic_helper_commit_tail_rpm().
1736 * Note that the default ordering of how the various stages are called is to
1737 * match the legacy modeset helper library closest.
1739 void drm_atomic_helper_commit_tail(struct drm_atomic_state *old_state)
1741 struct drm_device *dev = old_state->dev;
1743 drm_atomic_helper_commit_modeset_disables(dev, old_state);
1745 drm_atomic_helper_commit_planes(dev, old_state, 0);
1747 drm_atomic_helper_commit_modeset_enables(dev, old_state);
1749 drm_atomic_helper_fake_vblank(old_state);
1751 drm_atomic_helper_commit_hw_done(old_state);
1753 drm_atomic_helper_wait_for_vblanks(dev, old_state);
1755 drm_atomic_helper_cleanup_planes(dev, old_state);
1757 EXPORT_SYMBOL(drm_atomic_helper_commit_tail);
1760 * drm_atomic_helper_commit_tail_rpm - commit atomic update to hardware
1761 * @old_state: new modeset state to be committed
1763 * This is an alternative implementation for the
1764 * &drm_mode_config_helper_funcs.atomic_commit_tail hook, for drivers
1765 * that support runtime_pm or need the CRTC to be enabled to perform a
1766 * commit. Otherwise, one should use the default implementation
1767 * drm_atomic_helper_commit_tail().
1769 void drm_atomic_helper_commit_tail_rpm(struct drm_atomic_state *old_state)
1771 struct drm_device *dev = old_state->dev;
1773 drm_atomic_helper_commit_modeset_disables(dev, old_state);
1775 drm_atomic_helper_commit_modeset_enables(dev, old_state);
1777 drm_atomic_helper_commit_planes(dev, old_state,
1778 DRM_PLANE_COMMIT_ACTIVE_ONLY);
1780 drm_atomic_helper_fake_vblank(old_state);
1782 drm_atomic_helper_commit_hw_done(old_state);
1784 drm_atomic_helper_wait_for_vblanks(dev, old_state);
1786 drm_atomic_helper_cleanup_planes(dev, old_state);
1788 EXPORT_SYMBOL(drm_atomic_helper_commit_tail_rpm);
1790 static void commit_tail(struct drm_atomic_state *old_state)
1792 struct drm_device *dev = old_state->dev;
1793 const struct drm_mode_config_helper_funcs *funcs;
1794 struct drm_crtc_state *new_crtc_state;
1795 struct drm_crtc *crtc;
1798 unsigned int i, new_self_refresh_mask = 0;
1800 funcs = dev->mode_config.helper_private;
1803 * We're measuring the _entire_ commit, so the time will vary depending
1804 * on how many fences and objects are involved. For the purposes of self
1805 * refresh, this is desirable since it'll give us an idea of how
1806 * congested things are. This will inform our decision on how often we
1807 * should enter self refresh after idle.
1809 * These times will be averaged out in the self refresh helpers to avoid
1810 * overreacting over one outlier frame
1812 start = ktime_get();
1814 drm_atomic_helper_wait_for_fences(dev, old_state, false);
1816 drm_atomic_helper_wait_for_dependencies(old_state);
1819 * We cannot safely access new_crtc_state after
1820 * drm_atomic_helper_commit_hw_done() so figure out which crtc's have
1821 * self-refresh active beforehand:
1823 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i)
1824 if (new_crtc_state->self_refresh_active)
1825 new_self_refresh_mask |= BIT(i);
1827 if (funcs && funcs->atomic_commit_tail)
1828 funcs->atomic_commit_tail(old_state);
1830 drm_atomic_helper_commit_tail(old_state);
1832 commit_time_ms = ktime_ms_delta(ktime_get(), start);
1833 if (commit_time_ms > 0)
1834 drm_self_refresh_helper_update_avg_times(old_state,
1835 (unsigned long)commit_time_ms,
1836 new_self_refresh_mask);
1838 drm_atomic_helper_commit_cleanup_done(old_state);
1840 drm_atomic_state_put(old_state);
1843 static void commit_work(struct work_struct *work)
1845 struct drm_atomic_state *state = container_of(work,
1846 struct drm_atomic_state,
1852 * drm_atomic_helper_async_check - check if state can be committed asynchronously
1854 * @state: the driver state object
1856 * This helper will check if it is possible to commit the state asynchronously.
1857 * Async commits are not supposed to swap the states like normal sync commits
1858 * but just do in-place changes on the current state.
1860 * It will return 0 if the commit can happen in an asynchronous fashion or error
1861 * if not. Note that error just mean it can't be committed asynchronously, if it
1862 * fails the commit should be treated like a normal synchronous commit.
1864 int drm_atomic_helper_async_check(struct drm_device *dev,
1865 struct drm_atomic_state *state)
1867 struct drm_crtc *crtc;
1868 struct drm_crtc_state *crtc_state;
1869 struct drm_plane *plane = NULL;
1870 struct drm_plane_state *old_plane_state = NULL;
1871 struct drm_plane_state *new_plane_state = NULL;
1872 const struct drm_plane_helper_funcs *funcs;
1873 int i, ret, n_planes = 0;
1875 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
1876 if (drm_atomic_crtc_needs_modeset(crtc_state))
1880 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i)
1883 /* FIXME: we support only single plane updates for now */
1884 if (n_planes != 1) {
1886 "only single plane async updates are supported\n");
1890 if (!new_plane_state->crtc ||
1891 old_plane_state->crtc != new_plane_state->crtc) {
1893 "[PLANE:%d:%s] async update cannot change CRTC\n",
1894 plane->base.id, plane->name);
1898 funcs = plane->helper_private;
1899 if (!funcs->atomic_async_update) {
1901 "[PLANE:%d:%s] driver does not support async updates\n",
1902 plane->base.id, plane->name);
1906 if (new_plane_state->fence) {
1908 "[PLANE:%d:%s] missing fence for async update\n",
1909 plane->base.id, plane->name);
1914 * Don't do an async update if there is an outstanding commit modifying
1915 * the plane. This prevents our async update's changes from getting
1916 * overridden by a previous synchronous update's state.
1918 if (old_plane_state->commit &&
1919 !try_wait_for_completion(&old_plane_state->commit->hw_done)) {
1921 "[PLANE:%d:%s] inflight previous commit preventing async commit\n",
1922 plane->base.id, plane->name);
1926 ret = funcs->atomic_async_check(plane, state);
1929 "[PLANE:%d:%s] driver async check failed\n",
1930 plane->base.id, plane->name);
1933 EXPORT_SYMBOL(drm_atomic_helper_async_check);
1936 * drm_atomic_helper_async_commit - commit state asynchronously
1938 * @state: the driver state object
1940 * This function commits a state asynchronously, i.e., not vblank
1941 * synchronized. It should be used on a state only when
1942 * drm_atomic_async_check() succeeds. Async commits are not supposed to swap
1943 * the states like normal sync commits, but just do in-place changes on the
1946 * TODO: Implement full swap instead of doing in-place changes.
1948 void drm_atomic_helper_async_commit(struct drm_device *dev,
1949 struct drm_atomic_state *state)
1951 struct drm_plane *plane;
1952 struct drm_plane_state *plane_state;
1953 const struct drm_plane_helper_funcs *funcs;
1956 for_each_new_plane_in_state(state, plane, plane_state, i) {
1957 struct drm_framebuffer *new_fb = plane_state->fb;
1958 struct drm_framebuffer *old_fb = plane->state->fb;
1960 funcs = plane->helper_private;
1961 funcs->atomic_async_update(plane, state);
1964 * ->atomic_async_update() is supposed to update the
1965 * plane->state in-place, make sure at least common
1966 * properties have been properly updated.
1968 WARN_ON_ONCE(plane->state->fb != new_fb);
1969 WARN_ON_ONCE(plane->state->crtc_x != plane_state->crtc_x);
1970 WARN_ON_ONCE(plane->state->crtc_y != plane_state->crtc_y);
1971 WARN_ON_ONCE(plane->state->src_x != plane_state->src_x);
1972 WARN_ON_ONCE(plane->state->src_y != plane_state->src_y);
1975 * Make sure the FBs have been swapped so that cleanups in the
1976 * new_state performs a cleanup in the old FB.
1978 WARN_ON_ONCE(plane_state->fb != old_fb);
1981 EXPORT_SYMBOL(drm_atomic_helper_async_commit);
1984 * drm_atomic_helper_commit - commit validated state object
1986 * @state: the driver state object
1987 * @nonblock: whether nonblocking behavior is requested.
1989 * This function commits a with drm_atomic_helper_check() pre-validated state
1990 * object. This can still fail when e.g. the framebuffer reservation fails. This
1991 * function implements nonblocking commits, using
1992 * drm_atomic_helper_setup_commit() and related functions.
1994 * Committing the actual hardware state is done through the
1995 * &drm_mode_config_helper_funcs.atomic_commit_tail callback, or its default
1996 * implementation drm_atomic_helper_commit_tail().
1999 * Zero for success or -errno.
2001 int drm_atomic_helper_commit(struct drm_device *dev,
2002 struct drm_atomic_state *state,
2007 if (state->async_update) {
2008 ret = drm_atomic_helper_prepare_planes(dev, state);
2012 drm_atomic_helper_async_commit(dev, state);
2013 drm_atomic_helper_unprepare_planes(dev, state);
2018 ret = drm_atomic_helper_setup_commit(state, nonblock);
2022 INIT_WORK(&state->commit_work, commit_work);
2024 ret = drm_atomic_helper_prepare_planes(dev, state);
2029 ret = drm_atomic_helper_wait_for_fences(dev, state, true);
2035 * This is the point of no return - everything below never fails except
2036 * when the hw goes bonghits. Which means we can commit the new state on
2037 * the software side now.
2040 ret = drm_atomic_helper_swap_state(state, true);
2045 * Everything below can be run asynchronously without the need to grab
2046 * any modeset locks at all under one condition: It must be guaranteed
2047 * that the asynchronous work has either been cancelled (if the driver
2048 * supports it, which at least requires that the framebuffers get
2049 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
2050 * before the new state gets committed on the software side with
2051 * drm_atomic_helper_swap_state().
2053 * This scheme allows new atomic state updates to be prepared and
2054 * checked in parallel to the asynchronous completion of the previous
2055 * update. Which is important since compositors need to figure out the
2056 * composition of the next frame right after having submitted the
2059 * NOTE: Commit work has multiple phases, first hardware commit, then
2060 * cleanup. We want them to overlap, hence need system_unbound_wq to
2061 * make sure work items don't artificially stall on each another.
2064 drm_atomic_state_get(state);
2066 queue_work(system_unbound_wq, &state->commit_work);
2073 drm_atomic_helper_unprepare_planes(dev, state);
2076 EXPORT_SYMBOL(drm_atomic_helper_commit);
2079 * DOC: implementing nonblocking commit
2081 * Nonblocking atomic commits should use struct &drm_crtc_commit to sequence
2082 * different operations against each another. Locks, especially struct
2083 * &drm_modeset_lock, should not be held in worker threads or any other
2084 * asynchronous context used to commit the hardware state.
2086 * drm_atomic_helper_commit() implements the recommended sequence for
2087 * nonblocking commits, using drm_atomic_helper_setup_commit() internally:
2089 * 1. Run drm_atomic_helper_prepare_planes(). Since this can fail and we
2090 * need to propagate out of memory/VRAM errors to userspace, it must be called
2093 * 2. Synchronize with any outstanding nonblocking commit worker threads which
2094 * might be affected by the new state update. This is handled by
2095 * drm_atomic_helper_setup_commit().
2097 * Asynchronous workers need to have sufficient parallelism to be able to run
2098 * different atomic commits on different CRTCs in parallel. The simplest way to
2099 * achieve this is by running them on the &system_unbound_wq work queue. Note
2100 * that drivers are not required to split up atomic commits and run an
2101 * individual commit in parallel - userspace is supposed to do that if it cares.
2102 * But it might be beneficial to do that for modesets, since those necessarily
2103 * must be done as one global operation, and enabling or disabling a CRTC can
2104 * take a long time. But even that is not required.
2106 * IMPORTANT: A &drm_atomic_state update for multiple CRTCs is sequenced
2107 * against all CRTCs therein. Therefore for atomic state updates which only flip
2108 * planes the driver must not get the struct &drm_crtc_state of unrelated CRTCs
2109 * in its atomic check code: This would prevent committing of atomic updates to
2110 * multiple CRTCs in parallel. In general, adding additional state structures
2111 * should be avoided as much as possible, because this reduces parallelism in
2112 * (nonblocking) commits, both due to locking and due to commit sequencing
2115 * 3. The software state is updated synchronously with
2116 * drm_atomic_helper_swap_state(). Doing this under the protection of all modeset
2117 * locks means concurrent callers never see inconsistent state. Note that commit
2118 * workers do not hold any locks; their access is only coordinated through
2119 * ordering. If workers would access state only through the pointers in the
2120 * free-standing state objects (currently not the case for any driver) then even
2121 * multiple pending commits could be in-flight at the same time.
2123 * 4. Schedule a work item to do all subsequent steps, using the split-out
2124 * commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and
2125 * then cleaning up the framebuffers after the old framebuffer is no longer
2126 * being displayed. The scheduled work should synchronize against other workers
2127 * using the &drm_crtc_commit infrastructure as needed. See
2128 * drm_atomic_helper_setup_commit() for more details.
2131 static int stall_checks(struct drm_crtc *crtc, bool nonblock)
2133 struct drm_crtc_commit *commit, *stall_commit = NULL;
2134 bool completed = true;
2138 spin_lock(&crtc->commit_lock);
2140 list_for_each_entry(commit, &crtc->commit_list, commit_entry) {
2142 completed = try_wait_for_completion(&commit->flip_done);
2144 * Userspace is not allowed to get ahead of the previous
2145 * commit with nonblocking ones.
2147 if (!completed && nonblock) {
2148 spin_unlock(&crtc->commit_lock);
2149 drm_dbg_atomic(crtc->dev,
2150 "[CRTC:%d:%s] busy with a previous commit\n",
2151 crtc->base.id, crtc->name);
2155 } else if (i == 1) {
2156 stall_commit = drm_crtc_commit_get(commit);
2162 spin_unlock(&crtc->commit_lock);
2167 /* We don't want to let commits get ahead of cleanup work too much,
2168 * stalling on 2nd previous commit means triple-buffer won't ever stall.
2170 ret = wait_for_completion_interruptible_timeout(&stall_commit->cleanup_done,
2173 drm_err(crtc->dev, "[CRTC:%d:%s] cleanup_done timed out\n",
2174 crtc->base.id, crtc->name);
2176 drm_crtc_commit_put(stall_commit);
2178 return ret < 0 ? ret : 0;
2181 static void release_crtc_commit(struct completion *completion)
2183 struct drm_crtc_commit *commit = container_of(completion,
2187 drm_crtc_commit_put(commit);
2190 static void init_commit(struct drm_crtc_commit *commit, struct drm_crtc *crtc)
2192 init_completion(&commit->flip_done);
2193 init_completion(&commit->hw_done);
2194 init_completion(&commit->cleanup_done);
2195 INIT_LIST_HEAD(&commit->commit_entry);
2196 kref_init(&commit->ref);
2197 commit->crtc = crtc;
2200 static struct drm_crtc_commit *
2201 crtc_or_fake_commit(struct drm_atomic_state *state, struct drm_crtc *crtc)
2204 struct drm_crtc_state *new_crtc_state;
2206 new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
2208 return new_crtc_state->commit;
2211 if (!state->fake_commit) {
2212 state->fake_commit = kzalloc(sizeof(*state->fake_commit), GFP_KERNEL);
2213 if (!state->fake_commit)
2216 init_commit(state->fake_commit, NULL);
2219 return state->fake_commit;
2223 * drm_atomic_helper_setup_commit - setup possibly nonblocking commit
2224 * @state: new modeset state to be committed
2225 * @nonblock: whether nonblocking behavior is requested.
2227 * This function prepares @state to be used by the atomic helper's support for
2228 * nonblocking commits. Drivers using the nonblocking commit infrastructure
2229 * should always call this function from their
2230 * &drm_mode_config_funcs.atomic_commit hook.
2232 * Drivers that need to extend the commit setup to private objects can use the
2233 * &drm_mode_config_helper_funcs.atomic_commit_setup hook.
2235 * To be able to use this support drivers need to use a few more helper
2236 * functions. drm_atomic_helper_wait_for_dependencies() must be called before
2237 * actually committing the hardware state, and for nonblocking commits this call
2238 * must be placed in the async worker. See also drm_atomic_helper_swap_state()
2239 * and its stall parameter, for when a driver's commit hooks look at the
2240 * &drm_crtc.state, &drm_plane.state or &drm_connector.state pointer directly.
2242 * Completion of the hardware commit step must be signalled using
2243 * drm_atomic_helper_commit_hw_done(). After this step the driver is not allowed
2244 * to read or change any permanent software or hardware modeset state. The only
2245 * exception is state protected by other means than &drm_modeset_lock locks.
2246 * Only the free standing @state with pointers to the old state structures can
2247 * be inspected, e.g. to clean up old buffers using
2248 * drm_atomic_helper_cleanup_planes().
2250 * At the very end, before cleaning up @state drivers must call
2251 * drm_atomic_helper_commit_cleanup_done().
2253 * This is all implemented by in drm_atomic_helper_commit(), giving drivers a
2254 * complete and easy-to-use default implementation of the atomic_commit() hook.
2256 * The tracking of asynchronously executed and still pending commits is done
2257 * using the core structure &drm_crtc_commit.
2259 * By default there's no need to clean up resources allocated by this function
2260 * explicitly: drm_atomic_state_default_clear() will take care of that
2265 * 0 on success. -EBUSY when userspace schedules nonblocking commits too fast,
2266 * -ENOMEM on allocation failures and -EINTR when a signal is pending.
2268 int drm_atomic_helper_setup_commit(struct drm_atomic_state *state,
2271 struct drm_crtc *crtc;
2272 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
2273 struct drm_connector *conn;
2274 struct drm_connector_state *old_conn_state, *new_conn_state;
2275 struct drm_plane *plane;
2276 struct drm_plane_state *old_plane_state, *new_plane_state;
2277 struct drm_crtc_commit *commit;
2278 const struct drm_mode_config_helper_funcs *funcs;
2281 funcs = state->dev->mode_config.helper_private;
2283 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
2284 commit = kzalloc(sizeof(*commit), GFP_KERNEL);
2288 init_commit(commit, crtc);
2290 new_crtc_state->commit = commit;
2292 ret = stall_checks(crtc, nonblock);
2297 * Drivers only send out events when at least either current or
2298 * new CRTC state is active. Complete right away if everything
2301 if (!old_crtc_state->active && !new_crtc_state->active) {
2302 complete_all(&commit->flip_done);
2306 if (!new_crtc_state->event) {
2307 commit->event = kzalloc(sizeof(*commit->event),
2312 new_crtc_state->event = commit->event;
2315 new_crtc_state->event->base.completion = &commit->flip_done;
2316 new_crtc_state->event->base.completion_release = release_crtc_commit;
2317 drm_crtc_commit_get(commit);
2319 commit->abort_completion = true;
2321 state->crtcs[i].commit = commit;
2322 drm_crtc_commit_get(commit);
2325 for_each_oldnew_connector_in_state(state, conn, old_conn_state, new_conn_state, i) {
2327 * Userspace is not allowed to get ahead of the previous
2328 * commit with nonblocking ones.
2330 if (nonblock && old_conn_state->commit &&
2331 !try_wait_for_completion(&old_conn_state->commit->flip_done)) {
2332 drm_dbg_atomic(conn->dev,
2333 "[CONNECTOR:%d:%s] busy with a previous commit\n",
2334 conn->base.id, conn->name);
2339 /* Always track connectors explicitly for e.g. link retraining. */
2340 commit = crtc_or_fake_commit(state, new_conn_state->crtc ?: old_conn_state->crtc);
2344 new_conn_state->commit = drm_crtc_commit_get(commit);
2347 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
2349 * Userspace is not allowed to get ahead of the previous
2350 * commit with nonblocking ones.
2352 if (nonblock && old_plane_state->commit &&
2353 !try_wait_for_completion(&old_plane_state->commit->flip_done)) {
2354 drm_dbg_atomic(plane->dev,
2355 "[PLANE:%d:%s] busy with a previous commit\n",
2356 plane->base.id, plane->name);
2361 /* Always track planes explicitly for async pageflip support. */
2362 commit = crtc_or_fake_commit(state, new_plane_state->crtc ?: old_plane_state->crtc);
2366 new_plane_state->commit = drm_crtc_commit_get(commit);
2369 if (funcs && funcs->atomic_commit_setup)
2370 return funcs->atomic_commit_setup(state);
2374 EXPORT_SYMBOL(drm_atomic_helper_setup_commit);
2377 * drm_atomic_helper_wait_for_dependencies - wait for required preceeding commits
2378 * @old_state: atomic state object with old state structures
2380 * This function waits for all preceeding commits that touch the same CRTC as
2381 * @old_state to both be committed to the hardware (as signalled by
2382 * drm_atomic_helper_commit_hw_done()) and executed by the hardware (as signalled
2383 * by calling drm_crtc_send_vblank_event() on the &drm_crtc_state.event).
2385 * This is part of the atomic helper support for nonblocking commits, see
2386 * drm_atomic_helper_setup_commit() for an overview.
2388 void drm_atomic_helper_wait_for_dependencies(struct drm_atomic_state *old_state)
2390 struct drm_crtc *crtc;
2391 struct drm_crtc_state *old_crtc_state;
2392 struct drm_plane *plane;
2393 struct drm_plane_state *old_plane_state;
2394 struct drm_connector *conn;
2395 struct drm_connector_state *old_conn_state;
2399 for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
2400 ret = drm_crtc_commit_wait(old_crtc_state->commit);
2403 "[CRTC:%d:%s] commit wait timed out\n",
2404 crtc->base.id, crtc->name);
2407 for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
2408 ret = drm_crtc_commit_wait(old_conn_state->commit);
2411 "[CONNECTOR:%d:%s] commit wait timed out\n",
2412 conn->base.id, conn->name);
2415 for_each_old_plane_in_state(old_state, plane, old_plane_state, i) {
2416 ret = drm_crtc_commit_wait(old_plane_state->commit);
2419 "[PLANE:%d:%s] commit wait timed out\n",
2420 plane->base.id, plane->name);
2423 EXPORT_SYMBOL(drm_atomic_helper_wait_for_dependencies);
2426 * drm_atomic_helper_fake_vblank - fake VBLANK events if needed
2427 * @old_state: atomic state object with old state structures
2429 * This function walks all CRTCs and fakes VBLANK events on those with
2430 * &drm_crtc_state.no_vblank set to true and &drm_crtc_state.event != NULL.
2431 * The primary use of this function is writeback connectors working in oneshot
2432 * mode and faking VBLANK events. In this case they only fake the VBLANK event
2433 * when a job is queued, and any change to the pipeline that does not touch the
2434 * connector is leading to timeouts when calling
2435 * drm_atomic_helper_wait_for_vblanks() or
2436 * drm_atomic_helper_wait_for_flip_done(). In addition to writeback
2437 * connectors, this function can also fake VBLANK events for CRTCs without
2440 * This is part of the atomic helper support for nonblocking commits, see
2441 * drm_atomic_helper_setup_commit() for an overview.
2443 void drm_atomic_helper_fake_vblank(struct drm_atomic_state *old_state)
2445 struct drm_crtc_state *new_crtc_state;
2446 struct drm_crtc *crtc;
2449 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
2450 unsigned long flags;
2452 if (!new_crtc_state->no_vblank)
2455 spin_lock_irqsave(&old_state->dev->event_lock, flags);
2456 if (new_crtc_state->event) {
2457 drm_crtc_send_vblank_event(crtc,
2458 new_crtc_state->event);
2459 new_crtc_state->event = NULL;
2461 spin_unlock_irqrestore(&old_state->dev->event_lock, flags);
2464 EXPORT_SYMBOL(drm_atomic_helper_fake_vblank);
2467 * drm_atomic_helper_commit_hw_done - setup possible nonblocking commit
2468 * @old_state: atomic state object with old state structures
2470 * This function is used to signal completion of the hardware commit step. After
2471 * this step the driver is not allowed to read or change any permanent software
2472 * or hardware modeset state. The only exception is state protected by other
2473 * means than &drm_modeset_lock locks.
2475 * Drivers should try to postpone any expensive or delayed cleanup work after
2476 * this function is called.
2478 * This is part of the atomic helper support for nonblocking commits, see
2479 * drm_atomic_helper_setup_commit() for an overview.
2481 void drm_atomic_helper_commit_hw_done(struct drm_atomic_state *old_state)
2483 struct drm_crtc *crtc;
2484 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
2485 struct drm_crtc_commit *commit;
2488 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
2489 commit = new_crtc_state->commit;
2494 * copy new_crtc_state->commit to old_crtc_state->commit,
2495 * it's unsafe to touch new_crtc_state after hw_done,
2496 * but we still need to do so in cleanup_done().
2498 if (old_crtc_state->commit)
2499 drm_crtc_commit_put(old_crtc_state->commit);
2501 old_crtc_state->commit = drm_crtc_commit_get(commit);
2503 /* backend must have consumed any event by now */
2504 WARN_ON(new_crtc_state->event);
2505 complete_all(&commit->hw_done);
2508 if (old_state->fake_commit) {
2509 complete_all(&old_state->fake_commit->hw_done);
2510 complete_all(&old_state->fake_commit->flip_done);
2513 EXPORT_SYMBOL(drm_atomic_helper_commit_hw_done);
2516 * drm_atomic_helper_commit_cleanup_done - signal completion of commit
2517 * @old_state: atomic state object with old state structures
2519 * This signals completion of the atomic update @old_state, including any
2520 * cleanup work. If used, it must be called right before calling
2521 * drm_atomic_state_put().
2523 * This is part of the atomic helper support for nonblocking commits, see
2524 * drm_atomic_helper_setup_commit() for an overview.
2526 void drm_atomic_helper_commit_cleanup_done(struct drm_atomic_state *old_state)
2528 struct drm_crtc *crtc;
2529 struct drm_crtc_state *old_crtc_state;
2530 struct drm_crtc_commit *commit;
2533 for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
2534 commit = old_crtc_state->commit;
2535 if (WARN_ON(!commit))
2538 complete_all(&commit->cleanup_done);
2539 WARN_ON(!try_wait_for_completion(&commit->hw_done));
2541 spin_lock(&crtc->commit_lock);
2542 list_del(&commit->commit_entry);
2543 spin_unlock(&crtc->commit_lock);
2546 if (old_state->fake_commit) {
2547 complete_all(&old_state->fake_commit->cleanup_done);
2548 WARN_ON(!try_wait_for_completion(&old_state->fake_commit->hw_done));
2551 EXPORT_SYMBOL(drm_atomic_helper_commit_cleanup_done);
2554 * drm_atomic_helper_prepare_planes - prepare plane resources before commit
2556 * @state: atomic state object with new state structures
2558 * This function prepares plane state, specifically framebuffers, for the new
2559 * configuration, by calling &drm_plane_helper_funcs.prepare_fb. If any failure
2560 * is encountered this function will call &drm_plane_helper_funcs.cleanup_fb on
2561 * any already successfully prepared framebuffer.
2564 * 0 on success, negative error code on failure.
2566 int drm_atomic_helper_prepare_planes(struct drm_device *dev,
2567 struct drm_atomic_state *state)
2569 struct drm_connector *connector;
2570 struct drm_connector_state *new_conn_state;
2571 struct drm_plane *plane;
2572 struct drm_plane_state *new_plane_state;
2575 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
2576 if (!new_conn_state->writeback_job)
2579 ret = drm_writeback_prepare_job(new_conn_state->writeback_job);
2584 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
2585 const struct drm_plane_helper_funcs *funcs;
2587 funcs = plane->helper_private;
2589 if (funcs->prepare_fb) {
2590 ret = funcs->prepare_fb(plane, new_plane_state);
2592 goto fail_prepare_fb;
2594 WARN_ON_ONCE(funcs->cleanup_fb);
2596 if (!drm_core_check_feature(dev, DRIVER_GEM))
2599 ret = drm_gem_plane_helper_prepare_fb(plane, new_plane_state);
2601 goto fail_prepare_fb;
2605 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
2606 const struct drm_plane_helper_funcs *funcs = plane->helper_private;
2608 if (funcs->begin_fb_access) {
2609 ret = funcs->begin_fb_access(plane, new_plane_state);
2611 goto fail_begin_fb_access;
2617 fail_begin_fb_access:
2618 for_each_new_plane_in_state(state, plane, new_plane_state, j) {
2619 const struct drm_plane_helper_funcs *funcs = plane->helper_private;
2624 if (funcs->end_fb_access)
2625 funcs->end_fb_access(plane, new_plane_state);
2627 i = j; /* set i to upper limit to cleanup all planes */
2629 for_each_new_plane_in_state(state, plane, new_plane_state, j) {
2630 const struct drm_plane_helper_funcs *funcs;
2635 funcs = plane->helper_private;
2637 if (funcs->cleanup_fb)
2638 funcs->cleanup_fb(plane, new_plane_state);
2643 EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
2646 * drm_atomic_helper_unprepare_planes - release plane resources on aborts
2648 * @state: atomic state object with old state structures
2650 * This function cleans up plane state, specifically framebuffers, from the
2651 * atomic state. It undoes the effects of drm_atomic_helper_prepare_planes()
2652 * when aborting an atomic commit. For cleaning up after a successful commit
2653 * use drm_atomic_helper_cleanup_planes().
2655 void drm_atomic_helper_unprepare_planes(struct drm_device *dev,
2656 struct drm_atomic_state *state)
2658 struct drm_plane *plane;
2659 struct drm_plane_state *new_plane_state;
2662 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
2663 const struct drm_plane_helper_funcs *funcs = plane->helper_private;
2665 if (funcs->end_fb_access)
2666 funcs->end_fb_access(plane, new_plane_state);
2669 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
2670 const struct drm_plane_helper_funcs *funcs = plane->helper_private;
2672 if (funcs->cleanup_fb)
2673 funcs->cleanup_fb(plane, new_plane_state);
2676 EXPORT_SYMBOL(drm_atomic_helper_unprepare_planes);
2678 static bool plane_crtc_active(const struct drm_plane_state *state)
2680 return state->crtc && state->crtc->state->active;
2684 * drm_atomic_helper_commit_planes - commit plane state
2686 * @old_state: atomic state object with old state structures
2687 * @flags: flags for committing plane state
2689 * This function commits the new plane state using the plane and atomic helper
2690 * functions for planes and CRTCs. It assumes that the atomic state has already
2691 * been pushed into the relevant object state pointers, since this step can no
2694 * It still requires the global state object @old_state to know which planes and
2695 * crtcs need to be updated though.
2697 * Note that this function does all plane updates across all CRTCs in one step.
2698 * If the hardware can't support this approach look at
2699 * drm_atomic_helper_commit_planes_on_crtc() instead.
2701 * Plane parameters can be updated by applications while the associated CRTC is
2702 * disabled. The DRM/KMS core will store the parameters in the plane state,
2703 * which will be available to the driver when the CRTC is turned on. As a result
2704 * most drivers don't need to be immediately notified of plane updates for a
2707 * Unless otherwise needed, drivers are advised to set the ACTIVE_ONLY flag in
2708 * @flags in order not to receive plane update notifications related to a
2709 * disabled CRTC. This avoids the need to manually ignore plane updates in
2710 * driver code when the driver and/or hardware can't or just don't need to deal
2711 * with updates on disabled CRTCs, for example when supporting runtime PM.
2713 * Drivers may set the NO_DISABLE_AFTER_MODESET flag in @flags if the relevant
2714 * display controllers require to disable a CRTC's planes when the CRTC is
2715 * disabled. This function would skip the &drm_plane_helper_funcs.atomic_disable
2716 * call for a plane if the CRTC of the old plane state needs a modesetting
2717 * operation. Of course, the drivers need to disable the planes in their CRTC
2718 * disable callbacks since no one else would do that.
2720 * The drm_atomic_helper_commit() default implementation doesn't set the
2721 * ACTIVE_ONLY flag to most closely match the behaviour of the legacy helpers.
2722 * This should not be copied blindly by drivers.
2724 void drm_atomic_helper_commit_planes(struct drm_device *dev,
2725 struct drm_atomic_state *old_state,
2728 struct drm_crtc *crtc;
2729 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
2730 struct drm_plane *plane;
2731 struct drm_plane_state *old_plane_state, *new_plane_state;
2733 bool active_only = flags & DRM_PLANE_COMMIT_ACTIVE_ONLY;
2734 bool no_disable = flags & DRM_PLANE_COMMIT_NO_DISABLE_AFTER_MODESET;
2736 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
2737 const struct drm_crtc_helper_funcs *funcs;
2739 funcs = crtc->helper_private;
2741 if (!funcs || !funcs->atomic_begin)
2744 if (active_only && !new_crtc_state->active)
2747 funcs->atomic_begin(crtc, old_state);
2750 for_each_oldnew_plane_in_state(old_state, plane, old_plane_state, new_plane_state, i) {
2751 const struct drm_plane_helper_funcs *funcs;
2754 funcs = plane->helper_private;
2759 disabling = drm_atomic_plane_disabling(old_plane_state,
2764 * Skip planes related to inactive CRTCs. If the plane
2765 * is enabled use the state of the current CRTC. If the
2766 * plane is being disabled use the state of the old
2767 * CRTC to avoid skipping planes being disabled on an
2770 if (!disabling && !plane_crtc_active(new_plane_state))
2772 if (disabling && !plane_crtc_active(old_plane_state))
2777 * Special-case disabling the plane if drivers support it.
2779 if (disabling && funcs->atomic_disable) {
2780 struct drm_crtc_state *crtc_state;
2782 crtc_state = old_plane_state->crtc->state;
2784 if (drm_atomic_crtc_needs_modeset(crtc_state) &&
2788 funcs->atomic_disable(plane, old_state);
2789 } else if (new_plane_state->crtc || disabling) {
2790 funcs->atomic_update(plane, old_state);
2792 if (!disabling && funcs->atomic_enable) {
2793 if (drm_atomic_plane_enabling(old_plane_state, new_plane_state))
2794 funcs->atomic_enable(plane, old_state);
2799 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
2800 const struct drm_crtc_helper_funcs *funcs;
2802 funcs = crtc->helper_private;
2804 if (!funcs || !funcs->atomic_flush)
2807 if (active_only && !new_crtc_state->active)
2810 funcs->atomic_flush(crtc, old_state);
2814 * Signal end of framebuffer access here before hw_done. After hw_done,
2815 * a later commit might have already released the plane state.
2817 for_each_old_plane_in_state(old_state, plane, old_plane_state, i) {
2818 const struct drm_plane_helper_funcs *funcs = plane->helper_private;
2820 if (funcs->end_fb_access)
2821 funcs->end_fb_access(plane, old_plane_state);
2824 EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
2827 * drm_atomic_helper_commit_planes_on_crtc - commit plane state for a CRTC
2828 * @old_crtc_state: atomic state object with the old CRTC state
2830 * This function commits the new plane state using the plane and atomic helper
2831 * functions for planes on the specific CRTC. It assumes that the atomic state
2832 * has already been pushed into the relevant object state pointers, since this
2833 * step can no longer fail.
2835 * This function is useful when plane updates should be done CRTC-by-CRTC
2836 * instead of one global step like drm_atomic_helper_commit_planes() does.
2838 * This function can only be savely used when planes are not allowed to move
2839 * between different CRTCs because this function doesn't handle inter-CRTC
2840 * dependencies. Callers need to ensure that either no such dependencies exist,
2841 * resolve them through ordering of commit calls or through some other means.
2844 drm_atomic_helper_commit_planes_on_crtc(struct drm_crtc_state *old_crtc_state)
2846 const struct drm_crtc_helper_funcs *crtc_funcs;
2847 struct drm_crtc *crtc = old_crtc_state->crtc;
2848 struct drm_atomic_state *old_state = old_crtc_state->state;
2849 struct drm_crtc_state *new_crtc_state =
2850 drm_atomic_get_new_crtc_state(old_state, crtc);
2851 struct drm_plane *plane;
2852 unsigned int plane_mask;
2854 plane_mask = old_crtc_state->plane_mask;
2855 plane_mask |= new_crtc_state->plane_mask;
2857 crtc_funcs = crtc->helper_private;
2858 if (crtc_funcs && crtc_funcs->atomic_begin)
2859 crtc_funcs->atomic_begin(crtc, old_state);
2861 drm_for_each_plane_mask(plane, crtc->dev, plane_mask) {
2862 struct drm_plane_state *old_plane_state =
2863 drm_atomic_get_old_plane_state(old_state, plane);
2864 struct drm_plane_state *new_plane_state =
2865 drm_atomic_get_new_plane_state(old_state, plane);
2866 const struct drm_plane_helper_funcs *plane_funcs;
2869 plane_funcs = plane->helper_private;
2871 if (!old_plane_state || !plane_funcs)
2874 WARN_ON(new_plane_state->crtc &&
2875 new_plane_state->crtc != crtc);
2877 disabling = drm_atomic_plane_disabling(old_plane_state, new_plane_state);
2879 if (disabling && plane_funcs->atomic_disable) {
2880 plane_funcs->atomic_disable(plane, old_state);
2881 } else if (new_plane_state->crtc || disabling) {
2882 plane_funcs->atomic_update(plane, old_state);
2884 if (!disabling && plane_funcs->atomic_enable) {
2885 if (drm_atomic_plane_enabling(old_plane_state, new_plane_state))
2886 plane_funcs->atomic_enable(plane, old_state);
2891 if (crtc_funcs && crtc_funcs->atomic_flush)
2892 crtc_funcs->atomic_flush(crtc, old_state);
2894 EXPORT_SYMBOL(drm_atomic_helper_commit_planes_on_crtc);
2897 * drm_atomic_helper_disable_planes_on_crtc - helper to disable CRTC's planes
2898 * @old_crtc_state: atomic state object with the old CRTC state
2899 * @atomic: if set, synchronize with CRTC's atomic_begin/flush hooks
2901 * Disables all planes associated with the given CRTC. This can be
2902 * used for instance in the CRTC helper atomic_disable callback to disable
2905 * If the atomic-parameter is set the function calls the CRTC's
2906 * atomic_begin hook before and atomic_flush hook after disabling the
2909 * It is a bug to call this function without having implemented the
2910 * &drm_plane_helper_funcs.atomic_disable plane hook.
2913 drm_atomic_helper_disable_planes_on_crtc(struct drm_crtc_state *old_crtc_state,
2916 struct drm_crtc *crtc = old_crtc_state->crtc;
2917 const struct drm_crtc_helper_funcs *crtc_funcs =
2918 crtc->helper_private;
2919 struct drm_plane *plane;
2921 if (atomic && crtc_funcs && crtc_funcs->atomic_begin)
2922 crtc_funcs->atomic_begin(crtc, NULL);
2924 drm_atomic_crtc_state_for_each_plane(plane, old_crtc_state) {
2925 const struct drm_plane_helper_funcs *plane_funcs =
2926 plane->helper_private;
2931 WARN_ON(!plane_funcs->atomic_disable);
2932 if (plane_funcs->atomic_disable)
2933 plane_funcs->atomic_disable(plane, NULL);
2936 if (atomic && crtc_funcs && crtc_funcs->atomic_flush)
2937 crtc_funcs->atomic_flush(crtc, NULL);
2939 EXPORT_SYMBOL(drm_atomic_helper_disable_planes_on_crtc);
2942 * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
2944 * @old_state: atomic state object with old state structures
2946 * This function cleans up plane state, specifically framebuffers, from the old
2947 * configuration. Hence the old configuration must be perserved in @old_state to
2948 * be able to call this function.
2950 * This function may not be called on the new state when the atomic update
2951 * fails at any point after calling drm_atomic_helper_prepare_planes(). Use
2952 * drm_atomic_helper_unprepare_planes() in this case.
2954 void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
2955 struct drm_atomic_state *old_state)
2957 struct drm_plane *plane;
2958 struct drm_plane_state *old_plane_state;
2961 for_each_old_plane_in_state(old_state, plane, old_plane_state, i) {
2962 const struct drm_plane_helper_funcs *funcs = plane->helper_private;
2964 if (funcs->cleanup_fb)
2965 funcs->cleanup_fb(plane, old_plane_state);
2968 EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
2971 * drm_atomic_helper_swap_state - store atomic state into current sw state
2972 * @state: atomic state
2973 * @stall: stall for preceding commits
2975 * This function stores the atomic state into the current state pointers in all
2976 * driver objects. It should be called after all failing steps have been done
2977 * and succeeded, but before the actual hardware state is committed.
2979 * For cleanup and error recovery the current state for all changed objects will
2980 * be swapped into @state.
2982 * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
2984 * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
2986 * 2. Do any other steps that might fail.
2988 * 3. Put the staged state into the current state pointers with this function.
2990 * 4. Actually commit the hardware state.
2992 * 5. Call drm_atomic_helper_cleanup_planes() with @state, which since step 3
2993 * contains the old state. Also do any other cleanup required with that state.
2995 * @stall must be set when nonblocking commits for this driver directly access
2996 * the &drm_plane.state, &drm_crtc.state or &drm_connector.state pointer. With
2997 * the current atomic helpers this is almost always the case, since the helpers
2998 * don't pass the right state structures to the callbacks.
3002 * Returns 0 on success. Can return -ERESTARTSYS when @stall is true and the
3003 * waiting for the previous commits has been interrupted.
3005 int drm_atomic_helper_swap_state(struct drm_atomic_state *state,
3009 struct drm_connector *connector;
3010 struct drm_connector_state *old_conn_state, *new_conn_state;
3011 struct drm_crtc *crtc;
3012 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
3013 struct drm_plane *plane;
3014 struct drm_plane_state *old_plane_state, *new_plane_state;
3015 struct drm_crtc_commit *commit;
3016 struct drm_private_obj *obj;
3017 struct drm_private_state *old_obj_state, *new_obj_state;
3021 * We have to stall for hw_done here before
3022 * drm_atomic_helper_wait_for_dependencies() because flip
3023 * depth > 1 is not yet supported by all drivers. As long as
3024 * obj->state is directly dereferenced anywhere in the drivers
3025 * atomic_commit_tail function, then it's unsafe to swap state
3026 * before drm_atomic_helper_commit_hw_done() is called.
3029 for_each_old_crtc_in_state(state, crtc, old_crtc_state, i) {
3030 commit = old_crtc_state->commit;
3035 ret = wait_for_completion_interruptible(&commit->hw_done);
3040 for_each_old_connector_in_state(state, connector, old_conn_state, i) {
3041 commit = old_conn_state->commit;
3046 ret = wait_for_completion_interruptible(&commit->hw_done);
3051 for_each_old_plane_in_state(state, plane, old_plane_state, i) {
3052 commit = old_plane_state->commit;
3057 ret = wait_for_completion_interruptible(&commit->hw_done);
3063 for_each_oldnew_connector_in_state(state, connector, old_conn_state, new_conn_state, i) {
3064 WARN_ON(connector->state != old_conn_state);
3066 old_conn_state->state = state;
3067 new_conn_state->state = NULL;
3069 state->connectors[i].state = old_conn_state;
3070 connector->state = new_conn_state;
3073 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
3074 WARN_ON(crtc->state != old_crtc_state);
3076 old_crtc_state->state = state;
3077 new_crtc_state->state = NULL;
3079 state->crtcs[i].state = old_crtc_state;
3080 crtc->state = new_crtc_state;
3082 if (new_crtc_state->commit) {
3083 spin_lock(&crtc->commit_lock);
3084 list_add(&new_crtc_state->commit->commit_entry,
3085 &crtc->commit_list);
3086 spin_unlock(&crtc->commit_lock);
3088 new_crtc_state->commit->event = NULL;
3092 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
3093 WARN_ON(plane->state != old_plane_state);
3095 old_plane_state->state = state;
3096 new_plane_state->state = NULL;
3098 state->planes[i].state = old_plane_state;
3099 plane->state = new_plane_state;
3102 for_each_oldnew_private_obj_in_state(state, obj, old_obj_state, new_obj_state, i) {
3103 WARN_ON(obj->state != old_obj_state);
3105 old_obj_state->state = state;
3106 new_obj_state->state = NULL;
3108 state->private_objs[i].state = old_obj_state;
3109 obj->state = new_obj_state;
3114 EXPORT_SYMBOL(drm_atomic_helper_swap_state);
3117 * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
3118 * @plane: plane object to update
3119 * @crtc: owning CRTC of owning plane
3120 * @fb: framebuffer to flip onto plane
3121 * @crtc_x: x offset of primary plane on @crtc
3122 * @crtc_y: y offset of primary plane on @crtc
3123 * @crtc_w: width of primary plane rectangle on @crtc
3124 * @crtc_h: height of primary plane rectangle on @crtc
3125 * @src_x: x offset of @fb for panning
3126 * @src_y: y offset of @fb for panning
3127 * @src_w: width of source rectangle in @fb
3128 * @src_h: height of source rectangle in @fb
3129 * @ctx: lock acquire context
3131 * Provides a default plane update handler using the atomic driver interface.
3134 * Zero on success, error code on failure
3136 int drm_atomic_helper_update_plane(struct drm_plane *plane,
3137 struct drm_crtc *crtc,
3138 struct drm_framebuffer *fb,
3139 int crtc_x, int crtc_y,
3140 unsigned int crtc_w, unsigned int crtc_h,
3141 uint32_t src_x, uint32_t src_y,
3142 uint32_t src_w, uint32_t src_h,
3143 struct drm_modeset_acquire_ctx *ctx)
3145 struct drm_atomic_state *state;
3146 struct drm_plane_state *plane_state;
3149 state = drm_atomic_state_alloc(plane->dev);
3153 state->acquire_ctx = ctx;
3154 plane_state = drm_atomic_get_plane_state(state, plane);
3155 if (IS_ERR(plane_state)) {
3156 ret = PTR_ERR(plane_state);
3160 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
3163 drm_atomic_set_fb_for_plane(plane_state, fb);
3164 plane_state->crtc_x = crtc_x;
3165 plane_state->crtc_y = crtc_y;
3166 plane_state->crtc_w = crtc_w;
3167 plane_state->crtc_h = crtc_h;
3168 plane_state->src_x = src_x;
3169 plane_state->src_y = src_y;
3170 plane_state->src_w = src_w;
3171 plane_state->src_h = src_h;
3173 if (plane == crtc->cursor)
3174 state->legacy_cursor_update = true;
3176 ret = drm_atomic_commit(state);
3178 drm_atomic_state_put(state);
3181 EXPORT_SYMBOL(drm_atomic_helper_update_plane);
3184 * drm_atomic_helper_disable_plane - Helper for primary plane disable using atomic
3185 * @plane: plane to disable
3186 * @ctx: lock acquire context
3188 * Provides a default plane disable handler using the atomic driver interface.
3191 * Zero on success, error code on failure
3193 int drm_atomic_helper_disable_plane(struct drm_plane *plane,
3194 struct drm_modeset_acquire_ctx *ctx)
3196 struct drm_atomic_state *state;
3197 struct drm_plane_state *plane_state;
3200 state = drm_atomic_state_alloc(plane->dev);
3204 state->acquire_ctx = ctx;
3205 plane_state = drm_atomic_get_plane_state(state, plane);
3206 if (IS_ERR(plane_state)) {
3207 ret = PTR_ERR(plane_state);
3211 if (plane_state->crtc && plane_state->crtc->cursor == plane)
3212 plane_state->state->legacy_cursor_update = true;
3214 ret = __drm_atomic_helper_disable_plane(plane, plane_state);
3218 ret = drm_atomic_commit(state);
3220 drm_atomic_state_put(state);
3223 EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
3226 * drm_atomic_helper_set_config - set a new config from userspace
3227 * @set: mode set configuration
3228 * @ctx: lock acquisition context
3230 * Provides a default CRTC set_config handler using the atomic driver interface.
3232 * NOTE: For backwards compatibility with old userspace this automatically
3233 * resets the "link-status" property to GOOD, to force any link
3234 * re-training. The SETCRTC ioctl does not define whether an update does
3235 * need a full modeset or just a plane update, hence we're allowed to do
3236 * that. See also drm_connector_set_link_status_property().
3239 * Returns 0 on success, negative errno numbers on failure.
3241 int drm_atomic_helper_set_config(struct drm_mode_set *set,
3242 struct drm_modeset_acquire_ctx *ctx)
3244 struct drm_atomic_state *state;
3245 struct drm_crtc *crtc = set->crtc;
3248 state = drm_atomic_state_alloc(crtc->dev);
3252 state->acquire_ctx = ctx;
3253 ret = __drm_atomic_helper_set_config(set, state);
3257 ret = handle_conflicting_encoders(state, true);
3261 ret = drm_atomic_commit(state);
3264 drm_atomic_state_put(state);
3267 EXPORT_SYMBOL(drm_atomic_helper_set_config);
3270 * drm_atomic_helper_disable_all - disable all currently active outputs
3272 * @ctx: lock acquisition context
3274 * Loops through all connectors, finding those that aren't turned off and then
3275 * turns them off by setting their DPMS mode to OFF and deactivating the CRTC
3276 * that they are connected to.
3278 * This is used for example in suspend/resume to disable all currently active
3279 * functions when suspending. If you just want to shut down everything at e.g.
3280 * driver unload, look at drm_atomic_helper_shutdown().
3282 * Note that if callers haven't already acquired all modeset locks this might
3283 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
3286 * 0 on success or a negative error code on failure.
3289 * drm_atomic_helper_suspend(), drm_atomic_helper_resume() and
3290 * drm_atomic_helper_shutdown().
3292 int drm_atomic_helper_disable_all(struct drm_device *dev,
3293 struct drm_modeset_acquire_ctx *ctx)
3295 struct drm_atomic_state *state;
3296 struct drm_connector_state *conn_state;
3297 struct drm_connector *conn;
3298 struct drm_plane_state *plane_state;
3299 struct drm_plane *plane;
3300 struct drm_crtc_state *crtc_state;
3301 struct drm_crtc *crtc;
3304 state = drm_atomic_state_alloc(dev);
3308 state->acquire_ctx = ctx;
3310 drm_for_each_crtc(crtc, dev) {
3311 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3312 if (IS_ERR(crtc_state)) {
3313 ret = PTR_ERR(crtc_state);
3317 crtc_state->active = false;
3319 ret = drm_atomic_set_mode_prop_for_crtc(crtc_state, NULL);
3323 ret = drm_atomic_add_affected_planes(state, crtc);
3327 ret = drm_atomic_add_affected_connectors(state, crtc);
3332 for_each_new_connector_in_state(state, conn, conn_state, i) {
3333 ret = drm_atomic_set_crtc_for_connector(conn_state, NULL);
3338 for_each_new_plane_in_state(state, plane, plane_state, i) {
3339 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
3343 drm_atomic_set_fb_for_plane(plane_state, NULL);
3346 ret = drm_atomic_commit(state);
3348 drm_atomic_state_put(state);
3351 EXPORT_SYMBOL(drm_atomic_helper_disable_all);
3354 * drm_atomic_helper_shutdown - shutdown all CRTC
3357 * This shuts down all CRTC, which is useful for driver unloading. Shutdown on
3358 * suspend should instead be handled with drm_atomic_helper_suspend(), since
3359 * that also takes a snapshot of the modeset state to be restored on resume.
3361 * This is just a convenience wrapper around drm_atomic_helper_disable_all(),
3362 * and it is the atomic version of drm_helper_force_disable_all().
3364 void drm_atomic_helper_shutdown(struct drm_device *dev)
3366 struct drm_modeset_acquire_ctx ctx;
3369 DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx, 0, ret);
3371 ret = drm_atomic_helper_disable_all(dev, &ctx);
3374 "Disabling all crtc's during unload failed with %i\n",
3377 DRM_MODESET_LOCK_ALL_END(dev, ctx, ret);
3379 EXPORT_SYMBOL(drm_atomic_helper_shutdown);
3382 * drm_atomic_helper_duplicate_state - duplicate an atomic state object
3384 * @ctx: lock acquisition context
3386 * Makes a copy of the current atomic state by looping over all objects and
3387 * duplicating their respective states. This is used for example by suspend/
3388 * resume support code to save the state prior to suspend such that it can
3389 * be restored upon resume.
3391 * Note that this treats atomic state as persistent between save and restore.
3392 * Drivers must make sure that this is possible and won't result in confusion
3393 * or erroneous behaviour.
3395 * Note that if callers haven't already acquired all modeset locks this might
3396 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
3399 * A pointer to the copy of the atomic state object on success or an
3400 * ERR_PTR()-encoded error code on failure.
3403 * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
3405 struct drm_atomic_state *
3406 drm_atomic_helper_duplicate_state(struct drm_device *dev,
3407 struct drm_modeset_acquire_ctx *ctx)
3409 struct drm_atomic_state *state;
3410 struct drm_connector *conn;
3411 struct drm_connector_list_iter conn_iter;
3412 struct drm_plane *plane;
3413 struct drm_crtc *crtc;
3416 state = drm_atomic_state_alloc(dev);
3418 return ERR_PTR(-ENOMEM);
3420 state->acquire_ctx = ctx;
3421 state->duplicated = true;
3423 drm_for_each_crtc(crtc, dev) {
3424 struct drm_crtc_state *crtc_state;
3426 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3427 if (IS_ERR(crtc_state)) {
3428 err = PTR_ERR(crtc_state);
3433 drm_for_each_plane(plane, dev) {
3434 struct drm_plane_state *plane_state;
3436 plane_state = drm_atomic_get_plane_state(state, plane);
3437 if (IS_ERR(plane_state)) {
3438 err = PTR_ERR(plane_state);
3443 drm_connector_list_iter_begin(dev, &conn_iter);
3444 drm_for_each_connector_iter(conn, &conn_iter) {
3445 struct drm_connector_state *conn_state;
3447 conn_state = drm_atomic_get_connector_state(state, conn);
3448 if (IS_ERR(conn_state)) {
3449 err = PTR_ERR(conn_state);
3450 drm_connector_list_iter_end(&conn_iter);
3454 drm_connector_list_iter_end(&conn_iter);
3456 /* clear the acquire context so that it isn't accidentally reused */
3457 state->acquire_ctx = NULL;
3461 drm_atomic_state_put(state);
3462 state = ERR_PTR(err);
3467 EXPORT_SYMBOL(drm_atomic_helper_duplicate_state);
3470 * drm_atomic_helper_suspend - subsystem-level suspend helper
3473 * Duplicates the current atomic state, disables all active outputs and then
3474 * returns a pointer to the original atomic state to the caller. Drivers can
3475 * pass this pointer to the drm_atomic_helper_resume() helper upon resume to
3476 * restore the output configuration that was active at the time the system
3479 * Note that it is potentially unsafe to use this. The atomic state object
3480 * returned by this function is assumed to be persistent. Drivers must ensure
3481 * that this holds true. Before calling this function, drivers must make sure
3482 * to suspend fbdev emulation so that nothing can be using the device.
3485 * A pointer to a copy of the state before suspend on success or an ERR_PTR()-
3486 * encoded error code on failure. Drivers should store the returned atomic
3487 * state object and pass it to the drm_atomic_helper_resume() helper upon
3491 * drm_atomic_helper_duplicate_state(), drm_atomic_helper_disable_all(),
3492 * drm_atomic_helper_resume(), drm_atomic_helper_commit_duplicated_state()
3494 struct drm_atomic_state *drm_atomic_helper_suspend(struct drm_device *dev)
3496 struct drm_modeset_acquire_ctx ctx;
3497 struct drm_atomic_state *state;
3500 /* This can never be returned, but it makes the compiler happy */
3501 state = ERR_PTR(-EINVAL);
3503 DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx, 0, err);
3505 state = drm_atomic_helper_duplicate_state(dev, &ctx);
3509 err = drm_atomic_helper_disable_all(dev, &ctx);
3511 drm_atomic_state_put(state);
3512 state = ERR_PTR(err);
3517 DRM_MODESET_LOCK_ALL_END(dev, ctx, err);
3519 return ERR_PTR(err);
3523 EXPORT_SYMBOL(drm_atomic_helper_suspend);
3526 * drm_atomic_helper_commit_duplicated_state - commit duplicated state
3527 * @state: duplicated atomic state to commit
3528 * @ctx: pointer to acquire_ctx to use for commit.
3530 * The state returned by drm_atomic_helper_duplicate_state() and
3531 * drm_atomic_helper_suspend() is partially invalid, and needs to
3532 * be fixed up before commit.
3535 * 0 on success or a negative error code on failure.
3538 * drm_atomic_helper_suspend()
3540 int drm_atomic_helper_commit_duplicated_state(struct drm_atomic_state *state,
3541 struct drm_modeset_acquire_ctx *ctx)
3544 struct drm_plane *plane;
3545 struct drm_plane_state *new_plane_state;
3546 struct drm_connector *connector;
3547 struct drm_connector_state *new_conn_state;
3548 struct drm_crtc *crtc;
3549 struct drm_crtc_state *new_crtc_state;
3551 state->acquire_ctx = ctx;
3553 for_each_new_plane_in_state(state, plane, new_plane_state, i)
3554 state->planes[i].old_state = plane->state;
3556 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i)
3557 state->crtcs[i].old_state = crtc->state;
3559 for_each_new_connector_in_state(state, connector, new_conn_state, i)
3560 state->connectors[i].old_state = connector->state;
3562 ret = drm_atomic_commit(state);
3564 state->acquire_ctx = NULL;
3568 EXPORT_SYMBOL(drm_atomic_helper_commit_duplicated_state);
3571 * drm_atomic_helper_resume - subsystem-level resume helper
3573 * @state: atomic state to resume to
3575 * Calls drm_mode_config_reset() to synchronize hardware and software states,
3576 * grabs all modeset locks and commits the atomic state object. This can be
3577 * used in conjunction with the drm_atomic_helper_suspend() helper to
3578 * implement suspend/resume for drivers that support atomic mode-setting.
3581 * 0 on success or a negative error code on failure.
3584 * drm_atomic_helper_suspend()
3586 int drm_atomic_helper_resume(struct drm_device *dev,
3587 struct drm_atomic_state *state)
3589 struct drm_modeset_acquire_ctx ctx;
3592 drm_mode_config_reset(dev);
3594 DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx, 0, err);
3596 err = drm_atomic_helper_commit_duplicated_state(state, &ctx);
3598 DRM_MODESET_LOCK_ALL_END(dev, ctx, err);
3599 drm_atomic_state_put(state);
3603 EXPORT_SYMBOL(drm_atomic_helper_resume);
3605 static int page_flip_common(struct drm_atomic_state *state,
3606 struct drm_crtc *crtc,
3607 struct drm_framebuffer *fb,
3608 struct drm_pending_vblank_event *event,
3611 struct drm_plane *plane = crtc->primary;
3612 struct drm_plane_state *plane_state;
3613 struct drm_crtc_state *crtc_state;
3616 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3617 if (IS_ERR(crtc_state))
3618 return PTR_ERR(crtc_state);
3620 crtc_state->event = event;
3621 crtc_state->async_flip = flags & DRM_MODE_PAGE_FLIP_ASYNC;
3623 plane_state = drm_atomic_get_plane_state(state, plane);
3624 if (IS_ERR(plane_state))
3625 return PTR_ERR(plane_state);
3627 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
3630 drm_atomic_set_fb_for_plane(plane_state, fb);
3632 /* Make sure we don't accidentally do a full modeset. */
3633 state->allow_modeset = false;
3634 if (!crtc_state->active) {
3635 drm_dbg_atomic(crtc->dev,
3636 "[CRTC:%d:%s] disabled, rejecting legacy flip\n",
3637 crtc->base.id, crtc->name);
3645 * drm_atomic_helper_page_flip - execute a legacy page flip
3647 * @fb: DRM framebuffer
3648 * @event: optional DRM event to signal upon completion
3649 * @flags: flip flags for non-vblank sync'ed updates
3650 * @ctx: lock acquisition context
3652 * Provides a default &drm_crtc_funcs.page_flip implementation
3653 * using the atomic driver interface.
3656 * Returns 0 on success, negative errno numbers on failure.
3659 * drm_atomic_helper_page_flip_target()
3661 int drm_atomic_helper_page_flip(struct drm_crtc *crtc,
3662 struct drm_framebuffer *fb,
3663 struct drm_pending_vblank_event *event,
3665 struct drm_modeset_acquire_ctx *ctx)
3667 struct drm_plane *plane = crtc->primary;
3668 struct drm_atomic_state *state;
3671 state = drm_atomic_state_alloc(plane->dev);
3675 state->acquire_ctx = ctx;
3677 ret = page_flip_common(state, crtc, fb, event, flags);
3681 ret = drm_atomic_nonblocking_commit(state);
3683 drm_atomic_state_put(state);
3686 EXPORT_SYMBOL(drm_atomic_helper_page_flip);
3689 * drm_atomic_helper_page_flip_target - do page flip on target vblank period.
3691 * @fb: DRM framebuffer
3692 * @event: optional DRM event to signal upon completion
3693 * @flags: flip flags for non-vblank sync'ed updates
3694 * @target: specifying the target vblank period when the flip to take effect
3695 * @ctx: lock acquisition context
3697 * Provides a default &drm_crtc_funcs.page_flip_target implementation.
3698 * Similar to drm_atomic_helper_page_flip() with extra parameter to specify
3699 * target vblank period to flip.
3702 * Returns 0 on success, negative errno numbers on failure.
3704 int drm_atomic_helper_page_flip_target(struct drm_crtc *crtc,
3705 struct drm_framebuffer *fb,
3706 struct drm_pending_vblank_event *event,
3709 struct drm_modeset_acquire_ctx *ctx)
3711 struct drm_plane *plane = crtc->primary;
3712 struct drm_atomic_state *state;
3713 struct drm_crtc_state *crtc_state;
3716 state = drm_atomic_state_alloc(plane->dev);
3720 state->acquire_ctx = ctx;
3722 ret = page_flip_common(state, crtc, fb, event, flags);
3726 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
3727 if (WARN_ON(!crtc_state)) {
3731 crtc_state->target_vblank = target;
3733 ret = drm_atomic_nonblocking_commit(state);
3735 drm_atomic_state_put(state);
3738 EXPORT_SYMBOL(drm_atomic_helper_page_flip_target);
3741 * drm_atomic_helper_bridge_propagate_bus_fmt() - Propagate output format to
3742 * the input end of a bridge
3743 * @bridge: bridge control structure
3744 * @bridge_state: new bridge state
3745 * @crtc_state: new CRTC state
3746 * @conn_state: new connector state
3747 * @output_fmt: tested output bus format
3748 * @num_input_fmts: will contain the size of the returned array
3750 * This helper is a pluggable implementation of the
3751 * &drm_bridge_funcs.atomic_get_input_bus_fmts operation for bridges that don't
3752 * modify the bus configuration between their input and their output. It
3753 * returns an array of input formats with a single element set to @output_fmt.
3756 * a valid format array of size @num_input_fmts, or NULL if the allocation
3760 drm_atomic_helper_bridge_propagate_bus_fmt(struct drm_bridge *bridge,
3761 struct drm_bridge_state *bridge_state,
3762 struct drm_crtc_state *crtc_state,
3763 struct drm_connector_state *conn_state,
3765 unsigned int *num_input_fmts)
3769 input_fmts = kzalloc(sizeof(*input_fmts), GFP_KERNEL);
3771 *num_input_fmts = 0;
3775 *num_input_fmts = 1;
3776 input_fmts[0] = output_fmt;
3779 EXPORT_SYMBOL(drm_atomic_helper_bridge_propagate_bus_fmt);