virtgpu_ttm.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /*
  2. * Copyright (C) 2015 Red Hat, Inc.
  3. * All Rights Reserved.
  4. *
  5. * Authors:
  6. * Dave Airlie
  7. * Alon Levy
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a
  10. * copy of this software and associated documentation files (the "Software"),
  11. * to deal in the Software without restriction, including without limitation
  12. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  13. * and/or sell copies of the Software, and to permit persons to whom the
  14. * Software is furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  22. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  23. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  24. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  25. * OTHER DEALINGS IN THE SOFTWARE.
  26. */
  27. #include <drm/ttm/ttm_bo_api.h>
  28. #include <drm/ttm/ttm_bo_driver.h>
  29. #include <drm/ttm/ttm_placement.h>
  30. #include <drm/ttm/ttm_page_alloc.h>
  31. #include <drm/ttm/ttm_module.h>
  32. #include <drm/drmP.h>
  33. #include <drm/drm.h>
  34. #include <drm/virtgpu_drm.h>
  35. #include "virtgpu_drv.h"
  36. #include <linux/delay.h>
  37. #define DRM_FILE_PAGE_OFFSET (0x100000000ULL >> PAGE_SHIFT)
  38. static struct
  39. virtio_gpu_device *virtio_gpu_get_vgdev(struct ttm_bo_device *bdev)
  40. {
  41. struct virtio_gpu_mman *mman;
  42. struct virtio_gpu_device *vgdev;
  43. mman = container_of(bdev, struct virtio_gpu_mman, bdev);
  44. vgdev = container_of(mman, struct virtio_gpu_device, mman);
  45. return vgdev;
  46. }
  47. static int virtio_gpu_ttm_mem_global_init(struct drm_global_reference *ref)
  48. {
  49. return ttm_mem_global_init(ref->object);
  50. }
  51. static void virtio_gpu_ttm_mem_global_release(struct drm_global_reference *ref)
  52. {
  53. ttm_mem_global_release(ref->object);
  54. }
  55. static int virtio_gpu_ttm_global_init(struct virtio_gpu_device *vgdev)
  56. {
  57. struct drm_global_reference *global_ref;
  58. int r;
  59. vgdev->mman.mem_global_referenced = false;
  60. global_ref = &vgdev->mman.mem_global_ref;
  61. global_ref->global_type = DRM_GLOBAL_TTM_MEM;
  62. global_ref->size = sizeof(struct ttm_mem_global);
  63. global_ref->init = &virtio_gpu_ttm_mem_global_init;
  64. global_ref->release = &virtio_gpu_ttm_mem_global_release;
  65. r = drm_global_item_ref(global_ref);
  66. if (r != 0) {
  67. DRM_ERROR("Failed setting up TTM memory accounting "
  68. "subsystem.\n");
  69. return r;
  70. }
  71. vgdev->mman.bo_global_ref.mem_glob =
  72. vgdev->mman.mem_global_ref.object;
  73. global_ref = &vgdev->mman.bo_global_ref.ref;
  74. global_ref->global_type = DRM_GLOBAL_TTM_BO;
  75. global_ref->size = sizeof(struct ttm_bo_global);
  76. global_ref->init = &ttm_bo_global_init;
  77. global_ref->release = &ttm_bo_global_release;
  78. r = drm_global_item_ref(global_ref);
  79. if (r != 0) {
  80. DRM_ERROR("Failed setting up TTM BO subsystem.\n");
  81. drm_global_item_unref(&vgdev->mman.mem_global_ref);
  82. return r;
  83. }
  84. vgdev->mman.mem_global_referenced = true;
  85. return 0;
  86. }
  87. static void virtio_gpu_ttm_global_fini(struct virtio_gpu_device *vgdev)
  88. {
  89. if (vgdev->mman.mem_global_referenced) {
  90. drm_global_item_unref(&vgdev->mman.bo_global_ref.ref);
  91. drm_global_item_unref(&vgdev->mman.mem_global_ref);
  92. vgdev->mman.mem_global_referenced = false;
  93. }
  94. }
  95. int virtio_gpu_mmap(struct file *filp, struct vm_area_struct *vma)
  96. {
  97. struct drm_file *file_priv;
  98. struct virtio_gpu_device *vgdev;
  99. int r;
  100. file_priv = filp->private_data;
  101. vgdev = file_priv->minor->dev->dev_private;
  102. if (vgdev == NULL) {
  103. DRM_ERROR(
  104. "filp->private_data->minor->dev->dev_private == NULL\n");
  105. return -EINVAL;
  106. }
  107. r = ttm_bo_mmap(filp, vma, &vgdev->mman.bdev);
  108. return r;
  109. }
  110. static int virtio_gpu_invalidate_caches(struct ttm_bo_device *bdev,
  111. uint32_t flags)
  112. {
  113. return 0;
  114. }
  115. static int ttm_bo_man_get_node(struct ttm_mem_type_manager *man,
  116. struct ttm_buffer_object *bo,
  117. const struct ttm_place *place,
  118. struct ttm_mem_reg *mem)
  119. {
  120. mem->mm_node = (void *)1;
  121. return 0;
  122. }
  123. static void ttm_bo_man_put_node(struct ttm_mem_type_manager *man,
  124. struct ttm_mem_reg *mem)
  125. {
  126. mem->mm_node = (void *)NULL;
  127. }
  128. static int ttm_bo_man_init(struct ttm_mem_type_manager *man,
  129. unsigned long p_size)
  130. {
  131. return 0;
  132. }
  133. static int ttm_bo_man_takedown(struct ttm_mem_type_manager *man)
  134. {
  135. return 0;
  136. }
  137. static void ttm_bo_man_debug(struct ttm_mem_type_manager *man,
  138. struct drm_printer *printer)
  139. {
  140. }
  141. static const struct ttm_mem_type_manager_func virtio_gpu_bo_manager_func = {
  142. .init = ttm_bo_man_init,
  143. .takedown = ttm_bo_man_takedown,
  144. .get_node = ttm_bo_man_get_node,
  145. .put_node = ttm_bo_man_put_node,
  146. .debug = ttm_bo_man_debug
  147. };
  148. static int virtio_gpu_init_mem_type(struct ttm_bo_device *bdev, uint32_t type,
  149. struct ttm_mem_type_manager *man)
  150. {
  151. struct virtio_gpu_device *vgdev;
  152. vgdev = virtio_gpu_get_vgdev(bdev);
  153. switch (type) {
  154. case TTM_PL_SYSTEM:
  155. /* System memory */
  156. man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
  157. man->available_caching = TTM_PL_MASK_CACHING;
  158. man->default_caching = TTM_PL_FLAG_CACHED;
  159. break;
  160. case TTM_PL_TT:
  161. man->func = &virtio_gpu_bo_manager_func;
  162. man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
  163. man->available_caching = TTM_PL_MASK_CACHING;
  164. man->default_caching = TTM_PL_FLAG_CACHED;
  165. break;
  166. default:
  167. DRM_ERROR("Unsupported memory type %u\n", (unsigned int)type);
  168. return -EINVAL;
  169. }
  170. return 0;
  171. }
  172. static void virtio_gpu_evict_flags(struct ttm_buffer_object *bo,
  173. struct ttm_placement *placement)
  174. {
  175. static const struct ttm_place placements = {
  176. .fpfn = 0,
  177. .lpfn = 0,
  178. .flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM,
  179. };
  180. placement->placement = &placements;
  181. placement->busy_placement = &placements;
  182. placement->num_placement = 1;
  183. placement->num_busy_placement = 1;
  184. }
  185. static int virtio_gpu_verify_access(struct ttm_buffer_object *bo,
  186. struct file *filp)
  187. {
  188. return 0;
  189. }
  190. static int virtio_gpu_ttm_io_mem_reserve(struct ttm_bo_device *bdev,
  191. struct ttm_mem_reg *mem)
  192. {
  193. struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
  194. mem->bus.addr = NULL;
  195. mem->bus.offset = 0;
  196. mem->bus.size = mem->num_pages << PAGE_SHIFT;
  197. mem->bus.base = 0;
  198. mem->bus.is_iomem = false;
  199. if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
  200. return -EINVAL;
  201. switch (mem->mem_type) {
  202. case TTM_PL_SYSTEM:
  203. case TTM_PL_TT:
  204. /* system memory */
  205. return 0;
  206. default:
  207. return -EINVAL;
  208. }
  209. return 0;
  210. }
  211. static void virtio_gpu_ttm_io_mem_free(struct ttm_bo_device *bdev,
  212. struct ttm_mem_reg *mem)
  213. {
  214. }
  215. /*
  216. * TTM backend functions.
  217. */
  218. struct virtio_gpu_ttm_tt {
  219. struct ttm_dma_tt ttm;
  220. struct virtio_gpu_device *vgdev;
  221. u64 offset;
  222. };
  223. static int virtio_gpu_ttm_backend_bind(struct ttm_tt *ttm,
  224. struct ttm_mem_reg *bo_mem)
  225. {
  226. struct virtio_gpu_ttm_tt *gtt = (void *)ttm;
  227. gtt->offset = (unsigned long)(bo_mem->start << PAGE_SHIFT);
  228. if (!ttm->num_pages)
  229. WARN(1, "nothing to bind %lu pages for mreg %p back %p!\n",
  230. ttm->num_pages, bo_mem, ttm);
  231. /* Not implemented */
  232. return 0;
  233. }
  234. static int virtio_gpu_ttm_backend_unbind(struct ttm_tt *ttm)
  235. {
  236. /* Not implemented */
  237. return 0;
  238. }
  239. static void virtio_gpu_ttm_backend_destroy(struct ttm_tt *ttm)
  240. {
  241. struct virtio_gpu_ttm_tt *gtt = (void *)ttm;
  242. ttm_dma_tt_fini(&gtt->ttm);
  243. kfree(gtt);
  244. }
  245. static struct ttm_backend_func virtio_gpu_backend_func = {
  246. .bind = &virtio_gpu_ttm_backend_bind,
  247. .unbind = &virtio_gpu_ttm_backend_unbind,
  248. .destroy = &virtio_gpu_ttm_backend_destroy,
  249. };
  250. static struct ttm_tt *virtio_gpu_ttm_tt_create(struct ttm_buffer_object *bo,
  251. uint32_t page_flags)
  252. {
  253. struct virtio_gpu_device *vgdev;
  254. struct virtio_gpu_ttm_tt *gtt;
  255. vgdev = virtio_gpu_get_vgdev(bo->bdev);
  256. gtt = kzalloc(sizeof(struct virtio_gpu_ttm_tt), GFP_KERNEL);
  257. if (gtt == NULL)
  258. return NULL;
  259. gtt->ttm.ttm.func = &virtio_gpu_backend_func;
  260. gtt->vgdev = vgdev;
  261. if (ttm_dma_tt_init(&gtt->ttm, bo, page_flags)) {
  262. kfree(gtt);
  263. return NULL;
  264. }
  265. return &gtt->ttm.ttm;
  266. }
  267. static void virtio_gpu_move_null(struct ttm_buffer_object *bo,
  268. struct ttm_mem_reg *new_mem)
  269. {
  270. struct ttm_mem_reg *old_mem = &bo->mem;
  271. BUG_ON(old_mem->mm_node != NULL);
  272. *old_mem = *new_mem;
  273. new_mem->mm_node = NULL;
  274. }
  275. static int virtio_gpu_bo_move(struct ttm_buffer_object *bo, bool evict,
  276. struct ttm_operation_ctx *ctx,
  277. struct ttm_mem_reg *new_mem)
  278. {
  279. int ret;
  280. ret = ttm_bo_wait(bo, ctx->interruptible, ctx->no_wait_gpu);
  281. if (ret)
  282. return ret;
  283. virtio_gpu_move_null(bo, new_mem);
  284. return 0;
  285. }
  286. static void virtio_gpu_bo_move_notify(struct ttm_buffer_object *tbo,
  287. bool evict,
  288. struct ttm_mem_reg *new_mem)
  289. {
  290. struct virtio_gpu_object *bo;
  291. struct virtio_gpu_device *vgdev;
  292. bo = container_of(tbo, struct virtio_gpu_object, tbo);
  293. vgdev = (struct virtio_gpu_device *)bo->gem_base.dev->dev_private;
  294. if (!new_mem || (new_mem->placement & TTM_PL_FLAG_SYSTEM)) {
  295. if (bo->hw_res_handle)
  296. virtio_gpu_object_detach(vgdev, bo);
  297. } else if (new_mem->placement & TTM_PL_FLAG_TT) {
  298. if (bo->hw_res_handle) {
  299. virtio_gpu_object_attach(vgdev, bo, bo->hw_res_handle,
  300. NULL);
  301. }
  302. }
  303. }
  304. static void virtio_gpu_bo_swap_notify(struct ttm_buffer_object *tbo)
  305. {
  306. struct virtio_gpu_object *bo;
  307. struct virtio_gpu_device *vgdev;
  308. bo = container_of(tbo, struct virtio_gpu_object, tbo);
  309. vgdev = (struct virtio_gpu_device *)bo->gem_base.dev->dev_private;
  310. if (bo->pages)
  311. virtio_gpu_object_free_sg_table(bo);
  312. }
  313. static struct ttm_bo_driver virtio_gpu_bo_driver = {
  314. .ttm_tt_create = &virtio_gpu_ttm_tt_create,
  315. .invalidate_caches = &virtio_gpu_invalidate_caches,
  316. .init_mem_type = &virtio_gpu_init_mem_type,
  317. .eviction_valuable = ttm_bo_eviction_valuable,
  318. .evict_flags = &virtio_gpu_evict_flags,
  319. .move = &virtio_gpu_bo_move,
  320. .verify_access = &virtio_gpu_verify_access,
  321. .io_mem_reserve = &virtio_gpu_ttm_io_mem_reserve,
  322. .io_mem_free = &virtio_gpu_ttm_io_mem_free,
  323. .move_notify = &virtio_gpu_bo_move_notify,
  324. .swap_notify = &virtio_gpu_bo_swap_notify,
  325. };
  326. int virtio_gpu_ttm_init(struct virtio_gpu_device *vgdev)
  327. {
  328. int r;
  329. r = virtio_gpu_ttm_global_init(vgdev);
  330. if (r)
  331. return r;
  332. /* No others user of address space so set it to 0 */
  333. r = ttm_bo_device_init(&vgdev->mman.bdev,
  334. vgdev->mman.bo_global_ref.ref.object,
  335. &virtio_gpu_bo_driver,
  336. vgdev->ddev->anon_inode->i_mapping,
  337. DRM_FILE_PAGE_OFFSET, 0);
  338. if (r) {
  339. DRM_ERROR("failed initializing buffer object driver(%d).\n", r);
  340. goto err_dev_init;
  341. }
  342. r = ttm_bo_init_mm(&vgdev->mman.bdev, TTM_PL_TT, 0);
  343. if (r) {
  344. DRM_ERROR("Failed initializing GTT heap.\n");
  345. goto err_mm_init;
  346. }
  347. return 0;
  348. err_mm_init:
  349. ttm_bo_device_release(&vgdev->mman.bdev);
  350. err_dev_init:
  351. virtio_gpu_ttm_global_fini(vgdev);
  352. return r;
  353. }
  354. void virtio_gpu_ttm_fini(struct virtio_gpu_device *vgdev)
  355. {
  356. ttm_bo_device_release(&vgdev->mman.bdev);
  357. virtio_gpu_ttm_global_fini(vgdev);
  358. DRM_INFO("virtio_gpu: ttm finalized\n");
  359. }