Merge tag 'stable/for-linus-4.0-rc6-tag' of git://git.kernel.org/pub/scm/linux/kernel...
[cascardo/linux.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_KMS("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_KMS("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_KMS("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_KMS("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_KMS("[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_KMS("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 (plane->funcs->atomic_get_property) {
454                 return plane->funcs->atomic_get_property(plane, state, property, val);
455         } else {
456                 return -EINVAL;
457         }
458
459         return 0;
460 }
461
462 /**
463  * drm_atomic_plane_check - check plane state
464  * @plane: plane to check
465  * @state: plane state to check
466  *
467  * Provides core sanity checks for plane state.
468  *
469  * RETURNS:
470  * Zero on success, error code on failure
471  */
472 static int drm_atomic_plane_check(struct drm_plane *plane,
473                 struct drm_plane_state *state)
474 {
475         unsigned int fb_width, fb_height;
476         unsigned int i;
477
478         /* either *both* CRTC and FB must be set, or neither */
479         if (WARN_ON(state->crtc && !state->fb)) {
480                 DRM_DEBUG_KMS("CRTC set but no FB\n");
481                 return -EINVAL;
482         } else if (WARN_ON(state->fb && !state->crtc)) {
483                 DRM_DEBUG_KMS("FB set but no CRTC\n");
484                 return -EINVAL;
485         }
486
487         /* if disabled, we don't care about the rest of the state: */
488         if (!state->crtc)
489                 return 0;
490
491         /* Check whether this plane is usable on this CRTC */
492         if (!(plane->possible_crtcs & drm_crtc_mask(state->crtc))) {
493                 DRM_DEBUG_KMS("Invalid crtc for plane\n");
494                 return -EINVAL;
495         }
496
497         /* Check whether this plane supports the fb pixel format. */
498         for (i = 0; i < plane->format_count; i++)
499                 if (state->fb->pixel_format == plane->format_types[i])
500                         break;
501         if (i == plane->format_count) {
502                 DRM_DEBUG_KMS("Invalid pixel format %s\n",
503                               drm_get_format_name(state->fb->pixel_format));
504                 return -EINVAL;
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_KMS("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_KMS("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_KMS("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_KMS("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_KMS("Link plane state %p to [CRTC:%d]\n",
756                               plane_state, crtc->base.id);
757         else
758                 DRM_DEBUG_KMS("Link plane state %p to [NOCRTC]\n", plane_state);
759
760         return 0;
761 }
762 EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane);
763
764 /**
765  * drm_atomic_set_fb_for_plane - set crtc for plane
766  * @plane_state: atomic state object for the plane
767  * @fb: fb to use for the plane
768  *
769  * Changing the assigned framebuffer for a plane requires us to grab a reference
770  * to the new fb and drop the reference to the old fb, if there is one. This
771  * function takes care of all these details besides updating the pointer in the
772  * state object itself.
773  */
774 void
775 drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
776                             struct drm_framebuffer *fb)
777 {
778         if (plane_state->fb)
779                 drm_framebuffer_unreference(plane_state->fb);
780         if (fb)
781                 drm_framebuffer_reference(fb);
782         plane_state->fb = fb;
783
784         if (fb)
785                 DRM_DEBUG_KMS("Set [FB:%d] for plane state %p\n",
786                               fb->base.id, plane_state);
787         else
788                 DRM_DEBUG_KMS("Set [NOFB] for plane state %p\n", plane_state);
789 }
790 EXPORT_SYMBOL(drm_atomic_set_fb_for_plane);
791
792 /**
793  * drm_atomic_set_crtc_for_connector - set crtc for connector
794  * @conn_state: atomic state object for the connector
795  * @crtc: crtc to use for the connector
796  *
797  * Changing the assigned crtc for a connector requires us to grab the lock and
798  * state for the new crtc, as needed. This function takes care of all these
799  * details besides updating the pointer in the state object itself.
800  *
801  * Returns:
802  * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
803  * then the w/w mutex code has detected a deadlock and the entire atomic
804  * sequence must be restarted. All other errors are fatal.
805  */
806 int
807 drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
808                                   struct drm_crtc *crtc)
809 {
810         struct drm_crtc_state *crtc_state;
811
812         if (crtc) {
813                 crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc);
814                 if (IS_ERR(crtc_state))
815                         return PTR_ERR(crtc_state);
816         }
817
818         conn_state->crtc = crtc;
819
820         if (crtc)
821                 DRM_DEBUG_KMS("Link connector state %p to [CRTC:%d]\n",
822                               conn_state, crtc->base.id);
823         else
824                 DRM_DEBUG_KMS("Link connector state %p to [NOCRTC]\n",
825                               conn_state);
826
827         return 0;
828 }
829 EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector);
830
831 /**
832  * drm_atomic_add_affected_connectors - add connectors for crtc
833  * @state: atomic state
834  * @crtc: DRM crtc
835  *
836  * This function walks the current configuration and adds all connectors
837  * currently using @crtc to the atomic configuration @state. Note that this
838  * function must acquire the connection mutex. This can potentially cause
839  * unneeded seralization if the update is just for the planes on one crtc. Hence
840  * drivers and helpers should only call this when really needed (e.g. when a
841  * full modeset needs to happen due to some change).
842  *
843  * Returns:
844  * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
845  * then the w/w mutex code has detected a deadlock and the entire atomic
846  * sequence must be restarted. All other errors are fatal.
847  */
848 int
849 drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
850                                    struct drm_crtc *crtc)
851 {
852         struct drm_mode_config *config = &state->dev->mode_config;
853         struct drm_connector *connector;
854         struct drm_connector_state *conn_state;
855         int ret;
856
857         ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
858         if (ret)
859                 return ret;
860
861         DRM_DEBUG_KMS("Adding all current connectors for [CRTC:%d] to %p\n",
862                       crtc->base.id, state);
863
864         /*
865          * Changed connectors are already in @state, so only need to look at the
866          * current configuration.
867          */
868         list_for_each_entry(connector, &config->connector_list, head) {
869                 if (connector->state->crtc != crtc)
870                         continue;
871
872                 conn_state = drm_atomic_get_connector_state(state, connector);
873                 if (IS_ERR(conn_state))
874                         return PTR_ERR(conn_state);
875         }
876
877         return 0;
878 }
879 EXPORT_SYMBOL(drm_atomic_add_affected_connectors);
880
881 /**
882  * drm_atomic_connectors_for_crtc - count number of connected outputs
883  * @state: atomic state
884  * @crtc: DRM crtc
885  *
886  * This function counts all connectors which will be connected to @crtc
887  * according to @state. Useful to recompute the enable state for @crtc.
888  */
889 int
890 drm_atomic_connectors_for_crtc(struct drm_atomic_state *state,
891                                struct drm_crtc *crtc)
892 {
893         int i, num_connected_connectors = 0;
894
895         for (i = 0; i < state->num_connector; i++) {
896                 struct drm_connector_state *conn_state;
897
898                 conn_state = state->connector_states[i];
899
900                 if (conn_state && conn_state->crtc == crtc)
901                         num_connected_connectors++;
902         }
903
904         DRM_DEBUG_KMS("State %p has %i connectors for [CRTC:%d]\n",
905                       state, num_connected_connectors, crtc->base.id);
906
907         return num_connected_connectors;
908 }
909 EXPORT_SYMBOL(drm_atomic_connectors_for_crtc);
910
911 /**
912  * drm_atomic_legacy_backoff - locking backoff for legacy ioctls
913  * @state: atomic state
914  *
915  * This function should be used by legacy entry points which don't understand
916  * -EDEADLK semantics. For simplicity this one will grab all modeset locks after
917  *  the slowpath completed.
918  */
919 void drm_atomic_legacy_backoff(struct drm_atomic_state *state)
920 {
921         int ret;
922
923 retry:
924         drm_modeset_backoff(state->acquire_ctx);
925
926         ret = drm_modeset_lock(&state->dev->mode_config.connection_mutex,
927                                state->acquire_ctx);
928         if (ret)
929                 goto retry;
930         ret = drm_modeset_lock_all_crtcs(state->dev,
931                                          state->acquire_ctx);
932         if (ret)
933                 goto retry;
934 }
935 EXPORT_SYMBOL(drm_atomic_legacy_backoff);
936
937 /**
938  * drm_atomic_check_only - check whether a given config would work
939  * @state: atomic configuration to check
940  *
941  * Note that this function can return -EDEADLK if the driver needed to acquire
942  * more locks but encountered a deadlock. The caller must then do the usual w/w
943  * backoff dance and restart. All other errors are fatal.
944  *
945  * Returns:
946  * 0 on success, negative error code on failure.
947  */
948 int drm_atomic_check_only(struct drm_atomic_state *state)
949 {
950         struct drm_device *dev = state->dev;
951         struct drm_mode_config *config = &dev->mode_config;
952         int nplanes = config->num_total_plane;
953         int ncrtcs = config->num_crtc;
954         int i, ret = 0;
955
956         DRM_DEBUG_KMS("checking %p\n", state);
957
958         for (i = 0; i < nplanes; i++) {
959                 struct drm_plane *plane = state->planes[i];
960
961                 if (!plane)
962                         continue;
963
964                 ret = drm_atomic_plane_check(plane, state->plane_states[i]);
965                 if (ret) {
966                         DRM_DEBUG_KMS("[PLANE:%d] atomic core check failed\n",
967                                       plane->base.id);
968                         return ret;
969                 }
970         }
971
972         for (i = 0; i < ncrtcs; i++) {
973                 struct drm_crtc *crtc = state->crtcs[i];
974
975                 if (!crtc)
976                         continue;
977
978                 ret = drm_atomic_crtc_check(crtc, state->crtc_states[i]);
979                 if (ret) {
980                         DRM_DEBUG_KMS("[CRTC:%d] atomic core check failed\n",
981                                       crtc->base.id);
982                         return ret;
983                 }
984         }
985
986         if (config->funcs->atomic_check)
987                 ret = config->funcs->atomic_check(state->dev, state);
988
989         if (!state->allow_modeset) {
990                 for (i = 0; i < ncrtcs; i++) {
991                         struct drm_crtc *crtc = state->crtcs[i];
992                         struct drm_crtc_state *crtc_state = state->crtc_states[i];
993
994                         if (!crtc)
995                                 continue;
996
997                         if (crtc_state->mode_changed ||
998                             crtc_state->active_changed) {
999                                 DRM_DEBUG_KMS("[CRTC:%d] requires full modeset\n",
1000                                               crtc->base.id);
1001                                 return -EINVAL;
1002                         }
1003                 }
1004         }
1005
1006         return ret;
1007 }
1008 EXPORT_SYMBOL(drm_atomic_check_only);
1009
1010 /**
1011  * drm_atomic_commit - commit configuration atomically
1012  * @state: atomic configuration to check
1013  *
1014  * Note that this function can return -EDEADLK if the driver needed to acquire
1015  * more locks but encountered a deadlock. The caller must then do the usual w/w
1016  * backoff dance and restart. All other errors are fatal.
1017  *
1018  * Also note that on successful execution ownership of @state is transferred
1019  * from the caller of this function to the function itself. The caller must not
1020  * free or in any other way access @state. If the function fails then the caller
1021  * must clean up @state itself.
1022  *
1023  * Returns:
1024  * 0 on success, negative error code on failure.
1025  */
1026 int drm_atomic_commit(struct drm_atomic_state *state)
1027 {
1028         struct drm_mode_config *config = &state->dev->mode_config;
1029         int ret;
1030
1031         ret = drm_atomic_check_only(state);
1032         if (ret)
1033                 return ret;
1034
1035         DRM_DEBUG_KMS("commiting %p\n", state);
1036
1037         return config->funcs->atomic_commit(state->dev, state, false);
1038 }
1039 EXPORT_SYMBOL(drm_atomic_commit);
1040
1041 /**
1042  * drm_atomic_async_commit - atomic&async configuration commit
1043  * @state: atomic configuration to check
1044  *
1045  * Note that this function can return -EDEADLK if the driver needed to acquire
1046  * more locks but encountered a deadlock. The caller must then do the usual w/w
1047  * backoff dance and restart. All other errors are fatal.
1048  *
1049  * Also note that on successful execution ownership of @state is transferred
1050  * from the caller of this function to the function itself. The caller must not
1051  * free or in any other way access @state. If the function fails then the caller
1052  * must clean up @state itself.
1053  *
1054  * Returns:
1055  * 0 on success, negative error code on failure.
1056  */
1057 int drm_atomic_async_commit(struct drm_atomic_state *state)
1058 {
1059         struct drm_mode_config *config = &state->dev->mode_config;
1060         int ret;
1061
1062         ret = drm_atomic_check_only(state);
1063         if (ret)
1064                 return ret;
1065
1066         DRM_DEBUG_KMS("commiting %p asynchronously\n", state);
1067
1068         return config->funcs->atomic_commit(state->dev, state, true);
1069 }
1070 EXPORT_SYMBOL(drm_atomic_async_commit);
1071
1072 /*
1073  * The big monstor ioctl
1074  */
1075
1076 static struct drm_pending_vblank_event *create_vblank_event(
1077                 struct drm_device *dev, struct drm_file *file_priv, uint64_t user_data)
1078 {
1079         struct drm_pending_vblank_event *e = NULL;
1080         unsigned long flags;
1081
1082         spin_lock_irqsave(&dev->event_lock, flags);
1083         if (file_priv->event_space < sizeof e->event) {
1084                 spin_unlock_irqrestore(&dev->event_lock, flags);
1085                 goto out;
1086         }
1087         file_priv->event_space -= sizeof e->event;
1088         spin_unlock_irqrestore(&dev->event_lock, flags);
1089
1090         e = kzalloc(sizeof *e, GFP_KERNEL);
1091         if (e == NULL) {
1092                 spin_lock_irqsave(&dev->event_lock, flags);
1093                 file_priv->event_space += sizeof e->event;
1094                 spin_unlock_irqrestore(&dev->event_lock, flags);
1095                 goto out;
1096         }
1097
1098         e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
1099         e->event.base.length = sizeof e->event;
1100         e->event.user_data = user_data;
1101         e->base.event = &e->event.base;
1102         e->base.file_priv = file_priv;
1103         e->base.destroy = (void (*) (struct drm_pending_event *)) kfree;
1104
1105 out:
1106         return e;
1107 }
1108
1109 static void destroy_vblank_event(struct drm_device *dev,
1110                 struct drm_file *file_priv, struct drm_pending_vblank_event *e)
1111 {
1112         unsigned long flags;
1113
1114         spin_lock_irqsave(&dev->event_lock, flags);
1115         file_priv->event_space += sizeof e->event;
1116         spin_unlock_irqrestore(&dev->event_lock, flags);
1117         kfree(e);
1118 }
1119
1120 static int atomic_set_prop(struct drm_atomic_state *state,
1121                 struct drm_mode_object *obj, struct drm_property *prop,
1122                 uint64_t prop_value)
1123 {
1124         struct drm_mode_object *ref;
1125         int ret;
1126
1127         if (!drm_property_change_valid_get(prop, prop_value, &ref))
1128                 return -EINVAL;
1129
1130         switch (obj->type) {
1131         case DRM_MODE_OBJECT_CONNECTOR: {
1132                 struct drm_connector *connector = obj_to_connector(obj);
1133                 struct drm_connector_state *connector_state;
1134
1135                 connector_state = drm_atomic_get_connector_state(state, connector);
1136                 if (IS_ERR(connector_state)) {
1137                         ret = PTR_ERR(connector_state);
1138                         break;
1139                 }
1140
1141                 ret = drm_atomic_connector_set_property(connector,
1142                                 connector_state, prop, prop_value);
1143                 break;
1144         }
1145         case DRM_MODE_OBJECT_CRTC: {
1146                 struct drm_crtc *crtc = obj_to_crtc(obj);
1147                 struct drm_crtc_state *crtc_state;
1148
1149                 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1150                 if (IS_ERR(crtc_state)) {
1151                         ret = PTR_ERR(crtc_state);
1152                         break;
1153                 }
1154
1155                 ret = drm_atomic_crtc_set_property(crtc,
1156                                 crtc_state, prop, prop_value);
1157                 break;
1158         }
1159         case DRM_MODE_OBJECT_PLANE: {
1160                 struct drm_plane *plane = obj_to_plane(obj);
1161                 struct drm_plane_state *plane_state;
1162
1163                 plane_state = drm_atomic_get_plane_state(state, plane);
1164                 if (IS_ERR(plane_state)) {
1165                         ret = PTR_ERR(plane_state);
1166                         break;
1167                 }
1168
1169                 ret = drm_atomic_plane_set_property(plane,
1170                                 plane_state, prop, prop_value);
1171                 break;
1172         }
1173         default:
1174                 ret = -EINVAL;
1175                 break;
1176         }
1177
1178         drm_property_change_valid_put(prop, ref);
1179         return ret;
1180 }
1181
1182 int drm_mode_atomic_ioctl(struct drm_device *dev,
1183                           void *data, struct drm_file *file_priv)
1184 {
1185         struct drm_mode_atomic *arg = data;
1186         uint32_t __user *objs_ptr = (uint32_t __user *)(unsigned long)(arg->objs_ptr);
1187         uint32_t __user *count_props_ptr = (uint32_t __user *)(unsigned long)(arg->count_props_ptr);
1188         uint32_t __user *props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
1189         uint64_t __user *prop_values_ptr = (uint64_t __user *)(unsigned long)(arg->prop_values_ptr);
1190         unsigned int copied_objs, copied_props;
1191         struct drm_atomic_state *state;
1192         struct drm_modeset_acquire_ctx ctx;
1193         struct drm_plane *plane;
1194         unsigned plane_mask = 0;
1195         int ret = 0;
1196         unsigned int i, j;
1197
1198         /* disallow for drivers not supporting atomic: */
1199         if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
1200                 return -EINVAL;
1201
1202         /* disallow for userspace that has not enabled atomic cap (even
1203          * though this may be a bit overkill, since legacy userspace
1204          * wouldn't know how to call this ioctl)
1205          */
1206         if (!file_priv->atomic)
1207                 return -EINVAL;
1208
1209         if (arg->flags & ~DRM_MODE_ATOMIC_FLAGS)
1210                 return -EINVAL;
1211
1212         if (arg->reserved)
1213                 return -EINVAL;
1214
1215         if ((arg->flags & DRM_MODE_PAGE_FLIP_ASYNC) &&
1216                         !dev->mode_config.async_page_flip)
1217                 return -EINVAL;
1218
1219         /* can't test and expect an event at the same time. */
1220         if ((arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) &&
1221                         (arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
1222                 return -EINVAL;
1223
1224         drm_modeset_acquire_init(&ctx, 0);
1225
1226         state = drm_atomic_state_alloc(dev);
1227         if (!state)
1228                 return -ENOMEM;
1229
1230         state->acquire_ctx = &ctx;
1231         state->allow_modeset = !!(arg->flags & DRM_MODE_ATOMIC_ALLOW_MODESET);
1232
1233 retry:
1234         copied_objs = 0;
1235         copied_props = 0;
1236
1237         for (i = 0; i < arg->count_objs; i++) {
1238                 uint32_t obj_id, count_props;
1239                 struct drm_mode_object *obj;
1240
1241                 if (get_user(obj_id, objs_ptr + copied_objs)) {
1242                         ret = -EFAULT;
1243                         goto fail;
1244                 }
1245
1246                 obj = drm_mode_object_find(dev, obj_id, DRM_MODE_OBJECT_ANY);
1247                 if (!obj || !obj->properties) {
1248                         ret = -ENOENT;
1249                         goto fail;
1250                 }
1251
1252                 if (obj->type == DRM_MODE_OBJECT_PLANE) {
1253                         plane = obj_to_plane(obj);
1254                         plane_mask |= (1 << drm_plane_index(plane));
1255                         plane->old_fb = plane->fb;
1256                 }
1257
1258                 if (get_user(count_props, count_props_ptr + copied_objs)) {
1259                         ret = -EFAULT;
1260                         goto fail;
1261                 }
1262
1263                 copied_objs++;
1264
1265                 for (j = 0; j < count_props; j++) {
1266                         uint32_t prop_id;
1267                         uint64_t prop_value;
1268                         struct drm_property *prop;
1269
1270                         if (get_user(prop_id, props_ptr + copied_props)) {
1271                                 ret = -EFAULT;
1272                                 goto fail;
1273                         }
1274
1275                         prop = drm_property_find(dev, prop_id);
1276                         if (!prop) {
1277                                 ret = -ENOENT;
1278                                 goto fail;
1279                         }
1280
1281                         if (copy_from_user(&prop_value,
1282                                            prop_values_ptr + copied_props,
1283                                            sizeof(prop_value))) {
1284                                 ret = -EFAULT;
1285                                 goto fail;
1286                         }
1287
1288                         ret = atomic_set_prop(state, obj, prop, prop_value);
1289                         if (ret)
1290                                 goto fail;
1291
1292                         copied_props++;
1293                 }
1294         }
1295
1296         if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
1297                 int ncrtcs = dev->mode_config.num_crtc;
1298
1299                 for (i = 0; i < ncrtcs; i++) {
1300                         struct drm_crtc_state *crtc_state = state->crtc_states[i];
1301                         struct drm_pending_vblank_event *e;
1302
1303                         if (!crtc_state)
1304                                 continue;
1305
1306                         e = create_vblank_event(dev, file_priv, arg->user_data);
1307                         if (!e) {
1308                                 ret = -ENOMEM;
1309                                 goto fail;
1310                         }
1311
1312                         crtc_state->event = e;
1313                 }
1314         }
1315
1316         if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) {
1317                 ret = drm_atomic_check_only(state);
1318                 /* _check_only() does not free state, unlike _commit() */
1319                 drm_atomic_state_free(state);
1320         } else if (arg->flags & DRM_MODE_ATOMIC_NONBLOCK) {
1321                 ret = drm_atomic_async_commit(state);
1322         } else {
1323                 ret = drm_atomic_commit(state);
1324         }
1325
1326         /* if succeeded, fixup legacy plane crtc/fb ptrs before dropping
1327          * locks (ie. while it is still safe to deref plane->state).  We
1328          * need to do this here because the driver entry points cannot
1329          * distinguish between legacy and atomic ioctls.
1330          */
1331         drm_for_each_plane_mask(plane, dev, plane_mask) {
1332                 if (ret == 0) {
1333                         struct drm_framebuffer *new_fb = plane->state->fb;
1334                         if (new_fb)
1335                                 drm_framebuffer_reference(new_fb);
1336                         plane->fb = new_fb;
1337                         plane->crtc = plane->state->crtc;
1338                 } else {
1339                         plane->old_fb = NULL;
1340                 }
1341                 if (plane->old_fb) {
1342                         drm_framebuffer_unreference(plane->old_fb);
1343                         plane->old_fb = NULL;
1344                 }
1345         }
1346
1347         drm_modeset_drop_locks(&ctx);
1348         drm_modeset_acquire_fini(&ctx);
1349
1350         return ret;
1351
1352 fail:
1353         if (ret == -EDEADLK)
1354                 goto backoff;
1355
1356         if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
1357                 int ncrtcs = dev->mode_config.num_crtc;
1358
1359                 for (i = 0; i < ncrtcs; i++) {
1360                         struct drm_crtc_state *crtc_state = state->crtc_states[i];
1361
1362                         if (!crtc_state)
1363                                 continue;
1364
1365                         destroy_vblank_event(dev, file_priv, crtc_state->event);
1366                         crtc_state->event = NULL;
1367                 }
1368         }
1369
1370         drm_atomic_state_free(state);
1371
1372         drm_modeset_drop_locks(&ctx);
1373         drm_modeset_acquire_fini(&ctx);
1374
1375         return ret;
1376
1377 backoff:
1378         drm_atomic_state_clear(state);
1379         drm_modeset_backoff(&ctx);
1380
1381         goto retry;
1382 }