ttm_object.h 8.6 KB

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