ttm_tt.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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/shmem_fs.h>
  34. #include <linux/file.h>
  35. #include <linux/swap.h>
  36. #include <linux/slab.h>
  37. #include <linux/export.h>
  38. #include "drm_cache.h"
  39. #include "drm_mem_util.h"
  40. #include "ttm/ttm_module.h"
  41. #include "ttm/ttm_bo_driver.h"
  42. #include "ttm/ttm_placement.h"
  43. #include "ttm/ttm_page_alloc.h"
  44. static int ttm_tt_swapin(struct ttm_tt *ttm);
  45. /**
  46. * Allocates storage for pointers to the pages that back the ttm.
  47. */
  48. static void ttm_tt_alloc_page_directory(struct ttm_tt *ttm)
  49. {
  50. ttm->pages = drm_calloc_large(ttm->num_pages, sizeof(*ttm->pages));
  51. ttm->dma_address = drm_calloc_large(ttm->num_pages,
  52. sizeof(*ttm->dma_address));
  53. }
  54. static void ttm_tt_free_page_directory(struct ttm_tt *ttm)
  55. {
  56. drm_free_large(ttm->pages);
  57. ttm->pages = NULL;
  58. drm_free_large(ttm->dma_address);
  59. ttm->dma_address = NULL;
  60. }
  61. static struct page *__ttm_tt_get_page(struct ttm_tt *ttm, int index)
  62. {
  63. struct page *p;
  64. struct list_head h;
  65. struct ttm_mem_global *mem_glob = ttm->glob->mem_glob;
  66. int ret;
  67. while (NULL == (p = ttm->pages[index])) {
  68. INIT_LIST_HEAD(&h);
  69. ret = ttm_get_pages(&h, ttm->page_flags, ttm->caching_state, 1,
  70. &ttm->dma_address[index]);
  71. if (ret != 0)
  72. return NULL;
  73. p = list_first_entry(&h, struct page, lru);
  74. ret = ttm_mem_global_alloc_page(mem_glob, p, false, false);
  75. if (unlikely(ret != 0))
  76. goto out_err;
  77. if (PageHighMem(p))
  78. ttm->pages[--ttm->first_himem_page] = p;
  79. else
  80. ttm->pages[++ttm->last_lomem_page] = p;
  81. }
  82. return p;
  83. out_err:
  84. put_page(p);
  85. return NULL;
  86. }
  87. struct page *ttm_tt_get_page(struct ttm_tt *ttm, int index)
  88. {
  89. int ret;
  90. if (unlikely(ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)) {
  91. ret = ttm_tt_swapin(ttm);
  92. if (unlikely(ret != 0))
  93. return NULL;
  94. }
  95. return __ttm_tt_get_page(ttm, index);
  96. }
  97. int ttm_tt_populate(struct ttm_tt *ttm)
  98. {
  99. struct page *page;
  100. unsigned long i;
  101. struct ttm_backend *be;
  102. int ret;
  103. if (ttm->state != tt_unpopulated)
  104. return 0;
  105. if (unlikely(ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)) {
  106. ret = ttm_tt_swapin(ttm);
  107. if (unlikely(ret != 0))
  108. return ret;
  109. }
  110. be = ttm->be;
  111. for (i = 0; i < ttm->num_pages; ++i) {
  112. page = __ttm_tt_get_page(ttm, i);
  113. if (!page)
  114. return -ENOMEM;
  115. }
  116. be->func->populate(be, ttm->num_pages, ttm->pages,
  117. ttm->dummy_read_page, ttm->dma_address);
  118. ttm->state = tt_unbound;
  119. return 0;
  120. }
  121. EXPORT_SYMBOL(ttm_tt_populate);
  122. #ifdef CONFIG_X86
  123. static inline int ttm_tt_set_page_caching(struct page *p,
  124. enum ttm_caching_state c_old,
  125. enum ttm_caching_state c_new)
  126. {
  127. int ret = 0;
  128. if (PageHighMem(p))
  129. return 0;
  130. if (c_old != tt_cached) {
  131. /* p isn't in the default caching state, set it to
  132. * writeback first to free its current memtype. */
  133. ret = set_pages_wb(p, 1);
  134. if (ret)
  135. return ret;
  136. }
  137. if (c_new == tt_wc)
  138. ret = set_memory_wc((unsigned long) page_address(p), 1);
  139. else if (c_new == tt_uncached)
  140. ret = set_pages_uc(p, 1);
  141. return ret;
  142. }
  143. #else /* CONFIG_X86 */
  144. static inline int ttm_tt_set_page_caching(struct page *p,
  145. enum ttm_caching_state c_old,
  146. enum ttm_caching_state c_new)
  147. {
  148. return 0;
  149. }
  150. #endif /* CONFIG_X86 */
  151. /*
  152. * Change caching policy for the linear kernel map
  153. * for range of pages in a ttm.
  154. */
  155. static int ttm_tt_set_caching(struct ttm_tt *ttm,
  156. enum ttm_caching_state c_state)
  157. {
  158. int i, j;
  159. struct page *cur_page;
  160. int ret;
  161. if (ttm->caching_state == c_state)
  162. return 0;
  163. if (ttm->state == tt_unpopulated) {
  164. /* Change caching but don't populate */
  165. ttm->caching_state = c_state;
  166. return 0;
  167. }
  168. if (ttm->caching_state == tt_cached)
  169. drm_clflush_pages(ttm->pages, ttm->num_pages);
  170. for (i = 0; i < ttm->num_pages; ++i) {
  171. cur_page = ttm->pages[i];
  172. if (likely(cur_page != NULL)) {
  173. ret = ttm_tt_set_page_caching(cur_page,
  174. ttm->caching_state,
  175. c_state);
  176. if (unlikely(ret != 0))
  177. goto out_err;
  178. }
  179. }
  180. ttm->caching_state = c_state;
  181. return 0;
  182. out_err:
  183. for (j = 0; j < i; ++j) {
  184. cur_page = ttm->pages[j];
  185. if (likely(cur_page != NULL)) {
  186. (void)ttm_tt_set_page_caching(cur_page, c_state,
  187. ttm->caching_state);
  188. }
  189. }
  190. return ret;
  191. }
  192. int ttm_tt_set_placement_caching(struct ttm_tt *ttm, uint32_t placement)
  193. {
  194. enum ttm_caching_state state;
  195. if (placement & TTM_PL_FLAG_WC)
  196. state = tt_wc;
  197. else if (placement & TTM_PL_FLAG_UNCACHED)
  198. state = tt_uncached;
  199. else
  200. state = tt_cached;
  201. return ttm_tt_set_caching(ttm, state);
  202. }
  203. EXPORT_SYMBOL(ttm_tt_set_placement_caching);
  204. static void ttm_tt_free_alloced_pages(struct ttm_tt *ttm)
  205. {
  206. int i;
  207. unsigned count = 0;
  208. struct list_head h;
  209. struct page *cur_page;
  210. struct ttm_backend *be = ttm->be;
  211. INIT_LIST_HEAD(&h);
  212. if (be)
  213. be->func->clear(be);
  214. for (i = 0; i < ttm->num_pages; ++i) {
  215. cur_page = ttm->pages[i];
  216. ttm->pages[i] = NULL;
  217. if (cur_page) {
  218. if (page_count(cur_page) != 1)
  219. printk(KERN_ERR TTM_PFX
  220. "Erroneous page count. "
  221. "Leaking pages.\n");
  222. ttm_mem_global_free_page(ttm->glob->mem_glob,
  223. cur_page);
  224. list_add(&cur_page->lru, &h);
  225. count++;
  226. }
  227. }
  228. ttm_put_pages(&h, count, ttm->page_flags, ttm->caching_state,
  229. ttm->dma_address);
  230. ttm->state = tt_unpopulated;
  231. ttm->first_himem_page = ttm->num_pages;
  232. ttm->last_lomem_page = -1;
  233. }
  234. void ttm_tt_destroy(struct ttm_tt *ttm)
  235. {
  236. struct ttm_backend *be;
  237. if (unlikely(ttm == NULL))
  238. return;
  239. be = ttm->be;
  240. if (likely(be != NULL)) {
  241. be->func->destroy(be);
  242. ttm->be = NULL;
  243. }
  244. if (likely(ttm->pages != NULL)) {
  245. ttm_tt_free_alloced_pages(ttm);
  246. ttm_tt_free_page_directory(ttm);
  247. }
  248. if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTENT_SWAP) &&
  249. ttm->swap_storage)
  250. fput(ttm->swap_storage);
  251. kfree(ttm);
  252. }
  253. struct ttm_tt *ttm_tt_create(struct ttm_bo_device *bdev, unsigned long size,
  254. uint32_t page_flags, struct page *dummy_read_page)
  255. {
  256. struct ttm_bo_driver *bo_driver = bdev->driver;
  257. struct ttm_tt *ttm;
  258. if (!bo_driver)
  259. return NULL;
  260. ttm = kzalloc(sizeof(*ttm), GFP_KERNEL);
  261. if (!ttm)
  262. return NULL;
  263. ttm->glob = bdev->glob;
  264. ttm->num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  265. ttm->first_himem_page = ttm->num_pages;
  266. ttm->last_lomem_page = -1;
  267. ttm->caching_state = tt_cached;
  268. ttm->page_flags = page_flags;
  269. ttm->dummy_read_page = dummy_read_page;
  270. ttm_tt_alloc_page_directory(ttm);
  271. if (!ttm->pages) {
  272. ttm_tt_destroy(ttm);
  273. printk(KERN_ERR TTM_PFX "Failed allocating page table\n");
  274. return NULL;
  275. }
  276. ttm->be = bo_driver->create_ttm_backend_entry(bdev);
  277. if (!ttm->be) {
  278. ttm_tt_destroy(ttm);
  279. printk(KERN_ERR TTM_PFX "Failed creating ttm backend entry\n");
  280. return NULL;
  281. }
  282. ttm->state = tt_unpopulated;
  283. return ttm;
  284. }
  285. void ttm_tt_unbind(struct ttm_tt *ttm)
  286. {
  287. int ret;
  288. struct ttm_backend *be = ttm->be;
  289. if (ttm->state == tt_bound) {
  290. ret = be->func->unbind(be);
  291. BUG_ON(ret);
  292. ttm->state = tt_unbound;
  293. }
  294. }
  295. int ttm_tt_bind(struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem)
  296. {
  297. int ret = 0;
  298. struct ttm_backend *be;
  299. if (!ttm)
  300. return -EINVAL;
  301. if (ttm->state == tt_bound)
  302. return 0;
  303. be = ttm->be;
  304. ret = ttm_tt_populate(ttm);
  305. if (ret)
  306. return ret;
  307. ret = be->func->bind(be, bo_mem);
  308. if (unlikely(ret != 0))
  309. return ret;
  310. ttm->state = tt_bound;
  311. return 0;
  312. }
  313. EXPORT_SYMBOL(ttm_tt_bind);
  314. static int ttm_tt_swapin(struct ttm_tt *ttm)
  315. {
  316. struct address_space *swap_space;
  317. struct file *swap_storage;
  318. struct page *from_page;
  319. struct page *to_page;
  320. void *from_virtual;
  321. void *to_virtual;
  322. int i;
  323. int ret = -ENOMEM;
  324. swap_storage = ttm->swap_storage;
  325. BUG_ON(swap_storage == NULL);
  326. swap_space = swap_storage->f_path.dentry->d_inode->i_mapping;
  327. for (i = 0; i < ttm->num_pages; ++i) {
  328. from_page = shmem_read_mapping_page(swap_space, i);
  329. if (IS_ERR(from_page)) {
  330. ret = PTR_ERR(from_page);
  331. goto out_err;
  332. }
  333. to_page = __ttm_tt_get_page(ttm, i);
  334. if (unlikely(to_page == NULL))
  335. goto out_err;
  336. preempt_disable();
  337. from_virtual = kmap_atomic(from_page, KM_USER0);
  338. to_virtual = kmap_atomic(to_page, KM_USER1);
  339. memcpy(to_virtual, from_virtual, PAGE_SIZE);
  340. kunmap_atomic(to_virtual, KM_USER1);
  341. kunmap_atomic(from_virtual, KM_USER0);
  342. preempt_enable();
  343. page_cache_release(from_page);
  344. }
  345. if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTENT_SWAP))
  346. fput(swap_storage);
  347. ttm->swap_storage = NULL;
  348. ttm->page_flags &= ~TTM_PAGE_FLAG_SWAPPED;
  349. return 0;
  350. out_err:
  351. ttm_tt_free_alloced_pages(ttm);
  352. return ret;
  353. }
  354. int ttm_tt_swapout(struct ttm_tt *ttm, struct file *persistent_swap_storage)
  355. {
  356. struct address_space *swap_space;
  357. struct file *swap_storage;
  358. struct page *from_page;
  359. struct page *to_page;
  360. void *from_virtual;
  361. void *to_virtual;
  362. int i;
  363. int ret = -ENOMEM;
  364. BUG_ON(ttm->state != tt_unbound && ttm->state != tt_unpopulated);
  365. BUG_ON(ttm->caching_state != tt_cached);
  366. if (!persistent_swap_storage) {
  367. swap_storage = shmem_file_setup("ttm swap",
  368. ttm->num_pages << PAGE_SHIFT,
  369. 0);
  370. if (unlikely(IS_ERR(swap_storage))) {
  371. printk(KERN_ERR "Failed allocating swap storage.\n");
  372. return PTR_ERR(swap_storage);
  373. }
  374. } else
  375. swap_storage = persistent_swap_storage;
  376. swap_space = swap_storage->f_path.dentry->d_inode->i_mapping;
  377. for (i = 0; i < ttm->num_pages; ++i) {
  378. from_page = ttm->pages[i];
  379. if (unlikely(from_page == NULL))
  380. continue;
  381. to_page = shmem_read_mapping_page(swap_space, i);
  382. if (unlikely(IS_ERR(to_page))) {
  383. ret = PTR_ERR(to_page);
  384. goto out_err;
  385. }
  386. preempt_disable();
  387. from_virtual = kmap_atomic(from_page, KM_USER0);
  388. to_virtual = kmap_atomic(to_page, KM_USER1);
  389. memcpy(to_virtual, from_virtual, PAGE_SIZE);
  390. kunmap_atomic(to_virtual, KM_USER1);
  391. kunmap_atomic(from_virtual, KM_USER0);
  392. preempt_enable();
  393. set_page_dirty(to_page);
  394. mark_page_accessed(to_page);
  395. page_cache_release(to_page);
  396. }
  397. ttm_tt_free_alloced_pages(ttm);
  398. ttm->swap_storage = swap_storage;
  399. ttm->page_flags |= TTM_PAGE_FLAG_SWAPPED;
  400. if (persistent_swap_storage)
  401. ttm->page_flags |= TTM_PAGE_FLAG_PERSISTENT_SWAP;
  402. return 0;
  403. out_err:
  404. if (!persistent_swap_storage)
  405. fput(swap_storage);
  406. return ret;
  407. }