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