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