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