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