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