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