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