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