projects/linux·blob master · refs

4190 lines · 128.1 KiB

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