drm_mode_object.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  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 <linux/uaccess.h>
  24. #include <drm/drm_atomic.h>
  25. #include <drm/drm_drv.h>
  26. #include <drm/drm_device.h>
  27. #include <drm/drm_file.h>
  28. #include <drm/drm_mode_object.h>
  29. #include <drm/drm_plane.h>
  30. #include <drm/drm_print.h>
  31. #include "drm_crtc_internal.h"
  32. /*
  33. * Internal function to assign a slot in the object idr and optionally
  34. * register the object into the idr.
  35. */
  36. int __drm_mode_object_add(struct drm_device *dev, struct drm_mode_object *obj,
  37. uint32_t obj_type, bool register_obj,
  38. void (*obj_free_cb)(struct kref *kref))
  39. {
  40. int ret;
  41. WARN_ON(!dev->driver->load && dev->registered && !obj_free_cb);
  42. mutex_lock(&dev->mode_config.idr_mutex);
  43. ret = idr_alloc(&dev->mode_config.object_idr, register_obj ? obj : NULL,
  44. 1, 0, GFP_KERNEL);
  45. if (ret >= 0) {
  46. /*
  47. * Set up the object linking under the protection of the idr
  48. * lock so that other users can't see inconsistent state.
  49. */
  50. obj->id = ret;
  51. obj->type = obj_type;
  52. if (obj_free_cb) {
  53. obj->free_cb = obj_free_cb;
  54. kref_init(&obj->refcount);
  55. }
  56. }
  57. mutex_unlock(&dev->mode_config.idr_mutex);
  58. return ret < 0 ? ret : 0;
  59. }
  60. /**
  61. * drm_mode_object_add - allocate a new modeset identifier
  62. * @dev: DRM device
  63. * @obj: object pointer, used to generate unique ID
  64. * @obj_type: object type
  65. *
  66. * Create a unique identifier based on @ptr in @dev's identifier space. Used
  67. * for tracking modes, CRTCs and connectors.
  68. *
  69. * Returns:
  70. * Zero on success, error code on failure.
  71. */
  72. int drm_mode_object_add(struct drm_device *dev,
  73. struct drm_mode_object *obj, uint32_t obj_type)
  74. {
  75. return __drm_mode_object_add(dev, obj, obj_type, true, NULL);
  76. }
  77. EXPORT_SYMBOL_FOR_TESTS_ONLY(drm_mode_object_add);
  78. void drm_mode_object_register(struct drm_device *dev,
  79. struct drm_mode_object *obj)
  80. {
  81. mutex_lock(&dev->mode_config.idr_mutex);
  82. idr_replace(&dev->mode_config.object_idr, obj, obj->id);
  83. mutex_unlock(&dev->mode_config.idr_mutex);
  84. }
  85. /**
  86. * drm_mode_object_unregister - free a modeset identifier
  87. * @dev: DRM device
  88. * @object: object to free
  89. *
  90. * Free @id from @dev's unique identifier pool.
  91. * This function can be called multiple times, and guards against
  92. * multiple removals.
  93. * These modeset identifiers are _not_ reference counted. Hence don't use this
  94. * for reference counted modeset objects like framebuffers.
  95. */
  96. void drm_mode_object_unregister(struct drm_device *dev,
  97. struct drm_mode_object *object)
  98. {
  99. WARN_ON(!dev->driver->load && dev->registered && !object->free_cb);
  100. mutex_lock(&dev->mode_config.idr_mutex);
  101. if (object->id) {
  102. idr_remove(&dev->mode_config.object_idr, object->id);
  103. object->id = 0;
  104. }
  105. mutex_unlock(&dev->mode_config.idr_mutex);
  106. }
  107. /**
  108. * drm_mode_object_lease_required - check types which must be leased to be used
  109. * @type: type of object
  110. *
  111. * Returns whether the provided type of drm_mode_object must
  112. * be owned or leased to be used by a process.
  113. */
  114. bool drm_mode_object_lease_required(uint32_t type)
  115. {
  116. switch(type) {
  117. case DRM_MODE_OBJECT_CRTC:
  118. case DRM_MODE_OBJECT_CONNECTOR:
  119. case DRM_MODE_OBJECT_PLANE:
  120. return true;
  121. default:
  122. return false;
  123. }
  124. }
  125. struct drm_mode_object *__drm_mode_object_find(struct drm_device *dev,
  126. struct drm_file *file_priv,
  127. uint32_t id, uint32_t type)
  128. {
  129. struct drm_mode_object *obj = NULL;
  130. mutex_lock(&dev->mode_config.idr_mutex);
  131. obj = idr_find(&dev->mode_config.object_idr, id);
  132. if (obj && type != DRM_MODE_OBJECT_ANY && obj->type != type)
  133. obj = NULL;
  134. if (obj && obj->id != id)
  135. obj = NULL;
  136. if (obj && drm_mode_object_lease_required(obj->type) &&
  137. !_drm_lease_held(file_priv, obj->id)) {
  138. drm_dbg_kms(dev, "[OBJECT:%d] not included in lease", id);
  139. obj = NULL;
  140. }
  141. if (obj && obj->free_cb) {
  142. if (!kref_get_unless_zero(&obj->refcount))
  143. obj = NULL;
  144. }
  145. mutex_unlock(&dev->mode_config.idr_mutex);
  146. return obj;
  147. }
  148. /**
  149. * drm_mode_object_find - look up a drm object with static lifetime
  150. * @dev: drm device
  151. * @file_priv: drm file
  152. * @id: id of the mode object
  153. * @type: type of the mode object
  154. *
  155. * This function is used to look up a modeset object. It will acquire a
  156. * reference for reference counted objects. This reference must be dropped again
  157. * by callind drm_mode_object_put().
  158. */
  159. struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
  160. struct drm_file *file_priv,
  161. uint32_t id, uint32_t type)
  162. {
  163. struct drm_mode_object *obj = NULL;
  164. obj = __drm_mode_object_find(dev, file_priv, id, type);
  165. return obj;
  166. }
  167. EXPORT_SYMBOL(drm_mode_object_find);
  168. /**
  169. * drm_mode_object_put - release a mode object reference
  170. * @obj: DRM mode object
  171. *
  172. * This function decrements the object's refcount if it is a refcounted modeset
  173. * object. It is a no-op on any other object. This is used to drop references
  174. * acquired with drm_mode_object_get().
  175. */
  176. void drm_mode_object_put(struct drm_mode_object *obj)
  177. {
  178. if (obj->free_cb) {
  179. DRM_DEBUG("OBJ ID: %d (%d)\n", obj->id, kref_read(&obj->refcount));
  180. kref_put(&obj->refcount, obj->free_cb);
  181. }
  182. }
  183. EXPORT_SYMBOL(drm_mode_object_put);
  184. /**
  185. * drm_mode_object_get - acquire a mode object reference
  186. * @obj: DRM mode object
  187. *
  188. * This function increments the object's refcount if it is a refcounted modeset
  189. * object. It is a no-op on any other object. References should be dropped again
  190. * by calling drm_mode_object_put().
  191. */
  192. void drm_mode_object_get(struct drm_mode_object *obj)
  193. {
  194. if (obj->free_cb) {
  195. DRM_DEBUG("OBJ ID: %d (%d)\n", obj->id, kref_read(&obj->refcount));
  196. kref_get(&obj->refcount);
  197. }
  198. }
  199. EXPORT_SYMBOL(drm_mode_object_get);
  200. /**
  201. * drm_object_attach_property - attach a property to a modeset object
  202. * @obj: drm modeset object
  203. * @property: property to attach
  204. * @init_val: initial value of the property
  205. *
  206. * This attaches the given property to the modeset object with the given initial
  207. * value. Currently this function cannot fail since the properties are stored in
  208. * a statically sized array.
  209. *
  210. * Note that all properties must be attached before the object itself is
  211. * registered and accessible from userspace.
  212. */
  213. void drm_object_attach_property(struct drm_mode_object *obj,
  214. struct drm_property *property,
  215. uint64_t init_val)
  216. {
  217. int count = obj->properties->count;
  218. struct drm_device *dev = property->dev;
  219. if (obj->type == DRM_MODE_OBJECT_CONNECTOR) {
  220. struct drm_connector *connector = obj_to_connector(obj);
  221. WARN_ON(!dev->driver->load &&
  222. connector->registration_state == DRM_CONNECTOR_REGISTERED);
  223. } else {
  224. WARN_ON(!dev->driver->load && dev->registered);
  225. }
  226. if (count == DRM_OBJECT_MAX_PROPERTY) {
  227. WARN(1, "Failed to attach object property (type: 0x%x). Please "
  228. "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
  229. "you see this message on the same object type.\n",
  230. obj->type);
  231. return;
  232. }
  233. obj->properties->properties[count] = property;
  234. obj->properties->values[count] = init_val;
  235. obj->properties->count++;
  236. }
  237. EXPORT_SYMBOL(drm_object_attach_property);
  238. /**
  239. * drm_object_property_set_value - set the value of a property
  240. * @obj: drm mode object to set property value for
  241. * @property: property to set
  242. * @val: value the property should be set to
  243. *
  244. * This function sets a given property on a given object. This function only
  245. * changes the software state of the property, it does not call into the
  246. * driver's ->set_property callback.
  247. *
  248. * Note that atomic drivers should not have any need to call this, the core will
  249. * ensure consistency of values reported back to userspace through the
  250. * appropriate ->atomic_get_property callback. Only legacy drivers should call
  251. * this function to update the tracked value (after clamping and other
  252. * restrictions have been applied).
  253. *
  254. * Returns:
  255. * Zero on success, error code on failure.
  256. */
  257. int drm_object_property_set_value(struct drm_mode_object *obj,
  258. struct drm_property *property, uint64_t val)
  259. {
  260. int i;
  261. WARN_ON(drm_drv_uses_atomic_modeset(property->dev) &&
  262. !(property->flags & DRM_MODE_PROP_IMMUTABLE));
  263. for (i = 0; i < obj->properties->count; i++) {
  264. if (obj->properties->properties[i] == property) {
  265. obj->properties->values[i] = val;
  266. return 0;
  267. }
  268. }
  269. return -EINVAL;
  270. }
  271. EXPORT_SYMBOL(drm_object_property_set_value);
  272. static int __drm_object_property_get_prop_value(struct drm_mode_object *obj,
  273. struct drm_property *property,
  274. uint64_t *val)
  275. {
  276. int i;
  277. for (i = 0; i < obj->properties->count; i++) {
  278. if (obj->properties->properties[i] == property) {
  279. *val = obj->properties->values[i];
  280. return 0;
  281. }
  282. }
  283. return -EINVAL;
  284. }
  285. static int __drm_object_property_get_value(struct drm_mode_object *obj,
  286. struct drm_property *property,
  287. uint64_t *val)
  288. {
  289. /* read-only properties bypass atomic mechanism and still store
  290. * their value in obj->properties->values[].. mostly to avoid
  291. * having to deal w/ EDID and similar props in atomic paths:
  292. */
  293. if (drm_drv_uses_atomic_modeset(property->dev) &&
  294. !(property->flags & DRM_MODE_PROP_IMMUTABLE))
  295. return drm_atomic_get_property(obj, property, val);
  296. return __drm_object_property_get_prop_value(obj, property, val);
  297. }
  298. /**
  299. * drm_object_property_get_value - retrieve the value of a property
  300. * @obj: drm mode object to get property value from
  301. * @property: property to retrieve
  302. * @val: storage for the property value
  303. *
  304. * This function retrieves the softare state of the given property for the given
  305. * property. Since there is no driver callback to retrieve the current property
  306. * value this might be out of sync with the hardware, depending upon the driver
  307. * and property.
  308. *
  309. * Atomic drivers should never call this function directly, the core will read
  310. * out property values through the various ->atomic_get_property callbacks.
  311. *
  312. * Returns:
  313. * Zero on success, error code on failure.
  314. */
  315. int drm_object_property_get_value(struct drm_mode_object *obj,
  316. struct drm_property *property, uint64_t *val)
  317. {
  318. WARN_ON(drm_drv_uses_atomic_modeset(property->dev));
  319. return __drm_object_property_get_value(obj, property, val);
  320. }
  321. EXPORT_SYMBOL(drm_object_property_get_value);
  322. /**
  323. * drm_object_property_get_default_value - retrieve the default value of a
  324. * property when in atomic mode.
  325. * @obj: drm mode object to get property value from
  326. * @property: property to retrieve
  327. * @val: storage for the property value
  328. *
  329. * This function retrieves the default state of the given property as passed in
  330. * to drm_object_attach_property
  331. *
  332. * Only atomic drivers should call this function directly, as for non-atomic
  333. * drivers it will return the current value.
  334. *
  335. * Returns:
  336. * Zero on success, error code on failure.
  337. */
  338. int drm_object_property_get_default_value(struct drm_mode_object *obj,
  339. struct drm_property *property,
  340. uint64_t *val)
  341. {
  342. WARN_ON(!drm_drv_uses_atomic_modeset(property->dev));
  343. return __drm_object_property_get_prop_value(obj, property, val);
  344. }
  345. EXPORT_SYMBOL(drm_object_property_get_default_value);
  346. /**
  347. * drm_object_immutable_property_get_value - retrieve the value of a property
  348. * @obj: drm mode object to get property value from
  349. * @property: property to retrieve
  350. * @val: storage for the property value
  351. *
  352. * This function retrieves the software state of the given immutable property
  353. * for the given mode object.
  354. *
  355. * This function can be called by both atomic and non-atomic drivers.
  356. *
  357. * Returns:
  358. * Zero on success, error code on failure.
  359. */
  360. int drm_object_immutable_property_get_value(struct drm_mode_object *obj,
  361. struct drm_property *property,
  362. uint64_t *val)
  363. {
  364. if (drm_WARN_ON(property->dev, !(property->flags & DRM_MODE_PROP_IMMUTABLE)))
  365. return -EINVAL;
  366. return __drm_object_property_get_prop_value(obj, property, val);
  367. }
  368. EXPORT_SYMBOL(drm_object_immutable_property_get_value);
  369. /* helper for getconnector and getproperties ioctls */
  370. int drm_mode_object_get_properties(struct drm_mode_object *obj, bool atomic,
  371. bool plane_color_pipeline,
  372. uint32_t __user *prop_ptr,
  373. uint64_t __user *prop_values,
  374. uint32_t *arg_count_props)
  375. {
  376. int i, ret, count;
  377. for (i = 0, count = 0; i < obj->properties->count; i++) {
  378. struct drm_property *prop = obj->properties->properties[i];
  379. uint64_t val;
  380. if ((prop->flags & DRM_MODE_PROP_ATOMIC) && !atomic)
  381. continue;
  382. if (plane_color_pipeline && obj->type == DRM_MODE_OBJECT_PLANE) {
  383. struct drm_plane *plane = obj_to_plane(obj);
  384. if (prop == plane->color_encoding_property ||
  385. prop == plane->color_range_property)
  386. continue;
  387. }
  388. if (!plane_color_pipeline && obj->type == DRM_MODE_OBJECT_PLANE) {
  389. struct drm_plane *plane = obj_to_plane(obj);
  390. if (prop == plane->color_pipeline_property)
  391. continue;
  392. }
  393. if (*arg_count_props > count) {
  394. ret = __drm_object_property_get_value(obj, prop, &val);
  395. if (ret)
  396. return ret;
  397. if (put_user(prop->base.id, prop_ptr + count))
  398. return -EFAULT;
  399. if (put_user(val, prop_values + count))
  400. return -EFAULT;
  401. }
  402. count++;
  403. }
  404. *arg_count_props = count;
  405. return 0;
  406. }
  407. /**
  408. * drm_mode_obj_get_properties_ioctl - get the current value of a object's property
  409. * @dev: DRM device
  410. * @data: ioctl data
  411. * @file_priv: DRM file info
  412. *
  413. * This function retrieves the current value for an object's property. Compared
  414. * to the connector specific ioctl this one is extended to also work on crtc and
  415. * plane objects.
  416. *
  417. * Called by the user via ioctl.
  418. *
  419. * Returns:
  420. * Zero on success, negative errno on failure.
  421. */
  422. int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
  423. struct drm_file *file_priv)
  424. {
  425. struct drm_mode_obj_get_properties *arg = data;
  426. struct drm_mode_object *obj;
  427. struct drm_modeset_acquire_ctx ctx;
  428. int ret = 0;
  429. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  430. return -EOPNOTSUPP;
  431. DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx, 0, ret);
  432. obj = drm_mode_object_find(dev, file_priv, arg->obj_id, arg->obj_type);
  433. if (!obj) {
  434. ret = -ENOENT;
  435. goto out;
  436. }
  437. if (!obj->properties) {
  438. ret = -EINVAL;
  439. goto out_unref;
  440. }
  441. ret = drm_mode_object_get_properties(obj, file_priv->atomic,
  442. file_priv->plane_color_pipeline,
  443. (uint32_t __user *)(unsigned long)(arg->props_ptr),
  444. (uint64_t __user *)(unsigned long)(arg->prop_values_ptr),
  445. &arg->count_props);
  446. out_unref:
  447. drm_mode_object_put(obj);
  448. out:
  449. DRM_MODESET_LOCK_ALL_END(dev, ctx, ret);
  450. return ret;
  451. }
  452. struct drm_property *drm_mode_obj_find_prop_id(struct drm_mode_object *obj,
  453. uint32_t prop_id)
  454. {
  455. int i;
  456. for (i = 0; i < obj->properties->count; i++)
  457. if (obj->properties->properties[i]->base.id == prop_id)
  458. return obj->properties->properties[i];
  459. return NULL;
  460. }
  461. EXPORT_SYMBOL_FOR_TESTS_ONLY(drm_mode_obj_find_prop_id);
  462. static int set_property_legacy(struct drm_mode_object *obj,
  463. struct drm_property *prop,
  464. uint64_t prop_value)
  465. {
  466. struct drm_device *dev = prop->dev;
  467. struct drm_mode_object *ref;
  468. struct drm_modeset_acquire_ctx ctx;
  469. int ret = -EINVAL;
  470. if (!drm_property_change_valid_get(prop, prop_value, &ref))
  471. return -EINVAL;
  472. DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx, 0, ret);
  473. switch (obj->type) {
  474. case DRM_MODE_OBJECT_CONNECTOR:
  475. ret = drm_connector_set_obj_prop(obj, prop, prop_value);
  476. break;
  477. case DRM_MODE_OBJECT_CRTC:
  478. ret = drm_mode_crtc_set_obj_prop(obj, prop, prop_value);
  479. break;
  480. case DRM_MODE_OBJECT_PLANE:
  481. ret = drm_mode_plane_set_obj_prop(obj_to_plane(obj),
  482. prop, prop_value);
  483. break;
  484. }
  485. drm_property_change_valid_put(prop, ref);
  486. DRM_MODESET_LOCK_ALL_END(dev, ctx, ret);
  487. return ret;
  488. }
  489. static int set_property_atomic(struct drm_mode_object *obj,
  490. struct drm_file *file_priv,
  491. struct drm_property *prop,
  492. uint64_t prop_value)
  493. {
  494. struct drm_device *dev = prop->dev;
  495. struct drm_atomic_state *state;
  496. struct drm_modeset_acquire_ctx ctx;
  497. int ret;
  498. state = drm_atomic_state_alloc(dev);
  499. if (!state)
  500. return -ENOMEM;
  501. drm_modeset_acquire_init(&ctx, 0);
  502. state->acquire_ctx = &ctx;
  503. retry:
  504. if (prop == state->dev->mode_config.dpms_property) {
  505. if (obj->type != DRM_MODE_OBJECT_CONNECTOR) {
  506. ret = -EINVAL;
  507. goto out;
  508. }
  509. ret = drm_atomic_connector_commit_dpms(state,
  510. obj_to_connector(obj),
  511. prop_value);
  512. } else {
  513. ret = drm_atomic_set_property(state, file_priv, obj, prop, prop_value, false);
  514. if (ret)
  515. goto out;
  516. ret = drm_atomic_commit(state);
  517. }
  518. out:
  519. if (ret == -EDEADLK) {
  520. drm_atomic_state_clear(state);
  521. drm_modeset_backoff(&ctx);
  522. goto retry;
  523. }
  524. drm_atomic_state_put(state);
  525. drm_modeset_drop_locks(&ctx);
  526. drm_modeset_acquire_fini(&ctx);
  527. return ret;
  528. }
  529. int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
  530. struct drm_file *file_priv)
  531. {
  532. struct drm_mode_obj_set_property *arg = data;
  533. struct drm_mode_object *arg_obj;
  534. struct drm_property *property;
  535. int ret = -EINVAL;
  536. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  537. return -EOPNOTSUPP;
  538. arg_obj = drm_mode_object_find(dev, file_priv, arg->obj_id, arg->obj_type);
  539. if (!arg_obj)
  540. return -ENOENT;
  541. if (!arg_obj->properties)
  542. goto out_unref;
  543. property = drm_mode_obj_find_prop_id(arg_obj, arg->prop_id);
  544. if (!property)
  545. goto out_unref;
  546. if (drm_drv_uses_atomic_modeset(property->dev))
  547. ret = set_property_atomic(arg_obj, file_priv, property, arg->value);
  548. else
  549. ret = set_property_legacy(arg_obj, property, arg->value);
  550. out_unref:
  551. drm_mode_object_put(arg_obj);
  552. return ret;
  553. }