ttm_page_alloc_dma.c 30 KB

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