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