ttm_page_alloc_dma.c 30 KB

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