2 * Copyright (C) 2014 Red Hat
3 * Copyright (C) 2014 Intel Corp.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
24 * Rob Clark <robdclark@gmail.com>
25 * Daniel Vetter <daniel.vetter@ffwll.ch>
29 #include <drm/drm_atomic.h>
30 #include <drm/drm_plane_helper.h>
31 #include <drm/drm_crtc_helper.h>
32 #include <drm/drm_atomic_helper.h>
33 #include <linux/fence.h>
36 drm_atomic_helper_plane_changed(struct drm_atomic_state *state,
37 struct drm_plane_state *plane_state,
38 struct drm_plane *plane)
40 struct drm_crtc_state *crtc_state;
42 if (plane->state->crtc) {
43 crtc_state = state->crtc_states[drm_crtc_index(plane->crtc)];
45 if (WARN_ON(!crtc_state))
48 crtc_state->planes_changed = true;
51 if (plane_state->crtc) {
53 state->crtc_states[drm_crtc_index(plane_state->crtc)];
55 if (WARN_ON(!crtc_state))
58 crtc_state->planes_changed = true;
62 static struct drm_crtc *
63 get_current_crtc_for_encoder(struct drm_device *dev,
64 struct drm_encoder *encoder)
66 struct drm_mode_config *config = &dev->mode_config;
67 struct drm_connector *connector;
69 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
71 list_for_each_entry(connector, &config->connector_list, head) {
72 if (connector->state->best_encoder != encoder)
75 return connector->state->crtc;
82 steal_encoder(struct drm_atomic_state *state,
83 struct drm_encoder *encoder,
84 struct drm_crtc *encoder_crtc)
86 struct drm_mode_config *config = &state->dev->mode_config;
87 struct drm_crtc_state *crtc_state;
88 struct drm_connector *connector;
89 struct drm_connector_state *connector_state;
93 * We can only steal an encoder coming from a connector, which means we
94 * must already hold the connection_mutex.
96 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
98 DRM_DEBUG_KMS("[ENCODER:%d:%s] in use on [CRTC:%d], stealing it\n",
99 encoder->base.id, encoder->name,
100 encoder_crtc->base.id);
102 crtc_state = drm_atomic_get_crtc_state(state, encoder_crtc);
103 if (IS_ERR(crtc_state))
104 return PTR_ERR(crtc_state);
106 crtc_state->mode_changed = true;
108 list_for_each_entry(connector, &config->connector_list, head) {
109 if (connector->state->best_encoder != encoder)
112 DRM_DEBUG_KMS("Stealing encoder from [CONNECTOR:%d:%s]\n",
116 connector_state = drm_atomic_get_connector_state(state,
118 if (IS_ERR(connector_state))
119 return PTR_ERR(connector_state);
121 ret = drm_atomic_set_crtc_for_connector(connector_state, NULL);
124 connector_state->best_encoder = NULL;
131 update_connector_routing(struct drm_atomic_state *state, int conn_idx)
133 struct drm_connector_helper_funcs *funcs;
134 struct drm_encoder *new_encoder;
135 struct drm_crtc *encoder_crtc;
136 struct drm_connector *connector;
137 struct drm_connector_state *connector_state;
138 struct drm_crtc_state *crtc_state;
141 connector = state->connectors[conn_idx];
142 connector_state = state->connector_states[conn_idx];
147 DRM_DEBUG_KMS("Updating routing for [CONNECTOR:%d:%s]\n",
151 if (connector->state->crtc != connector_state->crtc) {
152 if (connector->state->crtc) {
153 idx = drm_crtc_index(connector->state->crtc);
155 crtc_state = state->crtc_states[idx];
156 crtc_state->mode_changed = true;
159 if (connector_state->crtc) {
160 idx = drm_crtc_index(connector_state->crtc);
162 crtc_state = state->crtc_states[idx];
163 crtc_state->mode_changed = true;
167 if (!connector_state->crtc) {
168 DRM_DEBUG_KMS("Disabling [CONNECTOR:%d:%s]\n",
172 connector_state->best_encoder = NULL;
177 funcs = connector->helper_private;
178 new_encoder = funcs->best_encoder(connector);
181 DRM_DEBUG_KMS("No suitable encoder found for [CONNECTOR:%d:%s]\n",
187 if (new_encoder == connector_state->best_encoder) {
188 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] keeps [ENCODER:%d:%s], now on [CRTC:%d]\n",
191 new_encoder->base.id,
193 connector_state->crtc->base.id);
198 encoder_crtc = get_current_crtc_for_encoder(state->dev,
202 ret = steal_encoder(state, new_encoder, encoder_crtc);
204 DRM_DEBUG_KMS("Encoder stealing failed for [CONNECTOR:%d:%s]\n",
211 connector_state->best_encoder = new_encoder;
212 idx = drm_crtc_index(connector_state->crtc);
214 crtc_state = state->crtc_states[idx];
215 crtc_state->mode_changed = true;
217 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] using [ENCODER:%d:%s] on [CRTC:%d]\n",
220 new_encoder->base.id,
222 connector_state->crtc->base.id);
228 mode_fixup(struct drm_atomic_state *state)
230 int ncrtcs = state->dev->mode_config.num_crtc;
231 int nconnectors = state->dev->mode_config.num_connector;
232 struct drm_crtc_state *crtc_state;
233 struct drm_connector_state *conn_state;
237 for (i = 0; i < ncrtcs; i++) {
238 crtc_state = state->crtc_states[i];
240 if (!crtc_state || !crtc_state->mode_changed)
243 drm_mode_copy(&crtc_state->adjusted_mode, &crtc_state->mode);
246 for (i = 0; i < nconnectors; i++) {
247 struct drm_encoder_helper_funcs *funcs;
248 struct drm_encoder *encoder;
250 conn_state = state->connector_states[i];
255 WARN_ON(!!conn_state->best_encoder != !!conn_state->crtc);
257 if (!conn_state->crtc || !conn_state->best_encoder)
261 state->crtc_states[drm_crtc_index(conn_state->crtc)];
264 * Each encoder has at most one connector (since we always steal
265 * it away), so we won't call ->mode_fixup twice.
267 encoder = conn_state->best_encoder;
268 funcs = encoder->helper_private;
270 if (encoder->bridge && encoder->bridge->funcs->mode_fixup) {
271 ret = encoder->bridge->funcs->mode_fixup(
272 encoder->bridge, &crtc_state->mode,
273 &crtc_state->adjusted_mode);
275 DRM_DEBUG_KMS("Bridge fixup failed\n");
281 ret = funcs->mode_fixup(encoder, &crtc_state->mode,
282 &crtc_state->adjusted_mode);
284 DRM_DEBUG_KMS("[ENCODER:%d:%s] fixup failed\n",
285 encoder->base.id, encoder->name);
290 for (i = 0; i < ncrtcs; i++) {
291 struct drm_crtc_helper_funcs *funcs;
292 struct drm_crtc *crtc;
294 crtc_state = state->crtc_states[i];
295 crtc = state->crtcs[i];
297 if (!crtc_state || !crtc_state->mode_changed)
300 funcs = crtc->helper_private;
301 ret = funcs->mode_fixup(crtc, &crtc_state->mode,
302 &crtc_state->adjusted_mode);
304 DRM_DEBUG_KMS("[CRTC:%d] fixup failed\n",
314 drm_atomic_helper_check_prepare(struct drm_device *dev,
315 struct drm_atomic_state *state)
317 int ncrtcs = dev->mode_config.num_crtc;
318 int nconnectors = dev->mode_config.num_connector;
319 struct drm_crtc *crtc;
320 struct drm_crtc_state *crtc_state;
323 for (i = 0; i < ncrtcs; i++) {
324 crtc = state->crtcs[i];
325 crtc_state = state->crtc_states[i];
330 if (!drm_mode_equal(&crtc->state->mode, &crtc_state->mode)) {
331 DRM_DEBUG_KMS("[CRTC:%d] mode changed\n",
333 crtc_state->mode_changed = true;
336 if (crtc->state->enable != crtc_state->enable) {
337 DRM_DEBUG_KMS("[CRTC:%d] enable changed\n",
339 crtc_state->mode_changed = true;
343 for (i = 0; i < nconnectors; i++) {
345 * This only sets crtc->mode_changed for routing changes,
346 * drivers must set crtc->mode_changed themselves when connector
347 * properties need to be updated.
349 ret = update_connector_routing(state, i);
355 * After all the routing has been prepared we need to add in any
356 * connector which is itself unchanged, but who's crtc changes it's
357 * configuration. This must be done before calling mode_fixup in case a
358 * crtc only changed its mode but has the same set of connectors.
360 for (i = 0; i < ncrtcs; i++) {
363 crtc = state->crtcs[i];
364 crtc_state = state->crtc_states[i];
366 if (!crtc || !crtc_state->mode_changed)
369 DRM_DEBUG_KMS("[CRTC:%d] needs full modeset, enable: %c\n",
371 crtc_state->enable ? 'y' : 'n');
373 ret = drm_atomic_add_affected_connectors(state, crtc);
377 num_connectors = drm_atomic_connectors_for_crtc(state,
380 if (crtc_state->enable != !!num_connectors) {
381 DRM_DEBUG_KMS("[CRTC:%d] enabled/connectors mismatch\n",
388 return mode_fixup(state);
392 * drm_atomic_helper_check - validate state object
394 * @state: the driver state object
396 * Check the state object to see if the requested state is physically possible.
397 * Only crtcs and planes have check callbacks, so for any additional (global)
398 * checking that a driver needs it can simply wrap that around this function.
399 * Drivers without such needs can directly use this as their ->atomic_check()
403 * Zero for success or -errno
405 int drm_atomic_helper_check(struct drm_device *dev,
406 struct drm_atomic_state *state)
408 int nplanes = dev->mode_config.num_total_plane;
409 int ncrtcs = dev->mode_config.num_crtc;
412 ret = drm_atomic_helper_check_prepare(dev, state);
416 for (i = 0; i < nplanes; i++) {
417 struct drm_plane_helper_funcs *funcs;
418 struct drm_plane *plane = state->planes[i];
419 struct drm_plane_state *plane_state = state->plane_states[i];
424 funcs = plane->helper_private;
426 drm_atomic_helper_plane_changed(state, plane_state, plane);
428 if (!funcs || !funcs->atomic_check)
431 ret = funcs->atomic_check(plane, plane_state);
433 DRM_DEBUG_KMS("[PLANE:%d] atomic check failed\n",
439 for (i = 0; i < ncrtcs; i++) {
440 struct drm_crtc_helper_funcs *funcs;
441 struct drm_crtc *crtc = state->crtcs[i];
446 funcs = crtc->helper_private;
448 if (!funcs || !funcs->atomic_check)
451 ret = funcs->atomic_check(crtc, state->crtc_states[i]);
453 DRM_DEBUG_KMS("[CRTC:%d] atomic check failed\n",
461 EXPORT_SYMBOL(drm_atomic_helper_check);
464 disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state)
466 int ncrtcs = old_state->dev->mode_config.num_crtc;
467 int nconnectors = old_state->dev->mode_config.num_connector;
470 for (i = 0; i < nconnectors; i++) {
471 struct drm_connector_state *old_conn_state;
472 struct drm_connector *connector;
473 struct drm_encoder_helper_funcs *funcs;
474 struct drm_encoder *encoder;
476 old_conn_state = old_state->connector_states[i];
477 connector = old_state->connectors[i];
479 /* Shut down everything that's in the changeset and currently
480 * still on. So need to check the old, saved state. */
481 if (!old_conn_state || !old_conn_state->crtc)
484 encoder = connector->state->best_encoder;
489 funcs = encoder->helper_private;
492 * Each encoder has at most one connector (since we always steal
493 * it away), so we won't call call disable hooks twice.
496 encoder->bridge->funcs->disable(encoder->bridge);
498 /* Right function depends upon target state. */
499 if (connector->state->crtc)
500 funcs->prepare(encoder);
501 else if (funcs->disable)
502 funcs->disable(encoder);
504 funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
507 encoder->bridge->funcs->post_disable(encoder->bridge);
510 for (i = 0; i < ncrtcs; i++) {
511 struct drm_crtc_helper_funcs *funcs;
512 struct drm_crtc *crtc;
514 crtc = old_state->crtcs[i];
516 /* Shut down everything that needs a full modeset. */
517 if (!crtc || !crtc->state->mode_changed)
520 funcs = crtc->helper_private;
522 /* Right function depends upon target state. */
523 if (crtc->state->enable)
524 funcs->prepare(crtc);
525 else if (funcs->disable)
526 funcs->disable(crtc);
528 funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
533 set_routing_links(struct drm_device *dev, struct drm_atomic_state *old_state)
535 int nconnectors = dev->mode_config.num_connector;
536 int ncrtcs = old_state->dev->mode_config.num_crtc;
539 /* clear out existing links */
540 for (i = 0; i < nconnectors; i++) {
541 struct drm_connector *connector;
543 connector = old_state->connectors[i];
545 if (!connector || !connector->encoder)
548 WARN_ON(!connector->encoder->crtc);
550 connector->encoder->crtc = NULL;
551 connector->encoder = NULL;
555 for (i = 0; i < nconnectors; i++) {
556 struct drm_connector *connector;
558 connector = old_state->connectors[i];
560 if (!connector || !connector->state->crtc)
563 if (WARN_ON(!connector->state->best_encoder))
566 connector->encoder = connector->state->best_encoder;
567 connector->encoder->crtc = connector->state->crtc;
570 /* set legacy state in the crtc structure */
571 for (i = 0; i < ncrtcs; i++) {
572 struct drm_crtc *crtc;
574 crtc = old_state->crtcs[i];
579 crtc->mode = crtc->state->mode;
580 crtc->enabled = crtc->state->enable;
581 crtc->x = crtc->primary->state->src_x >> 16;
582 crtc->y = crtc->primary->state->src_y >> 16;
587 crtc_set_mode(struct drm_device *dev, struct drm_atomic_state *old_state)
589 int ncrtcs = old_state->dev->mode_config.num_crtc;
590 int nconnectors = old_state->dev->mode_config.num_connector;
593 for (i = 0; i < ncrtcs; i++) {
594 struct drm_crtc_helper_funcs *funcs;
595 struct drm_crtc *crtc;
597 crtc = old_state->crtcs[i];
599 if (!crtc || !crtc->state->mode_changed)
602 funcs = crtc->helper_private;
604 if (crtc->state->enable)
605 funcs->mode_set_nofb(crtc);
608 for (i = 0; i < nconnectors; i++) {
609 struct drm_connector *connector;
610 struct drm_crtc_state *new_crtc_state;
611 struct drm_encoder_helper_funcs *funcs;
612 struct drm_encoder *encoder;
613 struct drm_display_mode *mode, *adjusted_mode;
615 connector = old_state->connectors[i];
617 if (!connector || !connector->state->best_encoder)
620 encoder = connector->state->best_encoder;
621 funcs = encoder->helper_private;
622 new_crtc_state = connector->state->crtc->state;
623 mode = &new_crtc_state->mode;
624 adjusted_mode = &new_crtc_state->adjusted_mode;
627 * Each encoder has at most one connector (since we always steal
628 * it away), so we won't call call mode_set hooks twice.
630 funcs->mode_set(encoder, mode, adjusted_mode);
632 if (encoder->bridge && encoder->bridge->funcs->mode_set)
633 encoder->bridge->funcs->mode_set(encoder->bridge,
634 mode, adjusted_mode);
639 * drm_atomic_helper_commit_pre_planes - modeset commit before plane updates
641 * @state: atomic state
643 * This function commits the modeset changes that need to be committed before
644 * updating planes. It shuts down all the outputs that need to be shut down and
645 * prepares them (if required) with the new mode.
647 void drm_atomic_helper_commit_pre_planes(struct drm_device *dev,
648 struct drm_atomic_state *state)
650 disable_outputs(dev, state);
651 set_routing_links(dev, state);
652 crtc_set_mode(dev, state);
654 EXPORT_SYMBOL(drm_atomic_helper_commit_pre_planes);
657 * drm_atomic_helper_commit_post_planes - modeset commit after plane updates
659 * @old_state: atomic state object with old state structures
661 * This function commits the modeset changes that need to be committed after
662 * updating planes: It enables all the outputs with the new configuration which
663 * had to be turned off for the update.
665 void drm_atomic_helper_commit_post_planes(struct drm_device *dev,
666 struct drm_atomic_state *old_state)
668 int ncrtcs = old_state->dev->mode_config.num_crtc;
669 int nconnectors = old_state->dev->mode_config.num_connector;
672 for (i = 0; i < ncrtcs; i++) {
673 struct drm_crtc_helper_funcs *funcs;
674 struct drm_crtc *crtc;
676 crtc = old_state->crtcs[i];
678 /* Need to filter out CRTCs where only planes change. */
679 if (!crtc || !crtc->state->mode_changed)
682 funcs = crtc->helper_private;
684 if (crtc->state->enable)
688 for (i = 0; i < nconnectors; i++) {
689 struct drm_connector *connector;
690 struct drm_encoder_helper_funcs *funcs;
691 struct drm_encoder *encoder;
693 connector = old_state->connectors[i];
695 if (!connector || !connector->state->best_encoder)
698 encoder = connector->state->best_encoder;
699 funcs = encoder->helper_private;
702 * Each encoder has at most one connector (since we always steal
703 * it away), so we won't call call enable hooks twice.
706 encoder->bridge->funcs->pre_enable(encoder->bridge);
708 funcs->commit(encoder);
711 encoder->bridge->funcs->enable(encoder->bridge);
714 EXPORT_SYMBOL(drm_atomic_helper_commit_post_planes);
716 static void wait_for_fences(struct drm_device *dev,
717 struct drm_atomic_state *state)
719 int nplanes = dev->mode_config.num_total_plane;
722 for (i = 0; i < nplanes; i++) {
723 struct drm_plane *plane = state->planes[i];
725 if (!plane || !plane->state->fence)
728 WARN_ON(!plane->state->fb);
730 fence_wait(plane->state->fence, false);
731 fence_put(plane->state->fence);
732 plane->state->fence = NULL;
737 wait_for_vblanks(struct drm_device *dev, struct drm_atomic_state *old_state)
739 struct drm_crtc *crtc;
740 struct drm_crtc_state *old_crtc_state;
741 int ncrtcs = old_state->dev->mode_config.num_crtc;
744 for (i = 0; i < ncrtcs; i++) {
745 crtc = old_state->crtcs[i];
746 old_crtc_state = old_state->crtc_states[i];
751 /* No one cares about the old state, so abuse it for tracking
752 * and store whether we hold a vblank reference (and should do a
753 * vblank wait) in the ->enable boolean. */
754 old_crtc_state->enable = false;
756 if (!crtc->state->enable)
759 ret = drm_crtc_vblank_get(crtc);
763 old_crtc_state->enable = true;
764 old_crtc_state->last_vblank_count = drm_vblank_count(dev, i);
767 for (i = 0; i < ncrtcs; i++) {
768 crtc = old_state->crtcs[i];
769 old_crtc_state = old_state->crtc_states[i];
771 if (!crtc || !old_crtc_state->enable)
774 ret = wait_event_timeout(dev->vblank[i].queue,
775 old_crtc_state->last_vblank_count !=
776 drm_vblank_count(dev, i),
777 msecs_to_jiffies(50));
779 drm_crtc_vblank_put(crtc);
784 * drm_atomic_helper_commit - commit validated state object
786 * @state: the driver state object
787 * @async: asynchronous commit
789 * This function commits a with drm_atomic_helper_check() pre-validated state
790 * object. This can still fail when e.g. the framebuffer reservation fails. For
791 * now this doesn't implement asynchronous commits.
794 * Zero for success or -errno.
796 int drm_atomic_helper_commit(struct drm_device *dev,
797 struct drm_atomic_state *state,
805 ret = drm_atomic_helper_prepare_planes(dev, state);
810 * This is the point of no return - everything below never fails except
811 * when the hw goes bonghits. Which means we can commit the new state on
812 * the software side now.
815 drm_atomic_helper_swap_state(dev, state);
818 * Everything below can be run asynchronously without the need to grab
819 * any modeset locks at all under one conditions: It must be guaranteed
820 * that the asynchronous work has either been cancelled (if the driver
821 * supports it, which at least requires that the framebuffers get
822 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
823 * before the new state gets committed on the software side with
824 * drm_atomic_helper_swap_state().
826 * This scheme allows new atomic state updates to be prepared and
827 * checked in parallel to the asynchronous completion of the previous
828 * update. Which is important since compositors need to figure out the
829 * composition of the next frame right after having submitted the
833 wait_for_fences(dev, state);
835 drm_atomic_helper_commit_pre_planes(dev, state);
837 drm_atomic_helper_commit_planes(dev, state);
839 drm_atomic_helper_commit_post_planes(dev, state);
841 wait_for_vblanks(dev, state);
843 drm_atomic_helper_cleanup_planes(dev, state);
845 drm_atomic_state_free(state);
849 EXPORT_SYMBOL(drm_atomic_helper_commit);
852 * drm_atomic_helper_prepare_planes - prepare plane resources after commit
854 * @state: atomic state object with old state structures
856 * This function prepares plane state, specifically framebuffers, for the new
857 * configuration. If any failure is encountered this function will call
858 * ->cleanup_fb on any already successfully prepared framebuffer.
861 * 0 on success, negative error code on failure.
863 int drm_atomic_helper_prepare_planes(struct drm_device *dev,
864 struct drm_atomic_state *state)
866 int nplanes = dev->mode_config.num_total_plane;
869 for (i = 0; i < nplanes; i++) {
870 struct drm_plane_helper_funcs *funcs;
871 struct drm_plane *plane = state->planes[i];
872 struct drm_framebuffer *fb;
877 funcs = plane->helper_private;
879 fb = state->plane_states[i]->fb;
881 if (fb && funcs->prepare_fb) {
882 ret = funcs->prepare_fb(plane, fb);
891 for (i--; i >= 0; i--) {
892 struct drm_plane_helper_funcs *funcs;
893 struct drm_plane *plane = state->planes[i];
894 struct drm_framebuffer *fb;
899 funcs = plane->helper_private;
901 fb = state->plane_states[i]->fb;
903 if (fb && funcs->cleanup_fb)
904 funcs->cleanup_fb(plane, fb);
910 EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
913 * drm_atomic_helper_commit_planes - commit plane state
915 * @state: atomic state
917 * This function commits the new plane state using the plane and atomic helper
918 * functions for planes and crtcs. It assumes that the atomic state has already
919 * been pushed into the relevant object state pointers, since this step can no
922 * It still requires the global state object @state to know which planes and
923 * crtcs need to be updated though.
925 void drm_atomic_helper_commit_planes(struct drm_device *dev,
926 struct drm_atomic_state *state)
928 int nplanes = dev->mode_config.num_total_plane;
929 int ncrtcs = dev->mode_config.num_crtc;
932 for (i = 0; i < ncrtcs; i++) {
933 struct drm_crtc_helper_funcs *funcs;
934 struct drm_crtc *crtc = state->crtcs[i];
939 funcs = crtc->helper_private;
941 if (!funcs || !funcs->atomic_begin)
944 funcs->atomic_begin(crtc);
947 for (i = 0; i < nplanes; i++) {
948 struct drm_plane_helper_funcs *funcs;
949 struct drm_plane *plane = state->planes[i];
954 funcs = plane->helper_private;
956 if (!funcs || !funcs->atomic_update)
959 funcs->atomic_update(plane);
962 for (i = 0; i < ncrtcs; i++) {
963 struct drm_crtc_helper_funcs *funcs;
964 struct drm_crtc *crtc = state->crtcs[i];
969 funcs = crtc->helper_private;
971 if (!funcs || !funcs->atomic_flush)
974 funcs->atomic_flush(crtc);
977 EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
980 * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
982 * @old_state: atomic state object with old state structures
984 * This function cleans up plane state, specifically framebuffers, from the old
985 * configuration. Hence the old configuration must be perserved in @old_state to
986 * be able to call this function.
988 * This function must also be called on the new state when the atomic update
989 * fails at any point after calling drm_atomic_helper_prepare_planes().
991 void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
992 struct drm_atomic_state *old_state)
994 int nplanes = dev->mode_config.num_total_plane;
997 for (i = 0; i < nplanes; i++) {
998 struct drm_plane_helper_funcs *funcs;
999 struct drm_plane *plane = old_state->planes[i];
1000 struct drm_framebuffer *old_fb;
1005 funcs = plane->helper_private;
1007 old_fb = old_state->plane_states[i]->fb;
1009 if (old_fb && funcs->cleanup_fb)
1010 funcs->cleanup_fb(plane, old_fb);
1013 EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
1016 * drm_atomic_helper_swap_state - store atomic state into current sw state
1018 * @state: atomic state
1020 * This function stores the atomic state into the current state pointers in all
1021 * driver objects. It should be called after all failing steps have been done
1022 * and succeeded, but before the actual hardware state is committed.
1024 * For cleanup and error recovery the current state for all changed objects will
1025 * be swaped into @state.
1027 * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
1029 * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
1031 * 2. Do any other steps that might fail.
1033 * 3. Put the staged state into the current state pointers with this function.
1035 * 4. Actually commit the hardware state.
1037 * 5. Call drm_atomic_helper_cleanup_planes with @state, which since step 3
1038 * contains the old state. Also do any other cleanup required with that state.
1040 void drm_atomic_helper_swap_state(struct drm_device *dev,
1041 struct drm_atomic_state *state)
1045 for (i = 0; i < dev->mode_config.num_connector; i++) {
1046 struct drm_connector *connector = state->connectors[i];
1051 connector->state->state = state;
1052 swap(state->connector_states[i], connector->state);
1053 connector->state->state = NULL;
1056 for (i = 0; i < dev->mode_config.num_crtc; i++) {
1057 struct drm_crtc *crtc = state->crtcs[i];
1062 crtc->state->state = state;
1063 swap(state->crtc_states[i], crtc->state);
1064 crtc->state->state = NULL;
1067 for (i = 0; i < dev->mode_config.num_total_plane; i++) {
1068 struct drm_plane *plane = state->planes[i];
1073 plane->state->state = state;
1074 swap(state->plane_states[i], plane->state);
1075 plane->state->state = NULL;
1078 EXPORT_SYMBOL(drm_atomic_helper_swap_state);
1081 * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
1082 * @plane: plane object to update
1083 * @crtc: owning CRTC of owning plane
1084 * @fb: framebuffer to flip onto plane
1085 * @crtc_x: x offset of primary plane on crtc
1086 * @crtc_y: y offset of primary plane on crtc
1087 * @crtc_w: width of primary plane rectangle on crtc
1088 * @crtc_h: height of primary plane rectangle on crtc
1089 * @src_x: x offset of @fb for panning
1090 * @src_y: y offset of @fb for panning
1091 * @src_w: width of source rectangle in @fb
1092 * @src_h: height of source rectangle in @fb
1094 * Provides a default plane update handler using the atomic driver interface.
1097 * Zero on success, error code on failure
1099 int drm_atomic_helper_update_plane(struct drm_plane *plane,
1100 struct drm_crtc *crtc,
1101 struct drm_framebuffer *fb,
1102 int crtc_x, int crtc_y,
1103 unsigned int crtc_w, unsigned int crtc_h,
1104 uint32_t src_x, uint32_t src_y,
1105 uint32_t src_w, uint32_t src_h)
1107 struct drm_atomic_state *state;
1108 struct drm_plane_state *plane_state;
1111 state = drm_atomic_state_alloc(plane->dev);
1115 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1117 plane_state = drm_atomic_get_plane_state(state, plane);
1118 if (IS_ERR(plane_state)) {
1119 ret = PTR_ERR(plane_state);
1123 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
1126 plane_state->fb = fb;
1127 plane_state->crtc_x = crtc_x;
1128 plane_state->crtc_y = crtc_y;
1129 plane_state->crtc_h = crtc_h;
1130 plane_state->crtc_w = crtc_w;
1131 plane_state->src_x = src_x;
1132 plane_state->src_y = src_y;
1133 plane_state->src_h = src_h;
1134 plane_state->src_w = src_w;
1136 ret = drm_atomic_commit(state);
1140 /* Driver takes ownership of state on successful commit. */
1143 if (ret == -EDEADLK)
1146 drm_atomic_state_free(state);
1150 drm_atomic_legacy_backoff(state);
1151 drm_atomic_state_clear(state);
1154 * Someone might have exchanged the framebuffer while we dropped locks
1155 * in the backoff code. We need to fix up the fb refcount tracking the
1158 plane->old_fb = plane->fb;
1162 EXPORT_SYMBOL(drm_atomic_helper_update_plane);
1165 * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
1166 * @plane: plane to disable
1168 * Provides a default plane disable handler using the atomic driver interface.
1171 * Zero on success, error code on failure
1173 int drm_atomic_helper_disable_plane(struct drm_plane *plane)
1175 struct drm_atomic_state *state;
1176 struct drm_plane_state *plane_state;
1179 state = drm_atomic_state_alloc(plane->dev);
1183 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(plane->crtc);
1185 plane_state = drm_atomic_get_plane_state(state, plane);
1186 if (IS_ERR(plane_state)) {
1187 ret = PTR_ERR(plane_state);
1191 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
1194 plane_state->fb = NULL;
1195 plane_state->crtc_x = 0;
1196 plane_state->crtc_y = 0;
1197 plane_state->crtc_h = 0;
1198 plane_state->crtc_w = 0;
1199 plane_state->src_x = 0;
1200 plane_state->src_y = 0;
1201 plane_state->src_h = 0;
1202 plane_state->src_w = 0;
1204 ret = drm_atomic_commit(state);
1208 /* Driver takes ownership of state on successful commit. */
1211 if (ret == -EDEADLK)
1214 drm_atomic_state_free(state);
1218 drm_atomic_legacy_backoff(state);
1219 drm_atomic_state_clear(state);
1222 * Someone might have exchanged the framebuffer while we dropped locks
1223 * in the backoff code. We need to fix up the fb refcount tracking the
1226 plane->old_fb = plane->fb;
1230 EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
1232 static int update_output_state(struct drm_atomic_state *state,
1233 struct drm_mode_set *set)
1235 struct drm_device *dev = set->crtc->dev;
1236 struct drm_connector_state *conn_state;
1237 int nconnectors = state->dev->mode_config.num_connector;
1238 int ncrtcs = state->dev->mode_config.num_crtc;
1241 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
1242 state->acquire_ctx);
1246 /* First grab all affected connector/crtc states. */
1247 for (i = 0; i < set->num_connectors; i++) {
1248 conn_state = drm_atomic_get_connector_state(state,
1249 set->connectors[i]);
1250 if (IS_ERR(conn_state))
1251 return PTR_ERR(conn_state);
1254 for (i = 0; i < ncrtcs; i++) {
1255 struct drm_crtc *crtc = state->crtcs[i];
1260 ret = drm_atomic_add_affected_connectors(state, crtc);
1265 /* Then recompute connector->crtc links and crtc enabling state. */
1266 for (i = 0; i < nconnectors; i++) {
1267 struct drm_connector *connector;
1269 connector = state->connectors[i];
1270 conn_state = state->connector_states[i];
1275 if (conn_state->crtc == set->crtc) {
1276 ret = drm_atomic_set_crtc_for_connector(conn_state,
1282 for (j = 0; j < set->num_connectors; j++) {
1283 if (set->connectors[j] == connector) {
1284 ret = drm_atomic_set_crtc_for_connector(conn_state,
1293 for (i = 0; i < ncrtcs; i++) {
1294 struct drm_crtc *crtc = state->crtcs[i];
1295 struct drm_crtc_state *crtc_state = state->crtc_states[i];
1300 /* Don't update ->enable for the CRTC in the set_config request,
1301 * since a mismatch would indicate a bug in the upper layers.
1302 * The actual modeset code later on will catch any
1303 * inconsistencies here. */
1304 if (crtc == set->crtc)
1307 crtc_state->enable =
1308 drm_atomic_connectors_for_crtc(state, crtc);
1315 * drm_atomic_helper_set_config - set a new config from userspace
1316 * @set: mode set configuration
1318 * Provides a default crtc set_config handler using the atomic driver interface.
1321 * Returns 0 on success, negative errno numbers on failure.
1323 int drm_atomic_helper_set_config(struct drm_mode_set *set)
1325 struct drm_atomic_state *state;
1326 struct drm_crtc *crtc = set->crtc;
1327 struct drm_crtc_state *crtc_state;
1328 struct drm_plane_state *primary_state;
1331 state = drm_atomic_state_alloc(crtc->dev);
1335 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1337 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1338 if (IS_ERR(crtc_state)) {
1339 ret = PTR_ERR(crtc_state);
1345 WARN_ON(set->num_connectors);
1347 crtc_state->enable = false;
1352 WARN_ON(!set->num_connectors);
1354 crtc_state->enable = true;
1355 drm_mode_copy(&crtc_state->mode, set->mode);
1357 primary_state = drm_atomic_get_plane_state(state, crtc->primary);
1358 if (IS_ERR(primary_state)) {
1359 ret = PTR_ERR(primary_state);
1363 ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
1366 primary_state->fb = set->fb;
1367 primary_state->crtc_x = 0;
1368 primary_state->crtc_y = 0;
1369 primary_state->crtc_h = set->mode->vdisplay;
1370 primary_state->crtc_w = set->mode->hdisplay;
1371 primary_state->src_x = set->x << 16;
1372 primary_state->src_y = set->y << 16;
1373 primary_state->src_h = set->mode->vdisplay << 16;
1374 primary_state->src_w = set->mode->hdisplay << 16;
1377 ret = update_output_state(state, set);
1381 ret = drm_atomic_commit(state);
1385 /* Driver takes ownership of state on successful commit. */
1388 if (ret == -EDEADLK)
1391 drm_atomic_state_free(state);
1395 drm_atomic_legacy_backoff(state);
1396 drm_atomic_state_clear(state);
1399 * Someone might have exchanged the framebuffer while we dropped locks
1400 * in the backoff code. We need to fix up the fb refcount tracking the
1403 crtc->primary->old_fb = crtc->primary->fb;
1407 EXPORT_SYMBOL(drm_atomic_helper_set_config);
1410 * drm_atomic_helper_crtc_set_property - helper for crtc prorties
1412 * @property: DRM property
1413 * @val: value of property
1415 * Provides a default plane disablle handler using the atomic driver interface.
1418 * Zero on success, error code on failure
1421 drm_atomic_helper_crtc_set_property(struct drm_crtc *crtc,
1422 struct drm_property *property,
1425 struct drm_atomic_state *state;
1426 struct drm_crtc_state *crtc_state;
1429 state = drm_atomic_state_alloc(crtc->dev);
1433 /* ->set_property is always called with all locks held. */
1434 state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
1436 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1437 if (IS_ERR(crtc_state)) {
1438 ret = PTR_ERR(crtc_state);
1442 ret = crtc->funcs->atomic_set_property(crtc, crtc_state,
1447 ret = drm_atomic_commit(state);
1451 /* Driver takes ownership of state on successful commit. */
1454 if (ret == -EDEADLK)
1457 drm_atomic_state_free(state);
1461 drm_atomic_legacy_backoff(state);
1462 drm_atomic_state_clear(state);
1466 EXPORT_SYMBOL(drm_atomic_helper_crtc_set_property);
1469 * drm_atomic_helper_plane_set_property - helper for plane prorties
1471 * @property: DRM property
1472 * @val: value of property
1474 * Provides a default plane disable handler using the atomic driver interface.
1477 * Zero on success, error code on failure
1480 drm_atomic_helper_plane_set_property(struct drm_plane *plane,
1481 struct drm_property *property,
1484 struct drm_atomic_state *state;
1485 struct drm_plane_state *plane_state;
1488 state = drm_atomic_state_alloc(plane->dev);
1492 /* ->set_property is always called with all locks held. */
1493 state->acquire_ctx = plane->dev->mode_config.acquire_ctx;
1495 plane_state = drm_atomic_get_plane_state(state, plane);
1496 if (IS_ERR(plane_state)) {
1497 ret = PTR_ERR(plane_state);
1501 ret = plane->funcs->atomic_set_property(plane, plane_state,
1506 ret = drm_atomic_commit(state);
1510 /* Driver takes ownership of state on successful commit. */
1513 if (ret == -EDEADLK)
1516 drm_atomic_state_free(state);
1520 drm_atomic_legacy_backoff(state);
1521 drm_atomic_state_clear(state);
1525 EXPORT_SYMBOL(drm_atomic_helper_plane_set_property);
1528 * drm_atomic_helper_connector_set_property - helper for connector prorties
1529 * @connector: DRM connector
1530 * @property: DRM property
1531 * @val: value of property
1533 * Provides a default plane disablle handler using the atomic driver interface.
1536 * Zero on success, error code on failure
1539 drm_atomic_helper_connector_set_property(struct drm_connector *connector,
1540 struct drm_property *property,
1543 struct drm_atomic_state *state;
1544 struct drm_connector_state *connector_state;
1547 state = drm_atomic_state_alloc(connector->dev);
1551 /* ->set_property is always called with all locks held. */
1552 state->acquire_ctx = connector->dev->mode_config.acquire_ctx;
1554 connector_state = drm_atomic_get_connector_state(state, connector);
1555 if (IS_ERR(connector_state)) {
1556 ret = PTR_ERR(connector_state);
1560 ret = connector->funcs->atomic_set_property(connector, connector_state,
1565 ret = drm_atomic_commit(state);
1569 /* Driver takes ownership of state on successful commit. */
1572 if (ret == -EDEADLK)
1575 drm_atomic_state_free(state);
1579 drm_atomic_legacy_backoff(state);
1580 drm_atomic_state_clear(state);
1584 EXPORT_SYMBOL(drm_atomic_helper_connector_set_property);