GNU Linux-libre 4.14.254-gnu1
[releases.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                 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
578
579                 if (!drm_mode_equal(&old_crtc_state->mode, &new_crtc_state->mode)) {
580                         DRM_DEBUG_ATOMIC("[CRTC:%d:%s] mode changed\n",
581                                          crtc->base.id, crtc->name);
582                         new_crtc_state->mode_changed = true;
583                 }
584
585                 if (old_crtc_state->enable != new_crtc_state->enable) {
586                         DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enable changed\n",
587                                          crtc->base.id, crtc->name);
588
589                         /*
590                          * For clarity this assignment is done here, but
591                          * enable == 0 is only true when there are no
592                          * connectors and a NULL mode.
593                          *
594                          * The other way around is true as well. enable != 0
595                          * iff connectors are attached and a mode is set.
596                          */
597                         new_crtc_state->mode_changed = true;
598                         new_crtc_state->connectors_changed = true;
599                 }
600
601                 if (old_crtc_state->active != new_crtc_state->active) {
602                         DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active changed\n",
603                                          crtc->base.id, crtc->name);
604                         new_crtc_state->active_changed = true;
605                 }
606
607                 if (new_crtc_state->enable != has_connectors) {
608                         DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled/connectors mismatch\n",
609                                          crtc->base.id, crtc->name);
610
611                         return -EINVAL;
612                 }
613         }
614
615         ret = handle_conflicting_encoders(state, false);
616         if (ret)
617                 return ret;
618
619         for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
620                 const struct drm_connector_helper_funcs *funcs = connector->helper_private;
621
622                 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
623
624                 /*
625                  * This only sets crtc->connectors_changed for routing changes,
626                  * drivers must set crtc->connectors_changed themselves when
627                  * connector properties need to be updated.
628                  */
629                 ret = update_connector_routing(state, connector,
630                                                old_connector_state,
631                                                new_connector_state);
632                 if (ret)
633                         return ret;
634                 if (old_connector_state->crtc) {
635                         new_crtc_state = drm_atomic_get_new_crtc_state(state,
636                                                                        old_connector_state->crtc);
637                         if (old_connector_state->link_status !=
638                             new_connector_state->link_status)
639                                 new_crtc_state->connectors_changed = true;
640                 }
641
642                 if (funcs->atomic_check)
643                         ret = funcs->atomic_check(connector, new_connector_state);
644                 if (ret)
645                         return ret;
646
647                 connectors_mask += BIT(i);
648         }
649
650         /*
651          * After all the routing has been prepared we need to add in any
652          * connector which is itself unchanged, but who's crtc changes it's
653          * configuration. This must be done before calling mode_fixup in case a
654          * crtc only changed its mode but has the same set of connectors.
655          */
656         for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
657                 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
658                         continue;
659
660                 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] needs all connectors, enable: %c, active: %c\n",
661                                  crtc->base.id, crtc->name,
662                                  new_crtc_state->enable ? 'y' : 'n',
663                                  new_crtc_state->active ? 'y' : 'n');
664
665                 ret = drm_atomic_add_affected_connectors(state, crtc);
666                 if (ret != 0)
667                         return ret;
668
669                 ret = drm_atomic_add_affected_planes(state, crtc);
670                 if (ret != 0)
671                         return ret;
672         }
673
674         /*
675          * Iterate over all connectors again, to make sure atomic_check()
676          * has been called on them when a modeset is forced.
677          */
678         for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
679                 const struct drm_connector_helper_funcs *funcs = connector->helper_private;
680
681                 if (connectors_mask & BIT(i))
682                         continue;
683
684                 if (funcs->atomic_check)
685                         ret = funcs->atomic_check(connector, new_connector_state);
686                 if (ret)
687                         return ret;
688         }
689
690         ret = mode_valid(state);
691         if (ret)
692                 return ret;
693
694         return mode_fixup(state);
695 }
696 EXPORT_SYMBOL(drm_atomic_helper_check_modeset);
697
698 /**
699  * drm_atomic_helper_check_planes - validate state object for planes changes
700  * @dev: DRM device
701  * @state: the driver state object
702  *
703  * Check the state object to see if the requested state is physically possible.
704  * This does all the plane update related checks using by calling into the
705  * &drm_crtc_helper_funcs.atomic_check and &drm_plane_helper_funcs.atomic_check
706  * hooks provided by the driver.
707  *
708  * It also sets &drm_crtc_state.planes_changed to indicate that a crtc has
709  * updated planes.
710  *
711  * RETURNS:
712  * Zero for success or -errno
713  */
714 int
715 drm_atomic_helper_check_planes(struct drm_device *dev,
716                                struct drm_atomic_state *state)
717 {
718         struct drm_crtc *crtc;
719         struct drm_crtc_state *new_crtc_state;
720         struct drm_plane *plane;
721         struct drm_plane_state *new_plane_state, *old_plane_state;
722         int i, ret = 0;
723
724         for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
725                 const struct drm_plane_helper_funcs *funcs;
726
727                 WARN_ON(!drm_modeset_is_locked(&plane->mutex));
728
729                 funcs = plane->helper_private;
730
731                 drm_atomic_helper_plane_changed(state, old_plane_state, new_plane_state, plane);
732
733                 if (!funcs || !funcs->atomic_check)
734                         continue;
735
736                 ret = funcs->atomic_check(plane, new_plane_state);
737                 if (ret) {
738                         DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic driver check failed\n",
739                                          plane->base.id, plane->name);
740                         return ret;
741                 }
742         }
743
744         for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
745                 const struct drm_crtc_helper_funcs *funcs;
746
747                 funcs = crtc->helper_private;
748
749                 if (!funcs || !funcs->atomic_check)
750                         continue;
751
752                 ret = funcs->atomic_check(crtc, new_crtc_state);
753                 if (ret) {
754                         DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic driver check failed\n",
755                                          crtc->base.id, crtc->name);
756                         return ret;
757                 }
758         }
759
760         return ret;
761 }
762 EXPORT_SYMBOL(drm_atomic_helper_check_planes);
763
764 /**
765  * drm_atomic_helper_check - validate state object
766  * @dev: DRM device
767  * @state: the driver state object
768  *
769  * Check the state object to see if the requested state is physically possible.
770  * Only crtcs and planes have check callbacks, so for any additional (global)
771  * checking that a driver needs it can simply wrap that around this function.
772  * Drivers without such needs can directly use this as their
773  * &drm_mode_config_funcs.atomic_check callback.
774  *
775  * This just wraps the two parts of the state checking for planes and modeset
776  * state in the default order: First it calls drm_atomic_helper_check_modeset()
777  * and then drm_atomic_helper_check_planes(). The assumption is that the
778  * @drm_plane_helper_funcs.atomic_check and @drm_crtc_helper_funcs.atomic_check
779  * functions depend upon an updated adjusted_mode.clock to e.g. properly compute
780  * watermarks.
781  *
782  * RETURNS:
783  * Zero for success or -errno
784  */
785 int drm_atomic_helper_check(struct drm_device *dev,
786                             struct drm_atomic_state *state)
787 {
788         int ret;
789
790         ret = drm_atomic_helper_check_modeset(dev, state);
791         if (ret)
792                 return ret;
793
794         ret = drm_atomic_helper_check_planes(dev, state);
795         if (ret)
796                 return ret;
797
798         if (state->legacy_cursor_update)
799                 state->async_update = !drm_atomic_helper_async_check(dev, state);
800
801         return ret;
802 }
803 EXPORT_SYMBOL(drm_atomic_helper_check);
804
805 static void
806 disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state)
807 {
808         struct drm_connector *connector;
809         struct drm_connector_state *old_conn_state, *new_conn_state;
810         struct drm_crtc *crtc;
811         struct drm_crtc_state *old_crtc_state, *new_crtc_state;
812         int i;
813
814         for_each_oldnew_connector_in_state(old_state, connector, old_conn_state, new_conn_state, i) {
815                 const struct drm_encoder_helper_funcs *funcs;
816                 struct drm_encoder *encoder;
817
818                 /* Shut down everything that's in the changeset and currently
819                  * still on. So need to check the old, saved state. */
820                 if (!old_conn_state->crtc)
821                         continue;
822
823                 old_crtc_state = drm_atomic_get_old_crtc_state(old_state, old_conn_state->crtc);
824
825                 if (!old_crtc_state->active ||
826                     !drm_atomic_crtc_needs_modeset(old_conn_state->crtc->state))
827                         continue;
828
829                 encoder = old_conn_state->best_encoder;
830
831                 /* We shouldn't get this far if we didn't previously have
832                  * an encoder.. but WARN_ON() rather than explode.
833                  */
834                 if (WARN_ON(!encoder))
835                         continue;
836
837                 funcs = encoder->helper_private;
838
839                 DRM_DEBUG_ATOMIC("disabling [ENCODER:%d:%s]\n",
840                                  encoder->base.id, encoder->name);
841
842                 /*
843                  * Each encoder has at most one connector (since we always steal
844                  * it away), so we won't call disable hooks twice.
845                  */
846                 drm_bridge_disable(encoder->bridge);
847
848                 /* Right function depends upon target state. */
849                 if (funcs) {
850                         if (new_conn_state->crtc && funcs->prepare)
851                                 funcs->prepare(encoder);
852                         else if (funcs->disable)
853                                 funcs->disable(encoder);
854                         else if (funcs->dpms)
855                                 funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
856                 }
857
858                 drm_bridge_post_disable(encoder->bridge);
859         }
860
861         for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
862                 const struct drm_crtc_helper_funcs *funcs;
863
864                 /* Shut down everything that needs a full modeset. */
865                 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
866                         continue;
867
868                 if (!old_crtc_state->active)
869                         continue;
870
871                 funcs = crtc->helper_private;
872
873                 DRM_DEBUG_ATOMIC("disabling [CRTC:%d:%s]\n",
874                                  crtc->base.id, crtc->name);
875
876
877                 /* Right function depends upon target state. */
878                 if (new_crtc_state->enable && funcs->prepare)
879                         funcs->prepare(crtc);
880                 else if (funcs->atomic_disable)
881                         funcs->atomic_disable(crtc, old_crtc_state);
882                 else if (funcs->disable)
883                         funcs->disable(crtc);
884                 else
885                         funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
886         }
887 }
888
889 /**
890  * drm_atomic_helper_update_legacy_modeset_state - update legacy modeset state
891  * @dev: DRM device
892  * @old_state: atomic state object with old state structures
893  *
894  * This function updates all the various legacy modeset state pointers in
895  * connectors, encoders and crtcs. It also updates the timestamping constants
896  * used for precise vblank timestamps by calling
897  * drm_calc_timestamping_constants().
898  *
899  * Drivers can use this for building their own atomic commit if they don't have
900  * a pure helper-based modeset implementation.
901  */
902 void
903 drm_atomic_helper_update_legacy_modeset_state(struct drm_device *dev,
904                                               struct drm_atomic_state *old_state)
905 {
906         struct drm_connector *connector;
907         struct drm_connector_state *old_conn_state, *new_conn_state;
908         struct drm_crtc *crtc;
909         struct drm_crtc_state *new_crtc_state;
910         int i;
911
912         /* clear out existing links and update dpms */
913         for_each_oldnew_connector_in_state(old_state, connector, old_conn_state, new_conn_state, i) {
914                 if (connector->encoder) {
915                         WARN_ON(!connector->encoder->crtc);
916
917                         connector->encoder->crtc = NULL;
918                         connector->encoder = NULL;
919                 }
920
921                 crtc = new_conn_state->crtc;
922                 if ((!crtc && old_conn_state->crtc) ||
923                     (crtc && drm_atomic_crtc_needs_modeset(crtc->state))) {
924                         int mode = DRM_MODE_DPMS_OFF;
925
926                         if (crtc && crtc->state->active)
927                                 mode = DRM_MODE_DPMS_ON;
928
929                         connector->dpms = mode;
930                 }
931         }
932
933         /* set new links */
934         for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
935                 if (!new_conn_state->crtc)
936                         continue;
937
938                 if (WARN_ON(!new_conn_state->best_encoder))
939                         continue;
940
941                 connector->encoder = new_conn_state->best_encoder;
942                 connector->encoder->crtc = new_conn_state->crtc;
943         }
944
945         /* set legacy state in the crtc structure */
946         for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
947                 struct drm_plane *primary = crtc->primary;
948                 struct drm_plane_state *new_plane_state;
949
950                 crtc->mode = new_crtc_state->mode;
951                 crtc->enabled = new_crtc_state->enable;
952
953                 new_plane_state =
954                         drm_atomic_get_new_plane_state(old_state, primary);
955
956                 if (new_plane_state && new_plane_state->crtc == crtc) {
957                         crtc->x = new_plane_state->src_x >> 16;
958                         crtc->y = new_plane_state->src_y >> 16;
959                 }
960
961                 if (new_crtc_state->enable)
962                         drm_calc_timestamping_constants(crtc,
963                                                         &new_crtc_state->adjusted_mode);
964         }
965 }
966 EXPORT_SYMBOL(drm_atomic_helper_update_legacy_modeset_state);
967
968 static void
969 crtc_set_mode(struct drm_device *dev, struct drm_atomic_state *old_state)
970 {
971         struct drm_crtc *crtc;
972         struct drm_crtc_state *new_crtc_state;
973         struct drm_connector *connector;
974         struct drm_connector_state *new_conn_state;
975         int i;
976
977         for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
978                 const struct drm_crtc_helper_funcs *funcs;
979
980                 if (!new_crtc_state->mode_changed)
981                         continue;
982
983                 funcs = crtc->helper_private;
984
985                 if (new_crtc_state->enable && funcs->mode_set_nofb) {
986                         DRM_DEBUG_ATOMIC("modeset on [CRTC:%d:%s]\n",
987                                          crtc->base.id, crtc->name);
988
989                         funcs->mode_set_nofb(crtc);
990                 }
991         }
992
993         for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
994                 const struct drm_encoder_helper_funcs *funcs;
995                 struct drm_encoder *encoder;
996                 struct drm_display_mode *mode, *adjusted_mode;
997
998                 if (!new_conn_state->best_encoder)
999                         continue;
1000
1001                 encoder = new_conn_state->best_encoder;
1002                 funcs = encoder->helper_private;
1003                 new_crtc_state = new_conn_state->crtc->state;
1004                 mode = &new_crtc_state->mode;
1005                 adjusted_mode = &new_crtc_state->adjusted_mode;
1006
1007                 if (!new_crtc_state->mode_changed)
1008                         continue;
1009
1010                 DRM_DEBUG_ATOMIC("modeset on [ENCODER:%d:%s]\n",
1011                                  encoder->base.id, encoder->name);
1012
1013                 /*
1014                  * Each encoder has at most one connector (since we always steal
1015                  * it away), so we won't call mode_set hooks twice.
1016                  */
1017                 if (funcs && funcs->atomic_mode_set) {
1018                         funcs->atomic_mode_set(encoder, new_crtc_state,
1019                                                new_conn_state);
1020                 } else if (funcs && funcs->mode_set) {
1021                         funcs->mode_set(encoder, mode, adjusted_mode);
1022                 }
1023
1024                 drm_bridge_mode_set(encoder->bridge, mode, adjusted_mode);
1025         }
1026 }
1027
1028 /**
1029  * drm_atomic_helper_commit_modeset_disables - modeset commit to disable outputs
1030  * @dev: DRM device
1031  * @old_state: atomic state object with old state structures
1032  *
1033  * This function shuts down all the outputs that need to be shut down and
1034  * prepares them (if required) with the new mode.
1035  *
1036  * For compatibility with legacy crtc helpers this should be called before
1037  * drm_atomic_helper_commit_planes(), which is what the default commit function
1038  * does. But drivers with different needs can group the modeset commits together
1039  * and do the plane commits at the end. This is useful for drivers doing runtime
1040  * PM since planes updates then only happen when the CRTC is actually enabled.
1041  */
1042 void drm_atomic_helper_commit_modeset_disables(struct drm_device *dev,
1043                                                struct drm_atomic_state *old_state)
1044 {
1045         disable_outputs(dev, old_state);
1046
1047         drm_atomic_helper_update_legacy_modeset_state(dev, old_state);
1048
1049         crtc_set_mode(dev, old_state);
1050 }
1051 EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_disables);
1052
1053 /**
1054  * drm_atomic_helper_commit_modeset_enables - modeset commit to enable outputs
1055  * @dev: DRM device
1056  * @old_state: atomic state object with old state structures
1057  *
1058  * This function enables all the outputs with the new configuration which had to
1059  * be turned off for the update.
1060  *
1061  * For compatibility with legacy crtc helpers this should be called after
1062  * drm_atomic_helper_commit_planes(), which is what the default commit function
1063  * does. But drivers with different needs can group the modeset commits together
1064  * and do the plane commits at the end. This is useful for drivers doing runtime
1065  * PM since planes updates then only happen when the CRTC is actually enabled.
1066  */
1067 void drm_atomic_helper_commit_modeset_enables(struct drm_device *dev,
1068                                               struct drm_atomic_state *old_state)
1069 {
1070         struct drm_crtc *crtc;
1071         struct drm_crtc_state *old_crtc_state;
1072         struct drm_crtc_state *new_crtc_state;
1073         struct drm_connector *connector;
1074         struct drm_connector_state *new_conn_state;
1075         int i;
1076
1077         for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
1078                 const struct drm_crtc_helper_funcs *funcs;
1079
1080                 /* Need to filter out CRTCs where only planes change. */
1081                 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
1082                         continue;
1083
1084                 if (!new_crtc_state->active)
1085                         continue;
1086
1087                 funcs = crtc->helper_private;
1088
1089                 if (new_crtc_state->enable) {
1090                         DRM_DEBUG_ATOMIC("enabling [CRTC:%d:%s]\n",
1091                                          crtc->base.id, crtc->name);
1092
1093                         if (funcs->atomic_enable)
1094                                 funcs->atomic_enable(crtc, old_crtc_state);
1095                         else
1096                                 funcs->commit(crtc);
1097                 }
1098         }
1099
1100         for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
1101                 const struct drm_encoder_helper_funcs *funcs;
1102                 struct drm_encoder *encoder;
1103
1104                 if (!new_conn_state->best_encoder)
1105                         continue;
1106
1107                 if (!new_conn_state->crtc->state->active ||
1108                     !drm_atomic_crtc_needs_modeset(new_conn_state->crtc->state))
1109                         continue;
1110
1111                 encoder = new_conn_state->best_encoder;
1112                 funcs = encoder->helper_private;
1113
1114                 DRM_DEBUG_ATOMIC("enabling [ENCODER:%d:%s]\n",
1115                                  encoder->base.id, encoder->name);
1116
1117                 /*
1118                  * Each encoder has at most one connector (since we always steal
1119                  * it away), so we won't call enable hooks twice.
1120                  */
1121                 drm_bridge_pre_enable(encoder->bridge);
1122
1123                 if (funcs) {
1124                         if (funcs->enable)
1125                                 funcs->enable(encoder);
1126                         else if (funcs->commit)
1127                                 funcs->commit(encoder);
1128                 }
1129
1130                 drm_bridge_enable(encoder->bridge);
1131         }
1132 }
1133 EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_enables);
1134
1135 /**
1136  * drm_atomic_helper_wait_for_fences - wait for fences stashed in plane state
1137  * @dev: DRM device
1138  * @state: atomic state object with old state structures
1139  * @pre_swap: If true, do an interruptible wait, and @state is the new state.
1140  *      Otherwise @state is the old state.
1141  *
1142  * For implicit sync, driver should fish the exclusive fence out from the
1143  * incoming fb's and stash it in the drm_plane_state.  This is called after
1144  * drm_atomic_helper_swap_state() so it uses the current plane state (and
1145  * just uses the atomic state to find the changed planes)
1146  *
1147  * Note that @pre_swap is needed since the point where we block for fences moves
1148  * around depending upon whether an atomic commit is blocking or
1149  * non-blocking. For non-blocking commit all waiting needs to happen after
1150  * drm_atomic_helper_swap_state() is called, but for blocking commits we want
1151  * to wait **before** we do anything that can't be easily rolled back. That is
1152  * before we call drm_atomic_helper_swap_state().
1153  *
1154  * Returns zero if success or < 0 if dma_fence_wait() fails.
1155  */
1156 int drm_atomic_helper_wait_for_fences(struct drm_device *dev,
1157                                       struct drm_atomic_state *state,
1158                                       bool pre_swap)
1159 {
1160         struct drm_plane *plane;
1161         struct drm_plane_state *new_plane_state;
1162         int i, ret;
1163
1164         for_each_new_plane_in_state(state, plane, new_plane_state, i) {
1165                 if (!new_plane_state->fence)
1166                         continue;
1167
1168                 WARN_ON(!new_plane_state->fb);
1169
1170                 /*
1171                  * If waiting for fences pre-swap (ie: nonblock), userspace can
1172                  * still interrupt the operation. Instead of blocking until the
1173                  * timer expires, make the wait interruptible.
1174                  */
1175                 ret = dma_fence_wait(new_plane_state->fence, pre_swap);
1176                 if (ret)
1177                         return ret;
1178
1179                 dma_fence_put(new_plane_state->fence);
1180                 new_plane_state->fence = NULL;
1181         }
1182
1183         return 0;
1184 }
1185 EXPORT_SYMBOL(drm_atomic_helper_wait_for_fences);
1186
1187 /**
1188  * drm_atomic_helper_wait_for_vblanks - wait for vblank on crtcs
1189  * @dev: DRM device
1190  * @old_state: atomic state object with old state structures
1191  *
1192  * Helper to, after atomic commit, wait for vblanks on all effected
1193  * crtcs (ie. before cleaning up old framebuffers using
1194  * drm_atomic_helper_cleanup_planes()). It will only wait on CRTCs where the
1195  * framebuffers have actually changed to optimize for the legacy cursor and
1196  * plane update use-case.
1197  *
1198  * Drivers using the nonblocking commit tracking support initialized by calling
1199  * drm_atomic_helper_setup_commit() should look at
1200  * drm_atomic_helper_wait_for_flip_done() as an alternative.
1201  */
1202 void
1203 drm_atomic_helper_wait_for_vblanks(struct drm_device *dev,
1204                 struct drm_atomic_state *old_state)
1205 {
1206         struct drm_crtc *crtc;
1207         struct drm_crtc_state *old_crtc_state, *new_crtc_state;
1208         int i, ret;
1209         unsigned crtc_mask = 0;
1210
1211          /*
1212           * Legacy cursor ioctls are completely unsynced, and userspace
1213           * relies on that (by doing tons of cursor updates).
1214           */
1215         if (old_state->legacy_cursor_update)
1216                 return;
1217
1218         for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
1219                 if (!new_crtc_state->active || !new_crtc_state->planes_changed)
1220                         continue;
1221
1222                 ret = drm_crtc_vblank_get(crtc);
1223                 if (ret != 0)
1224                         continue;
1225
1226                 crtc_mask |= drm_crtc_mask(crtc);
1227                 old_state->crtcs[i].last_vblank_count = drm_crtc_vblank_count(crtc);
1228         }
1229
1230         for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
1231                 if (!(crtc_mask & drm_crtc_mask(crtc)))
1232                         continue;
1233
1234                 ret = wait_event_timeout(dev->vblank[i].queue,
1235                                 old_state->crtcs[i].last_vblank_count !=
1236                                         drm_crtc_vblank_count(crtc),
1237                                 msecs_to_jiffies(50));
1238
1239                 WARN(!ret, "[CRTC:%d:%s] vblank wait timed out\n",
1240                      crtc->base.id, crtc->name);
1241
1242                 drm_crtc_vblank_put(crtc);
1243         }
1244 }
1245 EXPORT_SYMBOL(drm_atomic_helper_wait_for_vblanks);
1246
1247 /**
1248  * drm_atomic_helper_wait_for_flip_done - wait for all page flips to be done
1249  * @dev: DRM device
1250  * @old_state: atomic state object with old state structures
1251  *
1252  * Helper to, after atomic commit, wait for page flips on all effected
1253  * crtcs (ie. before cleaning up old framebuffers using
1254  * drm_atomic_helper_cleanup_planes()). Compared to
1255  * drm_atomic_helper_wait_for_vblanks() this waits for the completion of on all
1256  * CRTCs, assuming that cursors-only updates are signalling their completion
1257  * immediately (or using a different path).
1258  *
1259  * This requires that drivers use the nonblocking commit tracking support
1260  * initialized using drm_atomic_helper_setup_commit().
1261  */
1262 void drm_atomic_helper_wait_for_flip_done(struct drm_device *dev,
1263                                           struct drm_atomic_state *old_state)
1264 {
1265         struct drm_crtc_state *unused;
1266         struct drm_crtc *crtc;
1267         int i;
1268
1269         for_each_new_crtc_in_state(old_state, crtc, unused, i) {
1270                 struct drm_crtc_commit *commit = old_state->crtcs[i].commit;
1271                 int ret;
1272
1273                 if (!commit)
1274                         continue;
1275
1276                 ret = wait_for_completion_timeout(&commit->flip_done, 10 * HZ);
1277                 if (ret == 0)
1278                         DRM_ERROR("[CRTC:%d:%s] flip_done timed out\n",
1279                                   crtc->base.id, crtc->name);
1280         }
1281 }
1282 EXPORT_SYMBOL(drm_atomic_helper_wait_for_flip_done);
1283
1284 /**
1285  * drm_atomic_helper_commit_tail - commit atomic update to hardware
1286  * @old_state: atomic state object with old state structures
1287  *
1288  * This is the default implementation for the
1289  * &drm_mode_config_helper_funcs.atomic_commit_tail hook, for drivers
1290  * that do not support runtime_pm or do not need the CRTC to be
1291  * enabled to perform a commit. Otherwise, see
1292  * drm_atomic_helper_commit_tail_rpm().
1293  *
1294  * Note that the default ordering of how the various stages are called is to
1295  * match the legacy modeset helper library closest.
1296  */
1297 void drm_atomic_helper_commit_tail(struct drm_atomic_state *old_state)
1298 {
1299         struct drm_device *dev = old_state->dev;
1300
1301         drm_atomic_helper_commit_modeset_disables(dev, old_state);
1302
1303         drm_atomic_helper_commit_planes(dev, old_state, 0);
1304
1305         drm_atomic_helper_commit_modeset_enables(dev, old_state);
1306
1307         drm_atomic_helper_commit_hw_done(old_state);
1308
1309         drm_atomic_helper_wait_for_vblanks(dev, old_state);
1310
1311         drm_atomic_helper_cleanup_planes(dev, old_state);
1312 }
1313 EXPORT_SYMBOL(drm_atomic_helper_commit_tail);
1314
1315 /**
1316  * drm_atomic_helper_commit_tail_rpm - commit atomic update to hardware
1317  * @old_state: new modeset state to be committed
1318  *
1319  * This is an alternative implementation for the
1320  * &drm_mode_config_helper_funcs.atomic_commit_tail hook, for drivers
1321  * that support runtime_pm or need the CRTC to be enabled to perform a
1322  * commit. Otherwise, one should use the default implementation
1323  * drm_atomic_helper_commit_tail().
1324  */
1325 void drm_atomic_helper_commit_tail_rpm(struct drm_atomic_state *old_state)
1326 {
1327         struct drm_device *dev = old_state->dev;
1328
1329         drm_atomic_helper_commit_modeset_disables(dev, old_state);
1330
1331         drm_atomic_helper_commit_modeset_enables(dev, old_state);
1332
1333         drm_atomic_helper_commit_planes(dev, old_state,
1334                                         DRM_PLANE_COMMIT_ACTIVE_ONLY);
1335
1336         drm_atomic_helper_commit_hw_done(old_state);
1337
1338         drm_atomic_helper_wait_for_vblanks(dev, old_state);
1339
1340         drm_atomic_helper_cleanup_planes(dev, old_state);
1341 }
1342 EXPORT_SYMBOL(drm_atomic_helper_commit_tail_rpm);
1343
1344 static void commit_tail(struct drm_atomic_state *old_state)
1345 {
1346         struct drm_device *dev = old_state->dev;
1347         const struct drm_mode_config_helper_funcs *funcs;
1348
1349         funcs = dev->mode_config.helper_private;
1350
1351         drm_atomic_helper_wait_for_fences(dev, old_state, false);
1352
1353         drm_atomic_helper_wait_for_dependencies(old_state);
1354
1355         if (funcs && funcs->atomic_commit_tail)
1356                 funcs->atomic_commit_tail(old_state);
1357         else
1358                 drm_atomic_helper_commit_tail(old_state);
1359
1360         drm_atomic_helper_commit_cleanup_done(old_state);
1361
1362         drm_atomic_state_put(old_state);
1363 }
1364
1365 static void commit_work(struct work_struct *work)
1366 {
1367         struct drm_atomic_state *state = container_of(work,
1368                                                       struct drm_atomic_state,
1369                                                       commit_work);
1370         commit_tail(state);
1371 }
1372
1373 /**
1374  * drm_atomic_helper_async_check - check if state can be commited asynchronously
1375  * @dev: DRM device
1376  * @state: the driver state object
1377  *
1378  * This helper will check if it is possible to commit the state asynchronously.
1379  * Async commits are not supposed to swap the states like normal sync commits
1380  * but just do in-place changes on the current state.
1381  *
1382  * It will return 0 if the commit can happen in an asynchronous fashion or error
1383  * if not. Note that error just mean it can't be commited asynchronously, if it
1384  * fails the commit should be treated like a normal synchronous commit.
1385  */
1386 int drm_atomic_helper_async_check(struct drm_device *dev,
1387                                    struct drm_atomic_state *state)
1388 {
1389         struct drm_crtc *crtc;
1390         struct drm_crtc_state *crtc_state;
1391         struct drm_crtc_commit *commit;
1392         struct drm_plane *__plane, *plane = NULL;
1393         struct drm_plane_state *__plane_state, *plane_state = NULL;
1394         const struct drm_plane_helper_funcs *funcs;
1395         int i, j, n_planes = 0;
1396
1397         for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
1398                 if (drm_atomic_crtc_needs_modeset(crtc_state))
1399                         return -EINVAL;
1400         }
1401
1402         for_each_new_plane_in_state(state, __plane, __plane_state, i) {
1403                 n_planes++;
1404                 plane = __plane;
1405                 plane_state = __plane_state;
1406         }
1407
1408         /* FIXME: we support only single plane updates for now */
1409         if (!plane || n_planes != 1)
1410                 return -EINVAL;
1411
1412         if (!plane_state->crtc)
1413                 return -EINVAL;
1414
1415         funcs = plane->helper_private;
1416         if (!funcs->atomic_async_update)
1417                 return -EINVAL;
1418
1419         if (plane_state->fence)
1420                 return -EINVAL;
1421
1422         /*
1423          * Don't do an async update if there is an outstanding commit modifying
1424          * the plane.  This prevents our async update's changes from getting
1425          * overridden by a previous synchronous update's state.
1426          */
1427         for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
1428                 if (plane->crtc != crtc)
1429                         continue;
1430
1431                 spin_lock(&crtc->commit_lock);
1432                 commit = list_first_entry_or_null(&crtc->commit_list,
1433                                                   struct drm_crtc_commit,
1434                                                   commit_entry);
1435                 if (!commit) {
1436                         spin_unlock(&crtc->commit_lock);
1437                         continue;
1438                 }
1439                 spin_unlock(&crtc->commit_lock);
1440
1441                 if (!crtc->state->state)
1442                         continue;
1443
1444                 for_each_plane_in_state(crtc->state->state, __plane,
1445                                         __plane_state, j) {
1446                         if (__plane == plane)
1447                                 return -EINVAL;
1448                 }
1449         }
1450
1451         return funcs->atomic_async_check(plane, plane_state);
1452 }
1453 EXPORT_SYMBOL(drm_atomic_helper_async_check);
1454
1455 /**
1456  * drm_atomic_helper_async_commit - commit state asynchronously
1457  * @dev: DRM device
1458  * @state: the driver state object
1459  *
1460  * This function commits a state asynchronously, i.e., not vblank
1461  * synchronized. It should be used on a state only when
1462  * drm_atomic_async_check() succeeds. Async commits are not supposed to swap
1463  * the states like normal sync commits, but just do in-place changes on the
1464  * current state.
1465  *
1466  * TODO: Implement full swap instead of doing in-place changes.
1467  */
1468 void drm_atomic_helper_async_commit(struct drm_device *dev,
1469                                     struct drm_atomic_state *state)
1470 {
1471         struct drm_plane *plane;
1472         struct drm_plane_state *plane_state;
1473         const struct drm_plane_helper_funcs *funcs;
1474         int i;
1475
1476         for_each_new_plane_in_state(state, plane, plane_state, i) {
1477                 struct drm_framebuffer *old_fb = plane->state->fb;
1478
1479                 funcs = plane->helper_private;
1480                 funcs->atomic_async_update(plane, plane_state);
1481
1482                 /*
1483                  * Make sure the FBs have been swapped so that cleanups in the
1484                  * new_state performs a cleanup in the old FB.
1485                  */
1486                 WARN_ON_ONCE(plane_state->fb != old_fb);
1487         }
1488 }
1489 EXPORT_SYMBOL(drm_atomic_helper_async_commit);
1490
1491 /**
1492  * drm_atomic_helper_commit - commit validated state object
1493  * @dev: DRM device
1494  * @state: the driver state object
1495  * @nonblock: whether nonblocking behavior is requested.
1496  *
1497  * This function commits a with drm_atomic_helper_check() pre-validated state
1498  * object. This can still fail when e.g. the framebuffer reservation fails. This
1499  * function implements nonblocking commits, using
1500  * drm_atomic_helper_setup_commit() and related functions.
1501  *
1502  * Committing the actual hardware state is done through the
1503  * &drm_mode_config_helper_funcs.atomic_commit_tail callback, or it's default
1504  * implementation drm_atomic_helper_commit_tail().
1505  *
1506  * RETURNS:
1507  * Zero for success or -errno.
1508  */
1509 int drm_atomic_helper_commit(struct drm_device *dev,
1510                              struct drm_atomic_state *state,
1511                              bool nonblock)
1512 {
1513         int ret;
1514
1515         if (state->async_update) {
1516                 ret = drm_atomic_helper_prepare_planes(dev, state);
1517                 if (ret)
1518                         return ret;
1519
1520                 drm_atomic_helper_async_commit(dev, state);
1521                 drm_atomic_helper_cleanup_planes(dev, state);
1522
1523                 return 0;
1524         }
1525
1526         ret = drm_atomic_helper_setup_commit(state, nonblock);
1527         if (ret)
1528                 return ret;
1529
1530         INIT_WORK(&state->commit_work, commit_work);
1531
1532         ret = drm_atomic_helper_prepare_planes(dev, state);
1533         if (ret)
1534                 return ret;
1535
1536         if (!nonblock) {
1537                 ret = drm_atomic_helper_wait_for_fences(dev, state, true);
1538                 if (ret)
1539                         goto err;
1540         }
1541
1542         /*
1543          * This is the point of no return - everything below never fails except
1544          * when the hw goes bonghits. Which means we can commit the new state on
1545          * the software side now.
1546          */
1547
1548         ret = drm_atomic_helper_swap_state(state, true);
1549         if (ret)
1550                 goto err;
1551
1552         /*
1553          * Everything below can be run asynchronously without the need to grab
1554          * any modeset locks at all under one condition: It must be guaranteed
1555          * that the asynchronous work has either been cancelled (if the driver
1556          * supports it, which at least requires that the framebuffers get
1557          * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
1558          * before the new state gets committed on the software side with
1559          * drm_atomic_helper_swap_state().
1560          *
1561          * This scheme allows new atomic state updates to be prepared and
1562          * checked in parallel to the asynchronous completion of the previous
1563          * update. Which is important since compositors need to figure out the
1564          * composition of the next frame right after having submitted the
1565          * current layout.
1566          *
1567          * NOTE: Commit work has multiple phases, first hardware commit, then
1568          * cleanup. We want them to overlap, hence need system_unbound_wq to
1569          * make sure work items don't artifically stall on each another.
1570          */
1571
1572         drm_atomic_state_get(state);
1573         if (nonblock)
1574                 queue_work(system_unbound_wq, &state->commit_work);
1575         else
1576                 commit_tail(state);
1577
1578         return 0;
1579
1580 err:
1581         drm_atomic_helper_cleanup_planes(dev, state);
1582         return ret;
1583 }
1584 EXPORT_SYMBOL(drm_atomic_helper_commit);
1585
1586 /**
1587  * DOC: implementing nonblocking commit
1588  *
1589  * Nonblocking atomic commits have to be implemented in the following sequence:
1590  *
1591  * 1. Run drm_atomic_helper_prepare_planes() first. This is the only function
1592  * which commit needs to call which can fail, so we want to run it first and
1593  * synchronously.
1594  *
1595  * 2. Synchronize with any outstanding nonblocking commit worker threads which
1596  * might be affected the new state update. This can be done by either cancelling
1597  * or flushing the work items, depending upon whether the driver can deal with
1598  * cancelled updates. Note that it is important to ensure that the framebuffer
1599  * cleanup is still done when cancelling.
1600  *
1601  * Asynchronous workers need to have sufficient parallelism to be able to run
1602  * different atomic commits on different CRTCs in parallel. The simplest way to
1603  * achive this is by running them on the &system_unbound_wq work queue. Note
1604  * that drivers are not required to split up atomic commits and run an
1605  * individual commit in parallel - userspace is supposed to do that if it cares.
1606  * But it might be beneficial to do that for modesets, since those necessarily
1607  * must be done as one global operation, and enabling or disabling a CRTC can
1608  * take a long time. But even that is not required.
1609  *
1610  * 3. The software state is updated synchronously with
1611  * drm_atomic_helper_swap_state(). Doing this under the protection of all modeset
1612  * locks means concurrent callers never see inconsistent state. And doing this
1613  * while it's guaranteed that no relevant nonblocking worker runs means that
1614  * nonblocking workers do not need grab any locks. Actually they must not grab
1615  * locks, for otherwise the work flushing will deadlock.
1616  *
1617  * 4. Schedule a work item to do all subsequent steps, using the split-out
1618  * commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and
1619  * then cleaning up the framebuffers after the old framebuffer is no longer
1620  * being displayed.
1621  *
1622  * The above scheme is implemented in the atomic helper libraries in
1623  * drm_atomic_helper_commit() using a bunch of helper functions. See
1624  * drm_atomic_helper_setup_commit() for a starting point.
1625  */
1626
1627 static int stall_checks(struct drm_crtc *crtc, bool nonblock)
1628 {
1629         struct drm_crtc_commit *commit, *stall_commit = NULL;
1630         bool completed = true;
1631         int i;
1632         long ret = 0;
1633
1634         spin_lock(&crtc->commit_lock);
1635         i = 0;
1636         list_for_each_entry(commit, &crtc->commit_list, commit_entry) {
1637                 if (i == 0) {
1638                         completed = try_wait_for_completion(&commit->flip_done);
1639                         /* Userspace is not allowed to get ahead of the previous
1640                          * commit with nonblocking ones. */
1641                         if (!completed && nonblock) {
1642                                 spin_unlock(&crtc->commit_lock);
1643                                 return -EBUSY;
1644                         }
1645                 } else if (i == 1) {
1646                         stall_commit = commit;
1647                         drm_crtc_commit_get(stall_commit);
1648                         break;
1649                 }
1650
1651                 i++;
1652         }
1653         spin_unlock(&crtc->commit_lock);
1654
1655         if (!stall_commit)
1656                 return 0;
1657
1658         /* We don't want to let commits get ahead of cleanup work too much,
1659          * stalling on 2nd previous commit means triple-buffer won't ever stall.
1660          */
1661         ret = wait_for_completion_interruptible_timeout(&stall_commit->cleanup_done,
1662                                                         10*HZ);
1663         if (ret == 0)
1664                 DRM_ERROR("[CRTC:%d:%s] cleanup_done timed out\n",
1665                           crtc->base.id, crtc->name);
1666
1667         drm_crtc_commit_put(stall_commit);
1668
1669         return ret < 0 ? ret : 0;
1670 }
1671
1672 static void release_crtc_commit(struct completion *completion)
1673 {
1674         struct drm_crtc_commit *commit = container_of(completion,
1675                                                       typeof(*commit),
1676                                                       flip_done);
1677
1678         drm_crtc_commit_put(commit);
1679 }
1680
1681 /**
1682  * drm_atomic_helper_setup_commit - setup possibly nonblocking commit
1683  * @state: new modeset state to be committed
1684  * @nonblock: whether nonblocking behavior is requested.
1685  *
1686  * This function prepares @state to be used by the atomic helper's support for
1687  * nonblocking commits. Drivers using the nonblocking commit infrastructure
1688  * should always call this function from their
1689  * &drm_mode_config_funcs.atomic_commit hook.
1690  *
1691  * To be able to use this support drivers need to use a few more helper
1692  * functions. drm_atomic_helper_wait_for_dependencies() must be called before
1693  * actually committing the hardware state, and for nonblocking commits this call
1694  * must be placed in the async worker. See also drm_atomic_helper_swap_state()
1695  * and it's stall parameter, for when a driver's commit hooks look at the
1696  * &drm_crtc.state, &drm_plane.state or &drm_connector.state pointer directly.
1697  *
1698  * Completion of the hardware commit step must be signalled using
1699  * drm_atomic_helper_commit_hw_done(). After this step the driver is not allowed
1700  * to read or change any permanent software or hardware modeset state. The only
1701  * exception is state protected by other means than &drm_modeset_lock locks.
1702  * Only the free standing @state with pointers to the old state structures can
1703  * be inspected, e.g. to clean up old buffers using
1704  * drm_atomic_helper_cleanup_planes().
1705  *
1706  * At the very end, before cleaning up @state drivers must call
1707  * drm_atomic_helper_commit_cleanup_done().
1708  *
1709  * This is all implemented by in drm_atomic_helper_commit(), giving drivers a
1710  * complete and esay-to-use default implementation of the atomic_commit() hook.
1711  *
1712  * The tracking of asynchronously executed and still pending commits is done
1713  * using the core structure &drm_crtc_commit.
1714  *
1715  * By default there's no need to clean up resources allocated by this function
1716  * explicitly: drm_atomic_state_default_clear() will take care of that
1717  * automatically.
1718  *
1719  * Returns:
1720  *
1721  * 0 on success. -EBUSY when userspace schedules nonblocking commits too fast,
1722  * -ENOMEM on allocation failures and -EINTR when a signal is pending.
1723  */
1724 int drm_atomic_helper_setup_commit(struct drm_atomic_state *state,
1725                                    bool nonblock)
1726 {
1727         struct drm_crtc *crtc;
1728         struct drm_crtc_state *old_crtc_state, *new_crtc_state;
1729         struct drm_crtc_commit *commit;
1730         int i, ret;
1731
1732         for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
1733                 commit = kzalloc(sizeof(*commit), GFP_KERNEL);
1734                 if (!commit)
1735                         return -ENOMEM;
1736
1737                 init_completion(&commit->flip_done);
1738                 init_completion(&commit->hw_done);
1739                 init_completion(&commit->cleanup_done);
1740                 INIT_LIST_HEAD(&commit->commit_entry);
1741                 kref_init(&commit->ref);
1742                 commit->crtc = crtc;
1743
1744                 state->crtcs[i].commit = commit;
1745
1746                 ret = stall_checks(crtc, nonblock);
1747                 if (ret)
1748                         return ret;
1749
1750                 /* Drivers only send out events when at least either current or
1751                  * new CRTC state is active. Complete right away if everything
1752                  * stays off. */
1753                 if (!old_crtc_state->active && !new_crtc_state->active) {
1754                         complete_all(&commit->flip_done);
1755                         continue;
1756                 }
1757
1758                 /* Legacy cursor updates are fully unsynced. */
1759                 if (state->legacy_cursor_update) {
1760                         complete_all(&commit->flip_done);
1761                         continue;
1762                 }
1763
1764                 if (!new_crtc_state->event) {
1765                         commit->event = kzalloc(sizeof(*commit->event),
1766                                                 GFP_KERNEL);
1767                         if (!commit->event)
1768                                 return -ENOMEM;
1769
1770                         new_crtc_state->event = commit->event;
1771                 }
1772
1773                 new_crtc_state->event->base.completion = &commit->flip_done;
1774                 new_crtc_state->event->base.completion_release = release_crtc_commit;
1775                 drm_crtc_commit_get(commit);
1776         }
1777
1778         return 0;
1779 }
1780 EXPORT_SYMBOL(drm_atomic_helper_setup_commit);
1781
1782
1783 static struct drm_crtc_commit *preceeding_commit(struct drm_crtc *crtc)
1784 {
1785         struct drm_crtc_commit *commit;
1786         int i = 0;
1787
1788         list_for_each_entry(commit, &crtc->commit_list, commit_entry) {
1789                 /* skip the first entry, that's the current commit */
1790                 if (i == 1)
1791                         return commit;
1792                 i++;
1793         }
1794
1795         return NULL;
1796 }
1797
1798 /**
1799  * drm_atomic_helper_wait_for_dependencies - wait for required preceeding commits
1800  * @old_state: atomic state object with old state structures
1801  *
1802  * This function waits for all preceeding commits that touch the same CRTC as
1803  * @old_state to both be committed to the hardware (as signalled by
1804  * drm_atomic_helper_commit_hw_done) and executed by the hardware (as signalled
1805  * by calling drm_crtc_vblank_send_event() on the &drm_crtc_state.event).
1806  *
1807  * This is part of the atomic helper support for nonblocking commits, see
1808  * drm_atomic_helper_setup_commit() for an overview.
1809  */
1810 void drm_atomic_helper_wait_for_dependencies(struct drm_atomic_state *old_state)
1811 {
1812         struct drm_crtc *crtc;
1813         struct drm_crtc_state *new_crtc_state;
1814         struct drm_crtc_commit *commit;
1815         int i;
1816         long ret;
1817
1818         for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
1819                 spin_lock(&crtc->commit_lock);
1820                 commit = preceeding_commit(crtc);
1821                 if (commit)
1822                         drm_crtc_commit_get(commit);
1823                 spin_unlock(&crtc->commit_lock);
1824
1825                 if (!commit)
1826                         continue;
1827
1828                 ret = wait_for_completion_timeout(&commit->hw_done,
1829                                                   10*HZ);
1830                 if (ret == 0)
1831                         DRM_ERROR("[CRTC:%d:%s] hw_done timed out\n",
1832                                   crtc->base.id, crtc->name);
1833
1834                 /* Currently no support for overwriting flips, hence
1835                  * stall for previous one to execute completely. */
1836                 ret = wait_for_completion_timeout(&commit->flip_done,
1837                                                   10*HZ);
1838                 if (ret == 0)
1839                         DRM_ERROR("[CRTC:%d:%s] flip_done timed out\n",
1840                                   crtc->base.id, crtc->name);
1841
1842                 drm_crtc_commit_put(commit);
1843         }
1844 }
1845 EXPORT_SYMBOL(drm_atomic_helper_wait_for_dependencies);
1846
1847 /**
1848  * drm_atomic_helper_commit_hw_done - setup possible nonblocking commit
1849  * @old_state: atomic state object with old state structures
1850  *
1851  * This function is used to signal completion of the hardware commit step. After
1852  * this step the driver is not allowed to read or change any permanent software
1853  * or hardware modeset state. The only exception is state protected by other
1854  * means than &drm_modeset_lock locks.
1855  *
1856  * Drivers should try to postpone any expensive or delayed cleanup work after
1857  * this function is called.
1858  *
1859  * This is part of the atomic helper support for nonblocking commits, see
1860  * drm_atomic_helper_setup_commit() for an overview.
1861  */
1862 void drm_atomic_helper_commit_hw_done(struct drm_atomic_state *old_state)
1863 {
1864         struct drm_crtc *crtc;
1865         struct drm_crtc_state *new_crtc_state;
1866         struct drm_crtc_commit *commit;
1867         int i;
1868
1869         for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
1870                 commit = old_state->crtcs[i].commit;
1871                 if (!commit)
1872                         continue;
1873
1874                 /* backend must have consumed any event by now */
1875                 WARN_ON(new_crtc_state->event);
1876                 complete_all(&commit->hw_done);
1877         }
1878 }
1879 EXPORT_SYMBOL(drm_atomic_helper_commit_hw_done);
1880
1881 /**
1882  * drm_atomic_helper_commit_cleanup_done - signal completion of commit
1883  * @old_state: atomic state object with old state structures
1884  *
1885  * This signals completion of the atomic update @old_state, including any
1886  * cleanup work. If used, it must be called right before calling
1887  * drm_atomic_state_put().
1888  *
1889  * This is part of the atomic helper support for nonblocking commits, see
1890  * drm_atomic_helper_setup_commit() for an overview.
1891  */
1892 void drm_atomic_helper_commit_cleanup_done(struct drm_atomic_state *old_state)
1893 {
1894         struct drm_crtc *crtc;
1895         struct drm_crtc_state *new_crtc_state;
1896         struct drm_crtc_commit *commit;
1897         int i;
1898         long ret;
1899
1900         for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
1901                 commit = old_state->crtcs[i].commit;
1902                 if (WARN_ON(!commit))
1903                         continue;
1904
1905                 complete_all(&commit->cleanup_done);
1906                 WARN_ON(!try_wait_for_completion(&commit->hw_done));
1907
1908                 /* commit_list borrows our reference, need to remove before we
1909                  * clean up our drm_atomic_state. But only after it actually
1910                  * completed, otherwise subsequent commits won't stall properly. */
1911                 if (try_wait_for_completion(&commit->flip_done))
1912                         goto del_commit;
1913
1914                 /* We must wait for the vblank event to signal our completion
1915                  * before releasing our reference, since the vblank work does
1916                  * not hold a reference of its own. */
1917                 ret = wait_for_completion_timeout(&commit->flip_done,
1918                                                   10*HZ);
1919                 if (ret == 0)
1920                         DRM_ERROR("[CRTC:%d:%s] flip_done timed out\n",
1921                                   crtc->base.id, crtc->name);
1922
1923 del_commit:
1924                 spin_lock(&crtc->commit_lock);
1925                 list_del(&commit->commit_entry);
1926                 spin_unlock(&crtc->commit_lock);
1927         }
1928 }
1929 EXPORT_SYMBOL(drm_atomic_helper_commit_cleanup_done);
1930
1931 /**
1932  * drm_atomic_helper_prepare_planes - prepare plane resources before commit
1933  * @dev: DRM device
1934  * @state: atomic state object with new state structures
1935  *
1936  * This function prepares plane state, specifically framebuffers, for the new
1937  * configuration, by calling &drm_plane_helper_funcs.prepare_fb. If any failure
1938  * is encountered this function will call &drm_plane_helper_funcs.cleanup_fb on
1939  * any already successfully prepared framebuffer.
1940  *
1941  * Returns:
1942  * 0 on success, negative error code on failure.
1943  */
1944 int drm_atomic_helper_prepare_planes(struct drm_device *dev,
1945                                      struct drm_atomic_state *state)
1946 {
1947         struct drm_plane *plane;
1948         struct drm_plane_state *new_plane_state;
1949         int ret, i, j;
1950
1951         for_each_new_plane_in_state(state, plane, new_plane_state, i) {
1952                 const struct drm_plane_helper_funcs *funcs;
1953
1954                 funcs = plane->helper_private;
1955
1956                 if (funcs->prepare_fb) {
1957                         ret = funcs->prepare_fb(plane, new_plane_state);
1958                         if (ret)
1959                                 goto fail;
1960                 }
1961         }
1962
1963         return 0;
1964
1965 fail:
1966         for_each_new_plane_in_state(state, plane, new_plane_state, j) {
1967                 const struct drm_plane_helper_funcs *funcs;
1968
1969                 if (j >= i)
1970                         continue;
1971
1972                 funcs = plane->helper_private;
1973
1974                 if (funcs->cleanup_fb)
1975                         funcs->cleanup_fb(plane, new_plane_state);
1976         }
1977
1978         return ret;
1979 }
1980 EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
1981
1982 static bool plane_crtc_active(const struct drm_plane_state *state)
1983 {
1984         return state->crtc && state->crtc->state->active;
1985 }
1986
1987 /**
1988  * drm_atomic_helper_commit_planes - commit plane state
1989  * @dev: DRM device
1990  * @old_state: atomic state object with old state structures
1991  * @flags: flags for committing plane state
1992  *
1993  * This function commits the new plane state using the plane and atomic helper
1994  * functions for planes and crtcs. It assumes that the atomic state has already
1995  * been pushed into the relevant object state pointers, since this step can no
1996  * longer fail.
1997  *
1998  * It still requires the global state object @old_state to know which planes and
1999  * crtcs need to be updated though.
2000  *
2001  * Note that this function does all plane updates across all CRTCs in one step.
2002  * If the hardware can't support this approach look at
2003  * drm_atomic_helper_commit_planes_on_crtc() instead.
2004  *
2005  * Plane parameters can be updated by applications while the associated CRTC is
2006  * disabled. The DRM/KMS core will store the parameters in the plane state,
2007  * which will be available to the driver when the CRTC is turned on. As a result
2008  * most drivers don't need to be immediately notified of plane updates for a
2009  * disabled CRTC.
2010  *
2011  * Unless otherwise needed, drivers are advised to set the ACTIVE_ONLY flag in
2012  * @flags in order not to receive plane update notifications related to a
2013  * disabled CRTC. This avoids the need to manually ignore plane updates in
2014  * driver code when the driver and/or hardware can't or just don't need to deal
2015  * with updates on disabled CRTCs, for example when supporting runtime PM.
2016  *
2017  * Drivers may set the NO_DISABLE_AFTER_MODESET flag in @flags if the relevant
2018  * display controllers require to disable a CRTC's planes when the CRTC is
2019  * disabled. This function would skip the &drm_plane_helper_funcs.atomic_disable
2020  * call for a plane if the CRTC of the old plane state needs a modesetting
2021  * operation. Of course, the drivers need to disable the planes in their CRTC
2022  * disable callbacks since no one else would do that.
2023  *
2024  * The drm_atomic_helper_commit() default implementation doesn't set the
2025  * ACTIVE_ONLY flag to most closely match the behaviour of the legacy helpers.
2026  * This should not be copied blindly by drivers.
2027  */
2028 void drm_atomic_helper_commit_planes(struct drm_device *dev,
2029                                      struct drm_atomic_state *old_state,
2030                                      uint32_t flags)
2031 {
2032         struct drm_crtc *crtc;
2033         struct drm_crtc_state *old_crtc_state, *new_crtc_state;
2034         struct drm_plane *plane;
2035         struct drm_plane_state *old_plane_state, *new_plane_state;
2036         int i;
2037         bool active_only = flags & DRM_PLANE_COMMIT_ACTIVE_ONLY;
2038         bool no_disable = flags & DRM_PLANE_COMMIT_NO_DISABLE_AFTER_MODESET;
2039
2040         for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
2041                 const struct drm_crtc_helper_funcs *funcs;
2042
2043                 funcs = crtc->helper_private;
2044
2045                 if (!funcs || !funcs->atomic_begin)
2046                         continue;
2047
2048                 if (active_only && !new_crtc_state->active)
2049                         continue;
2050
2051                 funcs->atomic_begin(crtc, old_crtc_state);
2052         }
2053
2054         for_each_oldnew_plane_in_state(old_state, plane, old_plane_state, new_plane_state, i) {
2055                 const struct drm_plane_helper_funcs *funcs;
2056                 bool disabling;
2057
2058                 funcs = plane->helper_private;
2059
2060                 if (!funcs)
2061                         continue;
2062
2063                 disabling = drm_atomic_plane_disabling(old_plane_state,
2064                                                        new_plane_state);
2065
2066                 if (active_only) {
2067                         /*
2068                          * Skip planes related to inactive CRTCs. If the plane
2069                          * is enabled use the state of the current CRTC. If the
2070                          * plane is being disabled use the state of the old
2071                          * CRTC to avoid skipping planes being disabled on an
2072                          * active CRTC.
2073                          */
2074                         if (!disabling && !plane_crtc_active(new_plane_state))
2075                                 continue;
2076                         if (disabling && !plane_crtc_active(old_plane_state))
2077                                 continue;
2078                 }
2079
2080                 /*
2081                  * Special-case disabling the plane if drivers support it.
2082                  */
2083                 if (disabling && funcs->atomic_disable) {
2084                         struct drm_crtc_state *crtc_state;
2085
2086                         crtc_state = old_plane_state->crtc->state;
2087
2088                         if (drm_atomic_crtc_needs_modeset(crtc_state) &&
2089                             no_disable)
2090                                 continue;
2091
2092                         funcs->atomic_disable(plane, old_plane_state);
2093                 } else if (new_plane_state->crtc || disabling) {
2094                         funcs->atomic_update(plane, old_plane_state);
2095                 }
2096         }
2097
2098         for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
2099                 const struct drm_crtc_helper_funcs *funcs;
2100
2101                 funcs = crtc->helper_private;
2102
2103                 if (!funcs || !funcs->atomic_flush)
2104                         continue;
2105
2106                 if (active_only && !new_crtc_state->active)
2107                         continue;
2108
2109                 funcs->atomic_flush(crtc, old_crtc_state);
2110         }
2111 }
2112 EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
2113
2114 /**
2115  * drm_atomic_helper_commit_planes_on_crtc - commit plane state for a crtc
2116  * @old_crtc_state: atomic state object with the old crtc state
2117  *
2118  * This function commits the new plane state using the plane and atomic helper
2119  * functions for planes on the specific crtc. It assumes that the atomic state
2120  * has already been pushed into the relevant object state pointers, since this
2121  * step can no longer fail.
2122  *
2123  * This function is useful when plane updates should be done crtc-by-crtc
2124  * instead of one global step like drm_atomic_helper_commit_planes() does.
2125  *
2126  * This function can only be savely used when planes are not allowed to move
2127  * between different CRTCs because this function doesn't handle inter-CRTC
2128  * depencies. Callers need to ensure that either no such depencies exist,
2129  * resolve them through ordering of commit calls or through some other means.
2130  */
2131 void
2132 drm_atomic_helper_commit_planes_on_crtc(struct drm_crtc_state *old_crtc_state)
2133 {
2134         const struct drm_crtc_helper_funcs *crtc_funcs;
2135         struct drm_crtc *crtc = old_crtc_state->crtc;
2136         struct drm_atomic_state *old_state = old_crtc_state->state;
2137         struct drm_plane *plane;
2138         unsigned plane_mask;
2139
2140         plane_mask = old_crtc_state->plane_mask;
2141         plane_mask |= crtc->state->plane_mask;
2142
2143         crtc_funcs = crtc->helper_private;
2144         if (crtc_funcs && crtc_funcs->atomic_begin)
2145                 crtc_funcs->atomic_begin(crtc, old_crtc_state);
2146
2147         drm_for_each_plane_mask(plane, crtc->dev, plane_mask) {
2148                 struct drm_plane_state *old_plane_state =
2149                         drm_atomic_get_old_plane_state(old_state, plane);
2150                 const struct drm_plane_helper_funcs *plane_funcs;
2151
2152                 plane_funcs = plane->helper_private;
2153
2154                 if (!old_plane_state || !plane_funcs)
2155                         continue;
2156
2157                 WARN_ON(plane->state->crtc && plane->state->crtc != crtc);
2158
2159                 if (drm_atomic_plane_disabling(old_plane_state, plane->state) &&
2160                     plane_funcs->atomic_disable)
2161                         plane_funcs->atomic_disable(plane, old_plane_state);
2162                 else if (plane->state->crtc ||
2163                          drm_atomic_plane_disabling(old_plane_state, plane->state))
2164                         plane_funcs->atomic_update(plane, old_plane_state);
2165         }
2166
2167         if (crtc_funcs && crtc_funcs->atomic_flush)
2168                 crtc_funcs->atomic_flush(crtc, old_crtc_state);
2169 }
2170 EXPORT_SYMBOL(drm_atomic_helper_commit_planes_on_crtc);
2171
2172 /**
2173  * drm_atomic_helper_disable_planes_on_crtc - helper to disable CRTC's planes
2174  * @old_crtc_state: atomic state object with the old CRTC state
2175  * @atomic: if set, synchronize with CRTC's atomic_begin/flush hooks
2176  *
2177  * Disables all planes associated with the given CRTC. This can be
2178  * used for instance in the CRTC helper atomic_disable callback to disable
2179  * all planes.
2180  *
2181  * If the atomic-parameter is set the function calls the CRTC's
2182  * atomic_begin hook before and atomic_flush hook after disabling the
2183  * planes.
2184  *
2185  * It is a bug to call this function without having implemented the
2186  * &drm_plane_helper_funcs.atomic_disable plane hook.
2187  */
2188 void
2189 drm_atomic_helper_disable_planes_on_crtc(struct drm_crtc_state *old_crtc_state,
2190                                          bool atomic)
2191 {
2192         struct drm_crtc *crtc = old_crtc_state->crtc;
2193         const struct drm_crtc_helper_funcs *crtc_funcs =
2194                 crtc->helper_private;
2195         struct drm_plane *plane;
2196
2197         if (atomic && crtc_funcs && crtc_funcs->atomic_begin)
2198                 crtc_funcs->atomic_begin(crtc, NULL);
2199
2200         drm_atomic_crtc_state_for_each_plane(plane, old_crtc_state) {
2201                 const struct drm_plane_helper_funcs *plane_funcs =
2202                         plane->helper_private;
2203
2204                 if (!plane_funcs)
2205                         continue;
2206
2207                 WARN_ON(!plane_funcs->atomic_disable);
2208                 if (plane_funcs->atomic_disable)
2209                         plane_funcs->atomic_disable(plane, NULL);
2210         }
2211
2212         if (atomic && crtc_funcs && crtc_funcs->atomic_flush)
2213                 crtc_funcs->atomic_flush(crtc, NULL);
2214 }
2215 EXPORT_SYMBOL(drm_atomic_helper_disable_planes_on_crtc);
2216
2217 /**
2218  * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
2219  * @dev: DRM device
2220  * @old_state: atomic state object with old state structures
2221  *
2222  * This function cleans up plane state, specifically framebuffers, from the old
2223  * configuration. Hence the old configuration must be perserved in @old_state to
2224  * be able to call this function.
2225  *
2226  * This function must also be called on the new state when the atomic update
2227  * fails at any point after calling drm_atomic_helper_prepare_planes().
2228  */
2229 void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
2230                                       struct drm_atomic_state *old_state)
2231 {
2232         struct drm_plane *plane;
2233         struct drm_plane_state *old_plane_state, *new_plane_state;
2234         int i;
2235
2236         for_each_oldnew_plane_in_state(old_state, plane, old_plane_state, new_plane_state, i) {
2237                 const struct drm_plane_helper_funcs *funcs;
2238                 struct drm_plane_state *plane_state;
2239
2240                 /*
2241                  * This might be called before swapping when commit is aborted,
2242                  * in which case we have to cleanup the new state.
2243                  */
2244                 if (old_plane_state == plane->state)
2245                         plane_state = new_plane_state;
2246                 else
2247                         plane_state = old_plane_state;
2248
2249                 funcs = plane->helper_private;
2250
2251                 if (funcs->cleanup_fb)
2252                         funcs->cleanup_fb(plane, plane_state);
2253         }
2254 }
2255 EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
2256
2257 /**
2258  * drm_atomic_helper_swap_state - store atomic state into current sw state
2259  * @state: atomic state
2260  * @stall: stall for preceeding commits
2261  *
2262  * This function stores the atomic state into the current state pointers in all
2263  * driver objects. It should be called after all failing steps have been done
2264  * and succeeded, but before the actual hardware state is committed.
2265  *
2266  * For cleanup and error recovery the current state for all changed objects will
2267  * be swapped into @state.
2268  *
2269  * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
2270  *
2271  * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
2272  *
2273  * 2. Do any other steps that might fail.
2274  *
2275  * 3. Put the staged state into the current state pointers with this function.
2276  *
2277  * 4. Actually commit the hardware state.
2278  *
2279  * 5. Call drm_atomic_helper_cleanup_planes() with @state, which since step 3
2280  * contains the old state. Also do any other cleanup required with that state.
2281  *
2282  * @stall must be set when nonblocking commits for this driver directly access
2283  * the &drm_plane.state, &drm_crtc.state or &drm_connector.state pointer. With
2284  * the current atomic helpers this is almost always the case, since the helpers
2285  * don't pass the right state structures to the callbacks.
2286  *
2287  * Returns:
2288  *
2289  * Returns 0 on success. Can return -ERESTARTSYS when @stall is true and the
2290  * waiting for the previous commits has been interrupted.
2291  */
2292 int drm_atomic_helper_swap_state(struct drm_atomic_state *state,
2293                                   bool stall)
2294 {
2295         int i, ret;
2296         struct drm_connector *connector;
2297         struct drm_connector_state *old_conn_state, *new_conn_state;
2298         struct drm_crtc *crtc;
2299         struct drm_crtc_state *old_crtc_state, *new_crtc_state;
2300         struct drm_plane *plane;
2301         struct drm_plane_state *old_plane_state, *new_plane_state;
2302         struct drm_crtc_commit *commit;
2303         struct drm_private_obj *obj;
2304         struct drm_private_state *old_obj_state, *new_obj_state;
2305
2306         if (stall) {
2307                 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
2308                         spin_lock(&crtc->commit_lock);
2309                         commit = list_first_entry_or_null(&crtc->commit_list,
2310                                         struct drm_crtc_commit, commit_entry);
2311                         if (commit)
2312                                 drm_crtc_commit_get(commit);
2313                         spin_unlock(&crtc->commit_lock);
2314
2315                         if (!commit)
2316                                 continue;
2317
2318                         ret = wait_for_completion_interruptible(&commit->hw_done);
2319                         drm_crtc_commit_put(commit);
2320
2321                         if (ret)
2322                                 return ret;
2323                 }
2324         }
2325
2326         for_each_oldnew_connector_in_state(state, connector, old_conn_state, new_conn_state, i) {
2327                 WARN_ON(connector->state != old_conn_state);
2328
2329                 old_conn_state->state = state;
2330                 new_conn_state->state = NULL;
2331
2332                 state->connectors[i].state = old_conn_state;
2333                 connector->state = new_conn_state;
2334         }
2335
2336         for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
2337                 WARN_ON(crtc->state != old_crtc_state);
2338
2339                 old_crtc_state->state = state;
2340                 new_crtc_state->state = NULL;
2341
2342                 state->crtcs[i].state = old_crtc_state;
2343                 crtc->state = new_crtc_state;
2344
2345                 if (state->crtcs[i].commit) {
2346                         spin_lock(&crtc->commit_lock);
2347                         list_add(&state->crtcs[i].commit->commit_entry,
2348                                  &crtc->commit_list);
2349                         spin_unlock(&crtc->commit_lock);
2350
2351                         state->crtcs[i].commit->event = NULL;
2352                 }
2353         }
2354
2355         for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
2356                 WARN_ON(plane->state != old_plane_state);
2357
2358                 old_plane_state->state = state;
2359                 new_plane_state->state = NULL;
2360
2361                 state->planes[i].state = old_plane_state;
2362                 plane->state = new_plane_state;
2363         }
2364
2365         for_each_oldnew_private_obj_in_state(state, obj, old_obj_state, new_obj_state, i) {
2366                 WARN_ON(obj->state != old_obj_state);
2367
2368                 old_obj_state->state = state;
2369                 new_obj_state->state = NULL;
2370
2371                 state->private_objs[i].state = old_obj_state;
2372                 obj->state = new_obj_state;
2373         }
2374
2375         return 0;
2376 }
2377 EXPORT_SYMBOL(drm_atomic_helper_swap_state);
2378
2379 /**
2380  * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
2381  * @plane: plane object to update
2382  * @crtc: owning CRTC of owning plane
2383  * @fb: framebuffer to flip onto plane
2384  * @crtc_x: x offset of primary plane on crtc
2385  * @crtc_y: y offset of primary plane on crtc
2386  * @crtc_w: width of primary plane rectangle on crtc
2387  * @crtc_h: height of primary plane rectangle on crtc
2388  * @src_x: x offset of @fb for panning
2389  * @src_y: y offset of @fb for panning
2390  * @src_w: width of source rectangle in @fb
2391  * @src_h: height of source rectangle in @fb
2392  * @ctx: lock acquire context
2393  *
2394  * Provides a default plane update handler using the atomic driver interface.
2395  *
2396  * RETURNS:
2397  * Zero on success, error code on failure
2398  */
2399 int drm_atomic_helper_update_plane(struct drm_plane *plane,
2400                                    struct drm_crtc *crtc,
2401                                    struct drm_framebuffer *fb,
2402                                    int crtc_x, int crtc_y,
2403                                    unsigned int crtc_w, unsigned int crtc_h,
2404                                    uint32_t src_x, uint32_t src_y,
2405                                    uint32_t src_w, uint32_t src_h,
2406                                    struct drm_modeset_acquire_ctx *ctx)
2407 {
2408         struct drm_atomic_state *state;
2409         struct drm_plane_state *plane_state;
2410         int ret = 0;
2411
2412         state = drm_atomic_state_alloc(plane->dev);
2413         if (!state)
2414                 return -ENOMEM;
2415
2416         state->acquire_ctx = ctx;
2417         plane_state = drm_atomic_get_plane_state(state, plane);
2418         if (IS_ERR(plane_state)) {
2419                 ret = PTR_ERR(plane_state);
2420                 goto fail;
2421         }
2422
2423         ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
2424         if (ret != 0)
2425                 goto fail;
2426         drm_atomic_set_fb_for_plane(plane_state, fb);
2427         plane_state->crtc_x = crtc_x;
2428         plane_state->crtc_y = crtc_y;
2429         plane_state->crtc_w = crtc_w;
2430         plane_state->crtc_h = crtc_h;
2431         plane_state->src_x = src_x;
2432         plane_state->src_y = src_y;
2433         plane_state->src_w = src_w;
2434         plane_state->src_h = src_h;
2435
2436         if (plane == crtc->cursor)
2437                 state->legacy_cursor_update = true;
2438
2439         ret = drm_atomic_commit(state);
2440 fail:
2441         drm_atomic_state_put(state);
2442         return ret;
2443 }
2444 EXPORT_SYMBOL(drm_atomic_helper_update_plane);
2445
2446 /**
2447  * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
2448  * @plane: plane to disable
2449  * @ctx: lock acquire context
2450  *
2451  * Provides a default plane disable handler using the atomic driver interface.
2452  *
2453  * RETURNS:
2454  * Zero on success, error code on failure
2455  */
2456 int drm_atomic_helper_disable_plane(struct drm_plane *plane,
2457                                     struct drm_modeset_acquire_ctx *ctx)
2458 {
2459         struct drm_atomic_state *state;
2460         struct drm_plane_state *plane_state;
2461         int ret = 0;
2462
2463         state = drm_atomic_state_alloc(plane->dev);
2464         if (!state)
2465                 return -ENOMEM;
2466
2467         state->acquire_ctx = ctx;
2468         plane_state = drm_atomic_get_plane_state(state, plane);
2469         if (IS_ERR(plane_state)) {
2470                 ret = PTR_ERR(plane_state);
2471                 goto fail;
2472         }
2473
2474         if (plane_state->crtc && (plane == plane->crtc->cursor))
2475                 plane_state->state->legacy_cursor_update = true;
2476
2477         ret = __drm_atomic_helper_disable_plane(plane, plane_state);
2478         if (ret != 0)
2479                 goto fail;
2480
2481         ret = drm_atomic_commit(state);
2482 fail:
2483         drm_atomic_state_put(state);
2484         return ret;
2485 }
2486 EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
2487
2488 /* just used from fb-helper and atomic-helper: */
2489 int __drm_atomic_helper_disable_plane(struct drm_plane *plane,
2490                 struct drm_plane_state *plane_state)
2491 {
2492         int ret;
2493
2494         ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
2495         if (ret != 0)
2496                 return ret;
2497
2498         drm_atomic_set_fb_for_plane(plane_state, NULL);
2499         plane_state->crtc_x = 0;
2500         plane_state->crtc_y = 0;
2501         plane_state->crtc_w = 0;
2502         plane_state->crtc_h = 0;
2503         plane_state->src_x = 0;
2504         plane_state->src_y = 0;
2505         plane_state->src_w = 0;
2506         plane_state->src_h = 0;
2507
2508         return 0;
2509 }
2510
2511 static int update_output_state(struct drm_atomic_state *state,
2512                                struct drm_mode_set *set)
2513 {
2514         struct drm_device *dev = set->crtc->dev;
2515         struct drm_crtc *crtc;
2516         struct drm_crtc_state *new_crtc_state;
2517         struct drm_connector *connector;
2518         struct drm_connector_state *new_conn_state;
2519         int ret, i;
2520
2521         ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
2522                                state->acquire_ctx);
2523         if (ret)
2524                 return ret;
2525
2526         /* First disable all connectors on the target crtc. */
2527         ret = drm_atomic_add_affected_connectors(state, set->crtc);
2528         if (ret)
2529                 return ret;
2530
2531         for_each_new_connector_in_state(state, connector, new_conn_state, i) {
2532                 if (new_conn_state->crtc == set->crtc) {
2533                         ret = drm_atomic_set_crtc_for_connector(new_conn_state,
2534                                                                 NULL);
2535                         if (ret)
2536                                 return ret;
2537
2538                         /* Make sure legacy setCrtc always re-trains */
2539                         new_conn_state->link_status = DRM_LINK_STATUS_GOOD;
2540                 }
2541         }
2542
2543         /* Then set all connectors from set->connectors on the target crtc */
2544         for (i = 0; i < set->num_connectors; i++) {
2545                 new_conn_state = drm_atomic_get_connector_state(state,
2546                                                             set->connectors[i]);
2547                 if (IS_ERR(new_conn_state))
2548                         return PTR_ERR(new_conn_state);
2549
2550                 ret = drm_atomic_set_crtc_for_connector(new_conn_state,
2551                                                         set->crtc);
2552                 if (ret)
2553                         return ret;
2554         }
2555
2556         for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
2557                 /* Don't update ->enable for the CRTC in the set_config request,
2558                  * since a mismatch would indicate a bug in the upper layers.
2559                  * The actual modeset code later on will catch any
2560                  * inconsistencies here. */
2561                 if (crtc == set->crtc)
2562                         continue;
2563
2564                 if (!new_crtc_state->connector_mask) {
2565                         ret = drm_atomic_set_mode_prop_for_crtc(new_crtc_state,
2566                                                                 NULL);
2567                         if (ret < 0)
2568                                 return ret;
2569
2570                         new_crtc_state->active = false;
2571                 }
2572         }
2573
2574         return 0;
2575 }
2576
2577 /**
2578  * drm_atomic_helper_set_config - set a new config from userspace
2579  * @set: mode set configuration
2580  * @ctx: lock acquisition context
2581  *
2582  * Provides a default crtc set_config handler using the atomic driver interface.
2583  *
2584  * NOTE: For backwards compatibility with old userspace this automatically
2585  * resets the "link-status" property to GOOD, to force any link
2586  * re-training. The SETCRTC ioctl does not define whether an update does
2587  * need a full modeset or just a plane update, hence we're allowed to do
2588  * that. See also drm_mode_connector_set_link_status_property().
2589  *
2590  * Returns:
2591  * Returns 0 on success, negative errno numbers on failure.
2592  */
2593 int drm_atomic_helper_set_config(struct drm_mode_set *set,
2594                                  struct drm_modeset_acquire_ctx *ctx)
2595 {
2596         struct drm_atomic_state *state;
2597         struct drm_crtc *crtc = set->crtc;
2598         int ret = 0;
2599
2600         state = drm_atomic_state_alloc(crtc->dev);
2601         if (!state)
2602                 return -ENOMEM;
2603
2604         state->acquire_ctx = ctx;
2605         ret = __drm_atomic_helper_set_config(set, state);
2606         if (ret != 0)
2607                 goto fail;
2608
2609         ret = handle_conflicting_encoders(state, true);
2610         if (ret)
2611                 goto fail;
2612
2613         ret = drm_atomic_commit(state);
2614
2615 fail:
2616         drm_atomic_state_put(state);
2617         return ret;
2618 }
2619 EXPORT_SYMBOL(drm_atomic_helper_set_config);
2620
2621 /* just used from fb-helper and atomic-helper: */
2622 int __drm_atomic_helper_set_config(struct drm_mode_set *set,
2623                 struct drm_atomic_state *state)
2624 {
2625         struct drm_crtc_state *crtc_state;
2626         struct drm_plane_state *primary_state;
2627         struct drm_crtc *crtc = set->crtc;
2628         int hdisplay, vdisplay;
2629         int ret;
2630
2631         crtc_state = drm_atomic_get_crtc_state(state, crtc);
2632         if (IS_ERR(crtc_state))
2633                 return PTR_ERR(crtc_state);
2634
2635         primary_state = drm_atomic_get_plane_state(state, crtc->primary);
2636         if (IS_ERR(primary_state))
2637                 return PTR_ERR(primary_state);
2638
2639         if (!set->mode) {
2640                 WARN_ON(set->fb);
2641                 WARN_ON(set->num_connectors);
2642
2643                 ret = drm_atomic_set_mode_for_crtc(crtc_state, NULL);
2644                 if (ret != 0)
2645                         return ret;
2646
2647                 crtc_state->active = false;
2648
2649                 ret = drm_atomic_set_crtc_for_plane(primary_state, NULL);
2650                 if (ret != 0)
2651                         return ret;
2652
2653                 drm_atomic_set_fb_for_plane(primary_state, NULL);
2654
2655                 goto commit;
2656         }
2657
2658         WARN_ON(!set->fb);
2659         WARN_ON(!set->num_connectors);
2660
2661         ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
2662         if (ret != 0)
2663                 return ret;
2664
2665         crtc_state->active = true;
2666
2667         ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
2668         if (ret != 0)
2669                 return ret;
2670
2671         drm_mode_get_hv_timing(set->mode, &hdisplay, &vdisplay);
2672
2673         drm_atomic_set_fb_for_plane(primary_state, set->fb);
2674         primary_state->crtc_x = 0;
2675         primary_state->crtc_y = 0;
2676         primary_state->crtc_w = hdisplay;
2677         primary_state->crtc_h = vdisplay;
2678         primary_state->src_x = set->x << 16;
2679         primary_state->src_y = set->y << 16;
2680         if (drm_rotation_90_or_270(primary_state->rotation)) {
2681                 primary_state->src_w = vdisplay << 16;
2682                 primary_state->src_h = hdisplay << 16;
2683         } else {
2684                 primary_state->src_w = hdisplay << 16;
2685                 primary_state->src_h = vdisplay << 16;
2686         }
2687
2688 commit:
2689         ret = update_output_state(state, set);
2690         if (ret)
2691                 return ret;
2692
2693         return 0;
2694 }
2695
2696 static int __drm_atomic_helper_disable_all(struct drm_device *dev,
2697                                            struct drm_modeset_acquire_ctx *ctx,
2698                                            bool clean_old_fbs)
2699 {
2700         struct drm_atomic_state *state;
2701         struct drm_connector_state *conn_state;
2702         struct drm_connector *conn;
2703         struct drm_plane_state *plane_state;
2704         struct drm_plane *plane;
2705         struct drm_crtc_state *crtc_state;
2706         struct drm_crtc *crtc;
2707         unsigned plane_mask = 0;
2708         int ret, i;
2709
2710         state = drm_atomic_state_alloc(dev);
2711         if (!state)
2712                 return -ENOMEM;
2713
2714         state->acquire_ctx = ctx;
2715
2716         drm_for_each_crtc(crtc, dev) {
2717                 crtc_state = drm_atomic_get_crtc_state(state, crtc);
2718                 if (IS_ERR(crtc_state)) {
2719                         ret = PTR_ERR(crtc_state);
2720                         goto free;
2721                 }
2722
2723                 crtc_state->active = false;
2724
2725                 ret = drm_atomic_set_mode_prop_for_crtc(crtc_state, NULL);
2726                 if (ret < 0)
2727                         goto free;
2728
2729                 ret = drm_atomic_add_affected_planes(state, crtc);
2730                 if (ret < 0)
2731                         goto free;
2732
2733                 ret = drm_atomic_add_affected_connectors(state, crtc);
2734                 if (ret < 0)
2735                         goto free;
2736         }
2737
2738         for_each_new_connector_in_state(state, conn, conn_state, i) {
2739                 ret = drm_atomic_set_crtc_for_connector(conn_state, NULL);
2740                 if (ret < 0)
2741                         goto free;
2742         }
2743
2744         for_each_new_plane_in_state(state, plane, plane_state, i) {
2745                 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
2746                 if (ret < 0)
2747                         goto free;
2748
2749                 drm_atomic_set_fb_for_plane(plane_state, NULL);
2750
2751                 if (clean_old_fbs) {
2752                         plane->old_fb = plane->fb;
2753                         plane_mask |= BIT(drm_plane_index(plane));
2754                 }
2755         }
2756
2757         ret = drm_atomic_commit(state);
2758 free:
2759         if (plane_mask)
2760                 drm_atomic_clean_old_fb(dev, plane_mask, ret);
2761         drm_atomic_state_put(state);
2762         return ret;
2763 }
2764
2765 /**
2766  * drm_atomic_helper_disable_all - disable all currently active outputs
2767  * @dev: DRM device
2768  * @ctx: lock acquisition context
2769  *
2770  * Loops through all connectors, finding those that aren't turned off and then
2771  * turns them off by setting their DPMS mode to OFF and deactivating the CRTC
2772  * that they are connected to.
2773  *
2774  * This is used for example in suspend/resume to disable all currently active
2775  * functions when suspending. If you just want to shut down everything at e.g.
2776  * driver unload, look at drm_atomic_helper_shutdown().
2777  *
2778  * Note that if callers haven't already acquired all modeset locks this might
2779  * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
2780  *
2781  * Returns:
2782  * 0 on success or a negative error code on failure.
2783  *
2784  * See also:
2785  * drm_atomic_helper_suspend(), drm_atomic_helper_resume() and
2786  * drm_atomic_helper_shutdown().
2787  */
2788 int drm_atomic_helper_disable_all(struct drm_device *dev,
2789                                   struct drm_modeset_acquire_ctx *ctx)
2790 {
2791         return __drm_atomic_helper_disable_all(dev, ctx, false);
2792 }
2793 EXPORT_SYMBOL(drm_atomic_helper_disable_all);
2794
2795 /**
2796  * drm_atomic_helper_shutdown - shutdown all CRTC
2797  * @dev: DRM device
2798  *
2799  * This shuts down all CRTC, which is useful for driver unloading. Shutdown on
2800  * suspend should instead be handled with drm_atomic_helper_suspend(), since
2801  * that also takes a snapshot of the modeset state to be restored on resume.
2802  *
2803  * This is just a convenience wrapper around drm_atomic_helper_disable_all(),
2804  * and it is the atomic version of drm_crtc_force_disable_all().
2805  */
2806 void drm_atomic_helper_shutdown(struct drm_device *dev)
2807 {
2808         struct drm_modeset_acquire_ctx ctx;
2809         int ret;
2810
2811         drm_modeset_acquire_init(&ctx, 0);
2812         while (1) {
2813                 ret = drm_modeset_lock_all_ctx(dev, &ctx);
2814                 if (!ret)
2815                         ret = __drm_atomic_helper_disable_all(dev, &ctx, true);
2816
2817                 if (ret != -EDEADLK)
2818                         break;
2819
2820                 drm_modeset_backoff(&ctx);
2821         }
2822
2823         if (ret)
2824                 DRM_ERROR("Disabling all crtc's during unload failed with %i\n", ret);
2825
2826         drm_modeset_drop_locks(&ctx);
2827         drm_modeset_acquire_fini(&ctx);
2828 }
2829 EXPORT_SYMBOL(drm_atomic_helper_shutdown);
2830
2831 /**
2832  * drm_atomic_helper_suspend - subsystem-level suspend helper
2833  * @dev: DRM device
2834  *
2835  * Duplicates the current atomic state, disables all active outputs and then
2836  * returns a pointer to the original atomic state to the caller. Drivers can
2837  * pass this pointer to the drm_atomic_helper_resume() helper upon resume to
2838  * restore the output configuration that was active at the time the system
2839  * entered suspend.
2840  *
2841  * Note that it is potentially unsafe to use this. The atomic state object
2842  * returned by this function is assumed to be persistent. Drivers must ensure
2843  * that this holds true. Before calling this function, drivers must make sure
2844  * to suspend fbdev emulation so that nothing can be using the device.
2845  *
2846  * Returns:
2847  * A pointer to a copy of the state before suspend on success or an ERR_PTR()-
2848  * encoded error code on failure. Drivers should store the returned atomic
2849  * state object and pass it to the drm_atomic_helper_resume() helper upon
2850  * resume.
2851  *
2852  * See also:
2853  * drm_atomic_helper_duplicate_state(), drm_atomic_helper_disable_all(),
2854  * drm_atomic_helper_resume(), drm_atomic_helper_commit_duplicated_state()
2855  */
2856 struct drm_atomic_state *drm_atomic_helper_suspend(struct drm_device *dev)
2857 {
2858         struct drm_modeset_acquire_ctx ctx;
2859         struct drm_atomic_state *state;
2860         int err;
2861
2862         drm_modeset_acquire_init(&ctx, 0);
2863
2864 retry:
2865         err = drm_modeset_lock_all_ctx(dev, &ctx);
2866         if (err < 0) {
2867                 state = ERR_PTR(err);
2868                 goto unlock;
2869         }
2870
2871         state = drm_atomic_helper_duplicate_state(dev, &ctx);
2872         if (IS_ERR(state))
2873                 goto unlock;
2874
2875         err = drm_atomic_helper_disable_all(dev, &ctx);
2876         if (err < 0) {
2877                 drm_atomic_state_put(state);
2878                 state = ERR_PTR(err);
2879                 goto unlock;
2880         }
2881
2882 unlock:
2883         if (PTR_ERR(state) == -EDEADLK) {
2884                 drm_modeset_backoff(&ctx);
2885                 goto retry;
2886         }
2887
2888         drm_modeset_drop_locks(&ctx);
2889         drm_modeset_acquire_fini(&ctx);
2890         return state;
2891 }
2892 EXPORT_SYMBOL(drm_atomic_helper_suspend);
2893
2894 /**
2895  * drm_atomic_helper_commit_duplicated_state - commit duplicated state
2896  * @state: duplicated atomic state to commit
2897  * @ctx: pointer to acquire_ctx to use for commit.
2898  *
2899  * The state returned by drm_atomic_helper_duplicate_state() and
2900  * drm_atomic_helper_suspend() is partially invalid, and needs to
2901  * be fixed up before commit.
2902  *
2903  * Returns:
2904  * 0 on success or a negative error code on failure.
2905  *
2906  * See also:
2907  * drm_atomic_helper_suspend()
2908  */
2909 int drm_atomic_helper_commit_duplicated_state(struct drm_atomic_state *state,
2910                                               struct drm_modeset_acquire_ctx *ctx)
2911 {
2912         int i, ret;
2913         struct drm_plane *plane;
2914         struct drm_plane_state *new_plane_state;
2915         struct drm_connector *connector;
2916         struct drm_connector_state *new_conn_state;
2917         struct drm_crtc *crtc;
2918         struct drm_crtc_state *new_crtc_state;
2919
2920         state->acquire_ctx = ctx;
2921
2922         for_each_new_plane_in_state(state, plane, new_plane_state, i)
2923                 state->planes[i].old_state = plane->state;
2924
2925         for_each_new_crtc_in_state(state, crtc, new_crtc_state, i)
2926                 state->crtcs[i].old_state = crtc->state;
2927
2928         for_each_new_connector_in_state(state, connector, new_conn_state, i)
2929                 state->connectors[i].old_state = connector->state;
2930
2931         ret = drm_atomic_commit(state);
2932
2933         state->acquire_ctx = NULL;
2934
2935         return ret;
2936 }
2937 EXPORT_SYMBOL(drm_atomic_helper_commit_duplicated_state);
2938
2939 /**
2940  * drm_atomic_helper_resume - subsystem-level resume helper
2941  * @dev: DRM device
2942  * @state: atomic state to resume to
2943  *
2944  * Calls drm_mode_config_reset() to synchronize hardware and software states,
2945  * grabs all modeset locks and commits the atomic state object. This can be
2946  * used in conjunction with the drm_atomic_helper_suspend() helper to
2947  * implement suspend/resume for drivers that support atomic mode-setting.
2948  *
2949  * Returns:
2950  * 0 on success or a negative error code on failure.
2951  *
2952  * See also:
2953  * drm_atomic_helper_suspend()
2954  */
2955 int drm_atomic_helper_resume(struct drm_device *dev,
2956                              struct drm_atomic_state *state)
2957 {
2958         struct drm_modeset_acquire_ctx ctx;
2959         int err;
2960
2961         drm_mode_config_reset(dev);
2962
2963         drm_modeset_acquire_init(&ctx, 0);
2964         while (1) {
2965                 err = drm_modeset_lock_all_ctx(dev, &ctx);
2966                 if (err)
2967                         goto out;
2968
2969                 err = drm_atomic_helper_commit_duplicated_state(state, &ctx);
2970 out:
2971                 if (err != -EDEADLK)
2972                         break;
2973
2974                 drm_modeset_backoff(&ctx);
2975         }
2976
2977         drm_atomic_state_put(state);
2978         drm_modeset_drop_locks(&ctx);
2979         drm_modeset_acquire_fini(&ctx);
2980
2981         return err;
2982 }
2983 EXPORT_SYMBOL(drm_atomic_helper_resume);
2984
2985 static int page_flip_common(struct drm_atomic_state *state,
2986                             struct drm_crtc *crtc,
2987                             struct drm_framebuffer *fb,
2988                             struct drm_pending_vblank_event *event,
2989                             uint32_t flags)
2990 {
2991         struct drm_plane *plane = crtc->primary;
2992         struct drm_plane_state *plane_state;
2993         struct drm_crtc_state *crtc_state;
2994         int ret = 0;
2995
2996         crtc_state = drm_atomic_get_crtc_state(state, crtc);
2997         if (IS_ERR(crtc_state))
2998                 return PTR_ERR(crtc_state);
2999
3000         crtc_state->event = event;
3001         crtc_state->pageflip_flags = flags;
3002
3003         plane_state = drm_atomic_get_plane_state(state, plane);
3004         if (IS_ERR(plane_state))
3005                 return PTR_ERR(plane_state);
3006
3007         ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
3008         if (ret != 0)
3009                 return ret;
3010         drm_atomic_set_fb_for_plane(plane_state, fb);
3011
3012         /* Make sure we don't accidentally do a full modeset. */
3013         state->allow_modeset = false;
3014         if (!crtc_state->active) {
3015                 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] disabled, rejecting legacy flip\n",
3016                                  crtc->base.id, crtc->name);
3017                 return -EINVAL;
3018         }
3019
3020         return ret;
3021 }
3022
3023 /**
3024  * drm_atomic_helper_page_flip - execute a legacy page flip
3025  * @crtc: DRM crtc
3026  * @fb: DRM framebuffer
3027  * @event: optional DRM event to signal upon completion
3028  * @flags: flip flags for non-vblank sync'ed updates
3029  * @ctx: lock acquisition context
3030  *
3031  * Provides a default &drm_crtc_funcs.page_flip implementation
3032  * using the atomic driver interface.
3033  *
3034  * Returns:
3035  * Returns 0 on success, negative errno numbers on failure.
3036  *
3037  * See also:
3038  * drm_atomic_helper_page_flip_target()
3039  */
3040 int drm_atomic_helper_page_flip(struct drm_crtc *crtc,
3041                                 struct drm_framebuffer *fb,
3042                                 struct drm_pending_vblank_event *event,
3043                                 uint32_t flags,
3044                                 struct drm_modeset_acquire_ctx *ctx)
3045 {
3046         struct drm_plane *plane = crtc->primary;
3047         struct drm_atomic_state *state;
3048         int ret = 0;
3049
3050         state = drm_atomic_state_alloc(plane->dev);
3051         if (!state)
3052                 return -ENOMEM;
3053
3054         state->acquire_ctx = ctx;
3055
3056         ret = page_flip_common(state, crtc, fb, event, flags);
3057         if (ret != 0)
3058                 goto fail;
3059
3060         ret = drm_atomic_nonblocking_commit(state);
3061 fail:
3062         drm_atomic_state_put(state);
3063         return ret;
3064 }
3065 EXPORT_SYMBOL(drm_atomic_helper_page_flip);
3066
3067 /**
3068  * drm_atomic_helper_page_flip_target - do page flip on target vblank period.
3069  * @crtc: DRM crtc
3070  * @fb: DRM framebuffer
3071  * @event: optional DRM event to signal upon completion
3072  * @flags: flip flags for non-vblank sync'ed updates
3073  * @target: specifying the target vblank period when the flip to take effect
3074  * @ctx: lock acquisition context
3075  *
3076  * Provides a default &drm_crtc_funcs.page_flip_target implementation.
3077  * Similar to drm_atomic_helper_page_flip() with extra parameter to specify
3078  * target vblank period to flip.
3079  *
3080  * Returns:
3081  * Returns 0 on success, negative errno numbers on failure.
3082  */
3083 int drm_atomic_helper_page_flip_target(struct drm_crtc *crtc,
3084                                        struct drm_framebuffer *fb,
3085                                        struct drm_pending_vblank_event *event,
3086                                        uint32_t flags,
3087                                        uint32_t target,
3088                                        struct drm_modeset_acquire_ctx *ctx)
3089 {
3090         struct drm_plane *plane = crtc->primary;
3091         struct drm_atomic_state *state;
3092         struct drm_crtc_state *crtc_state;
3093         int ret = 0;
3094
3095         state = drm_atomic_state_alloc(plane->dev);
3096         if (!state)
3097                 return -ENOMEM;
3098
3099         state->acquire_ctx = ctx;
3100
3101         ret = page_flip_common(state, crtc, fb, event, flags);
3102         if (ret != 0)
3103                 goto fail;
3104
3105         crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
3106         if (WARN_ON(!crtc_state)) {
3107                 ret = -EINVAL;
3108                 goto fail;
3109         }
3110         crtc_state->target_vblank = target;
3111
3112         ret = drm_atomic_nonblocking_commit(state);
3113 fail:
3114         drm_atomic_state_put(state);
3115         return ret;
3116 }
3117 EXPORT_SYMBOL(drm_atomic_helper_page_flip_target);
3118
3119 /**
3120  * drm_atomic_helper_best_encoder - Helper for
3121  *      &drm_connector_helper_funcs.best_encoder callback
3122  * @connector: Connector control structure
3123  *
3124  * This is a &drm_connector_helper_funcs.best_encoder callback helper for
3125  * connectors that support exactly 1 encoder, statically determined at driver
3126  * init time.
3127  */
3128 struct drm_encoder *
3129 drm_atomic_helper_best_encoder(struct drm_connector *connector)
3130 {
3131         WARN_ON(connector->encoder_ids[1]);
3132         return drm_encoder_find(connector->dev, connector->encoder_ids[0]);
3133 }
3134 EXPORT_SYMBOL(drm_atomic_helper_best_encoder);
3135
3136 /**
3137  * DOC: atomic state reset and initialization
3138  *
3139  * Both the drm core and the atomic helpers assume that there is always the full
3140  * and correct atomic software state for all connectors, CRTCs and planes
3141  * available. Which is a bit a problem on driver load and also after system
3142  * suspend. One way to solve this is to have a hardware state read-out
3143  * infrastructure which reconstructs the full software state (e.g. the i915
3144  * driver).
3145  *
3146  * The simpler solution is to just reset the software state to everything off,
3147  * which is easiest to do by calling drm_mode_config_reset(). To facilitate this
3148  * the atomic helpers provide default reset implementations for all hooks.
3149  *
3150  * On the upside the precise state tracking of atomic simplifies system suspend
3151  * and resume a lot. For drivers using drm_mode_config_reset() a complete recipe
3152  * is implemented in drm_atomic_helper_suspend() and drm_atomic_helper_resume().
3153  * For other drivers the building blocks are split out, see the documentation
3154  * for these functions.
3155  */
3156
3157 /**
3158  * drm_atomic_helper_crtc_reset - default &drm_crtc_funcs.reset hook for CRTCs
3159  * @crtc: drm CRTC
3160  *
3161  * Resets the atomic state for @crtc by freeing the state pointer (which might
3162  * be NULL, e.g. at driver load time) and allocating a new empty state object.
3163  */
3164 void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc)
3165 {
3166         if (crtc->state)
3167                 __drm_atomic_helper_crtc_destroy_state(crtc->state);
3168
3169         kfree(crtc->state);
3170         crtc->state = kzalloc(sizeof(*crtc->state), GFP_KERNEL);
3171
3172         if (crtc->state)
3173                 crtc->state->crtc = crtc;
3174 }
3175 EXPORT_SYMBOL(drm_atomic_helper_crtc_reset);
3176
3177 /**
3178  * __drm_atomic_helper_crtc_duplicate_state - copy atomic CRTC state
3179  * @crtc: CRTC object
3180  * @state: atomic CRTC state
3181  *
3182  * Copies atomic state from a CRTC's current state and resets inferred values.
3183  * This is useful for drivers that subclass the CRTC state.
3184  */
3185 void __drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc,
3186                                               struct drm_crtc_state *state)
3187 {
3188         memcpy(state, crtc->state, sizeof(*state));
3189
3190         if (state->mode_blob)
3191                 drm_property_blob_get(state->mode_blob);
3192         if (state->degamma_lut)
3193                 drm_property_blob_get(state->degamma_lut);
3194         if (state->ctm)
3195                 drm_property_blob_get(state->ctm);
3196         if (state->gamma_lut)
3197                 drm_property_blob_get(state->gamma_lut);
3198         state->mode_changed = false;
3199         state->active_changed = false;
3200         state->planes_changed = false;
3201         state->connectors_changed = false;
3202         state->color_mgmt_changed = false;
3203         state->zpos_changed = false;
3204         state->event = NULL;
3205         state->pageflip_flags = 0;
3206 }
3207 EXPORT_SYMBOL(__drm_atomic_helper_crtc_duplicate_state);
3208
3209 /**
3210  * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
3211  * @crtc: drm CRTC
3212  *
3213  * Default CRTC state duplicate hook for drivers which don't have their own
3214  * subclassed CRTC state structure.
3215  */
3216 struct drm_crtc_state *
3217 drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc)
3218 {
3219         struct drm_crtc_state *state;
3220
3221         if (WARN_ON(!crtc->state))
3222                 return NULL;
3223
3224         state = kmalloc(sizeof(*state), GFP_KERNEL);
3225         if (state)
3226                 __drm_atomic_helper_crtc_duplicate_state(crtc, state);
3227
3228         return state;
3229 }
3230 EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state);
3231
3232 /**
3233  * __drm_atomic_helper_crtc_destroy_state - release CRTC state
3234  * @state: CRTC state object to release
3235  *
3236  * Releases all resources stored in the CRTC state without actually freeing
3237  * the memory of the CRTC state. This is useful for drivers that subclass the
3238  * CRTC state.
3239  */
3240 void __drm_atomic_helper_crtc_destroy_state(struct drm_crtc_state *state)
3241 {
3242         drm_property_blob_put(state->mode_blob);
3243         drm_property_blob_put(state->degamma_lut);
3244         drm_property_blob_put(state->ctm);
3245         drm_property_blob_put(state->gamma_lut);
3246 }
3247 EXPORT_SYMBOL(__drm_atomic_helper_crtc_destroy_state);
3248
3249 /**
3250  * drm_atomic_helper_crtc_destroy_state - default state destroy hook
3251  * @crtc: drm CRTC
3252  * @state: CRTC state object to release
3253  *
3254  * Default CRTC state destroy hook for drivers which don't have their own
3255  * subclassed CRTC state structure.
3256  */
3257 void drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
3258                                           struct drm_crtc_state *state)
3259 {
3260         __drm_atomic_helper_crtc_destroy_state(state);
3261         kfree(state);
3262 }
3263 EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state);
3264
3265 /**
3266  * drm_atomic_helper_plane_reset - default &drm_plane_funcs.reset hook for planes
3267  * @plane: drm plane
3268  *
3269  * Resets the atomic state for @plane by freeing the state pointer (which might
3270  * be NULL, e.g. at driver load time) and allocating a new empty state object.
3271  */
3272 void drm_atomic_helper_plane_reset(struct drm_plane *plane)
3273 {
3274         if (plane->state)
3275                 __drm_atomic_helper_plane_destroy_state(plane->state);
3276
3277         kfree(plane->state);
3278         plane->state = kzalloc(sizeof(*plane->state), GFP_KERNEL);
3279
3280         if (plane->state) {
3281                 plane->state->plane = plane;
3282                 plane->state->rotation = DRM_MODE_ROTATE_0;
3283         }
3284 }
3285 EXPORT_SYMBOL(drm_atomic_helper_plane_reset);
3286
3287 /**
3288  * __drm_atomic_helper_plane_duplicate_state - copy atomic plane state
3289  * @plane: plane object
3290  * @state: atomic plane state
3291  *
3292  * Copies atomic state from a plane's current state. This is useful for
3293  * drivers that subclass the plane state.
3294  */
3295 void __drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane,
3296                                                struct drm_plane_state *state)
3297 {
3298         memcpy(state, plane->state, sizeof(*state));
3299
3300         if (state->fb)
3301                 drm_framebuffer_get(state->fb);
3302
3303         state->fence = NULL;
3304 }
3305 EXPORT_SYMBOL(__drm_atomic_helper_plane_duplicate_state);
3306
3307 /**
3308  * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
3309  * @plane: drm plane
3310  *
3311  * Default plane state duplicate hook for drivers which don't have their own
3312  * subclassed plane state structure.
3313  */
3314 struct drm_plane_state *
3315 drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane)
3316 {
3317         struct drm_plane_state *state;
3318
3319         if (WARN_ON(!plane->state))
3320                 return NULL;
3321
3322         state = kmalloc(sizeof(*state), GFP_KERNEL);
3323         if (state)
3324                 __drm_atomic_helper_plane_duplicate_state(plane, state);
3325
3326         return state;
3327 }
3328 EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state);
3329
3330 /**
3331  * __drm_atomic_helper_plane_destroy_state - release plane state
3332  * @state: plane state object to release
3333  *
3334  * Releases all resources stored in the plane state without actually freeing
3335  * the memory of the plane state. This is useful for drivers that subclass the
3336  * plane state.
3337  */
3338 void __drm_atomic_helper_plane_destroy_state(struct drm_plane_state *state)
3339 {
3340         if (state->fb)
3341                 drm_framebuffer_put(state->fb);
3342
3343         if (state->fence)
3344                 dma_fence_put(state->fence);
3345 }
3346 EXPORT_SYMBOL(__drm_atomic_helper_plane_destroy_state);
3347
3348 /**
3349  * drm_atomic_helper_plane_destroy_state - default state destroy hook
3350  * @plane: drm plane
3351  * @state: plane state object to release
3352  *
3353  * Default plane state destroy hook for drivers which don't have their own
3354  * subclassed plane state structure.
3355  */
3356 void drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
3357                                            struct drm_plane_state *state)
3358 {
3359         __drm_atomic_helper_plane_destroy_state(state);
3360         kfree(state);
3361 }
3362 EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state);
3363
3364 /**
3365  * __drm_atomic_helper_connector_reset - reset state on connector
3366  * @connector: drm connector
3367  * @conn_state: connector state to assign
3368  *
3369  * Initializes the newly allocated @conn_state and assigns it to
3370  * the &drm_conector->state pointer of @connector, usually required when
3371  * initializing the drivers or when called from the &drm_connector_funcs.reset
3372  * hook.
3373  *
3374  * This is useful for drivers that subclass the connector state.
3375  */
3376 void
3377 __drm_atomic_helper_connector_reset(struct drm_connector *connector,
3378                                     struct drm_connector_state *conn_state)
3379 {
3380         if (conn_state)
3381                 conn_state->connector = connector;
3382
3383         connector->state = conn_state;
3384 }
3385 EXPORT_SYMBOL(__drm_atomic_helper_connector_reset);
3386
3387 /**
3388  * drm_atomic_helper_connector_reset - default &drm_connector_funcs.reset hook for connectors
3389  * @connector: drm connector
3390  *
3391  * Resets the atomic state for @connector by freeing the state pointer (which
3392  * might be NULL, e.g. at driver load time) and allocating a new empty state
3393  * object.
3394  */
3395 void drm_atomic_helper_connector_reset(struct drm_connector *connector)
3396 {
3397         struct drm_connector_state *conn_state =
3398                 kzalloc(sizeof(*conn_state), GFP_KERNEL);
3399
3400         if (connector->state)
3401                 __drm_atomic_helper_connector_destroy_state(connector->state);
3402
3403         kfree(connector->state);
3404         __drm_atomic_helper_connector_reset(connector, conn_state);
3405 }
3406 EXPORT_SYMBOL(drm_atomic_helper_connector_reset);
3407
3408 /**
3409  * __drm_atomic_helper_connector_duplicate_state - copy atomic connector state
3410  * @connector: connector object
3411  * @state: atomic connector state
3412  *
3413  * Copies atomic state from a connector's current state. This is useful for
3414  * drivers that subclass the connector state.
3415  */
3416 void
3417 __drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector,
3418                                             struct drm_connector_state *state)
3419 {
3420         memcpy(state, connector->state, sizeof(*state));
3421         if (state->crtc)
3422                 drm_connector_get(connector);
3423 }
3424 EXPORT_SYMBOL(__drm_atomic_helper_connector_duplicate_state);
3425
3426 /**
3427  * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
3428  * @connector: drm connector
3429  *
3430  * Default connector state duplicate hook for drivers which don't have their own
3431  * subclassed connector state structure.
3432  */
3433 struct drm_connector_state *
3434 drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
3435 {
3436         struct drm_connector_state *state;
3437
3438         if (WARN_ON(!connector->state))
3439                 return NULL;
3440
3441         state = kmalloc(sizeof(*state), GFP_KERNEL);
3442         if (state)
3443                 __drm_atomic_helper_connector_duplicate_state(connector, state);
3444
3445         return state;
3446 }
3447 EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
3448
3449 /**
3450  * drm_atomic_helper_duplicate_state - duplicate an atomic state object
3451  * @dev: DRM device
3452  * @ctx: lock acquisition context
3453  *
3454  * Makes a copy of the current atomic state by looping over all objects and
3455  * duplicating their respective states. This is used for example by suspend/
3456  * resume support code to save the state prior to suspend such that it can
3457  * be restored upon resume.
3458  *
3459  * Note that this treats atomic state as persistent between save and restore.
3460  * Drivers must make sure that this is possible and won't result in confusion
3461  * or erroneous behaviour.
3462  *
3463  * Note that if callers haven't already acquired all modeset locks this might
3464  * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
3465  *
3466  * Returns:
3467  * A pointer to the copy of the atomic state object on success or an
3468  * ERR_PTR()-encoded error code on failure.
3469  *
3470  * See also:
3471  * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
3472  */
3473 struct drm_atomic_state *
3474 drm_atomic_helper_duplicate_state(struct drm_device *dev,
3475                                   struct drm_modeset_acquire_ctx *ctx)
3476 {
3477         struct drm_atomic_state *state;
3478         struct drm_connector *conn;
3479         struct drm_connector_list_iter conn_iter;
3480         struct drm_plane *plane;
3481         struct drm_crtc *crtc;
3482         int err = 0;
3483
3484         state = drm_atomic_state_alloc(dev);
3485         if (!state)
3486                 return ERR_PTR(-ENOMEM);
3487
3488         state->acquire_ctx = ctx;
3489
3490         drm_for_each_crtc(crtc, dev) {
3491                 struct drm_crtc_state *crtc_state;
3492
3493                 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3494                 if (IS_ERR(crtc_state)) {
3495                         err = PTR_ERR(crtc_state);
3496                         goto free;
3497                 }
3498         }
3499
3500         drm_for_each_plane(plane, dev) {
3501                 struct drm_plane_state *plane_state;
3502
3503                 plane_state = drm_atomic_get_plane_state(state, plane);
3504                 if (IS_ERR(plane_state)) {
3505                         err = PTR_ERR(plane_state);
3506                         goto free;
3507                 }
3508         }
3509
3510         drm_connector_list_iter_begin(dev, &conn_iter);
3511         drm_for_each_connector_iter(conn, &conn_iter) {
3512                 struct drm_connector_state *conn_state;
3513
3514                 conn_state = drm_atomic_get_connector_state(state, conn);
3515                 if (IS_ERR(conn_state)) {
3516                         err = PTR_ERR(conn_state);
3517                         drm_connector_list_iter_end(&conn_iter);
3518                         goto free;
3519                 }
3520         }
3521         drm_connector_list_iter_end(&conn_iter);
3522
3523         /* clear the acquire context so that it isn't accidentally reused */
3524         state->acquire_ctx = NULL;
3525
3526 free:
3527         if (err < 0) {
3528                 drm_atomic_state_put(state);
3529                 state = ERR_PTR(err);
3530         }
3531
3532         return state;
3533 }
3534 EXPORT_SYMBOL(drm_atomic_helper_duplicate_state);
3535
3536 /**
3537  * __drm_atomic_helper_connector_destroy_state - release connector state
3538  * @state: connector state object to release
3539  *
3540  * Releases all resources stored in the connector state without actually
3541  * freeing the memory of the connector state. This is useful for drivers that
3542  * subclass the connector state.
3543  */
3544 void
3545 __drm_atomic_helper_connector_destroy_state(struct drm_connector_state *state)
3546 {
3547         if (state->crtc)
3548                 drm_connector_put(state->connector);
3549 }
3550 EXPORT_SYMBOL(__drm_atomic_helper_connector_destroy_state);
3551
3552 /**
3553  * drm_atomic_helper_connector_destroy_state - default state destroy hook
3554  * @connector: drm connector
3555  * @state: connector state object to release
3556  *
3557  * Default connector state destroy hook for drivers which don't have their own
3558  * subclassed connector state structure.
3559  */
3560 void drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
3561                                           struct drm_connector_state *state)
3562 {
3563         __drm_atomic_helper_connector_destroy_state(state);
3564         kfree(state);
3565 }
3566 EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state);
3567
3568 /**
3569  * drm_atomic_helper_legacy_gamma_set - set the legacy gamma correction table
3570  * @crtc: CRTC object
3571  * @red: red correction table
3572  * @green: green correction table
3573  * @blue: green correction table
3574  * @size: size of the tables
3575  * @ctx: lock acquire context
3576  *
3577  * Implements support for legacy gamma correction table for drivers
3578  * that support color management through the DEGAMMA_LUT/GAMMA_LUT
3579  * properties. See drm_crtc_enable_color_mgmt() and the containing chapter for
3580  * how the atomic color management and gamma tables work.
3581  */
3582 int drm_atomic_helper_legacy_gamma_set(struct drm_crtc *crtc,
3583                                        u16 *red, u16 *green, u16 *blue,
3584                                        uint32_t size,
3585                                        struct drm_modeset_acquire_ctx *ctx)
3586 {
3587         struct drm_device *dev = crtc->dev;
3588         struct drm_atomic_state *state;
3589         struct drm_crtc_state *crtc_state;
3590         struct drm_property_blob *blob = NULL;
3591         struct drm_color_lut *blob_data;
3592         int i, ret = 0;
3593         bool replaced;
3594
3595         state = drm_atomic_state_alloc(crtc->dev);
3596         if (!state)
3597                 return -ENOMEM;
3598
3599         blob = drm_property_create_blob(dev,
3600                                         sizeof(struct drm_color_lut) * size,
3601                                         NULL);
3602         if (IS_ERR(blob)) {
3603                 ret = PTR_ERR(blob);
3604                 blob = NULL;
3605                 goto fail;
3606         }
3607
3608         /* Prepare GAMMA_LUT with the legacy values. */
3609         blob_data = (struct drm_color_lut *) blob->data;
3610         for (i = 0; i < size; i++) {
3611                 blob_data[i].red = red[i];
3612                 blob_data[i].green = green[i];
3613                 blob_data[i].blue = blue[i];
3614         }
3615
3616         state->acquire_ctx = ctx;
3617         crtc_state = drm_atomic_get_crtc_state(state, crtc);
3618         if (IS_ERR(crtc_state)) {
3619                 ret = PTR_ERR(crtc_state);
3620                 goto fail;
3621         }
3622
3623         /* Reset DEGAMMA_LUT and CTM properties. */
3624         replaced  = drm_property_replace_blob(&crtc_state->degamma_lut, NULL);
3625         replaced |= drm_property_replace_blob(&crtc_state->ctm, NULL);
3626         replaced |= drm_property_replace_blob(&crtc_state->gamma_lut, blob);
3627         crtc_state->color_mgmt_changed |= replaced;
3628
3629         ret = drm_atomic_commit(state);
3630
3631 fail:
3632         drm_atomic_state_put(state);
3633         drm_property_blob_put(blob);
3634         return ret;
3635 }
3636 EXPORT_SYMBOL(drm_atomic_helper_legacy_gamma_set);
3637
3638 /**
3639  * __drm_atomic_helper_private_duplicate_state - copy atomic private state
3640  * @obj: CRTC object
3641  * @state: new private object state
3642  *
3643  * Copies atomic state from a private objects's current state and resets inferred values.
3644  * This is useful for drivers that subclass the private state.
3645  */
3646 void __drm_atomic_helper_private_obj_duplicate_state(struct drm_private_obj *obj,
3647                                                      struct drm_private_state *state)
3648 {
3649         memcpy(state, obj->state, sizeof(*state));
3650 }
3651 EXPORT_SYMBOL(__drm_atomic_helper_private_obj_duplicate_state);