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