drm_mode_object.c 17 KB

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