ttm_tt.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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/pagemap.h>
  30. #include <linux/types.h>
  31. #include <drm/ttm/ttm_caching.h>
  32. #include <drm/ttm/ttm_kmap_iter.h>
  33. struct ttm_device;
  34. struct ttm_tt;
  35. struct ttm_resource;
  36. struct ttm_buffer_object;
  37. struct ttm_operation_ctx;
  38. /**
  39. * struct ttm_tt - This is a structure holding the pages, caching- and aperture
  40. * binding status for a buffer object that isn't backed by fixed (VRAM / AGP)
  41. * memory.
  42. */
  43. struct ttm_tt {
  44. /** @pages: Array of pages backing the data. */
  45. struct page **pages;
  46. /**
  47. * @page_flags: The page flags.
  48. *
  49. * Supported values:
  50. *
  51. * TTM_TT_FLAG_SWAPPED: Set by TTM when the pages have been unpopulated
  52. * and swapped out by TTM. Calling ttm_tt_populate() will then swap the
  53. * pages back in, and unset the flag. Drivers should in general never
  54. * need to touch this.
  55. *
  56. * TTM_TT_FLAG_ZERO_ALLOC: Set if the pages will be zeroed on
  57. * allocation.
  58. *
  59. * TTM_TT_FLAG_EXTERNAL: Set if the underlying pages were allocated
  60. * externally, like with dma-buf or userptr. This effectively disables
  61. * TTM swapping out such pages. Also important is to prevent TTM from
  62. * ever directly mapping these pages.
  63. *
  64. * Note that enum ttm_bo_type.ttm_bo_type_sg objects will always enable
  65. * this flag.
  66. *
  67. * TTM_TT_FLAG_EXTERNAL_MAPPABLE: Same behaviour as
  68. * TTM_TT_FLAG_EXTERNAL, but with the reduced restriction that it is
  69. * still valid to use TTM to map the pages directly. This is useful when
  70. * implementing a ttm_tt backend which still allocates driver owned
  71. * pages underneath(say with shmem).
  72. *
  73. * Note that since this also implies TTM_TT_FLAG_EXTERNAL, the usage
  74. * here should always be:
  75. *
  76. * page_flags = TTM_TT_FLAG_EXTERNAL |
  77. * TTM_TT_FLAG_EXTERNAL_MAPPABLE;
  78. *
  79. * TTM_TT_FLAG_PRIV_POPULATED: TTM internal only. DO NOT USE. This is
  80. * set by TTM after ttm_tt_populate() has successfully returned, and is
  81. * then unset when TTM calls ttm_tt_unpopulate().
  82. */
  83. #define TTM_TT_FLAG_SWAPPED (1 << 0)
  84. #define TTM_TT_FLAG_ZERO_ALLOC (1 << 1)
  85. #define TTM_TT_FLAG_EXTERNAL (1 << 2)
  86. #define TTM_TT_FLAG_EXTERNAL_MAPPABLE (1 << 3)
  87. #define TTM_TT_FLAG_PRIV_POPULATED (1U << 31)
  88. uint32_t page_flags;
  89. /** @num_pages: Number of pages in the page array. */
  90. uint32_t num_pages;
  91. /** @sg: for SG objects via dma-buf. */
  92. struct sg_table *sg;
  93. /** @dma_address: The DMA (bus) addresses of the pages. */
  94. dma_addr_t *dma_address;
  95. /** @swap_storage: Pointer to shmem struct file for swap storage. */
  96. struct file *swap_storage;
  97. /**
  98. * @caching: The current caching state of the pages, see enum
  99. * ttm_caching.
  100. */
  101. enum ttm_caching caching;
  102. };
  103. /**
  104. * struct ttm_kmap_iter_tt - Specialization of a mappig iterator for a tt.
  105. * @base: Embedded struct ttm_kmap_iter providing the usage interface
  106. * @tt: Cached struct ttm_tt.
  107. * @prot: Cached page protection for mapping.
  108. */
  109. struct ttm_kmap_iter_tt {
  110. struct ttm_kmap_iter base;
  111. struct ttm_tt *tt;
  112. pgprot_t prot;
  113. };
  114. static inline bool ttm_tt_is_populated(struct ttm_tt *tt)
  115. {
  116. return tt->page_flags & TTM_TT_FLAG_PRIV_POPULATED;
  117. }
  118. /**
  119. * ttm_tt_create
  120. *
  121. * @bo: pointer to a struct ttm_buffer_object
  122. * @zero_alloc: true if allocated pages needs to be zeroed
  123. *
  124. * Make sure we have a TTM structure allocated for the given BO.
  125. * No pages are actually allocated.
  126. */
  127. int ttm_tt_create(struct ttm_buffer_object *bo, bool zero_alloc);
  128. /**
  129. * ttm_tt_init
  130. *
  131. * @ttm: The struct ttm_tt.
  132. * @bo: The buffer object we create the ttm for.
  133. * @page_flags: Page flags as identified by TTM_TT_FLAG_XX flags.
  134. * @caching: the desired caching state of the pages
  135. * @extra_pages: Extra pages needed for the driver.
  136. *
  137. * Create a struct ttm_tt to back data with system memory pages.
  138. * No pages are actually allocated.
  139. * Returns:
  140. * NULL: Out of memory.
  141. */
  142. int ttm_tt_init(struct ttm_tt *ttm, struct ttm_buffer_object *bo,
  143. uint32_t page_flags, enum ttm_caching caching,
  144. unsigned long extra_pages);
  145. int ttm_sg_tt_init(struct ttm_tt *ttm_dma, struct ttm_buffer_object *bo,
  146. uint32_t page_flags, enum ttm_caching caching);
  147. /**
  148. * ttm_tt_fini
  149. *
  150. * @ttm: the ttm_tt structure.
  151. *
  152. * Free memory of ttm_tt structure
  153. */
  154. void ttm_tt_fini(struct ttm_tt *ttm);
  155. /**
  156. * ttm_tt_destroy:
  157. *
  158. * @bdev: the ttm_device this object belongs to
  159. * @ttm: The struct ttm_tt.
  160. *
  161. * Unbind, unpopulate and destroy common struct ttm_tt.
  162. */
  163. void ttm_tt_destroy(struct ttm_device *bdev, struct ttm_tt *ttm);
  164. /**
  165. * ttm_tt_swapin:
  166. *
  167. * @ttm: The struct ttm_tt.
  168. *
  169. * Swap in a previously swap out ttm_tt.
  170. */
  171. int ttm_tt_swapin(struct ttm_tt *ttm);
  172. int ttm_tt_swapout(struct ttm_device *bdev, struct ttm_tt *ttm,
  173. gfp_t gfp_flags);
  174. /**
  175. * ttm_tt_populate - allocate pages for a ttm
  176. *
  177. * @bdev: the ttm_device this object belongs to
  178. * @ttm: Pointer to the ttm_tt structure
  179. * @ctx: operation context for populating the tt object.
  180. *
  181. * Calls the driver method to allocate pages for a ttm
  182. */
  183. int ttm_tt_populate(struct ttm_device *bdev, struct ttm_tt *ttm,
  184. struct ttm_operation_ctx *ctx);
  185. /**
  186. * ttm_tt_unpopulate - free pages from a ttm
  187. *
  188. * @bdev: the ttm_device this object belongs to
  189. * @ttm: Pointer to the ttm_tt structure
  190. *
  191. * Calls the driver method to free all pages from a ttm
  192. */
  193. void ttm_tt_unpopulate(struct ttm_device *bdev, struct ttm_tt *ttm);
  194. /**
  195. * ttm_tt_mark_for_clear - Mark pages for clearing on populate.
  196. *
  197. * @ttm: Pointer to the ttm_tt structure
  198. *
  199. * Marks pages for clearing so that the next time the page vector is
  200. * populated, the pages will be cleared.
  201. */
  202. static inline void ttm_tt_mark_for_clear(struct ttm_tt *ttm)
  203. {
  204. ttm->page_flags |= TTM_TT_FLAG_ZERO_ALLOC;
  205. }
  206. void ttm_tt_mgr_init(unsigned long num_pages, unsigned long num_dma32_pages);
  207. struct ttm_kmap_iter *ttm_kmap_iter_tt_init(struct ttm_kmap_iter_tt *iter_tt,
  208. struct ttm_tt *tt);
  209. #if IS_ENABLED(CONFIG_AGP)
  210. #include <linux/agp_backend.h>
  211. /**
  212. * ttm_agp_tt_create
  213. *
  214. * @bo: Buffer object we allocate the ttm for.
  215. * @bridge: The agp bridge this device is sitting on.
  216. * @page_flags: Page flags as identified by TTM_TT_FLAG_XX flags.
  217. *
  218. *
  219. * Create a TTM backend that uses the indicated AGP bridge as an aperture
  220. * for TT memory. This function uses the linux agpgart interface to
  221. * bind and unbind memory backing a ttm_tt.
  222. */
  223. struct ttm_tt *ttm_agp_tt_create(struct ttm_buffer_object *bo,
  224. struct agp_bridge_data *bridge,
  225. uint32_t page_flags);
  226. int ttm_agp_bind(struct ttm_tt *ttm, struct ttm_resource *bo_mem);
  227. void ttm_agp_unbind(struct ttm_tt *ttm);
  228. void ttm_agp_destroy(struct ttm_tt *ttm);
  229. bool ttm_agp_is_bound(struct ttm_tt *ttm);
  230. #endif
  231. #endif