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_state *new_crtc_state;
1413 struct drm_crtc *crtc;
1416 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
1417 struct drm_crtc_commit *commit = new_crtc_state->commit;
1423 ret = wait_for_completion_timeout(&commit->flip_done, 10 * HZ);
1425 DRM_ERROR("[CRTC:%d:%s] flip_done timed out\n",
1426 crtc->base.id, crtc->name);
1429 EXPORT_SYMBOL(drm_atomic_helper_wait_for_flip_done);
1432 * drm_atomic_helper_commit_tail - commit atomic update to hardware
1433 * @old_state: atomic state object with old state structures
1435 * This is the default implementation for the
1436 * &drm_mode_config_helper_funcs.atomic_commit_tail hook, for drivers
1437 * that do not support runtime_pm or do not need the CRTC to be
1438 * enabled to perform a commit. Otherwise, see
1439 * drm_atomic_helper_commit_tail_rpm().
1441 * Note that the default ordering of how the various stages are called is to
1442 * match the legacy modeset helper library closest.
1444 void drm_atomic_helper_commit_tail(struct drm_atomic_state *old_state)
1446 struct drm_device *dev = old_state->dev;
1448 drm_atomic_helper_commit_modeset_disables(dev, old_state);
1450 drm_atomic_helper_commit_planes(dev, old_state, 0);
1452 drm_atomic_helper_commit_modeset_enables(dev, old_state);
1454 drm_atomic_helper_fake_vblank(old_state);
1456 drm_atomic_helper_commit_hw_done(old_state);
1458 drm_atomic_helper_wait_for_vblanks(dev, old_state);
1460 drm_atomic_helper_cleanup_planes(dev, old_state);
1462 EXPORT_SYMBOL(drm_atomic_helper_commit_tail);
1465 * drm_atomic_helper_commit_tail_rpm - commit atomic update to hardware
1466 * @old_state: new modeset state to be committed
1468 * This is an alternative implementation for the
1469 * &drm_mode_config_helper_funcs.atomic_commit_tail hook, for drivers
1470 * that support runtime_pm or need the CRTC to be enabled to perform a
1471 * commit. Otherwise, one should use the default implementation
1472 * drm_atomic_helper_commit_tail().
1474 void drm_atomic_helper_commit_tail_rpm(struct drm_atomic_state *old_state)
1476 struct drm_device *dev = old_state->dev;
1478 drm_atomic_helper_commit_modeset_disables(dev, old_state);
1480 drm_atomic_helper_commit_modeset_enables(dev, old_state);
1482 drm_atomic_helper_commit_planes(dev, old_state,
1483 DRM_PLANE_COMMIT_ACTIVE_ONLY);
1485 drm_atomic_helper_fake_vblank(old_state);
1487 drm_atomic_helper_commit_hw_done(old_state);
1489 drm_atomic_helper_wait_for_vblanks(dev, old_state);
1491 drm_atomic_helper_cleanup_planes(dev, old_state);
1493 EXPORT_SYMBOL(drm_atomic_helper_commit_tail_rpm);
1495 static void commit_tail(struct drm_atomic_state *old_state)
1497 struct drm_device *dev = old_state->dev;
1498 const struct drm_mode_config_helper_funcs *funcs;
1500 funcs = dev->mode_config.helper_private;
1502 drm_atomic_helper_wait_for_fences(dev, old_state, false);
1504 drm_atomic_helper_wait_for_dependencies(old_state);
1506 if (funcs && funcs->atomic_commit_tail)
1507 funcs->atomic_commit_tail(old_state);
1509 drm_atomic_helper_commit_tail(old_state);
1511 drm_atomic_helper_commit_cleanup_done(old_state);
1513 drm_atomic_state_put(old_state);
1516 static void commit_work(struct work_struct *work)
1518 struct drm_atomic_state *state = container_of(work,
1519 struct drm_atomic_state,
1525 * drm_atomic_helper_async_check - check if state can be commited asynchronously
1527 * @state: the driver state object
1529 * This helper will check if it is possible to commit the state asynchronously.
1530 * Async commits are not supposed to swap the states like normal sync commits
1531 * but just do in-place changes on the current state.
1533 * It will return 0 if the commit can happen in an asynchronous fashion or error
1534 * if not. Note that error just mean it can't be commited asynchronously, if it
1535 * fails the commit should be treated like a normal synchronous commit.
1537 int drm_atomic_helper_async_check(struct drm_device *dev,
1538 struct drm_atomic_state *state)
1540 struct drm_crtc *crtc;
1541 struct drm_crtc_state *crtc_state;
1542 struct drm_plane *plane = NULL;
1543 struct drm_plane_state *old_plane_state = NULL;
1544 struct drm_plane_state *new_plane_state = NULL;
1545 const struct drm_plane_helper_funcs *funcs;
1546 int i, n_planes = 0;
1548 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
1549 if (drm_atomic_crtc_needs_modeset(crtc_state))
1553 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i)
1556 /* FIXME: we support only single plane updates for now */
1560 if (!new_plane_state->crtc ||
1561 old_plane_state->crtc != new_plane_state->crtc)
1564 funcs = plane->helper_private;
1565 if (!funcs->atomic_async_update)
1568 if (new_plane_state->fence)
1572 * Don't do an async update if there is an outstanding commit modifying
1573 * the plane. This prevents our async update's changes from getting
1574 * overridden by a previous synchronous update's state.
1576 if (old_plane_state->commit &&
1577 !try_wait_for_completion(&old_plane_state->commit->hw_done))
1580 return funcs->atomic_async_check(plane, new_plane_state);
1582 EXPORT_SYMBOL(drm_atomic_helper_async_check);
1585 * drm_atomic_helper_async_commit - commit state asynchronously
1587 * @state: the driver state object
1589 * This function commits a state asynchronously, i.e., not vblank
1590 * synchronized. It should be used on a state only when
1591 * drm_atomic_async_check() succeeds. Async commits are not supposed to swap
1592 * the states like normal sync commits, but just do in-place changes on the
1595 void drm_atomic_helper_async_commit(struct drm_device *dev,
1596 struct drm_atomic_state *state)
1598 struct drm_plane *plane;
1599 struct drm_plane_state *plane_state;
1600 const struct drm_plane_helper_funcs *funcs;
1603 for_each_new_plane_in_state(state, plane, plane_state, i) {
1604 funcs = plane->helper_private;
1605 funcs->atomic_async_update(plane, plane_state);
1608 * ->atomic_async_update() is supposed to update the
1609 * plane->state in-place, make sure at least common
1610 * properties have been properly updated.
1612 WARN_ON_ONCE(plane->state->fb != plane_state->fb);
1613 WARN_ON_ONCE(plane->state->crtc_x != plane_state->crtc_x);
1614 WARN_ON_ONCE(plane->state->crtc_y != plane_state->crtc_y);
1615 WARN_ON_ONCE(plane->state->src_x != plane_state->src_x);
1616 WARN_ON_ONCE(plane->state->src_y != plane_state->src_y);
1619 EXPORT_SYMBOL(drm_atomic_helper_async_commit);
1622 * drm_atomic_helper_commit - commit validated state object
1624 * @state: the driver state object
1625 * @nonblock: whether nonblocking behavior is requested.
1627 * This function commits a with drm_atomic_helper_check() pre-validated state
1628 * object. This can still fail when e.g. the framebuffer reservation fails. This
1629 * function implements nonblocking commits, using
1630 * drm_atomic_helper_setup_commit() and related functions.
1632 * Committing the actual hardware state is done through the
1633 * &drm_mode_config_helper_funcs.atomic_commit_tail callback, or it's default
1634 * implementation drm_atomic_helper_commit_tail().
1637 * Zero for success or -errno.
1639 int drm_atomic_helper_commit(struct drm_device *dev,
1640 struct drm_atomic_state *state,
1645 if (state->async_update) {
1646 ret = drm_atomic_helper_prepare_planes(dev, state);
1650 drm_atomic_helper_async_commit(dev, state);
1651 drm_atomic_helper_cleanup_planes(dev, state);
1656 ret = drm_atomic_helper_setup_commit(state, nonblock);
1660 INIT_WORK(&state->commit_work, commit_work);
1662 ret = drm_atomic_helper_prepare_planes(dev, state);
1667 ret = drm_atomic_helper_wait_for_fences(dev, state, true);
1673 * This is the point of no return - everything below never fails except
1674 * when the hw goes bonghits. Which means we can commit the new state on
1675 * the software side now.
1678 ret = drm_atomic_helper_swap_state(state, true);
1683 * Everything below can be run asynchronously without the need to grab
1684 * any modeset locks at all under one condition: It must be guaranteed
1685 * that the asynchronous work has either been cancelled (if the driver
1686 * supports it, which at least requires that the framebuffers get
1687 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
1688 * before the new state gets committed on the software side with
1689 * drm_atomic_helper_swap_state().
1691 * This scheme allows new atomic state updates to be prepared and
1692 * checked in parallel to the asynchronous completion of the previous
1693 * update. Which is important since compositors need to figure out the
1694 * composition of the next frame right after having submitted the
1697 * NOTE: Commit work has multiple phases, first hardware commit, then
1698 * cleanup. We want them to overlap, hence need system_unbound_wq to
1699 * make sure work items don't artifically stall on each another.
1702 drm_atomic_state_get(state);
1704 queue_work(system_unbound_wq, &state->commit_work);
1711 drm_atomic_helper_cleanup_planes(dev, state);
1714 EXPORT_SYMBOL(drm_atomic_helper_commit);
1717 * DOC: implementing nonblocking commit
1719 * Nonblocking atomic commits have to be implemented in the following sequence:
1721 * 1. Run drm_atomic_helper_prepare_planes() first. This is the only function
1722 * which commit needs to call which can fail, so we want to run it first and
1725 * 2. Synchronize with any outstanding nonblocking commit worker threads which
1726 * might be affected the new state update. This can be done by either cancelling
1727 * or flushing the work items, depending upon whether the driver can deal with
1728 * cancelled updates. Note that it is important to ensure that the framebuffer
1729 * cleanup is still done when cancelling.
1731 * Asynchronous workers need to have sufficient parallelism to be able to run
1732 * different atomic commits on different CRTCs in parallel. The simplest way to
1733 * achive this is by running them on the &system_unbound_wq work queue. Note
1734 * that drivers are not required to split up atomic commits and run an
1735 * individual commit in parallel - userspace is supposed to do that if it cares.
1736 * But it might be beneficial to do that for modesets, since those necessarily
1737 * must be done as one global operation, and enabling or disabling a CRTC can
1738 * take a long time. But even that is not required.
1740 * 3. The software state is updated synchronously with
1741 * drm_atomic_helper_swap_state(). Doing this under the protection of all modeset
1742 * locks means concurrent callers never see inconsistent state. And doing this
1743 * while it's guaranteed that no relevant nonblocking worker runs means that
1744 * nonblocking workers do not need grab any locks. Actually they must not grab
1745 * locks, for otherwise the work flushing will deadlock.
1747 * 4. Schedule a work item to do all subsequent steps, using the split-out
1748 * commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and
1749 * then cleaning up the framebuffers after the old framebuffer is no longer
1752 * The above scheme is implemented in the atomic helper libraries in
1753 * drm_atomic_helper_commit() using a bunch of helper functions. See
1754 * drm_atomic_helper_setup_commit() for a starting point.
1757 static int stall_checks(struct drm_crtc *crtc, bool nonblock)
1759 struct drm_crtc_commit *commit, *stall_commit = NULL;
1760 bool completed = true;
1764 spin_lock(&crtc->commit_lock);
1766 list_for_each_entry(commit, &crtc->commit_list, commit_entry) {
1768 completed = try_wait_for_completion(&commit->flip_done);
1769 /* Userspace is not allowed to get ahead of the previous
1770 * commit with nonblocking ones. */
1771 if (!completed && nonblock) {
1772 spin_unlock(&crtc->commit_lock);
1775 } else if (i == 1) {
1776 stall_commit = drm_crtc_commit_get(commit);
1782 spin_unlock(&crtc->commit_lock);
1787 /* We don't want to let commits get ahead of cleanup work too much,
1788 * stalling on 2nd previous commit means triple-buffer won't ever stall.
1790 ret = wait_for_completion_interruptible_timeout(&stall_commit->cleanup_done,
1793 DRM_ERROR("[CRTC:%d:%s] cleanup_done timed out\n",
1794 crtc->base.id, crtc->name);
1796 drm_crtc_commit_put(stall_commit);
1798 return ret < 0 ? ret : 0;
1801 static void release_crtc_commit(struct completion *completion)
1803 struct drm_crtc_commit *commit = container_of(completion,
1807 drm_crtc_commit_put(commit);
1810 static void init_commit(struct drm_crtc_commit *commit, struct drm_crtc *crtc)
1812 init_completion(&commit->flip_done);
1813 init_completion(&commit->hw_done);
1814 init_completion(&commit->cleanup_done);
1815 INIT_LIST_HEAD(&commit->commit_entry);
1816 kref_init(&commit->ref);
1817 commit->crtc = crtc;
1820 static struct drm_crtc_commit *
1821 crtc_or_fake_commit(struct drm_atomic_state *state, struct drm_crtc *crtc)
1824 struct drm_crtc_state *new_crtc_state;
1826 new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
1828 return new_crtc_state->commit;
1831 if (!state->fake_commit) {
1832 state->fake_commit = kzalloc(sizeof(*state->fake_commit), GFP_KERNEL);
1833 if (!state->fake_commit)
1836 init_commit(state->fake_commit, NULL);
1839 return state->fake_commit;
1843 * drm_atomic_helper_setup_commit - setup possibly nonblocking commit
1844 * @state: new modeset state to be committed
1845 * @nonblock: whether nonblocking behavior is requested.
1847 * This function prepares @state to be used by the atomic helper's support for
1848 * nonblocking commits. Drivers using the nonblocking commit infrastructure
1849 * should always call this function from their
1850 * &drm_mode_config_funcs.atomic_commit hook.
1852 * To be able to use this support drivers need to use a few more helper
1853 * functions. drm_atomic_helper_wait_for_dependencies() must be called before
1854 * actually committing the hardware state, and for nonblocking commits this call
1855 * must be placed in the async worker. See also drm_atomic_helper_swap_state()
1856 * and it's stall parameter, for when a driver's commit hooks look at the
1857 * &drm_crtc.state, &drm_plane.state or &drm_connector.state pointer directly.
1859 * Completion of the hardware commit step must be signalled using
1860 * drm_atomic_helper_commit_hw_done(). After this step the driver is not allowed
1861 * to read or change any permanent software or hardware modeset state. The only
1862 * exception is state protected by other means than &drm_modeset_lock locks.
1863 * Only the free standing @state with pointers to the old state structures can
1864 * be inspected, e.g. to clean up old buffers using
1865 * drm_atomic_helper_cleanup_planes().
1867 * At the very end, before cleaning up @state drivers must call
1868 * drm_atomic_helper_commit_cleanup_done().
1870 * This is all implemented by in drm_atomic_helper_commit(), giving drivers a
1871 * complete and easy-to-use default implementation of the atomic_commit() hook.
1873 * The tracking of asynchronously executed and still pending commits is done
1874 * using the core structure &drm_crtc_commit.
1876 * By default there's no need to clean up resources allocated by this function
1877 * explicitly: drm_atomic_state_default_clear() will take care of that
1882 * 0 on success. -EBUSY when userspace schedules nonblocking commits too fast,
1883 * -ENOMEM on allocation failures and -EINTR when a signal is pending.
1885 int drm_atomic_helper_setup_commit(struct drm_atomic_state *state,
1888 struct drm_crtc *crtc;
1889 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
1890 struct drm_connector *conn;
1891 struct drm_connector_state *old_conn_state, *new_conn_state;
1892 struct drm_plane *plane;
1893 struct drm_plane_state *old_plane_state, *new_plane_state;
1894 struct drm_crtc_commit *commit;
1897 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
1898 commit = kzalloc(sizeof(*commit), GFP_KERNEL);
1902 init_commit(commit, crtc);
1904 new_crtc_state->commit = commit;
1906 ret = stall_checks(crtc, nonblock);
1910 /* Drivers only send out events when at least either current or
1911 * new CRTC state is active. Complete right away if everything
1913 if (!old_crtc_state->active && !new_crtc_state->active) {
1914 complete_all(&commit->flip_done);
1918 /* Legacy cursor updates are fully unsynced. */
1919 if (state->legacy_cursor_update) {
1920 complete_all(&commit->flip_done);
1924 if (!new_crtc_state->event) {
1925 commit->event = kzalloc(sizeof(*commit->event),
1930 new_crtc_state->event = commit->event;
1933 new_crtc_state->event->base.completion = &commit->flip_done;
1934 new_crtc_state->event->base.completion_release = release_crtc_commit;
1935 drm_crtc_commit_get(commit);
1937 commit->abort_completion = true;
1940 for_each_oldnew_connector_in_state(state, conn, old_conn_state, new_conn_state, i) {
1941 /* Userspace is not allowed to get ahead of the previous
1942 * commit with nonblocking ones. */
1943 if (nonblock && old_conn_state->commit &&
1944 !try_wait_for_completion(&old_conn_state->commit->flip_done))
1947 /* Always track connectors explicitly for e.g. link retraining. */
1948 commit = crtc_or_fake_commit(state, new_conn_state->crtc ?: old_conn_state->crtc);
1952 new_conn_state->commit = drm_crtc_commit_get(commit);
1955 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
1956 /* Userspace is not allowed to get ahead of the previous
1957 * commit with nonblocking ones. */
1958 if (nonblock && old_plane_state->commit &&
1959 !try_wait_for_completion(&old_plane_state->commit->flip_done))
1962 /* Always track planes explicitly for async pageflip support. */
1963 commit = crtc_or_fake_commit(state, new_plane_state->crtc ?: old_plane_state->crtc);
1967 new_plane_state->commit = drm_crtc_commit_get(commit);
1972 EXPORT_SYMBOL(drm_atomic_helper_setup_commit);
1975 * drm_atomic_helper_wait_for_dependencies - wait for required preceeding commits
1976 * @old_state: atomic state object with old state structures
1978 * This function waits for all preceeding commits that touch the same CRTC as
1979 * @old_state to both be committed to the hardware (as signalled by
1980 * drm_atomic_helper_commit_hw_done) and executed by the hardware (as signalled
1981 * by calling drm_crtc_send_vblank_event() on the &drm_crtc_state.event).
1983 * This is part of the atomic helper support for nonblocking commits, see
1984 * drm_atomic_helper_setup_commit() for an overview.
1986 void drm_atomic_helper_wait_for_dependencies(struct drm_atomic_state *old_state)
1988 struct drm_crtc *crtc;
1989 struct drm_crtc_state *old_crtc_state;
1990 struct drm_plane *plane;
1991 struct drm_plane_state *old_plane_state;
1992 struct drm_connector *conn;
1993 struct drm_connector_state *old_conn_state;
1994 struct drm_crtc_commit *commit;
1998 for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
1999 commit = old_crtc_state->commit;
2004 ret = wait_for_completion_timeout(&commit->hw_done,
2007 DRM_ERROR("[CRTC:%d:%s] hw_done timed out\n",
2008 crtc->base.id, crtc->name);
2010 /* Currently no support for overwriting flips, hence
2011 * stall for previous one to execute completely. */
2012 ret = wait_for_completion_timeout(&commit->flip_done,
2015 DRM_ERROR("[CRTC:%d:%s] flip_done timed out\n",
2016 crtc->base.id, crtc->name);
2019 for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
2020 commit = old_conn_state->commit;
2025 ret = wait_for_completion_timeout(&commit->hw_done,
2028 DRM_ERROR("[CONNECTOR:%d:%s] hw_done timed out\n",
2029 conn->base.id, conn->name);
2031 /* Currently no support for overwriting flips, hence
2032 * stall for previous one to execute completely. */
2033 ret = wait_for_completion_timeout(&commit->flip_done,
2036 DRM_ERROR("[CONNECTOR:%d:%s] flip_done timed out\n",
2037 conn->base.id, conn->name);
2040 for_each_old_plane_in_state(old_state, plane, old_plane_state, i) {
2041 commit = old_plane_state->commit;
2046 ret = wait_for_completion_timeout(&commit->hw_done,
2049 DRM_ERROR("[PLANE:%d:%s] hw_done timed out\n",
2050 plane->base.id, plane->name);
2052 /* Currently no support for overwriting flips, hence
2053 * stall for previous one to execute completely. */
2054 ret = wait_for_completion_timeout(&commit->flip_done,
2057 DRM_ERROR("[PLANE:%d:%s] flip_done timed out\n",
2058 plane->base.id, plane->name);
2061 EXPORT_SYMBOL(drm_atomic_helper_wait_for_dependencies);
2064 * drm_atomic_helper_fake_vblank - fake VBLANK events if needed
2065 * @old_state: atomic state object with old state structures
2067 * This function walks all CRTCs and fake VBLANK events on those with
2068 * &drm_crtc_state.no_vblank set to true and &drm_crtc_state.event != NULL.
2069 * The primary use of this function is writeback connectors working in oneshot
2070 * mode and faking VBLANK events. In this case they only fake the VBLANK event
2071 * when a job is queued, and any change to the pipeline that does not touch the
2072 * connector is leading to timeouts when calling
2073 * drm_atomic_helper_wait_for_vblanks() or
2074 * drm_atomic_helper_wait_for_flip_done().
2076 * This is part of the atomic helper support for nonblocking commits, see
2077 * drm_atomic_helper_setup_commit() for an overview.
2079 void drm_atomic_helper_fake_vblank(struct drm_atomic_state *old_state)
2081 struct drm_crtc_state *new_crtc_state;
2082 struct drm_crtc *crtc;
2085 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
2086 unsigned long flags;
2088 if (!new_crtc_state->no_vblank)
2091 spin_lock_irqsave(&old_state->dev->event_lock, flags);
2092 if (new_crtc_state->event) {
2093 drm_crtc_send_vblank_event(crtc,
2094 new_crtc_state->event);
2095 new_crtc_state->event = NULL;
2097 spin_unlock_irqrestore(&old_state->dev->event_lock, flags);
2100 EXPORT_SYMBOL(drm_atomic_helper_fake_vblank);
2103 * drm_atomic_helper_commit_hw_done - setup possible nonblocking commit
2104 * @old_state: atomic state object with old state structures
2106 * This function is used to signal completion of the hardware commit step. After
2107 * this step the driver is not allowed to read or change any permanent software
2108 * or hardware modeset state. The only exception is state protected by other
2109 * means than &drm_modeset_lock locks.
2111 * Drivers should try to postpone any expensive or delayed cleanup work after
2112 * this function is called.
2114 * This is part of the atomic helper support for nonblocking commits, see
2115 * drm_atomic_helper_setup_commit() for an overview.
2117 void drm_atomic_helper_commit_hw_done(struct drm_atomic_state *old_state)
2119 struct drm_crtc *crtc;
2120 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
2121 struct drm_crtc_commit *commit;
2124 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
2125 commit = new_crtc_state->commit;
2130 * copy new_crtc_state->commit to old_crtc_state->commit,
2131 * it's unsafe to touch new_crtc_state after hw_done,
2132 * but we still need to do so in cleanup_done().
2134 if (old_crtc_state->commit)
2135 drm_crtc_commit_put(old_crtc_state->commit);
2137 old_crtc_state->commit = drm_crtc_commit_get(commit);
2139 /* backend must have consumed any event by now */
2140 WARN_ON(new_crtc_state->event);
2141 complete_all(&commit->hw_done);
2144 if (old_state->fake_commit) {
2145 complete_all(&old_state->fake_commit->hw_done);
2146 complete_all(&old_state->fake_commit->flip_done);
2149 EXPORT_SYMBOL(drm_atomic_helper_commit_hw_done);
2152 * drm_atomic_helper_commit_cleanup_done - signal completion of commit
2153 * @old_state: atomic state object with old state structures
2155 * This signals completion of the atomic update @old_state, including any
2156 * cleanup work. If used, it must be called right before calling
2157 * drm_atomic_state_put().
2159 * This is part of the atomic helper support for nonblocking commits, see
2160 * drm_atomic_helper_setup_commit() for an overview.
2162 void drm_atomic_helper_commit_cleanup_done(struct drm_atomic_state *old_state)
2164 struct drm_crtc *crtc;
2165 struct drm_crtc_state *old_crtc_state;
2166 struct drm_crtc_commit *commit;
2169 for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
2170 commit = old_crtc_state->commit;
2171 if (WARN_ON(!commit))
2174 complete_all(&commit->cleanup_done);
2175 WARN_ON(!try_wait_for_completion(&commit->hw_done));
2177 spin_lock(&crtc->commit_lock);
2178 list_del(&commit->commit_entry);
2179 spin_unlock(&crtc->commit_lock);
2182 if (old_state->fake_commit)
2183 complete_all(&old_state->fake_commit->cleanup_done);
2185 EXPORT_SYMBOL(drm_atomic_helper_commit_cleanup_done);
2188 * drm_atomic_helper_prepare_planes - prepare plane resources before commit
2190 * @state: atomic state object with new state structures
2192 * This function prepares plane state, specifically framebuffers, for the new
2193 * configuration, by calling &drm_plane_helper_funcs.prepare_fb. If any failure
2194 * is encountered this function will call &drm_plane_helper_funcs.cleanup_fb on
2195 * any already successfully prepared framebuffer.
2198 * 0 on success, negative error code on failure.
2200 int drm_atomic_helper_prepare_planes(struct drm_device *dev,
2201 struct drm_atomic_state *state)
2203 struct drm_plane *plane;
2204 struct drm_plane_state *new_plane_state;
2207 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
2208 const struct drm_plane_helper_funcs *funcs;
2210 funcs = plane->helper_private;
2212 if (funcs->prepare_fb) {
2213 ret = funcs->prepare_fb(plane, new_plane_state);
2222 for_each_new_plane_in_state(state, plane, new_plane_state, j) {
2223 const struct drm_plane_helper_funcs *funcs;
2228 funcs = plane->helper_private;
2230 if (funcs->cleanup_fb)
2231 funcs->cleanup_fb(plane, new_plane_state);
2236 EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
2238 static bool plane_crtc_active(const struct drm_plane_state *state)
2240 return state->crtc && state->crtc->state->active;
2244 * drm_atomic_helper_commit_planes - commit plane state
2246 * @old_state: atomic state object with old state structures
2247 * @flags: flags for committing plane state
2249 * This function commits the new plane state using the plane and atomic helper
2250 * functions for planes and crtcs. It assumes that the atomic state has already
2251 * been pushed into the relevant object state pointers, since this step can no
2254 * It still requires the global state object @old_state to know which planes and
2255 * crtcs need to be updated though.
2257 * Note that this function does all plane updates across all CRTCs in one step.
2258 * If the hardware can't support this approach look at
2259 * drm_atomic_helper_commit_planes_on_crtc() instead.
2261 * Plane parameters can be updated by applications while the associated CRTC is
2262 * disabled. The DRM/KMS core will store the parameters in the plane state,
2263 * which will be available to the driver when the CRTC is turned on. As a result
2264 * most drivers don't need to be immediately notified of plane updates for a
2267 * Unless otherwise needed, drivers are advised to set the ACTIVE_ONLY flag in
2268 * @flags in order not to receive plane update notifications related to a
2269 * disabled CRTC. This avoids the need to manually ignore plane updates in
2270 * driver code when the driver and/or hardware can't or just don't need to deal
2271 * with updates on disabled CRTCs, for example when supporting runtime PM.
2273 * Drivers may set the NO_DISABLE_AFTER_MODESET flag in @flags if the relevant
2274 * display controllers require to disable a CRTC's planes when the CRTC is
2275 * disabled. This function would skip the &drm_plane_helper_funcs.atomic_disable
2276 * call for a plane if the CRTC of the old plane state needs a modesetting
2277 * operation. Of course, the drivers need to disable the planes in their CRTC
2278 * disable callbacks since no one else would do that.
2280 * The drm_atomic_helper_commit() default implementation doesn't set the
2281 * ACTIVE_ONLY flag to most closely match the behaviour of the legacy helpers.
2282 * This should not be copied blindly by drivers.
2284 void drm_atomic_helper_commit_planes(struct drm_device *dev,
2285 struct drm_atomic_state *old_state,
2288 struct drm_crtc *crtc;
2289 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
2290 struct drm_plane *plane;
2291 struct drm_plane_state *old_plane_state, *new_plane_state;
2293 bool active_only = flags & DRM_PLANE_COMMIT_ACTIVE_ONLY;
2294 bool no_disable = flags & DRM_PLANE_COMMIT_NO_DISABLE_AFTER_MODESET;
2296 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
2297 const struct drm_crtc_helper_funcs *funcs;
2299 funcs = crtc->helper_private;
2301 if (!funcs || !funcs->atomic_begin)
2304 if (active_only && !new_crtc_state->active)
2307 funcs->atomic_begin(crtc, old_crtc_state);
2310 for_each_oldnew_plane_in_state(old_state, plane, old_plane_state, new_plane_state, i) {
2311 const struct drm_plane_helper_funcs *funcs;
2314 funcs = plane->helper_private;
2319 disabling = drm_atomic_plane_disabling(old_plane_state,
2324 * Skip planes related to inactive CRTCs. If the plane
2325 * is enabled use the state of the current CRTC. If the
2326 * plane is being disabled use the state of the old
2327 * CRTC to avoid skipping planes being disabled on an
2330 if (!disabling && !plane_crtc_active(new_plane_state))
2332 if (disabling && !plane_crtc_active(old_plane_state))
2337 * Special-case disabling the plane if drivers support it.
2339 if (disabling && funcs->atomic_disable) {
2340 struct drm_crtc_state *crtc_state;
2342 crtc_state = old_plane_state->crtc->state;
2344 if (drm_atomic_crtc_needs_modeset(crtc_state) &&
2348 funcs->atomic_disable(plane, old_plane_state);
2349 } else if (new_plane_state->crtc || disabling) {
2350 funcs->atomic_update(plane, old_plane_state);
2354 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
2355 const struct drm_crtc_helper_funcs *funcs;
2357 funcs = crtc->helper_private;
2359 if (!funcs || !funcs->atomic_flush)
2362 if (active_only && !new_crtc_state->active)
2365 funcs->atomic_flush(crtc, old_crtc_state);
2368 EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
2371 * drm_atomic_helper_commit_planes_on_crtc - commit plane state for a crtc
2372 * @old_crtc_state: atomic state object with the old crtc state
2374 * This function commits the new plane state using the plane and atomic helper
2375 * functions for planes on the specific crtc. It assumes that the atomic state
2376 * has already been pushed into the relevant object state pointers, since this
2377 * step can no longer fail.
2379 * This function is useful when plane updates should be done crtc-by-crtc
2380 * instead of one global step like drm_atomic_helper_commit_planes() does.
2382 * This function can only be savely used when planes are not allowed to move
2383 * between different CRTCs because this function doesn't handle inter-CRTC
2384 * depencies. Callers need to ensure that either no such depencies exist,
2385 * resolve them through ordering of commit calls or through some other means.
2388 drm_atomic_helper_commit_planes_on_crtc(struct drm_crtc_state *old_crtc_state)
2390 const struct drm_crtc_helper_funcs *crtc_funcs;
2391 struct drm_crtc *crtc = old_crtc_state->crtc;
2392 struct drm_atomic_state *old_state = old_crtc_state->state;
2393 struct drm_crtc_state *new_crtc_state =
2394 drm_atomic_get_new_crtc_state(old_state, crtc);
2395 struct drm_plane *plane;
2396 unsigned plane_mask;
2398 plane_mask = old_crtc_state->plane_mask;
2399 plane_mask |= new_crtc_state->plane_mask;
2401 crtc_funcs = crtc->helper_private;
2402 if (crtc_funcs && crtc_funcs->atomic_begin)
2403 crtc_funcs->atomic_begin(crtc, old_crtc_state);
2405 drm_for_each_plane_mask(plane, crtc->dev, plane_mask) {
2406 struct drm_plane_state *old_plane_state =
2407 drm_atomic_get_old_plane_state(old_state, plane);
2408 struct drm_plane_state *new_plane_state =
2409 drm_atomic_get_new_plane_state(old_state, plane);
2410 const struct drm_plane_helper_funcs *plane_funcs;
2412 plane_funcs = plane->helper_private;
2414 if (!old_plane_state || !plane_funcs)
2417 WARN_ON(new_plane_state->crtc &&
2418 new_plane_state->crtc != crtc);
2420 if (drm_atomic_plane_disabling(old_plane_state, new_plane_state) &&
2421 plane_funcs->atomic_disable)
2422 plane_funcs->atomic_disable(plane, old_plane_state);
2423 else if (new_plane_state->crtc ||
2424 drm_atomic_plane_disabling(old_plane_state, new_plane_state))
2425 plane_funcs->atomic_update(plane, old_plane_state);
2428 if (crtc_funcs && crtc_funcs->atomic_flush)
2429 crtc_funcs->atomic_flush(crtc, old_crtc_state);
2431 EXPORT_SYMBOL(drm_atomic_helper_commit_planes_on_crtc);
2434 * drm_atomic_helper_disable_planes_on_crtc - helper to disable CRTC's planes
2435 * @old_crtc_state: atomic state object with the old CRTC state
2436 * @atomic: if set, synchronize with CRTC's atomic_begin/flush hooks
2438 * Disables all planes associated with the given CRTC. This can be
2439 * used for instance in the CRTC helper atomic_disable callback to disable
2442 * If the atomic-parameter is set the function calls the CRTC's
2443 * atomic_begin hook before and atomic_flush hook after disabling the
2446 * It is a bug to call this function without having implemented the
2447 * &drm_plane_helper_funcs.atomic_disable plane hook.
2450 drm_atomic_helper_disable_planes_on_crtc(struct drm_crtc_state *old_crtc_state,
2453 struct drm_crtc *crtc = old_crtc_state->crtc;
2454 const struct drm_crtc_helper_funcs *crtc_funcs =
2455 crtc->helper_private;
2456 struct drm_plane *plane;
2458 if (atomic && crtc_funcs && crtc_funcs->atomic_begin)
2459 crtc_funcs->atomic_begin(crtc, NULL);
2461 drm_atomic_crtc_state_for_each_plane(plane, old_crtc_state) {
2462 const struct drm_plane_helper_funcs *plane_funcs =
2463 plane->helper_private;
2468 WARN_ON(!plane_funcs->atomic_disable);
2469 if (plane_funcs->atomic_disable)
2470 plane_funcs->atomic_disable(plane, NULL);
2473 if (atomic && crtc_funcs && crtc_funcs->atomic_flush)
2474 crtc_funcs->atomic_flush(crtc, NULL);
2476 EXPORT_SYMBOL(drm_atomic_helper_disable_planes_on_crtc);
2479 * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
2481 * @old_state: atomic state object with old state structures
2483 * This function cleans up plane state, specifically framebuffers, from the old
2484 * configuration. Hence the old configuration must be perserved in @old_state to
2485 * be able to call this function.
2487 * This function must also be called on the new state when the atomic update
2488 * fails at any point after calling drm_atomic_helper_prepare_planes().
2490 void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
2491 struct drm_atomic_state *old_state)
2493 struct drm_plane *plane;
2494 struct drm_plane_state *old_plane_state, *new_plane_state;
2497 for_each_oldnew_plane_in_state(old_state, plane, old_plane_state, new_plane_state, i) {
2498 const struct drm_plane_helper_funcs *funcs;
2499 struct drm_plane_state *plane_state;
2502 * This might be called before swapping when commit is aborted,
2503 * in which case we have to cleanup the new state.
2505 if (old_plane_state == plane->state)
2506 plane_state = new_plane_state;
2508 plane_state = old_plane_state;
2510 funcs = plane->helper_private;
2512 if (funcs->cleanup_fb)
2513 funcs->cleanup_fb(plane, plane_state);
2516 EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
2519 * drm_atomic_helper_swap_state - store atomic state into current sw state
2520 * @state: atomic state
2521 * @stall: stall for preceeding commits
2523 * This function stores the atomic state into the current state pointers in all
2524 * driver objects. It should be called after all failing steps have been done
2525 * and succeeded, but before the actual hardware state is committed.
2527 * For cleanup and error recovery the current state for all changed objects will
2528 * be swapped into @state.
2530 * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
2532 * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
2534 * 2. Do any other steps that might fail.
2536 * 3. Put the staged state into the current state pointers with this function.
2538 * 4. Actually commit the hardware state.
2540 * 5. Call drm_atomic_helper_cleanup_planes() with @state, which since step 3
2541 * contains the old state. Also do any other cleanup required with that state.
2543 * @stall must be set when nonblocking commits for this driver directly access
2544 * the &drm_plane.state, &drm_crtc.state or &drm_connector.state pointer. With
2545 * the current atomic helpers this is almost always the case, since the helpers
2546 * don't pass the right state structures to the callbacks.
2550 * Returns 0 on success. Can return -ERESTARTSYS when @stall is true and the
2551 * waiting for the previous commits has been interrupted.
2553 int drm_atomic_helper_swap_state(struct drm_atomic_state *state,
2557 struct drm_connector *connector;
2558 struct drm_connector_state *old_conn_state, *new_conn_state;
2559 struct drm_crtc *crtc;
2560 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
2561 struct drm_plane *plane;
2562 struct drm_plane_state *old_plane_state, *new_plane_state;
2563 struct drm_crtc_commit *commit;
2564 struct drm_private_obj *obj;
2565 struct drm_private_state *old_obj_state, *new_obj_state;
2569 * We have to stall for hw_done here before
2570 * drm_atomic_helper_wait_for_dependencies() because flip
2571 * depth > 1 is not yet supported by all drivers. As long as
2572 * obj->state is directly dereferenced anywhere in the drivers
2573 * atomic_commit_tail function, then it's unsafe to swap state
2574 * before drm_atomic_helper_commit_hw_done() is called.
2577 for_each_old_crtc_in_state(state, crtc, old_crtc_state, i) {
2578 commit = old_crtc_state->commit;
2583 ret = wait_for_completion_interruptible(&commit->hw_done);
2588 for_each_old_connector_in_state(state, connector, old_conn_state, i) {
2589 commit = old_conn_state->commit;
2594 ret = wait_for_completion_interruptible(&commit->hw_done);
2599 for_each_old_plane_in_state(state, plane, old_plane_state, i) {
2600 commit = old_plane_state->commit;
2605 ret = wait_for_completion_interruptible(&commit->hw_done);
2611 for_each_oldnew_connector_in_state(state, connector, old_conn_state, new_conn_state, i) {
2612 WARN_ON(connector->state != old_conn_state);
2614 old_conn_state->state = state;
2615 new_conn_state->state = NULL;
2617 state->connectors[i].state = old_conn_state;
2618 connector->state = new_conn_state;
2621 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
2622 WARN_ON(crtc->state != old_crtc_state);
2624 old_crtc_state->state = state;
2625 new_crtc_state->state = NULL;
2627 state->crtcs[i].state = old_crtc_state;
2628 crtc->state = new_crtc_state;
2630 if (new_crtc_state->commit) {
2631 spin_lock(&crtc->commit_lock);
2632 list_add(&new_crtc_state->commit->commit_entry,
2633 &crtc->commit_list);
2634 spin_unlock(&crtc->commit_lock);
2636 new_crtc_state->commit->event = NULL;
2640 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
2641 WARN_ON(plane->state != old_plane_state);
2643 old_plane_state->state = state;
2644 new_plane_state->state = NULL;
2646 state->planes[i].state = old_plane_state;
2647 plane->state = new_plane_state;
2650 for_each_oldnew_private_obj_in_state(state, obj, old_obj_state, new_obj_state, i) {
2651 WARN_ON(obj->state != old_obj_state);
2653 old_obj_state->state = state;
2654 new_obj_state->state = NULL;
2656 state->private_objs[i].state = old_obj_state;
2657 obj->state = new_obj_state;
2662 EXPORT_SYMBOL(drm_atomic_helper_swap_state);
2665 * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
2666 * @plane: plane object to update
2667 * @crtc: owning CRTC of owning plane
2668 * @fb: framebuffer to flip onto plane
2669 * @crtc_x: x offset of primary plane on crtc
2670 * @crtc_y: y offset of primary plane on crtc
2671 * @crtc_w: width of primary plane rectangle on crtc
2672 * @crtc_h: height of primary plane rectangle on crtc
2673 * @src_x: x offset of @fb for panning
2674 * @src_y: y offset of @fb for panning
2675 * @src_w: width of source rectangle in @fb
2676 * @src_h: height of source rectangle in @fb
2677 * @ctx: lock acquire context
2679 * Provides a default plane update handler using the atomic driver interface.
2682 * Zero on success, error code on failure
2684 int drm_atomic_helper_update_plane(struct drm_plane *plane,
2685 struct drm_crtc *crtc,
2686 struct drm_framebuffer *fb,
2687 int crtc_x, int crtc_y,
2688 unsigned int crtc_w, unsigned int crtc_h,
2689 uint32_t src_x, uint32_t src_y,
2690 uint32_t src_w, uint32_t src_h,
2691 struct drm_modeset_acquire_ctx *ctx)
2693 struct drm_atomic_state *state;
2694 struct drm_plane_state *plane_state;
2697 state = drm_atomic_state_alloc(plane->dev);
2701 state->acquire_ctx = ctx;
2702 plane_state = drm_atomic_get_plane_state(state, plane);
2703 if (IS_ERR(plane_state)) {
2704 ret = PTR_ERR(plane_state);
2708 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
2711 drm_atomic_set_fb_for_plane(plane_state, fb);
2712 plane_state->crtc_x = crtc_x;
2713 plane_state->crtc_y = crtc_y;
2714 plane_state->crtc_w = crtc_w;
2715 plane_state->crtc_h = crtc_h;
2716 plane_state->src_x = src_x;
2717 plane_state->src_y = src_y;
2718 plane_state->src_w = src_w;
2719 plane_state->src_h = src_h;
2721 if (plane == crtc->cursor)
2722 state->legacy_cursor_update = true;
2724 ret = drm_atomic_commit(state);
2726 drm_atomic_state_put(state);
2729 EXPORT_SYMBOL(drm_atomic_helper_update_plane);
2732 * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
2733 * @plane: plane to disable
2734 * @ctx: lock acquire context
2736 * Provides a default plane disable handler using the atomic driver interface.
2739 * Zero on success, error code on failure
2741 int drm_atomic_helper_disable_plane(struct drm_plane *plane,
2742 struct drm_modeset_acquire_ctx *ctx)
2744 struct drm_atomic_state *state;
2745 struct drm_plane_state *plane_state;
2748 state = drm_atomic_state_alloc(plane->dev);
2752 state->acquire_ctx = ctx;
2753 plane_state = drm_atomic_get_plane_state(state, plane);
2754 if (IS_ERR(plane_state)) {
2755 ret = PTR_ERR(plane_state);
2759 if (plane_state->crtc && plane_state->crtc->cursor == plane)
2760 plane_state->state->legacy_cursor_update = true;
2762 ret = __drm_atomic_helper_disable_plane(plane, plane_state);
2766 ret = drm_atomic_commit(state);
2768 drm_atomic_state_put(state);
2771 EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
2773 /* just used from fb-helper and atomic-helper: */
2774 int __drm_atomic_helper_disable_plane(struct drm_plane *plane,
2775 struct drm_plane_state *plane_state)
2779 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
2783 drm_atomic_set_fb_for_plane(plane_state, NULL);
2784 plane_state->crtc_x = 0;
2785 plane_state->crtc_y = 0;
2786 plane_state->crtc_w = 0;
2787 plane_state->crtc_h = 0;
2788 plane_state->src_x = 0;
2789 plane_state->src_y = 0;
2790 plane_state->src_w = 0;
2791 plane_state->src_h = 0;
2796 static int update_output_state(struct drm_atomic_state *state,
2797 struct drm_mode_set *set)
2799 struct drm_device *dev = set->crtc->dev;
2800 struct drm_crtc *crtc;
2801 struct drm_crtc_state *new_crtc_state;
2802 struct drm_connector *connector;
2803 struct drm_connector_state *new_conn_state;
2806 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
2807 state->acquire_ctx);
2811 /* First disable all connectors on the target crtc. */
2812 ret = drm_atomic_add_affected_connectors(state, set->crtc);
2816 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
2817 if (new_conn_state->crtc == set->crtc) {
2818 ret = drm_atomic_set_crtc_for_connector(new_conn_state,
2823 /* Make sure legacy setCrtc always re-trains */
2824 new_conn_state->link_status = DRM_LINK_STATUS_GOOD;
2828 /* Then set all connectors from set->connectors on the target crtc */
2829 for (i = 0; i < set->num_connectors; i++) {
2830 new_conn_state = drm_atomic_get_connector_state(state,
2831 set->connectors[i]);
2832 if (IS_ERR(new_conn_state))
2833 return PTR_ERR(new_conn_state);
2835 ret = drm_atomic_set_crtc_for_connector(new_conn_state,
2841 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
2842 /* Don't update ->enable for the CRTC in the set_config request,
2843 * since a mismatch would indicate a bug in the upper layers.
2844 * The actual modeset code later on will catch any
2845 * inconsistencies here. */
2846 if (crtc == set->crtc)
2849 if (!new_crtc_state->connector_mask) {
2850 ret = drm_atomic_set_mode_prop_for_crtc(new_crtc_state,
2855 new_crtc_state->active = false;
2863 * drm_atomic_helper_set_config - set a new config from userspace
2864 * @set: mode set configuration
2865 * @ctx: lock acquisition context
2867 * Provides a default crtc set_config handler using the atomic driver interface.
2869 * NOTE: For backwards compatibility with old userspace this automatically
2870 * resets the "link-status" property to GOOD, to force any link
2871 * re-training. The SETCRTC ioctl does not define whether an update does
2872 * need a full modeset or just a plane update, hence we're allowed to do
2873 * that. See also drm_connector_set_link_status_property().
2876 * Returns 0 on success, negative errno numbers on failure.
2878 int drm_atomic_helper_set_config(struct drm_mode_set *set,
2879 struct drm_modeset_acquire_ctx *ctx)
2881 struct drm_atomic_state *state;
2882 struct drm_crtc *crtc = set->crtc;
2885 state = drm_atomic_state_alloc(crtc->dev);
2889 state->acquire_ctx = ctx;
2890 ret = __drm_atomic_helper_set_config(set, state);
2894 ret = handle_conflicting_encoders(state, true);
2898 ret = drm_atomic_commit(state);
2901 drm_atomic_state_put(state);
2904 EXPORT_SYMBOL(drm_atomic_helper_set_config);
2906 /* just used from fb-helper and atomic-helper: */
2907 int __drm_atomic_helper_set_config(struct drm_mode_set *set,
2908 struct drm_atomic_state *state)
2910 struct drm_crtc_state *crtc_state;
2911 struct drm_plane_state *primary_state;
2912 struct drm_crtc *crtc = set->crtc;
2913 int hdisplay, vdisplay;
2916 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2917 if (IS_ERR(crtc_state))
2918 return PTR_ERR(crtc_state);
2920 primary_state = drm_atomic_get_plane_state(state, crtc->primary);
2921 if (IS_ERR(primary_state))
2922 return PTR_ERR(primary_state);
2926 WARN_ON(set->num_connectors);
2928 ret = drm_atomic_set_mode_for_crtc(crtc_state, NULL);
2932 crtc_state->active = false;
2934 ret = drm_atomic_set_crtc_for_plane(primary_state, NULL);
2938 drm_atomic_set_fb_for_plane(primary_state, NULL);
2944 WARN_ON(!set->num_connectors);
2946 ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
2950 crtc_state->active = true;
2952 ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
2956 drm_mode_get_hv_timing(set->mode, &hdisplay, &vdisplay);
2958 drm_atomic_set_fb_for_plane(primary_state, set->fb);
2959 primary_state->crtc_x = 0;
2960 primary_state->crtc_y = 0;
2961 primary_state->crtc_w = hdisplay;
2962 primary_state->crtc_h = vdisplay;
2963 primary_state->src_x = set->x << 16;
2964 primary_state->src_y = set->y << 16;
2965 if (drm_rotation_90_or_270(primary_state->rotation)) {
2966 primary_state->src_w = vdisplay << 16;
2967 primary_state->src_h = hdisplay << 16;
2969 primary_state->src_w = hdisplay << 16;
2970 primary_state->src_h = vdisplay << 16;
2974 ret = update_output_state(state, set);
2981 static int __drm_atomic_helper_disable_all(struct drm_device *dev,
2982 struct drm_modeset_acquire_ctx *ctx,
2985 struct drm_atomic_state *state;
2986 struct drm_connector_state *conn_state;
2987 struct drm_connector *conn;
2988 struct drm_plane_state *plane_state;
2989 struct drm_plane *plane;
2990 struct drm_crtc_state *crtc_state;
2991 struct drm_crtc *crtc;
2994 state = drm_atomic_state_alloc(dev);
2998 state->acquire_ctx = ctx;
3000 drm_for_each_crtc(crtc, dev) {
3001 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3002 if (IS_ERR(crtc_state)) {
3003 ret = PTR_ERR(crtc_state);
3007 crtc_state->active = false;
3009 ret = drm_atomic_set_mode_prop_for_crtc(crtc_state, NULL);
3013 ret = drm_atomic_add_affected_planes(state, crtc);
3017 ret = drm_atomic_add_affected_connectors(state, crtc);
3022 for_each_new_connector_in_state(state, conn, conn_state, i) {
3023 ret = drm_atomic_set_crtc_for_connector(conn_state, NULL);
3028 for_each_new_plane_in_state(state, plane, plane_state, i) {
3029 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
3033 drm_atomic_set_fb_for_plane(plane_state, NULL);
3036 ret = drm_atomic_commit(state);
3038 drm_atomic_state_put(state);
3043 * drm_atomic_helper_disable_all - disable all currently active outputs
3045 * @ctx: lock acquisition context
3047 * Loops through all connectors, finding those that aren't turned off and then
3048 * turns them off by setting their DPMS mode to OFF and deactivating the CRTC
3049 * that they are connected to.
3051 * This is used for example in suspend/resume to disable all currently active
3052 * functions when suspending. If you just want to shut down everything at e.g.
3053 * driver unload, look at drm_atomic_helper_shutdown().
3055 * Note that if callers haven't already acquired all modeset locks this might
3056 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
3059 * 0 on success or a negative error code on failure.
3062 * drm_atomic_helper_suspend(), drm_atomic_helper_resume() and
3063 * drm_atomic_helper_shutdown().
3065 int drm_atomic_helper_disable_all(struct drm_device *dev,
3066 struct drm_modeset_acquire_ctx *ctx)
3068 return __drm_atomic_helper_disable_all(dev, ctx, false);
3070 EXPORT_SYMBOL(drm_atomic_helper_disable_all);
3073 * drm_atomic_helper_shutdown - shutdown all CRTC
3076 * This shuts down all CRTC, which is useful for driver unloading. Shutdown on
3077 * suspend should instead be handled with drm_atomic_helper_suspend(), since
3078 * that also takes a snapshot of the modeset state to be restored on resume.
3080 * This is just a convenience wrapper around drm_atomic_helper_disable_all(),
3081 * and it is the atomic version of drm_crtc_force_disable_all().
3083 void drm_atomic_helper_shutdown(struct drm_device *dev)
3085 struct drm_modeset_acquire_ctx ctx;
3088 drm_modeset_acquire_init(&ctx, 0);
3090 ret = drm_modeset_lock_all_ctx(dev, &ctx);
3092 ret = __drm_atomic_helper_disable_all(dev, &ctx, true);
3094 if (ret != -EDEADLK)
3097 drm_modeset_backoff(&ctx);
3101 DRM_ERROR("Disabling all crtc's during unload failed with %i\n", ret);
3103 drm_modeset_drop_locks(&ctx);
3104 drm_modeset_acquire_fini(&ctx);
3106 EXPORT_SYMBOL(drm_atomic_helper_shutdown);
3109 * drm_atomic_helper_suspend - subsystem-level suspend helper
3112 * Duplicates the current atomic state, disables all active outputs and then
3113 * returns a pointer to the original atomic state to the caller. Drivers can
3114 * pass this pointer to the drm_atomic_helper_resume() helper upon resume to
3115 * restore the output configuration that was active at the time the system
3118 * Note that it is potentially unsafe to use this. The atomic state object
3119 * returned by this function is assumed to be persistent. Drivers must ensure
3120 * that this holds true. Before calling this function, drivers must make sure
3121 * to suspend fbdev emulation so that nothing can be using the device.
3124 * A pointer to a copy of the state before suspend on success or an ERR_PTR()-
3125 * encoded error code on failure. Drivers should store the returned atomic
3126 * state object and pass it to the drm_atomic_helper_resume() helper upon
3130 * drm_atomic_helper_duplicate_state(), drm_atomic_helper_disable_all(),
3131 * drm_atomic_helper_resume(), drm_atomic_helper_commit_duplicated_state()
3133 struct drm_atomic_state *drm_atomic_helper_suspend(struct drm_device *dev)
3135 struct drm_modeset_acquire_ctx ctx;
3136 struct drm_atomic_state *state;
3139 drm_modeset_acquire_init(&ctx, 0);
3142 err = drm_modeset_lock_all_ctx(dev, &ctx);
3144 state = ERR_PTR(err);
3148 state = drm_atomic_helper_duplicate_state(dev, &ctx);
3152 err = drm_atomic_helper_disable_all(dev, &ctx);
3154 drm_atomic_state_put(state);
3155 state = ERR_PTR(err);
3160 if (PTR_ERR(state) == -EDEADLK) {
3161 drm_modeset_backoff(&ctx);
3165 drm_modeset_drop_locks(&ctx);
3166 drm_modeset_acquire_fini(&ctx);
3169 EXPORT_SYMBOL(drm_atomic_helper_suspend);
3172 * drm_atomic_helper_commit_duplicated_state - commit duplicated state
3173 * @state: duplicated atomic state to commit
3174 * @ctx: pointer to acquire_ctx to use for commit.
3176 * The state returned by drm_atomic_helper_duplicate_state() and
3177 * drm_atomic_helper_suspend() is partially invalid, and needs to
3178 * be fixed up before commit.
3181 * 0 on success or a negative error code on failure.
3184 * drm_atomic_helper_suspend()
3186 int drm_atomic_helper_commit_duplicated_state(struct drm_atomic_state *state,
3187 struct drm_modeset_acquire_ctx *ctx)
3190 struct drm_plane *plane;
3191 struct drm_plane_state *new_plane_state;
3192 struct drm_connector *connector;
3193 struct drm_connector_state *new_conn_state;
3194 struct drm_crtc *crtc;
3195 struct drm_crtc_state *new_crtc_state;
3197 state->acquire_ctx = ctx;
3199 for_each_new_plane_in_state(state, plane, new_plane_state, i)
3200 state->planes[i].old_state = plane->state;
3202 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i)
3203 state->crtcs[i].old_state = crtc->state;
3205 for_each_new_connector_in_state(state, connector, new_conn_state, i)
3206 state->connectors[i].old_state = connector->state;
3208 return drm_atomic_commit(state);
3210 EXPORT_SYMBOL(drm_atomic_helper_commit_duplicated_state);
3213 * drm_atomic_helper_resume - subsystem-level resume helper
3215 * @state: atomic state to resume to
3217 * Calls drm_mode_config_reset() to synchronize hardware and software states,
3218 * grabs all modeset locks and commits the atomic state object. This can be
3219 * used in conjunction with the drm_atomic_helper_suspend() helper to
3220 * implement suspend/resume for drivers that support atomic mode-setting.
3223 * 0 on success or a negative error code on failure.
3226 * drm_atomic_helper_suspend()
3228 int drm_atomic_helper_resume(struct drm_device *dev,
3229 struct drm_atomic_state *state)
3231 struct drm_modeset_acquire_ctx ctx;
3234 drm_mode_config_reset(dev);
3236 drm_modeset_acquire_init(&ctx, 0);
3238 err = drm_modeset_lock_all_ctx(dev, &ctx);
3242 err = drm_atomic_helper_commit_duplicated_state(state, &ctx);
3244 if (err != -EDEADLK)
3247 drm_modeset_backoff(&ctx);
3250 drm_atomic_state_put(state);
3251 drm_modeset_drop_locks(&ctx);
3252 drm_modeset_acquire_fini(&ctx);
3256 EXPORT_SYMBOL(drm_atomic_helper_resume);
3258 static int page_flip_common(struct drm_atomic_state *state,
3259 struct drm_crtc *crtc,
3260 struct drm_framebuffer *fb,
3261 struct drm_pending_vblank_event *event,
3264 struct drm_plane *plane = crtc->primary;
3265 struct drm_plane_state *plane_state;
3266 struct drm_crtc_state *crtc_state;
3269 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3270 if (IS_ERR(crtc_state))
3271 return PTR_ERR(crtc_state);
3273 crtc_state->event = event;
3274 crtc_state->pageflip_flags = flags;
3276 plane_state = drm_atomic_get_plane_state(state, plane);
3277 if (IS_ERR(plane_state))
3278 return PTR_ERR(plane_state);
3280 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
3283 drm_atomic_set_fb_for_plane(plane_state, fb);
3285 /* Make sure we don't accidentally do a full modeset. */
3286 state->allow_modeset = false;
3287 if (!crtc_state->active) {
3288 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] disabled, rejecting legacy flip\n",
3289 crtc->base.id, crtc->name);
3297 * drm_atomic_helper_page_flip - execute a legacy page flip
3299 * @fb: DRM framebuffer
3300 * @event: optional DRM event to signal upon completion
3301 * @flags: flip flags for non-vblank sync'ed updates
3302 * @ctx: lock acquisition context
3304 * Provides a default &drm_crtc_funcs.page_flip implementation
3305 * using the atomic driver interface.
3308 * Returns 0 on success, negative errno numbers on failure.
3311 * drm_atomic_helper_page_flip_target()
3313 int drm_atomic_helper_page_flip(struct drm_crtc *crtc,
3314 struct drm_framebuffer *fb,
3315 struct drm_pending_vblank_event *event,
3317 struct drm_modeset_acquire_ctx *ctx)
3319 struct drm_plane *plane = crtc->primary;
3320 struct drm_atomic_state *state;
3323 state = drm_atomic_state_alloc(plane->dev);
3327 state->acquire_ctx = ctx;
3329 ret = page_flip_common(state, crtc, fb, event, flags);
3333 ret = drm_atomic_nonblocking_commit(state);
3335 drm_atomic_state_put(state);
3338 EXPORT_SYMBOL(drm_atomic_helper_page_flip);
3341 * drm_atomic_helper_page_flip_target - do page flip on target vblank period.
3343 * @fb: DRM framebuffer
3344 * @event: optional DRM event to signal upon completion
3345 * @flags: flip flags for non-vblank sync'ed updates
3346 * @target: specifying the target vblank period when the flip to take effect
3347 * @ctx: lock acquisition context
3349 * Provides a default &drm_crtc_funcs.page_flip_target implementation.
3350 * Similar to drm_atomic_helper_page_flip() with extra parameter to specify
3351 * target vblank period to flip.
3354 * Returns 0 on success, negative errno numbers on failure.
3356 int drm_atomic_helper_page_flip_target(struct drm_crtc *crtc,
3357 struct drm_framebuffer *fb,
3358 struct drm_pending_vblank_event *event,
3361 struct drm_modeset_acquire_ctx *ctx)
3363 struct drm_plane *plane = crtc->primary;
3364 struct drm_atomic_state *state;
3365 struct drm_crtc_state *crtc_state;
3368 state = drm_atomic_state_alloc(plane->dev);
3372 state->acquire_ctx = ctx;
3374 ret = page_flip_common(state, crtc, fb, event, flags);
3378 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
3379 if (WARN_ON(!crtc_state)) {
3383 crtc_state->target_vblank = target;
3385 ret = drm_atomic_nonblocking_commit(state);
3387 drm_atomic_state_put(state);
3390 EXPORT_SYMBOL(drm_atomic_helper_page_flip_target);
3393 * drm_atomic_helper_best_encoder - Helper for
3394 * &drm_connector_helper_funcs.best_encoder callback
3395 * @connector: Connector control structure
3397 * This is a &drm_connector_helper_funcs.best_encoder callback helper for
3398 * connectors that support exactly 1 encoder, statically determined at driver
3401 struct drm_encoder *
3402 drm_atomic_helper_best_encoder(struct drm_connector *connector)
3404 WARN_ON(connector->encoder_ids[1]);
3405 return drm_encoder_find(connector->dev, NULL, connector->encoder_ids[0]);
3407 EXPORT_SYMBOL(drm_atomic_helper_best_encoder);
3410 * DOC: atomic state reset and initialization
3412 * Both the drm core and the atomic helpers assume that there is always the full
3413 * and correct atomic software state for all connectors, CRTCs and planes
3414 * available. Which is a bit a problem on driver load and also after system
3415 * suspend. One way to solve this is to have a hardware state read-out
3416 * infrastructure which reconstructs the full software state (e.g. the i915
3419 * The simpler solution is to just reset the software state to everything off,
3420 * which is easiest to do by calling drm_mode_config_reset(). To facilitate this
3421 * the atomic helpers provide default reset implementations for all hooks.
3423 * On the upside the precise state tracking of atomic simplifies system suspend
3424 * and resume a lot. For drivers using drm_mode_config_reset() a complete recipe
3425 * is implemented in drm_atomic_helper_suspend() and drm_atomic_helper_resume().
3426 * For other drivers the building blocks are split out, see the documentation
3427 * for these functions.
3431 * drm_atomic_helper_crtc_reset - default &drm_crtc_funcs.reset hook for CRTCs
3434 * Resets the atomic state for @crtc by freeing the state pointer (which might
3435 * be NULL, e.g. at driver load time) and allocating a new empty state object.
3437 void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc)
3440 __drm_atomic_helper_crtc_destroy_state(crtc->state);
3443 crtc->state = kzalloc(sizeof(*crtc->state), GFP_KERNEL);
3446 crtc->state->crtc = crtc;
3448 EXPORT_SYMBOL(drm_atomic_helper_crtc_reset);
3451 * __drm_atomic_helper_crtc_duplicate_state - copy atomic CRTC state
3452 * @crtc: CRTC object
3453 * @state: atomic CRTC state
3455 * Copies atomic state from a CRTC's current state and resets inferred values.
3456 * This is useful for drivers that subclass the CRTC state.
3458 void __drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc,
3459 struct drm_crtc_state *state)
3461 memcpy(state, crtc->state, sizeof(*state));
3463 if (state->mode_blob)
3464 drm_property_blob_get(state->mode_blob);
3465 if (state->degamma_lut)
3466 drm_property_blob_get(state->degamma_lut);
3468 drm_property_blob_get(state->ctm);
3469 if (state->gamma_lut)
3470 drm_property_blob_get(state->gamma_lut);
3471 state->mode_changed = false;
3472 state->active_changed = false;
3473 state->planes_changed = false;
3474 state->connectors_changed = false;
3475 state->color_mgmt_changed = false;
3476 state->zpos_changed = false;
3477 state->commit = NULL;
3478 state->event = NULL;
3479 state->pageflip_flags = 0;
3481 EXPORT_SYMBOL(__drm_atomic_helper_crtc_duplicate_state);
3484 * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
3487 * Default CRTC state duplicate hook for drivers which don't have their own
3488 * subclassed CRTC state structure.
3490 struct drm_crtc_state *
3491 drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc)
3493 struct drm_crtc_state *state;
3495 if (WARN_ON(!crtc->state))
3498 state = kmalloc(sizeof(*state), GFP_KERNEL);
3500 __drm_atomic_helper_crtc_duplicate_state(crtc, state);
3504 EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state);
3507 * __drm_atomic_helper_crtc_destroy_state - release CRTC state
3508 * @state: CRTC state object to release
3510 * Releases all resources stored in the CRTC state without actually freeing
3511 * the memory of the CRTC state. This is useful for drivers that subclass the
3514 void __drm_atomic_helper_crtc_destroy_state(struct drm_crtc_state *state)
3516 if (state->commit) {
3518 * In the event that a non-blocking commit returns
3519 * -ERESTARTSYS before the commit_tail work is queued, we will
3520 * have an extra reference to the commit object. Release it, if
3521 * the event has not been consumed by the worker.
3523 * state->event may be freed, so we can't directly look at
3524 * state->event->base.completion.
3526 if (state->event && state->commit->abort_completion)
3527 drm_crtc_commit_put(state->commit);
3529 kfree(state->commit->event);
3530 state->commit->event = NULL;
3532 drm_crtc_commit_put(state->commit);
3535 drm_property_blob_put(state->mode_blob);
3536 drm_property_blob_put(state->degamma_lut);
3537 drm_property_blob_put(state->ctm);
3538 drm_property_blob_put(state->gamma_lut);
3540 EXPORT_SYMBOL(__drm_atomic_helper_crtc_destroy_state);
3543 * drm_atomic_helper_crtc_destroy_state - default state destroy hook
3545 * @state: CRTC state object to release
3547 * Default CRTC state destroy hook for drivers which don't have their own
3548 * subclassed CRTC state structure.
3550 void drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
3551 struct drm_crtc_state *state)
3553 __drm_atomic_helper_crtc_destroy_state(state);
3556 EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state);
3559 * __drm_atomic_helper_plane_reset - resets planes state to default values
3560 * @plane: plane object, must not be NULL
3561 * @state: atomic plane state, must not be NULL
3563 * Initializes plane state to default. This is useful for drivers that subclass
3566 void __drm_atomic_helper_plane_reset(struct drm_plane *plane,
3567 struct drm_plane_state *state)
3569 state->plane = plane;
3570 state->rotation = DRM_MODE_ROTATE_0;
3572 state->alpha = DRM_BLEND_ALPHA_OPAQUE;
3573 state->pixel_blend_mode = DRM_MODE_BLEND_PREMULTI;
3575 plane->state = state;
3577 EXPORT_SYMBOL(__drm_atomic_helper_plane_reset);
3580 * drm_atomic_helper_plane_reset - default &drm_plane_funcs.reset hook for planes
3583 * Resets the atomic state for @plane by freeing the state pointer (which might
3584 * be NULL, e.g. at driver load time) and allocating a new empty state object.
3586 void drm_atomic_helper_plane_reset(struct drm_plane *plane)
3589 __drm_atomic_helper_plane_destroy_state(plane->state);
3591 kfree(plane->state);
3592 plane->state = kzalloc(sizeof(*plane->state), GFP_KERNEL);
3594 __drm_atomic_helper_plane_reset(plane, plane->state);
3596 EXPORT_SYMBOL(drm_atomic_helper_plane_reset);
3599 * __drm_atomic_helper_plane_duplicate_state - copy atomic plane state
3600 * @plane: plane object
3601 * @state: atomic plane state
3603 * Copies atomic state from a plane's current state. This is useful for
3604 * drivers that subclass the plane state.
3606 void __drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane,
3607 struct drm_plane_state *state)
3609 memcpy(state, plane->state, sizeof(*state));
3612 drm_framebuffer_get(state->fb);
3614 state->fence = NULL;
3615 state->commit = NULL;
3617 EXPORT_SYMBOL(__drm_atomic_helper_plane_duplicate_state);
3620 * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
3623 * Default plane state duplicate hook for drivers which don't have their own
3624 * subclassed plane state structure.
3626 struct drm_plane_state *
3627 drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane)
3629 struct drm_plane_state *state;
3631 if (WARN_ON(!plane->state))
3634 state = kmalloc(sizeof(*state), GFP_KERNEL);
3636 __drm_atomic_helper_plane_duplicate_state(plane, state);
3640 EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state);
3643 * __drm_atomic_helper_plane_destroy_state - release plane state
3644 * @state: plane state object to release
3646 * Releases all resources stored in the plane state without actually freeing
3647 * the memory of the plane state. This is useful for drivers that subclass the
3650 void __drm_atomic_helper_plane_destroy_state(struct drm_plane_state *state)
3653 drm_framebuffer_put(state->fb);
3656 dma_fence_put(state->fence);
3659 drm_crtc_commit_put(state->commit);
3661 EXPORT_SYMBOL(__drm_atomic_helper_plane_destroy_state);
3664 * drm_atomic_helper_plane_destroy_state - default state destroy hook
3666 * @state: plane state object to release
3668 * Default plane state destroy hook for drivers which don't have their own
3669 * subclassed plane state structure.
3671 void drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
3672 struct drm_plane_state *state)
3674 __drm_atomic_helper_plane_destroy_state(state);
3677 EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state);
3680 * __drm_atomic_helper_connector_reset - reset state on connector
3681 * @connector: drm connector
3682 * @conn_state: connector state to assign
3684 * Initializes the newly allocated @conn_state and assigns it to
3685 * the &drm_conector->state pointer of @connector, usually required when
3686 * initializing the drivers or when called from the &drm_connector_funcs.reset
3689 * This is useful for drivers that subclass the connector state.
3692 __drm_atomic_helper_connector_reset(struct drm_connector *connector,
3693 struct drm_connector_state *conn_state)
3696 conn_state->connector = connector;
3698 connector->state = conn_state;
3700 EXPORT_SYMBOL(__drm_atomic_helper_connector_reset);
3703 * drm_atomic_helper_connector_reset - default &drm_connector_funcs.reset hook for connectors
3704 * @connector: drm connector
3706 * Resets the atomic state for @connector by freeing the state pointer (which
3707 * might be NULL, e.g. at driver load time) and allocating a new empty state
3710 void drm_atomic_helper_connector_reset(struct drm_connector *connector)
3712 struct drm_connector_state *conn_state =
3713 kzalloc(sizeof(*conn_state), GFP_KERNEL);
3715 if (connector->state)
3716 __drm_atomic_helper_connector_destroy_state(connector->state);
3718 kfree(connector->state);
3719 __drm_atomic_helper_connector_reset(connector, conn_state);
3721 EXPORT_SYMBOL(drm_atomic_helper_connector_reset);
3724 * __drm_atomic_helper_connector_duplicate_state - copy atomic connector state
3725 * @connector: connector object
3726 * @state: atomic connector state
3728 * Copies atomic state from a connector's current state. This is useful for
3729 * drivers that subclass the connector state.
3732 __drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector,
3733 struct drm_connector_state *state)
3735 memcpy(state, connector->state, sizeof(*state));
3737 drm_connector_get(connector);
3738 state->commit = NULL;
3740 /* Don't copy over a writeback job, they are used only once */
3741 state->writeback_job = NULL;
3743 EXPORT_SYMBOL(__drm_atomic_helper_connector_duplicate_state);
3746 * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
3747 * @connector: drm connector
3749 * Default connector state duplicate hook for drivers which don't have their own
3750 * subclassed connector state structure.
3752 struct drm_connector_state *
3753 drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
3755 struct drm_connector_state *state;
3757 if (WARN_ON(!connector->state))
3760 state = kmalloc(sizeof(*state), GFP_KERNEL);
3762 __drm_atomic_helper_connector_duplicate_state(connector, state);
3766 EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
3769 * drm_atomic_helper_duplicate_state - duplicate an atomic state object
3771 * @ctx: lock acquisition context
3773 * Makes a copy of the current atomic state by looping over all objects and
3774 * duplicating their respective states. This is used for example by suspend/
3775 * resume support code to save the state prior to suspend such that it can
3776 * be restored upon resume.
3778 * Note that this treats atomic state as persistent between save and restore.
3779 * Drivers must make sure that this is possible and won't result in confusion
3780 * or erroneous behaviour.
3782 * Note that if callers haven't already acquired all modeset locks this might
3783 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
3786 * A pointer to the copy of the atomic state object on success or an
3787 * ERR_PTR()-encoded error code on failure.
3790 * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
3792 struct drm_atomic_state *
3793 drm_atomic_helper_duplicate_state(struct drm_device *dev,
3794 struct drm_modeset_acquire_ctx *ctx)
3796 struct drm_atomic_state *state;
3797 struct drm_connector *conn;
3798 struct drm_connector_list_iter conn_iter;
3799 struct drm_plane *plane;
3800 struct drm_crtc *crtc;
3803 state = drm_atomic_state_alloc(dev);
3805 return ERR_PTR(-ENOMEM);
3807 state->acquire_ctx = ctx;
3809 drm_for_each_crtc(crtc, dev) {
3810 struct drm_crtc_state *crtc_state;
3812 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3813 if (IS_ERR(crtc_state)) {
3814 err = PTR_ERR(crtc_state);
3819 drm_for_each_plane(plane, dev) {
3820 struct drm_plane_state *plane_state;
3822 plane_state = drm_atomic_get_plane_state(state, plane);
3823 if (IS_ERR(plane_state)) {
3824 err = PTR_ERR(plane_state);
3829 drm_connector_list_iter_begin(dev, &conn_iter);
3830 drm_for_each_connector_iter(conn, &conn_iter) {
3831 struct drm_connector_state *conn_state;
3833 conn_state = drm_atomic_get_connector_state(state, conn);
3834 if (IS_ERR(conn_state)) {
3835 err = PTR_ERR(conn_state);
3836 drm_connector_list_iter_end(&conn_iter);
3840 drm_connector_list_iter_end(&conn_iter);
3842 /* clear the acquire context so that it isn't accidentally reused */
3843 state->acquire_ctx = NULL;
3847 drm_atomic_state_put(state);
3848 state = ERR_PTR(err);
3853 EXPORT_SYMBOL(drm_atomic_helper_duplicate_state);
3856 * __drm_atomic_helper_connector_destroy_state - release connector state
3857 * @state: connector state object to release
3859 * Releases all resources stored in the connector state without actually
3860 * freeing the memory of the connector state. This is useful for drivers that
3861 * subclass the connector state.
3864 __drm_atomic_helper_connector_destroy_state(struct drm_connector_state *state)
3867 drm_connector_put(state->connector);
3870 drm_crtc_commit_put(state->commit);
3872 EXPORT_SYMBOL(__drm_atomic_helper_connector_destroy_state);
3875 * drm_atomic_helper_connector_destroy_state - default state destroy hook
3876 * @connector: drm connector
3877 * @state: connector state object to release
3879 * Default connector state destroy hook for drivers which don't have their own
3880 * subclassed connector state structure.
3882 void drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
3883 struct drm_connector_state *state)
3885 __drm_atomic_helper_connector_destroy_state(state);
3888 EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state);
3891 * drm_atomic_helper_legacy_gamma_set - set the legacy gamma correction table
3892 * @crtc: CRTC object
3893 * @red: red correction table
3894 * @green: green correction table
3895 * @blue: green correction table
3896 * @size: size of the tables
3897 * @ctx: lock acquire context
3899 * Implements support for legacy gamma correction table for drivers
3900 * that support color management through the DEGAMMA_LUT/GAMMA_LUT
3901 * properties. See drm_crtc_enable_color_mgmt() and the containing chapter for
3902 * how the atomic color management and gamma tables work.
3904 int drm_atomic_helper_legacy_gamma_set(struct drm_crtc *crtc,
3905 u16 *red, u16 *green, u16 *blue,
3907 struct drm_modeset_acquire_ctx *ctx)
3909 struct drm_device *dev = crtc->dev;
3910 struct drm_atomic_state *state;
3911 struct drm_crtc_state *crtc_state;
3912 struct drm_property_blob *blob = NULL;
3913 struct drm_color_lut *blob_data;
3917 state = drm_atomic_state_alloc(crtc->dev);
3921 blob = drm_property_create_blob(dev,
3922 sizeof(struct drm_color_lut) * size,
3925 ret = PTR_ERR(blob);
3930 /* Prepare GAMMA_LUT with the legacy values. */
3931 blob_data = blob->data;
3932 for (i = 0; i < size; i++) {
3933 blob_data[i].red = red[i];
3934 blob_data[i].green = green[i];
3935 blob_data[i].blue = blue[i];
3938 state->acquire_ctx = ctx;
3939 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3940 if (IS_ERR(crtc_state)) {
3941 ret = PTR_ERR(crtc_state);
3945 /* Reset DEGAMMA_LUT and CTM properties. */
3946 replaced = drm_property_replace_blob(&crtc_state->degamma_lut, NULL);
3947 replaced |= drm_property_replace_blob(&crtc_state->ctm, NULL);
3948 replaced |= drm_property_replace_blob(&crtc_state->gamma_lut, blob);
3949 crtc_state->color_mgmt_changed |= replaced;
3951 ret = drm_atomic_commit(state);
3954 drm_atomic_state_put(state);
3955 drm_property_blob_put(blob);
3958 EXPORT_SYMBOL(drm_atomic_helper_legacy_gamma_set);
3961 * __drm_atomic_helper_private_duplicate_state - copy atomic private state
3963 * @state: new private object state
3965 * Copies atomic state from a private objects's current state and resets inferred values.
3966 * This is useful for drivers that subclass the private state.
3968 void __drm_atomic_helper_private_obj_duplicate_state(struct drm_private_obj *obj,
3969 struct drm_private_state *state)
3971 memcpy(state, obj->state, sizeof(*state));
3973 EXPORT_SYMBOL(__drm_atomic_helper_private_obj_duplicate_state);