virtgpu_object.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. drm_gem_object_release(&bo->gem_base);
  37. kfree(bo);
  38. }
  39. static void virtio_gpu_init_ttm_placement(struct virtio_gpu_object *vgbo,
  40. bool pinned)
  41. {
  42. u32 c = 1;
  43. u32 pflag = pinned ? TTM_PL_FLAG_NO_EVICT : 0;
  44. vgbo->placement.placement = &vgbo->placement_code;
  45. vgbo->placement.busy_placement = &vgbo->placement_code;
  46. vgbo->placement_code.fpfn = 0;
  47. vgbo->placement_code.lpfn = 0;
  48. vgbo->placement_code.flags =
  49. TTM_PL_MASK_CACHING | TTM_PL_FLAG_TT | pflag;
  50. vgbo->placement.num_placement = c;
  51. vgbo->placement.num_busy_placement = c;
  52. }
  53. int virtio_gpu_object_create(struct virtio_gpu_device *vgdev,
  54. unsigned long size, bool kernel, bool pinned,
  55. struct virtio_gpu_object **bo_ptr)
  56. {
  57. struct virtio_gpu_object *bo;
  58. enum ttm_bo_type type;
  59. size_t acc_size;
  60. int ret;
  61. if (kernel)
  62. type = ttm_bo_type_kernel;
  63. else
  64. type = ttm_bo_type_device;
  65. *bo_ptr = NULL;
  66. acc_size = ttm_bo_dma_acc_size(&vgdev->mman.bdev, size,
  67. sizeof(struct virtio_gpu_object));
  68. bo = kzalloc(sizeof(struct virtio_gpu_object), GFP_KERNEL);
  69. if (bo == NULL)
  70. return -ENOMEM;
  71. size = roundup(size, PAGE_SIZE);
  72. ret = drm_gem_object_init(vgdev->ddev, &bo->gem_base, size);
  73. if (ret != 0)
  74. return ret;
  75. bo->dumb = false;
  76. virtio_gpu_init_ttm_placement(bo, pinned);
  77. ret = ttm_bo_init(&vgdev->mman.bdev, &bo->tbo, size, type,
  78. &bo->placement, 0, !kernel, NULL, acc_size,
  79. NULL, NULL, &virtio_gpu_ttm_bo_destroy);
  80. /* ttm_bo_init failure will call the destroy */
  81. if (ret != 0)
  82. return ret;
  83. *bo_ptr = bo;
  84. return 0;
  85. }
  86. int virtio_gpu_object_kmap(struct virtio_gpu_object *bo, void **ptr)
  87. {
  88. bool is_iomem;
  89. int r;
  90. if (bo->vmap) {
  91. if (ptr)
  92. *ptr = bo->vmap;
  93. return 0;
  94. }
  95. r = ttm_bo_kmap(&bo->tbo, 0, bo->tbo.num_pages, &bo->kmap);
  96. if (r)
  97. return r;
  98. bo->vmap = ttm_kmap_obj_virtual(&bo->kmap, &is_iomem);
  99. if (ptr)
  100. *ptr = bo->vmap;
  101. return 0;
  102. }
  103. int virtio_gpu_object_get_sg_table(struct virtio_gpu_device *qdev,
  104. struct virtio_gpu_object *bo)
  105. {
  106. int ret;
  107. struct page **pages = bo->tbo.ttm->pages;
  108. int nr_pages = bo->tbo.num_pages;
  109. /* wtf swapping */
  110. if (bo->pages)
  111. return 0;
  112. if (bo->tbo.ttm->state == tt_unpopulated)
  113. bo->tbo.ttm->bdev->driver->ttm_tt_populate(bo->tbo.ttm);
  114. bo->pages = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
  115. if (!bo->pages)
  116. goto out;
  117. ret = sg_alloc_table_from_pages(bo->pages, pages, nr_pages, 0,
  118. nr_pages << PAGE_SHIFT, GFP_KERNEL);
  119. if (ret)
  120. goto out;
  121. return 0;
  122. out:
  123. kfree(bo->pages);
  124. bo->pages = NULL;
  125. return -ENOMEM;
  126. }
  127. void virtio_gpu_object_free_sg_table(struct virtio_gpu_object *bo)
  128. {
  129. sg_free_table(bo->pages);
  130. kfree(bo->pages);
  131. bo->pages = NULL;
  132. }
  133. int virtio_gpu_object_wait(struct virtio_gpu_object *bo, bool no_wait)
  134. {
  135. int r;
  136. r = ttm_bo_reserve(&bo->tbo, true, no_wait, NULL);
  137. if (unlikely(r != 0))
  138. return r;
  139. r = ttm_bo_wait(&bo->tbo, true, no_wait);
  140. ttm_bo_unreserve(&bo->tbo);
  141. return r;
  142. }