]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/gpu/drm/drm_atomic_helper.c
drm: Convert connector_helper_funcs->atomic_check to accept drm_atomic_state
[mirror_ubuntu-jammy-kernel.git] / drivers / gpu / drm / drm_atomic_helper.c
CommitLineData
c2fcd274
DV
1/*
2 * Copyright (C) 2014 Red Hat
3 * Copyright (C) 2014 Intel Corp.
4 *
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:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
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.
22 *
23 * Authors:
24 * Rob Clark <robdclark@gmail.com>
25 * Daniel Vetter <daniel.vetter@ffwll.ch>
26 */
27
0500c04e
SR
28#include <linux/dma-fence.h>
29
c2fcd274 30#include <drm/drm_atomic.h>
0500c04e 31#include <drm/drm_atomic_helper.h>
72fdb40c 32#include <drm/drm_atomic_uapi.h>
0500c04e
SR
33#include <drm/drm_damage_helper.h>
34#include <drm/drm_device.h>
c2fcd274 35#include <drm/drm_plane_helper.h>
0500c04e
SR
36#include <drm/drm_print.h>
37#include <drm/drm_vblank.h>
935774cd 38#include <drm/drm_writeback.h>
c2fcd274 39
faf94a08 40#include "drm_crtc_helper_internal.h"
44d1240d
MS
41#include "drm_crtc_internal.h"
42
3150c7d0
DV
43/**
44 * DOC: overview
45 *
46 * This helper library provides implementations of check and commit functions on
47 * top of the CRTC modeset helper callbacks and the plane helper callbacks. It
48 * also provides convenience implementations for the atomic state handling
49 * callbacks for drivers which don't need to subclass the drm core structures to
50 * add their own additional internal state.
51 *
52 * This library also provides default implementations for the check callback in
26196f7e
DV
53 * drm_atomic_helper_check() and for the commit callback with
54 * drm_atomic_helper_commit(). But the individual stages and callbacks are
55 * exposed to allow drivers to mix and match and e.g. use the plane helpers only
3150c7d0
DV
56 * together with a driver private modeset implementation.
57 *
58 * This library also provides implementations for all the legacy driver
26196f7e
DV
59 * interfaces on top of the atomic interface. See drm_atomic_helper_set_config(),
60 * drm_atomic_helper_disable_plane(), drm_atomic_helper_disable_plane() and the
3150c7d0
DV
61 * various functions to implement set_property callbacks. New drivers must not
62 * implement these functions themselves but must use the provided helpers.
092d01da
DV
63 *
64 * The atomic helper uses the same function table structures as all other
ea0dd85a
DV
65 * modesetting helpers. See the documentation for &struct drm_crtc_helper_funcs,
66 * struct &drm_encoder_helper_funcs and &struct drm_connector_helper_funcs. It
67 * also shares the &struct drm_plane_helper_funcs function table with the plane
092d01da 68 * helpers.
3150c7d0 69 */
c2fcd274
DV
70static void
71drm_atomic_helper_plane_changed(struct drm_atomic_state *state,
415c3ac3 72 struct drm_plane_state *old_plane_state,
c2fcd274
DV
73 struct drm_plane_state *plane_state,
74 struct drm_plane *plane)
75{
76 struct drm_crtc_state *crtc_state;
77
415c3ac3 78 if (old_plane_state->crtc) {
b4d93679
ML
79 crtc_state = drm_atomic_get_new_crtc_state(state,
80 old_plane_state->crtc);
c2fcd274
DV
81
82 if (WARN_ON(!crtc_state))
83 return;
84
85 crtc_state->planes_changed = true;
86 }
87
88 if (plane_state->crtc) {
b4d93679 89 crtc_state = drm_atomic_get_new_crtc_state(state, plane_state->crtc);
c2fcd274
DV
90
91 if (WARN_ON(!crtc_state))
92 return;
93
94 crtc_state->planes_changed = true;
95 }
96}
97
297e30b5
DV
98/*
99 * For connectors that support multiple encoders, either the
100 * .atomic_best_encoder() or .best_encoder() operation must be implemented.
101 */
102static struct drm_encoder *
103pick_single_encoder_for_connector(struct drm_connector *connector)
104{
105 WARN_ON(connector->encoder_ids[1]);
106 return drm_encoder_find(connector->dev, NULL, connector->encoder_ids[0]);
107}
108
8248b65d
ML
109static int handle_conflicting_encoders(struct drm_atomic_state *state,
110 bool disable_conflicting_encoders)
97ac3204 111{
415c3ac3 112 struct drm_connector_state *new_conn_state;
40616a26 113 struct drm_connector *connector;
c36a3254 114 struct drm_connector_list_iter conn_iter;
40616a26
ML
115 struct drm_encoder *encoder;
116 unsigned encoder_mask = 0;
c36a3254 117 int i, ret = 0;
97ac3204 118
8248b65d
ML
119 /*
120 * First loop, find all newly assigned encoders from the connectors
121 * part of the state. If the same encoder is assigned to multiple
122 * connectors bail out.
123 */
415c3ac3 124 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
40616a26
ML
125 const struct drm_connector_helper_funcs *funcs = connector->helper_private;
126 struct drm_encoder *new_encoder;
127
415c3ac3 128 if (!new_conn_state->crtc)
97ac3204
DV
129 continue;
130
40616a26 131 if (funcs->atomic_best_encoder)
415c3ac3 132 new_encoder = funcs->atomic_best_encoder(connector, new_conn_state);
a0909cc5 133 else if (funcs->best_encoder)
40616a26 134 new_encoder = funcs->best_encoder(connector);
a0909cc5 135 else
297e30b5 136 new_encoder = pick_single_encoder_for_connector(connector);
40616a26 137
8248b65d 138 if (new_encoder) {
6f3be036 139 if (encoder_mask & drm_encoder_mask(new_encoder)) {
8248b65d
ML
140 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] on [CONNECTOR:%d:%s] already assigned\n",
141 new_encoder->base.id, new_encoder->name,
142 connector->base.id, connector->name);
143
144 return -EINVAL;
145 }
146
6f3be036 147 encoder_mask |= drm_encoder_mask(new_encoder);
8248b65d 148 }
97ac3204
DV
149 }
150
8248b65d
ML
151 if (!encoder_mask)
152 return 0;
97ac3204 153
8248b65d
ML
154 /*
155 * Second loop, iterate over all connectors not part of the state.
156 *
157 * If a conflicting encoder is found and disable_conflicting_encoders
158 * is not set, an error is returned. Userspace can provide a solution
159 * through the atomic ioctl.
160 *
161 * If the flag is set conflicting connectors are removed from the crtc
162 * and the crtc is disabled if no encoder is left. This preserves
163 * compatibility with the legacy set_config behavior.
164 */
b982dab1 165 drm_connector_list_iter_begin(state->dev, &conn_iter);
c36a3254 166 drm_for_each_connector_iter(connector, &conn_iter) {
40616a26 167 struct drm_crtc_state *crtc_state;
623369e5 168
b4d93679 169 if (drm_atomic_get_new_connector_state(state, connector))
40616a26 170 continue;
623369e5 171
40616a26 172 encoder = connector->state->best_encoder;
6f3be036 173 if (!encoder || !(encoder_mask & drm_encoder_mask(encoder)))
623369e5
DV
174 continue;
175
8248b65d
ML
176 if (!disable_conflicting_encoders) {
177 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s] by [CONNECTOR:%d:%s]\n",
178 encoder->base.id, encoder->name,
179 connector->state->crtc->base.id,
180 connector->state->crtc->name,
181 connector->base.id, connector->name);
c36a3254
DV
182 ret = -EINVAL;
183 goto out;
8248b65d
ML
184 }
185
415c3ac3
ML
186 new_conn_state = drm_atomic_get_connector_state(state, connector);
187 if (IS_ERR(new_conn_state)) {
188 ret = PTR_ERR(new_conn_state);
c36a3254
DV
189 goto out;
190 }
40616a26
ML
191
192 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s], disabling [CONNECTOR:%d:%s]\n",
193 encoder->base.id, encoder->name,
415c3ac3 194 new_conn_state->crtc->base.id, new_conn_state->crtc->name,
40616a26
ML
195 connector->base.id, connector->name);
196
b4d93679 197 crtc_state = drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
40616a26 198
415c3ac3 199 ret = drm_atomic_set_crtc_for_connector(new_conn_state, NULL);
40616a26 200 if (ret)
c36a3254 201 goto out;
40616a26
ML
202
203 if (!crtc_state->connector_mask) {
204 ret = drm_atomic_set_mode_prop_for_crtc(crtc_state,
205 NULL);
206 if (ret < 0)
c36a3254 207 goto out;
40616a26
ML
208
209 crtc_state->active = false;
210 }
623369e5 211 }
c36a3254 212out:
b982dab1 213 drm_connector_list_iter_end(&conn_iter);
623369e5 214
c36a3254 215 return ret;
623369e5
DV
216}
217
e87a52b3
ML
218static void
219set_best_encoder(struct drm_atomic_state *state,
220 struct drm_connector_state *conn_state,
221 struct drm_encoder *encoder)
222{
223 struct drm_crtc_state *crtc_state;
224 struct drm_crtc *crtc;
225
226 if (conn_state->best_encoder) {
227 /* Unset the encoder_mask in the old crtc state. */
228 crtc = conn_state->connector->state->crtc;
229
230 /* A NULL crtc is an error here because we should have
231 * duplicated a NULL best_encoder when crtc was NULL.
232 * As an exception restoring duplicated atomic state
233 * during resume is allowed, so don't warn when
234 * best_encoder is equal to encoder we intend to set.
235 */
236 WARN_ON(!crtc && encoder != conn_state->best_encoder);
237 if (crtc) {
b4d93679 238 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
e87a52b3
ML
239
240 crtc_state->encoder_mask &=
6f3be036 241 ~drm_encoder_mask(conn_state->best_encoder);
e87a52b3
ML
242 }
243 }
244
245 if (encoder) {
246 crtc = conn_state->crtc;
247 WARN_ON(!crtc);
248 if (crtc) {
b4d93679 249 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
e87a52b3
ML
250
251 crtc_state->encoder_mask |=
6f3be036 252 drm_encoder_mask(encoder);
e87a52b3
ML
253 }
254 }
255
256 conn_state->best_encoder = encoder;
257}
258
ec5aaa58 259static void
623369e5 260steal_encoder(struct drm_atomic_state *state,
ff19b786 261 struct drm_encoder *encoder)
623369e5 262{
623369e5
DV
263 struct drm_crtc_state *crtc_state;
264 struct drm_connector *connector;
415c3ac3 265 struct drm_connector_state *old_connector_state, *new_connector_state;
ec5aaa58 266 int i;
623369e5 267
415c3ac3 268 for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
ff19b786 269 struct drm_crtc *encoder_crtc;
623369e5 270
415c3ac3 271 if (new_connector_state->best_encoder != encoder)
623369e5
DV
272 continue;
273
415c3ac3 274 encoder_crtc = old_connector_state->crtc;
623369e5 275
ff19b786
ML
276 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s], stealing it\n",
277 encoder->base.id, encoder->name,
278 encoder_crtc->base.id, encoder_crtc->name);
e87a52b3 279
415c3ac3 280 set_best_encoder(state, new_connector_state, NULL);
623369e5 281
b4d93679 282 crtc_state = drm_atomic_get_new_crtc_state(state, encoder_crtc);
ff19b786
ML
283 crtc_state->connectors_changed = true;
284
ec5aaa58 285 return;
623369e5 286 }
623369e5
DV
287}
288
289static int
9459545b
ML
290update_connector_routing(struct drm_atomic_state *state,
291 struct drm_connector *connector,
415c3ac3
ML
292 struct drm_connector_state *old_connector_state,
293 struct drm_connector_state *new_connector_state)
623369e5 294{
b5ceff20 295 const struct drm_connector_helper_funcs *funcs;
623369e5 296 struct drm_encoder *new_encoder;
623369e5 297 struct drm_crtc_state *crtc_state;
623369e5 298
17a38d9c
DV
299 DRM_DEBUG_ATOMIC("Updating routing for [CONNECTOR:%d:%s]\n",
300 connector->base.id,
301 connector->name);
623369e5 302
415c3ac3
ML
303 if (old_connector_state->crtc != new_connector_state->crtc) {
304 if (old_connector_state->crtc) {
b4d93679 305 crtc_state = drm_atomic_get_new_crtc_state(state, old_connector_state->crtc);
fc596660 306 crtc_state->connectors_changed = true;
623369e5
DV
307 }
308
415c3ac3 309 if (new_connector_state->crtc) {
b4d93679 310 crtc_state = drm_atomic_get_new_crtc_state(state, new_connector_state->crtc);
fc596660 311 crtc_state->connectors_changed = true;
623369e5
DV
312 }
313 }
314
415c3ac3 315 if (!new_connector_state->crtc) {
17a38d9c 316 DRM_DEBUG_ATOMIC("Disabling [CONNECTOR:%d:%s]\n",
623369e5
DV
317 connector->base.id,
318 connector->name);
319
415c3ac3 320 set_best_encoder(state, new_connector_state, NULL);
623369e5
DV
321
322 return 0;
323 }
324
de9f8eea
LP
325 crtc_state = drm_atomic_get_new_crtc_state(state,
326 new_connector_state->crtc);
327 /*
328 * For compatibility with legacy users, we want to make sure that
329 * we allow DPMS On->Off modesets on unregistered connectors. Modesets
330 * which would result in anything else must be considered invalid, to
331 * avoid turning on new displays on dead connectors.
332 *
333 * Since the connector can be unregistered at any point during an
334 * atomic check or commit, this is racy. But that's OK: all we care
335 * about is ensuring that userspace can't do anything but shut off the
1e55a53a 336 * display on a connector that was destroyed after it's been notified,
de9f8eea 337 * not before.
022debad
LP
338 *
339 * Additionally, we also want to ignore connector registration when
340 * we're trying to restore an atomic state during system resume since
341 * there's a chance the connector may have been destroyed during the
342 * process, but it's better to ignore that then cause
343 * drm_atomic_helper_resume() to fail.
de9f8eea 344 */
022debad
LP
345 if (!state->duplicated && drm_connector_is_unregistered(connector) &&
346 crtc_state->active) {
de9f8eea
LP
347 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] is not registered\n",
348 connector->base.id, connector->name);
349 return -EINVAL;
350 }
351
623369e5 352 funcs = connector->helper_private;
3b8a684b
DV
353
354 if (funcs->atomic_best_encoder)
355 new_encoder = funcs->atomic_best_encoder(connector,
415c3ac3 356 new_connector_state);
c61b93fe 357 else if (funcs->best_encoder)
3b8a684b 358 new_encoder = funcs->best_encoder(connector);
c61b93fe 359 else
297e30b5 360 new_encoder = pick_single_encoder_for_connector(connector);
623369e5
DV
361
362 if (!new_encoder) {
17a38d9c
DV
363 DRM_DEBUG_ATOMIC("No suitable encoder found for [CONNECTOR:%d:%s]\n",
364 connector->base.id,
365 connector->name);
623369e5
DV
366 return -EINVAL;
367 }
368
415c3ac3 369 if (!drm_encoder_crtc_ok(new_encoder, new_connector_state->crtc)) {
6ac7c548 370 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] incompatible with [CRTC:%d:%s]\n",
5481c8fb
DV
371 new_encoder->base.id,
372 new_encoder->name,
415c3ac3
ML
373 new_connector_state->crtc->base.id,
374 new_connector_state->crtc->name);
5481c8fb
DV
375 return -EINVAL;
376 }
377
415c3ac3
ML
378 if (new_encoder == new_connector_state->best_encoder) {
379 set_best_encoder(state, new_connector_state, new_encoder);
e87a52b3 380
fa3ab4c2 381 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] keeps [ENCODER:%d:%s], now on [CRTC:%d:%s]\n",
17a38d9c
DV
382 connector->base.id,
383 connector->name,
384 new_encoder->base.id,
385 new_encoder->name,
415c3ac3
ML
386 new_connector_state->crtc->base.id,
387 new_connector_state->crtc->name);
623369e5
DV
388
389 return 0;
390 }
391
ec5aaa58 392 steal_encoder(state, new_encoder);
6ea76f3c 393
415c3ac3 394 set_best_encoder(state, new_connector_state, new_encoder);
e87a52b3 395
fc596660 396 crtc_state->connectors_changed = true;
623369e5 397
fa3ab4c2 398 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] using [ENCODER:%d:%s] on [CRTC:%d:%s]\n",
17a38d9c
DV
399 connector->base.id,
400 connector->name,
401 new_encoder->base.id,
402 new_encoder->name,
415c3ac3
ML
403 new_connector_state->crtc->base.id,
404 new_connector_state->crtc->name);
623369e5
DV
405
406 return 0;
407}
408
409static int
410mode_fixup(struct drm_atomic_state *state)
411{
df63b999 412 struct drm_crtc *crtc;
415c3ac3 413 struct drm_crtc_state *new_crtc_state;
df63b999 414 struct drm_connector *connector;
415c3ac3 415 struct drm_connector_state *new_conn_state;
623369e5 416 int i;
f9ad86e4 417 int ret;
623369e5 418
415c3ac3
ML
419 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
420 if (!new_crtc_state->mode_changed &&
421 !new_crtc_state->connectors_changed)
623369e5
DV
422 continue;
423
415c3ac3 424 drm_mode_copy(&new_crtc_state->adjusted_mode, &new_crtc_state->mode);
623369e5
DV
425 }
426
415c3ac3 427 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
b5ceff20 428 const struct drm_encoder_helper_funcs *funcs;
623369e5
DV
429 struct drm_encoder *encoder;
430
415c3ac3 431 WARN_ON(!!new_conn_state->best_encoder != !!new_conn_state->crtc);
623369e5 432
415c3ac3 433 if (!new_conn_state->crtc || !new_conn_state->best_encoder)
623369e5
DV
434 continue;
435
415c3ac3 436 new_crtc_state =
b4d93679 437 drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
623369e5
DV
438
439 /*
440 * Each encoder has at most one connector (since we always steal
441 * it away), so we won't call ->mode_fixup twice.
442 */
415c3ac3 443 encoder = new_conn_state->best_encoder;
623369e5
DV
444 funcs = encoder->helper_private;
445
415c3ac3
ML
446 ret = drm_bridge_mode_fixup(encoder->bridge, &new_crtc_state->mode,
447 &new_crtc_state->adjusted_mode);
862e686c
AT
448 if (!ret) {
449 DRM_DEBUG_ATOMIC("Bridge fixup failed\n");
450 return -EINVAL;
623369e5
DV
451 }
452
2827635e 453 if (funcs && funcs->atomic_check) {
415c3ac3
ML
454 ret = funcs->atomic_check(encoder, new_crtc_state,
455 new_conn_state);
4cd4df80 456 if (ret) {
17a38d9c
DV
457 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] check failed\n",
458 encoder->base.id, encoder->name);
4cd4df80
TR
459 return ret;
460 }
2827635e 461 } else if (funcs && funcs->mode_fixup) {
415c3ac3
ML
462 ret = funcs->mode_fixup(encoder, &new_crtc_state->mode,
463 &new_crtc_state->adjusted_mode);
4cd4df80 464 if (!ret) {
17a38d9c
DV
465 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] fixup failed\n",
466 encoder->base.id, encoder->name);
4cd4df80
TR
467 return -EINVAL;
468 }
623369e5
DV
469 }
470 }
471
415c3ac3 472 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
b5ceff20 473 const struct drm_crtc_helper_funcs *funcs;
623369e5 474
415c3ac3 475 if (!new_crtc_state->enable)
f55f1701
LY
476 continue;
477
415c3ac3
ML
478 if (!new_crtc_state->mode_changed &&
479 !new_crtc_state->connectors_changed)
623369e5
DV
480 continue;
481
482 funcs = crtc->helper_private;
840bfe95
ACO
483 if (!funcs->mode_fixup)
484 continue;
485
415c3ac3
ML
486 ret = funcs->mode_fixup(crtc, &new_crtc_state->mode,
487 &new_crtc_state->adjusted_mode);
623369e5 488 if (!ret) {
fa3ab4c2
VS
489 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] fixup failed\n",
490 crtc->base.id, crtc->name);
623369e5
DV
491 return -EINVAL;
492 }
493 }
494
495 return 0;
496}
497
faf94a08
JA
498static enum drm_mode_status mode_valid_path(struct drm_connector *connector,
499 struct drm_encoder *encoder,
500 struct drm_crtc *crtc,
8518f05a 501 const struct drm_display_mode *mode)
faf94a08
JA
502{
503 enum drm_mode_status ret;
504
505 ret = drm_encoder_mode_valid(encoder, mode);
506 if (ret != MODE_OK) {
507 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] mode_valid() failed\n",
508 encoder->base.id, encoder->name);
509 return ret;
510 }
511
512 ret = drm_bridge_mode_valid(encoder->bridge, mode);
513 if (ret != MODE_OK) {
514 DRM_DEBUG_ATOMIC("[BRIDGE] mode_valid() failed\n");
515 return ret;
516 }
517
518 ret = drm_crtc_mode_valid(crtc, mode);
519 if (ret != MODE_OK) {
520 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] mode_valid() failed\n",
521 crtc->base.id, crtc->name);
522 return ret;
523 }
524
525 return ret;
526}
527
528static int
529mode_valid(struct drm_atomic_state *state)
530{
531 struct drm_connector_state *conn_state;
532 struct drm_connector *connector;
533 int i;
534
535 for_each_new_connector_in_state(state, connector, conn_state, i) {
536 struct drm_encoder *encoder = conn_state->best_encoder;
537 struct drm_crtc *crtc = conn_state->crtc;
538 struct drm_crtc_state *crtc_state;
539 enum drm_mode_status mode_status;
8518f05a 540 const struct drm_display_mode *mode;
faf94a08
JA
541
542 if (!crtc || !encoder)
543 continue;
544
545 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
546 if (!crtc_state)
547 continue;
548 if (!crtc_state->mode_changed && !crtc_state->connectors_changed)
549 continue;
550
551 mode = &crtc_state->mode;
552
553 mode_status = mode_valid_path(connector, encoder, crtc, mode);
554 if (mode_status != MODE_OK)
555 return -EINVAL;
556 }
557
558 return 0;
559}
560
d9b13620 561/**
f98bd3ef 562 * drm_atomic_helper_check_modeset - validate state object for modeset changes
d9b13620
DV
563 * @dev: DRM device
564 * @state: the driver state object
565 *
566 * Check the state object to see if the requested state is physically possible.
567 * This does all the crtc and connector related computations for an atomic
ce09d766
ML
568 * update and adds any additional connectors needed for full modesets. It calls
569 * the various per-object callbacks in the follow order:
570 *
571 * 1. &drm_connector_helper_funcs.atomic_best_encoder for determining the new encoder.
572 * 2. &drm_connector_helper_funcs.atomic_check to validate the connector state.
573 * 3. If it's determined a modeset is needed then all connectors on the affected crtc
574 * crtc are added and &drm_connector_helper_funcs.atomic_check is run on them.
faf94a08
JA
575 * 4. &drm_encoder_helper_funcs.mode_valid, &drm_bridge_funcs.mode_valid and
576 * &drm_crtc_helper_funcs.mode_valid are called on the affected components.
577 * 5. &drm_bridge_funcs.mode_fixup is called on all encoder bridges.
578 * 6. &drm_encoder_helper_funcs.atomic_check is called to validate any encoder state.
ce09d766
ML
579 * This function is only called when the encoder will be part of a configured crtc,
580 * it must not be used for implementing connector property validation.
581 * If this function is NULL, &drm_atomic_encoder_helper_funcs.mode_fixup is called
582 * instead.
faf94a08 583 * 7. &drm_crtc_helper_funcs.mode_fixup is called last, to fix up the mode with crtc constraints.
6806cdf9
DV
584 *
585 * &drm_crtc_state.mode_changed is set when the input mode is changed.
586 * &drm_crtc_state.connectors_changed is set when a connector is added or
587 * removed from the crtc. &drm_crtc_state.active_changed is set when
588 * &drm_crtc_state.active changes, which is used for DPMS.
d807ed1c 589 * See also: drm_atomic_crtc_needs_modeset()
d9b13620
DV
590 *
591 * IMPORTANT:
592 *
6806cdf9
DV
593 * Drivers which set &drm_crtc_state.mode_changed (e.g. in their
594 * &drm_plane_helper_funcs.atomic_check hooks if a plane update can't be done
595 * without a full modeset) _must_ call this function afterwards after that
596 * change. It is permitted to call this function multiple times for the same
597 * update, e.g. when the &drm_crtc_helper_funcs.atomic_check functions depend
598 * upon the adjusted dotclock for fifo space allocation and watermark
599 * computation.
d9b13620 600 *
c39032a8 601 * RETURNS:
d9b13620
DV
602 * Zero for success or -errno
603 */
604int
934ce1c2 605drm_atomic_helper_check_modeset(struct drm_device *dev,
623369e5
DV
606 struct drm_atomic_state *state)
607{
623369e5 608 struct drm_crtc *crtc;
415c3ac3 609 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
df63b999 610 struct drm_connector *connector;
415c3ac3 611 struct drm_connector_state *old_connector_state, *new_connector_state;
623369e5 612 int i, ret;
ce09d766 613 unsigned connectors_mask = 0;
623369e5 614
415c3ac3 615 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
970ece83
ML
616 bool has_connectors =
617 !!new_crtc_state->connector_mask;
618
869e188a
DV
619 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
620
415c3ac3 621 if (!drm_mode_equal(&old_crtc_state->mode, &new_crtc_state->mode)) {
fa3ab4c2
VS
622 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] mode changed\n",
623 crtc->base.id, crtc->name);
415c3ac3 624 new_crtc_state->mode_changed = true;
623369e5
DV
625 }
626
415c3ac3 627 if (old_crtc_state->enable != new_crtc_state->enable) {
fa3ab4c2
VS
628 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enable changed\n",
629 crtc->base.id, crtc->name);
fc596660
ML
630
631 /*
632 * For clarity this assignment is done here, but
633 * enable == 0 is only true when there are no
634 * connectors and a NULL mode.
635 *
636 * The other way around is true as well. enable != 0
637 * iff connectors are attached and a mode is set.
638 */
415c3ac3
ML
639 new_crtc_state->mode_changed = true;
640 new_crtc_state->connectors_changed = true;
623369e5 641 }
24d6652c
ML
642
643 if (old_crtc_state->active != new_crtc_state->active) {
644 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active changed\n",
645 crtc->base.id, crtc->name);
646 new_crtc_state->active_changed = true;
647 }
970ece83
ML
648
649 if (new_crtc_state->enable != has_connectors) {
650 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled/connectors mismatch\n",
651 crtc->base.id, crtc->name);
652
653 return -EINVAL;
654 }
623369e5
DV
655 }
656
44596b8c 657 ret = handle_conflicting_encoders(state, false);
8248b65d
ML
658 if (ret)
659 return ret;
40616a26 660
415c3ac3 661 for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
ce09d766
ML
662 const struct drm_connector_helper_funcs *funcs = connector->helper_private;
663
869e188a
DV
664 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
665
623369e5 666 /*
d807ed1c
BS
667 * This only sets crtc->connectors_changed for routing changes,
668 * drivers must set crtc->connectors_changed themselves when
669 * connector properties need to be updated.
623369e5 670 */
9459545b 671 ret = update_connector_routing(state, connector,
415c3ac3
ML
672 old_connector_state,
673 new_connector_state);
623369e5
DV
674 if (ret)
675 return ret;
415c3ac3 676 if (old_connector_state->crtc) {
b4d93679
ML
677 new_crtc_state = drm_atomic_get_new_crtc_state(state,
678 old_connector_state->crtc);
415c3ac3
ML
679 if (old_connector_state->link_status !=
680 new_connector_state->link_status)
681 new_crtc_state->connectors_changed = true;
47e22ff1
RS
682
683 if (old_connector_state->max_requested_bpc !=
684 new_connector_state->max_requested_bpc)
685 new_crtc_state->connectors_changed = true;
40ee6fbe 686 }
ce09d766
ML
687
688 if (funcs->atomic_check)
6f3b6278 689 ret = funcs->atomic_check(connector, state);
ce09d766
ML
690 if (ret)
691 return ret;
692
ca52bea9 693 connectors_mask |= BIT(i);
623369e5
DV
694 }
695
696 /*
697 * After all the routing has been prepared we need to add in any
1e55a53a 698 * connector which is itself unchanged, but whose crtc changes its
623369e5
DV
699 * configuration. This must be done before calling mode_fixup in case a
700 * crtc only changed its mode but has the same set of connectors.
701 */
415c3ac3 702 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
415c3ac3 703 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
623369e5
DV
704 continue;
705
fa3ab4c2
VS
706 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] needs all connectors, enable: %c, active: %c\n",
707 crtc->base.id, crtc->name,
415c3ac3
ML
708 new_crtc_state->enable ? 'y' : 'n',
709 new_crtc_state->active ? 'y' : 'n');
623369e5
DV
710
711 ret = drm_atomic_add_affected_connectors(state, crtc);
712 if (ret != 0)
713 return ret;
714
57744aa7
ML
715 ret = drm_atomic_add_affected_planes(state, crtc);
716 if (ret != 0)
717 return ret;
623369e5
DV
718 }
719
ce09d766
ML
720 /*
721 * Iterate over all connectors again, to make sure atomic_check()
722 * has been called on them when a modeset is forced.
723 */
724 for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
725 const struct drm_connector_helper_funcs *funcs = connector->helper_private;
726
727 if (connectors_mask & BIT(i))
728 continue;
729
730 if (funcs->atomic_check)
6f3b6278 731 ret = funcs->atomic_check(connector, state);
ce09d766
ML
732 if (ret)
733 return ret;
734 }
735
faf94a08
JA
736 ret = mode_valid(state);
737 if (ret)
738 return ret;
739
623369e5
DV
740 return mode_fixup(state);
741}
d9b13620 742EXPORT_SYMBOL(drm_atomic_helper_check_modeset);
623369e5 743
a01cb8ba
VS
744/**
745 * drm_atomic_helper_check_plane_state() - Check plane state for validity
746 * @plane_state: plane state to check
747 * @crtc_state: crtc state to check
a01cb8ba
VS
748 * @min_scale: minimum @src:@dest scaling factor in 16.16 fixed point
749 * @max_scale: maximum @src:@dest scaling factor in 16.16 fixed point
750 * @can_position: is it legal to position the plane such that it
751 * doesn't cover the entire crtc? This will generally
752 * only be false for primary planes.
753 * @can_update_disabled: can the plane be updated while the crtc
754 * is disabled?
755 *
756 * Checks that a desired plane update is valid, and updates various
757 * bits of derived state (clipped coordinates etc.). Drivers that provide
758 * their own plane handling rather than helper-provided implementations may
759 * still wish to call this function to avoid duplication of error checking
760 * code.
761 *
762 * RETURNS:
763 * Zero if update appears valid, error code on failure
764 */
765int drm_atomic_helper_check_plane_state(struct drm_plane_state *plane_state,
766 const struct drm_crtc_state *crtc_state,
a01cb8ba
VS
767 int min_scale,
768 int max_scale,
769 bool can_position,
770 bool can_update_disabled)
771{
772 struct drm_framebuffer *fb = plane_state->fb;
773 struct drm_rect *src = &plane_state->src;
774 struct drm_rect *dst = &plane_state->dst;
775 unsigned int rotation = plane_state->rotation;
81af63a4 776 struct drm_rect clip = {};
a01cb8ba
VS
777 int hscale, vscale;
778
779 WARN_ON(plane_state->crtc && plane_state->crtc != crtc_state->crtc);
780
781 *src = drm_plane_state_src(plane_state);
782 *dst = drm_plane_state_dest(plane_state);
783
784 if (!fb) {
785 plane_state->visible = false;
786 return 0;
787 }
788
789 /* crtc should only be NULL when disabling (i.e., !fb) */
790 if (WARN_ON(!plane_state->crtc)) {
791 plane_state->visible = false;
792 return 0;
793 }
794
795 if (!crtc_state->enable && !can_update_disabled) {
796 DRM_DEBUG_KMS("Cannot update plane of a disabled CRTC.\n");
797 return -EINVAL;
798 }
799
800 drm_rect_rotate(src, fb->width << 16, fb->height << 16, rotation);
801
802 /* Check scaling */
803 hscale = drm_rect_calc_hscale(src, dst, min_scale, max_scale);
804 vscale = drm_rect_calc_vscale(src, dst, min_scale, max_scale);
805 if (hscale < 0 || vscale < 0) {
806 DRM_DEBUG_KMS("Invalid scaling of plane\n");
807 drm_rect_debug_print("src: ", &plane_state->src, true);
808 drm_rect_debug_print("dst: ", &plane_state->dst, false);
809 return -ERANGE;
810 }
811
81af63a4
VS
812 if (crtc_state->enable)
813 drm_mode_get_hv_timing(&crtc_state->mode, &clip.x2, &clip.y2);
814
f96bdf56 815 plane_state->visible = drm_rect_clip_scaled(src, dst, &clip);
a01cb8ba
VS
816
817 drm_rect_rotate_inv(src, fb->width << 16, fb->height << 16, rotation);
818
819 if (!plane_state->visible)
820 /*
821 * Plane isn't visible; some drivers can handle this
822 * so we just return success here. Drivers that can't
823 * (including those that use the primary plane helper's
824 * update function) will return an error from their
825 * update_plane handler.
826 */
827 return 0;
828
81af63a4 829 if (!can_position && !drm_rect_equals(dst, &clip)) {
a01cb8ba
VS
830 DRM_DEBUG_KMS("Plane must cover entire CRTC\n");
831 drm_rect_debug_print("dst: ", dst, false);
81af63a4 832 drm_rect_debug_print("clip: ", &clip, false);
a01cb8ba
VS
833 return -EINVAL;
834 }
835
836 return 0;
837}
838EXPORT_SYMBOL(drm_atomic_helper_check_plane_state);
839
c2fcd274 840/**
f98bd3ef 841 * drm_atomic_helper_check_planes - validate state object for planes changes
c2fcd274
DV
842 * @dev: DRM device
843 * @state: the driver state object
844 *
845 * Check the state object to see if the requested state is physically possible.
d9b13620 846 * This does all the plane update related checks using by calling into the
6806cdf9
DV
847 * &drm_crtc_helper_funcs.atomic_check and &drm_plane_helper_funcs.atomic_check
848 * hooks provided by the driver.
c2fcd274 849 *
6806cdf9 850 * It also sets &drm_crtc_state.planes_changed to indicate that a crtc has
fc596660
ML
851 * updated planes.
852 *
c39032a8 853 * RETURNS:
c2fcd274
DV
854 * Zero for success or -errno
855 */
d9b13620
DV
856int
857drm_atomic_helper_check_planes(struct drm_device *dev,
858 struct drm_atomic_state *state)
c2fcd274 859{
df63b999 860 struct drm_crtc *crtc;
415c3ac3 861 struct drm_crtc_state *new_crtc_state;
df63b999 862 struct drm_plane *plane;
415c3ac3 863 struct drm_plane_state *new_plane_state, *old_plane_state;
c2fcd274
DV
864 int i, ret = 0;
865
415c3ac3 866 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
b5ceff20 867 const struct drm_plane_helper_funcs *funcs;
c2fcd274 868
869e188a
DV
869 WARN_ON(!drm_modeset_is_locked(&plane->mutex));
870
c2fcd274
DV
871 funcs = plane->helper_private;
872
415c3ac3 873 drm_atomic_helper_plane_changed(state, old_plane_state, new_plane_state, plane);
c2fcd274 874
d9778b40
DR
875 drm_atomic_helper_check_plane_damage(state, new_plane_state);
876
c2fcd274
DV
877 if (!funcs || !funcs->atomic_check)
878 continue;
879
415c3ac3 880 ret = funcs->atomic_check(plane, new_plane_state);
c2fcd274 881 if (ret) {
9f4c97a2
VS
882 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic driver check failed\n",
883 plane->base.id, plane->name);
c2fcd274
DV
884 return ret;
885 }
886 }
887
415c3ac3 888 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
b5ceff20 889 const struct drm_crtc_helper_funcs *funcs;
c2fcd274
DV
890
891 funcs = crtc->helper_private;
892
893 if (!funcs || !funcs->atomic_check)
894 continue;
895
415c3ac3 896 ret = funcs->atomic_check(crtc, new_crtc_state);
c2fcd274 897 if (ret) {
fa3ab4c2
VS
898 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic driver check failed\n",
899 crtc->base.id, crtc->name);
c2fcd274
DV
900 return ret;
901 }
902 }
903
d9b13620
DV
904 return ret;
905}
906EXPORT_SYMBOL(drm_atomic_helper_check_planes);
907
908/**
909 * drm_atomic_helper_check - validate state object
910 * @dev: DRM device
911 * @state: the driver state object
912 *
913 * Check the state object to see if the requested state is physically possible.
914 * Only crtcs and planes have check callbacks, so for any additional (global)
915 * checking that a driver needs it can simply wrap that around this function.
6806cdf9
DV
916 * Drivers without such needs can directly use this as their
917 * &drm_mode_config_funcs.atomic_check callback.
d9b13620 918 *
b4274fbe
DV
919 * This just wraps the two parts of the state checking for planes and modeset
920 * state in the default order: First it calls drm_atomic_helper_check_modeset()
921 * and then drm_atomic_helper_check_planes(). The assumption is that the
6806cdf9
DV
922 * @drm_plane_helper_funcs.atomic_check and @drm_crtc_helper_funcs.atomic_check
923 * functions depend upon an updated adjusted_mode.clock to e.g. properly compute
924 * watermarks.
b4274fbe 925 *
49efffc7
PU
926 * Note that zpos normalization will add all enable planes to the state which
927 * might not desired for some drivers.
928 * For example enable/disable of a cursor plane which have fixed zpos value
929 * would trigger all other enabled planes to be forced to the state change.
930 *
c39032a8 931 * RETURNS:
d9b13620
DV
932 * Zero for success or -errno
933 */
934int drm_atomic_helper_check(struct drm_device *dev,
935 struct drm_atomic_state *state)
936{
937 int ret;
938
b4274fbe 939 ret = drm_atomic_helper_check_modeset(dev, state);
d9b13620
DV
940 if (ret)
941 return ret;
942
49efffc7
PU
943 if (dev->mode_config.normalize_zpos) {
944 ret = drm_atomic_normalize_zpos(dev, state);
945 if (ret)
946 return ret;
947 }
948
b4274fbe 949 ret = drm_atomic_helper_check_planes(dev, state);
934ce1c2
RC
950 if (ret)
951 return ret;
952
fef9df8b
GP
953 if (state->legacy_cursor_update)
954 state->async_update = !drm_atomic_helper_async_check(dev, state);
955
c2fcd274
DV
956 return ret;
957}
958EXPORT_SYMBOL(drm_atomic_helper_check);
959
623369e5
DV
960static void
961disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state)
962{
df63b999 963 struct drm_connector *connector;
415c3ac3 964 struct drm_connector_state *old_conn_state, *new_conn_state;
df63b999 965 struct drm_crtc *crtc;
415c3ac3 966 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
623369e5
DV
967 int i;
968
415c3ac3 969 for_each_oldnew_connector_in_state(old_state, connector, old_conn_state, new_conn_state, i) {
b5ceff20 970 const struct drm_encoder_helper_funcs *funcs;
623369e5
DV
971 struct drm_encoder *encoder;
972
623369e5
DV
973 /* Shut down everything that's in the changeset and currently
974 * still on. So need to check the old, saved state. */
df63b999 975 if (!old_conn_state->crtc)
623369e5
DV
976 continue;
977
b4d93679 978 old_crtc_state = drm_atomic_get_old_crtc_state(old_state, old_conn_state->crtc);
eab3bbef 979
4218a32f 980 if (!old_crtc_state->active ||
2465ff62 981 !drm_atomic_crtc_needs_modeset(old_conn_state->crtc->state))
eab3bbef
DV
982 continue;
983
46df9adb 984 encoder = old_conn_state->best_encoder;
623369e5 985
46df9adb
RC
986 /* We shouldn't get this far if we didn't previously have
987 * an encoder.. but WARN_ON() rather than explode.
988 */
989 if (WARN_ON(!encoder))
623369e5
DV
990 continue;
991
992 funcs = encoder->helper_private;
993
17a38d9c
DV
994 DRM_DEBUG_ATOMIC("disabling [ENCODER:%d:%s]\n",
995 encoder->base.id, encoder->name);
95d6eb3b 996
623369e5
DV
997 /*
998 * Each encoder has at most one connector (since we always steal
f98bd3ef 999 * it away), so we won't call disable hooks twice.
623369e5 1000 */
5ade071b 1001 drm_atomic_bridge_disable(encoder->bridge, old_state);
623369e5
DV
1002
1003 /* Right function depends upon target state. */
2827635e 1004 if (funcs) {
43c76d72
SP
1005 if (funcs->atomic_disable)
1006 funcs->atomic_disable(encoder, old_state);
1007 else if (new_conn_state->crtc && funcs->prepare)
2827635e
NT
1008 funcs->prepare(encoder);
1009 else if (funcs->disable)
1010 funcs->disable(encoder);
1011 else if (funcs->dpms)
1012 funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
1013 }
623369e5 1014
5ade071b 1015 drm_atomic_bridge_post_disable(encoder->bridge, old_state);
623369e5
DV
1016 }
1017
415c3ac3 1018 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
b5ceff20 1019 const struct drm_crtc_helper_funcs *funcs;
84014b0a 1020 int ret;
623369e5
DV
1021
1022 /* Shut down everything that needs a full modeset. */
415c3ac3 1023 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
eab3bbef
DV
1024 continue;
1025
1026 if (!old_crtc_state->active)
623369e5
DV
1027 continue;
1028
1029 funcs = crtc->helper_private;
1030
fa3ab4c2
VS
1031 DRM_DEBUG_ATOMIC("disabling [CRTC:%d:%s]\n",
1032 crtc->base.id, crtc->name);
95d6eb3b
DV
1033
1034
623369e5 1035 /* Right function depends upon target state. */
415c3ac3 1036 if (new_crtc_state->enable && funcs->prepare)
623369e5 1037 funcs->prepare(crtc);
c9ac8b4c
LY
1038 else if (funcs->atomic_disable)
1039 funcs->atomic_disable(crtc, old_crtc_state);
623369e5
DV
1040 else if (funcs->disable)
1041 funcs->disable(crtc);
fe616928 1042 else if (funcs->dpms)
623369e5 1043 funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
84014b0a
DV
1044
1045 if (!(dev->irq_enabled && dev->num_crtcs))
1046 continue;
1047
1048 ret = drm_crtc_vblank_get(crtc);
1049 WARN_ONCE(ret != -EINVAL, "driver forgot to call drm_crtc_vblank_off()\n");
1050 if (ret == 0)
1051 drm_crtc_vblank_put(crtc);
623369e5
DV
1052 }
1053}
1054
4c18d301
DV
1055/**
1056 * drm_atomic_helper_update_legacy_modeset_state - update legacy modeset state
1057 * @dev: DRM device
1058 * @old_state: atomic state object with old state structures
1059 *
1060 * This function updates all the various legacy modeset state pointers in
1061 * connectors, encoders and crtcs. It also updates the timestamping constants
1062 * used for precise vblank timestamps by calling
1063 * drm_calc_timestamping_constants().
1064 *
1065 * Drivers can use this for building their own atomic commit if they don't have
1066 * a pure helper-based modeset implementation.
2e2b96ef
DV
1067 *
1068 * Since these updates are not synchronized with lockings, only code paths
1069 * called from &drm_mode_config_helper_funcs.atomic_commit_tail can look at the
1070 * legacy state filled out by this helper. Defacto this means this helper and
1071 * the legacy state pointers are only really useful for transitioning an
1072 * existing driver to the atomic world.
4c18d301
DV
1073 */
1074void
1075drm_atomic_helper_update_legacy_modeset_state(struct drm_device *dev,
1076 struct drm_atomic_state *old_state)
623369e5 1077{
df63b999 1078 struct drm_connector *connector;
415c3ac3 1079 struct drm_connector_state *old_conn_state, *new_conn_state;
df63b999 1080 struct drm_crtc *crtc;
415c3ac3 1081 struct drm_crtc_state *new_crtc_state;
623369e5
DV
1082 int i;
1083
8c10342c 1084 /* clear out existing links and update dpms */
415c3ac3 1085 for_each_oldnew_connector_in_state(old_state, connector, old_conn_state, new_conn_state, i) {
8c10342c
ML
1086 if (connector->encoder) {
1087 WARN_ON(!connector->encoder->crtc);
1088
1089 connector->encoder->crtc = NULL;
1090 connector->encoder = NULL;
1091 }
623369e5 1092
415c3ac3 1093 crtc = new_conn_state->crtc;
8c10342c
ML
1094 if ((!crtc && old_conn_state->crtc) ||
1095 (crtc && drm_atomic_crtc_needs_modeset(crtc->state))) {
8c10342c 1096 int mode = DRM_MODE_DPMS_OFF;
623369e5 1097
8c10342c
ML
1098 if (crtc && crtc->state->active)
1099 mode = DRM_MODE_DPMS_ON;
1100
1101 connector->dpms = mode;
8c10342c 1102 }
623369e5
DV
1103 }
1104
1105 /* set new links */
415c3ac3
ML
1106 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
1107 if (!new_conn_state->crtc)
623369e5
DV
1108 continue;
1109
415c3ac3 1110 if (WARN_ON(!new_conn_state->best_encoder))
623369e5
DV
1111 continue;
1112
415c3ac3
ML
1113 connector->encoder = new_conn_state->best_encoder;
1114 connector->encoder->crtc = new_conn_state->crtc;
623369e5
DV
1115 }
1116
1117 /* set legacy state in the crtc structure */
415c3ac3 1118 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
2660801f 1119 struct drm_plane *primary = crtc->primary;
b4d93679 1120 struct drm_plane_state *new_plane_state;
2660801f 1121
415c3ac3
ML
1122 crtc->mode = new_crtc_state->mode;
1123 crtc->enabled = new_crtc_state->enable;
2660801f 1124
b4d93679
ML
1125 new_plane_state =
1126 drm_atomic_get_new_plane_state(old_state, primary);
1127
1128 if (new_plane_state && new_plane_state->crtc == crtc) {
1129 crtc->x = new_plane_state->src_x >> 16;
1130 crtc->y = new_plane_state->src_y >> 16;
2660801f 1131 }
3d51d2d2 1132
415c3ac3 1133 if (new_crtc_state->enable)
3d51d2d2 1134 drm_calc_timestamping_constants(crtc,
415c3ac3 1135 &new_crtc_state->adjusted_mode);
623369e5
DV
1136 }
1137}
4c18d301 1138EXPORT_SYMBOL(drm_atomic_helper_update_legacy_modeset_state);
623369e5
DV
1139
1140static void
1141crtc_set_mode(struct drm_device *dev, struct drm_atomic_state *old_state)
1142{
df63b999 1143 struct drm_crtc *crtc;
415c3ac3 1144 struct drm_crtc_state *new_crtc_state;
df63b999 1145 struct drm_connector *connector;
415c3ac3 1146 struct drm_connector_state *new_conn_state;
623369e5
DV
1147 int i;
1148
415c3ac3 1149 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
b5ceff20 1150 const struct drm_crtc_helper_funcs *funcs;
623369e5 1151
415c3ac3 1152 if (!new_crtc_state->mode_changed)
623369e5
DV
1153 continue;
1154
1155 funcs = crtc->helper_private;
1156
415c3ac3 1157 if (new_crtc_state->enable && funcs->mode_set_nofb) {
fa3ab4c2
VS
1158 DRM_DEBUG_ATOMIC("modeset on [CRTC:%d:%s]\n",
1159 crtc->base.id, crtc->name);
95d6eb3b 1160
623369e5 1161 funcs->mode_set_nofb(crtc);
95d6eb3b 1162 }
623369e5
DV
1163 }
1164
415c3ac3 1165 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
b5ceff20 1166 const struct drm_encoder_helper_funcs *funcs;
623369e5
DV
1167 struct drm_encoder *encoder;
1168 struct drm_display_mode *mode, *adjusted_mode;
1169
415c3ac3 1170 if (!new_conn_state->best_encoder)
623369e5
DV
1171 continue;
1172
415c3ac3 1173 encoder = new_conn_state->best_encoder;
623369e5 1174 funcs = encoder->helper_private;
415c3ac3 1175 new_crtc_state = new_conn_state->crtc->state;
623369e5
DV
1176 mode = &new_crtc_state->mode;
1177 adjusted_mode = &new_crtc_state->adjusted_mode;
1178
eab3bbef
DV
1179 if (!new_crtc_state->mode_changed)
1180 continue;
1181
17a38d9c
DV
1182 DRM_DEBUG_ATOMIC("modeset on [ENCODER:%d:%s]\n",
1183 encoder->base.id, encoder->name);
95d6eb3b 1184
623369e5
DV
1185 /*
1186 * Each encoder has at most one connector (since we always steal
f98bd3ef 1187 * it away), so we won't call mode_set hooks twice.
623369e5 1188 */
fe4a11c9
PZ
1189 if (funcs && funcs->atomic_mode_set) {
1190 funcs->atomic_mode_set(encoder, new_crtc_state,
415c3ac3 1191 new_conn_state);
fe4a11c9 1192 } else if (funcs && funcs->mode_set) {
c982bd90 1193 funcs->mode_set(encoder, mode, adjusted_mode);
fe4a11c9 1194 }
623369e5 1195
862e686c 1196 drm_bridge_mode_set(encoder->bridge, mode, adjusted_mode);
623369e5
DV
1197 }
1198}
1199
1200/**
1af434a9 1201 * drm_atomic_helper_commit_modeset_disables - modeset commit to disable outputs
623369e5 1202 * @dev: DRM device
a072f809 1203 * @old_state: atomic state object with old state structures
623369e5 1204 *
1af434a9 1205 * This function shuts down all the outputs that need to be shut down and
623369e5 1206 * prepares them (if required) with the new mode.
1af434a9 1207 *
60acc4eb 1208 * For compatibility with legacy crtc helpers this should be called before
1af434a9
DV
1209 * drm_atomic_helper_commit_planes(), which is what the default commit function
1210 * does. But drivers with different needs can group the modeset commits together
1211 * and do the plane commits at the end. This is useful for drivers doing runtime
1212 * PM since planes updates then only happen when the CRTC is actually enabled.
623369e5 1213 */
1af434a9
DV
1214void drm_atomic_helper_commit_modeset_disables(struct drm_device *dev,
1215 struct drm_atomic_state *old_state)
623369e5 1216{
a072f809 1217 disable_outputs(dev, old_state);
4c18d301
DV
1218
1219 drm_atomic_helper_update_legacy_modeset_state(dev, old_state);
1220
a072f809 1221 crtc_set_mode(dev, old_state);
623369e5 1222}
1af434a9 1223EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_disables);
623369e5 1224
935774cd
BS
1225static void drm_atomic_helper_commit_writebacks(struct drm_device *dev,
1226 struct drm_atomic_state *old_state)
1227{
1228 struct drm_connector *connector;
1229 struct drm_connector_state *new_conn_state;
1230 int i;
1231
1232 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
1233 const struct drm_connector_helper_funcs *funcs;
1234
1235 funcs = connector->helper_private;
814bde99
BB
1236 if (!funcs->atomic_commit)
1237 continue;
935774cd
BS
1238
1239 if (new_conn_state->writeback_job && new_conn_state->writeback_job->fb) {
1240 WARN_ON(connector->connector_type != DRM_MODE_CONNECTOR_WRITEBACK);
425132fd 1241 funcs->atomic_commit(connector, new_conn_state);
935774cd
BS
1242 }
1243 }
1244}
1245
623369e5 1246/**
1af434a9 1247 * drm_atomic_helper_commit_modeset_enables - modeset commit to enable outputs
623369e5
DV
1248 * @dev: DRM device
1249 * @old_state: atomic state object with old state structures
1250 *
1af434a9
DV
1251 * This function enables all the outputs with the new configuration which had to
1252 * be turned off for the update.
1253 *
60acc4eb 1254 * For compatibility with legacy crtc helpers this should be called after
1af434a9
DV
1255 * drm_atomic_helper_commit_planes(), which is what the default commit function
1256 * does. But drivers with different needs can group the modeset commits together
1257 * and do the plane commits at the end. This is useful for drivers doing runtime
1258 * PM since planes updates then only happen when the CRTC is actually enabled.
623369e5 1259 */
1af434a9
DV
1260void drm_atomic_helper_commit_modeset_enables(struct drm_device *dev,
1261 struct drm_atomic_state *old_state)
623369e5 1262{
df63b999 1263 struct drm_crtc *crtc;
0b20a0f8 1264 struct drm_crtc_state *old_crtc_state;
415c3ac3 1265 struct drm_crtc_state *new_crtc_state;
df63b999 1266 struct drm_connector *connector;
415c3ac3 1267 struct drm_connector_state *new_conn_state;
623369e5
DV
1268 int i;
1269
0b20a0f8 1270 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
b5ceff20 1271 const struct drm_crtc_helper_funcs *funcs;
623369e5
DV
1272
1273 /* Need to filter out CRTCs where only planes change. */
415c3ac3 1274 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
eab3bbef
DV
1275 continue;
1276
415c3ac3 1277 if (!new_crtc_state->active)
623369e5
DV
1278 continue;
1279
1280 funcs = crtc->helper_private;
1281
415c3ac3 1282 if (new_crtc_state->enable) {
fa3ab4c2
VS
1283 DRM_DEBUG_ATOMIC("enabling [CRTC:%d:%s]\n",
1284 crtc->base.id, crtc->name);
0b20a0f8
LP
1285 if (funcs->atomic_enable)
1286 funcs->atomic_enable(crtc, old_crtc_state);
fe616928 1287 else if (funcs->commit)
ee0a89cf
DV
1288 funcs->commit(crtc);
1289 }
623369e5
DV
1290 }
1291
415c3ac3 1292 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
b5ceff20 1293 const struct drm_encoder_helper_funcs *funcs;
623369e5
DV
1294 struct drm_encoder *encoder;
1295
415c3ac3 1296 if (!new_conn_state->best_encoder)
623369e5
DV
1297 continue;
1298
415c3ac3
ML
1299 if (!new_conn_state->crtc->state->active ||
1300 !drm_atomic_crtc_needs_modeset(new_conn_state->crtc->state))
eab3bbef
DV
1301 continue;
1302
415c3ac3 1303 encoder = new_conn_state->best_encoder;
623369e5
DV
1304 funcs = encoder->helper_private;
1305
17a38d9c
DV
1306 DRM_DEBUG_ATOMIC("enabling [ENCODER:%d:%s]\n",
1307 encoder->base.id, encoder->name);
95d6eb3b 1308
623369e5
DV
1309 /*
1310 * Each encoder has at most one connector (since we always steal
f98bd3ef 1311 * it away), so we won't call enable hooks twice.
623369e5 1312 */
5ade071b 1313 drm_atomic_bridge_pre_enable(encoder->bridge, old_state);
623369e5 1314
2827635e 1315 if (funcs) {
43c76d72
SP
1316 if (funcs->atomic_enable)
1317 funcs->atomic_enable(encoder, old_state);
1318 else if (funcs->enable)
2827635e
NT
1319 funcs->enable(encoder);
1320 else if (funcs->commit)
1321 funcs->commit(encoder);
1322 }
623369e5 1323
5ade071b 1324 drm_atomic_bridge_enable(encoder->bridge, old_state);
623369e5 1325 }
935774cd
BS
1326
1327 drm_atomic_helper_commit_writebacks(dev, old_state);
623369e5 1328}
1af434a9 1329EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_enables);
623369e5 1330
4c5b7f3a
RC
1331/**
1332 * drm_atomic_helper_wait_for_fences - wait for fences stashed in plane state
1333 * @dev: DRM device
1334 * @state: atomic state object with old state structures
1ea0c02e
DV
1335 * @pre_swap: If true, do an interruptible wait, and @state is the new state.
1336 * Otherwise @state is the old state.
4c5b7f3a
RC
1337 *
1338 * For implicit sync, driver should fish the exclusive fence out from the
1339 * incoming fb's and stash it in the drm_plane_state. This is called after
1340 * drm_atomic_helper_swap_state() so it uses the current plane state (and
1341 * just uses the atomic state to find the changed planes)
f6ce410a 1342 *
1ea0c02e
DV
1343 * Note that @pre_swap is needed since the point where we block for fences moves
1344 * around depending upon whether an atomic commit is blocking or
42590372
GP
1345 * non-blocking. For non-blocking commit all waiting needs to happen after
1346 * drm_atomic_helper_swap_state() is called, but for blocking commits we want
1ea0c02e
DV
1347 * to wait **before** we do anything that can't be easily rolled back. That is
1348 * before we call drm_atomic_helper_swap_state().
1349 *
f54d1867 1350 * Returns zero if success or < 0 if dma_fence_wait() fails.
4c5b7f3a 1351 */
f6ce410a
GP
1352int drm_atomic_helper_wait_for_fences(struct drm_device *dev,
1353 struct drm_atomic_state *state,
1354 bool pre_swap)
e2330f07 1355{
df63b999 1356 struct drm_plane *plane;
415c3ac3 1357 struct drm_plane_state *new_plane_state;
f6ce410a 1358 int i, ret;
e2330f07 1359
415c3ac3
ML
1360 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
1361 if (!new_plane_state->fence)
e2330f07
DV
1362 continue;
1363
415c3ac3 1364 WARN_ON(!new_plane_state->fb);
f6ce410a
GP
1365
1366 /*
1367 * If waiting for fences pre-swap (ie: nonblock), userspace can
1368 * still interrupt the operation. Instead of blocking until the
1369 * timer expires, make the wait interruptible.
1370 */
415c3ac3 1371 ret = dma_fence_wait(new_plane_state->fence, pre_swap);
f6ce410a
GP
1372 if (ret)
1373 return ret;
e2330f07 1374
415c3ac3
ML
1375 dma_fence_put(new_plane_state->fence);
1376 new_plane_state->fence = NULL;
e2330f07 1377 }
f6ce410a
GP
1378
1379 return 0;
e2330f07 1380}
4c5b7f3a 1381EXPORT_SYMBOL(drm_atomic_helper_wait_for_fences);
e2330f07 1382
5ee3229c
RC
1383/**
1384 * drm_atomic_helper_wait_for_vblanks - wait for vblank on crtcs
1385 * @dev: DRM device
1386 * @old_state: atomic state object with old state structures
1387 *
1388 * Helper to, after atomic commit, wait for vblanks on all effected
1389 * crtcs (ie. before cleaning up old framebuffers using
01086487 1390 * drm_atomic_helper_cleanup_planes()). It will only wait on CRTCs where the
ab58e338
DV
1391 * framebuffers have actually changed to optimize for the legacy cursor and
1392 * plane update use-case.
01086487
BB
1393 *
1394 * Drivers using the nonblocking commit tracking support initialized by calling
1395 * drm_atomic_helper_setup_commit() should look at
1396 * drm_atomic_helper_wait_for_flip_done() as an alternative.
5ee3229c
RC
1397 */
1398void
1399drm_atomic_helper_wait_for_vblanks(struct drm_device *dev,
1400 struct drm_atomic_state *old_state)
623369e5
DV
1401{
1402 struct drm_crtc *crtc;
415c3ac3 1403 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
623369e5 1404 int i, ret;
bdc57146 1405 unsigned crtc_mask = 0;
623369e5 1406
bdc57146
ML
1407 /*
1408 * Legacy cursor ioctls are completely unsynced, and userspace
1409 * relies on that (by doing tons of cursor updates).
1410 */
1411 if (old_state->legacy_cursor_update)
1412 return;
623369e5 1413
415c3ac3 1414 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
a76dfe35 1415 if (!new_crtc_state->active)
ab58e338
DV
1416 continue;
1417
623369e5
DV
1418 ret = drm_crtc_vblank_get(crtc);
1419 if (ret != 0)
1420 continue;
1421
bdc57146
ML
1422 crtc_mask |= drm_crtc_mask(crtc);
1423 old_state->crtcs[i].last_vblank_count = drm_crtc_vblank_count(crtc);
623369e5
DV
1424 }
1425
415c3ac3 1426 for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
bdc57146 1427 if (!(crtc_mask & drm_crtc_mask(crtc)))
623369e5
DV
1428 continue;
1429
1430 ret = wait_event_timeout(dev->vblank[i].queue,
bdc57146 1431 old_state->crtcs[i].last_vblank_count !=
d4853630 1432 drm_crtc_vblank_count(crtc),
b3198c38 1433 msecs_to_jiffies(100));
623369e5 1434
6ac7c548
RK
1435 WARN(!ret, "[CRTC:%d:%s] vblank wait timed out\n",
1436 crtc->base.id, crtc->name);
8d4d0d70 1437
623369e5
DV
1438 drm_crtc_vblank_put(crtc);
1439 }
1440}
5ee3229c 1441EXPORT_SYMBOL(drm_atomic_helper_wait_for_vblanks);
623369e5 1442
01086487
BB
1443/**
1444 * drm_atomic_helper_wait_for_flip_done - wait for all page flips to be done
1445 * @dev: DRM device
1446 * @old_state: atomic state object with old state structures
1447 *
1448 * Helper to, after atomic commit, wait for page flips on all effected
1449 * crtcs (ie. before cleaning up old framebuffers using
1450 * drm_atomic_helper_cleanup_planes()). Compared to
1451 * drm_atomic_helper_wait_for_vblanks() this waits for the completion of on all
1452 * CRTCs, assuming that cursors-only updates are signalling their completion
1453 * immediately (or using a different path).
1454 *
1455 * This requires that drivers use the nonblocking commit tracking support
1456 * initialized using drm_atomic_helper_setup_commit().
1457 */
1458void drm_atomic_helper_wait_for_flip_done(struct drm_device *dev,
1459 struct drm_atomic_state *old_state)
1460{
01086487
BB
1461 struct drm_crtc *crtc;
1462 int i;
1463
4364bcb2
LL
1464 for (i = 0; i < dev->mode_config.num_crtc; i++) {
1465 struct drm_crtc_commit *commit = old_state->crtcs[i].commit;
01086487
BB
1466 int ret;
1467
4364bcb2
LL
1468 crtc = old_state->crtcs[i].ptr;
1469
1470 if (!crtc || !commit)
01086487
BB
1471 continue;
1472
1473 ret = wait_for_completion_timeout(&commit->flip_done, 10 * HZ);
1474 if (ret == 0)
1475 DRM_ERROR("[CRTC:%d:%s] flip_done timed out\n",
1476 crtc->base.id, crtc->name);
1477 }
2de42f79
VS
1478
1479 if (old_state->fake_commit)
1480 complete_all(&old_state->fake_commit->flip_done);
01086487
BB
1481}
1482EXPORT_SYMBOL(drm_atomic_helper_wait_for_flip_done);
1483
623369e5 1484/**
9f2a7950 1485 * drm_atomic_helper_commit_tail - commit atomic update to hardware
1ea0c02e 1486 * @old_state: atomic state object with old state structures
623369e5 1487 *
6806cdf9 1488 * This is the default implementation for the
81a099ac
MR
1489 * &drm_mode_config_helper_funcs.atomic_commit_tail hook, for drivers
1490 * that do not support runtime_pm or do not need the CRTC to be
1491 * enabled to perform a commit. Otherwise, see
1492 * drm_atomic_helper_commit_tail_rpm().
623369e5 1493 *
9f2a7950 1494 * Note that the default ordering of how the various stages are called is to
81a099ac 1495 * match the legacy modeset helper library closest.
9f2a7950 1496 */
1ea0c02e 1497void drm_atomic_helper_commit_tail(struct drm_atomic_state *old_state)
9f2a7950 1498{
1ea0c02e 1499 struct drm_device *dev = old_state->dev;
9f2a7950 1500
1ea0c02e 1501 drm_atomic_helper_commit_modeset_disables(dev, old_state);
9f2a7950 1502
1ea0c02e 1503 drm_atomic_helper_commit_planes(dev, old_state, 0);
9f2a7950 1504
1ea0c02e 1505 drm_atomic_helper_commit_modeset_enables(dev, old_state);
9f2a7950 1506
6fb42b66
BB
1507 drm_atomic_helper_fake_vblank(old_state);
1508
1ea0c02e 1509 drm_atomic_helper_commit_hw_done(old_state);
9f2a7950 1510
1ea0c02e 1511 drm_atomic_helper_wait_for_vblanks(dev, old_state);
9f2a7950 1512
1ea0c02e 1513 drm_atomic_helper_cleanup_planes(dev, old_state);
9f2a7950
DV
1514}
1515EXPORT_SYMBOL(drm_atomic_helper_commit_tail);
1516
81a099ac
MR
1517/**
1518 * drm_atomic_helper_commit_tail_rpm - commit atomic update to hardware
1519 * @old_state: new modeset state to be committed
1520 *
1521 * This is an alternative implementation for the
1522 * &drm_mode_config_helper_funcs.atomic_commit_tail hook, for drivers
1523 * that support runtime_pm or need the CRTC to be enabled to perform a
1524 * commit. Otherwise, one should use the default implementation
1525 * drm_atomic_helper_commit_tail().
1526 */
1527void drm_atomic_helper_commit_tail_rpm(struct drm_atomic_state *old_state)
1528{
1529 struct drm_device *dev = old_state->dev;
1530
1531 drm_atomic_helper_commit_modeset_disables(dev, old_state);
1532
1533 drm_atomic_helper_commit_modeset_enables(dev, old_state);
1534
1535 drm_atomic_helper_commit_planes(dev, old_state,
1536 DRM_PLANE_COMMIT_ACTIVE_ONLY);
1537
6fb42b66
BB
1538 drm_atomic_helper_fake_vblank(old_state);
1539
81a099ac
MR
1540 drm_atomic_helper_commit_hw_done(old_state);
1541
1542 drm_atomic_helper_wait_for_vblanks(dev, old_state);
1543
1544 drm_atomic_helper_cleanup_planes(dev, old_state);
1545}
1546EXPORT_SYMBOL(drm_atomic_helper_commit_tail_rpm);
1547
1ea0c02e 1548static void commit_tail(struct drm_atomic_state *old_state)
9f2a7950 1549{
1ea0c02e 1550 struct drm_device *dev = old_state->dev;
a4b10cce 1551 const struct drm_mode_config_helper_funcs *funcs;
9f2a7950
DV
1552
1553 funcs = dev->mode_config.helper_private;
1554
1ea0c02e 1555 drm_atomic_helper_wait_for_fences(dev, old_state, false);
9f2a7950 1556
1ea0c02e 1557 drm_atomic_helper_wait_for_dependencies(old_state);
9f2a7950
DV
1558
1559 if (funcs && funcs->atomic_commit_tail)
1ea0c02e 1560 funcs->atomic_commit_tail(old_state);
9f2a7950 1561 else
1ea0c02e 1562 drm_atomic_helper_commit_tail(old_state);
9f2a7950 1563
1ea0c02e 1564 drm_atomic_helper_commit_cleanup_done(old_state);
9f2a7950 1565
1ea0c02e 1566 drm_atomic_state_put(old_state);
9f2a7950
DV
1567}
1568
1569static void commit_work(struct work_struct *work)
1570{
1571 struct drm_atomic_state *state = container_of(work,
1572 struct drm_atomic_state,
1573 commit_work);
1574 commit_tail(state);
1575}
1576
fef9df8b
GP
1577/**
1578 * drm_atomic_helper_async_check - check if state can be commited asynchronously
1579 * @dev: DRM device
1580 * @state: the driver state object
1581 *
1582 * This helper will check if it is possible to commit the state asynchronously.
1583 * Async commits are not supposed to swap the states like normal sync commits
1584 * but just do in-place changes on the current state.
1585 *
1586 * It will return 0 if the commit can happen in an asynchronous fashion or error
1587 * if not. Note that error just mean it can't be commited asynchronously, if it
1588 * fails the commit should be treated like a normal synchronous commit.
1589 */
1590int drm_atomic_helper_async_check(struct drm_device *dev,
1591 struct drm_atomic_state *state)
1592{
1593 struct drm_crtc *crtc;
1594 struct drm_crtc_state *crtc_state;
de2d8db3
BB
1595 struct drm_plane *plane = NULL;
1596 struct drm_plane_state *old_plane_state = NULL;
1597 struct drm_plane_state *new_plane_state = NULL;
fef9df8b 1598 const struct drm_plane_helper_funcs *funcs;
669c9215 1599 int i, n_planes = 0;
fef9df8b
GP
1600
1601 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
1602 if (drm_atomic_crtc_needs_modeset(crtc_state))
1603 return -EINVAL;
1604 }
1605
669c9215 1606 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i)
fef9df8b 1607 n_planes++;
fef9df8b
GP
1608
1609 /* FIXME: we support only single plane updates for now */
669c9215 1610 if (n_planes != 1)
fef9df8b
GP
1611 return -EINVAL;
1612
603ba2df
BB
1613 if (!new_plane_state->crtc ||
1614 old_plane_state->crtc != new_plane_state->crtc)
fef9df8b
GP
1615 return -EINVAL;
1616
22163229
NK
1617 /*
1618 * FIXME: Since prepare_fb and cleanup_fb are always called on
1619 * the new_plane_state for async updates we need to block framebuffer
1620 * changes. This prevents use of a fb that's been cleaned up and
1621 * double cleanups from occuring.
1622 */
1623 if (old_plane_state->fb != new_plane_state->fb)
1624 return -EINVAL;
1625
fef9df8b
GP
1626 funcs = plane->helper_private;
1627 if (!funcs->atomic_async_update)
1628 return -EINVAL;
1629
669c9215 1630 if (new_plane_state->fence)
fef9df8b
GP
1631 return -EINVAL;
1632
1633 /*
1634 * Don't do an async update if there is an outstanding commit modifying
1635 * the plane. This prevents our async update's changes from getting
1636 * overridden by a previous synchronous update's state.
1637 */
669c9215
ML
1638 if (old_plane_state->commit &&
1639 !try_wait_for_completion(&old_plane_state->commit->hw_done))
1640 return -EBUSY;
fef9df8b 1641
669c9215 1642 return funcs->atomic_async_check(plane, new_plane_state);
fef9df8b
GP
1643}
1644EXPORT_SYMBOL(drm_atomic_helper_async_check);
1645
1646/**
1647 * drm_atomic_helper_async_commit - commit state asynchronously
1648 * @dev: DRM device
1649 * @state: the driver state object
1650 *
1651 * This function commits a state asynchronously, i.e., not vblank
1652 * synchronized. It should be used on a state only when
1653 * drm_atomic_async_check() succeeds. Async commits are not supposed to swap
1654 * the states like normal sync commits, but just do in-place changes on the
1655 * current state.
1656 */
1657void drm_atomic_helper_async_commit(struct drm_device *dev,
1658 struct drm_atomic_state *state)
1659{
1660 struct drm_plane *plane;
1661 struct drm_plane_state *plane_state;
1662 const struct drm_plane_helper_funcs *funcs;
1663 int i;
1664
1665 for_each_new_plane_in_state(state, plane, plane_state, i) {
1666 funcs = plane->helper_private;
1667 funcs->atomic_async_update(plane, plane_state);
02edfd9c
BB
1668
1669 /*
1670 * ->atomic_async_update() is supposed to update the
1671 * plane->state in-place, make sure at least common
1672 * properties have been properly updated.
1673 */
1674 WARN_ON_ONCE(plane->state->fb != plane_state->fb);
1675 WARN_ON_ONCE(plane->state->crtc_x != plane_state->crtc_x);
1676 WARN_ON_ONCE(plane->state->crtc_y != plane_state->crtc_y);
1677 WARN_ON_ONCE(plane->state->src_x != plane_state->src_x);
1678 WARN_ON_ONCE(plane->state->src_y != plane_state->src_y);
fef9df8b
GP
1679 }
1680}
1681EXPORT_SYMBOL(drm_atomic_helper_async_commit);
1682
9f2a7950
DV
1683/**
1684 * drm_atomic_helper_commit - commit validated state object
1685 * @dev: DRM device
1686 * @state: the driver state object
1687 * @nonblock: whether nonblocking behavior is requested.
1688 *
1689 * This function commits a with drm_atomic_helper_check() pre-validated state
1690 * object. This can still fail when e.g. the framebuffer reservation fails. This
1691 * function implements nonblocking commits, using
1692 * drm_atomic_helper_setup_commit() and related functions.
1693 *
9f2a7950 1694 * Committing the actual hardware state is done through the
1e55a53a 1695 * &drm_mode_config_helper_funcs.atomic_commit_tail callback, or its default
6806cdf9 1696 * implementation drm_atomic_helper_commit_tail().
6e48ae32 1697 *
c39032a8 1698 * RETURNS:
623369e5
DV
1699 * Zero for success or -errno.
1700 */
1701int drm_atomic_helper_commit(struct drm_device *dev,
1702 struct drm_atomic_state *state,
286dbb8d 1703 bool nonblock)
623369e5
DV
1704{
1705 int ret;
1706
fef9df8b
GP
1707 if (state->async_update) {
1708 ret = drm_atomic_helper_prepare_planes(dev, state);
1709 if (ret)
1710 return ret;
1711
1712 drm_atomic_helper_async_commit(dev, state);
1713 drm_atomic_helper_cleanup_planes(dev, state);
1714
1715 return 0;
1716 }
1717
a095caa7
DV
1718 ret = drm_atomic_helper_setup_commit(state, nonblock);
1719 if (ret)
1720 return ret;
1721
9f2a7950
DV
1722 INIT_WORK(&state->commit_work, commit_work);
1723
623369e5
DV
1724 ret = drm_atomic_helper_prepare_planes(dev, state);
1725 if (ret)
1726 return ret;
1727
f6ce410a
GP
1728 if (!nonblock) {
1729 ret = drm_atomic_helper_wait_for_fences(dev, state, true);
c066d231
ML
1730 if (ret)
1731 goto err;
f6ce410a
GP
1732 }
1733
623369e5
DV
1734 /*
1735 * This is the point of no return - everything below never fails except
1736 * when the hw goes bonghits. Which means we can commit the new state on
1737 * the software side now.
1738 */
1739
c066d231
ML
1740 ret = drm_atomic_helper_swap_state(state, true);
1741 if (ret)
1742 goto err;
623369e5
DV
1743
1744 /*
1745 * Everything below can be run asynchronously without the need to grab
f98bd3ef 1746 * any modeset locks at all under one condition: It must be guaranteed
623369e5
DV
1747 * that the asynchronous work has either been cancelled (if the driver
1748 * supports it, which at least requires that the framebuffers get
1749 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
1750 * before the new state gets committed on the software side with
1751 * drm_atomic_helper_swap_state().
1752 *
1753 * This scheme allows new atomic state updates to be prepared and
1754 * checked in parallel to the asynchronous completion of the previous
1755 * update. Which is important since compositors need to figure out the
1756 * composition of the next frame right after having submitted the
1757 * current layout.
9f2a7950
DV
1758 *
1759 * NOTE: Commit work has multiple phases, first hardware commit, then
1760 * cleanup. We want them to overlap, hence need system_unbound_wq to
a0689e34 1761 * make sure work items don't artificially stall on each another.
623369e5
DV
1762 */
1763
0853695c 1764 drm_atomic_state_get(state);
9f2a7950
DV
1765 if (nonblock)
1766 queue_work(system_unbound_wq, &state->commit_work);
1767 else
1768 commit_tail(state);
623369e5
DV
1769
1770 return 0;
c066d231
ML
1771
1772err:
1773 drm_atomic_helper_cleanup_planes(dev, state);
1774 return ret;
623369e5
DV
1775}
1776EXPORT_SYMBOL(drm_atomic_helper_commit);
1777
e8c833a7 1778/**
286dbb8d 1779 * DOC: implementing nonblocking commit
e8c833a7 1780 *
9f2a7950 1781 * Nonblocking atomic commits have to be implemented in the following sequence:
e8c833a7
DV
1782 *
1783 * 1. Run drm_atomic_helper_prepare_planes() first. This is the only function
1784 * which commit needs to call which can fail, so we want to run it first and
1785 * synchronously.
1786 *
286dbb8d 1787 * 2. Synchronize with any outstanding nonblocking commit worker threads which
e8c833a7
DV
1788 * might be affected the new state update. This can be done by either cancelling
1789 * or flushing the work items, depending upon whether the driver can deal with
1790 * cancelled updates. Note that it is important to ensure that the framebuffer
1791 * cleanup is still done when cancelling.
1792 *
9f2a7950
DV
1793 * Asynchronous workers need to have sufficient parallelism to be able to run
1794 * different atomic commits on different CRTCs in parallel. The simplest way to
a0689e34 1795 * achieve this is by running them on the &system_unbound_wq work queue. Note
9f2a7950
DV
1796 * that drivers are not required to split up atomic commits and run an
1797 * individual commit in parallel - userspace is supposed to do that if it cares.
1798 * But it might be beneficial to do that for modesets, since those necessarily
1799 * must be done as one global operation, and enabling or disabling a CRTC can
1800 * take a long time. But even that is not required.
e8c833a7
DV
1801 *
1802 * 3. The software state is updated synchronously with
26196f7e 1803 * drm_atomic_helper_swap_state(). Doing this under the protection of all modeset
e8c833a7 1804 * locks means concurrent callers never see inconsistent state. And doing this
286dbb8d
ML
1805 * while it's guaranteed that no relevant nonblocking worker runs means that
1806 * nonblocking workers do not need grab any locks. Actually they must not grab
1807 * locks, for otherwise the work flushing will deadlock.
e8c833a7
DV
1808 *
1809 * 4. Schedule a work item to do all subsequent steps, using the split-out
1810 * commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and
1811 * then cleaning up the framebuffers after the old framebuffer is no longer
1812 * being displayed.
9f2a7950
DV
1813 *
1814 * The above scheme is implemented in the atomic helper libraries in
1815 * drm_atomic_helper_commit() using a bunch of helper functions. See
1816 * drm_atomic_helper_setup_commit() for a starting point.
e8c833a7
DV
1817 */
1818
a095caa7
DV
1819static int stall_checks(struct drm_crtc *crtc, bool nonblock)
1820{
1821 struct drm_crtc_commit *commit, *stall_commit = NULL;
1822 bool completed = true;
1823 int i;
1824 long ret = 0;
1825
1826 spin_lock(&crtc->commit_lock);
1827 i = 0;
1828 list_for_each_entry(commit, &crtc->commit_list, commit_entry) {
1829 if (i == 0) {
1830 completed = try_wait_for_completion(&commit->flip_done);
1831 /* Userspace is not allowed to get ahead of the previous
1832 * commit with nonblocking ones. */
1833 if (!completed && nonblock) {
1834 spin_unlock(&crtc->commit_lock);
1835 return -EBUSY;
1836 }
1837 } else if (i == 1) {
f46640b9 1838 stall_commit = drm_crtc_commit_get(commit);
a095caa7 1839 break;
723c3e55 1840 }
a095caa7
DV
1841
1842 i++;
1843 }
1844 spin_unlock(&crtc->commit_lock);
1845
1846 if (!stall_commit)
1847 return 0;
1848
1849 /* We don't want to let commits get ahead of cleanup work too much,
1850 * stalling on 2nd previous commit means triple-buffer won't ever stall.
1851 */
723c3e55 1852 ret = wait_for_completion_interruptible_timeout(&stall_commit->cleanup_done,
a095caa7
DV
1853 10*HZ);
1854 if (ret == 0)
1855 DRM_ERROR("[CRTC:%d:%s] cleanup_done timed out\n",
1856 crtc->base.id, crtc->name);
1857
1858 drm_crtc_commit_put(stall_commit);
1859
1860 return ret < 0 ? ret : 0;
1861}
1862
899cc5f1 1863static void release_crtc_commit(struct completion *completion)
24835e44
DV
1864{
1865 struct drm_crtc_commit *commit = container_of(completion,
1866 typeof(*commit),
1867 flip_done);
1868
1869 drm_crtc_commit_put(commit);
1870}
1871
21a01abb
ML
1872static void init_commit(struct drm_crtc_commit *commit, struct drm_crtc *crtc)
1873{
1874 init_completion(&commit->flip_done);
1875 init_completion(&commit->hw_done);
1876 init_completion(&commit->cleanup_done);
1877 INIT_LIST_HEAD(&commit->commit_entry);
1878 kref_init(&commit->ref);
1879 commit->crtc = crtc;
1880}
1881
1882static struct drm_crtc_commit *
1883crtc_or_fake_commit(struct drm_atomic_state *state, struct drm_crtc *crtc)
1884{
1885 if (crtc) {
1886 struct drm_crtc_state *new_crtc_state;
1887
1888 new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
1889
1890 return new_crtc_state->commit;
1891 }
1892
1893 if (!state->fake_commit) {
1894 state->fake_commit = kzalloc(sizeof(*state->fake_commit), GFP_KERNEL);
1895 if (!state->fake_commit)
1896 return NULL;
1897
1898 init_commit(state->fake_commit, NULL);
1899 }
1900
1901 return state->fake_commit;
1902}
1903
a095caa7
DV
1904/**
1905 * drm_atomic_helper_setup_commit - setup possibly nonblocking commit
1906 * @state: new modeset state to be committed
1907 * @nonblock: whether nonblocking behavior is requested.
1908 *
1909 * This function prepares @state to be used by the atomic helper's support for
1910 * nonblocking commits. Drivers using the nonblocking commit infrastructure
6806cdf9
DV
1911 * should always call this function from their
1912 * &drm_mode_config_funcs.atomic_commit hook.
a095caa7
DV
1913 *
1914 * To be able to use this support drivers need to use a few more helper
1915 * functions. drm_atomic_helper_wait_for_dependencies() must be called before
1916 * actually committing the hardware state, and for nonblocking commits this call
1917 * must be placed in the async worker. See also drm_atomic_helper_swap_state()
1e55a53a 1918 * and its stall parameter, for when a driver's commit hooks look at the
6806cdf9 1919 * &drm_crtc.state, &drm_plane.state or &drm_connector.state pointer directly.
a095caa7
DV
1920 *
1921 * Completion of the hardware commit step must be signalled using
1922 * drm_atomic_helper_commit_hw_done(). After this step the driver is not allowed
1923 * to read or change any permanent software or hardware modeset state. The only
1924 * exception is state protected by other means than &drm_modeset_lock locks.
1925 * Only the free standing @state with pointers to the old state structures can
1926 * be inspected, e.g. to clean up old buffers using
1927 * drm_atomic_helper_cleanup_planes().
1928 *
1929 * At the very end, before cleaning up @state drivers must call
1930 * drm_atomic_helper_commit_cleanup_done().
1931 *
1932 * This is all implemented by in drm_atomic_helper_commit(), giving drivers a
9ac07815 1933 * complete and easy-to-use default implementation of the atomic_commit() hook.
a095caa7
DV
1934 *
1935 * The tracking of asynchronously executed and still pending commits is done
1936 * using the core structure &drm_crtc_commit.
1937 *
1938 * By default there's no need to clean up resources allocated by this function
1939 * explicitly: drm_atomic_state_default_clear() will take care of that
1940 * automatically.
1941 *
1942 * Returns:
1943 *
1944 * 0 on success. -EBUSY when userspace schedules nonblocking commits too fast,
1945 * -ENOMEM on allocation failures and -EINTR when a signal is pending.
1946 */
1947int drm_atomic_helper_setup_commit(struct drm_atomic_state *state,
1948 bool nonblock)
1949{
1950 struct drm_crtc *crtc;
415c3ac3 1951 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
21a01abb
ML
1952 struct drm_connector *conn;
1953 struct drm_connector_state *old_conn_state, *new_conn_state;
1954 struct drm_plane *plane;
1955 struct drm_plane_state *old_plane_state, *new_plane_state;
a095caa7
DV
1956 struct drm_crtc_commit *commit;
1957 int i, ret;
1958
415c3ac3 1959 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
a095caa7
DV
1960 commit = kzalloc(sizeof(*commit), GFP_KERNEL);
1961 if (!commit)
1962 return -ENOMEM;
1963
21a01abb 1964 init_commit(commit, crtc);
a095caa7 1965
163bcc2c 1966 new_crtc_state->commit = commit;
a095caa7
DV
1967
1968 ret = stall_checks(crtc, nonblock);
1969 if (ret)
1970 return ret;
1971
1972 /* Drivers only send out events when at least either current or
1973 * new CRTC state is active. Complete right away if everything
1974 * stays off. */
415c3ac3 1975 if (!old_crtc_state->active && !new_crtc_state->active) {
a095caa7
DV
1976 complete_all(&commit->flip_done);
1977 continue;
1978 }
1979
1980 /* Legacy cursor updates are fully unsynced. */
1981 if (state->legacy_cursor_update) {
1982 complete_all(&commit->flip_done);
1983 continue;
1984 }
1985
415c3ac3 1986 if (!new_crtc_state->event) {
a095caa7
DV
1987 commit->event = kzalloc(sizeof(*commit->event),
1988 GFP_KERNEL);
1989 if (!commit->event)
1990 return -ENOMEM;
1991
415c3ac3 1992 new_crtc_state->event = commit->event;
a095caa7
DV
1993 }
1994
415c3ac3
ML
1995 new_crtc_state->event->base.completion = &commit->flip_done;
1996 new_crtc_state->event->base.completion_release = release_crtc_commit;
24835e44 1997 drm_crtc_commit_get(commit);
1c6ceeee
LSL
1998
1999 commit->abort_completion = true;
4364bcb2
LL
2000
2001 state->crtcs[i].commit = commit;
2002 drm_crtc_commit_get(commit);
a095caa7
DV
2003 }
2004
21a01abb 2005 for_each_oldnew_connector_in_state(state, conn, old_conn_state, new_conn_state, i) {
21a01abb
ML
2006 /* Userspace is not allowed to get ahead of the previous
2007 * commit with nonblocking ones. */
2008 if (nonblock && old_conn_state->commit &&
2009 !try_wait_for_completion(&old_conn_state->commit->flip_done))
2010 return -EBUSY;
a095caa7 2011
1f2d9bdc
DV
2012 /* Always track connectors explicitly for e.g. link retraining. */
2013 commit = crtc_or_fake_commit(state, new_conn_state->crtc ?: old_conn_state->crtc);
21a01abb
ML
2014 if (!commit)
2015 return -ENOMEM;
a095caa7 2016
21a01abb
ML
2017 new_conn_state->commit = drm_crtc_commit_get(commit);
2018 }
2019
2020 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
21a01abb
ML
2021 /* Userspace is not allowed to get ahead of the previous
2022 * commit with nonblocking ones. */
2023 if (nonblock && old_plane_state->commit &&
2024 !try_wait_for_completion(&old_plane_state->commit->flip_done))
2025 return -EBUSY;
2026
1f2d9bdc 2027 /* Always track planes explicitly for async pageflip support. */
4edd6084 2028 commit = crtc_or_fake_commit(state, new_plane_state->crtc ?: old_plane_state->crtc);
21a01abb
ML
2029 if (!commit)
2030 return -ENOMEM;
2031
2032 new_plane_state->commit = drm_crtc_commit_get(commit);
a095caa7
DV
2033 }
2034
a095caa7 2035 return 0;
a095caa7 2036}
a095caa7 2037EXPORT_SYMBOL(drm_atomic_helper_setup_commit);
a095caa7
DV
2038
2039/**
2040 * drm_atomic_helper_wait_for_dependencies - wait for required preceeding commits
1ea0c02e 2041 * @old_state: atomic state object with old state structures
a095caa7
DV
2042 *
2043 * This function waits for all preceeding commits that touch the same CRTC as
1ea0c02e 2044 * @old_state to both be committed to the hardware (as signalled by
a095caa7 2045 * drm_atomic_helper_commit_hw_done) and executed by the hardware (as signalled
277b09cf 2046 * by calling drm_crtc_send_vblank_event() on the &drm_crtc_state.event).
a095caa7
DV
2047 *
2048 * This is part of the atomic helper support for nonblocking commits, see
2049 * drm_atomic_helper_setup_commit() for an overview.
2050 */
1ea0c02e 2051void drm_atomic_helper_wait_for_dependencies(struct drm_atomic_state *old_state)
a095caa7
DV
2052{
2053 struct drm_crtc *crtc;
163bcc2c 2054 struct drm_crtc_state *old_crtc_state;
21a01abb
ML
2055 struct drm_plane *plane;
2056 struct drm_plane_state *old_plane_state;
2057 struct drm_connector *conn;
2058 struct drm_connector_state *old_conn_state;
a095caa7
DV
2059 struct drm_crtc_commit *commit;
2060 int i;
2061 long ret;
2062
163bcc2c
ML
2063 for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
2064 commit = old_crtc_state->commit;
a095caa7
DV
2065
2066 if (!commit)
2067 continue;
2068
2069 ret = wait_for_completion_timeout(&commit->hw_done,
2070 10*HZ);
2071 if (ret == 0)
2072 DRM_ERROR("[CRTC:%d:%s] hw_done timed out\n",
2073 crtc->base.id, crtc->name);
2074
2075 /* Currently no support for overwriting flips, hence
2076 * stall for previous one to execute completely. */
2077 ret = wait_for_completion_timeout(&commit->flip_done,
2078 10*HZ);
2079 if (ret == 0)
2080 DRM_ERROR("[CRTC:%d:%s] flip_done timed out\n",
2081 crtc->base.id, crtc->name);
a095caa7 2082 }
21a01abb
ML
2083
2084 for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
2085 commit = old_conn_state->commit;
a095caa7 2086
21a01abb
ML
2087 if (!commit)
2088 continue;
2089
2090 ret = wait_for_completion_timeout(&commit->hw_done,
2091 10*HZ);
2092 if (ret == 0)
2093 DRM_ERROR("[CONNECTOR:%d:%s] hw_done timed out\n",
2094 conn->base.id, conn->name);
2095
2096 /* Currently no support for overwriting flips, hence
2097 * stall for previous one to execute completely. */
2098 ret = wait_for_completion_timeout(&commit->flip_done,
2099 10*HZ);
2100 if (ret == 0)
2101 DRM_ERROR("[CONNECTOR:%d:%s] flip_done timed out\n",
2102 conn->base.id, conn->name);
2103 }
2104
2105 for_each_old_plane_in_state(old_state, plane, old_plane_state, i) {
2106 commit = old_plane_state->commit;
2107
2108 if (!commit)
2109 continue;
2110
2111 ret = wait_for_completion_timeout(&commit->hw_done,
2112 10*HZ);
2113 if (ret == 0)
2114 DRM_ERROR("[PLANE:%d:%s] hw_done timed out\n",
2115 plane->base.id, plane->name);
2116
2117 /* Currently no support for overwriting flips, hence
2118 * stall for previous one to execute completely. */
2119 ret = wait_for_completion_timeout(&commit->flip_done,
2120 10*HZ);
2121 if (ret == 0)
2122 DRM_ERROR("[PLANE:%d:%s] flip_done timed out\n",
2123 plane->base.id, plane->name);
a095caa7
DV
2124 }
2125}
2126EXPORT_SYMBOL(drm_atomic_helper_wait_for_dependencies);
2127
b25c60af
BB
2128/**
2129 * drm_atomic_helper_fake_vblank - fake VBLANK events if needed
2130 * @old_state: atomic state object with old state structures
2131 *
2132 * This function walks all CRTCs and fake VBLANK events on those with
2133 * &drm_crtc_state.no_vblank set to true and &drm_crtc_state.event != NULL.
2134 * The primary use of this function is writeback connectors working in oneshot
2135 * mode and faking VBLANK events. In this case they only fake the VBLANK event
2136 * when a job is queued, and any change to the pipeline that does not touch the
2137 * connector is leading to timeouts when calling
2138 * drm_atomic_helper_wait_for_vblanks() or
2139 * drm_atomic_helper_wait_for_flip_done().
2140 *
2141 * This is part of the atomic helper support for nonblocking commits, see
2142 * drm_atomic_helper_setup_commit() for an overview.
2143 */
2144void drm_atomic_helper_fake_vblank(struct drm_atomic_state *old_state)
2145{
2146 struct drm_crtc_state *new_crtc_state;
2147 struct drm_crtc *crtc;
2148 int i;
2149
2150 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
2151 unsigned long flags;
2152
2153 if (!new_crtc_state->no_vblank)
2154 continue;
2155
2156 spin_lock_irqsave(&old_state->dev->event_lock, flags);
2157 if (new_crtc_state->event) {
2158 drm_crtc_send_vblank_event(crtc,
2159 new_crtc_state->event);
2160 new_crtc_state->event = NULL;
2161 }
2162 spin_unlock_irqrestore(&old_state->dev->event_lock, flags);
2163 }
2164}
2165EXPORT_SYMBOL(drm_atomic_helper_fake_vblank);
2166
a095caa7
DV
2167/**
2168 * drm_atomic_helper_commit_hw_done - setup possible nonblocking commit
1ea0c02e 2169 * @old_state: atomic state object with old state structures
a095caa7
DV
2170 *
2171 * This function is used to signal completion of the hardware commit step. After
2172 * this step the driver is not allowed to read or change any permanent software
2173 * or hardware modeset state. The only exception is state protected by other
2174 * means than &drm_modeset_lock locks.
2175 *
2176 * Drivers should try to postpone any expensive or delayed cleanup work after
2177 * this function is called.
2178 *
2179 * This is part of the atomic helper support for nonblocking commits, see
2180 * drm_atomic_helper_setup_commit() for an overview.
2181 */
1ea0c02e 2182void drm_atomic_helper_commit_hw_done(struct drm_atomic_state *old_state)
a095caa7
DV
2183{
2184 struct drm_crtc *crtc;
163bcc2c 2185 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
a095caa7
DV
2186 struct drm_crtc_commit *commit;
2187 int i;
2188
163bcc2c
ML
2189 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
2190 commit = new_crtc_state->commit;
a095caa7
DV
2191 if (!commit)
2192 continue;
2193
163bcc2c
ML
2194 /*
2195 * copy new_crtc_state->commit to old_crtc_state->commit,
2196 * it's unsafe to touch new_crtc_state after hw_done,
2197 * but we still need to do so in cleanup_done().
2198 */
2199 if (old_crtc_state->commit)
2200 drm_crtc_commit_put(old_crtc_state->commit);
2201
2202 old_crtc_state->commit = drm_crtc_commit_get(commit);
2203
a095caa7 2204 /* backend must have consumed any event by now */
415c3ac3 2205 WARN_ON(new_crtc_state->event);
a095caa7 2206 complete_all(&commit->hw_done);
a095caa7 2207 }
21a01abb
ML
2208
2209 if (old_state->fake_commit) {
2210 complete_all(&old_state->fake_commit->hw_done);
2211 complete_all(&old_state->fake_commit->flip_done);
2212 }
a095caa7
DV
2213}
2214EXPORT_SYMBOL(drm_atomic_helper_commit_hw_done);
2215
2216/**
2217 * drm_atomic_helper_commit_cleanup_done - signal completion of commit
1ea0c02e 2218 * @old_state: atomic state object with old state structures
a095caa7 2219 *
1ea0c02e
DV
2220 * This signals completion of the atomic update @old_state, including any
2221 * cleanup work. If used, it must be called right before calling
0853695c 2222 * drm_atomic_state_put().
a095caa7
DV
2223 *
2224 * This is part of the atomic helper support for nonblocking commits, see
2225 * drm_atomic_helper_setup_commit() for an overview.
2226 */
1ea0c02e 2227void drm_atomic_helper_commit_cleanup_done(struct drm_atomic_state *old_state)
a095caa7
DV
2228{
2229 struct drm_crtc *crtc;
163bcc2c 2230 struct drm_crtc_state *old_crtc_state;
a095caa7
DV
2231 struct drm_crtc_commit *commit;
2232 int i;
a095caa7 2233
163bcc2c
ML
2234 for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
2235 commit = old_crtc_state->commit;
a095caa7
DV
2236 if (WARN_ON(!commit))
2237 continue;
2238
a095caa7
DV
2239 complete_all(&commit->cleanup_done);
2240 WARN_ON(!try_wait_for_completion(&commit->hw_done));
2241
7141fd3e 2242 spin_lock(&crtc->commit_lock);
a095caa7
DV
2243 list_del(&commit->commit_entry);
2244 spin_unlock(&crtc->commit_lock);
2245 }
21a01abb 2246
10a599fa 2247 if (old_state->fake_commit) {
21a01abb 2248 complete_all(&old_state->fake_commit->cleanup_done);
10a599fa
VS
2249 WARN_ON(!try_wait_for_completion(&old_state->fake_commit->hw_done));
2250 }
a095caa7
DV
2251}
2252EXPORT_SYMBOL(drm_atomic_helper_commit_cleanup_done);
2253
c2fcd274 2254/**
2e3afd47 2255 * drm_atomic_helper_prepare_planes - prepare plane resources before commit
c2fcd274 2256 * @dev: DRM device
2e3afd47 2257 * @state: atomic state object with new state structures
c2fcd274
DV
2258 *
2259 * This function prepares plane state, specifically framebuffers, for the new
6806cdf9
DV
2260 * configuration, by calling &drm_plane_helper_funcs.prepare_fb. If any failure
2261 * is encountered this function will call &drm_plane_helper_funcs.cleanup_fb on
2262 * any already successfully prepared framebuffer.
c2fcd274
DV
2263 *
2264 * Returns:
2265 * 0 on success, negative error code on failure.
2266 */
2267int drm_atomic_helper_prepare_planes(struct drm_device *dev,
2268 struct drm_atomic_state *state)
2269{
9d2230dc
LP
2270 struct drm_connector *connector;
2271 struct drm_connector_state *new_conn_state;
be9174a4 2272 struct drm_plane *plane;
415c3ac3 2273 struct drm_plane_state *new_plane_state;
be9174a4 2274 int ret, i, j;
c2fcd274 2275
9d2230dc
LP
2276 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
2277 if (!new_conn_state->writeback_job)
2278 continue;
2279
2280 ret = drm_writeback_prepare_job(new_conn_state->writeback_job);
2281 if (ret < 0)
2282 return ret;
2283 }
2284
415c3ac3 2285 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
b5ceff20 2286 const struct drm_plane_helper_funcs *funcs;
c2fcd274
DV
2287
2288 funcs = plane->helper_private;
2289
844f9111 2290 if (funcs->prepare_fb) {
415c3ac3 2291 ret = funcs->prepare_fb(plane, new_plane_state);
c2fcd274
DV
2292 if (ret)
2293 goto fail;
2294 }
2295 }
2296
2297 return 0;
2298
2299fail:
415c3ac3 2300 for_each_new_plane_in_state(state, plane, new_plane_state, j) {
b5ceff20 2301 const struct drm_plane_helper_funcs *funcs;
c2fcd274 2302
be9174a4 2303 if (j >= i)
c2fcd274
DV
2304 continue;
2305
2306 funcs = plane->helper_private;
2307
844f9111 2308 if (funcs->cleanup_fb)
415c3ac3 2309 funcs->cleanup_fb(plane, new_plane_state);
c2fcd274
DV
2310 }
2311
2312 return ret;
2313}
2314EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
2315
7135ac54 2316static bool plane_crtc_active(const struct drm_plane_state *state)
aef9dbb8
DV
2317{
2318 return state->crtc && state->crtc->state->active;
2319}
2320
c2fcd274
DV
2321/**
2322 * drm_atomic_helper_commit_planes - commit plane state
2323 * @dev: DRM device
b0fcfc89 2324 * @old_state: atomic state object with old state structures
2b58e98d 2325 * @flags: flags for committing plane state
c2fcd274
DV
2326 *
2327 * This function commits the new plane state using the plane and atomic helper
2328 * functions for planes and crtcs. It assumes that the atomic state has already
2329 * been pushed into the relevant object state pointers, since this step can no
2330 * longer fail.
2331 *
b0fcfc89 2332 * It still requires the global state object @old_state to know which planes and
c2fcd274 2333 * crtcs need to be updated though.
de28d021
ML
2334 *
2335 * Note that this function does all plane updates across all CRTCs in one step.
2336 * If the hardware can't support this approach look at
2337 * drm_atomic_helper_commit_planes_on_crtc() instead.
6e48ae32
DV
2338 *
2339 * Plane parameters can be updated by applications while the associated CRTC is
2340 * disabled. The DRM/KMS core will store the parameters in the plane state,
2341 * which will be available to the driver when the CRTC is turned on. As a result
2342 * most drivers don't need to be immediately notified of plane updates for a
2343 * disabled CRTC.
2344 *
2b58e98d
LY
2345 * Unless otherwise needed, drivers are advised to set the ACTIVE_ONLY flag in
2346 * @flags in order not to receive plane update notifications related to a
2347 * disabled CRTC. This avoids the need to manually ignore plane updates in
6e48ae32
DV
2348 * driver code when the driver and/or hardware can't or just don't need to deal
2349 * with updates on disabled CRTCs, for example when supporting runtime PM.
2350 *
2b58e98d
LY
2351 * Drivers may set the NO_DISABLE_AFTER_MODESET flag in @flags if the relevant
2352 * display controllers require to disable a CRTC's planes when the CRTC is
6806cdf9
DV
2353 * disabled. This function would skip the &drm_plane_helper_funcs.atomic_disable
2354 * call for a plane if the CRTC of the old plane state needs a modesetting
2355 * operation. Of course, the drivers need to disable the planes in their CRTC
2356 * disable callbacks since no one else would do that.
2b58e98d
LY
2357 *
2358 * The drm_atomic_helper_commit() default implementation doesn't set the
2359 * ACTIVE_ONLY flag to most closely match the behaviour of the legacy helpers.
2360 * This should not be copied blindly by drivers.
c2fcd274
DV
2361 */
2362void drm_atomic_helper_commit_planes(struct drm_device *dev,
aef9dbb8 2363 struct drm_atomic_state *old_state,
2b58e98d 2364 uint32_t flags)
c2fcd274 2365{
df63b999 2366 struct drm_crtc *crtc;
415c3ac3 2367 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
df63b999 2368 struct drm_plane *plane;
415c3ac3 2369 struct drm_plane_state *old_plane_state, *new_plane_state;
c2fcd274 2370 int i;
2b58e98d
LY
2371 bool active_only = flags & DRM_PLANE_COMMIT_ACTIVE_ONLY;
2372 bool no_disable = flags & DRM_PLANE_COMMIT_NO_DISABLE_AFTER_MODESET;
c2fcd274 2373
415c3ac3 2374 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
b5ceff20 2375 const struct drm_crtc_helper_funcs *funcs;
c2fcd274
DV
2376
2377 funcs = crtc->helper_private;
2378
2379 if (!funcs || !funcs->atomic_begin)
2380 continue;
2381
415c3ac3 2382 if (active_only && !new_crtc_state->active)
aef9dbb8
DV
2383 continue;
2384
613d2b27 2385 funcs->atomic_begin(crtc, old_crtc_state);
c2fcd274
DV
2386 }
2387
415c3ac3 2388 for_each_oldnew_plane_in_state(old_state, plane, old_plane_state, new_plane_state, i) {
b5ceff20 2389 const struct drm_plane_helper_funcs *funcs;
216c59d6 2390 bool disabling;
c2fcd274
DV
2391
2392 funcs = plane->helper_private;
2393
3cad4b68 2394 if (!funcs)
c2fcd274
DV
2395 continue;
2396
51ffa12d
ML
2397 disabling = drm_atomic_plane_disabling(old_plane_state,
2398 new_plane_state);
216c59d6
LP
2399
2400 if (active_only) {
2401 /*
2402 * Skip planes related to inactive CRTCs. If the plane
2403 * is enabled use the state of the current CRTC. If the
2404 * plane is being disabled use the state of the old
2405 * CRTC to avoid skipping planes being disabled on an
2406 * active CRTC.
2407 */
415c3ac3 2408 if (!disabling && !plane_crtc_active(new_plane_state))
216c59d6
LP
2409 continue;
2410 if (disabling && !plane_crtc_active(old_plane_state))
2411 continue;
2412 }
aef9dbb8 2413
407b8bd9
TR
2414 /*
2415 * Special-case disabling the plane if drivers support it.
2416 */
2b58e98d
LY
2417 if (disabling && funcs->atomic_disable) {
2418 struct drm_crtc_state *crtc_state;
2419
2420 crtc_state = old_plane_state->crtc->state;
2421
2422 if (drm_atomic_crtc_needs_modeset(crtc_state) &&
2423 no_disable)
2424 continue;
2425
407b8bd9 2426 funcs->atomic_disable(plane, old_plane_state);
415c3ac3 2427 } else if (new_plane_state->crtc || disabling) {
407b8bd9 2428 funcs->atomic_update(plane, old_plane_state);
2b58e98d 2429 }
c2fcd274
DV
2430 }
2431
415c3ac3 2432 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
b5ceff20 2433 const struct drm_crtc_helper_funcs *funcs;
c2fcd274
DV
2434
2435 funcs = crtc->helper_private;
2436
2437 if (!funcs || !funcs->atomic_flush)
2438 continue;
2439
415c3ac3 2440 if (active_only && !new_crtc_state->active)
aef9dbb8
DV
2441 continue;
2442
613d2b27 2443 funcs->atomic_flush(crtc, old_crtc_state);
c2fcd274
DV
2444 }
2445}
2446EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
2447
de28d021
ML
2448/**
2449 * drm_atomic_helper_commit_planes_on_crtc - commit plane state for a crtc
2450 * @old_crtc_state: atomic state object with the old crtc state
2451 *
2452 * This function commits the new plane state using the plane and atomic helper
2453 * functions for planes on the specific crtc. It assumes that the atomic state
2454 * has already been pushed into the relevant object state pointers, since this
2455 * step can no longer fail.
2456 *
2457 * This function is useful when plane updates should be done crtc-by-crtc
2458 * instead of one global step like drm_atomic_helper_commit_planes() does.
2459 *
2460 * This function can only be savely used when planes are not allowed to move
2461 * between different CRTCs because this function doesn't handle inter-CRTC
2462 * depencies. Callers need to ensure that either no such depencies exist,
2463 * resolve them through ordering of commit calls or through some other means.
2464 */
2465void
2466drm_atomic_helper_commit_planes_on_crtc(struct drm_crtc_state *old_crtc_state)
2467{
2468 const struct drm_crtc_helper_funcs *crtc_funcs;
2469 struct drm_crtc *crtc = old_crtc_state->crtc;
2470 struct drm_atomic_state *old_state = old_crtc_state->state;
e35a2f9a
VS
2471 struct drm_crtc_state *new_crtc_state =
2472 drm_atomic_get_new_crtc_state(old_state, crtc);
de28d021
ML
2473 struct drm_plane *plane;
2474 unsigned plane_mask;
2475
2476 plane_mask = old_crtc_state->plane_mask;
e35a2f9a 2477 plane_mask |= new_crtc_state->plane_mask;
de28d021
ML
2478
2479 crtc_funcs = crtc->helper_private;
2480 if (crtc_funcs && crtc_funcs->atomic_begin)
613d2b27 2481 crtc_funcs->atomic_begin(crtc, old_crtc_state);
de28d021
ML
2482
2483 drm_for_each_plane_mask(plane, crtc->dev, plane_mask) {
2484 struct drm_plane_state *old_plane_state =
b4d93679 2485 drm_atomic_get_old_plane_state(old_state, plane);
e35a2f9a
VS
2486 struct drm_plane_state *new_plane_state =
2487 drm_atomic_get_new_plane_state(old_state, plane);
de28d021
ML
2488 const struct drm_plane_helper_funcs *plane_funcs;
2489
2490 plane_funcs = plane->helper_private;
2491
2492 if (!old_plane_state || !plane_funcs)
2493 continue;
2494
e35a2f9a
VS
2495 WARN_ON(new_plane_state->crtc &&
2496 new_plane_state->crtc != crtc);
de28d021 2497
e35a2f9a 2498 if (drm_atomic_plane_disabling(old_plane_state, new_plane_state) &&
de28d021
ML
2499 plane_funcs->atomic_disable)
2500 plane_funcs->atomic_disable(plane, old_plane_state);
e35a2f9a
VS
2501 else if (new_plane_state->crtc ||
2502 drm_atomic_plane_disabling(old_plane_state, new_plane_state))
de28d021
ML
2503 plane_funcs->atomic_update(plane, old_plane_state);
2504 }
2505
2506 if (crtc_funcs && crtc_funcs->atomic_flush)
613d2b27 2507 crtc_funcs->atomic_flush(crtc, old_crtc_state);
de28d021
ML
2508}
2509EXPORT_SYMBOL(drm_atomic_helper_commit_planes_on_crtc);
2510
6753ba97
JS
2511/**
2512 * drm_atomic_helper_disable_planes_on_crtc - helper to disable CRTC's planes
28500291 2513 * @old_crtc_state: atomic state object with the old CRTC state
6753ba97
JS
2514 * @atomic: if set, synchronize with CRTC's atomic_begin/flush hooks
2515 *
2516 * Disables all planes associated with the given CRTC. This can be
28500291
LY
2517 * used for instance in the CRTC helper atomic_disable callback to disable
2518 * all planes.
6753ba97
JS
2519 *
2520 * If the atomic-parameter is set the function calls the CRTC's
2521 * atomic_begin hook before and atomic_flush hook after disabling the
2522 * planes.
2523 *
2524 * It is a bug to call this function without having implemented the
6806cdf9 2525 * &drm_plane_helper_funcs.atomic_disable plane hook.
6753ba97 2526 */
28500291
LY
2527void
2528drm_atomic_helper_disable_planes_on_crtc(struct drm_crtc_state *old_crtc_state,
2529 bool atomic)
6753ba97 2530{
28500291 2531 struct drm_crtc *crtc = old_crtc_state->crtc;
6753ba97
JS
2532 const struct drm_crtc_helper_funcs *crtc_funcs =
2533 crtc->helper_private;
2534 struct drm_plane *plane;
2535
2536 if (atomic && crtc_funcs && crtc_funcs->atomic_begin)
2537 crtc_funcs->atomic_begin(crtc, NULL);
2538
28500291 2539 drm_atomic_crtc_state_for_each_plane(plane, old_crtc_state) {
6753ba97
JS
2540 const struct drm_plane_helper_funcs *plane_funcs =
2541 plane->helper_private;
2542
28500291 2543 if (!plane_funcs)
6753ba97
JS
2544 continue;
2545
2546 WARN_ON(!plane_funcs->atomic_disable);
2547 if (plane_funcs->atomic_disable)
2548 plane_funcs->atomic_disable(plane, NULL);
2549 }
2550
2551 if (atomic && crtc_funcs && crtc_funcs->atomic_flush)
2552 crtc_funcs->atomic_flush(crtc, NULL);
2553}
2554EXPORT_SYMBOL(drm_atomic_helper_disable_planes_on_crtc);
2555
c2fcd274
DV
2556/**
2557 * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
2558 * @dev: DRM device
2559 * @old_state: atomic state object with old state structures
2560 *
2561 * This function cleans up plane state, specifically framebuffers, from the old
2562 * configuration. Hence the old configuration must be perserved in @old_state to
2563 * be able to call this function.
2564 *
2565 * This function must also be called on the new state when the atomic update
2566 * fails at any point after calling drm_atomic_helper_prepare_planes().
2567 */
2568void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
2569 struct drm_atomic_state *old_state)
2570{
df63b999 2571 struct drm_plane *plane;
415c3ac3 2572 struct drm_plane_state *old_plane_state, *new_plane_state;
c2fcd274
DV
2573 int i;
2574
415c3ac3 2575 for_each_oldnew_plane_in_state(old_state, plane, old_plane_state, new_plane_state, i) {
b5ceff20 2576 const struct drm_plane_helper_funcs *funcs;
415c3ac3
ML
2577 struct drm_plane_state *plane_state;
2578
2579 /*
2580 * This might be called before swapping when commit is aborted,
2581 * in which case we have to cleanup the new state.
2582 */
2583 if (old_plane_state == plane->state)
2584 plane_state = new_plane_state;
2585 else
2586 plane_state = old_plane_state;
c2fcd274 2587
c2fcd274
DV
2588 funcs = plane->helper_private;
2589
844f9111
ML
2590 if (funcs->cleanup_fb)
2591 funcs->cleanup_fb(plane, plane_state);
c2fcd274
DV
2592 }
2593}
2594EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
2595
2596/**
2597 * drm_atomic_helper_swap_state - store atomic state into current sw state
c2fcd274 2598 * @state: atomic state
c066d231 2599 * @stall: stall for preceeding commits
c2fcd274
DV
2600 *
2601 * This function stores the atomic state into the current state pointers in all
2602 * driver objects. It should be called after all failing steps have been done
2603 * and succeeded, but before the actual hardware state is committed.
2604 *
2605 * For cleanup and error recovery the current state for all changed objects will
c066d231 2606 * be swapped into @state.
c2fcd274
DV
2607 *
2608 * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
2609 *
2610 * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
2611 *
2612 * 2. Do any other steps that might fail.
2613 *
2614 * 3. Put the staged state into the current state pointers with this function.
2615 *
2616 * 4. Actually commit the hardware state.
2617 *
26196f7e 2618 * 5. Call drm_atomic_helper_cleanup_planes() with @state, which since step 3
c2fcd274 2619 * contains the old state. Also do any other cleanup required with that state.
a095caa7
DV
2620 *
2621 * @stall must be set when nonblocking commits for this driver directly access
6806cdf9
DV
2622 * the &drm_plane.state, &drm_crtc.state or &drm_connector.state pointer. With
2623 * the current atomic helpers this is almost always the case, since the helpers
a095caa7 2624 * don't pass the right state structures to the callbacks.
c066d231
ML
2625 *
2626 * Returns:
2627 *
c4bbb735
ML
2628 * Returns 0 on success. Can return -ERESTARTSYS when @stall is true and the
2629 * waiting for the previous commits has been interrupted.
c2fcd274 2630 */
c066d231 2631int drm_atomic_helper_swap_state(struct drm_atomic_state *state,
5e84c269 2632 bool stall)
c2fcd274 2633{
c4bbb735 2634 int i, ret;
be9174a4 2635 struct drm_connector *connector;
415c3ac3 2636 struct drm_connector_state *old_conn_state, *new_conn_state;
be9174a4 2637 struct drm_crtc *crtc;
415c3ac3 2638 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
be9174a4 2639 struct drm_plane *plane;
415c3ac3 2640 struct drm_plane_state *old_plane_state, *new_plane_state;
a095caa7 2641 struct drm_crtc_commit *commit;
a4370c77
VS
2642 struct drm_private_obj *obj;
2643 struct drm_private_state *old_obj_state, *new_obj_state;
a095caa7
DV
2644
2645 if (stall) {
21a01abb
ML
2646 /*
2647 * We have to stall for hw_done here before
2648 * drm_atomic_helper_wait_for_dependencies() because flip
2649 * depth > 1 is not yet supported by all drivers. As long as
2650 * obj->state is directly dereferenced anywhere in the drivers
2651 * atomic_commit_tail function, then it's unsafe to swap state
2652 * before drm_atomic_helper_commit_hw_done() is called.
2653 */
2654
163bcc2c
ML
2655 for_each_old_crtc_in_state(state, crtc, old_crtc_state, i) {
2656 commit = old_crtc_state->commit;
a095caa7
DV
2657
2658 if (!commit)
2659 continue;
2660
c4bbb735 2661 ret = wait_for_completion_interruptible(&commit->hw_done);
c4bbb735
ML
2662 if (ret)
2663 return ret;
a095caa7 2664 }
21a01abb
ML
2665
2666 for_each_old_connector_in_state(state, connector, old_conn_state, i) {
2667 commit = old_conn_state->commit;
a095caa7
DV
2668
2669 if (!commit)
2670 continue;
2671
c4bbb735 2672 ret = wait_for_completion_interruptible(&commit->hw_done);
21a01abb
ML
2673 if (ret)
2674 return ret;
2675 }
c4bbb735 2676
21a01abb
ML
2677 for_each_old_plane_in_state(state, plane, old_plane_state, i) {
2678 commit = old_plane_state->commit;
2679
2680 if (!commit)
2681 continue;
2682
2683 ret = wait_for_completion_interruptible(&commit->hw_done);
c4bbb735
ML
2684 if (ret)
2685 return ret;
a095caa7
DV
2686 }
2687 }
c2fcd274 2688
415c3ac3 2689 for_each_oldnew_connector_in_state(state, connector, old_conn_state, new_conn_state, i) {
581e49fe
ML
2690 WARN_ON(connector->state != old_conn_state);
2691
415c3ac3
ML
2692 old_conn_state->state = state;
2693 new_conn_state->state = NULL;
2694
2695 state->connectors[i].state = old_conn_state;
2696 connector->state = new_conn_state;
c2fcd274
DV
2697 }
2698
415c3ac3 2699 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
581e49fe
ML
2700 WARN_ON(crtc->state != old_crtc_state);
2701
415c3ac3
ML
2702 old_crtc_state->state = state;
2703 new_crtc_state->state = NULL;
2704
2705 state->crtcs[i].state = old_crtc_state;
2706 crtc->state = new_crtc_state;
a095caa7 2707
163bcc2c 2708 if (new_crtc_state->commit) {
a095caa7 2709 spin_lock(&crtc->commit_lock);
163bcc2c 2710 list_add(&new_crtc_state->commit->commit_entry,
a095caa7
DV
2711 &crtc->commit_list);
2712 spin_unlock(&crtc->commit_lock);
2713
163bcc2c 2714 new_crtc_state->commit->event = NULL;
a095caa7 2715 }
c2fcd274
DV
2716 }
2717
415c3ac3 2718 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
581e49fe
ML
2719 WARN_ON(plane->state != old_plane_state);
2720
415c3ac3
ML
2721 old_plane_state->state = state;
2722 new_plane_state->state = NULL;
2723
2724 state->planes[i].state = old_plane_state;
2725 plane->state = new_plane_state;
c2fcd274 2726 }
b430c27a 2727
a4370c77
VS
2728 for_each_oldnew_private_obj_in_state(state, obj, old_obj_state, new_obj_state, i) {
2729 WARN_ON(obj->state != old_obj_state);
2730
2731 old_obj_state->state = state;
2732 new_obj_state->state = NULL;
2733
2734 state->private_objs[i].state = old_obj_state;
2735 obj->state = new_obj_state;
2736 }
c066d231
ML
2737
2738 return 0;
c2fcd274
DV
2739}
2740EXPORT_SYMBOL(drm_atomic_helper_swap_state);
042652ed
DV
2741
2742/**
2743 * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
2744 * @plane: plane object to update
2745 * @crtc: owning CRTC of owning plane
2746 * @fb: framebuffer to flip onto plane
2747 * @crtc_x: x offset of primary plane on crtc
2748 * @crtc_y: y offset of primary plane on crtc
2749 * @crtc_w: width of primary plane rectangle on crtc
2750 * @crtc_h: height of primary plane rectangle on crtc
2751 * @src_x: x offset of @fb for panning
2752 * @src_y: y offset of @fb for panning
2753 * @src_w: width of source rectangle in @fb
2754 * @src_h: height of source rectangle in @fb
34a2ab5e 2755 * @ctx: lock acquire context
042652ed
DV
2756 *
2757 * Provides a default plane update handler using the atomic driver interface.
2758 *
2759 * RETURNS:
2760 * Zero on success, error code on failure
2761 */
2762int drm_atomic_helper_update_plane(struct drm_plane *plane,
2763 struct drm_crtc *crtc,
2764 struct drm_framebuffer *fb,
2765 int crtc_x, int crtc_y,
2766 unsigned int crtc_w, unsigned int crtc_h,
2767 uint32_t src_x, uint32_t src_y,
34a2ab5e
DV
2768 uint32_t src_w, uint32_t src_h,
2769 struct drm_modeset_acquire_ctx *ctx)
042652ed
DV
2770{
2771 struct drm_atomic_state *state;
2772 struct drm_plane_state *plane_state;
2773 int ret = 0;
2774
2775 state = drm_atomic_state_alloc(plane->dev);
2776 if (!state)
2777 return -ENOMEM;
2778
d26f96c7 2779 state->acquire_ctx = ctx;
042652ed
DV
2780 plane_state = drm_atomic_get_plane_state(state, plane);
2781 if (IS_ERR(plane_state)) {
2782 ret = PTR_ERR(plane_state);
2783 goto fail;
2784 }
2785
07cc0ef6 2786 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
042652ed
DV
2787 if (ret != 0)
2788 goto fail;
321ebf04 2789 drm_atomic_set_fb_for_plane(plane_state, fb);
042652ed
DV
2790 plane_state->crtc_x = crtc_x;
2791 plane_state->crtc_y = crtc_y;
042652ed 2792 plane_state->crtc_w = crtc_w;
02e6f379 2793 plane_state->crtc_h = crtc_h;
042652ed
DV
2794 plane_state->src_x = src_x;
2795 plane_state->src_y = src_y;
042652ed 2796 plane_state->src_w = src_w;
02e6f379 2797 plane_state->src_h = src_h;
042652ed 2798
3671c580
DV
2799 if (plane == crtc->cursor)
2800 state->legacy_cursor_update = true;
2801
042652ed 2802 ret = drm_atomic_commit(state);
042652ed 2803fail:
0853695c 2804 drm_atomic_state_put(state);
042652ed 2805 return ret;
042652ed
DV
2806}
2807EXPORT_SYMBOL(drm_atomic_helper_update_plane);
2808
2809/**
2810 * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
2811 * @plane: plane to disable
19315294 2812 * @ctx: lock acquire context
042652ed
DV
2813 *
2814 * Provides a default plane disable handler using the atomic driver interface.
2815 *
2816 * RETURNS:
2817 * Zero on success, error code on failure
2818 */
19315294
DV
2819int drm_atomic_helper_disable_plane(struct drm_plane *plane,
2820 struct drm_modeset_acquire_ctx *ctx)
042652ed
DV
2821{
2822 struct drm_atomic_state *state;
2823 struct drm_plane_state *plane_state;
2824 int ret = 0;
2825
2826 state = drm_atomic_state_alloc(plane->dev);
2827 if (!state)
2828 return -ENOMEM;
2829
d26f96c7 2830 state->acquire_ctx = ctx;
042652ed
DV
2831 plane_state = drm_atomic_get_plane_state(state, plane);
2832 if (IS_ERR(plane_state)) {
2833 ret = PTR_ERR(plane_state);
2834 goto fail;
2835 }
2836
a36c027d 2837 if (plane_state->crtc && plane_state->crtc->cursor == plane)
24e79d0d
ML
2838 plane_state->state->legacy_cursor_update = true;
2839
bbb1e524 2840 ret = __drm_atomic_helper_disable_plane(plane, plane_state);
042652ed
DV
2841 if (ret != 0)
2842 goto fail;
f02ad907 2843
042652ed 2844 ret = drm_atomic_commit(state);
042652ed 2845fail:
0853695c 2846 drm_atomic_state_put(state);
042652ed 2847 return ret;
042652ed
DV
2848}
2849EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
2850
042652ed
DV
2851/**
2852 * drm_atomic_helper_set_config - set a new config from userspace
2853 * @set: mode set configuration
a4eff9aa 2854 * @ctx: lock acquisition context
042652ed
DV
2855 *
2856 * Provides a default crtc set_config handler using the atomic driver interface.
2857 *
40ee6fbe
MN
2858 * NOTE: For backwards compatibility with old userspace this automatically
2859 * resets the "link-status" property to GOOD, to force any link
2860 * re-training. The SETCRTC ioctl does not define whether an update does
2861 * need a full modeset or just a plane update, hence we're allowed to do
97e14fbe 2862 * that. See also drm_connector_set_link_status_property().
40ee6fbe 2863 *
042652ed
DV
2864 * Returns:
2865 * Returns 0 on success, negative errno numbers on failure.
2866 */
a4eff9aa
DV
2867int drm_atomic_helper_set_config(struct drm_mode_set *set,
2868 struct drm_modeset_acquire_ctx *ctx)
042652ed
DV
2869{
2870 struct drm_atomic_state *state;
2871 struct drm_crtc *crtc = set->crtc;
042652ed
DV
2872 int ret = 0;
2873
2874 state = drm_atomic_state_alloc(crtc->dev);
2875 if (!state)
2876 return -ENOMEM;
2877
38b6441e 2878 state->acquire_ctx = ctx;
bbb1e524
RC
2879 ret = __drm_atomic_helper_set_config(set, state);
2880 if (ret != 0)
1fa4da04 2881 goto fail;
042652ed 2882
44596b8c
ML
2883 ret = handle_conflicting_encoders(state, true);
2884 if (ret)
2885 return ret;
2886
bbb1e524 2887 ret = drm_atomic_commit(state);
bbb1e524 2888
1fa4da04 2889fail:
0853695c 2890 drm_atomic_state_put(state);
bbb1e524 2891 return ret;
bbb1e524
RC
2892}
2893EXPORT_SYMBOL(drm_atomic_helper_set_config);
2894
37406a60
SP
2895/**
2896 * drm_atomic_helper_disable_all - disable all currently active outputs
2897 * @dev: DRM device
2898 * @ctx: lock acquisition context
2899 *
2900 * Loops through all connectors, finding those that aren't turned off and then
2901 * turns them off by setting their DPMS mode to OFF and deactivating the CRTC
2902 * that they are connected to.
2903 *
2904 * This is used for example in suspend/resume to disable all currently active
2905 * functions when suspending. If you just want to shut down everything at e.g.
2906 * driver unload, look at drm_atomic_helper_shutdown().
2907 *
2908 * Note that if callers haven't already acquired all modeset locks this might
2909 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
2910 *
2911 * Returns:
2912 * 0 on success or a negative error code on failure.
2913 *
2914 * See also:
2915 * drm_atomic_helper_suspend(), drm_atomic_helper_resume() and
2916 * drm_atomic_helper_shutdown().
2917 */
2918int drm_atomic_helper_disable_all(struct drm_device *dev,
2919 struct drm_modeset_acquire_ctx *ctx)
14942760
TR
2920{
2921 struct drm_atomic_state *state;
9b2104f4 2922 struct drm_connector_state *conn_state;
14942760 2923 struct drm_connector *conn;
9b2104f4
ML
2924 struct drm_plane_state *plane_state;
2925 struct drm_plane *plane;
2926 struct drm_crtc_state *crtc_state;
2927 struct drm_crtc *crtc;
2928 int ret, i;
14942760
TR
2929
2930 state = drm_atomic_state_alloc(dev);
2931 if (!state)
2932 return -ENOMEM;
2933
2934 state->acquire_ctx = ctx;
2935
9b2104f4 2936 drm_for_each_crtc(crtc, dev) {
14942760
TR
2937 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2938 if (IS_ERR(crtc_state)) {
9b2104f4 2939 ret = PTR_ERR(crtc_state);
14942760
TR
2940 goto free;
2941 }
2942
2943 crtc_state->active = false;
9b2104f4
ML
2944
2945 ret = drm_atomic_set_mode_prop_for_crtc(crtc_state, NULL);
2946 if (ret < 0)
2947 goto free;
2948
2949 ret = drm_atomic_add_affected_planes(state, crtc);
2950 if (ret < 0)
2951 goto free;
2952
2953 ret = drm_atomic_add_affected_connectors(state, crtc);
2954 if (ret < 0)
2955 goto free;
2956 }
2957
dfb8bb3b 2958 for_each_new_connector_in_state(state, conn, conn_state, i) {
9b2104f4
ML
2959 ret = drm_atomic_set_crtc_for_connector(conn_state, NULL);
2960 if (ret < 0)
2961 goto free;
2962 }
2963
dfb8bb3b 2964 for_each_new_plane_in_state(state, plane, plane_state, i) {
9b2104f4
ML
2965 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
2966 if (ret < 0)
2967 goto free;
2968
2969 drm_atomic_set_fb_for_plane(plane_state, NULL);
14942760
TR
2970 }
2971
9b2104f4 2972 ret = drm_atomic_commit(state);
14942760 2973free:
0853695c 2974 drm_atomic_state_put(state);
9b2104f4 2975 return ret;
14942760
TR
2976}
2977EXPORT_SYMBOL(drm_atomic_helper_disable_all);
2978
18dddadc
DV
2979/**
2980 * drm_atomic_helper_shutdown - shutdown all CRTC
2981 * @dev: DRM device
2982 *
2983 * This shuts down all CRTC, which is useful for driver unloading. Shutdown on
2984 * suspend should instead be handled with drm_atomic_helper_suspend(), since
2985 * that also takes a snapshot of the modeset state to be restored on resume.
2986 *
2987 * This is just a convenience wrapper around drm_atomic_helper_disable_all(),
2988 * and it is the atomic version of drm_crtc_force_disable_all().
2989 */
2990void drm_atomic_helper_shutdown(struct drm_device *dev)
2991{
2992 struct drm_modeset_acquire_ctx ctx;
2993 int ret;
2994
b7ea04d2 2995 DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx, 0, ret);
18dddadc 2996
37406a60 2997 ret = drm_atomic_helper_disable_all(dev, &ctx);
18dddadc
DV
2998 if (ret)
2999 DRM_ERROR("Disabling all crtc's during unload failed with %i\n", ret);
3000
b7ea04d2 3001 DRM_MODESET_LOCK_ALL_END(ctx, ret);
18dddadc
DV
3002}
3003EXPORT_SYMBOL(drm_atomic_helper_shutdown);
3004
1d8224e7
DV
3005/**
3006 * drm_atomic_helper_duplicate_state - duplicate an atomic state object
3007 * @dev: DRM device
3008 * @ctx: lock acquisition context
3009 *
3010 * Makes a copy of the current atomic state by looping over all objects and
3011 * duplicating their respective states. This is used for example by suspend/
3012 * resume support code to save the state prior to suspend such that it can
3013 * be restored upon resume.
3014 *
3015 * Note that this treats atomic state as persistent between save and restore.
3016 * Drivers must make sure that this is possible and won't result in confusion
3017 * or erroneous behaviour.
3018 *
3019 * Note that if callers haven't already acquired all modeset locks this might
3020 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
3021 *
3022 * Returns:
3023 * A pointer to the copy of the atomic state object on success or an
3024 * ERR_PTR()-encoded error code on failure.
3025 *
3026 * See also:
3027 * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
3028 */
3029struct drm_atomic_state *
3030drm_atomic_helper_duplicate_state(struct drm_device *dev,
3031 struct drm_modeset_acquire_ctx *ctx)
3032{
3033 struct drm_atomic_state *state;
3034 struct drm_connector *conn;
3035 struct drm_connector_list_iter conn_iter;
3036 struct drm_plane *plane;
3037 struct drm_crtc *crtc;
3038 int err = 0;
3039
3040 state = drm_atomic_state_alloc(dev);
3041 if (!state)
3042 return ERR_PTR(-ENOMEM);
3043
3044 state->acquire_ctx = ctx;
022debad 3045 state->duplicated = true;
1d8224e7
DV
3046
3047 drm_for_each_crtc(crtc, dev) {
3048 struct drm_crtc_state *crtc_state;
3049
3050 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3051 if (IS_ERR(crtc_state)) {
3052 err = PTR_ERR(crtc_state);
3053 goto free;
3054 }
3055 }
3056
3057 drm_for_each_plane(plane, dev) {
3058 struct drm_plane_state *plane_state;
3059
3060 plane_state = drm_atomic_get_plane_state(state, plane);
3061 if (IS_ERR(plane_state)) {
3062 err = PTR_ERR(plane_state);
3063 goto free;
3064 }
3065 }
3066
3067 drm_connector_list_iter_begin(dev, &conn_iter);
3068 drm_for_each_connector_iter(conn, &conn_iter) {
3069 struct drm_connector_state *conn_state;
3070
3071 conn_state = drm_atomic_get_connector_state(state, conn);
3072 if (IS_ERR(conn_state)) {
3073 err = PTR_ERR(conn_state);
3074 drm_connector_list_iter_end(&conn_iter);
3075 goto free;
3076 }
3077 }
3078 drm_connector_list_iter_end(&conn_iter);
3079
3080 /* clear the acquire context so that it isn't accidentally reused */
3081 state->acquire_ctx = NULL;
3082
3083free:
3084 if (err < 0) {
3085 drm_atomic_state_put(state);
3086 state = ERR_PTR(err);
3087 }
3088
3089 return state;
3090}
3091EXPORT_SYMBOL(drm_atomic_helper_duplicate_state);
3092
14942760
TR
3093/**
3094 * drm_atomic_helper_suspend - subsystem-level suspend helper
3095 * @dev: DRM device
3096 *
3097 * Duplicates the current atomic state, disables all active outputs and then
3098 * returns a pointer to the original atomic state to the caller. Drivers can
3099 * pass this pointer to the drm_atomic_helper_resume() helper upon resume to
3100 * restore the output configuration that was active at the time the system
3101 * entered suspend.
3102 *
3103 * Note that it is potentially unsafe to use this. The atomic state object
3104 * returned by this function is assumed to be persistent. Drivers must ensure
3105 * that this holds true. Before calling this function, drivers must make sure
3106 * to suspend fbdev emulation so that nothing can be using the device.
3107 *
3108 * Returns:
3109 * A pointer to a copy of the state before suspend on success or an ERR_PTR()-
3110 * encoded error code on failure. Drivers should store the returned atomic
3111 * state object and pass it to the drm_atomic_helper_resume() helper upon
3112 * resume.
3113 *
3114 * See also:
3115 * drm_atomic_helper_duplicate_state(), drm_atomic_helper_disable_all(),
581e49fe 3116 * drm_atomic_helper_resume(), drm_atomic_helper_commit_duplicated_state()
14942760
TR
3117 */
3118struct drm_atomic_state *drm_atomic_helper_suspend(struct drm_device *dev)
3119{
3120 struct drm_modeset_acquire_ctx ctx;
3121 struct drm_atomic_state *state;
3122 int err;
3123
615aa3d9
SP
3124 /* This can never be returned, but it makes the compiler happy */
3125 state = ERR_PTR(-EINVAL);
14942760 3126
b7ea04d2 3127 DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx, 0, err);
14942760
TR
3128
3129 state = drm_atomic_helper_duplicate_state(dev, &ctx);
3130 if (IS_ERR(state))
3131 goto unlock;
3132
3133 err = drm_atomic_helper_disable_all(dev, &ctx);
3134 if (err < 0) {
0853695c 3135 drm_atomic_state_put(state);
14942760
TR
3136 state = ERR_PTR(err);
3137 goto unlock;
3138 }
3139
3140unlock:
b7ea04d2
SP
3141 DRM_MODESET_LOCK_ALL_END(ctx, err);
3142 if (err)
3143 return ERR_PTR(err);
14942760 3144
14942760
TR
3145 return state;
3146}
3147EXPORT_SYMBOL(drm_atomic_helper_suspend);
3148
581e49fe
ML
3149/**
3150 * drm_atomic_helper_commit_duplicated_state - commit duplicated state
3151 * @state: duplicated atomic state to commit
3152 * @ctx: pointer to acquire_ctx to use for commit.
3153 *
3154 * The state returned by drm_atomic_helper_duplicate_state() and
3155 * drm_atomic_helper_suspend() is partially invalid, and needs to
3156 * be fixed up before commit.
3157 *
3158 * Returns:
3159 * 0 on success or a negative error code on failure.
3160 *
3161 * See also:
3162 * drm_atomic_helper_suspend()
3163 */
3164int drm_atomic_helper_commit_duplicated_state(struct drm_atomic_state *state,
3165 struct drm_modeset_acquire_ctx *ctx)
3166{
aa394b0d 3167 int i, ret;
581e49fe 3168 struct drm_plane *plane;
415c3ac3 3169 struct drm_plane_state *new_plane_state;
581e49fe 3170 struct drm_connector *connector;
415c3ac3 3171 struct drm_connector_state *new_conn_state;
581e49fe 3172 struct drm_crtc *crtc;
415c3ac3 3173 struct drm_crtc_state *new_crtc_state;
581e49fe
ML
3174
3175 state->acquire_ctx = ctx;
3176
e00fb856 3177 for_each_new_plane_in_state(state, plane, new_plane_state, i)
581e49fe
ML
3178 state->planes[i].old_state = plane->state;
3179
415c3ac3 3180 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i)
581e49fe
ML
3181 state->crtcs[i].old_state = crtc->state;
3182
415c3ac3 3183 for_each_new_connector_in_state(state, connector, new_conn_state, i)
581e49fe
ML
3184 state->connectors[i].old_state = connector->state;
3185
aa394b0d
SP
3186 ret = drm_atomic_commit(state);
3187
3188 state->acquire_ctx = NULL;
3189
3190 return ret;
581e49fe
ML
3191}
3192EXPORT_SYMBOL(drm_atomic_helper_commit_duplicated_state);
3193
14942760
TR
3194/**
3195 * drm_atomic_helper_resume - subsystem-level resume helper
3196 * @dev: DRM device
3197 * @state: atomic state to resume to
3198 *
3199 * Calls drm_mode_config_reset() to synchronize hardware and software states,
3200 * grabs all modeset locks and commits the atomic state object. This can be
3201 * used in conjunction with the drm_atomic_helper_suspend() helper to
3202 * implement suspend/resume for drivers that support atomic mode-setting.
3203 *
3204 * Returns:
3205 * 0 on success or a negative error code on failure.
3206 *
3207 * See also:
3208 * drm_atomic_helper_suspend()
3209 */
3210int drm_atomic_helper_resume(struct drm_device *dev,
3211 struct drm_atomic_state *state)
3212{
a5b8444e 3213 struct drm_modeset_acquire_ctx ctx;
14942760
TR
3214 int err;
3215
3216 drm_mode_config_reset(dev);
581e49fe 3217
b7ea04d2 3218 DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx, 0, err);
869e188a 3219
b7ea04d2 3220 err = drm_atomic_helper_commit_duplicated_state(state, &ctx);
a5b8444e 3221
b7ea04d2 3222 DRM_MODESET_LOCK_ALL_END(ctx, err);
6d281b1f 3223 drm_atomic_state_put(state);
14942760
TR
3224
3225 return err;
3226}
3227EXPORT_SYMBOL(drm_atomic_helper_resume);
3228
8c3a8181
DV
3229static int page_flip_common(struct drm_atomic_state *state,
3230 struct drm_crtc *crtc,
3231 struct drm_framebuffer *fb,
3232 struct drm_pending_vblank_event *event,
3233 uint32_t flags)
f869a6ec
AG
3234{
3235 struct drm_plane *plane = crtc->primary;
3236 struct drm_plane_state *plane_state;
3237 struct drm_crtc_state *crtc_state;
3238 int ret = 0;
3239
3240 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3241 if (IS_ERR(crtc_state))
3242 return PTR_ERR(crtc_state);
3243
3244 crtc_state->event = event;
6cbe5c46 3245 crtc_state->pageflip_flags = flags;
f869a6ec
AG
3246
3247 plane_state = drm_atomic_get_plane_state(state, plane);
3248 if (IS_ERR(plane_state))
3249 return PTR_ERR(plane_state);
3250
f869a6ec
AG
3251 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
3252 if (ret != 0)
3253 return ret;
3254 drm_atomic_set_fb_for_plane(plane_state, fb);
3255
3256 /* Make sure we don't accidentally do a full modeset. */
3257 state->allow_modeset = false;
3258 if (!crtc_state->active) {
6ac7c548
RK
3259 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] disabled, rejecting legacy flip\n",
3260 crtc->base.id, crtc->name);
f869a6ec
AG
3261 return -EINVAL;
3262 }
3263
3264 return ret;
3265}
3266
8bc0f312
DV
3267/**
3268 * drm_atomic_helper_page_flip - execute a legacy page flip
3269 * @crtc: DRM crtc
3270 * @fb: DRM framebuffer
3271 * @event: optional DRM event to signal upon completion
3272 * @flags: flip flags for non-vblank sync'ed updates
41292b1f 3273 * @ctx: lock acquisition context
8bc0f312 3274 *
f869a6ec
AG
3275 * Provides a default &drm_crtc_funcs.page_flip implementation
3276 * using the atomic driver interface.
8bc0f312 3277 *
8bc0f312
DV
3278 * Returns:
3279 * Returns 0 on success, negative errno numbers on failure.
f869a6ec
AG
3280 *
3281 * See also:
3282 * drm_atomic_helper_page_flip_target()
8bc0f312
DV
3283 */
3284int drm_atomic_helper_page_flip(struct drm_crtc *crtc,
3285 struct drm_framebuffer *fb,
3286 struct drm_pending_vblank_event *event,
41292b1f
DV
3287 uint32_t flags,
3288 struct drm_modeset_acquire_ctx *ctx)
8bc0f312
DV
3289{
3290 struct drm_plane *plane = crtc->primary;
3291 struct drm_atomic_state *state;
8bc0f312
DV
3292 int ret = 0;
3293
8bc0f312
DV
3294 state = drm_atomic_state_alloc(plane->dev);
3295 if (!state)
3296 return -ENOMEM;
3297
043e7fb6 3298 state->acquire_ctx = ctx;
f869a6ec 3299
6cbe5c46 3300 ret = page_flip_common(state, crtc, fb, event, flags);
f869a6ec 3301 if (ret != 0)
8bc0f312 3302 goto fail;
8bc0f312 3303
f869a6ec 3304 ret = drm_atomic_nonblocking_commit(state);
f869a6ec 3305fail:
f869a6ec
AG
3306 drm_atomic_state_put(state);
3307 return ret;
f869a6ec
AG
3308}
3309EXPORT_SYMBOL(drm_atomic_helper_page_flip);
3310
3311/**
3312 * drm_atomic_helper_page_flip_target - do page flip on target vblank period.
3313 * @crtc: DRM crtc
3314 * @fb: DRM framebuffer
3315 * @event: optional DRM event to signal upon completion
3316 * @flags: flip flags for non-vblank sync'ed updates
3317 * @target: specifying the target vblank period when the flip to take effect
41292b1f 3318 * @ctx: lock acquisition context
f869a6ec
AG
3319 *
3320 * Provides a default &drm_crtc_funcs.page_flip_target implementation.
3321 * Similar to drm_atomic_helper_page_flip() with extra parameter to specify
3322 * target vblank period to flip.
3323 *
3324 * Returns:
3325 * Returns 0 on success, negative errno numbers on failure.
3326 */
8c3a8181
DV
3327int drm_atomic_helper_page_flip_target(struct drm_crtc *crtc,
3328 struct drm_framebuffer *fb,
3329 struct drm_pending_vblank_event *event,
3330 uint32_t flags,
3331 uint32_t target,
3332 struct drm_modeset_acquire_ctx *ctx)
f869a6ec
AG
3333{
3334 struct drm_plane *plane = crtc->primary;
3335 struct drm_atomic_state *state;
3336 struct drm_crtc_state *crtc_state;
3337 int ret = 0;
3338
f869a6ec
AG
3339 state = drm_atomic_state_alloc(plane->dev);
3340 if (!state)
3341 return -ENOMEM;
3342
043e7fb6 3343 state->acquire_ctx = ctx;
f869a6ec 3344
6cbe5c46 3345 ret = page_flip_common(state, crtc, fb, event, flags);
8bc0f312
DV
3346 if (ret != 0)
3347 goto fail;
8bc0f312 3348
b4d93679 3349 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
f869a6ec 3350 if (WARN_ON(!crtc_state)) {
4cba6850
DV
3351 ret = -EINVAL;
3352 goto fail;
3353 }
f869a6ec 3354 crtc_state->target_vblank = target;
4cba6850 3355
b837ba0a 3356 ret = drm_atomic_nonblocking_commit(state);
8bc0f312 3357fail:
0853695c 3358 drm_atomic_state_put(state);
8bc0f312 3359 return ret;
8bc0f312 3360}
f869a6ec 3361EXPORT_SYMBOL(drm_atomic_helper_page_flip_target);
1d8224e7
DV
3362
3363/**
3364 * drm_atomic_helper_legacy_gamma_set - set the legacy gamma correction table
3365 * @crtc: CRTC object
3366 * @red: red correction table
3367 * @green: green correction table
3368 * @blue: green correction table
3369 * @size: size of the tables
3370 * @ctx: lock acquire context
3371 *
3372 * Implements support for legacy gamma correction table for drivers
3373 * that support color management through the DEGAMMA_LUT/GAMMA_LUT
3374 * properties. See drm_crtc_enable_color_mgmt() and the containing chapter for
3375 * how the atomic color management and gamma tables work.
3376 */
3377int drm_atomic_helper_legacy_gamma_set(struct drm_crtc *crtc,
3378 u16 *red, u16 *green, u16 *blue,
3379 uint32_t size,
3380 struct drm_modeset_acquire_ctx *ctx)
3381{
3382 struct drm_device *dev = crtc->dev;
3383 struct drm_atomic_state *state;
3384 struct drm_crtc_state *crtc_state;
3385 struct drm_property_blob *blob = NULL;
3386 struct drm_color_lut *blob_data;
3387 int i, ret = 0;
3388 bool replaced;
3389
3390 state = drm_atomic_state_alloc(crtc->dev);
3391 if (!state)
3392 return -ENOMEM;
3393
3394 blob = drm_property_create_blob(dev,
3395 sizeof(struct drm_color_lut) * size,
3396 NULL);
3397 if (IS_ERR(blob)) {
3398 ret = PTR_ERR(blob);
3399 blob = NULL;
3400 goto fail;
3401 }
3402
3403 /* Prepare GAMMA_LUT with the legacy values. */
3404 blob_data = blob->data;
3405 for (i = 0; i < size; i++) {
3406 blob_data[i].red = red[i];
3407 blob_data[i].green = green[i];
3408 blob_data[i].blue = blue[i];
3409 }
3410
3411 state->acquire_ctx = ctx;
3412 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3413 if (IS_ERR(crtc_state)) {
3414 ret = PTR_ERR(crtc_state);
3415 goto fail;
3416 }
3417
3418 /* Reset DEGAMMA_LUT and CTM properties. */
3419 replaced = drm_property_replace_blob(&crtc_state->degamma_lut, NULL);
3420 replaced |= drm_property_replace_blob(&crtc_state->ctm, NULL);
3421 replaced |= drm_property_replace_blob(&crtc_state->gamma_lut, blob);
3422 crtc_state->color_mgmt_changed |= replaced;
3423
3424 ret = drm_atomic_commit(state);
3425
3426fail:
3427 drm_atomic_state_put(state);
3428 drm_property_blob_put(blob);
3429 return ret;
3430}
3431EXPORT_SYMBOL(drm_atomic_helper_legacy_gamma_set);