vmwgfx_buffer.c 7.4 KB

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