drm: rcar-du: Don't initialize event->pipe field
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / drm / drm_atomic.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
29 #include <drm/drmP.h>
30 #include <drm/drm_atomic.h>
31 #include <drm/drm_plane_helper.h>
32
33 static void kfree_state(struct drm_atomic_state *state)
34 {
35         kfree(state->connectors);
36         kfree(state->connector_states);
37         kfree(state->crtcs);
38         kfree(state->crtc_states);
39         kfree(state->planes);
40         kfree(state->plane_states);
41         kfree(state);
42 }
43
44 /**
45  * drm_atomic_state_alloc - allocate atomic state
46  * @dev: DRM device
47  *
48  * This allocates an empty atomic state to track updates.
49  */
50 struct drm_atomic_state *
51 drm_atomic_state_alloc(struct drm_device *dev)
52 {
53         struct drm_atomic_state *state;
54
55         state = kzalloc(sizeof(*state), GFP_KERNEL);
56         if (!state)
57                 return NULL;
58
59         /* TODO legacy paths should maybe do a better job about
60          * setting this appropriately?
61          */
62         state->allow_modeset = true;
63
64         state->num_connector = ACCESS_ONCE(dev->mode_config.num_connector);
65
66         state->crtcs = kcalloc(dev->mode_config.num_crtc,
67                                sizeof(*state->crtcs), GFP_KERNEL);
68         if (!state->crtcs)
69                 goto fail;
70         state->crtc_states = kcalloc(dev->mode_config.num_crtc,
71                                      sizeof(*state->crtc_states), GFP_KERNEL);
72         if (!state->crtc_states)
73                 goto fail;
74         state->planes = kcalloc(dev->mode_config.num_total_plane,
75                                 sizeof(*state->planes), GFP_KERNEL);
76         if (!state->planes)
77                 goto fail;
78         state->plane_states = kcalloc(dev->mode_config.num_total_plane,
79                                       sizeof(*state->plane_states), GFP_KERNEL);
80         if (!state->plane_states)
81                 goto fail;
82         state->connectors = kcalloc(state->num_connector,
83                                     sizeof(*state->connectors),
84                                     GFP_KERNEL);
85         if (!state->connectors)
86                 goto fail;
87         state->connector_states = kcalloc(state->num_connector,
88                                           sizeof(*state->connector_states),
89                                           GFP_KERNEL);
90         if (!state->connector_states)
91                 goto fail;
92
93         state->dev = dev;
94
95         DRM_DEBUG_ATOMIC("Allocate atomic state %p\n", state);
96
97         return state;
98 fail:
99         kfree_state(state);
100
101         return NULL;
102 }
103 EXPORT_SYMBOL(drm_atomic_state_alloc);
104
105 /**
106  * drm_atomic_state_clear - clear state object
107  * @state: atomic state
108  *
109  * When the w/w mutex algorithm detects a deadlock we need to back off and drop
110  * all locks. So someone else could sneak in and change the current modeset
111  * configuration. Which means that all the state assembled in @state is no
112  * longer an atomic update to the current state, but to some arbitrary earlier
113  * state. Which could break assumptions the driver's ->atomic_check likely
114  * relies on.
115  *
116  * Hence we must clear all cached state and completely start over, using this
117  * function.
118  */
119 void drm_atomic_state_clear(struct drm_atomic_state *state)
120 {
121         struct drm_device *dev = state->dev;
122         struct drm_mode_config *config = &dev->mode_config;
123         int i;
124
125         DRM_DEBUG_ATOMIC("Clearing atomic state %p\n", state);
126
127         for (i = 0; i < state->num_connector; i++) {
128                 struct drm_connector *connector = state->connectors[i];
129
130                 if (!connector)
131                         continue;
132
133                 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
134
135                 connector->funcs->atomic_destroy_state(connector,
136                                                        state->connector_states[i]);
137                 state->connector_states[i] = NULL;
138         }
139
140         for (i = 0; i < config->num_crtc; i++) {
141                 struct drm_crtc *crtc = state->crtcs[i];
142
143                 if (!crtc)
144                         continue;
145
146                 crtc->funcs->atomic_destroy_state(crtc,
147                                                   state->crtc_states[i]);
148                 state->crtc_states[i] = NULL;
149         }
150
151         for (i = 0; i < config->num_total_plane; i++) {
152                 struct drm_plane *plane = state->planes[i];
153
154                 if (!plane)
155                         continue;
156
157                 plane->funcs->atomic_destroy_state(plane,
158                                                    state->plane_states[i]);
159                 state->plane_states[i] = NULL;
160         }
161 }
162 EXPORT_SYMBOL(drm_atomic_state_clear);
163
164 /**
165  * drm_atomic_state_free - free all memory for an atomic state
166  * @state: atomic state to deallocate
167  *
168  * This frees all memory associated with an atomic state, including all the
169  * per-object state for planes, crtcs and connectors.
170  */
171 void drm_atomic_state_free(struct drm_atomic_state *state)
172 {
173         drm_atomic_state_clear(state);
174
175         DRM_DEBUG_ATOMIC("Freeing atomic state %p\n", state);
176
177         kfree_state(state);
178 }
179 EXPORT_SYMBOL(drm_atomic_state_free);
180
181 /**
182  * drm_atomic_get_crtc_state - get crtc state
183  * @state: global atomic state object
184  * @crtc: crtc to get state object for
185  *
186  * This function returns the crtc state for the given crtc, allocating it if
187  * needed. It will also grab the relevant crtc lock to make sure that the state
188  * is consistent.
189  *
190  * Returns:
191  *
192  * Either the allocated state or the error code encoded into the pointer. When
193  * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
194  * entire atomic sequence must be restarted. All other errors are fatal.
195  */
196 struct drm_crtc_state *
197 drm_atomic_get_crtc_state(struct drm_atomic_state *state,
198                           struct drm_crtc *crtc)
199 {
200         int ret, index;
201         struct drm_crtc_state *crtc_state;
202
203         index = drm_crtc_index(crtc);
204
205         if (state->crtc_states[index])
206                 return state->crtc_states[index];
207
208         ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx);
209         if (ret)
210                 return ERR_PTR(ret);
211
212         crtc_state = crtc->funcs->atomic_duplicate_state(crtc);
213         if (!crtc_state)
214                 return ERR_PTR(-ENOMEM);
215
216         state->crtc_states[index] = crtc_state;
217         state->crtcs[index] = crtc;
218         crtc_state->state = state;
219
220         DRM_DEBUG_ATOMIC("Added [CRTC:%d] %p state to %p\n",
221                          crtc->base.id, crtc_state, state);
222
223         return crtc_state;
224 }
225 EXPORT_SYMBOL(drm_atomic_get_crtc_state);
226
227 /**
228  * drm_atomic_crtc_set_property - set property on CRTC
229  * @crtc: the drm CRTC to set a property on
230  * @state: the state object to update with the new property value
231  * @property: the property to set
232  * @val: the new property value
233  *
234  * Use this instead of calling crtc->atomic_set_property directly.
235  * This function handles generic/core properties and calls out to
236  * driver's ->atomic_set_property() for driver properties.  To ensure
237  * consistent behavior you must call this function rather than the
238  * driver hook directly.
239  *
240  * RETURNS:
241  * Zero on success, error code on failure
242  */
243 int drm_atomic_crtc_set_property(struct drm_crtc *crtc,
244                 struct drm_crtc_state *state, struct drm_property *property,
245                 uint64_t val)
246 {
247         struct drm_device *dev = crtc->dev;
248         struct drm_mode_config *config = &dev->mode_config;
249
250         /* FIXME: Mode prop is missing, which also controls ->enable. */
251         if (property == config->prop_active) {
252                 state->active = val;
253         } else if (crtc->funcs->atomic_set_property)
254                 return crtc->funcs->atomic_set_property(crtc, state, property, val);
255         return -EINVAL;
256 }
257 EXPORT_SYMBOL(drm_atomic_crtc_set_property);
258
259 /*
260  * This function handles generic/core properties and calls out to
261  * driver's ->atomic_get_property() for driver properties.  To ensure
262  * consistent behavior you must call this function rather than the
263  * driver hook directly.
264  */
265 int drm_atomic_crtc_get_property(struct drm_crtc *crtc,
266                 const struct drm_crtc_state *state,
267                 struct drm_property *property, uint64_t *val)
268 {
269         if (crtc->funcs->atomic_get_property)
270                 return crtc->funcs->atomic_get_property(crtc, state, property, val);
271         return -EINVAL;
272 }
273
274 /**
275  * drm_atomic_crtc_check - check crtc state
276  * @crtc: crtc to check
277  * @state: crtc state to check
278  *
279  * Provides core sanity checks for crtc state.
280  *
281  * RETURNS:
282  * Zero on success, error code on failure
283  */
284 static int drm_atomic_crtc_check(struct drm_crtc *crtc,
285                 struct drm_crtc_state *state)
286 {
287         /* NOTE: we explicitly don't enforce constraints such as primary
288          * layer covering entire screen, since that is something we want
289          * to allow (on hw that supports it).  For hw that does not, it
290          * should be checked in driver's crtc->atomic_check() vfunc.
291          *
292          * TODO: Add generic modeset state checks once we support those.
293          */
294
295         if (state->active && !state->enable) {
296                 DRM_DEBUG_ATOMIC("[CRTC:%d] active without enabled\n",
297                                  crtc->base.id);
298                 return -EINVAL;
299         }
300
301         return 0;
302 }
303
304 /**
305  * drm_atomic_get_plane_state - get plane state
306  * @state: global atomic state object
307  * @plane: plane to get state object for
308  *
309  * This function returns the plane state for the given plane, allocating it if
310  * needed. It will also grab the relevant plane lock to make sure that the state
311  * is consistent.
312  *
313  * Returns:
314  *
315  * Either the allocated state or the error code encoded into the pointer. When
316  * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
317  * entire atomic sequence must be restarted. All other errors are fatal.
318  */
319 struct drm_plane_state *
320 drm_atomic_get_plane_state(struct drm_atomic_state *state,
321                           struct drm_plane *plane)
322 {
323         int ret, index;
324         struct drm_plane_state *plane_state;
325
326         index = drm_plane_index(plane);
327
328         if (state->plane_states[index])
329                 return state->plane_states[index];
330
331         ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
332         if (ret)
333                 return ERR_PTR(ret);
334
335         plane_state = plane->funcs->atomic_duplicate_state(plane);
336         if (!plane_state)
337                 return ERR_PTR(-ENOMEM);
338
339         state->plane_states[index] = plane_state;
340         state->planes[index] = plane;
341         plane_state->state = state;
342
343         DRM_DEBUG_ATOMIC("Added [PLANE:%d] %p state to %p\n",
344                          plane->base.id, plane_state, state);
345
346         if (plane_state->crtc) {
347                 struct drm_crtc_state *crtc_state;
348
349                 crtc_state = drm_atomic_get_crtc_state(state,
350                                                        plane_state->crtc);
351                 if (IS_ERR(crtc_state))
352                         return ERR_CAST(crtc_state);
353         }
354
355         return plane_state;
356 }
357 EXPORT_SYMBOL(drm_atomic_get_plane_state);
358
359 /**
360  * drm_atomic_plane_set_property - set property on plane
361  * @plane: the drm plane to set a property on
362  * @state: the state object to update with the new property value
363  * @property: the property to set
364  * @val: the new property value
365  *
366  * Use this instead of calling plane->atomic_set_property directly.
367  * This function handles generic/core properties and calls out to
368  * driver's ->atomic_set_property() for driver properties.  To ensure
369  * consistent behavior you must call this function rather than the
370  * driver hook directly.
371  *
372  * RETURNS:
373  * Zero on success, error code on failure
374  */
375 int drm_atomic_plane_set_property(struct drm_plane *plane,
376                 struct drm_plane_state *state, struct drm_property *property,
377                 uint64_t val)
378 {
379         struct drm_device *dev = plane->dev;
380         struct drm_mode_config *config = &dev->mode_config;
381
382         if (property == config->prop_fb_id) {
383                 struct drm_framebuffer *fb = drm_framebuffer_lookup(dev, val);
384                 drm_atomic_set_fb_for_plane(state, fb);
385                 if (fb)
386                         drm_framebuffer_unreference(fb);
387         } else if (property == config->prop_crtc_id) {
388                 struct drm_crtc *crtc = drm_crtc_find(dev, val);
389                 return drm_atomic_set_crtc_for_plane(state, crtc);
390         } else if (property == config->prop_crtc_x) {
391                 state->crtc_x = U642I64(val);
392         } else if (property == config->prop_crtc_y) {
393                 state->crtc_y = U642I64(val);
394         } else if (property == config->prop_crtc_w) {
395                 state->crtc_w = val;
396         } else if (property == config->prop_crtc_h) {
397                 state->crtc_h = val;
398         } else if (property == config->prop_src_x) {
399                 state->src_x = val;
400         } else if (property == config->prop_src_y) {
401                 state->src_y = val;
402         } else if (property == config->prop_src_w) {
403                 state->src_w = val;
404         } else if (property == config->prop_src_h) {
405                 state->src_h = val;
406         } else if (property == config->rotation_property) {
407                 state->rotation = val;
408         } else if (plane->funcs->atomic_set_property) {
409                 return plane->funcs->atomic_set_property(plane, state,
410                                 property, val);
411         } else {
412                 return -EINVAL;
413         }
414
415         return 0;
416 }
417 EXPORT_SYMBOL(drm_atomic_plane_set_property);
418
419 /*
420  * This function handles generic/core properties and calls out to
421  * driver's ->atomic_get_property() for driver properties.  To ensure
422  * consistent behavior you must call this function rather than the
423  * driver hook directly.
424  */
425 static int
426 drm_atomic_plane_get_property(struct drm_plane *plane,
427                 const struct drm_plane_state *state,
428                 struct drm_property *property, uint64_t *val)
429 {
430         struct drm_device *dev = plane->dev;
431         struct drm_mode_config *config = &dev->mode_config;
432
433         if (property == config->prop_fb_id) {
434                 *val = (state->fb) ? state->fb->base.id : 0;
435         } else if (property == config->prop_crtc_id) {
436                 *val = (state->crtc) ? state->crtc->base.id : 0;
437         } else if (property == config->prop_crtc_x) {
438                 *val = I642U64(state->crtc_x);
439         } else if (property == config->prop_crtc_y) {
440                 *val = I642U64(state->crtc_y);
441         } else if (property == config->prop_crtc_w) {
442                 *val = state->crtc_w;
443         } else if (property == config->prop_crtc_h) {
444                 *val = state->crtc_h;
445         } else if (property == config->prop_src_x) {
446                 *val = state->src_x;
447         } else if (property == config->prop_src_y) {
448                 *val = state->src_y;
449         } else if (property == config->prop_src_w) {
450                 *val = state->src_w;
451         } else if (property == config->prop_src_h) {
452                 *val = state->src_h;
453         } else if (property == config->rotation_property) {
454                 *val = state->rotation;
455         } else if (plane->funcs->atomic_get_property) {
456                 return plane->funcs->atomic_get_property(plane, state, property, val);
457         } else {
458                 return -EINVAL;
459         }
460
461         return 0;
462 }
463
464 /**
465  * drm_atomic_plane_check - check plane state
466  * @plane: plane to check
467  * @state: plane state to check
468  *
469  * Provides core sanity checks for plane state.
470  *
471  * RETURNS:
472  * Zero on success, error code on failure
473  */
474 static int drm_atomic_plane_check(struct drm_plane *plane,
475                 struct drm_plane_state *state)
476 {
477         unsigned int fb_width, fb_height;
478         int ret;
479
480         /* either *both* CRTC and FB must be set, or neither */
481         if (WARN_ON(state->crtc && !state->fb)) {
482                 DRM_DEBUG_ATOMIC("CRTC set but no FB\n");
483                 return -EINVAL;
484         } else if (WARN_ON(state->fb && !state->crtc)) {
485                 DRM_DEBUG_ATOMIC("FB set but no CRTC\n");
486                 return -EINVAL;
487         }
488
489         /* if disabled, we don't care about the rest of the state: */
490         if (!state->crtc)
491                 return 0;
492
493         /* Check whether this plane is usable on this CRTC */
494         if (!(plane->possible_crtcs & drm_crtc_mask(state->crtc))) {
495                 DRM_DEBUG_ATOMIC("Invalid crtc for plane\n");
496                 return -EINVAL;
497         }
498
499         /* Check whether this plane supports the fb pixel format. */
500         ret = drm_plane_check_pixel_format(plane, state->fb->pixel_format);
501         if (ret) {
502                 DRM_DEBUG_ATOMIC("Invalid pixel format %s\n",
503                                  drm_get_format_name(state->fb->pixel_format));
504                 return ret;
505         }
506
507         /* Give drivers some help against integer overflows */
508         if (state->crtc_w > INT_MAX ||
509             state->crtc_x > INT_MAX - (int32_t) state->crtc_w ||
510             state->crtc_h > INT_MAX ||
511             state->crtc_y > INT_MAX - (int32_t) state->crtc_h) {
512                 DRM_DEBUG_ATOMIC("Invalid CRTC coordinates %ux%u+%d+%d\n",
513                                  state->crtc_w, state->crtc_h,
514                                  state->crtc_x, state->crtc_y);
515                 return -ERANGE;
516         }
517
518         fb_width = state->fb->width << 16;
519         fb_height = state->fb->height << 16;
520
521         /* Make sure source coordinates are inside the fb. */
522         if (state->src_w > fb_width ||
523             state->src_x > fb_width - state->src_w ||
524             state->src_h > fb_height ||
525             state->src_y > fb_height - state->src_h) {
526                 DRM_DEBUG_ATOMIC("Invalid source coordinates "
527                                  "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
528                                  state->src_w >> 16, ((state->src_w & 0xffff) * 15625) >> 10,
529                                  state->src_h >> 16, ((state->src_h & 0xffff) * 15625) >> 10,
530                                  state->src_x >> 16, ((state->src_x & 0xffff) * 15625) >> 10,
531                                  state->src_y >> 16, ((state->src_y & 0xffff) * 15625) >> 10);
532                 return -ENOSPC;
533         }
534
535         return 0;
536 }
537
538 /**
539  * drm_atomic_get_connector_state - get connector state
540  * @state: global atomic state object
541  * @connector: connector to get state object for
542  *
543  * This function returns the connector state for the given connector,
544  * allocating it if needed. It will also grab the relevant connector lock to
545  * make sure that the state is consistent.
546  *
547  * Returns:
548  *
549  * Either the allocated state or the error code encoded into the pointer. When
550  * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
551  * entire atomic sequence must be restarted. All other errors are fatal.
552  */
553 struct drm_connector_state *
554 drm_atomic_get_connector_state(struct drm_atomic_state *state,
555                           struct drm_connector *connector)
556 {
557         int ret, index;
558         struct drm_mode_config *config = &connector->dev->mode_config;
559         struct drm_connector_state *connector_state;
560
561         ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
562         if (ret)
563                 return ERR_PTR(ret);
564
565         index = drm_connector_index(connector);
566
567         /*
568          * Construction of atomic state updates can race with a connector
569          * hot-add which might overflow. In this case flip the table and just
570          * restart the entire ioctl - no one is fast enough to livelock a cpu
571          * with physical hotplug events anyway.
572          *
573          * Note that we only grab the indexes once we have the right lock to
574          * prevent hotplug/unplugging of connectors. So removal is no problem,
575          * at most the array is a bit too large.
576          */
577         if (index >= state->num_connector) {
578                 DRM_DEBUG_ATOMIC("Hot-added connector would overflow state array, restarting\n");
579                 return ERR_PTR(-EAGAIN);
580         }
581
582         if (state->connector_states[index])
583                 return state->connector_states[index];
584
585         connector_state = connector->funcs->atomic_duplicate_state(connector);
586         if (!connector_state)
587                 return ERR_PTR(-ENOMEM);
588
589         state->connector_states[index] = connector_state;
590         state->connectors[index] = connector;
591         connector_state->state = state;
592
593         DRM_DEBUG_ATOMIC("Added [CONNECTOR:%d] %p state to %p\n",
594                          connector->base.id, connector_state, state);
595
596         if (connector_state->crtc) {
597                 struct drm_crtc_state *crtc_state;
598
599                 crtc_state = drm_atomic_get_crtc_state(state,
600                                                        connector_state->crtc);
601                 if (IS_ERR(crtc_state))
602                         return ERR_CAST(crtc_state);
603         }
604
605         return connector_state;
606 }
607 EXPORT_SYMBOL(drm_atomic_get_connector_state);
608
609 /**
610  * drm_atomic_connector_set_property - set property on connector.
611  * @connector: the drm connector to set a property on
612  * @state: the state object to update with the new property value
613  * @property: the property to set
614  * @val: the new property value
615  *
616  * Use this instead of calling connector->atomic_set_property directly.
617  * This function handles generic/core properties and calls out to
618  * driver's ->atomic_set_property() for driver properties.  To ensure
619  * consistent behavior you must call this function rather than the
620  * driver hook directly.
621  *
622  * RETURNS:
623  * Zero on success, error code on failure
624  */
625 int drm_atomic_connector_set_property(struct drm_connector *connector,
626                 struct drm_connector_state *state, struct drm_property *property,
627                 uint64_t val)
628 {
629         struct drm_device *dev = connector->dev;
630         struct drm_mode_config *config = &dev->mode_config;
631
632         if (property == config->prop_crtc_id) {
633                 struct drm_crtc *crtc = drm_crtc_find(dev, val);
634                 return drm_atomic_set_crtc_for_connector(state, crtc);
635         } else if (property == config->dpms_property) {
636                 /* setting DPMS property requires special handling, which
637                  * is done in legacy setprop path for us.  Disallow (for
638                  * now?) atomic writes to DPMS property:
639                  */
640                 return -EINVAL;
641         } else if (connector->funcs->atomic_set_property) {
642                 return connector->funcs->atomic_set_property(connector,
643                                 state, property, val);
644         } else {
645                 return -EINVAL;
646         }
647 }
648 EXPORT_SYMBOL(drm_atomic_connector_set_property);
649
650 /*
651  * This function handles generic/core properties and calls out to
652  * driver's ->atomic_get_property() for driver properties.  To ensure
653  * consistent behavior you must call this function rather than the
654  * driver hook directly.
655  */
656 static int
657 drm_atomic_connector_get_property(struct drm_connector *connector,
658                 const struct drm_connector_state *state,
659                 struct drm_property *property, uint64_t *val)
660 {
661         struct drm_device *dev = connector->dev;
662         struct drm_mode_config *config = &dev->mode_config;
663
664         if (property == config->prop_crtc_id) {
665                 *val = (state->crtc) ? state->crtc->base.id : 0;
666         } else if (property == config->dpms_property) {
667                 *val = connector->dpms;
668         } else if (connector->funcs->atomic_get_property) {
669                 return connector->funcs->atomic_get_property(connector,
670                                 state, property, val);
671         } else {
672                 return -EINVAL;
673         }
674
675         return 0;
676 }
677
678 int drm_atomic_get_property(struct drm_mode_object *obj,
679                 struct drm_property *property, uint64_t *val)
680 {
681         struct drm_device *dev = property->dev;
682         int ret;
683
684         switch (obj->type) {
685         case DRM_MODE_OBJECT_CONNECTOR: {
686                 struct drm_connector *connector = obj_to_connector(obj);
687                 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
688                 ret = drm_atomic_connector_get_property(connector,
689                                 connector->state, property, val);
690                 break;
691         }
692         case DRM_MODE_OBJECT_CRTC: {
693                 struct drm_crtc *crtc = obj_to_crtc(obj);
694                 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
695                 ret = drm_atomic_crtc_get_property(crtc,
696                                 crtc->state, property, val);
697                 break;
698         }
699         case DRM_MODE_OBJECT_PLANE: {
700                 struct drm_plane *plane = obj_to_plane(obj);
701                 WARN_ON(!drm_modeset_is_locked(&plane->mutex));
702                 ret = drm_atomic_plane_get_property(plane,
703                                 plane->state, property, val);
704                 break;
705         }
706         default:
707                 ret = -EINVAL;
708                 break;
709         }
710
711         return ret;
712 }
713
714 /**
715  * drm_atomic_set_crtc_for_plane - set crtc for plane
716  * @plane_state: the plane whose incoming state to update
717  * @crtc: crtc to use for the plane
718  *
719  * Changing the assigned crtc for a plane requires us to grab the lock and state
720  * for the new crtc, as needed. This function takes care of all these details
721  * besides updating the pointer in the state object itself.
722  *
723  * Returns:
724  * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
725  * then the w/w mutex code has detected a deadlock and the entire atomic
726  * sequence must be restarted. All other errors are fatal.
727  */
728 int
729 drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
730                               struct drm_crtc *crtc)
731 {
732         struct drm_plane *plane = plane_state->plane;
733         struct drm_crtc_state *crtc_state;
734
735         if (plane_state->crtc) {
736                 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
737                                                        plane_state->crtc);
738                 if (WARN_ON(IS_ERR(crtc_state)))
739                         return PTR_ERR(crtc_state);
740
741                 crtc_state->plane_mask &= ~(1 << drm_plane_index(plane));
742         }
743
744         plane_state->crtc = crtc;
745
746         if (crtc) {
747                 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
748                                                        crtc);
749                 if (IS_ERR(crtc_state))
750                         return PTR_ERR(crtc_state);
751                 crtc_state->plane_mask |= (1 << drm_plane_index(plane));
752         }
753
754         if (crtc)
755                 DRM_DEBUG_ATOMIC("Link plane state %p to [CRTC:%d]\n",
756                                  plane_state, crtc->base.id);
757         else
758                 DRM_DEBUG_ATOMIC("Link plane state %p to [NOCRTC]\n",
759                                  plane_state);
760
761         return 0;
762 }
763 EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane);
764
765 /**
766  * drm_atomic_set_fb_for_plane - set crtc for plane
767  * @plane_state: atomic state object for the plane
768  * @fb: fb to use for the plane
769  *
770  * Changing the assigned framebuffer for a plane requires us to grab a reference
771  * to the new fb and drop the reference to the old fb, if there is one. This
772  * function takes care of all these details besides updating the pointer in the
773  * state object itself.
774  */
775 void
776 drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
777                             struct drm_framebuffer *fb)
778 {
779         if (plane_state->fb)
780                 drm_framebuffer_unreference(plane_state->fb);
781         if (fb)
782                 drm_framebuffer_reference(fb);
783         plane_state->fb = fb;
784
785         if (fb)
786                 DRM_DEBUG_ATOMIC("Set [FB:%d] for plane state %p\n",
787                                  fb->base.id, plane_state);
788         else
789                 DRM_DEBUG_ATOMIC("Set [NOFB] for plane state %p\n",
790                                  plane_state);
791 }
792 EXPORT_SYMBOL(drm_atomic_set_fb_for_plane);
793
794 /**
795  * drm_atomic_set_crtc_for_connector - set crtc for connector
796  * @conn_state: atomic state object for the connector
797  * @crtc: crtc to use for the connector
798  *
799  * Changing the assigned crtc for a connector requires us to grab the lock and
800  * state for the new crtc, as needed. This function takes care of all these
801  * details besides updating the pointer in the state object itself.
802  *
803  * Returns:
804  * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
805  * then the w/w mutex code has detected a deadlock and the entire atomic
806  * sequence must be restarted. All other errors are fatal.
807  */
808 int
809 drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
810                                   struct drm_crtc *crtc)
811 {
812         struct drm_crtc_state *crtc_state;
813
814         if (crtc) {
815                 crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc);
816                 if (IS_ERR(crtc_state))
817                         return PTR_ERR(crtc_state);
818         }
819
820         conn_state->crtc = crtc;
821
822         if (crtc)
823                 DRM_DEBUG_ATOMIC("Link connector state %p to [CRTC:%d]\n",
824                                  conn_state, crtc->base.id);
825         else
826                 DRM_DEBUG_ATOMIC("Link connector state %p to [NOCRTC]\n",
827                                  conn_state);
828
829         return 0;
830 }
831 EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector);
832
833 /**
834  * drm_atomic_add_affected_connectors - add connectors for crtc
835  * @state: atomic state
836  * @crtc: DRM crtc
837  *
838  * This function walks the current configuration and adds all connectors
839  * currently using @crtc to the atomic configuration @state. Note that this
840  * function must acquire the connection mutex. This can potentially cause
841  * unneeded seralization if the update is just for the planes on one crtc. Hence
842  * drivers and helpers should only call this when really needed (e.g. when a
843  * full modeset needs to happen due to some change).
844  *
845  * Returns:
846  * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
847  * then the w/w mutex code has detected a deadlock and the entire atomic
848  * sequence must be restarted. All other errors are fatal.
849  */
850 int
851 drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
852                                    struct drm_crtc *crtc)
853 {
854         struct drm_mode_config *config = &state->dev->mode_config;
855         struct drm_connector *connector;
856         struct drm_connector_state *conn_state;
857         int ret;
858
859         ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
860         if (ret)
861                 return ret;
862
863         DRM_DEBUG_ATOMIC("Adding all current connectors for [CRTC:%d] to %p\n",
864                          crtc->base.id, state);
865
866         /*
867          * Changed connectors are already in @state, so only need to look at the
868          * current configuration.
869          */
870         list_for_each_entry(connector, &config->connector_list, head) {
871                 if (connector->state->crtc != crtc)
872                         continue;
873
874                 conn_state = drm_atomic_get_connector_state(state, connector);
875                 if (IS_ERR(conn_state))
876                         return PTR_ERR(conn_state);
877         }
878
879         return 0;
880 }
881 EXPORT_SYMBOL(drm_atomic_add_affected_connectors);
882
883 /**
884  * drm_atomic_connectors_for_crtc - count number of connected outputs
885  * @state: atomic state
886  * @crtc: DRM crtc
887  *
888  * This function counts all connectors which will be connected to @crtc
889  * according to @state. Useful to recompute the enable state for @crtc.
890  */
891 int
892 drm_atomic_connectors_for_crtc(struct drm_atomic_state *state,
893                                struct drm_crtc *crtc)
894 {
895         int i, num_connected_connectors = 0;
896
897         for (i = 0; i < state->num_connector; i++) {
898                 struct drm_connector_state *conn_state;
899
900                 conn_state = state->connector_states[i];
901
902                 if (conn_state && conn_state->crtc == crtc)
903                         num_connected_connectors++;
904         }
905
906         DRM_DEBUG_ATOMIC("State %p has %i connectors for [CRTC:%d]\n",
907                          state, num_connected_connectors, crtc->base.id);
908
909         return num_connected_connectors;
910 }
911 EXPORT_SYMBOL(drm_atomic_connectors_for_crtc);
912
913 /**
914  * drm_atomic_legacy_backoff - locking backoff for legacy ioctls
915  * @state: atomic state
916  *
917  * This function should be used by legacy entry points which don't understand
918  * -EDEADLK semantics. For simplicity this one will grab all modeset locks after
919  *  the slowpath completed.
920  */
921 void drm_atomic_legacy_backoff(struct drm_atomic_state *state)
922 {
923         int ret;
924
925 retry:
926         drm_modeset_backoff(state->acquire_ctx);
927
928         ret = drm_modeset_lock(&state->dev->mode_config.connection_mutex,
929                                state->acquire_ctx);
930         if (ret)
931                 goto retry;
932         ret = drm_modeset_lock_all_crtcs(state->dev,
933                                          state->acquire_ctx);
934         if (ret)
935                 goto retry;
936 }
937 EXPORT_SYMBOL(drm_atomic_legacy_backoff);
938
939 /**
940  * drm_atomic_check_only - check whether a given config would work
941  * @state: atomic configuration to check
942  *
943  * Note that this function can return -EDEADLK if the driver needed to acquire
944  * more locks but encountered a deadlock. The caller must then do the usual w/w
945  * backoff dance and restart. All other errors are fatal.
946  *
947  * Returns:
948  * 0 on success, negative error code on failure.
949  */
950 int drm_atomic_check_only(struct drm_atomic_state *state)
951 {
952         struct drm_device *dev = state->dev;
953         struct drm_mode_config *config = &dev->mode_config;
954         int nplanes = config->num_total_plane;
955         int ncrtcs = config->num_crtc;
956         int i, ret = 0;
957
958         DRM_DEBUG_ATOMIC("checking %p\n", state);
959
960         for (i = 0; i < nplanes; i++) {
961                 struct drm_plane *plane = state->planes[i];
962
963                 if (!plane)
964                         continue;
965
966                 ret = drm_atomic_plane_check(plane, state->plane_states[i]);
967                 if (ret) {
968                         DRM_DEBUG_ATOMIC("[PLANE:%d] atomic core check failed\n",
969                                          plane->base.id);
970                         return ret;
971                 }
972         }
973
974         for (i = 0; i < ncrtcs; i++) {
975                 struct drm_crtc *crtc = state->crtcs[i];
976
977                 if (!crtc)
978                         continue;
979
980                 ret = drm_atomic_crtc_check(crtc, state->crtc_states[i]);
981                 if (ret) {
982                         DRM_DEBUG_ATOMIC("[CRTC:%d] atomic core check failed\n",
983                                          crtc->base.id);
984                         return ret;
985                 }
986         }
987
988         if (config->funcs->atomic_check)
989                 ret = config->funcs->atomic_check(state->dev, state);
990
991         if (!state->allow_modeset) {
992                 for (i = 0; i < ncrtcs; i++) {
993                         struct drm_crtc *crtc = state->crtcs[i];
994                         struct drm_crtc_state *crtc_state = state->crtc_states[i];
995
996                         if (!crtc)
997                                 continue;
998
999                         if (crtc_state->mode_changed ||
1000                             crtc_state->active_changed) {
1001                                 DRM_DEBUG_ATOMIC("[CRTC:%d] requires full modeset\n",
1002                                                  crtc->base.id);
1003                                 return -EINVAL;
1004                         }
1005                 }
1006         }
1007
1008         return ret;
1009 }
1010 EXPORT_SYMBOL(drm_atomic_check_only);
1011
1012 /**
1013  * drm_atomic_commit - commit configuration atomically
1014  * @state: atomic configuration to check
1015  *
1016  * Note that this function can return -EDEADLK if the driver needed to acquire
1017  * more locks but encountered a deadlock. The caller must then do the usual w/w
1018  * backoff dance and restart. All other errors are fatal.
1019  *
1020  * Also note that on successful execution ownership of @state is transferred
1021  * from the caller of this function to the function itself. The caller must not
1022  * free or in any other way access @state. If the function fails then the caller
1023  * must clean up @state itself.
1024  *
1025  * Returns:
1026  * 0 on success, negative error code on failure.
1027  */
1028 int drm_atomic_commit(struct drm_atomic_state *state)
1029 {
1030         struct drm_mode_config *config = &state->dev->mode_config;
1031         int ret;
1032
1033         ret = drm_atomic_check_only(state);
1034         if (ret)
1035                 return ret;
1036
1037         DRM_DEBUG_ATOMIC("commiting %p\n", state);
1038
1039         return config->funcs->atomic_commit(state->dev, state, false);
1040 }
1041 EXPORT_SYMBOL(drm_atomic_commit);
1042
1043 /**
1044  * drm_atomic_async_commit - atomic&async configuration commit
1045  * @state: atomic configuration to check
1046  *
1047  * Note that this function can return -EDEADLK if the driver needed to acquire
1048  * more locks but encountered a deadlock. The caller must then do the usual w/w
1049  * backoff dance and restart. All other errors are fatal.
1050  *
1051  * Also note that on successful execution ownership of @state is transferred
1052  * from the caller of this function to the function itself. The caller must not
1053  * free or in any other way access @state. If the function fails then the caller
1054  * must clean up @state itself.
1055  *
1056  * Returns:
1057  * 0 on success, negative error code on failure.
1058  */
1059 int drm_atomic_async_commit(struct drm_atomic_state *state)
1060 {
1061         struct drm_mode_config *config = &state->dev->mode_config;
1062         int ret;
1063
1064         ret = drm_atomic_check_only(state);
1065         if (ret)
1066                 return ret;
1067
1068         DRM_DEBUG_ATOMIC("commiting %p asynchronously\n", state);
1069
1070         return config->funcs->atomic_commit(state->dev, state, true);
1071 }
1072 EXPORT_SYMBOL(drm_atomic_async_commit);
1073
1074 /*
1075  * The big monstor ioctl
1076  */
1077
1078 static struct drm_pending_vblank_event *create_vblank_event(
1079                 struct drm_device *dev, struct drm_file *file_priv, uint64_t user_data)
1080 {
1081         struct drm_pending_vblank_event *e = NULL;
1082         unsigned long flags;
1083
1084         spin_lock_irqsave(&dev->event_lock, flags);
1085         if (file_priv->event_space < sizeof e->event) {
1086                 spin_unlock_irqrestore(&dev->event_lock, flags);
1087                 goto out;
1088         }
1089         file_priv->event_space -= sizeof e->event;
1090         spin_unlock_irqrestore(&dev->event_lock, flags);
1091
1092         e = kzalloc(sizeof *e, GFP_KERNEL);
1093         if (e == NULL) {
1094                 spin_lock_irqsave(&dev->event_lock, flags);
1095                 file_priv->event_space += sizeof e->event;
1096                 spin_unlock_irqrestore(&dev->event_lock, flags);
1097                 goto out;
1098         }
1099
1100         e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
1101         e->event.base.length = sizeof e->event;
1102         e->event.user_data = user_data;
1103         e->base.event = &e->event.base;
1104         e->base.file_priv = file_priv;
1105         e->base.destroy = (void (*) (struct drm_pending_event *)) kfree;
1106
1107 out:
1108         return e;
1109 }
1110
1111 static void destroy_vblank_event(struct drm_device *dev,
1112                 struct drm_file *file_priv, struct drm_pending_vblank_event *e)
1113 {
1114         unsigned long flags;
1115
1116         spin_lock_irqsave(&dev->event_lock, flags);
1117         file_priv->event_space += sizeof e->event;
1118         spin_unlock_irqrestore(&dev->event_lock, flags);
1119         kfree(e);
1120 }
1121
1122 static int atomic_set_prop(struct drm_atomic_state *state,
1123                 struct drm_mode_object *obj, struct drm_property *prop,
1124                 uint64_t prop_value)
1125 {
1126         struct drm_mode_object *ref;
1127         int ret;
1128
1129         if (!drm_property_change_valid_get(prop, prop_value, &ref))
1130                 return -EINVAL;
1131
1132         switch (obj->type) {
1133         case DRM_MODE_OBJECT_CONNECTOR: {
1134                 struct drm_connector *connector = obj_to_connector(obj);
1135                 struct drm_connector_state *connector_state;
1136
1137                 connector_state = drm_atomic_get_connector_state(state, connector);
1138                 if (IS_ERR(connector_state)) {
1139                         ret = PTR_ERR(connector_state);
1140                         break;
1141                 }
1142
1143                 ret = drm_atomic_connector_set_property(connector,
1144                                 connector_state, prop, prop_value);
1145                 break;
1146         }
1147         case DRM_MODE_OBJECT_CRTC: {
1148                 struct drm_crtc *crtc = obj_to_crtc(obj);
1149                 struct drm_crtc_state *crtc_state;
1150
1151                 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1152                 if (IS_ERR(crtc_state)) {
1153                         ret = PTR_ERR(crtc_state);
1154                         break;
1155                 }
1156
1157                 ret = drm_atomic_crtc_set_property(crtc,
1158                                 crtc_state, prop, prop_value);
1159                 break;
1160         }
1161         case DRM_MODE_OBJECT_PLANE: {
1162                 struct drm_plane *plane = obj_to_plane(obj);
1163                 struct drm_plane_state *plane_state;
1164
1165                 plane_state = drm_atomic_get_plane_state(state, plane);
1166                 if (IS_ERR(plane_state)) {
1167                         ret = PTR_ERR(plane_state);
1168                         break;
1169                 }
1170
1171                 ret = drm_atomic_plane_set_property(plane,
1172                                 plane_state, prop, prop_value);
1173                 break;
1174         }
1175         default:
1176                 ret = -EINVAL;
1177                 break;
1178         }
1179
1180         drm_property_change_valid_put(prop, ref);
1181         return ret;
1182 }
1183
1184 int drm_mode_atomic_ioctl(struct drm_device *dev,
1185                           void *data, struct drm_file *file_priv)
1186 {
1187         struct drm_mode_atomic *arg = data;
1188         uint32_t __user *objs_ptr = (uint32_t __user *)(unsigned long)(arg->objs_ptr);
1189         uint32_t __user *count_props_ptr = (uint32_t __user *)(unsigned long)(arg->count_props_ptr);
1190         uint32_t __user *props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
1191         uint64_t __user *prop_values_ptr = (uint64_t __user *)(unsigned long)(arg->prop_values_ptr);
1192         unsigned int copied_objs, copied_props;
1193         struct drm_atomic_state *state;
1194         struct drm_modeset_acquire_ctx ctx;
1195         struct drm_plane *plane;
1196         unsigned plane_mask = 0;
1197         int ret = 0;
1198         unsigned int i, j;
1199
1200         /* disallow for drivers not supporting atomic: */
1201         if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
1202                 return -EINVAL;
1203
1204         /* disallow for userspace that has not enabled atomic cap (even
1205          * though this may be a bit overkill, since legacy userspace
1206          * wouldn't know how to call this ioctl)
1207          */
1208         if (!file_priv->atomic)
1209                 return -EINVAL;
1210
1211         if (arg->flags & ~DRM_MODE_ATOMIC_FLAGS)
1212                 return -EINVAL;
1213
1214         if (arg->reserved)
1215                 return -EINVAL;
1216
1217         if ((arg->flags & DRM_MODE_PAGE_FLIP_ASYNC) &&
1218                         !dev->mode_config.async_page_flip)
1219                 return -EINVAL;
1220
1221         /* can't test and expect an event at the same time. */
1222         if ((arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) &&
1223                         (arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
1224                 return -EINVAL;
1225
1226         drm_modeset_acquire_init(&ctx, 0);
1227
1228         state = drm_atomic_state_alloc(dev);
1229         if (!state)
1230                 return -ENOMEM;
1231
1232         state->acquire_ctx = &ctx;
1233         state->allow_modeset = !!(arg->flags & DRM_MODE_ATOMIC_ALLOW_MODESET);
1234
1235 retry:
1236         copied_objs = 0;
1237         copied_props = 0;
1238
1239         for (i = 0; i < arg->count_objs; i++) {
1240                 uint32_t obj_id, count_props;
1241                 struct drm_mode_object *obj;
1242
1243                 if (get_user(obj_id, objs_ptr + copied_objs)) {
1244                         ret = -EFAULT;
1245                         goto fail;
1246                 }
1247
1248                 obj = drm_mode_object_find(dev, obj_id, DRM_MODE_OBJECT_ANY);
1249                 if (!obj || !obj->properties) {
1250                         ret = -ENOENT;
1251                         goto fail;
1252                 }
1253
1254                 if (obj->type == DRM_MODE_OBJECT_PLANE) {
1255                         plane = obj_to_plane(obj);
1256                         plane_mask |= (1 << drm_plane_index(plane));
1257                         plane->old_fb = plane->fb;
1258                 }
1259
1260                 if (get_user(count_props, count_props_ptr + copied_objs)) {
1261                         ret = -EFAULT;
1262                         goto fail;
1263                 }
1264
1265                 copied_objs++;
1266
1267                 for (j = 0; j < count_props; j++) {
1268                         uint32_t prop_id;
1269                         uint64_t prop_value;
1270                         struct drm_property *prop;
1271
1272                         if (get_user(prop_id, props_ptr + copied_props)) {
1273                                 ret = -EFAULT;
1274                                 goto fail;
1275                         }
1276
1277                         prop = drm_property_find(dev, prop_id);
1278                         if (!prop) {
1279                                 ret = -ENOENT;
1280                                 goto fail;
1281                         }
1282
1283                         if (copy_from_user(&prop_value,
1284                                            prop_values_ptr + copied_props,
1285                                            sizeof(prop_value))) {
1286                                 ret = -EFAULT;
1287                                 goto fail;
1288                         }
1289
1290                         ret = atomic_set_prop(state, obj, prop, prop_value);
1291                         if (ret)
1292                                 goto fail;
1293
1294                         copied_props++;
1295                 }
1296         }
1297
1298         if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
1299                 int ncrtcs = dev->mode_config.num_crtc;
1300
1301                 for (i = 0; i < ncrtcs; i++) {
1302                         struct drm_crtc_state *crtc_state = state->crtc_states[i];
1303                         struct drm_pending_vblank_event *e;
1304
1305                         if (!crtc_state)
1306                                 continue;
1307
1308                         e = create_vblank_event(dev, file_priv, arg->user_data);
1309                         if (!e) {
1310                                 ret = -ENOMEM;
1311                                 goto fail;
1312                         }
1313
1314                         crtc_state->event = e;
1315                 }
1316         }
1317
1318         if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) {
1319                 ret = drm_atomic_check_only(state);
1320                 /* _check_only() does not free state, unlike _commit() */
1321                 drm_atomic_state_free(state);
1322         } else if (arg->flags & DRM_MODE_ATOMIC_NONBLOCK) {
1323                 ret = drm_atomic_async_commit(state);
1324         } else {
1325                 ret = drm_atomic_commit(state);
1326         }
1327
1328         /* if succeeded, fixup legacy plane crtc/fb ptrs before dropping
1329          * locks (ie. while it is still safe to deref plane->state).  We
1330          * need to do this here because the driver entry points cannot
1331          * distinguish between legacy and atomic ioctls.
1332          */
1333         drm_for_each_plane_mask(plane, dev, plane_mask) {
1334                 if (ret == 0) {
1335                         struct drm_framebuffer *new_fb = plane->state->fb;
1336                         if (new_fb)
1337                                 drm_framebuffer_reference(new_fb);
1338                         plane->fb = new_fb;
1339                         plane->crtc = plane->state->crtc;
1340                 } else {
1341                         plane->old_fb = NULL;
1342                 }
1343                 if (plane->old_fb) {
1344                         drm_framebuffer_unreference(plane->old_fb);
1345                         plane->old_fb = NULL;
1346                 }
1347         }
1348
1349         drm_modeset_drop_locks(&ctx);
1350         drm_modeset_acquire_fini(&ctx);
1351
1352         return ret;
1353
1354 fail:
1355         if (ret == -EDEADLK)
1356                 goto backoff;
1357
1358         if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
1359                 int ncrtcs = dev->mode_config.num_crtc;
1360
1361                 for (i = 0; i < ncrtcs; i++) {
1362                         struct drm_crtc_state *crtc_state = state->crtc_states[i];
1363
1364                         if (!crtc_state)
1365                                 continue;
1366
1367                         destroy_vblank_event(dev, file_priv, crtc_state->event);
1368                         crtc_state->event = NULL;
1369                 }
1370         }
1371
1372         drm_atomic_state_free(state);
1373
1374         drm_modeset_drop_locks(&ctx);
1375         drm_modeset_acquire_fini(&ctx);
1376
1377         return ret;
1378
1379 backoff:
1380         drm_atomic_state_clear(state);
1381         drm_modeset_backoff(&ctx);
1382
1383         goto retry;
1384 }