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