vmwgfx_validation.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /* SPDX-License-Identifier: GPL-2.0 OR MIT */
  2. /**************************************************************************
  3. *
  4. * Copyright © 2018 - 2022 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/hashtable.h>
  32. #include <linux/ww_mutex.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_context - Per command submission validation context
  39. * @ht: Hash table used to find resource- or buffer object duplicates
  40. * @resource_list: List head for resource validation metadata
  41. * @resource_ctx_list: List head for resource validation metadata for
  42. * resources that need to be validated before those in @resource_list
  43. * @bo_list: List head for buffer objects
  44. * @page_list: List of pages used by the memory allocator
  45. * @ticket: Ticked used for ww mutex locking
  46. * @res_mutex: Pointer to mutex used for resource reserving
  47. * @merge_dups: Whether to merge metadata for duplicate resources or
  48. * buffer objects
  49. * @mem_size_left: Free memory left in the last page in @page_list
  50. * @page_address: Kernel virtual address of the last page in @page_list
  51. */
  52. struct vmw_validation_context {
  53. struct vmw_sw_context *sw_context;
  54. struct list_head resource_list;
  55. struct list_head resource_ctx_list;
  56. struct list_head bo_list;
  57. struct list_head page_list;
  58. struct ww_acquire_ctx ticket;
  59. struct mutex *res_mutex;
  60. unsigned int merge_dups;
  61. unsigned int mem_size_left;
  62. u8 *page_address;
  63. };
  64. struct vmw_bo;
  65. struct vmw_resource;
  66. struct vmw_fence_obj;
  67. #if 0
  68. /**
  69. * DECLARE_VAL_CONTEXT - Declare a validation context with initialization
  70. * @_name: The name of the variable
  71. * @_sw_context: Contains the hash table used to find dups or NULL if none
  72. * @_merge_dups: Whether to merge duplicate buffer object- or resource
  73. * entries. If set to true, ideally a hash table pointer should be supplied
  74. * as well unless the number of resources and buffer objects per validation
  75. * is known to be very small
  76. */
  77. #endif
  78. #define DECLARE_VAL_CONTEXT(_name, _sw_context, _merge_dups) \
  79. struct vmw_validation_context _name = \
  80. { .sw_context = _sw_context, \
  81. .resource_list = LIST_HEAD_INIT((_name).resource_list), \
  82. .resource_ctx_list = LIST_HEAD_INIT((_name).resource_ctx_list), \
  83. .bo_list = LIST_HEAD_INIT((_name).bo_list), \
  84. .page_list = LIST_HEAD_INIT((_name).page_list), \
  85. .res_mutex = NULL, \
  86. .merge_dups = _merge_dups, \
  87. .mem_size_left = 0, \
  88. }
  89. /**
  90. * vmw_validation_has_bos - return whether the validation context has
  91. * any buffer objects registered.
  92. *
  93. * @ctx: The validation context
  94. * Returns: Whether any buffer objects are registered
  95. */
  96. static inline bool
  97. vmw_validation_has_bos(struct vmw_validation_context *ctx)
  98. {
  99. return !list_empty(&ctx->bo_list);
  100. }
  101. /**
  102. * vmw_validation_bo_reserve - Reserve buffer objects registered with a
  103. * validation context
  104. * @ctx: The validation context
  105. * @intr: Perform waits interruptible
  106. *
  107. * Return: Zero on success, -ERESTARTSYS when interrupted, negative error
  108. * code on failure
  109. */
  110. static inline int
  111. vmw_validation_bo_reserve(struct vmw_validation_context *ctx,
  112. bool intr)
  113. {
  114. return ttm_eu_reserve_buffers(&ctx->ticket, &ctx->bo_list, intr,
  115. NULL);
  116. }
  117. /**
  118. * vmw_validation_bo_fence - Unreserve and fence buffer objects registered
  119. * with a validation context
  120. * @ctx: The validation context
  121. *
  122. * This function unreserves the buffer objects previously reserved using
  123. * vmw_validation_bo_reserve, and fences them with a fence object.
  124. */
  125. static inline void
  126. vmw_validation_bo_fence(struct vmw_validation_context *ctx,
  127. struct vmw_fence_obj *fence)
  128. {
  129. ttm_eu_fence_buffer_objects(&ctx->ticket, &ctx->bo_list,
  130. (void *) fence);
  131. }
  132. /**
  133. * vmw_validation_align - Align a validation memory allocation
  134. * @val: The size to be aligned
  135. *
  136. * Returns: @val aligned to the granularity used by the validation memory
  137. * allocator.
  138. */
  139. static inline unsigned int vmw_validation_align(unsigned int val)
  140. {
  141. return ALIGN(val, sizeof(long));
  142. }
  143. int vmw_validation_add_bo(struct vmw_validation_context *ctx,
  144. struct vmw_bo *vbo);
  145. int vmw_validation_bo_validate(struct vmw_validation_context *ctx, bool intr);
  146. void vmw_validation_unref_lists(struct vmw_validation_context *ctx);
  147. int vmw_validation_add_resource(struct vmw_validation_context *ctx,
  148. struct vmw_resource *res,
  149. size_t priv_size,
  150. u32 dirty,
  151. void **p_node,
  152. bool *first_usage);
  153. void vmw_validation_drop_ht(struct vmw_validation_context *ctx);
  154. int vmw_validation_res_reserve(struct vmw_validation_context *ctx,
  155. bool intr);
  156. void vmw_validation_res_unreserve(struct vmw_validation_context *ctx,
  157. bool backoff);
  158. void vmw_validation_res_switch_backup(struct vmw_validation_context *ctx,
  159. void *val_private,
  160. struct vmw_bo *vbo,
  161. unsigned long backup_offset);
  162. int vmw_validation_res_validate(struct vmw_validation_context *ctx, bool intr);
  163. int vmw_validation_prepare(struct vmw_validation_context *ctx,
  164. struct mutex *mutex, bool intr);
  165. void vmw_validation_revert(struct vmw_validation_context *ctx);
  166. void vmw_validation_done(struct vmw_validation_context *ctx,
  167. struct vmw_fence_obj *fence);
  168. void *vmw_validation_mem_alloc(struct vmw_validation_context *ctx,
  169. unsigned int size);
  170. int vmw_validation_preload_bo(struct vmw_validation_context *ctx);
  171. int vmw_validation_preload_res(struct vmw_validation_context *ctx,
  172. unsigned int size);
  173. void vmw_validation_res_set_dirty(struct vmw_validation_context *ctx,
  174. void *val_private, u32 dirty);
  175. void vmw_validation_bo_backoff(struct vmw_validation_context *ctx);
  176. #endif