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