blob: e8bdb860a19fc03278e928a8621677eea801f7a1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
3 * GUS's memory allocation routines / bottom layer
4 *
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
22#include <sound/driver.h>
23#include <linux/slab.h>
Paulo Marques543537b2005-06-23 00:09:02 -070024#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <sound/core.h>
26#include <sound/gus.h>
27#include <sound/info.h>
28
29#ifdef CONFIG_SND_DEBUG
Takashi Iwai5e2da202005-11-17 14:36:44 +010030static void snd_gf1_mem_info_read(struct snd_info_entry *entry,
31 struct snd_info_buffer *buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#endif
33
Takashi Iwai5e2da202005-11-17 14:36:44 +010034void snd_gf1_mem_lock(struct snd_gf1_mem * alloc, int xup)
Linus Torvalds1da177e2005-04-16 15:20:36 -070035{
36 if (!xup) {
37 down(&alloc->memory_mutex);
38 } else {
39 up(&alloc->memory_mutex);
40 }
41}
42
Takashi Iwai5e2da202005-11-17 14:36:44 +010043static struct snd_gf1_mem_block *snd_gf1_mem_xalloc(struct snd_gf1_mem * alloc,
44 struct snd_gf1_mem_block * block)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
Takashi Iwai5e2da202005-11-17 14:36:44 +010046 struct snd_gf1_mem_block *pblock, *nblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Takashi Iwai5e2da202005-11-17 14:36:44 +010048 nblock = kmalloc(sizeof(struct snd_gf1_mem_block), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 if (nblock == NULL)
50 return NULL;
51 *nblock = *block;
52 pblock = alloc->first;
53 while (pblock) {
54 if (pblock->ptr > nblock->ptr) {
55 nblock->prev = pblock->prev;
56 nblock->next = pblock;
57 pblock->prev = nblock;
58 if (pblock == alloc->first)
59 alloc->first = nblock;
60 else
61 nblock->prev->next = nblock;
62 up(&alloc->memory_mutex);
63 return NULL;
64 }
65 pblock = pblock->next;
66 }
67 nblock->next = NULL;
68 if (alloc->last == NULL) {
69 nblock->prev = NULL;
70 alloc->first = alloc->last = nblock;
71 } else {
72 nblock->prev = alloc->last;
73 alloc->last->next = nblock;
74 alloc->last = nblock;
75 }
76 return nblock;
77}
78
Takashi Iwai5e2da202005-11-17 14:36:44 +010079int snd_gf1_mem_xfree(struct snd_gf1_mem * alloc, struct snd_gf1_mem_block * block)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
81 if (block->share) { /* ok.. shared block */
82 block->share--;
83 up(&alloc->memory_mutex);
84 return 0;
85 }
86 if (alloc->first == block) {
87 alloc->first = block->next;
88 if (block->next)
89 block->next->prev = NULL;
90 } else {
91 block->prev->next = block->next;
92 if (block->next)
93 block->next->prev = block->prev;
94 }
95 if (alloc->last == block) {
96 alloc->last = block->prev;
97 if (block->prev)
98 block->prev->next = NULL;
99 } else {
100 block->next->prev = block->prev;
101 if (block->prev)
102 block->prev->next = block->next;
103 }
104 kfree(block->name);
105 kfree(block);
106 return 0;
107}
108
Takashi Iwai5e2da202005-11-17 14:36:44 +0100109static struct snd_gf1_mem_block *snd_gf1_mem_look(struct snd_gf1_mem * alloc,
Adrian Bunk209ac852005-05-23 10:29:53 +0200110 unsigned int address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
Takashi Iwai5e2da202005-11-17 14:36:44 +0100112 struct snd_gf1_mem_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114 for (block = alloc->first; block; block = block->next) {
115 if (block->ptr == address) {
116 return block;
117 }
118 }
119 return NULL;
120}
121
Takashi Iwai5e2da202005-11-17 14:36:44 +0100122static struct snd_gf1_mem_block *snd_gf1_mem_share(struct snd_gf1_mem * alloc,
Adrian Bunk209ac852005-05-23 10:29:53 +0200123 unsigned int *share_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
Takashi Iwai5e2da202005-11-17 14:36:44 +0100125 struct snd_gf1_mem_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127 if (!share_id[0] && !share_id[1] &&
128 !share_id[2] && !share_id[3])
129 return NULL;
130 for (block = alloc->first; block; block = block->next)
131 if (!memcmp(share_id, block->share_id, sizeof(share_id)))
132 return block;
133 return NULL;
134}
135
Takashi Iwai5e2da202005-11-17 14:36:44 +0100136static int snd_gf1_mem_find(struct snd_gf1_mem * alloc,
137 struct snd_gf1_mem_block * block,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 unsigned int size, int w_16, int align)
139{
Takashi Iwai5e2da202005-11-17 14:36:44 +0100140 struct snd_gf1_bank_info *info = w_16 ? alloc->banks_16 : alloc->banks_8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 unsigned int idx, boundary;
142 int size1;
Takashi Iwai5e2da202005-11-17 14:36:44 +0100143 struct snd_gf1_mem_block *pblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 unsigned int ptr1, ptr2;
145
146 align--;
147 if (w_16 && align < 1)
148 align = 1;
149 block->flags = w_16 ? SNDRV_GF1_MEM_BLOCK_16BIT : 0;
150 block->owner = SNDRV_GF1_MEM_OWNER_DRIVER;
151 block->share = 0;
152 block->share_id[0] = block->share_id[1] =
153 block->share_id[2] = block->share_id[3] = 0;
154 block->name = NULL;
155 block->prev = block->next = NULL;
156 for (pblock = alloc->first, idx = 0; pblock; pblock = pblock->next) {
157 while (pblock->ptr >= (boundary = info[idx].address + info[idx].size))
158 idx++;
159 while (pblock->ptr + pblock->size >= (boundary = info[idx].address + info[idx].size))
160 idx++;
161 ptr2 = boundary;
162 if (pblock->next) {
163 if (pblock->ptr + pblock->size == pblock->next->ptr)
164 continue;
165 if (pblock->next->ptr < boundary)
166 ptr2 = pblock->next->ptr;
167 }
168 ptr1 = (pblock->ptr + pblock->size + align) & ~align;
169 if (ptr1 >= ptr2)
170 continue;
171 size1 = ptr2 - ptr1;
172 if ((int)size <= size1) {
173 block->ptr = ptr1;
174 block->size = size;
175 return 0;
176 }
177 }
178 while (++idx < 4) {
179 if (size <= info[idx].size) {
180 /* I assume that bank address is already aligned.. */
181 block->ptr = info[idx].address;
182 block->size = size;
183 return 0;
184 }
185 }
186 return -ENOMEM;
187}
188
Takashi Iwai5e2da202005-11-17 14:36:44 +0100189struct snd_gf1_mem_block *snd_gf1_mem_alloc(struct snd_gf1_mem * alloc, int owner,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 char *name, int size, int w_16, int align,
191 unsigned int *share_id)
192{
Takashi Iwai5e2da202005-11-17 14:36:44 +0100193 struct snd_gf1_mem_block block, *nblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195 snd_gf1_mem_lock(alloc, 0);
196 if (share_id != NULL) {
197 nblock = snd_gf1_mem_share(alloc, share_id);
198 if (nblock != NULL) {
199 if (size != (int)nblock->size) {
200 /* TODO: remove in the future */
Takashi Iwai99b359b2005-10-20 18:26:44 +0200201 snd_printk(KERN_ERR "snd_gf1_mem_alloc - share: sizes differ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 goto __std;
203 }
204 nblock->share++;
205 snd_gf1_mem_lock(alloc, 1);
206 return NULL;
207 }
208 }
209 __std:
210 if (snd_gf1_mem_find(alloc, &block, size, w_16, align) < 0) {
211 snd_gf1_mem_lock(alloc, 1);
212 return NULL;
213 }
214 if (share_id != NULL)
215 memcpy(&block.share_id, share_id, sizeof(block.share_id));
216 block.owner = owner;
Paulo Marques543537b2005-06-23 00:09:02 -0700217 block.name = kstrdup(name, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 nblock = snd_gf1_mem_xalloc(alloc, &block);
219 snd_gf1_mem_lock(alloc, 1);
220 return nblock;
221}
222
Takashi Iwai5e2da202005-11-17 14:36:44 +0100223int snd_gf1_mem_free(struct snd_gf1_mem * alloc, unsigned int address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
225 int result;
Takashi Iwai5e2da202005-11-17 14:36:44 +0100226 struct snd_gf1_mem_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 snd_gf1_mem_lock(alloc, 0);
229 if ((block = snd_gf1_mem_look(alloc, address)) != NULL) {
230 result = snd_gf1_mem_xfree(alloc, block);
231 snd_gf1_mem_lock(alloc, 1);
232 return result;
233 }
234 snd_gf1_mem_lock(alloc, 1);
235 return -EINVAL;
236}
237
Takashi Iwai5e2da202005-11-17 14:36:44 +0100238int snd_gf1_mem_init(struct snd_gus_card * gus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
Takashi Iwai5e2da202005-11-17 14:36:44 +0100240 struct snd_gf1_mem *alloc;
241 struct snd_gf1_mem_block block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242#ifdef CONFIG_SND_DEBUG
Takashi Iwai5e2da202005-11-17 14:36:44 +0100243 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244#endif
245
246 alloc = &gus->gf1.mem_alloc;
247 init_MUTEX(&alloc->memory_mutex);
248 alloc->first = alloc->last = NULL;
249 if (!gus->gf1.memory)
250 return 0;
251
252 memset(&block, 0, sizeof(block));
253 block.owner = SNDRV_GF1_MEM_OWNER_DRIVER;
254 if (gus->gf1.enh_mode) {
255 block.ptr = 0;
256 block.size = 1024;
Paulo Marques543537b2005-06-23 00:09:02 -0700257 block.name = kstrdup("InterWave LFOs", GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 if (snd_gf1_mem_xalloc(alloc, &block) == NULL)
259 return -ENOMEM;
260 }
261 block.ptr = gus->gf1.default_voice_address;
262 block.size = 4;
Paulo Marques543537b2005-06-23 00:09:02 -0700263 block.name = kstrdup("Voice default (NULL's)", GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 if (snd_gf1_mem_xalloc(alloc, &block) == NULL)
265 return -ENOMEM;
266#ifdef CONFIG_SND_DEBUG
267 if (! snd_card_proc_new(gus->card, "gusmem", &entry)) {
268 snd_info_set_text_ops(entry, gus, 1024, snd_gf1_mem_info_read);
269 entry->c.text.read_size = 256 * 1024;
270 }
271#endif
272 return 0;
273}
274
Takashi Iwai5e2da202005-11-17 14:36:44 +0100275int snd_gf1_mem_done(struct snd_gus_card * gus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
Takashi Iwai5e2da202005-11-17 14:36:44 +0100277 struct snd_gf1_mem *alloc;
278 struct snd_gf1_mem_block *block, *nblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280 alloc = &gus->gf1.mem_alloc;
281 block = alloc->first;
282 while (block) {
283 nblock = block->next;
284 snd_gf1_mem_xfree(alloc, block);
285 block = nblock;
286 }
287 return 0;
288}
289
290#ifdef CONFIG_SND_DEBUG
Takashi Iwai5e2da202005-11-17 14:36:44 +0100291static void snd_gf1_mem_info_read(struct snd_info_entry *entry,
292 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
Takashi Iwai5e2da202005-11-17 14:36:44 +0100294 struct snd_gus_card *gus;
295 struct snd_gf1_mem *alloc;
296 struct snd_gf1_mem_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 unsigned int total, used;
298 int i;
299
300 gus = entry->private_data;
301 alloc = &gus->gf1.mem_alloc;
302 down(&alloc->memory_mutex);
303 snd_iprintf(buffer, "8-bit banks : \n ");
304 for (i = 0; i < 4; i++)
305 snd_iprintf(buffer, "0x%06x (%04ik)%s", alloc->banks_8[i].address, alloc->banks_8[i].size >> 10, i + 1 < 4 ? "," : "");
306 snd_iprintf(buffer, "\n"
307 "16-bit banks : \n ");
308 for (i = total = 0; i < 4; i++) {
309 snd_iprintf(buffer, "0x%06x (%04ik)%s", alloc->banks_16[i].address, alloc->banks_16[i].size >> 10, i + 1 < 4 ? "," : "");
310 total += alloc->banks_16[i].size;
311 }
312 snd_iprintf(buffer, "\n");
313 used = 0;
314 for (block = alloc->first, i = 0; block; block = block->next, i++) {
315 used += block->size;
316 snd_iprintf(buffer, "Block %i at 0x%lx onboard 0x%x size %i (0x%x):\n", i, (long) block, block->ptr, block->size, block->size);
317 if (block->share ||
318 block->share_id[0] || block->share_id[1] ||
319 block->share_id[2] || block->share_id[3])
320 snd_iprintf(buffer, " Share : %i [id0 0x%x] [id1 0x%x] [id2 0x%x] [id3 0x%x]\n",
321 block->share,
322 block->share_id[0], block->share_id[1],
323 block->share_id[2], block->share_id[3]);
324 snd_iprintf(buffer, " Flags :%s\n",
325 block->flags & SNDRV_GF1_MEM_BLOCK_16BIT ? " 16-bit" : "");
326 snd_iprintf(buffer, " Owner : ");
327 switch (block->owner) {
328 case SNDRV_GF1_MEM_OWNER_DRIVER:
329 snd_iprintf(buffer, "driver - %s\n", block->name);
330 break;
331 case SNDRV_GF1_MEM_OWNER_WAVE_SIMPLE:
332 snd_iprintf(buffer, "SIMPLE wave\n");
333 break;
334 case SNDRV_GF1_MEM_OWNER_WAVE_GF1:
335 snd_iprintf(buffer, "GF1 wave\n");
336 break;
337 case SNDRV_GF1_MEM_OWNER_WAVE_IWFFFF:
338 snd_iprintf(buffer, "IWFFFF wave\n");
339 break;
340 default:
341 snd_iprintf(buffer, "unknown\n");
342 }
343 }
344 snd_iprintf(buffer, " Total: memory = %i, used = %i, free = %i\n",
345 total, used, total - used);
346 up(&alloc->memory_mutex);
347#if 0
348 ultra_iprintf(buffer, " Verify: free = %i, max 8-bit block = %i, max 16-bit block = %i\n",
349 ultra_memory_free_size(card, &card->gf1.mem_alloc),
350 ultra_memory_free_block(card, &card->gf1.mem_alloc, 0),
351 ultra_memory_free_block(card, &card->gf1.mem_alloc, 1));
352#endif
353}
354#endif