vmwgfx_buffer.c 8.6 KB

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