xref: /dflybsd-src/sys/dev/drm/drm_atomic.c (revision 3f2dd94a569761201b5b0a18b2f697f97fe1b9dc)
12c9916cdSFrançois Tigeot /*
22c9916cdSFrançois Tigeot  * Copyright (C) 2014 Red Hat
32c9916cdSFrançois Tigeot  * Copyright (C) 2014 Intel Corp.
42c9916cdSFrançois Tigeot  *
52c9916cdSFrançois Tigeot  * Permission is hereby granted, free of charge, to any person obtaining a
62c9916cdSFrançois Tigeot  * copy of this software and associated documentation files (the "Software"),
72c9916cdSFrançois Tigeot  * to deal in the Software without restriction, including without limitation
82c9916cdSFrançois Tigeot  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
92c9916cdSFrançois Tigeot  * and/or sell copies of the Software, and to permit persons to whom the
102c9916cdSFrançois Tigeot  * Software is furnished to do so, subject to the following conditions:
112c9916cdSFrançois Tigeot  *
122c9916cdSFrançois Tigeot  * The above copyright notice and this permission notice shall be included in
132c9916cdSFrançois Tigeot  * all copies or substantial portions of the Software.
142c9916cdSFrançois Tigeot  *
152c9916cdSFrançois Tigeot  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
162c9916cdSFrançois Tigeot  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
172c9916cdSFrançois Tigeot  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
182c9916cdSFrançois Tigeot  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
192c9916cdSFrançois Tigeot  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
202c9916cdSFrançois Tigeot  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
212c9916cdSFrançois Tigeot  * OTHER DEALINGS IN THE SOFTWARE.
222c9916cdSFrançois Tigeot  *
232c9916cdSFrançois Tigeot  * Authors:
242c9916cdSFrançois Tigeot  * Rob Clark <robdclark@gmail.com>
252c9916cdSFrançois Tigeot  * Daniel Vetter <daniel.vetter@ffwll.ch>
262c9916cdSFrançois Tigeot  */
272c9916cdSFrançois Tigeot 
282c9916cdSFrançois Tigeot 
292c9916cdSFrançois Tigeot #include <drm/drmP.h>
302c9916cdSFrançois Tigeot #include <drm/drm_atomic.h>
3183b4b9b9SFrançois Tigeot #include <drm/drm_mode.h>
324be47400SFrançois Tigeot #include <drm/drm_print.h>
334be47400SFrançois Tigeot #include <linux/sync_file.h>
342c9916cdSFrançois Tigeot 
352c9916cdSFrançois Tigeot #include "drm_crtc_internal.h"
362c9916cdSFrançois Tigeot 
__drm_crtc_commit_free(struct kref * kref)37a85cb24fSFrançois Tigeot void __drm_crtc_commit_free(struct kref *kref)
381dedbd3bSFrançois Tigeot {
391dedbd3bSFrançois Tigeot 	struct drm_crtc_commit *commit =
401dedbd3bSFrançois Tigeot 		container_of(kref, struct drm_crtc_commit, ref);
411dedbd3bSFrançois Tigeot 
421dedbd3bSFrançois Tigeot 	kfree(commit);
431dedbd3bSFrançois Tigeot }
44a85cb24fSFrançois Tigeot EXPORT_SYMBOL(__drm_crtc_commit_free);
451dedbd3bSFrançois Tigeot 
4619c468b4SFrançois Tigeot /**
4719c468b4SFrançois Tigeot  * drm_atomic_state_default_release -
4819c468b4SFrançois Tigeot  * release memory initialized by drm_atomic_state_init
4919c468b4SFrançois Tigeot  * @state: atomic state
5019c468b4SFrançois Tigeot  *
5119c468b4SFrançois Tigeot  * Free all the memory allocated by drm_atomic_state_init.
5219c468b4SFrançois Tigeot  * This is useful for drivers that subclass the atomic state.
5319c468b4SFrançois Tigeot  */
drm_atomic_state_default_release(struct drm_atomic_state * state)5419c468b4SFrançois Tigeot void drm_atomic_state_default_release(struct drm_atomic_state *state)
552c9916cdSFrançois Tigeot {
562c9916cdSFrançois Tigeot 	kfree(state->connectors);
572c9916cdSFrançois Tigeot 	kfree(state->crtcs);
582c9916cdSFrançois Tigeot 	kfree(state->planes);
59*3f2dd94aSFrançois Tigeot 	kfree(state->private_objs);
602c9916cdSFrançois Tigeot }
6119c468b4SFrançois Tigeot EXPORT_SYMBOL(drm_atomic_state_default_release);
622c9916cdSFrançois Tigeot 
632c9916cdSFrançois Tigeot /**
6419c468b4SFrançois Tigeot  * drm_atomic_state_init - init new atomic state
652c9916cdSFrançois Tigeot  * @dev: DRM device
6619c468b4SFrançois Tigeot  * @state: atomic state
672c9916cdSFrançois Tigeot  *
6819c468b4SFrançois Tigeot  * Default implementation for filling in a new atomic state.
6919c468b4SFrançois Tigeot  * This is useful for drivers that subclass the atomic state.
702c9916cdSFrançois Tigeot  */
7119c468b4SFrançois Tigeot int
drm_atomic_state_init(struct drm_device * dev,struct drm_atomic_state * state)7219c468b4SFrançois Tigeot drm_atomic_state_init(struct drm_device *dev, struct drm_atomic_state *state)
732c9916cdSFrançois Tigeot {
744be47400SFrançois Tigeot 	kref_init(&state->ref);
754be47400SFrançois Tigeot 
762c9916cdSFrançois Tigeot 	/* TODO legacy paths should maybe do a better job about
772c9916cdSFrançois Tigeot 	 * setting this appropriately?
782c9916cdSFrançois Tigeot 	 */
792c9916cdSFrançois Tigeot 	state->allow_modeset = true;
802c9916cdSFrançois Tigeot 
812c9916cdSFrançois Tigeot 	state->crtcs = kcalloc(dev->mode_config.num_crtc,
822c9916cdSFrançois Tigeot 			       sizeof(*state->crtcs), GFP_KERNEL);
832c9916cdSFrançois Tigeot 	if (!state->crtcs)
842c9916cdSFrançois Tigeot 		goto fail;
852c9916cdSFrançois Tigeot 	state->planes = kcalloc(dev->mode_config.num_total_plane,
862c9916cdSFrançois Tigeot 				sizeof(*state->planes), GFP_KERNEL);
872c9916cdSFrançois Tigeot 	if (!state->planes)
882c9916cdSFrançois Tigeot 		goto fail;
892c9916cdSFrançois Tigeot 
902c9916cdSFrançois Tigeot 	state->dev = dev;
912c9916cdSFrançois Tigeot 
9219c468b4SFrançois Tigeot 	DRM_DEBUG_ATOMIC("Allocated atomic state %p\n", state);
932c9916cdSFrançois Tigeot 
9419c468b4SFrançois Tigeot 	return 0;
952c9916cdSFrançois Tigeot fail:
9619c468b4SFrançois Tigeot 	drm_atomic_state_default_release(state);
9719c468b4SFrançois Tigeot 	return -ENOMEM;
9819c468b4SFrançois Tigeot }
9919c468b4SFrançois Tigeot EXPORT_SYMBOL(drm_atomic_state_init);
1002c9916cdSFrançois Tigeot 
10119c468b4SFrançois Tigeot /**
10219c468b4SFrançois Tigeot  * drm_atomic_state_alloc - allocate atomic state
10319c468b4SFrançois Tigeot  * @dev: DRM device
10419c468b4SFrançois Tigeot  *
10519c468b4SFrançois Tigeot  * This allocates an empty atomic state to track updates.
10619c468b4SFrançois Tigeot  */
10719c468b4SFrançois Tigeot struct drm_atomic_state *
drm_atomic_state_alloc(struct drm_device * dev)10819c468b4SFrançois Tigeot drm_atomic_state_alloc(struct drm_device *dev)
10919c468b4SFrançois Tigeot {
11019c468b4SFrançois Tigeot 	struct drm_mode_config *config = &dev->mode_config;
11119c468b4SFrançois Tigeot 
11219c468b4SFrançois Tigeot 	if (!config->funcs->atomic_state_alloc) {
113*3f2dd94aSFrançois Tigeot 		struct drm_atomic_state *state;
114*3f2dd94aSFrançois Tigeot 
11519c468b4SFrançois Tigeot 		state = kzalloc(sizeof(*state), GFP_KERNEL);
11619c468b4SFrançois Tigeot 		if (!state)
1172c9916cdSFrançois Tigeot 			return NULL;
11819c468b4SFrançois Tigeot 		if (drm_atomic_state_init(dev, state) < 0) {
11919c468b4SFrançois Tigeot 			kfree(state);
12019c468b4SFrançois Tigeot 			return NULL;
12119c468b4SFrançois Tigeot 		}
12219c468b4SFrançois Tigeot 		return state;
12319c468b4SFrançois Tigeot 	}
12419c468b4SFrançois Tigeot 
12519c468b4SFrançois Tigeot 	return config->funcs->atomic_state_alloc(dev);
1262c9916cdSFrançois Tigeot }
1272c9916cdSFrançois Tigeot EXPORT_SYMBOL(drm_atomic_state_alloc);
1282c9916cdSFrançois Tigeot 
1292c9916cdSFrançois Tigeot /**
13019c468b4SFrançois Tigeot  * drm_atomic_state_default_clear - clear base atomic state
1312c9916cdSFrançois Tigeot  * @state: atomic state
1322c9916cdSFrançois Tigeot  *
13319c468b4SFrançois Tigeot  * Default implementation for clearing atomic state.
13419c468b4SFrançois Tigeot  * This is useful for drivers that subclass the atomic state.
1352c9916cdSFrançois Tigeot  */
drm_atomic_state_default_clear(struct drm_atomic_state * state)13619c468b4SFrançois Tigeot void drm_atomic_state_default_clear(struct drm_atomic_state *state)
1372c9916cdSFrançois Tigeot {
1382c9916cdSFrançois Tigeot 	struct drm_device *dev = state->dev;
1392c9916cdSFrançois Tigeot 	struct drm_mode_config *config = &dev->mode_config;
1402c9916cdSFrançois Tigeot 	int i;
1412c9916cdSFrançois Tigeot 
142477eb7f9SFrançois Tigeot 	DRM_DEBUG_ATOMIC("Clearing atomic state %p\n", state);
1432c9916cdSFrançois Tigeot 
1442c9916cdSFrançois Tigeot 	for (i = 0; i < state->num_connector; i++) {
1451dedbd3bSFrançois Tigeot 		struct drm_connector *connector = state->connectors[i].ptr;
1462c9916cdSFrançois Tigeot 
1472c9916cdSFrançois Tigeot 		if (!connector)
1482c9916cdSFrançois Tigeot 			continue;
1492c9916cdSFrançois Tigeot 
1508621f407SFrançois Tigeot 		connector->funcs->atomic_destroy_state(connector,
1511dedbd3bSFrançois Tigeot 						       state->connectors[i].state);
1521dedbd3bSFrançois Tigeot 		state->connectors[i].ptr = NULL;
1531dedbd3bSFrançois Tigeot 		state->connectors[i].state = NULL;
154a85cb24fSFrançois Tigeot 		drm_connector_put(connector);
1552c9916cdSFrançois Tigeot 	}
1562c9916cdSFrançois Tigeot 
1572c9916cdSFrançois Tigeot 	for (i = 0; i < config->num_crtc; i++) {
1581dedbd3bSFrançois Tigeot 		struct drm_crtc *crtc = state->crtcs[i].ptr;
1592c9916cdSFrançois Tigeot 
1602c9916cdSFrançois Tigeot 		if (!crtc)
1612c9916cdSFrançois Tigeot 			continue;
1622c9916cdSFrançois Tigeot 
1632c9916cdSFrançois Tigeot 		crtc->funcs->atomic_destroy_state(crtc,
1641dedbd3bSFrançois Tigeot 						  state->crtcs[i].state);
1651dedbd3bSFrançois Tigeot 
1661dedbd3bSFrançois Tigeot 		state->crtcs[i].ptr = NULL;
1671dedbd3bSFrançois Tigeot 		state->crtcs[i].state = NULL;
1682c9916cdSFrançois Tigeot 	}
1692c9916cdSFrançois Tigeot 
1702c9916cdSFrançois Tigeot 	for (i = 0; i < config->num_total_plane; i++) {
1711dedbd3bSFrançois Tigeot 		struct drm_plane *plane = state->planes[i].ptr;
1722c9916cdSFrançois Tigeot 
1732c9916cdSFrançois Tigeot 		if (!plane)
1742c9916cdSFrançois Tigeot 			continue;
1752c9916cdSFrançois Tigeot 
1762c9916cdSFrançois Tigeot 		plane->funcs->atomic_destroy_state(plane,
1771dedbd3bSFrançois Tigeot 						   state->planes[i].state);
1781dedbd3bSFrançois Tigeot 		state->planes[i].ptr = NULL;
1791dedbd3bSFrançois Tigeot 		state->planes[i].state = NULL;
1802c9916cdSFrançois Tigeot 	}
181*3f2dd94aSFrançois Tigeot 
182*3f2dd94aSFrançois Tigeot 	for (i = 0; i < state->num_private_objs; i++) {
183*3f2dd94aSFrançois Tigeot 		struct drm_private_obj *obj = state->private_objs[i].ptr;
184*3f2dd94aSFrançois Tigeot 
185*3f2dd94aSFrançois Tigeot 		obj->funcs->atomic_destroy_state(obj,
186*3f2dd94aSFrançois Tigeot 						 state->private_objs[i].state);
187*3f2dd94aSFrançois Tigeot 		state->private_objs[i].ptr = NULL;
188*3f2dd94aSFrançois Tigeot 		state->private_objs[i].state = NULL;
189*3f2dd94aSFrançois Tigeot 	}
190*3f2dd94aSFrançois Tigeot 	state->num_private_objs = 0;
191*3f2dd94aSFrançois Tigeot 
192*3f2dd94aSFrançois Tigeot 	if (state->fake_commit) {
193*3f2dd94aSFrançois Tigeot 		drm_crtc_commit_put(state->fake_commit);
194*3f2dd94aSFrançois Tigeot 		state->fake_commit = NULL;
195*3f2dd94aSFrançois Tigeot 	}
1962c9916cdSFrançois Tigeot }
19719c468b4SFrançois Tigeot EXPORT_SYMBOL(drm_atomic_state_default_clear);
19819c468b4SFrançois Tigeot 
19919c468b4SFrançois Tigeot /**
20019c468b4SFrançois Tigeot  * drm_atomic_state_clear - clear state object
20119c468b4SFrançois Tigeot  * @state: atomic state
20219c468b4SFrançois Tigeot  *
20319c468b4SFrançois Tigeot  * When the w/w mutex algorithm detects a deadlock we need to back off and drop
20419c468b4SFrançois Tigeot  * all locks. So someone else could sneak in and change the current modeset
20519c468b4SFrançois Tigeot  * configuration. Which means that all the state assembled in @state is no
20619c468b4SFrançois Tigeot  * longer an atomic update to the current state, but to some arbitrary earlier
207a85cb24fSFrançois Tigeot  * state. Which could break assumptions the driver's
208a85cb24fSFrançois Tigeot  * &drm_mode_config_funcs.atomic_check likely relies on.
20919c468b4SFrançois Tigeot  *
21019c468b4SFrançois Tigeot  * Hence we must clear all cached state and completely start over, using this
21119c468b4SFrançois Tigeot  * function.
21219c468b4SFrançois Tigeot  */
drm_atomic_state_clear(struct drm_atomic_state * state)21319c468b4SFrançois Tigeot void drm_atomic_state_clear(struct drm_atomic_state *state)
21419c468b4SFrançois Tigeot {
21519c468b4SFrançois Tigeot 	struct drm_device *dev = state->dev;
21619c468b4SFrançois Tigeot 	struct drm_mode_config *config = &dev->mode_config;
21719c468b4SFrançois Tigeot 
21819c468b4SFrançois Tigeot 	if (config->funcs->atomic_state_clear)
21919c468b4SFrançois Tigeot 		config->funcs->atomic_state_clear(state);
22019c468b4SFrançois Tigeot 	else
22119c468b4SFrançois Tigeot 		drm_atomic_state_default_clear(state);
22219c468b4SFrançois Tigeot }
2232c9916cdSFrançois Tigeot EXPORT_SYMBOL(drm_atomic_state_clear);
2242c9916cdSFrançois Tigeot 
2252c9916cdSFrançois Tigeot /**
2264be47400SFrançois Tigeot  * __drm_atomic_state_free - free all memory for an atomic state
2274be47400SFrançois Tigeot  * @ref: This atomic state to deallocate
2282c9916cdSFrançois Tigeot  *
2292c9916cdSFrançois Tigeot  * This frees all memory associated with an atomic state, including all the
2302c9916cdSFrançois Tigeot  * per-object state for planes, crtcs and connectors.
2312c9916cdSFrançois Tigeot  */
__drm_atomic_state_free(struct kref * ref)2324be47400SFrançois Tigeot void __drm_atomic_state_free(struct kref *ref)
2332c9916cdSFrançois Tigeot {
2344be47400SFrançois Tigeot 	struct drm_atomic_state *state = container_of(ref, typeof(*state), ref);
2354be47400SFrançois Tigeot 	struct drm_mode_config *config = &state->dev->mode_config;
23619c468b4SFrançois Tigeot 
2372c9916cdSFrançois Tigeot 	drm_atomic_state_clear(state);
2382c9916cdSFrançois Tigeot 
239477eb7f9SFrançois Tigeot 	DRM_DEBUG_ATOMIC("Freeing atomic state %p\n", state);
2402c9916cdSFrançois Tigeot 
24119c468b4SFrançois Tigeot 	if (config->funcs->atomic_state_free) {
24219c468b4SFrançois Tigeot 		config->funcs->atomic_state_free(state);
24319c468b4SFrançois Tigeot 	} else {
24419c468b4SFrançois Tigeot 		drm_atomic_state_default_release(state);
24519c468b4SFrançois Tigeot 		kfree(state);
24619c468b4SFrançois Tigeot 	}
2472c9916cdSFrançois Tigeot }
2484be47400SFrançois Tigeot EXPORT_SYMBOL(__drm_atomic_state_free);
2492c9916cdSFrançois Tigeot 
2502c9916cdSFrançois Tigeot /**
2512c9916cdSFrançois Tigeot  * drm_atomic_get_crtc_state - get crtc state
2522c9916cdSFrançois Tigeot  * @state: global atomic state object
2532c9916cdSFrançois Tigeot  * @crtc: crtc to get state object for
2542c9916cdSFrançois Tigeot  *
2552c9916cdSFrançois Tigeot  * This function returns the crtc state for the given crtc, allocating it if
2562c9916cdSFrançois Tigeot  * needed. It will also grab the relevant crtc lock to make sure that the state
2572c9916cdSFrançois Tigeot  * is consistent.
2582c9916cdSFrançois Tigeot  *
2592c9916cdSFrançois Tigeot  * Returns:
2602c9916cdSFrançois Tigeot  *
2612c9916cdSFrançois Tigeot  * Either the allocated state or the error code encoded into the pointer. When
2622c9916cdSFrançois Tigeot  * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
2632c9916cdSFrançois Tigeot  * entire atomic sequence must be restarted. All other errors are fatal.
2642c9916cdSFrançois Tigeot  */
2652c9916cdSFrançois Tigeot struct drm_crtc_state *
drm_atomic_get_crtc_state(struct drm_atomic_state * state,struct drm_crtc * crtc)2662c9916cdSFrançois Tigeot drm_atomic_get_crtc_state(struct drm_atomic_state *state,
2672c9916cdSFrançois Tigeot 			  struct drm_crtc *crtc)
2682c9916cdSFrançois Tigeot {
26919c468b4SFrançois Tigeot 	int ret, index = drm_crtc_index(crtc);
2702c9916cdSFrançois Tigeot 	struct drm_crtc_state *crtc_state;
2712c9916cdSFrançois Tigeot 
2728621f407SFrançois Tigeot 	WARN_ON(!state->acquire_ctx);
2738621f407SFrançois Tigeot 
27419c468b4SFrançois Tigeot 	crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
27519c468b4SFrançois Tigeot 	if (crtc_state)
27619c468b4SFrançois Tigeot 		return crtc_state;
2772c9916cdSFrançois Tigeot 
2782c9916cdSFrançois Tigeot 	ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx);
2792c9916cdSFrançois Tigeot 	if (ret)
2802c9916cdSFrançois Tigeot 		return ERR_PTR(ret);
2812c9916cdSFrançois Tigeot 
2822c9916cdSFrançois Tigeot 	crtc_state = crtc->funcs->atomic_duplicate_state(crtc);
2832c9916cdSFrançois Tigeot 	if (!crtc_state)
2842c9916cdSFrançois Tigeot 		return ERR_PTR(-ENOMEM);
2852c9916cdSFrançois Tigeot 
2861dedbd3bSFrançois Tigeot 	state->crtcs[index].state = crtc_state;
287a85cb24fSFrançois Tigeot 	state->crtcs[index].old_state = crtc->state;
288a85cb24fSFrançois Tigeot 	state->crtcs[index].new_state = crtc_state;
2891dedbd3bSFrançois Tigeot 	state->crtcs[index].ptr = crtc;
2902c9916cdSFrançois Tigeot 	crtc_state->state = state;
2912c9916cdSFrançois Tigeot 
292aee94f86SFrançois Tigeot 	DRM_DEBUG_ATOMIC("Added [CRTC:%d:%s] %p state to %p\n",
293aee94f86SFrançois Tigeot 			 crtc->base.id, crtc->name, crtc_state, state);
2942c9916cdSFrançois Tigeot 
2952c9916cdSFrançois Tigeot 	return crtc_state;
2962c9916cdSFrançois Tigeot }
2972c9916cdSFrançois Tigeot EXPORT_SYMBOL(drm_atomic_get_crtc_state);
2982c9916cdSFrançois Tigeot 
set_out_fence_for_crtc(struct drm_atomic_state * state,struct drm_crtc * crtc,s32 __user * fence_ptr)2994be47400SFrançois Tigeot static void set_out_fence_for_crtc(struct drm_atomic_state *state,
3004be47400SFrançois Tigeot 				   struct drm_crtc *crtc, s32 __user *fence_ptr)
3014be47400SFrançois Tigeot {
3024be47400SFrançois Tigeot 	state->crtcs[drm_crtc_index(crtc)].out_fence_ptr = fence_ptr;
3034be47400SFrançois Tigeot }
3044be47400SFrançois Tigeot 
get_out_fence_for_crtc(struct drm_atomic_state * state,struct drm_crtc * crtc)3054be47400SFrançois Tigeot static s32 __user *get_out_fence_for_crtc(struct drm_atomic_state *state,
3064be47400SFrançois Tigeot 					  struct drm_crtc *crtc)
3074be47400SFrançois Tigeot {
3084be47400SFrançois Tigeot 	s32 __user *fence_ptr;
3094be47400SFrançois Tigeot 
3104be47400SFrançois Tigeot 	fence_ptr = state->crtcs[drm_crtc_index(crtc)].out_fence_ptr;
3114be47400SFrançois Tigeot 	state->crtcs[drm_crtc_index(crtc)].out_fence_ptr = NULL;
3124be47400SFrançois Tigeot 
3134be47400SFrançois Tigeot 	return fence_ptr;
3144be47400SFrançois Tigeot }
3154be47400SFrançois Tigeot 
3162c9916cdSFrançois Tigeot /**
31719c468b4SFrançois Tigeot  * drm_atomic_set_mode_for_crtc - set mode for CRTC
31819c468b4SFrançois Tigeot  * @state: the CRTC whose incoming state to update
31919c468b4SFrançois Tigeot  * @mode: kernel-internal mode to use for the CRTC, or NULL to disable
32019c468b4SFrançois Tigeot  *
321a85cb24fSFrançois Tigeot  * Set a mode (originating from the kernel) on the desired CRTC state and update
322a85cb24fSFrançois Tigeot  * the enable property.
32319c468b4SFrançois Tigeot  *
32419c468b4SFrançois Tigeot  * RETURNS:
32519c468b4SFrançois Tigeot  * Zero on success, error code on failure. Cannot return -EDEADLK.
32619c468b4SFrançois Tigeot  */
drm_atomic_set_mode_for_crtc(struct drm_crtc_state * state,const struct drm_display_mode * mode)32719c468b4SFrançois Tigeot int drm_atomic_set_mode_for_crtc(struct drm_crtc_state *state,
328*3f2dd94aSFrançois Tigeot 				 const struct drm_display_mode *mode)
32919c468b4SFrançois Tigeot {
33019c468b4SFrançois Tigeot 	struct drm_mode_modeinfo umode;
33119c468b4SFrançois Tigeot 
33219c468b4SFrançois Tigeot 	/* Early return for no change. */
33319c468b4SFrançois Tigeot 	if (mode && memcmp(&state->mode, mode, sizeof(*mode)) == 0)
33419c468b4SFrançois Tigeot 		return 0;
33519c468b4SFrançois Tigeot 
336a85cb24fSFrançois Tigeot 	drm_property_blob_put(state->mode_blob);
33719c468b4SFrançois Tigeot 	state->mode_blob = NULL;
33819c468b4SFrançois Tigeot 
33919c468b4SFrançois Tigeot 	if (mode) {
34019c468b4SFrançois Tigeot 		drm_mode_convert_to_umode(&umode, mode);
34119c468b4SFrançois Tigeot 		state->mode_blob =
34219c468b4SFrançois Tigeot 			drm_property_create_blob(state->crtc->dev,
34319c468b4SFrançois Tigeot 		                                 sizeof(umode),
34419c468b4SFrançois Tigeot 		                                 &umode);
34519c468b4SFrançois Tigeot 		if (IS_ERR(state->mode_blob))
34619c468b4SFrançois Tigeot 			return PTR_ERR(state->mode_blob);
34719c468b4SFrançois Tigeot 
34819c468b4SFrançois Tigeot 		drm_mode_copy(&state->mode, mode);
34919c468b4SFrançois Tigeot 		state->enable = true;
35019c468b4SFrançois Tigeot 		DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n",
35119c468b4SFrançois Tigeot 				 mode->name, state);
35219c468b4SFrançois Tigeot 	} else {
35319c468b4SFrançois Tigeot 		memset(&state->mode, 0, sizeof(state->mode));
35419c468b4SFrançois Tigeot 		state->enable = false;
35519c468b4SFrançois Tigeot 		DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n",
35619c468b4SFrançois Tigeot 				 state);
35719c468b4SFrançois Tigeot 	}
35819c468b4SFrançois Tigeot 
35919c468b4SFrançois Tigeot 	return 0;
36019c468b4SFrançois Tigeot }
36119c468b4SFrançois Tigeot EXPORT_SYMBOL(drm_atomic_set_mode_for_crtc);
36219c468b4SFrançois Tigeot 
36319c468b4SFrançois Tigeot /**
36419c468b4SFrançois Tigeot  * drm_atomic_set_mode_prop_for_crtc - set mode for CRTC
36519c468b4SFrançois Tigeot  * @state: the CRTC whose incoming state to update
36619c468b4SFrançois Tigeot  * @blob: pointer to blob property to use for mode
36719c468b4SFrançois Tigeot  *
36819c468b4SFrançois Tigeot  * Set a mode (originating from a blob property) on the desired CRTC state.
36919c468b4SFrançois Tigeot  * This function will take a reference on the blob property for the CRTC state,
37019c468b4SFrançois Tigeot  * and release the reference held on the state's existing mode property, if any
37119c468b4SFrançois Tigeot  * was set.
37219c468b4SFrançois Tigeot  *
37319c468b4SFrançois Tigeot  * RETURNS:
37419c468b4SFrançois Tigeot  * Zero on success, error code on failure. Cannot return -EDEADLK.
37519c468b4SFrançois Tigeot  */
drm_atomic_set_mode_prop_for_crtc(struct drm_crtc_state * state,struct drm_property_blob * blob)37619c468b4SFrançois Tigeot int drm_atomic_set_mode_prop_for_crtc(struct drm_crtc_state *state,
37719c468b4SFrançois Tigeot                                       struct drm_property_blob *blob)
37819c468b4SFrançois Tigeot {
37919c468b4SFrançois Tigeot 	if (blob == state->mode_blob)
38019c468b4SFrançois Tigeot 		return 0;
38119c468b4SFrançois Tigeot 
382a85cb24fSFrançois Tigeot 	drm_property_blob_put(state->mode_blob);
38319c468b4SFrançois Tigeot 	state->mode_blob = NULL;
38419c468b4SFrançois Tigeot 
3858621f407SFrançois Tigeot 	memset(&state->mode, 0, sizeof(state->mode));
3868621f407SFrançois Tigeot 
38719c468b4SFrançois Tigeot 	if (blob) {
38819c468b4SFrançois Tigeot 		if (blob->length != sizeof(struct drm_mode_modeinfo) ||
38919c468b4SFrançois Tigeot 		    drm_mode_convert_umode(&state->mode,
39019c468b4SFrançois Tigeot 		                           (const struct drm_mode_modeinfo *)
39119c468b4SFrançois Tigeot 		                            blob->data))
39219c468b4SFrançois Tigeot 			return -EINVAL;
39319c468b4SFrançois Tigeot 
394a85cb24fSFrançois Tigeot 		state->mode_blob = drm_property_blob_get(blob);
39519c468b4SFrançois Tigeot 		state->enable = true;
39619c468b4SFrançois Tigeot 		DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n",
39719c468b4SFrançois Tigeot 				 state->mode.name, state);
39819c468b4SFrançois Tigeot 	} else {
39919c468b4SFrançois Tigeot 		state->enable = false;
40019c468b4SFrançois Tigeot 		DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n",
40119c468b4SFrançois Tigeot 				 state);
40219c468b4SFrançois Tigeot 	}
40319c468b4SFrançois Tigeot 
40419c468b4SFrançois Tigeot 	return 0;
40519c468b4SFrançois Tigeot }
40619c468b4SFrançois Tigeot EXPORT_SYMBOL(drm_atomic_set_mode_prop_for_crtc);
40719c468b4SFrançois Tigeot 
408c0e85e96SFrançois Tigeot static int
drm_atomic_replace_property_blob_from_id(struct drm_device * dev,struct drm_property_blob ** blob,uint64_t blob_id,ssize_t expected_size,bool * replaced)409*3f2dd94aSFrançois Tigeot drm_atomic_replace_property_blob_from_id(struct drm_device *dev,
410c0e85e96SFrançois Tigeot 					 struct drm_property_blob **blob,
411c0e85e96SFrançois Tigeot 					 uint64_t blob_id,
412c0e85e96SFrançois Tigeot 					 ssize_t expected_size,
413c0e85e96SFrançois Tigeot 					 bool *replaced)
414c0e85e96SFrançois Tigeot {
415c0e85e96SFrançois Tigeot 	struct drm_property_blob *new_blob = NULL;
416c0e85e96SFrançois Tigeot 
417c0e85e96SFrançois Tigeot 	if (blob_id != 0) {
418*3f2dd94aSFrançois Tigeot 		new_blob = drm_property_lookup_blob(dev, blob_id);
419c0e85e96SFrançois Tigeot 		if (new_blob == NULL)
420c0e85e96SFrançois Tigeot 			return -EINVAL;
4214be47400SFrançois Tigeot 
4224be47400SFrançois Tigeot 		if (expected_size > 0 && expected_size != new_blob->length) {
423a85cb24fSFrançois Tigeot 			drm_property_blob_put(new_blob);
424c0e85e96SFrançois Tigeot 			return -EINVAL;
425c0e85e96SFrançois Tigeot 		}
4264be47400SFrançois Tigeot 	}
427c0e85e96SFrançois Tigeot 
428*3f2dd94aSFrançois Tigeot 	*replaced |= drm_property_replace_blob(blob, new_blob);
429a85cb24fSFrançois Tigeot 	drm_property_blob_put(new_blob);
430c0e85e96SFrançois Tigeot 
431c0e85e96SFrançois Tigeot 	return 0;
432c0e85e96SFrançois Tigeot }
433c0e85e96SFrançois Tigeot 
434c0e85e96SFrançois Tigeot /**
4352c9916cdSFrançois Tigeot  * drm_atomic_crtc_set_property - set property on CRTC
4362c9916cdSFrançois Tigeot  * @crtc: the drm CRTC to set a property on
4372c9916cdSFrançois Tigeot  * @state: the state object to update with the new property value
4382c9916cdSFrançois Tigeot  * @property: the property to set
4392c9916cdSFrançois Tigeot  * @val: the new property value
4402c9916cdSFrançois Tigeot  *
441a85cb24fSFrançois Tigeot  * This function handles generic/core properties and calls out to driver's
442a85cb24fSFrançois Tigeot  * &drm_crtc_funcs.atomic_set_property for driver properties. To ensure
443a85cb24fSFrançois Tigeot  * consistent behavior you must call this function rather than the driver hook
444a85cb24fSFrançois Tigeot  * directly.
4452c9916cdSFrançois Tigeot  *
4462c9916cdSFrançois Tigeot  * RETURNS:
4472c9916cdSFrançois Tigeot  * Zero on success, error code on failure
4482c9916cdSFrançois Tigeot  */
drm_atomic_crtc_set_property(struct drm_crtc * crtc,struct drm_crtc_state * state,struct drm_property * property,uint64_t val)4492c9916cdSFrançois Tigeot int drm_atomic_crtc_set_property(struct drm_crtc *crtc,
4502c9916cdSFrançois Tigeot 		struct drm_crtc_state *state, struct drm_property *property,
4512c9916cdSFrançois Tigeot 		uint64_t val)
4522c9916cdSFrançois Tigeot {
4532c9916cdSFrançois Tigeot 	struct drm_device *dev = crtc->dev;
4542c9916cdSFrançois Tigeot 	struct drm_mode_config *config = &dev->mode_config;
455c0e85e96SFrançois Tigeot 	bool replaced = false;
45619c468b4SFrançois Tigeot 	int ret;
4572c9916cdSFrançois Tigeot 
458477eb7f9SFrançois Tigeot 	if (property == config->prop_active)
4592c9916cdSFrançois Tigeot 		state->active = val;
46019c468b4SFrançois Tigeot 	else if (property == config->prop_mode_id) {
46119c468b4SFrançois Tigeot 		struct drm_property_blob *mode =
46219c468b4SFrançois Tigeot 			drm_property_lookup_blob(dev, val);
46319c468b4SFrançois Tigeot 		ret = drm_atomic_set_mode_prop_for_crtc(state, mode);
464a85cb24fSFrançois Tigeot 		drm_property_blob_put(mode);
46519c468b4SFrançois Tigeot 		return ret;
466c0e85e96SFrançois Tigeot 	} else if (property == config->degamma_lut_property) {
467*3f2dd94aSFrançois Tigeot 		ret = drm_atomic_replace_property_blob_from_id(dev,
468c0e85e96SFrançois Tigeot 					&state->degamma_lut,
469c0e85e96SFrançois Tigeot 					val,
470c0e85e96SFrançois Tigeot 					-1,
471c0e85e96SFrançois Tigeot 					&replaced);
4728621f407SFrançois Tigeot 		state->color_mgmt_changed |= replaced;
473c0e85e96SFrançois Tigeot 		return ret;
474c0e85e96SFrançois Tigeot 	} else if (property == config->ctm_property) {
475*3f2dd94aSFrançois Tigeot 		ret = drm_atomic_replace_property_blob_from_id(dev,
476c0e85e96SFrançois Tigeot 					&state->ctm,
477c0e85e96SFrançois Tigeot 					val,
478c0e85e96SFrançois Tigeot 					sizeof(struct drm_color_ctm),
479c0e85e96SFrançois Tigeot 					&replaced);
4808621f407SFrançois Tigeot 		state->color_mgmt_changed |= replaced;
481c0e85e96SFrançois Tigeot 		return ret;
482c0e85e96SFrançois Tigeot 	} else if (property == config->gamma_lut_property) {
483*3f2dd94aSFrançois Tigeot 		ret = drm_atomic_replace_property_blob_from_id(dev,
484c0e85e96SFrançois Tigeot 					&state->gamma_lut,
485c0e85e96SFrançois Tigeot 					val,
486c0e85e96SFrançois Tigeot 					-1,
487c0e85e96SFrançois Tigeot 					&replaced);
4888621f407SFrançois Tigeot 		state->color_mgmt_changed |= replaced;
489c0e85e96SFrançois Tigeot 		return ret;
4904be47400SFrançois Tigeot 	} else if (property == config->prop_out_fence_ptr) {
4914be47400SFrançois Tigeot 		s32 __user *fence_ptr = u64_to_user_ptr(val);
4924be47400SFrançois Tigeot 
4934be47400SFrançois Tigeot 		if (!fence_ptr)
4944be47400SFrançois Tigeot 			return 0;
4954be47400SFrançois Tigeot 
4964be47400SFrançois Tigeot 		if (put_user(-1, fence_ptr))
4974be47400SFrançois Tigeot 			return -EFAULT;
4984be47400SFrançois Tigeot 
4994be47400SFrançois Tigeot 		set_out_fence_for_crtc(state->state, crtc, fence_ptr);
500c0e85e96SFrançois Tigeot 	} else if (crtc->funcs->atomic_set_property)
5012c9916cdSFrançois Tigeot 		return crtc->funcs->atomic_set_property(crtc, state, property, val);
502477eb7f9SFrançois Tigeot 	else
5032c9916cdSFrançois Tigeot 		return -EINVAL;
504477eb7f9SFrançois Tigeot 
505477eb7f9SFrançois Tigeot 	return 0;
5062c9916cdSFrançois Tigeot }
5072c9916cdSFrançois Tigeot EXPORT_SYMBOL(drm_atomic_crtc_set_property);
5082c9916cdSFrançois Tigeot 
509aee94f86SFrançois Tigeot /**
510aee94f86SFrançois Tigeot  * drm_atomic_crtc_get_property - get property value from CRTC state
511aee94f86SFrançois Tigeot  * @crtc: the drm CRTC to set a property on
512aee94f86SFrançois Tigeot  * @state: the state object to get the property value from
513aee94f86SFrançois Tigeot  * @property: the property to set
514aee94f86SFrançois Tigeot  * @val: return location for the property value
515aee94f86SFrançois Tigeot  *
516a85cb24fSFrançois Tigeot  * This function handles generic/core properties and calls out to driver's
517a85cb24fSFrançois Tigeot  * &drm_crtc_funcs.atomic_get_property for driver properties. To ensure
518a85cb24fSFrançois Tigeot  * consistent behavior you must call this function rather than the driver hook
519a85cb24fSFrançois Tigeot  * directly.
520aee94f86SFrançois Tigeot  *
521aee94f86SFrançois Tigeot  * RETURNS:
522aee94f86SFrançois Tigeot  * Zero on success, error code on failure
5232c9916cdSFrançois Tigeot  */
524352ff8bdSFrançois Tigeot static int
drm_atomic_crtc_get_property(struct drm_crtc * crtc,const struct drm_crtc_state * state,struct drm_property * property,uint64_t * val)525352ff8bdSFrançois Tigeot drm_atomic_crtc_get_property(struct drm_crtc *crtc,
5262c9916cdSFrançois Tigeot 		const struct drm_crtc_state *state,
5272c9916cdSFrançois Tigeot 		struct drm_property *property, uint64_t *val)
5282c9916cdSFrançois Tigeot {
529477eb7f9SFrançois Tigeot 	struct drm_device *dev = crtc->dev;
530477eb7f9SFrançois Tigeot 	struct drm_mode_config *config = &dev->mode_config;
531477eb7f9SFrançois Tigeot 
532477eb7f9SFrançois Tigeot 	if (property == config->prop_active)
533477eb7f9SFrançois Tigeot 		*val = state->active;
53419c468b4SFrançois Tigeot 	else if (property == config->prop_mode_id)
53519c468b4SFrançois Tigeot 		*val = (state->mode_blob) ? state->mode_blob->base.id : 0;
536c0e85e96SFrançois Tigeot 	else if (property == config->degamma_lut_property)
537c0e85e96SFrançois Tigeot 		*val = (state->degamma_lut) ? state->degamma_lut->base.id : 0;
538c0e85e96SFrançois Tigeot 	else if (property == config->ctm_property)
539c0e85e96SFrançois Tigeot 		*val = (state->ctm) ? state->ctm->base.id : 0;
540c0e85e96SFrançois Tigeot 	else if (property == config->gamma_lut_property)
541c0e85e96SFrançois Tigeot 		*val = (state->gamma_lut) ? state->gamma_lut->base.id : 0;
5424be47400SFrançois Tigeot 	else if (property == config->prop_out_fence_ptr)
5434be47400SFrançois Tigeot 		*val = 0;
544477eb7f9SFrançois Tigeot 	else if (crtc->funcs->atomic_get_property)
5452c9916cdSFrançois Tigeot 		return crtc->funcs->atomic_get_property(crtc, state, property, val);
546477eb7f9SFrançois Tigeot 	else
5472c9916cdSFrançois Tigeot 		return -EINVAL;
548477eb7f9SFrançois Tigeot 
549477eb7f9SFrançois Tigeot 	return 0;
5502c9916cdSFrançois Tigeot }
5512c9916cdSFrançois Tigeot 
5522c9916cdSFrançois Tigeot /**
5532c9916cdSFrançois Tigeot  * drm_atomic_crtc_check - check crtc state
5542c9916cdSFrançois Tigeot  * @crtc: crtc to check
5552c9916cdSFrançois Tigeot  * @state: crtc state to check
5562c9916cdSFrançois Tigeot  *
5572c9916cdSFrançois Tigeot  * Provides core sanity checks for crtc state.
5582c9916cdSFrançois Tigeot  *
5592c9916cdSFrançois Tigeot  * RETURNS:
5602c9916cdSFrançois Tigeot  * Zero on success, error code on failure
5612c9916cdSFrançois Tigeot  */
drm_atomic_crtc_check(struct drm_crtc * crtc,struct drm_crtc_state * state)5622c9916cdSFrançois Tigeot static int drm_atomic_crtc_check(struct drm_crtc *crtc,
5632c9916cdSFrançois Tigeot 		struct drm_crtc_state *state)
5642c9916cdSFrançois Tigeot {
5652c9916cdSFrançois Tigeot 	/* NOTE: we explicitly don't enforce constraints such as primary
5662c9916cdSFrançois Tigeot 	 * layer covering entire screen, since that is something we want
5672c9916cdSFrançois Tigeot 	 * to allow (on hw that supports it).  For hw that does not, it
5682c9916cdSFrançois Tigeot 	 * should be checked in driver's crtc->atomic_check() vfunc.
5692c9916cdSFrançois Tigeot 	 *
5702c9916cdSFrançois Tigeot 	 * TODO: Add generic modeset state checks once we support those.
5712c9916cdSFrançois Tigeot 	 */
5722c9916cdSFrançois Tigeot 
5732c9916cdSFrançois Tigeot 	if (state->active && !state->enable) {
574aee94f86SFrançois Tigeot 		DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active without enabled\n",
575aee94f86SFrançois Tigeot 				 crtc->base.id, crtc->name);
5762c9916cdSFrançois Tigeot 		return -EINVAL;
5772c9916cdSFrançois Tigeot 	}
5782c9916cdSFrançois Tigeot 
57919c468b4SFrançois Tigeot 	/* The state->enable vs. state->mode_blob checks can be WARN_ON,
58019c468b4SFrançois Tigeot 	 * as this is a kernel-internal detail that userspace should never
58119c468b4SFrançois Tigeot 	 * be able to trigger. */
58219c468b4SFrançois Tigeot 	if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
58319c468b4SFrançois Tigeot 	    WARN_ON(state->enable && !state->mode_blob)) {
584aee94f86SFrançois Tigeot 		DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled without mode blob\n",
585aee94f86SFrançois Tigeot 				 crtc->base.id, crtc->name);
58619c468b4SFrançois Tigeot 		return -EINVAL;
58719c468b4SFrançois Tigeot 	}
58819c468b4SFrançois Tigeot 
58919c468b4SFrançois Tigeot 	if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
59019c468b4SFrançois Tigeot 	    WARN_ON(!state->enable && state->mode_blob)) {
591aee94f86SFrançois Tigeot 		DRM_DEBUG_ATOMIC("[CRTC:%d:%s] disabled with mode blob\n",
592aee94f86SFrançois Tigeot 				 crtc->base.id, crtc->name);
593aee94f86SFrançois Tigeot 		return -EINVAL;
594aee94f86SFrançois Tigeot 	}
595aee94f86SFrançois Tigeot 
596aee94f86SFrançois Tigeot 	/*
597aee94f86SFrançois Tigeot 	 * Reject event generation for when a CRTC is off and stays off.
598aee94f86SFrançois Tigeot 	 * It wouldn't be hard to implement this, but userspace has a track
599aee94f86SFrançois Tigeot 	 * record of happily burning through 100% cpu (or worse, crash) when the
600aee94f86SFrançois Tigeot 	 * display pipe is suspended. To avoid all that fun just reject updates
601aee94f86SFrançois Tigeot 	 * that ask for events since likely that indicates a bug in the
602aee94f86SFrançois Tigeot 	 * compositor's drawing loop. This is consistent with the vblank IOCTL
603aee94f86SFrançois Tigeot 	 * and legacy page_flip IOCTL which also reject service on a disabled
604aee94f86SFrançois Tigeot 	 * pipe.
605aee94f86SFrançois Tigeot 	 */
606aee94f86SFrançois Tigeot 	if (state->event && !state->active && !crtc->state->active) {
607a85cb24fSFrançois Tigeot 		DRM_DEBUG_ATOMIC("[CRTC:%d:%s] requesting event but off\n",
608a85cb24fSFrançois Tigeot 				 crtc->base.id, crtc->name);
60919c468b4SFrançois Tigeot 		return -EINVAL;
61019c468b4SFrançois Tigeot 	}
61119c468b4SFrançois Tigeot 
6122c9916cdSFrançois Tigeot 	return 0;
6132c9916cdSFrançois Tigeot }
6142c9916cdSFrançois Tigeot 
drm_atomic_crtc_print_state(struct drm_printer * p,const struct drm_crtc_state * state)6154be47400SFrançois Tigeot static void drm_atomic_crtc_print_state(struct drm_printer *p,
6164be47400SFrançois Tigeot 		const struct drm_crtc_state *state)
6174be47400SFrançois Tigeot {
6184be47400SFrançois Tigeot 	struct drm_crtc *crtc = state->crtc;
6194be47400SFrançois Tigeot 
6204be47400SFrançois Tigeot 	drm_printf(p, "crtc[%u]: %s\n", crtc->base.id, crtc->name);
6214be47400SFrançois Tigeot 	drm_printf(p, "\tenable=%d\n", state->enable);
6224be47400SFrançois Tigeot 	drm_printf(p, "\tactive=%d\n", state->active);
6234be47400SFrançois Tigeot 	drm_printf(p, "\tplanes_changed=%d\n", state->planes_changed);
6244be47400SFrançois Tigeot 	drm_printf(p, "\tmode_changed=%d\n", state->mode_changed);
6254be47400SFrançois Tigeot 	drm_printf(p, "\tactive_changed=%d\n", state->active_changed);
6264be47400SFrançois Tigeot 	drm_printf(p, "\tconnectors_changed=%d\n", state->connectors_changed);
6274be47400SFrançois Tigeot 	drm_printf(p, "\tcolor_mgmt_changed=%d\n", state->color_mgmt_changed);
6284be47400SFrançois Tigeot 	drm_printf(p, "\tplane_mask=%x\n", state->plane_mask);
6294be47400SFrançois Tigeot 	drm_printf(p, "\tconnector_mask=%x\n", state->connector_mask);
6304be47400SFrançois Tigeot 	drm_printf(p, "\tencoder_mask=%x\n", state->encoder_mask);
6314be47400SFrançois Tigeot 	drm_printf(p, "\tmode: " DRM_MODE_FMT "\n", DRM_MODE_ARG(&state->mode));
6324be47400SFrançois Tigeot 
6334be47400SFrançois Tigeot 	if (crtc->funcs->atomic_print_state)
6344be47400SFrançois Tigeot 		crtc->funcs->atomic_print_state(p, state);
6354be47400SFrançois Tigeot }
6364be47400SFrançois Tigeot 
6372c9916cdSFrançois Tigeot /**
6382c9916cdSFrançois Tigeot  * drm_atomic_get_plane_state - get plane state
6392c9916cdSFrançois Tigeot  * @state: global atomic state object
6402c9916cdSFrançois Tigeot  * @plane: plane to get state object for
6412c9916cdSFrançois Tigeot  *
6422c9916cdSFrançois Tigeot  * This function returns the plane state for the given plane, allocating it if
6432c9916cdSFrançois Tigeot  * needed. It will also grab the relevant plane lock to make sure that the state
6442c9916cdSFrançois Tigeot  * is consistent.
6452c9916cdSFrançois Tigeot  *
6462c9916cdSFrançois Tigeot  * Returns:
6472c9916cdSFrançois Tigeot  *
6482c9916cdSFrançois Tigeot  * Either the allocated state or the error code encoded into the pointer. When
6492c9916cdSFrançois Tigeot  * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
6502c9916cdSFrançois Tigeot  * entire atomic sequence must be restarted. All other errors are fatal.
6512c9916cdSFrançois Tigeot  */
6522c9916cdSFrançois Tigeot struct drm_plane_state *
drm_atomic_get_plane_state(struct drm_atomic_state * state,struct drm_plane * plane)6532c9916cdSFrançois Tigeot drm_atomic_get_plane_state(struct drm_atomic_state *state,
6542c9916cdSFrançois Tigeot 			  struct drm_plane *plane)
6552c9916cdSFrançois Tigeot {
65619c468b4SFrançois Tigeot 	int ret, index = drm_plane_index(plane);
6572c9916cdSFrançois Tigeot 	struct drm_plane_state *plane_state;
6582c9916cdSFrançois Tigeot 
6598621f407SFrançois Tigeot 	WARN_ON(!state->acquire_ctx);
6608621f407SFrançois Tigeot 
66119c468b4SFrançois Tigeot 	plane_state = drm_atomic_get_existing_plane_state(state, plane);
66219c468b4SFrançois Tigeot 	if (plane_state)
66319c468b4SFrançois Tigeot 		return plane_state;
6642c9916cdSFrançois Tigeot 
6652c9916cdSFrançois Tigeot 	ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
6662c9916cdSFrançois Tigeot 	if (ret)
6672c9916cdSFrançois Tigeot 		return ERR_PTR(ret);
6682c9916cdSFrançois Tigeot 
6692c9916cdSFrançois Tigeot 	plane_state = plane->funcs->atomic_duplicate_state(plane);
6702c9916cdSFrançois Tigeot 	if (!plane_state)
6712c9916cdSFrançois Tigeot 		return ERR_PTR(-ENOMEM);
6722c9916cdSFrançois Tigeot 
6731dedbd3bSFrançois Tigeot 	state->planes[index].state = plane_state;
6741dedbd3bSFrançois Tigeot 	state->planes[index].ptr = plane;
675a85cb24fSFrançois Tigeot 	state->planes[index].old_state = plane->state;
676a85cb24fSFrançois Tigeot 	state->planes[index].new_state = plane_state;
6772c9916cdSFrançois Tigeot 	plane_state->state = state;
6782c9916cdSFrançois Tigeot 
679aee94f86SFrançois Tigeot 	DRM_DEBUG_ATOMIC("Added [PLANE:%d:%s] %p state to %p\n",
680aee94f86SFrançois Tigeot 			 plane->base.id, plane->name, plane_state, state);
6812c9916cdSFrançois Tigeot 
6822c9916cdSFrançois Tigeot 	if (plane_state->crtc) {
6832c9916cdSFrançois Tigeot 		struct drm_crtc_state *crtc_state;
6842c9916cdSFrançois Tigeot 
6852c9916cdSFrançois Tigeot 		crtc_state = drm_atomic_get_crtc_state(state,
6862c9916cdSFrançois Tigeot 						       plane_state->crtc);
6872c9916cdSFrançois Tigeot 		if (IS_ERR(crtc_state))
6882c9916cdSFrançois Tigeot 			return ERR_CAST(crtc_state);
6892c9916cdSFrançois Tigeot 	}
6902c9916cdSFrançois Tigeot 
6912c9916cdSFrançois Tigeot 	return plane_state;
6922c9916cdSFrançois Tigeot }
6932c9916cdSFrançois Tigeot EXPORT_SYMBOL(drm_atomic_get_plane_state);
6942c9916cdSFrançois Tigeot 
6952c9916cdSFrançois Tigeot /**
6962c9916cdSFrançois Tigeot  * drm_atomic_plane_set_property - set property on plane
6972c9916cdSFrançois Tigeot  * @plane: the drm plane to set a property on
6982c9916cdSFrançois Tigeot  * @state: the state object to update with the new property value
6992c9916cdSFrançois Tigeot  * @property: the property to set
7002c9916cdSFrançois Tigeot  * @val: the new property value
7012c9916cdSFrançois Tigeot  *
702a85cb24fSFrançois Tigeot  * This function handles generic/core properties and calls out to driver's
703a85cb24fSFrançois Tigeot  * &drm_plane_funcs.atomic_set_property for driver properties.  To ensure
704a85cb24fSFrançois Tigeot  * consistent behavior you must call this function rather than the driver hook
705a85cb24fSFrançois Tigeot  * directly.
7062c9916cdSFrançois Tigeot  *
7072c9916cdSFrançois Tigeot  * RETURNS:
7082c9916cdSFrançois Tigeot  * Zero on success, error code on failure
7092c9916cdSFrançois Tigeot  */
drm_atomic_plane_set_property(struct drm_plane * plane,struct drm_plane_state * state,struct drm_property * property,uint64_t val)710*3f2dd94aSFrançois Tigeot static int drm_atomic_plane_set_property(struct drm_plane *plane,
7112c9916cdSFrançois Tigeot 		struct drm_plane_state *state, struct drm_property *property,
7122c9916cdSFrançois Tigeot 		uint64_t val)
7132c9916cdSFrançois Tigeot {
7142c9916cdSFrançois Tigeot 	struct drm_device *dev = plane->dev;
7152c9916cdSFrançois Tigeot 	struct drm_mode_config *config = &dev->mode_config;
7162c9916cdSFrançois Tigeot 
7172c9916cdSFrançois Tigeot 	if (property == config->prop_fb_id) {
718*3f2dd94aSFrançois Tigeot 		struct drm_framebuffer *fb = drm_framebuffer_lookup(dev, NULL, val);
7192c9916cdSFrançois Tigeot 		drm_atomic_set_fb_for_plane(state, fb);
7202c9916cdSFrançois Tigeot 		if (fb)
721a85cb24fSFrançois Tigeot 			drm_framebuffer_put(fb);
7224be47400SFrançois Tigeot 	} else if (property == config->prop_in_fence_fd) {
7234be47400SFrançois Tigeot 		if (state->fence)
7244be47400SFrançois Tigeot 			return -EINVAL;
7254be47400SFrançois Tigeot 
7264be47400SFrançois Tigeot 		if (U642I64(val) == -1)
7274be47400SFrançois Tigeot 			return 0;
7284be47400SFrançois Tigeot 
7294be47400SFrançois Tigeot 		state->fence = sync_file_get_fence(val);
7304be47400SFrançois Tigeot 		if (!state->fence)
7314be47400SFrançois Tigeot 			return -EINVAL;
7324be47400SFrançois Tigeot 
7332c9916cdSFrançois Tigeot 	} else if (property == config->prop_crtc_id) {
734*3f2dd94aSFrançois Tigeot 		struct drm_crtc *crtc = drm_crtc_find(dev, NULL, val);
7352c9916cdSFrançois Tigeot 		return drm_atomic_set_crtc_for_plane(state, crtc);
7362c9916cdSFrançois Tigeot 	} else if (property == config->prop_crtc_x) {
7372c9916cdSFrançois Tigeot 		state->crtc_x = U642I64(val);
7382c9916cdSFrançois Tigeot 	} else if (property == config->prop_crtc_y) {
7392c9916cdSFrançois Tigeot 		state->crtc_y = U642I64(val);
7402c9916cdSFrançois Tigeot 	} else if (property == config->prop_crtc_w) {
7412c9916cdSFrançois Tigeot 		state->crtc_w = val;
7422c9916cdSFrançois Tigeot 	} else if (property == config->prop_crtc_h) {
7432c9916cdSFrançois Tigeot 		state->crtc_h = val;
7442c9916cdSFrançois Tigeot 	} else if (property == config->prop_src_x) {
7452c9916cdSFrançois Tigeot 		state->src_x = val;
7462c9916cdSFrançois Tigeot 	} else if (property == config->prop_src_y) {
7472c9916cdSFrançois Tigeot 		state->src_y = val;
7482c9916cdSFrançois Tigeot 	} else if (property == config->prop_src_w) {
7492c9916cdSFrançois Tigeot 		state->src_w = val;
7502c9916cdSFrançois Tigeot 	} else if (property == config->prop_src_h) {
7512c9916cdSFrançois Tigeot 		state->src_h = val;
7524be47400SFrançois Tigeot 	} else if (property == plane->rotation_property) {
753*3f2dd94aSFrançois Tigeot 		if (!is_power_of_2(val & DRM_MODE_ROTATE_MASK))
7544be47400SFrançois Tigeot 			return -EINVAL;
7552c9916cdSFrançois Tigeot 		state->rotation = val;
7561dedbd3bSFrançois Tigeot 	} else if (property == plane->zpos_property) {
7571dedbd3bSFrançois Tigeot 		state->zpos = val;
7582c9916cdSFrançois Tigeot 	} else if (plane->funcs->atomic_set_property) {
7592c9916cdSFrançois Tigeot 		return plane->funcs->atomic_set_property(plane, state,
7602c9916cdSFrançois Tigeot 				property, val);
7612c9916cdSFrançois Tigeot 	} else {
7622c9916cdSFrançois Tigeot 		return -EINVAL;
7632c9916cdSFrançois Tigeot 	}
7642c9916cdSFrançois Tigeot 
7652c9916cdSFrançois Tigeot 	return 0;
7662c9916cdSFrançois Tigeot }
7672c9916cdSFrançois Tigeot 
768aee94f86SFrançois Tigeot /**
769aee94f86SFrançois Tigeot  * drm_atomic_plane_get_property - get property value from plane state
770aee94f86SFrançois Tigeot  * @plane: the drm plane to set a property on
771aee94f86SFrançois Tigeot  * @state: the state object to get the property value from
772aee94f86SFrançois Tigeot  * @property: the property to set
773aee94f86SFrançois Tigeot  * @val: return location for the property value
774aee94f86SFrançois Tigeot  *
775a85cb24fSFrançois Tigeot  * This function handles generic/core properties and calls out to driver's
776a85cb24fSFrançois Tigeot  * &drm_plane_funcs.atomic_get_property for driver properties.  To ensure
777a85cb24fSFrançois Tigeot  * consistent behavior you must call this function rather than the driver hook
778a85cb24fSFrançois Tigeot  * directly.
779aee94f86SFrançois Tigeot  *
780aee94f86SFrançois Tigeot  * RETURNS:
781aee94f86SFrançois Tigeot  * Zero on success, error code on failure
7822c9916cdSFrançois Tigeot  */
7832c9916cdSFrançois Tigeot static int
drm_atomic_plane_get_property(struct drm_plane * plane,const struct drm_plane_state * state,struct drm_property * property,uint64_t * val)7842c9916cdSFrançois Tigeot drm_atomic_plane_get_property(struct drm_plane *plane,
7852c9916cdSFrançois Tigeot 		const struct drm_plane_state *state,
7862c9916cdSFrançois Tigeot 		struct drm_property *property, uint64_t *val)
7872c9916cdSFrançois Tigeot {
7882c9916cdSFrançois Tigeot 	struct drm_device *dev = plane->dev;
7892c9916cdSFrançois Tigeot 	struct drm_mode_config *config = &dev->mode_config;
7902c9916cdSFrançois Tigeot 
7912c9916cdSFrançois Tigeot 	if (property == config->prop_fb_id) {
7922c9916cdSFrançois Tigeot 		*val = (state->fb) ? state->fb->base.id : 0;
7934be47400SFrançois Tigeot 	} else if (property == config->prop_in_fence_fd) {
7944be47400SFrançois Tigeot 		*val = -1;
7952c9916cdSFrançois Tigeot 	} else if (property == config->prop_crtc_id) {
7962c9916cdSFrançois Tigeot 		*val = (state->crtc) ? state->crtc->base.id : 0;
7972c9916cdSFrançois Tigeot 	} else if (property == config->prop_crtc_x) {
7982c9916cdSFrançois Tigeot 		*val = I642U64(state->crtc_x);
7992c9916cdSFrançois Tigeot 	} else if (property == config->prop_crtc_y) {
8002c9916cdSFrançois Tigeot 		*val = I642U64(state->crtc_y);
8012c9916cdSFrançois Tigeot 	} else if (property == config->prop_crtc_w) {
8022c9916cdSFrançois Tigeot 		*val = state->crtc_w;
8032c9916cdSFrançois Tigeot 	} else if (property == config->prop_crtc_h) {
8042c9916cdSFrançois Tigeot 		*val = state->crtc_h;
8052c9916cdSFrançois Tigeot 	} else if (property == config->prop_src_x) {
8062c9916cdSFrançois Tigeot 		*val = state->src_x;
8072c9916cdSFrançois Tigeot 	} else if (property == config->prop_src_y) {
8082c9916cdSFrançois Tigeot 		*val = state->src_y;
8092c9916cdSFrançois Tigeot 	} else if (property == config->prop_src_w) {
8102c9916cdSFrançois Tigeot 		*val = state->src_w;
8112c9916cdSFrançois Tigeot 	} else if (property == config->prop_src_h) {
8122c9916cdSFrançois Tigeot 		*val = state->src_h;
8134be47400SFrançois Tigeot 	} else if (property == plane->rotation_property) {
814477eb7f9SFrançois Tigeot 		*val = state->rotation;
8151dedbd3bSFrançois Tigeot 	} else if (property == plane->zpos_property) {
8161dedbd3bSFrançois Tigeot 		*val = state->zpos;
8172c9916cdSFrançois Tigeot 	} else if (plane->funcs->atomic_get_property) {
8182c9916cdSFrançois Tigeot 		return plane->funcs->atomic_get_property(plane, state, property, val);
8192c9916cdSFrançois Tigeot 	} else {
8202c9916cdSFrançois Tigeot 		return -EINVAL;
8212c9916cdSFrançois Tigeot 	}
8222c9916cdSFrançois Tigeot 
8232c9916cdSFrançois Tigeot 	return 0;
8242c9916cdSFrançois Tigeot }
8252c9916cdSFrançois Tigeot 
826352ff8bdSFrançois Tigeot static bool
plane_switching_crtc(struct drm_atomic_state * state,struct drm_plane * plane,struct drm_plane_state * plane_state)827352ff8bdSFrançois Tigeot plane_switching_crtc(struct drm_atomic_state *state,
828352ff8bdSFrançois Tigeot 		     struct drm_plane *plane,
829352ff8bdSFrançois Tigeot 		     struct drm_plane_state *plane_state)
830352ff8bdSFrançois Tigeot {
831352ff8bdSFrançois Tigeot 	if (!plane->state->crtc || !plane_state->crtc)
832352ff8bdSFrançois Tigeot 		return false;
833352ff8bdSFrançois Tigeot 
834352ff8bdSFrançois Tigeot 	if (plane->state->crtc == plane_state->crtc)
835352ff8bdSFrançois Tigeot 		return false;
836352ff8bdSFrançois Tigeot 
837352ff8bdSFrançois Tigeot 	/* This could be refined, but currently there's no helper or driver code
838352ff8bdSFrançois Tigeot 	 * to implement direct switching of active planes nor userspace to take
839352ff8bdSFrançois Tigeot 	 * advantage of more direct plane switching without the intermediate
840352ff8bdSFrançois Tigeot 	 * full OFF state.
841352ff8bdSFrançois Tigeot 	 */
842352ff8bdSFrançois Tigeot 	return true;
843352ff8bdSFrançois Tigeot }
844352ff8bdSFrançois Tigeot 
8452c9916cdSFrançois Tigeot /**
8462c9916cdSFrançois Tigeot  * drm_atomic_plane_check - check plane state
8472c9916cdSFrançois Tigeot  * @plane: plane to check
8482c9916cdSFrançois Tigeot  * @state: plane state to check
8492c9916cdSFrançois Tigeot  *
8502c9916cdSFrançois Tigeot  * Provides core sanity checks for plane state.
8512c9916cdSFrançois Tigeot  *
8522c9916cdSFrançois Tigeot  * RETURNS:
8532c9916cdSFrançois Tigeot  * Zero on success, error code on failure
8542c9916cdSFrançois Tigeot  */
drm_atomic_plane_check(struct drm_plane * plane,struct drm_plane_state * state)8552c9916cdSFrançois Tigeot static int drm_atomic_plane_check(struct drm_plane *plane,
8562c9916cdSFrançois Tigeot 		struct drm_plane_state *state)
8572c9916cdSFrançois Tigeot {
8582c9916cdSFrançois Tigeot 	unsigned int fb_width, fb_height;
859477eb7f9SFrançois Tigeot 	int ret;
8602c9916cdSFrançois Tigeot 
8612c9916cdSFrançois Tigeot 	/* either *both* CRTC and FB must be set, or neither */
8622c9916cdSFrançois Tigeot 	if (WARN_ON(state->crtc && !state->fb)) {
863477eb7f9SFrançois Tigeot 		DRM_DEBUG_ATOMIC("CRTC set but no FB\n");
8642c9916cdSFrançois Tigeot 		return -EINVAL;
8652c9916cdSFrançois Tigeot 	} else if (WARN_ON(state->fb && !state->crtc)) {
866477eb7f9SFrançois Tigeot 		DRM_DEBUG_ATOMIC("FB set but no CRTC\n");
8672c9916cdSFrançois Tigeot 		return -EINVAL;
8682c9916cdSFrançois Tigeot 	}
8692c9916cdSFrançois Tigeot 
8702c9916cdSFrançois Tigeot 	/* if disabled, we don't care about the rest of the state: */
8712c9916cdSFrançois Tigeot 	if (!state->crtc)
8722c9916cdSFrançois Tigeot 		return 0;
8732c9916cdSFrançois Tigeot 
8742c9916cdSFrançois Tigeot 	/* Check whether this plane is usable on this CRTC */
8752c9916cdSFrançois Tigeot 	if (!(plane->possible_crtcs & drm_crtc_mask(state->crtc))) {
876477eb7f9SFrançois Tigeot 		DRM_DEBUG_ATOMIC("Invalid crtc for plane\n");
8772c9916cdSFrançois Tigeot 		return -EINVAL;
8782c9916cdSFrançois Tigeot 	}
8792c9916cdSFrançois Tigeot 
8802c9916cdSFrançois Tigeot 	/* Check whether this plane supports the fb pixel format. */
881a85cb24fSFrançois Tigeot 	ret = drm_plane_check_pixel_format(plane, state->fb->format->format);
882477eb7f9SFrançois Tigeot 	if (ret) {
8834be47400SFrançois Tigeot 		struct drm_format_name_buf format_name;
8844be47400SFrançois Tigeot 		DRM_DEBUG_ATOMIC("Invalid pixel format %s\n",
885a85cb24fSFrançois Tigeot 		                 drm_get_format_name(state->fb->format->format,
8864be47400SFrançois Tigeot 		                                     &format_name));
887477eb7f9SFrançois Tigeot 		return ret;
8882c9916cdSFrançois Tigeot 	}
8892c9916cdSFrançois Tigeot 
8902c9916cdSFrançois Tigeot 	/* Give drivers some help against integer overflows */
8912c9916cdSFrançois Tigeot 	if (state->crtc_w > INT_MAX ||
8922c9916cdSFrançois Tigeot 	    state->crtc_x > INT_MAX - (int32_t) state->crtc_w ||
8932c9916cdSFrançois Tigeot 	    state->crtc_h > INT_MAX ||
8942c9916cdSFrançois Tigeot 	    state->crtc_y > INT_MAX - (int32_t) state->crtc_h) {
895477eb7f9SFrançois Tigeot 		DRM_DEBUG_ATOMIC("Invalid CRTC coordinates %ux%u+%d+%d\n",
8962c9916cdSFrançois Tigeot 				 state->crtc_w, state->crtc_h,
8972c9916cdSFrançois Tigeot 				 state->crtc_x, state->crtc_y);
8982c9916cdSFrançois Tigeot 		return -ERANGE;
8992c9916cdSFrançois Tigeot 	}
9002c9916cdSFrançois Tigeot 
9012c9916cdSFrançois Tigeot 	fb_width = state->fb->width << 16;
9022c9916cdSFrançois Tigeot 	fb_height = state->fb->height << 16;
9032c9916cdSFrançois Tigeot 
9042c9916cdSFrançois Tigeot 	/* Make sure source coordinates are inside the fb. */
9052c9916cdSFrançois Tigeot 	if (state->src_w > fb_width ||
9062c9916cdSFrançois Tigeot 	    state->src_x > fb_width - state->src_w ||
9072c9916cdSFrançois Tigeot 	    state->src_h > fb_height ||
9082c9916cdSFrançois Tigeot 	    state->src_y > fb_height - state->src_h) {
909477eb7f9SFrançois Tigeot 		DRM_DEBUG_ATOMIC("Invalid source coordinates "
9102c9916cdSFrançois Tigeot 				 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
9112c9916cdSFrançois Tigeot 				 state->src_w >> 16, ((state->src_w & 0xffff) * 15625) >> 10,
9122c9916cdSFrançois Tigeot 				 state->src_h >> 16, ((state->src_h & 0xffff) * 15625) >> 10,
9132c9916cdSFrançois Tigeot 				 state->src_x >> 16, ((state->src_x & 0xffff) * 15625) >> 10,
9142c9916cdSFrançois Tigeot 				 state->src_y >> 16, ((state->src_y & 0xffff) * 15625) >> 10);
9152c9916cdSFrançois Tigeot 		return -ENOSPC;
9162c9916cdSFrançois Tigeot 	}
9172c9916cdSFrançois Tigeot 
918352ff8bdSFrançois Tigeot 	if (plane_switching_crtc(state->state, plane, state)) {
919aee94f86SFrançois Tigeot 		DRM_DEBUG_ATOMIC("[PLANE:%d:%s] switching CRTC directly\n",
920aee94f86SFrançois Tigeot 				 plane->base.id, plane->name);
921352ff8bdSFrançois Tigeot 		return -EINVAL;
922352ff8bdSFrançois Tigeot 	}
923352ff8bdSFrançois Tigeot 
9242c9916cdSFrançois Tigeot 	return 0;
9252c9916cdSFrançois Tigeot }
9262c9916cdSFrançois Tigeot 
drm_atomic_plane_print_state(struct drm_printer * p,const struct drm_plane_state * state)9274be47400SFrançois Tigeot static void drm_atomic_plane_print_state(struct drm_printer *p,
9284be47400SFrançois Tigeot 		const struct drm_plane_state *state)
9294be47400SFrançois Tigeot {
9304be47400SFrançois Tigeot 	struct drm_plane *plane = state->plane;
9314be47400SFrançois Tigeot 	struct drm_rect src  = drm_plane_state_src(state);
9324be47400SFrançois Tigeot 	struct drm_rect dest = drm_plane_state_dest(state);
9334be47400SFrançois Tigeot 
9344be47400SFrançois Tigeot 	drm_printf(p, "plane[%u]: %s\n", plane->base.id, plane->name);
9354be47400SFrançois Tigeot 	drm_printf(p, "\tcrtc=%s\n", state->crtc ? state->crtc->name : "(null)");
9364be47400SFrançois Tigeot 	drm_printf(p, "\tfb=%u\n", state->fb ? state->fb->base.id : 0);
9374be47400SFrançois Tigeot 	if (state->fb) {
9384be47400SFrançois Tigeot 		struct drm_framebuffer *fb = state->fb;
939a85cb24fSFrançois Tigeot 		int i, n = fb->format->num_planes;
9404be47400SFrançois Tigeot 		struct drm_format_name_buf format_name;
9414be47400SFrançois Tigeot 
9424be47400SFrançois Tigeot 		drm_printf(p, "\t\tformat=%s\n",
943a85cb24fSFrançois Tigeot 		              drm_get_format_name(fb->format->format, &format_name));
944a85cb24fSFrançois Tigeot 		drm_printf(p, "\t\t\tmodifier=0x%lx\n", fb->modifier);
9454be47400SFrançois Tigeot 		drm_printf(p, "\t\tsize=%dx%d\n", fb->width, fb->height);
9464be47400SFrançois Tigeot 		drm_printf(p, "\t\tlayers:\n");
9474be47400SFrançois Tigeot 		for (i = 0; i < n; i++) {
9484be47400SFrançois Tigeot 			drm_printf(p, "\t\t\tpitch[%d]=%u\n", i, fb->pitches[i]);
9494be47400SFrançois Tigeot 			drm_printf(p, "\t\t\toffset[%d]=%u\n", i, fb->offsets[i]);
9504be47400SFrançois Tigeot 		}
9514be47400SFrançois Tigeot 	}
9524be47400SFrançois Tigeot 	drm_printf(p, "\tcrtc-pos=" DRM_RECT_FMT "\n", DRM_RECT_ARG(&dest));
9534be47400SFrançois Tigeot 	drm_printf(p, "\tsrc-pos=" DRM_RECT_FP_FMT "\n", DRM_RECT_FP_ARG(&src));
9544be47400SFrançois Tigeot 	drm_printf(p, "\trotation=%x\n", state->rotation);
9554be47400SFrançois Tigeot 
9564be47400SFrançois Tigeot 	if (plane->funcs->atomic_print_state)
9574be47400SFrançois Tigeot 		plane->funcs->atomic_print_state(p, state);
9584be47400SFrançois Tigeot }
9594be47400SFrançois Tigeot 
9602c9916cdSFrançois Tigeot /**
961*3f2dd94aSFrançois Tigeot  * drm_atomic_private_obj_init - initialize private object
962*3f2dd94aSFrançois Tigeot  * @obj: private object
963*3f2dd94aSFrançois Tigeot  * @state: initial private object state
964*3f2dd94aSFrançois Tigeot  * @funcs: pointer to the struct of function pointers that identify the object
965*3f2dd94aSFrançois Tigeot  * type
966*3f2dd94aSFrançois Tigeot  *
967*3f2dd94aSFrançois Tigeot  * Initialize the private object, which can be embedded into any
968*3f2dd94aSFrançois Tigeot  * driver private object that needs its own atomic state.
969*3f2dd94aSFrançois Tigeot  */
970*3f2dd94aSFrançois Tigeot void
drm_atomic_private_obj_init(struct drm_private_obj * obj,struct drm_private_state * state,const struct drm_private_state_funcs * funcs)971*3f2dd94aSFrançois Tigeot drm_atomic_private_obj_init(struct drm_private_obj *obj,
972*3f2dd94aSFrançois Tigeot 			    struct drm_private_state *state,
973*3f2dd94aSFrançois Tigeot 			    const struct drm_private_state_funcs *funcs)
974*3f2dd94aSFrançois Tigeot {
975*3f2dd94aSFrançois Tigeot 	memset(obj, 0, sizeof(*obj));
976*3f2dd94aSFrançois Tigeot 
977*3f2dd94aSFrançois Tigeot 	obj->state = state;
978*3f2dd94aSFrançois Tigeot 	obj->funcs = funcs;
979*3f2dd94aSFrançois Tigeot }
980*3f2dd94aSFrançois Tigeot EXPORT_SYMBOL(drm_atomic_private_obj_init);
981*3f2dd94aSFrançois Tigeot 
982*3f2dd94aSFrançois Tigeot /**
983*3f2dd94aSFrançois Tigeot  * drm_atomic_private_obj_fini - finalize private object
984*3f2dd94aSFrançois Tigeot  * @obj: private object
985*3f2dd94aSFrançois Tigeot  *
986*3f2dd94aSFrançois Tigeot  * Finalize the private object.
987*3f2dd94aSFrançois Tigeot  */
988*3f2dd94aSFrançois Tigeot void
drm_atomic_private_obj_fini(struct drm_private_obj * obj)989*3f2dd94aSFrançois Tigeot drm_atomic_private_obj_fini(struct drm_private_obj *obj)
990*3f2dd94aSFrançois Tigeot {
991*3f2dd94aSFrançois Tigeot 	obj->funcs->atomic_destroy_state(obj, obj->state);
992*3f2dd94aSFrançois Tigeot }
993*3f2dd94aSFrançois Tigeot EXPORT_SYMBOL(drm_atomic_private_obj_fini);
994*3f2dd94aSFrançois Tigeot 
995*3f2dd94aSFrançois Tigeot /**
996*3f2dd94aSFrançois Tigeot  * drm_atomic_get_private_obj_state - get private object state
997*3f2dd94aSFrançois Tigeot  * @state: global atomic state
998*3f2dd94aSFrançois Tigeot  * @obj: private object to get the state for
999*3f2dd94aSFrançois Tigeot  *
1000*3f2dd94aSFrançois Tigeot  * This function returns the private object state for the given private object,
1001*3f2dd94aSFrançois Tigeot  * allocating the state if needed. It does not grab any locks as the caller is
1002*3f2dd94aSFrançois Tigeot  * expected to care of any required locking.
1003*3f2dd94aSFrançois Tigeot  *
1004*3f2dd94aSFrançois Tigeot  * RETURNS:
1005*3f2dd94aSFrançois Tigeot  *
1006*3f2dd94aSFrançois Tigeot  * Either the allocated state or the error code encoded into a pointer.
1007*3f2dd94aSFrançois Tigeot  */
1008*3f2dd94aSFrançois Tigeot struct drm_private_state *
drm_atomic_get_private_obj_state(struct drm_atomic_state * state,struct drm_private_obj * obj)1009*3f2dd94aSFrançois Tigeot drm_atomic_get_private_obj_state(struct drm_atomic_state *state,
1010*3f2dd94aSFrançois Tigeot 				 struct drm_private_obj *obj)
1011*3f2dd94aSFrançois Tigeot {
1012*3f2dd94aSFrançois Tigeot 	int index, num_objs, i;
1013*3f2dd94aSFrançois Tigeot 	size_t size;
1014*3f2dd94aSFrançois Tigeot 	struct __drm_private_objs_state *arr;
1015*3f2dd94aSFrançois Tigeot 	struct drm_private_state *obj_state;
1016*3f2dd94aSFrançois Tigeot 
1017*3f2dd94aSFrançois Tigeot 	for (i = 0; i < state->num_private_objs; i++)
1018*3f2dd94aSFrançois Tigeot 		if (obj == state->private_objs[i].ptr)
1019*3f2dd94aSFrançois Tigeot 			return state->private_objs[i].state;
1020*3f2dd94aSFrançois Tigeot 
1021*3f2dd94aSFrançois Tigeot 	num_objs = state->num_private_objs + 1;
1022*3f2dd94aSFrançois Tigeot 	size = sizeof(*state->private_objs) * num_objs;
1023*3f2dd94aSFrançois Tigeot 	arr = krealloc(state->private_objs, size, M_DRM, GFP_KERNEL);
1024*3f2dd94aSFrançois Tigeot 	if (!arr)
1025*3f2dd94aSFrançois Tigeot 		return ERR_PTR(-ENOMEM);
1026*3f2dd94aSFrançois Tigeot 
1027*3f2dd94aSFrançois Tigeot 	state->private_objs = arr;
1028*3f2dd94aSFrançois Tigeot 	index = state->num_private_objs;
1029*3f2dd94aSFrançois Tigeot 	memset(&state->private_objs[index], 0, sizeof(*state->private_objs));
1030*3f2dd94aSFrançois Tigeot 
1031*3f2dd94aSFrançois Tigeot 	obj_state = obj->funcs->atomic_duplicate_state(obj);
1032*3f2dd94aSFrançois Tigeot 	if (!obj_state)
1033*3f2dd94aSFrançois Tigeot 		return ERR_PTR(-ENOMEM);
1034*3f2dd94aSFrançois Tigeot 
1035*3f2dd94aSFrançois Tigeot 	state->private_objs[index].state = obj_state;
1036*3f2dd94aSFrançois Tigeot 	state->private_objs[index].old_state = obj->state;
1037*3f2dd94aSFrançois Tigeot 	state->private_objs[index].new_state = obj_state;
1038*3f2dd94aSFrançois Tigeot 	state->private_objs[index].ptr = obj;
1039*3f2dd94aSFrançois Tigeot 
1040*3f2dd94aSFrançois Tigeot 	state->num_private_objs = num_objs;
1041*3f2dd94aSFrançois Tigeot 
1042*3f2dd94aSFrançois Tigeot 	DRM_DEBUG_ATOMIC("Added new private object %p state %p to %p\n",
1043*3f2dd94aSFrançois Tigeot 			 obj, obj_state, state);
1044*3f2dd94aSFrançois Tigeot 
1045*3f2dd94aSFrançois Tigeot 	return obj_state;
1046*3f2dd94aSFrançois Tigeot }
1047*3f2dd94aSFrançois Tigeot EXPORT_SYMBOL(drm_atomic_get_private_obj_state);
1048*3f2dd94aSFrançois Tigeot 
1049*3f2dd94aSFrançois Tigeot /**
10502c9916cdSFrançois Tigeot  * drm_atomic_get_connector_state - get connector state
10512c9916cdSFrançois Tigeot  * @state: global atomic state object
10522c9916cdSFrançois Tigeot  * @connector: connector to get state object for
10532c9916cdSFrançois Tigeot  *
10542c9916cdSFrançois Tigeot  * This function returns the connector state for the given connector,
10552c9916cdSFrançois Tigeot  * allocating it if needed. It will also grab the relevant connector lock to
10562c9916cdSFrançois Tigeot  * make sure that the state is consistent.
10572c9916cdSFrançois Tigeot  *
10582c9916cdSFrançois Tigeot  * Returns:
10592c9916cdSFrançois Tigeot  *
10602c9916cdSFrançois Tigeot  * Either the allocated state or the error code encoded into the pointer. When
10612c9916cdSFrançois Tigeot  * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
10622c9916cdSFrançois Tigeot  * entire atomic sequence must be restarted. All other errors are fatal.
10632c9916cdSFrançois Tigeot  */
10642c9916cdSFrançois Tigeot struct drm_connector_state *
drm_atomic_get_connector_state(struct drm_atomic_state * state,struct drm_connector * connector)10652c9916cdSFrançois Tigeot drm_atomic_get_connector_state(struct drm_atomic_state *state,
10662c9916cdSFrançois Tigeot 			  struct drm_connector *connector)
10672c9916cdSFrançois Tigeot {
10682c9916cdSFrançois Tigeot 	int ret, index;
10692c9916cdSFrançois Tigeot 	struct drm_mode_config *config = &connector->dev->mode_config;
10702c9916cdSFrançois Tigeot 	struct drm_connector_state *connector_state;
10712c9916cdSFrançois Tigeot 
10728621f407SFrançois Tigeot 	WARN_ON(!state->acquire_ctx);
10738621f407SFrançois Tigeot 
10742c9916cdSFrançois Tigeot 	ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
10752c9916cdSFrançois Tigeot 	if (ret)
10762c9916cdSFrançois Tigeot 		return ERR_PTR(ret);
10772c9916cdSFrançois Tigeot 
10782c9916cdSFrançois Tigeot 	index = drm_connector_index(connector);
10792c9916cdSFrançois Tigeot 
10802c9916cdSFrançois Tigeot 	if (index >= state->num_connector) {
10811dedbd3bSFrançois Tigeot 		struct __drm_connnectors_state *c;
1082c0e85e96SFrançois Tigeot 		int alloc = max(index + 1, config->num_connector);
1083c0e85e96SFrançois Tigeot 
10844be47400SFrançois Tigeot 		c = krealloc(state->connectors,
10854be47400SFrançois Tigeot 			     alloc * sizeof(*state->connectors), M_DRM,
10864be47400SFrançois Tigeot 			     GFP_KERNEL);
1087c0e85e96SFrançois Tigeot 		if (!c)
1088c0e85e96SFrançois Tigeot 			return ERR_PTR(-ENOMEM);
1089c0e85e96SFrançois Tigeot 
1090c0e85e96SFrançois Tigeot 		state->connectors = c;
1091c0e85e96SFrançois Tigeot 		memset(&state->connectors[state->num_connector], 0,
1092c0e85e96SFrançois Tigeot 		       sizeof(*state->connectors) * (alloc - state->num_connector));
1093c0e85e96SFrançois Tigeot 
1094c0e85e96SFrançois Tigeot 		state->num_connector = alloc;
10952c9916cdSFrançois Tigeot 	}
10962c9916cdSFrançois Tigeot 
10971dedbd3bSFrançois Tigeot 	if (state->connectors[index].state)
10981dedbd3bSFrançois Tigeot 		return state->connectors[index].state;
10992c9916cdSFrançois Tigeot 
11002c9916cdSFrançois Tigeot 	connector_state = connector->funcs->atomic_duplicate_state(connector);
11012c9916cdSFrançois Tigeot 	if (!connector_state)
11022c9916cdSFrançois Tigeot 		return ERR_PTR(-ENOMEM);
11032c9916cdSFrançois Tigeot 
1104a85cb24fSFrançois Tigeot 	drm_connector_get(connector);
11051dedbd3bSFrançois Tigeot 	state->connectors[index].state = connector_state;
1106a85cb24fSFrançois Tigeot 	state->connectors[index].old_state = connector->state;
1107a85cb24fSFrançois Tigeot 	state->connectors[index].new_state = connector_state;
11081dedbd3bSFrançois Tigeot 	state->connectors[index].ptr = connector;
11092c9916cdSFrançois Tigeot 	connector_state->state = state;
11102c9916cdSFrançois Tigeot 
1111a85cb24fSFrançois Tigeot 	DRM_DEBUG_ATOMIC("Added [CONNECTOR:%d:%s] %p state to %p\n",
1112a85cb24fSFrançois Tigeot 			 connector->base.id, connector->name,
1113a85cb24fSFrançois Tigeot 			 connector_state, state);
11142c9916cdSFrançois Tigeot 
11152c9916cdSFrançois Tigeot 	if (connector_state->crtc) {
11162c9916cdSFrançois Tigeot 		struct drm_crtc_state *crtc_state;
11172c9916cdSFrançois Tigeot 
11182c9916cdSFrançois Tigeot 		crtc_state = drm_atomic_get_crtc_state(state,
11192c9916cdSFrançois Tigeot 						       connector_state->crtc);
11202c9916cdSFrançois Tigeot 		if (IS_ERR(crtc_state))
11212c9916cdSFrançois Tigeot 			return ERR_CAST(crtc_state);
11222c9916cdSFrançois Tigeot 	}
11232c9916cdSFrançois Tigeot 
11242c9916cdSFrançois Tigeot 	return connector_state;
11252c9916cdSFrançois Tigeot }
11262c9916cdSFrançois Tigeot EXPORT_SYMBOL(drm_atomic_get_connector_state);
11272c9916cdSFrançois Tigeot 
11282c9916cdSFrançois Tigeot /**
11292c9916cdSFrançois Tigeot  * drm_atomic_connector_set_property - set property on connector.
11302c9916cdSFrançois Tigeot  * @connector: the drm connector to set a property on
11312c9916cdSFrançois Tigeot  * @state: the state object to update with the new property value
11322c9916cdSFrançois Tigeot  * @property: the property to set
11332c9916cdSFrançois Tigeot  * @val: the new property value
11342c9916cdSFrançois Tigeot  *
1135a85cb24fSFrançois Tigeot  * This function handles generic/core properties and calls out to driver's
1136a85cb24fSFrançois Tigeot  * &drm_connector_funcs.atomic_set_property for driver properties.  To ensure
1137a85cb24fSFrançois Tigeot  * consistent behavior you must call this function rather than the driver hook
1138a85cb24fSFrançois Tigeot  * directly.
11392c9916cdSFrançois Tigeot  *
11402c9916cdSFrançois Tigeot  * RETURNS:
11412c9916cdSFrançois Tigeot  * Zero on success, error code on failure
11422c9916cdSFrançois Tigeot  */
drm_atomic_connector_set_property(struct drm_connector * connector,struct drm_connector_state * state,struct drm_property * property,uint64_t val)1143*3f2dd94aSFrançois Tigeot static int drm_atomic_connector_set_property(struct drm_connector *connector,
11442c9916cdSFrançois Tigeot 		struct drm_connector_state *state, struct drm_property *property,
11452c9916cdSFrançois Tigeot 		uint64_t val)
11462c9916cdSFrançois Tigeot {
11472c9916cdSFrançois Tigeot 	struct drm_device *dev = connector->dev;
11482c9916cdSFrançois Tigeot 	struct drm_mode_config *config = &dev->mode_config;
11492c9916cdSFrançois Tigeot 
11502c9916cdSFrançois Tigeot 	if (property == config->prop_crtc_id) {
1151*3f2dd94aSFrançois Tigeot 		struct drm_crtc *crtc = drm_crtc_find(dev, NULL, val);
11522c9916cdSFrançois Tigeot 		return drm_atomic_set_crtc_for_connector(state, crtc);
11532c9916cdSFrançois Tigeot 	} else if (property == config->dpms_property) {
11542c9916cdSFrançois Tigeot 		/* setting DPMS property requires special handling, which
11552c9916cdSFrançois Tigeot 		 * is done in legacy setprop path for us.  Disallow (for
11562c9916cdSFrançois Tigeot 		 * now?) atomic writes to DPMS property:
11572c9916cdSFrançois Tigeot 		 */
11582c9916cdSFrançois Tigeot 		return -EINVAL;
11594be47400SFrançois Tigeot 	} else if (property == config->tv_select_subconnector_property) {
11604be47400SFrançois Tigeot 		state->tv.subconnector = val;
11614be47400SFrançois Tigeot 	} else if (property == config->tv_left_margin_property) {
11624be47400SFrançois Tigeot 		state->tv.margins.left = val;
11634be47400SFrançois Tigeot 	} else if (property == config->tv_right_margin_property) {
11644be47400SFrançois Tigeot 		state->tv.margins.right = val;
11654be47400SFrançois Tigeot 	} else if (property == config->tv_top_margin_property) {
11664be47400SFrançois Tigeot 		state->tv.margins.top = val;
11674be47400SFrançois Tigeot 	} else if (property == config->tv_bottom_margin_property) {
11684be47400SFrançois Tigeot 		state->tv.margins.bottom = val;
11694be47400SFrançois Tigeot 	} else if (property == config->tv_mode_property) {
11704be47400SFrançois Tigeot 		state->tv.mode = val;
11714be47400SFrançois Tigeot 	} else if (property == config->tv_brightness_property) {
11724be47400SFrançois Tigeot 		state->tv.brightness = val;
11734be47400SFrançois Tigeot 	} else if (property == config->tv_contrast_property) {
11744be47400SFrançois Tigeot 		state->tv.contrast = val;
11754be47400SFrançois Tigeot 	} else if (property == config->tv_flicker_reduction_property) {
11764be47400SFrançois Tigeot 		state->tv.flicker_reduction = val;
11774be47400SFrançois Tigeot 	} else if (property == config->tv_overscan_property) {
11784be47400SFrançois Tigeot 		state->tv.overscan = val;
11794be47400SFrançois Tigeot 	} else if (property == config->tv_saturation_property) {
11804be47400SFrançois Tigeot 		state->tv.saturation = val;
11814be47400SFrançois Tigeot 	} else if (property == config->tv_hue_property) {
11824be47400SFrançois Tigeot 		state->tv.hue = val;
1183a85cb24fSFrançois Tigeot 	} else if (property == config->link_status_property) {
1184a85cb24fSFrançois Tigeot 		/* Never downgrade from GOOD to BAD on userspace's request here,
1185a85cb24fSFrançois Tigeot 		 * only hw issues can do that.
1186a85cb24fSFrançois Tigeot 		 *
1187a85cb24fSFrançois Tigeot 		 * For an atomic property the userspace doesn't need to be able
1188a85cb24fSFrançois Tigeot 		 * to understand all the properties, but needs to be able to
1189a85cb24fSFrançois Tigeot 		 * restore the state it wants on VT switch. So if the userspace
1190a85cb24fSFrançois Tigeot 		 * tries to change the link_status from GOOD to BAD, driver
1191a85cb24fSFrançois Tigeot 		 * silently rejects it and returns a 0. This prevents userspace
1192a85cb24fSFrançois Tigeot 		 * from accidently breaking  the display when it restores the
1193a85cb24fSFrançois Tigeot 		 * state.
1194a85cb24fSFrançois Tigeot 		 */
1195a85cb24fSFrançois Tigeot 		if (state->link_status != DRM_LINK_STATUS_GOOD)
1196a85cb24fSFrançois Tigeot 			state->link_status = val;
1197*3f2dd94aSFrançois Tigeot 	} else if (property == config->aspect_ratio_property) {
1198*3f2dd94aSFrançois Tigeot 		state->picture_aspect_ratio = val;
1199*3f2dd94aSFrançois Tigeot 	} else if (property == connector->scaling_mode_property) {
1200*3f2dd94aSFrançois Tigeot 		state->scaling_mode = val;
12012c9916cdSFrançois Tigeot 	} else if (connector->funcs->atomic_set_property) {
12022c9916cdSFrançois Tigeot 		return connector->funcs->atomic_set_property(connector,
12032c9916cdSFrançois Tigeot 				state, property, val);
12042c9916cdSFrançois Tigeot 	} else {
12052c9916cdSFrançois Tigeot 		return -EINVAL;
12062c9916cdSFrançois Tigeot 	}
12074be47400SFrançois Tigeot 
12084be47400SFrançois Tigeot 	return 0;
12092c9916cdSFrançois Tigeot }
12102c9916cdSFrançois Tigeot 
drm_atomic_connector_print_state(struct drm_printer * p,const struct drm_connector_state * state)12114be47400SFrançois Tigeot static void drm_atomic_connector_print_state(struct drm_printer *p,
12124be47400SFrançois Tigeot 		const struct drm_connector_state *state)
12134be47400SFrançois Tigeot {
12144be47400SFrançois Tigeot 	struct drm_connector *connector = state->connector;
12154be47400SFrançois Tigeot 
12164be47400SFrançois Tigeot 	drm_printf(p, "connector[%u]: %s\n", connector->base.id, connector->name);
12174be47400SFrançois Tigeot 	drm_printf(p, "\tcrtc=%s\n", state->crtc ? state->crtc->name : "(null)");
12184be47400SFrançois Tigeot 
12194be47400SFrançois Tigeot 	if (connector->funcs->atomic_print_state)
12204be47400SFrançois Tigeot 		connector->funcs->atomic_print_state(p, state);
12214be47400SFrançois Tigeot }
12224be47400SFrançois Tigeot 
1223aee94f86SFrançois Tigeot /**
1224aee94f86SFrançois Tigeot  * drm_atomic_connector_get_property - get property value from connector state
1225aee94f86SFrançois Tigeot  * @connector: the drm connector to set a property on
1226aee94f86SFrançois Tigeot  * @state: the state object to get the property value from
1227aee94f86SFrançois Tigeot  * @property: the property to set
1228aee94f86SFrançois Tigeot  * @val: return location for the property value
1229aee94f86SFrançois Tigeot  *
1230a85cb24fSFrançois Tigeot  * This function handles generic/core properties and calls out to driver's
1231a85cb24fSFrançois Tigeot  * &drm_connector_funcs.atomic_get_property for driver properties.  To ensure
1232a85cb24fSFrançois Tigeot  * consistent behavior you must call this function rather than the driver hook
1233a85cb24fSFrançois Tigeot  * directly.
1234aee94f86SFrançois Tigeot  *
1235aee94f86SFrançois Tigeot  * RETURNS:
1236aee94f86SFrançois Tigeot  * Zero on success, error code on failure
12372c9916cdSFrançois Tigeot  */
12382c9916cdSFrançois Tigeot static int
drm_atomic_connector_get_property(struct drm_connector * connector,const struct drm_connector_state * state,struct drm_property * property,uint64_t * val)12392c9916cdSFrançois Tigeot drm_atomic_connector_get_property(struct drm_connector *connector,
12402c9916cdSFrançois Tigeot 		const struct drm_connector_state *state,
12412c9916cdSFrançois Tigeot 		struct drm_property *property, uint64_t *val)
12422c9916cdSFrançois Tigeot {
12432c9916cdSFrançois Tigeot 	struct drm_device *dev = connector->dev;
12442c9916cdSFrançois Tigeot 	struct drm_mode_config *config = &dev->mode_config;
12452c9916cdSFrançois Tigeot 
12462c9916cdSFrançois Tigeot 	if (property == config->prop_crtc_id) {
12472c9916cdSFrançois Tigeot 		*val = (state->crtc) ? state->crtc->base.id : 0;
12482c9916cdSFrançois Tigeot 	} else if (property == config->dpms_property) {
12492c9916cdSFrançois Tigeot 		*val = connector->dpms;
12504be47400SFrançois Tigeot 	} else if (property == config->tv_select_subconnector_property) {
12514be47400SFrançois Tigeot 		*val = state->tv.subconnector;
12524be47400SFrançois Tigeot 	} else if (property == config->tv_left_margin_property) {
12534be47400SFrançois Tigeot 		*val = state->tv.margins.left;
12544be47400SFrançois Tigeot 	} else if (property == config->tv_right_margin_property) {
12554be47400SFrançois Tigeot 		*val = state->tv.margins.right;
12564be47400SFrançois Tigeot 	} else if (property == config->tv_top_margin_property) {
12574be47400SFrançois Tigeot 		*val = state->tv.margins.top;
12584be47400SFrançois Tigeot 	} else if (property == config->tv_bottom_margin_property) {
12594be47400SFrançois Tigeot 		*val = state->tv.margins.bottom;
12604be47400SFrançois Tigeot 	} else if (property == config->tv_mode_property) {
12614be47400SFrançois Tigeot 		*val = state->tv.mode;
12624be47400SFrançois Tigeot 	} else if (property == config->tv_brightness_property) {
12634be47400SFrançois Tigeot 		*val = state->tv.brightness;
12644be47400SFrançois Tigeot 	} else if (property == config->tv_contrast_property) {
12654be47400SFrançois Tigeot 		*val = state->tv.contrast;
12664be47400SFrançois Tigeot 	} else if (property == config->tv_flicker_reduction_property) {
12674be47400SFrançois Tigeot 		*val = state->tv.flicker_reduction;
12684be47400SFrançois Tigeot 	} else if (property == config->tv_overscan_property) {
12694be47400SFrançois Tigeot 		*val = state->tv.overscan;
12704be47400SFrançois Tigeot 	} else if (property == config->tv_saturation_property) {
12714be47400SFrançois Tigeot 		*val = state->tv.saturation;
12724be47400SFrançois Tigeot 	} else if (property == config->tv_hue_property) {
12734be47400SFrançois Tigeot 		*val = state->tv.hue;
1274a85cb24fSFrançois Tigeot 	} else if (property == config->link_status_property) {
1275a85cb24fSFrançois Tigeot 		*val = state->link_status;
1276*3f2dd94aSFrançois Tigeot 	} else if (property == config->aspect_ratio_property) {
1277*3f2dd94aSFrançois Tigeot 		*val = state->picture_aspect_ratio;
1278*3f2dd94aSFrançois Tigeot 	} else if (property == connector->scaling_mode_property) {
1279*3f2dd94aSFrançois Tigeot 		*val = state->scaling_mode;
12802c9916cdSFrançois Tigeot 	} else if (connector->funcs->atomic_get_property) {
12812c9916cdSFrançois Tigeot 		return connector->funcs->atomic_get_property(connector,
12822c9916cdSFrançois Tigeot 				state, property, val);
12832c9916cdSFrançois Tigeot 	} else {
12842c9916cdSFrançois Tigeot 		return -EINVAL;
12852c9916cdSFrançois Tigeot 	}
12862c9916cdSFrançois Tigeot 
12872c9916cdSFrançois Tigeot 	return 0;
12882c9916cdSFrançois Tigeot }
12892c9916cdSFrançois Tigeot 
drm_atomic_get_property(struct drm_mode_object * obj,struct drm_property * property,uint64_t * val)12902c9916cdSFrançois Tigeot int drm_atomic_get_property(struct drm_mode_object *obj,
12912c9916cdSFrançois Tigeot 		struct drm_property *property, uint64_t *val)
12922c9916cdSFrançois Tigeot {
12932c9916cdSFrançois Tigeot 	struct drm_device *dev = property->dev;
12942c9916cdSFrançois Tigeot 	int ret;
12952c9916cdSFrançois Tigeot 
12962c9916cdSFrançois Tigeot 	switch (obj->type) {
12972c9916cdSFrançois Tigeot 	case DRM_MODE_OBJECT_CONNECTOR: {
12982c9916cdSFrançois Tigeot 		struct drm_connector *connector = obj_to_connector(obj);
12992c9916cdSFrançois Tigeot 		WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
13002c9916cdSFrançois Tigeot 		ret = drm_atomic_connector_get_property(connector,
13012c9916cdSFrançois Tigeot 				connector->state, property, val);
13022c9916cdSFrançois Tigeot 		break;
13032c9916cdSFrançois Tigeot 	}
13042c9916cdSFrançois Tigeot 	case DRM_MODE_OBJECT_CRTC: {
13052c9916cdSFrançois Tigeot 		struct drm_crtc *crtc = obj_to_crtc(obj);
13062c9916cdSFrançois Tigeot 		WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
13072c9916cdSFrançois Tigeot 		ret = drm_atomic_crtc_get_property(crtc,
13082c9916cdSFrançois Tigeot 				crtc->state, property, val);
13092c9916cdSFrançois Tigeot 		break;
13102c9916cdSFrançois Tigeot 	}
13112c9916cdSFrançois Tigeot 	case DRM_MODE_OBJECT_PLANE: {
13122c9916cdSFrançois Tigeot 		struct drm_plane *plane = obj_to_plane(obj);
13132c9916cdSFrançois Tigeot 		WARN_ON(!drm_modeset_is_locked(&plane->mutex));
13142c9916cdSFrançois Tigeot 		ret = drm_atomic_plane_get_property(plane,
13152c9916cdSFrançois Tigeot 				plane->state, property, val);
13162c9916cdSFrançois Tigeot 		break;
13172c9916cdSFrançois Tigeot 	}
13182c9916cdSFrançois Tigeot 	default:
13192c9916cdSFrançois Tigeot 		ret = -EINVAL;
13202c9916cdSFrançois Tigeot 		break;
13212c9916cdSFrançois Tigeot 	}
13222c9916cdSFrançois Tigeot 
13232c9916cdSFrançois Tigeot 	return ret;
13242c9916cdSFrançois Tigeot }
13252c9916cdSFrançois Tigeot 
13262c9916cdSFrançois Tigeot /**
13272c9916cdSFrançois Tigeot  * drm_atomic_set_crtc_for_plane - set crtc for plane
13282c9916cdSFrançois Tigeot  * @plane_state: the plane whose incoming state to update
13292c9916cdSFrançois Tigeot  * @crtc: crtc to use for the plane
13302c9916cdSFrançois Tigeot  *
13312c9916cdSFrançois Tigeot  * Changing the assigned crtc for a plane requires us to grab the lock and state
13322c9916cdSFrançois Tigeot  * for the new crtc, as needed. This function takes care of all these details
13332c9916cdSFrançois Tigeot  * besides updating the pointer in the state object itself.
13342c9916cdSFrançois Tigeot  *
13352c9916cdSFrançois Tigeot  * Returns:
13362c9916cdSFrançois Tigeot  * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
13372c9916cdSFrançois Tigeot  * then the w/w mutex code has detected a deadlock and the entire atomic
13382c9916cdSFrançois Tigeot  * sequence must be restarted. All other errors are fatal.
13392c9916cdSFrançois Tigeot  */
13402c9916cdSFrançois Tigeot int
drm_atomic_set_crtc_for_plane(struct drm_plane_state * plane_state,struct drm_crtc * crtc)13412c9916cdSFrançois Tigeot drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
13422c9916cdSFrançois Tigeot 			      struct drm_crtc *crtc)
13432c9916cdSFrançois Tigeot {
13442c9916cdSFrançois Tigeot 	struct drm_plane *plane = plane_state->plane;
13452c9916cdSFrançois Tigeot 	struct drm_crtc_state *crtc_state;
13462c9916cdSFrançois Tigeot 
13472c9916cdSFrançois Tigeot 	if (plane_state->crtc) {
13482c9916cdSFrançois Tigeot 		crtc_state = drm_atomic_get_crtc_state(plane_state->state,
13492c9916cdSFrançois Tigeot 						       plane_state->crtc);
13502c9916cdSFrançois Tigeot 		if (WARN_ON(IS_ERR(crtc_state)))
13512c9916cdSFrançois Tigeot 			return PTR_ERR(crtc_state);
13522c9916cdSFrançois Tigeot 
13532c9916cdSFrançois Tigeot 		crtc_state->plane_mask &= ~(1 << drm_plane_index(plane));
13542c9916cdSFrançois Tigeot 	}
13552c9916cdSFrançois Tigeot 
13562c9916cdSFrançois Tigeot 	plane_state->crtc = crtc;
13572c9916cdSFrançois Tigeot 
13582c9916cdSFrançois Tigeot 	if (crtc) {
13592c9916cdSFrançois Tigeot 		crtc_state = drm_atomic_get_crtc_state(plane_state->state,
13602c9916cdSFrançois Tigeot 						       crtc);
13612c9916cdSFrançois Tigeot 		if (IS_ERR(crtc_state))
13622c9916cdSFrançois Tigeot 			return PTR_ERR(crtc_state);
13632c9916cdSFrançois Tigeot 		crtc_state->plane_mask |= (1 << drm_plane_index(plane));
13642c9916cdSFrançois Tigeot 	}
13652c9916cdSFrançois Tigeot 
13662c9916cdSFrançois Tigeot 	if (crtc)
1367aee94f86SFrançois Tigeot 		DRM_DEBUG_ATOMIC("Link plane state %p to [CRTC:%d:%s]\n",
1368aee94f86SFrançois Tigeot 				 plane_state, crtc->base.id, crtc->name);
13692c9916cdSFrançois Tigeot 	else
1370477eb7f9SFrançois Tigeot 		DRM_DEBUG_ATOMIC("Link plane state %p to [NOCRTC]\n",
1371477eb7f9SFrançois Tigeot 				 plane_state);
13722c9916cdSFrançois Tigeot 
13732c9916cdSFrançois Tigeot 	return 0;
13742c9916cdSFrançois Tigeot }
13752c9916cdSFrançois Tigeot EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane);
13762c9916cdSFrançois Tigeot 
13772c9916cdSFrançois Tigeot /**
1378477eb7f9SFrançois Tigeot  * drm_atomic_set_fb_for_plane - set framebuffer for plane
13792c9916cdSFrançois Tigeot  * @plane_state: atomic state object for the plane
13802c9916cdSFrançois Tigeot  * @fb: fb to use for the plane
13812c9916cdSFrançois Tigeot  *
13822c9916cdSFrançois Tigeot  * Changing the assigned framebuffer for a plane requires us to grab a reference
13832c9916cdSFrançois Tigeot  * to the new fb and drop the reference to the old fb, if there is one. This
13842c9916cdSFrançois Tigeot  * function takes care of all these details besides updating the pointer in the
13852c9916cdSFrançois Tigeot  * state object itself.
13862c9916cdSFrançois Tigeot  */
13872c9916cdSFrançois Tigeot void
drm_atomic_set_fb_for_plane(struct drm_plane_state * plane_state,struct drm_framebuffer * fb)13882c9916cdSFrançois Tigeot drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
13892c9916cdSFrançois Tigeot 			    struct drm_framebuffer *fb)
13902c9916cdSFrançois Tigeot {
13912c9916cdSFrançois Tigeot 	if (fb)
1392477eb7f9SFrançois Tigeot 		DRM_DEBUG_ATOMIC("Set [FB:%d] for plane state %p\n",
13932c9916cdSFrançois Tigeot 				 fb->base.id, plane_state);
13942c9916cdSFrançois Tigeot 	else
1395477eb7f9SFrançois Tigeot 		DRM_DEBUG_ATOMIC("Set [NOFB] for plane state %p\n",
1396477eb7f9SFrançois Tigeot 				 plane_state);
13974be47400SFrançois Tigeot 
13984be47400SFrançois Tigeot 	drm_framebuffer_assign(&plane_state->fb, fb);
13992c9916cdSFrançois Tigeot }
14002c9916cdSFrançois Tigeot EXPORT_SYMBOL(drm_atomic_set_fb_for_plane);
14012c9916cdSFrançois Tigeot 
14022c9916cdSFrançois Tigeot /**
14034be47400SFrançois Tigeot  * drm_atomic_set_fence_for_plane - set fence for plane
14044be47400SFrançois Tigeot  * @plane_state: atomic state object for the plane
14054be47400SFrançois Tigeot  * @fence: dma_fence to use for the plane
14064be47400SFrançois Tigeot  *
14074be47400SFrançois Tigeot  * Helper to setup the plane_state fence in case it is not set yet.
14084be47400SFrançois Tigeot  * By using this drivers doesn't need to worry if the user choose
14094be47400SFrançois Tigeot  * implicit or explicit fencing.
14104be47400SFrançois Tigeot  *
14114be47400SFrançois Tigeot  * This function will not set the fence to the state if it was set
1412a85cb24fSFrançois Tigeot  * via explicit fencing interfaces on the atomic ioctl. In that case it will
1413a85cb24fSFrançois Tigeot  * drop the reference to the fence as we are not storing it anywhere.
1414a85cb24fSFrançois Tigeot  * Otherwise, if &drm_plane_state.fence is not set this function we just set it
1415a85cb24fSFrançois Tigeot  * with the received implicit fence. In both cases this function consumes a
1416a85cb24fSFrançois Tigeot  * reference for @fence.
14174be47400SFrançois Tigeot  */
14184be47400SFrançois Tigeot void
drm_atomic_set_fence_for_plane(struct drm_plane_state * plane_state,struct dma_fence * fence)14194be47400SFrançois Tigeot drm_atomic_set_fence_for_plane(struct drm_plane_state *plane_state,
14204be47400SFrançois Tigeot 			       struct dma_fence *fence)
14214be47400SFrançois Tigeot {
14224be47400SFrançois Tigeot 	if (plane_state->fence) {
14234be47400SFrançois Tigeot 		dma_fence_put(fence);
14244be47400SFrançois Tigeot 		return;
14254be47400SFrançois Tigeot 	}
14264be47400SFrançois Tigeot 
14274be47400SFrançois Tigeot 	plane_state->fence = fence;
14284be47400SFrançois Tigeot }
14294be47400SFrançois Tigeot EXPORT_SYMBOL(drm_atomic_set_fence_for_plane);
14304be47400SFrançois Tigeot 
14314be47400SFrançois Tigeot /**
14322c9916cdSFrançois Tigeot  * drm_atomic_set_crtc_for_connector - set crtc for connector
14332c9916cdSFrançois Tigeot  * @conn_state: atomic state object for the connector
14342c9916cdSFrançois Tigeot  * @crtc: crtc to use for the connector
14352c9916cdSFrançois Tigeot  *
14362c9916cdSFrançois Tigeot  * Changing the assigned crtc for a connector requires us to grab the lock and
14372c9916cdSFrançois Tigeot  * state for the new crtc, as needed. This function takes care of all these
14382c9916cdSFrançois Tigeot  * details besides updating the pointer in the state object itself.
14392c9916cdSFrançois Tigeot  *
14402c9916cdSFrançois Tigeot  * Returns:
14412c9916cdSFrançois Tigeot  * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
14422c9916cdSFrançois Tigeot  * then the w/w mutex code has detected a deadlock and the entire atomic
14432c9916cdSFrançois Tigeot  * sequence must be restarted. All other errors are fatal.
14442c9916cdSFrançois Tigeot  */
14452c9916cdSFrançois Tigeot int
drm_atomic_set_crtc_for_connector(struct drm_connector_state * conn_state,struct drm_crtc * crtc)14462c9916cdSFrançois Tigeot drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
14472c9916cdSFrançois Tigeot 				  struct drm_crtc *crtc)
14482c9916cdSFrançois Tigeot {
14492c9916cdSFrançois Tigeot 	struct drm_crtc_state *crtc_state;
14502c9916cdSFrançois Tigeot 
14518621f407SFrançois Tigeot 	if (conn_state->crtc == crtc)
14528621f407SFrançois Tigeot 		return 0;
14538621f407SFrançois Tigeot 
14548621f407SFrançois Tigeot 	if (conn_state->crtc) {
1455a85cb24fSFrançois Tigeot 		crtc_state = drm_atomic_get_new_crtc_state(conn_state->state,
1456aee94f86SFrançois Tigeot 							   conn_state->crtc);
1457aee94f86SFrançois Tigeot 
1458aee94f86SFrançois Tigeot 		crtc_state->connector_mask &=
1459aee94f86SFrançois Tigeot 			~(1 << drm_connector_index(conn_state->connector));
14608621f407SFrançois Tigeot 
1461a85cb24fSFrançois Tigeot 		drm_connector_put(conn_state->connector);
14628621f407SFrançois Tigeot 		conn_state->crtc = NULL;
1463aee94f86SFrançois Tigeot 	}
1464aee94f86SFrançois Tigeot 
14652c9916cdSFrançois Tigeot 	if (crtc) {
14662c9916cdSFrançois Tigeot 		crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc);
14672c9916cdSFrançois Tigeot 		if (IS_ERR(crtc_state))
14682c9916cdSFrançois Tigeot 			return PTR_ERR(crtc_state);
1469aee94f86SFrançois Tigeot 
1470aee94f86SFrançois Tigeot 		crtc_state->connector_mask |=
1471aee94f86SFrançois Tigeot 			1 << drm_connector_index(conn_state->connector);
14722c9916cdSFrançois Tigeot 
1473a85cb24fSFrançois Tigeot 		drm_connector_get(conn_state->connector);
14742c9916cdSFrançois Tigeot 		conn_state->crtc = crtc;
14752c9916cdSFrançois Tigeot 
1476aee94f86SFrançois Tigeot 		DRM_DEBUG_ATOMIC("Link connector state %p to [CRTC:%d:%s]\n",
1477aee94f86SFrançois Tigeot 				 conn_state, crtc->base.id, crtc->name);
14788621f407SFrançois Tigeot 	} else {
1479477eb7f9SFrançois Tigeot 		DRM_DEBUG_ATOMIC("Link connector state %p to [NOCRTC]\n",
14802c9916cdSFrançois Tigeot 				 conn_state);
14818621f407SFrançois Tigeot 	}
14822c9916cdSFrançois Tigeot 
14832c9916cdSFrançois Tigeot 	return 0;
14842c9916cdSFrançois Tigeot }
14852c9916cdSFrançois Tigeot EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector);
14862c9916cdSFrançois Tigeot 
14872c9916cdSFrançois Tigeot /**
14882c9916cdSFrançois Tigeot  * drm_atomic_add_affected_connectors - add connectors for crtc
14892c9916cdSFrançois Tigeot  * @state: atomic state
14902c9916cdSFrançois Tigeot  * @crtc: DRM crtc
14912c9916cdSFrançois Tigeot  *
14922c9916cdSFrançois Tigeot  * This function walks the current configuration and adds all connectors
14932c9916cdSFrançois Tigeot  * currently using @crtc to the atomic configuration @state. Note that this
14942c9916cdSFrançois Tigeot  * function must acquire the connection mutex. This can potentially cause
14952c9916cdSFrançois Tigeot  * unneeded seralization if the update is just for the planes on one crtc. Hence
14962c9916cdSFrançois Tigeot  * drivers and helpers should only call this when really needed (e.g. when a
14972c9916cdSFrançois Tigeot  * full modeset needs to happen due to some change).
14982c9916cdSFrançois Tigeot  *
14992c9916cdSFrançois Tigeot  * Returns:
15002c9916cdSFrançois Tigeot  * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
15012c9916cdSFrançois Tigeot  * then the w/w mutex code has detected a deadlock and the entire atomic
15022c9916cdSFrançois Tigeot  * sequence must be restarted. All other errors are fatal.
15032c9916cdSFrançois Tigeot  */
15042c9916cdSFrançois Tigeot int
drm_atomic_add_affected_connectors(struct drm_atomic_state * state,struct drm_crtc * crtc)15052c9916cdSFrançois Tigeot drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
15062c9916cdSFrançois Tigeot 				   struct drm_crtc *crtc)
15072c9916cdSFrançois Tigeot {
15082c9916cdSFrançois Tigeot 	struct drm_mode_config *config = &state->dev->mode_config;
15092c9916cdSFrançois Tigeot 	struct drm_connector *connector;
15102c9916cdSFrançois Tigeot 	struct drm_connector_state *conn_state;
1511a85cb24fSFrançois Tigeot 	struct drm_connector_list_iter conn_iter;
1512a85cb24fSFrançois Tigeot 	struct drm_crtc_state *crtc_state;
15132c9916cdSFrançois Tigeot 	int ret;
15142c9916cdSFrançois Tigeot 
1515a85cb24fSFrançois Tigeot 	crtc_state = drm_atomic_get_crtc_state(state, crtc);
1516a85cb24fSFrançois Tigeot 	if (IS_ERR(crtc_state))
1517a85cb24fSFrançois Tigeot 		return PTR_ERR(crtc_state);
1518a85cb24fSFrançois Tigeot 
15192c9916cdSFrançois Tigeot 	ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
15202c9916cdSFrançois Tigeot 	if (ret)
15212c9916cdSFrançois Tigeot 		return ret;
15222c9916cdSFrançois Tigeot 
1523aee94f86SFrançois Tigeot 	DRM_DEBUG_ATOMIC("Adding all current connectors for [CRTC:%d:%s] to %p\n",
1524aee94f86SFrançois Tigeot 			 crtc->base.id, crtc->name, state);
15252c9916cdSFrançois Tigeot 
15262c9916cdSFrançois Tigeot 	/*
1527a85cb24fSFrançois Tigeot 	 * Changed connectors are already in @state, so only need to look
1528a85cb24fSFrançois Tigeot 	 * at the connector_mask in crtc_state.
15292c9916cdSFrançois Tigeot 	 */
1530a85cb24fSFrançois Tigeot 	drm_connector_list_iter_begin(state->dev, &conn_iter);
1531a85cb24fSFrançois Tigeot 	drm_for_each_connector_iter(connector, &conn_iter) {
1532a85cb24fSFrançois Tigeot 		if (!(crtc_state->connector_mask & (1 << drm_connector_index(connector))))
15332c9916cdSFrançois Tigeot 			continue;
15342c9916cdSFrançois Tigeot 
15352c9916cdSFrançois Tigeot 		conn_state = drm_atomic_get_connector_state(state, connector);
1536a85cb24fSFrançois Tigeot 		if (IS_ERR(conn_state)) {
1537a85cb24fSFrançois Tigeot 			drm_connector_list_iter_end(&conn_iter);
15382c9916cdSFrançois Tigeot 			return PTR_ERR(conn_state);
15392c9916cdSFrançois Tigeot 		}
1540a85cb24fSFrançois Tigeot 	}
1541a85cb24fSFrançois Tigeot 	drm_connector_list_iter_end(&conn_iter);
15422c9916cdSFrançois Tigeot 
15432c9916cdSFrançois Tigeot 	return 0;
15442c9916cdSFrançois Tigeot }
15452c9916cdSFrançois Tigeot EXPORT_SYMBOL(drm_atomic_add_affected_connectors);
15462c9916cdSFrançois Tigeot 
15472c9916cdSFrançois Tigeot /**
154819c468b4SFrançois Tigeot  * drm_atomic_add_affected_planes - add planes for crtc
154919c468b4SFrançois Tigeot  * @state: atomic state
155019c468b4SFrançois Tigeot  * @crtc: DRM crtc
155119c468b4SFrançois Tigeot  *
155219c468b4SFrançois Tigeot  * This function walks the current configuration and adds all planes
155319c468b4SFrançois Tigeot  * currently used by @crtc to the atomic configuration @state. This is useful
155419c468b4SFrançois Tigeot  * when an atomic commit also needs to check all currently enabled plane on
155519c468b4SFrançois Tigeot  * @crtc, e.g. when changing the mode. It's also useful when re-enabling a CRTC
155619c468b4SFrançois Tigeot  * to avoid special code to force-enable all planes.
155719c468b4SFrançois Tigeot  *
155819c468b4SFrançois Tigeot  * Since acquiring a plane state will always also acquire the w/w mutex of the
155919c468b4SFrançois Tigeot  * current CRTC for that plane (if there is any) adding all the plane states for
156019c468b4SFrançois Tigeot  * a CRTC will not reduce parallism of atomic updates.
156119c468b4SFrançois Tigeot  *
156219c468b4SFrançois Tigeot  * Returns:
156319c468b4SFrançois Tigeot  * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
156419c468b4SFrançois Tigeot  * then the w/w mutex code has detected a deadlock and the entire atomic
156519c468b4SFrançois Tigeot  * sequence must be restarted. All other errors are fatal.
156619c468b4SFrançois Tigeot  */
156719c468b4SFrançois Tigeot int
drm_atomic_add_affected_planes(struct drm_atomic_state * state,struct drm_crtc * crtc)156819c468b4SFrançois Tigeot drm_atomic_add_affected_planes(struct drm_atomic_state *state,
156919c468b4SFrançois Tigeot 			       struct drm_crtc *crtc)
157019c468b4SFrançois Tigeot {
157119c468b4SFrançois Tigeot 	struct drm_plane *plane;
157219c468b4SFrançois Tigeot 
1573a85cb24fSFrançois Tigeot 	WARN_ON(!drm_atomic_get_new_crtc_state(state, crtc));
157419c468b4SFrançois Tigeot 
157519c468b4SFrançois Tigeot 	drm_for_each_plane_mask(plane, state->dev, crtc->state->plane_mask) {
157619c468b4SFrançois Tigeot 		struct drm_plane_state *plane_state =
157719c468b4SFrançois Tigeot 			drm_atomic_get_plane_state(state, plane);
157819c468b4SFrançois Tigeot 
157919c468b4SFrançois Tigeot 		if (IS_ERR(plane_state))
158019c468b4SFrançois Tigeot 			return PTR_ERR(plane_state);
158119c468b4SFrançois Tigeot 	}
158219c468b4SFrançois Tigeot 	return 0;
158319c468b4SFrançois Tigeot }
158419c468b4SFrançois Tigeot EXPORT_SYMBOL(drm_atomic_add_affected_planes);
158519c468b4SFrançois Tigeot 
158619c468b4SFrançois Tigeot /**
15872c9916cdSFrançois Tigeot  * drm_atomic_check_only - check whether a given config would work
15882c9916cdSFrançois Tigeot  * @state: atomic configuration to check
15892c9916cdSFrançois Tigeot  *
15902c9916cdSFrançois Tigeot  * Note that this function can return -EDEADLK if the driver needed to acquire
15912c9916cdSFrançois Tigeot  * more locks but encountered a deadlock. The caller must then do the usual w/w
15922c9916cdSFrançois Tigeot  * backoff dance and restart. All other errors are fatal.
15932c9916cdSFrançois Tigeot  *
15942c9916cdSFrançois Tigeot  * Returns:
15952c9916cdSFrançois Tigeot  * 0 on success, negative error code on failure.
15962c9916cdSFrançois Tigeot  */
drm_atomic_check_only(struct drm_atomic_state * state)15972c9916cdSFrançois Tigeot int drm_atomic_check_only(struct drm_atomic_state *state)
15982c9916cdSFrançois Tigeot {
15992c9916cdSFrançois Tigeot 	struct drm_device *dev = state->dev;
16002c9916cdSFrançois Tigeot 	struct drm_mode_config *config = &dev->mode_config;
1601477eb7f9SFrançois Tigeot 	struct drm_plane *plane;
1602477eb7f9SFrançois Tigeot 	struct drm_plane_state *plane_state;
1603477eb7f9SFrançois Tigeot 	struct drm_crtc *crtc;
1604477eb7f9SFrançois Tigeot 	struct drm_crtc_state *crtc_state;
16052c9916cdSFrançois Tigeot 	int i, ret = 0;
16062c9916cdSFrançois Tigeot 
1607477eb7f9SFrançois Tigeot 	DRM_DEBUG_ATOMIC("checking %p\n", state);
16082c9916cdSFrançois Tigeot 
1609a85cb24fSFrançois Tigeot 	for_each_new_plane_in_state(state, plane, plane_state, i) {
1610477eb7f9SFrançois Tigeot 		ret = drm_atomic_plane_check(plane, plane_state);
16112c9916cdSFrançois Tigeot 		if (ret) {
1612aee94f86SFrançois Tigeot 			DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic core check failed\n",
1613aee94f86SFrançois Tigeot 					 plane->base.id, plane->name);
16142c9916cdSFrançois Tigeot 			return ret;
16152c9916cdSFrançois Tigeot 		}
16162c9916cdSFrançois Tigeot 	}
16172c9916cdSFrançois Tigeot 
1618a85cb24fSFrançois Tigeot 	for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
1619477eb7f9SFrançois Tigeot 		ret = drm_atomic_crtc_check(crtc, crtc_state);
16202c9916cdSFrançois Tigeot 		if (ret) {
1621aee94f86SFrançois Tigeot 			DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic core check failed\n",
1622aee94f86SFrançois Tigeot 					 crtc->base.id, crtc->name);
16232c9916cdSFrançois Tigeot 			return ret;
16242c9916cdSFrançois Tigeot 		}
16252c9916cdSFrançois Tigeot 	}
16262c9916cdSFrançois Tigeot 
16272c9916cdSFrançois Tigeot 	if (config->funcs->atomic_check)
16282c9916cdSFrançois Tigeot 		ret = config->funcs->atomic_check(state->dev, state);
16292c9916cdSFrançois Tigeot 
1630a85cb24fSFrançois Tigeot 	if (ret)
1631a85cb24fSFrançois Tigeot 		return ret;
1632a85cb24fSFrançois Tigeot 
16332c9916cdSFrançois Tigeot 	if (!state->allow_modeset) {
1634a85cb24fSFrançois Tigeot 		for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
163519c468b4SFrançois Tigeot 			if (drm_atomic_crtc_needs_modeset(crtc_state)) {
1636aee94f86SFrançois Tigeot 				DRM_DEBUG_ATOMIC("[CRTC:%d:%s] requires full modeset\n",
1637aee94f86SFrançois Tigeot 						 crtc->base.id, crtc->name);
16382c9916cdSFrançois Tigeot 				return -EINVAL;
16392c9916cdSFrançois Tigeot 			}
16402c9916cdSFrançois Tigeot 		}
16412c9916cdSFrançois Tigeot 	}
16422c9916cdSFrançois Tigeot 
1643a85cb24fSFrançois Tigeot 	return 0;
16442c9916cdSFrançois Tigeot }
16452c9916cdSFrançois Tigeot EXPORT_SYMBOL(drm_atomic_check_only);
16462c9916cdSFrançois Tigeot 
16472c9916cdSFrançois Tigeot /**
16482c9916cdSFrançois Tigeot  * drm_atomic_commit - commit configuration atomically
16492c9916cdSFrançois Tigeot  * @state: atomic configuration to check
16502c9916cdSFrançois Tigeot  *
16512c9916cdSFrançois Tigeot  * Note that this function can return -EDEADLK if the driver needed to acquire
16522c9916cdSFrançois Tigeot  * more locks but encountered a deadlock. The caller must then do the usual w/w
16532c9916cdSFrançois Tigeot  * backoff dance and restart. All other errors are fatal.
16542c9916cdSFrançois Tigeot  *
1655a85cb24fSFrançois Tigeot  * This function will take its own reference on @state.
1656a85cb24fSFrançois Tigeot  * Callers should always release their reference with drm_atomic_state_put().
16572c9916cdSFrançois Tigeot  *
16582c9916cdSFrançois Tigeot  * Returns:
16592c9916cdSFrançois Tigeot  * 0 on success, negative error code on failure.
16602c9916cdSFrançois Tigeot  */
drm_atomic_commit(struct drm_atomic_state * state)16612c9916cdSFrançois Tigeot int drm_atomic_commit(struct drm_atomic_state *state)
16622c9916cdSFrançois Tigeot {
16632c9916cdSFrançois Tigeot 	struct drm_mode_config *config = &state->dev->mode_config;
16642c9916cdSFrançois Tigeot 	int ret;
16652c9916cdSFrançois Tigeot 
16662c9916cdSFrançois Tigeot 	ret = drm_atomic_check_only(state);
16672c9916cdSFrançois Tigeot 	if (ret)
16682c9916cdSFrançois Tigeot 		return ret;
16692c9916cdSFrançois Tigeot 
1670*3f2dd94aSFrançois Tigeot 	DRM_DEBUG_ATOMIC("committing %p\n", state);
16712c9916cdSFrançois Tigeot 
16722c9916cdSFrançois Tigeot 	return config->funcs->atomic_commit(state->dev, state, false);
16732c9916cdSFrançois Tigeot }
16742c9916cdSFrançois Tigeot EXPORT_SYMBOL(drm_atomic_commit);
16752c9916cdSFrançois Tigeot 
16762c9916cdSFrançois Tigeot /**
1677a85cb24fSFrançois Tigeot  * drm_atomic_nonblocking_commit - atomic nonblocking commit
16782c9916cdSFrançois Tigeot  * @state: atomic configuration to check
16792c9916cdSFrançois Tigeot  *
16802c9916cdSFrançois Tigeot  * Note that this function can return -EDEADLK if the driver needed to acquire
16812c9916cdSFrançois Tigeot  * more locks but encountered a deadlock. The caller must then do the usual w/w
16822c9916cdSFrançois Tigeot  * backoff dance and restart. All other errors are fatal.
16832c9916cdSFrançois Tigeot  *
1684a85cb24fSFrançois Tigeot  * This function will take its own reference on @state.
1685a85cb24fSFrançois Tigeot  * Callers should always release their reference with drm_atomic_state_put().
16862c9916cdSFrançois Tigeot  *
16872c9916cdSFrançois Tigeot  * Returns:
16882c9916cdSFrançois Tigeot  * 0 on success, negative error code on failure.
16892c9916cdSFrançois Tigeot  */
drm_atomic_nonblocking_commit(struct drm_atomic_state * state)16908621f407SFrançois Tigeot int drm_atomic_nonblocking_commit(struct drm_atomic_state *state)
16912c9916cdSFrançois Tigeot {
16922c9916cdSFrançois Tigeot 	struct drm_mode_config *config = &state->dev->mode_config;
16932c9916cdSFrançois Tigeot 	int ret;
16942c9916cdSFrançois Tigeot 
16952c9916cdSFrançois Tigeot 	ret = drm_atomic_check_only(state);
16962c9916cdSFrançois Tigeot 	if (ret)
16972c9916cdSFrançois Tigeot 		return ret;
16982c9916cdSFrançois Tigeot 
1699*3f2dd94aSFrançois Tigeot 	DRM_DEBUG_ATOMIC("committing %p nonblocking\n", state);
17002c9916cdSFrançois Tigeot 
17012c9916cdSFrançois Tigeot 	return config->funcs->atomic_commit(state->dev, state, true);
17022c9916cdSFrançois Tigeot }
17038621f407SFrançois Tigeot EXPORT_SYMBOL(drm_atomic_nonblocking_commit);
17042c9916cdSFrançois Tigeot 
drm_atomic_print_state(const struct drm_atomic_state * state)17054be47400SFrançois Tigeot static void drm_atomic_print_state(const struct drm_atomic_state *state)
17064be47400SFrançois Tigeot {
17074be47400SFrançois Tigeot 	struct drm_printer p = drm_info_printer(state->dev->dev);
17084be47400SFrançois Tigeot 	struct drm_plane *plane;
17094be47400SFrançois Tigeot 	struct drm_plane_state *plane_state;
17104be47400SFrançois Tigeot 	struct drm_crtc *crtc;
17114be47400SFrançois Tigeot 	struct drm_crtc_state *crtc_state;
17124be47400SFrançois Tigeot 	struct drm_connector *connector;
17134be47400SFrançois Tigeot 	struct drm_connector_state *connector_state;
17144be47400SFrançois Tigeot 	int i;
17154be47400SFrançois Tigeot 
17164be47400SFrançois Tigeot 	DRM_DEBUG_ATOMIC("checking %p\n", state);
17174be47400SFrançois Tigeot 
1718a85cb24fSFrançois Tigeot 	for_each_new_plane_in_state(state, plane, plane_state, i)
17194be47400SFrançois Tigeot 		drm_atomic_plane_print_state(&p, plane_state);
17204be47400SFrançois Tigeot 
1721a85cb24fSFrançois Tigeot 	for_each_new_crtc_in_state(state, crtc, crtc_state, i)
17224be47400SFrançois Tigeot 		drm_atomic_crtc_print_state(&p, crtc_state);
17234be47400SFrançois Tigeot 
1724a85cb24fSFrançois Tigeot 	for_each_new_connector_in_state(state, connector, connector_state, i)
17254be47400SFrançois Tigeot 		drm_atomic_connector_print_state(&p, connector_state);
17264be47400SFrançois Tigeot }
17274be47400SFrançois Tigeot 
__drm_state_dump(struct drm_device * dev,struct drm_printer * p,bool take_locks)1728a85cb24fSFrançois Tigeot static void __drm_state_dump(struct drm_device *dev, struct drm_printer *p,
1729a85cb24fSFrançois Tigeot 			     bool take_locks)
1730a85cb24fSFrançois Tigeot {
1731a85cb24fSFrançois Tigeot 	struct drm_mode_config *config = &dev->mode_config;
1732a85cb24fSFrançois Tigeot 	struct drm_plane *plane;
1733a85cb24fSFrançois Tigeot 	struct drm_crtc *crtc;
1734a85cb24fSFrançois Tigeot 	struct drm_connector *connector;
1735a85cb24fSFrançois Tigeot 	struct drm_connector_list_iter conn_iter;
1736a85cb24fSFrançois Tigeot 
1737a85cb24fSFrançois Tigeot 	if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
1738a85cb24fSFrançois Tigeot 		return;
1739a85cb24fSFrançois Tigeot 
1740a85cb24fSFrançois Tigeot 	list_for_each_entry(plane, &config->plane_list, head) {
1741a85cb24fSFrançois Tigeot 		if (take_locks)
1742a85cb24fSFrançois Tigeot 			drm_modeset_lock(&plane->mutex, NULL);
1743a85cb24fSFrançois Tigeot 		drm_atomic_plane_print_state(p, plane->state);
1744a85cb24fSFrançois Tigeot 		if (take_locks)
1745a85cb24fSFrançois Tigeot 			drm_modeset_unlock(&plane->mutex);
1746a85cb24fSFrançois Tigeot 	}
1747a85cb24fSFrançois Tigeot 
1748a85cb24fSFrançois Tigeot 	list_for_each_entry(crtc, &config->crtc_list, head) {
1749a85cb24fSFrançois Tigeot 		if (take_locks)
1750a85cb24fSFrançois Tigeot 			drm_modeset_lock(&crtc->mutex, NULL);
1751a85cb24fSFrançois Tigeot 		drm_atomic_crtc_print_state(p, crtc->state);
1752a85cb24fSFrançois Tigeot 		if (take_locks)
1753a85cb24fSFrançois Tigeot 			drm_modeset_unlock(&crtc->mutex);
1754a85cb24fSFrançois Tigeot 	}
1755a85cb24fSFrançois Tigeot 
1756a85cb24fSFrançois Tigeot 	drm_connector_list_iter_begin(dev, &conn_iter);
1757a85cb24fSFrançois Tigeot 	if (take_locks)
1758a85cb24fSFrançois Tigeot 		drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
1759a85cb24fSFrançois Tigeot 	drm_for_each_connector_iter(connector, &conn_iter)
1760a85cb24fSFrançois Tigeot 		drm_atomic_connector_print_state(p, connector->state);
1761a85cb24fSFrançois Tigeot 	if (take_locks)
1762a85cb24fSFrançois Tigeot 		drm_modeset_unlock(&dev->mode_config.connection_mutex);
1763a85cb24fSFrançois Tigeot 	drm_connector_list_iter_end(&conn_iter);
1764a85cb24fSFrançois Tigeot }
1765a85cb24fSFrançois Tigeot 
17664be47400SFrançois Tigeot /**
17674be47400SFrançois Tigeot  * drm_state_dump - dump entire device atomic state
17684be47400SFrançois Tigeot  * @dev: the drm device
17694be47400SFrançois Tigeot  * @p: where to print the state to
17704be47400SFrançois Tigeot  *
17714be47400SFrançois Tigeot  * Just for debugging.  Drivers might want an option to dump state
17724be47400SFrançois Tigeot  * to dmesg in case of error irq's.  (Hint, you probably want to
17734be47400SFrançois Tigeot  * ratelimit this!)
17744be47400SFrançois Tigeot  *
17754be47400SFrançois Tigeot  * The caller must drm_modeset_lock_all(), or if this is called
17764be47400SFrançois Tigeot  * from error irq handler, it should not be enabled by default.
17774be47400SFrançois Tigeot  * (Ie. if you are debugging errors you might not care that this
17784be47400SFrançois Tigeot  * is racey.  But calling this without all modeset locks held is
17794be47400SFrançois Tigeot  * not inherently safe.)
17804be47400SFrançois Tigeot  */
drm_state_dump(struct drm_device * dev,struct drm_printer * p)17814be47400SFrançois Tigeot void drm_state_dump(struct drm_device *dev, struct drm_printer *p)
17824be47400SFrançois Tigeot {
1783a85cb24fSFrançois Tigeot 	__drm_state_dump(dev, p, false);
17844be47400SFrançois Tigeot }
17854be47400SFrançois Tigeot EXPORT_SYMBOL(drm_state_dump);
17864be47400SFrançois Tigeot 
17874be47400SFrançois Tigeot #ifdef CONFIG_DEBUG_FS
drm_state_info(struct seq_file * m,void * data)17884be47400SFrançois Tigeot static int drm_state_info(struct seq_file *m, void *data)
17894be47400SFrançois Tigeot {
17904be47400SFrançois Tigeot 	struct drm_info_node *node = (struct drm_info_node *) m->private;
17914be47400SFrançois Tigeot 	struct drm_device *dev = node->minor->dev;
17924be47400SFrançois Tigeot 	struct drm_printer p = drm_seq_file_printer(m);
17934be47400SFrançois Tigeot 
1794a85cb24fSFrançois Tigeot 	__drm_state_dump(dev, &p, true);
17954be47400SFrançois Tigeot 
17964be47400SFrançois Tigeot 	return 0;
17974be47400SFrançois Tigeot }
17984be47400SFrançois Tigeot 
17994be47400SFrançois Tigeot /* any use in debugfs files to dump individual planes/crtc/etc? */
18004be47400SFrançois Tigeot static const struct drm_info_list drm_atomic_debugfs_list[] = {
18014be47400SFrançois Tigeot 	{"state", drm_state_info, 0},
18024be47400SFrançois Tigeot };
18034be47400SFrançois Tigeot 
drm_atomic_debugfs_init(struct drm_minor * minor)18044be47400SFrançois Tigeot int drm_atomic_debugfs_init(struct drm_minor *minor)
18054be47400SFrançois Tigeot {
18064be47400SFrançois Tigeot 	return drm_debugfs_create_files(drm_atomic_debugfs_list,
18074be47400SFrançois Tigeot 			ARRAY_SIZE(drm_atomic_debugfs_list),
18084be47400SFrançois Tigeot 			minor->debugfs_root, minor);
18094be47400SFrançois Tigeot }
18104be47400SFrançois Tigeot #endif
18114be47400SFrançois Tigeot 
18122c9916cdSFrançois Tigeot /*
18132c9916cdSFrançois Tigeot  * The big monstor ioctl
18142c9916cdSFrançois Tigeot  */
18152c9916cdSFrançois Tigeot 
create_vblank_event(struct drm_crtc * crtc,uint64_t user_data)18162c9916cdSFrançois Tigeot static struct drm_pending_vblank_event *create_vblank_event(
1817*3f2dd94aSFrançois Tigeot 		struct drm_crtc *crtc, uint64_t user_data)
18182c9916cdSFrançois Tigeot {
18192c9916cdSFrançois Tigeot 	struct drm_pending_vblank_event *e = NULL;
18202c9916cdSFrançois Tigeot 
18212c9916cdSFrançois Tigeot 	e = kzalloc(sizeof *e, GFP_KERNEL);
1822c0e85e96SFrançois Tigeot 	if (!e)
1823c0e85e96SFrançois Tigeot 		return NULL;
18242c9916cdSFrançois Tigeot 
18252c9916cdSFrançois Tigeot 	e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
1826c0e85e96SFrançois Tigeot 	e->event.base.length = sizeof(e->event);
1827*3f2dd94aSFrançois Tigeot 	e->event.vbl.crtc_id = crtc->base.id;
1828*3f2dd94aSFrançois Tigeot 	e->event.vbl.user_data = user_data;
18292c9916cdSFrançois Tigeot 
1830c0e85e96SFrançois Tigeot 	return e;
18312c9916cdSFrançois Tigeot }
18322c9916cdSFrançois Tigeot 
drm_atomic_connector_commit_dpms(struct drm_atomic_state * state,struct drm_connector * connector,int mode)1833*3f2dd94aSFrançois Tigeot int drm_atomic_connector_commit_dpms(struct drm_atomic_state *state,
1834*3f2dd94aSFrançois Tigeot 				     struct drm_connector *connector,
1835*3f2dd94aSFrançois Tigeot 				     int mode)
1836*3f2dd94aSFrançois Tigeot {
1837*3f2dd94aSFrançois Tigeot 	struct drm_connector *tmp_connector;
1838*3f2dd94aSFrançois Tigeot 	struct drm_connector_state *new_conn_state;
1839*3f2dd94aSFrançois Tigeot 	struct drm_crtc *crtc;
1840*3f2dd94aSFrançois Tigeot 	struct drm_crtc_state *crtc_state;
1841*3f2dd94aSFrançois Tigeot 	int i, ret, old_mode = connector->dpms;
1842*3f2dd94aSFrançois Tigeot 	bool active = false;
1843*3f2dd94aSFrançois Tigeot 
1844*3f2dd94aSFrançois Tigeot 	ret = drm_modeset_lock(&state->dev->mode_config.connection_mutex,
1845*3f2dd94aSFrançois Tigeot 			       state->acquire_ctx);
1846*3f2dd94aSFrançois Tigeot 	if (ret)
1847*3f2dd94aSFrançois Tigeot 		return ret;
1848*3f2dd94aSFrançois Tigeot 
1849*3f2dd94aSFrançois Tigeot 	if (mode != DRM_MODE_DPMS_ON)
1850*3f2dd94aSFrançois Tigeot 		mode = DRM_MODE_DPMS_OFF;
1851*3f2dd94aSFrançois Tigeot 	connector->dpms = mode;
1852*3f2dd94aSFrançois Tigeot 
1853*3f2dd94aSFrançois Tigeot 	crtc = connector->state->crtc;
1854*3f2dd94aSFrançois Tigeot 	if (!crtc)
1855*3f2dd94aSFrançois Tigeot 		goto out;
1856*3f2dd94aSFrançois Tigeot 	ret = drm_atomic_add_affected_connectors(state, crtc);
1857*3f2dd94aSFrançois Tigeot 	if (ret)
1858*3f2dd94aSFrançois Tigeot 		goto out;
1859*3f2dd94aSFrançois Tigeot 
1860*3f2dd94aSFrançois Tigeot 	crtc_state = drm_atomic_get_crtc_state(state, crtc);
1861*3f2dd94aSFrançois Tigeot 	if (IS_ERR(crtc_state)) {
1862*3f2dd94aSFrançois Tigeot 		ret = PTR_ERR(crtc_state);
1863*3f2dd94aSFrançois Tigeot 		goto out;
1864*3f2dd94aSFrançois Tigeot 	}
1865*3f2dd94aSFrançois Tigeot 
1866*3f2dd94aSFrançois Tigeot 	for_each_new_connector_in_state(state, tmp_connector, new_conn_state, i) {
1867*3f2dd94aSFrançois Tigeot 		if (new_conn_state->crtc != crtc)
1868*3f2dd94aSFrançois Tigeot 			continue;
1869*3f2dd94aSFrançois Tigeot 		if (tmp_connector->dpms == DRM_MODE_DPMS_ON) {
1870*3f2dd94aSFrançois Tigeot 			active = true;
1871*3f2dd94aSFrançois Tigeot 			break;
1872*3f2dd94aSFrançois Tigeot 		}
1873*3f2dd94aSFrançois Tigeot 	}
1874*3f2dd94aSFrançois Tigeot 
1875*3f2dd94aSFrançois Tigeot 	crtc_state->active = active;
1876*3f2dd94aSFrançois Tigeot 	ret = drm_atomic_commit(state);
1877*3f2dd94aSFrançois Tigeot out:
1878*3f2dd94aSFrançois Tigeot 	if (ret != 0)
1879*3f2dd94aSFrançois Tigeot 		connector->dpms = old_mode;
1880*3f2dd94aSFrançois Tigeot 	return ret;
1881*3f2dd94aSFrançois Tigeot }
1882*3f2dd94aSFrançois Tigeot 
drm_atomic_set_property(struct drm_atomic_state * state,struct drm_mode_object * obj,struct drm_property * prop,uint64_t prop_value)1883*3f2dd94aSFrançois Tigeot int drm_atomic_set_property(struct drm_atomic_state *state,
1884*3f2dd94aSFrançois Tigeot 			    struct drm_mode_object *obj,
1885*3f2dd94aSFrançois Tigeot 			    struct drm_property *prop,
18862c9916cdSFrançois Tigeot 			    uint64_t prop_value)
18872c9916cdSFrançois Tigeot {
18882c9916cdSFrançois Tigeot 	struct drm_mode_object *ref;
18892c9916cdSFrançois Tigeot 	int ret;
18902c9916cdSFrançois Tigeot 
18912c9916cdSFrançois Tigeot 	if (!drm_property_change_valid_get(prop, prop_value, &ref))
18922c9916cdSFrançois Tigeot 		return -EINVAL;
18932c9916cdSFrançois Tigeot 
18942c9916cdSFrançois Tigeot 	switch (obj->type) {
18952c9916cdSFrançois Tigeot 	case DRM_MODE_OBJECT_CONNECTOR: {
18962c9916cdSFrançois Tigeot 		struct drm_connector *connector = obj_to_connector(obj);
18972c9916cdSFrançois Tigeot 		struct drm_connector_state *connector_state;
18982c9916cdSFrançois Tigeot 
18992c9916cdSFrançois Tigeot 		connector_state = drm_atomic_get_connector_state(state, connector);
19002c9916cdSFrançois Tigeot 		if (IS_ERR(connector_state)) {
19012c9916cdSFrançois Tigeot 			ret = PTR_ERR(connector_state);
19022c9916cdSFrançois Tigeot 			break;
19032c9916cdSFrançois Tigeot 		}
19042c9916cdSFrançois Tigeot 
19052c9916cdSFrançois Tigeot 		ret = drm_atomic_connector_set_property(connector,
19062c9916cdSFrançois Tigeot 				connector_state, prop, prop_value);
19072c9916cdSFrançois Tigeot 		break;
19082c9916cdSFrançois Tigeot 	}
19092c9916cdSFrançois Tigeot 	case DRM_MODE_OBJECT_CRTC: {
19102c9916cdSFrançois Tigeot 		struct drm_crtc *crtc = obj_to_crtc(obj);
19112c9916cdSFrançois Tigeot 		struct drm_crtc_state *crtc_state;
19122c9916cdSFrançois Tigeot 
19132c9916cdSFrançois Tigeot 		crtc_state = drm_atomic_get_crtc_state(state, crtc);
19142c9916cdSFrançois Tigeot 		if (IS_ERR(crtc_state)) {
19152c9916cdSFrançois Tigeot 			ret = PTR_ERR(crtc_state);
19162c9916cdSFrançois Tigeot 			break;
19172c9916cdSFrançois Tigeot 		}
19182c9916cdSFrançois Tigeot 
19192c9916cdSFrançois Tigeot 		ret = drm_atomic_crtc_set_property(crtc,
19202c9916cdSFrançois Tigeot 				crtc_state, prop, prop_value);
19212c9916cdSFrançois Tigeot 		break;
19222c9916cdSFrançois Tigeot 	}
19232c9916cdSFrançois Tigeot 	case DRM_MODE_OBJECT_PLANE: {
19242c9916cdSFrançois Tigeot 		struct drm_plane *plane = obj_to_plane(obj);
19252c9916cdSFrançois Tigeot 		struct drm_plane_state *plane_state;
19262c9916cdSFrançois Tigeot 
19272c9916cdSFrançois Tigeot 		plane_state = drm_atomic_get_plane_state(state, plane);
19282c9916cdSFrançois Tigeot 		if (IS_ERR(plane_state)) {
19292c9916cdSFrançois Tigeot 			ret = PTR_ERR(plane_state);
19302c9916cdSFrançois Tigeot 			break;
19312c9916cdSFrançois Tigeot 		}
19322c9916cdSFrançois Tigeot 
19332c9916cdSFrançois Tigeot 		ret = drm_atomic_plane_set_property(plane,
19342c9916cdSFrançois Tigeot 				plane_state, prop, prop_value);
19352c9916cdSFrançois Tigeot 		break;
19362c9916cdSFrançois Tigeot 	}
19372c9916cdSFrançois Tigeot 	default:
19382c9916cdSFrançois Tigeot 		ret = -EINVAL;
19392c9916cdSFrançois Tigeot 		break;
19402c9916cdSFrançois Tigeot 	}
19412c9916cdSFrançois Tigeot 
19422c9916cdSFrançois Tigeot 	drm_property_change_valid_put(prop, ref);
19432c9916cdSFrançois Tigeot 	return ret;
19442c9916cdSFrançois Tigeot }
19452c9916cdSFrançois Tigeot 
1946352ff8bdSFrançois Tigeot /**
1947aee94f86SFrançois Tigeot  * drm_atomic_clean_old_fb -- Unset old_fb pointers and set plane->fb pointers.
1948352ff8bdSFrançois Tigeot  *
1949352ff8bdSFrançois Tigeot  * @dev: drm device to check.
1950352ff8bdSFrançois Tigeot  * @plane_mask: plane mask for planes that were updated.
1951352ff8bdSFrançois Tigeot  * @ret: return value, can be -EDEADLK for a retry.
1952352ff8bdSFrançois Tigeot  *
1953a85cb24fSFrançois Tigeot  * Before doing an update &drm_plane.old_fb is set to &drm_plane.fb, but before
1954a85cb24fSFrançois Tigeot  * dropping the locks old_fb needs to be set to NULL and plane->fb updated. This
1955a85cb24fSFrançois Tigeot  * is a common operation for each atomic update, so this call is split off as a
1956a85cb24fSFrançois Tigeot  * helper.
1957352ff8bdSFrançois Tigeot  */
drm_atomic_clean_old_fb(struct drm_device * dev,unsigned plane_mask,int ret)1958352ff8bdSFrançois Tigeot void drm_atomic_clean_old_fb(struct drm_device *dev,
1959352ff8bdSFrançois Tigeot 			     unsigned plane_mask,
1960352ff8bdSFrançois Tigeot 			     int ret)
1961352ff8bdSFrançois Tigeot {
1962352ff8bdSFrançois Tigeot 	struct drm_plane *plane;
1963352ff8bdSFrançois Tigeot 
1964352ff8bdSFrançois Tigeot 	/* if succeeded, fixup legacy plane crtc/fb ptrs before dropping
1965352ff8bdSFrançois Tigeot 	 * locks (ie. while it is still safe to deref plane->state).  We
1966352ff8bdSFrançois Tigeot 	 * need to do this here because the driver entry points cannot
1967352ff8bdSFrançois Tigeot 	 * distinguish between legacy and atomic ioctls.
1968352ff8bdSFrançois Tigeot 	 */
1969352ff8bdSFrançois Tigeot 	drm_for_each_plane_mask(plane, dev, plane_mask) {
1970352ff8bdSFrançois Tigeot 		if (ret == 0) {
1971352ff8bdSFrançois Tigeot 			struct drm_framebuffer *new_fb = plane->state->fb;
1972352ff8bdSFrançois Tigeot 			if (new_fb)
1973a85cb24fSFrançois Tigeot 				drm_framebuffer_get(new_fb);
1974352ff8bdSFrançois Tigeot 			plane->fb = new_fb;
1975352ff8bdSFrançois Tigeot 			plane->crtc = plane->state->crtc;
1976352ff8bdSFrançois Tigeot 
1977352ff8bdSFrançois Tigeot 			if (plane->old_fb)
1978a85cb24fSFrançois Tigeot 				drm_framebuffer_put(plane->old_fb);
1979352ff8bdSFrançois Tigeot 		}
1980352ff8bdSFrançois Tigeot 		plane->old_fb = NULL;
1981352ff8bdSFrançois Tigeot 	}
1982352ff8bdSFrançois Tigeot }
1983352ff8bdSFrançois Tigeot EXPORT_SYMBOL(drm_atomic_clean_old_fb);
1984352ff8bdSFrançois Tigeot 
19854be47400SFrançois Tigeot /**
19864be47400SFrançois Tigeot  * DOC: explicit fencing properties
19874be47400SFrançois Tigeot  *
19884be47400SFrançois Tigeot  * Explicit fencing allows userspace to control the buffer synchronization
19894be47400SFrançois Tigeot  * between devices. A Fence or a group of fences are transfered to/from
19904be47400SFrançois Tigeot  * userspace using Sync File fds and there are two DRM properties for that.
19914be47400SFrançois Tigeot  * IN_FENCE_FD on each DRM Plane to send fences to the kernel and
19924be47400SFrançois Tigeot  * OUT_FENCE_PTR on each DRM CRTC to receive fences from the kernel.
19934be47400SFrançois Tigeot  *
19944be47400SFrançois Tigeot  * As a contrast, with implicit fencing the kernel keeps track of any
19954be47400SFrançois Tigeot  * ongoing rendering, and automatically ensures that the atomic update waits
19964be47400SFrançois Tigeot  * for any pending rendering to complete. For shared buffers represented with
1997a85cb24fSFrançois Tigeot  * a &struct dma_buf this is tracked in &struct reservation_object.
19984be47400SFrançois Tigeot  * Implicit syncing is how Linux traditionally worked (e.g. DRI2/3 on X.org),
19994be47400SFrançois Tigeot  * whereas explicit fencing is what Android wants.
20004be47400SFrançois Tigeot  *
20014be47400SFrançois Tigeot  * "IN_FENCE_FD”:
20024be47400SFrançois Tigeot  *	Use this property to pass a fence that DRM should wait on before
20034be47400SFrançois Tigeot  *	proceeding with the Atomic Commit request and show the framebuffer for
20044be47400SFrançois Tigeot  *	the plane on the screen. The fence can be either a normal fence or a
20054be47400SFrançois Tigeot  *	merged one, the sync_file framework will handle both cases and use a
20064be47400SFrançois Tigeot  *	fence_array if a merged fence is received. Passing -1 here means no
20074be47400SFrançois Tigeot  *	fences to wait on.
20084be47400SFrançois Tigeot  *
20094be47400SFrançois Tigeot  *	If the Atomic Commit request has the DRM_MODE_ATOMIC_TEST_ONLY flag
20104be47400SFrançois Tigeot  *	it will only check if the Sync File is a valid one.
20114be47400SFrançois Tigeot  *
20124be47400SFrançois Tigeot  *	On the driver side the fence is stored on the @fence parameter of
2013a85cb24fSFrançois Tigeot  *	&struct drm_plane_state. Drivers which also support implicit fencing
20144be47400SFrançois Tigeot  *	should set the implicit fence using drm_atomic_set_fence_for_plane(),
20154be47400SFrançois Tigeot  *	to make sure there's consistent behaviour between drivers in precedence
20164be47400SFrançois Tigeot  *	of implicit vs. explicit fencing.
20174be47400SFrançois Tigeot  *
20184be47400SFrançois Tigeot  * "OUT_FENCE_PTR”:
20194be47400SFrançois Tigeot  *	Use this property to pass a file descriptor pointer to DRM. Once the
20204be47400SFrançois Tigeot  *	Atomic Commit request call returns OUT_FENCE_PTR will be filled with
20214be47400SFrançois Tigeot  *	the file descriptor number of a Sync File. This Sync File contains the
20224be47400SFrançois Tigeot  *	CRTC fence that will be signaled when all framebuffers present on the
20234be47400SFrançois Tigeot  *	Atomic Commit * request for that given CRTC are scanned out on the
20244be47400SFrançois Tigeot  *	screen.
20254be47400SFrançois Tigeot  *
20264be47400SFrançois Tigeot  *	The Atomic Commit request fails if a invalid pointer is passed. If the
20274be47400SFrançois Tigeot  *	Atomic Commit request fails for any other reason the out fence fd
20284be47400SFrançois Tigeot  *	returned will be -1. On a Atomic Commit with the
20294be47400SFrançois Tigeot  *	DRM_MODE_ATOMIC_TEST_ONLY flag the out fence will also be set to -1.
20304be47400SFrançois Tigeot  *
20314be47400SFrançois Tigeot  *	Note that out-fences don't have a special interface to drivers and are
2032a85cb24fSFrançois Tigeot  *	internally represented by a &struct drm_pending_vblank_event in struct
20334be47400SFrançois Tigeot  *	&drm_crtc_state, which is also used by the nonblocking atomic commit
20344be47400SFrançois Tigeot  *	helpers and for the DRM event handling for existing userspace.
20354be47400SFrançois Tigeot  */
20364be47400SFrançois Tigeot 
20374be47400SFrançois Tigeot struct drm_out_fence_state {
20384be47400SFrançois Tigeot 	s32 __user *out_fence_ptr;
20394be47400SFrançois Tigeot 	struct sync_file *sync_file;
20404be47400SFrançois Tigeot 	int fd;
20414be47400SFrançois Tigeot };
20424be47400SFrançois Tigeot 
setup_out_fence(struct drm_out_fence_state * fence_state,struct dma_fence * fence)20434be47400SFrançois Tigeot static int setup_out_fence(struct drm_out_fence_state *fence_state,
20444be47400SFrançois Tigeot 			   struct dma_fence *fence)
20454be47400SFrançois Tigeot {
20464be47400SFrançois Tigeot 	fence_state->fd = get_unused_fd_flags(O_CLOEXEC);
20474be47400SFrançois Tigeot 	if (fence_state->fd < 0)
20484be47400SFrançois Tigeot 		return fence_state->fd;
20494be47400SFrançois Tigeot 
20504be47400SFrançois Tigeot 	if (put_user(fence_state->fd, fence_state->out_fence_ptr))
20514be47400SFrançois Tigeot 		return -EFAULT;
20524be47400SFrançois Tigeot 
20534be47400SFrançois Tigeot 	fence_state->sync_file = sync_file_create(fence);
20544be47400SFrançois Tigeot 	if (!fence_state->sync_file)
20554be47400SFrançois Tigeot 		return -ENOMEM;
20564be47400SFrançois Tigeot 
20574be47400SFrançois Tigeot 	return 0;
20584be47400SFrançois Tigeot }
20594be47400SFrançois Tigeot 
prepare_crtc_signaling(struct drm_device * dev,struct drm_atomic_state * state,struct drm_mode_atomic * arg,struct drm_file * file_priv,struct drm_out_fence_state ** fence_state,unsigned int * num_fences)20604be47400SFrançois Tigeot static int prepare_crtc_signaling(struct drm_device *dev,
20614be47400SFrançois Tigeot 				  struct drm_atomic_state *state,
20624be47400SFrançois Tigeot 				  struct drm_mode_atomic *arg,
20634be47400SFrançois Tigeot 				  struct drm_file *file_priv,
20644be47400SFrançois Tigeot 				  struct drm_out_fence_state **fence_state,
20654be47400SFrançois Tigeot 				  unsigned int *num_fences)
20664be47400SFrançois Tigeot {
20674be47400SFrançois Tigeot 	struct drm_crtc *crtc;
20684be47400SFrançois Tigeot 	struct drm_crtc_state *crtc_state;
2069*3f2dd94aSFrançois Tigeot 	int i, c = 0, ret;
20704be47400SFrançois Tigeot 
20714be47400SFrançois Tigeot 	if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)
20724be47400SFrançois Tigeot 		return 0;
20734be47400SFrançois Tigeot 
2074a85cb24fSFrançois Tigeot 	for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
20754be47400SFrançois Tigeot 		s32 __user *fence_ptr;
20764be47400SFrançois Tigeot 
20774be47400SFrançois Tigeot 		fence_ptr = get_out_fence_for_crtc(crtc_state->state, crtc);
20784be47400SFrançois Tigeot 
20794be47400SFrançois Tigeot 		if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT || fence_ptr) {
20804be47400SFrançois Tigeot 			struct drm_pending_vblank_event *e;
20814be47400SFrançois Tigeot 
2082*3f2dd94aSFrançois Tigeot 			e = create_vblank_event(crtc, arg->user_data);
20834be47400SFrançois Tigeot 			if (!e)
20844be47400SFrançois Tigeot 				return -ENOMEM;
20854be47400SFrançois Tigeot 
20864be47400SFrançois Tigeot 			crtc_state->event = e;
20874be47400SFrançois Tigeot 		}
20884be47400SFrançois Tigeot 
20894be47400SFrançois Tigeot 		if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
20904be47400SFrançois Tigeot 			struct drm_pending_vblank_event *e = crtc_state->event;
20914be47400SFrançois Tigeot 
20924be47400SFrançois Tigeot 			if (!file_priv)
20934be47400SFrançois Tigeot 				continue;
20944be47400SFrançois Tigeot 
20954be47400SFrançois Tigeot 			ret = drm_event_reserve_init(dev, file_priv, &e->base,
20964be47400SFrançois Tigeot 						     &e->event.base);
20974be47400SFrançois Tigeot 			if (ret) {
20984be47400SFrançois Tigeot 				kfree(e);
20994be47400SFrançois Tigeot 				crtc_state->event = NULL;
21004be47400SFrançois Tigeot 				return ret;
21014be47400SFrançois Tigeot 			}
21024be47400SFrançois Tigeot 		}
21034be47400SFrançois Tigeot 
21044be47400SFrançois Tigeot 		if (fence_ptr) {
21054be47400SFrançois Tigeot 			struct dma_fence *fence;
21064be47400SFrançois Tigeot 			struct drm_out_fence_state *f;
21074be47400SFrançois Tigeot 
21084be47400SFrançois Tigeot 			f = krealloc(*fence_state,
21094be47400SFrançois Tigeot 				     sizeof(**fence_state) * (*num_fences + 1),
21104be47400SFrançois Tigeot 				     M_DRM, GFP_KERNEL);
21114be47400SFrançois Tigeot 			if (!f)
21124be47400SFrançois Tigeot 				return -ENOMEM;
21134be47400SFrançois Tigeot 
21144be47400SFrançois Tigeot 			memset(&f[*num_fences], 0, sizeof(*f));
21154be47400SFrançois Tigeot 
21164be47400SFrançois Tigeot 			f[*num_fences].out_fence_ptr = fence_ptr;
21174be47400SFrançois Tigeot 			*fence_state = f;
21184be47400SFrançois Tigeot 
21194be47400SFrançois Tigeot 			fence = drm_crtc_create_fence(crtc);
21204be47400SFrançois Tigeot 			if (!fence)
21214be47400SFrançois Tigeot 				return -ENOMEM;
21224be47400SFrançois Tigeot 
21234be47400SFrançois Tigeot 			ret = setup_out_fence(&f[(*num_fences)++], fence);
21244be47400SFrançois Tigeot 			if (ret) {
21254be47400SFrançois Tigeot 				dma_fence_put(fence);
21264be47400SFrançois Tigeot 				return ret;
21274be47400SFrançois Tigeot 			}
21284be47400SFrançois Tigeot 
21294be47400SFrançois Tigeot 			crtc_state->event->base.fence = fence;
21304be47400SFrançois Tigeot 		}
2131*3f2dd94aSFrançois Tigeot 
2132*3f2dd94aSFrançois Tigeot 		c++;
21334be47400SFrançois Tigeot 	}
21344be47400SFrançois Tigeot 
2135*3f2dd94aSFrançois Tigeot 	/*
2136*3f2dd94aSFrançois Tigeot 	 * Having this flag means user mode pends on event which will never
2137*3f2dd94aSFrançois Tigeot 	 * reach due to lack of at least one CRTC for signaling
2138*3f2dd94aSFrançois Tigeot 	 */
2139*3f2dd94aSFrançois Tigeot 	if (c == 0 && (arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
2140*3f2dd94aSFrançois Tigeot 		return -EINVAL;
2141*3f2dd94aSFrançois Tigeot 
21424be47400SFrançois Tigeot 	return 0;
21434be47400SFrançois Tigeot }
21444be47400SFrançois Tigeot 
complete_crtc_signaling(struct drm_device * dev,struct drm_atomic_state * state,struct drm_out_fence_state * fence_state,unsigned int num_fences,bool install_fds)21454be47400SFrançois Tigeot static void complete_crtc_signaling(struct drm_device *dev,
21464be47400SFrançois Tigeot 				    struct drm_atomic_state *state,
21474be47400SFrançois Tigeot 				    struct drm_out_fence_state *fence_state,
21484be47400SFrançois Tigeot 				    unsigned int num_fences,
21494be47400SFrançois Tigeot 				    bool install_fds)
21504be47400SFrançois Tigeot {
21514be47400SFrançois Tigeot 	struct drm_crtc *crtc;
21524be47400SFrançois Tigeot 	struct drm_crtc_state *crtc_state;
21534be47400SFrançois Tigeot 	int i;
21544be47400SFrançois Tigeot 
21554be47400SFrançois Tigeot 	if (install_fds) {
21564be47400SFrançois Tigeot 		for (i = 0; i < num_fences; i++)
21574be47400SFrançois Tigeot 			fd_install(fence_state[i].fd,
21584be47400SFrançois Tigeot 				   fence_state[i].sync_file->file);
21594be47400SFrançois Tigeot 
21604be47400SFrançois Tigeot 		kfree(fence_state);
21614be47400SFrançois Tigeot 		return;
21624be47400SFrançois Tigeot 	}
21634be47400SFrançois Tigeot 
2164a85cb24fSFrançois Tigeot 	for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
21654be47400SFrançois Tigeot 		struct drm_pending_vblank_event *event = crtc_state->event;
21664be47400SFrançois Tigeot 		/*
21674be47400SFrançois Tigeot 		 * Free the allocated event. drm_atomic_helper_setup_commit
21684be47400SFrançois Tigeot 		 * can allocate an event too, so only free it if it's ours
21694be47400SFrançois Tigeot 		 * to prevent a double free in drm_atomic_state_clear.
21704be47400SFrançois Tigeot 		 */
21714be47400SFrançois Tigeot 		if (event && (event->base.fence || event->base.file_priv)) {
21724be47400SFrançois Tigeot 			drm_event_cancel_free(dev, &event->base);
21734be47400SFrançois Tigeot 			crtc_state->event = NULL;
21744be47400SFrançois Tigeot 		}
21754be47400SFrançois Tigeot 	}
21764be47400SFrançois Tigeot 
21774be47400SFrançois Tigeot 	if (!fence_state)
21784be47400SFrançois Tigeot 		return;
21794be47400SFrançois Tigeot 
21804be47400SFrançois Tigeot 	for (i = 0; i < num_fences; i++) {
21814be47400SFrançois Tigeot 		if (fence_state[i].sync_file)
21824be47400SFrançois Tigeot 			fput(fence_state[i].sync_file->file);
21834be47400SFrançois Tigeot 		if (fence_state[i].fd >= 0)
21844be47400SFrançois Tigeot 			put_unused_fd(fence_state[i].fd);
21854be47400SFrançois Tigeot 
21864be47400SFrançois Tigeot 		/* If this fails log error to the user */
21874be47400SFrançois Tigeot 		if (fence_state[i].out_fence_ptr &&
21884be47400SFrançois Tigeot 		    put_user(-1, fence_state[i].out_fence_ptr))
21894be47400SFrançois Tigeot 			DRM_DEBUG_ATOMIC("Couldn't clear out_fence_ptr\n");
21904be47400SFrançois Tigeot 	}
21914be47400SFrançois Tigeot 
21924be47400SFrançois Tigeot 	kfree(fence_state);
21934be47400SFrançois Tigeot }
21944be47400SFrançois Tigeot 
drm_mode_atomic_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)21952c9916cdSFrançois Tigeot int drm_mode_atomic_ioctl(struct drm_device *dev,
21962c9916cdSFrançois Tigeot 			  void *data, struct drm_file *file_priv)
21972c9916cdSFrançois Tigeot {
21982c9916cdSFrançois Tigeot 	struct drm_mode_atomic *arg = data;
21992c9916cdSFrançois Tigeot 	uint32_t __user *objs_ptr = (uint32_t __user *)(unsigned long)(arg->objs_ptr);
22002c9916cdSFrançois Tigeot 	uint32_t __user *count_props_ptr = (uint32_t __user *)(unsigned long)(arg->count_props_ptr);
22012c9916cdSFrançois Tigeot 	uint32_t __user *props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
22022c9916cdSFrançois Tigeot 	uint64_t __user *prop_values_ptr = (uint64_t __user *)(unsigned long)(arg->prop_values_ptr);
22032c9916cdSFrançois Tigeot 	unsigned int copied_objs, copied_props;
22042c9916cdSFrançois Tigeot 	struct drm_atomic_state *state;
22052c9916cdSFrançois Tigeot 	struct drm_modeset_acquire_ctx ctx;
22062c9916cdSFrançois Tigeot 	struct drm_plane *plane;
2207a85cb24fSFrançois Tigeot 	struct drm_out_fence_state *fence_state;
2208352ff8bdSFrançois Tigeot 	unsigned plane_mask;
22092c9916cdSFrançois Tigeot 	int ret = 0;
2210a85cb24fSFrançois Tigeot 	unsigned int i, j, num_fences;
22112c9916cdSFrançois Tigeot 
22122c9916cdSFrançois Tigeot 	/* disallow for drivers not supporting atomic: */
22132c9916cdSFrançois Tigeot 	if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
22142c9916cdSFrançois Tigeot 		return -EINVAL;
22152c9916cdSFrançois Tigeot 
22162c9916cdSFrançois Tigeot 	/* disallow for userspace that has not enabled atomic cap (even
22172c9916cdSFrançois Tigeot 	 * though this may be a bit overkill, since legacy userspace
22182c9916cdSFrançois Tigeot 	 * wouldn't know how to call this ioctl)
22192c9916cdSFrançois Tigeot 	 */
22202c9916cdSFrançois Tigeot 	if (!file_priv->atomic)
22212c9916cdSFrançois Tigeot 		return -EINVAL;
22222c9916cdSFrançois Tigeot 
22232c9916cdSFrançois Tigeot 	if (arg->flags & ~DRM_MODE_ATOMIC_FLAGS)
22242c9916cdSFrançois Tigeot 		return -EINVAL;
22252c9916cdSFrançois Tigeot 
22262c9916cdSFrançois Tigeot 	if (arg->reserved)
22272c9916cdSFrançois Tigeot 		return -EINVAL;
22282c9916cdSFrançois Tigeot 
22292c9916cdSFrançois Tigeot 	if ((arg->flags & DRM_MODE_PAGE_FLIP_ASYNC) &&
22302c9916cdSFrançois Tigeot 			!dev->mode_config.async_page_flip)
22312c9916cdSFrançois Tigeot 		return -EINVAL;
22322c9916cdSFrançois Tigeot 
22332c9916cdSFrançois Tigeot 	/* can't test and expect an event at the same time. */
22342c9916cdSFrançois Tigeot 	if ((arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) &&
22352c9916cdSFrançois Tigeot 			(arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
22362c9916cdSFrançois Tigeot 		return -EINVAL;
22372c9916cdSFrançois Tigeot 
2238*3f2dd94aSFrançois Tigeot 	drm_modeset_acquire_init(&ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE);
22392c9916cdSFrançois Tigeot 
22402c9916cdSFrançois Tigeot 	state = drm_atomic_state_alloc(dev);
22412c9916cdSFrançois Tigeot 	if (!state)
22422c9916cdSFrançois Tigeot 		return -ENOMEM;
22432c9916cdSFrançois Tigeot 
22442c9916cdSFrançois Tigeot 	state->acquire_ctx = &ctx;
22452c9916cdSFrançois Tigeot 	state->allow_modeset = !!(arg->flags & DRM_MODE_ATOMIC_ALLOW_MODESET);
22462c9916cdSFrançois Tigeot 
22472c9916cdSFrançois Tigeot retry:
2248352ff8bdSFrançois Tigeot 	plane_mask = 0;
22492c9916cdSFrançois Tigeot 	copied_objs = 0;
22502c9916cdSFrançois Tigeot 	copied_props = 0;
2251a85cb24fSFrançois Tigeot 	fence_state = NULL;
2252a85cb24fSFrançois Tigeot 	num_fences = 0;
22532c9916cdSFrançois Tigeot 
22542c9916cdSFrançois Tigeot 	for (i = 0; i < arg->count_objs; i++) {
22552c9916cdSFrançois Tigeot 		uint32_t obj_id, count_props;
22562c9916cdSFrançois Tigeot 		struct drm_mode_object *obj;
22572c9916cdSFrançois Tigeot 
22582c9916cdSFrançois Tigeot 		if (get_user(obj_id, objs_ptr + copied_objs)) {
22592c9916cdSFrançois Tigeot 			ret = -EFAULT;
2260a05eeebfSFrançois Tigeot 			goto out;
22612c9916cdSFrançois Tigeot 		}
22622c9916cdSFrançois Tigeot 
2263*3f2dd94aSFrançois Tigeot 		obj = drm_mode_object_find(dev, file_priv, obj_id, DRM_MODE_OBJECT_ANY);
22648621f407SFrançois Tigeot 		if (!obj) {
22658621f407SFrançois Tigeot 			ret = -ENOENT;
22668621f407SFrançois Tigeot 			goto out;
22678621f407SFrançois Tigeot 		}
22688621f407SFrançois Tigeot 
22698621f407SFrançois Tigeot 		if (!obj->properties) {
2270a85cb24fSFrançois Tigeot 			drm_mode_object_put(obj);
22712c9916cdSFrançois Tigeot 			ret = -ENOENT;
2272a05eeebfSFrançois Tigeot 			goto out;
22732c9916cdSFrançois Tigeot 		}
22742c9916cdSFrançois Tigeot 
22752c9916cdSFrançois Tigeot 		if (get_user(count_props, count_props_ptr + copied_objs)) {
2276a85cb24fSFrançois Tigeot 			drm_mode_object_put(obj);
22772c9916cdSFrançois Tigeot 			ret = -EFAULT;
2278a05eeebfSFrançois Tigeot 			goto out;
22792c9916cdSFrançois Tigeot 		}
22802c9916cdSFrançois Tigeot 
22812c9916cdSFrançois Tigeot 		copied_objs++;
22822c9916cdSFrançois Tigeot 
22832c9916cdSFrançois Tigeot 		for (j = 0; j < count_props; j++) {
22842c9916cdSFrançois Tigeot 			uint32_t prop_id;
22852c9916cdSFrançois Tigeot 			uint64_t prop_value;
22862c9916cdSFrançois Tigeot 			struct drm_property *prop;
22872c9916cdSFrançois Tigeot 
22882c9916cdSFrançois Tigeot 			if (get_user(prop_id, props_ptr + copied_props)) {
2289a85cb24fSFrançois Tigeot 				drm_mode_object_put(obj);
22902c9916cdSFrançois Tigeot 				ret = -EFAULT;
2291a05eeebfSFrançois Tigeot 				goto out;
22922c9916cdSFrançois Tigeot 			}
22932c9916cdSFrançois Tigeot 
22941dedbd3bSFrançois Tigeot 			prop = drm_mode_obj_find_prop_id(obj, prop_id);
22952c9916cdSFrançois Tigeot 			if (!prop) {
2296a85cb24fSFrançois Tigeot 				drm_mode_object_put(obj);
22972c9916cdSFrançois Tigeot 				ret = -ENOENT;
2298a05eeebfSFrançois Tigeot 				goto out;
22992c9916cdSFrançois Tigeot 			}
23002c9916cdSFrançois Tigeot 
23012c9916cdSFrançois Tigeot 			if (copy_from_user(&prop_value,
23022c9916cdSFrançois Tigeot 					   prop_values_ptr + copied_props,
23032c9916cdSFrançois Tigeot 					   sizeof(prop_value))) {
2304a85cb24fSFrançois Tigeot 				drm_mode_object_put(obj);
23052c9916cdSFrançois Tigeot 				ret = -EFAULT;
2306a05eeebfSFrançois Tigeot 				goto out;
23072c9916cdSFrançois Tigeot 			}
23082c9916cdSFrançois Tigeot 
2309*3f2dd94aSFrançois Tigeot 			ret = drm_atomic_set_property(state, obj, prop,
2310*3f2dd94aSFrançois Tigeot 						      prop_value);
23118621f407SFrançois Tigeot 			if (ret) {
2312a85cb24fSFrançois Tigeot 				drm_mode_object_put(obj);
2313a05eeebfSFrançois Tigeot 				goto out;
23148621f407SFrançois Tigeot 			}
23152c9916cdSFrançois Tigeot 
23162c9916cdSFrançois Tigeot 			copied_props++;
23172c9916cdSFrançois Tigeot 		}
2318a05eeebfSFrançois Tigeot 
2319a05eeebfSFrançois Tigeot 		if (obj->type == DRM_MODE_OBJECT_PLANE && count_props &&
2320a05eeebfSFrançois Tigeot 		    !(arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)) {
2321a05eeebfSFrançois Tigeot 			plane = obj_to_plane(obj);
2322a05eeebfSFrançois Tigeot 			plane_mask |= (1 << drm_plane_index(plane));
2323a05eeebfSFrançois Tigeot 			plane->old_fb = plane->fb;
2324a05eeebfSFrançois Tigeot 		}
2325a85cb24fSFrançois Tigeot 		drm_mode_object_put(obj);
23262c9916cdSFrançois Tigeot 	}
23272c9916cdSFrançois Tigeot 
23284be47400SFrançois Tigeot 	ret = prepare_crtc_signaling(dev, state, arg, file_priv, &fence_state,
23294be47400SFrançois Tigeot 				     &num_fences);
23304be47400SFrançois Tigeot 	if (ret)
2331a05eeebfSFrançois Tigeot 		goto out;
23322c9916cdSFrançois Tigeot 
23332c9916cdSFrançois Tigeot 	if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) {
23342c9916cdSFrançois Tigeot 		ret = drm_atomic_check_only(state);
23352c9916cdSFrançois Tigeot 	} else if (arg->flags & DRM_MODE_ATOMIC_NONBLOCK) {
23368621f407SFrançois Tigeot 		ret = drm_atomic_nonblocking_commit(state);
23372c9916cdSFrançois Tigeot 	} else {
23384be47400SFrançois Tigeot 		if (unlikely(drm_debug & DRM_UT_STATE))
23394be47400SFrançois Tigeot 			drm_atomic_print_state(state);
23404be47400SFrançois Tigeot 
23412c9916cdSFrançois Tigeot 		ret = drm_atomic_commit(state);
23422c9916cdSFrançois Tigeot 	}
23432c9916cdSFrançois Tigeot 
2344a05eeebfSFrançois Tigeot out:
2345352ff8bdSFrançois Tigeot 	drm_atomic_clean_old_fb(dev, plane_mask, ret);
23462c9916cdSFrançois Tigeot 
23474be47400SFrançois Tigeot 	complete_crtc_signaling(dev, state, fence_state, num_fences, !ret);
23482c9916cdSFrançois Tigeot 
2349a05eeebfSFrançois Tigeot 	if (ret == -EDEADLK) {
2350a05eeebfSFrançois Tigeot 		drm_atomic_state_clear(state);
2351*3f2dd94aSFrançois Tigeot 		ret = drm_modeset_backoff(&ctx);
2352*3f2dd94aSFrançois Tigeot 		if (!ret)
2353a05eeebfSFrançois Tigeot 			goto retry;
2354a05eeebfSFrançois Tigeot 	}
2355a05eeebfSFrançois Tigeot 
23564be47400SFrançois Tigeot 	drm_atomic_state_put(state);
23572c9916cdSFrançois Tigeot 
23582c9916cdSFrançois Tigeot 	drm_modeset_drop_locks(&ctx);
23592c9916cdSFrançois Tigeot 	drm_modeset_acquire_fini(&ctx);
23602c9916cdSFrançois Tigeot 
23612c9916cdSFrançois Tigeot 	return ret;
23622c9916cdSFrançois Tigeot }
2363