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