ttm_tt.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. #include <linux/sched.h>
  31. #include <linux/highmem.h>
  32. #include <linux/pagemap.h>
  33. #include <linux/shmem_fs.h>
  34. #include <linux/file.h>
  35. #include <linux/swap.h>
  36. #include <linux/slab.h>
  37. #include <linux/export.h>
  38. #include "drm_cache.h"
  39. #include "drm_mem_util.h"
  40. #include "ttm/ttm_module.h"
  41. #include "ttm/ttm_bo_driver.h"
  42. #include "ttm/ttm_placement.h"
  43. #include "ttm/ttm_page_alloc.h"
  44. /**
  45. * Allocates storage for pointers to the pages that back the ttm.
  46. */
  47. static void ttm_tt_alloc_page_directory(struct ttm_tt *ttm)
  48. {
  49. ttm->pages = drm_calloc_large(ttm->num_pages, sizeof(void*));
  50. }
  51. static void ttm_dma_tt_alloc_page_directory(struct ttm_dma_tt *ttm)
  52. {
  53. ttm->ttm.pages = drm_calloc_large(ttm->ttm.num_pages, sizeof(void*));
  54. ttm->dma_address = drm_calloc_large(ttm->ttm.num_pages,
  55. sizeof(*ttm->dma_address));
  56. }
  57. #ifdef CONFIG_X86
  58. static inline int ttm_tt_set_page_caching(struct page *p,
  59. enum ttm_caching_state c_old,
  60. enum ttm_caching_state c_new)
  61. {
  62. int ret = 0;
  63. if (PageHighMem(p))
  64. return 0;
  65. if (c_old != tt_cached) {
  66. /* p isn't in the default caching state, set it to
  67. * writeback first to free its current memtype. */
  68. ret = set_pages_wb(p, 1);
  69. if (ret)
  70. return ret;
  71. }
  72. if (c_new == tt_wc)
  73. ret = set_memory_wc((unsigned long) page_address(p), 1);
  74. else if (c_new == tt_uncached)
  75. ret = set_pages_uc(p, 1);
  76. return ret;
  77. }
  78. #else /* CONFIG_X86 */
  79. static inline int ttm_tt_set_page_caching(struct page *p,
  80. enum ttm_caching_state c_old,
  81. enum ttm_caching_state c_new)
  82. {
  83. return 0;
  84. }
  85. #endif /* CONFIG_X86 */
  86. /*
  87. * Change caching policy for the linear kernel map
  88. * for range of pages in a ttm.
  89. */
  90. static int ttm_tt_set_caching(struct ttm_tt *ttm,
  91. enum ttm_caching_state c_state)
  92. {
  93. int i, j;
  94. struct page *cur_page;
  95. int ret;
  96. if (ttm->caching_state == c_state)
  97. return 0;
  98. if (ttm->state == tt_unpopulated) {
  99. /* Change caching but don't populate */
  100. ttm->caching_state = c_state;
  101. return 0;
  102. }
  103. if (ttm->caching_state == tt_cached)
  104. drm_clflush_pages(ttm->pages, ttm->num_pages);
  105. for (i = 0; i < ttm->num_pages; ++i) {
  106. cur_page = ttm->pages[i];
  107. if (likely(cur_page != NULL)) {
  108. ret = ttm_tt_set_page_caching(cur_page,
  109. ttm->caching_state,
  110. c_state);
  111. if (unlikely(ret != 0))
  112. goto out_err;
  113. }
  114. }
  115. ttm->caching_state = c_state;
  116. return 0;
  117. out_err:
  118. for (j = 0; j < i; ++j) {
  119. cur_page = ttm->pages[j];
  120. if (likely(cur_page != NULL)) {
  121. (void)ttm_tt_set_page_caching(cur_page, c_state,
  122. ttm->caching_state);
  123. }
  124. }
  125. return ret;
  126. }
  127. int ttm_tt_set_placement_caching(struct ttm_tt *ttm, uint32_t placement)
  128. {
  129. enum ttm_caching_state state;
  130. if (placement & TTM_PL_FLAG_WC)
  131. state = tt_wc;
  132. else if (placement & TTM_PL_FLAG_UNCACHED)
  133. state = tt_uncached;
  134. else
  135. state = tt_cached;
  136. return ttm_tt_set_caching(ttm, state);
  137. }
  138. EXPORT_SYMBOL(ttm_tt_set_placement_caching);
  139. void ttm_tt_destroy(struct ttm_tt *ttm)
  140. {
  141. if (unlikely(ttm == NULL))
  142. return;
  143. if (ttm->state == tt_bound) {
  144. ttm_tt_unbind(ttm);
  145. }
  146. if (likely(ttm->pages != NULL)) {
  147. ttm->bdev->driver->ttm_tt_unpopulate(ttm);
  148. }
  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. printk(KERN_ERR TTM_PFX "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. printk(KERN_ERR TTM_PFX "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. void *from_virtual;
  247. void *to_virtual;
  248. int i;
  249. int ret = -ENOMEM;
  250. swap_storage = ttm->swap_storage;
  251. BUG_ON(swap_storage == NULL);
  252. swap_space = swap_storage->f_path.dentry->d_inode->i_mapping;
  253. for (i = 0; i < ttm->num_pages; ++i) {
  254. from_page = shmem_read_mapping_page(swap_space, i);
  255. if (IS_ERR(from_page)) {
  256. ret = PTR_ERR(from_page);
  257. goto out_err;
  258. }
  259. to_page = ttm->pages[i];
  260. if (unlikely(to_page == NULL))
  261. goto out_err;
  262. preempt_disable();
  263. from_virtual = kmap_atomic(from_page, KM_USER0);
  264. to_virtual = kmap_atomic(to_page, KM_USER1);
  265. memcpy(to_virtual, from_virtual, PAGE_SIZE);
  266. kunmap_atomic(to_virtual, KM_USER1);
  267. kunmap_atomic(from_virtual, KM_USER0);
  268. preempt_enable();
  269. page_cache_release(from_page);
  270. }
  271. if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTENT_SWAP))
  272. fput(swap_storage);
  273. ttm->swap_storage = NULL;
  274. ttm->page_flags &= ~TTM_PAGE_FLAG_SWAPPED;
  275. return 0;
  276. out_err:
  277. return ret;
  278. }
  279. int ttm_tt_swapout(struct ttm_tt *ttm, struct file *persistent_swap_storage)
  280. {
  281. struct address_space *swap_space;
  282. struct file *swap_storage;
  283. struct page *from_page;
  284. struct page *to_page;
  285. void *from_virtual;
  286. void *to_virtual;
  287. int i;
  288. int ret = -ENOMEM;
  289. BUG_ON(ttm->state != tt_unbound && ttm->state != tt_unpopulated);
  290. BUG_ON(ttm->caching_state != tt_cached);
  291. if (!persistent_swap_storage) {
  292. swap_storage = shmem_file_setup("ttm swap",
  293. ttm->num_pages << PAGE_SHIFT,
  294. 0);
  295. if (unlikely(IS_ERR(swap_storage))) {
  296. printk(KERN_ERR "Failed allocating swap storage.\n");
  297. return PTR_ERR(swap_storage);
  298. }
  299. } else
  300. swap_storage = persistent_swap_storage;
  301. swap_space = swap_storage->f_path.dentry->d_inode->i_mapping;
  302. for (i = 0; i < ttm->num_pages; ++i) {
  303. from_page = ttm->pages[i];
  304. if (unlikely(from_page == NULL))
  305. continue;
  306. to_page = shmem_read_mapping_page(swap_space, i);
  307. if (unlikely(IS_ERR(to_page))) {
  308. ret = PTR_ERR(to_page);
  309. goto out_err;
  310. }
  311. preempt_disable();
  312. from_virtual = kmap_atomic(from_page, KM_USER0);
  313. to_virtual = kmap_atomic(to_page, KM_USER1);
  314. memcpy(to_virtual, from_virtual, PAGE_SIZE);
  315. kunmap_atomic(to_virtual, KM_USER1);
  316. kunmap_atomic(from_virtual, KM_USER0);
  317. preempt_enable();
  318. set_page_dirty(to_page);
  319. mark_page_accessed(to_page);
  320. page_cache_release(to_page);
  321. }
  322. ttm->bdev->driver->ttm_tt_unpopulate(ttm);
  323. ttm->swap_storage = swap_storage;
  324. ttm->page_flags |= TTM_PAGE_FLAG_SWAPPED;
  325. if (persistent_swap_storage)
  326. ttm->page_flags |= TTM_PAGE_FLAG_PERSISTENT_SWAP;
  327. return 0;
  328. out_err:
  329. if (!persistent_swap_storage)
  330. fput(swap_storage);
  331. return ret;
  332. }