drm_mode_object.c 13 KB

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