ttm_tt.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  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. static int ttm_tt_swapin(struct ttm_tt *ttm);
  42. /**
  43. * Allocates storage for pointers to the pages that back the ttm.
  44. */
  45. static void ttm_tt_alloc_page_directory(struct ttm_tt *ttm)
  46. {
  47. ttm->pages = drm_calloc_large(ttm->num_pages, sizeof(*ttm->pages));
  48. }
  49. static void ttm_tt_free_page_directory(struct ttm_tt *ttm)
  50. {
  51. drm_free_large(ttm->pages);
  52. ttm->pages = NULL;
  53. }
  54. static struct page *ttm_tt_alloc_page(unsigned page_flags)
  55. {
  56. gfp_t gfp_flags = GFP_USER;
  57. if (page_flags & TTM_PAGE_FLAG_ZERO_ALLOC)
  58. gfp_flags |= __GFP_ZERO;
  59. if (page_flags & TTM_PAGE_FLAG_DMA32)
  60. gfp_flags |= __GFP_DMA32;
  61. else
  62. gfp_flags |= __GFP_HIGHMEM;
  63. return alloc_page(gfp_flags);
  64. }
  65. static void ttm_tt_free_user_pages(struct ttm_tt *ttm)
  66. {
  67. int write;
  68. int dirty;
  69. struct page *page;
  70. int i;
  71. struct ttm_backend *be = ttm->be;
  72. BUG_ON(!(ttm->page_flags & TTM_PAGE_FLAG_USER));
  73. write = ((ttm->page_flags & TTM_PAGE_FLAG_WRITE) != 0);
  74. dirty = ((ttm->page_flags & TTM_PAGE_FLAG_USER_DIRTY) != 0);
  75. if (be)
  76. be->func->clear(be);
  77. for (i = 0; i < ttm->num_pages; ++i) {
  78. page = ttm->pages[i];
  79. if (page == NULL)
  80. continue;
  81. if (page == ttm->dummy_read_page) {
  82. BUG_ON(write);
  83. continue;
  84. }
  85. if (write && dirty && !PageReserved(page))
  86. set_page_dirty_lock(page);
  87. ttm->pages[i] = NULL;
  88. ttm_mem_global_free(ttm->glob->mem_glob, PAGE_SIZE);
  89. put_page(page);
  90. }
  91. ttm->state = tt_unpopulated;
  92. ttm->first_himem_page = ttm->num_pages;
  93. ttm->last_lomem_page = -1;
  94. }
  95. static struct page *__ttm_tt_get_page(struct ttm_tt *ttm, int index)
  96. {
  97. struct page *p;
  98. struct ttm_mem_global *mem_glob = ttm->glob->mem_glob;
  99. int ret;
  100. while (NULL == (p = ttm->pages[index])) {
  101. p = ttm_tt_alloc_page(ttm->page_flags);
  102. if (!p)
  103. return NULL;
  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);
  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 (c_state != tt_cached) {
  194. ret = ttm_tt_populate(ttm);
  195. if (unlikely(ret != 0))
  196. return ret;
  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. struct page *cur_page;
  238. struct ttm_backend *be = ttm->be;
  239. if (be)
  240. be->func->clear(be);
  241. (void)ttm_tt_set_caching(ttm, tt_cached);
  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. __free_page(cur_page);
  253. }
  254. }
  255. ttm->state = tt_unpopulated;
  256. ttm->first_himem_page = ttm->num_pages;
  257. ttm->last_lomem_page = -1;
  258. }
  259. void ttm_tt_destroy(struct ttm_tt *ttm)
  260. {
  261. struct ttm_backend *be;
  262. if (unlikely(ttm == NULL))
  263. return;
  264. be = ttm->be;
  265. if (likely(be != NULL)) {
  266. be->func->destroy(be);
  267. ttm->be = NULL;
  268. }
  269. if (likely(ttm->pages != NULL)) {
  270. if (ttm->page_flags & TTM_PAGE_FLAG_USER)
  271. ttm_tt_free_user_pages(ttm);
  272. else
  273. ttm_tt_free_alloced_pages(ttm);
  274. ttm_tt_free_page_directory(ttm);
  275. }
  276. if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTANT_SWAP) &&
  277. ttm->swap_storage)
  278. fput(ttm->swap_storage);
  279. kfree(ttm);
  280. }
  281. int ttm_tt_set_user(struct ttm_tt *ttm,
  282. struct task_struct *tsk,
  283. unsigned long start, unsigned long num_pages)
  284. {
  285. struct mm_struct *mm = tsk->mm;
  286. int ret;
  287. int write = (ttm->page_flags & TTM_PAGE_FLAG_WRITE) != 0;
  288. struct ttm_mem_global *mem_glob = ttm->glob->mem_glob;
  289. BUG_ON(num_pages != ttm->num_pages);
  290. BUG_ON((ttm->page_flags & TTM_PAGE_FLAG_USER) == 0);
  291. /**
  292. * Account user pages as lowmem pages for now.
  293. */
  294. ret = ttm_mem_global_alloc(mem_glob, num_pages * PAGE_SIZE,
  295. false, false);
  296. if (unlikely(ret != 0))
  297. return ret;
  298. down_read(&mm->mmap_sem);
  299. ret = get_user_pages(tsk, mm, start, num_pages,
  300. write, 0, ttm->pages, NULL);
  301. up_read(&mm->mmap_sem);
  302. if (ret != num_pages && write) {
  303. ttm_tt_free_user_pages(ttm);
  304. ttm_mem_global_free(mem_glob, num_pages * PAGE_SIZE);
  305. return -ENOMEM;
  306. }
  307. ttm->tsk = tsk;
  308. ttm->start = start;
  309. ttm->state = tt_unbound;
  310. return 0;
  311. }
  312. struct ttm_tt *ttm_tt_create(struct ttm_bo_device *bdev, unsigned long size,
  313. uint32_t page_flags, struct page *dummy_read_page)
  314. {
  315. struct ttm_bo_driver *bo_driver = bdev->driver;
  316. struct ttm_tt *ttm;
  317. if (!bo_driver)
  318. return NULL;
  319. ttm = kzalloc(sizeof(*ttm), GFP_KERNEL);
  320. if (!ttm)
  321. return NULL;
  322. ttm->glob = bdev->glob;
  323. ttm->num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  324. ttm->first_himem_page = ttm->num_pages;
  325. ttm->last_lomem_page = -1;
  326. ttm->caching_state = tt_cached;
  327. ttm->page_flags = page_flags;
  328. ttm->dummy_read_page = dummy_read_page;
  329. ttm_tt_alloc_page_directory(ttm);
  330. if (!ttm->pages) {
  331. ttm_tt_destroy(ttm);
  332. printk(KERN_ERR TTM_PFX "Failed allocating page table\n");
  333. return NULL;
  334. }
  335. ttm->be = bo_driver->create_ttm_backend_entry(bdev);
  336. if (!ttm->be) {
  337. ttm_tt_destroy(ttm);
  338. printk(KERN_ERR TTM_PFX "Failed creating ttm backend entry\n");
  339. return NULL;
  340. }
  341. ttm->state = tt_unpopulated;
  342. return ttm;
  343. }
  344. void ttm_tt_unbind(struct ttm_tt *ttm)
  345. {
  346. int ret;
  347. struct ttm_backend *be = ttm->be;
  348. if (ttm->state == tt_bound) {
  349. ret = be->func->unbind(be);
  350. BUG_ON(ret);
  351. ttm->state = tt_unbound;
  352. }
  353. }
  354. int ttm_tt_bind(struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem)
  355. {
  356. int ret = 0;
  357. struct ttm_backend *be;
  358. if (!ttm)
  359. return -EINVAL;
  360. if (ttm->state == tt_bound)
  361. return 0;
  362. be = ttm->be;
  363. ret = ttm_tt_populate(ttm);
  364. if (ret)
  365. return ret;
  366. ret = be->func->bind(be, bo_mem);
  367. if (ret) {
  368. printk(KERN_ERR TTM_PFX "Couldn't bind backend.\n");
  369. return ret;
  370. }
  371. ttm->state = tt_bound;
  372. if (ttm->page_flags & TTM_PAGE_FLAG_USER)
  373. ttm->page_flags |= TTM_PAGE_FLAG_USER_DIRTY;
  374. return 0;
  375. }
  376. EXPORT_SYMBOL(ttm_tt_bind);
  377. static int ttm_tt_swapin(struct ttm_tt *ttm)
  378. {
  379. struct address_space *swap_space;
  380. struct file *swap_storage;
  381. struct page *from_page;
  382. struct page *to_page;
  383. void *from_virtual;
  384. void *to_virtual;
  385. int i;
  386. int ret = -ENOMEM;
  387. if (ttm->page_flags & TTM_PAGE_FLAG_USER) {
  388. ret = ttm_tt_set_user(ttm, ttm->tsk, ttm->start,
  389. ttm->num_pages);
  390. if (unlikely(ret != 0))
  391. return ret;
  392. ttm->page_flags &= ~TTM_PAGE_FLAG_SWAPPED;
  393. return 0;
  394. }
  395. swap_storage = ttm->swap_storage;
  396. BUG_ON(swap_storage == NULL);
  397. swap_space = swap_storage->f_path.dentry->d_inode->i_mapping;
  398. for (i = 0; i < ttm->num_pages; ++i) {
  399. from_page = read_mapping_page(swap_space, i, NULL);
  400. if (IS_ERR(from_page)) {
  401. ret = PTR_ERR(from_page);
  402. goto out_err;
  403. }
  404. to_page = __ttm_tt_get_page(ttm, i);
  405. if (unlikely(to_page == NULL))
  406. goto out_err;
  407. preempt_disable();
  408. from_virtual = kmap_atomic(from_page, KM_USER0);
  409. to_virtual = kmap_atomic(to_page, KM_USER1);
  410. memcpy(to_virtual, from_virtual, PAGE_SIZE);
  411. kunmap_atomic(to_virtual, KM_USER1);
  412. kunmap_atomic(from_virtual, KM_USER0);
  413. preempt_enable();
  414. page_cache_release(from_page);
  415. }
  416. if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTANT_SWAP))
  417. fput(swap_storage);
  418. ttm->swap_storage = NULL;
  419. ttm->page_flags &= ~TTM_PAGE_FLAG_SWAPPED;
  420. return 0;
  421. out_err:
  422. ttm_tt_free_alloced_pages(ttm);
  423. return ret;
  424. }
  425. int ttm_tt_swapout(struct ttm_tt *ttm, struct file *persistant_swap_storage)
  426. {
  427. struct address_space *swap_space;
  428. struct file *swap_storage;
  429. struct page *from_page;
  430. struct page *to_page;
  431. void *from_virtual;
  432. void *to_virtual;
  433. int i;
  434. int ret = -ENOMEM;
  435. BUG_ON(ttm->state != tt_unbound && ttm->state != tt_unpopulated);
  436. BUG_ON(ttm->caching_state != tt_cached);
  437. /*
  438. * For user buffers, just unpin the pages, as there should be
  439. * vma references.
  440. */
  441. if (ttm->page_flags & TTM_PAGE_FLAG_USER) {
  442. ttm_tt_free_user_pages(ttm);
  443. ttm->page_flags |= TTM_PAGE_FLAG_SWAPPED;
  444. ttm->swap_storage = NULL;
  445. return 0;
  446. }
  447. if (!persistant_swap_storage) {
  448. swap_storage = shmem_file_setup("ttm swap",
  449. ttm->num_pages << PAGE_SHIFT,
  450. 0);
  451. if (unlikely(IS_ERR(swap_storage))) {
  452. printk(KERN_ERR "Failed allocating swap storage.\n");
  453. return PTR_ERR(swap_storage);
  454. }
  455. } else
  456. swap_storage = persistant_swap_storage;
  457. swap_space = swap_storage->f_path.dentry->d_inode->i_mapping;
  458. for (i = 0; i < ttm->num_pages; ++i) {
  459. from_page = ttm->pages[i];
  460. if (unlikely(from_page == NULL))
  461. continue;
  462. to_page = read_mapping_page(swap_space, i, NULL);
  463. if (unlikely(IS_ERR(to_page))) {
  464. ret = PTR_ERR(to_page);
  465. goto out_err;
  466. }
  467. preempt_disable();
  468. from_virtual = kmap_atomic(from_page, KM_USER0);
  469. to_virtual = kmap_atomic(to_page, KM_USER1);
  470. memcpy(to_virtual, from_virtual, PAGE_SIZE);
  471. kunmap_atomic(to_virtual, KM_USER1);
  472. kunmap_atomic(from_virtual, KM_USER0);
  473. preempt_enable();
  474. set_page_dirty(to_page);
  475. mark_page_accessed(to_page);
  476. page_cache_release(to_page);
  477. }
  478. ttm_tt_free_alloced_pages(ttm);
  479. ttm->swap_storage = swap_storage;
  480. ttm->page_flags |= TTM_PAGE_FLAG_SWAPPED;
  481. if (persistant_swap_storage)
  482. ttm->page_flags |= TTM_PAGE_FLAG_PERSISTANT_SWAP;
  483. return 0;
  484. out_err:
  485. if (!persistant_swap_storage)
  486. fput(swap_storage);
  487. return ret;
  488. }