ttm_page_alloc_dma.c 30 KB

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