Merge tag 'v4.20' into for-linus
[linux-2.6-microblaze.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_atomic_uapi.h>
31 #include <drm/drm_plane_helper.h>
32 #include <drm/drm_crtc_helper.h>
33 #include <drm/drm_atomic_helper.h>
34 #include <drm/drm_writeback.h>
35 #include <linux/dma-fence.h>
36
37 #include "drm_crtc_helper_internal.h"
38 #include "drm_crtc_internal.h"
39
40 /**
41  * DOC: overview
42  *
43  * This helper library provides implementations of check and commit functions on
44  * top of the CRTC modeset helper callbacks and the plane helper callbacks. It
45  * also provides convenience implementations for the atomic state handling
46  * callbacks for drivers which don't need to subclass the drm core structures to
47  * add their own additional internal state.
48  *
49  * This library also provides default implementations for the check callback in
50  * drm_atomic_helper_check() and for the commit callback with
51  * drm_atomic_helper_commit(). But the individual stages and callbacks are
52  * exposed to allow drivers to mix and match and e.g. use the plane helpers only
53  * together with a driver private modeset implementation.
54  *
55  * This library also provides implementations for all the legacy driver
56  * interfaces on top of the atomic interface. See drm_atomic_helper_set_config(),
57  * drm_atomic_helper_disable_plane(), drm_atomic_helper_disable_plane() and the
58  * various functions to implement set_property callbacks. New drivers must not
59  * implement these functions themselves but must use the provided helpers.
60  *
61  * The atomic helper uses the same function table structures as all other
62  * modesetting helpers. See the documentation for &struct drm_crtc_helper_funcs,
63  * struct &drm_encoder_helper_funcs and &struct drm_connector_helper_funcs. It
64  * also shares the &struct drm_plane_helper_funcs function table with the plane
65  * helpers.
66  */
67 static void
68 drm_atomic_helper_plane_changed(struct drm_atomic_state *state,
69                                 struct drm_plane_state *old_plane_state,
70                                 struct drm_plane_state *plane_state,
71                                 struct drm_plane *plane)
72 {
73         struct drm_crtc_state *crtc_state;
74
75         if (old_plane_state->crtc) {
76                 crtc_state = drm_atomic_get_new_crtc_state(state,
77                                                            old_plane_state->crtc);
78
79                 if (WARN_ON(!crtc_state))
80                         return;
81
82                 crtc_state->planes_changed = true;
83         }
84
85         if (plane_state->crtc) {
86                 crtc_state = drm_atomic_get_new_crtc_state(state, plane_state->crtc);
87
88                 if (WARN_ON(!crtc_state))
89                         return;
90
91                 crtc_state->planes_changed = true;
92         }
93 }
94
95 static int handle_conflicting_encoders(struct drm_atomic_state *state,
96                                        bool disable_conflicting_encoders)
97 {
98         struct drm_connector_state *new_conn_state;
99         struct drm_connector *connector;
100         struct drm_connector_list_iter conn_iter;
101         struct drm_encoder *encoder;
102         unsigned encoder_mask = 0;
103         int i, ret = 0;
104
105         /*
106          * First loop, find all newly assigned encoders from the connectors
107          * part of the state. If the same encoder is assigned to multiple
108          * connectors bail out.
109          */
110         for_each_new_connector_in_state(state, connector, new_conn_state, i) {
111                 const struct drm_connector_helper_funcs *funcs = connector->helper_private;
112                 struct drm_encoder *new_encoder;
113
114                 if (!new_conn_state->crtc)
115                         continue;
116
117                 if (funcs->atomic_best_encoder)
118                         new_encoder = funcs->atomic_best_encoder(connector, new_conn_state);
119                 else if (funcs->best_encoder)
120                         new_encoder = funcs->best_encoder(connector);
121                 else
122                         new_encoder = drm_atomic_helper_best_encoder(connector);
123
124                 if (new_encoder) {
125                         if (encoder_mask & drm_encoder_mask(new_encoder)) {
126                                 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] on [CONNECTOR:%d:%s] already assigned\n",
127                                         new_encoder->base.id, new_encoder->name,
128                                         connector->base.id, connector->name);
129
130                                 return -EINVAL;
131                         }
132
133                         encoder_mask |= drm_encoder_mask(new_encoder);
134                 }
135         }
136
137         if (!encoder_mask)
138                 return 0;
139
140         /*
141          * Second loop, iterate over all connectors not part of the state.
142          *
143          * If a conflicting encoder is found and disable_conflicting_encoders
144          * is not set, an error is returned. Userspace can provide a solution
145          * through the atomic ioctl.
146          *
147          * If the flag is set conflicting connectors are removed from the crtc
148          * and the crtc is disabled if no encoder is left. This preserves
149          * compatibility with the legacy set_config behavior.
150          */
151         drm_connector_list_iter_begin(state->dev, &conn_iter);
152         drm_for_each_connector_iter(connector, &conn_iter) {
153                 struct drm_crtc_state *crtc_state;
154
155                 if (drm_atomic_get_new_connector_state(state, connector))
156                         continue;
157
158                 encoder = connector->state->best_encoder;
159                 if (!encoder || !(encoder_mask & drm_encoder_mask(encoder)))
160                         continue;
161
162                 if (!disable_conflicting_encoders) {
163                         DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s] by [CONNECTOR:%d:%s]\n",
164                                          encoder->base.id, encoder->name,
165                                          connector->state->crtc->base.id,
166                                          connector->state->crtc->name,
167                                          connector->base.id, connector->name);
168                         ret = -EINVAL;
169                         goto out;
170                 }
171
172                 new_conn_state = drm_atomic_get_connector_state(state, connector);
173                 if (IS_ERR(new_conn_state)) {
174                         ret = PTR_ERR(new_conn_state);
175                         goto out;
176                 }
177
178                 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s], disabling [CONNECTOR:%d:%s]\n",
179                                  encoder->base.id, encoder->name,
180                                  new_conn_state->crtc->base.id, new_conn_state->crtc->name,
181                                  connector->base.id, connector->name);
182
183                 crtc_state = drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
184
185                 ret = drm_atomic_set_crtc_for_connector(new_conn_state, NULL);
186                 if (ret)
187                         goto out;
188
189                 if (!crtc_state->connector_mask) {
190                         ret = drm_atomic_set_mode_prop_for_crtc(crtc_state,
191                                                                 NULL);
192                         if (ret < 0)
193                                 goto out;
194
195                         crtc_state->active = false;
196                 }
197         }
198 out:
199         drm_connector_list_iter_end(&conn_iter);
200
201         return ret;
202 }
203
204 static void
205 set_best_encoder(struct drm_atomic_state *state,
206                  struct drm_connector_state *conn_state,
207                  struct drm_encoder *encoder)
208 {
209         struct drm_crtc_state *crtc_state;
210         struct drm_crtc *crtc;
211
212         if (conn_state->best_encoder) {
213                 /* Unset the encoder_mask in the old crtc state. */
214                 crtc = conn_state->connector->state->crtc;
215
216                 /* A NULL crtc is an error here because we should have
217                  *  duplicated a NULL best_encoder when crtc was NULL.
218                  * As an exception restoring duplicated atomic state
219                  * during resume is allowed, so don't warn when
220                  * best_encoder is equal to encoder we intend to set.
221                  */
222                 WARN_ON(!crtc && encoder != conn_state->best_encoder);
223                 if (crtc) {
224                         crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
225
226                         crtc_state->encoder_mask &=
227                                 ~drm_encoder_mask(conn_state->best_encoder);
228                 }
229         }
230
231         if (encoder) {
232                 crtc = conn_state->crtc;
233                 WARN_ON(!crtc);
234                 if (crtc) {
235                         crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
236
237                         crtc_state->encoder_mask |=
238                                 drm_encoder_mask(encoder);
239                 }
240         }
241
242         conn_state->best_encoder = encoder;
243 }
244
245 static void
246 steal_encoder(struct drm_atomic_state *state,
247               struct drm_encoder *encoder)
248 {
249         struct drm_crtc_state *crtc_state;
250         struct drm_connector *connector;
251         struct drm_connector_state *old_connector_state, *new_connector_state;
252         int i;
253
254         for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
255                 struct drm_crtc *encoder_crtc;
256
257                 if (new_connector_state->best_encoder != encoder)
258                         continue;
259
260                 encoder_crtc = old_connector_state->crtc;
261
262                 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s], stealing it\n",
263                                  encoder->base.id, encoder->name,
264                                  encoder_crtc->base.id, encoder_crtc->name);
265
266                 set_best_encoder(state, new_connector_state, NULL);
267
268                 crtc_state = drm_atomic_get_new_crtc_state(state, encoder_crtc);
269                 crtc_state->connectors_changed = true;
270
271                 return;
272         }
273 }
274
275 static int
276 update_connector_routing(struct drm_atomic_state *state,
277                          struct drm_connector *connector,
278                          struct drm_connector_state *old_connector_state,
279                          struct drm_connector_state *new_connector_state)
280 {
281         const struct drm_connector_helper_funcs *funcs;
282         struct drm_encoder *new_encoder;
283         struct drm_crtc_state *crtc_state;
284
285         DRM_DEBUG_ATOMIC("Updating routing for [CONNECTOR:%d:%s]\n",
286                          connector->base.id,
287                          connector->name);
288
289         if (old_connector_state->crtc != new_connector_state->crtc) {
290                 if (old_connector_state->crtc) {
291                         crtc_state = drm_atomic_get_new_crtc_state(state, old_connector_state->crtc);
292                         crtc_state->connectors_changed = true;
293                 }
294
295                 if (new_connector_state->crtc) {
296                         crtc_state = drm_atomic_get_new_crtc_state(state, new_connector_state->crtc);
297                         crtc_state->connectors_changed = true;
298                 }
299         }
300
301         if (!new_connector_state->crtc) {
302                 DRM_DEBUG_ATOMIC("Disabling [CONNECTOR:%d:%s]\n",
303                                 connector->base.id,
304                                 connector->name);
305
306                 set_best_encoder(state, new_connector_state, NULL);
307
308                 return 0;
309         }
310
311         crtc_state = drm_atomic_get_new_crtc_state(state,
312                                                    new_connector_state->crtc);
313         /*
314          * For compatibility with legacy users, we want to make sure that
315          * we allow DPMS On->Off modesets on unregistered connectors. Modesets
316          * which would result in anything else must be considered invalid, to
317          * avoid turning on new displays on dead connectors.
318          *
319          * Since the connector can be unregistered at any point during an
320          * atomic check or commit, this is racy. But that's OK: all we care
321          * about is ensuring that userspace can't do anything but shut off the
322          * display on a connector that was destroyed after its been notified,
323          * not before.
324          */
325         if (drm_connector_is_unregistered(connector) && crtc_state->active) {
326                 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] is not registered\n",
327                                  connector->base.id, connector->name);
328                 return -EINVAL;
329         }
330
331         funcs = connector->helper_private;
332
333         if (funcs->atomic_best_encoder)
334                 new_encoder = funcs->atomic_best_encoder(connector,
335                                                          new_connector_state);
336         else if (funcs->best_encoder)
337                 new_encoder = funcs->best_encoder(connector);
338         else
339                 new_encoder = drm_atomic_helper_best_encoder(connector);
340
341         if (!new_encoder) {
342                 DRM_DEBUG_ATOMIC("No suitable encoder found for [CONNECTOR:%d:%s]\n",
343                                  connector->base.id,
344                                  connector->name);
345                 return -EINVAL;
346         }
347
348         if (!drm_encoder_crtc_ok(new_encoder, new_connector_state->crtc)) {
349                 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] incompatible with [CRTC:%d:%s]\n",
350                                  new_encoder->base.id,
351                                  new_encoder->name,
352                                  new_connector_state->crtc->base.id,
353                                  new_connector_state->crtc->name);
354                 return -EINVAL;
355         }
356
357         if (new_encoder == new_connector_state->best_encoder) {
358                 set_best_encoder(state, new_connector_state, new_encoder);
359
360                 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] keeps [ENCODER:%d:%s], now on [CRTC:%d:%s]\n",
361                                  connector->base.id,
362                                  connector->name,
363                                  new_encoder->base.id,
364                                  new_encoder->name,
365                                  new_connector_state->crtc->base.id,
366                                  new_connector_state->crtc->name);
367
368                 return 0;
369         }
370
371         steal_encoder(state, new_encoder);
372
373         set_best_encoder(state, new_connector_state, new_encoder);
374
375         crtc_state->connectors_changed = true;
376
377         DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] using [ENCODER:%d:%s] on [CRTC:%d:%s]\n",
378                          connector->base.id,
379                          connector->name,
380                          new_encoder->base.id,
381                          new_encoder->name,
382                          new_connector_state->crtc->base.id,
383                          new_connector_state->crtc->name);
384
385         return 0;
386 }
387
388 static int
389 mode_fixup(struct drm_atomic_state *state)
390 {
391         struct drm_crtc *crtc;
392         struct drm_crtc_state *new_crtc_state;
393         struct drm_connector *connector;
394         struct drm_connector_state *new_conn_state;
395         int i;
396         int ret;
397
398         for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
399                 if (!new_crtc_state->mode_changed &&
400                     !new_crtc_state->connectors_changed)
401                         continue;
402
403                 drm_mode_copy(&new_crtc_state->adjusted_mode, &new_crtc_state->mode);
404         }
405
406         for_each_new_connector_in_state(state, connector, new_conn_state, i) {
407                 const struct drm_encoder_helper_funcs *funcs;
408                 struct drm_encoder *encoder;
409
410                 WARN_ON(!!new_conn_state->best_encoder != !!new_conn_state->crtc);
411
412                 if (!new_conn_state->crtc || !new_conn_state->best_encoder)
413                         continue;
414
415                 new_crtc_state =
416                         drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
417
418                 /*
419                  * Each encoder has at most one connector (since we always steal
420                  * it away), so we won't call ->mode_fixup twice.
421                  */
422                 encoder = new_conn_state->best_encoder;
423                 funcs = encoder->helper_private;
424
425                 ret = drm_bridge_mode_fixup(encoder->bridge, &new_crtc_state->mode,
426                                 &new_crtc_state->adjusted_mode);
427                 if (!ret) {
428                         DRM_DEBUG_ATOMIC("Bridge fixup failed\n");
429                         return -EINVAL;
430                 }
431
432                 if (funcs && funcs->atomic_check) {
433                         ret = funcs->atomic_check(encoder, new_crtc_state,
434                                                   new_conn_state);
435                         if (ret) {
436                                 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] check failed\n",
437                                                  encoder->base.id, encoder->name);
438                                 return ret;
439                         }
440                 } else if (funcs && funcs->mode_fixup) {
441                         ret = funcs->mode_fixup(encoder, &new_crtc_state->mode,
442                                                 &new_crtc_state->adjusted_mode);
443                         if (!ret) {
444                                 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] fixup failed\n",
445                                                  encoder->base.id, encoder->name);
446                                 return -EINVAL;
447                         }
448                 }
449         }
450
451         for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
452                 const struct drm_crtc_helper_funcs *funcs;
453
454                 if (!new_crtc_state->enable)
455                         continue;
456
457                 if (!new_crtc_state->mode_changed &&
458                     !new_crtc_state->connectors_changed)
459                         continue;
460
461                 funcs = crtc->helper_private;
462                 if (!funcs->mode_fixup)
463                         continue;
464
465                 ret = funcs->mode_fixup(crtc, &new_crtc_state->mode,
466                                         &new_crtc_state->adjusted_mode);
467                 if (!ret) {
468                         DRM_DEBUG_ATOMIC("[CRTC:%d:%s] fixup failed\n",
469                                          crtc->base.id, crtc->name);
470                         return -EINVAL;
471                 }
472         }
473
474         return 0;
475 }
476
477 static enum drm_mode_status mode_valid_path(struct drm_connector *connector,
478                                             struct drm_encoder *encoder,
479                                             struct drm_crtc *crtc,
480                                             struct drm_display_mode *mode)
481 {
482         enum drm_mode_status ret;
483
484         ret = drm_encoder_mode_valid(encoder, mode);
485         if (ret != MODE_OK) {
486                 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] mode_valid() failed\n",
487                                 encoder->base.id, encoder->name);
488                 return ret;
489         }
490
491         ret = drm_bridge_mode_valid(encoder->bridge, mode);
492         if (ret != MODE_OK) {
493                 DRM_DEBUG_ATOMIC("[BRIDGE] mode_valid() failed\n");
494                 return ret;
495         }
496
497         ret = drm_crtc_mode_valid(crtc, mode);
498         if (ret != MODE_OK) {
499                 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] mode_valid() failed\n",
500                                 crtc->base.id, crtc->name);
501                 return ret;
502         }
503
504         return ret;
505 }
506
507 static int
508 mode_valid(struct drm_atomic_state *state)
509 {
510         struct drm_connector_state *conn_state;
511         struct drm_connector *connector;
512         int i;
513
514         for_each_new_connector_in_state(state, connector, conn_state, i) {
515                 struct drm_encoder *encoder = conn_state->best_encoder;
516                 struct drm_crtc *crtc = conn_state->crtc;
517                 struct drm_crtc_state *crtc_state;
518                 enum drm_mode_status mode_status;
519                 struct drm_display_mode *mode;
520
521                 if (!crtc || !encoder)
522                         continue;
523
524                 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
525                 if (!crtc_state)
526                         continue;
527                 if (!crtc_state->mode_changed && !crtc_state->connectors_changed)
528                         continue;
529
530                 mode = &crtc_state->mode;
531
532                 mode_status = mode_valid_path(connector, encoder, crtc, mode);
533                 if (mode_status != MODE_OK)
534                         return -EINVAL;
535         }
536
537         return 0;
538 }
539
540 /**
541  * drm_atomic_helper_check_modeset - validate state object for modeset changes
542  * @dev: DRM device
543  * @state: the driver state object
544  *
545  * Check the state object to see if the requested state is physically possible.
546  * This does all the crtc and connector related computations for an atomic
547  * update and adds any additional connectors needed for full modesets. It calls
548  * the various per-object callbacks in the follow order:
549  *
550  * 1. &drm_connector_helper_funcs.atomic_best_encoder for determining the new encoder.
551  * 2. &drm_connector_helper_funcs.atomic_check to validate the connector state.
552  * 3. If it's determined a modeset is needed then all connectors on the affected crtc
553  *    crtc are added and &drm_connector_helper_funcs.atomic_check is run on them.
554  * 4. &drm_encoder_helper_funcs.mode_valid, &drm_bridge_funcs.mode_valid and
555  *    &drm_crtc_helper_funcs.mode_valid are called on the affected components.
556  * 5. &drm_bridge_funcs.mode_fixup is called on all encoder bridges.
557  * 6. &drm_encoder_helper_funcs.atomic_check is called to validate any encoder state.
558  *    This function is only called when the encoder will be part of a configured crtc,
559  *    it must not be used for implementing connector property validation.
560  *    If this function is NULL, &drm_atomic_encoder_helper_funcs.mode_fixup is called
561  *    instead.
562  * 7. &drm_crtc_helper_funcs.mode_fixup is called last, to fix up the mode with crtc constraints.
563  *
564  * &drm_crtc_state.mode_changed is set when the input mode is changed.
565  * &drm_crtc_state.connectors_changed is set when a connector is added or
566  * removed from the crtc.  &drm_crtc_state.active_changed is set when
567  * &drm_crtc_state.active changes, which is used for DPMS.
568  * See also: drm_atomic_crtc_needs_modeset()
569  *
570  * IMPORTANT:
571  *
572  * Drivers which set &drm_crtc_state.mode_changed (e.g. in their
573  * &drm_plane_helper_funcs.atomic_check hooks if a plane update can't be done
574  * without a full modeset) _must_ call this function afterwards after that
575  * change. It is permitted to call this function multiple times for the same
576  * update, e.g. when the &drm_crtc_helper_funcs.atomic_check functions depend
577  * upon the adjusted dotclock for fifo space allocation and watermark
578  * computation.
579  *
580  * RETURNS:
581  * Zero for success or -errno
582  */
583 int
584 drm_atomic_helper_check_modeset(struct drm_device *dev,
585                                 struct drm_atomic_state *state)
586 {
587         struct drm_crtc *crtc;
588         struct drm_crtc_state *old_crtc_state, *new_crtc_state;
589         struct drm_connector *connector;
590         struct drm_connector_state *old_connector_state, *new_connector_state;
591         int i, ret;
592         unsigned connectors_mask = 0;
593
594         for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
595                 bool has_connectors =
596                         !!new_crtc_state->connector_mask;
597
598                 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
599
600                 if (!drm_mode_equal(&old_crtc_state->mode, &new_crtc_state->mode)) {
601                         DRM_DEBUG_ATOMIC("[CRTC:%d:%s] mode changed\n",
602                                          crtc->base.id, crtc->name);
603                         new_crtc_state->mode_changed = true;
604                 }
605
606                 if (old_crtc_state->enable != new_crtc_state->enable) {
607                         DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enable changed\n",
608                                          crtc->base.id, crtc->name);
609
610                         /*
611                          * For clarity this assignment is done here, but
612                          * enable == 0 is only true when there are no
613                          * connectors and a NULL mode.
614                          *
615                          * The other way around is true as well. enable != 0
616                          * iff connectors are attached and a mode is set.
617                          */
618                         new_crtc_state->mode_changed = true;
619                         new_crtc_state->connectors_changed = true;
620                 }
621
622                 if (old_crtc_state->active != new_crtc_state->active) {
623                         DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active changed\n",
624                                          crtc->base.id, crtc->name);
625                         new_crtc_state->active_changed = true;
626                 }
627
628                 if (new_crtc_state->enable != has_connectors) {
629                         DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled/connectors mismatch\n",
630                                          crtc->base.id, crtc->name);
631
632                         return -EINVAL;
633                 }
634         }
635
636         ret = handle_conflicting_encoders(state, false);
637         if (ret)
638                 return ret;
639
640         for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
641                 const struct drm_connector_helper_funcs *funcs = connector->helper_private;
642
643                 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
644
645                 /*
646                  * This only sets crtc->connectors_changed for routing changes,
647                  * drivers must set crtc->connectors_changed themselves when
648                  * connector properties need to be updated.
649                  */
650                 ret = update_connector_routing(state, connector,
651                                                old_connector_state,
652                                                new_connector_state);
653                 if (ret)
654                         return ret;
655                 if (old_connector_state->crtc) {
656                         new_crtc_state = drm_atomic_get_new_crtc_state(state,
657                                                                        old_connector_state->crtc);
658                         if (old_connector_state->link_status !=
659                             new_connector_state->link_status)
660                                 new_crtc_state->connectors_changed = true;
661                 }
662
663                 if (funcs->atomic_check)
664                         ret = funcs->atomic_check(connector, new_connector_state);
665                 if (ret)
666                         return ret;
667
668                 connectors_mask |= BIT(i);
669         }
670
671         /*
672          * After all the routing has been prepared we need to add in any
673          * connector which is itself unchanged, but who's crtc changes it's
674          * configuration. This must be done before calling mode_fixup in case a
675          * crtc only changed its mode but has the same set of connectors.
676          */
677         for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
678                 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
679                         continue;
680
681                 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] needs all connectors, enable: %c, active: %c\n",
682                                  crtc->base.id, crtc->name,
683                                  new_crtc_state->enable ? 'y' : 'n',
684                                  new_crtc_state->active ? 'y' : 'n');
685
686                 ret = drm_atomic_add_affected_connectors(state, crtc);
687                 if (ret != 0)
688                         return ret;
689
690                 ret = drm_atomic_add_affected_planes(state, crtc);
691                 if (ret != 0)
692                         return ret;
693         }
694
695         /*
696          * Iterate over all connectors again, to make sure atomic_check()
697          * has been called on them when a modeset is forced.
698          */
699         for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
700                 const struct drm_connector_helper_funcs *funcs = connector->helper_private;
701
702                 if (connectors_mask & BIT(i))
703                         continue;
704
705                 if (funcs->atomic_check)
706                         ret = funcs->atomic_check(connector, new_connector_state);
707                 if (ret)
708                         return ret;
709         }
710
711         ret = mode_valid(state);
712         if (ret)
713                 return ret;
714
715         return mode_fixup(state);
716 }
717 EXPORT_SYMBOL(drm_atomic_helper_check_modeset);
718
719 /**
720  * drm_atomic_helper_check_plane_state() - Check plane state for validity
721  * @plane_state: plane state to check
722  * @crtc_state: crtc state to check
723  * @min_scale: minimum @src:@dest scaling factor in 16.16 fixed point
724  * @max_scale: maximum @src:@dest scaling factor in 16.16 fixed point
725  * @can_position: is it legal to position the plane such that it
726  *                doesn't cover the entire crtc?  This will generally
727  *                only be false for primary planes.
728  * @can_update_disabled: can the plane be updated while the crtc
729  *                       is disabled?
730  *
731  * Checks that a desired plane update is valid, and updates various
732  * bits of derived state (clipped coordinates etc.). Drivers that provide
733  * their own plane handling rather than helper-provided implementations may
734  * still wish to call this function to avoid duplication of error checking
735  * code.
736  *
737  * RETURNS:
738  * Zero if update appears valid, error code on failure
739  */
740 int drm_atomic_helper_check_plane_state(struct drm_plane_state *plane_state,
741                                         const struct drm_crtc_state *crtc_state,
742                                         int min_scale,
743                                         int max_scale,
744                                         bool can_position,
745                                         bool can_update_disabled)
746 {
747         struct drm_framebuffer *fb = plane_state->fb;
748         struct drm_rect *src = &plane_state->src;
749         struct drm_rect *dst = &plane_state->dst;
750         unsigned int rotation = plane_state->rotation;
751         struct drm_rect clip = {};
752         int hscale, vscale;
753
754         WARN_ON(plane_state->crtc && plane_state->crtc != crtc_state->crtc);
755
756         *src = drm_plane_state_src(plane_state);
757         *dst = drm_plane_state_dest(plane_state);
758
759         if (!fb) {
760                 plane_state->visible = false;
761                 return 0;
762         }
763
764         /* crtc should only be NULL when disabling (i.e., !fb) */
765         if (WARN_ON(!plane_state->crtc)) {
766                 plane_state->visible = false;
767                 return 0;
768         }
769
770         if (!crtc_state->enable && !can_update_disabled) {
771                 DRM_DEBUG_KMS("Cannot update plane of a disabled CRTC.\n");
772                 return -EINVAL;
773         }
774
775         drm_rect_rotate(src, fb->width << 16, fb->height << 16, rotation);
776
777         /* Check scaling */
778         hscale = drm_rect_calc_hscale(src, dst, min_scale, max_scale);
779         vscale = drm_rect_calc_vscale(src, dst, min_scale, max_scale);
780         if (hscale < 0 || vscale < 0) {
781                 DRM_DEBUG_KMS("Invalid scaling of plane\n");
782                 drm_rect_debug_print("src: ", &plane_state->src, true);
783                 drm_rect_debug_print("dst: ", &plane_state->dst, false);
784                 return -ERANGE;
785         }
786
787         if (crtc_state->enable)
788                 drm_mode_get_hv_timing(&crtc_state->mode, &clip.x2, &clip.y2);
789
790         plane_state->visible = drm_rect_clip_scaled(src, dst, &clip);
791
792         drm_rect_rotate_inv(src, fb->width << 16, fb->height << 16, rotation);
793
794         if (!plane_state->visible)
795                 /*
796                  * Plane isn't visible; some drivers can handle this
797                  * so we just return success here.  Drivers that can't
798                  * (including those that use the primary plane helper's
799                  * update function) will return an error from their
800                  * update_plane handler.
801                  */
802                 return 0;
803
804         if (!can_position && !drm_rect_equals(dst, &clip)) {
805                 DRM_DEBUG_KMS("Plane must cover entire CRTC\n");
806                 drm_rect_debug_print("dst: ", dst, false);
807                 drm_rect_debug_print("clip: ", &clip, false);
808                 return -EINVAL;
809         }
810
811         return 0;
812 }
813 EXPORT_SYMBOL(drm_atomic_helper_check_plane_state);
814
815 /**
816  * drm_atomic_helper_check_planes - validate state object for planes changes
817  * @dev: DRM device
818  * @state: the driver state object
819  *
820  * Check the state object to see if the requested state is physically possible.
821  * This does all the plane update related checks using by calling into the
822  * &drm_crtc_helper_funcs.atomic_check and &drm_plane_helper_funcs.atomic_check
823  * hooks provided by the driver.
824  *
825  * It also sets &drm_crtc_state.planes_changed to indicate that a crtc has
826  * updated planes.
827  *
828  * RETURNS:
829  * Zero for success or -errno
830  */
831 int
832 drm_atomic_helper_check_planes(struct drm_device *dev,
833                                struct drm_atomic_state *state)
834 {
835         struct drm_crtc *crtc;
836         struct drm_crtc_state *new_crtc_state;
837         struct drm_plane *plane;
838         struct drm_plane_state *new_plane_state, *old_plane_state;
839         int i, ret = 0;
840
841         for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
842                 const struct drm_plane_helper_funcs *funcs;
843
844                 WARN_ON(!drm_modeset_is_locked(&plane->mutex));
845
846                 funcs = plane->helper_private;
847
848                 drm_atomic_helper_plane_changed(state, old_plane_state, new_plane_state, plane);
849
850                 if (!funcs || !funcs->atomic_check)
851                         continue;
852
853                 ret = funcs->atomic_check(plane, new_plane_state);
854                 if (ret) {
855                         DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic driver check failed\n",
856                                          plane->base.id, plane->name);
857                         return ret;
858                 }
859         }
860
861         for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
862                 const struct drm_crtc_helper_funcs *funcs;
863
864                 funcs = crtc->helper_private;
865
866                 if (!funcs || !funcs->atomic_check)
867                         continue;
868
869                 ret = funcs->atomic_check(crtc, new_crtc_state);
870                 if (ret) {
871                         DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic driver check failed\n",
872                                          crtc->base.id, crtc->name);
873                         return ret;
874                 }
875         }
876
877         return ret;
878 }
879 EXPORT_SYMBOL(drm_atomic_helper_check_planes);
880
881 /**
882  * drm_atomic_helper_check - validate state object
883  * @dev: DRM device
884  * @state: the driver state object
885  *
886  * Check the state object to see if the requested state is physically possible.
887  * Only crtcs and planes have check callbacks, so for any additional (global)
888  * checking that a driver needs it can simply wrap that around this function.
889  * Drivers without such needs can directly use this as their
890  * &drm_mode_config_funcs.atomic_check callback.
891  *
892  * This just wraps the two parts of the state checking for planes and modeset
893  * state in the default order: First it calls drm_atomic_helper_check_modeset()
894  * and then drm_atomic_helper_check_planes(). The assumption is that the
895  * @drm_plane_helper_funcs.atomic_check and @drm_crtc_helper_funcs.atomic_check
896  * functions depend upon an updated adjusted_mode.clock to e.g. properly compute
897  * watermarks.
898  *
899  * Note that zpos normalization will add all enable planes to the state which
900  * might not desired for some drivers.
901  * For example enable/disable of a cursor plane which have fixed zpos value
902  * would trigger all other enabled planes to be forced to the state change.
903  *
904  * RETURNS:
905  * Zero for success or -errno
906  */
907 int drm_atomic_helper_check(struct drm_device *dev,
908                             struct drm_atomic_state *state)
909 {
910         int ret;
911
912         ret = drm_atomic_helper_check_modeset(dev, state);
913         if (ret)
914                 return ret;
915
916         if (dev->mode_config.normalize_zpos) {
917                 ret = drm_atomic_normalize_zpos(dev, state);
918                 if (ret)
919                         return ret;
920         }
921
922         ret = drm_atomic_helper_check_planes(dev, state);
923         if (ret)
924                 return ret;
925
926         if (state->legacy_cursor_update)
927                 state->async_update = !drm_atomic_helper_async_check(dev, state);
928
929         return ret;
930 }
931 EXPORT_SYMBOL(drm_atomic_helper_check);
932
933 static void
934 disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state)
935 {
936         struct drm_connector *connector;
937         struct drm_connector_state *old_conn_state, *new_conn_state;
938         struct drm_crtc *crtc;
939         struct drm_crtc_state *old_crtc_state, *new_crtc_state;
940         int i;
941
942         for_each_oldnew_connector_in_state(old_state, connector, old_conn_state, new_conn_state, i) {
943                 const struct drm_encoder_helper_funcs *funcs;
944                 struct drm_encoder *encoder;
945
946                 /* Shut down everything that's in the changeset and currently
947                  * still on. So need to check the old, saved state. */
948                 if (!old_conn_state->crtc)
949                         continue;
950
951                 old_crtc_state = drm_atomic_get_old_crtc_state(old_state, old_conn_state->crtc);
952
953                 if (!old_crtc_state->active ||
954                     !drm_atomic_crtc_needs_modeset(old_conn_state->crtc->state))
955                         continue;
956
957                 encoder = old_conn_state->best_encoder;
958
959                 /* We shouldn't get this far if we didn't previously have
960                  * an encoder.. but WARN_ON() rather than explode.
961                  */
962                 if (WARN_ON(!encoder))
963                         continue;
964
965                 funcs = encoder->helper_private;
966
967                 DRM_DEBUG_ATOMIC("disabling [ENCODER:%d:%s]\n",
968                                  encoder->base.id, encoder->name);
969
970                 /*
971                  * Each encoder has at most one connector (since we always steal
972                  * it away), so we won't call disable hooks twice.
973                  */
974                 drm_bridge_disable(encoder->bridge);
975
976                 /* Right function depends upon target state. */
977                 if (funcs) {
978                         if (new_conn_state->crtc && funcs->prepare)
979                                 funcs->prepare(encoder);
980                         else if (funcs->disable)
981                                 funcs->disable(encoder);
982                         else if (funcs->dpms)
983                                 funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
984                 }
985
986                 drm_bridge_post_disable(encoder->bridge);
987         }
988
989         for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
990                 const struct drm_crtc_helper_funcs *funcs;
991                 int ret;
992
993                 /* Shut down everything that needs a full modeset. */
994                 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
995                         continue;
996
997                 if (!old_crtc_state->active)
998                         continue;
999
1000                 funcs = crtc->helper_private;
1001
1002                 DRM_DEBUG_ATOMIC("disabling [CRTC:%d:%s]\n",
1003                                  crtc->base.id, crtc->name);
1004
1005
1006                 /* Right function depends upon target state. */
1007                 if (new_crtc_state->enable && funcs->prepare)
1008                         funcs->prepare(crtc);
1009                 else if (funcs->atomic_disable)
1010                         funcs->atomic_disable(crtc, old_crtc_state);
1011                 else if (funcs->disable)
1012                         funcs->disable(crtc);
1013                 else
1014                         funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
1015
1016                 if (!(dev->irq_enabled && dev->num_crtcs))
1017                         continue;
1018
1019                 ret = drm_crtc_vblank_get(crtc);
1020                 WARN_ONCE(ret != -EINVAL, "driver forgot to call drm_crtc_vblank_off()\n");
1021                 if (ret == 0)
1022                         drm_crtc_vblank_put(crtc);
1023         }
1024 }
1025
1026 /**
1027  * drm_atomic_helper_update_legacy_modeset_state - update legacy modeset state
1028  * @dev: DRM device
1029  * @old_state: atomic state object with old state structures
1030  *
1031  * This function updates all the various legacy modeset state pointers in
1032  * connectors, encoders and crtcs. It also updates the timestamping constants
1033  * used for precise vblank timestamps by calling
1034  * drm_calc_timestamping_constants().
1035  *
1036  * Drivers can use this for building their own atomic commit if they don't have
1037  * a pure helper-based modeset implementation.
1038  *
1039  * Since these updates are not synchronized with lockings, only code paths
1040  * called from &drm_mode_config_helper_funcs.atomic_commit_tail can look at the
1041  * legacy state filled out by this helper. Defacto this means this helper and
1042  * the legacy state pointers are only really useful for transitioning an
1043  * existing driver to the atomic world.
1044  */
1045 void
1046 drm_atomic_helper_update_legacy_modeset_state(struct drm_device *dev,
1047                                               struct drm_atomic_state *old_state)
1048 {
1049         struct drm_connector *connector;
1050         struct drm_connector_state *old_conn_state, *new_conn_state;
1051         struct drm_crtc *crtc;
1052         struct drm_crtc_state *new_crtc_state;
1053         int i;
1054
1055         /* clear out existing links and update dpms */
1056         for_each_oldnew_connector_in_state(old_state, connector, old_conn_state, new_conn_state, i) {
1057                 if (connector->encoder) {
1058                         WARN_ON(!connector->encoder->crtc);
1059
1060                         connector->encoder->crtc = NULL;
1061                         connector->encoder = NULL;
1062                 }
1063
1064                 crtc = new_conn_state->crtc;
1065                 if ((!crtc && old_conn_state->crtc) ||
1066                     (crtc && drm_atomic_crtc_needs_modeset(crtc->state))) {
1067                         int mode = DRM_MODE_DPMS_OFF;
1068
1069                         if (crtc && crtc->state->active)
1070                                 mode = DRM_MODE_DPMS_ON;
1071
1072                         connector->dpms = mode;
1073                 }
1074         }
1075
1076         /* set new links */
1077         for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
1078                 if (!new_conn_state->crtc)
1079                         continue;
1080
1081                 if (WARN_ON(!new_conn_state->best_encoder))
1082                         continue;
1083
1084                 connector->encoder = new_conn_state->best_encoder;
1085                 connector->encoder->crtc = new_conn_state->crtc;
1086         }
1087
1088         /* set legacy state in the crtc structure */
1089         for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
1090                 struct drm_plane *primary = crtc->primary;
1091                 struct drm_plane_state *new_plane_state;
1092
1093                 crtc->mode = new_crtc_state->mode;
1094                 crtc->enabled = new_crtc_state->enable;
1095
1096                 new_plane_state =
1097                         drm_atomic_get_new_plane_state(old_state, primary);
1098
1099                 if (new_plane_state && new_plane_state->crtc == crtc) {
1100                         crtc->x = new_plane_state->src_x >> 16;
1101                         crtc->y = new_plane_state->src_y >> 16;
1102                 }
1103
1104                 if (new_crtc_state->enable)
1105                         drm_calc_timestamping_constants(crtc,
1106                                                         &new_crtc_state->adjusted_mode);
1107         }
1108 }
1109 EXPORT_SYMBOL(drm_atomic_helper_update_legacy_modeset_state);
1110
1111 static void
1112 crtc_set_mode(struct drm_device *dev, struct drm_atomic_state *old_state)
1113 {
1114         struct drm_crtc *crtc;
1115         struct drm_crtc_state *new_crtc_state;
1116         struct drm_connector *connector;
1117         struct drm_connector_state *new_conn_state;
1118         int i;
1119
1120         for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
1121                 const struct drm_crtc_helper_funcs *funcs;
1122
1123                 if (!new_crtc_state->mode_changed)
1124                         continue;
1125
1126                 funcs = crtc->helper_private;
1127
1128                 if (new_crtc_state->enable && funcs->mode_set_nofb) {
1129                         DRM_DEBUG_ATOMIC("modeset on [CRTC:%d:%s]\n",
1130                                          crtc->base.id, crtc->name);
1131
1132                         funcs->mode_set_nofb(crtc);
1133                 }
1134         }
1135
1136         for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
1137                 const struct drm_encoder_helper_funcs *funcs;
1138                 struct drm_encoder *encoder;
1139                 struct drm_display_mode *mode, *adjusted_mode;
1140
1141                 if (!new_conn_state->best_encoder)
1142                         continue;
1143
1144                 encoder = new_conn_state->best_encoder;
1145                 funcs = encoder->helper_private;
1146                 new_crtc_state = new_conn_state->crtc->state;
1147                 mode = &new_crtc_state->mode;
1148                 adjusted_mode = &new_crtc_state->adjusted_mode;
1149
1150                 if (!new_crtc_state->mode_changed)
1151                         continue;
1152
1153                 DRM_DEBUG_ATOMIC("modeset on [ENCODER:%d:%s]\n",
1154                                  encoder->base.id, encoder->name);
1155
1156                 /*
1157                  * Each encoder has at most one connector (since we always steal
1158                  * it away), so we won't call mode_set hooks twice.
1159                  */
1160                 if (funcs && funcs->atomic_mode_set) {
1161                         funcs->atomic_mode_set(encoder, new_crtc_state,
1162                                                new_conn_state);
1163                 } else if (funcs && funcs->mode_set) {
1164                         funcs->mode_set(encoder, mode, adjusted_mode);
1165                 }
1166
1167                 drm_bridge_mode_set(encoder->bridge, mode, adjusted_mode);
1168         }
1169 }
1170
1171 /**
1172  * drm_atomic_helper_commit_modeset_disables - modeset commit to disable outputs
1173  * @dev: DRM device
1174  * @old_state: atomic state object with old state structures
1175  *
1176  * This function shuts down all the outputs that need to be shut down and
1177  * prepares them (if required) with the new mode.
1178  *
1179  * For compatibility with legacy crtc helpers this should be called before
1180  * drm_atomic_helper_commit_planes(), which is what the default commit function
1181  * does. But drivers with different needs can group the modeset commits together
1182  * and do the plane commits at the end. This is useful for drivers doing runtime
1183  * PM since planes updates then only happen when the CRTC is actually enabled.
1184  */
1185 void drm_atomic_helper_commit_modeset_disables(struct drm_device *dev,
1186                                                struct drm_atomic_state *old_state)
1187 {
1188         disable_outputs(dev, old_state);
1189
1190         drm_atomic_helper_update_legacy_modeset_state(dev, old_state);
1191
1192         crtc_set_mode(dev, old_state);
1193 }
1194 EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_disables);
1195
1196 static void drm_atomic_helper_commit_writebacks(struct drm_device *dev,
1197                                                 struct drm_atomic_state *old_state)
1198 {
1199         struct drm_connector *connector;
1200         struct drm_connector_state *new_conn_state;
1201         int i;
1202
1203         for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
1204                 const struct drm_connector_helper_funcs *funcs;
1205
1206                 funcs = connector->helper_private;
1207                 if (!funcs->atomic_commit)
1208                         continue;
1209
1210                 if (new_conn_state->writeback_job && new_conn_state->writeback_job->fb) {
1211                         WARN_ON(connector->connector_type != DRM_MODE_CONNECTOR_WRITEBACK);
1212                         funcs->atomic_commit(connector, new_conn_state);
1213                 }
1214         }
1215 }
1216
1217 /**
1218  * drm_atomic_helper_commit_modeset_enables - modeset commit to enable outputs
1219  * @dev: DRM device
1220  * @old_state: atomic state object with old state structures
1221  *
1222  * This function enables all the outputs with the new configuration which had to
1223  * be turned off for the update.
1224  *
1225  * For compatibility with legacy crtc helpers this should be called after
1226  * drm_atomic_helper_commit_planes(), which is what the default commit function
1227  * does. But drivers with different needs can group the modeset commits together
1228  * and do the plane commits at the end. This is useful for drivers doing runtime
1229  * PM since planes updates then only happen when the CRTC is actually enabled.
1230  */
1231 void drm_atomic_helper_commit_modeset_enables(struct drm_device *dev,
1232                                               struct drm_atomic_state *old_state)
1233 {
1234         struct drm_crtc *crtc;
1235         struct drm_crtc_state *old_crtc_state;
1236         struct drm_crtc_state *new_crtc_state;
1237         struct drm_connector *connector;
1238         struct drm_connector_state *new_conn_state;
1239         int i;
1240
1241         for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
1242                 const struct drm_crtc_helper_funcs *funcs;
1243
1244                 /* Need to filter out CRTCs where only planes change. */
1245                 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
1246                         continue;
1247
1248                 if (!new_crtc_state->active)
1249                         continue;
1250
1251                 funcs = crtc->helper_private;
1252
1253                 if (new_crtc_state->enable) {
1254                         DRM_DEBUG_ATOMIC("enabling [CRTC:%d:%s]\n",
1255                                          crtc->base.id, crtc->name);
1256
1257                         if (funcs->atomic_enable)
1258                                 funcs->atomic_enable(crtc, old_crtc_state);
1259                         else
1260                                 funcs->commit(crtc);
1261                 }
1262         }
1263
1264         for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
1265                 const struct drm_encoder_helper_funcs *funcs;
1266                 struct drm_encoder *encoder;
1267
1268                 if (!new_conn_state->best_encoder)
1269                         continue;
1270
1271                 if (!new_conn_state->crtc->state->active ||
1272                     !drm_atomic_crtc_needs_modeset(new_conn_state->crtc->state))
1273                         continue;
1274
1275                 encoder = new_conn_state->best_encoder;
1276                 funcs = encoder->helper_private;
1277
1278                 DRM_DEBUG_ATOMIC("enabling [ENCODER:%d:%s]\n",
1279                                  encoder->base.id, encoder->name);
1280
1281                 /*
1282                  * Each encoder has at most one connector (since we always steal
1283                  * it away), so we won't call enable hooks twice.
1284                  */
1285                 drm_bridge_pre_enable(encoder->bridge);
1286
1287                 if (funcs) {
1288                         if (funcs->enable)
1289                                 funcs->enable(encoder);
1290                         else if (funcs->commit)
1291                                 funcs->commit(encoder);
1292                 }
1293
1294                 drm_bridge_enable(encoder->bridge);
1295         }
1296
1297         drm_atomic_helper_commit_writebacks(dev, old_state);
1298 }
1299 EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_enables);
1300
1301 /**
1302  * drm_atomic_helper_wait_for_fences - wait for fences stashed in plane state
1303  * @dev: DRM device
1304  * @state: atomic state object with old state structures
1305  * @pre_swap: If true, do an interruptible wait, and @state is the new state.
1306  *      Otherwise @state is the old state.
1307  *
1308  * For implicit sync, driver should fish the exclusive fence out from the
1309  * incoming fb's and stash it in the drm_plane_state.  This is called after
1310  * drm_atomic_helper_swap_state() so it uses the current plane state (and
1311  * just uses the atomic state to find the changed planes)
1312  *
1313  * Note that @pre_swap is needed since the point where we block for fences moves
1314  * around depending upon whether an atomic commit is blocking or
1315  * non-blocking. For non-blocking commit all waiting needs to happen after
1316  * drm_atomic_helper_swap_state() is called, but for blocking commits we want
1317  * to wait **before** we do anything that can't be easily rolled back. That is
1318  * before we call drm_atomic_helper_swap_state().
1319  *
1320  * Returns zero if success or < 0 if dma_fence_wait() fails.
1321  */
1322 int drm_atomic_helper_wait_for_fences(struct drm_device *dev,
1323                                       struct drm_atomic_state *state,
1324                                       bool pre_swap)
1325 {
1326         struct drm_plane *plane;
1327         struct drm_plane_state *new_plane_state;
1328         int i, ret;
1329
1330         for_each_new_plane_in_state(state, plane, new_plane_state, i) {
1331                 if (!new_plane_state->fence)
1332                         continue;
1333
1334                 WARN_ON(!new_plane_state->fb);
1335
1336                 /*
1337                  * If waiting for fences pre-swap (ie: nonblock), userspace can
1338                  * still interrupt the operation. Instead of blocking until the
1339                  * timer expires, make the wait interruptible.
1340                  */
1341                 ret = dma_fence_wait(new_plane_state->fence, pre_swap);
1342                 if (ret)
1343                         return ret;
1344
1345                 dma_fence_put(new_plane_state->fence);
1346                 new_plane_state->fence = NULL;
1347         }
1348
1349         return 0;
1350 }
1351 EXPORT_SYMBOL(drm_atomic_helper_wait_for_fences);
1352
1353 /**
1354  * drm_atomic_helper_wait_for_vblanks - wait for vblank on crtcs
1355  * @dev: DRM device
1356  * @old_state: atomic state object with old state structures
1357  *
1358  * Helper to, after atomic commit, wait for vblanks on all effected
1359  * crtcs (ie. before cleaning up old framebuffers using
1360  * drm_atomic_helper_cleanup_planes()). It will only wait on CRTCs where the
1361  * framebuffers have actually changed to optimize for the legacy cursor and
1362  * plane update use-case.
1363  *
1364  * Drivers using the nonblocking commit tracking support initialized by calling
1365  * drm_atomic_helper_setup_commit() should look at
1366  * drm_atomic_helper_wait_for_flip_done() as an alternative.
1367  */
1368 void
1369 drm_atomic_helper_wait_for_vblanks(struct drm_device *dev,
1370                 struct drm_atomic_state *old_state)
1371 {
1372         struct drm_crtc *crtc;
1373         struct drm_crtc_state *old_crtc_state, *new_crtc_state;
1374         int i, ret;
1375         unsigned crtc_mask = 0;
1376
1377          /*
1378           * Legacy cursor ioctls are completely unsynced, and userspace
1379           * relies on that (by doing tons of cursor updates).
1380           */
1381         if (old_state->legacy_cursor_update)
1382                 return;
1383
1384         for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
1385                 if (!new_crtc_state->active)
1386                         continue;
1387
1388                 ret = drm_crtc_vblank_get(crtc);
1389                 if (ret != 0)
1390                         continue;
1391
1392                 crtc_mask |= drm_crtc_mask(crtc);
1393                 old_state->crtcs[i].last_vblank_count = drm_crtc_vblank_count(crtc);
1394         }
1395
1396         for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
1397                 if (!(crtc_mask & drm_crtc_mask(crtc)))
1398                         continue;
1399
1400                 ret = wait_event_timeout(dev->vblank[i].queue,
1401                                 old_state->crtcs[i].last_vblank_count !=
1402                                         drm_crtc_vblank_count(crtc),
1403                                 msecs_to_jiffies(50));
1404
1405                 WARN(!ret, "[CRTC:%d:%s] vblank wait timed out\n",
1406                      crtc->base.id, crtc->name);
1407
1408                 drm_crtc_vblank_put(crtc);
1409         }
1410 }
1411 EXPORT_SYMBOL(drm_atomic_helper_wait_for_vblanks);
1412
1413 /**
1414  * drm_atomic_helper_wait_for_flip_done - wait for all page flips to be done
1415  * @dev: DRM device
1416  * @old_state: atomic state object with old state structures
1417  *
1418  * Helper to, after atomic commit, wait for page flips on all effected
1419  * crtcs (ie. before cleaning up old framebuffers using
1420  * drm_atomic_helper_cleanup_planes()). Compared to
1421  * drm_atomic_helper_wait_for_vblanks() this waits for the completion of on all
1422  * CRTCs, assuming that cursors-only updates are signalling their completion
1423  * immediately (or using a different path).
1424  *
1425  * This requires that drivers use the nonblocking commit tracking support
1426  * initialized using drm_atomic_helper_setup_commit().
1427  */
1428 void drm_atomic_helper_wait_for_flip_done(struct drm_device *dev,
1429                                           struct drm_atomic_state *old_state)
1430 {
1431         struct drm_crtc *crtc;
1432         int i;
1433
1434         for (i = 0; i < dev->mode_config.num_crtc; i++) {
1435                 struct drm_crtc_commit *commit = old_state->crtcs[i].commit;
1436                 int ret;
1437
1438                 crtc = old_state->crtcs[i].ptr;
1439
1440                 if (!crtc || !commit)
1441                         continue;
1442
1443                 ret = wait_for_completion_timeout(&commit->flip_done, 10 * HZ);
1444                 if (ret == 0)
1445                         DRM_ERROR("[CRTC:%d:%s] flip_done timed out\n",
1446                                   crtc->base.id, crtc->name);
1447         }
1448 }
1449 EXPORT_SYMBOL(drm_atomic_helper_wait_for_flip_done);
1450
1451 /**
1452  * drm_atomic_helper_commit_tail - commit atomic update to hardware
1453  * @old_state: atomic state object with old state structures
1454  *
1455  * This is the default implementation for the
1456  * &drm_mode_config_helper_funcs.atomic_commit_tail hook, for drivers
1457  * that do not support runtime_pm or do not need the CRTC to be
1458  * enabled to perform a commit. Otherwise, see
1459  * drm_atomic_helper_commit_tail_rpm().
1460  *
1461  * Note that the default ordering of how the various stages are called is to
1462  * match the legacy modeset helper library closest.
1463  */
1464 void drm_atomic_helper_commit_tail(struct drm_atomic_state *old_state)
1465 {
1466         struct drm_device *dev = old_state->dev;
1467
1468         drm_atomic_helper_commit_modeset_disables(dev, old_state);
1469
1470         drm_atomic_helper_commit_planes(dev, old_state, 0);
1471
1472         drm_atomic_helper_commit_modeset_enables(dev, old_state);
1473
1474         drm_atomic_helper_fake_vblank(old_state);
1475
1476         drm_atomic_helper_commit_hw_done(old_state);
1477
1478         drm_atomic_helper_wait_for_vblanks(dev, old_state);
1479
1480         drm_atomic_helper_cleanup_planes(dev, old_state);
1481 }
1482 EXPORT_SYMBOL(drm_atomic_helper_commit_tail);
1483
1484 /**
1485  * drm_atomic_helper_commit_tail_rpm - commit atomic update to hardware
1486  * @old_state: new modeset state to be committed
1487  *
1488  * This is an alternative implementation for the
1489  * &drm_mode_config_helper_funcs.atomic_commit_tail hook, for drivers
1490  * that support runtime_pm or need the CRTC to be enabled to perform a
1491  * commit. Otherwise, one should use the default implementation
1492  * drm_atomic_helper_commit_tail().
1493  */
1494 void drm_atomic_helper_commit_tail_rpm(struct drm_atomic_state *old_state)
1495 {
1496         struct drm_device *dev = old_state->dev;
1497
1498         drm_atomic_helper_commit_modeset_disables(dev, old_state);
1499
1500         drm_atomic_helper_commit_modeset_enables(dev, old_state);
1501
1502         drm_atomic_helper_commit_planes(dev, old_state,
1503                                         DRM_PLANE_COMMIT_ACTIVE_ONLY);
1504
1505         drm_atomic_helper_fake_vblank(old_state);
1506
1507         drm_atomic_helper_commit_hw_done(old_state);
1508
1509         drm_atomic_helper_wait_for_vblanks(dev, old_state);
1510
1511         drm_atomic_helper_cleanup_planes(dev, old_state);
1512 }
1513 EXPORT_SYMBOL(drm_atomic_helper_commit_tail_rpm);
1514
1515 static void commit_tail(struct drm_atomic_state *old_state)
1516 {
1517         struct drm_device *dev = old_state->dev;
1518         const struct drm_mode_config_helper_funcs *funcs;
1519
1520         funcs = dev->mode_config.helper_private;
1521
1522         drm_atomic_helper_wait_for_fences(dev, old_state, false);
1523
1524         drm_atomic_helper_wait_for_dependencies(old_state);
1525
1526         if (funcs && funcs->atomic_commit_tail)
1527                 funcs->atomic_commit_tail(old_state);
1528         else
1529                 drm_atomic_helper_commit_tail(old_state);
1530
1531         drm_atomic_helper_commit_cleanup_done(old_state);
1532
1533         drm_atomic_state_put(old_state);
1534 }
1535
1536 static void commit_work(struct work_struct *work)
1537 {
1538         struct drm_atomic_state *state = container_of(work,
1539                                                       struct drm_atomic_state,
1540                                                       commit_work);
1541         commit_tail(state);
1542 }
1543
1544 /**
1545  * drm_atomic_helper_async_check - check if state can be commited asynchronously
1546  * @dev: DRM device
1547  * @state: the driver state object
1548  *
1549  * This helper will check if it is possible to commit the state asynchronously.
1550  * Async commits are not supposed to swap the states like normal sync commits
1551  * but just do in-place changes on the current state.
1552  *
1553  * It will return 0 if the commit can happen in an asynchronous fashion or error
1554  * if not. Note that error just mean it can't be commited asynchronously, if it
1555  * fails the commit should be treated like a normal synchronous commit.
1556  */
1557 int drm_atomic_helper_async_check(struct drm_device *dev,
1558                                    struct drm_atomic_state *state)
1559 {
1560         struct drm_crtc *crtc;
1561         struct drm_crtc_state *crtc_state;
1562         struct drm_plane *plane = NULL;
1563         struct drm_plane_state *old_plane_state = NULL;
1564         struct drm_plane_state *new_plane_state = NULL;
1565         const struct drm_plane_helper_funcs *funcs;
1566         int i, n_planes = 0;
1567
1568         for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
1569                 if (drm_atomic_crtc_needs_modeset(crtc_state))
1570                         return -EINVAL;
1571         }
1572
1573         for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i)
1574                 n_planes++;
1575
1576         /* FIXME: we support only single plane updates for now */
1577         if (n_planes != 1)
1578                 return -EINVAL;
1579
1580         if (!new_plane_state->crtc ||
1581             old_plane_state->crtc != new_plane_state->crtc)
1582                 return -EINVAL;
1583
1584         funcs = plane->helper_private;
1585         if (!funcs->atomic_async_update)
1586                 return -EINVAL;
1587
1588         if (new_plane_state->fence)
1589                 return -EINVAL;
1590
1591         /*
1592          * Don't do an async update if there is an outstanding commit modifying
1593          * the plane.  This prevents our async update's changes from getting
1594          * overridden by a previous synchronous update's state.
1595          */
1596         if (old_plane_state->commit &&
1597             !try_wait_for_completion(&old_plane_state->commit->hw_done))
1598                 return -EBUSY;
1599
1600         return funcs->atomic_async_check(plane, new_plane_state);
1601 }
1602 EXPORT_SYMBOL(drm_atomic_helper_async_check);
1603
1604 /**
1605  * drm_atomic_helper_async_commit - commit state asynchronously
1606  * @dev: DRM device
1607  * @state: the driver state object
1608  *
1609  * This function commits a state asynchronously, i.e., not vblank
1610  * synchronized. It should be used on a state only when
1611  * drm_atomic_async_check() succeeds. Async commits are not supposed to swap
1612  * the states like normal sync commits, but just do in-place changes on the
1613  * current state.
1614  */
1615 void drm_atomic_helper_async_commit(struct drm_device *dev,
1616                                     struct drm_atomic_state *state)
1617 {
1618         struct drm_plane *plane;
1619         struct drm_plane_state *plane_state;
1620         const struct drm_plane_helper_funcs *funcs;
1621         int i;
1622
1623         for_each_new_plane_in_state(state, plane, plane_state, i) {
1624                 funcs = plane->helper_private;
1625                 funcs->atomic_async_update(plane, plane_state);
1626
1627                 /*
1628                  * ->atomic_async_update() is supposed to update the
1629                  * plane->state in-place, make sure at least common
1630                  * properties have been properly updated.
1631                  */
1632                 WARN_ON_ONCE(plane->state->fb != plane_state->fb);
1633                 WARN_ON_ONCE(plane->state->crtc_x != plane_state->crtc_x);
1634                 WARN_ON_ONCE(plane->state->crtc_y != plane_state->crtc_y);
1635                 WARN_ON_ONCE(plane->state->src_x != plane_state->src_x);
1636                 WARN_ON_ONCE(plane->state->src_y != plane_state->src_y);
1637         }
1638 }
1639 EXPORT_SYMBOL(drm_atomic_helper_async_commit);
1640
1641 /**
1642  * drm_atomic_helper_commit - commit validated state object
1643  * @dev: DRM device
1644  * @state: the driver state object
1645  * @nonblock: whether nonblocking behavior is requested.
1646  *
1647  * This function commits a with drm_atomic_helper_check() pre-validated state
1648  * object. This can still fail when e.g. the framebuffer reservation fails. This
1649  * function implements nonblocking commits, using
1650  * drm_atomic_helper_setup_commit() and related functions.
1651  *
1652  * Committing the actual hardware state is done through the
1653  * &drm_mode_config_helper_funcs.atomic_commit_tail callback, or it's default
1654  * implementation drm_atomic_helper_commit_tail().
1655  *
1656  * RETURNS:
1657  * Zero for success or -errno.
1658  */
1659 int drm_atomic_helper_commit(struct drm_device *dev,
1660                              struct drm_atomic_state *state,
1661                              bool nonblock)
1662 {
1663         int ret;
1664
1665         if (state->async_update) {
1666                 ret = drm_atomic_helper_prepare_planes(dev, state);
1667                 if (ret)
1668                         return ret;
1669
1670                 drm_atomic_helper_async_commit(dev, state);
1671                 drm_atomic_helper_cleanup_planes(dev, state);
1672
1673                 return 0;
1674         }
1675
1676         ret = drm_atomic_helper_setup_commit(state, nonblock);
1677         if (ret)
1678                 return ret;
1679
1680         INIT_WORK(&state->commit_work, commit_work);
1681
1682         ret = drm_atomic_helper_prepare_planes(dev, state);
1683         if (ret)
1684                 return ret;
1685
1686         if (!nonblock) {
1687                 ret = drm_atomic_helper_wait_for_fences(dev, state, true);
1688                 if (ret)
1689                         goto err;
1690         }
1691
1692         /*
1693          * This is the point of no return - everything below never fails except
1694          * when the hw goes bonghits. Which means we can commit the new state on
1695          * the software side now.
1696          */
1697
1698         ret = drm_atomic_helper_swap_state(state, true);
1699         if (ret)
1700                 goto err;
1701
1702         /*
1703          * Everything below can be run asynchronously without the need to grab
1704          * any modeset locks at all under one condition: It must be guaranteed
1705          * that the asynchronous work has either been cancelled (if the driver
1706          * supports it, which at least requires that the framebuffers get
1707          * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
1708          * before the new state gets committed on the software side with
1709          * drm_atomic_helper_swap_state().
1710          *
1711          * This scheme allows new atomic state updates to be prepared and
1712          * checked in parallel to the asynchronous completion of the previous
1713          * update. Which is important since compositors need to figure out the
1714          * composition of the next frame right after having submitted the
1715          * current layout.
1716          *
1717          * NOTE: Commit work has multiple phases, first hardware commit, then
1718          * cleanup. We want them to overlap, hence need system_unbound_wq to
1719          * make sure work items don't artifically stall on each another.
1720          */
1721
1722         drm_atomic_state_get(state);
1723         if (nonblock)
1724                 queue_work(system_unbound_wq, &state->commit_work);
1725         else
1726                 commit_tail(state);
1727
1728         return 0;
1729
1730 err:
1731         drm_atomic_helper_cleanup_planes(dev, state);
1732         return ret;
1733 }
1734 EXPORT_SYMBOL(drm_atomic_helper_commit);
1735
1736 /**
1737  * DOC: implementing nonblocking commit
1738  *
1739  * Nonblocking atomic commits have to be implemented in the following sequence:
1740  *
1741  * 1. Run drm_atomic_helper_prepare_planes() first. This is the only function
1742  * which commit needs to call which can fail, so we want to run it first and
1743  * synchronously.
1744  *
1745  * 2. Synchronize with any outstanding nonblocking commit worker threads which
1746  * might be affected the new state update. This can be done by either cancelling
1747  * or flushing the work items, depending upon whether the driver can deal with
1748  * cancelled updates. Note that it is important to ensure that the framebuffer
1749  * cleanup is still done when cancelling.
1750  *
1751  * Asynchronous workers need to have sufficient parallelism to be able to run
1752  * different atomic commits on different CRTCs in parallel. The simplest way to
1753  * achive this is by running them on the &system_unbound_wq work queue. Note
1754  * that drivers are not required to split up atomic commits and run an
1755  * individual commit in parallel - userspace is supposed to do that if it cares.
1756  * But it might be beneficial to do that for modesets, since those necessarily
1757  * must be done as one global operation, and enabling or disabling a CRTC can
1758  * take a long time. But even that is not required.
1759  *
1760  * 3. The software state is updated synchronously with
1761  * drm_atomic_helper_swap_state(). Doing this under the protection of all modeset
1762  * locks means concurrent callers never see inconsistent state. And doing this
1763  * while it's guaranteed that no relevant nonblocking worker runs means that
1764  * nonblocking workers do not need grab any locks. Actually they must not grab
1765  * locks, for otherwise the work flushing will deadlock.
1766  *
1767  * 4. Schedule a work item to do all subsequent steps, using the split-out
1768  * commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and
1769  * then cleaning up the framebuffers after the old framebuffer is no longer
1770  * being displayed.
1771  *
1772  * The above scheme is implemented in the atomic helper libraries in
1773  * drm_atomic_helper_commit() using a bunch of helper functions. See
1774  * drm_atomic_helper_setup_commit() for a starting point.
1775  */
1776
1777 static int stall_checks(struct drm_crtc *crtc, bool nonblock)
1778 {
1779         struct drm_crtc_commit *commit, *stall_commit = NULL;
1780         bool completed = true;
1781         int i;
1782         long ret = 0;
1783
1784         spin_lock(&crtc->commit_lock);
1785         i = 0;
1786         list_for_each_entry(commit, &crtc->commit_list, commit_entry) {
1787                 if (i == 0) {
1788                         completed = try_wait_for_completion(&commit->flip_done);
1789                         /* Userspace is not allowed to get ahead of the previous
1790                          * commit with nonblocking ones. */
1791                         if (!completed && nonblock) {
1792                                 spin_unlock(&crtc->commit_lock);
1793                                 return -EBUSY;
1794                         }
1795                 } else if (i == 1) {
1796                         stall_commit = drm_crtc_commit_get(commit);
1797                         break;
1798                 }
1799
1800                 i++;
1801         }
1802         spin_unlock(&crtc->commit_lock);
1803
1804         if (!stall_commit)
1805                 return 0;
1806
1807         /* We don't want to let commits get ahead of cleanup work too much,
1808          * stalling on 2nd previous commit means triple-buffer won't ever stall.
1809          */
1810         ret = wait_for_completion_interruptible_timeout(&stall_commit->cleanup_done,
1811                                                         10*HZ);
1812         if (ret == 0)
1813                 DRM_ERROR("[CRTC:%d:%s] cleanup_done timed out\n",
1814                           crtc->base.id, crtc->name);
1815
1816         drm_crtc_commit_put(stall_commit);
1817
1818         return ret < 0 ? ret : 0;
1819 }
1820
1821 static void release_crtc_commit(struct completion *completion)
1822 {
1823         struct drm_crtc_commit *commit = container_of(completion,
1824                                                       typeof(*commit),
1825                                                       flip_done);
1826
1827         drm_crtc_commit_put(commit);
1828 }
1829
1830 static void init_commit(struct drm_crtc_commit *commit, struct drm_crtc *crtc)
1831 {
1832         init_completion(&commit->flip_done);
1833         init_completion(&commit->hw_done);
1834         init_completion(&commit->cleanup_done);
1835         INIT_LIST_HEAD(&commit->commit_entry);
1836         kref_init(&commit->ref);
1837         commit->crtc = crtc;
1838 }
1839
1840 static struct drm_crtc_commit *
1841 crtc_or_fake_commit(struct drm_atomic_state *state, struct drm_crtc *crtc)
1842 {
1843         if (crtc) {
1844                 struct drm_crtc_state *new_crtc_state;
1845
1846                 new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
1847
1848                 return new_crtc_state->commit;
1849         }
1850
1851         if (!state->fake_commit) {
1852                 state->fake_commit = kzalloc(sizeof(*state->fake_commit), GFP_KERNEL);
1853                 if (!state->fake_commit)
1854                         return NULL;
1855
1856                 init_commit(state->fake_commit, NULL);
1857         }
1858
1859         return state->fake_commit;
1860 }
1861
1862 /**
1863  * drm_atomic_helper_setup_commit - setup possibly nonblocking commit
1864  * @state: new modeset state to be committed
1865  * @nonblock: whether nonblocking behavior is requested.
1866  *
1867  * This function prepares @state to be used by the atomic helper's support for
1868  * nonblocking commits. Drivers using the nonblocking commit infrastructure
1869  * should always call this function from their
1870  * &drm_mode_config_funcs.atomic_commit hook.
1871  *
1872  * To be able to use this support drivers need to use a few more helper
1873  * functions. drm_atomic_helper_wait_for_dependencies() must be called before
1874  * actually committing the hardware state, and for nonblocking commits this call
1875  * must be placed in the async worker. See also drm_atomic_helper_swap_state()
1876  * and it's stall parameter, for when a driver's commit hooks look at the
1877  * &drm_crtc.state, &drm_plane.state or &drm_connector.state pointer directly.
1878  *
1879  * Completion of the hardware commit step must be signalled using
1880  * drm_atomic_helper_commit_hw_done(). After this step the driver is not allowed
1881  * to read or change any permanent software or hardware modeset state. The only
1882  * exception is state protected by other means than &drm_modeset_lock locks.
1883  * Only the free standing @state with pointers to the old state structures can
1884  * be inspected, e.g. to clean up old buffers using
1885  * drm_atomic_helper_cleanup_planes().
1886  *
1887  * At the very end, before cleaning up @state drivers must call
1888  * drm_atomic_helper_commit_cleanup_done().
1889  *
1890  * This is all implemented by in drm_atomic_helper_commit(), giving drivers a
1891  * complete and easy-to-use default implementation of the atomic_commit() hook.
1892  *
1893  * The tracking of asynchronously executed and still pending commits is done
1894  * using the core structure &drm_crtc_commit.
1895  *
1896  * By default there's no need to clean up resources allocated by this function
1897  * explicitly: drm_atomic_state_default_clear() will take care of that
1898  * automatically.
1899  *
1900  * Returns:
1901  *
1902  * 0 on success. -EBUSY when userspace schedules nonblocking commits too fast,
1903  * -ENOMEM on allocation failures and -EINTR when a signal is pending.
1904  */
1905 int drm_atomic_helper_setup_commit(struct drm_atomic_state *state,
1906                                    bool nonblock)
1907 {
1908         struct drm_crtc *crtc;
1909         struct drm_crtc_state *old_crtc_state, *new_crtc_state;
1910         struct drm_connector *conn;
1911         struct drm_connector_state *old_conn_state, *new_conn_state;
1912         struct drm_plane *plane;
1913         struct drm_plane_state *old_plane_state, *new_plane_state;
1914         struct drm_crtc_commit *commit;
1915         int i, ret;
1916
1917         for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
1918                 commit = kzalloc(sizeof(*commit), GFP_KERNEL);
1919                 if (!commit)
1920                         return -ENOMEM;
1921
1922                 init_commit(commit, crtc);
1923
1924                 new_crtc_state->commit = commit;
1925
1926                 ret = stall_checks(crtc, nonblock);
1927                 if (ret)
1928                         return ret;
1929
1930                 /* Drivers only send out events when at least either current or
1931                  * new CRTC state is active. Complete right away if everything
1932                  * stays off. */
1933                 if (!old_crtc_state->active && !new_crtc_state->active) {
1934                         complete_all(&commit->flip_done);
1935                         continue;
1936                 }
1937
1938                 /* Legacy cursor updates are fully unsynced. */
1939                 if (state->legacy_cursor_update) {
1940                         complete_all(&commit->flip_done);
1941                         continue;
1942                 }
1943
1944                 if (!new_crtc_state->event) {
1945                         commit->event = kzalloc(sizeof(*commit->event),
1946                                                 GFP_KERNEL);
1947                         if (!commit->event)
1948                                 return -ENOMEM;
1949
1950                         new_crtc_state->event = commit->event;
1951                 }
1952
1953                 new_crtc_state->event->base.completion = &commit->flip_done;
1954                 new_crtc_state->event->base.completion_release = release_crtc_commit;
1955                 drm_crtc_commit_get(commit);
1956
1957                 commit->abort_completion = true;
1958
1959                 state->crtcs[i].commit = commit;
1960                 drm_crtc_commit_get(commit);
1961         }
1962
1963         for_each_oldnew_connector_in_state(state, conn, old_conn_state, new_conn_state, i) {
1964                 /* Userspace is not allowed to get ahead of the previous
1965                  * commit with nonblocking ones. */
1966                 if (nonblock && old_conn_state->commit &&
1967                     !try_wait_for_completion(&old_conn_state->commit->flip_done))
1968                         return -EBUSY;
1969
1970                 /* Always track connectors explicitly for e.g. link retraining. */
1971                 commit = crtc_or_fake_commit(state, new_conn_state->crtc ?: old_conn_state->crtc);
1972                 if (!commit)
1973                         return -ENOMEM;
1974
1975                 new_conn_state->commit = drm_crtc_commit_get(commit);
1976         }
1977
1978         for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
1979                 /* Userspace is not allowed to get ahead of the previous
1980                  * commit with nonblocking ones. */
1981                 if (nonblock && old_plane_state->commit &&
1982                     !try_wait_for_completion(&old_plane_state->commit->flip_done))
1983                         return -EBUSY;
1984
1985                 /* Always track planes explicitly for async pageflip support. */
1986                 commit = crtc_or_fake_commit(state, new_plane_state->crtc ?: old_plane_state->crtc);
1987                 if (!commit)
1988                         return -ENOMEM;
1989
1990                 new_plane_state->commit = drm_crtc_commit_get(commit);
1991         }
1992
1993         return 0;
1994 }
1995 EXPORT_SYMBOL(drm_atomic_helper_setup_commit);
1996
1997 /**
1998  * drm_atomic_helper_wait_for_dependencies - wait for required preceeding commits
1999  * @old_state: atomic state object with old state structures
2000  *
2001  * This function waits for all preceeding commits that touch the same CRTC as
2002  * @old_state to both be committed to the hardware (as signalled by
2003  * drm_atomic_helper_commit_hw_done) and executed by the hardware (as signalled
2004  * by calling drm_crtc_send_vblank_event() on the &drm_crtc_state.event).
2005  *
2006  * This is part of the atomic helper support for nonblocking commits, see
2007  * drm_atomic_helper_setup_commit() for an overview.
2008  */
2009 void drm_atomic_helper_wait_for_dependencies(struct drm_atomic_state *old_state)
2010 {
2011         struct drm_crtc *crtc;
2012         struct drm_crtc_state *old_crtc_state;
2013         struct drm_plane *plane;
2014         struct drm_plane_state *old_plane_state;
2015         struct drm_connector *conn;
2016         struct drm_connector_state *old_conn_state;
2017         struct drm_crtc_commit *commit;
2018         int i;
2019         long ret;
2020
2021         for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
2022                 commit = old_crtc_state->commit;
2023
2024                 if (!commit)
2025                         continue;
2026
2027                 ret = wait_for_completion_timeout(&commit->hw_done,
2028                                                   10*HZ);
2029                 if (ret == 0)
2030                         DRM_ERROR("[CRTC:%d:%s] hw_done timed out\n",
2031                                   crtc->base.id, crtc->name);
2032
2033                 /* Currently no support for overwriting flips, hence
2034                  * stall for previous one to execute completely. */
2035                 ret = wait_for_completion_timeout(&commit->flip_done,
2036                                                   10*HZ);
2037                 if (ret == 0)
2038                         DRM_ERROR("[CRTC:%d:%s] flip_done timed out\n",
2039                                   crtc->base.id, crtc->name);
2040         }
2041
2042         for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
2043                 commit = old_conn_state->commit;
2044
2045                 if (!commit)
2046                         continue;
2047
2048                 ret = wait_for_completion_timeout(&commit->hw_done,
2049                                                   10*HZ);
2050                 if (ret == 0)
2051                         DRM_ERROR("[CONNECTOR:%d:%s] hw_done timed out\n",
2052                                   conn->base.id, conn->name);
2053
2054                 /* Currently no support for overwriting flips, hence
2055                  * stall for previous one to execute completely. */
2056                 ret = wait_for_completion_timeout(&commit->flip_done,
2057                                                   10*HZ);
2058                 if (ret == 0)
2059                         DRM_ERROR("[CONNECTOR:%d:%s] flip_done timed out\n",
2060                                   conn->base.id, conn->name);
2061         }
2062
2063         for_each_old_plane_in_state(old_state, plane, old_plane_state, i) {
2064                 commit = old_plane_state->commit;
2065
2066                 if (!commit)
2067                         continue;
2068
2069                 ret = wait_for_completion_timeout(&commit->hw_done,
2070                                                   10*HZ);
2071                 if (ret == 0)
2072                         DRM_ERROR("[PLANE:%d:%s] hw_done timed out\n",
2073                                   plane->base.id, plane->name);
2074
2075                 /* Currently no support for overwriting flips, hence
2076                  * stall for previous one to execute completely. */
2077                 ret = wait_for_completion_timeout(&commit->flip_done,
2078                                                   10*HZ);
2079                 if (ret == 0)
2080                         DRM_ERROR("[PLANE:%d:%s] flip_done timed out\n",
2081                                   plane->base.id, plane->name);
2082         }
2083 }
2084 EXPORT_SYMBOL(drm_atomic_helper_wait_for_dependencies);
2085
2086 /**
2087  * drm_atomic_helper_fake_vblank - fake VBLANK events if needed
2088  * @old_state: atomic state object with old state structures
2089  *
2090  * This function walks all CRTCs and fake VBLANK events on those with
2091  * &drm_crtc_state.no_vblank set to true and &drm_crtc_state.event != NULL.
2092  * The primary use of this function is writeback connectors working in oneshot
2093  * mode and faking VBLANK events. In this case they only fake the VBLANK event
2094  * when a job is queued, and any change to the pipeline that does not touch the
2095  * connector is leading to timeouts when calling
2096  * drm_atomic_helper_wait_for_vblanks() or
2097  * drm_atomic_helper_wait_for_flip_done().
2098  *
2099  * This is part of the atomic helper support for nonblocking commits, see
2100  * drm_atomic_helper_setup_commit() for an overview.
2101  */
2102 void drm_atomic_helper_fake_vblank(struct drm_atomic_state *old_state)
2103 {
2104         struct drm_crtc_state *new_crtc_state;
2105         struct drm_crtc *crtc;
2106         int i;
2107
2108         for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
2109                 unsigned long flags;
2110
2111                 if (!new_crtc_state->no_vblank)
2112                         continue;
2113
2114                 spin_lock_irqsave(&old_state->dev->event_lock, flags);
2115                 if (new_crtc_state->event) {
2116                         drm_crtc_send_vblank_event(crtc,
2117                                                    new_crtc_state->event);
2118                         new_crtc_state->event = NULL;
2119                 }
2120                 spin_unlock_irqrestore(&old_state->dev->event_lock, flags);
2121         }
2122 }
2123 EXPORT_SYMBOL(drm_atomic_helper_fake_vblank);
2124
2125 /**
2126  * drm_atomic_helper_commit_hw_done - setup possible nonblocking commit
2127  * @old_state: atomic state object with old state structures
2128  *
2129  * This function is used to signal completion of the hardware commit step. After
2130  * this step the driver is not allowed to read or change any permanent software
2131  * or hardware modeset state. The only exception is state protected by other
2132  * means than &drm_modeset_lock locks.
2133  *
2134  * Drivers should try to postpone any expensive or delayed cleanup work after
2135  * this function is called.
2136  *
2137  * This is part of the atomic helper support for nonblocking commits, see
2138  * drm_atomic_helper_setup_commit() for an overview.
2139  */
2140 void drm_atomic_helper_commit_hw_done(struct drm_atomic_state *old_state)
2141 {
2142         struct drm_crtc *crtc;
2143         struct drm_crtc_state *old_crtc_state, *new_crtc_state;
2144         struct drm_crtc_commit *commit;
2145         int i;
2146
2147         for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
2148                 commit = new_crtc_state->commit;
2149                 if (!commit)
2150                         continue;
2151
2152                 /*
2153                  * copy new_crtc_state->commit to old_crtc_state->commit,
2154                  * it's unsafe to touch new_crtc_state after hw_done,
2155                  * but we still need to do so in cleanup_done().
2156                  */
2157                 if (old_crtc_state->commit)
2158                         drm_crtc_commit_put(old_crtc_state->commit);
2159
2160                 old_crtc_state->commit = drm_crtc_commit_get(commit);
2161
2162                 /* backend must have consumed any event by now */
2163                 WARN_ON(new_crtc_state->event);
2164                 complete_all(&commit->hw_done);
2165         }
2166
2167         if (old_state->fake_commit) {
2168                 complete_all(&old_state->fake_commit->hw_done);
2169                 complete_all(&old_state->fake_commit->flip_done);
2170         }
2171 }
2172 EXPORT_SYMBOL(drm_atomic_helper_commit_hw_done);
2173
2174 /**
2175  * drm_atomic_helper_commit_cleanup_done - signal completion of commit
2176  * @old_state: atomic state object with old state structures
2177  *
2178  * This signals completion of the atomic update @old_state, including any
2179  * cleanup work. If used, it must be called right before calling
2180  * drm_atomic_state_put().
2181  *
2182  * This is part of the atomic helper support for nonblocking commits, see
2183  * drm_atomic_helper_setup_commit() for an overview.
2184  */
2185 void drm_atomic_helper_commit_cleanup_done(struct drm_atomic_state *old_state)
2186 {
2187         struct drm_crtc *crtc;
2188         struct drm_crtc_state *old_crtc_state;
2189         struct drm_crtc_commit *commit;
2190         int i;
2191
2192         for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
2193                 commit = old_crtc_state->commit;
2194                 if (WARN_ON(!commit))
2195                         continue;
2196
2197                 complete_all(&commit->cleanup_done);
2198                 WARN_ON(!try_wait_for_completion(&commit->hw_done));
2199
2200                 spin_lock(&crtc->commit_lock);
2201                 list_del(&commit->commit_entry);
2202                 spin_unlock(&crtc->commit_lock);
2203         }
2204
2205         if (old_state->fake_commit)
2206                 complete_all(&old_state->fake_commit->cleanup_done);
2207 }
2208 EXPORT_SYMBOL(drm_atomic_helper_commit_cleanup_done);
2209
2210 /**
2211  * drm_atomic_helper_prepare_planes - prepare plane resources before commit
2212  * @dev: DRM device
2213  * @state: atomic state object with new state structures
2214  *
2215  * This function prepares plane state, specifically framebuffers, for the new
2216  * configuration, by calling &drm_plane_helper_funcs.prepare_fb. If any failure
2217  * is encountered this function will call &drm_plane_helper_funcs.cleanup_fb on
2218  * any already successfully prepared framebuffer.
2219  *
2220  * Returns:
2221  * 0 on success, negative error code on failure.
2222  */
2223 int drm_atomic_helper_prepare_planes(struct drm_device *dev,
2224                                      struct drm_atomic_state *state)
2225 {
2226         struct drm_plane *plane;
2227         struct drm_plane_state *new_plane_state;
2228         int ret, i, j;
2229
2230         for_each_new_plane_in_state(state, plane, new_plane_state, i) {
2231                 const struct drm_plane_helper_funcs *funcs;
2232
2233                 funcs = plane->helper_private;
2234
2235                 if (funcs->prepare_fb) {
2236                         ret = funcs->prepare_fb(plane, new_plane_state);
2237                         if (ret)
2238                                 goto fail;
2239                 }
2240         }
2241
2242         return 0;
2243
2244 fail:
2245         for_each_new_plane_in_state(state, plane, new_plane_state, j) {
2246                 const struct drm_plane_helper_funcs *funcs;
2247
2248                 if (j >= i)
2249                         continue;
2250
2251                 funcs = plane->helper_private;
2252
2253                 if (funcs->cleanup_fb)
2254                         funcs->cleanup_fb(plane, new_plane_state);
2255         }
2256
2257         return ret;
2258 }
2259 EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
2260
2261 static bool plane_crtc_active(const struct drm_plane_state *state)
2262 {
2263         return state->crtc && state->crtc->state->active;
2264 }
2265
2266 /**
2267  * drm_atomic_helper_commit_planes - commit plane state
2268  * @dev: DRM device
2269  * @old_state: atomic state object with old state structures
2270  * @flags: flags for committing plane state
2271  *
2272  * This function commits the new plane state using the plane and atomic helper
2273  * functions for planes and crtcs. It assumes that the atomic state has already
2274  * been pushed into the relevant object state pointers, since this step can no
2275  * longer fail.
2276  *
2277  * It still requires the global state object @old_state to know which planes and
2278  * crtcs need to be updated though.
2279  *
2280  * Note that this function does all plane updates across all CRTCs in one step.
2281  * If the hardware can't support this approach look at
2282  * drm_atomic_helper_commit_planes_on_crtc() instead.
2283  *
2284  * Plane parameters can be updated by applications while the associated CRTC is
2285  * disabled. The DRM/KMS core will store the parameters in the plane state,
2286  * which will be available to the driver when the CRTC is turned on. As a result
2287  * most drivers don't need to be immediately notified of plane updates for a
2288  * disabled CRTC.
2289  *
2290  * Unless otherwise needed, drivers are advised to set the ACTIVE_ONLY flag in
2291  * @flags in order not to receive plane update notifications related to a
2292  * disabled CRTC. This avoids the need to manually ignore plane updates in
2293  * driver code when the driver and/or hardware can't or just don't need to deal
2294  * with updates on disabled CRTCs, for example when supporting runtime PM.
2295  *
2296  * Drivers may set the NO_DISABLE_AFTER_MODESET flag in @flags if the relevant
2297  * display controllers require to disable a CRTC's planes when the CRTC is
2298  * disabled. This function would skip the &drm_plane_helper_funcs.atomic_disable
2299  * call for a plane if the CRTC of the old plane state needs a modesetting
2300  * operation. Of course, the drivers need to disable the planes in their CRTC
2301  * disable callbacks since no one else would do that.
2302  *
2303  * The drm_atomic_helper_commit() default implementation doesn't set the
2304  * ACTIVE_ONLY flag to most closely match the behaviour of the legacy helpers.
2305  * This should not be copied blindly by drivers.
2306  */
2307 void drm_atomic_helper_commit_planes(struct drm_device *dev,
2308                                      struct drm_atomic_state *old_state,
2309                                      uint32_t flags)
2310 {
2311         struct drm_crtc *crtc;
2312         struct drm_crtc_state *old_crtc_state, *new_crtc_state;
2313         struct drm_plane *plane;
2314         struct drm_plane_state *old_plane_state, *new_plane_state;
2315         int i;
2316         bool active_only = flags & DRM_PLANE_COMMIT_ACTIVE_ONLY;
2317         bool no_disable = flags & DRM_PLANE_COMMIT_NO_DISABLE_AFTER_MODESET;
2318
2319         for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
2320                 const struct drm_crtc_helper_funcs *funcs;
2321
2322                 funcs = crtc->helper_private;
2323
2324                 if (!funcs || !funcs->atomic_begin)
2325                         continue;
2326
2327                 if (active_only && !new_crtc_state->active)
2328                         continue;
2329
2330                 funcs->atomic_begin(crtc, old_crtc_state);
2331         }
2332
2333         for_each_oldnew_plane_in_state(old_state, plane, old_plane_state, new_plane_state, i) {
2334                 const struct drm_plane_helper_funcs *funcs;
2335                 bool disabling;
2336
2337                 funcs = plane->helper_private;
2338
2339                 if (!funcs)
2340                         continue;
2341
2342                 disabling = drm_atomic_plane_disabling(old_plane_state,
2343                                                        new_plane_state);
2344
2345                 if (active_only) {
2346                         /*
2347                          * Skip planes related to inactive CRTCs. If the plane
2348                          * is enabled use the state of the current CRTC. If the
2349                          * plane is being disabled use the state of the old
2350                          * CRTC to avoid skipping planes being disabled on an
2351                          * active CRTC.
2352                          */
2353                         if (!disabling && !plane_crtc_active(new_plane_state))
2354                                 continue;
2355                         if (disabling && !plane_crtc_active(old_plane_state))
2356                                 continue;
2357                 }
2358
2359                 /*
2360                  * Special-case disabling the plane if drivers support it.
2361                  */
2362                 if (disabling && funcs->atomic_disable) {
2363                         struct drm_crtc_state *crtc_state;
2364
2365                         crtc_state = old_plane_state->crtc->state;
2366
2367                         if (drm_atomic_crtc_needs_modeset(crtc_state) &&
2368                             no_disable)
2369                                 continue;
2370
2371                         funcs->atomic_disable(plane, old_plane_state);
2372                 } else if (new_plane_state->crtc || disabling) {
2373                         funcs->atomic_update(plane, old_plane_state);
2374                 }
2375         }
2376
2377         for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
2378                 const struct drm_crtc_helper_funcs *funcs;
2379
2380                 funcs = crtc->helper_private;
2381
2382                 if (!funcs || !funcs->atomic_flush)
2383                         continue;
2384
2385                 if (active_only && !new_crtc_state->active)
2386                         continue;
2387
2388                 funcs->atomic_flush(crtc, old_crtc_state);
2389         }
2390 }
2391 EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
2392
2393 /**
2394  * drm_atomic_helper_commit_planes_on_crtc - commit plane state for a crtc
2395  * @old_crtc_state: atomic state object with the old crtc state
2396  *
2397  * This function commits the new plane state using the plane and atomic helper
2398  * functions for planes on the specific crtc. It assumes that the atomic state
2399  * has already been pushed into the relevant object state pointers, since this
2400  * step can no longer fail.
2401  *
2402  * This function is useful when plane updates should be done crtc-by-crtc
2403  * instead of one global step like drm_atomic_helper_commit_planes() does.
2404  *
2405  * This function can only be savely used when planes are not allowed to move
2406  * between different CRTCs because this function doesn't handle inter-CRTC
2407  * depencies. Callers need to ensure that either no such depencies exist,
2408  * resolve them through ordering of commit calls or through some other means.
2409  */
2410 void
2411 drm_atomic_helper_commit_planes_on_crtc(struct drm_crtc_state *old_crtc_state)
2412 {
2413         const struct drm_crtc_helper_funcs *crtc_funcs;
2414         struct drm_crtc *crtc = old_crtc_state->crtc;
2415         struct drm_atomic_state *old_state = old_crtc_state->state;
2416         struct drm_crtc_state *new_crtc_state =
2417                 drm_atomic_get_new_crtc_state(old_state, crtc);
2418         struct drm_plane *plane;
2419         unsigned plane_mask;
2420
2421         plane_mask = old_crtc_state->plane_mask;
2422         plane_mask |= new_crtc_state->plane_mask;
2423
2424         crtc_funcs = crtc->helper_private;
2425         if (crtc_funcs && crtc_funcs->atomic_begin)
2426                 crtc_funcs->atomic_begin(crtc, old_crtc_state);
2427
2428         drm_for_each_plane_mask(plane, crtc->dev, plane_mask) {
2429                 struct drm_plane_state *old_plane_state =
2430                         drm_atomic_get_old_plane_state(old_state, plane);
2431                 struct drm_plane_state *new_plane_state =
2432                         drm_atomic_get_new_plane_state(old_state, plane);
2433                 const struct drm_plane_helper_funcs *plane_funcs;
2434
2435                 plane_funcs = plane->helper_private;
2436
2437                 if (!old_plane_state || !plane_funcs)
2438                         continue;
2439
2440                 WARN_ON(new_plane_state->crtc &&
2441                         new_plane_state->crtc != crtc);
2442
2443                 if (drm_atomic_plane_disabling(old_plane_state, new_plane_state) &&
2444                     plane_funcs->atomic_disable)
2445                         plane_funcs->atomic_disable(plane, old_plane_state);
2446                 else if (new_plane_state->crtc ||
2447                          drm_atomic_plane_disabling(old_plane_state, new_plane_state))
2448                         plane_funcs->atomic_update(plane, old_plane_state);
2449         }
2450
2451         if (crtc_funcs && crtc_funcs->atomic_flush)
2452                 crtc_funcs->atomic_flush(crtc, old_crtc_state);
2453 }
2454 EXPORT_SYMBOL(drm_atomic_helper_commit_planes_on_crtc);
2455
2456 /**
2457  * drm_atomic_helper_disable_planes_on_crtc - helper to disable CRTC's planes
2458  * @old_crtc_state: atomic state object with the old CRTC state
2459  * @atomic: if set, synchronize with CRTC's atomic_begin/flush hooks
2460  *
2461  * Disables all planes associated with the given CRTC. This can be
2462  * used for instance in the CRTC helper atomic_disable callback to disable
2463  * all planes.
2464  *
2465  * If the atomic-parameter is set the function calls the CRTC's
2466  * atomic_begin hook before and atomic_flush hook after disabling the
2467  * planes.
2468  *
2469  * It is a bug to call this function without having implemented the
2470  * &drm_plane_helper_funcs.atomic_disable plane hook.
2471  */
2472 void
2473 drm_atomic_helper_disable_planes_on_crtc(struct drm_crtc_state *old_crtc_state,
2474                                          bool atomic)
2475 {
2476         struct drm_crtc *crtc = old_crtc_state->crtc;
2477         const struct drm_crtc_helper_funcs *crtc_funcs =
2478                 crtc->helper_private;
2479         struct drm_plane *plane;
2480
2481         if (atomic && crtc_funcs && crtc_funcs->atomic_begin)
2482                 crtc_funcs->atomic_begin(crtc, NULL);
2483
2484         drm_atomic_crtc_state_for_each_plane(plane, old_crtc_state) {
2485                 const struct drm_plane_helper_funcs *plane_funcs =
2486                         plane->helper_private;
2487
2488                 if (!plane_funcs)
2489                         continue;
2490
2491                 WARN_ON(!plane_funcs->atomic_disable);
2492                 if (plane_funcs->atomic_disable)
2493                         plane_funcs->atomic_disable(plane, NULL);
2494         }
2495
2496         if (atomic && crtc_funcs && crtc_funcs->atomic_flush)
2497                 crtc_funcs->atomic_flush(crtc, NULL);
2498 }
2499 EXPORT_SYMBOL(drm_atomic_helper_disable_planes_on_crtc);
2500
2501 /**
2502  * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
2503  * @dev: DRM device
2504  * @old_state: atomic state object with old state structures
2505  *
2506  * This function cleans up plane state, specifically framebuffers, from the old
2507  * configuration. Hence the old configuration must be perserved in @old_state to
2508  * be able to call this function.
2509  *
2510  * This function must also be called on the new state when the atomic update
2511  * fails at any point after calling drm_atomic_helper_prepare_planes().
2512  */
2513 void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
2514                                       struct drm_atomic_state *old_state)
2515 {
2516         struct drm_plane *plane;
2517         struct drm_plane_state *old_plane_state, *new_plane_state;
2518         int i;
2519
2520         for_each_oldnew_plane_in_state(old_state, plane, old_plane_state, new_plane_state, i) {
2521                 const struct drm_plane_helper_funcs *funcs;
2522                 struct drm_plane_state *plane_state;
2523
2524                 /*
2525                  * This might be called before swapping when commit is aborted,
2526                  * in which case we have to cleanup the new state.
2527                  */
2528                 if (old_plane_state == plane->state)
2529                         plane_state = new_plane_state;
2530                 else
2531                         plane_state = old_plane_state;
2532
2533                 funcs = plane->helper_private;
2534
2535                 if (funcs->cleanup_fb)
2536                         funcs->cleanup_fb(plane, plane_state);
2537         }
2538 }
2539 EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
2540
2541 /**
2542  * drm_atomic_helper_swap_state - store atomic state into current sw state
2543  * @state: atomic state
2544  * @stall: stall for preceeding commits
2545  *
2546  * This function stores the atomic state into the current state pointers in all
2547  * driver objects. It should be called after all failing steps have been done
2548  * and succeeded, but before the actual hardware state is committed.
2549  *
2550  * For cleanup and error recovery the current state for all changed objects will
2551  * be swapped into @state.
2552  *
2553  * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
2554  *
2555  * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
2556  *
2557  * 2. Do any other steps that might fail.
2558  *
2559  * 3. Put the staged state into the current state pointers with this function.
2560  *
2561  * 4. Actually commit the hardware state.
2562  *
2563  * 5. Call drm_atomic_helper_cleanup_planes() with @state, which since step 3
2564  * contains the old state. Also do any other cleanup required with that state.
2565  *
2566  * @stall must be set when nonblocking commits for this driver directly access
2567  * the &drm_plane.state, &drm_crtc.state or &drm_connector.state pointer. With
2568  * the current atomic helpers this is almost always the case, since the helpers
2569  * don't pass the right state structures to the callbacks.
2570  *
2571  * Returns:
2572  *
2573  * Returns 0 on success. Can return -ERESTARTSYS when @stall is true and the
2574  * waiting for the previous commits has been interrupted.
2575  */
2576 int drm_atomic_helper_swap_state(struct drm_atomic_state *state,
2577                                   bool stall)
2578 {
2579         int i, ret;
2580         struct drm_connector *connector;
2581         struct drm_connector_state *old_conn_state, *new_conn_state;
2582         struct drm_crtc *crtc;
2583         struct drm_crtc_state *old_crtc_state, *new_crtc_state;
2584         struct drm_plane *plane;
2585         struct drm_plane_state *old_plane_state, *new_plane_state;
2586         struct drm_crtc_commit *commit;
2587         struct drm_private_obj *obj;
2588         struct drm_private_state *old_obj_state, *new_obj_state;
2589
2590         if (stall) {
2591                 /*
2592                  * We have to stall for hw_done here before
2593                  * drm_atomic_helper_wait_for_dependencies() because flip
2594                  * depth > 1 is not yet supported by all drivers. As long as
2595                  * obj->state is directly dereferenced anywhere in the drivers
2596                  * atomic_commit_tail function, then it's unsafe to swap state
2597                  * before drm_atomic_helper_commit_hw_done() is called.
2598                  */
2599
2600                 for_each_old_crtc_in_state(state, crtc, old_crtc_state, i) {
2601                         commit = old_crtc_state->commit;
2602
2603                         if (!commit)
2604                                 continue;
2605
2606                         ret = wait_for_completion_interruptible(&commit->hw_done);
2607                         if (ret)
2608                                 return ret;
2609                 }
2610
2611                 for_each_old_connector_in_state(state, connector, old_conn_state, i) {
2612                         commit = old_conn_state->commit;
2613
2614                         if (!commit)
2615                                 continue;
2616
2617                         ret = wait_for_completion_interruptible(&commit->hw_done);
2618                         if (ret)
2619                                 return ret;
2620                 }
2621
2622                 for_each_old_plane_in_state(state, plane, old_plane_state, i) {
2623                         commit = old_plane_state->commit;
2624
2625                         if (!commit)
2626                                 continue;
2627
2628                         ret = wait_for_completion_interruptible(&commit->hw_done);
2629                         if (ret)
2630                                 return ret;
2631                 }
2632         }
2633
2634         for_each_oldnew_connector_in_state(state, connector, old_conn_state, new_conn_state, i) {
2635                 WARN_ON(connector->state != old_conn_state);
2636
2637                 old_conn_state->state = state;
2638                 new_conn_state->state = NULL;
2639
2640                 state->connectors[i].state = old_conn_state;
2641                 connector->state = new_conn_state;
2642         }
2643
2644         for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
2645                 WARN_ON(crtc->state != old_crtc_state);
2646
2647                 old_crtc_state->state = state;
2648                 new_crtc_state->state = NULL;
2649
2650                 state->crtcs[i].state = old_crtc_state;
2651                 crtc->state = new_crtc_state;
2652
2653                 if (new_crtc_state->commit) {
2654                         spin_lock(&crtc->commit_lock);
2655                         list_add(&new_crtc_state->commit->commit_entry,
2656                                  &crtc->commit_list);
2657                         spin_unlock(&crtc->commit_lock);
2658
2659                         new_crtc_state->commit->event = NULL;
2660                 }
2661         }
2662
2663         for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
2664                 WARN_ON(plane->state != old_plane_state);
2665
2666                 old_plane_state->state = state;
2667                 new_plane_state->state = NULL;
2668
2669                 state->planes[i].state = old_plane_state;
2670                 plane->state = new_plane_state;
2671         }
2672
2673         for_each_oldnew_private_obj_in_state(state, obj, old_obj_state, new_obj_state, i) {
2674                 WARN_ON(obj->state != old_obj_state);
2675
2676                 old_obj_state->state = state;
2677                 new_obj_state->state = NULL;
2678
2679                 state->private_objs[i].state = old_obj_state;
2680                 obj->state = new_obj_state;
2681         }
2682
2683         return 0;
2684 }
2685 EXPORT_SYMBOL(drm_atomic_helper_swap_state);
2686
2687 /**
2688  * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
2689  * @plane: plane object to update
2690  * @crtc: owning CRTC of owning plane
2691  * @fb: framebuffer to flip onto plane
2692  * @crtc_x: x offset of primary plane on crtc
2693  * @crtc_y: y offset of primary plane on crtc
2694  * @crtc_w: width of primary plane rectangle on crtc
2695  * @crtc_h: height of primary plane rectangle on crtc
2696  * @src_x: x offset of @fb for panning
2697  * @src_y: y offset of @fb for panning
2698  * @src_w: width of source rectangle in @fb
2699  * @src_h: height of source rectangle in @fb
2700  * @ctx: lock acquire context
2701  *
2702  * Provides a default plane update handler using the atomic driver interface.
2703  *
2704  * RETURNS:
2705  * Zero on success, error code on failure
2706  */
2707 int drm_atomic_helper_update_plane(struct drm_plane *plane,
2708                                    struct drm_crtc *crtc,
2709                                    struct drm_framebuffer *fb,
2710                                    int crtc_x, int crtc_y,
2711                                    unsigned int crtc_w, unsigned int crtc_h,
2712                                    uint32_t src_x, uint32_t src_y,
2713                                    uint32_t src_w, uint32_t src_h,
2714                                    struct drm_modeset_acquire_ctx *ctx)
2715 {
2716         struct drm_atomic_state *state;
2717         struct drm_plane_state *plane_state;
2718         int ret = 0;
2719
2720         state = drm_atomic_state_alloc(plane->dev);
2721         if (!state)
2722                 return -ENOMEM;
2723
2724         state->acquire_ctx = ctx;
2725         plane_state = drm_atomic_get_plane_state(state, plane);
2726         if (IS_ERR(plane_state)) {
2727                 ret = PTR_ERR(plane_state);
2728                 goto fail;
2729         }
2730
2731         ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
2732         if (ret != 0)
2733                 goto fail;
2734         drm_atomic_set_fb_for_plane(plane_state, fb);
2735         plane_state->crtc_x = crtc_x;
2736         plane_state->crtc_y = crtc_y;
2737         plane_state->crtc_w = crtc_w;
2738         plane_state->crtc_h = crtc_h;
2739         plane_state->src_x = src_x;
2740         plane_state->src_y = src_y;
2741         plane_state->src_w = src_w;
2742         plane_state->src_h = src_h;
2743
2744         if (plane == crtc->cursor)
2745                 state->legacy_cursor_update = true;
2746
2747         ret = drm_atomic_commit(state);
2748 fail:
2749         drm_atomic_state_put(state);
2750         return ret;
2751 }
2752 EXPORT_SYMBOL(drm_atomic_helper_update_plane);
2753
2754 /**
2755  * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
2756  * @plane: plane to disable
2757  * @ctx: lock acquire context
2758  *
2759  * Provides a default plane disable handler using the atomic driver interface.
2760  *
2761  * RETURNS:
2762  * Zero on success, error code on failure
2763  */
2764 int drm_atomic_helper_disable_plane(struct drm_plane *plane,
2765                                     struct drm_modeset_acquire_ctx *ctx)
2766 {
2767         struct drm_atomic_state *state;
2768         struct drm_plane_state *plane_state;
2769         int ret = 0;
2770
2771         state = drm_atomic_state_alloc(plane->dev);
2772         if (!state)
2773                 return -ENOMEM;
2774
2775         state->acquire_ctx = ctx;
2776         plane_state = drm_atomic_get_plane_state(state, plane);
2777         if (IS_ERR(plane_state)) {
2778                 ret = PTR_ERR(plane_state);
2779                 goto fail;
2780         }
2781
2782         if (plane_state->crtc && plane_state->crtc->cursor == plane)
2783                 plane_state->state->legacy_cursor_update = true;
2784
2785         ret = __drm_atomic_helper_disable_plane(plane, plane_state);
2786         if (ret != 0)
2787                 goto fail;
2788
2789         ret = drm_atomic_commit(state);
2790 fail:
2791         drm_atomic_state_put(state);
2792         return ret;
2793 }
2794 EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
2795
2796 /* just used from fb-helper and atomic-helper: */
2797 int __drm_atomic_helper_disable_plane(struct drm_plane *plane,
2798                 struct drm_plane_state *plane_state)
2799 {
2800         int ret;
2801
2802         ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
2803         if (ret != 0)
2804                 return ret;
2805
2806         drm_atomic_set_fb_for_plane(plane_state, NULL);
2807         plane_state->crtc_x = 0;
2808         plane_state->crtc_y = 0;
2809         plane_state->crtc_w = 0;
2810         plane_state->crtc_h = 0;
2811         plane_state->src_x = 0;
2812         plane_state->src_y = 0;
2813         plane_state->src_w = 0;
2814         plane_state->src_h = 0;
2815
2816         return 0;
2817 }
2818
2819 static int update_output_state(struct drm_atomic_state *state,
2820                                struct drm_mode_set *set)
2821 {
2822         struct drm_device *dev = set->crtc->dev;
2823         struct drm_crtc *crtc;
2824         struct drm_crtc_state *new_crtc_state;
2825         struct drm_connector *connector;
2826         struct drm_connector_state *new_conn_state;
2827         int ret, i;
2828
2829         ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
2830                                state->acquire_ctx);
2831         if (ret)
2832                 return ret;
2833
2834         /* First disable all connectors on the target crtc. */
2835         ret = drm_atomic_add_affected_connectors(state, set->crtc);
2836         if (ret)
2837                 return ret;
2838
2839         for_each_new_connector_in_state(state, connector, new_conn_state, i) {
2840                 if (new_conn_state->crtc == set->crtc) {
2841                         ret = drm_atomic_set_crtc_for_connector(new_conn_state,
2842                                                                 NULL);
2843                         if (ret)
2844                                 return ret;
2845
2846                         /* Make sure legacy setCrtc always re-trains */
2847                         new_conn_state->link_status = DRM_LINK_STATUS_GOOD;
2848                 }
2849         }
2850
2851         /* Then set all connectors from set->connectors on the target crtc */
2852         for (i = 0; i < set->num_connectors; i++) {
2853                 new_conn_state = drm_atomic_get_connector_state(state,
2854                                                             set->connectors[i]);
2855                 if (IS_ERR(new_conn_state))
2856                         return PTR_ERR(new_conn_state);
2857
2858                 ret = drm_atomic_set_crtc_for_connector(new_conn_state,
2859                                                         set->crtc);
2860                 if (ret)
2861                         return ret;
2862         }
2863
2864         for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
2865                 /* Don't update ->enable for the CRTC in the set_config request,
2866                  * since a mismatch would indicate a bug in the upper layers.
2867                  * The actual modeset code later on will catch any
2868                  * inconsistencies here. */
2869                 if (crtc == set->crtc)
2870                         continue;
2871
2872                 if (!new_crtc_state->connector_mask) {
2873                         ret = drm_atomic_set_mode_prop_for_crtc(new_crtc_state,
2874                                                                 NULL);
2875                         if (ret < 0)
2876                                 return ret;
2877
2878                         new_crtc_state->active = false;
2879                 }
2880         }
2881
2882         return 0;
2883 }
2884
2885 /**
2886  * drm_atomic_helper_set_config - set a new config from userspace
2887  * @set: mode set configuration
2888  * @ctx: lock acquisition context
2889  *
2890  * Provides a default crtc set_config handler using the atomic driver interface.
2891  *
2892  * NOTE: For backwards compatibility with old userspace this automatically
2893  * resets the "link-status" property to GOOD, to force any link
2894  * re-training. The SETCRTC ioctl does not define whether an update does
2895  * need a full modeset or just a plane update, hence we're allowed to do
2896  * that. See also drm_connector_set_link_status_property().
2897  *
2898  * Returns:
2899  * Returns 0 on success, negative errno numbers on failure.
2900  */
2901 int drm_atomic_helper_set_config(struct drm_mode_set *set,
2902                                  struct drm_modeset_acquire_ctx *ctx)
2903 {
2904         struct drm_atomic_state *state;
2905         struct drm_crtc *crtc = set->crtc;
2906         int ret = 0;
2907
2908         state = drm_atomic_state_alloc(crtc->dev);
2909         if (!state)
2910                 return -ENOMEM;
2911
2912         state->acquire_ctx = ctx;
2913         ret = __drm_atomic_helper_set_config(set, state);
2914         if (ret != 0)
2915                 goto fail;
2916
2917         ret = handle_conflicting_encoders(state, true);
2918         if (ret)
2919                 return ret;
2920
2921         ret = drm_atomic_commit(state);
2922
2923 fail:
2924         drm_atomic_state_put(state);
2925         return ret;
2926 }
2927 EXPORT_SYMBOL(drm_atomic_helper_set_config);
2928
2929 /* just used from fb-helper and atomic-helper: */
2930 int __drm_atomic_helper_set_config(struct drm_mode_set *set,
2931                 struct drm_atomic_state *state)
2932 {
2933         struct drm_crtc_state *crtc_state;
2934         struct drm_plane_state *primary_state;
2935         struct drm_crtc *crtc = set->crtc;
2936         int hdisplay, vdisplay;
2937         int ret;
2938
2939         crtc_state = drm_atomic_get_crtc_state(state, crtc);
2940         if (IS_ERR(crtc_state))
2941                 return PTR_ERR(crtc_state);
2942
2943         primary_state = drm_atomic_get_plane_state(state, crtc->primary);
2944         if (IS_ERR(primary_state))
2945                 return PTR_ERR(primary_state);
2946
2947         if (!set->mode) {
2948                 WARN_ON(set->fb);
2949                 WARN_ON(set->num_connectors);
2950
2951                 ret = drm_atomic_set_mode_for_crtc(crtc_state, NULL);
2952                 if (ret != 0)
2953                         return ret;
2954
2955                 crtc_state->active = false;
2956
2957                 ret = drm_atomic_set_crtc_for_plane(primary_state, NULL);
2958                 if (ret != 0)
2959                         return ret;
2960
2961                 drm_atomic_set_fb_for_plane(primary_state, NULL);
2962
2963                 goto commit;
2964         }
2965
2966         WARN_ON(!set->fb);
2967         WARN_ON(!set->num_connectors);
2968
2969         ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
2970         if (ret != 0)
2971                 return ret;
2972
2973         crtc_state->active = true;
2974
2975         ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
2976         if (ret != 0)
2977                 return ret;
2978
2979         drm_mode_get_hv_timing(set->mode, &hdisplay, &vdisplay);
2980
2981         drm_atomic_set_fb_for_plane(primary_state, set->fb);
2982         primary_state->crtc_x = 0;
2983         primary_state->crtc_y = 0;
2984         primary_state->crtc_w = hdisplay;
2985         primary_state->crtc_h = vdisplay;
2986         primary_state->src_x = set->x << 16;
2987         primary_state->src_y = set->y << 16;
2988         if (drm_rotation_90_or_270(primary_state->rotation)) {
2989                 primary_state->src_w = vdisplay << 16;
2990                 primary_state->src_h = hdisplay << 16;
2991         } else {
2992                 primary_state->src_w = hdisplay << 16;
2993                 primary_state->src_h = vdisplay << 16;
2994         }
2995
2996 commit:
2997         ret = update_output_state(state, set);
2998         if (ret)
2999                 return ret;
3000
3001         return 0;
3002 }
3003
3004 static int __drm_atomic_helper_disable_all(struct drm_device *dev,
3005                                            struct drm_modeset_acquire_ctx *ctx,
3006                                            bool clean_old_fbs)
3007 {
3008         struct drm_atomic_state *state;
3009         struct drm_connector_state *conn_state;
3010         struct drm_connector *conn;
3011         struct drm_plane_state *plane_state;
3012         struct drm_plane *plane;
3013         struct drm_crtc_state *crtc_state;
3014         struct drm_crtc *crtc;
3015         int ret, i;
3016
3017         state = drm_atomic_state_alloc(dev);
3018         if (!state)
3019                 return -ENOMEM;
3020
3021         state->acquire_ctx = ctx;
3022
3023         drm_for_each_crtc(crtc, dev) {
3024                 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3025                 if (IS_ERR(crtc_state)) {
3026                         ret = PTR_ERR(crtc_state);
3027                         goto free;
3028                 }
3029
3030                 crtc_state->active = false;
3031
3032                 ret = drm_atomic_set_mode_prop_for_crtc(crtc_state, NULL);
3033                 if (ret < 0)
3034                         goto free;
3035
3036                 ret = drm_atomic_add_affected_planes(state, crtc);
3037                 if (ret < 0)
3038                         goto free;
3039
3040                 ret = drm_atomic_add_affected_connectors(state, crtc);
3041                 if (ret < 0)
3042                         goto free;
3043         }
3044
3045         for_each_new_connector_in_state(state, conn, conn_state, i) {
3046                 ret = drm_atomic_set_crtc_for_connector(conn_state, NULL);
3047                 if (ret < 0)
3048                         goto free;
3049         }
3050
3051         for_each_new_plane_in_state(state, plane, plane_state, i) {
3052                 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
3053                 if (ret < 0)
3054                         goto free;
3055
3056                 drm_atomic_set_fb_for_plane(plane_state, NULL);
3057         }
3058
3059         ret = drm_atomic_commit(state);
3060 free:
3061         drm_atomic_state_put(state);
3062         return ret;
3063 }
3064
3065 /**
3066  * drm_atomic_helper_disable_all - disable all currently active outputs
3067  * @dev: DRM device
3068  * @ctx: lock acquisition context
3069  *
3070  * Loops through all connectors, finding those that aren't turned off and then
3071  * turns them off by setting their DPMS mode to OFF and deactivating the CRTC
3072  * that they are connected to.
3073  *
3074  * This is used for example in suspend/resume to disable all currently active
3075  * functions when suspending. If you just want to shut down everything at e.g.
3076  * driver unload, look at drm_atomic_helper_shutdown().
3077  *
3078  * Note that if callers haven't already acquired all modeset locks this might
3079  * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
3080  *
3081  * Returns:
3082  * 0 on success or a negative error code on failure.
3083  *
3084  * See also:
3085  * drm_atomic_helper_suspend(), drm_atomic_helper_resume() and
3086  * drm_atomic_helper_shutdown().
3087  */
3088 int drm_atomic_helper_disable_all(struct drm_device *dev,
3089                                   struct drm_modeset_acquire_ctx *ctx)
3090 {
3091         return __drm_atomic_helper_disable_all(dev, ctx, false);
3092 }
3093 EXPORT_SYMBOL(drm_atomic_helper_disable_all);
3094
3095 /**
3096  * drm_atomic_helper_shutdown - shutdown all CRTC
3097  * @dev: DRM device
3098  *
3099  * This shuts down all CRTC, which is useful for driver unloading. Shutdown on
3100  * suspend should instead be handled with drm_atomic_helper_suspend(), since
3101  * that also takes a snapshot of the modeset state to be restored on resume.
3102  *
3103  * This is just a convenience wrapper around drm_atomic_helper_disable_all(),
3104  * and it is the atomic version of drm_crtc_force_disable_all().
3105  */
3106 void drm_atomic_helper_shutdown(struct drm_device *dev)
3107 {
3108         struct drm_modeset_acquire_ctx ctx;
3109         int ret;
3110
3111         drm_modeset_acquire_init(&ctx, 0);
3112         while (1) {
3113                 ret = drm_modeset_lock_all_ctx(dev, &ctx);
3114                 if (!ret)
3115                         ret = __drm_atomic_helper_disable_all(dev, &ctx, true);
3116
3117                 if (ret != -EDEADLK)
3118                         break;
3119
3120                 drm_modeset_backoff(&ctx);
3121         }
3122
3123         if (ret)
3124                 DRM_ERROR("Disabling all crtc's during unload failed with %i\n", ret);
3125
3126         drm_modeset_drop_locks(&ctx);
3127         drm_modeset_acquire_fini(&ctx);
3128 }
3129 EXPORT_SYMBOL(drm_atomic_helper_shutdown);
3130
3131 /**
3132  * drm_atomic_helper_suspend - subsystem-level suspend helper
3133  * @dev: DRM device
3134  *
3135  * Duplicates the current atomic state, disables all active outputs and then
3136  * returns a pointer to the original atomic state to the caller. Drivers can
3137  * pass this pointer to the drm_atomic_helper_resume() helper upon resume to
3138  * restore the output configuration that was active at the time the system
3139  * entered suspend.
3140  *
3141  * Note that it is potentially unsafe to use this. The atomic state object
3142  * returned by this function is assumed to be persistent. Drivers must ensure
3143  * that this holds true. Before calling this function, drivers must make sure
3144  * to suspend fbdev emulation so that nothing can be using the device.
3145  *
3146  * Returns:
3147  * A pointer to a copy of the state before suspend on success or an ERR_PTR()-
3148  * encoded error code on failure. Drivers should store the returned atomic
3149  * state object and pass it to the drm_atomic_helper_resume() helper upon
3150  * resume.
3151  *
3152  * See also:
3153  * drm_atomic_helper_duplicate_state(), drm_atomic_helper_disable_all(),
3154  * drm_atomic_helper_resume(), drm_atomic_helper_commit_duplicated_state()
3155  */
3156 struct drm_atomic_state *drm_atomic_helper_suspend(struct drm_device *dev)
3157 {
3158         struct drm_modeset_acquire_ctx ctx;
3159         struct drm_atomic_state *state;
3160         int err;
3161
3162         drm_modeset_acquire_init(&ctx, 0);
3163
3164 retry:
3165         err = drm_modeset_lock_all_ctx(dev, &ctx);
3166         if (err < 0) {
3167                 state = ERR_PTR(err);
3168                 goto unlock;
3169         }
3170
3171         state = drm_atomic_helper_duplicate_state(dev, &ctx);
3172         if (IS_ERR(state))
3173                 goto unlock;
3174
3175         err = drm_atomic_helper_disable_all(dev, &ctx);
3176         if (err < 0) {
3177                 drm_atomic_state_put(state);
3178                 state = ERR_PTR(err);
3179                 goto unlock;
3180         }
3181
3182 unlock:
3183         if (PTR_ERR(state) == -EDEADLK) {
3184                 drm_modeset_backoff(&ctx);
3185                 goto retry;
3186         }
3187
3188         drm_modeset_drop_locks(&ctx);
3189         drm_modeset_acquire_fini(&ctx);
3190         return state;
3191 }
3192 EXPORT_SYMBOL(drm_atomic_helper_suspend);
3193
3194 /**
3195  * drm_atomic_helper_commit_duplicated_state - commit duplicated state
3196  * @state: duplicated atomic state to commit
3197  * @ctx: pointer to acquire_ctx to use for commit.
3198  *
3199  * The state returned by drm_atomic_helper_duplicate_state() and
3200  * drm_atomic_helper_suspend() is partially invalid, and needs to
3201  * be fixed up before commit.
3202  *
3203  * Returns:
3204  * 0 on success or a negative error code on failure.
3205  *
3206  * See also:
3207  * drm_atomic_helper_suspend()
3208  */
3209 int drm_atomic_helper_commit_duplicated_state(struct drm_atomic_state *state,
3210                                               struct drm_modeset_acquire_ctx *ctx)
3211 {
3212         int i;
3213         struct drm_plane *plane;
3214         struct drm_plane_state *new_plane_state;
3215         struct drm_connector *connector;
3216         struct drm_connector_state *new_conn_state;
3217         struct drm_crtc *crtc;
3218         struct drm_crtc_state *new_crtc_state;
3219
3220         state->acquire_ctx = ctx;
3221
3222         for_each_new_plane_in_state(state, plane, new_plane_state, i)
3223                 state->planes[i].old_state = plane->state;
3224
3225         for_each_new_crtc_in_state(state, crtc, new_crtc_state, i)
3226                 state->crtcs[i].old_state = crtc->state;
3227
3228         for_each_new_connector_in_state(state, connector, new_conn_state, i)
3229                 state->connectors[i].old_state = connector->state;
3230
3231         return drm_atomic_commit(state);
3232 }
3233 EXPORT_SYMBOL(drm_atomic_helper_commit_duplicated_state);
3234
3235 /**
3236  * drm_atomic_helper_resume - subsystem-level resume helper
3237  * @dev: DRM device
3238  * @state: atomic state to resume to
3239  *
3240  * Calls drm_mode_config_reset() to synchronize hardware and software states,
3241  * grabs all modeset locks and commits the atomic state object. This can be
3242  * used in conjunction with the drm_atomic_helper_suspend() helper to
3243  * implement suspend/resume for drivers that support atomic mode-setting.
3244  *
3245  * Returns:
3246  * 0 on success or a negative error code on failure.
3247  *
3248  * See also:
3249  * drm_atomic_helper_suspend()
3250  */
3251 int drm_atomic_helper_resume(struct drm_device *dev,
3252                              struct drm_atomic_state *state)
3253 {
3254         struct drm_modeset_acquire_ctx ctx;
3255         int err;
3256
3257         drm_mode_config_reset(dev);
3258
3259         drm_modeset_acquire_init(&ctx, 0);
3260         while (1) {
3261                 err = drm_modeset_lock_all_ctx(dev, &ctx);
3262                 if (err)
3263                         goto out;
3264
3265                 err = drm_atomic_helper_commit_duplicated_state(state, &ctx);
3266 out:
3267                 if (err != -EDEADLK)
3268                         break;
3269
3270                 drm_modeset_backoff(&ctx);
3271         }
3272
3273         drm_atomic_state_put(state);
3274         drm_modeset_drop_locks(&ctx);
3275         drm_modeset_acquire_fini(&ctx);
3276
3277         return err;
3278 }
3279 EXPORT_SYMBOL(drm_atomic_helper_resume);
3280
3281 static int page_flip_common(struct drm_atomic_state *state,
3282                             struct drm_crtc *crtc,
3283                             struct drm_framebuffer *fb,
3284                             struct drm_pending_vblank_event *event,
3285                             uint32_t flags)
3286 {
3287         struct drm_plane *plane = crtc->primary;
3288         struct drm_plane_state *plane_state;
3289         struct drm_crtc_state *crtc_state;
3290         int ret = 0;
3291
3292         crtc_state = drm_atomic_get_crtc_state(state, crtc);
3293         if (IS_ERR(crtc_state))
3294                 return PTR_ERR(crtc_state);
3295
3296         crtc_state->event = event;
3297         crtc_state->pageflip_flags = flags;
3298
3299         plane_state = drm_atomic_get_plane_state(state, plane);
3300         if (IS_ERR(plane_state))
3301                 return PTR_ERR(plane_state);
3302
3303         ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
3304         if (ret != 0)
3305                 return ret;
3306         drm_atomic_set_fb_for_plane(plane_state, fb);
3307
3308         /* Make sure we don't accidentally do a full modeset. */
3309         state->allow_modeset = false;
3310         if (!crtc_state->active) {
3311                 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] disabled, rejecting legacy flip\n",
3312                                  crtc->base.id, crtc->name);
3313                 return -EINVAL;
3314         }
3315
3316         return ret;
3317 }
3318
3319 /**
3320  * drm_atomic_helper_page_flip - execute a legacy page flip
3321  * @crtc: DRM crtc
3322  * @fb: DRM framebuffer
3323  * @event: optional DRM event to signal upon completion
3324  * @flags: flip flags for non-vblank sync'ed updates
3325  * @ctx: lock acquisition context
3326  *
3327  * Provides a default &drm_crtc_funcs.page_flip implementation
3328  * using the atomic driver interface.
3329  *
3330  * Returns:
3331  * Returns 0 on success, negative errno numbers on failure.
3332  *
3333  * See also:
3334  * drm_atomic_helper_page_flip_target()
3335  */
3336 int drm_atomic_helper_page_flip(struct drm_crtc *crtc,
3337                                 struct drm_framebuffer *fb,
3338                                 struct drm_pending_vblank_event *event,
3339                                 uint32_t flags,
3340                                 struct drm_modeset_acquire_ctx *ctx)
3341 {
3342         struct drm_plane *plane = crtc->primary;
3343         struct drm_atomic_state *state;
3344         int ret = 0;
3345
3346         state = drm_atomic_state_alloc(plane->dev);
3347         if (!state)
3348                 return -ENOMEM;
3349
3350         state->acquire_ctx = ctx;
3351
3352         ret = page_flip_common(state, crtc, fb, event, flags);
3353         if (ret != 0)
3354                 goto fail;
3355
3356         ret = drm_atomic_nonblocking_commit(state);
3357 fail:
3358         drm_atomic_state_put(state);
3359         return ret;
3360 }
3361 EXPORT_SYMBOL(drm_atomic_helper_page_flip);
3362
3363 /**
3364  * drm_atomic_helper_page_flip_target - do page flip on target vblank period.
3365  * @crtc: DRM crtc
3366  * @fb: DRM framebuffer
3367  * @event: optional DRM event to signal upon completion
3368  * @flags: flip flags for non-vblank sync'ed updates
3369  * @target: specifying the target vblank period when the flip to take effect
3370  * @ctx: lock acquisition context
3371  *
3372  * Provides a default &drm_crtc_funcs.page_flip_target implementation.
3373  * Similar to drm_atomic_helper_page_flip() with extra parameter to specify
3374  * target vblank period to flip.
3375  *
3376  * Returns:
3377  * Returns 0 on success, negative errno numbers on failure.
3378  */
3379 int drm_atomic_helper_page_flip_target(struct drm_crtc *crtc,
3380                                        struct drm_framebuffer *fb,
3381                                        struct drm_pending_vblank_event *event,
3382                                        uint32_t flags,
3383                                        uint32_t target,
3384                                        struct drm_modeset_acquire_ctx *ctx)
3385 {
3386         struct drm_plane *plane = crtc->primary;
3387         struct drm_atomic_state *state;
3388         struct drm_crtc_state *crtc_state;
3389         int ret = 0;
3390
3391         state = drm_atomic_state_alloc(plane->dev);
3392         if (!state)
3393                 return -ENOMEM;
3394
3395         state->acquire_ctx = ctx;
3396
3397         ret = page_flip_common(state, crtc, fb, event, flags);
3398         if (ret != 0)
3399                 goto fail;
3400
3401         crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
3402         if (WARN_ON(!crtc_state)) {
3403                 ret = -EINVAL;
3404                 goto fail;
3405         }
3406         crtc_state->target_vblank = target;
3407
3408         ret = drm_atomic_nonblocking_commit(state);
3409 fail:
3410         drm_atomic_state_put(state);
3411         return ret;
3412 }
3413 EXPORT_SYMBOL(drm_atomic_helper_page_flip_target);
3414
3415 /**
3416  * drm_atomic_helper_best_encoder - Helper for
3417  *      &drm_connector_helper_funcs.best_encoder callback
3418  * @connector: Connector control structure
3419  *
3420  * This is a &drm_connector_helper_funcs.best_encoder callback helper for
3421  * connectors that support exactly 1 encoder, statically determined at driver
3422  * init time.
3423  */
3424 struct drm_encoder *
3425 drm_atomic_helper_best_encoder(struct drm_connector *connector)
3426 {
3427         WARN_ON(connector->encoder_ids[1]);
3428         return drm_encoder_find(connector->dev, NULL, connector->encoder_ids[0]);
3429 }
3430 EXPORT_SYMBOL(drm_atomic_helper_best_encoder);
3431
3432 /**
3433  * DOC: atomic state reset and initialization
3434  *
3435  * Both the drm core and the atomic helpers assume that there is always the full
3436  * and correct atomic software state for all connectors, CRTCs and planes
3437  * available. Which is a bit a problem on driver load and also after system
3438  * suspend. One way to solve this is to have a hardware state read-out
3439  * infrastructure which reconstructs the full software state (e.g. the i915
3440  * driver).
3441  *
3442  * The simpler solution is to just reset the software state to everything off,
3443  * which is easiest to do by calling drm_mode_config_reset(). To facilitate this
3444  * the atomic helpers provide default reset implementations for all hooks.
3445  *
3446  * On the upside the precise state tracking of atomic simplifies system suspend
3447  * and resume a lot. For drivers using drm_mode_config_reset() a complete recipe
3448  * is implemented in drm_atomic_helper_suspend() and drm_atomic_helper_resume().
3449  * For other drivers the building blocks are split out, see the documentation
3450  * for these functions.
3451  */
3452
3453 /**
3454  * drm_atomic_helper_crtc_reset - default &drm_crtc_funcs.reset hook for CRTCs
3455  * @crtc: drm CRTC
3456  *
3457  * Resets the atomic state for @crtc by freeing the state pointer (which might
3458  * be NULL, e.g. at driver load time) and allocating a new empty state object.
3459  */
3460 void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc)
3461 {
3462         if (crtc->state)
3463                 __drm_atomic_helper_crtc_destroy_state(crtc->state);
3464
3465         kfree(crtc->state);
3466         crtc->state = kzalloc(sizeof(*crtc->state), GFP_KERNEL);
3467
3468         if (crtc->state)
3469                 crtc->state->crtc = crtc;
3470 }
3471 EXPORT_SYMBOL(drm_atomic_helper_crtc_reset);
3472
3473 /**
3474  * __drm_atomic_helper_crtc_duplicate_state - copy atomic CRTC state
3475  * @crtc: CRTC object
3476  * @state: atomic CRTC state
3477  *
3478  * Copies atomic state from a CRTC's current state and resets inferred values.
3479  * This is useful for drivers that subclass the CRTC state.
3480  */
3481 void __drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc,
3482                                               struct drm_crtc_state *state)
3483 {
3484         memcpy(state, crtc->state, sizeof(*state));
3485
3486         if (state->mode_blob)
3487                 drm_property_blob_get(state->mode_blob);
3488         if (state->degamma_lut)
3489                 drm_property_blob_get(state->degamma_lut);
3490         if (state->ctm)
3491                 drm_property_blob_get(state->ctm);
3492         if (state->gamma_lut)
3493                 drm_property_blob_get(state->gamma_lut);
3494         state->mode_changed = false;
3495         state->active_changed = false;
3496         state->planes_changed = false;
3497         state->connectors_changed = false;
3498         state->color_mgmt_changed = false;
3499         state->zpos_changed = false;
3500         state->commit = NULL;
3501         state->event = NULL;
3502         state->pageflip_flags = 0;
3503 }
3504 EXPORT_SYMBOL(__drm_atomic_helper_crtc_duplicate_state);
3505
3506 /**
3507  * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
3508  * @crtc: drm CRTC
3509  *
3510  * Default CRTC state duplicate hook for drivers which don't have their own
3511  * subclassed CRTC state structure.
3512  */
3513 struct drm_crtc_state *
3514 drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc)
3515 {
3516         struct drm_crtc_state *state;
3517
3518         if (WARN_ON(!crtc->state))
3519                 return NULL;
3520
3521         state = kmalloc(sizeof(*state), GFP_KERNEL);
3522         if (state)
3523                 __drm_atomic_helper_crtc_duplicate_state(crtc, state);
3524
3525         return state;
3526 }
3527 EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state);
3528
3529 /**
3530  * __drm_atomic_helper_crtc_destroy_state - release CRTC state
3531  * @state: CRTC state object to release
3532  *
3533  * Releases all resources stored in the CRTC state without actually freeing
3534  * the memory of the CRTC state. This is useful for drivers that subclass the
3535  * CRTC state.
3536  */
3537 void __drm_atomic_helper_crtc_destroy_state(struct drm_crtc_state *state)
3538 {
3539         if (state->commit) {
3540                 /*
3541                  * In the event that a non-blocking commit returns
3542                  * -ERESTARTSYS before the commit_tail work is queued, we will
3543                  * have an extra reference to the commit object. Release it, if
3544                  * the event has not been consumed by the worker.
3545                  *
3546                  * state->event may be freed, so we can't directly look at
3547                  * state->event->base.completion.
3548                  */
3549                 if (state->event && state->commit->abort_completion)
3550                         drm_crtc_commit_put(state->commit);
3551
3552                 kfree(state->commit->event);
3553                 state->commit->event = NULL;
3554
3555                 drm_crtc_commit_put(state->commit);
3556         }
3557
3558         drm_property_blob_put(state->mode_blob);
3559         drm_property_blob_put(state->degamma_lut);
3560         drm_property_blob_put(state->ctm);
3561         drm_property_blob_put(state->gamma_lut);
3562 }
3563 EXPORT_SYMBOL(__drm_atomic_helper_crtc_destroy_state);
3564
3565 /**
3566  * drm_atomic_helper_crtc_destroy_state - default state destroy hook
3567  * @crtc: drm CRTC
3568  * @state: CRTC state object to release
3569  *
3570  * Default CRTC state destroy hook for drivers which don't have their own
3571  * subclassed CRTC state structure.
3572  */
3573 void drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
3574                                           struct drm_crtc_state *state)
3575 {
3576         __drm_atomic_helper_crtc_destroy_state(state);
3577         kfree(state);
3578 }
3579 EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state);
3580
3581 /**
3582  * __drm_atomic_helper_plane_reset - resets planes state to default values
3583  * @plane: plane object, must not be NULL
3584  * @state: atomic plane state, must not be NULL
3585  *
3586  * Initializes plane state to default. This is useful for drivers that subclass
3587  * the plane state.
3588  */
3589 void __drm_atomic_helper_plane_reset(struct drm_plane *plane,
3590                                      struct drm_plane_state *state)
3591 {
3592         state->plane = plane;
3593         state->rotation = DRM_MODE_ROTATE_0;
3594
3595         state->alpha = DRM_BLEND_ALPHA_OPAQUE;
3596         state->pixel_blend_mode = DRM_MODE_BLEND_PREMULTI;
3597
3598         plane->state = state;
3599 }
3600 EXPORT_SYMBOL(__drm_atomic_helper_plane_reset);
3601
3602 /**
3603  * drm_atomic_helper_plane_reset - default &drm_plane_funcs.reset hook for planes
3604  * @plane: drm plane
3605  *
3606  * Resets the atomic state for @plane by freeing the state pointer (which might
3607  * be NULL, e.g. at driver load time) and allocating a new empty state object.
3608  */
3609 void drm_atomic_helper_plane_reset(struct drm_plane *plane)
3610 {
3611         if (plane->state)
3612                 __drm_atomic_helper_plane_destroy_state(plane->state);
3613
3614         kfree(plane->state);
3615         plane->state = kzalloc(sizeof(*plane->state), GFP_KERNEL);
3616         if (plane->state)
3617                 __drm_atomic_helper_plane_reset(plane, plane->state);
3618 }
3619 EXPORT_SYMBOL(drm_atomic_helper_plane_reset);
3620
3621 /**
3622  * __drm_atomic_helper_plane_duplicate_state - copy atomic plane state
3623  * @plane: plane object
3624  * @state: atomic plane state
3625  *
3626  * Copies atomic state from a plane's current state. This is useful for
3627  * drivers that subclass the plane state.
3628  */
3629 void __drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane,
3630                                                struct drm_plane_state *state)
3631 {
3632         memcpy(state, plane->state, sizeof(*state));
3633
3634         if (state->fb)
3635                 drm_framebuffer_get(state->fb);
3636
3637         state->fence = NULL;
3638         state->commit = NULL;
3639 }
3640 EXPORT_SYMBOL(__drm_atomic_helper_plane_duplicate_state);
3641
3642 /**
3643  * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
3644  * @plane: drm plane
3645  *
3646  * Default plane state duplicate hook for drivers which don't have their own
3647  * subclassed plane state structure.
3648  */
3649 struct drm_plane_state *
3650 drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane)
3651 {
3652         struct drm_plane_state *state;
3653
3654         if (WARN_ON(!plane->state))
3655                 return NULL;
3656
3657         state = kmalloc(sizeof(*state), GFP_KERNEL);
3658         if (state)
3659                 __drm_atomic_helper_plane_duplicate_state(plane, state);
3660
3661         return state;
3662 }
3663 EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state);
3664
3665 /**
3666  * __drm_atomic_helper_plane_destroy_state - release plane state
3667  * @state: plane state object to release
3668  *
3669  * Releases all resources stored in the plane state without actually freeing
3670  * the memory of the plane state. This is useful for drivers that subclass the
3671  * plane state.
3672  */
3673 void __drm_atomic_helper_plane_destroy_state(struct drm_plane_state *state)
3674 {
3675         if (state->fb)
3676                 drm_framebuffer_put(state->fb);
3677
3678         if (state->fence)
3679                 dma_fence_put(state->fence);
3680
3681         if (state->commit)
3682                 drm_crtc_commit_put(state->commit);
3683 }
3684 EXPORT_SYMBOL(__drm_atomic_helper_plane_destroy_state);
3685
3686 /**
3687  * drm_atomic_helper_plane_destroy_state - default state destroy hook
3688  * @plane: drm plane
3689  * @state: plane state object to release
3690  *
3691  * Default plane state destroy hook for drivers which don't have their own
3692  * subclassed plane state structure.
3693  */
3694 void drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
3695                                            struct drm_plane_state *state)
3696 {
3697         __drm_atomic_helper_plane_destroy_state(state);
3698         kfree(state);
3699 }
3700 EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state);
3701
3702 /**
3703  * __drm_atomic_helper_connector_reset - reset state on connector
3704  * @connector: drm connector
3705  * @conn_state: connector state to assign
3706  *
3707  * Initializes the newly allocated @conn_state and assigns it to
3708  * the &drm_conector->state pointer of @connector, usually required when
3709  * initializing the drivers or when called from the &drm_connector_funcs.reset
3710  * hook.
3711  *
3712  * This is useful for drivers that subclass the connector state.
3713  */
3714 void
3715 __drm_atomic_helper_connector_reset(struct drm_connector *connector,
3716                                     struct drm_connector_state *conn_state)
3717 {
3718         if (conn_state)
3719                 conn_state->connector = connector;
3720
3721         connector->state = conn_state;
3722 }
3723 EXPORT_SYMBOL(__drm_atomic_helper_connector_reset);
3724
3725 /**
3726  * drm_atomic_helper_connector_reset - default &drm_connector_funcs.reset hook for connectors
3727  * @connector: drm connector
3728  *
3729  * Resets the atomic state for @connector by freeing the state pointer (which
3730  * might be NULL, e.g. at driver load time) and allocating a new empty state
3731  * object.
3732  */
3733 void drm_atomic_helper_connector_reset(struct drm_connector *connector)
3734 {
3735         struct drm_connector_state *conn_state =
3736                 kzalloc(sizeof(*conn_state), GFP_KERNEL);
3737
3738         if (connector->state)
3739                 __drm_atomic_helper_connector_destroy_state(connector->state);
3740
3741         kfree(connector->state);
3742         __drm_atomic_helper_connector_reset(connector, conn_state);
3743 }
3744 EXPORT_SYMBOL(drm_atomic_helper_connector_reset);
3745
3746 /**
3747  * __drm_atomic_helper_connector_duplicate_state - copy atomic connector state
3748  * @connector: connector object
3749  * @state: atomic connector state
3750  *
3751  * Copies atomic state from a connector's current state. This is useful for
3752  * drivers that subclass the connector state.
3753  */
3754 void
3755 __drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector,
3756                                             struct drm_connector_state *state)
3757 {
3758         memcpy(state, connector->state, sizeof(*state));
3759         if (state->crtc)
3760                 drm_connector_get(connector);
3761         state->commit = NULL;
3762
3763         /* Don't copy over a writeback job, they are used only once */
3764         state->writeback_job = NULL;
3765 }
3766 EXPORT_SYMBOL(__drm_atomic_helper_connector_duplicate_state);
3767
3768 /**
3769  * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
3770  * @connector: drm connector
3771  *
3772  * Default connector state duplicate hook for drivers which don't have their own
3773  * subclassed connector state structure.
3774  */
3775 struct drm_connector_state *
3776 drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
3777 {
3778         struct drm_connector_state *state;
3779
3780         if (WARN_ON(!connector->state))
3781                 return NULL;
3782
3783         state = kmalloc(sizeof(*state), GFP_KERNEL);
3784         if (state)
3785                 __drm_atomic_helper_connector_duplicate_state(connector, state);
3786
3787         return state;
3788 }
3789 EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
3790
3791 /**
3792  * drm_atomic_helper_duplicate_state - duplicate an atomic state object
3793  * @dev: DRM device
3794  * @ctx: lock acquisition context
3795  *
3796  * Makes a copy of the current atomic state by looping over all objects and
3797  * duplicating their respective states. This is used for example by suspend/
3798  * resume support code to save the state prior to suspend such that it can
3799  * be restored upon resume.
3800  *
3801  * Note that this treats atomic state as persistent between save and restore.
3802  * Drivers must make sure that this is possible and won't result in confusion
3803  * or erroneous behaviour.
3804  *
3805  * Note that if callers haven't already acquired all modeset locks this might
3806  * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
3807  *
3808  * Returns:
3809  * A pointer to the copy of the atomic state object on success or an
3810  * ERR_PTR()-encoded error code on failure.
3811  *
3812  * See also:
3813  * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
3814  */
3815 struct drm_atomic_state *
3816 drm_atomic_helper_duplicate_state(struct drm_device *dev,
3817                                   struct drm_modeset_acquire_ctx *ctx)
3818 {
3819         struct drm_atomic_state *state;
3820         struct drm_connector *conn;
3821         struct drm_connector_list_iter conn_iter;
3822         struct drm_plane *plane;
3823         struct drm_crtc *crtc;
3824         int err = 0;
3825
3826         state = drm_atomic_state_alloc(dev);
3827         if (!state)
3828                 return ERR_PTR(-ENOMEM);
3829
3830         state->acquire_ctx = ctx;
3831
3832         drm_for_each_crtc(crtc, dev) {
3833                 struct drm_crtc_state *crtc_state;
3834
3835                 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3836                 if (IS_ERR(crtc_state)) {
3837                         err = PTR_ERR(crtc_state);
3838                         goto free;
3839                 }
3840         }
3841
3842         drm_for_each_plane(plane, dev) {
3843                 struct drm_plane_state *plane_state;
3844
3845                 plane_state = drm_atomic_get_plane_state(state, plane);
3846                 if (IS_ERR(plane_state)) {
3847                         err = PTR_ERR(plane_state);
3848                         goto free;
3849                 }
3850         }
3851
3852         drm_connector_list_iter_begin(dev, &conn_iter);
3853         drm_for_each_connector_iter(conn, &conn_iter) {
3854                 struct drm_connector_state *conn_state;
3855
3856                 conn_state = drm_atomic_get_connector_state(state, conn);
3857                 if (IS_ERR(conn_state)) {
3858                         err = PTR_ERR(conn_state);
3859                         drm_connector_list_iter_end(&conn_iter);
3860                         goto free;
3861                 }
3862         }
3863         drm_connector_list_iter_end(&conn_iter);
3864
3865         /* clear the acquire context so that it isn't accidentally reused */
3866         state->acquire_ctx = NULL;
3867
3868 free:
3869         if (err < 0) {
3870                 drm_atomic_state_put(state);
3871                 state = ERR_PTR(err);
3872         }
3873
3874         return state;
3875 }
3876 EXPORT_SYMBOL(drm_atomic_helper_duplicate_state);
3877
3878 /**
3879  * __drm_atomic_helper_connector_destroy_state - release connector state
3880  * @state: connector state object to release
3881  *
3882  * Releases all resources stored in the connector state without actually
3883  * freeing the memory of the connector state. This is useful for drivers that
3884  * subclass the connector state.
3885  */
3886 void
3887 __drm_atomic_helper_connector_destroy_state(struct drm_connector_state *state)
3888 {
3889         if (state->crtc)
3890                 drm_connector_put(state->connector);
3891
3892         if (state->commit)
3893                 drm_crtc_commit_put(state->commit);
3894 }
3895 EXPORT_SYMBOL(__drm_atomic_helper_connector_destroy_state);
3896
3897 /**
3898  * drm_atomic_helper_connector_destroy_state - default state destroy hook
3899  * @connector: drm connector
3900  * @state: connector state object to release
3901  *
3902  * Default connector state destroy hook for drivers which don't have their own
3903  * subclassed connector state structure.
3904  */
3905 void drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
3906                                           struct drm_connector_state *state)
3907 {
3908         __drm_atomic_helper_connector_destroy_state(state);
3909         kfree(state);
3910 }
3911 EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state);
3912
3913 /**
3914  * drm_atomic_helper_legacy_gamma_set - set the legacy gamma correction table
3915  * @crtc: CRTC object
3916  * @red: red correction table
3917  * @green: green correction table
3918  * @blue: green correction table
3919  * @size: size of the tables
3920  * @ctx: lock acquire context
3921  *
3922  * Implements support for legacy gamma correction table for drivers
3923  * that support color management through the DEGAMMA_LUT/GAMMA_LUT
3924  * properties. See drm_crtc_enable_color_mgmt() and the containing chapter for
3925  * how the atomic color management and gamma tables work.
3926  */
3927 int drm_atomic_helper_legacy_gamma_set(struct drm_crtc *crtc,
3928                                        u16 *red, u16 *green, u16 *blue,
3929                                        uint32_t size,
3930                                        struct drm_modeset_acquire_ctx *ctx)
3931 {
3932         struct drm_device *dev = crtc->dev;
3933         struct drm_atomic_state *state;
3934         struct drm_crtc_state *crtc_state;
3935         struct drm_property_blob *blob = NULL;
3936         struct drm_color_lut *blob_data;
3937         int i, ret = 0;
3938         bool replaced;
3939
3940         state = drm_atomic_state_alloc(crtc->dev);
3941         if (!state)
3942                 return -ENOMEM;
3943
3944         blob = drm_property_create_blob(dev,
3945                                         sizeof(struct drm_color_lut) * size,
3946                                         NULL);
3947         if (IS_ERR(blob)) {
3948                 ret = PTR_ERR(blob);
3949                 blob = NULL;
3950                 goto fail;
3951         }
3952
3953         /* Prepare GAMMA_LUT with the legacy values. */
3954         blob_data = blob->data;
3955         for (i = 0; i < size; i++) {
3956                 blob_data[i].red = red[i];
3957                 blob_data[i].green = green[i];
3958                 blob_data[i].blue = blue[i];
3959         }
3960
3961         state->acquire_ctx = ctx;
3962         crtc_state = drm_atomic_get_crtc_state(state, crtc);
3963         if (IS_ERR(crtc_state)) {
3964                 ret = PTR_ERR(crtc_state);
3965                 goto fail;
3966         }
3967
3968         /* Reset DEGAMMA_LUT and CTM properties. */
3969         replaced  = drm_property_replace_blob(&crtc_state->degamma_lut, NULL);
3970         replaced |= drm_property_replace_blob(&crtc_state->ctm, NULL);
3971         replaced |= drm_property_replace_blob(&crtc_state->gamma_lut, blob);
3972         crtc_state->color_mgmt_changed |= replaced;
3973
3974         ret = drm_atomic_commit(state);
3975
3976 fail:
3977         drm_atomic_state_put(state);
3978         drm_property_blob_put(blob);
3979         return ret;
3980 }
3981 EXPORT_SYMBOL(drm_atomic_helper_legacy_gamma_set);
3982
3983 /**
3984  * __drm_atomic_helper_private_duplicate_state - copy atomic private state
3985  * @obj: CRTC object
3986  * @state: new private object state
3987  *
3988  * Copies atomic state from a private objects's current state and resets inferred values.
3989  * This is useful for drivers that subclass the private state.
3990  */
3991 void __drm_atomic_helper_private_obj_duplicate_state(struct drm_private_obj *obj,
3992                                                      struct drm_private_state *state)
3993 {
3994         memcpy(state, obj->state, sizeof(*state));
3995 }
3996 EXPORT_SYMBOL(__drm_atomic_helper_private_obj_duplicate_state);