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