virtgpu_ttm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. #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. }
  160. static int ttm_bo_man_init(struct ttm_mem_type_manager *man,
  161. unsigned long p_size)
  162. {
  163. return 0;
  164. }
  165. static int ttm_bo_man_takedown(struct ttm_mem_type_manager *man)
  166. {
  167. return 0;
  168. }
  169. static void ttm_bo_man_debug(struct ttm_mem_type_manager *man,
  170. struct drm_printer *printer)
  171. {
  172. }
  173. static const struct ttm_mem_type_manager_func virtio_gpu_bo_manager_func = {
  174. .init = ttm_bo_man_init,
  175. .takedown = ttm_bo_man_takedown,
  176. .get_node = ttm_bo_man_get_node,
  177. .put_node = ttm_bo_man_put_node,
  178. .debug = ttm_bo_man_debug
  179. };
  180. static int virtio_gpu_init_mem_type(struct ttm_bo_device *bdev, uint32_t type,
  181. struct ttm_mem_type_manager *man)
  182. {
  183. struct virtio_gpu_device *vgdev;
  184. vgdev = virtio_gpu_get_vgdev(bdev);
  185. switch (type) {
  186. case TTM_PL_SYSTEM:
  187. /* System memory */
  188. man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
  189. man->available_caching = TTM_PL_MASK_CACHING;
  190. man->default_caching = TTM_PL_FLAG_CACHED;
  191. break;
  192. case TTM_PL_TT:
  193. man->func = &virtio_gpu_bo_manager_func;
  194. man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
  195. man->available_caching = TTM_PL_MASK_CACHING;
  196. man->default_caching = TTM_PL_FLAG_CACHED;
  197. break;
  198. default:
  199. DRM_ERROR("Unsupported memory type %u\n", (unsigned int)type);
  200. return -EINVAL;
  201. }
  202. return 0;
  203. }
  204. static void virtio_gpu_evict_flags(struct ttm_buffer_object *bo,
  205. struct ttm_placement *placement)
  206. {
  207. static const struct ttm_place placements = {
  208. .fpfn = 0,
  209. .lpfn = 0,
  210. .flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM,
  211. };
  212. placement->placement = &placements;
  213. placement->busy_placement = &placements;
  214. placement->num_placement = 1;
  215. placement->num_busy_placement = 1;
  216. }
  217. static int virtio_gpu_verify_access(struct ttm_buffer_object *bo,
  218. struct file *filp)
  219. {
  220. return 0;
  221. }
  222. static int virtio_gpu_ttm_io_mem_reserve(struct ttm_bo_device *bdev,
  223. struct ttm_mem_reg *mem)
  224. {
  225. struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
  226. mem->bus.addr = NULL;
  227. mem->bus.offset = 0;
  228. mem->bus.size = mem->num_pages << PAGE_SHIFT;
  229. mem->bus.base = 0;
  230. mem->bus.is_iomem = false;
  231. if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
  232. return -EINVAL;
  233. switch (mem->mem_type) {
  234. case TTM_PL_SYSTEM:
  235. case TTM_PL_TT:
  236. /* system memory */
  237. return 0;
  238. default:
  239. return -EINVAL;
  240. }
  241. return 0;
  242. }
  243. static void virtio_gpu_ttm_io_mem_free(struct ttm_bo_device *bdev,
  244. struct ttm_mem_reg *mem)
  245. {
  246. }
  247. /*
  248. * TTM backend functions.
  249. */
  250. struct virtio_gpu_ttm_tt {
  251. struct ttm_dma_tt ttm;
  252. struct virtio_gpu_device *vgdev;
  253. u64 offset;
  254. };
  255. static int virtio_gpu_ttm_backend_bind(struct ttm_tt *ttm,
  256. struct ttm_mem_reg *bo_mem)
  257. {
  258. struct virtio_gpu_ttm_tt *gtt = (void *)ttm;
  259. gtt->offset = (unsigned long)(bo_mem->start << PAGE_SHIFT);
  260. if (!ttm->num_pages)
  261. WARN(1, "nothing to bind %lu pages for mreg %p back %p!\n",
  262. ttm->num_pages, bo_mem, ttm);
  263. /* Not implemented */
  264. return 0;
  265. }
  266. static int virtio_gpu_ttm_backend_unbind(struct ttm_tt *ttm)
  267. {
  268. /* Not implemented */
  269. return 0;
  270. }
  271. static void virtio_gpu_ttm_backend_destroy(struct ttm_tt *ttm)
  272. {
  273. struct virtio_gpu_ttm_tt *gtt = (void *)ttm;
  274. ttm_dma_tt_fini(&gtt->ttm);
  275. kfree(gtt);
  276. }
  277. static struct ttm_backend_func virtio_gpu_backend_func = {
  278. .bind = &virtio_gpu_ttm_backend_bind,
  279. .unbind = &virtio_gpu_ttm_backend_unbind,
  280. .destroy = &virtio_gpu_ttm_backend_destroy,
  281. };
  282. static struct ttm_tt *virtio_gpu_ttm_tt_create(struct ttm_buffer_object *bo,
  283. uint32_t page_flags)
  284. {
  285. struct virtio_gpu_device *vgdev;
  286. struct virtio_gpu_ttm_tt *gtt;
  287. vgdev = virtio_gpu_get_vgdev(bo->bdev);
  288. gtt = kzalloc(sizeof(struct virtio_gpu_ttm_tt), GFP_KERNEL);
  289. if (gtt == NULL)
  290. return NULL;
  291. gtt->ttm.ttm.func = &virtio_gpu_backend_func;
  292. gtt->vgdev = vgdev;
  293. if (ttm_dma_tt_init(&gtt->ttm, bo, page_flags)) {
  294. kfree(gtt);
  295. return NULL;
  296. }
  297. return &gtt->ttm.ttm;
  298. }
  299. static void virtio_gpu_move_null(struct ttm_buffer_object *bo,
  300. struct ttm_mem_reg *new_mem)
  301. {
  302. struct ttm_mem_reg *old_mem = &bo->mem;
  303. BUG_ON(old_mem->mm_node != NULL);
  304. *old_mem = *new_mem;
  305. new_mem->mm_node = NULL;
  306. }
  307. static int virtio_gpu_bo_move(struct ttm_buffer_object *bo, bool evict,
  308. struct ttm_operation_ctx *ctx,
  309. struct ttm_mem_reg *new_mem)
  310. {
  311. int ret;
  312. ret = ttm_bo_wait(bo, ctx->interruptible, ctx->no_wait_gpu);
  313. if (ret)
  314. return ret;
  315. virtio_gpu_move_null(bo, new_mem);
  316. return 0;
  317. }
  318. static void virtio_gpu_bo_move_notify(struct ttm_buffer_object *tbo,
  319. bool evict,
  320. struct ttm_mem_reg *new_mem)
  321. {
  322. struct virtio_gpu_object *bo;
  323. struct virtio_gpu_device *vgdev;
  324. bo = container_of(tbo, struct virtio_gpu_object, tbo);
  325. vgdev = (struct virtio_gpu_device *)bo->gem_base.dev->dev_private;
  326. if (!new_mem || (new_mem->placement & TTM_PL_FLAG_SYSTEM)) {
  327. if (bo->hw_res_handle)
  328. virtio_gpu_cmd_resource_inval_backing(vgdev,
  329. bo->hw_res_handle);
  330. } else if (new_mem->placement & TTM_PL_FLAG_TT) {
  331. if (bo->hw_res_handle) {
  332. virtio_gpu_object_attach(vgdev, bo, bo->hw_res_handle,
  333. NULL);
  334. }
  335. }
  336. }
  337. static void virtio_gpu_bo_swap_notify(struct ttm_buffer_object *tbo)
  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 (bo->pages)
  344. virtio_gpu_object_free_sg_table(bo);
  345. }
  346. static struct ttm_bo_driver virtio_gpu_bo_driver = {
  347. .ttm_tt_create = &virtio_gpu_ttm_tt_create,
  348. .invalidate_caches = &virtio_gpu_invalidate_caches,
  349. .init_mem_type = &virtio_gpu_init_mem_type,
  350. .eviction_valuable = ttm_bo_eviction_valuable,
  351. .evict_flags = &virtio_gpu_evict_flags,
  352. .move = &virtio_gpu_bo_move,
  353. .verify_access = &virtio_gpu_verify_access,
  354. .io_mem_reserve = &virtio_gpu_ttm_io_mem_reserve,
  355. .io_mem_free = &virtio_gpu_ttm_io_mem_free,
  356. .move_notify = &virtio_gpu_bo_move_notify,
  357. .swap_notify = &virtio_gpu_bo_swap_notify,
  358. };
  359. int virtio_gpu_ttm_init(struct virtio_gpu_device *vgdev)
  360. {
  361. int r;
  362. r = virtio_gpu_ttm_global_init(vgdev);
  363. if (r)
  364. return r;
  365. /* No others user of address space so set it to 0 */
  366. r = ttm_bo_device_init(&vgdev->mman.bdev,
  367. vgdev->mman.bo_global_ref.ref.object,
  368. &virtio_gpu_bo_driver,
  369. vgdev->ddev->anon_inode->i_mapping,
  370. DRM_FILE_PAGE_OFFSET, 0);
  371. if (r) {
  372. DRM_ERROR("failed initializing buffer object driver(%d).\n", r);
  373. goto err_dev_init;
  374. }
  375. r = ttm_bo_init_mm(&vgdev->mman.bdev, TTM_PL_TT, 0);
  376. if (r) {
  377. DRM_ERROR("Failed initializing GTT heap.\n");
  378. goto err_mm_init;
  379. }
  380. return 0;
  381. err_mm_init:
  382. ttm_bo_device_release(&vgdev->mman.bdev);
  383. err_dev_init:
  384. virtio_gpu_ttm_global_fini(vgdev);
  385. return r;
  386. }
  387. void virtio_gpu_ttm_fini(struct virtio_gpu_device *vgdev)
  388. {
  389. ttm_bo_device_release(&vgdev->mman.bdev);
  390. virtio_gpu_ttm_global_fini(vgdev);
  391. DRM_INFO("virtio_gpu: ttm finalized\n");
  392. }