fad2b932cf7243158fa257deb25bc0e63b26c18a
[cascardo/linux.git] / drivers / gpu / drm / drm_atomic_helper.c
1 /*
2  * Copyright (C) 2014 Red Hat
3  * Copyright (C) 2014 Intel Corp.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21  * OTHER DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  * Rob Clark <robdclark@gmail.com>
25  * Daniel Vetter <daniel.vetter@ffwll.ch>
26  */
27
28 #include <drm/drmP.h>
29 #include <drm/drm_atomic.h>
30 #include <drm/drm_plane_helper.h>
31 #include <drm/drm_crtc_helper.h>
32 #include <drm/drm_atomic_helper.h>
33 #include <linux/fence.h>
34
35 /**
36  * DOC: overview
37  *
38  * This helper library provides implementations of check and commit functions on
39  * top of the CRTC modeset helper callbacks and the plane helper callbacks. It
40  * also provides convenience implementations for the atomic state handling
41  * callbacks for drivers which don't need to subclass the drm core structures to
42  * add their own additional internal state.
43  *
44  * This library also provides default implementations for the check callback in
45  * drm_atomic_helper_check and for the commit callback with
46  * drm_atomic_helper_commit. But the individual stages and callbacks are expose
47  * to allow drivers to mix and match and e.g. use the plane helpers only
48  * together with a driver private modeset implementation.
49  *
50  * This library also provides implementations for all the legacy driver
51  * interfaces on top of the atomic interface. See drm_atomic_helper_set_config,
52  * drm_atomic_helper_disable_plane, drm_atomic_helper_disable_plane and the
53  * various functions to implement set_property callbacks. New drivers must not
54  * implement these functions themselves but must use the provided helpers.
55  */
56 static void
57 drm_atomic_helper_plane_changed(struct drm_atomic_state *state,
58                                 struct drm_plane_state *plane_state,
59                                 struct drm_plane *plane)
60 {
61         struct drm_crtc_state *crtc_state;
62
63         if (plane->state->crtc) {
64                 crtc_state = state->crtc_states[drm_crtc_index(plane->crtc)];
65
66                 if (WARN_ON(!crtc_state))
67                         return;
68
69                 crtc_state->planes_changed = true;
70         }
71
72         if (plane_state->crtc) {
73                 crtc_state =
74                         state->crtc_states[drm_crtc_index(plane_state->crtc)];
75
76                 if (WARN_ON(!crtc_state))
77                         return;
78
79                 crtc_state->planes_changed = true;
80         }
81 }
82
83 static struct drm_crtc *
84 get_current_crtc_for_encoder(struct drm_device *dev,
85                              struct drm_encoder *encoder)
86 {
87         struct drm_mode_config *config = &dev->mode_config;
88         struct drm_connector *connector;
89
90         WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
91
92         list_for_each_entry(connector, &config->connector_list, head) {
93                 if (connector->state->best_encoder != encoder)
94                         continue;
95
96                 return connector->state->crtc;
97         }
98
99         return NULL;
100 }
101
102 static int
103 steal_encoder(struct drm_atomic_state *state,
104               struct drm_encoder *encoder,
105               struct drm_crtc *encoder_crtc)
106 {
107         struct drm_mode_config *config = &state->dev->mode_config;
108         struct drm_crtc_state *crtc_state;
109         struct drm_connector *connector;
110         struct drm_connector_state *connector_state;
111         int ret;
112
113         /*
114          * We can only steal an encoder coming from a connector, which means we
115          * must already hold the connection_mutex.
116          */
117         WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
118
119         DRM_DEBUG_KMS("[ENCODER:%d:%s] in use on [CRTC:%d], stealing it\n",
120                       encoder->base.id, encoder->name,
121                       encoder_crtc->base.id);
122
123         crtc_state = drm_atomic_get_crtc_state(state, encoder_crtc);
124         if (IS_ERR(crtc_state))
125                 return PTR_ERR(crtc_state);
126
127         crtc_state->mode_changed = true;
128
129         list_for_each_entry(connector, &config->connector_list, head) {
130                 if (connector->state->best_encoder != encoder)
131                         continue;
132
133                 DRM_DEBUG_KMS("Stealing encoder from [CONNECTOR:%d:%s]\n",
134                               connector->base.id,
135                               connector->name);
136
137                 connector_state = drm_atomic_get_connector_state(state,
138                                                                  connector);
139                 if (IS_ERR(connector_state))
140                         return PTR_ERR(connector_state);
141
142                 ret = drm_atomic_set_crtc_for_connector(connector_state, NULL);
143                 if (ret)
144                         return ret;
145                 connector_state->best_encoder = NULL;
146         }
147
148         return 0;
149 }
150
151 static int
152 update_connector_routing(struct drm_atomic_state *state, int conn_idx)
153 {
154         struct drm_connector_helper_funcs *funcs;
155         struct drm_encoder *new_encoder;
156         struct drm_crtc *encoder_crtc;
157         struct drm_connector *connector;
158         struct drm_connector_state *connector_state;
159         struct drm_crtc_state *crtc_state;
160         int idx, ret;
161
162         connector = state->connectors[conn_idx];
163         connector_state = state->connector_states[conn_idx];
164
165         if (!connector)
166                 return 0;
167
168         DRM_DEBUG_KMS("Updating routing for [CONNECTOR:%d:%s]\n",
169                         connector->base.id,
170                         connector->name);
171
172         if (connector->state->crtc != connector_state->crtc) {
173                 if (connector->state->crtc) {
174                         idx = drm_crtc_index(connector->state->crtc);
175
176                         crtc_state = state->crtc_states[idx];
177                         crtc_state->mode_changed = true;
178                 }
179
180                 if (connector_state->crtc) {
181                         idx = drm_crtc_index(connector_state->crtc);
182
183                         crtc_state = state->crtc_states[idx];
184                         crtc_state->mode_changed = true;
185                 }
186         }
187
188         if (!connector_state->crtc) {
189                 DRM_DEBUG_KMS("Disabling [CONNECTOR:%d:%s]\n",
190                                 connector->base.id,
191                                 connector->name);
192
193                 connector_state->best_encoder = NULL;
194
195                 return 0;
196         }
197
198         funcs = connector->helper_private;
199         new_encoder = funcs->best_encoder(connector);
200
201         if (!new_encoder) {
202                 DRM_DEBUG_KMS("No suitable encoder found for [CONNECTOR:%d:%s]\n",
203                               connector->base.id,
204                               connector->name);
205                 return -EINVAL;
206         }
207
208         if (new_encoder == connector_state->best_encoder) {
209                 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] keeps [ENCODER:%d:%s], now on [CRTC:%d]\n",
210                               connector->base.id,
211                               connector->name,
212                               new_encoder->base.id,
213                               new_encoder->name,
214                               connector_state->crtc->base.id);
215
216                 return 0;
217         }
218
219         encoder_crtc = get_current_crtc_for_encoder(state->dev,
220                                                     new_encoder);
221
222         if (encoder_crtc) {
223                 ret = steal_encoder(state, new_encoder, encoder_crtc);
224                 if (ret) {
225                         DRM_DEBUG_KMS("Encoder stealing failed for [CONNECTOR:%d:%s]\n",
226                                       connector->base.id,
227                                       connector->name);
228                         return ret;
229                 }
230         }
231
232         connector_state->best_encoder = new_encoder;
233         idx = drm_crtc_index(connector_state->crtc);
234
235         crtc_state = state->crtc_states[idx];
236         crtc_state->mode_changed = true;
237
238         DRM_DEBUG_KMS("[CONNECTOR:%d:%s] using [ENCODER:%d:%s] on [CRTC:%d]\n",
239                       connector->base.id,
240                       connector->name,
241                       new_encoder->base.id,
242                       new_encoder->name,
243                       connector_state->crtc->base.id);
244
245         return 0;
246 }
247
248 static int
249 mode_fixup(struct drm_atomic_state *state)
250 {
251         int ncrtcs = state->dev->mode_config.num_crtc;
252         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 /**
758  * drm_atomic_helper_wait_for_vblanks - wait for vblank on crtcs
759  * @dev: DRM device
760  * @old_state: atomic state object with old state structures
761  *
762  * Helper to, after atomic commit, wait for vblanks on all effected
763  * crtcs (ie. before cleaning up old framebuffers using
764  * drm_atomic_helper_cleanup_planes())
765  */
766 void
767 drm_atomic_helper_wait_for_vblanks(struct drm_device *dev,
768                 struct drm_atomic_state *old_state)
769 {
770         struct drm_crtc *crtc;
771         struct drm_crtc_state *old_crtc_state;
772         int ncrtcs = old_state->dev->mode_config.num_crtc;
773         int i, ret;
774
775         for (i = 0; i < ncrtcs; i++) {
776                 crtc = old_state->crtcs[i];
777                 old_crtc_state = old_state->crtc_states[i];
778
779                 if (!crtc)
780                         continue;
781
782                 /* No one cares about the old state, so abuse it for tracking
783                  * and store whether we hold a vblank reference (and should do a
784                  * vblank wait) in the ->enable boolean. */
785                 old_crtc_state->enable = false;
786
787                 if (!crtc->state->enable)
788                         continue;
789
790                 ret = drm_crtc_vblank_get(crtc);
791                 if (ret != 0)
792                         continue;
793
794                 old_crtc_state->enable = true;
795                 old_crtc_state->last_vblank_count = drm_vblank_count(dev, i);
796         }
797
798         for (i = 0; i < ncrtcs; i++) {
799                 crtc = old_state->crtcs[i];
800                 old_crtc_state = old_state->crtc_states[i];
801
802                 if (!crtc || !old_crtc_state->enable)
803                         continue;
804
805                 ret = wait_event_timeout(dev->vblank[i].queue,
806                                 old_crtc_state->last_vblank_count !=
807                                         drm_vblank_count(dev, i),
808                                 msecs_to_jiffies(50));
809
810                 drm_crtc_vblank_put(crtc);
811         }
812 }
813 EXPORT_SYMBOL(drm_atomic_helper_wait_for_vblanks);
814
815 /**
816  * drm_atomic_helper_commit - commit validated state object
817  * @dev: DRM device
818  * @state: the driver state object
819  * @async: asynchronous commit
820  *
821  * This function commits a with drm_atomic_helper_check() pre-validated state
822  * object. This can still fail when e.g. the framebuffer reservation fails. For
823  * now this doesn't implement asynchronous commits.
824  *
825  * RETURNS
826  * Zero for success or -errno.
827  */
828 int drm_atomic_helper_commit(struct drm_device *dev,
829                              struct drm_atomic_state *state,
830                              bool async)
831 {
832         int ret;
833
834         if (async)
835                 return -EBUSY;
836
837         ret = drm_atomic_helper_prepare_planes(dev, state);
838         if (ret)
839                 return ret;
840
841         /*
842          * This is the point of no return - everything below never fails except
843          * when the hw goes bonghits. Which means we can commit the new state on
844          * the software side now.
845          */
846
847         drm_atomic_helper_swap_state(dev, state);
848
849         /*
850          * Everything below can be run asynchronously without the need to grab
851          * any modeset locks at all under one conditions: It must be guaranteed
852          * that the asynchronous work has either been cancelled (if the driver
853          * supports it, which at least requires that the framebuffers get
854          * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
855          * before the new state gets committed on the software side with
856          * drm_atomic_helper_swap_state().
857          *
858          * This scheme allows new atomic state updates to be prepared and
859          * checked in parallel to the asynchronous completion of the previous
860          * update. Which is important since compositors need to figure out the
861          * composition of the next frame right after having submitted the
862          * current layout.
863          */
864
865         wait_for_fences(dev, state);
866
867         drm_atomic_helper_commit_pre_planes(dev, state);
868
869         drm_atomic_helper_commit_planes(dev, state);
870
871         drm_atomic_helper_commit_post_planes(dev, state);
872
873         drm_atomic_helper_wait_for_vblanks(dev, state);
874
875         drm_atomic_helper_cleanup_planes(dev, state);
876
877         drm_atomic_state_free(state);
878
879         return 0;
880 }
881 EXPORT_SYMBOL(drm_atomic_helper_commit);
882
883 /**
884  * DOC: implementing async commit
885  *
886  * For now the atomic helpers don't support async commit directly. If there is
887  * real need it could be added though, using the dma-buf fence infrastructure
888  * for generic synchronization with outstanding rendering.
889  *
890  * For now drivers have to implement async commit themselves, with the following
891  * sequence being the recommended one:
892  *
893  * 1. Run drm_atomic_helper_prepare_planes() first. This is the only function
894  * which commit needs to call which can fail, so we want to run it first and
895  * synchronously.
896  *
897  * 2. Synchronize with any outstanding asynchronous commit worker threads which
898  * might be affected the new state update. This can be done by either cancelling
899  * or flushing the work items, depending upon whether the driver can deal with
900  * cancelled updates. Note that it is important to ensure that the framebuffer
901  * cleanup is still done when cancelling.
902  *
903  * For sufficient parallelism it is recommended to have a work item per crtc
904  * (for updates which don't touch global state) and a global one. Then we only
905  * need to synchronize with the crtc work items for changed crtcs and the global
906  * work item, which allows nice concurrent updates on disjoint sets of crtcs.
907  *
908  * 3. The software state is updated synchronously with
909  * drm_atomic_helper_swap_state. Doing this under the protection of all modeset
910  * locks means concurrent callers never see inconsistent state. And doing this
911  * while it's guaranteed that no relevant async worker runs means that async
912  * workers do not need grab any locks. Actually they must not grab locks, for
913  * otherwise the work flushing will deadlock.
914  *
915  * 4. Schedule a work item to do all subsequent steps, using the split-out
916  * commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and
917  * then cleaning up the framebuffers after the old framebuffer is no longer
918  * being displayed.
919  */
920
921 /**
922  * drm_atomic_helper_prepare_planes - prepare plane resources after commit
923  * @dev: DRM device
924  * @state: atomic state object with old state structures
925  *
926  * This function prepares plane state, specifically framebuffers, for the new
927  * configuration. If any failure is encountered this function will call
928  * ->cleanup_fb on any already successfully prepared framebuffer.
929  *
930  * Returns:
931  * 0 on success, negative error code on failure.
932  */
933 int drm_atomic_helper_prepare_planes(struct drm_device *dev,
934                                      struct drm_atomic_state *state)
935 {
936         int nplanes = dev->mode_config.num_total_plane;
937         int ret, i;
938
939         for (i = 0; i < nplanes; i++) {
940                 struct drm_plane_helper_funcs *funcs;
941                 struct drm_plane *plane = state->planes[i];
942                 struct drm_framebuffer *fb;
943
944                 if (!plane)
945                         continue;
946
947                 funcs = plane->helper_private;
948
949                 fb = state->plane_states[i]->fb;
950
951                 if (fb && funcs->prepare_fb) {
952                         ret = funcs->prepare_fb(plane, fb);
953                         if (ret)
954                                 goto fail;
955                 }
956         }
957
958         return 0;
959
960 fail:
961         for (i--; i >= 0; i--) {
962                 struct drm_plane_helper_funcs *funcs;
963                 struct drm_plane *plane = state->planes[i];
964                 struct drm_framebuffer *fb;
965
966                 if (!plane)
967                         continue;
968
969                 funcs = plane->helper_private;
970
971                 fb = state->plane_states[i]->fb;
972
973                 if (fb && funcs->cleanup_fb)
974                         funcs->cleanup_fb(plane, fb);
975
976         }
977
978         return ret;
979 }
980 EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
981
982 /**
983  * drm_atomic_helper_commit_planes - commit plane state
984  * @dev: DRM device
985  * @state: atomic state
986  *
987  * This function commits the new plane state using the plane and atomic helper
988  * functions for planes and crtcs. It assumes that the atomic state has already
989  * been pushed into the relevant object state pointers, since this step can no
990  * longer fail.
991  *
992  * It still requires the global state object @state to know which planes and
993  * crtcs need to be updated though.
994  */
995 void drm_atomic_helper_commit_planes(struct drm_device *dev,
996                                      struct drm_atomic_state *state)
997 {
998         int nplanes = dev->mode_config.num_total_plane;
999         int ncrtcs = dev->mode_config.num_crtc;
1000         int i;
1001
1002         for (i = 0; i < ncrtcs; i++) {
1003                 struct drm_crtc_helper_funcs *funcs;
1004                 struct drm_crtc *crtc = state->crtcs[i];
1005
1006                 if (!crtc)
1007                         continue;
1008
1009                 funcs = crtc->helper_private;
1010
1011                 if (!funcs || !funcs->atomic_begin)
1012                         continue;
1013
1014                 funcs->atomic_begin(crtc);
1015         }
1016
1017         for (i = 0; i < nplanes; i++) {
1018                 struct drm_plane_helper_funcs *funcs;
1019                 struct drm_plane *plane = state->planes[i];
1020
1021                 if (!plane)
1022                         continue;
1023
1024                 funcs = plane->helper_private;
1025
1026                 if (!funcs || !funcs->atomic_update)
1027                         continue;
1028
1029                 funcs->atomic_update(plane);
1030         }
1031
1032         for (i = 0; i < ncrtcs; i++) {
1033                 struct drm_crtc_helper_funcs *funcs;
1034                 struct drm_crtc *crtc = state->crtcs[i];
1035
1036                 if (!crtc)
1037                         continue;
1038
1039                 funcs = crtc->helper_private;
1040
1041                 if (!funcs || !funcs->atomic_flush)
1042                         continue;
1043
1044                 funcs->atomic_flush(crtc);
1045         }
1046 }
1047 EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
1048
1049 /**
1050  * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
1051  * @dev: DRM device
1052  * @old_state: atomic state object with old state structures
1053  *
1054  * This function cleans up plane state, specifically framebuffers, from the old
1055  * configuration. Hence the old configuration must be perserved in @old_state to
1056  * be able to call this function.
1057  *
1058  * This function must also be called on the new state when the atomic update
1059  * fails at any point after calling drm_atomic_helper_prepare_planes().
1060  */
1061 void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
1062                                       struct drm_atomic_state *old_state)
1063 {
1064         int nplanes = dev->mode_config.num_total_plane;
1065         int i;
1066
1067         for (i = 0; i < nplanes; i++) {
1068                 struct drm_plane_helper_funcs *funcs;
1069                 struct drm_plane *plane = old_state->planes[i];
1070                 struct drm_framebuffer *old_fb;
1071
1072                 if (!plane)
1073                         continue;
1074
1075                 funcs = plane->helper_private;
1076
1077                 old_fb = old_state->plane_states[i]->fb;
1078
1079                 if (old_fb && funcs->cleanup_fb)
1080                         funcs->cleanup_fb(plane, old_fb);
1081         }
1082 }
1083 EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
1084
1085 /**
1086  * drm_atomic_helper_swap_state - store atomic state into current sw state
1087  * @dev: DRM device
1088  * @state: atomic state
1089  *
1090  * This function stores the atomic state into the current state pointers in all
1091  * driver objects. It should be called after all failing steps have been done
1092  * and succeeded, but before the actual hardware state is committed.
1093  *
1094  * For cleanup and error recovery the current state for all changed objects will
1095  * be swaped into @state.
1096  *
1097  * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
1098  *
1099  * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
1100  *
1101  * 2. Do any other steps that might fail.
1102  *
1103  * 3. Put the staged state into the current state pointers with this function.
1104  *
1105  * 4. Actually commit the hardware state.
1106  *
1107  * 5. Call drm_atomic_helper_cleanup_planes with @state, which since step 3
1108  * contains the old state. Also do any other cleanup required with that state.
1109  */
1110 void drm_atomic_helper_swap_state(struct drm_device *dev,
1111                                   struct drm_atomic_state *state)
1112 {
1113         int i;
1114
1115         for (i = 0; i < dev->mode_config.num_connector; i++) {
1116                 struct drm_connector *connector = state->connectors[i];
1117
1118                 if (!connector)
1119                         continue;
1120
1121                 connector->state->state = state;
1122                 swap(state->connector_states[i], connector->state);
1123                 connector->state->state = NULL;
1124         }
1125
1126         for (i = 0; i < dev->mode_config.num_crtc; i++) {
1127                 struct drm_crtc *crtc = state->crtcs[i];
1128
1129                 if (!crtc)
1130                         continue;
1131
1132                 crtc->state->state = state;
1133                 swap(state->crtc_states[i], crtc->state);
1134                 crtc->state->state = NULL;
1135         }
1136
1137         for (i = 0; i < dev->mode_config.num_total_plane; i++) {
1138                 struct drm_plane *plane = state->planes[i];
1139
1140                 if (!plane)
1141                         continue;
1142
1143                 plane->state->state = state;
1144                 swap(state->plane_states[i], plane->state);
1145                 plane->state->state = NULL;
1146         }
1147 }
1148 EXPORT_SYMBOL(drm_atomic_helper_swap_state);
1149
1150 /**
1151  * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
1152  * @plane: plane object to update
1153  * @crtc: owning CRTC of owning plane
1154  * @fb: framebuffer to flip onto plane
1155  * @crtc_x: x offset of primary plane on crtc
1156  * @crtc_y: y offset of primary plane on crtc
1157  * @crtc_w: width of primary plane rectangle on crtc
1158  * @crtc_h: height of primary plane rectangle on crtc
1159  * @src_x: x offset of @fb for panning
1160  * @src_y: y offset of @fb for panning
1161  * @src_w: width of source rectangle in @fb
1162  * @src_h: height of source rectangle in @fb
1163  *
1164  * Provides a default plane update handler using the atomic driver interface.
1165  *
1166  * RETURNS:
1167  * Zero on success, error code on failure
1168  */
1169 int drm_atomic_helper_update_plane(struct drm_plane *plane,
1170                                    struct drm_crtc *crtc,
1171                                    struct drm_framebuffer *fb,
1172                                    int crtc_x, int crtc_y,
1173                                    unsigned int crtc_w, unsigned int crtc_h,
1174                                    uint32_t src_x, uint32_t src_y,
1175                                    uint32_t src_w, uint32_t src_h)
1176 {
1177         struct drm_atomic_state *state;
1178         struct drm_plane_state *plane_state;
1179         int ret = 0;
1180
1181         state = drm_atomic_state_alloc(plane->dev);
1182         if (!state)
1183                 return -ENOMEM;
1184
1185         state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1186 retry:
1187         plane_state = drm_atomic_get_plane_state(state, plane);
1188         if (IS_ERR(plane_state)) {
1189                 ret = PTR_ERR(plane_state);
1190                 goto fail;
1191         }
1192
1193         ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
1194         if (ret != 0)
1195                 goto fail;
1196         drm_atomic_set_fb_for_plane(plane_state, fb);
1197         plane_state->crtc_x = crtc_x;
1198         plane_state->crtc_y = crtc_y;
1199         plane_state->crtc_h = crtc_h;
1200         plane_state->crtc_w = crtc_w;
1201         plane_state->src_x = src_x;
1202         plane_state->src_y = src_y;
1203         plane_state->src_h = src_h;
1204         plane_state->src_w = src_w;
1205
1206         ret = drm_atomic_commit(state);
1207         if (ret != 0)
1208                 goto fail;
1209
1210         /* Driver takes ownership of state on successful commit. */
1211         return 0;
1212 fail:
1213         if (ret == -EDEADLK)
1214                 goto backoff;
1215
1216         drm_atomic_state_free(state);
1217
1218         return ret;
1219 backoff:
1220         drm_atomic_legacy_backoff(state);
1221         drm_atomic_state_clear(state);
1222
1223         /*
1224          * Someone might have exchanged the framebuffer while we dropped locks
1225          * in the backoff code. We need to fix up the fb refcount tracking the
1226          * core does for us.
1227          */
1228         plane->old_fb = plane->fb;
1229
1230         goto retry;
1231 }
1232 EXPORT_SYMBOL(drm_atomic_helper_update_plane);
1233
1234 /**
1235  * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
1236  * @plane: plane to disable
1237  *
1238  * Provides a default plane disable handler using the atomic driver interface.
1239  *
1240  * RETURNS:
1241  * Zero on success, error code on failure
1242  */
1243 int drm_atomic_helper_disable_plane(struct drm_plane *plane)
1244 {
1245         struct drm_atomic_state *state;
1246         struct drm_plane_state *plane_state;
1247         int ret = 0;
1248
1249         state = drm_atomic_state_alloc(plane->dev);
1250         if (!state)
1251                 return -ENOMEM;
1252
1253         state->acquire_ctx = drm_modeset_legacy_acquire_ctx(plane->crtc);
1254 retry:
1255         plane_state = drm_atomic_get_plane_state(state, plane);
1256         if (IS_ERR(plane_state)) {
1257                 ret = PTR_ERR(plane_state);
1258                 goto fail;
1259         }
1260
1261         ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
1262         if (ret != 0)
1263                 goto fail;
1264         drm_atomic_set_fb_for_plane(plane_state, NULL);
1265         plane_state->crtc_x = 0;
1266         plane_state->crtc_y = 0;
1267         plane_state->crtc_h = 0;
1268         plane_state->crtc_w = 0;
1269         plane_state->src_x = 0;
1270         plane_state->src_y = 0;
1271         plane_state->src_h = 0;
1272         plane_state->src_w = 0;
1273
1274         ret = drm_atomic_commit(state);
1275         if (ret != 0)
1276                 goto fail;
1277
1278         /* Driver takes ownership of state on successful commit. */
1279         return 0;
1280 fail:
1281         if (ret == -EDEADLK)
1282                 goto backoff;
1283
1284         drm_atomic_state_free(state);
1285
1286         return ret;
1287 backoff:
1288         drm_atomic_legacy_backoff(state);
1289         drm_atomic_state_clear(state);
1290
1291         /*
1292          * Someone might have exchanged the framebuffer while we dropped locks
1293          * in the backoff code. We need to fix up the fb refcount tracking the
1294          * core does for us.
1295          */
1296         plane->old_fb = plane->fb;
1297
1298         goto retry;
1299 }
1300 EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
1301
1302 static int update_output_state(struct drm_atomic_state *state,
1303                                struct drm_mode_set *set)
1304 {
1305         struct drm_device *dev = set->crtc->dev;
1306         struct drm_connector_state *conn_state;
1307         int nconnectors = state->dev->mode_config.num_connector;
1308         int ncrtcs = state->dev->mode_config.num_crtc;
1309         int ret, i, j;
1310
1311         ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
1312                                state->acquire_ctx);
1313         if (ret)
1314                 return ret;
1315
1316         /* First grab all affected connector/crtc states. */
1317         for (i = 0; i < set->num_connectors; i++) {
1318                 conn_state = drm_atomic_get_connector_state(state,
1319                                                             set->connectors[i]);
1320                 if (IS_ERR(conn_state))
1321                         return PTR_ERR(conn_state);
1322         }
1323
1324         for (i = 0; i < ncrtcs; i++) {
1325                 struct drm_crtc *crtc = state->crtcs[i];
1326
1327                 if (!crtc)
1328                         continue;
1329
1330                 ret = drm_atomic_add_affected_connectors(state, crtc);
1331                 if (ret)
1332                         return ret;
1333         }
1334
1335         /* Then recompute connector->crtc links and crtc enabling state. */
1336         for (i = 0; i < nconnectors; i++) {
1337                 struct drm_connector *connector;
1338
1339                 connector = state->connectors[i];
1340                 conn_state = state->connector_states[i];
1341
1342                 if (!connector)
1343                         continue;
1344
1345                 if (conn_state->crtc == set->crtc) {
1346                         ret = drm_atomic_set_crtc_for_connector(conn_state,
1347                                                                 NULL);
1348                         if (ret)
1349                                 return ret;
1350                 }
1351
1352                 for (j = 0; j < set->num_connectors; j++) {
1353                         if (set->connectors[j] == connector) {
1354                                 ret = drm_atomic_set_crtc_for_connector(conn_state,
1355                                                                         set->crtc);
1356                                 if (ret)
1357                                         return ret;
1358                                 break;
1359                         }
1360                 }
1361         }
1362
1363         for (i = 0; i < ncrtcs; i++) {
1364                 struct drm_crtc *crtc = state->crtcs[i];
1365                 struct drm_crtc_state *crtc_state = state->crtc_states[i];
1366
1367                 if (!crtc)
1368                         continue;
1369
1370                 /* Don't update ->enable for the CRTC in the set_config request,
1371                  * since a mismatch would indicate a bug in the upper layers.
1372                  * The actual modeset code later on will catch any
1373                  * inconsistencies here. */
1374                 if (crtc == set->crtc)
1375                         continue;
1376
1377                 crtc_state->enable =
1378                         drm_atomic_connectors_for_crtc(state, crtc);
1379         }
1380
1381         return 0;
1382 }
1383
1384 /**
1385  * drm_atomic_helper_set_config - set a new config from userspace
1386  * @set: mode set configuration
1387  *
1388  * Provides a default crtc set_config handler using the atomic driver interface.
1389  *
1390  * Returns:
1391  * Returns 0 on success, negative errno numbers on failure.
1392  */
1393 int drm_atomic_helper_set_config(struct drm_mode_set *set)
1394 {
1395         struct drm_atomic_state *state;
1396         struct drm_crtc *crtc = set->crtc;
1397         struct drm_crtc_state *crtc_state;
1398         struct drm_plane_state *primary_state;
1399         int ret = 0;
1400
1401         state = drm_atomic_state_alloc(crtc->dev);
1402         if (!state)
1403                 return -ENOMEM;
1404
1405         state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1406 retry:
1407         crtc_state = drm_atomic_get_crtc_state(state, crtc);
1408         if (IS_ERR(crtc_state)) {
1409                 ret = PTR_ERR(crtc_state);
1410                 goto fail;
1411         }
1412
1413         if (!set->mode) {
1414                 WARN_ON(set->fb);
1415                 WARN_ON(set->num_connectors);
1416
1417                 crtc_state->enable = false;
1418                 goto commit;
1419         }
1420
1421         WARN_ON(!set->fb);
1422         WARN_ON(!set->num_connectors);
1423
1424         crtc_state->enable = true;
1425         drm_mode_copy(&crtc_state->mode, set->mode);
1426
1427         primary_state = drm_atomic_get_plane_state(state, crtc->primary);
1428         if (IS_ERR(primary_state)) {
1429                 ret = PTR_ERR(primary_state);
1430                 goto fail;
1431         }
1432
1433         ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
1434         if (ret != 0)
1435                 goto fail;
1436         drm_atomic_set_fb_for_plane(primary_state, set->fb);
1437         primary_state->crtc_x = 0;
1438         primary_state->crtc_y = 0;
1439         primary_state->crtc_h = set->mode->vdisplay;
1440         primary_state->crtc_w = set->mode->hdisplay;
1441         primary_state->src_x = set->x << 16;
1442         primary_state->src_y = set->y << 16;
1443         primary_state->src_h = set->mode->vdisplay << 16;
1444         primary_state->src_w = set->mode->hdisplay << 16;
1445
1446 commit:
1447         ret = update_output_state(state, set);
1448         if (ret)
1449                 goto fail;
1450
1451         ret = drm_atomic_commit(state);
1452         if (ret != 0)
1453                 goto fail;
1454
1455         /* Driver takes ownership of state on successful commit. */
1456         return 0;
1457 fail:
1458         if (ret == -EDEADLK)
1459                 goto backoff;
1460
1461         drm_atomic_state_free(state);
1462
1463         return ret;
1464 backoff:
1465         drm_atomic_legacy_backoff(state);
1466         drm_atomic_state_clear(state);
1467
1468         /*
1469          * Someone might have exchanged the framebuffer while we dropped locks
1470          * in the backoff code. We need to fix up the fb refcount tracking the
1471          * core does for us.
1472          */
1473         crtc->primary->old_fb = crtc->primary->fb;
1474
1475         goto retry;
1476 }
1477 EXPORT_SYMBOL(drm_atomic_helper_set_config);
1478
1479 /**
1480  * drm_atomic_helper_crtc_set_property - helper for crtc prorties
1481  * @crtc: DRM crtc
1482  * @property: DRM property
1483  * @val: value of property
1484  *
1485  * Provides a default plane disablle handler using the atomic driver interface.
1486  *
1487  * RETURNS:
1488  * Zero on success, error code on failure
1489  */
1490 int
1491 drm_atomic_helper_crtc_set_property(struct drm_crtc *crtc,
1492                                     struct drm_property *property,
1493                                     uint64_t val)
1494 {
1495         struct drm_atomic_state *state;
1496         struct drm_crtc_state *crtc_state;
1497         int ret = 0;
1498
1499         state = drm_atomic_state_alloc(crtc->dev);
1500         if (!state)
1501                 return -ENOMEM;
1502
1503         /* ->set_property is always called with all locks held. */
1504         state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
1505 retry:
1506         crtc_state = drm_atomic_get_crtc_state(state, crtc);
1507         if (IS_ERR(crtc_state)) {
1508                 ret = PTR_ERR(crtc_state);
1509                 goto fail;
1510         }
1511
1512         ret = crtc->funcs->atomic_set_property(crtc, crtc_state,
1513                                                property, val);
1514         if (ret)
1515                 goto fail;
1516
1517         ret = drm_atomic_commit(state);
1518         if (ret != 0)
1519                 goto fail;
1520
1521         /* Driver takes ownership of state on successful commit. */
1522         return 0;
1523 fail:
1524         if (ret == -EDEADLK)
1525                 goto backoff;
1526
1527         drm_atomic_state_free(state);
1528
1529         return ret;
1530 backoff:
1531         drm_atomic_legacy_backoff(state);
1532         drm_atomic_state_clear(state);
1533
1534         goto retry;
1535 }
1536 EXPORT_SYMBOL(drm_atomic_helper_crtc_set_property);
1537
1538 /**
1539  * drm_atomic_helper_plane_set_property - helper for plane prorties
1540  * @plane: DRM plane
1541  * @property: DRM property
1542  * @val: value of property
1543  *
1544  * Provides a default plane disable handler using the atomic driver interface.
1545  *
1546  * RETURNS:
1547  * Zero on success, error code on failure
1548  */
1549 int
1550 drm_atomic_helper_plane_set_property(struct drm_plane *plane,
1551                                     struct drm_property *property,
1552                                     uint64_t val)
1553 {
1554         struct drm_atomic_state *state;
1555         struct drm_plane_state *plane_state;
1556         int ret = 0;
1557
1558         state = drm_atomic_state_alloc(plane->dev);
1559         if (!state)
1560                 return -ENOMEM;
1561
1562         /* ->set_property is always called with all locks held. */
1563         state->acquire_ctx = plane->dev->mode_config.acquire_ctx;
1564 retry:
1565         plane_state = drm_atomic_get_plane_state(state, plane);
1566         if (IS_ERR(plane_state)) {
1567                 ret = PTR_ERR(plane_state);
1568                 goto fail;
1569         }
1570
1571         ret = plane->funcs->atomic_set_property(plane, plane_state,
1572                                                property, val);
1573         if (ret)
1574                 goto fail;
1575
1576         ret = drm_atomic_commit(state);
1577         if (ret != 0)
1578                 goto fail;
1579
1580         /* Driver takes ownership of state on successful commit. */
1581         return 0;
1582 fail:
1583         if (ret == -EDEADLK)
1584                 goto backoff;
1585
1586         drm_atomic_state_free(state);
1587
1588         return ret;
1589 backoff:
1590         drm_atomic_legacy_backoff(state);
1591         drm_atomic_state_clear(state);
1592
1593         goto retry;
1594 }
1595 EXPORT_SYMBOL(drm_atomic_helper_plane_set_property);
1596
1597 /**
1598  * drm_atomic_helper_connector_set_property - helper for connector prorties
1599  * @connector: DRM connector
1600  * @property: DRM property
1601  * @val: value of property
1602  *
1603  * Provides a default plane disablle handler using the atomic driver interface.
1604  *
1605  * RETURNS:
1606  * Zero on success, error code on failure
1607  */
1608 int
1609 drm_atomic_helper_connector_set_property(struct drm_connector *connector,
1610                                     struct drm_property *property,
1611                                     uint64_t val)
1612 {
1613         struct drm_atomic_state *state;
1614         struct drm_connector_state *connector_state;
1615         int ret = 0;
1616
1617         state = drm_atomic_state_alloc(connector->dev);
1618         if (!state)
1619                 return -ENOMEM;
1620
1621         /* ->set_property is always called with all locks held. */
1622         state->acquire_ctx = connector->dev->mode_config.acquire_ctx;
1623 retry:
1624         connector_state = drm_atomic_get_connector_state(state, connector);
1625         if (IS_ERR(connector_state)) {
1626                 ret = PTR_ERR(connector_state);
1627                 goto fail;
1628         }
1629
1630         ret = connector->funcs->atomic_set_property(connector, connector_state,
1631                                                property, val);
1632         if (ret)
1633                 goto fail;
1634
1635         ret = drm_atomic_commit(state);
1636         if (ret != 0)
1637                 goto fail;
1638
1639         /* Driver takes ownership of state on successful commit. */
1640         return 0;
1641 fail:
1642         if (ret == -EDEADLK)
1643                 goto backoff;
1644
1645         drm_atomic_state_free(state);
1646
1647         return ret;
1648 backoff:
1649         drm_atomic_legacy_backoff(state);
1650         drm_atomic_state_clear(state);
1651
1652         goto retry;
1653 }
1654 EXPORT_SYMBOL(drm_atomic_helper_connector_set_property);
1655
1656 /**
1657  * drm_atomic_helper_page_flip - execute a legacy page flip
1658  * @crtc: DRM crtc
1659  * @fb: DRM framebuffer
1660  * @event: optional DRM event to signal upon completion
1661  * @flags: flip flags for non-vblank sync'ed updates
1662  *
1663  * Provides a default page flip implementation using the atomic driver interface.
1664  *
1665  * Note that for now so called async page flips (i.e. updates which are not
1666  * synchronized to vblank) are not supported, since the atomic interfaces have
1667  * no provisions for this yet.
1668  *
1669  * Returns:
1670  * Returns 0 on success, negative errno numbers on failure.
1671  */
1672 int drm_atomic_helper_page_flip(struct drm_crtc *crtc,
1673                                 struct drm_framebuffer *fb,
1674                                 struct drm_pending_vblank_event *event,
1675                                 uint32_t flags)
1676 {
1677         struct drm_plane *plane = crtc->primary;
1678         struct drm_atomic_state *state;
1679         struct drm_plane_state *plane_state;
1680         struct drm_crtc_state *crtc_state;
1681         int ret = 0;
1682
1683         if (flags & DRM_MODE_PAGE_FLIP_ASYNC)
1684                 return -EINVAL;
1685
1686         state = drm_atomic_state_alloc(plane->dev);
1687         if (!state)
1688                 return -ENOMEM;
1689
1690         state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1691 retry:
1692         crtc_state = drm_atomic_get_crtc_state(state, crtc);
1693         if (IS_ERR(crtc_state)) {
1694                 ret = PTR_ERR(crtc_state);
1695                 goto fail;
1696         }
1697         crtc_state->event = event;
1698
1699         plane_state = drm_atomic_get_plane_state(state, plane);
1700         if (IS_ERR(plane_state)) {
1701                 ret = PTR_ERR(plane_state);
1702                 goto fail;
1703         }
1704
1705         ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
1706         if (ret != 0)
1707                 goto fail;
1708         drm_atomic_set_fb_for_plane(plane_state, fb);
1709
1710         ret = drm_atomic_async_commit(state);
1711         if (ret != 0)
1712                 goto fail;
1713
1714         /* TODO: ->page_flip is the only driver callback where the core
1715          * doesn't update plane->fb. For now patch it up here. */
1716         plane->fb = plane->state->fb;
1717
1718         /* Driver takes ownership of state on successful async commit. */
1719         return 0;
1720 fail:
1721         if (ret == -EDEADLK)
1722                 goto backoff;
1723
1724         drm_atomic_state_free(state);
1725
1726         return ret;
1727 backoff:
1728         drm_atomic_legacy_backoff(state);
1729         drm_atomic_state_clear(state);
1730
1731         /*
1732          * Someone might have exchanged the framebuffer while we dropped locks
1733          * in the backoff code. We need to fix up the fb refcount tracking the
1734          * core does for us.
1735          */
1736         plane->old_fb = plane->fb;
1737
1738         goto retry;
1739 }
1740 EXPORT_SYMBOL(drm_atomic_helper_page_flip);
1741
1742 /**
1743  * DOC: atomic state reset and initialization
1744  *
1745  * Both the drm core and the atomic helpers assume that there is always the full
1746  * and correct atomic software state for all connectors, CRTCs and planes
1747  * available. Which is a bit a problem on driver load and also after system
1748  * suspend. One way to solve this is to have a hardware state read-out
1749  * infrastructure which reconstructs the full software state (e.g. the i915
1750  * driver).
1751  *
1752  * The simpler solution is to just reset the software state to everything off,
1753  * which is easiest to do by calling drm_mode_config_reset(). To facilitate this
1754  * the atomic helpers provide default reset implementations for all hooks.
1755  */
1756
1757 /**
1758  * drm_atomic_helper_crtc_reset - default ->reset hook for CRTCs
1759  * @crtc: drm CRTC
1760  *
1761  * Resets the atomic state for @crtc by freeing the state pointer (which might
1762  * be NULL, e.g. at driver load time) and allocating a new empty state object.
1763  */
1764 void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc)
1765 {
1766         kfree(crtc->state);
1767         crtc->state = kzalloc(sizeof(*crtc->state), GFP_KERNEL);
1768 }
1769 EXPORT_SYMBOL(drm_atomic_helper_crtc_reset);
1770
1771 /**
1772  * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
1773  * @crtc: drm CRTC
1774  *
1775  * Default CRTC state duplicate hook for drivers which don't have their own
1776  * subclassed CRTC state structure.
1777  */
1778 struct drm_crtc_state *
1779 drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc)
1780 {
1781         struct drm_crtc_state *state;
1782
1783         if (WARN_ON(!crtc->state))
1784                 return NULL;
1785
1786         state = kmemdup(crtc->state, sizeof(*crtc->state), GFP_KERNEL);
1787
1788         if (state) {
1789                 state->mode_changed = false;
1790                 state->planes_changed = false;
1791                 state->event = NULL;
1792         }
1793
1794         return state;
1795 }
1796 EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state);
1797
1798 /**
1799  * drm_atomic_helper_crtc_destroy_state - default state destroy hook
1800  * @crtc: drm CRTC
1801  * @state: CRTC state object to release
1802  *
1803  * Default CRTC state destroy hook for drivers which don't have their own
1804  * subclassed CRTC state structure.
1805  */
1806 void drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
1807                                           struct drm_crtc_state *state)
1808 {
1809         kfree(state);
1810 }
1811 EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state);
1812
1813 /**
1814  * drm_atomic_helper_plane_reset - default ->reset hook for planes
1815  * @plane: drm plane
1816  *
1817  * Resets the atomic state for @plane by freeing the state pointer (which might
1818  * be NULL, e.g. at driver load time) and allocating a new empty state object.
1819  */
1820 void drm_atomic_helper_plane_reset(struct drm_plane *plane)
1821 {
1822         if (plane->state && plane->state->fb)
1823                 drm_framebuffer_unreference(plane->state->fb);
1824
1825         kfree(plane->state);
1826         plane->state = kzalloc(sizeof(*plane->state), GFP_KERNEL);
1827 }
1828 EXPORT_SYMBOL(drm_atomic_helper_plane_reset);
1829
1830 /**
1831  * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
1832  * @plane: drm plane
1833  *
1834  * Default plane state duplicate hook for drivers which don't have their own
1835  * subclassed plane state structure.
1836  */
1837 struct drm_plane_state *
1838 drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane)
1839 {
1840         struct drm_plane_state *state;
1841
1842         if (WARN_ON(!plane->state))
1843                 return NULL;
1844
1845         state = kmemdup(plane->state, sizeof(*plane->state), GFP_KERNEL);
1846
1847         if (state && state->fb)
1848                 drm_framebuffer_reference(state->fb);
1849
1850         return state;
1851 }
1852 EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state);
1853
1854 /**
1855  * drm_atomic_helper_plane_destroy_state - default state destroy hook
1856  * @plane: drm plane
1857  * @state: plane state object to release
1858  *
1859  * Default plane state destroy hook for drivers which don't have their own
1860  * subclassed plane state structure.
1861  */
1862 void drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
1863                                            struct drm_plane_state *state)
1864 {
1865         if (state->fb)
1866                 drm_framebuffer_unreference(state->fb);
1867
1868         kfree(state);
1869 }
1870 EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state);
1871
1872 /**
1873  * drm_atomic_helper_connector_reset - default ->reset hook for connectors
1874  * @connector: drm connector
1875  *
1876  * Resets the atomic state for @connector by freeing the state pointer (which
1877  * might be NULL, e.g. at driver load time) and allocating a new empty state
1878  * object.
1879  */
1880 void drm_atomic_helper_connector_reset(struct drm_connector *connector)
1881 {
1882         kfree(connector->state);
1883         connector->state = kzalloc(sizeof(*connector->state), GFP_KERNEL);
1884 }
1885 EXPORT_SYMBOL(drm_atomic_helper_connector_reset);
1886
1887 /**
1888  * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
1889  * @connector: drm connector
1890  *
1891  * Default connector state duplicate hook for drivers which don't have their own
1892  * subclassed connector state structure.
1893  */
1894 struct drm_connector_state *
1895 drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
1896 {
1897         if (WARN_ON(!connector->state))
1898                 return NULL;
1899
1900         return kmemdup(connector->state, sizeof(*connector->state), GFP_KERNEL);
1901 }
1902 EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
1903
1904 /**
1905  * drm_atomic_helper_connector_destroy_state - default state destroy hook
1906  * @connector: drm connector
1907  * @state: connector state object to release
1908  *
1909  * Default connector state destroy hook for drivers which don't have their own
1910  * subclassed connector state structure.
1911  */
1912 void drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
1913                                           struct drm_connector_state *state)
1914 {
1915         kfree(state);
1916 }
1917 EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state);