2 * Copyright (C) 2014 Red Hat
3 * Author: Rob Clark <robdclark@gmail.com>
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along with
15 * this program. If not, see <http://www.gnu.org/licenses/>.
23 struct drm_atomic_state *state;
25 struct msm_fence_cb fence_cb;
28 static void fence_cb(struct msm_fence_cb *cb);
30 static struct msm_commit *new_commit(struct drm_atomic_state *state)
32 struct msm_commit *c = kzalloc(sizeof(*c), GFP_KERNEL);
38 /* TODO we might need a way to indicate to run the cb on a
39 * different wq so wait_for_vblanks() doesn't block retiring
42 INIT_FENCE_CB(&c->fence_cb, fence_cb);
47 /* The (potentially) asynchronous part of the commit. At this point
48 * nothing can fail short of armageddon.
50 static void complete_commit(struct msm_commit *c)
52 struct drm_atomic_state *state = c->state;
53 struct drm_device *dev = state->dev;
55 drm_atomic_helper_commit_pre_planes(dev, state);
57 drm_atomic_helper_commit_planes(dev, state);
59 drm_atomic_helper_commit_post_planes(dev, state);
61 drm_atomic_helper_wait_for_vblanks(dev, state);
63 drm_atomic_helper_cleanup_planes(dev, state);
65 drm_atomic_state_free(state);
70 static void fence_cb(struct msm_fence_cb *cb)
72 struct msm_commit *c =
73 container_of(cb, struct msm_commit, fence_cb);
77 static void add_fb(struct msm_commit *c, struct drm_framebuffer *fb)
79 struct drm_gem_object *obj = msm_framebuffer_bo(fb, 0);
80 c->fence = max(c->fence, msm_gem_fence(to_msm_bo(obj), MSM_PREP_READ));
85 * drm_atomic_helper_commit - commit validated state object
87 * @state: the driver state object
88 * @async: asynchronous commit
90 * This function commits a with drm_atomic_helper_check() pre-validated state
91 * object. This can still fail when e.g. the framebuffer reservation fails. For
92 * now this doesn't implement asynchronous commits.
95 * Zero for success or -errno.
97 int msm_atomic_commit(struct drm_device *dev,
98 struct drm_atomic_state *state, bool async)
100 struct msm_commit *c;
101 int nplanes = dev->mode_config.num_total_plane;
104 ret = drm_atomic_helper_prepare_planes(dev, state);
108 c = new_commit(state);
111 * Figure out what fence to wait for:
113 for (i = 0; i < nplanes; i++) {
114 struct drm_plane *plane = state->planes[i];
115 struct drm_plane_state *new_state = state->plane_states[i];
120 if ((plane->state->fb != new_state->fb) && new_state->fb)
121 add_fb(c, new_state->fb);
125 * This is the point of no return - everything below never fails except
126 * when the hw goes bonghits. Which means we can commit the new state on
127 * the software side now.
130 drm_atomic_helper_swap_state(dev, state);
133 * Everything below can be run asynchronously without the need to grab
134 * any modeset locks at all under one conditions: It must be guaranteed
135 * that the asynchronous work has either been cancelled (if the driver
136 * supports it, which at least requires that the framebuffers get
137 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
138 * before the new state gets committed on the software side with
139 * drm_atomic_helper_swap_state().
141 * This scheme allows new atomic state updates to be prepared and
142 * checked in parallel to the asynchronous completion of the previous
143 * update. Which is important since compositors need to figure out the
144 * composition of the next frame right after having submitted the
149 msm_queue_fence_cb(dev, &c->fence_cb, c->fence);
153 ret = msm_wait_fence_interruptable(dev, c->fence, NULL);
155 WARN_ON(ret); // TODO unswap state back? or??