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