drm_atomic_helper.c revision 1.4 1 /* $NetBSD: drm_atomic_helper.c,v 1.4 2018/08/27 06:50:45 riastradh Exp $ */
2
3 /*
4 * Copyright (C) 2014 Red Hat
5 * Copyright (C) 2014 Intel Corp.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 *
25 * Authors:
26 * Rob Clark <robdclark (at) gmail.com>
27 * Daniel Vetter <daniel.vetter (at) ffwll.ch>
28 */
29
30 #include <sys/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: drm_atomic_helper.c,v 1.4 2018/08/27 06:50:45 riastradh Exp $");
32
33 #include <drm/drmP.h>
34 #include <drm/drm_atomic.h>
35 #include <drm/drm_plane_helper.h>
36 #include <drm/drm_crtc_helper.h>
37 #include <drm/drm_atomic_helper.h>
38 #include <linux/export.h>
39 #include <linux/fence.h>
40
41 /**
42 * DOC: overview
43 *
44 * This helper library provides implementations of check and commit functions on
45 * top of the CRTC modeset helper callbacks and the plane helper callbacks. It
46 * also provides convenience implementations for the atomic state handling
47 * callbacks for drivers which don't need to subclass the drm core structures to
48 * add their own additional internal state.
49 *
50 * This library also provides default implementations for the check callback in
51 * drm_atomic_helper_check() and for the commit callback with
52 * drm_atomic_helper_commit(). But the individual stages and callbacks are
53 * exposed to allow drivers to mix and match and e.g. use the plane helpers only
54 * together with a driver private modeset implementation.
55 *
56 * This library also provides implementations for all the legacy driver
57 * interfaces on top of the atomic interface. See drm_atomic_helper_set_config(),
58 * drm_atomic_helper_disable_plane(), drm_atomic_helper_disable_plane() and the
59 * various functions to implement set_property callbacks. New drivers must not
60 * implement these functions themselves but must use the provided helpers.
61 */
62 static void
63 drm_atomic_helper_plane_changed(struct drm_atomic_state *state,
64 struct drm_plane_state *plane_state,
65 struct drm_plane *plane)
66 {
67 struct drm_crtc_state *crtc_state;
68
69 if (plane->state->crtc) {
70 crtc_state = state->crtc_states[drm_crtc_index(plane->state->crtc)];
71
72 if (WARN_ON(!crtc_state))
73 return;
74
75 crtc_state->planes_changed = true;
76 }
77
78 if (plane_state->crtc) {
79 crtc_state =
80 state->crtc_states[drm_crtc_index(plane_state->crtc)];
81
82 if (WARN_ON(!crtc_state))
83 return;
84
85 crtc_state->planes_changed = true;
86 }
87 }
88
89 static struct drm_crtc *
90 get_current_crtc_for_encoder(struct drm_device *dev,
91 struct drm_encoder *encoder)
92 {
93 struct drm_mode_config *config = &dev->mode_config;
94 struct drm_connector *connector;
95
96 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
97
98 drm_for_each_connector(connector, dev) {
99 if (connector->state->best_encoder != encoder)
100 continue;
101
102 return connector->state->crtc;
103 }
104
105 return NULL;
106 }
107
108 static int
109 steal_encoder(struct drm_atomic_state *state,
110 struct drm_encoder *encoder,
111 struct drm_crtc *encoder_crtc)
112 {
113 struct drm_mode_config *config = &state->dev->mode_config;
114 struct drm_crtc_state *crtc_state;
115 struct drm_connector *connector;
116 struct drm_connector_state *connector_state;
117
118 /*
119 * We can only steal an encoder coming from a connector, which means we
120 * must already hold the connection_mutex.
121 */
122 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
123
124 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d], stealing it\n",
125 encoder->base.id, encoder->name,
126 encoder_crtc->base.id);
127
128 crtc_state = drm_atomic_get_crtc_state(state, encoder_crtc);
129 if (IS_ERR(crtc_state))
130 return PTR_ERR(crtc_state);
131
132 crtc_state->connectors_changed = true;
133
134 list_for_each_entry(connector, &config->connector_list, head) {
135 if (connector->state->best_encoder != encoder)
136 continue;
137
138 DRM_DEBUG_ATOMIC("Stealing encoder from [CONNECTOR:%d:%s]\n",
139 connector->base.id,
140 connector->name);
141
142 connector_state = drm_atomic_get_connector_state(state,
143 connector);
144 if (IS_ERR(connector_state))
145 return PTR_ERR(connector_state);
146
147 connector_state->best_encoder = NULL;
148 }
149
150 return 0;
151 }
152
153 static int
154 update_connector_routing(struct drm_atomic_state *state, int conn_idx)
155 {
156 const struct drm_connector_helper_funcs *funcs;
157 struct drm_encoder *new_encoder;
158 struct drm_crtc *encoder_crtc;
159 struct drm_connector *connector;
160 struct drm_connector_state *connector_state;
161 struct drm_crtc_state *crtc_state;
162 int idx, ret;
163
164 connector = state->connectors[conn_idx];
165 connector_state = state->connector_states[conn_idx];
166
167 if (!connector)
168 return 0;
169
170 DRM_DEBUG_ATOMIC("Updating routing for [CONNECTOR:%d:%s]\n",
171 connector->base.id,
172 connector->name);
173
174 if (connector->state->crtc != connector_state->crtc) {
175 if (connector->state->crtc) {
176 idx = drm_crtc_index(connector->state->crtc);
177
178 crtc_state = state->crtc_states[idx];
179 crtc_state->connectors_changed = true;
180 }
181
182 if (connector_state->crtc) {
183 idx = drm_crtc_index(connector_state->crtc);
184
185 crtc_state = state->crtc_states[idx];
186 crtc_state->connectors_changed = true;
187 }
188 }
189
190 if (!connector_state->crtc) {
191 DRM_DEBUG_ATOMIC("Disabling [CONNECTOR:%d:%s]\n",
192 connector->base.id,
193 connector->name);
194
195 connector_state->best_encoder = NULL;
196
197 return 0;
198 }
199
200 funcs = connector->helper_private;
201
202 if (funcs->atomic_best_encoder)
203 new_encoder = funcs->atomic_best_encoder(connector,
204 connector_state);
205 else
206 new_encoder = funcs->best_encoder(connector);
207
208 if (!new_encoder) {
209 DRM_DEBUG_ATOMIC("No suitable encoder found for [CONNECTOR:%d:%s]\n",
210 connector->base.id,
211 connector->name);
212 return -EINVAL;
213 }
214
215 if (!drm_encoder_crtc_ok(new_encoder, connector_state->crtc)) {
216 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] incompatible with [CRTC:%d]\n",
217 new_encoder->base.id,
218 new_encoder->name,
219 connector_state->crtc->base.id);
220 return -EINVAL;
221 }
222
223 if (new_encoder == connector_state->best_encoder) {
224 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] keeps [ENCODER:%d:%s], now on [CRTC:%d]\n",
225 connector->base.id,
226 connector->name,
227 new_encoder->base.id,
228 new_encoder->name,
229 connector_state->crtc->base.id);
230
231 return 0;
232 }
233
234 encoder_crtc = get_current_crtc_for_encoder(state->dev,
235 new_encoder);
236
237 if (encoder_crtc) {
238 ret = steal_encoder(state, new_encoder, encoder_crtc);
239 if (ret) {
240 DRM_DEBUG_ATOMIC("Encoder stealing failed for [CONNECTOR:%d:%s]\n",
241 connector->base.id,
242 connector->name);
243 return ret;
244 }
245 }
246
247 if (WARN_ON(!connector_state->crtc))
248 return -EINVAL;
249
250 connector_state->best_encoder = new_encoder;
251 idx = drm_crtc_index(connector_state->crtc);
252
253 crtc_state = state->crtc_states[idx];
254 crtc_state->connectors_changed = true;
255
256 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] using [ENCODER:%d:%s] on [CRTC:%d]\n",
257 connector->base.id,
258 connector->name,
259 new_encoder->base.id,
260 new_encoder->name,
261 connector_state->crtc->base.id);
262
263 return 0;
264 }
265
266 static int
267 mode_fixup(struct drm_atomic_state *state)
268 {
269 struct drm_crtc *crtc;
270 struct drm_crtc_state *crtc_state;
271 struct drm_connector *connector;
272 struct drm_connector_state *conn_state;
273 int i;
274 int ret;
275
276 for_each_crtc_in_state(state, crtc, crtc_state, i) {
277 if (!crtc_state->mode_changed &&
278 !crtc_state->connectors_changed)
279 continue;
280
281 drm_mode_copy(&crtc_state->adjusted_mode, &crtc_state->mode);
282 }
283
284 for_each_connector_in_state(state, connector, conn_state, i) {
285 const struct drm_encoder_helper_funcs *funcs;
286 struct drm_encoder *encoder;
287
288 WARN_ON(!!conn_state->best_encoder != !!conn_state->crtc);
289
290 if (!conn_state->crtc || !conn_state->best_encoder)
291 continue;
292
293 crtc_state =
294 state->crtc_states[drm_crtc_index(conn_state->crtc)];
295
296 /*
297 * Each encoder has at most one connector (since we always steal
298 * it away), so we won't call ->mode_fixup twice.
299 */
300 encoder = conn_state->best_encoder;
301 funcs = encoder->helper_private;
302 if (!funcs)
303 continue;
304
305 ret = drm_bridge_mode_fixup(encoder->bridge, &crtc_state->mode,
306 &crtc_state->adjusted_mode);
307 if (!ret) {
308 DRM_DEBUG_ATOMIC("Bridge fixup failed\n");
309 return -EINVAL;
310 }
311
312 if (funcs->atomic_check) {
313 ret = funcs->atomic_check(encoder, crtc_state,
314 conn_state);
315 if (ret) {
316 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] check failed\n",
317 encoder->base.id, encoder->name);
318 return ret;
319 }
320 } else if (funcs->mode_fixup) {
321 ret = funcs->mode_fixup(encoder, &crtc_state->mode,
322 &crtc_state->adjusted_mode);
323 if (!ret) {
324 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] fixup failed\n",
325 encoder->base.id, encoder->name);
326 return -EINVAL;
327 }
328 }
329 }
330
331 for_each_crtc_in_state(state, crtc, crtc_state, i) {
332 const struct drm_crtc_helper_funcs *funcs;
333
334 if (!crtc_state->mode_changed &&
335 !crtc_state->connectors_changed)
336 continue;
337
338 funcs = crtc->helper_private;
339 if (!funcs->mode_fixup)
340 continue;
341
342 ret = funcs->mode_fixup(crtc, &crtc_state->mode,
343 &crtc_state->adjusted_mode);
344 if (!ret) {
345 DRM_DEBUG_ATOMIC("[CRTC:%d] fixup failed\n",
346 crtc->base.id);
347 return -EINVAL;
348 }
349 }
350
351 return 0;
352 }
353
354 /**
355 * drm_atomic_helper_check_modeset - validate state object for modeset changes
356 * @dev: DRM device
357 * @state: the driver state object
358 *
359 * Check the state object to see if the requested state is physically possible.
360 * This does all the crtc and connector related computations for an atomic
361 * update and adds any additional connectors needed for full modesets and calls
362 * down into ->mode_fixup functions of the driver backend.
363 *
364 * crtc_state->mode_changed is set when the input mode is changed.
365 * crtc_state->connectors_changed is set when a connector is added or
366 * removed from the crtc.
367 * crtc_state->active_changed is set when crtc_state->active changes,
368 * which is used for dpms.
369 *
370 * IMPORTANT:
371 *
372 * Drivers which update ->mode_changed (e.g. in their ->atomic_check hooks if a
373 * plane update can't be done without a full modeset) _must_ call this function
374 * afterwards after that change. It is permitted to call this function multiple
375 * times for the same update, e.g. when the ->atomic_check functions depend upon
376 * the adjusted dotclock for fifo space allocation and watermark computation.
377 *
378 * RETURNS
379 * Zero for success or -errno
380 */
381 int
382 drm_atomic_helper_check_modeset(struct drm_device *dev,
383 struct drm_atomic_state *state)
384 {
385 struct drm_crtc *crtc;
386 struct drm_crtc_state *crtc_state;
387 struct drm_connector *connector;
388 struct drm_connector_state *connector_state __unused;
389 int i, ret;
390
391 for_each_crtc_in_state(state, crtc, crtc_state, i) {
392 if (!drm_mode_equal(&crtc->state->mode, &crtc_state->mode)) {
393 DRM_DEBUG_ATOMIC("[CRTC:%d] mode changed\n",
394 crtc->base.id);
395 crtc_state->mode_changed = true;
396 }
397
398 if (crtc->state->enable != crtc_state->enable) {
399 DRM_DEBUG_ATOMIC("[CRTC:%d] enable changed\n",
400 crtc->base.id);
401
402 /*
403 * For clarity this assignment is done here, but
404 * enable == 0 is only true when there are no
405 * connectors and a NULL mode.
406 *
407 * The other way around is true as well. enable != 0
408 * iff connectors are attached and a mode is set.
409 */
410 crtc_state->mode_changed = true;
411 crtc_state->connectors_changed = true;
412 }
413 }
414
415 for_each_connector_in_state(state, connector, connector_state, i) {
416 /*
417 * This only sets crtc->mode_changed for routing changes,
418 * drivers must set crtc->mode_changed themselves when connector
419 * properties need to be updated.
420 */
421 ret = update_connector_routing(state, i);
422 if (ret)
423 return ret;
424 }
425
426 /*
427 * After all the routing has been prepared we need to add in any
428 * connector which is itself unchanged, but who's crtc changes it's
429 * configuration. This must be done before calling mode_fixup in case a
430 * crtc only changed its mode but has the same set of connectors.
431 */
432 for_each_crtc_in_state(state, crtc, crtc_state, i) {
433 int num_connectors;
434
435 /*
436 * We must set ->active_changed after walking connectors for
437 * otherwise an update that only changes active would result in
438 * a full modeset because update_connector_routing force that.
439 */
440 if (crtc->state->active != crtc_state->active) {
441 DRM_DEBUG_ATOMIC("[CRTC:%d] active changed\n",
442 crtc->base.id);
443 crtc_state->active_changed = true;
444 }
445
446 if (!drm_atomic_crtc_needs_modeset(crtc_state))
447 continue;
448
449 DRM_DEBUG_ATOMIC("[CRTC:%d] needs all connectors, enable: %c, active: %c\n",
450 crtc->base.id,
451 crtc_state->enable ? 'y' : 'n',
452 crtc_state->active ? 'y' : 'n');
453
454 ret = drm_atomic_add_affected_connectors(state, crtc);
455 if (ret != 0)
456 return ret;
457
458 ret = drm_atomic_add_affected_planes(state, crtc);
459 if (ret != 0)
460 return ret;
461
462 num_connectors = drm_atomic_connectors_for_crtc(state,
463 crtc);
464
465 if (crtc_state->enable != !!num_connectors) {
466 DRM_DEBUG_ATOMIC("[CRTC:%d] enabled/connectors mismatch\n",
467 crtc->base.id);
468
469 return -EINVAL;
470 }
471 }
472
473 return mode_fixup(state);
474 }
475 EXPORT_SYMBOL(drm_atomic_helper_check_modeset);
476
477 /**
478 * drm_atomic_helper_check_planes - validate state object for planes changes
479 * @dev: DRM device
480 * @state: the driver state object
481 *
482 * Check the state object to see if the requested state is physically possible.
483 * This does all the plane update related checks using by calling into the
484 * ->atomic_check hooks provided by the driver.
485 *
486 * It also sets crtc_state->planes_changed to indicate that a crtc has
487 * updated planes.
488 *
489 * RETURNS
490 * Zero for success or -errno
491 */
492 int
493 drm_atomic_helper_check_planes(struct drm_device *dev,
494 struct drm_atomic_state *state)
495 {
496 struct drm_crtc *crtc;
497 struct drm_crtc_state *crtc_state;
498 struct drm_plane *plane;
499 struct drm_plane_state *plane_state;
500 int i, ret = 0;
501
502 for_each_plane_in_state(state, plane, plane_state, i) {
503 const struct drm_plane_helper_funcs *funcs;
504
505 funcs = plane->helper_private;
506
507 drm_atomic_helper_plane_changed(state, plane_state, plane);
508
509 if (!funcs || !funcs->atomic_check)
510 continue;
511
512 ret = funcs->atomic_check(plane, plane_state);
513 if (ret) {
514 DRM_DEBUG_ATOMIC("[PLANE:%d] atomic driver check failed\n",
515 plane->base.id);
516 return ret;
517 }
518 }
519
520 for_each_crtc_in_state(state, crtc, crtc_state, i) {
521 const struct drm_crtc_helper_funcs *funcs;
522
523 funcs = crtc->helper_private;
524
525 if (!funcs || !funcs->atomic_check)
526 continue;
527
528 ret = funcs->atomic_check(crtc, state->crtc_states[i]);
529 if (ret) {
530 DRM_DEBUG_ATOMIC("[CRTC:%d] atomic driver check failed\n",
531 crtc->base.id);
532 return ret;
533 }
534 }
535
536 return ret;
537 }
538 EXPORT_SYMBOL(drm_atomic_helper_check_planes);
539
540 /**
541 * drm_atomic_helper_check - validate state object
542 * @dev: DRM device
543 * @state: the driver state object
544 *
545 * Check the state object to see if the requested state is physically possible.
546 * Only crtcs and planes have check callbacks, so for any additional (global)
547 * checking that a driver needs it can simply wrap that around this function.
548 * Drivers without such needs can directly use this as their ->atomic_check()
549 * callback.
550 *
551 * This just wraps the two parts of the state checking for planes and modeset
552 * state in the default order: First it calls drm_atomic_helper_check_modeset()
553 * and then drm_atomic_helper_check_planes(). The assumption is that the
554 * ->atomic_check functions depend upon an updated adjusted_mode.clock to
555 * e.g. properly compute watermarks.
556 *
557 * RETURNS
558 * Zero for success or -errno
559 */
560 int drm_atomic_helper_check(struct drm_device *dev,
561 struct drm_atomic_state *state)
562 {
563 int ret;
564
565 ret = drm_atomic_helper_check_modeset(dev, state);
566 if (ret)
567 return ret;
568
569 ret = drm_atomic_helper_check_planes(dev, state);
570 if (ret)
571 return ret;
572
573 return ret;
574 }
575 EXPORT_SYMBOL(drm_atomic_helper_check);
576
577 static void
578 disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state)
579 {
580 struct drm_connector *connector;
581 struct drm_connector_state *old_conn_state;
582 struct drm_crtc *crtc;
583 struct drm_crtc_state *old_crtc_state;
584 int i;
585
586 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
587 const struct drm_encoder_helper_funcs *funcs;
588 struct drm_encoder *encoder;
589 struct drm_crtc_state *old_crtc_state;
590
591 /* Shut down everything that's in the changeset and currently
592 * still on. So need to check the old, saved state. */
593 if (!old_conn_state->crtc)
594 continue;
595
596 old_crtc_state = old_state->crtc_states[drm_crtc_index(old_conn_state->crtc)];
597
598 if (!old_crtc_state->active ||
599 !drm_atomic_crtc_needs_modeset(old_conn_state->crtc->state))
600 continue;
601
602 encoder = old_conn_state->best_encoder;
603
604 /* We shouldn't get this far if we didn't previously have
605 * an encoder.. but WARN_ON() rather than explode.
606 */
607 if (WARN_ON(!encoder))
608 continue;
609
610 funcs = encoder->helper_private;
611
612 DRM_DEBUG_ATOMIC("disabling [ENCODER:%d:%s]\n",
613 encoder->base.id, encoder->name);
614
615 /*
616 * Each encoder has at most one connector (since we always steal
617 * it away), so we won't call disable hooks twice.
618 */
619 drm_bridge_disable(encoder->bridge);
620
621 /* Right function depends upon target state. */
622 if (connector->state->crtc && funcs->prepare)
623 funcs->prepare(encoder);
624 else if (funcs->disable)
625 funcs->disable(encoder);
626 else
627 funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
628
629 drm_bridge_post_disable(encoder->bridge);
630 }
631
632 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
633 const struct drm_crtc_helper_funcs *funcs;
634
635 /* Shut down everything that needs a full modeset. */
636 if (!drm_atomic_crtc_needs_modeset(crtc->state))
637 continue;
638
639 if (!old_crtc_state->active)
640 continue;
641
642 funcs = crtc->helper_private;
643
644 DRM_DEBUG_ATOMIC("disabling [CRTC:%d]\n",
645 crtc->base.id);
646
647
648 /* Right function depends upon target state. */
649 if (crtc->state->enable && funcs->prepare)
650 funcs->prepare(crtc);
651 else if (funcs->disable)
652 funcs->disable(crtc);
653 else
654 funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
655 }
656 }
657
658 /**
659 * drm_atomic_helper_update_legacy_modeset_state - update legacy modeset state
660 * @dev: DRM device
661 * @old_state: atomic state object with old state structures
662 *
663 * This function updates all the various legacy modeset state pointers in
664 * connectors, encoders and crtcs. It also updates the timestamping constants
665 * used for precise vblank timestamps by calling
666 * drm_calc_timestamping_constants().
667 *
668 * Drivers can use this for building their own atomic commit if they don't have
669 * a pure helper-based modeset implementation.
670 */
671 void
672 drm_atomic_helper_update_legacy_modeset_state(struct drm_device *dev,
673 struct drm_atomic_state *old_state)
674 {
675 struct drm_connector *connector;
676 struct drm_connector_state *old_conn_state;
677 struct drm_crtc *crtc;
678 struct drm_crtc_state *old_crtc_state;
679 int i;
680
681 /* clear out existing links and update dpms */
682 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
683 if (connector->encoder) {
684 WARN_ON(!connector->encoder->crtc);
685
686 connector->encoder->crtc = NULL;
687 connector->encoder = NULL;
688 }
689
690 crtc = connector->state->crtc;
691 if ((!crtc && old_conn_state->crtc) ||
692 (crtc && drm_atomic_crtc_needs_modeset(crtc->state))) {
693 struct drm_property *dpms_prop =
694 dev->mode_config.dpms_property;
695 int mode = DRM_MODE_DPMS_OFF;
696
697 if (crtc && crtc->state->active)
698 mode = DRM_MODE_DPMS_ON;
699
700 connector->dpms = mode;
701 drm_object_property_set_value(&connector->base,
702 dpms_prop, mode);
703 }
704 }
705
706 /* set new links */
707 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
708 if (!connector->state->crtc)
709 continue;
710
711 if (WARN_ON(!connector->state->best_encoder))
712 continue;
713
714 connector->encoder = connector->state->best_encoder;
715 connector->encoder->crtc = connector->state->crtc;
716 }
717
718 /* set legacy state in the crtc structure */
719 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
720 struct drm_plane *primary = crtc->primary;
721
722 crtc->mode = crtc->state->mode;
723 crtc->enabled = crtc->state->enable;
724
725 if (drm_atomic_get_existing_plane_state(old_state, primary) &&
726 primary->state->crtc == crtc) {
727 crtc->x = primary->state->src_x >> 16;
728 crtc->y = primary->state->src_y >> 16;
729 }
730
731 if (crtc->state->enable)
732 drm_calc_timestamping_constants(crtc,
733 &crtc->state->adjusted_mode);
734 }
735 }
736 EXPORT_SYMBOL(drm_atomic_helper_update_legacy_modeset_state);
737
738 static void
739 crtc_set_mode(struct drm_device *dev, struct drm_atomic_state *old_state)
740 {
741 struct drm_crtc *crtc;
742 struct drm_crtc_state *old_crtc_state;
743 struct drm_connector *connector;
744 struct drm_connector_state *old_conn_state __unused;
745 int i;
746
747 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
748 const struct drm_crtc_helper_funcs *funcs;
749
750 if (!crtc->state->mode_changed)
751 continue;
752
753 funcs = crtc->helper_private;
754
755 if (crtc->state->enable && funcs->mode_set_nofb) {
756 DRM_DEBUG_ATOMIC("modeset on [CRTC:%d]\n",
757 crtc->base.id);
758
759 funcs->mode_set_nofb(crtc);
760 }
761 }
762
763 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
764 const struct drm_encoder_helper_funcs *funcs;
765 struct drm_crtc_state *new_crtc_state;
766 struct drm_encoder *encoder;
767 struct drm_display_mode *mode, *adjusted_mode;
768
769 if (!connector->state->best_encoder)
770 continue;
771
772 encoder = connector->state->best_encoder;
773 funcs = encoder->helper_private;
774 new_crtc_state = connector->state->crtc->state;
775 mode = &new_crtc_state->mode;
776 adjusted_mode = &new_crtc_state->adjusted_mode;
777
778 if (!new_crtc_state->mode_changed)
779 continue;
780
781 DRM_DEBUG_ATOMIC("modeset on [ENCODER:%d:%s]\n",
782 encoder->base.id, encoder->name);
783
784 /*
785 * Each encoder has at most one connector (since we always steal
786 * it away), so we won't call mode_set hooks twice.
787 */
788 if (funcs->mode_set)
789 funcs->mode_set(encoder, mode, adjusted_mode);
790
791 drm_bridge_mode_set(encoder->bridge, mode, adjusted_mode);
792 }
793 }
794
795 /**
796 * drm_atomic_helper_commit_modeset_disables - modeset commit to disable outputs
797 * @dev: DRM device
798 * @old_state: atomic state object with old state structures
799 *
800 * This function shuts down all the outputs that need to be shut down and
801 * prepares them (if required) with the new mode.
802 *
803 * For compatibility with legacy crtc helpers this should be called before
804 * drm_atomic_helper_commit_planes(), which is what the default commit function
805 * does. But drivers with different needs can group the modeset commits together
806 * and do the plane commits at the end. This is useful for drivers doing runtime
807 * PM since planes updates then only happen when the CRTC is actually enabled.
808 */
809 void drm_atomic_helper_commit_modeset_disables(struct drm_device *dev,
810 struct drm_atomic_state *old_state)
811 {
812 disable_outputs(dev, old_state);
813
814 drm_atomic_helper_update_legacy_modeset_state(dev, old_state);
815
816 crtc_set_mode(dev, old_state);
817 }
818 EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_disables);
819
820 /**
821 * drm_atomic_helper_commit_modeset_enables - modeset commit to enable outputs
822 * @dev: DRM device
823 * @old_state: atomic state object with old state structures
824 *
825 * This function enables all the outputs with the new configuration which had to
826 * be turned off for the update.
827 *
828 * For compatibility with legacy crtc helpers this should be called after
829 * drm_atomic_helper_commit_planes(), which is what the default commit function
830 * does. But drivers with different needs can group the modeset commits together
831 * and do the plane commits at the end. This is useful for drivers doing runtime
832 * PM since planes updates then only happen when the CRTC is actually enabled.
833 */
834 void drm_atomic_helper_commit_modeset_enables(struct drm_device *dev,
835 struct drm_atomic_state *old_state)
836 {
837 struct drm_crtc *crtc;
838 struct drm_crtc_state *old_crtc_state __unused;
839 struct drm_connector *connector;
840 struct drm_connector_state *old_conn_state __unused;
841 int i;
842
843 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
844 const struct drm_crtc_helper_funcs *funcs;
845
846 /* Need to filter out CRTCs where only planes change. */
847 if (!drm_atomic_crtc_needs_modeset(crtc->state))
848 continue;
849
850 if (!crtc->state->active)
851 continue;
852
853 funcs = crtc->helper_private;
854
855 if (crtc->state->enable) {
856 DRM_DEBUG_ATOMIC("enabling [CRTC:%d]\n",
857 crtc->base.id);
858
859 if (funcs->enable)
860 funcs->enable(crtc);
861 else
862 funcs->commit(crtc);
863 }
864 }
865
866 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
867 const struct drm_encoder_helper_funcs *funcs;
868 struct drm_encoder *encoder;
869
870 if (!connector->state->best_encoder)
871 continue;
872
873 if (!connector->state->crtc->state->active ||
874 !drm_atomic_crtc_needs_modeset(connector->state->crtc->state))
875 continue;
876
877 encoder = connector->state->best_encoder;
878 funcs = encoder->helper_private;
879
880 DRM_DEBUG_ATOMIC("enabling [ENCODER:%d:%s]\n",
881 encoder->base.id, encoder->name);
882
883 /*
884 * Each encoder has at most one connector (since we always steal
885 * it away), so we won't call enable hooks twice.
886 */
887 drm_bridge_pre_enable(encoder->bridge);
888
889 if (funcs->enable)
890 funcs->enable(encoder);
891 else
892 funcs->commit(encoder);
893
894 drm_bridge_enable(encoder->bridge);
895 }
896 }
897 EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_enables);
898
899 static void wait_for_fences(struct drm_device *dev,
900 struct drm_atomic_state *state)
901 {
902 struct drm_plane *plane;
903 struct drm_plane_state *plane_state;
904 int i;
905
906 for_each_plane_in_state(state, plane, plane_state, i) {
907 if (!plane->state->fence)
908 continue;
909
910 WARN_ON(!plane->state->fb);
911
912 fence_wait(plane->state->fence, false);
913 fence_put(plane->state->fence);
914 plane->state->fence = NULL;
915 }
916 }
917
918 static bool framebuffer_changed(struct drm_device *dev,
919 struct drm_atomic_state *old_state,
920 struct drm_crtc *crtc)
921 {
922 struct drm_plane *plane;
923 struct drm_plane_state *old_plane_state;
924 int i;
925
926 for_each_plane_in_state(old_state, plane, old_plane_state, i) {
927 if (plane->state->crtc != crtc &&
928 old_plane_state->crtc != crtc)
929 continue;
930
931 if (plane->state->fb != old_plane_state->fb)
932 return true;
933 }
934
935 return false;
936 }
937
938 /**
939 * drm_atomic_helper_wait_for_vblanks - wait for vblank on crtcs
940 * @dev: DRM device
941 * @old_state: atomic state object with old state structures
942 *
943 * Helper to, after atomic commit, wait for vblanks on all effected
944 * crtcs (ie. before cleaning up old framebuffers using
945 * drm_atomic_helper_cleanup_planes()). It will only wait on crtcs where the
946 * framebuffers have actually changed to optimize for the legacy cursor and
947 * plane update use-case.
948 */
949 void
950 drm_atomic_helper_wait_for_vblanks(struct drm_device *dev,
951 struct drm_atomic_state *old_state)
952 {
953 struct drm_crtc *crtc;
954 struct drm_crtc_state *old_crtc_state;
955 int i, ret;
956
957 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
958 /* No one cares about the old state, so abuse it for tracking
959 * and store whether we hold a vblank reference (and should do a
960 * vblank wait) in the ->enable boolean. */
961 old_crtc_state->enable = false;
962
963 if (!crtc->state->enable)
964 continue;
965
966 /* Legacy cursor ioctls are completely unsynced, and userspace
967 * relies on that (by doing tons of cursor updates). */
968 if (old_state->legacy_cursor_update)
969 continue;
970
971 if (!framebuffer_changed(dev, old_state, crtc))
972 continue;
973
974 ret = drm_crtc_vblank_get(crtc);
975 if (ret != 0)
976 continue;
977
978 old_crtc_state->enable = true;
979 old_crtc_state->last_vblank_count = drm_crtc_vblank_count(crtc);
980 }
981
982 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
983 if (!old_crtc_state->enable)
984 continue;
985
986 #ifdef __NetBSD__
987 spin_lock(&dev->vbl_lock);
988 DRM_SPIN_WAIT_ON(ret, &dev->vblank[i].queue, &dev->vbl_lock,
989 msecs_to_jiffies(50),
990 (old_crtc_state->last_vblank_count !=
991 drm_crtc_vblank_count(crtc)));
992 spin_unlock(&dev->vbl_lock);
993 #else
994 ret = wait_event_timeout(dev->vblank[i].queue,
995 old_crtc_state->last_vblank_count !=
996 drm_crtc_vblank_count(crtc),
997 msecs_to_jiffies(50));
998 #endif
999
1000 drm_crtc_vblank_put(crtc);
1001 }
1002 }
1003 EXPORT_SYMBOL(drm_atomic_helper_wait_for_vblanks);
1004
1005 /**
1006 * drm_atomic_helper_commit - commit validated state object
1007 * @dev: DRM device
1008 * @state: the driver state object
1009 * @async: asynchronous commit
1010 *
1011 * This function commits a with drm_atomic_helper_check() pre-validated state
1012 * object. This can still fail when e.g. the framebuffer reservation fails. For
1013 * now this doesn't implement asynchronous commits.
1014 *
1015 * Note that right now this function does not support async commits, and hence
1016 * driver writers must implement their own version for now. Also note that the
1017 * default ordering of how the various stages are called is to match the legacy
1018 * modeset helper library closest. One peculiarity of that is that it doesn't
1019 * mesh well with runtime PM at all.
1020 *
1021 * For drivers supporting runtime PM the recommended sequence is
1022 *
1023 * drm_atomic_helper_commit_modeset_disables(dev, state);
1024 *
1025 * drm_atomic_helper_commit_modeset_enables(dev, state);
1026 *
1027 * drm_atomic_helper_commit_planes(dev, state, true);
1028 *
1029 * See the kerneldoc entries for these three functions for more details.
1030 *
1031 * RETURNS
1032 * Zero for success or -errno.
1033 */
1034 int drm_atomic_helper_commit(struct drm_device *dev,
1035 struct drm_atomic_state *state,
1036 bool async)
1037 {
1038 int ret;
1039
1040 if (async)
1041 return -EBUSY;
1042
1043 ret = drm_atomic_helper_prepare_planes(dev, state);
1044 if (ret)
1045 return ret;
1046
1047 /*
1048 * This is the point of no return - everything below never fails except
1049 * when the hw goes bonghits. Which means we can commit the new state on
1050 * the software side now.
1051 */
1052
1053 drm_atomic_helper_swap_state(dev, state);
1054
1055 /*
1056 * Everything below can be run asynchronously without the need to grab
1057 * any modeset locks at all under one condition: It must be guaranteed
1058 * that the asynchronous work has either been cancelled (if the driver
1059 * supports it, which at least requires that the framebuffers get
1060 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
1061 * before the new state gets committed on the software side with
1062 * drm_atomic_helper_swap_state().
1063 *
1064 * This scheme allows new atomic state updates to be prepared and
1065 * checked in parallel to the asynchronous completion of the previous
1066 * update. Which is important since compositors need to figure out the
1067 * composition of the next frame right after having submitted the
1068 * current layout.
1069 */
1070
1071 wait_for_fences(dev, state);
1072
1073 drm_atomic_helper_commit_modeset_disables(dev, state);
1074
1075 drm_atomic_helper_commit_planes(dev, state, false);
1076
1077 drm_atomic_helper_commit_modeset_enables(dev, state);
1078
1079 drm_atomic_helper_wait_for_vblanks(dev, state);
1080
1081 drm_atomic_helper_cleanup_planes(dev, state);
1082
1083 drm_atomic_state_free(state);
1084
1085 return 0;
1086 }
1087 EXPORT_SYMBOL(drm_atomic_helper_commit);
1088
1089 /**
1090 * DOC: implementing async commit
1091 *
1092 * For now the atomic helpers don't support async commit directly. If there is
1093 * real need it could be added though, using the dma-buf fence infrastructure
1094 * for generic synchronization with outstanding rendering.
1095 *
1096 * For now drivers have to implement async commit themselves, with the following
1097 * sequence being the recommended one:
1098 *
1099 * 1. Run drm_atomic_helper_prepare_planes() first. This is the only function
1100 * which commit needs to call which can fail, so we want to run it first and
1101 * synchronously.
1102 *
1103 * 2. Synchronize with any outstanding asynchronous commit worker threads which
1104 * might be affected the new state update. This can be done by either cancelling
1105 * or flushing the work items, depending upon whether the driver can deal with
1106 * cancelled updates. Note that it is important to ensure that the framebuffer
1107 * cleanup is still done when cancelling.
1108 *
1109 * For sufficient parallelism it is recommended to have a work item per crtc
1110 * (for updates which don't touch global state) and a global one. Then we only
1111 * need to synchronize with the crtc work items for changed crtcs and the global
1112 * work item, which allows nice concurrent updates on disjoint sets of crtcs.
1113 *
1114 * 3. The software state is updated synchronously with
1115 * drm_atomic_helper_swap_state(). Doing this under the protection of all modeset
1116 * locks means concurrent callers never see inconsistent state. And doing this
1117 * while it's guaranteed that no relevant async worker runs means that async
1118 * workers do not need grab any locks. Actually they must not grab locks, for
1119 * otherwise the work flushing will deadlock.
1120 *
1121 * 4. Schedule a work item to do all subsequent steps, using the split-out
1122 * commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and
1123 * then cleaning up the framebuffers after the old framebuffer is no longer
1124 * being displayed.
1125 */
1126
1127 /**
1128 * drm_atomic_helper_prepare_planes - prepare plane resources before commit
1129 * @dev: DRM device
1130 * @state: atomic state object with new state structures
1131 *
1132 * This function prepares plane state, specifically framebuffers, for the new
1133 * configuration. If any failure is encountered this function will call
1134 * ->cleanup_fb on any already successfully prepared framebuffer.
1135 *
1136 * Returns:
1137 * 0 on success, negative error code on failure.
1138 */
1139 int drm_atomic_helper_prepare_planes(struct drm_device *dev,
1140 struct drm_atomic_state *state)
1141 {
1142 int nplanes = dev->mode_config.num_total_plane;
1143 int ret, i;
1144
1145 for (i = 0; i < nplanes; i++) {
1146 const struct drm_plane_helper_funcs *funcs;
1147 struct drm_plane *plane = state->planes[i];
1148 struct drm_plane_state *plane_state = state->plane_states[i];
1149
1150 if (!plane)
1151 continue;
1152
1153 funcs = plane->helper_private;
1154
1155 if (funcs->prepare_fb) {
1156 ret = funcs->prepare_fb(plane, plane_state);
1157 if (ret)
1158 goto fail;
1159 }
1160 }
1161
1162 return 0;
1163
1164 fail:
1165 for (i--; i >= 0; i--) {
1166 const struct drm_plane_helper_funcs *funcs;
1167 struct drm_plane *plane = state->planes[i];
1168 struct drm_plane_state *plane_state = state->plane_states[i];
1169
1170 if (!plane)
1171 continue;
1172
1173 funcs = plane->helper_private;
1174
1175 if (funcs->cleanup_fb)
1176 funcs->cleanup_fb(plane, plane_state);
1177
1178 }
1179
1180 return ret;
1181 }
1182 EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
1183
1184 static bool plane_crtc_active(struct drm_plane_state *state)
1185 {
1186 return state->crtc && state->crtc->state->active;
1187 }
1188
1189 /**
1190 * drm_atomic_helper_commit_planes - commit plane state
1191 * @dev: DRM device
1192 * @old_state: atomic state object with old state structures
1193 * @active_only: Only commit on active CRTC if set
1194 *
1195 * This function commits the new plane state using the plane and atomic helper
1196 * functions for planes and crtcs. It assumes that the atomic state has already
1197 * been pushed into the relevant object state pointers, since this step can no
1198 * longer fail.
1199 *
1200 * It still requires the global state object @old_state to know which planes and
1201 * crtcs need to be updated though.
1202 *
1203 * Note that this function does all plane updates across all CRTCs in one step.
1204 * If the hardware can't support this approach look at
1205 * drm_atomic_helper_commit_planes_on_crtc() instead.
1206 *
1207 * Plane parameters can be updated by applications while the associated CRTC is
1208 * disabled. The DRM/KMS core will store the parameters in the plane state,
1209 * which will be available to the driver when the CRTC is turned on. As a result
1210 * most drivers don't need to be immediately notified of plane updates for a
1211 * disabled CRTC.
1212 *
1213 * Unless otherwise needed, drivers are advised to set the @active_only
1214 * parameters to true in order not to receive plane update notifications related
1215 * to a disabled CRTC. This avoids the need to manually ignore plane updates in
1216 * driver code when the driver and/or hardware can't or just don't need to deal
1217 * with updates on disabled CRTCs, for example when supporting runtime PM.
1218 *
1219 * The drm_atomic_helper_commit() default implementation only sets @active_only
1220 * to false to most closely match the behaviour of the legacy helpers. This should
1221 * not be copied blindly by drivers.
1222 */
1223 void drm_atomic_helper_commit_planes(struct drm_device *dev,
1224 struct drm_atomic_state *old_state,
1225 bool active_only)
1226 {
1227 struct drm_crtc *crtc;
1228 struct drm_crtc_state *old_crtc_state;
1229 struct drm_plane *plane;
1230 struct drm_plane_state *old_plane_state;
1231 int i;
1232
1233 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
1234 const struct drm_crtc_helper_funcs *funcs;
1235
1236 funcs = crtc->helper_private;
1237
1238 if (!funcs || !funcs->atomic_begin)
1239 continue;
1240
1241 if (active_only && !crtc->state->active)
1242 continue;
1243
1244 funcs->atomic_begin(crtc, old_crtc_state);
1245 }
1246
1247 for_each_plane_in_state(old_state, plane, old_plane_state, i) {
1248 const struct drm_plane_helper_funcs *funcs;
1249 bool disabling;
1250
1251 funcs = plane->helper_private;
1252
1253 if (!funcs)
1254 continue;
1255
1256 disabling = drm_atomic_plane_disabling(plane, old_plane_state);
1257
1258 if (active_only) {
1259 /*
1260 * Skip planes related to inactive CRTCs. If the plane
1261 * is enabled use the state of the current CRTC. If the
1262 * plane is being disabled use the state of the old
1263 * CRTC to avoid skipping planes being disabled on an
1264 * active CRTC.
1265 */
1266 if (!disabling && !plane_crtc_active(plane->state))
1267 continue;
1268 if (disabling && !plane_crtc_active(old_plane_state))
1269 continue;
1270 }
1271
1272 /*
1273 * Special-case disabling the plane if drivers support it.
1274 */
1275 if (disabling && funcs->atomic_disable)
1276 funcs->atomic_disable(plane, old_plane_state);
1277 else if (plane->state->crtc || disabling)
1278 funcs->atomic_update(plane, old_plane_state);
1279 }
1280
1281 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
1282 const struct drm_crtc_helper_funcs *funcs;
1283
1284 funcs = crtc->helper_private;
1285
1286 if (!funcs || !funcs->atomic_flush)
1287 continue;
1288
1289 if (active_only && !crtc->state->active)
1290 continue;
1291
1292 funcs->atomic_flush(crtc, old_crtc_state);
1293 }
1294 }
1295 EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
1296
1297 /**
1298 * drm_atomic_helper_commit_planes_on_crtc - commit plane state for a crtc
1299 * @old_crtc_state: atomic state object with the old crtc state
1300 *
1301 * This function commits the new plane state using the plane and atomic helper
1302 * functions for planes on the specific crtc. It assumes that the atomic state
1303 * has already been pushed into the relevant object state pointers, since this
1304 * step can no longer fail.
1305 *
1306 * This function is useful when plane updates should be done crtc-by-crtc
1307 * instead of one global step like drm_atomic_helper_commit_planes() does.
1308 *
1309 * This function can only be savely used when planes are not allowed to move
1310 * between different CRTCs because this function doesn't handle inter-CRTC
1311 * depencies. Callers need to ensure that either no such depencies exist,
1312 * resolve them through ordering of commit calls or through some other means.
1313 */
1314 void
1315 drm_atomic_helper_commit_planes_on_crtc(struct drm_crtc_state *old_crtc_state)
1316 {
1317 const struct drm_crtc_helper_funcs *crtc_funcs;
1318 struct drm_crtc *crtc = old_crtc_state->crtc;
1319 struct drm_atomic_state *old_state = old_crtc_state->state;
1320 struct drm_plane *plane;
1321 unsigned plane_mask;
1322
1323 plane_mask = old_crtc_state->plane_mask;
1324 plane_mask |= crtc->state->plane_mask;
1325
1326 crtc_funcs = crtc->helper_private;
1327 if (crtc_funcs && crtc_funcs->atomic_begin)
1328 crtc_funcs->atomic_begin(crtc, old_crtc_state);
1329
1330 drm_for_each_plane_mask(plane, crtc->dev, plane_mask) {
1331 struct drm_plane_state *old_plane_state =
1332 drm_atomic_get_existing_plane_state(old_state, plane);
1333 const struct drm_plane_helper_funcs *plane_funcs;
1334
1335 plane_funcs = plane->helper_private;
1336
1337 if (!old_plane_state || !plane_funcs)
1338 continue;
1339
1340 WARN_ON(plane->state->crtc && plane->state->crtc != crtc);
1341
1342 if (drm_atomic_plane_disabling(plane, old_plane_state) &&
1343 plane_funcs->atomic_disable)
1344 plane_funcs->atomic_disable(plane, old_plane_state);
1345 else if (plane->state->crtc ||
1346 drm_atomic_plane_disabling(plane, old_plane_state))
1347 plane_funcs->atomic_update(plane, old_plane_state);
1348 }
1349
1350 if (crtc_funcs && crtc_funcs->atomic_flush)
1351 crtc_funcs->atomic_flush(crtc, old_crtc_state);
1352 }
1353 EXPORT_SYMBOL(drm_atomic_helper_commit_planes_on_crtc);
1354
1355 /**
1356 * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
1357 * @dev: DRM device
1358 * @old_state: atomic state object with old state structures
1359 *
1360 * This function cleans up plane state, specifically framebuffers, from the old
1361 * configuration. Hence the old configuration must be perserved in @old_state to
1362 * be able to call this function.
1363 *
1364 * This function must also be called on the new state when the atomic update
1365 * fails at any point after calling drm_atomic_helper_prepare_planes().
1366 */
1367 void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
1368 struct drm_atomic_state *old_state)
1369 {
1370 struct drm_plane *plane;
1371 struct drm_plane_state *plane_state;
1372 int i;
1373
1374 for_each_plane_in_state(old_state, plane, plane_state, i) {
1375 const struct drm_plane_helper_funcs *funcs;
1376
1377 funcs = plane->helper_private;
1378
1379 if (funcs->cleanup_fb)
1380 funcs->cleanup_fb(plane, plane_state);
1381 }
1382 }
1383 EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
1384
1385 /**
1386 * drm_atomic_helper_swap_state - store atomic state into current sw state
1387 * @dev: DRM device
1388 * @state: atomic state
1389 *
1390 * This function stores the atomic state into the current state pointers in all
1391 * driver objects. It should be called after all failing steps have been done
1392 * and succeeded, but before the actual hardware state is committed.
1393 *
1394 * For cleanup and error recovery the current state for all changed objects will
1395 * be swaped into @state.
1396 *
1397 * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
1398 *
1399 * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
1400 *
1401 * 2. Do any other steps that might fail.
1402 *
1403 * 3. Put the staged state into the current state pointers with this function.
1404 *
1405 * 4. Actually commit the hardware state.
1406 *
1407 * 5. Call drm_atomic_helper_cleanup_planes() with @state, which since step 3
1408 * contains the old state. Also do any other cleanup required with that state.
1409 */
1410 void drm_atomic_helper_swap_state(struct drm_device *dev,
1411 struct drm_atomic_state *state)
1412 {
1413 int i;
1414
1415 for (i = 0; i < dev->mode_config.num_connector; i++) {
1416 struct drm_connector *connector = state->connectors[i];
1417
1418 if (!connector)
1419 continue;
1420
1421 connector->state->state = state;
1422 swap(state->connector_states[i], connector->state);
1423 connector->state->state = NULL;
1424 }
1425
1426 for (i = 0; i < dev->mode_config.num_crtc; i++) {
1427 struct drm_crtc *crtc = state->crtcs[i];
1428
1429 if (!crtc)
1430 continue;
1431
1432 crtc->state->state = state;
1433 swap(state->crtc_states[i], crtc->state);
1434 crtc->state->state = NULL;
1435 }
1436
1437 for (i = 0; i < dev->mode_config.num_total_plane; i++) {
1438 struct drm_plane *plane = state->planes[i];
1439
1440 if (!plane)
1441 continue;
1442
1443 plane->state->state = state;
1444 swap(state->plane_states[i], plane->state);
1445 plane->state->state = NULL;
1446 }
1447 }
1448 EXPORT_SYMBOL(drm_atomic_helper_swap_state);
1449
1450 /**
1451 * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
1452 * @plane: plane object to update
1453 * @crtc: owning CRTC of owning plane
1454 * @fb: framebuffer to flip onto plane
1455 * @crtc_x: x offset of primary plane on crtc
1456 * @crtc_y: y offset of primary plane on crtc
1457 * @crtc_w: width of primary plane rectangle on crtc
1458 * @crtc_h: height of primary plane rectangle on crtc
1459 * @src_x: x offset of @fb for panning
1460 * @src_y: y offset of @fb for panning
1461 * @src_w: width of source rectangle in @fb
1462 * @src_h: height of source rectangle in @fb
1463 *
1464 * Provides a default plane update handler using the atomic driver interface.
1465 *
1466 * RETURNS:
1467 * Zero on success, error code on failure
1468 */
1469 int drm_atomic_helper_update_plane(struct drm_plane *plane,
1470 struct drm_crtc *crtc,
1471 struct drm_framebuffer *fb,
1472 int crtc_x, int crtc_y,
1473 unsigned int crtc_w, unsigned int crtc_h,
1474 uint32_t src_x, uint32_t src_y,
1475 uint32_t src_w, uint32_t src_h)
1476 {
1477 struct drm_atomic_state *state;
1478 struct drm_plane_state *plane_state;
1479 int ret = 0;
1480
1481 state = drm_atomic_state_alloc(plane->dev);
1482 if (!state)
1483 return -ENOMEM;
1484
1485 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1486 retry:
1487 plane_state = drm_atomic_get_plane_state(state, plane);
1488 if (IS_ERR(plane_state)) {
1489 ret = PTR_ERR(plane_state);
1490 goto fail;
1491 }
1492
1493 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
1494 if (ret != 0)
1495 goto fail;
1496 drm_atomic_set_fb_for_plane(plane_state, fb);
1497 plane_state->crtc_x = crtc_x;
1498 plane_state->crtc_y = crtc_y;
1499 plane_state->crtc_h = crtc_h;
1500 plane_state->crtc_w = crtc_w;
1501 plane_state->src_x = src_x;
1502 plane_state->src_y = src_y;
1503 plane_state->src_h = src_h;
1504 plane_state->src_w = src_w;
1505
1506 if (plane == crtc->cursor)
1507 state->legacy_cursor_update = true;
1508
1509 ret = drm_atomic_commit(state);
1510 if (ret != 0)
1511 goto fail;
1512
1513 /* Driver takes ownership of state on successful commit. */
1514 return 0;
1515 fail:
1516 if (ret == -EDEADLK)
1517 goto backoff;
1518
1519 drm_atomic_state_free(state);
1520
1521 return ret;
1522 backoff:
1523 drm_atomic_state_clear(state);
1524 drm_atomic_legacy_backoff(state);
1525
1526 /*
1527 * Someone might have exchanged the framebuffer while we dropped locks
1528 * in the backoff code. We need to fix up the fb refcount tracking the
1529 * core does for us.
1530 */
1531 plane->old_fb = plane->fb;
1532
1533 goto retry;
1534 }
1535 EXPORT_SYMBOL(drm_atomic_helper_update_plane);
1536
1537 /**
1538 * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
1539 * @plane: plane to disable
1540 *
1541 * Provides a default plane disable handler using the atomic driver interface.
1542 *
1543 * RETURNS:
1544 * Zero on success, error code on failure
1545 */
1546 int drm_atomic_helper_disable_plane(struct drm_plane *plane)
1547 {
1548 struct drm_atomic_state *state;
1549 struct drm_plane_state *plane_state;
1550 int ret = 0;
1551
1552 /*
1553 * FIXME: Without plane->crtc set we can't get at the implicit legacy
1554 * acquire context. The real fix will be to wire the acquire ctx through
1555 * everywhere we need it, but meanwhile prevent chaos by just skipping
1556 * this noop. The critical case is the cursor ioctls which a) only grab
1557 * crtc/cursor-plane locks (so we need the crtc to get at the right
1558 * acquire context) and b) can try to disable the plane multiple times.
1559 */
1560 if (!plane->crtc)
1561 return 0;
1562
1563 state = drm_atomic_state_alloc(plane->dev);
1564 if (!state)
1565 return -ENOMEM;
1566
1567 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(plane->crtc);
1568 retry:
1569 plane_state = drm_atomic_get_plane_state(state, plane);
1570 if (IS_ERR(plane_state)) {
1571 ret = PTR_ERR(plane_state);
1572 goto fail;
1573 }
1574
1575 if (plane_state->crtc && (plane == plane->crtc->cursor))
1576 plane_state->state->legacy_cursor_update = true;
1577
1578 ret = __drm_atomic_helper_disable_plane(plane, plane_state);
1579 if (ret != 0)
1580 goto fail;
1581
1582 ret = drm_atomic_commit(state);
1583 if (ret != 0)
1584 goto fail;
1585
1586 /* Driver takes ownership of state on successful commit. */
1587 return 0;
1588 fail:
1589 if (ret == -EDEADLK)
1590 goto backoff;
1591
1592 drm_atomic_state_free(state);
1593
1594 return ret;
1595 backoff:
1596 drm_atomic_state_clear(state);
1597 drm_atomic_legacy_backoff(state);
1598
1599 /*
1600 * Someone might have exchanged the framebuffer while we dropped locks
1601 * in the backoff code. We need to fix up the fb refcount tracking the
1602 * core does for us.
1603 */
1604 plane->old_fb = plane->fb;
1605
1606 goto retry;
1607 }
1608 EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
1609
1610 /* just used from fb-helper and atomic-helper: */
1611 int __drm_atomic_helper_disable_plane(struct drm_plane *plane,
1612 struct drm_plane_state *plane_state)
1613 {
1614 int ret;
1615
1616 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
1617 if (ret != 0)
1618 return ret;
1619
1620 drm_atomic_set_fb_for_plane(plane_state, NULL);
1621 plane_state->crtc_x = 0;
1622 plane_state->crtc_y = 0;
1623 plane_state->crtc_h = 0;
1624 plane_state->crtc_w = 0;
1625 plane_state->src_x = 0;
1626 plane_state->src_y = 0;
1627 plane_state->src_h = 0;
1628 plane_state->src_w = 0;
1629
1630 return 0;
1631 }
1632
1633 static int update_output_state(struct drm_atomic_state *state,
1634 struct drm_mode_set *set)
1635 {
1636 struct drm_device *dev = set->crtc->dev;
1637 struct drm_crtc *crtc;
1638 struct drm_crtc_state *crtc_state;
1639 struct drm_connector *connector;
1640 struct drm_connector_state *conn_state;
1641 int ret, i, j;
1642
1643 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
1644 state->acquire_ctx);
1645 if (ret)
1646 return ret;
1647
1648 /* First grab all affected connector/crtc states. */
1649 for (i = 0; i < set->num_connectors; i++) {
1650 conn_state = drm_atomic_get_connector_state(state,
1651 set->connectors[i]);
1652 if (IS_ERR(conn_state))
1653 return PTR_ERR(conn_state);
1654 }
1655
1656 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1657 ret = drm_atomic_add_affected_connectors(state, crtc);
1658 if (ret)
1659 return ret;
1660 }
1661
1662 /* Then recompute connector->crtc links and crtc enabling state. */
1663 for_each_connector_in_state(state, connector, conn_state, i) {
1664 if (conn_state->crtc == set->crtc) {
1665 ret = drm_atomic_set_crtc_for_connector(conn_state,
1666 NULL);
1667 if (ret)
1668 return ret;
1669 }
1670
1671 for (j = 0; j < set->num_connectors; j++) {
1672 if (set->connectors[j] == connector) {
1673 ret = drm_atomic_set_crtc_for_connector(conn_state,
1674 set->crtc);
1675 if (ret)
1676 return ret;
1677 break;
1678 }
1679 }
1680 }
1681
1682 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1683 /* Don't update ->enable for the CRTC in the set_config request,
1684 * since a mismatch would indicate a bug in the upper layers.
1685 * The actual modeset code later on will catch any
1686 * inconsistencies here. */
1687 if (crtc == set->crtc)
1688 continue;
1689
1690 if (!drm_atomic_connectors_for_crtc(state, crtc)) {
1691 ret = drm_atomic_set_mode_prop_for_crtc(crtc_state,
1692 NULL);
1693 if (ret < 0)
1694 return ret;
1695
1696 crtc_state->active = false;
1697 }
1698 }
1699
1700 return 0;
1701 }
1702
1703 /**
1704 * drm_atomic_helper_set_config - set a new config from userspace
1705 * @set: mode set configuration
1706 *
1707 * Provides a default crtc set_config handler using the atomic driver interface.
1708 *
1709 * Returns:
1710 * Returns 0 on success, negative errno numbers on failure.
1711 */
1712 int drm_atomic_helper_set_config(struct drm_mode_set *set)
1713 {
1714 struct drm_atomic_state *state;
1715 struct drm_crtc *crtc = set->crtc;
1716 int ret = 0;
1717
1718 state = drm_atomic_state_alloc(crtc->dev);
1719 if (!state)
1720 return -ENOMEM;
1721
1722 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1723 retry:
1724 ret = __drm_atomic_helper_set_config(set, state);
1725 if (ret != 0)
1726 goto fail;
1727
1728 ret = drm_atomic_commit(state);
1729 if (ret != 0)
1730 goto fail;
1731
1732 /* Driver takes ownership of state on successful commit. */
1733 return 0;
1734 fail:
1735 if (ret == -EDEADLK)
1736 goto backoff;
1737
1738 drm_atomic_state_free(state);
1739
1740 return ret;
1741 backoff:
1742 drm_atomic_state_clear(state);
1743 drm_atomic_legacy_backoff(state);
1744
1745 /*
1746 * Someone might have exchanged the framebuffer while we dropped locks
1747 * in the backoff code. We need to fix up the fb refcount tracking the
1748 * core does for us.
1749 */
1750 crtc->primary->old_fb = crtc->primary->fb;
1751
1752 goto retry;
1753 }
1754 EXPORT_SYMBOL(drm_atomic_helper_set_config);
1755
1756 /* just used from fb-helper and atomic-helper: */
1757 int __drm_atomic_helper_set_config(struct drm_mode_set *set,
1758 struct drm_atomic_state *state)
1759 {
1760 struct drm_crtc_state *crtc_state;
1761 struct drm_plane_state *primary_state;
1762 struct drm_crtc *crtc = set->crtc;
1763 int hdisplay, vdisplay;
1764 int ret;
1765
1766 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1767 if (IS_ERR(crtc_state))
1768 return PTR_ERR(crtc_state);
1769
1770 primary_state = drm_atomic_get_plane_state(state, crtc->primary);
1771 if (IS_ERR(primary_state))
1772 return PTR_ERR(primary_state);
1773
1774 if (!set->mode) {
1775 WARN_ON(set->fb);
1776 WARN_ON(set->num_connectors);
1777
1778 ret = drm_atomic_set_mode_for_crtc(crtc_state, NULL);
1779 if (ret != 0)
1780 return ret;
1781
1782 crtc_state->active = false;
1783
1784 ret = drm_atomic_set_crtc_for_plane(primary_state, NULL);
1785 if (ret != 0)
1786 return ret;
1787
1788 drm_atomic_set_fb_for_plane(primary_state, NULL);
1789
1790 goto commit;
1791 }
1792
1793 WARN_ON(!set->fb);
1794 WARN_ON(!set->num_connectors);
1795
1796 ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
1797 if (ret != 0)
1798 return ret;
1799
1800 crtc_state->active = true;
1801
1802 ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
1803 if (ret != 0)
1804 return ret;
1805
1806 drm_crtc_get_hv_timing(set->mode, &hdisplay, &vdisplay);
1807
1808 drm_atomic_set_fb_for_plane(primary_state, set->fb);
1809 primary_state->crtc_x = 0;
1810 primary_state->crtc_y = 0;
1811 primary_state->crtc_h = vdisplay;
1812 primary_state->crtc_w = hdisplay;
1813 primary_state->src_x = set->x << 16;
1814 primary_state->src_y = set->y << 16;
1815 if (primary_state->rotation & (BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270))) {
1816 primary_state->src_h = hdisplay << 16;
1817 primary_state->src_w = vdisplay << 16;
1818 } else {
1819 primary_state->src_h = vdisplay << 16;
1820 primary_state->src_w = hdisplay << 16;
1821 }
1822
1823 commit:
1824 ret = update_output_state(state, set);
1825 if (ret)
1826 return ret;
1827
1828 return 0;
1829 }
1830
1831 /**
1832 * drm_atomic_helper_crtc_set_property - helper for crtc properties
1833 * @crtc: DRM crtc
1834 * @property: DRM property
1835 * @val: value of property
1836 *
1837 * Provides a default crtc set_property handler using the atomic driver
1838 * interface.
1839 *
1840 * RETURNS:
1841 * Zero on success, error code on failure
1842 */
1843 int
1844 drm_atomic_helper_crtc_set_property(struct drm_crtc *crtc,
1845 struct drm_property *property,
1846 uint64_t val)
1847 {
1848 struct drm_atomic_state *state;
1849 struct drm_crtc_state *crtc_state;
1850 int ret = 0;
1851
1852 state = drm_atomic_state_alloc(crtc->dev);
1853 if (!state)
1854 return -ENOMEM;
1855
1856 /* ->set_property is always called with all locks held. */
1857 state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
1858 retry:
1859 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1860 if (IS_ERR(crtc_state)) {
1861 ret = PTR_ERR(crtc_state);
1862 goto fail;
1863 }
1864
1865 ret = drm_atomic_crtc_set_property(crtc, crtc_state,
1866 property, val);
1867 if (ret)
1868 goto fail;
1869
1870 ret = drm_atomic_commit(state);
1871 if (ret != 0)
1872 goto fail;
1873
1874 /* Driver takes ownership of state on successful commit. */
1875 return 0;
1876 fail:
1877 if (ret == -EDEADLK)
1878 goto backoff;
1879
1880 drm_atomic_state_free(state);
1881
1882 return ret;
1883 backoff:
1884 drm_atomic_state_clear(state);
1885 drm_atomic_legacy_backoff(state);
1886
1887 goto retry;
1888 }
1889 EXPORT_SYMBOL(drm_atomic_helper_crtc_set_property);
1890
1891 /**
1892 * drm_atomic_helper_plane_set_property - helper for plane properties
1893 * @plane: DRM plane
1894 * @property: DRM property
1895 * @val: value of property
1896 *
1897 * Provides a default plane set_property handler using the atomic driver
1898 * interface.
1899 *
1900 * RETURNS:
1901 * Zero on success, error code on failure
1902 */
1903 int
1904 drm_atomic_helper_plane_set_property(struct drm_plane *plane,
1905 struct drm_property *property,
1906 uint64_t val)
1907 {
1908 struct drm_atomic_state *state;
1909 struct drm_plane_state *plane_state;
1910 int ret = 0;
1911
1912 state = drm_atomic_state_alloc(plane->dev);
1913 if (!state)
1914 return -ENOMEM;
1915
1916 /* ->set_property is always called with all locks held. */
1917 state->acquire_ctx = plane->dev->mode_config.acquire_ctx;
1918 retry:
1919 plane_state = drm_atomic_get_plane_state(state, plane);
1920 if (IS_ERR(plane_state)) {
1921 ret = PTR_ERR(plane_state);
1922 goto fail;
1923 }
1924
1925 ret = drm_atomic_plane_set_property(plane, plane_state,
1926 property, val);
1927 if (ret)
1928 goto fail;
1929
1930 ret = drm_atomic_commit(state);
1931 if (ret != 0)
1932 goto fail;
1933
1934 /* Driver takes ownership of state on successful commit. */
1935 return 0;
1936 fail:
1937 if (ret == -EDEADLK)
1938 goto backoff;
1939
1940 drm_atomic_state_free(state);
1941
1942 return ret;
1943 backoff:
1944 drm_atomic_state_clear(state);
1945 drm_atomic_legacy_backoff(state);
1946
1947 goto retry;
1948 }
1949 EXPORT_SYMBOL(drm_atomic_helper_plane_set_property);
1950
1951 /**
1952 * drm_atomic_helper_connector_set_property - helper for connector properties
1953 * @connector: DRM connector
1954 * @property: DRM property
1955 * @val: value of property
1956 *
1957 * Provides a default connector set_property handler using the atomic driver
1958 * interface.
1959 *
1960 * RETURNS:
1961 * Zero on success, error code on failure
1962 */
1963 int
1964 drm_atomic_helper_connector_set_property(struct drm_connector *connector,
1965 struct drm_property *property,
1966 uint64_t val)
1967 {
1968 struct drm_atomic_state *state;
1969 struct drm_connector_state *connector_state;
1970 int ret = 0;
1971
1972 state = drm_atomic_state_alloc(connector->dev);
1973 if (!state)
1974 return -ENOMEM;
1975
1976 /* ->set_property is always called with all locks held. */
1977 state->acquire_ctx = connector->dev->mode_config.acquire_ctx;
1978 retry:
1979 connector_state = drm_atomic_get_connector_state(state, connector);
1980 if (IS_ERR(connector_state)) {
1981 ret = PTR_ERR(connector_state);
1982 goto fail;
1983 }
1984
1985 ret = drm_atomic_connector_set_property(connector, connector_state,
1986 property, val);
1987 if (ret)
1988 goto fail;
1989
1990 ret = drm_atomic_commit(state);
1991 if (ret != 0)
1992 goto fail;
1993
1994 /* Driver takes ownership of state on successful commit. */
1995 return 0;
1996 fail:
1997 if (ret == -EDEADLK)
1998 goto backoff;
1999
2000 drm_atomic_state_free(state);
2001
2002 return ret;
2003 backoff:
2004 drm_atomic_state_clear(state);
2005 drm_atomic_legacy_backoff(state);
2006
2007 goto retry;
2008 }
2009 EXPORT_SYMBOL(drm_atomic_helper_connector_set_property);
2010
2011 /**
2012 * drm_atomic_helper_page_flip - execute a legacy page flip
2013 * @crtc: DRM crtc
2014 * @fb: DRM framebuffer
2015 * @event: optional DRM event to signal upon completion
2016 * @flags: flip flags for non-vblank sync'ed updates
2017 *
2018 * Provides a default page flip implementation using the atomic driver interface.
2019 *
2020 * Note that for now so called async page flips (i.e. updates which are not
2021 * synchronized to vblank) are not supported, since the atomic interfaces have
2022 * no provisions for this yet.
2023 *
2024 * Returns:
2025 * Returns 0 on success, negative errno numbers on failure.
2026 */
2027 int drm_atomic_helper_page_flip(struct drm_crtc *crtc,
2028 struct drm_framebuffer *fb,
2029 struct drm_pending_vblank_event *event,
2030 uint32_t flags)
2031 {
2032 struct drm_plane *plane = crtc->primary;
2033 struct drm_atomic_state *state;
2034 struct drm_plane_state *plane_state;
2035 struct drm_crtc_state *crtc_state;
2036 int ret = 0;
2037
2038 if (flags & DRM_MODE_PAGE_FLIP_ASYNC)
2039 return -EINVAL;
2040
2041 state = drm_atomic_state_alloc(plane->dev);
2042 if (!state)
2043 return -ENOMEM;
2044
2045 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2046 retry:
2047 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2048 if (IS_ERR(crtc_state)) {
2049 ret = PTR_ERR(crtc_state);
2050 goto fail;
2051 }
2052 crtc_state->event = event;
2053
2054 plane_state = drm_atomic_get_plane_state(state, plane);
2055 if (IS_ERR(plane_state)) {
2056 ret = PTR_ERR(plane_state);
2057 goto fail;
2058 }
2059
2060 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
2061 if (ret != 0)
2062 goto fail;
2063 drm_atomic_set_fb_for_plane(plane_state, fb);
2064
2065 ret = drm_atomic_async_commit(state);
2066 if (ret != 0)
2067 goto fail;
2068
2069 /* Driver takes ownership of state on successful async commit. */
2070 return 0;
2071 fail:
2072 if (ret == -EDEADLK)
2073 goto backoff;
2074
2075 drm_atomic_state_free(state);
2076
2077 return ret;
2078 backoff:
2079 drm_atomic_state_clear(state);
2080 drm_atomic_legacy_backoff(state);
2081
2082 /*
2083 * Someone might have exchanged the framebuffer while we dropped locks
2084 * in the backoff code. We need to fix up the fb refcount tracking the
2085 * core does for us.
2086 */
2087 plane->old_fb = plane->fb;
2088
2089 goto retry;
2090 }
2091 EXPORT_SYMBOL(drm_atomic_helper_page_flip);
2092
2093 /**
2094 * drm_atomic_helper_connector_dpms() - connector dpms helper implementation
2095 * @connector: affected connector
2096 * @mode: DPMS mode
2097 *
2098 * This is the main helper function provided by the atomic helper framework for
2099 * implementing the legacy DPMS connector interface. It computes the new desired
2100 * ->active state for the corresponding CRTC (if the connector is enabled) and
2101 * updates it.
2102 *
2103 * Returns:
2104 * Returns 0 on success, negative errno numbers on failure.
2105 */
2106 int drm_atomic_helper_connector_dpms(struct drm_connector *connector,
2107 int mode)
2108 {
2109 struct drm_mode_config *config = &connector->dev->mode_config;
2110 struct drm_atomic_state *state;
2111 struct drm_crtc_state *crtc_state;
2112 struct drm_crtc *crtc;
2113 struct drm_connector *tmp_connector;
2114 int ret;
2115 bool active = false;
2116 int old_mode = connector->dpms;
2117
2118 if (mode != DRM_MODE_DPMS_ON)
2119 mode = DRM_MODE_DPMS_OFF;
2120
2121 connector->dpms = mode;
2122 crtc = connector->state->crtc;
2123
2124 if (!crtc)
2125 return 0;
2126
2127 state = drm_atomic_state_alloc(connector->dev);
2128 if (!state)
2129 return -ENOMEM;
2130
2131 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2132 retry:
2133 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2134 if (IS_ERR(crtc_state)) {
2135 ret = PTR_ERR(crtc_state);
2136 goto fail;
2137 }
2138
2139 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
2140
2141 drm_for_each_connector(tmp_connector, connector->dev) {
2142 if (tmp_connector->state->crtc != crtc)
2143 continue;
2144
2145 if (tmp_connector->dpms == DRM_MODE_DPMS_ON) {
2146 active = true;
2147 break;
2148 }
2149 }
2150 crtc_state->active = active;
2151
2152 ret = drm_atomic_commit(state);
2153 if (ret != 0)
2154 goto fail;
2155
2156 /* Driver takes ownership of state on successful commit. */
2157 return 0;
2158 fail:
2159 if (ret == -EDEADLK)
2160 goto backoff;
2161
2162 connector->dpms = old_mode;
2163 drm_atomic_state_free(state);
2164
2165 return ret;
2166 backoff:
2167 drm_atomic_state_clear(state);
2168 drm_atomic_legacy_backoff(state);
2169
2170 goto retry;
2171 }
2172 EXPORT_SYMBOL(drm_atomic_helper_connector_dpms);
2173
2174 /**
2175 * DOC: atomic state reset and initialization
2176 *
2177 * Both the drm core and the atomic helpers assume that there is always the full
2178 * and correct atomic software state for all connectors, CRTCs and planes
2179 * available. Which is a bit a problem on driver load and also after system
2180 * suspend. One way to solve this is to have a hardware state read-out
2181 * infrastructure which reconstructs the full software state (e.g. the i915
2182 * driver).
2183 *
2184 * The simpler solution is to just reset the software state to everything off,
2185 * which is easiest to do by calling drm_mode_config_reset(). To facilitate this
2186 * the atomic helpers provide default reset implementations for all hooks.
2187 */
2188
2189 /**
2190 * drm_atomic_helper_crtc_reset - default ->reset hook for CRTCs
2191 * @crtc: drm CRTC
2192 *
2193 * Resets the atomic state for @crtc by freeing the state pointer (which might
2194 * be NULL, e.g. at driver load time) and allocating a new empty state object.
2195 */
2196 void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc)
2197 {
2198 if (crtc->state && crtc->state->mode_blob)
2199 drm_property_unreference_blob(crtc->state->mode_blob);
2200 kfree(crtc->state);
2201 crtc->state = kzalloc(sizeof(*crtc->state), GFP_KERNEL);
2202
2203 if (crtc->state)
2204 crtc->state->crtc = crtc;
2205 }
2206 EXPORT_SYMBOL(drm_atomic_helper_crtc_reset);
2207
2208 /**
2209 * __drm_atomic_helper_crtc_duplicate_state - copy atomic CRTC state
2210 * @crtc: CRTC object
2211 * @state: atomic CRTC state
2212 *
2213 * Copies atomic state from a CRTC's current state and resets inferred values.
2214 * This is useful for drivers that subclass the CRTC state.
2215 */
2216 void __drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc,
2217 struct drm_crtc_state *state)
2218 {
2219 memcpy(state, crtc->state, sizeof(*state));
2220
2221 if (state->mode_blob)
2222 drm_property_reference_blob(state->mode_blob);
2223 state->mode_changed = false;
2224 state->active_changed = false;
2225 state->planes_changed = false;
2226 state->connectors_changed = false;
2227 state->event = NULL;
2228 }
2229 EXPORT_SYMBOL(__drm_atomic_helper_crtc_duplicate_state);
2230
2231 /**
2232 * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
2233 * @crtc: drm CRTC
2234 *
2235 * Default CRTC state duplicate hook for drivers which don't have their own
2236 * subclassed CRTC state structure.
2237 */
2238 struct drm_crtc_state *
2239 drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc)
2240 {
2241 struct drm_crtc_state *state;
2242
2243 if (WARN_ON(!crtc->state))
2244 return NULL;
2245
2246 state = kmalloc(sizeof(*state), GFP_KERNEL);
2247 if (state)
2248 __drm_atomic_helper_crtc_duplicate_state(crtc, state);
2249
2250 return state;
2251 }
2252 EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state);
2253
2254 /**
2255 * __drm_atomic_helper_crtc_destroy_state - release CRTC state
2256 * @crtc: CRTC object
2257 * @state: CRTC state object to release
2258 *
2259 * Releases all resources stored in the CRTC state without actually freeing
2260 * the memory of the CRTC state. This is useful for drivers that subclass the
2261 * CRTC state.
2262 */
2263 void __drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
2264 struct drm_crtc_state *state)
2265 {
2266 if (state->mode_blob)
2267 drm_property_unreference_blob(state->mode_blob);
2268 }
2269 EXPORT_SYMBOL(__drm_atomic_helper_crtc_destroy_state);
2270
2271 /**
2272 * drm_atomic_helper_crtc_destroy_state - default state destroy hook
2273 * @crtc: drm CRTC
2274 * @state: CRTC state object to release
2275 *
2276 * Default CRTC state destroy hook for drivers which don't have their own
2277 * subclassed CRTC state structure.
2278 */
2279 void drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
2280 struct drm_crtc_state *state)
2281 {
2282 __drm_atomic_helper_crtc_destroy_state(crtc, state);
2283 kfree(state);
2284 }
2285 EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state);
2286
2287 /**
2288 * drm_atomic_helper_plane_reset - default ->reset hook for planes
2289 * @plane: drm plane
2290 *
2291 * Resets the atomic state for @plane by freeing the state pointer (which might
2292 * be NULL, e.g. at driver load time) and allocating a new empty state object.
2293 */
2294 void drm_atomic_helper_plane_reset(struct drm_plane *plane)
2295 {
2296 if (plane->state && plane->state->fb)
2297 drm_framebuffer_unreference(plane->state->fb);
2298
2299 kfree(plane->state);
2300 plane->state = kzalloc(sizeof(*plane->state), GFP_KERNEL);
2301
2302 if (plane->state)
2303 plane->state->plane = plane;
2304 }
2305 EXPORT_SYMBOL(drm_atomic_helper_plane_reset);
2306
2307 /**
2308 * __drm_atomic_helper_plane_duplicate_state - copy atomic plane state
2309 * @plane: plane object
2310 * @state: atomic plane state
2311 *
2312 * Copies atomic state from a plane's current state. This is useful for
2313 * drivers that subclass the plane state.
2314 */
2315 void __drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane,
2316 struct drm_plane_state *state)
2317 {
2318 memcpy(state, plane->state, sizeof(*state));
2319
2320 if (state->fb)
2321 drm_framebuffer_reference(state->fb);
2322 }
2323 EXPORT_SYMBOL(__drm_atomic_helper_plane_duplicate_state);
2324
2325 /**
2326 * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
2327 * @plane: drm plane
2328 *
2329 * Default plane state duplicate hook for drivers which don't have their own
2330 * subclassed plane state structure.
2331 */
2332 struct drm_plane_state *
2333 drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane)
2334 {
2335 struct drm_plane_state *state;
2336
2337 if (WARN_ON(!plane->state))
2338 return NULL;
2339
2340 state = kmalloc(sizeof(*state), GFP_KERNEL);
2341 if (state)
2342 __drm_atomic_helper_plane_duplicate_state(plane, state);
2343
2344 return state;
2345 }
2346 EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state);
2347
2348 /**
2349 * __drm_atomic_helper_plane_destroy_state - release plane state
2350 * @plane: plane object
2351 * @state: plane state object to release
2352 *
2353 * Releases all resources stored in the plane state without actually freeing
2354 * the memory of the plane state. This is useful for drivers that subclass the
2355 * plane state.
2356 */
2357 void __drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
2358 struct drm_plane_state *state)
2359 {
2360 if (state->fb)
2361 drm_framebuffer_unreference(state->fb);
2362 }
2363 EXPORT_SYMBOL(__drm_atomic_helper_plane_destroy_state);
2364
2365 /**
2366 * drm_atomic_helper_plane_destroy_state - default state destroy hook
2367 * @plane: drm plane
2368 * @state: plane state object to release
2369 *
2370 * Default plane state destroy hook for drivers which don't have their own
2371 * subclassed plane state structure.
2372 */
2373 void drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
2374 struct drm_plane_state *state)
2375 {
2376 __drm_atomic_helper_plane_destroy_state(plane, state);
2377 kfree(state);
2378 }
2379 EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state);
2380
2381 /**
2382 * drm_atomic_helper_connector_reset - default ->reset hook for connectors
2383 * @connector: drm connector
2384 *
2385 * Resets the atomic state for @connector by freeing the state pointer (which
2386 * might be NULL, e.g. at driver load time) and allocating a new empty state
2387 * object.
2388 */
2389 void drm_atomic_helper_connector_reset(struct drm_connector *connector)
2390 {
2391 kfree(connector->state);
2392 connector->state = kzalloc(sizeof(*connector->state), GFP_KERNEL);
2393
2394 if (connector->state)
2395 connector->state->connector = connector;
2396 }
2397 EXPORT_SYMBOL(drm_atomic_helper_connector_reset);
2398
2399 /**
2400 * __drm_atomic_helper_connector_duplicate_state - copy atomic connector state
2401 * @connector: connector object
2402 * @state: atomic connector state
2403 *
2404 * Copies atomic state from a connector's current state. This is useful for
2405 * drivers that subclass the connector state.
2406 */
2407 void
2408 __drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector,
2409 struct drm_connector_state *state)
2410 {
2411 memcpy(state, connector->state, sizeof(*state));
2412 }
2413 EXPORT_SYMBOL(__drm_atomic_helper_connector_duplicate_state);
2414
2415 /**
2416 * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
2417 * @connector: drm connector
2418 *
2419 * Default connector state duplicate hook for drivers which don't have their own
2420 * subclassed connector state structure.
2421 */
2422 struct drm_connector_state *
2423 drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
2424 {
2425 struct drm_connector_state *state;
2426
2427 if (WARN_ON(!connector->state))
2428 return NULL;
2429
2430 state = kmalloc(sizeof(*state), GFP_KERNEL);
2431 if (state)
2432 __drm_atomic_helper_connector_duplicate_state(connector, state);
2433
2434 return state;
2435 }
2436 EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
2437
2438 /**
2439 * drm_atomic_helper_duplicate_state - duplicate an atomic state object
2440 * @dev: DRM device
2441 * @ctx: lock acquisition context
2442 *
2443 * Makes a copy of the current atomic state by looping over all objects and
2444 * duplicating their respective states.
2445 *
2446 * Note that this treats atomic state as persistent between save and restore.
2447 * Drivers must make sure that this is possible and won't result in confusion
2448 * or erroneous behaviour.
2449 *
2450 * Note that if callers haven't already acquired all modeset locks this might
2451 * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
2452 *
2453 * Returns:
2454 * A pointer to the copy of the atomic state object on success or an
2455 * ERR_PTR()-encoded error code on failure.
2456 */
2457 struct drm_atomic_state *
2458 drm_atomic_helper_duplicate_state(struct drm_device *dev,
2459 struct drm_modeset_acquire_ctx *ctx)
2460 {
2461 struct drm_atomic_state *state;
2462 struct drm_connector *conn;
2463 struct drm_plane *plane;
2464 struct drm_crtc *crtc;
2465 int err = 0;
2466
2467 state = drm_atomic_state_alloc(dev);
2468 if (!state)
2469 return ERR_PTR(-ENOMEM);
2470
2471 state->acquire_ctx = ctx;
2472
2473 drm_for_each_crtc(crtc, dev) {
2474 struct drm_crtc_state *crtc_state;
2475
2476 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2477 if (IS_ERR(crtc_state)) {
2478 err = PTR_ERR(crtc_state);
2479 goto free;
2480 }
2481 }
2482
2483 drm_for_each_plane(plane, dev) {
2484 struct drm_plane_state *plane_state;
2485
2486 plane_state = drm_atomic_get_plane_state(state, plane);
2487 if (IS_ERR(plane_state)) {
2488 err = PTR_ERR(plane_state);
2489 goto free;
2490 }
2491 }
2492
2493 drm_for_each_connector(conn, dev) {
2494 struct drm_connector_state *conn_state;
2495
2496 conn_state = drm_atomic_get_connector_state(state, conn);
2497 if (IS_ERR(conn_state)) {
2498 err = PTR_ERR(conn_state);
2499 goto free;
2500 }
2501 }
2502
2503 /* clear the acquire context so that it isn't accidentally reused */
2504 state->acquire_ctx = NULL;
2505
2506 free:
2507 if (err < 0) {
2508 drm_atomic_state_free(state);
2509 state = ERR_PTR(err);
2510 }
2511
2512 return state;
2513 }
2514 EXPORT_SYMBOL(drm_atomic_helper_duplicate_state);
2515
2516 /**
2517 * __drm_atomic_helper_connector_destroy_state - release connector state
2518 * @connector: connector object
2519 * @state: connector state object to release
2520 *
2521 * Releases all resources stored in the connector state without actually
2522 * freeing the memory of the connector state. This is useful for drivers that
2523 * subclass the connector state.
2524 */
2525 void
2526 __drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
2527 struct drm_connector_state *state)
2528 {
2529 /*
2530 * This is currently a placeholder so that drivers that subclass the
2531 * state will automatically do the right thing if code is ever added
2532 * to this function.
2533 */
2534 }
2535 EXPORT_SYMBOL(__drm_atomic_helper_connector_destroy_state);
2536
2537 /**
2538 * drm_atomic_helper_connector_destroy_state - default state destroy hook
2539 * @connector: drm connector
2540 * @state: connector state object to release
2541 *
2542 * Default connector state destroy hook for drivers which don't have their own
2543 * subclassed connector state structure.
2544 */
2545 void drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
2546 struct drm_connector_state *state)
2547 {
2548 __drm_atomic_helper_connector_destroy_state(connector, state);
2549 kfree(state);
2550 }
2551 EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state);
2552