vmwgfx_buffer.c 9.8 KB

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