]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/gpu/drm/drm_atomic_helper.c
45b4f34bebcd8dee65f2ca53b6cbfaf8805216cd
[mirror_ubuntu-bionic-kernel.git] / drivers / gpu / drm / drm_atomic_helper.c
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>
32 #include <drm/drm_atomic_helper.h>
33 #include <linux/dma-fence.h>
34
35 #include "drm_crtc_helper_internal.h"
36 #include "drm_crtc_internal.h"
37
38 /**
39 * DOC: overview
40 *
41 * This helper library provides implementations of check and commit functions on
42 * top of the CRTC modeset helper callbacks and the plane helper callbacks. It
43 * also provides convenience implementations for the atomic state handling
44 * callbacks for drivers which don't need to subclass the drm core structures to
45 * add their own additional internal state.
46 *
47 * This library also provides default implementations for the check callback in
48 * drm_atomic_helper_check() and for the commit callback with
49 * drm_atomic_helper_commit(). But the individual stages and callbacks are
50 * exposed to allow drivers to mix and match and e.g. use the plane helpers only
51 * together with a driver private modeset implementation.
52 *
53 * This library also provides implementations for all the legacy driver
54 * interfaces on top of the atomic interface. See drm_atomic_helper_set_config(),
55 * drm_atomic_helper_disable_plane(), drm_atomic_helper_disable_plane() and the
56 * various functions to implement set_property callbacks. New drivers must not
57 * implement these functions themselves but must use the provided helpers.
58 *
59 * The atomic helper uses the same function table structures as all other
60 * modesetting helpers. See the documentation for &struct drm_crtc_helper_funcs,
61 * struct &drm_encoder_helper_funcs and &struct drm_connector_helper_funcs. It
62 * also shares the &struct drm_plane_helper_funcs function table with the plane
63 * helpers.
64 */
65 static void
66 drm_atomic_helper_plane_changed(struct drm_atomic_state *state,
67 struct drm_plane_state *old_plane_state,
68 struct drm_plane_state *plane_state,
69 struct drm_plane *plane)
70 {
71 struct drm_crtc_state *crtc_state;
72
73 if (old_plane_state->crtc) {
74 crtc_state = drm_atomic_get_new_crtc_state(state,
75 old_plane_state->crtc);
76
77 if (WARN_ON(!crtc_state))
78 return;
79
80 crtc_state->planes_changed = true;
81 }
82
83 if (plane_state->crtc) {
84 crtc_state = drm_atomic_get_new_crtc_state(state, plane_state->crtc);
85
86 if (WARN_ON(!crtc_state))
87 return;
88
89 crtc_state->planes_changed = true;
90 }
91 }
92
93 static int handle_conflicting_encoders(struct drm_atomic_state *state,
94 bool disable_conflicting_encoders)
95 {
96 struct drm_connector_state *new_conn_state;
97 struct drm_connector *connector;
98 struct drm_connector_list_iter conn_iter;
99 struct drm_encoder *encoder;
100 unsigned encoder_mask = 0;
101 int i, ret = 0;
102
103 /*
104 * First loop, find all newly assigned encoders from the connectors
105 * part of the state. If the same encoder is assigned to multiple
106 * connectors bail out.
107 */
108 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
109 const struct drm_connector_helper_funcs *funcs = connector->helper_private;
110 struct drm_encoder *new_encoder;
111
112 if (!new_conn_state->crtc)
113 continue;
114
115 if (funcs->atomic_best_encoder)
116 new_encoder = funcs->atomic_best_encoder(connector, new_conn_state);
117 else if (funcs->best_encoder)
118 new_encoder = funcs->best_encoder(connector);
119 else
120 new_encoder = drm_atomic_helper_best_encoder(connector);
121
122 if (new_encoder) {
123 if (encoder_mask & (1 << drm_encoder_index(new_encoder))) {
124 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] on [CONNECTOR:%d:%s] already assigned\n",
125 new_encoder->base.id, new_encoder->name,
126 connector->base.id, connector->name);
127
128 return -EINVAL;
129 }
130
131 encoder_mask |= 1 << drm_encoder_index(new_encoder);
132 }
133 }
134
135 if (!encoder_mask)
136 return 0;
137
138 /*
139 * Second loop, iterate over all connectors not part of the state.
140 *
141 * If a conflicting encoder is found and disable_conflicting_encoders
142 * is not set, an error is returned. Userspace can provide a solution
143 * through the atomic ioctl.
144 *
145 * If the flag is set conflicting connectors are removed from the crtc
146 * and the crtc is disabled if no encoder is left. This preserves
147 * compatibility with the legacy set_config behavior.
148 */
149 drm_connector_list_iter_begin(state->dev, &conn_iter);
150 drm_for_each_connector_iter(connector, &conn_iter) {
151 struct drm_crtc_state *crtc_state;
152
153 if (drm_atomic_get_new_connector_state(state, connector))
154 continue;
155
156 encoder = connector->state->best_encoder;
157 if (!encoder || !(encoder_mask & (1 << drm_encoder_index(encoder))))
158 continue;
159
160 if (!disable_conflicting_encoders) {
161 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s] by [CONNECTOR:%d:%s]\n",
162 encoder->base.id, encoder->name,
163 connector->state->crtc->base.id,
164 connector->state->crtc->name,
165 connector->base.id, connector->name);
166 ret = -EINVAL;
167 goto out;
168 }
169
170 new_conn_state = drm_atomic_get_connector_state(state, connector);
171 if (IS_ERR(new_conn_state)) {
172 ret = PTR_ERR(new_conn_state);
173 goto out;
174 }
175
176 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s], disabling [CONNECTOR:%d:%s]\n",
177 encoder->base.id, encoder->name,
178 new_conn_state->crtc->base.id, new_conn_state->crtc->name,
179 connector->base.id, connector->name);
180
181 crtc_state = drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
182
183 ret = drm_atomic_set_crtc_for_connector(new_conn_state, NULL);
184 if (ret)
185 goto out;
186
187 if (!crtc_state->connector_mask) {
188 ret = drm_atomic_set_mode_prop_for_crtc(crtc_state,
189 NULL);
190 if (ret < 0)
191 goto out;
192
193 crtc_state->active = false;
194 }
195 }
196 out:
197 drm_connector_list_iter_end(&conn_iter);
198
199 return ret;
200 }
201
202 static void
203 set_best_encoder(struct drm_atomic_state *state,
204 struct drm_connector_state *conn_state,
205 struct drm_encoder *encoder)
206 {
207 struct drm_crtc_state *crtc_state;
208 struct drm_crtc *crtc;
209
210 if (conn_state->best_encoder) {
211 /* Unset the encoder_mask in the old crtc state. */
212 crtc = conn_state->connector->state->crtc;
213
214 /* A NULL crtc is an error here because we should have
215 * duplicated a NULL best_encoder when crtc was NULL.
216 * As an exception restoring duplicated atomic state
217 * during resume is allowed, so don't warn when
218 * best_encoder is equal to encoder we intend to set.
219 */
220 WARN_ON(!crtc && encoder != conn_state->best_encoder);
221 if (crtc) {
222 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
223
224 crtc_state->encoder_mask &=
225 ~(1 << drm_encoder_index(conn_state->best_encoder));
226 }
227 }
228
229 if (encoder) {
230 crtc = conn_state->crtc;
231 WARN_ON(!crtc);
232 if (crtc) {
233 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
234
235 crtc_state->encoder_mask |=
236 1 << drm_encoder_index(encoder);
237 }
238 }
239
240 conn_state->best_encoder = encoder;
241 }
242
243 static void
244 steal_encoder(struct drm_atomic_state *state,
245 struct drm_encoder *encoder)
246 {
247 struct drm_crtc_state *crtc_state;
248 struct drm_connector *connector;
249 struct drm_connector_state *old_connector_state, *new_connector_state;
250 int i;
251
252 for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
253 struct drm_crtc *encoder_crtc;
254
255 if (new_connector_state->best_encoder != encoder)
256 continue;
257
258 encoder_crtc = old_connector_state->crtc;
259
260 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s], stealing it\n",
261 encoder->base.id, encoder->name,
262 encoder_crtc->base.id, encoder_crtc->name);
263
264 set_best_encoder(state, new_connector_state, NULL);
265
266 crtc_state = drm_atomic_get_new_crtc_state(state, encoder_crtc);
267 crtc_state->connectors_changed = true;
268
269 return;
270 }
271 }
272
273 static int
274 update_connector_routing(struct drm_atomic_state *state,
275 struct drm_connector *connector,
276 struct drm_connector_state *old_connector_state,
277 struct drm_connector_state *new_connector_state)
278 {
279 const struct drm_connector_helper_funcs *funcs;
280 struct drm_encoder *new_encoder;
281 struct drm_crtc_state *crtc_state;
282
283 DRM_DEBUG_ATOMIC("Updating routing for [CONNECTOR:%d:%s]\n",
284 connector->base.id,
285 connector->name);
286
287 if (old_connector_state->crtc != new_connector_state->crtc) {
288 if (old_connector_state->crtc) {
289 crtc_state = drm_atomic_get_new_crtc_state(state, old_connector_state->crtc);
290 crtc_state->connectors_changed = true;
291 }
292
293 if (new_connector_state->crtc) {
294 crtc_state = drm_atomic_get_new_crtc_state(state, new_connector_state->crtc);
295 crtc_state->connectors_changed = true;
296 }
297 }
298
299 if (!new_connector_state->crtc) {
300 DRM_DEBUG_ATOMIC("Disabling [CONNECTOR:%d:%s]\n",
301 connector->base.id,
302 connector->name);
303
304 set_best_encoder(state, new_connector_state, NULL);
305
306 return 0;
307 }
308
309 funcs = connector->helper_private;
310
311 if (funcs->atomic_best_encoder)
312 new_encoder = funcs->atomic_best_encoder(connector,
313 new_connector_state);
314 else if (funcs->best_encoder)
315 new_encoder = funcs->best_encoder(connector);
316 else
317 new_encoder = drm_atomic_helper_best_encoder(connector);
318
319 if (!new_encoder) {
320 DRM_DEBUG_ATOMIC("No suitable encoder found for [CONNECTOR:%d:%s]\n",
321 connector->base.id,
322 connector->name);
323 return -EINVAL;
324 }
325
326 if (!drm_encoder_crtc_ok(new_encoder, new_connector_state->crtc)) {
327 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] incompatible with [CRTC:%d:%s]\n",
328 new_encoder->base.id,
329 new_encoder->name,
330 new_connector_state->crtc->base.id,
331 new_connector_state->crtc->name);
332 return -EINVAL;
333 }
334
335 if (new_encoder == new_connector_state->best_encoder) {
336 set_best_encoder(state, new_connector_state, new_encoder);
337
338 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] keeps [ENCODER:%d:%s], now on [CRTC:%d:%s]\n",
339 connector->base.id,
340 connector->name,
341 new_encoder->base.id,
342 new_encoder->name,
343 new_connector_state->crtc->base.id,
344 new_connector_state->crtc->name);
345
346 return 0;
347 }
348
349 steal_encoder(state, new_encoder);
350
351 set_best_encoder(state, new_connector_state, new_encoder);
352
353 crtc_state = drm_atomic_get_new_crtc_state(state, new_connector_state->crtc);
354 crtc_state->connectors_changed = true;
355
356 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] using [ENCODER:%d:%s] on [CRTC:%d:%s]\n",
357 connector->base.id,
358 connector->name,
359 new_encoder->base.id,
360 new_encoder->name,
361 new_connector_state->crtc->base.id,
362 new_connector_state->crtc->name);
363
364 return 0;
365 }
366
367 static int
368 mode_fixup(struct drm_atomic_state *state)
369 {
370 struct drm_crtc *crtc;
371 struct drm_crtc_state *new_crtc_state;
372 struct drm_connector *connector;
373 struct drm_connector_state *new_conn_state;
374 int i;
375 int ret;
376
377 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
378 if (!new_crtc_state->mode_changed &&
379 !new_crtc_state->connectors_changed)
380 continue;
381
382 drm_mode_copy(&new_crtc_state->adjusted_mode, &new_crtc_state->mode);
383 }
384
385 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
386 const struct drm_encoder_helper_funcs *funcs;
387 struct drm_encoder *encoder;
388
389 WARN_ON(!!new_conn_state->best_encoder != !!new_conn_state->crtc);
390
391 if (!new_conn_state->crtc || !new_conn_state->best_encoder)
392 continue;
393
394 new_crtc_state =
395 drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
396
397 /*
398 * Each encoder has at most one connector (since we always steal
399 * it away), so we won't call ->mode_fixup twice.
400 */
401 encoder = new_conn_state->best_encoder;
402 funcs = encoder->helper_private;
403
404 ret = drm_bridge_mode_fixup(encoder->bridge, &new_crtc_state->mode,
405 &new_crtc_state->adjusted_mode);
406 if (!ret) {
407 DRM_DEBUG_ATOMIC("Bridge fixup failed\n");
408 return -EINVAL;
409 }
410
411 if (funcs && funcs->atomic_check) {
412 ret = funcs->atomic_check(encoder, new_crtc_state,
413 new_conn_state);
414 if (ret) {
415 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] check failed\n",
416 encoder->base.id, encoder->name);
417 return ret;
418 }
419 } else if (funcs && funcs->mode_fixup) {
420 ret = funcs->mode_fixup(encoder, &new_crtc_state->mode,
421 &new_crtc_state->adjusted_mode);
422 if (!ret) {
423 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] fixup failed\n",
424 encoder->base.id, encoder->name);
425 return -EINVAL;
426 }
427 }
428 }
429
430 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
431 const struct drm_crtc_helper_funcs *funcs;
432
433 if (!new_crtc_state->enable)
434 continue;
435
436 if (!new_crtc_state->mode_changed &&
437 !new_crtc_state->connectors_changed)
438 continue;
439
440 funcs = crtc->helper_private;
441 if (!funcs->mode_fixup)
442 continue;
443
444 ret = funcs->mode_fixup(crtc, &new_crtc_state->mode,
445 &new_crtc_state->adjusted_mode);
446 if (!ret) {
447 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] fixup failed\n",
448 crtc->base.id, crtc->name);
449 return -EINVAL;
450 }
451 }
452
453 return 0;
454 }
455
456 static enum drm_mode_status mode_valid_path(struct drm_connector *connector,
457 struct drm_encoder *encoder,
458 struct drm_crtc *crtc,
459 struct drm_display_mode *mode)
460 {
461 enum drm_mode_status ret;
462
463 ret = drm_encoder_mode_valid(encoder, mode);
464 if (ret != MODE_OK) {
465 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] mode_valid() failed\n",
466 encoder->base.id, encoder->name);
467 return ret;
468 }
469
470 ret = drm_bridge_mode_valid(encoder->bridge, mode);
471 if (ret != MODE_OK) {
472 DRM_DEBUG_ATOMIC("[BRIDGE] mode_valid() failed\n");
473 return ret;
474 }
475
476 ret = drm_crtc_mode_valid(crtc, mode);
477 if (ret != MODE_OK) {
478 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] mode_valid() failed\n",
479 crtc->base.id, crtc->name);
480 return ret;
481 }
482
483 return ret;
484 }
485
486 static int
487 mode_valid(struct drm_atomic_state *state)
488 {
489 struct drm_connector_state *conn_state;
490 struct drm_connector *connector;
491 int i;
492
493 for_each_new_connector_in_state(state, connector, conn_state, i) {
494 struct drm_encoder *encoder = conn_state->best_encoder;
495 struct drm_crtc *crtc = conn_state->crtc;
496 struct drm_crtc_state *crtc_state;
497 enum drm_mode_status mode_status;
498 struct drm_display_mode *mode;
499
500 if (!crtc || !encoder)
501 continue;
502
503 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
504 if (!crtc_state)
505 continue;
506 if (!crtc_state->mode_changed && !crtc_state->connectors_changed)
507 continue;
508
509 mode = &crtc_state->mode;
510
511 mode_status = mode_valid_path(connector, encoder, crtc, mode);
512 if (mode_status != MODE_OK)
513 return -EINVAL;
514 }
515
516 return 0;
517 }
518
519 /**
520 * drm_atomic_helper_check_modeset - validate state object for modeset changes
521 * @dev: DRM device
522 * @state: the driver state object
523 *
524 * Check the state object to see if the requested state is physically possible.
525 * This does all the crtc and connector related computations for an atomic
526 * update and adds any additional connectors needed for full modesets. It calls
527 * the various per-object callbacks in the follow order:
528 *
529 * 1. &drm_connector_helper_funcs.atomic_best_encoder for determining the new encoder.
530 * 2. &drm_connector_helper_funcs.atomic_check to validate the connector state.
531 * 3. If it's determined a modeset is needed then all connectors on the affected crtc
532 * crtc are added and &drm_connector_helper_funcs.atomic_check is run on them.
533 * 4. &drm_encoder_helper_funcs.mode_valid, &drm_bridge_funcs.mode_valid and
534 * &drm_crtc_helper_funcs.mode_valid are called on the affected components.
535 * 5. &drm_bridge_funcs.mode_fixup is called on all encoder bridges.
536 * 6. &drm_encoder_helper_funcs.atomic_check is called to validate any encoder state.
537 * This function is only called when the encoder will be part of a configured crtc,
538 * it must not be used for implementing connector property validation.
539 * If this function is NULL, &drm_atomic_encoder_helper_funcs.mode_fixup is called
540 * instead.
541 * 7. &drm_crtc_helper_funcs.mode_fixup is called last, to fix up the mode with crtc constraints.
542 *
543 * &drm_crtc_state.mode_changed is set when the input mode is changed.
544 * &drm_crtc_state.connectors_changed is set when a connector is added or
545 * removed from the crtc. &drm_crtc_state.active_changed is set when
546 * &drm_crtc_state.active changes, which is used for DPMS.
547 * See also: drm_atomic_crtc_needs_modeset()
548 *
549 * IMPORTANT:
550 *
551 * Drivers which set &drm_crtc_state.mode_changed (e.g. in their
552 * &drm_plane_helper_funcs.atomic_check hooks if a plane update can't be done
553 * without a full modeset) _must_ call this function afterwards after that
554 * change. It is permitted to call this function multiple times for the same
555 * update, e.g. when the &drm_crtc_helper_funcs.atomic_check functions depend
556 * upon the adjusted dotclock for fifo space allocation and watermark
557 * computation.
558 *
559 * RETURNS:
560 * Zero for success or -errno
561 */
562 int
563 drm_atomic_helper_check_modeset(struct drm_device *dev,
564 struct drm_atomic_state *state)
565 {
566 struct drm_crtc *crtc;
567 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
568 struct drm_connector *connector;
569 struct drm_connector_state *old_connector_state, *new_connector_state;
570 int i, ret;
571 unsigned connectors_mask = 0;
572
573 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
574 bool has_connectors =
575 !!new_crtc_state->connector_mask;
576
577 if (!drm_mode_equal(&old_crtc_state->mode, &new_crtc_state->mode)) {
578 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] mode changed\n",
579 crtc->base.id, crtc->name);
580 new_crtc_state->mode_changed = true;
581 }
582
583 if (old_crtc_state->enable != new_crtc_state->enable) {
584 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enable changed\n",
585 crtc->base.id, crtc->name);
586
587 /*
588 * For clarity this assignment is done here, but
589 * enable == 0 is only true when there are no
590 * connectors and a NULL mode.
591 *
592 * The other way around is true as well. enable != 0
593 * iff connectors are attached and a mode is set.
594 */
595 new_crtc_state->mode_changed = true;
596 new_crtc_state->connectors_changed = true;
597 }
598
599 if (old_crtc_state->active != new_crtc_state->active) {
600 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active changed\n",
601 crtc->base.id, crtc->name);
602 new_crtc_state->active_changed = true;
603 }
604
605 if (new_crtc_state->enable != has_connectors) {
606 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled/connectors mismatch\n",
607 crtc->base.id, crtc->name);
608
609 return -EINVAL;
610 }
611 }
612
613 ret = handle_conflicting_encoders(state, false);
614 if (ret)
615 return ret;
616
617 for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
618 const struct drm_connector_helper_funcs *funcs = connector->helper_private;
619
620 /*
621 * This only sets crtc->connectors_changed for routing changes,
622 * drivers must set crtc->connectors_changed themselves when
623 * connector properties need to be updated.
624 */
625 ret = update_connector_routing(state, connector,
626 old_connector_state,
627 new_connector_state);
628 if (ret)
629 return ret;
630 if (old_connector_state->crtc) {
631 new_crtc_state = drm_atomic_get_new_crtc_state(state,
632 old_connector_state->crtc);
633 if (old_connector_state->link_status !=
634 new_connector_state->link_status)
635 new_crtc_state->connectors_changed = true;
636 }
637
638 if (funcs->atomic_check)
639 ret = funcs->atomic_check(connector, new_connector_state);
640 if (ret)
641 return ret;
642
643 connectors_mask += BIT(i);
644 }
645
646 /*
647 * After all the routing has been prepared we need to add in any
648 * connector which is itself unchanged, but who's crtc changes it's
649 * configuration. This must be done before calling mode_fixup in case a
650 * crtc only changed its mode but has the same set of connectors.
651 */
652 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
653 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
654 continue;
655
656 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] needs all connectors, enable: %c, active: %c\n",
657 crtc->base.id, crtc->name,
658 new_crtc_state->enable ? 'y' : 'n',
659 new_crtc_state->active ? 'y' : 'n');
660
661 ret = drm_atomic_add_affected_connectors(state, crtc);
662 if (ret != 0)
663 return ret;
664
665 ret = drm_atomic_add_affected_planes(state, crtc);
666 if (ret != 0)
667 return ret;
668 }
669
670 /*
671 * Iterate over all connectors again, to make sure atomic_check()
672 * has been called on them when a modeset is forced.
673 */
674 for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
675 const struct drm_connector_helper_funcs *funcs = connector->helper_private;
676
677 if (connectors_mask & BIT(i))
678 continue;
679
680 if (funcs->atomic_check)
681 ret = funcs->atomic_check(connector, new_connector_state);
682 if (ret)
683 return ret;
684 }
685
686 ret = mode_valid(state);
687 if (ret)
688 return ret;
689
690 return mode_fixup(state);
691 }
692 EXPORT_SYMBOL(drm_atomic_helper_check_modeset);
693
694 /**
695 * drm_atomic_helper_check_planes - validate state object for planes changes
696 * @dev: DRM device
697 * @state: the driver state object
698 *
699 * Check the state object to see if the requested state is physically possible.
700 * This does all the plane update related checks using by calling into the
701 * &drm_crtc_helper_funcs.atomic_check and &drm_plane_helper_funcs.atomic_check
702 * hooks provided by the driver.
703 *
704 * It also sets &drm_crtc_state.planes_changed to indicate that a crtc has
705 * updated planes.
706 *
707 * RETURNS:
708 * Zero for success or -errno
709 */
710 int
711 drm_atomic_helper_check_planes(struct drm_device *dev,
712 struct drm_atomic_state *state)
713 {
714 struct drm_crtc *crtc;
715 struct drm_crtc_state *new_crtc_state;
716 struct drm_plane *plane;
717 struct drm_plane_state *new_plane_state, *old_plane_state;
718 int i, ret = 0;
719
720 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
721 const struct drm_plane_helper_funcs *funcs;
722
723 funcs = plane->helper_private;
724
725 drm_atomic_helper_plane_changed(state, old_plane_state, new_plane_state, plane);
726
727 if (!funcs || !funcs->atomic_check)
728 continue;
729
730 ret = funcs->atomic_check(plane, new_plane_state);
731 if (ret) {
732 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic driver check failed\n",
733 plane->base.id, plane->name);
734 return ret;
735 }
736 }
737
738 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
739 const struct drm_crtc_helper_funcs *funcs;
740
741 funcs = crtc->helper_private;
742
743 if (!funcs || !funcs->atomic_check)
744 continue;
745
746 ret = funcs->atomic_check(crtc, new_crtc_state);
747 if (ret) {
748 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic driver check failed\n",
749 crtc->base.id, crtc->name);
750 return ret;
751 }
752 }
753
754 return ret;
755 }
756 EXPORT_SYMBOL(drm_atomic_helper_check_planes);
757
758 /**
759 * drm_atomic_helper_check - validate state object
760 * @dev: DRM device
761 * @state: the driver state object
762 *
763 * Check the state object to see if the requested state is physically possible.
764 * Only crtcs and planes have check callbacks, so for any additional (global)
765 * checking that a driver needs it can simply wrap that around this function.
766 * Drivers without such needs can directly use this as their
767 * &drm_mode_config_funcs.atomic_check callback.
768 *
769 * This just wraps the two parts of the state checking for planes and modeset
770 * state in the default order: First it calls drm_atomic_helper_check_modeset()
771 * and then drm_atomic_helper_check_planes(). The assumption is that the
772 * @drm_plane_helper_funcs.atomic_check and @drm_crtc_helper_funcs.atomic_check
773 * functions depend upon an updated adjusted_mode.clock to e.g. properly compute
774 * watermarks.
775 *
776 * RETURNS:
777 * Zero for success or -errno
778 */
779 int drm_atomic_helper_check(struct drm_device *dev,
780 struct drm_atomic_state *state)
781 {
782 int ret;
783
784 ret = drm_atomic_helper_check_modeset(dev, state);
785 if (ret)
786 return ret;
787
788 ret = drm_atomic_helper_check_planes(dev, state);
789 if (ret)
790 return ret;
791
792 return ret;
793 }
794 EXPORT_SYMBOL(drm_atomic_helper_check);
795
796 static void
797 disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state)
798 {
799 struct drm_connector *connector;
800 struct drm_connector_state *old_conn_state, *new_conn_state;
801 struct drm_crtc *crtc;
802 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
803 int i;
804
805 for_each_oldnew_connector_in_state(old_state, connector, old_conn_state, new_conn_state, i) {
806 const struct drm_encoder_helper_funcs *funcs;
807 struct drm_encoder *encoder;
808
809 /* Shut down everything that's in the changeset and currently
810 * still on. So need to check the old, saved state. */
811 if (!old_conn_state->crtc)
812 continue;
813
814 old_crtc_state = drm_atomic_get_old_crtc_state(old_state, old_conn_state->crtc);
815
816 if (!old_crtc_state->active ||
817 !drm_atomic_crtc_needs_modeset(old_conn_state->crtc->state))
818 continue;
819
820 encoder = old_conn_state->best_encoder;
821
822 /* We shouldn't get this far if we didn't previously have
823 * an encoder.. but WARN_ON() rather than explode.
824 */
825 if (WARN_ON(!encoder))
826 continue;
827
828 funcs = encoder->helper_private;
829
830 DRM_DEBUG_ATOMIC("disabling [ENCODER:%d:%s]\n",
831 encoder->base.id, encoder->name);
832
833 /*
834 * Each encoder has at most one connector (since we always steal
835 * it away), so we won't call disable hooks twice.
836 */
837 drm_bridge_disable(encoder->bridge);
838
839 /* Right function depends upon target state. */
840 if (funcs) {
841 if (new_conn_state->crtc && funcs->prepare)
842 funcs->prepare(encoder);
843 else if (funcs->disable)
844 funcs->disable(encoder);
845 else if (funcs->dpms)
846 funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
847 }
848
849 drm_bridge_post_disable(encoder->bridge);
850 }
851
852 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
853 const struct drm_crtc_helper_funcs *funcs;
854
855 /* Shut down everything that needs a full modeset. */
856 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
857 continue;
858
859 if (!old_crtc_state->active)
860 continue;
861
862 funcs = crtc->helper_private;
863
864 DRM_DEBUG_ATOMIC("disabling [CRTC:%d:%s]\n",
865 crtc->base.id, crtc->name);
866
867
868 /* Right function depends upon target state. */
869 if (new_crtc_state->enable && funcs->prepare)
870 funcs->prepare(crtc);
871 else if (funcs->atomic_disable)
872 funcs->atomic_disable(crtc, old_crtc_state);
873 else if (funcs->disable)
874 funcs->disable(crtc);
875 else
876 funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
877 }
878 }
879
880 /**
881 * drm_atomic_helper_update_legacy_modeset_state - update legacy modeset state
882 * @dev: DRM device
883 * @old_state: atomic state object with old state structures
884 *
885 * This function updates all the various legacy modeset state pointers in
886 * connectors, encoders and crtcs. It also updates the timestamping constants
887 * used for precise vblank timestamps by calling
888 * drm_calc_timestamping_constants().
889 *
890 * Drivers can use this for building their own atomic commit if they don't have
891 * a pure helper-based modeset implementation.
892 */
893 void
894 drm_atomic_helper_update_legacy_modeset_state(struct drm_device *dev,
895 struct drm_atomic_state *old_state)
896 {
897 struct drm_connector *connector;
898 struct drm_connector_state *old_conn_state, *new_conn_state;
899 struct drm_crtc *crtc;
900 struct drm_crtc_state *new_crtc_state;
901 int i;
902
903 /* clear out existing links and update dpms */
904 for_each_oldnew_connector_in_state(old_state, connector, old_conn_state, new_conn_state, i) {
905 if (connector->encoder) {
906 WARN_ON(!connector->encoder->crtc);
907
908 connector->encoder->crtc = NULL;
909 connector->encoder = NULL;
910 }
911
912 crtc = new_conn_state->crtc;
913 if ((!crtc && old_conn_state->crtc) ||
914 (crtc && drm_atomic_crtc_needs_modeset(crtc->state))) {
915 struct drm_property *dpms_prop =
916 dev->mode_config.dpms_property;
917 int mode = DRM_MODE_DPMS_OFF;
918
919 if (crtc && crtc->state->active)
920 mode = DRM_MODE_DPMS_ON;
921
922 connector->dpms = mode;
923 drm_object_property_set_value(&connector->base,
924 dpms_prop, mode);
925 }
926 }
927
928 /* set new links */
929 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
930 if (!new_conn_state->crtc)
931 continue;
932
933 if (WARN_ON(!new_conn_state->best_encoder))
934 continue;
935
936 connector->encoder = new_conn_state->best_encoder;
937 connector->encoder->crtc = new_conn_state->crtc;
938 }
939
940 /* set legacy state in the crtc structure */
941 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
942 struct drm_plane *primary = crtc->primary;
943 struct drm_plane_state *new_plane_state;
944
945 crtc->mode = new_crtc_state->mode;
946 crtc->enabled = new_crtc_state->enable;
947
948 new_plane_state =
949 drm_atomic_get_new_plane_state(old_state, primary);
950
951 if (new_plane_state && new_plane_state->crtc == crtc) {
952 crtc->x = new_plane_state->src_x >> 16;
953 crtc->y = new_plane_state->src_y >> 16;
954 }
955
956 if (new_crtc_state->enable)
957 drm_calc_timestamping_constants(crtc,
958 &new_crtc_state->adjusted_mode);
959 }
960 }
961 EXPORT_SYMBOL(drm_atomic_helper_update_legacy_modeset_state);
962
963 static void
964 crtc_set_mode(struct drm_device *dev, struct drm_atomic_state *old_state)
965 {
966 struct drm_crtc *crtc;
967 struct drm_crtc_state *new_crtc_state;
968 struct drm_connector *connector;
969 struct drm_connector_state *new_conn_state;
970 int i;
971
972 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
973 const struct drm_crtc_helper_funcs *funcs;
974
975 if (!new_crtc_state->mode_changed)
976 continue;
977
978 funcs = crtc->helper_private;
979
980 if (new_crtc_state->enable && funcs->mode_set_nofb) {
981 DRM_DEBUG_ATOMIC("modeset on [CRTC:%d:%s]\n",
982 crtc->base.id, crtc->name);
983
984 funcs->mode_set_nofb(crtc);
985 }
986 }
987
988 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
989 const struct drm_encoder_helper_funcs *funcs;
990 struct drm_encoder *encoder;
991 struct drm_display_mode *mode, *adjusted_mode;
992
993 if (!new_conn_state->best_encoder)
994 continue;
995
996 encoder = new_conn_state->best_encoder;
997 funcs = encoder->helper_private;
998 new_crtc_state = new_conn_state->crtc->state;
999 mode = &new_crtc_state->mode;
1000 adjusted_mode = &new_crtc_state->adjusted_mode;
1001
1002 if (!new_crtc_state->mode_changed)
1003 continue;
1004
1005 DRM_DEBUG_ATOMIC("modeset on [ENCODER:%d:%s]\n",
1006 encoder->base.id, encoder->name);
1007
1008 /*
1009 * Each encoder has at most one connector (since we always steal
1010 * it away), so we won't call mode_set hooks twice.
1011 */
1012 if (funcs && funcs->atomic_mode_set) {
1013 funcs->atomic_mode_set(encoder, new_crtc_state,
1014 new_conn_state);
1015 } else if (funcs && funcs->mode_set) {
1016 funcs->mode_set(encoder, mode, adjusted_mode);
1017 }
1018
1019 drm_bridge_mode_set(encoder->bridge, mode, adjusted_mode);
1020 }
1021 }
1022
1023 /**
1024 * drm_atomic_helper_commit_modeset_disables - modeset commit to disable outputs
1025 * @dev: DRM device
1026 * @old_state: atomic state object with old state structures
1027 *
1028 * This function shuts down all the outputs that need to be shut down and
1029 * prepares them (if required) with the new mode.
1030 *
1031 * For compatibility with legacy crtc helpers this should be called before
1032 * drm_atomic_helper_commit_planes(), which is what the default commit function
1033 * does. But drivers with different needs can group the modeset commits together
1034 * and do the plane commits at the end. This is useful for drivers doing runtime
1035 * PM since planes updates then only happen when the CRTC is actually enabled.
1036 */
1037 void drm_atomic_helper_commit_modeset_disables(struct drm_device *dev,
1038 struct drm_atomic_state *old_state)
1039 {
1040 disable_outputs(dev, old_state);
1041
1042 drm_atomic_helper_update_legacy_modeset_state(dev, old_state);
1043
1044 crtc_set_mode(dev, old_state);
1045 }
1046 EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_disables);
1047
1048 /**
1049 * drm_atomic_helper_commit_modeset_enables - modeset commit to enable outputs
1050 * @dev: DRM device
1051 * @old_state: atomic state object with old state structures
1052 *
1053 * This function enables all the outputs with the new configuration which had to
1054 * be turned off for the update.
1055 *
1056 * For compatibility with legacy crtc helpers this should be called after
1057 * drm_atomic_helper_commit_planes(), which is what the default commit function
1058 * does. But drivers with different needs can group the modeset commits together
1059 * and do the plane commits at the end. This is useful for drivers doing runtime
1060 * PM since planes updates then only happen when the CRTC is actually enabled.
1061 */
1062 void drm_atomic_helper_commit_modeset_enables(struct drm_device *dev,
1063 struct drm_atomic_state *old_state)
1064 {
1065 struct drm_crtc *crtc;
1066 struct drm_crtc_state *new_crtc_state;
1067 struct drm_connector *connector;
1068 struct drm_connector_state *new_conn_state;
1069 int i;
1070
1071 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
1072 const struct drm_crtc_helper_funcs *funcs;
1073
1074 /* Need to filter out CRTCs where only planes change. */
1075 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
1076 continue;
1077
1078 if (!new_crtc_state->active)
1079 continue;
1080
1081 funcs = crtc->helper_private;
1082
1083 if (new_crtc_state->enable) {
1084 DRM_DEBUG_ATOMIC("enabling [CRTC:%d:%s]\n",
1085 crtc->base.id, crtc->name);
1086
1087 if (funcs->enable)
1088 funcs->enable(crtc);
1089 else
1090 funcs->commit(crtc);
1091 }
1092 }
1093
1094 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
1095 const struct drm_encoder_helper_funcs *funcs;
1096 struct drm_encoder *encoder;
1097
1098 if (!new_conn_state->best_encoder)
1099 continue;
1100
1101 if (!new_conn_state->crtc->state->active ||
1102 !drm_atomic_crtc_needs_modeset(new_conn_state->crtc->state))
1103 continue;
1104
1105 encoder = new_conn_state->best_encoder;
1106 funcs = encoder->helper_private;
1107
1108 DRM_DEBUG_ATOMIC("enabling [ENCODER:%d:%s]\n",
1109 encoder->base.id, encoder->name);
1110
1111 /*
1112 * Each encoder has at most one connector (since we always steal
1113 * it away), so we won't call enable hooks twice.
1114 */
1115 drm_bridge_pre_enable(encoder->bridge);
1116
1117 if (funcs) {
1118 if (funcs->enable)
1119 funcs->enable(encoder);
1120 else if (funcs->commit)
1121 funcs->commit(encoder);
1122 }
1123
1124 drm_bridge_enable(encoder->bridge);
1125 }
1126 }
1127 EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_enables);
1128
1129 /**
1130 * drm_atomic_helper_wait_for_fences - wait for fences stashed in plane state
1131 * @dev: DRM device
1132 * @state: atomic state object with old state structures
1133 * @pre_swap: If true, do an interruptible wait, and @state is the new state.
1134 * Otherwise @state is the old state.
1135 *
1136 * For implicit sync, driver should fish the exclusive fence out from the
1137 * incoming fb's and stash it in the drm_plane_state. This is called after
1138 * drm_atomic_helper_swap_state() so it uses the current plane state (and
1139 * just uses the atomic state to find the changed planes)
1140 *
1141 * Note that @pre_swap is needed since the point where we block for fences moves
1142 * around depending upon whether an atomic commit is blocking or
1143 * non-blocking. For non-blocking commit all waiting needs to happen after
1144 * drm_atomic_helper_swap_state() is called, but for blocking commits we want
1145 * to wait **before** we do anything that can't be easily rolled back. That is
1146 * before we call drm_atomic_helper_swap_state().
1147 *
1148 * Returns zero if success or < 0 if dma_fence_wait() fails.
1149 */
1150 int drm_atomic_helper_wait_for_fences(struct drm_device *dev,
1151 struct drm_atomic_state *state,
1152 bool pre_swap)
1153 {
1154 struct drm_plane *plane;
1155 struct drm_plane_state *new_plane_state;
1156 int i, ret;
1157
1158 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
1159 if (!new_plane_state->fence)
1160 continue;
1161
1162 WARN_ON(!new_plane_state->fb);
1163
1164 /*
1165 * If waiting for fences pre-swap (ie: nonblock), userspace can
1166 * still interrupt the operation. Instead of blocking until the
1167 * timer expires, make the wait interruptible.
1168 */
1169 ret = dma_fence_wait(new_plane_state->fence, pre_swap);
1170 if (ret)
1171 return ret;
1172
1173 dma_fence_put(new_plane_state->fence);
1174 new_plane_state->fence = NULL;
1175 }
1176
1177 return 0;
1178 }
1179 EXPORT_SYMBOL(drm_atomic_helper_wait_for_fences);
1180
1181 /**
1182 * drm_atomic_helper_wait_for_vblanks - wait for vblank on crtcs
1183 * @dev: DRM device
1184 * @old_state: atomic state object with old state structures
1185 *
1186 * Helper to, after atomic commit, wait for vblanks on all effected
1187 * crtcs (ie. before cleaning up old framebuffers using
1188 * drm_atomic_helper_cleanup_planes()). It will only wait on CRTCs where the
1189 * framebuffers have actually changed to optimize for the legacy cursor and
1190 * plane update use-case.
1191 *
1192 * Drivers using the nonblocking commit tracking support initialized by calling
1193 * drm_atomic_helper_setup_commit() should look at
1194 * drm_atomic_helper_wait_for_flip_done() as an alternative.
1195 */
1196 void
1197 drm_atomic_helper_wait_for_vblanks(struct drm_device *dev,
1198 struct drm_atomic_state *old_state)
1199 {
1200 struct drm_crtc *crtc;
1201 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
1202 int i, ret;
1203 unsigned crtc_mask = 0;
1204
1205 /*
1206 * Legacy cursor ioctls are completely unsynced, and userspace
1207 * relies on that (by doing tons of cursor updates).
1208 */
1209 if (old_state->legacy_cursor_update)
1210 return;
1211
1212 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
1213 if (!new_crtc_state->active || !new_crtc_state->planes_changed)
1214 continue;
1215
1216 ret = drm_crtc_vblank_get(crtc);
1217 if (ret != 0)
1218 continue;
1219
1220 crtc_mask |= drm_crtc_mask(crtc);
1221 old_state->crtcs[i].last_vblank_count = drm_crtc_vblank_count(crtc);
1222 }
1223
1224 for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
1225 if (!(crtc_mask & drm_crtc_mask(crtc)))
1226 continue;
1227
1228 ret = wait_event_timeout(dev->vblank[i].queue,
1229 old_state->crtcs[i].last_vblank_count !=
1230 drm_crtc_vblank_count(crtc),
1231 msecs_to_jiffies(50));
1232
1233 WARN(!ret, "[CRTC:%d:%s] vblank wait timed out\n",
1234 crtc->base.id, crtc->name);
1235
1236 drm_crtc_vblank_put(crtc);
1237 }
1238 }
1239 EXPORT_SYMBOL(drm_atomic_helper_wait_for_vblanks);
1240
1241 /**
1242 * drm_atomic_helper_wait_for_flip_done - wait for all page flips to be done
1243 * @dev: DRM device
1244 * @old_state: atomic state object with old state structures
1245 *
1246 * Helper to, after atomic commit, wait for page flips on all effected
1247 * crtcs (ie. before cleaning up old framebuffers using
1248 * drm_atomic_helper_cleanup_planes()). Compared to
1249 * drm_atomic_helper_wait_for_vblanks() this waits for the completion of on all
1250 * CRTCs, assuming that cursors-only updates are signalling their completion
1251 * immediately (or using a different path).
1252 *
1253 * This requires that drivers use the nonblocking commit tracking support
1254 * initialized using drm_atomic_helper_setup_commit().
1255 */
1256 void drm_atomic_helper_wait_for_flip_done(struct drm_device *dev,
1257 struct drm_atomic_state *old_state)
1258 {
1259 struct drm_crtc_state *crtc_state;
1260 struct drm_crtc *crtc;
1261 int i;
1262
1263 for_each_crtc_in_state(old_state, crtc, crtc_state, i) {
1264 struct drm_crtc_commit *commit = old_state->crtcs[i].commit;
1265 int ret;
1266
1267 if (!commit)
1268 continue;
1269
1270 ret = wait_for_completion_timeout(&commit->flip_done, 10 * HZ);
1271 if (ret == 0)
1272 DRM_ERROR("[CRTC:%d:%s] flip_done timed out\n",
1273 crtc->base.id, crtc->name);
1274 }
1275 }
1276 EXPORT_SYMBOL(drm_atomic_helper_wait_for_flip_done);
1277
1278 /**
1279 * drm_atomic_helper_commit_tail - commit atomic update to hardware
1280 * @old_state: atomic state object with old state structures
1281 *
1282 * This is the default implementation for the
1283 * &drm_mode_config_helper_funcs.atomic_commit_tail hook.
1284 *
1285 * Note that the default ordering of how the various stages are called is to
1286 * match the legacy modeset helper library closest. One peculiarity of that is
1287 * that it doesn't mesh well with runtime PM at all.
1288 *
1289 * For drivers supporting runtime PM the recommended sequence is instead ::
1290 *
1291 * drm_atomic_helper_commit_modeset_disables(dev, old_state);
1292 *
1293 * drm_atomic_helper_commit_modeset_enables(dev, old_state);
1294 *
1295 * drm_atomic_helper_commit_planes(dev, old_state,
1296 * DRM_PLANE_COMMIT_ACTIVE_ONLY);
1297 *
1298 * for committing the atomic update to hardware. See the kerneldoc entries for
1299 * these three functions for more details.
1300 */
1301 void drm_atomic_helper_commit_tail(struct drm_atomic_state *old_state)
1302 {
1303 struct drm_device *dev = old_state->dev;
1304
1305 drm_atomic_helper_commit_modeset_disables(dev, old_state);
1306
1307 drm_atomic_helper_commit_planes(dev, old_state, 0);
1308
1309 drm_atomic_helper_commit_modeset_enables(dev, old_state);
1310
1311 drm_atomic_helper_commit_hw_done(old_state);
1312
1313 drm_atomic_helper_wait_for_vblanks(dev, old_state);
1314
1315 drm_atomic_helper_cleanup_planes(dev, old_state);
1316 }
1317 EXPORT_SYMBOL(drm_atomic_helper_commit_tail);
1318
1319 static void commit_tail(struct drm_atomic_state *old_state)
1320 {
1321 struct drm_device *dev = old_state->dev;
1322 const struct drm_mode_config_helper_funcs *funcs;
1323
1324 funcs = dev->mode_config.helper_private;
1325
1326 drm_atomic_helper_wait_for_fences(dev, old_state, false);
1327
1328 drm_atomic_helper_wait_for_dependencies(old_state);
1329
1330 if (funcs && funcs->atomic_commit_tail)
1331 funcs->atomic_commit_tail(old_state);
1332 else
1333 drm_atomic_helper_commit_tail(old_state);
1334
1335 drm_atomic_helper_commit_cleanup_done(old_state);
1336
1337 drm_atomic_state_put(old_state);
1338 }
1339
1340 static void commit_work(struct work_struct *work)
1341 {
1342 struct drm_atomic_state *state = container_of(work,
1343 struct drm_atomic_state,
1344 commit_work);
1345 commit_tail(state);
1346 }
1347
1348 /**
1349 * drm_atomic_helper_commit - commit validated state object
1350 * @dev: DRM device
1351 * @state: the driver state object
1352 * @nonblock: whether nonblocking behavior is requested.
1353 *
1354 * This function commits a with drm_atomic_helper_check() pre-validated state
1355 * object. This can still fail when e.g. the framebuffer reservation fails. This
1356 * function implements nonblocking commits, using
1357 * drm_atomic_helper_setup_commit() and related functions.
1358 *
1359 * Committing the actual hardware state is done through the
1360 * &drm_mode_config_helper_funcs.atomic_commit_tail callback, or it's default
1361 * implementation drm_atomic_helper_commit_tail().
1362 *
1363 * RETURNS:
1364 * Zero for success or -errno.
1365 */
1366 int drm_atomic_helper_commit(struct drm_device *dev,
1367 struct drm_atomic_state *state,
1368 bool nonblock)
1369 {
1370 int ret;
1371
1372 ret = drm_atomic_helper_setup_commit(state, nonblock);
1373 if (ret)
1374 return ret;
1375
1376 INIT_WORK(&state->commit_work, commit_work);
1377
1378 ret = drm_atomic_helper_prepare_planes(dev, state);
1379 if (ret)
1380 return ret;
1381
1382 if (!nonblock) {
1383 ret = drm_atomic_helper_wait_for_fences(dev, state, true);
1384 if (ret) {
1385 drm_atomic_helper_cleanup_planes(dev, state);
1386 return ret;
1387 }
1388 }
1389
1390 /*
1391 * This is the point of no return - everything below never fails except
1392 * when the hw goes bonghits. Which means we can commit the new state on
1393 * the software side now.
1394 */
1395
1396 drm_atomic_helper_swap_state(state, true);
1397
1398 /*
1399 * Everything below can be run asynchronously without the need to grab
1400 * any modeset locks at all under one condition: It must be guaranteed
1401 * that the asynchronous work has either been cancelled (if the driver
1402 * supports it, which at least requires that the framebuffers get
1403 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
1404 * before the new state gets committed on the software side with
1405 * drm_atomic_helper_swap_state().
1406 *
1407 * This scheme allows new atomic state updates to be prepared and
1408 * checked in parallel to the asynchronous completion of the previous
1409 * update. Which is important since compositors need to figure out the
1410 * composition of the next frame right after having submitted the
1411 * current layout.
1412 *
1413 * NOTE: Commit work has multiple phases, first hardware commit, then
1414 * cleanup. We want them to overlap, hence need system_unbound_wq to
1415 * make sure work items don't artifically stall on each another.
1416 */
1417
1418 drm_atomic_state_get(state);
1419 if (nonblock)
1420 queue_work(system_unbound_wq, &state->commit_work);
1421 else
1422 commit_tail(state);
1423
1424 return 0;
1425 }
1426 EXPORT_SYMBOL(drm_atomic_helper_commit);
1427
1428 /**
1429 * DOC: implementing nonblocking commit
1430 *
1431 * Nonblocking atomic commits have to be implemented in the following sequence:
1432 *
1433 * 1. Run drm_atomic_helper_prepare_planes() first. This is the only function
1434 * which commit needs to call which can fail, so we want to run it first and
1435 * synchronously.
1436 *
1437 * 2. Synchronize with any outstanding nonblocking commit worker threads which
1438 * might be affected the new state update. This can be done by either cancelling
1439 * or flushing the work items, depending upon whether the driver can deal with
1440 * cancelled updates. Note that it is important to ensure that the framebuffer
1441 * cleanup is still done when cancelling.
1442 *
1443 * Asynchronous workers need to have sufficient parallelism to be able to run
1444 * different atomic commits on different CRTCs in parallel. The simplest way to
1445 * achive this is by running them on the &system_unbound_wq work queue. Note
1446 * that drivers are not required to split up atomic commits and run an
1447 * individual commit in parallel - userspace is supposed to do that if it cares.
1448 * But it might be beneficial to do that for modesets, since those necessarily
1449 * must be done as one global operation, and enabling or disabling a CRTC can
1450 * take a long time. But even that is not required.
1451 *
1452 * 3. The software state is updated synchronously with
1453 * drm_atomic_helper_swap_state(). Doing this under the protection of all modeset
1454 * locks means concurrent callers never see inconsistent state. And doing this
1455 * while it's guaranteed that no relevant nonblocking worker runs means that
1456 * nonblocking workers do not need grab any locks. Actually they must not grab
1457 * locks, for otherwise the work flushing will deadlock.
1458 *
1459 * 4. Schedule a work item to do all subsequent steps, using the split-out
1460 * commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and
1461 * then cleaning up the framebuffers after the old framebuffer is no longer
1462 * being displayed.
1463 *
1464 * The above scheme is implemented in the atomic helper libraries in
1465 * drm_atomic_helper_commit() using a bunch of helper functions. See
1466 * drm_atomic_helper_setup_commit() for a starting point.
1467 */
1468
1469 static int stall_checks(struct drm_crtc *crtc, bool nonblock)
1470 {
1471 struct drm_crtc_commit *commit, *stall_commit = NULL;
1472 bool completed = true;
1473 int i;
1474 long ret = 0;
1475
1476 spin_lock(&crtc->commit_lock);
1477 i = 0;
1478 list_for_each_entry(commit, &crtc->commit_list, commit_entry) {
1479 if (i == 0) {
1480 completed = try_wait_for_completion(&commit->flip_done);
1481 /* Userspace is not allowed to get ahead of the previous
1482 * commit with nonblocking ones. */
1483 if (!completed && nonblock) {
1484 spin_unlock(&crtc->commit_lock);
1485 return -EBUSY;
1486 }
1487 } else if (i == 1) {
1488 stall_commit = commit;
1489 drm_crtc_commit_get(stall_commit);
1490 break;
1491 }
1492
1493 i++;
1494 }
1495 spin_unlock(&crtc->commit_lock);
1496
1497 if (!stall_commit)
1498 return 0;
1499
1500 /* We don't want to let commits get ahead of cleanup work too much,
1501 * stalling on 2nd previous commit means triple-buffer won't ever stall.
1502 */
1503 ret = wait_for_completion_interruptible_timeout(&stall_commit->cleanup_done,
1504 10*HZ);
1505 if (ret == 0)
1506 DRM_ERROR("[CRTC:%d:%s] cleanup_done timed out\n",
1507 crtc->base.id, crtc->name);
1508
1509 drm_crtc_commit_put(stall_commit);
1510
1511 return ret < 0 ? ret : 0;
1512 }
1513
1514 static void release_crtc_commit(struct completion *completion)
1515 {
1516 struct drm_crtc_commit *commit = container_of(completion,
1517 typeof(*commit),
1518 flip_done);
1519
1520 drm_crtc_commit_put(commit);
1521 }
1522
1523 /**
1524 * drm_atomic_helper_setup_commit - setup possibly nonblocking commit
1525 * @state: new modeset state to be committed
1526 * @nonblock: whether nonblocking behavior is requested.
1527 *
1528 * This function prepares @state to be used by the atomic helper's support for
1529 * nonblocking commits. Drivers using the nonblocking commit infrastructure
1530 * should always call this function from their
1531 * &drm_mode_config_funcs.atomic_commit hook.
1532 *
1533 * To be able to use this support drivers need to use a few more helper
1534 * functions. drm_atomic_helper_wait_for_dependencies() must be called before
1535 * actually committing the hardware state, and for nonblocking commits this call
1536 * must be placed in the async worker. See also drm_atomic_helper_swap_state()
1537 * and it's stall parameter, for when a driver's commit hooks look at the
1538 * &drm_crtc.state, &drm_plane.state or &drm_connector.state pointer directly.
1539 *
1540 * Completion of the hardware commit step must be signalled using
1541 * drm_atomic_helper_commit_hw_done(). After this step the driver is not allowed
1542 * to read or change any permanent software or hardware modeset state. The only
1543 * exception is state protected by other means than &drm_modeset_lock locks.
1544 * Only the free standing @state with pointers to the old state structures can
1545 * be inspected, e.g. to clean up old buffers using
1546 * drm_atomic_helper_cleanup_planes().
1547 *
1548 * At the very end, before cleaning up @state drivers must call
1549 * drm_atomic_helper_commit_cleanup_done().
1550 *
1551 * This is all implemented by in drm_atomic_helper_commit(), giving drivers a
1552 * complete and esay-to-use default implementation of the atomic_commit() hook.
1553 *
1554 * The tracking of asynchronously executed and still pending commits is done
1555 * using the core structure &drm_crtc_commit.
1556 *
1557 * By default there's no need to clean up resources allocated by this function
1558 * explicitly: drm_atomic_state_default_clear() will take care of that
1559 * automatically.
1560 *
1561 * Returns:
1562 *
1563 * 0 on success. -EBUSY when userspace schedules nonblocking commits too fast,
1564 * -ENOMEM on allocation failures and -EINTR when a signal is pending.
1565 */
1566 int drm_atomic_helper_setup_commit(struct drm_atomic_state *state,
1567 bool nonblock)
1568 {
1569 struct drm_crtc *crtc;
1570 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
1571 struct drm_crtc_commit *commit;
1572 int i, ret;
1573
1574 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
1575 commit = kzalloc(sizeof(*commit), GFP_KERNEL);
1576 if (!commit)
1577 return -ENOMEM;
1578
1579 init_completion(&commit->flip_done);
1580 init_completion(&commit->hw_done);
1581 init_completion(&commit->cleanup_done);
1582 INIT_LIST_HEAD(&commit->commit_entry);
1583 kref_init(&commit->ref);
1584 commit->crtc = crtc;
1585
1586 state->crtcs[i].commit = commit;
1587
1588 ret = stall_checks(crtc, nonblock);
1589 if (ret)
1590 return ret;
1591
1592 /* Drivers only send out events when at least either current or
1593 * new CRTC state is active. Complete right away if everything
1594 * stays off. */
1595 if (!old_crtc_state->active && !new_crtc_state->active) {
1596 complete_all(&commit->flip_done);
1597 continue;
1598 }
1599
1600 /* Legacy cursor updates are fully unsynced. */
1601 if (state->legacy_cursor_update) {
1602 complete_all(&commit->flip_done);
1603 continue;
1604 }
1605
1606 if (!new_crtc_state->event) {
1607 commit->event = kzalloc(sizeof(*commit->event),
1608 GFP_KERNEL);
1609 if (!commit->event)
1610 return -ENOMEM;
1611
1612 new_crtc_state->event = commit->event;
1613 }
1614
1615 new_crtc_state->event->base.completion = &commit->flip_done;
1616 new_crtc_state->event->base.completion_release = release_crtc_commit;
1617 drm_crtc_commit_get(commit);
1618 }
1619
1620 return 0;
1621 }
1622 EXPORT_SYMBOL(drm_atomic_helper_setup_commit);
1623
1624
1625 static struct drm_crtc_commit *preceeding_commit(struct drm_crtc *crtc)
1626 {
1627 struct drm_crtc_commit *commit;
1628 int i = 0;
1629
1630 list_for_each_entry(commit, &crtc->commit_list, commit_entry) {
1631 /* skip the first entry, that's the current commit */
1632 if (i == 1)
1633 return commit;
1634 i++;
1635 }
1636
1637 return NULL;
1638 }
1639
1640 /**
1641 * drm_atomic_helper_wait_for_dependencies - wait for required preceeding commits
1642 * @old_state: atomic state object with old state structures
1643 *
1644 * This function waits for all preceeding commits that touch the same CRTC as
1645 * @old_state to both be committed to the hardware (as signalled by
1646 * drm_atomic_helper_commit_hw_done) and executed by the hardware (as signalled
1647 * by calling drm_crtc_vblank_send_event() on the &drm_crtc_state.event).
1648 *
1649 * This is part of the atomic helper support for nonblocking commits, see
1650 * drm_atomic_helper_setup_commit() for an overview.
1651 */
1652 void drm_atomic_helper_wait_for_dependencies(struct drm_atomic_state *old_state)
1653 {
1654 struct drm_crtc *crtc;
1655 struct drm_crtc_state *new_crtc_state;
1656 struct drm_crtc_commit *commit;
1657 int i;
1658 long ret;
1659
1660 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
1661 spin_lock(&crtc->commit_lock);
1662 commit = preceeding_commit(crtc);
1663 if (commit)
1664 drm_crtc_commit_get(commit);
1665 spin_unlock(&crtc->commit_lock);
1666
1667 if (!commit)
1668 continue;
1669
1670 ret = wait_for_completion_timeout(&commit->hw_done,
1671 10*HZ);
1672 if (ret == 0)
1673 DRM_ERROR("[CRTC:%d:%s] hw_done timed out\n",
1674 crtc->base.id, crtc->name);
1675
1676 /* Currently no support for overwriting flips, hence
1677 * stall for previous one to execute completely. */
1678 ret = wait_for_completion_timeout(&commit->flip_done,
1679 10*HZ);
1680 if (ret == 0)
1681 DRM_ERROR("[CRTC:%d:%s] flip_done timed out\n",
1682 crtc->base.id, crtc->name);
1683
1684 drm_crtc_commit_put(commit);
1685 }
1686 }
1687 EXPORT_SYMBOL(drm_atomic_helper_wait_for_dependencies);
1688
1689 /**
1690 * drm_atomic_helper_commit_hw_done - setup possible nonblocking commit
1691 * @old_state: atomic state object with old state structures
1692 *
1693 * This function is used to signal completion of the hardware commit step. After
1694 * this step the driver is not allowed to read or change any permanent software
1695 * or hardware modeset state. The only exception is state protected by other
1696 * means than &drm_modeset_lock locks.
1697 *
1698 * Drivers should try to postpone any expensive or delayed cleanup work after
1699 * this function is called.
1700 *
1701 * This is part of the atomic helper support for nonblocking commits, see
1702 * drm_atomic_helper_setup_commit() for an overview.
1703 */
1704 void drm_atomic_helper_commit_hw_done(struct drm_atomic_state *old_state)
1705 {
1706 struct drm_crtc *crtc;
1707 struct drm_crtc_state *new_crtc_state;
1708 struct drm_crtc_commit *commit;
1709 int i;
1710
1711 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
1712 commit = old_state->crtcs[i].commit;
1713 if (!commit)
1714 continue;
1715
1716 /* backend must have consumed any event by now */
1717 WARN_ON(new_crtc_state->event);
1718 spin_lock(&crtc->commit_lock);
1719 complete_all(&commit->hw_done);
1720 spin_unlock(&crtc->commit_lock);
1721 }
1722 }
1723 EXPORT_SYMBOL(drm_atomic_helper_commit_hw_done);
1724
1725 /**
1726 * drm_atomic_helper_commit_cleanup_done - signal completion of commit
1727 * @old_state: atomic state object with old state structures
1728 *
1729 * This signals completion of the atomic update @old_state, including any
1730 * cleanup work. If used, it must be called right before calling
1731 * drm_atomic_state_put().
1732 *
1733 * This is part of the atomic helper support for nonblocking commits, see
1734 * drm_atomic_helper_setup_commit() for an overview.
1735 */
1736 void drm_atomic_helper_commit_cleanup_done(struct drm_atomic_state *old_state)
1737 {
1738 struct drm_crtc *crtc;
1739 struct drm_crtc_state *new_crtc_state;
1740 struct drm_crtc_commit *commit;
1741 int i;
1742 long ret;
1743
1744 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
1745 commit = old_state->crtcs[i].commit;
1746 if (WARN_ON(!commit))
1747 continue;
1748
1749 spin_lock(&crtc->commit_lock);
1750 complete_all(&commit->cleanup_done);
1751 WARN_ON(!try_wait_for_completion(&commit->hw_done));
1752
1753 /* commit_list borrows our reference, need to remove before we
1754 * clean up our drm_atomic_state. But only after it actually
1755 * completed, otherwise subsequent commits won't stall properly. */
1756 if (try_wait_for_completion(&commit->flip_done))
1757 goto del_commit;
1758
1759 spin_unlock(&crtc->commit_lock);
1760
1761 /* We must wait for the vblank event to signal our completion
1762 * before releasing our reference, since the vblank work does
1763 * not hold a reference of its own. */
1764 ret = wait_for_completion_timeout(&commit->flip_done,
1765 10*HZ);
1766 if (ret == 0)
1767 DRM_ERROR("[CRTC:%d:%s] flip_done timed out\n",
1768 crtc->base.id, crtc->name);
1769
1770 spin_lock(&crtc->commit_lock);
1771 del_commit:
1772 list_del(&commit->commit_entry);
1773 spin_unlock(&crtc->commit_lock);
1774 }
1775 }
1776 EXPORT_SYMBOL(drm_atomic_helper_commit_cleanup_done);
1777
1778 /**
1779 * drm_atomic_helper_prepare_planes - prepare plane resources before commit
1780 * @dev: DRM device
1781 * @state: atomic state object with new state structures
1782 *
1783 * This function prepares plane state, specifically framebuffers, for the new
1784 * configuration, by calling &drm_plane_helper_funcs.prepare_fb. If any failure
1785 * is encountered this function will call &drm_plane_helper_funcs.cleanup_fb on
1786 * any already successfully prepared framebuffer.
1787 *
1788 * Returns:
1789 * 0 on success, negative error code on failure.
1790 */
1791 int drm_atomic_helper_prepare_planes(struct drm_device *dev,
1792 struct drm_atomic_state *state)
1793 {
1794 struct drm_plane *plane;
1795 struct drm_plane_state *new_plane_state;
1796 int ret, i, j;
1797
1798 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
1799 const struct drm_plane_helper_funcs *funcs;
1800
1801 funcs = plane->helper_private;
1802
1803 if (funcs->prepare_fb) {
1804 ret = funcs->prepare_fb(plane, new_plane_state);
1805 if (ret)
1806 goto fail;
1807 }
1808 }
1809
1810 return 0;
1811
1812 fail:
1813 for_each_new_plane_in_state(state, plane, new_plane_state, j) {
1814 const struct drm_plane_helper_funcs *funcs;
1815
1816 if (j >= i)
1817 continue;
1818
1819 funcs = plane->helper_private;
1820
1821 if (funcs->cleanup_fb)
1822 funcs->cleanup_fb(plane, new_plane_state);
1823 }
1824
1825 return ret;
1826 }
1827 EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
1828
1829 static bool plane_crtc_active(const struct drm_plane_state *state)
1830 {
1831 return state->crtc && state->crtc->state->active;
1832 }
1833
1834 /**
1835 * drm_atomic_helper_commit_planes - commit plane state
1836 * @dev: DRM device
1837 * @old_state: atomic state object with old state structures
1838 * @flags: flags for committing plane state
1839 *
1840 * This function commits the new plane state using the plane and atomic helper
1841 * functions for planes and crtcs. It assumes that the atomic state has already
1842 * been pushed into the relevant object state pointers, since this step can no
1843 * longer fail.
1844 *
1845 * It still requires the global state object @old_state to know which planes and
1846 * crtcs need to be updated though.
1847 *
1848 * Note that this function does all plane updates across all CRTCs in one step.
1849 * If the hardware can't support this approach look at
1850 * drm_atomic_helper_commit_planes_on_crtc() instead.
1851 *
1852 * Plane parameters can be updated by applications while the associated CRTC is
1853 * disabled. The DRM/KMS core will store the parameters in the plane state,
1854 * which will be available to the driver when the CRTC is turned on. As a result
1855 * most drivers don't need to be immediately notified of plane updates for a
1856 * disabled CRTC.
1857 *
1858 * Unless otherwise needed, drivers are advised to set the ACTIVE_ONLY flag in
1859 * @flags in order not to receive plane update notifications related to a
1860 * disabled CRTC. This avoids the need to manually ignore plane updates in
1861 * driver code when the driver and/or hardware can't or just don't need to deal
1862 * with updates on disabled CRTCs, for example when supporting runtime PM.
1863 *
1864 * Drivers may set the NO_DISABLE_AFTER_MODESET flag in @flags if the relevant
1865 * display controllers require to disable a CRTC's planes when the CRTC is
1866 * disabled. This function would skip the &drm_plane_helper_funcs.atomic_disable
1867 * call for a plane if the CRTC of the old plane state needs a modesetting
1868 * operation. Of course, the drivers need to disable the planes in their CRTC
1869 * disable callbacks since no one else would do that.
1870 *
1871 * The drm_atomic_helper_commit() default implementation doesn't set the
1872 * ACTIVE_ONLY flag to most closely match the behaviour of the legacy helpers.
1873 * This should not be copied blindly by drivers.
1874 */
1875 void drm_atomic_helper_commit_planes(struct drm_device *dev,
1876 struct drm_atomic_state *old_state,
1877 uint32_t flags)
1878 {
1879 struct drm_crtc *crtc;
1880 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
1881 struct drm_plane *plane;
1882 struct drm_plane_state *old_plane_state, *new_plane_state;
1883 int i;
1884 bool active_only = flags & DRM_PLANE_COMMIT_ACTIVE_ONLY;
1885 bool no_disable = flags & DRM_PLANE_COMMIT_NO_DISABLE_AFTER_MODESET;
1886
1887 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
1888 const struct drm_crtc_helper_funcs *funcs;
1889
1890 funcs = crtc->helper_private;
1891
1892 if (!funcs || !funcs->atomic_begin)
1893 continue;
1894
1895 if (active_only && !new_crtc_state->active)
1896 continue;
1897
1898 funcs->atomic_begin(crtc, old_crtc_state);
1899 }
1900
1901 for_each_oldnew_plane_in_state(old_state, plane, old_plane_state, new_plane_state, i) {
1902 const struct drm_plane_helper_funcs *funcs;
1903 bool disabling;
1904
1905 funcs = plane->helper_private;
1906
1907 if (!funcs)
1908 continue;
1909
1910 disabling = drm_atomic_plane_disabling(old_plane_state,
1911 new_plane_state);
1912
1913 if (active_only) {
1914 /*
1915 * Skip planes related to inactive CRTCs. If the plane
1916 * is enabled use the state of the current CRTC. If the
1917 * plane is being disabled use the state of the old
1918 * CRTC to avoid skipping planes being disabled on an
1919 * active CRTC.
1920 */
1921 if (!disabling && !plane_crtc_active(new_plane_state))
1922 continue;
1923 if (disabling && !plane_crtc_active(old_plane_state))
1924 continue;
1925 }
1926
1927 /*
1928 * Special-case disabling the plane if drivers support it.
1929 */
1930 if (disabling && funcs->atomic_disable) {
1931 struct drm_crtc_state *crtc_state;
1932
1933 crtc_state = old_plane_state->crtc->state;
1934
1935 if (drm_atomic_crtc_needs_modeset(crtc_state) &&
1936 no_disable)
1937 continue;
1938
1939 funcs->atomic_disable(plane, old_plane_state);
1940 } else if (new_plane_state->crtc || disabling) {
1941 funcs->atomic_update(plane, old_plane_state);
1942 }
1943 }
1944
1945 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
1946 const struct drm_crtc_helper_funcs *funcs;
1947
1948 funcs = crtc->helper_private;
1949
1950 if (!funcs || !funcs->atomic_flush)
1951 continue;
1952
1953 if (active_only && !new_crtc_state->active)
1954 continue;
1955
1956 funcs->atomic_flush(crtc, old_crtc_state);
1957 }
1958 }
1959 EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
1960
1961 /**
1962 * drm_atomic_helper_commit_planes_on_crtc - commit plane state for a crtc
1963 * @old_crtc_state: atomic state object with the old crtc state
1964 *
1965 * This function commits the new plane state using the plane and atomic helper
1966 * functions for planes on the specific crtc. It assumes that the atomic state
1967 * has already been pushed into the relevant object state pointers, since this
1968 * step can no longer fail.
1969 *
1970 * This function is useful when plane updates should be done crtc-by-crtc
1971 * instead of one global step like drm_atomic_helper_commit_planes() does.
1972 *
1973 * This function can only be savely used when planes are not allowed to move
1974 * between different CRTCs because this function doesn't handle inter-CRTC
1975 * depencies. Callers need to ensure that either no such depencies exist,
1976 * resolve them through ordering of commit calls or through some other means.
1977 */
1978 void
1979 drm_atomic_helper_commit_planes_on_crtc(struct drm_crtc_state *old_crtc_state)
1980 {
1981 const struct drm_crtc_helper_funcs *crtc_funcs;
1982 struct drm_crtc *crtc = old_crtc_state->crtc;
1983 struct drm_atomic_state *old_state = old_crtc_state->state;
1984 struct drm_plane *plane;
1985 unsigned plane_mask;
1986
1987 plane_mask = old_crtc_state->plane_mask;
1988 plane_mask |= crtc->state->plane_mask;
1989
1990 crtc_funcs = crtc->helper_private;
1991 if (crtc_funcs && crtc_funcs->atomic_begin)
1992 crtc_funcs->atomic_begin(crtc, old_crtc_state);
1993
1994 drm_for_each_plane_mask(plane, crtc->dev, plane_mask) {
1995 struct drm_plane_state *old_plane_state =
1996 drm_atomic_get_old_plane_state(old_state, plane);
1997 const struct drm_plane_helper_funcs *plane_funcs;
1998
1999 plane_funcs = plane->helper_private;
2000
2001 if (!old_plane_state || !plane_funcs)
2002 continue;
2003
2004 WARN_ON(plane->state->crtc && plane->state->crtc != crtc);
2005
2006 if (drm_atomic_plane_disabling(old_plane_state, plane->state) &&
2007 plane_funcs->atomic_disable)
2008 plane_funcs->atomic_disable(plane, old_plane_state);
2009 else if (plane->state->crtc ||
2010 drm_atomic_plane_disabling(old_plane_state, plane->state))
2011 plane_funcs->atomic_update(plane, old_plane_state);
2012 }
2013
2014 if (crtc_funcs && crtc_funcs->atomic_flush)
2015 crtc_funcs->atomic_flush(crtc, old_crtc_state);
2016 }
2017 EXPORT_SYMBOL(drm_atomic_helper_commit_planes_on_crtc);
2018
2019 /**
2020 * drm_atomic_helper_disable_planes_on_crtc - helper to disable CRTC's planes
2021 * @old_crtc_state: atomic state object with the old CRTC state
2022 * @atomic: if set, synchronize with CRTC's atomic_begin/flush hooks
2023 *
2024 * Disables all planes associated with the given CRTC. This can be
2025 * used for instance in the CRTC helper atomic_disable callback to disable
2026 * all planes.
2027 *
2028 * If the atomic-parameter is set the function calls the CRTC's
2029 * atomic_begin hook before and atomic_flush hook after disabling the
2030 * planes.
2031 *
2032 * It is a bug to call this function without having implemented the
2033 * &drm_plane_helper_funcs.atomic_disable plane hook.
2034 */
2035 void
2036 drm_atomic_helper_disable_planes_on_crtc(struct drm_crtc_state *old_crtc_state,
2037 bool atomic)
2038 {
2039 struct drm_crtc *crtc = old_crtc_state->crtc;
2040 const struct drm_crtc_helper_funcs *crtc_funcs =
2041 crtc->helper_private;
2042 struct drm_plane *plane;
2043
2044 if (atomic && crtc_funcs && crtc_funcs->atomic_begin)
2045 crtc_funcs->atomic_begin(crtc, NULL);
2046
2047 drm_atomic_crtc_state_for_each_plane(plane, old_crtc_state) {
2048 const struct drm_plane_helper_funcs *plane_funcs =
2049 plane->helper_private;
2050
2051 if (!plane_funcs)
2052 continue;
2053
2054 WARN_ON(!plane_funcs->atomic_disable);
2055 if (plane_funcs->atomic_disable)
2056 plane_funcs->atomic_disable(plane, NULL);
2057 }
2058
2059 if (atomic && crtc_funcs && crtc_funcs->atomic_flush)
2060 crtc_funcs->atomic_flush(crtc, NULL);
2061 }
2062 EXPORT_SYMBOL(drm_atomic_helper_disable_planes_on_crtc);
2063
2064 /**
2065 * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
2066 * @dev: DRM device
2067 * @old_state: atomic state object with old state structures
2068 *
2069 * This function cleans up plane state, specifically framebuffers, from the old
2070 * configuration. Hence the old configuration must be perserved in @old_state to
2071 * be able to call this function.
2072 *
2073 * This function must also be called on the new state when the atomic update
2074 * fails at any point after calling drm_atomic_helper_prepare_planes().
2075 */
2076 void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
2077 struct drm_atomic_state *old_state)
2078 {
2079 struct drm_plane *plane;
2080 struct drm_plane_state *old_plane_state, *new_plane_state;
2081 int i;
2082
2083 for_each_oldnew_plane_in_state(old_state, plane, old_plane_state, new_plane_state, i) {
2084 const struct drm_plane_helper_funcs *funcs;
2085 struct drm_plane_state *plane_state;
2086
2087 /*
2088 * This might be called before swapping when commit is aborted,
2089 * in which case we have to cleanup the new state.
2090 */
2091 if (old_plane_state == plane->state)
2092 plane_state = new_plane_state;
2093 else
2094 plane_state = old_plane_state;
2095
2096 funcs = plane->helper_private;
2097
2098 if (funcs->cleanup_fb)
2099 funcs->cleanup_fb(plane, plane_state);
2100 }
2101 }
2102 EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
2103
2104 /**
2105 * drm_atomic_helper_swap_state - store atomic state into current sw state
2106 * @state: atomic state
2107 * @stall: stall for proceeding commits
2108 *
2109 * This function stores the atomic state into the current state pointers in all
2110 * driver objects. It should be called after all failing steps have been done
2111 * and succeeded, but before the actual hardware state is committed.
2112 *
2113 * For cleanup and error recovery the current state for all changed objects will
2114 * be swaped into @state.
2115 *
2116 * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
2117 *
2118 * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
2119 *
2120 * 2. Do any other steps that might fail.
2121 *
2122 * 3. Put the staged state into the current state pointers with this function.
2123 *
2124 * 4. Actually commit the hardware state.
2125 *
2126 * 5. Call drm_atomic_helper_cleanup_planes() with @state, which since step 3
2127 * contains the old state. Also do any other cleanup required with that state.
2128 *
2129 * @stall must be set when nonblocking commits for this driver directly access
2130 * the &drm_plane.state, &drm_crtc.state or &drm_connector.state pointer. With
2131 * the current atomic helpers this is almost always the case, since the helpers
2132 * don't pass the right state structures to the callbacks.
2133 */
2134 void drm_atomic_helper_swap_state(struct drm_atomic_state *state,
2135 bool stall)
2136 {
2137 int i;
2138 long ret;
2139 struct drm_connector *connector;
2140 struct drm_connector_state *old_conn_state, *new_conn_state;
2141 struct drm_crtc *crtc;
2142 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
2143 struct drm_plane *plane;
2144 struct drm_plane_state *old_plane_state, *new_plane_state;
2145 struct drm_crtc_commit *commit;
2146 void *obj, *obj_state;
2147 const struct drm_private_state_funcs *funcs;
2148
2149 if (stall) {
2150 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
2151 spin_lock(&crtc->commit_lock);
2152 commit = list_first_entry_or_null(&crtc->commit_list,
2153 struct drm_crtc_commit, commit_entry);
2154 if (commit)
2155 drm_crtc_commit_get(commit);
2156 spin_unlock(&crtc->commit_lock);
2157
2158 if (!commit)
2159 continue;
2160
2161 ret = wait_for_completion_timeout(&commit->hw_done,
2162 10*HZ);
2163 if (ret == 0)
2164 DRM_ERROR("[CRTC:%d:%s] hw_done timed out\n",
2165 crtc->base.id, crtc->name);
2166 drm_crtc_commit_put(commit);
2167 }
2168 }
2169
2170 for_each_oldnew_connector_in_state(state, connector, old_conn_state, new_conn_state, i) {
2171 WARN_ON(connector->state != old_conn_state);
2172
2173 old_conn_state->state = state;
2174 new_conn_state->state = NULL;
2175
2176 state->connectors[i].state = old_conn_state;
2177 connector->state = new_conn_state;
2178 }
2179
2180 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
2181 WARN_ON(crtc->state != old_crtc_state);
2182
2183 old_crtc_state->state = state;
2184 new_crtc_state->state = NULL;
2185
2186 state->crtcs[i].state = old_crtc_state;
2187 crtc->state = new_crtc_state;
2188
2189 if (state->crtcs[i].commit) {
2190 spin_lock(&crtc->commit_lock);
2191 list_add(&state->crtcs[i].commit->commit_entry,
2192 &crtc->commit_list);
2193 spin_unlock(&crtc->commit_lock);
2194
2195 state->crtcs[i].commit->event = NULL;
2196 }
2197 }
2198
2199 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
2200 WARN_ON(plane->state != old_plane_state);
2201
2202 old_plane_state->state = state;
2203 new_plane_state->state = NULL;
2204
2205 state->planes[i].state = old_plane_state;
2206 plane->state = new_plane_state;
2207 }
2208
2209 __for_each_private_obj(state, obj, obj_state, i, funcs)
2210 funcs->swap_state(obj, &state->private_objs[i].obj_state);
2211 }
2212 EXPORT_SYMBOL(drm_atomic_helper_swap_state);
2213
2214 /**
2215 * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
2216 * @plane: plane object to update
2217 * @crtc: owning CRTC of owning plane
2218 * @fb: framebuffer to flip onto plane
2219 * @crtc_x: x offset of primary plane on crtc
2220 * @crtc_y: y offset of primary plane on crtc
2221 * @crtc_w: width of primary plane rectangle on crtc
2222 * @crtc_h: height of primary plane rectangle on crtc
2223 * @src_x: x offset of @fb for panning
2224 * @src_y: y offset of @fb for panning
2225 * @src_w: width of source rectangle in @fb
2226 * @src_h: height of source rectangle in @fb
2227 * @ctx: lock acquire context
2228 *
2229 * Provides a default plane update handler using the atomic driver interface.
2230 *
2231 * RETURNS:
2232 * Zero on success, error code on failure
2233 */
2234 int drm_atomic_helper_update_plane(struct drm_plane *plane,
2235 struct drm_crtc *crtc,
2236 struct drm_framebuffer *fb,
2237 int crtc_x, int crtc_y,
2238 unsigned int crtc_w, unsigned int crtc_h,
2239 uint32_t src_x, uint32_t src_y,
2240 uint32_t src_w, uint32_t src_h,
2241 struct drm_modeset_acquire_ctx *ctx)
2242 {
2243 struct drm_atomic_state *state;
2244 struct drm_plane_state *plane_state;
2245 int ret = 0;
2246
2247 state = drm_atomic_state_alloc(plane->dev);
2248 if (!state)
2249 return -ENOMEM;
2250
2251 state->acquire_ctx = ctx;
2252 plane_state = drm_atomic_get_plane_state(state, plane);
2253 if (IS_ERR(plane_state)) {
2254 ret = PTR_ERR(plane_state);
2255 goto fail;
2256 }
2257
2258 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
2259 if (ret != 0)
2260 goto fail;
2261 drm_atomic_set_fb_for_plane(plane_state, fb);
2262 plane_state->crtc_x = crtc_x;
2263 plane_state->crtc_y = crtc_y;
2264 plane_state->crtc_w = crtc_w;
2265 plane_state->crtc_h = crtc_h;
2266 plane_state->src_x = src_x;
2267 plane_state->src_y = src_y;
2268 plane_state->src_w = src_w;
2269 plane_state->src_h = src_h;
2270
2271 if (plane == crtc->cursor)
2272 state->legacy_cursor_update = true;
2273
2274 ret = drm_atomic_commit(state);
2275 fail:
2276 drm_atomic_state_put(state);
2277 return ret;
2278 }
2279 EXPORT_SYMBOL(drm_atomic_helper_update_plane);
2280
2281 /**
2282 * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
2283 * @plane: plane to disable
2284 * @ctx: lock acquire context
2285 *
2286 * Provides a default plane disable handler using the atomic driver interface.
2287 *
2288 * RETURNS:
2289 * Zero on success, error code on failure
2290 */
2291 int drm_atomic_helper_disable_plane(struct drm_plane *plane,
2292 struct drm_modeset_acquire_ctx *ctx)
2293 {
2294 struct drm_atomic_state *state;
2295 struct drm_plane_state *plane_state;
2296 int ret = 0;
2297
2298 state = drm_atomic_state_alloc(plane->dev);
2299 if (!state)
2300 return -ENOMEM;
2301
2302 state->acquire_ctx = ctx;
2303 plane_state = drm_atomic_get_plane_state(state, plane);
2304 if (IS_ERR(plane_state)) {
2305 ret = PTR_ERR(plane_state);
2306 goto fail;
2307 }
2308
2309 if (plane_state->crtc && (plane == plane->crtc->cursor))
2310 plane_state->state->legacy_cursor_update = true;
2311
2312 ret = __drm_atomic_helper_disable_plane(plane, plane_state);
2313 if (ret != 0)
2314 goto fail;
2315
2316 ret = drm_atomic_commit(state);
2317 fail:
2318 drm_atomic_state_put(state);
2319 return ret;
2320 }
2321 EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
2322
2323 /* just used from fb-helper and atomic-helper: */
2324 int __drm_atomic_helper_disable_plane(struct drm_plane *plane,
2325 struct drm_plane_state *plane_state)
2326 {
2327 int ret;
2328
2329 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
2330 if (ret != 0)
2331 return ret;
2332
2333 drm_atomic_set_fb_for_plane(plane_state, NULL);
2334 plane_state->crtc_x = 0;
2335 plane_state->crtc_y = 0;
2336 plane_state->crtc_w = 0;
2337 plane_state->crtc_h = 0;
2338 plane_state->src_x = 0;
2339 plane_state->src_y = 0;
2340 plane_state->src_w = 0;
2341 plane_state->src_h = 0;
2342
2343 return 0;
2344 }
2345
2346 static int update_output_state(struct drm_atomic_state *state,
2347 struct drm_mode_set *set)
2348 {
2349 struct drm_device *dev = set->crtc->dev;
2350 struct drm_crtc *crtc;
2351 struct drm_crtc_state *new_crtc_state;
2352 struct drm_connector *connector;
2353 struct drm_connector_state *new_conn_state;
2354 int ret, i;
2355
2356 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
2357 state->acquire_ctx);
2358 if (ret)
2359 return ret;
2360
2361 /* First disable all connectors on the target crtc. */
2362 ret = drm_atomic_add_affected_connectors(state, set->crtc);
2363 if (ret)
2364 return ret;
2365
2366 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
2367 if (new_conn_state->crtc == set->crtc) {
2368 ret = drm_atomic_set_crtc_for_connector(new_conn_state,
2369 NULL);
2370 if (ret)
2371 return ret;
2372
2373 /* Make sure legacy setCrtc always re-trains */
2374 new_conn_state->link_status = DRM_LINK_STATUS_GOOD;
2375 }
2376 }
2377
2378 /* Then set all connectors from set->connectors on the target crtc */
2379 for (i = 0; i < set->num_connectors; i++) {
2380 new_conn_state = drm_atomic_get_connector_state(state,
2381 set->connectors[i]);
2382 if (IS_ERR(new_conn_state))
2383 return PTR_ERR(new_conn_state);
2384
2385 ret = drm_atomic_set_crtc_for_connector(new_conn_state,
2386 set->crtc);
2387 if (ret)
2388 return ret;
2389 }
2390
2391 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
2392 /* Don't update ->enable for the CRTC in the set_config request,
2393 * since a mismatch would indicate a bug in the upper layers.
2394 * The actual modeset code later on will catch any
2395 * inconsistencies here. */
2396 if (crtc == set->crtc)
2397 continue;
2398
2399 if (!new_crtc_state->connector_mask) {
2400 ret = drm_atomic_set_mode_prop_for_crtc(new_crtc_state,
2401 NULL);
2402 if (ret < 0)
2403 return ret;
2404
2405 new_crtc_state->active = false;
2406 }
2407 }
2408
2409 return 0;
2410 }
2411
2412 /**
2413 * drm_atomic_helper_set_config - set a new config from userspace
2414 * @set: mode set configuration
2415 * @ctx: lock acquisition context
2416 *
2417 * Provides a default crtc set_config handler using the atomic driver interface.
2418 *
2419 * NOTE: For backwards compatibility with old userspace this automatically
2420 * resets the "link-status" property to GOOD, to force any link
2421 * re-training. The SETCRTC ioctl does not define whether an update does
2422 * need a full modeset or just a plane update, hence we're allowed to do
2423 * that. See also drm_mode_connector_set_link_status_property().
2424 *
2425 * Returns:
2426 * Returns 0 on success, negative errno numbers on failure.
2427 */
2428 int drm_atomic_helper_set_config(struct drm_mode_set *set,
2429 struct drm_modeset_acquire_ctx *ctx)
2430 {
2431 struct drm_atomic_state *state;
2432 struct drm_crtc *crtc = set->crtc;
2433 int ret = 0;
2434
2435 state = drm_atomic_state_alloc(crtc->dev);
2436 if (!state)
2437 return -ENOMEM;
2438
2439 state->acquire_ctx = ctx;
2440 ret = __drm_atomic_helper_set_config(set, state);
2441 if (ret != 0)
2442 goto fail;
2443
2444 ret = handle_conflicting_encoders(state, true);
2445 if (ret)
2446 return ret;
2447
2448 ret = drm_atomic_commit(state);
2449
2450 fail:
2451 drm_atomic_state_put(state);
2452 return ret;
2453 }
2454 EXPORT_SYMBOL(drm_atomic_helper_set_config);
2455
2456 /* just used from fb-helper and atomic-helper: */
2457 int __drm_atomic_helper_set_config(struct drm_mode_set *set,
2458 struct drm_atomic_state *state)
2459 {
2460 struct drm_crtc_state *crtc_state;
2461 struct drm_plane_state *primary_state;
2462 struct drm_crtc *crtc = set->crtc;
2463 int hdisplay, vdisplay;
2464 int ret;
2465
2466 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2467 if (IS_ERR(crtc_state))
2468 return PTR_ERR(crtc_state);
2469
2470 primary_state = drm_atomic_get_plane_state(state, crtc->primary);
2471 if (IS_ERR(primary_state))
2472 return PTR_ERR(primary_state);
2473
2474 if (!set->mode) {
2475 WARN_ON(set->fb);
2476 WARN_ON(set->num_connectors);
2477
2478 ret = drm_atomic_set_mode_for_crtc(crtc_state, NULL);
2479 if (ret != 0)
2480 return ret;
2481
2482 crtc_state->active = false;
2483
2484 ret = drm_atomic_set_crtc_for_plane(primary_state, NULL);
2485 if (ret != 0)
2486 return ret;
2487
2488 drm_atomic_set_fb_for_plane(primary_state, NULL);
2489
2490 goto commit;
2491 }
2492
2493 WARN_ON(!set->fb);
2494 WARN_ON(!set->num_connectors);
2495
2496 ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
2497 if (ret != 0)
2498 return ret;
2499
2500 crtc_state->active = true;
2501
2502 ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
2503 if (ret != 0)
2504 return ret;
2505
2506 drm_mode_get_hv_timing(set->mode, &hdisplay, &vdisplay);
2507
2508 drm_atomic_set_fb_for_plane(primary_state, set->fb);
2509 primary_state->crtc_x = 0;
2510 primary_state->crtc_y = 0;
2511 primary_state->crtc_w = hdisplay;
2512 primary_state->crtc_h = vdisplay;
2513 primary_state->src_x = set->x << 16;
2514 primary_state->src_y = set->y << 16;
2515 if (drm_rotation_90_or_270(primary_state->rotation)) {
2516 primary_state->src_w = vdisplay << 16;
2517 primary_state->src_h = hdisplay << 16;
2518 } else {
2519 primary_state->src_w = hdisplay << 16;
2520 primary_state->src_h = vdisplay << 16;
2521 }
2522
2523 commit:
2524 ret = update_output_state(state, set);
2525 if (ret)
2526 return ret;
2527
2528 return 0;
2529 }
2530
2531 /**
2532 * drm_atomic_helper_disable_all - disable all currently active outputs
2533 * @dev: DRM device
2534 * @ctx: lock acquisition context
2535 *
2536 * Loops through all connectors, finding those that aren't turned off and then
2537 * turns them off by setting their DPMS mode to OFF and deactivating the CRTC
2538 * that they are connected to.
2539 *
2540 * This is used for example in suspend/resume to disable all currently active
2541 * functions when suspending. If you just want to shut down everything at e.g.
2542 * driver unload, look at drm_atomic_helper_shutdown().
2543 *
2544 * Note that if callers haven't already acquired all modeset locks this might
2545 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
2546 *
2547 * Returns:
2548 * 0 on success or a negative error code on failure.
2549 *
2550 * See also:
2551 * drm_atomic_helper_suspend(), drm_atomic_helper_resume() and
2552 * drm_atomic_helper_shutdown().
2553 */
2554 int drm_atomic_helper_disable_all(struct drm_device *dev,
2555 struct drm_modeset_acquire_ctx *ctx)
2556 {
2557 struct drm_atomic_state *state;
2558 struct drm_connector_state *conn_state;
2559 struct drm_connector *conn;
2560 struct drm_plane_state *plane_state;
2561 struct drm_plane *plane;
2562 struct drm_crtc_state *crtc_state;
2563 struct drm_crtc *crtc;
2564 int ret, i;
2565
2566 state = drm_atomic_state_alloc(dev);
2567 if (!state)
2568 return -ENOMEM;
2569
2570 state->acquire_ctx = ctx;
2571
2572 drm_for_each_crtc(crtc, dev) {
2573 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2574 if (IS_ERR(crtc_state)) {
2575 ret = PTR_ERR(crtc_state);
2576 goto free;
2577 }
2578
2579 crtc_state->active = false;
2580
2581 ret = drm_atomic_set_mode_prop_for_crtc(crtc_state, NULL);
2582 if (ret < 0)
2583 goto free;
2584
2585 ret = drm_atomic_add_affected_planes(state, crtc);
2586 if (ret < 0)
2587 goto free;
2588
2589 ret = drm_atomic_add_affected_connectors(state, crtc);
2590 if (ret < 0)
2591 goto free;
2592 }
2593
2594 for_each_connector_in_state(state, conn, conn_state, i) {
2595 ret = drm_atomic_set_crtc_for_connector(conn_state, NULL);
2596 if (ret < 0)
2597 goto free;
2598 }
2599
2600 for_each_plane_in_state(state, plane, plane_state, i) {
2601 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
2602 if (ret < 0)
2603 goto free;
2604
2605 drm_atomic_set_fb_for_plane(plane_state, NULL);
2606 }
2607
2608 ret = drm_atomic_commit(state);
2609 free:
2610 drm_atomic_state_put(state);
2611 return ret;
2612 }
2613
2614 EXPORT_SYMBOL(drm_atomic_helper_disable_all);
2615
2616 /**
2617 * drm_atomic_helper_shutdown - shutdown all CRTC
2618 * @dev: DRM device
2619 *
2620 * This shuts down all CRTC, which is useful for driver unloading. Shutdown on
2621 * suspend should instead be handled with drm_atomic_helper_suspend(), since
2622 * that also takes a snapshot of the modeset state to be restored on resume.
2623 *
2624 * This is just a convenience wrapper around drm_atomic_helper_disable_all(),
2625 * and it is the atomic version of drm_crtc_force_disable_all().
2626 */
2627 void drm_atomic_helper_shutdown(struct drm_device *dev)
2628 {
2629 struct drm_modeset_acquire_ctx ctx;
2630 int ret;
2631
2632 drm_modeset_acquire_init(&ctx, 0);
2633 while (1) {
2634 ret = drm_modeset_lock_all_ctx(dev, &ctx);
2635 if (!ret)
2636 ret = drm_atomic_helper_disable_all(dev, &ctx);
2637
2638 if (ret != -EDEADLK)
2639 break;
2640
2641 drm_modeset_backoff(&ctx);
2642 }
2643
2644 if (ret)
2645 DRM_ERROR("Disabling all crtc's during unload failed with %i\n", ret);
2646
2647 drm_modeset_drop_locks(&ctx);
2648 drm_modeset_acquire_fini(&ctx);
2649 }
2650 EXPORT_SYMBOL(drm_atomic_helper_shutdown);
2651
2652 /**
2653 * drm_atomic_helper_suspend - subsystem-level suspend helper
2654 * @dev: DRM device
2655 *
2656 * Duplicates the current atomic state, disables all active outputs and then
2657 * returns a pointer to the original atomic state to the caller. Drivers can
2658 * pass this pointer to the drm_atomic_helper_resume() helper upon resume to
2659 * restore the output configuration that was active at the time the system
2660 * entered suspend.
2661 *
2662 * Note that it is potentially unsafe to use this. The atomic state object
2663 * returned by this function is assumed to be persistent. Drivers must ensure
2664 * that this holds true. Before calling this function, drivers must make sure
2665 * to suspend fbdev emulation so that nothing can be using the device.
2666 *
2667 * Returns:
2668 * A pointer to a copy of the state before suspend on success or an ERR_PTR()-
2669 * encoded error code on failure. Drivers should store the returned atomic
2670 * state object and pass it to the drm_atomic_helper_resume() helper upon
2671 * resume.
2672 *
2673 * See also:
2674 * drm_atomic_helper_duplicate_state(), drm_atomic_helper_disable_all(),
2675 * drm_atomic_helper_resume(), drm_atomic_helper_commit_duplicated_state()
2676 */
2677 struct drm_atomic_state *drm_atomic_helper_suspend(struct drm_device *dev)
2678 {
2679 struct drm_modeset_acquire_ctx ctx;
2680 struct drm_atomic_state *state;
2681 int err;
2682
2683 drm_modeset_acquire_init(&ctx, 0);
2684
2685 retry:
2686 err = drm_modeset_lock_all_ctx(dev, &ctx);
2687 if (err < 0) {
2688 state = ERR_PTR(err);
2689 goto unlock;
2690 }
2691
2692 state = drm_atomic_helper_duplicate_state(dev, &ctx);
2693 if (IS_ERR(state))
2694 goto unlock;
2695
2696 err = drm_atomic_helper_disable_all(dev, &ctx);
2697 if (err < 0) {
2698 drm_atomic_state_put(state);
2699 state = ERR_PTR(err);
2700 goto unlock;
2701 }
2702
2703 unlock:
2704 if (PTR_ERR(state) == -EDEADLK) {
2705 drm_modeset_backoff(&ctx);
2706 goto retry;
2707 }
2708
2709 drm_modeset_drop_locks(&ctx);
2710 drm_modeset_acquire_fini(&ctx);
2711 return state;
2712 }
2713 EXPORT_SYMBOL(drm_atomic_helper_suspend);
2714
2715 /**
2716 * drm_atomic_helper_commit_duplicated_state - commit duplicated state
2717 * @state: duplicated atomic state to commit
2718 * @ctx: pointer to acquire_ctx to use for commit.
2719 *
2720 * The state returned by drm_atomic_helper_duplicate_state() and
2721 * drm_atomic_helper_suspend() is partially invalid, and needs to
2722 * be fixed up before commit.
2723 *
2724 * Returns:
2725 * 0 on success or a negative error code on failure.
2726 *
2727 * See also:
2728 * drm_atomic_helper_suspend()
2729 */
2730 int drm_atomic_helper_commit_duplicated_state(struct drm_atomic_state *state,
2731 struct drm_modeset_acquire_ctx *ctx)
2732 {
2733 int i;
2734 struct drm_plane *plane;
2735 struct drm_plane_state *new_plane_state;
2736 struct drm_connector *connector;
2737 struct drm_connector_state *new_conn_state;
2738 struct drm_crtc *crtc;
2739 struct drm_crtc_state *new_crtc_state;
2740
2741 state->acquire_ctx = ctx;
2742
2743 for_each_new_plane_in_state(state, plane, new_plane_state, i)
2744 state->planes[i].old_state = plane->state;
2745
2746 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i)
2747 state->crtcs[i].old_state = crtc->state;
2748
2749 for_each_new_connector_in_state(state, connector, new_conn_state, i)
2750 state->connectors[i].old_state = connector->state;
2751
2752 return drm_atomic_commit(state);
2753 }
2754 EXPORT_SYMBOL(drm_atomic_helper_commit_duplicated_state);
2755
2756 /**
2757 * drm_atomic_helper_resume - subsystem-level resume helper
2758 * @dev: DRM device
2759 * @state: atomic state to resume to
2760 *
2761 * Calls drm_mode_config_reset() to synchronize hardware and software states,
2762 * grabs all modeset locks and commits the atomic state object. This can be
2763 * used in conjunction with the drm_atomic_helper_suspend() helper to
2764 * implement suspend/resume for drivers that support atomic mode-setting.
2765 *
2766 * Returns:
2767 * 0 on success or a negative error code on failure.
2768 *
2769 * See also:
2770 * drm_atomic_helper_suspend()
2771 */
2772 int drm_atomic_helper_resume(struct drm_device *dev,
2773 struct drm_atomic_state *state)
2774 {
2775 struct drm_modeset_acquire_ctx ctx;
2776 int err;
2777
2778 drm_mode_config_reset(dev);
2779
2780 drm_modeset_acquire_init(&ctx, 0);
2781 while (1) {
2782 err = drm_atomic_helper_commit_duplicated_state(state, &ctx);
2783 if (err != -EDEADLK)
2784 break;
2785
2786 drm_modeset_backoff(&ctx);
2787 }
2788
2789 drm_modeset_drop_locks(&ctx);
2790 drm_modeset_acquire_fini(&ctx);
2791
2792 return err;
2793 }
2794 EXPORT_SYMBOL(drm_atomic_helper_resume);
2795
2796 /**
2797 * drm_atomic_helper_crtc_set_property - helper for crtc properties
2798 * @crtc: DRM crtc
2799 * @property: DRM property
2800 * @val: value of property
2801 *
2802 * Provides a default crtc set_property handler using the atomic driver
2803 * interface.
2804 *
2805 * RETURNS:
2806 * Zero on success, error code on failure
2807 */
2808 int
2809 drm_atomic_helper_crtc_set_property(struct drm_crtc *crtc,
2810 struct drm_property *property,
2811 uint64_t val)
2812 {
2813 struct drm_atomic_state *state;
2814 struct drm_crtc_state *crtc_state;
2815 int ret = 0;
2816
2817 state = drm_atomic_state_alloc(crtc->dev);
2818 if (!state)
2819 return -ENOMEM;
2820
2821 /* ->set_property is always called with all locks held. */
2822 state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
2823 retry:
2824 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2825 if (IS_ERR(crtc_state)) {
2826 ret = PTR_ERR(crtc_state);
2827 goto fail;
2828 }
2829
2830 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
2831 property, val);
2832 if (ret)
2833 goto fail;
2834
2835 ret = drm_atomic_commit(state);
2836 fail:
2837 if (ret == -EDEADLK)
2838 goto backoff;
2839
2840 drm_atomic_state_put(state);
2841 return ret;
2842
2843 backoff:
2844 drm_atomic_state_clear(state);
2845 drm_atomic_legacy_backoff(state);
2846
2847 goto retry;
2848 }
2849 EXPORT_SYMBOL(drm_atomic_helper_crtc_set_property);
2850
2851 /**
2852 * drm_atomic_helper_plane_set_property - helper for plane properties
2853 * @plane: DRM plane
2854 * @property: DRM property
2855 * @val: value of property
2856 *
2857 * Provides a default plane set_property handler using the atomic driver
2858 * interface.
2859 *
2860 * RETURNS:
2861 * Zero on success, error code on failure
2862 */
2863 int
2864 drm_atomic_helper_plane_set_property(struct drm_plane *plane,
2865 struct drm_property *property,
2866 uint64_t val)
2867 {
2868 struct drm_atomic_state *state;
2869 struct drm_plane_state *plane_state;
2870 int ret = 0;
2871
2872 state = drm_atomic_state_alloc(plane->dev);
2873 if (!state)
2874 return -ENOMEM;
2875
2876 /* ->set_property is always called with all locks held. */
2877 state->acquire_ctx = plane->dev->mode_config.acquire_ctx;
2878 retry:
2879 plane_state = drm_atomic_get_plane_state(state, plane);
2880 if (IS_ERR(plane_state)) {
2881 ret = PTR_ERR(plane_state);
2882 goto fail;
2883 }
2884
2885 ret = drm_atomic_plane_set_property(plane, plane_state,
2886 property, val);
2887 if (ret)
2888 goto fail;
2889
2890 ret = drm_atomic_commit(state);
2891 fail:
2892 if (ret == -EDEADLK)
2893 goto backoff;
2894
2895 drm_atomic_state_put(state);
2896 return ret;
2897
2898 backoff:
2899 drm_atomic_state_clear(state);
2900 drm_atomic_legacy_backoff(state);
2901
2902 goto retry;
2903 }
2904 EXPORT_SYMBOL(drm_atomic_helper_plane_set_property);
2905
2906 /**
2907 * drm_atomic_helper_connector_set_property - helper for connector properties
2908 * @connector: DRM connector
2909 * @property: DRM property
2910 * @val: value of property
2911 *
2912 * Provides a default connector set_property handler using the atomic driver
2913 * interface.
2914 *
2915 * RETURNS:
2916 * Zero on success, error code on failure
2917 */
2918 int
2919 drm_atomic_helper_connector_set_property(struct drm_connector *connector,
2920 struct drm_property *property,
2921 uint64_t val)
2922 {
2923 struct drm_atomic_state *state;
2924 struct drm_connector_state *connector_state;
2925 int ret = 0;
2926
2927 state = drm_atomic_state_alloc(connector->dev);
2928 if (!state)
2929 return -ENOMEM;
2930
2931 /* ->set_property is always called with all locks held. */
2932 state->acquire_ctx = connector->dev->mode_config.acquire_ctx;
2933 retry:
2934 connector_state = drm_atomic_get_connector_state(state, connector);
2935 if (IS_ERR(connector_state)) {
2936 ret = PTR_ERR(connector_state);
2937 goto fail;
2938 }
2939
2940 ret = drm_atomic_connector_set_property(connector, connector_state,
2941 property, val);
2942 if (ret)
2943 goto fail;
2944
2945 ret = drm_atomic_commit(state);
2946 fail:
2947 if (ret == -EDEADLK)
2948 goto backoff;
2949
2950 drm_atomic_state_put(state);
2951 return ret;
2952
2953 backoff:
2954 drm_atomic_state_clear(state);
2955 drm_atomic_legacy_backoff(state);
2956
2957 goto retry;
2958 }
2959 EXPORT_SYMBOL(drm_atomic_helper_connector_set_property);
2960
2961 static int page_flip_common(
2962 struct drm_atomic_state *state,
2963 struct drm_crtc *crtc,
2964 struct drm_framebuffer *fb,
2965 struct drm_pending_vblank_event *event,
2966 uint32_t flags)
2967 {
2968 struct drm_plane *plane = crtc->primary;
2969 struct drm_plane_state *plane_state;
2970 struct drm_crtc_state *crtc_state;
2971 int ret = 0;
2972
2973 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2974 if (IS_ERR(crtc_state))
2975 return PTR_ERR(crtc_state);
2976
2977 crtc_state->event = event;
2978 crtc_state->pageflip_flags = flags;
2979
2980 plane_state = drm_atomic_get_plane_state(state, plane);
2981 if (IS_ERR(plane_state))
2982 return PTR_ERR(plane_state);
2983
2984 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
2985 if (ret != 0)
2986 return ret;
2987 drm_atomic_set_fb_for_plane(plane_state, fb);
2988
2989 /* Make sure we don't accidentally do a full modeset. */
2990 state->allow_modeset = false;
2991 if (!crtc_state->active) {
2992 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] disabled, rejecting legacy flip\n",
2993 crtc->base.id, crtc->name);
2994 return -EINVAL;
2995 }
2996
2997 return ret;
2998 }
2999
3000 /**
3001 * drm_atomic_helper_page_flip - execute a legacy page flip
3002 * @crtc: DRM crtc
3003 * @fb: DRM framebuffer
3004 * @event: optional DRM event to signal upon completion
3005 * @flags: flip flags for non-vblank sync'ed updates
3006 * @ctx: lock acquisition context
3007 *
3008 * Provides a default &drm_crtc_funcs.page_flip implementation
3009 * using the atomic driver interface.
3010 *
3011 * Returns:
3012 * Returns 0 on success, negative errno numbers on failure.
3013 *
3014 * See also:
3015 * drm_atomic_helper_page_flip_target()
3016 */
3017 int drm_atomic_helper_page_flip(struct drm_crtc *crtc,
3018 struct drm_framebuffer *fb,
3019 struct drm_pending_vblank_event *event,
3020 uint32_t flags,
3021 struct drm_modeset_acquire_ctx *ctx)
3022 {
3023 struct drm_plane *plane = crtc->primary;
3024 struct drm_atomic_state *state;
3025 int ret = 0;
3026
3027 state = drm_atomic_state_alloc(plane->dev);
3028 if (!state)
3029 return -ENOMEM;
3030
3031 state->acquire_ctx = ctx;
3032
3033 ret = page_flip_common(state, crtc, fb, event, flags);
3034 if (ret != 0)
3035 goto fail;
3036
3037 ret = drm_atomic_nonblocking_commit(state);
3038 fail:
3039 drm_atomic_state_put(state);
3040 return ret;
3041 }
3042 EXPORT_SYMBOL(drm_atomic_helper_page_flip);
3043
3044 /**
3045 * drm_atomic_helper_page_flip_target - do page flip on target vblank period.
3046 * @crtc: DRM crtc
3047 * @fb: DRM framebuffer
3048 * @event: optional DRM event to signal upon completion
3049 * @flags: flip flags for non-vblank sync'ed updates
3050 * @target: specifying the target vblank period when the flip to take effect
3051 * @ctx: lock acquisition context
3052 *
3053 * Provides a default &drm_crtc_funcs.page_flip_target implementation.
3054 * Similar to drm_atomic_helper_page_flip() with extra parameter to specify
3055 * target vblank period to flip.
3056 *
3057 * Returns:
3058 * Returns 0 on success, negative errno numbers on failure.
3059 */
3060 int drm_atomic_helper_page_flip_target(
3061 struct drm_crtc *crtc,
3062 struct drm_framebuffer *fb,
3063 struct drm_pending_vblank_event *event,
3064 uint32_t flags,
3065 uint32_t target,
3066 struct drm_modeset_acquire_ctx *ctx)
3067 {
3068 struct drm_plane *plane = crtc->primary;
3069 struct drm_atomic_state *state;
3070 struct drm_crtc_state *crtc_state;
3071 int ret = 0;
3072
3073 state = drm_atomic_state_alloc(plane->dev);
3074 if (!state)
3075 return -ENOMEM;
3076
3077 state->acquire_ctx = ctx;
3078
3079 ret = page_flip_common(state, crtc, fb, event, flags);
3080 if (ret != 0)
3081 goto fail;
3082
3083 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
3084 if (WARN_ON(!crtc_state)) {
3085 ret = -EINVAL;
3086 goto fail;
3087 }
3088 crtc_state->target_vblank = target;
3089
3090 ret = drm_atomic_nonblocking_commit(state);
3091 fail:
3092 drm_atomic_state_put(state);
3093 return ret;
3094 }
3095 EXPORT_SYMBOL(drm_atomic_helper_page_flip_target);
3096
3097 /**
3098 * drm_atomic_helper_connector_dpms() - connector dpms helper implementation
3099 * @connector: affected connector
3100 * @mode: DPMS mode
3101 *
3102 * This is the main helper function provided by the atomic helper framework for
3103 * implementing the legacy DPMS connector interface. It computes the new desired
3104 * &drm_crtc_state.active state for the corresponding CRTC (if the connector is
3105 * enabled) and updates it.
3106 *
3107 * Returns:
3108 * Returns 0 on success, negative errno numbers on failure.
3109 */
3110 int drm_atomic_helper_connector_dpms(struct drm_connector *connector,
3111 int mode)
3112 {
3113 struct drm_mode_config *config = &connector->dev->mode_config;
3114 struct drm_atomic_state *state;
3115 struct drm_crtc_state *crtc_state;
3116 struct drm_crtc *crtc;
3117 struct drm_connector *tmp_connector;
3118 struct drm_connector_list_iter conn_iter;
3119 int ret;
3120 bool active = false;
3121 int old_mode = connector->dpms;
3122
3123 if (mode != DRM_MODE_DPMS_ON)
3124 mode = DRM_MODE_DPMS_OFF;
3125
3126 connector->dpms = mode;
3127 crtc = connector->state->crtc;
3128
3129 if (!crtc)
3130 return 0;
3131
3132 state = drm_atomic_state_alloc(connector->dev);
3133 if (!state)
3134 return -ENOMEM;
3135
3136 state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
3137 retry:
3138 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3139 if (IS_ERR(crtc_state)) {
3140 ret = PTR_ERR(crtc_state);
3141 goto fail;
3142 }
3143
3144 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
3145
3146 drm_connector_list_iter_begin(connector->dev, &conn_iter);
3147 drm_for_each_connector_iter(tmp_connector, &conn_iter) {
3148 if (tmp_connector->state->crtc != crtc)
3149 continue;
3150
3151 if (tmp_connector->dpms == DRM_MODE_DPMS_ON) {
3152 active = true;
3153 break;
3154 }
3155 }
3156 drm_connector_list_iter_end(&conn_iter);
3157 crtc_state->active = active;
3158
3159 ret = drm_atomic_commit(state);
3160 fail:
3161 if (ret == -EDEADLK)
3162 goto backoff;
3163 if (ret != 0)
3164 connector->dpms = old_mode;
3165 drm_atomic_state_put(state);
3166 return ret;
3167
3168 backoff:
3169 drm_atomic_state_clear(state);
3170 drm_atomic_legacy_backoff(state);
3171
3172 goto retry;
3173 }
3174 EXPORT_SYMBOL(drm_atomic_helper_connector_dpms);
3175
3176 /**
3177 * drm_atomic_helper_best_encoder - Helper for
3178 * &drm_connector_helper_funcs.best_encoder callback
3179 * @connector: Connector control structure
3180 *
3181 * This is a &drm_connector_helper_funcs.best_encoder callback helper for
3182 * connectors that support exactly 1 encoder, statically determined at driver
3183 * init time.
3184 */
3185 struct drm_encoder *
3186 drm_atomic_helper_best_encoder(struct drm_connector *connector)
3187 {
3188 WARN_ON(connector->encoder_ids[1]);
3189 return drm_encoder_find(connector->dev, connector->encoder_ids[0]);
3190 }
3191 EXPORT_SYMBOL(drm_atomic_helper_best_encoder);
3192
3193 /**
3194 * DOC: atomic state reset and initialization
3195 *
3196 * Both the drm core and the atomic helpers assume that there is always the full
3197 * and correct atomic software state for all connectors, CRTCs and planes
3198 * available. Which is a bit a problem on driver load and also after system
3199 * suspend. One way to solve this is to have a hardware state read-out
3200 * infrastructure which reconstructs the full software state (e.g. the i915
3201 * driver).
3202 *
3203 * The simpler solution is to just reset the software state to everything off,
3204 * which is easiest to do by calling drm_mode_config_reset(). To facilitate this
3205 * the atomic helpers provide default reset implementations for all hooks.
3206 *
3207 * On the upside the precise state tracking of atomic simplifies system suspend
3208 * and resume a lot. For drivers using drm_mode_config_reset() a complete recipe
3209 * is implemented in drm_atomic_helper_suspend() and drm_atomic_helper_resume().
3210 * For other drivers the building blocks are split out, see the documentation
3211 * for these functions.
3212 */
3213
3214 /**
3215 * drm_atomic_helper_crtc_reset - default &drm_crtc_funcs.reset hook for CRTCs
3216 * @crtc: drm CRTC
3217 *
3218 * Resets the atomic state for @crtc by freeing the state pointer (which might
3219 * be NULL, e.g. at driver load time) and allocating a new empty state object.
3220 */
3221 void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc)
3222 {
3223 if (crtc->state)
3224 __drm_atomic_helper_crtc_destroy_state(crtc->state);
3225
3226 kfree(crtc->state);
3227 crtc->state = kzalloc(sizeof(*crtc->state), GFP_KERNEL);
3228
3229 if (crtc->state)
3230 crtc->state->crtc = crtc;
3231 }
3232 EXPORT_SYMBOL(drm_atomic_helper_crtc_reset);
3233
3234 /**
3235 * __drm_atomic_helper_crtc_duplicate_state - copy atomic CRTC state
3236 * @crtc: CRTC object
3237 * @state: atomic CRTC state
3238 *
3239 * Copies atomic state from a CRTC's current state and resets inferred values.
3240 * This is useful for drivers that subclass the CRTC state.
3241 */
3242 void __drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc,
3243 struct drm_crtc_state *state)
3244 {
3245 memcpy(state, crtc->state, sizeof(*state));
3246
3247 if (state->mode_blob)
3248 drm_property_blob_get(state->mode_blob);
3249 if (state->degamma_lut)
3250 drm_property_blob_get(state->degamma_lut);
3251 if (state->ctm)
3252 drm_property_blob_get(state->ctm);
3253 if (state->gamma_lut)
3254 drm_property_blob_get(state->gamma_lut);
3255 state->mode_changed = false;
3256 state->active_changed = false;
3257 state->planes_changed = false;
3258 state->connectors_changed = false;
3259 state->color_mgmt_changed = false;
3260 state->zpos_changed = false;
3261 state->event = NULL;
3262 state->pageflip_flags = 0;
3263 }
3264 EXPORT_SYMBOL(__drm_atomic_helper_crtc_duplicate_state);
3265
3266 /**
3267 * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
3268 * @crtc: drm CRTC
3269 *
3270 * Default CRTC state duplicate hook for drivers which don't have their own
3271 * subclassed CRTC state structure.
3272 */
3273 struct drm_crtc_state *
3274 drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc)
3275 {
3276 struct drm_crtc_state *state;
3277
3278 if (WARN_ON(!crtc->state))
3279 return NULL;
3280
3281 state = kmalloc(sizeof(*state), GFP_KERNEL);
3282 if (state)
3283 __drm_atomic_helper_crtc_duplicate_state(crtc, state);
3284
3285 return state;
3286 }
3287 EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state);
3288
3289 /**
3290 * __drm_atomic_helper_crtc_destroy_state - release CRTC state
3291 * @state: CRTC state object to release
3292 *
3293 * Releases all resources stored in the CRTC state without actually freeing
3294 * the memory of the CRTC state. This is useful for drivers that subclass the
3295 * CRTC state.
3296 */
3297 void __drm_atomic_helper_crtc_destroy_state(struct drm_crtc_state *state)
3298 {
3299 drm_property_blob_put(state->mode_blob);
3300 drm_property_blob_put(state->degamma_lut);
3301 drm_property_blob_put(state->ctm);
3302 drm_property_blob_put(state->gamma_lut);
3303 }
3304 EXPORT_SYMBOL(__drm_atomic_helper_crtc_destroy_state);
3305
3306 /**
3307 * drm_atomic_helper_crtc_destroy_state - default state destroy hook
3308 * @crtc: drm CRTC
3309 * @state: CRTC state object to release
3310 *
3311 * Default CRTC state destroy hook for drivers which don't have their own
3312 * subclassed CRTC state structure.
3313 */
3314 void drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
3315 struct drm_crtc_state *state)
3316 {
3317 __drm_atomic_helper_crtc_destroy_state(state);
3318 kfree(state);
3319 }
3320 EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state);
3321
3322 /**
3323 * drm_atomic_helper_plane_reset - default &drm_plane_funcs.reset hook for planes
3324 * @plane: drm plane
3325 *
3326 * Resets the atomic state for @plane by freeing the state pointer (which might
3327 * be NULL, e.g. at driver load time) and allocating a new empty state object.
3328 */
3329 void drm_atomic_helper_plane_reset(struct drm_plane *plane)
3330 {
3331 if (plane->state)
3332 __drm_atomic_helper_plane_destroy_state(plane->state);
3333
3334 kfree(plane->state);
3335 plane->state = kzalloc(sizeof(*plane->state), GFP_KERNEL);
3336
3337 if (plane->state) {
3338 plane->state->plane = plane;
3339 plane->state->rotation = DRM_MODE_ROTATE_0;
3340 }
3341 }
3342 EXPORT_SYMBOL(drm_atomic_helper_plane_reset);
3343
3344 /**
3345 * __drm_atomic_helper_plane_duplicate_state - copy atomic plane state
3346 * @plane: plane object
3347 * @state: atomic plane state
3348 *
3349 * Copies atomic state from a plane's current state. This is useful for
3350 * drivers that subclass the plane state.
3351 */
3352 void __drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane,
3353 struct drm_plane_state *state)
3354 {
3355 memcpy(state, plane->state, sizeof(*state));
3356
3357 if (state->fb)
3358 drm_framebuffer_get(state->fb);
3359
3360 state->fence = NULL;
3361 }
3362 EXPORT_SYMBOL(__drm_atomic_helper_plane_duplicate_state);
3363
3364 /**
3365 * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
3366 * @plane: drm plane
3367 *
3368 * Default plane state duplicate hook for drivers which don't have their own
3369 * subclassed plane state structure.
3370 */
3371 struct drm_plane_state *
3372 drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane)
3373 {
3374 struct drm_plane_state *state;
3375
3376 if (WARN_ON(!plane->state))
3377 return NULL;
3378
3379 state = kmalloc(sizeof(*state), GFP_KERNEL);
3380 if (state)
3381 __drm_atomic_helper_plane_duplicate_state(plane, state);
3382
3383 return state;
3384 }
3385 EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state);
3386
3387 /**
3388 * __drm_atomic_helper_plane_destroy_state - release plane state
3389 * @state: plane state object to release
3390 *
3391 * Releases all resources stored in the plane state without actually freeing
3392 * the memory of the plane state. This is useful for drivers that subclass the
3393 * plane state.
3394 */
3395 void __drm_atomic_helper_plane_destroy_state(struct drm_plane_state *state)
3396 {
3397 if (state->fb)
3398 drm_framebuffer_put(state->fb);
3399
3400 if (state->fence)
3401 dma_fence_put(state->fence);
3402 }
3403 EXPORT_SYMBOL(__drm_atomic_helper_plane_destroy_state);
3404
3405 /**
3406 * drm_atomic_helper_plane_destroy_state - default state destroy hook
3407 * @plane: drm plane
3408 * @state: plane state object to release
3409 *
3410 * Default plane state destroy hook for drivers which don't have their own
3411 * subclassed plane state structure.
3412 */
3413 void drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
3414 struct drm_plane_state *state)
3415 {
3416 __drm_atomic_helper_plane_destroy_state(state);
3417 kfree(state);
3418 }
3419 EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state);
3420
3421 /**
3422 * __drm_atomic_helper_connector_reset - reset state on connector
3423 * @connector: drm connector
3424 * @conn_state: connector state to assign
3425 *
3426 * Initializes the newly allocated @conn_state and assigns it to
3427 * the &drm_conector->state pointer of @connector, usually required when
3428 * initializing the drivers or when called from the &drm_connector_funcs.reset
3429 * hook.
3430 *
3431 * This is useful for drivers that subclass the connector state.
3432 */
3433 void
3434 __drm_atomic_helper_connector_reset(struct drm_connector *connector,
3435 struct drm_connector_state *conn_state)
3436 {
3437 if (conn_state)
3438 conn_state->connector = connector;
3439
3440 connector->state = conn_state;
3441 }
3442 EXPORT_SYMBOL(__drm_atomic_helper_connector_reset);
3443
3444 /**
3445 * drm_atomic_helper_connector_reset - default &drm_connector_funcs.reset hook for connectors
3446 * @connector: drm connector
3447 *
3448 * Resets the atomic state for @connector by freeing the state pointer (which
3449 * might be NULL, e.g. at driver load time) and allocating a new empty state
3450 * object.
3451 */
3452 void drm_atomic_helper_connector_reset(struct drm_connector *connector)
3453 {
3454 struct drm_connector_state *conn_state =
3455 kzalloc(sizeof(*conn_state), GFP_KERNEL);
3456
3457 if (connector->state)
3458 __drm_atomic_helper_connector_destroy_state(connector->state);
3459
3460 kfree(connector->state);
3461 __drm_atomic_helper_connector_reset(connector, conn_state);
3462 }
3463 EXPORT_SYMBOL(drm_atomic_helper_connector_reset);
3464
3465 /**
3466 * __drm_atomic_helper_connector_duplicate_state - copy atomic connector state
3467 * @connector: connector object
3468 * @state: atomic connector state
3469 *
3470 * Copies atomic state from a connector's current state. This is useful for
3471 * drivers that subclass the connector state.
3472 */
3473 void
3474 __drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector,
3475 struct drm_connector_state *state)
3476 {
3477 memcpy(state, connector->state, sizeof(*state));
3478 if (state->crtc)
3479 drm_connector_get(connector);
3480 }
3481 EXPORT_SYMBOL(__drm_atomic_helper_connector_duplicate_state);
3482
3483 /**
3484 * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
3485 * @connector: drm connector
3486 *
3487 * Default connector state duplicate hook for drivers which don't have their own
3488 * subclassed connector state structure.
3489 */
3490 struct drm_connector_state *
3491 drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
3492 {
3493 struct drm_connector_state *state;
3494
3495 if (WARN_ON(!connector->state))
3496 return NULL;
3497
3498 state = kmalloc(sizeof(*state), GFP_KERNEL);
3499 if (state)
3500 __drm_atomic_helper_connector_duplicate_state(connector, state);
3501
3502 return state;
3503 }
3504 EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
3505
3506 /**
3507 * drm_atomic_helper_duplicate_state - duplicate an atomic state object
3508 * @dev: DRM device
3509 * @ctx: lock acquisition context
3510 *
3511 * Makes a copy of the current atomic state by looping over all objects and
3512 * duplicating their respective states. This is used for example by suspend/
3513 * resume support code to save the state prior to suspend such that it can
3514 * be restored upon resume.
3515 *
3516 * Note that this treats atomic state as persistent between save and restore.
3517 * Drivers must make sure that this is possible and won't result in confusion
3518 * or erroneous behaviour.
3519 *
3520 * Note that if callers haven't already acquired all modeset locks this might
3521 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
3522 *
3523 * Returns:
3524 * A pointer to the copy of the atomic state object on success or an
3525 * ERR_PTR()-encoded error code on failure.
3526 *
3527 * See also:
3528 * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
3529 */
3530 struct drm_atomic_state *
3531 drm_atomic_helper_duplicate_state(struct drm_device *dev,
3532 struct drm_modeset_acquire_ctx *ctx)
3533 {
3534 struct drm_atomic_state *state;
3535 struct drm_connector *conn;
3536 struct drm_connector_list_iter conn_iter;
3537 struct drm_plane *plane;
3538 struct drm_crtc *crtc;
3539 int err = 0;
3540
3541 state = drm_atomic_state_alloc(dev);
3542 if (!state)
3543 return ERR_PTR(-ENOMEM);
3544
3545 state->acquire_ctx = ctx;
3546
3547 drm_for_each_crtc(crtc, dev) {
3548 struct drm_crtc_state *crtc_state;
3549
3550 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3551 if (IS_ERR(crtc_state)) {
3552 err = PTR_ERR(crtc_state);
3553 goto free;
3554 }
3555 }
3556
3557 drm_for_each_plane(plane, dev) {
3558 struct drm_plane_state *plane_state;
3559
3560 plane_state = drm_atomic_get_plane_state(state, plane);
3561 if (IS_ERR(plane_state)) {
3562 err = PTR_ERR(plane_state);
3563 goto free;
3564 }
3565 }
3566
3567 drm_connector_list_iter_begin(dev, &conn_iter);
3568 drm_for_each_connector_iter(conn, &conn_iter) {
3569 struct drm_connector_state *conn_state;
3570
3571 conn_state = drm_atomic_get_connector_state(state, conn);
3572 if (IS_ERR(conn_state)) {
3573 err = PTR_ERR(conn_state);
3574 drm_connector_list_iter_end(&conn_iter);
3575 goto free;
3576 }
3577 }
3578 drm_connector_list_iter_end(&conn_iter);
3579
3580 /* clear the acquire context so that it isn't accidentally reused */
3581 state->acquire_ctx = NULL;
3582
3583 free:
3584 if (err < 0) {
3585 drm_atomic_state_put(state);
3586 state = ERR_PTR(err);
3587 }
3588
3589 return state;
3590 }
3591 EXPORT_SYMBOL(drm_atomic_helper_duplicate_state);
3592
3593 /**
3594 * __drm_atomic_helper_connector_destroy_state - release connector state
3595 * @state: connector state object to release
3596 *
3597 * Releases all resources stored in the connector state without actually
3598 * freeing the memory of the connector state. This is useful for drivers that
3599 * subclass the connector state.
3600 */
3601 void
3602 __drm_atomic_helper_connector_destroy_state(struct drm_connector_state *state)
3603 {
3604 if (state->crtc)
3605 drm_connector_put(state->connector);
3606 }
3607 EXPORT_SYMBOL(__drm_atomic_helper_connector_destroy_state);
3608
3609 /**
3610 * drm_atomic_helper_connector_destroy_state - default state destroy hook
3611 * @connector: drm connector
3612 * @state: connector state object to release
3613 *
3614 * Default connector state destroy hook for drivers which don't have their own
3615 * subclassed connector state structure.
3616 */
3617 void drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
3618 struct drm_connector_state *state)
3619 {
3620 __drm_atomic_helper_connector_destroy_state(state);
3621 kfree(state);
3622 }
3623 EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state);
3624
3625 /**
3626 * drm_atomic_helper_legacy_gamma_set - set the legacy gamma correction table
3627 * @crtc: CRTC object
3628 * @red: red correction table
3629 * @green: green correction table
3630 * @blue: green correction table
3631 * @size: size of the tables
3632 * @ctx: lock acquire context
3633 *
3634 * Implements support for legacy gamma correction table for drivers
3635 * that support color management through the DEGAMMA_LUT/GAMMA_LUT
3636 * properties. See drm_crtc_enable_color_mgmt() and the containing chapter for
3637 * how the atomic color management and gamma tables work.
3638 */
3639 int drm_atomic_helper_legacy_gamma_set(struct drm_crtc *crtc,
3640 u16 *red, u16 *green, u16 *blue,
3641 uint32_t size,
3642 struct drm_modeset_acquire_ctx *ctx)
3643 {
3644 struct drm_device *dev = crtc->dev;
3645 struct drm_mode_config *config = &dev->mode_config;
3646 struct drm_atomic_state *state;
3647 struct drm_crtc_state *crtc_state;
3648 struct drm_property_blob *blob = NULL;
3649 struct drm_color_lut *blob_data;
3650 int i, ret = 0;
3651
3652 state = drm_atomic_state_alloc(crtc->dev);
3653 if (!state)
3654 return -ENOMEM;
3655
3656 blob = drm_property_create_blob(dev,
3657 sizeof(struct drm_color_lut) * size,
3658 NULL);
3659 if (IS_ERR(blob)) {
3660 ret = PTR_ERR(blob);
3661 blob = NULL;
3662 goto fail;
3663 }
3664
3665 /* Prepare GAMMA_LUT with the legacy values. */
3666 blob_data = (struct drm_color_lut *) blob->data;
3667 for (i = 0; i < size; i++) {
3668 blob_data[i].red = red[i];
3669 blob_data[i].green = green[i];
3670 blob_data[i].blue = blue[i];
3671 }
3672
3673 state->acquire_ctx = ctx;
3674 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3675 if (IS_ERR(crtc_state)) {
3676 ret = PTR_ERR(crtc_state);
3677 goto fail;
3678 }
3679
3680 /* Reset DEGAMMA_LUT and CTM properties. */
3681 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
3682 config->degamma_lut_property, 0);
3683 if (ret)
3684 goto fail;
3685
3686 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
3687 config->ctm_property, 0);
3688 if (ret)
3689 goto fail;
3690
3691 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
3692 config->gamma_lut_property, blob->base.id);
3693 if (ret)
3694 goto fail;
3695
3696 ret = drm_atomic_commit(state);
3697
3698 fail:
3699 drm_atomic_state_put(state);
3700 drm_property_blob_put(blob);
3701 return ret;
3702 }
3703 EXPORT_SYMBOL(drm_atomic_helper_legacy_gamma_set);