ttm_tt.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. #ifndef _TTM_TT_H_
  28. #define _TTM_TT_H_
  29. #include <linux/types.h>
  30. struct ttm_tt;
  31. struct ttm_mem_reg;
  32. struct ttm_buffer_object;
  33. struct ttm_operation_ctx;
  34. #define TTM_PAGE_FLAG_WRITE (1 << 3)
  35. #define TTM_PAGE_FLAG_SWAPPED (1 << 4)
  36. #define TTM_PAGE_FLAG_PERSISTENT_SWAP (1 << 5)
  37. #define TTM_PAGE_FLAG_ZERO_ALLOC (1 << 6)
  38. #define TTM_PAGE_FLAG_DMA32 (1 << 7)
  39. #define TTM_PAGE_FLAG_SG (1 << 8)
  40. #define TTM_PAGE_FLAG_NO_RETRY (1 << 9)
  41. enum ttm_caching_state {
  42. tt_uncached,
  43. tt_wc,
  44. tt_cached
  45. };
  46. struct ttm_backend_func {
  47. /**
  48. * struct ttm_backend_func member bind
  49. *
  50. * @ttm: Pointer to a struct ttm_tt.
  51. * @bo_mem: Pointer to a struct ttm_mem_reg describing the
  52. * memory type and location for binding.
  53. *
  54. * Bind the backend pages into the aperture in the location
  55. * indicated by @bo_mem. This function should be able to handle
  56. * differences between aperture and system page sizes.
  57. */
  58. int (*bind) (struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem);
  59. /**
  60. * struct ttm_backend_func member unbind
  61. *
  62. * @ttm: Pointer to a struct ttm_tt.
  63. *
  64. * Unbind previously bound backend pages. This function should be
  65. * able to handle differences between aperture and system page sizes.
  66. */
  67. int (*unbind) (struct ttm_tt *ttm);
  68. /**
  69. * struct ttm_backend_func member destroy
  70. *
  71. * @ttm: Pointer to a struct ttm_tt.
  72. *
  73. * Destroy the backend. This will be call back from ttm_tt_destroy so
  74. * don't call ttm_tt_destroy from the callback or infinite loop.
  75. */
  76. void (*destroy) (struct ttm_tt *ttm);
  77. };
  78. /**
  79. * struct ttm_tt
  80. *
  81. * @bdev: Pointer to a struct ttm_bo_device.
  82. * @func: Pointer to a struct ttm_backend_func that describes
  83. * the backend methods.
  84. * pointer.
  85. * @pages: Array of pages backing the data.
  86. * @num_pages: Number of pages in the page array.
  87. * @bdev: Pointer to the current struct ttm_bo_device.
  88. * @be: Pointer to the ttm backend.
  89. * @swap_storage: Pointer to shmem struct file for swap storage.
  90. * @caching_state: The current caching state of the pages.
  91. * @state: The current binding state of the pages.
  92. *
  93. * This is a structure holding the pages, caching- and aperture binding
  94. * status for a buffer object that isn't backed by fixed (VRAM / AGP)
  95. * memory.
  96. */
  97. struct ttm_tt {
  98. struct ttm_bo_device *bdev;
  99. struct ttm_backend_func *func;
  100. struct page **pages;
  101. uint32_t page_flags;
  102. unsigned long num_pages;
  103. struct sg_table *sg; /* for SG objects via dma-buf */
  104. struct file *swap_storage;
  105. enum ttm_caching_state caching_state;
  106. enum {
  107. tt_bound,
  108. tt_unbound,
  109. tt_unpopulated,
  110. } state;
  111. };
  112. /**
  113. * struct ttm_dma_tt
  114. *
  115. * @ttm: Base ttm_tt struct.
  116. * @dma_address: The DMA (bus) addresses of the pages
  117. * @pages_list: used by some page allocation backend
  118. *
  119. * This is a structure holding the pages, caching- and aperture binding
  120. * status for a buffer object that isn't backed by fixed (VRAM / AGP)
  121. * memory.
  122. */
  123. struct ttm_dma_tt {
  124. struct ttm_tt ttm;
  125. dma_addr_t *dma_address;
  126. struct list_head pages_list;
  127. };
  128. /**
  129. * ttm_tt_create
  130. *
  131. * @bo: pointer to a struct ttm_buffer_object
  132. * @zero_alloc: true if allocated pages needs to be zeroed
  133. *
  134. * Make sure we have a TTM structure allocated for the given BO.
  135. * No pages are actually allocated.
  136. */
  137. int ttm_tt_create(struct ttm_buffer_object *bo, bool zero_alloc);
  138. /**
  139. * ttm_tt_init
  140. *
  141. * @ttm: The struct ttm_tt.
  142. * @bo: The buffer object we create the ttm for.
  143. * @page_flags: Page flags as identified by TTM_PAGE_FLAG_XX flags.
  144. *
  145. * Create a struct ttm_tt to back data with system memory pages.
  146. * No pages are actually allocated.
  147. * Returns:
  148. * NULL: Out of memory.
  149. */
  150. int ttm_tt_init(struct ttm_tt *ttm, struct ttm_buffer_object *bo,
  151. uint32_t page_flags);
  152. int ttm_dma_tt_init(struct ttm_dma_tt *ttm_dma, struct ttm_buffer_object *bo,
  153. uint32_t page_flags);
  154. int ttm_sg_tt_init(struct ttm_dma_tt *ttm_dma, struct ttm_buffer_object *bo,
  155. uint32_t page_flags);
  156. /**
  157. * ttm_tt_fini
  158. *
  159. * @ttm: the ttm_tt structure.
  160. *
  161. * Free memory of ttm_tt structure
  162. */
  163. void ttm_tt_fini(struct ttm_tt *ttm);
  164. void ttm_dma_tt_fini(struct ttm_dma_tt *ttm_dma);
  165. /**
  166. * ttm_ttm_bind:
  167. *
  168. * @ttm: The struct ttm_tt containing backing pages.
  169. * @bo_mem: The struct ttm_mem_reg identifying the binding location.
  170. *
  171. * Bind the pages of @ttm to an aperture location identified by @bo_mem
  172. */
  173. int ttm_tt_bind(struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem,
  174. struct ttm_operation_ctx *ctx);
  175. /**
  176. * ttm_ttm_destroy:
  177. *
  178. * @ttm: The struct ttm_tt.
  179. *
  180. * Unbind, unpopulate and destroy common struct ttm_tt.
  181. */
  182. void ttm_tt_destroy(struct ttm_tt *ttm);
  183. /**
  184. * ttm_ttm_unbind:
  185. *
  186. * @ttm: The struct ttm_tt.
  187. *
  188. * Unbind a struct ttm_tt.
  189. */
  190. void ttm_tt_unbind(struct ttm_tt *ttm);
  191. /**
  192. * ttm_tt_swapin:
  193. *
  194. * @ttm: The struct ttm_tt.
  195. *
  196. * Swap in a previously swap out ttm_tt.
  197. */
  198. int ttm_tt_swapin(struct ttm_tt *ttm);
  199. /**
  200. * ttm_tt_set_placement_caching:
  201. *
  202. * @ttm A struct ttm_tt the backing pages of which will change caching policy.
  203. * @placement: Flag indicating the desired caching policy.
  204. *
  205. * This function will change caching policy of any default kernel mappings of
  206. * the pages backing @ttm. If changing from cached to uncached or
  207. * write-combined,
  208. * all CPU caches will first be flushed to make sure the data of the pages
  209. * hit RAM. This function may be very costly as it involves global TLB
  210. * and cache flushes and potential page splitting / combining.
  211. */
  212. int ttm_tt_set_placement_caching(struct ttm_tt *ttm, uint32_t placement);
  213. int ttm_tt_swapout(struct ttm_tt *ttm, struct file *persistent_swap_storage);
  214. /**
  215. * ttm_tt_populate - allocate pages for a ttm
  216. *
  217. * @ttm: Pointer to the ttm_tt structure
  218. *
  219. * Calls the driver method to allocate pages for a ttm
  220. */
  221. int ttm_tt_populate(struct ttm_tt *ttm, struct ttm_operation_ctx *ctx);
  222. /**
  223. * ttm_tt_unpopulate - free pages from a ttm
  224. *
  225. * @ttm: Pointer to the ttm_tt structure
  226. *
  227. * Calls the driver method to free all pages from a ttm
  228. */
  229. void ttm_tt_unpopulate(struct ttm_tt *ttm);
  230. #if IS_ENABLED(CONFIG_AGP)
  231. #include <linux/agp_backend.h>
  232. /**
  233. * ttm_agp_tt_create
  234. *
  235. * @bo: Buffer object we allocate the ttm for.
  236. * @bridge: The agp bridge this device is sitting on.
  237. * @page_flags: Page flags as identified by TTM_PAGE_FLAG_XX flags.
  238. *
  239. *
  240. * Create a TTM backend that uses the indicated AGP bridge as an aperture
  241. * for TT memory. This function uses the linux agpgart interface to
  242. * bind and unbind memory backing a ttm_tt.
  243. */
  244. struct ttm_tt *ttm_agp_tt_create(struct ttm_buffer_object *bo,
  245. struct agp_bridge_data *bridge,
  246. uint32_t page_flags);
  247. int ttm_agp_tt_populate(struct ttm_tt *ttm, struct ttm_operation_ctx *ctx);
  248. void ttm_agp_tt_unpopulate(struct ttm_tt *ttm);
  249. #endif
  250. #endif