ttm_tt.c 13 KB

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