ttm_tt.c 13 KB

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