drm_mode_object.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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. struct drm_file *file_priv,
  100. uint32_t id, uint32_t type)
  101. {
  102. struct drm_mode_object *obj = NULL;
  103. mutex_lock(&dev->mode_config.idr_mutex);
  104. obj = idr_find(&dev->mode_config.crtc_idr, id);
  105. if (obj && type != DRM_MODE_OBJECT_ANY && obj->type != type)
  106. obj = NULL;
  107. if (obj && obj->id != id)
  108. obj = NULL;
  109. if (obj && obj->free_cb) {
  110. if (!kref_get_unless_zero(&obj->refcount))
  111. obj = NULL;
  112. }
  113. mutex_unlock(&dev->mode_config.idr_mutex);
  114. return obj;
  115. }
  116. /**
  117. * drm_mode_object_find - look up a drm object with static lifetime
  118. * @file_priv: drm file
  119. * @id: id of the mode object
  120. * @type: type of the mode object
  121. *
  122. * This function is used to look up a modeset object. It will acquire a
  123. * reference for reference counted objects. This reference must be dropped again
  124. * by callind drm_mode_object_put().
  125. */
  126. struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
  127. struct drm_file *file_priv,
  128. uint32_t id, uint32_t type)
  129. {
  130. struct drm_mode_object *obj = NULL;
  131. obj = __drm_mode_object_find(dev, file_priv, id, type);
  132. return obj;
  133. }
  134. EXPORT_SYMBOL(drm_mode_object_find);
  135. /**
  136. * drm_mode_object_put - release a mode object reference
  137. * @obj: DRM mode object
  138. *
  139. * This function decrements the object's refcount if it is a refcounted modeset
  140. * object. It is a no-op on any other object. This is used to drop references
  141. * acquired with drm_mode_object_get().
  142. */
  143. void drm_mode_object_put(struct drm_mode_object *obj)
  144. {
  145. if (obj->free_cb) {
  146. DRM_DEBUG("OBJ ID: %d (%d)\n", obj->id, kref_read(&obj->refcount));
  147. kref_put(&obj->refcount, obj->free_cb);
  148. }
  149. }
  150. EXPORT_SYMBOL(drm_mode_object_put);
  151. /**
  152. * drm_mode_object_get - acquire a mode object reference
  153. * @obj: DRM mode object
  154. *
  155. * This function increments the object's refcount if it is a refcounted modeset
  156. * object. It is a no-op on any other object. References should be dropped again
  157. * by calling drm_mode_object_put().
  158. */
  159. void drm_mode_object_get(struct drm_mode_object *obj)
  160. {
  161. if (obj->free_cb) {
  162. DRM_DEBUG("OBJ ID: %d (%d)\n", obj->id, kref_read(&obj->refcount));
  163. kref_get(&obj->refcount);
  164. }
  165. }
  166. EXPORT_SYMBOL(drm_mode_object_get);
  167. /**
  168. * drm_object_attach_property - attach a property to a modeset object
  169. * @obj: drm modeset object
  170. * @property: property to attach
  171. * @init_val: initial value of the property
  172. *
  173. * This attaches the given property to the modeset object with the given initial
  174. * value. Currently this function cannot fail since the properties are stored in
  175. * a statically sized array.
  176. */
  177. void drm_object_attach_property(struct drm_mode_object *obj,
  178. struct drm_property *property,
  179. uint64_t init_val)
  180. {
  181. int count = obj->properties->count;
  182. if (count == DRM_OBJECT_MAX_PROPERTY) {
  183. WARN(1, "Failed to attach object property (type: 0x%x). Please "
  184. "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
  185. "you see this message on the same object type.\n",
  186. obj->type);
  187. return;
  188. }
  189. obj->properties->properties[count] = property;
  190. obj->properties->values[count] = init_val;
  191. obj->properties->count++;
  192. }
  193. EXPORT_SYMBOL(drm_object_attach_property);
  194. /**
  195. * drm_object_property_set_value - set the value of a property
  196. * @obj: drm mode object to set property value for
  197. * @property: property to set
  198. * @val: value the property should be set to
  199. *
  200. * This function sets a given property on a given object. This function only
  201. * changes the software state of the property, it does not call into the
  202. * driver's ->set_property callback.
  203. *
  204. * Note that atomic drivers should not have any need to call this, the core will
  205. * ensure consistency of values reported back to userspace through the
  206. * appropriate ->atomic_get_property callback. Only legacy drivers should call
  207. * this function to update the tracked value (after clamping and other
  208. * restrictions have been applied).
  209. *
  210. * Returns:
  211. * Zero on success, error code on failure.
  212. */
  213. int drm_object_property_set_value(struct drm_mode_object *obj,
  214. struct drm_property *property, uint64_t val)
  215. {
  216. int i;
  217. WARN_ON(drm_drv_uses_atomic_modeset(property->dev) &&
  218. !(property->flags & DRM_MODE_PROP_IMMUTABLE));
  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. static int __drm_object_property_get_value(struct drm_mode_object *obj,
  229. struct drm_property *property,
  230. uint64_t *val)
  231. {
  232. int i;
  233. /* read-only properties bypass atomic mechanism and still store
  234. * their value in obj->properties->values[].. mostly to avoid
  235. * having to deal w/ EDID and similar props in atomic paths:
  236. */
  237. if (drm_drv_uses_atomic_modeset(property->dev) &&
  238. !(property->flags & DRM_MODE_PROP_IMMUTABLE))
  239. return drm_atomic_get_property(obj, property, val);
  240. for (i = 0; i < obj->properties->count; i++) {
  241. if (obj->properties->properties[i] == property) {
  242. *val = obj->properties->values[i];
  243. return 0;
  244. }
  245. }
  246. return -EINVAL;
  247. }
  248. /**
  249. * drm_object_property_get_value - retrieve the value of a property
  250. * @obj: drm mode object to get property value from
  251. * @property: property to retrieve
  252. * @val: storage for the property value
  253. *
  254. * This function retrieves the softare state of the given property for the given
  255. * property. Since there is no driver callback to retrieve the current property
  256. * value this might be out of sync with the hardware, depending upon the driver
  257. * and property.
  258. *
  259. * Atomic drivers should never call this function directly, the core will read
  260. * out property values through the various ->atomic_get_property callbacks.
  261. *
  262. * Returns:
  263. * Zero on success, error code on failure.
  264. */
  265. int drm_object_property_get_value(struct drm_mode_object *obj,
  266. struct drm_property *property, uint64_t *val)
  267. {
  268. WARN_ON(drm_drv_uses_atomic_modeset(property->dev));
  269. return __drm_object_property_get_value(obj, property, val);
  270. }
  271. EXPORT_SYMBOL(drm_object_property_get_value);
  272. /* helper for getconnector and getproperties ioctls */
  273. int drm_mode_object_get_properties(struct drm_mode_object *obj, bool atomic,
  274. uint32_t __user *prop_ptr,
  275. uint64_t __user *prop_values,
  276. uint32_t *arg_count_props)
  277. {
  278. int i, ret, count;
  279. for (i = 0, count = 0; i < obj->properties->count; i++) {
  280. struct drm_property *prop = obj->properties->properties[i];
  281. uint64_t val;
  282. if ((prop->flags & DRM_MODE_PROP_ATOMIC) && !atomic)
  283. continue;
  284. if (*arg_count_props > count) {
  285. ret = __drm_object_property_get_value(obj, prop, &val);
  286. if (ret)
  287. return ret;
  288. if (put_user(prop->base.id, prop_ptr + count))
  289. return -EFAULT;
  290. if (put_user(val, prop_values + count))
  291. return -EFAULT;
  292. }
  293. count++;
  294. }
  295. *arg_count_props = count;
  296. return 0;
  297. }
  298. /**
  299. * drm_mode_obj_get_properties_ioctl - get the current value of a object's property
  300. * @dev: DRM device
  301. * @data: ioctl data
  302. * @file_priv: DRM file info
  303. *
  304. * This function retrieves the current value for an object's property. Compared
  305. * to the connector specific ioctl this one is extended to also work on crtc and
  306. * plane objects.
  307. *
  308. * Called by the user via ioctl.
  309. *
  310. * Returns:
  311. * Zero on success, negative errno on failure.
  312. */
  313. int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
  314. struct drm_file *file_priv)
  315. {
  316. struct drm_mode_obj_get_properties *arg = data;
  317. struct drm_mode_object *obj;
  318. int ret = 0;
  319. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  320. return -EINVAL;
  321. drm_modeset_lock_all(dev);
  322. obj = drm_mode_object_find(dev, file_priv, arg->obj_id, arg->obj_type);
  323. if (!obj) {
  324. ret = -ENOENT;
  325. goto out;
  326. }
  327. if (!obj->properties) {
  328. ret = -EINVAL;
  329. goto out_unref;
  330. }
  331. ret = drm_mode_object_get_properties(obj, file_priv->atomic,
  332. (uint32_t __user *)(unsigned long)(arg->props_ptr),
  333. (uint64_t __user *)(unsigned long)(arg->prop_values_ptr),
  334. &arg->count_props);
  335. out_unref:
  336. drm_mode_object_put(obj);
  337. out:
  338. drm_modeset_unlock_all(dev);
  339. return ret;
  340. }
  341. struct drm_property *drm_mode_obj_find_prop_id(struct drm_mode_object *obj,
  342. uint32_t prop_id)
  343. {
  344. int i;
  345. for (i = 0; i < obj->properties->count; i++)
  346. if (obj->properties->properties[i]->base.id == prop_id)
  347. return obj->properties->properties[i];
  348. return NULL;
  349. }
  350. static int set_property_legacy(struct drm_mode_object *obj,
  351. struct drm_property *prop,
  352. uint64_t prop_value)
  353. {
  354. struct drm_device *dev = prop->dev;
  355. struct drm_mode_object *ref;
  356. int ret = -EINVAL;
  357. if (!drm_property_change_valid_get(prop, prop_value, &ref))
  358. return -EINVAL;
  359. drm_modeset_lock_all(dev);
  360. switch (obj->type) {
  361. case DRM_MODE_OBJECT_CONNECTOR:
  362. ret = drm_mode_connector_set_obj_prop(obj, prop,
  363. prop_value);
  364. break;
  365. case DRM_MODE_OBJECT_CRTC:
  366. ret = drm_mode_crtc_set_obj_prop(obj, prop, prop_value);
  367. break;
  368. case DRM_MODE_OBJECT_PLANE:
  369. ret = drm_mode_plane_set_obj_prop(obj_to_plane(obj),
  370. prop, prop_value);
  371. break;
  372. }
  373. drm_property_change_valid_put(prop, ref);
  374. drm_modeset_unlock_all(dev);
  375. return ret;
  376. }
  377. static int set_property_atomic(struct drm_mode_object *obj,
  378. struct drm_property *prop,
  379. uint64_t prop_value)
  380. {
  381. struct drm_device *dev = prop->dev;
  382. struct drm_atomic_state *state;
  383. struct drm_modeset_acquire_ctx ctx;
  384. int ret;
  385. drm_modeset_acquire_init(&ctx, 0);
  386. state = drm_atomic_state_alloc(dev);
  387. if (!state)
  388. return -ENOMEM;
  389. state->acquire_ctx = &ctx;
  390. retry:
  391. if (prop == state->dev->mode_config.dpms_property) {
  392. if (obj->type != DRM_MODE_OBJECT_CONNECTOR) {
  393. ret = -EINVAL;
  394. goto out;
  395. }
  396. ret = drm_atomic_connector_commit_dpms(state,
  397. obj_to_connector(obj),
  398. prop_value);
  399. } else {
  400. ret = drm_atomic_set_property(state, obj, prop, prop_value);
  401. if (ret)
  402. goto out;
  403. ret = drm_atomic_commit(state);
  404. }
  405. out:
  406. if (ret == -EDEADLK) {
  407. drm_atomic_state_clear(state);
  408. drm_modeset_backoff(&ctx);
  409. goto retry;
  410. }
  411. drm_atomic_state_put(state);
  412. drm_modeset_drop_locks(&ctx);
  413. drm_modeset_acquire_fini(&ctx);
  414. return ret;
  415. }
  416. int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
  417. struct drm_file *file_priv)
  418. {
  419. struct drm_mode_obj_set_property *arg = data;
  420. struct drm_mode_object *arg_obj;
  421. struct drm_property *property;
  422. int ret = -EINVAL;
  423. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  424. return -EINVAL;
  425. arg_obj = drm_mode_object_find(dev, file_priv, arg->obj_id, arg->obj_type);
  426. if (!arg_obj)
  427. return -ENOENT;
  428. if (!arg_obj->properties)
  429. goto out_unref;
  430. property = drm_mode_obj_find_prop_id(arg_obj, arg->prop_id);
  431. if (!property)
  432. goto out_unref;
  433. if (drm_drv_uses_atomic_modeset(property->dev))
  434. ret = set_property_atomic(arg_obj, property, arg->value);
  435. else
  436. ret = set_property_legacy(arg_obj, property, arg->value);
  437. out_unref:
  438. drm_mode_object_put(arg_obj);
  439. return ret;
  440. }