drm_atomic.c 50.4 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
/*
 * 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>
 */


S
Sam Ravnborg 已提交
29 30
#include <linux/sync_file.h>

D
Daniel Vetter 已提交
31
#include <drm/drm_atomic.h>
D
Daniel Vetter 已提交
32
#include <drm/drm_atomic_uapi.h>
33
#include <drm/drm_bridge.h>
S
Sam Ravnborg 已提交
34 35 36 37 38
#include <drm/drm_debugfs.h>
#include <drm/drm_device.h>
#include <drm/drm_drv.h>
#include <drm/drm_file.h>
#include <drm/drm_fourcc.h>
39
#include <drm/drm_mode.h>
40
#include <drm/drm_print.h>
41
#include <drm/drm_writeback.h>
D
Daniel Vetter 已提交
42

43
#include "drm_crtc_internal.h"
44
#include "drm_internal.h"
45

46
void __drm_crtc_commit_free(struct kref *kref)
47 48 49 50 51 52
{
	struct drm_crtc_commit *commit =
		container_of(kref, struct drm_crtc_commit, ref);

	kfree(commit);
}
53
EXPORT_SYMBOL(__drm_crtc_commit_free);
54

55 56 57 58 59 60
/**
 * 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.
61 62
 * This should only be used by drivers which are still subclassing
 * &drm_atomic_state and haven't switched to &drm_private_state yet.
63 64
 */
void drm_atomic_state_default_release(struct drm_atomic_state *state)
D
Daniel Vetter 已提交
65 66 67 68
{
	kfree(state->connectors);
	kfree(state->crtcs);
	kfree(state->planes);
69
	kfree(state->private_objs);
D
Daniel Vetter 已提交
70
}
71
EXPORT_SYMBOL(drm_atomic_state_default_release);
D
Daniel Vetter 已提交
72 73

/**
74
 * drm_atomic_state_init - init new atomic state
D
Daniel Vetter 已提交
75
 * @dev: DRM device
76
 * @state: atomic state
D
Daniel Vetter 已提交
77
 *
78
 * Default implementation for filling in a new atomic state.
79 80
 * 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 已提交
81
 */
82 83
int
drm_atomic_state_init(struct drm_device *dev, struct drm_atomic_state *state)
D
Daniel Vetter 已提交
84
{
85 86
	kref_init(&state->ref);

R
Rob Clark 已提交
87 88 89 90 91
	/* TODO legacy paths should maybe do a better job about
	 * setting this appropriately?
	 */
	state->allow_modeset = true;

D
Daniel Vetter 已提交
92 93 94 95 96 97 98 99 100 101 102
	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;

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

105
	return 0;
D
Daniel Vetter 已提交
106
fail:
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
	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) {
124 125
		struct drm_atomic_state *state;

126 127 128 129 130 131 132 133 134
		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 已提交
135

136
	return config->funcs->atomic_state_alloc(dev);
D
Daniel Vetter 已提交
137 138 139 140
}
EXPORT_SYMBOL(drm_atomic_state_alloc);

/**
141
 * drm_atomic_state_default_clear - clear base atomic state
D
Daniel Vetter 已提交
142 143
 * @state: atomic state
 *
144
 * Default implementation for clearing atomic state.
145 146
 * 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 已提交
147
 */
148
void drm_atomic_state_default_clear(struct drm_atomic_state *state)
D
Daniel Vetter 已提交
149 150
{
	struct drm_device *dev = state->dev;
151
	struct drm_mode_config *config = &dev->mode_config;
D
Daniel Vetter 已提交
152 153
	int i;

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

156
	for (i = 0; i < state->num_connector; i++) {
157
		struct drm_connector *connector = state->connectors[i].ptr;
D
Daniel Vetter 已提交
158 159 160 161

		if (!connector)
			continue;

162
		connector->funcs->atomic_destroy_state(connector,
163 164 165
						       state->connectors[i].state);
		state->connectors[i].ptr = NULL;
		state->connectors[i].state = NULL;
166 167
		state->connectors[i].old_state = NULL;
		state->connectors[i].new_state = NULL;
168
		drm_connector_put(connector);
D
Daniel Vetter 已提交
169 170
	}

171
	for (i = 0; i < config->num_crtc; i++) {
172
		struct drm_crtc *crtc = state->crtcs[i].ptr;
D
Daniel Vetter 已提交
173 174 175 176 177

		if (!crtc)
			continue;

		crtc->funcs->atomic_destroy_state(crtc,
178
						  state->crtcs[i].state);
179

180 181
		state->crtcs[i].ptr = NULL;
		state->crtcs[i].state = NULL;
182 183
		state->crtcs[i].old_state = NULL;
		state->crtcs[i].new_state = NULL;
184 185 186 187 188

		if (state->crtcs[i].commit) {
			drm_crtc_commit_put(state->crtcs[i].commit);
			state->crtcs[i].commit = NULL;
		}
D
Daniel Vetter 已提交
189 190
	}

191
	for (i = 0; i < config->num_total_plane; i++) {
192
		struct drm_plane *plane = state->planes[i].ptr;
D
Daniel Vetter 已提交
193 194 195 196 197

		if (!plane)
			continue;

		plane->funcs->atomic_destroy_state(plane,
198 199 200
						   state->planes[i].state);
		state->planes[i].ptr = NULL;
		state->planes[i].state = NULL;
201 202
		state->planes[i].old_state = NULL;
		state->planes[i].new_state = NULL;
D
Daniel Vetter 已提交
203
	}
204 205

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

208 209 210 211
		obj->funcs->atomic_destroy_state(obj,
						 state->private_objs[i].state);
		state->private_objs[i].ptr = NULL;
		state->private_objs[i].state = NULL;
212 213
		state->private_objs[i].old_state = NULL;
		state->private_objs[i].new_state = NULL;
214 215 216
	}
	state->num_private_objs = 0;

217 218 219 220
	if (state->fake_commit) {
		drm_crtc_commit_put(state->fake_commit);
		state->fake_commit = NULL;
	}
D
Daniel Vetter 已提交
221
}
222 223 224 225 226 227 228 229 230 231
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
232 233
 * state. Which could break assumptions the driver's
 * &drm_mode_config_funcs.atomic_check likely relies on.
234 235 236 237 238 239 240 241 242 243 244 245 246 247
 *
 * 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 已提交
248 249 250
EXPORT_SYMBOL(drm_atomic_state_clear);

/**
251 252
 * __drm_atomic_state_free - free all memory for an atomic state
 * @ref: This atomic state to deallocate
D
Daniel Vetter 已提交
253 254
 *
 * This frees all memory associated with an atomic state, including all the
255
 * per-object state for planes, CRTCs and connectors.
D
Daniel Vetter 已提交
256
 */
257
void __drm_atomic_state_free(struct kref *ref)
D
Daniel Vetter 已提交
258
{
259 260
	struct drm_atomic_state *state = container_of(ref, typeof(*state), ref);
	struct drm_mode_config *config = &state->dev->mode_config;
261

D
Daniel Vetter 已提交
262 263
	drm_atomic_state_clear(state);

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

266 267 268 269 270 271
	if (config->funcs->atomic_state_free) {
		config->funcs->atomic_state_free(state);
	} else {
		drm_atomic_state_default_release(state);
		kfree(state);
	}
D
Daniel Vetter 已提交
272
}
273
EXPORT_SYMBOL(__drm_atomic_state_free);
D
Daniel Vetter 已提交
274 275

/**
276
 * drm_atomic_get_crtc_state - get CRTC state
D
Daniel Vetter 已提交
277
 * @state: global atomic state object
278
 * @crtc: CRTC to get state object for
D
Daniel Vetter 已提交
279
 *
280 281
 * 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
D
Daniel Vetter 已提交
282 283 284 285 286 287 288 289 290 291 292 293
 * 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)
{
294
	int ret, index = drm_crtc_index(crtc);
D
Daniel Vetter 已提交
295 296
	struct drm_crtc_state *crtc_state;

297 298
	WARN_ON(!state->acquire_ctx);

299 300 301
	crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
	if (crtc_state)
		return crtc_state;
D
Daniel Vetter 已提交
302 303 304 305 306 307 308 309 310

	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);

311
	state->crtcs[index].state = crtc_state;
312 313
	state->crtcs[index].old_state = crtc->state;
	state->crtcs[index].new_state = crtc_state;
314
	state->crtcs[index].ptr = crtc;
D
Daniel Vetter 已提交
315 316
	crtc_state->state = state;

317 318
	DRM_DEBUG_ATOMIC("Added [CRTC:%d:%s] %p state to %p\n",
			 crtc->base.id, crtc->name, crtc_state, state);
D
Daniel Vetter 已提交
319 320 321 322 323

	return crtc_state;
}
EXPORT_SYMBOL(drm_atomic_get_crtc_state);

324 325
static int drm_atomic_crtc_check(const struct drm_crtc_state *old_crtc_state,
				 const struct drm_crtc_state *new_crtc_state)
R
Rob Clark 已提交
326
{
327 328
	struct drm_crtc *crtc = new_crtc_state->crtc;

R
Rob Clark 已提交
329 330 331 332 333 334 335
	/* 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.
	 */
336

337
	if (new_crtc_state->active && !new_crtc_state->enable) {
338 339
		DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active without enabled\n",
				 crtc->base.id, crtc->name);
340 341 342
		return -EINVAL;
	}

343 344 345 346
	/* 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) &&
347
	    WARN_ON(new_crtc_state->enable && !new_crtc_state->mode_blob)) {
348 349
		DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled without mode blob\n",
				 crtc->base.id, crtc->name);
350 351 352 353
		return -EINVAL;
	}

	if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
354
	    WARN_ON(!new_crtc_state->enable && new_crtc_state->mode_blob)) {
355 356
		DRM_DEBUG_ATOMIC("[CRTC:%d:%s] disabled with mode blob\n",
				 crtc->base.id, crtc->name);
357 358 359
		return -EINVAL;
	}

360 361 362 363 364 365 366 367 368 369
	/*
	 * 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.
	 */
370 371
	if (new_crtc_state->event &&
	    !new_crtc_state->active && !old_crtc_state->active) {
372 373
		DRM_DEBUG_ATOMIC("[CRTC:%d:%s] requesting event but off\n",
				 crtc->base.id, crtc->name);
374 375 376
		return -EINVAL;
	}

R
Rob Clark 已提交
377 378 379
	return 0;
}

380 381 382 383 384 385 386 387
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);
388
	drm_printf(p, "\tself_refresh_active=%d\n", state->self_refresh_active);
389 390 391 392 393 394 395 396 397 398 399 400 401 402
	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);
}

403 404 405 406 407
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;
408 409 410 411 412
	const struct drm_display_info *info = &connector->display_info;

	state->max_bpc = info->bpc ? info->bpc : 8;
	if (connector->max_bpc_property)
		state->max_bpc = min(state->max_bpc, state->max_requested_bpc);
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433

	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;
	}

434 435 436 437 438 439 440 441 442
	if (!writeback_job->fb) {
		if (writeback_job->out_fence) {
			DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] requesting out-fence without framebuffer\n",
					 connector->base.id, connector->name);
			return -EINVAL;
		}

		drm_writeback_cleanup_job(writeback_job);
		state->writeback_job = NULL;
443 444
	}

445 446 447
	return 0;
}

D
Daniel Vetter 已提交
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
/**
 * 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)
{
467
	int ret, index = drm_plane_index(plane);
D
Daniel Vetter 已提交
468 469
	struct drm_plane_state *plane_state;

470 471
	WARN_ON(!state->acquire_ctx);

472 473 474 475 476
	/* the legacy pointers should never be set */
	WARN_ON(plane->fb);
	WARN_ON(plane->old_fb);
	WARN_ON(plane->crtc);

477 478 479
	plane_state = drm_atomic_get_existing_plane_state(state, plane);
	if (plane_state)
		return plane_state;
D
Daniel Vetter 已提交
480

D
Daniel Vetter 已提交
481
	ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
D
Daniel Vetter 已提交
482 483 484 485 486 487 488
	if (ret)
		return ERR_PTR(ret);

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

489 490
	state->planes[index].state = plane_state;
	state->planes[index].ptr = plane;
491 492
	state->planes[index].old_state = plane->state;
	state->planes[index].new_state = plane_state;
D
Daniel Vetter 已提交
493 494
	plane_state->state = state;

495 496
	DRM_DEBUG_ATOMIC("Added [PLANE:%d:%s] %p state to %p\n",
			 plane->base.id, plane->name, plane_state, state);
D
Daniel Vetter 已提交
497 498 499 500 501 502 503 504 505 506 507 508 509 510

	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);

511
static bool
512 513
plane_switching_crtc(const struct drm_plane_state *old_plane_state,
		     const struct drm_plane_state *new_plane_state)
514
{
515
	if (!old_plane_state->crtc || !new_plane_state->crtc)
516 517
		return false;

518
	if (old_plane_state->crtc == new_plane_state->crtc)
519 520 521 522 523 524 525 526 527 528
		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 已提交
529 530
/**
 * drm_atomic_plane_check - check plane state
531 532
 * @old_plane_state: old plane state to check
 * @new_plane_state: new plane state to check
R
Rob Clark 已提交
533 534 535 536 537 538
 *
 * Provides core sanity checks for plane state.
 *
 * RETURNS:
 * Zero on success, error code on failure
 */
539 540
static int drm_atomic_plane_check(const struct drm_plane_state *old_plane_state,
				  const struct drm_plane_state *new_plane_state)
R
Rob Clark 已提交
541
{
542 543 544
	struct drm_plane *plane = new_plane_state->plane;
	struct drm_crtc *crtc = new_plane_state->crtc;
	const struct drm_framebuffer *fb = new_plane_state->fb;
R
Rob Clark 已提交
545
	unsigned int fb_width, fb_height;
546 547
	struct drm_mode_rect *clips;
	uint32_t num_clips;
548
	int ret;
R
Rob Clark 已提交
549 550

	/* either *both* CRTC and FB must be set, or neither */
551
	if (crtc && !fb) {
552 553
		DRM_DEBUG_ATOMIC("[PLANE:%d:%s] CRTC set but no FB\n",
				 plane->base.id, plane->name);
R
Rob Clark 已提交
554
		return -EINVAL;
555
	} else if (fb && !crtc) {
556 557
		DRM_DEBUG_ATOMIC("[PLANE:%d:%s] FB set but no CRTC\n",
				 plane->base.id, plane->name);
R
Rob Clark 已提交
558 559 560 561
		return -EINVAL;
	}

	/* if disabled, we don't care about the rest of the state: */
562
	if (!crtc)
R
Rob Clark 已提交
563 564 565
		return 0;

	/* Check whether this plane is usable on this CRTC */
566
	if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
567
		DRM_DEBUG_ATOMIC("Invalid [CRTC:%d:%s] for [PLANE:%d:%s]\n",
568
				 crtc->base.id, crtc->name,
569
				 plane->base.id, plane->name);
R
Rob Clark 已提交
570 571 572 573
		return -EINVAL;
	}

	/* Check whether this plane supports the fb pixel format. */
574 575
	ret = drm_plane_check_pixel_format(plane, fb->format->format,
					   fb->modifier);
576
	if (ret) {
577
		struct drm_format_name_buf format_name;
578 579
		DRM_DEBUG_ATOMIC("[PLANE:%d:%s] invalid pixel format %s, modifier 0x%llx\n",
				 plane->base.id, plane->name,
580
				 drm_get_format_name(fb->format->format,
581
						     &format_name),
582
				 fb->modifier);
583
		return ret;
R
Rob Clark 已提交
584 585 586
	}

	/* Give drivers some help against integer overflows */
587 588 589 590
	if (new_plane_state->crtc_w > INT_MAX ||
	    new_plane_state->crtc_x > INT_MAX - (int32_t) new_plane_state->crtc_w ||
	    new_plane_state->crtc_h > INT_MAX ||
	    new_plane_state->crtc_y > INT_MAX - (int32_t) new_plane_state->crtc_h) {
591 592
		DRM_DEBUG_ATOMIC("[PLANE:%d:%s] invalid CRTC coordinates %ux%u+%d+%d\n",
				 plane->base.id, plane->name,
593 594
				 new_plane_state->crtc_w, new_plane_state->crtc_h,
				 new_plane_state->crtc_x, new_plane_state->crtc_y);
R
Rob Clark 已提交
595 596 597
		return -ERANGE;
	}

598 599
	fb_width = fb->width << 16;
	fb_height = fb->height << 16;
R
Rob Clark 已提交
600 601

	/* Make sure source coordinates are inside the fb. */
602 603 604 605
	if (new_plane_state->src_w > fb_width ||
	    new_plane_state->src_x > fb_width - new_plane_state->src_w ||
	    new_plane_state->src_h > fb_height ||
	    new_plane_state->src_y > fb_height - new_plane_state->src_h) {
606
		DRM_DEBUG_ATOMIC("[PLANE:%d:%s] invalid source coordinates "
607
				 "%u.%06ux%u.%06u+%u.%06u+%u.%06u (fb %ux%u)\n",
608
				 plane->base.id, plane->name,
609 610 611 612 613 614 615 616 617
				 new_plane_state->src_w >> 16,
				 ((new_plane_state->src_w & 0xffff) * 15625) >> 10,
				 new_plane_state->src_h >> 16,
				 ((new_plane_state->src_h & 0xffff) * 15625) >> 10,
				 new_plane_state->src_x >> 16,
				 ((new_plane_state->src_x & 0xffff) * 15625) >> 10,
				 new_plane_state->src_y >> 16,
				 ((new_plane_state->src_y & 0xffff) * 15625) >> 10,
				 fb->width, fb->height);
R
Rob Clark 已提交
618 619 620
		return -ENOSPC;
	}

621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
	clips = drm_plane_get_damage_clips(new_plane_state);
	num_clips = drm_plane_get_damage_clips_count(new_plane_state);

	/* Make sure damage clips are valid and inside the fb. */
	while (num_clips > 0) {
		if (clips->x1 >= clips->x2 ||
		    clips->y1 >= clips->y2 ||
		    clips->x1 < 0 ||
		    clips->y1 < 0 ||
		    clips->x2 > fb_width ||
		    clips->y2 > fb_height) {
			DRM_DEBUG_ATOMIC("[PLANE:%d:%s] invalid damage clip %d %d %d %d\n",
					 plane->base.id, plane->name, clips->x1,
					 clips->y1, clips->x2, clips->y2);
			return -EINVAL;
		}
		clips++;
		num_clips--;
	}

641
	if (plane_switching_crtc(old_plane_state, new_plane_state)) {
642 643
		DRM_DEBUG_ATOMIC("[PLANE:%d:%s] switching CRTC directly\n",
				 plane->base.id, plane->name);
644 645 646
		return -EINVAL;
	}

R
Rob Clark 已提交
647 648 649
	return 0;
}

650 651 652 653 654 655 656 657 658 659
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);
660 661
	if (state->fb)
		drm_framebuffer_print_info(p, 2, state->fb);
662 663 664
	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);
665
	drm_printf(p, "\tnormalized-zpos=%x\n", state->normalized_zpos);
666 667 668 669
	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));
670 671 672 673 674

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

675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
/**
 * 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
692 693 694 695 696 697
 * acquired by calling drm_atomic_get_private_obj_state(). This also takes care
 * of locking, hence drivers should not have a need to call drm_modeset_lock()
 * directly. Sequence of the actual hardware state commit is not handled,
 * drivers might need to keep track of struct drm_crtc_commit within subclassed
 * structure of &drm_private_state as necessary, e.g. similar to
 * &drm_plane_state.commit. See also &drm_atomic_state.fake_commit.
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
 *
 * 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.
 */

713 714
/**
 * drm_atomic_private_obj_init - initialize private object
715
 * @dev: DRM device this object will be attached to
716 717 718 719 720 721 722 723 724
 * @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
725 726
drm_atomic_private_obj_init(struct drm_device *dev,
			    struct drm_private_obj *obj,
727 728 729 730 731
			    struct drm_private_state *state,
			    const struct drm_private_state_funcs *funcs)
{
	memset(obj, 0, sizeof(*obj));

732 733
	drm_modeset_lock_init(&obj->lock);

734 735
	obj->state = state;
	obj->funcs = funcs;
736
	list_add_tail(&obj->head, &dev->mode_config.privobj_list);
737 738 739 740 741 742 743 744 745 746 747 748
}
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)
{
749
	list_del(&obj->head);
750
	obj->funcs->atomic_destroy_state(obj, obj->state);
751
	drm_modeset_lock_fini(&obj->lock);
752 753 754
}
EXPORT_SYMBOL(drm_atomic_private_obj_fini);

755 756 757 758 759 760
/**
 * 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,
761 762
 * allocating the state if needed. It will also grab the relevant private
 * object lock to make sure that the state is consistent.
763 764 765 766 767
 *
 * RETURNS:
 *
 * Either the allocated state or the error code encoded into a pointer.
 */
768 769 770
struct drm_private_state *
drm_atomic_get_private_obj_state(struct drm_atomic_state *state,
				 struct drm_private_obj *obj)
771
{
772
	int index, num_objs, i, ret;
773 774
	size_t size;
	struct __drm_private_objs_state *arr;
775
	struct drm_private_state *obj_state;
776 777

	for (i = 0; i < state->num_private_objs; i++)
778 779
		if (obj == state->private_objs[i].ptr)
			return state->private_objs[i].state;
780

781 782 783 784
	ret = drm_modeset_lock(&obj->lock, state->acquire_ctx);
	if (ret)
		return ERR_PTR(ret);

785 786 787 788 789 790 791 792 793 794
	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));

795 796
	obj_state = obj->funcs->atomic_duplicate_state(obj);
	if (!obj_state)
797 798
		return ERR_PTR(-ENOMEM);

799 800 801 802
	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;
803
	obj_state->state = state;
804

805 806
	state->num_private_objs = num_objs;

807 808
	DRM_DEBUG_ATOMIC("Added new private object %p state %p to %p\n",
			 obj, obj_state, state);
809

810
	return obj_state;
811 812 813
}
EXPORT_SYMBOL(drm_atomic_get_private_obj_state);

814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857
/**
 * drm_atomic_get_old_private_obj_state
 * @state: global atomic state object
 * @obj: private_obj to grab
 *
 * This function returns the old private object state for the given private_obj,
 * or NULL if the private_obj is not part of the global atomic state.
 */
struct drm_private_state *
drm_atomic_get_old_private_obj_state(struct drm_atomic_state *state,
				     struct drm_private_obj *obj)
{
	int i;

	for (i = 0; i < state->num_private_objs; i++)
		if (obj == state->private_objs[i].ptr)
			return state->private_objs[i].old_state;

	return NULL;
}
EXPORT_SYMBOL(drm_atomic_get_old_private_obj_state);

/**
 * drm_atomic_get_new_private_obj_state
 * @state: global atomic state object
 * @obj: private_obj to grab
 *
 * This function returns the new private object state for the given private_obj,
 * or NULL if the private_obj is not part of the global atomic state.
 */
struct drm_private_state *
drm_atomic_get_new_private_obj_state(struct drm_atomic_state *state,
				     struct drm_private_obj *obj)
{
	int i;

	for (i = 0; i < state->num_private_objs; i++)
		if (obj == state->private_objs[i].ptr)
			return state->private_objs[i].new_state;

	return NULL;
}
EXPORT_SYMBOL(drm_atomic_get_new_private_obj_state);

858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926
/**
 * drm_atomic_get_old_connector_for_encoder - Get old connector for an encoder
 * @state: Atomic state
 * @encoder: The encoder to fetch the connector state for
 *
 * This function finds and returns the connector that was connected to @encoder
 * as specified by the @state.
 *
 * If there is no connector in @state which previously had @encoder connected to
 * it, this function will return NULL. While this may seem like an invalid use
 * case, it is sometimes useful to differentiate commits which had no prior
 * connectors attached to @encoder vs ones that did (and to inspect their
 * state). This is especially true in enable hooks because the pipeline has
 * changed.
 *
 * Returns: The old connector connected to @encoder, or NULL if the encoder is
 * not connected.
 */
struct drm_connector *
drm_atomic_get_old_connector_for_encoder(struct drm_atomic_state *state,
					 struct drm_encoder *encoder)
{
	struct drm_connector_state *conn_state;
	struct drm_connector *connector;
	unsigned int i;

	for_each_old_connector_in_state(state, connector, conn_state, i) {
		if (conn_state->best_encoder == encoder)
			return connector;
	}

	return NULL;
}
EXPORT_SYMBOL(drm_atomic_get_old_connector_for_encoder);

/**
 * drm_atomic_get_new_connector_for_encoder - Get new connector for an encoder
 * @state: Atomic state
 * @encoder: The encoder to fetch the connector state for
 *
 * This function finds and returns the connector that will be connected to
 * @encoder as specified by the @state.
 *
 * If there is no connector in @state which will have @encoder connected to it,
 * this function will return NULL. While this may seem like an invalid use case,
 * it is sometimes useful to differentiate commits which have no connectors
 * attached to @encoder vs ones that do (and to inspect their state). This is
 * especially true in disable hooks because the pipeline will change.
 *
 * Returns: The new connector connected to @encoder, or NULL if the encoder is
 * not connected.
 */
struct drm_connector *
drm_atomic_get_new_connector_for_encoder(struct drm_atomic_state *state,
					 struct drm_encoder *encoder)
{
	struct drm_connector_state *conn_state;
	struct drm_connector *connector;
	unsigned int i;

	for_each_new_connector_in_state(state, connector, conn_state, i) {
		if (conn_state->best_encoder == encoder)
			return connector;
	}

	return NULL;
}
EXPORT_SYMBOL(drm_atomic_get_new_connector_for_encoder);

D
Daniel Vetter 已提交
927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949
/**
 * 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;

950 951
	WARN_ON(!state->acquire_ctx);

952 953 954 955
	ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
	if (ret)
		return ERR_PTR(ret);

D
Daniel Vetter 已提交
956 957
	index = drm_connector_index(connector);

958
	if (index >= state->num_connector) {
959
		struct __drm_connnectors_state *c;
960 961 962 963 964 965 966 967 968 969 970
		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;
971 972
	}

973 974
	if (state->connectors[index].state)
		return state->connectors[index].state;
D
Daniel Vetter 已提交
975 976 977 978 979

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

980
	drm_connector_get(connector);
981
	state->connectors[index].state = connector_state;
982 983
	state->connectors[index].old_state = connector->state;
	state->connectors[index].new_state = connector_state;
984
	state->connectors[index].ptr = connector;
D
Daniel Vetter 已提交
985 986
	connector_state->state = state;

987 988 989
	DRM_DEBUG_ATOMIC("Added [CONNECTOR:%d:%s] %p state to %p\n",
			 connector->base.id, connector->name,
			 connector_state, state);
D
Daniel Vetter 已提交
990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003

	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);

1004 1005 1006 1007 1008 1009 1010
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)");
1011
	drm_printf(p, "\tself_refresh_aware=%d\n", state->self_refresh_aware);
1012

1013 1014 1015 1016
	if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
		if (state->writeback_job && state->writeback_job->fb)
			drm_printf(p, "\tfb=%d\n", state->writeback_job->fb->base.id);

1017 1018 1019 1020
	if (connector->funcs->atomic_print_state)
		connector->funcs->atomic_print_state(p, state);
}

1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
/**
 * drm_atomic_get_bridge_state - get bridge state
 * @state: global atomic state object
 * @bridge: bridge to get state object for
 *
 * This function returns the bridge state for the given bridge, allocating it
 * if needed. It will also grab the relevant bridge 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.
 */
struct drm_bridge_state *
drm_atomic_get_bridge_state(struct drm_atomic_state *state,
			    struct drm_bridge *bridge)
{
	struct drm_private_state *obj_state;

	obj_state = drm_atomic_get_private_obj_state(state, &bridge->base);
	if (IS_ERR(obj_state))
		return ERR_CAST(obj_state);

	return drm_priv_to_bridge_state(obj_state);
}
EXPORT_SYMBOL(drm_atomic_get_bridge_state);

/**
 * drm_atomic_get_old_bridge_state - get old bridge state, if it exists
 * @state: global atomic state object
 * @bridge: bridge to grab
 *
 * This function returns the old bridge state for the given bridge, or NULL if
 * the bridge is not part of the global atomic state.
 */
struct drm_bridge_state *
drm_atomic_get_old_bridge_state(struct drm_atomic_state *state,
				struct drm_bridge *bridge)
{
	struct drm_private_state *obj_state;

	obj_state = drm_atomic_get_old_private_obj_state(state, &bridge->base);
	if (!obj_state)
		return NULL;

	return drm_priv_to_bridge_state(obj_state);
}
EXPORT_SYMBOL(drm_atomic_get_old_bridge_state);

/**
 * drm_atomic_get_new_bridge_state - get new bridge state, if it exists
 * @state: global atomic state object
 * @bridge: bridge to grab
 *
 * This function returns the new bridge state for the given bridge, or NULL if
 * the bridge is not part of the global atomic state.
 */
struct drm_bridge_state *
drm_atomic_get_new_bridge_state(struct drm_atomic_state *state,
				struct drm_bridge *bridge)
{
	struct drm_private_state *obj_state;

	obj_state = drm_atomic_get_new_private_obj_state(state, &bridge->base);
	if (!obj_state)
		return NULL;

	return drm_priv_to_bridge_state(obj_state);
}
EXPORT_SYMBOL(drm_atomic_get_new_bridge_state);

/**
 * drm_atomic_add_encoder_bridges - add bridges attached to an encoder
 * @state: atomic state
 * @encoder: DRM encoder
 *
 * This function adds all bridges attached to @encoder. This is needed to add
 * bridge states to @state and make them available when
1101 1102 1103
 * &drm_bridge_funcs.atomic_check(), &drm_bridge_funcs.atomic_pre_enable(),
 * &drm_bridge_funcs.atomic_enable(),
 * &drm_bridge_funcs.atomic_disable_post_disable() are called.
1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
 *
 * 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_encoder_bridges(struct drm_atomic_state *state,
			       struct drm_encoder *encoder)
{
	struct drm_bridge_state *bridge_state;
	struct drm_bridge *bridge;

	if (!encoder)
		return 0;

	DRM_DEBUG_ATOMIC("Adding all bridges for [encoder:%d:%s] to %p\n",
			 encoder->base.id, encoder->name, state);

	drm_for_each_bridge_in_chain(encoder, bridge) {
		/* Skip bridges that don't implement the atomic state hooks. */
		if (!bridge->funcs->atomic_duplicate_state)
			continue;

		bridge_state = drm_atomic_get_bridge_state(state, bridge);
		if (IS_ERR(bridge_state))
			return PTR_ERR(bridge_state);
	}

	return 0;
}
EXPORT_SYMBOL(drm_atomic_add_encoder_bridges);

D
Daniel Vetter 已提交
1137
/**
1138
 * drm_atomic_add_affected_connectors - add connectors for CRTC
D
Daniel Vetter 已提交
1139
 * @state: atomic state
1140
 * @crtc: DRM CRTC
D
Daniel Vetter 已提交
1141 1142 1143 1144
 *
 * 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
1145
 * unneeded seralization if the update is just for the planes on one CRTC. Hence
D
Daniel Vetter 已提交
1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160
 * 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;
1161
	struct drm_connector_list_iter conn_iter;
1162
	struct drm_crtc_state *crtc_state;
D
Daniel Vetter 已提交
1163 1164
	int ret;

1165 1166 1167 1168
	crtc_state = drm_atomic_get_crtc_state(state, crtc);
	if (IS_ERR(crtc_state))
		return PTR_ERR(crtc_state);

D
Daniel Vetter 已提交
1169 1170 1171 1172
	ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
	if (ret)
		return ret;

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

	/*
1177 1178
	 * Changed connectors are already in @state, so only need to look
	 * at the connector_mask in crtc_state.
D
Daniel Vetter 已提交
1179
	 */
1180
	drm_connector_list_iter_begin(state->dev, &conn_iter);
1181
	drm_for_each_connector_iter(connector, &conn_iter) {
V
Ville Syrjälä 已提交
1182
		if (!(crtc_state->connector_mask & drm_connector_mask(connector)))
D
Daniel Vetter 已提交
1183 1184 1185
			continue;

		conn_state = drm_atomic_get_connector_state(state, connector);
1186
		if (IS_ERR(conn_state)) {
1187
			drm_connector_list_iter_end(&conn_iter);
D
Daniel Vetter 已提交
1188
			return PTR_ERR(conn_state);
1189
		}
D
Daniel Vetter 已提交
1190
	}
1191
	drm_connector_list_iter_end(&conn_iter);
D
Daniel Vetter 已提交
1192 1193 1194 1195 1196

	return 0;
}
EXPORT_SYMBOL(drm_atomic_add_affected_connectors);

1197
/**
1198
 * drm_atomic_add_affected_planes - add planes for CRTC
1199
 * @state: atomic state
1200
 * @crtc: DRM CRTC
1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220
 *
 * 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)
{
1221 1222
	const struct drm_crtc_state *old_crtc_state =
		drm_atomic_get_old_crtc_state(state, crtc);
1223 1224
	struct drm_plane *plane;

1225
	WARN_ON(!drm_atomic_get_new_crtc_state(state, crtc));
1226

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

1230
	drm_for_each_plane_mask(plane, state->dev, old_crtc_state->plane_mask) {
1231 1232 1233 1234 1235 1236 1237 1238 1239 1240
		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 已提交
1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253
/**
 * 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 已提交
1254 1255
	struct drm_device *dev = state->dev;
	struct drm_mode_config *config = &dev->mode_config;
1256
	struct drm_plane *plane;
1257 1258
	struct drm_plane_state *old_plane_state;
	struct drm_plane_state *new_plane_state;
1259
	struct drm_crtc *crtc;
1260 1261
	struct drm_crtc_state *old_crtc_state;
	struct drm_crtc_state *new_crtc_state;
1262 1263
	struct drm_connector *conn;
	struct drm_connector_state *conn_state;
R
Rob Clark 已提交
1264
	int i, ret = 0;
D
Daniel Vetter 已提交
1265

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

1268 1269
	for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
		ret = drm_atomic_plane_check(old_plane_state, new_plane_state);
R
Rob Clark 已提交
1270
		if (ret) {
1271 1272
			DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic core check failed\n",
					 plane->base.id, plane->name);
R
Rob Clark 已提交
1273 1274 1275 1276
			return ret;
		}
	}

1277 1278
	for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
		ret = drm_atomic_crtc_check(old_crtc_state, new_crtc_state);
R
Rob Clark 已提交
1279
		if (ret) {
1280 1281
			DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic core check failed\n",
					 crtc->base.id, crtc->name);
R
Rob Clark 已提交
1282 1283 1284 1285
			return ret;
		}
	}

1286 1287 1288 1289 1290 1291 1292 1293 1294
	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;
		}
	}

1295
	if (config->funcs->atomic_check) {
R
Rob Clark 已提交
1296 1297
		ret = config->funcs->atomic_check(state->dev, state);

1298 1299 1300 1301 1302 1303
		if (ret) {
			DRM_DEBUG_ATOMIC("atomic driver check for %p failed: %d\n",
					 state, ret);
			return ret;
		}
	}
1304

R
Rob Clark 已提交
1305
	if (!state->allow_modeset) {
1306 1307
		for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
			if (drm_atomic_crtc_needs_modeset(new_crtc_state)) {
1308 1309
				DRM_DEBUG_ATOMIC("[CRTC:%d:%s] requires full modeset\n",
						 crtc->base.id, crtc->name);
R
Rob Clark 已提交
1310 1311 1312 1313 1314
				return -EINVAL;
			}
		}
	}

1315
	return 0;
D
Daniel Vetter 已提交
1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326
}
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.
 *
1327 1328
 * This function will take its own reference on @state.
 * Callers should always release their reference with drm_atomic_state_put().
D
Daniel Vetter 已提交
1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341
 *
 * 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;

1342
	DRM_DEBUG_ATOMIC("committing %p\n", state);
D
Daniel Vetter 已提交
1343 1344 1345 1346 1347 1348

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

/**
1349
 * drm_atomic_nonblocking_commit - atomic nonblocking commit
D
Daniel Vetter 已提交
1350 1351 1352 1353 1354 1355
 * @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.
 *
1356 1357
 * This function will take its own reference on @state.
 * Callers should always release their reference with drm_atomic_state_put().
D
Daniel Vetter 已提交
1358 1359 1360 1361
 *
 * Returns:
 * 0 on success, negative error code on failure.
 */
1362
int drm_atomic_nonblocking_commit(struct drm_atomic_state *state)
D
Daniel Vetter 已提交
1363 1364 1365 1366 1367 1368 1369 1370
{
	struct drm_mode_config *config = &state->dev->mode_config;
	int ret;

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

1371
	DRM_DEBUG_ATOMIC("committing %p nonblocking\n", state);
D
Daniel Vetter 已提交
1372 1373 1374

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

1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 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
/* just used from drm-client and atomic-helper: */
int __drm_atomic_helper_disable_plane(struct drm_plane *plane,
				      struct drm_plane_state *plane_state)
{
	int ret;

	ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
	if (ret != 0)
		return ret;

	drm_atomic_set_fb_for_plane(plane_state, NULL);
	plane_state->crtc_x = 0;
	plane_state->crtc_y = 0;
	plane_state->crtc_w = 0;
	plane_state->crtc_h = 0;
	plane_state->src_x = 0;
	plane_state->src_y = 0;
	plane_state->src_w = 0;
	plane_state->src_h = 0;

	return 0;
}
EXPORT_SYMBOL(__drm_atomic_helper_disable_plane);

static int update_output_state(struct drm_atomic_state *state,
			       struct drm_mode_set *set)
{
	struct drm_device *dev = set->crtc->dev;
	struct drm_crtc *crtc;
	struct drm_crtc_state *new_crtc_state;
	struct drm_connector *connector;
	struct drm_connector_state *new_conn_state;
	int ret, i;

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

	/* First disable all connectors on the target crtc. */
	ret = drm_atomic_add_affected_connectors(state, set->crtc);
	if (ret)
		return ret;

	for_each_new_connector_in_state(state, connector, new_conn_state, i) {
		if (new_conn_state->crtc == set->crtc) {
			ret = drm_atomic_set_crtc_for_connector(new_conn_state,
								NULL);
			if (ret)
				return ret;

			/* Make sure legacy setCrtc always re-trains */
			new_conn_state->link_status = DRM_LINK_STATUS_GOOD;
		}
	}

	/* Then set all connectors from set->connectors on the target crtc */
	for (i = 0; i < set->num_connectors; i++) {
		new_conn_state = drm_atomic_get_connector_state(state,
								set->connectors[i]);
		if (IS_ERR(new_conn_state))
			return PTR_ERR(new_conn_state);

		ret = drm_atomic_set_crtc_for_connector(new_conn_state,
							set->crtc);
		if (ret)
			return ret;
	}

	for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
		/*
		 * Don't update ->enable for the CRTC in the set_config request,
		 * since a mismatch would indicate a bug in the upper layers.
		 * The actual modeset code later on will catch any
		 * inconsistencies here.
		 */
		if (crtc == set->crtc)
			continue;

		if (!new_crtc_state->connector_mask) {
			ret = drm_atomic_set_mode_prop_for_crtc(new_crtc_state,
								NULL);
			if (ret < 0)
				return ret;

			new_crtc_state->active = false;
		}
	}

	return 0;
}

/* just used from drm-client and atomic-helper: */
int __drm_atomic_helper_set_config(struct drm_mode_set *set,
				   struct drm_atomic_state *state)
{
	struct drm_crtc_state *crtc_state;
	struct drm_plane_state *primary_state;
	struct drm_crtc *crtc = set->crtc;
	int hdisplay, vdisplay;
	int ret;

	crtc_state = drm_atomic_get_crtc_state(state, crtc);
	if (IS_ERR(crtc_state))
		return PTR_ERR(crtc_state);

	primary_state = drm_atomic_get_plane_state(state, crtc->primary);
	if (IS_ERR(primary_state))
		return PTR_ERR(primary_state);

	if (!set->mode) {
		WARN_ON(set->fb);
		WARN_ON(set->num_connectors);

		ret = drm_atomic_set_mode_for_crtc(crtc_state, NULL);
		if (ret != 0)
			return ret;

		crtc_state->active = false;

		ret = drm_atomic_set_crtc_for_plane(primary_state, NULL);
		if (ret != 0)
			return ret;

		drm_atomic_set_fb_for_plane(primary_state, NULL);

		goto commit;
	}

	WARN_ON(!set->fb);
	WARN_ON(!set->num_connectors);

	ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
	if (ret != 0)
		return ret;

	crtc_state->active = true;

	ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
	if (ret != 0)
		return ret;

	drm_mode_get_hv_timing(set->mode, &hdisplay, &vdisplay);

	drm_atomic_set_fb_for_plane(primary_state, set->fb);
	primary_state->crtc_x = 0;
	primary_state->crtc_y = 0;
	primary_state->crtc_w = hdisplay;
	primary_state->crtc_h = vdisplay;
	primary_state->src_x = set->x << 16;
	primary_state->src_y = set->y << 16;
	if (drm_rotation_90_or_270(primary_state->rotation)) {
		primary_state->src_w = vdisplay << 16;
		primary_state->src_h = hdisplay << 16;
	} else {
		primary_state->src_w = hdisplay << 16;
		primary_state->src_h = vdisplay << 16;
	}

commit:
	ret = update_output_state(state, set);
	if (ret)
		return ret;

	return 0;
}
EXPORT_SYMBOL(__drm_atomic_helper_set_config);

D
Daniel Vetter 已提交
1545
void drm_atomic_print_state(const struct drm_atomic_state *state)
1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557
{
	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);

1558
	for_each_new_plane_in_state(state, plane, plane_state, i)
1559 1560
		drm_atomic_plane_print_state(&p, plane_state);

1561
	for_each_new_crtc_in_state(state, crtc, crtc_state, i)
1562 1563
		drm_atomic_crtc_print_state(&p, crtc_state);

1564
	for_each_new_connector_in_state(state, connector, connector_state, i)
1565 1566 1567
		drm_atomic_connector_print_state(&p, connector_state);
}

1568 1569
static void __drm_state_dump(struct drm_device *dev, struct drm_printer *p,
			     bool take_locks)
1570 1571 1572 1573 1574
{
	struct drm_mode_config *config = &dev->mode_config;
	struct drm_plane *plane;
	struct drm_crtc *crtc;
	struct drm_connector *connector;
1575
	struct drm_connector_list_iter conn_iter;
1576

1577
	if (!drm_drv_uses_atomic_modeset(dev))
1578 1579
		return;

1580 1581 1582
	list_for_each_entry(plane, &config->plane_list, head) {
		if (take_locks)
			drm_modeset_lock(&plane->mutex, NULL);
1583
		drm_atomic_plane_print_state(p, plane->state);
1584 1585 1586
		if (take_locks)
			drm_modeset_unlock(&plane->mutex);
	}
1587

1588 1589 1590
	list_for_each_entry(crtc, &config->crtc_list, head) {
		if (take_locks)
			drm_modeset_lock(&crtc->mutex, NULL);
1591
		drm_atomic_crtc_print_state(p, crtc->state);
1592 1593 1594
		if (take_locks)
			drm_modeset_unlock(&crtc->mutex);
	}
1595

1596
	drm_connector_list_iter_begin(dev, &conn_iter);
1597 1598
	if (take_locks)
		drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
1599
	drm_for_each_connector_iter(connector, &conn_iter)
1600
		drm_atomic_connector_print_state(p, connector->state);
1601 1602
	if (take_locks)
		drm_modeset_unlock(&dev->mode_config.connection_mutex);
1603
	drm_connector_list_iter_end(&conn_iter);
1604
}
1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624

/**
 * 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);
}
1625 1626 1627 1628 1629 1630 1631 1632 1633
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);

1634
	__drm_state_dump(dev, &p, true);
1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650

	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