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