Linux Audio

Check our new training course

Buildroot integration, development and maintenance

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