xref: /netbsd-src/sys/external/bsd/drm2/dist/drm/drm_atomic_uapi.c (revision d95770457554cf793db417b12e8b1f00fd21e581)
1*d9577045Sriastradh /*	$NetBSD: drm_atomic_uapi.c,v 1.7 2021/12/19 10:45:49 riastradh Exp $	*/
24e390cabSriastradh 
34e390cabSriastradh /*
44e390cabSriastradh  * Copyright (C) 2014 Red Hat
54e390cabSriastradh  * Copyright (C) 2014 Intel Corp.
64e390cabSriastradh  * Copyright (C) 2018 Intel Corp.
74e390cabSriastradh  *
84e390cabSriastradh  * Permission is hereby granted, free of charge, to any person obtaining a
94e390cabSriastradh  * copy of this software and associated documentation files (the "Software"),
104e390cabSriastradh  * to deal in the Software without restriction, including without limitation
114e390cabSriastradh  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
124e390cabSriastradh  * and/or sell copies of the Software, and to permit persons to whom the
134e390cabSriastradh  * Software is furnished to do so, subject to the following conditions:
144e390cabSriastradh  *
154e390cabSriastradh  * The above copyright notice and this permission notice shall be included in
164e390cabSriastradh  * all copies or substantial portions of the Software.
174e390cabSriastradh  *
184e390cabSriastradh  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
194e390cabSriastradh  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
204e390cabSriastradh  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
214e390cabSriastradh  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
224e390cabSriastradh  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
234e390cabSriastradh  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
244e390cabSriastradh  * OTHER DEALINGS IN THE SOFTWARE.
254e390cabSriastradh  *
264e390cabSriastradh  * Authors:
274e390cabSriastradh  * Rob Clark <robdclark@gmail.com>
284e390cabSriastradh  * Daniel Vetter <daniel.vetter@ffwll.ch>
294e390cabSriastradh  */
304e390cabSriastradh 
314e390cabSriastradh #include <sys/cdefs.h>
32*d9577045Sriastradh __KERNEL_RCSID(0, "$NetBSD: drm_atomic_uapi.c,v 1.7 2021/12/19 10:45:49 riastradh Exp $");
334e390cabSriastradh 
344e390cabSriastradh #include <drm/drm_atomic_uapi.h>
354e390cabSriastradh #include <drm/drm_atomic.h>
364e390cabSriastradh #include <drm/drm_print.h>
374e390cabSriastradh #include <drm/drm_drv.h>
384e390cabSriastradh #include <drm/drm_writeback.h>
394e390cabSriastradh #include <drm/drm_vblank.h>
404e390cabSriastradh 
414e390cabSriastradh #include <linux/dma-fence.h>
424e390cabSriastradh #include <linux/uaccess.h>
434e390cabSriastradh #include <linux/sync_file.h>
444e390cabSriastradh #include <linux/file.h>
454e390cabSriastradh 
464e390cabSriastradh #include "drm_crtc_internal.h"
474e390cabSriastradh 
484e390cabSriastradh /**
494e390cabSriastradh  * DOC: overview
504e390cabSriastradh  *
514e390cabSriastradh  * This file contains the marshalling and demarshalling glue for the atomic UAPI
524e390cabSriastradh  * in all its forms: The monster ATOMIC IOCTL itself, code for GET_PROPERTY and
534e390cabSriastradh  * SET_PROPERTY IOCTLs. Plus interface functions for compatibility helpers and
544e390cabSriastradh  * drivers which have special needs to construct their own atomic updates, e.g.
554e390cabSriastradh  * for load detect or similiar.
564e390cabSriastradh  */
574e390cabSriastradh 
584e390cabSriastradh /**
594e390cabSriastradh  * drm_atomic_set_mode_for_crtc - set mode for CRTC
604e390cabSriastradh  * @state: the CRTC whose incoming state to update
614e390cabSriastradh  * @mode: kernel-internal mode to use for the CRTC, or NULL to disable
624e390cabSriastradh  *
634e390cabSriastradh  * Set a mode (originating from the kernel) on the desired CRTC state and update
644e390cabSriastradh  * the enable property.
654e390cabSriastradh  *
664e390cabSriastradh  * RETURNS:
674e390cabSriastradh  * Zero on success, error code on failure. Cannot return -EDEADLK.
684e390cabSriastradh  */
drm_atomic_set_mode_for_crtc(struct drm_crtc_state * state,const struct drm_display_mode * mode)694e390cabSriastradh int drm_atomic_set_mode_for_crtc(struct drm_crtc_state *state,
704e390cabSriastradh 				 const struct drm_display_mode *mode)
714e390cabSriastradh {
724e390cabSriastradh 	struct drm_crtc *crtc = state->crtc;
734e390cabSriastradh 	struct drm_mode_modeinfo umode;
744e390cabSriastradh 
754e390cabSriastradh 	/* Early return for no change. */
764e390cabSriastradh 	if (mode && memcmp(&state->mode, mode, sizeof(*mode)) == 0)
774e390cabSriastradh 		return 0;
784e390cabSriastradh 
794e390cabSriastradh 	drm_property_blob_put(state->mode_blob);
804e390cabSriastradh 	state->mode_blob = NULL;
814e390cabSriastradh 
824e390cabSriastradh 	if (mode) {
834e390cabSriastradh 		drm_mode_convert_to_umode(&umode, mode);
844e390cabSriastradh 		state->mode_blob =
854e390cabSriastradh 			drm_property_create_blob(state->crtc->dev,
864e390cabSriastradh 		                                 sizeof(umode),
874e390cabSriastradh 		                                 &umode);
884e390cabSriastradh 		if (IS_ERR(state->mode_blob))
894e390cabSriastradh 			return PTR_ERR(state->mode_blob);
904e390cabSriastradh 
914e390cabSriastradh 		drm_mode_copy(&state->mode, mode);
924e390cabSriastradh 		state->enable = true;
934e390cabSriastradh 		DRM_DEBUG_ATOMIC("Set [MODE:%s] for [CRTC:%d:%s] state %p\n",
944e390cabSriastradh 				 mode->name, crtc->base.id, crtc->name, state);
954e390cabSriastradh 	} else {
964e390cabSriastradh 		memset(&state->mode, 0, sizeof(state->mode));
974e390cabSriastradh 		state->enable = false;
984e390cabSriastradh 		DRM_DEBUG_ATOMIC("Set [NOMODE] for [CRTC:%d:%s] state %p\n",
994e390cabSriastradh 				 crtc->base.id, crtc->name, state);
1004e390cabSriastradh 	}
1014e390cabSriastradh 
1024e390cabSriastradh 	return 0;
1034e390cabSriastradh }
1044e390cabSriastradh EXPORT_SYMBOL(drm_atomic_set_mode_for_crtc);
1054e390cabSriastradh 
1064e390cabSriastradh /**
1074e390cabSriastradh  * drm_atomic_set_mode_prop_for_crtc - set mode for CRTC
1084e390cabSriastradh  * @state: the CRTC whose incoming state to update
1094e390cabSriastradh  * @blob: pointer to blob property to use for mode
1104e390cabSriastradh  *
1114e390cabSriastradh  * Set a mode (originating from a blob property) on the desired CRTC state.
1124e390cabSriastradh  * This function will take a reference on the blob property for the CRTC state,
1134e390cabSriastradh  * and release the reference held on the state's existing mode property, if any
1144e390cabSriastradh  * was set.
1154e390cabSriastradh  *
1164e390cabSriastradh  * RETURNS:
1174e390cabSriastradh  * Zero on success, error code on failure. Cannot return -EDEADLK.
1184e390cabSriastradh  */
drm_atomic_set_mode_prop_for_crtc(struct drm_crtc_state * state,struct drm_property_blob * blob)1194e390cabSriastradh int drm_atomic_set_mode_prop_for_crtc(struct drm_crtc_state *state,
1204e390cabSriastradh                                       struct drm_property_blob *blob)
1214e390cabSriastradh {
1224e390cabSriastradh 	struct drm_crtc *crtc = state->crtc;
1234e390cabSriastradh 
1244e390cabSriastradh 	if (blob == state->mode_blob)
1254e390cabSriastradh 		return 0;
1264e390cabSriastradh 
1274e390cabSriastradh 	drm_property_blob_put(state->mode_blob);
1284e390cabSriastradh 	state->mode_blob = NULL;
1294e390cabSriastradh 
1304e390cabSriastradh 	memset(&state->mode, 0, sizeof(state->mode));
1314e390cabSriastradh 
1324e390cabSriastradh 	if (blob) {
1334e390cabSriastradh 		int ret;
1344e390cabSriastradh 
1354e390cabSriastradh 		if (blob->length != sizeof(struct drm_mode_modeinfo)) {
1364e390cabSriastradh 			DRM_DEBUG_ATOMIC("[CRTC:%d:%s] bad mode blob length: %zu\n",
1374e390cabSriastradh 					 crtc->base.id, crtc->name,
1384e390cabSriastradh 					 blob->length);
1394e390cabSriastradh 			return -EINVAL;
1404e390cabSriastradh 		}
1414e390cabSriastradh 
1424e390cabSriastradh 		ret = drm_mode_convert_umode(crtc->dev,
1434e390cabSriastradh 					     &state->mode, blob->data);
1444e390cabSriastradh 		if (ret) {
1454e390cabSriastradh 			DRM_DEBUG_ATOMIC("[CRTC:%d:%s] invalid mode (ret=%d, status=%s):\n",
1464e390cabSriastradh 					 crtc->base.id, crtc->name,
1474e390cabSriastradh 					 ret, drm_get_mode_status_name(state->mode.status));
1484e390cabSriastradh 			drm_mode_debug_printmodeline(&state->mode);
1494e390cabSriastradh 			return -EINVAL;
1504e390cabSriastradh 		}
1514e390cabSriastradh 
1524e390cabSriastradh 		state->mode_blob = drm_property_blob_get(blob);
1534e390cabSriastradh 		state->enable = true;
1544e390cabSriastradh 		DRM_DEBUG_ATOMIC("Set [MODE:%s] for [CRTC:%d:%s] state %p\n",
1554e390cabSriastradh 				 state->mode.name, crtc->base.id, crtc->name,
1564e390cabSriastradh 				 state);
1574e390cabSriastradh 	} else {
1584e390cabSriastradh 		state->enable = false;
1594e390cabSriastradh 		DRM_DEBUG_ATOMIC("Set [NOMODE] for [CRTC:%d:%s] state %p\n",
1604e390cabSriastradh 				 crtc->base.id, crtc->name, state);
1614e390cabSriastradh 	}
1624e390cabSriastradh 
1634e390cabSriastradh 	return 0;
1644e390cabSriastradh }
1654e390cabSriastradh EXPORT_SYMBOL(drm_atomic_set_mode_prop_for_crtc);
1664e390cabSriastradh 
1674e390cabSriastradh /**
1684e390cabSriastradh  * drm_atomic_set_crtc_for_plane - set CRTC for plane
1694e390cabSriastradh  * @plane_state: the plane whose incoming state to update
1704e390cabSriastradh  * @crtc: CRTC to use for the plane
1714e390cabSriastradh  *
1724e390cabSriastradh  * Changing the assigned CRTC for a plane requires us to grab the lock and state
1734e390cabSriastradh  * for the new CRTC, as needed. This function takes care of all these details
1744e390cabSriastradh  * besides updating the pointer in the state object itself.
1754e390cabSriastradh  *
1764e390cabSriastradh  * Returns:
1774e390cabSriastradh  * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1784e390cabSriastradh  * then the w/w mutex code has detected a deadlock and the entire atomic
1794e390cabSriastradh  * sequence must be restarted. All other errors are fatal.
1804e390cabSriastradh  */
1814e390cabSriastradh int
drm_atomic_set_crtc_for_plane(struct drm_plane_state * plane_state,struct drm_crtc * crtc)1824e390cabSriastradh drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
1834e390cabSriastradh 			      struct drm_crtc *crtc)
1844e390cabSriastradh {
1854e390cabSriastradh 	struct drm_plane *plane = plane_state->plane;
1864e390cabSriastradh 	struct drm_crtc_state *crtc_state;
1874e390cabSriastradh 	/* Nothing to do for same crtc*/
1884e390cabSriastradh 	if (plane_state->crtc == crtc)
1894e390cabSriastradh 		return 0;
1904e390cabSriastradh 	if (plane_state->crtc) {
1914e390cabSriastradh 		crtc_state = drm_atomic_get_crtc_state(plane_state->state,
1924e390cabSriastradh 						       plane_state->crtc);
1934e390cabSriastradh 		if (WARN_ON(IS_ERR(crtc_state)))
1944e390cabSriastradh 			return PTR_ERR(crtc_state);
1954e390cabSriastradh 
1964e390cabSriastradh 		crtc_state->plane_mask &= ~drm_plane_mask(plane);
1974e390cabSriastradh 	}
1984e390cabSriastradh 
1994e390cabSriastradh 	plane_state->crtc = crtc;
2004e390cabSriastradh 
2014e390cabSriastradh 	if (crtc) {
2024e390cabSriastradh 		crtc_state = drm_atomic_get_crtc_state(plane_state->state,
2034e390cabSriastradh 						       crtc);
2044e390cabSriastradh 		if (IS_ERR(crtc_state))
2054e390cabSriastradh 			return PTR_ERR(crtc_state);
2064e390cabSriastradh 		crtc_state->plane_mask |= drm_plane_mask(plane);
2074e390cabSriastradh 	}
2084e390cabSriastradh 
2094e390cabSriastradh 	if (crtc)
2104e390cabSriastradh 		DRM_DEBUG_ATOMIC("Link [PLANE:%d:%s] state %p to [CRTC:%d:%s]\n",
2114e390cabSriastradh 				 plane->base.id, plane->name, plane_state,
2124e390cabSriastradh 				 crtc->base.id, crtc->name);
2134e390cabSriastradh 	else
2144e390cabSriastradh 		DRM_DEBUG_ATOMIC("Link [PLANE:%d:%s] state %p to [NOCRTC]\n",
2154e390cabSriastradh 				 plane->base.id, plane->name, plane_state);
2164e390cabSriastradh 
2174e390cabSriastradh 	return 0;
2184e390cabSriastradh }
2194e390cabSriastradh EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane);
2204e390cabSriastradh 
2214e390cabSriastradh /**
2224e390cabSriastradh  * drm_atomic_set_fb_for_plane - set framebuffer for plane
2234e390cabSriastradh  * @plane_state: atomic state object for the plane
2244e390cabSriastradh  * @fb: fb to use for the plane
2254e390cabSriastradh  *
2264e390cabSriastradh  * Changing the assigned framebuffer for a plane requires us to grab a reference
2274e390cabSriastradh  * to the new fb and drop the reference to the old fb, if there is one. This
2284e390cabSriastradh  * function takes care of all these details besides updating the pointer in the
2294e390cabSriastradh  * state object itself.
2304e390cabSriastradh  */
2314e390cabSriastradh void
drm_atomic_set_fb_for_plane(struct drm_plane_state * plane_state,struct drm_framebuffer * fb)2324e390cabSriastradh drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
2334e390cabSriastradh 			    struct drm_framebuffer *fb)
2344e390cabSriastradh {
2354e390cabSriastradh 	struct drm_plane *plane = plane_state->plane;
2364e390cabSriastradh 
2374e390cabSriastradh 	if (fb)
2384e390cabSriastradh 		DRM_DEBUG_ATOMIC("Set [FB:%d] for [PLANE:%d:%s] state %p\n",
2394e390cabSriastradh 				 fb->base.id, plane->base.id, plane->name,
2404e390cabSriastradh 				 plane_state);
2414e390cabSriastradh 	else
2424e390cabSriastradh 		DRM_DEBUG_ATOMIC("Set [NOFB] for [PLANE:%d:%s] state %p\n",
2434e390cabSriastradh 				 plane->base.id, plane->name, plane_state);
2444e390cabSriastradh 
2454e390cabSriastradh 	drm_framebuffer_assign(&plane_state->fb, fb);
2464e390cabSriastradh }
2474e390cabSriastradh EXPORT_SYMBOL(drm_atomic_set_fb_for_plane);
2484e390cabSriastradh 
2494e390cabSriastradh /**
2504e390cabSriastradh  * drm_atomic_set_fence_for_plane - set fence for plane
2514e390cabSriastradh  * @plane_state: atomic state object for the plane
2524e390cabSriastradh  * @fence: dma_fence to use for the plane
2534e390cabSriastradh  *
2544e390cabSriastradh  * Helper to setup the plane_state fence in case it is not set yet.
2554e390cabSriastradh  * By using this drivers doesn't need to worry if the user choose
2564e390cabSriastradh  * implicit or explicit fencing.
2574e390cabSriastradh  *
2584e390cabSriastradh  * This function will not set the fence to the state if it was set
2594e390cabSriastradh  * via explicit fencing interfaces on the atomic ioctl. In that case it will
2604e390cabSriastradh  * drop the reference to the fence as we are not storing it anywhere.
2614e390cabSriastradh  * Otherwise, if &drm_plane_state.fence is not set this function we just set it
2624e390cabSriastradh  * with the received implicit fence. In both cases this function consumes a
2634e390cabSriastradh  * reference for @fence.
2644e390cabSriastradh  *
2654e390cabSriastradh  * This way explicit fencing can be used to overrule implicit fencing, which is
2664e390cabSriastradh  * important to make explicit fencing use-cases work: One example is using one
2674e390cabSriastradh  * buffer for 2 screens with different refresh rates. Implicit fencing will
2684e390cabSriastradh  * clamp rendering to the refresh rate of the slower screen, whereas explicit
2694e390cabSriastradh  * fence allows 2 independent render and display loops on a single buffer. If a
2704e390cabSriastradh  * driver allows obeys both implicit and explicit fences for plane updates, then
2714e390cabSriastradh  * it will break all the benefits of explicit fencing.
2724e390cabSriastradh  */
2734e390cabSriastradh void
drm_atomic_set_fence_for_plane(struct drm_plane_state * plane_state,struct dma_fence * fence)2744e390cabSriastradh drm_atomic_set_fence_for_plane(struct drm_plane_state *plane_state,
2754e390cabSriastradh 			       struct dma_fence *fence)
2764e390cabSriastradh {
2774e390cabSriastradh 	if (plane_state->fence) {
2784e390cabSriastradh 		dma_fence_put(fence);
2794e390cabSriastradh 		return;
2804e390cabSriastradh 	}
2814e390cabSriastradh 
2824e390cabSriastradh 	plane_state->fence = fence;
2834e390cabSriastradh }
2844e390cabSriastradh EXPORT_SYMBOL(drm_atomic_set_fence_for_plane);
2854e390cabSriastradh 
2864e390cabSriastradh /**
2874e390cabSriastradh  * drm_atomic_set_crtc_for_connector - set CRTC for connector
2884e390cabSriastradh  * @conn_state: atomic state object for the connector
2894e390cabSriastradh  * @crtc: CRTC to use for the connector
2904e390cabSriastradh  *
2914e390cabSriastradh  * Changing the assigned CRTC for a connector requires us to grab the lock and
2924e390cabSriastradh  * state for the new CRTC, as needed. This function takes care of all these
2934e390cabSriastradh  * details besides updating the pointer in the state object itself.
2944e390cabSriastradh  *
2954e390cabSriastradh  * Returns:
2964e390cabSriastradh  * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
2974e390cabSriastradh  * then the w/w mutex code has detected a deadlock and the entire atomic
2984e390cabSriastradh  * sequence must be restarted. All other errors are fatal.
2994e390cabSriastradh  */
3004e390cabSriastradh int
drm_atomic_set_crtc_for_connector(struct drm_connector_state * conn_state,struct drm_crtc * crtc)3014e390cabSriastradh drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
3024e390cabSriastradh 				  struct drm_crtc *crtc)
3034e390cabSriastradh {
3044e390cabSriastradh 	struct drm_connector *connector = conn_state->connector;
3054e390cabSriastradh 	struct drm_crtc_state *crtc_state;
3064e390cabSriastradh 
3074e390cabSriastradh 	if (conn_state->crtc == crtc)
3084e390cabSriastradh 		return 0;
3094e390cabSriastradh 
3104e390cabSriastradh 	if (conn_state->crtc) {
3114e390cabSriastradh 		crtc_state = drm_atomic_get_new_crtc_state(conn_state->state,
3124e390cabSriastradh 							   conn_state->crtc);
3134e390cabSriastradh 
3144e390cabSriastradh 		crtc_state->connector_mask &=
3154e390cabSriastradh 			~drm_connector_mask(conn_state->connector);
3164e390cabSriastradh 
3174e390cabSriastradh 		drm_connector_put(conn_state->connector);
3184e390cabSriastradh 		conn_state->crtc = NULL;
3194e390cabSriastradh 	}
3204e390cabSriastradh 
3214e390cabSriastradh 	if (crtc) {
3224e390cabSriastradh 		crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc);
3234e390cabSriastradh 		if (IS_ERR(crtc_state))
3244e390cabSriastradh 			return PTR_ERR(crtc_state);
3254e390cabSriastradh 
3264e390cabSriastradh 		crtc_state->connector_mask |=
3274e390cabSriastradh 			drm_connector_mask(conn_state->connector);
3284e390cabSriastradh 
3294e390cabSriastradh 		drm_connector_get(conn_state->connector);
3304e390cabSriastradh 		conn_state->crtc = crtc;
3314e390cabSriastradh 
3324e390cabSriastradh 		DRM_DEBUG_ATOMIC("Link [CONNECTOR:%d:%s] state %p to [CRTC:%d:%s]\n",
3334e390cabSriastradh 				 connector->base.id, connector->name,
3344e390cabSriastradh 				 conn_state, crtc->base.id, crtc->name);
3354e390cabSriastradh 	} else {
3364e390cabSriastradh 		DRM_DEBUG_ATOMIC("Link [CONNECTOR:%d:%s] state %p to [NOCRTC]\n",
3374e390cabSriastradh 				 connector->base.id, connector->name,
3384e390cabSriastradh 				 conn_state);
3394e390cabSriastradh 	}
3404e390cabSriastradh 
3414e390cabSriastradh 	return 0;
3424e390cabSriastradh }
3434e390cabSriastradh EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector);
3444e390cabSriastradh 
set_out_fence_for_crtc(struct drm_atomic_state * state,struct drm_crtc * crtc,s32 __user * fence_ptr)3454e390cabSriastradh static void set_out_fence_for_crtc(struct drm_atomic_state *state,
3464e390cabSriastradh 				   struct drm_crtc *crtc, s32 __user *fence_ptr)
3474e390cabSriastradh {
3484e390cabSriastradh 	state->crtcs[drm_crtc_index(crtc)].out_fence_ptr = fence_ptr;
3494e390cabSriastradh }
3504e390cabSriastradh 
get_out_fence_for_crtc(struct drm_atomic_state * state,struct drm_crtc * crtc)3514e390cabSriastradh static s32 __user *get_out_fence_for_crtc(struct drm_atomic_state *state,
3524e390cabSriastradh 					  struct drm_crtc *crtc)
3534e390cabSriastradh {
3544e390cabSriastradh 	s32 __user *fence_ptr;
3554e390cabSriastradh 
3564e390cabSriastradh 	fence_ptr = state->crtcs[drm_crtc_index(crtc)].out_fence_ptr;
3574e390cabSriastradh 	state->crtcs[drm_crtc_index(crtc)].out_fence_ptr = NULL;
3584e390cabSriastradh 
3594e390cabSriastradh 	return fence_ptr;
3604e390cabSriastradh }
3614e390cabSriastradh 
set_out_fence_for_connector(struct drm_atomic_state * state,struct drm_connector * connector,s32 __user * fence_ptr)3624e390cabSriastradh static int set_out_fence_for_connector(struct drm_atomic_state *state,
3634e390cabSriastradh 					struct drm_connector *connector,
3644e390cabSriastradh 					s32 __user *fence_ptr)
3654e390cabSriastradh {
3664e390cabSriastradh 	unsigned int index = drm_connector_index(connector);
3674e390cabSriastradh 
3684e390cabSriastradh 	if (!fence_ptr)
3694e390cabSriastradh 		return 0;
3704e390cabSriastradh 
3714e390cabSriastradh 	if (put_user(-1, fence_ptr))
3724e390cabSriastradh 		return -EFAULT;
3734e390cabSriastradh 
3744e390cabSriastradh 	state->connectors[index].out_fence_ptr = fence_ptr;
3754e390cabSriastradh 
3764e390cabSriastradh 	return 0;
3774e390cabSriastradh }
3784e390cabSriastradh 
get_out_fence_for_connector(struct drm_atomic_state * state,struct drm_connector * connector)3794e390cabSriastradh static s32 __user *get_out_fence_for_connector(struct drm_atomic_state *state,
3804e390cabSriastradh 					       struct drm_connector *connector)
3814e390cabSriastradh {
3824e390cabSriastradh 	unsigned int index = drm_connector_index(connector);
3834e390cabSriastradh 	s32 __user *fence_ptr;
3844e390cabSriastradh 
3854e390cabSriastradh 	fence_ptr = state->connectors[index].out_fence_ptr;
3864e390cabSriastradh 	state->connectors[index].out_fence_ptr = NULL;
3874e390cabSriastradh 
3884e390cabSriastradh 	return fence_ptr;
3894e390cabSriastradh }
3904e390cabSriastradh 
3914e390cabSriastradh 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)3924e390cabSriastradh drm_atomic_replace_property_blob_from_id(struct drm_device *dev,
3934e390cabSriastradh 					 struct drm_property_blob **blob,
3944e390cabSriastradh 					 uint64_t blob_id,
3954e390cabSriastradh 					 ssize_t expected_size,
3964e390cabSriastradh 					 ssize_t expected_elem_size,
3974e390cabSriastradh 					 bool *replaced)
3984e390cabSriastradh {
3994e390cabSriastradh 	struct drm_property_blob *new_blob = NULL;
4004e390cabSriastradh 
4014e390cabSriastradh 	if (blob_id != 0) {
4024e390cabSriastradh 		new_blob = drm_property_lookup_blob(dev, blob_id);
4034e390cabSriastradh 		if (new_blob == NULL)
4044e390cabSriastradh 			return -EINVAL;
4054e390cabSriastradh 
4064e390cabSriastradh 		if (expected_size > 0 &&
4074e390cabSriastradh 		    new_blob->length != expected_size) {
4084e390cabSriastradh 			drm_property_blob_put(new_blob);
4094e390cabSriastradh 			return -EINVAL;
4104e390cabSriastradh 		}
4114e390cabSriastradh 		if (expected_elem_size > 0 &&
4124e390cabSriastradh 		    new_blob->length % expected_elem_size != 0) {
4134e390cabSriastradh 			drm_property_blob_put(new_blob);
4144e390cabSriastradh 			return -EINVAL;
4154e390cabSriastradh 		}
4164e390cabSriastradh 	}
4174e390cabSriastradh 
4184e390cabSriastradh 	*replaced |= drm_property_replace_blob(blob, new_blob);
4194e390cabSriastradh 	drm_property_blob_put(new_blob);
4204e390cabSriastradh 
4214e390cabSriastradh 	return 0;
4224e390cabSriastradh }
4234e390cabSriastradh 
drm_atomic_crtc_set_property(struct drm_crtc * crtc,struct drm_crtc_state * state,struct drm_property * property,uint64_t val)4244e390cabSriastradh static int drm_atomic_crtc_set_property(struct drm_crtc *crtc,
4254e390cabSriastradh 		struct drm_crtc_state *state, struct drm_property *property,
4264e390cabSriastradh 		uint64_t val)
4274e390cabSriastradh {
4284e390cabSriastradh 	struct drm_device *dev = crtc->dev;
4294e390cabSriastradh 	struct drm_mode_config *config = &dev->mode_config;
4304e390cabSriastradh 	bool replaced = false;
4314e390cabSriastradh 	int ret;
4324e390cabSriastradh 
4334e390cabSriastradh 	if (property == config->prop_active)
4344e390cabSriastradh 		state->active = val;
4354e390cabSriastradh 	else if (property == config->prop_mode_id) {
4364e390cabSriastradh 		struct drm_property_blob *mode =
4374e390cabSriastradh 			drm_property_lookup_blob(dev, val);
4384e390cabSriastradh 		ret = drm_atomic_set_mode_prop_for_crtc(state, mode);
4394e390cabSriastradh 		drm_property_blob_put(mode);
4404e390cabSriastradh 		return ret;
4414e390cabSriastradh 	} else if (property == config->prop_vrr_enabled) {
4424e390cabSriastradh 		state->vrr_enabled = val;
4434e390cabSriastradh 	} else if (property == config->degamma_lut_property) {
4444e390cabSriastradh 		ret = drm_atomic_replace_property_blob_from_id(dev,
4454e390cabSriastradh 					&state->degamma_lut,
4464e390cabSriastradh 					val,
4474e390cabSriastradh 					-1, sizeof(struct drm_color_lut),
4484e390cabSriastradh 					&replaced);
4494e390cabSriastradh 		state->color_mgmt_changed |= replaced;
4504e390cabSriastradh 		return ret;
4514e390cabSriastradh 	} else if (property == config->ctm_property) {
4524e390cabSriastradh 		ret = drm_atomic_replace_property_blob_from_id(dev,
4534e390cabSriastradh 					&state->ctm,
4544e390cabSriastradh 					val,
4554e390cabSriastradh 					sizeof(struct drm_color_ctm), -1,
4564e390cabSriastradh 					&replaced);
4574e390cabSriastradh 		state->color_mgmt_changed |= replaced;
4584e390cabSriastradh 		return ret;
4594e390cabSriastradh 	} else if (property == config->gamma_lut_property) {
4604e390cabSriastradh 		ret = drm_atomic_replace_property_blob_from_id(dev,
4614e390cabSriastradh 					&state->gamma_lut,
4624e390cabSriastradh 					val,
4634e390cabSriastradh 					-1, sizeof(struct drm_color_lut),
4644e390cabSriastradh 					&replaced);
4654e390cabSriastradh 		state->color_mgmt_changed |= replaced;
4664e390cabSriastradh 		return ret;
4674e390cabSriastradh 	} else if (property == config->prop_out_fence_ptr) {
4684e390cabSriastradh 		s32 __user *fence_ptr = u64_to_user_ptr(val);
4694e390cabSriastradh 
4704e390cabSriastradh 		if (!fence_ptr)
4714e390cabSriastradh 			return 0;
4724e390cabSriastradh 
4734e390cabSriastradh 		if (put_user(-1, fence_ptr))
4744e390cabSriastradh 			return -EFAULT;
4754e390cabSriastradh 
4764e390cabSriastradh 		set_out_fence_for_crtc(state->state, crtc, fence_ptr);
4774e390cabSriastradh 	} else if (crtc->funcs->atomic_set_property) {
4784e390cabSriastradh 		return crtc->funcs->atomic_set_property(crtc, state, property, val);
4794e390cabSriastradh 	} else {
4804e390cabSriastradh 		DRM_DEBUG_ATOMIC("[CRTC:%d:%s] unknown property [PROP:%d:%s]]\n",
4814e390cabSriastradh 				 crtc->base.id, crtc->name,
4824e390cabSriastradh 				 property->base.id, property->name);
4834e390cabSriastradh 		return -EINVAL;
4844e390cabSriastradh 	}
4854e390cabSriastradh 
4864e390cabSriastradh 	return 0;
4874e390cabSriastradh }
4884e390cabSriastradh 
4894e390cabSriastradh static int
drm_atomic_crtc_get_property(struct drm_crtc * crtc,const struct drm_crtc_state * state,struct drm_property * property,uint64_t * val)4904e390cabSriastradh drm_atomic_crtc_get_property(struct drm_crtc *crtc,
4914e390cabSriastradh 		const struct drm_crtc_state *state,
4924e390cabSriastradh 		struct drm_property *property, uint64_t *val)
4934e390cabSriastradh {
4944e390cabSriastradh 	struct drm_device *dev = crtc->dev;
4954e390cabSriastradh 	struct drm_mode_config *config = &dev->mode_config;
4964e390cabSriastradh 
4974e390cabSriastradh 	if (property == config->prop_active)
4984e390cabSriastradh 		*val = drm_atomic_crtc_effectively_active(state);
4994e390cabSriastradh 	else if (property == config->prop_mode_id)
5004e390cabSriastradh 		*val = (state->mode_blob) ? state->mode_blob->base.id : 0;
5014e390cabSriastradh 	else if (property == config->prop_vrr_enabled)
5024e390cabSriastradh 		*val = state->vrr_enabled;
5034e390cabSriastradh 	else if (property == config->degamma_lut_property)
5044e390cabSriastradh 		*val = (state->degamma_lut) ? state->degamma_lut->base.id : 0;
5054e390cabSriastradh 	else if (property == config->ctm_property)
5064e390cabSriastradh 		*val = (state->ctm) ? state->ctm->base.id : 0;
5074e390cabSriastradh 	else if (property == config->gamma_lut_property)
5084e390cabSriastradh 		*val = (state->gamma_lut) ? state->gamma_lut->base.id : 0;
5094e390cabSriastradh 	else if (property == config->prop_out_fence_ptr)
5104e390cabSriastradh 		*val = 0;
5114e390cabSriastradh 	else if (crtc->funcs->atomic_get_property)
5124e390cabSriastradh 		return crtc->funcs->atomic_get_property(crtc, state, property, val);
5134e390cabSriastradh 	else
5144e390cabSriastradh 		return -EINVAL;
5154e390cabSriastradh 
5164e390cabSriastradh 	return 0;
5174e390cabSriastradh }
5184e390cabSriastradh 
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)5194e390cabSriastradh static int drm_atomic_plane_set_property(struct drm_plane *plane,
5204e390cabSriastradh 		struct drm_plane_state *state, struct drm_file *file_priv,
5214e390cabSriastradh 		struct drm_property *property, uint64_t val)
5224e390cabSriastradh {
5234e390cabSriastradh 	struct drm_device *dev = plane->dev;
5244e390cabSriastradh 	struct drm_mode_config *config = &dev->mode_config;
5254e390cabSriastradh 	bool replaced = false;
5264e390cabSriastradh 	int ret;
5274e390cabSriastradh 
5284e390cabSriastradh 	if (property == config->prop_fb_id) {
5294e390cabSriastradh 		struct drm_framebuffer *fb;
5304e390cabSriastradh 		fb = drm_framebuffer_lookup(dev, file_priv, val);
5314e390cabSriastradh 		drm_atomic_set_fb_for_plane(state, fb);
5324e390cabSriastradh 		if (fb)
5334e390cabSriastradh 			drm_framebuffer_put(fb);
5344e390cabSriastradh 	} else if (property == config->prop_in_fence_fd) {
5354e390cabSriastradh 		if (state->fence)
5364e390cabSriastradh 			return -EINVAL;
5374e390cabSriastradh 
5384e390cabSriastradh 		if (U642I64(val) == -1)
5394e390cabSriastradh 			return 0;
5404e390cabSriastradh 
5414e390cabSriastradh 		state->fence = sync_file_get_fence(val);
5424e390cabSriastradh 		if (!state->fence)
5434e390cabSriastradh 			return -EINVAL;
5444e390cabSriastradh 
5454e390cabSriastradh 	} else if (property == config->prop_crtc_id) {
5464e390cabSriastradh 		struct drm_crtc *crtc = drm_crtc_find(dev, file_priv, val);
5474e390cabSriastradh 		if (val && !crtc)
5484e390cabSriastradh 			return -EACCES;
5494e390cabSriastradh 		return drm_atomic_set_crtc_for_plane(state, crtc);
5504e390cabSriastradh 	} else if (property == config->prop_crtc_x) {
5514e390cabSriastradh 		state->crtc_x = U642I64(val);
5524e390cabSriastradh 	} else if (property == config->prop_crtc_y) {
5534e390cabSriastradh 		state->crtc_y = U642I64(val);
5544e390cabSriastradh 	} else if (property == config->prop_crtc_w) {
5554e390cabSriastradh 		state->crtc_w = val;
5564e390cabSriastradh 	} else if (property == config->prop_crtc_h) {
5574e390cabSriastradh 		state->crtc_h = val;
5584e390cabSriastradh 	} else if (property == config->prop_src_x) {
5594e390cabSriastradh 		state->src_x = val;
5604e390cabSriastradh 	} else if (property == config->prop_src_y) {
5614e390cabSriastradh 		state->src_y = val;
5624e390cabSriastradh 	} else if (property == config->prop_src_w) {
5634e390cabSriastradh 		state->src_w = val;
5644e390cabSriastradh 	} else if (property == config->prop_src_h) {
5654e390cabSriastradh 		state->src_h = val;
5664e390cabSriastradh 	} else if (property == plane->alpha_property) {
5674e390cabSriastradh 		state->alpha = val;
5684e390cabSriastradh 	} else if (property == plane->blend_mode_property) {
5694e390cabSriastradh 		state->pixel_blend_mode = val;
5704e390cabSriastradh 	} else if (property == plane->rotation_property) {
5714e390cabSriastradh 		if (!is_power_of_2(val & DRM_MODE_ROTATE_MASK)) {
5724271e346Sriastradh 			DRM_DEBUG_ATOMIC("[PLANE:%d:%s] bad rotation bitmask: 0x%"PRIx64"\n",
5734e390cabSriastradh 					 plane->base.id, plane->name, val);
5744e390cabSriastradh 			return -EINVAL;
5754e390cabSriastradh 		}
5764e390cabSriastradh 		state->rotation = val;
5774e390cabSriastradh 	} else if (property == plane->zpos_property) {
5784e390cabSriastradh 		state->zpos = val;
5794e390cabSriastradh 	} else if (property == plane->color_encoding_property) {
5804e390cabSriastradh 		state->color_encoding = val;
5814e390cabSriastradh 	} else if (property == plane->color_range_property) {
5824e390cabSriastradh 		state->color_range = val;
5834e390cabSriastradh 	} else if (property == config->prop_fb_damage_clips) {
5844e390cabSriastradh 		ret = drm_atomic_replace_property_blob_from_id(dev,
5854e390cabSriastradh 					&state->fb_damage_clips,
5864e390cabSriastradh 					val,
5874e390cabSriastradh 					-1,
5884e390cabSriastradh 					sizeof(struct drm_rect),
5894e390cabSriastradh 					&replaced);
5904e390cabSriastradh 		return ret;
5914e390cabSriastradh 	} else if (plane->funcs->atomic_set_property) {
5924e390cabSriastradh 		return plane->funcs->atomic_set_property(plane, state,
5934e390cabSriastradh 				property, val);
5944e390cabSriastradh 	} else {
5954e390cabSriastradh 		DRM_DEBUG_ATOMIC("[PLANE:%d:%s] unknown property [PROP:%d:%s]]\n",
5964e390cabSriastradh 				 plane->base.id, plane->name,
5974e390cabSriastradh 				 property->base.id, property->name);
5984e390cabSriastradh 		return -EINVAL;
5994e390cabSriastradh 	}
6004e390cabSriastradh 
6014e390cabSriastradh 	return 0;
6024e390cabSriastradh }
6034e390cabSriastradh 
6044e390cabSriastradh static int
drm_atomic_plane_get_property(struct drm_plane * plane,const struct drm_plane_state * state,struct drm_property * property,uint64_t * val)6054e390cabSriastradh drm_atomic_plane_get_property(struct drm_plane *plane,
6064e390cabSriastradh 		const struct drm_plane_state *state,
6074e390cabSriastradh 		struct drm_property *property, uint64_t *val)
6084e390cabSriastradh {
6094e390cabSriastradh 	struct drm_device *dev = plane->dev;
6104e390cabSriastradh 	struct drm_mode_config *config = &dev->mode_config;
6114e390cabSriastradh 
6124e390cabSriastradh 	if (property == config->prop_fb_id) {
6134e390cabSriastradh 		*val = (state->fb) ? state->fb->base.id : 0;
6144e390cabSriastradh 	} else if (property == config->prop_in_fence_fd) {
6154e390cabSriastradh 		*val = -1;
6164e390cabSriastradh 	} else if (property == config->prop_crtc_id) {
6174e390cabSriastradh 		*val = (state->crtc) ? state->crtc->base.id : 0;
6184e390cabSriastradh 	} else if (property == config->prop_crtc_x) {
6194e390cabSriastradh 		*val = I642U64(state->crtc_x);
6204e390cabSriastradh 	} else if (property == config->prop_crtc_y) {
6214e390cabSriastradh 		*val = I642U64(state->crtc_y);
6224e390cabSriastradh 	} else if (property == config->prop_crtc_w) {
6234e390cabSriastradh 		*val = state->crtc_w;
6244e390cabSriastradh 	} else if (property == config->prop_crtc_h) {
6254e390cabSriastradh 		*val = state->crtc_h;
6264e390cabSriastradh 	} else if (property == config->prop_src_x) {
6274e390cabSriastradh 		*val = state->src_x;
6284e390cabSriastradh 	} else if (property == config->prop_src_y) {
6294e390cabSriastradh 		*val = state->src_y;
6304e390cabSriastradh 	} else if (property == config->prop_src_w) {
6314e390cabSriastradh 		*val = state->src_w;
6324e390cabSriastradh 	} else if (property == config->prop_src_h) {
6334e390cabSriastradh 		*val = state->src_h;
6344e390cabSriastradh 	} else if (property == plane->alpha_property) {
6354e390cabSriastradh 		*val = state->alpha;
6364e390cabSriastradh 	} else if (property == plane->blend_mode_property) {
6374e390cabSriastradh 		*val = state->pixel_blend_mode;
6384e390cabSriastradh 	} else if (property == plane->rotation_property) {
6394e390cabSriastradh 		*val = state->rotation;
6404e390cabSriastradh 	} else if (property == plane->zpos_property) {
6414e390cabSriastradh 		*val = state->zpos;
6424e390cabSriastradh 	} else if (property == plane->color_encoding_property) {
6434e390cabSriastradh 		*val = state->color_encoding;
6444e390cabSriastradh 	} else if (property == plane->color_range_property) {
6454e390cabSriastradh 		*val = state->color_range;
6464e390cabSriastradh 	} else if (property == config->prop_fb_damage_clips) {
6474e390cabSriastradh 		*val = (state->fb_damage_clips) ?
6484e390cabSriastradh 			state->fb_damage_clips->base.id : 0;
6494e390cabSriastradh 	} else if (plane->funcs->atomic_get_property) {
6504e390cabSriastradh 		return plane->funcs->atomic_get_property(plane, state, property, val);
6514e390cabSriastradh 	} else {
6524e390cabSriastradh 		return -EINVAL;
6534e390cabSriastradh 	}
6544e390cabSriastradh 
6554e390cabSriastradh 	return 0;
6564e390cabSriastradh }
6574e390cabSriastradh 
drm_atomic_set_writeback_fb_for_connector(struct drm_connector_state * conn_state,struct drm_framebuffer * fb)6584e390cabSriastradh static int drm_atomic_set_writeback_fb_for_connector(
6594e390cabSriastradh 		struct drm_connector_state *conn_state,
6604e390cabSriastradh 		struct drm_framebuffer *fb)
6614e390cabSriastradh {
6624e390cabSriastradh 	int ret;
6634e390cabSriastradh 
6644e390cabSriastradh 	ret = drm_writeback_set_fb(conn_state, fb);
6654e390cabSriastradh 	if (ret < 0)
6664e390cabSriastradh 		return ret;
6674e390cabSriastradh 
6684e390cabSriastradh 	if (fb)
6694e390cabSriastradh 		DRM_DEBUG_ATOMIC("Set [FB:%d] for connector state %p\n",
6704e390cabSriastradh 				 fb->base.id, conn_state);
6714e390cabSriastradh 	else
6724e390cabSriastradh 		DRM_DEBUG_ATOMIC("Set [NOFB] for connector state %p\n",
6734e390cabSriastradh 				 conn_state);
6744e390cabSriastradh 
6754e390cabSriastradh 	return 0;
6764e390cabSriastradh }
6774e390cabSriastradh 
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)6784e390cabSriastradh static int drm_atomic_connector_set_property(struct drm_connector *connector,
6794e390cabSriastradh 		struct drm_connector_state *state, struct drm_file *file_priv,
6804e390cabSriastradh 		struct drm_property *property, uint64_t val)
6814e390cabSriastradh {
6824e390cabSriastradh 	struct drm_device *dev = connector->dev;
6834e390cabSriastradh 	struct drm_mode_config *config = &dev->mode_config;
6844e390cabSriastradh 	bool replaced = false;
6854e390cabSriastradh 
6864e390cabSriastradh 	if (property == config->prop_crtc_id) {
6874e390cabSriastradh 		struct drm_crtc *crtc = drm_crtc_find(dev, file_priv, val);
6884e390cabSriastradh 		if (val && !crtc)
6894e390cabSriastradh 			return -EACCES;
6904e390cabSriastradh 		return drm_atomic_set_crtc_for_connector(state, crtc);
6914e390cabSriastradh 	} else if (property == config->dpms_property) {
6924e390cabSriastradh 		/* setting DPMS property requires special handling, which
6934e390cabSriastradh 		 * is done in legacy setprop path for us.  Disallow (for
6944e390cabSriastradh 		 * now?) atomic writes to DPMS property:
6954e390cabSriastradh 		 */
6964e390cabSriastradh 		return -EINVAL;
6974e390cabSriastradh 	} else if (property == config->tv_select_subconnector_property) {
6984e390cabSriastradh 		state->tv.subconnector = val;
6994e390cabSriastradh 	} else if (property == config->tv_left_margin_property) {
7004e390cabSriastradh 		state->tv.margins.left = val;
7014e390cabSriastradh 	} else if (property == config->tv_right_margin_property) {
7024e390cabSriastradh 		state->tv.margins.right = val;
7034e390cabSriastradh 	} else if (property == config->tv_top_margin_property) {
7044e390cabSriastradh 		state->tv.margins.top = val;
7054e390cabSriastradh 	} else if (property == config->tv_bottom_margin_property) {
7064e390cabSriastradh 		state->tv.margins.bottom = val;
7074e390cabSriastradh 	} else if (property == config->tv_mode_property) {
7084e390cabSriastradh 		state->tv.mode = val;
7094e390cabSriastradh 	} else if (property == config->tv_brightness_property) {
7104e390cabSriastradh 		state->tv.brightness = val;
7114e390cabSriastradh 	} else if (property == config->tv_contrast_property) {
7124e390cabSriastradh 		state->tv.contrast = val;
7134e390cabSriastradh 	} else if (property == config->tv_flicker_reduction_property) {
7144e390cabSriastradh 		state->tv.flicker_reduction = val;
7154e390cabSriastradh 	} else if (property == config->tv_overscan_property) {
7164e390cabSriastradh 		state->tv.overscan = val;
7174e390cabSriastradh 	} else if (property == config->tv_saturation_property) {
7184e390cabSriastradh 		state->tv.saturation = val;
7194e390cabSriastradh 	} else if (property == config->tv_hue_property) {
7204e390cabSriastradh 		state->tv.hue = val;
7214e390cabSriastradh 	} else if (property == config->link_status_property) {
7224e390cabSriastradh 		/* Never downgrade from GOOD to BAD on userspace's request here,
7234e390cabSriastradh 		 * only hw issues can do that.
7244e390cabSriastradh 		 *
7254e390cabSriastradh 		 * For an atomic property the userspace doesn't need to be able
7264e390cabSriastradh 		 * to understand all the properties, but needs to be able to
7274e390cabSriastradh 		 * restore the state it wants on VT switch. So if the userspace
7284e390cabSriastradh 		 * tries to change the link_status from GOOD to BAD, driver
7294e390cabSriastradh 		 * silently rejects it and returns a 0. This prevents userspace
7304e390cabSriastradh 		 * from accidently breaking  the display when it restores the
7314e390cabSriastradh 		 * state.
7324e390cabSriastradh 		 */
7334e390cabSriastradh 		if (state->link_status != DRM_LINK_STATUS_GOOD)
7344e390cabSriastradh 			state->link_status = val;
7354e390cabSriastradh 	} else if (property == config->hdr_output_metadata_property) {
7366ef5ce45Sriastradh 		int ret;
7374e390cabSriastradh 		ret = drm_atomic_replace_property_blob_from_id(dev,
7384e390cabSriastradh 				&state->hdr_output_metadata,
7394e390cabSriastradh 				val,
7404e390cabSriastradh 				sizeof(struct hdr_output_metadata), -1,
7414e390cabSriastradh 				&replaced);
7424e390cabSriastradh 		return ret;
7434e390cabSriastradh 	} else if (property == config->aspect_ratio_property) {
7444e390cabSriastradh 		state->picture_aspect_ratio = val;
7454e390cabSriastradh 	} else if (property == config->content_type_property) {
7464e390cabSriastradh 		state->content_type = val;
7474e390cabSriastradh 	} else if (property == connector->scaling_mode_property) {
7484e390cabSriastradh 		state->scaling_mode = val;
7494e390cabSriastradh 	} else if (property == config->content_protection_property) {
7504e390cabSriastradh 		if (val == DRM_MODE_CONTENT_PROTECTION_ENABLED) {
7514e390cabSriastradh 			DRM_DEBUG_KMS("only drivers can set CP Enabled\n");
7524e390cabSriastradh 			return -EINVAL;
7534e390cabSriastradh 		}
7544e390cabSriastradh 		state->content_protection = val;
7554e390cabSriastradh 	} else if (property == config->hdcp_content_type_property) {
7564e390cabSriastradh 		state->hdcp_content_type = val;
7574e390cabSriastradh 	} else if (property == connector->colorspace_property) {
7584e390cabSriastradh 		state->colorspace = val;
7594e390cabSriastradh 	} else if (property == config->writeback_fb_id_property) {
7604e390cabSriastradh 		struct drm_framebuffer *fb;
7614e390cabSriastradh 		int ret;
7624e390cabSriastradh 		fb = drm_framebuffer_lookup(dev, file_priv, val);
7634e390cabSriastradh 		ret = drm_atomic_set_writeback_fb_for_connector(state, fb);
7644e390cabSriastradh 		if (fb)
7654e390cabSriastradh 			drm_framebuffer_put(fb);
7664e390cabSriastradh 		return ret;
7674e390cabSriastradh 	} else if (property == config->writeback_out_fence_ptr_property) {
7684e390cabSriastradh 		s32 __user *fence_ptr = u64_to_user_ptr(val);
7694e390cabSriastradh 
7704e390cabSriastradh 		return set_out_fence_for_connector(state->state, connector,
7714e390cabSriastradh 						   fence_ptr);
7724e390cabSriastradh 	} else if (property == connector->max_bpc_property) {
7734e390cabSriastradh 		state->max_requested_bpc = val;
7744e390cabSriastradh 	} else if (connector->funcs->atomic_set_property) {
7754e390cabSriastradh 		return connector->funcs->atomic_set_property(connector,
7764e390cabSriastradh 				state, property, val);
7774e390cabSriastradh 	} else {
7784e390cabSriastradh 		DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] unknown property [PROP:%d:%s]]\n",
7794e390cabSriastradh 				 connector->base.id, connector->name,
7804e390cabSriastradh 				 property->base.id, property->name);
7814e390cabSriastradh 		return -EINVAL;
7824e390cabSriastradh 	}
7834e390cabSriastradh 
7844e390cabSriastradh 	return 0;
7854e390cabSriastradh }
7864e390cabSriastradh 
7874e390cabSriastradh static int
drm_atomic_connector_get_property(struct drm_connector * connector,const struct drm_connector_state * state,struct drm_property * property,uint64_t * val)7884e390cabSriastradh drm_atomic_connector_get_property(struct drm_connector *connector,
7894e390cabSriastradh 		const struct drm_connector_state *state,
7904e390cabSriastradh 		struct drm_property *property, uint64_t *val)
7914e390cabSriastradh {
7924e390cabSriastradh 	struct drm_device *dev = connector->dev;
7934e390cabSriastradh 	struct drm_mode_config *config = &dev->mode_config;
7944e390cabSriastradh 
7954e390cabSriastradh 	if (property == config->prop_crtc_id) {
7964e390cabSriastradh 		*val = (state->crtc) ? state->crtc->base.id : 0;
7974e390cabSriastradh 	} else if (property == config->dpms_property) {
7984e390cabSriastradh 		if (state->crtc && state->crtc->state->self_refresh_active)
7994e390cabSriastradh 			*val = DRM_MODE_DPMS_ON;
8004e390cabSriastradh 		else
8014e390cabSriastradh 			*val = connector->dpms;
8024e390cabSriastradh 	} else if (property == config->tv_select_subconnector_property) {
8034e390cabSriastradh 		*val = state->tv.subconnector;
8044e390cabSriastradh 	} else if (property == config->tv_left_margin_property) {
8054e390cabSriastradh 		*val = state->tv.margins.left;
8064e390cabSriastradh 	} else if (property == config->tv_right_margin_property) {
8074e390cabSriastradh 		*val = state->tv.margins.right;
8084e390cabSriastradh 	} else if (property == config->tv_top_margin_property) {
8094e390cabSriastradh 		*val = state->tv.margins.top;
8104e390cabSriastradh 	} else if (property == config->tv_bottom_margin_property) {
8114e390cabSriastradh 		*val = state->tv.margins.bottom;
8124e390cabSriastradh 	} else if (property == config->tv_mode_property) {
8134e390cabSriastradh 		*val = state->tv.mode;
8144e390cabSriastradh 	} else if (property == config->tv_brightness_property) {
8154e390cabSriastradh 		*val = state->tv.brightness;
8164e390cabSriastradh 	} else if (property == config->tv_contrast_property) {
8174e390cabSriastradh 		*val = state->tv.contrast;
8184e390cabSriastradh 	} else if (property == config->tv_flicker_reduction_property) {
8194e390cabSriastradh 		*val = state->tv.flicker_reduction;
8204e390cabSriastradh 	} else if (property == config->tv_overscan_property) {
8214e390cabSriastradh 		*val = state->tv.overscan;
8224e390cabSriastradh 	} else if (property == config->tv_saturation_property) {
8234e390cabSriastradh 		*val = state->tv.saturation;
8244e390cabSriastradh 	} else if (property == config->tv_hue_property) {
8254e390cabSriastradh 		*val = state->tv.hue;
8264e390cabSriastradh 	} else if (property == config->link_status_property) {
8274e390cabSriastradh 		*val = state->link_status;
8284e390cabSriastradh 	} else if (property == config->aspect_ratio_property) {
8294e390cabSriastradh 		*val = state->picture_aspect_ratio;
8304e390cabSriastradh 	} else if (property == config->content_type_property) {
8314e390cabSriastradh 		*val = state->content_type;
8324e390cabSriastradh 	} else if (property == connector->colorspace_property) {
8334e390cabSriastradh 		*val = state->colorspace;
8344e390cabSriastradh 	} else if (property == connector->scaling_mode_property) {
8354e390cabSriastradh 		*val = state->scaling_mode;
8364e390cabSriastradh 	} else if (property == config->hdr_output_metadata_property) {
8374e390cabSriastradh 		*val = state->hdr_output_metadata ?
8384e390cabSriastradh 			state->hdr_output_metadata->base.id : 0;
8394e390cabSriastradh 	} else if (property == config->content_protection_property) {
8404e390cabSriastradh 		*val = state->content_protection;
8414e390cabSriastradh 	} else if (property == config->hdcp_content_type_property) {
8424e390cabSriastradh 		*val = state->hdcp_content_type;
8434e390cabSriastradh 	} else if (property == config->writeback_fb_id_property) {
8444e390cabSriastradh 		/* Writeback framebuffer is one-shot, write and forget */
8454e390cabSriastradh 		*val = 0;
8464e390cabSriastradh 	} else if (property == config->writeback_out_fence_ptr_property) {
8474e390cabSriastradh 		*val = 0;
8484e390cabSriastradh 	} else if (property == connector->max_bpc_property) {
8494e390cabSriastradh 		*val = state->max_requested_bpc;
8504e390cabSriastradh 	} else if (connector->funcs->atomic_get_property) {
8514e390cabSriastradh 		return connector->funcs->atomic_get_property(connector,
8524e390cabSriastradh 				state, property, val);
8534e390cabSriastradh 	} else {
8544e390cabSriastradh 		return -EINVAL;
8554e390cabSriastradh 	}
8564e390cabSriastradh 
8574e390cabSriastradh 	return 0;
8584e390cabSriastradh }
8594e390cabSriastradh 
drm_atomic_get_property(struct drm_mode_object * obj,struct drm_property * property,uint64_t * val)8604e390cabSriastradh int drm_atomic_get_property(struct drm_mode_object *obj,
8614e390cabSriastradh 		struct drm_property *property, uint64_t *val)
8624e390cabSriastradh {
8634e390cabSriastradh 	struct drm_device *dev = property->dev;
8644e390cabSriastradh 	int ret;
8654e390cabSriastradh 
8664e390cabSriastradh 	switch (obj->type) {
8674e390cabSriastradh 	case DRM_MODE_OBJECT_CONNECTOR: {
8684e390cabSriastradh 		struct drm_connector *connector = obj_to_connector(obj);
8694e390cabSriastradh 		WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
8704e390cabSriastradh 		ret = drm_atomic_connector_get_property(connector,
8714e390cabSriastradh 				connector->state, property, val);
8724e390cabSriastradh 		break;
8734e390cabSriastradh 	}
8744e390cabSriastradh 	case DRM_MODE_OBJECT_CRTC: {
8754e390cabSriastradh 		struct drm_crtc *crtc = obj_to_crtc(obj);
8764e390cabSriastradh 		WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
8774e390cabSriastradh 		ret = drm_atomic_crtc_get_property(crtc,
8784e390cabSriastradh 				crtc->state, property, val);
8794e390cabSriastradh 		break;
8804e390cabSriastradh 	}
8814e390cabSriastradh 	case DRM_MODE_OBJECT_PLANE: {
8824e390cabSriastradh 		struct drm_plane *plane = obj_to_plane(obj);
8834e390cabSriastradh 		WARN_ON(!drm_modeset_is_locked(&plane->mutex));
8844e390cabSriastradh 		ret = drm_atomic_plane_get_property(plane,
8854e390cabSriastradh 				plane->state, property, val);
8864e390cabSriastradh 		break;
8874e390cabSriastradh 	}
8884e390cabSriastradh 	default:
8894e390cabSriastradh 		ret = -EINVAL;
8904e390cabSriastradh 		break;
8914e390cabSriastradh 	}
8924e390cabSriastradh 
8934e390cabSriastradh 	return ret;
8944e390cabSriastradh }
8954e390cabSriastradh 
8964e390cabSriastradh /*
8974e390cabSriastradh  * The big monster ioctl
8984e390cabSriastradh  */
8994e390cabSriastradh 
create_vblank_event(struct drm_crtc * crtc,uint64_t user_data)9004e390cabSriastradh static struct drm_pending_vblank_event *create_vblank_event(
9014e390cabSriastradh 		struct drm_crtc *crtc, uint64_t user_data)
9024e390cabSriastradh {
9034e390cabSriastradh 	struct drm_pending_vblank_event *e = NULL;
9044e390cabSriastradh 
9054e390cabSriastradh 	e = kzalloc(sizeof *e, GFP_KERNEL);
9064e390cabSriastradh 	if (!e)
9074e390cabSriastradh 		return NULL;
9084e390cabSriastradh 
9094e390cabSriastradh 	e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
9104e390cabSriastradh 	e->event.base.length = sizeof(e->event);
9114e390cabSriastradh 	e->event.vbl.crtc_id = crtc->base.id;
9124e390cabSriastradh 	e->event.vbl.user_data = user_data;
9134e390cabSriastradh 
9144e390cabSriastradh 	return e;
9154e390cabSriastradh }
9164e390cabSriastradh 
drm_atomic_connector_commit_dpms(struct drm_atomic_state * state,struct drm_connector * connector,int mode)9174e390cabSriastradh int drm_atomic_connector_commit_dpms(struct drm_atomic_state *state,
9184e390cabSriastradh 				     struct drm_connector *connector,
9194e390cabSriastradh 				     int mode)
9204e390cabSriastradh {
9214e390cabSriastradh 	struct drm_connector *tmp_connector;
9224e390cabSriastradh 	struct drm_connector_state *new_conn_state;
9234e390cabSriastradh 	struct drm_crtc *crtc;
9244e390cabSriastradh 	struct drm_crtc_state *crtc_state;
9254e390cabSriastradh 	int i, ret, old_mode = connector->dpms;
9264e390cabSriastradh 	bool active = false;
9274e390cabSriastradh 
9284e390cabSriastradh 	ret = drm_modeset_lock(&state->dev->mode_config.connection_mutex,
9294e390cabSriastradh 			       state->acquire_ctx);
9304e390cabSriastradh 	if (ret)
9314e390cabSriastradh 		return ret;
9324e390cabSriastradh 
9334e390cabSriastradh 	if (mode != DRM_MODE_DPMS_ON)
9344e390cabSriastradh 		mode = DRM_MODE_DPMS_OFF;
9354e390cabSriastradh 	connector->dpms = mode;
9364e390cabSriastradh 
9374e390cabSriastradh 	crtc = connector->state->crtc;
9384e390cabSriastradh 	if (!crtc)
9394e390cabSriastradh 		goto out;
9404e390cabSriastradh 	ret = drm_atomic_add_affected_connectors(state, crtc);
9414e390cabSriastradh 	if (ret)
9424e390cabSriastradh 		goto out;
9434e390cabSriastradh 
9444e390cabSriastradh 	crtc_state = drm_atomic_get_crtc_state(state, crtc);
9454e390cabSriastradh 	if (IS_ERR(crtc_state)) {
9464e390cabSriastradh 		ret = PTR_ERR(crtc_state);
9474e390cabSriastradh 		goto out;
9484e390cabSriastradh 	}
9494e390cabSriastradh 
9504e390cabSriastradh 	for_each_new_connector_in_state(state, tmp_connector, new_conn_state, i) {
9514e390cabSriastradh 		if (new_conn_state->crtc != crtc)
9524e390cabSriastradh 			continue;
9534e390cabSriastradh 		if (tmp_connector->dpms == DRM_MODE_DPMS_ON) {
9544e390cabSriastradh 			active = true;
9554e390cabSriastradh 			break;
9564e390cabSriastradh 		}
9574e390cabSriastradh 	}
9584e390cabSriastradh 
9594e390cabSriastradh 	crtc_state->active = active;
9604e390cabSriastradh 	ret = drm_atomic_commit(state);
9614e390cabSriastradh out:
9624e390cabSriastradh 	if (ret != 0)
9634e390cabSriastradh 		connector->dpms = old_mode;
9644e390cabSriastradh 	return ret;
9654e390cabSriastradh }
9664e390cabSriastradh 
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)9674e390cabSriastradh int drm_atomic_set_property(struct drm_atomic_state *state,
9684e390cabSriastradh 			    struct drm_file *file_priv,
9694e390cabSriastradh 			    struct drm_mode_object *obj,
9704e390cabSriastradh 			    struct drm_property *prop,
9714e390cabSriastradh 			    uint64_t prop_value)
9724e390cabSriastradh {
9734e390cabSriastradh 	struct drm_mode_object *ref;
9744e390cabSriastradh 	int ret;
9754e390cabSriastradh 
9764e390cabSriastradh 	if (!drm_property_change_valid_get(prop, prop_value, &ref))
9774e390cabSriastradh 		return -EINVAL;
9784e390cabSriastradh 
9794e390cabSriastradh 	switch (obj->type) {
9804e390cabSriastradh 	case DRM_MODE_OBJECT_CONNECTOR: {
9814e390cabSriastradh 		struct drm_connector *connector = obj_to_connector(obj);
9824e390cabSriastradh 		struct drm_connector_state *connector_state;
9834e390cabSriastradh 
9844e390cabSriastradh 		connector_state = drm_atomic_get_connector_state(state, connector);
9854e390cabSriastradh 		if (IS_ERR(connector_state)) {
9864e390cabSriastradh 			ret = PTR_ERR(connector_state);
9874e390cabSriastradh 			break;
9884e390cabSriastradh 		}
9894e390cabSriastradh 
9904e390cabSriastradh 		ret = drm_atomic_connector_set_property(connector,
9914e390cabSriastradh 				connector_state, file_priv,
9924e390cabSriastradh 				prop, prop_value);
9934e390cabSriastradh 		break;
9944e390cabSriastradh 	}
9954e390cabSriastradh 	case DRM_MODE_OBJECT_CRTC: {
9964e390cabSriastradh 		struct drm_crtc *crtc = obj_to_crtc(obj);
9974e390cabSriastradh 		struct drm_crtc_state *crtc_state;
9984e390cabSriastradh 
9994e390cabSriastradh 		crtc_state = drm_atomic_get_crtc_state(state, crtc);
10004e390cabSriastradh 		if (IS_ERR(crtc_state)) {
10014e390cabSriastradh 			ret = PTR_ERR(crtc_state);
10024e390cabSriastradh 			break;
10034e390cabSriastradh 		}
10044e390cabSriastradh 
10054e390cabSriastradh 		ret = drm_atomic_crtc_set_property(crtc,
10064e390cabSriastradh 				crtc_state, prop, prop_value);
10074e390cabSriastradh 		break;
10084e390cabSriastradh 	}
10094e390cabSriastradh 	case DRM_MODE_OBJECT_PLANE: {
10104e390cabSriastradh 		struct drm_plane *plane = obj_to_plane(obj);
10114e390cabSriastradh 		struct drm_plane_state *plane_state;
10124e390cabSriastradh 
10134e390cabSriastradh 		plane_state = drm_atomic_get_plane_state(state, plane);
10144e390cabSriastradh 		if (IS_ERR(plane_state)) {
10154e390cabSriastradh 			ret = PTR_ERR(plane_state);
10164e390cabSriastradh 			break;
10174e390cabSriastradh 		}
10184e390cabSriastradh 
10194e390cabSriastradh 		ret = drm_atomic_plane_set_property(plane,
10204e390cabSriastradh 				plane_state, file_priv,
10214e390cabSriastradh 				prop, prop_value);
10224e390cabSriastradh 		break;
10234e390cabSriastradh 	}
10244e390cabSriastradh 	default:
10254e390cabSriastradh 		ret = -EINVAL;
10264e390cabSriastradh 		break;
10274e390cabSriastradh 	}
10284e390cabSriastradh 
10294e390cabSriastradh 	drm_property_change_valid_put(prop, ref);
10304e390cabSriastradh 	return ret;
10314e390cabSriastradh }
10324e390cabSriastradh 
10334e390cabSriastradh /**
10344e390cabSriastradh  * DOC: explicit fencing properties
10354e390cabSriastradh  *
10364e390cabSriastradh  * Explicit fencing allows userspace to control the buffer synchronization
10374e390cabSriastradh  * between devices. A Fence or a group of fences are transfered to/from
10384e390cabSriastradh  * userspace using Sync File fds and there are two DRM properties for that.
10394e390cabSriastradh  * IN_FENCE_FD on each DRM Plane to send fences to the kernel and
10404e390cabSriastradh  * OUT_FENCE_PTR on each DRM CRTC to receive fences from the kernel.
10414e390cabSriastradh  *
10424e390cabSriastradh  * As a contrast, with implicit fencing the kernel keeps track of any
10434e390cabSriastradh  * ongoing rendering, and automatically ensures that the atomic update waits
10444e390cabSriastradh  * for any pending rendering to complete. For shared buffers represented with
10454e390cabSriastradh  * a &struct dma_buf this is tracked in &struct dma_resv.
10464e390cabSriastradh  * Implicit syncing is how Linux traditionally worked (e.g. DRI2/3 on X.org),
10474e390cabSriastradh  * whereas explicit fencing is what Android wants.
10484e390cabSriastradh  *
10494e390cabSriastradh  * "IN_FENCE_FD”:
10504e390cabSriastradh  *	Use this property to pass a fence that DRM should wait on before
10514e390cabSriastradh  *	proceeding with the Atomic Commit request and show the framebuffer for
10524e390cabSriastradh  *	the plane on the screen. The fence can be either a normal fence or a
10534e390cabSriastradh  *	merged one, the sync_file framework will handle both cases and use a
10544e390cabSriastradh  *	fence_array if a merged fence is received. Passing -1 here means no
10554e390cabSriastradh  *	fences to wait on.
10564e390cabSriastradh  *
10574e390cabSriastradh  *	If the Atomic Commit request has the DRM_MODE_ATOMIC_TEST_ONLY flag
10584e390cabSriastradh  *	it will only check if the Sync File is a valid one.
10594e390cabSriastradh  *
10604e390cabSriastradh  *	On the driver side the fence is stored on the @fence parameter of
10614e390cabSriastradh  *	&struct drm_plane_state. Drivers which also support implicit fencing
10624e390cabSriastradh  *	should set the implicit fence using drm_atomic_set_fence_for_plane(),
10634e390cabSriastradh  *	to make sure there's consistent behaviour between drivers in precedence
10644e390cabSriastradh  *	of implicit vs. explicit fencing.
10654e390cabSriastradh  *
10664e390cabSriastradh  * "OUT_FENCE_PTR”:
10674e390cabSriastradh  *	Use this property to pass a file descriptor pointer to DRM. Once the
10684e390cabSriastradh  *	Atomic Commit request call returns OUT_FENCE_PTR will be filled with
10694e390cabSriastradh  *	the file descriptor number of a Sync File. This Sync File contains the
10704e390cabSriastradh  *	CRTC fence that will be signaled when all framebuffers present on the
10714e390cabSriastradh  *	Atomic Commit * request for that given CRTC are scanned out on the
10724e390cabSriastradh  *	screen.
10734e390cabSriastradh  *
10744e390cabSriastradh  *	The Atomic Commit request fails if a invalid pointer is passed. If the
10754e390cabSriastradh  *	Atomic Commit request fails for any other reason the out fence fd
10764e390cabSriastradh  *	returned will be -1. On a Atomic Commit with the
10774e390cabSriastradh  *	DRM_MODE_ATOMIC_TEST_ONLY flag the out fence will also be set to -1.
10784e390cabSriastradh  *
10794e390cabSriastradh  *	Note that out-fences don't have a special interface to drivers and are
10804e390cabSriastradh  *	internally represented by a &struct drm_pending_vblank_event in struct
10814e390cabSriastradh  *	&drm_crtc_state, which is also used by the nonblocking atomic commit
10824e390cabSriastradh  *	helpers and for the DRM event handling for existing userspace.
10834e390cabSriastradh  */
10844e390cabSriastradh 
10854e390cabSriastradh struct drm_out_fence_state {
10864e390cabSriastradh 	s32 __user *out_fence_ptr;
10874e390cabSriastradh 	struct sync_file *sync_file;
10884e390cabSriastradh 	int fd;
10894e390cabSriastradh };
10904e390cabSriastradh 
setup_out_fence(struct drm_out_fence_state * fence_state,struct dma_fence * fence)10914e390cabSriastradh static int setup_out_fence(struct drm_out_fence_state *fence_state,
10924e390cabSriastradh 			   struct dma_fence *fence)
10934e390cabSriastradh {
1094b8914d8dSriastradh #ifdef __NetBSD__
1095b8914d8dSriastradh 	int fd = -1;
1096b8914d8dSriastradh 	struct file *fp = NULL;
1097b8914d8dSriastradh 	int ret;
1098b8914d8dSriastradh 
1099b8914d8dSriastradh 	/* Allocate a file descriptor.	*/
1100b8914d8dSriastradh 	/* XXX errno NetBSD->Linux */
1101b8914d8dSriastradh 	ret = -fd_allocfile(&fp, &fd);
1102b8914d8dSriastradh 	if (ret)
1103b8914d8dSriastradh 		goto out;
1104b8914d8dSriastradh 
1105b8914d8dSriastradh 	/* Prepare to transmit it to user.  */
1106b8914d8dSriastradh 	/* XXX errno NetBSD->Linux */
11076b695b16Sriastradh 	ret = -copyout(&fd, fence_state->out_fence_ptr, sizeof fd);
1108b8914d8dSriastradh 	if (ret)
1109b8914d8dSriastradh 		goto out;
1110b8914d8dSriastradh 
1111b8914d8dSriastradh 	/* Create sync file.  */
1112b8914d8dSriastradh 	fence_state->sync_file = sync_file_create(fence, fp);
1113b8914d8dSriastradh 	if (fence_state->sync_file == NULL) {
1114b8914d8dSriastradh 		ret = -ENOMEM;
1115b8914d8dSriastradh 		goto out;
1116b8914d8dSriastradh 	}
1117*d9577045Sriastradh 	fd_affix(curproc, fp, fd);
1118b8914d8dSriastradh 	fp = NULL;		/* sync_file consumes */
1119b8914d8dSriastradh 
1120b8914d8dSriastradh out:	if (fp != NULL) {
1121b8914d8dSriastradh 		fd_abort(curproc, fp, fd);
1122b8914d8dSriastradh 		fd = -1;
1123b8914d8dSriastradh 	}
1124b8914d8dSriastradh 	fence_state->fd = fd;
1125b8914d8dSriastradh 	return ret;
1126b8914d8dSriastradh #else
11274e390cabSriastradh 	fence_state->fd = get_unused_fd_flags(O_CLOEXEC);
11284e390cabSriastradh 	if (fence_state->fd < 0)
11294e390cabSriastradh 		return fence_state->fd;
11304e390cabSriastradh 
11314e390cabSriastradh 	if (put_user(fence_state->fd, fence_state->out_fence_ptr))
11324e390cabSriastradh 		return -EFAULT;
11334e390cabSriastradh 
11344e390cabSriastradh 	fence_state->sync_file = sync_file_create(fence);
11354e390cabSriastradh 	if (!fence_state->sync_file)
11364e390cabSriastradh 		return -ENOMEM;
11374e390cabSriastradh 
11384e390cabSriastradh 	return 0;
1139b8914d8dSriastradh #endif
11404e390cabSriastradh }
11414e390cabSriastradh 
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)11424e390cabSriastradh static int prepare_signaling(struct drm_device *dev,
11434e390cabSriastradh 				  struct drm_atomic_state *state,
11444e390cabSriastradh 				  struct drm_mode_atomic *arg,
11454e390cabSriastradh 				  struct drm_file *file_priv,
11464e390cabSriastradh 				  struct drm_out_fence_state **fence_state,
11474e390cabSriastradh 				  unsigned int *num_fences)
11484e390cabSriastradh {
11494e390cabSriastradh 	struct drm_crtc *crtc;
11504e390cabSriastradh 	struct drm_crtc_state *crtc_state;
11514e390cabSriastradh 	struct drm_connector *conn;
11524e390cabSriastradh 	struct drm_connector_state *conn_state;
11534e390cabSriastradh 	int i, c = 0, ret;
11544e390cabSriastradh 
11554e390cabSriastradh 	if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)
11564e390cabSriastradh 		return 0;
11574e390cabSriastradh 
11584e390cabSriastradh 	for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
11594e390cabSriastradh 		s32 __user *fence_ptr;
11604e390cabSriastradh 
11614e390cabSriastradh 		fence_ptr = get_out_fence_for_crtc(crtc_state->state, crtc);
11624e390cabSriastradh 
11634e390cabSriastradh 		if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT || fence_ptr) {
11644e390cabSriastradh 			struct drm_pending_vblank_event *e;
11654e390cabSriastradh 
11664e390cabSriastradh 			e = create_vblank_event(crtc, arg->user_data);
11674e390cabSriastradh 			if (!e)
11684e390cabSriastradh 				return -ENOMEM;
11694e390cabSriastradh 
11704e390cabSriastradh 			crtc_state->event = e;
11714e390cabSriastradh 		}
11724e390cabSriastradh 
11734e390cabSriastradh 		if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
11744e390cabSriastradh 			struct drm_pending_vblank_event *e = crtc_state->event;
11754e390cabSriastradh 
11764e390cabSriastradh 			if (!file_priv)
11774e390cabSriastradh 				continue;
11784e390cabSriastradh 
11794e390cabSriastradh 			ret = drm_event_reserve_init(dev, file_priv, &e->base,
11804e390cabSriastradh 						     &e->event.base);
11814e390cabSriastradh 			if (ret) {
11824e390cabSriastradh 				kfree(e);
11834e390cabSriastradh 				crtc_state->event = NULL;
11844e390cabSriastradh 				return ret;
11854e390cabSriastradh 			}
11864e390cabSriastradh 		}
11874e390cabSriastradh 
11884e390cabSriastradh 		if (fence_ptr) {
11894e390cabSriastradh 			struct dma_fence *fence;
11904e390cabSriastradh 			struct drm_out_fence_state *f;
11914e390cabSriastradh 
11924e390cabSriastradh 			f = krealloc(*fence_state, sizeof(**fence_state) *
11934e390cabSriastradh 				     (*num_fences + 1), GFP_KERNEL);
11944e390cabSriastradh 			if (!f)
11954e390cabSriastradh 				return -ENOMEM;
11964e390cabSriastradh 
11974e390cabSriastradh 			memset(&f[*num_fences], 0, sizeof(*f));
11984e390cabSriastradh 
11994e390cabSriastradh 			f[*num_fences].out_fence_ptr = fence_ptr;
12004e390cabSriastradh 			*fence_state = f;
12014e390cabSriastradh 
12024e390cabSriastradh 			fence = drm_crtc_create_fence(crtc);
12034e390cabSriastradh 			if (!fence)
12044e390cabSriastradh 				return -ENOMEM;
12054e390cabSriastradh 
12064e390cabSriastradh 			ret = setup_out_fence(&f[(*num_fences)++], fence);
12074e390cabSriastradh 			if (ret) {
12084e390cabSriastradh 				dma_fence_put(fence);
12094e390cabSriastradh 				return ret;
12104e390cabSriastradh 			}
12114e390cabSriastradh 
12124e390cabSriastradh 			crtc_state->event->base.fence = fence;
12134e390cabSriastradh 		}
12144e390cabSriastradh 
12154e390cabSriastradh 		c++;
12164e390cabSriastradh 	}
12174e390cabSriastradh 
12184e390cabSriastradh 	for_each_new_connector_in_state(state, conn, conn_state, i) {
12194e390cabSriastradh 		struct drm_writeback_connector *wb_conn;
12204e390cabSriastradh 		struct drm_out_fence_state *f;
12214e390cabSriastradh 		struct dma_fence *fence;
12224e390cabSriastradh 		s32 __user *fence_ptr;
12234e390cabSriastradh 
12244e390cabSriastradh 		if (!conn_state->writeback_job)
12254e390cabSriastradh 			continue;
12264e390cabSriastradh 
12274e390cabSriastradh 		fence_ptr = get_out_fence_for_connector(state, conn);
12284e390cabSriastradh 		if (!fence_ptr)
12294e390cabSriastradh 			continue;
12304e390cabSriastradh 
12314e390cabSriastradh 		f = krealloc(*fence_state, sizeof(**fence_state) *
12324e390cabSriastradh 			     (*num_fences + 1), GFP_KERNEL);
12334e390cabSriastradh 		if (!f)
12344e390cabSriastradh 			return -ENOMEM;
12354e390cabSriastradh 
12364e390cabSriastradh 		memset(&f[*num_fences], 0, sizeof(*f));
12374e390cabSriastradh 
12384e390cabSriastradh 		f[*num_fences].out_fence_ptr = fence_ptr;
12394e390cabSriastradh 		*fence_state = f;
12404e390cabSriastradh 
12414e390cabSriastradh 		wb_conn = drm_connector_to_writeback(conn);
12424e390cabSriastradh 		fence = drm_writeback_get_out_fence(wb_conn);
12434e390cabSriastradh 		if (!fence)
12444e390cabSriastradh 			return -ENOMEM;
12454e390cabSriastradh 
12464e390cabSriastradh 		ret = setup_out_fence(&f[(*num_fences)++], fence);
12474e390cabSriastradh 		if (ret) {
12484e390cabSriastradh 			dma_fence_put(fence);
12494e390cabSriastradh 			return ret;
12504e390cabSriastradh 		}
12514e390cabSriastradh 
12524e390cabSriastradh 		conn_state->writeback_job->out_fence = fence;
12534e390cabSriastradh 	}
12544e390cabSriastradh 
12554e390cabSriastradh 	/*
12564e390cabSriastradh 	 * Having this flag means user mode pends on event which will never
12574e390cabSriastradh 	 * reach due to lack of at least one CRTC for signaling
12584e390cabSriastradh 	 */
12594e390cabSriastradh 	if (c == 0 && (arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
12604e390cabSriastradh 		return -EINVAL;
12614e390cabSriastradh 
12624e390cabSriastradh 	return 0;
12634e390cabSriastradh }
12644e390cabSriastradh 
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)12654e390cabSriastradh static void complete_signaling(struct drm_device *dev,
12664e390cabSriastradh 			       struct drm_atomic_state *state,
12674e390cabSriastradh 			       struct drm_out_fence_state *fence_state,
12684e390cabSriastradh 			       unsigned int num_fences,
12694e390cabSriastradh 			       bool install_fds)
12704e390cabSriastradh {
12714e390cabSriastradh 	struct drm_crtc *crtc;
12724e390cabSriastradh 	struct drm_crtc_state *crtc_state;
12734e390cabSriastradh 	int i;
12744e390cabSriastradh 
12754e390cabSriastradh 	if (install_fds) {
12764e390cabSriastradh 		for (i = 0; i < num_fences; i++)
12774e390cabSriastradh 			fd_install(fence_state[i].fd,
12784e390cabSriastradh 				   fence_state[i].sync_file->file);
12794e390cabSriastradh 
12804e390cabSriastradh 		kfree(fence_state);
12814e390cabSriastradh 		return;
12824e390cabSriastradh 	}
12834e390cabSriastradh 
12844e390cabSriastradh 	for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
12854e390cabSriastradh 		struct drm_pending_vblank_event *event = crtc_state->event;
12864e390cabSriastradh 		/*
12874e390cabSriastradh 		 * Free the allocated event. drm_atomic_helper_setup_commit
12884e390cabSriastradh 		 * can allocate an event too, so only free it if it's ours
12894e390cabSriastradh 		 * to prevent a double free in drm_atomic_state_clear.
12904e390cabSriastradh 		 */
12914e390cabSriastradh 		if (event && (event->base.fence || event->base.file_priv)) {
12924e390cabSriastradh 			drm_event_cancel_free(dev, &event->base);
12934e390cabSriastradh 			crtc_state->event = NULL;
12944e390cabSriastradh 		}
12954e390cabSriastradh 	}
12964e390cabSriastradh 
12974e390cabSriastradh 	if (!fence_state)
12984e390cabSriastradh 		return;
12994e390cabSriastradh 
13004e390cabSriastradh 	for (i = 0; i < num_fences; i++) {
1301b8914d8dSriastradh #ifdef __NetBSD__
1302b8914d8dSriastradh 		if (fd_getfile(fence_state[i].fd))
1303b8914d8dSriastradh 			(void)fd_close(fence_state[i].fd);
1304b8914d8dSriastradh #else
13054e390cabSriastradh 		if (fence_state[i].sync_file)
13064e390cabSriastradh 			fput(fence_state[i].sync_file->file);
13074e390cabSriastradh 		if (fence_state[i].fd >= 0)
13084e390cabSriastradh 			put_unused_fd(fence_state[i].fd);
1309b8914d8dSriastradh #endif
13104e390cabSriastradh 
13114e390cabSriastradh 		/* If this fails log error to the user */
13124e390cabSriastradh 		if (fence_state[i].out_fence_ptr &&
13134e390cabSriastradh 		    put_user(-1, fence_state[i].out_fence_ptr))
13144e390cabSriastradh 			DRM_DEBUG_ATOMIC("Couldn't clear out_fence_ptr\n");
13154e390cabSriastradh 	}
13164e390cabSriastradh 
13174e390cabSriastradh 	kfree(fence_state);
13184e390cabSriastradh }
13194e390cabSriastradh 
drm_mode_atomic_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)13204e390cabSriastradh int drm_mode_atomic_ioctl(struct drm_device *dev,
13214e390cabSriastradh 			  void *data, struct drm_file *file_priv)
13224e390cabSriastradh {
13234e390cabSriastradh 	struct drm_mode_atomic *arg = data;
13244e390cabSriastradh 	uint32_t __user *objs_ptr = (uint32_t __user *)(unsigned long)(arg->objs_ptr);
13254e390cabSriastradh 	uint32_t __user *count_props_ptr = (uint32_t __user *)(unsigned long)(arg->count_props_ptr);
13264e390cabSriastradh 	uint32_t __user *props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
13274e390cabSriastradh 	uint64_t __user *prop_values_ptr = (uint64_t __user *)(unsigned long)(arg->prop_values_ptr);
13284e390cabSriastradh 	unsigned int copied_objs, copied_props;
13294e390cabSriastradh 	struct drm_atomic_state *state;
13304e390cabSriastradh 	struct drm_modeset_acquire_ctx ctx;
13314e390cabSriastradh 	struct drm_out_fence_state *fence_state;
13324e390cabSriastradh 	int ret = 0;
13334e390cabSriastradh 	unsigned int i, j, num_fences;
13344e390cabSriastradh 
13354e390cabSriastradh 	/* disallow for drivers not supporting atomic: */
13364e390cabSriastradh 	if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
13374e390cabSriastradh 		return -EOPNOTSUPP;
13384e390cabSriastradh 
13394e390cabSriastradh 	/* disallow for userspace that has not enabled atomic cap (even
13404e390cabSriastradh 	 * though this may be a bit overkill, since legacy userspace
13414e390cabSriastradh 	 * wouldn't know how to call this ioctl)
13424e390cabSriastradh 	 */
13434e390cabSriastradh 	if (!file_priv->atomic)
13444e390cabSriastradh 		return -EINVAL;
13454e390cabSriastradh 
13464e390cabSriastradh 	if (arg->flags & ~DRM_MODE_ATOMIC_FLAGS)
13474e390cabSriastradh 		return -EINVAL;
13484e390cabSriastradh 
13494e390cabSriastradh 	if (arg->reserved)
13504e390cabSriastradh 		return -EINVAL;
13514e390cabSriastradh 
13524e390cabSriastradh 	if (arg->flags & DRM_MODE_PAGE_FLIP_ASYNC)
13534e390cabSriastradh 		return -EINVAL;
13544e390cabSriastradh 
13554e390cabSriastradh 	/* can't test and expect an event at the same time. */
13564e390cabSriastradh 	if ((arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) &&
13574e390cabSriastradh 			(arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
13584e390cabSriastradh 		return -EINVAL;
13594e390cabSriastradh 
13604e390cabSriastradh 	state = drm_atomic_state_alloc(dev);
13614e390cabSriastradh 	if (!state)
13624e390cabSriastradh 		return -ENOMEM;
13634e390cabSriastradh 
13644e390cabSriastradh 	drm_modeset_acquire_init(&ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE);
13654e390cabSriastradh 	state->acquire_ctx = &ctx;
13664e390cabSriastradh 	state->allow_modeset = !!(arg->flags & DRM_MODE_ATOMIC_ALLOW_MODESET);
13674e390cabSriastradh 
13684e390cabSriastradh retry:
13694e390cabSriastradh 	copied_objs = 0;
13704e390cabSriastradh 	copied_props = 0;
13714e390cabSriastradh 	fence_state = NULL;
13724e390cabSriastradh 	num_fences = 0;
13734e390cabSriastradh 
13744e390cabSriastradh 	for (i = 0; i < arg->count_objs; i++) {
13754e390cabSriastradh 		uint32_t obj_id, count_props;
13764e390cabSriastradh 		struct drm_mode_object *obj;
13774e390cabSriastradh 
13784e390cabSriastradh 		if (get_user(obj_id, objs_ptr + copied_objs)) {
13794e390cabSriastradh 			ret = -EFAULT;
13804e390cabSriastradh 			goto out;
13814e390cabSriastradh 		}
13824e390cabSriastradh 
13834e390cabSriastradh 		obj = drm_mode_object_find(dev, file_priv, obj_id, DRM_MODE_OBJECT_ANY);
13844e390cabSriastradh 		if (!obj) {
13854e390cabSriastradh 			ret = -ENOENT;
13864e390cabSriastradh 			goto out;
13874e390cabSriastradh 		}
13884e390cabSriastradh 
13894e390cabSriastradh 		if (!obj->properties) {
13904e390cabSriastradh 			drm_mode_object_put(obj);
13914e390cabSriastradh 			ret = -ENOENT;
13924e390cabSriastradh 			goto out;
13934e390cabSriastradh 		}
13944e390cabSriastradh 
13954e390cabSriastradh 		if (get_user(count_props, count_props_ptr + copied_objs)) {
13964e390cabSriastradh 			drm_mode_object_put(obj);
13974e390cabSriastradh 			ret = -EFAULT;
13984e390cabSriastradh 			goto out;
13994e390cabSriastradh 		}
14004e390cabSriastradh 
14014e390cabSriastradh 		copied_objs++;
14024e390cabSriastradh 
14034e390cabSriastradh 		for (j = 0; j < count_props; j++) {
14044e390cabSriastradh 			uint32_t prop_id;
14054e390cabSriastradh 			uint64_t prop_value;
14064e390cabSriastradh 			struct drm_property *prop;
14074e390cabSriastradh 
14084e390cabSriastradh 			if (get_user(prop_id, props_ptr + copied_props)) {
14094e390cabSriastradh 				drm_mode_object_put(obj);
14104e390cabSriastradh 				ret = -EFAULT;
14114e390cabSriastradh 				goto out;
14124e390cabSriastradh 			}
14134e390cabSriastradh 
14144e390cabSriastradh 			prop = drm_mode_obj_find_prop_id(obj, prop_id);
14154e390cabSriastradh 			if (!prop) {
14164e390cabSriastradh 				drm_mode_object_put(obj);
14174e390cabSriastradh 				ret = -ENOENT;
14184e390cabSriastradh 				goto out;
14194e390cabSriastradh 			}
14204e390cabSriastradh 
14214e390cabSriastradh 			if (copy_from_user(&prop_value,
14224e390cabSriastradh 					   prop_values_ptr + copied_props,
14234e390cabSriastradh 					   sizeof(prop_value))) {
14244e390cabSriastradh 				drm_mode_object_put(obj);
14254e390cabSriastradh 				ret = -EFAULT;
14264e390cabSriastradh 				goto out;
14274e390cabSriastradh 			}
14284e390cabSriastradh 
14294e390cabSriastradh 			ret = drm_atomic_set_property(state, file_priv,
14304e390cabSriastradh 						      obj, prop, prop_value);
14314e390cabSriastradh 			if (ret) {
14324e390cabSriastradh 				drm_mode_object_put(obj);
14334e390cabSriastradh 				goto out;
14344e390cabSriastradh 			}
14354e390cabSriastradh 
14364e390cabSriastradh 			copied_props++;
14374e390cabSriastradh 		}
14384e390cabSriastradh 
14394e390cabSriastradh 		drm_mode_object_put(obj);
14404e390cabSriastradh 	}
14414e390cabSriastradh 
14424e390cabSriastradh 	ret = prepare_signaling(dev, state, arg, file_priv, &fence_state,
14434e390cabSriastradh 				&num_fences);
14444e390cabSriastradh 	if (ret)
14454e390cabSriastradh 		goto out;
14464e390cabSriastradh 
14474e390cabSriastradh 	if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) {
14484e390cabSriastradh 		ret = drm_atomic_check_only(state);
14494e390cabSriastradh 	} else if (arg->flags & DRM_MODE_ATOMIC_NONBLOCK) {
14504e390cabSriastradh 		ret = drm_atomic_nonblocking_commit(state);
14514e390cabSriastradh 	} else {
14524e390cabSriastradh 		if (drm_debug_enabled(DRM_UT_STATE))
14534e390cabSriastradh 			drm_atomic_print_state(state);
14544e390cabSriastradh 
14554e390cabSriastradh 		ret = drm_atomic_commit(state);
14564e390cabSriastradh 	}
14574e390cabSriastradh 
14584e390cabSriastradh out:
14594e390cabSriastradh 	complete_signaling(dev, state, fence_state, num_fences, !ret);
14604e390cabSriastradh 
14614e390cabSriastradh 	if (ret == -EDEADLK) {
14624e390cabSriastradh 		drm_atomic_state_clear(state);
14634e390cabSriastradh 		ret = drm_modeset_backoff(&ctx);
14644e390cabSriastradh 		if (!ret)
14654e390cabSriastradh 			goto retry;
14664e390cabSriastradh 	}
14674e390cabSriastradh 
14684e390cabSriastradh 	drm_atomic_state_put(state);
14694e390cabSriastradh 
14704e390cabSriastradh 	drm_modeset_drop_locks(&ctx);
14714e390cabSriastradh 	drm_modeset_acquire_fini(&ctx);
14724e390cabSriastradh 
14734e390cabSriastradh 	return ret;
14744e390cabSriastradh }
1475