2 * Scatter-Gather buffer
4 * Copyright (c) by Takashi Iwai <tiwai@suse.de>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <linux/config.h>
23 #include <linux/slab.h>
25 #include <linux/vmalloc.h>
26 #include <sound/memalloc.h>
29 /* table entries are align to 32 */
30 #define SGBUF_TBL_ALIGN 32
31 #define sgbuf_align_table(tbl) ((((tbl) + SGBUF_TBL_ALIGN - 1) / SGBUF_TBL_ALIGN) * SGBUF_TBL_ALIGN)
33 int snd_free_sgbuf_pages(struct snd_dma_buffer *dmab)
35 struct snd_sg_buf *sgbuf = dmab->private_data;
36 struct snd_dma_buffer tmpb;
42 tmpb.dev.type = SNDRV_DMA_TYPE_DEV;
43 tmpb.dev.dev = sgbuf->dev;
44 for (i = 0; i < sgbuf->pages; i++) {
45 tmpb.area = sgbuf->table[i].buf;
46 tmpb.addr = sgbuf->table[i].addr;
47 tmpb.bytes = PAGE_SIZE;
48 snd_dma_free_pages(&tmpb);
56 if (sgbuf->page_table)
57 kfree(sgbuf->page_table);
59 dmab->private_data = NULL;
64 void *snd_malloc_sgbuf_pages(struct device *device,
65 size_t size, struct snd_dma_buffer *dmab,
68 struct snd_sg_buf *sgbuf;
69 unsigned int i, pages;
70 struct snd_dma_buffer tmpb;
74 dmab->private_data = sgbuf = kmalloc(sizeof(*sgbuf), GFP_KERNEL);
77 memset(sgbuf, 0, sizeof(*sgbuf));
79 pages = snd_sgbuf_aligned_pages(size);
80 sgbuf->tblsize = sgbuf_align_table(pages);
81 sgbuf->table = kmalloc(sizeof(*sgbuf->table) * sgbuf->tblsize, GFP_KERNEL);
84 memset(sgbuf->table, 0, sizeof(*sgbuf->table) * sgbuf->tblsize);
85 sgbuf->page_table = kmalloc(sizeof(*sgbuf->page_table) * sgbuf->tblsize, GFP_KERNEL);
86 if (! sgbuf->page_table)
88 memset(sgbuf->page_table, 0, sizeof(*sgbuf->page_table) * sgbuf->tblsize);
90 /* allocate each page */
91 for (i = 0; i < pages; i++) {
92 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, device, PAGE_SIZE, &tmpb) < 0) {
95 *res_size = size = sgbuf->pages * PAGE_SIZE;
98 sgbuf->table[i].buf = tmpb.area;
99 sgbuf->table[i].addr = tmpb.addr;
100 sgbuf->page_table[i] = virt_to_page(tmpb.area);
105 dmab->area = vmap(sgbuf->page_table, sgbuf->pages, VM_MAP, PAGE_KERNEL);
111 snd_free_sgbuf_pages(dmab); /* free the table */