ttm_resource.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /*
  2. * Copyright 2020 Advanced Micro Devices, Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Authors: Christian König
  23. */
  24. #ifndef _TTM_RESOURCE_H_
  25. #define _TTM_RESOURCE_H_
  26. #include <linux/types.h>
  27. #include <linux/list.h>
  28. #include <linux/mutex.h>
  29. #include <linux/iosys-map.h>
  30. #include <linux/dma-fence.h>
  31. #include <drm/drm_print.h>
  32. #include <drm/ttm/ttm_caching.h>
  33. #include <drm/ttm/ttm_kmap_iter.h>
  34. #define TTM_MAX_BO_PRIORITY 4U
  35. #define TTM_NUM_MEM_TYPES 8
  36. struct ttm_device;
  37. struct ttm_resource_manager;
  38. struct ttm_resource;
  39. struct ttm_place;
  40. struct ttm_buffer_object;
  41. struct ttm_placement;
  42. struct iosys_map;
  43. struct io_mapping;
  44. struct sg_table;
  45. struct scatterlist;
  46. struct ttm_resource_manager_func {
  47. /**
  48. * struct ttm_resource_manager_func member alloc
  49. *
  50. * @man: Pointer to a memory type manager.
  51. * @bo: Pointer to the buffer object we're allocating space for.
  52. * @place: Placement details.
  53. * @res: Resulting pointer to the ttm_resource.
  54. *
  55. * This function should allocate space in the memory type managed
  56. * by @man. Placement details if applicable are given by @place. If
  57. * successful, a filled in ttm_resource object should be returned in
  58. * @res. @res::start should be set to a value identifying the beginning
  59. * of the range allocated, and the function should return zero.
  60. * If the manager can't fulfill the request -ENOSPC should be returned.
  61. * If a system error occurred, preventing the request to be fulfilled,
  62. * the function should return a negative error code.
  63. *
  64. * This function may not be called from within atomic context and needs
  65. * to take care of its own locking to protect any data structures
  66. * managing the space.
  67. */
  68. int (*alloc)(struct ttm_resource_manager *man,
  69. struct ttm_buffer_object *bo,
  70. const struct ttm_place *place,
  71. struct ttm_resource **res);
  72. /**
  73. * struct ttm_resource_manager_func member free
  74. *
  75. * @man: Pointer to a memory type manager.
  76. * @res: Pointer to a struct ttm_resource to be freed.
  77. *
  78. * This function frees memory type resources previously allocated.
  79. * May not be called from within atomic context.
  80. */
  81. void (*free)(struct ttm_resource_manager *man,
  82. struct ttm_resource *res);
  83. /**
  84. * struct ttm_resource_manager_func member intersects
  85. *
  86. * @man: Pointer to a memory type manager.
  87. * @res: Pointer to a struct ttm_resource to be checked.
  88. * @place: Placement to check against.
  89. * @size: Size of the check.
  90. *
  91. * Test if @res intersects with @place + @size. Used to judge if
  92. * evictions are valueable or not.
  93. */
  94. bool (*intersects)(struct ttm_resource_manager *man,
  95. struct ttm_resource *res,
  96. const struct ttm_place *place,
  97. size_t size);
  98. /**
  99. * struct ttm_resource_manager_func member compatible
  100. *
  101. * @man: Pointer to a memory type manager.
  102. * @res: Pointer to a struct ttm_resource to be checked.
  103. * @place: Placement to check against.
  104. * @size: Size of the check.
  105. *
  106. * Test if @res compatible with @place + @size. Used to check of
  107. * the need to move the backing store or not.
  108. */
  109. bool (*compatible)(struct ttm_resource_manager *man,
  110. struct ttm_resource *res,
  111. const struct ttm_place *place,
  112. size_t size);
  113. /**
  114. * struct ttm_resource_manager_func member debug
  115. *
  116. * @man: Pointer to a memory type manager.
  117. * @printer: Prefix to be used in printout to identify the caller.
  118. *
  119. * This function is called to print out the state of the memory
  120. * type manager to aid debugging of out-of-memory conditions.
  121. * It may not be called from within atomic context.
  122. */
  123. void (*debug)(struct ttm_resource_manager *man,
  124. struct drm_printer *printer);
  125. };
  126. /**
  127. * struct ttm_resource_manager
  128. *
  129. * @use_type: The memory type is enabled.
  130. * @use_tt: If a TT object should be used for the backing store.
  131. * @size: Size of the managed region.
  132. * @bdev: ttm device this manager belongs to
  133. * @func: structure pointer implementing the range manager. See above
  134. * @move_lock: lock for move fence
  135. * @move: The fence of the last pipelined move operation.
  136. * @lru: The lru list for this memory type.
  137. *
  138. * This structure is used to identify and manage memory types for a device.
  139. */
  140. struct ttm_resource_manager {
  141. /*
  142. * No protection. Constant from start.
  143. */
  144. bool use_type;
  145. bool use_tt;
  146. struct ttm_device *bdev;
  147. uint64_t size;
  148. const struct ttm_resource_manager_func *func;
  149. spinlock_t move_lock;
  150. /*
  151. * Protected by @move_lock.
  152. */
  153. struct dma_fence *move;
  154. /*
  155. * Protected by the bdev->lru_lock.
  156. */
  157. struct list_head lru[TTM_MAX_BO_PRIORITY];
  158. /**
  159. * @usage: How much of the resources are used, protected by the
  160. * bdev->lru_lock.
  161. */
  162. uint64_t usage;
  163. };
  164. /**
  165. * struct ttm_bus_placement
  166. *
  167. * @addr: mapped virtual address
  168. * @offset: physical addr
  169. * @is_iomem: is this io memory ?
  170. * @caching: See enum ttm_caching
  171. *
  172. * Structure indicating the bus placement of an object.
  173. */
  174. struct ttm_bus_placement {
  175. void *addr;
  176. phys_addr_t offset;
  177. bool is_iomem;
  178. enum ttm_caching caching;
  179. };
  180. /**
  181. * struct ttm_resource
  182. *
  183. * @start: Start of the allocation.
  184. * @num_pages: Actual size of resource in pages.
  185. * @mem_type: Resource type of the allocation.
  186. * @placement: Placement flags.
  187. * @bus: Placement on io bus accessible to the CPU
  188. * @bo: weak reference to the BO, protected by ttm_device::lru_lock
  189. *
  190. * Structure indicating the placement and space resources used by a
  191. * buffer object.
  192. */
  193. struct ttm_resource {
  194. unsigned long start;
  195. unsigned long num_pages;
  196. uint32_t mem_type;
  197. uint32_t placement;
  198. struct ttm_bus_placement bus;
  199. struct ttm_buffer_object *bo;
  200. /**
  201. * @lru: Least recently used list, see &ttm_resource_manager.lru
  202. */
  203. struct list_head lru;
  204. };
  205. /**
  206. * struct ttm_resource_cursor
  207. *
  208. * @priority: the current priority
  209. *
  210. * Cursor to iterate over the resources in a manager.
  211. */
  212. struct ttm_resource_cursor {
  213. unsigned int priority;
  214. };
  215. /**
  216. * struct ttm_lru_bulk_move_pos
  217. *
  218. * @first: first res in the bulk move range
  219. * @last: last res in the bulk move range
  220. *
  221. * Range of resources for a lru bulk move.
  222. */
  223. struct ttm_lru_bulk_move_pos {
  224. struct ttm_resource *first;
  225. struct ttm_resource *last;
  226. };
  227. /**
  228. * struct ttm_lru_bulk_move
  229. *
  230. * @pos: first/last lru entry for resources in the each domain/priority
  231. *
  232. * Container for the current bulk move state. Should be used with
  233. * ttm_lru_bulk_move_init() and ttm_bo_set_bulk_move().
  234. */
  235. struct ttm_lru_bulk_move {
  236. struct ttm_lru_bulk_move_pos pos[TTM_NUM_MEM_TYPES][TTM_MAX_BO_PRIORITY];
  237. };
  238. /**
  239. * struct ttm_kmap_iter_iomap - Specialization for a struct io_mapping +
  240. * struct sg_table backed struct ttm_resource.
  241. * @base: Embedded struct ttm_kmap_iter providing the usage interface.
  242. * @iomap: struct io_mapping representing the underlying linear io_memory.
  243. * @st: sg_table into @iomap, representing the memory of the struct ttm_resource.
  244. * @start: Offset that needs to be subtracted from @st to make
  245. * sg_dma_address(st->sgl) - @start == 0 for @iomap start.
  246. * @cache: Scatterlist traversal cache for fast lookups.
  247. * @cache.sg: Pointer to the currently cached scatterlist segment.
  248. * @cache.i: First index of @sg. PAGE_SIZE granularity.
  249. * @cache.end: Last index + 1 of @sg. PAGE_SIZE granularity.
  250. * @cache.offs: First offset into @iomap of @sg. PAGE_SIZE granularity.
  251. */
  252. struct ttm_kmap_iter_iomap {
  253. struct ttm_kmap_iter base;
  254. struct io_mapping *iomap;
  255. struct sg_table *st;
  256. resource_size_t start;
  257. struct {
  258. struct scatterlist *sg;
  259. pgoff_t i;
  260. pgoff_t end;
  261. pgoff_t offs;
  262. } cache;
  263. };
  264. /**
  265. * struct ttm_kmap_iter_linear_io - Iterator specialization for linear io
  266. * @base: The base iterator
  267. * @dmap: Points to the starting address of the region
  268. * @needs_unmap: Whether we need to unmap on fini
  269. */
  270. struct ttm_kmap_iter_linear_io {
  271. struct ttm_kmap_iter base;
  272. struct iosys_map dmap;
  273. bool needs_unmap;
  274. };
  275. /**
  276. * ttm_resource_manager_set_used
  277. *
  278. * @man: A memory manager object.
  279. * @used: usage state to set.
  280. *
  281. * Set the manager in use flag. If disabled the manager is no longer
  282. * used for object placement.
  283. */
  284. static inline void
  285. ttm_resource_manager_set_used(struct ttm_resource_manager *man, bool used)
  286. {
  287. int i;
  288. for (i = 0; i < TTM_MAX_BO_PRIORITY; i++)
  289. WARN_ON(!list_empty(&man->lru[i]));
  290. man->use_type = used;
  291. }
  292. /**
  293. * ttm_resource_manager_used
  294. *
  295. * @man: Manager to get used state for
  296. *
  297. * Get the in use flag for a manager.
  298. * Returns:
  299. * true is used, false if not.
  300. */
  301. static inline bool ttm_resource_manager_used(struct ttm_resource_manager *man)
  302. {
  303. return man->use_type;
  304. }
  305. /**
  306. * ttm_resource_manager_cleanup
  307. *
  308. * @man: A memory manager object.
  309. *
  310. * Cleanup the move fences from the memory manager object.
  311. */
  312. static inline void
  313. ttm_resource_manager_cleanup(struct ttm_resource_manager *man)
  314. {
  315. dma_fence_put(man->move);
  316. man->move = NULL;
  317. }
  318. void ttm_lru_bulk_move_init(struct ttm_lru_bulk_move *bulk);
  319. void ttm_lru_bulk_move_tail(struct ttm_lru_bulk_move *bulk);
  320. void ttm_resource_add_bulk_move(struct ttm_resource *res,
  321. struct ttm_buffer_object *bo);
  322. void ttm_resource_del_bulk_move(struct ttm_resource *res,
  323. struct ttm_buffer_object *bo);
  324. void ttm_resource_move_to_lru_tail(struct ttm_resource *res);
  325. void ttm_resource_init(struct ttm_buffer_object *bo,
  326. const struct ttm_place *place,
  327. struct ttm_resource *res);
  328. void ttm_resource_fini(struct ttm_resource_manager *man,
  329. struct ttm_resource *res);
  330. int ttm_resource_alloc(struct ttm_buffer_object *bo,
  331. const struct ttm_place *place,
  332. struct ttm_resource **res);
  333. void ttm_resource_free(struct ttm_buffer_object *bo, struct ttm_resource **res);
  334. bool ttm_resource_intersects(struct ttm_device *bdev,
  335. struct ttm_resource *res,
  336. const struct ttm_place *place,
  337. size_t size);
  338. bool ttm_resource_compatible(struct ttm_device *bdev,
  339. struct ttm_resource *res,
  340. const struct ttm_place *place,
  341. size_t size);
  342. bool ttm_resource_compat(struct ttm_resource *res,
  343. struct ttm_placement *placement);
  344. void ttm_resource_set_bo(struct ttm_resource *res,
  345. struct ttm_buffer_object *bo);
  346. void ttm_resource_manager_init(struct ttm_resource_manager *man,
  347. struct ttm_device *bdev,
  348. uint64_t size);
  349. int ttm_resource_manager_evict_all(struct ttm_device *bdev,
  350. struct ttm_resource_manager *man);
  351. uint64_t ttm_resource_manager_usage(struct ttm_resource_manager *man);
  352. void ttm_resource_manager_debug(struct ttm_resource_manager *man,
  353. struct drm_printer *p);
  354. struct ttm_resource *
  355. ttm_resource_manager_first(struct ttm_resource_manager *man,
  356. struct ttm_resource_cursor *cursor);
  357. struct ttm_resource *
  358. ttm_resource_manager_next(struct ttm_resource_manager *man,
  359. struct ttm_resource_cursor *cursor,
  360. struct ttm_resource *res);
  361. /**
  362. * ttm_resource_manager_for_each_res - iterate over all resources
  363. * @man: the resource manager
  364. * @cursor: struct ttm_resource_cursor for the current position
  365. * @res: the current resource
  366. *
  367. * Iterate over all the evictable resources in a resource manager.
  368. */
  369. #define ttm_resource_manager_for_each_res(man, cursor, res) \
  370. for (res = ttm_resource_manager_first(man, cursor); res; \
  371. res = ttm_resource_manager_next(man, cursor, res))
  372. struct ttm_kmap_iter *
  373. ttm_kmap_iter_iomap_init(struct ttm_kmap_iter_iomap *iter_io,
  374. struct io_mapping *iomap,
  375. struct sg_table *st,
  376. resource_size_t start);
  377. struct ttm_kmap_iter_linear_io;
  378. struct ttm_kmap_iter *
  379. ttm_kmap_iter_linear_io_init(struct ttm_kmap_iter_linear_io *iter_io,
  380. struct ttm_device *bdev,
  381. struct ttm_resource *mem);
  382. void ttm_kmap_iter_linear_io_fini(struct ttm_kmap_iter_linear_io *iter_io,
  383. struct ttm_device *bdev,
  384. struct ttm_resource *mem);
  385. void ttm_resource_manager_create_debugfs(struct ttm_resource_manager *man,
  386. struct dentry * parent,
  387. const char *name);
  388. #endif