ttm_tt.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. /* SPDX-License-Identifier: GPL-2.0 OR MIT */
  2. /**************************************************************************
  3. *
  4. * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
  5. * All Rights Reserved.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the
  9. * "Software"), to deal in the Software without restriction, including
  10. * without limitation the rights to use, copy, modify, merge, publish,
  11. * distribute, sub license, and/or sell copies of the Software, and to
  12. * permit persons to whom the Software is furnished to do so, subject to
  13. * the following conditions:
  14. *
  15. * The above copyright notice and this permission notice (including the
  16. * next paragraph) shall be included in all copies or substantial portions
  17. * of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  22. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  23. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  24. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  25. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. *
  27. **************************************************************************/
  28. /*
  29. * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
  30. */
  31. #define pr_fmt(fmt) "[TTM] " fmt
  32. #include <linux/sched.h>
  33. #include <linux/pagemap.h>
  34. #include <linux/shmem_fs.h>
  35. #include <linux/file.h>
  36. #include <drm/drm_cache.h>
  37. #include <drm/ttm/ttm_bo_driver.h>
  38. #include <drm/ttm/ttm_page_alloc.h>
  39. #ifdef CONFIG_X86
  40. #include <asm/set_memory.h>
  41. #endif
  42. /**
  43. * Allocates a ttm structure for the given BO.
  44. */
  45. int ttm_tt_create(struct ttm_buffer_object *bo, bool zero_alloc)
  46. {
  47. struct ttm_bo_device *bdev = bo->bdev;
  48. uint32_t page_flags = 0;
  49. reservation_object_assert_held(bo->resv);
  50. if (bdev->need_dma32)
  51. page_flags |= TTM_PAGE_FLAG_DMA32;
  52. if (bdev->no_retry)
  53. page_flags |= TTM_PAGE_FLAG_NO_RETRY;
  54. switch (bo->type) {
  55. case ttm_bo_type_device:
  56. if (zero_alloc)
  57. page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
  58. break;
  59. case ttm_bo_type_kernel:
  60. break;
  61. case ttm_bo_type_sg:
  62. page_flags |= TTM_PAGE_FLAG_SG;
  63. break;
  64. default:
  65. bo->ttm = NULL;
  66. pr_err("Illegal buffer object type\n");
  67. return -EINVAL;
  68. }
  69. bo->ttm = bdev->driver->ttm_tt_create(bo, page_flags);
  70. if (unlikely(bo->ttm == NULL))
  71. return -ENOMEM;
  72. return 0;
  73. }
  74. /**
  75. * Allocates storage for pointers to the pages that back the ttm.
  76. */
  77. static int ttm_tt_alloc_page_directory(struct ttm_tt *ttm)
  78. {
  79. ttm->pages = kvmalloc_array(ttm->num_pages, sizeof(void*),
  80. GFP_KERNEL | __GFP_ZERO);
  81. if (!ttm->pages)
  82. return -ENOMEM;
  83. return 0;
  84. }
  85. static int ttm_dma_tt_alloc_page_directory(struct ttm_dma_tt *ttm)
  86. {
  87. ttm->ttm.pages = kvmalloc_array(ttm->ttm.num_pages,
  88. sizeof(*ttm->ttm.pages) +
  89. sizeof(*ttm->dma_address),
  90. GFP_KERNEL | __GFP_ZERO);
  91. if (!ttm->ttm.pages)
  92. return -ENOMEM;
  93. ttm->dma_address = (void *) (ttm->ttm.pages + ttm->ttm.num_pages);
  94. return 0;
  95. }
  96. static int ttm_sg_tt_alloc_page_directory(struct ttm_dma_tt *ttm)
  97. {
  98. ttm->dma_address = kvmalloc_array(ttm->ttm.num_pages,
  99. sizeof(*ttm->dma_address),
  100. GFP_KERNEL | __GFP_ZERO);
  101. if (!ttm->dma_address)
  102. return -ENOMEM;
  103. return 0;
  104. }
  105. #ifdef CONFIG_X86
  106. static inline int ttm_tt_set_page_caching(struct page *p,
  107. enum ttm_caching_state c_old,
  108. enum ttm_caching_state c_new)
  109. {
  110. int ret = 0;
  111. if (PageHighMem(p))
  112. return 0;
  113. if (c_old != tt_cached) {
  114. /* p isn't in the default caching state, set it to
  115. * writeback first to free its current memtype. */
  116. ret = set_pages_wb(p, 1);
  117. if (ret)
  118. return ret;
  119. }
  120. if (c_new == tt_wc)
  121. ret = set_memory_wc((unsigned long) page_address(p), 1);
  122. else if (c_new == tt_uncached)
  123. ret = set_pages_uc(p, 1);
  124. return ret;
  125. }
  126. #else /* CONFIG_X86 */
  127. static inline int ttm_tt_set_page_caching(struct page *p,
  128. enum ttm_caching_state c_old,
  129. enum ttm_caching_state c_new)
  130. {
  131. return 0;
  132. }
  133. #endif /* CONFIG_X86 */
  134. /*
  135. * Change caching policy for the linear kernel map
  136. * for range of pages in a ttm.
  137. */
  138. static int ttm_tt_set_caching(struct ttm_tt *ttm,
  139. enum ttm_caching_state c_state)
  140. {
  141. int i, j;
  142. struct page *cur_page;
  143. int ret;
  144. if (ttm->caching_state == c_state)
  145. return 0;
  146. if (ttm->state == tt_unpopulated) {
  147. /* Change caching but don't populate */
  148. ttm->caching_state = c_state;
  149. return 0;
  150. }
  151. if (ttm->caching_state == tt_cached)
  152. drm_clflush_pages(ttm->pages, ttm->num_pages);
  153. for (i = 0; i < ttm->num_pages; ++i) {
  154. cur_page = ttm->pages[i];
  155. if (likely(cur_page != NULL)) {
  156. ret = ttm_tt_set_page_caching(cur_page,
  157. ttm->caching_state,
  158. c_state);
  159. if (unlikely(ret != 0))
  160. goto out_err;
  161. }
  162. }
  163. ttm->caching_state = c_state;
  164. return 0;
  165. out_err:
  166. for (j = 0; j < i; ++j) {
  167. cur_page = ttm->pages[j];
  168. if (likely(cur_page != NULL)) {
  169. (void)ttm_tt_set_page_caching(cur_page, c_state,
  170. ttm->caching_state);
  171. }
  172. }
  173. return ret;
  174. }
  175. int ttm_tt_set_placement_caching(struct ttm_tt *ttm, uint32_t placement)
  176. {
  177. enum ttm_caching_state state;
  178. if (placement & TTM_PL_FLAG_WC)
  179. state = tt_wc;
  180. else if (placement & TTM_PL_FLAG_UNCACHED)
  181. state = tt_uncached;
  182. else
  183. state = tt_cached;
  184. return ttm_tt_set_caching(ttm, state);
  185. }
  186. EXPORT_SYMBOL(ttm_tt_set_placement_caching);
  187. void ttm_tt_destroy(struct ttm_tt *ttm)
  188. {
  189. if (ttm == NULL)
  190. return;
  191. ttm_tt_unbind(ttm);
  192. if (ttm->state == tt_unbound)
  193. ttm_tt_unpopulate(ttm);
  194. if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTENT_SWAP) &&
  195. ttm->swap_storage)
  196. fput(ttm->swap_storage);
  197. ttm->swap_storage = NULL;
  198. ttm->func->destroy(ttm);
  199. }
  200. void ttm_tt_init_fields(struct ttm_tt *ttm, struct ttm_buffer_object *bo,
  201. uint32_t page_flags)
  202. {
  203. ttm->bdev = bo->bdev;
  204. ttm->num_pages = bo->num_pages;
  205. ttm->caching_state = tt_cached;
  206. ttm->page_flags = page_flags;
  207. ttm->state = tt_unpopulated;
  208. ttm->swap_storage = NULL;
  209. ttm->sg = bo->sg;
  210. }
  211. int ttm_tt_init(struct ttm_tt *ttm, struct ttm_buffer_object *bo,
  212. uint32_t page_flags)
  213. {
  214. ttm_tt_init_fields(ttm, bo, page_flags);
  215. if (ttm_tt_alloc_page_directory(ttm)) {
  216. ttm_tt_destroy(ttm);
  217. pr_err("Failed allocating page table\n");
  218. return -ENOMEM;
  219. }
  220. return 0;
  221. }
  222. EXPORT_SYMBOL(ttm_tt_init);
  223. void ttm_tt_fini(struct ttm_tt *ttm)
  224. {
  225. kvfree(ttm->pages);
  226. ttm->pages = NULL;
  227. }
  228. EXPORT_SYMBOL(ttm_tt_fini);
  229. int ttm_dma_tt_init(struct ttm_dma_tt *ttm_dma, struct ttm_buffer_object *bo,
  230. uint32_t page_flags)
  231. {
  232. struct ttm_tt *ttm = &ttm_dma->ttm;
  233. ttm_tt_init_fields(ttm, bo, page_flags);
  234. INIT_LIST_HEAD(&ttm_dma->pages_list);
  235. if (ttm_dma_tt_alloc_page_directory(ttm_dma)) {
  236. ttm_tt_destroy(ttm);
  237. pr_err("Failed allocating page table\n");
  238. return -ENOMEM;
  239. }
  240. return 0;
  241. }
  242. EXPORT_SYMBOL(ttm_dma_tt_init);
  243. int ttm_sg_tt_init(struct ttm_dma_tt *ttm_dma, struct ttm_buffer_object *bo,
  244. uint32_t page_flags)
  245. {
  246. struct ttm_tt *ttm = &ttm_dma->ttm;
  247. int ret;
  248. ttm_tt_init_fields(ttm, bo, page_flags);
  249. INIT_LIST_HEAD(&ttm_dma->pages_list);
  250. if (page_flags & TTM_PAGE_FLAG_SG)
  251. ret = ttm_sg_tt_alloc_page_directory(ttm_dma);
  252. else
  253. ret = ttm_dma_tt_alloc_page_directory(ttm_dma);
  254. if (ret) {
  255. ttm_tt_destroy(ttm);
  256. pr_err("Failed allocating page table\n");
  257. return -ENOMEM;
  258. }
  259. return 0;
  260. }
  261. EXPORT_SYMBOL(ttm_sg_tt_init);
  262. void ttm_dma_tt_fini(struct ttm_dma_tt *ttm_dma)
  263. {
  264. struct ttm_tt *ttm = &ttm_dma->ttm;
  265. if (ttm->pages)
  266. kvfree(ttm->pages);
  267. else
  268. kvfree(ttm_dma->dma_address);
  269. ttm->pages = NULL;
  270. ttm_dma->dma_address = NULL;
  271. }
  272. EXPORT_SYMBOL(ttm_dma_tt_fini);
  273. void ttm_tt_unbind(struct ttm_tt *ttm)
  274. {
  275. int ret;
  276. if (ttm->state == tt_bound) {
  277. ret = ttm->func->unbind(ttm);
  278. BUG_ON(ret);
  279. ttm->state = tt_unbound;
  280. }
  281. }
  282. int ttm_tt_bind(struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem,
  283. struct ttm_operation_ctx *ctx)
  284. {
  285. int ret = 0;
  286. if (!ttm)
  287. return -EINVAL;
  288. if (ttm->state == tt_bound)
  289. return 0;
  290. ret = ttm_tt_populate(ttm, ctx);
  291. if (ret)
  292. return ret;
  293. ret = ttm->func->bind(ttm, bo_mem);
  294. if (unlikely(ret != 0))
  295. return ret;
  296. ttm->state = tt_bound;
  297. return 0;
  298. }
  299. EXPORT_SYMBOL(ttm_tt_bind);
  300. int ttm_tt_swapin(struct ttm_tt *ttm)
  301. {
  302. struct address_space *swap_space;
  303. struct file *swap_storage;
  304. struct page *from_page;
  305. struct page *to_page;
  306. int i;
  307. int ret = -ENOMEM;
  308. swap_storage = ttm->swap_storage;
  309. BUG_ON(swap_storage == NULL);
  310. swap_space = swap_storage->f_mapping;
  311. for (i = 0; i < ttm->num_pages; ++i) {
  312. gfp_t gfp_mask = mapping_gfp_mask(swap_space);
  313. gfp_mask |= (ttm->page_flags & TTM_PAGE_FLAG_NO_RETRY ? __GFP_RETRY_MAYFAIL : 0);
  314. from_page = shmem_read_mapping_page_gfp(swap_space, i, gfp_mask);
  315. if (IS_ERR(from_page)) {
  316. ret = PTR_ERR(from_page);
  317. goto out_err;
  318. }
  319. to_page = ttm->pages[i];
  320. if (unlikely(to_page == NULL))
  321. goto out_err;
  322. copy_highpage(to_page, from_page);
  323. put_page(from_page);
  324. }
  325. if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTENT_SWAP))
  326. fput(swap_storage);
  327. ttm->swap_storage = NULL;
  328. ttm->page_flags &= ~TTM_PAGE_FLAG_SWAPPED;
  329. return 0;
  330. out_err:
  331. return ret;
  332. }
  333. int ttm_tt_swapout(struct ttm_tt *ttm, struct file *persistent_swap_storage)
  334. {
  335. struct address_space *swap_space;
  336. struct file *swap_storage;
  337. struct page *from_page;
  338. struct page *to_page;
  339. int i;
  340. int ret = -ENOMEM;
  341. BUG_ON(ttm->state != tt_unbound && ttm->state != tt_unpopulated);
  342. BUG_ON(ttm->caching_state != tt_cached);
  343. if (!persistent_swap_storage) {
  344. swap_storage = shmem_file_setup("ttm swap",
  345. ttm->num_pages << PAGE_SHIFT,
  346. 0);
  347. if (IS_ERR(swap_storage)) {
  348. pr_err("Failed allocating swap storage\n");
  349. return PTR_ERR(swap_storage);
  350. }
  351. } else {
  352. swap_storage = persistent_swap_storage;
  353. }
  354. swap_space = swap_storage->f_mapping;
  355. for (i = 0; i < ttm->num_pages; ++i) {
  356. gfp_t gfp_mask = mapping_gfp_mask(swap_space);
  357. gfp_mask |= (ttm->page_flags & TTM_PAGE_FLAG_NO_RETRY ? __GFP_RETRY_MAYFAIL : 0);
  358. from_page = ttm->pages[i];
  359. if (unlikely(from_page == NULL))
  360. continue;
  361. to_page = shmem_read_mapping_page_gfp(swap_space, i, gfp_mask);
  362. if (IS_ERR(to_page)) {
  363. ret = PTR_ERR(to_page);
  364. goto out_err;
  365. }
  366. copy_highpage(to_page, from_page);
  367. set_page_dirty(to_page);
  368. mark_page_accessed(to_page);
  369. put_page(to_page);
  370. }
  371. ttm_tt_unpopulate(ttm);
  372. ttm->swap_storage = swap_storage;
  373. ttm->page_flags |= TTM_PAGE_FLAG_SWAPPED;
  374. if (persistent_swap_storage)
  375. ttm->page_flags |= TTM_PAGE_FLAG_PERSISTENT_SWAP;
  376. return 0;
  377. out_err:
  378. if (!persistent_swap_storage)
  379. fput(swap_storage);
  380. return ret;
  381. }
  382. static void ttm_tt_add_mapping(struct ttm_tt *ttm)
  383. {
  384. pgoff_t i;
  385. if (ttm->page_flags & TTM_PAGE_FLAG_SG)
  386. return;
  387. for (i = 0; i < ttm->num_pages; ++i)
  388. ttm->pages[i]->mapping = ttm->bdev->dev_mapping;
  389. }
  390. int ttm_tt_populate(struct ttm_tt *ttm, struct ttm_operation_ctx *ctx)
  391. {
  392. int ret;
  393. if (ttm->state != tt_unpopulated)
  394. return 0;
  395. if (ttm->bdev->driver->ttm_tt_populate)
  396. ret = ttm->bdev->driver->ttm_tt_populate(ttm, ctx);
  397. else
  398. ret = ttm_pool_populate(ttm, ctx);
  399. if (!ret)
  400. ttm_tt_add_mapping(ttm);
  401. return ret;
  402. }
  403. static void ttm_tt_clear_mapping(struct ttm_tt *ttm)
  404. {
  405. pgoff_t i;
  406. struct page **page = ttm->pages;
  407. if (ttm->page_flags & TTM_PAGE_FLAG_SG)
  408. return;
  409. for (i = 0; i < ttm->num_pages; ++i) {
  410. (*page)->mapping = NULL;
  411. (*page++)->index = 0;
  412. }
  413. }
  414. void ttm_tt_unpopulate(struct ttm_tt *ttm)
  415. {
  416. if (ttm->state == tt_unpopulated)
  417. return;
  418. ttm_tt_clear_mapping(ttm);
  419. if (ttm->bdev->driver->ttm_tt_unpopulate)
  420. ttm->bdev->driver->ttm_tt_unpopulate(ttm);
  421. else
  422. ttm_pool_unpopulate(ttm);
  423. }