Linux Audio

Check our new training course

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