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