ttm_object.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /**************************************************************************
  2. *
  3. * Copyright (c) 2006-2023 VMware, Inc., Palo Alto, CA., USA
  4. * All Rights Reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sub license, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice (including the
  15. * next paragraph) shall be included in all copies or substantial portions
  16. * of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  21. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  22. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  23. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  24. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. *
  26. **************************************************************************/
  27. /*
  28. * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
  29. */
  30. /** @file ttm_object.h
  31. *
  32. * Base- and reference object implementation for the various
  33. * ttm objects. Implements reference counting, minimal security checks
  34. * and release on file close.
  35. */
  36. #ifndef _TTM_OBJECT_H_
  37. #define _TTM_OBJECT_H_
  38. #include <linux/dma-buf.h>
  39. #include <linux/kref.h>
  40. #include <linux/list.h>
  41. #include <linux/rcupdate.h>
  42. #include <drm/ttm/ttm_bo.h>
  43. /**
  44. * enum ttm_object_type
  45. *
  46. * One entry per ttm object type.
  47. * Device-specific types should use the
  48. * ttm_driver_typex types.
  49. */
  50. enum ttm_object_type {
  51. ttm_fence_type,
  52. ttm_lock_type,
  53. ttm_prime_type,
  54. ttm_driver_type0 = 256,
  55. ttm_driver_type1,
  56. ttm_driver_type2,
  57. ttm_driver_type3,
  58. ttm_driver_type4,
  59. ttm_driver_type5
  60. };
  61. struct ttm_object_file;
  62. struct ttm_object_device;
  63. /**
  64. * struct ttm_base_object
  65. *
  66. * @hash: hash entry for the per-device object hash.
  67. * @type: derived type this object is base class for.
  68. * @shareable: Other ttm_object_files can access this object.
  69. *
  70. * @tfile: Pointer to ttm_object_file of the creator.
  71. * NULL if the object was not created by a user request.
  72. * (kernel object).
  73. *
  74. * @refcount: Number of references to this object, not
  75. * including the hash entry. A reference to a base object can
  76. * only be held by a ref object.
  77. *
  78. * @refcount_release: A function to be called when there are
  79. * no more references to this object. This function should
  80. * destroy the object (or make sure destruction eventually happens),
  81. * and when it is called, the object has
  82. * already been taken out of the per-device hash. The parameter
  83. * "base" should be set to NULL by the function.
  84. *
  85. * @ref_obj_release: A function to be called when a reference object
  86. * with another ttm_ref_type than TTM_REF_USAGE is deleted.
  87. * This function may, for example, release a lock held by a user-space
  88. * process.
  89. *
  90. * This struct is intended to be used as a base struct for objects that
  91. * are visible to user-space. It provides a global name, race-safe
  92. * access and refcounting, minimal access control and hooks for unref actions.
  93. */
  94. struct ttm_base_object {
  95. struct rcu_head rhead;
  96. struct ttm_object_file *tfile;
  97. struct kref refcount;
  98. void (*refcount_release) (struct ttm_base_object **base);
  99. u64 handle;
  100. enum ttm_object_type object_type;
  101. u32 shareable;
  102. };
  103. /**
  104. * struct ttm_prime_object - Modified base object that is prime-aware
  105. *
  106. * @base: struct ttm_base_object that we derive from
  107. * @mutex: Mutex protecting the @dma_buf member.
  108. * @size: Size of the dma_buf associated with this object
  109. * @real_type: Type of the underlying object. Needed since we're setting
  110. * the value of @base::object_type to ttm_prime_type
  111. * @dma_buf: Non ref-coutned pointer to a struct dma_buf created from this
  112. * object.
  113. * @refcount_release: The underlying object's release method. Needed since
  114. * we set @base::refcount_release to our own release method.
  115. */
  116. struct ttm_prime_object {
  117. struct ttm_base_object base;
  118. struct mutex mutex;
  119. size_t size;
  120. enum ttm_object_type real_type;
  121. struct dma_buf *dma_buf;
  122. void (*refcount_release) (struct ttm_base_object **);
  123. };
  124. /**
  125. * ttm_base_object_init
  126. *
  127. * @tfile: Pointer to a struct ttm_object_file.
  128. * @base: The struct ttm_base_object to initialize.
  129. * @shareable: This object is shareable with other applications.
  130. * (different @tfile pointers.)
  131. * @type: The object type.
  132. * @refcount_release: See the struct ttm_base_object description.
  133. * @ref_obj_release: See the struct ttm_base_object description.
  134. *
  135. * Initializes a struct ttm_base_object.
  136. */
  137. extern int ttm_base_object_init(struct ttm_object_file *tfile,
  138. struct ttm_base_object *base,
  139. bool shareable,
  140. enum ttm_object_type type,
  141. void (*refcount_release) (struct ttm_base_object
  142. **));
  143. /**
  144. * ttm_base_object_lookup
  145. *
  146. * @tfile: Pointer to a struct ttm_object_file.
  147. * @key: Hash key
  148. *
  149. * Looks up a struct ttm_base_object with the key @key.
  150. */
  151. extern struct ttm_base_object *ttm_base_object_lookup(struct ttm_object_file
  152. *tfile, uint64_t key);
  153. /**
  154. * ttm_base_object_lookup_for_ref
  155. *
  156. * @tdev: Pointer to a struct ttm_object_device.
  157. * @key: Hash key
  158. *
  159. * Looks up a struct ttm_base_object with the key @key.
  160. * This function should only be used when the struct tfile associated with the
  161. * caller doesn't yet have a reference to the base object.
  162. */
  163. extern struct ttm_base_object *
  164. ttm_base_object_lookup_for_ref(struct ttm_object_device *tdev, uint64_t key);
  165. /**
  166. * ttm_base_object_unref
  167. *
  168. * @p_base: Pointer to a pointer referencing a struct ttm_base_object.
  169. *
  170. * Decrements the base object refcount and clears the pointer pointed to by
  171. * p_base.
  172. */
  173. extern void ttm_base_object_unref(struct ttm_base_object **p_base);
  174. /**
  175. * ttm_ref_object_add.
  176. *
  177. * @tfile: A struct ttm_object_file representing the application owning the
  178. * ref_object.
  179. * @base: The base object to reference.
  180. * @ref_type: The type of reference.
  181. * @existed: Upon completion, indicates that an identical reference object
  182. * already existed, and the refcount was upped on that object instead.
  183. * @require_existed: Fail with -EPERM if an identical ref object didn't
  184. * already exist.
  185. *
  186. * Checks that the base object is shareable and adds a ref object to it.
  187. *
  188. * Adding a ref object to a base object is basically like referencing the
  189. * base object, but a user-space application holds the reference. When the
  190. * file corresponding to @tfile is closed, all its reference objects are
  191. * deleted. A reference object can have different types depending on what
  192. * it's intended for. It can be refcounting to prevent object destruction,
  193. * When user-space takes a lock, it can add a ref object to that lock to
  194. * make sure the lock is released if the application dies. A ref object
  195. * will hold a single reference on a base object.
  196. */
  197. extern int ttm_ref_object_add(struct ttm_object_file *tfile,
  198. struct ttm_base_object *base,
  199. bool *existed,
  200. bool require_existed);
  201. /**
  202. * ttm_ref_object_base_unref
  203. *
  204. * @key: Key representing the base object.
  205. * @ref_type: Ref type of the ref object to be dereferenced.
  206. *
  207. * Unreference a ref object with type @ref_type
  208. * on the base object identified by @key. If there are no duplicate
  209. * references, the ref object will be destroyed and the base object
  210. * will be unreferenced.
  211. */
  212. extern int ttm_ref_object_base_unref(struct ttm_object_file *tfile,
  213. unsigned long key);
  214. /**
  215. * ttm_object_file_init - initialize a struct ttm_object file
  216. *
  217. * @tdev: A struct ttm_object device this file is initialized on.
  218. *
  219. * This is typically called by the file_ops::open function.
  220. */
  221. extern struct ttm_object_file *ttm_object_file_init(struct ttm_object_device
  222. *tdev);
  223. /**
  224. * ttm_object_file_release - release data held by a ttm_object_file
  225. *
  226. * @p_tfile: Pointer to pointer to the ttm_object_file object to release.
  227. * *p_tfile will be set to NULL by this function.
  228. *
  229. * Releases all data associated by a ttm_object_file.
  230. * Typically called from file_ops::release. The caller must
  231. * ensure that there are no concurrent users of tfile.
  232. */
  233. extern void ttm_object_file_release(struct ttm_object_file **p_tfile);
  234. /**
  235. * ttm_object device init - initialize a struct ttm_object_device
  236. *
  237. * @ops: DMA buf ops for prime objects of this device.
  238. *
  239. * This function is typically called on device initialization to prepare
  240. * data structures needed for ttm base and ref objects.
  241. */
  242. extern struct ttm_object_device *
  243. ttm_object_device_init(const struct dma_buf_ops *ops);
  244. /**
  245. * ttm_object_device_release - release data held by a ttm_object_device
  246. *
  247. * @p_tdev: Pointer to pointer to the ttm_object_device object to release.
  248. * *p_tdev will be set to NULL by this function.
  249. *
  250. * Releases all data associated by a ttm_object_device.
  251. * Typically called from driver::unload before the destruction of the
  252. * device private data structure.
  253. */
  254. extern void ttm_object_device_release(struct ttm_object_device **p_tdev);
  255. #define ttm_base_object_kfree(__object, __base)\
  256. kfree_rcu(__object, __base.rhead)
  257. extern int ttm_prime_object_init(struct ttm_object_file *tfile,
  258. size_t size,
  259. struct ttm_prime_object *prime,
  260. enum ttm_object_type type,
  261. void (*refcount_release)
  262. (struct ttm_base_object **));
  263. static inline enum ttm_object_type
  264. ttm_base_object_type(struct ttm_base_object *base)
  265. {
  266. return (base->object_type == ttm_prime_type) ?
  267. container_of(base, struct ttm_prime_object, base)->real_type :
  268. base->object_type;
  269. }
  270. extern int ttm_prime_fd_to_handle(struct ttm_object_file *tfile,
  271. int fd, u32 *handle);
  272. extern int ttm_prime_handle_to_fd(struct ttm_object_file *tfile,
  273. uint32_t handle, uint32_t flags,
  274. int *prime_fd);
  275. #define ttm_prime_object_kfree(__obj, __prime) \
  276. kfree_rcu(__obj, __prime.base.rhead)
  277. static inline int ttm_bo_wait(struct ttm_buffer_object *bo, bool intr,
  278. bool no_wait)
  279. {
  280. struct ttm_operation_ctx ctx = { intr, no_wait };
  281. return ttm_bo_wait_ctx(bo, &ctx);
  282. }
  283. #endif