ttm_tt.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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_state)
  169. {
  170. int ret = 0;
  171. if (PageHighMem(p))
  172. return 0;
  173. if (get_page_memtype(p) != -1) {
  174. /* p isn't in the default caching state, set it to
  175. * writeback first to free its current memtype. */
  176. ret = set_pages_wb(p, 1);
  177. if (ret)
  178. return ret;
  179. }
  180. if (c_state == tt_wc)
  181. ret = set_memory_wc((unsigned long) page_address(p), 1);
  182. else if (c_state == tt_uncached)
  183. ret = set_pages_uc(p, 1);
  184. return ret;
  185. }
  186. #else /* CONFIG_X86 */
  187. static inline int ttm_tt_set_page_caching(struct page *p,
  188. enum ttm_caching_state c_state)
  189. {
  190. return 0;
  191. }
  192. #endif /* CONFIG_X86 */
  193. /*
  194. * Change caching policy for the linear kernel map
  195. * for range of pages in a ttm.
  196. */
  197. static int ttm_tt_set_caching(struct ttm_tt *ttm,
  198. enum ttm_caching_state c_state)
  199. {
  200. int i, j;
  201. struct page *cur_page;
  202. int ret;
  203. if (ttm->caching_state == c_state)
  204. return 0;
  205. if (c_state != tt_cached) {
  206. ret = ttm_tt_populate(ttm);
  207. if (unlikely(ret != 0))
  208. return ret;
  209. }
  210. if (ttm->caching_state == tt_cached)
  211. drm_clflush_pages(ttm->pages, ttm->num_pages);
  212. for (i = 0; i < ttm->num_pages; ++i) {
  213. cur_page = ttm->pages[i];
  214. if (likely(cur_page != NULL)) {
  215. ret = ttm_tt_set_page_caching(cur_page, c_state);
  216. if (unlikely(ret != 0))
  217. goto out_err;
  218. }
  219. }
  220. ttm->caching_state = c_state;
  221. return 0;
  222. out_err:
  223. for (j = 0; j < i; ++j) {
  224. cur_page = ttm->pages[j];
  225. if (likely(cur_page != NULL)) {
  226. (void)ttm_tt_set_page_caching(cur_page,
  227. ttm->caching_state);
  228. }
  229. }
  230. return ret;
  231. }
  232. int ttm_tt_set_placement_caching(struct ttm_tt *ttm, uint32_t placement)
  233. {
  234. enum ttm_caching_state state;
  235. if (placement & TTM_PL_FLAG_WC)
  236. state = tt_wc;
  237. else if (placement & TTM_PL_FLAG_UNCACHED)
  238. state = tt_uncached;
  239. else
  240. state = tt_cached;
  241. return ttm_tt_set_caching(ttm, state);
  242. }
  243. EXPORT_SYMBOL(ttm_tt_set_placement_caching);
  244. static void ttm_tt_free_alloced_pages(struct ttm_tt *ttm)
  245. {
  246. int i;
  247. struct page *cur_page;
  248. struct ttm_backend *be = ttm->be;
  249. if (be)
  250. be->func->clear(be);
  251. (void)ttm_tt_set_caching(ttm, tt_cached);
  252. for (i = 0; i < ttm->num_pages; ++i) {
  253. cur_page = ttm->pages[i];
  254. ttm->pages[i] = NULL;
  255. if (cur_page) {
  256. if (page_count(cur_page) != 1)
  257. printk(KERN_ERR TTM_PFX
  258. "Erroneous page count. "
  259. "Leaking pages.\n");
  260. ttm_mem_global_free_page(ttm->glob->mem_glob,
  261. cur_page);
  262. __free_page(cur_page);
  263. }
  264. }
  265. ttm->state = tt_unpopulated;
  266. ttm->first_himem_page = ttm->num_pages;
  267. ttm->last_lomem_page = -1;
  268. }
  269. void ttm_tt_destroy(struct ttm_tt *ttm)
  270. {
  271. struct ttm_backend *be;
  272. if (unlikely(ttm == NULL))
  273. return;
  274. be = ttm->be;
  275. if (likely(be != NULL)) {
  276. be->func->destroy(be);
  277. ttm->be = NULL;
  278. }
  279. if (likely(ttm->pages != NULL)) {
  280. if (ttm->page_flags & TTM_PAGE_FLAG_USER)
  281. ttm_tt_free_user_pages(ttm);
  282. else
  283. ttm_tt_free_alloced_pages(ttm);
  284. ttm_tt_free_page_directory(ttm);
  285. }
  286. if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTANT_SWAP) &&
  287. ttm->swap_storage)
  288. fput(ttm->swap_storage);
  289. kfree(ttm);
  290. }
  291. int ttm_tt_set_user(struct ttm_tt *ttm,
  292. struct task_struct *tsk,
  293. unsigned long start, unsigned long num_pages)
  294. {
  295. struct mm_struct *mm = tsk->mm;
  296. int ret;
  297. int write = (ttm->page_flags & TTM_PAGE_FLAG_WRITE) != 0;
  298. struct ttm_mem_global *mem_glob = ttm->glob->mem_glob;
  299. BUG_ON(num_pages != ttm->num_pages);
  300. BUG_ON((ttm->page_flags & TTM_PAGE_FLAG_USER) == 0);
  301. /**
  302. * Account user pages as lowmem pages for now.
  303. */
  304. ret = ttm_mem_global_alloc(mem_glob, num_pages * PAGE_SIZE,
  305. false, false);
  306. if (unlikely(ret != 0))
  307. return ret;
  308. down_read(&mm->mmap_sem);
  309. ret = get_user_pages(tsk, mm, start, num_pages,
  310. write, 0, ttm->pages, NULL);
  311. up_read(&mm->mmap_sem);
  312. if (ret != num_pages && write) {
  313. ttm_tt_free_user_pages(ttm);
  314. ttm_mem_global_free(mem_glob, num_pages * PAGE_SIZE);
  315. return -ENOMEM;
  316. }
  317. ttm->tsk = tsk;
  318. ttm->start = start;
  319. ttm->state = tt_unbound;
  320. return 0;
  321. }
  322. struct ttm_tt *ttm_tt_create(struct ttm_bo_device *bdev, unsigned long size,
  323. uint32_t page_flags, struct page *dummy_read_page)
  324. {
  325. struct ttm_bo_driver *bo_driver = bdev->driver;
  326. struct ttm_tt *ttm;
  327. if (!bo_driver)
  328. return NULL;
  329. ttm = kzalloc(sizeof(*ttm), GFP_KERNEL);
  330. if (!ttm)
  331. return NULL;
  332. ttm->glob = bdev->glob;
  333. ttm->num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  334. ttm->first_himem_page = ttm->num_pages;
  335. ttm->last_lomem_page = -1;
  336. ttm->caching_state = tt_cached;
  337. ttm->page_flags = page_flags;
  338. ttm->dummy_read_page = dummy_read_page;
  339. ttm_tt_alloc_page_directory(ttm);
  340. if (!ttm->pages) {
  341. ttm_tt_destroy(ttm);
  342. printk(KERN_ERR TTM_PFX "Failed allocating page table\n");
  343. return NULL;
  344. }
  345. ttm->be = bo_driver->create_ttm_backend_entry(bdev);
  346. if (!ttm->be) {
  347. ttm_tt_destroy(ttm);
  348. printk(KERN_ERR TTM_PFX "Failed creating ttm backend entry\n");
  349. return NULL;
  350. }
  351. ttm->state = tt_unpopulated;
  352. return ttm;
  353. }
  354. void ttm_tt_unbind(struct ttm_tt *ttm)
  355. {
  356. int ret;
  357. struct ttm_backend *be = ttm->be;
  358. if (ttm->state == tt_bound) {
  359. ret = be->func->unbind(be);
  360. BUG_ON(ret);
  361. ttm->state = tt_unbound;
  362. }
  363. }
  364. int ttm_tt_bind(struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem)
  365. {
  366. int ret = 0;
  367. struct ttm_backend *be;
  368. if (!ttm)
  369. return -EINVAL;
  370. if (ttm->state == tt_bound)
  371. return 0;
  372. be = ttm->be;
  373. ret = ttm_tt_populate(ttm);
  374. if (ret)
  375. return ret;
  376. ret = be->func->bind(be, bo_mem);
  377. if (ret) {
  378. printk(KERN_ERR TTM_PFX "Couldn't bind backend.\n");
  379. return ret;
  380. }
  381. ttm->state = tt_bound;
  382. if (ttm->page_flags & TTM_PAGE_FLAG_USER)
  383. ttm->page_flags |= TTM_PAGE_FLAG_USER_DIRTY;
  384. return 0;
  385. }
  386. EXPORT_SYMBOL(ttm_tt_bind);
  387. static int ttm_tt_swapin(struct ttm_tt *ttm)
  388. {
  389. struct address_space *swap_space;
  390. struct file *swap_storage;
  391. struct page *from_page;
  392. struct page *to_page;
  393. void *from_virtual;
  394. void *to_virtual;
  395. int i;
  396. int ret = -ENOMEM;
  397. if (ttm->page_flags & TTM_PAGE_FLAG_USER) {
  398. ret = ttm_tt_set_user(ttm, ttm->tsk, ttm->start,
  399. ttm->num_pages);
  400. if (unlikely(ret != 0))
  401. return ret;
  402. ttm->page_flags &= ~TTM_PAGE_FLAG_SWAPPED;
  403. return 0;
  404. }
  405. swap_storage = ttm->swap_storage;
  406. BUG_ON(swap_storage == NULL);
  407. swap_space = swap_storage->f_path.dentry->d_inode->i_mapping;
  408. for (i = 0; i < ttm->num_pages; ++i) {
  409. from_page = read_mapping_page(swap_space, i, NULL);
  410. if (IS_ERR(from_page)) {
  411. ret = PTR_ERR(from_page);
  412. goto out_err;
  413. }
  414. to_page = __ttm_tt_get_page(ttm, i);
  415. if (unlikely(to_page == NULL))
  416. goto out_err;
  417. preempt_disable();
  418. from_virtual = kmap_atomic(from_page, KM_USER0);
  419. to_virtual = kmap_atomic(to_page, KM_USER1);
  420. memcpy(to_virtual, from_virtual, PAGE_SIZE);
  421. kunmap_atomic(to_virtual, KM_USER1);
  422. kunmap_atomic(from_virtual, KM_USER0);
  423. preempt_enable();
  424. page_cache_release(from_page);
  425. }
  426. if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTANT_SWAP))
  427. fput(swap_storage);
  428. ttm->swap_storage = NULL;
  429. ttm->page_flags &= ~TTM_PAGE_FLAG_SWAPPED;
  430. return 0;
  431. out_err:
  432. ttm_tt_free_alloced_pages(ttm);
  433. return ret;
  434. }
  435. int ttm_tt_swapout(struct ttm_tt *ttm, struct file *persistant_swap_storage)
  436. {
  437. struct address_space *swap_space;
  438. struct file *swap_storage;
  439. struct page *from_page;
  440. struct page *to_page;
  441. void *from_virtual;
  442. void *to_virtual;
  443. int i;
  444. int ret = -ENOMEM;
  445. BUG_ON(ttm->state != tt_unbound && ttm->state != tt_unpopulated);
  446. BUG_ON(ttm->caching_state != tt_cached);
  447. /*
  448. * For user buffers, just unpin the pages, as there should be
  449. * vma references.
  450. */
  451. if (ttm->page_flags & TTM_PAGE_FLAG_USER) {
  452. ttm_tt_free_user_pages(ttm);
  453. ttm->page_flags |= TTM_PAGE_FLAG_SWAPPED;
  454. ttm->swap_storage = NULL;
  455. return 0;
  456. }
  457. if (!persistant_swap_storage) {
  458. swap_storage = shmem_file_setup("ttm swap",
  459. ttm->num_pages << PAGE_SHIFT,
  460. 0);
  461. if (unlikely(IS_ERR(swap_storage))) {
  462. printk(KERN_ERR "Failed allocating swap storage.\n");
  463. return PTR_ERR(swap_storage);
  464. }
  465. } else
  466. swap_storage = persistant_swap_storage;
  467. swap_space = swap_storage->f_path.dentry->d_inode->i_mapping;
  468. for (i = 0; i < ttm->num_pages; ++i) {
  469. from_page = ttm->pages[i];
  470. if (unlikely(from_page == NULL))
  471. continue;
  472. to_page = read_mapping_page(swap_space, i, NULL);
  473. if (unlikely(IS_ERR(to_page))) {
  474. ret = PTR_ERR(to_page);
  475. goto out_err;
  476. }
  477. preempt_disable();
  478. from_virtual = kmap_atomic(from_page, KM_USER0);
  479. to_virtual = kmap_atomic(to_page, KM_USER1);
  480. memcpy(to_virtual, from_virtual, PAGE_SIZE);
  481. kunmap_atomic(to_virtual, KM_USER1);
  482. kunmap_atomic(from_virtual, KM_USER0);
  483. preempt_enable();
  484. set_page_dirty(to_page);
  485. mark_page_accessed(to_page);
  486. page_cache_release(to_page);
  487. }
  488. ttm_tt_free_alloced_pages(ttm);
  489. ttm->swap_storage = swap_storage;
  490. ttm->page_flags |= TTM_PAGE_FLAG_SWAPPED;
  491. if (persistant_swap_storage)
  492. ttm->page_flags |= TTM_PAGE_FLAG_PERSISTANT_SWAP;
  493. return 0;
  494. out_err:
  495. if (!persistant_swap_storage)
  496. fput(swap_storage);
  497. return ret;
  498. }