drm_mode_object.c 14 KB

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