vmwgfx_bo.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /* SPDX-License-Identifier: GPL-2.0 OR MIT */
  2. /**************************************************************************
  3. *
  4. * Copyright (c) 2023-2024 Broadcom. All Rights Reserved. The term
  5. * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
  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_BO_H
  29. #define VMWGFX_BO_H
  30. #include "device_include/svga_reg.h"
  31. #include <drm/ttm/ttm_bo.h>
  32. #include <drm/ttm/ttm_placement.h>
  33. #include <linux/rbtree_types.h>
  34. #include <linux/types.h>
  35. #include <linux/xarray.h>
  36. struct vmw_bo_dirty;
  37. struct vmw_fence_obj;
  38. struct vmw_private;
  39. struct vmw_resource;
  40. struct vmw_surface;
  41. enum vmw_bo_domain {
  42. VMW_BO_DOMAIN_SYS = BIT(0),
  43. VMW_BO_DOMAIN_WAITABLE_SYS = BIT(1),
  44. VMW_BO_DOMAIN_VRAM = BIT(2),
  45. VMW_BO_DOMAIN_GMR = BIT(3),
  46. VMW_BO_DOMAIN_MOB = BIT(4),
  47. };
  48. struct vmw_bo_params {
  49. u32 domain;
  50. u32 busy_domain;
  51. enum ttm_bo_type bo_type;
  52. bool pin;
  53. bool keep_resv;
  54. size_t size;
  55. struct dma_resv *resv;
  56. struct sg_table *sg;
  57. };
  58. /**
  59. * struct vmw_bo - TTM buffer object with vmwgfx additions
  60. * @tbo: The TTM buffer object
  61. * @placement: The preferred placement for this buffer object
  62. * @places: The chosen places for the preferred placement.
  63. * @busy_places: Chosen busy places for the preferred placement
  64. * @map: Kmap object for semi-persistent mappings
  65. * @res_tree: RB tree of resources using this buffer object as a backing MOB
  66. * @res_prios: Eviction priority counts for attached resources
  67. * @map_count: The number of currently active maps. Will differ from the
  68. * cpu_writers because it includes kernel maps.
  69. * @cpu_writers: Number of synccpu write grabs. Protected by reservation when
  70. * increased. May be decreased without reservation.
  71. * @dx_query_ctx: DX context if this buffer object is used as a DX query MOB
  72. * @dirty: structure for user-space dirty-tracking
  73. */
  74. struct vmw_bo {
  75. struct ttm_buffer_object tbo;
  76. struct ttm_placement placement;
  77. struct ttm_place places[5];
  78. /* Protected by reservation */
  79. struct ttm_bo_kmap_obj map;
  80. struct rb_root res_tree;
  81. u32 res_prios[TTM_MAX_BO_PRIORITY];
  82. struct xarray detached_resources;
  83. atomic_t map_count;
  84. atomic_t cpu_writers;
  85. /* Not ref-counted. Protected by binding_mutex */
  86. struct vmw_resource *dx_query_ctx;
  87. struct vmw_bo_dirty *dirty;
  88. bool is_dumb;
  89. struct vmw_surface *dumb_surface;
  90. };
  91. void vmw_bo_placement_set(struct vmw_bo *bo, u32 domain, u32 busy_domain);
  92. void vmw_bo_placement_set_default_accelerated(struct vmw_bo *bo);
  93. int vmw_bo_create(struct vmw_private *dev_priv,
  94. struct vmw_bo_params *params,
  95. struct vmw_bo **p_bo);
  96. int vmw_bo_unref_ioctl(struct drm_device *dev, void *data,
  97. struct drm_file *file_priv);
  98. int vmw_bo_pin_in_vram(struct vmw_private *dev_priv,
  99. struct vmw_bo *buf,
  100. bool interruptible);
  101. int vmw_bo_pin_in_vram_or_gmr(struct vmw_private *dev_priv,
  102. struct vmw_bo *buf,
  103. bool interruptible);
  104. int vmw_bo_pin_in_start_of_vram(struct vmw_private *vmw_priv,
  105. struct vmw_bo *bo,
  106. bool interruptible);
  107. void vmw_bo_pin_reserved(struct vmw_bo *bo, bool pin);
  108. int vmw_bo_unpin(struct vmw_private *vmw_priv,
  109. struct vmw_bo *bo,
  110. bool interruptible);
  111. void vmw_bo_get_guest_ptr(const struct ttm_buffer_object *buf,
  112. SVGAGuestPtr *ptr);
  113. int vmw_user_bo_synccpu_ioctl(struct drm_device *dev, void *data,
  114. struct drm_file *file_priv);
  115. void vmw_bo_fence_single(struct ttm_buffer_object *bo,
  116. struct vmw_fence_obj *fence);
  117. void *vmw_bo_map_and_cache(struct vmw_bo *vbo);
  118. void *vmw_bo_map_and_cache_size(struct vmw_bo *vbo, size_t size);
  119. void vmw_bo_unmap(struct vmw_bo *vbo);
  120. void vmw_bo_move_notify(struct ttm_buffer_object *bo,
  121. struct ttm_resource *mem);
  122. void vmw_bo_swap_notify(struct ttm_buffer_object *bo);
  123. int vmw_bo_add_detached_resource(struct vmw_bo *vbo, struct vmw_resource *res);
  124. void vmw_bo_del_detached_resource(struct vmw_bo *vbo, struct vmw_resource *res);
  125. struct vmw_surface *vmw_bo_surface(struct vmw_bo *vbo);
  126. int vmw_user_bo_lookup(struct drm_file *filp,
  127. u32 handle,
  128. struct vmw_bo **out);
  129. /**
  130. * vmw_bo_adjust_prio - Adjust the buffer object eviction priority
  131. * according to attached resources
  132. * @vbo: The struct vmw_bo
  133. */
  134. static inline void vmw_bo_prio_adjust(struct vmw_bo *vbo)
  135. {
  136. int i = ARRAY_SIZE(vbo->res_prios);
  137. while (i--) {
  138. if (vbo->res_prios[i]) {
  139. vbo->tbo.priority = i;
  140. return;
  141. }
  142. }
  143. vbo->tbo.priority = 3;
  144. }
  145. /**
  146. * vmw_bo_prio_add - Notify a buffer object of a newly attached resource
  147. * eviction priority
  148. * @vbo: The struct vmw_bo
  149. * @prio: The resource priority
  150. *
  151. * After being notified, the code assigns the highest resource eviction priority
  152. * to the backing buffer object (mob).
  153. */
  154. static inline void vmw_bo_prio_add(struct vmw_bo *vbo, int prio)
  155. {
  156. if (vbo->res_prios[prio]++ == 0)
  157. vmw_bo_prio_adjust(vbo);
  158. }
  159. /**
  160. * vmw_bo_used_prio_del - Notify a buffer object of a resource with a certain
  161. * priority being removed
  162. * @vbo: The struct vmw_bo
  163. * @prio: The resource priority
  164. *
  165. * After being notified, the code assigns the highest resource eviction priority
  166. * to the backing buffer object (mob).
  167. */
  168. static inline void vmw_bo_prio_del(struct vmw_bo *vbo, int prio)
  169. {
  170. if (--vbo->res_prios[prio] == 0)
  171. vmw_bo_prio_adjust(vbo);
  172. }
  173. static inline void vmw_bo_unreference(struct vmw_bo **buf)
  174. {
  175. struct vmw_bo *tmp_buf = *buf;
  176. *buf = NULL;
  177. if (tmp_buf)
  178. drm_gem_object_put(&tmp_buf->tbo.base);
  179. }
  180. static inline struct vmw_bo *vmw_bo_reference(struct vmw_bo *buf)
  181. {
  182. drm_gem_object_get(&buf->tbo.base);
  183. return buf;
  184. }
  185. static inline struct vmw_bo *vmw_user_bo_ref(struct vmw_bo *vbo)
  186. {
  187. drm_gem_object_get(&vbo->tbo.base);
  188. return vbo;
  189. }
  190. static inline void vmw_user_bo_unref(struct vmw_bo **buf)
  191. {
  192. struct vmw_bo *tmp_buf = *buf;
  193. *buf = NULL;
  194. if (tmp_buf)
  195. drm_gem_object_put(&tmp_buf->tbo.base);
  196. }
  197. static inline struct vmw_bo *to_vmw_bo(struct drm_gem_object *gobj)
  198. {
  199. return container_of((gobj), struct vmw_bo, tbo.base);
  200. }
  201. s32 vmw_bo_mobid(struct vmw_bo *vbo);
  202. #endif // VMWGFX_BO_H