drm_property.c 27 KB

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