ttm_page_alloc_dma.c 29 KB

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