ttm_tt.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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_cache.h"
  40. #include "drm_mem_util.h"
  41. #include "ttm/ttm_module.h"
  42. #include "ttm/ttm_bo_driver.h"
  43. #include "ttm/ttm_placement.h"
  44. #include "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->bdev->driver->ttm_tt_unpopulate(ttm);
  149. }
  150. if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTENT_SWAP) &&
  151. ttm->swap_storage)
  152. fput(ttm->swap_storage);
  153. ttm->swap_storage = NULL;
  154. ttm->func->destroy(ttm);
  155. }
  156. int ttm_tt_init(struct ttm_tt *ttm, struct ttm_bo_device *bdev,
  157. unsigned long size, uint32_t page_flags,
  158. struct page *dummy_read_page)
  159. {
  160. ttm->bdev = bdev;
  161. ttm->glob = bdev->glob;
  162. ttm->num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  163. ttm->caching_state = tt_cached;
  164. ttm->page_flags = page_flags;
  165. ttm->dummy_read_page = dummy_read_page;
  166. ttm->state = tt_unpopulated;
  167. ttm->swap_storage = NULL;
  168. ttm_tt_alloc_page_directory(ttm);
  169. if (!ttm->pages) {
  170. ttm_tt_destroy(ttm);
  171. pr_err("Failed allocating page table\n");
  172. return -ENOMEM;
  173. }
  174. return 0;
  175. }
  176. EXPORT_SYMBOL(ttm_tt_init);
  177. void ttm_tt_fini(struct ttm_tt *ttm)
  178. {
  179. drm_free_large(ttm->pages);
  180. ttm->pages = NULL;
  181. }
  182. EXPORT_SYMBOL(ttm_tt_fini);
  183. int ttm_dma_tt_init(struct ttm_dma_tt *ttm_dma, struct ttm_bo_device *bdev,
  184. unsigned long size, uint32_t page_flags,
  185. struct page *dummy_read_page)
  186. {
  187. struct ttm_tt *ttm = &ttm_dma->ttm;
  188. ttm->bdev = bdev;
  189. ttm->glob = bdev->glob;
  190. ttm->num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  191. ttm->caching_state = tt_cached;
  192. ttm->page_flags = page_flags;
  193. ttm->dummy_read_page = dummy_read_page;
  194. ttm->state = tt_unpopulated;
  195. ttm->swap_storage = NULL;
  196. INIT_LIST_HEAD(&ttm_dma->pages_list);
  197. ttm_dma_tt_alloc_page_directory(ttm_dma);
  198. if (!ttm->pages || !ttm_dma->dma_address) {
  199. ttm_tt_destroy(ttm);
  200. pr_err("Failed allocating page table\n");
  201. return -ENOMEM;
  202. }
  203. return 0;
  204. }
  205. EXPORT_SYMBOL(ttm_dma_tt_init);
  206. void ttm_dma_tt_fini(struct ttm_dma_tt *ttm_dma)
  207. {
  208. struct ttm_tt *ttm = &ttm_dma->ttm;
  209. drm_free_large(ttm->pages);
  210. ttm->pages = NULL;
  211. drm_free_large(ttm_dma->dma_address);
  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. void *from_virtual;
  248. void *to_virtual;
  249. int i;
  250. int ret = -ENOMEM;
  251. swap_storage = ttm->swap_storage;
  252. BUG_ON(swap_storage == NULL);
  253. swap_space = swap_storage->f_path.dentry->d_inode->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. preempt_disable();
  264. from_virtual = kmap_atomic(from_page);
  265. to_virtual = kmap_atomic(to_page);
  266. memcpy(to_virtual, from_virtual, PAGE_SIZE);
  267. kunmap_atomic(to_virtual);
  268. kunmap_atomic(from_virtual);
  269. preempt_enable();
  270. page_cache_release(from_page);
  271. }
  272. if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTENT_SWAP))
  273. fput(swap_storage);
  274. ttm->swap_storage = NULL;
  275. ttm->page_flags &= ~TTM_PAGE_FLAG_SWAPPED;
  276. return 0;
  277. out_err:
  278. return ret;
  279. }
  280. int ttm_tt_swapout(struct ttm_tt *ttm, struct file *persistent_swap_storage)
  281. {
  282. struct address_space *swap_space;
  283. struct file *swap_storage;
  284. struct page *from_page;
  285. struct page *to_page;
  286. void *from_virtual;
  287. void *to_virtual;
  288. int i;
  289. int ret = -ENOMEM;
  290. BUG_ON(ttm->state != tt_unbound && ttm->state != tt_unpopulated);
  291. BUG_ON(ttm->caching_state != tt_cached);
  292. if (!persistent_swap_storage) {
  293. swap_storage = shmem_file_setup("ttm swap",
  294. ttm->num_pages << PAGE_SHIFT,
  295. 0);
  296. if (unlikely(IS_ERR(swap_storage))) {
  297. pr_err("Failed allocating swap storage\n");
  298. return PTR_ERR(swap_storage);
  299. }
  300. } else
  301. swap_storage = persistent_swap_storage;
  302. swap_space = swap_storage->f_path.dentry->d_inode->i_mapping;
  303. for (i = 0; i < ttm->num_pages; ++i) {
  304. from_page = ttm->pages[i];
  305. if (unlikely(from_page == NULL))
  306. continue;
  307. to_page = shmem_read_mapping_page(swap_space, i);
  308. if (unlikely(IS_ERR(to_page))) {
  309. ret = PTR_ERR(to_page);
  310. goto out_err;
  311. }
  312. preempt_disable();
  313. from_virtual = kmap_atomic(from_page);
  314. to_virtual = kmap_atomic(to_page);
  315. memcpy(to_virtual, from_virtual, PAGE_SIZE);
  316. kunmap_atomic(to_virtual);
  317. kunmap_atomic(from_virtual);
  318. preempt_enable();
  319. set_page_dirty(to_page);
  320. mark_page_accessed(to_page);
  321. page_cache_release(to_page);
  322. }
  323. ttm->bdev->driver->ttm_tt_unpopulate(ttm);
  324. ttm->swap_storage = swap_storage;
  325. ttm->page_flags |= TTM_PAGE_FLAG_SWAPPED;
  326. if (persistent_swap_storage)
  327. ttm->page_flags |= TTM_PAGE_FLAG_PERSISTENT_SWAP;
  328. return 0;
  329. out_err:
  330. if (!persistent_swap_storage)
  331. fput(swap_storage);
  332. return ret;
  333. }