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>
35 drm_atomic_helper_plane_changed(struct drm_atomic_state *state,
36 struct drm_plane_state *plane_state,
37 struct drm_plane *plane)
39 struct drm_crtc_state *crtc_state;
41 if (plane->state->crtc) {
42 crtc_state = state->crtc_states[drm_crtc_index(plane->crtc)];
44 if (WARN_ON(!crtc_state))
47 crtc_state->planes_changed = true;
50 if (plane_state->crtc) {
52 state->crtc_states[drm_crtc_index(plane_state->crtc)];
54 if (WARN_ON(!crtc_state))
57 crtc_state->planes_changed = true;
61 static struct drm_crtc *
62 get_current_crtc_for_encoder(struct drm_device *dev,
63 struct drm_encoder *encoder)
65 struct drm_mode_config *config = &dev->mode_config;
66 struct drm_connector *connector;
68 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
70 list_for_each_entry(connector, &config->connector_list, head) {
71 if (connector->state->best_encoder != encoder)
74 return connector->state->crtc;
81 steal_encoder(struct drm_atomic_state *state,
82 struct drm_encoder *encoder,
83 struct drm_crtc *encoder_crtc)
85 struct drm_mode_config *config = &state->dev->mode_config;
86 struct drm_crtc_state *crtc_state;
87 struct drm_connector *connector;
88 struct drm_connector_state *connector_state;
92 * We can only steal an encoder coming from a connector, which means we
93 * must already hold the connection_mutex.
95 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
97 DRM_DEBUG_KMS("[ENCODER:%d:%s] in use on [CRTC:%d], stealing it\n",
98 encoder->base.id, encoder->name,
99 encoder_crtc->base.id);
101 crtc_state = drm_atomic_get_crtc_state(state, encoder_crtc);
102 if (IS_ERR(crtc_state))
103 return PTR_ERR(crtc_state);
105 crtc_state->mode_changed = true;
107 list_for_each_entry(connector, &config->connector_list, head) {
108 if (connector->state->best_encoder != encoder)
111 DRM_DEBUG_KMS("Stealing encoder from [CONNECTOR:%d:%s]\n",
115 connector_state = drm_atomic_get_connector_state(state,
117 if (IS_ERR(connector_state))
118 return PTR_ERR(connector_state);
120 ret = drm_atomic_set_crtc_for_connector(connector_state, NULL);
123 connector_state->best_encoder = NULL;
130 update_connector_routing(struct drm_atomic_state *state, int conn_idx)
132 struct drm_connector_helper_funcs *funcs;
133 struct drm_encoder *new_encoder;
134 struct drm_crtc *encoder_crtc;
135 struct drm_connector *connector;
136 struct drm_connector_state *connector_state;
137 struct drm_crtc_state *crtc_state;
140 connector = state->connectors[conn_idx];
141 connector_state = state->connector_states[conn_idx];
146 DRM_DEBUG_KMS("Updating routing for [CONNECTOR:%d:%s]\n",
150 if (connector->state->crtc != connector_state->crtc) {
151 if (connector->state->crtc) {
152 idx = drm_crtc_index(connector->state->crtc);
154 crtc_state = state->crtc_states[idx];
155 crtc_state->mode_changed = true;
158 if (connector_state->crtc) {
159 idx = drm_crtc_index(connector_state->crtc);
161 crtc_state = state->crtc_states[idx];
162 crtc_state->mode_changed = true;
166 if (!connector_state->crtc) {
167 DRM_DEBUG_KMS("Disabling [CONNECTOR:%d:%s]\n",
171 connector_state->best_encoder = NULL;
176 funcs = connector->helper_private;
177 new_encoder = funcs->best_encoder(connector);
180 DRM_DEBUG_KMS("No suitable encoder found for [CONNECTOR:%d:%s]\n",
186 if (new_encoder == connector_state->best_encoder) {
187 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] keeps [ENCODER:%d:%s], now on [CRTC:%d]\n",
190 new_encoder->base.id,
192 connector_state->crtc->base.id);
197 encoder_crtc = get_current_crtc_for_encoder(state->dev,
201 ret = steal_encoder(state, new_encoder, encoder_crtc);
203 DRM_DEBUG_KMS("Encoder stealing failed for [CONNECTOR:%d:%s]\n",
210 connector_state->best_encoder = new_encoder;
211 idx = drm_crtc_index(connector_state->crtc);
213 crtc_state = state->crtc_states[idx];
214 crtc_state->mode_changed = true;
216 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] using [ENCODER:%d:%s] on [CRTC:%d]\n",
219 new_encoder->base.id,
221 connector_state->crtc->base.id);
227 mode_fixup(struct drm_atomic_state *state)
229 int ncrtcs = state->dev->mode_config.num_crtc;
230 int nconnectors = state->dev->mode_config.num_connector;
231 struct drm_crtc_state *crtc_state;
232 struct drm_connector_state *conn_state;
236 for (i = 0; i < ncrtcs; i++) {
237 crtc_state = state->crtc_states[i];
239 if (!crtc_state || !crtc_state->mode_changed)
242 drm_mode_copy(&crtc_state->adjusted_mode, &crtc_state->mode);
245 for (i = 0; i < nconnectors; i++) {
246 struct drm_encoder_helper_funcs *funcs;
247 struct drm_encoder *encoder;
249 conn_state = state->connector_states[i];
254 WARN_ON(!!conn_state->best_encoder != !!conn_state->crtc);
256 if (!conn_state->crtc || !conn_state->best_encoder)
260 state->crtc_states[drm_crtc_index(conn_state->crtc)];
263 * Each encoder has at most one connector (since we always steal
264 * it away), so we won't call ->mode_fixup twice.
266 encoder = conn_state->best_encoder;
267 funcs = encoder->helper_private;
269 if (encoder->bridge && encoder->bridge->funcs->mode_fixup) {
270 ret = encoder->bridge->funcs->mode_fixup(
271 encoder->bridge, &crtc_state->mode,
272 &crtc_state->adjusted_mode);
274 DRM_DEBUG_KMS("Bridge fixup failed\n");
280 ret = funcs->mode_fixup(encoder, &crtc_state->mode,
281 &crtc_state->adjusted_mode);
283 DRM_DEBUG_KMS("[ENCODER:%d:%s] fixup failed\n",
284 encoder->base.id, encoder->name);
289 for (i = 0; i < ncrtcs; i++) {
290 struct drm_crtc_helper_funcs *funcs;
291 struct drm_crtc *crtc;
293 crtc_state = state->crtc_states[i];
294 crtc = state->crtcs[i];
296 if (!crtc_state || !crtc_state->mode_changed)
299 funcs = crtc->helper_private;
300 ret = funcs->mode_fixup(crtc, &crtc_state->mode,
301 &crtc_state->adjusted_mode);
303 DRM_DEBUG_KMS("[CRTC:%d] fixup failed\n",
313 drm_atomic_helper_check_prepare(struct drm_device *dev,
314 struct drm_atomic_state *state)
316 int ncrtcs = dev->mode_config.num_crtc;
317 int nconnectors = dev->mode_config.num_connector;
318 struct drm_crtc *crtc;
319 struct drm_crtc_state *crtc_state;
322 for (i = 0; i < ncrtcs; i++) {
323 crtc = state->crtcs[i];
324 crtc_state = state->crtc_states[i];
329 if (!drm_mode_equal(&crtc->state->mode, &crtc_state->mode)) {
330 DRM_DEBUG_KMS("[CRTC:%d] mode changed\n",
332 crtc_state->mode_changed = true;
335 if (crtc->state->enable != crtc_state->enable) {
336 DRM_DEBUG_KMS("[CRTC:%d] enable changed\n",
338 crtc_state->mode_changed = true;
342 for (i = 0; i < nconnectors; i++) {
344 * This only sets crtc->mode_changed for routing changes,
345 * drivers must set crtc->mode_changed themselves when connector
346 * properties need to be updated.
348 ret = update_connector_routing(state, i);
354 * After all the routing has been prepared we need to add in any
355 * connector which is itself unchanged, but who's crtc changes it's
356 * configuration. This must be done before calling mode_fixup in case a
357 * crtc only changed its mode but has the same set of connectors.
359 for (i = 0; i < ncrtcs; i++) {
362 crtc = state->crtcs[i];
363 crtc_state = state->crtc_states[i];
365 if (!crtc || !crtc_state->mode_changed)
368 DRM_DEBUG_KMS("[CRTC:%d] needs full modeset, enable: %c\n",
370 crtc_state->enable ? 'y' : 'n');
372 ret = drm_atomic_add_affected_connectors(state, crtc);
376 num_connectors = drm_atomic_connectors_for_crtc(state,
379 if (crtc_state->enable != !!num_connectors) {
380 DRM_DEBUG_KMS("[CRTC:%d] enabled/connectors mismatch\n",
387 return mode_fixup(state);
391 * drm_atomic_helper_check - validate state object
393 * @state: the driver state object
395 * Check the state object to see if the requested state is physically possible.
396 * Only crtcs and planes have check callbacks, so for any additional (global)
397 * checking that a driver needs it can simply wrap that around this function.
398 * Drivers without such needs can directly use this as their ->atomic_check()
402 * Zero for success or -errno
404 int drm_atomic_helper_check(struct drm_device *dev,
405 struct drm_atomic_state *state)
407 int nplanes = dev->mode_config.num_total_plane;
408 int ncrtcs = dev->mode_config.num_crtc;
411 ret = drm_atomic_helper_check_prepare(dev, state);
415 for (i = 0; i < nplanes; i++) {
416 struct drm_plane_helper_funcs *funcs;
417 struct drm_plane *plane = state->planes[i];
418 struct drm_plane_state *plane_state = state->plane_states[i];
423 funcs = plane->helper_private;
425 drm_atomic_helper_plane_changed(state, plane_state, plane);
427 if (!funcs || !funcs->atomic_check)
430 ret = funcs->atomic_check(plane, plane_state);
432 DRM_DEBUG_KMS("[PLANE:%d] atomic check failed\n",
438 for (i = 0; i < ncrtcs; i++) {
439 struct drm_crtc_helper_funcs *funcs;
440 struct drm_crtc *crtc = state->crtcs[i];
445 funcs = crtc->helper_private;
447 if (!funcs || !funcs->atomic_check)
450 ret = funcs->atomic_check(crtc, state->crtc_states[i]);
452 DRM_DEBUG_KMS("[CRTC:%d] atomic check failed\n",
460 EXPORT_SYMBOL(drm_atomic_helper_check);
463 disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state)
465 int ncrtcs = old_state->dev->mode_config.num_crtc;
466 int nconnectors = old_state->dev->mode_config.num_connector;
469 for (i = 0; i < nconnectors; i++) {
470 struct drm_connector_state *old_conn_state;
471 struct drm_connector *connector;
472 struct drm_encoder_helper_funcs *funcs;
473 struct drm_encoder *encoder;
475 old_conn_state = old_state->connector_states[i];
476 connector = old_state->connectors[i];
478 /* Shut down everything that's in the changeset and currently
479 * still on. So need to check the old, saved state. */
480 if (!old_conn_state || !old_conn_state->crtc)
483 encoder = connector->state->best_encoder;
488 funcs = encoder->helper_private;
491 * Each encoder has at most one connector (since we always steal
492 * it away), so we won't call call disable hooks twice.
495 encoder->bridge->funcs->disable(encoder->bridge);
497 /* Right function depends upon target state. */
498 if (connector->state->crtc)
499 funcs->prepare(encoder);
500 else if (funcs->disable)
501 funcs->disable(encoder);
503 funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
506 encoder->bridge->funcs->post_disable(encoder->bridge);
509 for (i = 0; i < ncrtcs; i++) {
510 struct drm_crtc_helper_funcs *funcs;
511 struct drm_crtc *crtc;
513 crtc = old_state->crtcs[i];
515 /* Shut down everything that needs a full modeset. */
516 if (!crtc || !crtc->state->mode_changed)
519 funcs = crtc->helper_private;
521 /* Right function depends upon target state. */
522 if (crtc->state->enable)
523 funcs->prepare(crtc);
524 else if (funcs->disable)
525 funcs->disable(crtc);
527 funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
532 set_routing_links(struct drm_device *dev, struct drm_atomic_state *old_state)
534 int nconnectors = dev->mode_config.num_connector;
535 int ncrtcs = old_state->dev->mode_config.num_crtc;
538 /* clear out existing links */
539 for (i = 0; i < nconnectors; i++) {
540 struct drm_connector *connector;
542 connector = old_state->connectors[i];
544 if (!connector || !connector->encoder)
547 WARN_ON(!connector->encoder->crtc);
549 connector->encoder->crtc = NULL;
550 connector->encoder = NULL;
554 for (i = 0; i < nconnectors; i++) {
555 struct drm_connector *connector;
557 connector = old_state->connectors[i];
559 if (!connector || !connector->state->crtc)
562 if (WARN_ON(!connector->state->best_encoder))
565 connector->encoder = connector->state->best_encoder;
566 connector->encoder->crtc = connector->state->crtc;
569 /* set legacy state in the crtc structure */
570 for (i = 0; i < ncrtcs; i++) {
571 struct drm_crtc *crtc;
573 crtc = old_state->crtcs[i];
578 crtc->mode = crtc->state->mode;
579 crtc->enabled = crtc->state->enable;
580 crtc->x = crtc->primary->state->src_x >> 16;
581 crtc->y = crtc->primary->state->src_y >> 16;
586 crtc_set_mode(struct drm_device *dev, struct drm_atomic_state *old_state)
588 int ncrtcs = old_state->dev->mode_config.num_crtc;
589 int nconnectors = old_state->dev->mode_config.num_connector;
592 for (i = 0; i < ncrtcs; i++) {
593 struct drm_crtc_helper_funcs *funcs;
594 struct drm_crtc *crtc;
596 crtc = old_state->crtcs[i];
598 if (!crtc || !crtc->state->mode_changed)
601 funcs = crtc->helper_private;
603 if (crtc->state->enable)
604 funcs->mode_set_nofb(crtc);
607 for (i = 0; i < nconnectors; i++) {
608 struct drm_connector *connector;
609 struct drm_crtc_state *new_crtc_state;
610 struct drm_encoder_helper_funcs *funcs;
611 struct drm_encoder *encoder;
612 struct drm_display_mode *mode, *adjusted_mode;
614 connector = old_state->connectors[i];
616 if (!connector || !connector->state->best_encoder)
619 encoder = connector->state->best_encoder;
620 funcs = encoder->helper_private;
621 new_crtc_state = connector->state->crtc->state;
622 mode = &new_crtc_state->mode;
623 adjusted_mode = &new_crtc_state->adjusted_mode;
626 * Each encoder has at most one connector (since we always steal
627 * it away), so we won't call call mode_set hooks twice.
629 funcs->mode_set(encoder, mode, adjusted_mode);
631 if (encoder->bridge && encoder->bridge->funcs->mode_set)
632 encoder->bridge->funcs->mode_set(encoder->bridge,
633 mode, adjusted_mode);
638 * drm_atomic_helper_commit_pre_planes - modeset commit before plane updates
640 * @state: atomic state
642 * This function commits the modeset changes that need to be committed before
643 * updating planes. It shuts down all the outputs that need to be shut down and
644 * prepares them (if required) with the new mode.
646 void drm_atomic_helper_commit_pre_planes(struct drm_device *dev,
647 struct drm_atomic_state *state)
649 disable_outputs(dev, state);
650 set_routing_links(dev, state);
651 crtc_set_mode(dev, state);
653 EXPORT_SYMBOL(drm_atomic_helper_commit_pre_planes);
656 * drm_atomic_helper_commit_post_planes - modeset commit after plane updates
658 * @old_state: atomic state object with old state structures
660 * This function commits the modeset changes that need to be committed after
661 * updating planes: It enables all the outputs with the new configuration which
662 * had to be turned off for the update.
664 void drm_atomic_helper_commit_post_planes(struct drm_device *dev,
665 struct drm_atomic_state *old_state)
667 int ncrtcs = old_state->dev->mode_config.num_crtc;
668 int nconnectors = old_state->dev->mode_config.num_connector;
671 for (i = 0; i < ncrtcs; i++) {
672 struct drm_crtc_helper_funcs *funcs;
673 struct drm_crtc *crtc;
675 crtc = old_state->crtcs[i];
677 /* Need to filter out CRTCs where only planes change. */
678 if (!crtc || !crtc->state->mode_changed)
681 funcs = crtc->helper_private;
683 if (crtc->state->enable)
687 for (i = 0; i < nconnectors; i++) {
688 struct drm_connector *connector;
689 struct drm_encoder_helper_funcs *funcs;
690 struct drm_encoder *encoder;
692 connector = old_state->connectors[i];
694 if (!connector || !connector->state->best_encoder)
697 encoder = connector->state->best_encoder;
698 funcs = encoder->helper_private;
701 * Each encoder has at most one connector (since we always steal
702 * it away), so we won't call call enable hooks twice.
705 encoder->bridge->funcs->pre_enable(encoder->bridge);
707 funcs->commit(encoder);
710 encoder->bridge->funcs->enable(encoder->bridge);
713 EXPORT_SYMBOL(drm_atomic_helper_commit_post_planes);
716 wait_for_vblanks(struct drm_device *dev, struct drm_atomic_state *old_state)
718 struct drm_crtc *crtc;
719 struct drm_crtc_state *old_crtc_state;
720 int ncrtcs = old_state->dev->mode_config.num_crtc;
723 for (i = 0; i < ncrtcs; i++) {
724 crtc = old_state->crtcs[i];
725 old_crtc_state = old_state->crtc_states[i];
730 /* No one cares about the old state, so abuse it for tracking
731 * and store whether we hold a vblank reference (and should do a
732 * vblank wait) in the ->enable boolean. */
733 old_crtc_state->enable = false;
735 if (!crtc->state->enable)
738 ret = drm_crtc_vblank_get(crtc);
742 old_crtc_state->enable = true;
743 old_crtc_state->last_vblank_count = drm_vblank_count(dev, i);
746 for (i = 0; i < ncrtcs; i++) {
747 crtc = old_state->crtcs[i];
748 old_crtc_state = old_state->crtc_states[i];
750 if (!crtc || !old_crtc_state->enable)
753 ret = wait_event_timeout(dev->vblank[i].queue,
754 old_crtc_state->last_vblank_count !=
755 drm_vblank_count(dev, i),
756 msecs_to_jiffies(50));
758 drm_crtc_vblank_put(crtc);
763 * drm_atomic_helper_commit - commit validated state object
765 * @state: the driver state object
766 * @async: asynchronous commit
768 * This function commits a with drm_atomic_helper_check() pre-validated state
769 * object. This can still fail when e.g. the framebuffer reservation fails. For
770 * now this doesn't implement asynchronous commits.
773 * Zero for success or -errno.
775 int drm_atomic_helper_commit(struct drm_device *dev,
776 struct drm_atomic_state *state,
784 ret = drm_atomic_helper_prepare_planes(dev, state);
789 * This is the point of no return - everything below never fails except
790 * when the hw goes bonghits. Which means we can commit the new state on
791 * the software side now.
794 drm_atomic_helper_swap_state(dev, state);
797 * Everything below can be run asynchronously without the need to grab
798 * any modeset locks at all under one conditions: It must be guaranteed
799 * that the asynchronous work has either been cancelled (if the driver
800 * supports it, which at least requires that the framebuffers get
801 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
802 * before the new state gets committed on the software side with
803 * drm_atomic_helper_swap_state().
805 * This scheme allows new atomic state updates to be prepared and
806 * checked in parallel to the asynchronous completion of the previous
807 * update. Which is important since compositors need to figure out the
808 * composition of the next frame right after having submitted the
812 drm_atomic_helper_commit_pre_planes(dev, state);
814 drm_atomic_helper_commit_planes(dev, state);
816 drm_atomic_helper_commit_post_planes(dev, state);
818 wait_for_vblanks(dev, state);
820 drm_atomic_helper_cleanup_planes(dev, state);
822 drm_atomic_state_free(state);
826 EXPORT_SYMBOL(drm_atomic_helper_commit);
829 * drm_atomic_helper_prepare_planes - prepare plane resources after commit
831 * @state: atomic state object with old state structures
833 * This function prepares plane state, specifically framebuffers, for the new
834 * configuration. If any failure is encountered this function will call
835 * ->cleanup_fb on any already successfully prepared framebuffer.
838 * 0 on success, negative error code on failure.
840 int drm_atomic_helper_prepare_planes(struct drm_device *dev,
841 struct drm_atomic_state *state)
843 int nplanes = dev->mode_config.num_total_plane;
846 for (i = 0; i < nplanes; i++) {
847 struct drm_plane_helper_funcs *funcs;
848 struct drm_plane *plane = state->planes[i];
849 struct drm_framebuffer *fb;
854 funcs = plane->helper_private;
856 fb = state->plane_states[i]->fb;
858 if (fb && funcs->prepare_fb) {
859 ret = funcs->prepare_fb(plane, fb);
868 for (i--; i >= 0; i--) {
869 struct drm_plane_helper_funcs *funcs;
870 struct drm_plane *plane = state->planes[i];
871 struct drm_framebuffer *fb;
876 funcs = plane->helper_private;
878 fb = state->plane_states[i]->fb;
880 if (fb && funcs->cleanup_fb)
881 funcs->cleanup_fb(plane, fb);
887 EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
890 * drm_atomic_helper_commit_planes - commit plane state
892 * @state: atomic state
894 * This function commits the new plane state using the plane and atomic helper
895 * functions for planes and crtcs. It assumes that the atomic state has already
896 * been pushed into the relevant object state pointers, since this step can no
899 * It still requires the global state object @state to know which planes and
900 * crtcs need to be updated though.
902 void drm_atomic_helper_commit_planes(struct drm_device *dev,
903 struct drm_atomic_state *state)
905 int nplanes = dev->mode_config.num_total_plane;
906 int ncrtcs = dev->mode_config.num_crtc;
909 for (i = 0; i < ncrtcs; i++) {
910 struct drm_crtc_helper_funcs *funcs;
911 struct drm_crtc *crtc = state->crtcs[i];
916 funcs = crtc->helper_private;
918 if (!funcs || !funcs->atomic_begin)
921 funcs->atomic_begin(crtc);
924 for (i = 0; i < nplanes; i++) {
925 struct drm_plane_helper_funcs *funcs;
926 struct drm_plane *plane = state->planes[i];
931 funcs = plane->helper_private;
933 if (!funcs || !funcs->atomic_update)
936 funcs->atomic_update(plane);
939 for (i = 0; i < ncrtcs; i++) {
940 struct drm_crtc_helper_funcs *funcs;
941 struct drm_crtc *crtc = state->crtcs[i];
946 funcs = crtc->helper_private;
948 if (!funcs || !funcs->atomic_flush)
951 funcs->atomic_flush(crtc);
954 EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
957 * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
959 * @old_state: atomic state object with old state structures
961 * This function cleans up plane state, specifically framebuffers, from the old
962 * configuration. Hence the old configuration must be perserved in @old_state to
963 * be able to call this function.
965 * This function must also be called on the new state when the atomic update
966 * fails at any point after calling drm_atomic_helper_prepare_planes().
968 void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
969 struct drm_atomic_state *old_state)
971 int nplanes = dev->mode_config.num_total_plane;
974 for (i = 0; i < nplanes; i++) {
975 struct drm_plane_helper_funcs *funcs;
976 struct drm_plane *plane = old_state->planes[i];
977 struct drm_framebuffer *old_fb;
982 funcs = plane->helper_private;
984 old_fb = old_state->plane_states[i]->fb;
986 if (old_fb && funcs->cleanup_fb)
987 funcs->cleanup_fb(plane, old_fb);
990 EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
993 * drm_atomic_helper_swap_state - store atomic state into current sw state
995 * @state: atomic state
997 * This function stores the atomic state into the current state pointers in all
998 * driver objects. It should be called after all failing steps have been done
999 * and succeeded, but before the actual hardware state is committed.
1001 * For cleanup and error recovery the current state for all changed objects will
1002 * be swaped into @state.
1004 * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
1006 * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
1008 * 2. Do any other steps that might fail.
1010 * 3. Put the staged state into the current state pointers with this function.
1012 * 4. Actually commit the hardware state.
1014 * 5. Call drm_atomic_helper_cleanup_planes with @state, which since step 3
1015 * contains the old state. Also do any other cleanup required with that state.
1017 void drm_atomic_helper_swap_state(struct drm_device *dev,
1018 struct drm_atomic_state *state)
1022 for (i = 0; i < dev->mode_config.num_connector; i++) {
1023 struct drm_connector *connector = state->connectors[i];
1028 connector->state->state = state;
1029 swap(state->connector_states[i], connector->state);
1030 connector->state->state = NULL;
1033 for (i = 0; i < dev->mode_config.num_crtc; i++) {
1034 struct drm_crtc *crtc = state->crtcs[i];
1039 crtc->state->state = state;
1040 swap(state->crtc_states[i], crtc->state);
1041 crtc->state->state = NULL;
1044 for (i = 0; i < dev->mode_config.num_total_plane; i++) {
1045 struct drm_plane *plane = state->planes[i];
1050 plane->state->state = state;
1051 swap(state->plane_states[i], plane->state);
1052 plane->state->state = NULL;
1055 EXPORT_SYMBOL(drm_atomic_helper_swap_state);
1058 * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
1059 * @plane: plane object to update
1060 * @crtc: owning CRTC of owning plane
1061 * @fb: framebuffer to flip onto plane
1062 * @crtc_x: x offset of primary plane on crtc
1063 * @crtc_y: y offset of primary plane on crtc
1064 * @crtc_w: width of primary plane rectangle on crtc
1065 * @crtc_h: height of primary plane rectangle on crtc
1066 * @src_x: x offset of @fb for panning
1067 * @src_y: y offset of @fb for panning
1068 * @src_w: width of source rectangle in @fb
1069 * @src_h: height of source rectangle in @fb
1071 * Provides a default plane update handler using the atomic driver interface.
1074 * Zero on success, error code on failure
1076 int drm_atomic_helper_update_plane(struct drm_plane *plane,
1077 struct drm_crtc *crtc,
1078 struct drm_framebuffer *fb,
1079 int crtc_x, int crtc_y,
1080 unsigned int crtc_w, unsigned int crtc_h,
1081 uint32_t src_x, uint32_t src_y,
1082 uint32_t src_w, uint32_t src_h)
1084 struct drm_atomic_state *state;
1085 struct drm_plane_state *plane_state;
1088 state = drm_atomic_state_alloc(plane->dev);
1092 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1094 plane_state = drm_atomic_get_plane_state(state, plane);
1095 if (IS_ERR(plane_state)) {
1096 ret = PTR_ERR(plane_state);
1100 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
1103 plane_state->fb = fb;
1104 plane_state->crtc_x = crtc_x;
1105 plane_state->crtc_y = crtc_y;
1106 plane_state->crtc_h = crtc_h;
1107 plane_state->crtc_w = crtc_w;
1108 plane_state->src_x = src_x;
1109 plane_state->src_y = src_y;
1110 plane_state->src_h = src_h;
1111 plane_state->src_w = src_w;
1113 ret = drm_atomic_commit(state);
1117 /* Driver takes ownership of state on successful commit. */
1120 if (ret == -EDEADLK)
1123 drm_atomic_state_free(state);
1127 drm_atomic_legacy_backoff(state);
1128 drm_atomic_state_clear(state);
1131 * Someone might have exchanged the framebuffer while we dropped locks
1132 * in the backoff code. We need to fix up the fb refcount tracking the
1135 plane->old_fb = plane->fb;
1139 EXPORT_SYMBOL(drm_atomic_helper_update_plane);
1142 * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
1143 * @plane: plane to disable
1145 * Provides a default plane disable handler using the atomic driver interface.
1148 * Zero on success, error code on failure
1150 int drm_atomic_helper_disable_plane(struct drm_plane *plane)
1152 struct drm_atomic_state *state;
1153 struct drm_plane_state *plane_state;
1156 state = drm_atomic_state_alloc(plane->dev);
1160 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(plane->crtc);
1162 plane_state = drm_atomic_get_plane_state(state, plane);
1163 if (IS_ERR(plane_state)) {
1164 ret = PTR_ERR(plane_state);
1168 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
1171 plane_state->fb = NULL;
1172 plane_state->crtc_x = 0;
1173 plane_state->crtc_y = 0;
1174 plane_state->crtc_h = 0;
1175 plane_state->crtc_w = 0;
1176 plane_state->src_x = 0;
1177 plane_state->src_y = 0;
1178 plane_state->src_h = 0;
1179 plane_state->src_w = 0;
1181 ret = drm_atomic_commit(state);
1185 /* Driver takes ownership of state on successful commit. */
1188 if (ret == -EDEADLK)
1191 drm_atomic_state_free(state);
1195 drm_atomic_legacy_backoff(state);
1196 drm_atomic_state_clear(state);
1199 * Someone might have exchanged the framebuffer while we dropped locks
1200 * in the backoff code. We need to fix up the fb refcount tracking the
1203 plane->old_fb = plane->fb;
1207 EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
1209 static int update_output_state(struct drm_atomic_state *state,
1210 struct drm_mode_set *set)
1212 struct drm_device *dev = set->crtc->dev;
1213 struct drm_connector_state *conn_state;
1214 int nconnectors = state->dev->mode_config.num_connector;
1215 int ncrtcs = state->dev->mode_config.num_crtc;
1218 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
1219 state->acquire_ctx);
1223 /* First grab all affected connector/crtc states. */
1224 for (i = 0; i < set->num_connectors; i++) {
1225 conn_state = drm_atomic_get_connector_state(state,
1226 set->connectors[i]);
1227 if (IS_ERR(conn_state))
1228 return PTR_ERR(conn_state);
1231 for (i = 0; i < ncrtcs; i++) {
1232 struct drm_crtc *crtc = state->crtcs[i];
1237 ret = drm_atomic_add_affected_connectors(state, crtc);
1242 /* Then recompute connector->crtc links and crtc enabling state. */
1243 for (i = 0; i < nconnectors; i++) {
1244 struct drm_connector *connector;
1246 connector = state->connectors[i];
1247 conn_state = state->connector_states[i];
1252 if (conn_state->crtc == set->crtc) {
1253 ret = drm_atomic_set_crtc_for_connector(conn_state,
1259 for (j = 0; j < set->num_connectors; j++) {
1260 if (set->connectors[j] == connector) {
1261 ret = drm_atomic_set_crtc_for_connector(conn_state,
1270 for (i = 0; i < ncrtcs; i++) {
1271 struct drm_crtc *crtc = state->crtcs[i];
1272 struct drm_crtc_state *crtc_state = state->crtc_states[i];
1277 /* Don't update ->enable for the CRTC in the set_config request,
1278 * since a mismatch would indicate a bug in the upper layers.
1279 * The actual modeset code later on will catch any
1280 * inconsistencies here. */
1281 if (crtc == set->crtc)
1284 crtc_state->enable =
1285 drm_atomic_connectors_for_crtc(state, crtc);
1292 * drm_atomic_helper_set_config - set a new config from userspace
1293 * @set: mode set configuration
1295 * Provides a default crtc set_config handler using the atomic driver interface.
1298 * Returns 0 on success, negative errno numbers on failure.
1300 int drm_atomic_helper_set_config(struct drm_mode_set *set)
1302 struct drm_atomic_state *state;
1303 struct drm_crtc *crtc = set->crtc;
1304 struct drm_crtc_state *crtc_state;
1305 struct drm_plane_state *primary_state;
1308 state = drm_atomic_state_alloc(crtc->dev);
1312 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1314 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1315 if (IS_ERR(crtc_state)) {
1316 ret = PTR_ERR(crtc_state);
1322 WARN_ON(set->num_connectors);
1324 crtc_state->enable = false;
1329 WARN_ON(!set->num_connectors);
1331 crtc_state->enable = true;
1332 drm_mode_copy(&crtc_state->mode, set->mode);
1334 primary_state = drm_atomic_get_plane_state(state, crtc->primary);
1335 if (IS_ERR(primary_state)) {
1336 ret = PTR_ERR(primary_state);
1340 ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
1343 primary_state->fb = set->fb;
1344 primary_state->crtc_x = 0;
1345 primary_state->crtc_y = 0;
1346 primary_state->crtc_h = set->mode->vdisplay;
1347 primary_state->crtc_w = set->mode->hdisplay;
1348 primary_state->src_x = set->x << 16;
1349 primary_state->src_y = set->y << 16;
1350 primary_state->src_h = set->mode->vdisplay << 16;
1351 primary_state->src_w = set->mode->hdisplay << 16;
1354 ret = update_output_state(state, set);
1358 ret = drm_atomic_commit(state);
1362 /* Driver takes ownership of state on successful commit. */
1365 if (ret == -EDEADLK)
1368 drm_atomic_state_free(state);
1372 drm_atomic_legacy_backoff(state);
1373 drm_atomic_state_clear(state);
1376 * Someone might have exchanged the framebuffer while we dropped locks
1377 * in the backoff code. We need to fix up the fb refcount tracking the
1380 crtc->primary->old_fb = crtc->primary->fb;
1384 EXPORT_SYMBOL(drm_atomic_helper_set_config);
1387 * drm_atomic_helper_crtc_set_property - helper for crtc prorties
1389 * @property: DRM property
1390 * @val: value of property
1392 * Provides a default plane disablle handler using the atomic driver interface.
1395 * Zero on success, error code on failure
1398 drm_atomic_helper_crtc_set_property(struct drm_crtc *crtc,
1399 struct drm_property *property,
1402 struct drm_atomic_state *state;
1403 struct drm_crtc_state *crtc_state;
1406 state = drm_atomic_state_alloc(crtc->dev);
1410 /* ->set_property is always called with all locks held. */
1411 state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
1413 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1414 if (IS_ERR(crtc_state)) {
1415 ret = PTR_ERR(crtc_state);
1419 ret = crtc->funcs->atomic_set_property(crtc, crtc_state,
1424 ret = drm_atomic_commit(state);
1428 /* Driver takes ownership of state on successful commit. */
1431 if (ret == -EDEADLK)
1434 drm_atomic_state_free(state);
1438 drm_atomic_legacy_backoff(state);
1439 drm_atomic_state_clear(state);
1443 EXPORT_SYMBOL(drm_atomic_helper_crtc_set_property);
1446 * drm_atomic_helper_plane_set_property - helper for plane prorties
1448 * @property: DRM property
1449 * @val: value of property
1451 * Provides a default plane disable handler using the atomic driver interface.
1454 * Zero on success, error code on failure
1457 drm_atomic_helper_plane_set_property(struct drm_plane *plane,
1458 struct drm_property *property,
1461 struct drm_atomic_state *state;
1462 struct drm_plane_state *plane_state;
1465 state = drm_atomic_state_alloc(plane->dev);
1469 /* ->set_property is always called with all locks held. */
1470 state->acquire_ctx = plane->dev->mode_config.acquire_ctx;
1472 plane_state = drm_atomic_get_plane_state(state, plane);
1473 if (IS_ERR(plane_state)) {
1474 ret = PTR_ERR(plane_state);
1478 ret = plane->funcs->atomic_set_property(plane, plane_state,
1483 ret = drm_atomic_commit(state);
1487 /* Driver takes ownership of state on successful commit. */
1490 if (ret == -EDEADLK)
1493 drm_atomic_state_free(state);
1497 drm_atomic_legacy_backoff(state);
1498 drm_atomic_state_clear(state);
1502 EXPORT_SYMBOL(drm_atomic_helper_plane_set_property);
1505 * drm_atomic_helper_connector_set_property - helper for connector prorties
1506 * @connector: DRM connector
1507 * @property: DRM property
1508 * @val: value of property
1510 * Provides a default plane disablle handler using the atomic driver interface.
1513 * Zero on success, error code on failure
1516 drm_atomic_helper_connector_set_property(struct drm_connector *connector,
1517 struct drm_property *property,
1520 struct drm_atomic_state *state;
1521 struct drm_connector_state *connector_state;
1524 state = drm_atomic_state_alloc(connector->dev);
1528 /* ->set_property is always called with all locks held. */
1529 state->acquire_ctx = connector->dev->mode_config.acquire_ctx;
1531 connector_state = drm_atomic_get_connector_state(state, connector);
1532 if (IS_ERR(connector_state)) {
1533 ret = PTR_ERR(connector_state);
1537 ret = connector->funcs->atomic_set_property(connector, connector_state,
1542 ret = drm_atomic_commit(state);
1546 /* Driver takes ownership of state on successful commit. */
1549 if (ret == -EDEADLK)
1552 drm_atomic_state_free(state);
1556 drm_atomic_legacy_backoff(state);
1557 drm_atomic_state_clear(state);
1561 EXPORT_SYMBOL(drm_atomic_helper_connector_set_property);