blob: 3e88fa24eab3d382b037a873c6849fe32ead1d46 [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
81 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
82 if (!property)
83 return NULL;
84
85 property->dev = dev;
86
87 if (num_values) {
88 property->values = kcalloc(num_values, sizeof(uint64_t),
89 GFP_KERNEL);
90 if (!property->values)
91 goto fail;
92 }
93
Thierry Reding2135ea72017-02-28 15:46:37 +010094 ret = drm_mode_object_add(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
Daniel Vetter59e71ee2016-08-29 10:27:55 +020095 if (ret)
96 goto fail;
97
98 property->flags = flags;
99 property->num_values = num_values;
100 INIT_LIST_HEAD(&property->enum_list);
101
102 if (name) {
103 strncpy(property->name, name, DRM_PROP_NAME_LEN);
104 property->name[DRM_PROP_NAME_LEN-1] = '\0';
105 }
106
107 list_add_tail(&property->head, &dev->mode_config.property_list);
108
109 WARN_ON(!drm_property_type_valid(property));
110
111 return property;
112fail:
113 kfree(property->values);
114 kfree(property);
115 return NULL;
116}
117EXPORT_SYMBOL(drm_property_create);
118
119/**
120 * drm_property_create_enum - create a new enumeration property type
121 * @dev: drm device
122 * @flags: flags specifying the property type
123 * @name: name of the property
124 * @props: enumeration lists with property values
125 * @num_values: number of pre-defined values
126 *
127 * This creates a new generic drm property which can then be attached to a drm
Daniel Vetter6a8a66e2016-11-23 20:23:27 +0100128 * object with drm_object_attach_property(). The returned property object must
129 * be freed with drm_property_destroy(), which is done automatically when
130 * calling drm_mode_config_cleanup().
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200131 *
132 * Userspace is only allowed to set one of the predefined values for enumeration
133 * properties.
134 *
135 * Returns:
136 * A pointer to the newly created property on success, NULL on failure.
137 */
138struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
139 const char *name,
140 const struct drm_prop_enum_list *props,
141 int num_values)
142{
143 struct drm_property *property;
144 int i, ret;
145
146 flags |= DRM_MODE_PROP_ENUM;
147
148 property = drm_property_create(dev, flags, name, num_values);
149 if (!property)
150 return NULL;
151
152 for (i = 0; i < num_values; i++) {
153 ret = drm_property_add_enum(property, i,
154 props[i].type,
155 props[i].name);
156 if (ret) {
157 drm_property_destroy(dev, property);
158 return NULL;
159 }
160 }
161
162 return property;
163}
164EXPORT_SYMBOL(drm_property_create_enum);
165
166/**
167 * drm_property_create_bitmask - create a new bitmask property type
168 * @dev: drm device
169 * @flags: flags specifying the property type
170 * @name: name of the property
171 * @props: enumeration lists with property bitflags
172 * @num_props: size of the @props array
173 * @supported_bits: bitmask of all supported enumeration values
174 *
175 * This creates a new bitmask drm property which can then be attached to a drm
Daniel Vetter6a8a66e2016-11-23 20:23:27 +0100176 * object with drm_object_attach_property(). The returned property object must
177 * be freed with drm_property_destroy(), which is done automatically when
178 * calling drm_mode_config_cleanup().
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200179 *
180 * Compared to plain enumeration properties userspace is allowed to set any
181 * or'ed together combination of the predefined property bitflag values
182 *
183 * Returns:
184 * A pointer to the newly created property on success, NULL on failure.
185 */
186struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
187 int flags, const char *name,
188 const struct drm_prop_enum_list *props,
189 int num_props,
190 uint64_t supported_bits)
191{
192 struct drm_property *property;
193 int i, ret, index = 0;
194 int num_values = hweight64(supported_bits);
195
196 flags |= DRM_MODE_PROP_BITMASK;
197
198 property = drm_property_create(dev, flags, name, num_values);
199 if (!property)
200 return NULL;
201 for (i = 0; i < num_props; i++) {
202 if (!(supported_bits & (1ULL << props[i].type)))
203 continue;
204
205 if (WARN_ON(index >= num_values)) {
206 drm_property_destroy(dev, property);
207 return NULL;
208 }
209
210 ret = drm_property_add_enum(property, index++,
211 props[i].type,
212 props[i].name);
213 if (ret) {
214 drm_property_destroy(dev, property);
215 return NULL;
216 }
217 }
218
219 return property;
220}
221EXPORT_SYMBOL(drm_property_create_bitmask);
222
223static struct drm_property *property_create_range(struct drm_device *dev,
224 int flags, const char *name,
225 uint64_t min, uint64_t max)
226{
227 struct drm_property *property;
228
229 property = drm_property_create(dev, flags, name, 2);
230 if (!property)
231 return NULL;
232
233 property->values[0] = min;
234 property->values[1] = max;
235
236 return property;
237}
238
239/**
240 * drm_property_create_range - create a new unsigned ranged property type
241 * @dev: drm device
242 * @flags: flags specifying the property type
243 * @name: name of the property
244 * @min: minimum value of the property
245 * @max: maximum value of the property
246 *
247 * This creates a new generic drm property which can then be attached to a drm
Daniel Vetter6a8a66e2016-11-23 20:23:27 +0100248 * object with drm_object_attach_property(). The returned property object must
249 * be freed with drm_property_destroy(), which is done automatically when
250 * calling drm_mode_config_cleanup().
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200251 *
252 * Userspace is allowed to set any unsigned integer value in the (min, max)
253 * range inclusive.
254 *
255 * Returns:
256 * A pointer to the newly created property on success, NULL on failure.
257 */
258struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
259 const char *name,
260 uint64_t min, uint64_t max)
261{
262 return property_create_range(dev, DRM_MODE_PROP_RANGE | flags,
263 name, min, max);
264}
265EXPORT_SYMBOL(drm_property_create_range);
266
267/**
268 * drm_property_create_signed_range - create a new signed ranged property type
269 * @dev: drm device
270 * @flags: flags specifying the property type
271 * @name: name of the property
272 * @min: minimum value of the property
273 * @max: maximum value of the property
274 *
275 * This creates a new generic drm property which can then be attached to a drm
Daniel Vetter6a8a66e2016-11-23 20:23:27 +0100276 * object with drm_object_attach_property(). The returned property object must
277 * be freed with drm_property_destroy(), which is done automatically when
278 * calling drm_mode_config_cleanup().
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200279 *
280 * Userspace is allowed to set any signed integer value in the (min, max)
281 * range inclusive.
282 *
283 * Returns:
284 * A pointer to the newly created property on success, NULL on failure.
285 */
286struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
287 int flags, const char *name,
288 int64_t min, int64_t max)
289{
290 return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags,
291 name, I642U64(min), I642U64(max));
292}
293EXPORT_SYMBOL(drm_property_create_signed_range);
294
295/**
296 * drm_property_create_object - create a new object property type
297 * @dev: drm device
298 * @flags: flags specifying the property type
299 * @name: name of the property
300 * @type: object type from DRM_MODE_OBJECT_* defines
301 *
302 * This creates a new generic drm property which can then be attached to a drm
Daniel Vetter6a8a66e2016-11-23 20:23:27 +0100303 * object with drm_object_attach_property(). The returned property object must
304 * be freed with drm_property_destroy(), which is done automatically when
305 * calling drm_mode_config_cleanup().
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200306 *
307 * Userspace is only allowed to set this to any property value of the given
308 * @type. Only useful for atomic properties, which is enforced.
309 *
310 * Returns:
311 * A pointer to the newly created property on success, NULL on failure.
312 */
313struct drm_property *drm_property_create_object(struct drm_device *dev,
Daniel Vetterc8458c72016-08-29 10:27:57 +0200314 int flags, const char *name,
315 uint32_t type)
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200316{
317 struct drm_property *property;
318
319 flags |= DRM_MODE_PROP_OBJECT;
320
321 if (WARN_ON(!(flags & DRM_MODE_PROP_ATOMIC)))
322 return NULL;
323
324 property = drm_property_create(dev, flags, name, 1);
325 if (!property)
326 return NULL;
327
328 property->values[0] = type;
329
330 return property;
331}
332EXPORT_SYMBOL(drm_property_create_object);
333
334/**
335 * drm_property_create_bool - create a new boolean property type
336 * @dev: drm device
337 * @flags: flags specifying the property type
338 * @name: name of the property
339 *
340 * This creates a new generic drm property which can then be attached to a drm
Daniel Vetter6a8a66e2016-11-23 20:23:27 +0100341 * object with drm_object_attach_property(). The returned property object must
342 * be freed with drm_property_destroy(), which is done automatically when
343 * calling drm_mode_config_cleanup().
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200344 *
345 * This is implemented as a ranged property with only {0, 1} as valid values.
346 *
347 * Returns:
348 * A pointer to the newly created property on success, NULL on failure.
349 */
350struct drm_property *drm_property_create_bool(struct drm_device *dev, int flags,
Daniel Vetterc8458c72016-08-29 10:27:57 +0200351 const char *name)
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200352{
353 return drm_property_create_range(dev, flags, name, 0, 1);
354}
355EXPORT_SYMBOL(drm_property_create_bool);
356
357/**
358 * drm_property_add_enum - add a possible value to an enumeration property
359 * @property: enumeration property to change
360 * @index: index of the new enumeration
361 * @value: value of the new enumeration
362 * @name: symbolic name of the new enumeration
363 *
364 * This functions adds enumerations to a property.
365 *
366 * It's use is deprecated, drivers should use one of the more specific helpers
367 * to directly create the property with all enumerations already attached.
368 *
369 * Returns:
370 * Zero on success, error code on failure.
371 */
372int drm_property_add_enum(struct drm_property *property, int index,
373 uint64_t value, const char *name)
374{
375 struct drm_property_enum *prop_enum;
376
377 if (!(drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
378 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
379 return -EINVAL;
380
381 /*
382 * Bitmask enum properties have the additional constraint of values
383 * from 0 to 63
384 */
385 if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK) &&
386 (value > 63))
387 return -EINVAL;
388
389 if (!list_empty(&property->enum_list)) {
390 list_for_each_entry(prop_enum, &property->enum_list, head) {
391 if (prop_enum->value == value) {
392 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
393 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
394 return 0;
395 }
396 }
397 }
398
399 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
400 if (!prop_enum)
401 return -ENOMEM;
402
403 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
404 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
405 prop_enum->value = value;
406
407 property->values[index] = value;
408 list_add_tail(&prop_enum->head, &property->enum_list);
409 return 0;
410}
411EXPORT_SYMBOL(drm_property_add_enum);
412
413/**
414 * drm_property_destroy - destroy a drm property
415 * @dev: drm device
416 * @property: property to destry
417 *
418 * This function frees a property including any attached resources like
419 * enumeration values.
420 */
421void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
422{
423 struct drm_property_enum *prop_enum, *pt;
424
425 list_for_each_entry_safe(prop_enum, pt, &property->enum_list, head) {
426 list_del(&prop_enum->head);
427 kfree(prop_enum);
428 }
429
430 if (property->num_values)
431 kfree(property->values);
432 drm_mode_object_unregister(dev, &property->base);
433 list_del(&property->head);
434 kfree(property);
435}
436EXPORT_SYMBOL(drm_property_destroy);
437
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200438int drm_mode_getproperty_ioctl(struct drm_device *dev,
439 void *data, struct drm_file *file_priv)
440{
441 struct drm_mode_get_property *out_resp = data;
442 struct drm_property *property;
443 int enum_count = 0;
444 int value_count = 0;
Daniel Vettereb8eb022017-04-03 10:32:55 +0200445 int i, copied;
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200446 struct drm_property_enum *prop_enum;
447 struct drm_mode_property_enum __user *enum_ptr;
448 uint64_t __user *values_ptr;
449
450 if (!drm_core_check_feature(dev, DRIVER_MODESET))
451 return -EINVAL;
452
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200453 property = drm_property_find(dev, out_resp->prop_id);
Daniel Vettereb8eb022017-04-03 10:32:55 +0200454 if (!property)
455 return -ENOENT;
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200456
457 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
458 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
459 out_resp->flags = property->flags;
460
Daniel Vettereb8eb022017-04-03 10:32:55 +0200461 value_count = property->num_values;
462 values_ptr = u64_to_user_ptr(out_resp->values_ptr);
463
464 for (i = 0; i < value_count; i++) {
465 if (i < out_resp->count_values &&
466 put_user(property->values[i], values_ptr + i)) {
467 return -EFAULT;
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200468 }
469 }
470 out_resp->count_values = value_count;
471
Daniel Vettereb8eb022017-04-03 10:32:55 +0200472 copied = 0;
473 enum_ptr = u64_to_user_ptr(out_resp->enum_blob_ptr);
474
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200475 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
Daniel Vettereb8eb022017-04-03 10:32:55 +0200476 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
477 list_for_each_entry(prop_enum, &property->enum_list, head) {
478 enum_count++;
Daniel Vetter8cb68c82017-04-10 13:54:45 +0200479 if (out_resp->count_enum_blobs < enum_count)
Daniel Vettereb8eb022017-04-03 10:32:55 +0200480 continue;
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200481
Daniel Vettereb8eb022017-04-03 10:32:55 +0200482 if (copy_to_user(&enum_ptr[copied].value,
483 &prop_enum->value, sizeof(uint64_t)))
484 return -EFAULT;
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200485
Daniel Vettereb8eb022017-04-03 10:32:55 +0200486 if (copy_to_user(&enum_ptr[copied].name,
487 &prop_enum->name, DRM_PROP_NAME_LEN))
488 return -EFAULT;
489 copied++;
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200490 }
491 out_resp->count_enum_blobs = enum_count;
492 }
493
494 /*
495 * NOTE: The idea seems to have been to use this to read all the blob
496 * property values. But nothing ever added them to the corresponding
497 * list, userspace always used the special-purpose get_blob ioctl to
498 * read the value for a blob property. It also doesn't make a lot of
499 * sense to return values here when everything else is just metadata for
500 * the property itself.
501 */
502 if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
503 out_resp->count_enum_blobs = 0;
Daniel Vettereb8eb022017-04-03 10:32:55 +0200504
505 return 0;
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200506}
507
508static void drm_property_free_blob(struct kref *kref)
509{
510 struct drm_property_blob *blob =
511 container_of(kref, struct drm_property_blob, base.refcount);
512
513 mutex_lock(&blob->dev->mode_config.blob_lock);
514 list_del(&blob->head_global);
515 mutex_unlock(&blob->dev->mode_config.blob_lock);
516
517 drm_mode_object_unregister(blob->dev, &blob->base);
518
519 kfree(blob);
520}
521
522/**
523 * drm_property_create_blob - Create new blob property
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200524 * @dev: DRM device to create property for
525 * @length: Length to allocate for blob data
526 * @data: If specified, copies data into blob
527 *
Daniel Vetterc8458c72016-08-29 10:27:57 +0200528 * Creates a new blob property for a specified DRM device, optionally
529 * copying data. Note that blob properties are meant to be invariant, hence the
530 * data must be filled out before the blob is used as the value of any property.
531 *
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200532 * Returns:
533 * New blob property with a single reference on success, or an ERR_PTR
534 * value on failure.
535 */
536struct drm_property_blob *
537drm_property_create_blob(struct drm_device *dev, size_t length,
538 const void *data)
539{
540 struct drm_property_blob *blob;
541 int ret;
542
543 if (!length || length > ULONG_MAX - sizeof(struct drm_property_blob))
544 return ERR_PTR(-EINVAL);
545
546 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
547 if (!blob)
548 return ERR_PTR(-ENOMEM);
549
550 /* This must be explicitly initialised, so we can safely call list_del
551 * on it in the removal handler, even if it isn't in a file list. */
552 INIT_LIST_HEAD(&blob->head_file);
553 blob->length = length;
554 blob->dev = dev;
555
556 if (data)
557 memcpy(blob->data, data, length);
558
Thierry Reding2135ea72017-02-28 15:46:37 +0100559 ret = __drm_mode_object_add(dev, &blob->base, DRM_MODE_OBJECT_BLOB,
560 true, drm_property_free_blob);
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200561 if (ret) {
562 kfree(blob);
563 return ERR_PTR(-EINVAL);
564 }
565
566 mutex_lock(&dev->mode_config.blob_lock);
567 list_add_tail(&blob->head_global,
568 &dev->mode_config.property_blob_list);
569 mutex_unlock(&dev->mode_config.blob_lock);
570
571 return blob;
572}
573EXPORT_SYMBOL(drm_property_create_blob);
574
575/**
Thierry Reding6472e502017-02-28 15:46:42 +0100576 * drm_property_blob_put - release a blob property reference
577 * @blob: DRM blob property
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200578 *
Thierry Reding6472e502017-02-28 15:46:42 +0100579 * Releases a reference to a blob property. May free the object.
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200580 */
Thierry Reding6472e502017-02-28 15:46:42 +0100581void drm_property_blob_put(struct drm_property_blob *blob)
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200582{
583 if (!blob)
584 return;
585
Thierry Reding020a2182017-02-28 15:46:38 +0100586 drm_mode_object_put(&blob->base);
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200587}
Thierry Reding6472e502017-02-28 15:46:42 +0100588EXPORT_SYMBOL(drm_property_blob_put);
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200589
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200590void drm_property_destroy_user_blobs(struct drm_device *dev,
591 struct drm_file *file_priv)
592{
593 struct drm_property_blob *blob, *bt;
594
595 /*
596 * When the file gets released that means no one else can access the
597 * blob list any more, so no need to grab dev->blob_lock.
598 */
599 list_for_each_entry_safe(blob, bt, &file_priv->blobs, head_file) {
600 list_del_init(&blob->head_file);
Thierry Reding6472e502017-02-28 15:46:42 +0100601 drm_property_blob_put(blob);
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200602 }
603}
604
605/**
Thierry Reding6472e502017-02-28 15:46:42 +0100606 * drm_property_blob_get - acquire blob property reference
607 * @blob: DRM blob property
Daniel Vetterc8458c72016-08-29 10:27:57 +0200608 *
Thierry Reding6472e502017-02-28 15:46:42 +0100609 * Acquires a reference to an existing blob property. Returns @blob, which
Daniel Vetterc8458c72016-08-29 10:27:57 +0200610 * allows this to be used as a shorthand in assignments.
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200611 */
Thierry Reding6472e502017-02-28 15:46:42 +0100612struct drm_property_blob *drm_property_blob_get(struct drm_property_blob *blob)
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200613{
Thierry Reding020a2182017-02-28 15:46:38 +0100614 drm_mode_object_get(&blob->base);
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200615 return blob;
616}
Thierry Reding6472e502017-02-28 15:46:42 +0100617EXPORT_SYMBOL(drm_property_blob_get);
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200618
619/**
620 * drm_property_lookup_blob - look up a blob property and take a reference
621 * @dev: drm device
622 * @id: id of the blob property
623 *
624 * If successful, this takes an additional reference to the blob property.
625 * callers need to make sure to eventually unreference the returned property
Thierry Reding6472e502017-02-28 15:46:42 +0100626 * again, using drm_property_blob_put().
Daniel Vetterc8458c72016-08-29 10:27:57 +0200627 *
628 * Return:
629 * NULL on failure, pointer to the blob on success.
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200630 */
631struct drm_property_blob *drm_property_lookup_blob(struct drm_device *dev,
632 uint32_t id)
633{
634 struct drm_mode_object *obj;
635 struct drm_property_blob *blob = NULL;
636
637 obj = __drm_mode_object_find(dev, id, DRM_MODE_OBJECT_BLOB);
638 if (obj)
639 blob = obj_to_blob(obj);
640 return blob;
641}
642EXPORT_SYMBOL(drm_property_lookup_blob);
643
644/**
Daniel Vetterc8458c72016-08-29 10:27:57 +0200645 * drm_property_replace_global_blob - replace existing blob property
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200646 * @dev: drm device
647 * @replace: location of blob property pointer to be replaced
648 * @length: length of data for new blob, or 0 for no data
649 * @data: content for new blob, or NULL for no data
650 * @obj_holds_id: optional object for property holding blob ID
651 * @prop_holds_id: optional property holding blob ID
652 * @return 0 on success or error on failure
653 *
Daniel Vetterc8458c72016-08-29 10:27:57 +0200654 * This function will replace a global property in the blob list, optionally
655 * updating a property which holds the ID of that property.
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200656 *
657 * If length is 0 or data is NULL, no new blob will be created, and the holding
658 * property, if specified, will be set to 0.
659 *
660 * Access to the replace pointer is assumed to be protected by the caller, e.g.
661 * by holding the relevant modesetting object lock for its parent.
662 *
663 * For example, a drm_connector has a 'PATH' property, which contains the ID
664 * of a blob property with the value of the MST path information. Calling this
665 * function with replace pointing to the connector's path_blob_ptr, length and
666 * data set for the new path information, obj_holds_id set to the connector's
667 * base object, and prop_holds_id set to the path property name, will perform
668 * a completely atomic update. The access to path_blob_ptr is protected by the
669 * caller holding a lock on the connector.
670 */
671int drm_property_replace_global_blob(struct drm_device *dev,
672 struct drm_property_blob **replace,
673 size_t length,
674 const void *data,
675 struct drm_mode_object *obj_holds_id,
676 struct drm_property *prop_holds_id)
677{
678 struct drm_property_blob *new_blob = NULL;
679 struct drm_property_blob *old_blob = NULL;
680 int ret;
681
682 WARN_ON(replace == NULL);
683
684 old_blob = *replace;
685
686 if (length && data) {
687 new_blob = drm_property_create_blob(dev, length, data);
688 if (IS_ERR(new_blob))
689 return PTR_ERR(new_blob);
690 }
691
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200692 if (obj_holds_id) {
693 ret = drm_object_property_set_value(obj_holds_id,
694 prop_holds_id,
695 new_blob ?
696 new_blob->base.id : 0);
697 if (ret != 0)
698 goto err_created;
699 }
700
Thierry Reding6472e502017-02-28 15:46:42 +0100701 drm_property_blob_put(old_blob);
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200702 *replace = new_blob;
703
704 return 0;
705
706err_created:
Thierry Reding6472e502017-02-28 15:46:42 +0100707 drm_property_blob_put(new_blob);
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200708 return ret;
709}
710EXPORT_SYMBOL(drm_property_replace_global_blob);
711
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200712int drm_mode_getblob_ioctl(struct drm_device *dev,
713 void *data, struct drm_file *file_priv)
714{
715 struct drm_mode_get_blob *out_resp = data;
716 struct drm_property_blob *blob;
717 int ret = 0;
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200718
719 if (!drm_core_check_feature(dev, DRIVER_MODESET))
720 return -EINVAL;
721
722 blob = drm_property_lookup_blob(dev, out_resp->blob_id);
723 if (!blob)
724 return -ENOENT;
725
726 if (out_resp->length == blob->length) {
Chris Wilson75df6242016-11-27 17:09:08 +0000727 if (copy_to_user(u64_to_user_ptr(out_resp->data),
728 blob->data,
729 blob->length)) {
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200730 ret = -EFAULT;
731 goto unref;
732 }
733 }
734 out_resp->length = blob->length;
735unref:
Thierry Reding6472e502017-02-28 15:46:42 +0100736 drm_property_blob_put(blob);
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200737
738 return ret;
739}
740
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200741int drm_mode_createblob_ioctl(struct drm_device *dev,
742 void *data, struct drm_file *file_priv)
743{
744 struct drm_mode_create_blob *out_resp = data;
745 struct drm_property_blob *blob;
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200746 int ret = 0;
747
748 if (!drm_core_check_feature(dev, DRIVER_MODESET))
749 return -EINVAL;
750
751 blob = drm_property_create_blob(dev, out_resp->length, NULL);
752 if (IS_ERR(blob))
753 return PTR_ERR(blob);
754
Chris Wilson75df6242016-11-27 17:09:08 +0000755 if (copy_from_user(blob->data,
756 u64_to_user_ptr(out_resp->data),
757 out_resp->length)) {
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200758 ret = -EFAULT;
759 goto out_blob;
760 }
761
762 /* Dropping the lock between create_blob and our access here is safe
763 * as only the same file_priv can remove the blob; at this point, it is
764 * not associated with any file_priv. */
765 mutex_lock(&dev->mode_config.blob_lock);
766 out_resp->blob_id = blob->base.id;
767 list_add_tail(&blob->head_file, &file_priv->blobs);
768 mutex_unlock(&dev->mode_config.blob_lock);
769
770 return 0;
771
772out_blob:
Thierry Reding6472e502017-02-28 15:46:42 +0100773 drm_property_blob_put(blob);
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200774 return ret;
775}
776
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200777int drm_mode_destroyblob_ioctl(struct drm_device *dev,
778 void *data, struct drm_file *file_priv)
779{
780 struct drm_mode_destroy_blob *out_resp = data;
781 struct drm_property_blob *blob = NULL, *bt;
782 bool found = false;
783 int ret = 0;
784
785 if (!drm_core_check_feature(dev, DRIVER_MODESET))
786 return -EINVAL;
787
788 blob = drm_property_lookup_blob(dev, out_resp->blob_id);
789 if (!blob)
790 return -ENOENT;
791
792 mutex_lock(&dev->mode_config.blob_lock);
793 /* Ensure the property was actually created by this user. */
794 list_for_each_entry(bt, &file_priv->blobs, head_file) {
795 if (bt == blob) {
796 found = true;
797 break;
798 }
799 }
800
801 if (!found) {
802 ret = -EPERM;
803 goto err;
804 }
805
806 /* We must drop head_file here, because we may not be the last
807 * reference on the blob. */
808 list_del_init(&blob->head_file);
809 mutex_unlock(&dev->mode_config.blob_lock);
810
811 /* One reference from lookup, and one from the filp. */
Thierry Reding6472e502017-02-28 15:46:42 +0100812 drm_property_blob_put(blob);
813 drm_property_blob_put(blob);
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200814
815 return 0;
816
817err:
818 mutex_unlock(&dev->mode_config.blob_lock);
Thierry Reding6472e502017-02-28 15:46:42 +0100819 drm_property_blob_put(blob);
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200820
821 return ret;
822}
823
824/* Some properties could refer to dynamic refcnt'd objects, or things that
825 * need special locking to handle lifetime issues (ie. to ensure the prop
826 * value doesn't become invalid part way through the property update due to
827 * race). The value returned by reference via 'obj' should be passed back
828 * to drm_property_change_valid_put() after the property is set (and the
829 * object to which the property is attached has a chance to take it's own
830 * reference).
831 */
832bool drm_property_change_valid_get(struct drm_property *property,
Daniel Vetterc8458c72016-08-29 10:27:57 +0200833 uint64_t value, struct drm_mode_object **ref)
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200834{
835 int i;
836
837 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
838 return false;
839
840 *ref = NULL;
841
842 if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) {
843 if (value < property->values[0] || value > property->values[1])
844 return false;
845 return true;
846 } else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) {
847 int64_t svalue = U642I64(value);
848
849 if (svalue < U642I64(property->values[0]) ||
850 svalue > U642I64(property->values[1]))
851 return false;
852 return true;
853 } else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
854 uint64_t valid_mask = 0;
855
856 for (i = 0; i < property->num_values; i++)
857 valid_mask |= (1ULL << property->values[i]);
858 return !(value & ~valid_mask);
Maarten Lankhorst30c06572016-09-07 11:52:40 +0200859 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
860 struct drm_property_blob *blob;
861
862 if (value == 0)
863 return true;
864
865 blob = drm_property_lookup_blob(property->dev, value);
866 if (blob) {
867 *ref = &blob->base;
868 return true;
869 } else {
870 return false;
871 }
872 } else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200873 /* a zero value for an object property translates to null: */
874 if (value == 0)
875 return true;
876
877 *ref = __drm_mode_object_find(property->dev, value,
878 property->values[0]);
879 return *ref != NULL;
880 }
881
882 for (i = 0; i < property->num_values; i++)
883 if (property->values[i] == value)
884 return true;
885 return false;
886}
887
888void drm_property_change_valid_put(struct drm_property *property,
Maarten Lankhorst30c06572016-09-07 11:52:40 +0200889 struct drm_mode_object *ref)
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200890{
891 if (!ref)
892 return;
893
Maarten Lankhorst30c06572016-09-07 11:52:40 +0200894 if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
Thierry Reding020a2182017-02-28 15:46:38 +0100895 drm_mode_object_put(ref);
Maarten Lankhorst30c06572016-09-07 11:52:40 +0200896 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
Thierry Reding6472e502017-02-28 15:46:42 +0100897 drm_property_blob_put(obj_to_blob(ref));
Daniel Vetter59e71ee2016-08-29 10:27:55 +0200898}