memalloc.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
  4. * Takashi Iwai <tiwai@suse.de>
  5. *
  6. * Generic memory allocators
  7. */
  8. #include <linux/slab.h>
  9. #include <linux/mm.h>
  10. #include <linux/dma-mapping.h>
  11. #include <linux/dma-map-ops.h>
  12. #include <linux/genalloc.h>
  13. #include <linux/highmem.h>
  14. #include <linux/vmalloc.h>
  15. #ifdef CONFIG_X86
  16. #include <asm/set_memory.h>
  17. #endif
  18. #include <sound/memalloc.h>
  19. struct snd_malloc_ops {
  20. void *(*alloc)(struct snd_dma_buffer *dmab, size_t size);
  21. void (*free)(struct snd_dma_buffer *dmab);
  22. dma_addr_t (*get_addr)(struct snd_dma_buffer *dmab, size_t offset);
  23. struct page *(*get_page)(struct snd_dma_buffer *dmab, size_t offset);
  24. unsigned int (*get_chunk_size)(struct snd_dma_buffer *dmab,
  25. unsigned int ofs, unsigned int size);
  26. int (*mmap)(struct snd_dma_buffer *dmab, struct vm_area_struct *area);
  27. void (*sync)(struct snd_dma_buffer *dmab, enum snd_dma_sync_mode mode);
  28. };
  29. #define DEFAULT_GFP \
  30. (GFP_KERNEL | \
  31. __GFP_RETRY_MAYFAIL | /* don't trigger OOM-killer */ \
  32. __GFP_NOWARN) /* no stack trace print - this call is non-critical */
  33. static const struct snd_malloc_ops *snd_dma_get_ops(struct snd_dma_buffer *dmab);
  34. static void *__snd_dma_alloc_pages(struct snd_dma_buffer *dmab, size_t size)
  35. {
  36. const struct snd_malloc_ops *ops = snd_dma_get_ops(dmab);
  37. if (WARN_ON_ONCE(!ops || !ops->alloc))
  38. return NULL;
  39. return ops->alloc(dmab, size);
  40. }
  41. /**
  42. * snd_dma_alloc_dir_pages - allocate the buffer area according to the given
  43. * type and direction
  44. * @type: the DMA buffer type
  45. * @device: the device pointer
  46. * @dir: DMA direction
  47. * @size: the buffer size to allocate
  48. * @dmab: buffer allocation record to store the allocated data
  49. *
  50. * Calls the memory-allocator function for the corresponding
  51. * buffer type.
  52. *
  53. * Return: Zero if the buffer with the given size is allocated successfully,
  54. * otherwise a negative value on error.
  55. */
  56. int snd_dma_alloc_dir_pages(int type, struct device *device,
  57. enum dma_data_direction dir, size_t size,
  58. struct snd_dma_buffer *dmab)
  59. {
  60. if (WARN_ON(!size))
  61. return -ENXIO;
  62. if (WARN_ON(!dmab))
  63. return -ENXIO;
  64. size = PAGE_ALIGN(size);
  65. dmab->dev.type = type;
  66. dmab->dev.dev = device;
  67. dmab->dev.dir = dir;
  68. dmab->bytes = 0;
  69. dmab->addr = 0;
  70. dmab->private_data = NULL;
  71. dmab->area = __snd_dma_alloc_pages(dmab, size);
  72. if (!dmab->area)
  73. return -ENOMEM;
  74. dmab->bytes = size;
  75. return 0;
  76. }
  77. EXPORT_SYMBOL(snd_dma_alloc_dir_pages);
  78. /**
  79. * snd_dma_alloc_pages_fallback - allocate the buffer area according to the given type with fallback
  80. * @type: the DMA buffer type
  81. * @device: the device pointer
  82. * @size: the buffer size to allocate
  83. * @dmab: buffer allocation record to store the allocated data
  84. *
  85. * Calls the memory-allocator function for the corresponding
  86. * buffer type. When no space is left, this function reduces the size and
  87. * tries to allocate again. The size actually allocated is stored in
  88. * res_size argument.
  89. *
  90. * Return: Zero if the buffer with the given size is allocated successfully,
  91. * otherwise a negative value on error.
  92. */
  93. int snd_dma_alloc_pages_fallback(int type, struct device *device, size_t size,
  94. struct snd_dma_buffer *dmab)
  95. {
  96. int err;
  97. while ((err = snd_dma_alloc_pages(type, device, size, dmab)) < 0) {
  98. if (err != -ENOMEM)
  99. return err;
  100. if (size <= PAGE_SIZE)
  101. return -ENOMEM;
  102. size >>= 1;
  103. size = PAGE_SIZE << get_order(size);
  104. }
  105. if (! dmab->area)
  106. return -ENOMEM;
  107. return 0;
  108. }
  109. EXPORT_SYMBOL(snd_dma_alloc_pages_fallback);
  110. /**
  111. * snd_dma_free_pages - release the allocated buffer
  112. * @dmab: the buffer allocation record to release
  113. *
  114. * Releases the allocated buffer via snd_dma_alloc_pages().
  115. */
  116. void snd_dma_free_pages(struct snd_dma_buffer *dmab)
  117. {
  118. const struct snd_malloc_ops *ops = snd_dma_get_ops(dmab);
  119. if (ops && ops->free)
  120. ops->free(dmab);
  121. }
  122. EXPORT_SYMBOL(snd_dma_free_pages);
  123. /* called by devres */
  124. static void __snd_release_pages(struct device *dev, void *res)
  125. {
  126. snd_dma_free_pages(res);
  127. }
  128. /**
  129. * snd_devm_alloc_dir_pages - allocate the buffer and manage with devres
  130. * @dev: the device pointer
  131. * @type: the DMA buffer type
  132. * @dir: DMA direction
  133. * @size: the buffer size to allocate
  134. *
  135. * Allocate buffer pages depending on the given type and manage using devres.
  136. * The pages will be released automatically at the device removal.
  137. *
  138. * Unlike snd_dma_alloc_pages(), this function requires the real device pointer,
  139. * hence it can't work with SNDRV_DMA_TYPE_CONTINUOUS or
  140. * SNDRV_DMA_TYPE_VMALLOC type.
  141. *
  142. * Return: the snd_dma_buffer object at success, or NULL if failed
  143. */
  144. struct snd_dma_buffer *
  145. snd_devm_alloc_dir_pages(struct device *dev, int type,
  146. enum dma_data_direction dir, size_t size)
  147. {
  148. struct snd_dma_buffer *dmab;
  149. int err;
  150. if (WARN_ON(type == SNDRV_DMA_TYPE_CONTINUOUS ||
  151. type == SNDRV_DMA_TYPE_VMALLOC))
  152. return NULL;
  153. dmab = devres_alloc(__snd_release_pages, sizeof(*dmab), GFP_KERNEL);
  154. if (!dmab)
  155. return NULL;
  156. err = snd_dma_alloc_dir_pages(type, dev, dir, size, dmab);
  157. if (err < 0) {
  158. devres_free(dmab);
  159. return NULL;
  160. }
  161. devres_add(dev, dmab);
  162. return dmab;
  163. }
  164. EXPORT_SYMBOL_GPL(snd_devm_alloc_dir_pages);
  165. /**
  166. * snd_dma_buffer_mmap - perform mmap of the given DMA buffer
  167. * @dmab: buffer allocation information
  168. * @area: VM area information
  169. *
  170. * Return: zero if successful, or a negative error code
  171. */
  172. int snd_dma_buffer_mmap(struct snd_dma_buffer *dmab,
  173. struct vm_area_struct *area)
  174. {
  175. const struct snd_malloc_ops *ops;
  176. if (!dmab)
  177. return -ENOENT;
  178. ops = snd_dma_get_ops(dmab);
  179. if (ops && ops->mmap)
  180. return ops->mmap(dmab, area);
  181. else
  182. return -ENOENT;
  183. }
  184. EXPORT_SYMBOL(snd_dma_buffer_mmap);
  185. #ifdef CONFIG_HAS_DMA
  186. /**
  187. * snd_dma_buffer_sync - sync DMA buffer between CPU and device
  188. * @dmab: buffer allocation information
  189. * @mode: sync mode
  190. */
  191. void snd_dma_buffer_sync(struct snd_dma_buffer *dmab,
  192. enum snd_dma_sync_mode mode)
  193. {
  194. const struct snd_malloc_ops *ops;
  195. if (!dmab || !dmab->dev.need_sync)
  196. return;
  197. ops = snd_dma_get_ops(dmab);
  198. if (ops && ops->sync)
  199. ops->sync(dmab, mode);
  200. }
  201. EXPORT_SYMBOL_GPL(snd_dma_buffer_sync);
  202. #endif /* CONFIG_HAS_DMA */
  203. /**
  204. * snd_sgbuf_get_addr - return the physical address at the corresponding offset
  205. * @dmab: buffer allocation information
  206. * @offset: offset in the ring buffer
  207. *
  208. * Return: the physical address
  209. */
  210. dma_addr_t snd_sgbuf_get_addr(struct snd_dma_buffer *dmab, size_t offset)
  211. {
  212. const struct snd_malloc_ops *ops = snd_dma_get_ops(dmab);
  213. if (ops && ops->get_addr)
  214. return ops->get_addr(dmab, offset);
  215. else
  216. return dmab->addr + offset;
  217. }
  218. EXPORT_SYMBOL(snd_sgbuf_get_addr);
  219. /**
  220. * snd_sgbuf_get_page - return the physical page at the corresponding offset
  221. * @dmab: buffer allocation information
  222. * @offset: offset in the ring buffer
  223. *
  224. * Return: the page pointer
  225. */
  226. struct page *snd_sgbuf_get_page(struct snd_dma_buffer *dmab, size_t offset)
  227. {
  228. const struct snd_malloc_ops *ops = snd_dma_get_ops(dmab);
  229. if (ops && ops->get_page)
  230. return ops->get_page(dmab, offset);
  231. else
  232. return virt_to_page(dmab->area + offset);
  233. }
  234. EXPORT_SYMBOL(snd_sgbuf_get_page);
  235. /**
  236. * snd_sgbuf_get_chunk_size - compute the max chunk size with continuous pages
  237. * on sg-buffer
  238. * @dmab: buffer allocation information
  239. * @ofs: offset in the ring buffer
  240. * @size: the requested size
  241. *
  242. * Return: the chunk size
  243. */
  244. unsigned int snd_sgbuf_get_chunk_size(struct snd_dma_buffer *dmab,
  245. unsigned int ofs, unsigned int size)
  246. {
  247. const struct snd_malloc_ops *ops = snd_dma_get_ops(dmab);
  248. if (ops && ops->get_chunk_size)
  249. return ops->get_chunk_size(dmab, ofs, size);
  250. else
  251. return size;
  252. }
  253. EXPORT_SYMBOL(snd_sgbuf_get_chunk_size);
  254. /*
  255. * Continuous pages allocator
  256. */
  257. static void *do_alloc_pages(struct device *dev, size_t size, dma_addr_t *addr,
  258. bool wc)
  259. {
  260. void *p;
  261. gfp_t gfp = GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN;
  262. again:
  263. p = alloc_pages_exact(size, gfp);
  264. if (!p)
  265. return NULL;
  266. *addr = page_to_phys(virt_to_page(p));
  267. if (!dev)
  268. return p;
  269. if ((*addr + size - 1) & ~dev->coherent_dma_mask) {
  270. if (IS_ENABLED(CONFIG_ZONE_DMA32) && !(gfp & GFP_DMA32)) {
  271. gfp |= GFP_DMA32;
  272. goto again;
  273. }
  274. if (IS_ENABLED(CONFIG_ZONE_DMA) && !(gfp & GFP_DMA)) {
  275. gfp = (gfp & ~GFP_DMA32) | GFP_DMA;
  276. goto again;
  277. }
  278. }
  279. #ifdef CONFIG_X86
  280. if (wc)
  281. set_memory_wc((unsigned long)(p), size >> PAGE_SHIFT);
  282. #endif
  283. return p;
  284. }
  285. static void do_free_pages(void *p, size_t size, bool wc)
  286. {
  287. #ifdef CONFIG_X86
  288. if (wc)
  289. set_memory_wb((unsigned long)(p), size >> PAGE_SHIFT);
  290. #endif
  291. free_pages_exact(p, size);
  292. }
  293. static void *snd_dma_continuous_alloc(struct snd_dma_buffer *dmab, size_t size)
  294. {
  295. return do_alloc_pages(dmab->dev.dev, size, &dmab->addr, false);
  296. }
  297. static void snd_dma_continuous_free(struct snd_dma_buffer *dmab)
  298. {
  299. do_free_pages(dmab->area, dmab->bytes, false);
  300. }
  301. static int snd_dma_continuous_mmap(struct snd_dma_buffer *dmab,
  302. struct vm_area_struct *area)
  303. {
  304. return remap_pfn_range(area, area->vm_start,
  305. dmab->addr >> PAGE_SHIFT,
  306. area->vm_end - area->vm_start,
  307. area->vm_page_prot);
  308. }
  309. static const struct snd_malloc_ops snd_dma_continuous_ops = {
  310. .alloc = snd_dma_continuous_alloc,
  311. .free = snd_dma_continuous_free,
  312. .mmap = snd_dma_continuous_mmap,
  313. };
  314. /*
  315. * VMALLOC allocator
  316. */
  317. static void *snd_dma_vmalloc_alloc(struct snd_dma_buffer *dmab, size_t size)
  318. {
  319. return vmalloc(size);
  320. }
  321. static void snd_dma_vmalloc_free(struct snd_dma_buffer *dmab)
  322. {
  323. vfree(dmab->area);
  324. }
  325. static int snd_dma_vmalloc_mmap(struct snd_dma_buffer *dmab,
  326. struct vm_area_struct *area)
  327. {
  328. return remap_vmalloc_range(area, dmab->area, 0);
  329. }
  330. #define get_vmalloc_page_addr(dmab, offset) \
  331. page_to_phys(vmalloc_to_page((dmab)->area + (offset)))
  332. static dma_addr_t snd_dma_vmalloc_get_addr(struct snd_dma_buffer *dmab,
  333. size_t offset)
  334. {
  335. return get_vmalloc_page_addr(dmab, offset) + offset % PAGE_SIZE;
  336. }
  337. static struct page *snd_dma_vmalloc_get_page(struct snd_dma_buffer *dmab,
  338. size_t offset)
  339. {
  340. return vmalloc_to_page(dmab->area + offset);
  341. }
  342. static unsigned int
  343. snd_dma_vmalloc_get_chunk_size(struct snd_dma_buffer *dmab,
  344. unsigned int ofs, unsigned int size)
  345. {
  346. unsigned int start, end;
  347. unsigned long addr;
  348. start = ALIGN_DOWN(ofs, PAGE_SIZE);
  349. end = ofs + size - 1; /* the last byte address */
  350. /* check page continuity */
  351. addr = get_vmalloc_page_addr(dmab, start);
  352. for (;;) {
  353. start += PAGE_SIZE;
  354. if (start > end)
  355. break;
  356. addr += PAGE_SIZE;
  357. if (get_vmalloc_page_addr(dmab, start) != addr)
  358. return start - ofs;
  359. }
  360. /* ok, all on continuous pages */
  361. return size;
  362. }
  363. static const struct snd_malloc_ops snd_dma_vmalloc_ops = {
  364. .alloc = snd_dma_vmalloc_alloc,
  365. .free = snd_dma_vmalloc_free,
  366. .mmap = snd_dma_vmalloc_mmap,
  367. .get_addr = snd_dma_vmalloc_get_addr,
  368. .get_page = snd_dma_vmalloc_get_page,
  369. .get_chunk_size = snd_dma_vmalloc_get_chunk_size,
  370. };
  371. #ifdef CONFIG_HAS_DMA
  372. /*
  373. * IRAM allocator
  374. */
  375. #ifdef CONFIG_GENERIC_ALLOCATOR
  376. static void *snd_dma_iram_alloc(struct snd_dma_buffer *dmab, size_t size)
  377. {
  378. struct device *dev = dmab->dev.dev;
  379. struct gen_pool *pool;
  380. void *p;
  381. if (dev->of_node) {
  382. pool = of_gen_pool_get(dev->of_node, "iram", 0);
  383. /* Assign the pool into private_data field */
  384. dmab->private_data = pool;
  385. p = gen_pool_dma_alloc_align(pool, size, &dmab->addr, PAGE_SIZE);
  386. if (p)
  387. return p;
  388. }
  389. /* Internal memory might have limited size and no enough space,
  390. * so if we fail to malloc, try to fetch memory traditionally.
  391. */
  392. dmab->dev.type = SNDRV_DMA_TYPE_DEV;
  393. return __snd_dma_alloc_pages(dmab, size);
  394. }
  395. static void snd_dma_iram_free(struct snd_dma_buffer *dmab)
  396. {
  397. struct gen_pool *pool = dmab->private_data;
  398. if (pool && dmab->area)
  399. gen_pool_free(pool, (unsigned long)dmab->area, dmab->bytes);
  400. }
  401. static int snd_dma_iram_mmap(struct snd_dma_buffer *dmab,
  402. struct vm_area_struct *area)
  403. {
  404. area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
  405. return remap_pfn_range(area, area->vm_start,
  406. dmab->addr >> PAGE_SHIFT,
  407. area->vm_end - area->vm_start,
  408. area->vm_page_prot);
  409. }
  410. static const struct snd_malloc_ops snd_dma_iram_ops = {
  411. .alloc = snd_dma_iram_alloc,
  412. .free = snd_dma_iram_free,
  413. .mmap = snd_dma_iram_mmap,
  414. };
  415. #endif /* CONFIG_GENERIC_ALLOCATOR */
  416. /*
  417. * Coherent device pages allocator
  418. */
  419. static void *snd_dma_dev_alloc(struct snd_dma_buffer *dmab, size_t size)
  420. {
  421. return dma_alloc_coherent(dmab->dev.dev, size, &dmab->addr, DEFAULT_GFP);
  422. }
  423. static void snd_dma_dev_free(struct snd_dma_buffer *dmab)
  424. {
  425. dma_free_coherent(dmab->dev.dev, dmab->bytes, dmab->area, dmab->addr);
  426. }
  427. static int snd_dma_dev_mmap(struct snd_dma_buffer *dmab,
  428. struct vm_area_struct *area)
  429. {
  430. return dma_mmap_coherent(dmab->dev.dev, area,
  431. dmab->area, dmab->addr, dmab->bytes);
  432. }
  433. static const struct snd_malloc_ops snd_dma_dev_ops = {
  434. .alloc = snd_dma_dev_alloc,
  435. .free = snd_dma_dev_free,
  436. .mmap = snd_dma_dev_mmap,
  437. };
  438. /*
  439. * Write-combined pages
  440. */
  441. #ifdef CONFIG_SND_DMA_SGBUF
  442. /* x86-specific allocations */
  443. static void *snd_dma_wc_alloc(struct snd_dma_buffer *dmab, size_t size)
  444. {
  445. void *p = do_alloc_pages(dmab->dev.dev, size, &dmab->addr, true);
  446. if (!p)
  447. return NULL;
  448. dmab->addr = dma_map_single(dmab->dev.dev, p, size, DMA_BIDIRECTIONAL);
  449. if (dma_mapping_error(dmab->dev.dev, dmab->addr)) {
  450. do_free_pages(dmab->area, size, true);
  451. return NULL;
  452. }
  453. return p;
  454. }
  455. static void snd_dma_wc_free(struct snd_dma_buffer *dmab)
  456. {
  457. dma_unmap_single(dmab->dev.dev, dmab->addr, dmab->bytes,
  458. DMA_BIDIRECTIONAL);
  459. do_free_pages(dmab->area, dmab->bytes, true);
  460. }
  461. static int snd_dma_wc_mmap(struct snd_dma_buffer *dmab,
  462. struct vm_area_struct *area)
  463. {
  464. area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
  465. return dma_mmap_coherent(dmab->dev.dev, area,
  466. dmab->area, dmab->addr, dmab->bytes);
  467. }
  468. #else
  469. static void *snd_dma_wc_alloc(struct snd_dma_buffer *dmab, size_t size)
  470. {
  471. return dma_alloc_wc(dmab->dev.dev, size, &dmab->addr, DEFAULT_GFP);
  472. }
  473. static void snd_dma_wc_free(struct snd_dma_buffer *dmab)
  474. {
  475. dma_free_wc(dmab->dev.dev, dmab->bytes, dmab->area, dmab->addr);
  476. }
  477. static int snd_dma_wc_mmap(struct snd_dma_buffer *dmab,
  478. struct vm_area_struct *area)
  479. {
  480. return dma_mmap_wc(dmab->dev.dev, area,
  481. dmab->area, dmab->addr, dmab->bytes);
  482. }
  483. #endif
  484. static const struct snd_malloc_ops snd_dma_wc_ops = {
  485. .alloc = snd_dma_wc_alloc,
  486. .free = snd_dma_wc_free,
  487. .mmap = snd_dma_wc_mmap,
  488. };
  489. /*
  490. * Non-contiguous pages allocator
  491. */
  492. static void *snd_dma_noncontig_alloc(struct snd_dma_buffer *dmab, size_t size)
  493. {
  494. struct sg_table *sgt;
  495. void *p;
  496. sgt = dma_alloc_noncontiguous(dmab->dev.dev, size, dmab->dev.dir,
  497. DEFAULT_GFP, 0);
  498. if (!sgt)
  499. return NULL;
  500. dmab->dev.need_sync = dma_need_sync(dmab->dev.dev,
  501. sg_dma_address(sgt->sgl));
  502. p = dma_vmap_noncontiguous(dmab->dev.dev, size, sgt);
  503. if (p) {
  504. dmab->private_data = sgt;
  505. /* store the first page address for convenience */
  506. dmab->addr = snd_sgbuf_get_addr(dmab, 0);
  507. } else {
  508. dma_free_noncontiguous(dmab->dev.dev, size, sgt, dmab->dev.dir);
  509. }
  510. return p;
  511. }
  512. static void snd_dma_noncontig_free(struct snd_dma_buffer *dmab)
  513. {
  514. dma_vunmap_noncontiguous(dmab->dev.dev, dmab->area);
  515. dma_free_noncontiguous(dmab->dev.dev, dmab->bytes, dmab->private_data,
  516. dmab->dev.dir);
  517. }
  518. static int snd_dma_noncontig_mmap(struct snd_dma_buffer *dmab,
  519. struct vm_area_struct *area)
  520. {
  521. return dma_mmap_noncontiguous(dmab->dev.dev, area,
  522. dmab->bytes, dmab->private_data);
  523. }
  524. static void snd_dma_noncontig_sync(struct snd_dma_buffer *dmab,
  525. enum snd_dma_sync_mode mode)
  526. {
  527. if (mode == SNDRV_DMA_SYNC_CPU) {
  528. if (dmab->dev.dir == DMA_TO_DEVICE)
  529. return;
  530. invalidate_kernel_vmap_range(dmab->area, dmab->bytes);
  531. dma_sync_sgtable_for_cpu(dmab->dev.dev, dmab->private_data,
  532. dmab->dev.dir);
  533. } else {
  534. if (dmab->dev.dir == DMA_FROM_DEVICE)
  535. return;
  536. flush_kernel_vmap_range(dmab->area, dmab->bytes);
  537. dma_sync_sgtable_for_device(dmab->dev.dev, dmab->private_data,
  538. dmab->dev.dir);
  539. }
  540. }
  541. static inline void snd_dma_noncontig_iter_set(struct snd_dma_buffer *dmab,
  542. struct sg_page_iter *piter,
  543. size_t offset)
  544. {
  545. struct sg_table *sgt = dmab->private_data;
  546. __sg_page_iter_start(piter, sgt->sgl, sgt->orig_nents,
  547. offset >> PAGE_SHIFT);
  548. }
  549. static dma_addr_t snd_dma_noncontig_get_addr(struct snd_dma_buffer *dmab,
  550. size_t offset)
  551. {
  552. struct sg_dma_page_iter iter;
  553. snd_dma_noncontig_iter_set(dmab, &iter.base, offset);
  554. __sg_page_iter_dma_next(&iter);
  555. return sg_page_iter_dma_address(&iter) + offset % PAGE_SIZE;
  556. }
  557. static struct page *snd_dma_noncontig_get_page(struct snd_dma_buffer *dmab,
  558. size_t offset)
  559. {
  560. struct sg_page_iter iter;
  561. snd_dma_noncontig_iter_set(dmab, &iter, offset);
  562. __sg_page_iter_next(&iter);
  563. return sg_page_iter_page(&iter);
  564. }
  565. static unsigned int
  566. snd_dma_noncontig_get_chunk_size(struct snd_dma_buffer *dmab,
  567. unsigned int ofs, unsigned int size)
  568. {
  569. struct sg_dma_page_iter iter;
  570. unsigned int start, end;
  571. unsigned long addr;
  572. start = ALIGN_DOWN(ofs, PAGE_SIZE);
  573. end = ofs + size - 1; /* the last byte address */
  574. snd_dma_noncontig_iter_set(dmab, &iter.base, start);
  575. if (!__sg_page_iter_dma_next(&iter))
  576. return 0;
  577. /* check page continuity */
  578. addr = sg_page_iter_dma_address(&iter);
  579. for (;;) {
  580. start += PAGE_SIZE;
  581. if (start > end)
  582. break;
  583. addr += PAGE_SIZE;
  584. if (!__sg_page_iter_dma_next(&iter) ||
  585. sg_page_iter_dma_address(&iter) != addr)
  586. return start - ofs;
  587. }
  588. /* ok, all on continuous pages */
  589. return size;
  590. }
  591. static const struct snd_malloc_ops snd_dma_noncontig_ops = {
  592. .alloc = snd_dma_noncontig_alloc,
  593. .free = snd_dma_noncontig_free,
  594. .mmap = snd_dma_noncontig_mmap,
  595. .sync = snd_dma_noncontig_sync,
  596. .get_addr = snd_dma_noncontig_get_addr,
  597. .get_page = snd_dma_noncontig_get_page,
  598. .get_chunk_size = snd_dma_noncontig_get_chunk_size,
  599. };
  600. #ifdef CONFIG_SND_DMA_SGBUF
  601. /* Fallback SG-buffer allocations for x86 */
  602. struct snd_dma_sg_fallback {
  603. struct sg_table sgt; /* used by get_addr - must be the first item */
  604. size_t count;
  605. struct page **pages;
  606. unsigned int *npages;
  607. };
  608. static void __snd_dma_sg_fallback_free(struct snd_dma_buffer *dmab,
  609. struct snd_dma_sg_fallback *sgbuf)
  610. {
  611. bool wc = dmab->dev.type == SNDRV_DMA_TYPE_DEV_WC_SG;
  612. size_t i, size;
  613. if (sgbuf->pages && sgbuf->npages) {
  614. i = 0;
  615. while (i < sgbuf->count) {
  616. size = sgbuf->npages[i];
  617. if (!size)
  618. break;
  619. do_free_pages(page_address(sgbuf->pages[i]),
  620. size << PAGE_SHIFT, wc);
  621. i += size;
  622. }
  623. }
  624. kvfree(sgbuf->pages);
  625. kvfree(sgbuf->npages);
  626. kfree(sgbuf);
  627. }
  628. /* fallback manual S/G buffer allocations */
  629. static void *snd_dma_sg_fallback_alloc(struct snd_dma_buffer *dmab, size_t size)
  630. {
  631. bool wc = dmab->dev.type == SNDRV_DMA_TYPE_DEV_WC_SG;
  632. struct snd_dma_sg_fallback *sgbuf;
  633. struct page **pagep, *curp;
  634. size_t chunk;
  635. dma_addr_t addr;
  636. unsigned int idx, npages;
  637. void *p;
  638. sgbuf = kzalloc(sizeof(*sgbuf), GFP_KERNEL);
  639. if (!sgbuf)
  640. return NULL;
  641. size = PAGE_ALIGN(size);
  642. sgbuf->count = size >> PAGE_SHIFT;
  643. sgbuf->pages = kvcalloc(sgbuf->count, sizeof(*sgbuf->pages), GFP_KERNEL);
  644. sgbuf->npages = kvcalloc(sgbuf->count, sizeof(*sgbuf->npages), GFP_KERNEL);
  645. if (!sgbuf->pages || !sgbuf->npages)
  646. goto error;
  647. pagep = sgbuf->pages;
  648. chunk = size;
  649. idx = 0;
  650. while (size > 0) {
  651. chunk = min(size, chunk);
  652. p = do_alloc_pages(dmab->dev.dev, chunk, &addr, wc);
  653. if (!p) {
  654. if (chunk <= PAGE_SIZE)
  655. goto error;
  656. chunk >>= 1;
  657. chunk = PAGE_SIZE << get_order(chunk);
  658. continue;
  659. }
  660. size -= chunk;
  661. /* fill pages */
  662. npages = chunk >> PAGE_SHIFT;
  663. sgbuf->npages[idx] = npages;
  664. idx += npages;
  665. curp = virt_to_page(p);
  666. while (npages--)
  667. *pagep++ = curp++;
  668. }
  669. if (sg_alloc_table_from_pages(&sgbuf->sgt, sgbuf->pages, sgbuf->count,
  670. 0, sgbuf->count << PAGE_SHIFT, GFP_KERNEL))
  671. goto error;
  672. if (dma_map_sgtable(dmab->dev.dev, &sgbuf->sgt, DMA_BIDIRECTIONAL, 0))
  673. goto error_dma_map;
  674. p = vmap(sgbuf->pages, sgbuf->count, VM_MAP, PAGE_KERNEL);
  675. if (!p)
  676. goto error_vmap;
  677. dmab->private_data = sgbuf;
  678. /* store the first page address for convenience */
  679. dmab->addr = snd_sgbuf_get_addr(dmab, 0);
  680. return p;
  681. error_vmap:
  682. dma_unmap_sgtable(dmab->dev.dev, &sgbuf->sgt, DMA_BIDIRECTIONAL, 0);
  683. error_dma_map:
  684. sg_free_table(&sgbuf->sgt);
  685. error:
  686. __snd_dma_sg_fallback_free(dmab, sgbuf);
  687. return NULL;
  688. }
  689. static void snd_dma_sg_fallback_free(struct snd_dma_buffer *dmab)
  690. {
  691. struct snd_dma_sg_fallback *sgbuf = dmab->private_data;
  692. vunmap(dmab->area);
  693. dma_unmap_sgtable(dmab->dev.dev, &sgbuf->sgt, DMA_BIDIRECTIONAL, 0);
  694. sg_free_table(&sgbuf->sgt);
  695. __snd_dma_sg_fallback_free(dmab, dmab->private_data);
  696. }
  697. static int snd_dma_sg_fallback_mmap(struct snd_dma_buffer *dmab,
  698. struct vm_area_struct *area)
  699. {
  700. struct snd_dma_sg_fallback *sgbuf = dmab->private_data;
  701. if (dmab->dev.type == SNDRV_DMA_TYPE_DEV_WC_SG)
  702. area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
  703. return vm_map_pages(area, sgbuf->pages, sgbuf->count);
  704. }
  705. static void *snd_dma_sg_alloc(struct snd_dma_buffer *dmab, size_t size)
  706. {
  707. int type = dmab->dev.type;
  708. void *p;
  709. /* try the standard DMA API allocation at first */
  710. if (type == SNDRV_DMA_TYPE_DEV_WC_SG)
  711. dmab->dev.type = SNDRV_DMA_TYPE_DEV_WC;
  712. else
  713. dmab->dev.type = SNDRV_DMA_TYPE_DEV;
  714. p = __snd_dma_alloc_pages(dmab, size);
  715. if (p)
  716. return p;
  717. dmab->dev.type = type; /* restore the type */
  718. return snd_dma_sg_fallback_alloc(dmab, size);
  719. }
  720. static const struct snd_malloc_ops snd_dma_sg_ops = {
  721. .alloc = snd_dma_sg_alloc,
  722. .free = snd_dma_sg_fallback_free,
  723. .mmap = snd_dma_sg_fallback_mmap,
  724. /* reuse noncontig helper */
  725. .get_addr = snd_dma_noncontig_get_addr,
  726. /* reuse vmalloc helpers */
  727. .get_page = snd_dma_vmalloc_get_page,
  728. .get_chunk_size = snd_dma_vmalloc_get_chunk_size,
  729. };
  730. #endif /* CONFIG_SND_DMA_SGBUF */
  731. /*
  732. * Non-coherent pages allocator
  733. */
  734. static void *snd_dma_noncoherent_alloc(struct snd_dma_buffer *dmab, size_t size)
  735. {
  736. void *p;
  737. p = dma_alloc_noncoherent(dmab->dev.dev, size, &dmab->addr,
  738. dmab->dev.dir, DEFAULT_GFP);
  739. if (p)
  740. dmab->dev.need_sync = dma_need_sync(dmab->dev.dev, dmab->addr);
  741. return p;
  742. }
  743. static void snd_dma_noncoherent_free(struct snd_dma_buffer *dmab)
  744. {
  745. dma_free_noncoherent(dmab->dev.dev, dmab->bytes, dmab->area,
  746. dmab->addr, dmab->dev.dir);
  747. }
  748. static int snd_dma_noncoherent_mmap(struct snd_dma_buffer *dmab,
  749. struct vm_area_struct *area)
  750. {
  751. area->vm_page_prot = vm_get_page_prot(area->vm_flags);
  752. return dma_mmap_pages(dmab->dev.dev, area,
  753. area->vm_end - area->vm_start,
  754. virt_to_page(dmab->area));
  755. }
  756. static void snd_dma_noncoherent_sync(struct snd_dma_buffer *dmab,
  757. enum snd_dma_sync_mode mode)
  758. {
  759. if (mode == SNDRV_DMA_SYNC_CPU) {
  760. if (dmab->dev.dir != DMA_TO_DEVICE)
  761. dma_sync_single_for_cpu(dmab->dev.dev, dmab->addr,
  762. dmab->bytes, dmab->dev.dir);
  763. } else {
  764. if (dmab->dev.dir != DMA_FROM_DEVICE)
  765. dma_sync_single_for_device(dmab->dev.dev, dmab->addr,
  766. dmab->bytes, dmab->dev.dir);
  767. }
  768. }
  769. static const struct snd_malloc_ops snd_dma_noncoherent_ops = {
  770. .alloc = snd_dma_noncoherent_alloc,
  771. .free = snd_dma_noncoherent_free,
  772. .mmap = snd_dma_noncoherent_mmap,
  773. .sync = snd_dma_noncoherent_sync,
  774. };
  775. #endif /* CONFIG_HAS_DMA */
  776. /*
  777. * Entry points
  778. */
  779. static const struct snd_malloc_ops *snd_dma_ops[] = {
  780. [SNDRV_DMA_TYPE_CONTINUOUS] = &snd_dma_continuous_ops,
  781. [SNDRV_DMA_TYPE_VMALLOC] = &snd_dma_vmalloc_ops,
  782. #ifdef CONFIG_HAS_DMA
  783. [SNDRV_DMA_TYPE_DEV] = &snd_dma_dev_ops,
  784. [SNDRV_DMA_TYPE_DEV_WC] = &snd_dma_wc_ops,
  785. [SNDRV_DMA_TYPE_NONCONTIG] = &snd_dma_noncontig_ops,
  786. [SNDRV_DMA_TYPE_NONCOHERENT] = &snd_dma_noncoherent_ops,
  787. #ifdef CONFIG_SND_DMA_SGBUF
  788. [SNDRV_DMA_TYPE_DEV_SG] = &snd_dma_sg_ops,
  789. [SNDRV_DMA_TYPE_DEV_WC_SG] = &snd_dma_sg_ops,
  790. #endif
  791. #ifdef CONFIG_GENERIC_ALLOCATOR
  792. [SNDRV_DMA_TYPE_DEV_IRAM] = &snd_dma_iram_ops,
  793. #endif /* CONFIG_GENERIC_ALLOCATOR */
  794. #endif /* CONFIG_HAS_DMA */
  795. };
  796. static const struct snd_malloc_ops *snd_dma_get_ops(struct snd_dma_buffer *dmab)
  797. {
  798. if (WARN_ON_ONCE(!dmab))
  799. return NULL;
  800. if (WARN_ON_ONCE(dmab->dev.type <= SNDRV_DMA_TYPE_UNKNOWN ||
  801. dmab->dev.type >= ARRAY_SIZE(snd_dma_ops)))
  802. return NULL;
  803. return snd_dma_ops[dmab->dev.type];
  804. }