ttm_tt.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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. #define pr_fmt(fmt) "[TTM] " fmt
  31. #include <linux/sched.h>
  32. #include <linux/pagemap.h>
  33. #include <linux/shmem_fs.h>
  34. #include <linux/file.h>
  35. #include <drm/drm_cache.h>
  36. #include <drm/ttm/ttm_bo_driver.h>
  37. #include <drm/ttm/ttm_page_alloc.h>
  38. #ifdef CONFIG_X86
  39. #include <asm/set_memory.h>
  40. #endif
  41. /**
  42. * Allocates a ttm structure for the given BO.
  43. */
  44. int ttm_tt_create(struct ttm_buffer_object *bo, bool zero_alloc)
  45. {
  46. struct ttm_bo_device *bdev = bo->bdev;
  47. uint32_t page_flags = 0;
  48. reservation_object_assert_held(bo->resv);
  49. if (bdev->need_dma32)
  50. page_flags |= TTM_PAGE_FLAG_DMA32;
  51. if (bdev->no_retry)
  52. page_flags |= TTM_PAGE_FLAG_NO_RETRY;
  53. switch (bo->type) {
  54. case ttm_bo_type_device:
  55. if (zero_alloc)
  56. page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
  57. break;
  58. case ttm_bo_type_kernel:
  59. break;
  60. case ttm_bo_type_sg:
  61. page_flags |= TTM_PAGE_FLAG_SG;
  62. break;
  63. default:
  64. bo->ttm = NULL;
  65. pr_err("Illegal buffer object type\n");
  66. return -EINVAL;
  67. }
  68. bo->ttm = bdev->driver->ttm_tt_create(bo, page_flags);
  69. if (unlikely(bo->ttm == NULL))
  70. return -ENOMEM;
  71. return 0;
  72. }
  73. /**
  74. * Allocates storage for pointers to the pages that back the ttm.
  75. */
  76. static int ttm_tt_alloc_page_directory(struct ttm_tt *ttm)
  77. {
  78. ttm->pages = kvmalloc_array(ttm->num_pages, sizeof(void*),
  79. GFP_KERNEL | __GFP_ZERO);
  80. if (!ttm->pages)
  81. return -ENOMEM;
  82. return 0;
  83. }
  84. static int ttm_dma_tt_alloc_page_directory(struct ttm_dma_tt *ttm)
  85. {
  86. ttm->ttm.pages = kvmalloc_array(ttm->ttm.num_pages,
  87. sizeof(*ttm->ttm.pages) +
  88. sizeof(*ttm->dma_address),
  89. GFP_KERNEL | __GFP_ZERO);
  90. if (!ttm->ttm.pages)
  91. return -ENOMEM;
  92. ttm->dma_address = (void *) (ttm->ttm.pages + ttm->ttm.num_pages);
  93. return 0;
  94. }
  95. static int ttm_sg_tt_alloc_page_directory(struct ttm_dma_tt *ttm)
  96. {
  97. ttm->dma_address = kvmalloc_array(ttm->ttm.num_pages,
  98. sizeof(*ttm->dma_address),
  99. GFP_KERNEL | __GFP_ZERO);
  100. if (!ttm->dma_address)
  101. return -ENOMEM;
  102. return 0;
  103. }
  104. #ifdef CONFIG_X86
  105. static inline int ttm_tt_set_page_caching(struct page *p,
  106. enum ttm_caching_state c_old,
  107. enum ttm_caching_state c_new)
  108. {
  109. int ret = 0;
  110. if (PageHighMem(p))
  111. return 0;
  112. if (c_old != tt_cached) {
  113. /* p isn't in the default caching state, set it to
  114. * writeback first to free its current memtype. */
  115. ret = set_pages_wb(p, 1);
  116. if (ret)
  117. return ret;
  118. }
  119. if (c_new == tt_wc)
  120. ret = set_memory_wc((unsigned long) page_address(p), 1);
  121. else if (c_new == tt_uncached)
  122. ret = set_pages_uc(p, 1);
  123. return ret;
  124. }
  125. #else /* CONFIG_X86 */
  126. static inline int ttm_tt_set_page_caching(struct page *p,
  127. enum ttm_caching_state c_old,
  128. enum ttm_caching_state c_new)
  129. {
  130. return 0;
  131. }
  132. #endif /* CONFIG_X86 */
  133. /*
  134. * Change caching policy for the linear kernel map
  135. * for range of pages in a ttm.
  136. */
  137. static int ttm_tt_set_caching(struct ttm_tt *ttm,
  138. enum ttm_caching_state c_state)
  139. {
  140. int i, j;
  141. struct page *cur_page;
  142. int ret;
  143. if (ttm->caching_state == c_state)
  144. return 0;
  145. if (ttm->state == tt_unpopulated) {
  146. /* Change caching but don't populate */
  147. ttm->caching_state = c_state;
  148. return 0;
  149. }
  150. if (ttm->caching_state == tt_cached)
  151. drm_clflush_pages(ttm->pages, ttm->num_pages);
  152. for (i = 0; i < ttm->num_pages; ++i) {
  153. cur_page = ttm->pages[i];
  154. if (likely(cur_page != NULL)) {
  155. ret = ttm_tt_set_page_caching(cur_page,
  156. ttm->caching_state,
  157. c_state);
  158. if (unlikely(ret != 0))
  159. goto out_err;
  160. }
  161. }
  162. ttm->caching_state = c_state;
  163. return 0;
  164. out_err:
  165. for (j = 0; j < i; ++j) {
  166. cur_page = ttm->pages[j];
  167. if (likely(cur_page != NULL)) {
  168. (void)ttm_tt_set_page_caching(cur_page, c_state,
  169. ttm->caching_state);
  170. }
  171. }
  172. return ret;
  173. }
  174. int ttm_tt_set_placement_caching(struct ttm_tt *ttm, uint32_t placement)
  175. {
  176. enum ttm_caching_state state;
  177. if (placement & TTM_PL_FLAG_WC)
  178. state = tt_wc;
  179. else if (placement & TTM_PL_FLAG_UNCACHED)
  180. state = tt_uncached;
  181. else
  182. state = tt_cached;
  183. return ttm_tt_set_caching(ttm, state);
  184. }
  185. EXPORT_SYMBOL(ttm_tt_set_placement_caching);
  186. void ttm_tt_destroy(struct ttm_tt *ttm)
  187. {
  188. if (ttm == NULL)
  189. return;
  190. ttm_tt_unbind(ttm);
  191. if (ttm->state == tt_unbound)
  192. ttm_tt_unpopulate(ttm);
  193. if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTENT_SWAP) &&
  194. ttm->swap_storage)
  195. fput(ttm->swap_storage);
  196. ttm->swap_storage = NULL;
  197. ttm->func->destroy(ttm);
  198. }
  199. void ttm_tt_init_fields(struct ttm_tt *ttm, struct ttm_buffer_object *bo,
  200. uint32_t page_flags)
  201. {
  202. ttm->bdev = bo->bdev;
  203. ttm->num_pages = bo->num_pages;
  204. ttm->caching_state = tt_cached;
  205. ttm->page_flags = page_flags;
  206. ttm->state = tt_unpopulated;
  207. ttm->swap_storage = NULL;
  208. ttm->sg = bo->sg;
  209. }
  210. int ttm_tt_init(struct ttm_tt *ttm, struct ttm_buffer_object *bo,
  211. uint32_t page_flags)
  212. {
  213. ttm_tt_init_fields(ttm, bo, page_flags);
  214. if (ttm_tt_alloc_page_directory(ttm)) {
  215. ttm_tt_destroy(ttm);
  216. pr_err("Failed allocating page table\n");
  217. return -ENOMEM;
  218. }
  219. return 0;
  220. }
  221. EXPORT_SYMBOL(ttm_tt_init);
  222. void ttm_tt_fini(struct ttm_tt *ttm)
  223. {
  224. kvfree(ttm->pages);
  225. ttm->pages = NULL;
  226. }
  227. EXPORT_SYMBOL(ttm_tt_fini);
  228. int ttm_dma_tt_init(struct ttm_dma_tt *ttm_dma, struct ttm_buffer_object *bo,
  229. uint32_t page_flags)
  230. {
  231. struct ttm_tt *ttm = &ttm_dma->ttm;
  232. ttm_tt_init_fields(ttm, bo, page_flags);
  233. INIT_LIST_HEAD(&ttm_dma->pages_list);
  234. if (ttm_dma_tt_alloc_page_directory(ttm_dma)) {
  235. ttm_tt_destroy(ttm);
  236. pr_err("Failed allocating page table\n");
  237. return -ENOMEM;
  238. }
  239. return 0;
  240. }
  241. EXPORT_SYMBOL(ttm_dma_tt_init);
  242. int ttm_sg_tt_init(struct ttm_dma_tt *ttm_dma, struct ttm_buffer_object *bo,
  243. uint32_t page_flags)
  244. {
  245. struct ttm_tt *ttm = &ttm_dma->ttm;
  246. int ret;
  247. ttm_tt_init_fields(ttm, bo, page_flags);
  248. INIT_LIST_HEAD(&ttm_dma->pages_list);
  249. if (page_flags & TTM_PAGE_FLAG_SG)
  250. ret = ttm_sg_tt_alloc_page_directory(ttm_dma);
  251. else
  252. ret = ttm_dma_tt_alloc_page_directory(ttm_dma);
  253. if (ret) {
  254. ttm_tt_destroy(ttm);
  255. pr_err("Failed allocating page table\n");
  256. return -ENOMEM;
  257. }
  258. return 0;
  259. }
  260. EXPORT_SYMBOL(ttm_sg_tt_init);
  261. void ttm_dma_tt_fini(struct ttm_dma_tt *ttm_dma)
  262. {
  263. struct ttm_tt *ttm = &ttm_dma->ttm;
  264. if (ttm->pages)
  265. kvfree(ttm->pages);
  266. else
  267. kvfree(ttm_dma->dma_address);
  268. ttm->pages = NULL;
  269. ttm_dma->dma_address = NULL;
  270. }
  271. EXPORT_SYMBOL(ttm_dma_tt_fini);
  272. void ttm_tt_unbind(struct ttm_tt *ttm)
  273. {
  274. int ret;
  275. if (ttm->state == tt_bound) {
  276. ret = ttm->func->unbind(ttm);
  277. BUG_ON(ret);
  278. ttm->state = tt_unbound;
  279. }
  280. }
  281. int ttm_tt_bind(struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem,
  282. struct ttm_operation_ctx *ctx)
  283. {
  284. int ret = 0;
  285. if (!ttm)
  286. return -EINVAL;
  287. if (ttm->state == tt_bound)
  288. return 0;
  289. ret = ttm_tt_populate(ttm, ctx);
  290. if (ret)
  291. return ret;
  292. ret = ttm->func->bind(ttm, bo_mem);
  293. if (unlikely(ret != 0))
  294. return ret;
  295. ttm->state = tt_bound;
  296. return 0;
  297. }
  298. EXPORT_SYMBOL(ttm_tt_bind);
  299. int ttm_tt_swapin(struct ttm_tt *ttm)
  300. {
  301. struct address_space *swap_space;
  302. struct file *swap_storage;
  303. struct page *from_page;
  304. struct page *to_page;
  305. int i;
  306. int ret = -ENOMEM;
  307. swap_storage = ttm->swap_storage;
  308. BUG_ON(swap_storage == NULL);
  309. swap_space = swap_storage->f_mapping;
  310. for (i = 0; i < ttm->num_pages; ++i) {
  311. gfp_t gfp_mask = mapping_gfp_mask(swap_space);
  312. gfp_mask |= (ttm->page_flags & TTM_PAGE_FLAG_NO_RETRY ? __GFP_RETRY_MAYFAIL : 0);
  313. from_page = shmem_read_mapping_page_gfp(swap_space, i, gfp_mask);
  314. if (IS_ERR(from_page)) {
  315. ret = PTR_ERR(from_page);
  316. goto out_err;
  317. }
  318. to_page = ttm->pages[i];
  319. if (unlikely(to_page == NULL))
  320. goto out_err;
  321. copy_highpage(to_page, from_page);
  322. put_page(from_page);
  323. }
  324. if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTENT_SWAP))
  325. fput(swap_storage);
  326. ttm->swap_storage = NULL;
  327. ttm->page_flags &= ~TTM_PAGE_FLAG_SWAPPED;
  328. return 0;
  329. out_err:
  330. return ret;
  331. }
  332. int ttm_tt_swapout(struct ttm_tt *ttm, struct file *persistent_swap_storage)
  333. {
  334. struct address_space *swap_space;
  335. struct file *swap_storage;
  336. struct page *from_page;
  337. struct page *to_page;
  338. int i;
  339. int ret = -ENOMEM;
  340. BUG_ON(ttm->state != tt_unbound && ttm->state != tt_unpopulated);
  341. BUG_ON(ttm->caching_state != tt_cached);
  342. if (!persistent_swap_storage) {
  343. swap_storage = shmem_file_setup("ttm swap",
  344. ttm->num_pages << PAGE_SHIFT,
  345. 0);
  346. if (IS_ERR(swap_storage)) {
  347. pr_err("Failed allocating swap storage\n");
  348. return PTR_ERR(swap_storage);
  349. }
  350. } else {
  351. swap_storage = persistent_swap_storage;
  352. }
  353. swap_space = swap_storage->f_mapping;
  354. for (i = 0; i < ttm->num_pages; ++i) {
  355. gfp_t gfp_mask = mapping_gfp_mask(swap_space);
  356. gfp_mask |= (ttm->page_flags & TTM_PAGE_FLAG_NO_RETRY ? __GFP_RETRY_MAYFAIL : 0);
  357. from_page = ttm->pages[i];
  358. if (unlikely(from_page == NULL))
  359. continue;
  360. to_page = shmem_read_mapping_page_gfp(swap_space, i, gfp_mask);
  361. if (IS_ERR(to_page)) {
  362. ret = PTR_ERR(to_page);
  363. goto out_err;
  364. }
  365. copy_highpage(to_page, from_page);
  366. set_page_dirty(to_page);
  367. mark_page_accessed(to_page);
  368. put_page(to_page);
  369. }
  370. ttm_tt_unpopulate(ttm);
  371. ttm->swap_storage = swap_storage;
  372. ttm->page_flags |= TTM_PAGE_FLAG_SWAPPED;
  373. if (persistent_swap_storage)
  374. ttm->page_flags |= TTM_PAGE_FLAG_PERSISTENT_SWAP;
  375. return 0;
  376. out_err:
  377. if (!persistent_swap_storage)
  378. fput(swap_storage);
  379. return ret;
  380. }
  381. static void ttm_tt_add_mapping(struct ttm_tt *ttm)
  382. {
  383. pgoff_t i;
  384. if (ttm->page_flags & TTM_PAGE_FLAG_SG)
  385. return;
  386. for (i = 0; i < ttm->num_pages; ++i)
  387. ttm->pages[i]->mapping = ttm->bdev->dev_mapping;
  388. }
  389. int ttm_tt_populate(struct ttm_tt *ttm, struct ttm_operation_ctx *ctx)
  390. {
  391. int ret;
  392. if (ttm->state != tt_unpopulated)
  393. return 0;
  394. if (ttm->bdev->driver->ttm_tt_populate)
  395. ret = ttm->bdev->driver->ttm_tt_populate(ttm, ctx);
  396. else
  397. ret = ttm_pool_populate(ttm, ctx);
  398. if (!ret)
  399. ttm_tt_add_mapping(ttm);
  400. return ret;
  401. }
  402. static void ttm_tt_clear_mapping(struct ttm_tt *ttm)
  403. {
  404. pgoff_t i;
  405. struct page **page = ttm->pages;
  406. if (ttm->page_flags & TTM_PAGE_FLAG_SG)
  407. return;
  408. for (i = 0; i < ttm->num_pages; ++i) {
  409. (*page)->mapping = NULL;
  410. (*page++)->index = 0;
  411. }
  412. }
  413. void ttm_tt_unpopulate(struct ttm_tt *ttm)
  414. {
  415. if (ttm->state == tt_unpopulated)
  416. return;
  417. ttm_tt_clear_mapping(ttm);
  418. if (ttm->bdev->driver->ttm_tt_unpopulate)
  419. ttm->bdev->driver->ttm_tt_unpopulate(ttm);
  420. else
  421. ttm_pool_unpopulate(ttm);
  422. }