xref: /linux/drivers/gpu/drm/drm_atomic.c (revision 50f0033d1a0f3a8e9eed09ab68067fbb57b0669d)
1 /*
2  * Copyright (C) 2014 Red Hat
3  * Copyright (C) 2014 Intel Corp.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21  * OTHER DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  * Rob Clark <robdclark@gmail.com>
25  * Daniel Vetter <daniel.vetter@ffwll.ch>
26  */
27 
28 
29 #include <drm/drmP.h>
30 #include <drm/drm_atomic.h>
31 #include <drm/drm_mode.h>
32 #include <drm/drm_plane_helper.h>
33 #include <drm/drm_print.h>
34 #include <linux/sync_file.h>
35 
36 #include "drm_crtc_internal.h"
37 
38 static void crtc_commit_free(struct kref *kref)
39 {
40 	struct drm_crtc_commit *commit =
41 		container_of(kref, struct drm_crtc_commit, ref);
42 
43 	kfree(commit);
44 }
45 
46 void drm_crtc_commit_put(struct drm_crtc_commit *commit)
47 {
48 	kref_put(&commit->ref, crtc_commit_free);
49 }
50 EXPORT_SYMBOL(drm_crtc_commit_put);
51 
52 /**
53  * drm_atomic_state_default_release -
54  * release memory initialized by drm_atomic_state_init
55  * @state: atomic state
56  *
57  * Free all the memory allocated by drm_atomic_state_init.
58  * This is useful for drivers that subclass the atomic state.
59  */
60 void drm_atomic_state_default_release(struct drm_atomic_state *state)
61 {
62 	kfree(state->connectors);
63 	kfree(state->crtcs);
64 	kfree(state->planes);
65 }
66 EXPORT_SYMBOL(drm_atomic_state_default_release);
67 
68 /**
69  * drm_atomic_state_init - init new atomic state
70  * @dev: DRM device
71  * @state: atomic state
72  *
73  * Default implementation for filling in a new atomic state.
74  * This is useful for drivers that subclass the atomic state.
75  */
76 int
77 drm_atomic_state_init(struct drm_device *dev, struct drm_atomic_state *state)
78 {
79 	kref_init(&state->ref);
80 
81 	/* TODO legacy paths should maybe do a better job about
82 	 * setting this appropriately?
83 	 */
84 	state->allow_modeset = true;
85 
86 	state->crtcs = kcalloc(dev->mode_config.num_crtc,
87 			       sizeof(*state->crtcs), GFP_KERNEL);
88 	if (!state->crtcs)
89 		goto fail;
90 	state->planes = kcalloc(dev->mode_config.num_total_plane,
91 				sizeof(*state->planes), GFP_KERNEL);
92 	if (!state->planes)
93 		goto fail;
94 
95 	state->dev = dev;
96 
97 	DRM_DEBUG_ATOMIC("Allocated atomic state %p\n", state);
98 
99 	return 0;
100 fail:
101 	drm_atomic_state_default_release(state);
102 	return -ENOMEM;
103 }
104 EXPORT_SYMBOL(drm_atomic_state_init);
105 
106 /**
107  * drm_atomic_state_alloc - allocate atomic state
108  * @dev: DRM device
109  *
110  * This allocates an empty atomic state to track updates.
111  */
112 struct drm_atomic_state *
113 drm_atomic_state_alloc(struct drm_device *dev)
114 {
115 	struct drm_mode_config *config = &dev->mode_config;
116 	struct drm_atomic_state *state;
117 
118 	if (!config->funcs->atomic_state_alloc) {
119 		state = kzalloc(sizeof(*state), GFP_KERNEL);
120 		if (!state)
121 			return NULL;
122 		if (drm_atomic_state_init(dev, state) < 0) {
123 			kfree(state);
124 			return NULL;
125 		}
126 		return state;
127 	}
128 
129 	return config->funcs->atomic_state_alloc(dev);
130 }
131 EXPORT_SYMBOL(drm_atomic_state_alloc);
132 
133 /**
134  * drm_atomic_state_default_clear - clear base atomic state
135  * @state: atomic state
136  *
137  * Default implementation for clearing atomic state.
138  * This is useful for drivers that subclass the atomic state.
139  */
140 void drm_atomic_state_default_clear(struct drm_atomic_state *state)
141 {
142 	struct drm_device *dev = state->dev;
143 	struct drm_mode_config *config = &dev->mode_config;
144 	int i;
145 
146 	DRM_DEBUG_ATOMIC("Clearing atomic state %p\n", state);
147 
148 	for (i = 0; i < state->num_connector; i++) {
149 		struct drm_connector *connector = state->connectors[i].ptr;
150 
151 		if (!connector)
152 			continue;
153 
154 		connector->funcs->atomic_destroy_state(connector,
155 						       state->connectors[i].state);
156 		state->connectors[i].ptr = NULL;
157 		state->connectors[i].state = NULL;
158 		drm_connector_unreference(connector);
159 	}
160 
161 	for (i = 0; i < config->num_crtc; i++) {
162 		struct drm_crtc *crtc = state->crtcs[i].ptr;
163 
164 		if (!crtc)
165 			continue;
166 
167 		crtc->funcs->atomic_destroy_state(crtc,
168 						  state->crtcs[i].state);
169 
170 		if (state->crtcs[i].commit) {
171 			kfree(state->crtcs[i].commit->event);
172 			state->crtcs[i].commit->event = NULL;
173 			drm_crtc_commit_put(state->crtcs[i].commit);
174 		}
175 
176 		state->crtcs[i].commit = NULL;
177 		state->crtcs[i].ptr = NULL;
178 		state->crtcs[i].state = NULL;
179 	}
180 
181 	for (i = 0; i < config->num_total_plane; i++) {
182 		struct drm_plane *plane = state->planes[i].ptr;
183 
184 		if (!plane)
185 			continue;
186 
187 		plane->funcs->atomic_destroy_state(plane,
188 						   state->planes[i].state);
189 		state->planes[i].ptr = NULL;
190 		state->planes[i].state = NULL;
191 	}
192 }
193 EXPORT_SYMBOL(drm_atomic_state_default_clear);
194 
195 /**
196  * drm_atomic_state_clear - clear state object
197  * @state: atomic state
198  *
199  * When the w/w mutex algorithm detects a deadlock we need to back off and drop
200  * all locks. So someone else could sneak in and change the current modeset
201  * configuration. Which means that all the state assembled in @state is no
202  * longer an atomic update to the current state, but to some arbitrary earlier
203  * state. Which could break assumptions the driver's ->atomic_check likely
204  * relies on.
205  *
206  * Hence we must clear all cached state and completely start over, using this
207  * function.
208  */
209 void drm_atomic_state_clear(struct drm_atomic_state *state)
210 {
211 	struct drm_device *dev = state->dev;
212 	struct drm_mode_config *config = &dev->mode_config;
213 
214 	if (config->funcs->atomic_state_clear)
215 		config->funcs->atomic_state_clear(state);
216 	else
217 		drm_atomic_state_default_clear(state);
218 }
219 EXPORT_SYMBOL(drm_atomic_state_clear);
220 
221 /**
222  * __drm_atomic_state_free - free all memory for an atomic state
223  * @ref: This atomic state to deallocate
224  *
225  * This frees all memory associated with an atomic state, including all the
226  * per-object state for planes, crtcs and connectors.
227  */
228 void __drm_atomic_state_free(struct kref *ref)
229 {
230 	struct drm_atomic_state *state = container_of(ref, typeof(*state), ref);
231 	struct drm_mode_config *config = &state->dev->mode_config;
232 
233 	drm_atomic_state_clear(state);
234 
235 	DRM_DEBUG_ATOMIC("Freeing atomic state %p\n", state);
236 
237 	if (config->funcs->atomic_state_free) {
238 		config->funcs->atomic_state_free(state);
239 	} else {
240 		drm_atomic_state_default_release(state);
241 		kfree(state);
242 	}
243 }
244 EXPORT_SYMBOL(__drm_atomic_state_free);
245 
246 /**
247  * drm_atomic_get_crtc_state - get crtc state
248  * @state: global atomic state object
249  * @crtc: crtc to get state object for
250  *
251  * This function returns the crtc state for the given crtc, allocating it if
252  * needed. It will also grab the relevant crtc lock to make sure that the state
253  * is consistent.
254  *
255  * Returns:
256  *
257  * Either the allocated state or the error code encoded into the pointer. When
258  * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
259  * entire atomic sequence must be restarted. All other errors are fatal.
260  */
261 struct drm_crtc_state *
262 drm_atomic_get_crtc_state(struct drm_atomic_state *state,
263 			  struct drm_crtc *crtc)
264 {
265 	int ret, index = drm_crtc_index(crtc);
266 	struct drm_crtc_state *crtc_state;
267 
268 	WARN_ON(!state->acquire_ctx);
269 
270 	crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
271 	if (crtc_state)
272 		return crtc_state;
273 
274 	ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx);
275 	if (ret)
276 		return ERR_PTR(ret);
277 
278 	crtc_state = crtc->funcs->atomic_duplicate_state(crtc);
279 	if (!crtc_state)
280 		return ERR_PTR(-ENOMEM);
281 
282 	state->crtcs[index].state = crtc_state;
283 	state->crtcs[index].ptr = crtc;
284 	crtc_state->state = state;
285 
286 	DRM_DEBUG_ATOMIC("Added [CRTC:%d:%s] %p state to %p\n",
287 			 crtc->base.id, crtc->name, crtc_state, state);
288 
289 	return crtc_state;
290 }
291 EXPORT_SYMBOL(drm_atomic_get_crtc_state);
292 
293 static void set_out_fence_for_crtc(struct drm_atomic_state *state,
294 				   struct drm_crtc *crtc, s64 __user *fence_ptr)
295 {
296 	state->crtcs[drm_crtc_index(crtc)].out_fence_ptr = fence_ptr;
297 }
298 
299 static s64 __user *get_out_fence_for_crtc(struct drm_atomic_state *state,
300 					  struct drm_crtc *crtc)
301 {
302 	s64 __user *fence_ptr;
303 
304 	fence_ptr = state->crtcs[drm_crtc_index(crtc)].out_fence_ptr;
305 	state->crtcs[drm_crtc_index(crtc)].out_fence_ptr = NULL;
306 
307 	return fence_ptr;
308 }
309 
310 /**
311  * drm_atomic_set_mode_for_crtc - set mode for CRTC
312  * @state: the CRTC whose incoming state to update
313  * @mode: kernel-internal mode to use for the CRTC, or NULL to disable
314  *
315  * Set a mode (originating from the kernel) on the desired CRTC state. Does
316  * not change any other state properties, including enable, active, or
317  * mode_changed.
318  *
319  * RETURNS:
320  * Zero on success, error code on failure. Cannot return -EDEADLK.
321  */
322 int drm_atomic_set_mode_for_crtc(struct drm_crtc_state *state,
323 				 struct drm_display_mode *mode)
324 {
325 	struct drm_mode_modeinfo umode;
326 
327 	/* Early return for no change. */
328 	if (mode && memcmp(&state->mode, mode, sizeof(*mode)) == 0)
329 		return 0;
330 
331 	drm_property_unreference_blob(state->mode_blob);
332 	state->mode_blob = NULL;
333 
334 	if (mode) {
335 		drm_mode_convert_to_umode(&umode, mode);
336 		state->mode_blob =
337 			drm_property_create_blob(state->crtc->dev,
338 		                                 sizeof(umode),
339 		                                 &umode);
340 		if (IS_ERR(state->mode_blob))
341 			return PTR_ERR(state->mode_blob);
342 
343 		drm_mode_copy(&state->mode, mode);
344 		state->enable = true;
345 		DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n",
346 				 mode->name, state);
347 	} else {
348 		memset(&state->mode, 0, sizeof(state->mode));
349 		state->enable = false;
350 		DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n",
351 				 state);
352 	}
353 
354 	return 0;
355 }
356 EXPORT_SYMBOL(drm_atomic_set_mode_for_crtc);
357 
358 /**
359  * drm_atomic_set_mode_prop_for_crtc - set mode for CRTC
360  * @state: the CRTC whose incoming state to update
361  * @blob: pointer to blob property to use for mode
362  *
363  * Set a mode (originating from a blob property) on the desired CRTC state.
364  * This function will take a reference on the blob property for the CRTC state,
365  * and release the reference held on the state's existing mode property, if any
366  * was set.
367  *
368  * RETURNS:
369  * Zero on success, error code on failure. Cannot return -EDEADLK.
370  */
371 int drm_atomic_set_mode_prop_for_crtc(struct drm_crtc_state *state,
372                                       struct drm_property_blob *blob)
373 {
374 	if (blob == state->mode_blob)
375 		return 0;
376 
377 	drm_property_unreference_blob(state->mode_blob);
378 	state->mode_blob = NULL;
379 
380 	memset(&state->mode, 0, sizeof(state->mode));
381 
382 	if (blob) {
383 		if (blob->length != sizeof(struct drm_mode_modeinfo) ||
384 		    drm_mode_convert_umode(&state->mode,
385 		                           (const struct drm_mode_modeinfo *)
386 		                            blob->data))
387 			return -EINVAL;
388 
389 		state->mode_blob = drm_property_reference_blob(blob);
390 		state->enable = true;
391 		DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n",
392 				 state->mode.name, state);
393 	} else {
394 		state->enable = false;
395 		DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n",
396 				 state);
397 	}
398 
399 	return 0;
400 }
401 EXPORT_SYMBOL(drm_atomic_set_mode_prop_for_crtc);
402 
403 /**
404  * drm_atomic_replace_property_blob - replace a blob property
405  * @blob: a pointer to the member blob to be replaced
406  * @new_blob: the new blob to replace with
407  * @replaced: whether the blob has been replaced
408  *
409  * RETURNS:
410  * Zero on success, error code on failure
411  */
412 static void
413 drm_atomic_replace_property_blob(struct drm_property_blob **blob,
414 				 struct drm_property_blob *new_blob,
415 				 bool *replaced)
416 {
417 	struct drm_property_blob *old_blob = *blob;
418 
419 	if (old_blob == new_blob)
420 		return;
421 
422 	drm_property_unreference_blob(old_blob);
423 	if (new_blob)
424 		drm_property_reference_blob(new_blob);
425 	*blob = new_blob;
426 	*replaced = true;
427 
428 	return;
429 }
430 
431 static int
432 drm_atomic_replace_property_blob_from_id(struct drm_crtc *crtc,
433 					 struct drm_property_blob **blob,
434 					 uint64_t blob_id,
435 					 ssize_t expected_size,
436 					 bool *replaced)
437 {
438 	struct drm_property_blob *new_blob = NULL;
439 
440 	if (blob_id != 0) {
441 		new_blob = drm_property_lookup_blob(crtc->dev, blob_id);
442 		if (new_blob == NULL)
443 			return -EINVAL;
444 
445 		if (expected_size > 0 && expected_size != new_blob->length) {
446 			drm_property_unreference_blob(new_blob);
447 			return -EINVAL;
448 		}
449 	}
450 
451 	drm_atomic_replace_property_blob(blob, new_blob, replaced);
452 	drm_property_unreference_blob(new_blob);
453 
454 	return 0;
455 }
456 
457 /**
458  * drm_atomic_crtc_set_property - set property on CRTC
459  * @crtc: the drm CRTC to set a property on
460  * @state: the state object to update with the new property value
461  * @property: the property to set
462  * @val: the new property value
463  *
464  * Use this instead of calling crtc->atomic_set_property directly.
465  * This function handles generic/core properties and calls out to
466  * driver's ->atomic_set_property() for driver properties.  To ensure
467  * consistent behavior you must call this function rather than the
468  * driver hook directly.
469  *
470  * RETURNS:
471  * Zero on success, error code on failure
472  */
473 int drm_atomic_crtc_set_property(struct drm_crtc *crtc,
474 		struct drm_crtc_state *state, struct drm_property *property,
475 		uint64_t val)
476 {
477 	struct drm_device *dev = crtc->dev;
478 	struct drm_mode_config *config = &dev->mode_config;
479 	bool replaced = false;
480 	int ret;
481 
482 	if (property == config->prop_active)
483 		state->active = val;
484 	else if (property == config->prop_mode_id) {
485 		struct drm_property_blob *mode =
486 			drm_property_lookup_blob(dev, val);
487 		ret = drm_atomic_set_mode_prop_for_crtc(state, mode);
488 		drm_property_unreference_blob(mode);
489 		return ret;
490 	} else if (property == config->degamma_lut_property) {
491 		ret = drm_atomic_replace_property_blob_from_id(crtc,
492 					&state->degamma_lut,
493 					val,
494 					-1,
495 					&replaced);
496 		state->color_mgmt_changed |= replaced;
497 		return ret;
498 	} else if (property == config->ctm_property) {
499 		ret = drm_atomic_replace_property_blob_from_id(crtc,
500 					&state->ctm,
501 					val,
502 					sizeof(struct drm_color_ctm),
503 					&replaced);
504 		state->color_mgmt_changed |= replaced;
505 		return ret;
506 	} else if (property == config->gamma_lut_property) {
507 		ret = drm_atomic_replace_property_blob_from_id(crtc,
508 					&state->gamma_lut,
509 					val,
510 					-1,
511 					&replaced);
512 		state->color_mgmt_changed |= replaced;
513 		return ret;
514 	} else if (property == config->prop_out_fence_ptr) {
515 		s64 __user *fence_ptr = u64_to_user_ptr(val);
516 
517 		if (!fence_ptr)
518 			return 0;
519 
520 		if (put_user(-1, fence_ptr))
521 			return -EFAULT;
522 
523 		set_out_fence_for_crtc(state->state, crtc, fence_ptr);
524 	} else if (crtc->funcs->atomic_set_property)
525 		return crtc->funcs->atomic_set_property(crtc, state, property, val);
526 	else
527 		return -EINVAL;
528 
529 	return 0;
530 }
531 EXPORT_SYMBOL(drm_atomic_crtc_set_property);
532 
533 /**
534  * drm_atomic_crtc_get_property - get property value from CRTC state
535  * @crtc: the drm CRTC to set a property on
536  * @state: the state object to get the property value from
537  * @property: the property to set
538  * @val: return location for the property value
539  *
540  * This function handles generic/core properties and calls out to
541  * driver's ->atomic_get_property() for driver properties.  To ensure
542  * consistent behavior you must call this function rather than the
543  * driver hook directly.
544  *
545  * RETURNS:
546  * Zero on success, error code on failure
547  */
548 static int
549 drm_atomic_crtc_get_property(struct drm_crtc *crtc,
550 		const struct drm_crtc_state *state,
551 		struct drm_property *property, uint64_t *val)
552 {
553 	struct drm_device *dev = crtc->dev;
554 	struct drm_mode_config *config = &dev->mode_config;
555 
556 	if (property == config->prop_active)
557 		*val = state->active;
558 	else if (property == config->prop_mode_id)
559 		*val = (state->mode_blob) ? state->mode_blob->base.id : 0;
560 	else if (property == config->degamma_lut_property)
561 		*val = (state->degamma_lut) ? state->degamma_lut->base.id : 0;
562 	else if (property == config->ctm_property)
563 		*val = (state->ctm) ? state->ctm->base.id : 0;
564 	else if (property == config->gamma_lut_property)
565 		*val = (state->gamma_lut) ? state->gamma_lut->base.id : 0;
566 	else if (property == config->prop_out_fence_ptr)
567 		*val = 0;
568 	else if (crtc->funcs->atomic_get_property)
569 		return crtc->funcs->atomic_get_property(crtc, state, property, val);
570 	else
571 		return -EINVAL;
572 
573 	return 0;
574 }
575 
576 /**
577  * drm_atomic_crtc_check - check crtc state
578  * @crtc: crtc to check
579  * @state: crtc state to check
580  *
581  * Provides core sanity checks for crtc state.
582  *
583  * RETURNS:
584  * Zero on success, error code on failure
585  */
586 static int drm_atomic_crtc_check(struct drm_crtc *crtc,
587 		struct drm_crtc_state *state)
588 {
589 	/* NOTE: we explicitly don't enforce constraints such as primary
590 	 * layer covering entire screen, since that is something we want
591 	 * to allow (on hw that supports it).  For hw that does not, it
592 	 * should be checked in driver's crtc->atomic_check() vfunc.
593 	 *
594 	 * TODO: Add generic modeset state checks once we support those.
595 	 */
596 
597 	if (state->active && !state->enable) {
598 		DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active without enabled\n",
599 				 crtc->base.id, crtc->name);
600 		return -EINVAL;
601 	}
602 
603 	/* The state->enable vs. state->mode_blob checks can be WARN_ON,
604 	 * as this is a kernel-internal detail that userspace should never
605 	 * be able to trigger. */
606 	if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
607 	    WARN_ON(state->enable && !state->mode_blob)) {
608 		DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled without mode blob\n",
609 				 crtc->base.id, crtc->name);
610 		return -EINVAL;
611 	}
612 
613 	if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
614 	    WARN_ON(!state->enable && state->mode_blob)) {
615 		DRM_DEBUG_ATOMIC("[CRTC:%d:%s] disabled with mode blob\n",
616 				 crtc->base.id, crtc->name);
617 		return -EINVAL;
618 	}
619 
620 	/*
621 	 * Reject event generation for when a CRTC is off and stays off.
622 	 * It wouldn't be hard to implement this, but userspace has a track
623 	 * record of happily burning through 100% cpu (or worse, crash) when the
624 	 * display pipe is suspended. To avoid all that fun just reject updates
625 	 * that ask for events since likely that indicates a bug in the
626 	 * compositor's drawing loop. This is consistent with the vblank IOCTL
627 	 * and legacy page_flip IOCTL which also reject service on a disabled
628 	 * pipe.
629 	 */
630 	if (state->event && !state->active && !crtc->state->active) {
631 		DRM_DEBUG_ATOMIC("[CRTC:%d] requesting event but off\n",
632 				 crtc->base.id);
633 		return -EINVAL;
634 	}
635 
636 	return 0;
637 }
638 
639 static void drm_atomic_crtc_print_state(struct drm_printer *p,
640 		const struct drm_crtc_state *state)
641 {
642 	struct drm_crtc *crtc = state->crtc;
643 
644 	drm_printf(p, "crtc[%u]: %s\n", crtc->base.id, crtc->name);
645 	drm_printf(p, "\tenable=%d\n", state->enable);
646 	drm_printf(p, "\tactive=%d\n", state->active);
647 	drm_printf(p, "\tplanes_changed=%d\n", state->planes_changed);
648 	drm_printf(p, "\tmode_changed=%d\n", state->mode_changed);
649 	drm_printf(p, "\tactive_changed=%d\n", state->active_changed);
650 	drm_printf(p, "\tconnectors_changed=%d\n", state->connectors_changed);
651 	drm_printf(p, "\tcolor_mgmt_changed=%d\n", state->color_mgmt_changed);
652 	drm_printf(p, "\tplane_mask=%x\n", state->plane_mask);
653 	drm_printf(p, "\tconnector_mask=%x\n", state->connector_mask);
654 	drm_printf(p, "\tencoder_mask=%x\n", state->encoder_mask);
655 	drm_printf(p, "\tmode: " DRM_MODE_FMT "\n", DRM_MODE_ARG(&state->mode));
656 
657 	if (crtc->funcs->atomic_print_state)
658 		crtc->funcs->atomic_print_state(p, state);
659 }
660 
661 /**
662  * drm_atomic_get_plane_state - get plane state
663  * @state: global atomic state object
664  * @plane: plane to get state object for
665  *
666  * This function returns the plane state for the given plane, allocating it if
667  * needed. It will also grab the relevant plane lock to make sure that the state
668  * is consistent.
669  *
670  * Returns:
671  *
672  * Either the allocated state or the error code encoded into the pointer. When
673  * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
674  * entire atomic sequence must be restarted. All other errors are fatal.
675  */
676 struct drm_plane_state *
677 drm_atomic_get_plane_state(struct drm_atomic_state *state,
678 			  struct drm_plane *plane)
679 {
680 	int ret, index = drm_plane_index(plane);
681 	struct drm_plane_state *plane_state;
682 
683 	WARN_ON(!state->acquire_ctx);
684 
685 	plane_state = drm_atomic_get_existing_plane_state(state, plane);
686 	if (plane_state)
687 		return plane_state;
688 
689 	ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
690 	if (ret)
691 		return ERR_PTR(ret);
692 
693 	plane_state = plane->funcs->atomic_duplicate_state(plane);
694 	if (!plane_state)
695 		return ERR_PTR(-ENOMEM);
696 
697 	state->planes[index].state = plane_state;
698 	state->planes[index].ptr = plane;
699 	plane_state->state = state;
700 
701 	DRM_DEBUG_ATOMIC("Added [PLANE:%d:%s] %p state to %p\n",
702 			 plane->base.id, plane->name, plane_state, state);
703 
704 	if (plane_state->crtc) {
705 		struct drm_crtc_state *crtc_state;
706 
707 		crtc_state = drm_atomic_get_crtc_state(state,
708 						       plane_state->crtc);
709 		if (IS_ERR(crtc_state))
710 			return ERR_CAST(crtc_state);
711 	}
712 
713 	return plane_state;
714 }
715 EXPORT_SYMBOL(drm_atomic_get_plane_state);
716 
717 /**
718  * drm_atomic_plane_set_property - set property on plane
719  * @plane: the drm plane to set a property on
720  * @state: the state object to update with the new property value
721  * @property: the property to set
722  * @val: the new property value
723  *
724  * Use this instead of calling plane->atomic_set_property directly.
725  * This function handles generic/core properties and calls out to
726  * driver's ->atomic_set_property() for driver properties.  To ensure
727  * consistent behavior you must call this function rather than the
728  * driver hook directly.
729  *
730  * RETURNS:
731  * Zero on success, error code on failure
732  */
733 int drm_atomic_plane_set_property(struct drm_plane *plane,
734 		struct drm_plane_state *state, struct drm_property *property,
735 		uint64_t val)
736 {
737 	struct drm_device *dev = plane->dev;
738 	struct drm_mode_config *config = &dev->mode_config;
739 
740 	if (property == config->prop_fb_id) {
741 		struct drm_framebuffer *fb = drm_framebuffer_lookup(dev, val);
742 		drm_atomic_set_fb_for_plane(state, fb);
743 		if (fb)
744 			drm_framebuffer_unreference(fb);
745 	} else if (property == config->prop_in_fence_fd) {
746 		if (state->fence)
747 			return -EINVAL;
748 
749 		if (U642I64(val) == -1)
750 			return 0;
751 
752 		state->fence = sync_file_get_fence(val);
753 		if (!state->fence)
754 			return -EINVAL;
755 
756 	} else if (property == config->prop_crtc_id) {
757 		struct drm_crtc *crtc = drm_crtc_find(dev, val);
758 		return drm_atomic_set_crtc_for_plane(state, crtc);
759 	} else if (property == config->prop_crtc_x) {
760 		state->crtc_x = U642I64(val);
761 	} else if (property == config->prop_crtc_y) {
762 		state->crtc_y = U642I64(val);
763 	} else if (property == config->prop_crtc_w) {
764 		state->crtc_w = val;
765 	} else if (property == config->prop_crtc_h) {
766 		state->crtc_h = val;
767 	} else if (property == config->prop_src_x) {
768 		state->src_x = val;
769 	} else if (property == config->prop_src_y) {
770 		state->src_y = val;
771 	} else if (property == config->prop_src_w) {
772 		state->src_w = val;
773 	} else if (property == config->prop_src_h) {
774 		state->src_h = val;
775 	} else if (property == plane->rotation_property) {
776 		if (!is_power_of_2(val & DRM_ROTATE_MASK))
777 			return -EINVAL;
778 		state->rotation = val;
779 	} else if (property == plane->zpos_property) {
780 		state->zpos = val;
781 	} else if (plane->funcs->atomic_set_property) {
782 		return plane->funcs->atomic_set_property(plane, state,
783 				property, val);
784 	} else {
785 		return -EINVAL;
786 	}
787 
788 	return 0;
789 }
790 EXPORT_SYMBOL(drm_atomic_plane_set_property);
791 
792 /**
793  * drm_atomic_plane_get_property - get property value from plane state
794  * @plane: the drm plane to set a property on
795  * @state: the state object to get the property value from
796  * @property: the property to set
797  * @val: return location for the property value
798  *
799  * This function handles generic/core properties and calls out to
800  * driver's ->atomic_get_property() for driver properties.  To ensure
801  * consistent behavior you must call this function rather than the
802  * driver hook directly.
803  *
804  * RETURNS:
805  * Zero on success, error code on failure
806  */
807 static int
808 drm_atomic_plane_get_property(struct drm_plane *plane,
809 		const struct drm_plane_state *state,
810 		struct drm_property *property, uint64_t *val)
811 {
812 	struct drm_device *dev = plane->dev;
813 	struct drm_mode_config *config = &dev->mode_config;
814 
815 	if (property == config->prop_fb_id) {
816 		*val = (state->fb) ? state->fb->base.id : 0;
817 	} else if (property == config->prop_in_fence_fd) {
818 		*val = -1;
819 	} else if (property == config->prop_crtc_id) {
820 		*val = (state->crtc) ? state->crtc->base.id : 0;
821 	} else if (property == config->prop_crtc_x) {
822 		*val = I642U64(state->crtc_x);
823 	} else if (property == config->prop_crtc_y) {
824 		*val = I642U64(state->crtc_y);
825 	} else if (property == config->prop_crtc_w) {
826 		*val = state->crtc_w;
827 	} else if (property == config->prop_crtc_h) {
828 		*val = state->crtc_h;
829 	} else if (property == config->prop_src_x) {
830 		*val = state->src_x;
831 	} else if (property == config->prop_src_y) {
832 		*val = state->src_y;
833 	} else if (property == config->prop_src_w) {
834 		*val = state->src_w;
835 	} else if (property == config->prop_src_h) {
836 		*val = state->src_h;
837 	} else if (property == plane->rotation_property) {
838 		*val = state->rotation;
839 	} else if (property == plane->zpos_property) {
840 		*val = state->zpos;
841 	} else if (plane->funcs->atomic_get_property) {
842 		return plane->funcs->atomic_get_property(plane, state, property, val);
843 	} else {
844 		return -EINVAL;
845 	}
846 
847 	return 0;
848 }
849 
850 static bool
851 plane_switching_crtc(struct drm_atomic_state *state,
852 		     struct drm_plane *plane,
853 		     struct drm_plane_state *plane_state)
854 {
855 	if (!plane->state->crtc || !plane_state->crtc)
856 		return false;
857 
858 	if (plane->state->crtc == plane_state->crtc)
859 		return false;
860 
861 	/* This could be refined, but currently there's no helper or driver code
862 	 * to implement direct switching of active planes nor userspace to take
863 	 * advantage of more direct plane switching without the intermediate
864 	 * full OFF state.
865 	 */
866 	return true;
867 }
868 
869 /**
870  * drm_atomic_plane_check - check plane state
871  * @plane: plane to check
872  * @state: plane state to check
873  *
874  * Provides core sanity checks for plane state.
875  *
876  * RETURNS:
877  * Zero on success, error code on failure
878  */
879 static int drm_atomic_plane_check(struct drm_plane *plane,
880 		struct drm_plane_state *state)
881 {
882 	unsigned int fb_width, fb_height;
883 	int ret;
884 
885 	/* either *both* CRTC and FB must be set, or neither */
886 	if (WARN_ON(state->crtc && !state->fb)) {
887 		DRM_DEBUG_ATOMIC("CRTC set but no FB\n");
888 		return -EINVAL;
889 	} else if (WARN_ON(state->fb && !state->crtc)) {
890 		DRM_DEBUG_ATOMIC("FB set but no CRTC\n");
891 		return -EINVAL;
892 	}
893 
894 	/* if disabled, we don't care about the rest of the state: */
895 	if (!state->crtc)
896 		return 0;
897 
898 	/* Check whether this plane is usable on this CRTC */
899 	if (!(plane->possible_crtcs & drm_crtc_mask(state->crtc))) {
900 		DRM_DEBUG_ATOMIC("Invalid crtc for plane\n");
901 		return -EINVAL;
902 	}
903 
904 	/* Check whether this plane supports the fb pixel format. */
905 	ret = drm_plane_check_pixel_format(plane, state->fb->format->format);
906 	if (ret) {
907 		struct drm_format_name_buf format_name;
908 		DRM_DEBUG_ATOMIC("Invalid pixel format %s\n",
909 		                 drm_get_format_name(state->fb->format->format,
910 		                                     &format_name));
911 		return ret;
912 	}
913 
914 	/* Give drivers some help against integer overflows */
915 	if (state->crtc_w > INT_MAX ||
916 	    state->crtc_x > INT_MAX - (int32_t) state->crtc_w ||
917 	    state->crtc_h > INT_MAX ||
918 	    state->crtc_y > INT_MAX - (int32_t) state->crtc_h) {
919 		DRM_DEBUG_ATOMIC("Invalid CRTC coordinates %ux%u+%d+%d\n",
920 				 state->crtc_w, state->crtc_h,
921 				 state->crtc_x, state->crtc_y);
922 		return -ERANGE;
923 	}
924 
925 	fb_width = state->fb->width << 16;
926 	fb_height = state->fb->height << 16;
927 
928 	/* Make sure source coordinates are inside the fb. */
929 	if (state->src_w > fb_width ||
930 	    state->src_x > fb_width - state->src_w ||
931 	    state->src_h > fb_height ||
932 	    state->src_y > fb_height - state->src_h) {
933 		DRM_DEBUG_ATOMIC("Invalid source coordinates "
934 				 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
935 				 state->src_w >> 16, ((state->src_w & 0xffff) * 15625) >> 10,
936 				 state->src_h >> 16, ((state->src_h & 0xffff) * 15625) >> 10,
937 				 state->src_x >> 16, ((state->src_x & 0xffff) * 15625) >> 10,
938 				 state->src_y >> 16, ((state->src_y & 0xffff) * 15625) >> 10);
939 		return -ENOSPC;
940 	}
941 
942 	if (plane_switching_crtc(state->state, plane, state)) {
943 		DRM_DEBUG_ATOMIC("[PLANE:%d:%s] switching CRTC directly\n",
944 				 plane->base.id, plane->name);
945 		return -EINVAL;
946 	}
947 
948 	return 0;
949 }
950 
951 static void drm_atomic_plane_print_state(struct drm_printer *p,
952 		const struct drm_plane_state *state)
953 {
954 	struct drm_plane *plane = state->plane;
955 	struct drm_rect src  = drm_plane_state_src(state);
956 	struct drm_rect dest = drm_plane_state_dest(state);
957 
958 	drm_printf(p, "plane[%u]: %s\n", plane->base.id, plane->name);
959 	drm_printf(p, "\tcrtc=%s\n", state->crtc ? state->crtc->name : "(null)");
960 	drm_printf(p, "\tfb=%u\n", state->fb ? state->fb->base.id : 0);
961 	if (state->fb) {
962 		struct drm_framebuffer *fb = state->fb;
963 		int i, n = fb->format->num_planes;
964 		struct drm_format_name_buf format_name;
965 
966 		drm_printf(p, "\t\tformat=%s\n",
967 		              drm_get_format_name(fb->format->format, &format_name));
968 		drm_printf(p, "\t\t\tmodifier=0x%llx\n", fb->modifier);
969 		drm_printf(p, "\t\tsize=%dx%d\n", fb->width, fb->height);
970 		drm_printf(p, "\t\tlayers:\n");
971 		for (i = 0; i < n; i++) {
972 			drm_printf(p, "\t\t\tpitch[%d]=%u\n", i, fb->pitches[i]);
973 			drm_printf(p, "\t\t\toffset[%d]=%u\n", i, fb->offsets[i]);
974 		}
975 	}
976 	drm_printf(p, "\tcrtc-pos=" DRM_RECT_FMT "\n", DRM_RECT_ARG(&dest));
977 	drm_printf(p, "\tsrc-pos=" DRM_RECT_FP_FMT "\n", DRM_RECT_FP_ARG(&src));
978 	drm_printf(p, "\trotation=%x\n", state->rotation);
979 
980 	if (plane->funcs->atomic_print_state)
981 		plane->funcs->atomic_print_state(p, state);
982 }
983 
984 /**
985  * drm_atomic_get_connector_state - get connector state
986  * @state: global atomic state object
987  * @connector: connector to get state object for
988  *
989  * This function returns the connector state for the given connector,
990  * allocating it if needed. It will also grab the relevant connector lock to
991  * make sure that the state is consistent.
992  *
993  * Returns:
994  *
995  * Either the allocated state or the error code encoded into the pointer. When
996  * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
997  * entire atomic sequence must be restarted. All other errors are fatal.
998  */
999 struct drm_connector_state *
1000 drm_atomic_get_connector_state(struct drm_atomic_state *state,
1001 			  struct drm_connector *connector)
1002 {
1003 	int ret, index;
1004 	struct drm_mode_config *config = &connector->dev->mode_config;
1005 	struct drm_connector_state *connector_state;
1006 
1007 	WARN_ON(!state->acquire_ctx);
1008 
1009 	ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
1010 	if (ret)
1011 		return ERR_PTR(ret);
1012 
1013 	index = drm_connector_index(connector);
1014 
1015 	if (index >= state->num_connector) {
1016 		struct __drm_connnectors_state *c;
1017 		int alloc = max(index + 1, config->num_connector);
1018 
1019 		c = krealloc(state->connectors, alloc * sizeof(*state->connectors), GFP_KERNEL);
1020 		if (!c)
1021 			return ERR_PTR(-ENOMEM);
1022 
1023 		state->connectors = c;
1024 		memset(&state->connectors[state->num_connector], 0,
1025 		       sizeof(*state->connectors) * (alloc - state->num_connector));
1026 
1027 		state->num_connector = alloc;
1028 	}
1029 
1030 	if (state->connectors[index].state)
1031 		return state->connectors[index].state;
1032 
1033 	connector_state = connector->funcs->atomic_duplicate_state(connector);
1034 	if (!connector_state)
1035 		return ERR_PTR(-ENOMEM);
1036 
1037 	drm_connector_reference(connector);
1038 	state->connectors[index].state = connector_state;
1039 	state->connectors[index].ptr = connector;
1040 	connector_state->state = state;
1041 
1042 	DRM_DEBUG_ATOMIC("Added [CONNECTOR:%d] %p state to %p\n",
1043 			 connector->base.id, connector_state, state);
1044 
1045 	if (connector_state->crtc) {
1046 		struct drm_crtc_state *crtc_state;
1047 
1048 		crtc_state = drm_atomic_get_crtc_state(state,
1049 						       connector_state->crtc);
1050 		if (IS_ERR(crtc_state))
1051 			return ERR_CAST(crtc_state);
1052 	}
1053 
1054 	return connector_state;
1055 }
1056 EXPORT_SYMBOL(drm_atomic_get_connector_state);
1057 
1058 /**
1059  * drm_atomic_connector_set_property - set property on connector.
1060  * @connector: the drm connector to set a property on
1061  * @state: the state object to update with the new property value
1062  * @property: the property to set
1063  * @val: the new property value
1064  *
1065  * Use this instead of calling connector->atomic_set_property directly.
1066  * This function handles generic/core properties and calls out to
1067  * driver's ->atomic_set_property() for driver properties.  To ensure
1068  * consistent behavior you must call this function rather than the
1069  * driver hook directly.
1070  *
1071  * RETURNS:
1072  * Zero on success, error code on failure
1073  */
1074 int drm_atomic_connector_set_property(struct drm_connector *connector,
1075 		struct drm_connector_state *state, struct drm_property *property,
1076 		uint64_t val)
1077 {
1078 	struct drm_device *dev = connector->dev;
1079 	struct drm_mode_config *config = &dev->mode_config;
1080 
1081 	if (property == config->prop_crtc_id) {
1082 		struct drm_crtc *crtc = drm_crtc_find(dev, val);
1083 		return drm_atomic_set_crtc_for_connector(state, crtc);
1084 	} else if (property == config->dpms_property) {
1085 		/* setting DPMS property requires special handling, which
1086 		 * is done in legacy setprop path for us.  Disallow (for
1087 		 * now?) atomic writes to DPMS property:
1088 		 */
1089 		return -EINVAL;
1090 	} else if (property == config->tv_select_subconnector_property) {
1091 		state->tv.subconnector = val;
1092 	} else if (property == config->tv_left_margin_property) {
1093 		state->tv.margins.left = val;
1094 	} else if (property == config->tv_right_margin_property) {
1095 		state->tv.margins.right = val;
1096 	} else if (property == config->tv_top_margin_property) {
1097 		state->tv.margins.top = val;
1098 	} else if (property == config->tv_bottom_margin_property) {
1099 		state->tv.margins.bottom = val;
1100 	} else if (property == config->tv_mode_property) {
1101 		state->tv.mode = val;
1102 	} else if (property == config->tv_brightness_property) {
1103 		state->tv.brightness = val;
1104 	} else if (property == config->tv_contrast_property) {
1105 		state->tv.contrast = val;
1106 	} else if (property == config->tv_flicker_reduction_property) {
1107 		state->tv.flicker_reduction = val;
1108 	} else if (property == config->tv_overscan_property) {
1109 		state->tv.overscan = val;
1110 	} else if (property == config->tv_saturation_property) {
1111 		state->tv.saturation = val;
1112 	} else if (property == config->tv_hue_property) {
1113 		state->tv.hue = val;
1114 	} else if (connector->funcs->atomic_set_property) {
1115 		return connector->funcs->atomic_set_property(connector,
1116 				state, property, val);
1117 	} else {
1118 		return -EINVAL;
1119 	}
1120 
1121 	return 0;
1122 }
1123 EXPORT_SYMBOL(drm_atomic_connector_set_property);
1124 
1125 static void drm_atomic_connector_print_state(struct drm_printer *p,
1126 		const struct drm_connector_state *state)
1127 {
1128 	struct drm_connector *connector = state->connector;
1129 
1130 	drm_printf(p, "connector[%u]: %s\n", connector->base.id, connector->name);
1131 	drm_printf(p, "\tcrtc=%s\n", state->crtc ? state->crtc->name : "(null)");
1132 
1133 	if (connector->funcs->atomic_print_state)
1134 		connector->funcs->atomic_print_state(p, state);
1135 }
1136 
1137 /**
1138  * drm_atomic_connector_get_property - get property value from connector state
1139  * @connector: the drm connector to set a property on
1140  * @state: the state object to get the property value from
1141  * @property: the property to set
1142  * @val: return location for the property value
1143  *
1144  * This function handles generic/core properties and calls out to
1145  * driver's ->atomic_get_property() for driver properties.  To ensure
1146  * consistent behavior you must call this function rather than the
1147  * driver hook directly.
1148  *
1149  * RETURNS:
1150  * Zero on success, error code on failure
1151  */
1152 static int
1153 drm_atomic_connector_get_property(struct drm_connector *connector,
1154 		const struct drm_connector_state *state,
1155 		struct drm_property *property, uint64_t *val)
1156 {
1157 	struct drm_device *dev = connector->dev;
1158 	struct drm_mode_config *config = &dev->mode_config;
1159 
1160 	if (property == config->prop_crtc_id) {
1161 		*val = (state->crtc) ? state->crtc->base.id : 0;
1162 	} else if (property == config->dpms_property) {
1163 		*val = connector->dpms;
1164 	} else if (property == config->tv_select_subconnector_property) {
1165 		*val = state->tv.subconnector;
1166 	} else if (property == config->tv_left_margin_property) {
1167 		*val = state->tv.margins.left;
1168 	} else if (property == config->tv_right_margin_property) {
1169 		*val = state->tv.margins.right;
1170 	} else if (property == config->tv_top_margin_property) {
1171 		*val = state->tv.margins.top;
1172 	} else if (property == config->tv_bottom_margin_property) {
1173 		*val = state->tv.margins.bottom;
1174 	} else if (property == config->tv_mode_property) {
1175 		*val = state->tv.mode;
1176 	} else if (property == config->tv_brightness_property) {
1177 		*val = state->tv.brightness;
1178 	} else if (property == config->tv_contrast_property) {
1179 		*val = state->tv.contrast;
1180 	} else if (property == config->tv_flicker_reduction_property) {
1181 		*val = state->tv.flicker_reduction;
1182 	} else if (property == config->tv_overscan_property) {
1183 		*val = state->tv.overscan;
1184 	} else if (property == config->tv_saturation_property) {
1185 		*val = state->tv.saturation;
1186 	} else if (property == config->tv_hue_property) {
1187 		*val = state->tv.hue;
1188 	} else if (connector->funcs->atomic_get_property) {
1189 		return connector->funcs->atomic_get_property(connector,
1190 				state, property, val);
1191 	} else {
1192 		return -EINVAL;
1193 	}
1194 
1195 	return 0;
1196 }
1197 
1198 int drm_atomic_get_property(struct drm_mode_object *obj,
1199 		struct drm_property *property, uint64_t *val)
1200 {
1201 	struct drm_device *dev = property->dev;
1202 	int ret;
1203 
1204 	switch (obj->type) {
1205 	case DRM_MODE_OBJECT_CONNECTOR: {
1206 		struct drm_connector *connector = obj_to_connector(obj);
1207 		WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
1208 		ret = drm_atomic_connector_get_property(connector,
1209 				connector->state, property, val);
1210 		break;
1211 	}
1212 	case DRM_MODE_OBJECT_CRTC: {
1213 		struct drm_crtc *crtc = obj_to_crtc(obj);
1214 		WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
1215 		ret = drm_atomic_crtc_get_property(crtc,
1216 				crtc->state, property, val);
1217 		break;
1218 	}
1219 	case DRM_MODE_OBJECT_PLANE: {
1220 		struct drm_plane *plane = obj_to_plane(obj);
1221 		WARN_ON(!drm_modeset_is_locked(&plane->mutex));
1222 		ret = drm_atomic_plane_get_property(plane,
1223 				plane->state, property, val);
1224 		break;
1225 	}
1226 	default:
1227 		ret = -EINVAL;
1228 		break;
1229 	}
1230 
1231 	return ret;
1232 }
1233 
1234 /**
1235  * drm_atomic_set_crtc_for_plane - set crtc for plane
1236  * @plane_state: the plane whose incoming state to update
1237  * @crtc: crtc to use for the plane
1238  *
1239  * Changing the assigned crtc for a plane requires us to grab the lock and state
1240  * for the new crtc, as needed. This function takes care of all these details
1241  * besides updating the pointer in the state object itself.
1242  *
1243  * Returns:
1244  * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1245  * then the w/w mutex code has detected a deadlock and the entire atomic
1246  * sequence must be restarted. All other errors are fatal.
1247  */
1248 int
1249 drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
1250 			      struct drm_crtc *crtc)
1251 {
1252 	struct drm_plane *plane = plane_state->plane;
1253 	struct drm_crtc_state *crtc_state;
1254 
1255 	if (plane_state->crtc) {
1256 		crtc_state = drm_atomic_get_crtc_state(plane_state->state,
1257 						       plane_state->crtc);
1258 		if (WARN_ON(IS_ERR(crtc_state)))
1259 			return PTR_ERR(crtc_state);
1260 
1261 		crtc_state->plane_mask &= ~(1 << drm_plane_index(plane));
1262 	}
1263 
1264 	plane_state->crtc = crtc;
1265 
1266 	if (crtc) {
1267 		crtc_state = drm_atomic_get_crtc_state(plane_state->state,
1268 						       crtc);
1269 		if (IS_ERR(crtc_state))
1270 			return PTR_ERR(crtc_state);
1271 		crtc_state->plane_mask |= (1 << drm_plane_index(plane));
1272 	}
1273 
1274 	if (crtc)
1275 		DRM_DEBUG_ATOMIC("Link plane state %p to [CRTC:%d:%s]\n",
1276 				 plane_state, crtc->base.id, crtc->name);
1277 	else
1278 		DRM_DEBUG_ATOMIC("Link plane state %p to [NOCRTC]\n",
1279 				 plane_state);
1280 
1281 	return 0;
1282 }
1283 EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane);
1284 
1285 /**
1286  * drm_atomic_set_fb_for_plane - set framebuffer for plane
1287  * @plane_state: atomic state object for the plane
1288  * @fb: fb to use for the plane
1289  *
1290  * Changing the assigned framebuffer for a plane requires us to grab a reference
1291  * to the new fb and drop the reference to the old fb, if there is one. This
1292  * function takes care of all these details besides updating the pointer in the
1293  * state object itself.
1294  */
1295 void
1296 drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
1297 			    struct drm_framebuffer *fb)
1298 {
1299 	if (fb)
1300 		DRM_DEBUG_ATOMIC("Set [FB:%d] for plane state %p\n",
1301 				 fb->base.id, plane_state);
1302 	else
1303 		DRM_DEBUG_ATOMIC("Set [NOFB] for plane state %p\n",
1304 				 plane_state);
1305 
1306 	drm_framebuffer_assign(&plane_state->fb, fb);
1307 }
1308 EXPORT_SYMBOL(drm_atomic_set_fb_for_plane);
1309 
1310 /**
1311  * drm_atomic_set_fence_for_plane - set fence for plane
1312  * @plane_state: atomic state object for the plane
1313  * @fence: dma_fence to use for the plane
1314  *
1315  * Helper to setup the plane_state fence in case it is not set yet.
1316  * By using this drivers doesn't need to worry if the user choose
1317  * implicit or explicit fencing.
1318  *
1319  * This function will not set the fence to the state if it was set
1320  * via explicit fencing interfaces on the atomic ioctl. It will
1321  * all drope the reference to the fence as we not storing it
1322  * anywhere.
1323  *
1324  * Otherwise, if plane_state->fence is not set this function we
1325  * just set it with the received implict fence.
1326  */
1327 void
1328 drm_atomic_set_fence_for_plane(struct drm_plane_state *plane_state,
1329 			       struct dma_fence *fence)
1330 {
1331 	if (plane_state->fence) {
1332 		dma_fence_put(fence);
1333 		return;
1334 	}
1335 
1336 	plane_state->fence = fence;
1337 }
1338 EXPORT_SYMBOL(drm_atomic_set_fence_for_plane);
1339 
1340 /**
1341  * drm_atomic_set_crtc_for_connector - set crtc for connector
1342  * @conn_state: atomic state object for the connector
1343  * @crtc: crtc to use for the connector
1344  *
1345  * Changing the assigned crtc for a connector requires us to grab the lock and
1346  * state for the new crtc, as needed. This function takes care of all these
1347  * details besides updating the pointer in the state object itself.
1348  *
1349  * Returns:
1350  * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1351  * then the w/w mutex code has detected a deadlock and the entire atomic
1352  * sequence must be restarted. All other errors are fatal.
1353  */
1354 int
1355 drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
1356 				  struct drm_crtc *crtc)
1357 {
1358 	struct drm_crtc_state *crtc_state;
1359 
1360 	if (conn_state->crtc == crtc)
1361 		return 0;
1362 
1363 	if (conn_state->crtc) {
1364 		crtc_state = drm_atomic_get_existing_crtc_state(conn_state->state,
1365 								conn_state->crtc);
1366 
1367 		crtc_state->connector_mask &=
1368 			~(1 << drm_connector_index(conn_state->connector));
1369 
1370 		drm_connector_unreference(conn_state->connector);
1371 		conn_state->crtc = NULL;
1372 	}
1373 
1374 	if (crtc) {
1375 		crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc);
1376 		if (IS_ERR(crtc_state))
1377 			return PTR_ERR(crtc_state);
1378 
1379 		crtc_state->connector_mask |=
1380 			1 << drm_connector_index(conn_state->connector);
1381 
1382 		drm_connector_reference(conn_state->connector);
1383 		conn_state->crtc = crtc;
1384 
1385 		DRM_DEBUG_ATOMIC("Link connector state %p to [CRTC:%d:%s]\n",
1386 				 conn_state, crtc->base.id, crtc->name);
1387 	} else {
1388 		DRM_DEBUG_ATOMIC("Link connector state %p to [NOCRTC]\n",
1389 				 conn_state);
1390 	}
1391 
1392 	return 0;
1393 }
1394 EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector);
1395 
1396 /**
1397  * drm_atomic_add_affected_connectors - add connectors for crtc
1398  * @state: atomic state
1399  * @crtc: DRM crtc
1400  *
1401  * This function walks the current configuration and adds all connectors
1402  * currently using @crtc to the atomic configuration @state. Note that this
1403  * function must acquire the connection mutex. This can potentially cause
1404  * unneeded seralization if the update is just for the planes on one crtc. Hence
1405  * drivers and helpers should only call this when really needed (e.g. when a
1406  * full modeset needs to happen due to some change).
1407  *
1408  * Returns:
1409  * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1410  * then the w/w mutex code has detected a deadlock and the entire atomic
1411  * sequence must be restarted. All other errors are fatal.
1412  */
1413 int
1414 drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
1415 				   struct drm_crtc *crtc)
1416 {
1417 	struct drm_mode_config *config = &state->dev->mode_config;
1418 	struct drm_connector *connector;
1419 	struct drm_connector_state *conn_state;
1420 	struct drm_connector_list_iter conn_iter;
1421 	int ret;
1422 
1423 	ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
1424 	if (ret)
1425 		return ret;
1426 
1427 	DRM_DEBUG_ATOMIC("Adding all current connectors for [CRTC:%d:%s] to %p\n",
1428 			 crtc->base.id, crtc->name, state);
1429 
1430 	/*
1431 	 * Changed connectors are already in @state, so only need to look at the
1432 	 * current configuration.
1433 	 */
1434 	drm_connector_list_iter_get(state->dev, &conn_iter);
1435 	drm_for_each_connector_iter(connector, &conn_iter) {
1436 		if (connector->state->crtc != crtc)
1437 			continue;
1438 
1439 		conn_state = drm_atomic_get_connector_state(state, connector);
1440 		if (IS_ERR(conn_state)) {
1441 			drm_connector_list_iter_put(&conn_iter);
1442 			return PTR_ERR(conn_state);
1443 		}
1444 	}
1445 	drm_connector_list_iter_put(&conn_iter);
1446 
1447 	return 0;
1448 }
1449 EXPORT_SYMBOL(drm_atomic_add_affected_connectors);
1450 
1451 /**
1452  * drm_atomic_add_affected_planes - add planes for crtc
1453  * @state: atomic state
1454  * @crtc: DRM crtc
1455  *
1456  * This function walks the current configuration and adds all planes
1457  * currently used by @crtc to the atomic configuration @state. This is useful
1458  * when an atomic commit also needs to check all currently enabled plane on
1459  * @crtc, e.g. when changing the mode. It's also useful when re-enabling a CRTC
1460  * to avoid special code to force-enable all planes.
1461  *
1462  * Since acquiring a plane state will always also acquire the w/w mutex of the
1463  * current CRTC for that plane (if there is any) adding all the plane states for
1464  * a CRTC will not reduce parallism of atomic updates.
1465  *
1466  * Returns:
1467  * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1468  * then the w/w mutex code has detected a deadlock and the entire atomic
1469  * sequence must be restarted. All other errors are fatal.
1470  */
1471 int
1472 drm_atomic_add_affected_planes(struct drm_atomic_state *state,
1473 			       struct drm_crtc *crtc)
1474 {
1475 	struct drm_plane *plane;
1476 
1477 	WARN_ON(!drm_atomic_get_existing_crtc_state(state, crtc));
1478 
1479 	drm_for_each_plane_mask(plane, state->dev, crtc->state->plane_mask) {
1480 		struct drm_plane_state *plane_state =
1481 			drm_atomic_get_plane_state(state, plane);
1482 
1483 		if (IS_ERR(plane_state))
1484 			return PTR_ERR(plane_state);
1485 	}
1486 	return 0;
1487 }
1488 EXPORT_SYMBOL(drm_atomic_add_affected_planes);
1489 
1490 /**
1491  * drm_atomic_legacy_backoff - locking backoff for legacy ioctls
1492  * @state: atomic state
1493  *
1494  * This function should be used by legacy entry points which don't understand
1495  * -EDEADLK semantics. For simplicity this one will grab all modeset locks after
1496  * the slowpath completed.
1497  */
1498 void drm_atomic_legacy_backoff(struct drm_atomic_state *state)
1499 {
1500 	struct drm_device *dev = state->dev;
1501 	unsigned crtc_mask = 0;
1502 	struct drm_crtc *crtc;
1503 	int ret;
1504 	bool global = false;
1505 
1506 	drm_for_each_crtc(crtc, dev) {
1507 		if (crtc->acquire_ctx != state->acquire_ctx)
1508 			continue;
1509 
1510 		crtc_mask |= drm_crtc_mask(crtc);
1511 		crtc->acquire_ctx = NULL;
1512 	}
1513 
1514 	if (WARN_ON(dev->mode_config.acquire_ctx == state->acquire_ctx)) {
1515 		global = true;
1516 
1517 		dev->mode_config.acquire_ctx = NULL;
1518 	}
1519 
1520 retry:
1521 	drm_modeset_backoff(state->acquire_ctx);
1522 
1523 	ret = drm_modeset_lock_all_ctx(dev, state->acquire_ctx);
1524 	if (ret)
1525 		goto retry;
1526 
1527 	drm_for_each_crtc(crtc, dev)
1528 		if (drm_crtc_mask(crtc) & crtc_mask)
1529 			crtc->acquire_ctx = state->acquire_ctx;
1530 
1531 	if (global)
1532 		dev->mode_config.acquire_ctx = state->acquire_ctx;
1533 }
1534 EXPORT_SYMBOL(drm_atomic_legacy_backoff);
1535 
1536 /**
1537  * drm_atomic_check_only - check whether a given config would work
1538  * @state: atomic configuration to check
1539  *
1540  * Note that this function can return -EDEADLK if the driver needed to acquire
1541  * more locks but encountered a deadlock. The caller must then do the usual w/w
1542  * backoff dance and restart. All other errors are fatal.
1543  *
1544  * Returns:
1545  * 0 on success, negative error code on failure.
1546  */
1547 int drm_atomic_check_only(struct drm_atomic_state *state)
1548 {
1549 	struct drm_device *dev = state->dev;
1550 	struct drm_mode_config *config = &dev->mode_config;
1551 	struct drm_plane *plane;
1552 	struct drm_plane_state *plane_state;
1553 	struct drm_crtc *crtc;
1554 	struct drm_crtc_state *crtc_state;
1555 	int i, ret = 0;
1556 
1557 	DRM_DEBUG_ATOMIC("checking %p\n", state);
1558 
1559 	for_each_plane_in_state(state, plane, plane_state, i) {
1560 		ret = drm_atomic_plane_check(plane, plane_state);
1561 		if (ret) {
1562 			DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic core check failed\n",
1563 					 plane->base.id, plane->name);
1564 			return ret;
1565 		}
1566 	}
1567 
1568 	for_each_crtc_in_state(state, crtc, crtc_state, i) {
1569 		ret = drm_atomic_crtc_check(crtc, crtc_state);
1570 		if (ret) {
1571 			DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic core check failed\n",
1572 					 crtc->base.id, crtc->name);
1573 			return ret;
1574 		}
1575 	}
1576 
1577 	if (config->funcs->atomic_check)
1578 		ret = config->funcs->atomic_check(state->dev, state);
1579 
1580 	if (!state->allow_modeset) {
1581 		for_each_crtc_in_state(state, crtc, crtc_state, i) {
1582 			if (drm_atomic_crtc_needs_modeset(crtc_state)) {
1583 				DRM_DEBUG_ATOMIC("[CRTC:%d:%s] requires full modeset\n",
1584 						 crtc->base.id, crtc->name);
1585 				return -EINVAL;
1586 			}
1587 		}
1588 	}
1589 
1590 	return ret;
1591 }
1592 EXPORT_SYMBOL(drm_atomic_check_only);
1593 
1594 /**
1595  * drm_atomic_commit - commit configuration atomically
1596  * @state: atomic configuration to check
1597  *
1598  * Note that this function can return -EDEADLK if the driver needed to acquire
1599  * more locks but encountered a deadlock. The caller must then do the usual w/w
1600  * backoff dance and restart. All other errors are fatal.
1601  *
1602  * Also note that on successful execution ownership of @state is transferred
1603  * from the caller of this function to the function itself. The caller must not
1604  * free or in any other way access @state. If the function fails then the caller
1605  * must clean up @state itself.
1606  *
1607  * Returns:
1608  * 0 on success, negative error code on failure.
1609  */
1610 int drm_atomic_commit(struct drm_atomic_state *state)
1611 {
1612 	struct drm_mode_config *config = &state->dev->mode_config;
1613 	int ret;
1614 
1615 	ret = drm_atomic_check_only(state);
1616 	if (ret)
1617 		return ret;
1618 
1619 	DRM_DEBUG_ATOMIC("commiting %p\n", state);
1620 
1621 	return config->funcs->atomic_commit(state->dev, state, false);
1622 }
1623 EXPORT_SYMBOL(drm_atomic_commit);
1624 
1625 /**
1626  * drm_atomic_nonblocking_commit - atomic&nonblocking configuration commit
1627  * @state: atomic configuration to check
1628  *
1629  * Note that this function can return -EDEADLK if the driver needed to acquire
1630  * more locks but encountered a deadlock. The caller must then do the usual w/w
1631  * backoff dance and restart. All other errors are fatal.
1632  *
1633  * Also note that on successful execution ownership of @state is transferred
1634  * from the caller of this function to the function itself. The caller must not
1635  * free or in any other way access @state. If the function fails then the caller
1636  * must clean up @state itself.
1637  *
1638  * Returns:
1639  * 0 on success, negative error code on failure.
1640  */
1641 int drm_atomic_nonblocking_commit(struct drm_atomic_state *state)
1642 {
1643 	struct drm_mode_config *config = &state->dev->mode_config;
1644 	int ret;
1645 
1646 	ret = drm_atomic_check_only(state);
1647 	if (ret)
1648 		return ret;
1649 
1650 	DRM_DEBUG_ATOMIC("commiting %p nonblocking\n", state);
1651 
1652 	return config->funcs->atomic_commit(state->dev, state, true);
1653 }
1654 EXPORT_SYMBOL(drm_atomic_nonblocking_commit);
1655 
1656 static void drm_atomic_print_state(const struct drm_atomic_state *state)
1657 {
1658 	struct drm_printer p = drm_info_printer(state->dev->dev);
1659 	struct drm_plane *plane;
1660 	struct drm_plane_state *plane_state;
1661 	struct drm_crtc *crtc;
1662 	struct drm_crtc_state *crtc_state;
1663 	struct drm_connector *connector;
1664 	struct drm_connector_state *connector_state;
1665 	int i;
1666 
1667 	DRM_DEBUG_ATOMIC("checking %p\n", state);
1668 
1669 	for_each_plane_in_state(state, plane, plane_state, i)
1670 		drm_atomic_plane_print_state(&p, plane_state);
1671 
1672 	for_each_crtc_in_state(state, crtc, crtc_state, i)
1673 		drm_atomic_crtc_print_state(&p, crtc_state);
1674 
1675 	for_each_connector_in_state(state, connector, connector_state, i)
1676 		drm_atomic_connector_print_state(&p, connector_state);
1677 }
1678 
1679 /**
1680  * drm_state_dump - dump entire device atomic state
1681  * @dev: the drm device
1682  * @p: where to print the state to
1683  *
1684  * Just for debugging.  Drivers might want an option to dump state
1685  * to dmesg in case of error irq's.  (Hint, you probably want to
1686  * ratelimit this!)
1687  *
1688  * The caller must drm_modeset_lock_all(), or if this is called
1689  * from error irq handler, it should not be enabled by default.
1690  * (Ie. if you are debugging errors you might not care that this
1691  * is racey.  But calling this without all modeset locks held is
1692  * not inherently safe.)
1693  */
1694 void drm_state_dump(struct drm_device *dev, struct drm_printer *p)
1695 {
1696 	struct drm_mode_config *config = &dev->mode_config;
1697 	struct drm_plane *plane;
1698 	struct drm_crtc *crtc;
1699 	struct drm_connector *connector;
1700 	struct drm_connector_list_iter conn_iter;
1701 
1702 	if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
1703 		return;
1704 
1705 	list_for_each_entry(plane, &config->plane_list, head)
1706 		drm_atomic_plane_print_state(p, plane->state);
1707 
1708 	list_for_each_entry(crtc, &config->crtc_list, head)
1709 		drm_atomic_crtc_print_state(p, crtc->state);
1710 
1711 	drm_connector_list_iter_get(dev, &conn_iter);
1712 	drm_for_each_connector_iter(connector, &conn_iter)
1713 		drm_atomic_connector_print_state(p, connector->state);
1714 	drm_connector_list_iter_put(&conn_iter);
1715 }
1716 EXPORT_SYMBOL(drm_state_dump);
1717 
1718 #ifdef CONFIG_DEBUG_FS
1719 static int drm_state_info(struct seq_file *m, void *data)
1720 {
1721 	struct drm_info_node *node = (struct drm_info_node *) m->private;
1722 	struct drm_device *dev = node->minor->dev;
1723 	struct drm_printer p = drm_seq_file_printer(m);
1724 
1725 	drm_modeset_lock_all(dev);
1726 	drm_state_dump(dev, &p);
1727 	drm_modeset_unlock_all(dev);
1728 
1729 	return 0;
1730 }
1731 
1732 /* any use in debugfs files to dump individual planes/crtc/etc? */
1733 static const struct drm_info_list drm_atomic_debugfs_list[] = {
1734 	{"state", drm_state_info, 0},
1735 };
1736 
1737 int drm_atomic_debugfs_init(struct drm_minor *minor)
1738 {
1739 	return drm_debugfs_create_files(drm_atomic_debugfs_list,
1740 			ARRAY_SIZE(drm_atomic_debugfs_list),
1741 			minor->debugfs_root, minor);
1742 }
1743 
1744 int drm_atomic_debugfs_cleanup(struct drm_minor *minor)
1745 {
1746 	return drm_debugfs_remove_files(drm_atomic_debugfs_list,
1747 					ARRAY_SIZE(drm_atomic_debugfs_list),
1748 					minor);
1749 }
1750 #endif
1751 
1752 /*
1753  * The big monstor ioctl
1754  */
1755 
1756 static struct drm_pending_vblank_event *create_vblank_event(
1757 		struct drm_device *dev, uint64_t user_data)
1758 {
1759 	struct drm_pending_vblank_event *e = NULL;
1760 
1761 	e = kzalloc(sizeof *e, GFP_KERNEL);
1762 	if (!e)
1763 		return NULL;
1764 
1765 	e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
1766 	e->event.base.length = sizeof(e->event);
1767 	e->event.user_data = user_data;
1768 
1769 	return e;
1770 }
1771 
1772 static int atomic_set_prop(struct drm_atomic_state *state,
1773 		struct drm_mode_object *obj, struct drm_property *prop,
1774 		uint64_t prop_value)
1775 {
1776 	struct drm_mode_object *ref;
1777 	int ret;
1778 
1779 	if (!drm_property_change_valid_get(prop, prop_value, &ref))
1780 		return -EINVAL;
1781 
1782 	switch (obj->type) {
1783 	case DRM_MODE_OBJECT_CONNECTOR: {
1784 		struct drm_connector *connector = obj_to_connector(obj);
1785 		struct drm_connector_state *connector_state;
1786 
1787 		connector_state = drm_atomic_get_connector_state(state, connector);
1788 		if (IS_ERR(connector_state)) {
1789 			ret = PTR_ERR(connector_state);
1790 			break;
1791 		}
1792 
1793 		ret = drm_atomic_connector_set_property(connector,
1794 				connector_state, prop, prop_value);
1795 		break;
1796 	}
1797 	case DRM_MODE_OBJECT_CRTC: {
1798 		struct drm_crtc *crtc = obj_to_crtc(obj);
1799 		struct drm_crtc_state *crtc_state;
1800 
1801 		crtc_state = drm_atomic_get_crtc_state(state, crtc);
1802 		if (IS_ERR(crtc_state)) {
1803 			ret = PTR_ERR(crtc_state);
1804 			break;
1805 		}
1806 
1807 		ret = drm_atomic_crtc_set_property(crtc,
1808 				crtc_state, prop, prop_value);
1809 		break;
1810 	}
1811 	case DRM_MODE_OBJECT_PLANE: {
1812 		struct drm_plane *plane = obj_to_plane(obj);
1813 		struct drm_plane_state *plane_state;
1814 
1815 		plane_state = drm_atomic_get_plane_state(state, plane);
1816 		if (IS_ERR(plane_state)) {
1817 			ret = PTR_ERR(plane_state);
1818 			break;
1819 		}
1820 
1821 		ret = drm_atomic_plane_set_property(plane,
1822 				plane_state, prop, prop_value);
1823 		break;
1824 	}
1825 	default:
1826 		ret = -EINVAL;
1827 		break;
1828 	}
1829 
1830 	drm_property_change_valid_put(prop, ref);
1831 	return ret;
1832 }
1833 
1834 /**
1835  * drm_atomic_clean_old_fb -- Unset old_fb pointers and set plane->fb pointers.
1836  *
1837  * @dev: drm device to check.
1838  * @plane_mask: plane mask for planes that were updated.
1839  * @ret: return value, can be -EDEADLK for a retry.
1840  *
1841  * Before doing an update plane->old_fb is set to plane->fb,
1842  * but before dropping the locks old_fb needs to be set to NULL
1843  * and plane->fb updated. This is a common operation for each
1844  * atomic update, so this call is split off as a helper.
1845  */
1846 void drm_atomic_clean_old_fb(struct drm_device *dev,
1847 			     unsigned plane_mask,
1848 			     int ret)
1849 {
1850 	struct drm_plane *plane;
1851 
1852 	/* if succeeded, fixup legacy plane crtc/fb ptrs before dropping
1853 	 * locks (ie. while it is still safe to deref plane->state).  We
1854 	 * need to do this here because the driver entry points cannot
1855 	 * distinguish between legacy and atomic ioctls.
1856 	 */
1857 	drm_for_each_plane_mask(plane, dev, plane_mask) {
1858 		if (ret == 0) {
1859 			struct drm_framebuffer *new_fb = plane->state->fb;
1860 			if (new_fb)
1861 				drm_framebuffer_reference(new_fb);
1862 			plane->fb = new_fb;
1863 			plane->crtc = plane->state->crtc;
1864 
1865 			if (plane->old_fb)
1866 				drm_framebuffer_unreference(plane->old_fb);
1867 		}
1868 		plane->old_fb = NULL;
1869 	}
1870 }
1871 EXPORT_SYMBOL(drm_atomic_clean_old_fb);
1872 
1873 /**
1874  * DOC: explicit fencing properties
1875  *
1876  * Explicit fencing allows userspace to control the buffer synchronization
1877  * between devices. A Fence or a group of fences are transfered to/from
1878  * userspace using Sync File fds and there are two DRM properties for that.
1879  * IN_FENCE_FD on each DRM Plane to send fences to the kernel and
1880  * OUT_FENCE_PTR on each DRM CRTC to receive fences from the kernel.
1881  *
1882  * As a contrast, with implicit fencing the kernel keeps track of any
1883  * ongoing rendering, and automatically ensures that the atomic update waits
1884  * for any pending rendering to complete. For shared buffers represented with
1885  * a struct &dma_buf this is tracked in &reservation_object structures.
1886  * Implicit syncing is how Linux traditionally worked (e.g. DRI2/3 on X.org),
1887  * whereas explicit fencing is what Android wants.
1888  *
1889  * "IN_FENCE_FD”:
1890  *	Use this property to pass a fence that DRM should wait on before
1891  *	proceeding with the Atomic Commit request and show the framebuffer for
1892  *	the plane on the screen. The fence can be either a normal fence or a
1893  *	merged one, the sync_file framework will handle both cases and use a
1894  *	fence_array if a merged fence is received. Passing -1 here means no
1895  *	fences to wait on.
1896  *
1897  *	If the Atomic Commit request has the DRM_MODE_ATOMIC_TEST_ONLY flag
1898  *	it will only check if the Sync File is a valid one.
1899  *
1900  *	On the driver side the fence is stored on the @fence parameter of
1901  *	struct &drm_plane_state. Drivers which also support implicit fencing
1902  *	should set the implicit fence using drm_atomic_set_fence_for_plane(),
1903  *	to make sure there's consistent behaviour between drivers in precedence
1904  *	of implicit vs. explicit fencing.
1905  *
1906  * "OUT_FENCE_PTR”:
1907  *	Use this property to pass a file descriptor pointer to DRM. Once the
1908  *	Atomic Commit request call returns OUT_FENCE_PTR will be filled with
1909  *	the file descriptor number of a Sync File. This Sync File contains the
1910  *	CRTC fence that will be signaled when all framebuffers present on the
1911  *	Atomic Commit * request for that given CRTC are scanned out on the
1912  *	screen.
1913  *
1914  *	The Atomic Commit request fails if a invalid pointer is passed. If the
1915  *	Atomic Commit request fails for any other reason the out fence fd
1916  *	returned will be -1. On a Atomic Commit with the
1917  *	DRM_MODE_ATOMIC_TEST_ONLY flag the out fence will also be set to -1.
1918  *
1919  *	Note that out-fences don't have a special interface to drivers and are
1920  *	internally represented by a struct &drm_pending_vblank_event in struct
1921  *	&drm_crtc_state, which is also used by the nonblocking atomic commit
1922  *	helpers and for the DRM event handling for existing userspace.
1923  */
1924 
1925 struct drm_out_fence_state {
1926 	s64 __user *out_fence_ptr;
1927 	struct sync_file *sync_file;
1928 	int fd;
1929 };
1930 
1931 static int setup_out_fence(struct drm_out_fence_state *fence_state,
1932 			   struct dma_fence *fence)
1933 {
1934 	fence_state->fd = get_unused_fd_flags(O_CLOEXEC);
1935 	if (fence_state->fd < 0)
1936 		return fence_state->fd;
1937 
1938 	if (put_user(fence_state->fd, fence_state->out_fence_ptr))
1939 		return -EFAULT;
1940 
1941 	fence_state->sync_file = sync_file_create(fence);
1942 	if (!fence_state->sync_file)
1943 		return -ENOMEM;
1944 
1945 	return 0;
1946 }
1947 
1948 static int prepare_crtc_signaling(struct drm_device *dev,
1949 				  struct drm_atomic_state *state,
1950 				  struct drm_mode_atomic *arg,
1951 				  struct drm_file *file_priv,
1952 				  struct drm_out_fence_state **fence_state,
1953 				  unsigned int *num_fences)
1954 {
1955 	struct drm_crtc *crtc;
1956 	struct drm_crtc_state *crtc_state;
1957 	int i, ret;
1958 
1959 	if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)
1960 		return 0;
1961 
1962 	for_each_crtc_in_state(state, crtc, crtc_state, i) {
1963 		u64 __user *fence_ptr;
1964 
1965 		fence_ptr = get_out_fence_for_crtc(crtc_state->state, crtc);
1966 
1967 		if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT || fence_ptr) {
1968 			struct drm_pending_vblank_event *e;
1969 
1970 			e = create_vblank_event(dev, arg->user_data);
1971 			if (!e)
1972 				return -ENOMEM;
1973 
1974 			crtc_state->event = e;
1975 		}
1976 
1977 		if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
1978 			struct drm_pending_vblank_event *e = crtc_state->event;
1979 
1980 			if (!file_priv)
1981 				continue;
1982 
1983 			ret = drm_event_reserve_init(dev, file_priv, &e->base,
1984 						     &e->event.base);
1985 			if (ret) {
1986 				kfree(e);
1987 				crtc_state->event = NULL;
1988 				return ret;
1989 			}
1990 		}
1991 
1992 		if (fence_ptr) {
1993 			struct dma_fence *fence;
1994 			struct drm_out_fence_state *f;
1995 
1996 			f = krealloc(*fence_state, sizeof(**fence_state) *
1997 				     (*num_fences + 1), GFP_KERNEL);
1998 			if (!f)
1999 				return -ENOMEM;
2000 
2001 			memset(&f[*num_fences], 0, sizeof(*f));
2002 
2003 			f[*num_fences].out_fence_ptr = fence_ptr;
2004 			*fence_state = f;
2005 
2006 			fence = drm_crtc_create_fence(crtc);
2007 			if (!fence)
2008 				return -ENOMEM;
2009 
2010 			ret = setup_out_fence(&f[(*num_fences)++], fence);
2011 			if (ret) {
2012 				dma_fence_put(fence);
2013 				return ret;
2014 			}
2015 
2016 			crtc_state->event->base.fence = fence;
2017 		}
2018 	}
2019 
2020 	return 0;
2021 }
2022 
2023 static void complete_crtc_signaling(struct drm_device *dev,
2024 				    struct drm_atomic_state *state,
2025 				    struct drm_out_fence_state *fence_state,
2026 				    unsigned int num_fences,
2027 				    bool install_fds)
2028 {
2029 	struct drm_crtc *crtc;
2030 	struct drm_crtc_state *crtc_state;
2031 	int i;
2032 
2033 	if (install_fds) {
2034 		for (i = 0; i < num_fences; i++)
2035 			fd_install(fence_state[i].fd,
2036 				   fence_state[i].sync_file->file);
2037 
2038 		kfree(fence_state);
2039 		return;
2040 	}
2041 
2042 	for_each_crtc_in_state(state, crtc, crtc_state, i) {
2043 		/*
2044 		 * TEST_ONLY and PAGE_FLIP_EVENT are mutually
2045 		 * exclusive, if they weren't, this code should be
2046 		 * called on success for TEST_ONLY too.
2047 		 */
2048 		if (crtc_state->event)
2049 			drm_event_cancel_free(dev, &crtc_state->event->base);
2050 	}
2051 
2052 	if (!fence_state)
2053 		return;
2054 
2055 	for (i = 0; i < num_fences; i++) {
2056 		if (fence_state[i].sync_file)
2057 			fput(fence_state[i].sync_file->file);
2058 		if (fence_state[i].fd >= 0)
2059 			put_unused_fd(fence_state[i].fd);
2060 
2061 		/* If this fails log error to the user */
2062 		if (fence_state[i].out_fence_ptr &&
2063 		    put_user(-1, fence_state[i].out_fence_ptr))
2064 			DRM_DEBUG_ATOMIC("Couldn't clear out_fence_ptr\n");
2065 	}
2066 
2067 	kfree(fence_state);
2068 }
2069 
2070 int drm_mode_atomic_ioctl(struct drm_device *dev,
2071 			  void *data, struct drm_file *file_priv)
2072 {
2073 	struct drm_mode_atomic *arg = data;
2074 	uint32_t __user *objs_ptr = (uint32_t __user *)(unsigned long)(arg->objs_ptr);
2075 	uint32_t __user *count_props_ptr = (uint32_t __user *)(unsigned long)(arg->count_props_ptr);
2076 	uint32_t __user *props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
2077 	uint64_t __user *prop_values_ptr = (uint64_t __user *)(unsigned long)(arg->prop_values_ptr);
2078 	unsigned int copied_objs, copied_props;
2079 	struct drm_atomic_state *state;
2080 	struct drm_modeset_acquire_ctx ctx;
2081 	struct drm_plane *plane;
2082 	struct drm_out_fence_state *fence_state = NULL;
2083 	unsigned plane_mask;
2084 	int ret = 0;
2085 	unsigned int i, j, num_fences = 0;
2086 
2087 	/* disallow for drivers not supporting atomic: */
2088 	if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
2089 		return -EINVAL;
2090 
2091 	/* disallow for userspace that has not enabled atomic cap (even
2092 	 * though this may be a bit overkill, since legacy userspace
2093 	 * wouldn't know how to call this ioctl)
2094 	 */
2095 	if (!file_priv->atomic)
2096 		return -EINVAL;
2097 
2098 	if (arg->flags & ~DRM_MODE_ATOMIC_FLAGS)
2099 		return -EINVAL;
2100 
2101 	if (arg->reserved)
2102 		return -EINVAL;
2103 
2104 	if ((arg->flags & DRM_MODE_PAGE_FLIP_ASYNC) &&
2105 			!dev->mode_config.async_page_flip)
2106 		return -EINVAL;
2107 
2108 	/* can't test and expect an event at the same time. */
2109 	if ((arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) &&
2110 			(arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
2111 		return -EINVAL;
2112 
2113 	drm_modeset_acquire_init(&ctx, 0);
2114 
2115 	state = drm_atomic_state_alloc(dev);
2116 	if (!state)
2117 		return -ENOMEM;
2118 
2119 	state->acquire_ctx = &ctx;
2120 	state->allow_modeset = !!(arg->flags & DRM_MODE_ATOMIC_ALLOW_MODESET);
2121 
2122 retry:
2123 	plane_mask = 0;
2124 	copied_objs = 0;
2125 	copied_props = 0;
2126 
2127 	for (i = 0; i < arg->count_objs; i++) {
2128 		uint32_t obj_id, count_props;
2129 		struct drm_mode_object *obj;
2130 
2131 		if (get_user(obj_id, objs_ptr + copied_objs)) {
2132 			ret = -EFAULT;
2133 			goto out;
2134 		}
2135 
2136 		obj = drm_mode_object_find(dev, obj_id, DRM_MODE_OBJECT_ANY);
2137 		if (!obj) {
2138 			ret = -ENOENT;
2139 			goto out;
2140 		}
2141 
2142 		if (!obj->properties) {
2143 			drm_mode_object_unreference(obj);
2144 			ret = -ENOENT;
2145 			goto out;
2146 		}
2147 
2148 		if (get_user(count_props, count_props_ptr + copied_objs)) {
2149 			drm_mode_object_unreference(obj);
2150 			ret = -EFAULT;
2151 			goto out;
2152 		}
2153 
2154 		copied_objs++;
2155 
2156 		for (j = 0; j < count_props; j++) {
2157 			uint32_t prop_id;
2158 			uint64_t prop_value;
2159 			struct drm_property *prop;
2160 
2161 			if (get_user(prop_id, props_ptr + copied_props)) {
2162 				drm_mode_object_unreference(obj);
2163 				ret = -EFAULT;
2164 				goto out;
2165 			}
2166 
2167 			prop = drm_mode_obj_find_prop_id(obj, prop_id);
2168 			if (!prop) {
2169 				drm_mode_object_unreference(obj);
2170 				ret = -ENOENT;
2171 				goto out;
2172 			}
2173 
2174 			if (copy_from_user(&prop_value,
2175 					   prop_values_ptr + copied_props,
2176 					   sizeof(prop_value))) {
2177 				drm_mode_object_unreference(obj);
2178 				ret = -EFAULT;
2179 				goto out;
2180 			}
2181 
2182 			ret = atomic_set_prop(state, obj, prop, prop_value);
2183 			if (ret) {
2184 				drm_mode_object_unreference(obj);
2185 				goto out;
2186 			}
2187 
2188 			copied_props++;
2189 		}
2190 
2191 		if (obj->type == DRM_MODE_OBJECT_PLANE && count_props &&
2192 		    !(arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)) {
2193 			plane = obj_to_plane(obj);
2194 			plane_mask |= (1 << drm_plane_index(plane));
2195 			plane->old_fb = plane->fb;
2196 		}
2197 		drm_mode_object_unreference(obj);
2198 	}
2199 
2200 	ret = prepare_crtc_signaling(dev, state, arg, file_priv, &fence_state,
2201 				     &num_fences);
2202 	if (ret)
2203 		goto out;
2204 
2205 	if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) {
2206 		ret = drm_atomic_check_only(state);
2207 	} else if (arg->flags & DRM_MODE_ATOMIC_NONBLOCK) {
2208 		ret = drm_atomic_nonblocking_commit(state);
2209 	} else {
2210 		if (unlikely(drm_debug & DRM_UT_STATE))
2211 			drm_atomic_print_state(state);
2212 
2213 		ret = drm_atomic_commit(state);
2214 	}
2215 
2216 out:
2217 	drm_atomic_clean_old_fb(dev, plane_mask, ret);
2218 
2219 	complete_crtc_signaling(dev, state, fence_state, num_fences, !ret);
2220 
2221 	if (ret == -EDEADLK) {
2222 		drm_atomic_state_clear(state);
2223 		drm_modeset_backoff(&ctx);
2224 		goto retry;
2225 	}
2226 
2227 	drm_atomic_state_put(state);
2228 
2229 	drm_modeset_drop_locks(&ctx);
2230 	drm_modeset_acquire_fini(&ctx);
2231 
2232 	return ret;
2233 }
2234