blob: c4936bc4318e35eeb73c5e8bf594c772ee77050b [file] [log] [blame]
Daniel Vetterc2fcd272014-11-05 00:14:14 +01001/*
2 * Copyright (C) 2014 Red Hat
3 * Copyright (C) 2014 Intel Corp.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Rob Clark <robdclark@gmail.com>
25 * Daniel Vetter <daniel.vetter@ffwll.ch>
26 */
27
28#include <drm/drmP.h>
29#include <drm/drm_atomic.h>
30#include <drm/drm_plane_helper.h>
31#include <drm/drm_crtc_helper.h>
Daniel Vetter623369e2014-09-16 17:50:47 +020032#include <drm/drm_atomic_helper.h>
Daniel Vetterc2fcd272014-11-05 00:14:14 +010033
34static void
35drm_atomic_helper_plane_changed(struct drm_atomic_state *state,
36 struct drm_plane_state *plane_state,
37 struct drm_plane *plane)
38{
39 struct drm_crtc_state *crtc_state;
40
41 if (plane->state->crtc) {
42 crtc_state = state->crtc_states[drm_crtc_index(plane->crtc)];
43
44 if (WARN_ON(!crtc_state))
45 return;
46
47 crtc_state->planes_changed = true;
48 }
49
50 if (plane_state->crtc) {
51 crtc_state =
52 state->crtc_states[drm_crtc_index(plane_state->crtc)];
53
54 if (WARN_ON(!crtc_state))
55 return;
56
57 crtc_state->planes_changed = true;
58 }
59}
60
Daniel Vetter623369e2014-09-16 17:50:47 +020061static struct drm_crtc *
62get_current_crtc_for_encoder(struct drm_device *dev,
63 struct drm_encoder *encoder)
64{
65 struct drm_mode_config *config = &dev->mode_config;
66 struct drm_connector *connector;
67
68 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
69
70 list_for_each_entry(connector, &config->connector_list, head) {
71 if (connector->state->best_encoder != encoder)
72 continue;
73
74 return connector->state->crtc;
75 }
76
77 return NULL;
78}
79
80static int
81steal_encoder(struct drm_atomic_state *state,
82 struct drm_encoder *encoder,
83 struct drm_crtc *encoder_crtc)
84{
85 struct drm_mode_config *config = &state->dev->mode_config;
86 struct drm_crtc_state *crtc_state;
87 struct drm_connector *connector;
88 struct drm_connector_state *connector_state;
89
90 /*
91 * We can only steal an encoder coming from a connector, which means we
92 * must already hold the connection_mutex.
93 */
94 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
95
96 DRM_DEBUG_KMS("[ENCODER:%d:%s] in use on [CRTC:%d], stealing it\n",
97 encoder->base.id, encoder->name,
98 encoder_crtc->base.id);
99
100 crtc_state = drm_atomic_get_crtc_state(state, encoder_crtc);
101 if (IS_ERR(crtc_state))
102 return PTR_ERR(crtc_state);
103
104 crtc_state->mode_changed = true;
105
106 list_for_each_entry(connector, &config->connector_list, head) {
107 if (connector->state->best_encoder != encoder)
108 continue;
109
110 DRM_DEBUG_KMS("Stealing encoder from [CONNECTOR:%d:%s]\n",
111 connector->base.id,
112 connector->name);
113
114 connector_state = drm_atomic_get_connector_state(state,
115 connector);
116 if (IS_ERR(connector_state))
117 return PTR_ERR(connector_state);
118
119 connector_state->crtc = NULL;
120 connector_state->best_encoder = NULL;
121 }
122
123 return 0;
124}
125
126static int
127update_connector_routing(struct drm_atomic_state *state, int conn_idx)
128{
129 struct drm_connector_helper_funcs *funcs;
130 struct drm_encoder *new_encoder;
131 struct drm_crtc *encoder_crtc;
132 struct drm_connector *connector;
133 struct drm_connector_state *connector_state;
134 struct drm_crtc_state *crtc_state;
135 int idx, ret;
136
137 connector = state->connectors[conn_idx];
138 connector_state = state->connector_states[conn_idx];
139
140 if (!connector)
141 return 0;
142
143 DRM_DEBUG_KMS("Updating routing for [CONNECTOR:%d:%s]\n",
144 connector->base.id,
145 connector->name);
146
147 if (connector->state->crtc != connector_state->crtc) {
148 if (connector->state->crtc) {
149 idx = drm_crtc_index(connector->state->crtc);
150
151 crtc_state = state->crtc_states[idx];
152 crtc_state->mode_changed = true;
153 }
154
155 if (connector_state->crtc) {
156 idx = drm_crtc_index(connector_state->crtc);
157
158 crtc_state = state->crtc_states[idx];
159 crtc_state->mode_changed = true;
160 }
161 }
162
163 if (!connector_state->crtc) {
164 DRM_DEBUG_KMS("Disabling [CONNECTOR:%d:%s]\n",
165 connector->base.id,
166 connector->name);
167
168 connector_state->best_encoder = NULL;
169
170 return 0;
171 }
172
173 funcs = connector->helper_private;
174 new_encoder = funcs->best_encoder(connector);
175
176 if (!new_encoder) {
177 DRM_DEBUG_KMS("No suitable encoder found for [CONNECTOR:%d:%s]\n",
178 connector->base.id,
179 connector->name);
180 return -EINVAL;
181 }
182
183 if (new_encoder == connector_state->best_encoder) {
184 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] keeps [ENCODER:%d:%s], now on [CRTC:%d]\n",
185 connector->base.id,
186 connector->name,
187 new_encoder->base.id,
188 new_encoder->name,
189 connector_state->crtc->base.id);
190
191 return 0;
192 }
193
194 encoder_crtc = get_current_crtc_for_encoder(state->dev,
195 new_encoder);
196
197 if (encoder_crtc) {
198 ret = steal_encoder(state, new_encoder, encoder_crtc);
199 if (ret) {
200 DRM_DEBUG_KMS("Encoder stealing failed for [CONNECTOR:%d:%s]\n",
201 connector->base.id,
202 connector->name);
203 return ret;
204 }
205 }
206
207 connector_state->best_encoder = new_encoder;
208 idx = drm_crtc_index(connector_state->crtc);
209
210 crtc_state = state->crtc_states[idx];
211 crtc_state->mode_changed = true;
212
213 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] using [ENCODER:%d:%s] on [CRTC:%d]\n",
214 connector->base.id,
215 connector->name,
216 new_encoder->base.id,
217 new_encoder->name,
218 connector_state->crtc->base.id);
219
220 return 0;
221}
222
223static int
224mode_fixup(struct drm_atomic_state *state)
225{
226 int ncrtcs = state->dev->mode_config.num_crtc;
227 int nconnectors = state->dev->mode_config.num_connector;
228 struct drm_crtc_state *crtc_state;
229 struct drm_connector_state *conn_state;
230 int i;
231 bool ret;
232
233 for (i = 0; i < ncrtcs; i++) {
234 crtc_state = state->crtc_states[i];
235
236 if (!crtc_state || !crtc_state->mode_changed)
237 continue;
238
239 drm_mode_copy(&crtc_state->adjusted_mode, &crtc_state->mode);
240 }
241
242 for (i = 0; i < nconnectors; i++) {
243 struct drm_encoder_helper_funcs *funcs;
244 struct drm_encoder *encoder;
245
246 conn_state = state->connector_states[i];
247
248 if (!conn_state)
249 continue;
250
251 WARN_ON(!!conn_state->best_encoder != !!conn_state->crtc);
252
253 if (!conn_state->crtc || !conn_state->best_encoder)
254 continue;
255
256 crtc_state =
257 state->crtc_states[drm_crtc_index(conn_state->crtc)];
258
259 /*
260 * Each encoder has at most one connector (since we always steal
261 * it away), so we won't call ->mode_fixup twice.
262 */
263 encoder = conn_state->best_encoder;
264 funcs = encoder->helper_private;
265
266 if (encoder->bridge && encoder->bridge->funcs->mode_fixup) {
267 ret = encoder->bridge->funcs->mode_fixup(
268 encoder->bridge, &crtc_state->mode,
269 &crtc_state->adjusted_mode);
270 if (!ret) {
271 DRM_DEBUG_KMS("Bridge fixup failed\n");
272 return -EINVAL;
273 }
274 }
275
276
277 ret = funcs->mode_fixup(encoder, &crtc_state->mode,
278 &crtc_state->adjusted_mode);
279 if (!ret) {
280 DRM_DEBUG_KMS("[ENCODER:%d:%s] fixup failed\n",
281 encoder->base.id, encoder->name);
282 return -EINVAL;
283 }
284 }
285
286 for (i = 0; i < ncrtcs; i++) {
287 struct drm_crtc_helper_funcs *funcs;
288 struct drm_crtc *crtc;
289
290 crtc_state = state->crtc_states[i];
291 crtc = state->crtcs[i];
292
293 if (!crtc_state || !crtc_state->mode_changed)
294 continue;
295
296 funcs = crtc->helper_private;
297 ret = funcs->mode_fixup(crtc, &crtc_state->mode,
298 &crtc_state->adjusted_mode);
299 if (!ret) {
300 DRM_DEBUG_KMS("[CRTC:%d] fixup failed\n",
301 crtc->base.id);
302 return -EINVAL;
303 }
304 }
305
306 return 0;
307}
308
309static int
310drm_atomic_helper_check_prepare(struct drm_device *dev,
311 struct drm_atomic_state *state)
312{
313 int ncrtcs = dev->mode_config.num_crtc;
314 int nconnectors = dev->mode_config.num_connector;
315 struct drm_crtc *crtc;
316 struct drm_crtc_state *crtc_state;
317 int i, ret;
318
319 for (i = 0; i < ncrtcs; i++) {
320 crtc = state->crtcs[i];
321 crtc_state = state->crtc_states[i];
322
323 if (!crtc)
324 continue;
325
326 if (!drm_mode_equal(&crtc->state->mode, &crtc_state->mode)) {
327 DRM_DEBUG_KMS("[CRTC:%d] mode changed\n",
328 crtc->base.id);
329 crtc_state->mode_changed = true;
330 }
331
332 if (crtc->state->enable != crtc_state->enable) {
333 DRM_DEBUG_KMS("[CRTC:%d] enable changed\n",
334 crtc->base.id);
335 crtc_state->mode_changed = true;
336 }
337 }
338
339 for (i = 0; i < nconnectors; i++) {
340 /*
341 * This only sets crtc->mode_changed for routing changes,
342 * drivers must set crtc->mode_changed themselves when connector
343 * properties need to be updated.
344 */
345 ret = update_connector_routing(state, i);
346 if (ret)
347 return ret;
348 }
349
350 /*
351 * After all the routing has been prepared we need to add in any
352 * connector which is itself unchanged, but who's crtc changes it's
353 * configuration. This must be done before calling mode_fixup in case a
354 * crtc only changed its mode but has the same set of connectors.
355 */
356 for (i = 0; i < ncrtcs; i++) {
357 int num_connectors;
358
359 crtc = state->crtcs[i];
360 crtc_state = state->crtc_states[i];
361
362 if (!crtc || !crtc_state->mode_changed)
363 continue;
364
365 DRM_DEBUG_KMS("[CRTC:%d] needs full modeset, enable: %c\n",
366 crtc->base.id,
367 crtc_state->enable ? 'y' : 'n');
368
369 ret = drm_atomic_add_affected_connectors(state, crtc);
370 if (ret != 0)
371 return ret;
372
373 num_connectors = drm_atomic_connectors_for_crtc(state,
374 crtc);
375
376 if (crtc_state->enable != !!num_connectors) {
377 DRM_DEBUG_KMS("[CRTC:%d] enabled/connectors mismatch\n",
378 crtc->base.id);
379
380 return -EINVAL;
381 }
382 }
383
384 return mode_fixup(state);
385}
386
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100387/**
388 * drm_atomic_helper_check - validate state object
389 * @dev: DRM device
390 * @state: the driver state object
391 *
392 * Check the state object to see if the requested state is physically possible.
393 * Only crtcs and planes have check callbacks, so for any additional (global)
394 * checking that a driver needs it can simply wrap that around this function.
395 * Drivers without such needs can directly use this as their ->atomic_check()
396 * callback.
397 *
398 * RETURNS
399 * Zero for success or -errno
400 */
401int drm_atomic_helper_check(struct drm_device *dev,
402 struct drm_atomic_state *state)
403{
404 int nplanes = dev->mode_config.num_total_plane;
405 int ncrtcs = dev->mode_config.num_crtc;
406 int i, ret = 0;
407
Daniel Vetter623369e2014-09-16 17:50:47 +0200408 ret = drm_atomic_helper_check_prepare(dev, state);
409 if (ret)
410 return ret;
411
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100412 for (i = 0; i < nplanes; i++) {
413 struct drm_plane_helper_funcs *funcs;
414 struct drm_plane *plane = state->planes[i];
415 struct drm_plane_state *plane_state = state->plane_states[i];
416
417 if (!plane)
418 continue;
419
420 funcs = plane->helper_private;
421
422 drm_atomic_helper_plane_changed(state, plane_state, plane);
423
424 if (!funcs || !funcs->atomic_check)
425 continue;
426
427 ret = funcs->atomic_check(plane, plane_state);
428 if (ret) {
429 DRM_DEBUG_KMS("[PLANE:%d] atomic check failed\n",
430 plane->base.id);
431 return ret;
432 }
433 }
434
435 for (i = 0; i < ncrtcs; i++) {
436 struct drm_crtc_helper_funcs *funcs;
437 struct drm_crtc *crtc = state->crtcs[i];
438
439 if (!crtc)
440 continue;
441
442 funcs = crtc->helper_private;
443
444 if (!funcs || !funcs->atomic_check)
445 continue;
446
447 ret = funcs->atomic_check(crtc, state->crtc_states[i]);
448 if (ret) {
449 DRM_DEBUG_KMS("[CRTC:%d] atomic check failed\n",
450 crtc->base.id);
451 return ret;
452 }
453 }
454
455 return ret;
456}
457EXPORT_SYMBOL(drm_atomic_helper_check);
458
Daniel Vetter623369e2014-09-16 17:50:47 +0200459static void
460disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state)
461{
462 int ncrtcs = old_state->dev->mode_config.num_crtc;
463 int nconnectors = old_state->dev->mode_config.num_connector;
464 int i;
465
466 for (i = 0; i < nconnectors; i++) {
467 struct drm_connector_state *old_conn_state;
468 struct drm_connector *connector;
469 struct drm_encoder_helper_funcs *funcs;
470 struct drm_encoder *encoder;
471
472 old_conn_state = old_state->connector_states[i];
473 connector = old_state->connectors[i];
474
475 /* Shut down everything that's in the changeset and currently
476 * still on. So need to check the old, saved state. */
477 if (!old_conn_state || !old_conn_state->crtc)
478 continue;
479
480 encoder = connector->state->best_encoder;
481
482 if (!encoder)
483 continue;
484
485 funcs = encoder->helper_private;
486
487 /*
488 * Each encoder has at most one connector (since we always steal
489 * it away), so we won't call call disable hooks twice.
490 */
491 if (encoder->bridge)
492 encoder->bridge->funcs->disable(encoder->bridge);
493
494 /* Right function depends upon target state. */
495 if (connector->state->crtc)
496 funcs->prepare(encoder);
497 else if (funcs->disable)
498 funcs->disable(encoder);
499 else
500 funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
501
502 if (encoder->bridge)
503 encoder->bridge->funcs->post_disable(encoder->bridge);
504 }
505
506 for (i = 0; i < ncrtcs; i++) {
507 struct drm_crtc_helper_funcs *funcs;
508 struct drm_crtc *crtc;
509
510 crtc = old_state->crtcs[i];
511
512 /* Shut down everything that needs a full modeset. */
513 if (!crtc || !crtc->state->mode_changed)
514 continue;
515
516 funcs = crtc->helper_private;
517
518 /* Right function depends upon target state. */
519 if (crtc->state->enable)
520 funcs->prepare(crtc);
521 else if (funcs->disable)
522 funcs->disable(crtc);
523 else
524 funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
525 }
526}
527
528static void
529set_routing_links(struct drm_device *dev, struct drm_atomic_state *old_state)
530{
531 int nconnectors = dev->mode_config.num_connector;
532 int ncrtcs = old_state->dev->mode_config.num_crtc;
533 int i;
534
535 /* clear out existing links */
536 for (i = 0; i < nconnectors; i++) {
537 struct drm_connector *connector;
538
539 connector = old_state->connectors[i];
540
541 if (!connector || !connector->encoder)
542 continue;
543
544 WARN_ON(!connector->encoder->crtc);
545
546 connector->encoder->crtc = NULL;
547 connector->encoder = NULL;
548 }
549
550 /* set new links */
551 for (i = 0; i < nconnectors; i++) {
552 struct drm_connector *connector;
553
554 connector = old_state->connectors[i];
555
556 if (!connector || !connector->state->crtc)
557 continue;
558
559 if (WARN_ON(!connector->state->best_encoder))
560 continue;
561
562 connector->encoder = connector->state->best_encoder;
563 connector->encoder->crtc = connector->state->crtc;
564 }
565
566 /* set legacy state in the crtc structure */
567 for (i = 0; i < ncrtcs; i++) {
568 struct drm_crtc *crtc;
569
570 crtc = old_state->crtcs[i];
571
572 if (!crtc)
573 continue;
574
575 crtc->mode = crtc->state->mode;
576 crtc->enabled = crtc->state->enable;
577 crtc->x = crtc->primary->state->src_x >> 16;
578 crtc->y = crtc->primary->state->src_y >> 16;
579 }
580}
581
582static void
583crtc_set_mode(struct drm_device *dev, struct drm_atomic_state *old_state)
584{
585 int ncrtcs = old_state->dev->mode_config.num_crtc;
586 int nconnectors = old_state->dev->mode_config.num_connector;
587 int i;
588
589 for (i = 0; i < ncrtcs; i++) {
590 struct drm_crtc_helper_funcs *funcs;
591 struct drm_crtc *crtc;
592
593 crtc = old_state->crtcs[i];
594
595 if (!crtc || !crtc->state->mode_changed)
596 continue;
597
598 funcs = crtc->helper_private;
599
600 if (crtc->state->enable)
601 funcs->mode_set_nofb(crtc);
602 }
603
604 for (i = 0; i < nconnectors; i++) {
605 struct drm_connector *connector;
606 struct drm_crtc_state *new_crtc_state;
607 struct drm_encoder_helper_funcs *funcs;
608 struct drm_encoder *encoder;
609 struct drm_display_mode *mode, *adjusted_mode;
610
611 connector = old_state->connectors[i];
612
613 if (!connector || !connector->state->best_encoder)
614 continue;
615
616 encoder = connector->state->best_encoder;
617 funcs = encoder->helper_private;
618 new_crtc_state = connector->state->crtc->state;
619 mode = &new_crtc_state->mode;
620 adjusted_mode = &new_crtc_state->adjusted_mode;
621
622 /*
623 * Each encoder has at most one connector (since we always steal
624 * it away), so we won't call call mode_set hooks twice.
625 */
626 funcs->mode_set(encoder, mode, adjusted_mode);
627
628 if (encoder->bridge && encoder->bridge->funcs->mode_set)
629 encoder->bridge->funcs->mode_set(encoder->bridge,
630 mode, adjusted_mode);
631 }
632}
633
634/**
635 * drm_atomic_helper_commit_pre_planes - modeset commit before plane updates
636 * @dev: DRM device
637 * @state: atomic state
638 *
639 * This function commits the modeset changes that need to be committed before
640 * updating planes. It shuts down all the outputs that need to be shut down and
641 * prepares them (if required) with the new mode.
642 */
643void drm_atomic_helper_commit_pre_planes(struct drm_device *dev,
644 struct drm_atomic_state *state)
645{
646 disable_outputs(dev, state);
647 set_routing_links(dev, state);
648 crtc_set_mode(dev, state);
649}
650EXPORT_SYMBOL(drm_atomic_helper_commit_pre_planes);
651
652/**
653 * drm_atomic_helper_commit_post_planes - modeset commit after plane updates
654 * @dev: DRM device
655 * @old_state: atomic state object with old state structures
656 *
657 * This function commits the modeset changes that need to be committed after
658 * updating planes: It enables all the outputs with the new configuration which
659 * had to be turned off for the update.
660 */
661void drm_atomic_helper_commit_post_planes(struct drm_device *dev,
662 struct drm_atomic_state *old_state)
663{
664 int ncrtcs = old_state->dev->mode_config.num_crtc;
665 int nconnectors = old_state->dev->mode_config.num_connector;
666 int i;
667
668 for (i = 0; i < ncrtcs; i++) {
669 struct drm_crtc_helper_funcs *funcs;
670 struct drm_crtc *crtc;
671
672 crtc = old_state->crtcs[i];
673
674 /* Need to filter out CRTCs where only planes change. */
675 if (!crtc || !crtc->state->mode_changed)
676 continue;
677
678 funcs = crtc->helper_private;
679
680 if (crtc->state->enable)
681 funcs->commit(crtc);
682 }
683
684 for (i = 0; i < nconnectors; i++) {
685 struct drm_connector *connector;
686 struct drm_encoder_helper_funcs *funcs;
687 struct drm_encoder *encoder;
688
689 connector = old_state->connectors[i];
690
691 if (!connector || !connector->state->best_encoder)
692 continue;
693
694 encoder = connector->state->best_encoder;
695 funcs = encoder->helper_private;
696
697 /*
698 * Each encoder has at most one connector (since we always steal
699 * it away), so we won't call call enable hooks twice.
700 */
701 if (encoder->bridge)
702 encoder->bridge->funcs->pre_enable(encoder->bridge);
703
704 funcs->commit(encoder);
705
706 if (encoder->bridge)
707 encoder->bridge->funcs->enable(encoder->bridge);
708 }
709}
710EXPORT_SYMBOL(drm_atomic_helper_commit_post_planes);
711
712static void
713wait_for_vblanks(struct drm_device *dev, struct drm_atomic_state *old_state)
714{
715 struct drm_crtc *crtc;
716 struct drm_crtc_state *old_crtc_state;
717 int ncrtcs = old_state->dev->mode_config.num_crtc;
718 int i, ret;
719
720 for (i = 0; i < ncrtcs; i++) {
721 crtc = old_state->crtcs[i];
722 old_crtc_state = old_state->crtc_states[i];
723
724 if (!crtc)
725 continue;
726
727 /* No one cares about the old state, so abuse it for tracking
728 * and store whether we hold a vblank reference (and should do a
729 * vblank wait) in the ->enable boolean. */
730 old_crtc_state->enable = false;
731
732 if (!crtc->state->enable)
733 continue;
734
735 ret = drm_crtc_vblank_get(crtc);
736 if (ret != 0)
737 continue;
738
739 old_crtc_state->enable = true;
740 old_crtc_state->last_vblank_count = drm_vblank_count(dev, i);
741 }
742
743 for (i = 0; i < ncrtcs; i++) {
744 crtc = old_state->crtcs[i];
745 old_crtc_state = old_state->crtc_states[i];
746
747 if (!crtc || !old_crtc_state->enable)
748 continue;
749
750 ret = wait_event_timeout(dev->vblank[i].queue,
751 old_crtc_state->last_vblank_count !=
752 drm_vblank_count(dev, i),
753 msecs_to_jiffies(50));
754
755 drm_crtc_vblank_put(crtc);
756 }
757}
758
759/**
760 * drm_atomic_helper_commit - commit validated state object
761 * @dev: DRM device
762 * @state: the driver state object
763 * @async: asynchronous commit
764 *
765 * This function commits a with drm_atomic_helper_check() pre-validated state
766 * object. This can still fail when e.g. the framebuffer reservation fails. For
767 * now this doesn't implement asynchronous commits.
768 *
769 * RETURNS
770 * Zero for success or -errno.
771 */
772int drm_atomic_helper_commit(struct drm_device *dev,
773 struct drm_atomic_state *state,
774 bool async)
775{
776 int ret;
777
778 if (async)
779 return -EBUSY;
780
781 ret = drm_atomic_helper_prepare_planes(dev, state);
782 if (ret)
783 return ret;
784
785 /*
786 * This is the point of no return - everything below never fails except
787 * when the hw goes bonghits. Which means we can commit the new state on
788 * the software side now.
789 */
790
791 drm_atomic_helper_swap_state(dev, state);
792
793 /*
794 * Everything below can be run asynchronously without the need to grab
795 * any modeset locks at all under one conditions: It must be guaranteed
796 * that the asynchronous work has either been cancelled (if the driver
797 * supports it, which at least requires that the framebuffers get
798 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
799 * before the new state gets committed on the software side with
800 * drm_atomic_helper_swap_state().
801 *
802 * This scheme allows new atomic state updates to be prepared and
803 * checked in parallel to the asynchronous completion of the previous
804 * update. Which is important since compositors need to figure out the
805 * composition of the next frame right after having submitted the
806 * current layout.
807 */
808
809 drm_atomic_helper_commit_pre_planes(dev, state);
810
811 drm_atomic_helper_commit_planes(dev, state);
812
813 drm_atomic_helper_commit_post_planes(dev, state);
814
815 wait_for_vblanks(dev, state);
816
817 drm_atomic_helper_cleanup_planes(dev, state);
818
819 drm_atomic_state_free(state);
820
821 return 0;
822}
823EXPORT_SYMBOL(drm_atomic_helper_commit);
824
Daniel Vetterc2fcd272014-11-05 00:14:14 +0100825/**
826 * drm_atomic_helper_prepare_planes - prepare plane resources after commit
827 * @dev: DRM device
828 * @state: atomic state object with old state structures
829 *
830 * This function prepares plane state, specifically framebuffers, for the new
831 * configuration. If any failure is encountered this function will call
832 * ->cleanup_fb on any already successfully prepared framebuffer.
833 *
834 * Returns:
835 * 0 on success, negative error code on failure.
836 */
837int drm_atomic_helper_prepare_planes(struct drm_device *dev,
838 struct drm_atomic_state *state)
839{
840 int nplanes = dev->mode_config.num_total_plane;
841 int ret, i;
842
843 for (i = 0; i < nplanes; i++) {
844 struct drm_plane_helper_funcs *funcs;
845 struct drm_plane *plane = state->planes[i];
846 struct drm_framebuffer *fb;
847
848 if (!plane)
849 continue;
850
851 funcs = plane->helper_private;
852
853 fb = state->plane_states[i]->fb;
854
855 if (fb && funcs->prepare_fb) {
856 ret = funcs->prepare_fb(plane, fb);
857 if (ret)
858 goto fail;
859 }
860 }
861
862 return 0;
863
864fail:
865 for (i--; i >= 0; i--) {
866 struct drm_plane_helper_funcs *funcs;
867 struct drm_plane *plane = state->planes[i];
868 struct drm_framebuffer *fb;
869
870 if (!plane)
871 continue;
872
873 funcs = plane->helper_private;
874
875 fb = state->plane_states[i]->fb;
876
877 if (fb && funcs->cleanup_fb)
878 funcs->cleanup_fb(plane, fb);
879
880 }
881
882 return ret;
883}
884EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
885
886/**
887 * drm_atomic_helper_commit_planes - commit plane state
888 * @dev: DRM device
889 * @state: atomic state
890 *
891 * This function commits the new plane state using the plane and atomic helper
892 * functions for planes and crtcs. It assumes that the atomic state has already
893 * been pushed into the relevant object state pointers, since this step can no
894 * longer fail.
895 *
896 * It still requires the global state object @state to know which planes and
897 * crtcs need to be updated though.
898 */
899void drm_atomic_helper_commit_planes(struct drm_device *dev,
900 struct drm_atomic_state *state)
901{
902 int nplanes = dev->mode_config.num_total_plane;
903 int ncrtcs = dev->mode_config.num_crtc;
904 int i;
905
906 for (i = 0; i < ncrtcs; i++) {
907 struct drm_crtc_helper_funcs *funcs;
908 struct drm_crtc *crtc = state->crtcs[i];
909
910 if (!crtc)
911 continue;
912
913 funcs = crtc->helper_private;
914
915 if (!funcs || !funcs->atomic_begin)
916 continue;
917
918 funcs->atomic_begin(crtc);
919 }
920
921 for (i = 0; i < nplanes; i++) {
922 struct drm_plane_helper_funcs *funcs;
923 struct drm_plane *plane = state->planes[i];
924
925 if (!plane)
926 continue;
927
928 funcs = plane->helper_private;
929
930 if (!funcs || !funcs->atomic_update)
931 continue;
932
933 funcs->atomic_update(plane);
934 }
935
936 for (i = 0; i < ncrtcs; i++) {
937 struct drm_crtc_helper_funcs *funcs;
938 struct drm_crtc *crtc = state->crtcs[i];
939
940 if (!crtc)
941 continue;
942
943 funcs = crtc->helper_private;
944
945 if (!funcs || !funcs->atomic_flush)
946 continue;
947
948 funcs->atomic_flush(crtc);
949 }
950}
951EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
952
953/**
954 * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
955 * @dev: DRM device
956 * @old_state: atomic state object with old state structures
957 *
958 * This function cleans up plane state, specifically framebuffers, from the old
959 * configuration. Hence the old configuration must be perserved in @old_state to
960 * be able to call this function.
961 *
962 * This function must also be called on the new state when the atomic update
963 * fails at any point after calling drm_atomic_helper_prepare_planes().
964 */
965void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
966 struct drm_atomic_state *old_state)
967{
968 int nplanes = dev->mode_config.num_total_plane;
969 int i;
970
971 for (i = 0; i < nplanes; i++) {
972 struct drm_plane_helper_funcs *funcs;
973 struct drm_plane *plane = old_state->planes[i];
974 struct drm_framebuffer *old_fb;
975
976 if (!plane)
977 continue;
978
979 funcs = plane->helper_private;
980
981 old_fb = old_state->plane_states[i]->fb;
982
983 if (old_fb && funcs->cleanup_fb)
984 funcs->cleanup_fb(plane, old_fb);
985 }
986}
987EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
988
989/**
990 * drm_atomic_helper_swap_state - store atomic state into current sw state
991 * @dev: DRM device
992 * @state: atomic state
993 *
994 * This function stores the atomic state into the current state pointers in all
995 * driver objects. It should be called after all failing steps have been done
996 * and succeeded, but before the actual hardware state is committed.
997 *
998 * For cleanup and error recovery the current state for all changed objects will
999 * be swaped into @state.
1000 *
1001 * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
1002 *
1003 * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
1004 *
1005 * 2. Do any other steps that might fail.
1006 *
1007 * 3. Put the staged state into the current state pointers with this function.
1008 *
1009 * 4. Actually commit the hardware state.
1010 *
1011 * 5. Call drm_atomic_helper_cleanup_planes with @state, which since step 3
1012 * contains the old state. Also do any other cleanup required with that state.
1013 */
1014void drm_atomic_helper_swap_state(struct drm_device *dev,
1015 struct drm_atomic_state *state)
1016{
1017 int i;
1018
1019 for (i = 0; i < dev->mode_config.num_connector; i++) {
1020 struct drm_connector *connector = state->connectors[i];
1021
1022 if (!connector)
1023 continue;
1024
1025 connector->state->state = state;
1026 swap(state->connector_states[i], connector->state);
1027 connector->state->state = NULL;
1028 }
1029
1030 for (i = 0; i < dev->mode_config.num_crtc; i++) {
1031 struct drm_crtc *crtc = state->crtcs[i];
1032
1033 if (!crtc)
1034 continue;
1035
1036 crtc->state->state = state;
1037 swap(state->crtc_states[i], crtc->state);
1038 crtc->state->state = NULL;
1039 }
1040
1041 for (i = 0; i < dev->mode_config.num_total_plane; i++) {
1042 struct drm_plane *plane = state->planes[i];
1043
1044 if (!plane)
1045 continue;
1046
1047 plane->state->state = state;
1048 swap(state->plane_states[i], plane->state);
1049 plane->state->state = NULL;
1050 }
1051}
1052EXPORT_SYMBOL(drm_atomic_helper_swap_state);