xref: /linux/drivers/gpu/drm/drm_atomic.c (revision e05b08d7d0162cf77fff119367fb1a2d5ab3e669)
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 /**
645  * drm_atomic_get_old_colorop_state - get colorop state, if it exists
646  * @state: global atomic state object
647  * @colorop: colorop to grab
648  *
649  * This function returns the old colorop state for the given colorop, or
650  * NULL if the colorop is not part of the global atomic state.
651  */
652 struct drm_colorop_state *
653 drm_atomic_get_old_colorop_state(struct drm_atomic_state *state,
654 				 struct drm_colorop *colorop)
655 {
656 	return state->colorops[drm_colorop_index(colorop)].old_state;
657 }
658 EXPORT_SYMBOL(drm_atomic_get_old_colorop_state);
659 
660 /**
661  * drm_atomic_get_new_colorop_state - get colorop state, if it exists
662  * @state: global atomic state object
663  * @colorop: colorop to grab
664  *
665  * This function returns the new colorop state for the given colorop, or
666  * NULL if the colorop is not part of the global atomic state.
667  */
668 struct drm_colorop_state *
669 drm_atomic_get_new_colorop_state(struct drm_atomic_state *state,
670 				 struct drm_colorop *colorop)
671 {
672 	return state->colorops[drm_colorop_index(colorop)].new_state;
673 }
674 EXPORT_SYMBOL(drm_atomic_get_new_colorop_state);
675 
676 static bool
677 plane_switching_crtc(const struct drm_plane_state *old_plane_state,
678 		     const struct drm_plane_state *new_plane_state)
679 {
680 	if (!old_plane_state->crtc || !new_plane_state->crtc)
681 		return false;
682 
683 	if (old_plane_state->crtc == new_plane_state->crtc)
684 		return false;
685 
686 	/* This could be refined, but currently there's no helper or driver code
687 	 * to implement direct switching of active planes nor userspace to take
688 	 * advantage of more direct plane switching without the intermediate
689 	 * full OFF state.
690 	 */
691 	return true;
692 }
693 
694 /**
695  * drm_atomic_plane_check - check plane state
696  * @old_plane_state: old plane state to check
697  * @new_plane_state: new plane state to check
698  *
699  * Provides core sanity checks for plane state.
700  *
701  * RETURNS:
702  * Zero on success, error code on failure
703  */
704 static int drm_atomic_plane_check(const struct drm_plane_state *old_plane_state,
705 				  const struct drm_plane_state *new_plane_state)
706 {
707 	struct drm_plane *plane = new_plane_state->plane;
708 	struct drm_crtc *crtc = new_plane_state->crtc;
709 	const struct drm_framebuffer *fb = new_plane_state->fb;
710 	unsigned int fb_width, fb_height;
711 	struct drm_mode_rect *clips;
712 	uint32_t num_clips;
713 
714 	/* either *both* CRTC and FB must be set, or neither */
715 	if (crtc && !fb) {
716 		drm_dbg_atomic(plane->dev, "[PLANE:%d:%s] CRTC set but no FB\n",
717 			       plane->base.id, plane->name);
718 		return -EINVAL;
719 	} else if (fb && !crtc) {
720 		drm_dbg_atomic(plane->dev, "[PLANE:%d:%s] FB set but no CRTC\n",
721 			       plane->base.id, plane->name);
722 		return -EINVAL;
723 	}
724 
725 	/* if disabled, we don't care about the rest of the state: */
726 	if (!crtc)
727 		return 0;
728 
729 	/* Check whether this plane is usable on this CRTC */
730 	if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
731 		drm_dbg_atomic(plane->dev,
732 			       "Invalid [CRTC:%d:%s] for [PLANE:%d:%s]\n",
733 			       crtc->base.id, crtc->name,
734 			       plane->base.id, plane->name);
735 		return -EINVAL;
736 	}
737 
738 	/* Check whether this plane supports the fb pixel format. */
739 	if (!drm_plane_has_format(plane, fb->format->format, fb->modifier)) {
740 		drm_dbg_atomic(plane->dev,
741 			       "[PLANE:%d:%s] invalid pixel format %p4cc, modifier 0x%llx\n",
742 			       plane->base.id, plane->name,
743 			       &fb->format->format, fb->modifier);
744 		return -EINVAL;
745 	}
746 
747 	/* Give drivers some help against integer overflows */
748 	if (new_plane_state->crtc_w > INT_MAX ||
749 	    new_plane_state->crtc_x > INT_MAX - (int32_t) new_plane_state->crtc_w ||
750 	    new_plane_state->crtc_h > INT_MAX ||
751 	    new_plane_state->crtc_y > INT_MAX - (int32_t) new_plane_state->crtc_h) {
752 		drm_dbg_atomic(plane->dev,
753 			       "[PLANE:%d:%s] invalid CRTC coordinates %ux%u+%d+%d\n",
754 			       plane->base.id, plane->name,
755 			       new_plane_state->crtc_w, new_plane_state->crtc_h,
756 			       new_plane_state->crtc_x, new_plane_state->crtc_y);
757 		return -ERANGE;
758 	}
759 
760 	fb_width = fb->width << 16;
761 	fb_height = fb->height << 16;
762 
763 	/* Make sure source coordinates are inside the fb. */
764 	if (new_plane_state->src_w > fb_width ||
765 	    new_plane_state->src_x > fb_width - new_plane_state->src_w ||
766 	    new_plane_state->src_h > fb_height ||
767 	    new_plane_state->src_y > fb_height - new_plane_state->src_h) {
768 		drm_dbg_atomic(plane->dev,
769 			       "[PLANE:%d:%s] invalid source coordinates "
770 			       "%u.%06ux%u.%06u+%u.%06u+%u.%06u (fb %ux%u)\n",
771 			       plane->base.id, plane->name,
772 			       new_plane_state->src_w >> 16,
773 			       ((new_plane_state->src_w & 0xffff) * 15625) >> 10,
774 			       new_plane_state->src_h >> 16,
775 			       ((new_plane_state->src_h & 0xffff) * 15625) >> 10,
776 			       new_plane_state->src_x >> 16,
777 			       ((new_plane_state->src_x & 0xffff) * 15625) >> 10,
778 			       new_plane_state->src_y >> 16,
779 			       ((new_plane_state->src_y & 0xffff) * 15625) >> 10,
780 			       fb->width, fb->height);
781 		return -ENOSPC;
782 	}
783 
784 	clips = __drm_plane_get_damage_clips(new_plane_state);
785 	num_clips = drm_plane_get_damage_clips_count(new_plane_state);
786 
787 	/* Make sure damage clips are valid and inside the fb. */
788 	while (num_clips > 0) {
789 		if (clips->x1 >= clips->x2 ||
790 		    clips->y1 >= clips->y2 ||
791 		    clips->x1 < 0 ||
792 		    clips->y1 < 0 ||
793 		    clips->x2 > fb_width ||
794 		    clips->y2 > fb_height) {
795 			drm_dbg_atomic(plane->dev,
796 				       "[PLANE:%d:%s] invalid damage clip %d %d %d %d\n",
797 				       plane->base.id, plane->name, clips->x1,
798 				       clips->y1, clips->x2, clips->y2);
799 			return -EINVAL;
800 		}
801 		clips++;
802 		num_clips--;
803 	}
804 
805 	if (plane_switching_crtc(old_plane_state, new_plane_state)) {
806 		drm_dbg_atomic(plane->dev,
807 			       "[PLANE:%d:%s] switching CRTC directly\n",
808 			       plane->base.id, plane->name);
809 		return -EINVAL;
810 	}
811 
812 	return 0;
813 }
814 
815 static void drm_atomic_colorop_print_state(struct drm_printer *p,
816 					   const struct drm_colorop_state *state)
817 {
818 	struct drm_colorop *colorop = state->colorop;
819 
820 	drm_printf(p, "colorop[%u]:\n", colorop->base.id);
821 	drm_printf(p, "\ttype=%s\n", drm_get_colorop_type_name(colorop->type));
822 	if (colorop->bypass_property)
823 		drm_printf(p, "\tbypass=%u\n", state->bypass);
824 
825 	switch (colorop->type) {
826 	case DRM_COLOROP_1D_CURVE:
827 		drm_printf(p, "\tcurve_1d_type=%s\n",
828 			   drm_get_colorop_curve_1d_type_name(state->curve_1d_type));
829 		break;
830 	case DRM_COLOROP_1D_LUT:
831 		drm_printf(p, "\tsize=%d\n", colorop->size);
832 		drm_printf(p, "\tinterpolation=%s\n",
833 			   drm_get_colorop_lut1d_interpolation_name(colorop->lut1d_interpolation));
834 		drm_printf(p, "\tdata blob id=%d\n", state->data ? state->data->base.id : 0);
835 		break;
836 	case DRM_COLOROP_CTM_3X4:
837 		drm_printf(p, "\tdata blob id=%d\n", state->data ? state->data->base.id : 0);
838 		break;
839 	case DRM_COLOROP_MULTIPLIER:
840 		drm_printf(p, "\tmultiplier=%llu\n", state->multiplier);
841 		break;
842 	case DRM_COLOROP_3D_LUT:
843 		drm_printf(p, "\tsize=%d\n", colorop->size);
844 		drm_printf(p, "\tinterpolation=%s\n",
845 			   drm_get_colorop_lut3d_interpolation_name(colorop->lut3d_interpolation));
846 		drm_printf(p, "\tdata blob id=%d\n", state->data ? state->data->base.id : 0);
847 		break;
848 	default:
849 		break;
850 	}
851 
852 	drm_printf(p, "\tnext=%d\n", colorop->next ? colorop->next->base.id : 0);
853 }
854 
855 static void drm_atomic_plane_print_state(struct drm_printer *p,
856 		const struct drm_plane_state *state)
857 {
858 	struct drm_plane *plane = state->plane;
859 	struct drm_rect src  = drm_plane_state_src(state);
860 	struct drm_rect dest = drm_plane_state_dest(state);
861 
862 	drm_printf(p, "plane[%u]: %s\n", plane->base.id, plane->name);
863 	drm_printf(p, "\tcrtc=%s\n", state->crtc ? state->crtc->name : "(null)");
864 	drm_printf(p, "\tfb=%u\n", state->fb ? state->fb->base.id : 0);
865 	if (state->fb)
866 		drm_framebuffer_print_info(p, 2, state->fb);
867 	drm_printf(p, "\tcrtc-pos=" DRM_RECT_FMT "\n", DRM_RECT_ARG(&dest));
868 	drm_printf(p, "\tsrc-pos=" DRM_RECT_FP_FMT "\n", DRM_RECT_FP_ARG(&src));
869 	drm_printf(p, "\trotation=%x\n", state->rotation);
870 	drm_printf(p, "\tnormalized-zpos=%x\n", state->normalized_zpos);
871 	drm_printf(p, "\tcolor-encoding=%s\n",
872 		   drm_get_color_encoding_name(state->color_encoding));
873 	drm_printf(p, "\tcolor-range=%s\n",
874 		   drm_get_color_range_name(state->color_range));
875 	drm_printf(p, "\tcolor_mgmt_changed=%d\n", state->color_mgmt_changed);
876 	drm_printf(p, "\tcolor-pipeline=%d\n",
877 		   state->color_pipeline ? state->color_pipeline->base.id : 0);
878 	if (plane->funcs->atomic_print_state)
879 		plane->funcs->atomic_print_state(p, state);
880 }
881 
882 /**
883  * DOC: handling driver private state
884  *
885  * Very often the DRM objects exposed to userspace in the atomic modeset api
886  * (&drm_connector, &drm_crtc and &drm_plane) do not map neatly to the
887  * underlying hardware. Especially for any kind of shared resources (e.g. shared
888  * clocks, scaler units, bandwidth and fifo limits shared among a group of
889  * planes or CRTCs, and so on) it makes sense to model these as independent
890  * objects. Drivers then need to do similar state tracking and commit ordering for
891  * such private (since not exposed to userspace) objects as the atomic core and
892  * helpers already provide for connectors, planes and CRTCs.
893  *
894  * To make this easier on drivers the atomic core provides some support to track
895  * driver private state objects using struct &drm_private_obj, with the
896  * associated state struct &drm_private_state.
897  *
898  * Similar to userspace-exposed objects, private state structures can be
899  * acquired by calling drm_atomic_get_private_obj_state(). This also takes care
900  * of locking, hence drivers should not have a need to call drm_modeset_lock()
901  * directly. Sequence of the actual hardware state commit is not handled,
902  * drivers might need to keep track of struct drm_crtc_commit within subclassed
903  * structure of &drm_private_state as necessary, e.g. similar to
904  * &drm_plane_state.commit. See also &drm_atomic_state.fake_commit.
905  *
906  * All private state structures contained in a &drm_atomic_state update can be
907  * iterated using for_each_oldnew_private_obj_in_state(),
908  * for_each_new_private_obj_in_state() and for_each_old_private_obj_in_state().
909  * Drivers are recommended to wrap these for each type of driver private state
910  * object they have, filtering on &drm_private_obj.funcs using for_each_if(), at
911  * least if they want to iterate over all objects of a given type.
912  *
913  * An earlier way to handle driver private state was by subclassing struct
914  * &drm_atomic_state. But since that encourages non-standard ways to implement
915  * the check/commit split atomic requires (by using e.g. "check and rollback or
916  * commit instead" of "duplicate state, check, then either commit or release
917  * duplicated state) it is deprecated in favour of using &drm_private_state.
918  */
919 
920 /**
921  * drm_atomic_private_obj_init - initialize private object
922  * @dev: DRM device this object will be attached to
923  * @obj: private object
924  * @state: initial private object state
925  * @funcs: pointer to the struct of function pointers that identify the object
926  * type
927  *
928  * Initialize the private object, which can be embedded into any
929  * driver private object that needs its own atomic state.
930  */
931 void
932 drm_atomic_private_obj_init(struct drm_device *dev,
933 			    struct drm_private_obj *obj,
934 			    struct drm_private_state *state,
935 			    const struct drm_private_state_funcs *funcs)
936 {
937 	memset(obj, 0, sizeof(*obj));
938 
939 	drm_modeset_lock_init(&obj->lock);
940 
941 	obj->dev = dev;
942 	obj->state = state;
943 	obj->funcs = funcs;
944 	list_add_tail(&obj->head, &dev->mode_config.privobj_list);
945 
946 	state->obj = obj;
947 }
948 EXPORT_SYMBOL(drm_atomic_private_obj_init);
949 
950 /**
951  * drm_atomic_private_obj_fini - finalize private object
952  * @obj: private object
953  *
954  * Finalize the private object.
955  */
956 void
957 drm_atomic_private_obj_fini(struct drm_private_obj *obj)
958 {
959 	list_del(&obj->head);
960 	obj->funcs->atomic_destroy_state(obj, obj->state);
961 	drm_modeset_lock_fini(&obj->lock);
962 }
963 EXPORT_SYMBOL(drm_atomic_private_obj_fini);
964 
965 /**
966  * drm_atomic_get_private_obj_state - get private object state
967  * @state: global atomic state
968  * @obj: private object to get the state for
969  *
970  * This function returns the private object state for the given private object,
971  * allocating the state if needed. It will also grab the relevant private
972  * object lock to make sure that the state is consistent.
973  *
974  * RETURNS:
975  * Either the allocated state or the error code encoded into a pointer.
976  */
977 struct drm_private_state *
978 drm_atomic_get_private_obj_state(struct drm_atomic_state *state,
979 				 struct drm_private_obj *obj)
980 {
981 	int index, num_objs, ret;
982 	size_t size;
983 	struct __drm_private_objs_state *arr;
984 	struct drm_private_state *obj_state;
985 
986 	WARN_ON(!state->acquire_ctx);
987 	drm_WARN_ON(state->dev, state->checked);
988 
989 	obj_state = drm_atomic_get_new_private_obj_state(state, obj);
990 	if (obj_state)
991 		return obj_state;
992 
993 	ret = drm_modeset_lock(&obj->lock, state->acquire_ctx);
994 	if (ret)
995 		return ERR_PTR(ret);
996 
997 	num_objs = state->num_private_objs + 1;
998 	size = sizeof(*state->private_objs) * num_objs;
999 	arr = krealloc(state->private_objs, size, GFP_KERNEL);
1000 	if (!arr)
1001 		return ERR_PTR(-ENOMEM);
1002 
1003 	state->private_objs = arr;
1004 	index = state->num_private_objs;
1005 	memset(&state->private_objs[index], 0, sizeof(*state->private_objs));
1006 
1007 	obj_state = obj->funcs->atomic_duplicate_state(obj);
1008 	if (!obj_state)
1009 		return ERR_PTR(-ENOMEM);
1010 
1011 	state->private_objs[index].state_to_destroy = obj_state;
1012 	state->private_objs[index].old_state = obj->state;
1013 	state->private_objs[index].new_state = obj_state;
1014 	state->private_objs[index].ptr = obj;
1015 	obj_state->state = state;
1016 
1017 	state->num_private_objs = num_objs;
1018 
1019 	drm_dbg_atomic(state->dev,
1020 		       "Added new private object %p state %p to %p\n",
1021 		       obj, obj_state, state);
1022 
1023 	return obj_state;
1024 }
1025 EXPORT_SYMBOL(drm_atomic_get_private_obj_state);
1026 
1027 /**
1028  * drm_atomic_get_old_private_obj_state
1029  * @state: global atomic state object
1030  * @obj: private_obj to grab
1031  *
1032  * This function returns the old private object state for the given private_obj,
1033  * or NULL if the private_obj is not part of the global atomic state.
1034  */
1035 struct drm_private_state *
1036 drm_atomic_get_old_private_obj_state(const struct drm_atomic_state *state,
1037 				     struct drm_private_obj *obj)
1038 {
1039 	int i;
1040 
1041 	for (i = 0; i < state->num_private_objs; i++)
1042 		if (obj == state->private_objs[i].ptr)
1043 			return state->private_objs[i].old_state;
1044 
1045 	return NULL;
1046 }
1047 EXPORT_SYMBOL(drm_atomic_get_old_private_obj_state);
1048 
1049 /**
1050  * drm_atomic_get_new_private_obj_state
1051  * @state: global atomic state object
1052  * @obj: private_obj to grab
1053  *
1054  * This function returns the new private object state for the given private_obj,
1055  * or NULL if the private_obj is not part of the global atomic state.
1056  */
1057 struct drm_private_state *
1058 drm_atomic_get_new_private_obj_state(const struct drm_atomic_state *state,
1059 				     struct drm_private_obj *obj)
1060 {
1061 	int i;
1062 
1063 	for (i = 0; i < state->num_private_objs; i++)
1064 		if (obj == state->private_objs[i].ptr)
1065 			return state->private_objs[i].new_state;
1066 
1067 	return NULL;
1068 }
1069 EXPORT_SYMBOL(drm_atomic_get_new_private_obj_state);
1070 
1071 /**
1072  * drm_atomic_get_old_connector_for_encoder - Get old connector for an encoder
1073  * @state: Atomic state
1074  * @encoder: The encoder to fetch the connector state for
1075  *
1076  * This function finds and returns the connector that was connected to @encoder
1077  * as specified by the @state.
1078  *
1079  * If there is no connector in @state which previously had @encoder connected to
1080  * it, this function will return NULL. While this may seem like an invalid use
1081  * case, it is sometimes useful to differentiate commits which had no prior
1082  * connectors attached to @encoder vs ones that did (and to inspect their
1083  * state). This is especially true in enable hooks because the pipeline has
1084  * changed.
1085  *
1086  * If you don't have access to the atomic state, see
1087  * drm_atomic_get_connector_for_encoder().
1088  *
1089  * Returns: The old connector connected to @encoder, or NULL if the encoder is
1090  * not connected.
1091  */
1092 struct drm_connector *
1093 drm_atomic_get_old_connector_for_encoder(const struct drm_atomic_state *state,
1094 					 struct drm_encoder *encoder)
1095 {
1096 	struct drm_connector_state *conn_state;
1097 	struct drm_connector *connector;
1098 	unsigned int i;
1099 
1100 	for_each_old_connector_in_state(state, connector, conn_state, i) {
1101 		if (conn_state->best_encoder == encoder)
1102 			return connector;
1103 	}
1104 
1105 	return NULL;
1106 }
1107 EXPORT_SYMBOL(drm_atomic_get_old_connector_for_encoder);
1108 
1109 /**
1110  * drm_atomic_get_new_connector_for_encoder - Get new connector for an encoder
1111  * @state: Atomic state
1112  * @encoder: The encoder to fetch the connector state for
1113  *
1114  * This function finds and returns the connector that will be connected to
1115  * @encoder as specified by the @state.
1116  *
1117  * If there is no connector in @state which will have @encoder connected to it,
1118  * this function will return NULL. While this may seem like an invalid use case,
1119  * it is sometimes useful to differentiate commits which have no connectors
1120  * attached to @encoder vs ones that do (and to inspect their state). This is
1121  * especially true in disable hooks because the pipeline will change.
1122  *
1123  * If you don't have access to the atomic state, see
1124  * drm_atomic_get_connector_for_encoder().
1125  *
1126  * Returns: The new connector connected to @encoder, or NULL if the encoder is
1127  * not connected.
1128  */
1129 struct drm_connector *
1130 drm_atomic_get_new_connector_for_encoder(const struct drm_atomic_state *state,
1131 					 struct drm_encoder *encoder)
1132 {
1133 	struct drm_connector_state *conn_state;
1134 	struct drm_connector *connector;
1135 	unsigned int i;
1136 
1137 	for_each_new_connector_in_state(state, connector, conn_state, i) {
1138 		if (conn_state->best_encoder == encoder)
1139 			return connector;
1140 	}
1141 
1142 	return NULL;
1143 }
1144 EXPORT_SYMBOL(drm_atomic_get_new_connector_for_encoder);
1145 
1146 /**
1147  * drm_atomic_get_connector_for_encoder - Get connector currently assigned to an encoder
1148  * @encoder: The encoder to find the connector of
1149  * @ctx: Modeset locking context
1150  *
1151  * This function finds and returns the connector currently assigned to
1152  * an @encoder.
1153  *
1154  * It is similar to the drm_atomic_get_old_connector_for_encoder() and
1155  * drm_atomic_get_new_connector_for_encoder() helpers, but doesn't
1156  * require access to the atomic state. If you have access to it, prefer
1157  * using these. This helper is typically useful in situations where you
1158  * don't have access to the atomic state, like detect, link repair,
1159  * threaded interrupt handlers, or hooks from other frameworks (ALSA,
1160  * CEC, etc.).
1161  *
1162  * Returns:
1163  * The connector connected to @encoder, or an error pointer otherwise.
1164  * When the error is EDEADLK, a deadlock has been detected and the
1165  * sequence must be restarted.
1166  */
1167 struct drm_connector *
1168 drm_atomic_get_connector_for_encoder(const struct drm_encoder *encoder,
1169 				     struct drm_modeset_acquire_ctx *ctx)
1170 {
1171 	struct drm_connector_list_iter conn_iter;
1172 	struct drm_connector *out_connector = ERR_PTR(-EINVAL);
1173 	struct drm_connector *connector;
1174 	struct drm_device *dev = encoder->dev;
1175 	int ret;
1176 
1177 	ret = drm_modeset_lock(&dev->mode_config.connection_mutex, ctx);
1178 	if (ret)
1179 		return ERR_PTR(ret);
1180 
1181 	drm_connector_list_iter_begin(dev, &conn_iter);
1182 	drm_for_each_connector_iter(connector, &conn_iter) {
1183 		if (!connector->state)
1184 			continue;
1185 
1186 		if (encoder == connector->state->best_encoder) {
1187 			out_connector = connector;
1188 			break;
1189 		}
1190 	}
1191 	drm_connector_list_iter_end(&conn_iter);
1192 	drm_modeset_unlock(&dev->mode_config.connection_mutex);
1193 
1194 	return out_connector;
1195 }
1196 EXPORT_SYMBOL(drm_atomic_get_connector_for_encoder);
1197 
1198 
1199 /**
1200  * drm_atomic_get_old_crtc_for_encoder - Get old crtc for an encoder
1201  * @state: Atomic state
1202  * @encoder: The encoder to fetch the crtc state for
1203  *
1204  * This function finds and returns the crtc that was connected to @encoder
1205  * as specified by the @state.
1206  *
1207  * Returns: The old crtc connected to @encoder, or NULL if the encoder is
1208  * not connected.
1209  */
1210 struct drm_crtc *
1211 drm_atomic_get_old_crtc_for_encoder(struct drm_atomic_state *state,
1212 				    struct drm_encoder *encoder)
1213 {
1214 	struct drm_connector *connector;
1215 	struct drm_connector_state *conn_state;
1216 
1217 	connector = drm_atomic_get_old_connector_for_encoder(state, encoder);
1218 	if (!connector)
1219 		return NULL;
1220 
1221 	conn_state = drm_atomic_get_old_connector_state(state, connector);
1222 	if (!conn_state)
1223 		return NULL;
1224 
1225 	return conn_state->crtc;
1226 }
1227 EXPORT_SYMBOL(drm_atomic_get_old_crtc_for_encoder);
1228 
1229 /**
1230  * drm_atomic_get_new_crtc_for_encoder - Get new crtc for an encoder
1231  * @state: Atomic state
1232  * @encoder: The encoder to fetch the crtc state for
1233  *
1234  * This function finds and returns the crtc that will be connected to @encoder
1235  * as specified by the @state.
1236  *
1237  * Returns: The new crtc connected to @encoder, or NULL if the encoder is
1238  * not connected.
1239  */
1240 struct drm_crtc *
1241 drm_atomic_get_new_crtc_for_encoder(struct drm_atomic_state *state,
1242 				    struct drm_encoder *encoder)
1243 {
1244 	struct drm_connector *connector;
1245 	struct drm_connector_state *conn_state;
1246 
1247 	connector = drm_atomic_get_new_connector_for_encoder(state, encoder);
1248 	if (!connector)
1249 		return NULL;
1250 
1251 	conn_state = drm_atomic_get_new_connector_state(state, connector);
1252 	if (!conn_state)
1253 		return NULL;
1254 
1255 	return conn_state->crtc;
1256 }
1257 EXPORT_SYMBOL(drm_atomic_get_new_crtc_for_encoder);
1258 
1259 /**
1260  * drm_atomic_get_connector_state - get connector state
1261  * @state: global atomic state object
1262  * @connector: connector to get state object for
1263  *
1264  * This function returns the connector state for the given connector,
1265  * allocating it if needed. It will also grab the relevant connector lock to
1266  * make sure that the state is consistent.
1267  *
1268  * Returns:
1269  * Either the allocated state or the error code encoded into the pointer. When
1270  * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
1271  * entire atomic sequence must be restarted. All other errors are fatal.
1272  */
1273 struct drm_connector_state *
1274 drm_atomic_get_connector_state(struct drm_atomic_state *state,
1275 			  struct drm_connector *connector)
1276 {
1277 	int ret, index;
1278 	struct drm_mode_config *config = &connector->dev->mode_config;
1279 	struct drm_connector_state *connector_state;
1280 
1281 	WARN_ON(!state->acquire_ctx);
1282 	drm_WARN_ON(state->dev, state->checked);
1283 
1284 	ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
1285 	if (ret)
1286 		return ERR_PTR(ret);
1287 
1288 	index = drm_connector_index(connector);
1289 
1290 	if (index >= state->num_connector) {
1291 		struct __drm_connnectors_state *c;
1292 		int alloc = max(index + 1, config->num_connector);
1293 
1294 		c = krealloc_array(state->connectors, alloc,
1295 				   sizeof(*state->connectors), GFP_KERNEL);
1296 		if (!c)
1297 			return ERR_PTR(-ENOMEM);
1298 
1299 		state->connectors = c;
1300 		memset(&state->connectors[state->num_connector], 0,
1301 		       sizeof(*state->connectors) * (alloc - state->num_connector));
1302 
1303 		state->num_connector = alloc;
1304 	}
1305 
1306 	connector_state = drm_atomic_get_new_connector_state(state, connector);
1307 	if (connector_state)
1308 		return connector_state;
1309 
1310 	connector_state = connector->funcs->atomic_duplicate_state(connector);
1311 	if (!connector_state)
1312 		return ERR_PTR(-ENOMEM);
1313 
1314 	drm_connector_get(connector);
1315 	state->connectors[index].state_to_destroy = connector_state;
1316 	state->connectors[index].old_state = connector->state;
1317 	state->connectors[index].new_state = connector_state;
1318 	state->connectors[index].ptr = connector;
1319 	connector_state->state = state;
1320 
1321 	drm_dbg_atomic(connector->dev, "Added [CONNECTOR:%d:%s] %p state to %p\n",
1322 			 connector->base.id, connector->name,
1323 			 connector_state, state);
1324 
1325 	if (connector_state->crtc) {
1326 		struct drm_crtc_state *crtc_state;
1327 
1328 		crtc_state = drm_atomic_get_crtc_state(state,
1329 						       connector_state->crtc);
1330 		if (IS_ERR(crtc_state))
1331 			return ERR_CAST(crtc_state);
1332 	}
1333 
1334 	return connector_state;
1335 }
1336 EXPORT_SYMBOL(drm_atomic_get_connector_state);
1337 
1338 static void drm_atomic_connector_print_state(struct drm_printer *p,
1339 		const struct drm_connector_state *state)
1340 {
1341 	struct drm_connector *connector = state->connector;
1342 
1343 	drm_printf(p, "connector[%u]: %s\n", connector->base.id, connector->name);
1344 	drm_printf(p, "\tcrtc=%s\n", state->crtc ? state->crtc->name : "(null)");
1345 	drm_printf(p, "\tself_refresh_aware=%d\n", state->self_refresh_aware);
1346 	drm_printf(p, "\tinterlace_allowed=%d\n", connector->interlace_allowed);
1347 	drm_printf(p, "\tycbcr_420_allowed=%d\n", connector->ycbcr_420_allowed);
1348 	drm_printf(p, "\tmax_requested_bpc=%d\n", state->max_requested_bpc);
1349 	drm_printf(p, "\tcolorspace=%s\n", drm_get_colorspace_name(state->colorspace));
1350 
1351 	if (connector->connector_type == DRM_MODE_CONNECTOR_HDMIA ||
1352 	    connector->connector_type == DRM_MODE_CONNECTOR_HDMIB) {
1353 		drm_printf(p, "\tbroadcast_rgb=%s\n",
1354 			   drm_hdmi_connector_get_broadcast_rgb_name(state->hdmi.broadcast_rgb));
1355 		drm_printf(p, "\tis_limited_range=%c\n", state->hdmi.is_limited_range ? 'y' : 'n');
1356 		drm_printf(p, "\toutput_bpc=%u\n", state->hdmi.output_bpc);
1357 		drm_printf(p, "\toutput_format=%s\n",
1358 			   drm_hdmi_connector_get_output_format_name(state->hdmi.output_format));
1359 		drm_printf(p, "\ttmds_char_rate=%llu\n", state->hdmi.tmds_char_rate);
1360 	}
1361 
1362 	if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
1363 		if (state->writeback_job && state->writeback_job->fb)
1364 			drm_printf(p, "\tfb=%d\n", state->writeback_job->fb->base.id);
1365 
1366 	if (connector->funcs->atomic_print_state)
1367 		connector->funcs->atomic_print_state(p, state);
1368 }
1369 
1370 /**
1371  * drm_atomic_get_bridge_state - get bridge state
1372  * @state: global atomic state object
1373  * @bridge: bridge to get state object for
1374  *
1375  * This function returns the bridge state for the given bridge, allocating it
1376  * if needed. It will also grab the relevant bridge lock to make sure that the
1377  * state is consistent.
1378  *
1379  * Returns:
1380  * Either the allocated state or the error code encoded into the pointer. When
1381  * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
1382  * entire atomic sequence must be restarted.
1383  */
1384 struct drm_bridge_state *
1385 drm_atomic_get_bridge_state(struct drm_atomic_state *state,
1386 			    struct drm_bridge *bridge)
1387 {
1388 	struct drm_private_state *obj_state;
1389 
1390 	obj_state = drm_atomic_get_private_obj_state(state, &bridge->base);
1391 	if (IS_ERR(obj_state))
1392 		return ERR_CAST(obj_state);
1393 
1394 	return drm_priv_to_bridge_state(obj_state);
1395 }
1396 EXPORT_SYMBOL(drm_atomic_get_bridge_state);
1397 
1398 /**
1399  * drm_atomic_get_old_bridge_state - get old bridge state, if it exists
1400  * @state: global atomic state object
1401  * @bridge: bridge to grab
1402  *
1403  * This function returns the old bridge state for the given bridge, or NULL if
1404  * the bridge is not part of the global atomic state.
1405  */
1406 struct drm_bridge_state *
1407 drm_atomic_get_old_bridge_state(const struct drm_atomic_state *state,
1408 				struct drm_bridge *bridge)
1409 {
1410 	struct drm_private_state *obj_state;
1411 
1412 	obj_state = drm_atomic_get_old_private_obj_state(state, &bridge->base);
1413 	if (!obj_state)
1414 		return NULL;
1415 
1416 	return drm_priv_to_bridge_state(obj_state);
1417 }
1418 EXPORT_SYMBOL(drm_atomic_get_old_bridge_state);
1419 
1420 /**
1421  * drm_atomic_get_new_bridge_state - get new bridge state, if it exists
1422  * @state: global atomic state object
1423  * @bridge: bridge to grab
1424  *
1425  * This function returns the new bridge state for the given bridge, or NULL if
1426  * the bridge is not part of the global atomic state.
1427  */
1428 struct drm_bridge_state *
1429 drm_atomic_get_new_bridge_state(const struct drm_atomic_state *state,
1430 				struct drm_bridge *bridge)
1431 {
1432 	struct drm_private_state *obj_state;
1433 
1434 	obj_state = drm_atomic_get_new_private_obj_state(state, &bridge->base);
1435 	if (!obj_state)
1436 		return NULL;
1437 
1438 	return drm_priv_to_bridge_state(obj_state);
1439 }
1440 EXPORT_SYMBOL(drm_atomic_get_new_bridge_state);
1441 
1442 /**
1443  * drm_atomic_add_encoder_bridges - add bridges attached to an encoder
1444  * @state: atomic state
1445  * @encoder: DRM encoder
1446  *
1447  * This function adds all bridges attached to @encoder. This is needed to add
1448  * bridge states to @state and make them available when
1449  * &drm_bridge_funcs.atomic_check(), &drm_bridge_funcs.atomic_pre_enable(),
1450  * &drm_bridge_funcs.atomic_enable(),
1451  * &drm_bridge_funcs.atomic_disable_post_disable() are called.
1452  *
1453  * Returns:
1454  * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1455  * then the w/w mutex code has detected a deadlock and the entire atomic
1456  * sequence must be restarted. All other errors are fatal.
1457  */
1458 int
1459 drm_atomic_add_encoder_bridges(struct drm_atomic_state *state,
1460 			       struct drm_encoder *encoder)
1461 {
1462 	struct drm_bridge_state *bridge_state;
1463 
1464 	if (!encoder)
1465 		return 0;
1466 
1467 	drm_dbg_atomic(encoder->dev,
1468 		       "Adding all bridges for [encoder:%d:%s] to %p\n",
1469 		       encoder->base.id, encoder->name, state);
1470 
1471 	drm_for_each_bridge_in_chain_scoped(encoder, bridge) {
1472 		/* Skip bridges that don't implement the atomic state hooks. */
1473 		if (!bridge->funcs->atomic_duplicate_state)
1474 			continue;
1475 
1476 		bridge_state = drm_atomic_get_bridge_state(state, bridge);
1477 		if (IS_ERR(bridge_state))
1478 			return PTR_ERR(bridge_state);
1479 	}
1480 
1481 	return 0;
1482 }
1483 EXPORT_SYMBOL(drm_atomic_add_encoder_bridges);
1484 
1485 /**
1486  * drm_atomic_add_affected_connectors - add connectors for CRTC
1487  * @state: atomic state
1488  * @crtc: DRM CRTC
1489  *
1490  * This function walks the current configuration and adds all connectors
1491  * currently using @crtc to the atomic configuration @state. Note that this
1492  * function must acquire the connection mutex. This can potentially cause
1493  * unneeded serialization if the update is just for the planes on one CRTC. Hence
1494  * drivers and helpers should only call this when really needed (e.g. when a
1495  * full modeset needs to happen due to some change).
1496  *
1497  * Returns:
1498  * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1499  * then the w/w mutex code has detected a deadlock and the entire atomic
1500  * sequence must be restarted. All other errors are fatal.
1501  */
1502 int
1503 drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
1504 				   struct drm_crtc *crtc)
1505 {
1506 	struct drm_mode_config *config = &state->dev->mode_config;
1507 	struct drm_connector *connector;
1508 	struct drm_connector_state *conn_state;
1509 	struct drm_connector_list_iter conn_iter;
1510 	struct drm_crtc_state *crtc_state;
1511 	int ret;
1512 
1513 	crtc_state = drm_atomic_get_crtc_state(state, crtc);
1514 	if (IS_ERR(crtc_state))
1515 		return PTR_ERR(crtc_state);
1516 
1517 	ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
1518 	if (ret)
1519 		return ret;
1520 
1521 	drm_dbg_atomic(crtc->dev,
1522 		       "Adding all current connectors for [CRTC:%d:%s] to %p\n",
1523 		       crtc->base.id, crtc->name, state);
1524 
1525 	/*
1526 	 * Changed connectors are already in @state, so only need to look
1527 	 * at the connector_mask in crtc_state.
1528 	 */
1529 	drm_connector_list_iter_begin(state->dev, &conn_iter);
1530 	drm_for_each_connector_iter(connector, &conn_iter) {
1531 		if (!(crtc_state->connector_mask & drm_connector_mask(connector)))
1532 			continue;
1533 
1534 		conn_state = drm_atomic_get_connector_state(state, connector);
1535 		if (IS_ERR(conn_state)) {
1536 			drm_connector_list_iter_end(&conn_iter);
1537 			return PTR_ERR(conn_state);
1538 		}
1539 	}
1540 	drm_connector_list_iter_end(&conn_iter);
1541 
1542 	return 0;
1543 }
1544 EXPORT_SYMBOL(drm_atomic_add_affected_connectors);
1545 
1546 /**
1547  * drm_atomic_add_affected_planes - add planes for CRTC
1548  * @state: atomic state
1549  * @crtc: DRM CRTC
1550  *
1551  * This function walks the current configuration and adds all planes
1552  * currently used by @crtc to the atomic configuration @state. This is useful
1553  * when an atomic commit also needs to check all currently enabled plane on
1554  * @crtc, e.g. when changing the mode. It's also useful when re-enabling a CRTC
1555  * to avoid special code to force-enable all planes.
1556  *
1557  * Since acquiring a plane state will always also acquire the w/w mutex of the
1558  * current CRTC for that plane (if there is any) adding all the plane states for
1559  * a CRTC will not reduce parallelism of atomic updates.
1560  *
1561  * Returns:
1562  * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1563  * then the w/w mutex code has detected a deadlock and the entire atomic
1564  * sequence must be restarted. All other errors are fatal.
1565  */
1566 int
1567 drm_atomic_add_affected_planes(struct drm_atomic_state *state,
1568 			       struct drm_crtc *crtc)
1569 {
1570 	const struct drm_crtc_state *old_crtc_state =
1571 		drm_atomic_get_old_crtc_state(state, crtc);
1572 	struct drm_plane *plane;
1573 
1574 	WARN_ON(!drm_atomic_get_new_crtc_state(state, crtc));
1575 
1576 	drm_dbg_atomic(crtc->dev,
1577 		       "Adding all current planes for [CRTC:%d:%s] to %p\n",
1578 		       crtc->base.id, crtc->name, state);
1579 
1580 	drm_for_each_plane_mask(plane, state->dev, old_crtc_state->plane_mask) {
1581 		struct drm_plane_state *plane_state =
1582 			drm_atomic_get_plane_state(state, plane);
1583 
1584 		if (IS_ERR(plane_state))
1585 			return PTR_ERR(plane_state);
1586 	}
1587 	return 0;
1588 }
1589 EXPORT_SYMBOL(drm_atomic_add_affected_planes);
1590 
1591 /**
1592  * drm_atomic_add_affected_colorops - add colorops for plane
1593  * @state: atomic state
1594  * @plane: DRM plane
1595  *
1596  * This function walks the current configuration and adds all colorops
1597  * currently used by @plane to the atomic configuration @state. This is useful
1598  * when an atomic commit also needs to check all currently enabled colorop on
1599  * @plane, e.g. when changing the mode. It's also useful when re-enabling a plane
1600  * to avoid special code to force-enable all colorops.
1601  *
1602  * Since acquiring a colorop state will always also acquire the w/w mutex of the
1603  * current plane for that colorop (if there is any) adding all the colorop states for
1604  * a plane will not reduce parallelism of atomic updates.
1605  *
1606  * Returns:
1607  * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1608  * then the w/w mutex code has detected a deadlock and the entire atomic
1609  * sequence must be restarted. All other errors are fatal.
1610  */
1611 int
1612 drm_atomic_add_affected_colorops(struct drm_atomic_state *state,
1613 				 struct drm_plane *plane)
1614 {
1615 	struct drm_colorop *colorop;
1616 	struct drm_colorop_state *colorop_state;
1617 
1618 	WARN_ON(!drm_atomic_get_new_plane_state(state, plane));
1619 
1620 	drm_dbg_atomic(plane->dev,
1621 		       "Adding all current colorops for [PLANE:%d:%s] to %p\n",
1622 		       plane->base.id, plane->name, state);
1623 
1624 	drm_for_each_colorop(colorop, plane->dev) {
1625 		if (colorop->plane != plane)
1626 			continue;
1627 
1628 		colorop_state = drm_atomic_get_colorop_state(state, colorop);
1629 		if (IS_ERR(colorop_state))
1630 			return PTR_ERR(colorop_state);
1631 	}
1632 
1633 	return 0;
1634 }
1635 EXPORT_SYMBOL(drm_atomic_add_affected_colorops);
1636 
1637 /**
1638  * drm_atomic_check_only - check whether a given config would work
1639  * @state: atomic configuration to check
1640  *
1641  * Note that this function can return -EDEADLK if the driver needed to acquire
1642  * more locks but encountered a deadlock. The caller must then do the usual w/w
1643  * backoff dance and restart. All other errors are fatal.
1644  *
1645  * Returns:
1646  * 0 on success, negative error code on failure.
1647  */
1648 int drm_atomic_check_only(struct drm_atomic_state *state)
1649 {
1650 	struct drm_device *dev = state->dev;
1651 	struct drm_mode_config *config = &dev->mode_config;
1652 	struct drm_plane *plane;
1653 	struct drm_plane_state *old_plane_state;
1654 	struct drm_plane_state *new_plane_state;
1655 	struct drm_crtc *crtc;
1656 	struct drm_crtc_state *old_crtc_state;
1657 	struct drm_crtc_state *new_crtc_state;
1658 	struct drm_connector *conn;
1659 	struct drm_connector_state *conn_state;
1660 	unsigned int requested_crtc = 0;
1661 	unsigned int affected_crtc = 0;
1662 	int i, ret = 0;
1663 
1664 	drm_dbg_atomic(dev, "checking %p\n", state);
1665 
1666 	for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
1667 		if (new_crtc_state->enable)
1668 			requested_crtc |= drm_crtc_mask(crtc);
1669 	}
1670 
1671 	for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
1672 		ret = drm_atomic_plane_check(old_plane_state, new_plane_state);
1673 		if (ret) {
1674 			drm_dbg_atomic(dev, "[PLANE:%d:%s] atomic core check failed\n",
1675 				       plane->base.id, plane->name);
1676 			return ret;
1677 		}
1678 	}
1679 
1680 	for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
1681 		ret = drm_atomic_crtc_check(old_crtc_state, new_crtc_state);
1682 		if (ret) {
1683 			drm_dbg_atomic(dev, "[CRTC:%d:%s] atomic core check failed\n",
1684 				       crtc->base.id, crtc->name);
1685 			return ret;
1686 		}
1687 	}
1688 
1689 	for_each_new_connector_in_state(state, conn, conn_state, i) {
1690 		ret = drm_atomic_connector_check(conn, conn_state);
1691 		if (ret) {
1692 			drm_dbg_atomic(dev, "[CONNECTOR:%d:%s] atomic core check failed\n",
1693 				       conn->base.id, conn->name);
1694 			return ret;
1695 		}
1696 	}
1697 
1698 	if (config->funcs->atomic_check) {
1699 		ret = config->funcs->atomic_check(state->dev, state);
1700 
1701 		if (ret) {
1702 			drm_dbg_atomic(dev, "atomic driver check for %p failed: %d\n",
1703 				       state, ret);
1704 			return ret;
1705 		}
1706 	}
1707 
1708 	if (!state->allow_modeset) {
1709 		for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
1710 			if (drm_atomic_crtc_needs_modeset(new_crtc_state)) {
1711 				drm_dbg_atomic(dev, "[CRTC:%d:%s] requires full modeset\n",
1712 					       crtc->base.id, crtc->name);
1713 				return -EINVAL;
1714 			}
1715 		}
1716 	}
1717 
1718 	for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
1719 		if (new_crtc_state->enable)
1720 			affected_crtc |= drm_crtc_mask(crtc);
1721 	}
1722 
1723 	/*
1724 	 * For commits that allow modesets drivers can add other CRTCs to the
1725 	 * atomic commit, e.g. when they need to reallocate global resources.
1726 	 * This can cause spurious EBUSY, which robs compositors of a very
1727 	 * effective sanity check for their drawing loop. Therefor only allow
1728 	 * drivers to add unrelated CRTC states for modeset commits.
1729 	 *
1730 	 * FIXME: Should add affected_crtc mask to the ATOMIC IOCTL as an output
1731 	 * so compositors know what's going on.
1732 	 */
1733 	if (affected_crtc != requested_crtc) {
1734 		drm_dbg_atomic(dev,
1735 			       "driver added CRTC to commit: requested 0x%x, affected 0x%0x\n",
1736 			       requested_crtc, affected_crtc);
1737 		WARN(!state->allow_modeset, "adding CRTC not allowed without modesets: requested 0x%x, affected 0x%0x\n",
1738 		     requested_crtc, affected_crtc);
1739 	}
1740 
1741 	state->checked = true;
1742 
1743 	return 0;
1744 }
1745 EXPORT_SYMBOL(drm_atomic_check_only);
1746 
1747 /**
1748  * drm_atomic_commit - commit configuration atomically
1749  * @state: atomic configuration to check
1750  *
1751  * Note that this function can return -EDEADLK if the driver needed to acquire
1752  * more locks but encountered a deadlock. The caller must then do the usual w/w
1753  * backoff dance and restart. All other errors are fatal.
1754  *
1755  * This function will take its own reference on @state.
1756  * Callers should always release their reference with drm_atomic_state_put().
1757  *
1758  * Returns:
1759  * 0 on success, negative error code on failure.
1760  */
1761 int drm_atomic_commit(struct drm_atomic_state *state)
1762 {
1763 	struct drm_mode_config *config = &state->dev->mode_config;
1764 	struct drm_printer p = drm_info_printer(state->dev->dev);
1765 	int ret;
1766 
1767 	if (drm_debug_enabled(DRM_UT_STATE))
1768 		drm_atomic_print_new_state(state, &p);
1769 
1770 	ret = drm_atomic_check_only(state);
1771 	if (ret)
1772 		return ret;
1773 
1774 	drm_dbg_atomic(state->dev, "committing %p\n", state);
1775 
1776 	return config->funcs->atomic_commit(state->dev, state, false);
1777 }
1778 EXPORT_SYMBOL(drm_atomic_commit);
1779 
1780 /**
1781  * drm_atomic_nonblocking_commit - atomic nonblocking commit
1782  * @state: atomic configuration to check
1783  *
1784  * Note that this function can return -EDEADLK if the driver needed to acquire
1785  * more locks but encountered a deadlock. The caller must then do the usual w/w
1786  * backoff dance and restart. All other errors are fatal.
1787  *
1788  * This function will take its own reference on @state.
1789  * Callers should always release their reference with drm_atomic_state_put().
1790  *
1791  * Returns:
1792  * 0 on success, negative error code on failure.
1793  */
1794 int drm_atomic_nonblocking_commit(struct drm_atomic_state *state)
1795 {
1796 	struct drm_mode_config *config = &state->dev->mode_config;
1797 	int ret;
1798 
1799 	ret = drm_atomic_check_only(state);
1800 	if (ret)
1801 		return ret;
1802 
1803 	drm_dbg_atomic(state->dev, "committing %p nonblocking\n", state);
1804 
1805 	return config->funcs->atomic_commit(state->dev, state, true);
1806 }
1807 EXPORT_SYMBOL(drm_atomic_nonblocking_commit);
1808 
1809 /* just used from drm-client and atomic-helper: */
1810 int __drm_atomic_helper_disable_plane(struct drm_plane *plane,
1811 				      struct drm_plane_state *plane_state)
1812 {
1813 	int ret;
1814 
1815 	ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
1816 	if (ret != 0)
1817 		return ret;
1818 
1819 	drm_atomic_set_fb_for_plane(plane_state, NULL);
1820 	plane_state->crtc_x = 0;
1821 	plane_state->crtc_y = 0;
1822 	plane_state->crtc_w = 0;
1823 	plane_state->crtc_h = 0;
1824 	plane_state->src_x = 0;
1825 	plane_state->src_y = 0;
1826 	plane_state->src_w = 0;
1827 	plane_state->src_h = 0;
1828 
1829 	return 0;
1830 }
1831 EXPORT_SYMBOL(__drm_atomic_helper_disable_plane);
1832 
1833 static int update_output_state(struct drm_atomic_state *state,
1834 			       struct drm_mode_set *set)
1835 {
1836 	struct drm_device *dev = set->crtc->dev;
1837 	struct drm_crtc *crtc;
1838 	struct drm_crtc_state *new_crtc_state;
1839 	struct drm_connector *connector;
1840 	struct drm_connector_state *new_conn_state;
1841 	int ret, i;
1842 
1843 	ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
1844 			       state->acquire_ctx);
1845 	if (ret)
1846 		return ret;
1847 
1848 	/* First disable all connectors on the target crtc. */
1849 	ret = drm_atomic_add_affected_connectors(state, set->crtc);
1850 	if (ret)
1851 		return ret;
1852 
1853 	for_each_new_connector_in_state(state, connector, new_conn_state, i) {
1854 		if (new_conn_state->crtc == set->crtc) {
1855 			ret = drm_atomic_set_crtc_for_connector(new_conn_state,
1856 								NULL);
1857 			if (ret)
1858 				return ret;
1859 
1860 			/* Make sure legacy setCrtc always re-trains */
1861 			new_conn_state->link_status = DRM_LINK_STATUS_GOOD;
1862 		}
1863 	}
1864 
1865 	/* Then set all connectors from set->connectors on the target crtc */
1866 	for (i = 0; i < set->num_connectors; i++) {
1867 		new_conn_state = drm_atomic_get_connector_state(state,
1868 								set->connectors[i]);
1869 		if (IS_ERR(new_conn_state))
1870 			return PTR_ERR(new_conn_state);
1871 
1872 		ret = drm_atomic_set_crtc_for_connector(new_conn_state,
1873 							set->crtc);
1874 		if (ret)
1875 			return ret;
1876 	}
1877 
1878 	for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
1879 		/*
1880 		 * Don't update ->enable for the CRTC in the set_config request,
1881 		 * since a mismatch would indicate a bug in the upper layers.
1882 		 * The actual modeset code later on will catch any
1883 		 * inconsistencies here.
1884 		 */
1885 		if (crtc == set->crtc)
1886 			continue;
1887 
1888 		if (!new_crtc_state->connector_mask) {
1889 			ret = drm_atomic_set_mode_prop_for_crtc(new_crtc_state,
1890 								NULL);
1891 			if (ret < 0)
1892 				return ret;
1893 
1894 			new_crtc_state->active = false;
1895 		}
1896 	}
1897 
1898 	return 0;
1899 }
1900 
1901 /* just used from drm-client and atomic-helper: */
1902 int __drm_atomic_helper_set_config(struct drm_mode_set *set,
1903 				   struct drm_atomic_state *state)
1904 {
1905 	struct drm_crtc_state *crtc_state;
1906 	struct drm_plane_state *primary_state;
1907 	struct drm_crtc *crtc = set->crtc;
1908 	int hdisplay, vdisplay;
1909 	int ret;
1910 
1911 	crtc_state = drm_atomic_get_crtc_state(state, crtc);
1912 	if (IS_ERR(crtc_state))
1913 		return PTR_ERR(crtc_state);
1914 
1915 	primary_state = drm_atomic_get_plane_state(state, crtc->primary);
1916 	if (IS_ERR(primary_state))
1917 		return PTR_ERR(primary_state);
1918 
1919 	if (!set->mode) {
1920 		WARN_ON(set->fb);
1921 		WARN_ON(set->num_connectors);
1922 
1923 		ret = drm_atomic_set_mode_for_crtc(crtc_state, NULL);
1924 		if (ret != 0)
1925 			return ret;
1926 
1927 		crtc_state->active = false;
1928 
1929 		ret = drm_atomic_set_crtc_for_plane(primary_state, NULL);
1930 		if (ret != 0)
1931 			return ret;
1932 
1933 		drm_atomic_set_fb_for_plane(primary_state, NULL);
1934 
1935 		goto commit;
1936 	}
1937 
1938 	WARN_ON(!set->fb);
1939 	WARN_ON(!set->num_connectors);
1940 
1941 	ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
1942 	if (ret != 0)
1943 		return ret;
1944 
1945 	crtc_state->active = true;
1946 
1947 	ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
1948 	if (ret != 0)
1949 		return ret;
1950 
1951 	drm_mode_get_hv_timing(set->mode, &hdisplay, &vdisplay);
1952 
1953 	drm_atomic_set_fb_for_plane(primary_state, set->fb);
1954 	primary_state->crtc_x = 0;
1955 	primary_state->crtc_y = 0;
1956 	primary_state->crtc_w = hdisplay;
1957 	primary_state->crtc_h = vdisplay;
1958 	primary_state->src_x = set->x << 16;
1959 	primary_state->src_y = set->y << 16;
1960 	if (drm_rotation_90_or_270(primary_state->rotation)) {
1961 		primary_state->src_w = vdisplay << 16;
1962 		primary_state->src_h = hdisplay << 16;
1963 	} else {
1964 		primary_state->src_w = hdisplay << 16;
1965 		primary_state->src_h = vdisplay << 16;
1966 	}
1967 
1968 commit:
1969 	ret = update_output_state(state, set);
1970 	if (ret)
1971 		return ret;
1972 
1973 	return 0;
1974 }
1975 EXPORT_SYMBOL(__drm_atomic_helper_set_config);
1976 
1977 static void drm_atomic_private_obj_print_state(struct drm_printer *p,
1978 					       const struct drm_private_state *state)
1979 {
1980 	struct drm_private_obj *obj = state->obj;
1981 
1982 	if (obj->funcs->atomic_print_state)
1983 		obj->funcs->atomic_print_state(p, state);
1984 }
1985 
1986 /**
1987  * drm_atomic_print_new_state - prints drm atomic state
1988  * @state: atomic configuration to check
1989  * @p: drm printer
1990  *
1991  * This functions prints the drm atomic state snapshot using the drm printer
1992  * which is passed to it. This snapshot can be used for debugging purposes.
1993  *
1994  * Note that this function looks into the new state objects and hence its not
1995  * safe to be used after the call to drm_atomic_helper_commit_hw_done().
1996  */
1997 void drm_atomic_print_new_state(const struct drm_atomic_state *state,
1998 		struct drm_printer *p)
1999 {
2000 	struct drm_plane *plane;
2001 	struct drm_plane_state *plane_state;
2002 	struct drm_crtc *crtc;
2003 	struct drm_crtc_state *crtc_state;
2004 	struct drm_connector *connector;
2005 	struct drm_connector_state *connector_state;
2006 	struct drm_private_obj *obj;
2007 	struct drm_private_state *obj_state;
2008 	int i;
2009 
2010 	if (!p) {
2011 		drm_err(state->dev, "invalid drm printer\n");
2012 		return;
2013 	}
2014 
2015 	drm_dbg_atomic(state->dev, "checking %p\n", state);
2016 
2017 	for_each_new_plane_in_state(state, plane, plane_state, i)
2018 		drm_atomic_plane_print_state(p, plane_state);
2019 
2020 	for_each_new_crtc_in_state(state, crtc, crtc_state, i)
2021 		drm_atomic_crtc_print_state(p, crtc_state);
2022 
2023 	for_each_new_connector_in_state(state, connector, connector_state, i)
2024 		drm_atomic_connector_print_state(p, connector_state);
2025 
2026 	for_each_new_private_obj_in_state(state, obj, obj_state, i)
2027 		drm_atomic_private_obj_print_state(p, obj_state);
2028 }
2029 EXPORT_SYMBOL(drm_atomic_print_new_state);
2030 
2031 static void __drm_state_dump(struct drm_device *dev, struct drm_printer *p,
2032 			     bool take_locks)
2033 {
2034 	struct drm_mode_config *config = &dev->mode_config;
2035 	struct drm_colorop *colorop;
2036 	struct drm_plane *plane;
2037 	struct drm_crtc *crtc;
2038 	struct drm_connector *connector;
2039 	struct drm_connector_list_iter conn_iter;
2040 	struct drm_private_obj *obj;
2041 
2042 	if (!drm_drv_uses_atomic_modeset(dev))
2043 		return;
2044 
2045 	list_for_each_entry(colorop, &config->colorop_list, head) {
2046 		if (take_locks)
2047 			drm_modeset_lock(&colorop->plane->mutex, NULL);
2048 		drm_atomic_colorop_print_state(p, colorop->state);
2049 		if (take_locks)
2050 			drm_modeset_unlock(&colorop->plane->mutex);
2051 	}
2052 
2053 	list_for_each_entry(plane, &config->plane_list, head) {
2054 		if (take_locks)
2055 			drm_modeset_lock(&plane->mutex, NULL);
2056 		drm_atomic_plane_print_state(p, plane->state);
2057 		if (take_locks)
2058 			drm_modeset_unlock(&plane->mutex);
2059 	}
2060 
2061 	list_for_each_entry(crtc, &config->crtc_list, head) {
2062 		if (take_locks)
2063 			drm_modeset_lock(&crtc->mutex, NULL);
2064 		drm_atomic_crtc_print_state(p, crtc->state);
2065 		if (take_locks)
2066 			drm_modeset_unlock(&crtc->mutex);
2067 	}
2068 
2069 	drm_connector_list_iter_begin(dev, &conn_iter);
2070 	if (take_locks)
2071 		drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
2072 	drm_for_each_connector_iter(connector, &conn_iter)
2073 		drm_atomic_connector_print_state(p, connector->state);
2074 	if (take_locks)
2075 		drm_modeset_unlock(&dev->mode_config.connection_mutex);
2076 	drm_connector_list_iter_end(&conn_iter);
2077 
2078 	list_for_each_entry(obj, &config->privobj_list, head) {
2079 		if (take_locks)
2080 			drm_modeset_lock(&obj->lock, NULL);
2081 		drm_atomic_private_obj_print_state(p, obj->state);
2082 		if (take_locks)
2083 			drm_modeset_unlock(&obj->lock);
2084 	}
2085 }
2086 
2087 /**
2088  * drm_state_dump - dump entire device atomic state
2089  * @dev: the drm device
2090  * @p: where to print the state to
2091  *
2092  * Just for debugging.  Drivers might want an option to dump state
2093  * to dmesg in case of error irq's.  (Hint, you probably want to
2094  * ratelimit this!)
2095  *
2096  * The caller must wrap this drm_modeset_lock_all_ctx() and
2097  * drm_modeset_drop_locks(). If this is called from error irq handler, it should
2098  * not be enabled by default - if you are debugging errors you might
2099  * not care that this is racey, but calling this without all modeset locks held
2100  * is inherently unsafe.
2101  */
2102 void drm_state_dump(struct drm_device *dev, struct drm_printer *p)
2103 {
2104 	__drm_state_dump(dev, p, false);
2105 }
2106 EXPORT_SYMBOL(drm_state_dump);
2107 
2108 #ifdef CONFIG_DEBUG_FS
2109 static int drm_state_info(struct seq_file *m, void *data)
2110 {
2111 	struct drm_debugfs_entry *entry = m->private;
2112 	struct drm_device *dev = entry->dev;
2113 	struct drm_printer p = drm_seq_file_printer(m);
2114 
2115 	__drm_state_dump(dev, &p, true);
2116 
2117 	return 0;
2118 }
2119 
2120 /* any use in debugfs files to dump individual planes/crtc/etc? */
2121 static const struct drm_debugfs_info drm_atomic_debugfs_list[] = {
2122 	{"state", drm_state_info, 0},
2123 };
2124 
2125 void drm_atomic_debugfs_init(struct drm_device *dev)
2126 {
2127 	drm_debugfs_add_files(dev, drm_atomic_debugfs_list,
2128 			      ARRAY_SIZE(drm_atomic_debugfs_list));
2129 }
2130 #endif
2131