drm_mode_object.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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. WARN_ON(drm_drv_uses_atomic_modeset(property->dev) &&
  216. !(property->flags & DRM_MODE_PROP_IMMUTABLE));
  217. for (i = 0; i < obj->properties->count; i++) {
  218. if (obj->properties->properties[i] == property) {
  219. obj->properties->values[i] = val;
  220. return 0;
  221. }
  222. }
  223. return -EINVAL;
  224. }
  225. EXPORT_SYMBOL(drm_object_property_set_value);
  226. static int __drm_object_property_get_value(struct drm_mode_object *obj,
  227. struct drm_property *property,
  228. uint64_t *val)
  229. {
  230. int i;
  231. /* read-only properties bypass atomic mechanism and still store
  232. * their value in obj->properties->values[].. mostly to avoid
  233. * having to deal w/ EDID and similar props in atomic paths:
  234. */
  235. if (drm_drv_uses_atomic_modeset(property->dev) &&
  236. !(property->flags & DRM_MODE_PROP_IMMUTABLE))
  237. return drm_atomic_get_property(obj, property, val);
  238. for (i = 0; i < obj->properties->count; i++) {
  239. if (obj->properties->properties[i] == property) {
  240. *val = obj->properties->values[i];
  241. return 0;
  242. }
  243. }
  244. return -EINVAL;
  245. }
  246. /**
  247. * drm_object_property_get_value - retrieve the value of a property
  248. * @obj: drm mode object to get property value from
  249. * @property: property to retrieve
  250. * @val: storage for the property value
  251. *
  252. * This function retrieves the softare state of the given property for the given
  253. * property. Since there is no driver callback to retrieve the current property
  254. * value this might be out of sync with the hardware, depending upon the driver
  255. * and property.
  256. *
  257. * Atomic drivers should never call this function directly, the core will read
  258. * out property values through the various ->atomic_get_property callbacks.
  259. *
  260. * Returns:
  261. * Zero on success, error code on failure.
  262. */
  263. int drm_object_property_get_value(struct drm_mode_object *obj,
  264. struct drm_property *property, uint64_t *val)
  265. {
  266. WARN_ON(drm_drv_uses_atomic_modeset(property->dev));
  267. return __drm_object_property_get_value(obj, property, val);
  268. }
  269. EXPORT_SYMBOL(drm_object_property_get_value);
  270. /* helper for getconnector and getproperties ioctls */
  271. int drm_mode_object_get_properties(struct drm_mode_object *obj, bool atomic,
  272. uint32_t __user *prop_ptr,
  273. uint64_t __user *prop_values,
  274. uint32_t *arg_count_props)
  275. {
  276. int i, ret, count;
  277. for (i = 0, count = 0; i < obj->properties->count; i++) {
  278. struct drm_property *prop = obj->properties->properties[i];
  279. uint64_t val;
  280. if ((prop->flags & DRM_MODE_PROP_ATOMIC) && !atomic)
  281. continue;
  282. if (*arg_count_props > count) {
  283. ret = __drm_object_property_get_value(obj, prop, &val);
  284. if (ret)
  285. return ret;
  286. if (put_user(prop->base.id, prop_ptr + count))
  287. return -EFAULT;
  288. if (put_user(val, prop_values + count))
  289. return -EFAULT;
  290. }
  291. count++;
  292. }
  293. *arg_count_props = count;
  294. return 0;
  295. }
  296. /**
  297. * drm_mode_obj_get_properties_ioctl - get the current value of a object's property
  298. * @dev: DRM device
  299. * @data: ioctl data
  300. * @file_priv: DRM file info
  301. *
  302. * This function retrieves the current value for an object's property. Compared
  303. * to the connector specific ioctl this one is extended to also work on crtc and
  304. * plane objects.
  305. *
  306. * Called by the user via ioctl.
  307. *
  308. * Returns:
  309. * Zero on success, negative errno on failure.
  310. */
  311. int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
  312. struct drm_file *file_priv)
  313. {
  314. struct drm_mode_obj_get_properties *arg = data;
  315. struct drm_mode_object *obj;
  316. int ret = 0;
  317. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  318. return -EINVAL;
  319. drm_modeset_lock_all(dev);
  320. obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
  321. if (!obj) {
  322. ret = -ENOENT;
  323. goto out;
  324. }
  325. if (!obj->properties) {
  326. ret = -EINVAL;
  327. goto out_unref;
  328. }
  329. ret = drm_mode_object_get_properties(obj, file_priv->atomic,
  330. (uint32_t __user *)(unsigned long)(arg->props_ptr),
  331. (uint64_t __user *)(unsigned long)(arg->prop_values_ptr),
  332. &arg->count_props);
  333. out_unref:
  334. drm_mode_object_put(obj);
  335. out:
  336. drm_modeset_unlock_all(dev);
  337. return ret;
  338. }
  339. struct drm_property *drm_mode_obj_find_prop_id(struct drm_mode_object *obj,
  340. uint32_t prop_id)
  341. {
  342. int i;
  343. for (i = 0; i < obj->properties->count; i++)
  344. if (obj->properties->properties[i]->base.id == prop_id)
  345. return obj->properties->properties[i];
  346. return NULL;
  347. }
  348. static int set_property_legacy(struct drm_mode_object *obj,
  349. struct drm_property *prop,
  350. uint64_t prop_value)
  351. {
  352. struct drm_device *dev = prop->dev;
  353. struct drm_mode_object *ref;
  354. int ret = -EINVAL;
  355. if (!drm_property_change_valid_get(prop, prop_value, &ref))
  356. return -EINVAL;
  357. drm_modeset_lock_all(dev);
  358. switch (obj->type) {
  359. case DRM_MODE_OBJECT_CONNECTOR:
  360. ret = drm_mode_connector_set_obj_prop(obj, prop,
  361. prop_value);
  362. break;
  363. case DRM_MODE_OBJECT_CRTC:
  364. ret = drm_mode_crtc_set_obj_prop(obj, prop, prop_value);
  365. break;
  366. case DRM_MODE_OBJECT_PLANE:
  367. ret = drm_mode_plane_set_obj_prop(obj_to_plane(obj),
  368. prop, prop_value);
  369. break;
  370. }
  371. drm_property_change_valid_put(prop, ref);
  372. drm_modeset_unlock_all(dev);
  373. return ret;
  374. }
  375. static int set_property_atomic(struct drm_mode_object *obj,
  376. struct drm_property *prop,
  377. uint64_t prop_value)
  378. {
  379. struct drm_device *dev = prop->dev;
  380. struct drm_atomic_state *state;
  381. struct drm_modeset_acquire_ctx ctx;
  382. int ret;
  383. drm_modeset_acquire_init(&ctx, 0);
  384. state = drm_atomic_state_alloc(dev);
  385. if (!state)
  386. return -ENOMEM;
  387. state->acquire_ctx = &ctx;
  388. retry:
  389. if (prop == state->dev->mode_config.dpms_property) {
  390. if (obj->type != DRM_MODE_OBJECT_CONNECTOR) {
  391. ret = -EINVAL;
  392. goto out;
  393. }
  394. ret = drm_atomic_connector_commit_dpms(state,
  395. obj_to_connector(obj),
  396. prop_value);
  397. } else {
  398. ret = drm_atomic_set_property(state, obj, prop, prop_value);
  399. if (ret)
  400. goto out;
  401. ret = drm_atomic_commit(state);
  402. }
  403. out:
  404. if (ret == -EDEADLK) {
  405. drm_atomic_state_clear(state);
  406. drm_modeset_backoff(&ctx);
  407. goto retry;
  408. }
  409. drm_atomic_state_put(state);
  410. drm_modeset_drop_locks(&ctx);
  411. drm_modeset_acquire_fini(&ctx);
  412. return ret;
  413. }
  414. int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
  415. struct drm_file *file_priv)
  416. {
  417. struct drm_mode_obj_set_property *arg = data;
  418. struct drm_mode_object *arg_obj;
  419. struct drm_property *property;
  420. int ret = -EINVAL;
  421. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  422. return -EINVAL;
  423. arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
  424. if (!arg_obj)
  425. return -ENOENT;
  426. if (!arg_obj->properties)
  427. goto out_unref;
  428. property = drm_mode_obj_find_prop_id(arg_obj, arg->prop_id);
  429. if (!property)
  430. goto out_unref;
  431. if (drm_drv_uses_atomic_modeset(property->dev))
  432. ret = set_property_atomic(arg_obj, property, arg->value);
  433. else
  434. ret = set_property_legacy(arg_obj, property, arg->value);
  435. out_unref:
  436. drm_mode_object_put(arg_obj);
  437. return ret;
  438. }