ttm_tt.c 9.2 KB

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