vmwgfx_dmabuf.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /**************************************************************************
  2. *
  3. * Copyright © 2011-2015 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. #include <drm/ttm/ttm_placement.h>
  28. #include <drm/drmP.h>
  29. #include "vmwgfx_drv.h"
  30. /**
  31. * vmw_dmabuf_pin_in_placement - Validate a buffer to placement.
  32. *
  33. * @dev_priv: Driver private.
  34. * @buf: DMA buffer to move.
  35. * @placement: The placement to pin it.
  36. * @interruptible: Use interruptible wait.
  37. *
  38. * Returns
  39. * -ERESTARTSYS if interrupted by a signal.
  40. */
  41. int vmw_dmabuf_pin_in_placement(struct vmw_private *dev_priv,
  42. struct vmw_dma_buffer *buf,
  43. struct ttm_placement *placement,
  44. bool interruptible)
  45. {
  46. struct ttm_buffer_object *bo = &buf->base;
  47. int ret;
  48. ret = ttm_write_lock(&dev_priv->reservation_sem, interruptible);
  49. if (unlikely(ret != 0))
  50. return ret;
  51. vmw_execbuf_release_pinned_bo(dev_priv);
  52. ret = ttm_bo_reserve(bo, interruptible, false, false, NULL);
  53. if (unlikely(ret != 0))
  54. goto err;
  55. ret = ttm_bo_validate(bo, placement, interruptible, false);
  56. if (!ret)
  57. vmw_bo_pin_reserved(buf, true);
  58. ttm_bo_unreserve(bo);
  59. err:
  60. ttm_write_unlock(&dev_priv->reservation_sem);
  61. return ret;
  62. }
  63. /**
  64. * vmw_dmabuf_pin_in_vram_or_gmr - Move a buffer to vram or gmr.
  65. *
  66. * This function takes the reservation_sem in write mode.
  67. * Flushes and unpins the query bo to avoid failures.
  68. *
  69. * @dev_priv: Driver private.
  70. * @buf: DMA buffer to move.
  71. * @pin: Pin buffer if true.
  72. * @interruptible: Use interruptible wait.
  73. *
  74. * Returns
  75. * -ERESTARTSYS if interrupted by a signal.
  76. */
  77. int vmw_dmabuf_pin_in_vram_or_gmr(struct vmw_private *dev_priv,
  78. struct vmw_dma_buffer *buf,
  79. bool interruptible)
  80. {
  81. struct ttm_buffer_object *bo = &buf->base;
  82. int ret;
  83. ret = ttm_write_lock(&dev_priv->reservation_sem, interruptible);
  84. if (unlikely(ret != 0))
  85. return ret;
  86. vmw_execbuf_release_pinned_bo(dev_priv);
  87. ret = ttm_bo_reserve(bo, interruptible, false, false, NULL);
  88. if (unlikely(ret != 0))
  89. goto err;
  90. ret = ttm_bo_validate(bo, &vmw_vram_gmr_placement, interruptible,
  91. false);
  92. if (likely(ret == 0) || ret == -ERESTARTSYS)
  93. goto out_unreserve;
  94. ret = ttm_bo_validate(bo, &vmw_vram_placement, interruptible, false);
  95. out_unreserve:
  96. if (!ret)
  97. vmw_bo_pin_reserved(buf, true);
  98. ttm_bo_unreserve(bo);
  99. err:
  100. ttm_write_unlock(&dev_priv->reservation_sem);
  101. return ret;
  102. }
  103. /**
  104. * vmw_dmabuf_pin_in_vram - Move a buffer to vram.
  105. *
  106. * This function takes the reservation_sem in write mode.
  107. * Flushes and unpins the query bo to avoid failures.
  108. *
  109. * @dev_priv: Driver private.
  110. * @buf: DMA buffer to move.
  111. * @interruptible: Use interruptible wait.
  112. *
  113. * Returns
  114. * -ERESTARTSYS if interrupted by a signal.
  115. */
  116. int vmw_dmabuf_pin_in_vram(struct vmw_private *dev_priv,
  117. struct vmw_dma_buffer *buf,
  118. bool interruptible)
  119. {
  120. return vmw_dmabuf_pin_in_placement(dev_priv, buf, &vmw_vram_placement,
  121. interruptible);
  122. }
  123. /**
  124. * vmw_dmabuf_pin_in_start_of_vram - Move a buffer to start of vram.
  125. *
  126. * This function takes the reservation_sem in write mode.
  127. * Flushes and unpins the query bo to avoid failures.
  128. *
  129. * @dev_priv: Driver private.
  130. * @buf: DMA buffer to pin.
  131. * @interruptible: Use interruptible wait.
  132. *
  133. * Returns
  134. * -ERESTARTSYS if interrupted by a signal.
  135. */
  136. int vmw_dmabuf_pin_in_start_of_vram(struct vmw_private *dev_priv,
  137. struct vmw_dma_buffer *buf,
  138. bool interruptible)
  139. {
  140. struct ttm_buffer_object *bo = &buf->base;
  141. struct ttm_placement placement;
  142. struct ttm_place place;
  143. int ret = 0;
  144. place = vmw_vram_placement.placement[0];
  145. place.lpfn = bo->num_pages;
  146. placement.num_placement = 1;
  147. placement.placement = &place;
  148. placement.num_busy_placement = 1;
  149. placement.busy_placement = &place;
  150. ret = ttm_write_lock(&dev_priv->reservation_sem, interruptible);
  151. if (unlikely(ret != 0))
  152. return ret;
  153. vmw_execbuf_release_pinned_bo(dev_priv);
  154. ret = ttm_bo_reserve(bo, interruptible, false, false, NULL);
  155. if (unlikely(ret != 0))
  156. goto err_unlock;
  157. /*
  158. * Is this buffer already in vram but not at the start of it?
  159. * In that case, evict it first because TTM isn't good at handling
  160. * that situation.
  161. */
  162. if (bo->mem.mem_type == TTM_PL_VRAM &&
  163. bo->mem.start < bo->num_pages &&
  164. bo->mem.start > 0)
  165. (void) ttm_bo_validate(bo, &vmw_sys_placement, false, false);
  166. ret = ttm_bo_validate(bo, &placement, interruptible, false);
  167. /* For some reason we didn't end up at the start of vram */
  168. WARN_ON(ret == 0 && bo->offset != 0);
  169. if (!ret)
  170. vmw_bo_pin_reserved(buf, true);
  171. ttm_bo_unreserve(bo);
  172. err_unlock:
  173. ttm_write_unlock(&dev_priv->reservation_sem);
  174. return ret;
  175. }
  176. /**
  177. * vmw_dmabuf_unpin - Unpin the buffer given buffer, does not move the buffer.
  178. *
  179. * This function takes the reservation_sem in write mode.
  180. *
  181. * @dev_priv: Driver private.
  182. * @buf: DMA buffer to unpin.
  183. * @interruptible: Use interruptible wait.
  184. *
  185. * Returns
  186. * -ERESTARTSYS if interrupted by a signal.
  187. */
  188. int vmw_dmabuf_unpin(struct vmw_private *dev_priv,
  189. struct vmw_dma_buffer *buf,
  190. bool interruptible)
  191. {
  192. struct ttm_buffer_object *bo = &buf->base;
  193. int ret;
  194. ret = ttm_read_lock(&dev_priv->reservation_sem, interruptible);
  195. if (unlikely(ret != 0))
  196. return ret;
  197. ret = ttm_bo_reserve(bo, interruptible, false, false, NULL);
  198. if (unlikely(ret != 0))
  199. goto err;
  200. vmw_bo_pin_reserved(buf, false);
  201. ttm_bo_unreserve(bo);
  202. err:
  203. ttm_read_unlock(&dev_priv->reservation_sem);
  204. return ret;
  205. }
  206. /**
  207. * vmw_bo_get_guest_ptr - Get the guest ptr representing the current placement
  208. * of a buffer.
  209. *
  210. * @bo: Pointer to a struct ttm_buffer_object. Must be pinned or reserved.
  211. * @ptr: SVGAGuestPtr returning the result.
  212. */
  213. void vmw_bo_get_guest_ptr(const struct ttm_buffer_object *bo,
  214. SVGAGuestPtr *ptr)
  215. {
  216. if (bo->mem.mem_type == TTM_PL_VRAM) {
  217. ptr->gmrId = SVGA_GMR_FRAMEBUFFER;
  218. ptr->offset = bo->offset;
  219. } else {
  220. ptr->gmrId = bo->mem.start;
  221. ptr->offset = 0;
  222. }
  223. }
  224. /**
  225. * vmw_bo_pin_reserved - Pin or unpin a buffer object without moving it.
  226. *
  227. * @vbo: The buffer object. Must be reserved.
  228. * @pin: Whether to pin or unpin.
  229. *
  230. */
  231. void vmw_bo_pin_reserved(struct vmw_dma_buffer *vbo, bool pin)
  232. {
  233. struct ttm_place pl;
  234. struct ttm_placement placement;
  235. struct ttm_buffer_object *bo = &vbo->base;
  236. uint32_t old_mem_type = bo->mem.mem_type;
  237. int ret;
  238. lockdep_assert_held(&bo->resv->lock.base);
  239. if (pin) {
  240. if (vbo->pin_count++ > 0)
  241. return;
  242. } else {
  243. WARN_ON(vbo->pin_count <= 0);
  244. if (--vbo->pin_count > 0)
  245. return;
  246. }
  247. pl.fpfn = 0;
  248. pl.lpfn = 0;
  249. pl.flags = TTM_PL_FLAG_VRAM | VMW_PL_FLAG_GMR | VMW_PL_FLAG_MOB
  250. | TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
  251. if (pin)
  252. pl.flags |= TTM_PL_FLAG_NO_EVICT;
  253. memset(&placement, 0, sizeof(placement));
  254. placement.num_placement = 1;
  255. placement.placement = &pl;
  256. ret = ttm_bo_validate(bo, &placement, false, true);
  257. BUG_ON(ret != 0 || bo->mem.mem_type != old_mem_type);
  258. }