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