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