ttm_tt.c 13 KB

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