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