vmwgfx_buffer.c 9.3 KB

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