ttm_tt.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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. static int ttm_tt_swapin(struct ttm_tt *ttm);
  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(*ttm->pages));
  51. ttm->dma_address = drm_calloc_large(ttm->num_pages,
  52. sizeof(*ttm->dma_address));
  53. }
  54. static void ttm_tt_free_page_directory(struct ttm_tt *ttm)
  55. {
  56. drm_free_large(ttm->pages);
  57. ttm->pages = NULL;
  58. drm_free_large(ttm->dma_address);
  59. ttm->dma_address = NULL;
  60. }
  61. static struct page *__ttm_tt_get_page(struct ttm_tt *ttm, int index)
  62. {
  63. struct page *p;
  64. struct list_head h;
  65. struct ttm_mem_global *mem_glob = ttm->glob->mem_glob;
  66. int ret;
  67. if (NULL == (p = ttm->pages[index])) {
  68. INIT_LIST_HEAD(&h);
  69. ret = ttm_get_pages(&h, ttm->page_flags, ttm->caching_state, 1,
  70. &ttm->dma_address[index]);
  71. if (ret != 0)
  72. return NULL;
  73. p = list_first_entry(&h, struct page, lru);
  74. ret = ttm_mem_global_alloc_page(mem_glob, p, false, false);
  75. if (unlikely(ret != 0))
  76. goto out_err;
  77. ttm->pages[index] = p;
  78. }
  79. return p;
  80. out_err:
  81. INIT_LIST_HEAD(&h);
  82. list_add(&p->lru, &h);
  83. ttm_put_pages(&h, 1, ttm->page_flags,
  84. ttm->caching_state, &ttm->dma_address[index]);
  85. return NULL;
  86. }
  87. struct page *ttm_tt_get_page(struct ttm_tt *ttm, int index)
  88. {
  89. int ret;
  90. if (unlikely(ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)) {
  91. ret = ttm_tt_swapin(ttm);
  92. if (unlikely(ret != 0))
  93. return NULL;
  94. }
  95. return __ttm_tt_get_page(ttm, index);
  96. }
  97. int ttm_tt_populate(struct ttm_tt *ttm)
  98. {
  99. struct page *page;
  100. unsigned long i;
  101. struct ttm_backend *be;
  102. int ret;
  103. if (ttm->state != tt_unpopulated)
  104. return 0;
  105. if (unlikely(ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)) {
  106. ret = ttm_tt_swapin(ttm);
  107. if (unlikely(ret != 0))
  108. return ret;
  109. }
  110. be = ttm->be;
  111. for (i = 0; i < ttm->num_pages; ++i) {
  112. page = __ttm_tt_get_page(ttm, i);
  113. if (!page)
  114. return -ENOMEM;
  115. }
  116. be->func->populate(be, ttm->num_pages, ttm->pages,
  117. ttm->dummy_read_page, ttm->dma_address);
  118. ttm->state = tt_unbound;
  119. return 0;
  120. }
  121. EXPORT_SYMBOL(ttm_tt_populate);
  122. #ifdef CONFIG_X86
  123. static inline int ttm_tt_set_page_caching(struct page *p,
  124. enum ttm_caching_state c_old,
  125. enum ttm_caching_state c_new)
  126. {
  127. int ret = 0;
  128. if (PageHighMem(p))
  129. return 0;
  130. if (c_old != tt_cached) {
  131. /* p isn't in the default caching state, set it to
  132. * writeback first to free its current memtype. */
  133. ret = set_pages_wb(p, 1);
  134. if (ret)
  135. return ret;
  136. }
  137. if (c_new == tt_wc)
  138. ret = set_memory_wc((unsigned long) page_address(p), 1);
  139. else if (c_new == tt_uncached)
  140. ret = set_pages_uc(p, 1);
  141. return ret;
  142. }
  143. #else /* CONFIG_X86 */
  144. static inline int ttm_tt_set_page_caching(struct page *p,
  145. enum ttm_caching_state c_old,
  146. enum ttm_caching_state c_new)
  147. {
  148. return 0;
  149. }
  150. #endif /* CONFIG_X86 */
  151. /*
  152. * Change caching policy for the linear kernel map
  153. * for range of pages in a ttm.
  154. */
  155. static int ttm_tt_set_caching(struct ttm_tt *ttm,
  156. enum ttm_caching_state c_state)
  157. {
  158. int i, j;
  159. struct page *cur_page;
  160. int ret;
  161. if (ttm->caching_state == c_state)
  162. return 0;
  163. if (ttm->state == tt_unpopulated) {
  164. /* Change caching but don't populate */
  165. ttm->caching_state = c_state;
  166. return 0;
  167. }
  168. if (ttm->caching_state == tt_cached)
  169. drm_clflush_pages(ttm->pages, ttm->num_pages);
  170. for (i = 0; i < ttm->num_pages; ++i) {
  171. cur_page = ttm->pages[i];
  172. if (likely(cur_page != NULL)) {
  173. ret = ttm_tt_set_page_caching(cur_page,
  174. ttm->caching_state,
  175. c_state);
  176. if (unlikely(ret != 0))
  177. goto out_err;
  178. }
  179. }
  180. ttm->caching_state = c_state;
  181. return 0;
  182. out_err:
  183. for (j = 0; j < i; ++j) {
  184. cur_page = ttm->pages[j];
  185. if (likely(cur_page != NULL)) {
  186. (void)ttm_tt_set_page_caching(cur_page, c_state,
  187. ttm->caching_state);
  188. }
  189. }
  190. return ret;
  191. }
  192. int ttm_tt_set_placement_caching(struct ttm_tt *ttm, uint32_t placement)
  193. {
  194. enum ttm_caching_state state;
  195. if (placement & TTM_PL_FLAG_WC)
  196. state = tt_wc;
  197. else if (placement & TTM_PL_FLAG_UNCACHED)
  198. state = tt_uncached;
  199. else
  200. state = tt_cached;
  201. return ttm_tt_set_caching(ttm, state);
  202. }
  203. EXPORT_SYMBOL(ttm_tt_set_placement_caching);
  204. static void ttm_tt_free_alloced_pages(struct ttm_tt *ttm)
  205. {
  206. int i;
  207. unsigned count = 0;
  208. struct list_head h;
  209. struct page *cur_page;
  210. struct ttm_backend *be = ttm->be;
  211. INIT_LIST_HEAD(&h);
  212. if (be)
  213. be->func->clear(be);
  214. for (i = 0; i < ttm->num_pages; ++i) {
  215. cur_page = ttm->pages[i];
  216. ttm->pages[i] = NULL;
  217. if (cur_page) {
  218. if (page_count(cur_page) != 1)
  219. printk(KERN_ERR TTM_PFX
  220. "Erroneous page count. "
  221. "Leaking pages.\n");
  222. ttm_mem_global_free_page(ttm->glob->mem_glob,
  223. cur_page);
  224. list_add(&cur_page->lru, &h);
  225. count++;
  226. }
  227. }
  228. ttm_put_pages(&h, count, ttm->page_flags, ttm->caching_state,
  229. ttm->dma_address);
  230. ttm->state = tt_unpopulated;
  231. }
  232. void ttm_tt_destroy(struct ttm_tt *ttm)
  233. {
  234. struct ttm_backend *be;
  235. if (unlikely(ttm == NULL))
  236. return;
  237. be = ttm->be;
  238. if (likely(be != NULL)) {
  239. be->func->destroy(be);
  240. ttm->be = NULL;
  241. }
  242. if (likely(ttm->pages != NULL)) {
  243. ttm_tt_free_alloced_pages(ttm);
  244. ttm_tt_free_page_directory(ttm);
  245. }
  246. if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTENT_SWAP) &&
  247. ttm->swap_storage)
  248. fput(ttm->swap_storage);
  249. kfree(ttm);
  250. }
  251. struct ttm_tt *ttm_tt_create(struct ttm_bo_device *bdev, unsigned long size,
  252. uint32_t page_flags, struct page *dummy_read_page)
  253. {
  254. struct ttm_bo_driver *bo_driver = bdev->driver;
  255. struct ttm_tt *ttm;
  256. if (!bo_driver)
  257. return NULL;
  258. ttm = kzalloc(sizeof(*ttm), GFP_KERNEL);
  259. if (!ttm)
  260. return NULL;
  261. ttm->glob = bdev->glob;
  262. ttm->num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  263. ttm->caching_state = tt_cached;
  264. ttm->page_flags = page_flags;
  265. ttm->dummy_read_page = dummy_read_page;
  266. ttm_tt_alloc_page_directory(ttm);
  267. if (!ttm->pages || !ttm->dma_address) {
  268. ttm_tt_destroy(ttm);
  269. printk(KERN_ERR TTM_PFX "Failed allocating page table\n");
  270. return NULL;
  271. }
  272. ttm->be = bo_driver->create_ttm_backend_entry(bdev);
  273. if (!ttm->be) {
  274. ttm_tt_destroy(ttm);
  275. printk(KERN_ERR TTM_PFX "Failed creating ttm backend entry\n");
  276. return NULL;
  277. }
  278. ttm->state = tt_unpopulated;
  279. return ttm;
  280. }
  281. void ttm_tt_unbind(struct ttm_tt *ttm)
  282. {
  283. int ret;
  284. struct ttm_backend *be = ttm->be;
  285. if (ttm->state == tt_bound) {
  286. ret = be->func->unbind(be);
  287. BUG_ON(ret);
  288. ttm->state = tt_unbound;
  289. }
  290. }
  291. int ttm_tt_bind(struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem)
  292. {
  293. int ret = 0;
  294. struct ttm_backend *be;
  295. if (!ttm)
  296. return -EINVAL;
  297. if (ttm->state == tt_bound)
  298. return 0;
  299. be = ttm->be;
  300. ret = ttm_tt_populate(ttm);
  301. if (ret)
  302. return ret;
  303. ret = be->func->bind(be, bo_mem);
  304. if (unlikely(ret != 0))
  305. return ret;
  306. ttm->state = tt_bound;
  307. return 0;
  308. }
  309. EXPORT_SYMBOL(ttm_tt_bind);
  310. static int ttm_tt_swapin(struct ttm_tt *ttm)
  311. {
  312. struct address_space *swap_space;
  313. struct file *swap_storage;
  314. struct page *from_page;
  315. struct page *to_page;
  316. void *from_virtual;
  317. void *to_virtual;
  318. int i;
  319. int ret = -ENOMEM;
  320. swap_storage = ttm->swap_storage;
  321. BUG_ON(swap_storage == NULL);
  322. swap_space = swap_storage->f_path.dentry->d_inode->i_mapping;
  323. for (i = 0; i < ttm->num_pages; ++i) {
  324. from_page = shmem_read_mapping_page(swap_space, i);
  325. if (IS_ERR(from_page)) {
  326. ret = PTR_ERR(from_page);
  327. goto out_err;
  328. }
  329. to_page = __ttm_tt_get_page(ttm, i);
  330. if (unlikely(to_page == NULL))
  331. goto out_err;
  332. preempt_disable();
  333. from_virtual = kmap_atomic(from_page, KM_USER0);
  334. to_virtual = kmap_atomic(to_page, KM_USER1);
  335. memcpy(to_virtual, from_virtual, PAGE_SIZE);
  336. kunmap_atomic(to_virtual, KM_USER1);
  337. kunmap_atomic(from_virtual, KM_USER0);
  338. preempt_enable();
  339. page_cache_release(from_page);
  340. }
  341. if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTENT_SWAP))
  342. fput(swap_storage);
  343. ttm->swap_storage = NULL;
  344. ttm->page_flags &= ~TTM_PAGE_FLAG_SWAPPED;
  345. return 0;
  346. out_err:
  347. ttm_tt_free_alloced_pages(ttm);
  348. return ret;
  349. }
  350. int ttm_tt_swapout(struct ttm_tt *ttm, struct file *persistent_swap_storage)
  351. {
  352. struct address_space *swap_space;
  353. struct file *swap_storage;
  354. struct page *from_page;
  355. struct page *to_page;
  356. void *from_virtual;
  357. void *to_virtual;
  358. int i;
  359. int ret = -ENOMEM;
  360. BUG_ON(ttm->state != tt_unbound && ttm->state != tt_unpopulated);
  361. BUG_ON(ttm->caching_state != tt_cached);
  362. if (!persistent_swap_storage) {
  363. swap_storage = shmem_file_setup("ttm swap",
  364. ttm->num_pages << PAGE_SHIFT,
  365. 0);
  366. if (unlikely(IS_ERR(swap_storage))) {
  367. printk(KERN_ERR "Failed allocating swap storage.\n");
  368. return PTR_ERR(swap_storage);
  369. }
  370. } else
  371. swap_storage = persistent_swap_storage;
  372. swap_space = swap_storage->f_path.dentry->d_inode->i_mapping;
  373. for (i = 0; i < ttm->num_pages; ++i) {
  374. from_page = ttm->pages[i];
  375. if (unlikely(from_page == NULL))
  376. continue;
  377. to_page = shmem_read_mapping_page(swap_space, i);
  378. if (unlikely(IS_ERR(to_page))) {
  379. ret = PTR_ERR(to_page);
  380. goto out_err;
  381. }
  382. preempt_disable();
  383. from_virtual = kmap_atomic(from_page, KM_USER0);
  384. to_virtual = kmap_atomic(to_page, KM_USER1);
  385. memcpy(to_virtual, from_virtual, PAGE_SIZE);
  386. kunmap_atomic(to_virtual, KM_USER1);
  387. kunmap_atomic(from_virtual, KM_USER0);
  388. preempt_enable();
  389. set_page_dirty(to_page);
  390. mark_page_accessed(to_page);
  391. page_cache_release(to_page);
  392. }
  393. ttm_tt_free_alloced_pages(ttm);
  394. ttm->swap_storage = swap_storage;
  395. ttm->page_flags |= TTM_PAGE_FLAG_SWAPPED;
  396. if (persistent_swap_storage)
  397. ttm->page_flags |= TTM_PAGE_FLAG_PERSISTENT_SWAP;
  398. return 0;
  399. out_err:
  400. if (!persistent_swap_storage)
  401. fput(swap_storage);
  402. return ret;
  403. }