blob: e676b1ecc705f0c6a24a714ab4da628ceb95d424 [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
Ville Syrjälä1371f262018-03-06 18:48:45 +0200381 if (WARN_ON(!drm_property_type_is(property, DRM_MODE_PROP_ENUM) &&
382 !drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200383 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
Ville Syrjälä6f881d02018-03-06 18:48:44 +0200393 list_for_each_entry(prop_enum, &property->enum_list, head) {
394 if (WARN_ON(prop_enum->value == value))
395 return -EINVAL;
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200396 }
397
398 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
399 if (!prop_enum)
400 return -ENOMEM;
401
402 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
403 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
404 prop_enum->value = value;
405
406 property->values[index] = value;
407 list_add_tail(&prop_enum->head, &property->enum_list);
408 return 0;
409}
410EXPORT_SYMBOL(drm_property_add_enum);
411
412/**
413 * drm_property_destroy - destroy a drm property
414 * @dev: drm device
415 * @property: property to destry
416 *
417 * This function frees a property including any attached resources like
418 * enumeration values.
419 */
420void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
421{
422 struct drm_property_enum *prop_enum, *pt;
423
424 list_for_each_entry_safe(prop_enum, pt, &property->enum_list, head) {
425 list_del(&prop_enum->head);
426 kfree(prop_enum);
427 }
428
429 if (property->num_values)
430 kfree(property->values);
431 drm_mode_object_unregister(dev, &property->base);
432 list_del(&property->head);
433 kfree(property);
434}
435EXPORT_SYMBOL(drm_property_destroy);
436
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200437int drm_mode_getproperty_ioctl(struct drm_device *dev,
438 void *data, struct drm_file *file_priv)
439{
440 struct drm_mode_get_property *out_resp = data;
441 struct drm_property *property;
442 int enum_count = 0;
443 int value_count = 0;
Daniel Vettereb8eb022017-04-03 10:32:55 +0200444 int i, copied;
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200445 struct drm_property_enum *prop_enum;
446 struct drm_mode_property_enum __user *enum_ptr;
447 uint64_t __user *values_ptr;
448
449 if (!drm_core_check_feature(dev, DRIVER_MODESET))
450 return -EINVAL;
451
Keith Packard418da172017-03-14 23:25:07 -0700452 property = drm_property_find(dev, file_priv, out_resp->prop_id);
Daniel Vettereb8eb022017-04-03 10:32:55 +0200453 if (!property)
454 return -ENOENT;
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200455
456 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
457 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
458 out_resp->flags = property->flags;
459
Daniel Vettereb8eb022017-04-03 10:32:55 +0200460 value_count = property->num_values;
461 values_ptr = u64_to_user_ptr(out_resp->values_ptr);
462
463 for (i = 0; i < value_count; i++) {
464 if (i < out_resp->count_values &&
465 put_user(property->values[i], values_ptr + i)) {
466 return -EFAULT;
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200467 }
468 }
469 out_resp->count_values = value_count;
470
Daniel Vettereb8eb022017-04-03 10:32:55 +0200471 copied = 0;
472 enum_ptr = u64_to_user_ptr(out_resp->enum_blob_ptr);
473
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200474 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
Daniel Vettereb8eb022017-04-03 10:32:55 +0200475 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
476 list_for_each_entry(prop_enum, &property->enum_list, head) {
477 enum_count++;
Daniel Vetter8cb68c82017-04-10 13:54:45 +0200478 if (out_resp->count_enum_blobs < enum_count)
Daniel Vettereb8eb022017-04-03 10:32:55 +0200479 continue;
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200480
Daniel Vettereb8eb022017-04-03 10:32:55 +0200481 if (copy_to_user(&enum_ptr[copied].value,
482 &prop_enum->value, sizeof(uint64_t)))
483 return -EFAULT;
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200484
Daniel Vettereb8eb022017-04-03 10:32:55 +0200485 if (copy_to_user(&enum_ptr[copied].name,
486 &prop_enum->name, DRM_PROP_NAME_LEN))
487 return -EFAULT;
488 copied++;
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200489 }
490 out_resp->count_enum_blobs = enum_count;
491 }
492
493 /*
494 * NOTE: The idea seems to have been to use this to read all the blob
495 * property values. But nothing ever added them to the corresponding
496 * list, userspace always used the special-purpose get_blob ioctl to
497 * read the value for a blob property. It also doesn't make a lot of
498 * sense to return values here when everything else is just metadata for
499 * the property itself.
500 */
501 if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
502 out_resp->count_enum_blobs = 0;
Daniel Vettereb8eb022017-04-03 10:32:55 +0200503
504 return 0;
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200505}
506
507static void drm_property_free_blob(struct kref *kref)
508{
509 struct drm_property_blob *blob =
510 container_of(kref, struct drm_property_blob, base.refcount);
511
512 mutex_lock(&blob->dev->mode_config.blob_lock);
513 list_del(&blob->head_global);
514 mutex_unlock(&blob->dev->mode_config.blob_lock);
515
516 drm_mode_object_unregister(blob->dev, &blob->base);
517
518 kfree(blob);
519}
520
521/**
522 * drm_property_create_blob - Create new blob property
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200523 * @dev: DRM device to create property for
524 * @length: Length to allocate for blob data
525 * @data: If specified, copies data into blob
526 *
Daniel Vetterc8458c72016-08-29 10:27:57 +0200527 * Creates a new blob property for a specified DRM device, optionally
528 * copying data. Note that blob properties are meant to be invariant, hence the
529 * data must be filled out before the blob is used as the value of any property.
530 *
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200531 * Returns:
532 * New blob property with a single reference on success, or an ERR_PTR
533 * value on failure.
534 */
535struct drm_property_blob *
536drm_property_create_blob(struct drm_device *dev, size_t length,
537 const void *data)
538{
539 struct drm_property_blob *blob;
540 int ret;
541
542 if (!length || length > ULONG_MAX - sizeof(struct drm_property_blob))
543 return ERR_PTR(-EINVAL);
544
545 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
546 if (!blob)
547 return ERR_PTR(-ENOMEM);
548
549 /* This must be explicitly initialised, so we can safely call list_del
550 * on it in the removal handler, even if it isn't in a file list. */
551 INIT_LIST_HEAD(&blob->head_file);
552 blob->length = length;
553 blob->dev = dev;
554
555 if (data)
556 memcpy(blob->data, data, length);
557
Thierry Reding2135ea72017-02-28 15:46:37 +0100558 ret = __drm_mode_object_add(dev, &blob->base, DRM_MODE_OBJECT_BLOB,
559 true, drm_property_free_blob);
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200560 if (ret) {
561 kfree(blob);
562 return ERR_PTR(-EINVAL);
563 }
564
565 mutex_lock(&dev->mode_config.blob_lock);
566 list_add_tail(&blob->head_global,
567 &dev->mode_config.property_blob_list);
568 mutex_unlock(&dev->mode_config.blob_lock);
569
570 return blob;
571}
572EXPORT_SYMBOL(drm_property_create_blob);
573
574/**
Thierry Reding6472e502017-02-28 15:46:42 +0100575 * drm_property_blob_put - release a blob property reference
576 * @blob: DRM blob property
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200577 *
Thierry Reding6472e502017-02-28 15:46:42 +0100578 * Releases a reference to a blob property. May free the object.
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200579 */
Thierry Reding6472e502017-02-28 15:46:42 +0100580void drm_property_blob_put(struct drm_property_blob *blob)
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200581{
582 if (!blob)
583 return;
584
Thierry Reding020a2182017-02-28 15:46:38 +0100585 drm_mode_object_put(&blob->base);
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200586}
Thierry Reding6472e502017-02-28 15:46:42 +0100587EXPORT_SYMBOL(drm_property_blob_put);
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200588
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200589void drm_property_destroy_user_blobs(struct drm_device *dev,
590 struct drm_file *file_priv)
591{
592 struct drm_property_blob *blob, *bt;
593
594 /*
595 * When the file gets released that means no one else can access the
596 * blob list any more, so no need to grab dev->blob_lock.
597 */
598 list_for_each_entry_safe(blob, bt, &file_priv->blobs, head_file) {
599 list_del_init(&blob->head_file);
Thierry Reding6472e502017-02-28 15:46:42 +0100600 drm_property_blob_put(blob);
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200601 }
602}
603
604/**
Thierry Reding6472e502017-02-28 15:46:42 +0100605 * drm_property_blob_get - acquire blob property reference
606 * @blob: DRM blob property
Daniel Vetterc8458c72016-08-29 10:27:57 +0200607 *
Thierry Reding6472e502017-02-28 15:46:42 +0100608 * Acquires a reference to an existing blob property. Returns @blob, which
Daniel Vetterc8458c72016-08-29 10:27:57 +0200609 * allows this to be used as a shorthand in assignments.
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200610 */
Thierry Reding6472e502017-02-28 15:46:42 +0100611struct drm_property_blob *drm_property_blob_get(struct drm_property_blob *blob)
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200612{
Thierry Reding020a2182017-02-28 15:46:38 +0100613 drm_mode_object_get(&blob->base);
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200614 return blob;
615}
Thierry Reding6472e502017-02-28 15:46:42 +0100616EXPORT_SYMBOL(drm_property_blob_get);
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200617
618/**
619 * drm_property_lookup_blob - look up a blob property and take a reference
620 * @dev: drm device
621 * @id: id of the blob property
622 *
623 * If successful, this takes an additional reference to the blob property.
624 * callers need to make sure to eventually unreference the returned property
Thierry Reding6472e502017-02-28 15:46:42 +0100625 * again, using drm_property_blob_put().
Daniel Vetterc8458c72016-08-29 10:27:57 +0200626 *
627 * Return:
628 * NULL on failure, pointer to the blob on success.
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200629 */
630struct drm_property_blob *drm_property_lookup_blob(struct drm_device *dev,
631 uint32_t id)
632{
633 struct drm_mode_object *obj;
634 struct drm_property_blob *blob = NULL;
635
Keith Packard418da172017-03-14 23:25:07 -0700636 obj = __drm_mode_object_find(dev, NULL, id, DRM_MODE_OBJECT_BLOB);
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200637 if (obj)
638 blob = obj_to_blob(obj);
639 return blob;
640}
641EXPORT_SYMBOL(drm_property_lookup_blob);
642
643/**
Daniel Vetterc8458c72016-08-29 10:27:57 +0200644 * drm_property_replace_global_blob - replace existing blob property
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200645 * @dev: drm device
646 * @replace: location of blob property pointer to be replaced
647 * @length: length of data for new blob, or 0 for no data
648 * @data: content for new blob, or NULL for no data
649 * @obj_holds_id: optional object for property holding blob ID
650 * @prop_holds_id: optional property holding blob ID
651 * @return 0 on success or error on failure
652 *
Daniel Vetterc8458c72016-08-29 10:27:57 +0200653 * This function will replace a global property in the blob list, optionally
654 * updating a property which holds the ID of that property.
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200655 *
656 * If length is 0 or data is NULL, no new blob will be created, and the holding
657 * property, if specified, will be set to 0.
658 *
659 * Access to the replace pointer is assumed to be protected by the caller, e.g.
660 * by holding the relevant modesetting object lock for its parent.
661 *
662 * For example, a drm_connector has a 'PATH' property, which contains the ID
663 * of a blob property with the value of the MST path information. Calling this
664 * function with replace pointing to the connector's path_blob_ptr, length and
665 * data set for the new path information, obj_holds_id set to the connector's
666 * base object, and prop_holds_id set to the path property name, will perform
667 * a completely atomic update. The access to path_blob_ptr is protected by the
668 * caller holding a lock on the connector.
669 */
670int drm_property_replace_global_blob(struct drm_device *dev,
671 struct drm_property_blob **replace,
672 size_t length,
673 const void *data,
674 struct drm_mode_object *obj_holds_id,
675 struct drm_property *prop_holds_id)
676{
677 struct drm_property_blob *new_blob = NULL;
678 struct drm_property_blob *old_blob = NULL;
679 int ret;
680
681 WARN_ON(replace == NULL);
682
683 old_blob = *replace;
684
685 if (length && data) {
686 new_blob = drm_property_create_blob(dev, length, data);
687 if (IS_ERR(new_blob))
688 return PTR_ERR(new_blob);
689 }
690
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200691 if (obj_holds_id) {
692 ret = drm_object_property_set_value(obj_holds_id,
693 prop_holds_id,
694 new_blob ?
695 new_blob->base.id : 0);
696 if (ret != 0)
697 goto err_created;
698 }
699
Thierry Reding6472e502017-02-28 15:46:42 +0100700 drm_property_blob_put(old_blob);
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200701 *replace = new_blob;
702
703 return 0;
704
705err_created:
Thierry Reding6472e502017-02-28 15:46:42 +0100706 drm_property_blob_put(new_blob);
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200707 return ret;
708}
709EXPORT_SYMBOL(drm_property_replace_global_blob);
710
Peter Rosin5f057ff2017-07-13 18:25:25 +0200711/**
712 * drm_property_replace_blob - replace a blob property
713 * @blob: a pointer to the member blob to be replaced
714 * @new_blob: the new blob to replace with
715 *
716 * Return: true if the blob was in fact replaced.
717 */
718bool drm_property_replace_blob(struct drm_property_blob **blob,
719 struct drm_property_blob *new_blob)
720{
721 struct drm_property_blob *old_blob = *blob;
722
723 if (old_blob == new_blob)
724 return false;
725
726 drm_property_blob_put(old_blob);
727 if (new_blob)
728 drm_property_blob_get(new_blob);
729 *blob = new_blob;
730 return true;
731}
732EXPORT_SYMBOL(drm_property_replace_blob);
733
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200734int drm_mode_getblob_ioctl(struct drm_device *dev,
735 void *data, struct drm_file *file_priv)
736{
737 struct drm_mode_get_blob *out_resp = data;
738 struct drm_property_blob *blob;
739 int ret = 0;
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200740
741 if (!drm_core_check_feature(dev, DRIVER_MODESET))
742 return -EINVAL;
743
744 blob = drm_property_lookup_blob(dev, out_resp->blob_id);
745 if (!blob)
746 return -ENOENT;
747
748 if (out_resp->length == blob->length) {
Chris Wilson75df6242016-11-27 17:09:08 +0000749 if (copy_to_user(u64_to_user_ptr(out_resp->data),
750 blob->data,
751 blob->length)) {
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200752 ret = -EFAULT;
753 goto unref;
754 }
755 }
756 out_resp->length = blob->length;
757unref:
Thierry Reding6472e502017-02-28 15:46:42 +0100758 drm_property_blob_put(blob);
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200759
760 return ret;
761}
762
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200763int drm_mode_createblob_ioctl(struct drm_device *dev,
764 void *data, struct drm_file *file_priv)
765{
766 struct drm_mode_create_blob *out_resp = data;
767 struct drm_property_blob *blob;
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200768 int ret = 0;
769
770 if (!drm_core_check_feature(dev, DRIVER_MODESET))
771 return -EINVAL;
772
773 blob = drm_property_create_blob(dev, out_resp->length, NULL);
774 if (IS_ERR(blob))
775 return PTR_ERR(blob);
776
Chris Wilson75df6242016-11-27 17:09:08 +0000777 if (copy_from_user(blob->data,
778 u64_to_user_ptr(out_resp->data),
779 out_resp->length)) {
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200780 ret = -EFAULT;
781 goto out_blob;
782 }
783
784 /* Dropping the lock between create_blob and our access here is safe
785 * as only the same file_priv can remove the blob; at this point, it is
786 * not associated with any file_priv. */
787 mutex_lock(&dev->mode_config.blob_lock);
788 out_resp->blob_id = blob->base.id;
789 list_add_tail(&blob->head_file, &file_priv->blobs);
790 mutex_unlock(&dev->mode_config.blob_lock);
791
792 return 0;
793
794out_blob:
Thierry Reding6472e502017-02-28 15:46:42 +0100795 drm_property_blob_put(blob);
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200796 return ret;
797}
798
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200799int drm_mode_destroyblob_ioctl(struct drm_device *dev,
800 void *data, struct drm_file *file_priv)
801{
802 struct drm_mode_destroy_blob *out_resp = data;
803 struct drm_property_blob *blob = NULL, *bt;
804 bool found = false;
805 int ret = 0;
806
807 if (!drm_core_check_feature(dev, DRIVER_MODESET))
808 return -EINVAL;
809
810 blob = drm_property_lookup_blob(dev, out_resp->blob_id);
811 if (!blob)
812 return -ENOENT;
813
814 mutex_lock(&dev->mode_config.blob_lock);
815 /* Ensure the property was actually created by this user. */
816 list_for_each_entry(bt, &file_priv->blobs, head_file) {
817 if (bt == blob) {
818 found = true;
819 break;
820 }
821 }
822
823 if (!found) {
824 ret = -EPERM;
825 goto err;
826 }
827
828 /* We must drop head_file here, because we may not be the last
829 * reference on the blob. */
830 list_del_init(&blob->head_file);
831 mutex_unlock(&dev->mode_config.blob_lock);
832
833 /* One reference from lookup, and one from the filp. */
Thierry Reding6472e502017-02-28 15:46:42 +0100834 drm_property_blob_put(blob);
835 drm_property_blob_put(blob);
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200836
837 return 0;
838
839err:
840 mutex_unlock(&dev->mode_config.blob_lock);
Thierry Reding6472e502017-02-28 15:46:42 +0100841 drm_property_blob_put(blob);
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200842
843 return ret;
844}
845
846/* Some properties could refer to dynamic refcnt'd objects, or things that
847 * need special locking to handle lifetime issues (ie. to ensure the prop
848 * value doesn't become invalid part way through the property update due to
849 * race). The value returned by reference via 'obj' should be passed back
850 * to drm_property_change_valid_put() after the property is set (and the
851 * object to which the property is attached has a chance to take it's own
852 * reference).
853 */
854bool drm_property_change_valid_get(struct drm_property *property,
Daniel Vetterc8458c72016-08-29 10:27:57 +0200855 uint64_t value, struct drm_mode_object **ref)
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200856{
857 int i;
858
859 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
860 return false;
861
862 *ref = NULL;
863
864 if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) {
865 if (value < property->values[0] || value > property->values[1])
866 return false;
867 return true;
868 } else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) {
869 int64_t svalue = U642I64(value);
870
871 if (svalue < U642I64(property->values[0]) ||
872 svalue > U642I64(property->values[1]))
873 return false;
874 return true;
875 } else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
876 uint64_t valid_mask = 0;
877
878 for (i = 0; i < property->num_values; i++)
879 valid_mask |= (1ULL << property->values[i]);
880 return !(value & ~valid_mask);
Maarten Lankhorst30c06572016-09-07 11:52:40 +0200881 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
882 struct drm_property_blob *blob;
883
884 if (value == 0)
885 return true;
886
887 blob = drm_property_lookup_blob(property->dev, value);
888 if (blob) {
889 *ref = &blob->base;
890 return true;
891 } else {
892 return false;
893 }
894 } else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200895 /* a zero value for an object property translates to null: */
896 if (value == 0)
897 return true;
898
Keith Packard418da172017-03-14 23:25:07 -0700899 *ref = __drm_mode_object_find(property->dev, NULL, value,
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200900 property->values[0]);
901 return *ref != NULL;
902 }
903
904 for (i = 0; i < property->num_values; i++)
905 if (property->values[i] == value)
906 return true;
907 return false;
908}
909
910void drm_property_change_valid_put(struct drm_property *property,
Maarten Lankhorst30c06572016-09-07 11:52:40 +0200911 struct drm_mode_object *ref)
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200912{
913 if (!ref)
914 return;
915
Maarten Lankhorst30c06572016-09-07 11:52:40 +0200916 if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
Thierry Reding020a2182017-02-28 15:46:38 +0100917 drm_mode_object_put(ref);
Maarten Lankhorst30c06572016-09-07 11:52:40 +0200918 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
Thierry Reding6472e502017-02-28 15:46:42 +0100919 drm_property_blob_put(obj_to_blob(ref));
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200920}