xref: /dflybsd-src/sys/dev/drm/drm_mode_object.c (revision 3f2dd94a569761201b5b0a18b2f697f97fe1b9dc)
11dedbd3bSFrançois Tigeot /*
21dedbd3bSFrançois Tigeot  * Copyright (c) 2016 Intel Corporation
31dedbd3bSFrançois Tigeot  *
41dedbd3bSFrançois Tigeot  * Permission to use, copy, modify, distribute, and sell this software and its
51dedbd3bSFrançois Tigeot  * documentation for any purpose is hereby granted without fee, provided that
61dedbd3bSFrançois Tigeot  * the above copyright notice appear in all copies and that both that copyright
71dedbd3bSFrançois Tigeot  * notice and this permission notice appear in supporting documentation, and
81dedbd3bSFrançois Tigeot  * that the name of the copyright holders not be used in advertising or
91dedbd3bSFrançois Tigeot  * publicity pertaining to distribution of the software without specific,
101dedbd3bSFrançois Tigeot  * written prior permission.  The copyright holders make no representations
111dedbd3bSFrançois Tigeot  * about the suitability of this software for any purpose.  It is provided "as
121dedbd3bSFrançois Tigeot  * is" without express or implied warranty.
131dedbd3bSFrançois Tigeot  *
141dedbd3bSFrançois Tigeot  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
151dedbd3bSFrançois Tigeot  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
161dedbd3bSFrançois Tigeot  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
171dedbd3bSFrançois Tigeot  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
181dedbd3bSFrançois Tigeot  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
191dedbd3bSFrançois Tigeot  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
201dedbd3bSFrançois Tigeot  * OF THIS SOFTWARE.
211dedbd3bSFrançois Tigeot  */
221dedbd3bSFrançois Tigeot 
231dedbd3bSFrançois Tigeot #include <linux/export.h>
241dedbd3bSFrançois Tigeot #include <drm/drmP.h>
251dedbd3bSFrançois Tigeot #include <drm/drm_mode_object.h>
26a85cb24fSFrançois Tigeot #include <drm/drm_atomic.h>
271dedbd3bSFrançois Tigeot 
281dedbd3bSFrançois Tigeot #include "drm_crtc_internal.h"
291dedbd3bSFrançois Tigeot 
301dedbd3bSFrançois Tigeot /*
311dedbd3bSFrançois Tigeot  * Internal function to assign a slot in the object idr and optionally
321dedbd3bSFrançois Tigeot  * register the object into the idr.
331dedbd3bSFrançois Tigeot  */
__drm_mode_object_add(struct drm_device * dev,struct drm_mode_object * obj,uint32_t obj_type,bool register_obj,void (* obj_free_cb)(struct kref * kref))34a85cb24fSFrançois Tigeot int __drm_mode_object_add(struct drm_device *dev, struct drm_mode_object *obj,
35a85cb24fSFrançois Tigeot 			  uint32_t obj_type, bool register_obj,
361dedbd3bSFrançois Tigeot 			  void (*obj_free_cb)(struct kref *kref))
371dedbd3bSFrançois Tigeot {
381dedbd3bSFrançois Tigeot 	int ret;
391dedbd3bSFrançois Tigeot 
401dedbd3bSFrançois Tigeot 	mutex_lock(&dev->mode_config.idr_mutex);
411dedbd3bSFrançois Tigeot 	ret = idr_alloc(&dev->mode_config.crtc_idr, register_obj ? obj : NULL, 1, 0, GFP_KERNEL);
421dedbd3bSFrançois Tigeot 	if (ret >= 0) {
431dedbd3bSFrançois Tigeot 		/*
441dedbd3bSFrançois Tigeot 		 * Set up the object linking under the protection of the idr
451dedbd3bSFrançois Tigeot 		 * lock so that other users can't see inconsistent state.
461dedbd3bSFrançois Tigeot 		 */
471dedbd3bSFrançois Tigeot 		obj->id = ret;
481dedbd3bSFrançois Tigeot 		obj->type = obj_type;
491dedbd3bSFrançois Tigeot 		if (obj_free_cb) {
501dedbd3bSFrançois Tigeot 			obj->free_cb = obj_free_cb;
511dedbd3bSFrançois Tigeot 			kref_init(&obj->refcount);
521dedbd3bSFrançois Tigeot 		}
531dedbd3bSFrançois Tigeot 	}
541dedbd3bSFrançois Tigeot 	mutex_unlock(&dev->mode_config.idr_mutex);
551dedbd3bSFrançois Tigeot 
561dedbd3bSFrançois Tigeot 	return ret < 0 ? ret : 0;
571dedbd3bSFrançois Tigeot }
581dedbd3bSFrançois Tigeot 
591dedbd3bSFrançois Tigeot /**
60a85cb24fSFrançois Tigeot  * drm_mode_object_add - allocate a new modeset identifier
611dedbd3bSFrançois Tigeot  * @dev: DRM device
621dedbd3bSFrançois Tigeot  * @obj: object pointer, used to generate unique ID
631dedbd3bSFrançois Tigeot  * @obj_type: object type
641dedbd3bSFrançois Tigeot  *
651dedbd3bSFrançois Tigeot  * Create a unique identifier based on @ptr in @dev's identifier space.  Used
66a85cb24fSFrançois Tigeot  * for tracking modes, CRTCs and connectors.
671dedbd3bSFrançois Tigeot  *
681dedbd3bSFrançois Tigeot  * Returns:
691dedbd3bSFrançois Tigeot  * Zero on success, error code on failure.
701dedbd3bSFrançois Tigeot  */
drm_mode_object_add(struct drm_device * dev,struct drm_mode_object * obj,uint32_t obj_type)71a85cb24fSFrançois Tigeot int drm_mode_object_add(struct drm_device *dev,
721dedbd3bSFrançois Tigeot 			struct drm_mode_object *obj, uint32_t obj_type)
731dedbd3bSFrançois Tigeot {
74a85cb24fSFrançois Tigeot 	return __drm_mode_object_add(dev, obj, obj_type, true, NULL);
751dedbd3bSFrançois Tigeot }
761dedbd3bSFrançois Tigeot 
drm_mode_object_register(struct drm_device * dev,struct drm_mode_object * obj)771dedbd3bSFrançois Tigeot void drm_mode_object_register(struct drm_device *dev,
781dedbd3bSFrançois Tigeot 			      struct drm_mode_object *obj)
791dedbd3bSFrançois Tigeot {
801dedbd3bSFrançois Tigeot 	mutex_lock(&dev->mode_config.idr_mutex);
811dedbd3bSFrançois Tigeot 	idr_replace(&dev->mode_config.crtc_idr, obj, obj->id);
821dedbd3bSFrançois Tigeot 	mutex_unlock(&dev->mode_config.idr_mutex);
831dedbd3bSFrançois Tigeot }
841dedbd3bSFrançois Tigeot 
851dedbd3bSFrançois Tigeot /**
861dedbd3bSFrançois Tigeot  * drm_mode_object_unregister - free a modeset identifer
871dedbd3bSFrançois Tigeot  * @dev: DRM device
881dedbd3bSFrançois Tigeot  * @object: object to free
891dedbd3bSFrançois Tigeot  *
901dedbd3bSFrançois Tigeot  * Free @id from @dev's unique identifier pool.
911dedbd3bSFrançois Tigeot  * This function can be called multiple times, and guards against
921dedbd3bSFrançois Tigeot  * multiple removals.
931dedbd3bSFrançois Tigeot  * These modeset identifiers are _not_ reference counted. Hence don't use this
941dedbd3bSFrançois Tigeot  * for reference counted modeset objects like framebuffers.
951dedbd3bSFrançois Tigeot  */
drm_mode_object_unregister(struct drm_device * dev,struct drm_mode_object * object)961dedbd3bSFrançois Tigeot void drm_mode_object_unregister(struct drm_device *dev,
971dedbd3bSFrançois Tigeot 				struct drm_mode_object *object)
981dedbd3bSFrançois Tigeot {
991dedbd3bSFrançois Tigeot 	mutex_lock(&dev->mode_config.idr_mutex);
1001dedbd3bSFrançois Tigeot 	if (object->id) {
1011dedbd3bSFrançois Tigeot 		idr_remove(&dev->mode_config.crtc_idr, object->id);
1021dedbd3bSFrançois Tigeot 		object->id = 0;
1031dedbd3bSFrançois Tigeot 	}
1041dedbd3bSFrançois Tigeot 	mutex_unlock(&dev->mode_config.idr_mutex);
1051dedbd3bSFrançois Tigeot }
1061dedbd3bSFrançois Tigeot 
107*3f2dd94aSFrançois Tigeot /**
108*3f2dd94aSFrançois Tigeot  * drm_lease_required - check types which must be leased to be used
109*3f2dd94aSFrançois Tigeot  * @type: type of object
110*3f2dd94aSFrançois Tigeot  *
111*3f2dd94aSFrançois Tigeot  * Returns whether the provided type of drm_mode_object must
112*3f2dd94aSFrançois Tigeot  * be owned or leased to be used by a process.
113*3f2dd94aSFrançois Tigeot  */
drm_mode_object_lease_required(uint32_t type)114*3f2dd94aSFrançois Tigeot bool drm_mode_object_lease_required(uint32_t type)
115*3f2dd94aSFrançois Tigeot {
116*3f2dd94aSFrançois Tigeot 	switch(type) {
117*3f2dd94aSFrançois Tigeot 	case DRM_MODE_OBJECT_CRTC:
118*3f2dd94aSFrançois Tigeot 	case DRM_MODE_OBJECT_CONNECTOR:
119*3f2dd94aSFrançois Tigeot 	case DRM_MODE_OBJECT_PLANE:
120*3f2dd94aSFrançois Tigeot 		return true;
121*3f2dd94aSFrançois Tigeot 	default:
122*3f2dd94aSFrançois Tigeot 		return false;
123*3f2dd94aSFrançois Tigeot 	}
124*3f2dd94aSFrançois Tigeot }
125*3f2dd94aSFrançois Tigeot 
__drm_mode_object_find(struct drm_device * dev,struct drm_file * file_priv,uint32_t id,uint32_t type)1261dedbd3bSFrançois Tigeot struct drm_mode_object *__drm_mode_object_find(struct drm_device *dev,
127*3f2dd94aSFrançois Tigeot 					       struct drm_file *file_priv,
1281dedbd3bSFrançois Tigeot 					       uint32_t id, uint32_t type)
1291dedbd3bSFrançois Tigeot {
1301dedbd3bSFrançois Tigeot 	struct drm_mode_object *obj = NULL;
1311dedbd3bSFrançois Tigeot 
1321dedbd3bSFrançois Tigeot 	mutex_lock(&dev->mode_config.idr_mutex);
1331dedbd3bSFrançois Tigeot 	obj = idr_find(&dev->mode_config.crtc_idr, id);
1341dedbd3bSFrançois Tigeot 	if (obj && type != DRM_MODE_OBJECT_ANY && obj->type != type)
1351dedbd3bSFrançois Tigeot 		obj = NULL;
1361dedbd3bSFrançois Tigeot 	if (obj && obj->id != id)
1371dedbd3bSFrançois Tigeot 		obj = NULL;
1381dedbd3bSFrançois Tigeot 
139*3f2dd94aSFrançois Tigeot #if 0
140*3f2dd94aSFrançois Tigeot 	if (obj && drm_mode_object_lease_required(obj->type) &&
141*3f2dd94aSFrançois Tigeot 	    !_drm_lease_held(file_priv, obj->id))
142*3f2dd94aSFrançois Tigeot 		obj = NULL;
143*3f2dd94aSFrançois Tigeot #endif
144*3f2dd94aSFrançois Tigeot 
1451dedbd3bSFrançois Tigeot 	if (obj && obj->free_cb) {
1461dedbd3bSFrançois Tigeot 		if (!kref_get_unless_zero(&obj->refcount))
1471dedbd3bSFrançois Tigeot 			obj = NULL;
1481dedbd3bSFrançois Tigeot 	}
1491dedbd3bSFrançois Tigeot 	mutex_unlock(&dev->mode_config.idr_mutex);
1501dedbd3bSFrançois Tigeot 
1511dedbd3bSFrançois Tigeot 	return obj;
1521dedbd3bSFrançois Tigeot }
1531dedbd3bSFrançois Tigeot 
1541dedbd3bSFrançois Tigeot /**
1551dedbd3bSFrançois Tigeot  * drm_mode_object_find - look up a drm object with static lifetime
1561dedbd3bSFrançois Tigeot  * @dev: drm device
157*3f2dd94aSFrançois Tigeot  * @file_priv: drm file
1581dedbd3bSFrançois Tigeot  * @id: id of the mode object
1591dedbd3bSFrançois Tigeot  * @type: type of the mode object
1601dedbd3bSFrançois Tigeot  *
1611dedbd3bSFrançois Tigeot  * This function is used to look up a modeset object. It will acquire a
1621dedbd3bSFrançois Tigeot  * reference for reference counted objects. This reference must be dropped again
163a85cb24fSFrançois Tigeot  * by callind drm_mode_object_put().
1641dedbd3bSFrançois Tigeot  */
drm_mode_object_find(struct drm_device * dev,struct drm_file * file_priv,uint32_t id,uint32_t type)1651dedbd3bSFrançois Tigeot struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
166*3f2dd94aSFrançois Tigeot 		struct drm_file *file_priv,
1671dedbd3bSFrançois Tigeot 		uint32_t id, uint32_t type)
1681dedbd3bSFrançois Tigeot {
1691dedbd3bSFrançois Tigeot 	struct drm_mode_object *obj = NULL;
1701dedbd3bSFrançois Tigeot 
171*3f2dd94aSFrançois Tigeot 	obj = __drm_mode_object_find(dev, file_priv, id, type);
1721dedbd3bSFrançois Tigeot 	return obj;
1731dedbd3bSFrançois Tigeot }
1741dedbd3bSFrançois Tigeot EXPORT_SYMBOL(drm_mode_object_find);
1751dedbd3bSFrançois Tigeot 
1761dedbd3bSFrançois Tigeot /**
177a85cb24fSFrançois Tigeot  * drm_mode_object_put - release a mode object reference
178a85cb24fSFrançois Tigeot  * @obj: DRM mode object
1791dedbd3bSFrançois Tigeot  *
1801dedbd3bSFrançois Tigeot  * This function decrements the object's refcount if it is a refcounted modeset
1811dedbd3bSFrançois Tigeot  * object. It is a no-op on any other object. This is used to drop references
182a85cb24fSFrançois Tigeot  * acquired with drm_mode_object_get().
1831dedbd3bSFrançois Tigeot  */
drm_mode_object_put(struct drm_mode_object * obj)184a85cb24fSFrançois Tigeot void drm_mode_object_put(struct drm_mode_object *obj)
1851dedbd3bSFrançois Tigeot {
1861dedbd3bSFrançois Tigeot 	if (obj->free_cb) {
187*3f2dd94aSFrançois Tigeot 		DRM_DEBUG("OBJ ID: %d (%d)\n", obj->id, kref_read(&obj->refcount));
1881dedbd3bSFrançois Tigeot 		kref_put(&obj->refcount, obj->free_cb);
1891dedbd3bSFrançois Tigeot 	}
1901dedbd3bSFrançois Tigeot }
191a85cb24fSFrançois Tigeot EXPORT_SYMBOL(drm_mode_object_put);
1921dedbd3bSFrançois Tigeot 
1931dedbd3bSFrançois Tigeot /**
194a85cb24fSFrançois Tigeot  * drm_mode_object_get - acquire a mode object reference
195a85cb24fSFrançois Tigeot  * @obj: DRM mode object
1961dedbd3bSFrançois Tigeot  *
1971dedbd3bSFrançois Tigeot  * This function increments the object's refcount if it is a refcounted modeset
1981dedbd3bSFrançois Tigeot  * object. It is a no-op on any other object. References should be dropped again
199a85cb24fSFrançois Tigeot  * by calling drm_mode_object_put().
2001dedbd3bSFrançois Tigeot  */
drm_mode_object_get(struct drm_mode_object * obj)201a85cb24fSFrançois Tigeot void drm_mode_object_get(struct drm_mode_object *obj)
2021dedbd3bSFrançois Tigeot {
2031dedbd3bSFrançois Tigeot 	if (obj->free_cb) {
204*3f2dd94aSFrançois Tigeot 		DRM_DEBUG("OBJ ID: %d (%d)\n", obj->id, kref_read(&obj->refcount));
2051dedbd3bSFrançois Tigeot 		kref_get(&obj->refcount);
2061dedbd3bSFrançois Tigeot 	}
2071dedbd3bSFrançois Tigeot }
208a85cb24fSFrançois Tigeot EXPORT_SYMBOL(drm_mode_object_get);
2091dedbd3bSFrançois Tigeot 
2101dedbd3bSFrançois Tigeot /**
2111dedbd3bSFrançois Tigeot  * drm_object_attach_property - attach a property to a modeset object
2121dedbd3bSFrançois Tigeot  * @obj: drm modeset object
2131dedbd3bSFrançois Tigeot  * @property: property to attach
2141dedbd3bSFrançois Tigeot  * @init_val: initial value of the property
2151dedbd3bSFrançois Tigeot  *
2161dedbd3bSFrançois Tigeot  * This attaches the given property to the modeset object with the given initial
2171dedbd3bSFrançois Tigeot  * value. Currently this function cannot fail since the properties are stored in
2181dedbd3bSFrançois Tigeot  * a statically sized array.
2191dedbd3bSFrançois Tigeot  */
drm_object_attach_property(struct drm_mode_object * obj,struct drm_property * property,uint64_t init_val)2201dedbd3bSFrançois Tigeot void drm_object_attach_property(struct drm_mode_object *obj,
2211dedbd3bSFrançois Tigeot 				struct drm_property *property,
2221dedbd3bSFrançois Tigeot 				uint64_t init_val)
2231dedbd3bSFrançois Tigeot {
2241dedbd3bSFrançois Tigeot 	int count = obj->properties->count;
2251dedbd3bSFrançois Tigeot 
2261dedbd3bSFrançois Tigeot 	if (count == DRM_OBJECT_MAX_PROPERTY) {
2271dedbd3bSFrançois Tigeot 		WARN(1, "Failed to attach object property (type: 0x%x). Please "
2281dedbd3bSFrançois Tigeot 			"increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
2291dedbd3bSFrançois Tigeot 			"you see this message on the same object type.\n",
2301dedbd3bSFrançois Tigeot 			obj->type);
2311dedbd3bSFrançois Tigeot 		return;
2321dedbd3bSFrançois Tigeot 	}
2331dedbd3bSFrançois Tigeot 
2341dedbd3bSFrançois Tigeot 	obj->properties->properties[count] = property;
2351dedbd3bSFrançois Tigeot 	obj->properties->values[count] = init_val;
2361dedbd3bSFrançois Tigeot 	obj->properties->count++;
2371dedbd3bSFrançois Tigeot }
2381dedbd3bSFrançois Tigeot EXPORT_SYMBOL(drm_object_attach_property);
2391dedbd3bSFrançois Tigeot 
2401dedbd3bSFrançois Tigeot /**
2411dedbd3bSFrançois Tigeot  * drm_object_property_set_value - set the value of a property
2421dedbd3bSFrançois Tigeot  * @obj: drm mode object to set property value for
2431dedbd3bSFrançois Tigeot  * @property: property to set
2441dedbd3bSFrançois Tigeot  * @val: value the property should be set to
2451dedbd3bSFrançois Tigeot  *
2461dedbd3bSFrançois Tigeot  * This function sets a given property on a given object. This function only
2471dedbd3bSFrançois Tigeot  * changes the software state of the property, it does not call into the
2481dedbd3bSFrançois Tigeot  * driver's ->set_property callback.
2491dedbd3bSFrançois Tigeot  *
2501dedbd3bSFrançois Tigeot  * Note that atomic drivers should not have any need to call this, the core will
2511dedbd3bSFrançois Tigeot  * ensure consistency of values reported back to userspace through the
2521dedbd3bSFrançois Tigeot  * appropriate ->atomic_get_property callback. Only legacy drivers should call
2531dedbd3bSFrançois Tigeot  * this function to update the tracked value (after clamping and other
2541dedbd3bSFrançois Tigeot  * restrictions have been applied).
2551dedbd3bSFrançois Tigeot  *
2561dedbd3bSFrançois Tigeot  * Returns:
2571dedbd3bSFrançois Tigeot  * Zero on success, error code on failure.
2581dedbd3bSFrançois Tigeot  */
drm_object_property_set_value(struct drm_mode_object * obj,struct drm_property * property,uint64_t val)2591dedbd3bSFrançois Tigeot int drm_object_property_set_value(struct drm_mode_object *obj,
2601dedbd3bSFrançois Tigeot 				  struct drm_property *property, uint64_t val)
2611dedbd3bSFrançois Tigeot {
2621dedbd3bSFrançois Tigeot 	int i;
2631dedbd3bSFrançois Tigeot 
264*3f2dd94aSFrançois Tigeot 	WARN_ON(drm_drv_uses_atomic_modeset(property->dev) &&
265*3f2dd94aSFrançois Tigeot 		!(property->flags & DRM_MODE_PROP_IMMUTABLE));
266*3f2dd94aSFrançois Tigeot 
2671dedbd3bSFrançois Tigeot 	for (i = 0; i < obj->properties->count; i++) {
2681dedbd3bSFrançois Tigeot 		if (obj->properties->properties[i] == property) {
2691dedbd3bSFrançois Tigeot 			obj->properties->values[i] = val;
2701dedbd3bSFrançois Tigeot 			return 0;
2711dedbd3bSFrançois Tigeot 		}
2721dedbd3bSFrançois Tigeot 	}
2731dedbd3bSFrançois Tigeot 
2741dedbd3bSFrançois Tigeot 	return -EINVAL;
2751dedbd3bSFrançois Tigeot }
2761dedbd3bSFrançois Tigeot EXPORT_SYMBOL(drm_object_property_set_value);
2771dedbd3bSFrançois Tigeot 
__drm_object_property_get_value(struct drm_mode_object * obj,struct drm_property * property,uint64_t * val)278*3f2dd94aSFrançois Tigeot static int __drm_object_property_get_value(struct drm_mode_object *obj,
279*3f2dd94aSFrançois Tigeot 					   struct drm_property *property,
280*3f2dd94aSFrançois Tigeot 					   uint64_t *val)
2811dedbd3bSFrançois Tigeot {
2821dedbd3bSFrançois Tigeot 	int i;
2831dedbd3bSFrançois Tigeot 
2841dedbd3bSFrançois Tigeot 	/* read-only properties bypass atomic mechanism and still store
2851dedbd3bSFrançois Tigeot 	 * their value in obj->properties->values[].. mostly to avoid
2861dedbd3bSFrançois Tigeot 	 * having to deal w/ EDID and similar props in atomic paths:
2871dedbd3bSFrançois Tigeot 	 */
288a85cb24fSFrançois Tigeot 	if (drm_drv_uses_atomic_modeset(property->dev) &&
2891dedbd3bSFrançois Tigeot 			!(property->flags & DRM_MODE_PROP_IMMUTABLE))
2901dedbd3bSFrançois Tigeot 		return drm_atomic_get_property(obj, property, val);
2911dedbd3bSFrançois Tigeot 
2921dedbd3bSFrançois Tigeot 	for (i = 0; i < obj->properties->count; i++) {
2931dedbd3bSFrançois Tigeot 		if (obj->properties->properties[i] == property) {
2941dedbd3bSFrançois Tigeot 			*val = obj->properties->values[i];
2951dedbd3bSFrançois Tigeot 			return 0;
2961dedbd3bSFrançois Tigeot 		}
2971dedbd3bSFrançois Tigeot 
2981dedbd3bSFrançois Tigeot 	}
2991dedbd3bSFrançois Tigeot 
3001dedbd3bSFrançois Tigeot 	return -EINVAL;
3011dedbd3bSFrançois Tigeot }
302*3f2dd94aSFrançois Tigeot 
303*3f2dd94aSFrançois Tigeot /**
304*3f2dd94aSFrançois Tigeot  * drm_object_property_get_value - retrieve the value of a property
305*3f2dd94aSFrançois Tigeot  * @obj: drm mode object to get property value from
306*3f2dd94aSFrançois Tigeot  * @property: property to retrieve
307*3f2dd94aSFrançois Tigeot  * @val: storage for the property value
308*3f2dd94aSFrançois Tigeot  *
309*3f2dd94aSFrançois Tigeot  * This function retrieves the softare state of the given property for the given
310*3f2dd94aSFrançois Tigeot  * property. Since there is no driver callback to retrieve the current property
311*3f2dd94aSFrançois Tigeot  * value this might be out of sync with the hardware, depending upon the driver
312*3f2dd94aSFrançois Tigeot  * and property.
313*3f2dd94aSFrançois Tigeot  *
314*3f2dd94aSFrançois Tigeot  * Atomic drivers should never call this function directly, the core will read
315*3f2dd94aSFrançois Tigeot  * out property values through the various ->atomic_get_property callbacks.
316*3f2dd94aSFrançois Tigeot  *
317*3f2dd94aSFrançois Tigeot  * Returns:
318*3f2dd94aSFrançois Tigeot  * Zero on success, error code on failure.
319*3f2dd94aSFrançois Tigeot  */
drm_object_property_get_value(struct drm_mode_object * obj,struct drm_property * property,uint64_t * val)320*3f2dd94aSFrançois Tigeot int drm_object_property_get_value(struct drm_mode_object *obj,
321*3f2dd94aSFrançois Tigeot 				  struct drm_property *property, uint64_t *val)
322*3f2dd94aSFrançois Tigeot {
323*3f2dd94aSFrançois Tigeot 	WARN_ON(drm_drv_uses_atomic_modeset(property->dev));
324*3f2dd94aSFrançois Tigeot 
325*3f2dd94aSFrançois Tigeot 	return __drm_object_property_get_value(obj, property, val);
326*3f2dd94aSFrançois Tigeot }
3271dedbd3bSFrançois Tigeot EXPORT_SYMBOL(drm_object_property_get_value);
3281dedbd3bSFrançois Tigeot 
3291dedbd3bSFrançois Tigeot /* helper for getconnector and getproperties ioctls */
drm_mode_object_get_properties(struct drm_mode_object * obj,bool atomic,uint32_t __user * prop_ptr,uint64_t __user * prop_values,uint32_t * arg_count_props)3301dedbd3bSFrançois Tigeot int drm_mode_object_get_properties(struct drm_mode_object *obj, bool atomic,
3311dedbd3bSFrançois Tigeot 				   uint32_t __user *prop_ptr,
3321dedbd3bSFrançois Tigeot 				   uint64_t __user *prop_values,
3331dedbd3bSFrançois Tigeot 				   uint32_t *arg_count_props)
3341dedbd3bSFrançois Tigeot {
3351dedbd3bSFrançois Tigeot 	int i, ret, count;
3361dedbd3bSFrançois Tigeot 
3371dedbd3bSFrançois Tigeot 	for (i = 0, count = 0; i < obj->properties->count; i++) {
3381dedbd3bSFrançois Tigeot 		struct drm_property *prop = obj->properties->properties[i];
3391dedbd3bSFrançois Tigeot 		uint64_t val;
3401dedbd3bSFrançois Tigeot 
3411dedbd3bSFrançois Tigeot 		if ((prop->flags & DRM_MODE_PROP_ATOMIC) && !atomic)
3421dedbd3bSFrançois Tigeot 			continue;
3431dedbd3bSFrançois Tigeot 
3441dedbd3bSFrançois Tigeot 		if (*arg_count_props > count) {
345*3f2dd94aSFrançois Tigeot 			ret = __drm_object_property_get_value(obj, prop, &val);
3461dedbd3bSFrançois Tigeot 			if (ret)
3471dedbd3bSFrançois Tigeot 				return ret;
3481dedbd3bSFrançois Tigeot 
3491dedbd3bSFrançois Tigeot 			if (put_user(prop->base.id, prop_ptr + count))
3501dedbd3bSFrançois Tigeot 				return -EFAULT;
3511dedbd3bSFrançois Tigeot 
3521dedbd3bSFrançois Tigeot 			if (put_user(val, prop_values + count))
3531dedbd3bSFrançois Tigeot 				return -EFAULT;
3541dedbd3bSFrançois Tigeot 		}
3551dedbd3bSFrançois Tigeot 
3561dedbd3bSFrançois Tigeot 		count++;
3571dedbd3bSFrançois Tigeot 	}
3581dedbd3bSFrançois Tigeot 	*arg_count_props = count;
3591dedbd3bSFrançois Tigeot 
3601dedbd3bSFrançois Tigeot 	return 0;
3611dedbd3bSFrançois Tigeot }
3621dedbd3bSFrançois Tigeot 
3631dedbd3bSFrançois Tigeot /**
3641dedbd3bSFrançois Tigeot  * drm_mode_obj_get_properties_ioctl - get the current value of a object's property
3651dedbd3bSFrançois Tigeot  * @dev: DRM device
3661dedbd3bSFrançois Tigeot  * @data: ioctl data
3671dedbd3bSFrançois Tigeot  * @file_priv: DRM file info
3681dedbd3bSFrançois Tigeot  *
3691dedbd3bSFrançois Tigeot  * This function retrieves the current value for an object's property. Compared
3701dedbd3bSFrançois Tigeot  * to the connector specific ioctl this one is extended to also work on crtc and
3711dedbd3bSFrançois Tigeot  * plane objects.
3721dedbd3bSFrançois Tigeot  *
3731dedbd3bSFrançois Tigeot  * Called by the user via ioctl.
3741dedbd3bSFrançois Tigeot  *
3751dedbd3bSFrançois Tigeot  * Returns:
3761dedbd3bSFrançois Tigeot  * Zero on success, negative errno on failure.
3771dedbd3bSFrançois Tigeot  */
drm_mode_obj_get_properties_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)3781dedbd3bSFrançois Tigeot int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
3791dedbd3bSFrançois Tigeot 				      struct drm_file *file_priv)
3801dedbd3bSFrançois Tigeot {
3811dedbd3bSFrançois Tigeot 	struct drm_mode_obj_get_properties *arg = data;
3821dedbd3bSFrançois Tigeot 	struct drm_mode_object *obj;
3831dedbd3bSFrançois Tigeot 	int ret = 0;
3841dedbd3bSFrançois Tigeot 
3851dedbd3bSFrançois Tigeot 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
3861dedbd3bSFrançois Tigeot 		return -EINVAL;
3871dedbd3bSFrançois Tigeot 
3881dedbd3bSFrançois Tigeot 	drm_modeset_lock_all(dev);
3891dedbd3bSFrançois Tigeot 
390*3f2dd94aSFrançois Tigeot 	obj = drm_mode_object_find(dev, file_priv, arg->obj_id, arg->obj_type);
3911dedbd3bSFrançois Tigeot 	if (!obj) {
3921dedbd3bSFrançois Tigeot 		ret = -ENOENT;
3931dedbd3bSFrançois Tigeot 		goto out;
3941dedbd3bSFrançois Tigeot 	}
3951dedbd3bSFrançois Tigeot 	if (!obj->properties) {
3961dedbd3bSFrançois Tigeot 		ret = -EINVAL;
3971dedbd3bSFrançois Tigeot 		goto out_unref;
3981dedbd3bSFrançois Tigeot 	}
3991dedbd3bSFrançois Tigeot 
4001dedbd3bSFrançois Tigeot 	ret = drm_mode_object_get_properties(obj, file_priv->atomic,
4011dedbd3bSFrançois Tigeot 			(uint32_t __user *)(unsigned long)(arg->props_ptr),
4021dedbd3bSFrançois Tigeot 			(uint64_t __user *)(unsigned long)(arg->prop_values_ptr),
4031dedbd3bSFrançois Tigeot 			&arg->count_props);
4041dedbd3bSFrançois Tigeot 
4051dedbd3bSFrançois Tigeot out_unref:
406a85cb24fSFrançois Tigeot 	drm_mode_object_put(obj);
4071dedbd3bSFrançois Tigeot out:
4081dedbd3bSFrançois Tigeot 	drm_modeset_unlock_all(dev);
4091dedbd3bSFrançois Tigeot 	return ret;
4101dedbd3bSFrançois Tigeot }
4111dedbd3bSFrançois Tigeot 
drm_mode_obj_find_prop_id(struct drm_mode_object * obj,uint32_t prop_id)4121dedbd3bSFrançois Tigeot struct drm_property *drm_mode_obj_find_prop_id(struct drm_mode_object *obj,
4131dedbd3bSFrançois Tigeot 					       uint32_t prop_id)
4141dedbd3bSFrançois Tigeot {
4151dedbd3bSFrançois Tigeot 	int i;
4161dedbd3bSFrançois Tigeot 
4171dedbd3bSFrançois Tigeot 	for (i = 0; i < obj->properties->count; i++)
4181dedbd3bSFrançois Tigeot 		if (obj->properties->properties[i]->base.id == prop_id)
4191dedbd3bSFrançois Tigeot 			return obj->properties->properties[i];
4201dedbd3bSFrançois Tigeot 
4211dedbd3bSFrançois Tigeot 	return NULL;
4221dedbd3bSFrançois Tigeot }
4231dedbd3bSFrançois Tigeot 
set_property_legacy(struct drm_mode_object * obj,struct drm_property * prop,uint64_t prop_value)424*3f2dd94aSFrançois Tigeot static int set_property_legacy(struct drm_mode_object *obj,
425*3f2dd94aSFrançois Tigeot 			       struct drm_property *prop,
426*3f2dd94aSFrançois Tigeot 			       uint64_t prop_value)
427*3f2dd94aSFrançois Tigeot {
428*3f2dd94aSFrançois Tigeot 	struct drm_device *dev = prop->dev;
429*3f2dd94aSFrançois Tigeot 	struct drm_mode_object *ref;
430*3f2dd94aSFrançois Tigeot 	int ret = -EINVAL;
431*3f2dd94aSFrançois Tigeot 
432*3f2dd94aSFrançois Tigeot 	if (!drm_property_change_valid_get(prop, prop_value, &ref))
433*3f2dd94aSFrançois Tigeot 		return -EINVAL;
434*3f2dd94aSFrançois Tigeot 
435*3f2dd94aSFrançois Tigeot 	drm_modeset_lock_all(dev);
436*3f2dd94aSFrançois Tigeot 	switch (obj->type) {
437*3f2dd94aSFrançois Tigeot 	case DRM_MODE_OBJECT_CONNECTOR:
438*3f2dd94aSFrançois Tigeot 		ret = drm_mode_connector_set_obj_prop(obj, prop,
439*3f2dd94aSFrançois Tigeot 						      prop_value);
440*3f2dd94aSFrançois Tigeot 		break;
441*3f2dd94aSFrançois Tigeot 	case DRM_MODE_OBJECT_CRTC:
442*3f2dd94aSFrançois Tigeot 		ret = drm_mode_crtc_set_obj_prop(obj, prop, prop_value);
443*3f2dd94aSFrançois Tigeot 		break;
444*3f2dd94aSFrançois Tigeot 	case DRM_MODE_OBJECT_PLANE:
445*3f2dd94aSFrançois Tigeot 		ret = drm_mode_plane_set_obj_prop(obj_to_plane(obj),
446*3f2dd94aSFrançois Tigeot 						  prop, prop_value);
447*3f2dd94aSFrançois Tigeot 		break;
448*3f2dd94aSFrançois Tigeot 	}
449*3f2dd94aSFrançois Tigeot 	drm_property_change_valid_put(prop, ref);
450*3f2dd94aSFrançois Tigeot 	drm_modeset_unlock_all(dev);
451*3f2dd94aSFrançois Tigeot 
452*3f2dd94aSFrançois Tigeot 	return ret;
453*3f2dd94aSFrançois Tigeot }
454*3f2dd94aSFrançois Tigeot 
set_property_atomic(struct drm_mode_object * obj,struct drm_property * prop,uint64_t prop_value)455*3f2dd94aSFrançois Tigeot static int set_property_atomic(struct drm_mode_object *obj,
456*3f2dd94aSFrançois Tigeot 			       struct drm_property *prop,
457*3f2dd94aSFrançois Tigeot 			       uint64_t prop_value)
458*3f2dd94aSFrançois Tigeot {
459*3f2dd94aSFrançois Tigeot 	struct drm_device *dev = prop->dev;
460*3f2dd94aSFrançois Tigeot 	struct drm_atomic_state *state;
461*3f2dd94aSFrançois Tigeot 	struct drm_modeset_acquire_ctx ctx;
462*3f2dd94aSFrançois Tigeot 	int ret;
463*3f2dd94aSFrançois Tigeot 
464*3f2dd94aSFrançois Tigeot 	drm_modeset_acquire_init(&ctx, 0);
465*3f2dd94aSFrançois Tigeot 
466*3f2dd94aSFrançois Tigeot 	state = drm_atomic_state_alloc(dev);
467*3f2dd94aSFrançois Tigeot 	if (!state)
468*3f2dd94aSFrançois Tigeot 		return -ENOMEM;
469*3f2dd94aSFrançois Tigeot 	state->acquire_ctx = &ctx;
470*3f2dd94aSFrançois Tigeot retry:
471*3f2dd94aSFrançois Tigeot 	if (prop == state->dev->mode_config.dpms_property) {
472*3f2dd94aSFrançois Tigeot 		if (obj->type != DRM_MODE_OBJECT_CONNECTOR) {
473*3f2dd94aSFrançois Tigeot 			ret = -EINVAL;
474*3f2dd94aSFrançois Tigeot 			goto out;
475*3f2dd94aSFrançois Tigeot 		}
476*3f2dd94aSFrançois Tigeot 
477*3f2dd94aSFrançois Tigeot 		ret = drm_atomic_connector_commit_dpms(state,
478*3f2dd94aSFrançois Tigeot 						       obj_to_connector(obj),
479*3f2dd94aSFrançois Tigeot 						       prop_value);
480*3f2dd94aSFrançois Tigeot 	} else {
481*3f2dd94aSFrançois Tigeot 		ret = drm_atomic_set_property(state, obj, prop, prop_value);
482*3f2dd94aSFrançois Tigeot 		if (ret)
483*3f2dd94aSFrançois Tigeot 			goto out;
484*3f2dd94aSFrançois Tigeot 		ret = drm_atomic_commit(state);
485*3f2dd94aSFrançois Tigeot 	}
486*3f2dd94aSFrançois Tigeot out:
487*3f2dd94aSFrançois Tigeot 	if (ret == -EDEADLK) {
488*3f2dd94aSFrançois Tigeot 		drm_atomic_state_clear(state);
489*3f2dd94aSFrançois Tigeot 		drm_modeset_backoff(&ctx);
490*3f2dd94aSFrançois Tigeot 		goto retry;
491*3f2dd94aSFrançois Tigeot 	}
492*3f2dd94aSFrançois Tigeot 
493*3f2dd94aSFrançois Tigeot 	drm_atomic_state_put(state);
494*3f2dd94aSFrançois Tigeot 
495*3f2dd94aSFrançois Tigeot 	drm_modeset_drop_locks(&ctx);
496*3f2dd94aSFrançois Tigeot 	drm_modeset_acquire_fini(&ctx);
497*3f2dd94aSFrançois Tigeot 
498*3f2dd94aSFrançois Tigeot 	return ret;
499*3f2dd94aSFrançois Tigeot }
500*3f2dd94aSFrançois Tigeot 
drm_mode_obj_set_property_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)5011dedbd3bSFrançois Tigeot int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
5021dedbd3bSFrançois Tigeot 				    struct drm_file *file_priv)
5031dedbd3bSFrançois Tigeot {
5041dedbd3bSFrançois Tigeot 	struct drm_mode_obj_set_property *arg = data;
5051dedbd3bSFrançois Tigeot 	struct drm_mode_object *arg_obj;
5061dedbd3bSFrançois Tigeot 	struct drm_property *property;
5071dedbd3bSFrançois Tigeot 	int ret = -EINVAL;
5081dedbd3bSFrançois Tigeot 
5091dedbd3bSFrançois Tigeot 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
5101dedbd3bSFrançois Tigeot 		return -EINVAL;
5111dedbd3bSFrançois Tigeot 
512*3f2dd94aSFrançois Tigeot 	arg_obj = drm_mode_object_find(dev, file_priv, arg->obj_id, arg->obj_type);
513*3f2dd94aSFrançois Tigeot 	if (!arg_obj)
514*3f2dd94aSFrançois Tigeot 		return -ENOENT;
5151dedbd3bSFrançois Tigeot 
5161dedbd3bSFrançois Tigeot 	if (!arg_obj->properties)
5171dedbd3bSFrançois Tigeot 		goto out_unref;
5181dedbd3bSFrançois Tigeot 
5191dedbd3bSFrançois Tigeot 	property = drm_mode_obj_find_prop_id(arg_obj, arg->prop_id);
5201dedbd3bSFrançois Tigeot 	if (!property)
5211dedbd3bSFrançois Tigeot 		goto out_unref;
5221dedbd3bSFrançois Tigeot 
523*3f2dd94aSFrançois Tigeot 	if (drm_drv_uses_atomic_modeset(property->dev))
524*3f2dd94aSFrançois Tigeot 		ret = set_property_atomic(arg_obj, property, arg->value);
525*3f2dd94aSFrançois Tigeot 	else
526*3f2dd94aSFrançois Tigeot 		ret = set_property_legacy(arg_obj, property, arg->value);
5271dedbd3bSFrançois Tigeot 
5281dedbd3bSFrançois Tigeot out_unref:
529a85cb24fSFrançois Tigeot 	drm_mode_object_put(arg_obj);
5301dedbd3bSFrançois Tigeot 	return ret;
5311dedbd3bSFrançois Tigeot }
532