xref: /linux/drivers/gpu/drm/drm_atomic.c (revision 815e260a18a3af4dab59025ee99a7156c0e8b5e0)
1 /*
2  * Copyright (C) 2014 Red Hat
3  * Copyright (C) 2014 Intel Corp.
4  * Copyright (c) 2020-2021, The Linux Foundation. All rights reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22  * OTHER DEALINGS IN THE SOFTWARE.
23  *
24  * Authors:
25  * Rob Clark <robdclark@gmail.com>
26  * Daniel Vetter <daniel.vetter@ffwll.ch>
27  */
28 
29 #include <linux/export.h>
30 #include <linux/sync_file.h>
31 
32 #include <drm/drm_atomic.h>
33 #include <drm/drm_atomic_uapi.h>
34 #include <drm/drm_blend.h>
35 #include <drm/drm_bridge.h>
36 #include <drm/drm_debugfs.h>
37 #include <drm/drm_device.h>
38 #include <drm/drm_drv.h>
39 #include <drm/drm_file.h>
40 #include <drm/drm_fourcc.h>
41 #include <drm/drm_framebuffer.h>
42 #include <drm/drm_mode.h>
43 #include <drm/drm_print.h>
44 #include <drm/drm_writeback.h>
45 
46 #include "drm_crtc_internal.h"
47 #include "drm_internal.h"
48 
49 void __drm_crtc_commit_free(struct kref *kref)
50 {
51 	struct drm_crtc_commit *commit =
52 		container_of(kref, struct drm_crtc_commit, ref);
53 
54 	kfree(commit);
55 }
56 EXPORT_SYMBOL(__drm_crtc_commit_free);
57 
58 /**
59  * drm_crtc_commit_wait - Waits for a commit to complete
60  * @commit: &drm_crtc_commit to wait for
61  *
62  * Waits for a given &drm_crtc_commit to be programmed into the
63  * hardware and flipped to.
64  *
65  * Returns:
66  * 0 on success, a negative error code otherwise.
67  */
68 int drm_crtc_commit_wait(struct drm_crtc_commit *commit)
69 {
70 	unsigned long timeout = 10 * HZ;
71 	int ret;
72 
73 	if (!commit)
74 		return 0;
75 
76 	ret = wait_for_completion_timeout(&commit->hw_done, timeout);
77 	if (!ret) {
78 		drm_err(commit->crtc->dev, "hw_done timed out\n");
79 		return -ETIMEDOUT;
80 	}
81 
82 	/*
83 	 * Currently no support for overwriting flips, hence
84 	 * stall for previous one to execute completely.
85 	 */
86 	ret = wait_for_completion_timeout(&commit->flip_done, timeout);
87 	if (!ret) {
88 		drm_err(commit->crtc->dev, "flip_done timed out\n");
89 		return -ETIMEDOUT;
90 	}
91 
92 	return 0;
93 }
94 EXPORT_SYMBOL(drm_crtc_commit_wait);
95 
96 /**
97  * drm_atomic_state_default_release -
98  * release memory initialized by drm_atomic_state_init
99  * @state: atomic state
100  *
101  * Free all the memory allocated by drm_atomic_state_init.
102  * This should only be used by drivers which are still subclassing
103  * &drm_atomic_state and haven't switched to &drm_private_state yet.
104  */
105 void drm_atomic_state_default_release(struct drm_atomic_state *state)
106 {
107 	kfree(state->connectors);
108 	kfree(state->crtcs);
109 	kfree(state->planes);
110 	kfree(state->private_objs);
111 }
112 EXPORT_SYMBOL(drm_atomic_state_default_release);
113 
114 /**
115  * drm_atomic_state_init - init new atomic state
116  * @dev: DRM device
117  * @state: atomic state
118  *
119  * Default implementation for filling in a new atomic state.
120  * This should only be used by drivers which are still subclassing
121  * &drm_atomic_state and haven't switched to &drm_private_state yet.
122  */
123 int
124 drm_atomic_state_init(struct drm_device *dev, struct drm_atomic_state *state)
125 {
126 	kref_init(&state->ref);
127 
128 	/* TODO legacy paths should maybe do a better job about
129 	 * setting this appropriately?
130 	 */
131 	state->allow_modeset = true;
132 
133 	state->crtcs = kcalloc(dev->mode_config.num_crtc,
134 			       sizeof(*state->crtcs), GFP_KERNEL);
135 	if (!state->crtcs)
136 		goto fail;
137 	state->planes = kcalloc(dev->mode_config.num_total_plane,
138 				sizeof(*state->planes), GFP_KERNEL);
139 	if (!state->planes)
140 		goto fail;
141 
142 	/*
143 	 * Because drm_atomic_state can be committed asynchronously we need our
144 	 * own reference and cannot rely on the on implied by drm_file in the
145 	 * ioctl call.
146 	 */
147 	drm_dev_get(dev);
148 	state->dev = dev;
149 
150 	drm_dbg_atomic(dev, "Allocated atomic state %p\n", state);
151 
152 	return 0;
153 fail:
154 	drm_atomic_state_default_release(state);
155 	return -ENOMEM;
156 }
157 EXPORT_SYMBOL(drm_atomic_state_init);
158 
159 /**
160  * drm_atomic_state_alloc - allocate atomic state
161  * @dev: DRM device
162  *
163  * This allocates an empty atomic state to track updates.
164  */
165 struct drm_atomic_state *
166 drm_atomic_state_alloc(struct drm_device *dev)
167 {
168 	struct drm_mode_config *config = &dev->mode_config;
169 
170 	if (!config->funcs->atomic_state_alloc) {
171 		struct drm_atomic_state *state;
172 
173 		state = kzalloc(sizeof(*state), GFP_KERNEL);
174 		if (!state)
175 			return NULL;
176 		if (drm_atomic_state_init(dev, state) < 0) {
177 			kfree(state);
178 			return NULL;
179 		}
180 		return state;
181 	}
182 
183 	return config->funcs->atomic_state_alloc(dev);
184 }
185 EXPORT_SYMBOL(drm_atomic_state_alloc);
186 
187 /**
188  * drm_atomic_state_default_clear - clear base atomic state
189  * @state: atomic state
190  *
191  * Default implementation for clearing atomic state.
192  * This should only be used by drivers which are still subclassing
193  * &drm_atomic_state and haven't switched to &drm_private_state yet.
194  */
195 void drm_atomic_state_default_clear(struct drm_atomic_state *state)
196 {
197 	struct drm_device *dev = state->dev;
198 	struct drm_mode_config *config = &dev->mode_config;
199 	int i;
200 
201 	drm_dbg_atomic(dev, "Clearing atomic state %p\n", state);
202 
203 	state->checked = false;
204 
205 	for (i = 0; i < state->num_connector; i++) {
206 		struct drm_connector *connector = state->connectors[i].ptr;
207 
208 		if (!connector)
209 			continue;
210 
211 		connector->funcs->atomic_destroy_state(connector,
212 						       state->connectors[i].state_to_destroy);
213 		state->connectors[i].ptr = NULL;
214 		state->connectors[i].state_to_destroy = NULL;
215 		state->connectors[i].old_state = NULL;
216 		state->connectors[i].new_state = NULL;
217 		drm_connector_put(connector);
218 	}
219 
220 	for (i = 0; i < config->num_crtc; i++) {
221 		struct drm_crtc *crtc = state->crtcs[i].ptr;
222 
223 		if (!crtc)
224 			continue;
225 
226 		crtc->funcs->atomic_destroy_state(crtc,
227 						  state->crtcs[i].state_to_destroy);
228 
229 		state->crtcs[i].ptr = NULL;
230 		state->crtcs[i].state_to_destroy = NULL;
231 		state->crtcs[i].old_state = NULL;
232 		state->crtcs[i].new_state = NULL;
233 
234 		if (state->crtcs[i].commit) {
235 			drm_crtc_commit_put(state->crtcs[i].commit);
236 			state->crtcs[i].commit = NULL;
237 		}
238 	}
239 
240 	for (i = 0; i < config->num_total_plane; i++) {
241 		struct drm_plane *plane = state->planes[i].ptr;
242 
243 		if (!plane)
244 			continue;
245 
246 		plane->funcs->atomic_destroy_state(plane,
247 						   state->planes[i].state_to_destroy);
248 		state->planes[i].ptr = NULL;
249 		state->planes[i].state_to_destroy = NULL;
250 		state->planes[i].old_state = NULL;
251 		state->planes[i].new_state = NULL;
252 	}
253 
254 	for (i = 0; i < state->num_private_objs; i++) {
255 		struct drm_private_obj *obj = state->private_objs[i].ptr;
256 
257 		obj->funcs->atomic_destroy_state(obj,
258 						 state->private_objs[i].state_to_destroy);
259 		state->private_objs[i].ptr = NULL;
260 		state->private_objs[i].state_to_destroy = NULL;
261 		state->private_objs[i].old_state = NULL;
262 		state->private_objs[i].new_state = NULL;
263 	}
264 	state->num_private_objs = 0;
265 
266 	if (state->fake_commit) {
267 		drm_crtc_commit_put(state->fake_commit);
268 		state->fake_commit = NULL;
269 	}
270 }
271 EXPORT_SYMBOL(drm_atomic_state_default_clear);
272 
273 /**
274  * drm_atomic_state_clear - clear state object
275  * @state: atomic state
276  *
277  * When the w/w mutex algorithm detects a deadlock we need to back off and drop
278  * all locks. So someone else could sneak in and change the current modeset
279  * configuration. Which means that all the state assembled in @state is no
280  * longer an atomic update to the current state, but to some arbitrary earlier
281  * state. Which could break assumptions the driver's
282  * &drm_mode_config_funcs.atomic_check likely relies on.
283  *
284  * Hence we must clear all cached state and completely start over, using this
285  * function.
286  */
287 void drm_atomic_state_clear(struct drm_atomic_state *state)
288 {
289 	struct drm_device *dev = state->dev;
290 	struct drm_mode_config *config = &dev->mode_config;
291 
292 	if (config->funcs->atomic_state_clear)
293 		config->funcs->atomic_state_clear(state);
294 	else
295 		drm_atomic_state_default_clear(state);
296 }
297 EXPORT_SYMBOL(drm_atomic_state_clear);
298 
299 /**
300  * __drm_atomic_state_free - free all memory for an atomic state
301  * @ref: This atomic state to deallocate
302  *
303  * This frees all memory associated with an atomic state, including all the
304  * per-object state for planes, CRTCs and connectors.
305  */
306 void __drm_atomic_state_free(struct kref *ref)
307 {
308 	struct drm_atomic_state *state = container_of(ref, typeof(*state), ref);
309 	struct drm_device *dev = state->dev;
310 	struct drm_mode_config *config = &dev->mode_config;
311 
312 	drm_atomic_state_clear(state);
313 
314 	drm_dbg_atomic(state->dev, "Freeing atomic state %p\n", state);
315 
316 	if (config->funcs->atomic_state_free) {
317 		config->funcs->atomic_state_free(state);
318 	} else {
319 		drm_atomic_state_default_release(state);
320 		kfree(state);
321 	}
322 
323 	drm_dev_put(dev);
324 }
325 EXPORT_SYMBOL(__drm_atomic_state_free);
326 
327 /**
328  * drm_atomic_get_crtc_state - get CRTC state
329  * @state: global atomic state object
330  * @crtc: CRTC to get state object for
331  *
332  * This function returns the CRTC state for the given CRTC, allocating it if
333  * needed. It will also grab the relevant CRTC lock to make sure that the state
334  * is consistent.
335  *
336  * WARNING: Drivers may only add new CRTC states to a @state if
337  * drm_atomic_state.allow_modeset is set, or if it's a driver-internal commit
338  * not created by userspace through an IOCTL call.
339  *
340  * Returns:
341  * Either the allocated state or the error code encoded into the pointer. When
342  * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
343  * entire atomic sequence must be restarted. All other errors are fatal.
344  */
345 struct drm_crtc_state *
346 drm_atomic_get_crtc_state(struct drm_atomic_state *state,
347 			  struct drm_crtc *crtc)
348 {
349 	int ret, index = drm_crtc_index(crtc);
350 	struct drm_crtc_state *crtc_state;
351 
352 	WARN_ON(!state->acquire_ctx);
353 	drm_WARN_ON(state->dev, state->checked);
354 
355 	crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
356 	if (crtc_state)
357 		return crtc_state;
358 
359 	ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx);
360 	if (ret)
361 		return ERR_PTR(ret);
362 
363 	crtc_state = crtc->funcs->atomic_duplicate_state(crtc);
364 	if (!crtc_state)
365 		return ERR_PTR(-ENOMEM);
366 
367 	state->crtcs[index].state_to_destroy = crtc_state;
368 	state->crtcs[index].old_state = crtc->state;
369 	state->crtcs[index].new_state = crtc_state;
370 	state->crtcs[index].ptr = crtc;
371 	crtc_state->state = state;
372 
373 	drm_dbg_atomic(state->dev, "Added [CRTC:%d:%s] %p state to %p\n",
374 		       crtc->base.id, crtc->name, crtc_state, state);
375 
376 	return crtc_state;
377 }
378 EXPORT_SYMBOL(drm_atomic_get_crtc_state);
379 
380 static int drm_atomic_crtc_check(const struct drm_crtc_state *old_crtc_state,
381 				 const struct drm_crtc_state *new_crtc_state)
382 {
383 	struct drm_crtc *crtc = new_crtc_state->crtc;
384 
385 	/* NOTE: we explicitly don't enforce constraints such as primary
386 	 * layer covering entire screen, since that is something we want
387 	 * to allow (on hw that supports it).  For hw that does not, it
388 	 * should be checked in driver's crtc->atomic_check() vfunc.
389 	 *
390 	 * TODO: Add generic modeset state checks once we support those.
391 	 */
392 
393 	if (new_crtc_state->active && !new_crtc_state->enable) {
394 		drm_dbg_atomic(crtc->dev,
395 			       "[CRTC:%d:%s] active without enabled\n",
396 			       crtc->base.id, crtc->name);
397 		return -EINVAL;
398 	}
399 
400 	/* The state->enable vs. state->mode_blob checks can be WARN_ON,
401 	 * as this is a kernel-internal detail that userspace should never
402 	 * be able to trigger.
403 	 */
404 	if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
405 	    WARN_ON(new_crtc_state->enable && !new_crtc_state->mode_blob)) {
406 		drm_dbg_atomic(crtc->dev,
407 			       "[CRTC:%d:%s] enabled without mode blob\n",
408 			       crtc->base.id, crtc->name);
409 		return -EINVAL;
410 	}
411 
412 	if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
413 	    WARN_ON(!new_crtc_state->enable && new_crtc_state->mode_blob)) {
414 		drm_dbg_atomic(crtc->dev,
415 			       "[CRTC:%d:%s] disabled with mode blob\n",
416 			       crtc->base.id, crtc->name);
417 		return -EINVAL;
418 	}
419 
420 	/*
421 	 * Reject event generation for when a CRTC is off and stays off.
422 	 * It wouldn't be hard to implement this, but userspace has a track
423 	 * record of happily burning through 100% cpu (or worse, crash) when the
424 	 * display pipe is suspended. To avoid all that fun just reject updates
425 	 * that ask for events since likely that indicates a bug in the
426 	 * compositor's drawing loop. This is consistent with the vblank IOCTL
427 	 * and legacy page_flip IOCTL which also reject service on a disabled
428 	 * pipe.
429 	 */
430 	if (new_crtc_state->event &&
431 	    !new_crtc_state->active && !old_crtc_state->active) {
432 		drm_dbg_atomic(crtc->dev,
433 			       "[CRTC:%d:%s] requesting event but off\n",
434 			       crtc->base.id, crtc->name);
435 		return -EINVAL;
436 	}
437 
438 	return 0;
439 }
440 
441 static void drm_atomic_crtc_print_state(struct drm_printer *p,
442 		const struct drm_crtc_state *state)
443 {
444 	struct drm_crtc *crtc = state->crtc;
445 
446 	drm_printf(p, "crtc[%u]: %s\n", crtc->base.id, crtc->name);
447 	drm_printf(p, "\tenable=%d\n", state->enable);
448 	drm_printf(p, "\tactive=%d\n", state->active);
449 	drm_printf(p, "\tself_refresh_active=%d\n", state->self_refresh_active);
450 	drm_printf(p, "\tplanes_changed=%d\n", state->planes_changed);
451 	drm_printf(p, "\tmode_changed=%d\n", state->mode_changed);
452 	drm_printf(p, "\tactive_changed=%d\n", state->active_changed);
453 	drm_printf(p, "\tconnectors_changed=%d\n", state->connectors_changed);
454 	drm_printf(p, "\tcolor_mgmt_changed=%d\n", state->color_mgmt_changed);
455 	drm_printf(p, "\tplane_mask=%x\n", state->plane_mask);
456 	drm_printf(p, "\tconnector_mask=%x\n", state->connector_mask);
457 	drm_printf(p, "\tencoder_mask=%x\n", state->encoder_mask);
458 	drm_printf(p, "\tmode: " DRM_MODE_FMT "\n", DRM_MODE_ARG(&state->mode));
459 
460 	if (crtc->funcs->atomic_print_state)
461 		crtc->funcs->atomic_print_state(p, state);
462 }
463 
464 static int drm_atomic_connector_check(struct drm_connector *connector,
465 		struct drm_connector_state *state)
466 {
467 	struct drm_crtc_state *crtc_state;
468 	struct drm_writeback_job *writeback_job = state->writeback_job;
469 	const struct drm_display_info *info = &connector->display_info;
470 
471 	state->max_bpc = info->bpc ? info->bpc : 8;
472 	if (connector->max_bpc_property)
473 		state->max_bpc = min(state->max_bpc, state->max_requested_bpc);
474 
475 	if ((connector->connector_type != DRM_MODE_CONNECTOR_WRITEBACK) || !writeback_job)
476 		return 0;
477 
478 	if (writeback_job->fb && !state->crtc) {
479 		drm_dbg_atomic(connector->dev,
480 			       "[CONNECTOR:%d:%s] framebuffer without CRTC\n",
481 			       connector->base.id, connector->name);
482 		return -EINVAL;
483 	}
484 
485 	if (state->crtc)
486 		crtc_state = drm_atomic_get_new_crtc_state(state->state,
487 							   state->crtc);
488 
489 	if (writeback_job->fb && !crtc_state->active) {
490 		drm_dbg_atomic(connector->dev,
491 			       "[CONNECTOR:%d:%s] has framebuffer, but [CRTC:%d] is off\n",
492 			       connector->base.id, connector->name,
493 			       state->crtc->base.id);
494 		return -EINVAL;
495 	}
496 
497 	if (!writeback_job->fb) {
498 		if (writeback_job->out_fence) {
499 			drm_dbg_atomic(connector->dev,
500 				       "[CONNECTOR:%d:%s] requesting out-fence without framebuffer\n",
501 				       connector->base.id, connector->name);
502 			return -EINVAL;
503 		}
504 
505 		drm_writeback_cleanup_job(writeback_job);
506 		state->writeback_job = NULL;
507 	}
508 
509 	return 0;
510 }
511 
512 /**
513  * drm_atomic_get_plane_state - get plane state
514  * @state: global atomic state object
515  * @plane: plane to get state object for
516  *
517  * This function returns the plane state for the given plane, allocating it if
518  * needed. It will also grab the relevant plane lock to make sure that the state
519  * is consistent.
520  *
521  * Returns:
522  * Either the allocated state or the error code encoded into the pointer. When
523  * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
524  * entire atomic sequence must be restarted. All other errors are fatal.
525  */
526 struct drm_plane_state *
527 drm_atomic_get_plane_state(struct drm_atomic_state *state,
528 			  struct drm_plane *plane)
529 {
530 	int ret, index = drm_plane_index(plane);
531 	struct drm_plane_state *plane_state;
532 
533 	WARN_ON(!state->acquire_ctx);
534 	drm_WARN_ON(state->dev, state->checked);
535 
536 	/* the legacy pointers should never be set */
537 	WARN_ON(plane->fb);
538 	WARN_ON(plane->old_fb);
539 	WARN_ON(plane->crtc);
540 
541 	plane_state = drm_atomic_get_new_plane_state(state, plane);
542 	if (plane_state)
543 		return plane_state;
544 
545 	ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
546 	if (ret)
547 		return ERR_PTR(ret);
548 
549 	plane_state = plane->funcs->atomic_duplicate_state(plane);
550 	if (!plane_state)
551 		return ERR_PTR(-ENOMEM);
552 
553 	state->planes[index].state_to_destroy = plane_state;
554 	state->planes[index].ptr = plane;
555 	state->planes[index].old_state = plane->state;
556 	state->planes[index].new_state = plane_state;
557 	plane_state->state = state;
558 
559 	drm_dbg_atomic(plane->dev, "Added [PLANE:%d:%s] %p state to %p\n",
560 		       plane->base.id, plane->name, plane_state, state);
561 
562 	if (plane_state->crtc) {
563 		struct drm_crtc_state *crtc_state;
564 
565 		crtc_state = drm_atomic_get_crtc_state(state,
566 						       plane_state->crtc);
567 		if (IS_ERR(crtc_state))
568 			return ERR_CAST(crtc_state);
569 	}
570 
571 	return plane_state;
572 }
573 EXPORT_SYMBOL(drm_atomic_get_plane_state);
574 
575 static bool
576 plane_switching_crtc(const struct drm_plane_state *old_plane_state,
577 		     const struct drm_plane_state *new_plane_state)
578 {
579 	if (!old_plane_state->crtc || !new_plane_state->crtc)
580 		return false;
581 
582 	if (old_plane_state->crtc == new_plane_state->crtc)
583 		return false;
584 
585 	/* This could be refined, but currently there's no helper or driver code
586 	 * to implement direct switching of active planes nor userspace to take
587 	 * advantage of more direct plane switching without the intermediate
588 	 * full OFF state.
589 	 */
590 	return true;
591 }
592 
593 /**
594  * drm_atomic_plane_check - check plane state
595  * @old_plane_state: old plane state to check
596  * @new_plane_state: new plane state to check
597  *
598  * Provides core sanity checks for plane state.
599  *
600  * RETURNS:
601  * Zero on success, error code on failure
602  */
603 static int drm_atomic_plane_check(const struct drm_plane_state *old_plane_state,
604 				  const struct drm_plane_state *new_plane_state)
605 {
606 	struct drm_plane *plane = new_plane_state->plane;
607 	struct drm_crtc *crtc = new_plane_state->crtc;
608 	const struct drm_framebuffer *fb = new_plane_state->fb;
609 	unsigned int fb_width, fb_height;
610 	struct drm_mode_rect *clips;
611 	uint32_t num_clips;
612 
613 	/* either *both* CRTC and FB must be set, or neither */
614 	if (crtc && !fb) {
615 		drm_dbg_atomic(plane->dev, "[PLANE:%d:%s] CRTC set but no FB\n",
616 			       plane->base.id, plane->name);
617 		return -EINVAL;
618 	} else if (fb && !crtc) {
619 		drm_dbg_atomic(plane->dev, "[PLANE:%d:%s] FB set but no CRTC\n",
620 			       plane->base.id, plane->name);
621 		return -EINVAL;
622 	}
623 
624 	/* if disabled, we don't care about the rest of the state: */
625 	if (!crtc)
626 		return 0;
627 
628 	/* Check whether this plane is usable on this CRTC */
629 	if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
630 		drm_dbg_atomic(plane->dev,
631 			       "Invalid [CRTC:%d:%s] for [PLANE:%d:%s]\n",
632 			       crtc->base.id, crtc->name,
633 			       plane->base.id, plane->name);
634 		return -EINVAL;
635 	}
636 
637 	/* Check whether this plane supports the fb pixel format. */
638 	if (!drm_plane_has_format(plane, fb->format->format, fb->modifier)) {
639 		drm_dbg_atomic(plane->dev,
640 			       "[PLANE:%d:%s] invalid pixel format %p4cc, modifier 0x%llx\n",
641 			       plane->base.id, plane->name,
642 			       &fb->format->format, fb->modifier);
643 		return -EINVAL;
644 	}
645 
646 	/* Give drivers some help against integer overflows */
647 	if (new_plane_state->crtc_w > INT_MAX ||
648 	    new_plane_state->crtc_x > INT_MAX - (int32_t) new_plane_state->crtc_w ||
649 	    new_plane_state->crtc_h > INT_MAX ||
650 	    new_plane_state->crtc_y > INT_MAX - (int32_t) new_plane_state->crtc_h) {
651 		drm_dbg_atomic(plane->dev,
652 			       "[PLANE:%d:%s] invalid CRTC coordinates %ux%u+%d+%d\n",
653 			       plane->base.id, plane->name,
654 			       new_plane_state->crtc_w, new_plane_state->crtc_h,
655 			       new_plane_state->crtc_x, new_plane_state->crtc_y);
656 		return -ERANGE;
657 	}
658 
659 	fb_width = fb->width << 16;
660 	fb_height = fb->height << 16;
661 
662 	/* Make sure source coordinates are inside the fb. */
663 	if (new_plane_state->src_w > fb_width ||
664 	    new_plane_state->src_x > fb_width - new_plane_state->src_w ||
665 	    new_plane_state->src_h > fb_height ||
666 	    new_plane_state->src_y > fb_height - new_plane_state->src_h) {
667 		drm_dbg_atomic(plane->dev,
668 			       "[PLANE:%d:%s] invalid source coordinates "
669 			       "%u.%06ux%u.%06u+%u.%06u+%u.%06u (fb %ux%u)\n",
670 			       plane->base.id, plane->name,
671 			       new_plane_state->src_w >> 16,
672 			       ((new_plane_state->src_w & 0xffff) * 15625) >> 10,
673 			       new_plane_state->src_h >> 16,
674 			       ((new_plane_state->src_h & 0xffff) * 15625) >> 10,
675 			       new_plane_state->src_x >> 16,
676 			       ((new_plane_state->src_x & 0xffff) * 15625) >> 10,
677 			       new_plane_state->src_y >> 16,
678 			       ((new_plane_state->src_y & 0xffff) * 15625) >> 10,
679 			       fb->width, fb->height);
680 		return -ENOSPC;
681 	}
682 
683 	clips = __drm_plane_get_damage_clips(new_plane_state);
684 	num_clips = drm_plane_get_damage_clips_count(new_plane_state);
685 
686 	/* Make sure damage clips are valid and inside the fb. */
687 	while (num_clips > 0) {
688 		if (clips->x1 >= clips->x2 ||
689 		    clips->y1 >= clips->y2 ||
690 		    clips->x1 < 0 ||
691 		    clips->y1 < 0 ||
692 		    clips->x2 > fb_width ||
693 		    clips->y2 > fb_height) {
694 			drm_dbg_atomic(plane->dev,
695 				       "[PLANE:%d:%s] invalid damage clip %d %d %d %d\n",
696 				       plane->base.id, plane->name, clips->x1,
697 				       clips->y1, clips->x2, clips->y2);
698 			return -EINVAL;
699 		}
700 		clips++;
701 		num_clips--;
702 	}
703 
704 	if (plane_switching_crtc(old_plane_state, new_plane_state)) {
705 		drm_dbg_atomic(plane->dev,
706 			       "[PLANE:%d:%s] switching CRTC directly\n",
707 			       plane->base.id, plane->name);
708 		return -EINVAL;
709 	}
710 
711 	return 0;
712 }
713 
714 static void drm_atomic_plane_print_state(struct drm_printer *p,
715 		const struct drm_plane_state *state)
716 {
717 	struct drm_plane *plane = state->plane;
718 	struct drm_rect src  = drm_plane_state_src(state);
719 	struct drm_rect dest = drm_plane_state_dest(state);
720 
721 	drm_printf(p, "plane[%u]: %s\n", plane->base.id, plane->name);
722 	drm_printf(p, "\tcrtc=%s\n", state->crtc ? state->crtc->name : "(null)");
723 	drm_printf(p, "\tfb=%u\n", state->fb ? state->fb->base.id : 0);
724 	if (state->fb)
725 		drm_framebuffer_print_info(p, 2, state->fb);
726 	drm_printf(p, "\tcrtc-pos=" DRM_RECT_FMT "\n", DRM_RECT_ARG(&dest));
727 	drm_printf(p, "\tsrc-pos=" DRM_RECT_FP_FMT "\n", DRM_RECT_FP_ARG(&src));
728 	drm_printf(p, "\trotation=%x\n", state->rotation);
729 	drm_printf(p, "\tnormalized-zpos=%x\n", state->normalized_zpos);
730 	drm_printf(p, "\tcolor-encoding=%s\n",
731 		   drm_get_color_encoding_name(state->color_encoding));
732 	drm_printf(p, "\tcolor-range=%s\n",
733 		   drm_get_color_range_name(state->color_range));
734 	drm_printf(p, "\tcolor_mgmt_changed=%d\n", state->color_mgmt_changed);
735 
736 	if (plane->funcs->atomic_print_state)
737 		plane->funcs->atomic_print_state(p, state);
738 }
739 
740 /**
741  * DOC: handling driver private state
742  *
743  * Very often the DRM objects exposed to userspace in the atomic modeset api
744  * (&drm_connector, &drm_crtc and &drm_plane) do not map neatly to the
745  * underlying hardware. Especially for any kind of shared resources (e.g. shared
746  * clocks, scaler units, bandwidth and fifo limits shared among a group of
747  * planes or CRTCs, and so on) it makes sense to model these as independent
748  * objects. Drivers then need to do similar state tracking and commit ordering for
749  * such private (since not exposed to userspace) objects as the atomic core and
750  * helpers already provide for connectors, planes and CRTCs.
751  *
752  * To make this easier on drivers the atomic core provides some support to track
753  * driver private state objects using struct &drm_private_obj, with the
754  * associated state struct &drm_private_state.
755  *
756  * Similar to userspace-exposed objects, private state structures can be
757  * acquired by calling drm_atomic_get_private_obj_state(). This also takes care
758  * of locking, hence drivers should not have a need to call drm_modeset_lock()
759  * directly. Sequence of the actual hardware state commit is not handled,
760  * drivers might need to keep track of struct drm_crtc_commit within subclassed
761  * structure of &drm_private_state as necessary, e.g. similar to
762  * &drm_plane_state.commit. See also &drm_atomic_state.fake_commit.
763  *
764  * All private state structures contained in a &drm_atomic_state update can be
765  * iterated using for_each_oldnew_private_obj_in_state(),
766  * for_each_new_private_obj_in_state() and for_each_old_private_obj_in_state().
767  * Drivers are recommended to wrap these for each type of driver private state
768  * object they have, filtering on &drm_private_obj.funcs using for_each_if(), at
769  * least if they want to iterate over all objects of a given type.
770  *
771  * An earlier way to handle driver private state was by subclassing struct
772  * &drm_atomic_state. But since that encourages non-standard ways to implement
773  * the check/commit split atomic requires (by using e.g. "check and rollback or
774  * commit instead" of "duplicate state, check, then either commit or release
775  * duplicated state) it is deprecated in favour of using &drm_private_state.
776  */
777 
778 /**
779  * drm_atomic_private_obj_init - initialize private object
780  * @dev: DRM device this object will be attached to
781  * @obj: private object
782  * @state: initial private object state
783  * @funcs: pointer to the struct of function pointers that identify the object
784  * type
785  *
786  * Initialize the private object, which can be embedded into any
787  * driver private object that needs its own atomic state.
788  */
789 void
790 drm_atomic_private_obj_init(struct drm_device *dev,
791 			    struct drm_private_obj *obj,
792 			    struct drm_private_state *state,
793 			    const struct drm_private_state_funcs *funcs)
794 {
795 	memset(obj, 0, sizeof(*obj));
796 
797 	drm_modeset_lock_init(&obj->lock);
798 
799 	obj->state = state;
800 	obj->funcs = funcs;
801 	list_add_tail(&obj->head, &dev->mode_config.privobj_list);
802 
803 	state->obj = obj;
804 }
805 EXPORT_SYMBOL(drm_atomic_private_obj_init);
806 
807 /**
808  * drm_atomic_private_obj_fini - finalize private object
809  * @obj: private object
810  *
811  * Finalize the private object.
812  */
813 void
814 drm_atomic_private_obj_fini(struct drm_private_obj *obj)
815 {
816 	list_del(&obj->head);
817 	obj->funcs->atomic_destroy_state(obj, obj->state);
818 	drm_modeset_lock_fini(&obj->lock);
819 }
820 EXPORT_SYMBOL(drm_atomic_private_obj_fini);
821 
822 /**
823  * drm_atomic_get_private_obj_state - get private object state
824  * @state: global atomic state
825  * @obj: private object to get the state for
826  *
827  * This function returns the private object state for the given private object,
828  * allocating the state if needed. It will also grab the relevant private
829  * object lock to make sure that the state is consistent.
830  *
831  * RETURNS:
832  * Either the allocated state or the error code encoded into a pointer.
833  */
834 struct drm_private_state *
835 drm_atomic_get_private_obj_state(struct drm_atomic_state *state,
836 				 struct drm_private_obj *obj)
837 {
838 	int index, num_objs, ret;
839 	size_t size;
840 	struct __drm_private_objs_state *arr;
841 	struct drm_private_state *obj_state;
842 
843 	WARN_ON(!state->acquire_ctx);
844 	drm_WARN_ON(state->dev, state->checked);
845 
846 	obj_state = drm_atomic_get_new_private_obj_state(state, obj);
847 	if (obj_state)
848 		return obj_state;
849 
850 	ret = drm_modeset_lock(&obj->lock, state->acquire_ctx);
851 	if (ret)
852 		return ERR_PTR(ret);
853 
854 	num_objs = state->num_private_objs + 1;
855 	size = sizeof(*state->private_objs) * num_objs;
856 	arr = krealloc(state->private_objs, size, GFP_KERNEL);
857 	if (!arr)
858 		return ERR_PTR(-ENOMEM);
859 
860 	state->private_objs = arr;
861 	index = state->num_private_objs;
862 	memset(&state->private_objs[index], 0, sizeof(*state->private_objs));
863 
864 	obj_state = obj->funcs->atomic_duplicate_state(obj);
865 	if (!obj_state)
866 		return ERR_PTR(-ENOMEM);
867 
868 	state->private_objs[index].state_to_destroy = obj_state;
869 	state->private_objs[index].old_state = obj->state;
870 	state->private_objs[index].new_state = obj_state;
871 	state->private_objs[index].ptr = obj;
872 	obj_state->state = state;
873 
874 	state->num_private_objs = num_objs;
875 
876 	drm_dbg_atomic(state->dev,
877 		       "Added new private object %p state %p to %p\n",
878 		       obj, obj_state, state);
879 
880 	return obj_state;
881 }
882 EXPORT_SYMBOL(drm_atomic_get_private_obj_state);
883 
884 /**
885  * drm_atomic_get_old_private_obj_state
886  * @state: global atomic state object
887  * @obj: private_obj to grab
888  *
889  * This function returns the old private object state for the given private_obj,
890  * or NULL if the private_obj is not part of the global atomic state.
891  */
892 struct drm_private_state *
893 drm_atomic_get_old_private_obj_state(const struct drm_atomic_state *state,
894 				     struct drm_private_obj *obj)
895 {
896 	int i;
897 
898 	for (i = 0; i < state->num_private_objs; i++)
899 		if (obj == state->private_objs[i].ptr)
900 			return state->private_objs[i].old_state;
901 
902 	return NULL;
903 }
904 EXPORT_SYMBOL(drm_atomic_get_old_private_obj_state);
905 
906 /**
907  * drm_atomic_get_new_private_obj_state
908  * @state: global atomic state object
909  * @obj: private_obj to grab
910  *
911  * This function returns the new private object state for the given private_obj,
912  * or NULL if the private_obj is not part of the global atomic state.
913  */
914 struct drm_private_state *
915 drm_atomic_get_new_private_obj_state(const struct drm_atomic_state *state,
916 				     struct drm_private_obj *obj)
917 {
918 	int i;
919 
920 	for (i = 0; i < state->num_private_objs; i++)
921 		if (obj == state->private_objs[i].ptr)
922 			return state->private_objs[i].new_state;
923 
924 	return NULL;
925 }
926 EXPORT_SYMBOL(drm_atomic_get_new_private_obj_state);
927 
928 /**
929  * drm_atomic_get_old_connector_for_encoder - Get old connector for an encoder
930  * @state: Atomic state
931  * @encoder: The encoder to fetch the connector state for
932  *
933  * This function finds and returns the connector that was connected to @encoder
934  * as specified by the @state.
935  *
936  * If there is no connector in @state which previously had @encoder connected to
937  * it, this function will return NULL. While this may seem like an invalid use
938  * case, it is sometimes useful to differentiate commits which had no prior
939  * connectors attached to @encoder vs ones that did (and to inspect their
940  * state). This is especially true in enable hooks because the pipeline has
941  * changed.
942  *
943  * If you don't have access to the atomic state, see
944  * drm_atomic_get_connector_for_encoder().
945  *
946  * Returns: The old connector connected to @encoder, or NULL if the encoder is
947  * not connected.
948  */
949 struct drm_connector *
950 drm_atomic_get_old_connector_for_encoder(const struct drm_atomic_state *state,
951 					 struct drm_encoder *encoder)
952 {
953 	struct drm_connector_state *conn_state;
954 	struct drm_connector *connector;
955 	unsigned int i;
956 
957 	for_each_old_connector_in_state(state, connector, conn_state, i) {
958 		if (conn_state->best_encoder == encoder)
959 			return connector;
960 	}
961 
962 	return NULL;
963 }
964 EXPORT_SYMBOL(drm_atomic_get_old_connector_for_encoder);
965 
966 /**
967  * drm_atomic_get_new_connector_for_encoder - Get new connector for an encoder
968  * @state: Atomic state
969  * @encoder: The encoder to fetch the connector state for
970  *
971  * This function finds and returns the connector that will be connected to
972  * @encoder as specified by the @state.
973  *
974  * If there is no connector in @state which will have @encoder connected to it,
975  * this function will return NULL. While this may seem like an invalid use case,
976  * it is sometimes useful to differentiate commits which have no connectors
977  * attached to @encoder vs ones that do (and to inspect their state). This is
978  * especially true in disable hooks because the pipeline will change.
979  *
980  * If you don't have access to the atomic state, see
981  * drm_atomic_get_connector_for_encoder().
982  *
983  * Returns: The new connector connected to @encoder, or NULL if the encoder is
984  * not connected.
985  */
986 struct drm_connector *
987 drm_atomic_get_new_connector_for_encoder(const struct drm_atomic_state *state,
988 					 struct drm_encoder *encoder)
989 {
990 	struct drm_connector_state *conn_state;
991 	struct drm_connector *connector;
992 	unsigned int i;
993 
994 	for_each_new_connector_in_state(state, connector, conn_state, i) {
995 		if (conn_state->best_encoder == encoder)
996 			return connector;
997 	}
998 
999 	return NULL;
1000 }
1001 EXPORT_SYMBOL(drm_atomic_get_new_connector_for_encoder);
1002 
1003 /**
1004  * drm_atomic_get_connector_for_encoder - Get connector currently assigned to an encoder
1005  * @encoder: The encoder to find the connector of
1006  * @ctx: Modeset locking context
1007  *
1008  * This function finds and returns the connector currently assigned to
1009  * an @encoder.
1010  *
1011  * It is similar to the drm_atomic_get_old_connector_for_encoder() and
1012  * drm_atomic_get_new_connector_for_encoder() helpers, but doesn't
1013  * require access to the atomic state. If you have access to it, prefer
1014  * using these. This helper is typically useful in situations where you
1015  * don't have access to the atomic state, like detect, link repair,
1016  * threaded interrupt handlers, or hooks from other frameworks (ALSA,
1017  * CEC, etc.).
1018  *
1019  * Returns:
1020  * The connector connected to @encoder, or an error pointer otherwise.
1021  * When the error is EDEADLK, a deadlock has been detected and the
1022  * sequence must be restarted.
1023  */
1024 struct drm_connector *
1025 drm_atomic_get_connector_for_encoder(const struct drm_encoder *encoder,
1026 				     struct drm_modeset_acquire_ctx *ctx)
1027 {
1028 	struct drm_connector_list_iter conn_iter;
1029 	struct drm_connector *out_connector = ERR_PTR(-EINVAL);
1030 	struct drm_connector *connector;
1031 	struct drm_device *dev = encoder->dev;
1032 	int ret;
1033 
1034 	ret = drm_modeset_lock(&dev->mode_config.connection_mutex, ctx);
1035 	if (ret)
1036 		return ERR_PTR(ret);
1037 
1038 	drm_connector_list_iter_begin(dev, &conn_iter);
1039 	drm_for_each_connector_iter(connector, &conn_iter) {
1040 		if (!connector->state)
1041 			continue;
1042 
1043 		if (encoder == connector->state->best_encoder) {
1044 			out_connector = connector;
1045 			break;
1046 		}
1047 	}
1048 	drm_connector_list_iter_end(&conn_iter);
1049 	drm_modeset_unlock(&dev->mode_config.connection_mutex);
1050 
1051 	return out_connector;
1052 }
1053 EXPORT_SYMBOL(drm_atomic_get_connector_for_encoder);
1054 
1055 
1056 /**
1057  * drm_atomic_get_old_crtc_for_encoder - Get old crtc for an encoder
1058  * @state: Atomic state
1059  * @encoder: The encoder to fetch the crtc state for
1060  *
1061  * This function finds and returns the crtc that was connected to @encoder
1062  * as specified by the @state.
1063  *
1064  * Returns: The old crtc connected to @encoder, or NULL if the encoder is
1065  * not connected.
1066  */
1067 struct drm_crtc *
1068 drm_atomic_get_old_crtc_for_encoder(struct drm_atomic_state *state,
1069 				    struct drm_encoder *encoder)
1070 {
1071 	struct drm_connector *connector;
1072 	struct drm_connector_state *conn_state;
1073 
1074 	connector = drm_atomic_get_old_connector_for_encoder(state, encoder);
1075 	if (!connector)
1076 		return NULL;
1077 
1078 	conn_state = drm_atomic_get_old_connector_state(state, connector);
1079 	if (!conn_state)
1080 		return NULL;
1081 
1082 	return conn_state->crtc;
1083 }
1084 EXPORT_SYMBOL(drm_atomic_get_old_crtc_for_encoder);
1085 
1086 /**
1087  * drm_atomic_get_new_crtc_for_encoder - Get new crtc for an encoder
1088  * @state: Atomic state
1089  * @encoder: The encoder to fetch the crtc state for
1090  *
1091  * This function finds and returns the crtc that will be connected to @encoder
1092  * as specified by the @state.
1093  *
1094  * Returns: The new crtc connected to @encoder, or NULL if the encoder is
1095  * not connected.
1096  */
1097 struct drm_crtc *
1098 drm_atomic_get_new_crtc_for_encoder(struct drm_atomic_state *state,
1099 				    struct drm_encoder *encoder)
1100 {
1101 	struct drm_connector *connector;
1102 	struct drm_connector_state *conn_state;
1103 
1104 	connector = drm_atomic_get_new_connector_for_encoder(state, encoder);
1105 	if (!connector)
1106 		return NULL;
1107 
1108 	conn_state = drm_atomic_get_new_connector_state(state, connector);
1109 	if (!conn_state)
1110 		return NULL;
1111 
1112 	return conn_state->crtc;
1113 }
1114 EXPORT_SYMBOL(drm_atomic_get_new_crtc_for_encoder);
1115 
1116 /**
1117  * drm_atomic_get_connector_state - get connector state
1118  * @state: global atomic state object
1119  * @connector: connector to get state object for
1120  *
1121  * This function returns the connector state for the given connector,
1122  * allocating it if needed. It will also grab the relevant connector lock to
1123  * make sure that the state is consistent.
1124  *
1125  * Returns:
1126  * Either the allocated state or the error code encoded into the pointer. When
1127  * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
1128  * entire atomic sequence must be restarted. All other errors are fatal.
1129  */
1130 struct drm_connector_state *
1131 drm_atomic_get_connector_state(struct drm_atomic_state *state,
1132 			  struct drm_connector *connector)
1133 {
1134 	int ret, index;
1135 	struct drm_mode_config *config = &connector->dev->mode_config;
1136 	struct drm_connector_state *connector_state;
1137 
1138 	WARN_ON(!state->acquire_ctx);
1139 	drm_WARN_ON(state->dev, state->checked);
1140 
1141 	ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
1142 	if (ret)
1143 		return ERR_PTR(ret);
1144 
1145 	index = drm_connector_index(connector);
1146 
1147 	if (index >= state->num_connector) {
1148 		struct __drm_connnectors_state *c;
1149 		int alloc = max(index + 1, config->num_connector);
1150 
1151 		c = krealloc_array(state->connectors, alloc,
1152 				   sizeof(*state->connectors), GFP_KERNEL);
1153 		if (!c)
1154 			return ERR_PTR(-ENOMEM);
1155 
1156 		state->connectors = c;
1157 		memset(&state->connectors[state->num_connector], 0,
1158 		       sizeof(*state->connectors) * (alloc - state->num_connector));
1159 
1160 		state->num_connector = alloc;
1161 	}
1162 
1163 	connector_state = drm_atomic_get_new_connector_state(state, connector);
1164 	if (connector_state)
1165 		return connector_state;
1166 
1167 	connector_state = connector->funcs->atomic_duplicate_state(connector);
1168 	if (!connector_state)
1169 		return ERR_PTR(-ENOMEM);
1170 
1171 	drm_connector_get(connector);
1172 	state->connectors[index].state_to_destroy = connector_state;
1173 	state->connectors[index].old_state = connector->state;
1174 	state->connectors[index].new_state = connector_state;
1175 	state->connectors[index].ptr = connector;
1176 	connector_state->state = state;
1177 
1178 	drm_dbg_atomic(connector->dev, "Added [CONNECTOR:%d:%s] %p state to %p\n",
1179 			 connector->base.id, connector->name,
1180 			 connector_state, state);
1181 
1182 	if (connector_state->crtc) {
1183 		struct drm_crtc_state *crtc_state;
1184 
1185 		crtc_state = drm_atomic_get_crtc_state(state,
1186 						       connector_state->crtc);
1187 		if (IS_ERR(crtc_state))
1188 			return ERR_CAST(crtc_state);
1189 	}
1190 
1191 	return connector_state;
1192 }
1193 EXPORT_SYMBOL(drm_atomic_get_connector_state);
1194 
1195 static void drm_atomic_connector_print_state(struct drm_printer *p,
1196 		const struct drm_connector_state *state)
1197 {
1198 	struct drm_connector *connector = state->connector;
1199 
1200 	drm_printf(p, "connector[%u]: %s\n", connector->base.id, connector->name);
1201 	drm_printf(p, "\tcrtc=%s\n", state->crtc ? state->crtc->name : "(null)");
1202 	drm_printf(p, "\tself_refresh_aware=%d\n", state->self_refresh_aware);
1203 	drm_printf(p, "\tinterlace_allowed=%d\n", connector->interlace_allowed);
1204 	drm_printf(p, "\tycbcr_420_allowed=%d\n", connector->ycbcr_420_allowed);
1205 	drm_printf(p, "\tmax_requested_bpc=%d\n", state->max_requested_bpc);
1206 	drm_printf(p, "\tcolorspace=%s\n", drm_get_colorspace_name(state->colorspace));
1207 
1208 	if (connector->connector_type == DRM_MODE_CONNECTOR_HDMIA ||
1209 	    connector->connector_type == DRM_MODE_CONNECTOR_HDMIB) {
1210 		drm_printf(p, "\tbroadcast_rgb=%s\n",
1211 			   drm_hdmi_connector_get_broadcast_rgb_name(state->hdmi.broadcast_rgb));
1212 		drm_printf(p, "\tis_limited_range=%c\n", state->hdmi.is_limited_range ? 'y' : 'n');
1213 		drm_printf(p, "\toutput_bpc=%u\n", state->hdmi.output_bpc);
1214 		drm_printf(p, "\toutput_format=%s\n",
1215 			   drm_hdmi_connector_get_output_format_name(state->hdmi.output_format));
1216 		drm_printf(p, "\ttmds_char_rate=%llu\n", state->hdmi.tmds_char_rate);
1217 	}
1218 
1219 	if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
1220 		if (state->writeback_job && state->writeback_job->fb)
1221 			drm_printf(p, "\tfb=%d\n", state->writeback_job->fb->base.id);
1222 
1223 	if (connector->funcs->atomic_print_state)
1224 		connector->funcs->atomic_print_state(p, state);
1225 }
1226 
1227 /**
1228  * drm_atomic_get_bridge_state - get bridge state
1229  * @state: global atomic state object
1230  * @bridge: bridge to get state object for
1231  *
1232  * This function returns the bridge state for the given bridge, allocating it
1233  * if needed. It will also grab the relevant bridge lock to make sure that the
1234  * state is consistent.
1235  *
1236  * Returns:
1237  * Either the allocated state or the error code encoded into the pointer. When
1238  * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
1239  * entire atomic sequence must be restarted.
1240  */
1241 struct drm_bridge_state *
1242 drm_atomic_get_bridge_state(struct drm_atomic_state *state,
1243 			    struct drm_bridge *bridge)
1244 {
1245 	struct drm_private_state *obj_state;
1246 
1247 	obj_state = drm_atomic_get_private_obj_state(state, &bridge->base);
1248 	if (IS_ERR(obj_state))
1249 		return ERR_CAST(obj_state);
1250 
1251 	return drm_priv_to_bridge_state(obj_state);
1252 }
1253 EXPORT_SYMBOL(drm_atomic_get_bridge_state);
1254 
1255 /**
1256  * drm_atomic_get_old_bridge_state - get old bridge state, if it exists
1257  * @state: global atomic state object
1258  * @bridge: bridge to grab
1259  *
1260  * This function returns the old bridge state for the given bridge, or NULL if
1261  * the bridge is not part of the global atomic state.
1262  */
1263 struct drm_bridge_state *
1264 drm_atomic_get_old_bridge_state(const struct drm_atomic_state *state,
1265 				struct drm_bridge *bridge)
1266 {
1267 	struct drm_private_state *obj_state;
1268 
1269 	obj_state = drm_atomic_get_old_private_obj_state(state, &bridge->base);
1270 	if (!obj_state)
1271 		return NULL;
1272 
1273 	return drm_priv_to_bridge_state(obj_state);
1274 }
1275 EXPORT_SYMBOL(drm_atomic_get_old_bridge_state);
1276 
1277 /**
1278  * drm_atomic_get_new_bridge_state - get new bridge state, if it exists
1279  * @state: global atomic state object
1280  * @bridge: bridge to grab
1281  *
1282  * This function returns the new bridge state for the given bridge, or NULL if
1283  * the bridge is not part of the global atomic state.
1284  */
1285 struct drm_bridge_state *
1286 drm_atomic_get_new_bridge_state(const struct drm_atomic_state *state,
1287 				struct drm_bridge *bridge)
1288 {
1289 	struct drm_private_state *obj_state;
1290 
1291 	obj_state = drm_atomic_get_new_private_obj_state(state, &bridge->base);
1292 	if (!obj_state)
1293 		return NULL;
1294 
1295 	return drm_priv_to_bridge_state(obj_state);
1296 }
1297 EXPORT_SYMBOL(drm_atomic_get_new_bridge_state);
1298 
1299 /**
1300  * drm_atomic_add_encoder_bridges - add bridges attached to an encoder
1301  * @state: atomic state
1302  * @encoder: DRM encoder
1303  *
1304  * This function adds all bridges attached to @encoder. This is needed to add
1305  * bridge states to @state and make them available when
1306  * &drm_bridge_funcs.atomic_check(), &drm_bridge_funcs.atomic_pre_enable(),
1307  * &drm_bridge_funcs.atomic_enable(),
1308  * &drm_bridge_funcs.atomic_disable_post_disable() are called.
1309  *
1310  * Returns:
1311  * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1312  * then the w/w mutex code has detected a deadlock and the entire atomic
1313  * sequence must be restarted. All other errors are fatal.
1314  */
1315 int
1316 drm_atomic_add_encoder_bridges(struct drm_atomic_state *state,
1317 			       struct drm_encoder *encoder)
1318 {
1319 	struct drm_bridge_state *bridge_state;
1320 
1321 	if (!encoder)
1322 		return 0;
1323 
1324 	drm_dbg_atomic(encoder->dev,
1325 		       "Adding all bridges for [encoder:%d:%s] to %p\n",
1326 		       encoder->base.id, encoder->name, state);
1327 
1328 	drm_for_each_bridge_in_chain_scoped(encoder, bridge) {
1329 		/* Skip bridges that don't implement the atomic state hooks. */
1330 		if (!bridge->funcs->atomic_duplicate_state)
1331 			continue;
1332 
1333 		bridge_state = drm_atomic_get_bridge_state(state, bridge);
1334 		if (IS_ERR(bridge_state))
1335 			return PTR_ERR(bridge_state);
1336 	}
1337 
1338 	return 0;
1339 }
1340 EXPORT_SYMBOL(drm_atomic_add_encoder_bridges);
1341 
1342 /**
1343  * drm_atomic_add_affected_connectors - add connectors for CRTC
1344  * @state: atomic state
1345  * @crtc: DRM CRTC
1346  *
1347  * This function walks the current configuration and adds all connectors
1348  * currently using @crtc to the atomic configuration @state. Note that this
1349  * function must acquire the connection mutex. This can potentially cause
1350  * unneeded serialization if the update is just for the planes on one CRTC. Hence
1351  * drivers and helpers should only call this when really needed (e.g. when a
1352  * full modeset needs to happen due to some change).
1353  *
1354  * Returns:
1355  * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1356  * then the w/w mutex code has detected a deadlock and the entire atomic
1357  * sequence must be restarted. All other errors are fatal.
1358  */
1359 int
1360 drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
1361 				   struct drm_crtc *crtc)
1362 {
1363 	struct drm_mode_config *config = &state->dev->mode_config;
1364 	struct drm_connector *connector;
1365 	struct drm_connector_state *conn_state;
1366 	struct drm_connector_list_iter conn_iter;
1367 	struct drm_crtc_state *crtc_state;
1368 	int ret;
1369 
1370 	crtc_state = drm_atomic_get_crtc_state(state, crtc);
1371 	if (IS_ERR(crtc_state))
1372 		return PTR_ERR(crtc_state);
1373 
1374 	ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
1375 	if (ret)
1376 		return ret;
1377 
1378 	drm_dbg_atomic(crtc->dev,
1379 		       "Adding all current connectors for [CRTC:%d:%s] to %p\n",
1380 		       crtc->base.id, crtc->name, state);
1381 
1382 	/*
1383 	 * Changed connectors are already in @state, so only need to look
1384 	 * at the connector_mask in crtc_state.
1385 	 */
1386 	drm_connector_list_iter_begin(state->dev, &conn_iter);
1387 	drm_for_each_connector_iter(connector, &conn_iter) {
1388 		if (!(crtc_state->connector_mask & drm_connector_mask(connector)))
1389 			continue;
1390 
1391 		conn_state = drm_atomic_get_connector_state(state, connector);
1392 		if (IS_ERR(conn_state)) {
1393 			drm_connector_list_iter_end(&conn_iter);
1394 			return PTR_ERR(conn_state);
1395 		}
1396 	}
1397 	drm_connector_list_iter_end(&conn_iter);
1398 
1399 	return 0;
1400 }
1401 EXPORT_SYMBOL(drm_atomic_add_affected_connectors);
1402 
1403 /**
1404  * drm_atomic_add_affected_planes - add planes for CRTC
1405  * @state: atomic state
1406  * @crtc: DRM CRTC
1407  *
1408  * This function walks the current configuration and adds all planes
1409  * currently used by @crtc to the atomic configuration @state. This is useful
1410  * when an atomic commit also needs to check all currently enabled plane on
1411  * @crtc, e.g. when changing the mode. It's also useful when re-enabling a CRTC
1412  * to avoid special code to force-enable all planes.
1413  *
1414  * Since acquiring a plane state will always also acquire the w/w mutex of the
1415  * current CRTC for that plane (if there is any) adding all the plane states for
1416  * a CRTC will not reduce parallelism of atomic updates.
1417  *
1418  * Returns:
1419  * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1420  * then the w/w mutex code has detected a deadlock and the entire atomic
1421  * sequence must be restarted. All other errors are fatal.
1422  */
1423 int
1424 drm_atomic_add_affected_planes(struct drm_atomic_state *state,
1425 			       struct drm_crtc *crtc)
1426 {
1427 	const struct drm_crtc_state *old_crtc_state =
1428 		drm_atomic_get_old_crtc_state(state, crtc);
1429 	struct drm_plane *plane;
1430 
1431 	WARN_ON(!drm_atomic_get_new_crtc_state(state, crtc));
1432 
1433 	drm_dbg_atomic(crtc->dev,
1434 		       "Adding all current planes for [CRTC:%d:%s] to %p\n",
1435 		       crtc->base.id, crtc->name, state);
1436 
1437 	drm_for_each_plane_mask(plane, state->dev, old_crtc_state->plane_mask) {
1438 		struct drm_plane_state *plane_state =
1439 			drm_atomic_get_plane_state(state, plane);
1440 
1441 		if (IS_ERR(plane_state))
1442 			return PTR_ERR(plane_state);
1443 	}
1444 	return 0;
1445 }
1446 EXPORT_SYMBOL(drm_atomic_add_affected_planes);
1447 
1448 /**
1449  * drm_atomic_check_only - check whether a given config would work
1450  * @state: atomic configuration to check
1451  *
1452  * Note that this function can return -EDEADLK if the driver needed to acquire
1453  * more locks but encountered a deadlock. The caller must then do the usual w/w
1454  * backoff dance and restart. All other errors are fatal.
1455  *
1456  * Returns:
1457  * 0 on success, negative error code on failure.
1458  */
1459 int drm_atomic_check_only(struct drm_atomic_state *state)
1460 {
1461 	struct drm_device *dev = state->dev;
1462 	struct drm_mode_config *config = &dev->mode_config;
1463 	struct drm_plane *plane;
1464 	struct drm_plane_state *old_plane_state;
1465 	struct drm_plane_state *new_plane_state;
1466 	struct drm_crtc *crtc;
1467 	struct drm_crtc_state *old_crtc_state;
1468 	struct drm_crtc_state *new_crtc_state;
1469 	struct drm_connector *conn;
1470 	struct drm_connector_state *conn_state;
1471 	unsigned int requested_crtc = 0;
1472 	unsigned int affected_crtc = 0;
1473 	int i, ret = 0;
1474 
1475 	drm_dbg_atomic(dev, "checking %p\n", state);
1476 
1477 	for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
1478 		if (new_crtc_state->enable)
1479 			requested_crtc |= drm_crtc_mask(crtc);
1480 	}
1481 
1482 	for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
1483 		ret = drm_atomic_plane_check(old_plane_state, new_plane_state);
1484 		if (ret) {
1485 			drm_dbg_atomic(dev, "[PLANE:%d:%s] atomic core check failed\n",
1486 				       plane->base.id, plane->name);
1487 			return ret;
1488 		}
1489 	}
1490 
1491 	for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
1492 		ret = drm_atomic_crtc_check(old_crtc_state, new_crtc_state);
1493 		if (ret) {
1494 			drm_dbg_atomic(dev, "[CRTC:%d:%s] atomic core check failed\n",
1495 				       crtc->base.id, crtc->name);
1496 			return ret;
1497 		}
1498 	}
1499 
1500 	for_each_new_connector_in_state(state, conn, conn_state, i) {
1501 		ret = drm_atomic_connector_check(conn, conn_state);
1502 		if (ret) {
1503 			drm_dbg_atomic(dev, "[CONNECTOR:%d:%s] atomic core check failed\n",
1504 				       conn->base.id, conn->name);
1505 			return ret;
1506 		}
1507 	}
1508 
1509 	if (config->funcs->atomic_check) {
1510 		ret = config->funcs->atomic_check(state->dev, state);
1511 
1512 		if (ret) {
1513 			drm_dbg_atomic(dev, "atomic driver check for %p failed: %d\n",
1514 				       state, ret);
1515 			return ret;
1516 		}
1517 	}
1518 
1519 	if (!state->allow_modeset) {
1520 		for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
1521 			if (drm_atomic_crtc_needs_modeset(new_crtc_state)) {
1522 				drm_dbg_atomic(dev, "[CRTC:%d:%s] requires full modeset\n",
1523 					       crtc->base.id, crtc->name);
1524 				return -EINVAL;
1525 			}
1526 		}
1527 	}
1528 
1529 	for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
1530 		if (new_crtc_state->enable)
1531 			affected_crtc |= drm_crtc_mask(crtc);
1532 	}
1533 
1534 	/*
1535 	 * For commits that allow modesets drivers can add other CRTCs to the
1536 	 * atomic commit, e.g. when they need to reallocate global resources.
1537 	 * This can cause spurious EBUSY, which robs compositors of a very
1538 	 * effective sanity check for their drawing loop. Therefor only allow
1539 	 * drivers to add unrelated CRTC states for modeset commits.
1540 	 *
1541 	 * FIXME: Should add affected_crtc mask to the ATOMIC IOCTL as an output
1542 	 * so compositors know what's going on.
1543 	 */
1544 	if (affected_crtc != requested_crtc) {
1545 		drm_dbg_atomic(dev,
1546 			       "driver added CRTC to commit: requested 0x%x, affected 0x%0x\n",
1547 			       requested_crtc, affected_crtc);
1548 		WARN(!state->allow_modeset, "adding CRTC not allowed without modesets: requested 0x%x, affected 0x%0x\n",
1549 		     requested_crtc, affected_crtc);
1550 	}
1551 
1552 	state->checked = true;
1553 
1554 	return 0;
1555 }
1556 EXPORT_SYMBOL(drm_atomic_check_only);
1557 
1558 /**
1559  * drm_atomic_commit - commit configuration atomically
1560  * @state: atomic configuration to check
1561  *
1562  * Note that this function can return -EDEADLK if the driver needed to acquire
1563  * more locks but encountered a deadlock. The caller must then do the usual w/w
1564  * backoff dance and restart. All other errors are fatal.
1565  *
1566  * This function will take its own reference on @state.
1567  * Callers should always release their reference with drm_atomic_state_put().
1568  *
1569  * Returns:
1570  * 0 on success, negative error code on failure.
1571  */
1572 int drm_atomic_commit(struct drm_atomic_state *state)
1573 {
1574 	struct drm_mode_config *config = &state->dev->mode_config;
1575 	struct drm_printer p = drm_info_printer(state->dev->dev);
1576 	int ret;
1577 
1578 	if (drm_debug_enabled(DRM_UT_STATE))
1579 		drm_atomic_print_new_state(state, &p);
1580 
1581 	ret = drm_atomic_check_only(state);
1582 	if (ret)
1583 		return ret;
1584 
1585 	drm_dbg_atomic(state->dev, "committing %p\n", state);
1586 
1587 	return config->funcs->atomic_commit(state->dev, state, false);
1588 }
1589 EXPORT_SYMBOL(drm_atomic_commit);
1590 
1591 /**
1592  * drm_atomic_nonblocking_commit - atomic nonblocking commit
1593  * @state: atomic configuration to check
1594  *
1595  * Note that this function can return -EDEADLK if the driver needed to acquire
1596  * more locks but encountered a deadlock. The caller must then do the usual w/w
1597  * backoff dance and restart. All other errors are fatal.
1598  *
1599  * This function will take its own reference on @state.
1600  * Callers should always release their reference with drm_atomic_state_put().
1601  *
1602  * Returns:
1603  * 0 on success, negative error code on failure.
1604  */
1605 int drm_atomic_nonblocking_commit(struct drm_atomic_state *state)
1606 {
1607 	struct drm_mode_config *config = &state->dev->mode_config;
1608 	int ret;
1609 
1610 	ret = drm_atomic_check_only(state);
1611 	if (ret)
1612 		return ret;
1613 
1614 	drm_dbg_atomic(state->dev, "committing %p nonblocking\n", state);
1615 
1616 	return config->funcs->atomic_commit(state->dev, state, true);
1617 }
1618 EXPORT_SYMBOL(drm_atomic_nonblocking_commit);
1619 
1620 /* just used from drm-client and atomic-helper: */
1621 int __drm_atomic_helper_disable_plane(struct drm_plane *plane,
1622 				      struct drm_plane_state *plane_state)
1623 {
1624 	int ret;
1625 
1626 	ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
1627 	if (ret != 0)
1628 		return ret;
1629 
1630 	drm_atomic_set_fb_for_plane(plane_state, NULL);
1631 	plane_state->crtc_x = 0;
1632 	plane_state->crtc_y = 0;
1633 	plane_state->crtc_w = 0;
1634 	plane_state->crtc_h = 0;
1635 	plane_state->src_x = 0;
1636 	plane_state->src_y = 0;
1637 	plane_state->src_w = 0;
1638 	plane_state->src_h = 0;
1639 
1640 	return 0;
1641 }
1642 EXPORT_SYMBOL(__drm_atomic_helper_disable_plane);
1643 
1644 static int update_output_state(struct drm_atomic_state *state,
1645 			       struct drm_mode_set *set)
1646 {
1647 	struct drm_device *dev = set->crtc->dev;
1648 	struct drm_crtc *crtc;
1649 	struct drm_crtc_state *new_crtc_state;
1650 	struct drm_connector *connector;
1651 	struct drm_connector_state *new_conn_state;
1652 	int ret, i;
1653 
1654 	ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
1655 			       state->acquire_ctx);
1656 	if (ret)
1657 		return ret;
1658 
1659 	/* First disable all connectors on the target crtc. */
1660 	ret = drm_atomic_add_affected_connectors(state, set->crtc);
1661 	if (ret)
1662 		return ret;
1663 
1664 	for_each_new_connector_in_state(state, connector, new_conn_state, i) {
1665 		if (new_conn_state->crtc == set->crtc) {
1666 			ret = drm_atomic_set_crtc_for_connector(new_conn_state,
1667 								NULL);
1668 			if (ret)
1669 				return ret;
1670 
1671 			/* Make sure legacy setCrtc always re-trains */
1672 			new_conn_state->link_status = DRM_LINK_STATUS_GOOD;
1673 		}
1674 	}
1675 
1676 	/* Then set all connectors from set->connectors on the target crtc */
1677 	for (i = 0; i < set->num_connectors; i++) {
1678 		new_conn_state = drm_atomic_get_connector_state(state,
1679 								set->connectors[i]);
1680 		if (IS_ERR(new_conn_state))
1681 			return PTR_ERR(new_conn_state);
1682 
1683 		ret = drm_atomic_set_crtc_for_connector(new_conn_state,
1684 							set->crtc);
1685 		if (ret)
1686 			return ret;
1687 	}
1688 
1689 	for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
1690 		/*
1691 		 * Don't update ->enable for the CRTC in the set_config request,
1692 		 * since a mismatch would indicate a bug in the upper layers.
1693 		 * The actual modeset code later on will catch any
1694 		 * inconsistencies here.
1695 		 */
1696 		if (crtc == set->crtc)
1697 			continue;
1698 
1699 		if (!new_crtc_state->connector_mask) {
1700 			ret = drm_atomic_set_mode_prop_for_crtc(new_crtc_state,
1701 								NULL);
1702 			if (ret < 0)
1703 				return ret;
1704 
1705 			new_crtc_state->active = false;
1706 		}
1707 	}
1708 
1709 	return 0;
1710 }
1711 
1712 /* just used from drm-client and atomic-helper: */
1713 int __drm_atomic_helper_set_config(struct drm_mode_set *set,
1714 				   struct drm_atomic_state *state)
1715 {
1716 	struct drm_crtc_state *crtc_state;
1717 	struct drm_plane_state *primary_state;
1718 	struct drm_crtc *crtc = set->crtc;
1719 	int hdisplay, vdisplay;
1720 	int ret;
1721 
1722 	crtc_state = drm_atomic_get_crtc_state(state, crtc);
1723 	if (IS_ERR(crtc_state))
1724 		return PTR_ERR(crtc_state);
1725 
1726 	primary_state = drm_atomic_get_plane_state(state, crtc->primary);
1727 	if (IS_ERR(primary_state))
1728 		return PTR_ERR(primary_state);
1729 
1730 	if (!set->mode) {
1731 		WARN_ON(set->fb);
1732 		WARN_ON(set->num_connectors);
1733 
1734 		ret = drm_atomic_set_mode_for_crtc(crtc_state, NULL);
1735 		if (ret != 0)
1736 			return ret;
1737 
1738 		crtc_state->active = false;
1739 
1740 		ret = drm_atomic_set_crtc_for_plane(primary_state, NULL);
1741 		if (ret != 0)
1742 			return ret;
1743 
1744 		drm_atomic_set_fb_for_plane(primary_state, NULL);
1745 
1746 		goto commit;
1747 	}
1748 
1749 	WARN_ON(!set->fb);
1750 	WARN_ON(!set->num_connectors);
1751 
1752 	ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
1753 	if (ret != 0)
1754 		return ret;
1755 
1756 	crtc_state->active = true;
1757 
1758 	ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
1759 	if (ret != 0)
1760 		return ret;
1761 
1762 	drm_mode_get_hv_timing(set->mode, &hdisplay, &vdisplay);
1763 
1764 	drm_atomic_set_fb_for_plane(primary_state, set->fb);
1765 	primary_state->crtc_x = 0;
1766 	primary_state->crtc_y = 0;
1767 	primary_state->crtc_w = hdisplay;
1768 	primary_state->crtc_h = vdisplay;
1769 	primary_state->src_x = set->x << 16;
1770 	primary_state->src_y = set->y << 16;
1771 	if (drm_rotation_90_or_270(primary_state->rotation)) {
1772 		primary_state->src_w = vdisplay << 16;
1773 		primary_state->src_h = hdisplay << 16;
1774 	} else {
1775 		primary_state->src_w = hdisplay << 16;
1776 		primary_state->src_h = vdisplay << 16;
1777 	}
1778 
1779 commit:
1780 	ret = update_output_state(state, set);
1781 	if (ret)
1782 		return ret;
1783 
1784 	return 0;
1785 }
1786 EXPORT_SYMBOL(__drm_atomic_helper_set_config);
1787 
1788 static void drm_atomic_private_obj_print_state(struct drm_printer *p,
1789 					       const struct drm_private_state *state)
1790 {
1791 	struct drm_private_obj *obj = state->obj;
1792 
1793 	if (obj->funcs->atomic_print_state)
1794 		obj->funcs->atomic_print_state(p, state);
1795 }
1796 
1797 /**
1798  * drm_atomic_print_new_state - prints drm atomic state
1799  * @state: atomic configuration to check
1800  * @p: drm printer
1801  *
1802  * This functions prints the drm atomic state snapshot using the drm printer
1803  * which is passed to it. This snapshot can be used for debugging purposes.
1804  *
1805  * Note that this function looks into the new state objects and hence its not
1806  * safe to be used after the call to drm_atomic_helper_commit_hw_done().
1807  */
1808 void drm_atomic_print_new_state(const struct drm_atomic_state *state,
1809 		struct drm_printer *p)
1810 {
1811 	struct drm_plane *plane;
1812 	struct drm_plane_state *plane_state;
1813 	struct drm_crtc *crtc;
1814 	struct drm_crtc_state *crtc_state;
1815 	struct drm_connector *connector;
1816 	struct drm_connector_state *connector_state;
1817 	struct drm_private_obj *obj;
1818 	struct drm_private_state *obj_state;
1819 	int i;
1820 
1821 	if (!p) {
1822 		drm_err(state->dev, "invalid drm printer\n");
1823 		return;
1824 	}
1825 
1826 	drm_dbg_atomic(state->dev, "checking %p\n", state);
1827 
1828 	for_each_new_plane_in_state(state, plane, plane_state, i)
1829 		drm_atomic_plane_print_state(p, plane_state);
1830 
1831 	for_each_new_crtc_in_state(state, crtc, crtc_state, i)
1832 		drm_atomic_crtc_print_state(p, crtc_state);
1833 
1834 	for_each_new_connector_in_state(state, connector, connector_state, i)
1835 		drm_atomic_connector_print_state(p, connector_state);
1836 
1837 	for_each_new_private_obj_in_state(state, obj, obj_state, i)
1838 		drm_atomic_private_obj_print_state(p, obj_state);
1839 }
1840 EXPORT_SYMBOL(drm_atomic_print_new_state);
1841 
1842 static void __drm_state_dump(struct drm_device *dev, struct drm_printer *p,
1843 			     bool take_locks)
1844 {
1845 	struct drm_mode_config *config = &dev->mode_config;
1846 	struct drm_plane *plane;
1847 	struct drm_crtc *crtc;
1848 	struct drm_connector *connector;
1849 	struct drm_connector_list_iter conn_iter;
1850 	struct drm_private_obj *obj;
1851 
1852 	if (!drm_drv_uses_atomic_modeset(dev))
1853 		return;
1854 
1855 	list_for_each_entry(plane, &config->plane_list, head) {
1856 		if (take_locks)
1857 			drm_modeset_lock(&plane->mutex, NULL);
1858 		drm_atomic_plane_print_state(p, plane->state);
1859 		if (take_locks)
1860 			drm_modeset_unlock(&plane->mutex);
1861 	}
1862 
1863 	list_for_each_entry(crtc, &config->crtc_list, head) {
1864 		if (take_locks)
1865 			drm_modeset_lock(&crtc->mutex, NULL);
1866 		drm_atomic_crtc_print_state(p, crtc->state);
1867 		if (take_locks)
1868 			drm_modeset_unlock(&crtc->mutex);
1869 	}
1870 
1871 	drm_connector_list_iter_begin(dev, &conn_iter);
1872 	if (take_locks)
1873 		drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
1874 	drm_for_each_connector_iter(connector, &conn_iter)
1875 		drm_atomic_connector_print_state(p, connector->state);
1876 	if (take_locks)
1877 		drm_modeset_unlock(&dev->mode_config.connection_mutex);
1878 	drm_connector_list_iter_end(&conn_iter);
1879 
1880 	list_for_each_entry(obj, &config->privobj_list, head) {
1881 		if (take_locks)
1882 			drm_modeset_lock(&obj->lock, NULL);
1883 		drm_atomic_private_obj_print_state(p, obj->state);
1884 		if (take_locks)
1885 			drm_modeset_unlock(&obj->lock);
1886 	}
1887 }
1888 
1889 /**
1890  * drm_state_dump - dump entire device atomic state
1891  * @dev: the drm device
1892  * @p: where to print the state to
1893  *
1894  * Just for debugging.  Drivers might want an option to dump state
1895  * to dmesg in case of error irq's.  (Hint, you probably want to
1896  * ratelimit this!)
1897  *
1898  * The caller must wrap this drm_modeset_lock_all_ctx() and
1899  * drm_modeset_drop_locks(). If this is called from error irq handler, it should
1900  * not be enabled by default - if you are debugging errors you might
1901  * not care that this is racey, but calling this without all modeset locks held
1902  * is inherently unsafe.
1903  */
1904 void drm_state_dump(struct drm_device *dev, struct drm_printer *p)
1905 {
1906 	__drm_state_dump(dev, p, false);
1907 }
1908 EXPORT_SYMBOL(drm_state_dump);
1909 
1910 #ifdef CONFIG_DEBUG_FS
1911 static int drm_state_info(struct seq_file *m, void *data)
1912 {
1913 	struct drm_debugfs_entry *entry = m->private;
1914 	struct drm_device *dev = entry->dev;
1915 	struct drm_printer p = drm_seq_file_printer(m);
1916 
1917 	__drm_state_dump(dev, &p, true);
1918 
1919 	return 0;
1920 }
1921 
1922 /* any use in debugfs files to dump individual planes/crtc/etc? */
1923 static const struct drm_debugfs_info drm_atomic_debugfs_list[] = {
1924 	{"state", drm_state_info, 0},
1925 };
1926 
1927 void drm_atomic_debugfs_init(struct drm_device *dev)
1928 {
1929 	drm_debugfs_add_files(dev, drm_atomic_debugfs_list,
1930 			      ARRAY_SIZE(drm_atomic_debugfs_list));
1931 }
1932 #endif
1933