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