drm_mode_object.c 15 KB

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