vmwgfx_dmabuf.c 8.4 KB

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