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