ttm_device.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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_DEVICE_H_
  25. #define _TTM_DEVICE_H_
  26. #include <linux/types.h>
  27. #include <linux/workqueue.h>
  28. #include <drm/ttm/ttm_resource.h>
  29. #include <drm/ttm/ttm_pool.h>
  30. struct ttm_device;
  31. struct ttm_placement;
  32. struct ttm_buffer_object;
  33. struct ttm_operation_ctx;
  34. /**
  35. * struct ttm_global - Buffer object driver global data.
  36. */
  37. extern struct ttm_global {
  38. /**
  39. * @dummy_read_page: Pointer to a dummy page used for mapping requests
  40. * of unpopulated pages. Constant after init.
  41. */
  42. struct page *dummy_read_page;
  43. /**
  44. * @device_list: List of buffer object devices. Protected by
  45. * ttm_global_mutex.
  46. */
  47. struct list_head device_list;
  48. /**
  49. * @bo_count: Number of buffer objects allocated by devices.
  50. */
  51. atomic_t bo_count;
  52. } ttm_glob;
  53. struct ttm_device_funcs {
  54. /**
  55. * ttm_tt_create
  56. *
  57. * @bo: The buffer object to create the ttm for.
  58. * @page_flags: Page flags as identified by TTM_TT_FLAG_XX flags.
  59. *
  60. * Create a struct ttm_tt to back data with system memory pages.
  61. * No pages are actually allocated.
  62. * Returns:
  63. * NULL: Out of memory.
  64. */
  65. struct ttm_tt *(*ttm_tt_create)(struct ttm_buffer_object *bo,
  66. uint32_t page_flags);
  67. /**
  68. * ttm_tt_populate
  69. *
  70. * @ttm: The struct ttm_tt to contain the backing pages.
  71. *
  72. * Allocate all backing pages
  73. * Returns:
  74. * -ENOMEM: Out of memory.
  75. */
  76. int (*ttm_tt_populate)(struct ttm_device *bdev,
  77. struct ttm_tt *ttm,
  78. struct ttm_operation_ctx *ctx);
  79. /**
  80. * ttm_tt_unpopulate
  81. *
  82. * @ttm: The struct ttm_tt to contain the backing pages.
  83. *
  84. * Free all backing page
  85. */
  86. void (*ttm_tt_unpopulate)(struct ttm_device *bdev,
  87. struct ttm_tt *ttm);
  88. /**
  89. * ttm_tt_destroy
  90. *
  91. * @bdev: Pointer to a ttm device
  92. * @ttm: Pointer to a struct ttm_tt.
  93. *
  94. * Destroy the backend. This will be call back from ttm_tt_destroy so
  95. * don't call ttm_tt_destroy from the callback or infinite loop.
  96. */
  97. void (*ttm_tt_destroy)(struct ttm_device *bdev, struct ttm_tt *ttm);
  98. /**
  99. * struct ttm_bo_driver member eviction_valuable
  100. *
  101. * @bo: the buffer object to be evicted
  102. * @place: placement we need room for
  103. *
  104. * Check with the driver if it is valuable to evict a BO to make room
  105. * for a certain placement.
  106. */
  107. bool (*eviction_valuable)(struct ttm_buffer_object *bo,
  108. const struct ttm_place *place);
  109. /**
  110. * struct ttm_bo_driver member evict_flags:
  111. *
  112. * @bo: the buffer object to be evicted
  113. *
  114. * Return the bo flags for a buffer which is not mapped to the hardware.
  115. * These will be placed in proposed_flags so that when the move is
  116. * finished, they'll end up in bo->mem.flags
  117. * This should not cause multihop evictions, and the core will warn
  118. * if one is proposed.
  119. */
  120. void (*evict_flags)(struct ttm_buffer_object *bo,
  121. struct ttm_placement *placement);
  122. /**
  123. * struct ttm_bo_driver member move:
  124. *
  125. * @bo: the buffer to move
  126. * @evict: whether this motion is evicting the buffer from
  127. * the graphics address space
  128. * @ctx: context for this move with parameters
  129. * @new_mem: the new memory region receiving the buffer
  130. * @hop: placement for driver directed intermediate hop
  131. *
  132. * Move a buffer between two memory regions.
  133. * Returns errno -EMULTIHOP if driver requests a hop
  134. */
  135. int (*move)(struct ttm_buffer_object *bo, bool evict,
  136. struct ttm_operation_ctx *ctx,
  137. struct ttm_resource *new_mem,
  138. struct ttm_place *hop);
  139. /**
  140. * Hook to notify driver about a resource delete.
  141. */
  142. void (*delete_mem_notify)(struct ttm_buffer_object *bo);
  143. /**
  144. * notify the driver that we're about to swap out this bo
  145. */
  146. void (*swap_notify)(struct ttm_buffer_object *bo);
  147. /**
  148. * Driver callback on when mapping io memory (for bo_move_memcpy
  149. * for instance). TTM will take care to call io_mem_free whenever
  150. * the mapping is not use anymore. io_mem_reserve & io_mem_free
  151. * are balanced.
  152. */
  153. int (*io_mem_reserve)(struct ttm_device *bdev,
  154. struct ttm_resource *mem);
  155. void (*io_mem_free)(struct ttm_device *bdev,
  156. struct ttm_resource *mem);
  157. /**
  158. * Return the pfn for a given page_offset inside the BO.
  159. *
  160. * @bo: the BO to look up the pfn for
  161. * @page_offset: the offset to look up
  162. */
  163. unsigned long (*io_mem_pfn)(struct ttm_buffer_object *bo,
  164. unsigned long page_offset);
  165. /**
  166. * Read/write memory buffers for ptrace access
  167. *
  168. * @bo: the BO to access
  169. * @offset: the offset from the start of the BO
  170. * @buf: pointer to source/destination buffer
  171. * @len: number of bytes to copy
  172. * @write: whether to read (0) from or write (non-0) to BO
  173. *
  174. * If successful, this function should return the number of
  175. * bytes copied, -EIO otherwise. If the number of bytes
  176. * returned is < len, the function may be called again with
  177. * the remainder of the buffer to copy.
  178. */
  179. int (*access_memory)(struct ttm_buffer_object *bo, unsigned long offset,
  180. void *buf, int len, int write);
  181. /**
  182. * Notify the driver that we're about to release a BO
  183. *
  184. * @bo: BO that is about to be released
  185. *
  186. * Gives the driver a chance to do any cleanup, including
  187. * adding fences that may force a delayed delete
  188. */
  189. void (*release_notify)(struct ttm_buffer_object *bo);
  190. };
  191. /**
  192. * struct ttm_device - Buffer object driver device-specific data.
  193. */
  194. struct ttm_device {
  195. /**
  196. * @device_list: Our entry in the global device list.
  197. * Constant after bo device init
  198. */
  199. struct list_head device_list;
  200. /**
  201. * @funcs: Function table for the device.
  202. * Constant after bo device init
  203. */
  204. const struct ttm_device_funcs *funcs;
  205. /**
  206. * @sysman: Resource manager for the system domain.
  207. * Access via ttm_manager_type.
  208. */
  209. struct ttm_resource_manager sysman;
  210. /**
  211. * @man_drv: An array of resource_managers, one per resource type.
  212. */
  213. struct ttm_resource_manager *man_drv[TTM_NUM_MEM_TYPES];
  214. /**
  215. * @vma_manager: Address space manager for finding BOs to mmap.
  216. */
  217. struct drm_vma_offset_manager *vma_manager;
  218. /**
  219. * @pool: page pool for the device.
  220. */
  221. struct ttm_pool pool;
  222. /**
  223. * @lru_lock: Protection for the per manager LRU and ddestroy lists.
  224. */
  225. spinlock_t lru_lock;
  226. /**
  227. * @pinned: Buffer objects which are pinned and so not on any LRU list.
  228. */
  229. struct list_head pinned;
  230. /**
  231. * @dev_mapping: A pointer to the struct address_space for invalidating
  232. * CPU mappings on buffer move. Protected by load/unload sync.
  233. */
  234. struct address_space *dev_mapping;
  235. /**
  236. * @wq: Work queue structure for the delayed delete workqueue.
  237. */
  238. struct workqueue_struct *wq;
  239. };
  240. int ttm_global_swapout(struct ttm_operation_ctx *ctx, gfp_t gfp_flags);
  241. int ttm_device_swapout(struct ttm_device *bdev, struct ttm_operation_ctx *ctx,
  242. gfp_t gfp_flags);
  243. static inline struct ttm_resource_manager *
  244. ttm_manager_type(struct ttm_device *bdev, int mem_type)
  245. {
  246. BUILD_BUG_ON(__builtin_constant_p(mem_type)
  247. && mem_type >= TTM_NUM_MEM_TYPES);
  248. return bdev->man_drv[mem_type];
  249. }
  250. static inline void ttm_set_driver_manager(struct ttm_device *bdev, int type,
  251. struct ttm_resource_manager *manager)
  252. {
  253. BUILD_BUG_ON(__builtin_constant_p(type) && type >= TTM_NUM_MEM_TYPES);
  254. bdev->man_drv[type] = manager;
  255. }
  256. int ttm_device_init(struct ttm_device *bdev, const struct ttm_device_funcs *funcs,
  257. struct device *dev, struct address_space *mapping,
  258. struct drm_vma_offset_manager *vma_manager,
  259. bool use_dma_alloc, bool use_dma32);
  260. void ttm_device_fini(struct ttm_device *bdev);
  261. void ttm_device_clear_dma_mappings(struct ttm_device *bdev);
  262. #endif