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