drm_mode_object.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  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 <linux/uaccess.h>
  24. #include <drm/drm_atomic.h>
  25. #include <drm/drm_drv.h>
  26. #include <drm/drm_device.h>
  27. #include <drm/drm_file.h>
  28. #include <drm/drm_mode_object.h>
  29. #include <drm/drm_print.h>
  30. #include "drm_crtc_internal.h"
  31. /*
  32. * Internal function to assign a slot in the object idr and optionally
  33. * register the object into the idr.
  34. */
  35. int __drm_mode_object_add(struct drm_device *dev, struct drm_mode_object *obj,
  36. uint32_t obj_type, bool register_obj,
  37. void (*obj_free_cb)(struct kref *kref))
  38. {
  39. int ret;
  40. WARN_ON(!dev->driver->load && dev->registered && !obj_free_cb);
  41. mutex_lock(&dev->mode_config.idr_mutex);
  42. ret = idr_alloc(&dev->mode_config.object_idr, register_obj ? obj : NULL,
  43. 1, 0, GFP_KERNEL);
  44. if (ret >= 0) {
  45. /*
  46. * Set up the object linking under the protection of the idr
  47. * lock so that other users can't see inconsistent state.
  48. */
  49. obj->id = ret;
  50. obj->type = obj_type;
  51. if (obj_free_cb) {
  52. obj->free_cb = obj_free_cb;
  53. kref_init(&obj->refcount);
  54. }
  55. }
  56. mutex_unlock(&dev->mode_config.idr_mutex);
  57. return ret < 0 ? ret : 0;
  58. }
  59. /**
  60. * drm_mode_object_add - allocate a new modeset identifier
  61. * @dev: DRM device
  62. * @obj: object pointer, used to generate unique ID
  63. * @obj_type: object type
  64. *
  65. * Create a unique identifier based on @ptr in @dev's identifier space. Used
  66. * for tracking modes, CRTCs and connectors.
  67. *
  68. * Returns:
  69. * Zero on success, error code on failure.
  70. */
  71. int drm_mode_object_add(struct drm_device *dev,
  72. struct drm_mode_object *obj, uint32_t obj_type)
  73. {
  74. return __drm_mode_object_add(dev, obj, obj_type, true, NULL);
  75. }
  76. void drm_mode_object_register(struct drm_device *dev,
  77. struct drm_mode_object *obj)
  78. {
  79. mutex_lock(&dev->mode_config.idr_mutex);
  80. idr_replace(&dev->mode_config.object_idr, obj, obj->id);
  81. mutex_unlock(&dev->mode_config.idr_mutex);
  82. }
  83. /**
  84. * drm_mode_object_unregister - free a modeset identifer
  85. * @dev: DRM device
  86. * @object: object to free
  87. *
  88. * Free @id from @dev's unique identifier pool.
  89. * This function can be called multiple times, and guards against
  90. * multiple removals.
  91. * These modeset identifiers are _not_ reference counted. Hence don't use this
  92. * for reference counted modeset objects like framebuffers.
  93. */
  94. void drm_mode_object_unregister(struct drm_device *dev,
  95. struct drm_mode_object *object)
  96. {
  97. WARN_ON(!dev->driver->load && dev->registered && !object->free_cb);
  98. mutex_lock(&dev->mode_config.idr_mutex);
  99. if (object->id) {
  100. idr_remove(&dev->mode_config.object_idr, object->id);
  101. object->id = 0;
  102. }
  103. mutex_unlock(&dev->mode_config.idr_mutex);
  104. }
  105. /**
  106. * drm_lease_required - check types which must be leased to be used
  107. * @type: type of object
  108. *
  109. * Returns whether the provided type of drm_mode_object must
  110. * be owned or leased to be used by a process.
  111. */
  112. bool drm_mode_object_lease_required(uint32_t type)
  113. {
  114. switch(type) {
  115. case DRM_MODE_OBJECT_CRTC:
  116. case DRM_MODE_OBJECT_CONNECTOR:
  117. case DRM_MODE_OBJECT_PLANE:
  118. return true;
  119. default:
  120. return false;
  121. }
  122. }
  123. struct drm_mode_object *__drm_mode_object_find(struct drm_device *dev,
  124. struct drm_file *file_priv,
  125. uint32_t id, uint32_t type)
  126. {
  127. struct drm_mode_object *obj = NULL;
  128. mutex_lock(&dev->mode_config.idr_mutex);
  129. obj = idr_find(&dev->mode_config.object_idr, id);
  130. if (obj && type != DRM_MODE_OBJECT_ANY && obj->type != type)
  131. obj = NULL;
  132. if (obj && obj->id != id)
  133. obj = NULL;
  134. if (obj && drm_mode_object_lease_required(obj->type) &&
  135. !_drm_lease_held(file_priv, obj->id))
  136. obj = NULL;
  137. if (obj && obj->free_cb) {
  138. if (!kref_get_unless_zero(&obj->refcount))
  139. obj = NULL;
  140. }
  141. mutex_unlock(&dev->mode_config.idr_mutex);
  142. return obj;
  143. }
  144. /**
  145. * drm_mode_object_find - look up a drm object with static lifetime
  146. * @dev: drm device
  147. * @file_priv: drm file
  148. * @id: id of the mode object
  149. * @type: type of the mode object
  150. *
  151. * This function is used to look up a modeset object. It will acquire a
  152. * reference for reference counted objects. This reference must be dropped again
  153. * by callind drm_mode_object_put().
  154. */
  155. struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
  156. struct drm_file *file_priv,
  157. uint32_t id, uint32_t type)
  158. {
  159. struct drm_mode_object *obj = NULL;
  160. obj = __drm_mode_object_find(dev, file_priv, id, type);
  161. return obj;
  162. }
  163. EXPORT_SYMBOL(drm_mode_object_find);
  164. /**
  165. * drm_mode_object_put - release a mode object reference
  166. * @obj: DRM mode object
  167. *
  168. * This function decrements the object's refcount if it is a refcounted modeset
  169. * object. It is a no-op on any other object. This is used to drop references
  170. * acquired with drm_mode_object_get().
  171. */
  172. void drm_mode_object_put(struct drm_mode_object *obj)
  173. {
  174. if (obj->free_cb) {
  175. DRM_DEBUG("OBJ ID: %d (%d)\n", obj->id, kref_read(&obj->refcount));
  176. kref_put(&obj->refcount, obj->free_cb);
  177. }
  178. }
  179. EXPORT_SYMBOL(drm_mode_object_put);
  180. /**
  181. * drm_mode_object_get - acquire a mode object reference
  182. * @obj: DRM mode object
  183. *
  184. * This function increments the object's refcount if it is a refcounted modeset
  185. * object. It is a no-op on any other object. References should be dropped again
  186. * by calling drm_mode_object_put().
  187. */
  188. void drm_mode_object_get(struct drm_mode_object *obj)
  189. {
  190. if (obj->free_cb) {
  191. DRM_DEBUG("OBJ ID: %d (%d)\n", obj->id, kref_read(&obj->refcount));
  192. kref_get(&obj->refcount);
  193. }
  194. }
  195. EXPORT_SYMBOL(drm_mode_object_get);
  196. /**
  197. * drm_object_attach_property - attach a property to a modeset object
  198. * @obj: drm modeset object
  199. * @property: property to attach
  200. * @init_val: initial value of the property
  201. *
  202. * This attaches the given property to the modeset object with the given initial
  203. * value. Currently this function cannot fail since the properties are stored in
  204. * a statically sized array.
  205. *
  206. * Note that all properties must be attached before the object itself is
  207. * registered and accessible from userspace.
  208. */
  209. void drm_object_attach_property(struct drm_mode_object *obj,
  210. struct drm_property *property,
  211. uint64_t init_val)
  212. {
  213. int count = obj->properties->count;
  214. struct drm_device *dev = property->dev;
  215. if (obj->type == DRM_MODE_OBJECT_CONNECTOR) {
  216. struct drm_connector *connector = obj_to_connector(obj);
  217. WARN_ON(!dev->driver->load &&
  218. connector->registration_state == DRM_CONNECTOR_REGISTERED);
  219. } else {
  220. WARN_ON(!dev->driver->load && dev->registered);
  221. }
  222. if (count == DRM_OBJECT_MAX_PROPERTY) {
  223. WARN(1, "Failed to attach object property (type: 0x%x). Please "
  224. "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
  225. "you see this message on the same object type.\n",
  226. obj->type);
  227. return;
  228. }
  229. obj->properties->properties[count] = property;
  230. obj->properties->values[count] = init_val;
  231. obj->properties->count++;
  232. }
  233. EXPORT_SYMBOL(drm_object_attach_property);
  234. /**
  235. * drm_object_property_set_value - set the value of a property
  236. * @obj: drm mode object to set property value for
  237. * @property: property to set
  238. * @val: value the property should be set to
  239. *
  240. * This function sets a given property on a given object. This function only
  241. * changes the software state of the property, it does not call into the
  242. * driver's ->set_property callback.
  243. *
  244. * Note that atomic drivers should not have any need to call this, the core will
  245. * ensure consistency of values reported back to userspace through the
  246. * appropriate ->atomic_get_property callback. Only legacy drivers should call
  247. * this function to update the tracked value (after clamping and other
  248. * restrictions have been applied).
  249. *
  250. * Returns:
  251. * Zero on success, error code on failure.
  252. */
  253. int drm_object_property_set_value(struct drm_mode_object *obj,
  254. struct drm_property *property, uint64_t val)
  255. {
  256. int i;
  257. WARN_ON(drm_drv_uses_atomic_modeset(property->dev) &&
  258. !(property->flags & DRM_MODE_PROP_IMMUTABLE));
  259. for (i = 0; i < obj->properties->count; i++) {
  260. if (obj->properties->properties[i] == property) {
  261. obj->properties->values[i] = val;
  262. return 0;
  263. }
  264. }
  265. return -EINVAL;
  266. }
  267. EXPORT_SYMBOL(drm_object_property_set_value);
  268. static int __drm_object_property_get_value(struct drm_mode_object *obj,
  269. struct drm_property *property,
  270. uint64_t *val)
  271. {
  272. int i;
  273. /* read-only properties bypass atomic mechanism and still store
  274. * their value in obj->properties->values[].. mostly to avoid
  275. * having to deal w/ EDID and similar props in atomic paths:
  276. */
  277. if (drm_drv_uses_atomic_modeset(property->dev) &&
  278. !(property->flags & DRM_MODE_PROP_IMMUTABLE))
  279. return drm_atomic_get_property(obj, property, val);
  280. for (i = 0; i < obj->properties->count; i++) {
  281. if (obj->properties->properties[i] == property) {
  282. *val = obj->properties->values[i];
  283. return 0;
  284. }
  285. }
  286. return -EINVAL;
  287. }
  288. /**
  289. * drm_object_property_get_value - retrieve the value of a property
  290. * @obj: drm mode object to get property value from
  291. * @property: property to retrieve
  292. * @val: storage for the property value
  293. *
  294. * This function retrieves the softare state of the given property for the given
  295. * property. Since there is no driver callback to retrieve the current property
  296. * value this might be out of sync with the hardware, depending upon the driver
  297. * and property.
  298. *
  299. * Atomic drivers should never call this function directly, the core will read
  300. * out property values through the various ->atomic_get_property callbacks.
  301. *
  302. * Returns:
  303. * Zero on success, error code on failure.
  304. */
  305. int drm_object_property_get_value(struct drm_mode_object *obj,
  306. struct drm_property *property, uint64_t *val)
  307. {
  308. WARN_ON(drm_drv_uses_atomic_modeset(property->dev));
  309. return __drm_object_property_get_value(obj, property, val);
  310. }
  311. EXPORT_SYMBOL(drm_object_property_get_value);
  312. /* helper for getconnector and getproperties ioctls */
  313. int drm_mode_object_get_properties(struct drm_mode_object *obj, bool atomic,
  314. uint32_t __user *prop_ptr,
  315. uint64_t __user *prop_values,
  316. uint32_t *arg_count_props)
  317. {
  318. int i, ret, count;
  319. for (i = 0, count = 0; i < obj->properties->count; i++) {
  320. struct drm_property *prop = obj->properties->properties[i];
  321. uint64_t val;
  322. if ((prop->flags & DRM_MODE_PROP_ATOMIC) && !atomic)
  323. continue;
  324. if (*arg_count_props > count) {
  325. ret = __drm_object_property_get_value(obj, prop, &val);
  326. if (ret)
  327. return ret;
  328. if (put_user(prop->base.id, prop_ptr + count))
  329. return -EFAULT;
  330. if (put_user(val, prop_values + count))
  331. return -EFAULT;
  332. }
  333. count++;
  334. }
  335. *arg_count_props = count;
  336. return 0;
  337. }
  338. /**
  339. * drm_mode_obj_get_properties_ioctl - get the current value of a object's property
  340. * @dev: DRM device
  341. * @data: ioctl data
  342. * @file_priv: DRM file info
  343. *
  344. * This function retrieves the current value for an object's property. Compared
  345. * to the connector specific ioctl this one is extended to also work on crtc and
  346. * plane objects.
  347. *
  348. * Called by the user via ioctl.
  349. *
  350. * Returns:
  351. * Zero on success, negative errno on failure.
  352. */
  353. int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
  354. struct drm_file *file_priv)
  355. {
  356. struct drm_mode_obj_get_properties *arg = data;
  357. struct drm_mode_object *obj;
  358. struct drm_modeset_acquire_ctx ctx;
  359. int ret = 0;
  360. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  361. return -EOPNOTSUPP;
  362. DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx, 0, ret);
  363. obj = drm_mode_object_find(dev, file_priv, arg->obj_id, arg->obj_type);
  364. if (!obj) {
  365. ret = -ENOENT;
  366. goto out;
  367. }
  368. if (!obj->properties) {
  369. ret = -EINVAL;
  370. goto out_unref;
  371. }
  372. ret = drm_mode_object_get_properties(obj, file_priv->atomic,
  373. (uint32_t __user *)(unsigned long)(arg->props_ptr),
  374. (uint64_t __user *)(unsigned long)(arg->prop_values_ptr),
  375. &arg->count_props);
  376. out_unref:
  377. drm_mode_object_put(obj);
  378. out:
  379. DRM_MODESET_LOCK_ALL_END(dev, ctx, ret);
  380. return ret;
  381. }
  382. struct drm_property *drm_mode_obj_find_prop_id(struct drm_mode_object *obj,
  383. uint32_t prop_id)
  384. {
  385. int i;
  386. for (i = 0; i < obj->properties->count; i++)
  387. if (obj->properties->properties[i]->base.id == prop_id)
  388. return obj->properties->properties[i];
  389. return NULL;
  390. }
  391. static int set_property_legacy(struct drm_mode_object *obj,
  392. struct drm_property *prop,
  393. uint64_t prop_value)
  394. {
  395. struct drm_device *dev = prop->dev;
  396. struct drm_mode_object *ref;
  397. struct drm_modeset_acquire_ctx ctx;
  398. int ret = -EINVAL;
  399. if (!drm_property_change_valid_get(prop, prop_value, &ref))
  400. return -EINVAL;
  401. DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx, 0, ret);
  402. switch (obj->type) {
  403. case DRM_MODE_OBJECT_CONNECTOR:
  404. ret = drm_connector_set_obj_prop(obj, prop, prop_value);
  405. break;
  406. case DRM_MODE_OBJECT_CRTC:
  407. ret = drm_mode_crtc_set_obj_prop(obj, prop, prop_value);
  408. break;
  409. case DRM_MODE_OBJECT_PLANE:
  410. ret = drm_mode_plane_set_obj_prop(obj_to_plane(obj),
  411. prop, prop_value);
  412. break;
  413. }
  414. drm_property_change_valid_put(prop, ref);
  415. DRM_MODESET_LOCK_ALL_END(dev, ctx, ret);
  416. return ret;
  417. }
  418. static int set_property_atomic(struct drm_mode_object *obj,
  419. struct drm_file *file_priv,
  420. struct drm_property *prop,
  421. uint64_t prop_value)
  422. {
  423. struct drm_device *dev = prop->dev;
  424. struct drm_atomic_state *state;
  425. struct drm_modeset_acquire_ctx ctx;
  426. int ret;
  427. state = drm_atomic_state_alloc(dev);
  428. if (!state)
  429. return -ENOMEM;
  430. drm_modeset_acquire_init(&ctx, 0);
  431. state->acquire_ctx = &ctx;
  432. retry:
  433. if (prop == state->dev->mode_config.dpms_property) {
  434. if (obj->type != DRM_MODE_OBJECT_CONNECTOR) {
  435. ret = -EINVAL;
  436. goto out;
  437. }
  438. ret = drm_atomic_connector_commit_dpms(state,
  439. obj_to_connector(obj),
  440. prop_value);
  441. } else {
  442. ret = drm_atomic_set_property(state, file_priv, obj, prop, prop_value);
  443. if (ret)
  444. goto out;
  445. ret = drm_atomic_commit(state);
  446. }
  447. out:
  448. if (ret == -EDEADLK) {
  449. drm_atomic_state_clear(state);
  450. drm_modeset_backoff(&ctx);
  451. goto retry;
  452. }
  453. drm_atomic_state_put(state);
  454. drm_modeset_drop_locks(&ctx);
  455. drm_modeset_acquire_fini(&ctx);
  456. return ret;
  457. }
  458. int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
  459. struct drm_file *file_priv)
  460. {
  461. struct drm_mode_obj_set_property *arg = data;
  462. struct drm_mode_object *arg_obj;
  463. struct drm_property *property;
  464. int ret = -EINVAL;
  465. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  466. return -EOPNOTSUPP;
  467. arg_obj = drm_mode_object_find(dev, file_priv, arg->obj_id, arg->obj_type);
  468. if (!arg_obj)
  469. return -ENOENT;
  470. if (!arg_obj->properties)
  471. goto out_unref;
  472. property = drm_mode_obj_find_prop_id(arg_obj, arg->prop_id);
  473. if (!property)
  474. goto out_unref;
  475. if (drm_drv_uses_atomic_modeset(property->dev))
  476. ret = set_property_atomic(arg_obj, file_priv, property, arg->value);
  477. else
  478. ret = set_property_legacy(arg_obj, property, arg->value);
  479. out_unref:
  480. drm_mode_object_put(arg_obj);
  481. return ret;
  482. }