ttm_tt.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. #define pr_fmt(fmt) "[TTM] " fmt
  31. #include <linux/sched.h>
  32. #include <linux/highmem.h>
  33. #include <linux/pagemap.h>
  34. #include <linux/shmem_fs.h>
  35. #include <linux/file.h>
  36. #include <linux/swap.h>
  37. #include <linux/slab.h>
  38. #include <linux/export.h>
  39. #include <drm/drm_cache.h>
  40. #include <drm/drm_mem_util.h>
  41. #include <drm/ttm/ttm_module.h>
  42. #include <drm/ttm/ttm_bo_driver.h>
  43. #include <drm/ttm/ttm_placement.h>
  44. #include <drm/ttm/ttm_page_alloc.h>
  45. /**
  46. * Allocates storage for pointers to the pages that back the ttm.
  47. */
  48. static void ttm_tt_alloc_page_directory(struct ttm_tt *ttm)
  49. {
  50. ttm->pages = drm_calloc_large(ttm->num_pages, sizeof(void*));
  51. }
  52. static void ttm_dma_tt_alloc_page_directory(struct ttm_dma_tt *ttm)
  53. {
  54. ttm->ttm.pages = drm_calloc_large(ttm->ttm.num_pages, sizeof(void*));
  55. ttm->dma_address = drm_calloc_large(ttm->ttm.num_pages,
  56. sizeof(*ttm->dma_address));
  57. }
  58. #ifdef CONFIG_X86
  59. static inline int ttm_tt_set_page_caching(struct page *p,
  60. enum ttm_caching_state c_old,
  61. enum ttm_caching_state c_new)
  62. {
  63. int ret = 0;
  64. if (PageHighMem(p))
  65. return 0;
  66. if (c_old != tt_cached) {
  67. /* p isn't in the default caching state, set it to
  68. * writeback first to free its current memtype. */
  69. ret = set_pages_wb(p, 1);
  70. if (ret)
  71. return ret;
  72. }
  73. if (c_new == tt_wc)
  74. ret = set_memory_wc((unsigned long) page_address(p), 1);
  75. else if (c_new == tt_uncached)
  76. ret = set_pages_uc(p, 1);
  77. return ret;
  78. }
  79. #else /* CONFIG_X86 */
  80. static inline int ttm_tt_set_page_caching(struct page *p,
  81. enum ttm_caching_state c_old,
  82. enum ttm_caching_state c_new)
  83. {
  84. return 0;
  85. }
  86. #endif /* CONFIG_X86 */
  87. /*
  88. * Change caching policy for the linear kernel map
  89. * for range of pages in a ttm.
  90. */
  91. static int ttm_tt_set_caching(struct ttm_tt *ttm,
  92. enum ttm_caching_state c_state)
  93. {
  94. int i, j;
  95. struct page *cur_page;
  96. int ret;
  97. if (ttm->caching_state == c_state)
  98. return 0;
  99. if (ttm->state == tt_unpopulated) {
  100. /* Change caching but don't populate */
  101. ttm->caching_state = c_state;
  102. return 0;
  103. }
  104. if (ttm->caching_state == tt_cached)
  105. drm_clflush_pages(ttm->pages, ttm->num_pages);
  106. for (i = 0; i < ttm->num_pages; ++i) {
  107. cur_page = ttm->pages[i];
  108. if (likely(cur_page != NULL)) {
  109. ret = ttm_tt_set_page_caching(cur_page,
  110. ttm->caching_state,
  111. c_state);
  112. if (unlikely(ret != 0))
  113. goto out_err;
  114. }
  115. }
  116. ttm->caching_state = c_state;
  117. return 0;
  118. out_err:
  119. for (j = 0; j < i; ++j) {
  120. cur_page = ttm->pages[j];
  121. if (likely(cur_page != NULL)) {
  122. (void)ttm_tt_set_page_caching(cur_page, c_state,
  123. ttm->caching_state);
  124. }
  125. }
  126. return ret;
  127. }
  128. int ttm_tt_set_placement_caching(struct ttm_tt *ttm, uint32_t placement)
  129. {
  130. enum ttm_caching_state state;
  131. if (placement & TTM_PL_FLAG_WC)
  132. state = tt_wc;
  133. else if (placement & TTM_PL_FLAG_UNCACHED)
  134. state = tt_uncached;
  135. else
  136. state = tt_cached;
  137. return ttm_tt_set_caching(ttm, state);
  138. }
  139. EXPORT_SYMBOL(ttm_tt_set_placement_caching);
  140. void ttm_tt_destroy(struct ttm_tt *ttm)
  141. {
  142. if (unlikely(ttm == NULL))
  143. return;
  144. if (ttm->state == tt_bound) {
  145. ttm_tt_unbind(ttm);
  146. }
  147. if (ttm->state == tt_unbound)
  148. ttm_tt_unpopulate(ttm);
  149. if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTENT_SWAP) &&
  150. ttm->swap_storage)
  151. fput(ttm->swap_storage);
  152. ttm->swap_storage = NULL;
  153. ttm->func->destroy(ttm);
  154. }
  155. int ttm_tt_init(struct ttm_tt *ttm, struct ttm_bo_device *bdev,
  156. unsigned long size, uint32_t page_flags,
  157. struct page *dummy_read_page)
  158. {
  159. ttm->bdev = bdev;
  160. ttm->glob = bdev->glob;
  161. ttm->num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  162. ttm->caching_state = tt_cached;
  163. ttm->page_flags = page_flags;
  164. ttm->dummy_read_page = dummy_read_page;
  165. ttm->state = tt_unpopulated;
  166. ttm->swap_storage = NULL;
  167. ttm_tt_alloc_page_directory(ttm);
  168. if (!ttm->pages) {
  169. ttm_tt_destroy(ttm);
  170. pr_err("Failed allocating page table\n");
  171. return -ENOMEM;
  172. }
  173. return 0;
  174. }
  175. EXPORT_SYMBOL(ttm_tt_init);
  176. void ttm_tt_fini(struct ttm_tt *ttm)
  177. {
  178. drm_free_large(ttm->pages);
  179. ttm->pages = NULL;
  180. }
  181. EXPORT_SYMBOL(ttm_tt_fini);
  182. int ttm_dma_tt_init(struct ttm_dma_tt *ttm_dma, struct ttm_bo_device *bdev,
  183. unsigned long size, uint32_t page_flags,
  184. struct page *dummy_read_page)
  185. {
  186. struct ttm_tt *ttm = &ttm_dma->ttm;
  187. ttm->bdev = bdev;
  188. ttm->glob = bdev->glob;
  189. ttm->num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  190. ttm->caching_state = tt_cached;
  191. ttm->page_flags = page_flags;
  192. ttm->dummy_read_page = dummy_read_page;
  193. ttm->state = tt_unpopulated;
  194. ttm->swap_storage = NULL;
  195. INIT_LIST_HEAD(&ttm_dma->pages_list);
  196. ttm_dma_tt_alloc_page_directory(ttm_dma);
  197. if (!ttm->pages || !ttm_dma->dma_address) {
  198. ttm_tt_destroy(ttm);
  199. pr_err("Failed allocating page table\n");
  200. return -ENOMEM;
  201. }
  202. return 0;
  203. }
  204. EXPORT_SYMBOL(ttm_dma_tt_init);
  205. void ttm_dma_tt_fini(struct ttm_dma_tt *ttm_dma)
  206. {
  207. struct ttm_tt *ttm = &ttm_dma->ttm;
  208. drm_free_large(ttm->pages);
  209. ttm->pages = NULL;
  210. drm_free_large(ttm_dma->dma_address);
  211. ttm_dma->dma_address = NULL;
  212. }
  213. EXPORT_SYMBOL(ttm_dma_tt_fini);
  214. void ttm_tt_unbind(struct ttm_tt *ttm)
  215. {
  216. int ret;
  217. if (ttm->state == tt_bound) {
  218. ret = ttm->func->unbind(ttm);
  219. BUG_ON(ret);
  220. ttm->state = tt_unbound;
  221. }
  222. }
  223. int ttm_tt_bind(struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem)
  224. {
  225. int ret = 0;
  226. if (!ttm)
  227. return -EINVAL;
  228. if (ttm->state == tt_bound)
  229. return 0;
  230. ret = ttm->bdev->driver->ttm_tt_populate(ttm);
  231. if (ret)
  232. return ret;
  233. ret = ttm->func->bind(ttm, bo_mem);
  234. if (unlikely(ret != 0))
  235. return ret;
  236. ttm->state = tt_bound;
  237. return 0;
  238. }
  239. EXPORT_SYMBOL(ttm_tt_bind);
  240. int ttm_tt_swapin(struct ttm_tt *ttm)
  241. {
  242. struct address_space *swap_space;
  243. struct file *swap_storage;
  244. struct page *from_page;
  245. struct page *to_page;
  246. int i;
  247. int ret = -ENOMEM;
  248. swap_storage = ttm->swap_storage;
  249. BUG_ON(swap_storage == NULL);
  250. swap_space = file_inode(swap_storage)->i_mapping;
  251. for (i = 0; i < ttm->num_pages; ++i) {
  252. from_page = shmem_read_mapping_page(swap_space, i);
  253. if (IS_ERR(from_page)) {
  254. ret = PTR_ERR(from_page);
  255. goto out_err;
  256. }
  257. to_page = ttm->pages[i];
  258. if (unlikely(to_page == NULL))
  259. goto out_err;
  260. copy_highpage(to_page, from_page);
  261. page_cache_release(from_page);
  262. }
  263. if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTENT_SWAP))
  264. fput(swap_storage);
  265. ttm->swap_storage = NULL;
  266. ttm->page_flags &= ~TTM_PAGE_FLAG_SWAPPED;
  267. return 0;
  268. out_err:
  269. return ret;
  270. }
  271. int ttm_tt_swapout(struct ttm_tt *ttm, struct file *persistent_swap_storage)
  272. {
  273. struct address_space *swap_space;
  274. struct file *swap_storage;
  275. struct page *from_page;
  276. struct page *to_page;
  277. int i;
  278. int ret = -ENOMEM;
  279. BUG_ON(ttm->state != tt_unbound && ttm->state != tt_unpopulated);
  280. BUG_ON(ttm->caching_state != tt_cached);
  281. if (!persistent_swap_storage) {
  282. swap_storage = shmem_file_setup("ttm swap",
  283. ttm->num_pages << PAGE_SHIFT,
  284. 0);
  285. if (unlikely(IS_ERR(swap_storage))) {
  286. pr_err("Failed allocating swap storage\n");
  287. return PTR_ERR(swap_storage);
  288. }
  289. } else
  290. swap_storage = persistent_swap_storage;
  291. swap_space = file_inode(swap_storage)->i_mapping;
  292. for (i = 0; i < ttm->num_pages; ++i) {
  293. from_page = ttm->pages[i];
  294. if (unlikely(from_page == NULL))
  295. continue;
  296. to_page = shmem_read_mapping_page(swap_space, i);
  297. if (unlikely(IS_ERR(to_page))) {
  298. ret = PTR_ERR(to_page);
  299. goto out_err;
  300. }
  301. copy_highpage(to_page, from_page);
  302. set_page_dirty(to_page);
  303. mark_page_accessed(to_page);
  304. page_cache_release(to_page);
  305. }
  306. ttm_tt_unpopulate(ttm);
  307. ttm->swap_storage = swap_storage;
  308. ttm->page_flags |= TTM_PAGE_FLAG_SWAPPED;
  309. if (persistent_swap_storage)
  310. ttm->page_flags |= TTM_PAGE_FLAG_PERSISTENT_SWAP;
  311. return 0;
  312. out_err:
  313. if (!persistent_swap_storage)
  314. fput(swap_storage);
  315. return ret;
  316. }
  317. static void ttm_tt_clear_mapping(struct ttm_tt *ttm)
  318. {
  319. pgoff_t i;
  320. struct page **page = ttm->pages;
  321. if (ttm->page_flags & TTM_PAGE_FLAG_SG)
  322. return;
  323. for (i = 0; i < ttm->num_pages; ++i) {
  324. (*page)->mapping = NULL;
  325. (*page++)->index = 0;
  326. }
  327. }
  328. void ttm_tt_unpopulate(struct ttm_tt *ttm)
  329. {
  330. if (ttm->state == tt_unpopulated)
  331. return;
  332. ttm_tt_clear_mapping(ttm);
  333. ttm->bdev->driver->ttm_tt_unpopulate(ttm);
  334. }