blob: 6ac7192d0ad65fbe9583fffae57890fb5f2b7f8b [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"
Rob Clarkfde5de62016-03-15 15:35:08 -040021#include "msm_fence.h"
Rob Clarkcf3a7e42014-11-08 13:21:06 -050022
23struct msm_commit {
Rob Clark0b776d42015-01-30 17:04:45 -050024 struct drm_device *dev;
Rob Clarkcf3a7e42014-11-08 13:21:06 -050025 struct drm_atomic_state *state;
Rob Clarkba00c3f2016-03-16 18:18:17 -040026 struct work_struct work;
Rob Clarkf86afec2014-11-25 12:41:18 -050027 uint32_t crtc_mask;
Rob Clarkcf3a7e42014-11-08 13:21:06 -050028};
29
Rob Clarkba00c3f2016-03-16 18:18:17 -040030static void commit_worker(struct work_struct *work);
Rob Clarkcf3a7e42014-11-08 13:21:06 -050031
Rob Clarkf86afec2014-11-25 12:41:18 -050032/* block until specified crtcs are no longer pending update, and
33 * atomically mark them as pending update
34 */
35static int start_atomic(struct msm_drm_private *priv, uint32_t crtc_mask)
36{
37 int ret;
38
39 spin_lock(&priv->pending_crtcs_event.lock);
40 ret = wait_event_interruptible_locked(priv->pending_crtcs_event,
41 !(priv->pending_crtcs & crtc_mask));
42 if (ret == 0) {
43 DBG("start: %08x", crtc_mask);
44 priv->pending_crtcs |= crtc_mask;
45 }
46 spin_unlock(&priv->pending_crtcs_event.lock);
47
48 return ret;
49}
50
51/* clear specified crtcs (no longer pending update)
52 */
53static void end_atomic(struct msm_drm_private *priv, uint32_t crtc_mask)
54{
55 spin_lock(&priv->pending_crtcs_event.lock);
56 DBG("end: %08x", crtc_mask);
57 priv->pending_crtcs &= ~crtc_mask;
58 wake_up_all_locked(&priv->pending_crtcs_event);
59 spin_unlock(&priv->pending_crtcs_event.lock);
60}
61
Rob Clark0b776d42015-01-30 17:04:45 -050062static struct msm_commit *commit_init(struct drm_atomic_state *state)
Rob Clarkcf3a7e42014-11-08 13:21:06 -050063{
64 struct msm_commit *c = kzalloc(sizeof(*c), GFP_KERNEL);
65
66 if (!c)
67 return NULL;
68
Rob Clark0b776d42015-01-30 17:04:45 -050069 c->dev = state->dev;
Rob Clarkcf3a7e42014-11-08 13:21:06 -050070 c->state = state;
Rob Clark0b776d42015-01-30 17:04:45 -050071
Rob Clarkba00c3f2016-03-16 18:18:17 -040072 INIT_WORK(&c->work, commit_worker);
Rob Clarkcf3a7e42014-11-08 13:21:06 -050073
74 return c;
75}
76
Rob Clark0b776d42015-01-30 17:04:45 -050077static void commit_destroy(struct msm_commit *c)
78{
79 end_atomic(c->dev->dev_private, c->crtc_mask);
80 kfree(c);
81}
82
Hai Li0a5c9aa2015-04-28 19:35:37 -040083static void msm_atomic_wait_for_commit_done(struct drm_device *dev,
84 struct drm_atomic_state *old_state)
85{
86 struct drm_crtc *crtc;
87 struct msm_drm_private *priv = old_state->dev->dev_private;
88 struct msm_kms *kms = priv->kms;
89 int ncrtcs = old_state->dev->mode_config.num_crtc;
90 int i;
91
92 for (i = 0; i < ncrtcs; i++) {
93 crtc = old_state->crtcs[i];
94
95 if (!crtc)
96 continue;
97
98 if (!crtc->state->enable)
99 continue;
100
101 /* Legacy cursor ioctls are completely unsynced, and userspace
102 * relies on that (by doing tons of cursor updates). */
103 if (old_state->legacy_cursor_update)
104 continue;
105
106 kms->funcs->wait_for_crtc_commit_done(kms, crtc);
107 }
108}
109
Rob Clarkba00c3f2016-03-16 18:18:17 -0400110static void wait_fences(struct msm_commit *c, bool async)
111{
112 int nplanes = c->dev->mode_config.num_total_plane;
113 ktime_t timeout = ktime_add_ms(ktime_get(), 1000);
114 int i;
115
116 for (i = 0; i < nplanes; i++) {
117 struct drm_plane *plane = c->state->planes[i];
118 struct drm_plane_state *new_state = c->state->plane_states[i];
119
120 if (!plane)
121 continue;
122
123 if ((plane->state->fb != new_state->fb) && new_state->fb) {
124 struct drm_gem_object *obj =
125 msm_framebuffer_bo(new_state->fb, 0);
126 msm_gem_cpu_sync(obj, MSM_PREP_READ, &timeout);
127 }
128 }
129}
130
Rob Clarkcf3a7e42014-11-08 13:21:06 -0500131/* The (potentially) asynchronous part of the commit. At this point
132 * nothing can fail short of armageddon.
133 */
Rob Clarkba00c3f2016-03-16 18:18:17 -0400134static void complete_commit(struct msm_commit *c, bool async)
Rob Clarkcf3a7e42014-11-08 13:21:06 -0500135{
136 struct drm_atomic_state *state = c->state;
137 struct drm_device *dev = state->dev;
Rob Clark0b776d42015-01-30 17:04:45 -0500138 struct msm_drm_private *priv = dev->dev_private;
139 struct msm_kms *kms = priv->kms;
140
Rob Clarkba00c3f2016-03-16 18:18:17 -0400141 wait_fences(c, async);
142
Rob Clark0b776d42015-01-30 17:04:45 -0500143 kms->funcs->prepare_commit(kms, state);
Rob Clarkcf3a7e42014-11-08 13:21:06 -0500144
Daniel Vetter1af434a2015-02-22 12:24:19 +0100145 drm_atomic_helper_commit_modeset_disables(dev, state);
Rob Clarkcf3a7e42014-11-08 13:21:06 -0500146
Daniel Vetteraef9dbb2015-09-08 12:02:07 +0200147 drm_atomic_helper_commit_planes(dev, state, false);
Rob Clarkcf3a7e42014-11-08 13:21:06 -0500148
Daniel Vetter1af434a2015-02-22 12:24:19 +0100149 drm_atomic_helper_commit_modeset_enables(dev, state);
Rob Clarkcf3a7e42014-11-08 13:21:06 -0500150
Rob Clarkf86afec2014-11-25 12:41:18 -0500151 /* NOTE: _wait_for_vblanks() only waits for vblank on
152 * enabled CRTCs. So we end up faulting when disabling
153 * due to (potentially) unref'ing the outgoing fb's
154 * before the vblank when the disable has latched.
155 *
156 * But if it did wait on disabled (or newly disabled)
157 * CRTCs, that would be racy (ie. we could have missed
158 * the irq. We need some way to poll for pipe shut
159 * down. Or just live with occasionally hitting the
160 * timeout in the CRTC disable path (which really should
161 * not be critical path)
162 */
163
Hai Li0a5c9aa2015-04-28 19:35:37 -0400164 msm_atomic_wait_for_commit_done(dev, state);
Rob Clarkcf3a7e42014-11-08 13:21:06 -0500165
166 drm_atomic_helper_cleanup_planes(dev, state);
167
Rob Clark0b776d42015-01-30 17:04:45 -0500168 kms->funcs->complete_commit(kms, state);
169
Rob Clarkcf3a7e42014-11-08 13:21:06 -0500170 drm_atomic_state_free(state);
171
Rob Clark0b776d42015-01-30 17:04:45 -0500172 commit_destroy(c);
Rob Clarkcf3a7e42014-11-08 13:21:06 -0500173}
174
Rob Clarkba00c3f2016-03-16 18:18:17 -0400175static void commit_worker(struct work_struct *work)
Rob Clarkcf3a7e42014-11-08 13:21:06 -0500176{
Rob Clarkba00c3f2016-03-16 18:18:17 -0400177 complete_commit(container_of(work, struct msm_commit, work), true);
Rob Clarkcf3a7e42014-11-08 13:21:06 -0500178}
179
Daniel Vetterb4274fb2014-11-26 17:02:18 +0100180int msm_atomic_check(struct drm_device *dev,
181 struct drm_atomic_state *state)
182{
183 int ret;
184
185 /*
186 * msm ->atomic_check can update ->mode_changed for pixel format
187 * changes, hence must be run before we check the modeset changes.
188 */
189 ret = drm_atomic_helper_check_planes(dev, state);
190 if (ret)
191 return ret;
192
193 ret = drm_atomic_helper_check_modeset(dev, state);
194 if (ret)
195 return ret;
196
197 return ret;
198}
199
Rob Clarkcf3a7e42014-11-08 13:21:06 -0500200/**
201 * drm_atomic_helper_commit - commit validated state object
202 * @dev: DRM device
203 * @state: the driver state object
Maarten Lankhorsta3ccfb92016-04-26 16:11:38 +0200204 * @nonblock: nonblocking commit
Rob Clarkcf3a7e42014-11-08 13:21:06 -0500205 *
206 * This function commits a with drm_atomic_helper_check() pre-validated state
Maarten Lankhorsta3ccfb92016-04-26 16:11:38 +0200207 * object. This can still fail when e.g. the framebuffer reservation fails.
Rob Clarkcf3a7e42014-11-08 13:21:06 -0500208 *
209 * RETURNS
210 * Zero for success or -errno.
211 */
212int msm_atomic_commit(struct drm_device *dev,
Maarten Lankhorsta3ccfb92016-04-26 16:11:38 +0200213 struct drm_atomic_state *state, bool nonblock)
Rob Clarkcf3a7e42014-11-08 13:21:06 -0500214{
Rob Clarkca762a82016-03-15 17:22:13 -0400215 struct msm_drm_private *priv = dev->dev_private;
Rob Clarkf86afec2014-11-25 12:41:18 -0500216 int ncrtcs = dev->mode_config.num_crtc;
217 struct msm_commit *c;
Rob Clarkcf3a7e42014-11-08 13:21:06 -0500218 int i, ret;
219
220 ret = drm_atomic_helper_prepare_planes(dev, state);
221 if (ret)
222 return ret;
223
Rob Clark0b776d42015-01-30 17:04:45 -0500224 c = commit_init(state);
Laurent Pinchartf65c18c2015-05-27 14:39:46 +0300225 if (!c) {
226 ret = -ENOMEM;
227 goto error;
228 }
Rob Clarkf86afec2014-11-25 12:41:18 -0500229
230 /*
231 * Figure out what crtcs we have:
232 */
233 for (i = 0; i < ncrtcs; i++) {
234 struct drm_crtc *crtc = state->crtcs[i];
235 if (!crtc)
236 continue;
237 c->crtc_mask |= (1 << drm_crtc_index(crtc));
238 }
Rob Clarkcf3a7e42014-11-08 13:21:06 -0500239
240 /*
Rob Clarkf86afec2014-11-25 12:41:18 -0500241 * Wait for pending updates on any of the same crtc's and then
242 * mark our set of crtc's as busy:
243 */
244 ret = start_atomic(dev->dev_private, c->crtc_mask);
Laurent Pinchart5b2e2b62015-02-23 00:58:03 +0200245 if (ret) {
246 kfree(c);
Laurent Pinchartf65c18c2015-05-27 14:39:46 +0300247 goto error;
Laurent Pinchart5b2e2b62015-02-23 00:58:03 +0200248 }
Rob Clarkf86afec2014-11-25 12:41:18 -0500249
250 /*
Rob Clarkcf3a7e42014-11-08 13:21:06 -0500251 * This is the point of no return - everything below never fails except
252 * when the hw goes bonghits. Which means we can commit the new state on
253 * the software side now.
254 */
255
256 drm_atomic_helper_swap_state(dev, state);
257
258 /*
259 * Everything below can be run asynchronously without the need to grab
260 * any modeset locks at all under one conditions: It must be guaranteed
261 * that the asynchronous work has either been cancelled (if the driver
262 * supports it, which at least requires that the framebuffers get
263 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
264 * before the new state gets committed on the software side with
265 * drm_atomic_helper_swap_state().
266 *
267 * This scheme allows new atomic state updates to be prepared and
268 * checked in parallel to the asynchronous completion of the previous
269 * update. Which is important since compositors need to figure out the
270 * composition of the next frame right after having submitted the
271 * current layout.
272 */
273
Rob Clarkba00c3f2016-03-16 18:18:17 -0400274 if (nonblock) {
275 queue_work(priv->atomic_wq, &c->work);
Rob Clarkcf3a7e42014-11-08 13:21:06 -0500276 return 0;
277 }
278
Rob Clarkba00c3f2016-03-16 18:18:17 -0400279 complete_commit(c, false);
Rob Clarkcf3a7e42014-11-08 13:21:06 -0500280
281 return 0;
Laurent Pinchartf65c18c2015-05-27 14:39:46 +0300282
283error:
284 drm_atomic_helper_cleanup_planes(dev, state);
285 return ret;
Rob Clarkcf3a7e42014-11-08 13:21:06 -0500286}