ttm_tt.c 9.4 KB

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