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