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