vmwgfx_validation.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /* SPDX-License-Identifier: GPL-2.0 OR MIT */
  2. /**************************************************************************
  3. *
  4. * Copyright © 2018 VMware, Inc., Palo Alto, CA., USA
  5. * All Rights Reserved.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the
  9. * "Software"), to deal in the Software without restriction, including
  10. * without limitation the rights to use, copy, modify, merge, publish,
  11. * distribute, sub license, and/or sell copies of the Software, and to
  12. * permit persons to whom the Software is furnished to do so, subject to
  13. * the following conditions:
  14. *
  15. * The above copyright notice and this permission notice (including the
  16. * next paragraph) shall be included in all copies or substantial portions
  17. * of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  22. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  23. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  24. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  25. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. *
  27. **************************************************************************/
  28. #ifndef _VMWGFX_VALIDATION_H_
  29. #define _VMWGFX_VALIDATION_H_
  30. #include <drm/drm_hashtab.h>
  31. #include <linux/list.h>
  32. #include <linux/ww_mutex.h>
  33. #include <drm/ttm/ttm_execbuf_util.h>
  34. /**
  35. * struct vmw_validation_mem - Custom interface to provide memory reservations
  36. * for the validation code.
  37. * @reserve_mem: Callback to reserve memory
  38. * @unreserve_mem: Callback to unreserve memory
  39. * @gran: Reservation granularity. Contains a hint how much memory should
  40. * be reserved in each call to @reserve_mem(). A slow implementation may want
  41. * reservation to be done in large batches.
  42. */
  43. struct vmw_validation_mem {
  44. int (*reserve_mem)(struct vmw_validation_mem *m, size_t size);
  45. void (*unreserve_mem)(struct vmw_validation_mem *m, size_t size);
  46. size_t gran;
  47. };
  48. /**
  49. * struct vmw_validation_context - Per command submission validation context
  50. * @ht: Hash table used to find resource- or buffer object duplicates
  51. * @resource_list: List head for resource validation metadata
  52. * @resource_ctx_list: List head for resource validation metadata for
  53. * resources that need to be validated before those in @resource_list
  54. * @bo_list: List head for buffer objects
  55. * @page_list: List of pages used by the memory allocator
  56. * @ticket: Ticked used for ww mutex locking
  57. * @res_mutex: Pointer to mutex used for resource reserving
  58. * @merge_dups: Whether to merge metadata for duplicate resources or
  59. * buffer objects
  60. * @mem_size_left: Free memory left in the last page in @page_list
  61. * @page_address: Kernel virtual address of the last page in @page_list
  62. * @vm: A pointer to the memory reservation interface or NULL if no
  63. * memory reservation is needed.
  64. * @vm_size_left: Amount of reserved memory that so far has not been allocated.
  65. * @total_mem: Amount of reserved memory.
  66. */
  67. struct vmw_validation_context {
  68. struct drm_open_hash *ht;
  69. struct list_head resource_list;
  70. struct list_head resource_ctx_list;
  71. struct list_head bo_list;
  72. struct list_head page_list;
  73. struct ww_acquire_ctx ticket;
  74. struct mutex *res_mutex;
  75. unsigned int merge_dups;
  76. unsigned int mem_size_left;
  77. u8 *page_address;
  78. struct vmw_validation_mem *vm;
  79. size_t vm_size_left;
  80. size_t total_mem;
  81. };
  82. struct vmw_buffer_object;
  83. struct vmw_resource;
  84. struct vmw_fence_obj;
  85. #if 0
  86. /**
  87. * DECLARE_VAL_CONTEXT - Declare a validation context with initialization
  88. * @_name: The name of the variable
  89. * @_ht: The hash table used to find dups or NULL if none
  90. * @_merge_dups: Whether to merge duplicate buffer object- or resource
  91. * entries. If set to true, ideally a hash table pointer should be supplied
  92. * as well unless the number of resources and buffer objects per validation
  93. * is known to be very small
  94. */
  95. #endif
  96. #define DECLARE_VAL_CONTEXT(_name, _ht, _merge_dups) \
  97. struct vmw_validation_context _name = \
  98. { .ht = _ht, \
  99. .resource_list = LIST_HEAD_INIT((_name).resource_list), \
  100. .resource_ctx_list = LIST_HEAD_INIT((_name).resource_ctx_list), \
  101. .bo_list = LIST_HEAD_INIT((_name).bo_list), \
  102. .page_list = LIST_HEAD_INIT((_name).page_list), \
  103. .res_mutex = NULL, \
  104. .merge_dups = _merge_dups, \
  105. .mem_size_left = 0, \
  106. }
  107. /**
  108. * vmw_validation_has_bos - return whether the validation context has
  109. * any buffer objects registered.
  110. *
  111. * @ctx: The validation context
  112. * Returns: Whether any buffer objects are registered
  113. */
  114. static inline bool
  115. vmw_validation_has_bos(struct vmw_validation_context *ctx)
  116. {
  117. return !list_empty(&ctx->bo_list);
  118. }
  119. /**
  120. * vmw_validation_set_val_mem - Register a validation mem object for
  121. * validation memory reservation
  122. * @ctx: The validation context
  123. * @vm: Pointer to a struct vmw_validation_mem
  124. *
  125. * Must be set before the first attempt to allocate validation memory.
  126. */
  127. static inline void
  128. vmw_validation_set_val_mem(struct vmw_validation_context *ctx,
  129. struct vmw_validation_mem *vm)
  130. {
  131. ctx->vm = vm;
  132. }
  133. /**
  134. * vmw_validation_set_ht - Register a hash table for duplicate finding
  135. * @ctx: The validation context
  136. * @ht: Pointer to a hash table to use for duplicate finding
  137. * This function is intended to be used if the hash table wasn't
  138. * available at validation context declaration time
  139. */
  140. static inline void vmw_validation_set_ht(struct vmw_validation_context *ctx,
  141. struct drm_open_hash *ht)
  142. {
  143. ctx->ht = ht;
  144. }
  145. /**
  146. * vmw_validation_bo_reserve - Reserve buffer objects registered with a
  147. * validation context
  148. * @ctx: The validation context
  149. * @intr: Perform waits interruptible
  150. *
  151. * Return: Zero on success, -ERESTARTSYS when interrupted, negative error
  152. * code on failure
  153. */
  154. static inline int
  155. vmw_validation_bo_reserve(struct vmw_validation_context *ctx,
  156. bool intr)
  157. {
  158. return ttm_eu_reserve_buffers(&ctx->ticket, &ctx->bo_list, intr,
  159. NULL);
  160. }
  161. /**
  162. * vmw_validation_bo_backoff - Unreserve buffer objects registered with a
  163. * validation context
  164. * @ctx: The validation context
  165. *
  166. * This function unreserves the buffer objects previously reserved using
  167. * vmw_validation_bo_reserve. It's typically used as part of an error path
  168. */
  169. static inline void
  170. vmw_validation_bo_backoff(struct vmw_validation_context *ctx)
  171. {
  172. ttm_eu_backoff_reservation(&ctx->ticket, &ctx->bo_list);
  173. }
  174. /**
  175. * vmw_validation_bo_fence - Unreserve and fence buffer objects registered
  176. * with a validation context
  177. * @ctx: The validation context
  178. *
  179. * This function unreserves the buffer objects previously reserved using
  180. * vmw_validation_bo_reserve, and fences them with a fence object.
  181. */
  182. static inline void
  183. vmw_validation_bo_fence(struct vmw_validation_context *ctx,
  184. struct vmw_fence_obj *fence)
  185. {
  186. ttm_eu_fence_buffer_objects(&ctx->ticket, &ctx->bo_list,
  187. (void *) fence);
  188. }
  189. /**
  190. * vmw_validation_context_init - Initialize a validation context
  191. * @ctx: Pointer to the validation context to initialize
  192. *
  193. * This function initializes a validation context with @merge_dups set
  194. * to false
  195. */
  196. static inline void
  197. vmw_validation_context_init(struct vmw_validation_context *ctx)
  198. {
  199. memset(ctx, 0, sizeof(*ctx));
  200. INIT_LIST_HEAD(&ctx->resource_list);
  201. INIT_LIST_HEAD(&ctx->resource_ctx_list);
  202. INIT_LIST_HEAD(&ctx->bo_list);
  203. }
  204. /**
  205. * vmw_validation_align - Align a validation memory allocation
  206. * @val: The size to be aligned
  207. *
  208. * Returns: @val aligned to the granularity used by the validation memory
  209. * allocator.
  210. */
  211. static inline unsigned int vmw_validation_align(unsigned int val)
  212. {
  213. return ALIGN(val, sizeof(long));
  214. }
  215. int vmw_validation_add_bo(struct vmw_validation_context *ctx,
  216. struct vmw_buffer_object *vbo,
  217. bool as_mob, bool cpu_blit);
  218. int vmw_validation_bo_validate_single(struct ttm_buffer_object *bo,
  219. bool interruptible,
  220. bool validate_as_mob);
  221. int vmw_validation_bo_validate(struct vmw_validation_context *ctx, bool intr);
  222. void vmw_validation_unref_lists(struct vmw_validation_context *ctx);
  223. int vmw_validation_add_resource(struct vmw_validation_context *ctx,
  224. struct vmw_resource *res,
  225. size_t priv_size,
  226. void **p_node,
  227. bool *first_usage);
  228. void vmw_validation_drop_ht(struct vmw_validation_context *ctx);
  229. int vmw_validation_res_reserve(struct vmw_validation_context *ctx,
  230. bool intr);
  231. void vmw_validation_res_unreserve(struct vmw_validation_context *ctx,
  232. bool backoff);
  233. void vmw_validation_res_switch_backup(struct vmw_validation_context *ctx,
  234. void *val_private,
  235. struct vmw_buffer_object *vbo,
  236. unsigned long backup_offset);
  237. int vmw_validation_res_validate(struct vmw_validation_context *ctx, bool intr);
  238. int vmw_validation_prepare(struct vmw_validation_context *ctx,
  239. struct mutex *mutex, bool intr);
  240. void vmw_validation_revert(struct vmw_validation_context *ctx);
  241. void vmw_validation_done(struct vmw_validation_context *ctx,
  242. struct vmw_fence_obj *fence);
  243. void *vmw_validation_mem_alloc(struct vmw_validation_context *ctx,
  244. unsigned int size);
  245. int vmw_validation_preload_bo(struct vmw_validation_context *ctx);
  246. int vmw_validation_preload_res(struct vmw_validation_context *ctx,
  247. unsigned int size);
  248. #endif