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