virtgpu_ttm.c 12 KB

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