xref: /openbmc/linux/drivers/gpu/drm/drm_property.c (revision b240b419db5d624ce7a5a397d6f62a1a686009ec)
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 <drm/drmP.h>
25 #include <drm/drm_property.h>
26 
27 #include "drm_crtc_internal.h"
28 
29 /**
30  * DOC: overview
31  *
32  * Properties as represented by &drm_property are used to extend the modeset
33  * interface exposed to userspace. For the atomic modeset IOCTL properties are
34  * even the only way to transport metadata about the desired new modeset
35  * configuration from userspace to the kernel. Properties have a well-defined
36  * value range, which is enforced by the drm core. See the documentation of the
37  * flags member of &struct drm_property for an overview of the different
38  * property types and ranges.
39  *
40  * Properties don't store the current value directly, but need to be
41  * instatiated by attaching them to a &drm_mode_object with
42  * drm_object_attach_property().
43  *
44  * Property values are only 64bit. To support bigger piles of data (like gamma
45  * tables, color correction matrices or large structures) a property can instead
46  * point at a &drm_property_blob with that additional data.
47  *
48  * Properties are defined by their symbolic name, userspace must keep a
49  * per-object mapping from those names to the property ID used in the atomic
50  * IOCTL and in the get/set property IOCTL.
51  */
52 
53 static bool drm_property_flags_valid(u32 flags)
54 {
55 	u32 legacy_type = flags & DRM_MODE_PROP_LEGACY_TYPE;
56 	u32 ext_type = flags & DRM_MODE_PROP_EXTENDED_TYPE;
57 
58 	/* Reject undefined/deprecated flags */
59 	if (flags & ~(DRM_MODE_PROP_LEGACY_TYPE |
60 		      DRM_MODE_PROP_EXTENDED_TYPE |
61 		      DRM_MODE_PROP_IMMUTABLE |
62 		      DRM_MODE_PROP_ATOMIC))
63 		return false;
64 
65 	/* We want either a legacy type or an extended type, but not both */
66 	if (!legacy_type == !ext_type)
67 		return false;
68 
69 	/* Only one legacy type at a time please */
70 	if (legacy_type && !is_power_of_2(legacy_type))
71 		return false;
72 
73 	return true;
74 }
75 
76 /**
77  * drm_property_create - create a new property type
78  * @dev: drm device
79  * @flags: flags specifying the property type
80  * @name: name of the property
81  * @num_values: number of pre-defined values
82  *
83  * This creates a new generic drm property which can then be attached to a drm
84  * object with drm_object_attach_property(). The returned property object must
85  * be freed with drm_property_destroy(), which is done automatically when
86  * calling drm_mode_config_cleanup().
87  *
88  * Returns:
89  * A pointer to the newly created property on success, NULL on failure.
90  */
91 struct drm_property *drm_property_create(struct drm_device *dev,
92 					 u32 flags, const char *name,
93 					 int num_values)
94 {
95 	struct drm_property *property = NULL;
96 	int ret;
97 
98 	if (WARN_ON(!drm_property_flags_valid(flags)))
99 		return NULL;
100 
101 	if (WARN_ON(strlen(name) >= DRM_PROP_NAME_LEN))
102 		return NULL;
103 
104 	property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
105 	if (!property)
106 		return NULL;
107 
108 	property->dev = dev;
109 
110 	if (num_values) {
111 		property->values = kcalloc(num_values, sizeof(uint64_t),
112 					   GFP_KERNEL);
113 		if (!property->values)
114 			goto fail;
115 	}
116 
117 	ret = drm_mode_object_add(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
118 	if (ret)
119 		goto fail;
120 
121 	property->flags = flags;
122 	property->num_values = num_values;
123 	INIT_LIST_HEAD(&property->enum_list);
124 
125 	strncpy(property->name, name, DRM_PROP_NAME_LEN);
126 	property->name[DRM_PROP_NAME_LEN-1] = '\0';
127 
128 	list_add_tail(&property->head, &dev->mode_config.property_list);
129 
130 	return property;
131 fail:
132 	kfree(property->values);
133 	kfree(property);
134 	return NULL;
135 }
136 EXPORT_SYMBOL(drm_property_create);
137 
138 /**
139  * drm_property_create_enum - create a new enumeration property type
140  * @dev: drm device
141  * @flags: flags specifying the property type
142  * @name: name of the property
143  * @props: enumeration lists with property values
144  * @num_values: number of pre-defined values
145  *
146  * This creates a new generic drm property which can then be attached to a drm
147  * object with drm_object_attach_property(). The returned property object must
148  * be freed with drm_property_destroy(), which is done automatically when
149  * calling drm_mode_config_cleanup().
150  *
151  * Userspace is only allowed to set one of the predefined values for enumeration
152  * properties.
153  *
154  * Returns:
155  * A pointer to the newly created property on success, NULL on failure.
156  */
157 struct drm_property *drm_property_create_enum(struct drm_device *dev,
158 					      u32 flags, const char *name,
159 					      const struct drm_prop_enum_list *props,
160 					      int num_values)
161 {
162 	struct drm_property *property;
163 	int i, ret;
164 
165 	flags |= DRM_MODE_PROP_ENUM;
166 
167 	property = drm_property_create(dev, flags, name, num_values);
168 	if (!property)
169 		return NULL;
170 
171 	for (i = 0; i < num_values; i++) {
172 		ret = drm_property_add_enum(property, i,
173 				      props[i].type,
174 				      props[i].name);
175 		if (ret) {
176 			drm_property_destroy(dev, property);
177 			return NULL;
178 		}
179 	}
180 
181 	return property;
182 }
183 EXPORT_SYMBOL(drm_property_create_enum);
184 
185 /**
186  * drm_property_create_bitmask - create a new bitmask property type
187  * @dev: drm device
188  * @flags: flags specifying the property type
189  * @name: name of the property
190  * @props: enumeration lists with property bitflags
191  * @num_props: size of the @props array
192  * @supported_bits: bitmask of all supported enumeration values
193  *
194  * This creates a new bitmask drm property which can then be attached to a drm
195  * object with drm_object_attach_property(). The returned property object must
196  * be freed with drm_property_destroy(), which is done automatically when
197  * calling drm_mode_config_cleanup().
198  *
199  * Compared to plain enumeration properties userspace is allowed to set any
200  * or'ed together combination of the predefined property bitflag values
201  *
202  * Returns:
203  * A pointer to the newly created property on success, NULL on failure.
204  */
205 struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
206 						 u32 flags, const char *name,
207 						 const struct drm_prop_enum_list *props,
208 						 int num_props,
209 						 uint64_t supported_bits)
210 {
211 	struct drm_property *property;
212 	int i, ret, index = 0;
213 	int num_values = hweight64(supported_bits);
214 
215 	flags |= DRM_MODE_PROP_BITMASK;
216 
217 	property = drm_property_create(dev, flags, name, num_values);
218 	if (!property)
219 		return NULL;
220 	for (i = 0; i < num_props; i++) {
221 		if (!(supported_bits & (1ULL << props[i].type)))
222 			continue;
223 
224 		if (WARN_ON(index >= num_values)) {
225 			drm_property_destroy(dev, property);
226 			return NULL;
227 		}
228 
229 		ret = drm_property_add_enum(property, index++,
230 				      props[i].type,
231 				      props[i].name);
232 		if (ret) {
233 			drm_property_destroy(dev, property);
234 			return NULL;
235 		}
236 	}
237 
238 	return property;
239 }
240 EXPORT_SYMBOL(drm_property_create_bitmask);
241 
242 static struct drm_property *property_create_range(struct drm_device *dev,
243 						  u32 flags, const char *name,
244 						  uint64_t min, uint64_t max)
245 {
246 	struct drm_property *property;
247 
248 	property = drm_property_create(dev, flags, name, 2);
249 	if (!property)
250 		return NULL;
251 
252 	property->values[0] = min;
253 	property->values[1] = max;
254 
255 	return property;
256 }
257 
258 /**
259  * drm_property_create_range - create a new unsigned ranged property type
260  * @dev: drm device
261  * @flags: flags specifying the property type
262  * @name: name of the property
263  * @min: minimum value of the property
264  * @max: maximum value of the property
265  *
266  * This creates a new generic drm property which can then be attached to a drm
267  * object with drm_object_attach_property(). The returned property object must
268  * be freed with drm_property_destroy(), which is done automatically when
269  * calling drm_mode_config_cleanup().
270  *
271  * Userspace is allowed to set any unsigned integer value in the (min, max)
272  * range inclusive.
273  *
274  * Returns:
275  * A pointer to the newly created property on success, NULL on failure.
276  */
277 struct drm_property *drm_property_create_range(struct drm_device *dev,
278 					       u32 flags, const char *name,
279 					       uint64_t min, uint64_t max)
280 {
281 	return property_create_range(dev, DRM_MODE_PROP_RANGE | flags,
282 			name, min, max);
283 }
284 EXPORT_SYMBOL(drm_property_create_range);
285 
286 /**
287  * drm_property_create_signed_range - create a new signed ranged property type
288  * @dev: drm device
289  * @flags: flags specifying the property type
290  * @name: name of the property
291  * @min: minimum value of the property
292  * @max: maximum value of the property
293  *
294  * This creates a new generic drm property which can then be attached to a drm
295  * object with drm_object_attach_property(). The returned property object must
296  * be freed with drm_property_destroy(), which is done automatically when
297  * calling drm_mode_config_cleanup().
298  *
299  * Userspace is allowed to set any signed integer value in the (min, max)
300  * range inclusive.
301  *
302  * Returns:
303  * A pointer to the newly created property on success, NULL on failure.
304  */
305 struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
306 						      u32 flags, const char *name,
307 						      int64_t min, int64_t max)
308 {
309 	return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags,
310 			name, I642U64(min), I642U64(max));
311 }
312 EXPORT_SYMBOL(drm_property_create_signed_range);
313 
314 /**
315  * drm_property_create_object - create a new object property type
316  * @dev: drm device
317  * @flags: flags specifying the property type
318  * @name: name of the property
319  * @type: object type from DRM_MODE_OBJECT_* defines
320  *
321  * This creates a new generic drm property which can then be attached to a drm
322  * object with drm_object_attach_property(). The returned property object must
323  * be freed with drm_property_destroy(), which is done automatically when
324  * calling drm_mode_config_cleanup().
325  *
326  * Userspace is only allowed to set this to any property value of the given
327  * @type. Only useful for atomic properties, which is enforced.
328  *
329  * Returns:
330  * A pointer to the newly created property on success, NULL on failure.
331  */
332 struct drm_property *drm_property_create_object(struct drm_device *dev,
333 						u32 flags, const char *name,
334 						uint32_t type)
335 {
336 	struct drm_property *property;
337 
338 	flags |= DRM_MODE_PROP_OBJECT;
339 
340 	if (WARN_ON(!(flags & DRM_MODE_PROP_ATOMIC)))
341 		return NULL;
342 
343 	property = drm_property_create(dev, flags, name, 1);
344 	if (!property)
345 		return NULL;
346 
347 	property->values[0] = type;
348 
349 	return property;
350 }
351 EXPORT_SYMBOL(drm_property_create_object);
352 
353 /**
354  * drm_property_create_bool - create a new boolean property type
355  * @dev: drm device
356  * @flags: flags specifying the property type
357  * @name: name of the property
358  *
359  * This creates a new generic drm property which can then be attached to a drm
360  * object with drm_object_attach_property(). The returned property object must
361  * be freed with drm_property_destroy(), which is done automatically when
362  * calling drm_mode_config_cleanup().
363  *
364  * This is implemented as a ranged property with only {0, 1} as valid values.
365  *
366  * Returns:
367  * A pointer to the newly created property on success, NULL on failure.
368  */
369 struct drm_property *drm_property_create_bool(struct drm_device *dev,
370 					      u32 flags, const char *name)
371 {
372 	return drm_property_create_range(dev, flags, name, 0, 1);
373 }
374 EXPORT_SYMBOL(drm_property_create_bool);
375 
376 /**
377  * drm_property_add_enum - add a possible value to an enumeration property
378  * @property: enumeration property to change
379  * @index: index of the new enumeration
380  * @value: value of the new enumeration
381  * @name: symbolic name of the new enumeration
382  *
383  * This functions adds enumerations to a property.
384  *
385  * It's use is deprecated, drivers should use one of the more specific helpers
386  * to directly create the property with all enumerations already attached.
387  *
388  * Returns:
389  * Zero on success, error code on failure.
390  */
391 int drm_property_add_enum(struct drm_property *property, int index,
392 			  uint64_t value, const char *name)
393 {
394 	struct drm_property_enum *prop_enum;
395 
396 	if (WARN_ON(strlen(name) >= DRM_PROP_NAME_LEN))
397 		return -EINVAL;
398 
399 	if (WARN_ON(!drm_property_type_is(property, DRM_MODE_PROP_ENUM) &&
400 		    !drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
401 		return -EINVAL;
402 
403 	/*
404 	 * Bitmask enum properties have the additional constraint of values
405 	 * from 0 to 63
406 	 */
407 	if (WARN_ON(drm_property_type_is(property, DRM_MODE_PROP_BITMASK) &&
408 		    value > 63))
409 		return -EINVAL;
410 
411 	list_for_each_entry(prop_enum, &property->enum_list, head) {
412 		if (WARN_ON(prop_enum->value == value))
413 			return -EINVAL;
414 	}
415 
416 	prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
417 	if (!prop_enum)
418 		return -ENOMEM;
419 
420 	strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
421 	prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
422 	prop_enum->value = value;
423 
424 	property->values[index] = value;
425 	list_add_tail(&prop_enum->head, &property->enum_list);
426 	return 0;
427 }
428 EXPORT_SYMBOL(drm_property_add_enum);
429 
430 /**
431  * drm_property_destroy - destroy a drm property
432  * @dev: drm device
433  * @property: property to destry
434  *
435  * This function frees a property including any attached resources like
436  * enumeration values.
437  */
438 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
439 {
440 	struct drm_property_enum *prop_enum, *pt;
441 
442 	list_for_each_entry_safe(prop_enum, pt, &property->enum_list, head) {
443 		list_del(&prop_enum->head);
444 		kfree(prop_enum);
445 	}
446 
447 	if (property->num_values)
448 		kfree(property->values);
449 	drm_mode_object_unregister(dev, &property->base);
450 	list_del(&property->head);
451 	kfree(property);
452 }
453 EXPORT_SYMBOL(drm_property_destroy);
454 
455 int drm_mode_getproperty_ioctl(struct drm_device *dev,
456 			       void *data, struct drm_file *file_priv)
457 {
458 	struct drm_mode_get_property *out_resp = data;
459 	struct drm_property *property;
460 	int enum_count = 0;
461 	int value_count = 0;
462 	int i, copied;
463 	struct drm_property_enum *prop_enum;
464 	struct drm_mode_property_enum __user *enum_ptr;
465 	uint64_t __user *values_ptr;
466 
467 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
468 		return -EINVAL;
469 
470 	property = drm_property_find(dev, file_priv, out_resp->prop_id);
471 	if (!property)
472 		return -ENOENT;
473 
474 	strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
475 	out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
476 	out_resp->flags = property->flags;
477 
478 	value_count = property->num_values;
479 	values_ptr = u64_to_user_ptr(out_resp->values_ptr);
480 
481 	for (i = 0; i < value_count; i++) {
482 		if (i < out_resp->count_values &&
483 		    put_user(property->values[i], values_ptr + i)) {
484 			return -EFAULT;
485 		}
486 	}
487 	out_resp->count_values = value_count;
488 
489 	copied = 0;
490 	enum_ptr = u64_to_user_ptr(out_resp->enum_blob_ptr);
491 
492 	if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
493 	    drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
494 		list_for_each_entry(prop_enum, &property->enum_list, head) {
495 			enum_count++;
496 			if (out_resp->count_enum_blobs < enum_count)
497 				continue;
498 
499 			if (copy_to_user(&enum_ptr[copied].value,
500 					 &prop_enum->value, sizeof(uint64_t)))
501 				return -EFAULT;
502 
503 			if (copy_to_user(&enum_ptr[copied].name,
504 					 &prop_enum->name, DRM_PROP_NAME_LEN))
505 				return -EFAULT;
506 			copied++;
507 		}
508 		out_resp->count_enum_blobs = enum_count;
509 	}
510 
511 	/*
512 	 * NOTE: The idea seems to have been to use this to read all the blob
513 	 * property values. But nothing ever added them to the corresponding
514 	 * list, userspace always used the special-purpose get_blob ioctl to
515 	 * read the value for a blob property. It also doesn't make a lot of
516 	 * sense to return values here when everything else is just metadata for
517 	 * the property itself.
518 	 */
519 	if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
520 		out_resp->count_enum_blobs = 0;
521 
522 	return 0;
523 }
524 
525 static void drm_property_free_blob(struct kref *kref)
526 {
527 	struct drm_property_blob *blob =
528 		container_of(kref, struct drm_property_blob, base.refcount);
529 
530 	mutex_lock(&blob->dev->mode_config.blob_lock);
531 	list_del(&blob->head_global);
532 	mutex_unlock(&blob->dev->mode_config.blob_lock);
533 
534 	drm_mode_object_unregister(blob->dev, &blob->base);
535 
536 	kfree(blob);
537 }
538 
539 /**
540  * drm_property_create_blob - Create new blob property
541  * @dev: DRM device to create property for
542  * @length: Length to allocate for blob data
543  * @data: If specified, copies data into blob
544  *
545  * Creates a new blob property for a specified DRM device, optionally
546  * copying data. Note that blob properties are meant to be invariant, hence the
547  * data must be filled out before the blob is used as the value of any property.
548  *
549  * Returns:
550  * New blob property with a single reference on success, or an ERR_PTR
551  * value on failure.
552  */
553 struct drm_property_blob *
554 drm_property_create_blob(struct drm_device *dev, size_t length,
555 			 const void *data)
556 {
557 	struct drm_property_blob *blob;
558 	int ret;
559 
560 	if (!length || length > ULONG_MAX - sizeof(struct drm_property_blob))
561 		return ERR_PTR(-EINVAL);
562 
563 	blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
564 	if (!blob)
565 		return ERR_PTR(-ENOMEM);
566 
567 	/* This must be explicitly initialised, so we can safely call list_del
568 	 * on it in the removal handler, even if it isn't in a file list. */
569 	INIT_LIST_HEAD(&blob->head_file);
570 	blob->data = (void *)blob + sizeof(*blob);
571 	blob->length = length;
572 	blob->dev = dev;
573 
574 	if (data)
575 		memcpy(blob->data, data, length);
576 
577 	ret = __drm_mode_object_add(dev, &blob->base, DRM_MODE_OBJECT_BLOB,
578 				    true, drm_property_free_blob);
579 	if (ret) {
580 		kfree(blob);
581 		return ERR_PTR(-EINVAL);
582 	}
583 
584 	mutex_lock(&dev->mode_config.blob_lock);
585 	list_add_tail(&blob->head_global,
586 	              &dev->mode_config.property_blob_list);
587 	mutex_unlock(&dev->mode_config.blob_lock);
588 
589 	return blob;
590 }
591 EXPORT_SYMBOL(drm_property_create_blob);
592 
593 /**
594  * drm_property_blob_put - release a blob property reference
595  * @blob: DRM blob property
596  *
597  * Releases a reference to a blob property. May free the object.
598  */
599 void drm_property_blob_put(struct drm_property_blob *blob)
600 {
601 	if (!blob)
602 		return;
603 
604 	drm_mode_object_put(&blob->base);
605 }
606 EXPORT_SYMBOL(drm_property_blob_put);
607 
608 void drm_property_destroy_user_blobs(struct drm_device *dev,
609 				     struct drm_file *file_priv)
610 {
611 	struct drm_property_blob *blob, *bt;
612 
613 	/*
614 	 * When the file gets released that means no one else can access the
615 	 * blob list any more, so no need to grab dev->blob_lock.
616 	 */
617 	list_for_each_entry_safe(blob, bt, &file_priv->blobs, head_file) {
618 		list_del_init(&blob->head_file);
619 		drm_property_blob_put(blob);
620 	}
621 }
622 
623 /**
624  * drm_property_blob_get - acquire blob property reference
625  * @blob: DRM blob property
626  *
627  * Acquires a reference to an existing blob property. Returns @blob, which
628  * allows this to be used as a shorthand in assignments.
629  */
630 struct drm_property_blob *drm_property_blob_get(struct drm_property_blob *blob)
631 {
632 	drm_mode_object_get(&blob->base);
633 	return blob;
634 }
635 EXPORT_SYMBOL(drm_property_blob_get);
636 
637 /**
638  * drm_property_lookup_blob - look up a blob property and take a reference
639  * @dev: drm device
640  * @id: id of the blob property
641  *
642  * If successful, this takes an additional reference to the blob property.
643  * callers need to make sure to eventually unreference the returned property
644  * again, using drm_property_blob_put().
645  *
646  * Return:
647  * NULL on failure, pointer to the blob on success.
648  */
649 struct drm_property_blob *drm_property_lookup_blob(struct drm_device *dev,
650 					           uint32_t id)
651 {
652 	struct drm_mode_object *obj;
653 	struct drm_property_blob *blob = NULL;
654 
655 	obj = __drm_mode_object_find(dev, NULL, id, DRM_MODE_OBJECT_BLOB);
656 	if (obj)
657 		blob = obj_to_blob(obj);
658 	return blob;
659 }
660 EXPORT_SYMBOL(drm_property_lookup_blob);
661 
662 /**
663  * drm_property_replace_global_blob - replace existing blob property
664  * @dev: drm device
665  * @replace: location of blob property pointer to be replaced
666  * @length: length of data for new blob, or 0 for no data
667  * @data: content for new blob, or NULL for no data
668  * @obj_holds_id: optional object for property holding blob ID
669  * @prop_holds_id: optional property holding blob ID
670  * @return 0 on success or error on failure
671  *
672  * This function will replace a global property in the blob list, optionally
673  * updating a property which holds the ID of that property.
674  *
675  * If length is 0 or data is NULL, no new blob will be created, and the holding
676  * property, if specified, will be set to 0.
677  *
678  * Access to the replace pointer is assumed to be protected by the caller, e.g.
679  * by holding the relevant modesetting object lock for its parent.
680  *
681  * For example, a drm_connector has a 'PATH' property, which contains the ID
682  * of a blob property with the value of the MST path information. Calling this
683  * function with replace pointing to the connector's path_blob_ptr, length and
684  * data set for the new path information, obj_holds_id set to the connector's
685  * base object, and prop_holds_id set to the path property name, will perform
686  * a completely atomic update. The access to path_blob_ptr is protected by the
687  * caller holding a lock on the connector.
688  */
689 int drm_property_replace_global_blob(struct drm_device *dev,
690 				     struct drm_property_blob **replace,
691 				     size_t length,
692 				     const void *data,
693 				     struct drm_mode_object *obj_holds_id,
694 				     struct drm_property *prop_holds_id)
695 {
696 	struct drm_property_blob *new_blob = NULL;
697 	struct drm_property_blob *old_blob = NULL;
698 	int ret;
699 
700 	WARN_ON(replace == NULL);
701 
702 	old_blob = *replace;
703 
704 	if (length && data) {
705 		new_blob = drm_property_create_blob(dev, length, data);
706 		if (IS_ERR(new_blob))
707 			return PTR_ERR(new_blob);
708 	}
709 
710 	if (obj_holds_id) {
711 		ret = drm_object_property_set_value(obj_holds_id,
712 						    prop_holds_id,
713 						    new_blob ?
714 						        new_blob->base.id : 0);
715 		if (ret != 0)
716 			goto err_created;
717 	}
718 
719 	drm_property_blob_put(old_blob);
720 	*replace = new_blob;
721 
722 	return 0;
723 
724 err_created:
725 	drm_property_blob_put(new_blob);
726 	return ret;
727 }
728 EXPORT_SYMBOL(drm_property_replace_global_blob);
729 
730 /**
731  * drm_property_replace_blob - replace a blob property
732  * @blob: a pointer to the member blob to be replaced
733  * @new_blob: the new blob to replace with
734  *
735  * Return: true if the blob was in fact replaced.
736  */
737 bool drm_property_replace_blob(struct drm_property_blob **blob,
738 			       struct drm_property_blob *new_blob)
739 {
740 	struct drm_property_blob *old_blob = *blob;
741 
742 	if (old_blob == new_blob)
743 		return false;
744 
745 	drm_property_blob_put(old_blob);
746 	if (new_blob)
747 		drm_property_blob_get(new_blob);
748 	*blob = new_blob;
749 	return true;
750 }
751 EXPORT_SYMBOL(drm_property_replace_blob);
752 
753 int drm_mode_getblob_ioctl(struct drm_device *dev,
754 			   void *data, struct drm_file *file_priv)
755 {
756 	struct drm_mode_get_blob *out_resp = data;
757 	struct drm_property_blob *blob;
758 	int ret = 0;
759 
760 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
761 		return -EINVAL;
762 
763 	blob = drm_property_lookup_blob(dev, out_resp->blob_id);
764 	if (!blob)
765 		return -ENOENT;
766 
767 	if (out_resp->length == blob->length) {
768 		if (copy_to_user(u64_to_user_ptr(out_resp->data),
769 				 blob->data,
770 				 blob->length)) {
771 			ret = -EFAULT;
772 			goto unref;
773 		}
774 	}
775 	out_resp->length = blob->length;
776 unref:
777 	drm_property_blob_put(blob);
778 
779 	return ret;
780 }
781 
782 int drm_mode_createblob_ioctl(struct drm_device *dev,
783 			      void *data, struct drm_file *file_priv)
784 {
785 	struct drm_mode_create_blob *out_resp = data;
786 	struct drm_property_blob *blob;
787 	int ret = 0;
788 
789 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
790 		return -EINVAL;
791 
792 	blob = drm_property_create_blob(dev, out_resp->length, NULL);
793 	if (IS_ERR(blob))
794 		return PTR_ERR(blob);
795 
796 	if (copy_from_user(blob->data,
797 			   u64_to_user_ptr(out_resp->data),
798 			   out_resp->length)) {
799 		ret = -EFAULT;
800 		goto out_blob;
801 	}
802 
803 	/* Dropping the lock between create_blob and our access here is safe
804 	 * as only the same file_priv can remove the blob; at this point, it is
805 	 * not associated with any file_priv. */
806 	mutex_lock(&dev->mode_config.blob_lock);
807 	out_resp->blob_id = blob->base.id;
808 	list_add_tail(&blob->head_file, &file_priv->blobs);
809 	mutex_unlock(&dev->mode_config.blob_lock);
810 
811 	return 0;
812 
813 out_blob:
814 	drm_property_blob_put(blob);
815 	return ret;
816 }
817 
818 int drm_mode_destroyblob_ioctl(struct drm_device *dev,
819 			       void *data, struct drm_file *file_priv)
820 {
821 	struct drm_mode_destroy_blob *out_resp = data;
822 	struct drm_property_blob *blob = NULL, *bt;
823 	bool found = false;
824 	int ret = 0;
825 
826 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
827 		return -EINVAL;
828 
829 	blob = drm_property_lookup_blob(dev, out_resp->blob_id);
830 	if (!blob)
831 		return -ENOENT;
832 
833 	mutex_lock(&dev->mode_config.blob_lock);
834 	/* Ensure the property was actually created by this user. */
835 	list_for_each_entry(bt, &file_priv->blobs, head_file) {
836 		if (bt == blob) {
837 			found = true;
838 			break;
839 		}
840 	}
841 
842 	if (!found) {
843 		ret = -EPERM;
844 		goto err;
845 	}
846 
847 	/* We must drop head_file here, because we may not be the last
848 	 * reference on the blob. */
849 	list_del_init(&blob->head_file);
850 	mutex_unlock(&dev->mode_config.blob_lock);
851 
852 	/* One reference from lookup, and one from the filp. */
853 	drm_property_blob_put(blob);
854 	drm_property_blob_put(blob);
855 
856 	return 0;
857 
858 err:
859 	mutex_unlock(&dev->mode_config.blob_lock);
860 	drm_property_blob_put(blob);
861 
862 	return ret;
863 }
864 
865 /* Some properties could refer to dynamic refcnt'd objects, or things that
866  * need special locking to handle lifetime issues (ie. to ensure the prop
867  * value doesn't become invalid part way through the property update due to
868  * race).  The value returned by reference via 'obj' should be passed back
869  * to drm_property_change_valid_put() after the property is set (and the
870  * object to which the property is attached has a chance to take it's own
871  * reference).
872  */
873 bool drm_property_change_valid_get(struct drm_property *property,
874 				   uint64_t value, struct drm_mode_object **ref)
875 {
876 	int i;
877 
878 	if (property->flags & DRM_MODE_PROP_IMMUTABLE)
879 		return false;
880 
881 	*ref = NULL;
882 
883 	if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) {
884 		if (value < property->values[0] || value > property->values[1])
885 			return false;
886 		return true;
887 	} else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) {
888 		int64_t svalue = U642I64(value);
889 
890 		if (svalue < U642I64(property->values[0]) ||
891 				svalue > U642I64(property->values[1]))
892 			return false;
893 		return true;
894 	} else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
895 		uint64_t valid_mask = 0;
896 
897 		for (i = 0; i < property->num_values; i++)
898 			valid_mask |= (1ULL << property->values[i]);
899 		return !(value & ~valid_mask);
900 	} else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
901 		struct drm_property_blob *blob;
902 
903 		if (value == 0)
904 			return true;
905 
906 		blob = drm_property_lookup_blob(property->dev, value);
907 		if (blob) {
908 			*ref = &blob->base;
909 			return true;
910 		} else {
911 			return false;
912 		}
913 	} else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
914 		/* a zero value for an object property translates to null: */
915 		if (value == 0)
916 			return true;
917 
918 		*ref = __drm_mode_object_find(property->dev, NULL, value,
919 					      property->values[0]);
920 		return *ref != NULL;
921 	}
922 
923 	for (i = 0; i < property->num_values; i++)
924 		if (property->values[i] == value)
925 			return true;
926 	return false;
927 }
928 
929 void drm_property_change_valid_put(struct drm_property *property,
930 		struct drm_mode_object *ref)
931 {
932 	if (!ref)
933 		return;
934 
935 	if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
936 		drm_mode_object_put(ref);
937 	} else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
938 		drm_property_blob_put(obj_to_blob(ref));
939 }
940