vmwgfx_dmabuf.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /**************************************************************************
  2. *
  3. * Copyright © 2011 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_to_placement - Validate a buffer to placement.
  32. *
  33. * @dev_priv: Driver private.
  34. * @buf: DMA buffer to move.
  35. * @pin: Pin buffer if true.
  36. * @interruptible: Use interruptible wait.
  37. *
  38. * May only be called by the current master since it assumes that the
  39. * master lock is the current master's lock.
  40. * This function takes the master's lock in write mode.
  41. * Flushes and unpins the query bo to avoid failures.
  42. *
  43. * Returns
  44. * -ERESTARTSYS if interrupted by a signal.
  45. */
  46. int vmw_dmabuf_to_placement(struct vmw_private *dev_priv,
  47. struct vmw_dma_buffer *buf,
  48. struct ttm_placement *placement,
  49. bool interruptible)
  50. {
  51. struct ttm_buffer_object *bo = &buf->base;
  52. int ret;
  53. ret = ttm_write_lock(&dev_priv->reservation_sem, interruptible);
  54. if (unlikely(ret != 0))
  55. return ret;
  56. vmw_execbuf_release_pinned_bo(dev_priv);
  57. ret = ttm_bo_reserve(bo, interruptible, false, false, NULL);
  58. if (unlikely(ret != 0))
  59. goto err;
  60. ret = ttm_bo_validate(bo, placement, interruptible, false);
  61. ttm_bo_unreserve(bo);
  62. err:
  63. ttm_write_unlock(&dev_priv->reservation_sem);
  64. return ret;
  65. }
  66. /**
  67. * vmw_dmabuf_to_vram_or_gmr - Move a buffer to vram or gmr.
  68. *
  69. * May only be called by the current master since it assumes that the
  70. * master lock is the current master's lock.
  71. * This function takes the master's lock in write mode.
  72. * Flushes and unpins the query bo if @pin == true to avoid failures.
  73. *
  74. * @dev_priv: Driver private.
  75. * @buf: DMA buffer to move.
  76. * @pin: Pin buffer if true.
  77. * @interruptible: Use interruptible wait.
  78. *
  79. * Returns
  80. * -ERESTARTSYS if interrupted by a signal.
  81. */
  82. int vmw_dmabuf_to_vram_or_gmr(struct vmw_private *dev_priv,
  83. struct vmw_dma_buffer *buf,
  84. bool pin, bool interruptible)
  85. {
  86. struct ttm_buffer_object *bo = &buf->base;
  87. struct ttm_placement *placement;
  88. int ret;
  89. ret = ttm_write_lock(&dev_priv->reservation_sem, interruptible);
  90. if (unlikely(ret != 0))
  91. return ret;
  92. if (pin)
  93. vmw_execbuf_release_pinned_bo(dev_priv);
  94. ret = ttm_bo_reserve(bo, interruptible, false, false, NULL);
  95. if (unlikely(ret != 0))
  96. goto err;
  97. /**
  98. * Put BO in VRAM if there is space, otherwise as a GMR.
  99. * If there is no space in VRAM and GMR ids are all used up,
  100. * start evicting GMRs to make room. If the DMA buffer can't be
  101. * used as a GMR, this will return -ENOMEM.
  102. */
  103. if (pin)
  104. placement = &vmw_vram_gmr_ne_placement;
  105. else
  106. placement = &vmw_vram_gmr_placement;
  107. ret = ttm_bo_validate(bo, placement, interruptible, false);
  108. if (likely(ret == 0) || ret == -ERESTARTSYS)
  109. goto err_unreserve;
  110. /**
  111. * If that failed, try VRAM again, this time evicting
  112. * previous contents.
  113. */
  114. if (pin)
  115. placement = &vmw_vram_ne_placement;
  116. else
  117. placement = &vmw_vram_placement;
  118. ret = ttm_bo_validate(bo, placement, interruptible, false);
  119. err_unreserve:
  120. ttm_bo_unreserve(bo);
  121. err:
  122. ttm_write_unlock(&dev_priv->reservation_sem);
  123. return ret;
  124. }
  125. /**
  126. * vmw_dmabuf_to_vram - Move a buffer to vram.
  127. *
  128. * May only be called by the current master since it assumes that the
  129. * master lock is the current master's lock.
  130. * This function takes the master's lock in write mode.
  131. *
  132. * @dev_priv: Driver private.
  133. * @buf: DMA buffer to move.
  134. * @pin: Pin buffer in vram if true.
  135. * @interruptible: Use interruptible wait.
  136. *
  137. * Returns
  138. * -ERESTARTSYS if interrupted by a signal.
  139. */
  140. int vmw_dmabuf_to_vram(struct vmw_private *dev_priv,
  141. struct vmw_dma_buffer *buf,
  142. bool pin, bool interruptible)
  143. {
  144. struct ttm_placement *placement;
  145. if (pin)
  146. placement = &vmw_vram_ne_placement;
  147. else
  148. placement = &vmw_vram_placement;
  149. return vmw_dmabuf_to_placement(dev_priv, buf,
  150. placement,
  151. interruptible);
  152. }
  153. /**
  154. * vmw_dmabuf_to_start_of_vram - Move a buffer to start of vram.
  155. *
  156. * May only be called by the current master since it assumes that the
  157. * master lock is the current master's lock.
  158. * This function takes the master's lock in write mode.
  159. * Flushes and unpins the query bo if @pin == true to avoid failures.
  160. *
  161. * @dev_priv: Driver private.
  162. * @buf: DMA buffer to move.
  163. * @pin: Pin buffer in vram if true.
  164. * @interruptible: Use interruptible wait.
  165. *
  166. * Returns
  167. * -ERESTARTSYS if interrupted by a signal.
  168. */
  169. int vmw_dmabuf_to_start_of_vram(struct vmw_private *dev_priv,
  170. struct vmw_dma_buffer *buf,
  171. bool pin, bool interruptible)
  172. {
  173. struct ttm_buffer_object *bo = &buf->base;
  174. struct ttm_placement placement;
  175. struct ttm_place place;
  176. int ret = 0;
  177. if (pin)
  178. place = vmw_vram_ne_placement.placement[0];
  179. else
  180. place = vmw_vram_placement.placement[0];
  181. place.lpfn = bo->num_pages;
  182. placement.num_placement = 1;
  183. placement.placement = &place;
  184. placement.num_busy_placement = 1;
  185. placement.busy_placement = &place;
  186. ret = ttm_write_lock(&dev_priv->reservation_sem, interruptible);
  187. if (unlikely(ret != 0))
  188. return ret;
  189. if (pin)
  190. vmw_execbuf_release_pinned_bo(dev_priv);
  191. ret = ttm_bo_reserve(bo, interruptible, false, false, NULL);
  192. if (unlikely(ret != 0))
  193. goto err_unlock;
  194. /* Is this buffer already in vram but not at the start of it? */
  195. if (bo->mem.mem_type == TTM_PL_VRAM &&
  196. bo->mem.start < bo->num_pages &&
  197. bo->mem.start > 0)
  198. (void) ttm_bo_validate(bo, &vmw_sys_placement, false, false);
  199. ret = ttm_bo_validate(bo, &placement, interruptible, false);
  200. /* For some reason we didn't up at the start of vram */
  201. WARN_ON(ret == 0 && bo->offset != 0);
  202. ttm_bo_unreserve(bo);
  203. err_unlock:
  204. ttm_write_unlock(&dev_priv->reservation_sem);
  205. return ret;
  206. }
  207. /**
  208. * vmw_dmabuf_upin - Unpin the buffer given buffer, does not move the buffer.
  209. *
  210. * May only be called by the current master since it assumes that the
  211. * master lock is the current master's lock.
  212. * This function takes the master's lock in write mode.
  213. *
  214. * @dev_priv: Driver private.
  215. * @buf: DMA buffer to unpin.
  216. * @interruptible: Use interruptible wait.
  217. *
  218. * Returns
  219. * -ERESTARTSYS if interrupted by a signal.
  220. */
  221. int vmw_dmabuf_unpin(struct vmw_private *dev_priv,
  222. struct vmw_dma_buffer *buf,
  223. bool interruptible)
  224. {
  225. /*
  226. * We could in theory early out if the buffer is
  227. * unpinned but we need to lock and reserve the buffer
  228. * anyways so we don't gain much by that.
  229. */
  230. return vmw_dmabuf_to_placement(dev_priv, buf,
  231. &vmw_evictable_placement,
  232. interruptible);
  233. }
  234. /**
  235. * vmw_bo_get_guest_ptr - Get the guest ptr representing the current placement
  236. * of a buffer.
  237. *
  238. * @bo: Pointer to a struct ttm_buffer_object. Must be pinned or reserved.
  239. * @ptr: SVGAGuestPtr returning the result.
  240. */
  241. void vmw_bo_get_guest_ptr(const struct ttm_buffer_object *bo,
  242. SVGAGuestPtr *ptr)
  243. {
  244. if (bo->mem.mem_type == TTM_PL_VRAM) {
  245. ptr->gmrId = SVGA_GMR_FRAMEBUFFER;
  246. ptr->offset = bo->offset;
  247. } else {
  248. ptr->gmrId = bo->mem.start;
  249. ptr->offset = 0;
  250. }
  251. }
  252. /**
  253. * vmw_bo_pin - Pin or unpin a buffer object without moving it.
  254. *
  255. * @bo: The buffer object. Must be reserved.
  256. * @pin: Whether to pin or unpin.
  257. *
  258. */
  259. void vmw_bo_pin(struct ttm_buffer_object *bo, bool pin)
  260. {
  261. struct ttm_place pl;
  262. struct ttm_placement placement;
  263. uint32_t old_mem_type = bo->mem.mem_type;
  264. int ret;
  265. lockdep_assert_held(&bo->resv->lock.base);
  266. pl.fpfn = 0;
  267. pl.lpfn = 0;
  268. pl.flags = TTM_PL_FLAG_VRAM | VMW_PL_FLAG_GMR | VMW_PL_FLAG_MOB
  269. | TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
  270. if (pin)
  271. pl.flags |= TTM_PL_FLAG_NO_EVICT;
  272. memset(&placement, 0, sizeof(placement));
  273. placement.num_placement = 1;
  274. placement.placement = &pl;
  275. ret = ttm_bo_validate(bo, &placement, false, true);
  276. BUG_ON(ret != 0 || bo->mem.mem_type != old_mem_type);
  277. }