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