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