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