OSDN Git Service

Merge tag 'drm/tegra/for-4.1-rc1' of git://anongit.freedesktop.org/tegra/linux into...
[uclinux-h8/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 expose
47  * 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 static void
57 drm_atomic_helper_plane_changed(struct drm_atomic_state *state,
58                                 struct drm_plane_state *plane_state,
59                                 struct drm_plane *plane)
60 {
61         struct drm_crtc_state *crtc_state;
62
63         if (plane->state->crtc) {
64                 crtc_state = state->crtc_states[drm_crtc_index(plane->state->crtc)];
65
66                 if (WARN_ON(!crtc_state))
67                         return;
68
69                 crtc_state->planes_changed = true;
70         }
71
72         if (plane_state->crtc) {
73                 crtc_state =
74                         state->crtc_states[drm_crtc_index(plane_state->crtc)];
75
76                 if (WARN_ON(!crtc_state))
77                         return;
78
79                 crtc_state->planes_changed = true;
80         }
81 }
82
83 static struct drm_crtc *
84 get_current_crtc_for_encoder(struct drm_device *dev,
85                              struct drm_encoder *encoder)
86 {
87         struct drm_mode_config *config = &dev->mode_config;
88         struct drm_connector *connector;
89
90         WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
91
92         list_for_each_entry(connector, &config->connector_list, head) {
93                 if (connector->state->best_encoder != encoder)
94                         continue;
95
96                 return connector->state->crtc;
97         }
98
99         return NULL;
100 }
101
102 static int
103 steal_encoder(struct drm_atomic_state *state,
104               struct drm_encoder *encoder,
105               struct drm_crtc *encoder_crtc)
106 {
107         struct drm_mode_config *config = &state->dev->mode_config;
108         struct drm_crtc_state *crtc_state;
109         struct drm_connector *connector;
110         struct drm_connector_state *connector_state;
111         int ret;
112
113         /*
114          * We can only steal an encoder coming from a connector, which means we
115          * must already hold the connection_mutex.
116          */
117         WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
118
119         DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d], stealing it\n",
120                          encoder->base.id, encoder->name,
121                          encoder_crtc->base.id);
122
123         crtc_state = drm_atomic_get_crtc_state(state, encoder_crtc);
124         if (IS_ERR(crtc_state))
125                 return PTR_ERR(crtc_state);
126
127         crtc_state->mode_changed = true;
128
129         list_for_each_entry(connector, &config->connector_list, head) {
130                 if (connector->state->best_encoder != encoder)
131                         continue;
132
133                 DRM_DEBUG_ATOMIC("Stealing encoder from [CONNECTOR:%d:%s]\n",
134                                  connector->base.id,
135                                  connector->name);
136
137                 connector_state = drm_atomic_get_connector_state(state,
138                                                                  connector);
139                 if (IS_ERR(connector_state))
140                         return PTR_ERR(connector_state);
141
142                 ret = drm_atomic_set_crtc_for_connector(connector_state, NULL);
143                 if (ret)
144                         return ret;
145                 connector_state->best_encoder = NULL;
146         }
147
148         return 0;
149 }
150
151 static int
152 update_connector_routing(struct drm_atomic_state *state, int conn_idx)
153 {
154         const struct drm_connector_helper_funcs *funcs;
155         struct drm_encoder *new_encoder;
156         struct drm_crtc *encoder_crtc;
157         struct drm_connector *connector;
158         struct drm_connector_state *connector_state;
159         struct drm_crtc_state *crtc_state;
160         int idx, ret;
161
162         connector = state->connectors[conn_idx];
163         connector_state = state->connector_states[conn_idx];
164
165         if (!connector)
166                 return 0;
167
168         DRM_DEBUG_ATOMIC("Updating routing for [CONNECTOR:%d:%s]\n",
169                          connector->base.id,
170                          connector->name);
171
172         if (connector->state->crtc != connector_state->crtc) {
173                 if (connector->state->crtc) {
174                         idx = drm_crtc_index(connector->state->crtc);
175
176                         crtc_state = state->crtc_states[idx];
177                         crtc_state->mode_changed = true;
178                 }
179
180                 if (connector_state->crtc) {
181                         idx = drm_crtc_index(connector_state->crtc);
182
183                         crtc_state = state->crtc_states[idx];
184                         crtc_state->mode_changed = true;
185                 }
186         }
187
188         if (!connector_state->crtc) {
189                 DRM_DEBUG_ATOMIC("Disabling [CONNECTOR:%d:%s]\n",
190                                 connector->base.id,
191                                 connector->name);
192
193                 connector_state->best_encoder = NULL;
194
195                 return 0;
196         }
197
198         funcs = connector->helper_private;
199         new_encoder = funcs->best_encoder(connector);
200
201         if (!new_encoder) {
202                 DRM_DEBUG_ATOMIC("No suitable encoder found for [CONNECTOR:%d:%s]\n",
203                                  connector->base.id,
204                                  connector->name);
205                 return -EINVAL;
206         }
207
208         if (new_encoder == connector_state->best_encoder) {
209                 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] keeps [ENCODER:%d:%s], now on [CRTC:%d]\n",
210                                  connector->base.id,
211                                  connector->name,
212                                  new_encoder->base.id,
213                                  new_encoder->name,
214                                  connector_state->crtc->base.id);
215
216                 return 0;
217         }
218
219         encoder_crtc = get_current_crtc_for_encoder(state->dev,
220                                                     new_encoder);
221
222         if (encoder_crtc) {
223                 ret = steal_encoder(state, new_encoder, encoder_crtc);
224                 if (ret) {
225                         DRM_DEBUG_ATOMIC("Encoder stealing failed for [CONNECTOR:%d:%s]\n",
226                                          connector->base.id,
227                                          connector->name);
228                         return ret;
229                 }
230         }
231
232         connector_state->best_encoder = new_encoder;
233         idx = drm_crtc_index(connector_state->crtc);
234
235         crtc_state = state->crtc_states[idx];
236         crtc_state->mode_changed = true;
237
238         DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] using [ENCODER:%d:%s] on [CRTC:%d]\n",
239                          connector->base.id,
240                          connector->name,
241                          new_encoder->base.id,
242                          new_encoder->name,
243                          connector_state->crtc->base.id);
244
245         return 0;
246 }
247
248 static int
249 mode_fixup(struct drm_atomic_state *state)
250 {
251         int ncrtcs = state->dev->mode_config.num_crtc;
252         struct drm_crtc_state *crtc_state;
253         struct drm_connector_state *conn_state;
254         int i;
255         bool ret;
256
257         for (i = 0; i < ncrtcs; i++) {
258                 crtc_state = state->crtc_states[i];
259
260                 if (!crtc_state || !crtc_state->mode_changed)
261                         continue;
262
263                 drm_mode_copy(&crtc_state->adjusted_mode, &crtc_state->mode);
264         }
265
266         for (i = 0; i < state->num_connector; i++) {
267                 const struct drm_encoder_helper_funcs *funcs;
268                 struct drm_encoder *encoder;
269
270                 conn_state = state->connector_states[i];
271
272                 if (!conn_state)
273                         continue;
274
275                 WARN_ON(!!conn_state->best_encoder != !!conn_state->crtc);
276
277                 if (!conn_state->crtc || !conn_state->best_encoder)
278                         continue;
279
280                 crtc_state =
281                         state->crtc_states[drm_crtc_index(conn_state->crtc)];
282
283                 /*
284                  * Each encoder has at most one connector (since we always steal
285                  * it away), so we won't call ->mode_fixup twice.
286                  */
287                 encoder = conn_state->best_encoder;
288                 funcs = encoder->helper_private;
289
290                 if (encoder->bridge && encoder->bridge->funcs->mode_fixup) {
291                         ret = encoder->bridge->funcs->mode_fixup(
292                                         encoder->bridge, &crtc_state->mode,
293                                         &crtc_state->adjusted_mode);
294                         if (!ret) {
295                                 DRM_DEBUG_ATOMIC("Bridge fixup failed\n");
296                                 return -EINVAL;
297                         }
298                 }
299
300                 if (funcs->atomic_check) {
301                         ret = funcs->atomic_check(encoder, crtc_state,
302                                                   conn_state);
303                         if (ret) {
304                                 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] check failed\n",
305                                                  encoder->base.id, encoder->name);
306                                 return ret;
307                         }
308                 } else {
309                         ret = funcs->mode_fixup(encoder, &crtc_state->mode,
310                                                 &crtc_state->adjusted_mode);
311                         if (!ret) {
312                                 DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] fixup failed\n",
313                                                  encoder->base.id, encoder->name);
314                                 return -EINVAL;
315                         }
316                 }
317         }
318
319         for (i = 0; i < ncrtcs; i++) {
320                 const struct drm_crtc_helper_funcs *funcs;
321                 struct drm_crtc *crtc;
322
323                 crtc_state = state->crtc_states[i];
324                 crtc = state->crtcs[i];
325
326                 if (!crtc_state || !crtc_state->mode_changed)
327                         continue;
328
329                 funcs = crtc->helper_private;
330                 ret = funcs->mode_fixup(crtc, &crtc_state->mode,
331                                         &crtc_state->adjusted_mode);
332                 if (!ret) {
333                         DRM_DEBUG_ATOMIC("[CRTC:%d] fixup failed\n",
334                                          crtc->base.id);
335                         return -EINVAL;
336                 }
337         }
338
339         return 0;
340 }
341
342 static bool
343 needs_modeset(struct drm_crtc_state *state)
344 {
345         return state->mode_changed || state->active_changed;
346 }
347
348 /**
349  * drm_atomic_helper_check_modeset - validate state object for modeset changes
350  * @dev: DRM device
351  * @state: the driver state object
352  *
353  * Check the state object to see if the requested state is physically possible.
354  * This does all the crtc and connector related computations for an atomic
355  * update. It computes and updates crtc_state->mode_changed, adds any additional
356  * connectors needed for full modesets and calls down into ->mode_fixup
357  * functions of the driver backend.
358  *
359  * IMPORTANT:
360  *
361  * Drivers which update ->mode_changed (e.g. in their ->atomic_check hooks if a
362  * plane update can't be done without a full modeset) _must_ call this function
363  * afterwards after that change. It is permitted to call this function multiple
364  * times for the same update, e.g. when the ->atomic_check functions depend upon
365  * the adjusted dotclock for fifo space allocation and watermark computation.
366  *
367  * RETURNS
368  * Zero for success or -errno
369  */
370 int
371 drm_atomic_helper_check_modeset(struct drm_device *dev,
372                                 struct drm_atomic_state *state)
373 {
374         int ncrtcs = dev->mode_config.num_crtc;
375         struct drm_crtc *crtc;
376         struct drm_crtc_state *crtc_state;
377         int i, ret;
378
379         for (i = 0; i < ncrtcs; i++) {
380                 crtc = state->crtcs[i];
381                 crtc_state = state->crtc_states[i];
382
383                 if (!crtc)
384                         continue;
385
386                 if (!drm_mode_equal(&crtc->state->mode, &crtc_state->mode)) {
387                         DRM_DEBUG_ATOMIC("[CRTC:%d] mode changed\n",
388                                          crtc->base.id);
389                         crtc_state->mode_changed = true;
390                 }
391
392                 if (crtc->state->enable != crtc_state->enable) {
393                         DRM_DEBUG_ATOMIC("[CRTC:%d] enable changed\n",
394                                          crtc->base.id);
395                         crtc_state->mode_changed = true;
396                 }
397         }
398
399         for (i = 0; i < state->num_connector; i++) {
400                 /*
401                  * This only sets crtc->mode_changed for routing changes,
402                  * drivers must set crtc->mode_changed themselves when connector
403                  * properties need to be updated.
404                  */
405                 ret = update_connector_routing(state, i);
406                 if (ret)
407                         return ret;
408         }
409
410         /*
411          * After all the routing has been prepared we need to add in any
412          * connector which is itself unchanged, but who's crtc changes it's
413          * configuration. This must be done before calling mode_fixup in case a
414          * crtc only changed its mode but has the same set of connectors.
415          */
416         for (i = 0; i < ncrtcs; i++) {
417                 int num_connectors;
418
419                 crtc = state->crtcs[i];
420                 crtc_state = state->crtc_states[i];
421
422                 if (!crtc)
423                         continue;
424
425                 /*
426                  * We must set ->active_changed after walking connectors for
427                  * otherwise an update that only changes active would result in
428                  * a full modeset because update_connector_routing force that.
429                  */
430                 if (crtc->state->active != crtc_state->active) {
431                         DRM_DEBUG_ATOMIC("[CRTC:%d] active changed\n",
432                                          crtc->base.id);
433                         crtc_state->active_changed = true;
434                 }
435
436                 if (!needs_modeset(crtc_state))
437                         continue;
438
439                 DRM_DEBUG_ATOMIC("[CRTC:%d] needs all connectors, enable: %c, active: %c\n",
440                                  crtc->base.id,
441                                  crtc_state->enable ? 'y' : 'n',
442                               crtc_state->active ? 'y' : 'n');
443
444                 ret = drm_atomic_add_affected_connectors(state, crtc);
445                 if (ret != 0)
446                         return ret;
447
448                 num_connectors = drm_atomic_connectors_for_crtc(state,
449                                                                 crtc);
450
451                 if (crtc_state->enable != !!num_connectors) {
452                         DRM_DEBUG_ATOMIC("[CRTC:%d] enabled/connectors mismatch\n",
453                                          crtc->base.id);
454
455                         return -EINVAL;
456                 }
457         }
458
459         return mode_fixup(state);
460 }
461 EXPORT_SYMBOL(drm_atomic_helper_check_modeset);
462
463 /**
464  * drm_atomic_helper_check_planes - validate state object for planes changes
465  * @dev: DRM device
466  * @state: the driver state object
467  *
468  * Check the state object to see if the requested state is physically possible.
469  * This does all the plane update related checks using by calling into the
470  * ->atomic_check hooks provided by the driver.
471  *
472  * RETURNS
473  * Zero for success or -errno
474  */
475 int
476 drm_atomic_helper_check_planes(struct drm_device *dev,
477                                struct drm_atomic_state *state)
478 {
479         int nplanes = dev->mode_config.num_total_plane;
480         int ncrtcs = dev->mode_config.num_crtc;
481         int i, ret = 0;
482
483         for (i = 0; i < nplanes; i++) {
484                 const struct drm_plane_helper_funcs *funcs;
485                 struct drm_plane *plane = state->planes[i];
486                 struct drm_plane_state *plane_state = state->plane_states[i];
487
488                 if (!plane)
489                         continue;
490
491                 funcs = plane->helper_private;
492
493                 drm_atomic_helper_plane_changed(state, plane_state, plane);
494
495                 if (!funcs || !funcs->atomic_check)
496                         continue;
497
498                 ret = funcs->atomic_check(plane, plane_state);
499                 if (ret) {
500                         DRM_DEBUG_ATOMIC("[PLANE:%d] atomic driver check failed\n",
501                                          plane->base.id);
502                         return ret;
503                 }
504         }
505
506         for (i = 0; i < ncrtcs; i++) {
507                 const struct drm_crtc_helper_funcs *funcs;
508                 struct drm_crtc *crtc = state->crtcs[i];
509
510                 if (!crtc)
511                         continue;
512
513                 funcs = crtc->helper_private;
514
515                 if (!funcs || !funcs->atomic_check)
516                         continue;
517
518                 ret = funcs->atomic_check(crtc, state->crtc_states[i]);
519                 if (ret) {
520                         DRM_DEBUG_ATOMIC("[CRTC:%d] atomic driver check failed\n",
521                                          crtc->base.id);
522                         return ret;
523                 }
524         }
525
526         return ret;
527 }
528 EXPORT_SYMBOL(drm_atomic_helper_check_planes);
529
530 /**
531  * drm_atomic_helper_check - validate state object
532  * @dev: DRM device
533  * @state: the driver state object
534  *
535  * Check the state object to see if the requested state is physically possible.
536  * Only crtcs and planes have check callbacks, so for any additional (global)
537  * checking that a driver needs it can simply wrap that around this function.
538  * Drivers without such needs can directly use this as their ->atomic_check()
539  * callback.
540  *
541  * This just wraps the two parts of the state checking for planes and modeset
542  * state in the default order: First it calls drm_atomic_helper_check_modeset()
543  * and then drm_atomic_helper_check_planes(). The assumption is that the
544  * ->atomic_check functions depend upon an updated adjusted_mode.clock to
545  * e.g. properly compute watermarks.
546  *
547  * RETURNS
548  * Zero for success or -errno
549  */
550 int drm_atomic_helper_check(struct drm_device *dev,
551                             struct drm_atomic_state *state)
552 {
553         int ret;
554
555         ret = drm_atomic_helper_check_modeset(dev, state);
556         if (ret)
557                 return ret;
558
559         ret = drm_atomic_helper_check_planes(dev, state);
560         if (ret)
561                 return ret;
562
563         return ret;
564 }
565 EXPORT_SYMBOL(drm_atomic_helper_check);
566
567 static void
568 disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state)
569 {
570         int ncrtcs = old_state->dev->mode_config.num_crtc;
571         int i;
572
573         for (i = 0; i < old_state->num_connector; i++) {
574                 const struct drm_encoder_helper_funcs *funcs;
575                 struct drm_connector_state *old_conn_state;
576                 struct drm_connector *connector;
577                 struct drm_encoder *encoder;
578                 struct drm_crtc_state *old_crtc_state;
579
580                 old_conn_state = old_state->connector_states[i];
581                 connector = old_state->connectors[i];
582
583                 /* Shut down everything that's in the changeset and currently
584                  * still on. So need to check the old, saved state. */
585                 if (!old_conn_state || !old_conn_state->crtc)
586                         continue;
587
588                 old_crtc_state = old_state->crtc_states[drm_crtc_index(old_conn_state->crtc)];
589
590                 if (!old_crtc_state->active ||
591                     !needs_modeset(old_conn_state->crtc->state))
592                         continue;
593
594                 encoder = old_conn_state->best_encoder;
595
596                 /* We shouldn't get this far if we didn't previously have
597                  * an encoder.. but WARN_ON() rather than explode.
598                  */
599                 if (WARN_ON(!encoder))
600                         continue;
601
602                 funcs = encoder->helper_private;
603
604                 DRM_DEBUG_ATOMIC("disabling [ENCODER:%d:%s]\n",
605                                  encoder->base.id, encoder->name);
606
607                 /*
608                  * Each encoder has at most one connector (since we always steal
609                  * it away), so we won't call disable hooks twice.
610                  */
611                 if (encoder->bridge)
612                         encoder->bridge->funcs->disable(encoder->bridge);
613
614                 /* Right function depends upon target state. */
615                 if (connector->state->crtc && funcs->prepare)
616                         funcs->prepare(encoder);
617                 else if (funcs->disable)
618                         funcs->disable(encoder);
619                 else
620                         funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
621
622                 if (encoder->bridge)
623                         encoder->bridge->funcs->post_disable(encoder->bridge);
624         }
625
626         for (i = 0; i < ncrtcs; i++) {
627                 const struct drm_crtc_helper_funcs *funcs;
628                 struct drm_crtc *crtc;
629                 struct drm_crtc_state *old_crtc_state;
630
631                 crtc = old_state->crtcs[i];
632                 old_crtc_state = old_state->crtc_states[i];
633
634                 /* Shut down everything that needs a full modeset. */
635                 if (!crtc || !needs_modeset(crtc->state))
636                         continue;
637
638                 if (!old_crtc_state->active)
639                         continue;
640
641                 funcs = crtc->helper_private;
642
643                 DRM_DEBUG_ATOMIC("disabling [CRTC:%d]\n",
644                                  crtc->base.id);
645
646
647                 /* Right function depends upon target state. */
648                 if (crtc->state->enable && funcs->prepare)
649                         funcs->prepare(crtc);
650                 else if (funcs->disable)
651                         funcs->disable(crtc);
652                 else
653                         funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
654         }
655 }
656
657 static void
658 set_routing_links(struct drm_device *dev, struct drm_atomic_state *old_state)
659 {
660         int ncrtcs = old_state->dev->mode_config.num_crtc;
661         int i;
662
663         /* clear out existing links */
664         for (i = 0; i < old_state->num_connector; i++) {
665                 struct drm_connector *connector;
666
667                 connector = old_state->connectors[i];
668
669                 if (!connector || !connector->encoder)
670                         continue;
671
672                 WARN_ON(!connector->encoder->crtc);
673
674                 connector->encoder->crtc = NULL;
675                 connector->encoder = NULL;
676         }
677
678         /* set new links */
679         for (i = 0; i < old_state->num_connector; i++) {
680                 struct drm_connector *connector;
681
682                 connector = old_state->connectors[i];
683
684                 if (!connector || !connector->state->crtc)
685                         continue;
686
687                 if (WARN_ON(!connector->state->best_encoder))
688                         continue;
689
690                 connector->encoder = connector->state->best_encoder;
691                 connector->encoder->crtc = connector->state->crtc;
692         }
693
694         /* set legacy state in the crtc structure */
695         for (i = 0; i < ncrtcs; i++) {
696                 struct drm_crtc *crtc;
697
698                 crtc = old_state->crtcs[i];
699
700                 if (!crtc)
701                         continue;
702
703                 crtc->mode = crtc->state->mode;
704                 crtc->enabled = crtc->state->enable;
705                 crtc->x = crtc->primary->state->src_x >> 16;
706                 crtc->y = crtc->primary->state->src_y >> 16;
707         }
708 }
709
710 static void
711 crtc_set_mode(struct drm_device *dev, struct drm_atomic_state *old_state)
712 {
713         int ncrtcs = old_state->dev->mode_config.num_crtc;
714         int i;
715
716         for (i = 0; i < ncrtcs; i++) {
717                 const struct drm_crtc_helper_funcs *funcs;
718                 struct drm_crtc *crtc;
719
720                 crtc = old_state->crtcs[i];
721
722                 if (!crtc || !crtc->state->mode_changed)
723                         continue;
724
725                 funcs = crtc->helper_private;
726
727                 if (crtc->state->enable && funcs->mode_set_nofb) {
728                         DRM_DEBUG_ATOMIC("modeset on [CRTC:%d]\n",
729                                          crtc->base.id);
730
731                         funcs->mode_set_nofb(crtc);
732                 }
733         }
734
735         for (i = 0; i < old_state->num_connector; i++) {
736                 const struct drm_encoder_helper_funcs *funcs;
737                 struct drm_connector *connector;
738                 struct drm_crtc_state *new_crtc_state;
739                 struct drm_encoder *encoder;
740                 struct drm_display_mode *mode, *adjusted_mode;
741
742                 connector = old_state->connectors[i];
743
744                 if (!connector || !connector->state->best_encoder)
745                         continue;
746
747                 encoder = connector->state->best_encoder;
748                 funcs = encoder->helper_private;
749                 new_crtc_state = connector->state->crtc->state;
750                 mode = &new_crtc_state->mode;
751                 adjusted_mode = &new_crtc_state->adjusted_mode;
752
753                 if (!new_crtc_state->mode_changed)
754                         continue;
755
756                 DRM_DEBUG_ATOMIC("modeset on [ENCODER:%d:%s]\n",
757                                  encoder->base.id, encoder->name);
758
759                 /*
760                  * Each encoder has at most one connector (since we always steal
761                  * it away), so we won't call mode_set hooks twice.
762                  */
763                 if (funcs->mode_set)
764                         funcs->mode_set(encoder, mode, adjusted_mode);
765
766                 if (encoder->bridge && encoder->bridge->funcs->mode_set)
767                         encoder->bridge->funcs->mode_set(encoder->bridge,
768                                                          mode, adjusted_mode);
769         }
770 }
771
772 /**
773  * drm_atomic_helper_commit_modeset_disables - modeset commit to disable outputs
774  * @dev: DRM device
775  * @old_state: atomic state object with old state structures
776  *
777  * This function shuts down all the outputs that need to be shut down and
778  * prepares them (if required) with the new mode.
779  *
780  * For compatability with legacy crtc helpers this should be called before
781  * drm_atomic_helper_commit_planes(), which is what the default commit function
782  * does. But drivers with different needs can group the modeset commits together
783  * and do the plane commits at the end. This is useful for drivers doing runtime
784  * PM since planes updates then only happen when the CRTC is actually enabled.
785  */
786 void drm_atomic_helper_commit_modeset_disables(struct drm_device *dev,
787                                                struct drm_atomic_state *old_state)
788 {
789         disable_outputs(dev, old_state);
790         set_routing_links(dev, old_state);
791         crtc_set_mode(dev, old_state);
792 }
793 EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_disables);
794
795 /**
796  * drm_atomic_helper_commit_modeset_enables - modeset commit to enable outputs
797  * @dev: DRM device
798  * @old_state: atomic state object with old state structures
799  *
800  * This function enables all the outputs with the new configuration which had to
801  * be turned off for the update.
802  *
803  * For compatability with legacy crtc helpers this should be called after
804  * drm_atomic_helper_commit_planes(), which is what the default commit function
805  * does. But drivers with different needs can group the modeset commits together
806  * and do the plane commits at the end. This is useful for drivers doing runtime
807  * PM since planes updates then only happen when the CRTC is actually enabled.
808  */
809 void drm_atomic_helper_commit_modeset_enables(struct drm_device *dev,
810                                               struct drm_atomic_state *old_state)
811 {
812         int ncrtcs = old_state->dev->mode_config.num_crtc;
813         int i;
814
815         for (i = 0; i < ncrtcs; i++) {
816                 const struct drm_crtc_helper_funcs *funcs;
817                 struct drm_crtc *crtc;
818
819                 crtc = old_state->crtcs[i];
820
821                 /* Need to filter out CRTCs where only planes change. */
822                 if (!crtc || !needs_modeset(crtc->state))
823                         continue;
824
825                 if (!crtc->state->active)
826                         continue;
827
828                 funcs = crtc->helper_private;
829
830                 if (crtc->state->enable) {
831                         DRM_DEBUG_ATOMIC("enabling [CRTC:%d]\n",
832                                          crtc->base.id);
833
834                         if (funcs->enable)
835                                 funcs->enable(crtc);
836                         else
837                                 funcs->commit(crtc);
838                 }
839         }
840
841         for (i = 0; i < old_state->num_connector; i++) {
842                 const struct drm_encoder_helper_funcs *funcs;
843                 struct drm_connector *connector;
844                 struct drm_encoder *encoder;
845
846                 connector = old_state->connectors[i];
847
848                 if (!connector || !connector->state->best_encoder)
849                         continue;
850
851                 if (!connector->state->crtc->state->active ||
852                     !needs_modeset(connector->state->crtc->state))
853                         continue;
854
855                 encoder = connector->state->best_encoder;
856                 funcs = encoder->helper_private;
857
858                 DRM_DEBUG_ATOMIC("enabling [ENCODER:%d:%s]\n",
859                                  encoder->base.id, encoder->name);
860
861                 /*
862                  * Each encoder has at most one connector (since we always steal
863                  * it away), so we won't call enable hooks twice.
864                  */
865                 if (encoder->bridge)
866                         encoder->bridge->funcs->pre_enable(encoder->bridge);
867
868                 if (funcs->enable)
869                         funcs->enable(encoder);
870                 else
871                         funcs->commit(encoder);
872
873                 if (encoder->bridge)
874                         encoder->bridge->funcs->enable(encoder->bridge);
875         }
876 }
877 EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_enables);
878
879 static void wait_for_fences(struct drm_device *dev,
880                             struct drm_atomic_state *state)
881 {
882         int nplanes = dev->mode_config.num_total_plane;
883         int i;
884
885         for (i = 0; i < nplanes; i++) {
886                 struct drm_plane *plane = state->planes[i];
887
888                 if (!plane || !plane->state->fence)
889                         continue;
890
891                 WARN_ON(!plane->state->fb);
892
893                 fence_wait(plane->state->fence, false);
894                 fence_put(plane->state->fence);
895                 plane->state->fence = NULL;
896         }
897 }
898
899 static bool framebuffer_changed(struct drm_device *dev,
900                                 struct drm_atomic_state *old_state,
901                                 struct drm_crtc *crtc)
902 {
903         struct drm_plane *plane;
904         struct drm_plane_state *old_plane_state;
905         int nplanes = old_state->dev->mode_config.num_total_plane;
906         int i;
907
908         for (i = 0; i < nplanes; i++) {
909                 plane = old_state->planes[i];
910                 old_plane_state = old_state->plane_states[i];
911
912                 if (!plane)
913                         continue;
914
915                 if (plane->state->crtc != crtc &&
916                     old_plane_state->crtc != crtc)
917                         continue;
918
919                 if (plane->state->fb != old_plane_state->fb)
920                         return true;
921         }
922
923         return false;
924 }
925
926 /**
927  * drm_atomic_helper_wait_for_vblanks - wait for vblank on crtcs
928  * @dev: DRM device
929  * @old_state: atomic state object with old state structures
930  *
931  * Helper to, after atomic commit, wait for vblanks on all effected
932  * crtcs (ie. before cleaning up old framebuffers using
933  * drm_atomic_helper_cleanup_planes()). It will only wait on crtcs where the
934  * framebuffers have actually changed to optimize for the legacy cursor and
935  * plane update use-case.
936  */
937 void
938 drm_atomic_helper_wait_for_vblanks(struct drm_device *dev,
939                 struct drm_atomic_state *old_state)
940 {
941         struct drm_crtc *crtc;
942         struct drm_crtc_state *old_crtc_state;
943         int ncrtcs = old_state->dev->mode_config.num_crtc;
944         int i, ret;
945
946         for (i = 0; i < ncrtcs; i++) {
947                 crtc = old_state->crtcs[i];
948                 old_crtc_state = old_state->crtc_states[i];
949
950                 if (!crtc)
951                         continue;
952
953                 /* No one cares about the old state, so abuse it for tracking
954                  * and store whether we hold a vblank reference (and should do a
955                  * vblank wait) in the ->enable boolean. */
956                 old_crtc_state->enable = false;
957
958                 if (!crtc->state->enable)
959                         continue;
960
961                 /* Legacy cursor ioctls are completely unsynced, and userspace
962                  * relies on that (by doing tons of cursor updates). */
963                 if (old_state->legacy_cursor_update)
964                         continue;
965
966                 if (!framebuffer_changed(dev, old_state, crtc))
967                         continue;
968
969                 ret = drm_crtc_vblank_get(crtc);
970                 if (ret != 0)
971                         continue;
972
973                 old_crtc_state->enable = true;
974                 old_crtc_state->last_vblank_count = drm_vblank_count(dev, i);
975         }
976
977         for (i = 0; i < ncrtcs; i++) {
978                 crtc = old_state->crtcs[i];
979                 old_crtc_state = old_state->crtc_states[i];
980
981                 if (!crtc || !old_crtc_state->enable)
982                         continue;
983
984                 ret = wait_event_timeout(dev->vblank[i].queue,
985                                 old_crtc_state->last_vblank_count !=
986                                         drm_vblank_count(dev, i),
987                                 msecs_to_jiffies(50));
988
989                 drm_crtc_vblank_put(crtc);
990         }
991 }
992 EXPORT_SYMBOL(drm_atomic_helper_wait_for_vblanks);
993
994 /**
995  * drm_atomic_helper_commit - commit validated state object
996  * @dev: DRM device
997  * @state: the driver state object
998  * @async: asynchronous commit
999  *
1000  * This function commits a with drm_atomic_helper_check() pre-validated state
1001  * object. This can still fail when e.g. the framebuffer reservation fails. For
1002  * now this doesn't implement asynchronous commits.
1003  *
1004  * RETURNS
1005  * Zero for success or -errno.
1006  */
1007 int drm_atomic_helper_commit(struct drm_device *dev,
1008                              struct drm_atomic_state *state,
1009                              bool async)
1010 {
1011         int ret;
1012
1013         if (async)
1014                 return -EBUSY;
1015
1016         ret = drm_atomic_helper_prepare_planes(dev, state);
1017         if (ret)
1018                 return ret;
1019
1020         /*
1021          * This is the point of no return - everything below never fails except
1022          * when the hw goes bonghits. Which means we can commit the new state on
1023          * the software side now.
1024          */
1025
1026         drm_atomic_helper_swap_state(dev, state);
1027
1028         /*
1029          * Everything below can be run asynchronously without the need to grab
1030          * any modeset locks at all under one condition: It must be guaranteed
1031          * that the asynchronous work has either been cancelled (if the driver
1032          * supports it, which at least requires that the framebuffers get
1033          * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
1034          * before the new state gets committed on the software side with
1035          * drm_atomic_helper_swap_state().
1036          *
1037          * This scheme allows new atomic state updates to be prepared and
1038          * checked in parallel to the asynchronous completion of the previous
1039          * update. Which is important since compositors need to figure out the
1040          * composition of the next frame right after having submitted the
1041          * current layout.
1042          */
1043
1044         wait_for_fences(dev, state);
1045
1046         drm_atomic_helper_commit_modeset_disables(dev, state);
1047
1048         drm_atomic_helper_commit_planes(dev, state);
1049
1050         drm_atomic_helper_commit_modeset_enables(dev, state);
1051
1052         drm_atomic_helper_wait_for_vblanks(dev, state);
1053
1054         drm_atomic_helper_cleanup_planes(dev, state);
1055
1056         drm_atomic_state_free(state);
1057
1058         return 0;
1059 }
1060 EXPORT_SYMBOL(drm_atomic_helper_commit);
1061
1062 /**
1063  * DOC: implementing async commit
1064  *
1065  * For now the atomic helpers don't support async commit directly. If there is
1066  * real need it could be added though, using the dma-buf fence infrastructure
1067  * for generic synchronization with outstanding rendering.
1068  *
1069  * For now drivers have to implement async commit themselves, with the following
1070  * sequence being the recommended one:
1071  *
1072  * 1. Run drm_atomic_helper_prepare_planes() first. This is the only function
1073  * which commit needs to call which can fail, so we want to run it first and
1074  * synchronously.
1075  *
1076  * 2. Synchronize with any outstanding asynchronous commit worker threads which
1077  * might be affected the new state update. This can be done by either cancelling
1078  * or flushing the work items, depending upon whether the driver can deal with
1079  * cancelled updates. Note that it is important to ensure that the framebuffer
1080  * cleanup is still done when cancelling.
1081  *
1082  * For sufficient parallelism it is recommended to have a work item per crtc
1083  * (for updates which don't touch global state) and a global one. Then we only
1084  * need to synchronize with the crtc work items for changed crtcs and the global
1085  * work item, which allows nice concurrent updates on disjoint sets of crtcs.
1086  *
1087  * 3. The software state is updated synchronously with
1088  * drm_atomic_helper_swap_state. Doing this under the protection of all modeset
1089  * locks means concurrent callers never see inconsistent state. And doing this
1090  * while it's guaranteed that no relevant async worker runs means that async
1091  * workers do not need grab any locks. Actually they must not grab locks, for
1092  * otherwise the work flushing will deadlock.
1093  *
1094  * 4. Schedule a work item to do all subsequent steps, using the split-out
1095  * commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and
1096  * then cleaning up the framebuffers after the old framebuffer is no longer
1097  * being displayed.
1098  */
1099
1100 /**
1101  * drm_atomic_helper_prepare_planes - prepare plane resources before commit
1102  * @dev: DRM device
1103  * @state: atomic state object with new state structures
1104  *
1105  * This function prepares plane state, specifically framebuffers, for the new
1106  * configuration. If any failure is encountered this function will call
1107  * ->cleanup_fb on any already successfully prepared framebuffer.
1108  *
1109  * Returns:
1110  * 0 on success, negative error code on failure.
1111  */
1112 int drm_atomic_helper_prepare_planes(struct drm_device *dev,
1113                                      struct drm_atomic_state *state)
1114 {
1115         int nplanes = dev->mode_config.num_total_plane;
1116         int ret, i;
1117
1118         for (i = 0; i < nplanes; i++) {
1119                 const struct drm_plane_helper_funcs *funcs;
1120                 struct drm_plane *plane = state->planes[i];
1121                 struct drm_plane_state *plane_state = state->plane_states[i];
1122                 struct drm_framebuffer *fb;
1123
1124                 if (!plane)
1125                         continue;
1126
1127                 funcs = plane->helper_private;
1128
1129                 fb = plane_state->fb;
1130
1131                 if (fb && funcs->prepare_fb) {
1132                         ret = funcs->prepare_fb(plane, fb, plane_state);
1133                         if (ret)
1134                                 goto fail;
1135                 }
1136         }
1137
1138         return 0;
1139
1140 fail:
1141         for (i--; i >= 0; i--) {
1142                 const struct drm_plane_helper_funcs *funcs;
1143                 struct drm_plane *plane = state->planes[i];
1144                 struct drm_plane_state *plane_state = state->plane_states[i];
1145                 struct drm_framebuffer *fb;
1146
1147                 if (!plane)
1148                         continue;
1149
1150                 funcs = plane->helper_private;
1151
1152                 fb = state->plane_states[i]->fb;
1153
1154                 if (fb && funcs->cleanup_fb)
1155                         funcs->cleanup_fb(plane, fb, plane_state);
1156
1157         }
1158
1159         return ret;
1160 }
1161 EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
1162
1163 /**
1164  * drm_atomic_helper_commit_planes - commit plane state
1165  * @dev: DRM device
1166  * @old_state: atomic state object with old state structures
1167  *
1168  * This function commits the new plane state using the plane and atomic helper
1169  * functions for planes and crtcs. It assumes that the atomic state has already
1170  * been pushed into the relevant object state pointers, since this step can no
1171  * longer fail.
1172  *
1173  * It still requires the global state object @old_state to know which planes and
1174  * crtcs need to be updated though.
1175  */
1176 void drm_atomic_helper_commit_planes(struct drm_device *dev,
1177                                      struct drm_atomic_state *old_state)
1178 {
1179         int nplanes = dev->mode_config.num_total_plane;
1180         int ncrtcs = dev->mode_config.num_crtc;
1181         int i;
1182
1183         for (i = 0; i < ncrtcs; i++) {
1184                 const struct drm_crtc_helper_funcs *funcs;
1185                 struct drm_crtc *crtc = old_state->crtcs[i];
1186
1187                 if (!crtc)
1188                         continue;
1189
1190                 funcs = crtc->helper_private;
1191
1192                 if (!funcs || !funcs->atomic_begin)
1193                         continue;
1194
1195                 funcs->atomic_begin(crtc);
1196         }
1197
1198         for (i = 0; i < nplanes; i++) {
1199                 const struct drm_plane_helper_funcs *funcs;
1200                 struct drm_plane *plane = old_state->planes[i];
1201                 struct drm_plane_state *old_plane_state;
1202
1203                 if (!plane)
1204                         continue;
1205
1206                 funcs = plane->helper_private;
1207
1208                 if (!funcs)
1209                         continue;
1210
1211                 old_plane_state = old_state->plane_states[i];
1212
1213                 /*
1214                  * Special-case disabling the plane if drivers support it.
1215                  */
1216                 if (drm_atomic_plane_disabling(plane, old_plane_state) &&
1217                     funcs->atomic_disable)
1218                         funcs->atomic_disable(plane, old_plane_state);
1219                 else
1220                         funcs->atomic_update(plane, old_plane_state);
1221         }
1222
1223         for (i = 0; i < ncrtcs; i++) {
1224                 const struct drm_crtc_helper_funcs *funcs;
1225                 struct drm_crtc *crtc = old_state->crtcs[i];
1226
1227                 if (!crtc)
1228                         continue;
1229
1230                 funcs = crtc->helper_private;
1231
1232                 if (!funcs || !funcs->atomic_flush)
1233                         continue;
1234
1235                 funcs->atomic_flush(crtc);
1236         }
1237 }
1238 EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
1239
1240 /**
1241  * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
1242  * @dev: DRM device
1243  * @old_state: atomic state object with old state structures
1244  *
1245  * This function cleans up plane state, specifically framebuffers, from the old
1246  * configuration. Hence the old configuration must be perserved in @old_state to
1247  * be able to call this function.
1248  *
1249  * This function must also be called on the new state when the atomic update
1250  * fails at any point after calling drm_atomic_helper_prepare_planes().
1251  */
1252 void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
1253                                       struct drm_atomic_state *old_state)
1254 {
1255         int nplanes = dev->mode_config.num_total_plane;
1256         int i;
1257
1258         for (i = 0; i < nplanes; i++) {
1259                 const struct drm_plane_helper_funcs *funcs;
1260                 struct drm_plane *plane = old_state->planes[i];
1261                 struct drm_plane_state *plane_state = old_state->plane_states[i];
1262                 struct drm_framebuffer *old_fb;
1263
1264                 if (!plane)
1265                         continue;
1266
1267                 funcs = plane->helper_private;
1268
1269                 old_fb = plane_state->fb;
1270
1271                 if (old_fb && funcs->cleanup_fb)
1272                         funcs->cleanup_fb(plane, old_fb, plane_state);
1273         }
1274 }
1275 EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
1276
1277 /**
1278  * drm_atomic_helper_swap_state - store atomic state into current sw state
1279  * @dev: DRM device
1280  * @state: atomic state
1281  *
1282  * This function stores the atomic state into the current state pointers in all
1283  * driver objects. It should be called after all failing steps have been done
1284  * and succeeded, but before the actual hardware state is committed.
1285  *
1286  * For cleanup and error recovery the current state for all changed objects will
1287  * be swaped into @state.
1288  *
1289  * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
1290  *
1291  * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
1292  *
1293  * 2. Do any other steps that might fail.
1294  *
1295  * 3. Put the staged state into the current state pointers with this function.
1296  *
1297  * 4. Actually commit the hardware state.
1298  *
1299  * 5. Call drm_atomic_helper_cleanup_planes with @state, which since step 3
1300  * contains the old state. Also do any other cleanup required with that state.
1301  */
1302 void drm_atomic_helper_swap_state(struct drm_device *dev,
1303                                   struct drm_atomic_state *state)
1304 {
1305         int i;
1306
1307         for (i = 0; i < dev->mode_config.num_connector; i++) {
1308                 struct drm_connector *connector = state->connectors[i];
1309
1310                 if (!connector)
1311                         continue;
1312
1313                 connector->state->state = state;
1314                 swap(state->connector_states[i], connector->state);
1315                 connector->state->state = NULL;
1316         }
1317
1318         for (i = 0; i < dev->mode_config.num_crtc; i++) {
1319                 struct drm_crtc *crtc = state->crtcs[i];
1320
1321                 if (!crtc)
1322                         continue;
1323
1324                 crtc->state->state = state;
1325                 swap(state->crtc_states[i], crtc->state);
1326                 crtc->state->state = NULL;
1327         }
1328
1329         for (i = 0; i < dev->mode_config.num_total_plane; i++) {
1330                 struct drm_plane *plane = state->planes[i];
1331
1332                 if (!plane)
1333                         continue;
1334
1335                 plane->state->state = state;
1336                 swap(state->plane_states[i], plane->state);
1337                 plane->state->state = NULL;
1338         }
1339 }
1340 EXPORT_SYMBOL(drm_atomic_helper_swap_state);
1341
1342 /**
1343  * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
1344  * @plane: plane object to update
1345  * @crtc: owning CRTC of owning plane
1346  * @fb: framebuffer to flip onto plane
1347  * @crtc_x: x offset of primary plane on crtc
1348  * @crtc_y: y offset of primary plane on crtc
1349  * @crtc_w: width of primary plane rectangle on crtc
1350  * @crtc_h: height of primary plane rectangle on crtc
1351  * @src_x: x offset of @fb for panning
1352  * @src_y: y offset of @fb for panning
1353  * @src_w: width of source rectangle in @fb
1354  * @src_h: height of source rectangle in @fb
1355  *
1356  * Provides a default plane update handler using the atomic driver interface.
1357  *
1358  * RETURNS:
1359  * Zero on success, error code on failure
1360  */
1361 int drm_atomic_helper_update_plane(struct drm_plane *plane,
1362                                    struct drm_crtc *crtc,
1363                                    struct drm_framebuffer *fb,
1364                                    int crtc_x, int crtc_y,
1365                                    unsigned int crtc_w, unsigned int crtc_h,
1366                                    uint32_t src_x, uint32_t src_y,
1367                                    uint32_t src_w, uint32_t src_h)
1368 {
1369         struct drm_atomic_state *state;
1370         struct drm_plane_state *plane_state;
1371         int ret = 0;
1372
1373         state = drm_atomic_state_alloc(plane->dev);
1374         if (!state)
1375                 return -ENOMEM;
1376
1377         state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1378 retry:
1379         plane_state = drm_atomic_get_plane_state(state, plane);
1380         if (IS_ERR(plane_state)) {
1381                 ret = PTR_ERR(plane_state);
1382                 goto fail;
1383         }
1384
1385         ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
1386         if (ret != 0)
1387                 goto fail;
1388         drm_atomic_set_fb_for_plane(plane_state, fb);
1389         plane_state->crtc_x = crtc_x;
1390         plane_state->crtc_y = crtc_y;
1391         plane_state->crtc_h = crtc_h;
1392         plane_state->crtc_w = crtc_w;
1393         plane_state->src_x = src_x;
1394         plane_state->src_y = src_y;
1395         plane_state->src_h = src_h;
1396         plane_state->src_w = src_w;
1397
1398         ret = drm_atomic_commit(state);
1399         if (ret != 0)
1400                 goto fail;
1401
1402         if (plane == crtc->cursor)
1403                 state->legacy_cursor_update = true;
1404
1405         /* Driver takes ownership of state on successful commit. */
1406         return 0;
1407 fail:
1408         if (ret == -EDEADLK)
1409                 goto backoff;
1410
1411         drm_atomic_state_free(state);
1412
1413         return ret;
1414 backoff:
1415         drm_atomic_state_clear(state);
1416         drm_atomic_legacy_backoff(state);
1417
1418         /*
1419          * Someone might have exchanged the framebuffer while we dropped locks
1420          * in the backoff code. We need to fix up the fb refcount tracking the
1421          * core does for us.
1422          */
1423         plane->old_fb = plane->fb;
1424
1425         goto retry;
1426 }
1427 EXPORT_SYMBOL(drm_atomic_helper_update_plane);
1428
1429 /**
1430  * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
1431  * @plane: plane to disable
1432  *
1433  * Provides a default plane disable handler using the atomic driver interface.
1434  *
1435  * RETURNS:
1436  * Zero on success, error code on failure
1437  */
1438 int drm_atomic_helper_disable_plane(struct drm_plane *plane)
1439 {
1440         struct drm_atomic_state *state;
1441         struct drm_plane_state *plane_state;
1442         int ret = 0;
1443
1444         /*
1445          * FIXME: Without plane->crtc set we can't get at the implicit legacy
1446          * acquire context. The real fix will be to wire the acquire ctx through
1447          * everywhere we need it, but meanwhile prevent chaos by just skipping
1448          * this noop. The critical case is the cursor ioctls which a) only grab
1449          * crtc/cursor-plane locks (so we need the crtc to get at the right
1450          * acquire context) and b) can try to disable the plane multiple times.
1451          */
1452         if (!plane->crtc)
1453                 return 0;
1454
1455         state = drm_atomic_state_alloc(plane->dev);
1456         if (!state)
1457                 return -ENOMEM;
1458
1459         state->acquire_ctx = drm_modeset_legacy_acquire_ctx(plane->crtc);
1460 retry:
1461         plane_state = drm_atomic_get_plane_state(state, plane);
1462         if (IS_ERR(plane_state)) {
1463                 ret = PTR_ERR(plane_state);
1464                 goto fail;
1465         }
1466
1467         ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
1468         if (ret != 0)
1469                 goto fail;
1470         drm_atomic_set_fb_for_plane(plane_state, NULL);
1471         plane_state->crtc_x = 0;
1472         plane_state->crtc_y = 0;
1473         plane_state->crtc_h = 0;
1474         plane_state->crtc_w = 0;
1475         plane_state->src_x = 0;
1476         plane_state->src_y = 0;
1477         plane_state->src_h = 0;
1478         plane_state->src_w = 0;
1479
1480         if (plane == plane->crtc->cursor)
1481                 state->legacy_cursor_update = true;
1482
1483         ret = drm_atomic_commit(state);
1484         if (ret != 0)
1485                 goto fail;
1486
1487         /* Driver takes ownership of state on successful commit. */
1488         return 0;
1489 fail:
1490         if (ret == -EDEADLK)
1491                 goto backoff;
1492
1493         drm_atomic_state_free(state);
1494
1495         return ret;
1496 backoff:
1497         drm_atomic_state_clear(state);
1498         drm_atomic_legacy_backoff(state);
1499
1500         /*
1501          * Someone might have exchanged the framebuffer while we dropped locks
1502          * in the backoff code. We need to fix up the fb refcount tracking the
1503          * core does for us.
1504          */
1505         plane->old_fb = plane->fb;
1506
1507         goto retry;
1508 }
1509 EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
1510
1511 static int update_output_state(struct drm_atomic_state *state,
1512                                struct drm_mode_set *set)
1513 {
1514         struct drm_device *dev = set->crtc->dev;
1515         struct drm_connector_state *conn_state;
1516         int ncrtcs = state->dev->mode_config.num_crtc;
1517         int ret, i, j;
1518
1519         ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
1520                                state->acquire_ctx);
1521         if (ret)
1522                 return ret;
1523
1524         /* First grab all affected connector/crtc states. */
1525         for (i = 0; i < set->num_connectors; i++) {
1526                 conn_state = drm_atomic_get_connector_state(state,
1527                                                             set->connectors[i]);
1528                 if (IS_ERR(conn_state))
1529                         return PTR_ERR(conn_state);
1530         }
1531
1532         for (i = 0; i < ncrtcs; i++) {
1533                 struct drm_crtc *crtc = state->crtcs[i];
1534
1535                 if (!crtc)
1536                         continue;
1537
1538                 ret = drm_atomic_add_affected_connectors(state, crtc);
1539                 if (ret)
1540                         return ret;
1541         }
1542
1543         /* Then recompute connector->crtc links and crtc enabling state. */
1544         for (i = 0; i < state->num_connector; i++) {
1545                 struct drm_connector *connector;
1546
1547                 connector = state->connectors[i];
1548                 conn_state = state->connector_states[i];
1549
1550                 if (!connector)
1551                         continue;
1552
1553                 if (conn_state->crtc == set->crtc) {
1554                         ret = drm_atomic_set_crtc_for_connector(conn_state,
1555                                                                 NULL);
1556                         if (ret)
1557                                 return ret;
1558                 }
1559
1560                 for (j = 0; j < set->num_connectors; j++) {
1561                         if (set->connectors[j] == connector) {
1562                                 ret = drm_atomic_set_crtc_for_connector(conn_state,
1563                                                                         set->crtc);
1564                                 if (ret)
1565                                         return ret;
1566                                 break;
1567                         }
1568                 }
1569         }
1570
1571         for (i = 0; i < ncrtcs; i++) {
1572                 struct drm_crtc *crtc = state->crtcs[i];
1573                 struct drm_crtc_state *crtc_state = state->crtc_states[i];
1574
1575                 if (!crtc)
1576                         continue;
1577
1578                 /* Don't update ->enable for the CRTC in the set_config request,
1579                  * since a mismatch would indicate a bug in the upper layers.
1580                  * The actual modeset code later on will catch any
1581                  * inconsistencies here. */
1582                 if (crtc == set->crtc)
1583                         continue;
1584
1585                 crtc_state->enable =
1586                         drm_atomic_connectors_for_crtc(state, crtc);
1587         }
1588
1589         return 0;
1590 }
1591
1592 /**
1593  * drm_atomic_helper_set_config - set a new config from userspace
1594  * @set: mode set configuration
1595  *
1596  * Provides a default crtc set_config handler using the atomic driver interface.
1597  *
1598  * Returns:
1599  * Returns 0 on success, negative errno numbers on failure.
1600  */
1601 int drm_atomic_helper_set_config(struct drm_mode_set *set)
1602 {
1603         struct drm_atomic_state *state;
1604         struct drm_crtc *crtc = set->crtc;
1605         struct drm_crtc_state *crtc_state;
1606         struct drm_plane_state *primary_state;
1607         int ret = 0;
1608
1609         state = drm_atomic_state_alloc(crtc->dev);
1610         if (!state)
1611                 return -ENOMEM;
1612
1613         state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1614 retry:
1615         crtc_state = drm_atomic_get_crtc_state(state, crtc);
1616         if (IS_ERR(crtc_state)) {
1617                 ret = PTR_ERR(crtc_state);
1618                 goto fail;
1619         }
1620
1621         primary_state = drm_atomic_get_plane_state(state, crtc->primary);
1622         if (IS_ERR(primary_state)) {
1623                 ret = PTR_ERR(primary_state);
1624                 goto fail;
1625         }
1626
1627         if (!set->mode) {
1628                 WARN_ON(set->fb);
1629                 WARN_ON(set->num_connectors);
1630
1631                 crtc_state->enable = false;
1632                 crtc_state->active = false;
1633
1634                 ret = drm_atomic_set_crtc_for_plane(primary_state, NULL);
1635                 if (ret != 0)
1636                         goto fail;
1637
1638                 drm_atomic_set_fb_for_plane(primary_state, NULL);
1639
1640                 goto commit;
1641         }
1642
1643         WARN_ON(!set->fb);
1644         WARN_ON(!set->num_connectors);
1645
1646         crtc_state->enable = true;
1647         crtc_state->active = true;
1648         drm_mode_copy(&crtc_state->mode, set->mode);
1649
1650         ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
1651         if (ret != 0)
1652                 goto fail;
1653         drm_atomic_set_fb_for_plane(primary_state, set->fb);
1654         primary_state->crtc_x = 0;
1655         primary_state->crtc_y = 0;
1656         primary_state->crtc_h = set->mode->vdisplay;
1657         primary_state->crtc_w = set->mode->hdisplay;
1658         primary_state->src_x = set->x << 16;
1659         primary_state->src_y = set->y << 16;
1660         primary_state->src_h = set->mode->vdisplay << 16;
1661         primary_state->src_w = set->mode->hdisplay << 16;
1662
1663 commit:
1664         ret = update_output_state(state, set);
1665         if (ret)
1666                 goto fail;
1667
1668         ret = drm_atomic_commit(state);
1669         if (ret != 0)
1670                 goto fail;
1671
1672         /* Driver takes ownership of state on successful commit. */
1673         return 0;
1674 fail:
1675         if (ret == -EDEADLK)
1676                 goto backoff;
1677
1678         drm_atomic_state_free(state);
1679
1680         return ret;
1681 backoff:
1682         drm_atomic_state_clear(state);
1683         drm_atomic_legacy_backoff(state);
1684
1685         /*
1686          * Someone might have exchanged the framebuffer while we dropped locks
1687          * in the backoff code. We need to fix up the fb refcount tracking the
1688          * core does for us.
1689          */
1690         crtc->primary->old_fb = crtc->primary->fb;
1691
1692         goto retry;
1693 }
1694 EXPORT_SYMBOL(drm_atomic_helper_set_config);
1695
1696 /**
1697  * drm_atomic_helper_crtc_set_property - helper for crtc properties
1698  * @crtc: DRM crtc
1699  * @property: DRM property
1700  * @val: value of property
1701  *
1702  * Provides a default crtc set_property handler using the atomic driver
1703  * interface.
1704  *
1705  * RETURNS:
1706  * Zero on success, error code on failure
1707  */
1708 int
1709 drm_atomic_helper_crtc_set_property(struct drm_crtc *crtc,
1710                                     struct drm_property *property,
1711                                     uint64_t val)
1712 {
1713         struct drm_atomic_state *state;
1714         struct drm_crtc_state *crtc_state;
1715         int ret = 0;
1716
1717         state = drm_atomic_state_alloc(crtc->dev);
1718         if (!state)
1719                 return -ENOMEM;
1720
1721         /* ->set_property is always called with all locks held. */
1722         state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
1723 retry:
1724         crtc_state = drm_atomic_get_crtc_state(state, crtc);
1725         if (IS_ERR(crtc_state)) {
1726                 ret = PTR_ERR(crtc_state);
1727                 goto fail;
1728         }
1729
1730         ret = drm_atomic_crtc_set_property(crtc, crtc_state,
1731                         property, val);
1732         if (ret)
1733                 goto fail;
1734
1735         ret = drm_atomic_commit(state);
1736         if (ret != 0)
1737                 goto fail;
1738
1739         /* Driver takes ownership of state on successful commit. */
1740         return 0;
1741 fail:
1742         if (ret == -EDEADLK)
1743                 goto backoff;
1744
1745         drm_atomic_state_free(state);
1746
1747         return ret;
1748 backoff:
1749         drm_atomic_state_clear(state);
1750         drm_atomic_legacy_backoff(state);
1751
1752         goto retry;
1753 }
1754 EXPORT_SYMBOL(drm_atomic_helper_crtc_set_property);
1755
1756 /**
1757  * drm_atomic_helper_plane_set_property - helper for plane properties
1758  * @plane: DRM plane
1759  * @property: DRM property
1760  * @val: value of property
1761  *
1762  * Provides a default plane set_property handler using the atomic driver
1763  * interface.
1764  *
1765  * RETURNS:
1766  * Zero on success, error code on failure
1767  */
1768 int
1769 drm_atomic_helper_plane_set_property(struct drm_plane *plane,
1770                                     struct drm_property *property,
1771                                     uint64_t val)
1772 {
1773         struct drm_atomic_state *state;
1774         struct drm_plane_state *plane_state;
1775         int ret = 0;
1776
1777         state = drm_atomic_state_alloc(plane->dev);
1778         if (!state)
1779                 return -ENOMEM;
1780
1781         /* ->set_property is always called with all locks held. */
1782         state->acquire_ctx = plane->dev->mode_config.acquire_ctx;
1783 retry:
1784         plane_state = drm_atomic_get_plane_state(state, plane);
1785         if (IS_ERR(plane_state)) {
1786                 ret = PTR_ERR(plane_state);
1787                 goto fail;
1788         }
1789
1790         ret = drm_atomic_plane_set_property(plane, plane_state,
1791                         property, val);
1792         if (ret)
1793                 goto fail;
1794
1795         ret = drm_atomic_commit(state);
1796         if (ret != 0)
1797                 goto fail;
1798
1799         /* Driver takes ownership of state on successful commit. */
1800         return 0;
1801 fail:
1802         if (ret == -EDEADLK)
1803                 goto backoff;
1804
1805         drm_atomic_state_free(state);
1806
1807         return ret;
1808 backoff:
1809         drm_atomic_state_clear(state);
1810         drm_atomic_legacy_backoff(state);
1811
1812         goto retry;
1813 }
1814 EXPORT_SYMBOL(drm_atomic_helper_plane_set_property);
1815
1816 /**
1817  * drm_atomic_helper_connector_set_property - helper for connector properties
1818  * @connector: DRM connector
1819  * @property: DRM property
1820  * @val: value of property
1821  *
1822  * Provides a default connector set_property handler using the atomic driver
1823  * interface.
1824  *
1825  * RETURNS:
1826  * Zero on success, error code on failure
1827  */
1828 int
1829 drm_atomic_helper_connector_set_property(struct drm_connector *connector,
1830                                     struct drm_property *property,
1831                                     uint64_t val)
1832 {
1833         struct drm_atomic_state *state;
1834         struct drm_connector_state *connector_state;
1835         int ret = 0;
1836
1837         state = drm_atomic_state_alloc(connector->dev);
1838         if (!state)
1839                 return -ENOMEM;
1840
1841         /* ->set_property is always called with all locks held. */
1842         state->acquire_ctx = connector->dev->mode_config.acquire_ctx;
1843 retry:
1844         connector_state = drm_atomic_get_connector_state(state, connector);
1845         if (IS_ERR(connector_state)) {
1846                 ret = PTR_ERR(connector_state);
1847                 goto fail;
1848         }
1849
1850         ret = drm_atomic_connector_set_property(connector, connector_state,
1851                         property, val);
1852         if (ret)
1853                 goto fail;
1854
1855         ret = drm_atomic_commit(state);
1856         if (ret != 0)
1857                 goto fail;
1858
1859         /* Driver takes ownership of state on successful commit. */
1860         return 0;
1861 fail:
1862         if (ret == -EDEADLK)
1863                 goto backoff;
1864
1865         drm_atomic_state_free(state);
1866
1867         return ret;
1868 backoff:
1869         drm_atomic_state_clear(state);
1870         drm_atomic_legacy_backoff(state);
1871
1872         goto retry;
1873 }
1874 EXPORT_SYMBOL(drm_atomic_helper_connector_set_property);
1875
1876 /**
1877  * drm_atomic_helper_page_flip - execute a legacy page flip
1878  * @crtc: DRM crtc
1879  * @fb: DRM framebuffer
1880  * @event: optional DRM event to signal upon completion
1881  * @flags: flip flags for non-vblank sync'ed updates
1882  *
1883  * Provides a default page flip implementation using the atomic driver interface.
1884  *
1885  * Note that for now so called async page flips (i.e. updates which are not
1886  * synchronized to vblank) are not supported, since the atomic interfaces have
1887  * no provisions for this yet.
1888  *
1889  * Returns:
1890  * Returns 0 on success, negative errno numbers on failure.
1891  */
1892 int drm_atomic_helper_page_flip(struct drm_crtc *crtc,
1893                                 struct drm_framebuffer *fb,
1894                                 struct drm_pending_vblank_event *event,
1895                                 uint32_t flags)
1896 {
1897         struct drm_plane *plane = crtc->primary;
1898         struct drm_atomic_state *state;
1899         struct drm_plane_state *plane_state;
1900         struct drm_crtc_state *crtc_state;
1901         int ret = 0;
1902
1903         if (flags & DRM_MODE_PAGE_FLIP_ASYNC)
1904                 return -EINVAL;
1905
1906         state = drm_atomic_state_alloc(plane->dev);
1907         if (!state)
1908                 return -ENOMEM;
1909
1910         state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1911 retry:
1912         crtc_state = drm_atomic_get_crtc_state(state, crtc);
1913         if (IS_ERR(crtc_state)) {
1914                 ret = PTR_ERR(crtc_state);
1915                 goto fail;
1916         }
1917         crtc_state->event = event;
1918
1919         plane_state = drm_atomic_get_plane_state(state, plane);
1920         if (IS_ERR(plane_state)) {
1921                 ret = PTR_ERR(plane_state);
1922                 goto fail;
1923         }
1924
1925         ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
1926         if (ret != 0)
1927                 goto fail;
1928         drm_atomic_set_fb_for_plane(plane_state, fb);
1929
1930         ret = drm_atomic_async_commit(state);
1931         if (ret != 0)
1932                 goto fail;
1933
1934         /* TODO: ->page_flip is the only driver callback where the core
1935          * doesn't update plane->fb. For now patch it up here. */
1936         plane->fb = plane->state->fb;
1937
1938         /* Driver takes ownership of state on successful async commit. */
1939         return 0;
1940 fail:
1941         if (ret == -EDEADLK)
1942                 goto backoff;
1943
1944         drm_atomic_state_free(state);
1945
1946         return ret;
1947 backoff:
1948         drm_atomic_state_clear(state);
1949         drm_atomic_legacy_backoff(state);
1950
1951         /*
1952          * Someone might have exchanged the framebuffer while we dropped locks
1953          * in the backoff code. We need to fix up the fb refcount tracking the
1954          * core does for us.
1955          */
1956         plane->old_fb = plane->fb;
1957
1958         goto retry;
1959 }
1960 EXPORT_SYMBOL(drm_atomic_helper_page_flip);
1961
1962 /**
1963  * drm_atomic_helper_connector_dpms() - connector dpms helper implementation
1964  * @connector: affected connector
1965  * @mode: DPMS mode
1966  *
1967  * This is the main helper function provided by the atomic helper framework for
1968  * implementing the legacy DPMS connector interface. It computes the new desired
1969  * ->active state for the corresponding CRTC (if the connector is enabled) and
1970  *  updates it.
1971  */
1972 void drm_atomic_helper_connector_dpms(struct drm_connector *connector,
1973                                       int mode)
1974 {
1975         struct drm_mode_config *config = &connector->dev->mode_config;
1976         struct drm_atomic_state *state;
1977         struct drm_crtc_state *crtc_state;
1978         struct drm_crtc *crtc;
1979         struct drm_connector *tmp_connector;
1980         int ret;
1981         bool active = false;
1982
1983         if (mode != DRM_MODE_DPMS_ON)
1984                 mode = DRM_MODE_DPMS_OFF;
1985
1986         connector->dpms = mode;
1987         crtc = connector->state->crtc;
1988
1989         if (!crtc)
1990                 return;
1991
1992         /* FIXME: ->dpms has no return value so can't forward the -ENOMEM. */
1993         state = drm_atomic_state_alloc(connector->dev);
1994         if (!state)
1995                 return;
1996
1997         state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1998 retry:
1999         crtc_state = drm_atomic_get_crtc_state(state, crtc);
2000         if (IS_ERR(crtc_state))
2001                 return;
2002
2003         WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
2004
2005         list_for_each_entry(tmp_connector, &config->connector_list, head) {
2006                 if (tmp_connector->state->crtc != crtc)
2007                         continue;
2008
2009                 if (tmp_connector->dpms == DRM_MODE_DPMS_ON) {
2010                         active = true;
2011                         break;
2012                 }
2013         }
2014         crtc_state->active = active;
2015
2016         ret = drm_atomic_commit(state);
2017         if (ret != 0)
2018                 goto fail;
2019
2020         /* Driver takes ownership of state on successful async commit. */
2021         return;
2022 fail:
2023         if (ret == -EDEADLK)
2024                 goto backoff;
2025
2026         drm_atomic_state_free(state);
2027
2028         WARN(1, "Driver bug: Changing ->active failed with ret=%i\n", ret);
2029
2030         return;
2031 backoff:
2032         drm_atomic_state_clear(state);
2033         drm_atomic_legacy_backoff(state);
2034
2035         goto retry;
2036 }
2037 EXPORT_SYMBOL(drm_atomic_helper_connector_dpms);
2038
2039 /**
2040  * DOC: atomic state reset and initialization
2041  *
2042  * Both the drm core and the atomic helpers assume that there is always the full
2043  * and correct atomic software state for all connectors, CRTCs and planes
2044  * available. Which is a bit a problem on driver load and also after system
2045  * suspend. One way to solve this is to have a hardware state read-out
2046  * infrastructure which reconstructs the full software state (e.g. the i915
2047  * driver).
2048  *
2049  * The simpler solution is to just reset the software state to everything off,
2050  * which is easiest to do by calling drm_mode_config_reset(). To facilitate this
2051  * the atomic helpers provide default reset implementations for all hooks.
2052  */
2053
2054 /**
2055  * drm_atomic_helper_crtc_reset - default ->reset hook for CRTCs
2056  * @crtc: drm CRTC
2057  *
2058  * Resets the atomic state for @crtc by freeing the state pointer (which might
2059  * be NULL, e.g. at driver load time) and allocating a new empty state object.
2060  */
2061 void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc)
2062 {
2063         kfree(crtc->state);
2064         crtc->state = kzalloc(sizeof(*crtc->state), GFP_KERNEL);
2065
2066         if (crtc->state)
2067                 crtc->state->crtc = crtc;
2068 }
2069 EXPORT_SYMBOL(drm_atomic_helper_crtc_reset);
2070
2071 /**
2072  * __drm_atomic_helper_crtc_duplicate_state - copy atomic CRTC state
2073  * @crtc: CRTC object
2074  * @state: atomic CRTC state
2075  *
2076  * Copies atomic state from a CRTC's current state and resets inferred values.
2077  * This is useful for drivers that subclass the CRTC state.
2078  */
2079 void __drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc,
2080                                               struct drm_crtc_state *state)
2081 {
2082         memcpy(state, crtc->state, sizeof(*state));
2083
2084         state->mode_changed = false;
2085         state->active_changed = false;
2086         state->planes_changed = false;
2087         state->event = NULL;
2088 }
2089 EXPORT_SYMBOL(__drm_atomic_helper_crtc_duplicate_state);
2090
2091 /**
2092  * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
2093  * @crtc: drm CRTC
2094  *
2095  * Default CRTC state duplicate hook for drivers which don't have their own
2096  * subclassed CRTC state structure.
2097  */
2098 struct drm_crtc_state *
2099 drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc)
2100 {
2101         struct drm_crtc_state *state;
2102
2103         if (WARN_ON(!crtc->state))
2104                 return NULL;
2105
2106         state = kmalloc(sizeof(*state), GFP_KERNEL);
2107         if (state)
2108                 __drm_atomic_helper_crtc_duplicate_state(crtc, state);
2109
2110         return state;
2111 }
2112 EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state);
2113
2114 /**
2115  * __drm_atomic_helper_crtc_destroy_state - release CRTC state
2116  * @crtc: CRTC object
2117  * @state: CRTC state object to release
2118  *
2119  * Releases all resources stored in the CRTC state without actually freeing
2120  * the memory of the CRTC state. This is useful for drivers that subclass the
2121  * CRTC state.
2122  */
2123 void __drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
2124                                             struct drm_crtc_state *state)
2125 {
2126         /*
2127          * This is currently a placeholder so that drivers that subclass the
2128          * state will automatically do the right thing if code is ever added
2129          * to this function.
2130          */
2131 }
2132 EXPORT_SYMBOL(__drm_atomic_helper_crtc_destroy_state);
2133
2134 /**
2135  * drm_atomic_helper_crtc_destroy_state - default state destroy hook
2136  * @crtc: drm CRTC
2137  * @state: CRTC state object to release
2138  *
2139  * Default CRTC state destroy hook for drivers which don't have their own
2140  * subclassed CRTC state structure.
2141  */
2142 void drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
2143                                           struct drm_crtc_state *state)
2144 {
2145         __drm_atomic_helper_crtc_destroy_state(crtc, state);
2146         kfree(state);
2147 }
2148 EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state);
2149
2150 /**
2151  * drm_atomic_helper_plane_reset - default ->reset hook for planes
2152  * @plane: drm plane
2153  *
2154  * Resets the atomic state for @plane by freeing the state pointer (which might
2155  * be NULL, e.g. at driver load time) and allocating a new empty state object.
2156  */
2157 void drm_atomic_helper_plane_reset(struct drm_plane *plane)
2158 {
2159         if (plane->state && plane->state->fb)
2160                 drm_framebuffer_unreference(plane->state->fb);
2161
2162         kfree(plane->state);
2163         plane->state = kzalloc(sizeof(*plane->state), GFP_KERNEL);
2164
2165         if (plane->state)
2166                 plane->state->plane = plane;
2167 }
2168 EXPORT_SYMBOL(drm_atomic_helper_plane_reset);
2169
2170 /**
2171  * __drm_atomic_helper_plane_duplicate_state - copy atomic plane state
2172  * @plane: plane object
2173  * @state: atomic plane state
2174  *
2175  * Copies atomic state from a plane's current state. This is useful for
2176  * drivers that subclass the plane state.
2177  */
2178 void __drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane,
2179                                                struct drm_plane_state *state)
2180 {
2181         memcpy(state, plane->state, sizeof(*state));
2182
2183         if (state->fb)
2184                 drm_framebuffer_reference(state->fb);
2185 }
2186 EXPORT_SYMBOL(__drm_atomic_helper_plane_duplicate_state);
2187
2188 /**
2189  * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
2190  * @plane: drm plane
2191  *
2192  * Default plane state duplicate hook for drivers which don't have their own
2193  * subclassed plane state structure.
2194  */
2195 struct drm_plane_state *
2196 drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane)
2197 {
2198         struct drm_plane_state *state;
2199
2200         if (WARN_ON(!plane->state))
2201                 return NULL;
2202
2203         state = kmalloc(sizeof(*state), GFP_KERNEL);
2204         if (state)
2205                 __drm_atomic_helper_plane_duplicate_state(plane, state);
2206
2207         return state;
2208 }
2209 EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state);
2210
2211 /**
2212  * __drm_atomic_helper_plane_destroy_state - release plane state
2213  * @plane: plane object
2214  * @state: plane state object to release
2215  *
2216  * Releases all resources stored in the plane state without actually freeing
2217  * the memory of the plane state. This is useful for drivers that subclass the
2218  * plane state.
2219  */
2220 void __drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
2221                                              struct drm_plane_state *state)
2222 {
2223         if (state->fb)
2224                 drm_framebuffer_unreference(state->fb);
2225 }
2226 EXPORT_SYMBOL(__drm_atomic_helper_plane_destroy_state);
2227
2228 /**
2229  * drm_atomic_helper_plane_destroy_state - default state destroy hook
2230  * @plane: drm plane
2231  * @state: plane state object to release
2232  *
2233  * Default plane state destroy hook for drivers which don't have their own
2234  * subclassed plane state structure.
2235  */
2236 void drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
2237                                            struct drm_plane_state *state)
2238 {
2239         __drm_atomic_helper_plane_destroy_state(plane, state);
2240         kfree(state);
2241 }
2242 EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state);
2243
2244 /**
2245  * drm_atomic_helper_connector_reset - default ->reset hook for connectors
2246  * @connector: drm connector
2247  *
2248  * Resets the atomic state for @connector by freeing the state pointer (which
2249  * might be NULL, e.g. at driver load time) and allocating a new empty state
2250  * object.
2251  */
2252 void drm_atomic_helper_connector_reset(struct drm_connector *connector)
2253 {
2254         kfree(connector->state);
2255         connector->state = kzalloc(sizeof(*connector->state), GFP_KERNEL);
2256
2257         if (connector->state)
2258                 connector->state->connector = connector;
2259 }
2260 EXPORT_SYMBOL(drm_atomic_helper_connector_reset);
2261
2262 /**
2263  * __drm_atomic_helper_connector_duplicate_state - copy atomic connector state
2264  * @connector: connector object
2265  * @state: atomic connector state
2266  *
2267  * Copies atomic state from a connector's current state. This is useful for
2268  * drivers that subclass the connector state.
2269  */
2270 void
2271 __drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector,
2272                                             struct drm_connector_state *state)
2273 {
2274         memcpy(state, connector->state, sizeof(*state));
2275 }
2276 EXPORT_SYMBOL(__drm_atomic_helper_connector_duplicate_state);
2277
2278 /**
2279  * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
2280  * @connector: drm connector
2281  *
2282  * Default connector state duplicate hook for drivers which don't have their own
2283  * subclassed connector state structure.
2284  */
2285 struct drm_connector_state *
2286 drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
2287 {
2288         struct drm_connector_state *state;
2289
2290         if (WARN_ON(!connector->state))
2291                 return NULL;
2292
2293         state = kmalloc(sizeof(*state), GFP_KERNEL);
2294         if (state)
2295                 __drm_atomic_helper_connector_duplicate_state(connector, state);
2296
2297         return state;
2298 }
2299 EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
2300
2301 /**
2302  * __drm_atomic_helper_connector_destroy_state - release connector state
2303  * @connector: connector object
2304  * @state: connector state object to release
2305  *
2306  * Releases all resources stored in the connector state without actually
2307  * freeing the memory of the connector state. This is useful for drivers that
2308  * subclass the connector state.
2309  */
2310 void
2311 __drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
2312                                             struct drm_connector_state *state)
2313 {
2314         /*
2315          * This is currently a placeholder so that drivers that subclass the
2316          * state will automatically do the right thing if code is ever added
2317          * to this function.
2318          */
2319 }
2320 EXPORT_SYMBOL(__drm_atomic_helper_connector_destroy_state);
2321
2322 /**
2323  * drm_atomic_helper_connector_destroy_state - default state destroy hook
2324  * @connector: drm connector
2325  * @state: connector state object to release
2326  *
2327  * Default connector state destroy hook for drivers which don't have their own
2328  * subclassed connector state structure.
2329  */
2330 void drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
2331                                           struct drm_connector_state *state)
2332 {
2333         __drm_atomic_helper_connector_destroy_state(connector, state);
2334         kfree(state);
2335 }
2336 EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state);