ttm_page_alloc_dma.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153
  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. #ifdef TTM_HAS_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. #ifdef TTM_HAS_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. #ifdef TTM_HAS_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. #ifdef TTM_HAS_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. d_page->p = virt_to_page(d_page->vaddr);
  311. else {
  312. kfree(d_page);
  313. d_page = NULL;
  314. }
  315. return d_page;
  316. }
  317. static enum pool_type ttm_to_type(int flags, enum ttm_caching_state cstate)
  318. {
  319. enum pool_type type = IS_UNDEFINED;
  320. if (flags & TTM_PAGE_FLAG_DMA32)
  321. type |= IS_DMA32;
  322. if (cstate == tt_cached)
  323. type |= IS_CACHED;
  324. else if (cstate == tt_uncached)
  325. type |= IS_UC;
  326. else
  327. type |= IS_WC;
  328. return type;
  329. }
  330. static void ttm_pool_update_free_locked(struct dma_pool *pool,
  331. unsigned freed_pages)
  332. {
  333. pool->npages_free -= freed_pages;
  334. pool->nfrees += freed_pages;
  335. }
  336. /* set memory back to wb and free the pages. */
  337. static void ttm_dma_pages_put(struct dma_pool *pool, struct list_head *d_pages,
  338. struct page *pages[], unsigned npages)
  339. {
  340. struct dma_page *d_page, *tmp;
  341. /* Don't set WB on WB page pool. */
  342. if (npages && !(pool->type & IS_CACHED) &&
  343. set_pages_array_wb(pages, npages))
  344. pr_err("%s: Failed to set %d pages to wb!\n",
  345. pool->dev_name, npages);
  346. list_for_each_entry_safe(d_page, tmp, d_pages, page_list) {
  347. list_del(&d_page->page_list);
  348. __ttm_dma_free_page(pool, d_page);
  349. }
  350. }
  351. static void ttm_dma_page_put(struct dma_pool *pool, struct dma_page *d_page)
  352. {
  353. /* Don't set WB on WB page pool. */
  354. if (!(pool->type & IS_CACHED) && set_pages_array_wb(&d_page->p, 1))
  355. pr_err("%s: Failed to set %d pages to wb!\n",
  356. pool->dev_name, 1);
  357. list_del(&d_page->page_list);
  358. __ttm_dma_free_page(pool, d_page);
  359. }
  360. /*
  361. * Free pages from pool.
  362. *
  363. * To prevent hogging the ttm_swap process we only free NUM_PAGES_TO_ALLOC
  364. * number of pages in one go.
  365. *
  366. * @pool: to free the pages from
  367. * @nr_free: If set to true will free all pages in pool
  368. * @gfp: GFP flags.
  369. **/
  370. static unsigned ttm_dma_page_pool_free(struct dma_pool *pool, unsigned nr_free,
  371. gfp_t gfp)
  372. {
  373. unsigned long irq_flags;
  374. struct dma_page *dma_p, *tmp;
  375. struct page **pages_to_free;
  376. struct list_head d_pages;
  377. unsigned freed_pages = 0,
  378. npages_to_free = nr_free;
  379. if (NUM_PAGES_TO_ALLOC < nr_free)
  380. npages_to_free = NUM_PAGES_TO_ALLOC;
  381. #if 0
  382. if (nr_free > 1) {
  383. pr_debug("%s: (%s:%d) Attempting to free %d (%d) pages\n",
  384. pool->dev_name, pool->name, current->pid,
  385. npages_to_free, nr_free);
  386. }
  387. #endif
  388. pages_to_free = kmalloc(npages_to_free * sizeof(struct page *), gfp);
  389. if (!pages_to_free) {
  390. pr_err("%s: Failed to allocate memory for pool free operation\n",
  391. pool->dev_name);
  392. return 0;
  393. }
  394. INIT_LIST_HEAD(&d_pages);
  395. restart:
  396. spin_lock_irqsave(&pool->lock, irq_flags);
  397. /* We picking the oldest ones off the list */
  398. list_for_each_entry_safe_reverse(dma_p, tmp, &pool->free_list,
  399. page_list) {
  400. if (freed_pages >= npages_to_free)
  401. break;
  402. /* Move the dma_page from one list to another. */
  403. list_move(&dma_p->page_list, &d_pages);
  404. pages_to_free[freed_pages++] = dma_p->p;
  405. /* We can only remove NUM_PAGES_TO_ALLOC at a time. */
  406. if (freed_pages >= NUM_PAGES_TO_ALLOC) {
  407. ttm_pool_update_free_locked(pool, freed_pages);
  408. /**
  409. * Because changing page caching is costly
  410. * we unlock the pool to prevent stalling.
  411. */
  412. spin_unlock_irqrestore(&pool->lock, irq_flags);
  413. ttm_dma_pages_put(pool, &d_pages, pages_to_free,
  414. freed_pages);
  415. INIT_LIST_HEAD(&d_pages);
  416. if (likely(nr_free != FREE_ALL_PAGES))
  417. nr_free -= freed_pages;
  418. if (NUM_PAGES_TO_ALLOC >= nr_free)
  419. npages_to_free = nr_free;
  420. else
  421. npages_to_free = NUM_PAGES_TO_ALLOC;
  422. freed_pages = 0;
  423. /* free all so restart the processing */
  424. if (nr_free)
  425. goto restart;
  426. /* Not allowed to fall through or break because
  427. * following context is inside spinlock while we are
  428. * outside here.
  429. */
  430. goto out;
  431. }
  432. }
  433. /* remove range of pages from the pool */
  434. if (freed_pages) {
  435. ttm_pool_update_free_locked(pool, freed_pages);
  436. nr_free -= freed_pages;
  437. }
  438. spin_unlock_irqrestore(&pool->lock, irq_flags);
  439. if (freed_pages)
  440. ttm_dma_pages_put(pool, &d_pages, pages_to_free, freed_pages);
  441. out:
  442. kfree(pages_to_free);
  443. return nr_free;
  444. }
  445. static void ttm_dma_free_pool(struct device *dev, enum pool_type type)
  446. {
  447. struct device_pools *p;
  448. struct dma_pool *pool;
  449. if (!dev)
  450. return;
  451. mutex_lock(&_manager->lock);
  452. list_for_each_entry_reverse(p, &_manager->pools, pools) {
  453. if (p->dev != dev)
  454. continue;
  455. pool = p->pool;
  456. if (pool->type != type)
  457. continue;
  458. list_del(&p->pools);
  459. kfree(p);
  460. _manager->npools--;
  461. break;
  462. }
  463. list_for_each_entry_reverse(pool, &dev->dma_pools, pools) {
  464. if (pool->type != type)
  465. continue;
  466. /* Takes a spinlock.. */
  467. ttm_dma_page_pool_free(pool, FREE_ALL_PAGES, GFP_KERNEL);
  468. WARN_ON(((pool->npages_in_use + pool->npages_free) != 0));
  469. /* This code path is called after _all_ references to the
  470. * struct device has been dropped - so nobody should be
  471. * touching it. In case somebody is trying to _add_ we are
  472. * guarded by the mutex. */
  473. list_del(&pool->pools);
  474. kfree(pool);
  475. break;
  476. }
  477. mutex_unlock(&_manager->lock);
  478. }
  479. /*
  480. * On free-ing of the 'struct device' this deconstructor is run.
  481. * Albeit the pool might have already been freed earlier.
  482. */
  483. static void ttm_dma_pool_release(struct device *dev, void *res)
  484. {
  485. struct dma_pool *pool = *(struct dma_pool **)res;
  486. if (pool)
  487. ttm_dma_free_pool(dev, pool->type);
  488. }
  489. static int ttm_dma_pool_match(struct device *dev, void *res, void *match_data)
  490. {
  491. return *(struct dma_pool **)res == match_data;
  492. }
  493. static struct dma_pool *ttm_dma_pool_init(struct device *dev, gfp_t flags,
  494. enum pool_type type)
  495. {
  496. char *n[] = {"wc", "uc", "cached", " dma32", "unknown",};
  497. enum pool_type t[] = {IS_WC, IS_UC, IS_CACHED, IS_DMA32, IS_UNDEFINED};
  498. struct device_pools *sec_pool = NULL;
  499. struct dma_pool *pool = NULL, **ptr;
  500. unsigned i;
  501. int ret = -ENODEV;
  502. char *p;
  503. if (!dev)
  504. return NULL;
  505. ptr = devres_alloc(ttm_dma_pool_release, sizeof(*ptr), GFP_KERNEL);
  506. if (!ptr)
  507. return NULL;
  508. ret = -ENOMEM;
  509. pool = kmalloc_node(sizeof(struct dma_pool), GFP_KERNEL,
  510. dev_to_node(dev));
  511. if (!pool)
  512. goto err_mem;
  513. sec_pool = kmalloc_node(sizeof(struct device_pools), GFP_KERNEL,
  514. dev_to_node(dev));
  515. if (!sec_pool)
  516. goto err_mem;
  517. INIT_LIST_HEAD(&sec_pool->pools);
  518. sec_pool->dev = dev;
  519. sec_pool->pool = pool;
  520. INIT_LIST_HEAD(&pool->free_list);
  521. INIT_LIST_HEAD(&pool->inuse_list);
  522. INIT_LIST_HEAD(&pool->pools);
  523. spin_lock_init(&pool->lock);
  524. pool->dev = dev;
  525. pool->npages_free = pool->npages_in_use = 0;
  526. pool->nfrees = 0;
  527. pool->gfp_flags = flags;
  528. pool->size = PAGE_SIZE;
  529. pool->type = type;
  530. pool->nrefills = 0;
  531. p = pool->name;
  532. for (i = 0; i < 5; i++) {
  533. if (type & t[i]) {
  534. p += snprintf(p, sizeof(pool->name) - (p - pool->name),
  535. "%s", n[i]);
  536. }
  537. }
  538. *p = 0;
  539. /* We copy the name for pr_ calls b/c when dma_pool_destroy is called
  540. * - the kobj->name has already been deallocated.*/
  541. snprintf(pool->dev_name, sizeof(pool->dev_name), "%s %s",
  542. dev_driver_string(dev), dev_name(dev));
  543. mutex_lock(&_manager->lock);
  544. /* You can get the dma_pool from either the global: */
  545. list_add(&sec_pool->pools, &_manager->pools);
  546. _manager->npools++;
  547. /* or from 'struct device': */
  548. list_add(&pool->pools, &dev->dma_pools);
  549. mutex_unlock(&_manager->lock);
  550. *ptr = pool;
  551. devres_add(dev, ptr);
  552. return pool;
  553. err_mem:
  554. devres_free(ptr);
  555. kfree(sec_pool);
  556. kfree(pool);
  557. return ERR_PTR(ret);
  558. }
  559. static struct dma_pool *ttm_dma_find_pool(struct device *dev,
  560. enum pool_type type)
  561. {
  562. struct dma_pool *pool, *tmp, *found = NULL;
  563. if (type == IS_UNDEFINED)
  564. return found;
  565. /* NB: We iterate on the 'struct dev' which has no spinlock, but
  566. * it does have a kref which we have taken. The kref is taken during
  567. * graphic driver loading - in the drm_pci_init it calls either
  568. * pci_dev_get or pci_register_driver which both end up taking a kref
  569. * on 'struct device'.
  570. *
  571. * On teardown, the graphic drivers end up quiescing the TTM (put_pages)
  572. * and calls the dev_res deconstructors: ttm_dma_pool_release. The nice
  573. * thing is at that point of time there are no pages associated with the
  574. * driver so this function will not be called.
  575. */
  576. list_for_each_entry_safe(pool, tmp, &dev->dma_pools, pools) {
  577. if (pool->type != type)
  578. continue;
  579. found = pool;
  580. break;
  581. }
  582. return found;
  583. }
  584. /*
  585. * Free pages the pages that failed to change the caching state. If there
  586. * are pages that have changed their caching state already put them to the
  587. * pool.
  588. */
  589. static void ttm_dma_handle_caching_state_failure(struct dma_pool *pool,
  590. struct list_head *d_pages,
  591. struct page **failed_pages,
  592. unsigned cpages)
  593. {
  594. struct dma_page *d_page, *tmp;
  595. struct page *p;
  596. unsigned i = 0;
  597. p = failed_pages[0];
  598. if (!p)
  599. return;
  600. /* Find the failed page. */
  601. list_for_each_entry_safe(d_page, tmp, d_pages, page_list) {
  602. if (d_page->p != p)
  603. continue;
  604. /* .. and then progress over the full list. */
  605. list_del(&d_page->page_list);
  606. __ttm_dma_free_page(pool, d_page);
  607. if (++i < cpages)
  608. p = failed_pages[i];
  609. else
  610. break;
  611. }
  612. }
  613. /*
  614. * Allocate 'count' pages, and put 'need' number of them on the
  615. * 'pages' and as well on the 'dma_address' starting at 'dma_offset' offset.
  616. * The full list of pages should also be on 'd_pages'.
  617. * We return zero for success, and negative numbers as errors.
  618. */
  619. static int ttm_dma_pool_alloc_new_pages(struct dma_pool *pool,
  620. struct list_head *d_pages,
  621. unsigned count)
  622. {
  623. struct page **caching_array;
  624. struct dma_page *dma_p;
  625. struct page *p;
  626. int r = 0;
  627. unsigned i, cpages;
  628. unsigned max_cpages = min(count,
  629. (unsigned)(PAGE_SIZE/sizeof(struct page *)));
  630. /* allocate array for page caching change */
  631. caching_array = kmalloc(max_cpages*sizeof(struct page *), GFP_KERNEL);
  632. if (!caching_array) {
  633. pr_err("%s: Unable to allocate table for new pages\n",
  634. pool->dev_name);
  635. return -ENOMEM;
  636. }
  637. if (count > 1) {
  638. pr_debug("%s: (%s:%d) Getting %d pages\n",
  639. pool->dev_name, pool->name, current->pid, count);
  640. }
  641. for (i = 0, cpages = 0; i < count; ++i) {
  642. dma_p = __ttm_dma_alloc_page(pool);
  643. if (!dma_p) {
  644. pr_err("%s: Unable to get page %u\n",
  645. pool->dev_name, i);
  646. /* store already allocated pages in the pool after
  647. * setting the caching state */
  648. if (cpages) {
  649. r = ttm_set_pages_caching(pool, caching_array,
  650. cpages);
  651. if (r)
  652. ttm_dma_handle_caching_state_failure(
  653. pool, d_pages, caching_array,
  654. cpages);
  655. }
  656. r = -ENOMEM;
  657. goto out;
  658. }
  659. p = dma_p->p;
  660. #ifdef CONFIG_HIGHMEM
  661. /* gfp flags of highmem page should never be dma32 so we
  662. * we should be fine in such case
  663. */
  664. if (!PageHighMem(p))
  665. #endif
  666. {
  667. caching_array[cpages++] = p;
  668. if (cpages == max_cpages) {
  669. /* Note: Cannot hold the spinlock */
  670. r = ttm_set_pages_caching(pool, caching_array,
  671. cpages);
  672. if (r) {
  673. ttm_dma_handle_caching_state_failure(
  674. pool, d_pages, caching_array,
  675. cpages);
  676. goto out;
  677. }
  678. cpages = 0;
  679. }
  680. }
  681. list_add(&dma_p->page_list, d_pages);
  682. }
  683. if (cpages) {
  684. r = ttm_set_pages_caching(pool, caching_array, cpages);
  685. if (r)
  686. ttm_dma_handle_caching_state_failure(pool, d_pages,
  687. caching_array, cpages);
  688. }
  689. out:
  690. kfree(caching_array);
  691. return r;
  692. }
  693. /*
  694. * @return count of pages still required to fulfill the request.
  695. */
  696. static int ttm_dma_page_pool_fill_locked(struct dma_pool *pool,
  697. unsigned long *irq_flags)
  698. {
  699. unsigned count = _manager->options.small;
  700. int r = pool->npages_free;
  701. if (count > pool->npages_free) {
  702. struct list_head d_pages;
  703. INIT_LIST_HEAD(&d_pages);
  704. spin_unlock_irqrestore(&pool->lock, *irq_flags);
  705. /* Returns how many more are neccessary to fulfill the
  706. * request. */
  707. r = ttm_dma_pool_alloc_new_pages(pool, &d_pages, count);
  708. spin_lock_irqsave(&pool->lock, *irq_flags);
  709. if (!r) {
  710. /* Add the fresh to the end.. */
  711. list_splice(&d_pages, &pool->free_list);
  712. ++pool->nrefills;
  713. pool->npages_free += count;
  714. r = count;
  715. } else {
  716. struct dma_page *d_page;
  717. unsigned cpages = 0;
  718. pr_err("%s: Failed to fill %s pool (r:%d)!\n",
  719. pool->dev_name, pool->name, r);
  720. list_for_each_entry(d_page, &d_pages, page_list) {
  721. cpages++;
  722. }
  723. list_splice_tail(&d_pages, &pool->free_list);
  724. pool->npages_free += cpages;
  725. r = cpages;
  726. }
  727. }
  728. return r;
  729. }
  730. /*
  731. * @return count of pages still required to fulfill the request.
  732. * The populate list is actually a stack (not that is matters as TTM
  733. * allocates one page at a time.
  734. */
  735. static int ttm_dma_pool_get_pages(struct dma_pool *pool,
  736. struct ttm_dma_tt *ttm_dma,
  737. unsigned index)
  738. {
  739. struct dma_page *d_page;
  740. struct ttm_tt *ttm = &ttm_dma->ttm;
  741. unsigned long irq_flags;
  742. int count, r = -ENOMEM;
  743. spin_lock_irqsave(&pool->lock, irq_flags);
  744. count = ttm_dma_page_pool_fill_locked(pool, &irq_flags);
  745. if (count) {
  746. d_page = list_first_entry(&pool->free_list, struct dma_page, page_list);
  747. ttm->pages[index] = d_page->p;
  748. ttm_dma->cpu_address[index] = d_page->vaddr;
  749. ttm_dma->dma_address[index] = d_page->dma;
  750. list_move_tail(&d_page->page_list, &ttm_dma->pages_list);
  751. r = 0;
  752. pool->npages_in_use += 1;
  753. pool->npages_free -= 1;
  754. }
  755. spin_unlock_irqrestore(&pool->lock, irq_flags);
  756. return r;
  757. }
  758. /*
  759. * On success pages list will hold count number of correctly
  760. * cached pages. On failure will hold the negative return value (-ENOMEM, etc).
  761. */
  762. int ttm_dma_populate(struct ttm_dma_tt *ttm_dma, struct device *dev)
  763. {
  764. struct ttm_tt *ttm = &ttm_dma->ttm;
  765. struct ttm_mem_global *mem_glob = ttm->glob->mem_glob;
  766. struct dma_pool *pool;
  767. enum pool_type type;
  768. unsigned i;
  769. gfp_t gfp_flags;
  770. int ret;
  771. if (ttm->state != tt_unpopulated)
  772. return 0;
  773. type = ttm_to_type(ttm->page_flags, ttm->caching_state);
  774. if (ttm->page_flags & TTM_PAGE_FLAG_DMA32)
  775. gfp_flags = GFP_USER | GFP_DMA32;
  776. else
  777. gfp_flags = GFP_HIGHUSER;
  778. if (ttm->page_flags & TTM_PAGE_FLAG_ZERO_ALLOC)
  779. gfp_flags |= __GFP_ZERO;
  780. pool = ttm_dma_find_pool(dev, type);
  781. if (!pool) {
  782. pool = ttm_dma_pool_init(dev, gfp_flags, type);
  783. if (IS_ERR_OR_NULL(pool)) {
  784. return -ENOMEM;
  785. }
  786. }
  787. INIT_LIST_HEAD(&ttm_dma->pages_list);
  788. for (i = 0; i < ttm->num_pages; ++i) {
  789. ret = ttm_dma_pool_get_pages(pool, ttm_dma, i);
  790. if (ret != 0) {
  791. ttm_dma_unpopulate(ttm_dma, dev);
  792. return -ENOMEM;
  793. }
  794. ret = ttm_mem_global_alloc_page(mem_glob, ttm->pages[i],
  795. false, false);
  796. if (unlikely(ret != 0)) {
  797. ttm_dma_unpopulate(ttm_dma, dev);
  798. return -ENOMEM;
  799. }
  800. }
  801. if (unlikely(ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)) {
  802. ret = ttm_tt_swapin(ttm);
  803. if (unlikely(ret != 0)) {
  804. ttm_dma_unpopulate(ttm_dma, dev);
  805. return ret;
  806. }
  807. }
  808. ttm->state = tt_unbound;
  809. return 0;
  810. }
  811. EXPORT_SYMBOL_GPL(ttm_dma_populate);
  812. /* Put all pages in pages list to correct pool to wait for reuse */
  813. void ttm_dma_unpopulate(struct ttm_dma_tt *ttm_dma, struct device *dev)
  814. {
  815. struct ttm_tt *ttm = &ttm_dma->ttm;
  816. struct dma_pool *pool;
  817. struct dma_page *d_page, *next;
  818. enum pool_type type;
  819. bool is_cached = false;
  820. unsigned count = 0, i, npages = 0;
  821. unsigned long irq_flags;
  822. type = ttm_to_type(ttm->page_flags, ttm->caching_state);
  823. pool = ttm_dma_find_pool(dev, type);
  824. if (!pool)
  825. return;
  826. is_cached = (ttm_dma_find_pool(pool->dev,
  827. ttm_to_type(ttm->page_flags, tt_cached)) == pool);
  828. /* make sure pages array match list and count number of pages */
  829. list_for_each_entry(d_page, &ttm_dma->pages_list, page_list) {
  830. ttm->pages[count] = d_page->p;
  831. count++;
  832. }
  833. spin_lock_irqsave(&pool->lock, irq_flags);
  834. pool->npages_in_use -= count;
  835. if (is_cached) {
  836. pool->nfrees += count;
  837. } else {
  838. pool->npages_free += count;
  839. list_splice(&ttm_dma->pages_list, &pool->free_list);
  840. npages = count;
  841. if (pool->npages_free > _manager->options.max_size) {
  842. npages = pool->npages_free - _manager->options.max_size;
  843. /* free at least NUM_PAGES_TO_ALLOC number of pages
  844. * to reduce calls to set_memory_wb */
  845. if (npages < NUM_PAGES_TO_ALLOC)
  846. npages = NUM_PAGES_TO_ALLOC;
  847. }
  848. }
  849. spin_unlock_irqrestore(&pool->lock, irq_flags);
  850. if (is_cached) {
  851. list_for_each_entry_safe(d_page, next, &ttm_dma->pages_list, page_list) {
  852. ttm_mem_global_free_page(ttm->glob->mem_glob,
  853. d_page->p);
  854. ttm_dma_page_put(pool, d_page);
  855. }
  856. } else {
  857. for (i = 0; i < count; i++) {
  858. ttm_mem_global_free_page(ttm->glob->mem_glob,
  859. ttm->pages[i]);
  860. }
  861. }
  862. INIT_LIST_HEAD(&ttm_dma->pages_list);
  863. for (i = 0; i < ttm->num_pages; i++) {
  864. ttm->pages[i] = NULL;
  865. ttm_dma->cpu_address[i] = 0;
  866. ttm_dma->dma_address[i] = 0;
  867. }
  868. /* shrink pool if necessary (only on !is_cached pools)*/
  869. if (npages)
  870. ttm_dma_page_pool_free(pool, npages, GFP_KERNEL);
  871. ttm->state = tt_unpopulated;
  872. }
  873. EXPORT_SYMBOL_GPL(ttm_dma_unpopulate);
  874. /**
  875. * Callback for mm to request pool to reduce number of page held.
  876. *
  877. * XXX: (dchinner) Deadlock warning!
  878. *
  879. * We need to pass sc->gfp_mask to ttm_dma_page_pool_free().
  880. *
  881. * I'm getting sadder as I hear more pathetical whimpers about needing per-pool
  882. * shrinkers
  883. */
  884. static unsigned long
  885. ttm_dma_pool_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
  886. {
  887. static unsigned start_pool;
  888. unsigned idx = 0;
  889. unsigned pool_offset;
  890. unsigned shrink_pages = sc->nr_to_scan;
  891. struct device_pools *p;
  892. unsigned long freed = 0;
  893. if (list_empty(&_manager->pools))
  894. return SHRINK_STOP;
  895. if (!mutex_trylock(&_manager->lock))
  896. return SHRINK_STOP;
  897. if (!_manager->npools)
  898. goto out;
  899. pool_offset = ++start_pool % _manager->npools;
  900. list_for_each_entry(p, &_manager->pools, pools) {
  901. unsigned nr_free;
  902. if (!p->dev)
  903. continue;
  904. if (shrink_pages == 0)
  905. break;
  906. /* Do it in round-robin fashion. */
  907. if (++idx < pool_offset)
  908. continue;
  909. nr_free = shrink_pages;
  910. shrink_pages = ttm_dma_page_pool_free(p->pool, nr_free,
  911. sc->gfp_mask);
  912. freed += nr_free - shrink_pages;
  913. pr_debug("%s: (%s:%d) Asked to shrink %d, have %d more to go\n",
  914. p->pool->dev_name, p->pool->name, current->pid,
  915. nr_free, shrink_pages);
  916. }
  917. out:
  918. mutex_unlock(&_manager->lock);
  919. return freed;
  920. }
  921. static unsigned long
  922. ttm_dma_pool_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
  923. {
  924. struct device_pools *p;
  925. unsigned long count = 0;
  926. if (!mutex_trylock(&_manager->lock))
  927. return 0;
  928. list_for_each_entry(p, &_manager->pools, pools)
  929. count += p->pool->npages_free;
  930. mutex_unlock(&_manager->lock);
  931. return count;
  932. }
  933. static void ttm_dma_pool_mm_shrink_init(struct ttm_pool_manager *manager)
  934. {
  935. manager->mm_shrink.count_objects = ttm_dma_pool_shrink_count;
  936. manager->mm_shrink.scan_objects = &ttm_dma_pool_shrink_scan;
  937. manager->mm_shrink.seeks = 1;
  938. register_shrinker(&manager->mm_shrink);
  939. }
  940. static void ttm_dma_pool_mm_shrink_fini(struct ttm_pool_manager *manager)
  941. {
  942. unregister_shrinker(&manager->mm_shrink);
  943. }
  944. int ttm_dma_page_alloc_init(struct ttm_mem_global *glob, unsigned max_pages)
  945. {
  946. int ret = -ENOMEM;
  947. WARN_ON(_manager);
  948. pr_info("Initializing DMA pool allocator\n");
  949. _manager = kzalloc(sizeof(*_manager), GFP_KERNEL);
  950. if (!_manager)
  951. goto err;
  952. mutex_init(&_manager->lock);
  953. INIT_LIST_HEAD(&_manager->pools);
  954. _manager->options.max_size = max_pages;
  955. _manager->options.small = SMALL_ALLOCATION;
  956. _manager->options.alloc_size = NUM_PAGES_TO_ALLOC;
  957. /* This takes care of auto-freeing the _manager */
  958. ret = kobject_init_and_add(&_manager->kobj, &ttm_pool_kobj_type,
  959. &glob->kobj, "dma_pool");
  960. if (unlikely(ret != 0)) {
  961. kobject_put(&_manager->kobj);
  962. goto err;
  963. }
  964. ttm_dma_pool_mm_shrink_init(_manager);
  965. return 0;
  966. err:
  967. return ret;
  968. }
  969. void ttm_dma_page_alloc_fini(void)
  970. {
  971. struct device_pools *p, *t;
  972. pr_info("Finalizing DMA pool allocator\n");
  973. ttm_dma_pool_mm_shrink_fini(_manager);
  974. list_for_each_entry_safe_reverse(p, t, &_manager->pools, pools) {
  975. dev_dbg(p->dev, "(%s:%d) Freeing.\n", p->pool->name,
  976. current->pid);
  977. WARN_ON(devres_destroy(p->dev, ttm_dma_pool_release,
  978. ttm_dma_pool_match, p->pool));
  979. ttm_dma_free_pool(p->dev, p->pool->type);
  980. }
  981. kobject_put(&_manager->kobj);
  982. _manager = NULL;
  983. }
  984. int ttm_dma_page_alloc_debugfs(struct seq_file *m, void *data)
  985. {
  986. struct device_pools *p;
  987. struct dma_pool *pool = NULL;
  988. char *h[] = {"pool", "refills", "pages freed", "inuse", "available",
  989. "name", "virt", "busaddr"};
  990. if (!_manager) {
  991. seq_printf(m, "No pool allocator running.\n");
  992. return 0;
  993. }
  994. seq_printf(m, "%13s %12s %13s %8s %8s %8s\n",
  995. h[0], h[1], h[2], h[3], h[4], h[5]);
  996. mutex_lock(&_manager->lock);
  997. list_for_each_entry(p, &_manager->pools, pools) {
  998. struct device *dev = p->dev;
  999. if (!dev)
  1000. continue;
  1001. pool = p->pool;
  1002. seq_printf(m, "%13s %12ld %13ld %8d %8d %8s\n",
  1003. pool->name, pool->nrefills,
  1004. pool->nfrees, pool->npages_in_use,
  1005. pool->npages_free,
  1006. pool->dev_name);
  1007. }
  1008. mutex_unlock(&_manager->lock);
  1009. return 0;
  1010. }
  1011. EXPORT_SYMBOL_GPL(ttm_dma_page_alloc_debugfs);
  1012. #endif