drm_atomic.c 80.9 KB
Newer Older
D
Daniel Vetter 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/*
 * Copyright (C) 2014 Red Hat
 * Copyright (C) 2014 Intel Corp.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * Authors:
 * Rob Clark <robdclark@gmail.com>
 * Daniel Vetter <daniel.vetter@ffwll.ch>
 */


#include <drm/drmP.h>
#include <drm/drm_atomic.h>
31
#include <drm/drm_mode.h>
32
#include <drm/drm_print.h>
33
#include <drm/drm_writeback.h>
G
Gustavo Padovan 已提交
34
#include <linux/sync_file.h>
D
Daniel Vetter 已提交
35

36
#include "drm_crtc_internal.h"
37
#include "drm_internal.h"
38

39
void __drm_crtc_commit_free(struct kref *kref)
40 41 42 43 44 45
{
	struct drm_crtc_commit *commit =
		container_of(kref, struct drm_crtc_commit, ref);

	kfree(commit);
}
46
EXPORT_SYMBOL(__drm_crtc_commit_free);
47

48 49 50 51 52 53
/**
 * drm_atomic_state_default_release -
 * release memory initialized by drm_atomic_state_init
 * @state: atomic state
 *
 * Free all the memory allocated by drm_atomic_state_init.
54 55
 * This should only be used by drivers which are still subclassing
 * &drm_atomic_state and haven't switched to &drm_private_state yet.
56 57
 */
void drm_atomic_state_default_release(struct drm_atomic_state *state)
D
Daniel Vetter 已提交
58 59 60 61
{
	kfree(state->connectors);
	kfree(state->crtcs);
	kfree(state->planes);
62
	kfree(state->private_objs);
D
Daniel Vetter 已提交
63
}
64
EXPORT_SYMBOL(drm_atomic_state_default_release);
D
Daniel Vetter 已提交
65 66

/**
67
 * drm_atomic_state_init - init new atomic state
D
Daniel Vetter 已提交
68
 * @dev: DRM device
69
 * @state: atomic state
D
Daniel Vetter 已提交
70
 *
71
 * Default implementation for filling in a new atomic state.
72 73
 * This should only be used by drivers which are still subclassing
 * &drm_atomic_state and haven't switched to &drm_private_state yet.
D
Daniel Vetter 已提交
74
 */
75 76
int
drm_atomic_state_init(struct drm_device *dev, struct drm_atomic_state *state)
D
Daniel Vetter 已提交
77
{
78 79
	kref_init(&state->ref);

R
Rob Clark 已提交
80 81 82 83 84
	/* TODO legacy paths should maybe do a better job about
	 * setting this appropriately?
	 */
	state->allow_modeset = true;

D
Daniel Vetter 已提交
85 86 87 88 89 90 91 92 93 94 95
	state->crtcs = kcalloc(dev->mode_config.num_crtc,
			       sizeof(*state->crtcs), GFP_KERNEL);
	if (!state->crtcs)
		goto fail;
	state->planes = kcalloc(dev->mode_config.num_total_plane,
				sizeof(*state->planes), GFP_KERNEL);
	if (!state->planes)
		goto fail;

	state->dev = dev;

96
	DRM_DEBUG_ATOMIC("Allocated atomic state %p\n", state);
D
Daniel Vetter 已提交
97

98
	return 0;
D
Daniel Vetter 已提交
99
fail:
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
	drm_atomic_state_default_release(state);
	return -ENOMEM;
}
EXPORT_SYMBOL(drm_atomic_state_init);

/**
 * drm_atomic_state_alloc - allocate atomic state
 * @dev: DRM device
 *
 * This allocates an empty atomic state to track updates.
 */
struct drm_atomic_state *
drm_atomic_state_alloc(struct drm_device *dev)
{
	struct drm_mode_config *config = &dev->mode_config;

	if (!config->funcs->atomic_state_alloc) {
117 118
		struct drm_atomic_state *state;

119 120 121 122 123 124 125 126 127
		state = kzalloc(sizeof(*state), GFP_KERNEL);
		if (!state)
			return NULL;
		if (drm_atomic_state_init(dev, state) < 0) {
			kfree(state);
			return NULL;
		}
		return state;
	}
D
Daniel Vetter 已提交
128

129
	return config->funcs->atomic_state_alloc(dev);
D
Daniel Vetter 已提交
130 131 132 133
}
EXPORT_SYMBOL(drm_atomic_state_alloc);

/**
134
 * drm_atomic_state_default_clear - clear base atomic state
D
Daniel Vetter 已提交
135 136
 * @state: atomic state
 *
137
 * Default implementation for clearing atomic state.
138 139
 * This should only be used by drivers which are still subclassing
 * &drm_atomic_state and haven't switched to &drm_private_state yet.
D
Daniel Vetter 已提交
140
 */
141
void drm_atomic_state_default_clear(struct drm_atomic_state *state)
D
Daniel Vetter 已提交
142 143
{
	struct drm_device *dev = state->dev;
144
	struct drm_mode_config *config = &dev->mode_config;
D
Daniel Vetter 已提交
145 146
	int i;

D
Daniel Vetter 已提交
147
	DRM_DEBUG_ATOMIC("Clearing atomic state %p\n", state);
D
Daniel Vetter 已提交
148

149
	for (i = 0; i < state->num_connector; i++) {
150
		struct drm_connector *connector = state->connectors[i].ptr;
D
Daniel Vetter 已提交
151 152 153 154

		if (!connector)
			continue;

155
		connector->funcs->atomic_destroy_state(connector,
156 157 158
						       state->connectors[i].state);
		state->connectors[i].ptr = NULL;
		state->connectors[i].state = NULL;
159 160
		state->connectors[i].old_state = NULL;
		state->connectors[i].new_state = NULL;
161
		drm_connector_put(connector);
D
Daniel Vetter 已提交
162 163
	}

164
	for (i = 0; i < config->num_crtc; i++) {
165
		struct drm_crtc *crtc = state->crtcs[i].ptr;
D
Daniel Vetter 已提交
166 167 168 169 170

		if (!crtc)
			continue;

		crtc->funcs->atomic_destroy_state(crtc,
171
						  state->crtcs[i].state);
172

173 174
		state->crtcs[i].ptr = NULL;
		state->crtcs[i].state = NULL;
175 176
		state->crtcs[i].old_state = NULL;
		state->crtcs[i].new_state = NULL;
D
Daniel Vetter 已提交
177 178
	}

179
	for (i = 0; i < config->num_total_plane; i++) {
180
		struct drm_plane *plane = state->planes[i].ptr;
D
Daniel Vetter 已提交
181 182 183 184 185

		if (!plane)
			continue;

		plane->funcs->atomic_destroy_state(plane,
186 187 188
						   state->planes[i].state);
		state->planes[i].ptr = NULL;
		state->planes[i].state = NULL;
189 190
		state->planes[i].old_state = NULL;
		state->planes[i].new_state = NULL;
D
Daniel Vetter 已提交
191
	}
192 193

	for (i = 0; i < state->num_private_objs; i++) {
194
		struct drm_private_obj *obj = state->private_objs[i].ptr;
195

196 197 198 199
		obj->funcs->atomic_destroy_state(obj,
						 state->private_objs[i].state);
		state->private_objs[i].ptr = NULL;
		state->private_objs[i].state = NULL;
200 201
		state->private_objs[i].old_state = NULL;
		state->private_objs[i].new_state = NULL;
202 203 204
	}
	state->num_private_objs = 0;

205 206 207 208
	if (state->fake_commit) {
		drm_crtc_commit_put(state->fake_commit);
		state->fake_commit = NULL;
	}
D
Daniel Vetter 已提交
209
}
210 211 212 213 214 215 216 217 218 219
EXPORT_SYMBOL(drm_atomic_state_default_clear);

/**
 * drm_atomic_state_clear - clear state object
 * @state: atomic state
 *
 * When the w/w mutex algorithm detects a deadlock we need to back off and drop
 * all locks. So someone else could sneak in and change the current modeset
 * configuration. Which means that all the state assembled in @state is no
 * longer an atomic update to the current state, but to some arbitrary earlier
220 221
 * state. Which could break assumptions the driver's
 * &drm_mode_config_funcs.atomic_check likely relies on.
222 223 224 225 226 227 228 229 230 231 232 233 234 235
 *
 * Hence we must clear all cached state and completely start over, using this
 * function.
 */
void drm_atomic_state_clear(struct drm_atomic_state *state)
{
	struct drm_device *dev = state->dev;
	struct drm_mode_config *config = &dev->mode_config;

	if (config->funcs->atomic_state_clear)
		config->funcs->atomic_state_clear(state);
	else
		drm_atomic_state_default_clear(state);
}
D
Daniel Vetter 已提交
236 237 238
EXPORT_SYMBOL(drm_atomic_state_clear);

/**
239 240
 * __drm_atomic_state_free - free all memory for an atomic state
 * @ref: This atomic state to deallocate
D
Daniel Vetter 已提交
241 242 243 244
 *
 * This frees all memory associated with an atomic state, including all the
 * per-object state for planes, crtcs and connectors.
 */
245
void __drm_atomic_state_free(struct kref *ref)
D
Daniel Vetter 已提交
246
{
247 248
	struct drm_atomic_state *state = container_of(ref, typeof(*state), ref);
	struct drm_mode_config *config = &state->dev->mode_config;
249

D
Daniel Vetter 已提交
250 251
	drm_atomic_state_clear(state);

D
Daniel Vetter 已提交
252
	DRM_DEBUG_ATOMIC("Freeing atomic state %p\n", state);
D
Daniel Vetter 已提交
253

254 255 256 257 258 259
	if (config->funcs->atomic_state_free) {
		config->funcs->atomic_state_free(state);
	} else {
		drm_atomic_state_default_release(state);
		kfree(state);
	}
D
Daniel Vetter 已提交
260
}
261
EXPORT_SYMBOL(__drm_atomic_state_free);
D
Daniel Vetter 已提交
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281

/**
 * drm_atomic_get_crtc_state - get crtc state
 * @state: global atomic state object
 * @crtc: crtc to get state object for
 *
 * This function returns the crtc state for the given crtc, allocating it if
 * needed. It will also grab the relevant crtc lock to make sure that the state
 * is consistent.
 *
 * Returns:
 *
 * Either the allocated state or the error code encoded into the pointer. When
 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
 * entire atomic sequence must be restarted. All other errors are fatal.
 */
struct drm_crtc_state *
drm_atomic_get_crtc_state(struct drm_atomic_state *state,
			  struct drm_crtc *crtc)
{
282
	int ret, index = drm_crtc_index(crtc);
D
Daniel Vetter 已提交
283 284
	struct drm_crtc_state *crtc_state;

285 286
	WARN_ON(!state->acquire_ctx);

287 288 289
	crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
	if (crtc_state)
		return crtc_state;
D
Daniel Vetter 已提交
290 291 292 293 294 295 296 297 298

	ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx);
	if (ret)
		return ERR_PTR(ret);

	crtc_state = crtc->funcs->atomic_duplicate_state(crtc);
	if (!crtc_state)
		return ERR_PTR(-ENOMEM);

299
	state->crtcs[index].state = crtc_state;
300 301
	state->crtcs[index].old_state = crtc->state;
	state->crtcs[index].new_state = crtc_state;
302
	state->crtcs[index].ptr = crtc;
D
Daniel Vetter 已提交
303 304
	crtc_state->state = state;

305 306
	DRM_DEBUG_ATOMIC("Added [CRTC:%d:%s] %p state to %p\n",
			 crtc->base.id, crtc->name, crtc_state, state);
D
Daniel Vetter 已提交
307 308 309 310 311

	return crtc_state;
}
EXPORT_SYMBOL(drm_atomic_get_crtc_state);

312
static void set_out_fence_for_crtc(struct drm_atomic_state *state,
313
				   struct drm_crtc *crtc, s32 __user *fence_ptr)
314 315 316 317
{
	state->crtcs[drm_crtc_index(crtc)].out_fence_ptr = fence_ptr;
}

318
static s32 __user *get_out_fence_for_crtc(struct drm_atomic_state *state,
319 320
					  struct drm_crtc *crtc)
{
321
	s32 __user *fence_ptr;
322 323 324 325 326 327 328

	fence_ptr = state->crtcs[drm_crtc_index(crtc)].out_fence_ptr;
	state->crtcs[drm_crtc_index(crtc)].out_fence_ptr = NULL;

	return fence_ptr;
}

329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
static int set_out_fence_for_connector(struct drm_atomic_state *state,
					struct drm_connector *connector,
					s32 __user *fence_ptr)
{
	unsigned int index = drm_connector_index(connector);

	if (!fence_ptr)
		return 0;

	if (put_user(-1, fence_ptr))
		return -EFAULT;

	state->connectors[index].out_fence_ptr = fence_ptr;

	return 0;
}

static s32 __user *get_out_fence_for_connector(struct drm_atomic_state *state,
					       struct drm_connector *connector)
{
	unsigned int index = drm_connector_index(connector);
	s32 __user *fence_ptr;

	fence_ptr = state->connectors[index].out_fence_ptr;
	state->connectors[index].out_fence_ptr = NULL;

	return fence_ptr;
}

358 359 360 361 362
/**
 * drm_atomic_set_mode_for_crtc - set mode for CRTC
 * @state: the CRTC whose incoming state to update
 * @mode: kernel-internal mode to use for the CRTC, or NULL to disable
 *
363 364
 * Set a mode (originating from the kernel) on the desired CRTC state and update
 * the enable property.
365 366 367 368 369
 *
 * RETURNS:
 * Zero on success, error code on failure. Cannot return -EDEADLK.
 */
int drm_atomic_set_mode_for_crtc(struct drm_crtc_state *state,
370
				 const struct drm_display_mode *mode)
371
{
372
	struct drm_crtc *crtc = state->crtc;
373 374
	struct drm_mode_modeinfo umode;

375 376 377 378
	/* Early return for no change. */
	if (mode && memcmp(&state->mode, mode, sizeof(*mode)) == 0)
		return 0;

379
	drm_property_blob_put(state->mode_blob);
380 381
	state->mode_blob = NULL;

382
	if (mode) {
383 384 385 386 387 388 389 390
		drm_mode_convert_to_umode(&umode, mode);
		state->mode_blob =
			drm_property_create_blob(state->crtc->dev,
		                                 sizeof(umode),
		                                 &umode);
		if (IS_ERR(state->mode_blob))
			return PTR_ERR(state->mode_blob);

391 392
		drm_mode_copy(&state->mode, mode);
		state->enable = true;
393 394
		DRM_DEBUG_ATOMIC("Set [MODE:%s] for [CRTC:%d:%s] state %p\n",
				 mode->name, crtc->base.id, crtc->name, state);
395 396 397
	} else {
		memset(&state->mode, 0, sizeof(state->mode));
		state->enable = false;
398 399
		DRM_DEBUG_ATOMIC("Set [NOMODE] for [CRTC:%d:%s] state %p\n",
				 crtc->base.id, crtc->name, state);
400 401 402 403 404 405
	}

	return 0;
}
EXPORT_SYMBOL(drm_atomic_set_mode_for_crtc);

D
Daniel Stone 已提交
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
/**
 * drm_atomic_set_mode_prop_for_crtc - set mode for CRTC
 * @state: the CRTC whose incoming state to update
 * @blob: pointer to blob property to use for mode
 *
 * Set a mode (originating from a blob property) on the desired CRTC state.
 * This function will take a reference on the blob property for the CRTC state,
 * and release the reference held on the state's existing mode property, if any
 * was set.
 *
 * RETURNS:
 * Zero on success, error code on failure. Cannot return -EDEADLK.
 */
int drm_atomic_set_mode_prop_for_crtc(struct drm_crtc_state *state,
                                      struct drm_property_blob *blob)
{
422 423
	struct drm_crtc *crtc = state->crtc;

D
Daniel Stone 已提交
424 425 426
	if (blob == state->mode_blob)
		return 0;

427
	drm_property_blob_put(state->mode_blob);
D
Daniel Stone 已提交
428 429
	state->mode_blob = NULL;

430 431
	memset(&state->mode, 0, sizeof(state->mode));

D
Daniel Stone 已提交
432
	if (blob) {
V
Ville Syrjälä 已提交
433 434 435 436 437 438
		int ret;

		if (blob->length != sizeof(struct drm_mode_modeinfo)) {
			DRM_DEBUG_ATOMIC("[CRTC:%d:%s] bad mode blob length: %zu\n",
					 crtc->base.id, crtc->name,
					 blob->length);
D
Daniel Stone 已提交
439
			return -EINVAL;
V
Ville Syrjälä 已提交
440 441 442 443 444 445 446 447 448 449 450
		}

		ret = drm_mode_convert_umode(crtc->dev,
					     &state->mode, blob->data);
		if (ret) {
			DRM_DEBUG_ATOMIC("[CRTC:%d:%s] invalid mode (ret=%d, status=%s):\n",
					 crtc->base.id, crtc->name,
					 ret, drm_get_mode_status_name(state->mode.status));
			drm_mode_debug_printmodeline(&state->mode);
			return -EINVAL;
		}
D
Daniel Stone 已提交
451

452
		state->mode_blob = drm_property_blob_get(blob);
D
Daniel Stone 已提交
453
		state->enable = true;
454 455 456
		DRM_DEBUG_ATOMIC("Set [MODE:%s] for [CRTC:%d:%s] state %p\n",
				 state->mode.name, crtc->base.id, crtc->name,
				 state);
D
Daniel Stone 已提交
457 458
	} else {
		state->enable = false;
459 460
		DRM_DEBUG_ATOMIC("Set [NOMODE] for [CRTC:%d:%s] state %p\n",
				 crtc->base.id, crtc->name, state);
D
Daniel Stone 已提交
461 462 463 464 465 466
	}

	return 0;
}
EXPORT_SYMBOL(drm_atomic_set_mode_prop_for_crtc);

467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
/**
 * drm_atomic_replace_property_blob_from_id - lookup the new blob and replace the old one with it
 * @dev: DRM device
 * @blob: a pointer to the member blob to be replaced
 * @blob_id: ID of the new blob
 * @expected_size: total expected size of the blob data (in bytes)
 * @expected_elem_size: expected element size of the blob data (in bytes)
 * @replaced: did the blob get replaced?
 *
 * Replace @blob with another blob with the ID @blob_id. If @blob_id is zero
 * @blob becomes NULL.
 *
 * If @expected_size is positive the new blob length is expected to be equal
 * to @expected_size bytes. If @expected_elem_size is positive the new blob
 * length is expected to be a multiple of @expected_elem_size bytes. Otherwise
 * an error is returned.
 *
 * @replaced will indicate to the caller whether the blob was replaced or not.
 * If the old and new blobs were in fact the same blob @replaced will be false
 * otherwise it will be true.
 *
 * RETURNS:
 * Zero on success, error code on failure.
 */
491
static int
492
drm_atomic_replace_property_blob_from_id(struct drm_device *dev,
493 494 495
					 struct drm_property_blob **blob,
					 uint64_t blob_id,
					 ssize_t expected_size,
496
					 ssize_t expected_elem_size,
497 498 499 500 501
					 bool *replaced)
{
	struct drm_property_blob *new_blob = NULL;

	if (blob_id != 0) {
502
		new_blob = drm_property_lookup_blob(dev, blob_id);
503 504
		if (new_blob == NULL)
			return -EINVAL;
505

506 507 508 509 510 511 512
		if (expected_size > 0 &&
		    new_blob->length != expected_size) {
			drm_property_blob_put(new_blob);
			return -EINVAL;
		}
		if (expected_elem_size > 0 &&
		    new_blob->length % expected_elem_size != 0) {
513
			drm_property_blob_put(new_blob);
514
			return -EINVAL;
515
		}
516 517
	}

518
	*replaced |= drm_property_replace_blob(blob, new_blob);
519
	drm_property_blob_put(new_blob);
520 521 522 523

	return 0;
}

R
Rob Clark 已提交
524 525 526 527 528 529 530
/**
 * drm_atomic_crtc_set_property - set property on CRTC
 * @crtc: the drm CRTC to set a property on
 * @state: the state object to update with the new property value
 * @property: the property to set
 * @val: the new property value
 *
531 532 533 534
 * This function handles generic/core properties and calls out to driver's
 * &drm_crtc_funcs.atomic_set_property for driver properties. To ensure
 * consistent behavior you must call this function rather than the driver hook
 * directly.
R
Rob Clark 已提交
535 536 537 538 539 540 541 542
 *
 * RETURNS:
 * Zero on success, error code on failure
 */
int drm_atomic_crtc_set_property(struct drm_crtc *crtc,
		struct drm_crtc_state *state, struct drm_property *property,
		uint64_t val)
{
543 544
	struct drm_device *dev = crtc->dev;
	struct drm_mode_config *config = &dev->mode_config;
545
	bool replaced = false;
D
Daniel Stone 已提交
546
	int ret;
547

548
	if (property == config->prop_active)
549
		state->active = val;
D
Daniel Stone 已提交
550 551 552 553
	else if (property == config->prop_mode_id) {
		struct drm_property_blob *mode =
			drm_property_lookup_blob(dev, val);
		ret = drm_atomic_set_mode_prop_for_crtc(state, mode);
554
		drm_property_blob_put(mode);
D
Daniel Stone 已提交
555
		return ret;
556
	} else if (property == config->degamma_lut_property) {
557
		ret = drm_atomic_replace_property_blob_from_id(dev,
558 559
					&state->degamma_lut,
					val,
560
					-1, sizeof(struct drm_color_lut),
561
					&replaced);
562
		state->color_mgmt_changed |= replaced;
563 564
		return ret;
	} else if (property == config->ctm_property) {
565
		ret = drm_atomic_replace_property_blob_from_id(dev,
566 567
					&state->ctm,
					val,
568
					sizeof(struct drm_color_ctm), -1,
569
					&replaced);
570
		state->color_mgmt_changed |= replaced;
571 572
		return ret;
	} else if (property == config->gamma_lut_property) {
573
		ret = drm_atomic_replace_property_blob_from_id(dev,
574 575
					&state->gamma_lut,
					val,
576
					-1, sizeof(struct drm_color_lut),
577
					&replaced);
578
		state->color_mgmt_changed |= replaced;
579
		return ret;
580
	} else if (property == config->prop_out_fence_ptr) {
581
		s32 __user *fence_ptr = u64_to_user_ptr(val);
582 583 584 585 586 587 588 589

		if (!fence_ptr)
			return 0;

		if (put_user(-1, fence_ptr))
			return -EFAULT;

		set_out_fence_for_crtc(state->state, crtc, fence_ptr);
590
	} else if (crtc->funcs->atomic_set_property) {
R
Rob Clark 已提交
591
		return crtc->funcs->atomic_set_property(crtc, state, property, val);
592 593 594 595
	} else {
		DRM_DEBUG_ATOMIC("[CRTC:%d:%s] unknown property [PROP:%d:%s]]\n",
				 crtc->base.id, crtc->name,
				 property->base.id, property->name);
596
		return -EINVAL;
597
	}
598 599

	return 0;
R
Rob Clark 已提交
600 601 602
}
EXPORT_SYMBOL(drm_atomic_crtc_set_property);

603 604 605 606 607 608 609
/**
 * drm_atomic_crtc_get_property - get property value from CRTC state
 * @crtc: the drm CRTC to set a property on
 * @state: the state object to get the property value from
 * @property: the property to set
 * @val: return location for the property value
 *
610 611 612 613
 * This function handles generic/core properties and calls out to driver's
 * &drm_crtc_funcs.atomic_get_property for driver properties. To ensure
 * consistent behavior you must call this function rather than the driver hook
 * directly.
614 615 616
 *
 * RETURNS:
 * Zero on success, error code on failure
R
Rob Clark 已提交
617
 */
618 619
static int
drm_atomic_crtc_get_property(struct drm_crtc *crtc,
R
Rob Clark 已提交
620 621 622
		const struct drm_crtc_state *state,
		struct drm_property *property, uint64_t *val)
{
623 624 625 626 627
	struct drm_device *dev = crtc->dev;
	struct drm_mode_config *config = &dev->mode_config;

	if (property == config->prop_active)
		*val = state->active;
D
Daniel Stone 已提交
628 629
	else if (property == config->prop_mode_id)
		*val = (state->mode_blob) ? state->mode_blob->base.id : 0;
630 631 632 633 634 635
	else if (property == config->degamma_lut_property)
		*val = (state->degamma_lut) ? state->degamma_lut->base.id : 0;
	else if (property == config->ctm_property)
		*val = (state->ctm) ? state->ctm->base.id : 0;
	else if (property == config->gamma_lut_property)
		*val = (state->gamma_lut) ? state->gamma_lut->base.id : 0;
636 637
	else if (property == config->prop_out_fence_ptr)
		*val = 0;
638
	else if (crtc->funcs->atomic_get_property)
R
Rob Clark 已提交
639
		return crtc->funcs->atomic_get_property(crtc, state, property, val);
640 641 642 643
	else
		return -EINVAL;

	return 0;
R
Rob Clark 已提交
644 645
}

R
Rob Clark 已提交
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
/**
 * drm_atomic_crtc_check - check crtc state
 * @crtc: crtc to check
 * @state: crtc state to check
 *
 * Provides core sanity checks for crtc state.
 *
 * RETURNS:
 * Zero on success, error code on failure
 */
static int drm_atomic_crtc_check(struct drm_crtc *crtc,
		struct drm_crtc_state *state)
{
	/* NOTE: we explicitly don't enforce constraints such as primary
	 * layer covering entire screen, since that is something we want
	 * to allow (on hw that supports it).  For hw that does not, it
	 * should be checked in driver's crtc->atomic_check() vfunc.
	 *
	 * TODO: Add generic modeset state checks once we support those.
	 */
666 667

	if (state->active && !state->enable) {
668 669
		DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active without enabled\n",
				 crtc->base.id, crtc->name);
670 671 672
		return -EINVAL;
	}

673 674 675 676 677
	/* The state->enable vs. state->mode_blob checks can be WARN_ON,
	 * as this is a kernel-internal detail that userspace should never
	 * be able to trigger. */
	if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
	    WARN_ON(state->enable && !state->mode_blob)) {
678 679
		DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled without mode blob\n",
				 crtc->base.id, crtc->name);
680 681 682 683 684
		return -EINVAL;
	}

	if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
	    WARN_ON(!state->enable && state->mode_blob)) {
685 686
		DRM_DEBUG_ATOMIC("[CRTC:%d:%s] disabled with mode blob\n",
				 crtc->base.id, crtc->name);
687 688 689
		return -EINVAL;
	}

690 691 692 693 694 695 696 697 698 699 700
	/*
	 * Reject event generation for when a CRTC is off and stays off.
	 * It wouldn't be hard to implement this, but userspace has a track
	 * record of happily burning through 100% cpu (or worse, crash) when the
	 * display pipe is suspended. To avoid all that fun just reject updates
	 * that ask for events since likely that indicates a bug in the
	 * compositor's drawing loop. This is consistent with the vblank IOCTL
	 * and legacy page_flip IOCTL which also reject service on a disabled
	 * pipe.
	 */
	if (state->event && !state->active && !crtc->state->active) {
701 702
		DRM_DEBUG_ATOMIC("[CRTC:%d:%s] requesting event but off\n",
				 crtc->base.id, crtc->name);
703 704 705
		return -EINVAL;
	}

R
Rob Clark 已提交
706 707 708
	return 0;
}

709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
static void drm_atomic_crtc_print_state(struct drm_printer *p,
		const struct drm_crtc_state *state)
{
	struct drm_crtc *crtc = state->crtc;

	drm_printf(p, "crtc[%u]: %s\n", crtc->base.id, crtc->name);
	drm_printf(p, "\tenable=%d\n", state->enable);
	drm_printf(p, "\tactive=%d\n", state->active);
	drm_printf(p, "\tplanes_changed=%d\n", state->planes_changed);
	drm_printf(p, "\tmode_changed=%d\n", state->mode_changed);
	drm_printf(p, "\tactive_changed=%d\n", state->active_changed);
	drm_printf(p, "\tconnectors_changed=%d\n", state->connectors_changed);
	drm_printf(p, "\tcolor_mgmt_changed=%d\n", state->color_mgmt_changed);
	drm_printf(p, "\tplane_mask=%x\n", state->plane_mask);
	drm_printf(p, "\tconnector_mask=%x\n", state->connector_mask);
	drm_printf(p, "\tencoder_mask=%x\n", state->encoder_mask);
	drm_printf(p, "\tmode: " DRM_MODE_FMT "\n", DRM_MODE_ARG(&state->mode));

	if (crtc->funcs->atomic_print_state)
		crtc->funcs->atomic_print_state(p, state);
}

731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766
/**
 * drm_atomic_connector_check - check connector state
 * @connector: connector to check
 * @state: connector state to check
 *
 * Provides core sanity checks for connector state.
 *
 * RETURNS:
 * Zero on success, error code on failure
 */
static int drm_atomic_connector_check(struct drm_connector *connector,
		struct drm_connector_state *state)
{
	struct drm_crtc_state *crtc_state;
	struct drm_writeback_job *writeback_job = state->writeback_job;

	if ((connector->connector_type != DRM_MODE_CONNECTOR_WRITEBACK) || !writeback_job)
		return 0;

	if (writeback_job->fb && !state->crtc) {
		DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] framebuffer without CRTC\n",
				 connector->base.id, connector->name);
		return -EINVAL;
	}

	if (state->crtc)
		crtc_state = drm_atomic_get_existing_crtc_state(state->state,
								state->crtc);

	if (writeback_job->fb && !crtc_state->active) {
		DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] has framebuffer, but [CRTC:%d] is off\n",
				 connector->base.id, connector->name,
				 state->crtc->base.id);
		return -EINVAL;
	}

767 768 769 770 771 772
	if (writeback_job->out_fence && !writeback_job->fb) {
		DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] requesting out-fence without framebuffer\n",
				 connector->base.id, connector->name);
		return -EINVAL;
	}

773 774 775
	return 0;
}

D
Daniel Vetter 已提交
776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794
/**
 * drm_atomic_get_plane_state - get plane state
 * @state: global atomic state object
 * @plane: plane to get state object for
 *
 * This function returns the plane state for the given plane, allocating it if
 * needed. It will also grab the relevant plane lock to make sure that the state
 * is consistent.
 *
 * Returns:
 *
 * Either the allocated state or the error code encoded into the pointer. When
 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
 * entire atomic sequence must be restarted. All other errors are fatal.
 */
struct drm_plane_state *
drm_atomic_get_plane_state(struct drm_atomic_state *state,
			  struct drm_plane *plane)
{
795
	int ret, index = drm_plane_index(plane);
D
Daniel Vetter 已提交
796 797
	struct drm_plane_state *plane_state;

798 799
	WARN_ON(!state->acquire_ctx);

800 801 802 803 804
	/* the legacy pointers should never be set */
	WARN_ON(plane->fb);
	WARN_ON(plane->old_fb);
	WARN_ON(plane->crtc);

805 806 807
	plane_state = drm_atomic_get_existing_plane_state(state, plane);
	if (plane_state)
		return plane_state;
D
Daniel Vetter 已提交
808

D
Daniel Vetter 已提交
809
	ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
D
Daniel Vetter 已提交
810 811 812 813 814 815 816
	if (ret)
		return ERR_PTR(ret);

	plane_state = plane->funcs->atomic_duplicate_state(plane);
	if (!plane_state)
		return ERR_PTR(-ENOMEM);

817 818
	state->planes[index].state = plane_state;
	state->planes[index].ptr = plane;
819 820
	state->planes[index].old_state = plane->state;
	state->planes[index].new_state = plane_state;
D
Daniel Vetter 已提交
821 822
	plane_state->state = state;

823 824
	DRM_DEBUG_ATOMIC("Added [PLANE:%d:%s] %p state to %p\n",
			 plane->base.id, plane->name, plane_state, state);
D
Daniel Vetter 已提交
825 826 827 828 829 830 831 832 833 834 835 836 837 838

	if (plane_state->crtc) {
		struct drm_crtc_state *crtc_state;

		crtc_state = drm_atomic_get_crtc_state(state,
						       plane_state->crtc);
		if (IS_ERR(crtc_state))
			return ERR_CAST(crtc_state);
	}

	return plane_state;
}
EXPORT_SYMBOL(drm_atomic_get_plane_state);

R
Rob Clark 已提交
839 840 841 842 843 844 845
/**
 * drm_atomic_plane_set_property - set property on plane
 * @plane: the drm plane to set a property on
 * @state: the state object to update with the new property value
 * @property: the property to set
 * @val: the new property value
 *
846 847 848 849
 * This function handles generic/core properties and calls out to driver's
 * &drm_plane_funcs.atomic_set_property for driver properties.  To ensure
 * consistent behavior you must call this function rather than the driver hook
 * directly.
R
Rob Clark 已提交
850 851 852 853
 *
 * RETURNS:
 * Zero on success, error code on failure
 */
854
static int drm_atomic_plane_set_property(struct drm_plane *plane,
R
Rob Clark 已提交
855 856 857
		struct drm_plane_state *state, struct drm_property *property,
		uint64_t val)
{
R
Rob Clark 已提交
858 859 860 861
	struct drm_device *dev = plane->dev;
	struct drm_mode_config *config = &dev->mode_config;

	if (property == config->prop_fb_id) {
862
		struct drm_framebuffer *fb = drm_framebuffer_lookup(dev, NULL, val);
R
Rob Clark 已提交
863 864
		drm_atomic_set_fb_for_plane(state, fb);
		if (fb)
865
			drm_framebuffer_put(fb);
G
Gustavo Padovan 已提交
866 867 868 869 870 871 872 873 874 875 876
	} else if (property == config->prop_in_fence_fd) {
		if (state->fence)
			return -EINVAL;

		if (U642I64(val) == -1)
			return 0;

		state->fence = sync_file_get_fence(val);
		if (!state->fence)
			return -EINVAL;

R
Rob Clark 已提交
877
	} else if (property == config->prop_crtc_id) {
878
		struct drm_crtc *crtc = drm_crtc_find(dev, NULL, val);
R
Rob Clark 已提交
879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895
		return drm_atomic_set_crtc_for_plane(state, crtc);
	} else if (property == config->prop_crtc_x) {
		state->crtc_x = U642I64(val);
	} else if (property == config->prop_crtc_y) {
		state->crtc_y = U642I64(val);
	} else if (property == config->prop_crtc_w) {
		state->crtc_w = val;
	} else if (property == config->prop_crtc_h) {
		state->crtc_h = val;
	} else if (property == config->prop_src_x) {
		state->src_x = val;
	} else if (property == config->prop_src_y) {
		state->src_y = val;
	} else if (property == config->prop_src_w) {
		state->src_w = val;
	} else if (property == config->prop_src_h) {
		state->src_h = val;
896 897
	} else if (property == plane->alpha_property) {
		state->alpha = val;
898
	} else if (property == plane->rotation_property) {
899 900 901
		if (!is_power_of_2(val & DRM_MODE_ROTATE_MASK)) {
			DRM_DEBUG_ATOMIC("[PLANE:%d:%s] bad rotation bitmask: 0x%llx\n",
					 plane->base.id, plane->name, val);
902
			return -EINVAL;
903
		}
904
		state->rotation = val;
905 906
	} else if (property == plane->zpos_property) {
		state->zpos = val;
907 908 909 910
	} else if (property == plane->color_encoding_property) {
		state->color_encoding = val;
	} else if (property == plane->color_range_property) {
		state->color_range = val;
R
Rob Clark 已提交
911 912 913 914
	} else if (plane->funcs->atomic_set_property) {
		return plane->funcs->atomic_set_property(plane, state,
				property, val);
	} else {
915 916 917
		DRM_DEBUG_ATOMIC("[PLANE:%d:%s] unknown property [PROP:%d:%s]]\n",
				 plane->base.id, plane->name,
				 property->base.id, property->name);
R
Rob Clark 已提交
918 919 920 921
		return -EINVAL;
	}

	return 0;
R
Rob Clark 已提交
922 923
}

924 925 926 927 928 929 930
/**
 * drm_atomic_plane_get_property - get property value from plane state
 * @plane: the drm plane to set a property on
 * @state: the state object to get the property value from
 * @property: the property to set
 * @val: return location for the property value
 *
931 932 933 934
 * This function handles generic/core properties and calls out to driver's
 * &drm_plane_funcs.atomic_get_property for driver properties.  To ensure
 * consistent behavior you must call this function rather than the driver hook
 * directly.
935 936 937
 *
 * RETURNS:
 * Zero on success, error code on failure
R
Rob Clark 已提交
938
 */
939 940
static int
drm_atomic_plane_get_property(struct drm_plane *plane,
R
Rob Clark 已提交
941 942 943
		const struct drm_plane_state *state,
		struct drm_property *property, uint64_t *val)
{
R
Rob Clark 已提交
944 945 946 947 948
	struct drm_device *dev = plane->dev;
	struct drm_mode_config *config = &dev->mode_config;

	if (property == config->prop_fb_id) {
		*val = (state->fb) ? state->fb->base.id : 0;
G
Gustavo Padovan 已提交
949 950
	} else if (property == config->prop_in_fence_fd) {
		*val = -1;
R
Rob Clark 已提交
951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968
	} else if (property == config->prop_crtc_id) {
		*val = (state->crtc) ? state->crtc->base.id : 0;
	} else if (property == config->prop_crtc_x) {
		*val = I642U64(state->crtc_x);
	} else if (property == config->prop_crtc_y) {
		*val = I642U64(state->crtc_y);
	} else if (property == config->prop_crtc_w) {
		*val = state->crtc_w;
	} else if (property == config->prop_crtc_h) {
		*val = state->crtc_h;
	} else if (property == config->prop_src_x) {
		*val = state->src_x;
	} else if (property == config->prop_src_y) {
		*val = state->src_y;
	} else if (property == config->prop_src_w) {
		*val = state->src_w;
	} else if (property == config->prop_src_h) {
		*val = state->src_h;
969 970
	} else if (property == plane->alpha_property) {
		*val = state->alpha;
971
	} else if (property == plane->rotation_property) {
972
		*val = state->rotation;
973 974
	} else if (property == plane->zpos_property) {
		*val = state->zpos;
975 976 977 978
	} else if (property == plane->color_encoding_property) {
		*val = state->color_encoding;
	} else if (property == plane->color_range_property) {
		*val = state->color_range;
R
Rob Clark 已提交
979
	} else if (plane->funcs->atomic_get_property) {
R
Rob Clark 已提交
980
		return plane->funcs->atomic_get_property(plane, state, property, val);
R
Rob Clark 已提交
981 982 983 984 985
	} else {
		return -EINVAL;
	}

	return 0;
R
Rob Clark 已提交
986 987
}

988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006
static bool
plane_switching_crtc(struct drm_atomic_state *state,
		     struct drm_plane *plane,
		     struct drm_plane_state *plane_state)
{
	if (!plane->state->crtc || !plane_state->crtc)
		return false;

	if (plane->state->crtc == plane_state->crtc)
		return false;

	/* This could be refined, but currently there's no helper or driver code
	 * to implement direct switching of active planes nor userspace to take
	 * advantage of more direct plane switching without the intermediate
	 * full OFF state.
	 */
	return true;
}

R
Rob Clark 已提交
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
/**
 * drm_atomic_plane_check - check plane state
 * @plane: plane to check
 * @state: plane state to check
 *
 * Provides core sanity checks for plane state.
 *
 * RETURNS:
 * Zero on success, error code on failure
 */
static int drm_atomic_plane_check(struct drm_plane *plane,
		struct drm_plane_state *state)
{
	unsigned int fb_width, fb_height;
1021
	int ret;
R
Rob Clark 已提交
1022 1023

	/* either *both* CRTC and FB must be set, or neither */
1024
	if (state->crtc && !state->fb) {
1025 1026
		DRM_DEBUG_ATOMIC("[PLANE:%d:%s] CRTC set but no FB\n",
				 plane->base.id, plane->name);
R
Rob Clark 已提交
1027
		return -EINVAL;
1028
	} else if (state->fb && !state->crtc) {
1029 1030
		DRM_DEBUG_ATOMIC("[PLANE:%d:%s] FB set but no CRTC\n",
				 plane->base.id, plane->name);
R
Rob Clark 已提交
1031 1032 1033 1034 1035 1036 1037 1038 1039
		return -EINVAL;
	}

	/* if disabled, we don't care about the rest of the state: */
	if (!state->crtc)
		return 0;

	/* Check whether this plane is usable on this CRTC */
	if (!(plane->possible_crtcs & drm_crtc_mask(state->crtc))) {
1040 1041 1042
		DRM_DEBUG_ATOMIC("Invalid [CRTC:%d:%s] for [PLANE:%d:%s]\n",
				 state->crtc->base.id, state->crtc->name,
				 plane->base.id, plane->name);
R
Rob Clark 已提交
1043 1044 1045 1046
		return -EINVAL;
	}

	/* Check whether this plane supports the fb pixel format. */
1047 1048
	ret = drm_plane_check_pixel_format(plane, state->fb->format->format,
					   state->fb->modifier);
1049
	if (ret) {
1050
		struct drm_format_name_buf format_name;
1051 1052
		DRM_DEBUG_ATOMIC("[PLANE:%d:%s] invalid pixel format %s, modifier 0x%llx\n",
				 plane->base.id, plane->name,
1053 1054 1055
				 drm_get_format_name(state->fb->format->format,
						     &format_name),
				 state->fb->modifier);
1056
		return ret;
R
Rob Clark 已提交
1057 1058 1059 1060 1061 1062 1063
	}

	/* Give drivers some help against integer overflows */
	if (state->crtc_w > INT_MAX ||
	    state->crtc_x > INT_MAX - (int32_t) state->crtc_w ||
	    state->crtc_h > INT_MAX ||
	    state->crtc_y > INT_MAX - (int32_t) state->crtc_h) {
1064 1065
		DRM_DEBUG_ATOMIC("[PLANE:%d:%s] invalid CRTC coordinates %ux%u+%d+%d\n",
				 plane->base.id, plane->name,
D
Daniel Vetter 已提交
1066 1067
				 state->crtc_w, state->crtc_h,
				 state->crtc_x, state->crtc_y);
R
Rob Clark 已提交
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078
		return -ERANGE;
	}

	fb_width = state->fb->width << 16;
	fb_height = state->fb->height << 16;

	/* Make sure source coordinates are inside the fb. */
	if (state->src_w > fb_width ||
	    state->src_x > fb_width - state->src_w ||
	    state->src_h > fb_height ||
	    state->src_y > fb_height - state->src_h) {
1079
		DRM_DEBUG_ATOMIC("[PLANE:%d:%s] invalid source coordinates "
1080
				 "%u.%06ux%u.%06u+%u.%06u+%u.%06u (fb %ux%u)\n",
1081
				 plane->base.id, plane->name,
D
Daniel Vetter 已提交
1082 1083 1084
				 state->src_w >> 16, ((state->src_w & 0xffff) * 15625) >> 10,
				 state->src_h >> 16, ((state->src_h & 0xffff) * 15625) >> 10,
				 state->src_x >> 16, ((state->src_x & 0xffff) * 15625) >> 10,
1085 1086
				 state->src_y >> 16, ((state->src_y & 0xffff) * 15625) >> 10,
				 state->fb->width, state->fb->height);
R
Rob Clark 已提交
1087 1088 1089
		return -ENOSPC;
	}

1090
	if (plane_switching_crtc(state->state, plane, state)) {
1091 1092
		DRM_DEBUG_ATOMIC("[PLANE:%d:%s] switching CRTC directly\n",
				 plane->base.id, plane->name);
1093 1094 1095
		return -EINVAL;
	}

R
Rob Clark 已提交
1096 1097 1098
	return 0;
}

1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
static void drm_atomic_plane_print_state(struct drm_printer *p,
		const struct drm_plane_state *state)
{
	struct drm_plane *plane = state->plane;
	struct drm_rect src  = drm_plane_state_src(state);
	struct drm_rect dest = drm_plane_state_dest(state);

	drm_printf(p, "plane[%u]: %s\n", plane->base.id, plane->name);
	drm_printf(p, "\tcrtc=%s\n", state->crtc ? state->crtc->name : "(null)");
	drm_printf(p, "\tfb=%u\n", state->fb ? state->fb->base.id : 0);
1109 1110
	if (state->fb)
		drm_framebuffer_print_info(p, 2, state->fb);
1111 1112 1113
	drm_printf(p, "\tcrtc-pos=" DRM_RECT_FMT "\n", DRM_RECT_ARG(&dest));
	drm_printf(p, "\tsrc-pos=" DRM_RECT_FP_FMT "\n", DRM_RECT_FP_ARG(&src));
	drm_printf(p, "\trotation=%x\n", state->rotation);
1114 1115 1116 1117
	drm_printf(p, "\tcolor-encoding=%s\n",
		   drm_get_color_encoding_name(state->color_encoding));
	drm_printf(p, "\tcolor-range=%s\n",
		   drm_get_color_range_name(state->color_range));
1118 1119 1120 1121 1122

	if (plane->funcs->atomic_print_state)
		plane->funcs->atomic_print_state(p, state);
}

1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158
/**
 * DOC: handling driver private state
 *
 * Very often the DRM objects exposed to userspace in the atomic modeset api
 * (&drm_connector, &drm_crtc and &drm_plane) do not map neatly to the
 * underlying hardware. Especially for any kind of shared resources (e.g. shared
 * clocks, scaler units, bandwidth and fifo limits shared among a group of
 * planes or CRTCs, and so on) it makes sense to model these as independent
 * objects. Drivers then need to do similar state tracking and commit ordering for
 * such private (since not exposed to userpace) objects as the atomic core and
 * helpers already provide for connectors, planes and CRTCs.
 *
 * To make this easier on drivers the atomic core provides some support to track
 * driver private state objects using struct &drm_private_obj, with the
 * associated state struct &drm_private_state.
 *
 * Similar to userspace-exposed objects, private state structures can be
 * acquired by calling drm_atomic_get_private_obj_state(). Since this function
 * does not take care of locking, drivers should wrap it for each type of
 * private state object they have with the required call to drm_modeset_lock()
 * for the corresponding &drm_modeset_lock.
 *
 * All private state structures contained in a &drm_atomic_state update can be
 * iterated using for_each_oldnew_private_obj_in_state(),
 * for_each_new_private_obj_in_state() and for_each_old_private_obj_in_state().
 * Drivers are recommended to wrap these for each type of driver private state
 * object they have, filtering on &drm_private_obj.funcs using for_each_if(), at
 * least if they want to iterate over all objects of a given type.
 *
 * An earlier way to handle driver private state was by subclassing struct
 * &drm_atomic_state. But since that encourages non-standard ways to implement
 * the check/commit split atomic requires (by using e.g. "check and rollback or
 * commit instead" of "duplicate state, check, then either commit or release
 * duplicated state) it is deprecated in favour of using &drm_private_state.
 */

1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193
/**
 * drm_atomic_private_obj_init - initialize private object
 * @obj: private object
 * @state: initial private object state
 * @funcs: pointer to the struct of function pointers that identify the object
 * type
 *
 * Initialize the private object, which can be embedded into any
 * driver private object that needs its own atomic state.
 */
void
drm_atomic_private_obj_init(struct drm_private_obj *obj,
			    struct drm_private_state *state,
			    const struct drm_private_state_funcs *funcs)
{
	memset(obj, 0, sizeof(*obj));

	obj->state = state;
	obj->funcs = funcs;
}
EXPORT_SYMBOL(drm_atomic_private_obj_init);

/**
 * drm_atomic_private_obj_fini - finalize private object
 * @obj: private object
 *
 * Finalize the private object.
 */
void
drm_atomic_private_obj_fini(struct drm_private_obj *obj)
{
	obj->funcs->atomic_destroy_state(obj, obj->state);
}
EXPORT_SYMBOL(drm_atomic_private_obj_fini);

1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206
/**
 * drm_atomic_get_private_obj_state - get private object state
 * @state: global atomic state
 * @obj: private object to get the state for
 *
 * This function returns the private object state for the given private object,
 * allocating the state if needed. It does not grab any locks as the caller is
 * expected to care of any required locking.
 *
 * RETURNS:
 *
 * Either the allocated state or the error code encoded into a pointer.
 */
1207 1208 1209
struct drm_private_state *
drm_atomic_get_private_obj_state(struct drm_atomic_state *state,
				 struct drm_private_obj *obj)
1210 1211 1212 1213
{
	int index, num_objs, i;
	size_t size;
	struct __drm_private_objs_state *arr;
1214
	struct drm_private_state *obj_state;
1215 1216

	for (i = 0; i < state->num_private_objs; i++)
1217 1218
		if (obj == state->private_objs[i].ptr)
			return state->private_objs[i].state;
1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229

	num_objs = state->num_private_objs + 1;
	size = sizeof(*state->private_objs) * num_objs;
	arr = krealloc(state->private_objs, size, GFP_KERNEL);
	if (!arr)
		return ERR_PTR(-ENOMEM);

	state->private_objs = arr;
	index = state->num_private_objs;
	memset(&state->private_objs[index], 0, sizeof(*state->private_objs));

1230 1231
	obj_state = obj->funcs->atomic_duplicate_state(obj);
	if (!obj_state)
1232 1233
		return ERR_PTR(-ENOMEM);

1234 1235 1236 1237
	state->private_objs[index].state = obj_state;
	state->private_objs[index].old_state = obj->state;
	state->private_objs[index].new_state = obj_state;
	state->private_objs[index].ptr = obj;
1238
	obj_state->state = state;
1239

1240 1241
	state->num_private_objs = num_objs;

1242 1243
	DRM_DEBUG_ATOMIC("Added new private object %p state %p to %p\n",
			 obj, obj_state, state);
1244

1245
	return obj_state;
1246 1247 1248
}
EXPORT_SYMBOL(drm_atomic_get_private_obj_state);

D
Daniel Vetter 已提交
1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271
/**
 * drm_atomic_get_connector_state - get connector state
 * @state: global atomic state object
 * @connector: connector to get state object for
 *
 * This function returns the connector state for the given connector,
 * allocating it if needed. It will also grab the relevant connector lock to
 * make sure that the state is consistent.
 *
 * Returns:
 *
 * Either the allocated state or the error code encoded into the pointer. When
 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
 * entire atomic sequence must be restarted. All other errors are fatal.
 */
struct drm_connector_state *
drm_atomic_get_connector_state(struct drm_atomic_state *state,
			  struct drm_connector *connector)
{
	int ret, index;
	struct drm_mode_config *config = &connector->dev->mode_config;
	struct drm_connector_state *connector_state;

1272 1273
	WARN_ON(!state->acquire_ctx);

1274 1275 1276 1277
	ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
	if (ret)
		return ERR_PTR(ret);

D
Daniel Vetter 已提交
1278 1279
	index = drm_connector_index(connector);

1280
	if (index >= state->num_connector) {
1281
		struct __drm_connnectors_state *c;
1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292
		int alloc = max(index + 1, config->num_connector);

		c = krealloc(state->connectors, alloc * sizeof(*state->connectors), GFP_KERNEL);
		if (!c)
			return ERR_PTR(-ENOMEM);

		state->connectors = c;
		memset(&state->connectors[state->num_connector], 0,
		       sizeof(*state->connectors) * (alloc - state->num_connector));

		state->num_connector = alloc;
1293 1294
	}

1295 1296
	if (state->connectors[index].state)
		return state->connectors[index].state;
D
Daniel Vetter 已提交
1297 1298 1299 1300 1301

	connector_state = connector->funcs->atomic_duplicate_state(connector);
	if (!connector_state)
		return ERR_PTR(-ENOMEM);

1302
	drm_connector_get(connector);
1303
	state->connectors[index].state = connector_state;
1304 1305
	state->connectors[index].old_state = connector->state;
	state->connectors[index].new_state = connector_state;
1306
	state->connectors[index].ptr = connector;
D
Daniel Vetter 已提交
1307 1308
	connector_state->state = state;

1309 1310 1311
	DRM_DEBUG_ATOMIC("Added [CONNECTOR:%d:%s] %p state to %p\n",
			 connector->base.id, connector->name,
			 connector_state, state);
D
Daniel Vetter 已提交
1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325

	if (connector_state->crtc) {
		struct drm_crtc_state *crtc_state;

		crtc_state = drm_atomic_get_crtc_state(state,
						       connector_state->crtc);
		if (IS_ERR(crtc_state))
			return ERR_CAST(crtc_state);
	}

	return connector_state;
}
EXPORT_SYMBOL(drm_atomic_get_connector_state);

R
Rob Clark 已提交
1326 1327 1328 1329 1330 1331 1332
/**
 * drm_atomic_connector_set_property - set property on connector.
 * @connector: the drm connector to set a property on
 * @state: the state object to update with the new property value
 * @property: the property to set
 * @val: the new property value
 *
1333 1334 1335 1336
 * This function handles generic/core properties and calls out to driver's
 * &drm_connector_funcs.atomic_set_property for driver properties.  To ensure
 * consistent behavior you must call this function rather than the driver hook
 * directly.
R
Rob Clark 已提交
1337 1338 1339 1340
 *
 * RETURNS:
 * Zero on success, error code on failure
 */
1341
static int drm_atomic_connector_set_property(struct drm_connector *connector,
R
Rob Clark 已提交
1342 1343 1344 1345 1346 1347
		struct drm_connector_state *state, struct drm_property *property,
		uint64_t val)
{
	struct drm_device *dev = connector->dev;
	struct drm_mode_config *config = &dev->mode_config;

1348
	if (property == config->prop_crtc_id) {
1349
		struct drm_crtc *crtc = drm_crtc_find(dev, NULL, val);
1350 1351
		return drm_atomic_set_crtc_for_connector(state, crtc);
	} else if (property == config->dpms_property) {
R
Rob Clark 已提交
1352 1353 1354 1355 1356
		/* setting DPMS property requires special handling, which
		 * is done in legacy setprop path for us.  Disallow (for
		 * now?) atomic writes to DPMS property:
		 */
		return -EINVAL;
1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380
	} else if (property == config->tv_select_subconnector_property) {
		state->tv.subconnector = val;
	} else if (property == config->tv_left_margin_property) {
		state->tv.margins.left = val;
	} else if (property == config->tv_right_margin_property) {
		state->tv.margins.right = val;
	} else if (property == config->tv_top_margin_property) {
		state->tv.margins.top = val;
	} else if (property == config->tv_bottom_margin_property) {
		state->tv.margins.bottom = val;
	} else if (property == config->tv_mode_property) {
		state->tv.mode = val;
	} else if (property == config->tv_brightness_property) {
		state->tv.brightness = val;
	} else if (property == config->tv_contrast_property) {
		state->tv.contrast = val;
	} else if (property == config->tv_flicker_reduction_property) {
		state->tv.flicker_reduction = val;
	} else if (property == config->tv_overscan_property) {
		state->tv.overscan = val;
	} else if (property == config->tv_saturation_property) {
		state->tv.saturation = val;
	} else if (property == config->tv_hue_property) {
		state->tv.hue = val;
1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394
	} else if (property == config->link_status_property) {
		/* Never downgrade from GOOD to BAD on userspace's request here,
		 * only hw issues can do that.
		 *
		 * For an atomic property the userspace doesn't need to be able
		 * to understand all the properties, but needs to be able to
		 * restore the state it wants on VT switch. So if the userspace
		 * tries to change the link_status from GOOD to BAD, driver
		 * silently rejects it and returns a 0. This prevents userspace
		 * from accidently breaking  the display when it restores the
		 * state.
		 */
		if (state->link_status != DRM_LINK_STATUS_GOOD)
			state->link_status = val;
1395 1396
	} else if (property == config->aspect_ratio_property) {
		state->picture_aspect_ratio = val;
1397 1398
	} else if (property == config->content_type_property) {
		state->content_type = val;
1399 1400
	} else if (property == connector->scaling_mode_property) {
		state->scaling_mode = val;
S
Sean Paul 已提交
1401 1402 1403 1404 1405 1406
	} else if (property == connector->content_protection_property) {
		if (val == DRM_MODE_CONTENT_PROTECTION_ENABLED) {
			DRM_DEBUG_KMS("only drivers can set CP Enabled\n");
			return -EINVAL;
		}
		state->content_protection = val;
1407 1408 1409 1410 1411 1412
	} else if (property == config->writeback_fb_id_property) {
		struct drm_framebuffer *fb = drm_framebuffer_lookup(dev, NULL, val);
		int ret = drm_atomic_set_writeback_fb_for_connector(state, fb);
		if (fb)
			drm_framebuffer_put(fb);
		return ret;
1413 1414 1415 1416 1417
	} else if (property == config->writeback_out_fence_ptr_property) {
		s32 __user *fence_ptr = u64_to_user_ptr(val);

		return set_out_fence_for_connector(state->state, connector,
						   fence_ptr);
R
Rob Clark 已提交
1418 1419 1420 1421
	} else if (connector->funcs->atomic_set_property) {
		return connector->funcs->atomic_set_property(connector,
				state, property, val);
	} else {
1422 1423 1424
		DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] unknown property [PROP:%d:%s]]\n",
				 connector->base.id, connector->name,
				 property->base.id, property->name);
R
Rob Clark 已提交
1425 1426
		return -EINVAL;
	}
1427 1428

	return 0;
R
Rob Clark 已提交
1429 1430
}

1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442
static void drm_atomic_connector_print_state(struct drm_printer *p,
		const struct drm_connector_state *state)
{
	struct drm_connector *connector = state->connector;

	drm_printf(p, "connector[%u]: %s\n", connector->base.id, connector->name);
	drm_printf(p, "\tcrtc=%s\n", state->crtc ? state->crtc->name : "(null)");

	if (connector->funcs->atomic_print_state)
		connector->funcs->atomic_print_state(p, state);
}

1443 1444 1445 1446 1447 1448 1449
/**
 * drm_atomic_connector_get_property - get property value from connector state
 * @connector: the drm connector to set a property on
 * @state: the state object to get the property value from
 * @property: the property to set
 * @val: return location for the property value
 *
1450 1451 1452 1453
 * This function handles generic/core properties and calls out to driver's
 * &drm_connector_funcs.atomic_get_property for driver properties.  To ensure
 * consistent behavior you must call this function rather than the driver hook
 * directly.
1454 1455 1456
 *
 * RETURNS:
 * Zero on success, error code on failure
R
Rob Clark 已提交
1457
 */
1458 1459
static int
drm_atomic_connector_get_property(struct drm_connector *connector,
R
Rob Clark 已提交
1460 1461 1462 1463 1464 1465
		const struct drm_connector_state *state,
		struct drm_property *property, uint64_t *val)
{
	struct drm_device *dev = connector->dev;
	struct drm_mode_config *config = &dev->mode_config;

1466 1467 1468
	if (property == config->prop_crtc_id) {
		*val = (state->crtc) ? state->crtc->base.id : 0;
	} else if (property == config->dpms_property) {
R
Rob Clark 已提交
1469
		*val = connector->dpms;
1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493
	} else if (property == config->tv_select_subconnector_property) {
		*val = state->tv.subconnector;
	} else if (property == config->tv_left_margin_property) {
		*val = state->tv.margins.left;
	} else if (property == config->tv_right_margin_property) {
		*val = state->tv.margins.right;
	} else if (property == config->tv_top_margin_property) {
		*val = state->tv.margins.top;
	} else if (property == config->tv_bottom_margin_property) {
		*val = state->tv.margins.bottom;
	} else if (property == config->tv_mode_property) {
		*val = state->tv.mode;
	} else if (property == config->tv_brightness_property) {
		*val = state->tv.brightness;
	} else if (property == config->tv_contrast_property) {
		*val = state->tv.contrast;
	} else if (property == config->tv_flicker_reduction_property) {
		*val = state->tv.flicker_reduction;
	} else if (property == config->tv_overscan_property) {
		*val = state->tv.overscan;
	} else if (property == config->tv_saturation_property) {
		*val = state->tv.saturation;
	} else if (property == config->tv_hue_property) {
		*val = state->tv.hue;
1494 1495
	} else if (property == config->link_status_property) {
		*val = state->link_status;
1496 1497
	} else if (property == config->aspect_ratio_property) {
		*val = state->picture_aspect_ratio;
1498 1499
	} else if (property == config->content_type_property) {
		*val = state->content_type;
1500 1501
	} else if (property == connector->scaling_mode_property) {
		*val = state->scaling_mode;
S
Sean Paul 已提交
1502 1503
	} else if (property == connector->content_protection_property) {
		*val = state->content_protection;
1504 1505 1506
	} else if (property == config->writeback_fb_id_property) {
		/* Writeback framebuffer is one-shot, write and forget */
		*val = 0;
1507 1508
	} else if (property == config->writeback_out_fence_ptr_property) {
		*val = 0;
R
Rob Clark 已提交
1509 1510 1511 1512 1513 1514 1515 1516 1517 1518
	} else if (connector->funcs->atomic_get_property) {
		return connector->funcs->atomic_get_property(connector,
				state, property, val);
	} else {
		return -EINVAL;
	}

	return 0;
}

R
Rob Clark 已提交
1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554
int drm_atomic_get_property(struct drm_mode_object *obj,
		struct drm_property *property, uint64_t *val)
{
	struct drm_device *dev = property->dev;
	int ret;

	switch (obj->type) {
	case DRM_MODE_OBJECT_CONNECTOR: {
		struct drm_connector *connector = obj_to_connector(obj);
		WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
		ret = drm_atomic_connector_get_property(connector,
				connector->state, property, val);
		break;
	}
	case DRM_MODE_OBJECT_CRTC: {
		struct drm_crtc *crtc = obj_to_crtc(obj);
		WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
		ret = drm_atomic_crtc_get_property(crtc,
				crtc->state, property, val);
		break;
	}
	case DRM_MODE_OBJECT_PLANE: {
		struct drm_plane *plane = obj_to_plane(obj);
		WARN_ON(!drm_modeset_is_locked(&plane->mutex));
		ret = drm_atomic_plane_get_property(plane,
				plane->state, property, val);
		break;
	}
	default:
		ret = -EINVAL;
		break;
	}

	return ret;
}

D
Daniel Vetter 已提交
1555 1556
/**
 * drm_atomic_set_crtc_for_plane - set crtc for plane
1557
 * @plane_state: the plane whose incoming state to update
D
Daniel Vetter 已提交
1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569
 * @crtc: crtc to use for the plane
 *
 * Changing the assigned crtc for a plane requires us to grab the lock and state
 * for the new crtc, as needed. This function takes care of all these details
 * besides updating the pointer in the state object itself.
 *
 * Returns:
 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
 * then the w/w mutex code has detected a deadlock and the entire atomic
 * sequence must be restarted. All other errors are fatal.
 */
int
1570 1571
drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
			      struct drm_crtc *crtc)
D
Daniel Vetter 已提交
1572
{
1573
	struct drm_plane *plane = plane_state->plane;
D
Daniel Vetter 已提交
1574
	struct drm_crtc_state *crtc_state;
1575 1576 1577
	/* Nothing to do for same crtc*/
	if (plane_state->crtc == crtc)
		return 0;
1578 1579 1580 1581 1582 1583
	if (plane_state->crtc) {
		crtc_state = drm_atomic_get_crtc_state(plane_state->state,
						       plane_state->crtc);
		if (WARN_ON(IS_ERR(crtc_state)))
			return PTR_ERR(crtc_state);

V
Ville Syrjälä 已提交
1584
		crtc_state->plane_mask &= ~drm_plane_mask(plane);
1585 1586 1587 1588
	}

	plane_state->crtc = crtc;

D
Daniel Vetter 已提交
1589 1590 1591 1592 1593
	if (crtc) {
		crtc_state = drm_atomic_get_crtc_state(plane_state->state,
						       crtc);
		if (IS_ERR(crtc_state))
			return PTR_ERR(crtc_state);
V
Ville Syrjälä 已提交
1594
		crtc_state->plane_mask |= drm_plane_mask(plane);
D
Daniel Vetter 已提交
1595 1596 1597
	}

	if (crtc)
1598 1599 1600
		DRM_DEBUG_ATOMIC("Link [PLANE:%d:%s] state %p to [CRTC:%d:%s]\n",
				 plane->base.id, plane->name, plane_state,
				 crtc->base.id, crtc->name);
D
Daniel Vetter 已提交
1601
	else
1602 1603
		DRM_DEBUG_ATOMIC("Link [PLANE:%d:%s] state %p to [NOCRTC]\n",
				 plane->base.id, plane->name, plane_state);
D
Daniel Vetter 已提交
1604 1605 1606 1607 1608

	return 0;
}
EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane);

1609
/**
J
John Hunter 已提交
1610
 * drm_atomic_set_fb_for_plane - set framebuffer for plane
1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622
 * @plane_state: atomic state object for the plane
 * @fb: fb to use for the plane
 *
 * Changing the assigned framebuffer for a plane requires us to grab a reference
 * to the new fb and drop the reference to the old fb, if there is one. This
 * function takes care of all these details besides updating the pointer in the
 * state object itself.
 */
void
drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
			    struct drm_framebuffer *fb)
{
1623 1624
	struct drm_plane *plane = plane_state->plane;

1625
	if (fb)
1626 1627
		DRM_DEBUG_ATOMIC("Set [FB:%d] for [PLANE:%d:%s] state %p\n",
				 fb->base.id, plane->base.id, plane->name,
D
Daniel Vetter 已提交
1628
				 plane_state);
1629 1630 1631
	else
		DRM_DEBUG_ATOMIC("Set [NOFB] for [PLANE:%d:%s] state %p\n",
				 plane->base.id, plane->name, plane_state);
1632 1633

	drm_framebuffer_assign(&plane_state->fb, fb);
1634 1635 1636
}
EXPORT_SYMBOL(drm_atomic_set_fb_for_plane);

1637 1638 1639 1640 1641 1642 1643 1644 1645 1646
/**
 * drm_atomic_set_fence_for_plane - set fence for plane
 * @plane_state: atomic state object for the plane
 * @fence: dma_fence to use for the plane
 *
 * Helper to setup the plane_state fence in case it is not set yet.
 * By using this drivers doesn't need to worry if the user choose
 * implicit or explicit fencing.
 *
 * This function will not set the fence to the state if it was set
1647 1648 1649 1650 1651
 * via explicit fencing interfaces on the atomic ioctl. In that case it will
 * drop the reference to the fence as we are not storing it anywhere.
 * Otherwise, if &drm_plane_state.fence is not set this function we just set it
 * with the received implicit fence. In both cases this function consumes a
 * reference for @fence.
1652 1653 1654 1655 1656 1657 1658 1659
 *
 * This way explicit fencing can be used to overrule implicit fencing, which is
 * important to make explicit fencing use-cases work: One example is using one
 * buffer for 2 screens with different refresh rates. Implicit fencing will
 * clamp rendering to the refresh rate of the slower screen, whereas explicit
 * fence allows 2 independent render and display loops on a single buffer. If a
 * driver allows obeys both implicit and explicit fences for plane updates, then
 * it will break all the benefits of explicit fencing.
1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673
 */
void
drm_atomic_set_fence_for_plane(struct drm_plane_state *plane_state,
			       struct dma_fence *fence)
{
	if (plane_state->fence) {
		dma_fence_put(fence);
		return;
	}

	plane_state->fence = fence;
}
EXPORT_SYMBOL(drm_atomic_set_fence_for_plane);

D
Daniel Vetter 已提交
1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691
/**
 * drm_atomic_set_crtc_for_connector - set crtc for connector
 * @conn_state: atomic state object for the connector
 * @crtc: crtc to use for the connector
 *
 * Changing the assigned crtc for a connector requires us to grab the lock and
 * state for the new crtc, as needed. This function takes care of all these
 * details besides updating the pointer in the state object itself.
 *
 * Returns:
 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
 * then the w/w mutex code has detected a deadlock and the entire atomic
 * sequence must be restarted. All other errors are fatal.
 */
int
drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
				  struct drm_crtc *crtc)
{
1692
	struct drm_connector *connector = conn_state->connector;
D
Daniel Vetter 已提交
1693 1694
	struct drm_crtc_state *crtc_state;

1695 1696 1697 1698
	if (conn_state->crtc == crtc)
		return 0;

	if (conn_state->crtc) {
1699 1700
		crtc_state = drm_atomic_get_new_crtc_state(conn_state->state,
							   conn_state->crtc);
1701 1702 1703

		crtc_state->connector_mask &=
			~(1 << drm_connector_index(conn_state->connector));
1704

1705
		drm_connector_put(conn_state->connector);
1706
		conn_state->crtc = NULL;
1707 1708
	}

D
Daniel Vetter 已提交
1709 1710 1711 1712
	if (crtc) {
		crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc);
		if (IS_ERR(crtc_state))
			return PTR_ERR(crtc_state);
1713 1714 1715

		crtc_state->connector_mask |=
			1 << drm_connector_index(conn_state->connector);
D
Daniel Vetter 已提交
1716

1717
		drm_connector_get(conn_state->connector);
1718
		conn_state->crtc = crtc;
D
Daniel Vetter 已提交
1719

1720 1721
		DRM_DEBUG_ATOMIC("Link [CONNECTOR:%d:%s] state %p to [CRTC:%d:%s]\n",
				 connector->base.id, connector->name,
1722
				 conn_state, crtc->base.id, crtc->name);
1723
	} else {
1724 1725
		DRM_DEBUG_ATOMIC("Link [CONNECTOR:%d:%s] state %p to [NOCRTC]\n",
				 connector->base.id, connector->name,
D
Daniel Vetter 已提交
1726
				 conn_state);
1727
	}
D
Daniel Vetter 已提交
1728 1729 1730 1731 1732

	return 0;
}
EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector);

1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796
/*
 * drm_atomic_get_writeback_job - return or allocate a writeback job
 * @conn_state: Connector state to get the job for
 *
 * Writeback jobs have a different lifetime to the atomic state they are
 * associated with. This convenience function takes care of allocating a job
 * if there isn't yet one associated with the connector state, otherwise
 * it just returns the existing job.
 *
 * Returns: The writeback job for the given connector state
 */
static struct drm_writeback_job *
drm_atomic_get_writeback_job(struct drm_connector_state *conn_state)
{
	WARN_ON(conn_state->connector->connector_type != DRM_MODE_CONNECTOR_WRITEBACK);

	if (!conn_state->writeback_job)
		conn_state->writeback_job =
			kzalloc(sizeof(*conn_state->writeback_job), GFP_KERNEL);

	return conn_state->writeback_job;
}

/**
 * drm_atomic_set_writeback_fb_for_connector - set writeback framebuffer
 * @conn_state: atomic state object for the connector
 * @fb: fb to use for the connector
 *
 * This is used to set the framebuffer for a writeback connector, which outputs
 * to a buffer instead of an actual physical connector.
 * Changing the assigned framebuffer requires us to grab a reference to the new
 * fb and drop the reference to the old fb, if there is one. This function
 * takes care of all these details besides updating the pointer in the
 * state object itself.
 *
 * Note: The only way conn_state can already have an fb set is if the commit
 * sets the property more than once.
 *
 * See also: drm_writeback_connector_init()
 *
 * Returns: 0 on success
 */
int drm_atomic_set_writeback_fb_for_connector(
		struct drm_connector_state *conn_state,
		struct drm_framebuffer *fb)
{
	struct drm_writeback_job *job =
		drm_atomic_get_writeback_job(conn_state);
	if (!job)
		return -ENOMEM;

	drm_framebuffer_assign(&job->fb, fb);

	if (fb)
		DRM_DEBUG_ATOMIC("Set [FB:%d] for connector state %p\n",
				 fb->base.id, conn_state);
	else
		DRM_DEBUG_ATOMIC("Set [NOFB] for connector state %p\n",
				 conn_state);

	return 0;
}
EXPORT_SYMBOL(drm_atomic_set_writeback_fb_for_connector);

D
Daniel Vetter 已提交
1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820
/**
 * drm_atomic_add_affected_connectors - add connectors for crtc
 * @state: atomic state
 * @crtc: DRM crtc
 *
 * This function walks the current configuration and adds all connectors
 * currently using @crtc to the atomic configuration @state. Note that this
 * function must acquire the connection mutex. This can potentially cause
 * unneeded seralization if the update is just for the planes on one crtc. Hence
 * drivers and helpers should only call this when really needed (e.g. when a
 * full modeset needs to happen due to some change).
 *
 * Returns:
 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
 * then the w/w mutex code has detected a deadlock and the entire atomic
 * sequence must be restarted. All other errors are fatal.
 */
int
drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
				   struct drm_crtc *crtc)
{
	struct drm_mode_config *config = &state->dev->mode_config;
	struct drm_connector *connector;
	struct drm_connector_state *conn_state;
1821
	struct drm_connector_list_iter conn_iter;
1822
	struct drm_crtc_state *crtc_state;
D
Daniel Vetter 已提交
1823 1824
	int ret;

1825 1826 1827 1828
	crtc_state = drm_atomic_get_crtc_state(state, crtc);
	if (IS_ERR(crtc_state))
		return PTR_ERR(crtc_state);

D
Daniel Vetter 已提交
1829 1830 1831 1832
	ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
	if (ret)
		return ret;

1833 1834
	DRM_DEBUG_ATOMIC("Adding all current connectors for [CRTC:%d:%s] to %p\n",
			 crtc->base.id, crtc->name, state);
D
Daniel Vetter 已提交
1835 1836

	/*
1837 1838
	 * Changed connectors are already in @state, so only need to look
	 * at the connector_mask in crtc_state.
D
Daniel Vetter 已提交
1839
	 */
1840
	drm_connector_list_iter_begin(state->dev, &conn_iter);
1841
	drm_for_each_connector_iter(connector, &conn_iter) {
1842
		if (!(crtc_state->connector_mask & (1 << drm_connector_index(connector))))
D
Daniel Vetter 已提交
1843 1844 1845
			continue;

		conn_state = drm_atomic_get_connector_state(state, connector);
1846
		if (IS_ERR(conn_state)) {
1847
			drm_connector_list_iter_end(&conn_iter);
D
Daniel Vetter 已提交
1848
			return PTR_ERR(conn_state);
1849
		}
D
Daniel Vetter 已提交
1850
	}
1851
	drm_connector_list_iter_end(&conn_iter);
D
Daniel Vetter 已提交
1852 1853 1854 1855 1856

	return 0;
}
EXPORT_SYMBOL(drm_atomic_add_affected_connectors);

1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882
/**
 * drm_atomic_add_affected_planes - add planes for crtc
 * @state: atomic state
 * @crtc: DRM crtc
 *
 * This function walks the current configuration and adds all planes
 * currently used by @crtc to the atomic configuration @state. This is useful
 * when an atomic commit also needs to check all currently enabled plane on
 * @crtc, e.g. when changing the mode. It's also useful when re-enabling a CRTC
 * to avoid special code to force-enable all planes.
 *
 * Since acquiring a plane state will always also acquire the w/w mutex of the
 * current CRTC for that plane (if there is any) adding all the plane states for
 * a CRTC will not reduce parallism of atomic updates.
 *
 * Returns:
 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
 * then the w/w mutex code has detected a deadlock and the entire atomic
 * sequence must be restarted. All other errors are fatal.
 */
int
drm_atomic_add_affected_planes(struct drm_atomic_state *state,
			       struct drm_crtc *crtc)
{
	struct drm_plane *plane;

1883
	WARN_ON(!drm_atomic_get_new_crtc_state(state, crtc));
1884

1885 1886 1887
	DRM_DEBUG_ATOMIC("Adding all current planes for [CRTC:%d:%s] to %p\n",
			 crtc->base.id, crtc->name, state);

1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898
	drm_for_each_plane_mask(plane, state->dev, crtc->state->plane_mask) {
		struct drm_plane_state *plane_state =
			drm_atomic_get_plane_state(state, plane);

		if (IS_ERR(plane_state))
			return PTR_ERR(plane_state);
	}
	return 0;
}
EXPORT_SYMBOL(drm_atomic_add_affected_planes);

D
Daniel Vetter 已提交
1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911
/**
 * drm_atomic_check_only - check whether a given config would work
 * @state: atomic configuration to check
 *
 * Note that this function can return -EDEADLK if the driver needed to acquire
 * more locks but encountered a deadlock. The caller must then do the usual w/w
 * backoff dance and restart. All other errors are fatal.
 *
 * Returns:
 * 0 on success, negative error code on failure.
 */
int drm_atomic_check_only(struct drm_atomic_state *state)
{
R
Rob Clark 已提交
1912 1913
	struct drm_device *dev = state->dev;
	struct drm_mode_config *config = &dev->mode_config;
1914 1915 1916 1917
	struct drm_plane *plane;
	struct drm_plane_state *plane_state;
	struct drm_crtc *crtc;
	struct drm_crtc_state *crtc_state;
1918 1919
	struct drm_connector *conn;
	struct drm_connector_state *conn_state;
R
Rob Clark 已提交
1920
	int i, ret = 0;
D
Daniel Vetter 已提交
1921

D
Daniel Vetter 已提交
1922
	DRM_DEBUG_ATOMIC("checking %p\n", state);
D
Daniel Vetter 已提交
1923

1924
	for_each_new_plane_in_state(state, plane, plane_state, i) {
1925
		ret = drm_atomic_plane_check(plane, plane_state);
R
Rob Clark 已提交
1926
		if (ret) {
1927 1928
			DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic core check failed\n",
					 plane->base.id, plane->name);
R
Rob Clark 已提交
1929 1930 1931 1932
			return ret;
		}
	}

1933
	for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
1934
		ret = drm_atomic_crtc_check(crtc, crtc_state);
R
Rob Clark 已提交
1935
		if (ret) {
1936 1937
			DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic core check failed\n",
					 crtc->base.id, crtc->name);
R
Rob Clark 已提交
1938 1939 1940 1941
			return ret;
		}
	}

1942 1943 1944 1945 1946 1947 1948 1949 1950
	for_each_new_connector_in_state(state, conn, conn_state, i) {
		ret = drm_atomic_connector_check(conn, conn_state);
		if (ret) {
			DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] atomic core check failed\n",
					 conn->base.id, conn->name);
			return ret;
		}
	}

1951
	if (config->funcs->atomic_check) {
R
Rob Clark 已提交
1952 1953
		ret = config->funcs->atomic_check(state->dev, state);

1954 1955 1956 1957 1958 1959
		if (ret) {
			DRM_DEBUG_ATOMIC("atomic driver check for %p failed: %d\n",
					 state, ret);
			return ret;
		}
	}
1960

R
Rob Clark 已提交
1961
	if (!state->allow_modeset) {
1962
		for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
1963
			if (drm_atomic_crtc_needs_modeset(crtc_state)) {
1964 1965
				DRM_DEBUG_ATOMIC("[CRTC:%d:%s] requires full modeset\n",
						 crtc->base.id, crtc->name);
R
Rob Clark 已提交
1966 1967 1968 1969 1970
				return -EINVAL;
			}
		}
	}

1971
	return 0;
D
Daniel Vetter 已提交
1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982
}
EXPORT_SYMBOL(drm_atomic_check_only);

/**
 * drm_atomic_commit - commit configuration atomically
 * @state: atomic configuration to check
 *
 * Note that this function can return -EDEADLK if the driver needed to acquire
 * more locks but encountered a deadlock. The caller must then do the usual w/w
 * backoff dance and restart. All other errors are fatal.
 *
1983 1984
 * This function will take its own reference on @state.
 * Callers should always release their reference with drm_atomic_state_put().
D
Daniel Vetter 已提交
1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997
 *
 * Returns:
 * 0 on success, negative error code on failure.
 */
int drm_atomic_commit(struct drm_atomic_state *state)
{
	struct drm_mode_config *config = &state->dev->mode_config;
	int ret;

	ret = drm_atomic_check_only(state);
	if (ret)
		return ret;

1998
	DRM_DEBUG_ATOMIC("committing %p\n", state);
D
Daniel Vetter 已提交
1999 2000 2001 2002 2003 2004

	return config->funcs->atomic_commit(state->dev, state, false);
}
EXPORT_SYMBOL(drm_atomic_commit);

/**
2005
 * drm_atomic_nonblocking_commit - atomic nonblocking commit
D
Daniel Vetter 已提交
2006 2007 2008 2009 2010 2011
 * @state: atomic configuration to check
 *
 * Note that this function can return -EDEADLK if the driver needed to acquire
 * more locks but encountered a deadlock. The caller must then do the usual w/w
 * backoff dance and restart. All other errors are fatal.
 *
2012 2013
 * This function will take its own reference on @state.
 * Callers should always release their reference with drm_atomic_state_put().
D
Daniel Vetter 已提交
2014 2015 2016 2017
 *
 * Returns:
 * 0 on success, negative error code on failure.
 */
2018
int drm_atomic_nonblocking_commit(struct drm_atomic_state *state)
D
Daniel Vetter 已提交
2019 2020 2021 2022 2023 2024 2025 2026
{
	struct drm_mode_config *config = &state->dev->mode_config;
	int ret;

	ret = drm_atomic_check_only(state);
	if (ret)
		return ret;

2027
	DRM_DEBUG_ATOMIC("committing %p nonblocking\n", state);
D
Daniel Vetter 已提交
2028 2029 2030

	return config->funcs->atomic_commit(state->dev, state, true);
}
2031
EXPORT_SYMBOL(drm_atomic_nonblocking_commit);
R
Rob Clark 已提交
2032

2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045
static void drm_atomic_print_state(const struct drm_atomic_state *state)
{
	struct drm_printer p = drm_info_printer(state->dev->dev);
	struct drm_plane *plane;
	struct drm_plane_state *plane_state;
	struct drm_crtc *crtc;
	struct drm_crtc_state *crtc_state;
	struct drm_connector *connector;
	struct drm_connector_state *connector_state;
	int i;

	DRM_DEBUG_ATOMIC("checking %p\n", state);

2046
	for_each_new_plane_in_state(state, plane, plane_state, i)
2047 2048
		drm_atomic_plane_print_state(&p, plane_state);

2049
	for_each_new_crtc_in_state(state, crtc, crtc_state, i)
2050 2051
		drm_atomic_crtc_print_state(&p, crtc_state);

2052
	for_each_new_connector_in_state(state, connector, connector_state, i)
2053 2054 2055
		drm_atomic_connector_print_state(&p, connector_state);
}

2056 2057
static void __drm_state_dump(struct drm_device *dev, struct drm_printer *p,
			     bool take_locks)
2058 2059 2060 2061 2062
{
	struct drm_mode_config *config = &dev->mode_config;
	struct drm_plane *plane;
	struct drm_crtc *crtc;
	struct drm_connector *connector;
2063
	struct drm_connector_list_iter conn_iter;
2064 2065 2066 2067

	if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
		return;

2068 2069 2070
	list_for_each_entry(plane, &config->plane_list, head) {
		if (take_locks)
			drm_modeset_lock(&plane->mutex, NULL);
2071
		drm_atomic_plane_print_state(p, plane->state);
2072 2073 2074
		if (take_locks)
			drm_modeset_unlock(&plane->mutex);
	}
2075

2076 2077 2078
	list_for_each_entry(crtc, &config->crtc_list, head) {
		if (take_locks)
			drm_modeset_lock(&crtc->mutex, NULL);
2079
		drm_atomic_crtc_print_state(p, crtc->state);
2080 2081 2082
		if (take_locks)
			drm_modeset_unlock(&crtc->mutex);
	}
2083

2084
	drm_connector_list_iter_begin(dev, &conn_iter);
2085 2086
	if (take_locks)
		drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
2087
	drm_for_each_connector_iter(connector, &conn_iter)
2088
		drm_atomic_connector_print_state(p, connector->state);
2089 2090
	if (take_locks)
		drm_modeset_unlock(&dev->mode_config.connection_mutex);
2091
	drm_connector_list_iter_end(&conn_iter);
2092
}
2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112

/**
 * drm_state_dump - dump entire device atomic state
 * @dev: the drm device
 * @p: where to print the state to
 *
 * Just for debugging.  Drivers might want an option to dump state
 * to dmesg in case of error irq's.  (Hint, you probably want to
 * ratelimit this!)
 *
 * The caller must drm_modeset_lock_all(), or if this is called
 * from error irq handler, it should not be enabled by default.
 * (Ie. if you are debugging errors you might not care that this
 * is racey.  But calling this without all modeset locks held is
 * not inherently safe.)
 */
void drm_state_dump(struct drm_device *dev, struct drm_printer *p)
{
	__drm_state_dump(dev, p, false);
}
2113 2114 2115 2116 2117 2118 2119 2120 2121
EXPORT_SYMBOL(drm_state_dump);

#ifdef CONFIG_DEBUG_FS
static int drm_state_info(struct seq_file *m, void *data)
{
	struct drm_info_node *node = (struct drm_info_node *) m->private;
	struct drm_device *dev = node->minor->dev;
	struct drm_printer p = drm_seq_file_printer(m);

2122
	__drm_state_dump(dev, &p, true);
2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139

	return 0;
}

/* any use in debugfs files to dump individual planes/crtc/etc? */
static const struct drm_info_list drm_atomic_debugfs_list[] = {
	{"state", drm_state_info, 0},
};

int drm_atomic_debugfs_init(struct drm_minor *minor)
{
	return drm_debugfs_create_files(drm_atomic_debugfs_list,
			ARRAY_SIZE(drm_atomic_debugfs_list),
			minor->debugfs_root, minor);
}
#endif

R
Rob Clark 已提交
2140
/*
L
Liviu Dudau 已提交
2141
 * The big monster ioctl
R
Rob Clark 已提交
2142 2143 2144
 */

static struct drm_pending_vblank_event *create_vblank_event(
2145
		struct drm_crtc *crtc, uint64_t user_data)
R
Rob Clark 已提交
2146 2147 2148 2149
{
	struct drm_pending_vblank_event *e = NULL;

	e = kzalloc(sizeof *e, GFP_KERNEL);
2150 2151
	if (!e)
		return NULL;
R
Rob Clark 已提交
2152 2153

	e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
2154
	e->event.base.length = sizeof(e->event);
2155 2156
	e->event.vbl.crtc_id = crtc->base.id;
	e->event.vbl.user_data = user_data;
R
Rob Clark 已提交
2157

2158
	return e;
R
Rob Clark 已提交
2159 2160
}

2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214
int drm_atomic_connector_commit_dpms(struct drm_atomic_state *state,
				     struct drm_connector *connector,
				     int mode)
{
	struct drm_connector *tmp_connector;
	struct drm_connector_state *new_conn_state;
	struct drm_crtc *crtc;
	struct drm_crtc_state *crtc_state;
	int i, ret, old_mode = connector->dpms;
	bool active = false;

	ret = drm_modeset_lock(&state->dev->mode_config.connection_mutex,
			       state->acquire_ctx);
	if (ret)
		return ret;

	if (mode != DRM_MODE_DPMS_ON)
		mode = DRM_MODE_DPMS_OFF;
	connector->dpms = mode;

	crtc = connector->state->crtc;
	if (!crtc)
		goto out;
	ret = drm_atomic_add_affected_connectors(state, crtc);
	if (ret)
		goto out;

	crtc_state = drm_atomic_get_crtc_state(state, crtc);
	if (IS_ERR(crtc_state)) {
		ret = PTR_ERR(crtc_state);
		goto out;
	}

	for_each_new_connector_in_state(state, tmp_connector, new_conn_state, i) {
		if (new_conn_state->crtc != crtc)
			continue;
		if (tmp_connector->dpms == DRM_MODE_DPMS_ON) {
			active = true;
			break;
		}
	}

	crtc_state->active = active;
	ret = drm_atomic_commit(state);
out:
	if (ret != 0)
		connector->dpms = old_mode;
	return ret;
}

int drm_atomic_set_property(struct drm_atomic_state *state,
			    struct drm_mode_object *obj,
			    struct drm_property *prop,
			    uint64_t prop_value)
R
Rob Clark 已提交
2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273
{
	struct drm_mode_object *ref;
	int ret;

	if (!drm_property_change_valid_get(prop, prop_value, &ref))
		return -EINVAL;

	switch (obj->type) {
	case DRM_MODE_OBJECT_CONNECTOR: {
		struct drm_connector *connector = obj_to_connector(obj);
		struct drm_connector_state *connector_state;

		connector_state = drm_atomic_get_connector_state(state, connector);
		if (IS_ERR(connector_state)) {
			ret = PTR_ERR(connector_state);
			break;
		}

		ret = drm_atomic_connector_set_property(connector,
				connector_state, prop, prop_value);
		break;
	}
	case DRM_MODE_OBJECT_CRTC: {
		struct drm_crtc *crtc = obj_to_crtc(obj);
		struct drm_crtc_state *crtc_state;

		crtc_state = drm_atomic_get_crtc_state(state, crtc);
		if (IS_ERR(crtc_state)) {
			ret = PTR_ERR(crtc_state);
			break;
		}

		ret = drm_atomic_crtc_set_property(crtc,
				crtc_state, prop, prop_value);
		break;
	}
	case DRM_MODE_OBJECT_PLANE: {
		struct drm_plane *plane = obj_to_plane(obj);
		struct drm_plane_state *plane_state;

		plane_state = drm_atomic_get_plane_state(state, plane);
		if (IS_ERR(plane_state)) {
			ret = PTR_ERR(plane_state);
			break;
		}

		ret = drm_atomic_plane_set_property(plane,
				plane_state, prop, prop_value);
		break;
	}
	default:
		ret = -EINVAL;
		break;
	}

	drm_property_change_valid_put(prop, ref);
	return ret;
}

2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285
/**
 * DOC: explicit fencing properties
 *
 * Explicit fencing allows userspace to control the buffer synchronization
 * between devices. A Fence or a group of fences are transfered to/from
 * userspace using Sync File fds and there are two DRM properties for that.
 * IN_FENCE_FD on each DRM Plane to send fences to the kernel and
 * OUT_FENCE_PTR on each DRM CRTC to receive fences from the kernel.
 *
 * As a contrast, with implicit fencing the kernel keeps track of any
 * ongoing rendering, and automatically ensures that the atomic update waits
 * for any pending rendering to complete. For shared buffers represented with
2286
 * a &struct dma_buf this is tracked in &struct reservation_object.
2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301
 * Implicit syncing is how Linux traditionally worked (e.g. DRI2/3 on X.org),
 * whereas explicit fencing is what Android wants.
 *
 * "IN_FENCE_FD”:
 *	Use this property to pass a fence that DRM should wait on before
 *	proceeding with the Atomic Commit request and show the framebuffer for
 *	the plane on the screen. The fence can be either a normal fence or a
 *	merged one, the sync_file framework will handle both cases and use a
 *	fence_array if a merged fence is received. Passing -1 here means no
 *	fences to wait on.
 *
 *	If the Atomic Commit request has the DRM_MODE_ATOMIC_TEST_ONLY flag
 *	it will only check if the Sync File is a valid one.
 *
 *	On the driver side the fence is stored on the @fence parameter of
2302
 *	&struct drm_plane_state. Drivers which also support implicit fencing
2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320
 *	should set the implicit fence using drm_atomic_set_fence_for_plane(),
 *	to make sure there's consistent behaviour between drivers in precedence
 *	of implicit vs. explicit fencing.
 *
 * "OUT_FENCE_PTR”:
 *	Use this property to pass a file descriptor pointer to DRM. Once the
 *	Atomic Commit request call returns OUT_FENCE_PTR will be filled with
 *	the file descriptor number of a Sync File. This Sync File contains the
 *	CRTC fence that will be signaled when all framebuffers present on the
 *	Atomic Commit * request for that given CRTC are scanned out on the
 *	screen.
 *
 *	The Atomic Commit request fails if a invalid pointer is passed. If the
 *	Atomic Commit request fails for any other reason the out fence fd
 *	returned will be -1. On a Atomic Commit with the
 *	DRM_MODE_ATOMIC_TEST_ONLY flag the out fence will also be set to -1.
 *
 *	Note that out-fences don't have a special interface to drivers and are
2321
 *	internally represented by a &struct drm_pending_vblank_event in struct
2322 2323 2324 2325
 *	&drm_crtc_state, which is also used by the nonblocking atomic commit
 *	helpers and for the DRM event handling for existing userspace.
 */

2326
struct drm_out_fence_state {
2327
	s32 __user *out_fence_ptr;
2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348
	struct sync_file *sync_file;
	int fd;
};

static int setup_out_fence(struct drm_out_fence_state *fence_state,
			   struct dma_fence *fence)
{
	fence_state->fd = get_unused_fd_flags(O_CLOEXEC);
	if (fence_state->fd < 0)
		return fence_state->fd;

	if (put_user(fence_state->fd, fence_state->out_fence_ptr))
		return -EFAULT;

	fence_state->sync_file = sync_file_create(fence);
	if (!fence_state->sync_file)
		return -ENOMEM;

	return 0;
}

2349
static int prepare_signaling(struct drm_device *dev,
2350 2351 2352 2353 2354 2355 2356 2357
				  struct drm_atomic_state *state,
				  struct drm_mode_atomic *arg,
				  struct drm_file *file_priv,
				  struct drm_out_fence_state **fence_state,
				  unsigned int *num_fences)
{
	struct drm_crtc *crtc;
	struct drm_crtc_state *crtc_state;
2358 2359
	struct drm_connector *conn;
	struct drm_connector_state *conn_state;
2360
	int i, c = 0, ret;
2361 2362 2363 2364

	if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)
		return 0;

2365
	for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
2366
		s32 __user *fence_ptr;
2367 2368 2369 2370 2371 2372

		fence_ptr = get_out_fence_for_crtc(crtc_state->state, crtc);

		if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT || fence_ptr) {
			struct drm_pending_vblank_event *e;

2373
			e = create_vblank_event(crtc, arg->user_data);
2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408
			if (!e)
				return -ENOMEM;

			crtc_state->event = e;
		}

		if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
			struct drm_pending_vblank_event *e = crtc_state->event;

			if (!file_priv)
				continue;

			ret = drm_event_reserve_init(dev, file_priv, &e->base,
						     &e->event.base);
			if (ret) {
				kfree(e);
				crtc_state->event = NULL;
				return ret;
			}
		}

		if (fence_ptr) {
			struct dma_fence *fence;
			struct drm_out_fence_state *f;

			f = krealloc(*fence_state, sizeof(**fence_state) *
				     (*num_fences + 1), GFP_KERNEL);
			if (!f)
				return -ENOMEM;

			memset(&f[*num_fences], 0, sizeof(*f));

			f[*num_fences].out_fence_ptr = fence_ptr;
			*fence_state = f;

2409
			fence = drm_crtc_create_fence(crtc);
2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420
			if (!fence)
				return -ENOMEM;

			ret = setup_out_fence(&f[(*num_fences)++], fence);
			if (ret) {
				dma_fence_put(fence);
				return ret;
			}

			crtc_state->event->base.fence = fence;
		}
2421 2422

		c++;
2423 2424
	}

2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461
	for_each_new_connector_in_state(state, conn, conn_state, i) {
		struct drm_writeback_job *job;
		struct drm_out_fence_state *f;
		struct dma_fence *fence;
		s32 __user *fence_ptr;

		fence_ptr = get_out_fence_for_connector(state, conn);
		if (!fence_ptr)
			continue;

		job = drm_atomic_get_writeback_job(conn_state);
		if (!job)
			return -ENOMEM;

		f = krealloc(*fence_state, sizeof(**fence_state) *
			     (*num_fences + 1), GFP_KERNEL);
		if (!f)
			return -ENOMEM;

		memset(&f[*num_fences], 0, sizeof(*f));

		f[*num_fences].out_fence_ptr = fence_ptr;
		*fence_state = f;

		fence = drm_writeback_get_out_fence((struct drm_writeback_connector *)conn);
		if (!fence)
			return -ENOMEM;

		ret = setup_out_fence(&f[(*num_fences)++], fence);
		if (ret) {
			dma_fence_put(fence);
			return ret;
		}

		job->out_fence = fence;
	}

2462 2463 2464 2465 2466 2467 2468
	/*
	 * Having this flag means user mode pends on event which will never
	 * reach due to lack of at least one CRTC for signaling
	 */
	if (c == 0 && (arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
		return -EINVAL;

2469 2470 2471
	return 0;
}

2472 2473 2474 2475 2476
static void complete_signaling(struct drm_device *dev,
			       struct drm_atomic_state *state,
			       struct drm_out_fence_state *fence_state,
			       unsigned int num_fences,
			       bool install_fds)
2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490
{
	struct drm_crtc *crtc;
	struct drm_crtc_state *crtc_state;
	int i;

	if (install_fds) {
		for (i = 0; i < num_fences; i++)
			fd_install(fence_state[i].fd,
				   fence_state[i].sync_file->file);

		kfree(fence_state);
		return;
	}

2491
	for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
2492
		struct drm_pending_vblank_event *event = crtc_state->event;
2493
		/*
2494 2495 2496
		 * Free the allocated event. drm_atomic_helper_setup_commit
		 * can allocate an event too, so only free it if it's ours
		 * to prevent a double free in drm_atomic_state_clear.
2497
		 */
2498 2499 2500 2501
		if (event && (event->base.fence || event->base.file_priv)) {
			drm_event_cancel_free(dev, &event->base);
			crtc_state->event = NULL;
		}
2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521
	}

	if (!fence_state)
		return;

	for (i = 0; i < num_fences; i++) {
		if (fence_state[i].sync_file)
			fput(fence_state[i].sync_file->file);
		if (fence_state[i].fd >= 0)
			put_unused_fd(fence_state[i].fd);

		/* If this fails log error to the user */
		if (fence_state[i].out_fence_ptr &&
		    put_user(-1, fence_state[i].out_fence_ptr))
			DRM_DEBUG_ATOMIC("Couldn't clear out_fence_ptr\n");
	}

	kfree(fence_state);
}

R
Rob Clark 已提交
2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532
int drm_mode_atomic_ioctl(struct drm_device *dev,
			  void *data, struct drm_file *file_priv)
{
	struct drm_mode_atomic *arg = data;
	uint32_t __user *objs_ptr = (uint32_t __user *)(unsigned long)(arg->objs_ptr);
	uint32_t __user *count_props_ptr = (uint32_t __user *)(unsigned long)(arg->count_props_ptr);
	uint32_t __user *props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
	uint64_t __user *prop_values_ptr = (uint64_t __user *)(unsigned long)(arg->prop_values_ptr);
	unsigned int copied_objs, copied_props;
	struct drm_atomic_state *state;
	struct drm_modeset_acquire_ctx ctx;
2533
	struct drm_out_fence_state *fence_state;
R
Rob Clark 已提交
2534
	int ret = 0;
2535
	unsigned int i, j, num_fences;
R
Rob Clark 已提交
2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562

	/* disallow for drivers not supporting atomic: */
	if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
		return -EINVAL;

	/* disallow for userspace that has not enabled atomic cap (even
	 * though this may be a bit overkill, since legacy userspace
	 * wouldn't know how to call this ioctl)
	 */
	if (!file_priv->atomic)
		return -EINVAL;

	if (arg->flags & ~DRM_MODE_ATOMIC_FLAGS)
		return -EINVAL;

	if (arg->reserved)
		return -EINVAL;

	if ((arg->flags & DRM_MODE_PAGE_FLIP_ASYNC) &&
			!dev->mode_config.async_page_flip)
		return -EINVAL;

	/* can't test and expect an event at the same time. */
	if ((arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) &&
			(arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
		return -EINVAL;

2563
	drm_modeset_acquire_init(&ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE);
R
Rob Clark 已提交
2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574

	state = drm_atomic_state_alloc(dev);
	if (!state)
		return -ENOMEM;

	state->acquire_ctx = &ctx;
	state->allow_modeset = !!(arg->flags & DRM_MODE_ATOMIC_ALLOW_MODESET);

retry:
	copied_objs = 0;
	copied_props = 0;
2575 2576
	fence_state = NULL;
	num_fences = 0;
R
Rob Clark 已提交
2577 2578 2579 2580 2581 2582 2583

	for (i = 0; i < arg->count_objs; i++) {
		uint32_t obj_id, count_props;
		struct drm_mode_object *obj;

		if (get_user(obj_id, objs_ptr + copied_objs)) {
			ret = -EFAULT;
2584
			goto out;
R
Rob Clark 已提交
2585 2586
		}

2587
		obj = drm_mode_object_find(dev, file_priv, obj_id, DRM_MODE_OBJECT_ANY);
2588 2589 2590 2591 2592 2593
		if (!obj) {
			ret = -ENOENT;
			goto out;
		}

		if (!obj->properties) {
2594
			drm_mode_object_put(obj);
R
Rob Clark 已提交
2595
			ret = -ENOENT;
2596
			goto out;
R
Rob Clark 已提交
2597 2598 2599
		}

		if (get_user(count_props, count_props_ptr + copied_objs)) {
2600
			drm_mode_object_put(obj);
R
Rob Clark 已提交
2601
			ret = -EFAULT;
2602
			goto out;
R
Rob Clark 已提交
2603 2604 2605 2606 2607 2608 2609 2610 2611 2612
		}

		copied_objs++;

		for (j = 0; j < count_props; j++) {
			uint32_t prop_id;
			uint64_t prop_value;
			struct drm_property *prop;

			if (get_user(prop_id, props_ptr + copied_props)) {
2613
				drm_mode_object_put(obj);
R
Rob Clark 已提交
2614
				ret = -EFAULT;
2615
				goto out;
R
Rob Clark 已提交
2616 2617
			}

2618
			prop = drm_mode_obj_find_prop_id(obj, prop_id);
R
Rob Clark 已提交
2619
			if (!prop) {
2620
				drm_mode_object_put(obj);
R
Rob Clark 已提交
2621
				ret = -ENOENT;
2622
				goto out;
R
Rob Clark 已提交
2623 2624
			}

2625 2626 2627
			if (copy_from_user(&prop_value,
					   prop_values_ptr + copied_props,
					   sizeof(prop_value))) {
2628
				drm_mode_object_put(obj);
R
Rob Clark 已提交
2629
				ret = -EFAULT;
2630
				goto out;
R
Rob Clark 已提交
2631 2632
			}

2633 2634
			ret = drm_atomic_set_property(state, obj, prop,
						      prop_value);
2635
			if (ret) {
2636
				drm_mode_object_put(obj);
2637
				goto out;
2638
			}
R
Rob Clark 已提交
2639 2640 2641

			copied_props++;
		}
2642

2643
		drm_mode_object_put(obj);
R
Rob Clark 已提交
2644 2645
	}

2646 2647
	ret = prepare_signaling(dev, state, arg, file_priv, &fence_state,
				&num_fences);
2648 2649
	if (ret)
		goto out;
R
Rob Clark 已提交
2650 2651 2652 2653

	if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) {
		ret = drm_atomic_check_only(state);
	} else if (arg->flags & DRM_MODE_ATOMIC_NONBLOCK) {
2654
		ret = drm_atomic_nonblocking_commit(state);
R
Rob Clark 已提交
2655
	} else {
2656 2657 2658
		if (unlikely(drm_debug & DRM_UT_STATE))
			drm_atomic_print_state(state);

R
Rob Clark 已提交
2659 2660 2661
		ret = drm_atomic_commit(state);
	}

2662
out:
2663
	complete_signaling(dev, state, fence_state, num_fences, !ret);
2664

2665 2666
	if (ret == -EDEADLK) {
		drm_atomic_state_clear(state);
2667 2668 2669
		ret = drm_modeset_backoff(&ctx);
		if (!ret)
			goto retry;
2670
	}
R
Rob Clark 已提交
2671

2672
	drm_atomic_state_put(state);
R
Rob Clark 已提交
2673 2674 2675 2676 2677 2678

	drm_modeset_drop_locks(&ctx);
	drm_modeset_acquire_fini(&ctx);

	return ret;
}