blob: c37ac41125b536b7d9d27a829af2d06c6dd871ab [file] [log] [blame]
Daniel Vetter59e71ee2016-08-29 10:27:55 +02001/*
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
Daniel Vetterc8458c72016-08-29 10:27:57 +020029/**
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
Daniel Vetterea0dd852016-12-29 21:48:26 +010037 * flags member of &struct drm_property for an overview of the different
Daniel Vetterc8458c72016-08-29 10:27:57 +020038 * 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
Daniel Vetterd5745282017-01-25 07:26:45 +010045 * tables, color correction matrices or large structures) a property can instead
46 * point at a &drm_property_blob with that additional data.
Daniel Vetterc8458c72016-08-29 10:27:57 +020047 *
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
Daniel Vetter59e71ee2016-08-29 10:27:55 +020053static 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
Daniel Vetter6a8a66e2016-11-23 20:23:27 +010068 * 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().
Daniel Vetter59e71ee2016-08-29 10:27:55 +020071 *
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
Ville Syrjälä5ebbb5b2018-03-02 16:03:00 +020081 if (WARN_ON(strlen(name) >= DRM_PROP_NAME_LEN))
82 return NULL;
83
Daniel Vetter59e71ee2016-08-29 10:27:55 +020084 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
85 if (!property)
86 return NULL;
87
88 property->dev = dev;
89
90 if (num_values) {
91 property->values = kcalloc(num_values, sizeof(uint64_t),
92 GFP_KERNEL);
93 if (!property->values)
94 goto fail;
95 }
96
Thierry Reding2135ea72017-02-28 15:46:37 +010097 ret = drm_mode_object_add(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
Daniel Vetter59e71ee2016-08-29 10:27:55 +020098 if (ret)
99 goto fail;
100
101 property->flags = flags;
102 property->num_values = num_values;
103 INIT_LIST_HEAD(&property->enum_list);
104
Ville Syrjälä8e5e83a2018-03-02 15:25:42 +0200105 strncpy(property->name, name, DRM_PROP_NAME_LEN);
106 property->name[DRM_PROP_NAME_LEN-1] = '\0';
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200107
108 list_add_tail(&property->head, &dev->mode_config.property_list);
109
110 WARN_ON(!drm_property_type_valid(property));
111
112 return property;
113fail:
114 kfree(property->values);
115 kfree(property);
116 return NULL;
117}
118EXPORT_SYMBOL(drm_property_create);
119
120/**
121 * drm_property_create_enum - create a new enumeration property type
122 * @dev: drm device
123 * @flags: flags specifying the property type
124 * @name: name of the property
125 * @props: enumeration lists with property values
126 * @num_values: number of pre-defined values
127 *
128 * This creates a new generic drm property which can then be attached to a drm
Daniel Vetter6a8a66e2016-11-23 20:23:27 +0100129 * object with drm_object_attach_property(). The returned property object must
130 * be freed with drm_property_destroy(), which is done automatically when
131 * calling drm_mode_config_cleanup().
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200132 *
133 * Userspace is only allowed to set one of the predefined values for enumeration
134 * properties.
135 *
136 * Returns:
137 * A pointer to the newly created property on success, NULL on failure.
138 */
139struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
140 const char *name,
141 const struct drm_prop_enum_list *props,
142 int num_values)
143{
144 struct drm_property *property;
145 int i, ret;
146
147 flags |= DRM_MODE_PROP_ENUM;
148
149 property = drm_property_create(dev, flags, name, num_values);
150 if (!property)
151 return NULL;
152
153 for (i = 0; i < num_values; i++) {
154 ret = drm_property_add_enum(property, i,
155 props[i].type,
156 props[i].name);
157 if (ret) {
158 drm_property_destroy(dev, property);
159 return NULL;
160 }
161 }
162
163 return property;
164}
165EXPORT_SYMBOL(drm_property_create_enum);
166
167/**
168 * drm_property_create_bitmask - create a new bitmask property type
169 * @dev: drm device
170 * @flags: flags specifying the property type
171 * @name: name of the property
172 * @props: enumeration lists with property bitflags
173 * @num_props: size of the @props array
174 * @supported_bits: bitmask of all supported enumeration values
175 *
176 * This creates a new bitmask drm property which can then be attached to a drm
Daniel Vetter6a8a66e2016-11-23 20:23:27 +0100177 * object with drm_object_attach_property(). The returned property object must
178 * be freed with drm_property_destroy(), which is done automatically when
179 * calling drm_mode_config_cleanup().
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200180 *
181 * Compared to plain enumeration properties userspace is allowed to set any
182 * or'ed together combination of the predefined property bitflag values
183 *
184 * Returns:
185 * A pointer to the newly created property on success, NULL on failure.
186 */
187struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
188 int flags, const char *name,
189 const struct drm_prop_enum_list *props,
190 int num_props,
191 uint64_t supported_bits)
192{
193 struct drm_property *property;
194 int i, ret, index = 0;
195 int num_values = hweight64(supported_bits);
196
197 flags |= DRM_MODE_PROP_BITMASK;
198
199 property = drm_property_create(dev, flags, name, num_values);
200 if (!property)
201 return NULL;
202 for (i = 0; i < num_props; i++) {
203 if (!(supported_bits & (1ULL << props[i].type)))
204 continue;
205
206 if (WARN_ON(index >= num_values)) {
207 drm_property_destroy(dev, property);
208 return NULL;
209 }
210
211 ret = drm_property_add_enum(property, index++,
212 props[i].type,
213 props[i].name);
214 if (ret) {
215 drm_property_destroy(dev, property);
216 return NULL;
217 }
218 }
219
220 return property;
221}
222EXPORT_SYMBOL(drm_property_create_bitmask);
223
224static struct drm_property *property_create_range(struct drm_device *dev,
225 int flags, const char *name,
226 uint64_t min, uint64_t max)
227{
228 struct drm_property *property;
229
230 property = drm_property_create(dev, flags, name, 2);
231 if (!property)
232 return NULL;
233
234 property->values[0] = min;
235 property->values[1] = max;
236
237 return property;
238}
239
240/**
241 * drm_property_create_range - create a new unsigned ranged property type
242 * @dev: drm device
243 * @flags: flags specifying the property type
244 * @name: name of the property
245 * @min: minimum value of the property
246 * @max: maximum value of the property
247 *
248 * This creates a new generic drm property which can then be attached to a drm
Daniel Vetter6a8a66e2016-11-23 20:23:27 +0100249 * object with drm_object_attach_property(). The returned property object must
250 * be freed with drm_property_destroy(), which is done automatically when
251 * calling drm_mode_config_cleanup().
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200252 *
253 * Userspace is allowed to set any unsigned integer value in the (min, max)
254 * range inclusive.
255 *
256 * Returns:
257 * A pointer to the newly created property on success, NULL on failure.
258 */
259struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
260 const char *name,
261 uint64_t min, uint64_t max)
262{
263 return property_create_range(dev, DRM_MODE_PROP_RANGE | flags,
264 name, min, max);
265}
266EXPORT_SYMBOL(drm_property_create_range);
267
268/**
269 * drm_property_create_signed_range - create a new signed ranged property type
270 * @dev: drm device
271 * @flags: flags specifying the property type
272 * @name: name of the property
273 * @min: minimum value of the property
274 * @max: maximum value of the property
275 *
276 * This creates a new generic drm property which can then be attached to a drm
Daniel Vetter6a8a66e2016-11-23 20:23:27 +0100277 * object with drm_object_attach_property(). The returned property object must
278 * be freed with drm_property_destroy(), which is done automatically when
279 * calling drm_mode_config_cleanup().
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200280 *
281 * Userspace is allowed to set any signed integer value in the (min, max)
282 * range inclusive.
283 *
284 * Returns:
285 * A pointer to the newly created property on success, NULL on failure.
286 */
287struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
288 int flags, const char *name,
289 int64_t min, int64_t max)
290{
291 return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags,
292 name, I642U64(min), I642U64(max));
293}
294EXPORT_SYMBOL(drm_property_create_signed_range);
295
296/**
297 * drm_property_create_object - create a new object property type
298 * @dev: drm device
299 * @flags: flags specifying the property type
300 * @name: name of the property
301 * @type: object type from DRM_MODE_OBJECT_* defines
302 *
303 * This creates a new generic drm property which can then be attached to a drm
Daniel Vetter6a8a66e2016-11-23 20:23:27 +0100304 * object with drm_object_attach_property(). The returned property object must
305 * be freed with drm_property_destroy(), which is done automatically when
306 * calling drm_mode_config_cleanup().
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200307 *
308 * Userspace is only allowed to set this to any property value of the given
309 * @type. Only useful for atomic properties, which is enforced.
310 *
311 * Returns:
312 * A pointer to the newly created property on success, NULL on failure.
313 */
314struct drm_property *drm_property_create_object(struct drm_device *dev,
Daniel Vetterc8458c72016-08-29 10:27:57 +0200315 int flags, const char *name,
316 uint32_t type)
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200317{
318 struct drm_property *property;
319
320 flags |= DRM_MODE_PROP_OBJECT;
321
322 if (WARN_ON(!(flags & DRM_MODE_PROP_ATOMIC)))
323 return NULL;
324
325 property = drm_property_create(dev, flags, name, 1);
326 if (!property)
327 return NULL;
328
329 property->values[0] = type;
330
331 return property;
332}
333EXPORT_SYMBOL(drm_property_create_object);
334
335/**
336 * drm_property_create_bool - create a new boolean property type
337 * @dev: drm device
338 * @flags: flags specifying the property type
339 * @name: name of the property
340 *
341 * This creates a new generic drm property which can then be attached to a drm
Daniel Vetter6a8a66e2016-11-23 20:23:27 +0100342 * object with drm_object_attach_property(). The returned property object must
343 * be freed with drm_property_destroy(), which is done automatically when
344 * calling drm_mode_config_cleanup().
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200345 *
346 * This is implemented as a ranged property with only {0, 1} as valid values.
347 *
348 * Returns:
349 * A pointer to the newly created property on success, NULL on failure.
350 */
351struct drm_property *drm_property_create_bool(struct drm_device *dev, int flags,
Daniel Vetterc8458c72016-08-29 10:27:57 +0200352 const char *name)
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200353{
354 return drm_property_create_range(dev, flags, name, 0, 1);
355}
356EXPORT_SYMBOL(drm_property_create_bool);
357
358/**
359 * drm_property_add_enum - add a possible value to an enumeration property
360 * @property: enumeration property to change
361 * @index: index of the new enumeration
362 * @value: value of the new enumeration
363 * @name: symbolic name of the new enumeration
364 *
365 * This functions adds enumerations to a property.
366 *
367 * It's use is deprecated, drivers should use one of the more specific helpers
368 * to directly create the property with all enumerations already attached.
369 *
370 * Returns:
371 * Zero on success, error code on failure.
372 */
373int drm_property_add_enum(struct drm_property *property, int index,
374 uint64_t value, const char *name)
375{
376 struct drm_property_enum *prop_enum;
377
Ville Syrjälä5ebbb5b2018-03-02 16:03:00 +0200378 if (WARN_ON(strlen(name) >= DRM_PROP_NAME_LEN))
379 return -EINVAL;
380
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200381 if (!(drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
382 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
383 return -EINVAL;
384
385 /*
386 * Bitmask enum properties have the additional constraint of values
387 * from 0 to 63
388 */
389 if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK) &&
390 (value > 63))
391 return -EINVAL;
392
393 if (!list_empty(&property->enum_list)) {
394 list_for_each_entry(prop_enum, &property->enum_list, head) {
395 if (prop_enum->value == value) {
396 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
397 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
398 return 0;
399 }
400 }
401 }
402
403 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
404 if (!prop_enum)
405 return -ENOMEM;
406
407 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
408 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
409 prop_enum->value = value;
410
411 property->values[index] = value;
412 list_add_tail(&prop_enum->head, &property->enum_list);
413 return 0;
414}
415EXPORT_SYMBOL(drm_property_add_enum);
416
417/**
418 * drm_property_destroy - destroy a drm property
419 * @dev: drm device
420 * @property: property to destry
421 *
422 * This function frees a property including any attached resources like
423 * enumeration values.
424 */
425void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
426{
427 struct drm_property_enum *prop_enum, *pt;
428
429 list_for_each_entry_safe(prop_enum, pt, &property->enum_list, head) {
430 list_del(&prop_enum->head);
431 kfree(prop_enum);
432 }
433
434 if (property->num_values)
435 kfree(property->values);
436 drm_mode_object_unregister(dev, &property->base);
437 list_del(&property->head);
438 kfree(property);
439}
440EXPORT_SYMBOL(drm_property_destroy);
441
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200442int drm_mode_getproperty_ioctl(struct drm_device *dev,
443 void *data, struct drm_file *file_priv)
444{
445 struct drm_mode_get_property *out_resp = data;
446 struct drm_property *property;
447 int enum_count = 0;
448 int value_count = 0;
Daniel Vettereb8eb022017-04-03 10:32:55 +0200449 int i, copied;
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200450 struct drm_property_enum *prop_enum;
451 struct drm_mode_property_enum __user *enum_ptr;
452 uint64_t __user *values_ptr;
453
454 if (!drm_core_check_feature(dev, DRIVER_MODESET))
455 return -EINVAL;
456
Keith Packard418da172017-03-14 23:25:07 -0700457 property = drm_property_find(dev, file_priv, out_resp->prop_id);
Daniel Vettereb8eb022017-04-03 10:32:55 +0200458 if (!property)
459 return -ENOENT;
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200460
461 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
462 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
463 out_resp->flags = property->flags;
464
Daniel Vettereb8eb022017-04-03 10:32:55 +0200465 value_count = property->num_values;
466 values_ptr = u64_to_user_ptr(out_resp->values_ptr);
467
468 for (i = 0; i < value_count; i++) {
469 if (i < out_resp->count_values &&
470 put_user(property->values[i], values_ptr + i)) {
471 return -EFAULT;
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200472 }
473 }
474 out_resp->count_values = value_count;
475
Daniel Vettereb8eb022017-04-03 10:32:55 +0200476 copied = 0;
477 enum_ptr = u64_to_user_ptr(out_resp->enum_blob_ptr);
478
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200479 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
Daniel Vettereb8eb022017-04-03 10:32:55 +0200480 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
481 list_for_each_entry(prop_enum, &property->enum_list, head) {
482 enum_count++;
Daniel Vetter8cb68c82017-04-10 13:54:45 +0200483 if (out_resp->count_enum_blobs < enum_count)
Daniel Vettereb8eb022017-04-03 10:32:55 +0200484 continue;
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200485
Daniel Vettereb8eb022017-04-03 10:32:55 +0200486 if (copy_to_user(&enum_ptr[copied].value,
487 &prop_enum->value, sizeof(uint64_t)))
488 return -EFAULT;
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200489
Daniel Vettereb8eb022017-04-03 10:32:55 +0200490 if (copy_to_user(&enum_ptr[copied].name,
491 &prop_enum->name, DRM_PROP_NAME_LEN))
492 return -EFAULT;
493 copied++;
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200494 }
495 out_resp->count_enum_blobs = enum_count;
496 }
497
498 /*
499 * NOTE: The idea seems to have been to use this to read all the blob
500 * property values. But nothing ever added them to the corresponding
501 * list, userspace always used the special-purpose get_blob ioctl to
502 * read the value for a blob property. It also doesn't make a lot of
503 * sense to return values here when everything else is just metadata for
504 * the property itself.
505 */
506 if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
507 out_resp->count_enum_blobs = 0;
Daniel Vettereb8eb022017-04-03 10:32:55 +0200508
509 return 0;
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200510}
511
512static void drm_property_free_blob(struct kref *kref)
513{
514 struct drm_property_blob *blob =
515 container_of(kref, struct drm_property_blob, base.refcount);
516
517 mutex_lock(&blob->dev->mode_config.blob_lock);
518 list_del(&blob->head_global);
519 mutex_unlock(&blob->dev->mode_config.blob_lock);
520
521 drm_mode_object_unregister(blob->dev, &blob->base);
522
523 kfree(blob);
524}
525
526/**
527 * drm_property_create_blob - Create new blob property
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200528 * @dev: DRM device to create property for
529 * @length: Length to allocate for blob data
530 * @data: If specified, copies data into blob
531 *
Daniel Vetterc8458c72016-08-29 10:27:57 +0200532 * Creates a new blob property for a specified DRM device, optionally
533 * copying data. Note that blob properties are meant to be invariant, hence the
534 * data must be filled out before the blob is used as the value of any property.
535 *
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200536 * Returns:
537 * New blob property with a single reference on success, or an ERR_PTR
538 * value on failure.
539 */
540struct drm_property_blob *
541drm_property_create_blob(struct drm_device *dev, size_t length,
542 const void *data)
543{
544 struct drm_property_blob *blob;
545 int ret;
546
547 if (!length || length > ULONG_MAX - sizeof(struct drm_property_blob))
548 return ERR_PTR(-EINVAL);
549
550 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
551 if (!blob)
552 return ERR_PTR(-ENOMEM);
553
554 /* This must be explicitly initialised, so we can safely call list_del
555 * on it in the removal handler, even if it isn't in a file list. */
556 INIT_LIST_HEAD(&blob->head_file);
557 blob->length = length;
558 blob->dev = dev;
559
560 if (data)
561 memcpy(blob->data, data, length);
562
Thierry Reding2135ea72017-02-28 15:46:37 +0100563 ret = __drm_mode_object_add(dev, &blob->base, DRM_MODE_OBJECT_BLOB,
564 true, drm_property_free_blob);
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200565 if (ret) {
566 kfree(blob);
567 return ERR_PTR(-EINVAL);
568 }
569
570 mutex_lock(&dev->mode_config.blob_lock);
571 list_add_tail(&blob->head_global,
572 &dev->mode_config.property_blob_list);
573 mutex_unlock(&dev->mode_config.blob_lock);
574
575 return blob;
576}
577EXPORT_SYMBOL(drm_property_create_blob);
578
579/**
Thierry Reding6472e502017-02-28 15:46:42 +0100580 * drm_property_blob_put - release a blob property reference
581 * @blob: DRM blob property
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200582 *
Thierry Reding6472e502017-02-28 15:46:42 +0100583 * Releases a reference to a blob property. May free the object.
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200584 */
Thierry Reding6472e502017-02-28 15:46:42 +0100585void drm_property_blob_put(struct drm_property_blob *blob)
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200586{
587 if (!blob)
588 return;
589
Thierry Reding020a2182017-02-28 15:46:38 +0100590 drm_mode_object_put(&blob->base);
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200591}
Thierry Reding6472e502017-02-28 15:46:42 +0100592EXPORT_SYMBOL(drm_property_blob_put);
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200593
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200594void drm_property_destroy_user_blobs(struct drm_device *dev,
595 struct drm_file *file_priv)
596{
597 struct drm_property_blob *blob, *bt;
598
599 /*
600 * When the file gets released that means no one else can access the
601 * blob list any more, so no need to grab dev->blob_lock.
602 */
603 list_for_each_entry_safe(blob, bt, &file_priv->blobs, head_file) {
604 list_del_init(&blob->head_file);
Thierry Reding6472e502017-02-28 15:46:42 +0100605 drm_property_blob_put(blob);
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200606 }
607}
608
609/**
Thierry Reding6472e502017-02-28 15:46:42 +0100610 * drm_property_blob_get - acquire blob property reference
611 * @blob: DRM blob property
Daniel Vetterc8458c72016-08-29 10:27:57 +0200612 *
Thierry Reding6472e502017-02-28 15:46:42 +0100613 * Acquires a reference to an existing blob property. Returns @blob, which
Daniel Vetterc8458c72016-08-29 10:27:57 +0200614 * allows this to be used as a shorthand in assignments.
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200615 */
Thierry Reding6472e502017-02-28 15:46:42 +0100616struct drm_property_blob *drm_property_blob_get(struct drm_property_blob *blob)
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200617{
Thierry Reding020a2182017-02-28 15:46:38 +0100618 drm_mode_object_get(&blob->base);
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200619 return blob;
620}
Thierry Reding6472e502017-02-28 15:46:42 +0100621EXPORT_SYMBOL(drm_property_blob_get);
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200622
623/**
624 * drm_property_lookup_blob - look up a blob property and take a reference
625 * @dev: drm device
626 * @id: id of the blob property
627 *
628 * If successful, this takes an additional reference to the blob property.
629 * callers need to make sure to eventually unreference the returned property
Thierry Reding6472e502017-02-28 15:46:42 +0100630 * again, using drm_property_blob_put().
Daniel Vetterc8458c72016-08-29 10:27:57 +0200631 *
632 * Return:
633 * NULL on failure, pointer to the blob on success.
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200634 */
635struct drm_property_blob *drm_property_lookup_blob(struct drm_device *dev,
636 uint32_t id)
637{
638 struct drm_mode_object *obj;
639 struct drm_property_blob *blob = NULL;
640
Keith Packard418da172017-03-14 23:25:07 -0700641 obj = __drm_mode_object_find(dev, NULL, id, DRM_MODE_OBJECT_BLOB);
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200642 if (obj)
643 blob = obj_to_blob(obj);
644 return blob;
645}
646EXPORT_SYMBOL(drm_property_lookup_blob);
647
648/**
Daniel Vetterc8458c72016-08-29 10:27:57 +0200649 * drm_property_replace_global_blob - replace existing blob property
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200650 * @dev: drm device
651 * @replace: location of blob property pointer to be replaced
652 * @length: length of data for new blob, or 0 for no data
653 * @data: content for new blob, or NULL for no data
654 * @obj_holds_id: optional object for property holding blob ID
655 * @prop_holds_id: optional property holding blob ID
656 * @return 0 on success or error on failure
657 *
Daniel Vetterc8458c72016-08-29 10:27:57 +0200658 * This function will replace a global property in the blob list, optionally
659 * updating a property which holds the ID of that property.
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200660 *
661 * If length is 0 or data is NULL, no new blob will be created, and the holding
662 * property, if specified, will be set to 0.
663 *
664 * Access to the replace pointer is assumed to be protected by the caller, e.g.
665 * by holding the relevant modesetting object lock for its parent.
666 *
667 * For example, a drm_connector has a 'PATH' property, which contains the ID
668 * of a blob property with the value of the MST path information. Calling this
669 * function with replace pointing to the connector's path_blob_ptr, length and
670 * data set for the new path information, obj_holds_id set to the connector's
671 * base object, and prop_holds_id set to the path property name, will perform
672 * a completely atomic update. The access to path_blob_ptr is protected by the
673 * caller holding a lock on the connector.
674 */
675int drm_property_replace_global_blob(struct drm_device *dev,
676 struct drm_property_blob **replace,
677 size_t length,
678 const void *data,
679 struct drm_mode_object *obj_holds_id,
680 struct drm_property *prop_holds_id)
681{
682 struct drm_property_blob *new_blob = NULL;
683 struct drm_property_blob *old_blob = NULL;
684 int ret;
685
686 WARN_ON(replace == NULL);
687
688 old_blob = *replace;
689
690 if (length && data) {
691 new_blob = drm_property_create_blob(dev, length, data);
692 if (IS_ERR(new_blob))
693 return PTR_ERR(new_blob);
694 }
695
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200696 if (obj_holds_id) {
697 ret = drm_object_property_set_value(obj_holds_id,
698 prop_holds_id,
699 new_blob ?
700 new_blob->base.id : 0);
701 if (ret != 0)
702 goto err_created;
703 }
704
Thierry Reding6472e502017-02-28 15:46:42 +0100705 drm_property_blob_put(old_blob);
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200706 *replace = new_blob;
707
708 return 0;
709
710err_created:
Thierry Reding6472e502017-02-28 15:46:42 +0100711 drm_property_blob_put(new_blob);
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200712 return ret;
713}
714EXPORT_SYMBOL(drm_property_replace_global_blob);
715
Peter Rosin5f057ff2017-07-13 18:25:25 +0200716/**
717 * drm_property_replace_blob - replace a blob property
718 * @blob: a pointer to the member blob to be replaced
719 * @new_blob: the new blob to replace with
720 *
721 * Return: true if the blob was in fact replaced.
722 */
723bool drm_property_replace_blob(struct drm_property_blob **blob,
724 struct drm_property_blob *new_blob)
725{
726 struct drm_property_blob *old_blob = *blob;
727
728 if (old_blob == new_blob)
729 return false;
730
731 drm_property_blob_put(old_blob);
732 if (new_blob)
733 drm_property_blob_get(new_blob);
734 *blob = new_blob;
735 return true;
736}
737EXPORT_SYMBOL(drm_property_replace_blob);
738
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200739int 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;
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200745
746 if (!drm_core_check_feature(dev, DRIVER_MODESET))
747 return -EINVAL;
748
749 blob = drm_property_lookup_blob(dev, out_resp->blob_id);
750 if (!blob)
751 return -ENOENT;
752
753 if (out_resp->length == blob->length) {
Chris Wilson75df6242016-11-27 17:09:08 +0000754 if (copy_to_user(u64_to_user_ptr(out_resp->data),
755 blob->data,
756 blob->length)) {
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200757 ret = -EFAULT;
758 goto unref;
759 }
760 }
761 out_resp->length = blob->length;
762unref:
Thierry Reding6472e502017-02-28 15:46:42 +0100763 drm_property_blob_put(blob);
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200764
765 return ret;
766}
767
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200768int drm_mode_createblob_ioctl(struct drm_device *dev,
769 void *data, struct drm_file *file_priv)
770{
771 struct drm_mode_create_blob *out_resp = data;
772 struct drm_property_blob *blob;
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200773 int ret = 0;
774
775 if (!drm_core_check_feature(dev, DRIVER_MODESET))
776 return -EINVAL;
777
778 blob = drm_property_create_blob(dev, out_resp->length, NULL);
779 if (IS_ERR(blob))
780 return PTR_ERR(blob);
781
Chris Wilson75df6242016-11-27 17:09:08 +0000782 if (copy_from_user(blob->data,
783 u64_to_user_ptr(out_resp->data),
784 out_resp->length)) {
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200785 ret = -EFAULT;
786 goto out_blob;
787 }
788
789 /* Dropping the lock between create_blob and our access here is safe
790 * as only the same file_priv can remove the blob; at this point, it is
791 * not associated with any file_priv. */
792 mutex_lock(&dev->mode_config.blob_lock);
793 out_resp->blob_id = blob->base.id;
794 list_add_tail(&blob->head_file, &file_priv->blobs);
795 mutex_unlock(&dev->mode_config.blob_lock);
796
797 return 0;
798
799out_blob:
Thierry Reding6472e502017-02-28 15:46:42 +0100800 drm_property_blob_put(blob);
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200801 return ret;
802}
803
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200804int drm_mode_destroyblob_ioctl(struct drm_device *dev,
805 void *data, struct drm_file *file_priv)
806{
807 struct drm_mode_destroy_blob *out_resp = data;
808 struct drm_property_blob *blob = NULL, *bt;
809 bool found = false;
810 int ret = 0;
811
812 if (!drm_core_check_feature(dev, DRIVER_MODESET))
813 return -EINVAL;
814
815 blob = drm_property_lookup_blob(dev, out_resp->blob_id);
816 if (!blob)
817 return -ENOENT;
818
819 mutex_lock(&dev->mode_config.blob_lock);
820 /* Ensure the property was actually created by this user. */
821 list_for_each_entry(bt, &file_priv->blobs, head_file) {
822 if (bt == blob) {
823 found = true;
824 break;
825 }
826 }
827
828 if (!found) {
829 ret = -EPERM;
830 goto err;
831 }
832
833 /* We must drop head_file here, because we may not be the last
834 * reference on the blob. */
835 list_del_init(&blob->head_file);
836 mutex_unlock(&dev->mode_config.blob_lock);
837
838 /* One reference from lookup, and one from the filp. */
Thierry Reding6472e502017-02-28 15:46:42 +0100839 drm_property_blob_put(blob);
840 drm_property_blob_put(blob);
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200841
842 return 0;
843
844err:
845 mutex_unlock(&dev->mode_config.blob_lock);
Thierry Reding6472e502017-02-28 15:46:42 +0100846 drm_property_blob_put(blob);
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200847
848 return ret;
849}
850
851/* Some properties could refer to dynamic refcnt'd objects, or things that
852 * need special locking to handle lifetime issues (ie. to ensure the prop
853 * value doesn't become invalid part way through the property update due to
854 * race). The value returned by reference via 'obj' should be passed back
855 * to drm_property_change_valid_put() after the property is set (and the
856 * object to which the property is attached has a chance to take it's own
857 * reference).
858 */
859bool drm_property_change_valid_get(struct drm_property *property,
Daniel Vetterc8458c72016-08-29 10:27:57 +0200860 uint64_t value, struct drm_mode_object **ref)
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200861{
862 int i;
863
864 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
865 return false;
866
867 *ref = NULL;
868
869 if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) {
870 if (value < property->values[0] || value > property->values[1])
871 return false;
872 return true;
873 } else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) {
874 int64_t svalue = U642I64(value);
875
876 if (svalue < U642I64(property->values[0]) ||
877 svalue > U642I64(property->values[1]))
878 return false;
879 return true;
880 } else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
881 uint64_t valid_mask = 0;
882
883 for (i = 0; i < property->num_values; i++)
884 valid_mask |= (1ULL << property->values[i]);
885 return !(value & ~valid_mask);
Maarten Lankhorst30c06572016-09-07 11:52:40 +0200886 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
887 struct drm_property_blob *blob;
888
889 if (value == 0)
890 return true;
891
892 blob = drm_property_lookup_blob(property->dev, value);
893 if (blob) {
894 *ref = &blob->base;
895 return true;
896 } else {
897 return false;
898 }
899 } else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200900 /* a zero value for an object property translates to null: */
901 if (value == 0)
902 return true;
903
Keith Packard418da172017-03-14 23:25:07 -0700904 *ref = __drm_mode_object_find(property->dev, NULL, value,
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200905 property->values[0]);
906 return *ref != NULL;
907 }
908
909 for (i = 0; i < property->num_values; i++)
910 if (property->values[i] == value)
911 return true;
912 return false;
913}
914
915void drm_property_change_valid_put(struct drm_property *property,
Maarten Lankhorst30c06572016-09-07 11:52:40 +0200916 struct drm_mode_object *ref)
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200917{
918 if (!ref)
919 return;
920
Maarten Lankhorst30c06572016-09-07 11:52:40 +0200921 if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
Thierry Reding020a2182017-02-28 15:46:38 +0100922 drm_mode_object_put(ref);
Maarten Lankhorst30c06572016-09-07 11:52:40 +0200923 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
Thierry Reding6472e502017-02-28 15:46:42 +0100924 drm_property_blob_put(obj_to_blob(ref));
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200925}