virtgpu_ttm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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 <ttm/ttm_bo_api.h>
  28. #include <ttm/ttm_bo_driver.h>
  29. #include <ttm/ttm_placement.h>
  30. #include <ttm/ttm_page_alloc.h>
  31. #include <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. #if 0
  96. /*
  97. * Hmm, seems to not do anything useful. Leftover debug hack?
  98. * Something like printing pagefaults to kernel log?
  99. */
  100. static struct vm_operations_struct virtio_gpu_ttm_vm_ops;
  101. static const struct vm_operations_struct *ttm_vm_ops;
  102. static int virtio_gpu_ttm_fault(struct vm_fault *vmf)
  103. {
  104. struct ttm_buffer_object *bo;
  105. struct virtio_gpu_device *vgdev;
  106. int r;
  107. bo = (struct ttm_buffer_object *)vmf->vma->vm_private_data;
  108. if (bo == NULL)
  109. return VM_FAULT_NOPAGE;
  110. vgdev = virtio_gpu_get_vgdev(bo->bdev);
  111. r = ttm_vm_ops->fault(vmf);
  112. return r;
  113. }
  114. #endif
  115. int virtio_gpu_mmap(struct file *filp, struct vm_area_struct *vma)
  116. {
  117. struct drm_file *file_priv;
  118. struct virtio_gpu_device *vgdev;
  119. int r;
  120. file_priv = filp->private_data;
  121. vgdev = file_priv->minor->dev->dev_private;
  122. if (vgdev == NULL) {
  123. DRM_ERROR(
  124. "filp->private_data->minor->dev->dev_private == NULL\n");
  125. return -EINVAL;
  126. }
  127. r = ttm_bo_mmap(filp, vma, &vgdev->mman.bdev);
  128. #if 0
  129. if (unlikely(r != 0))
  130. return r;
  131. if (unlikely(ttm_vm_ops == NULL)) {
  132. ttm_vm_ops = vma->vm_ops;
  133. virtio_gpu_ttm_vm_ops = *ttm_vm_ops;
  134. virtio_gpu_ttm_vm_ops.fault = &virtio_gpu_ttm_fault;
  135. }
  136. vma->vm_ops = &virtio_gpu_ttm_vm_ops;
  137. return 0;
  138. #else
  139. return r;
  140. #endif
  141. }
  142. static int virtio_gpu_invalidate_caches(struct ttm_bo_device *bdev,
  143. uint32_t flags)
  144. {
  145. return 0;
  146. }
  147. static int ttm_bo_man_get_node(struct ttm_mem_type_manager *man,
  148. struct ttm_buffer_object *bo,
  149. const struct ttm_place *place,
  150. struct ttm_mem_reg *mem)
  151. {
  152. mem->mm_node = (void *)1;
  153. return 0;
  154. }
  155. static void ttm_bo_man_put_node(struct ttm_mem_type_manager *man,
  156. struct ttm_mem_reg *mem)
  157. {
  158. mem->mm_node = (void *)NULL;
  159. return;
  160. }
  161. static int ttm_bo_man_init(struct ttm_mem_type_manager *man,
  162. unsigned long p_size)
  163. {
  164. return 0;
  165. }
  166. static int ttm_bo_man_takedown(struct ttm_mem_type_manager *man)
  167. {
  168. return 0;
  169. }
  170. static void ttm_bo_man_debug(struct ttm_mem_type_manager *man,
  171. const char *prefix)
  172. {
  173. }
  174. static const struct ttm_mem_type_manager_func virtio_gpu_bo_manager_func = {
  175. .init = ttm_bo_man_init,
  176. .takedown = ttm_bo_man_takedown,
  177. .get_node = ttm_bo_man_get_node,
  178. .put_node = ttm_bo_man_put_node,
  179. .debug = ttm_bo_man_debug
  180. };
  181. static int virtio_gpu_init_mem_type(struct ttm_bo_device *bdev, uint32_t type,
  182. struct ttm_mem_type_manager *man)
  183. {
  184. struct virtio_gpu_device *vgdev;
  185. vgdev = virtio_gpu_get_vgdev(bdev);
  186. switch (type) {
  187. case TTM_PL_SYSTEM:
  188. /* System memory */
  189. man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
  190. man->available_caching = TTM_PL_MASK_CACHING;
  191. man->default_caching = TTM_PL_FLAG_CACHED;
  192. break;
  193. case TTM_PL_TT:
  194. man->func = &virtio_gpu_bo_manager_func;
  195. man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
  196. man->available_caching = TTM_PL_MASK_CACHING;
  197. man->default_caching = TTM_PL_FLAG_CACHED;
  198. break;
  199. default:
  200. DRM_ERROR("Unsupported memory type %u\n", (unsigned)type);
  201. return -EINVAL;
  202. }
  203. return 0;
  204. }
  205. static void virtio_gpu_evict_flags(struct ttm_buffer_object *bo,
  206. struct ttm_placement *placement)
  207. {
  208. static struct ttm_place placements = {
  209. .fpfn = 0,
  210. .lpfn = 0,
  211. .flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM,
  212. };
  213. placement->placement = &placements;
  214. placement->busy_placement = &placements;
  215. placement->num_placement = 1;
  216. placement->num_busy_placement = 1;
  217. return;
  218. }
  219. static int virtio_gpu_verify_access(struct ttm_buffer_object *bo,
  220. struct file *filp)
  221. {
  222. return 0;
  223. }
  224. static int virtio_gpu_ttm_io_mem_reserve(struct ttm_bo_device *bdev,
  225. struct ttm_mem_reg *mem)
  226. {
  227. struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
  228. mem->bus.addr = NULL;
  229. mem->bus.offset = 0;
  230. mem->bus.size = mem->num_pages << PAGE_SHIFT;
  231. mem->bus.base = 0;
  232. mem->bus.is_iomem = false;
  233. if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
  234. return -EINVAL;
  235. switch (mem->mem_type) {
  236. case TTM_PL_SYSTEM:
  237. case TTM_PL_TT:
  238. /* system memory */
  239. return 0;
  240. default:
  241. return -EINVAL;
  242. }
  243. return 0;
  244. }
  245. static void virtio_gpu_ttm_io_mem_free(struct ttm_bo_device *bdev,
  246. struct ttm_mem_reg *mem)
  247. {
  248. }
  249. /*
  250. * TTM backend functions.
  251. */
  252. struct virtio_gpu_ttm_tt {
  253. struct ttm_dma_tt ttm;
  254. struct virtio_gpu_device *vgdev;
  255. u64 offset;
  256. };
  257. static int virtio_gpu_ttm_backend_bind(struct ttm_tt *ttm,
  258. struct ttm_mem_reg *bo_mem)
  259. {
  260. struct virtio_gpu_ttm_tt *gtt = (void *)ttm;
  261. gtt->offset = (unsigned long)(bo_mem->start << PAGE_SHIFT);
  262. if (!ttm->num_pages)
  263. WARN(1, "nothing to bind %lu pages for mreg %p back %p!\n",
  264. ttm->num_pages, bo_mem, ttm);
  265. /* Not implemented */
  266. return 0;
  267. }
  268. static int virtio_gpu_ttm_backend_unbind(struct ttm_tt *ttm)
  269. {
  270. /* Not implemented */
  271. return 0;
  272. }
  273. static void virtio_gpu_ttm_backend_destroy(struct ttm_tt *ttm)
  274. {
  275. struct virtio_gpu_ttm_tt *gtt = (void *)ttm;
  276. ttm_dma_tt_fini(&gtt->ttm);
  277. kfree(gtt);
  278. }
  279. static struct ttm_backend_func virtio_gpu_backend_func = {
  280. .bind = &virtio_gpu_ttm_backend_bind,
  281. .unbind = &virtio_gpu_ttm_backend_unbind,
  282. .destroy = &virtio_gpu_ttm_backend_destroy,
  283. };
  284. static int virtio_gpu_ttm_tt_populate(struct ttm_tt *ttm)
  285. {
  286. if (ttm->state != tt_unpopulated)
  287. return 0;
  288. return ttm_pool_populate(ttm);
  289. }
  290. static void virtio_gpu_ttm_tt_unpopulate(struct ttm_tt *ttm)
  291. {
  292. ttm_pool_unpopulate(ttm);
  293. }
  294. static struct ttm_tt *virtio_gpu_ttm_tt_create(struct ttm_bo_device *bdev,
  295. unsigned long size,
  296. uint32_t page_flags,
  297. struct page *dummy_read_page)
  298. {
  299. struct virtio_gpu_device *vgdev;
  300. struct virtio_gpu_ttm_tt *gtt;
  301. vgdev = virtio_gpu_get_vgdev(bdev);
  302. gtt = kzalloc(sizeof(struct virtio_gpu_ttm_tt), GFP_KERNEL);
  303. if (gtt == NULL)
  304. return NULL;
  305. gtt->ttm.ttm.func = &virtio_gpu_backend_func;
  306. gtt->vgdev = vgdev;
  307. if (ttm_dma_tt_init(&gtt->ttm, bdev, size, page_flags,
  308. dummy_read_page)) {
  309. kfree(gtt);
  310. return NULL;
  311. }
  312. return &gtt->ttm.ttm;
  313. }
  314. static void virtio_gpu_move_null(struct ttm_buffer_object *bo,
  315. struct ttm_mem_reg *new_mem)
  316. {
  317. struct ttm_mem_reg *old_mem = &bo->mem;
  318. BUG_ON(old_mem->mm_node != NULL);
  319. *old_mem = *new_mem;
  320. new_mem->mm_node = NULL;
  321. }
  322. static int virtio_gpu_bo_move(struct ttm_buffer_object *bo,
  323. bool evict, bool interruptible,
  324. bool no_wait_gpu,
  325. struct ttm_mem_reg *new_mem)
  326. {
  327. int ret;
  328. ret = ttm_bo_wait(bo, interruptible, no_wait_gpu);
  329. if (ret)
  330. return ret;
  331. virtio_gpu_move_null(bo, new_mem);
  332. return 0;
  333. }
  334. static void virtio_gpu_bo_move_notify(struct ttm_buffer_object *tbo,
  335. bool evict,
  336. struct ttm_mem_reg *new_mem)
  337. {
  338. struct virtio_gpu_object *bo;
  339. struct virtio_gpu_device *vgdev;
  340. bo = container_of(tbo, struct virtio_gpu_object, tbo);
  341. vgdev = (struct virtio_gpu_device *)bo->gem_base.dev->dev_private;
  342. if (!new_mem || (new_mem->placement & TTM_PL_FLAG_SYSTEM)) {
  343. if (bo->hw_res_handle)
  344. virtio_gpu_cmd_resource_inval_backing(vgdev,
  345. bo->hw_res_handle);
  346. } else if (new_mem->placement & TTM_PL_FLAG_TT) {
  347. if (bo->hw_res_handle) {
  348. virtio_gpu_object_attach(vgdev, bo, bo->hw_res_handle,
  349. NULL);
  350. }
  351. }
  352. }
  353. static void virtio_gpu_bo_swap_notify(struct ttm_buffer_object *tbo)
  354. {
  355. struct virtio_gpu_object *bo;
  356. struct virtio_gpu_device *vgdev;
  357. bo = container_of(tbo, struct virtio_gpu_object, tbo);
  358. vgdev = (struct virtio_gpu_device *)bo->gem_base.dev->dev_private;
  359. if (bo->pages)
  360. virtio_gpu_object_free_sg_table(bo);
  361. }
  362. static struct ttm_bo_driver virtio_gpu_bo_driver = {
  363. .ttm_tt_create = &virtio_gpu_ttm_tt_create,
  364. .ttm_tt_populate = &virtio_gpu_ttm_tt_populate,
  365. .ttm_tt_unpopulate = &virtio_gpu_ttm_tt_unpopulate,
  366. .invalidate_caches = &virtio_gpu_invalidate_caches,
  367. .init_mem_type = &virtio_gpu_init_mem_type,
  368. .eviction_valuable = ttm_bo_eviction_valuable,
  369. .evict_flags = &virtio_gpu_evict_flags,
  370. .move = &virtio_gpu_bo_move,
  371. .verify_access = &virtio_gpu_verify_access,
  372. .io_mem_reserve = &virtio_gpu_ttm_io_mem_reserve,
  373. .io_mem_free = &virtio_gpu_ttm_io_mem_free,
  374. .move_notify = &virtio_gpu_bo_move_notify,
  375. .swap_notify = &virtio_gpu_bo_swap_notify,
  376. };
  377. int virtio_gpu_ttm_init(struct virtio_gpu_device *vgdev)
  378. {
  379. int r;
  380. r = virtio_gpu_ttm_global_init(vgdev);
  381. if (r)
  382. return r;
  383. /* No others user of address space so set it to 0 */
  384. r = ttm_bo_device_init(&vgdev->mman.bdev,
  385. vgdev->mman.bo_global_ref.ref.object,
  386. &virtio_gpu_bo_driver,
  387. vgdev->ddev->anon_inode->i_mapping,
  388. DRM_FILE_PAGE_OFFSET, 0);
  389. if (r) {
  390. DRM_ERROR("failed initializing buffer object driver(%d).\n", r);
  391. goto err_dev_init;
  392. }
  393. r = ttm_bo_init_mm(&vgdev->mman.bdev, TTM_PL_TT, 0);
  394. if (r) {
  395. DRM_ERROR("Failed initializing GTT heap.\n");
  396. goto err_mm_init;
  397. }
  398. return 0;
  399. err_mm_init:
  400. ttm_bo_device_release(&vgdev->mman.bdev);
  401. err_dev_init:
  402. virtio_gpu_ttm_global_fini(vgdev);
  403. return r;
  404. }
  405. void virtio_gpu_ttm_fini(struct virtio_gpu_device *vgdev)
  406. {
  407. ttm_bo_device_release(&vgdev->mman.bdev);
  408. virtio_gpu_ttm_global_fini(vgdev);
  409. DRM_INFO("virtio_gpu: ttm finalized\n");
  410. }