intel_region_ttm.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. // SPDX-License-Identifier: MIT
  2. /*
  3. * Copyright © 2021 Intel Corporation
  4. */
  5. #include <drm/ttm/ttm_device.h>
  6. #include <drm/ttm/ttm_range_manager.h>
  7. #include "i915_drv.h"
  8. #include "i915_scatterlist.h"
  9. #include "i915_ttm_buddy_manager.h"
  10. #include "intel_region_ttm.h"
  11. #include "gem/i915_gem_region.h"
  12. #include "gem/i915_gem_ttm.h" /* For the funcs/ops export only */
  13. /**
  14. * DOC: TTM support structure
  15. *
  16. * The code in this file deals with setting up memory managers for TTM
  17. * LMEM and MOCK regions and converting the output from
  18. * the managers to struct sg_table, Basically providing the mapping from
  19. * i915 GEM regions to TTM memory types and resource managers.
  20. */
  21. /**
  22. * intel_region_ttm_device_init - Initialize a TTM device
  23. * @dev_priv: Pointer to an i915 device private structure.
  24. *
  25. * Return: 0 on success, negative error code on failure.
  26. */
  27. int intel_region_ttm_device_init(struct drm_i915_private *dev_priv)
  28. {
  29. struct drm_device *drm = &dev_priv->drm;
  30. return ttm_device_init(&dev_priv->bdev, i915_ttm_driver(),
  31. drm->dev, drm->anon_inode->i_mapping,
  32. drm->vma_offset_manager, 0);
  33. }
  34. /**
  35. * intel_region_ttm_device_fini - Finalize a TTM device
  36. * @dev_priv: Pointer to an i915 device private structure.
  37. */
  38. void intel_region_ttm_device_fini(struct drm_i915_private *dev_priv)
  39. {
  40. ttm_device_fini(&dev_priv->bdev);
  41. }
  42. /*
  43. * Map the i915 memory regions to TTM memory types. We use the
  44. * driver-private types for now, reserving TTM_PL_VRAM for stolen
  45. * memory and TTM_PL_TT for GGTT use if decided to implement this.
  46. */
  47. int intel_region_to_ttm_type(const struct intel_memory_region *mem)
  48. {
  49. int type;
  50. GEM_BUG_ON(mem->type != INTEL_MEMORY_LOCAL &&
  51. mem->type != INTEL_MEMORY_MOCK &&
  52. mem->type != INTEL_MEMORY_SYSTEM);
  53. if (mem->type == INTEL_MEMORY_SYSTEM)
  54. return TTM_PL_SYSTEM;
  55. type = mem->instance + TTM_PL_PRIV;
  56. GEM_BUG_ON(type >= TTM_NUM_MEM_TYPES);
  57. return type;
  58. }
  59. /**
  60. * intel_region_ttm_init - Initialize a memory region for TTM.
  61. * @mem: The region to initialize.
  62. *
  63. * This function initializes a suitable TTM resource manager for the
  64. * region, and if it's a LMEM region type, attaches it to the TTM
  65. * device. MOCK regions are NOT attached to the TTM device, since we don't
  66. * have one for the mock selftests.
  67. *
  68. * Return: 0 on success, negative error code on failure.
  69. */
  70. int intel_region_ttm_init(struct intel_memory_region *mem)
  71. {
  72. struct ttm_device *bdev = &mem->i915->bdev;
  73. int mem_type = intel_region_to_ttm_type(mem);
  74. int ret;
  75. ret = i915_ttm_buddy_man_init(bdev, mem_type, false,
  76. resource_size(&mem->region),
  77. resource_size(&mem->io),
  78. mem->min_page_size, PAGE_SIZE);
  79. if (ret)
  80. return ret;
  81. mem->region_private = ttm_manager_type(bdev, mem_type);
  82. return 0;
  83. }
  84. /**
  85. * intel_region_ttm_fini - Finalize a TTM region.
  86. * @mem: The memory region
  87. *
  88. * This functions takes down the TTM resource manager associated with the
  89. * memory region, and if it was registered with the TTM device,
  90. * removes that registration.
  91. */
  92. int intel_region_ttm_fini(struct intel_memory_region *mem)
  93. {
  94. struct ttm_resource_manager *man = mem->region_private;
  95. int ret = -EBUSY;
  96. int count;
  97. /*
  98. * Put the region's move fences. This releases requests that
  99. * may hold on to contexts and vms that may hold on to buffer
  100. * objects placed in this region.
  101. */
  102. if (man)
  103. ttm_resource_manager_cleanup(man);
  104. /* Flush objects from region. */
  105. for (count = 0; count < 10; ++count) {
  106. i915_gem_flush_free_objects(mem->i915);
  107. mutex_lock(&mem->objects.lock);
  108. if (list_empty(&mem->objects.list))
  109. ret = 0;
  110. mutex_unlock(&mem->objects.lock);
  111. if (!ret)
  112. break;
  113. msleep(20);
  114. drain_workqueue(mem->i915->bdev.wq);
  115. }
  116. /* If we leaked objects, Don't free the region causing use after free */
  117. if (ret || !man)
  118. return ret;
  119. ret = i915_ttm_buddy_man_fini(&mem->i915->bdev,
  120. intel_region_to_ttm_type(mem));
  121. GEM_WARN_ON(ret);
  122. mem->region_private = NULL;
  123. return ret;
  124. }
  125. /**
  126. * intel_region_ttm_resource_to_rsgt -
  127. * Convert an opaque TTM resource manager resource to a refcounted sg_table.
  128. * @mem: The memory region.
  129. * @res: The resource manager resource obtained from the TTM resource manager.
  130. * @page_alignment: Required page alignment for each sg entry. Power of two.
  131. *
  132. * The gem backends typically use sg-tables for operations on the underlying
  133. * io_memory. So provide a way for the backends to translate the
  134. * nodes they are handed from TTM to sg-tables.
  135. *
  136. * Return: A malloced sg_table on success, an error pointer on failure.
  137. */
  138. struct i915_refct_sgt *
  139. intel_region_ttm_resource_to_rsgt(struct intel_memory_region *mem,
  140. struct ttm_resource *res,
  141. u32 page_alignment)
  142. {
  143. if (mem->is_range_manager) {
  144. struct ttm_range_mgr_node *range_node =
  145. to_ttm_range_mgr_node(res);
  146. return i915_rsgt_from_mm_node(&range_node->mm_nodes[0],
  147. mem->region.start,
  148. page_alignment);
  149. } else {
  150. return i915_rsgt_from_buddy_resource(res, mem->region.start,
  151. page_alignment);
  152. }
  153. }
  154. #ifdef CONFIG_DRM_I915_SELFTEST
  155. /**
  156. * intel_region_ttm_resource_alloc - Allocate memory resources from a region
  157. * @mem: The memory region,
  158. * @offset: BO offset
  159. * @size: The requested size in bytes
  160. * @flags: Allocation flags
  161. *
  162. * This functionality is provided only for callers that need to allocate
  163. * memory from standalone TTM range managers, without the TTM eviction
  164. * functionality. Don't use if you are not completely sure that's the
  165. * case. The returned opaque node can be converted to an sg_table using
  166. * intel_region_ttm_resource_to_st(), and can be freed using
  167. * intel_region_ttm_resource_free().
  168. *
  169. * Return: A valid pointer on success, an error pointer on failure.
  170. */
  171. struct ttm_resource *
  172. intel_region_ttm_resource_alloc(struct intel_memory_region *mem,
  173. resource_size_t offset,
  174. resource_size_t size,
  175. unsigned int flags)
  176. {
  177. struct ttm_resource_manager *man = mem->region_private;
  178. struct ttm_place place = {};
  179. struct ttm_buffer_object mock_bo = {};
  180. struct ttm_resource *res;
  181. int ret;
  182. if (flags & I915_BO_ALLOC_CONTIGUOUS)
  183. place.flags |= TTM_PL_FLAG_CONTIGUOUS;
  184. if (offset != I915_BO_INVALID_OFFSET) {
  185. if (WARN_ON(overflows_type(offset >> PAGE_SHIFT, place.fpfn))) {
  186. ret = -E2BIG;
  187. goto out;
  188. }
  189. place.fpfn = offset >> PAGE_SHIFT;
  190. if (WARN_ON(overflows_type(place.fpfn + (size >> PAGE_SHIFT), place.lpfn))) {
  191. ret = -E2BIG;
  192. goto out;
  193. }
  194. place.lpfn = place.fpfn + (size >> PAGE_SHIFT);
  195. } else if (resource_size(&mem->io) && resource_size(&mem->io) < mem->total) {
  196. if (flags & I915_BO_ALLOC_GPU_ONLY) {
  197. place.flags |= TTM_PL_FLAG_TOPDOWN;
  198. } else {
  199. place.fpfn = 0;
  200. if (WARN_ON(overflows_type(resource_size(&mem->io) >> PAGE_SHIFT, place.lpfn))) {
  201. ret = -E2BIG;
  202. goto out;
  203. }
  204. place.lpfn = resource_size(&mem->io) >> PAGE_SHIFT;
  205. }
  206. }
  207. mock_bo.base.size = size;
  208. mock_bo.bdev = &mem->i915->bdev;
  209. ret = man->func->alloc(man, &mock_bo, &place, &res);
  210. out:
  211. if (ret == -ENOSPC)
  212. ret = -ENXIO;
  213. if (!ret)
  214. res->bo = NULL; /* Rather blow up, then some uaf */
  215. return ret ? ERR_PTR(ret) : res;
  216. }
  217. #endif
  218. /**
  219. * intel_region_ttm_resource_free - Free a resource allocated from a resource manager
  220. * @mem: The region the resource was allocated from.
  221. * @res: The opaque resource representing an allocation.
  222. */
  223. void intel_region_ttm_resource_free(struct intel_memory_region *mem,
  224. struct ttm_resource *res)
  225. {
  226. struct ttm_resource_manager *man = mem->region_private;
  227. struct ttm_buffer_object mock_bo = {};
  228. mock_bo.base.size = res->size;
  229. mock_bo.bdev = &mem->i915->bdev;
  230. res->bo = &mock_bo;
  231. man->func->free(man, res);
  232. }