ttm_resource.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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. /**
  47. * enum ttm_lru_item_type - enumerate ttm_lru_item subclasses
  48. */
  49. enum ttm_lru_item_type {
  50. /** @TTM_LRU_RESOURCE: The resource subclass */
  51. TTM_LRU_RESOURCE,
  52. /** @TTM_LRU_HITCH: The iterator hitch subclass */
  53. TTM_LRU_HITCH
  54. };
  55. /**
  56. * struct ttm_lru_item - The TTM lru list node base class
  57. * @link: The list link
  58. * @type: The subclass type
  59. */
  60. struct ttm_lru_item {
  61. struct list_head link;
  62. enum ttm_lru_item_type type;
  63. };
  64. /**
  65. * ttm_lru_item_init() - initialize a struct ttm_lru_item
  66. * @item: The item to initialize
  67. * @type: The subclass type
  68. */
  69. static inline void ttm_lru_item_init(struct ttm_lru_item *item,
  70. enum ttm_lru_item_type type)
  71. {
  72. item->type = type;
  73. INIT_LIST_HEAD(&item->link);
  74. }
  75. static inline bool ttm_lru_item_is_res(const struct ttm_lru_item *item)
  76. {
  77. return item->type == TTM_LRU_RESOURCE;
  78. }
  79. struct ttm_resource_manager_func {
  80. /**
  81. * struct ttm_resource_manager_func member alloc
  82. *
  83. * @man: Pointer to a memory type manager.
  84. * @bo: Pointer to the buffer object we're allocating space for.
  85. * @place: Placement details.
  86. * @res: Resulting pointer to the ttm_resource.
  87. *
  88. * This function should allocate space in the memory type managed
  89. * by @man. Placement details if applicable are given by @place. If
  90. * successful, a filled in ttm_resource object should be returned in
  91. * @res. @res::start should be set to a value identifying the beginning
  92. * of the range allocated, and the function should return zero.
  93. * If the manager can't fulfill the request -ENOSPC should be returned.
  94. * If a system error occurred, preventing the request to be fulfilled,
  95. * the function should return a negative error code.
  96. *
  97. * This function may not be called from within atomic context and needs
  98. * to take care of its own locking to protect any data structures
  99. * managing the space.
  100. */
  101. int (*alloc)(struct ttm_resource_manager *man,
  102. struct ttm_buffer_object *bo,
  103. const struct ttm_place *place,
  104. struct ttm_resource **res);
  105. /**
  106. * struct ttm_resource_manager_func member free
  107. *
  108. * @man: Pointer to a memory type manager.
  109. * @res: Pointer to a struct ttm_resource to be freed.
  110. *
  111. * This function frees memory type resources previously allocated.
  112. * May not be called from within atomic context.
  113. */
  114. void (*free)(struct ttm_resource_manager *man,
  115. struct ttm_resource *res);
  116. /**
  117. * struct ttm_resource_manager_func member intersects
  118. *
  119. * @man: Pointer to a memory type manager.
  120. * @res: Pointer to a struct ttm_resource to be checked.
  121. * @place: Placement to check against.
  122. * @size: Size of the check.
  123. *
  124. * Test if @res intersects with @place + @size. Used to judge if
  125. * evictions are valueable or not.
  126. */
  127. bool (*intersects)(struct ttm_resource_manager *man,
  128. struct ttm_resource *res,
  129. const struct ttm_place *place,
  130. size_t size);
  131. /**
  132. * struct ttm_resource_manager_func member compatible
  133. *
  134. * @man: Pointer to a memory type manager.
  135. * @res: Pointer to a struct ttm_resource to be checked.
  136. * @place: Placement to check against.
  137. * @size: Size of the check.
  138. *
  139. * Test if @res compatible with @place + @size. Used to check of
  140. * the need to move the backing store or not.
  141. */
  142. bool (*compatible)(struct ttm_resource_manager *man,
  143. struct ttm_resource *res,
  144. const struct ttm_place *place,
  145. size_t size);
  146. /**
  147. * struct ttm_resource_manager_func member debug
  148. *
  149. * @man: Pointer to a memory type manager.
  150. * @printer: Prefix to be used in printout to identify the caller.
  151. *
  152. * This function is called to print out the state of the memory
  153. * type manager to aid debugging of out-of-memory conditions.
  154. * It may not be called from within atomic context.
  155. */
  156. void (*debug)(struct ttm_resource_manager *man,
  157. struct drm_printer *printer);
  158. };
  159. /**
  160. * struct ttm_resource_manager
  161. *
  162. * @use_type: The memory type is enabled.
  163. * @use_tt: If a TT object should be used for the backing store.
  164. * @size: Size of the managed region.
  165. * @bdev: ttm device this manager belongs to
  166. * @func: structure pointer implementing the range manager. See above
  167. * @move_lock: lock for move fence
  168. * @move: The fence of the last pipelined move operation.
  169. * @lru: The lru list for this memory type.
  170. *
  171. * This structure is used to identify and manage memory types for a device.
  172. */
  173. struct ttm_resource_manager {
  174. /*
  175. * No protection. Constant from start.
  176. */
  177. bool use_type;
  178. bool use_tt;
  179. struct ttm_device *bdev;
  180. uint64_t size;
  181. const struct ttm_resource_manager_func *func;
  182. spinlock_t move_lock;
  183. /*
  184. * Protected by @move_lock.
  185. */
  186. struct dma_fence *move;
  187. /*
  188. * Protected by the bdev->lru_lock.
  189. */
  190. struct list_head lru[TTM_MAX_BO_PRIORITY];
  191. /**
  192. * @usage: How much of the resources are used, protected by the
  193. * bdev->lru_lock.
  194. */
  195. uint64_t usage;
  196. };
  197. /**
  198. * struct ttm_bus_placement
  199. *
  200. * @addr: mapped virtual address
  201. * @offset: physical addr
  202. * @is_iomem: is this io memory ?
  203. * @caching: See enum ttm_caching
  204. *
  205. * Structure indicating the bus placement of an object.
  206. */
  207. struct ttm_bus_placement {
  208. void *addr;
  209. phys_addr_t offset;
  210. bool is_iomem;
  211. enum ttm_caching caching;
  212. };
  213. /**
  214. * struct ttm_resource
  215. *
  216. * @start: Start of the allocation.
  217. * @size: Actual size of resource in bytes.
  218. * @mem_type: Resource type of the allocation.
  219. * @placement: Placement flags.
  220. * @bus: Placement on io bus accessible to the CPU
  221. * @bo: weak reference to the BO, protected by ttm_device::lru_lock
  222. *
  223. * Structure indicating the placement and space resources used by a
  224. * buffer object.
  225. */
  226. struct ttm_resource {
  227. unsigned long start;
  228. size_t size;
  229. uint32_t mem_type;
  230. uint32_t placement;
  231. struct ttm_bus_placement bus;
  232. struct ttm_buffer_object *bo;
  233. /**
  234. * @lru: Least recently used list, see &ttm_resource_manager.lru
  235. */
  236. struct ttm_lru_item lru;
  237. };
  238. /**
  239. * ttm_lru_item_to_res() - Downcast a struct ttm_lru_item to a struct ttm_resource
  240. * @item: The struct ttm_lru_item to downcast
  241. *
  242. * Return: Pointer to the embedding struct ttm_resource
  243. */
  244. static inline struct ttm_resource *
  245. ttm_lru_item_to_res(struct ttm_lru_item *item)
  246. {
  247. return container_of(item, struct ttm_resource, lru);
  248. }
  249. /**
  250. * struct ttm_lru_bulk_move_pos
  251. *
  252. * @first: first res in the bulk move range
  253. * @last: last res in the bulk move range
  254. *
  255. * Range of resources for a lru bulk move.
  256. */
  257. struct ttm_lru_bulk_move_pos {
  258. struct ttm_resource *first;
  259. struct ttm_resource *last;
  260. };
  261. /**
  262. * struct ttm_lru_bulk_move
  263. * @pos: first/last lru entry for resources in the each domain/priority
  264. * @cursor_list: The list of cursors currently traversing any of
  265. * the sublists of @pos. Protected by the ttm device's lru_lock.
  266. *
  267. * Container for the current bulk move state. Should be used with
  268. * ttm_lru_bulk_move_init() and ttm_bo_set_bulk_move().
  269. * All BOs in a bulk_move structure need to share the same reservation object to
  270. * ensure that the bulk as a whole is locked for eviction even if only one BO of
  271. * the bulk is evicted.
  272. */
  273. struct ttm_lru_bulk_move {
  274. struct ttm_lru_bulk_move_pos pos[TTM_NUM_MEM_TYPES][TTM_MAX_BO_PRIORITY];
  275. struct list_head cursor_list;
  276. };
  277. /**
  278. * struct ttm_resource_cursor
  279. * @man: The resource manager currently being iterated over
  280. * @hitch: A hitch list node inserted before the next resource
  281. * to iterate over.
  282. * @bulk_link: A list link for the list of cursors traversing the
  283. * bulk sublist of @bulk. Protected by the ttm device's lru_lock.
  284. * @bulk: Pointer to struct ttm_lru_bulk_move whose subrange @hitch is
  285. * inserted to. NULL if none. Never dereference this pointer since
  286. * the struct ttm_lru_bulk_move object pointed to might have been
  287. * freed. The pointer is only for comparison.
  288. * @mem_type: The memory type of the LRU list being traversed.
  289. * This field is valid iff @bulk != NULL.
  290. * @priority: the current priority
  291. *
  292. * Cursor to iterate over the resources in a manager.
  293. */
  294. struct ttm_resource_cursor {
  295. struct ttm_resource_manager *man;
  296. struct ttm_lru_item hitch;
  297. struct list_head bulk_link;
  298. struct ttm_lru_bulk_move *bulk;
  299. unsigned int mem_type;
  300. unsigned int priority;
  301. };
  302. void ttm_resource_cursor_fini(struct ttm_resource_cursor *cursor);
  303. /**
  304. * struct ttm_kmap_iter_iomap - Specialization for a struct io_mapping +
  305. * struct sg_table backed struct ttm_resource.
  306. * @base: Embedded struct ttm_kmap_iter providing the usage interface.
  307. * @iomap: struct io_mapping representing the underlying linear io_memory.
  308. * @st: sg_table into @iomap, representing the memory of the struct ttm_resource.
  309. * @start: Offset that needs to be subtracted from @st to make
  310. * sg_dma_address(st->sgl) - @start == 0 for @iomap start.
  311. * @cache: Scatterlist traversal cache for fast lookups.
  312. * @cache.sg: Pointer to the currently cached scatterlist segment.
  313. * @cache.i: First index of @sg. PAGE_SIZE granularity.
  314. * @cache.end: Last index + 1 of @sg. PAGE_SIZE granularity.
  315. * @cache.offs: First offset into @iomap of @sg. PAGE_SIZE granularity.
  316. */
  317. struct ttm_kmap_iter_iomap {
  318. struct ttm_kmap_iter base;
  319. struct io_mapping *iomap;
  320. struct sg_table *st;
  321. resource_size_t start;
  322. struct {
  323. struct scatterlist *sg;
  324. pgoff_t i;
  325. pgoff_t end;
  326. pgoff_t offs;
  327. } cache;
  328. };
  329. /**
  330. * struct ttm_kmap_iter_linear_io - Iterator specialization for linear io
  331. * @base: The base iterator
  332. * @dmap: Points to the starting address of the region
  333. * @needs_unmap: Whether we need to unmap on fini
  334. */
  335. struct ttm_kmap_iter_linear_io {
  336. struct ttm_kmap_iter base;
  337. struct iosys_map dmap;
  338. bool needs_unmap;
  339. };
  340. /**
  341. * ttm_resource_manager_set_used
  342. *
  343. * @man: A memory manager object.
  344. * @used: usage state to set.
  345. *
  346. * Set the manager in use flag. If disabled the manager is no longer
  347. * used for object placement.
  348. */
  349. static inline void
  350. ttm_resource_manager_set_used(struct ttm_resource_manager *man, bool used)
  351. {
  352. int i;
  353. for (i = 0; i < TTM_MAX_BO_PRIORITY; i++)
  354. WARN_ON(!list_empty(&man->lru[i]));
  355. man->use_type = used;
  356. }
  357. /**
  358. * ttm_resource_manager_used
  359. *
  360. * @man: Manager to get used state for
  361. *
  362. * Get the in use flag for a manager.
  363. * Returns:
  364. * true is used, false if not.
  365. */
  366. static inline bool ttm_resource_manager_used(struct ttm_resource_manager *man)
  367. {
  368. return man->use_type;
  369. }
  370. /**
  371. * ttm_resource_manager_cleanup
  372. *
  373. * @man: A memory manager object.
  374. *
  375. * Cleanup the move fences from the memory manager object.
  376. */
  377. static inline void
  378. ttm_resource_manager_cleanup(struct ttm_resource_manager *man)
  379. {
  380. dma_fence_put(man->move);
  381. man->move = NULL;
  382. }
  383. void ttm_lru_bulk_move_init(struct ttm_lru_bulk_move *bulk);
  384. void ttm_lru_bulk_move_tail(struct ttm_lru_bulk_move *bulk);
  385. void ttm_lru_bulk_move_fini(struct ttm_device *bdev,
  386. struct ttm_lru_bulk_move *bulk);
  387. void ttm_resource_add_bulk_move(struct ttm_resource *res,
  388. struct ttm_buffer_object *bo);
  389. void ttm_resource_del_bulk_move(struct ttm_resource *res,
  390. struct ttm_buffer_object *bo);
  391. void ttm_resource_move_to_lru_tail(struct ttm_resource *res);
  392. void ttm_resource_init(struct ttm_buffer_object *bo,
  393. const struct ttm_place *place,
  394. struct ttm_resource *res);
  395. void ttm_resource_fini(struct ttm_resource_manager *man,
  396. struct ttm_resource *res);
  397. int ttm_resource_alloc(struct ttm_buffer_object *bo,
  398. const struct ttm_place *place,
  399. struct ttm_resource **res);
  400. void ttm_resource_free(struct ttm_buffer_object *bo, struct ttm_resource **res);
  401. bool ttm_resource_intersects(struct ttm_device *bdev,
  402. struct ttm_resource *res,
  403. const struct ttm_place *place,
  404. size_t size);
  405. bool ttm_resource_compatible(struct ttm_resource *res,
  406. struct ttm_placement *placement,
  407. bool evicting);
  408. void ttm_resource_set_bo(struct ttm_resource *res,
  409. struct ttm_buffer_object *bo);
  410. void ttm_resource_manager_init(struct ttm_resource_manager *man,
  411. struct ttm_device *bdev,
  412. uint64_t size);
  413. int ttm_resource_manager_evict_all(struct ttm_device *bdev,
  414. struct ttm_resource_manager *man);
  415. uint64_t ttm_resource_manager_usage(struct ttm_resource_manager *man);
  416. void ttm_resource_manager_debug(struct ttm_resource_manager *man,
  417. struct drm_printer *p);
  418. struct ttm_resource *
  419. ttm_resource_manager_first(struct ttm_resource_manager *man,
  420. struct ttm_resource_cursor *cursor);
  421. struct ttm_resource *
  422. ttm_resource_manager_next(struct ttm_resource_cursor *cursor);
  423. struct ttm_resource *
  424. ttm_lru_first_res_or_null(struct list_head *head);
  425. /**
  426. * ttm_resource_manager_for_each_res - iterate over all resources
  427. * @man: the resource manager
  428. * @cursor: struct ttm_resource_cursor for the current position
  429. * @res: the current resource
  430. *
  431. * Iterate over all the evictable resources in a resource manager.
  432. */
  433. #define ttm_resource_manager_for_each_res(man, cursor, res) \
  434. for (res = ttm_resource_manager_first(man, cursor); res; \
  435. res = ttm_resource_manager_next(cursor))
  436. struct ttm_kmap_iter *
  437. ttm_kmap_iter_iomap_init(struct ttm_kmap_iter_iomap *iter_io,
  438. struct io_mapping *iomap,
  439. struct sg_table *st,
  440. resource_size_t start);
  441. struct ttm_kmap_iter_linear_io;
  442. struct ttm_kmap_iter *
  443. ttm_kmap_iter_linear_io_init(struct ttm_kmap_iter_linear_io *iter_io,
  444. struct ttm_device *bdev,
  445. struct ttm_resource *mem);
  446. void ttm_kmap_iter_linear_io_fini(struct ttm_kmap_iter_linear_io *iter_io,
  447. struct ttm_device *bdev,
  448. struct ttm_resource *mem);
  449. void ttm_resource_manager_create_debugfs(struct ttm_resource_manager *man,
  450. struct dentry * parent,
  451. const char *name);
  452. #endif