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