vmwgfx_buffer.c 8.6 KB

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