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