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