a17b8e9c0a81c9b7b6bd47bc592e286f1d82bba9
[deliverable/linux.git] / drivers / gpu / drm / drm_atomic_helper.c
1 /*
2 * Copyright (C) 2014 Red Hat
3 * Copyright (C) 2014 Intel Corp.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Rob Clark <robdclark@gmail.com>
25 * Daniel Vetter <daniel.vetter@ffwll.ch>
26 */
27
28 #include <drm/drmP.h>
29 #include <drm/drm_atomic.h>
30 #include <drm/drm_plane_helper.h>
31 #include <drm/drm_crtc_helper.h>
32 #include <drm/drm_atomic_helper.h>
33 #include <linux/fence.h>
34
35 /**
36 * DOC: overview
37 *
38 * This helper library provides implementations of check and commit functions on
39 * top of the CRTC modeset helper callbacks and the plane helper callbacks. It
40 * also provides convenience implementations for the atomic state handling
41 * callbacks for drivers which don't need to subclass the drm core structures to
42 * add their own additional internal state.
43 *
44 * This library also provides default implementations for the check callback in
45 * drm_atomic_helper_check and for the commit callback with
46 * drm_atomic_helper_commit. But the individual stages and callbacks are expose
47 * to allow drivers to mix and match and e.g. use the plane helpers only
48 * together with a driver private modeset implementation.
49 *
50 * This library also provides implementations for all the legacy driver
51 * interfaces on top of the atomic interface. See drm_atomic_helper_set_config,
52 * drm_atomic_helper_disable_plane, drm_atomic_helper_disable_plane and the
53 * various functions to implement set_property callbacks. New drivers must not
54 * implement these functions themselves but must use the provided helpers.
55 */
56 static void
57 drm_atomic_helper_plane_changed(struct drm_atomic_state *state,
58 struct drm_plane_state *plane_state,
59 struct drm_plane *plane)
60 {
61 struct drm_crtc_state *crtc_state;
62
63 if (plane->state->crtc) {
64 crtc_state = state->crtc_states[drm_crtc_index(plane->crtc)];
65
66 if (WARN_ON(!crtc_state))
67 return;
68
69 crtc_state->planes_changed = true;
70 }
71
72 if (plane_state->crtc) {
73 crtc_state =
74 state->crtc_states[drm_crtc_index(plane_state->crtc)];
75
76 if (WARN_ON(!crtc_state))
77 return;
78
79 crtc_state->planes_changed = true;
80 }
81 }
82
83 static struct drm_crtc *
84 get_current_crtc_for_encoder(struct drm_device *dev,
85 struct drm_encoder *encoder)
86 {
87 struct drm_mode_config *config = &dev->mode_config;
88 struct drm_connector *connector;
89
90 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
91
92 list_for_each_entry(connector, &config->connector_list, head) {
93 if (connector->state->best_encoder != encoder)
94 continue;
95
96 return connector->state->crtc;
97 }
98
99 return NULL;
100 }
101
102 static int
103 steal_encoder(struct drm_atomic_state *state,
104 struct drm_encoder *encoder,
105 struct drm_crtc *encoder_crtc)
106 {
107 struct drm_mode_config *config = &state->dev->mode_config;
108 struct drm_crtc_state *crtc_state;
109 struct drm_connector *connector;
110 struct drm_connector_state *connector_state;
111 int ret;
112
113 /*
114 * We can only steal an encoder coming from a connector, which means we
115 * must already hold the connection_mutex.
116 */
117 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
118
119 DRM_DEBUG_KMS("[ENCODER:%d:%s] in use on [CRTC:%d], stealing it\n",
120 encoder->base.id, encoder->name,
121 encoder_crtc->base.id);
122
123 crtc_state = drm_atomic_get_crtc_state(state, encoder_crtc);
124 if (IS_ERR(crtc_state))
125 return PTR_ERR(crtc_state);
126
127 crtc_state->mode_changed = true;
128
129 list_for_each_entry(connector, &config->connector_list, head) {
130 if (connector->state->best_encoder != encoder)
131 continue;
132
133 DRM_DEBUG_KMS("Stealing encoder from [CONNECTOR:%d:%s]\n",
134 connector->base.id,
135 connector->name);
136
137 connector_state = drm_atomic_get_connector_state(state,
138 connector);
139 if (IS_ERR(connector_state))
140 return PTR_ERR(connector_state);
141
142 ret = drm_atomic_set_crtc_for_connector(connector_state, NULL);
143 if (ret)
144 return ret;
145 connector_state->best_encoder = NULL;
146 }
147
148 return 0;
149 }
150
151 static int
152 update_connector_routing(struct drm_atomic_state *state, int conn_idx)
153 {
154 struct drm_connector_helper_funcs *funcs;
155 struct drm_encoder *new_encoder;
156 struct drm_crtc *encoder_crtc;
157 struct drm_connector *connector;
158 struct drm_connector_state *connector_state;
159 struct drm_crtc_state *crtc_state;
160 int idx, ret;
161
162 connector = state->connectors[conn_idx];
163 connector_state = state->connector_states[conn_idx];
164
165 if (!connector)
166 return 0;
167
168 DRM_DEBUG_KMS("Updating routing for [CONNECTOR:%d:%s]\n",
169 connector->base.id,
170 connector->name);
171
172 if (connector->state->crtc != connector_state->crtc) {
173 if (connector->state->crtc) {
174 idx = drm_crtc_index(connector->state->crtc);
175
176 crtc_state = state->crtc_states[idx];
177 crtc_state->mode_changed = true;
178 }
179
180 if (connector_state->crtc) {
181 idx = drm_crtc_index(connector_state->crtc);
182
183 crtc_state = state->crtc_states[idx];
184 crtc_state->mode_changed = true;
185 }
186 }
187
188 if (!connector_state->crtc) {
189 DRM_DEBUG_KMS("Disabling [CONNECTOR:%d:%s]\n",
190 connector->base.id,
191 connector->name);
192
193 connector_state->best_encoder = NULL;
194
195 return 0;
196 }
197
198 funcs = connector->helper_private;
199 new_encoder = funcs->best_encoder(connector);
200
201 if (!new_encoder) {
202 DRM_DEBUG_KMS("No suitable encoder found for [CONNECTOR:%d:%s]\n",
203 connector->base.id,
204 connector->name);
205 return -EINVAL;
206 }
207
208 if (new_encoder == connector_state->best_encoder) {
209 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] keeps [ENCODER:%d:%s], now on [CRTC:%d]\n",
210 connector->base.id,
211 connector->name,
212 new_encoder->base.id,
213 new_encoder->name,
214 connector_state->crtc->base.id);
215
216 return 0;
217 }
218
219 encoder_crtc = get_current_crtc_for_encoder(state->dev,
220 new_encoder);
221
222 if (encoder_crtc) {
223 ret = steal_encoder(state, new_encoder, encoder_crtc);
224 if (ret) {
225 DRM_DEBUG_KMS("Encoder stealing failed for [CONNECTOR:%d:%s]\n",
226 connector->base.id,
227 connector->name);
228 return ret;
229 }
230 }
231
232 connector_state->best_encoder = new_encoder;
233 idx = drm_crtc_index(connector_state->crtc);
234
235 crtc_state = state->crtc_states[idx];
236 crtc_state->mode_changed = true;
237
238 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] using [ENCODER:%d:%s] on [CRTC:%d]\n",
239 connector->base.id,
240 connector->name,
241 new_encoder->base.id,
242 new_encoder->name,
243 connector_state->crtc->base.id);
244
245 return 0;
246 }
247
248 static int
249 mode_fixup(struct drm_atomic_state *state)
250 {
251 int ncrtcs = state->dev->mode_config.num_crtc;
252 struct drm_crtc_state *crtc_state;
253 struct drm_connector_state *conn_state;
254 int i;
255 bool ret;
256
257 for (i = 0; i < ncrtcs; i++) {
258 crtc_state = state->crtc_states[i];
259
260 if (!crtc_state || !crtc_state->mode_changed)
261 continue;
262
263 drm_mode_copy(&crtc_state->adjusted_mode, &crtc_state->mode);
264 }
265
266 for (i = 0; i < state->num_connector; i++) {
267 struct drm_encoder_helper_funcs *funcs;
268 struct drm_encoder *encoder;
269
270 conn_state = state->connector_states[i];
271
272 if (!conn_state)
273 continue;
274
275 WARN_ON(!!conn_state->best_encoder != !!conn_state->crtc);
276
277 if (!conn_state->crtc || !conn_state->best_encoder)
278 continue;
279
280 crtc_state =
281 state->crtc_states[drm_crtc_index(conn_state->crtc)];
282
283 /*
284 * Each encoder has at most one connector (since we always steal
285 * it away), so we won't call ->mode_fixup twice.
286 */
287 encoder = conn_state->best_encoder;
288 funcs = encoder->helper_private;
289
290 if (encoder->bridge && encoder->bridge->funcs->mode_fixup) {
291 ret = encoder->bridge->funcs->mode_fixup(
292 encoder->bridge, &crtc_state->mode,
293 &crtc_state->adjusted_mode);
294 if (!ret) {
295 DRM_DEBUG_KMS("Bridge fixup failed\n");
296 return -EINVAL;
297 }
298 }
299
300
301 ret = funcs->mode_fixup(encoder, &crtc_state->mode,
302 &crtc_state->adjusted_mode);
303 if (!ret) {
304 DRM_DEBUG_KMS("[ENCODER:%d:%s] fixup failed\n",
305 encoder->base.id, encoder->name);
306 return -EINVAL;
307 }
308 }
309
310 for (i = 0; i < ncrtcs; i++) {
311 struct drm_crtc_helper_funcs *funcs;
312 struct drm_crtc *crtc;
313
314 crtc_state = state->crtc_states[i];
315 crtc = state->crtcs[i];
316
317 if (!crtc_state || !crtc_state->mode_changed)
318 continue;
319
320 funcs = crtc->helper_private;
321 ret = funcs->mode_fixup(crtc, &crtc_state->mode,
322 &crtc_state->adjusted_mode);
323 if (!ret) {
324 DRM_DEBUG_KMS("[CRTC:%d] fixup failed\n",
325 crtc->base.id);
326 return -EINVAL;
327 }
328 }
329
330 return 0;
331 }
332
333 static int
334 drm_atomic_helper_check_modeset(struct drm_device *dev,
335 struct drm_atomic_state *state)
336 {
337 int ncrtcs = dev->mode_config.num_crtc;
338 struct drm_crtc *crtc;
339 struct drm_crtc_state *crtc_state;
340 int i, ret;
341
342 for (i = 0; i < ncrtcs; i++) {
343 crtc = state->crtcs[i];
344 crtc_state = state->crtc_states[i];
345
346 if (!crtc)
347 continue;
348
349 if (!drm_mode_equal(&crtc->state->mode, &crtc_state->mode)) {
350 DRM_DEBUG_KMS("[CRTC:%d] mode changed\n",
351 crtc->base.id);
352 crtc_state->mode_changed = true;
353 }
354
355 if (crtc->state->enable != crtc_state->enable) {
356 DRM_DEBUG_KMS("[CRTC:%d] enable changed\n",
357 crtc->base.id);
358 crtc_state->mode_changed = true;
359 }
360 }
361
362 for (i = 0; i < state->num_connector; i++) {
363 /*
364 * This only sets crtc->mode_changed for routing changes,
365 * drivers must set crtc->mode_changed themselves when connector
366 * properties need to be updated.
367 */
368 ret = update_connector_routing(state, i);
369 if (ret)
370 return ret;
371 }
372
373 /*
374 * After all the routing has been prepared we need to add in any
375 * connector which is itself unchanged, but who's crtc changes it's
376 * configuration. This must be done before calling mode_fixup in case a
377 * crtc only changed its mode but has the same set of connectors.
378 */
379 for (i = 0; i < ncrtcs; i++) {
380 int num_connectors;
381
382 crtc = state->crtcs[i];
383 crtc_state = state->crtc_states[i];
384
385 if (!crtc || !crtc_state->mode_changed)
386 continue;
387
388 DRM_DEBUG_KMS("[CRTC:%d] needs full modeset, enable: %c\n",
389 crtc->base.id,
390 crtc_state->enable ? 'y' : 'n');
391
392 ret = drm_atomic_add_affected_connectors(state, crtc);
393 if (ret != 0)
394 return ret;
395
396 num_connectors = drm_atomic_connectors_for_crtc(state,
397 crtc);
398
399 if (crtc_state->enable != !!num_connectors) {
400 DRM_DEBUG_KMS("[CRTC:%d] enabled/connectors mismatch\n",
401 crtc->base.id);
402
403 return -EINVAL;
404 }
405 }
406
407 return mode_fixup(state);
408 }
409
410 /**
411 * drm_atomic_helper_check - validate state object
412 * @dev: DRM device
413 * @state: the driver state object
414 *
415 * Check the state object to see if the requested state is physically possible.
416 * Only crtcs and planes have check callbacks, so for any additional (global)
417 * checking that a driver needs it can simply wrap that around this function.
418 * Drivers without such needs can directly use this as their ->atomic_check()
419 * callback.
420 *
421 * RETURNS
422 * Zero for success or -errno
423 */
424 int drm_atomic_helper_check(struct drm_device *dev,
425 struct drm_atomic_state *state)
426 {
427 int nplanes = dev->mode_config.num_total_plane;
428 int ncrtcs = dev->mode_config.num_crtc;
429 int i, ret = 0;
430
431 for (i = 0; i < nplanes; i++) {
432 struct drm_plane_helper_funcs *funcs;
433 struct drm_plane *plane = state->planes[i];
434 struct drm_plane_state *plane_state = state->plane_states[i];
435
436 if (!plane)
437 continue;
438
439 funcs = plane->helper_private;
440
441 drm_atomic_helper_plane_changed(state, plane_state, plane);
442
443 if (!funcs || !funcs->atomic_check)
444 continue;
445
446 ret = funcs->atomic_check(plane, plane_state);
447 if (ret) {
448 DRM_DEBUG_KMS("[PLANE:%d] atomic check failed\n",
449 plane->base.id);
450 return ret;
451 }
452 }
453
454 for (i = 0; i < ncrtcs; i++) {
455 struct drm_crtc_helper_funcs *funcs;
456 struct drm_crtc *crtc = state->crtcs[i];
457
458 if (!crtc)
459 continue;
460
461 funcs = crtc->helper_private;
462
463 if (!funcs || !funcs->atomic_check)
464 continue;
465
466 ret = funcs->atomic_check(crtc, state->crtc_states[i]);
467 if (ret) {
468 DRM_DEBUG_KMS("[CRTC:%d] atomic check failed\n",
469 crtc->base.id);
470 return ret;
471 }
472 }
473
474 ret = drm_atomic_helper_check_modeset(dev, state);
475 if (ret)
476 return ret;
477
478 return ret;
479 }
480 EXPORT_SYMBOL(drm_atomic_helper_check);
481
482 static void
483 disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state)
484 {
485 int ncrtcs = old_state->dev->mode_config.num_crtc;
486 int i;
487
488 for (i = 0; i < old_state->num_connector; i++) {
489 struct drm_connector_state *old_conn_state;
490 struct drm_connector *connector;
491 struct drm_encoder_helper_funcs *funcs;
492 struct drm_encoder *encoder;
493
494 old_conn_state = old_state->connector_states[i];
495 connector = old_state->connectors[i];
496
497 /* Shut down everything that's in the changeset and currently
498 * still on. So need to check the old, saved state. */
499 if (!old_conn_state || !old_conn_state->crtc)
500 continue;
501
502 encoder = old_conn_state->best_encoder;
503
504 /* We shouldn't get this far if we didn't previously have
505 * an encoder.. but WARN_ON() rather than explode.
506 */
507 if (WARN_ON(!encoder))
508 continue;
509
510 funcs = encoder->helper_private;
511
512 /*
513 * Each encoder has at most one connector (since we always steal
514 * it away), so we won't call call disable hooks twice.
515 */
516 if (encoder->bridge)
517 encoder->bridge->funcs->disable(encoder->bridge);
518
519 /* Right function depends upon target state. */
520 if (connector->state->crtc)
521 funcs->prepare(encoder);
522 else if (funcs->disable)
523 funcs->disable(encoder);
524 else
525 funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
526
527 if (encoder->bridge)
528 encoder->bridge->funcs->post_disable(encoder->bridge);
529 }
530
531 for (i = 0; i < ncrtcs; i++) {
532 struct drm_crtc_helper_funcs *funcs;
533 struct drm_crtc *crtc;
534
535 crtc = old_state->crtcs[i];
536
537 /* Shut down everything that needs a full modeset. */
538 if (!crtc || !crtc->state->mode_changed)
539 continue;
540
541 funcs = crtc->helper_private;
542
543 /* Right function depends upon target state. */
544 if (crtc->state->enable)
545 funcs->prepare(crtc);
546 else if (funcs->disable)
547 funcs->disable(crtc);
548 else
549 funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
550 }
551 }
552
553 static void
554 set_routing_links(struct drm_device *dev, struct drm_atomic_state *old_state)
555 {
556 int ncrtcs = old_state->dev->mode_config.num_crtc;
557 int i;
558
559 /* clear out existing links */
560 for (i = 0; i < old_state->num_connector; i++) {
561 struct drm_connector *connector;
562
563 connector = old_state->connectors[i];
564
565 if (!connector || !connector->encoder)
566 continue;
567
568 WARN_ON(!connector->encoder->crtc);
569
570 connector->encoder->crtc = NULL;
571 connector->encoder = NULL;
572 }
573
574 /* set new links */
575 for (i = 0; i < old_state->num_connector; i++) {
576 struct drm_connector *connector;
577
578 connector = old_state->connectors[i];
579
580 if (!connector || !connector->state->crtc)
581 continue;
582
583 if (WARN_ON(!connector->state->best_encoder))
584 continue;
585
586 connector->encoder = connector->state->best_encoder;
587 connector->encoder->crtc = connector->state->crtc;
588 }
589
590 /* set legacy state in the crtc structure */
591 for (i = 0; i < ncrtcs; i++) {
592 struct drm_crtc *crtc;
593
594 crtc = old_state->crtcs[i];
595
596 if (!crtc)
597 continue;
598
599 crtc->mode = crtc->state->mode;
600 crtc->enabled = crtc->state->enable;
601 crtc->x = crtc->primary->state->src_x >> 16;
602 crtc->y = crtc->primary->state->src_y >> 16;
603 }
604 }
605
606 static void
607 crtc_set_mode(struct drm_device *dev, struct drm_atomic_state *old_state)
608 {
609 int ncrtcs = old_state->dev->mode_config.num_crtc;
610 int i;
611
612 for (i = 0; i < ncrtcs; i++) {
613 struct drm_crtc_helper_funcs *funcs;
614 struct drm_crtc *crtc;
615
616 crtc = old_state->crtcs[i];
617
618 if (!crtc || !crtc->state->mode_changed)
619 continue;
620
621 funcs = crtc->helper_private;
622
623 if (crtc->state->enable)
624 funcs->mode_set_nofb(crtc);
625 }
626
627 for (i = 0; i < old_state->num_connector; i++) {
628 struct drm_connector *connector;
629 struct drm_crtc_state *new_crtc_state;
630 struct drm_encoder_helper_funcs *funcs;
631 struct drm_encoder *encoder;
632 struct drm_display_mode *mode, *adjusted_mode;
633
634 connector = old_state->connectors[i];
635
636 if (!connector || !connector->state->best_encoder)
637 continue;
638
639 encoder = connector->state->best_encoder;
640 funcs = encoder->helper_private;
641 new_crtc_state = connector->state->crtc->state;
642 mode = &new_crtc_state->mode;
643 adjusted_mode = &new_crtc_state->adjusted_mode;
644
645 /*
646 * Each encoder has at most one connector (since we always steal
647 * it away), so we won't call call mode_set hooks twice.
648 */
649 funcs->mode_set(encoder, mode, adjusted_mode);
650
651 if (encoder->bridge && encoder->bridge->funcs->mode_set)
652 encoder->bridge->funcs->mode_set(encoder->bridge,
653 mode, adjusted_mode);
654 }
655 }
656
657 /**
658 * drm_atomic_helper_commit_pre_planes - modeset commit before plane updates
659 * @dev: DRM device
660 * @state: atomic state
661 *
662 * This function commits the modeset changes that need to be committed before
663 * updating planes. It shuts down all the outputs that need to be shut down and
664 * prepares them (if required) with the new mode.
665 */
666 void drm_atomic_helper_commit_pre_planes(struct drm_device *dev,
667 struct drm_atomic_state *state)
668 {
669 disable_outputs(dev, state);
670 set_routing_links(dev, state);
671 crtc_set_mode(dev, state);
672 }
673 EXPORT_SYMBOL(drm_atomic_helper_commit_pre_planes);
674
675 /**
676 * drm_atomic_helper_commit_post_planes - modeset commit after plane updates
677 * @dev: DRM device
678 * @old_state: atomic state object with old state structures
679 *
680 * This function commits the modeset changes that need to be committed after
681 * updating planes: It enables all the outputs with the new configuration which
682 * had to be turned off for the update.
683 */
684 void drm_atomic_helper_commit_post_planes(struct drm_device *dev,
685 struct drm_atomic_state *old_state)
686 {
687 int ncrtcs = old_state->dev->mode_config.num_crtc;
688 int i;
689
690 for (i = 0; i < ncrtcs; i++) {
691 struct drm_crtc_helper_funcs *funcs;
692 struct drm_crtc *crtc;
693
694 crtc = old_state->crtcs[i];
695
696 /* Need to filter out CRTCs where only planes change. */
697 if (!crtc || !crtc->state->mode_changed)
698 continue;
699
700 funcs = crtc->helper_private;
701
702 if (crtc->state->enable)
703 funcs->commit(crtc);
704 }
705
706 for (i = 0; i < old_state->num_connector; i++) {
707 struct drm_connector *connector;
708 struct drm_encoder_helper_funcs *funcs;
709 struct drm_encoder *encoder;
710
711 connector = old_state->connectors[i];
712
713 if (!connector || !connector->state->best_encoder)
714 continue;
715
716 encoder = connector->state->best_encoder;
717 funcs = encoder->helper_private;
718
719 /*
720 * Each encoder has at most one connector (since we always steal
721 * it away), so we won't call call enable hooks twice.
722 */
723 if (encoder->bridge)
724 encoder->bridge->funcs->pre_enable(encoder->bridge);
725
726 funcs->commit(encoder);
727
728 if (encoder->bridge)
729 encoder->bridge->funcs->enable(encoder->bridge);
730 }
731 }
732 EXPORT_SYMBOL(drm_atomic_helper_commit_post_planes);
733
734 static void wait_for_fences(struct drm_device *dev,
735 struct drm_atomic_state *state)
736 {
737 int nplanes = dev->mode_config.num_total_plane;
738 int i;
739
740 for (i = 0; i < nplanes; i++) {
741 struct drm_plane *plane = state->planes[i];
742
743 if (!plane || !plane->state->fence)
744 continue;
745
746 WARN_ON(!plane->state->fb);
747
748 fence_wait(plane->state->fence, false);
749 fence_put(plane->state->fence);
750 plane->state->fence = NULL;
751 }
752 }
753
754 /**
755 * drm_atomic_helper_wait_for_vblanks - wait for vblank on crtcs
756 * @dev: DRM device
757 * @old_state: atomic state object with old state structures
758 *
759 * Helper to, after atomic commit, wait for vblanks on all effected
760 * crtcs (ie. before cleaning up old framebuffers using
761 * drm_atomic_helper_cleanup_planes())
762 */
763 void
764 drm_atomic_helper_wait_for_vblanks(struct drm_device *dev,
765 struct drm_atomic_state *old_state)
766 {
767 struct drm_crtc *crtc;
768 struct drm_crtc_state *old_crtc_state;
769 int ncrtcs = old_state->dev->mode_config.num_crtc;
770 int i, ret;
771
772 for (i = 0; i < ncrtcs; i++) {
773 crtc = old_state->crtcs[i];
774 old_crtc_state = old_state->crtc_states[i];
775
776 if (!crtc)
777 continue;
778
779 /* No one cares about the old state, so abuse it for tracking
780 * and store whether we hold a vblank reference (and should do a
781 * vblank wait) in the ->enable boolean. */
782 old_crtc_state->enable = false;
783
784 if (!crtc->state->enable)
785 continue;
786
787 ret = drm_crtc_vblank_get(crtc);
788 if (ret != 0)
789 continue;
790
791 old_crtc_state->enable = true;
792 old_crtc_state->last_vblank_count = drm_vblank_count(dev, i);
793 }
794
795 for (i = 0; i < ncrtcs; i++) {
796 crtc = old_state->crtcs[i];
797 old_crtc_state = old_state->crtc_states[i];
798
799 if (!crtc || !old_crtc_state->enable)
800 continue;
801
802 ret = wait_event_timeout(dev->vblank[i].queue,
803 old_crtc_state->last_vblank_count !=
804 drm_vblank_count(dev, i),
805 msecs_to_jiffies(50));
806
807 drm_crtc_vblank_put(crtc);
808 }
809 }
810 EXPORT_SYMBOL(drm_atomic_helper_wait_for_vblanks);
811
812 /**
813 * drm_atomic_helper_commit - commit validated state object
814 * @dev: DRM device
815 * @state: the driver state object
816 * @async: asynchronous commit
817 *
818 * This function commits a with drm_atomic_helper_check() pre-validated state
819 * object. This can still fail when e.g. the framebuffer reservation fails. For
820 * now this doesn't implement asynchronous commits.
821 *
822 * RETURNS
823 * Zero for success or -errno.
824 */
825 int drm_atomic_helper_commit(struct drm_device *dev,
826 struct drm_atomic_state *state,
827 bool async)
828 {
829 int ret;
830
831 if (async)
832 return -EBUSY;
833
834 ret = drm_atomic_helper_prepare_planes(dev, state);
835 if (ret)
836 return ret;
837
838 /*
839 * This is the point of no return - everything below never fails except
840 * when the hw goes bonghits. Which means we can commit the new state on
841 * the software side now.
842 */
843
844 drm_atomic_helper_swap_state(dev, state);
845
846 /*
847 * Everything below can be run asynchronously without the need to grab
848 * any modeset locks at all under one conditions: It must be guaranteed
849 * that the asynchronous work has either been cancelled (if the driver
850 * supports it, which at least requires that the framebuffers get
851 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
852 * before the new state gets committed on the software side with
853 * drm_atomic_helper_swap_state().
854 *
855 * This scheme allows new atomic state updates to be prepared and
856 * checked in parallel to the asynchronous completion of the previous
857 * update. Which is important since compositors need to figure out the
858 * composition of the next frame right after having submitted the
859 * current layout.
860 */
861
862 wait_for_fences(dev, state);
863
864 drm_atomic_helper_commit_pre_planes(dev, state);
865
866 drm_atomic_helper_commit_planes(dev, state);
867
868 drm_atomic_helper_commit_post_planes(dev, state);
869
870 drm_atomic_helper_wait_for_vblanks(dev, state);
871
872 drm_atomic_helper_cleanup_planes(dev, state);
873
874 drm_atomic_state_free(state);
875
876 return 0;
877 }
878 EXPORT_SYMBOL(drm_atomic_helper_commit);
879
880 /**
881 * DOC: implementing async commit
882 *
883 * For now the atomic helpers don't support async commit directly. If there is
884 * real need it could be added though, using the dma-buf fence infrastructure
885 * for generic synchronization with outstanding rendering.
886 *
887 * For now drivers have to implement async commit themselves, with the following
888 * sequence being the recommended one:
889 *
890 * 1. Run drm_atomic_helper_prepare_planes() first. This is the only function
891 * which commit needs to call which can fail, so we want to run it first and
892 * synchronously.
893 *
894 * 2. Synchronize with any outstanding asynchronous commit worker threads which
895 * might be affected the new state update. This can be done by either cancelling
896 * or flushing the work items, depending upon whether the driver can deal with
897 * cancelled updates. Note that it is important to ensure that the framebuffer
898 * cleanup is still done when cancelling.
899 *
900 * For sufficient parallelism it is recommended to have a work item per crtc
901 * (for updates which don't touch global state) and a global one. Then we only
902 * need to synchronize with the crtc work items for changed crtcs and the global
903 * work item, which allows nice concurrent updates on disjoint sets of crtcs.
904 *
905 * 3. The software state is updated synchronously with
906 * drm_atomic_helper_swap_state. Doing this under the protection of all modeset
907 * locks means concurrent callers never see inconsistent state. And doing this
908 * while it's guaranteed that no relevant async worker runs means that async
909 * workers do not need grab any locks. Actually they must not grab locks, for
910 * otherwise the work flushing will deadlock.
911 *
912 * 4. Schedule a work item to do all subsequent steps, using the split-out
913 * commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and
914 * then cleaning up the framebuffers after the old framebuffer is no longer
915 * being displayed.
916 */
917
918 /**
919 * drm_atomic_helper_prepare_planes - prepare plane resources after commit
920 * @dev: DRM device
921 * @state: atomic state object with old state structures
922 *
923 * This function prepares plane state, specifically framebuffers, for the new
924 * configuration. If any failure is encountered this function will call
925 * ->cleanup_fb on any already successfully prepared framebuffer.
926 *
927 * Returns:
928 * 0 on success, negative error code on failure.
929 */
930 int drm_atomic_helper_prepare_planes(struct drm_device *dev,
931 struct drm_atomic_state *state)
932 {
933 int nplanes = dev->mode_config.num_total_plane;
934 int ret, i;
935
936 for (i = 0; i < nplanes; i++) {
937 struct drm_plane_helper_funcs *funcs;
938 struct drm_plane *plane = state->planes[i];
939 struct drm_framebuffer *fb;
940
941 if (!plane)
942 continue;
943
944 funcs = plane->helper_private;
945
946 fb = state->plane_states[i]->fb;
947
948 if (fb && funcs->prepare_fb) {
949 ret = funcs->prepare_fb(plane, fb);
950 if (ret)
951 goto fail;
952 }
953 }
954
955 return 0;
956
957 fail:
958 for (i--; i >= 0; i--) {
959 struct drm_plane_helper_funcs *funcs;
960 struct drm_plane *plane = state->planes[i];
961 struct drm_framebuffer *fb;
962
963 if (!plane)
964 continue;
965
966 funcs = plane->helper_private;
967
968 fb = state->plane_states[i]->fb;
969
970 if (fb && funcs->cleanup_fb)
971 funcs->cleanup_fb(plane, fb);
972
973 }
974
975 return ret;
976 }
977 EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
978
979 /**
980 * drm_atomic_helper_commit_planes - commit plane state
981 * @dev: DRM device
982 * @old_state: atomic state object with old state structures
983 *
984 * This function commits the new plane state using the plane and atomic helper
985 * functions for planes and crtcs. It assumes that the atomic state has already
986 * been pushed into the relevant object state pointers, since this step can no
987 * longer fail.
988 *
989 * It still requires the global state object @old_state to know which planes and
990 * crtcs need to be updated though.
991 */
992 void drm_atomic_helper_commit_planes(struct drm_device *dev,
993 struct drm_atomic_state *old_state)
994 {
995 int nplanes = dev->mode_config.num_total_plane;
996 int ncrtcs = dev->mode_config.num_crtc;
997 int i;
998
999 for (i = 0; i < ncrtcs; i++) {
1000 struct drm_crtc_helper_funcs *funcs;
1001 struct drm_crtc *crtc = old_state->crtcs[i];
1002
1003 if (!crtc)
1004 continue;
1005
1006 funcs = crtc->helper_private;
1007
1008 if (!funcs || !funcs->atomic_begin)
1009 continue;
1010
1011 funcs->atomic_begin(crtc);
1012 }
1013
1014 for (i = 0; i < nplanes; i++) {
1015 struct drm_plane_helper_funcs *funcs;
1016 struct drm_plane *plane = old_state->planes[i];
1017
1018 if (!plane)
1019 continue;
1020
1021 funcs = plane->helper_private;
1022
1023 if (!funcs || !funcs->atomic_update)
1024 continue;
1025
1026 funcs->atomic_update(plane);
1027 }
1028
1029 for (i = 0; i < ncrtcs; i++) {
1030 struct drm_crtc_helper_funcs *funcs;
1031 struct drm_crtc *crtc = old_state->crtcs[i];
1032
1033 if (!crtc)
1034 continue;
1035
1036 funcs = crtc->helper_private;
1037
1038 if (!funcs || !funcs->atomic_flush)
1039 continue;
1040
1041 funcs->atomic_flush(crtc);
1042 }
1043 }
1044 EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
1045
1046 /**
1047 * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
1048 * @dev: DRM device
1049 * @old_state: atomic state object with old state structures
1050 *
1051 * This function cleans up plane state, specifically framebuffers, from the old
1052 * configuration. Hence the old configuration must be perserved in @old_state to
1053 * be able to call this function.
1054 *
1055 * This function must also be called on the new state when the atomic update
1056 * fails at any point after calling drm_atomic_helper_prepare_planes().
1057 */
1058 void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
1059 struct drm_atomic_state *old_state)
1060 {
1061 int nplanes = dev->mode_config.num_total_plane;
1062 int i;
1063
1064 for (i = 0; i < nplanes; i++) {
1065 struct drm_plane_helper_funcs *funcs;
1066 struct drm_plane *plane = old_state->planes[i];
1067 struct drm_framebuffer *old_fb;
1068
1069 if (!plane)
1070 continue;
1071
1072 funcs = plane->helper_private;
1073
1074 old_fb = old_state->plane_states[i]->fb;
1075
1076 if (old_fb && funcs->cleanup_fb)
1077 funcs->cleanup_fb(plane, old_fb);
1078 }
1079 }
1080 EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
1081
1082 /**
1083 * drm_atomic_helper_swap_state - store atomic state into current sw state
1084 * @dev: DRM device
1085 * @state: atomic state
1086 *
1087 * This function stores the atomic state into the current state pointers in all
1088 * driver objects. It should be called after all failing steps have been done
1089 * and succeeded, but before the actual hardware state is committed.
1090 *
1091 * For cleanup and error recovery the current state for all changed objects will
1092 * be swaped into @state.
1093 *
1094 * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
1095 *
1096 * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
1097 *
1098 * 2. Do any other steps that might fail.
1099 *
1100 * 3. Put the staged state into the current state pointers with this function.
1101 *
1102 * 4. Actually commit the hardware state.
1103 *
1104 * 5. Call drm_atomic_helper_cleanup_planes with @state, which since step 3
1105 * contains the old state. Also do any other cleanup required with that state.
1106 */
1107 void drm_atomic_helper_swap_state(struct drm_device *dev,
1108 struct drm_atomic_state *state)
1109 {
1110 int i;
1111
1112 for (i = 0; i < dev->mode_config.num_connector; i++) {
1113 struct drm_connector *connector = state->connectors[i];
1114
1115 if (!connector)
1116 continue;
1117
1118 connector->state->state = state;
1119 swap(state->connector_states[i], connector->state);
1120 connector->state->state = NULL;
1121 }
1122
1123 for (i = 0; i < dev->mode_config.num_crtc; i++) {
1124 struct drm_crtc *crtc = state->crtcs[i];
1125
1126 if (!crtc)
1127 continue;
1128
1129 crtc->state->state = state;
1130 swap(state->crtc_states[i], crtc->state);
1131 crtc->state->state = NULL;
1132 }
1133
1134 for (i = 0; i < dev->mode_config.num_total_plane; i++) {
1135 struct drm_plane *plane = state->planes[i];
1136
1137 if (!plane)
1138 continue;
1139
1140 plane->state->state = state;
1141 swap(state->plane_states[i], plane->state);
1142 plane->state->state = NULL;
1143 }
1144 }
1145 EXPORT_SYMBOL(drm_atomic_helper_swap_state);
1146
1147 /**
1148 * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
1149 * @plane: plane object to update
1150 * @crtc: owning CRTC of owning plane
1151 * @fb: framebuffer to flip onto plane
1152 * @crtc_x: x offset of primary plane on crtc
1153 * @crtc_y: y offset of primary plane on crtc
1154 * @crtc_w: width of primary plane rectangle on crtc
1155 * @crtc_h: height of primary plane rectangle on crtc
1156 * @src_x: x offset of @fb for panning
1157 * @src_y: y offset of @fb for panning
1158 * @src_w: width of source rectangle in @fb
1159 * @src_h: height of source rectangle in @fb
1160 *
1161 * Provides a default plane update handler using the atomic driver interface.
1162 *
1163 * RETURNS:
1164 * Zero on success, error code on failure
1165 */
1166 int drm_atomic_helper_update_plane(struct drm_plane *plane,
1167 struct drm_crtc *crtc,
1168 struct drm_framebuffer *fb,
1169 int crtc_x, int crtc_y,
1170 unsigned int crtc_w, unsigned int crtc_h,
1171 uint32_t src_x, uint32_t src_y,
1172 uint32_t src_w, uint32_t src_h)
1173 {
1174 struct drm_atomic_state *state;
1175 struct drm_plane_state *plane_state;
1176 int ret = 0;
1177
1178 state = drm_atomic_state_alloc(plane->dev);
1179 if (!state)
1180 return -ENOMEM;
1181
1182 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1183 retry:
1184 plane_state = drm_atomic_get_plane_state(state, plane);
1185 if (IS_ERR(plane_state)) {
1186 ret = PTR_ERR(plane_state);
1187 goto fail;
1188 }
1189
1190 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
1191 if (ret != 0)
1192 goto fail;
1193 drm_atomic_set_fb_for_plane(plane_state, fb);
1194 plane_state->crtc_x = crtc_x;
1195 plane_state->crtc_y = crtc_y;
1196 plane_state->crtc_h = crtc_h;
1197 plane_state->crtc_w = crtc_w;
1198 plane_state->src_x = src_x;
1199 plane_state->src_y = src_y;
1200 plane_state->src_h = src_h;
1201 plane_state->src_w = src_w;
1202
1203 ret = drm_atomic_commit(state);
1204 if (ret != 0)
1205 goto fail;
1206
1207 /* Driver takes ownership of state on successful commit. */
1208 return 0;
1209 fail:
1210 if (ret == -EDEADLK)
1211 goto backoff;
1212
1213 drm_atomic_state_free(state);
1214
1215 return ret;
1216 backoff:
1217 drm_atomic_state_clear(state);
1218 drm_atomic_legacy_backoff(state);
1219
1220 /*
1221 * Someone might have exchanged the framebuffer while we dropped locks
1222 * in the backoff code. We need to fix up the fb refcount tracking the
1223 * core does for us.
1224 */
1225 plane->old_fb = plane->fb;
1226
1227 goto retry;
1228 }
1229 EXPORT_SYMBOL(drm_atomic_helper_update_plane);
1230
1231 /**
1232 * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
1233 * @plane: plane to disable
1234 *
1235 * Provides a default plane disable handler using the atomic driver interface.
1236 *
1237 * RETURNS:
1238 * Zero on success, error code on failure
1239 */
1240 int drm_atomic_helper_disable_plane(struct drm_plane *plane)
1241 {
1242 struct drm_atomic_state *state;
1243 struct drm_plane_state *plane_state;
1244 int ret = 0;
1245
1246 state = drm_atomic_state_alloc(plane->dev);
1247 if (!state)
1248 return -ENOMEM;
1249
1250 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(plane->crtc);
1251 retry:
1252 plane_state = drm_atomic_get_plane_state(state, plane);
1253 if (IS_ERR(plane_state)) {
1254 ret = PTR_ERR(plane_state);
1255 goto fail;
1256 }
1257
1258 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
1259 if (ret != 0)
1260 goto fail;
1261 drm_atomic_set_fb_for_plane(plane_state, NULL);
1262 plane_state->crtc_x = 0;
1263 plane_state->crtc_y = 0;
1264 plane_state->crtc_h = 0;
1265 plane_state->crtc_w = 0;
1266 plane_state->src_x = 0;
1267 plane_state->src_y = 0;
1268 plane_state->src_h = 0;
1269 plane_state->src_w = 0;
1270
1271 ret = drm_atomic_commit(state);
1272 if (ret != 0)
1273 goto fail;
1274
1275 /* Driver takes ownership of state on successful commit. */
1276 return 0;
1277 fail:
1278 if (ret == -EDEADLK)
1279 goto backoff;
1280
1281 drm_atomic_state_free(state);
1282
1283 return ret;
1284 backoff:
1285 drm_atomic_state_clear(state);
1286 drm_atomic_legacy_backoff(state);
1287
1288 /*
1289 * Someone might have exchanged the framebuffer while we dropped locks
1290 * in the backoff code. We need to fix up the fb refcount tracking the
1291 * core does for us.
1292 */
1293 plane->old_fb = plane->fb;
1294
1295 goto retry;
1296 }
1297 EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
1298
1299 static int update_output_state(struct drm_atomic_state *state,
1300 struct drm_mode_set *set)
1301 {
1302 struct drm_device *dev = set->crtc->dev;
1303 struct drm_connector_state *conn_state;
1304 int ncrtcs = state->dev->mode_config.num_crtc;
1305 int ret, i, j;
1306
1307 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
1308 state->acquire_ctx);
1309 if (ret)
1310 return ret;
1311
1312 /* First grab all affected connector/crtc states. */
1313 for (i = 0; i < set->num_connectors; i++) {
1314 conn_state = drm_atomic_get_connector_state(state,
1315 set->connectors[i]);
1316 if (IS_ERR(conn_state))
1317 return PTR_ERR(conn_state);
1318 }
1319
1320 for (i = 0; i < ncrtcs; i++) {
1321 struct drm_crtc *crtc = state->crtcs[i];
1322
1323 if (!crtc)
1324 continue;
1325
1326 ret = drm_atomic_add_affected_connectors(state, crtc);
1327 if (ret)
1328 return ret;
1329 }
1330
1331 /* Then recompute connector->crtc links and crtc enabling state. */
1332 for (i = 0; i < state->num_connector; i++) {
1333 struct drm_connector *connector;
1334
1335 connector = state->connectors[i];
1336 conn_state = state->connector_states[i];
1337
1338 if (!connector)
1339 continue;
1340
1341 if (conn_state->crtc == set->crtc) {
1342 ret = drm_atomic_set_crtc_for_connector(conn_state,
1343 NULL);
1344 if (ret)
1345 return ret;
1346 }
1347
1348 for (j = 0; j < set->num_connectors; j++) {
1349 if (set->connectors[j] == connector) {
1350 ret = drm_atomic_set_crtc_for_connector(conn_state,
1351 set->crtc);
1352 if (ret)
1353 return ret;
1354 break;
1355 }
1356 }
1357 }
1358
1359 for (i = 0; i < ncrtcs; i++) {
1360 struct drm_crtc *crtc = state->crtcs[i];
1361 struct drm_crtc_state *crtc_state = state->crtc_states[i];
1362
1363 if (!crtc)
1364 continue;
1365
1366 /* Don't update ->enable for the CRTC in the set_config request,
1367 * since a mismatch would indicate a bug in the upper layers.
1368 * The actual modeset code later on will catch any
1369 * inconsistencies here. */
1370 if (crtc == set->crtc)
1371 continue;
1372
1373 crtc_state->enable =
1374 drm_atomic_connectors_for_crtc(state, crtc);
1375 }
1376
1377 return 0;
1378 }
1379
1380 /**
1381 * drm_atomic_helper_set_config - set a new config from userspace
1382 * @set: mode set configuration
1383 *
1384 * Provides a default crtc set_config handler using the atomic driver interface.
1385 *
1386 * Returns:
1387 * Returns 0 on success, negative errno numbers on failure.
1388 */
1389 int drm_atomic_helper_set_config(struct drm_mode_set *set)
1390 {
1391 struct drm_atomic_state *state;
1392 struct drm_crtc *crtc = set->crtc;
1393 struct drm_crtc_state *crtc_state;
1394 struct drm_plane_state *primary_state;
1395 int ret = 0;
1396
1397 state = drm_atomic_state_alloc(crtc->dev);
1398 if (!state)
1399 return -ENOMEM;
1400
1401 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1402 retry:
1403 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1404 if (IS_ERR(crtc_state)) {
1405 ret = PTR_ERR(crtc_state);
1406 goto fail;
1407 }
1408
1409 if (!set->mode) {
1410 WARN_ON(set->fb);
1411 WARN_ON(set->num_connectors);
1412
1413 crtc_state->enable = false;
1414 goto commit;
1415 }
1416
1417 WARN_ON(!set->fb);
1418 WARN_ON(!set->num_connectors);
1419
1420 crtc_state->enable = true;
1421 drm_mode_copy(&crtc_state->mode, set->mode);
1422
1423 primary_state = drm_atomic_get_plane_state(state, crtc->primary);
1424 if (IS_ERR(primary_state)) {
1425 ret = PTR_ERR(primary_state);
1426 goto fail;
1427 }
1428
1429 ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
1430 if (ret != 0)
1431 goto fail;
1432 drm_atomic_set_fb_for_plane(primary_state, set->fb);
1433 primary_state->crtc_x = 0;
1434 primary_state->crtc_y = 0;
1435 primary_state->crtc_h = set->mode->vdisplay;
1436 primary_state->crtc_w = set->mode->hdisplay;
1437 primary_state->src_x = set->x << 16;
1438 primary_state->src_y = set->y << 16;
1439 primary_state->src_h = set->mode->vdisplay << 16;
1440 primary_state->src_w = set->mode->hdisplay << 16;
1441
1442 commit:
1443 ret = update_output_state(state, set);
1444 if (ret)
1445 goto fail;
1446
1447 ret = drm_atomic_commit(state);
1448 if (ret != 0)
1449 goto fail;
1450
1451 /* Driver takes ownership of state on successful commit. */
1452 return 0;
1453 fail:
1454 if (ret == -EDEADLK)
1455 goto backoff;
1456
1457 drm_atomic_state_free(state);
1458
1459 return ret;
1460 backoff:
1461 drm_atomic_state_clear(state);
1462 drm_atomic_legacy_backoff(state);
1463
1464 /*
1465 * Someone might have exchanged the framebuffer while we dropped locks
1466 * in the backoff code. We need to fix up the fb refcount tracking the
1467 * core does for us.
1468 */
1469 crtc->primary->old_fb = crtc->primary->fb;
1470
1471 goto retry;
1472 }
1473 EXPORT_SYMBOL(drm_atomic_helper_set_config);
1474
1475 /**
1476 * drm_atomic_helper_crtc_set_property - helper for crtc prorties
1477 * @crtc: DRM crtc
1478 * @property: DRM property
1479 * @val: value of property
1480 *
1481 * Provides a default plane disablle handler using the atomic driver interface.
1482 *
1483 * RETURNS:
1484 * Zero on success, error code on failure
1485 */
1486 int
1487 drm_atomic_helper_crtc_set_property(struct drm_crtc *crtc,
1488 struct drm_property *property,
1489 uint64_t val)
1490 {
1491 struct drm_atomic_state *state;
1492 struct drm_crtc_state *crtc_state;
1493 int ret = 0;
1494
1495 state = drm_atomic_state_alloc(crtc->dev);
1496 if (!state)
1497 return -ENOMEM;
1498
1499 /* ->set_property is always called with all locks held. */
1500 state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
1501 retry:
1502 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1503 if (IS_ERR(crtc_state)) {
1504 ret = PTR_ERR(crtc_state);
1505 goto fail;
1506 }
1507
1508 ret = crtc->funcs->atomic_set_property(crtc, crtc_state,
1509 property, val);
1510 if (ret)
1511 goto fail;
1512
1513 ret = drm_atomic_commit(state);
1514 if (ret != 0)
1515 goto fail;
1516
1517 /* Driver takes ownership of state on successful commit. */
1518 return 0;
1519 fail:
1520 if (ret == -EDEADLK)
1521 goto backoff;
1522
1523 drm_atomic_state_free(state);
1524
1525 return ret;
1526 backoff:
1527 drm_atomic_state_clear(state);
1528 drm_atomic_legacy_backoff(state);
1529
1530 goto retry;
1531 }
1532 EXPORT_SYMBOL(drm_atomic_helper_crtc_set_property);
1533
1534 /**
1535 * drm_atomic_helper_plane_set_property - helper for plane prorties
1536 * @plane: DRM plane
1537 * @property: DRM property
1538 * @val: value of property
1539 *
1540 * Provides a default plane disable handler using the atomic driver interface.
1541 *
1542 * RETURNS:
1543 * Zero on success, error code on failure
1544 */
1545 int
1546 drm_atomic_helper_plane_set_property(struct drm_plane *plane,
1547 struct drm_property *property,
1548 uint64_t val)
1549 {
1550 struct drm_atomic_state *state;
1551 struct drm_plane_state *plane_state;
1552 int ret = 0;
1553
1554 state = drm_atomic_state_alloc(plane->dev);
1555 if (!state)
1556 return -ENOMEM;
1557
1558 /* ->set_property is always called with all locks held. */
1559 state->acquire_ctx = plane->dev->mode_config.acquire_ctx;
1560 retry:
1561 plane_state = drm_atomic_get_plane_state(state, plane);
1562 if (IS_ERR(plane_state)) {
1563 ret = PTR_ERR(plane_state);
1564 goto fail;
1565 }
1566
1567 ret = plane->funcs->atomic_set_property(plane, plane_state,
1568 property, val);
1569 if (ret)
1570 goto fail;
1571
1572 ret = drm_atomic_commit(state);
1573 if (ret != 0)
1574 goto fail;
1575
1576 /* Driver takes ownership of state on successful commit. */
1577 return 0;
1578 fail:
1579 if (ret == -EDEADLK)
1580 goto backoff;
1581
1582 drm_atomic_state_free(state);
1583
1584 return ret;
1585 backoff:
1586 drm_atomic_state_clear(state);
1587 drm_atomic_legacy_backoff(state);
1588
1589 goto retry;
1590 }
1591 EXPORT_SYMBOL(drm_atomic_helper_plane_set_property);
1592
1593 /**
1594 * drm_atomic_helper_connector_set_property - helper for connector prorties
1595 * @connector: DRM connector
1596 * @property: DRM property
1597 * @val: value of property
1598 *
1599 * Provides a default plane disablle handler using the atomic driver interface.
1600 *
1601 * RETURNS:
1602 * Zero on success, error code on failure
1603 */
1604 int
1605 drm_atomic_helper_connector_set_property(struct drm_connector *connector,
1606 struct drm_property *property,
1607 uint64_t val)
1608 {
1609 struct drm_atomic_state *state;
1610 struct drm_connector_state *connector_state;
1611 int ret = 0;
1612
1613 state = drm_atomic_state_alloc(connector->dev);
1614 if (!state)
1615 return -ENOMEM;
1616
1617 /* ->set_property is always called with all locks held. */
1618 state->acquire_ctx = connector->dev->mode_config.acquire_ctx;
1619 retry:
1620 connector_state = drm_atomic_get_connector_state(state, connector);
1621 if (IS_ERR(connector_state)) {
1622 ret = PTR_ERR(connector_state);
1623 goto fail;
1624 }
1625
1626 ret = connector->funcs->atomic_set_property(connector, connector_state,
1627 property, val);
1628 if (ret)
1629 goto fail;
1630
1631 ret = drm_atomic_commit(state);
1632 if (ret != 0)
1633 goto fail;
1634
1635 /* Driver takes ownership of state on successful commit. */
1636 return 0;
1637 fail:
1638 if (ret == -EDEADLK)
1639 goto backoff;
1640
1641 drm_atomic_state_free(state);
1642
1643 return ret;
1644 backoff:
1645 drm_atomic_state_clear(state);
1646 drm_atomic_legacy_backoff(state);
1647
1648 goto retry;
1649 }
1650 EXPORT_SYMBOL(drm_atomic_helper_connector_set_property);
1651
1652 /**
1653 * drm_atomic_helper_page_flip - execute a legacy page flip
1654 * @crtc: DRM crtc
1655 * @fb: DRM framebuffer
1656 * @event: optional DRM event to signal upon completion
1657 * @flags: flip flags for non-vblank sync'ed updates
1658 *
1659 * Provides a default page flip implementation using the atomic driver interface.
1660 *
1661 * Note that for now so called async page flips (i.e. updates which are not
1662 * synchronized to vblank) are not supported, since the atomic interfaces have
1663 * no provisions for this yet.
1664 *
1665 * Returns:
1666 * Returns 0 on success, negative errno numbers on failure.
1667 */
1668 int drm_atomic_helper_page_flip(struct drm_crtc *crtc,
1669 struct drm_framebuffer *fb,
1670 struct drm_pending_vblank_event *event,
1671 uint32_t flags)
1672 {
1673 struct drm_plane *plane = crtc->primary;
1674 struct drm_atomic_state *state;
1675 struct drm_plane_state *plane_state;
1676 struct drm_crtc_state *crtc_state;
1677 int ret = 0;
1678
1679 if (flags & DRM_MODE_PAGE_FLIP_ASYNC)
1680 return -EINVAL;
1681
1682 state = drm_atomic_state_alloc(plane->dev);
1683 if (!state)
1684 return -ENOMEM;
1685
1686 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1687 retry:
1688 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1689 if (IS_ERR(crtc_state)) {
1690 ret = PTR_ERR(crtc_state);
1691 goto fail;
1692 }
1693 crtc_state->event = event;
1694
1695 plane_state = drm_atomic_get_plane_state(state, plane);
1696 if (IS_ERR(plane_state)) {
1697 ret = PTR_ERR(plane_state);
1698 goto fail;
1699 }
1700
1701 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
1702 if (ret != 0)
1703 goto fail;
1704 drm_atomic_set_fb_for_plane(plane_state, fb);
1705
1706 ret = drm_atomic_async_commit(state);
1707 if (ret != 0)
1708 goto fail;
1709
1710 /* TODO: ->page_flip is the only driver callback where the core
1711 * doesn't update plane->fb. For now patch it up here. */
1712 plane->fb = plane->state->fb;
1713
1714 /* Driver takes ownership of state on successful async commit. */
1715 return 0;
1716 fail:
1717 if (ret == -EDEADLK)
1718 goto backoff;
1719
1720 drm_atomic_state_free(state);
1721
1722 return ret;
1723 backoff:
1724 drm_atomic_state_clear(state);
1725 drm_atomic_legacy_backoff(state);
1726
1727 /*
1728 * Someone might have exchanged the framebuffer while we dropped locks
1729 * in the backoff code. We need to fix up the fb refcount tracking the
1730 * core does for us.
1731 */
1732 plane->old_fb = plane->fb;
1733
1734 goto retry;
1735 }
1736 EXPORT_SYMBOL(drm_atomic_helper_page_flip);
1737
1738 /**
1739 * DOC: atomic state reset and initialization
1740 *
1741 * Both the drm core and the atomic helpers assume that there is always the full
1742 * and correct atomic software state for all connectors, CRTCs and planes
1743 * available. Which is a bit a problem on driver load and also after system
1744 * suspend. One way to solve this is to have a hardware state read-out
1745 * infrastructure which reconstructs the full software state (e.g. the i915
1746 * driver).
1747 *
1748 * The simpler solution is to just reset the software state to everything off,
1749 * which is easiest to do by calling drm_mode_config_reset(). To facilitate this
1750 * the atomic helpers provide default reset implementations for all hooks.
1751 */
1752
1753 /**
1754 * drm_atomic_helper_crtc_reset - default ->reset hook for CRTCs
1755 * @crtc: drm CRTC
1756 *
1757 * Resets the atomic state for @crtc by freeing the state pointer (which might
1758 * be NULL, e.g. at driver load time) and allocating a new empty state object.
1759 */
1760 void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc)
1761 {
1762 kfree(crtc->state);
1763 crtc->state = kzalloc(sizeof(*crtc->state), GFP_KERNEL);
1764 }
1765 EXPORT_SYMBOL(drm_atomic_helper_crtc_reset);
1766
1767 /**
1768 * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
1769 * @crtc: drm CRTC
1770 *
1771 * Default CRTC state duplicate hook for drivers which don't have their own
1772 * subclassed CRTC state structure.
1773 */
1774 struct drm_crtc_state *
1775 drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc)
1776 {
1777 struct drm_crtc_state *state;
1778
1779 if (WARN_ON(!crtc->state))
1780 return NULL;
1781
1782 state = kmemdup(crtc->state, sizeof(*crtc->state), GFP_KERNEL);
1783
1784 if (state) {
1785 state->mode_changed = false;
1786 state->planes_changed = false;
1787 state->event = NULL;
1788 }
1789
1790 return state;
1791 }
1792 EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state);
1793
1794 /**
1795 * drm_atomic_helper_crtc_destroy_state - default state destroy hook
1796 * @crtc: drm CRTC
1797 * @state: CRTC state object to release
1798 *
1799 * Default CRTC state destroy hook for drivers which don't have their own
1800 * subclassed CRTC state structure.
1801 */
1802 void drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
1803 struct drm_crtc_state *state)
1804 {
1805 kfree(state);
1806 }
1807 EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state);
1808
1809 /**
1810 * drm_atomic_helper_plane_reset - default ->reset hook for planes
1811 * @plane: drm plane
1812 *
1813 * Resets the atomic state for @plane by freeing the state pointer (which might
1814 * be NULL, e.g. at driver load time) and allocating a new empty state object.
1815 */
1816 void drm_atomic_helper_plane_reset(struct drm_plane *plane)
1817 {
1818 if (plane->state && plane->state->fb)
1819 drm_framebuffer_unreference(plane->state->fb);
1820
1821 kfree(plane->state);
1822 plane->state = kzalloc(sizeof(*plane->state), GFP_KERNEL);
1823 }
1824 EXPORT_SYMBOL(drm_atomic_helper_plane_reset);
1825
1826 /**
1827 * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
1828 * @plane: drm plane
1829 *
1830 * Default plane state duplicate hook for drivers which don't have their own
1831 * subclassed plane state structure.
1832 */
1833 struct drm_plane_state *
1834 drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane)
1835 {
1836 struct drm_plane_state *state;
1837
1838 if (WARN_ON(!plane->state))
1839 return NULL;
1840
1841 state = kmemdup(plane->state, sizeof(*plane->state), GFP_KERNEL);
1842
1843 if (state && state->fb)
1844 drm_framebuffer_reference(state->fb);
1845
1846 return state;
1847 }
1848 EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state);
1849
1850 /**
1851 * drm_atomic_helper_plane_destroy_state - default state destroy hook
1852 * @plane: drm plane
1853 * @state: plane state object to release
1854 *
1855 * Default plane state destroy hook for drivers which don't have their own
1856 * subclassed plane state structure.
1857 */
1858 void drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
1859 struct drm_plane_state *state)
1860 {
1861 if (state->fb)
1862 drm_framebuffer_unreference(state->fb);
1863
1864 kfree(state);
1865 }
1866 EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state);
1867
1868 /**
1869 * drm_atomic_helper_connector_reset - default ->reset hook for connectors
1870 * @connector: drm connector
1871 *
1872 * Resets the atomic state for @connector by freeing the state pointer (which
1873 * might be NULL, e.g. at driver load time) and allocating a new empty state
1874 * object.
1875 */
1876 void drm_atomic_helper_connector_reset(struct drm_connector *connector)
1877 {
1878 kfree(connector->state);
1879 connector->state = kzalloc(sizeof(*connector->state), GFP_KERNEL);
1880 }
1881 EXPORT_SYMBOL(drm_atomic_helper_connector_reset);
1882
1883 /**
1884 * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
1885 * @connector: drm connector
1886 *
1887 * Default connector state duplicate hook for drivers which don't have their own
1888 * subclassed connector state structure.
1889 */
1890 struct drm_connector_state *
1891 drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
1892 {
1893 if (WARN_ON(!connector->state))
1894 return NULL;
1895
1896 return kmemdup(connector->state, sizeof(*connector->state), GFP_KERNEL);
1897 }
1898 EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
1899
1900 /**
1901 * drm_atomic_helper_connector_destroy_state - default state destroy hook
1902 * @connector: drm connector
1903 * @state: connector state object to release
1904 *
1905 * Default connector state destroy hook for drivers which don't have their own
1906 * subclassed connector state structure.
1907 */
1908 void drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
1909 struct drm_connector_state *state)
1910 {
1911 kfree(state);
1912 }
1913 EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state);
This page took 0.071806 seconds and 4 git commands to generate.