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