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