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