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