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