ttm_page_alloc_dma.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275
  1. /*
  2. * Copyright 2011 (c) Oracle Corp.
  3. * Permission is hereby granted, free of charge, to any person obtaining a
  4. * copy of this software and associated documentation files (the "Software"),
  5. * to deal in the Software without restriction, including without limitation
  6. * the rights to use, copy, modify, merge, publish, distribute, sub license,
  7. * and/or sell copies of the Software, and to permit persons to whom the
  8. * Software is furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice (including the
  11. * next paragraph) shall be included in all copies or substantial portions
  12. * of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  17. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. *
  22. * Author: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
  23. */
  24. /*
  25. * A simple DMA pool losely based on dmapool.c. It has certain advantages
  26. * over the DMA pools:
  27. * - Pool collects resently freed pages for reuse (and hooks up to
  28. * the shrinker).
  29. * - Tracks currently in use pages
  30. * - Tracks whether the page is UC, WB or cached (and reverts to WB
  31. * when freed).
  32. */
  33. #if defined(CONFIG_SWIOTLB) || defined(CONFIG_INTEL_IOMMU)
  34. #define pr_fmt(fmt) "[TTM] " fmt
  35. #include <linux/dma-mapping.h>
  36. #include <linux/list.h>
  37. #include <linux/seq_file.h> /* for seq_printf */
  38. #include <linux/slab.h>
  39. #include <linux/spinlock.h>
  40. #include <linux/highmem.h>
  41. #include <linux/mm_types.h>
  42. #include <linux/module.h>
  43. #include <linux/mm.h>
  44. #include <linux/atomic.h>
  45. #include <linux/device.h>
  46. #include <linux/kthread.h>
  47. #include <drm/ttm/ttm_bo_driver.h>
  48. #include <drm/ttm/ttm_page_alloc.h>
  49. #if IS_ENABLED(CONFIG_AGP)
  50. #include <asm/agp.h>
  51. #endif
  52. #ifdef CONFIG_X86
  53. #include <asm/set_memory.h>
  54. #endif
  55. #define NUM_PAGES_TO_ALLOC (PAGE_SIZE/sizeof(struct page *))
  56. #define SMALL_ALLOCATION 4
  57. #define FREE_ALL_PAGES (~0U)
  58. #define VADDR_FLAG_HUGE_POOL 1UL
  59. enum pool_type {
  60. IS_UNDEFINED = 0,
  61. IS_WC = 1 << 1,
  62. IS_UC = 1 << 2,
  63. IS_CACHED = 1 << 3,
  64. IS_DMA32 = 1 << 4,
  65. IS_HUGE = 1 << 5
  66. };
  67. /*
  68. * The pool structure. There are up to nine pools:
  69. * - generic (not restricted to DMA32):
  70. * - write combined, uncached, cached.
  71. * - dma32 (up to 2^32 - so up 4GB):
  72. * - write combined, uncached, cached.
  73. * - huge (not restricted to DMA32):
  74. * - write combined, uncached, cached.
  75. * for each 'struct device'. The 'cached' is for pages that are actively used.
  76. * The other ones can be shrunk by the shrinker API if neccessary.
  77. * @pools: The 'struct device->dma_pools' link.
  78. * @type: Type of the pool
  79. * @lock: Protects the free_list from concurrnet access. Must be
  80. * used with irqsave/irqrestore variants because pool allocator maybe called
  81. * from delayed work.
  82. * @free_list: Pool of pages that are free to be used. No order requirements.
  83. * @dev: The device that is associated with these pools.
  84. * @size: Size used during DMA allocation.
  85. * @npages_free: Count of available pages for re-use.
  86. * @npages_in_use: Count of pages that are in use.
  87. * @nfrees: Stats when pool is shrinking.
  88. * @nrefills: Stats when the pool is grown.
  89. * @gfp_flags: Flags to pass for alloc_page.
  90. * @name: Name of the pool.
  91. * @dev_name: Name derieved from dev - similar to how dev_info works.
  92. * Used during shutdown as the dev_info during release is unavailable.
  93. */
  94. struct dma_pool {
  95. struct list_head pools; /* The 'struct device->dma_pools link */
  96. enum pool_type type;
  97. spinlock_t lock;
  98. struct list_head free_list;
  99. struct device *dev;
  100. unsigned size;
  101. unsigned npages_free;
  102. unsigned npages_in_use;
  103. unsigned long nfrees; /* Stats when shrunk. */
  104. unsigned long nrefills; /* Stats when grown. */
  105. gfp_t gfp_flags;
  106. char name[13]; /* "cached dma32" */
  107. char dev_name[64]; /* Constructed from dev */
  108. };
  109. /*
  110. * The accounting page keeping track of the allocated page along with
  111. * the DMA address.
  112. * @page_list: The link to the 'page_list' in 'struct dma_pool'.
  113. * @vaddr: The virtual address of the page and a flag if the page belongs to a
  114. * huge pool
  115. * @dma: The bus address of the page. If the page is not allocated
  116. * via the DMA API, it will be -1.
  117. */
  118. struct dma_page {
  119. struct list_head page_list;
  120. unsigned long vaddr;
  121. struct page *p;
  122. dma_addr_t dma;
  123. };
  124. /*
  125. * Limits for the pool. They are handled without locks because only place where
  126. * they may change is in sysfs store. They won't have immediate effect anyway
  127. * so forcing serialization to access them is pointless.
  128. */
  129. struct ttm_pool_opts {
  130. unsigned alloc_size;
  131. unsigned max_size;
  132. unsigned small;
  133. };
  134. /*
  135. * Contains the list of all of the 'struct device' and their corresponding
  136. * DMA pools. Guarded by _mutex->lock.
  137. * @pools: The link to 'struct ttm_pool_manager->pools'
  138. * @dev: The 'struct device' associated with the 'pool'
  139. * @pool: The 'struct dma_pool' associated with the 'dev'
  140. */
  141. struct device_pools {
  142. struct list_head pools;
  143. struct device *dev;
  144. struct dma_pool *pool;
  145. };
  146. /*
  147. * struct ttm_pool_manager - Holds memory pools for fast allocation
  148. *
  149. * @lock: Lock used when adding/removing from pools
  150. * @pools: List of 'struct device' and 'struct dma_pool' tuples.
  151. * @options: Limits for the pool.
  152. * @npools: Total amount of pools in existence.
  153. * @shrinker: The structure used by [un|]register_shrinker
  154. */
  155. struct ttm_pool_manager {
  156. struct mutex lock;
  157. struct list_head pools;
  158. struct ttm_pool_opts options;
  159. unsigned npools;
  160. struct shrinker mm_shrink;
  161. struct kobject kobj;
  162. };
  163. static struct ttm_pool_manager *_manager;
  164. static struct attribute ttm_page_pool_max = {
  165. .name = "pool_max_size",
  166. .mode = S_IRUGO | S_IWUSR
  167. };
  168. static struct attribute ttm_page_pool_small = {
  169. .name = "pool_small_allocation",
  170. .mode = S_IRUGO | S_IWUSR
  171. };
  172. static struct attribute ttm_page_pool_alloc_size = {
  173. .name = "pool_allocation_size",
  174. .mode = S_IRUGO | S_IWUSR
  175. };
  176. static struct attribute *ttm_pool_attrs[] = {
  177. &ttm_page_pool_max,
  178. &ttm_page_pool_small,
  179. &ttm_page_pool_alloc_size,
  180. NULL
  181. };
  182. static void ttm_pool_kobj_release(struct kobject *kobj)
  183. {
  184. struct ttm_pool_manager *m =
  185. container_of(kobj, struct ttm_pool_manager, kobj);
  186. kfree(m);
  187. }
  188. static ssize_t ttm_pool_store(struct kobject *kobj, struct attribute *attr,
  189. const char *buffer, size_t size)
  190. {
  191. struct ttm_pool_manager *m =
  192. container_of(kobj, struct ttm_pool_manager, kobj);
  193. int chars;
  194. unsigned val;
  195. chars = sscanf(buffer, "%u", &val);
  196. if (chars == 0)
  197. return size;
  198. /* Convert kb to number of pages */
  199. val = val / (PAGE_SIZE >> 10);
  200. if (attr == &ttm_page_pool_max)
  201. m->options.max_size = val;
  202. else if (attr == &ttm_page_pool_small)
  203. m->options.small = val;
  204. else if (attr == &ttm_page_pool_alloc_size) {
  205. if (val > NUM_PAGES_TO_ALLOC*8) {
  206. pr_err("Setting allocation size to %lu is not allowed. Recommended size is %lu\n",
  207. NUM_PAGES_TO_ALLOC*(PAGE_SIZE >> 7),
  208. NUM_PAGES_TO_ALLOC*(PAGE_SIZE >> 10));
  209. return size;
  210. } else if (val > NUM_PAGES_TO_ALLOC) {
  211. pr_warn("Setting allocation size to larger than %lu is not recommended\n",
  212. NUM_PAGES_TO_ALLOC*(PAGE_SIZE >> 10));
  213. }
  214. m->options.alloc_size = val;
  215. }
  216. return size;
  217. }
  218. static ssize_t ttm_pool_show(struct kobject *kobj, struct attribute *attr,
  219. char *buffer)
  220. {
  221. struct ttm_pool_manager *m =
  222. container_of(kobj, struct ttm_pool_manager, kobj);
  223. unsigned val = 0;
  224. if (attr == &ttm_page_pool_max)
  225. val = m->options.max_size;
  226. else if (attr == &ttm_page_pool_small)
  227. val = m->options.small;
  228. else if (attr == &ttm_page_pool_alloc_size)
  229. val = m->options.alloc_size;
  230. val = val * (PAGE_SIZE >> 10);
  231. return snprintf(buffer, PAGE_SIZE, "%u\n", val);
  232. }
  233. static const struct sysfs_ops ttm_pool_sysfs_ops = {
  234. .show = &ttm_pool_show,
  235. .store = &ttm_pool_store,
  236. };
  237. static struct kobj_type ttm_pool_kobj_type = {
  238. .release = &ttm_pool_kobj_release,
  239. .sysfs_ops = &ttm_pool_sysfs_ops,
  240. .default_attrs = ttm_pool_attrs,
  241. };
  242. #ifndef CONFIG_X86
  243. static int set_pages_array_wb(struct page **pages, int addrinarray)
  244. {
  245. #if IS_ENABLED(CONFIG_AGP)
  246. int i;
  247. for (i = 0; i < addrinarray; i++)
  248. unmap_page_from_agp(pages[i]);
  249. #endif
  250. return 0;
  251. }
  252. static int set_pages_array_wc(struct page **pages, int addrinarray)
  253. {
  254. #if IS_ENABLED(CONFIG_AGP)
  255. int i;
  256. for (i = 0; i < addrinarray; i++)
  257. map_page_into_agp(pages[i]);
  258. #endif
  259. return 0;
  260. }
  261. static int set_pages_array_uc(struct page **pages, int addrinarray)
  262. {
  263. #if IS_ENABLED(CONFIG_AGP)
  264. int i;
  265. for (i = 0; i < addrinarray; i++)
  266. map_page_into_agp(pages[i]);
  267. #endif
  268. return 0;
  269. }
  270. #endif /* for !CONFIG_X86 */
  271. static int ttm_set_pages_caching(struct dma_pool *pool,
  272. struct page **pages, unsigned cpages)
  273. {
  274. int r = 0;
  275. /* Set page caching */
  276. if (pool->type & IS_UC) {
  277. r = set_pages_array_uc(pages, cpages);
  278. if (r)
  279. pr_err("%s: Failed to set %d pages to uc!\n",
  280. pool->dev_name, cpages);
  281. }
  282. if (pool->type & IS_WC) {
  283. r = set_pages_array_wc(pages, cpages);
  284. if (r)
  285. pr_err("%s: Failed to set %d pages to wc!\n",
  286. pool->dev_name, cpages);
  287. }
  288. return r;
  289. }
  290. static void __ttm_dma_free_page(struct dma_pool *pool, struct dma_page *d_page)
  291. {
  292. dma_addr_t dma = d_page->dma;
  293. d_page->vaddr &= ~VADDR_FLAG_HUGE_POOL;
  294. dma_free_coherent(pool->dev, pool->size, (void *)d_page->vaddr, dma);
  295. kfree(d_page);
  296. d_page = NULL;
  297. }
  298. static struct dma_page *__ttm_dma_alloc_page(struct dma_pool *pool)
  299. {
  300. struct dma_page *d_page;
  301. unsigned long attrs = 0;
  302. void *vaddr;
  303. d_page = kmalloc(sizeof(struct dma_page), GFP_KERNEL);
  304. if (!d_page)
  305. return NULL;
  306. if (pool->type & IS_HUGE)
  307. attrs = DMA_ATTR_NO_WARN;
  308. vaddr = dma_alloc_attrs(pool->dev, pool->size, &d_page->dma,
  309. pool->gfp_flags, attrs);
  310. if (vaddr) {
  311. if (is_vmalloc_addr(vaddr))
  312. d_page->p = vmalloc_to_page(vaddr);
  313. else
  314. d_page->p = virt_to_page(vaddr);
  315. d_page->vaddr = (unsigned long)vaddr;
  316. if (pool->type & IS_HUGE)
  317. d_page->vaddr |= VADDR_FLAG_HUGE_POOL;
  318. } else {
  319. kfree(d_page);
  320. d_page = NULL;
  321. }
  322. return d_page;
  323. }
  324. static enum pool_type ttm_to_type(int flags, enum ttm_caching_state cstate)
  325. {
  326. enum pool_type type = IS_UNDEFINED;
  327. if (flags & TTM_PAGE_FLAG_DMA32)
  328. type |= IS_DMA32;
  329. if (cstate == tt_cached)
  330. type |= IS_CACHED;
  331. else if (cstate == tt_uncached)
  332. type |= IS_UC;
  333. else
  334. type |= IS_WC;
  335. return type;
  336. }
  337. static void ttm_pool_update_free_locked(struct dma_pool *pool,
  338. unsigned freed_pages)
  339. {
  340. pool->npages_free -= freed_pages;
  341. pool->nfrees += freed_pages;
  342. }
  343. /* set memory back to wb and free the pages. */
  344. static void ttm_dma_page_put(struct dma_pool *pool, struct dma_page *d_page)
  345. {
  346. struct page *page = d_page->p;
  347. unsigned i, num_pages;
  348. int ret;
  349. /* Don't set WB on WB page pool. */
  350. if (!(pool->type & IS_CACHED)) {
  351. num_pages = pool->size / PAGE_SIZE;
  352. for (i = 0; i < num_pages; ++i, ++page) {
  353. ret = set_pages_array_wb(&page, 1);
  354. if (ret) {
  355. pr_err("%s: Failed to set %d pages to wb!\n",
  356. pool->dev_name, 1);
  357. }
  358. }
  359. }
  360. list_del(&d_page->page_list);
  361. __ttm_dma_free_page(pool, d_page);
  362. }
  363. static void ttm_dma_pages_put(struct dma_pool *pool, struct list_head *d_pages,
  364. struct page *pages[], unsigned npages)
  365. {
  366. struct dma_page *d_page, *tmp;
  367. if (pool->type & IS_HUGE) {
  368. list_for_each_entry_safe(d_page, tmp, d_pages, page_list)
  369. ttm_dma_page_put(pool, d_page);
  370. return;
  371. }
  372. /* Don't set WB on WB page pool. */
  373. if (npages && !(pool->type & IS_CACHED) &&
  374. set_pages_array_wb(pages, npages))
  375. pr_err("%s: Failed to set %d pages to wb!\n",
  376. pool->dev_name, npages);
  377. list_for_each_entry_safe(d_page, tmp, d_pages, page_list) {
  378. list_del(&d_page->page_list);
  379. __ttm_dma_free_page(pool, d_page);
  380. }
  381. }
  382. /*
  383. * Free pages from pool.
  384. *
  385. * To prevent hogging the ttm_swap process we only free NUM_PAGES_TO_ALLOC
  386. * number of pages in one go.
  387. *
  388. * @pool: to free the pages from
  389. * @nr_free: If set to true will free all pages in pool
  390. * @use_static: Safe to use static buffer
  391. **/
  392. static unsigned ttm_dma_page_pool_free(struct dma_pool *pool, unsigned nr_free,
  393. bool use_static)
  394. {
  395. static struct page *static_buf[NUM_PAGES_TO_ALLOC];
  396. unsigned long irq_flags;
  397. struct dma_page *dma_p, *tmp;
  398. struct page **pages_to_free;
  399. struct list_head d_pages;
  400. unsigned freed_pages = 0,
  401. npages_to_free = nr_free;
  402. if (NUM_PAGES_TO_ALLOC < nr_free)
  403. npages_to_free = NUM_PAGES_TO_ALLOC;
  404. #if 0
  405. if (nr_free > 1) {
  406. pr_debug("%s: (%s:%d) Attempting to free %d (%d) pages\n",
  407. pool->dev_name, pool->name, current->pid,
  408. npages_to_free, nr_free);
  409. }
  410. #endif
  411. if (use_static)
  412. pages_to_free = static_buf;
  413. else
  414. pages_to_free = kmalloc(npages_to_free * sizeof(struct page *),
  415. GFP_KERNEL);
  416. if (!pages_to_free) {
  417. pr_debug("%s: Failed to allocate memory for pool free operation\n",
  418. pool->dev_name);
  419. return 0;
  420. }
  421. INIT_LIST_HEAD(&d_pages);
  422. restart:
  423. spin_lock_irqsave(&pool->lock, irq_flags);
  424. /* We picking the oldest ones off the list */
  425. list_for_each_entry_safe_reverse(dma_p, tmp, &pool->free_list,
  426. page_list) {
  427. if (freed_pages >= npages_to_free)
  428. break;
  429. /* Move the dma_page from one list to another. */
  430. list_move(&dma_p->page_list, &d_pages);
  431. pages_to_free[freed_pages++] = dma_p->p;
  432. /* We can only remove NUM_PAGES_TO_ALLOC at a time. */
  433. if (freed_pages >= NUM_PAGES_TO_ALLOC) {
  434. ttm_pool_update_free_locked(pool, freed_pages);
  435. /**
  436. * Because changing page caching is costly
  437. * we unlock the pool to prevent stalling.
  438. */
  439. spin_unlock_irqrestore(&pool->lock, irq_flags);
  440. ttm_dma_pages_put(pool, &d_pages, pages_to_free,
  441. freed_pages);
  442. INIT_LIST_HEAD(&d_pages);
  443. if (likely(nr_free != FREE_ALL_PAGES))
  444. nr_free -= freed_pages;
  445. if (NUM_PAGES_TO_ALLOC >= nr_free)
  446. npages_to_free = nr_free;
  447. else
  448. npages_to_free = NUM_PAGES_TO_ALLOC;
  449. freed_pages = 0;
  450. /* free all so restart the processing */
  451. if (nr_free)
  452. goto restart;
  453. /* Not allowed to fall through or break because
  454. * following context is inside spinlock while we are
  455. * outside here.
  456. */
  457. goto out;
  458. }
  459. }
  460. /* remove range of pages from the pool */
  461. if (freed_pages) {
  462. ttm_pool_update_free_locked(pool, freed_pages);
  463. nr_free -= freed_pages;
  464. }
  465. spin_unlock_irqrestore(&pool->lock, irq_flags);
  466. if (freed_pages)
  467. ttm_dma_pages_put(pool, &d_pages, pages_to_free, freed_pages);
  468. out:
  469. if (pages_to_free != static_buf)
  470. kfree(pages_to_free);
  471. return nr_free;
  472. }
  473. static void ttm_dma_free_pool(struct device *dev, enum pool_type type)
  474. {
  475. struct device_pools *p;
  476. struct dma_pool *pool;
  477. if (!dev)
  478. return;
  479. mutex_lock(&_manager->lock);
  480. list_for_each_entry_reverse(p, &_manager->pools, pools) {
  481. if (p->dev != dev)
  482. continue;
  483. pool = p->pool;
  484. if (pool->type != type)
  485. continue;
  486. list_del(&p->pools);
  487. kfree(p);
  488. _manager->npools--;
  489. break;
  490. }
  491. list_for_each_entry_reverse(pool, &dev->dma_pools, pools) {
  492. if (pool->type != type)
  493. continue;
  494. /* Takes a spinlock.. */
  495. /* OK to use static buffer since global mutex is held. */
  496. ttm_dma_page_pool_free(pool, FREE_ALL_PAGES, true);
  497. WARN_ON(((pool->npages_in_use + pool->npages_free) != 0));
  498. /* This code path is called after _all_ references to the
  499. * struct device has been dropped - so nobody should be
  500. * touching it. In case somebody is trying to _add_ we are
  501. * guarded by the mutex. */
  502. list_del(&pool->pools);
  503. kfree(pool);
  504. break;
  505. }
  506. mutex_unlock(&_manager->lock);
  507. }
  508. /*
  509. * On free-ing of the 'struct device' this deconstructor is run.
  510. * Albeit the pool might have already been freed earlier.
  511. */
  512. static void ttm_dma_pool_release(struct device *dev, void *res)
  513. {
  514. struct dma_pool *pool = *(struct dma_pool **)res;
  515. if (pool)
  516. ttm_dma_free_pool(dev, pool->type);
  517. }
  518. static int ttm_dma_pool_match(struct device *dev, void *res, void *match_data)
  519. {
  520. return *(struct dma_pool **)res == match_data;
  521. }
  522. static struct dma_pool *ttm_dma_pool_init(struct device *dev, gfp_t flags,
  523. enum pool_type type)
  524. {
  525. const char *n[] = {"wc", "uc", "cached", " dma32", "huge"};
  526. enum pool_type t[] = {IS_WC, IS_UC, IS_CACHED, IS_DMA32, IS_HUGE};
  527. struct device_pools *sec_pool = NULL;
  528. struct dma_pool *pool = NULL, **ptr;
  529. unsigned i;
  530. int ret = -ENODEV;
  531. char *p;
  532. if (!dev)
  533. return NULL;
  534. ptr = devres_alloc(ttm_dma_pool_release, sizeof(*ptr), GFP_KERNEL);
  535. if (!ptr)
  536. return NULL;
  537. ret = -ENOMEM;
  538. pool = kmalloc_node(sizeof(struct dma_pool), GFP_KERNEL,
  539. dev_to_node(dev));
  540. if (!pool)
  541. goto err_mem;
  542. sec_pool = kmalloc_node(sizeof(struct device_pools), GFP_KERNEL,
  543. dev_to_node(dev));
  544. if (!sec_pool)
  545. goto err_mem;
  546. INIT_LIST_HEAD(&sec_pool->pools);
  547. sec_pool->dev = dev;
  548. sec_pool->pool = pool;
  549. INIT_LIST_HEAD(&pool->free_list);
  550. INIT_LIST_HEAD(&pool->pools);
  551. spin_lock_init(&pool->lock);
  552. pool->dev = dev;
  553. pool->npages_free = pool->npages_in_use = 0;
  554. pool->nfrees = 0;
  555. pool->gfp_flags = flags;
  556. if (type & IS_HUGE)
  557. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  558. pool->size = HPAGE_PMD_SIZE;
  559. #else
  560. BUG();
  561. #endif
  562. else
  563. pool->size = PAGE_SIZE;
  564. pool->type = type;
  565. pool->nrefills = 0;
  566. p = pool->name;
  567. for (i = 0; i < ARRAY_SIZE(t); i++) {
  568. if (type & t[i]) {
  569. p += snprintf(p, sizeof(pool->name) - (p - pool->name),
  570. "%s", n[i]);
  571. }
  572. }
  573. *p = 0;
  574. /* We copy the name for pr_ calls b/c when dma_pool_destroy is called
  575. * - the kobj->name has already been deallocated.*/
  576. snprintf(pool->dev_name, sizeof(pool->dev_name), "%s %s",
  577. dev_driver_string(dev), dev_name(dev));
  578. mutex_lock(&_manager->lock);
  579. /* You can get the dma_pool from either the global: */
  580. list_add(&sec_pool->pools, &_manager->pools);
  581. _manager->npools++;
  582. /* or from 'struct device': */
  583. list_add(&pool->pools, &dev->dma_pools);
  584. mutex_unlock(&_manager->lock);
  585. *ptr = pool;
  586. devres_add(dev, ptr);
  587. return pool;
  588. err_mem:
  589. devres_free(ptr);
  590. kfree(sec_pool);
  591. kfree(pool);
  592. return ERR_PTR(ret);
  593. }
  594. static struct dma_pool *ttm_dma_find_pool(struct device *dev,
  595. enum pool_type type)
  596. {
  597. struct dma_pool *pool, *tmp, *found = NULL;
  598. if (type == IS_UNDEFINED)
  599. return found;
  600. /* NB: We iterate on the 'struct dev' which has no spinlock, but
  601. * it does have a kref which we have taken. The kref is taken during
  602. * graphic driver loading - in the drm_pci_init it calls either
  603. * pci_dev_get or pci_register_driver which both end up taking a kref
  604. * on 'struct device'.
  605. *
  606. * On teardown, the graphic drivers end up quiescing the TTM (put_pages)
  607. * and calls the dev_res deconstructors: ttm_dma_pool_release. The nice
  608. * thing is at that point of time there are no pages associated with the
  609. * driver so this function will not be called.
  610. */
  611. list_for_each_entry_safe(pool, tmp, &dev->dma_pools, pools) {
  612. if (pool->type != type)
  613. continue;
  614. found = pool;
  615. break;
  616. }
  617. return found;
  618. }
  619. /*
  620. * Free pages the pages that failed to change the caching state. If there
  621. * are pages that have changed their caching state already put them to the
  622. * pool.
  623. */
  624. static void ttm_dma_handle_caching_state_failure(struct dma_pool *pool,
  625. struct list_head *d_pages,
  626. struct page **failed_pages,
  627. unsigned cpages)
  628. {
  629. struct dma_page *d_page, *tmp;
  630. struct page *p;
  631. unsigned i = 0;
  632. p = failed_pages[0];
  633. if (!p)
  634. return;
  635. /* Find the failed page. */
  636. list_for_each_entry_safe(d_page, tmp, d_pages, page_list) {
  637. if (d_page->p != p)
  638. continue;
  639. /* .. and then progress over the full list. */
  640. list_del(&d_page->page_list);
  641. __ttm_dma_free_page(pool, d_page);
  642. if (++i < cpages)
  643. p = failed_pages[i];
  644. else
  645. break;
  646. }
  647. }
  648. /*
  649. * Allocate 'count' pages, and put 'need' number of them on the
  650. * 'pages' and as well on the 'dma_address' starting at 'dma_offset' offset.
  651. * The full list of pages should also be on 'd_pages'.
  652. * We return zero for success, and negative numbers as errors.
  653. */
  654. static int ttm_dma_pool_alloc_new_pages(struct dma_pool *pool,
  655. struct list_head *d_pages,
  656. unsigned count)
  657. {
  658. struct page **caching_array;
  659. struct dma_page *dma_p;
  660. struct page *p;
  661. int r = 0;
  662. unsigned i, j, npages, cpages;
  663. unsigned max_cpages = min(count,
  664. (unsigned)(PAGE_SIZE/sizeof(struct page *)));
  665. /* allocate array for page caching change */
  666. caching_array = kmalloc(max_cpages*sizeof(struct page *), GFP_KERNEL);
  667. if (!caching_array) {
  668. pr_debug("%s: Unable to allocate table for new pages\n",
  669. pool->dev_name);
  670. return -ENOMEM;
  671. }
  672. if (count > 1) {
  673. pr_debug("%s: (%s:%d) Getting %d pages\n",
  674. pool->dev_name, pool->name, current->pid, count);
  675. }
  676. for (i = 0, cpages = 0; i < count; ++i) {
  677. dma_p = __ttm_dma_alloc_page(pool);
  678. if (!dma_p) {
  679. pr_debug("%s: Unable to get page %u\n",
  680. pool->dev_name, i);
  681. /* store already allocated pages in the pool after
  682. * setting the caching state */
  683. if (cpages) {
  684. r = ttm_set_pages_caching(pool, caching_array,
  685. cpages);
  686. if (r)
  687. ttm_dma_handle_caching_state_failure(
  688. pool, d_pages, caching_array,
  689. cpages);
  690. }
  691. r = -ENOMEM;
  692. goto out;
  693. }
  694. p = dma_p->p;
  695. list_add(&dma_p->page_list, d_pages);
  696. #ifdef CONFIG_HIGHMEM
  697. /* gfp flags of highmem page should never be dma32 so we
  698. * we should be fine in such case
  699. */
  700. if (PageHighMem(p))
  701. continue;
  702. #endif
  703. npages = pool->size / PAGE_SIZE;
  704. for (j = 0; j < npages; ++j) {
  705. caching_array[cpages++] = p + j;
  706. if (cpages == max_cpages) {
  707. /* Note: Cannot hold the spinlock */
  708. r = ttm_set_pages_caching(pool, caching_array,
  709. cpages);
  710. if (r) {
  711. ttm_dma_handle_caching_state_failure(
  712. pool, d_pages, caching_array,
  713. cpages);
  714. goto out;
  715. }
  716. cpages = 0;
  717. }
  718. }
  719. }
  720. if (cpages) {
  721. r = ttm_set_pages_caching(pool, caching_array, cpages);
  722. if (r)
  723. ttm_dma_handle_caching_state_failure(pool, d_pages,
  724. caching_array, cpages);
  725. }
  726. out:
  727. kfree(caching_array);
  728. return r;
  729. }
  730. /*
  731. * @return count of pages still required to fulfill the request.
  732. */
  733. static int ttm_dma_page_pool_fill_locked(struct dma_pool *pool,
  734. unsigned long *irq_flags)
  735. {
  736. unsigned count = _manager->options.small;
  737. int r = pool->npages_free;
  738. if (count > pool->npages_free) {
  739. struct list_head d_pages;
  740. INIT_LIST_HEAD(&d_pages);
  741. spin_unlock_irqrestore(&pool->lock, *irq_flags);
  742. /* Returns how many more are neccessary to fulfill the
  743. * request. */
  744. r = ttm_dma_pool_alloc_new_pages(pool, &d_pages, count);
  745. spin_lock_irqsave(&pool->lock, *irq_flags);
  746. if (!r) {
  747. /* Add the fresh to the end.. */
  748. list_splice(&d_pages, &pool->free_list);
  749. ++pool->nrefills;
  750. pool->npages_free += count;
  751. r = count;
  752. } else {
  753. struct dma_page *d_page;
  754. unsigned cpages = 0;
  755. pr_debug("%s: Failed to fill %s pool (r:%d)!\n",
  756. pool->dev_name, pool->name, r);
  757. list_for_each_entry(d_page, &d_pages, page_list) {
  758. cpages++;
  759. }
  760. list_splice_tail(&d_pages, &pool->free_list);
  761. pool->npages_free += cpages;
  762. r = cpages;
  763. }
  764. }
  765. return r;
  766. }
  767. /*
  768. * @return count of pages still required to fulfill the request.
  769. * The populate list is actually a stack (not that is matters as TTM
  770. * allocates one page at a time.
  771. */
  772. static int ttm_dma_pool_get_pages(struct dma_pool *pool,
  773. struct ttm_dma_tt *ttm_dma,
  774. unsigned index)
  775. {
  776. struct dma_page *d_page;
  777. struct ttm_tt *ttm = &ttm_dma->ttm;
  778. unsigned long irq_flags;
  779. int count, r = -ENOMEM;
  780. spin_lock_irqsave(&pool->lock, irq_flags);
  781. count = ttm_dma_page_pool_fill_locked(pool, &irq_flags);
  782. if (count) {
  783. d_page = list_first_entry(&pool->free_list, struct dma_page, page_list);
  784. ttm->pages[index] = d_page->p;
  785. ttm_dma->dma_address[index] = d_page->dma;
  786. list_move_tail(&d_page->page_list, &ttm_dma->pages_list);
  787. r = 0;
  788. pool->npages_in_use += 1;
  789. pool->npages_free -= 1;
  790. }
  791. spin_unlock_irqrestore(&pool->lock, irq_flags);
  792. return r;
  793. }
  794. static gfp_t ttm_dma_pool_gfp_flags(struct ttm_dma_tt *ttm_dma, bool huge)
  795. {
  796. struct ttm_tt *ttm = &ttm_dma->ttm;
  797. gfp_t gfp_flags;
  798. if (ttm->page_flags & TTM_PAGE_FLAG_DMA32)
  799. gfp_flags = GFP_USER | GFP_DMA32;
  800. else
  801. gfp_flags = GFP_HIGHUSER;
  802. if (ttm->page_flags & TTM_PAGE_FLAG_ZERO_ALLOC)
  803. gfp_flags |= __GFP_ZERO;
  804. if (huge) {
  805. gfp_flags |= GFP_TRANSHUGE;
  806. gfp_flags &= ~__GFP_MOVABLE;
  807. gfp_flags &= ~__GFP_COMP;
  808. }
  809. return gfp_flags;
  810. }
  811. /*
  812. * On success pages list will hold count number of correctly
  813. * cached pages. On failure will hold the negative return value (-ENOMEM, etc).
  814. */
  815. int ttm_dma_populate(struct ttm_dma_tt *ttm_dma, struct device *dev,
  816. struct ttm_operation_ctx *ctx)
  817. {
  818. struct ttm_tt *ttm = &ttm_dma->ttm;
  819. struct ttm_mem_global *mem_glob = ttm->glob->mem_glob;
  820. unsigned long num_pages = ttm->num_pages;
  821. struct dma_pool *pool;
  822. enum pool_type type;
  823. unsigned i;
  824. int ret;
  825. if (ttm->state != tt_unpopulated)
  826. return 0;
  827. INIT_LIST_HEAD(&ttm_dma->pages_list);
  828. i = 0;
  829. type = ttm_to_type(ttm->page_flags, ttm->caching_state);
  830. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  831. if (ttm->page_flags & TTM_PAGE_FLAG_DMA32)
  832. goto skip_huge;
  833. pool = ttm_dma_find_pool(dev, type | IS_HUGE);
  834. if (!pool) {
  835. gfp_t gfp_flags = ttm_dma_pool_gfp_flags(ttm_dma, true);
  836. pool = ttm_dma_pool_init(dev, gfp_flags, type | IS_HUGE);
  837. if (IS_ERR_OR_NULL(pool))
  838. goto skip_huge;
  839. }
  840. while (num_pages >= HPAGE_PMD_NR) {
  841. unsigned j;
  842. ret = ttm_dma_pool_get_pages(pool, ttm_dma, i);
  843. if (ret != 0)
  844. break;
  845. ret = ttm_mem_global_alloc_page(mem_glob, ttm->pages[i],
  846. pool->size, ctx);
  847. if (unlikely(ret != 0)) {
  848. ttm_dma_unpopulate(ttm_dma, dev);
  849. return -ENOMEM;
  850. }
  851. for (j = i + 1; j < (i + HPAGE_PMD_NR); ++j) {
  852. ttm->pages[j] = ttm->pages[j - 1] + 1;
  853. ttm_dma->dma_address[j] = ttm_dma->dma_address[j - 1] +
  854. PAGE_SIZE;
  855. }
  856. i += HPAGE_PMD_NR;
  857. num_pages -= HPAGE_PMD_NR;
  858. }
  859. skip_huge:
  860. #endif
  861. pool = ttm_dma_find_pool(dev, type);
  862. if (!pool) {
  863. gfp_t gfp_flags = ttm_dma_pool_gfp_flags(ttm_dma, false);
  864. pool = ttm_dma_pool_init(dev, gfp_flags, type);
  865. if (IS_ERR_OR_NULL(pool))
  866. return -ENOMEM;
  867. }
  868. while (num_pages) {
  869. ret = ttm_dma_pool_get_pages(pool, ttm_dma, i);
  870. if (ret != 0) {
  871. ttm_dma_unpopulate(ttm_dma, dev);
  872. return -ENOMEM;
  873. }
  874. ret = ttm_mem_global_alloc_page(mem_glob, ttm->pages[i],
  875. pool->size, ctx);
  876. if (unlikely(ret != 0)) {
  877. ttm_dma_unpopulate(ttm_dma, dev);
  878. return -ENOMEM;
  879. }
  880. ++i;
  881. --num_pages;
  882. }
  883. if (unlikely(ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)) {
  884. ret = ttm_tt_swapin(ttm);
  885. if (unlikely(ret != 0)) {
  886. ttm_dma_unpopulate(ttm_dma, dev);
  887. return ret;
  888. }
  889. }
  890. ttm->state = tt_unbound;
  891. return 0;
  892. }
  893. EXPORT_SYMBOL_GPL(ttm_dma_populate);
  894. /* Put all pages in pages list to correct pool to wait for reuse */
  895. void ttm_dma_unpopulate(struct ttm_dma_tt *ttm_dma, struct device *dev)
  896. {
  897. struct ttm_tt *ttm = &ttm_dma->ttm;
  898. struct dma_pool *pool;
  899. struct dma_page *d_page, *next;
  900. enum pool_type type;
  901. bool is_cached = false;
  902. unsigned count, i, npages = 0;
  903. unsigned long irq_flags;
  904. type = ttm_to_type(ttm->page_flags, ttm->caching_state);
  905. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  906. pool = ttm_dma_find_pool(dev, type | IS_HUGE);
  907. if (pool) {
  908. count = 0;
  909. list_for_each_entry_safe(d_page, next, &ttm_dma->pages_list,
  910. page_list) {
  911. if (!(d_page->vaddr & VADDR_FLAG_HUGE_POOL))
  912. continue;
  913. count++;
  914. ttm_mem_global_free_page(ttm->glob->mem_glob,
  915. d_page->p, pool->size);
  916. ttm_dma_page_put(pool, d_page);
  917. }
  918. spin_lock_irqsave(&pool->lock, irq_flags);
  919. pool->npages_in_use -= count;
  920. pool->nfrees += count;
  921. spin_unlock_irqrestore(&pool->lock, irq_flags);
  922. }
  923. #endif
  924. pool = ttm_dma_find_pool(dev, type);
  925. if (!pool)
  926. return;
  927. is_cached = (ttm_dma_find_pool(pool->dev,
  928. ttm_to_type(ttm->page_flags, tt_cached)) == pool);
  929. /* make sure pages array match list and count number of pages */
  930. count = 0;
  931. list_for_each_entry(d_page, &ttm_dma->pages_list, page_list) {
  932. ttm->pages[count] = d_page->p;
  933. count++;
  934. }
  935. spin_lock_irqsave(&pool->lock, irq_flags);
  936. pool->npages_in_use -= count;
  937. if (is_cached) {
  938. pool->nfrees += count;
  939. } else {
  940. pool->npages_free += count;
  941. list_splice(&ttm_dma->pages_list, &pool->free_list);
  942. /*
  943. * Wait to have at at least NUM_PAGES_TO_ALLOC number of pages
  944. * to free in order to minimize calls to set_memory_wb().
  945. */
  946. if (pool->npages_free >= (_manager->options.max_size +
  947. NUM_PAGES_TO_ALLOC))
  948. npages = pool->npages_free - _manager->options.max_size;
  949. }
  950. spin_unlock_irqrestore(&pool->lock, irq_flags);
  951. if (is_cached) {
  952. list_for_each_entry_safe(d_page, next, &ttm_dma->pages_list, page_list) {
  953. ttm_mem_global_free_page(ttm->glob->mem_glob,
  954. d_page->p, pool->size);
  955. ttm_dma_page_put(pool, d_page);
  956. }
  957. } else {
  958. for (i = 0; i < count; i++) {
  959. ttm_mem_global_free_page(ttm->glob->mem_glob,
  960. ttm->pages[i], pool->size);
  961. }
  962. }
  963. INIT_LIST_HEAD(&ttm_dma->pages_list);
  964. for (i = 0; i < ttm->num_pages; i++) {
  965. ttm->pages[i] = NULL;
  966. ttm_dma->dma_address[i] = 0;
  967. }
  968. /* shrink pool if necessary (only on !is_cached pools)*/
  969. if (npages)
  970. ttm_dma_page_pool_free(pool, npages, false);
  971. ttm->state = tt_unpopulated;
  972. }
  973. EXPORT_SYMBOL_GPL(ttm_dma_unpopulate);
  974. /**
  975. * Callback for mm to request pool to reduce number of page held.
  976. *
  977. * XXX: (dchinner) Deadlock warning!
  978. *
  979. * I'm getting sadder as I hear more pathetical whimpers about needing per-pool
  980. * shrinkers
  981. */
  982. static unsigned long
  983. ttm_dma_pool_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
  984. {
  985. static unsigned start_pool;
  986. unsigned idx = 0;
  987. unsigned pool_offset;
  988. unsigned shrink_pages = sc->nr_to_scan;
  989. struct device_pools *p;
  990. unsigned long freed = 0;
  991. if (list_empty(&_manager->pools))
  992. return SHRINK_STOP;
  993. if (!mutex_trylock(&_manager->lock))
  994. return SHRINK_STOP;
  995. if (!_manager->npools)
  996. goto out;
  997. pool_offset = ++start_pool % _manager->npools;
  998. list_for_each_entry(p, &_manager->pools, pools) {
  999. unsigned nr_free;
  1000. if (!p->dev)
  1001. continue;
  1002. if (shrink_pages == 0)
  1003. break;
  1004. /* Do it in round-robin fashion. */
  1005. if (++idx < pool_offset)
  1006. continue;
  1007. nr_free = shrink_pages;
  1008. /* OK to use static buffer since global mutex is held. */
  1009. shrink_pages = ttm_dma_page_pool_free(p->pool, nr_free, true);
  1010. freed += nr_free - shrink_pages;
  1011. pr_debug("%s: (%s:%d) Asked to shrink %d, have %d more to go\n",
  1012. p->pool->dev_name, p->pool->name, current->pid,
  1013. nr_free, shrink_pages);
  1014. }
  1015. out:
  1016. mutex_unlock(&_manager->lock);
  1017. return freed;
  1018. }
  1019. static unsigned long
  1020. ttm_dma_pool_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
  1021. {
  1022. struct device_pools *p;
  1023. unsigned long count = 0;
  1024. if (!mutex_trylock(&_manager->lock))
  1025. return 0;
  1026. list_for_each_entry(p, &_manager->pools, pools)
  1027. count += p->pool->npages_free;
  1028. mutex_unlock(&_manager->lock);
  1029. return count;
  1030. }
  1031. static void ttm_dma_pool_mm_shrink_init(struct ttm_pool_manager *manager)
  1032. {
  1033. manager->mm_shrink.count_objects = ttm_dma_pool_shrink_count;
  1034. manager->mm_shrink.scan_objects = &ttm_dma_pool_shrink_scan;
  1035. manager->mm_shrink.seeks = 1;
  1036. register_shrinker(&manager->mm_shrink);
  1037. }
  1038. static void ttm_dma_pool_mm_shrink_fini(struct ttm_pool_manager *manager)
  1039. {
  1040. unregister_shrinker(&manager->mm_shrink);
  1041. }
  1042. int ttm_dma_page_alloc_init(struct ttm_mem_global *glob, unsigned max_pages)
  1043. {
  1044. int ret = -ENOMEM;
  1045. WARN_ON(_manager);
  1046. pr_info("Initializing DMA pool allocator\n");
  1047. _manager = kzalloc(sizeof(*_manager), GFP_KERNEL);
  1048. if (!_manager)
  1049. goto err;
  1050. mutex_init(&_manager->lock);
  1051. INIT_LIST_HEAD(&_manager->pools);
  1052. _manager->options.max_size = max_pages;
  1053. _manager->options.small = SMALL_ALLOCATION;
  1054. _manager->options.alloc_size = NUM_PAGES_TO_ALLOC;
  1055. /* This takes care of auto-freeing the _manager */
  1056. ret = kobject_init_and_add(&_manager->kobj, &ttm_pool_kobj_type,
  1057. &glob->kobj, "dma_pool");
  1058. if (unlikely(ret != 0)) {
  1059. kobject_put(&_manager->kobj);
  1060. goto err;
  1061. }
  1062. ttm_dma_pool_mm_shrink_init(_manager);
  1063. return 0;
  1064. err:
  1065. return ret;
  1066. }
  1067. void ttm_dma_page_alloc_fini(void)
  1068. {
  1069. struct device_pools *p, *t;
  1070. pr_info("Finalizing DMA pool allocator\n");
  1071. ttm_dma_pool_mm_shrink_fini(_manager);
  1072. list_for_each_entry_safe_reverse(p, t, &_manager->pools, pools) {
  1073. dev_dbg(p->dev, "(%s:%d) Freeing.\n", p->pool->name,
  1074. current->pid);
  1075. WARN_ON(devres_destroy(p->dev, ttm_dma_pool_release,
  1076. ttm_dma_pool_match, p->pool));
  1077. ttm_dma_free_pool(p->dev, p->pool->type);
  1078. }
  1079. kobject_put(&_manager->kobj);
  1080. _manager = NULL;
  1081. }
  1082. int ttm_dma_page_alloc_debugfs(struct seq_file *m, void *data)
  1083. {
  1084. struct device_pools *p;
  1085. struct dma_pool *pool = NULL;
  1086. if (!_manager) {
  1087. seq_printf(m, "No pool allocator running.\n");
  1088. return 0;
  1089. }
  1090. seq_printf(m, " pool refills pages freed inuse available name\n");
  1091. mutex_lock(&_manager->lock);
  1092. list_for_each_entry(p, &_manager->pools, pools) {
  1093. struct device *dev = p->dev;
  1094. if (!dev)
  1095. continue;
  1096. pool = p->pool;
  1097. seq_printf(m, "%13s %12ld %13ld %8d %8d %8s\n",
  1098. pool->name, pool->nrefills,
  1099. pool->nfrees, pool->npages_in_use,
  1100. pool->npages_free,
  1101. pool->dev_name);
  1102. }
  1103. mutex_unlock(&_manager->lock);
  1104. return 0;
  1105. }
  1106. EXPORT_SYMBOL_GPL(ttm_dma_page_alloc_debugfs);
  1107. #endif