2 * Copyright (C) 2014 Red Hat
3 * Copyright (C) 2014 Intel Corp.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
29 #include <drm/drm_atomic.h>
30 #include <drm/drm_atomic_uapi.h>
31 #include <drm/drm_plane_helper.h>
32 #include <drm/drm_crtc_helper.h>
33 #include <drm/drm_atomic_helper.h>
34 #include <drm/drm_writeback.h>
35 #include <linux/dma-fence.h>
37 #include "drm_crtc_helper_internal.h"
38 #include "drm_crtc_internal.h"
43 * This helper library provides implementations of check and commit functions on
44 * top of the CRTC modeset helper callbacks and the plane helper callbacks. It
45 * also provides convenience implementations for the atomic state handling
46 * callbacks for drivers which don't need to subclass the drm core structures to
47 * add their own additional internal state.
49 * This library also provides default implementations for the check callback in
50 * drm_atomic_helper_check() and for the commit callback with
51 * drm_atomic_helper_commit(). But the individual stages and callbacks are
52 * exposed to allow drivers to mix and match and e.g. use the plane helpers only
53 * together with a driver private modeset implementation.
55 * This library also provides implementations for all the legacy driver
56 * interfaces on top of the atomic interface. See drm_atomic_helper_set_config(),
57 * drm_atomic_helper_disable_plane(), drm_atomic_helper_disable_plane() and the
58 * various functions to implement set_property callbacks. New drivers must not
59 * implement these functions themselves but must use the provided helpers.
61 * The atomic helper uses the same function table structures as all other
62 * modesetting helpers. See the documentation for &struct drm_crtc_helper_funcs,
63 * struct &drm_encoder_helper_funcs and &struct drm_connector_helper_funcs. It
64 * also shares the &struct drm_plane_helper_funcs function table with the plane
68 drm_atomic_helper_plane_changed(struct drm_atomic_state *state,
69 struct drm_plane_state *old_plane_state,
70 struct drm_plane_state *plane_state,
71 struct drm_plane *plane)
73 struct drm_crtc_state *crtc_state;
75 if (old_plane_state->crtc) {
76 crtc_state = drm_atomic_get_new_crtc_state(state,
77 old_plane_state->crtc);
79 if (WARN_ON(!crtc_state))
82 crtc_state->planes_changed = true;
85 if (plane_state->crtc) {
86 crtc_state = drm_atomic_get_new_crtc_state(state, plane_state->crtc);
88 if (WARN_ON(!crtc_state))
91 crtc_state->planes_changed = true;
95 static int handle_conflicting_encoders(struct drm_atomic_state *state,
96 bool disable_conflicting_encoders)
98 struct drm_connector_state *new_conn_state;
99 struct drm_connector *connector;
100 struct drm_connector_list_iter conn_iter;
101 struct drm_encoder *encoder;
102 unsigned encoder_mask = 0;
106 * First loop, find all newly assigned encoders from the connectors
107 * part of the state. If the same encoder is assigned to multiple
108 * connectors bail out.
110 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
111 const struct drm_connector_helper_funcs *funcs = connector->helper_private;
112 struct drm_encoder *new_encoder;
114 if (!new_conn_state->crtc)
117 if (funcs->atomic_best_encoder)
118 new_encoder = funcs->atomic_best_encoder(connector, new_conn_state);
119 else if (funcs->best_encoder)
120 new_encoder = funcs->best_encoder(connector);
122 new_encoder = drm_atomic_helper_best_encoder(connector);
125 if (encoder_mask & drm_encoder_mask(new_encoder)) {
126 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] on [CONNECTOR:%d:%s] already assigned\n",
127 new_encoder->base.id, new_encoder->name,
128 connector->base.id, connector->name);
133 encoder_mask |= drm_encoder_mask(new_encoder);
141 * Second loop, iterate over all connectors not part of the state.
143 * If a conflicting encoder is found and disable_conflicting_encoders
144 * is not set, an error is returned. Userspace can provide a solution
145 * through the atomic ioctl.
147 * If the flag is set conflicting connectors are removed from the crtc
148 * and the crtc is disabled if no encoder is left. This preserves
149 * compatibility with the legacy set_config behavior.
151 drm_connector_list_iter_begin(state->dev, &conn_iter);
152 drm_for_each_connector_iter(connector, &conn_iter) {
153 struct drm_crtc_state *crtc_state;
155 if (drm_atomic_get_new_connector_state(state, connector))
158 encoder = connector->state->best_encoder;
159 if (!encoder || !(encoder_mask & drm_encoder_mask(encoder)))
162 if (!disable_conflicting_encoders) {
163 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s] by [CONNECTOR:%d:%s]\n",
164 encoder->base.id, encoder->name,
165 connector->state->crtc->base.id,
166 connector->state->crtc->name,
167 connector->base.id, connector->name);
172 new_conn_state = drm_atomic_get_connector_state(state, connector);
173 if (IS_ERR(new_conn_state)) {
174 ret = PTR_ERR(new_conn_state);
178 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s], disabling [CONNECTOR:%d:%s]\n",
179 encoder->base.id, encoder->name,
180 new_conn_state->crtc->base.id, new_conn_state->crtc->name,
181 connector->base.id, connector->name);
183 crtc_state = drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
185 ret = drm_atomic_set_crtc_for_connector(new_conn_state, NULL);
189 if (!crtc_state->connector_mask) {
190 ret = drm_atomic_set_mode_prop_for_crtc(crtc_state,
195 crtc_state->active = false;
199 drm_connector_list_iter_end(&conn_iter);
205 set_best_encoder(struct drm_atomic_state *state,
206 struct drm_connector_state *conn_state,
207 struct drm_encoder *encoder)
209 struct drm_crtc_state *crtc_state;
210 struct drm_crtc *crtc;
212 if (conn_state->best_encoder) {
213 /* Unset the encoder_mask in the old crtc state. */
214 crtc = conn_state->connector->state->crtc;
216 /* A NULL crtc is an error here because we should have
217 * duplicated a NULL best_encoder when crtc was NULL.
218 * As an exception restoring duplicated atomic state
219 * during resume is allowed, so don't warn when
220 * best_encoder is equal to encoder we intend to set.
222 WARN_ON(!crtc && encoder != conn_state->best_encoder);
224 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
226 crtc_state->encoder_mask &=
227 ~drm_encoder_mask(conn_state->best_encoder);
232 crtc = conn_state->crtc;
235 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
237 crtc_state->encoder_mask |=
238 drm_encoder_mask(encoder);
242 conn_state->best_encoder = encoder;
246 steal_encoder(struct drm_atomic_state *state,
247 struct drm_encoder *encoder)
249 struct drm_crtc_state *crtc_state;
250 struct drm_connector *connector;
251 struct drm_connector_state *old_connector_state, *new_connector_state;
254 for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
255 struct drm_crtc *encoder_crtc;
257 if (new_connector_state->best_encoder != encoder)
260 encoder_crtc = old_connector_state->crtc;
262 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s], stealing it\n",
263 encoder->base.id, encoder->name,
264 encoder_crtc->base.id, encoder_crtc->name);
266 set_best_encoder(state, new_connector_state, NULL);
268 crtc_state = drm_atomic_get_new_crtc_state(state, encoder_crtc);
269 crtc_state->connectors_changed = true;
276 update_connector_routing(struct drm_atomic_state *state,
277 struct drm_connector *connector,
278 struct drm_connector_state *old_connector_state,
279 struct drm_connector_state *new_connector_state)
281 const struct drm_connector_helper_funcs *funcs;
282 struct drm_encoder *new_encoder;
283 struct drm_crtc_state *crtc_state;
285 DRM_DEBUG_ATOMIC("Updating routing for [CONNECTOR:%d:%s]\n",
289 if (old_connector_state->crtc != new_connector_state->crtc) {
290 if (old_connector_state->crtc) {
291 crtc_state = drm_atomic_get_new_crtc_state(state, old_connector_state->crtc);
292 crtc_state->connectors_changed = true;
295 if (new_connector_state->crtc) {
296 crtc_state = drm_atomic_get_new_crtc_state(state, new_connector_state->crtc);
297 crtc_state->connectors_changed = true;
301 if (!new_connector_state->crtc) {
302 DRM_DEBUG_ATOMIC("Disabling [CONNECTOR:%d:%s]\n",
306 set_best_encoder(state, new_connector_state, NULL);
311 funcs = connector->helper_private;
313 if (funcs->atomic_best_encoder)
314 new_encoder = funcs->atomic_best_encoder(connector,
315 new_connector_state);
316 else if (funcs->best_encoder)
317 new_encoder = funcs->best_encoder(connector);
319 new_encoder = drm_atomic_helper_best_encoder(connector);
322 DRM_DEBUG_ATOMIC("No suitable encoder found for [CONNECTOR:%d:%s]\n",
328 if (!drm_encoder_crtc_ok(new_encoder, new_connector_state->crtc)) {
329 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] incompatible with [CRTC:%d:%s]\n",
330 new_encoder->base.id,
332 new_connector_state->crtc->base.id,
333 new_connector_state->crtc->name);
337 if (new_encoder == new_connector_state->best_encoder) {
338 set_best_encoder(state, new_connector_state, new_encoder);
340 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] keeps [ENCODER:%d:%s], now on [CRTC:%d:%s]\n",
343 new_encoder->base.id,
345 new_connector_state->crtc->base.id,
346 new_connector_state->crtc->name);
351 steal_encoder(state, new_encoder);
353 set_best_encoder(state, new_connector_state, new_encoder);
355 crtc_state = drm_atomic_get_new_crtc_state(state, new_connector_state->crtc);
356 crtc_state->connectors_changed = true;
358 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] using [ENCODER:%d:%s] on [CRTC:%d:%s]\n",
361 new_encoder->base.id,
363 new_connector_state->crtc->base.id,
364 new_connector_state->crtc->name);
370 mode_fixup(struct drm_atomic_state *state)
372 struct drm_crtc *crtc;
373 struct drm_crtc_state *new_crtc_state;
374 struct drm_connector *connector;
375 struct drm_connector_state *new_conn_state;
379 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
380 if (!new_crtc_state->mode_changed &&
381 !new_crtc_state->connectors_changed)
384 drm_mode_copy(&new_crtc_state->adjusted_mode, &new_crtc_state->mode);
387 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
388 const struct drm_encoder_helper_funcs *funcs;
389 struct drm_encoder *encoder;
391 WARN_ON(!!new_conn_state->best_encoder != !!new_conn_state->crtc);
393 if (!new_conn_state->crtc || !new_conn_state->best_encoder)
397 drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
400 * Each encoder has at most one connector (since we always steal
401 * it away), so we won't call ->mode_fixup twice.
403 encoder = new_conn_state->best_encoder;
404 funcs = encoder->helper_private;
406 ret = drm_bridge_mode_fixup(encoder->bridge, &new_crtc_state->mode,
407 &new_crtc_state->adjusted_mode);
409 DRM_DEBUG_ATOMIC("Bridge fixup failed\n");
413 if (funcs && funcs->atomic_check) {
414 ret = funcs->atomic_check(encoder, new_crtc_state,
417 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] check failed\n",
418 encoder->base.id, encoder->name);
421 } else if (funcs && funcs->mode_fixup) {
422 ret = funcs->mode_fixup(encoder, &new_crtc_state->mode,
423 &new_crtc_state->adjusted_mode);
425 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] fixup failed\n",
426 encoder->base.id, encoder->name);
432 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
433 const struct drm_crtc_helper_funcs *funcs;
435 if (!new_crtc_state->enable)
438 if (!new_crtc_state->mode_changed &&
439 !new_crtc_state->connectors_changed)
442 funcs = crtc->helper_private;
443 if (!funcs->mode_fixup)
446 ret = funcs->mode_fixup(crtc, &new_crtc_state->mode,
447 &new_crtc_state->adjusted_mode);
449 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] fixup failed\n",
450 crtc->base.id, crtc->name);
458 static enum drm_mode_status mode_valid_path(struct drm_connector *connector,
459 struct drm_encoder *encoder,
460 struct drm_crtc *crtc,
461 struct drm_display_mode *mode)
463 enum drm_mode_status ret;
465 ret = drm_encoder_mode_valid(encoder, mode);
466 if (ret != MODE_OK) {
467 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] mode_valid() failed\n",
468 encoder->base.id, encoder->name);
472 ret = drm_bridge_mode_valid(encoder->bridge, mode);
473 if (ret != MODE_OK) {
474 DRM_DEBUG_ATOMIC("[BRIDGE] mode_valid() failed\n");
478 ret = drm_crtc_mode_valid(crtc, mode);
479 if (ret != MODE_OK) {
480 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] mode_valid() failed\n",
481 crtc->base.id, crtc->name);
489 mode_valid(struct drm_atomic_state *state)
491 struct drm_connector_state *conn_state;
492 struct drm_connector *connector;
495 for_each_new_connector_in_state(state, connector, conn_state, i) {
496 struct drm_encoder *encoder = conn_state->best_encoder;
497 struct drm_crtc *crtc = conn_state->crtc;
498 struct drm_crtc_state *crtc_state;
499 enum drm_mode_status mode_status;
500 struct drm_display_mode *mode;
502 if (!crtc || !encoder)
505 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
508 if (!crtc_state->mode_changed && !crtc_state->connectors_changed)
511 mode = &crtc_state->mode;
513 mode_status = mode_valid_path(connector, encoder, crtc, mode);
514 if (mode_status != MODE_OK)
522 * drm_atomic_helper_check_modeset - validate state object for modeset changes
524 * @state: the driver state object
526 * Check the state object to see if the requested state is physically possible.
527 * This does all the crtc and connector related computations for an atomic
528 * update and adds any additional connectors needed for full modesets. It calls
529 * the various per-object callbacks in the follow order:
531 * 1. &drm_connector_helper_funcs.atomic_best_encoder for determining the new encoder.
532 * 2. &drm_connector_helper_funcs.atomic_check to validate the connector state.
533 * 3. If it's determined a modeset is needed then all connectors on the affected crtc
534 * crtc are added and &drm_connector_helper_funcs.atomic_check is run on them.
535 * 4. &drm_encoder_helper_funcs.mode_valid, &drm_bridge_funcs.mode_valid and
536 * &drm_crtc_helper_funcs.mode_valid are called on the affected components.
537 * 5. &drm_bridge_funcs.mode_fixup is called on all encoder bridges.
538 * 6. &drm_encoder_helper_funcs.atomic_check is called to validate any encoder state.
539 * This function is only called when the encoder will be part of a configured crtc,
540 * it must not be used for implementing connector property validation.
541 * If this function is NULL, &drm_atomic_encoder_helper_funcs.mode_fixup is called
543 * 7. &drm_crtc_helper_funcs.mode_fixup is called last, to fix up the mode with crtc constraints.
545 * &drm_crtc_state.mode_changed is set when the input mode is changed.
546 * &drm_crtc_state.connectors_changed is set when a connector is added or
547 * removed from the crtc. &drm_crtc_state.active_changed is set when
548 * &drm_crtc_state.active changes, which is used for DPMS.
549 * See also: drm_atomic_crtc_needs_modeset()
553 * Drivers which set &drm_crtc_state.mode_changed (e.g. in their
554 * &drm_plane_helper_funcs.atomic_check hooks if a plane update can't be done
555 * without a full modeset) _must_ call this function afterwards after that
556 * change. It is permitted to call this function multiple times for the same
557 * update, e.g. when the &drm_crtc_helper_funcs.atomic_check functions depend
558 * upon the adjusted dotclock for fifo space allocation and watermark
562 * Zero for success or -errno
565 drm_atomic_helper_check_modeset(struct drm_device *dev,
566 struct drm_atomic_state *state)
568 struct drm_crtc *crtc;
569 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
570 struct drm_connector *connector;
571 struct drm_connector_state *old_connector_state, *new_connector_state;
573 unsigned connectors_mask = 0;
575 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
576 bool has_connectors =
577 !!new_crtc_state->connector_mask;
579 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
581 if (!drm_mode_equal(&old_crtc_state->mode, &new_crtc_state->mode)) {
582 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] mode changed\n",
583 crtc->base.id, crtc->name);
584 new_crtc_state->mode_changed = true;
587 if (old_crtc_state->enable != new_crtc_state->enable) {
588 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enable changed\n",
589 crtc->base.id, crtc->name);
592 * For clarity this assignment is done here, but
593 * enable == 0 is only true when there are no
594 * connectors and a NULL mode.
596 * The other way around is true as well. enable != 0
597 * iff connectors are attached and a mode is set.
599 new_crtc_state->mode_changed = true;
600 new_crtc_state->connectors_changed = true;
603 if (old_crtc_state->active != new_crtc_state->active) {
604 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active changed\n",
605 crtc->base.id, crtc->name);
606 new_crtc_state->active_changed = true;
609 if (new_crtc_state->enable != has_connectors) {
610 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled/connectors mismatch\n",
611 crtc->base.id, crtc->name);
617 ret = handle_conflicting_encoders(state, false);
621 for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
622 const struct drm_connector_helper_funcs *funcs = connector->helper_private;
624 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
627 * This only sets crtc->connectors_changed for routing changes,
628 * drivers must set crtc->connectors_changed themselves when
629 * connector properties need to be updated.
631 ret = update_connector_routing(state, connector,
633 new_connector_state);
636 if (old_connector_state->crtc) {
637 new_crtc_state = drm_atomic_get_new_crtc_state(state,
638 old_connector_state->crtc);
639 if (old_connector_state->link_status !=
640 new_connector_state->link_status)
641 new_crtc_state->connectors_changed = true;
644 if (funcs->atomic_check)
645 ret = funcs->atomic_check(connector, new_connector_state);
649 connectors_mask |= BIT(i);
653 * After all the routing has been prepared we need to add in any
654 * connector which is itself unchanged, but who's crtc changes it's
655 * configuration. This must be done before calling mode_fixup in case a
656 * crtc only changed its mode but has the same set of connectors.
658 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
659 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
662 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] needs all connectors, enable: %c, active: %c\n",
663 crtc->base.id, crtc->name,
664 new_crtc_state->enable ? 'y' : 'n',
665 new_crtc_state->active ? 'y' : 'n');
667 ret = drm_atomic_add_affected_connectors(state, crtc);
671 ret = drm_atomic_add_affected_planes(state, crtc);
677 * Iterate over all connectors again, to make sure atomic_check()
678 * has been called on them when a modeset is forced.
680 for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
681 const struct drm_connector_helper_funcs *funcs = connector->helper_private;
683 if (connectors_mask & BIT(i))
686 if (funcs->atomic_check)
687 ret = funcs->atomic_check(connector, new_connector_state);
692 ret = mode_valid(state);
696 return mode_fixup(state);
698 EXPORT_SYMBOL(drm_atomic_helper_check_modeset);
701 * drm_atomic_helper_check_plane_state() - Check plane state for validity
702 * @plane_state: plane state to check
703 * @crtc_state: crtc state to check
704 * @min_scale: minimum @src:@dest scaling factor in 16.16 fixed point
705 * @max_scale: maximum @src:@dest scaling factor in 16.16 fixed point
706 * @can_position: is it legal to position the plane such that it
707 * doesn't cover the entire crtc? This will generally
708 * only be false for primary planes.
709 * @can_update_disabled: can the plane be updated while the crtc
712 * Checks that a desired plane update is valid, and updates various
713 * bits of derived state (clipped coordinates etc.). Drivers that provide
714 * their own plane handling rather than helper-provided implementations may
715 * still wish to call this function to avoid duplication of error checking
719 * Zero if update appears valid, error code on failure
721 int drm_atomic_helper_check_plane_state(struct drm_plane_state *plane_state,
722 const struct drm_crtc_state *crtc_state,
726 bool can_update_disabled)
728 struct drm_framebuffer *fb = plane_state->fb;
729 struct drm_rect *src = &plane_state->src;
730 struct drm_rect *dst = &plane_state->dst;
731 unsigned int rotation = plane_state->rotation;
732 struct drm_rect clip = {};
735 WARN_ON(plane_state->crtc && plane_state->crtc != crtc_state->crtc);
737 *src = drm_plane_state_src(plane_state);
738 *dst = drm_plane_state_dest(plane_state);
741 plane_state->visible = false;
745 /* crtc should only be NULL when disabling (i.e., !fb) */
746 if (WARN_ON(!plane_state->crtc)) {
747 plane_state->visible = false;
751 if (!crtc_state->enable && !can_update_disabled) {
752 DRM_DEBUG_KMS("Cannot update plane of a disabled CRTC.\n");
756 drm_rect_rotate(src, fb->width << 16, fb->height << 16, rotation);
759 hscale = drm_rect_calc_hscale(src, dst, min_scale, max_scale);
760 vscale = drm_rect_calc_vscale(src, dst, min_scale, max_scale);
761 if (hscale < 0 || vscale < 0) {
762 DRM_DEBUG_KMS("Invalid scaling of plane\n");
763 drm_rect_debug_print("src: ", &plane_state->src, true);
764 drm_rect_debug_print("dst: ", &plane_state->dst, false);
768 if (crtc_state->enable)
769 drm_mode_get_hv_timing(&crtc_state->mode, &clip.x2, &clip.y2);
771 plane_state->visible = drm_rect_clip_scaled(src, dst, &clip);
773 drm_rect_rotate_inv(src, fb->width << 16, fb->height << 16, rotation);
775 if (!plane_state->visible)
777 * Plane isn't visible; some drivers can handle this
778 * so we just return success here. Drivers that can't
779 * (including those that use the primary plane helper's
780 * update function) will return an error from their
781 * update_plane handler.
785 if (!can_position && !drm_rect_equals(dst, &clip)) {
786 DRM_DEBUG_KMS("Plane must cover entire CRTC\n");
787 drm_rect_debug_print("dst: ", dst, false);
788 drm_rect_debug_print("clip: ", &clip, false);
794 EXPORT_SYMBOL(drm_atomic_helper_check_plane_state);
797 * drm_atomic_helper_check_planes - validate state object for planes changes
799 * @state: the driver state object
801 * Check the state object to see if the requested state is physically possible.
802 * This does all the plane update related checks using by calling into the
803 * &drm_crtc_helper_funcs.atomic_check and &drm_plane_helper_funcs.atomic_check
804 * hooks provided by the driver.
806 * It also sets &drm_crtc_state.planes_changed to indicate that a crtc has
810 * Zero for success or -errno
813 drm_atomic_helper_check_planes(struct drm_device *dev,
814 struct drm_atomic_state *state)
816 struct drm_crtc *crtc;
817 struct drm_crtc_state *new_crtc_state;
818 struct drm_plane *plane;
819 struct drm_plane_state *new_plane_state, *old_plane_state;
822 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
823 const struct drm_plane_helper_funcs *funcs;
825 WARN_ON(!drm_modeset_is_locked(&plane->mutex));
827 funcs = plane->helper_private;
829 drm_atomic_helper_plane_changed(state, old_plane_state, new_plane_state, plane);
831 if (!funcs || !funcs->atomic_check)
834 ret = funcs->atomic_check(plane, new_plane_state);
836 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic driver check failed\n",
837 plane->base.id, plane->name);
842 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
843 const struct drm_crtc_helper_funcs *funcs;
845 funcs = crtc->helper_private;
847 if (!funcs || !funcs->atomic_check)
850 ret = funcs->atomic_check(crtc, new_crtc_state);
852 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic driver check failed\n",
853 crtc->base.id, crtc->name);
860 EXPORT_SYMBOL(drm_atomic_helper_check_planes);
863 * drm_atomic_helper_check - validate state object
865 * @state: the driver state object
867 * Check the state object to see if the requested state is physically possible.
868 * Only crtcs and planes have check callbacks, so for any additional (global)
869 * checking that a driver needs it can simply wrap that around this function.
870 * Drivers without such needs can directly use this as their
871 * &drm_mode_config_funcs.atomic_check callback.
873 * This just wraps the two parts of the state checking for planes and modeset
874 * state in the default order: First it calls drm_atomic_helper_check_modeset()
875 * and then drm_atomic_helper_check_planes(). The assumption is that the
876 * @drm_plane_helper_funcs.atomic_check and @drm_crtc_helper_funcs.atomic_check
877 * functions depend upon an updated adjusted_mode.clock to e.g. properly compute
880 * Note that zpos normalization will add all enable planes to the state which
881 * might not desired for some drivers.
882 * For example enable/disable of a cursor plane which have fixed zpos value
883 * would trigger all other enabled planes to be forced to the state change.
886 * Zero for success or -errno
888 int drm_atomic_helper_check(struct drm_device *dev,
889 struct drm_atomic_state *state)
893 ret = drm_atomic_helper_check_modeset(dev, state);
897 if (dev->mode_config.normalize_zpos) {
898 ret = drm_atomic_normalize_zpos(dev, state);
903 ret = drm_atomic_helper_check_planes(dev, state);
907 if (state->legacy_cursor_update)
908 state->async_update = !drm_atomic_helper_async_check(dev, state);
912 EXPORT_SYMBOL(drm_atomic_helper_check);
915 disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state)
917 struct drm_connector *connector;
918 struct drm_connector_state *old_conn_state, *new_conn_state;
919 struct drm_crtc *crtc;
920 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
923 for_each_oldnew_connector_in_state(old_state, connector, old_conn_state, new_conn_state, i) {
924 const struct drm_encoder_helper_funcs *funcs;
925 struct drm_encoder *encoder;
927 /* Shut down everything that's in the changeset and currently
928 * still on. So need to check the old, saved state. */
929 if (!old_conn_state->crtc)
932 old_crtc_state = drm_atomic_get_old_crtc_state(old_state, old_conn_state->crtc);
934 if (!old_crtc_state->active ||
935 !drm_atomic_crtc_needs_modeset(old_conn_state->crtc->state))
938 encoder = old_conn_state->best_encoder;
940 /* We shouldn't get this far if we didn't previously have
941 * an encoder.. but WARN_ON() rather than explode.
943 if (WARN_ON(!encoder))
946 funcs = encoder->helper_private;
948 DRM_DEBUG_ATOMIC("disabling [ENCODER:%d:%s]\n",
949 encoder->base.id, encoder->name);
952 * Each encoder has at most one connector (since we always steal
953 * it away), so we won't call disable hooks twice.
955 drm_bridge_disable(encoder->bridge);
957 /* Right function depends upon target state. */
959 if (new_conn_state->crtc && funcs->prepare)
960 funcs->prepare(encoder);
961 else if (funcs->disable)
962 funcs->disable(encoder);
963 else if (funcs->dpms)
964 funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
967 drm_bridge_post_disable(encoder->bridge);
970 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
971 const struct drm_crtc_helper_funcs *funcs;
974 /* Shut down everything that needs a full modeset. */
975 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
978 if (!old_crtc_state->active)
981 funcs = crtc->helper_private;
983 DRM_DEBUG_ATOMIC("disabling [CRTC:%d:%s]\n",
984 crtc->base.id, crtc->name);
987 /* Right function depends upon target state. */
988 if (new_crtc_state->enable && funcs->prepare)
989 funcs->prepare(crtc);
990 else if (funcs->atomic_disable)
991 funcs->atomic_disable(crtc, old_crtc_state);
992 else if (funcs->disable)
993 funcs->disable(crtc);
995 funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
997 if (!(dev->irq_enabled && dev->num_crtcs))
1000 ret = drm_crtc_vblank_get(crtc);
1001 WARN_ONCE(ret != -EINVAL, "driver forgot to call drm_crtc_vblank_off()\n");
1003 drm_crtc_vblank_put(crtc);
1008 * drm_atomic_helper_update_legacy_modeset_state - update legacy modeset state
1010 * @old_state: atomic state object with old state structures
1012 * This function updates all the various legacy modeset state pointers in
1013 * connectors, encoders and crtcs. It also updates the timestamping constants
1014 * used for precise vblank timestamps by calling
1015 * drm_calc_timestamping_constants().
1017 * Drivers can use this for building their own atomic commit if they don't have
1018 * a pure helper-based modeset implementation.
1020 * Since these updates are not synchronized with lockings, only code paths
1021 * called from &drm_mode_config_helper_funcs.atomic_commit_tail can look at the
1022 * legacy state filled out by this helper. Defacto this means this helper and
1023 * the legacy state pointers are only really useful for transitioning an
1024 * existing driver to the atomic world.
1027 drm_atomic_helper_update_legacy_modeset_state(struct drm_device *dev,
1028 struct drm_atomic_state *old_state)
1030 struct drm_connector *connector;
1031 struct drm_connector_state *old_conn_state, *new_conn_state;
1032 struct drm_crtc *crtc;
1033 struct drm_crtc_state *new_crtc_state;
1036 /* clear out existing links and update dpms */
1037 for_each_oldnew_connector_in_state(old_state, connector, old_conn_state, new_conn_state, i) {
1038 if (connector->encoder) {
1039 WARN_ON(!connector->encoder->crtc);
1041 connector->encoder->crtc = NULL;
1042 connector->encoder = NULL;
1045 crtc = new_conn_state->crtc;
1046 if ((!crtc && old_conn_state->crtc) ||
1047 (crtc && drm_atomic_crtc_needs_modeset(crtc->state))) {
1048 int mode = DRM_MODE_DPMS_OFF;
1050 if (crtc && crtc->state->active)
1051 mode = DRM_MODE_DPMS_ON;
1053 connector->dpms = mode;
1058 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
1059 if (!new_conn_state->crtc)
1062 if (WARN_ON(!new_conn_state->best_encoder))
1065 connector->encoder = new_conn_state->best_encoder;
1066 connector->encoder->crtc = new_conn_state->crtc;
1069 /* set legacy state in the crtc structure */
1070 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
1071 struct drm_plane *primary = crtc->primary;
1072 struct drm_plane_state *new_plane_state;
1074 crtc->mode = new_crtc_state->mode;
1075 crtc->enabled = new_crtc_state->enable;
1078 drm_atomic_get_new_plane_state(old_state, primary);
1080 if (new_plane_state && new_plane_state->crtc == crtc) {
1081 crtc->x = new_plane_state->src_x >> 16;
1082 crtc->y = new_plane_state->src_y >> 16;
1085 if (new_crtc_state->enable)
1086 drm_calc_timestamping_constants(crtc,
1087 &new_crtc_state->adjusted_mode);
1090 EXPORT_SYMBOL(drm_atomic_helper_update_legacy_modeset_state);
1093 crtc_set_mode(struct drm_device *dev, struct drm_atomic_state *old_state)
1095 struct drm_crtc *crtc;
1096 struct drm_crtc_state *new_crtc_state;
1097 struct drm_connector *connector;
1098 struct drm_connector_state *new_conn_state;
1101 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
1102 const struct drm_crtc_helper_funcs *funcs;
1104 if (!new_crtc_state->mode_changed)
1107 funcs = crtc->helper_private;
1109 if (new_crtc_state->enable && funcs->mode_set_nofb) {
1110 DRM_DEBUG_ATOMIC("modeset on [CRTC:%d:%s]\n",
1111 crtc->base.id, crtc->name);
1113 funcs->mode_set_nofb(crtc);
1117 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
1118 const struct drm_encoder_helper_funcs *funcs;
1119 struct drm_encoder *encoder;
1120 struct drm_display_mode *mode, *adjusted_mode;
1122 if (!new_conn_state->best_encoder)
1125 encoder = new_conn_state->best_encoder;
1126 funcs = encoder->helper_private;
1127 new_crtc_state = new_conn_state->crtc->state;
1128 mode = &new_crtc_state->mode;
1129 adjusted_mode = &new_crtc_state->adjusted_mode;
1131 if (!new_crtc_state->mode_changed)
1134 DRM_DEBUG_ATOMIC("modeset on [ENCODER:%d:%s]\n",
1135 encoder->base.id, encoder->name);
1138 * Each encoder has at most one connector (since we always steal
1139 * it away), so we won't call mode_set hooks twice.
1141 if (funcs && funcs->atomic_mode_set) {
1142 funcs->atomic_mode_set(encoder, new_crtc_state,
1144 } else if (funcs && funcs->mode_set) {
1145 funcs->mode_set(encoder, mode, adjusted_mode);
1148 drm_bridge_mode_set(encoder->bridge, mode, adjusted_mode);
1153 * drm_atomic_helper_commit_modeset_disables - modeset commit to disable outputs
1155 * @old_state: atomic state object with old state structures
1157 * This function shuts down all the outputs that need to be shut down and
1158 * prepares them (if required) with the new mode.
1160 * For compatibility with legacy crtc helpers this should be called before
1161 * drm_atomic_helper_commit_planes(), which is what the default commit function
1162 * does. But drivers with different needs can group the modeset commits together
1163 * and do the plane commits at the end. This is useful for drivers doing runtime
1164 * PM since planes updates then only happen when the CRTC is actually enabled.
1166 void drm_atomic_helper_commit_modeset_disables(struct drm_device *dev,
1167 struct drm_atomic_state *old_state)
1169 disable_outputs(dev, old_state);
1171 drm_atomic_helper_update_legacy_modeset_state(dev, old_state);
1173 crtc_set_mode(dev, old_state);
1175 EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_disables);
1177 static void drm_atomic_helper_commit_writebacks(struct drm_device *dev,
1178 struct drm_atomic_state *old_state)
1180 struct drm_connector *connector;
1181 struct drm_connector_state *new_conn_state;
1184 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
1185 const struct drm_connector_helper_funcs *funcs;
1187 funcs = connector->helper_private;
1188 if (!funcs->atomic_commit)
1191 if (new_conn_state->writeback_job && new_conn_state->writeback_job->fb) {
1192 WARN_ON(connector->connector_type != DRM_MODE_CONNECTOR_WRITEBACK);
1193 funcs->atomic_commit(connector, new_conn_state);
1199 * drm_atomic_helper_commit_modeset_enables - modeset commit to enable outputs
1201 * @old_state: atomic state object with old state structures
1203 * This function enables all the outputs with the new configuration which had to
1204 * be turned off for the update.
1206 * For compatibility with legacy crtc helpers this should be called after
1207 * drm_atomic_helper_commit_planes(), which is what the default commit function
1208 * does. But drivers with different needs can group the modeset commits together
1209 * and do the plane commits at the end. This is useful for drivers doing runtime
1210 * PM since planes updates then only happen when the CRTC is actually enabled.
1212 void drm_atomic_helper_commit_modeset_enables(struct drm_device *dev,
1213 struct drm_atomic_state *old_state)
1215 struct drm_crtc *crtc;
1216 struct drm_crtc_state *old_crtc_state;
1217 struct drm_crtc_state *new_crtc_state;
1218 struct drm_connector *connector;
1219 struct drm_connector_state *new_conn_state;
1222 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
1223 const struct drm_crtc_helper_funcs *funcs;
1225 /* Need to filter out CRTCs where only planes change. */
1226 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
1229 if (!new_crtc_state->active)
1232 funcs = crtc->helper_private;
1234 if (new_crtc_state->enable) {
1235 DRM_DEBUG_ATOMIC("enabling [CRTC:%d:%s]\n",
1236 crtc->base.id, crtc->name);
1238 if (funcs->atomic_enable)
1239 funcs->atomic_enable(crtc, old_crtc_state);
1241 funcs->commit(crtc);
1245 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
1246 const struct drm_encoder_helper_funcs *funcs;
1247 struct drm_encoder *encoder;
1249 if (!new_conn_state->best_encoder)
1252 if (!new_conn_state->crtc->state->active ||
1253 !drm_atomic_crtc_needs_modeset(new_conn_state->crtc->state))
1256 encoder = new_conn_state->best_encoder;
1257 funcs = encoder->helper_private;
1259 DRM_DEBUG_ATOMIC("enabling [ENCODER:%d:%s]\n",
1260 encoder->base.id, encoder->name);
1263 * Each encoder has at most one connector (since we always steal
1264 * it away), so we won't call enable hooks twice.
1266 drm_bridge_pre_enable(encoder->bridge);
1270 funcs->enable(encoder);
1271 else if (funcs->commit)
1272 funcs->commit(encoder);
1275 drm_bridge_enable(encoder->bridge);
1278 drm_atomic_helper_commit_writebacks(dev, old_state);
1280 EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_enables);
1283 * drm_atomic_helper_wait_for_fences - wait for fences stashed in plane state
1285 * @state: atomic state object with old state structures
1286 * @pre_swap: If true, do an interruptible wait, and @state is the new state.
1287 * Otherwise @state is the old state.
1289 * For implicit sync, driver should fish the exclusive fence out from the
1290 * incoming fb's and stash it in the drm_plane_state. This is called after
1291 * drm_atomic_helper_swap_state() so it uses the current plane state (and
1292 * just uses the atomic state to find the changed planes)
1294 * Note that @pre_swap is needed since the point where we block for fences moves
1295 * around depending upon whether an atomic commit is blocking or
1296 * non-blocking. For non-blocking commit all waiting needs to happen after
1297 * drm_atomic_helper_swap_state() is called, but for blocking commits we want
1298 * to wait **before** we do anything that can't be easily rolled back. That is
1299 * before we call drm_atomic_helper_swap_state().
1301 * Returns zero if success or < 0 if dma_fence_wait() fails.
1303 int drm_atomic_helper_wait_for_fences(struct drm_device *dev,
1304 struct drm_atomic_state *state,
1307 struct drm_plane *plane;
1308 struct drm_plane_state *new_plane_state;
1311 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
1312 if (!new_plane_state->fence)
1315 WARN_ON(!new_plane_state->fb);
1318 * If waiting for fences pre-swap (ie: nonblock), userspace can
1319 * still interrupt the operation. Instead of blocking until the
1320 * timer expires, make the wait interruptible.
1322 ret = dma_fence_wait(new_plane_state->fence, pre_swap);
1326 dma_fence_put(new_plane_state->fence);
1327 new_plane_state->fence = NULL;
1332 EXPORT_SYMBOL(drm_atomic_helper_wait_for_fences);
1335 * drm_atomic_helper_wait_for_vblanks - wait for vblank on crtcs
1337 * @old_state: atomic state object with old state structures
1339 * Helper to, after atomic commit, wait for vblanks on all effected
1340 * crtcs (ie. before cleaning up old framebuffers using
1341 * drm_atomic_helper_cleanup_planes()). It will only wait on CRTCs where the
1342 * framebuffers have actually changed to optimize for the legacy cursor and
1343 * plane update use-case.
1345 * Drivers using the nonblocking commit tracking support initialized by calling
1346 * drm_atomic_helper_setup_commit() should look at
1347 * drm_atomic_helper_wait_for_flip_done() as an alternative.
1350 drm_atomic_helper_wait_for_vblanks(struct drm_device *dev,
1351 struct drm_atomic_state *old_state)
1353 struct drm_crtc *crtc;
1354 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
1356 unsigned crtc_mask = 0;
1359 * Legacy cursor ioctls are completely unsynced, and userspace
1360 * relies on that (by doing tons of cursor updates).
1362 if (old_state->legacy_cursor_update)
1365 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
1366 if (!new_crtc_state->active)
1369 ret = drm_crtc_vblank_get(crtc);
1373 crtc_mask |= drm_crtc_mask(crtc);
1374 old_state->crtcs[i].last_vblank_count = drm_crtc_vblank_count(crtc);
1377 for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
1378 if (!(crtc_mask & drm_crtc_mask(crtc)))
1381 ret = wait_event_timeout(dev->vblank[i].queue,
1382 old_state->crtcs[i].last_vblank_count !=
1383 drm_crtc_vblank_count(crtc),
1384 msecs_to_jiffies(50));
1386 WARN(!ret, "[CRTC:%d:%s] vblank wait timed out\n",
1387 crtc->base.id, crtc->name);
1389 drm_crtc_vblank_put(crtc);
1392 EXPORT_SYMBOL(drm_atomic_helper_wait_for_vblanks);
1395 * drm_atomic_helper_wait_for_flip_done - wait for all page flips to be done
1397 * @old_state: atomic state object with old state structures
1399 * Helper to, after atomic commit, wait for page flips on all effected
1400 * crtcs (ie. before cleaning up old framebuffers using
1401 * drm_atomic_helper_cleanup_planes()). Compared to
1402 * drm_atomic_helper_wait_for_vblanks() this waits for the completion of on all
1403 * CRTCs, assuming that cursors-only updates are signalling their completion
1404 * immediately (or using a different path).
1406 * This requires that drivers use the nonblocking commit tracking support
1407 * initialized using drm_atomic_helper_setup_commit().
1409 void drm_atomic_helper_wait_for_flip_done(struct drm_device *dev,
1410 struct drm_atomic_state *old_state)
1412 struct drm_crtc *crtc;
1415 for (i = 0; i < dev->mode_config.num_crtc; i++) {
1416 struct drm_crtc_commit *commit = old_state->crtcs[i].commit;
1419 crtc = old_state->crtcs[i].ptr;
1421 if (!crtc || !commit)
1424 ret = wait_for_completion_timeout(&commit->flip_done, 10 * HZ);
1426 DRM_ERROR("[CRTC:%d:%s] flip_done timed out\n",
1427 crtc->base.id, crtc->name);
1430 EXPORT_SYMBOL(drm_atomic_helper_wait_for_flip_done);
1433 * drm_atomic_helper_commit_tail - commit atomic update to hardware
1434 * @old_state: atomic state object with old state structures
1436 * This is the default implementation for the
1437 * &drm_mode_config_helper_funcs.atomic_commit_tail hook, for drivers
1438 * that do not support runtime_pm or do not need the CRTC to be
1439 * enabled to perform a commit. Otherwise, see
1440 * drm_atomic_helper_commit_tail_rpm().
1442 * Note that the default ordering of how the various stages are called is to
1443 * match the legacy modeset helper library closest.
1445 void drm_atomic_helper_commit_tail(struct drm_atomic_state *old_state)
1447 struct drm_device *dev = old_state->dev;
1449 drm_atomic_helper_commit_modeset_disables(dev, old_state);
1451 drm_atomic_helper_commit_planes(dev, old_state, 0);
1453 drm_atomic_helper_commit_modeset_enables(dev, old_state);
1455 drm_atomic_helper_fake_vblank(old_state);
1457 drm_atomic_helper_commit_hw_done(old_state);
1459 drm_atomic_helper_wait_for_vblanks(dev, old_state);
1461 drm_atomic_helper_cleanup_planes(dev, old_state);
1463 EXPORT_SYMBOL(drm_atomic_helper_commit_tail);
1466 * drm_atomic_helper_commit_tail_rpm - commit atomic update to hardware
1467 * @old_state: new modeset state to be committed
1469 * This is an alternative implementation for the
1470 * &drm_mode_config_helper_funcs.atomic_commit_tail hook, for drivers
1471 * that support runtime_pm or need the CRTC to be enabled to perform a
1472 * commit. Otherwise, one should use the default implementation
1473 * drm_atomic_helper_commit_tail().
1475 void drm_atomic_helper_commit_tail_rpm(struct drm_atomic_state *old_state)
1477 struct drm_device *dev = old_state->dev;
1479 drm_atomic_helper_commit_modeset_disables(dev, old_state);
1481 drm_atomic_helper_commit_modeset_enables(dev, old_state);
1483 drm_atomic_helper_commit_planes(dev, old_state,
1484 DRM_PLANE_COMMIT_ACTIVE_ONLY);
1486 drm_atomic_helper_fake_vblank(old_state);
1488 drm_atomic_helper_commit_hw_done(old_state);
1490 drm_atomic_helper_wait_for_vblanks(dev, old_state);
1492 drm_atomic_helper_cleanup_planes(dev, old_state);
1494 EXPORT_SYMBOL(drm_atomic_helper_commit_tail_rpm);
1496 static void commit_tail(struct drm_atomic_state *old_state)
1498 struct drm_device *dev = old_state->dev;
1499 const struct drm_mode_config_helper_funcs *funcs;
1501 funcs = dev->mode_config.helper_private;
1503 drm_atomic_helper_wait_for_fences(dev, old_state, false);
1505 drm_atomic_helper_wait_for_dependencies(old_state);
1507 if (funcs && funcs->atomic_commit_tail)
1508 funcs->atomic_commit_tail(old_state);
1510 drm_atomic_helper_commit_tail(old_state);
1512 drm_atomic_helper_commit_cleanup_done(old_state);
1514 drm_atomic_state_put(old_state);
1517 static void commit_work(struct work_struct *work)
1519 struct drm_atomic_state *state = container_of(work,
1520 struct drm_atomic_state,
1526 * drm_atomic_helper_async_check - check if state can be commited asynchronously
1528 * @state: the driver state object
1530 * This helper will check if it is possible to commit the state asynchronously.
1531 * Async commits are not supposed to swap the states like normal sync commits
1532 * but just do in-place changes on the current state.
1534 * It will return 0 if the commit can happen in an asynchronous fashion or error
1535 * if not. Note that error just mean it can't be commited asynchronously, if it
1536 * fails the commit should be treated like a normal synchronous commit.
1538 int drm_atomic_helper_async_check(struct drm_device *dev,
1539 struct drm_atomic_state *state)
1541 struct drm_crtc *crtc;
1542 struct drm_crtc_state *crtc_state;
1543 struct drm_plane *plane = NULL;
1544 struct drm_plane_state *old_plane_state = NULL;
1545 struct drm_plane_state *new_plane_state = NULL;
1546 const struct drm_plane_helper_funcs *funcs;
1547 int i, n_planes = 0;
1549 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
1550 if (drm_atomic_crtc_needs_modeset(crtc_state))
1554 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i)
1557 /* FIXME: we support only single plane updates for now */
1561 if (!new_plane_state->crtc ||
1562 old_plane_state->crtc != new_plane_state->crtc)
1565 funcs = plane->helper_private;
1566 if (!funcs->atomic_async_update)
1569 if (new_plane_state->fence)
1573 * Don't do an async update if there is an outstanding commit modifying
1574 * the plane. This prevents our async update's changes from getting
1575 * overridden by a previous synchronous update's state.
1577 if (old_plane_state->commit &&
1578 !try_wait_for_completion(&old_plane_state->commit->hw_done))
1581 return funcs->atomic_async_check(plane, new_plane_state);
1583 EXPORT_SYMBOL(drm_atomic_helper_async_check);
1586 * drm_atomic_helper_async_commit - commit state asynchronously
1588 * @state: the driver state object
1590 * This function commits a state asynchronously, i.e., not vblank
1591 * synchronized. It should be used on a state only when
1592 * drm_atomic_async_check() succeeds. Async commits are not supposed to swap
1593 * the states like normal sync commits, but just do in-place changes on the
1596 void drm_atomic_helper_async_commit(struct drm_device *dev,
1597 struct drm_atomic_state *state)
1599 struct drm_plane *plane;
1600 struct drm_plane_state *plane_state;
1601 const struct drm_plane_helper_funcs *funcs;
1604 for_each_new_plane_in_state(state, plane, plane_state, i) {
1605 funcs = plane->helper_private;
1606 funcs->atomic_async_update(plane, plane_state);
1609 * ->atomic_async_update() is supposed to update the
1610 * plane->state in-place, make sure at least common
1611 * properties have been properly updated.
1613 WARN_ON_ONCE(plane->state->fb != plane_state->fb);
1614 WARN_ON_ONCE(plane->state->crtc_x != plane_state->crtc_x);
1615 WARN_ON_ONCE(plane->state->crtc_y != plane_state->crtc_y);
1616 WARN_ON_ONCE(plane->state->src_x != plane_state->src_x);
1617 WARN_ON_ONCE(plane->state->src_y != plane_state->src_y);
1620 EXPORT_SYMBOL(drm_atomic_helper_async_commit);
1623 * drm_atomic_helper_commit - commit validated state object
1625 * @state: the driver state object
1626 * @nonblock: whether nonblocking behavior is requested.
1628 * This function commits a with drm_atomic_helper_check() pre-validated state
1629 * object. This can still fail when e.g. the framebuffer reservation fails. This
1630 * function implements nonblocking commits, using
1631 * drm_atomic_helper_setup_commit() and related functions.
1633 * Committing the actual hardware state is done through the
1634 * &drm_mode_config_helper_funcs.atomic_commit_tail callback, or it's default
1635 * implementation drm_atomic_helper_commit_tail().
1638 * Zero for success or -errno.
1640 int drm_atomic_helper_commit(struct drm_device *dev,
1641 struct drm_atomic_state *state,
1646 if (state->async_update) {
1647 ret = drm_atomic_helper_prepare_planes(dev, state);
1651 drm_atomic_helper_async_commit(dev, state);
1652 drm_atomic_helper_cleanup_planes(dev, state);
1657 ret = drm_atomic_helper_setup_commit(state, nonblock);
1661 INIT_WORK(&state->commit_work, commit_work);
1663 ret = drm_atomic_helper_prepare_planes(dev, state);
1668 ret = drm_atomic_helper_wait_for_fences(dev, state, true);
1674 * This is the point of no return - everything below never fails except
1675 * when the hw goes bonghits. Which means we can commit the new state on
1676 * the software side now.
1679 ret = drm_atomic_helper_swap_state(state, true);
1684 * Everything below can be run asynchronously without the need to grab
1685 * any modeset locks at all under one condition: It must be guaranteed
1686 * that the asynchronous work has either been cancelled (if the driver
1687 * supports it, which at least requires that the framebuffers get
1688 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
1689 * before the new state gets committed on the software side with
1690 * drm_atomic_helper_swap_state().
1692 * This scheme allows new atomic state updates to be prepared and
1693 * checked in parallel to the asynchronous completion of the previous
1694 * update. Which is important since compositors need to figure out the
1695 * composition of the next frame right after having submitted the
1698 * NOTE: Commit work has multiple phases, first hardware commit, then
1699 * cleanup. We want them to overlap, hence need system_unbound_wq to
1700 * make sure work items don't artifically stall on each another.
1703 drm_atomic_state_get(state);
1705 queue_work(system_unbound_wq, &state->commit_work);
1712 drm_atomic_helper_cleanup_planes(dev, state);
1715 EXPORT_SYMBOL(drm_atomic_helper_commit);
1718 * DOC: implementing nonblocking commit
1720 * Nonblocking atomic commits have to be implemented in the following sequence:
1722 * 1. Run drm_atomic_helper_prepare_planes() first. This is the only function
1723 * which commit needs to call which can fail, so we want to run it first and
1726 * 2. Synchronize with any outstanding nonblocking commit worker threads which
1727 * might be affected the new state update. This can be done by either cancelling
1728 * or flushing the work items, depending upon whether the driver can deal with
1729 * cancelled updates. Note that it is important to ensure that the framebuffer
1730 * cleanup is still done when cancelling.
1732 * Asynchronous workers need to have sufficient parallelism to be able to run
1733 * different atomic commits on different CRTCs in parallel. The simplest way to
1734 * achive this is by running them on the &system_unbound_wq work queue. Note
1735 * that drivers are not required to split up atomic commits and run an
1736 * individual commit in parallel - userspace is supposed to do that if it cares.
1737 * But it might be beneficial to do that for modesets, since those necessarily
1738 * must be done as one global operation, and enabling or disabling a CRTC can
1739 * take a long time. But even that is not required.
1741 * 3. The software state is updated synchronously with
1742 * drm_atomic_helper_swap_state(). Doing this under the protection of all modeset
1743 * locks means concurrent callers never see inconsistent state. And doing this
1744 * while it's guaranteed that no relevant nonblocking worker runs means that
1745 * nonblocking workers do not need grab any locks. Actually they must not grab
1746 * locks, for otherwise the work flushing will deadlock.
1748 * 4. Schedule a work item to do all subsequent steps, using the split-out
1749 * commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and
1750 * then cleaning up the framebuffers after the old framebuffer is no longer
1753 * The above scheme is implemented in the atomic helper libraries in
1754 * drm_atomic_helper_commit() using a bunch of helper functions. See
1755 * drm_atomic_helper_setup_commit() for a starting point.
1758 static int stall_checks(struct drm_crtc *crtc, bool nonblock)
1760 struct drm_crtc_commit *commit, *stall_commit = NULL;
1761 bool completed = true;
1765 spin_lock(&crtc->commit_lock);
1767 list_for_each_entry(commit, &crtc->commit_list, commit_entry) {
1769 completed = try_wait_for_completion(&commit->flip_done);
1770 /* Userspace is not allowed to get ahead of the previous
1771 * commit with nonblocking ones. */
1772 if (!completed && nonblock) {
1773 spin_unlock(&crtc->commit_lock);
1776 } else if (i == 1) {
1777 stall_commit = drm_crtc_commit_get(commit);
1783 spin_unlock(&crtc->commit_lock);
1788 /* We don't want to let commits get ahead of cleanup work too much,
1789 * stalling on 2nd previous commit means triple-buffer won't ever stall.
1791 ret = wait_for_completion_interruptible_timeout(&stall_commit->cleanup_done,
1794 DRM_ERROR("[CRTC:%d:%s] cleanup_done timed out\n",
1795 crtc->base.id, crtc->name);
1797 drm_crtc_commit_put(stall_commit);
1799 return ret < 0 ? ret : 0;
1802 static void release_crtc_commit(struct completion *completion)
1804 struct drm_crtc_commit *commit = container_of(completion,
1808 drm_crtc_commit_put(commit);
1811 static void init_commit(struct drm_crtc_commit *commit, struct drm_crtc *crtc)
1813 init_completion(&commit->flip_done);
1814 init_completion(&commit->hw_done);
1815 init_completion(&commit->cleanup_done);
1816 INIT_LIST_HEAD(&commit->commit_entry);
1817 kref_init(&commit->ref);
1818 commit->crtc = crtc;
1821 static struct drm_crtc_commit *
1822 crtc_or_fake_commit(struct drm_atomic_state *state, struct drm_crtc *crtc)
1825 struct drm_crtc_state *new_crtc_state;
1827 new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
1829 return new_crtc_state->commit;
1832 if (!state->fake_commit) {
1833 state->fake_commit = kzalloc(sizeof(*state->fake_commit), GFP_KERNEL);
1834 if (!state->fake_commit)
1837 init_commit(state->fake_commit, NULL);
1840 return state->fake_commit;
1844 * drm_atomic_helper_setup_commit - setup possibly nonblocking commit
1845 * @state: new modeset state to be committed
1846 * @nonblock: whether nonblocking behavior is requested.
1848 * This function prepares @state to be used by the atomic helper's support for
1849 * nonblocking commits. Drivers using the nonblocking commit infrastructure
1850 * should always call this function from their
1851 * &drm_mode_config_funcs.atomic_commit hook.
1853 * To be able to use this support drivers need to use a few more helper
1854 * functions. drm_atomic_helper_wait_for_dependencies() must be called before
1855 * actually committing the hardware state, and for nonblocking commits this call
1856 * must be placed in the async worker. See also drm_atomic_helper_swap_state()
1857 * and it's stall parameter, for when a driver's commit hooks look at the
1858 * &drm_crtc.state, &drm_plane.state or &drm_connector.state pointer directly.
1860 * Completion of the hardware commit step must be signalled using
1861 * drm_atomic_helper_commit_hw_done(). After this step the driver is not allowed
1862 * to read or change any permanent software or hardware modeset state. The only
1863 * exception is state protected by other means than &drm_modeset_lock locks.
1864 * Only the free standing @state with pointers to the old state structures can
1865 * be inspected, e.g. to clean up old buffers using
1866 * drm_atomic_helper_cleanup_planes().
1868 * At the very end, before cleaning up @state drivers must call
1869 * drm_atomic_helper_commit_cleanup_done().
1871 * This is all implemented by in drm_atomic_helper_commit(), giving drivers a
1872 * complete and easy-to-use default implementation of the atomic_commit() hook.
1874 * The tracking of asynchronously executed and still pending commits is done
1875 * using the core structure &drm_crtc_commit.
1877 * By default there's no need to clean up resources allocated by this function
1878 * explicitly: drm_atomic_state_default_clear() will take care of that
1883 * 0 on success. -EBUSY when userspace schedules nonblocking commits too fast,
1884 * -ENOMEM on allocation failures and -EINTR when a signal is pending.
1886 int drm_atomic_helper_setup_commit(struct drm_atomic_state *state,
1889 struct drm_crtc *crtc;
1890 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
1891 struct drm_connector *conn;
1892 struct drm_connector_state *old_conn_state, *new_conn_state;
1893 struct drm_plane *plane;
1894 struct drm_plane_state *old_plane_state, *new_plane_state;
1895 struct drm_crtc_commit *commit;
1898 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
1899 commit = kzalloc(sizeof(*commit), GFP_KERNEL);
1903 init_commit(commit, crtc);
1905 new_crtc_state->commit = commit;
1907 ret = stall_checks(crtc, nonblock);
1911 /* Drivers only send out events when at least either current or
1912 * new CRTC state is active. Complete right away if everything
1914 if (!old_crtc_state->active && !new_crtc_state->active) {
1915 complete_all(&commit->flip_done);
1919 /* Legacy cursor updates are fully unsynced. */
1920 if (state->legacy_cursor_update) {
1921 complete_all(&commit->flip_done);
1925 if (!new_crtc_state->event) {
1926 commit->event = kzalloc(sizeof(*commit->event),
1931 new_crtc_state->event = commit->event;
1934 new_crtc_state->event->base.completion = &commit->flip_done;
1935 new_crtc_state->event->base.completion_release = release_crtc_commit;
1936 drm_crtc_commit_get(commit);
1938 commit->abort_completion = true;
1940 state->crtcs[i].commit = commit;
1941 drm_crtc_commit_get(commit);
1944 for_each_oldnew_connector_in_state(state, conn, old_conn_state, new_conn_state, i) {
1945 /* Userspace is not allowed to get ahead of the previous
1946 * commit with nonblocking ones. */
1947 if (nonblock && old_conn_state->commit &&
1948 !try_wait_for_completion(&old_conn_state->commit->flip_done))
1951 /* Always track connectors explicitly for e.g. link retraining. */
1952 commit = crtc_or_fake_commit(state, new_conn_state->crtc ?: old_conn_state->crtc);
1956 new_conn_state->commit = drm_crtc_commit_get(commit);
1959 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
1960 /* Userspace is not allowed to get ahead of the previous
1961 * commit with nonblocking ones. */
1962 if (nonblock && old_plane_state->commit &&
1963 !try_wait_for_completion(&old_plane_state->commit->flip_done))
1966 /* Always track planes explicitly for async pageflip support. */
1967 commit = crtc_or_fake_commit(state, new_plane_state->crtc ?: old_plane_state->crtc);
1971 new_plane_state->commit = drm_crtc_commit_get(commit);
1976 EXPORT_SYMBOL(drm_atomic_helper_setup_commit);
1979 * drm_atomic_helper_wait_for_dependencies - wait for required preceeding commits
1980 * @old_state: atomic state object with old state structures
1982 * This function waits for all preceeding commits that touch the same CRTC as
1983 * @old_state to both be committed to the hardware (as signalled by
1984 * drm_atomic_helper_commit_hw_done) and executed by the hardware (as signalled
1985 * by calling drm_crtc_send_vblank_event() on the &drm_crtc_state.event).
1987 * This is part of the atomic helper support for nonblocking commits, see
1988 * drm_atomic_helper_setup_commit() for an overview.
1990 void drm_atomic_helper_wait_for_dependencies(struct drm_atomic_state *old_state)
1992 struct drm_crtc *crtc;
1993 struct drm_crtc_state *old_crtc_state;
1994 struct drm_plane *plane;
1995 struct drm_plane_state *old_plane_state;
1996 struct drm_connector *conn;
1997 struct drm_connector_state *old_conn_state;
1998 struct drm_crtc_commit *commit;
2002 for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
2003 commit = old_crtc_state->commit;
2008 ret = wait_for_completion_timeout(&commit->hw_done,
2011 DRM_ERROR("[CRTC:%d:%s] hw_done timed out\n",
2012 crtc->base.id, crtc->name);
2014 /* Currently no support for overwriting flips, hence
2015 * stall for previous one to execute completely. */
2016 ret = wait_for_completion_timeout(&commit->flip_done,
2019 DRM_ERROR("[CRTC:%d:%s] flip_done timed out\n",
2020 crtc->base.id, crtc->name);
2023 for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
2024 commit = old_conn_state->commit;
2029 ret = wait_for_completion_timeout(&commit->hw_done,
2032 DRM_ERROR("[CONNECTOR:%d:%s] hw_done timed out\n",
2033 conn->base.id, conn->name);
2035 /* Currently no support for overwriting flips, hence
2036 * stall for previous one to execute completely. */
2037 ret = wait_for_completion_timeout(&commit->flip_done,
2040 DRM_ERROR("[CONNECTOR:%d:%s] flip_done timed out\n",
2041 conn->base.id, conn->name);
2044 for_each_old_plane_in_state(old_state, plane, old_plane_state, i) {
2045 commit = old_plane_state->commit;
2050 ret = wait_for_completion_timeout(&commit->hw_done,
2053 DRM_ERROR("[PLANE:%d:%s] hw_done timed out\n",
2054 plane->base.id, plane->name);
2056 /* Currently no support for overwriting flips, hence
2057 * stall for previous one to execute completely. */
2058 ret = wait_for_completion_timeout(&commit->flip_done,
2061 DRM_ERROR("[PLANE:%d:%s] flip_done timed out\n",
2062 plane->base.id, plane->name);
2065 EXPORT_SYMBOL(drm_atomic_helper_wait_for_dependencies);
2068 * drm_atomic_helper_fake_vblank - fake VBLANK events if needed
2069 * @old_state: atomic state object with old state structures
2071 * This function walks all CRTCs and fake VBLANK events on those with
2072 * &drm_crtc_state.no_vblank set to true and &drm_crtc_state.event != NULL.
2073 * The primary use of this function is writeback connectors working in oneshot
2074 * mode and faking VBLANK events. In this case they only fake the VBLANK event
2075 * when a job is queued, and any change to the pipeline that does not touch the
2076 * connector is leading to timeouts when calling
2077 * drm_atomic_helper_wait_for_vblanks() or
2078 * drm_atomic_helper_wait_for_flip_done().
2080 * This is part of the atomic helper support for nonblocking commits, see
2081 * drm_atomic_helper_setup_commit() for an overview.
2083 void drm_atomic_helper_fake_vblank(struct drm_atomic_state *old_state)
2085 struct drm_crtc_state *new_crtc_state;
2086 struct drm_crtc *crtc;
2089 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
2090 unsigned long flags;
2092 if (!new_crtc_state->no_vblank)
2095 spin_lock_irqsave(&old_state->dev->event_lock, flags);
2096 if (new_crtc_state->event) {
2097 drm_crtc_send_vblank_event(crtc,
2098 new_crtc_state->event);
2099 new_crtc_state->event = NULL;
2101 spin_unlock_irqrestore(&old_state->dev->event_lock, flags);
2104 EXPORT_SYMBOL(drm_atomic_helper_fake_vblank);
2107 * drm_atomic_helper_commit_hw_done - setup possible nonblocking commit
2108 * @old_state: atomic state object with old state structures
2110 * This function is used to signal completion of the hardware commit step. After
2111 * this step the driver is not allowed to read or change any permanent software
2112 * or hardware modeset state. The only exception is state protected by other
2113 * means than &drm_modeset_lock locks.
2115 * Drivers should try to postpone any expensive or delayed cleanup work after
2116 * this function is called.
2118 * This is part of the atomic helper support for nonblocking commits, see
2119 * drm_atomic_helper_setup_commit() for an overview.
2121 void drm_atomic_helper_commit_hw_done(struct drm_atomic_state *old_state)
2123 struct drm_crtc *crtc;
2124 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
2125 struct drm_crtc_commit *commit;
2128 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
2129 commit = new_crtc_state->commit;
2134 * copy new_crtc_state->commit to old_crtc_state->commit,
2135 * it's unsafe to touch new_crtc_state after hw_done,
2136 * but we still need to do so in cleanup_done().
2138 if (old_crtc_state->commit)
2139 drm_crtc_commit_put(old_crtc_state->commit);
2141 old_crtc_state->commit = drm_crtc_commit_get(commit);
2143 /* backend must have consumed any event by now */
2144 WARN_ON(new_crtc_state->event);
2145 complete_all(&commit->hw_done);
2148 if (old_state->fake_commit) {
2149 complete_all(&old_state->fake_commit->hw_done);
2150 complete_all(&old_state->fake_commit->flip_done);
2153 EXPORT_SYMBOL(drm_atomic_helper_commit_hw_done);
2156 * drm_atomic_helper_commit_cleanup_done - signal completion of commit
2157 * @old_state: atomic state object with old state structures
2159 * This signals completion of the atomic update @old_state, including any
2160 * cleanup work. If used, it must be called right before calling
2161 * drm_atomic_state_put().
2163 * This is part of the atomic helper support for nonblocking commits, see
2164 * drm_atomic_helper_setup_commit() for an overview.
2166 void drm_atomic_helper_commit_cleanup_done(struct drm_atomic_state *old_state)
2168 struct drm_crtc *crtc;
2169 struct drm_crtc_state *old_crtc_state;
2170 struct drm_crtc_commit *commit;
2173 for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
2174 commit = old_crtc_state->commit;
2175 if (WARN_ON(!commit))
2178 complete_all(&commit->cleanup_done);
2179 WARN_ON(!try_wait_for_completion(&commit->hw_done));
2181 spin_lock(&crtc->commit_lock);
2182 list_del(&commit->commit_entry);
2183 spin_unlock(&crtc->commit_lock);
2186 if (old_state->fake_commit)
2187 complete_all(&old_state->fake_commit->cleanup_done);
2189 EXPORT_SYMBOL(drm_atomic_helper_commit_cleanup_done);
2192 * drm_atomic_helper_prepare_planes - prepare plane resources before commit
2194 * @state: atomic state object with new state structures
2196 * This function prepares plane state, specifically framebuffers, for the new
2197 * configuration, by calling &drm_plane_helper_funcs.prepare_fb. If any failure
2198 * is encountered this function will call &drm_plane_helper_funcs.cleanup_fb on
2199 * any already successfully prepared framebuffer.
2202 * 0 on success, negative error code on failure.
2204 int drm_atomic_helper_prepare_planes(struct drm_device *dev,
2205 struct drm_atomic_state *state)
2207 struct drm_plane *plane;
2208 struct drm_plane_state *new_plane_state;
2211 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
2212 const struct drm_plane_helper_funcs *funcs;
2214 funcs = plane->helper_private;
2216 if (funcs->prepare_fb) {
2217 ret = funcs->prepare_fb(plane, new_plane_state);
2226 for_each_new_plane_in_state(state, plane, new_plane_state, j) {
2227 const struct drm_plane_helper_funcs *funcs;
2232 funcs = plane->helper_private;
2234 if (funcs->cleanup_fb)
2235 funcs->cleanup_fb(plane, new_plane_state);
2240 EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
2242 static bool plane_crtc_active(const struct drm_plane_state *state)
2244 return state->crtc && state->crtc->state->active;
2248 * drm_atomic_helper_commit_planes - commit plane state
2250 * @old_state: atomic state object with old state structures
2251 * @flags: flags for committing plane state
2253 * This function commits the new plane state using the plane and atomic helper
2254 * functions for planes and crtcs. It assumes that the atomic state has already
2255 * been pushed into the relevant object state pointers, since this step can no
2258 * It still requires the global state object @old_state to know which planes and
2259 * crtcs need to be updated though.
2261 * Note that this function does all plane updates across all CRTCs in one step.
2262 * If the hardware can't support this approach look at
2263 * drm_atomic_helper_commit_planes_on_crtc() instead.
2265 * Plane parameters can be updated by applications while the associated CRTC is
2266 * disabled. The DRM/KMS core will store the parameters in the plane state,
2267 * which will be available to the driver when the CRTC is turned on. As a result
2268 * most drivers don't need to be immediately notified of plane updates for a
2271 * Unless otherwise needed, drivers are advised to set the ACTIVE_ONLY flag in
2272 * @flags in order not to receive plane update notifications related to a
2273 * disabled CRTC. This avoids the need to manually ignore plane updates in
2274 * driver code when the driver and/or hardware can't or just don't need to deal
2275 * with updates on disabled CRTCs, for example when supporting runtime PM.
2277 * Drivers may set the NO_DISABLE_AFTER_MODESET flag in @flags if the relevant
2278 * display controllers require to disable a CRTC's planes when the CRTC is
2279 * disabled. This function would skip the &drm_plane_helper_funcs.atomic_disable
2280 * call for a plane if the CRTC of the old plane state needs a modesetting
2281 * operation. Of course, the drivers need to disable the planes in their CRTC
2282 * disable callbacks since no one else would do that.
2284 * The drm_atomic_helper_commit() default implementation doesn't set the
2285 * ACTIVE_ONLY flag to most closely match the behaviour of the legacy helpers.
2286 * This should not be copied blindly by drivers.
2288 void drm_atomic_helper_commit_planes(struct drm_device *dev,
2289 struct drm_atomic_state *old_state,
2292 struct drm_crtc *crtc;
2293 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
2294 struct drm_plane *plane;
2295 struct drm_plane_state *old_plane_state, *new_plane_state;
2297 bool active_only = flags & DRM_PLANE_COMMIT_ACTIVE_ONLY;
2298 bool no_disable = flags & DRM_PLANE_COMMIT_NO_DISABLE_AFTER_MODESET;
2300 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
2301 const struct drm_crtc_helper_funcs *funcs;
2303 funcs = crtc->helper_private;
2305 if (!funcs || !funcs->atomic_begin)
2308 if (active_only && !new_crtc_state->active)
2311 funcs->atomic_begin(crtc, old_crtc_state);
2314 for_each_oldnew_plane_in_state(old_state, plane, old_plane_state, new_plane_state, i) {
2315 const struct drm_plane_helper_funcs *funcs;
2318 funcs = plane->helper_private;
2323 disabling = drm_atomic_plane_disabling(old_plane_state,
2328 * Skip planes related to inactive CRTCs. If the plane
2329 * is enabled use the state of the current CRTC. If the
2330 * plane is being disabled use the state of the old
2331 * CRTC to avoid skipping planes being disabled on an
2334 if (!disabling && !plane_crtc_active(new_plane_state))
2336 if (disabling && !plane_crtc_active(old_plane_state))
2341 * Special-case disabling the plane if drivers support it.
2343 if (disabling && funcs->atomic_disable) {
2344 struct drm_crtc_state *crtc_state;
2346 crtc_state = old_plane_state->crtc->state;
2348 if (drm_atomic_crtc_needs_modeset(crtc_state) &&
2352 funcs->atomic_disable(plane, old_plane_state);
2353 } else if (new_plane_state->crtc || disabling) {
2354 funcs->atomic_update(plane, old_plane_state);
2358 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
2359 const struct drm_crtc_helper_funcs *funcs;
2361 funcs = crtc->helper_private;
2363 if (!funcs || !funcs->atomic_flush)
2366 if (active_only && !new_crtc_state->active)
2369 funcs->atomic_flush(crtc, old_crtc_state);
2372 EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
2375 * drm_atomic_helper_commit_planes_on_crtc - commit plane state for a crtc
2376 * @old_crtc_state: atomic state object with the old crtc state
2378 * This function commits the new plane state using the plane and atomic helper
2379 * functions for planes on the specific crtc. It assumes that the atomic state
2380 * has already been pushed into the relevant object state pointers, since this
2381 * step can no longer fail.
2383 * This function is useful when plane updates should be done crtc-by-crtc
2384 * instead of one global step like drm_atomic_helper_commit_planes() does.
2386 * This function can only be savely used when planes are not allowed to move
2387 * between different CRTCs because this function doesn't handle inter-CRTC
2388 * depencies. Callers need to ensure that either no such depencies exist,
2389 * resolve them through ordering of commit calls or through some other means.
2392 drm_atomic_helper_commit_planes_on_crtc(struct drm_crtc_state *old_crtc_state)
2394 const struct drm_crtc_helper_funcs *crtc_funcs;
2395 struct drm_crtc *crtc = old_crtc_state->crtc;
2396 struct drm_atomic_state *old_state = old_crtc_state->state;
2397 struct drm_crtc_state *new_crtc_state =
2398 drm_atomic_get_new_crtc_state(old_state, crtc);
2399 struct drm_plane *plane;
2400 unsigned plane_mask;
2402 plane_mask = old_crtc_state->plane_mask;
2403 plane_mask |= new_crtc_state->plane_mask;
2405 crtc_funcs = crtc->helper_private;
2406 if (crtc_funcs && crtc_funcs->atomic_begin)
2407 crtc_funcs->atomic_begin(crtc, old_crtc_state);
2409 drm_for_each_plane_mask(plane, crtc->dev, plane_mask) {
2410 struct drm_plane_state *old_plane_state =
2411 drm_atomic_get_old_plane_state(old_state, plane);
2412 struct drm_plane_state *new_plane_state =
2413 drm_atomic_get_new_plane_state(old_state, plane);
2414 const struct drm_plane_helper_funcs *plane_funcs;
2416 plane_funcs = plane->helper_private;
2418 if (!old_plane_state || !plane_funcs)
2421 WARN_ON(new_plane_state->crtc &&
2422 new_plane_state->crtc != crtc);
2424 if (drm_atomic_plane_disabling(old_plane_state, new_plane_state) &&
2425 plane_funcs->atomic_disable)
2426 plane_funcs->atomic_disable(plane, old_plane_state);
2427 else if (new_plane_state->crtc ||
2428 drm_atomic_plane_disabling(old_plane_state, new_plane_state))
2429 plane_funcs->atomic_update(plane, old_plane_state);
2432 if (crtc_funcs && crtc_funcs->atomic_flush)
2433 crtc_funcs->atomic_flush(crtc, old_crtc_state);
2435 EXPORT_SYMBOL(drm_atomic_helper_commit_planes_on_crtc);
2438 * drm_atomic_helper_disable_planes_on_crtc - helper to disable CRTC's planes
2439 * @old_crtc_state: atomic state object with the old CRTC state
2440 * @atomic: if set, synchronize with CRTC's atomic_begin/flush hooks
2442 * Disables all planes associated with the given CRTC. This can be
2443 * used for instance in the CRTC helper atomic_disable callback to disable
2446 * If the atomic-parameter is set the function calls the CRTC's
2447 * atomic_begin hook before and atomic_flush hook after disabling the
2450 * It is a bug to call this function without having implemented the
2451 * &drm_plane_helper_funcs.atomic_disable plane hook.
2454 drm_atomic_helper_disable_planes_on_crtc(struct drm_crtc_state *old_crtc_state,
2457 struct drm_crtc *crtc = old_crtc_state->crtc;
2458 const struct drm_crtc_helper_funcs *crtc_funcs =
2459 crtc->helper_private;
2460 struct drm_plane *plane;
2462 if (atomic && crtc_funcs && crtc_funcs->atomic_begin)
2463 crtc_funcs->atomic_begin(crtc, NULL);
2465 drm_atomic_crtc_state_for_each_plane(plane, old_crtc_state) {
2466 const struct drm_plane_helper_funcs *plane_funcs =
2467 plane->helper_private;
2472 WARN_ON(!plane_funcs->atomic_disable);
2473 if (plane_funcs->atomic_disable)
2474 plane_funcs->atomic_disable(plane, NULL);
2477 if (atomic && crtc_funcs && crtc_funcs->atomic_flush)
2478 crtc_funcs->atomic_flush(crtc, NULL);
2480 EXPORT_SYMBOL(drm_atomic_helper_disable_planes_on_crtc);
2483 * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
2485 * @old_state: atomic state object with old state structures
2487 * This function cleans up plane state, specifically framebuffers, from the old
2488 * configuration. Hence the old configuration must be perserved in @old_state to
2489 * be able to call this function.
2491 * This function must also be called on the new state when the atomic update
2492 * fails at any point after calling drm_atomic_helper_prepare_planes().
2494 void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
2495 struct drm_atomic_state *old_state)
2497 struct drm_plane *plane;
2498 struct drm_plane_state *old_plane_state, *new_plane_state;
2501 for_each_oldnew_plane_in_state(old_state, plane, old_plane_state, new_plane_state, i) {
2502 const struct drm_plane_helper_funcs *funcs;
2503 struct drm_plane_state *plane_state;
2506 * This might be called before swapping when commit is aborted,
2507 * in which case we have to cleanup the new state.
2509 if (old_plane_state == plane->state)
2510 plane_state = new_plane_state;
2512 plane_state = old_plane_state;
2514 funcs = plane->helper_private;
2516 if (funcs->cleanup_fb)
2517 funcs->cleanup_fb(plane, plane_state);
2520 EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
2523 * drm_atomic_helper_swap_state - store atomic state into current sw state
2524 * @state: atomic state
2525 * @stall: stall for preceeding commits
2527 * This function stores the atomic state into the current state pointers in all
2528 * driver objects. It should be called after all failing steps have been done
2529 * and succeeded, but before the actual hardware state is committed.
2531 * For cleanup and error recovery the current state for all changed objects will
2532 * be swapped into @state.
2534 * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
2536 * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
2538 * 2. Do any other steps that might fail.
2540 * 3. Put the staged state into the current state pointers with this function.
2542 * 4. Actually commit the hardware state.
2544 * 5. Call drm_atomic_helper_cleanup_planes() with @state, which since step 3
2545 * contains the old state. Also do any other cleanup required with that state.
2547 * @stall must be set when nonblocking commits for this driver directly access
2548 * the &drm_plane.state, &drm_crtc.state or &drm_connector.state pointer. With
2549 * the current atomic helpers this is almost always the case, since the helpers
2550 * don't pass the right state structures to the callbacks.
2554 * Returns 0 on success. Can return -ERESTARTSYS when @stall is true and the
2555 * waiting for the previous commits has been interrupted.
2557 int drm_atomic_helper_swap_state(struct drm_atomic_state *state,
2561 struct drm_connector *connector;
2562 struct drm_connector_state *old_conn_state, *new_conn_state;
2563 struct drm_crtc *crtc;
2564 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
2565 struct drm_plane *plane;
2566 struct drm_plane_state *old_plane_state, *new_plane_state;
2567 struct drm_crtc_commit *commit;
2568 struct drm_private_obj *obj;
2569 struct drm_private_state *old_obj_state, *new_obj_state;
2573 * We have to stall for hw_done here before
2574 * drm_atomic_helper_wait_for_dependencies() because flip
2575 * depth > 1 is not yet supported by all drivers. As long as
2576 * obj->state is directly dereferenced anywhere in the drivers
2577 * atomic_commit_tail function, then it's unsafe to swap state
2578 * before drm_atomic_helper_commit_hw_done() is called.
2581 for_each_old_crtc_in_state(state, crtc, old_crtc_state, i) {
2582 commit = old_crtc_state->commit;
2587 ret = wait_for_completion_interruptible(&commit->hw_done);
2592 for_each_old_connector_in_state(state, connector, old_conn_state, i) {
2593 commit = old_conn_state->commit;
2598 ret = wait_for_completion_interruptible(&commit->hw_done);
2603 for_each_old_plane_in_state(state, plane, old_plane_state, i) {
2604 commit = old_plane_state->commit;
2609 ret = wait_for_completion_interruptible(&commit->hw_done);
2615 for_each_oldnew_connector_in_state(state, connector, old_conn_state, new_conn_state, i) {
2616 WARN_ON(connector->state != old_conn_state);
2618 old_conn_state->state = state;
2619 new_conn_state->state = NULL;
2621 state->connectors[i].state = old_conn_state;
2622 connector->state = new_conn_state;
2625 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
2626 WARN_ON(crtc->state != old_crtc_state);
2628 old_crtc_state->state = state;
2629 new_crtc_state->state = NULL;
2631 state->crtcs[i].state = old_crtc_state;
2632 crtc->state = new_crtc_state;
2634 if (new_crtc_state->commit) {
2635 spin_lock(&crtc->commit_lock);
2636 list_add(&new_crtc_state->commit->commit_entry,
2637 &crtc->commit_list);
2638 spin_unlock(&crtc->commit_lock);
2640 new_crtc_state->commit->event = NULL;
2644 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
2645 WARN_ON(plane->state != old_plane_state);
2647 old_plane_state->state = state;
2648 new_plane_state->state = NULL;
2650 state->planes[i].state = old_plane_state;
2651 plane->state = new_plane_state;
2654 for_each_oldnew_private_obj_in_state(state, obj, old_obj_state, new_obj_state, i) {
2655 WARN_ON(obj->state != old_obj_state);
2657 old_obj_state->state = state;
2658 new_obj_state->state = NULL;
2660 state->private_objs[i].state = old_obj_state;
2661 obj->state = new_obj_state;
2666 EXPORT_SYMBOL(drm_atomic_helper_swap_state);
2669 * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
2670 * @plane: plane object to update
2671 * @crtc: owning CRTC of owning plane
2672 * @fb: framebuffer to flip onto plane
2673 * @crtc_x: x offset of primary plane on crtc
2674 * @crtc_y: y offset of primary plane on crtc
2675 * @crtc_w: width of primary plane rectangle on crtc
2676 * @crtc_h: height of primary plane rectangle on crtc
2677 * @src_x: x offset of @fb for panning
2678 * @src_y: y offset of @fb for panning
2679 * @src_w: width of source rectangle in @fb
2680 * @src_h: height of source rectangle in @fb
2681 * @ctx: lock acquire context
2683 * Provides a default plane update handler using the atomic driver interface.
2686 * Zero on success, error code on failure
2688 int drm_atomic_helper_update_plane(struct drm_plane *plane,
2689 struct drm_crtc *crtc,
2690 struct drm_framebuffer *fb,
2691 int crtc_x, int crtc_y,
2692 unsigned int crtc_w, unsigned int crtc_h,
2693 uint32_t src_x, uint32_t src_y,
2694 uint32_t src_w, uint32_t src_h,
2695 struct drm_modeset_acquire_ctx *ctx)
2697 struct drm_atomic_state *state;
2698 struct drm_plane_state *plane_state;
2701 state = drm_atomic_state_alloc(plane->dev);
2705 state->acquire_ctx = ctx;
2706 plane_state = drm_atomic_get_plane_state(state, plane);
2707 if (IS_ERR(plane_state)) {
2708 ret = PTR_ERR(plane_state);
2712 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
2715 drm_atomic_set_fb_for_plane(plane_state, fb);
2716 plane_state->crtc_x = crtc_x;
2717 plane_state->crtc_y = crtc_y;
2718 plane_state->crtc_w = crtc_w;
2719 plane_state->crtc_h = crtc_h;
2720 plane_state->src_x = src_x;
2721 plane_state->src_y = src_y;
2722 plane_state->src_w = src_w;
2723 plane_state->src_h = src_h;
2725 if (plane == crtc->cursor)
2726 state->legacy_cursor_update = true;
2728 ret = drm_atomic_commit(state);
2730 drm_atomic_state_put(state);
2733 EXPORT_SYMBOL(drm_atomic_helper_update_plane);
2736 * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
2737 * @plane: plane to disable
2738 * @ctx: lock acquire context
2740 * Provides a default plane disable handler using the atomic driver interface.
2743 * Zero on success, error code on failure
2745 int drm_atomic_helper_disable_plane(struct drm_plane *plane,
2746 struct drm_modeset_acquire_ctx *ctx)
2748 struct drm_atomic_state *state;
2749 struct drm_plane_state *plane_state;
2752 state = drm_atomic_state_alloc(plane->dev);
2756 state->acquire_ctx = ctx;
2757 plane_state = drm_atomic_get_plane_state(state, plane);
2758 if (IS_ERR(plane_state)) {
2759 ret = PTR_ERR(plane_state);
2763 if (plane_state->crtc && plane_state->crtc->cursor == plane)
2764 plane_state->state->legacy_cursor_update = true;
2766 ret = __drm_atomic_helper_disable_plane(plane, plane_state);
2770 ret = drm_atomic_commit(state);
2772 drm_atomic_state_put(state);
2775 EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
2777 /* just used from fb-helper and atomic-helper: */
2778 int __drm_atomic_helper_disable_plane(struct drm_plane *plane,
2779 struct drm_plane_state *plane_state)
2783 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
2787 drm_atomic_set_fb_for_plane(plane_state, NULL);
2788 plane_state->crtc_x = 0;
2789 plane_state->crtc_y = 0;
2790 plane_state->crtc_w = 0;
2791 plane_state->crtc_h = 0;
2792 plane_state->src_x = 0;
2793 plane_state->src_y = 0;
2794 plane_state->src_w = 0;
2795 plane_state->src_h = 0;
2800 static int update_output_state(struct drm_atomic_state *state,
2801 struct drm_mode_set *set)
2803 struct drm_device *dev = set->crtc->dev;
2804 struct drm_crtc *crtc;
2805 struct drm_crtc_state *new_crtc_state;
2806 struct drm_connector *connector;
2807 struct drm_connector_state *new_conn_state;
2810 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
2811 state->acquire_ctx);
2815 /* First disable all connectors on the target crtc. */
2816 ret = drm_atomic_add_affected_connectors(state, set->crtc);
2820 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
2821 if (new_conn_state->crtc == set->crtc) {
2822 ret = drm_atomic_set_crtc_for_connector(new_conn_state,
2827 /* Make sure legacy setCrtc always re-trains */
2828 new_conn_state->link_status = DRM_LINK_STATUS_GOOD;
2832 /* Then set all connectors from set->connectors on the target crtc */
2833 for (i = 0; i < set->num_connectors; i++) {
2834 new_conn_state = drm_atomic_get_connector_state(state,
2835 set->connectors[i]);
2836 if (IS_ERR(new_conn_state))
2837 return PTR_ERR(new_conn_state);
2839 ret = drm_atomic_set_crtc_for_connector(new_conn_state,
2845 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
2846 /* Don't update ->enable for the CRTC in the set_config request,
2847 * since a mismatch would indicate a bug in the upper layers.
2848 * The actual modeset code later on will catch any
2849 * inconsistencies here. */
2850 if (crtc == set->crtc)
2853 if (!new_crtc_state->connector_mask) {
2854 ret = drm_atomic_set_mode_prop_for_crtc(new_crtc_state,
2859 new_crtc_state->active = false;
2867 * drm_atomic_helper_set_config - set a new config from userspace
2868 * @set: mode set configuration
2869 * @ctx: lock acquisition context
2871 * Provides a default crtc set_config handler using the atomic driver interface.
2873 * NOTE: For backwards compatibility with old userspace this automatically
2874 * resets the "link-status" property to GOOD, to force any link
2875 * re-training. The SETCRTC ioctl does not define whether an update does
2876 * need a full modeset or just a plane update, hence we're allowed to do
2877 * that. See also drm_connector_set_link_status_property().
2880 * Returns 0 on success, negative errno numbers on failure.
2882 int drm_atomic_helper_set_config(struct drm_mode_set *set,
2883 struct drm_modeset_acquire_ctx *ctx)
2885 struct drm_atomic_state *state;
2886 struct drm_crtc *crtc = set->crtc;
2889 state = drm_atomic_state_alloc(crtc->dev);
2893 state->acquire_ctx = ctx;
2894 ret = __drm_atomic_helper_set_config(set, state);
2898 ret = handle_conflicting_encoders(state, true);
2902 ret = drm_atomic_commit(state);
2905 drm_atomic_state_put(state);
2908 EXPORT_SYMBOL(drm_atomic_helper_set_config);
2910 /* just used from fb-helper and atomic-helper: */
2911 int __drm_atomic_helper_set_config(struct drm_mode_set *set,
2912 struct drm_atomic_state *state)
2914 struct drm_crtc_state *crtc_state;
2915 struct drm_plane_state *primary_state;
2916 struct drm_crtc *crtc = set->crtc;
2917 int hdisplay, vdisplay;
2920 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2921 if (IS_ERR(crtc_state))
2922 return PTR_ERR(crtc_state);
2924 primary_state = drm_atomic_get_plane_state(state, crtc->primary);
2925 if (IS_ERR(primary_state))
2926 return PTR_ERR(primary_state);
2930 WARN_ON(set->num_connectors);
2932 ret = drm_atomic_set_mode_for_crtc(crtc_state, NULL);
2936 crtc_state->active = false;
2938 ret = drm_atomic_set_crtc_for_plane(primary_state, NULL);
2942 drm_atomic_set_fb_for_plane(primary_state, NULL);
2948 WARN_ON(!set->num_connectors);
2950 ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
2954 crtc_state->active = true;
2956 ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
2960 drm_mode_get_hv_timing(set->mode, &hdisplay, &vdisplay);
2962 drm_atomic_set_fb_for_plane(primary_state, set->fb);
2963 primary_state->crtc_x = 0;
2964 primary_state->crtc_y = 0;
2965 primary_state->crtc_w = hdisplay;
2966 primary_state->crtc_h = vdisplay;
2967 primary_state->src_x = set->x << 16;
2968 primary_state->src_y = set->y << 16;
2969 if (drm_rotation_90_or_270(primary_state->rotation)) {
2970 primary_state->src_w = vdisplay << 16;
2971 primary_state->src_h = hdisplay << 16;
2973 primary_state->src_w = hdisplay << 16;
2974 primary_state->src_h = vdisplay << 16;
2978 ret = update_output_state(state, set);
2985 static int __drm_atomic_helper_disable_all(struct drm_device *dev,
2986 struct drm_modeset_acquire_ctx *ctx,
2989 struct drm_atomic_state *state;
2990 struct drm_connector_state *conn_state;
2991 struct drm_connector *conn;
2992 struct drm_plane_state *plane_state;
2993 struct drm_plane *plane;
2994 struct drm_crtc_state *crtc_state;
2995 struct drm_crtc *crtc;
2998 state = drm_atomic_state_alloc(dev);
3002 state->acquire_ctx = ctx;
3004 drm_for_each_crtc(crtc, dev) {
3005 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3006 if (IS_ERR(crtc_state)) {
3007 ret = PTR_ERR(crtc_state);
3011 crtc_state->active = false;
3013 ret = drm_atomic_set_mode_prop_for_crtc(crtc_state, NULL);
3017 ret = drm_atomic_add_affected_planes(state, crtc);
3021 ret = drm_atomic_add_affected_connectors(state, crtc);
3026 for_each_new_connector_in_state(state, conn, conn_state, i) {
3027 ret = drm_atomic_set_crtc_for_connector(conn_state, NULL);
3032 for_each_new_plane_in_state(state, plane, plane_state, i) {
3033 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
3037 drm_atomic_set_fb_for_plane(plane_state, NULL);
3040 ret = drm_atomic_commit(state);
3042 drm_atomic_state_put(state);
3047 * drm_atomic_helper_disable_all - disable all currently active outputs
3049 * @ctx: lock acquisition context
3051 * Loops through all connectors, finding those that aren't turned off and then
3052 * turns them off by setting their DPMS mode to OFF and deactivating the CRTC
3053 * that they are connected to.
3055 * This is used for example in suspend/resume to disable all currently active
3056 * functions when suspending. If you just want to shut down everything at e.g.
3057 * driver unload, look at drm_atomic_helper_shutdown().
3059 * Note that if callers haven't already acquired all modeset locks this might
3060 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
3063 * 0 on success or a negative error code on failure.
3066 * drm_atomic_helper_suspend(), drm_atomic_helper_resume() and
3067 * drm_atomic_helper_shutdown().
3069 int drm_atomic_helper_disable_all(struct drm_device *dev,
3070 struct drm_modeset_acquire_ctx *ctx)
3072 return __drm_atomic_helper_disable_all(dev, ctx, false);
3074 EXPORT_SYMBOL(drm_atomic_helper_disable_all);
3077 * drm_atomic_helper_shutdown - shutdown all CRTC
3080 * This shuts down all CRTC, which is useful for driver unloading. Shutdown on
3081 * suspend should instead be handled with drm_atomic_helper_suspend(), since
3082 * that also takes a snapshot of the modeset state to be restored on resume.
3084 * This is just a convenience wrapper around drm_atomic_helper_disable_all(),
3085 * and it is the atomic version of drm_crtc_force_disable_all().
3087 void drm_atomic_helper_shutdown(struct drm_device *dev)
3089 struct drm_modeset_acquire_ctx ctx;
3092 drm_modeset_acquire_init(&ctx, 0);
3094 ret = drm_modeset_lock_all_ctx(dev, &ctx);
3096 ret = __drm_atomic_helper_disable_all(dev, &ctx, true);
3098 if (ret != -EDEADLK)
3101 drm_modeset_backoff(&ctx);
3105 DRM_ERROR("Disabling all crtc's during unload failed with %i\n", ret);
3107 drm_modeset_drop_locks(&ctx);
3108 drm_modeset_acquire_fini(&ctx);
3110 EXPORT_SYMBOL(drm_atomic_helper_shutdown);
3113 * drm_atomic_helper_suspend - subsystem-level suspend helper
3116 * Duplicates the current atomic state, disables all active outputs and then
3117 * returns a pointer to the original atomic state to the caller. Drivers can
3118 * pass this pointer to the drm_atomic_helper_resume() helper upon resume to
3119 * restore the output configuration that was active at the time the system
3122 * Note that it is potentially unsafe to use this. The atomic state object
3123 * returned by this function is assumed to be persistent. Drivers must ensure
3124 * that this holds true. Before calling this function, drivers must make sure
3125 * to suspend fbdev emulation so that nothing can be using the device.
3128 * A pointer to a copy of the state before suspend on success or an ERR_PTR()-
3129 * encoded error code on failure. Drivers should store the returned atomic
3130 * state object and pass it to the drm_atomic_helper_resume() helper upon
3134 * drm_atomic_helper_duplicate_state(), drm_atomic_helper_disable_all(),
3135 * drm_atomic_helper_resume(), drm_atomic_helper_commit_duplicated_state()
3137 struct drm_atomic_state *drm_atomic_helper_suspend(struct drm_device *dev)
3139 struct drm_modeset_acquire_ctx ctx;
3140 struct drm_atomic_state *state;
3143 drm_modeset_acquire_init(&ctx, 0);
3146 err = drm_modeset_lock_all_ctx(dev, &ctx);
3148 state = ERR_PTR(err);
3152 state = drm_atomic_helper_duplicate_state(dev, &ctx);
3156 err = drm_atomic_helper_disable_all(dev, &ctx);
3158 drm_atomic_state_put(state);
3159 state = ERR_PTR(err);
3164 if (PTR_ERR(state) == -EDEADLK) {
3165 drm_modeset_backoff(&ctx);
3169 drm_modeset_drop_locks(&ctx);
3170 drm_modeset_acquire_fini(&ctx);
3173 EXPORT_SYMBOL(drm_atomic_helper_suspend);
3176 * drm_atomic_helper_commit_duplicated_state - commit duplicated state
3177 * @state: duplicated atomic state to commit
3178 * @ctx: pointer to acquire_ctx to use for commit.
3180 * The state returned by drm_atomic_helper_duplicate_state() and
3181 * drm_atomic_helper_suspend() is partially invalid, and needs to
3182 * be fixed up before commit.
3185 * 0 on success or a negative error code on failure.
3188 * drm_atomic_helper_suspend()
3190 int drm_atomic_helper_commit_duplicated_state(struct drm_atomic_state *state,
3191 struct drm_modeset_acquire_ctx *ctx)
3194 struct drm_plane *plane;
3195 struct drm_plane_state *new_plane_state;
3196 struct drm_connector *connector;
3197 struct drm_connector_state *new_conn_state;
3198 struct drm_crtc *crtc;
3199 struct drm_crtc_state *new_crtc_state;
3201 state->acquire_ctx = ctx;
3203 for_each_new_plane_in_state(state, plane, new_plane_state, i)
3204 state->planes[i].old_state = plane->state;
3206 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i)
3207 state->crtcs[i].old_state = crtc->state;
3209 for_each_new_connector_in_state(state, connector, new_conn_state, i)
3210 state->connectors[i].old_state = connector->state;
3212 return drm_atomic_commit(state);
3214 EXPORT_SYMBOL(drm_atomic_helper_commit_duplicated_state);
3217 * drm_atomic_helper_resume - subsystem-level resume helper
3219 * @state: atomic state to resume to
3221 * Calls drm_mode_config_reset() to synchronize hardware and software states,
3222 * grabs all modeset locks and commits the atomic state object. This can be
3223 * used in conjunction with the drm_atomic_helper_suspend() helper to
3224 * implement suspend/resume for drivers that support atomic mode-setting.
3227 * 0 on success or a negative error code on failure.
3230 * drm_atomic_helper_suspend()
3232 int drm_atomic_helper_resume(struct drm_device *dev,
3233 struct drm_atomic_state *state)
3235 struct drm_modeset_acquire_ctx ctx;
3238 drm_mode_config_reset(dev);
3240 drm_modeset_acquire_init(&ctx, 0);
3242 err = drm_modeset_lock_all_ctx(dev, &ctx);
3246 err = drm_atomic_helper_commit_duplicated_state(state, &ctx);
3248 if (err != -EDEADLK)
3251 drm_modeset_backoff(&ctx);
3254 drm_atomic_state_put(state);
3255 drm_modeset_drop_locks(&ctx);
3256 drm_modeset_acquire_fini(&ctx);
3260 EXPORT_SYMBOL(drm_atomic_helper_resume);
3262 static int page_flip_common(struct drm_atomic_state *state,
3263 struct drm_crtc *crtc,
3264 struct drm_framebuffer *fb,
3265 struct drm_pending_vblank_event *event,
3268 struct drm_plane *plane = crtc->primary;
3269 struct drm_plane_state *plane_state;
3270 struct drm_crtc_state *crtc_state;
3273 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3274 if (IS_ERR(crtc_state))
3275 return PTR_ERR(crtc_state);
3277 crtc_state->event = event;
3278 crtc_state->pageflip_flags = flags;
3280 plane_state = drm_atomic_get_plane_state(state, plane);
3281 if (IS_ERR(plane_state))
3282 return PTR_ERR(plane_state);
3284 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
3287 drm_atomic_set_fb_for_plane(plane_state, fb);
3289 /* Make sure we don't accidentally do a full modeset. */
3290 state->allow_modeset = false;
3291 if (!crtc_state->active) {
3292 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] disabled, rejecting legacy flip\n",
3293 crtc->base.id, crtc->name);
3301 * drm_atomic_helper_page_flip - execute a legacy page flip
3303 * @fb: DRM framebuffer
3304 * @event: optional DRM event to signal upon completion
3305 * @flags: flip flags for non-vblank sync'ed updates
3306 * @ctx: lock acquisition context
3308 * Provides a default &drm_crtc_funcs.page_flip implementation
3309 * using the atomic driver interface.
3312 * Returns 0 on success, negative errno numbers on failure.
3315 * drm_atomic_helper_page_flip_target()
3317 int drm_atomic_helper_page_flip(struct drm_crtc *crtc,
3318 struct drm_framebuffer *fb,
3319 struct drm_pending_vblank_event *event,
3321 struct drm_modeset_acquire_ctx *ctx)
3323 struct drm_plane *plane = crtc->primary;
3324 struct drm_atomic_state *state;
3327 state = drm_atomic_state_alloc(plane->dev);
3331 state->acquire_ctx = ctx;
3333 ret = page_flip_common(state, crtc, fb, event, flags);
3337 ret = drm_atomic_nonblocking_commit(state);
3339 drm_atomic_state_put(state);
3342 EXPORT_SYMBOL(drm_atomic_helper_page_flip);
3345 * drm_atomic_helper_page_flip_target - do page flip on target vblank period.
3347 * @fb: DRM framebuffer
3348 * @event: optional DRM event to signal upon completion
3349 * @flags: flip flags for non-vblank sync'ed updates
3350 * @target: specifying the target vblank period when the flip to take effect
3351 * @ctx: lock acquisition context
3353 * Provides a default &drm_crtc_funcs.page_flip_target implementation.
3354 * Similar to drm_atomic_helper_page_flip() with extra parameter to specify
3355 * target vblank period to flip.
3358 * Returns 0 on success, negative errno numbers on failure.
3360 int drm_atomic_helper_page_flip_target(struct drm_crtc *crtc,
3361 struct drm_framebuffer *fb,
3362 struct drm_pending_vblank_event *event,
3365 struct drm_modeset_acquire_ctx *ctx)
3367 struct drm_plane *plane = crtc->primary;
3368 struct drm_atomic_state *state;
3369 struct drm_crtc_state *crtc_state;
3372 state = drm_atomic_state_alloc(plane->dev);
3376 state->acquire_ctx = ctx;
3378 ret = page_flip_common(state, crtc, fb, event, flags);
3382 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
3383 if (WARN_ON(!crtc_state)) {
3387 crtc_state->target_vblank = target;
3389 ret = drm_atomic_nonblocking_commit(state);
3391 drm_atomic_state_put(state);
3394 EXPORT_SYMBOL(drm_atomic_helper_page_flip_target);
3397 * drm_atomic_helper_best_encoder - Helper for
3398 * &drm_connector_helper_funcs.best_encoder callback
3399 * @connector: Connector control structure
3401 * This is a &drm_connector_helper_funcs.best_encoder callback helper for
3402 * connectors that support exactly 1 encoder, statically determined at driver
3405 struct drm_encoder *
3406 drm_atomic_helper_best_encoder(struct drm_connector *connector)
3408 WARN_ON(connector->encoder_ids[1]);
3409 return drm_encoder_find(connector->dev, NULL, connector->encoder_ids[0]);
3411 EXPORT_SYMBOL(drm_atomic_helper_best_encoder);
3414 * DOC: atomic state reset and initialization
3416 * Both the drm core and the atomic helpers assume that there is always the full
3417 * and correct atomic software state for all connectors, CRTCs and planes
3418 * available. Which is a bit a problem on driver load and also after system
3419 * suspend. One way to solve this is to have a hardware state read-out
3420 * infrastructure which reconstructs the full software state (e.g. the i915
3423 * The simpler solution is to just reset the software state to everything off,
3424 * which is easiest to do by calling drm_mode_config_reset(). To facilitate this
3425 * the atomic helpers provide default reset implementations for all hooks.
3427 * On the upside the precise state tracking of atomic simplifies system suspend
3428 * and resume a lot. For drivers using drm_mode_config_reset() a complete recipe
3429 * is implemented in drm_atomic_helper_suspend() and drm_atomic_helper_resume().
3430 * For other drivers the building blocks are split out, see the documentation
3431 * for these functions.
3435 * drm_atomic_helper_crtc_reset - default &drm_crtc_funcs.reset hook for CRTCs
3438 * Resets the atomic state for @crtc by freeing the state pointer (which might
3439 * be NULL, e.g. at driver load time) and allocating a new empty state object.
3441 void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc)
3444 __drm_atomic_helper_crtc_destroy_state(crtc->state);
3447 crtc->state = kzalloc(sizeof(*crtc->state), GFP_KERNEL);
3450 crtc->state->crtc = crtc;
3452 EXPORT_SYMBOL(drm_atomic_helper_crtc_reset);
3455 * __drm_atomic_helper_crtc_duplicate_state - copy atomic CRTC state
3456 * @crtc: CRTC object
3457 * @state: atomic CRTC state
3459 * Copies atomic state from a CRTC's current state and resets inferred values.
3460 * This is useful for drivers that subclass the CRTC state.
3462 void __drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc,
3463 struct drm_crtc_state *state)
3465 memcpy(state, crtc->state, sizeof(*state));
3467 if (state->mode_blob)
3468 drm_property_blob_get(state->mode_blob);
3469 if (state->degamma_lut)
3470 drm_property_blob_get(state->degamma_lut);
3472 drm_property_blob_get(state->ctm);
3473 if (state->gamma_lut)
3474 drm_property_blob_get(state->gamma_lut);
3475 state->mode_changed = false;
3476 state->active_changed = false;
3477 state->planes_changed = false;
3478 state->connectors_changed = false;
3479 state->color_mgmt_changed = false;
3480 state->zpos_changed = false;
3481 state->commit = NULL;
3482 state->event = NULL;
3483 state->pageflip_flags = 0;
3485 EXPORT_SYMBOL(__drm_atomic_helper_crtc_duplicate_state);
3488 * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
3491 * Default CRTC state duplicate hook for drivers which don't have their own
3492 * subclassed CRTC state structure.
3494 struct drm_crtc_state *
3495 drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc)
3497 struct drm_crtc_state *state;
3499 if (WARN_ON(!crtc->state))
3502 state = kmalloc(sizeof(*state), GFP_KERNEL);
3504 __drm_atomic_helper_crtc_duplicate_state(crtc, state);
3508 EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state);
3511 * __drm_atomic_helper_crtc_destroy_state - release CRTC state
3512 * @state: CRTC state object to release
3514 * Releases all resources stored in the CRTC state without actually freeing
3515 * the memory of the CRTC state. This is useful for drivers that subclass the
3518 void __drm_atomic_helper_crtc_destroy_state(struct drm_crtc_state *state)
3520 if (state->commit) {
3522 * In the event that a non-blocking commit returns
3523 * -ERESTARTSYS before the commit_tail work is queued, we will
3524 * have an extra reference to the commit object. Release it, if
3525 * the event has not been consumed by the worker.
3527 * state->event may be freed, so we can't directly look at
3528 * state->event->base.completion.
3530 if (state->event && state->commit->abort_completion)
3531 drm_crtc_commit_put(state->commit);
3533 kfree(state->commit->event);
3534 state->commit->event = NULL;
3536 drm_crtc_commit_put(state->commit);
3539 drm_property_blob_put(state->mode_blob);
3540 drm_property_blob_put(state->degamma_lut);
3541 drm_property_blob_put(state->ctm);
3542 drm_property_blob_put(state->gamma_lut);
3544 EXPORT_SYMBOL(__drm_atomic_helper_crtc_destroy_state);
3547 * drm_atomic_helper_crtc_destroy_state - default state destroy hook
3549 * @state: CRTC state object to release
3551 * Default CRTC state destroy hook for drivers which don't have their own
3552 * subclassed CRTC state structure.
3554 void drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
3555 struct drm_crtc_state *state)
3557 __drm_atomic_helper_crtc_destroy_state(state);
3560 EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state);
3563 * __drm_atomic_helper_plane_reset - resets planes state to default values
3564 * @plane: plane object, must not be NULL
3565 * @state: atomic plane state, must not be NULL
3567 * Initializes plane state to default. This is useful for drivers that subclass
3570 void __drm_atomic_helper_plane_reset(struct drm_plane *plane,
3571 struct drm_plane_state *state)
3573 state->plane = plane;
3574 state->rotation = DRM_MODE_ROTATE_0;
3576 state->alpha = DRM_BLEND_ALPHA_OPAQUE;
3577 state->pixel_blend_mode = DRM_MODE_BLEND_PREMULTI;
3579 plane->state = state;
3581 EXPORT_SYMBOL(__drm_atomic_helper_plane_reset);
3584 * drm_atomic_helper_plane_reset - default &drm_plane_funcs.reset hook for planes
3587 * Resets the atomic state for @plane by freeing the state pointer (which might
3588 * be NULL, e.g. at driver load time) and allocating a new empty state object.
3590 void drm_atomic_helper_plane_reset(struct drm_plane *plane)
3593 __drm_atomic_helper_plane_destroy_state(plane->state);
3595 kfree(plane->state);
3596 plane->state = kzalloc(sizeof(*plane->state), GFP_KERNEL);
3598 __drm_atomic_helper_plane_reset(plane, plane->state);
3600 EXPORT_SYMBOL(drm_atomic_helper_plane_reset);
3603 * __drm_atomic_helper_plane_duplicate_state - copy atomic plane state
3604 * @plane: plane object
3605 * @state: atomic plane state
3607 * Copies atomic state from a plane's current state. This is useful for
3608 * drivers that subclass the plane state.
3610 void __drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane,
3611 struct drm_plane_state *state)
3613 memcpy(state, plane->state, sizeof(*state));
3616 drm_framebuffer_get(state->fb);
3618 state->fence = NULL;
3619 state->commit = NULL;
3621 EXPORT_SYMBOL(__drm_atomic_helper_plane_duplicate_state);
3624 * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
3627 * Default plane state duplicate hook for drivers which don't have their own
3628 * subclassed plane state structure.
3630 struct drm_plane_state *
3631 drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane)
3633 struct drm_plane_state *state;
3635 if (WARN_ON(!plane->state))
3638 state = kmalloc(sizeof(*state), GFP_KERNEL);
3640 __drm_atomic_helper_plane_duplicate_state(plane, state);
3644 EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state);
3647 * __drm_atomic_helper_plane_destroy_state - release plane state
3648 * @state: plane state object to release
3650 * Releases all resources stored in the plane state without actually freeing
3651 * the memory of the plane state. This is useful for drivers that subclass the
3654 void __drm_atomic_helper_plane_destroy_state(struct drm_plane_state *state)
3657 drm_framebuffer_put(state->fb);
3660 dma_fence_put(state->fence);
3663 drm_crtc_commit_put(state->commit);
3665 EXPORT_SYMBOL(__drm_atomic_helper_plane_destroy_state);
3668 * drm_atomic_helper_plane_destroy_state - default state destroy hook
3670 * @state: plane state object to release
3672 * Default plane state destroy hook for drivers which don't have their own
3673 * subclassed plane state structure.
3675 void drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
3676 struct drm_plane_state *state)
3678 __drm_atomic_helper_plane_destroy_state(state);
3681 EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state);
3684 * __drm_atomic_helper_connector_reset - reset state on connector
3685 * @connector: drm connector
3686 * @conn_state: connector state to assign
3688 * Initializes the newly allocated @conn_state and assigns it to
3689 * the &drm_conector->state pointer of @connector, usually required when
3690 * initializing the drivers or when called from the &drm_connector_funcs.reset
3693 * This is useful for drivers that subclass the connector state.
3696 __drm_atomic_helper_connector_reset(struct drm_connector *connector,
3697 struct drm_connector_state *conn_state)
3700 conn_state->connector = connector;
3702 connector->state = conn_state;
3704 EXPORT_SYMBOL(__drm_atomic_helper_connector_reset);
3707 * drm_atomic_helper_connector_reset - default &drm_connector_funcs.reset hook for connectors
3708 * @connector: drm connector
3710 * Resets the atomic state for @connector by freeing the state pointer (which
3711 * might be NULL, e.g. at driver load time) and allocating a new empty state
3714 void drm_atomic_helper_connector_reset(struct drm_connector *connector)
3716 struct drm_connector_state *conn_state =
3717 kzalloc(sizeof(*conn_state), GFP_KERNEL);
3719 if (connector->state)
3720 __drm_atomic_helper_connector_destroy_state(connector->state);
3722 kfree(connector->state);
3723 __drm_atomic_helper_connector_reset(connector, conn_state);
3725 EXPORT_SYMBOL(drm_atomic_helper_connector_reset);
3728 * __drm_atomic_helper_connector_duplicate_state - copy atomic connector state
3729 * @connector: connector object
3730 * @state: atomic connector state
3732 * Copies atomic state from a connector's current state. This is useful for
3733 * drivers that subclass the connector state.
3736 __drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector,
3737 struct drm_connector_state *state)
3739 memcpy(state, connector->state, sizeof(*state));
3741 drm_connector_get(connector);
3742 state->commit = NULL;
3744 /* Don't copy over a writeback job, they are used only once */
3745 state->writeback_job = NULL;
3747 EXPORT_SYMBOL(__drm_atomic_helper_connector_duplicate_state);
3750 * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
3751 * @connector: drm connector
3753 * Default connector state duplicate hook for drivers which don't have their own
3754 * subclassed connector state structure.
3756 struct drm_connector_state *
3757 drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
3759 struct drm_connector_state *state;
3761 if (WARN_ON(!connector->state))
3764 state = kmalloc(sizeof(*state), GFP_KERNEL);
3766 __drm_atomic_helper_connector_duplicate_state(connector, state);
3770 EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
3773 * drm_atomic_helper_duplicate_state - duplicate an atomic state object
3775 * @ctx: lock acquisition context
3777 * Makes a copy of the current atomic state by looping over all objects and
3778 * duplicating their respective states. This is used for example by suspend/
3779 * resume support code to save the state prior to suspend such that it can
3780 * be restored upon resume.
3782 * Note that this treats atomic state as persistent between save and restore.
3783 * Drivers must make sure that this is possible and won't result in confusion
3784 * or erroneous behaviour.
3786 * Note that if callers haven't already acquired all modeset locks this might
3787 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
3790 * A pointer to the copy of the atomic state object on success or an
3791 * ERR_PTR()-encoded error code on failure.
3794 * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
3796 struct drm_atomic_state *
3797 drm_atomic_helper_duplicate_state(struct drm_device *dev,
3798 struct drm_modeset_acquire_ctx *ctx)
3800 struct drm_atomic_state *state;
3801 struct drm_connector *conn;
3802 struct drm_connector_list_iter conn_iter;
3803 struct drm_plane *plane;
3804 struct drm_crtc *crtc;
3807 state = drm_atomic_state_alloc(dev);
3809 return ERR_PTR(-ENOMEM);
3811 state->acquire_ctx = ctx;
3813 drm_for_each_crtc(crtc, dev) {
3814 struct drm_crtc_state *crtc_state;
3816 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3817 if (IS_ERR(crtc_state)) {
3818 err = PTR_ERR(crtc_state);
3823 drm_for_each_plane(plane, dev) {
3824 struct drm_plane_state *plane_state;
3826 plane_state = drm_atomic_get_plane_state(state, plane);
3827 if (IS_ERR(plane_state)) {
3828 err = PTR_ERR(plane_state);
3833 drm_connector_list_iter_begin(dev, &conn_iter);
3834 drm_for_each_connector_iter(conn, &conn_iter) {
3835 struct drm_connector_state *conn_state;
3837 conn_state = drm_atomic_get_connector_state(state, conn);
3838 if (IS_ERR(conn_state)) {
3839 err = PTR_ERR(conn_state);
3840 drm_connector_list_iter_end(&conn_iter);
3844 drm_connector_list_iter_end(&conn_iter);
3846 /* clear the acquire context so that it isn't accidentally reused */
3847 state->acquire_ctx = NULL;
3851 drm_atomic_state_put(state);
3852 state = ERR_PTR(err);
3857 EXPORT_SYMBOL(drm_atomic_helper_duplicate_state);
3860 * __drm_atomic_helper_connector_destroy_state - release connector state
3861 * @state: connector state object to release
3863 * Releases all resources stored in the connector state without actually
3864 * freeing the memory of the connector state. This is useful for drivers that
3865 * subclass the connector state.
3868 __drm_atomic_helper_connector_destroy_state(struct drm_connector_state *state)
3871 drm_connector_put(state->connector);
3874 drm_crtc_commit_put(state->commit);
3876 EXPORT_SYMBOL(__drm_atomic_helper_connector_destroy_state);
3879 * drm_atomic_helper_connector_destroy_state - default state destroy hook
3880 * @connector: drm connector
3881 * @state: connector state object to release
3883 * Default connector state destroy hook for drivers which don't have their own
3884 * subclassed connector state structure.
3886 void drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
3887 struct drm_connector_state *state)
3889 __drm_atomic_helper_connector_destroy_state(state);
3892 EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state);
3895 * drm_atomic_helper_legacy_gamma_set - set the legacy gamma correction table
3896 * @crtc: CRTC object
3897 * @red: red correction table
3898 * @green: green correction table
3899 * @blue: green correction table
3900 * @size: size of the tables
3901 * @ctx: lock acquire context
3903 * Implements support for legacy gamma correction table for drivers
3904 * that support color management through the DEGAMMA_LUT/GAMMA_LUT
3905 * properties. See drm_crtc_enable_color_mgmt() and the containing chapter for
3906 * how the atomic color management and gamma tables work.
3908 int drm_atomic_helper_legacy_gamma_set(struct drm_crtc *crtc,
3909 u16 *red, u16 *green, u16 *blue,
3911 struct drm_modeset_acquire_ctx *ctx)
3913 struct drm_device *dev = crtc->dev;
3914 struct drm_atomic_state *state;
3915 struct drm_crtc_state *crtc_state;
3916 struct drm_property_blob *blob = NULL;
3917 struct drm_color_lut *blob_data;
3921 state = drm_atomic_state_alloc(crtc->dev);
3925 blob = drm_property_create_blob(dev,
3926 sizeof(struct drm_color_lut) * size,
3929 ret = PTR_ERR(blob);
3934 /* Prepare GAMMA_LUT with the legacy values. */
3935 blob_data = blob->data;
3936 for (i = 0; i < size; i++) {
3937 blob_data[i].red = red[i];
3938 blob_data[i].green = green[i];
3939 blob_data[i].blue = blue[i];
3942 state->acquire_ctx = ctx;
3943 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3944 if (IS_ERR(crtc_state)) {
3945 ret = PTR_ERR(crtc_state);
3949 /* Reset DEGAMMA_LUT and CTM properties. */
3950 replaced = drm_property_replace_blob(&crtc_state->degamma_lut, NULL);
3951 replaced |= drm_property_replace_blob(&crtc_state->ctm, NULL);
3952 replaced |= drm_property_replace_blob(&crtc_state->gamma_lut, blob);
3953 crtc_state->color_mgmt_changed |= replaced;
3955 ret = drm_atomic_commit(state);
3958 drm_atomic_state_put(state);
3959 drm_property_blob_put(blob);
3962 EXPORT_SYMBOL(drm_atomic_helper_legacy_gamma_set);
3965 * __drm_atomic_helper_private_duplicate_state - copy atomic private state
3967 * @state: new private object state
3969 * Copies atomic state from a private objects's current state and resets inferred values.
3970 * This is useful for drivers that subclass the private state.
3972 void __drm_atomic_helper_private_obj_duplicate_state(struct drm_private_obj *obj,
3973 struct drm_private_state *state)
3975 memcpy(state, obj->state, sizeof(*state));
3977 EXPORT_SYMBOL(__drm_atomic_helper_private_obj_duplicate_state);