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