1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Copyright (c) 2016 Intel Corporation
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Permission to use, copy, modify, distribute, and sell this software and its
5*4882a593Smuzhiyun * documentation for any purpose is hereby granted without fee, provided that
6*4882a593Smuzhiyun * the above copyright notice appear in all copies and that both that copyright
7*4882a593Smuzhiyun * notice and this permission notice appear in supporting documentation, and
8*4882a593Smuzhiyun * that the name of the copyright holders not be used in advertising or
9*4882a593Smuzhiyun * publicity pertaining to distribution of the software without specific,
10*4882a593Smuzhiyun * written prior permission. The copyright holders make no representations
11*4882a593Smuzhiyun * about the suitability of this software for any purpose. It is provided "as
12*4882a593Smuzhiyun * is" without express or implied warranty.
13*4882a593Smuzhiyun *
14*4882a593Smuzhiyun * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15*4882a593Smuzhiyun * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16*4882a593Smuzhiyun * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17*4882a593Smuzhiyun * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18*4882a593Smuzhiyun * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19*4882a593Smuzhiyun * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20*4882a593Smuzhiyun * OF THIS SOFTWARE.
21*4882a593Smuzhiyun */
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun #include <linux/export.h>
24*4882a593Smuzhiyun #include <linux/uaccess.h>
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun #include <drm/drm_atomic.h>
27*4882a593Smuzhiyun #include <drm/drm_drv.h>
28*4882a593Smuzhiyun #include <drm/drm_device.h>
29*4882a593Smuzhiyun #include <drm/drm_file.h>
30*4882a593Smuzhiyun #include <drm/drm_mode_object.h>
31*4882a593Smuzhiyun #include <drm/drm_print.h>
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun #include "drm_crtc_internal.h"
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun /*
36*4882a593Smuzhiyun * Internal function to assign a slot in the object idr and optionally
37*4882a593Smuzhiyun * register the object into the idr.
38*4882a593Smuzhiyun */
__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))39*4882a593Smuzhiyun int __drm_mode_object_add(struct drm_device *dev, struct drm_mode_object *obj,
40*4882a593Smuzhiyun uint32_t obj_type, bool register_obj,
41*4882a593Smuzhiyun void (*obj_free_cb)(struct kref *kref))
42*4882a593Smuzhiyun {
43*4882a593Smuzhiyun int ret;
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun WARN_ON(!dev->driver->load && dev->registered && !obj_free_cb);
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun mutex_lock(&dev->mode_config.idr_mutex);
48*4882a593Smuzhiyun ret = idr_alloc(&dev->mode_config.object_idr, register_obj ? obj : NULL,
49*4882a593Smuzhiyun 1, 0, GFP_KERNEL);
50*4882a593Smuzhiyun if (ret >= 0) {
51*4882a593Smuzhiyun /*
52*4882a593Smuzhiyun * Set up the object linking under the protection of the idr
53*4882a593Smuzhiyun * lock so that other users can't see inconsistent state.
54*4882a593Smuzhiyun */
55*4882a593Smuzhiyun obj->id = ret;
56*4882a593Smuzhiyun obj->type = obj_type;
57*4882a593Smuzhiyun if (obj_free_cb) {
58*4882a593Smuzhiyun obj->free_cb = obj_free_cb;
59*4882a593Smuzhiyun kref_init(&obj->refcount);
60*4882a593Smuzhiyun }
61*4882a593Smuzhiyun }
62*4882a593Smuzhiyun mutex_unlock(&dev->mode_config.idr_mutex);
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun return ret < 0 ? ret : 0;
65*4882a593Smuzhiyun }
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun /**
68*4882a593Smuzhiyun * drm_mode_object_add - allocate a new modeset identifier
69*4882a593Smuzhiyun * @dev: DRM device
70*4882a593Smuzhiyun * @obj: object pointer, used to generate unique ID
71*4882a593Smuzhiyun * @obj_type: object type
72*4882a593Smuzhiyun *
73*4882a593Smuzhiyun * Create a unique identifier based on @ptr in @dev's identifier space. Used
74*4882a593Smuzhiyun * for tracking modes, CRTCs and connectors.
75*4882a593Smuzhiyun *
76*4882a593Smuzhiyun * Returns:
77*4882a593Smuzhiyun * Zero on success, error code on failure.
78*4882a593Smuzhiyun */
drm_mode_object_add(struct drm_device * dev,struct drm_mode_object * obj,uint32_t obj_type)79*4882a593Smuzhiyun int drm_mode_object_add(struct drm_device *dev,
80*4882a593Smuzhiyun struct drm_mode_object *obj, uint32_t obj_type)
81*4882a593Smuzhiyun {
82*4882a593Smuzhiyun return __drm_mode_object_add(dev, obj, obj_type, true, NULL);
83*4882a593Smuzhiyun }
84*4882a593Smuzhiyun
drm_mode_object_register(struct drm_device * dev,struct drm_mode_object * obj)85*4882a593Smuzhiyun void drm_mode_object_register(struct drm_device *dev,
86*4882a593Smuzhiyun struct drm_mode_object *obj)
87*4882a593Smuzhiyun {
88*4882a593Smuzhiyun mutex_lock(&dev->mode_config.idr_mutex);
89*4882a593Smuzhiyun idr_replace(&dev->mode_config.object_idr, obj, obj->id);
90*4882a593Smuzhiyun mutex_unlock(&dev->mode_config.idr_mutex);
91*4882a593Smuzhiyun }
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun /**
94*4882a593Smuzhiyun * drm_mode_object_unregister - free a modeset identifer
95*4882a593Smuzhiyun * @dev: DRM device
96*4882a593Smuzhiyun * @object: object to free
97*4882a593Smuzhiyun *
98*4882a593Smuzhiyun * Free @id from @dev's unique identifier pool.
99*4882a593Smuzhiyun * This function can be called multiple times, and guards against
100*4882a593Smuzhiyun * multiple removals.
101*4882a593Smuzhiyun * These modeset identifiers are _not_ reference counted. Hence don't use this
102*4882a593Smuzhiyun * for reference counted modeset objects like framebuffers.
103*4882a593Smuzhiyun */
drm_mode_object_unregister(struct drm_device * dev,struct drm_mode_object * object)104*4882a593Smuzhiyun void drm_mode_object_unregister(struct drm_device *dev,
105*4882a593Smuzhiyun struct drm_mode_object *object)
106*4882a593Smuzhiyun {
107*4882a593Smuzhiyun WARN_ON(!dev->driver->load && dev->registered && !object->free_cb);
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun mutex_lock(&dev->mode_config.idr_mutex);
110*4882a593Smuzhiyun if (object->id) {
111*4882a593Smuzhiyun idr_remove(&dev->mode_config.object_idr, object->id);
112*4882a593Smuzhiyun object->id = 0;
113*4882a593Smuzhiyun }
114*4882a593Smuzhiyun mutex_unlock(&dev->mode_config.idr_mutex);
115*4882a593Smuzhiyun }
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun /**
118*4882a593Smuzhiyun * drm_lease_required - check types which must be leased to be used
119*4882a593Smuzhiyun * @type: type of object
120*4882a593Smuzhiyun *
121*4882a593Smuzhiyun * Returns whether the provided type of drm_mode_object must
122*4882a593Smuzhiyun * be owned or leased to be used by a process.
123*4882a593Smuzhiyun */
drm_mode_object_lease_required(uint32_t type)124*4882a593Smuzhiyun bool drm_mode_object_lease_required(uint32_t type)
125*4882a593Smuzhiyun {
126*4882a593Smuzhiyun switch(type) {
127*4882a593Smuzhiyun case DRM_MODE_OBJECT_CRTC:
128*4882a593Smuzhiyun case DRM_MODE_OBJECT_CONNECTOR:
129*4882a593Smuzhiyun case DRM_MODE_OBJECT_PLANE:
130*4882a593Smuzhiyun return true;
131*4882a593Smuzhiyun default:
132*4882a593Smuzhiyun return false;
133*4882a593Smuzhiyun }
134*4882a593Smuzhiyun }
135*4882a593Smuzhiyun
__drm_mode_object_find(struct drm_device * dev,struct drm_file * file_priv,uint32_t id,uint32_t type)136*4882a593Smuzhiyun struct drm_mode_object *__drm_mode_object_find(struct drm_device *dev,
137*4882a593Smuzhiyun struct drm_file *file_priv,
138*4882a593Smuzhiyun uint32_t id, uint32_t type)
139*4882a593Smuzhiyun {
140*4882a593Smuzhiyun struct drm_mode_object *obj = NULL;
141*4882a593Smuzhiyun
142*4882a593Smuzhiyun mutex_lock(&dev->mode_config.idr_mutex);
143*4882a593Smuzhiyun obj = idr_find(&dev->mode_config.object_idr, id);
144*4882a593Smuzhiyun if (obj && type != DRM_MODE_OBJECT_ANY && obj->type != type)
145*4882a593Smuzhiyun obj = NULL;
146*4882a593Smuzhiyun if (obj && obj->id != id)
147*4882a593Smuzhiyun obj = NULL;
148*4882a593Smuzhiyun
149*4882a593Smuzhiyun if (obj && drm_mode_object_lease_required(obj->type) &&
150*4882a593Smuzhiyun !_drm_lease_held(file_priv, obj->id))
151*4882a593Smuzhiyun obj = NULL;
152*4882a593Smuzhiyun
153*4882a593Smuzhiyun if (obj && obj->free_cb) {
154*4882a593Smuzhiyun if (!kref_get_unless_zero(&obj->refcount))
155*4882a593Smuzhiyun obj = NULL;
156*4882a593Smuzhiyun }
157*4882a593Smuzhiyun mutex_unlock(&dev->mode_config.idr_mutex);
158*4882a593Smuzhiyun
159*4882a593Smuzhiyun return obj;
160*4882a593Smuzhiyun }
161*4882a593Smuzhiyun
162*4882a593Smuzhiyun /**
163*4882a593Smuzhiyun * drm_mode_object_find - look up a drm object with static lifetime
164*4882a593Smuzhiyun * @dev: drm device
165*4882a593Smuzhiyun * @file_priv: drm file
166*4882a593Smuzhiyun * @id: id of the mode object
167*4882a593Smuzhiyun * @type: type of the mode object
168*4882a593Smuzhiyun *
169*4882a593Smuzhiyun * This function is used to look up a modeset object. It will acquire a
170*4882a593Smuzhiyun * reference for reference counted objects. This reference must be dropped again
171*4882a593Smuzhiyun * by callind drm_mode_object_put().
172*4882a593Smuzhiyun */
drm_mode_object_find(struct drm_device * dev,struct drm_file * file_priv,uint32_t id,uint32_t type)173*4882a593Smuzhiyun struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
174*4882a593Smuzhiyun struct drm_file *file_priv,
175*4882a593Smuzhiyun uint32_t id, uint32_t type)
176*4882a593Smuzhiyun {
177*4882a593Smuzhiyun struct drm_mode_object *obj = NULL;
178*4882a593Smuzhiyun
179*4882a593Smuzhiyun obj = __drm_mode_object_find(dev, file_priv, id, type);
180*4882a593Smuzhiyun return obj;
181*4882a593Smuzhiyun }
182*4882a593Smuzhiyun EXPORT_SYMBOL(drm_mode_object_find);
183*4882a593Smuzhiyun
184*4882a593Smuzhiyun /**
185*4882a593Smuzhiyun * drm_mode_object_put - release a mode object reference
186*4882a593Smuzhiyun * @obj: DRM mode object
187*4882a593Smuzhiyun *
188*4882a593Smuzhiyun * This function decrements the object's refcount if it is a refcounted modeset
189*4882a593Smuzhiyun * object. It is a no-op on any other object. This is used to drop references
190*4882a593Smuzhiyun * acquired with drm_mode_object_get().
191*4882a593Smuzhiyun */
drm_mode_object_put(struct drm_mode_object * obj)192*4882a593Smuzhiyun void drm_mode_object_put(struct drm_mode_object *obj)
193*4882a593Smuzhiyun {
194*4882a593Smuzhiyun if (obj->free_cb) {
195*4882a593Smuzhiyun DRM_DEBUG("OBJ ID: %d (%d)\n", obj->id, kref_read(&obj->refcount));
196*4882a593Smuzhiyun kref_put(&obj->refcount, obj->free_cb);
197*4882a593Smuzhiyun }
198*4882a593Smuzhiyun }
199*4882a593Smuzhiyun EXPORT_SYMBOL(drm_mode_object_put);
200*4882a593Smuzhiyun
201*4882a593Smuzhiyun /**
202*4882a593Smuzhiyun * drm_mode_object_get - acquire a mode object reference
203*4882a593Smuzhiyun * @obj: DRM mode object
204*4882a593Smuzhiyun *
205*4882a593Smuzhiyun * This function increments the object's refcount if it is a refcounted modeset
206*4882a593Smuzhiyun * object. It is a no-op on any other object. References should be dropped again
207*4882a593Smuzhiyun * by calling drm_mode_object_put().
208*4882a593Smuzhiyun */
drm_mode_object_get(struct drm_mode_object * obj)209*4882a593Smuzhiyun void drm_mode_object_get(struct drm_mode_object *obj)
210*4882a593Smuzhiyun {
211*4882a593Smuzhiyun if (obj->free_cb) {
212*4882a593Smuzhiyun DRM_DEBUG("OBJ ID: %d (%d)\n", obj->id, kref_read(&obj->refcount));
213*4882a593Smuzhiyun kref_get(&obj->refcount);
214*4882a593Smuzhiyun }
215*4882a593Smuzhiyun }
216*4882a593Smuzhiyun EXPORT_SYMBOL(drm_mode_object_get);
217*4882a593Smuzhiyun
218*4882a593Smuzhiyun /**
219*4882a593Smuzhiyun * drm_object_attach_property - attach a property to a modeset object
220*4882a593Smuzhiyun * @obj: drm modeset object
221*4882a593Smuzhiyun * @property: property to attach
222*4882a593Smuzhiyun * @init_val: initial value of the property
223*4882a593Smuzhiyun *
224*4882a593Smuzhiyun * This attaches the given property to the modeset object with the given initial
225*4882a593Smuzhiyun * value. Currently this function cannot fail since the properties are stored in
226*4882a593Smuzhiyun * a statically sized array.
227*4882a593Smuzhiyun *
228*4882a593Smuzhiyun * Note that all properties must be attached before the object itself is
229*4882a593Smuzhiyun * registered and accessible from userspace.
230*4882a593Smuzhiyun */
drm_object_attach_property(struct drm_mode_object * obj,struct drm_property * property,uint64_t init_val)231*4882a593Smuzhiyun void drm_object_attach_property(struct drm_mode_object *obj,
232*4882a593Smuzhiyun struct drm_property *property,
233*4882a593Smuzhiyun uint64_t init_val)
234*4882a593Smuzhiyun {
235*4882a593Smuzhiyun int count = obj->properties->count;
236*4882a593Smuzhiyun struct drm_device *dev = property->dev;
237*4882a593Smuzhiyun
238*4882a593Smuzhiyun
239*4882a593Smuzhiyun if (obj->type == DRM_MODE_OBJECT_CONNECTOR) {
240*4882a593Smuzhiyun struct drm_connector *connector = obj_to_connector(obj);
241*4882a593Smuzhiyun
242*4882a593Smuzhiyun WARN_ON(!dev->driver->load &&
243*4882a593Smuzhiyun connector->registration_state == DRM_CONNECTOR_REGISTERED);
244*4882a593Smuzhiyun } else {
245*4882a593Smuzhiyun WARN_ON(!dev->driver->load && dev->registered);
246*4882a593Smuzhiyun }
247*4882a593Smuzhiyun
248*4882a593Smuzhiyun if (count == DRM_OBJECT_MAX_PROPERTY) {
249*4882a593Smuzhiyun WARN(1, "Failed to attach object property (type: 0x%x). Please "
250*4882a593Smuzhiyun "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
251*4882a593Smuzhiyun "you see this message on the same object type.\n",
252*4882a593Smuzhiyun obj->type);
253*4882a593Smuzhiyun return;
254*4882a593Smuzhiyun }
255*4882a593Smuzhiyun
256*4882a593Smuzhiyun obj->properties->properties[count] = property;
257*4882a593Smuzhiyun obj->properties->values[count] = init_val;
258*4882a593Smuzhiyun obj->properties->count++;
259*4882a593Smuzhiyun }
260*4882a593Smuzhiyun EXPORT_SYMBOL(drm_object_attach_property);
261*4882a593Smuzhiyun
262*4882a593Smuzhiyun /**
263*4882a593Smuzhiyun * drm_object_property_set_value - set the value of a property
264*4882a593Smuzhiyun * @obj: drm mode object to set property value for
265*4882a593Smuzhiyun * @property: property to set
266*4882a593Smuzhiyun * @val: value the property should be set to
267*4882a593Smuzhiyun *
268*4882a593Smuzhiyun * This function sets a given property on a given object. This function only
269*4882a593Smuzhiyun * changes the software state of the property, it does not call into the
270*4882a593Smuzhiyun * driver's ->set_property callback.
271*4882a593Smuzhiyun *
272*4882a593Smuzhiyun * Note that atomic drivers should not have any need to call this, the core will
273*4882a593Smuzhiyun * ensure consistency of values reported back to userspace through the
274*4882a593Smuzhiyun * appropriate ->atomic_get_property callback. Only legacy drivers should call
275*4882a593Smuzhiyun * this function to update the tracked value (after clamping and other
276*4882a593Smuzhiyun * restrictions have been applied).
277*4882a593Smuzhiyun *
278*4882a593Smuzhiyun * Returns:
279*4882a593Smuzhiyun * Zero on success, error code on failure.
280*4882a593Smuzhiyun */
drm_object_property_set_value(struct drm_mode_object * obj,struct drm_property * property,uint64_t val)281*4882a593Smuzhiyun int drm_object_property_set_value(struct drm_mode_object *obj,
282*4882a593Smuzhiyun struct drm_property *property, uint64_t val)
283*4882a593Smuzhiyun {
284*4882a593Smuzhiyun int i;
285*4882a593Smuzhiyun
286*4882a593Smuzhiyun WARN_ON(drm_drv_uses_atomic_modeset(property->dev) &&
287*4882a593Smuzhiyun !(property->flags & DRM_MODE_PROP_IMMUTABLE));
288*4882a593Smuzhiyun
289*4882a593Smuzhiyun for (i = 0; i < obj->properties->count; i++) {
290*4882a593Smuzhiyun if (obj->properties->properties[i] == property) {
291*4882a593Smuzhiyun obj->properties->values[i] = val;
292*4882a593Smuzhiyun return 0;
293*4882a593Smuzhiyun }
294*4882a593Smuzhiyun }
295*4882a593Smuzhiyun
296*4882a593Smuzhiyun return -EINVAL;
297*4882a593Smuzhiyun }
298*4882a593Smuzhiyun EXPORT_SYMBOL(drm_object_property_set_value);
299*4882a593Smuzhiyun
__drm_object_property_get_value(struct drm_mode_object * obj,struct drm_property * property,uint64_t * val)300*4882a593Smuzhiyun static int __drm_object_property_get_value(struct drm_mode_object *obj,
301*4882a593Smuzhiyun struct drm_property *property,
302*4882a593Smuzhiyun uint64_t *val)
303*4882a593Smuzhiyun {
304*4882a593Smuzhiyun int i;
305*4882a593Smuzhiyun
306*4882a593Smuzhiyun /* read-only properties bypass atomic mechanism and still store
307*4882a593Smuzhiyun * their value in obj->properties->values[].. mostly to avoid
308*4882a593Smuzhiyun * having to deal w/ EDID and similar props in atomic paths:
309*4882a593Smuzhiyun */
310*4882a593Smuzhiyun if (drm_drv_uses_atomic_modeset(property->dev) &&
311*4882a593Smuzhiyun !(property->flags & DRM_MODE_PROP_IMMUTABLE))
312*4882a593Smuzhiyun return drm_atomic_get_property(obj, property, val);
313*4882a593Smuzhiyun
314*4882a593Smuzhiyun for (i = 0; i < obj->properties->count; i++) {
315*4882a593Smuzhiyun if (obj->properties->properties[i] == property) {
316*4882a593Smuzhiyun *val = obj->properties->values[i];
317*4882a593Smuzhiyun return 0;
318*4882a593Smuzhiyun }
319*4882a593Smuzhiyun
320*4882a593Smuzhiyun }
321*4882a593Smuzhiyun
322*4882a593Smuzhiyun return -EINVAL;
323*4882a593Smuzhiyun }
324*4882a593Smuzhiyun
325*4882a593Smuzhiyun /**
326*4882a593Smuzhiyun * drm_object_property_get_value - retrieve the value of a property
327*4882a593Smuzhiyun * @obj: drm mode object to get property value from
328*4882a593Smuzhiyun * @property: property to retrieve
329*4882a593Smuzhiyun * @val: storage for the property value
330*4882a593Smuzhiyun *
331*4882a593Smuzhiyun * This function retrieves the softare state of the given property for the given
332*4882a593Smuzhiyun * property. Since there is no driver callback to retrieve the current property
333*4882a593Smuzhiyun * value this might be out of sync with the hardware, depending upon the driver
334*4882a593Smuzhiyun * and property.
335*4882a593Smuzhiyun *
336*4882a593Smuzhiyun * Atomic drivers should never call this function directly, the core will read
337*4882a593Smuzhiyun * out property values through the various ->atomic_get_property callbacks.
338*4882a593Smuzhiyun *
339*4882a593Smuzhiyun * Returns:
340*4882a593Smuzhiyun * Zero on success, error code on failure.
341*4882a593Smuzhiyun */
drm_object_property_get_value(struct drm_mode_object * obj,struct drm_property * property,uint64_t * val)342*4882a593Smuzhiyun int drm_object_property_get_value(struct drm_mode_object *obj,
343*4882a593Smuzhiyun struct drm_property *property, uint64_t *val)
344*4882a593Smuzhiyun {
345*4882a593Smuzhiyun WARN_ON(drm_drv_uses_atomic_modeset(property->dev));
346*4882a593Smuzhiyun
347*4882a593Smuzhiyun return __drm_object_property_get_value(obj, property, val);
348*4882a593Smuzhiyun }
349*4882a593Smuzhiyun EXPORT_SYMBOL(drm_object_property_get_value);
350*4882a593Smuzhiyun
351*4882a593Smuzhiyun /* 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)352*4882a593Smuzhiyun int drm_mode_object_get_properties(struct drm_mode_object *obj, bool atomic,
353*4882a593Smuzhiyun uint32_t __user *prop_ptr,
354*4882a593Smuzhiyun uint64_t __user *prop_values,
355*4882a593Smuzhiyun uint32_t *arg_count_props)
356*4882a593Smuzhiyun {
357*4882a593Smuzhiyun int i, ret, count;
358*4882a593Smuzhiyun
359*4882a593Smuzhiyun for (i = 0, count = 0; i < obj->properties->count; i++) {
360*4882a593Smuzhiyun struct drm_property *prop = obj->properties->properties[i];
361*4882a593Smuzhiyun uint64_t val;
362*4882a593Smuzhiyun
363*4882a593Smuzhiyun if ((prop->flags & DRM_MODE_PROP_ATOMIC) && !atomic)
364*4882a593Smuzhiyun continue;
365*4882a593Smuzhiyun
366*4882a593Smuzhiyun if (*arg_count_props > count) {
367*4882a593Smuzhiyun ret = __drm_object_property_get_value(obj, prop, &val);
368*4882a593Smuzhiyun if (ret)
369*4882a593Smuzhiyun return ret;
370*4882a593Smuzhiyun
371*4882a593Smuzhiyun if (put_user(prop->base.id, prop_ptr + count))
372*4882a593Smuzhiyun return -EFAULT;
373*4882a593Smuzhiyun
374*4882a593Smuzhiyun if (put_user(val, prop_values + count))
375*4882a593Smuzhiyun return -EFAULT;
376*4882a593Smuzhiyun }
377*4882a593Smuzhiyun
378*4882a593Smuzhiyun count++;
379*4882a593Smuzhiyun }
380*4882a593Smuzhiyun *arg_count_props = count;
381*4882a593Smuzhiyun
382*4882a593Smuzhiyun return 0;
383*4882a593Smuzhiyun }
384*4882a593Smuzhiyun
385*4882a593Smuzhiyun /**
386*4882a593Smuzhiyun * drm_mode_obj_get_properties_ioctl - get the current value of a object's property
387*4882a593Smuzhiyun * @dev: DRM device
388*4882a593Smuzhiyun * @data: ioctl data
389*4882a593Smuzhiyun * @file_priv: DRM file info
390*4882a593Smuzhiyun *
391*4882a593Smuzhiyun * This function retrieves the current value for an object's property. Compared
392*4882a593Smuzhiyun * to the connector specific ioctl this one is extended to also work on crtc and
393*4882a593Smuzhiyun * plane objects.
394*4882a593Smuzhiyun *
395*4882a593Smuzhiyun * Called by the user via ioctl.
396*4882a593Smuzhiyun *
397*4882a593Smuzhiyun * Returns:
398*4882a593Smuzhiyun * Zero on success, negative errno on failure.
399*4882a593Smuzhiyun */
drm_mode_obj_get_properties_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)400*4882a593Smuzhiyun int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
401*4882a593Smuzhiyun struct drm_file *file_priv)
402*4882a593Smuzhiyun {
403*4882a593Smuzhiyun struct drm_mode_obj_get_properties *arg = data;
404*4882a593Smuzhiyun struct drm_mode_object *obj;
405*4882a593Smuzhiyun struct drm_modeset_acquire_ctx ctx;
406*4882a593Smuzhiyun int ret = 0;
407*4882a593Smuzhiyun
408*4882a593Smuzhiyun if (!drm_core_check_feature(dev, DRIVER_MODESET))
409*4882a593Smuzhiyun return -EOPNOTSUPP;
410*4882a593Smuzhiyun
411*4882a593Smuzhiyun DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx, 0, ret);
412*4882a593Smuzhiyun
413*4882a593Smuzhiyun obj = drm_mode_object_find(dev, file_priv, arg->obj_id, arg->obj_type);
414*4882a593Smuzhiyun if (!obj) {
415*4882a593Smuzhiyun ret = -ENOENT;
416*4882a593Smuzhiyun goto out;
417*4882a593Smuzhiyun }
418*4882a593Smuzhiyun if (!obj->properties) {
419*4882a593Smuzhiyun ret = -EINVAL;
420*4882a593Smuzhiyun goto out_unref;
421*4882a593Smuzhiyun }
422*4882a593Smuzhiyun
423*4882a593Smuzhiyun ret = drm_mode_object_get_properties(obj, file_priv->atomic,
424*4882a593Smuzhiyun (uint32_t __user *)(unsigned long)(arg->props_ptr),
425*4882a593Smuzhiyun (uint64_t __user *)(unsigned long)(arg->prop_values_ptr),
426*4882a593Smuzhiyun &arg->count_props);
427*4882a593Smuzhiyun
428*4882a593Smuzhiyun out_unref:
429*4882a593Smuzhiyun drm_mode_object_put(obj);
430*4882a593Smuzhiyun out:
431*4882a593Smuzhiyun DRM_MODESET_LOCK_ALL_END(dev, ctx, ret);
432*4882a593Smuzhiyun return ret;
433*4882a593Smuzhiyun }
434*4882a593Smuzhiyun
drm_mode_obj_find_prop_id(struct drm_mode_object * obj,uint32_t prop_id)435*4882a593Smuzhiyun struct drm_property *drm_mode_obj_find_prop_id(struct drm_mode_object *obj,
436*4882a593Smuzhiyun uint32_t prop_id)
437*4882a593Smuzhiyun {
438*4882a593Smuzhiyun int i;
439*4882a593Smuzhiyun
440*4882a593Smuzhiyun for (i = 0; i < obj->properties->count; i++)
441*4882a593Smuzhiyun if (obj->properties->properties[i]->base.id == prop_id)
442*4882a593Smuzhiyun return obj->properties->properties[i];
443*4882a593Smuzhiyun
444*4882a593Smuzhiyun return NULL;
445*4882a593Smuzhiyun }
446*4882a593Smuzhiyun
set_property_legacy(struct drm_mode_object * obj,struct drm_property * prop,uint64_t prop_value)447*4882a593Smuzhiyun static int set_property_legacy(struct drm_mode_object *obj,
448*4882a593Smuzhiyun struct drm_property *prop,
449*4882a593Smuzhiyun uint64_t prop_value)
450*4882a593Smuzhiyun {
451*4882a593Smuzhiyun struct drm_device *dev = prop->dev;
452*4882a593Smuzhiyun struct drm_mode_object *ref;
453*4882a593Smuzhiyun struct drm_modeset_acquire_ctx ctx;
454*4882a593Smuzhiyun int ret = -EINVAL;
455*4882a593Smuzhiyun
456*4882a593Smuzhiyun if (!drm_property_change_valid_get(prop, prop_value, &ref))
457*4882a593Smuzhiyun return -EINVAL;
458*4882a593Smuzhiyun
459*4882a593Smuzhiyun DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx, 0, ret);
460*4882a593Smuzhiyun switch (obj->type) {
461*4882a593Smuzhiyun case DRM_MODE_OBJECT_CONNECTOR:
462*4882a593Smuzhiyun ret = drm_connector_set_obj_prop(obj, prop, prop_value);
463*4882a593Smuzhiyun break;
464*4882a593Smuzhiyun case DRM_MODE_OBJECT_CRTC:
465*4882a593Smuzhiyun ret = drm_mode_crtc_set_obj_prop(obj, prop, prop_value);
466*4882a593Smuzhiyun break;
467*4882a593Smuzhiyun case DRM_MODE_OBJECT_PLANE:
468*4882a593Smuzhiyun ret = drm_mode_plane_set_obj_prop(obj_to_plane(obj),
469*4882a593Smuzhiyun prop, prop_value);
470*4882a593Smuzhiyun break;
471*4882a593Smuzhiyun }
472*4882a593Smuzhiyun drm_property_change_valid_put(prop, ref);
473*4882a593Smuzhiyun DRM_MODESET_LOCK_ALL_END(dev, ctx, ret);
474*4882a593Smuzhiyun
475*4882a593Smuzhiyun return ret;
476*4882a593Smuzhiyun }
477*4882a593Smuzhiyun
set_property_atomic(struct drm_mode_object * obj,struct drm_file * file_priv,struct drm_property * prop,uint64_t prop_value)478*4882a593Smuzhiyun static int set_property_atomic(struct drm_mode_object *obj,
479*4882a593Smuzhiyun struct drm_file *file_priv,
480*4882a593Smuzhiyun struct drm_property *prop,
481*4882a593Smuzhiyun uint64_t prop_value)
482*4882a593Smuzhiyun {
483*4882a593Smuzhiyun struct drm_device *dev = prop->dev;
484*4882a593Smuzhiyun struct drm_atomic_state *state;
485*4882a593Smuzhiyun struct drm_modeset_acquire_ctx ctx;
486*4882a593Smuzhiyun int ret;
487*4882a593Smuzhiyun
488*4882a593Smuzhiyun state = drm_atomic_state_alloc(dev);
489*4882a593Smuzhiyun if (!state)
490*4882a593Smuzhiyun return -ENOMEM;
491*4882a593Smuzhiyun
492*4882a593Smuzhiyun drm_modeset_acquire_init(&ctx, 0);
493*4882a593Smuzhiyun state->acquire_ctx = &ctx;
494*4882a593Smuzhiyun
495*4882a593Smuzhiyun retry:
496*4882a593Smuzhiyun if (prop == state->dev->mode_config.dpms_property) {
497*4882a593Smuzhiyun if (obj->type != DRM_MODE_OBJECT_CONNECTOR) {
498*4882a593Smuzhiyun ret = -EINVAL;
499*4882a593Smuzhiyun goto out;
500*4882a593Smuzhiyun }
501*4882a593Smuzhiyun
502*4882a593Smuzhiyun ret = drm_atomic_connector_commit_dpms(state,
503*4882a593Smuzhiyun obj_to_connector(obj),
504*4882a593Smuzhiyun prop_value);
505*4882a593Smuzhiyun } else {
506*4882a593Smuzhiyun ret = drm_atomic_set_property(state, file_priv, obj, prop, prop_value);
507*4882a593Smuzhiyun if (ret)
508*4882a593Smuzhiyun goto out;
509*4882a593Smuzhiyun ret = drm_atomic_commit(state);
510*4882a593Smuzhiyun }
511*4882a593Smuzhiyun out:
512*4882a593Smuzhiyun if (ret == -EDEADLK) {
513*4882a593Smuzhiyun drm_atomic_state_clear(state);
514*4882a593Smuzhiyun drm_modeset_backoff(&ctx);
515*4882a593Smuzhiyun goto retry;
516*4882a593Smuzhiyun }
517*4882a593Smuzhiyun
518*4882a593Smuzhiyun drm_atomic_state_put(state);
519*4882a593Smuzhiyun
520*4882a593Smuzhiyun drm_modeset_drop_locks(&ctx);
521*4882a593Smuzhiyun drm_modeset_acquire_fini(&ctx);
522*4882a593Smuzhiyun
523*4882a593Smuzhiyun return ret;
524*4882a593Smuzhiyun }
525*4882a593Smuzhiyun
drm_mode_obj_set_property_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)526*4882a593Smuzhiyun int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
527*4882a593Smuzhiyun struct drm_file *file_priv)
528*4882a593Smuzhiyun {
529*4882a593Smuzhiyun struct drm_mode_obj_set_property *arg = data;
530*4882a593Smuzhiyun struct drm_mode_object *arg_obj;
531*4882a593Smuzhiyun struct drm_property *property;
532*4882a593Smuzhiyun int ret = -EINVAL;
533*4882a593Smuzhiyun
534*4882a593Smuzhiyun if (!drm_core_check_feature(dev, DRIVER_MODESET))
535*4882a593Smuzhiyun return -EOPNOTSUPP;
536*4882a593Smuzhiyun
537*4882a593Smuzhiyun arg_obj = drm_mode_object_find(dev, file_priv, arg->obj_id, arg->obj_type);
538*4882a593Smuzhiyun if (!arg_obj)
539*4882a593Smuzhiyun return -ENOENT;
540*4882a593Smuzhiyun
541*4882a593Smuzhiyun if (!arg_obj->properties)
542*4882a593Smuzhiyun goto out_unref;
543*4882a593Smuzhiyun
544*4882a593Smuzhiyun property = drm_mode_obj_find_prop_id(arg_obj, arg->prop_id);
545*4882a593Smuzhiyun if (!property)
546*4882a593Smuzhiyun goto out_unref;
547*4882a593Smuzhiyun
548*4882a593Smuzhiyun if (drm_drv_uses_atomic_modeset(property->dev))
549*4882a593Smuzhiyun ret = set_property_atomic(arg_obj, file_priv, property, arg->value);
550*4882a593Smuzhiyun else
551*4882a593Smuzhiyun ret = set_property_legacy(arg_obj, property, arg->value);
552*4882a593Smuzhiyun
553*4882a593Smuzhiyun out_unref:
554*4882a593Smuzhiyun drm_mode_object_put(arg_obj);
555*4882a593Smuzhiyun return ret;
556*4882a593Smuzhiyun }
557