blob: f8f18e882f97b92e39856baab23d47d14dffa96a [file] [log] [blame]
Rob Clarkcf3a7e42014-11-08 13:21:06 -05001/*
2 * Copyright (C) 2014 Red Hat
3 * Author: Rob Clark <robdclark@gmail.com>
4 *
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.
8 *
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
12 * more details.
13 *
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/>.
16 */
17
18#include "msm_drv.h"
19#include "msm_kms.h"
20#include "msm_gem.h"
21
22struct msm_commit {
23 struct drm_atomic_state *state;
24 uint32_t fence;
25 struct msm_fence_cb fence_cb;
26};
27
28static void fence_cb(struct msm_fence_cb *cb);
29
30static struct msm_commit *new_commit(struct drm_atomic_state *state)
31{
32 struct msm_commit *c = kzalloc(sizeof(*c), GFP_KERNEL);
33
34 if (!c)
35 return NULL;
36
37 c->state = state;
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
40 * bo's..
41 */
42 INIT_FENCE_CB(&c->fence_cb, fence_cb);
43
44 return c;
45}
46
47/* The (potentially) asynchronous part of the commit. At this point
48 * nothing can fail short of armageddon.
49 */
50static void complete_commit(struct msm_commit *c)
51{
52 struct drm_atomic_state *state = c->state;
53 struct drm_device *dev = state->dev;
54
55 drm_atomic_helper_commit_pre_planes(dev, state);
56
57 drm_atomic_helper_commit_planes(dev, state);
58
59 drm_atomic_helper_commit_post_planes(dev, state);
60
61 drm_atomic_helper_wait_for_vblanks(dev, state);
62
63 drm_atomic_helper_cleanup_planes(dev, state);
64
65 drm_atomic_state_free(state);
66
67 kfree(c);
68}
69
70static void fence_cb(struct msm_fence_cb *cb)
71{
72 struct msm_commit *c =
73 container_of(cb, struct msm_commit, fence_cb);
74 complete_commit(c);
75}
76
77static void add_fb(struct msm_commit *c, struct drm_framebuffer *fb)
78{
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));
81}
82
83
Daniel Vetterb4274fb2014-11-26 17:02:18 +010084int msm_atomic_check(struct drm_device *dev,
85 struct drm_atomic_state *state)
86{
87 int ret;
88
89 /*
90 * msm ->atomic_check can update ->mode_changed for pixel format
91 * changes, hence must be run before we check the modeset changes.
92 */
93 ret = drm_atomic_helper_check_planes(dev, state);
94 if (ret)
95 return ret;
96
97 ret = drm_atomic_helper_check_modeset(dev, state);
98 if (ret)
99 return ret;
100
101 return ret;
102}
103
Rob Clarkcf3a7e42014-11-08 13:21:06 -0500104/**
105 * drm_atomic_helper_commit - commit validated state object
106 * @dev: DRM device
107 * @state: the driver state object
108 * @async: asynchronous commit
109 *
110 * This function commits a with drm_atomic_helper_check() pre-validated state
111 * object. This can still fail when e.g. the framebuffer reservation fails. For
112 * now this doesn't implement asynchronous commits.
113 *
114 * RETURNS
115 * Zero for success or -errno.
116 */
117int msm_atomic_commit(struct drm_device *dev,
118 struct drm_atomic_state *state, bool async)
119{
120 struct msm_commit *c;
121 int nplanes = dev->mode_config.num_total_plane;
122 int i, ret;
123
124 ret = drm_atomic_helper_prepare_planes(dev, state);
125 if (ret)
126 return ret;
127
128 c = new_commit(state);
129
130 /*
131 * Figure out what fence to wait for:
132 */
133 for (i = 0; i < nplanes; i++) {
134 struct drm_plane *plane = state->planes[i];
135 struct drm_plane_state *new_state = state->plane_states[i];
136
137 if (!plane)
138 continue;
139
Rob Clark3e2f29e2014-11-19 12:29:33 -0500140 if ((plane->state->fb != new_state->fb) && new_state->fb)
Rob Clarkcf3a7e42014-11-08 13:21:06 -0500141 add_fb(c, new_state->fb);
142 }
143
144 /*
145 * This is the point of no return - everything below never fails except
146 * when the hw goes bonghits. Which means we can commit the new state on
147 * the software side now.
148 */
149
150 drm_atomic_helper_swap_state(dev, state);
151
152 /*
153 * Everything below can be run asynchronously without the need to grab
154 * any modeset locks at all under one conditions: It must be guaranteed
155 * that the asynchronous work has either been cancelled (if the driver
156 * supports it, which at least requires that the framebuffers get
157 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
158 * before the new state gets committed on the software side with
159 * drm_atomic_helper_swap_state().
160 *
161 * This scheme allows new atomic state updates to be prepared and
162 * checked in parallel to the asynchronous completion of the previous
163 * update. Which is important since compositors need to figure out the
164 * composition of the next frame right after having submitted the
165 * current layout.
166 */
167
168 if (async) {
169 msm_queue_fence_cb(dev, &c->fence_cb, c->fence);
170 return 0;
171 }
172
173 ret = msm_wait_fence_interruptable(dev, c->fence, NULL);
174 if (ret) {
175 WARN_ON(ret); // TODO unswap state back? or??
176 kfree(c);
177 return ret;
178 }
179
180 complete_commit(c);
181
182 return 0;
183}