1 /*
2  * Copyright (C) 2014 Red Hat
3  * Copyright (C) 2014 Intel Corp.
4  * Copyright (C) 2018 Intel Corp.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22  * OTHER DEALINGS IN THE SOFTWARE.
23  *
24  * Authors:
25  * Rob Clark <robdclark@gmail.com>
26  * Daniel Vetter <daniel.vetter@ffwll.ch>
27  */
28 
29 #include <drm/drm_atomic_uapi.h>
30 #include <drm/drm_atomic.h>
31 #include <drm/drm_print.h>
32 #include <drm/drm_drv.h>
33 #include <drm/drm_writeback.h>
34 #include <drm/drm_vblank.h>
35 
36 #include <linux/dma-fence.h>
37 #include <linux/uaccess.h>
38 #include <linux/sync_file.h>
39 #include <linux/file.h>
40 
41 #include "drm_crtc_internal.h"
42 
43 /**
44  * DOC: overview
45  *
46  * This file contains the marshalling and demarshalling glue for the atomic UAPI
47  * in all its forms: The monster ATOMIC IOCTL itself, code for GET_PROPERTY and
48  * SET_PROPERTY IOCTLs. Plus interface functions for compatibility helpers and
49  * drivers which have special needs to construct their own atomic updates, e.g.
50  * for load detect or similiar.
51  */
52 
53 /**
54  * drm_atomic_set_mode_for_crtc - set mode for CRTC
55  * @state: the CRTC whose incoming state to update
56  * @mode: kernel-internal mode to use for the CRTC, or NULL to disable
57  *
58  * Set a mode (originating from the kernel) on the desired CRTC state and update
59  * the enable property.
60  *
61  * RETURNS:
62  * Zero on success, error code on failure. Cannot return -EDEADLK.
63  */
64 int drm_atomic_set_mode_for_crtc(struct drm_crtc_state *state,
65 				 const struct drm_display_mode *mode)
66 {
67 	struct drm_crtc *crtc = state->crtc;
68 	struct drm_mode_modeinfo umode;
69 
70 	/* Early return for no change. */
71 	if (mode && memcmp(&state->mode, mode, sizeof(*mode)) == 0)
72 		return 0;
73 
74 	drm_property_blob_put(state->mode_blob);
75 	state->mode_blob = NULL;
76 
77 	if (mode) {
78 		drm_mode_convert_to_umode(&umode, mode);
79 		state->mode_blob =
80 			drm_property_create_blob(state->crtc->dev,
81 		                                 sizeof(umode),
82 		                                 &umode);
83 		if (IS_ERR(state->mode_blob))
84 			return PTR_ERR(state->mode_blob);
85 
86 		drm_mode_copy(&state->mode, mode);
87 		state->enable = true;
88 		DRM_DEBUG_ATOMIC("Set [MODE:%s] for [CRTC:%d:%s] state %p\n",
89 				 mode->name, crtc->base.id, crtc->name, state);
90 	} else {
91 		memset(&state->mode, 0, sizeof(state->mode));
92 		state->enable = false;
93 		DRM_DEBUG_ATOMIC("Set [NOMODE] for [CRTC:%d:%s] state %p\n",
94 				 crtc->base.id, crtc->name, state);
95 	}
96 
97 	return 0;
98 }
99 EXPORT_SYMBOL(drm_atomic_set_mode_for_crtc);
100 
101 /**
102  * drm_atomic_set_mode_prop_for_crtc - set mode for CRTC
103  * @state: the CRTC whose incoming state to update
104  * @blob: pointer to blob property to use for mode
105  *
106  * Set a mode (originating from a blob property) on the desired CRTC state.
107  * This function will take a reference on the blob property for the CRTC state,
108  * and release the reference held on the state's existing mode property, if any
109  * was set.
110  *
111  * RETURNS:
112  * Zero on success, error code on failure. Cannot return -EDEADLK.
113  */
114 int drm_atomic_set_mode_prop_for_crtc(struct drm_crtc_state *state,
115                                       struct drm_property_blob *blob)
116 {
117 	struct drm_crtc *crtc = state->crtc;
118 
119 	if (blob == state->mode_blob)
120 		return 0;
121 
122 	drm_property_blob_put(state->mode_blob);
123 	state->mode_blob = NULL;
124 
125 	memset(&state->mode, 0, sizeof(state->mode));
126 
127 	if (blob) {
128 		int ret;
129 
130 		if (blob->length != sizeof(struct drm_mode_modeinfo)) {
131 			DRM_DEBUG_ATOMIC("[CRTC:%d:%s] bad mode blob length: %zu\n",
132 					 crtc->base.id, crtc->name,
133 					 blob->length);
134 			return -EINVAL;
135 		}
136 
137 		ret = drm_mode_convert_umode(crtc->dev,
138 					     &state->mode, blob->data);
139 		if (ret) {
140 			DRM_DEBUG_ATOMIC("[CRTC:%d:%s] invalid mode (ret=%d, status=%s):\n",
141 					 crtc->base.id, crtc->name,
142 					 ret, drm_get_mode_status_name(state->mode.status));
143 			drm_mode_debug_printmodeline(&state->mode);
144 			return -EINVAL;
145 		}
146 
147 		state->mode_blob = drm_property_blob_get(blob);
148 		state->enable = true;
149 		DRM_DEBUG_ATOMIC("Set [MODE:%s] for [CRTC:%d:%s] state %p\n",
150 				 state->mode.name, crtc->base.id, crtc->name,
151 				 state);
152 	} else {
153 		state->enable = false;
154 		DRM_DEBUG_ATOMIC("Set [NOMODE] for [CRTC:%d:%s] state %p\n",
155 				 crtc->base.id, crtc->name, state);
156 	}
157 
158 	return 0;
159 }
160 EXPORT_SYMBOL(drm_atomic_set_mode_prop_for_crtc);
161 
162 /**
163  * drm_atomic_set_crtc_for_plane - set crtc for plane
164  * @plane_state: the plane whose incoming state to update
165  * @crtc: crtc to use for the plane
166  *
167  * Changing the assigned crtc for a plane requires us to grab the lock and state
168  * for the new crtc, as needed. This function takes care of all these details
169  * besides updating the pointer in the state object itself.
170  *
171  * Returns:
172  * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
173  * then the w/w mutex code has detected a deadlock and the entire atomic
174  * sequence must be restarted. All other errors are fatal.
175  */
176 int
177 drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
178 			      struct drm_crtc *crtc)
179 {
180 	struct drm_plane *plane = plane_state->plane;
181 	struct drm_crtc_state *crtc_state;
182 	/* Nothing to do for same crtc*/
183 	if (plane_state->crtc == crtc)
184 		return 0;
185 	if (plane_state->crtc) {
186 		crtc_state = drm_atomic_get_crtc_state(plane_state->state,
187 						       plane_state->crtc);
188 		if (WARN_ON(IS_ERR(crtc_state)))
189 			return PTR_ERR(crtc_state);
190 
191 		crtc_state->plane_mask &= ~drm_plane_mask(plane);
192 	}
193 
194 	plane_state->crtc = crtc;
195 
196 	if (crtc) {
197 		crtc_state = drm_atomic_get_crtc_state(plane_state->state,
198 						       crtc);
199 		if (IS_ERR(crtc_state))
200 			return PTR_ERR(crtc_state);
201 		crtc_state->plane_mask |= drm_plane_mask(plane);
202 	}
203 
204 	if (crtc)
205 		DRM_DEBUG_ATOMIC("Link [PLANE:%d:%s] state %p to [CRTC:%d:%s]\n",
206 				 plane->base.id, plane->name, plane_state,
207 				 crtc->base.id, crtc->name);
208 	else
209 		DRM_DEBUG_ATOMIC("Link [PLANE:%d:%s] state %p to [NOCRTC]\n",
210 				 plane->base.id, plane->name, plane_state);
211 
212 	return 0;
213 }
214 EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane);
215 
216 /**
217  * drm_atomic_set_fb_for_plane - set framebuffer for plane
218  * @plane_state: atomic state object for the plane
219  * @fb: fb to use for the plane
220  *
221  * Changing the assigned framebuffer for a plane requires us to grab a reference
222  * to the new fb and drop the reference to the old fb, if there is one. This
223  * function takes care of all these details besides updating the pointer in the
224  * state object itself.
225  */
226 void
227 drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
228 			    struct drm_framebuffer *fb)
229 {
230 	struct drm_plane *plane = plane_state->plane;
231 
232 	if (fb)
233 		DRM_DEBUG_ATOMIC("Set [FB:%d] for [PLANE:%d:%s] state %p\n",
234 				 fb->base.id, plane->base.id, plane->name,
235 				 plane_state);
236 	else
237 		DRM_DEBUG_ATOMIC("Set [NOFB] for [PLANE:%d:%s] state %p\n",
238 				 plane->base.id, plane->name, plane_state);
239 
240 	drm_framebuffer_assign(&plane_state->fb, fb);
241 }
242 EXPORT_SYMBOL(drm_atomic_set_fb_for_plane);
243 
244 /**
245  * drm_atomic_set_fence_for_plane - set fence for plane
246  * @plane_state: atomic state object for the plane
247  * @fence: dma_fence to use for the plane
248  *
249  * Helper to setup the plane_state fence in case it is not set yet.
250  * By using this drivers doesn't need to worry if the user choose
251  * implicit or explicit fencing.
252  *
253  * This function will not set the fence to the state if it was set
254  * via explicit fencing interfaces on the atomic ioctl. In that case it will
255  * drop the reference to the fence as we are not storing it anywhere.
256  * Otherwise, if &drm_plane_state.fence is not set this function we just set it
257  * with the received implicit fence. In both cases this function consumes a
258  * reference for @fence.
259  *
260  * This way explicit fencing can be used to overrule implicit fencing, which is
261  * important to make explicit fencing use-cases work: One example is using one
262  * buffer for 2 screens with different refresh rates. Implicit fencing will
263  * clamp rendering to the refresh rate of the slower screen, whereas explicit
264  * fence allows 2 independent render and display loops on a single buffer. If a
265  * driver allows obeys both implicit and explicit fences for plane updates, then
266  * it will break all the benefits of explicit fencing.
267  */
268 void
269 drm_atomic_set_fence_for_plane(struct drm_plane_state *plane_state,
270 			       struct dma_fence *fence)
271 {
272 	if (plane_state->fence) {
273 		dma_fence_put(fence);
274 		return;
275 	}
276 
277 	plane_state->fence = fence;
278 }
279 EXPORT_SYMBOL(drm_atomic_set_fence_for_plane);
280 
281 /**
282  * drm_atomic_set_crtc_for_connector - set crtc for connector
283  * @conn_state: atomic state object for the connector
284  * @crtc: crtc to use for the connector
285  *
286  * Changing the assigned crtc for a connector requires us to grab the lock and
287  * state for the new crtc, as needed. This function takes care of all these
288  * details besides updating the pointer in the state object itself.
289  *
290  * Returns:
291  * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
292  * then the w/w mutex code has detected a deadlock and the entire atomic
293  * sequence must be restarted. All other errors are fatal.
294  */
295 int
296 drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
297 				  struct drm_crtc *crtc)
298 {
299 	struct drm_connector *connector = conn_state->connector;
300 	struct drm_crtc_state *crtc_state;
301 
302 	if (conn_state->crtc == crtc)
303 		return 0;
304 
305 	if (conn_state->crtc) {
306 		crtc_state = drm_atomic_get_new_crtc_state(conn_state->state,
307 							   conn_state->crtc);
308 
309 		crtc_state->connector_mask &=
310 			~drm_connector_mask(conn_state->connector);
311 
312 		drm_connector_put(conn_state->connector);
313 		conn_state->crtc = NULL;
314 	}
315 
316 	if (crtc) {
317 		crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc);
318 		if (IS_ERR(crtc_state))
319 			return PTR_ERR(crtc_state);
320 
321 		crtc_state->connector_mask |=
322 			drm_connector_mask(conn_state->connector);
323 
324 		drm_connector_get(conn_state->connector);
325 		conn_state->crtc = crtc;
326 
327 		DRM_DEBUG_ATOMIC("Link [CONNECTOR:%d:%s] state %p to [CRTC:%d:%s]\n",
328 				 connector->base.id, connector->name,
329 				 conn_state, crtc->base.id, crtc->name);
330 	} else {
331 		DRM_DEBUG_ATOMIC("Link [CONNECTOR:%d:%s] state %p to [NOCRTC]\n",
332 				 connector->base.id, connector->name,
333 				 conn_state);
334 	}
335 
336 	return 0;
337 }
338 EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector);
339 
340 static void set_out_fence_for_crtc(struct drm_atomic_state *state,
341 				   struct drm_crtc *crtc, s32 __user *fence_ptr)
342 {
343 	state->crtcs[drm_crtc_index(crtc)].out_fence_ptr = fence_ptr;
344 }
345 
346 static s32 __user *get_out_fence_for_crtc(struct drm_atomic_state *state,
347 					  struct drm_crtc *crtc)
348 {
349 	s32 __user *fence_ptr;
350 
351 	fence_ptr = state->crtcs[drm_crtc_index(crtc)].out_fence_ptr;
352 	state->crtcs[drm_crtc_index(crtc)].out_fence_ptr = NULL;
353 
354 	return fence_ptr;
355 }
356 
357 static int set_out_fence_for_connector(struct drm_atomic_state *state,
358 					struct drm_connector *connector,
359 					s32 __user *fence_ptr)
360 {
361 	unsigned int index = drm_connector_index(connector);
362 
363 	if (!fence_ptr)
364 		return 0;
365 
366 	if (put_user(-1, fence_ptr))
367 		return -EFAULT;
368 
369 	state->connectors[index].out_fence_ptr = fence_ptr;
370 
371 	return 0;
372 }
373 
374 static s32 __user *get_out_fence_for_connector(struct drm_atomic_state *state,
375 					       struct drm_connector *connector)
376 {
377 	unsigned int index = drm_connector_index(connector);
378 	s32 __user *fence_ptr;
379 
380 	fence_ptr = state->connectors[index].out_fence_ptr;
381 	state->connectors[index].out_fence_ptr = NULL;
382 
383 	return fence_ptr;
384 }
385 
386 static int
387 drm_atomic_replace_property_blob_from_id(struct drm_device *dev,
388 					 struct drm_property_blob **blob,
389 					 uint64_t blob_id,
390 					 ssize_t expected_size,
391 					 ssize_t expected_elem_size,
392 					 bool *replaced)
393 {
394 	struct drm_property_blob *new_blob = NULL;
395 
396 	if (blob_id != 0) {
397 		new_blob = drm_property_lookup_blob(dev, blob_id);
398 		if (new_blob == NULL)
399 			return -EINVAL;
400 
401 		if (expected_size > 0 &&
402 		    new_blob->length != expected_size) {
403 			drm_property_blob_put(new_blob);
404 			return -EINVAL;
405 		}
406 		if (expected_elem_size > 0 &&
407 		    new_blob->length % expected_elem_size != 0) {
408 			drm_property_blob_put(new_blob);
409 			return -EINVAL;
410 		}
411 	}
412 
413 	*replaced |= drm_property_replace_blob(blob, new_blob);
414 	drm_property_blob_put(new_blob);
415 
416 	return 0;
417 }
418 
419 static int drm_atomic_crtc_set_property(struct drm_crtc *crtc,
420 		struct drm_crtc_state *state, struct drm_property *property,
421 		uint64_t val)
422 {
423 	struct drm_device *dev = crtc->dev;
424 	struct drm_mode_config *config = &dev->mode_config;
425 	bool replaced = false;
426 	int ret;
427 
428 	if (property == config->prop_active)
429 		state->active = val;
430 	else if (property == config->prop_mode_id) {
431 		struct drm_property_blob *mode =
432 			drm_property_lookup_blob(dev, val);
433 		ret = drm_atomic_set_mode_prop_for_crtc(state, mode);
434 		drm_property_blob_put(mode);
435 		return ret;
436 	} else if (property == config->prop_vrr_enabled) {
437 		state->vrr_enabled = val;
438 	} else if (property == config->degamma_lut_property) {
439 		ret = drm_atomic_replace_property_blob_from_id(dev,
440 					&state->degamma_lut,
441 					val,
442 					-1, sizeof(struct drm_color_lut),
443 					&replaced);
444 		state->color_mgmt_changed |= replaced;
445 		return ret;
446 	} else if (property == config->ctm_property) {
447 		ret = drm_atomic_replace_property_blob_from_id(dev,
448 					&state->ctm,
449 					val,
450 					sizeof(struct drm_color_ctm), -1,
451 					&replaced);
452 		state->color_mgmt_changed |= replaced;
453 		return ret;
454 	} else if (property == config->gamma_lut_property) {
455 		ret = drm_atomic_replace_property_blob_from_id(dev,
456 					&state->gamma_lut,
457 					val,
458 					-1, sizeof(struct drm_color_lut),
459 					&replaced);
460 		state->color_mgmt_changed |= replaced;
461 		return ret;
462 	} else if (property == config->prop_out_fence_ptr) {
463 		s32 __user *fence_ptr = u64_to_user_ptr(val);
464 
465 		if (!fence_ptr)
466 			return 0;
467 
468 		if (put_user(-1, fence_ptr))
469 			return -EFAULT;
470 
471 		set_out_fence_for_crtc(state->state, crtc, fence_ptr);
472 	} else if (crtc->funcs->atomic_set_property) {
473 		return crtc->funcs->atomic_set_property(crtc, state, property, val);
474 	} else {
475 		DRM_DEBUG_ATOMIC("[CRTC:%d:%s] unknown property [PROP:%d:%s]]\n",
476 				 crtc->base.id, crtc->name,
477 				 property->base.id, property->name);
478 		return -EINVAL;
479 	}
480 
481 	return 0;
482 }
483 
484 static int
485 drm_atomic_crtc_get_property(struct drm_crtc *crtc,
486 		const struct drm_crtc_state *state,
487 		struct drm_property *property, uint64_t *val)
488 {
489 	struct drm_device *dev = crtc->dev;
490 	struct drm_mode_config *config = &dev->mode_config;
491 
492 	if (property == config->prop_active)
493 		*val = state->active;
494 	else if (property == config->prop_mode_id)
495 		*val = (state->mode_blob) ? state->mode_blob->base.id : 0;
496 	else if (property == config->prop_vrr_enabled)
497 		*val = state->vrr_enabled;
498 	else if (property == config->degamma_lut_property)
499 		*val = (state->degamma_lut) ? state->degamma_lut->base.id : 0;
500 	else if (property == config->ctm_property)
501 		*val = (state->ctm) ? state->ctm->base.id : 0;
502 	else if (property == config->gamma_lut_property)
503 		*val = (state->gamma_lut) ? state->gamma_lut->base.id : 0;
504 	else if (property == config->prop_out_fence_ptr)
505 		*val = 0;
506 	else if (crtc->funcs->atomic_get_property)
507 		return crtc->funcs->atomic_get_property(crtc, state, property, val);
508 	else
509 		return -EINVAL;
510 
511 	return 0;
512 }
513 
514 static int drm_atomic_plane_set_property(struct drm_plane *plane,
515 		struct drm_plane_state *state, struct drm_property *property,
516 		uint64_t val)
517 {
518 	struct drm_device *dev = plane->dev;
519 	struct drm_mode_config *config = &dev->mode_config;
520 	bool replaced = false;
521 	int ret;
522 
523 	if (property == config->prop_fb_id) {
524 		struct drm_framebuffer *fb = drm_framebuffer_lookup(dev, NULL, val);
525 		drm_atomic_set_fb_for_plane(state, fb);
526 		if (fb)
527 			drm_framebuffer_put(fb);
528 	} else if (property == config->prop_in_fence_fd) {
529 		if (state->fence)
530 			return -EINVAL;
531 
532 		if (U642I64(val) == -1)
533 			return 0;
534 
535 		state->fence = sync_file_get_fence(val);
536 		if (!state->fence)
537 			return -EINVAL;
538 
539 	} else if (property == config->prop_crtc_id) {
540 		struct drm_crtc *crtc = drm_crtc_find(dev, NULL, val);
541 		return drm_atomic_set_crtc_for_plane(state, crtc);
542 	} else if (property == config->prop_crtc_x) {
543 		state->crtc_x = U642I64(val);
544 	} else if (property == config->prop_crtc_y) {
545 		state->crtc_y = U642I64(val);
546 	} else if (property == config->prop_crtc_w) {
547 		state->crtc_w = val;
548 	} else if (property == config->prop_crtc_h) {
549 		state->crtc_h = val;
550 	} else if (property == config->prop_src_x) {
551 		state->src_x = val;
552 	} else if (property == config->prop_src_y) {
553 		state->src_y = val;
554 	} else if (property == config->prop_src_w) {
555 		state->src_w = val;
556 	} else if (property == config->prop_src_h) {
557 		state->src_h = val;
558 	} else if (property == plane->alpha_property) {
559 		state->alpha = val;
560 	} else if (property == plane->blend_mode_property) {
561 		state->pixel_blend_mode = val;
562 	} else if (property == plane->rotation_property) {
563 		if (!is_power_of_2(val & DRM_MODE_ROTATE_MASK)) {
564 			DRM_DEBUG_ATOMIC("[PLANE:%d:%s] bad rotation bitmask: 0x%llx\n",
565 					 plane->base.id, plane->name, val);
566 			return -EINVAL;
567 		}
568 		state->rotation = val;
569 	} else if (property == plane->zpos_property) {
570 		state->zpos = val;
571 	} else if (property == plane->color_encoding_property) {
572 		state->color_encoding = val;
573 	} else if (property == plane->color_range_property) {
574 		state->color_range = val;
575 	} else if (property == config->prop_fb_damage_clips) {
576 		ret = drm_atomic_replace_property_blob_from_id(dev,
577 					&state->fb_damage_clips,
578 					val,
579 					-1,
580 					sizeof(struct drm_rect),
581 					&replaced);
582 		return ret;
583 	} else if (plane->funcs->atomic_set_property) {
584 		return plane->funcs->atomic_set_property(plane, state,
585 				property, val);
586 	} else {
587 		DRM_DEBUG_ATOMIC("[PLANE:%d:%s] unknown property [PROP:%d:%s]]\n",
588 				 plane->base.id, plane->name,
589 				 property->base.id, property->name);
590 		return -EINVAL;
591 	}
592 
593 	return 0;
594 }
595 
596 static int
597 drm_atomic_plane_get_property(struct drm_plane *plane,
598 		const struct drm_plane_state *state,
599 		struct drm_property *property, uint64_t *val)
600 {
601 	struct drm_device *dev = plane->dev;
602 	struct drm_mode_config *config = &dev->mode_config;
603 
604 	if (property == config->prop_fb_id) {
605 		*val = (state->fb) ? state->fb->base.id : 0;
606 	} else if (property == config->prop_in_fence_fd) {
607 		*val = -1;
608 	} else if (property == config->prop_crtc_id) {
609 		*val = (state->crtc) ? state->crtc->base.id : 0;
610 	} else if (property == config->prop_crtc_x) {
611 		*val = I642U64(state->crtc_x);
612 	} else if (property == config->prop_crtc_y) {
613 		*val = I642U64(state->crtc_y);
614 	} else if (property == config->prop_crtc_w) {
615 		*val = state->crtc_w;
616 	} else if (property == config->prop_crtc_h) {
617 		*val = state->crtc_h;
618 	} else if (property == config->prop_src_x) {
619 		*val = state->src_x;
620 	} else if (property == config->prop_src_y) {
621 		*val = state->src_y;
622 	} else if (property == config->prop_src_w) {
623 		*val = state->src_w;
624 	} else if (property == config->prop_src_h) {
625 		*val = state->src_h;
626 	} else if (property == plane->alpha_property) {
627 		*val = state->alpha;
628 	} else if (property == plane->blend_mode_property) {
629 		*val = state->pixel_blend_mode;
630 	} else if (property == plane->rotation_property) {
631 		*val = state->rotation;
632 	} else if (property == plane->zpos_property) {
633 		*val = state->zpos;
634 	} else if (property == plane->color_encoding_property) {
635 		*val = state->color_encoding;
636 	} else if (property == plane->color_range_property) {
637 		*val = state->color_range;
638 	} else if (property == config->prop_fb_damage_clips) {
639 		*val = (state->fb_damage_clips) ?
640 			state->fb_damage_clips->base.id : 0;
641 	} else if (plane->funcs->atomic_get_property) {
642 		return plane->funcs->atomic_get_property(plane, state, property, val);
643 	} else {
644 		return -EINVAL;
645 	}
646 
647 	return 0;
648 }
649 
650 static struct drm_writeback_job *
651 drm_atomic_get_writeback_job(struct drm_connector_state *conn_state)
652 {
653 	WARN_ON(conn_state->connector->connector_type != DRM_MODE_CONNECTOR_WRITEBACK);
654 
655 	if (!conn_state->writeback_job)
656 		conn_state->writeback_job =
657 			kzalloc(sizeof(*conn_state->writeback_job), GFP_KERNEL);
658 
659 	return conn_state->writeback_job;
660 }
661 
662 static int drm_atomic_set_writeback_fb_for_connector(
663 		struct drm_connector_state *conn_state,
664 		struct drm_framebuffer *fb)
665 {
666 	struct drm_writeback_job *job =
667 		drm_atomic_get_writeback_job(conn_state);
668 	if (!job)
669 		return -ENOMEM;
670 
671 	drm_framebuffer_assign(&job->fb, fb);
672 
673 	if (fb)
674 		DRM_DEBUG_ATOMIC("Set [FB:%d] for connector state %p\n",
675 				 fb->base.id, conn_state);
676 	else
677 		DRM_DEBUG_ATOMIC("Set [NOFB] for connector state %p\n",
678 				 conn_state);
679 
680 	return 0;
681 }
682 
683 static int drm_atomic_connector_set_property(struct drm_connector *connector,
684 		struct drm_connector_state *state, struct drm_property *property,
685 		uint64_t val)
686 {
687 	struct drm_device *dev = connector->dev;
688 	struct drm_mode_config *config = &dev->mode_config;
689 
690 	if (property == config->prop_crtc_id) {
691 		struct drm_crtc *crtc = drm_crtc_find(dev, NULL, val);
692 		return drm_atomic_set_crtc_for_connector(state, crtc);
693 	} else if (property == config->dpms_property) {
694 		/* setting DPMS property requires special handling, which
695 		 * is done in legacy setprop path for us.  Disallow (for
696 		 * now?) atomic writes to DPMS property:
697 		 */
698 		return -EINVAL;
699 	} else if (property == config->tv_select_subconnector_property) {
700 		state->tv.subconnector = val;
701 	} else if (property == config->tv_left_margin_property) {
702 		state->tv.margins.left = val;
703 	} else if (property == config->tv_right_margin_property) {
704 		state->tv.margins.right = val;
705 	} else if (property == config->tv_top_margin_property) {
706 		state->tv.margins.top = val;
707 	} else if (property == config->tv_bottom_margin_property) {
708 		state->tv.margins.bottom = val;
709 	} else if (property == config->tv_mode_property) {
710 		state->tv.mode = val;
711 	} else if (property == config->tv_brightness_property) {
712 		state->tv.brightness = val;
713 	} else if (property == config->tv_contrast_property) {
714 		state->tv.contrast = val;
715 	} else if (property == config->tv_flicker_reduction_property) {
716 		state->tv.flicker_reduction = val;
717 	} else if (property == config->tv_overscan_property) {
718 		state->tv.overscan = val;
719 	} else if (property == config->tv_saturation_property) {
720 		state->tv.saturation = val;
721 	} else if (property == config->tv_hue_property) {
722 		state->tv.hue = val;
723 	} else if (property == config->link_status_property) {
724 		/* Never downgrade from GOOD to BAD on userspace's request here,
725 		 * only hw issues can do that.
726 		 *
727 		 * For an atomic property the userspace doesn't need to be able
728 		 * to understand all the properties, but needs to be able to
729 		 * restore the state it wants on VT switch. So if the userspace
730 		 * tries to change the link_status from GOOD to BAD, driver
731 		 * silently rejects it and returns a 0. This prevents userspace
732 		 * from accidently breaking  the display when it restores the
733 		 * state.
734 		 */
735 		if (state->link_status != DRM_LINK_STATUS_GOOD)
736 			state->link_status = val;
737 	} else if (property == config->aspect_ratio_property) {
738 		state->picture_aspect_ratio = val;
739 	} else if (property == config->content_type_property) {
740 		state->content_type = val;
741 	} else if (property == connector->scaling_mode_property) {
742 		state->scaling_mode = val;
743 	} else if (property == connector->content_protection_property) {
744 		if (val == DRM_MODE_CONTENT_PROTECTION_ENABLED) {
745 			DRM_DEBUG_KMS("only drivers can set CP Enabled\n");
746 			return -EINVAL;
747 		}
748 		state->content_protection = val;
749 	} else if (property == config->writeback_fb_id_property) {
750 		struct drm_framebuffer *fb = drm_framebuffer_lookup(dev, NULL, val);
751 		int ret = drm_atomic_set_writeback_fb_for_connector(state, fb);
752 		if (fb)
753 			drm_framebuffer_put(fb);
754 		return ret;
755 	} else if (property == config->writeback_out_fence_ptr_property) {
756 		s32 __user *fence_ptr = u64_to_user_ptr(val);
757 
758 		return set_out_fence_for_connector(state->state, connector,
759 						   fence_ptr);
760 	} else if (property == connector->max_bpc_property) {
761 		state->max_requested_bpc = val;
762 	} else if (connector->funcs->atomic_set_property) {
763 		return connector->funcs->atomic_set_property(connector,
764 				state, property, val);
765 	} else {
766 		DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] unknown property [PROP:%d:%s]]\n",
767 				 connector->base.id, connector->name,
768 				 property->base.id, property->name);
769 		return -EINVAL;
770 	}
771 
772 	return 0;
773 }
774 
775 static int
776 drm_atomic_connector_get_property(struct drm_connector *connector,
777 		const struct drm_connector_state *state,
778 		struct drm_property *property, uint64_t *val)
779 {
780 	struct drm_device *dev = connector->dev;
781 	struct drm_mode_config *config = &dev->mode_config;
782 
783 	if (property == config->prop_crtc_id) {
784 		*val = (state->crtc) ? state->crtc->base.id : 0;
785 	} else if (property == config->dpms_property) {
786 		*val = connector->dpms;
787 	} else if (property == config->tv_select_subconnector_property) {
788 		*val = state->tv.subconnector;
789 	} else if (property == config->tv_left_margin_property) {
790 		*val = state->tv.margins.left;
791 	} else if (property == config->tv_right_margin_property) {
792 		*val = state->tv.margins.right;
793 	} else if (property == config->tv_top_margin_property) {
794 		*val = state->tv.margins.top;
795 	} else if (property == config->tv_bottom_margin_property) {
796 		*val = state->tv.margins.bottom;
797 	} else if (property == config->tv_mode_property) {
798 		*val = state->tv.mode;
799 	} else if (property == config->tv_brightness_property) {
800 		*val = state->tv.brightness;
801 	} else if (property == config->tv_contrast_property) {
802 		*val = state->tv.contrast;
803 	} else if (property == config->tv_flicker_reduction_property) {
804 		*val = state->tv.flicker_reduction;
805 	} else if (property == config->tv_overscan_property) {
806 		*val = state->tv.overscan;
807 	} else if (property == config->tv_saturation_property) {
808 		*val = state->tv.saturation;
809 	} else if (property == config->tv_hue_property) {
810 		*val = state->tv.hue;
811 	} else if (property == config->link_status_property) {
812 		*val = state->link_status;
813 	} else if (property == config->aspect_ratio_property) {
814 		*val = state->picture_aspect_ratio;
815 	} else if (property == config->content_type_property) {
816 		*val = state->content_type;
817 	} else if (property == connector->scaling_mode_property) {
818 		*val = state->scaling_mode;
819 	} else if (property == connector->content_protection_property) {
820 		*val = state->content_protection;
821 	} else if (property == config->writeback_fb_id_property) {
822 		/* Writeback framebuffer is one-shot, write and forget */
823 		*val = 0;
824 	} else if (property == config->writeback_out_fence_ptr_property) {
825 		*val = 0;
826 	} else if (property == connector->max_bpc_property) {
827 		*val = state->max_requested_bpc;
828 	} else if (connector->funcs->atomic_get_property) {
829 		return connector->funcs->atomic_get_property(connector,
830 				state, property, val);
831 	} else {
832 		return -EINVAL;
833 	}
834 
835 	return 0;
836 }
837 
838 int drm_atomic_get_property(struct drm_mode_object *obj,
839 		struct drm_property *property, uint64_t *val)
840 {
841 	struct drm_device *dev = property->dev;
842 	int ret;
843 
844 	switch (obj->type) {
845 	case DRM_MODE_OBJECT_CONNECTOR: {
846 		struct drm_connector *connector = obj_to_connector(obj);
847 		WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
848 		ret = drm_atomic_connector_get_property(connector,
849 				connector->state, property, val);
850 		break;
851 	}
852 	case DRM_MODE_OBJECT_CRTC: {
853 		struct drm_crtc *crtc = obj_to_crtc(obj);
854 		WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
855 		ret = drm_atomic_crtc_get_property(crtc,
856 				crtc->state, property, val);
857 		break;
858 	}
859 	case DRM_MODE_OBJECT_PLANE: {
860 		struct drm_plane *plane = obj_to_plane(obj);
861 		WARN_ON(!drm_modeset_is_locked(&plane->mutex));
862 		ret = drm_atomic_plane_get_property(plane,
863 				plane->state, property, val);
864 		break;
865 	}
866 	default:
867 		ret = -EINVAL;
868 		break;
869 	}
870 
871 	return ret;
872 }
873 
874 /*
875  * The big monster ioctl
876  */
877 
878 static struct drm_pending_vblank_event *create_vblank_event(
879 		struct drm_crtc *crtc, uint64_t user_data)
880 {
881 	struct drm_pending_vblank_event *e = NULL;
882 
883 	e = kzalloc(sizeof *e, GFP_KERNEL);
884 	if (!e)
885 		return NULL;
886 
887 	e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
888 	e->event.base.length = sizeof(e->event);
889 	e->event.vbl.crtc_id = crtc->base.id;
890 	e->event.vbl.user_data = user_data;
891 
892 	return e;
893 }
894 
895 int drm_atomic_connector_commit_dpms(struct drm_atomic_state *state,
896 				     struct drm_connector *connector,
897 				     int mode)
898 {
899 	struct drm_connector *tmp_connector;
900 	struct drm_connector_state *new_conn_state;
901 	struct drm_crtc *crtc;
902 	struct drm_crtc_state *crtc_state;
903 	int i, ret, old_mode = connector->dpms;
904 	bool active = false;
905 
906 	ret = drm_modeset_lock(&state->dev->mode_config.connection_mutex,
907 			       state->acquire_ctx);
908 	if (ret)
909 		return ret;
910 
911 	if (mode != DRM_MODE_DPMS_ON)
912 		mode = DRM_MODE_DPMS_OFF;
913 	connector->dpms = mode;
914 
915 	crtc = connector->state->crtc;
916 	if (!crtc)
917 		goto out;
918 	ret = drm_atomic_add_affected_connectors(state, crtc);
919 	if (ret)
920 		goto out;
921 
922 	crtc_state = drm_atomic_get_crtc_state(state, crtc);
923 	if (IS_ERR(crtc_state)) {
924 		ret = PTR_ERR(crtc_state);
925 		goto out;
926 	}
927 
928 	for_each_new_connector_in_state(state, tmp_connector, new_conn_state, i) {
929 		if (new_conn_state->crtc != crtc)
930 			continue;
931 		if (tmp_connector->dpms == DRM_MODE_DPMS_ON) {
932 			active = true;
933 			break;
934 		}
935 	}
936 
937 	crtc_state->active = active;
938 	ret = drm_atomic_commit(state);
939 out:
940 	if (ret != 0)
941 		connector->dpms = old_mode;
942 	return ret;
943 }
944 
945 int drm_atomic_set_property(struct drm_atomic_state *state,
946 			    struct drm_mode_object *obj,
947 			    struct drm_property *prop,
948 			    uint64_t prop_value)
949 {
950 	struct drm_mode_object *ref;
951 	int ret;
952 
953 	if (!drm_property_change_valid_get(prop, prop_value, &ref))
954 		return -EINVAL;
955 
956 	switch (obj->type) {
957 	case DRM_MODE_OBJECT_CONNECTOR: {
958 		struct drm_connector *connector = obj_to_connector(obj);
959 		struct drm_connector_state *connector_state;
960 
961 		connector_state = drm_atomic_get_connector_state(state, connector);
962 		if (IS_ERR(connector_state)) {
963 			ret = PTR_ERR(connector_state);
964 			break;
965 		}
966 
967 		ret = drm_atomic_connector_set_property(connector,
968 				connector_state, prop, prop_value);
969 		break;
970 	}
971 	case DRM_MODE_OBJECT_CRTC: {
972 		struct drm_crtc *crtc = obj_to_crtc(obj);
973 		struct drm_crtc_state *crtc_state;
974 
975 		crtc_state = drm_atomic_get_crtc_state(state, crtc);
976 		if (IS_ERR(crtc_state)) {
977 			ret = PTR_ERR(crtc_state);
978 			break;
979 		}
980 
981 		ret = drm_atomic_crtc_set_property(crtc,
982 				crtc_state, prop, prop_value);
983 		break;
984 	}
985 	case DRM_MODE_OBJECT_PLANE: {
986 		struct drm_plane *plane = obj_to_plane(obj);
987 		struct drm_plane_state *plane_state;
988 
989 		plane_state = drm_atomic_get_plane_state(state, plane);
990 		if (IS_ERR(plane_state)) {
991 			ret = PTR_ERR(plane_state);
992 			break;
993 		}
994 
995 		ret = drm_atomic_plane_set_property(plane,
996 				plane_state, prop, prop_value);
997 		break;
998 	}
999 	default:
1000 		ret = -EINVAL;
1001 		break;
1002 	}
1003 
1004 	drm_property_change_valid_put(prop, ref);
1005 	return ret;
1006 }
1007 
1008 /**
1009  * DOC: explicit fencing properties
1010  *
1011  * Explicit fencing allows userspace to control the buffer synchronization
1012  * between devices. A Fence or a group of fences are transfered to/from
1013  * userspace using Sync File fds and there are two DRM properties for that.
1014  * IN_FENCE_FD on each DRM Plane to send fences to the kernel and
1015  * OUT_FENCE_PTR on each DRM CRTC to receive fences from the kernel.
1016  *
1017  * As a contrast, with implicit fencing the kernel keeps track of any
1018  * ongoing rendering, and automatically ensures that the atomic update waits
1019  * for any pending rendering to complete. For shared buffers represented with
1020  * a &struct dma_buf this is tracked in &struct reservation_object.
1021  * Implicit syncing is how Linux traditionally worked (e.g. DRI2/3 on X.org),
1022  * whereas explicit fencing is what Android wants.
1023  *
1024  * "IN_FENCE_FD”:
1025  *	Use this property to pass a fence that DRM should wait on before
1026  *	proceeding with the Atomic Commit request and show the framebuffer for
1027  *	the plane on the screen. The fence can be either a normal fence or a
1028  *	merged one, the sync_file framework will handle both cases and use a
1029  *	fence_array if a merged fence is received. Passing -1 here means no
1030  *	fences to wait on.
1031  *
1032  *	If the Atomic Commit request has the DRM_MODE_ATOMIC_TEST_ONLY flag
1033  *	it will only check if the Sync File is a valid one.
1034  *
1035  *	On the driver side the fence is stored on the @fence parameter of
1036  *	&struct drm_plane_state. Drivers which also support implicit fencing
1037  *	should set the implicit fence using drm_atomic_set_fence_for_plane(),
1038  *	to make sure there's consistent behaviour between drivers in precedence
1039  *	of implicit vs. explicit fencing.
1040  *
1041  * "OUT_FENCE_PTR”:
1042  *	Use this property to pass a file descriptor pointer to DRM. Once the
1043  *	Atomic Commit request call returns OUT_FENCE_PTR will be filled with
1044  *	the file descriptor number of a Sync File. This Sync File contains the
1045  *	CRTC fence that will be signaled when all framebuffers present on the
1046  *	Atomic Commit * request for that given CRTC are scanned out on the
1047  *	screen.
1048  *
1049  *	The Atomic Commit request fails if a invalid pointer is passed. If the
1050  *	Atomic Commit request fails for any other reason the out fence fd
1051  *	returned will be -1. On a Atomic Commit with the
1052  *	DRM_MODE_ATOMIC_TEST_ONLY flag the out fence will also be set to -1.
1053  *
1054  *	Note that out-fences don't have a special interface to drivers and are
1055  *	internally represented by a &struct drm_pending_vblank_event in struct
1056  *	&drm_crtc_state, which is also used by the nonblocking atomic commit
1057  *	helpers and for the DRM event handling for existing userspace.
1058  */
1059 
1060 struct drm_out_fence_state {
1061 	s32 __user *out_fence_ptr;
1062 	struct sync_file *sync_file;
1063 	int fd;
1064 };
1065 
1066 static int setup_out_fence(struct drm_out_fence_state *fence_state,
1067 			   struct dma_fence *fence)
1068 {
1069 	fence_state->fd = get_unused_fd_flags(O_CLOEXEC);
1070 	if (fence_state->fd < 0)
1071 		return fence_state->fd;
1072 
1073 	if (put_user(fence_state->fd, fence_state->out_fence_ptr))
1074 		return -EFAULT;
1075 
1076 	fence_state->sync_file = sync_file_create(fence);
1077 	if (!fence_state->sync_file)
1078 		return -ENOMEM;
1079 
1080 	return 0;
1081 }
1082 
1083 static int prepare_signaling(struct drm_device *dev,
1084 				  struct drm_atomic_state *state,
1085 				  struct drm_mode_atomic *arg,
1086 				  struct drm_file *file_priv,
1087 				  struct drm_out_fence_state **fence_state,
1088 				  unsigned int *num_fences)
1089 {
1090 	struct drm_crtc *crtc;
1091 	struct drm_crtc_state *crtc_state;
1092 	struct drm_connector *conn;
1093 	struct drm_connector_state *conn_state;
1094 	int i, c = 0, ret;
1095 
1096 	if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)
1097 		return 0;
1098 
1099 	for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
1100 		s32 __user *fence_ptr;
1101 
1102 		fence_ptr = get_out_fence_for_crtc(crtc_state->state, crtc);
1103 
1104 		if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT || fence_ptr) {
1105 			struct drm_pending_vblank_event *e;
1106 
1107 			e = create_vblank_event(crtc, arg->user_data);
1108 			if (!e)
1109 				return -ENOMEM;
1110 
1111 			crtc_state->event = e;
1112 		}
1113 
1114 		if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
1115 			struct drm_pending_vblank_event *e = crtc_state->event;
1116 
1117 			if (!file_priv)
1118 				continue;
1119 
1120 			ret = drm_event_reserve_init(dev, file_priv, &e->base,
1121 						     &e->event.base);
1122 			if (ret) {
1123 				kfree(e);
1124 				crtc_state->event = NULL;
1125 				return ret;
1126 			}
1127 		}
1128 
1129 		if (fence_ptr) {
1130 			struct dma_fence *fence;
1131 			struct drm_out_fence_state *f;
1132 
1133 			f = krealloc(*fence_state, sizeof(**fence_state) *
1134 				     (*num_fences + 1), GFP_KERNEL);
1135 			if (!f)
1136 				return -ENOMEM;
1137 
1138 			memset(&f[*num_fences], 0, sizeof(*f));
1139 
1140 			f[*num_fences].out_fence_ptr = fence_ptr;
1141 			*fence_state = f;
1142 
1143 			fence = drm_crtc_create_fence(crtc);
1144 			if (!fence)
1145 				return -ENOMEM;
1146 
1147 			ret = setup_out_fence(&f[(*num_fences)++], fence);
1148 			if (ret) {
1149 				dma_fence_put(fence);
1150 				return ret;
1151 			}
1152 
1153 			crtc_state->event->base.fence = fence;
1154 		}
1155 
1156 		c++;
1157 	}
1158 
1159 	for_each_new_connector_in_state(state, conn, conn_state, i) {
1160 		struct drm_writeback_connector *wb_conn;
1161 		struct drm_writeback_job *job;
1162 		struct drm_out_fence_state *f;
1163 		struct dma_fence *fence;
1164 		s32 __user *fence_ptr;
1165 
1166 		fence_ptr = get_out_fence_for_connector(state, conn);
1167 		if (!fence_ptr)
1168 			continue;
1169 
1170 		job = drm_atomic_get_writeback_job(conn_state);
1171 		if (!job)
1172 			return -ENOMEM;
1173 
1174 		f = krealloc(*fence_state, sizeof(**fence_state) *
1175 			     (*num_fences + 1), GFP_KERNEL);
1176 		if (!f)
1177 			return -ENOMEM;
1178 
1179 		memset(&f[*num_fences], 0, sizeof(*f));
1180 
1181 		f[*num_fences].out_fence_ptr = fence_ptr;
1182 		*fence_state = f;
1183 
1184 		wb_conn = drm_connector_to_writeback(conn);
1185 		fence = drm_writeback_get_out_fence(wb_conn);
1186 		if (!fence)
1187 			return -ENOMEM;
1188 
1189 		ret = setup_out_fence(&f[(*num_fences)++], fence);
1190 		if (ret) {
1191 			dma_fence_put(fence);
1192 			return ret;
1193 		}
1194 
1195 		job->out_fence = fence;
1196 	}
1197 
1198 	/*
1199 	 * Having this flag means user mode pends on event which will never
1200 	 * reach due to lack of at least one CRTC for signaling
1201 	 */
1202 	if (c == 0 && (arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
1203 		return -EINVAL;
1204 
1205 	return 0;
1206 }
1207 
1208 static void complete_signaling(struct drm_device *dev,
1209 			       struct drm_atomic_state *state,
1210 			       struct drm_out_fence_state *fence_state,
1211 			       unsigned int num_fences,
1212 			       bool install_fds)
1213 {
1214 	struct drm_crtc *crtc;
1215 	struct drm_crtc_state *crtc_state;
1216 	int i;
1217 
1218 	if (install_fds) {
1219 		for (i = 0; i < num_fences; i++)
1220 			fd_install(fence_state[i].fd,
1221 				   fence_state[i].sync_file->file);
1222 
1223 		kfree(fence_state);
1224 		return;
1225 	}
1226 
1227 	for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
1228 		struct drm_pending_vblank_event *event = crtc_state->event;
1229 		/*
1230 		 * Free the allocated event. drm_atomic_helper_setup_commit
1231 		 * can allocate an event too, so only free it if it's ours
1232 		 * to prevent a double free in drm_atomic_state_clear.
1233 		 */
1234 		if (event && (event->base.fence || event->base.file_priv)) {
1235 			drm_event_cancel_free(dev, &event->base);
1236 			crtc_state->event = NULL;
1237 		}
1238 	}
1239 
1240 	if (!fence_state)
1241 		return;
1242 
1243 	for (i = 0; i < num_fences; i++) {
1244 		if (fence_state[i].sync_file)
1245 			fput(fence_state[i].sync_file->file);
1246 		if (fence_state[i].fd >= 0)
1247 			put_unused_fd(fence_state[i].fd);
1248 
1249 		/* If this fails log error to the user */
1250 		if (fence_state[i].out_fence_ptr &&
1251 		    put_user(-1, fence_state[i].out_fence_ptr))
1252 			DRM_DEBUG_ATOMIC("Couldn't clear out_fence_ptr\n");
1253 	}
1254 
1255 	kfree(fence_state);
1256 }
1257 
1258 int drm_mode_atomic_ioctl(struct drm_device *dev,
1259 			  void *data, struct drm_file *file_priv)
1260 {
1261 	struct drm_mode_atomic *arg = data;
1262 	uint32_t __user *objs_ptr = (uint32_t __user *)(unsigned long)(arg->objs_ptr);
1263 	uint32_t __user *count_props_ptr = (uint32_t __user *)(unsigned long)(arg->count_props_ptr);
1264 	uint32_t __user *props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
1265 	uint64_t __user *prop_values_ptr = (uint64_t __user *)(unsigned long)(arg->prop_values_ptr);
1266 	unsigned int copied_objs, copied_props;
1267 	struct drm_atomic_state *state;
1268 	struct drm_modeset_acquire_ctx ctx;
1269 	struct drm_out_fence_state *fence_state;
1270 	int ret = 0;
1271 	unsigned int i, j, num_fences;
1272 
1273 	/* disallow for drivers not supporting atomic: */
1274 	if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
1275 		return -EOPNOTSUPP;
1276 
1277 	/* disallow for userspace that has not enabled atomic cap (even
1278 	 * though this may be a bit overkill, since legacy userspace
1279 	 * wouldn't know how to call this ioctl)
1280 	 */
1281 	if (!file_priv->atomic)
1282 		return -EINVAL;
1283 
1284 	if (arg->flags & ~DRM_MODE_ATOMIC_FLAGS)
1285 		return -EINVAL;
1286 
1287 	if (arg->reserved)
1288 		return -EINVAL;
1289 
1290 	if ((arg->flags & DRM_MODE_PAGE_FLIP_ASYNC) &&
1291 			!dev->mode_config.async_page_flip)
1292 		return -EINVAL;
1293 
1294 	/* can't test and expect an event at the same time. */
1295 	if ((arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) &&
1296 			(arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
1297 		return -EINVAL;
1298 
1299 	state = drm_atomic_state_alloc(dev);
1300 	if (!state)
1301 		return -ENOMEM;
1302 
1303 	drm_modeset_acquire_init(&ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE);
1304 	state->acquire_ctx = &ctx;
1305 	state->allow_modeset = !!(arg->flags & DRM_MODE_ATOMIC_ALLOW_MODESET);
1306 
1307 retry:
1308 	copied_objs = 0;
1309 	copied_props = 0;
1310 	fence_state = NULL;
1311 	num_fences = 0;
1312 
1313 	for (i = 0; i < arg->count_objs; i++) {
1314 		uint32_t obj_id, count_props;
1315 		struct drm_mode_object *obj;
1316 
1317 		if (get_user(obj_id, objs_ptr + copied_objs)) {
1318 			ret = -EFAULT;
1319 			goto out;
1320 		}
1321 
1322 		obj = drm_mode_object_find(dev, file_priv, obj_id, DRM_MODE_OBJECT_ANY);
1323 		if (!obj) {
1324 			ret = -ENOENT;
1325 			goto out;
1326 		}
1327 
1328 		if (!obj->properties) {
1329 			drm_mode_object_put(obj);
1330 			ret = -ENOENT;
1331 			goto out;
1332 		}
1333 
1334 		if (get_user(count_props, count_props_ptr + copied_objs)) {
1335 			drm_mode_object_put(obj);
1336 			ret = -EFAULT;
1337 			goto out;
1338 		}
1339 
1340 		copied_objs++;
1341 
1342 		for (j = 0; j < count_props; j++) {
1343 			uint32_t prop_id;
1344 			uint64_t prop_value;
1345 			struct drm_property *prop;
1346 
1347 			if (get_user(prop_id, props_ptr + copied_props)) {
1348 				drm_mode_object_put(obj);
1349 				ret = -EFAULT;
1350 				goto out;
1351 			}
1352 
1353 			prop = drm_mode_obj_find_prop_id(obj, prop_id);
1354 			if (!prop) {
1355 				drm_mode_object_put(obj);
1356 				ret = -ENOENT;
1357 				goto out;
1358 			}
1359 
1360 			if (copy_from_user(&prop_value,
1361 					   prop_values_ptr + copied_props,
1362 					   sizeof(prop_value))) {
1363 				drm_mode_object_put(obj);
1364 				ret = -EFAULT;
1365 				goto out;
1366 			}
1367 
1368 			ret = drm_atomic_set_property(state, obj, prop,
1369 						      prop_value);
1370 			if (ret) {
1371 				drm_mode_object_put(obj);
1372 				goto out;
1373 			}
1374 
1375 			copied_props++;
1376 		}
1377 
1378 		drm_mode_object_put(obj);
1379 	}
1380 
1381 	ret = prepare_signaling(dev, state, arg, file_priv, &fence_state,
1382 				&num_fences);
1383 	if (ret)
1384 		goto out;
1385 
1386 	if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) {
1387 		ret = drm_atomic_check_only(state);
1388 	} else if (arg->flags & DRM_MODE_ATOMIC_NONBLOCK) {
1389 		ret = drm_atomic_nonblocking_commit(state);
1390 	} else {
1391 		if (unlikely(drm_debug & DRM_UT_STATE))
1392 			drm_atomic_print_state(state);
1393 
1394 		ret = drm_atomic_commit(state);
1395 	}
1396 
1397 out:
1398 	complete_signaling(dev, state, fence_state, num_fences, !ret);
1399 
1400 	if (ret == -EDEADLK) {
1401 		drm_atomic_state_clear(state);
1402 		ret = drm_modeset_backoff(&ctx);
1403 		if (!ret)
1404 			goto retry;
1405 	}
1406 
1407 	drm_atomic_state_put(state);
1408 
1409 	drm_modeset_drop_locks(&ctx);
1410 	drm_modeset_acquire_fini(&ctx);
1411 
1412 	return ret;
1413 }
1414