ttm_object.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /**************************************************************************
  2. *
  3. * Copyright (c) 2006-2009 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/list.h>
  39. #include <drm/drm_hashtab.h>
  40. #include <linux/kref.h>
  41. #include <linux/rcupdate.h>
  42. #include <ttm/ttm_memory.h>
  43. /**
  44. * enum ttm_ref_type
  45. *
  46. * Describes what type of reference a ref object holds.
  47. *
  48. * TTM_REF_USAGE is a simple refcount on a base object.
  49. *
  50. * TTM_REF_SYNCCPU_READ is a SYNCCPU_READ reference on a
  51. * buffer object.
  52. *
  53. * TTM_REF_SYNCCPU_WRITE is a SYNCCPU_WRITE reference on a
  54. * buffer object.
  55. *
  56. */
  57. enum ttm_ref_type {
  58. TTM_REF_USAGE,
  59. TTM_REF_SYNCCPU_READ,
  60. TTM_REF_SYNCCPU_WRITE,
  61. TTM_REF_NUM
  62. };
  63. /**
  64. * enum ttm_object_type
  65. *
  66. * One entry per ttm object type.
  67. * Device-specific types should use the
  68. * ttm_driver_typex types.
  69. */
  70. enum ttm_object_type {
  71. ttm_fence_type,
  72. ttm_buffer_type,
  73. ttm_lock_type,
  74. ttm_driver_type0 = 256,
  75. ttm_driver_type1,
  76. ttm_driver_type2,
  77. ttm_driver_type3,
  78. ttm_driver_type4,
  79. ttm_driver_type5
  80. };
  81. struct ttm_object_file;
  82. struct ttm_object_device;
  83. /**
  84. * struct ttm_base_object
  85. *
  86. * @hash: hash entry for the per-device object hash.
  87. * @type: derived type this object is base class for.
  88. * @shareable: Other ttm_object_files can access this object.
  89. *
  90. * @tfile: Pointer to ttm_object_file of the creator.
  91. * NULL if the object was not created by a user request.
  92. * (kernel object).
  93. *
  94. * @refcount: Number of references to this object, not
  95. * including the hash entry. A reference to a base object can
  96. * only be held by a ref object.
  97. *
  98. * @refcount_release: A function to be called when there are
  99. * no more references to this object. This function should
  100. * destroy the object (or make sure destruction eventually happens),
  101. * and when it is called, the object has
  102. * already been taken out of the per-device hash. The parameter
  103. * "base" should be set to NULL by the function.
  104. *
  105. * @ref_obj_release: A function to be called when a reference object
  106. * with another ttm_ref_type than TTM_REF_USAGE is deleted.
  107. * This function may, for example, release a lock held by a user-space
  108. * process.
  109. *
  110. * This struct is intended to be used as a base struct for objects that
  111. * are visible to user-space. It provides a global name, race-safe
  112. * access and refcounting, minimal access contol and hooks for unref actions.
  113. */
  114. struct ttm_base_object {
  115. struct rcu_head rhead;
  116. struct drm_hash_item hash;
  117. enum ttm_object_type object_type;
  118. bool shareable;
  119. struct ttm_object_file *tfile;
  120. struct kref refcount;
  121. void (*refcount_release) (struct ttm_base_object **base);
  122. void (*ref_obj_release) (struct ttm_base_object *base,
  123. enum ttm_ref_type ref_type);
  124. };
  125. /**
  126. * ttm_base_object_init
  127. *
  128. * @tfile: Pointer to a struct ttm_object_file.
  129. * @base: The struct ttm_base_object to initialize.
  130. * @shareable: This object is shareable with other applcations.
  131. * (different @tfile pointers.)
  132. * @type: The object type.
  133. * @refcount_release: See the struct ttm_base_object description.
  134. * @ref_obj_release: See the struct ttm_base_object description.
  135. *
  136. * Initializes a struct ttm_base_object.
  137. */
  138. extern int ttm_base_object_init(struct ttm_object_file *tfile,
  139. struct ttm_base_object *base,
  140. bool shareable,
  141. enum ttm_object_type type,
  142. void (*refcount_release) (struct ttm_base_object
  143. **),
  144. void (*ref_obj_release) (struct ttm_base_object
  145. *,
  146. enum ttm_ref_type
  147. ref_type));
  148. /**
  149. * ttm_base_object_lookup
  150. *
  151. * @tfile: Pointer to a struct ttm_object_file.
  152. * @key: Hash key
  153. *
  154. * Looks up a struct ttm_base_object with the key @key.
  155. * Also verifies that the object is visible to the application, by
  156. * comparing the @tfile argument and checking the object shareable flag.
  157. */
  158. extern struct ttm_base_object *ttm_base_object_lookup(struct ttm_object_file
  159. *tfile, uint32_t key);
  160. /**
  161. * ttm_base_object_unref
  162. *
  163. * @p_base: Pointer to a pointer referencing a struct ttm_base_object.
  164. *
  165. * Decrements the base object refcount and clears the pointer pointed to by
  166. * p_base.
  167. */
  168. extern void ttm_base_object_unref(struct ttm_base_object **p_base);
  169. /**
  170. * ttm_ref_object_add.
  171. *
  172. * @tfile: A struct ttm_object_file representing the application owning the
  173. * ref_object.
  174. * @base: The base object to reference.
  175. * @ref_type: The type of reference.
  176. * @existed: Upon completion, indicates that an identical reference object
  177. * already existed, and the refcount was upped on that object instead.
  178. *
  179. * Adding a ref object to a base object is basically like referencing the
  180. * base object, but a user-space application holds the reference. When the
  181. * file corresponding to @tfile is closed, all its reference objects are
  182. * deleted. A reference object can have different types depending on what
  183. * it's intended for. It can be refcounting to prevent object destruction,
  184. * When user-space takes a lock, it can add a ref object to that lock to
  185. * make sure the lock is released if the application dies. A ref object
  186. * will hold a single reference on a base object.
  187. */
  188. extern int ttm_ref_object_add(struct ttm_object_file *tfile,
  189. struct ttm_base_object *base,
  190. enum ttm_ref_type ref_type, bool *existed);
  191. /**
  192. * ttm_ref_object_base_unref
  193. *
  194. * @key: Key representing the base object.
  195. * @ref_type: Ref type of the ref object to be dereferenced.
  196. *
  197. * Unreference a ref object with type @ref_type
  198. * on the base object identified by @key. If there are no duplicate
  199. * references, the ref object will be destroyed and the base object
  200. * will be unreferenced.
  201. */
  202. extern int ttm_ref_object_base_unref(struct ttm_object_file *tfile,
  203. unsigned long key,
  204. enum ttm_ref_type ref_type);
  205. /**
  206. * ttm_object_file_init - initialize a struct ttm_object file
  207. *
  208. * @tdev: A struct ttm_object device this file is initialized on.
  209. * @hash_order: Order of the hash table used to hold the reference objects.
  210. *
  211. * This is typically called by the file_ops::open function.
  212. */
  213. extern struct ttm_object_file *ttm_object_file_init(struct ttm_object_device
  214. *tdev,
  215. unsigned int hash_order);
  216. /**
  217. * ttm_object_file_release - release data held by a ttm_object_file
  218. *
  219. * @p_tfile: Pointer to pointer to the ttm_object_file object to release.
  220. * *p_tfile will be set to NULL by this function.
  221. *
  222. * Releases all data associated by a ttm_object_file.
  223. * Typically called from file_ops::release. The caller must
  224. * ensure that there are no concurrent users of tfile.
  225. */
  226. extern void ttm_object_file_release(struct ttm_object_file **p_tfile);
  227. /**
  228. * ttm_object device init - initialize a struct ttm_object_device
  229. *
  230. * @hash_order: Order of hash table used to hash the base objects.
  231. *
  232. * This function is typically called on device initialization to prepare
  233. * data structures needed for ttm base and ref objects.
  234. */
  235. extern struct ttm_object_device *ttm_object_device_init
  236. (struct ttm_mem_global *mem_glob, unsigned int hash_order);
  237. /**
  238. * ttm_object_device_release - release data held by a ttm_object_device
  239. *
  240. * @p_tdev: Pointer to pointer to the ttm_object_device object to release.
  241. * *p_tdev will be set to NULL by this function.
  242. *
  243. * Releases all data associated by a ttm_object_device.
  244. * Typically called from driver::unload before the destruction of the
  245. * device private data structure.
  246. */
  247. extern void ttm_object_device_release(struct ttm_object_device **p_tdev);
  248. #define ttm_base_object_kfree(__object, __base)\
  249. kfree_rcu(__object, __base.rhead)
  250. #endif