ttm_tt.c 9.2 KB

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