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