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