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