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