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 * DOC: implementing async commit
854 * For now the atomic helpers don't support async commit directly. If there is
855 * real need it could be added though, using the dma-buf fence infrastructure
856 * for generic synchronization with outstanding rendering.
858 * For now drivers have to implement async commit themselves, with the following
859 * sequence being the recommended one:
861 * 1. Run drm_atomic_helper_prepare_planes() first. This is the only function
862 * which commit needs to call which can fail, so we want to run it first and
865 * 2. Synchronize with any outstanding asynchronous commit worker threads which
866 * might be affected the new state update. This can be done by either cancelling
867 * or flushing the work items, depending upon whether the driver can deal with
868 * cancelled updates. Note that it is important to ensure that the framebuffer
869 * cleanup is still done when cancelling.
871 * For sufficient parallelism it is recommended to have a work item per crtc
872 * (for updates which don't touch global state) and a global one. Then we only
873 * need to synchronize with the crtc work items for changed crtcs and the global
874 * work item, which allows nice concurrent updates on disjoint sets of crtcs.
876 * 3. The software state is updated synchronously with
877 * drm_atomic_helper_swap_state. Doing this under the protection of all modeset
878 * locks means concurrent callers never see inconsistent state. And doing this
879 * while it's guaranteed that no relevant async worker runs means that async
880 * workers do not need grab any locks. Actually they must not grab locks, for
881 * otherwise the work flushing will deadlock.
883 * 4. Schedule a work item to do all subsequent steps, using the split-out
884 * commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and
885 * then cleaning up the framebuffers after the old framebuffer is no longer
890 * drm_atomic_helper_prepare_planes - prepare plane resources after commit
892 * @state: atomic state object with old state structures
894 * This function prepares plane state, specifically framebuffers, for the new
895 * configuration. If any failure is encountered this function will call
896 * ->cleanup_fb on any already successfully prepared framebuffer.
899 * 0 on success, negative error code on failure.
901 int drm_atomic_helper_prepare_planes(struct drm_device *dev,
902 struct drm_atomic_state *state)
904 int nplanes = dev->mode_config.num_total_plane;
907 for (i = 0; i < nplanes; i++) {
908 struct drm_plane_helper_funcs *funcs;
909 struct drm_plane *plane = state->planes[i];
910 struct drm_framebuffer *fb;
915 funcs = plane->helper_private;
917 fb = state->plane_states[i]->fb;
919 if (fb && funcs->prepare_fb) {
920 ret = funcs->prepare_fb(plane, fb);
929 for (i--; i >= 0; i--) {
930 struct drm_plane_helper_funcs *funcs;
931 struct drm_plane *plane = state->planes[i];
932 struct drm_framebuffer *fb;
937 funcs = plane->helper_private;
939 fb = state->plane_states[i]->fb;
941 if (fb && funcs->cleanup_fb)
942 funcs->cleanup_fb(plane, fb);
948 EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
951 * drm_atomic_helper_commit_planes - commit plane state
953 * @state: atomic state
955 * This function commits the new plane state using the plane and atomic helper
956 * functions for planes and crtcs. It assumes that the atomic state has already
957 * been pushed into the relevant object state pointers, since this step can no
960 * It still requires the global state object @state to know which planes and
961 * crtcs need to be updated though.
963 void drm_atomic_helper_commit_planes(struct drm_device *dev,
964 struct drm_atomic_state *state)
966 int nplanes = dev->mode_config.num_total_plane;
967 int ncrtcs = dev->mode_config.num_crtc;
970 for (i = 0; i < ncrtcs; i++) {
971 struct drm_crtc_helper_funcs *funcs;
972 struct drm_crtc *crtc = state->crtcs[i];
977 funcs = crtc->helper_private;
979 if (!funcs || !funcs->atomic_begin)
982 funcs->atomic_begin(crtc);
985 for (i = 0; i < nplanes; i++) {
986 struct drm_plane_helper_funcs *funcs;
987 struct drm_plane *plane = state->planes[i];
992 funcs = plane->helper_private;
994 if (!funcs || !funcs->atomic_update)
997 funcs->atomic_update(plane);
1000 for (i = 0; i < ncrtcs; i++) {
1001 struct drm_crtc_helper_funcs *funcs;
1002 struct drm_crtc *crtc = state->crtcs[i];
1007 funcs = crtc->helper_private;
1009 if (!funcs || !funcs->atomic_flush)
1012 funcs->atomic_flush(crtc);
1015 EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
1018 * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
1020 * @old_state: atomic state object with old state structures
1022 * This function cleans up plane state, specifically framebuffers, from the old
1023 * configuration. Hence the old configuration must be perserved in @old_state to
1024 * be able to call this function.
1026 * This function must also be called on the new state when the atomic update
1027 * fails at any point after calling drm_atomic_helper_prepare_planes().
1029 void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
1030 struct drm_atomic_state *old_state)
1032 int nplanes = dev->mode_config.num_total_plane;
1035 for (i = 0; i < nplanes; i++) {
1036 struct drm_plane_helper_funcs *funcs;
1037 struct drm_plane *plane = old_state->planes[i];
1038 struct drm_framebuffer *old_fb;
1043 funcs = plane->helper_private;
1045 old_fb = old_state->plane_states[i]->fb;
1047 if (old_fb && funcs->cleanup_fb)
1048 funcs->cleanup_fb(plane, old_fb);
1051 EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
1054 * drm_atomic_helper_swap_state - store atomic state into current sw state
1056 * @state: atomic state
1058 * This function stores the atomic state into the current state pointers in all
1059 * driver objects. It should be called after all failing steps have been done
1060 * and succeeded, but before the actual hardware state is committed.
1062 * For cleanup and error recovery the current state for all changed objects will
1063 * be swaped into @state.
1065 * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
1067 * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
1069 * 2. Do any other steps that might fail.
1071 * 3. Put the staged state into the current state pointers with this function.
1073 * 4. Actually commit the hardware state.
1075 * 5. Call drm_atomic_helper_cleanup_planes with @state, which since step 3
1076 * contains the old state. Also do any other cleanup required with that state.
1078 void drm_atomic_helper_swap_state(struct drm_device *dev,
1079 struct drm_atomic_state *state)
1083 for (i = 0; i < dev->mode_config.num_connector; i++) {
1084 struct drm_connector *connector = state->connectors[i];
1089 connector->state->state = state;
1090 swap(state->connector_states[i], connector->state);
1091 connector->state->state = NULL;
1094 for (i = 0; i < dev->mode_config.num_crtc; i++) {
1095 struct drm_crtc *crtc = state->crtcs[i];
1100 crtc->state->state = state;
1101 swap(state->crtc_states[i], crtc->state);
1102 crtc->state->state = NULL;
1105 for (i = 0; i < dev->mode_config.num_total_plane; i++) {
1106 struct drm_plane *plane = state->planes[i];
1111 plane->state->state = state;
1112 swap(state->plane_states[i], plane->state);
1113 plane->state->state = NULL;
1116 EXPORT_SYMBOL(drm_atomic_helper_swap_state);
1119 * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
1120 * @plane: plane object to update
1121 * @crtc: owning CRTC of owning plane
1122 * @fb: framebuffer to flip onto plane
1123 * @crtc_x: x offset of primary plane on crtc
1124 * @crtc_y: y offset of primary plane on crtc
1125 * @crtc_w: width of primary plane rectangle on crtc
1126 * @crtc_h: height of primary plane rectangle on crtc
1127 * @src_x: x offset of @fb for panning
1128 * @src_y: y offset of @fb for panning
1129 * @src_w: width of source rectangle in @fb
1130 * @src_h: height of source rectangle in @fb
1132 * Provides a default plane update handler using the atomic driver interface.
1135 * Zero on success, error code on failure
1137 int drm_atomic_helper_update_plane(struct drm_plane *plane,
1138 struct drm_crtc *crtc,
1139 struct drm_framebuffer *fb,
1140 int crtc_x, int crtc_y,
1141 unsigned int crtc_w, unsigned int crtc_h,
1142 uint32_t src_x, uint32_t src_y,
1143 uint32_t src_w, uint32_t src_h)
1145 struct drm_atomic_state *state;
1146 struct drm_plane_state *plane_state;
1149 state = drm_atomic_state_alloc(plane->dev);
1153 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1155 plane_state = drm_atomic_get_plane_state(state, plane);
1156 if (IS_ERR(plane_state)) {
1157 ret = PTR_ERR(plane_state);
1161 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
1164 plane_state->fb = fb;
1165 plane_state->crtc_x = crtc_x;
1166 plane_state->crtc_y = crtc_y;
1167 plane_state->crtc_h = crtc_h;
1168 plane_state->crtc_w = crtc_w;
1169 plane_state->src_x = src_x;
1170 plane_state->src_y = src_y;
1171 plane_state->src_h = src_h;
1172 plane_state->src_w = src_w;
1174 ret = drm_atomic_commit(state);
1178 /* Driver takes ownership of state on successful commit. */
1181 if (ret == -EDEADLK)
1184 drm_atomic_state_free(state);
1188 drm_atomic_legacy_backoff(state);
1189 drm_atomic_state_clear(state);
1192 * Someone might have exchanged the framebuffer while we dropped locks
1193 * in the backoff code. We need to fix up the fb refcount tracking the
1196 plane->old_fb = plane->fb;
1200 EXPORT_SYMBOL(drm_atomic_helper_update_plane);
1203 * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
1204 * @plane: plane to disable
1206 * Provides a default plane disable handler using the atomic driver interface.
1209 * Zero on success, error code on failure
1211 int drm_atomic_helper_disable_plane(struct drm_plane *plane)
1213 struct drm_atomic_state *state;
1214 struct drm_plane_state *plane_state;
1217 state = drm_atomic_state_alloc(plane->dev);
1221 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(plane->crtc);
1223 plane_state = drm_atomic_get_plane_state(state, plane);
1224 if (IS_ERR(plane_state)) {
1225 ret = PTR_ERR(plane_state);
1229 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
1232 plane_state->fb = NULL;
1233 plane_state->crtc_x = 0;
1234 plane_state->crtc_y = 0;
1235 plane_state->crtc_h = 0;
1236 plane_state->crtc_w = 0;
1237 plane_state->src_x = 0;
1238 plane_state->src_y = 0;
1239 plane_state->src_h = 0;
1240 plane_state->src_w = 0;
1242 ret = drm_atomic_commit(state);
1246 /* Driver takes ownership of state on successful commit. */
1249 if (ret == -EDEADLK)
1252 drm_atomic_state_free(state);
1256 drm_atomic_legacy_backoff(state);
1257 drm_atomic_state_clear(state);
1260 * Someone might have exchanged the framebuffer while we dropped locks
1261 * in the backoff code. We need to fix up the fb refcount tracking the
1264 plane->old_fb = plane->fb;
1268 EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
1270 static int update_output_state(struct drm_atomic_state *state,
1271 struct drm_mode_set *set)
1273 struct drm_device *dev = set->crtc->dev;
1274 struct drm_connector_state *conn_state;
1275 int nconnectors = state->dev->mode_config.num_connector;
1276 int ncrtcs = state->dev->mode_config.num_crtc;
1279 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
1280 state->acquire_ctx);
1284 /* First grab all affected connector/crtc states. */
1285 for (i = 0; i < set->num_connectors; i++) {
1286 conn_state = drm_atomic_get_connector_state(state,
1287 set->connectors[i]);
1288 if (IS_ERR(conn_state))
1289 return PTR_ERR(conn_state);
1292 for (i = 0; i < ncrtcs; i++) {
1293 struct drm_crtc *crtc = state->crtcs[i];
1298 ret = drm_atomic_add_affected_connectors(state, crtc);
1303 /* Then recompute connector->crtc links and crtc enabling state. */
1304 for (i = 0; i < nconnectors; i++) {
1305 struct drm_connector *connector;
1307 connector = state->connectors[i];
1308 conn_state = state->connector_states[i];
1313 if (conn_state->crtc == set->crtc) {
1314 ret = drm_atomic_set_crtc_for_connector(conn_state,
1320 for (j = 0; j < set->num_connectors; j++) {
1321 if (set->connectors[j] == connector) {
1322 ret = drm_atomic_set_crtc_for_connector(conn_state,
1331 for (i = 0; i < ncrtcs; i++) {
1332 struct drm_crtc *crtc = state->crtcs[i];
1333 struct drm_crtc_state *crtc_state = state->crtc_states[i];
1338 /* Don't update ->enable for the CRTC in the set_config request,
1339 * since a mismatch would indicate a bug in the upper layers.
1340 * The actual modeset code later on will catch any
1341 * inconsistencies here. */
1342 if (crtc == set->crtc)
1345 crtc_state->enable =
1346 drm_atomic_connectors_for_crtc(state, crtc);
1353 * drm_atomic_helper_set_config - set a new config from userspace
1354 * @set: mode set configuration
1356 * Provides a default crtc set_config handler using the atomic driver interface.
1359 * Returns 0 on success, negative errno numbers on failure.
1361 int drm_atomic_helper_set_config(struct drm_mode_set *set)
1363 struct drm_atomic_state *state;
1364 struct drm_crtc *crtc = set->crtc;
1365 struct drm_crtc_state *crtc_state;
1366 struct drm_plane_state *primary_state;
1369 state = drm_atomic_state_alloc(crtc->dev);
1373 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1375 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1376 if (IS_ERR(crtc_state)) {
1377 ret = PTR_ERR(crtc_state);
1383 WARN_ON(set->num_connectors);
1385 crtc_state->enable = false;
1390 WARN_ON(!set->num_connectors);
1392 crtc_state->enable = true;
1393 drm_mode_copy(&crtc_state->mode, set->mode);
1395 primary_state = drm_atomic_get_plane_state(state, crtc->primary);
1396 if (IS_ERR(primary_state)) {
1397 ret = PTR_ERR(primary_state);
1401 ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
1404 primary_state->fb = set->fb;
1405 primary_state->crtc_x = 0;
1406 primary_state->crtc_y = 0;
1407 primary_state->crtc_h = set->mode->vdisplay;
1408 primary_state->crtc_w = set->mode->hdisplay;
1409 primary_state->src_x = set->x << 16;
1410 primary_state->src_y = set->y << 16;
1411 primary_state->src_h = set->mode->vdisplay << 16;
1412 primary_state->src_w = set->mode->hdisplay << 16;
1415 ret = update_output_state(state, set);
1419 ret = drm_atomic_commit(state);
1423 /* Driver takes ownership of state on successful commit. */
1426 if (ret == -EDEADLK)
1429 drm_atomic_state_free(state);
1433 drm_atomic_legacy_backoff(state);
1434 drm_atomic_state_clear(state);
1437 * Someone might have exchanged the framebuffer while we dropped locks
1438 * in the backoff code. We need to fix up the fb refcount tracking the
1441 crtc->primary->old_fb = crtc->primary->fb;
1445 EXPORT_SYMBOL(drm_atomic_helper_set_config);
1448 * drm_atomic_helper_crtc_set_property - helper for crtc prorties
1450 * @property: DRM property
1451 * @val: value of property
1453 * Provides a default plane disablle handler using the atomic driver interface.
1456 * Zero on success, error code on failure
1459 drm_atomic_helper_crtc_set_property(struct drm_crtc *crtc,
1460 struct drm_property *property,
1463 struct drm_atomic_state *state;
1464 struct drm_crtc_state *crtc_state;
1467 state = drm_atomic_state_alloc(crtc->dev);
1471 /* ->set_property is always called with all locks held. */
1472 state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
1474 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1475 if (IS_ERR(crtc_state)) {
1476 ret = PTR_ERR(crtc_state);
1480 ret = crtc->funcs->atomic_set_property(crtc, crtc_state,
1485 ret = drm_atomic_commit(state);
1489 /* Driver takes ownership of state on successful commit. */
1492 if (ret == -EDEADLK)
1495 drm_atomic_state_free(state);
1499 drm_atomic_legacy_backoff(state);
1500 drm_atomic_state_clear(state);
1504 EXPORT_SYMBOL(drm_atomic_helper_crtc_set_property);
1507 * drm_atomic_helper_plane_set_property - helper for plane prorties
1509 * @property: DRM property
1510 * @val: value of property
1512 * Provides a default plane disable handler using the atomic driver interface.
1515 * Zero on success, error code on failure
1518 drm_atomic_helper_plane_set_property(struct drm_plane *plane,
1519 struct drm_property *property,
1522 struct drm_atomic_state *state;
1523 struct drm_plane_state *plane_state;
1526 state = drm_atomic_state_alloc(plane->dev);
1530 /* ->set_property is always called with all locks held. */
1531 state->acquire_ctx = plane->dev->mode_config.acquire_ctx;
1533 plane_state = drm_atomic_get_plane_state(state, plane);
1534 if (IS_ERR(plane_state)) {
1535 ret = PTR_ERR(plane_state);
1539 ret = plane->funcs->atomic_set_property(plane, plane_state,
1544 ret = drm_atomic_commit(state);
1548 /* Driver takes ownership of state on successful commit. */
1551 if (ret == -EDEADLK)
1554 drm_atomic_state_free(state);
1558 drm_atomic_legacy_backoff(state);
1559 drm_atomic_state_clear(state);
1563 EXPORT_SYMBOL(drm_atomic_helper_plane_set_property);
1566 * drm_atomic_helper_connector_set_property - helper for connector prorties
1567 * @connector: DRM connector
1568 * @property: DRM property
1569 * @val: value of property
1571 * Provides a default plane disablle handler using the atomic driver interface.
1574 * Zero on success, error code on failure
1577 drm_atomic_helper_connector_set_property(struct drm_connector *connector,
1578 struct drm_property *property,
1581 struct drm_atomic_state *state;
1582 struct drm_connector_state *connector_state;
1585 state = drm_atomic_state_alloc(connector->dev);
1589 /* ->set_property is always called with all locks held. */
1590 state->acquire_ctx = connector->dev->mode_config.acquire_ctx;
1592 connector_state = drm_atomic_get_connector_state(state, connector);
1593 if (IS_ERR(connector_state)) {
1594 ret = PTR_ERR(connector_state);
1598 ret = connector->funcs->atomic_set_property(connector, connector_state,
1603 ret = drm_atomic_commit(state);
1607 /* Driver takes ownership of state on successful commit. */
1610 if (ret == -EDEADLK)
1613 drm_atomic_state_free(state);
1617 drm_atomic_legacy_backoff(state);
1618 drm_atomic_state_clear(state);
1622 EXPORT_SYMBOL(drm_atomic_helper_connector_set_property);
1625 * drm_atomic_helper_page_flip - execute a legacy page flip
1627 * @fb: DRM framebuffer
1628 * @event: optional DRM event to signal upon completion
1629 * @flags: flip flags for non-vblank sync'ed updates
1631 * Provides a default page flip implementation using the atomic driver interface.
1633 * Note that for now so called async page flips (i.e. updates which are not
1634 * synchronized to vblank) are not supported, since the atomic interfaces have
1635 * no provisions for this yet.
1638 * Returns 0 on success, negative errno numbers on failure.
1640 int drm_atomic_helper_page_flip(struct drm_crtc *crtc,
1641 struct drm_framebuffer *fb,
1642 struct drm_pending_vblank_event *event,
1645 struct drm_plane *plane = crtc->primary;
1646 struct drm_atomic_state *state;
1647 struct drm_plane_state *plane_state;
1648 struct drm_crtc_state *crtc_state;
1651 if (flags & DRM_MODE_PAGE_FLIP_ASYNC)
1654 state = drm_atomic_state_alloc(plane->dev);
1658 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1660 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1661 if (IS_ERR(crtc_state)) {
1662 ret = PTR_ERR(crtc_state);
1665 crtc_state->event = event;
1667 plane_state = drm_atomic_get_plane_state(state, plane);
1668 if (IS_ERR(plane_state)) {
1669 ret = PTR_ERR(plane_state);
1673 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
1676 plane_state->fb = fb;
1678 ret = drm_atomic_async_commit(state);
1682 /* TODO: ->page_flip is the only driver callback where the core
1683 * doesn't update plane->fb. For now patch it up here. */
1684 plane->fb = plane->state->fb;
1686 /* Driver takes ownership of state on successful async commit. */
1689 if (ret == -EDEADLK)
1692 drm_atomic_state_free(state);
1696 drm_atomic_legacy_backoff(state);
1697 drm_atomic_state_clear(state);
1700 * Someone might have exchanged the framebuffer while we dropped locks
1701 * in the backoff code. We need to fix up the fb refcount tracking the
1704 plane->old_fb = plane->fb;
1708 EXPORT_SYMBOL(drm_atomic_helper_page_flip);
1711 * drm_atomic_helper_crtc_reset - default ->reset hook for CRTCs
1714 * Resets the atomic state for @crtc by freeing the state pointer (which might
1715 * be NULL, e.g. at driver load time) and allocating a new empty state object.
1717 void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc)
1720 crtc->state = kzalloc(sizeof(*crtc->state), GFP_KERNEL);
1722 EXPORT_SYMBOL(drm_atomic_helper_crtc_reset);
1725 * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
1728 * Default CRTC state duplicate hook for drivers which don't have their own
1729 * subclassed CRTC state structure.
1731 struct drm_crtc_state *
1732 drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc)
1734 struct drm_crtc_state *state;
1736 if (WARN_ON(!crtc->state))
1739 state = kmemdup(crtc->state, sizeof(*crtc->state), GFP_KERNEL);
1742 state->mode_changed = false;
1743 state->planes_changed = false;
1744 state->event = NULL;
1749 EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state);
1752 * drm_atomic_helper_crtc_destroy_state - default state destroy hook
1754 * @state: CRTC state object to release
1756 * Default CRTC state destroy hook for drivers which don't have their own
1757 * subclassed CRTC state structure.
1759 void drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
1760 struct drm_crtc_state *state)
1764 EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state);
1767 * drm_atomic_helper_plane_reset - default ->reset hook for planes
1770 * Resets the atomic state for @plane by freeing the state pointer (which might
1771 * be NULL, e.g. at driver load time) and allocating a new empty state object.
1773 void drm_atomic_helper_plane_reset(struct drm_plane *plane)
1775 kfree(plane->state);
1776 plane->state = kzalloc(sizeof(*plane->state), GFP_KERNEL);
1778 EXPORT_SYMBOL(drm_atomic_helper_plane_reset);
1781 * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
1784 * Default plane state duplicate hook for drivers which don't have their own
1785 * subclassed plane state structure.
1787 struct drm_plane_state *
1788 drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane)
1790 if (WARN_ON(!plane->state))
1793 return kmemdup(plane->state, sizeof(*plane->state), GFP_KERNEL);
1795 EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state);
1798 * drm_atomic_helper_plane_destroy_state - default state destroy hook
1800 * @state: plane state object to release
1802 * Default plane state destroy hook for drivers which don't have their own
1803 * subclassed plane state structure.
1805 void drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
1806 struct drm_plane_state *state)
1810 EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state);
1813 * drm_atomic_helper_connector_reset - default ->reset hook for connectors
1814 * @connector: drm connector
1816 * Resets the atomic state for @connector by freeing the state pointer (which
1817 * might be NULL, e.g. at driver load time) and allocating a new empty state
1820 void drm_atomic_helper_connector_reset(struct drm_connector *connector)
1822 kfree(connector->state);
1823 connector->state = kzalloc(sizeof(*connector->state), GFP_KERNEL);
1825 EXPORT_SYMBOL(drm_atomic_helper_connector_reset);
1828 * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
1829 * @connector: drm connector
1831 * Default connector state duplicate hook for drivers which don't have their own
1832 * subclassed connector state structure.
1834 struct drm_connector_state *
1835 drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
1837 if (WARN_ON(!connector->state))
1840 return kmemdup(connector->state, sizeof(*connector->state), GFP_KERNEL);
1842 EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
1845 * drm_atomic_helper_connector_destroy_state - default state destroy hook
1846 * @connector: drm connector
1847 * @state: connector state object to release
1849 * Default connector state destroy hook for drivers which don't have their own
1850 * subclassed connector state structure.
1852 void drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
1853 struct drm_connector_state *state)
1857 EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state);