virtgpu_object.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Copyright (C) 2015 Red Hat, Inc.
  3. * All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * The above copyright notice and this permission notice (including the
  14. * next paragraph) shall be included in all copies or substantial
  15. * portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  20. * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
  21. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  22. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. */
  25. #include "virtgpu_drv.h"
  26. static void virtio_gpu_ttm_bo_destroy(struct ttm_buffer_object *tbo)
  27. {
  28. struct virtio_gpu_object *bo;
  29. struct virtio_gpu_device *vgdev;
  30. bo = container_of(tbo, struct virtio_gpu_object, tbo);
  31. vgdev = (struct virtio_gpu_device *)bo->gem_base.dev->dev_private;
  32. if (bo->hw_res_handle)
  33. virtio_gpu_cmd_unref_resource(vgdev, bo->hw_res_handle);
  34. if (bo->pages)
  35. virtio_gpu_object_free_sg_table(bo);
  36. if (bo->vmap)
  37. virtio_gpu_object_kunmap(bo);
  38. drm_gem_object_release(&bo->gem_base);
  39. kfree(bo);
  40. }
  41. static void virtio_gpu_init_ttm_placement(struct virtio_gpu_object *vgbo,
  42. bool pinned)
  43. {
  44. u32 c = 1;
  45. u32 pflag = pinned ? TTM_PL_FLAG_NO_EVICT : 0;
  46. vgbo->placement.placement = &vgbo->placement_code;
  47. vgbo->placement.busy_placement = &vgbo->placement_code;
  48. vgbo->placement_code.fpfn = 0;
  49. vgbo->placement_code.lpfn = 0;
  50. vgbo->placement_code.flags =
  51. TTM_PL_MASK_CACHING | TTM_PL_FLAG_TT | pflag;
  52. vgbo->placement.num_placement = c;
  53. vgbo->placement.num_busy_placement = c;
  54. }
  55. int virtio_gpu_object_create(struct virtio_gpu_device *vgdev,
  56. unsigned long size, bool kernel, bool pinned,
  57. struct virtio_gpu_object **bo_ptr)
  58. {
  59. struct virtio_gpu_object *bo;
  60. enum ttm_bo_type type;
  61. size_t acc_size;
  62. int ret;
  63. if (kernel)
  64. type = ttm_bo_type_kernel;
  65. else
  66. type = ttm_bo_type_device;
  67. *bo_ptr = NULL;
  68. acc_size = ttm_bo_dma_acc_size(&vgdev->mman.bdev, size,
  69. sizeof(struct virtio_gpu_object));
  70. bo = kzalloc(sizeof(struct virtio_gpu_object), GFP_KERNEL);
  71. if (bo == NULL)
  72. return -ENOMEM;
  73. size = roundup(size, PAGE_SIZE);
  74. ret = drm_gem_object_init(vgdev->ddev, &bo->gem_base, size);
  75. if (ret != 0) {
  76. kfree(bo);
  77. return ret;
  78. }
  79. bo->dumb = false;
  80. virtio_gpu_init_ttm_placement(bo, pinned);
  81. ret = ttm_bo_init(&vgdev->mman.bdev, &bo->tbo, size, type,
  82. &bo->placement, 0, !kernel, acc_size,
  83. NULL, NULL, &virtio_gpu_ttm_bo_destroy);
  84. /* ttm_bo_init failure will call the destroy */
  85. if (ret != 0)
  86. return ret;
  87. *bo_ptr = bo;
  88. return 0;
  89. }
  90. void virtio_gpu_object_kunmap(struct virtio_gpu_object *bo)
  91. {
  92. bo->vmap = NULL;
  93. ttm_bo_kunmap(&bo->kmap);
  94. }
  95. int virtio_gpu_object_kmap(struct virtio_gpu_object *bo)
  96. {
  97. bool is_iomem;
  98. int r;
  99. WARN_ON(bo->vmap);
  100. r = ttm_bo_kmap(&bo->tbo, 0, bo->tbo.num_pages, &bo->kmap);
  101. if (r)
  102. return r;
  103. bo->vmap = ttm_kmap_obj_virtual(&bo->kmap, &is_iomem);
  104. return 0;
  105. }
  106. int virtio_gpu_object_get_sg_table(struct virtio_gpu_device *qdev,
  107. struct virtio_gpu_object *bo)
  108. {
  109. int ret;
  110. struct page **pages = bo->tbo.ttm->pages;
  111. int nr_pages = bo->tbo.num_pages;
  112. struct ttm_operation_ctx ctx = {
  113. .interruptible = false,
  114. .no_wait_gpu = false
  115. };
  116. /* wtf swapping */
  117. if (bo->pages)
  118. return 0;
  119. if (bo->tbo.ttm->state == tt_unpopulated)
  120. bo->tbo.ttm->bdev->driver->ttm_tt_populate(bo->tbo.ttm, &ctx);
  121. bo->pages = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
  122. if (!bo->pages)
  123. goto out;
  124. ret = sg_alloc_table_from_pages(bo->pages, pages, nr_pages, 0,
  125. nr_pages << PAGE_SHIFT, GFP_KERNEL);
  126. if (ret)
  127. goto out;
  128. return 0;
  129. out:
  130. kfree(bo->pages);
  131. bo->pages = NULL;
  132. return -ENOMEM;
  133. }
  134. void virtio_gpu_object_free_sg_table(struct virtio_gpu_object *bo)
  135. {
  136. sg_free_table(bo->pages);
  137. kfree(bo->pages);
  138. bo->pages = NULL;
  139. }
  140. int virtio_gpu_object_wait(struct virtio_gpu_object *bo, bool no_wait)
  141. {
  142. int r;
  143. r = ttm_bo_reserve(&bo->tbo, true, no_wait, NULL);
  144. if (unlikely(r != 0))
  145. return r;
  146. r = ttm_bo_wait(&bo->tbo, true, no_wait);
  147. ttm_bo_unreserve(&bo->tbo);
  148. return r;
  149. }