ttm_tt.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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_tt_alloc_page_directory(ttm);
  167. if (!ttm->pages) {
  168. ttm_tt_destroy(ttm);
  169. printk(KERN_ERR TTM_PFX "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. INIT_LIST_HEAD(&ttm_dma->pages_list);
  194. ttm_dma_tt_alloc_page_directory(ttm_dma);
  195. if (!ttm->pages || !ttm_dma->dma_address) {
  196. ttm_tt_destroy(ttm);
  197. printk(KERN_ERR TTM_PFX "Failed allocating page table\n");
  198. return -ENOMEM;
  199. }
  200. return 0;
  201. }
  202. EXPORT_SYMBOL(ttm_dma_tt_init);
  203. void ttm_dma_tt_fini(struct ttm_dma_tt *ttm_dma)
  204. {
  205. struct ttm_tt *ttm = &ttm_dma->ttm;
  206. drm_free_large(ttm->pages);
  207. ttm->pages = NULL;
  208. drm_free_large(ttm_dma->dma_address);
  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. void *from_virtual;
  245. void *to_virtual;
  246. int i;
  247. int ret = -ENOMEM;
  248. swap_storage = ttm->swap_storage;
  249. BUG_ON(swap_storage == NULL);
  250. swap_space = swap_storage->f_path.dentry->d_inode->i_mapping;
  251. for (i = 0; i < ttm->num_pages; ++i) {
  252. from_page = shmem_read_mapping_page(swap_space, i);
  253. if (IS_ERR(from_page)) {
  254. ret = PTR_ERR(from_page);
  255. goto out_err;
  256. }
  257. to_page = ttm->pages[i];
  258. if (unlikely(to_page == NULL))
  259. goto out_err;
  260. preempt_disable();
  261. from_virtual = kmap_atomic(from_page, KM_USER0);
  262. to_virtual = kmap_atomic(to_page, KM_USER1);
  263. memcpy(to_virtual, from_virtual, PAGE_SIZE);
  264. kunmap_atomic(to_virtual, KM_USER1);
  265. kunmap_atomic(from_virtual, KM_USER0);
  266. preempt_enable();
  267. page_cache_release(from_page);
  268. }
  269. if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTENT_SWAP))
  270. fput(swap_storage);
  271. ttm->swap_storage = NULL;
  272. ttm->page_flags &= ~TTM_PAGE_FLAG_SWAPPED;
  273. return 0;
  274. out_err:
  275. return ret;
  276. }
  277. int ttm_tt_swapout(struct ttm_tt *ttm, struct file *persistent_swap_storage)
  278. {
  279. struct address_space *swap_space;
  280. struct file *swap_storage;
  281. struct page *from_page;
  282. struct page *to_page;
  283. void *from_virtual;
  284. void *to_virtual;
  285. int i;
  286. int ret = -ENOMEM;
  287. BUG_ON(ttm->state != tt_unbound && ttm->state != tt_unpopulated);
  288. BUG_ON(ttm->caching_state != tt_cached);
  289. if (!persistent_swap_storage) {
  290. swap_storage = shmem_file_setup("ttm swap",
  291. ttm->num_pages << PAGE_SHIFT,
  292. 0);
  293. if (unlikely(IS_ERR(swap_storage))) {
  294. printk(KERN_ERR "Failed allocating swap storage.\n");
  295. return PTR_ERR(swap_storage);
  296. }
  297. } else
  298. swap_storage = persistent_swap_storage;
  299. swap_space = swap_storage->f_path.dentry->d_inode->i_mapping;
  300. for (i = 0; i < ttm->num_pages; ++i) {
  301. from_page = ttm->pages[i];
  302. if (unlikely(from_page == NULL))
  303. continue;
  304. to_page = shmem_read_mapping_page(swap_space, i);
  305. if (unlikely(IS_ERR(to_page))) {
  306. ret = PTR_ERR(to_page);
  307. goto out_err;
  308. }
  309. preempt_disable();
  310. from_virtual = kmap_atomic(from_page, KM_USER0);
  311. to_virtual = kmap_atomic(to_page, KM_USER1);
  312. memcpy(to_virtual, from_virtual, PAGE_SIZE);
  313. kunmap_atomic(to_virtual, KM_USER1);
  314. kunmap_atomic(from_virtual, KM_USER0);
  315. preempt_enable();
  316. set_page_dirty(to_page);
  317. mark_page_accessed(to_page);
  318. page_cache_release(to_page);
  319. }
  320. ttm->bdev->driver->ttm_tt_unpopulate(ttm);
  321. ttm->swap_storage = swap_storage;
  322. ttm->page_flags |= TTM_PAGE_FLAG_SWAPPED;
  323. if (persistent_swap_storage)
  324. ttm->page_flags |= TTM_PAGE_FLAG_PERSISTENT_SWAP;
  325. return 0;
  326. out_err:
  327. if (!persistent_swap_storage)
  328. fput(swap_storage);
  329. return ret;
  330. }