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