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