1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Scatter-Gather buffer
5 * Copyright (c) by Takashi Iwai <tiwai@suse.de>
8 #include <linux/slab.h>
10 #include <linux/vmalloc.h>
11 #include <linux/export.h>
12 #include <asm/pgtable.h>
13 #include <sound/memalloc.h>
16 /* table entries are align to 32 */
17 #define SGBUF_TBL_ALIGN 32
18 #define sgbuf_align_table(tbl) ALIGN((tbl), SGBUF_TBL_ALIGN)
20 int snd_free_sgbuf_pages(struct snd_dma_buffer *dmab)
22 struct snd_sg_buf *sgbuf = dmab->private_data;
23 struct snd_dma_buffer tmpb;
32 tmpb.dev.type = SNDRV_DMA_TYPE_DEV;
33 if (dmab->dev.type == SNDRV_DMA_TYPE_DEV_UC_SG)
34 tmpb.dev.type = SNDRV_DMA_TYPE_DEV_UC;
35 tmpb.dev.dev = sgbuf->dev;
36 for (i = 0; i < sgbuf->pages; i++) {
37 if (!(sgbuf->table[i].addr & ~PAGE_MASK))
38 continue; /* continuous pages */
39 tmpb.area = sgbuf->table[i].buf;
40 tmpb.addr = sgbuf->table[i].addr & PAGE_MASK;
41 tmpb.bytes = (sgbuf->table[i].addr & ~PAGE_MASK) << PAGE_SHIFT;
42 snd_dma_free_pages(&tmpb);
46 kfree(sgbuf->page_table);
48 dmab->private_data = NULL;
53 #define MAX_ALLOC_PAGES 32
55 void *snd_malloc_sgbuf_pages(struct device *device,
56 size_t size, struct snd_dma_buffer *dmab,
59 struct snd_sg_buf *sgbuf;
60 unsigned int i, pages, chunk, maxpages;
61 struct snd_dma_buffer tmpb;
62 struct snd_sg_page *table;
63 struct page **pgtable;
64 int type = SNDRV_DMA_TYPE_DEV;
65 pgprot_t prot = PAGE_KERNEL;
69 dmab->private_data = sgbuf = kzalloc(sizeof(*sgbuf), GFP_KERNEL);
72 if (dmab->dev.type == SNDRV_DMA_TYPE_DEV_UC_SG) {
73 type = SNDRV_DMA_TYPE_DEV_UC;
74 #ifdef pgprot_noncached
75 prot = pgprot_noncached(PAGE_KERNEL);
79 pages = snd_sgbuf_aligned_pages(size);
80 sgbuf->tblsize = sgbuf_align_table(pages);
81 table = kcalloc(sgbuf->tblsize, sizeof(*table), GFP_KERNEL);
85 pgtable = kcalloc(sgbuf->tblsize, sizeof(*pgtable), GFP_KERNEL);
88 sgbuf->page_table = pgtable;
91 maxpages = MAX_ALLOC_PAGES;
94 /* don't be too eager to take a huge chunk */
98 if (snd_dma_alloc_pages_fallback(type, device,
104 size = sgbuf->pages * PAGE_SIZE;
107 chunk = tmpb.bytes >> PAGE_SHIFT;
108 for (i = 0; i < chunk; i++) {
109 table->buf = tmpb.area;
110 table->addr = tmpb.addr;
112 table->addr |= chunk; /* mark head */
114 *pgtable++ = virt_to_page(tmpb.area);
115 tmpb.area += PAGE_SIZE;
116 tmpb.addr += PAGE_SIZE;
118 sgbuf->pages += chunk;
120 if (chunk < maxpages)
125 dmab->area = vmap(sgbuf->page_table, sgbuf->pages, VM_MAP, prot);
129 *res_size = sgbuf->size;
133 snd_free_sgbuf_pages(dmab); /* free the table */
138 * compute the max chunk size with continuous pages on sg-buffer
140 unsigned int snd_sgbuf_get_chunk_size(struct snd_dma_buffer *dmab,
141 unsigned int ofs, unsigned int size)
143 struct snd_sg_buf *sg = dmab->private_data;
144 unsigned int start, end, pg;
146 start = ofs >> PAGE_SHIFT;
147 end = (ofs + size - 1) >> PAGE_SHIFT;
148 /* check page continuity */
149 pg = sg->table[start].addr >> PAGE_SHIFT;
155 if ((sg->table[start].addr >> PAGE_SHIFT) != pg)
156 return (start << PAGE_SHIFT) - ofs;
158 /* ok, all on continuous pages */
161 EXPORT_SYMBOL(snd_sgbuf_get_chunk_size);