ttm_page_alloc_dma.c 30 KB

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