Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Scatter-Gather buffer |
| 3 | * |
| 4 | * Copyright (c) by Takashi Iwai <tiwai@suse.de> |
| 5 | * |
| 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. |
| 10 | * |
| 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. |
| 15 | * |
| 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 |
| 19 | * |
| 20 | */ |
| 21 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/slab.h> |
| 23 | #include <linux/mm.h> |
| 24 | #include <linux/vmalloc.h> |
| 25 | #include <sound/memalloc.h> |
| 26 | |
| 27 | |
| 28 | /* table entries are align to 32 */ |
| 29 | #define SGBUF_TBL_ALIGN 32 |
Clemens Ladisch | 7ab3992 | 2006-10-09 08:13:32 +0200 | [diff] [blame] | 30 | #define sgbuf_align_table(tbl) ALIGN((tbl), SGBUF_TBL_ALIGN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
| 32 | int snd_free_sgbuf_pages(struct snd_dma_buffer *dmab) |
| 33 | { |
| 34 | struct snd_sg_buf *sgbuf = dmab->private_data; |
| 35 | struct snd_dma_buffer tmpb; |
| 36 | int i; |
| 37 | |
| 38 | if (! sgbuf) |
| 39 | return -EINVAL; |
| 40 | |
| 41 | tmpb.dev.type = SNDRV_DMA_TYPE_DEV; |
| 42 | tmpb.dev.dev = sgbuf->dev; |
| 43 | for (i = 0; i < sgbuf->pages; i++) { |
Takashi Iwai | 51e9f2e | 2008-07-30 15:13:33 +0200 | [diff] [blame] | 44 | if (!(sgbuf->table[i].addr & ~PAGE_MASK)) |
| 45 | continue; /* continuous pages */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | tmpb.area = sgbuf->table[i].buf; |
Takashi Iwai | 51e9f2e | 2008-07-30 15:13:33 +0200 | [diff] [blame] | 47 | tmpb.addr = sgbuf->table[i].addr & PAGE_MASK; |
| 48 | tmpb.bytes = (sgbuf->table[i].addr & ~PAGE_MASK) << PAGE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | snd_dma_free_pages(&tmpb); |
| 50 | } |
| 51 | if (dmab->area) |
| 52 | vunmap(dmab->area); |
| 53 | dmab->area = NULL; |
| 54 | |
| 55 | kfree(sgbuf->table); |
| 56 | kfree(sgbuf->page_table); |
| 57 | kfree(sgbuf); |
| 58 | dmab->private_data = NULL; |
| 59 | |
| 60 | return 0; |
| 61 | } |
| 62 | |
Takashi Iwai | 51e9f2e | 2008-07-30 15:13:33 +0200 | [diff] [blame] | 63 | #define MAX_ALLOC_PAGES 32 |
| 64 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | void *snd_malloc_sgbuf_pages(struct device *device, |
| 66 | size_t size, struct snd_dma_buffer *dmab, |
| 67 | size_t *res_size) |
| 68 | { |
| 69 | struct snd_sg_buf *sgbuf; |
Takashi Iwai | 51e9f2e | 2008-07-30 15:13:33 +0200 | [diff] [blame] | 70 | unsigned int i, pages, chunk, maxpages; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | struct snd_dma_buffer tmpb; |
Takashi Iwai | 51e9f2e | 2008-07-30 15:13:33 +0200 | [diff] [blame] | 72 | struct snd_sg_page *table; |
| 73 | struct page **pgtable; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | |
| 75 | dmab->area = NULL; |
| 76 | dmab->addr = 0; |
Panagiotis Issaris | 59feddb | 2006-07-25 15:28:03 +0200 | [diff] [blame] | 77 | dmab->private_data = sgbuf = kzalloc(sizeof(*sgbuf), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | if (! sgbuf) |
| 79 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | sgbuf->dev = device; |
| 81 | pages = snd_sgbuf_aligned_pages(size); |
| 82 | sgbuf->tblsize = sgbuf_align_table(pages); |
Takashi Iwai | 51e9f2e | 2008-07-30 15:13:33 +0200 | [diff] [blame] | 83 | table = kcalloc(sgbuf->tblsize, sizeof(*table), GFP_KERNEL); |
| 84 | if (!table) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | goto _failed; |
Takashi Iwai | 51e9f2e | 2008-07-30 15:13:33 +0200 | [diff] [blame] | 86 | sgbuf->table = table; |
| 87 | pgtable = kcalloc(sgbuf->tblsize, sizeof(*pgtable), GFP_KERNEL); |
| 88 | if (!pgtable) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | goto _failed; |
Takashi Iwai | 51e9f2e | 2008-07-30 15:13:33 +0200 | [diff] [blame] | 90 | sgbuf->page_table = pgtable; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | |
Takashi Iwai | 51e9f2e | 2008-07-30 15:13:33 +0200 | [diff] [blame] | 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) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | goto _failed; |
Takashi Iwai | 51e9f2e | 2008-07-30 15:13:33 +0200 | [diff] [blame] | 106 | size = sgbuf->pages * PAGE_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | break; |
| 108 | } |
Takashi Iwai | 51e9f2e | 2008-07-30 15:13:33 +0200 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | sgbuf->size = size; |
| 127 | dmab->area = vmap(sgbuf->page_table, sgbuf->pages, VM_MAP, PAGE_KERNEL); |
| 128 | if (! dmab->area) |
| 129 | goto _failed; |
Takashi Iwai | 51e9f2e | 2008-07-30 15:13:33 +0200 | [diff] [blame] | 130 | if (res_size) |
| 131 | *res_size = sgbuf->size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | return dmab->area; |
| 133 | |
| 134 | _failed: |
| 135 | snd_free_sgbuf_pages(dmab); /* free the table */ |
| 136 | return NULL; |
| 137 | } |