vmwgfx_validation.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 <linux/list.h>
  31. #include <linux/ww_mutex.h>
  32. #include <drm/drm_hashtab.h>
  33. #include <drm/ttm/ttm_execbuf_util.h>
  34. #define VMW_RES_DIRTY_NONE 0
  35. #define VMW_RES_DIRTY_SET BIT(0)
  36. #define VMW_RES_DIRTY_CLEAR BIT(1)
  37. /**
  38. * struct vmw_validation_mem - Custom interface to provide memory reservations
  39. * for the validation code.
  40. * @reserve_mem: Callback to reserve memory
  41. * @unreserve_mem: Callback to unreserve memory
  42. * @gran: Reservation granularity. Contains a hint how much memory should
  43. * be reserved in each call to @reserve_mem(). A slow implementation may want
  44. * reservation to be done in large batches.
  45. */
  46. struct vmw_validation_mem {
  47. int (*reserve_mem)(struct vmw_validation_mem *m, size_t size);
  48. void (*unreserve_mem)(struct vmw_validation_mem *m, size_t size);
  49. size_t gran;
  50. };
  51. /**
  52. * struct vmw_validation_context - Per command submission validation context
  53. * @ht: Hash table used to find resource- or buffer object duplicates
  54. * @resource_list: List head for resource validation metadata
  55. * @resource_ctx_list: List head for resource validation metadata for
  56. * resources that need to be validated before those in @resource_list
  57. * @bo_list: List head for buffer objects
  58. * @page_list: List of pages used by the memory allocator
  59. * @ticket: Ticked used for ww mutex locking
  60. * @res_mutex: Pointer to mutex used for resource reserving
  61. * @merge_dups: Whether to merge metadata for duplicate resources or
  62. * buffer objects
  63. * @mem_size_left: Free memory left in the last page in @page_list
  64. * @page_address: Kernel virtual address of the last page in @page_list
  65. * @vm: A pointer to the memory reservation interface or NULL if no
  66. * memory reservation is needed.
  67. * @vm_size_left: Amount of reserved memory that so far has not been allocated.
  68. * @total_mem: Amount of reserved memory.
  69. */
  70. struct vmw_validation_context {
  71. struct drm_open_hash *ht;
  72. struct list_head resource_list;
  73. struct list_head resource_ctx_list;
  74. struct list_head bo_list;
  75. struct list_head page_list;
  76. struct ww_acquire_ctx ticket;
  77. struct mutex *res_mutex;
  78. unsigned int merge_dups;
  79. unsigned int mem_size_left;
  80. u8 *page_address;
  81. struct vmw_validation_mem *vm;
  82. size_t vm_size_left;
  83. size_t total_mem;
  84. };
  85. struct vmw_buffer_object;
  86. struct vmw_resource;
  87. struct vmw_fence_obj;
  88. #if 0
  89. /**
  90. * DECLARE_VAL_CONTEXT - Declare a validation context with initialization
  91. * @_name: The name of the variable
  92. * @_ht: The hash table used to find dups or NULL if none
  93. * @_merge_dups: Whether to merge duplicate buffer object- or resource
  94. * entries. If set to true, ideally a hash table pointer should be supplied
  95. * as well unless the number of resources and buffer objects per validation
  96. * is known to be very small
  97. */
  98. #endif
  99. #define DECLARE_VAL_CONTEXT(_name, _ht, _merge_dups) \
  100. struct vmw_validation_context _name = \
  101. { .ht = _ht, \
  102. .resource_list = LIST_HEAD_INIT((_name).resource_list), \
  103. .resource_ctx_list = LIST_HEAD_INIT((_name).resource_ctx_list), \
  104. .bo_list = LIST_HEAD_INIT((_name).bo_list), \
  105. .page_list = LIST_HEAD_INIT((_name).page_list), \
  106. .res_mutex = NULL, \
  107. .merge_dups = _merge_dups, \
  108. .mem_size_left = 0, \
  109. }
  110. /**
  111. * vmw_validation_has_bos - return whether the validation context has
  112. * any buffer objects registered.
  113. *
  114. * @ctx: The validation context
  115. * Returns: Whether any buffer objects are registered
  116. */
  117. static inline bool
  118. vmw_validation_has_bos(struct vmw_validation_context *ctx)
  119. {
  120. return !list_empty(&ctx->bo_list);
  121. }
  122. /**
  123. * vmw_validation_set_val_mem - Register a validation mem object for
  124. * validation memory reservation
  125. * @ctx: The validation context
  126. * @vm: Pointer to a struct vmw_validation_mem
  127. *
  128. * Must be set before the first attempt to allocate validation memory.
  129. */
  130. static inline void
  131. vmw_validation_set_val_mem(struct vmw_validation_context *ctx,
  132. struct vmw_validation_mem *vm)
  133. {
  134. ctx->vm = vm;
  135. }
  136. /**
  137. * vmw_validation_set_ht - Register a hash table for duplicate finding
  138. * @ctx: The validation context
  139. * @ht: Pointer to a hash table to use for duplicate finding
  140. * This function is intended to be used if the hash table wasn't
  141. * available at validation context declaration time
  142. */
  143. static inline void vmw_validation_set_ht(struct vmw_validation_context *ctx,
  144. struct drm_open_hash *ht)
  145. {
  146. ctx->ht = ht;
  147. }
  148. /**
  149. * vmw_validation_bo_reserve - Reserve buffer objects registered with a
  150. * validation context
  151. * @ctx: The validation context
  152. * @intr: Perform waits interruptible
  153. *
  154. * Return: Zero on success, -ERESTARTSYS when interrupted, negative error
  155. * code on failure
  156. */
  157. static inline int
  158. vmw_validation_bo_reserve(struct vmw_validation_context *ctx,
  159. bool intr)
  160. {
  161. return ttm_eu_reserve_buffers(&ctx->ticket, &ctx->bo_list, intr,
  162. NULL);
  163. }
  164. /**
  165. * vmw_validation_bo_fence - Unreserve and fence buffer objects registered
  166. * with a validation context
  167. * @ctx: The validation context
  168. *
  169. * This function unreserves the buffer objects previously reserved using
  170. * vmw_validation_bo_reserve, and fences them with a fence object.
  171. */
  172. static inline void
  173. vmw_validation_bo_fence(struct vmw_validation_context *ctx,
  174. struct vmw_fence_obj *fence)
  175. {
  176. ttm_eu_fence_buffer_objects(&ctx->ticket, &ctx->bo_list,
  177. (void *) fence);
  178. }
  179. /**
  180. * vmw_validation_context_init - Initialize a validation context
  181. * @ctx: Pointer to the validation context to initialize
  182. *
  183. * This function initializes a validation context with @merge_dups set
  184. * to false
  185. */
  186. static inline void
  187. vmw_validation_context_init(struct vmw_validation_context *ctx)
  188. {
  189. memset(ctx, 0, sizeof(*ctx));
  190. INIT_LIST_HEAD(&ctx->resource_list);
  191. INIT_LIST_HEAD(&ctx->resource_ctx_list);
  192. INIT_LIST_HEAD(&ctx->bo_list);
  193. }
  194. /**
  195. * vmw_validation_align - Align a validation memory allocation
  196. * @val: The size to be aligned
  197. *
  198. * Returns: @val aligned to the granularity used by the validation memory
  199. * allocator.
  200. */
  201. static inline unsigned int vmw_validation_align(unsigned int val)
  202. {
  203. return ALIGN(val, sizeof(long));
  204. }
  205. int vmw_validation_add_bo(struct vmw_validation_context *ctx,
  206. struct vmw_buffer_object *vbo,
  207. bool as_mob, bool cpu_blit);
  208. int vmw_validation_bo_validate_single(struct ttm_buffer_object *bo,
  209. bool interruptible,
  210. bool validate_as_mob);
  211. int vmw_validation_bo_validate(struct vmw_validation_context *ctx, bool intr);
  212. void vmw_validation_unref_lists(struct vmw_validation_context *ctx);
  213. int vmw_validation_add_resource(struct vmw_validation_context *ctx,
  214. struct vmw_resource *res,
  215. size_t priv_size,
  216. u32 dirty,
  217. void **p_node,
  218. bool *first_usage);
  219. void vmw_validation_drop_ht(struct vmw_validation_context *ctx);
  220. int vmw_validation_res_reserve(struct vmw_validation_context *ctx,
  221. bool intr);
  222. void vmw_validation_res_unreserve(struct vmw_validation_context *ctx,
  223. bool backoff);
  224. void vmw_validation_res_switch_backup(struct vmw_validation_context *ctx,
  225. void *val_private,
  226. struct vmw_buffer_object *vbo,
  227. unsigned long backup_offset);
  228. int vmw_validation_res_validate(struct vmw_validation_context *ctx, bool intr);
  229. int vmw_validation_prepare(struct vmw_validation_context *ctx,
  230. struct mutex *mutex, bool intr);
  231. void vmw_validation_revert(struct vmw_validation_context *ctx);
  232. void vmw_validation_done(struct vmw_validation_context *ctx,
  233. struct vmw_fence_obj *fence);
  234. void *vmw_validation_mem_alloc(struct vmw_validation_context *ctx,
  235. unsigned int size);
  236. int vmw_validation_preload_bo(struct vmw_validation_context *ctx);
  237. int vmw_validation_preload_res(struct vmw_validation_context *ctx,
  238. unsigned int size);
  239. void vmw_validation_res_set_dirty(struct vmw_validation_context *ctx,
  240. void *val_private, u32 dirty);
  241. void vmw_validation_bo_backoff(struct vmw_validation_context *ctx);
  242. #endif