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