1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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_bridge.h>
35#include <drm/drm_damage_helper.h>
36#include <drm/drm_device.h>
37#include <drm/drm_drv.h>
38#include <drm/drm_plane_helper.h>
39#include <drm/drm_print.h>
40#include <drm/drm_self_refresh_helper.h>
41#include <drm/drm_vblank.h>
42#include <drm/drm_writeback.h>
43
44#include "drm_crtc_helper_internal.h"
45#include "drm_crtc_internal.h"
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74static void
75drm_atomic_helper_plane_changed(struct drm_atomic_state *state,
76 struct drm_plane_state *old_plane_state,
77 struct drm_plane_state *plane_state,
78 struct drm_plane *plane)
79{
80 struct drm_crtc_state *crtc_state;
81
82 if (old_plane_state->crtc) {
83 crtc_state = drm_atomic_get_new_crtc_state(state,
84 old_plane_state->crtc);
85
86 if (WARN_ON(!crtc_state))
87 return;
88
89 crtc_state->planes_changed = true;
90 }
91
92 if (plane_state->crtc) {
93 crtc_state = drm_atomic_get_new_crtc_state(state, plane_state->crtc);
94
95 if (WARN_ON(!crtc_state))
96 return;
97
98 crtc_state->planes_changed = true;
99 }
100}
101
102static int handle_conflicting_encoders(struct drm_atomic_state *state,
103 bool disable_conflicting_encoders)
104{
105 struct drm_connector_state *new_conn_state;
106 struct drm_connector *connector;
107 struct drm_connector_list_iter conn_iter;
108 struct drm_encoder *encoder;
109 unsigned encoder_mask = 0;
110 int i, ret = 0;
111
112
113
114
115
116
117 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
118 const struct drm_connector_helper_funcs *funcs = connector->helper_private;
119 struct drm_encoder *new_encoder;
120
121 if (!new_conn_state->crtc)
122 continue;
123
124 if (funcs->atomic_best_encoder)
125 new_encoder = funcs->atomic_best_encoder(connector,
126 state);
127 else if (funcs->best_encoder)
128 new_encoder = funcs->best_encoder(connector);
129 else
130 new_encoder = drm_connector_get_single_encoder(connector);
131
132 if (new_encoder) {
133 if (encoder_mask & drm_encoder_mask(new_encoder)) {
134 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] on [CONNECTOR:%d:%s] already assigned\n",
135 new_encoder->base.id, new_encoder->name,
136 connector->base.id, connector->name);
137
138 return -EINVAL;
139 }
140
141 encoder_mask |= drm_encoder_mask(new_encoder);
142 }
143 }
144
145 if (!encoder_mask)
146 return 0;
147
148
149
150
151
152
153
154
155
156
157
158
159 drm_connector_list_iter_begin(state->dev, &conn_iter);
160 drm_for_each_connector_iter(connector, &conn_iter) {
161 struct drm_crtc_state *crtc_state;
162
163 if (drm_atomic_get_new_connector_state(state, connector))
164 continue;
165
166 encoder = connector->state->best_encoder;
167 if (!encoder || !(encoder_mask & drm_encoder_mask(encoder)))
168 continue;
169
170 if (!disable_conflicting_encoders) {
171 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s] by [CONNECTOR:%d:%s]\n",
172 encoder->base.id, encoder->name,
173 connector->state->crtc->base.id,
174 connector->state->crtc->name,
175 connector->base.id, connector->name);
176 ret = -EINVAL;
177 goto out;
178 }
179
180 new_conn_state = drm_atomic_get_connector_state(state, connector);
181 if (IS_ERR(new_conn_state)) {
182 ret = PTR_ERR(new_conn_state);
183 goto out;
184 }
185
186 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s], disabling [CONNECTOR:%d:%s]\n",
187 encoder->base.id, encoder->name,
188 new_conn_state->crtc->base.id, new_conn_state->crtc->name,
189 connector->base.id, connector->name);
190
191 crtc_state = drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
192
193 ret = drm_atomic_set_crtc_for_connector(new_conn_state, NULL);
194 if (ret)
195 goto out;
196
197 if (!crtc_state->connector_mask) {
198 ret = drm_atomic_set_mode_prop_for_crtc(crtc_state,
199 NULL);
200 if (ret < 0)
201 goto out;
202
203 crtc_state->active = false;
204 }
205 }
206out:
207 drm_connector_list_iter_end(&conn_iter);
208
209 return ret;
210}
211
212static void
213set_best_encoder(struct drm_atomic_state *state,
214 struct drm_connector_state *conn_state,
215 struct drm_encoder *encoder)
216{
217 struct drm_crtc_state *crtc_state;
218 struct drm_crtc *crtc;
219
220 if (conn_state->best_encoder) {
221
222 crtc = conn_state->connector->state->crtc;
223
224
225
226
227
228
229
230 WARN_ON(!crtc && encoder != conn_state->best_encoder);
231 if (crtc) {
232 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
233
234 crtc_state->encoder_mask &=
235 ~drm_encoder_mask(conn_state->best_encoder);
236 }
237 }
238
239 if (encoder) {
240 crtc = conn_state->crtc;
241 WARN_ON(!crtc);
242 if (crtc) {
243 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
244
245 crtc_state->encoder_mask |=
246 drm_encoder_mask(encoder);
247 }
248 }
249
250 conn_state->best_encoder = encoder;
251}
252
253static void
254steal_encoder(struct drm_atomic_state *state,
255 struct drm_encoder *encoder)
256{
257 struct drm_crtc_state *crtc_state;
258 struct drm_connector *connector;
259 struct drm_connector_state *old_connector_state, *new_connector_state;
260 int i;
261
262 for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
263 struct drm_crtc *encoder_crtc;
264
265 if (new_connector_state->best_encoder != encoder)
266 continue;
267
268 encoder_crtc = old_connector_state->crtc;
269
270 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s], stealing it\n",
271 encoder->base.id, encoder->name,
272 encoder_crtc->base.id, encoder_crtc->name);
273
274 set_best_encoder(state, new_connector_state, NULL);
275
276 crtc_state = drm_atomic_get_new_crtc_state(state, encoder_crtc);
277 crtc_state->connectors_changed = true;
278
279 return;
280 }
281}
282
283static int
284update_connector_routing(struct drm_atomic_state *state,
285 struct drm_connector *connector,
286 struct drm_connector_state *old_connector_state,
287 struct drm_connector_state *new_connector_state)
288{
289 const struct drm_connector_helper_funcs *funcs;
290 struct drm_encoder *new_encoder;
291 struct drm_crtc_state *crtc_state;
292
293 DRM_DEBUG_ATOMIC("Updating routing for [CONNECTOR:%d:%s]\n",
294 connector->base.id,
295 connector->name);
296
297 if (old_connector_state->crtc != new_connector_state->crtc) {
298 if (old_connector_state->crtc) {
299 crtc_state = drm_atomic_get_new_crtc_state(state, old_connector_state->crtc);
300 crtc_state->connectors_changed = true;
301 }
302
303 if (new_connector_state->crtc) {
304 crtc_state = drm_atomic_get_new_crtc_state(state, new_connector_state->crtc);
305 crtc_state->connectors_changed = true;
306 }
307 }
308
309 if (!new_connector_state->crtc) {
310 DRM_DEBUG_ATOMIC("Disabling [CONNECTOR:%d:%s]\n",
311 connector->base.id,
312 connector->name);
313
314 set_best_encoder(state, new_connector_state, NULL);
315
316 return 0;
317 }
318
319 crtc_state = drm_atomic_get_new_crtc_state(state,
320 new_connector_state->crtc);
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339 if (!state->duplicated && drm_connector_is_unregistered(connector) &&
340 crtc_state->active) {
341 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] is not registered\n",
342 connector->base.id, connector->name);
343 return -EINVAL;
344 }
345
346 funcs = connector->helper_private;
347
348 if (funcs->atomic_best_encoder)
349 new_encoder = funcs->atomic_best_encoder(connector, state);
350 else if (funcs->best_encoder)
351 new_encoder = funcs->best_encoder(connector);
352 else
353 new_encoder = drm_connector_get_single_encoder(connector);
354
355 if (!new_encoder) {
356 DRM_DEBUG_ATOMIC("No suitable encoder found for [CONNECTOR:%d:%s]\n",
357 connector->base.id,
358 connector->name);
359 return -EINVAL;
360 }
361
362 if (!drm_encoder_crtc_ok(new_encoder, new_connector_state->crtc)) {
363 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] incompatible with [CRTC:%d:%s]\n",
364 new_encoder->base.id,
365 new_encoder->name,
366 new_connector_state->crtc->base.id,
367 new_connector_state->crtc->name);
368 return -EINVAL;
369 }
370
371 if (new_encoder == new_connector_state->best_encoder) {
372 set_best_encoder(state, new_connector_state, new_encoder);
373
374 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] keeps [ENCODER:%d:%s], now on [CRTC:%d:%s]\n",
375 connector->base.id,
376 connector->name,
377 new_encoder->base.id,
378 new_encoder->name,
379 new_connector_state->crtc->base.id,
380 new_connector_state->crtc->name);
381
382 return 0;
383 }
384
385 steal_encoder(state, new_encoder);
386
387 set_best_encoder(state, new_connector_state, new_encoder);
388
389 crtc_state->connectors_changed = true;
390
391 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] using [ENCODER:%d:%s] on [CRTC:%d:%s]\n",
392 connector->base.id,
393 connector->name,
394 new_encoder->base.id,
395 new_encoder->name,
396 new_connector_state->crtc->base.id,
397 new_connector_state->crtc->name);
398
399 return 0;
400}
401
402static int
403mode_fixup(struct drm_atomic_state *state)
404{
405 struct drm_crtc *crtc;
406 struct drm_crtc_state *new_crtc_state;
407 struct drm_connector *connector;
408 struct drm_connector_state *new_conn_state;
409 int i;
410 int ret;
411
412 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
413 if (!new_crtc_state->mode_changed &&
414 !new_crtc_state->connectors_changed)
415 continue;
416
417 drm_mode_copy(&new_crtc_state->adjusted_mode, &new_crtc_state->mode);
418 }
419
420 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
421 const struct drm_encoder_helper_funcs *funcs;
422 struct drm_encoder *encoder;
423 struct drm_bridge *bridge;
424
425 WARN_ON(!!new_conn_state->best_encoder != !!new_conn_state->crtc);
426
427 if (!new_conn_state->crtc || !new_conn_state->best_encoder)
428 continue;
429
430 new_crtc_state =
431 drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
432
433
434
435
436
437 encoder = new_conn_state->best_encoder;
438 funcs = encoder->helper_private;
439
440 bridge = drm_bridge_chain_get_first_bridge(encoder);
441 ret = drm_atomic_bridge_chain_check(bridge,
442 new_crtc_state,
443 new_conn_state);
444 if (ret) {
445 DRM_DEBUG_ATOMIC("Bridge atomic check failed\n");
446 return ret;
447 }
448
449 if (funcs && funcs->atomic_check) {
450 ret = funcs->atomic_check(encoder, new_crtc_state,
451 new_conn_state);
452 if (ret) {
453 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] check failed\n",
454 encoder->base.id, encoder->name);
455 return ret;
456 }
457 } else if (funcs && funcs->mode_fixup) {
458 ret = funcs->mode_fixup(encoder, &new_crtc_state->mode,
459 &new_crtc_state->adjusted_mode);
460 if (!ret) {
461 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] fixup failed\n",
462 encoder->base.id, encoder->name);
463 return -EINVAL;
464 }
465 }
466 }
467
468 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
469 const struct drm_crtc_helper_funcs *funcs;
470
471 if (!new_crtc_state->enable)
472 continue;
473
474 if (!new_crtc_state->mode_changed &&
475 !new_crtc_state->connectors_changed)
476 continue;
477
478 funcs = crtc->helper_private;
479 if (!funcs || !funcs->mode_fixup)
480 continue;
481
482 ret = funcs->mode_fixup(crtc, &new_crtc_state->mode,
483 &new_crtc_state->adjusted_mode);
484 if (!ret) {
485 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] fixup failed\n",
486 crtc->base.id, crtc->name);
487 return -EINVAL;
488 }
489 }
490
491 return 0;
492}
493
494static enum drm_mode_status mode_valid_path(struct drm_connector *connector,
495 struct drm_encoder *encoder,
496 struct drm_crtc *crtc,
497 const struct drm_display_mode *mode)
498{
499 struct drm_bridge *bridge;
500 enum drm_mode_status ret;
501
502 ret = drm_encoder_mode_valid(encoder, mode);
503 if (ret != MODE_OK) {
504 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] mode_valid() failed\n",
505 encoder->base.id, encoder->name);
506 return ret;
507 }
508
509 bridge = drm_bridge_chain_get_first_bridge(encoder);
510 ret = drm_bridge_chain_mode_valid(bridge, &connector->display_info,
511 mode);
512 if (ret != MODE_OK) {
513 DRM_DEBUG_ATOMIC("[BRIDGE] mode_valid() failed\n");
514 return ret;
515 }
516
517 ret = drm_crtc_mode_valid(crtc, mode);
518 if (ret != MODE_OK) {
519 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] mode_valid() failed\n",
520 crtc->base.id, crtc->name);
521 return ret;
522 }
523
524 return ret;
525}
526
527static int
528mode_valid(struct drm_atomic_state *state)
529{
530 struct drm_connector_state *conn_state;
531 struct drm_connector *connector;
532 int i;
533
534 for_each_new_connector_in_state(state, connector, conn_state, i) {
535 struct drm_encoder *encoder = conn_state->best_encoder;
536 struct drm_crtc *crtc = conn_state->crtc;
537 struct drm_crtc_state *crtc_state;
538 enum drm_mode_status mode_status;
539 const struct drm_display_mode *mode;
540
541 if (!crtc || !encoder)
542 continue;
543
544 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
545 if (!crtc_state)
546 continue;
547 if (!crtc_state->mode_changed && !crtc_state->connectors_changed)
548 continue;
549
550 mode = &crtc_state->mode;
551
552 mode_status = mode_valid_path(connector, encoder, crtc, mode);
553 if (mode_status != MODE_OK)
554 return -EINVAL;
555 }
556
557 return 0;
558}
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603int
604drm_atomic_helper_check_modeset(struct drm_device *dev,
605 struct drm_atomic_state *state)
606{
607 struct drm_crtc *crtc;
608 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
609 struct drm_connector *connector;
610 struct drm_connector_state *old_connector_state, *new_connector_state;
611 int i, ret;
612 unsigned connectors_mask = 0;
613
614 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
615 bool has_connectors =
616 !!new_crtc_state->connector_mask;
617
618 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
619
620 if (!drm_mode_equal(&old_crtc_state->mode, &new_crtc_state->mode)) {
621 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] mode changed\n",
622 crtc->base.id, crtc->name);
623 new_crtc_state->mode_changed = true;
624 }
625
626 if (old_crtc_state->enable != new_crtc_state->enable) {
627 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enable changed\n",
628 crtc->base.id, crtc->name);
629
630
631
632
633
634
635
636
637
638 new_crtc_state->mode_changed = true;
639 new_crtc_state->connectors_changed = true;
640 }
641
642 if (old_crtc_state->active != new_crtc_state->active) {
643 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active changed\n",
644 crtc->base.id, crtc->name);
645 new_crtc_state->active_changed = true;
646 }
647
648 if (new_crtc_state->enable != has_connectors) {
649 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled/connectors mismatch\n",
650 crtc->base.id, crtc->name);
651
652 return -EINVAL;
653 }
654
655 if (drm_dev_has_vblank(dev))
656 new_crtc_state->no_vblank = false;
657 else
658 new_crtc_state->no_vblank = true;
659 }
660
661 ret = handle_conflicting_encoders(state, false);
662 if (ret)
663 return ret;
664
665 for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
666 const struct drm_connector_helper_funcs *funcs = connector->helper_private;
667
668 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
669
670
671
672
673
674
675 ret = update_connector_routing(state, connector,
676 old_connector_state,
677 new_connector_state);
678 if (ret)
679 return ret;
680 if (old_connector_state->crtc) {
681 new_crtc_state = drm_atomic_get_new_crtc_state(state,
682 old_connector_state->crtc);
683 if (old_connector_state->link_status !=
684 new_connector_state->link_status)
685 new_crtc_state->connectors_changed = true;
686
687 if (old_connector_state->max_requested_bpc !=
688 new_connector_state->max_requested_bpc)
689 new_crtc_state->connectors_changed = true;
690 }
691
692 if (funcs->atomic_check)
693 ret = funcs->atomic_check(connector, state);
694 if (ret)
695 return ret;
696
697 connectors_mask |= BIT(i);
698 }
699
700
701
702
703
704
705
706 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
707 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
708 continue;
709
710 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] needs all connectors, enable: %c, active: %c\n",
711 crtc->base.id, crtc->name,
712 new_crtc_state->enable ? 'y' : 'n',
713 new_crtc_state->active ? 'y' : 'n');
714
715 ret = drm_atomic_add_affected_connectors(state, crtc);
716 if (ret != 0)
717 return ret;
718
719 ret = drm_atomic_add_affected_planes(state, crtc);
720 if (ret != 0)
721 return ret;
722 }
723
724
725
726
727
728 for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
729 const struct drm_connector_helper_funcs *funcs = connector->helper_private;
730
731 if (connectors_mask & BIT(i))
732 continue;
733
734 if (funcs->atomic_check)
735 ret = funcs->atomic_check(connector, state);
736 if (ret)
737 return ret;
738 }
739
740
741
742
743
744 for_each_oldnew_connector_in_state(state, connector,
745 old_connector_state,
746 new_connector_state, i) {
747 struct drm_encoder *encoder;
748
749 encoder = old_connector_state->best_encoder;
750 ret = drm_atomic_add_encoder_bridges(state, encoder);
751 if (ret)
752 return ret;
753
754 encoder = new_connector_state->best_encoder;
755 ret = drm_atomic_add_encoder_bridges(state, encoder);
756 if (ret)
757 return ret;
758 }
759
760 ret = mode_valid(state);
761 if (ret)
762 return ret;
763
764 return mode_fixup(state);
765}
766EXPORT_SYMBOL(drm_atomic_helper_check_modeset);
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789int drm_atomic_helper_check_plane_state(struct drm_plane_state *plane_state,
790 const struct drm_crtc_state *crtc_state,
791 int min_scale,
792 int max_scale,
793 bool can_position,
794 bool can_update_disabled)
795{
796 struct drm_framebuffer *fb = plane_state->fb;
797 struct drm_rect *src = &plane_state->src;
798 struct drm_rect *dst = &plane_state->dst;
799 unsigned int rotation = plane_state->rotation;
800 struct drm_rect clip = {};
801 int hscale, vscale;
802
803 WARN_ON(plane_state->crtc && plane_state->crtc != crtc_state->crtc);
804
805 *src = drm_plane_state_src(plane_state);
806 *dst = drm_plane_state_dest(plane_state);
807
808 if (!fb) {
809 plane_state->visible = false;
810 return 0;
811 }
812
813
814 if (WARN_ON(!plane_state->crtc)) {
815 plane_state->visible = false;
816 return 0;
817 }
818
819 if (!crtc_state->enable && !can_update_disabled) {
820 DRM_DEBUG_KMS("Cannot update plane of a disabled CRTC.\n");
821 return -EINVAL;
822 }
823
824 drm_rect_rotate(src, fb->width << 16, fb->height << 16, rotation);
825
826
827 hscale = drm_rect_calc_hscale(src, dst, min_scale, max_scale);
828 vscale = drm_rect_calc_vscale(src, dst, min_scale, max_scale);
829 if (hscale < 0 || vscale < 0) {
830 DRM_DEBUG_KMS("Invalid scaling of plane\n");
831 drm_rect_debug_print("src: ", &plane_state->src, true);
832 drm_rect_debug_print("dst: ", &plane_state->dst, false);
833 return -ERANGE;
834 }
835
836 if (crtc_state->enable)
837 drm_mode_get_hv_timing(&crtc_state->mode, &clip.x2, &clip.y2);
838
839 plane_state->visible = drm_rect_clip_scaled(src, dst, &clip);
840
841 drm_rect_rotate_inv(src, fb->width << 16, fb->height << 16, rotation);
842
843 if (!plane_state->visible)
844
845
846
847
848
849
850
851 return 0;
852
853 if (!can_position && !drm_rect_equals(dst, &clip)) {
854 DRM_DEBUG_KMS("Plane must cover entire CRTC\n");
855 drm_rect_debug_print("dst: ", dst, false);
856 drm_rect_debug_print("clip: ", &clip, false);
857 return -EINVAL;
858 }
859
860 return 0;
861}
862EXPORT_SYMBOL(drm_atomic_helper_check_plane_state);
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880int
881drm_atomic_helper_check_planes(struct drm_device *dev,
882 struct drm_atomic_state *state)
883{
884 struct drm_crtc *crtc;
885 struct drm_crtc_state *new_crtc_state;
886 struct drm_plane *plane;
887 struct drm_plane_state *new_plane_state, *old_plane_state;
888 int i, ret = 0;
889
890 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
891 const struct drm_plane_helper_funcs *funcs;
892
893 WARN_ON(!drm_modeset_is_locked(&plane->mutex));
894
895 funcs = plane->helper_private;
896
897 drm_atomic_helper_plane_changed(state, old_plane_state, new_plane_state, plane);
898
899 drm_atomic_helper_check_plane_damage(state, new_plane_state);
900
901 if (!funcs || !funcs->atomic_check)
902 continue;
903
904 ret = funcs->atomic_check(plane, state);
905 if (ret) {
906 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic driver check failed\n",
907 plane->base.id, plane->name);
908 return ret;
909 }
910 }
911
912 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
913 const struct drm_crtc_helper_funcs *funcs;
914
915 funcs = crtc->helper_private;
916
917 if (!funcs || !funcs->atomic_check)
918 continue;
919
920 ret = funcs->atomic_check(crtc, state);
921 if (ret) {
922 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic driver check failed\n",
923 crtc->base.id, crtc->name);
924 return ret;
925 }
926 }
927
928 return ret;
929}
930EXPORT_SYMBOL(drm_atomic_helper_check_planes);
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958int drm_atomic_helper_check(struct drm_device *dev,
959 struct drm_atomic_state *state)
960{
961 int ret;
962
963 ret = drm_atomic_helper_check_modeset(dev, state);
964 if (ret)
965 return ret;
966
967 if (dev->mode_config.normalize_zpos) {
968 ret = drm_atomic_normalize_zpos(dev, state);
969 if (ret)
970 return ret;
971 }
972
973 ret = drm_atomic_helper_check_planes(dev, state);
974 if (ret)
975 return ret;
976
977 if (state->legacy_cursor_update)
978 state->async_update = !drm_atomic_helper_async_check(dev, state);
979
980 drm_self_refresh_helper_alter_state(state);
981
982 return ret;
983}
984EXPORT_SYMBOL(drm_atomic_helper_check);
985
986static bool
987crtc_needs_disable(struct drm_crtc_state *old_state,
988 struct drm_crtc_state *new_state)
989{
990
991
992
993
994 if (!new_state)
995 return drm_atomic_crtc_effectively_active(old_state);
996
997
998
999
1000
1001
1002 return old_state->active ||
1003 (old_state->self_refresh_active && !new_state->enable) ||
1004 new_state->self_refresh_active;
1005}
1006
1007static void
1008disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state)
1009{
1010 struct drm_connector *connector;
1011 struct drm_connector_state *old_conn_state, *new_conn_state;
1012 struct drm_crtc *crtc;
1013 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
1014 int i;
1015
1016 for_each_oldnew_connector_in_state(old_state, connector, old_conn_state, new_conn_state, i) {
1017 const struct drm_encoder_helper_funcs *funcs;
1018 struct drm_encoder *encoder;
1019 struct drm_bridge *bridge;
1020
1021
1022
1023 if (!old_conn_state->crtc)
1024 continue;
1025
1026 old_crtc_state = drm_atomic_get_old_crtc_state(old_state, old_conn_state->crtc);
1027
1028 if (new_conn_state->crtc)
1029 new_crtc_state = drm_atomic_get_new_crtc_state(
1030 old_state,
1031 new_conn_state->crtc);
1032 else
1033 new_crtc_state = NULL;
1034
1035 if (!crtc_needs_disable(old_crtc_state, new_crtc_state) ||
1036 !drm_atomic_crtc_needs_modeset(old_conn_state->crtc->state))
1037 continue;
1038
1039 encoder = old_conn_state->best_encoder;
1040
1041
1042
1043
1044 if (WARN_ON(!encoder))
1045 continue;
1046
1047 funcs = encoder->helper_private;
1048
1049 DRM_DEBUG_ATOMIC("disabling [ENCODER:%d:%s]\n",
1050 encoder->base.id, encoder->name);
1051
1052
1053
1054
1055
1056 bridge = drm_bridge_chain_get_first_bridge(encoder);
1057 drm_atomic_bridge_chain_disable(bridge, old_state);
1058
1059
1060 if (funcs) {
1061 if (funcs->atomic_disable)
1062 funcs->atomic_disable(encoder, old_state);
1063 else if (new_conn_state->crtc && funcs->prepare)
1064 funcs->prepare(encoder);
1065 else if (funcs->disable)
1066 funcs->disable(encoder);
1067 else if (funcs->dpms)
1068 funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
1069 }
1070
1071 drm_atomic_bridge_chain_post_disable(bridge, old_state);
1072 }
1073
1074 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
1075 const struct drm_crtc_helper_funcs *funcs;
1076 int ret;
1077
1078
1079 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
1080 continue;
1081
1082 if (!crtc_needs_disable(old_crtc_state, new_crtc_state))
1083 continue;
1084
1085 funcs = crtc->helper_private;
1086
1087 DRM_DEBUG_ATOMIC("disabling [CRTC:%d:%s]\n",
1088 crtc->base.id, crtc->name);
1089
1090
1091
1092 if (new_crtc_state->enable && funcs->prepare)
1093 funcs->prepare(crtc);
1094 else if (funcs->atomic_disable)
1095 funcs->atomic_disable(crtc, old_state);
1096 else if (funcs->disable)
1097 funcs->disable(crtc);
1098 else if (funcs->dpms)
1099 funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
1100
1101 if (!drm_dev_has_vblank(dev))
1102 continue;
1103
1104 ret = drm_crtc_vblank_get(crtc);
1105 WARN_ONCE(ret != -EINVAL, "driver forgot to call drm_crtc_vblank_off()\n");
1106 if (ret == 0)
1107 drm_crtc_vblank_put(crtc);
1108 }
1109}
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128void
1129drm_atomic_helper_update_legacy_modeset_state(struct drm_device *dev,
1130 struct drm_atomic_state *old_state)
1131{
1132 struct drm_connector *connector;
1133 struct drm_connector_state *old_conn_state, *new_conn_state;
1134 struct drm_crtc *crtc;
1135 struct drm_crtc_state *new_crtc_state;
1136 int i;
1137
1138
1139 for_each_oldnew_connector_in_state(old_state, connector, old_conn_state, new_conn_state, i) {
1140 if (connector->encoder) {
1141 WARN_ON(!connector->encoder->crtc);
1142
1143 connector->encoder->crtc = NULL;
1144 connector->encoder = NULL;
1145 }
1146
1147 crtc = new_conn_state->crtc;
1148 if ((!crtc && old_conn_state->crtc) ||
1149 (crtc && drm_atomic_crtc_needs_modeset(crtc->state))) {
1150 int mode = DRM_MODE_DPMS_OFF;
1151
1152 if (crtc && crtc->state->active)
1153 mode = DRM_MODE_DPMS_ON;
1154
1155 connector->dpms = mode;
1156 }
1157 }
1158
1159
1160 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
1161 if (!new_conn_state->crtc)
1162 continue;
1163
1164 if (WARN_ON(!new_conn_state->best_encoder))
1165 continue;
1166
1167 connector->encoder = new_conn_state->best_encoder;
1168 connector->encoder->crtc = new_conn_state->crtc;
1169 }
1170
1171
1172 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
1173 struct drm_plane *primary = crtc->primary;
1174 struct drm_plane_state *new_plane_state;
1175
1176 crtc->mode = new_crtc_state->mode;
1177 crtc->enabled = new_crtc_state->enable;
1178
1179 new_plane_state =
1180 drm_atomic_get_new_plane_state(old_state, primary);
1181
1182 if (new_plane_state && new_plane_state->crtc == crtc) {
1183 crtc->x = new_plane_state->src_x >> 16;
1184 crtc->y = new_plane_state->src_y >> 16;
1185 }
1186 }
1187}
1188EXPORT_SYMBOL(drm_atomic_helper_update_legacy_modeset_state);
1189
1190
1191
1192
1193
1194
1195
1196
1197void drm_atomic_helper_calc_timestamping_constants(struct drm_atomic_state *state)
1198{
1199 struct drm_crtc_state *new_crtc_state;
1200 struct drm_crtc *crtc;
1201 int i;
1202
1203 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
1204 if (new_crtc_state->enable)
1205 drm_calc_timestamping_constants(crtc,
1206 &new_crtc_state->adjusted_mode);
1207 }
1208}
1209EXPORT_SYMBOL(drm_atomic_helper_calc_timestamping_constants);
1210
1211static void
1212crtc_set_mode(struct drm_device *dev, struct drm_atomic_state *old_state)
1213{
1214 struct drm_crtc *crtc;
1215 struct drm_crtc_state *new_crtc_state;
1216 struct drm_connector *connector;
1217 struct drm_connector_state *new_conn_state;
1218 int i;
1219
1220 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
1221 const struct drm_crtc_helper_funcs *funcs;
1222
1223 if (!new_crtc_state->mode_changed)
1224 continue;
1225
1226 funcs = crtc->helper_private;
1227
1228 if (new_crtc_state->enable && funcs->mode_set_nofb) {
1229 DRM_DEBUG_ATOMIC("modeset on [CRTC:%d:%s]\n",
1230 crtc->base.id, crtc->name);
1231
1232 funcs->mode_set_nofb(crtc);
1233 }
1234 }
1235
1236 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
1237 const struct drm_encoder_helper_funcs *funcs;
1238 struct drm_encoder *encoder;
1239 struct drm_display_mode *mode, *adjusted_mode;
1240 struct drm_bridge *bridge;
1241
1242 if (!new_conn_state->best_encoder)
1243 continue;
1244
1245 encoder = new_conn_state->best_encoder;
1246 funcs = encoder->helper_private;
1247 new_crtc_state = new_conn_state->crtc->state;
1248 mode = &new_crtc_state->mode;
1249 adjusted_mode = &new_crtc_state->adjusted_mode;
1250
1251 if (!new_crtc_state->mode_changed)
1252 continue;
1253
1254 DRM_DEBUG_ATOMIC("modeset on [ENCODER:%d:%s]\n",
1255 encoder->base.id, encoder->name);
1256
1257
1258
1259
1260
1261 if (funcs && funcs->atomic_mode_set) {
1262 funcs->atomic_mode_set(encoder, new_crtc_state,
1263 new_conn_state);
1264 } else if (funcs && funcs->mode_set) {
1265 funcs->mode_set(encoder, mode, adjusted_mode);
1266 }
1267
1268 bridge = drm_bridge_chain_get_first_bridge(encoder);
1269 drm_bridge_chain_mode_set(bridge, mode, adjusted_mode);
1270 }
1271}
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287void drm_atomic_helper_commit_modeset_disables(struct drm_device *dev,
1288 struct drm_atomic_state *old_state)
1289{
1290 disable_outputs(dev, old_state);
1291
1292 drm_atomic_helper_update_legacy_modeset_state(dev, old_state);
1293 drm_atomic_helper_calc_timestamping_constants(old_state);
1294
1295 crtc_set_mode(dev, old_state);
1296}
1297EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_disables);
1298
1299static void drm_atomic_helper_commit_writebacks(struct drm_device *dev,
1300 struct drm_atomic_state *old_state)
1301{
1302 struct drm_connector *connector;
1303 struct drm_connector_state *new_conn_state;
1304 int i;
1305
1306 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
1307 const struct drm_connector_helper_funcs *funcs;
1308
1309 funcs = connector->helper_private;
1310 if (!funcs->atomic_commit)
1311 continue;
1312
1313 if (new_conn_state->writeback_job && new_conn_state->writeback_job->fb) {
1314 WARN_ON(connector->connector_type != DRM_MODE_CONNECTOR_WRITEBACK);
1315 funcs->atomic_commit(connector, old_state);
1316 }
1317 }
1318}
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334void drm_atomic_helper_commit_modeset_enables(struct drm_device *dev,
1335 struct drm_atomic_state *old_state)
1336{
1337 struct drm_crtc *crtc;
1338 struct drm_crtc_state *old_crtc_state;
1339 struct drm_crtc_state *new_crtc_state;
1340 struct drm_connector *connector;
1341 struct drm_connector_state *new_conn_state;
1342 int i;
1343
1344 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
1345 const struct drm_crtc_helper_funcs *funcs;
1346
1347
1348 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
1349 continue;
1350
1351 if (!new_crtc_state->active)
1352 continue;
1353
1354 funcs = crtc->helper_private;
1355
1356 if (new_crtc_state->enable) {
1357 DRM_DEBUG_ATOMIC("enabling [CRTC:%d:%s]\n",
1358 crtc->base.id, crtc->name);
1359 if (funcs->atomic_enable)
1360 funcs->atomic_enable(crtc, old_state);
1361 else if (funcs->commit)
1362 funcs->commit(crtc);
1363 }
1364 }
1365
1366 for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
1367 const struct drm_encoder_helper_funcs *funcs;
1368 struct drm_encoder *encoder;
1369 struct drm_bridge *bridge;
1370
1371 if (!new_conn_state->best_encoder)
1372 continue;
1373
1374 if (!new_conn_state->crtc->state->active ||
1375 !drm_atomic_crtc_needs_modeset(new_conn_state->crtc->state))
1376 continue;
1377
1378 encoder = new_conn_state->best_encoder;
1379 funcs = encoder->helper_private;
1380
1381 DRM_DEBUG_ATOMIC("enabling [ENCODER:%d:%s]\n",
1382 encoder->base.id, encoder->name);
1383
1384
1385
1386
1387
1388 bridge = drm_bridge_chain_get_first_bridge(encoder);
1389 drm_atomic_bridge_chain_pre_enable(bridge, old_state);
1390
1391 if (funcs) {
1392 if (funcs->atomic_enable)
1393 funcs->atomic_enable(encoder, old_state);
1394 else if (funcs->enable)
1395 funcs->enable(encoder);
1396 else if (funcs->commit)
1397 funcs->commit(encoder);
1398 }
1399
1400 drm_atomic_bridge_chain_enable(bridge, old_state);
1401 }
1402
1403 drm_atomic_helper_commit_writebacks(dev, old_state);
1404}
1405EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_enables);
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428int drm_atomic_helper_wait_for_fences(struct drm_device *dev,
1429 struct drm_atomic_state *state,
1430 bool pre_swap)
1431{
1432 struct drm_plane *plane;
1433 struct drm_plane_state *new_plane_state;
1434 int i, ret;
1435
1436 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
1437 if (!new_plane_state->fence)
1438 continue;
1439
1440 WARN_ON(!new_plane_state->fb);
1441
1442
1443
1444
1445
1446
1447 ret = dma_fence_wait(new_plane_state->fence, pre_swap);
1448 if (ret)
1449 return ret;
1450
1451 dma_fence_put(new_plane_state->fence);
1452 new_plane_state->fence = NULL;
1453 }
1454
1455 return 0;
1456}
1457EXPORT_SYMBOL(drm_atomic_helper_wait_for_fences);
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474void
1475drm_atomic_helper_wait_for_vblanks(struct drm_device *dev,
1476 struct drm_atomic_state *old_state)
1477{
1478 struct drm_crtc *crtc;
1479 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
1480 int i, ret;
1481 unsigned crtc_mask = 0;
1482
1483
1484
1485
1486
1487 if (old_state->legacy_cursor_update)
1488 return;
1489
1490 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
1491 if (!new_crtc_state->active)
1492 continue;
1493
1494 ret = drm_crtc_vblank_get(crtc);
1495 if (ret != 0)
1496 continue;
1497
1498 crtc_mask |= drm_crtc_mask(crtc);
1499 old_state->crtcs[i].last_vblank_count = drm_crtc_vblank_count(crtc);
1500 }
1501
1502 for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
1503 if (!(crtc_mask & drm_crtc_mask(crtc)))
1504 continue;
1505
1506 ret = wait_event_timeout(dev->vblank[i].queue,
1507 old_state->crtcs[i].last_vblank_count !=
1508 drm_crtc_vblank_count(crtc),
1509 msecs_to_jiffies(100));
1510
1511 WARN(!ret, "[CRTC:%d:%s] vblank wait timed out\n",
1512 crtc->base.id, crtc->name);
1513
1514 drm_crtc_vblank_put(crtc);
1515 }
1516}
1517EXPORT_SYMBOL(drm_atomic_helper_wait_for_vblanks);
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534void drm_atomic_helper_wait_for_flip_done(struct drm_device *dev,
1535 struct drm_atomic_state *old_state)
1536{
1537 struct drm_crtc *crtc;
1538 int i;
1539
1540 for (i = 0; i < dev->mode_config.num_crtc; i++) {
1541 struct drm_crtc_commit *commit = old_state->crtcs[i].commit;
1542 int ret;
1543
1544 crtc = old_state->crtcs[i].ptr;
1545
1546 if (!crtc || !commit)
1547 continue;
1548
1549 ret = wait_for_completion_timeout(&commit->flip_done, 10 * HZ);
1550 if (ret == 0)
1551 DRM_ERROR("[CRTC:%d:%s] flip_done timed out\n",
1552 crtc->base.id, crtc->name);
1553 }
1554
1555 if (old_state->fake_commit)
1556 complete_all(&old_state->fake_commit->flip_done);
1557}
1558EXPORT_SYMBOL(drm_atomic_helper_wait_for_flip_done);
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573void drm_atomic_helper_commit_tail(struct drm_atomic_state *old_state)
1574{
1575 struct drm_device *dev = old_state->dev;
1576
1577 drm_atomic_helper_commit_modeset_disables(dev, old_state);
1578
1579 drm_atomic_helper_commit_planes(dev, old_state, 0);
1580
1581 drm_atomic_helper_commit_modeset_enables(dev, old_state);
1582
1583 drm_atomic_helper_fake_vblank(old_state);
1584
1585 drm_atomic_helper_commit_hw_done(old_state);
1586
1587 drm_atomic_helper_wait_for_vblanks(dev, old_state);
1588
1589 drm_atomic_helper_cleanup_planes(dev, old_state);
1590}
1591EXPORT_SYMBOL(drm_atomic_helper_commit_tail);
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603void drm_atomic_helper_commit_tail_rpm(struct drm_atomic_state *old_state)
1604{
1605 struct drm_device *dev = old_state->dev;
1606
1607 drm_atomic_helper_commit_modeset_disables(dev, old_state);
1608
1609 drm_atomic_helper_commit_modeset_enables(dev, old_state);
1610
1611 drm_atomic_helper_commit_planes(dev, old_state,
1612 DRM_PLANE_COMMIT_ACTIVE_ONLY);
1613
1614 drm_atomic_helper_fake_vblank(old_state);
1615
1616 drm_atomic_helper_commit_hw_done(old_state);
1617
1618 drm_atomic_helper_wait_for_vblanks(dev, old_state);
1619
1620 drm_atomic_helper_cleanup_planes(dev, old_state);
1621}
1622EXPORT_SYMBOL(drm_atomic_helper_commit_tail_rpm);
1623
1624static void commit_tail(struct drm_atomic_state *old_state)
1625{
1626 struct drm_device *dev = old_state->dev;
1627 const struct drm_mode_config_helper_funcs *funcs;
1628 struct drm_crtc_state *new_crtc_state;
1629 struct drm_crtc *crtc;
1630 ktime_t start;
1631 s64 commit_time_ms;
1632 unsigned int i, new_self_refresh_mask = 0;
1633
1634 funcs = dev->mode_config.helper_private;
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646 start = ktime_get();
1647
1648 drm_atomic_helper_wait_for_fences(dev, old_state, false);
1649
1650 drm_atomic_helper_wait_for_dependencies(old_state);
1651
1652
1653
1654
1655
1656
1657 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i)
1658 if (new_crtc_state->self_refresh_active)
1659 new_self_refresh_mask |= BIT(i);
1660
1661 if (funcs && funcs->atomic_commit_tail)
1662 funcs->atomic_commit_tail(old_state);
1663 else
1664 drm_atomic_helper_commit_tail(old_state);
1665
1666 commit_time_ms = ktime_ms_delta(ktime_get(), start);
1667 if (commit_time_ms > 0)
1668 drm_self_refresh_helper_update_avg_times(old_state,
1669 (unsigned long)commit_time_ms,
1670 new_self_refresh_mask);
1671
1672 drm_atomic_helper_commit_cleanup_done(old_state);
1673
1674 drm_atomic_state_put(old_state);
1675}
1676
1677static void commit_work(struct work_struct *work)
1678{
1679 struct drm_atomic_state *state = container_of(work,
1680 struct drm_atomic_state,
1681 commit_work);
1682 commit_tail(state);
1683}
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698int drm_atomic_helper_async_check(struct drm_device *dev,
1699 struct drm_atomic_state *state)
1700{
1701 struct drm_crtc *crtc;
1702 struct drm_crtc_state *crtc_state;
1703 struct drm_plane *plane = NULL;
1704 struct drm_plane_state *old_plane_state = NULL;
1705 struct drm_plane_state *new_plane_state = NULL;
1706 const struct drm_plane_helper_funcs *funcs;
1707 int i, n_planes = 0;
1708
1709 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
1710 if (drm_atomic_crtc_needs_modeset(crtc_state))
1711 return -EINVAL;
1712 }
1713
1714 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i)
1715 n_planes++;
1716
1717
1718 if (n_planes != 1)
1719 return -EINVAL;
1720
1721 if (!new_plane_state->crtc ||
1722 old_plane_state->crtc != new_plane_state->crtc)
1723 return -EINVAL;
1724
1725 funcs = plane->helper_private;
1726 if (!funcs->atomic_async_update)
1727 return -EINVAL;
1728
1729 if (new_plane_state->fence)
1730 return -EINVAL;
1731
1732
1733
1734
1735
1736
1737 if (old_plane_state->commit &&
1738 !try_wait_for_completion(&old_plane_state->commit->hw_done)) {
1739 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] inflight previous commit preventing async commit\n",
1740 plane->base.id, plane->name);
1741 return -EBUSY;
1742 }
1743
1744 return funcs->atomic_async_check(plane, state);
1745}
1746EXPORT_SYMBOL(drm_atomic_helper_async_check);
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761void drm_atomic_helper_async_commit(struct drm_device *dev,
1762 struct drm_atomic_state *state)
1763{
1764 struct drm_plane *plane;
1765 struct drm_plane_state *plane_state;
1766 const struct drm_plane_helper_funcs *funcs;
1767 int i;
1768
1769 for_each_new_plane_in_state(state, plane, plane_state, i) {
1770 struct drm_framebuffer *new_fb = plane_state->fb;
1771 struct drm_framebuffer *old_fb = plane->state->fb;
1772
1773 funcs = plane->helper_private;
1774 funcs->atomic_async_update(plane, state);
1775
1776
1777
1778
1779
1780
1781 WARN_ON_ONCE(plane->state->fb != new_fb);
1782 WARN_ON_ONCE(plane->state->crtc_x != plane_state->crtc_x);
1783 WARN_ON_ONCE(plane->state->crtc_y != plane_state->crtc_y);
1784 WARN_ON_ONCE(plane->state->src_x != plane_state->src_x);
1785 WARN_ON_ONCE(plane->state->src_y != plane_state->src_y);
1786
1787
1788
1789
1790
1791 WARN_ON_ONCE(plane_state->fb != old_fb);
1792 }
1793}
1794EXPORT_SYMBOL(drm_atomic_helper_async_commit);
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814int drm_atomic_helper_commit(struct drm_device *dev,
1815 struct drm_atomic_state *state,
1816 bool nonblock)
1817{
1818 int ret;
1819
1820 if (state->async_update) {
1821 ret = drm_atomic_helper_prepare_planes(dev, state);
1822 if (ret)
1823 return ret;
1824
1825 drm_atomic_helper_async_commit(dev, state);
1826 drm_atomic_helper_cleanup_planes(dev, state);
1827
1828 return 0;
1829 }
1830
1831 ret = drm_atomic_helper_setup_commit(state, nonblock);
1832 if (ret)
1833 return ret;
1834
1835 INIT_WORK(&state->commit_work, commit_work);
1836
1837 ret = drm_atomic_helper_prepare_planes(dev, state);
1838 if (ret)
1839 return ret;
1840
1841 if (!nonblock) {
1842 ret = drm_atomic_helper_wait_for_fences(dev, state, true);
1843 if (ret)
1844 goto err;
1845 }
1846
1847
1848
1849
1850
1851
1852
1853 ret = drm_atomic_helper_swap_state(state, true);
1854 if (ret)
1855 goto err;
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877 drm_atomic_state_get(state);
1878 if (nonblock)
1879 queue_work(system_unbound_wq, &state->commit_work);
1880 else
1881 commit_tail(state);
1882
1883 return 0;
1884
1885err:
1886 drm_atomic_helper_cleanup_planes(dev, state);
1887 return ret;
1888}
1889EXPORT_SYMBOL(drm_atomic_helper_commit);
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944static int stall_checks(struct drm_crtc *crtc, bool nonblock)
1945{
1946 struct drm_crtc_commit *commit, *stall_commit = NULL;
1947 bool completed = true;
1948 int i;
1949 long ret = 0;
1950
1951 spin_lock(&crtc->commit_lock);
1952 i = 0;
1953 list_for_each_entry(commit, &crtc->commit_list, commit_entry) {
1954 if (i == 0) {
1955 completed = try_wait_for_completion(&commit->flip_done);
1956
1957
1958 if (!completed && nonblock) {
1959 spin_unlock(&crtc->commit_lock);
1960 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] busy with a previous commit\n",
1961 crtc->base.id, crtc->name);
1962
1963 return -EBUSY;
1964 }
1965 } else if (i == 1) {
1966 stall_commit = drm_crtc_commit_get(commit);
1967 break;
1968 }
1969
1970 i++;
1971 }
1972 spin_unlock(&crtc->commit_lock);
1973
1974 if (!stall_commit)
1975 return 0;
1976
1977
1978
1979
1980 ret = wait_for_completion_interruptible_timeout(&stall_commit->cleanup_done,
1981 10*HZ);
1982 if (ret == 0)
1983 DRM_ERROR("[CRTC:%d:%s] cleanup_done timed out\n",
1984 crtc->base.id, crtc->name);
1985
1986 drm_crtc_commit_put(stall_commit);
1987
1988 return ret < 0 ? ret : 0;
1989}
1990
1991static void release_crtc_commit(struct completion *completion)
1992{
1993 struct drm_crtc_commit *commit = container_of(completion,
1994 typeof(*commit),
1995 flip_done);
1996
1997 drm_crtc_commit_put(commit);
1998}
1999
2000static void init_commit(struct drm_crtc_commit *commit, struct drm_crtc *crtc)
2001{
2002 init_completion(&commit->flip_done);
2003 init_completion(&commit->hw_done);
2004 init_completion(&commit->cleanup_done);
2005 INIT_LIST_HEAD(&commit->commit_entry);
2006 kref_init(&commit->ref);
2007 commit->crtc = crtc;
2008}
2009
2010static struct drm_crtc_commit *
2011crtc_or_fake_commit(struct drm_atomic_state *state, struct drm_crtc *crtc)
2012{
2013 if (crtc) {
2014 struct drm_crtc_state *new_crtc_state;
2015
2016 new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
2017
2018 return new_crtc_state->commit;
2019 }
2020
2021 if (!state->fake_commit) {
2022 state->fake_commit = kzalloc(sizeof(*state->fake_commit), GFP_KERNEL);
2023 if (!state->fake_commit)
2024 return NULL;
2025
2026 init_commit(state->fake_commit, NULL);
2027 }
2028
2029 return state->fake_commit;
2030}
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078int drm_atomic_helper_setup_commit(struct drm_atomic_state *state,
2079 bool nonblock)
2080{
2081 struct drm_crtc *crtc;
2082 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
2083 struct drm_connector *conn;
2084 struct drm_connector_state *old_conn_state, *new_conn_state;
2085 struct drm_plane *plane;
2086 struct drm_plane_state *old_plane_state, *new_plane_state;
2087 struct drm_crtc_commit *commit;
2088 const struct drm_mode_config_helper_funcs *funcs;
2089 int i, ret;
2090
2091 funcs = state->dev->mode_config.helper_private;
2092
2093 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
2094 commit = kzalloc(sizeof(*commit), GFP_KERNEL);
2095 if (!commit)
2096 return -ENOMEM;
2097
2098 init_commit(commit, crtc);
2099
2100 new_crtc_state->commit = commit;
2101
2102 ret = stall_checks(crtc, nonblock);
2103 if (ret)
2104 return ret;
2105
2106
2107
2108
2109 if (!old_crtc_state->active && !new_crtc_state->active) {
2110 complete_all(&commit->flip_done);
2111 continue;
2112 }
2113
2114
2115 if (state->legacy_cursor_update) {
2116 complete_all(&commit->flip_done);
2117 continue;
2118 }
2119
2120 if (!new_crtc_state->event) {
2121 commit->event = kzalloc(sizeof(*commit->event),
2122 GFP_KERNEL);
2123 if (!commit->event)
2124 return -ENOMEM;
2125
2126 new_crtc_state->event = commit->event;
2127 }
2128
2129 new_crtc_state->event->base.completion = &commit->flip_done;
2130 new_crtc_state->event->base.completion_release = release_crtc_commit;
2131 drm_crtc_commit_get(commit);
2132
2133 commit->abort_completion = true;
2134
2135 state->crtcs[i].commit = commit;
2136 drm_crtc_commit_get(commit);
2137 }
2138
2139 for_each_oldnew_connector_in_state(state, conn, old_conn_state, new_conn_state, i) {
2140
2141
2142 if (nonblock && old_conn_state->commit &&
2143 !try_wait_for_completion(&old_conn_state->commit->flip_done)) {
2144 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] busy with a previous commit\n",
2145 conn->base.id, conn->name);
2146
2147 return -EBUSY;
2148 }
2149
2150
2151 commit = crtc_or_fake_commit(state, new_conn_state->crtc ?: old_conn_state->crtc);
2152 if (!commit)
2153 return -ENOMEM;
2154
2155 new_conn_state->commit = drm_crtc_commit_get(commit);
2156 }
2157
2158 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
2159
2160
2161 if (nonblock && old_plane_state->commit &&
2162 !try_wait_for_completion(&old_plane_state->commit->flip_done)) {
2163 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] busy with a previous commit\n",
2164 plane->base.id, plane->name);
2165
2166 return -EBUSY;
2167 }
2168
2169
2170 commit = crtc_or_fake_commit(state, new_plane_state->crtc ?: old_plane_state->crtc);
2171 if (!commit)
2172 return -ENOMEM;
2173
2174 new_plane_state->commit = drm_crtc_commit_get(commit);
2175 }
2176
2177 if (funcs && funcs->atomic_commit_setup)
2178 return funcs->atomic_commit_setup(state);
2179
2180 return 0;
2181}
2182EXPORT_SYMBOL(drm_atomic_helper_setup_commit);
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196void drm_atomic_helper_wait_for_dependencies(struct drm_atomic_state *old_state)
2197{
2198 struct drm_crtc *crtc;
2199 struct drm_crtc_state *old_crtc_state;
2200 struct drm_plane *plane;
2201 struct drm_plane_state *old_plane_state;
2202 struct drm_connector *conn;
2203 struct drm_connector_state *old_conn_state;
2204 int i;
2205 long ret;
2206
2207 for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
2208 ret = drm_crtc_commit_wait(old_crtc_state->commit);
2209 if (ret)
2210 DRM_ERROR("[CRTC:%d:%s] commit wait timed out\n",
2211 crtc->base.id, crtc->name);
2212 }
2213
2214 for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
2215 ret = drm_crtc_commit_wait(old_conn_state->commit);
2216 if (ret)
2217 DRM_ERROR("[CONNECTOR:%d:%s] commit wait timed out\n",
2218 conn->base.id, conn->name);
2219 }
2220
2221 for_each_old_plane_in_state(old_state, plane, old_plane_state, i) {
2222 ret = drm_crtc_commit_wait(old_plane_state->commit);
2223 if (ret)
2224 DRM_ERROR("[PLANE:%d:%s] commit wait timed out\n",
2225 plane->base.id, plane->name);
2226 }
2227}
2228EXPORT_SYMBOL(drm_atomic_helper_wait_for_dependencies);
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248void drm_atomic_helper_fake_vblank(struct drm_atomic_state *old_state)
2249{
2250 struct drm_crtc_state *new_crtc_state;
2251 struct drm_crtc *crtc;
2252 int i;
2253
2254 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
2255 unsigned long flags;
2256
2257 if (!new_crtc_state->no_vblank)
2258 continue;
2259
2260 spin_lock_irqsave(&old_state->dev->event_lock, flags);
2261 if (new_crtc_state->event) {
2262 drm_crtc_send_vblank_event(crtc,
2263 new_crtc_state->event);
2264 new_crtc_state->event = NULL;
2265 }
2266 spin_unlock_irqrestore(&old_state->dev->event_lock, flags);
2267 }
2268}
2269EXPORT_SYMBOL(drm_atomic_helper_fake_vblank);
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286void drm_atomic_helper_commit_hw_done(struct drm_atomic_state *old_state)
2287{
2288 struct drm_crtc *crtc;
2289 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
2290 struct drm_crtc_commit *commit;
2291 int i;
2292
2293 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
2294 commit = new_crtc_state->commit;
2295 if (!commit)
2296 continue;
2297
2298
2299
2300
2301
2302
2303 if (old_crtc_state->commit)
2304 drm_crtc_commit_put(old_crtc_state->commit);
2305
2306 old_crtc_state->commit = drm_crtc_commit_get(commit);
2307
2308
2309 WARN_ON(new_crtc_state->event);
2310 complete_all(&commit->hw_done);
2311 }
2312
2313 if (old_state->fake_commit) {
2314 complete_all(&old_state->fake_commit->hw_done);
2315 complete_all(&old_state->fake_commit->flip_done);
2316 }
2317}
2318EXPORT_SYMBOL(drm_atomic_helper_commit_hw_done);
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331void drm_atomic_helper_commit_cleanup_done(struct drm_atomic_state *old_state)
2332{
2333 struct drm_crtc *crtc;
2334 struct drm_crtc_state *old_crtc_state;
2335 struct drm_crtc_commit *commit;
2336 int i;
2337
2338 for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
2339 commit = old_crtc_state->commit;
2340 if (WARN_ON(!commit))
2341 continue;
2342
2343 complete_all(&commit->cleanup_done);
2344 WARN_ON(!try_wait_for_completion(&commit->hw_done));
2345
2346 spin_lock(&crtc->commit_lock);
2347 list_del(&commit->commit_entry);
2348 spin_unlock(&crtc->commit_lock);
2349 }
2350
2351 if (old_state->fake_commit) {
2352 complete_all(&old_state->fake_commit->cleanup_done);
2353 WARN_ON(!try_wait_for_completion(&old_state->fake_commit->hw_done));
2354 }
2355}
2356EXPORT_SYMBOL(drm_atomic_helper_commit_cleanup_done);
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371int drm_atomic_helper_prepare_planes(struct drm_device *dev,
2372 struct drm_atomic_state *state)
2373{
2374 struct drm_connector *connector;
2375 struct drm_connector_state *new_conn_state;
2376 struct drm_plane *plane;
2377 struct drm_plane_state *new_plane_state;
2378 int ret, i, j;
2379
2380 for_each_new_connector_in_state(state, connector, new_conn_state, i) {
2381 if (!new_conn_state->writeback_job)
2382 continue;
2383
2384 ret = drm_writeback_prepare_job(new_conn_state->writeback_job);
2385 if (ret < 0)
2386 return ret;
2387 }
2388
2389 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
2390 const struct drm_plane_helper_funcs *funcs;
2391
2392 funcs = plane->helper_private;
2393
2394 if (funcs->prepare_fb) {
2395 ret = funcs->prepare_fb(plane, new_plane_state);
2396 if (ret)
2397 goto fail;
2398 }
2399 }
2400
2401 return 0;
2402
2403fail:
2404 for_each_new_plane_in_state(state, plane, new_plane_state, j) {
2405 const struct drm_plane_helper_funcs *funcs;
2406
2407 if (j >= i)
2408 continue;
2409
2410 funcs = plane->helper_private;
2411
2412 if (funcs->cleanup_fb)
2413 funcs->cleanup_fb(plane, new_plane_state);
2414 }
2415
2416 return ret;
2417}
2418EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
2419
2420static bool plane_crtc_active(const struct drm_plane_state *state)
2421{
2422 return state->crtc && state->crtc->state->active;
2423}
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466void drm_atomic_helper_commit_planes(struct drm_device *dev,
2467 struct drm_atomic_state *old_state,
2468 uint32_t flags)
2469{
2470 struct drm_crtc *crtc;
2471 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
2472 struct drm_plane *plane;
2473 struct drm_plane_state *old_plane_state, *new_plane_state;
2474 int i;
2475 bool active_only = flags & DRM_PLANE_COMMIT_ACTIVE_ONLY;
2476 bool no_disable = flags & DRM_PLANE_COMMIT_NO_DISABLE_AFTER_MODESET;
2477
2478 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
2479 const struct drm_crtc_helper_funcs *funcs;
2480
2481 funcs = crtc->helper_private;
2482
2483 if (!funcs || !funcs->atomic_begin)
2484 continue;
2485
2486 if (active_only && !new_crtc_state->active)
2487 continue;
2488
2489 funcs->atomic_begin(crtc, old_state);
2490 }
2491
2492 for_each_oldnew_plane_in_state(old_state, plane, old_plane_state, new_plane_state, i) {
2493 const struct drm_plane_helper_funcs *funcs;
2494 bool disabling;
2495
2496 funcs = plane->helper_private;
2497
2498 if (!funcs)
2499 continue;
2500
2501 disabling = drm_atomic_plane_disabling(old_plane_state,
2502 new_plane_state);
2503
2504 if (active_only) {
2505
2506
2507
2508
2509
2510
2511
2512 if (!disabling && !plane_crtc_active(new_plane_state))
2513 continue;
2514 if (disabling && !plane_crtc_active(old_plane_state))
2515 continue;
2516 }
2517
2518
2519
2520
2521 if (disabling && funcs->atomic_disable) {
2522 struct drm_crtc_state *crtc_state;
2523
2524 crtc_state = old_plane_state->crtc->state;
2525
2526 if (drm_atomic_crtc_needs_modeset(crtc_state) &&
2527 no_disable)
2528 continue;
2529
2530 funcs->atomic_disable(plane, old_state);
2531 } else if (new_plane_state->crtc || disabling) {
2532 funcs->atomic_update(plane, old_state);
2533 }
2534 }
2535
2536 for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
2537 const struct drm_crtc_helper_funcs *funcs;
2538
2539 funcs = crtc->helper_private;
2540
2541 if (!funcs || !funcs->atomic_flush)
2542 continue;
2543
2544 if (active_only && !new_crtc_state->active)
2545 continue;
2546
2547 funcs->atomic_flush(crtc, old_state);
2548 }
2549}
2550EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569void
2570drm_atomic_helper_commit_planes_on_crtc(struct drm_crtc_state *old_crtc_state)
2571{
2572 const struct drm_crtc_helper_funcs *crtc_funcs;
2573 struct drm_crtc *crtc = old_crtc_state->crtc;
2574 struct drm_atomic_state *old_state = old_crtc_state->state;
2575 struct drm_crtc_state *new_crtc_state =
2576 drm_atomic_get_new_crtc_state(old_state, crtc);
2577 struct drm_plane *plane;
2578 unsigned plane_mask;
2579
2580 plane_mask = old_crtc_state->plane_mask;
2581 plane_mask |= new_crtc_state->plane_mask;
2582
2583 crtc_funcs = crtc->helper_private;
2584 if (crtc_funcs && crtc_funcs->atomic_begin)
2585 crtc_funcs->atomic_begin(crtc, old_state);
2586
2587 drm_for_each_plane_mask(plane, crtc->dev, plane_mask) {
2588 struct drm_plane_state *old_plane_state =
2589 drm_atomic_get_old_plane_state(old_state, plane);
2590 struct drm_plane_state *new_plane_state =
2591 drm_atomic_get_new_plane_state(old_state, plane);
2592 const struct drm_plane_helper_funcs *plane_funcs;
2593
2594 plane_funcs = plane->helper_private;
2595
2596 if (!old_plane_state || !plane_funcs)
2597 continue;
2598
2599 WARN_ON(new_plane_state->crtc &&
2600 new_plane_state->crtc != crtc);
2601
2602 if (drm_atomic_plane_disabling(old_plane_state, new_plane_state) &&
2603 plane_funcs->atomic_disable)
2604 plane_funcs->atomic_disable(plane, old_state);
2605 else if (new_plane_state->crtc ||
2606 drm_atomic_plane_disabling(old_plane_state, new_plane_state))
2607 plane_funcs->atomic_update(plane, old_state);
2608 }
2609
2610 if (crtc_funcs && crtc_funcs->atomic_flush)
2611 crtc_funcs->atomic_flush(crtc, old_state);
2612}
2613EXPORT_SYMBOL(drm_atomic_helper_commit_planes_on_crtc);
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631void
2632drm_atomic_helper_disable_planes_on_crtc(struct drm_crtc_state *old_crtc_state,
2633 bool atomic)
2634{
2635 struct drm_crtc *crtc = old_crtc_state->crtc;
2636 const struct drm_crtc_helper_funcs *crtc_funcs =
2637 crtc->helper_private;
2638 struct drm_plane *plane;
2639
2640 if (atomic && crtc_funcs && crtc_funcs->atomic_begin)
2641 crtc_funcs->atomic_begin(crtc, NULL);
2642
2643 drm_atomic_crtc_state_for_each_plane(plane, old_crtc_state) {
2644 const struct drm_plane_helper_funcs *plane_funcs =
2645 plane->helper_private;
2646
2647 if (!plane_funcs)
2648 continue;
2649
2650 WARN_ON(!plane_funcs->atomic_disable);
2651 if (plane_funcs->atomic_disable)
2652 plane_funcs->atomic_disable(plane, NULL);
2653 }
2654
2655 if (atomic && crtc_funcs && crtc_funcs->atomic_flush)
2656 crtc_funcs->atomic_flush(crtc, NULL);
2657}
2658EXPORT_SYMBOL(drm_atomic_helper_disable_planes_on_crtc);
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
2673 struct drm_atomic_state *old_state)
2674{
2675 struct drm_plane *plane;
2676 struct drm_plane_state *old_plane_state, *new_plane_state;
2677 int i;
2678
2679 for_each_oldnew_plane_in_state(old_state, plane, old_plane_state, new_plane_state, i) {
2680 const struct drm_plane_helper_funcs *funcs;
2681 struct drm_plane_state *plane_state;
2682
2683
2684
2685
2686
2687 if (old_plane_state == plane->state)
2688 plane_state = new_plane_state;
2689 else
2690 plane_state = old_plane_state;
2691
2692 funcs = plane->helper_private;
2693
2694 if (funcs->cleanup_fb)
2695 funcs->cleanup_fb(plane, plane_state);
2696 }
2697}
2698EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735int drm_atomic_helper_swap_state(struct drm_atomic_state *state,
2736 bool stall)
2737{
2738 int i, ret;
2739 struct drm_connector *connector;
2740 struct drm_connector_state *old_conn_state, *new_conn_state;
2741 struct drm_crtc *crtc;
2742 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
2743 struct drm_plane *plane;
2744 struct drm_plane_state *old_plane_state, *new_plane_state;
2745 struct drm_crtc_commit *commit;
2746 struct drm_private_obj *obj;
2747 struct drm_private_state *old_obj_state, *new_obj_state;
2748
2749 if (stall) {
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759 for_each_old_crtc_in_state(state, crtc, old_crtc_state, i) {
2760 commit = old_crtc_state->commit;
2761
2762 if (!commit)
2763 continue;
2764
2765 ret = wait_for_completion_interruptible(&commit->hw_done);
2766 if (ret)
2767 return ret;
2768 }
2769
2770 for_each_old_connector_in_state(state, connector, old_conn_state, i) {
2771 commit = old_conn_state->commit;
2772
2773 if (!commit)
2774 continue;
2775
2776 ret = wait_for_completion_interruptible(&commit->hw_done);
2777 if (ret)
2778 return ret;
2779 }
2780
2781 for_each_old_plane_in_state(state, plane, old_plane_state, i) {
2782 commit = old_plane_state->commit;
2783
2784 if (!commit)
2785 continue;
2786
2787 ret = wait_for_completion_interruptible(&commit->hw_done);
2788 if (ret)
2789 return ret;
2790 }
2791 }
2792
2793 for_each_oldnew_connector_in_state(state, connector, old_conn_state, new_conn_state, i) {
2794 WARN_ON(connector->state != old_conn_state);
2795
2796 old_conn_state->state = state;
2797 new_conn_state->state = NULL;
2798
2799 state->connectors[i].state = old_conn_state;
2800 connector->state = new_conn_state;
2801 }
2802
2803 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
2804 WARN_ON(crtc->state != old_crtc_state);
2805
2806 old_crtc_state->state = state;
2807 new_crtc_state->state = NULL;
2808
2809 state->crtcs[i].state = old_crtc_state;
2810 crtc->state = new_crtc_state;
2811
2812 if (new_crtc_state->commit) {
2813 spin_lock(&crtc->commit_lock);
2814 list_add(&new_crtc_state->commit->commit_entry,
2815 &crtc->commit_list);
2816 spin_unlock(&crtc->commit_lock);
2817
2818 new_crtc_state->commit->event = NULL;
2819 }
2820 }
2821
2822 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
2823 WARN_ON(plane->state != old_plane_state);
2824
2825 old_plane_state->state = state;
2826 new_plane_state->state = NULL;
2827
2828 state->planes[i].state = old_plane_state;
2829 plane->state = new_plane_state;
2830 }
2831
2832 for_each_oldnew_private_obj_in_state(state, obj, old_obj_state, new_obj_state, i) {
2833 WARN_ON(obj->state != old_obj_state);
2834
2835 old_obj_state->state = state;
2836 new_obj_state->state = NULL;
2837
2838 state->private_objs[i].state = old_obj_state;
2839 obj->state = new_obj_state;
2840 }
2841
2842 return 0;
2843}
2844EXPORT_SYMBOL(drm_atomic_helper_swap_state);
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866int drm_atomic_helper_update_plane(struct drm_plane *plane,
2867 struct drm_crtc *crtc,
2868 struct drm_framebuffer *fb,
2869 int crtc_x, int crtc_y,
2870 unsigned int crtc_w, unsigned int crtc_h,
2871 uint32_t src_x, uint32_t src_y,
2872 uint32_t src_w, uint32_t src_h,
2873 struct drm_modeset_acquire_ctx *ctx)
2874{
2875 struct drm_atomic_state *state;
2876 struct drm_plane_state *plane_state;
2877 int ret = 0;
2878
2879 state = drm_atomic_state_alloc(plane->dev);
2880 if (!state)
2881 return -ENOMEM;
2882
2883 state->acquire_ctx = ctx;
2884 plane_state = drm_atomic_get_plane_state(state, plane);
2885 if (IS_ERR(plane_state)) {
2886 ret = PTR_ERR(plane_state);
2887 goto fail;
2888 }
2889
2890 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
2891 if (ret != 0)
2892 goto fail;
2893 drm_atomic_set_fb_for_plane(plane_state, fb);
2894 plane_state->crtc_x = crtc_x;
2895 plane_state->crtc_y = crtc_y;
2896 plane_state->crtc_w = crtc_w;
2897 plane_state->crtc_h = crtc_h;
2898 plane_state->src_x = src_x;
2899 plane_state->src_y = src_y;
2900 plane_state->src_w = src_w;
2901 plane_state->src_h = src_h;
2902
2903 if (plane == crtc->cursor)
2904 state->legacy_cursor_update = true;
2905
2906 ret = drm_atomic_commit(state);
2907fail:
2908 drm_atomic_state_put(state);
2909 return ret;
2910}
2911EXPORT_SYMBOL(drm_atomic_helper_update_plane);
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923int drm_atomic_helper_disable_plane(struct drm_plane *plane,
2924 struct drm_modeset_acquire_ctx *ctx)
2925{
2926 struct drm_atomic_state *state;
2927 struct drm_plane_state *plane_state;
2928 int ret = 0;
2929
2930 state = drm_atomic_state_alloc(plane->dev);
2931 if (!state)
2932 return -ENOMEM;
2933
2934 state->acquire_ctx = ctx;
2935 plane_state = drm_atomic_get_plane_state(state, plane);
2936 if (IS_ERR(plane_state)) {
2937 ret = PTR_ERR(plane_state);
2938 goto fail;
2939 }
2940
2941 if (plane_state->crtc && plane_state->crtc->cursor == plane)
2942 plane_state->state->legacy_cursor_update = true;
2943
2944 ret = __drm_atomic_helper_disable_plane(plane, plane_state);
2945 if (ret != 0)
2946 goto fail;
2947
2948 ret = drm_atomic_commit(state);
2949fail:
2950 drm_atomic_state_put(state);
2951 return ret;
2952}
2953EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971int drm_atomic_helper_set_config(struct drm_mode_set *set,
2972 struct drm_modeset_acquire_ctx *ctx)
2973{
2974 struct drm_atomic_state *state;
2975 struct drm_crtc *crtc = set->crtc;
2976 int ret = 0;
2977
2978 state = drm_atomic_state_alloc(crtc->dev);
2979 if (!state)
2980 return -ENOMEM;
2981
2982 state->acquire_ctx = ctx;
2983 ret = __drm_atomic_helper_set_config(set, state);
2984 if (ret != 0)
2985 goto fail;
2986
2987 ret = handle_conflicting_encoders(state, true);
2988 if (ret)
2989 goto fail;
2990
2991 ret = drm_atomic_commit(state);
2992
2993fail:
2994 drm_atomic_state_put(state);
2995 return ret;
2996}
2997EXPORT_SYMBOL(drm_atomic_helper_set_config);
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022int drm_atomic_helper_disable_all(struct drm_device *dev,
3023 struct drm_modeset_acquire_ctx *ctx)
3024{
3025 struct drm_atomic_state *state;
3026 struct drm_connector_state *conn_state;
3027 struct drm_connector *conn;
3028 struct drm_plane_state *plane_state;
3029 struct drm_plane *plane;
3030 struct drm_crtc_state *crtc_state;
3031 struct drm_crtc *crtc;
3032 int ret, i;
3033
3034 state = drm_atomic_state_alloc(dev);
3035 if (!state)
3036 return -ENOMEM;
3037
3038 state->acquire_ctx = ctx;
3039
3040 drm_for_each_crtc(crtc, dev) {
3041 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3042 if (IS_ERR(crtc_state)) {
3043 ret = PTR_ERR(crtc_state);
3044 goto free;
3045 }
3046
3047 crtc_state->active = false;
3048
3049 ret = drm_atomic_set_mode_prop_for_crtc(crtc_state, NULL);
3050 if (ret < 0)
3051 goto free;
3052
3053 ret = drm_atomic_add_affected_planes(state, crtc);
3054 if (ret < 0)
3055 goto free;
3056
3057 ret = drm_atomic_add_affected_connectors(state, crtc);
3058 if (ret < 0)
3059 goto free;
3060 }
3061
3062 for_each_new_connector_in_state(state, conn, conn_state, i) {
3063 ret = drm_atomic_set_crtc_for_connector(conn_state, NULL);
3064 if (ret < 0)
3065 goto free;
3066 }
3067
3068 for_each_new_plane_in_state(state, plane, plane_state, i) {
3069 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
3070 if (ret < 0)
3071 goto free;
3072
3073 drm_atomic_set_fb_for_plane(plane_state, NULL);
3074 }
3075
3076 ret = drm_atomic_commit(state);
3077free:
3078 drm_atomic_state_put(state);
3079 return ret;
3080}
3081EXPORT_SYMBOL(drm_atomic_helper_disable_all);
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094void drm_atomic_helper_shutdown(struct drm_device *dev)
3095{
3096 struct drm_modeset_acquire_ctx ctx;
3097 int ret;
3098
3099 DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx, 0, ret);
3100
3101 ret = drm_atomic_helper_disable_all(dev, &ctx);
3102 if (ret)
3103 DRM_ERROR("Disabling all crtc's during unload failed with %i\n", ret);
3104
3105 DRM_MODESET_LOCK_ALL_END(dev, ctx, ret);
3106}
3107EXPORT_SYMBOL(drm_atomic_helper_shutdown);
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133struct drm_atomic_state *
3134drm_atomic_helper_duplicate_state(struct drm_device *dev,
3135 struct drm_modeset_acquire_ctx *ctx)
3136{
3137 struct drm_atomic_state *state;
3138 struct drm_connector *conn;
3139 struct drm_connector_list_iter conn_iter;
3140 struct drm_plane *plane;
3141 struct drm_crtc *crtc;
3142 int err = 0;
3143
3144 state = drm_atomic_state_alloc(dev);
3145 if (!state)
3146 return ERR_PTR(-ENOMEM);
3147
3148 state->acquire_ctx = ctx;
3149 state->duplicated = true;
3150
3151 drm_for_each_crtc(crtc, dev) {
3152 struct drm_crtc_state *crtc_state;
3153
3154 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3155 if (IS_ERR(crtc_state)) {
3156 err = PTR_ERR(crtc_state);
3157 goto free;
3158 }
3159 }
3160
3161 drm_for_each_plane(plane, dev) {
3162 struct drm_plane_state *plane_state;
3163
3164 plane_state = drm_atomic_get_plane_state(state, plane);
3165 if (IS_ERR(plane_state)) {
3166 err = PTR_ERR(plane_state);
3167 goto free;
3168 }
3169 }
3170
3171 drm_connector_list_iter_begin(dev, &conn_iter);
3172 drm_for_each_connector_iter(conn, &conn_iter) {
3173 struct drm_connector_state *conn_state;
3174
3175 conn_state = drm_atomic_get_connector_state(state, conn);
3176 if (IS_ERR(conn_state)) {
3177 err = PTR_ERR(conn_state);
3178 drm_connector_list_iter_end(&conn_iter);
3179 goto free;
3180 }
3181 }
3182 drm_connector_list_iter_end(&conn_iter);
3183
3184
3185 state->acquire_ctx = NULL;
3186
3187free:
3188 if (err < 0) {
3189 drm_atomic_state_put(state);
3190 state = ERR_PTR(err);
3191 }
3192
3193 return state;
3194}
3195EXPORT_SYMBOL(drm_atomic_helper_duplicate_state);
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222struct drm_atomic_state *drm_atomic_helper_suspend(struct drm_device *dev)
3223{
3224 struct drm_modeset_acquire_ctx ctx;
3225 struct drm_atomic_state *state;
3226 int err;
3227
3228
3229 state = ERR_PTR(-EINVAL);
3230
3231 DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx, 0, err);
3232
3233 state = drm_atomic_helper_duplicate_state(dev, &ctx);
3234 if (IS_ERR(state))
3235 goto unlock;
3236
3237 err = drm_atomic_helper_disable_all(dev, &ctx);
3238 if (err < 0) {
3239 drm_atomic_state_put(state);
3240 state = ERR_PTR(err);
3241 goto unlock;
3242 }
3243
3244unlock:
3245 DRM_MODESET_LOCK_ALL_END(dev, ctx, err);
3246 if (err)
3247 return ERR_PTR(err);
3248
3249 return state;
3250}
3251EXPORT_SYMBOL(drm_atomic_helper_suspend);
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268int drm_atomic_helper_commit_duplicated_state(struct drm_atomic_state *state,
3269 struct drm_modeset_acquire_ctx *ctx)
3270{
3271 int i, ret;
3272 struct drm_plane *plane;
3273 struct drm_plane_state *new_plane_state;
3274 struct drm_connector *connector;
3275 struct drm_connector_state *new_conn_state;
3276 struct drm_crtc *crtc;
3277 struct drm_crtc_state *new_crtc_state;
3278
3279 state->acquire_ctx = ctx;
3280
3281 for_each_new_plane_in_state(state, plane, new_plane_state, i)
3282 state->planes[i].old_state = plane->state;
3283
3284 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i)
3285 state->crtcs[i].old_state = crtc->state;
3286
3287 for_each_new_connector_in_state(state, connector, new_conn_state, i)
3288 state->connectors[i].old_state = connector->state;
3289
3290 ret = drm_atomic_commit(state);
3291
3292 state->acquire_ctx = NULL;
3293
3294 return ret;
3295}
3296EXPORT_SYMBOL(drm_atomic_helper_commit_duplicated_state);
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314int drm_atomic_helper_resume(struct drm_device *dev,
3315 struct drm_atomic_state *state)
3316{
3317 struct drm_modeset_acquire_ctx ctx;
3318 int err;
3319
3320 drm_mode_config_reset(dev);
3321
3322 DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx, 0, err);
3323
3324 err = drm_atomic_helper_commit_duplicated_state(state, &ctx);
3325
3326 DRM_MODESET_LOCK_ALL_END(dev, ctx, err);
3327 drm_atomic_state_put(state);
3328
3329 return err;
3330}
3331EXPORT_SYMBOL(drm_atomic_helper_resume);
3332
3333static int page_flip_common(struct drm_atomic_state *state,
3334 struct drm_crtc *crtc,
3335 struct drm_framebuffer *fb,
3336 struct drm_pending_vblank_event *event,
3337 uint32_t flags)
3338{
3339 struct drm_plane *plane = crtc->primary;
3340 struct drm_plane_state *plane_state;
3341 struct drm_crtc_state *crtc_state;
3342 int ret = 0;
3343
3344 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3345 if (IS_ERR(crtc_state))
3346 return PTR_ERR(crtc_state);
3347
3348 crtc_state->event = event;
3349 crtc_state->async_flip = flags & DRM_MODE_PAGE_FLIP_ASYNC;
3350
3351 plane_state = drm_atomic_get_plane_state(state, plane);
3352 if (IS_ERR(plane_state))
3353 return PTR_ERR(plane_state);
3354
3355 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
3356 if (ret != 0)
3357 return ret;
3358 drm_atomic_set_fb_for_plane(plane_state, fb);
3359
3360
3361 state->allow_modeset = false;
3362 if (!crtc_state->active) {
3363 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] disabled, rejecting legacy flip\n",
3364 crtc->base.id, crtc->name);
3365 return -EINVAL;
3366 }
3367
3368 return ret;
3369}
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388int drm_atomic_helper_page_flip(struct drm_crtc *crtc,
3389 struct drm_framebuffer *fb,
3390 struct drm_pending_vblank_event *event,
3391 uint32_t flags,
3392 struct drm_modeset_acquire_ctx *ctx)
3393{
3394 struct drm_plane *plane = crtc->primary;
3395 struct drm_atomic_state *state;
3396 int ret = 0;
3397
3398 state = drm_atomic_state_alloc(plane->dev);
3399 if (!state)
3400 return -ENOMEM;
3401
3402 state->acquire_ctx = ctx;
3403
3404 ret = page_flip_common(state, crtc, fb, event, flags);
3405 if (ret != 0)
3406 goto fail;
3407
3408 ret = drm_atomic_nonblocking_commit(state);
3409fail:
3410 drm_atomic_state_put(state);
3411 return ret;
3412}
3413EXPORT_SYMBOL(drm_atomic_helper_page_flip);
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431int drm_atomic_helper_page_flip_target(struct drm_crtc *crtc,
3432 struct drm_framebuffer *fb,
3433 struct drm_pending_vblank_event *event,
3434 uint32_t flags,
3435 uint32_t target,
3436 struct drm_modeset_acquire_ctx *ctx)
3437{
3438 struct drm_plane *plane = crtc->primary;
3439 struct drm_atomic_state *state;
3440 struct drm_crtc_state *crtc_state;
3441 int ret = 0;
3442
3443 state = drm_atomic_state_alloc(plane->dev);
3444 if (!state)
3445 return -ENOMEM;
3446
3447 state->acquire_ctx = ctx;
3448
3449 ret = page_flip_common(state, crtc, fb, event, flags);
3450 if (ret != 0)
3451 goto fail;
3452
3453 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
3454 if (WARN_ON(!crtc_state)) {
3455 ret = -EINVAL;
3456 goto fail;
3457 }
3458 crtc_state->target_vblank = target;
3459
3460 ret = drm_atomic_nonblocking_commit(state);
3461fail:
3462 drm_atomic_state_put(state);
3463 return ret;
3464}
3465EXPORT_SYMBOL(drm_atomic_helper_page_flip_target);
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486u32 *
3487drm_atomic_helper_bridge_propagate_bus_fmt(struct drm_bridge *bridge,
3488 struct drm_bridge_state *bridge_state,
3489 struct drm_crtc_state *crtc_state,
3490 struct drm_connector_state *conn_state,
3491 u32 output_fmt,
3492 unsigned int *num_input_fmts)
3493{
3494 u32 *input_fmts;
3495
3496 input_fmts = kzalloc(sizeof(*input_fmts), GFP_KERNEL);
3497 if (!input_fmts) {
3498 *num_input_fmts = 0;
3499 return NULL;
3500 }
3501
3502 *num_input_fmts = 1;
3503 input_fmts[0] = output_fmt;
3504 return input_fmts;
3505}
3506EXPORT_SYMBOL(drm_atomic_helper_bridge_propagate_bus_fmt);
3507