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