xref: /linux/drivers/gpu/drm/drm_atomic_uapi.c (revision 0c86b42439b6c11d758b3392a21117934fef00c1)
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 <drm/drm_atomic.h>
31 #include <drm/drm_atomic_helper.h>
32 #include <drm/drm_atomic_uapi.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 (%s, %pe): " DRM_MODE_FMT "\n",
150 				       crtc->base.id, crtc->name,
151 				       drm_get_mode_status_name(state->mode.status),
152 				       ERR_PTR(ret), DRM_MODE_ARG(&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 drm_atomic_crtc_set_property(struct drm_crtc *crtc,
367 		struct drm_crtc_state *state, struct drm_property *property,
368 		uint64_t val)
369 {
370 	struct drm_device *dev = crtc->dev;
371 	struct drm_mode_config *config = &dev->mode_config;
372 	bool replaced = false;
373 	int ret;
374 
375 	if (property == config->prop_active)
376 		state->active = val;
377 	else if (property == config->prop_mode_id) {
378 		struct drm_property_blob *mode =
379 			drm_property_lookup_blob(dev, val);
380 		ret = drm_atomic_set_mode_prop_for_crtc(state, mode);
381 		drm_property_blob_put(mode);
382 		return ret;
383 	} else if (property == config->prop_vrr_enabled) {
384 		state->vrr_enabled = val;
385 	} else if (property == config->degamma_lut_property) {
386 		ret = drm_property_replace_blob_from_id(dev,
387 					&state->degamma_lut,
388 					val,
389 					-1, sizeof(struct drm_color_lut),
390 					&replaced);
391 		state->color_mgmt_changed |= replaced;
392 		return ret;
393 	} else if (property == config->ctm_property) {
394 		ret = drm_property_replace_blob_from_id(dev,
395 					&state->ctm,
396 					val,
397 					sizeof(struct drm_color_ctm), -1,
398 					&replaced);
399 		state->color_mgmt_changed |= replaced;
400 		return ret;
401 	} else if (property == config->gamma_lut_property) {
402 		ret = drm_property_replace_blob_from_id(dev,
403 					&state->gamma_lut,
404 					val,
405 					-1, sizeof(struct drm_color_lut),
406 					&replaced);
407 		state->color_mgmt_changed |= replaced;
408 		return ret;
409 	} else if (property == config->prop_out_fence_ptr) {
410 		s32 __user *fence_ptr = u64_to_user_ptr(val);
411 
412 		if (!fence_ptr)
413 			return 0;
414 
415 		if (put_user(-1, fence_ptr))
416 			return -EFAULT;
417 
418 		set_out_fence_for_crtc(state->state, crtc, fence_ptr);
419 	} else if (property == crtc->scaling_filter_property) {
420 		state->scaling_filter = val;
421 	} else if (crtc->funcs->atomic_set_property) {
422 		return crtc->funcs->atomic_set_property(crtc, state, property, val);
423 	} else {
424 		drm_dbg_atomic(crtc->dev,
425 			       "[CRTC:%d:%s] unknown property [PROP:%d:%s]\n",
426 			       crtc->base.id, crtc->name,
427 			       property->base.id, property->name);
428 		return -EINVAL;
429 	}
430 
431 	return 0;
432 }
433 
434 static int
435 drm_atomic_crtc_get_property(struct drm_crtc *crtc,
436 		const struct drm_crtc_state *state,
437 		struct drm_property *property, uint64_t *val)
438 {
439 	struct drm_device *dev = crtc->dev;
440 	struct drm_mode_config *config = &dev->mode_config;
441 
442 	if (property == config->prop_active)
443 		*val = drm_atomic_crtc_effectively_active(state);
444 	else if (property == config->prop_mode_id)
445 		*val = (state->mode_blob) ? state->mode_blob->base.id : 0;
446 	else if (property == config->prop_vrr_enabled)
447 		*val = state->vrr_enabled;
448 	else if (property == config->degamma_lut_property)
449 		*val = (state->degamma_lut) ? state->degamma_lut->base.id : 0;
450 	else if (property == config->ctm_property)
451 		*val = (state->ctm) ? state->ctm->base.id : 0;
452 	else if (property == config->gamma_lut_property)
453 		*val = (state->gamma_lut) ? state->gamma_lut->base.id : 0;
454 	else if (property == config->prop_out_fence_ptr)
455 		*val = 0;
456 	else if (property == crtc->scaling_filter_property)
457 		*val = state->scaling_filter;
458 	else if (crtc->funcs->atomic_get_property)
459 		return crtc->funcs->atomic_get_property(crtc, state, property, val);
460 	else {
461 		drm_dbg_atomic(dev,
462 			       "[CRTC:%d:%s] unknown property [PROP:%d:%s]\n",
463 			       crtc->base.id, crtc->name,
464 			       property->base.id, property->name);
465 		return -EINVAL;
466 	}
467 
468 	return 0;
469 }
470 
471 static int drm_atomic_plane_set_property(struct drm_plane *plane,
472 		struct drm_plane_state *state, struct drm_file *file_priv,
473 		struct drm_property *property, uint64_t val)
474 {
475 	struct drm_device *dev = plane->dev;
476 	struct drm_mode_config *config = &dev->mode_config;
477 	bool replaced = false;
478 	int ret;
479 
480 	if (property == config->prop_fb_id) {
481 		struct drm_framebuffer *fb;
482 
483 		fb = drm_framebuffer_lookup(dev, file_priv, val);
484 		drm_atomic_set_fb_for_plane(state, fb);
485 		if (fb)
486 			drm_framebuffer_put(fb);
487 	} else if (property == config->prop_in_fence_fd) {
488 		if (state->fence)
489 			return -EINVAL;
490 
491 		if (U642I64(val) == -1)
492 			return 0;
493 
494 		state->fence = sync_file_get_fence(val);
495 		if (!state->fence)
496 			return -EINVAL;
497 
498 	} else if (property == config->prop_crtc_id) {
499 		struct drm_crtc *crtc = drm_crtc_find(dev, file_priv, val);
500 
501 		if (val && !crtc) {
502 			drm_dbg_atomic(dev,
503 				       "[PROP:%d:%s] cannot find CRTC with ID %llu\n",
504 				       property->base.id, property->name, val);
505 			return -EACCES;
506 		}
507 		return drm_atomic_set_crtc_for_plane(state, crtc);
508 	} else if (property == config->prop_crtc_x) {
509 		state->crtc_x = U642I64(val);
510 	} else if (property == config->prop_crtc_y) {
511 		state->crtc_y = U642I64(val);
512 	} else if (property == config->prop_crtc_w) {
513 		state->crtc_w = val;
514 	} else if (property == config->prop_crtc_h) {
515 		state->crtc_h = val;
516 	} else if (property == config->prop_src_x) {
517 		state->src_x = val;
518 	} else if (property == config->prop_src_y) {
519 		state->src_y = val;
520 	} else if (property == config->prop_src_w) {
521 		state->src_w = val;
522 	} else if (property == config->prop_src_h) {
523 		state->src_h = val;
524 	} else if (property == plane->alpha_property) {
525 		state->alpha = val;
526 	} else if (property == plane->blend_mode_property) {
527 		state->pixel_blend_mode = val;
528 	} else if (property == plane->rotation_property) {
529 		if (!is_power_of_2(val & DRM_MODE_ROTATE_MASK)) {
530 			drm_dbg_atomic(plane->dev,
531 				       "[PLANE:%d:%s] bad rotation bitmask: 0x%llx\n",
532 				       plane->base.id, plane->name, val);
533 			return -EINVAL;
534 		}
535 		state->rotation = val;
536 	} else if (property == plane->zpos_property) {
537 		state->zpos = val;
538 	} else if (property == plane->color_encoding_property) {
539 		state->color_encoding = val;
540 	} else if (property == plane->color_range_property) {
541 		state->color_range = val;
542 	} else if (property == config->prop_fb_damage_clips) {
543 		ret = drm_property_replace_blob_from_id(dev,
544 					&state->fb_damage_clips,
545 					val,
546 					-1,
547 					sizeof(struct drm_mode_rect),
548 					&replaced);
549 		return ret;
550 	} else if (property == plane->scaling_filter_property) {
551 		state->scaling_filter = val;
552 	} else if (plane->funcs->atomic_set_property) {
553 		return plane->funcs->atomic_set_property(plane, state,
554 				property, val);
555 	} else if (property == plane->hotspot_x_property) {
556 		if (plane->type != DRM_PLANE_TYPE_CURSOR) {
557 			drm_dbg_atomic(plane->dev,
558 				       "[PLANE:%d:%s] is not a cursor plane: 0x%llx\n",
559 				       plane->base.id, plane->name, val);
560 			return -EINVAL;
561 		}
562 		state->hotspot_x = val;
563 	} else if (property == plane->hotspot_y_property) {
564 		if (plane->type != DRM_PLANE_TYPE_CURSOR) {
565 			drm_dbg_atomic(plane->dev,
566 				       "[PLANE:%d:%s] is not a cursor plane: 0x%llx\n",
567 				       plane->base.id, plane->name, val);
568 			return -EINVAL;
569 		}
570 		state->hotspot_y = val;
571 	} else {
572 		drm_dbg_atomic(plane->dev,
573 			       "[PLANE:%d:%s] unknown property [PROP:%d:%s]\n",
574 			       plane->base.id, plane->name,
575 			       property->base.id, property->name);
576 		return -EINVAL;
577 	}
578 
579 	return 0;
580 }
581 
582 static int
583 drm_atomic_plane_get_property(struct drm_plane *plane,
584 		const struct drm_plane_state *state,
585 		struct drm_property *property, uint64_t *val)
586 {
587 	struct drm_device *dev = plane->dev;
588 	struct drm_mode_config *config = &dev->mode_config;
589 
590 	if (property == config->prop_fb_id) {
591 		*val = (state->fb) ? state->fb->base.id : 0;
592 	} else if (property == config->prop_in_fence_fd) {
593 		*val = -1;
594 	} else if (property == config->prop_crtc_id) {
595 		*val = (state->crtc) ? state->crtc->base.id : 0;
596 	} else if (property == config->prop_crtc_x) {
597 		*val = I642U64(state->crtc_x);
598 	} else if (property == config->prop_crtc_y) {
599 		*val = I642U64(state->crtc_y);
600 	} else if (property == config->prop_crtc_w) {
601 		*val = state->crtc_w;
602 	} else if (property == config->prop_crtc_h) {
603 		*val = state->crtc_h;
604 	} else if (property == config->prop_src_x) {
605 		*val = state->src_x;
606 	} else if (property == config->prop_src_y) {
607 		*val = state->src_y;
608 	} else if (property == config->prop_src_w) {
609 		*val = state->src_w;
610 	} else if (property == config->prop_src_h) {
611 		*val = state->src_h;
612 	} else if (property == plane->alpha_property) {
613 		*val = state->alpha;
614 	} else if (property == plane->blend_mode_property) {
615 		*val = state->pixel_blend_mode;
616 	} else if (property == plane->rotation_property) {
617 		*val = state->rotation;
618 	} else if (property == plane->zpos_property) {
619 		*val = state->zpos;
620 	} else if (property == plane->color_encoding_property) {
621 		*val = state->color_encoding;
622 	} else if (property == plane->color_range_property) {
623 		*val = state->color_range;
624 	} else if (property == config->prop_fb_damage_clips) {
625 		*val = (state->fb_damage_clips) ?
626 			state->fb_damage_clips->base.id : 0;
627 	} else if (property == plane->scaling_filter_property) {
628 		*val = state->scaling_filter;
629 	} else if (plane->funcs->atomic_get_property) {
630 		return plane->funcs->atomic_get_property(plane, state, property, val);
631 	} else if (property == plane->hotspot_x_property) {
632 		*val = state->hotspot_x;
633 	} else if (property == plane->hotspot_y_property) {
634 		*val = state->hotspot_y;
635 	} else {
636 		drm_dbg_atomic(dev,
637 			       "[PLANE:%d:%s] unknown property [PROP:%d:%s]\n",
638 			       plane->base.id, plane->name,
639 			       property->base.id, property->name);
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 			drm_dbg_atomic(dev,
683 				       "[PROP:%d:%s] cannot find CRTC with ID %llu\n",
684 				       property->base.id, property->name, val);
685 			return -EACCES;
686 		}
687 		return drm_atomic_set_crtc_for_connector(state, crtc);
688 	} else if (property == config->dpms_property) {
689 		/* setting DPMS property requires special handling, which
690 		 * is done in legacy setprop path for us.  Disallow (for
691 		 * now?) atomic writes to DPMS property:
692 		 */
693 		drm_dbg_atomic(dev,
694 			       "legacy [PROP:%d:%s] can only be set via legacy uAPI\n",
695 			       property->base.id, property->name);
696 		return -EINVAL;
697 	} else if (property == config->tv_select_subconnector_property) {
698 		state->tv.select_subconnector = val;
699 	} else if (property == config->tv_subconnector_property) {
700 		state->tv.subconnector = val;
701 	} else if (property == config->tv_left_margin_property) {
702 		state->tv.margins.left = val;
703 	} else if (property == config->tv_right_margin_property) {
704 		state->tv.margins.right = val;
705 	} else if (property == config->tv_top_margin_property) {
706 		state->tv.margins.top = val;
707 	} else if (property == config->tv_bottom_margin_property) {
708 		state->tv.margins.bottom = val;
709 	} else if (property == config->legacy_tv_mode_property) {
710 		state->tv.legacy_mode = val;
711 	} else if (property == config->tv_mode_property) {
712 		state->tv.mode = val;
713 	} else if (property == config->tv_brightness_property) {
714 		state->tv.brightness = val;
715 	} else if (property == config->tv_contrast_property) {
716 		state->tv.contrast = val;
717 	} else if (property == config->tv_flicker_reduction_property) {
718 		state->tv.flicker_reduction = val;
719 	} else if (property == config->tv_overscan_property) {
720 		state->tv.overscan = val;
721 	} else if (property == config->tv_saturation_property) {
722 		state->tv.saturation = val;
723 	} else if (property == config->tv_hue_property) {
724 		state->tv.hue = val;
725 	} else if (property == config->link_status_property) {
726 		/* Never downgrade from GOOD to BAD on userspace's request here,
727 		 * only hw issues can do that.
728 		 *
729 		 * For an atomic property the userspace doesn't need to be able
730 		 * to understand all the properties, but needs to be able to
731 		 * restore the state it wants on VT switch. So if the userspace
732 		 * tries to change the link_status from GOOD to BAD, driver
733 		 * silently rejects it and returns a 0. This prevents userspace
734 		 * from accidentally breaking  the display when it restores the
735 		 * state.
736 		 */
737 		if (state->link_status != DRM_LINK_STATUS_GOOD)
738 			state->link_status = val;
739 	} else if (property == config->hdr_output_metadata_property) {
740 		ret = drm_property_replace_blob_from_id(dev,
741 				&state->hdr_output_metadata,
742 				val,
743 				sizeof(struct hdr_output_metadata), -1,
744 				&replaced);
745 		return ret;
746 	} else if (property == config->aspect_ratio_property) {
747 		state->picture_aspect_ratio = val;
748 	} else if (property == config->content_type_property) {
749 		state->content_type = val;
750 	} else if (property == connector->scaling_mode_property) {
751 		state->scaling_mode = val;
752 	} else if (property == config->content_protection_property) {
753 		if (val == DRM_MODE_CONTENT_PROTECTION_ENABLED) {
754 			drm_dbg_kms(dev, "only drivers can set CP Enabled\n");
755 			return -EINVAL;
756 		}
757 		state->content_protection = val;
758 	} else if (property == config->hdcp_content_type_property) {
759 		state->hdcp_content_type = val;
760 	} else if (property == connector->colorspace_property) {
761 		state->colorspace = val;
762 	} else if (property == config->writeback_fb_id_property) {
763 		struct drm_framebuffer *fb;
764 		int ret;
765 
766 		fb = drm_framebuffer_lookup(dev, file_priv, val);
767 		ret = drm_atomic_set_writeback_fb_for_connector(state, fb);
768 		if (fb)
769 			drm_framebuffer_put(fb);
770 		return ret;
771 	} else if (property == config->writeback_out_fence_ptr_property) {
772 		s32 __user *fence_ptr = u64_to_user_ptr(val);
773 
774 		return set_out_fence_for_connector(state->state, connector,
775 						   fence_ptr);
776 	} else if (property == connector->max_bpc_property) {
777 		state->max_requested_bpc = val;
778 	} else if (property == connector->privacy_screen_sw_state_property) {
779 		state->privacy_screen_sw_state = val;
780 	} else if (property == connector->broadcast_rgb_property) {
781 		state->hdmi.broadcast_rgb = val;
782 	} else if (connector->funcs->atomic_set_property) {
783 		return connector->funcs->atomic_set_property(connector,
784 				state, property, val);
785 	} else {
786 		drm_dbg_atomic(connector->dev,
787 			       "[CONNECTOR:%d:%s] unknown property [PROP:%d:%s]\n",
788 			       connector->base.id, connector->name,
789 			       property->base.id, property->name);
790 		return -EINVAL;
791 	}
792 
793 	return 0;
794 }
795 
796 static int
797 drm_atomic_connector_get_property(struct drm_connector *connector,
798 		const struct drm_connector_state *state,
799 		struct drm_property *property, uint64_t *val)
800 {
801 	struct drm_device *dev = connector->dev;
802 	struct drm_mode_config *config = &dev->mode_config;
803 
804 	if (property == config->prop_crtc_id) {
805 		*val = (state->crtc) ? state->crtc->base.id : 0;
806 	} else if (property == config->dpms_property) {
807 		if (state->crtc && state->crtc->state->self_refresh_active)
808 			*val = DRM_MODE_DPMS_ON;
809 		else
810 			*val = connector->dpms;
811 	} else if (property == config->tv_select_subconnector_property) {
812 		*val = state->tv.select_subconnector;
813 	} else if (property == config->tv_subconnector_property) {
814 		*val = state->tv.subconnector;
815 	} else if (property == config->tv_left_margin_property) {
816 		*val = state->tv.margins.left;
817 	} else if (property == config->tv_right_margin_property) {
818 		*val = state->tv.margins.right;
819 	} else if (property == config->tv_top_margin_property) {
820 		*val = state->tv.margins.top;
821 	} else if (property == config->tv_bottom_margin_property) {
822 		*val = state->tv.margins.bottom;
823 	} else if (property == config->legacy_tv_mode_property) {
824 		*val = state->tv.legacy_mode;
825 	} else if (property == config->tv_mode_property) {
826 		*val = state->tv.mode;
827 	} else if (property == config->tv_brightness_property) {
828 		*val = state->tv.brightness;
829 	} else if (property == config->tv_contrast_property) {
830 		*val = state->tv.contrast;
831 	} else if (property == config->tv_flicker_reduction_property) {
832 		*val = state->tv.flicker_reduction;
833 	} else if (property == config->tv_overscan_property) {
834 		*val = state->tv.overscan;
835 	} else if (property == config->tv_saturation_property) {
836 		*val = state->tv.saturation;
837 	} else if (property == config->tv_hue_property) {
838 		*val = state->tv.hue;
839 	} else if (property == config->link_status_property) {
840 		*val = state->link_status;
841 	} else if (property == config->aspect_ratio_property) {
842 		*val = state->picture_aspect_ratio;
843 	} else if (property == config->content_type_property) {
844 		*val = state->content_type;
845 	} else if (property == connector->colorspace_property) {
846 		*val = state->colorspace;
847 	} else if (property == connector->scaling_mode_property) {
848 		*val = state->scaling_mode;
849 	} else if (property == config->hdr_output_metadata_property) {
850 		*val = state->hdr_output_metadata ?
851 			state->hdr_output_metadata->base.id : 0;
852 	} else if (property == config->content_protection_property) {
853 		*val = state->content_protection;
854 	} else if (property == config->hdcp_content_type_property) {
855 		*val = state->hdcp_content_type;
856 	} else if (property == config->writeback_fb_id_property) {
857 		/* Writeback framebuffer is one-shot, write and forget */
858 		*val = 0;
859 	} else if (property == config->writeback_out_fence_ptr_property) {
860 		*val = 0;
861 	} else if (property == connector->max_bpc_property) {
862 		*val = state->max_requested_bpc;
863 	} else if (property == connector->privacy_screen_sw_state_property) {
864 		*val = state->privacy_screen_sw_state;
865 	} else if (property == connector->broadcast_rgb_property) {
866 		*val = state->hdmi.broadcast_rgb;
867 	} else if (connector->funcs->atomic_get_property) {
868 		return connector->funcs->atomic_get_property(connector,
869 				state, property, val);
870 	} else {
871 		drm_dbg_atomic(dev,
872 			       "[CONNECTOR:%d:%s] unknown property [PROP:%d:%s]\n",
873 			       connector->base.id, connector->name,
874 			       property->base.id, property->name);
875 		return -EINVAL;
876 	}
877 
878 	return 0;
879 }
880 
881 int drm_atomic_get_property(struct drm_mode_object *obj,
882 		struct drm_property *property, uint64_t *val)
883 {
884 	struct drm_device *dev = property->dev;
885 	int ret;
886 
887 	switch (obj->type) {
888 	case DRM_MODE_OBJECT_CONNECTOR: {
889 		struct drm_connector *connector = obj_to_connector(obj);
890 
891 		WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
892 		ret = drm_atomic_connector_get_property(connector,
893 				connector->state, property, val);
894 		break;
895 	}
896 	case DRM_MODE_OBJECT_CRTC: {
897 		struct drm_crtc *crtc = obj_to_crtc(obj);
898 
899 		WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
900 		ret = drm_atomic_crtc_get_property(crtc,
901 				crtc->state, property, val);
902 		break;
903 	}
904 	case DRM_MODE_OBJECT_PLANE: {
905 		struct drm_plane *plane = obj_to_plane(obj);
906 
907 		WARN_ON(!drm_modeset_is_locked(&plane->mutex));
908 		ret = drm_atomic_plane_get_property(plane,
909 				plane->state, property, val);
910 		break;
911 	}
912 	default:
913 		drm_dbg_atomic(dev, "[OBJECT:%d] has no properties\n", obj->id);
914 		ret = -EINVAL;
915 		break;
916 	}
917 
918 	return ret;
919 }
920 
921 /*
922  * The big monster ioctl
923  */
924 
925 static struct drm_pending_vblank_event *create_vblank_event(
926 		struct drm_crtc *crtc, uint64_t user_data)
927 {
928 	struct drm_pending_vblank_event *e = NULL;
929 
930 	e = kzalloc(sizeof *e, GFP_KERNEL);
931 	if (!e)
932 		return NULL;
933 
934 	e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
935 	e->event.base.length = sizeof(e->event);
936 	e->event.vbl.crtc_id = crtc->base.id;
937 	e->event.vbl.user_data = user_data;
938 
939 	return e;
940 }
941 
942 int drm_atomic_connector_commit_dpms(struct drm_atomic_state *state,
943 				     struct drm_connector *connector,
944 				     int mode)
945 {
946 	struct drm_connector *tmp_connector;
947 	struct drm_connector_state *new_conn_state;
948 	struct drm_crtc *crtc;
949 	struct drm_crtc_state *crtc_state;
950 	int i, ret, old_mode = connector->dpms;
951 	bool active = false;
952 
953 	ret = drm_modeset_lock(&state->dev->mode_config.connection_mutex,
954 			       state->acquire_ctx);
955 	if (ret)
956 		return ret;
957 
958 	if (mode != DRM_MODE_DPMS_ON)
959 		mode = DRM_MODE_DPMS_OFF;
960 
961 	if (connector->dpms == mode)
962 		goto out;
963 
964 	connector->dpms = mode;
965 
966 	crtc = connector->state->crtc;
967 	if (!crtc)
968 		goto out;
969 	ret = drm_atomic_add_affected_connectors(state, crtc);
970 	if (ret)
971 		goto out;
972 
973 	crtc_state = drm_atomic_get_crtc_state(state, crtc);
974 	if (IS_ERR(crtc_state)) {
975 		ret = PTR_ERR(crtc_state);
976 		goto out;
977 	}
978 
979 	for_each_new_connector_in_state(state, tmp_connector, new_conn_state, i) {
980 		if (new_conn_state->crtc != crtc)
981 			continue;
982 		if (tmp_connector->dpms == DRM_MODE_DPMS_ON) {
983 			active = true;
984 			break;
985 		}
986 	}
987 
988 	crtc_state->active = active;
989 	ret = drm_atomic_commit(state);
990 out:
991 	if (ret != 0)
992 		connector->dpms = old_mode;
993 	return ret;
994 }
995 
996 static int drm_atomic_check_prop_changes(int ret, uint64_t old_val, uint64_t prop_value,
997 					 struct drm_property *prop)
998 {
999 	if (ret != 0 || old_val != prop_value) {
1000 		drm_dbg_atomic(prop->dev,
1001 			       "[PROP:%d:%s] No prop can be changed during async flip\n",
1002 			       prop->base.id, prop->name);
1003 		return -EINVAL;
1004 	}
1005 
1006 	return 0;
1007 }
1008 
1009 int drm_atomic_set_property(struct drm_atomic_state *state,
1010 			    struct drm_file *file_priv,
1011 			    struct drm_mode_object *obj,
1012 			    struct drm_property *prop,
1013 			    u64 prop_value,
1014 			    bool async_flip)
1015 {
1016 	struct drm_mode_object *ref;
1017 	u64 old_val;
1018 	int ret;
1019 
1020 	if (!drm_property_change_valid_get(prop, prop_value, &ref))
1021 		return -EINVAL;
1022 
1023 	switch (obj->type) {
1024 	case DRM_MODE_OBJECT_CONNECTOR: {
1025 		struct drm_connector *connector = obj_to_connector(obj);
1026 		struct drm_connector_state *connector_state;
1027 
1028 		connector_state = drm_atomic_get_connector_state(state, connector);
1029 		if (IS_ERR(connector_state)) {
1030 			ret = PTR_ERR(connector_state);
1031 			break;
1032 		}
1033 
1034 		if (async_flip) {
1035 			ret = drm_atomic_connector_get_property(connector, connector_state,
1036 								prop, &old_val);
1037 			ret = drm_atomic_check_prop_changes(ret, old_val, prop_value, prop);
1038 			break;
1039 		}
1040 
1041 		ret = drm_atomic_connector_set_property(connector,
1042 				connector_state, file_priv,
1043 				prop, prop_value);
1044 		break;
1045 	}
1046 	case DRM_MODE_OBJECT_CRTC: {
1047 		struct drm_crtc *crtc = obj_to_crtc(obj);
1048 		struct drm_crtc_state *crtc_state;
1049 
1050 		crtc_state = drm_atomic_get_crtc_state(state, crtc);
1051 		if (IS_ERR(crtc_state)) {
1052 			ret = PTR_ERR(crtc_state);
1053 			break;
1054 		}
1055 
1056 		if (async_flip) {
1057 			ret = drm_atomic_crtc_get_property(crtc, crtc_state,
1058 							   prop, &old_val);
1059 			ret = drm_atomic_check_prop_changes(ret, old_val, prop_value, prop);
1060 			break;
1061 		}
1062 
1063 		ret = drm_atomic_crtc_set_property(crtc,
1064 				crtc_state, prop, prop_value);
1065 		break;
1066 	}
1067 	case DRM_MODE_OBJECT_PLANE: {
1068 		struct drm_plane *plane = obj_to_plane(obj);
1069 		struct drm_plane_state *plane_state;
1070 		struct drm_mode_config *config = &plane->dev->mode_config;
1071 		const struct drm_plane_helper_funcs *plane_funcs = plane->helper_private;
1072 
1073 		plane_state = drm_atomic_get_plane_state(state, plane);
1074 		if (IS_ERR(plane_state)) {
1075 			ret = PTR_ERR(plane_state);
1076 			break;
1077 		}
1078 
1079 		if (async_flip) {
1080 			/* check if the prop does a nop change */
1081 			if ((prop != config->prop_fb_id &&
1082 			     prop != config->prop_in_fence_fd &&
1083 			     prop != config->prop_fb_damage_clips)) {
1084 				ret = drm_atomic_plane_get_property(plane, plane_state,
1085 								    prop, &old_val);
1086 				ret = drm_atomic_check_prop_changes(ret, old_val, prop_value, prop);
1087 			}
1088 
1089 			/* ask the driver if this non-primary plane is supported */
1090 			if (plane->type != DRM_PLANE_TYPE_PRIMARY) {
1091 				ret = -EINVAL;
1092 
1093 				if (plane_funcs && plane_funcs->atomic_async_check)
1094 					ret = plane_funcs->atomic_async_check(plane, state, true);
1095 
1096 				if (ret) {
1097 					drm_dbg_atomic(prop->dev,
1098 						       "[PLANE:%d:%s] does not support async flips\n",
1099 						       obj->id, plane->name);
1100 					break;
1101 				}
1102 			}
1103 		}
1104 
1105 		ret = drm_atomic_plane_set_property(plane,
1106 				plane_state, file_priv,
1107 				prop, prop_value);
1108 		break;
1109 	}
1110 	default:
1111 		drm_dbg_atomic(prop->dev, "[OBJECT:%d] has no properties\n", obj->id);
1112 		ret = -EINVAL;
1113 		break;
1114 	}
1115 
1116 	drm_property_change_valid_put(prop, ref);
1117 	return ret;
1118 }
1119 
1120 /**
1121  * DOC: explicit fencing properties
1122  *
1123  * Explicit fencing allows userspace to control the buffer synchronization
1124  * between devices. A Fence or a group of fences are transferred to/from
1125  * userspace using Sync File fds and there are two DRM properties for that.
1126  * IN_FENCE_FD on each DRM Plane to send fences to the kernel and
1127  * OUT_FENCE_PTR on each DRM CRTC to receive fences from the kernel.
1128  *
1129  * As a contrast, with implicit fencing the kernel keeps track of any
1130  * ongoing rendering, and automatically ensures that the atomic update waits
1131  * for any pending rendering to complete. This is usually tracked in &struct
1132  * dma_resv which can also contain mandatory kernel fences. Implicit syncing
1133  * is how Linux traditionally worked (e.g. DRI2/3 on X.org), whereas explicit
1134  * fencing is what Android wants.
1135  *
1136  * "IN_FENCE_FD”:
1137  *	Use this property to pass a fence that DRM should wait on before
1138  *	proceeding with the Atomic Commit request and show the framebuffer for
1139  *	the plane on the screen. The fence can be either a normal fence or a
1140  *	merged one, the sync_file framework will handle both cases and use a
1141  *	fence_array if a merged fence is received. Passing -1 here means no
1142  *	fences to wait on.
1143  *
1144  *	If the Atomic Commit request has the DRM_MODE_ATOMIC_TEST_ONLY flag
1145  *	it will only check if the Sync File is a valid one.
1146  *
1147  *	On the driver side the fence is stored on the @fence parameter of
1148  *	&struct drm_plane_state. Drivers which also support implicit fencing
1149  *	should extract the implicit fence using drm_gem_plane_helper_prepare_fb(),
1150  *	to make sure there's consistent behaviour between drivers in precedence
1151  *	of implicit vs. explicit fencing.
1152  *
1153  * "OUT_FENCE_PTR”:
1154  *	Use this property to pass a file descriptor pointer to DRM. Once the
1155  *	Atomic Commit request call returns OUT_FENCE_PTR will be filled with
1156  *	the file descriptor number of a Sync File. This Sync File contains the
1157  *	CRTC fence that will be signaled when all framebuffers present on the
1158  *	Atomic Commit * request for that given CRTC are scanned out on the
1159  *	screen.
1160  *
1161  *	The Atomic Commit request fails if a invalid pointer is passed. If the
1162  *	Atomic Commit request fails for any other reason the out fence fd
1163  *	returned will be -1. On a Atomic Commit with the
1164  *	DRM_MODE_ATOMIC_TEST_ONLY flag the out fence will also be set to -1.
1165  *
1166  *	Note that out-fences don't have a special interface to drivers and are
1167  *	internally represented by a &struct drm_pending_vblank_event in struct
1168  *	&drm_crtc_state, which is also used by the nonblocking atomic commit
1169  *	helpers and for the DRM event handling for existing userspace.
1170  */
1171 
1172 struct drm_out_fence_state {
1173 	s32 __user *out_fence_ptr;
1174 	struct sync_file *sync_file;
1175 	int fd;
1176 };
1177 
1178 static int setup_out_fence(struct drm_out_fence_state *fence_state,
1179 			   struct dma_fence *fence)
1180 {
1181 	fence_state->fd = get_unused_fd_flags(O_CLOEXEC);
1182 	if (fence_state->fd < 0)
1183 		return fence_state->fd;
1184 
1185 	if (put_user(fence_state->fd, fence_state->out_fence_ptr))
1186 		return -EFAULT;
1187 
1188 	fence_state->sync_file = sync_file_create(fence);
1189 	if (!fence_state->sync_file)
1190 		return -ENOMEM;
1191 
1192 	return 0;
1193 }
1194 
1195 static int prepare_signaling(struct drm_device *dev,
1196 				  struct drm_atomic_state *state,
1197 				  struct drm_mode_atomic *arg,
1198 				  struct drm_file *file_priv,
1199 				  struct drm_out_fence_state **fence_state,
1200 				  unsigned int *num_fences)
1201 {
1202 	struct drm_crtc *crtc;
1203 	struct drm_crtc_state *crtc_state;
1204 	struct drm_connector *conn;
1205 	struct drm_connector_state *conn_state;
1206 	int i, c = 0, ret;
1207 
1208 	if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)
1209 		return 0;
1210 
1211 	for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
1212 		s32 __user *fence_ptr;
1213 
1214 		fence_ptr = get_out_fence_for_crtc(crtc_state->state, crtc);
1215 
1216 		if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT || fence_ptr) {
1217 			struct drm_pending_vblank_event *e;
1218 
1219 			e = create_vblank_event(crtc, arg->user_data);
1220 			if (!e)
1221 				return -ENOMEM;
1222 
1223 			crtc_state->event = e;
1224 		}
1225 
1226 		if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
1227 			struct drm_pending_vblank_event *e = crtc_state->event;
1228 
1229 			if (!file_priv)
1230 				continue;
1231 
1232 			ret = drm_event_reserve_init(dev, file_priv, &e->base,
1233 						     &e->event.base);
1234 			if (ret) {
1235 				kfree(e);
1236 				crtc_state->event = NULL;
1237 				return ret;
1238 			}
1239 		}
1240 
1241 		if (fence_ptr) {
1242 			struct dma_fence *fence;
1243 			struct drm_out_fence_state *f;
1244 
1245 			f = krealloc(*fence_state, sizeof(**fence_state) *
1246 				     (*num_fences + 1), GFP_KERNEL);
1247 			if (!f)
1248 				return -ENOMEM;
1249 
1250 			memset(&f[*num_fences], 0, sizeof(*f));
1251 
1252 			f[*num_fences].out_fence_ptr = fence_ptr;
1253 			*fence_state = f;
1254 
1255 			fence = drm_crtc_create_fence(crtc);
1256 			if (!fence)
1257 				return -ENOMEM;
1258 
1259 			ret = setup_out_fence(&f[(*num_fences)++], fence);
1260 			if (ret) {
1261 				dma_fence_put(fence);
1262 				return ret;
1263 			}
1264 
1265 			crtc_state->event->base.fence = fence;
1266 		}
1267 
1268 		c++;
1269 	}
1270 
1271 	for_each_new_connector_in_state(state, conn, conn_state, i) {
1272 		struct drm_writeback_connector *wb_conn;
1273 		struct drm_out_fence_state *f;
1274 		struct dma_fence *fence;
1275 		s32 __user *fence_ptr;
1276 
1277 		if (!conn_state->writeback_job)
1278 			continue;
1279 
1280 		fence_ptr = get_out_fence_for_connector(state, conn);
1281 		if (!fence_ptr)
1282 			continue;
1283 
1284 		f = krealloc(*fence_state, sizeof(**fence_state) *
1285 			     (*num_fences + 1), GFP_KERNEL);
1286 		if (!f)
1287 			return -ENOMEM;
1288 
1289 		memset(&f[*num_fences], 0, sizeof(*f));
1290 
1291 		f[*num_fences].out_fence_ptr = fence_ptr;
1292 		*fence_state = f;
1293 
1294 		wb_conn = drm_connector_to_writeback(conn);
1295 		fence = drm_writeback_get_out_fence(wb_conn);
1296 		if (!fence)
1297 			return -ENOMEM;
1298 
1299 		ret = setup_out_fence(&f[(*num_fences)++], fence);
1300 		if (ret) {
1301 			dma_fence_put(fence);
1302 			return ret;
1303 		}
1304 
1305 		conn_state->writeback_job->out_fence = fence;
1306 	}
1307 
1308 	/*
1309 	 * Having this flag means user mode pends on event which will never
1310 	 * reach due to lack of at least one CRTC for signaling
1311 	 */
1312 	if (c == 0 && (arg->flags & DRM_MODE_PAGE_FLIP_EVENT)) {
1313 		drm_dbg_atomic(dev, "need at least one CRTC for DRM_MODE_PAGE_FLIP_EVENT");
1314 		return -EINVAL;
1315 	}
1316 
1317 	return 0;
1318 }
1319 
1320 static void complete_signaling(struct drm_device *dev,
1321 			       struct drm_atomic_state *state,
1322 			       struct drm_out_fence_state *fence_state,
1323 			       unsigned int num_fences,
1324 			       bool install_fds)
1325 {
1326 	struct drm_crtc *crtc;
1327 	struct drm_crtc_state *crtc_state;
1328 	int i;
1329 
1330 	if (install_fds) {
1331 		for (i = 0; i < num_fences; i++)
1332 			fd_install(fence_state[i].fd,
1333 				   fence_state[i].sync_file->file);
1334 
1335 		kfree(fence_state);
1336 		return;
1337 	}
1338 
1339 	for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
1340 		struct drm_pending_vblank_event *event = crtc_state->event;
1341 		/*
1342 		 * Free the allocated event. drm_atomic_helper_setup_commit
1343 		 * can allocate an event too, so only free it if it's ours
1344 		 * to prevent a double free in drm_atomic_state_clear.
1345 		 */
1346 		if (event && (event->base.fence || event->base.file_priv)) {
1347 			drm_event_cancel_free(dev, &event->base);
1348 			crtc_state->event = NULL;
1349 		}
1350 	}
1351 
1352 	if (!fence_state)
1353 		return;
1354 
1355 	for (i = 0; i < num_fences; i++) {
1356 		if (fence_state[i].sync_file)
1357 			fput(fence_state[i].sync_file->file);
1358 		if (fence_state[i].fd >= 0)
1359 			put_unused_fd(fence_state[i].fd);
1360 
1361 		/* If this fails log error to the user */
1362 		if (fence_state[i].out_fence_ptr &&
1363 		    put_user(-1, fence_state[i].out_fence_ptr))
1364 			drm_dbg_atomic(dev, "Couldn't clear out_fence_ptr\n");
1365 	}
1366 
1367 	kfree(fence_state);
1368 }
1369 
1370 static void
1371 set_async_flip(struct drm_atomic_state *state)
1372 {
1373 	struct drm_crtc *crtc;
1374 	struct drm_crtc_state *crtc_state;
1375 	int i;
1376 
1377 	for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
1378 		crtc_state->async_flip = true;
1379 	}
1380 }
1381 
1382 int drm_mode_atomic_ioctl(struct drm_device *dev,
1383 			  void *data, struct drm_file *file_priv)
1384 {
1385 	struct drm_mode_atomic *arg = data;
1386 	uint32_t __user *objs_ptr = (uint32_t __user *)(unsigned long)(arg->objs_ptr);
1387 	uint32_t __user *count_props_ptr = (uint32_t __user *)(unsigned long)(arg->count_props_ptr);
1388 	uint32_t __user *props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
1389 	uint64_t __user *prop_values_ptr = (uint64_t __user *)(unsigned long)(arg->prop_values_ptr);
1390 	unsigned int copied_objs, copied_props;
1391 	struct drm_atomic_state *state;
1392 	struct drm_modeset_acquire_ctx ctx;
1393 	struct drm_out_fence_state *fence_state;
1394 	int ret = 0;
1395 	unsigned int i, j, num_fences;
1396 	bool async_flip = false;
1397 
1398 	/* disallow for drivers not supporting atomic: */
1399 	if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
1400 		return -EOPNOTSUPP;
1401 
1402 	/* disallow for userspace that has not enabled atomic cap (even
1403 	 * though this may be a bit overkill, since legacy userspace
1404 	 * wouldn't know how to call this ioctl)
1405 	 */
1406 	if (!file_priv->atomic) {
1407 		drm_dbg_atomic(dev,
1408 			       "commit failed: atomic cap not enabled\n");
1409 		return -EINVAL;
1410 	}
1411 
1412 	if (arg->flags & ~DRM_MODE_ATOMIC_FLAGS) {
1413 		drm_dbg_atomic(dev, "commit failed: invalid flag\n");
1414 		return -EINVAL;
1415 	}
1416 
1417 	if (arg->reserved) {
1418 		drm_dbg_atomic(dev, "commit failed: reserved field set\n");
1419 		return -EINVAL;
1420 	}
1421 
1422 	if (arg->flags & DRM_MODE_PAGE_FLIP_ASYNC) {
1423 		if (!dev->mode_config.async_page_flip) {
1424 			drm_dbg_atomic(dev,
1425 				       "commit failed: DRM_MODE_PAGE_FLIP_ASYNC not supported\n");
1426 			return -EINVAL;
1427 		}
1428 
1429 		async_flip = true;
1430 	}
1431 
1432 	/* can't test and expect an event at the same time. */
1433 	if ((arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) &&
1434 			(arg->flags & DRM_MODE_PAGE_FLIP_EVENT)) {
1435 		drm_dbg_atomic(dev,
1436 			       "commit failed: page-flip event requested with test-only commit\n");
1437 		return -EINVAL;
1438 	}
1439 
1440 	state = drm_atomic_state_alloc(dev);
1441 	if (!state)
1442 		return -ENOMEM;
1443 
1444 	drm_modeset_acquire_init(&ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE);
1445 	state->acquire_ctx = &ctx;
1446 	state->allow_modeset = !!(arg->flags & DRM_MODE_ATOMIC_ALLOW_MODESET);
1447 
1448 retry:
1449 	copied_objs = 0;
1450 	copied_props = 0;
1451 	fence_state = NULL;
1452 	num_fences = 0;
1453 
1454 	for (i = 0; i < arg->count_objs; i++) {
1455 		uint32_t obj_id, count_props;
1456 		struct drm_mode_object *obj;
1457 
1458 		if (get_user(obj_id, objs_ptr + copied_objs)) {
1459 			ret = -EFAULT;
1460 			goto out;
1461 		}
1462 
1463 		obj = drm_mode_object_find(dev, file_priv, obj_id, DRM_MODE_OBJECT_ANY);
1464 		if (!obj) {
1465 			drm_dbg_atomic(dev, "cannot find object ID %d", obj_id);
1466 			ret = -ENOENT;
1467 			goto out;
1468 		}
1469 
1470 		if (!obj->properties) {
1471 			drm_dbg_atomic(dev, "[OBJECT:%d] has no properties", obj_id);
1472 			drm_mode_object_put(obj);
1473 			ret = -ENOENT;
1474 			goto out;
1475 		}
1476 
1477 		if (get_user(count_props, count_props_ptr + copied_objs)) {
1478 			drm_mode_object_put(obj);
1479 			ret = -EFAULT;
1480 			goto out;
1481 		}
1482 
1483 		copied_objs++;
1484 
1485 		for (j = 0; j < count_props; j++) {
1486 			uint32_t prop_id;
1487 			uint64_t prop_value;
1488 			struct drm_property *prop;
1489 
1490 			if (get_user(prop_id, props_ptr + copied_props)) {
1491 				drm_mode_object_put(obj);
1492 				ret = -EFAULT;
1493 				goto out;
1494 			}
1495 
1496 			prop = drm_mode_obj_find_prop_id(obj, prop_id);
1497 			if (!prop) {
1498 				drm_dbg_atomic(dev,
1499 					       "[OBJECT:%d] cannot find property ID %d",
1500 					       obj_id, prop_id);
1501 				drm_mode_object_put(obj);
1502 				ret = -ENOENT;
1503 				goto out;
1504 			}
1505 
1506 			if (copy_from_user(&prop_value,
1507 					   prop_values_ptr + copied_props,
1508 					   sizeof(prop_value))) {
1509 				drm_mode_object_put(obj);
1510 				ret = -EFAULT;
1511 				goto out;
1512 			}
1513 
1514 			ret = drm_atomic_set_property(state, file_priv, obj,
1515 						      prop, prop_value, async_flip);
1516 			if (ret) {
1517 				drm_mode_object_put(obj);
1518 				goto out;
1519 			}
1520 
1521 			copied_props++;
1522 		}
1523 
1524 		drm_mode_object_put(obj);
1525 	}
1526 
1527 	ret = prepare_signaling(dev, state, arg, file_priv, &fence_state,
1528 				&num_fences);
1529 	if (ret)
1530 		goto out;
1531 
1532 	if (arg->flags & DRM_MODE_PAGE_FLIP_ASYNC)
1533 		set_async_flip(state);
1534 
1535 	if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) {
1536 		ret = drm_atomic_check_only(state);
1537 	} else if (arg->flags & DRM_MODE_ATOMIC_NONBLOCK) {
1538 		ret = drm_atomic_nonblocking_commit(state);
1539 	} else {
1540 		ret = drm_atomic_commit(state);
1541 	}
1542 
1543 out:
1544 	complete_signaling(dev, state, fence_state, num_fences, !ret);
1545 
1546 	if (ret == -EDEADLK) {
1547 		drm_atomic_state_clear(state);
1548 		ret = drm_modeset_backoff(&ctx);
1549 		if (!ret)
1550 			goto retry;
1551 	}
1552 
1553 	drm_atomic_state_put(state);
1554 
1555 	drm_modeset_drop_locks(&ctx);
1556 	drm_modeset_acquire_fini(&ctx);
1557 
1558 	return ret;
1559 }
1560