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