Skip to content

Commit 51e9f2e

Browse files
tiwaiperexg
authored andcommitted
ALSA: Allocate larger pages in sgbuf
Most hardwares have limited buffer-descriptor table length. This also restricts the max buffer size of the sound driver. For example, snd-hda-intel has 1MB buffer size limit, and this is because it can have at most 256 BDL entries. For supporting larger buffers, we need to allocate larger pages even for sg-buffers. This patch changes the sgbuf allocation code to try to allocate larger pages first. At each head of the allocated pages, the number of allocated pages is stored in the lowest bits of the corresponding entry of the table addr field. This change isn't visible as long as the driver uses snd_sgbuf_get_addr() helper. Also, the patch adds a new function, snd_pcm_sgbuf_get_chunk_size(). This returns the size of the chunk on continuous pages starting at the given position offset. If the chunk reaches to a non-continuous page, it returns the size to the boundary. Signed-off-by: Takashi Iwai <tiwai@suse.de> Signed-off-by: Jaroslav Kysela <perex@perex.cz>
1 parent 4e184f8 commit 51e9f2e

4 files changed

Lines changed: 77 additions & 18 deletions

File tree

‎include/sound/memalloc.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,9 @@ static inline unsigned int snd_sgbuf_aligned_pages(size_t size)
9797
*/
9898
static inline dma_addr_t snd_sgbuf_get_addr(struct snd_sg_buf *sgbuf, size_t offset)
9999
{
100-
return sgbuf->table[offset >> PAGE_SHIFT].addr + offset % PAGE_SIZE;
100+
dma_addr_t addr = sgbuf->table[offset >> PAGE_SHIFT].addr;
101+
addr &= PAGE_MASK;
102+
return addr + offset % PAGE_SIZE;
101103
}
102104

103105
/*

‎include/sound/pcm.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -996,7 +996,8 @@ snd_pcm_sgbuf_get_ptr(struct snd_pcm_substream *substream, unsigned int ofs)
996996

997997
struct page *snd_pcm_sgbuf_ops_page(struct snd_pcm_substream *substream,
998998
unsigned long offset);
999-
999+
unsigned int snd_pcm_sgbuf_get_chunk_size(struct snd_pcm_substream *substream,
1000+
unsigned int ofs, unsigned int size);
10001001

10011002
/* handle mmap counter - PCM mmap callback should handle this counter properly */
10021003
static inline void snd_pcm_mmap_data_open(struct vm_area_struct *area)

‎sound/core/pcm_memory.c‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,32 @@ struct page *snd_pcm_sgbuf_ops_page(struct snd_pcm_substream *substream, unsigne
324324

325325
EXPORT_SYMBOL(snd_pcm_sgbuf_ops_page);
326326

327+
/*
328+
* compute the max chunk size with continuous pages on sg-buffer
329+
*/
330+
unsigned int snd_pcm_sgbuf_get_chunk_size(struct snd_pcm_substream *substream,
331+
unsigned int ofs, unsigned int size)
332+
{
333+
struct snd_sg_buf *sg = snd_pcm_substream_sgbuf(substream);
334+
unsigned int start, end, pg;
335+
336+
start = ofs >> PAGE_SHIFT;
337+
end = (ofs + size - 1) >> PAGE_SHIFT;
338+
/* check page continuity */
339+
pg = sg->table[start].addr >> PAGE_SHIFT;
340+
for (;;) {
341+
start++;
342+
if (start > end)
343+
break;
344+
pg++;
345+
if ((sg->table[start].addr >> PAGE_SHIFT) != pg)
346+
return (start << PAGE_SHIFT) - ofs;
347+
}
348+
/* ok, all on continuous pages */
349+
return size;
350+
}
351+
EXPORT_SYMBOL(snd_pcm_sgbuf_get_chunk_size);
352+
327353
/**
328354
* snd_pcm_lib_malloc_pages - allocate the DMA buffer
329355
* @substream: the substream to allocate the DMA buffer to

‎sound/core/sgbuf.c‎

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,11 @@ int snd_free_sgbuf_pages(struct snd_dma_buffer *dmab)
4141
tmpb.dev.type = SNDRV_DMA_TYPE_DEV;
4242
tmpb.dev.dev = sgbuf->dev;
4343
for (i = 0; i < sgbuf->pages; i++) {
44+
if (!(sgbuf->table[i].addr & ~PAGE_MASK))
45+
continue; /* continuous pages */
4446
tmpb.area = sgbuf->table[i].buf;
45-
tmpb.addr = sgbuf->table[i].addr;
46-
tmpb.bytes = PAGE_SIZE;
47+
tmpb.addr = sgbuf->table[i].addr & PAGE_MASK;
48+
tmpb.bytes = (sgbuf->table[i].addr & ~PAGE_MASK) << PAGE_SHIFT;
4749
snd_dma_free_pages(&tmpb);
4850
}
4951
if (dmab->area)
@@ -58,13 +60,17 @@ int snd_free_sgbuf_pages(struct snd_dma_buffer *dmab)
5860
return 0;
5961
}
6062

63+
#define MAX_ALLOC_PAGES 32
64+
6165
void *snd_malloc_sgbuf_pages(struct device *device,
6266
size_t size, struct snd_dma_buffer *dmab,
6367
size_t *res_size)
6468
{
6569
struct snd_sg_buf *sgbuf;
66-
unsigned int i, pages;
70+
unsigned int i, pages, chunk, maxpages;
6771
struct snd_dma_buffer tmpb;
72+
struct snd_sg_page *table;
73+
struct page **pgtable;
6874

6975
dmab->area = NULL;
7076
dmab->addr = 0;
@@ -74,31 +80,55 @@ void *snd_malloc_sgbuf_pages(struct device *device,
7480
sgbuf->dev = device;
7581
pages = snd_sgbuf_aligned_pages(size);
7682
sgbuf->tblsize = sgbuf_align_table(pages);
77-
sgbuf->table = kcalloc(sgbuf->tblsize, sizeof(*sgbuf->table), GFP_KERNEL);
78-
if (! sgbuf->table)
83+
table = kcalloc(sgbuf->tblsize, sizeof(*table), GFP_KERNEL);
84+
if (!table)
7985
goto _failed;
80-
sgbuf->page_table = kcalloc(sgbuf->tblsize, sizeof(*sgbuf->page_table), GFP_KERNEL);
81-
if (! sgbuf->page_table)
86+
sgbuf->table = table;
87+
pgtable = kcalloc(sgbuf->tblsize, sizeof(*pgtable), GFP_KERNEL);
88+
if (!pgtable)
8289
goto _failed;
90+
sgbuf->page_table = pgtable;
8391

84-
/* allocate each page */
85-
for (i = 0; i < pages; i++) {
86-
if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, device, PAGE_SIZE, &tmpb) < 0) {
87-
if (res_size == NULL)
92+
/* allocate pages */
93+
maxpages = MAX_ALLOC_PAGES;
94+
while (pages > 0) {
95+
chunk = pages;
96+
/* don't be too eager to take a huge chunk */
97+
if (chunk > maxpages)
98+
chunk = maxpages;
99+
chunk <<= PAGE_SHIFT;
100+
if (snd_dma_alloc_pages_fallback(SNDRV_DMA_TYPE_DEV, device,
101+
chunk, &tmpb) < 0) {
102+
if (!sgbuf->pages)
103+
return NULL;
104+
if (!res_size)
88105
goto _failed;
89-
*res_size = size = sgbuf->pages * PAGE_SIZE;
106+
size = sgbuf->pages * PAGE_SIZE;
90107
break;
91108
}
92-
sgbuf->table[i].buf = tmpb.area;
93-
sgbuf->table[i].addr = tmpb.addr;
94-
sgbuf->page_table[i] = virt_to_page(tmpb.area);
95-
sgbuf->pages++;
109+
chunk = tmpb.bytes >> PAGE_SHIFT;
110+
for (i = 0; i < chunk; i++) {
111+
table->buf = tmpb.area;
112+
table->addr = tmpb.addr;
113+
if (!i)
114+
table->addr |= chunk; /* mark head */
115+
table++;
116+
*pgtable++ = virt_to_page(tmpb.area);
117+
tmpb.area += PAGE_SIZE;
118+
tmpb.addr += PAGE_SIZE;
119+
}
120+
sgbuf->pages += chunk;
121+
pages -= chunk;
122+
if (chunk < maxpages)
123+
maxpages = chunk;
96124
}
97125

98126
sgbuf->size = size;
99127
dmab->area = vmap(sgbuf->page_table, sgbuf->pages, VM_MAP, PAGE_KERNEL);
100128
if (! dmab->area)
101129
goto _failed;
130+
if (res_size)
131+
*res_size = sgbuf->size;
102132
return dmab->area;
103133

104134
_failed:

0 commit comments

Comments
 (0)