xref: /linux/drivers/gpu/drm/drm_atomic_uapi.c (revision ce801e5d6c1bac228bf10f75e8bede4285c58282)
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 	connector->dpms = mode;
961 
962 	crtc = connector->state->crtc;
963 	if (!crtc)
964 		goto out;
965 	ret = drm_atomic_add_affected_connectors(state, crtc);
966 	if (ret)
967 		goto out;
968 
969 	crtc_state = drm_atomic_get_crtc_state(state, crtc);
970 	if (IS_ERR(crtc_state)) {
971 		ret = PTR_ERR(crtc_state);
972 		goto out;
973 	}
974 
975 	for_each_new_connector_in_state(state, tmp_connector, new_conn_state, i) {
976 		if (new_conn_state->crtc != crtc)
977 			continue;
978 		if (tmp_connector->dpms == DRM_MODE_DPMS_ON) {
979 			active = true;
980 			break;
981 		}
982 	}
983 
984 	crtc_state->active = active;
985 	ret = drm_atomic_commit(state);
986 out:
987 	if (ret != 0)
988 		connector->dpms = old_mode;
989 	return ret;
990 }
991 
992 static int drm_atomic_check_prop_changes(int ret, uint64_t old_val, uint64_t prop_value,
993 					 struct drm_property *prop)
994 {
995 	if (ret != 0 || old_val != prop_value) {
996 		drm_dbg_atomic(prop->dev,
997 			       "[PROP:%d:%s] No prop can be changed during async flip\n",
998 			       prop->base.id, prop->name);
999 		return -EINVAL;
1000 	}
1001 
1002 	return 0;
1003 }
1004 
1005 int drm_atomic_set_property(struct drm_atomic_state *state,
1006 			    struct drm_file *file_priv,
1007 			    struct drm_mode_object *obj,
1008 			    struct drm_property *prop,
1009 			    u64 prop_value,
1010 			    bool async_flip)
1011 {
1012 	struct drm_mode_object *ref;
1013 	u64 old_val;
1014 	int ret;
1015 
1016 	if (!drm_property_change_valid_get(prop, prop_value, &ref))
1017 		return -EINVAL;
1018 
1019 	switch (obj->type) {
1020 	case DRM_MODE_OBJECT_CONNECTOR: {
1021 		struct drm_connector *connector = obj_to_connector(obj);
1022 		struct drm_connector_state *connector_state;
1023 
1024 		connector_state = drm_atomic_get_connector_state(state, connector);
1025 		if (IS_ERR(connector_state)) {
1026 			ret = PTR_ERR(connector_state);
1027 			break;
1028 		}
1029 
1030 		if (async_flip) {
1031 			ret = drm_atomic_connector_get_property(connector, connector_state,
1032 								prop, &old_val);
1033 			ret = drm_atomic_check_prop_changes(ret, old_val, prop_value, prop);
1034 			break;
1035 		}
1036 
1037 		ret = drm_atomic_connector_set_property(connector,
1038 				connector_state, file_priv,
1039 				prop, prop_value);
1040 		break;
1041 	}
1042 	case DRM_MODE_OBJECT_CRTC: {
1043 		struct drm_crtc *crtc = obj_to_crtc(obj);
1044 		struct drm_crtc_state *crtc_state;
1045 
1046 		crtc_state = drm_atomic_get_crtc_state(state, crtc);
1047 		if (IS_ERR(crtc_state)) {
1048 			ret = PTR_ERR(crtc_state);
1049 			break;
1050 		}
1051 
1052 		if (async_flip) {
1053 			ret = drm_atomic_crtc_get_property(crtc, crtc_state,
1054 							   prop, &old_val);
1055 			ret = drm_atomic_check_prop_changes(ret, old_val, prop_value, prop);
1056 			break;
1057 		}
1058 
1059 		ret = drm_atomic_crtc_set_property(crtc,
1060 				crtc_state, prop, prop_value);
1061 		break;
1062 	}
1063 	case DRM_MODE_OBJECT_PLANE: {
1064 		struct drm_plane *plane = obj_to_plane(obj);
1065 		struct drm_plane_state *plane_state;
1066 		struct drm_mode_config *config = &plane->dev->mode_config;
1067 		const struct drm_plane_helper_funcs *plane_funcs = plane->helper_private;
1068 
1069 		plane_state = drm_atomic_get_plane_state(state, plane);
1070 		if (IS_ERR(plane_state)) {
1071 			ret = PTR_ERR(plane_state);
1072 			break;
1073 		}
1074 
1075 		if (async_flip) {
1076 			/* check if the prop does a nop change */
1077 			if ((prop != config->prop_fb_id &&
1078 			     prop != config->prop_in_fence_fd &&
1079 			     prop != config->prop_fb_damage_clips)) {
1080 				ret = drm_atomic_plane_get_property(plane, plane_state,
1081 								    prop, &old_val);
1082 				ret = drm_atomic_check_prop_changes(ret, old_val, prop_value, prop);
1083 			}
1084 
1085 			/* ask the driver if this non-primary plane is supported */
1086 			if (plane->type != DRM_PLANE_TYPE_PRIMARY) {
1087 				ret = -EINVAL;
1088 
1089 				if (plane_funcs && plane_funcs->atomic_async_check)
1090 					ret = plane_funcs->atomic_async_check(plane, state, true);
1091 
1092 				if (ret) {
1093 					drm_dbg_atomic(prop->dev,
1094 						       "[PLANE:%d:%s] does not support async flips\n",
1095 						       obj->id, plane->name);
1096 					break;
1097 				}
1098 			}
1099 		}
1100 
1101 		ret = drm_atomic_plane_set_property(plane,
1102 				plane_state, file_priv,
1103 				prop, prop_value);
1104 		break;
1105 	}
1106 	default:
1107 		drm_dbg_atomic(prop->dev, "[OBJECT:%d] has no properties\n", obj->id);
1108 		ret = -EINVAL;
1109 		break;
1110 	}
1111 
1112 	drm_property_change_valid_put(prop, ref);
1113 	return ret;
1114 }
1115 
1116 /**
1117  * DOC: explicit fencing properties
1118  *
1119  * Explicit fencing allows userspace to control the buffer synchronization
1120  * between devices. A Fence or a group of fences are transferred to/from
1121  * userspace using Sync File fds and there are two DRM properties for that.
1122  * IN_FENCE_FD on each DRM Plane to send fences to the kernel and
1123  * OUT_FENCE_PTR on each DRM CRTC to receive fences from the kernel.
1124  *
1125  * As a contrast, with implicit fencing the kernel keeps track of any
1126  * ongoing rendering, and automatically ensures that the atomic update waits
1127  * for any pending rendering to complete. This is usually tracked in &struct
1128  * dma_resv which can also contain mandatory kernel fences. Implicit syncing
1129  * is how Linux traditionally worked (e.g. DRI2/3 on X.org), whereas explicit
1130  * fencing is what Android wants.
1131  *
1132  * "IN_FENCE_FD”:
1133  *	Use this property to pass a fence that DRM should wait on before
1134  *	proceeding with the Atomic Commit request and show the framebuffer for
1135  *	the plane on the screen. The fence can be either a normal fence or a
1136  *	merged one, the sync_file framework will handle both cases and use a
1137  *	fence_array if a merged fence is received. Passing -1 here means no
1138  *	fences to wait on.
1139  *
1140  *	If the Atomic Commit request has the DRM_MODE_ATOMIC_TEST_ONLY flag
1141  *	it will only check if the Sync File is a valid one.
1142  *
1143  *	On the driver side the fence is stored on the @fence parameter of
1144  *	&struct drm_plane_state. Drivers which also support implicit fencing
1145  *	should extract the implicit fence using drm_gem_plane_helper_prepare_fb(),
1146  *	to make sure there's consistent behaviour between drivers in precedence
1147  *	of implicit vs. explicit fencing.
1148  *
1149  * "OUT_FENCE_PTR”:
1150  *	Use this property to pass a file descriptor pointer to DRM. Once the
1151  *	Atomic Commit request call returns OUT_FENCE_PTR will be filled with
1152  *	the file descriptor number of a Sync File. This Sync File contains the
1153  *	CRTC fence that will be signaled when all framebuffers present on the
1154  *	Atomic Commit * request for that given CRTC are scanned out on the
1155  *	screen.
1156  *
1157  *	The Atomic Commit request fails if a invalid pointer is passed. If the
1158  *	Atomic Commit request fails for any other reason the out fence fd
1159  *	returned will be -1. On a Atomic Commit with the
1160  *	DRM_MODE_ATOMIC_TEST_ONLY flag the out fence will also be set to -1.
1161  *
1162  *	Note that out-fences don't have a special interface to drivers and are
1163  *	internally represented by a &struct drm_pending_vblank_event in struct
1164  *	&drm_crtc_state, which is also used by the nonblocking atomic commit
1165  *	helpers and for the DRM event handling for existing userspace.
1166  */
1167 
1168 struct drm_out_fence_state {
1169 	s32 __user *out_fence_ptr;
1170 	struct sync_file *sync_file;
1171 	int fd;
1172 };
1173 
1174 static int setup_out_fence(struct drm_out_fence_state *fence_state,
1175 			   struct dma_fence *fence)
1176 {
1177 	fence_state->fd = get_unused_fd_flags(O_CLOEXEC);
1178 	if (fence_state->fd < 0)
1179 		return fence_state->fd;
1180 
1181 	if (put_user(fence_state->fd, fence_state->out_fence_ptr))
1182 		return -EFAULT;
1183 
1184 	fence_state->sync_file = sync_file_create(fence);
1185 	if (!fence_state->sync_file)
1186 		return -ENOMEM;
1187 
1188 	return 0;
1189 }
1190 
1191 static int prepare_signaling(struct drm_device *dev,
1192 				  struct drm_atomic_state *state,
1193 				  struct drm_mode_atomic *arg,
1194 				  struct drm_file *file_priv,
1195 				  struct drm_out_fence_state **fence_state,
1196 				  unsigned int *num_fences)
1197 {
1198 	struct drm_crtc *crtc;
1199 	struct drm_crtc_state *crtc_state;
1200 	struct drm_connector *conn;
1201 	struct drm_connector_state *conn_state;
1202 	int i, c = 0, ret;
1203 
1204 	if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)
1205 		return 0;
1206 
1207 	for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
1208 		s32 __user *fence_ptr;
1209 
1210 		fence_ptr = get_out_fence_for_crtc(crtc_state->state, crtc);
1211 
1212 		if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT || fence_ptr) {
1213 			struct drm_pending_vblank_event *e;
1214 
1215 			e = create_vblank_event(crtc, arg->user_data);
1216 			if (!e)
1217 				return -ENOMEM;
1218 
1219 			crtc_state->event = e;
1220 		}
1221 
1222 		if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
1223 			struct drm_pending_vblank_event *e = crtc_state->event;
1224 
1225 			if (!file_priv)
1226 				continue;
1227 
1228 			ret = drm_event_reserve_init(dev, file_priv, &e->base,
1229 						     &e->event.base);
1230 			if (ret) {
1231 				kfree(e);
1232 				crtc_state->event = NULL;
1233 				return ret;
1234 			}
1235 		}
1236 
1237 		if (fence_ptr) {
1238 			struct dma_fence *fence;
1239 			struct drm_out_fence_state *f;
1240 
1241 			f = krealloc(*fence_state, sizeof(**fence_state) *
1242 				     (*num_fences + 1), GFP_KERNEL);
1243 			if (!f)
1244 				return -ENOMEM;
1245 
1246 			memset(&f[*num_fences], 0, sizeof(*f));
1247 
1248 			f[*num_fences].out_fence_ptr = fence_ptr;
1249 			*fence_state = f;
1250 
1251 			fence = drm_crtc_create_fence(crtc);
1252 			if (!fence)
1253 				return -ENOMEM;
1254 
1255 			ret = setup_out_fence(&f[(*num_fences)++], fence);
1256 			if (ret) {
1257 				dma_fence_put(fence);
1258 				return ret;
1259 			}
1260 
1261 			crtc_state->event->base.fence = fence;
1262 		}
1263 
1264 		c++;
1265 	}
1266 
1267 	for_each_new_connector_in_state(state, conn, conn_state, i) {
1268 		struct drm_writeback_connector *wb_conn;
1269 		struct drm_out_fence_state *f;
1270 		struct dma_fence *fence;
1271 		s32 __user *fence_ptr;
1272 
1273 		if (!conn_state->writeback_job)
1274 			continue;
1275 
1276 		fence_ptr = get_out_fence_for_connector(state, conn);
1277 		if (!fence_ptr)
1278 			continue;
1279 
1280 		f = krealloc(*fence_state, sizeof(**fence_state) *
1281 			     (*num_fences + 1), GFP_KERNEL);
1282 		if (!f)
1283 			return -ENOMEM;
1284 
1285 		memset(&f[*num_fences], 0, sizeof(*f));
1286 
1287 		f[*num_fences].out_fence_ptr = fence_ptr;
1288 		*fence_state = f;
1289 
1290 		wb_conn = drm_connector_to_writeback(conn);
1291 		fence = drm_writeback_get_out_fence(wb_conn);
1292 		if (!fence)
1293 			return -ENOMEM;
1294 
1295 		ret = setup_out_fence(&f[(*num_fences)++], fence);
1296 		if (ret) {
1297 			dma_fence_put(fence);
1298 			return ret;
1299 		}
1300 
1301 		conn_state->writeback_job->out_fence = fence;
1302 	}
1303 
1304 	/*
1305 	 * Having this flag means user mode pends on event which will never
1306 	 * reach due to lack of at least one CRTC for signaling
1307 	 */
1308 	if (c == 0 && (arg->flags & DRM_MODE_PAGE_FLIP_EVENT)) {
1309 		drm_dbg_atomic(dev, "need at least one CRTC for DRM_MODE_PAGE_FLIP_EVENT");
1310 		return -EINVAL;
1311 	}
1312 
1313 	return 0;
1314 }
1315 
1316 static void complete_signaling(struct drm_device *dev,
1317 			       struct drm_atomic_state *state,
1318 			       struct drm_out_fence_state *fence_state,
1319 			       unsigned int num_fences,
1320 			       bool install_fds)
1321 {
1322 	struct drm_crtc *crtc;
1323 	struct drm_crtc_state *crtc_state;
1324 	int i;
1325 
1326 	if (install_fds) {
1327 		for (i = 0; i < num_fences; i++)
1328 			fd_install(fence_state[i].fd,
1329 				   fence_state[i].sync_file->file);
1330 
1331 		kfree(fence_state);
1332 		return;
1333 	}
1334 
1335 	for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
1336 		struct drm_pending_vblank_event *event = crtc_state->event;
1337 		/*
1338 		 * Free the allocated event. drm_atomic_helper_setup_commit
1339 		 * can allocate an event too, so only free it if it's ours
1340 		 * to prevent a double free in drm_atomic_state_clear.
1341 		 */
1342 		if (event && (event->base.fence || event->base.file_priv)) {
1343 			drm_event_cancel_free(dev, &event->base);
1344 			crtc_state->event = NULL;
1345 		}
1346 	}
1347 
1348 	if (!fence_state)
1349 		return;
1350 
1351 	for (i = 0; i < num_fences; i++) {
1352 		if (fence_state[i].sync_file)
1353 			fput(fence_state[i].sync_file->file);
1354 		if (fence_state[i].fd >= 0)
1355 			put_unused_fd(fence_state[i].fd);
1356 
1357 		/* If this fails log error to the user */
1358 		if (fence_state[i].out_fence_ptr &&
1359 		    put_user(-1, fence_state[i].out_fence_ptr))
1360 			drm_dbg_atomic(dev, "Couldn't clear out_fence_ptr\n");
1361 	}
1362 
1363 	kfree(fence_state);
1364 }
1365 
1366 static void
1367 set_async_flip(struct drm_atomic_state *state)
1368 {
1369 	struct drm_crtc *crtc;
1370 	struct drm_crtc_state *crtc_state;
1371 	int i;
1372 
1373 	for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
1374 		crtc_state->async_flip = true;
1375 	}
1376 }
1377 
1378 int drm_mode_atomic_ioctl(struct drm_device *dev,
1379 			  void *data, struct drm_file *file_priv)
1380 {
1381 	struct drm_mode_atomic *arg = data;
1382 	uint32_t __user *objs_ptr = (uint32_t __user *)(unsigned long)(arg->objs_ptr);
1383 	uint32_t __user *count_props_ptr = (uint32_t __user *)(unsigned long)(arg->count_props_ptr);
1384 	uint32_t __user *props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
1385 	uint64_t __user *prop_values_ptr = (uint64_t __user *)(unsigned long)(arg->prop_values_ptr);
1386 	unsigned int copied_objs, copied_props;
1387 	struct drm_atomic_state *state;
1388 	struct drm_modeset_acquire_ctx ctx;
1389 	struct drm_out_fence_state *fence_state;
1390 	int ret = 0;
1391 	unsigned int i, j, num_fences;
1392 	bool async_flip = false;
1393 
1394 	/* disallow for drivers not supporting atomic: */
1395 	if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
1396 		return -EOPNOTSUPP;
1397 
1398 	/* disallow for userspace that has not enabled atomic cap (even
1399 	 * though this may be a bit overkill, since legacy userspace
1400 	 * wouldn't know how to call this ioctl)
1401 	 */
1402 	if (!file_priv->atomic) {
1403 		drm_dbg_atomic(dev,
1404 			       "commit failed: atomic cap not enabled\n");
1405 		return -EINVAL;
1406 	}
1407 
1408 	if (arg->flags & ~DRM_MODE_ATOMIC_FLAGS) {
1409 		drm_dbg_atomic(dev, "commit failed: invalid flag\n");
1410 		return -EINVAL;
1411 	}
1412 
1413 	if (arg->reserved) {
1414 		drm_dbg_atomic(dev, "commit failed: reserved field set\n");
1415 		return -EINVAL;
1416 	}
1417 
1418 	if (arg->flags & DRM_MODE_PAGE_FLIP_ASYNC) {
1419 		if (!dev->mode_config.async_page_flip) {
1420 			drm_dbg_atomic(dev,
1421 				       "commit failed: DRM_MODE_PAGE_FLIP_ASYNC not supported\n");
1422 			return -EINVAL;
1423 		}
1424 
1425 		async_flip = true;
1426 	}
1427 
1428 	/* can't test and expect an event at the same time. */
1429 	if ((arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) &&
1430 			(arg->flags & DRM_MODE_PAGE_FLIP_EVENT)) {
1431 		drm_dbg_atomic(dev,
1432 			       "commit failed: page-flip event requested with test-only commit\n");
1433 		return -EINVAL;
1434 	}
1435 
1436 	state = drm_atomic_state_alloc(dev);
1437 	if (!state)
1438 		return -ENOMEM;
1439 
1440 	drm_modeset_acquire_init(&ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE);
1441 	state->acquire_ctx = &ctx;
1442 	state->allow_modeset = !!(arg->flags & DRM_MODE_ATOMIC_ALLOW_MODESET);
1443 
1444 retry:
1445 	copied_objs = 0;
1446 	copied_props = 0;
1447 	fence_state = NULL;
1448 	num_fences = 0;
1449 
1450 	for (i = 0; i < arg->count_objs; i++) {
1451 		uint32_t obj_id, count_props;
1452 		struct drm_mode_object *obj;
1453 
1454 		if (get_user(obj_id, objs_ptr + copied_objs)) {
1455 			ret = -EFAULT;
1456 			goto out;
1457 		}
1458 
1459 		obj = drm_mode_object_find(dev, file_priv, obj_id, DRM_MODE_OBJECT_ANY);
1460 		if (!obj) {
1461 			drm_dbg_atomic(dev, "cannot find object ID %d", obj_id);
1462 			ret = -ENOENT;
1463 			goto out;
1464 		}
1465 
1466 		if (!obj->properties) {
1467 			drm_dbg_atomic(dev, "[OBJECT:%d] has no properties", obj_id);
1468 			drm_mode_object_put(obj);
1469 			ret = -ENOENT;
1470 			goto out;
1471 		}
1472 
1473 		if (get_user(count_props, count_props_ptr + copied_objs)) {
1474 			drm_mode_object_put(obj);
1475 			ret = -EFAULT;
1476 			goto out;
1477 		}
1478 
1479 		copied_objs++;
1480 
1481 		for (j = 0; j < count_props; j++) {
1482 			uint32_t prop_id;
1483 			uint64_t prop_value;
1484 			struct drm_property *prop;
1485 
1486 			if (get_user(prop_id, props_ptr + copied_props)) {
1487 				drm_mode_object_put(obj);
1488 				ret = -EFAULT;
1489 				goto out;
1490 			}
1491 
1492 			prop = drm_mode_obj_find_prop_id(obj, prop_id);
1493 			if (!prop) {
1494 				drm_dbg_atomic(dev,
1495 					       "[OBJECT:%d] cannot find property ID %d",
1496 					       obj_id, prop_id);
1497 				drm_mode_object_put(obj);
1498 				ret = -ENOENT;
1499 				goto out;
1500 			}
1501 
1502 			if (copy_from_user(&prop_value,
1503 					   prop_values_ptr + copied_props,
1504 					   sizeof(prop_value))) {
1505 				drm_mode_object_put(obj);
1506 				ret = -EFAULT;
1507 				goto out;
1508 			}
1509 
1510 			ret = drm_atomic_set_property(state, file_priv, obj,
1511 						      prop, prop_value, async_flip);
1512 			if (ret) {
1513 				drm_mode_object_put(obj);
1514 				goto out;
1515 			}
1516 
1517 			copied_props++;
1518 		}
1519 
1520 		drm_mode_object_put(obj);
1521 	}
1522 
1523 	ret = prepare_signaling(dev, state, arg, file_priv, &fence_state,
1524 				&num_fences);
1525 	if (ret)
1526 		goto out;
1527 
1528 	if (arg->flags & DRM_MODE_PAGE_FLIP_ASYNC)
1529 		set_async_flip(state);
1530 
1531 	if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) {
1532 		ret = drm_atomic_check_only(state);
1533 	} else if (arg->flags & DRM_MODE_ATOMIC_NONBLOCK) {
1534 		ret = drm_atomic_nonblocking_commit(state);
1535 	} else {
1536 		ret = drm_atomic_commit(state);
1537 	}
1538 
1539 out:
1540 	complete_signaling(dev, state, fence_state, num_fences, !ret);
1541 
1542 	if (ret == -EDEADLK) {
1543 		drm_atomic_state_clear(state);
1544 		ret = drm_modeset_backoff(&ctx);
1545 		if (!ret)
1546 			goto retry;
1547 	}
1548 
1549 	drm_atomic_state_put(state);
1550 
1551 	drm_modeset_drop_locks(&ctx);
1552 	drm_modeset_acquire_fini(&ctx);
1553 
1554 	return ret;
1555 }
1556