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