]> git.ipfire.org Git - thirdparty/kernel/stable.git/blame - drivers/gpu/drm/drm_atomic_helper.c
drm/atomic: check mode_changed *after* atomic_check
[thirdparty/kernel/stable.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 34
3150c7d0
DV
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 */
c2fcd274
DV
56static void
57drm_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
623369e5
DV
83static struct drm_crtc *
84get_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
102static int
103steal_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;
042652ed 111 int ret;
623369e5
DV
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
042652ed
DV
142 ret = drm_atomic_set_crtc_for_connector(connector_state, NULL);
143 if (ret)
144 return ret;
623369e5
DV
145 connector_state->best_encoder = NULL;
146 }
147
148 return 0;
149}
150
151static int
152update_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
248static int
249mode_fixup(struct drm_atomic_state *state)
250{
251 int ncrtcs = state->dev->mode_config.num_crtc;
623369e5
DV
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
f52b69f1 266 for (i = 0; i < state->num_connector; i++) {
623369e5
DV
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
333static int
934ce1c2 334drm_atomic_helper_check_modeset(struct drm_device *dev,
623369e5
DV
335 struct drm_atomic_state *state)
336{
337 int ncrtcs = dev->mode_config.num_crtc;
623369e5
DV
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
f52b69f1 362 for (i = 0; i < state->num_connector; i++) {
623369e5
DV
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
c2fcd274
DV
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 */
424int 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
934ce1c2
RC
474 ret = drm_atomic_helper_check_modeset(dev, state);
475 if (ret)
476 return ret;
477
c2fcd274
DV
478 return ret;
479}
480EXPORT_SYMBOL(drm_atomic_helper_check);
481
623369e5
DV
482static void
483disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state)
484{
485 int ncrtcs = old_state->dev->mode_config.num_crtc;
623369e5
DV
486 int i;
487
f52b69f1 488 for (i = 0; i < old_state->num_connector; i++) {
623369e5
DV
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 = connector->state->best_encoder;
503
504 if (!encoder)
505 continue;
506
507 funcs = encoder->helper_private;
508
509 /*
510 * Each encoder has at most one connector (since we always steal
511 * it away), so we won't call call disable hooks twice.
512 */
513 if (encoder->bridge)
514 encoder->bridge->funcs->disable(encoder->bridge);
515
516 /* Right function depends upon target state. */
517 if (connector->state->crtc)
518 funcs->prepare(encoder);
519 else if (funcs->disable)
520 funcs->disable(encoder);
521 else
522 funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
523
524 if (encoder->bridge)
525 encoder->bridge->funcs->post_disable(encoder->bridge);
526 }
527
528 for (i = 0; i < ncrtcs; i++) {
529 struct drm_crtc_helper_funcs *funcs;
530 struct drm_crtc *crtc;
531
532 crtc = old_state->crtcs[i];
533
534 /* Shut down everything that needs a full modeset. */
535 if (!crtc || !crtc->state->mode_changed)
536 continue;
537
538 funcs = crtc->helper_private;
539
540 /* Right function depends upon target state. */
541 if (crtc->state->enable)
542 funcs->prepare(crtc);
543 else if (funcs->disable)
544 funcs->disable(crtc);
545 else
546 funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
547 }
548}
549
550static void
551set_routing_links(struct drm_device *dev, struct drm_atomic_state *old_state)
552{
623369e5
DV
553 int ncrtcs = old_state->dev->mode_config.num_crtc;
554 int i;
555
556 /* clear out existing links */
f52b69f1 557 for (i = 0; i < old_state->num_connector; i++) {
623369e5
DV
558 struct drm_connector *connector;
559
560 connector = old_state->connectors[i];
561
562 if (!connector || !connector->encoder)
563 continue;
564
565 WARN_ON(!connector->encoder->crtc);
566
567 connector->encoder->crtc = NULL;
568 connector->encoder = NULL;
569 }
570
571 /* set new links */
f52b69f1 572 for (i = 0; i < old_state->num_connector; i++) {
623369e5
DV
573 struct drm_connector *connector;
574
575 connector = old_state->connectors[i];
576
577 if (!connector || !connector->state->crtc)
578 continue;
579
580 if (WARN_ON(!connector->state->best_encoder))
581 continue;
582
583 connector->encoder = connector->state->best_encoder;
584 connector->encoder->crtc = connector->state->crtc;
585 }
586
587 /* set legacy state in the crtc structure */
588 for (i = 0; i < ncrtcs; i++) {
589 struct drm_crtc *crtc;
590
591 crtc = old_state->crtcs[i];
592
593 if (!crtc)
594 continue;
595
596 crtc->mode = crtc->state->mode;
597 crtc->enabled = crtc->state->enable;
598 crtc->x = crtc->primary->state->src_x >> 16;
599 crtc->y = crtc->primary->state->src_y >> 16;
600 }
601}
602
603static void
604crtc_set_mode(struct drm_device *dev, struct drm_atomic_state *old_state)
605{
606 int ncrtcs = old_state->dev->mode_config.num_crtc;
623369e5
DV
607 int i;
608
609 for (i = 0; i < ncrtcs; i++) {
610 struct drm_crtc_helper_funcs *funcs;
611 struct drm_crtc *crtc;
612
613 crtc = old_state->crtcs[i];
614
615 if (!crtc || !crtc->state->mode_changed)
616 continue;
617
618 funcs = crtc->helper_private;
619
620 if (crtc->state->enable)
621 funcs->mode_set_nofb(crtc);
622 }
623
f52b69f1 624 for (i = 0; i < old_state->num_connector; i++) {
623369e5
DV
625 struct drm_connector *connector;
626 struct drm_crtc_state *new_crtc_state;
627 struct drm_encoder_helper_funcs *funcs;
628 struct drm_encoder *encoder;
629 struct drm_display_mode *mode, *adjusted_mode;
630
631 connector = old_state->connectors[i];
632
633 if (!connector || !connector->state->best_encoder)
634 continue;
635
636 encoder = connector->state->best_encoder;
637 funcs = encoder->helper_private;
638 new_crtc_state = connector->state->crtc->state;
639 mode = &new_crtc_state->mode;
640 adjusted_mode = &new_crtc_state->adjusted_mode;
641
642 /*
643 * Each encoder has at most one connector (since we always steal
644 * it away), so we won't call call mode_set hooks twice.
645 */
646 funcs->mode_set(encoder, mode, adjusted_mode);
647
648 if (encoder->bridge && encoder->bridge->funcs->mode_set)
649 encoder->bridge->funcs->mode_set(encoder->bridge,
650 mode, adjusted_mode);
651 }
652}
653
654/**
655 * drm_atomic_helper_commit_pre_planes - modeset commit before plane updates
656 * @dev: DRM device
657 * @state: atomic state
658 *
659 * This function commits the modeset changes that need to be committed before
660 * updating planes. It shuts down all the outputs that need to be shut down and
661 * prepares them (if required) with the new mode.
662 */
663void drm_atomic_helper_commit_pre_planes(struct drm_device *dev,
664 struct drm_atomic_state *state)
665{
666 disable_outputs(dev, state);
667 set_routing_links(dev, state);
668 crtc_set_mode(dev, state);
669}
670EXPORT_SYMBOL(drm_atomic_helper_commit_pre_planes);
671
672/**
673 * drm_atomic_helper_commit_post_planes - modeset commit after plane updates
674 * @dev: DRM device
675 * @old_state: atomic state object with old state structures
676 *
677 * This function commits the modeset changes that need to be committed after
678 * updating planes: It enables all the outputs with the new configuration which
679 * had to be turned off for the update.
680 */
681void drm_atomic_helper_commit_post_planes(struct drm_device *dev,
682 struct drm_atomic_state *old_state)
683{
684 int ncrtcs = old_state->dev->mode_config.num_crtc;
623369e5
DV
685 int i;
686
687 for (i = 0; i < ncrtcs; i++) {
688 struct drm_crtc_helper_funcs *funcs;
689 struct drm_crtc *crtc;
690
691 crtc = old_state->crtcs[i];
692
693 /* Need to filter out CRTCs where only planes change. */
694 if (!crtc || !crtc->state->mode_changed)
695 continue;
696
697 funcs = crtc->helper_private;
698
699 if (crtc->state->enable)
700 funcs->commit(crtc);
701 }
702
f52b69f1 703 for (i = 0; i < old_state->num_connector; i++) {
623369e5
DV
704 struct drm_connector *connector;
705 struct drm_encoder_helper_funcs *funcs;
706 struct drm_encoder *encoder;
707
708 connector = old_state->connectors[i];
709
710 if (!connector || !connector->state->best_encoder)
711 continue;
712
713 encoder = connector->state->best_encoder;
714 funcs = encoder->helper_private;
715
716 /*
717 * Each encoder has at most one connector (since we always steal
718 * it away), so we won't call call enable hooks twice.
719 */
720 if (encoder->bridge)
721 encoder->bridge->funcs->pre_enable(encoder->bridge);
722
723 funcs->commit(encoder);
724
725 if (encoder->bridge)
726 encoder->bridge->funcs->enable(encoder->bridge);
727 }
728}
729EXPORT_SYMBOL(drm_atomic_helper_commit_post_planes);
730
e2330f07
DV
731static void wait_for_fences(struct drm_device *dev,
732 struct drm_atomic_state *state)
733{
734 int nplanes = dev->mode_config.num_total_plane;
735 int i;
736
737 for (i = 0; i < nplanes; i++) {
738 struct drm_plane *plane = state->planes[i];
739
740 if (!plane || !plane->state->fence)
741 continue;
742
743 WARN_ON(!plane->state->fb);
744
745 fence_wait(plane->state->fence, false);
746 fence_put(plane->state->fence);
747 plane->state->fence = NULL;
748 }
749}
750
5ee3229c
RC
751/**
752 * drm_atomic_helper_wait_for_vblanks - wait for vblank on crtcs
753 * @dev: DRM device
754 * @old_state: atomic state object with old state structures
755 *
756 * Helper to, after atomic commit, wait for vblanks on all effected
757 * crtcs (ie. before cleaning up old framebuffers using
758 * drm_atomic_helper_cleanup_planes())
759 */
760void
761drm_atomic_helper_wait_for_vblanks(struct drm_device *dev,
762 struct drm_atomic_state *old_state)
623369e5
DV
763{
764 struct drm_crtc *crtc;
765 struct drm_crtc_state *old_crtc_state;
766 int ncrtcs = old_state->dev->mode_config.num_crtc;
767 int i, ret;
768
769 for (i = 0; i < ncrtcs; i++) {
770 crtc = old_state->crtcs[i];
771 old_crtc_state = old_state->crtc_states[i];
772
773 if (!crtc)
774 continue;
775
776 /* No one cares about the old state, so abuse it for tracking
777 * and store whether we hold a vblank reference (and should do a
778 * vblank wait) in the ->enable boolean. */
779 old_crtc_state->enable = false;
780
781 if (!crtc->state->enable)
782 continue;
783
784 ret = drm_crtc_vblank_get(crtc);
785 if (ret != 0)
786 continue;
787
788 old_crtc_state->enable = true;
789 old_crtc_state->last_vblank_count = drm_vblank_count(dev, i);
790 }
791
792 for (i = 0; i < ncrtcs; i++) {
793 crtc = old_state->crtcs[i];
794 old_crtc_state = old_state->crtc_states[i];
795
796 if (!crtc || !old_crtc_state->enable)
797 continue;
798
799 ret = wait_event_timeout(dev->vblank[i].queue,
800 old_crtc_state->last_vblank_count !=
801 drm_vblank_count(dev, i),
802 msecs_to_jiffies(50));
803
804 drm_crtc_vblank_put(crtc);
805 }
806}
5ee3229c 807EXPORT_SYMBOL(drm_atomic_helper_wait_for_vblanks);
623369e5
DV
808
809/**
810 * drm_atomic_helper_commit - commit validated state object
811 * @dev: DRM device
812 * @state: the driver state object
813 * @async: asynchronous commit
814 *
815 * This function commits a with drm_atomic_helper_check() pre-validated state
816 * object. This can still fail when e.g. the framebuffer reservation fails. For
817 * now this doesn't implement asynchronous commits.
818 *
819 * RETURNS
820 * Zero for success or -errno.
821 */
822int drm_atomic_helper_commit(struct drm_device *dev,
823 struct drm_atomic_state *state,
824 bool async)
825{
826 int ret;
827
828 if (async)
829 return -EBUSY;
830
831 ret = drm_atomic_helper_prepare_planes(dev, state);
832 if (ret)
833 return ret;
834
835 /*
836 * This is the point of no return - everything below never fails except
837 * when the hw goes bonghits. Which means we can commit the new state on
838 * the software side now.
839 */
840
841 drm_atomic_helper_swap_state(dev, state);
842
843 /*
844 * Everything below can be run asynchronously without the need to grab
845 * any modeset locks at all under one conditions: It must be guaranteed
846 * that the asynchronous work has either been cancelled (if the driver
847 * supports it, which at least requires that the framebuffers get
848 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
849 * before the new state gets committed on the software side with
850 * drm_atomic_helper_swap_state().
851 *
852 * This scheme allows new atomic state updates to be prepared and
853 * checked in parallel to the asynchronous completion of the previous
854 * update. Which is important since compositors need to figure out the
855 * composition of the next frame right after having submitted the
856 * current layout.
857 */
858
e2330f07
DV
859 wait_for_fences(dev, state);
860
623369e5
DV
861 drm_atomic_helper_commit_pre_planes(dev, state);
862
863 drm_atomic_helper_commit_planes(dev, state);
864
865 drm_atomic_helper_commit_post_planes(dev, state);
866
5ee3229c 867 drm_atomic_helper_wait_for_vblanks(dev, state);
623369e5
DV
868
869 drm_atomic_helper_cleanup_planes(dev, state);
870
871 drm_atomic_state_free(state);
872
873 return 0;
874}
875EXPORT_SYMBOL(drm_atomic_helper_commit);
876
e8c833a7
DV
877/**
878 * DOC: implementing async commit
879 *
880 * For now the atomic helpers don't support async commit directly. If there is
881 * real need it could be added though, using the dma-buf fence infrastructure
882 * for generic synchronization with outstanding rendering.
883 *
884 * For now drivers have to implement async commit themselves, with the following
885 * sequence being the recommended one:
886 *
887 * 1. Run drm_atomic_helper_prepare_planes() first. This is the only function
888 * which commit needs to call which can fail, so we want to run it first and
889 * synchronously.
890 *
891 * 2. Synchronize with any outstanding asynchronous commit worker threads which
892 * might be affected the new state update. This can be done by either cancelling
893 * or flushing the work items, depending upon whether the driver can deal with
894 * cancelled updates. Note that it is important to ensure that the framebuffer
895 * cleanup is still done when cancelling.
896 *
897 * For sufficient parallelism it is recommended to have a work item per crtc
898 * (for updates which don't touch global state) and a global one. Then we only
899 * need to synchronize with the crtc work items for changed crtcs and the global
900 * work item, which allows nice concurrent updates on disjoint sets of crtcs.
901 *
902 * 3. The software state is updated synchronously with
903 * drm_atomic_helper_swap_state. Doing this under the protection of all modeset
904 * locks means concurrent callers never see inconsistent state. And doing this
905 * while it's guaranteed that no relevant async worker runs means that async
906 * workers do not need grab any locks. Actually they must not grab locks, for
907 * otherwise the work flushing will deadlock.
908 *
909 * 4. Schedule a work item to do all subsequent steps, using the split-out
910 * commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and
911 * then cleaning up the framebuffers after the old framebuffer is no longer
912 * being displayed.
913 */
914
c2fcd274
DV
915/**
916 * drm_atomic_helper_prepare_planes - prepare plane resources after commit
917 * @dev: DRM device
918 * @state: atomic state object with old state structures
919 *
920 * This function prepares plane state, specifically framebuffers, for the new
921 * configuration. If any failure is encountered this function will call
922 * ->cleanup_fb on any already successfully prepared framebuffer.
923 *
924 * Returns:
925 * 0 on success, negative error code on failure.
926 */
927int drm_atomic_helper_prepare_planes(struct drm_device *dev,
928 struct drm_atomic_state *state)
929{
930 int nplanes = dev->mode_config.num_total_plane;
931 int ret, i;
932
933 for (i = 0; i < nplanes; i++) {
934 struct drm_plane_helper_funcs *funcs;
935 struct drm_plane *plane = state->planes[i];
936 struct drm_framebuffer *fb;
937
938 if (!plane)
939 continue;
940
941 funcs = plane->helper_private;
942
943 fb = state->plane_states[i]->fb;
944
945 if (fb && funcs->prepare_fb) {
946 ret = funcs->prepare_fb(plane, fb);
947 if (ret)
948 goto fail;
949 }
950 }
951
952 return 0;
953
954fail:
955 for (i--; i >= 0; i--) {
956 struct drm_plane_helper_funcs *funcs;
957 struct drm_plane *plane = state->planes[i];
958 struct drm_framebuffer *fb;
959
960 if (!plane)
961 continue;
962
963 funcs = plane->helper_private;
964
965 fb = state->plane_states[i]->fb;
966
967 if (fb && funcs->cleanup_fb)
968 funcs->cleanup_fb(plane, fb);
969
970 }
971
972 return ret;
973}
974EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
975
976/**
977 * drm_atomic_helper_commit_planes - commit plane state
978 * @dev: DRM device
b0fcfc89 979 * @old_state: atomic state object with old state structures
c2fcd274
DV
980 *
981 * This function commits the new plane state using the plane and atomic helper
982 * functions for planes and crtcs. It assumes that the atomic state has already
983 * been pushed into the relevant object state pointers, since this step can no
984 * longer fail.
985 *
b0fcfc89 986 * It still requires the global state object @old_state to know which planes and
c2fcd274
DV
987 * crtcs need to be updated though.
988 */
989void drm_atomic_helper_commit_planes(struct drm_device *dev,
b0fcfc89 990 struct drm_atomic_state *old_state)
c2fcd274
DV
991{
992 int nplanes = dev->mode_config.num_total_plane;
993 int ncrtcs = dev->mode_config.num_crtc;
994 int i;
995
996 for (i = 0; i < ncrtcs; i++) {
997 struct drm_crtc_helper_funcs *funcs;
b0fcfc89 998 struct drm_crtc *crtc = old_state->crtcs[i];
c2fcd274
DV
999
1000 if (!crtc)
1001 continue;
1002
1003 funcs = crtc->helper_private;
1004
1005 if (!funcs || !funcs->atomic_begin)
1006 continue;
1007
1008 funcs->atomic_begin(crtc);
1009 }
1010
1011 for (i = 0; i < nplanes; i++) {
1012 struct drm_plane_helper_funcs *funcs;
b0fcfc89 1013 struct drm_plane *plane = old_state->planes[i];
c2fcd274
DV
1014
1015 if (!plane)
1016 continue;
1017
1018 funcs = plane->helper_private;
1019
1020 if (!funcs || !funcs->atomic_update)
1021 continue;
1022
1023 funcs->atomic_update(plane);
1024 }
1025
1026 for (i = 0; i < ncrtcs; i++) {
1027 struct drm_crtc_helper_funcs *funcs;
b0fcfc89 1028 struct drm_crtc *crtc = old_state->crtcs[i];
c2fcd274
DV
1029
1030 if (!crtc)
1031 continue;
1032
1033 funcs = crtc->helper_private;
1034
1035 if (!funcs || !funcs->atomic_flush)
1036 continue;
1037
1038 funcs->atomic_flush(crtc);
1039 }
1040}
1041EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
1042
1043/**
1044 * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
1045 * @dev: DRM device
1046 * @old_state: atomic state object with old state structures
1047 *
1048 * This function cleans up plane state, specifically framebuffers, from the old
1049 * configuration. Hence the old configuration must be perserved in @old_state to
1050 * be able to call this function.
1051 *
1052 * This function must also be called on the new state when the atomic update
1053 * fails at any point after calling drm_atomic_helper_prepare_planes().
1054 */
1055void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
1056 struct drm_atomic_state *old_state)
1057{
1058 int nplanes = dev->mode_config.num_total_plane;
1059 int i;
1060
1061 for (i = 0; i < nplanes; i++) {
1062 struct drm_plane_helper_funcs *funcs;
1063 struct drm_plane *plane = old_state->planes[i];
1064 struct drm_framebuffer *old_fb;
1065
1066 if (!plane)
1067 continue;
1068
1069 funcs = plane->helper_private;
1070
1071 old_fb = old_state->plane_states[i]->fb;
1072
1073 if (old_fb && funcs->cleanup_fb)
1074 funcs->cleanup_fb(plane, old_fb);
1075 }
1076}
1077EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
1078
1079/**
1080 * drm_atomic_helper_swap_state - store atomic state into current sw state
1081 * @dev: DRM device
1082 * @state: atomic state
1083 *
1084 * This function stores the atomic state into the current state pointers in all
1085 * driver objects. It should be called after all failing steps have been done
1086 * and succeeded, but before the actual hardware state is committed.
1087 *
1088 * For cleanup and error recovery the current state for all changed objects will
1089 * be swaped into @state.
1090 *
1091 * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
1092 *
1093 * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
1094 *
1095 * 2. Do any other steps that might fail.
1096 *
1097 * 3. Put the staged state into the current state pointers with this function.
1098 *
1099 * 4. Actually commit the hardware state.
1100 *
1101 * 5. Call drm_atomic_helper_cleanup_planes with @state, which since step 3
1102 * contains the old state. Also do any other cleanup required with that state.
1103 */
1104void drm_atomic_helper_swap_state(struct drm_device *dev,
1105 struct drm_atomic_state *state)
1106{
1107 int i;
1108
1109 for (i = 0; i < dev->mode_config.num_connector; i++) {
1110 struct drm_connector *connector = state->connectors[i];
1111
1112 if (!connector)
1113 continue;
1114
1115 connector->state->state = state;
1116 swap(state->connector_states[i], connector->state);
1117 connector->state->state = NULL;
1118 }
1119
1120 for (i = 0; i < dev->mode_config.num_crtc; i++) {
1121 struct drm_crtc *crtc = state->crtcs[i];
1122
1123 if (!crtc)
1124 continue;
1125
1126 crtc->state->state = state;
1127 swap(state->crtc_states[i], crtc->state);
1128 crtc->state->state = NULL;
1129 }
1130
1131 for (i = 0; i < dev->mode_config.num_total_plane; i++) {
1132 struct drm_plane *plane = state->planes[i];
1133
1134 if (!plane)
1135 continue;
1136
1137 plane->state->state = state;
1138 swap(state->plane_states[i], plane->state);
1139 plane->state->state = NULL;
1140 }
1141}
1142EXPORT_SYMBOL(drm_atomic_helper_swap_state);
042652ed
DV
1143
1144/**
1145 * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
1146 * @plane: plane object to update
1147 * @crtc: owning CRTC of owning plane
1148 * @fb: framebuffer to flip onto plane
1149 * @crtc_x: x offset of primary plane on crtc
1150 * @crtc_y: y offset of primary plane on crtc
1151 * @crtc_w: width of primary plane rectangle on crtc
1152 * @crtc_h: height of primary plane rectangle on crtc
1153 * @src_x: x offset of @fb for panning
1154 * @src_y: y offset of @fb for panning
1155 * @src_w: width of source rectangle in @fb
1156 * @src_h: height of source rectangle in @fb
1157 *
1158 * Provides a default plane update handler using the atomic driver interface.
1159 *
1160 * RETURNS:
1161 * Zero on success, error code on failure
1162 */
1163int drm_atomic_helper_update_plane(struct drm_plane *plane,
1164 struct drm_crtc *crtc,
1165 struct drm_framebuffer *fb,
1166 int crtc_x, int crtc_y,
1167 unsigned int crtc_w, unsigned int crtc_h,
1168 uint32_t src_x, uint32_t src_y,
1169 uint32_t src_w, uint32_t src_h)
1170{
1171 struct drm_atomic_state *state;
1172 struct drm_plane_state *plane_state;
1173 int ret = 0;
1174
1175 state = drm_atomic_state_alloc(plane->dev);
1176 if (!state)
1177 return -ENOMEM;
1178
1179 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1180retry:
1181 plane_state = drm_atomic_get_plane_state(state, plane);
1182 if (IS_ERR(plane_state)) {
1183 ret = PTR_ERR(plane_state);
1184 goto fail;
1185 }
1186
1187 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
1188 if (ret != 0)
1189 goto fail;
321ebf04 1190 drm_atomic_set_fb_for_plane(plane_state, fb);
042652ed
DV
1191 plane_state->crtc_x = crtc_x;
1192 plane_state->crtc_y = crtc_y;
1193 plane_state->crtc_h = crtc_h;
1194 plane_state->crtc_w = crtc_w;
1195 plane_state->src_x = src_x;
1196 plane_state->src_y = src_y;
1197 plane_state->src_h = src_h;
1198 plane_state->src_w = src_w;
1199
1200 ret = drm_atomic_commit(state);
1201 if (ret != 0)
1202 goto fail;
1203
1204 /* Driver takes ownership of state on successful commit. */
1205 return 0;
1206fail:
1207 if (ret == -EDEADLK)
1208 goto backoff;
1209
1210 drm_atomic_state_free(state);
1211
1212 return ret;
1213backoff:
042652ed 1214 drm_atomic_state_clear(state);
6f75cea6 1215 drm_atomic_legacy_backoff(state);
042652ed
DV
1216
1217 /*
1218 * Someone might have exchanged the framebuffer while we dropped locks
1219 * in the backoff code. We need to fix up the fb refcount tracking the
1220 * core does for us.
1221 */
1222 plane->old_fb = plane->fb;
1223
1224 goto retry;
1225}
1226EXPORT_SYMBOL(drm_atomic_helper_update_plane);
1227
1228/**
1229 * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
1230 * @plane: plane to disable
1231 *
1232 * Provides a default plane disable handler using the atomic driver interface.
1233 *
1234 * RETURNS:
1235 * Zero on success, error code on failure
1236 */
1237int drm_atomic_helper_disable_plane(struct drm_plane *plane)
1238{
1239 struct drm_atomic_state *state;
1240 struct drm_plane_state *plane_state;
1241 int ret = 0;
1242
1243 state = drm_atomic_state_alloc(plane->dev);
1244 if (!state)
1245 return -ENOMEM;
1246
1247 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(plane->crtc);
1248retry:
1249 plane_state = drm_atomic_get_plane_state(state, plane);
1250 if (IS_ERR(plane_state)) {
1251 ret = PTR_ERR(plane_state);
1252 goto fail;
1253 }
1254
1255 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
1256 if (ret != 0)
1257 goto fail;
321ebf04 1258 drm_atomic_set_fb_for_plane(plane_state, NULL);
042652ed
DV
1259 plane_state->crtc_x = 0;
1260 plane_state->crtc_y = 0;
1261 plane_state->crtc_h = 0;
1262 plane_state->crtc_w = 0;
1263 plane_state->src_x = 0;
1264 plane_state->src_y = 0;
1265 plane_state->src_h = 0;
1266 plane_state->src_w = 0;
1267
1268 ret = drm_atomic_commit(state);
1269 if (ret != 0)
1270 goto fail;
1271
1272 /* Driver takes ownership of state on successful commit. */
1273 return 0;
1274fail:
1275 if (ret == -EDEADLK)
1276 goto backoff;
1277
1278 drm_atomic_state_free(state);
1279
1280 return ret;
1281backoff:
042652ed 1282 drm_atomic_state_clear(state);
6f75cea6 1283 drm_atomic_legacy_backoff(state);
042652ed
DV
1284
1285 /*
1286 * Someone might have exchanged the framebuffer while we dropped locks
1287 * in the backoff code. We need to fix up the fb refcount tracking the
1288 * core does for us.
1289 */
1290 plane->old_fb = plane->fb;
1291
1292 goto retry;
1293}
1294EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
1295
1296static int update_output_state(struct drm_atomic_state *state,
1297 struct drm_mode_set *set)
1298{
1299 struct drm_device *dev = set->crtc->dev;
1300 struct drm_connector_state *conn_state;
042652ed
DV
1301 int ncrtcs = state->dev->mode_config.num_crtc;
1302 int ret, i, j;
1303
1304 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
1305 state->acquire_ctx);
1306 if (ret)
1307 return ret;
1308
1309 /* First grab all affected connector/crtc states. */
1310 for (i = 0; i < set->num_connectors; i++) {
1311 conn_state = drm_atomic_get_connector_state(state,
1312 set->connectors[i]);
1313 if (IS_ERR(conn_state))
1314 return PTR_ERR(conn_state);
1315 }
1316
1317 for (i = 0; i < ncrtcs; i++) {
1318 struct drm_crtc *crtc = state->crtcs[i];
1319
1320 if (!crtc)
1321 continue;
1322
1323 ret = drm_atomic_add_affected_connectors(state, crtc);
1324 if (ret)
1325 return ret;
1326 }
1327
1328 /* Then recompute connector->crtc links and crtc enabling state. */
f52b69f1 1329 for (i = 0; i < state->num_connector; i++) {
042652ed
DV
1330 struct drm_connector *connector;
1331
1332 connector = state->connectors[i];
1333 conn_state = state->connector_states[i];
1334
1335 if (!connector)
1336 continue;
1337
1338 if (conn_state->crtc == set->crtc) {
1339 ret = drm_atomic_set_crtc_for_connector(conn_state,
1340 NULL);
1341 if (ret)
1342 return ret;
1343 }
1344
1345 for (j = 0; j < set->num_connectors; j++) {
1346 if (set->connectors[j] == connector) {
1347 ret = drm_atomic_set_crtc_for_connector(conn_state,
1348 set->crtc);
1349 if (ret)
1350 return ret;
1351 break;
1352 }
1353 }
1354 }
1355
1356 for (i = 0; i < ncrtcs; i++) {
1357 struct drm_crtc *crtc = state->crtcs[i];
1358 struct drm_crtc_state *crtc_state = state->crtc_states[i];
1359
1360 if (!crtc)
1361 continue;
1362
1363 /* Don't update ->enable for the CRTC in the set_config request,
1364 * since a mismatch would indicate a bug in the upper layers.
1365 * The actual modeset code later on will catch any
1366 * inconsistencies here. */
1367 if (crtc == set->crtc)
1368 continue;
1369
1370 crtc_state->enable =
1371 drm_atomic_connectors_for_crtc(state, crtc);
1372 }
1373
1374 return 0;
1375}
1376
1377/**
1378 * drm_atomic_helper_set_config - set a new config from userspace
1379 * @set: mode set configuration
1380 *
1381 * Provides a default crtc set_config handler using the atomic driver interface.
1382 *
1383 * Returns:
1384 * Returns 0 on success, negative errno numbers on failure.
1385 */
1386int drm_atomic_helper_set_config(struct drm_mode_set *set)
1387{
1388 struct drm_atomic_state *state;
1389 struct drm_crtc *crtc = set->crtc;
1390 struct drm_crtc_state *crtc_state;
1391 struct drm_plane_state *primary_state;
1392 int ret = 0;
1393
1394 state = drm_atomic_state_alloc(crtc->dev);
1395 if (!state)
1396 return -ENOMEM;
1397
1398 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1399retry:
1400 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1401 if (IS_ERR(crtc_state)) {
1402 ret = PTR_ERR(crtc_state);
1403 goto fail;
1404 }
1405
1406 if (!set->mode) {
1407 WARN_ON(set->fb);
1408 WARN_ON(set->num_connectors);
1409
1410 crtc_state->enable = false;
1411 goto commit;
1412 }
1413
1414 WARN_ON(!set->fb);
1415 WARN_ON(!set->num_connectors);
1416
1417 crtc_state->enable = true;
1418 drm_mode_copy(&crtc_state->mode, set->mode);
1419
1420 primary_state = drm_atomic_get_plane_state(state, crtc->primary);
1421 if (IS_ERR(primary_state)) {
1422 ret = PTR_ERR(primary_state);
1423 goto fail;
1424 }
1425
1426 ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
1427 if (ret != 0)
1428 goto fail;
321ebf04 1429 drm_atomic_set_fb_for_plane(primary_state, set->fb);
042652ed
DV
1430 primary_state->crtc_x = 0;
1431 primary_state->crtc_y = 0;
1432 primary_state->crtc_h = set->mode->vdisplay;
1433 primary_state->crtc_w = set->mode->hdisplay;
1434 primary_state->src_x = set->x << 16;
1435 primary_state->src_y = set->y << 16;
1436 primary_state->src_h = set->mode->vdisplay << 16;
1437 primary_state->src_w = set->mode->hdisplay << 16;
1438
1439commit:
1440 ret = update_output_state(state, set);
1441 if (ret)
1442 goto fail;
1443
1444 ret = drm_atomic_commit(state);
1445 if (ret != 0)
1446 goto fail;
1447
1448 /* Driver takes ownership of state on successful commit. */
1449 return 0;
1450fail:
1451 if (ret == -EDEADLK)
1452 goto backoff;
1453
1454 drm_atomic_state_free(state);
1455
1456 return ret;
1457backoff:
042652ed 1458 drm_atomic_state_clear(state);
6f75cea6 1459 drm_atomic_legacy_backoff(state);
042652ed
DV
1460
1461 /*
1462 * Someone might have exchanged the framebuffer while we dropped locks
1463 * in the backoff code. We need to fix up the fb refcount tracking the
1464 * core does for us.
1465 */
1466 crtc->primary->old_fb = crtc->primary->fb;
1467
1468 goto retry;
1469}
1470EXPORT_SYMBOL(drm_atomic_helper_set_config);
1471
1472/**
1473 * drm_atomic_helper_crtc_set_property - helper for crtc prorties
1474 * @crtc: DRM crtc
1475 * @property: DRM property
1476 * @val: value of property
1477 *
1478 * Provides a default plane disablle handler using the atomic driver interface.
1479 *
1480 * RETURNS:
1481 * Zero on success, error code on failure
1482 */
1483int
1484drm_atomic_helper_crtc_set_property(struct drm_crtc *crtc,
1485 struct drm_property *property,
1486 uint64_t val)
1487{
1488 struct drm_atomic_state *state;
1489 struct drm_crtc_state *crtc_state;
1490 int ret = 0;
1491
1492 state = drm_atomic_state_alloc(crtc->dev);
1493 if (!state)
1494 return -ENOMEM;
1495
1496 /* ->set_property is always called with all locks held. */
1497 state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
1498retry:
1499 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1500 if (IS_ERR(crtc_state)) {
1501 ret = PTR_ERR(crtc_state);
1502 goto fail;
1503 }
1504
1505 ret = crtc->funcs->atomic_set_property(crtc, crtc_state,
1506 property, val);
1507 if (ret)
1508 goto fail;
1509
1510 ret = drm_atomic_commit(state);
1511 if (ret != 0)
1512 goto fail;
1513
1514 /* Driver takes ownership of state on successful commit. */
1515 return 0;
1516fail:
1517 if (ret == -EDEADLK)
1518 goto backoff;
1519
1520 drm_atomic_state_free(state);
1521
1522 return ret;
1523backoff:
042652ed 1524 drm_atomic_state_clear(state);
6f75cea6 1525 drm_atomic_legacy_backoff(state);
042652ed
DV
1526
1527 goto retry;
1528}
1529EXPORT_SYMBOL(drm_atomic_helper_crtc_set_property);
1530
1531/**
1532 * drm_atomic_helper_plane_set_property - helper for plane prorties
1533 * @plane: DRM plane
1534 * @property: DRM property
1535 * @val: value of property
1536 *
1537 * Provides a default plane disable handler using the atomic driver interface.
1538 *
1539 * RETURNS:
1540 * Zero on success, error code on failure
1541 */
1542int
1543drm_atomic_helper_plane_set_property(struct drm_plane *plane,
1544 struct drm_property *property,
1545 uint64_t val)
1546{
1547 struct drm_atomic_state *state;
1548 struct drm_plane_state *plane_state;
1549 int ret = 0;
1550
1551 state = drm_atomic_state_alloc(plane->dev);
1552 if (!state)
1553 return -ENOMEM;
1554
1555 /* ->set_property is always called with all locks held. */
1556 state->acquire_ctx = plane->dev->mode_config.acquire_ctx;
1557retry:
1558 plane_state = drm_atomic_get_plane_state(state, plane);
1559 if (IS_ERR(plane_state)) {
1560 ret = PTR_ERR(plane_state);
1561 goto fail;
1562 }
1563
1564 ret = plane->funcs->atomic_set_property(plane, plane_state,
1565 property, val);
1566 if (ret)
1567 goto fail;
1568
1569 ret = drm_atomic_commit(state);
1570 if (ret != 0)
1571 goto fail;
1572
1573 /* Driver takes ownership of state on successful commit. */
1574 return 0;
1575fail:
1576 if (ret == -EDEADLK)
1577 goto backoff;
1578
1579 drm_atomic_state_free(state);
1580
1581 return ret;
1582backoff:
042652ed 1583 drm_atomic_state_clear(state);
6f75cea6 1584 drm_atomic_legacy_backoff(state);
042652ed
DV
1585
1586 goto retry;
1587}
1588EXPORT_SYMBOL(drm_atomic_helper_plane_set_property);
1589
1590/**
1591 * drm_atomic_helper_connector_set_property - helper for connector prorties
1592 * @connector: DRM connector
1593 * @property: DRM property
1594 * @val: value of property
1595 *
1596 * Provides a default plane disablle handler using the atomic driver interface.
1597 *
1598 * RETURNS:
1599 * Zero on success, error code on failure
1600 */
1601int
1602drm_atomic_helper_connector_set_property(struct drm_connector *connector,
1603 struct drm_property *property,
1604 uint64_t val)
1605{
1606 struct drm_atomic_state *state;
1607 struct drm_connector_state *connector_state;
1608 int ret = 0;
1609
1610 state = drm_atomic_state_alloc(connector->dev);
1611 if (!state)
1612 return -ENOMEM;
1613
1614 /* ->set_property is always called with all locks held. */
1615 state->acquire_ctx = connector->dev->mode_config.acquire_ctx;
1616retry:
1617 connector_state = drm_atomic_get_connector_state(state, connector);
1618 if (IS_ERR(connector_state)) {
1619 ret = PTR_ERR(connector_state);
1620 goto fail;
1621 }
1622
1623 ret = connector->funcs->atomic_set_property(connector, connector_state,
1624 property, val);
1625 if (ret)
1626 goto fail;
1627
1628 ret = drm_atomic_commit(state);
1629 if (ret != 0)
1630 goto fail;
1631
1632 /* Driver takes ownership of state on successful commit. */
1633 return 0;
1634fail:
1635 if (ret == -EDEADLK)
1636 goto backoff;
1637
1638 drm_atomic_state_free(state);
1639
1640 return ret;
1641backoff:
042652ed 1642 drm_atomic_state_clear(state);
6f75cea6 1643 drm_atomic_legacy_backoff(state);
042652ed
DV
1644
1645 goto retry;
1646}
1647EXPORT_SYMBOL(drm_atomic_helper_connector_set_property);
8bc0f312
DV
1648
1649/**
1650 * drm_atomic_helper_page_flip - execute a legacy page flip
1651 * @crtc: DRM crtc
1652 * @fb: DRM framebuffer
1653 * @event: optional DRM event to signal upon completion
1654 * @flags: flip flags for non-vblank sync'ed updates
1655 *
1656 * Provides a default page flip implementation using the atomic driver interface.
1657 *
1658 * Note that for now so called async page flips (i.e. updates which are not
1659 * synchronized to vblank) are not supported, since the atomic interfaces have
1660 * no provisions for this yet.
1661 *
1662 * Returns:
1663 * Returns 0 on success, negative errno numbers on failure.
1664 */
1665int drm_atomic_helper_page_flip(struct drm_crtc *crtc,
1666 struct drm_framebuffer *fb,
1667 struct drm_pending_vblank_event *event,
1668 uint32_t flags)
1669{
1670 struct drm_plane *plane = crtc->primary;
1671 struct drm_atomic_state *state;
1672 struct drm_plane_state *plane_state;
1673 struct drm_crtc_state *crtc_state;
1674 int ret = 0;
1675
1676 if (flags & DRM_MODE_PAGE_FLIP_ASYNC)
1677 return -EINVAL;
1678
1679 state = drm_atomic_state_alloc(plane->dev);
1680 if (!state)
1681 return -ENOMEM;
1682
1683 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1684retry:
1685 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1686 if (IS_ERR(crtc_state)) {
1687 ret = PTR_ERR(crtc_state);
1688 goto fail;
1689 }
1690 crtc_state->event = event;
1691
1692 plane_state = drm_atomic_get_plane_state(state, plane);
1693 if (IS_ERR(plane_state)) {
1694 ret = PTR_ERR(plane_state);
1695 goto fail;
1696 }
1697
1698 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
1699 if (ret != 0)
1700 goto fail;
321ebf04 1701 drm_atomic_set_fb_for_plane(plane_state, fb);
8bc0f312
DV
1702
1703 ret = drm_atomic_async_commit(state);
1704 if (ret != 0)
1705 goto fail;
1706
1707 /* TODO: ->page_flip is the only driver callback where the core
1708 * doesn't update plane->fb. For now patch it up here. */
1709 plane->fb = plane->state->fb;
1710
1711 /* Driver takes ownership of state on successful async commit. */
1712 return 0;
1713fail:
1714 if (ret == -EDEADLK)
1715 goto backoff;
1716
1717 drm_atomic_state_free(state);
1718
1719 return ret;
1720backoff:
8bc0f312 1721 drm_atomic_state_clear(state);
6f75cea6 1722 drm_atomic_legacy_backoff(state);
8bc0f312
DV
1723
1724 /*
1725 * Someone might have exchanged the framebuffer while we dropped locks
1726 * in the backoff code. We need to fix up the fb refcount tracking the
1727 * core does for us.
1728 */
1729 plane->old_fb = plane->fb;
1730
1731 goto retry;
1732}
1733EXPORT_SYMBOL(drm_atomic_helper_page_flip);
d461701c 1734
3150c7d0
DV
1735/**
1736 * DOC: atomic state reset and initialization
1737 *
1738 * Both the drm core and the atomic helpers assume that there is always the full
1739 * and correct atomic software state for all connectors, CRTCs and planes
1740 * available. Which is a bit a problem on driver load and also after system
1741 * suspend. One way to solve this is to have a hardware state read-out
1742 * infrastructure which reconstructs the full software state (e.g. the i915
1743 * driver).
1744 *
1745 * The simpler solution is to just reset the software state to everything off,
1746 * which is easiest to do by calling drm_mode_config_reset(). To facilitate this
1747 * the atomic helpers provide default reset implementations for all hooks.
1748 */
1749
d461701c
DV
1750/**
1751 * drm_atomic_helper_crtc_reset - default ->reset hook for CRTCs
1752 * @crtc: drm CRTC
1753 *
1754 * Resets the atomic state for @crtc by freeing the state pointer (which might
1755 * be NULL, e.g. at driver load time) and allocating a new empty state object.
1756 */
1757void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc)
1758{
1759 kfree(crtc->state);
1760 crtc->state = kzalloc(sizeof(*crtc->state), GFP_KERNEL);
1761}
1762EXPORT_SYMBOL(drm_atomic_helper_crtc_reset);
1763
1764/**
1765 * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
1766 * @crtc: drm CRTC
1767 *
1768 * Default CRTC state duplicate hook for drivers which don't have their own
1769 * subclassed CRTC state structure.
1770 */
1771struct drm_crtc_state *
1772drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc)
1773{
1774 struct drm_crtc_state *state;
1775
1776 if (WARN_ON(!crtc->state))
1777 return NULL;
1778
1779 state = kmemdup(crtc->state, sizeof(*crtc->state), GFP_KERNEL);
1780
1781 if (state) {
1782 state->mode_changed = false;
1783 state->planes_changed = false;
1784 state->event = NULL;
1785 }
1786
1787 return state;
1788}
1789EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state);
1790
1791/**
1792 * drm_atomic_helper_crtc_destroy_state - default state destroy hook
1793 * @crtc: drm CRTC
1794 * @state: CRTC state object to release
1795 *
1796 * Default CRTC state destroy hook for drivers which don't have their own
1797 * subclassed CRTC state structure.
1798 */
1799void drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
1800 struct drm_crtc_state *state)
1801{
1802 kfree(state);
1803}
1804EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state);
1805
1806/**
1807 * drm_atomic_helper_plane_reset - default ->reset hook for planes
1808 * @plane: drm plane
1809 *
1810 * Resets the atomic state for @plane by freeing the state pointer (which might
1811 * be NULL, e.g. at driver load time) and allocating a new empty state object.
1812 */
1813void drm_atomic_helper_plane_reset(struct drm_plane *plane)
1814{
321ebf04
DV
1815 if (plane->state && plane->state->fb)
1816 drm_framebuffer_unreference(plane->state->fb);
1817
d461701c
DV
1818 kfree(plane->state);
1819 plane->state = kzalloc(sizeof(*plane->state), GFP_KERNEL);
1820}
1821EXPORT_SYMBOL(drm_atomic_helper_plane_reset);
1822
1823/**
1824 * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
1825 * @plane: drm plane
1826 *
1827 * Default plane state duplicate hook for drivers which don't have their own
1828 * subclassed plane state structure.
1829 */
1830struct drm_plane_state *
1831drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane)
1832{
321ebf04
DV
1833 struct drm_plane_state *state;
1834
d461701c
DV
1835 if (WARN_ON(!plane->state))
1836 return NULL;
1837
321ebf04
DV
1838 state = kmemdup(plane->state, sizeof(*plane->state), GFP_KERNEL);
1839
1840 if (state && state->fb)
1841 drm_framebuffer_reference(state->fb);
1842
1843 return state;
d461701c
DV
1844}
1845EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state);
1846
1847/**
1848 * drm_atomic_helper_plane_destroy_state - default state destroy hook
1849 * @plane: drm plane
1850 * @state: plane state object to release
1851 *
1852 * Default plane state destroy hook for drivers which don't have their own
1853 * subclassed plane state structure.
1854 */
1855void drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
321ebf04 1856 struct drm_plane_state *state)
d461701c 1857{
321ebf04
DV
1858 if (state->fb)
1859 drm_framebuffer_unreference(state->fb);
1860
d461701c
DV
1861 kfree(state);
1862}
1863EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state);
1864
1865/**
1866 * drm_atomic_helper_connector_reset - default ->reset hook for connectors
1867 * @connector: drm connector
1868 *
1869 * Resets the atomic state for @connector by freeing the state pointer (which
1870 * might be NULL, e.g. at driver load time) and allocating a new empty state
1871 * object.
1872 */
1873void drm_atomic_helper_connector_reset(struct drm_connector *connector)
1874{
1875 kfree(connector->state);
1876 connector->state = kzalloc(sizeof(*connector->state), GFP_KERNEL);
1877}
1878EXPORT_SYMBOL(drm_atomic_helper_connector_reset);
1879
1880/**
1881 * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
1882 * @connector: drm connector
1883 *
1884 * Default connector state duplicate hook for drivers which don't have their own
1885 * subclassed connector state structure.
1886 */
1887struct drm_connector_state *
1888drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
1889{
1890 if (WARN_ON(!connector->state))
1891 return NULL;
1892
1893 return kmemdup(connector->state, sizeof(*connector->state), GFP_KERNEL);
1894}
1895EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
1896
1897/**
1898 * drm_atomic_helper_connector_destroy_state - default state destroy hook
1899 * @connector: drm connector
1900 * @state: connector state object to release
1901 *
1902 * Default connector state destroy hook for drivers which don't have their own
1903 * subclassed connector state structure.
1904 */
1905void drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
1906 struct drm_connector_state *state)
1907{
1908 kfree(state);
1909}
1910EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state);