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