ttm_tt.c 14 KB

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