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