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