Merge tag 'lsk-v4.4-16.07-android'
[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
47  * exposed 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->state->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         drm_for_each_connector(connector, dev) {
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_ATOMIC("[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->connectors_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_ATOMIC("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         const 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_ATOMIC("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->connectors_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->connectors_changed = true;
185                 }
186         }
187
188         if (!connector_state->crtc) {
189                 DRM_DEBUG_ATOMIC("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
200         if (funcs->atomic_best_encoder)
201                 new_encoder = funcs->atomic_best_encoder(connector,
202                                                          connector_state);
203         else
204                 new_encoder = funcs->best_encoder(connector);
205
206         if (!new_encoder) {
207                 DRM_DEBUG_ATOMIC("No suitable encoder found for [CONNECTOR:%d:%s]\n",
208                                  connector->base.id,
209                                  connector->name);
210                 return -EINVAL;
211         }
212
213         if (!drm_encoder_crtc_ok(new_encoder, connector_state->crtc)) {
214                 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] incompatible with [CRTC:%d]\n",
215                                  new_encoder->base.id,
216                                  new_encoder->name,
217                                  connector_state->crtc->base.id);
218                 return -EINVAL;
219         }
220
221         if (new_encoder == connector_state->best_encoder) {
222                 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] keeps [ENCODER:%d:%s], now on [CRTC:%d]\n",
223                                  connector->base.id,
224                                  connector->name,
225                                  new_encoder->base.id,
226                                  new_encoder->name,
227                                  connector_state->crtc->base.id);
228
229                 return 0;
230         }
231
232         encoder_crtc = get_current_crtc_for_encoder(state->dev,
233                                                     new_encoder);
234
235         if (encoder_crtc) {
236                 ret = steal_encoder(state, new_encoder, encoder_crtc);
237                 if (ret) {
238                         DRM_DEBUG_ATOMIC("Encoder stealing failed for [CONNECTOR:%d:%s]\n",
239                                          connector->base.id,
240                                          connector->name);
241                         return ret;
242                 }
243         }
244
245         if (WARN_ON(!connector_state->crtc))
246                 return -EINVAL;
247
248         connector_state->best_encoder = new_encoder;
249         idx = drm_crtc_index(connector_state->crtc);
250
251         crtc_state = state->crtc_states[idx];
252         crtc_state->connectors_changed = true;
253
254         DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] using [ENCODER:%d:%s] on [CRTC:%d]\n",
255                          connector->base.id,
256                          connector->name,
257                          new_encoder->base.id,
258                          new_encoder->name,
259                          connector_state->crtc->base.id);
260
261         return 0;
262 }
263
264 static int
265 mode_fixup(struct drm_atomic_state *state)
266 {
267         struct drm_crtc *crtc;
268         struct drm_crtc_state *crtc_state;
269         struct drm_connector *connector;
270         struct drm_connector_state *conn_state;
271         int i;
272         bool ret;
273
274         for_each_crtc_in_state(state, crtc, crtc_state, i) {
275                 if (!crtc_state->mode_changed &&
276                     !crtc_state->connectors_changed)
277                         continue;
278
279                 drm_mode_copy(&crtc_state->adjusted_mode, &crtc_state->mode);
280         }
281
282         for_each_connector_in_state(state, connector, conn_state, i) {
283                 const struct drm_encoder_helper_funcs *funcs;
284                 struct drm_encoder *encoder;
285
286                 WARN_ON(!!conn_state->best_encoder != !!conn_state->crtc);
287
288                 if (!conn_state->crtc || !conn_state->best_encoder)
289                         continue;
290
291                 crtc_state =
292                         state->crtc_states[drm_crtc_index(conn_state->crtc)];
293
294                 /*
295                  * Each encoder has at most one connector (since we always steal
296                  * it away), so we won't call ->mode_fixup twice.
297                  */
298                 encoder = conn_state->best_encoder;
299                 funcs = encoder->helper_private;
300                 if (!funcs)
301                         continue;
302
303                 ret = drm_bridge_mode_fixup(encoder->bridge, &crtc_state->mode,
304                                 &crtc_state->adjusted_mode);
305                 if (!ret) {
306                         DRM_DEBUG_ATOMIC("Bridge fixup failed\n");
307                         return -EINVAL;
308                 }
309
310                 if (funcs->atomic_check) {
311                         ret = funcs->atomic_check(encoder, crtc_state,
312                                                   conn_state);
313                         if (ret) {
314                                 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] check failed\n",
315                                                  encoder->base.id, encoder->name);
316                                 return ret;
317                         }
318                 } else if (funcs->mode_fixup) {
319                         ret = funcs->mode_fixup(encoder, &crtc_state->mode,
320                                                 &crtc_state->adjusted_mode);
321                         if (!ret) {
322                                 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] fixup failed\n",
323                                                  encoder->base.id, encoder->name);
324                                 return -EINVAL;
325                         }
326                 }
327         }
328
329         for_each_crtc_in_state(state, crtc, crtc_state, i) {
330                 const struct drm_crtc_helper_funcs *funcs;
331
332                 if (!crtc_state->mode_changed &&
333                     !crtc_state->connectors_changed)
334                         continue;
335
336                 funcs = crtc->helper_private;
337                 if (!funcs->mode_fixup)
338                         continue;
339
340                 ret = funcs->mode_fixup(crtc, &crtc_state->mode,
341                                         &crtc_state->adjusted_mode);
342                 if (!ret) {
343                         DRM_DEBUG_ATOMIC("[CRTC:%d] fixup failed\n",
344                                          crtc->base.id);
345                         return -EINVAL;
346                 }
347         }
348
349         return 0;
350 }
351
352 /**
353  * drm_atomic_helper_check_modeset - validate state object for modeset changes
354  * @dev: DRM device
355  * @state: the driver state object
356  *
357  * Check the state object to see if the requested state is physically possible.
358  * This does all the crtc and connector related computations for an atomic
359  * update and adds any additional connectors needed for full modesets and calls
360  * down into ->mode_fixup functions of the driver backend.
361  *
362  * crtc_state->mode_changed is set when the input mode is changed.
363  * crtc_state->connectors_changed is set when a connector is added or
364  * removed from the crtc.
365  * crtc_state->active_changed is set when crtc_state->active changes,
366  * which is used for dpms.
367  *
368  * IMPORTANT:
369  *
370  * Drivers which update ->mode_changed (e.g. in their ->atomic_check hooks if a
371  * plane update can't be done without a full modeset) _must_ call this function
372  * afterwards after that change. It is permitted to call this function multiple
373  * times for the same update, e.g. when the ->atomic_check functions depend upon
374  * the adjusted dotclock for fifo space allocation and watermark computation.
375  *
376  * RETURNS
377  * Zero for success or -errno
378  */
379 int
380 drm_atomic_helper_check_modeset(struct drm_device *dev,
381                                 struct drm_atomic_state *state)
382 {
383         struct drm_crtc *crtc;
384         struct drm_crtc_state *crtc_state;
385         struct drm_connector *connector;
386         struct drm_connector_state *connector_state;
387         int i, ret;
388
389         for_each_crtc_in_state(state, crtc, crtc_state, i) {
390                 if (!drm_mode_equal(&crtc->state->mode, &crtc_state->mode)) {
391                         DRM_DEBUG_ATOMIC("[CRTC:%d] mode changed\n",
392                                          crtc->base.id);
393                         crtc_state->mode_changed = true;
394                 }
395
396                 if (crtc->state->enable != crtc_state->enable) {
397                         DRM_DEBUG_ATOMIC("[CRTC:%d] enable changed\n",
398                                          crtc->base.id);
399
400                         /*
401                          * For clarity this assignment is done here, but
402                          * enable == 0 is only true when there are no
403                          * connectors and a NULL mode.
404                          *
405                          * The other way around is true as well. enable != 0
406                          * iff connectors are attached and a mode is set.
407                          */
408                         crtc_state->mode_changed = true;
409                         crtc_state->connectors_changed = true;
410                 }
411         }
412
413         for_each_connector_in_state(state, connector, connector_state, i) {
414                 /*
415                  * This only sets crtc->mode_changed for routing changes,
416                  * drivers must set crtc->mode_changed themselves when connector
417                  * properties need to be updated.
418                  */
419                 ret = update_connector_routing(state, i);
420                 if (ret)
421                         return ret;
422         }
423
424         /*
425          * After all the routing has been prepared we need to add in any
426          * connector which is itself unchanged, but who's crtc changes it's
427          * configuration. This must be done before calling mode_fixup in case a
428          * crtc only changed its mode but has the same set of connectors.
429          */
430         for_each_crtc_in_state(state, crtc, crtc_state, i) {
431                 int num_connectors;
432
433                 /*
434                  * We must set ->active_changed after walking connectors for
435                  * otherwise an update that only changes active would result in
436                  * a full modeset because update_connector_routing force that.
437                  */
438                 if (crtc->state->active != crtc_state->active) {
439                         DRM_DEBUG_ATOMIC("[CRTC:%d] active changed\n",
440                                          crtc->base.id);
441                         crtc_state->active_changed = true;
442                 }
443
444                 if (!drm_atomic_crtc_needs_modeset(crtc_state))
445                         continue;
446
447                 DRM_DEBUG_ATOMIC("[CRTC:%d] needs all connectors, enable: %c, active: %c\n",
448                                  crtc->base.id,
449                                  crtc_state->enable ? 'y' : 'n',
450                               crtc_state->active ? 'y' : 'n');
451
452                 ret = drm_atomic_add_affected_connectors(state, crtc);
453                 if (ret != 0)
454                         return ret;
455
456                 ret = drm_atomic_add_affected_planes(state, crtc);
457                 if (ret != 0)
458                         return ret;
459
460                 num_connectors = drm_atomic_connectors_for_crtc(state,
461                                                                 crtc);
462
463                 if (crtc_state->enable != !!num_connectors) {
464                         DRM_DEBUG_ATOMIC("[CRTC:%d] enabled/connectors mismatch\n",
465                                          crtc->base.id);
466
467                         return -EINVAL;
468                 }
469         }
470
471         return mode_fixup(state);
472 }
473 EXPORT_SYMBOL(drm_atomic_helper_check_modeset);
474
475 /**
476  * drm_atomic_helper_check_planes - validate state object for planes changes
477  * @dev: DRM device
478  * @state: the driver state object
479  *
480  * Check the state object to see if the requested state is physically possible.
481  * This does all the plane update related checks using by calling into the
482  * ->atomic_check hooks provided by the driver.
483  *
484  * It also sets crtc_state->planes_changed to indicate that a crtc has
485  * updated planes.
486  *
487  * RETURNS
488  * Zero for success or -errno
489  */
490 int
491 drm_atomic_helper_check_planes(struct drm_device *dev,
492                                struct drm_atomic_state *state)
493 {
494         struct drm_crtc *crtc;
495         struct drm_crtc_state *crtc_state;
496         struct drm_plane *plane;
497         struct drm_plane_state *plane_state;
498         int i, ret = 0;
499
500         for_each_plane_in_state(state, plane, plane_state, i) {
501                 const struct drm_plane_helper_funcs *funcs;
502
503                 funcs = plane->helper_private;
504
505                 drm_atomic_helper_plane_changed(state, plane_state, plane);
506
507                 if (!funcs || !funcs->atomic_check)
508                         continue;
509
510                 ret = funcs->atomic_check(plane, plane_state);
511                 if (ret) {
512                         DRM_DEBUG_ATOMIC("[PLANE:%d] atomic driver check failed\n",
513                                          plane->base.id);
514                         return ret;
515                 }
516         }
517
518         for_each_crtc_in_state(state, crtc, crtc_state, i) {
519                 const struct drm_crtc_helper_funcs *funcs;
520
521                 funcs = crtc->helper_private;
522
523                 if (!funcs || !funcs->atomic_check)
524                         continue;
525
526                 ret = funcs->atomic_check(crtc, state->crtc_states[i]);
527                 if (ret) {
528                         DRM_DEBUG_ATOMIC("[CRTC:%d] atomic driver check failed\n",
529                                          crtc->base.id);
530                         return ret;
531                 }
532         }
533
534         return ret;
535 }
536 EXPORT_SYMBOL(drm_atomic_helper_check_planes);
537
538 /**
539  * drm_atomic_helper_check - validate state object
540  * @dev: DRM device
541  * @state: the driver state object
542  *
543  * Check the state object to see if the requested state is physically possible.
544  * Only crtcs and planes have check callbacks, so for any additional (global)
545  * checking that a driver needs it can simply wrap that around this function.
546  * Drivers without such needs can directly use this as their ->atomic_check()
547  * callback.
548  *
549  * This just wraps the two parts of the state checking for planes and modeset
550  * state in the default order: First it calls drm_atomic_helper_check_modeset()
551  * and then drm_atomic_helper_check_planes(). The assumption is that the
552  * ->atomic_check functions depend upon an updated adjusted_mode.clock to
553  * e.g. properly compute watermarks.
554  *
555  * RETURNS
556  * Zero for success or -errno
557  */
558 int drm_atomic_helper_check(struct drm_device *dev,
559                             struct drm_atomic_state *state)
560 {
561         int ret;
562
563         ret = drm_atomic_helper_check_modeset(dev, state);
564         if (ret)
565                 return ret;
566
567         ret = drm_atomic_helper_check_planes(dev, state);
568         if (ret)
569                 return ret;
570
571         return ret;
572 }
573 EXPORT_SYMBOL(drm_atomic_helper_check);
574
575 static void
576 disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state)
577 {
578         struct drm_connector *connector;
579         struct drm_connector_state *old_conn_state;
580         struct drm_crtc *crtc;
581         struct drm_crtc_state *old_crtc_state;
582         int i;
583
584         for_each_connector_in_state(old_state, connector, old_conn_state, i) {
585                 const struct drm_encoder_helper_funcs *funcs;
586                 struct drm_encoder *encoder;
587                 struct drm_crtc_state *old_crtc_state;
588
589                 /* Shut down everything that's in the changeset and currently
590                  * still on. So need to check the old, saved state. */
591                 if (!old_conn_state->crtc)
592                         continue;
593
594                 old_crtc_state = old_state->crtc_states[drm_crtc_index(old_conn_state->crtc)];
595
596                 if (!old_crtc_state->active ||
597                     !drm_atomic_crtc_needs_modeset(old_conn_state->crtc->state))
598                         continue;
599
600                 encoder = old_conn_state->best_encoder;
601
602                 /* We shouldn't get this far if we didn't previously have
603                  * an encoder.. but WARN_ON() rather than explode.
604                  */
605                 if (WARN_ON(!encoder))
606                         continue;
607
608                 funcs = encoder->helper_private;
609
610                 DRM_DEBUG_ATOMIC("disabling [ENCODER:%d:%s]\n",
611                                  encoder->base.id, encoder->name);
612
613                 /*
614                  * Each encoder has at most one connector (since we always steal
615                  * it away), so we won't call disable hooks twice.
616                  */
617                 drm_bridge_disable(encoder->bridge);
618
619                 /* Right function depends upon target state. */
620                 if (connector->state->crtc && funcs->prepare)
621                         funcs->prepare(encoder);
622                 else if (funcs->disable)
623                         funcs->disable(encoder);
624                 else
625                         funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
626
627                 drm_bridge_post_disable(encoder->bridge);
628         }
629
630         for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
631                 const struct drm_crtc_helper_funcs *funcs;
632
633                 /* Shut down everything that needs a full modeset. */
634                 if (!drm_atomic_crtc_needs_modeset(crtc->state))
635                         continue;
636
637                 if (!old_crtc_state->active)
638                         continue;
639
640                 funcs = crtc->helper_private;
641
642                 DRM_DEBUG_ATOMIC("disabling [CRTC:%d]\n",
643                                  crtc->base.id);
644
645
646                 /* Right function depends upon target state. */
647                 if (crtc->state->enable && funcs->prepare)
648                         funcs->prepare(crtc);
649                 else if (funcs->disable)
650                         funcs->disable(crtc);
651                 else
652                         funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
653         }
654 }
655
656 /**
657  * drm_atomic_helper_update_legacy_modeset_state - update legacy modeset state
658  * @dev: DRM device
659  * @old_state: atomic state object with old state structures
660  *
661  * This function updates all the various legacy modeset state pointers in
662  * connectors, encoders and crtcs. It also updates the timestamping constants
663  * used for precise vblank timestamps by calling
664  * drm_calc_timestamping_constants().
665  *
666  * Drivers can use this for building their own atomic commit if they don't have
667  * a pure helper-based modeset implementation.
668  */
669 void
670 drm_atomic_helper_update_legacy_modeset_state(struct drm_device *dev,
671                                               struct drm_atomic_state *old_state)
672 {
673         struct drm_connector *connector;
674         struct drm_connector_state *old_conn_state;
675         struct drm_crtc *crtc;
676         struct drm_crtc_state *old_crtc_state;
677         int i;
678
679         /* clear out existing links and update dpms */
680         for_each_connector_in_state(old_state, connector, old_conn_state, i) {
681                 if (connector->encoder) {
682                         WARN_ON(!connector->encoder->crtc);
683
684                         connector->encoder->crtc = NULL;
685                         connector->encoder = NULL;
686                 }
687
688                 crtc = connector->state->crtc;
689                 if ((!crtc && old_conn_state->crtc) ||
690                     (crtc && drm_atomic_crtc_needs_modeset(crtc->state))) {
691                         struct drm_property *dpms_prop =
692                                 dev->mode_config.dpms_property;
693                         int mode = DRM_MODE_DPMS_OFF;
694
695                         if (crtc && crtc->state->active)
696                                 mode = DRM_MODE_DPMS_ON;
697
698                         connector->dpms = mode;
699                         drm_object_property_set_value(&connector->base,
700                                                       dpms_prop, mode);
701                 }
702         }
703
704         /* set new links */
705         for_each_connector_in_state(old_state, connector, old_conn_state, i) {
706                 if (!connector->state->crtc)
707                         continue;
708
709                 if (WARN_ON(!connector->state->best_encoder))
710                         continue;
711
712                 connector->encoder = connector->state->best_encoder;
713                 connector->encoder->crtc = connector->state->crtc;
714         }
715
716         /* set legacy state in the crtc structure */
717         for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
718                 struct drm_plane *primary = crtc->primary;
719
720                 crtc->mode = crtc->state->mode;
721                 crtc->enabled = crtc->state->enable;
722
723                 if (drm_atomic_get_existing_plane_state(old_state, primary) &&
724                     primary->state->crtc == crtc) {
725                         crtc->x = primary->state->src_x >> 16;
726                         crtc->y = primary->state->src_y >> 16;
727                 }
728
729                 if (crtc->state->enable)
730                         drm_calc_timestamping_constants(crtc,
731                                                         &crtc->state->adjusted_mode);
732         }
733 }
734 EXPORT_SYMBOL(drm_atomic_helper_update_legacy_modeset_state);
735
736 static void
737 crtc_set_mode(struct drm_device *dev, struct drm_atomic_state *old_state)
738 {
739         struct drm_crtc *crtc;
740         struct drm_crtc_state *old_crtc_state;
741         struct drm_connector *connector;
742         struct drm_connector_state *old_conn_state;
743         int i;
744
745         for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
746                 const struct drm_crtc_helper_funcs *funcs;
747
748                 if (!crtc->state->mode_changed)
749                         continue;
750
751                 funcs = crtc->helper_private;
752
753                 if (crtc->state->enable && funcs->mode_set_nofb) {
754                         DRM_DEBUG_ATOMIC("modeset on [CRTC:%d]\n",
755                                          crtc->base.id);
756
757                         funcs->mode_set_nofb(crtc);
758                 }
759         }
760
761         for_each_connector_in_state(old_state, connector, old_conn_state, i) {
762                 const struct drm_encoder_helper_funcs *funcs;
763                 struct drm_crtc_state *new_crtc_state;
764                 struct drm_encoder *encoder;
765                 struct drm_display_mode *mode, *adjusted_mode;
766
767                 if (!connector->state->best_encoder)
768                         continue;
769
770                 encoder = connector->state->best_encoder;
771                 funcs = encoder->helper_private;
772                 new_crtc_state = connector->state->crtc->state;
773                 mode = &new_crtc_state->mode;
774                 adjusted_mode = &new_crtc_state->adjusted_mode;
775
776                 if (!new_crtc_state->mode_changed)
777                         continue;
778
779                 DRM_DEBUG_ATOMIC("modeset on [ENCODER:%d:%s]\n",
780                                  encoder->base.id, encoder->name);
781
782                 /*
783                  * Each encoder has at most one connector (since we always steal
784                  * it away), so we won't call mode_set hooks twice.
785                  */
786                 if (funcs->mode_set)
787                         funcs->mode_set(encoder, mode, adjusted_mode);
788
789                 drm_bridge_mode_set(encoder->bridge, mode, adjusted_mode);
790         }
791 }
792
793 /**
794  * drm_atomic_helper_commit_modeset_disables - modeset commit to disable outputs
795  * @dev: DRM device
796  * @old_state: atomic state object with old state structures
797  *
798  * This function shuts down all the outputs that need to be shut down and
799  * prepares them (if required) with the new mode.
800  *
801  * For compatibility with legacy crtc helpers this should be called before
802  * drm_atomic_helper_commit_planes(), which is what the default commit function
803  * does. But drivers with different needs can group the modeset commits together
804  * and do the plane commits at the end. This is useful for drivers doing runtime
805  * PM since planes updates then only happen when the CRTC is actually enabled.
806  */
807 void drm_atomic_helper_commit_modeset_disables(struct drm_device *dev,
808                                                struct drm_atomic_state *old_state)
809 {
810         disable_outputs(dev, old_state);
811
812         drm_atomic_helper_update_legacy_modeset_state(dev, old_state);
813
814         crtc_set_mode(dev, old_state);
815 }
816 EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_disables);
817
818 /**
819  * drm_atomic_helper_commit_modeset_enables - modeset commit to enable outputs
820  * @dev: DRM device
821  * @old_state: atomic state object with old state structures
822  *
823  * This function enables all the outputs with the new configuration which had to
824  * be turned off for the update.
825  *
826  * For compatibility with legacy crtc helpers this should be called after
827  * drm_atomic_helper_commit_planes(), which is what the default commit function
828  * does. But drivers with different needs can group the modeset commits together
829  * and do the plane commits at the end. This is useful for drivers doing runtime
830  * PM since planes updates then only happen when the CRTC is actually enabled.
831  */
832 void drm_atomic_helper_commit_modeset_enables(struct drm_device *dev,
833                                               struct drm_atomic_state *old_state)
834 {
835         struct drm_crtc *crtc;
836         struct drm_crtc_state *old_crtc_state;
837         struct drm_connector *connector;
838         struct drm_connector_state *old_conn_state;
839         int i;
840
841         for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
842                 const struct drm_crtc_helper_funcs *funcs;
843
844                 /* Need to filter out CRTCs where only planes change. */
845                 if (!drm_atomic_crtc_needs_modeset(crtc->state))
846                         continue;
847
848                 if (!crtc->state->active)
849                         continue;
850
851                 funcs = crtc->helper_private;
852
853                 if (crtc->state->enable) {
854                         DRM_DEBUG_ATOMIC("enabling [CRTC:%d]\n",
855                                          crtc->base.id);
856
857                         if (funcs->enable)
858                                 funcs->enable(crtc);
859                         else
860                                 funcs->commit(crtc);
861                 }
862         }
863
864         for_each_connector_in_state(old_state, connector, old_conn_state, i) {
865                 const struct drm_encoder_helper_funcs *funcs;
866                 struct drm_encoder *encoder;
867
868                 if (!connector->state->best_encoder)
869                         continue;
870
871                 if (!connector->state->crtc->state->active ||
872                     !drm_atomic_crtc_needs_modeset(connector->state->crtc->state))
873                         continue;
874
875                 encoder = connector->state->best_encoder;
876                 funcs = encoder->helper_private;
877
878                 DRM_DEBUG_ATOMIC("enabling [ENCODER:%d:%s]\n",
879                                  encoder->base.id, encoder->name);
880
881                 /*
882                  * Each encoder has at most one connector (since we always steal
883                  * it away), so we won't call enable hooks twice.
884                  */
885                 drm_bridge_pre_enable(encoder->bridge);
886
887                 if (funcs->enable)
888                         funcs->enable(encoder);
889                 else
890                         funcs->commit(encoder);
891
892                 drm_bridge_enable(encoder->bridge);
893         }
894 }
895 EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_enables);
896
897 static void wait_for_fences(struct drm_device *dev,
898                             struct drm_atomic_state *state)
899 {
900         struct drm_plane *plane;
901         struct drm_plane_state *plane_state;
902         int i;
903
904         for_each_plane_in_state(state, plane, plane_state, i) {
905                 if (!plane->state->fence)
906                         continue;
907
908                 WARN_ON(!plane->state->fb);
909
910                 fence_wait(plane->state->fence, false);
911                 fence_put(plane->state->fence);
912                 plane->state->fence = NULL;
913         }
914 }
915
916 /**
917  * drm_atomic_helper_framebuffer_changed - check if framebuffer has changed
918  * @dev: DRM device
919  * @old_state: atomic state object with old state structures
920  * @crtc: DRM crtc
921  *
922  * Checks whether the framebuffer used for this CRTC changes as a result of
923  * the atomic update.  This is useful for drivers which cannot use
924  * drm_atomic_helper_wait_for_vblanks() and need to reimplement its
925  * functionality.
926  *
927  * Returns:
928  * true if the framebuffer changed.
929  */
930 bool drm_atomic_helper_framebuffer_changed(struct drm_device *dev,
931                                            struct drm_atomic_state *old_state,
932                                            struct drm_crtc *crtc)
933 {
934         struct drm_plane *plane;
935         struct drm_plane_state *old_plane_state;
936         int i;
937
938         for_each_plane_in_state(old_state, plane, old_plane_state, i) {
939                 if (plane->state->crtc != crtc &&
940                     old_plane_state->crtc != crtc)
941                         continue;
942
943                 if (plane->state->fb != old_plane_state->fb)
944                         return true;
945         }
946
947         return false;
948 }
949 EXPORT_SYMBOL(drm_atomic_helper_framebuffer_changed);
950
951 /**
952  * drm_atomic_helper_wait_for_vblanks - wait for vblank on crtcs
953  * @dev: DRM device
954  * @old_state: atomic state object with old state structures
955  *
956  * Helper to, after atomic commit, wait for vblanks on all effected
957  * crtcs (ie. before cleaning up old framebuffers using
958  * drm_atomic_helper_cleanup_planes()). It will only wait on crtcs where the
959  * framebuffers have actually changed to optimize for the legacy cursor and
960  * plane update use-case.
961  */
962 void
963 drm_atomic_helper_wait_for_vblanks(struct drm_device *dev,
964                 struct drm_atomic_state *old_state)
965 {
966         struct drm_crtc *crtc;
967         struct drm_crtc_state *old_crtc_state;
968         int i, ret;
969
970         for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
971                 /* No one cares about the old state, so abuse it for tracking
972                  * and store whether we hold a vblank reference (and should do a
973                  * vblank wait) in the ->enable boolean. */
974                 old_crtc_state->enable = false;
975
976                 if (!crtc->state->enable)
977                         continue;
978
979                 /* Legacy cursor ioctls are completely unsynced, and userspace
980                  * relies on that (by doing tons of cursor updates). */
981                 if (old_state->legacy_cursor_update)
982                         continue;
983
984                 if (!drm_atomic_helper_framebuffer_changed(dev,
985                                 old_state, crtc))
986                         continue;
987
988                 ret = drm_crtc_vblank_get(crtc);
989                 if (ret != 0)
990                         continue;
991
992                 old_crtc_state->enable = true;
993                 old_crtc_state->last_vblank_count = drm_crtc_vblank_count(crtc);
994         }
995
996         for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
997                 if (!old_crtc_state->enable)
998                         continue;
999
1000                 ret = wait_event_timeout(dev->vblank[i].queue,
1001                                 old_crtc_state->last_vblank_count !=
1002                                         drm_crtc_vblank_count(crtc),
1003                                 msecs_to_jiffies(50));
1004
1005                 drm_crtc_vblank_put(crtc);
1006         }
1007 }
1008 EXPORT_SYMBOL(drm_atomic_helper_wait_for_vblanks);
1009
1010 /**
1011  * drm_atomic_helper_commit - commit validated state object
1012  * @dev: DRM device
1013  * @state: the driver state object
1014  * @async: asynchronous commit
1015  *
1016  * This function commits a with drm_atomic_helper_check() pre-validated state
1017  * object. This can still fail when e.g. the framebuffer reservation fails. For
1018  * now this doesn't implement asynchronous commits.
1019  *
1020  * Note that right now this function does not support async commits, and hence
1021  * driver writers must implement their own version for now. Also note that the
1022  * default ordering of how the various stages are called is to match the legacy
1023  * modeset helper library closest. One peculiarity of that is that it doesn't
1024  * mesh well with runtime PM at all.
1025  *
1026  * For drivers supporting runtime PM the recommended sequence is
1027  *
1028  *     drm_atomic_helper_commit_modeset_disables(dev, state);
1029  *
1030  *     drm_atomic_helper_commit_modeset_enables(dev, state);
1031  *
1032  *     drm_atomic_helper_commit_planes(dev, state, true);
1033  *
1034  * See the kerneldoc entries for these three functions for more details.
1035  *
1036  * RETURNS
1037  * Zero for success or -errno.
1038  */
1039 int drm_atomic_helper_commit(struct drm_device *dev,
1040                              struct drm_atomic_state *state,
1041                              bool async)
1042 {
1043         int ret;
1044
1045         if (async)
1046                 return -EBUSY;
1047
1048         ret = drm_atomic_helper_prepare_planes(dev, state);
1049         if (ret)
1050                 return ret;
1051
1052         /*
1053          * This is the point of no return - everything below never fails except
1054          * when the hw goes bonghits. Which means we can commit the new state on
1055          * the software side now.
1056          */
1057
1058         drm_atomic_helper_swap_state(dev, state);
1059
1060         /*
1061          * Everything below can be run asynchronously without the need to grab
1062          * any modeset locks at all under one condition: It must be guaranteed
1063          * that the asynchronous work has either been cancelled (if the driver
1064          * supports it, which at least requires that the framebuffers get
1065          * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
1066          * before the new state gets committed on the software side with
1067          * drm_atomic_helper_swap_state().
1068          *
1069          * This scheme allows new atomic state updates to be prepared and
1070          * checked in parallel to the asynchronous completion of the previous
1071          * update. Which is important since compositors need to figure out the
1072          * composition of the next frame right after having submitted the
1073          * current layout.
1074          */
1075
1076         wait_for_fences(dev, state);
1077
1078         drm_atomic_helper_commit_modeset_disables(dev, state);
1079
1080         drm_atomic_helper_commit_planes(dev, state, false);
1081
1082         drm_atomic_helper_commit_modeset_enables(dev, state);
1083
1084         drm_atomic_helper_wait_for_vblanks(dev, state);
1085
1086         drm_atomic_helper_cleanup_planes(dev, state);
1087
1088         drm_atomic_state_free(state);
1089
1090         return 0;
1091 }
1092 EXPORT_SYMBOL(drm_atomic_helper_commit);
1093
1094 /**
1095  * DOC: implementing async commit
1096  *
1097  * For now the atomic helpers don't support async commit directly. If there is
1098  * real need it could be added though, using the dma-buf fence infrastructure
1099  * for generic synchronization with outstanding rendering.
1100  *
1101  * For now drivers have to implement async commit themselves, with the following
1102  * sequence being the recommended one:
1103  *
1104  * 1. Run drm_atomic_helper_prepare_planes() first. This is the only function
1105  * which commit needs to call which can fail, so we want to run it first and
1106  * synchronously.
1107  *
1108  * 2. Synchronize with any outstanding asynchronous commit worker threads which
1109  * might be affected the new state update. This can be done by either cancelling
1110  * or flushing the work items, depending upon whether the driver can deal with
1111  * cancelled updates. Note that it is important to ensure that the framebuffer
1112  * cleanup is still done when cancelling.
1113  *
1114  * For sufficient parallelism it is recommended to have a work item per crtc
1115  * (for updates which don't touch global state) and a global one. Then we only
1116  * need to synchronize with the crtc work items for changed crtcs and the global
1117  * work item, which allows nice concurrent updates on disjoint sets of crtcs.
1118  *
1119  * 3. The software state is updated synchronously with
1120  * drm_atomic_helper_swap_state(). Doing this under the protection of all modeset
1121  * locks means concurrent callers never see inconsistent state. And doing this
1122  * while it's guaranteed that no relevant async worker runs means that async
1123  * workers do not need grab any locks. Actually they must not grab locks, for
1124  * otherwise the work flushing will deadlock.
1125  *
1126  * 4. Schedule a work item to do all subsequent steps, using the split-out
1127  * commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and
1128  * then cleaning up the framebuffers after the old framebuffer is no longer
1129  * being displayed.
1130  */
1131
1132 /**
1133  * drm_atomic_helper_prepare_planes - prepare plane resources before commit
1134  * @dev: DRM device
1135  * @state: atomic state object with new state structures
1136  *
1137  * This function prepares plane state, specifically framebuffers, for the new
1138  * configuration. If any failure is encountered this function will call
1139  * ->cleanup_fb on any already successfully prepared framebuffer.
1140  *
1141  * Returns:
1142  * 0 on success, negative error code on failure.
1143  */
1144 int drm_atomic_helper_prepare_planes(struct drm_device *dev,
1145                                      struct drm_atomic_state *state)
1146 {
1147         int nplanes = dev->mode_config.num_total_plane;
1148         int ret, i;
1149
1150         for (i = 0; i < nplanes; i++) {
1151                 const struct drm_plane_helper_funcs *funcs;
1152                 struct drm_plane *plane = state->planes[i];
1153                 struct drm_plane_state *plane_state = state->plane_states[i];
1154
1155                 if (!plane)
1156                         continue;
1157
1158                 funcs = plane->helper_private;
1159
1160                 if (funcs->prepare_fb) {
1161                         ret = funcs->prepare_fb(plane, plane_state);
1162                         if (ret)
1163                                 goto fail;
1164                 }
1165         }
1166
1167         return 0;
1168
1169 fail:
1170         for (i--; i >= 0; i--) {
1171                 const struct drm_plane_helper_funcs *funcs;
1172                 struct drm_plane *plane = state->planes[i];
1173                 struct drm_plane_state *plane_state = state->plane_states[i];
1174
1175                 if (!plane)
1176                         continue;
1177
1178                 funcs = plane->helper_private;
1179
1180                 if (funcs->cleanup_fb)
1181                         funcs->cleanup_fb(plane, plane_state);
1182
1183         }
1184
1185         return ret;
1186 }
1187 EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
1188
1189 bool plane_crtc_active(struct drm_plane_state *state)
1190 {
1191         return state->crtc && state->crtc->state->active;
1192 }
1193
1194 /**
1195  * drm_atomic_helper_commit_planes - commit plane state
1196  * @dev: DRM device
1197  * @old_state: atomic state object with old state structures
1198  * @active_only: Only commit on active CRTC if set
1199  *
1200  * This function commits the new plane state using the plane and atomic helper
1201  * functions for planes and crtcs. It assumes that the atomic state has already
1202  * been pushed into the relevant object state pointers, since this step can no
1203  * longer fail.
1204  *
1205  * It still requires the global state object @old_state to know which planes and
1206  * crtcs need to be updated though.
1207  *
1208  * Note that this function does all plane updates across all CRTCs in one step.
1209  * If the hardware can't support this approach look at
1210  * drm_atomic_helper_commit_planes_on_crtc() instead.
1211  *
1212  * Plane parameters can be updated by applications while the associated CRTC is
1213  * disabled. The DRM/KMS core will store the parameters in the plane state,
1214  * which will be available to the driver when the CRTC is turned on. As a result
1215  * most drivers don't need to be immediately notified of plane updates for a
1216  * disabled CRTC.
1217  *
1218  * Unless otherwise needed, drivers are advised to set the @active_only
1219  * parameters to true in order not to receive plane update notifications related
1220  * to a disabled CRTC. This avoids the need to manually ignore plane updates in
1221  * driver code when the driver and/or hardware can't or just don't need to deal
1222  * with updates on disabled CRTCs, for example when supporting runtime PM.
1223  *
1224  * The drm_atomic_helper_commit() default implementation only sets @active_only
1225  * to false to most closely match the behaviour of the legacy helpers. This should
1226  * not be copied blindly by drivers.
1227  */
1228 void drm_atomic_helper_commit_planes(struct drm_device *dev,
1229                                      struct drm_atomic_state *old_state,
1230                                      bool active_only)
1231 {
1232         struct drm_crtc *crtc;
1233         struct drm_crtc_state *old_crtc_state;
1234         struct drm_plane *plane;
1235         struct drm_plane_state *old_plane_state;
1236         int i;
1237
1238         for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
1239                 const struct drm_crtc_helper_funcs *funcs;
1240
1241                 funcs = crtc->helper_private;
1242
1243                 if (!funcs || !funcs->atomic_begin)
1244                         continue;
1245
1246                 if (active_only && !crtc->state->active)
1247                         continue;
1248
1249                 funcs->atomic_begin(crtc, old_crtc_state);
1250         }
1251
1252         for_each_plane_in_state(old_state, plane, old_plane_state, i) {
1253                 const struct drm_plane_helper_funcs *funcs;
1254                 bool disabling;
1255
1256                 funcs = plane->helper_private;
1257
1258                 if (!funcs)
1259                         continue;
1260
1261                 disabling = drm_atomic_plane_disabling(plane, old_plane_state);
1262
1263                 if (active_only) {
1264                         /*
1265                          * Skip planes related to inactive CRTCs. If the plane
1266                          * is enabled use the state of the current CRTC. If the
1267                          * plane is being disabled use the state of the old
1268                          * CRTC to avoid skipping planes being disabled on an
1269                          * active CRTC.
1270                          */
1271                         if (!disabling && !plane_crtc_active(plane->state))
1272                                 continue;
1273                         if (disabling && !plane_crtc_active(old_plane_state))
1274                                 continue;
1275                 }
1276
1277                 /*
1278                  * Special-case disabling the plane if drivers support it.
1279                  */
1280                 if (disabling && funcs->atomic_disable)
1281                         funcs->atomic_disable(plane, old_plane_state);
1282                 else if (plane->state->crtc || disabling)
1283                         funcs->atomic_update(plane, old_plane_state);
1284         }
1285
1286         for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
1287                 const struct drm_crtc_helper_funcs *funcs;
1288
1289                 funcs = crtc->helper_private;
1290
1291                 if (!funcs || !funcs->atomic_flush)
1292                         continue;
1293
1294                 if (active_only && !crtc->state->active)
1295                         continue;
1296
1297                 funcs->atomic_flush(crtc, old_crtc_state);
1298         }
1299 }
1300 EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
1301
1302 /**
1303  * drm_atomic_helper_commit_planes_on_crtc - commit plane state for a crtc
1304  * @old_crtc_state: atomic state object with the old crtc state
1305  *
1306  * This function commits the new plane state using the plane and atomic helper
1307  * functions for planes on the specific crtc. It assumes that the atomic state
1308  * has already been pushed into the relevant object state pointers, since this
1309  * step can no longer fail.
1310  *
1311  * This function is useful when plane updates should be done crtc-by-crtc
1312  * instead of one global step like drm_atomic_helper_commit_planes() does.
1313  *
1314  * This function can only be savely used when planes are not allowed to move
1315  * between different CRTCs because this function doesn't handle inter-CRTC
1316  * depencies. Callers need to ensure that either no such depencies exist,
1317  * resolve them through ordering of commit calls or through some other means.
1318  */
1319 void
1320 drm_atomic_helper_commit_planes_on_crtc(struct drm_crtc_state *old_crtc_state)
1321 {
1322         const struct drm_crtc_helper_funcs *crtc_funcs;
1323         struct drm_crtc *crtc = old_crtc_state->crtc;
1324         struct drm_atomic_state *old_state = old_crtc_state->state;
1325         struct drm_plane *plane;
1326         unsigned plane_mask;
1327
1328         plane_mask = old_crtc_state->plane_mask;
1329         plane_mask |= crtc->state->plane_mask;
1330
1331         crtc_funcs = crtc->helper_private;
1332         if (crtc_funcs && crtc_funcs->atomic_begin)
1333                 crtc_funcs->atomic_begin(crtc, old_crtc_state);
1334
1335         drm_for_each_plane_mask(plane, crtc->dev, plane_mask) {
1336                 struct drm_plane_state *old_plane_state =
1337                         drm_atomic_get_existing_plane_state(old_state, plane);
1338                 const struct drm_plane_helper_funcs *plane_funcs;
1339
1340                 plane_funcs = plane->helper_private;
1341
1342                 if (!old_plane_state || !plane_funcs)
1343                         continue;
1344
1345                 WARN_ON(plane->state->crtc && plane->state->crtc != crtc);
1346
1347                 if (drm_atomic_plane_disabling(plane, old_plane_state) &&
1348                     plane_funcs->atomic_disable)
1349                         plane_funcs->atomic_disable(plane, old_plane_state);
1350                 else if (plane->state->crtc ||
1351                          drm_atomic_plane_disabling(plane, old_plane_state))
1352                         plane_funcs->atomic_update(plane, old_plane_state);
1353         }
1354
1355         if (crtc_funcs && crtc_funcs->atomic_flush)
1356                 crtc_funcs->atomic_flush(crtc, old_crtc_state);
1357 }
1358 EXPORT_SYMBOL(drm_atomic_helper_commit_planes_on_crtc);
1359
1360 /**
1361  * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
1362  * @dev: DRM device
1363  * @old_state: atomic state object with old state structures
1364  *
1365  * This function cleans up plane state, specifically framebuffers, from the old
1366  * configuration. Hence the old configuration must be perserved in @old_state to
1367  * be able to call this function.
1368  *
1369  * This function must also be called on the new state when the atomic update
1370  * fails at any point after calling drm_atomic_helper_prepare_planes().
1371  */
1372 void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
1373                                       struct drm_atomic_state *old_state)
1374 {
1375         struct drm_plane *plane;
1376         struct drm_plane_state *plane_state;
1377         int i;
1378
1379         for_each_plane_in_state(old_state, plane, plane_state, i) {
1380                 const struct drm_plane_helper_funcs *funcs;
1381
1382                 funcs = plane->helper_private;
1383
1384                 if (funcs->cleanup_fb)
1385                         funcs->cleanup_fb(plane, plane_state);
1386         }
1387 }
1388 EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
1389
1390 /**
1391  * drm_atomic_helper_swap_state - store atomic state into current sw state
1392  * @dev: DRM device
1393  * @state: atomic state
1394  *
1395  * This function stores the atomic state into the current state pointers in all
1396  * driver objects. It should be called after all failing steps have been done
1397  * and succeeded, but before the actual hardware state is committed.
1398  *
1399  * For cleanup and error recovery the current state for all changed objects will
1400  * be swaped into @state.
1401  *
1402  * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
1403  *
1404  * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
1405  *
1406  * 2. Do any other steps that might fail.
1407  *
1408  * 3. Put the staged state into the current state pointers with this function.
1409  *
1410  * 4. Actually commit the hardware state.
1411  *
1412  * 5. Call drm_atomic_helper_cleanup_planes() with @state, which since step 3
1413  * contains the old state. Also do any other cleanup required with that state.
1414  */
1415 void drm_atomic_helper_swap_state(struct drm_device *dev,
1416                                   struct drm_atomic_state *state)
1417 {
1418         int i;
1419
1420         for (i = 0; i < dev->mode_config.num_connector; i++) {
1421                 struct drm_connector *connector = state->connectors[i];
1422
1423                 if (!connector)
1424                         continue;
1425
1426                 connector->state->state = state;
1427                 swap(state->connector_states[i], connector->state);
1428                 connector->state->state = NULL;
1429         }
1430
1431         for (i = 0; i < dev->mode_config.num_crtc; i++) {
1432                 struct drm_crtc *crtc = state->crtcs[i];
1433
1434                 if (!crtc)
1435                         continue;
1436
1437                 crtc->state->state = state;
1438                 swap(state->crtc_states[i], crtc->state);
1439                 crtc->state->state = NULL;
1440         }
1441
1442         for (i = 0; i < dev->mode_config.num_total_plane; i++) {
1443                 struct drm_plane *plane = state->planes[i];
1444
1445                 if (!plane)
1446                         continue;
1447
1448                 plane->state->state = state;
1449                 swap(state->plane_states[i], plane->state);
1450                 plane->state->state = NULL;
1451         }
1452 }
1453 EXPORT_SYMBOL(drm_atomic_helper_swap_state);
1454
1455 /**
1456  * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
1457  * @plane: plane object to update
1458  * @crtc: owning CRTC of owning plane
1459  * @fb: framebuffer to flip onto plane
1460  * @crtc_x: x offset of primary plane on crtc
1461  * @crtc_y: y offset of primary plane on crtc
1462  * @crtc_w: width of primary plane rectangle on crtc
1463  * @crtc_h: height of primary plane rectangle on crtc
1464  * @src_x: x offset of @fb for panning
1465  * @src_y: y offset of @fb for panning
1466  * @src_w: width of source rectangle in @fb
1467  * @src_h: height of source rectangle in @fb
1468  *
1469  * Provides a default plane update handler using the atomic driver interface.
1470  *
1471  * RETURNS:
1472  * Zero on success, error code on failure
1473  */
1474 int drm_atomic_helper_update_plane(struct drm_plane *plane,
1475                                    struct drm_crtc *crtc,
1476                                    struct drm_framebuffer *fb,
1477                                    int crtc_x, int crtc_y,
1478                                    unsigned int crtc_w, unsigned int crtc_h,
1479                                    uint32_t src_x, uint32_t src_y,
1480                                    uint32_t src_w, uint32_t src_h)
1481 {
1482         struct drm_atomic_state *state;
1483         struct drm_plane_state *plane_state;
1484         int ret = 0;
1485
1486         state = drm_atomic_state_alloc(plane->dev);
1487         if (!state)
1488                 return -ENOMEM;
1489
1490         state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1491 retry:
1492         plane_state = drm_atomic_get_plane_state(state, plane);
1493         if (IS_ERR(plane_state)) {
1494                 ret = PTR_ERR(plane_state);
1495                 goto fail;
1496         }
1497
1498         ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
1499         if (ret != 0)
1500                 goto fail;
1501         drm_atomic_set_fb_for_plane(plane_state, fb);
1502         plane_state->crtc_x = crtc_x;
1503         plane_state->crtc_y = crtc_y;
1504         plane_state->crtc_h = crtc_h;
1505         plane_state->crtc_w = crtc_w;
1506         plane_state->src_x = src_x;
1507         plane_state->src_y = src_y;
1508         plane_state->src_h = src_h;
1509         plane_state->src_w = src_w;
1510
1511         if (plane == crtc->cursor)
1512                 state->legacy_cursor_update = true;
1513
1514         ret = drm_atomic_commit(state);
1515         if (ret != 0)
1516                 goto fail;
1517
1518         /* Driver takes ownership of state on successful commit. */
1519         return 0;
1520 fail:
1521         if (ret == -EDEADLK)
1522                 goto backoff;
1523
1524         drm_atomic_state_free(state);
1525
1526         return ret;
1527 backoff:
1528         drm_atomic_state_clear(state);
1529         drm_atomic_legacy_backoff(state);
1530
1531         /*
1532          * Someone might have exchanged the framebuffer while we dropped locks
1533          * in the backoff code. We need to fix up the fb refcount tracking the
1534          * core does for us.
1535          */
1536         plane->old_fb = plane->fb;
1537
1538         goto retry;
1539 }
1540 EXPORT_SYMBOL(drm_atomic_helper_update_plane);
1541
1542 /**
1543  * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
1544  * @plane: plane to disable
1545  *
1546  * Provides a default plane disable handler using the atomic driver interface.
1547  *
1548  * RETURNS:
1549  * Zero on success, error code on failure
1550  */
1551 int drm_atomic_helper_disable_plane(struct drm_plane *plane)
1552 {
1553         struct drm_atomic_state *state;
1554         struct drm_plane_state *plane_state;
1555         int ret = 0;
1556
1557         /*
1558          * FIXME: Without plane->crtc set we can't get at the implicit legacy
1559          * acquire context. The real fix will be to wire the acquire ctx through
1560          * everywhere we need it, but meanwhile prevent chaos by just skipping
1561          * this noop. The critical case is the cursor ioctls which a) only grab
1562          * crtc/cursor-plane locks (so we need the crtc to get at the right
1563          * acquire context) and b) can try to disable the plane multiple times.
1564          */
1565         if (!plane->crtc)
1566                 return 0;
1567
1568         state = drm_atomic_state_alloc(plane->dev);
1569         if (!state)
1570                 return -ENOMEM;
1571
1572         state->acquire_ctx = drm_modeset_legacy_acquire_ctx(plane->crtc);
1573 retry:
1574         plane_state = drm_atomic_get_plane_state(state, plane);
1575         if (IS_ERR(plane_state)) {
1576                 ret = PTR_ERR(plane_state);
1577                 goto fail;
1578         }
1579
1580         if (plane_state->crtc && (plane == plane->crtc->cursor))
1581                 plane_state->state->legacy_cursor_update = true;
1582
1583         ret = __drm_atomic_helper_disable_plane(plane, plane_state);
1584         if (ret != 0)
1585                 goto fail;
1586
1587         ret = drm_atomic_commit(state);
1588         if (ret != 0)
1589                 goto fail;
1590
1591         /* Driver takes ownership of state on successful commit. */
1592         return 0;
1593 fail:
1594         if (ret == -EDEADLK)
1595                 goto backoff;
1596
1597         drm_atomic_state_free(state);
1598
1599         return ret;
1600 backoff:
1601         drm_atomic_state_clear(state);
1602         drm_atomic_legacy_backoff(state);
1603
1604         /*
1605          * Someone might have exchanged the framebuffer while we dropped locks
1606          * in the backoff code. We need to fix up the fb refcount tracking the
1607          * core does for us.
1608          */
1609         plane->old_fb = plane->fb;
1610
1611         goto retry;
1612 }
1613 EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
1614
1615 /* just used from fb-helper and atomic-helper: */
1616 int __drm_atomic_helper_disable_plane(struct drm_plane *plane,
1617                 struct drm_plane_state *plane_state)
1618 {
1619         int ret;
1620
1621         ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
1622         if (ret != 0)
1623                 return ret;
1624
1625         drm_atomic_set_fb_for_plane(plane_state, NULL);
1626         plane_state->crtc_x = 0;
1627         plane_state->crtc_y = 0;
1628         plane_state->crtc_h = 0;
1629         plane_state->crtc_w = 0;
1630         plane_state->src_x = 0;
1631         plane_state->src_y = 0;
1632         plane_state->src_h = 0;
1633         plane_state->src_w = 0;
1634
1635         return 0;
1636 }
1637
1638 static int update_output_state(struct drm_atomic_state *state,
1639                                struct drm_mode_set *set)
1640 {
1641         struct drm_device *dev = set->crtc->dev;
1642         struct drm_crtc *crtc;
1643         struct drm_crtc_state *crtc_state;
1644         struct drm_connector *connector;
1645         struct drm_connector_state *conn_state;
1646         int ret, i, j;
1647
1648         ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
1649                                state->acquire_ctx);
1650         if (ret)
1651                 return ret;
1652
1653         /* First grab all affected connector/crtc states. */
1654         for (i = 0; i < set->num_connectors; i++) {
1655                 conn_state = drm_atomic_get_connector_state(state,
1656                                                             set->connectors[i]);
1657                 if (IS_ERR(conn_state))
1658                         return PTR_ERR(conn_state);
1659         }
1660
1661         for_each_crtc_in_state(state, crtc, crtc_state, i) {
1662                 ret = drm_atomic_add_affected_connectors(state, crtc);
1663                 if (ret)
1664                         return ret;
1665         }
1666
1667         /* Then recompute connector->crtc links and crtc enabling state. */
1668         for_each_connector_in_state(state, connector, conn_state, i) {
1669                 if (conn_state->crtc == set->crtc) {
1670                         ret = drm_atomic_set_crtc_for_connector(conn_state,
1671                                                                 NULL);
1672                         if (ret)
1673                                 return ret;
1674                 }
1675
1676                 for (j = 0; j < set->num_connectors; j++) {
1677                         if (set->connectors[j] == connector) {
1678                                 ret = drm_atomic_set_crtc_for_connector(conn_state,
1679                                                                         set->crtc);
1680                                 if (ret)
1681                                         return ret;
1682                                 break;
1683                         }
1684                 }
1685         }
1686
1687         for_each_crtc_in_state(state, crtc, crtc_state, i) {
1688                 /* Don't update ->enable for the CRTC in the set_config request,
1689                  * since a mismatch would indicate a bug in the upper layers.
1690                  * The actual modeset code later on will catch any
1691                  * inconsistencies here. */
1692                 if (crtc == set->crtc)
1693                         continue;
1694
1695                 if (!drm_atomic_connectors_for_crtc(state, crtc)) {
1696                         ret = drm_atomic_set_mode_prop_for_crtc(crtc_state,
1697                                                                 NULL);
1698                         if (ret < 0)
1699                                 return ret;
1700
1701                         crtc_state->active = false;
1702                 }
1703         }
1704
1705         return 0;
1706 }
1707
1708 /**
1709  * drm_atomic_helper_set_config - set a new config from userspace
1710  * @set: mode set configuration
1711  *
1712  * Provides a default crtc set_config handler using the atomic driver interface.
1713  *
1714  * Returns:
1715  * Returns 0 on success, negative errno numbers on failure.
1716  */
1717 int drm_atomic_helper_set_config(struct drm_mode_set *set)
1718 {
1719         struct drm_atomic_state *state;
1720         struct drm_crtc *crtc = set->crtc;
1721         int ret = 0;
1722
1723         state = drm_atomic_state_alloc(crtc->dev);
1724         if (!state)
1725                 return -ENOMEM;
1726
1727         state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1728 retry:
1729         ret = __drm_atomic_helper_set_config(set, state);
1730         if (ret != 0)
1731                 goto fail;
1732
1733         ret = drm_atomic_commit(state);
1734         if (ret != 0)
1735                 goto fail;
1736
1737         /* Driver takes ownership of state on successful commit. */
1738         return 0;
1739 fail:
1740         if (ret == -EDEADLK)
1741                 goto backoff;
1742
1743         drm_atomic_state_free(state);
1744
1745         return ret;
1746 backoff:
1747         drm_atomic_state_clear(state);
1748         drm_atomic_legacy_backoff(state);
1749
1750         /*
1751          * Someone might have exchanged the framebuffer while we dropped locks
1752          * in the backoff code. We need to fix up the fb refcount tracking the
1753          * core does for us.
1754          */
1755         crtc->primary->old_fb = crtc->primary->fb;
1756
1757         goto retry;
1758 }
1759 EXPORT_SYMBOL(drm_atomic_helper_set_config);
1760
1761 /* just used from fb-helper and atomic-helper: */
1762 int __drm_atomic_helper_set_config(struct drm_mode_set *set,
1763                 struct drm_atomic_state *state)
1764 {
1765         struct drm_crtc_state *crtc_state;
1766         struct drm_plane_state *primary_state;
1767         struct drm_crtc *crtc = set->crtc;
1768         int hdisplay, vdisplay;
1769         int ret;
1770
1771         crtc_state = drm_atomic_get_crtc_state(state, crtc);
1772         if (IS_ERR(crtc_state))
1773                 return PTR_ERR(crtc_state);
1774
1775         primary_state = drm_atomic_get_plane_state(state, crtc->primary);
1776         if (IS_ERR(primary_state))
1777                 return PTR_ERR(primary_state);
1778
1779         if (!set->mode) {
1780                 WARN_ON(set->fb);
1781                 WARN_ON(set->num_connectors);
1782
1783                 ret = drm_atomic_set_mode_for_crtc(crtc_state, NULL);
1784                 if (ret != 0)
1785                         return ret;
1786
1787                 crtc_state->active = false;
1788
1789                 ret = drm_atomic_set_crtc_for_plane(primary_state, NULL);
1790                 if (ret != 0)
1791                         return ret;
1792
1793                 drm_atomic_set_fb_for_plane(primary_state, NULL);
1794
1795                 goto commit;
1796         }
1797
1798         WARN_ON(!set->fb);
1799         WARN_ON(!set->num_connectors);
1800
1801         ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
1802         if (ret != 0)
1803                 return ret;
1804
1805         crtc_state->active = true;
1806
1807         ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
1808         if (ret != 0)
1809                 return ret;
1810
1811         drm_crtc_get_hv_timing(set->mode, &hdisplay, &vdisplay);
1812
1813         drm_atomic_set_fb_for_plane(primary_state, set->fb);
1814         primary_state->crtc_x = 0;
1815         primary_state->crtc_y = 0;
1816         primary_state->crtc_h = vdisplay;
1817         primary_state->crtc_w = hdisplay;
1818         primary_state->src_x = set->x << 16;
1819         primary_state->src_y = set->y << 16;
1820         if (primary_state->rotation & (BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270))) {
1821                 primary_state->src_h = hdisplay << 16;
1822                 primary_state->src_w = vdisplay << 16;
1823         } else {
1824                 primary_state->src_h = vdisplay << 16;
1825                 primary_state->src_w = hdisplay << 16;
1826         }
1827
1828 commit:
1829         ret = update_output_state(state, set);
1830         if (ret)
1831                 return ret;
1832
1833         return 0;
1834 }
1835
1836 /**
1837  * drm_atomic_helper_disable_all - disable all currently active outputs
1838  * @dev: DRM device
1839  * @ctx: lock acquisition context
1840  *
1841  * Loops through all connectors, finding those that aren't turned off and then
1842  * turns them off by setting their DPMS mode to OFF and deactivating the CRTC
1843  * that they are connected to.
1844  *
1845  * This is used for example in suspend/resume to disable all currently active
1846  * functions when suspending.
1847  *
1848  * Note that if callers haven't already acquired all modeset locks this might
1849  * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
1850  *
1851  * Returns:
1852  * 0 on success or a negative error code on failure.
1853  *
1854  * See also:
1855  * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
1856  */
1857 int drm_atomic_helper_disable_all(struct drm_device *dev,
1858                                   struct drm_modeset_acquire_ctx *ctx)
1859 {
1860         struct drm_atomic_state *state;
1861         struct drm_connector *conn;
1862         int err;
1863
1864         state = drm_atomic_state_alloc(dev);
1865         if (!state)
1866                 return -ENOMEM;
1867
1868         state->acquire_ctx = ctx;
1869
1870         drm_for_each_connector(conn, dev) {
1871                 struct drm_crtc *crtc = conn->state->crtc;
1872                 struct drm_crtc_state *crtc_state;
1873
1874                 if (!crtc || conn->dpms != DRM_MODE_DPMS_ON)
1875                         continue;
1876
1877                 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1878                 if (IS_ERR(crtc_state)) {
1879                         err = PTR_ERR(crtc_state);
1880                         goto free;
1881                 }
1882
1883                 crtc_state->active = false;
1884         }
1885
1886         err = drm_atomic_commit(state);
1887
1888 free:
1889         if (err < 0)
1890                 drm_atomic_state_free(state);
1891
1892         return err;
1893 }
1894 EXPORT_SYMBOL(drm_atomic_helper_disable_all);
1895
1896 /**
1897  * drm_atomic_helper_suspend - subsystem-level suspend helper
1898  * @dev: DRM device
1899  *
1900  * Duplicates the current atomic state, disables all active outputs and then
1901  * returns a pointer to the original atomic state to the caller. Drivers can
1902  * pass this pointer to the drm_atomic_helper_resume() helper upon resume to
1903  * restore the output configuration that was active at the time the system
1904  * entered suspend.
1905  *
1906  * Note that it is potentially unsafe to use this. The atomic state object
1907  * returned by this function is assumed to be persistent. Drivers must ensure
1908  * that this holds true. Before calling this function, drivers must make sure
1909  * to suspend fbdev emulation so that nothing can be using the device.
1910  *
1911  * Returns:
1912  * A pointer to a copy of the state before suspend on success or an ERR_PTR()-
1913  * encoded error code on failure. Drivers should store the returned atomic
1914  * state object and pass it to the drm_atomic_helper_resume() helper upon
1915  * resume.
1916  *
1917  * See also:
1918  * drm_atomic_helper_duplicate_state(), drm_atomic_helper_disable_all(),
1919  * drm_atomic_helper_resume()
1920  */
1921 struct drm_atomic_state *drm_atomic_helper_suspend(struct drm_device *dev)
1922 {
1923         struct drm_modeset_acquire_ctx ctx;
1924         struct drm_atomic_state *state;
1925         int err;
1926
1927         drm_modeset_acquire_init(&ctx, 0);
1928
1929 retry:
1930         err = drm_modeset_lock_all_ctx(dev, &ctx);
1931         if (err < 0) {
1932                 state = ERR_PTR(err);
1933                 goto unlock;
1934         }
1935
1936         state = drm_atomic_helper_duplicate_state(dev, &ctx);
1937         if (IS_ERR(state))
1938                 goto unlock;
1939
1940         err = drm_atomic_helper_disable_all(dev, &ctx);
1941         if (err < 0) {
1942                 drm_atomic_state_free(state);
1943                 state = ERR_PTR(err);
1944                 goto unlock;
1945         }
1946
1947 unlock:
1948         if (PTR_ERR(state) == -EDEADLK) {
1949                 drm_modeset_backoff(&ctx);
1950                 goto retry;
1951         }
1952
1953         drm_modeset_drop_locks(&ctx);
1954         drm_modeset_acquire_fini(&ctx);
1955         return state;
1956 }
1957 EXPORT_SYMBOL(drm_atomic_helper_suspend);
1958
1959 /**
1960  * drm_atomic_helper_resume - subsystem-level resume helper
1961  * @dev: DRM device
1962  * @state: atomic state to resume to
1963  *
1964  * Calls drm_mode_config_reset() to synchronize hardware and software states,
1965  * grabs all modeset locks and commits the atomic state object. This can be
1966  * used in conjunction with the drm_atomic_helper_suspend() helper to
1967  * implement suspend/resume for drivers that support atomic mode-setting.
1968  *
1969  * Returns:
1970  * 0 on success or a negative error code on failure.
1971  *
1972  * See also:
1973  * drm_atomic_helper_suspend()
1974  */
1975 int drm_atomic_helper_resume(struct drm_device *dev,
1976                              struct drm_atomic_state *state)
1977 {
1978         struct drm_mode_config *config = &dev->mode_config;
1979         int err;
1980
1981         drm_mode_config_reset(dev);
1982         drm_modeset_lock_all(dev);
1983         state->acquire_ctx = config->acquire_ctx;
1984         err = drm_atomic_commit(state);
1985         drm_modeset_unlock_all(dev);
1986
1987         return err;
1988 }
1989 EXPORT_SYMBOL(drm_atomic_helper_resume);
1990
1991 /**
1992  * drm_atomic_helper_crtc_set_property - helper for crtc properties
1993  * @crtc: DRM crtc
1994  * @property: DRM property
1995  * @val: value of property
1996  *
1997  * Provides a default crtc set_property handler using the atomic driver
1998  * interface.
1999  *
2000  * RETURNS:
2001  * Zero on success, error code on failure
2002  */
2003 int
2004 drm_atomic_helper_crtc_set_property(struct drm_crtc *crtc,
2005                                     struct drm_property *property,
2006                                     uint64_t val)
2007 {
2008         struct drm_atomic_state *state;
2009         struct drm_crtc_state *crtc_state;
2010         int ret = 0;
2011
2012         state = drm_atomic_state_alloc(crtc->dev);
2013         if (!state)
2014                 return -ENOMEM;
2015
2016         /* ->set_property is always called with all locks held. */
2017         state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
2018 retry:
2019         crtc_state = drm_atomic_get_crtc_state(state, crtc);
2020         if (IS_ERR(crtc_state)) {
2021                 ret = PTR_ERR(crtc_state);
2022                 goto fail;
2023         }
2024
2025         ret = drm_atomic_crtc_set_property(crtc, crtc_state,
2026                         property, val);
2027         if (ret)
2028                 goto fail;
2029
2030         ret = drm_atomic_commit(state);
2031         if (ret != 0)
2032                 goto fail;
2033
2034         /* Driver takes ownership of state on successful commit. */
2035         return 0;
2036 fail:
2037         if (ret == -EDEADLK)
2038                 goto backoff;
2039
2040         drm_atomic_state_free(state);
2041
2042         return ret;
2043 backoff:
2044         drm_atomic_state_clear(state);
2045         drm_atomic_legacy_backoff(state);
2046
2047         goto retry;
2048 }
2049 EXPORT_SYMBOL(drm_atomic_helper_crtc_set_property);
2050
2051 /**
2052  * drm_atomic_helper_plane_set_property - helper for plane properties
2053  * @plane: DRM plane
2054  * @property: DRM property
2055  * @val: value of property
2056  *
2057  * Provides a default plane set_property handler using the atomic driver
2058  * interface.
2059  *
2060  * RETURNS:
2061  * Zero on success, error code on failure
2062  */
2063 int
2064 drm_atomic_helper_plane_set_property(struct drm_plane *plane,
2065                                     struct drm_property *property,
2066                                     uint64_t val)
2067 {
2068         struct drm_atomic_state *state;
2069         struct drm_plane_state *plane_state;
2070         int ret = 0;
2071
2072         state = drm_atomic_state_alloc(plane->dev);
2073         if (!state)
2074                 return -ENOMEM;
2075
2076         /* ->set_property is always called with all locks held. */
2077         state->acquire_ctx = plane->dev->mode_config.acquire_ctx;
2078 retry:
2079         plane_state = drm_atomic_get_plane_state(state, plane);
2080         if (IS_ERR(plane_state)) {
2081                 ret = PTR_ERR(plane_state);
2082                 goto fail;
2083         }
2084
2085         ret = drm_atomic_plane_set_property(plane, plane_state,
2086                         property, val);
2087         if (ret)
2088                 goto fail;
2089
2090         ret = drm_atomic_commit(state);
2091         if (ret != 0)
2092                 goto fail;
2093
2094         /* Driver takes ownership of state on successful commit. */
2095         return 0;
2096 fail:
2097         if (ret == -EDEADLK)
2098                 goto backoff;
2099
2100         drm_atomic_state_free(state);
2101
2102         return ret;
2103 backoff:
2104         drm_atomic_state_clear(state);
2105         drm_atomic_legacy_backoff(state);
2106
2107         goto retry;
2108 }
2109 EXPORT_SYMBOL(drm_atomic_helper_plane_set_property);
2110
2111 /**
2112  * drm_atomic_helper_connector_set_property - helper for connector properties
2113  * @connector: DRM connector
2114  * @property: DRM property
2115  * @val: value of property
2116  *
2117  * Provides a default connector set_property handler using the atomic driver
2118  * interface.
2119  *
2120  * RETURNS:
2121  * Zero on success, error code on failure
2122  */
2123 int
2124 drm_atomic_helper_connector_set_property(struct drm_connector *connector,
2125                                     struct drm_property *property,
2126                                     uint64_t val)
2127 {
2128         struct drm_atomic_state *state;
2129         struct drm_connector_state *connector_state;
2130         int ret = 0;
2131
2132         state = drm_atomic_state_alloc(connector->dev);
2133         if (!state)
2134                 return -ENOMEM;
2135
2136         /* ->set_property is always called with all locks held. */
2137         state->acquire_ctx = connector->dev->mode_config.acquire_ctx;
2138 retry:
2139         connector_state = drm_atomic_get_connector_state(state, connector);
2140         if (IS_ERR(connector_state)) {
2141                 ret = PTR_ERR(connector_state);
2142                 goto fail;
2143         }
2144
2145         ret = drm_atomic_connector_set_property(connector, connector_state,
2146                         property, val);
2147         if (ret)
2148                 goto fail;
2149
2150         ret = drm_atomic_commit(state);
2151         if (ret != 0)
2152                 goto fail;
2153
2154         /* Driver takes ownership of state on successful commit. */
2155         return 0;
2156 fail:
2157         if (ret == -EDEADLK)
2158                 goto backoff;
2159
2160         drm_atomic_state_free(state);
2161
2162         return ret;
2163 backoff:
2164         drm_atomic_state_clear(state);
2165         drm_atomic_legacy_backoff(state);
2166
2167         goto retry;
2168 }
2169 EXPORT_SYMBOL(drm_atomic_helper_connector_set_property);
2170
2171 /**
2172  * drm_atomic_helper_page_flip - execute a legacy page flip
2173  * @crtc: DRM crtc
2174  * @fb: DRM framebuffer
2175  * @event: optional DRM event to signal upon completion
2176  * @flags: flip flags for non-vblank sync'ed updates
2177  *
2178  * Provides a default page flip implementation using the atomic driver interface.
2179  *
2180  * Note that for now so called async page flips (i.e. updates which are not
2181  * synchronized to vblank) are not supported, since the atomic interfaces have
2182  * no provisions for this yet.
2183  *
2184  * Returns:
2185  * Returns 0 on success, negative errno numbers on failure.
2186  */
2187 int drm_atomic_helper_page_flip(struct drm_crtc *crtc,
2188                                 struct drm_framebuffer *fb,
2189                                 struct drm_pending_vblank_event *event,
2190                                 uint32_t flags)
2191 {
2192         struct drm_plane *plane = crtc->primary;
2193         struct drm_atomic_state *state;
2194         struct drm_plane_state *plane_state;
2195         struct drm_crtc_state *crtc_state;
2196         int ret = 0;
2197
2198         if (flags & DRM_MODE_PAGE_FLIP_ASYNC)
2199                 return -EINVAL;
2200
2201         state = drm_atomic_state_alloc(plane->dev);
2202         if (!state)
2203                 return -ENOMEM;
2204
2205         state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2206 retry:
2207         crtc_state = drm_atomic_get_crtc_state(state, crtc);
2208         if (IS_ERR(crtc_state)) {
2209                 ret = PTR_ERR(crtc_state);
2210                 goto fail;
2211         }
2212         crtc_state->event = event;
2213
2214         plane_state = drm_atomic_get_plane_state(state, plane);
2215         if (IS_ERR(plane_state)) {
2216                 ret = PTR_ERR(plane_state);
2217                 goto fail;
2218         }
2219
2220         ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
2221         if (ret != 0)
2222                 goto fail;
2223         drm_atomic_set_fb_for_plane(plane_state, fb);
2224
2225         ret = drm_atomic_async_commit(state);
2226         if (ret != 0)
2227                 goto fail;
2228
2229         /* Driver takes ownership of state on successful async commit. */
2230         return 0;
2231 fail:
2232         if (ret == -EDEADLK)
2233                 goto backoff;
2234
2235         drm_atomic_state_free(state);
2236
2237         return ret;
2238 backoff:
2239         drm_atomic_state_clear(state);
2240         drm_atomic_legacy_backoff(state);
2241
2242         /*
2243          * Someone might have exchanged the framebuffer while we dropped locks
2244          * in the backoff code. We need to fix up the fb refcount tracking the
2245          * core does for us.
2246          */
2247         plane->old_fb = plane->fb;
2248
2249         goto retry;
2250 }
2251 EXPORT_SYMBOL(drm_atomic_helper_page_flip);
2252
2253 /**
2254  * drm_atomic_helper_connector_dpms() - connector dpms helper implementation
2255  * @connector: affected connector
2256  * @mode: DPMS mode
2257  *
2258  * This is the main helper function provided by the atomic helper framework for
2259  * implementing the legacy DPMS connector interface. It computes the new desired
2260  * ->active state for the corresponding CRTC (if the connector is enabled) and
2261  *  updates it.
2262  *
2263  * Returns:
2264  * Returns 0 on success, negative errno numbers on failure.
2265  */
2266 int drm_atomic_helper_connector_dpms(struct drm_connector *connector,
2267                                      int mode)
2268 {
2269         struct drm_mode_config *config = &connector->dev->mode_config;
2270         struct drm_atomic_state *state;
2271         struct drm_crtc_state *crtc_state;
2272         struct drm_crtc *crtc;
2273         struct drm_connector *tmp_connector;
2274         int ret;
2275         bool active = false;
2276         int old_mode = connector->dpms;
2277
2278         if (mode != DRM_MODE_DPMS_ON)
2279                 mode = DRM_MODE_DPMS_OFF;
2280
2281         connector->dpms = mode;
2282         crtc = connector->state->crtc;
2283
2284         if (!crtc)
2285                 return 0;
2286
2287         state = drm_atomic_state_alloc(connector->dev);
2288         if (!state)
2289                 return -ENOMEM;
2290
2291         state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2292 retry:
2293         crtc_state = drm_atomic_get_crtc_state(state, crtc);
2294         if (IS_ERR(crtc_state)) {
2295                 ret = PTR_ERR(crtc_state);
2296                 goto fail;
2297         }
2298
2299         WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
2300
2301         drm_for_each_connector(tmp_connector, connector->dev) {
2302                 if (tmp_connector->state->crtc != crtc)
2303                         continue;
2304
2305                 if (tmp_connector->dpms == DRM_MODE_DPMS_ON) {
2306                         active = true;
2307                         break;
2308                 }
2309         }
2310         crtc_state->active = active;
2311
2312         ret = drm_atomic_commit(state);
2313         if (ret != 0)
2314                 goto fail;
2315
2316         /* Driver takes ownership of state on successful commit. */
2317         return 0;
2318 fail:
2319         if (ret == -EDEADLK)
2320                 goto backoff;
2321
2322         connector->dpms = old_mode;
2323         drm_atomic_state_free(state);
2324
2325         return ret;
2326 backoff:
2327         drm_atomic_state_clear(state);
2328         drm_atomic_legacy_backoff(state);
2329
2330         goto retry;
2331 }
2332 EXPORT_SYMBOL(drm_atomic_helper_connector_dpms);
2333
2334 /**
2335  * DOC: atomic state reset and initialization
2336  *
2337  * Both the drm core and the atomic helpers assume that there is always the full
2338  * and correct atomic software state for all connectors, CRTCs and planes
2339  * available. Which is a bit a problem on driver load and also after system
2340  * suspend. One way to solve this is to have a hardware state read-out
2341  * infrastructure which reconstructs the full software state (e.g. the i915
2342  * driver).
2343  *
2344  * The simpler solution is to just reset the software state to everything off,
2345  * which is easiest to do by calling drm_mode_config_reset(). To facilitate this
2346  * the atomic helpers provide default reset implementations for all hooks.
2347  */
2348
2349 /**
2350  * drm_atomic_helper_crtc_reset - default ->reset hook for CRTCs
2351  * @crtc: drm CRTC
2352  *
2353  * Resets the atomic state for @crtc by freeing the state pointer (which might
2354  * be NULL, e.g. at driver load time) and allocating a new empty state object.
2355  */
2356 void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc)
2357 {
2358         if (crtc->state && crtc->state->mode_blob)
2359                 drm_property_unreference_blob(crtc->state->mode_blob);
2360         kfree(crtc->state);
2361         crtc->state = kzalloc(sizeof(*crtc->state), GFP_KERNEL);
2362
2363         if (crtc->state)
2364                 crtc->state->crtc = crtc;
2365 }
2366 EXPORT_SYMBOL(drm_atomic_helper_crtc_reset);
2367
2368 /**
2369  * __drm_atomic_helper_crtc_duplicate_state - copy atomic CRTC state
2370  * @crtc: CRTC object
2371  * @state: atomic CRTC state
2372  *
2373  * Copies atomic state from a CRTC's current state and resets inferred values.
2374  * This is useful for drivers that subclass the CRTC state.
2375  */
2376 void __drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc,
2377                                               struct drm_crtc_state *state)
2378 {
2379         memcpy(state, crtc->state, sizeof(*state));
2380
2381         if (state->mode_blob)
2382                 drm_property_reference_blob(state->mode_blob);
2383         state->mode_changed = false;
2384         state->active_changed = false;
2385         state->planes_changed = false;
2386         state->connectors_changed = false;
2387         state->event = NULL;
2388 }
2389 EXPORT_SYMBOL(__drm_atomic_helper_crtc_duplicate_state);
2390
2391 /**
2392  * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
2393  * @crtc: drm CRTC
2394  *
2395  * Default CRTC state duplicate hook for drivers which don't have their own
2396  * subclassed CRTC state structure.
2397  */
2398 struct drm_crtc_state *
2399 drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc)
2400 {
2401         struct drm_crtc_state *state;
2402
2403         if (WARN_ON(!crtc->state))
2404                 return NULL;
2405
2406         state = kmalloc(sizeof(*state), GFP_KERNEL);
2407         if (state)
2408                 __drm_atomic_helper_crtc_duplicate_state(crtc, state);
2409
2410         return state;
2411 }
2412 EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state);
2413
2414 /**
2415  * __drm_atomic_helper_crtc_destroy_state - release CRTC state
2416  * @crtc: CRTC object
2417  * @state: CRTC state object to release
2418  *
2419  * Releases all resources stored in the CRTC state without actually freeing
2420  * the memory of the CRTC state. This is useful for drivers that subclass the
2421  * CRTC state.
2422  */
2423 void __drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
2424                                             struct drm_crtc_state *state)
2425 {
2426         if (state->mode_blob)
2427                 drm_property_unreference_blob(state->mode_blob);
2428 }
2429 EXPORT_SYMBOL(__drm_atomic_helper_crtc_destroy_state);
2430
2431 /**
2432  * drm_atomic_helper_crtc_destroy_state - default state destroy hook
2433  * @crtc: drm CRTC
2434  * @state: CRTC state object to release
2435  *
2436  * Default CRTC state destroy hook for drivers which don't have their own
2437  * subclassed CRTC state structure.
2438  */
2439 void drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
2440                                           struct drm_crtc_state *state)
2441 {
2442         __drm_atomic_helper_crtc_destroy_state(crtc, state);
2443         kfree(state);
2444 }
2445 EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state);
2446
2447 /**
2448  * drm_atomic_helper_plane_reset - default ->reset hook for planes
2449  * @plane: drm plane
2450  *
2451  * Resets the atomic state for @plane by freeing the state pointer (which might
2452  * be NULL, e.g. at driver load time) and allocating a new empty state object.
2453  */
2454 void drm_atomic_helper_plane_reset(struct drm_plane *plane)
2455 {
2456         if (plane->state && plane->state->fb)
2457                 drm_framebuffer_unreference(plane->state->fb);
2458
2459         kfree(plane->state);
2460         plane->state = kzalloc(sizeof(*plane->state), GFP_KERNEL);
2461
2462         if (plane->state)
2463                 plane->state->plane = plane;
2464 }
2465 EXPORT_SYMBOL(drm_atomic_helper_plane_reset);
2466
2467 /**
2468  * __drm_atomic_helper_plane_duplicate_state - copy atomic plane state
2469  * @plane: plane object
2470  * @state: atomic plane state
2471  *
2472  * Copies atomic state from a plane's current state. This is useful for
2473  * drivers that subclass the plane state.
2474  */
2475 void __drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane,
2476                                                struct drm_plane_state *state)
2477 {
2478         memcpy(state, plane->state, sizeof(*state));
2479
2480         if (state->fb)
2481                 drm_framebuffer_reference(state->fb);
2482 }
2483 EXPORT_SYMBOL(__drm_atomic_helper_plane_duplicate_state);
2484
2485 /**
2486  * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
2487  * @plane: drm plane
2488  *
2489  * Default plane state duplicate hook for drivers which don't have their own
2490  * subclassed plane state structure.
2491  */
2492 struct drm_plane_state *
2493 drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane)
2494 {
2495         struct drm_plane_state *state;
2496
2497         if (WARN_ON(!plane->state))
2498                 return NULL;
2499
2500         state = kmalloc(sizeof(*state), GFP_KERNEL);
2501         if (state)
2502                 __drm_atomic_helper_plane_duplicate_state(plane, state);
2503
2504         return state;
2505 }
2506 EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state);
2507
2508 /**
2509  * __drm_atomic_helper_plane_destroy_state - release plane state
2510  * @plane: plane object
2511  * @state: plane state object to release
2512  *
2513  * Releases all resources stored in the plane state without actually freeing
2514  * the memory of the plane state. This is useful for drivers that subclass the
2515  * plane state.
2516  */
2517 void __drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
2518                                              struct drm_plane_state *state)
2519 {
2520         if (state->fb)
2521                 drm_framebuffer_unreference(state->fb);
2522 }
2523 EXPORT_SYMBOL(__drm_atomic_helper_plane_destroy_state);
2524
2525 /**
2526  * drm_atomic_helper_plane_destroy_state - default state destroy hook
2527  * @plane: drm plane
2528  * @state: plane state object to release
2529  *
2530  * Default plane state destroy hook for drivers which don't have their own
2531  * subclassed plane state structure.
2532  */
2533 void drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
2534                                            struct drm_plane_state *state)
2535 {
2536         __drm_atomic_helper_plane_destroy_state(plane, state);
2537         kfree(state);
2538 }
2539 EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state);
2540
2541 /**
2542  * drm_atomic_helper_connector_reset - default ->reset hook for connectors
2543  * @connector: drm connector
2544  *
2545  * Resets the atomic state for @connector by freeing the state pointer (which
2546  * might be NULL, e.g. at driver load time) and allocating a new empty state
2547  * object.
2548  */
2549 void drm_atomic_helper_connector_reset(struct drm_connector *connector)
2550 {
2551         kfree(connector->state);
2552         connector->state = kzalloc(sizeof(*connector->state), GFP_KERNEL);
2553
2554         if (connector->state)
2555                 connector->state->connector = connector;
2556 }
2557 EXPORT_SYMBOL(drm_atomic_helper_connector_reset);
2558
2559 /**
2560  * __drm_atomic_helper_connector_duplicate_state - copy atomic connector state
2561  * @connector: connector object
2562  * @state: atomic connector state
2563  *
2564  * Copies atomic state from a connector's current state. This is useful for
2565  * drivers that subclass the connector state.
2566  */
2567 void
2568 __drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector,
2569                                             struct drm_connector_state *state)
2570 {
2571         memcpy(state, connector->state, sizeof(*state));
2572 }
2573 EXPORT_SYMBOL(__drm_atomic_helper_connector_duplicate_state);
2574
2575 /**
2576  * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
2577  * @connector: drm connector
2578  *
2579  * Default connector state duplicate hook for drivers which don't have their own
2580  * subclassed connector state structure.
2581  */
2582 struct drm_connector_state *
2583 drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
2584 {
2585         struct drm_connector_state *state;
2586
2587         if (WARN_ON(!connector->state))
2588                 return NULL;
2589
2590         state = kmalloc(sizeof(*state), GFP_KERNEL);
2591         if (state)
2592                 __drm_atomic_helper_connector_duplicate_state(connector, state);
2593
2594         return state;
2595 }
2596 EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
2597
2598 /**
2599  * drm_atomic_helper_duplicate_state - duplicate an atomic state object
2600  * @dev: DRM device
2601  * @ctx: lock acquisition context
2602  *
2603  * Makes a copy of the current atomic state by looping over all objects and
2604  * duplicating their respective states. This is used for example by suspend/
2605  * resume support code to save the state prior to suspend such that it can
2606  * be restored upon resume.
2607  *
2608  * Note that this treats atomic state as persistent between save and restore.
2609  * Drivers must make sure that this is possible and won't result in confusion
2610  * or erroneous behaviour.
2611  *
2612  * Note that if callers haven't already acquired all modeset locks this might
2613  * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
2614  *
2615  * Returns:
2616  * A pointer to the copy of the atomic state object on success or an
2617  * ERR_PTR()-encoded error code on failure.
2618  *
2619  * See also:
2620  * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
2621  */
2622 struct drm_atomic_state *
2623 drm_atomic_helper_duplicate_state(struct drm_device *dev,
2624                                   struct drm_modeset_acquire_ctx *ctx)
2625 {
2626         struct drm_atomic_state *state;
2627         struct drm_connector *conn;
2628         struct drm_plane *plane;
2629         struct drm_crtc *crtc;
2630         int err = 0;
2631
2632         state = drm_atomic_state_alloc(dev);
2633         if (!state)
2634                 return ERR_PTR(-ENOMEM);
2635
2636         state->acquire_ctx = ctx;
2637
2638         drm_for_each_crtc(crtc, dev) {
2639                 struct drm_crtc_state *crtc_state;
2640
2641                 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2642                 if (IS_ERR(crtc_state)) {
2643                         err = PTR_ERR(crtc_state);
2644                         goto free;
2645                 }
2646         }
2647
2648         drm_for_each_plane(plane, dev) {
2649                 struct drm_plane_state *plane_state;
2650
2651                 plane_state = drm_atomic_get_plane_state(state, plane);
2652                 if (IS_ERR(plane_state)) {
2653                         err = PTR_ERR(plane_state);
2654                         goto free;
2655                 }
2656         }
2657
2658         drm_for_each_connector(conn, dev) {
2659                 struct drm_connector_state *conn_state;
2660
2661                 conn_state = drm_atomic_get_connector_state(state, conn);
2662                 if (IS_ERR(conn_state)) {
2663                         err = PTR_ERR(conn_state);
2664                         goto free;
2665                 }
2666         }
2667
2668         /* clear the acquire context so that it isn't accidentally reused */
2669         state->acquire_ctx = NULL;
2670
2671 free:
2672         if (err < 0) {
2673                 drm_atomic_state_free(state);
2674                 state = ERR_PTR(err);
2675         }
2676
2677         return state;
2678 }
2679 EXPORT_SYMBOL(drm_atomic_helper_duplicate_state);
2680
2681 /**
2682  * __drm_atomic_helper_connector_destroy_state - release connector state
2683  * @connector: connector object
2684  * @state: connector state object to release
2685  *
2686  * Releases all resources stored in the connector state without actually
2687  * freeing the memory of the connector state. This is useful for drivers that
2688  * subclass the connector state.
2689  */
2690 void
2691 __drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
2692                                             struct drm_connector_state *state)
2693 {
2694         /*
2695          * This is currently a placeholder so that drivers that subclass the
2696          * state will automatically do the right thing if code is ever added
2697          * to this function.
2698          */
2699 }
2700 EXPORT_SYMBOL(__drm_atomic_helper_connector_destroy_state);
2701
2702 /**
2703  * drm_atomic_helper_connector_destroy_state - default state destroy hook
2704  * @connector: drm connector
2705  * @state: connector state object to release
2706  *
2707  * Default connector state destroy hook for drivers which don't have their own
2708  * subclassed connector state structure.
2709  */
2710 void drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
2711                                           struct drm_connector_state *state)
2712 {
2713         __drm_atomic_helper_connector_destroy_state(connector, state);
2714         kfree(state);
2715 }
2716 EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state);