blob: 5a48aaefb97c13edff400fbbde88dde3cc6be01c [file] [log] [blame]
Rob Clarkcf3a7e42014-11-08 13:21:06 -05001/*
Dhaval Patel04c7e8e2016-09-26 20:14:31 -07002 * Copyright (c) 2016-2017, The Linux Foundation. All rights reserved.
Rob Clarkcf3a7e42014-11-08 13:21:06 -05003 * Copyright (C) 2014 Red Hat
4 * Author: Rob Clark <robdclark@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include "msm_drv.h"
20#include "msm_kms.h"
21#include "msm_gem.h"
Rob Clarkfde5de62016-03-15 15:35:08 -040022#include "msm_fence.h"
Narendra Muppalla77b32932017-05-10 13:53:11 -070023#include "sde_trace.h"
Rob Clarkcf3a7e42014-11-08 13:21:06 -050024
25struct msm_commit {
Rob Clark0b776d42015-01-30 17:04:45 -050026 struct drm_device *dev;
Rob Clarkcf3a7e42014-11-08 13:21:06 -050027 struct drm_atomic_state *state;
Rob Clarkf86afec2014-11-25 12:41:18 -050028 uint32_t crtc_mask;
Sandeep Pandaf48c46a2016-10-24 09:48:50 +053029 struct kthread_work commit_work;
Rob Clarkcf3a7e42014-11-08 13:21:06 -050030};
31
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 void commit_destroy(struct msm_commit *c)
63{
64 end_atomic(c->dev->dev_private, c->crtc_mask);
65 kfree(c);
66}
67
Lloyd Atkinsonaf7952d2016-06-26 22:41:26 -040068static void msm_atomic_wait_for_commit_done(
69 struct drm_device *dev,
Lloyd Atkinson940340d2016-12-09 14:49:37 -050070 struct drm_atomic_state *old_state)
Hai Li0a5c9aa2015-04-28 19:35:37 -040071{
72 struct drm_crtc *crtc;
Daniel Vetter8d76b792016-06-02 15:41:53 +020073 struct drm_crtc_state *crtc_state;
Hai Li0a5c9aa2015-04-28 19:35:37 -040074 struct msm_drm_private *priv = old_state->dev->dev_private;
75 struct msm_kms *kms = priv->kms;
Hai Li0a5c9aa2015-04-28 19:35:37 -040076 int i;
77
Daniel Vetter8d76b792016-06-02 15:41:53 +020078 for_each_crtc_in_state(old_state, crtc, crtc_state, i) {
Hai Li0a5c9aa2015-04-28 19:35:37 -040079 if (!crtc->state->enable)
80 continue;
81
82 /* Legacy cursor ioctls are completely unsynced, and userspace
83 * relies on that (by doing tons of cursor updates). */
84 if (old_state->legacy_cursor_update)
85 continue;
86
87 kms->funcs->wait_for_crtc_commit_done(kms, crtc);
88 }
89}
90
Lloyd Atkinsonaf7952d2016-06-26 22:41:26 -040091static void
92msm_disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state)
93{
94 struct drm_connector *connector;
95 struct drm_connector_state *old_conn_state;
96 struct drm_crtc *crtc;
97 struct drm_crtc_state *old_crtc_state;
98 int i;
99
Narendra Muppalla77b32932017-05-10 13:53:11 -0700100 SDE_ATRACE_BEGIN("msm_disable");
Lloyd Atkinsonaf7952d2016-06-26 22:41:26 -0400101 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
102 const struct drm_encoder_helper_funcs *funcs;
103 struct drm_encoder *encoder;
104 struct drm_crtc_state *old_crtc_state;
105 unsigned int crtc_idx;
106
107 /*
108 * Shut down everything that's in the changeset and currently
109 * still on. So need to check the old, saved state.
110 */
111 if (!old_conn_state->crtc)
112 continue;
113
114 crtc_idx = drm_crtc_index(old_conn_state->crtc);
Dhaval Patel04c7e8e2016-09-26 20:14:31 -0700115 old_crtc_state = drm_atomic_get_existing_crtc_state(old_state,
116 old_conn_state->crtc);
Lloyd Atkinsonaf7952d2016-06-26 22:41:26 -0400117
118 if (!old_crtc_state->active ||
119 !drm_atomic_crtc_needs_modeset(old_conn_state->crtc->state))
120 continue;
121
122 encoder = old_conn_state->best_encoder;
123
124 /* We shouldn't get this far if we didn't previously have
125 * an encoder.. but WARN_ON() rather than explode.
126 */
127 if (WARN_ON(!encoder))
128 continue;
129
130 if (msm_is_mode_seamless(
131 &connector->encoder->crtc->state->mode))
132 continue;
133
Jeykumar Sankaran69934622017-05-31 18:16:25 -0700134 if (msm_is_mode_seamless_dms(
135 &connector->encoder->crtc->state->adjusted_mode))
136 continue;
137
Lloyd Atkinsonaf7952d2016-06-26 22:41:26 -0400138 funcs = encoder->helper_private;
139
140 DRM_DEBUG_ATOMIC("disabling [ENCODER:%d:%s]\n",
141 encoder->base.id, encoder->name);
142
143 /*
144 * Each encoder has at most one connector (since we always steal
145 * it away), so we won't call disable hooks twice.
146 */
147 drm_bridge_disable(encoder->bridge);
148
149 /* Right function depends upon target state. */
150 if (connector->state->crtc && funcs->prepare)
151 funcs->prepare(encoder);
152 else if (funcs->disable)
153 funcs->disable(encoder);
154 else
155 funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
156
157 drm_bridge_post_disable(encoder->bridge);
158 }
159
160 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
161 const struct drm_crtc_helper_funcs *funcs;
162
163 /* Shut down everything that needs a full modeset. */
164 if (!drm_atomic_crtc_needs_modeset(crtc->state))
165 continue;
166
167 if (!old_crtc_state->active)
168 continue;
169
170 if (msm_is_mode_seamless(&crtc->state->mode))
171 continue;
172
Jeykumar Sankaran69934622017-05-31 18:16:25 -0700173 if (msm_is_mode_seamless_dms(&crtc->state->adjusted_mode))
174 continue;
175
Lloyd Atkinsonaf7952d2016-06-26 22:41:26 -0400176 funcs = crtc->helper_private;
177
178 DRM_DEBUG_ATOMIC("disabling [CRTC:%d]\n",
179 crtc->base.id);
180
181 /* Right function depends upon target state. */
182 if (crtc->state->enable && funcs->prepare)
183 funcs->prepare(crtc);
184 else if (funcs->disable)
185 funcs->disable(crtc);
186 else
187 funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
188 }
Narendra Muppalla77b32932017-05-10 13:53:11 -0700189 SDE_ATRACE_END("msm_disable");
Lloyd Atkinsonaf7952d2016-06-26 22:41:26 -0400190}
191
192static void
193msm_crtc_set_mode(struct drm_device *dev, struct drm_atomic_state *old_state)
194{
195 struct drm_crtc *crtc;
196 struct drm_crtc_state *old_crtc_state;
197 struct drm_connector *connector;
198 struct drm_connector_state *old_conn_state;
199 int i;
200
201 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
202 const struct drm_crtc_helper_funcs *funcs;
203
204 if (!crtc->state->mode_changed)
205 continue;
206
207 funcs = crtc->helper_private;
208
209 if (crtc->state->enable && funcs->mode_set_nofb) {
210 DRM_DEBUG_ATOMIC("modeset on [CRTC:%d]\n",
211 crtc->base.id);
212
213 funcs->mode_set_nofb(crtc);
214 }
215 }
216
217 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
218 const struct drm_encoder_helper_funcs *funcs;
219 struct drm_crtc_state *new_crtc_state;
220 struct drm_encoder *encoder;
221 struct drm_display_mode *mode, *adjusted_mode;
222
223 if (!connector->state->best_encoder)
224 continue;
225
226 encoder = connector->state->best_encoder;
227 funcs = encoder->helper_private;
228 new_crtc_state = connector->state->crtc->state;
229 mode = &new_crtc_state->mode;
230 adjusted_mode = &new_crtc_state->adjusted_mode;
231
232 if (!new_crtc_state->mode_changed)
233 continue;
234
235 DRM_DEBUG_ATOMIC("modeset on [ENCODER:%d:%s]\n",
236 encoder->base.id, encoder->name);
237
238 /*
239 * Each encoder has at most one connector (since we always steal
240 * it away), so we won't call mode_set hooks twice.
241 */
242 if (funcs->mode_set)
243 funcs->mode_set(encoder, mode, adjusted_mode);
244
245 drm_bridge_mode_set(encoder->bridge, mode, adjusted_mode);
246 }
247}
248
249/**
250 * msm_atomic_helper_commit_modeset_disables - modeset commit to disable outputs
251 * @dev: DRM device
252 * @old_state: atomic state object with old state structures
253 *
254 * This function shuts down all the outputs that need to be shut down and
255 * prepares them (if required) with the new mode.
256 *
257 * For compatibility with legacy crtc helpers this should be called before
258 * drm_atomic_helper_commit_planes(), which is what the default commit function
259 * does. But drivers with different needs can group the modeset commits together
260 * and do the plane commits at the end. This is useful for drivers doing runtime
261 * PM since planes updates then only happen when the CRTC is actually enabled.
262 */
263void msm_atomic_helper_commit_modeset_disables(struct drm_device *dev,
264 struct drm_atomic_state *old_state)
265{
266 msm_disable_outputs(dev, old_state);
267
268 drm_atomic_helper_update_legacy_modeset_state(dev, old_state);
269
Lloyd Atkinsonaf7952d2016-06-26 22:41:26 -0400270 msm_crtc_set_mode(dev, old_state);
Lloyd Atkinsonaf7952d2016-06-26 22:41:26 -0400271}
272
273/**
274 * msm_atomic_helper_commit_modeset_enables - modeset commit to enable outputs
275 * @dev: DRM device
276 * @old_state: atomic state object with old state structures
277 *
278 * This function enables all the outputs with the new configuration which had to
279 * be turned off for the update.
280 *
281 * For compatibility with legacy crtc helpers this should be called after
282 * drm_atomic_helper_commit_planes(), which is what the default commit function
283 * does. But drivers with different needs can group the modeset commits together
284 * and do the plane commits at the end. This is useful for drivers doing runtime
285 * PM since planes updates then only happen when the CRTC is actually enabled.
286 */
Lloyd Atkinson0b7ff592016-05-30 14:01:10 -0400287static void msm_atomic_helper_commit_modeset_enables(struct drm_device *dev,
Lloyd Atkinsonaf7952d2016-06-26 22:41:26 -0400288 struct drm_atomic_state *old_state)
289{
290 struct drm_crtc *crtc;
291 struct drm_crtc_state *old_crtc_state;
292 struct drm_connector *connector;
293 struct drm_connector_state *old_conn_state;
Lloyd Atkinson0b7ff592016-05-30 14:01:10 -0400294 struct msm_drm_private *priv = dev->dev_private;
295 struct msm_kms *kms = priv->kms;
296 int bridge_enable_count = 0;
Lloyd Atkinsonaf7952d2016-06-26 22:41:26 -0400297 int i;
298
Narendra Muppalla77b32932017-05-10 13:53:11 -0700299 SDE_ATRACE_BEGIN("msm_enable");
Lloyd Atkinsonaf7952d2016-06-26 22:41:26 -0400300 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
301 const struct drm_crtc_helper_funcs *funcs;
302
303 /* Need to filter out CRTCs where only planes change. */
304 if (!drm_atomic_crtc_needs_modeset(crtc->state))
305 continue;
306
307 if (!crtc->state->active)
308 continue;
309
310 if (msm_is_mode_seamless(&crtc->state->mode))
311 continue;
312
Jeykumar Sankaran69934622017-05-31 18:16:25 -0700313 /**
314 * On DMS switch, wait for ping pong done to ensure the current
315 * frame transfer is complete.
316 */
317 if (msm_is_mode_seamless_dms(&crtc->state->adjusted_mode))
318 kms->funcs->wait_for_tx_complete(kms, crtc);
319
Lloyd Atkinsonaf7952d2016-06-26 22:41:26 -0400320 funcs = crtc->helper_private;
321
322 if (crtc->state->enable) {
323 DRM_DEBUG_ATOMIC("enabling [CRTC:%d]\n",
324 crtc->base.id);
325
326 if (funcs->enable)
327 funcs->enable(crtc);
328 else
329 funcs->commit(crtc);
330 }
Lloyd Atkinsonaf7952d2016-06-26 22:41:26 -0400331
Lloyd Atkinson940340d2016-12-09 14:49:37 -0500332 if (msm_needs_vblank_pre_modeset(&crtc->state->adjusted_mode))
333 drm_crtc_wait_one_vblank(crtc);
334 }
Clarence Ipf99ccea2016-07-05 11:41:07 -0400335
Lloyd Atkinsonaf7952d2016-06-26 22:41:26 -0400336 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
337 const struct drm_encoder_helper_funcs *funcs;
338 struct drm_encoder *encoder;
339
340 if (!connector->state->best_encoder)
341 continue;
342
343 if (!connector->state->crtc->state->active ||
344 !drm_atomic_crtc_needs_modeset(
345 connector->state->crtc->state))
346 continue;
347
Lloyd Atkinsonaf7952d2016-06-26 22:41:26 -0400348 encoder = connector->state->best_encoder;
349 funcs = encoder->helper_private;
350
351 DRM_DEBUG_ATOMIC("enabling [ENCODER:%d:%s]\n",
352 encoder->base.id, encoder->name);
353
354 /*
355 * Each encoder has at most one connector (since we always steal
356 * it away), so we won't call enable hooks twice.
357 */
358 drm_bridge_pre_enable(encoder->bridge);
Lloyd Atkinson0b7ff592016-05-30 14:01:10 -0400359 ++bridge_enable_count;
Lloyd Atkinsonaf7952d2016-06-26 22:41:26 -0400360
361 if (funcs->enable)
362 funcs->enable(encoder);
363 else
364 funcs->commit(encoder);
Lloyd Atkinson0b7ff592016-05-30 14:01:10 -0400365 }
366
367 if (kms->funcs->commit) {
368 DRM_DEBUG_ATOMIC("triggering commit\n");
369 kms->funcs->commit(kms, old_state);
370 }
371
372 /* If no bridges were pre_enabled, skip iterating over them again */
Narendra Muppalla77b32932017-05-10 13:53:11 -0700373 if (bridge_enable_count == 0) {
374 SDE_ATRACE_END("msm_enable");
Lloyd Atkinson0b7ff592016-05-30 14:01:10 -0400375 return;
Narendra Muppalla77b32932017-05-10 13:53:11 -0700376 }
Lloyd Atkinson0b7ff592016-05-30 14:01:10 -0400377
378 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
379 struct drm_encoder *encoder;
380
381 if (!connector->state->best_encoder)
382 continue;
383
384 if (!connector->state->crtc->state->active ||
385 !drm_atomic_crtc_needs_modeset(
386 connector->state->crtc->state))
387 continue;
388
389 encoder = connector->state->best_encoder;
390
391 DRM_DEBUG_ATOMIC("bridge enable enabling [ENCODER:%d:%s]\n",
392 encoder->base.id, encoder->name);
Lloyd Atkinsonaf7952d2016-06-26 22:41:26 -0400393
394 drm_bridge_enable(encoder->bridge);
395 }
Narendra Muppalla77b32932017-05-10 13:53:11 -0700396 SDE_ATRACE_END("msm_enable");
Lloyd Atkinsonaf7952d2016-06-26 22:41:26 -0400397}
398
Rob Clarkcf3a7e42014-11-08 13:21:06 -0500399/* The (potentially) asynchronous part of the commit. At this point
400 * nothing can fail short of armageddon.
401 */
Sandeep Pandaf48c46a2016-10-24 09:48:50 +0530402static void complete_commit(struct msm_commit *c)
Rob Clarkcf3a7e42014-11-08 13:21:06 -0500403{
404 struct drm_atomic_state *state = c->state;
405 struct drm_device *dev = state->dev;
Rob Clark0b776d42015-01-30 17:04:45 -0500406 struct msm_drm_private *priv = dev->dev_private;
407 struct msm_kms *kms = priv->kms;
408
Gustavo Padovanf6ce4102016-09-12 16:08:11 -0300409 drm_atomic_helper_wait_for_fences(dev, state, false);
Rob Clarkba00c3f2016-03-16 18:18:17 -0400410
Rob Clark0b776d42015-01-30 17:04:45 -0500411 kms->funcs->prepare_commit(kms, state);
Rob Clarkcf3a7e42014-11-08 13:21:06 -0500412
Lloyd Atkinsonaf7952d2016-06-26 22:41:26 -0400413 msm_atomic_helper_commit_modeset_disables(dev, state);
Rob Clarkcf3a7e42014-11-08 13:21:06 -0500414
Liu Ying2b58e982016-08-29 17:12:03 +0800415 drm_atomic_helper_commit_planes(dev, state, 0);
Rob Clarkcf3a7e42014-11-08 13:21:06 -0500416
Lloyd Atkinsonaf7952d2016-06-26 22:41:26 -0400417 msm_atomic_helper_commit_modeset_enables(dev, state);
Rob Clarkcf3a7e42014-11-08 13:21:06 -0500418
Rob Clarkf86afec2014-11-25 12:41:18 -0500419 /* NOTE: _wait_for_vblanks() only waits for vblank on
420 * enabled CRTCs. So we end up faulting when disabling
421 * due to (potentially) unref'ing the outgoing fb's
422 * before the vblank when the disable has latched.
423 *
424 * But if it did wait on disabled (or newly disabled)
425 * CRTCs, that would be racy (ie. we could have missed
426 * the irq. We need some way to poll for pipe shut
427 * down. Or just live with occasionally hitting the
428 * timeout in the CRTC disable path (which really should
429 * not be critical path)
430 */
431
Lloyd Atkinson940340d2016-12-09 14:49:37 -0500432 msm_atomic_wait_for_commit_done(dev, state);
Rob Clarkcf3a7e42014-11-08 13:21:06 -0500433
434 drm_atomic_helper_cleanup_planes(dev, state);
435
Rob Clark0b776d42015-01-30 17:04:45 -0500436 kms->funcs->complete_commit(kms, state);
437
Rob Clarkcf3a7e42014-11-08 13:21:06 -0500438 drm_atomic_state_free(state);
439
Rob Clark0b776d42015-01-30 17:04:45 -0500440 commit_destroy(c);
Rob Clarkcf3a7e42014-11-08 13:21:06 -0500441}
442
Sandeep Pandaf48c46a2016-10-24 09:48:50 +0530443static void _msm_drm_commit_work_cb(struct kthread_work *work)
Rob Clarkcf3a7e42014-11-08 13:21:06 -0500444{
Sandeep Pandaf48c46a2016-10-24 09:48:50 +0530445 struct msm_commit *commit = NULL;
446
447 if (!work) {
448 DRM_ERROR("%s: Invalid commit work data!\n", __func__);
449 return;
450 }
451
452 commit = container_of(work, struct msm_commit, commit_work);
453
Narendra Muppalla77b32932017-05-10 13:53:11 -0700454 SDE_ATRACE_BEGIN("complete_commit");
Sandeep Pandaf48c46a2016-10-24 09:48:50 +0530455 complete_commit(commit);
Narendra Muppalla77b32932017-05-10 13:53:11 -0700456 SDE_ATRACE_END("complete_commit");
Sandeep Pandaf48c46a2016-10-24 09:48:50 +0530457}
458
459static struct msm_commit *commit_init(struct drm_atomic_state *state)
460{
461 struct msm_commit *c = kzalloc(sizeof(*c), GFP_KERNEL);
462
463 if (!c)
464 return NULL;
465
466 c->dev = state->dev;
467 c->state = state;
468
469 kthread_init_work(&c->commit_work, _msm_drm_commit_work_cb);
470
471 return c;
472}
473
474/* Start display thread function */
475static int msm_atomic_commit_dispatch(struct drm_device *dev,
476 struct drm_atomic_state *state, struct msm_commit *commit)
477{
478 struct msm_drm_private *priv = dev->dev_private;
479 struct drm_crtc *crtc = NULL;
480 struct drm_crtc_state *crtc_state = NULL;
481 int ret = -EINVAL, i = 0, j = 0;
482
483 for_each_crtc_in_state(state, crtc, crtc_state, i) {
484 for (j = 0; j < priv->num_crtcs; j++) {
485 if (priv->disp_thread[j].crtc_id ==
486 crtc->base.id) {
487 if (priv->disp_thread[j].thread) {
488 kthread_queue_work(
489 &priv->disp_thread[j].worker,
490 &commit->commit_work);
491 /* only return zero if work is
492 * queued successfully.
493 */
494 ret = 0;
495 } else {
496 DRM_ERROR(" Error for crtc_id: %d\n",
497 priv->disp_thread[j].crtc_id);
498 }
499 break;
500 }
501 }
502 /*
503 * TODO: handle cases where there will be more than
504 * one crtc per commit cycle. Remove this check then.
505 * Current assumption is there will be only one crtc
506 * per commit cycle.
507 */
508 if (j < priv->num_crtcs)
509 break;
510 }
511
512 return ret;
Rob Clarkcf3a7e42014-11-08 13:21:06 -0500513}
514
Rob Clarkcf3a7e42014-11-08 13:21:06 -0500515/**
516 * drm_atomic_helper_commit - commit validated state object
517 * @dev: DRM device
518 * @state: the driver state object
Maarten Lankhorsta3ccfb92016-04-26 16:11:38 +0200519 * @nonblock: nonblocking commit
Rob Clarkcf3a7e42014-11-08 13:21:06 -0500520 *
521 * This function commits a with drm_atomic_helper_check() pre-validated state
Maarten Lankhorsta3ccfb92016-04-26 16:11:38 +0200522 * object. This can still fail when e.g. the framebuffer reservation fails.
Rob Clarkcf3a7e42014-11-08 13:21:06 -0500523 *
524 * RETURNS
525 * Zero for success or -errno.
526 */
527int msm_atomic_commit(struct drm_device *dev,
Maarten Lankhorsta3ccfb92016-04-26 16:11:38 +0200528 struct drm_atomic_state *state, bool nonblock)
Rob Clarkcf3a7e42014-11-08 13:21:06 -0500529{
Rob Clarkca762a82016-03-15 17:22:13 -0400530 struct msm_drm_private *priv = dev->dev_private;
Rob Clarkf86afec2014-11-25 12:41:18 -0500531 struct msm_commit *c;
Daniel Vetter8d76b792016-06-02 15:41:53 +0200532 struct drm_crtc *crtc;
533 struct drm_crtc_state *crtc_state;
534 struct drm_plane *plane;
535 struct drm_plane_state *plane_state;
Rob Clarkcf3a7e42014-11-08 13:21:06 -0500536 int i, ret;
537
Narendra Muppalla77b32932017-05-10 13:53:11 -0700538 SDE_ATRACE_BEGIN("atomic_commit");
Rob Clarkcf3a7e42014-11-08 13:21:06 -0500539 ret = drm_atomic_helper_prepare_planes(dev, state);
Narendra Muppalla77b32932017-05-10 13:53:11 -0700540 if (ret) {
541 SDE_ATRACE_END("atomic_commit");
Rob Clarkcf3a7e42014-11-08 13:21:06 -0500542 return ret;
Narendra Muppalla77b32932017-05-10 13:53:11 -0700543 }
Rob Clarkcf3a7e42014-11-08 13:21:06 -0500544
Rob Clark0b776d42015-01-30 17:04:45 -0500545 c = commit_init(state);
Laurent Pinchartf65c18c2015-05-27 14:39:46 +0300546 if (!c) {
547 ret = -ENOMEM;
548 goto error;
549 }
Rob Clarkf86afec2014-11-25 12:41:18 -0500550
551 /*
552 * Figure out what crtcs we have:
553 */
Daniel Vetter8d76b792016-06-02 15:41:53 +0200554 for_each_crtc_in_state(state, crtc, crtc_state, i)
555 c->crtc_mask |= drm_crtc_mask(crtc);
Rob Clarkcf3a7e42014-11-08 13:21:06 -0500556
557 /*
Rob Clarkb6295f92016-03-15 18:26:28 -0400558 * Figure out what fence to wait for:
559 */
Daniel Vetter8d76b792016-06-02 15:41:53 +0200560 for_each_plane_in_state(state, plane, plane_state, i) {
561 if ((plane->state->fb != plane_state->fb) && plane_state->fb) {
562 struct drm_gem_object *obj = msm_framebuffer_bo(plane_state->fb, 0);
Rob Clarkb6295f92016-03-15 18:26:28 -0400563 struct msm_gem_object *msm_obj = to_msm_bo(obj);
564
Daniel Vetter8d76b792016-06-02 15:41:53 +0200565 plane_state->fence = reservation_object_get_excl_rcu(msm_obj->resv);
Rob Clarkb6295f92016-03-15 18:26:28 -0400566 }
567 }
568
569 /*
Rob Clarkf86afec2014-11-25 12:41:18 -0500570 * Wait for pending updates on any of the same crtc's and then
571 * mark our set of crtc's as busy:
572 */
573 ret = start_atomic(dev->dev_private, c->crtc_mask);
Laurent Pinchart5b2e2b62015-02-23 00:58:03 +0200574 if (ret) {
575 kfree(c);
Laurent Pinchartf65c18c2015-05-27 14:39:46 +0300576 goto error;
Laurent Pinchart5b2e2b62015-02-23 00:58:03 +0200577 }
Rob Clarkf86afec2014-11-25 12:41:18 -0500578
579 /*
Rob Clarkcf3a7e42014-11-08 13:21:06 -0500580 * This is the point of no return - everything below never fails except
581 * when the hw goes bonghits. Which means we can commit the new state on
582 * the software side now.
583 */
584
Daniel Vetter5e84c262016-06-10 00:06:32 +0200585 drm_atomic_helper_swap_state(state, true);
Rob Clarkcf3a7e42014-11-08 13:21:06 -0500586
587 /*
Clarence Ip24f80662016-06-13 19:05:32 -0400588 * Provide the driver a chance to prepare for output fences. This is
589 * done after the point of no return, but before asynchronous commits
590 * are dispatched to work queues, so that the fence preparation is
591 * finished before the .atomic_commit returns.
592 */
593 if (priv && priv->kms && priv->kms->funcs &&
594 priv->kms->funcs->prepare_fence)
595 priv->kms->funcs->prepare_fence(priv->kms, state);
596
597 /*
Rob Clarkcf3a7e42014-11-08 13:21:06 -0500598 * Everything below can be run asynchronously without the need to grab
599 * any modeset locks at all under one conditions: It must be guaranteed
600 * that the asynchronous work has either been cancelled (if the driver
601 * supports it, which at least requires that the framebuffers get
602 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
603 * before the new state gets committed on the software side with
604 * drm_atomic_helper_swap_state().
605 *
606 * This scheme allows new atomic state updates to be prepared and
607 * checked in parallel to the asynchronous completion of the previous
608 * update. Which is important since compositors need to figure out the
609 * composition of the next frame right after having submitted the
610 * current layout.
611 */
612
Rob Clarkba00c3f2016-03-16 18:18:17 -0400613 if (nonblock) {
Sandeep Pandaf48c46a2016-10-24 09:48:50 +0530614 ret = msm_atomic_commit_dispatch(dev, state, c);
615 if (ret) {
616 DRM_ERROR("%s: atomic commit failed\n", __func__);
617 drm_atomic_state_free(state);
618 commit_destroy(c);
619 goto error;
620 }
Narendra Muppalla77b32932017-05-10 13:53:11 -0700621 SDE_ATRACE_END("atomic_commit");
Rob Clarkcf3a7e42014-11-08 13:21:06 -0500622 return 0;
623 }
624
Sandeep Pandaf48c46a2016-10-24 09:48:50 +0530625 complete_commit(c);
Rob Clarkcf3a7e42014-11-08 13:21:06 -0500626
Narendra Muppalla77b32932017-05-10 13:53:11 -0700627 SDE_ATRACE_END("atomic_commit");
Rob Clarkcf3a7e42014-11-08 13:21:06 -0500628 return 0;
Laurent Pinchartf65c18c2015-05-27 14:39:46 +0300629
630error:
631 drm_atomic_helper_cleanup_planes(dev, state);
Narendra Muppalla77b32932017-05-10 13:53:11 -0700632 SDE_ATRACE_END("atomic_commit");
Laurent Pinchartf65c18c2015-05-27 14:39:46 +0300633 return ret;
Rob Clarkcf3a7e42014-11-08 13:21:06 -0500634}