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