ttm_tt.c 14 KB

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