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