ttm_page_alloc_dma.c 30 KB

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