vmwgfx_buffer.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /**************************************************************************
  2. *
  3. * Copyright © 2009 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 "vmwgfx_drv.h"
  28. #include "ttm/ttm_bo_driver.h"
  29. #include "ttm/ttm_placement.h"
  30. static uint32_t vram_placement_flags = TTM_PL_FLAG_VRAM |
  31. TTM_PL_FLAG_CACHED;
  32. static uint32_t vram_ne_placement_flags = TTM_PL_FLAG_VRAM |
  33. TTM_PL_FLAG_CACHED |
  34. TTM_PL_FLAG_NO_EVICT;
  35. static uint32_t sys_placement_flags = TTM_PL_FLAG_SYSTEM |
  36. TTM_PL_FLAG_CACHED;
  37. static uint32_t gmr_placement_flags = VMW_PL_FLAG_GMR |
  38. TTM_PL_FLAG_CACHED;
  39. static uint32_t gmr_ne_placement_flags = VMW_PL_FLAG_GMR |
  40. TTM_PL_FLAG_CACHED |
  41. TTM_PL_FLAG_NO_EVICT;
  42. struct ttm_placement vmw_vram_placement = {
  43. .fpfn = 0,
  44. .lpfn = 0,
  45. .num_placement = 1,
  46. .placement = &vram_placement_flags,
  47. .num_busy_placement = 1,
  48. .busy_placement = &vram_placement_flags
  49. };
  50. static uint32_t vram_gmr_placement_flags[] = {
  51. TTM_PL_FLAG_VRAM | TTM_PL_FLAG_CACHED,
  52. VMW_PL_FLAG_GMR | TTM_PL_FLAG_CACHED
  53. };
  54. struct ttm_placement vmw_vram_gmr_placement = {
  55. .fpfn = 0,
  56. .lpfn = 0,
  57. .num_placement = 2,
  58. .placement = vram_gmr_placement_flags,
  59. .num_busy_placement = 1,
  60. .busy_placement = &gmr_placement_flags
  61. };
  62. static uint32_t vram_gmr_ne_placement_flags[] = {
  63. TTM_PL_FLAG_VRAM | TTM_PL_FLAG_CACHED | TTM_PL_FLAG_NO_EVICT,
  64. VMW_PL_FLAG_GMR | TTM_PL_FLAG_CACHED | TTM_PL_FLAG_NO_EVICT
  65. };
  66. struct ttm_placement vmw_vram_gmr_ne_placement = {
  67. .fpfn = 0,
  68. .lpfn = 0,
  69. .num_placement = 2,
  70. .placement = vram_gmr_ne_placement_flags,
  71. .num_busy_placement = 1,
  72. .busy_placement = &gmr_ne_placement_flags
  73. };
  74. struct ttm_placement vmw_vram_sys_placement = {
  75. .fpfn = 0,
  76. .lpfn = 0,
  77. .num_placement = 1,
  78. .placement = &vram_placement_flags,
  79. .num_busy_placement = 1,
  80. .busy_placement = &sys_placement_flags
  81. };
  82. struct ttm_placement vmw_vram_ne_placement = {
  83. .fpfn = 0,
  84. .lpfn = 0,
  85. .num_placement = 1,
  86. .placement = &vram_ne_placement_flags,
  87. .num_busy_placement = 1,
  88. .busy_placement = &vram_ne_placement_flags
  89. };
  90. struct ttm_placement vmw_sys_placement = {
  91. .fpfn = 0,
  92. .lpfn = 0,
  93. .num_placement = 1,
  94. .placement = &sys_placement_flags,
  95. .num_busy_placement = 1,
  96. .busy_placement = &sys_placement_flags
  97. };
  98. static uint32_t evictable_placement_flags[] = {
  99. TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED,
  100. TTM_PL_FLAG_VRAM | TTM_PL_FLAG_CACHED,
  101. VMW_PL_FLAG_GMR | TTM_PL_FLAG_CACHED
  102. };
  103. struct ttm_placement vmw_evictable_placement = {
  104. .fpfn = 0,
  105. .lpfn = 0,
  106. .num_placement = 3,
  107. .placement = evictable_placement_flags,
  108. .num_busy_placement = 1,
  109. .busy_placement = &sys_placement_flags
  110. };
  111. struct vmw_ttm_backend {
  112. struct ttm_backend backend;
  113. struct page **pages;
  114. unsigned long num_pages;
  115. struct vmw_private *dev_priv;
  116. int gmr_id;
  117. };
  118. static int vmw_ttm_populate(struct ttm_backend *backend,
  119. unsigned long num_pages, struct page **pages,
  120. struct page *dummy_read_page,
  121. dma_addr_t *dma_addrs)
  122. {
  123. struct vmw_ttm_backend *vmw_be =
  124. container_of(backend, struct vmw_ttm_backend, backend);
  125. vmw_be->pages = pages;
  126. vmw_be->num_pages = num_pages;
  127. return 0;
  128. }
  129. static int vmw_ttm_bind(struct ttm_backend *backend, struct ttm_mem_reg *bo_mem)
  130. {
  131. struct vmw_ttm_backend *vmw_be =
  132. container_of(backend, struct vmw_ttm_backend, backend);
  133. vmw_be->gmr_id = bo_mem->start;
  134. return vmw_gmr_bind(vmw_be->dev_priv, vmw_be->pages,
  135. vmw_be->num_pages, vmw_be->gmr_id);
  136. }
  137. static int vmw_ttm_unbind(struct ttm_backend *backend)
  138. {
  139. struct vmw_ttm_backend *vmw_be =
  140. container_of(backend, struct vmw_ttm_backend, backend);
  141. vmw_gmr_unbind(vmw_be->dev_priv, vmw_be->gmr_id);
  142. return 0;
  143. }
  144. static void vmw_ttm_clear(struct ttm_backend *backend)
  145. {
  146. struct vmw_ttm_backend *vmw_be =
  147. container_of(backend, struct vmw_ttm_backend, backend);
  148. vmw_be->pages = NULL;
  149. vmw_be->num_pages = 0;
  150. }
  151. static void vmw_ttm_destroy(struct ttm_backend *backend)
  152. {
  153. struct vmw_ttm_backend *vmw_be =
  154. container_of(backend, struct vmw_ttm_backend, backend);
  155. kfree(vmw_be);
  156. }
  157. static struct ttm_backend_func vmw_ttm_func = {
  158. .populate = vmw_ttm_populate,
  159. .clear = vmw_ttm_clear,
  160. .bind = vmw_ttm_bind,
  161. .unbind = vmw_ttm_unbind,
  162. .destroy = vmw_ttm_destroy,
  163. };
  164. struct ttm_backend *vmw_ttm_backend_init(struct ttm_bo_device *bdev)
  165. {
  166. struct vmw_ttm_backend *vmw_be;
  167. vmw_be = kmalloc(sizeof(*vmw_be), GFP_KERNEL);
  168. if (!vmw_be)
  169. return NULL;
  170. vmw_be->backend.func = &vmw_ttm_func;
  171. vmw_be->dev_priv = container_of(bdev, struct vmw_private, bdev);
  172. return &vmw_be->backend;
  173. }
  174. int vmw_invalidate_caches(struct ttm_bo_device *bdev, uint32_t flags)
  175. {
  176. return 0;
  177. }
  178. int vmw_init_mem_type(struct ttm_bo_device *bdev, uint32_t type,
  179. struct ttm_mem_type_manager *man)
  180. {
  181. switch (type) {
  182. case TTM_PL_SYSTEM:
  183. /* System memory */
  184. man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
  185. man->available_caching = TTM_PL_FLAG_CACHED;
  186. man->default_caching = TTM_PL_FLAG_CACHED;
  187. break;
  188. case TTM_PL_VRAM:
  189. /* "On-card" video ram */
  190. man->func = &ttm_bo_manager_func;
  191. man->gpu_offset = 0;
  192. man->flags = TTM_MEMTYPE_FLAG_FIXED | TTM_MEMTYPE_FLAG_MAPPABLE;
  193. man->available_caching = TTM_PL_FLAG_CACHED;
  194. man->default_caching = TTM_PL_FLAG_CACHED;
  195. break;
  196. case VMW_PL_GMR:
  197. /*
  198. * "Guest Memory Regions" is an aperture like feature with
  199. * one slot per bo. There is an upper limit of the number of
  200. * slots as well as the bo size.
  201. */
  202. man->func = &vmw_gmrid_manager_func;
  203. man->gpu_offset = 0;
  204. man->flags = TTM_MEMTYPE_FLAG_CMA | TTM_MEMTYPE_FLAG_MAPPABLE;
  205. man->available_caching = TTM_PL_FLAG_CACHED;
  206. man->default_caching = TTM_PL_FLAG_CACHED;
  207. break;
  208. default:
  209. DRM_ERROR("Unsupported memory type %u\n", (unsigned)type);
  210. return -EINVAL;
  211. }
  212. return 0;
  213. }
  214. void vmw_evict_flags(struct ttm_buffer_object *bo,
  215. struct ttm_placement *placement)
  216. {
  217. *placement = vmw_sys_placement;
  218. }
  219. /**
  220. * FIXME: Proper access checks on buffers.
  221. */
  222. static int vmw_verify_access(struct ttm_buffer_object *bo, struct file *filp)
  223. {
  224. return 0;
  225. }
  226. static int vmw_ttm_io_mem_reserve(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
  227. {
  228. struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
  229. struct vmw_private *dev_priv = container_of(bdev, struct vmw_private, bdev);
  230. mem->bus.addr = NULL;
  231. mem->bus.is_iomem = false;
  232. mem->bus.offset = 0;
  233. mem->bus.size = mem->num_pages << PAGE_SHIFT;
  234. mem->bus.base = 0;
  235. if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
  236. return -EINVAL;
  237. switch (mem->mem_type) {
  238. case TTM_PL_SYSTEM:
  239. case VMW_PL_GMR:
  240. return 0;
  241. case TTM_PL_VRAM:
  242. mem->bus.offset = mem->start << PAGE_SHIFT;
  243. mem->bus.base = dev_priv->vram_start;
  244. mem->bus.is_iomem = true;
  245. break;
  246. default:
  247. return -EINVAL;
  248. }
  249. return 0;
  250. }
  251. static void vmw_ttm_io_mem_free(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
  252. {
  253. }
  254. static int vmw_ttm_fault_reserve_notify(struct ttm_buffer_object *bo)
  255. {
  256. return 0;
  257. }
  258. /**
  259. * FIXME: We're using the old vmware polling method to sync.
  260. * Do this with fences instead.
  261. */
  262. static void *vmw_sync_obj_ref(void *sync_obj)
  263. {
  264. return (void *)
  265. vmw_fence_obj_reference((struct vmw_fence_obj *) sync_obj);
  266. }
  267. static void vmw_sync_obj_unref(void **sync_obj)
  268. {
  269. vmw_fence_obj_unreference((struct vmw_fence_obj **) sync_obj);
  270. }
  271. static int vmw_sync_obj_flush(void *sync_obj, void *sync_arg)
  272. {
  273. vmw_fence_obj_flush((struct vmw_fence_obj *) sync_obj);
  274. return 0;
  275. }
  276. static bool vmw_sync_obj_signaled(void *sync_obj, void *sync_arg)
  277. {
  278. unsigned long flags = (unsigned long) sync_arg;
  279. return vmw_fence_obj_signaled((struct vmw_fence_obj *) sync_obj,
  280. (uint32_t) flags);
  281. }
  282. static int vmw_sync_obj_wait(void *sync_obj, void *sync_arg,
  283. bool lazy, bool interruptible)
  284. {
  285. unsigned long flags = (unsigned long) sync_arg;
  286. return vmw_fence_obj_wait((struct vmw_fence_obj *) sync_obj,
  287. (uint32_t) flags,
  288. lazy, interruptible,
  289. VMW_FENCE_WAIT_TIMEOUT);
  290. }
  291. struct ttm_bo_driver vmw_bo_driver = {
  292. .create_ttm_backend_entry = vmw_ttm_backend_init,
  293. .invalidate_caches = vmw_invalidate_caches,
  294. .init_mem_type = vmw_init_mem_type,
  295. .evict_flags = vmw_evict_flags,
  296. .move = NULL,
  297. .verify_access = vmw_verify_access,
  298. .sync_obj_signaled = vmw_sync_obj_signaled,
  299. .sync_obj_wait = vmw_sync_obj_wait,
  300. .sync_obj_flush = vmw_sync_obj_flush,
  301. .sync_obj_unref = vmw_sync_obj_unref,
  302. .sync_obj_ref = vmw_sync_obj_ref,
  303. .move_notify = NULL,
  304. .swap_notify = NULL,
  305. .fault_reserve_notify = &vmw_ttm_fault_reserve_notify,
  306. .io_mem_reserve = &vmw_ttm_io_mem_reserve,
  307. .io_mem_free = &vmw_ttm_io_mem_free,
  308. };