ttm_page_alloc_dma.c 30 KB

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