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