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