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