drm_mode_object.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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_mode_object.h>
  25. #include <drm/drm_atomic.h>
  26. #include "drm_crtc_internal.h"
  27. /*
  28. * Internal function to assign a slot in the object idr and optionally
  29. * register the object into the idr.
  30. */
  31. int __drm_mode_object_add(struct drm_device *dev, struct drm_mode_object *obj,
  32. uint32_t obj_type, bool register_obj,
  33. void (*obj_free_cb)(struct kref *kref))
  34. {
  35. int ret;
  36. mutex_lock(&dev->mode_config.idr_mutex);
  37. ret = idr_alloc(&dev->mode_config.crtc_idr, register_obj ? obj : NULL, 1, 0, GFP_KERNEL);
  38. if (ret >= 0) {
  39. /*
  40. * Set up the object linking under the protection of the idr
  41. * lock so that other users can't see inconsistent state.
  42. */
  43. obj->id = ret;
  44. obj->type = obj_type;
  45. if (obj_free_cb) {
  46. obj->free_cb = obj_free_cb;
  47. kref_init(&obj->refcount);
  48. }
  49. }
  50. mutex_unlock(&dev->mode_config.idr_mutex);
  51. return ret < 0 ? ret : 0;
  52. }
  53. /**
  54. * drm_mode_object_add - allocate a new modeset identifier
  55. * @dev: DRM device
  56. * @obj: object pointer, used to generate unique ID
  57. * @obj_type: object type
  58. *
  59. * Create a unique identifier based on @ptr in @dev's identifier space. Used
  60. * for tracking modes, CRTCs and connectors.
  61. *
  62. * Returns:
  63. * Zero on success, error code on failure.
  64. */
  65. int drm_mode_object_add(struct drm_device *dev,
  66. struct drm_mode_object *obj, uint32_t obj_type)
  67. {
  68. return __drm_mode_object_add(dev, obj, obj_type, true, NULL);
  69. }
  70. void drm_mode_object_register(struct drm_device *dev,
  71. struct drm_mode_object *obj)
  72. {
  73. mutex_lock(&dev->mode_config.idr_mutex);
  74. idr_replace(&dev->mode_config.crtc_idr, obj, obj->id);
  75. mutex_unlock(&dev->mode_config.idr_mutex);
  76. }
  77. /**
  78. * drm_mode_object_unregister - free a modeset identifer
  79. * @dev: DRM device
  80. * @object: object to free
  81. *
  82. * Free @id from @dev's unique identifier pool.
  83. * This function can be called multiple times, and guards against
  84. * multiple removals.
  85. * These modeset identifiers are _not_ reference counted. Hence don't use this
  86. * for reference counted modeset objects like framebuffers.
  87. */
  88. void drm_mode_object_unregister(struct drm_device *dev,
  89. struct drm_mode_object *object)
  90. {
  91. mutex_lock(&dev->mode_config.idr_mutex);
  92. if (object->id) {
  93. idr_remove(&dev->mode_config.crtc_idr, object->id);
  94. object->id = 0;
  95. }
  96. mutex_unlock(&dev->mode_config.idr_mutex);
  97. }
  98. struct drm_mode_object *__drm_mode_object_find(struct drm_device *dev,
  99. uint32_t id, uint32_t type)
  100. {
  101. struct drm_mode_object *obj = NULL;
  102. mutex_lock(&dev->mode_config.idr_mutex);
  103. obj = idr_find(&dev->mode_config.crtc_idr, id);
  104. if (obj && type != DRM_MODE_OBJECT_ANY && obj->type != type)
  105. obj = NULL;
  106. if (obj && obj->id != id)
  107. obj = NULL;
  108. if (obj && obj->free_cb) {
  109. if (!kref_get_unless_zero(&obj->refcount))
  110. obj = NULL;
  111. }
  112. mutex_unlock(&dev->mode_config.idr_mutex);
  113. return obj;
  114. }
  115. /**
  116. * drm_mode_object_find - look up a drm object with static lifetime
  117. * @dev: drm device
  118. * @id: id of the mode object
  119. * @type: type of the mode object
  120. *
  121. * This function is used to look up a modeset object. It will acquire a
  122. * reference for reference counted objects. This reference must be dropped again
  123. * by callind drm_mode_object_put().
  124. */
  125. struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
  126. uint32_t id, uint32_t type)
  127. {
  128. struct drm_mode_object *obj = NULL;
  129. obj = __drm_mode_object_find(dev, id, type);
  130. return obj;
  131. }
  132. EXPORT_SYMBOL(drm_mode_object_find);
  133. /**
  134. * drm_mode_object_put - release a mode object reference
  135. * @obj: DRM mode object
  136. *
  137. * This function decrements the object's refcount if it is a refcounted modeset
  138. * object. It is a no-op on any other object. This is used to drop references
  139. * acquired with drm_mode_object_get().
  140. */
  141. void drm_mode_object_put(struct drm_mode_object *obj)
  142. {
  143. if (obj->free_cb) {
  144. DRM_DEBUG("OBJ ID: %d (%d)\n", obj->id, kref_read(&obj->refcount));
  145. kref_put(&obj->refcount, obj->free_cb);
  146. }
  147. }
  148. EXPORT_SYMBOL(drm_mode_object_put);
  149. /**
  150. * drm_mode_object_get - acquire a mode object reference
  151. * @obj: DRM mode object
  152. *
  153. * This function increments the object's refcount if it is a refcounted modeset
  154. * object. It is a no-op on any other object. References should be dropped again
  155. * by calling drm_mode_object_put().
  156. */
  157. void drm_mode_object_get(struct drm_mode_object *obj)
  158. {
  159. if (obj->free_cb) {
  160. DRM_DEBUG("OBJ ID: %d (%d)\n", obj->id, kref_read(&obj->refcount));
  161. kref_get(&obj->refcount);
  162. }
  163. }
  164. EXPORT_SYMBOL(drm_mode_object_get);
  165. /**
  166. * drm_object_attach_property - attach a property to a modeset object
  167. * @obj: drm modeset object
  168. * @property: property to attach
  169. * @init_val: initial value of the property
  170. *
  171. * This attaches the given property to the modeset object with the given initial
  172. * value. Currently this function cannot fail since the properties are stored in
  173. * a statically sized array.
  174. */
  175. void drm_object_attach_property(struct drm_mode_object *obj,
  176. struct drm_property *property,
  177. uint64_t init_val)
  178. {
  179. int count = obj->properties->count;
  180. if (count == DRM_OBJECT_MAX_PROPERTY) {
  181. WARN(1, "Failed to attach object property (type: 0x%x). Please "
  182. "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
  183. "you see this message on the same object type.\n",
  184. obj->type);
  185. return;
  186. }
  187. obj->properties->properties[count] = property;
  188. obj->properties->values[count] = init_val;
  189. obj->properties->count++;
  190. }
  191. EXPORT_SYMBOL(drm_object_attach_property);
  192. /**
  193. * drm_object_property_set_value - set the value of a property
  194. * @obj: drm mode object to set property value for
  195. * @property: property to set
  196. * @val: value the property should be set to
  197. *
  198. * This function sets a given property on a given object. This function only
  199. * changes the software state of the property, it does not call into the
  200. * driver's ->set_property callback.
  201. *
  202. * Note that atomic drivers should not have any need to call this, the core will
  203. * ensure consistency of values reported back to userspace through the
  204. * appropriate ->atomic_get_property callback. Only legacy drivers should call
  205. * this function to update the tracked value (after clamping and other
  206. * restrictions have been applied).
  207. *
  208. * Returns:
  209. * Zero on success, error code on failure.
  210. */
  211. int drm_object_property_set_value(struct drm_mode_object *obj,
  212. struct drm_property *property, uint64_t val)
  213. {
  214. int i;
  215. for (i = 0; i < obj->properties->count; i++) {
  216. if (obj->properties->properties[i] == property) {
  217. obj->properties->values[i] = val;
  218. return 0;
  219. }
  220. }
  221. return -EINVAL;
  222. }
  223. EXPORT_SYMBOL(drm_object_property_set_value);
  224. /**
  225. * drm_object_property_get_value - retrieve the value of a property
  226. * @obj: drm mode object to get property value from
  227. * @property: property to retrieve
  228. * @val: storage for the property value
  229. *
  230. * This function retrieves the softare state of the given property for the given
  231. * property. Since there is no driver callback to retrieve the current property
  232. * value this might be out of sync with the hardware, depending upon the driver
  233. * and property.
  234. *
  235. * Atomic drivers should never call this function directly, the core will read
  236. * out property values through the various ->atomic_get_property callbacks.
  237. *
  238. * Returns:
  239. * Zero on success, error code on failure.
  240. */
  241. int drm_object_property_get_value(struct drm_mode_object *obj,
  242. struct drm_property *property, uint64_t *val)
  243. {
  244. int i;
  245. /* read-only properties bypass atomic mechanism and still store
  246. * their value in obj->properties->values[].. mostly to avoid
  247. * having to deal w/ EDID and similar props in atomic paths:
  248. */
  249. if (drm_drv_uses_atomic_modeset(property->dev) &&
  250. !(property->flags & DRM_MODE_PROP_IMMUTABLE))
  251. return drm_atomic_get_property(obj, property, val);
  252. for (i = 0; i < obj->properties->count; i++) {
  253. if (obj->properties->properties[i] == property) {
  254. *val = obj->properties->values[i];
  255. return 0;
  256. }
  257. }
  258. return -EINVAL;
  259. }
  260. EXPORT_SYMBOL(drm_object_property_get_value);
  261. /* helper for getconnector and getproperties ioctls */
  262. int drm_mode_object_get_properties(struct drm_mode_object *obj, bool atomic,
  263. uint32_t __user *prop_ptr,
  264. uint64_t __user *prop_values,
  265. uint32_t *arg_count_props)
  266. {
  267. int i, ret, count;
  268. for (i = 0, count = 0; i < obj->properties->count; i++) {
  269. struct drm_property *prop = obj->properties->properties[i];
  270. uint64_t val;
  271. if ((prop->flags & DRM_MODE_PROP_ATOMIC) && !atomic)
  272. continue;
  273. if (*arg_count_props > count) {
  274. ret = drm_object_property_get_value(obj, prop, &val);
  275. if (ret)
  276. return ret;
  277. if (put_user(prop->base.id, prop_ptr + count))
  278. return -EFAULT;
  279. if (put_user(val, prop_values + count))
  280. return -EFAULT;
  281. }
  282. count++;
  283. }
  284. *arg_count_props = count;
  285. return 0;
  286. }
  287. /**
  288. * drm_mode_obj_get_properties_ioctl - get the current value of a object's property
  289. * @dev: DRM device
  290. * @data: ioctl data
  291. * @file_priv: DRM file info
  292. *
  293. * This function retrieves the current value for an object's property. Compared
  294. * to the connector specific ioctl this one is extended to also work on crtc and
  295. * plane objects.
  296. *
  297. * Called by the user via ioctl.
  298. *
  299. * Returns:
  300. * Zero on success, negative errno on failure.
  301. */
  302. int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
  303. struct drm_file *file_priv)
  304. {
  305. struct drm_mode_obj_get_properties *arg = data;
  306. struct drm_mode_object *obj;
  307. int ret = 0;
  308. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  309. return -EINVAL;
  310. drm_modeset_lock_all(dev);
  311. obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
  312. if (!obj) {
  313. ret = -ENOENT;
  314. goto out;
  315. }
  316. if (!obj->properties) {
  317. ret = -EINVAL;
  318. goto out_unref;
  319. }
  320. ret = drm_mode_object_get_properties(obj, file_priv->atomic,
  321. (uint32_t __user *)(unsigned long)(arg->props_ptr),
  322. (uint64_t __user *)(unsigned long)(arg->prop_values_ptr),
  323. &arg->count_props);
  324. out_unref:
  325. drm_mode_object_put(obj);
  326. out:
  327. drm_modeset_unlock_all(dev);
  328. return ret;
  329. }
  330. struct drm_property *drm_mode_obj_find_prop_id(struct drm_mode_object *obj,
  331. uint32_t prop_id)
  332. {
  333. int i;
  334. for (i = 0; i < obj->properties->count; i++)
  335. if (obj->properties->properties[i]->base.id == prop_id)
  336. return obj->properties->properties[i];
  337. return NULL;
  338. }
  339. int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
  340. struct drm_file *file_priv)
  341. {
  342. struct drm_mode_obj_set_property *arg = data;
  343. struct drm_mode_object *arg_obj;
  344. struct drm_property *property;
  345. int ret = -EINVAL;
  346. struct drm_mode_object *ref;
  347. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  348. return -EINVAL;
  349. drm_modeset_lock_all(dev);
  350. arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
  351. if (!arg_obj) {
  352. ret = -ENOENT;
  353. goto out;
  354. }
  355. if (!arg_obj->properties)
  356. goto out_unref;
  357. property = drm_mode_obj_find_prop_id(arg_obj, arg->prop_id);
  358. if (!property)
  359. goto out_unref;
  360. if (!drm_property_change_valid_get(property, arg->value, &ref))
  361. goto out_unref;
  362. switch (arg_obj->type) {
  363. case DRM_MODE_OBJECT_CONNECTOR:
  364. ret = drm_mode_connector_set_obj_prop(arg_obj, property,
  365. arg->value);
  366. break;
  367. case DRM_MODE_OBJECT_CRTC:
  368. ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
  369. break;
  370. case DRM_MODE_OBJECT_PLANE:
  371. ret = drm_mode_plane_set_obj_prop(obj_to_plane(arg_obj),
  372. property, arg->value);
  373. break;
  374. }
  375. drm_property_change_valid_put(property, ref);
  376. out_unref:
  377. drm_mode_object_put(arg_obj);
  378. out:
  379. drm_modeset_unlock_all(dev);
  380. return ret;
  381. }