ttm_pool.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  1. // SPDX-License-Identifier: GPL-2.0 OR MIT
  2. /*
  3. * Copyright 2020 Advanced Micro Devices, Inc.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  19. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  20. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. * OTHER DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors: Christian König
  24. */
  25. /* Pooling of allocated pages is necessary because changing the caching
  26. * attributes on x86 of the linear mapping requires a costly cross CPU TLB
  27. * invalidate for those addresses.
  28. *
  29. * Additional to that allocations from the DMA coherent API are pooled as well
  30. * cause they are rather slow compared to alloc_pages+map.
  31. */
  32. #include <linux/module.h>
  33. #include <linux/dma-mapping.h>
  34. #include <linux/highmem.h>
  35. #include <linux/sched/mm.h>
  36. #ifdef CONFIG_X86
  37. #include <asm/set_memory.h>
  38. #endif
  39. #include <drm/ttm/ttm_pool.h>
  40. #include <drm/ttm/ttm_bo_driver.h>
  41. #include <drm/ttm/ttm_tt.h>
  42. #include "ttm_module.h"
  43. /**
  44. * struct ttm_pool_dma - Helper object for coherent DMA mappings
  45. *
  46. * @addr: original DMA address returned for the mapping
  47. * @vaddr: original vaddr return for the mapping and order in the lower bits
  48. */
  49. struct ttm_pool_dma {
  50. dma_addr_t addr;
  51. unsigned long vaddr;
  52. };
  53. static unsigned long page_pool_size;
  54. MODULE_PARM_DESC(page_pool_size, "Number of pages in the WC/UC/DMA pool");
  55. module_param(page_pool_size, ulong, 0644);
  56. static atomic_long_t allocated_pages;
  57. static struct ttm_pool_type global_write_combined[MAX_ORDER];
  58. static struct ttm_pool_type global_uncached[MAX_ORDER];
  59. static struct ttm_pool_type global_dma32_write_combined[MAX_ORDER];
  60. static struct ttm_pool_type global_dma32_uncached[MAX_ORDER];
  61. static spinlock_t shrinker_lock;
  62. static struct list_head shrinker_list;
  63. static struct shrinker mm_shrinker;
  64. /* Allocate pages of size 1 << order with the given gfp_flags */
  65. static struct page *ttm_pool_alloc_page(struct ttm_pool *pool, gfp_t gfp_flags,
  66. unsigned int order)
  67. {
  68. unsigned long attr = DMA_ATTR_FORCE_CONTIGUOUS;
  69. struct ttm_pool_dma *dma;
  70. struct page *p;
  71. void *vaddr;
  72. /* Don't set the __GFP_COMP flag for higher order allocations.
  73. * Mapping pages directly into an userspace process and calling
  74. * put_page() on a TTM allocated page is illegal.
  75. */
  76. if (order)
  77. gfp_flags |= __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN |
  78. __GFP_KSWAPD_RECLAIM;
  79. if (!pool->use_dma_alloc) {
  80. p = alloc_pages(gfp_flags, order);
  81. if (p)
  82. p->private = order;
  83. return p;
  84. }
  85. dma = kmalloc(sizeof(*dma), GFP_KERNEL);
  86. if (!dma)
  87. return NULL;
  88. if (order)
  89. attr |= DMA_ATTR_NO_WARN;
  90. vaddr = dma_alloc_attrs(pool->dev, (1ULL << order) * PAGE_SIZE,
  91. &dma->addr, gfp_flags, attr);
  92. if (!vaddr)
  93. goto error_free;
  94. /* TODO: This is an illegal abuse of the DMA API, but we need to rework
  95. * TTM page fault handling and extend the DMA API to clean this up.
  96. */
  97. if (is_vmalloc_addr(vaddr))
  98. p = vmalloc_to_page(vaddr);
  99. else
  100. p = virt_to_page(vaddr);
  101. dma->vaddr = (unsigned long)vaddr | order;
  102. p->private = (unsigned long)dma;
  103. return p;
  104. error_free:
  105. kfree(dma);
  106. return NULL;
  107. }
  108. /* Reset the caching and pages of size 1 << order */
  109. static void ttm_pool_free_page(struct ttm_pool *pool, enum ttm_caching caching,
  110. unsigned int order, struct page *p)
  111. {
  112. unsigned long attr = DMA_ATTR_FORCE_CONTIGUOUS;
  113. struct ttm_pool_dma *dma;
  114. void *vaddr;
  115. #ifdef CONFIG_X86
  116. /* We don't care that set_pages_wb is inefficient here. This is only
  117. * used when we have to shrink and CPU overhead is irrelevant then.
  118. */
  119. if (caching != ttm_cached && !PageHighMem(p))
  120. set_pages_wb(p, 1 << order);
  121. #endif
  122. if (!pool || !pool->use_dma_alloc) {
  123. __free_pages(p, order);
  124. return;
  125. }
  126. if (order)
  127. attr |= DMA_ATTR_NO_WARN;
  128. dma = (void *)p->private;
  129. vaddr = (void *)(dma->vaddr & PAGE_MASK);
  130. dma_free_attrs(pool->dev, (1UL << order) * PAGE_SIZE, vaddr, dma->addr,
  131. attr);
  132. kfree(dma);
  133. }
  134. /* Apply a new caching to an array of pages */
  135. static int ttm_pool_apply_caching(struct page **first, struct page **last,
  136. enum ttm_caching caching)
  137. {
  138. #ifdef CONFIG_X86
  139. unsigned int num_pages = last - first;
  140. if (!num_pages)
  141. return 0;
  142. switch (caching) {
  143. case ttm_cached:
  144. break;
  145. case ttm_write_combined:
  146. return set_pages_array_wc(first, num_pages);
  147. case ttm_uncached:
  148. return set_pages_array_uc(first, num_pages);
  149. }
  150. #endif
  151. return 0;
  152. }
  153. /* Map pages of 1 << order size and fill the DMA address array */
  154. static int ttm_pool_map(struct ttm_pool *pool, unsigned int order,
  155. struct page *p, dma_addr_t **dma_addr)
  156. {
  157. dma_addr_t addr;
  158. unsigned int i;
  159. if (pool->use_dma_alloc) {
  160. struct ttm_pool_dma *dma = (void *)p->private;
  161. addr = dma->addr;
  162. } else {
  163. size_t size = (1ULL << order) * PAGE_SIZE;
  164. addr = dma_map_page(pool->dev, p, 0, size, DMA_BIDIRECTIONAL);
  165. if (dma_mapping_error(pool->dev, addr))
  166. return -EFAULT;
  167. }
  168. for (i = 1 << order; i ; --i) {
  169. *(*dma_addr)++ = addr;
  170. addr += PAGE_SIZE;
  171. }
  172. return 0;
  173. }
  174. /* Unmap pages of 1 << order size */
  175. static void ttm_pool_unmap(struct ttm_pool *pool, dma_addr_t dma_addr,
  176. unsigned int num_pages)
  177. {
  178. /* Unmapped while freeing the page */
  179. if (pool->use_dma_alloc)
  180. return;
  181. dma_unmap_page(pool->dev, dma_addr, (long)num_pages << PAGE_SHIFT,
  182. DMA_BIDIRECTIONAL);
  183. }
  184. /* Give pages into a specific pool_type */
  185. static void ttm_pool_type_give(struct ttm_pool_type *pt, struct page *p)
  186. {
  187. unsigned int i, num_pages = 1 << pt->order;
  188. for (i = 0; i < num_pages; ++i) {
  189. if (PageHighMem(p))
  190. clear_highpage(p + i);
  191. else
  192. clear_page(page_address(p + i));
  193. }
  194. spin_lock(&pt->lock);
  195. list_add(&p->lru, &pt->pages);
  196. spin_unlock(&pt->lock);
  197. atomic_long_add(1 << pt->order, &allocated_pages);
  198. }
  199. /* Take pages from a specific pool_type, return NULL when nothing available */
  200. static struct page *ttm_pool_type_take(struct ttm_pool_type *pt)
  201. {
  202. struct page *p;
  203. spin_lock(&pt->lock);
  204. p = list_first_entry_or_null(&pt->pages, typeof(*p), lru);
  205. if (p) {
  206. atomic_long_sub(1 << pt->order, &allocated_pages);
  207. list_del(&p->lru);
  208. }
  209. spin_unlock(&pt->lock);
  210. return p;
  211. }
  212. /* Initialize and add a pool type to the global shrinker list */
  213. static void ttm_pool_type_init(struct ttm_pool_type *pt, struct ttm_pool *pool,
  214. enum ttm_caching caching, unsigned int order)
  215. {
  216. pt->pool = pool;
  217. pt->caching = caching;
  218. pt->order = order;
  219. spin_lock_init(&pt->lock);
  220. INIT_LIST_HEAD(&pt->pages);
  221. spin_lock(&shrinker_lock);
  222. list_add_tail(&pt->shrinker_list, &shrinker_list);
  223. spin_unlock(&shrinker_lock);
  224. }
  225. /* Remove a pool_type from the global shrinker list and free all pages */
  226. static void ttm_pool_type_fini(struct ttm_pool_type *pt)
  227. {
  228. struct page *p;
  229. spin_lock(&shrinker_lock);
  230. list_del(&pt->shrinker_list);
  231. spin_unlock(&shrinker_lock);
  232. while ((p = ttm_pool_type_take(pt)))
  233. ttm_pool_free_page(pt->pool, pt->caching, pt->order, p);
  234. }
  235. /* Return the pool_type to use for the given caching and order */
  236. static struct ttm_pool_type *ttm_pool_select_type(struct ttm_pool *pool,
  237. enum ttm_caching caching,
  238. unsigned int order)
  239. {
  240. if (pool->use_dma_alloc)
  241. return &pool->caching[caching].orders[order];
  242. #ifdef CONFIG_X86
  243. switch (caching) {
  244. case ttm_write_combined:
  245. if (pool->use_dma32)
  246. return &global_dma32_write_combined[order];
  247. return &global_write_combined[order];
  248. case ttm_uncached:
  249. if (pool->use_dma32)
  250. return &global_dma32_uncached[order];
  251. return &global_uncached[order];
  252. default:
  253. break;
  254. }
  255. #endif
  256. return NULL;
  257. }
  258. /* Free pages using the global shrinker list */
  259. static unsigned int ttm_pool_shrink(void)
  260. {
  261. struct ttm_pool_type *pt;
  262. unsigned int num_pages;
  263. struct page *p;
  264. spin_lock(&shrinker_lock);
  265. pt = list_first_entry(&shrinker_list, typeof(*pt), shrinker_list);
  266. list_move_tail(&pt->shrinker_list, &shrinker_list);
  267. spin_unlock(&shrinker_lock);
  268. p = ttm_pool_type_take(pt);
  269. if (p) {
  270. ttm_pool_free_page(pt->pool, pt->caching, pt->order, p);
  271. num_pages = 1 << pt->order;
  272. } else {
  273. num_pages = 0;
  274. }
  275. return num_pages;
  276. }
  277. /* Return the allocation order based for a page */
  278. static unsigned int ttm_pool_page_order(struct ttm_pool *pool, struct page *p)
  279. {
  280. if (pool->use_dma_alloc) {
  281. struct ttm_pool_dma *dma = (void *)p->private;
  282. return dma->vaddr & ~PAGE_MASK;
  283. }
  284. return p->private;
  285. }
  286. /* Called when we got a page, either from a pool or newly allocated */
  287. static int ttm_pool_page_allocated(struct ttm_pool *pool, unsigned int order,
  288. struct page *p, dma_addr_t **dma_addr,
  289. unsigned long *num_pages,
  290. struct page ***pages)
  291. {
  292. unsigned int i;
  293. int r;
  294. if (*dma_addr) {
  295. r = ttm_pool_map(pool, order, p, dma_addr);
  296. if (r)
  297. return r;
  298. }
  299. *num_pages -= 1 << order;
  300. for (i = 1 << order; i; --i, ++(*pages), ++p)
  301. **pages = p;
  302. return 0;
  303. }
  304. /**
  305. * ttm_pool_free_range() - Free a range of TTM pages
  306. * @pool: The pool used for allocating.
  307. * @tt: The struct ttm_tt holding the page pointers.
  308. * @caching: The page caching mode used by the range.
  309. * @start_page: index for first page to free.
  310. * @end_page: index for last page to free + 1.
  311. *
  312. * During allocation the ttm_tt page-vector may be populated with ranges of
  313. * pages with different attributes if allocation hit an error without being
  314. * able to completely fulfill the allocation. This function can be used
  315. * to free these individual ranges.
  316. */
  317. static void ttm_pool_free_range(struct ttm_pool *pool, struct ttm_tt *tt,
  318. enum ttm_caching caching,
  319. pgoff_t start_page, pgoff_t end_page)
  320. {
  321. struct page **pages = tt->pages;
  322. unsigned int order;
  323. pgoff_t i, nr;
  324. for (i = start_page; i < end_page; i += nr, pages += nr) {
  325. struct ttm_pool_type *pt = NULL;
  326. order = ttm_pool_page_order(pool, *pages);
  327. nr = (1UL << order);
  328. if (tt->dma_address)
  329. ttm_pool_unmap(pool, tt->dma_address[i], nr);
  330. pt = ttm_pool_select_type(pool, caching, order);
  331. if (pt)
  332. ttm_pool_type_give(pt, *pages);
  333. else
  334. ttm_pool_free_page(pool, caching, order, *pages);
  335. }
  336. }
  337. /**
  338. * ttm_pool_alloc - Fill a ttm_tt object
  339. *
  340. * @pool: ttm_pool to use
  341. * @tt: ttm_tt object to fill
  342. * @ctx: operation context
  343. *
  344. * Fill the ttm_tt object with pages and also make sure to DMA map them when
  345. * necessary.
  346. *
  347. * Returns: 0 on successe, negative error code otherwise.
  348. */
  349. int ttm_pool_alloc(struct ttm_pool *pool, struct ttm_tt *tt,
  350. struct ttm_operation_ctx *ctx)
  351. {
  352. pgoff_t num_pages = tt->num_pages;
  353. dma_addr_t *dma_addr = tt->dma_address;
  354. struct page **caching = tt->pages;
  355. struct page **pages = tt->pages;
  356. enum ttm_caching page_caching;
  357. gfp_t gfp_flags = GFP_USER;
  358. pgoff_t caching_divide;
  359. unsigned int order;
  360. struct page *p;
  361. int r;
  362. WARN_ON(!num_pages || ttm_tt_is_populated(tt));
  363. WARN_ON(dma_addr && !pool->dev);
  364. if (tt->page_flags & TTM_TT_FLAG_ZERO_ALLOC)
  365. gfp_flags |= __GFP_ZERO;
  366. if (ctx->gfp_retry_mayfail)
  367. gfp_flags |= __GFP_RETRY_MAYFAIL;
  368. if (pool->use_dma32)
  369. gfp_flags |= GFP_DMA32;
  370. else
  371. gfp_flags |= GFP_HIGHUSER;
  372. for (order = min_t(unsigned int, MAX_ORDER - 1, __fls(num_pages));
  373. num_pages;
  374. order = min_t(unsigned int, order, __fls(num_pages))) {
  375. struct ttm_pool_type *pt;
  376. page_caching = tt->caching;
  377. pt = ttm_pool_select_type(pool, tt->caching, order);
  378. p = pt ? ttm_pool_type_take(pt) : NULL;
  379. if (p) {
  380. r = ttm_pool_apply_caching(caching, pages,
  381. tt->caching);
  382. if (r)
  383. goto error_free_page;
  384. caching = pages;
  385. do {
  386. r = ttm_pool_page_allocated(pool, order, p,
  387. &dma_addr,
  388. &num_pages,
  389. &pages);
  390. if (r)
  391. goto error_free_page;
  392. caching = pages;
  393. if (num_pages < (1 << order))
  394. break;
  395. p = ttm_pool_type_take(pt);
  396. } while (p);
  397. }
  398. page_caching = ttm_cached;
  399. while (num_pages >= (1 << order) &&
  400. (p = ttm_pool_alloc_page(pool, gfp_flags, order))) {
  401. if (PageHighMem(p)) {
  402. r = ttm_pool_apply_caching(caching, pages,
  403. tt->caching);
  404. if (r)
  405. goto error_free_page;
  406. caching = pages;
  407. }
  408. r = ttm_pool_page_allocated(pool, order, p, &dma_addr,
  409. &num_pages, &pages);
  410. if (r)
  411. goto error_free_page;
  412. if (PageHighMem(p))
  413. caching = pages;
  414. }
  415. if (!p) {
  416. if (order) {
  417. --order;
  418. continue;
  419. }
  420. r = -ENOMEM;
  421. goto error_free_all;
  422. }
  423. }
  424. r = ttm_pool_apply_caching(caching, pages, tt->caching);
  425. if (r)
  426. goto error_free_all;
  427. return 0;
  428. error_free_page:
  429. ttm_pool_free_page(pool, page_caching, order, p);
  430. error_free_all:
  431. num_pages = tt->num_pages - num_pages;
  432. caching_divide = caching - tt->pages;
  433. ttm_pool_free_range(pool, tt, tt->caching, 0, caching_divide);
  434. ttm_pool_free_range(pool, tt, ttm_cached, caching_divide, num_pages);
  435. return r;
  436. }
  437. EXPORT_SYMBOL(ttm_pool_alloc);
  438. /**
  439. * ttm_pool_free - Free the backing pages from a ttm_tt object
  440. *
  441. * @pool: Pool to give pages back to.
  442. * @tt: ttm_tt object to unpopulate
  443. *
  444. * Give the packing pages back to a pool or free them
  445. */
  446. void ttm_pool_free(struct ttm_pool *pool, struct ttm_tt *tt)
  447. {
  448. ttm_pool_free_range(pool, tt, tt->caching, 0, tt->num_pages);
  449. while (atomic_long_read(&allocated_pages) > page_pool_size)
  450. ttm_pool_shrink();
  451. }
  452. EXPORT_SYMBOL(ttm_pool_free);
  453. /**
  454. * ttm_pool_init - Initialize a pool
  455. *
  456. * @pool: the pool to initialize
  457. * @dev: device for DMA allocations and mappings
  458. * @use_dma_alloc: true if coherent DMA alloc should be used
  459. * @use_dma32: true if GFP_DMA32 should be used
  460. *
  461. * Initialize the pool and its pool types.
  462. */
  463. void ttm_pool_init(struct ttm_pool *pool, struct device *dev,
  464. bool use_dma_alloc, bool use_dma32)
  465. {
  466. unsigned int i, j;
  467. WARN_ON(!dev && use_dma_alloc);
  468. pool->dev = dev;
  469. pool->use_dma_alloc = use_dma_alloc;
  470. pool->use_dma32 = use_dma32;
  471. if (use_dma_alloc) {
  472. for (i = 0; i < TTM_NUM_CACHING_TYPES; ++i)
  473. for (j = 0; j < MAX_ORDER; ++j)
  474. ttm_pool_type_init(&pool->caching[i].orders[j],
  475. pool, i, j);
  476. }
  477. }
  478. /**
  479. * ttm_pool_fini - Cleanup a pool
  480. *
  481. * @pool: the pool to clean up
  482. *
  483. * Free all pages in the pool and unregister the types from the global
  484. * shrinker.
  485. */
  486. void ttm_pool_fini(struct ttm_pool *pool)
  487. {
  488. unsigned int i, j;
  489. if (pool->use_dma_alloc) {
  490. for (i = 0; i < TTM_NUM_CACHING_TYPES; ++i)
  491. for (j = 0; j < MAX_ORDER; ++j)
  492. ttm_pool_type_fini(&pool->caching[i].orders[j]);
  493. }
  494. /* We removed the pool types from the LRU, but we need to also make sure
  495. * that no shrinker is concurrently freeing pages from the pool.
  496. */
  497. synchronize_shrinkers();
  498. }
  499. /* As long as pages are available make sure to release at least one */
  500. static unsigned long ttm_pool_shrinker_scan(struct shrinker *shrink,
  501. struct shrink_control *sc)
  502. {
  503. unsigned long num_freed = 0;
  504. do
  505. num_freed += ttm_pool_shrink();
  506. while (!num_freed && atomic_long_read(&allocated_pages));
  507. return num_freed;
  508. }
  509. /* Return the number of pages available or SHRINK_EMPTY if we have none */
  510. static unsigned long ttm_pool_shrinker_count(struct shrinker *shrink,
  511. struct shrink_control *sc)
  512. {
  513. unsigned long num_pages = atomic_long_read(&allocated_pages);
  514. return num_pages ? num_pages : SHRINK_EMPTY;
  515. }
  516. #ifdef CONFIG_DEBUG_FS
  517. /* Count the number of pages available in a pool_type */
  518. static unsigned int ttm_pool_type_count(struct ttm_pool_type *pt)
  519. {
  520. unsigned int count = 0;
  521. struct page *p;
  522. spin_lock(&pt->lock);
  523. /* Only used for debugfs, the overhead doesn't matter */
  524. list_for_each_entry(p, &pt->pages, lru)
  525. ++count;
  526. spin_unlock(&pt->lock);
  527. return count;
  528. }
  529. /* Print a nice header for the order */
  530. static void ttm_pool_debugfs_header(struct seq_file *m)
  531. {
  532. unsigned int i;
  533. seq_puts(m, "\t ");
  534. for (i = 0; i < MAX_ORDER; ++i)
  535. seq_printf(m, " ---%2u---", i);
  536. seq_puts(m, "\n");
  537. }
  538. /* Dump information about the different pool types */
  539. static void ttm_pool_debugfs_orders(struct ttm_pool_type *pt,
  540. struct seq_file *m)
  541. {
  542. unsigned int i;
  543. for (i = 0; i < MAX_ORDER; ++i)
  544. seq_printf(m, " %8u", ttm_pool_type_count(&pt[i]));
  545. seq_puts(m, "\n");
  546. }
  547. /* Dump the total amount of allocated pages */
  548. static void ttm_pool_debugfs_footer(struct seq_file *m)
  549. {
  550. seq_printf(m, "\ntotal\t: %8lu of %8lu\n",
  551. atomic_long_read(&allocated_pages), page_pool_size);
  552. }
  553. /* Dump the information for the global pools */
  554. static int ttm_pool_debugfs_globals_show(struct seq_file *m, void *data)
  555. {
  556. ttm_pool_debugfs_header(m);
  557. spin_lock(&shrinker_lock);
  558. seq_puts(m, "wc\t:");
  559. ttm_pool_debugfs_orders(global_write_combined, m);
  560. seq_puts(m, "uc\t:");
  561. ttm_pool_debugfs_orders(global_uncached, m);
  562. seq_puts(m, "wc 32\t:");
  563. ttm_pool_debugfs_orders(global_dma32_write_combined, m);
  564. seq_puts(m, "uc 32\t:");
  565. ttm_pool_debugfs_orders(global_dma32_uncached, m);
  566. spin_unlock(&shrinker_lock);
  567. ttm_pool_debugfs_footer(m);
  568. return 0;
  569. }
  570. DEFINE_SHOW_ATTRIBUTE(ttm_pool_debugfs_globals);
  571. /**
  572. * ttm_pool_debugfs - Debugfs dump function for a pool
  573. *
  574. * @pool: the pool to dump the information for
  575. * @m: seq_file to dump to
  576. *
  577. * Make a debugfs dump with the per pool and global information.
  578. */
  579. int ttm_pool_debugfs(struct ttm_pool *pool, struct seq_file *m)
  580. {
  581. unsigned int i;
  582. if (!pool->use_dma_alloc) {
  583. seq_puts(m, "unused\n");
  584. return 0;
  585. }
  586. ttm_pool_debugfs_header(m);
  587. spin_lock(&shrinker_lock);
  588. for (i = 0; i < TTM_NUM_CACHING_TYPES; ++i) {
  589. seq_puts(m, "DMA ");
  590. switch (i) {
  591. case ttm_cached:
  592. seq_puts(m, "\t:");
  593. break;
  594. case ttm_write_combined:
  595. seq_puts(m, "wc\t:");
  596. break;
  597. case ttm_uncached:
  598. seq_puts(m, "uc\t:");
  599. break;
  600. }
  601. ttm_pool_debugfs_orders(pool->caching[i].orders, m);
  602. }
  603. spin_unlock(&shrinker_lock);
  604. ttm_pool_debugfs_footer(m);
  605. return 0;
  606. }
  607. EXPORT_SYMBOL(ttm_pool_debugfs);
  608. /* Test the shrinker functions and dump the result */
  609. static int ttm_pool_debugfs_shrink_show(struct seq_file *m, void *data)
  610. {
  611. struct shrink_control sc = { .gfp_mask = GFP_NOFS };
  612. fs_reclaim_acquire(GFP_KERNEL);
  613. seq_printf(m, "%lu/%lu\n", ttm_pool_shrinker_count(&mm_shrinker, &sc),
  614. ttm_pool_shrinker_scan(&mm_shrinker, &sc));
  615. fs_reclaim_release(GFP_KERNEL);
  616. return 0;
  617. }
  618. DEFINE_SHOW_ATTRIBUTE(ttm_pool_debugfs_shrink);
  619. #endif
  620. /**
  621. * ttm_pool_mgr_init - Initialize globals
  622. *
  623. * @num_pages: default number of pages
  624. *
  625. * Initialize the global locks and lists for the MM shrinker.
  626. */
  627. int ttm_pool_mgr_init(unsigned long num_pages)
  628. {
  629. unsigned int i;
  630. if (!page_pool_size)
  631. page_pool_size = num_pages;
  632. spin_lock_init(&shrinker_lock);
  633. INIT_LIST_HEAD(&shrinker_list);
  634. for (i = 0; i < MAX_ORDER; ++i) {
  635. ttm_pool_type_init(&global_write_combined[i], NULL,
  636. ttm_write_combined, i);
  637. ttm_pool_type_init(&global_uncached[i], NULL, ttm_uncached, i);
  638. ttm_pool_type_init(&global_dma32_write_combined[i], NULL,
  639. ttm_write_combined, i);
  640. ttm_pool_type_init(&global_dma32_uncached[i], NULL,
  641. ttm_uncached, i);
  642. }
  643. #ifdef CONFIG_DEBUG_FS
  644. debugfs_create_file("page_pool", 0444, ttm_debugfs_root, NULL,
  645. &ttm_pool_debugfs_globals_fops);
  646. debugfs_create_file("page_pool_shrink", 0400, ttm_debugfs_root, NULL,
  647. &ttm_pool_debugfs_shrink_fops);
  648. #endif
  649. mm_shrinker.count_objects = ttm_pool_shrinker_count;
  650. mm_shrinker.scan_objects = ttm_pool_shrinker_scan;
  651. mm_shrinker.seeks = 1;
  652. return register_shrinker(&mm_shrinker, "drm-ttm_pool");
  653. }
  654. /**
  655. * ttm_pool_mgr_fini - Finalize globals
  656. *
  657. * Cleanup the global pools and unregister the MM shrinker.
  658. */
  659. void ttm_pool_mgr_fini(void)
  660. {
  661. unsigned int i;
  662. for (i = 0; i < MAX_ORDER; ++i) {
  663. ttm_pool_type_fini(&global_write_combined[i]);
  664. ttm_pool_type_fini(&global_uncached[i]);
  665. ttm_pool_type_fini(&global_dma32_write_combined[i]);
  666. ttm_pool_type_fini(&global_dma32_uncached[i]);
  667. }
  668. unregister_shrinker(&mm_shrinker);
  669. WARN_ON(!list_empty(&shrinker_list));
  670. }