ttm_bo.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. /**************************************************************************
  2. *
  3. * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
  4. * All Rights Reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sub license, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice (including the
  15. * next paragraph) shall be included in all copies or substantial portions
  16. * of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  21. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  22. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  23. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  24. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. *
  26. **************************************************************************/
  27. /*
  28. * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
  29. */
  30. #ifndef _TTM_BO_API_H_
  31. #define _TTM_BO_API_H_
  32. #include <drm/drm_gem.h>
  33. #include <linux/kref.h>
  34. #include <linux/list.h>
  35. #include "ttm_device.h"
  36. /* Default number of pre-faulted pages in the TTM fault handler */
  37. #define TTM_BO_VM_NUM_PREFAULT 16
  38. struct iosys_map;
  39. struct ttm_global;
  40. struct ttm_device;
  41. struct ttm_placement;
  42. struct ttm_place;
  43. struct ttm_resource;
  44. struct ttm_resource_manager;
  45. struct ttm_tt;
  46. /**
  47. * enum ttm_bo_type
  48. *
  49. * @ttm_bo_type_device: These are 'normal' buffers that can
  50. * be mmapped by user space. Each of these bos occupy a slot in the
  51. * device address space, that can be used for normal vm operations.
  52. *
  53. * @ttm_bo_type_kernel: These buffers are like ttm_bo_type_device buffers,
  54. * but they cannot be accessed from user-space. For kernel-only use.
  55. *
  56. * @ttm_bo_type_sg: Buffer made from dmabuf sg table shared with another
  57. * driver.
  58. */
  59. enum ttm_bo_type {
  60. ttm_bo_type_device,
  61. ttm_bo_type_kernel,
  62. ttm_bo_type_sg
  63. };
  64. /**
  65. * struct ttm_buffer_object
  66. *
  67. * @base: drm_gem_object superclass data.
  68. * @bdev: Pointer to the buffer object device structure.
  69. * @type: The bo type.
  70. * @page_alignment: Page alignment.
  71. * @destroy: Destruction function. If NULL, kfree is used.
  72. * @kref: Reference count of this buffer object. When this refcount reaches
  73. * zero, the object is destroyed or put on the delayed delete list.
  74. * @resource: structure describing current placement.
  75. * @ttm: TTM structure holding system pages.
  76. * @deleted: True if the object is only a zombie and already deleted.
  77. * @bulk_move: The bulk move object.
  78. * @priority: Priority for LRU, BOs with lower priority are evicted first.
  79. * @pin_count: Pin count.
  80. *
  81. * Base class for TTM buffer object, that deals with data placement and CPU
  82. * mappings. GPU mappings are really up to the driver, but for simpler GPUs
  83. * the driver can usually use the placement offset @offset directly as the
  84. * GPU virtual address. For drivers implementing multiple
  85. * GPU memory manager contexts, the driver should manage the address space
  86. * in these contexts separately and use these objects to get the correct
  87. * placement and caching for these GPU maps. This makes it possible to use
  88. * these objects for even quite elaborate memory management schemes.
  89. * The destroy member, the API visibility of this object makes it possible
  90. * to derive driver specific types.
  91. */
  92. struct ttm_buffer_object {
  93. struct drm_gem_object base;
  94. /*
  95. * Members constant at init.
  96. */
  97. struct ttm_device *bdev;
  98. enum ttm_bo_type type;
  99. uint32_t page_alignment;
  100. void (*destroy) (struct ttm_buffer_object *);
  101. /*
  102. * Members not needing protection.
  103. */
  104. struct kref kref;
  105. /*
  106. * Members protected by the bo::resv::reserved lock.
  107. */
  108. struct ttm_resource *resource;
  109. struct ttm_tt *ttm;
  110. bool deleted;
  111. struct ttm_lru_bulk_move *bulk_move;
  112. unsigned priority;
  113. unsigned pin_count;
  114. /**
  115. * @delayed_delete: Work item used when we can't delete the BO
  116. * immediately
  117. */
  118. struct work_struct delayed_delete;
  119. /**
  120. * @sg: external source of pages and DMA addresses, protected by the
  121. * reservation lock.
  122. */
  123. struct sg_table *sg;
  124. };
  125. #define TTM_BO_MAP_IOMEM_MASK 0x80
  126. /**
  127. * struct ttm_bo_kmap_obj
  128. *
  129. * @virtual: The current kernel virtual address.
  130. * @page: The page when kmap'ing a single page.
  131. * @bo_kmap_type: Type of bo_kmap.
  132. * @bo: The TTM BO.
  133. *
  134. * Object describing a kernel mapping. Since a TTM bo may be located
  135. * in various memory types with various caching policies, the
  136. * mapping can either be an ioremap, a vmap, a kmap or part of a
  137. * premapped region.
  138. */
  139. struct ttm_bo_kmap_obj {
  140. void *virtual;
  141. struct page *page;
  142. enum {
  143. ttm_bo_map_iomap = 1 | TTM_BO_MAP_IOMEM_MASK,
  144. ttm_bo_map_vmap = 2,
  145. ttm_bo_map_kmap = 3,
  146. ttm_bo_map_premapped = 4 | TTM_BO_MAP_IOMEM_MASK,
  147. } bo_kmap_type;
  148. struct ttm_buffer_object *bo;
  149. };
  150. /**
  151. * struct ttm_operation_ctx
  152. *
  153. * @interruptible: Sleep interruptible if sleeping.
  154. * @no_wait_gpu: Return immediately if the GPU is busy.
  155. * @gfp_retry_mayfail: Set the __GFP_RETRY_MAYFAIL when allocation pages.
  156. * @allow_res_evict: Allow eviction of reserved BOs. Can be used when multiple
  157. * BOs share the same reservation object.
  158. * @force_alloc: Don't check the memory account during suspend or CPU page
  159. * faults. Should only be used by TTM internally.
  160. * @resv: Reservation object to allow reserved evictions with.
  161. * @bytes_moved: Statistics on how many bytes have been moved.
  162. *
  163. * Context for TTM operations like changing buffer placement or general memory
  164. * allocation.
  165. */
  166. struct ttm_operation_ctx {
  167. bool interruptible;
  168. bool no_wait_gpu;
  169. bool gfp_retry_mayfail;
  170. bool allow_res_evict;
  171. bool force_alloc;
  172. struct dma_resv *resv;
  173. uint64_t bytes_moved;
  174. };
  175. struct ttm_lru_walk;
  176. /** struct ttm_lru_walk_ops - Operations for a LRU walk. */
  177. struct ttm_lru_walk_ops {
  178. /**
  179. * process_bo - Process this bo.
  180. * @walk: struct ttm_lru_walk describing the walk.
  181. * @bo: A locked and referenced buffer object.
  182. *
  183. * Return: Negative error code on error, User-defined positive value
  184. * (typically, but not always, size of the processed bo) on success.
  185. * On success, the returned values are summed by the walk and the
  186. * walk exits when its target is met.
  187. * 0 also indicates success, -EBUSY means this bo was skipped.
  188. */
  189. s64 (*process_bo)(struct ttm_lru_walk *walk, struct ttm_buffer_object *bo);
  190. };
  191. /**
  192. * struct ttm_lru_walk - Structure describing a LRU walk.
  193. */
  194. struct ttm_lru_walk {
  195. /** @ops: Pointer to the ops structure. */
  196. const struct ttm_lru_walk_ops *ops;
  197. /** @ctx: Pointer to the struct ttm_operation_ctx. */
  198. struct ttm_operation_ctx *ctx;
  199. /** @ticket: The struct ww_acquire_ctx if any. */
  200. struct ww_acquire_ctx *ticket;
  201. /** @trylock_only: Only use trylock for locking. */
  202. bool trylock_only;
  203. };
  204. s64 ttm_lru_walk_for_evict(struct ttm_lru_walk *walk, struct ttm_device *bdev,
  205. struct ttm_resource_manager *man, s64 target);
  206. /**
  207. * ttm_bo_get - reference a struct ttm_buffer_object
  208. *
  209. * @bo: The buffer object.
  210. */
  211. static inline void ttm_bo_get(struct ttm_buffer_object *bo)
  212. {
  213. kref_get(&bo->kref);
  214. }
  215. /**
  216. * ttm_bo_get_unless_zero - reference a struct ttm_buffer_object unless
  217. * its refcount has already reached zero.
  218. * @bo: The buffer object.
  219. *
  220. * Used to reference a TTM buffer object in lookups where the object is removed
  221. * from the lookup structure during the destructor and for RCU lookups.
  222. *
  223. * Returns: @bo if the referencing was successful, NULL otherwise.
  224. */
  225. static inline __must_check struct ttm_buffer_object *
  226. ttm_bo_get_unless_zero(struct ttm_buffer_object *bo)
  227. {
  228. if (!kref_get_unless_zero(&bo->kref))
  229. return NULL;
  230. return bo;
  231. }
  232. /**
  233. * ttm_bo_reserve:
  234. *
  235. * @bo: A pointer to a struct ttm_buffer_object.
  236. * @interruptible: Sleep interruptible if waiting.
  237. * @no_wait: Don't sleep while trying to reserve, rather return -EBUSY.
  238. * @ticket: ticket used to acquire the ww_mutex.
  239. *
  240. * Locks a buffer object for validation. (Or prevents other processes from
  241. * locking it for validation), while taking a number of measures to prevent
  242. * deadlocks.
  243. *
  244. * Returns:
  245. * -EDEADLK: The reservation may cause a deadlock.
  246. * Release all buffer reservations, wait for @bo to become unreserved and
  247. * try again.
  248. * -ERESTARTSYS: A wait for the buffer to become unreserved was interrupted by
  249. * a signal. Release all buffer reservations and return to user-space.
  250. * -EBUSY: The function needed to sleep, but @no_wait was true
  251. * -EALREADY: Bo already reserved using @ticket. This error code will only
  252. * be returned if @use_ticket is set to true.
  253. */
  254. static inline int ttm_bo_reserve(struct ttm_buffer_object *bo,
  255. bool interruptible, bool no_wait,
  256. struct ww_acquire_ctx *ticket)
  257. {
  258. int ret = 0;
  259. if (no_wait) {
  260. bool success;
  261. if (WARN_ON(ticket))
  262. return -EBUSY;
  263. success = dma_resv_trylock(bo->base.resv);
  264. return success ? 0 : -EBUSY;
  265. }
  266. if (interruptible)
  267. ret = dma_resv_lock_interruptible(bo->base.resv, ticket);
  268. else
  269. ret = dma_resv_lock(bo->base.resv, ticket);
  270. if (ret == -EINTR)
  271. return -ERESTARTSYS;
  272. return ret;
  273. }
  274. /**
  275. * ttm_bo_reserve_slowpath:
  276. * @bo: A pointer to a struct ttm_buffer_object.
  277. * @interruptible: Sleep interruptible if waiting.
  278. * @ticket: Ticket used to acquire the ww_mutex.
  279. *
  280. * This is called after ttm_bo_reserve returns -EAGAIN and we backed off
  281. * from all our other reservations. Because there are no other reservations
  282. * held by us, this function cannot deadlock any more.
  283. */
  284. static inline int ttm_bo_reserve_slowpath(struct ttm_buffer_object *bo,
  285. bool interruptible,
  286. struct ww_acquire_ctx *ticket)
  287. {
  288. if (interruptible) {
  289. int ret = dma_resv_lock_slow_interruptible(bo->base.resv,
  290. ticket);
  291. if (ret == -EINTR)
  292. ret = -ERESTARTSYS;
  293. return ret;
  294. }
  295. dma_resv_lock_slow(bo->base.resv, ticket);
  296. return 0;
  297. }
  298. void ttm_bo_move_to_lru_tail(struct ttm_buffer_object *bo);
  299. static inline void
  300. ttm_bo_move_to_lru_tail_unlocked(struct ttm_buffer_object *bo)
  301. {
  302. spin_lock(&bo->bdev->lru_lock);
  303. ttm_bo_move_to_lru_tail(bo);
  304. spin_unlock(&bo->bdev->lru_lock);
  305. }
  306. static inline void ttm_bo_assign_mem(struct ttm_buffer_object *bo,
  307. struct ttm_resource *new_mem)
  308. {
  309. WARN_ON(bo->resource);
  310. bo->resource = new_mem;
  311. }
  312. /**
  313. * ttm_bo_move_null - assign memory for a buffer object.
  314. * @bo: The bo to assign the memory to
  315. * @new_mem: The memory to be assigned.
  316. *
  317. * Assign the memory from new_mem to the memory of the buffer object bo.
  318. */
  319. static inline void ttm_bo_move_null(struct ttm_buffer_object *bo,
  320. struct ttm_resource *new_mem)
  321. {
  322. ttm_resource_free(bo, &bo->resource);
  323. ttm_bo_assign_mem(bo, new_mem);
  324. }
  325. /**
  326. * ttm_bo_unreserve
  327. *
  328. * @bo: A pointer to a struct ttm_buffer_object.
  329. *
  330. * Unreserve a previous reservation of @bo.
  331. */
  332. static inline void ttm_bo_unreserve(struct ttm_buffer_object *bo)
  333. {
  334. ttm_bo_move_to_lru_tail_unlocked(bo);
  335. dma_resv_unlock(bo->base.resv);
  336. }
  337. /**
  338. * ttm_kmap_obj_virtual
  339. *
  340. * @map: A struct ttm_bo_kmap_obj returned from ttm_bo_kmap.
  341. * @is_iomem: Pointer to an integer that on return indicates 1 if the
  342. * virtual map is io memory, 0 if normal memory.
  343. *
  344. * Returns the virtual address of a buffer object area mapped by ttm_bo_kmap.
  345. * If *is_iomem is 1 on return, the virtual address points to an io memory area,
  346. * that should strictly be accessed by the iowriteXX() and similar functions.
  347. */
  348. static inline void *ttm_kmap_obj_virtual(struct ttm_bo_kmap_obj *map,
  349. bool *is_iomem)
  350. {
  351. *is_iomem = !!(map->bo_kmap_type & TTM_BO_MAP_IOMEM_MASK);
  352. return map->virtual;
  353. }
  354. int ttm_bo_wait_ctx(struct ttm_buffer_object *bo,
  355. struct ttm_operation_ctx *ctx);
  356. int ttm_bo_validate(struct ttm_buffer_object *bo,
  357. struct ttm_placement *placement,
  358. struct ttm_operation_ctx *ctx);
  359. void ttm_bo_put(struct ttm_buffer_object *bo);
  360. void ttm_bo_set_bulk_move(struct ttm_buffer_object *bo,
  361. struct ttm_lru_bulk_move *bulk);
  362. bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
  363. const struct ttm_place *place);
  364. int ttm_bo_init_reserved(struct ttm_device *bdev, struct ttm_buffer_object *bo,
  365. enum ttm_bo_type type, struct ttm_placement *placement,
  366. uint32_t alignment, struct ttm_operation_ctx *ctx,
  367. struct sg_table *sg, struct dma_resv *resv,
  368. void (*destroy)(struct ttm_buffer_object *));
  369. int ttm_bo_init_validate(struct ttm_device *bdev, struct ttm_buffer_object *bo,
  370. enum ttm_bo_type type, struct ttm_placement *placement,
  371. uint32_t alignment, bool interruptible,
  372. struct sg_table *sg, struct dma_resv *resv,
  373. void (*destroy)(struct ttm_buffer_object *));
  374. int ttm_bo_kmap(struct ttm_buffer_object *bo, unsigned long start_page,
  375. unsigned long num_pages, struct ttm_bo_kmap_obj *map);
  376. void ttm_bo_kunmap(struct ttm_bo_kmap_obj *map);
  377. int ttm_bo_vmap(struct ttm_buffer_object *bo, struct iosys_map *map);
  378. void ttm_bo_vunmap(struct ttm_buffer_object *bo, struct iosys_map *map);
  379. int ttm_bo_mmap_obj(struct vm_area_struct *vma, struct ttm_buffer_object *bo);
  380. s64 ttm_bo_swapout(struct ttm_device *bdev, struct ttm_operation_ctx *ctx,
  381. struct ttm_resource_manager *man, gfp_t gfp_flags,
  382. s64 target);
  383. void ttm_bo_pin(struct ttm_buffer_object *bo);
  384. void ttm_bo_unpin(struct ttm_buffer_object *bo);
  385. int ttm_bo_evict_first(struct ttm_device *bdev,
  386. struct ttm_resource_manager *man,
  387. struct ttm_operation_ctx *ctx);
  388. vm_fault_t ttm_bo_vm_reserve(struct ttm_buffer_object *bo,
  389. struct vm_fault *vmf);
  390. vm_fault_t ttm_bo_vm_fault_reserved(struct vm_fault *vmf,
  391. pgprot_t prot,
  392. pgoff_t num_prefault);
  393. vm_fault_t ttm_bo_vm_fault(struct vm_fault *vmf);
  394. void ttm_bo_vm_open(struct vm_area_struct *vma);
  395. void ttm_bo_vm_close(struct vm_area_struct *vma);
  396. int ttm_bo_vm_access(struct vm_area_struct *vma, unsigned long addr,
  397. void *buf, int len, int write);
  398. vm_fault_t ttm_bo_vm_dummy_page(struct vm_fault *vmf, pgprot_t prot);
  399. int ttm_bo_mem_space(struct ttm_buffer_object *bo,
  400. struct ttm_placement *placement,
  401. struct ttm_resource **mem,
  402. struct ttm_operation_ctx *ctx);
  403. void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo);
  404. /*
  405. * ttm_bo_util.c
  406. */
  407. int ttm_mem_io_reserve(struct ttm_device *bdev,
  408. struct ttm_resource *mem);
  409. void ttm_mem_io_free(struct ttm_device *bdev,
  410. struct ttm_resource *mem);
  411. void ttm_move_memcpy(bool clear, u32 num_pages,
  412. struct ttm_kmap_iter *dst_iter,
  413. struct ttm_kmap_iter *src_iter);
  414. int ttm_bo_move_memcpy(struct ttm_buffer_object *bo,
  415. struct ttm_operation_ctx *ctx,
  416. struct ttm_resource *new_mem);
  417. int ttm_bo_move_accel_cleanup(struct ttm_buffer_object *bo,
  418. struct dma_fence *fence, bool evict,
  419. bool pipeline,
  420. struct ttm_resource *new_mem);
  421. void ttm_bo_move_sync_cleanup(struct ttm_buffer_object *bo,
  422. struct ttm_resource *new_mem);
  423. int ttm_bo_pipeline_gutting(struct ttm_buffer_object *bo);
  424. pgprot_t ttm_io_prot(struct ttm_buffer_object *bo, struct ttm_resource *res,
  425. pgprot_t tmp);
  426. void ttm_bo_tt_destroy(struct ttm_buffer_object *bo);
  427. #endif