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