1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * GUS's memory allocation routines / bottom layer
7 #include <linux/slab.h>
8 #include <linux/string.h>
9 #include <sound/core.h>
10 #include <sound/gus.h>
11 #include <sound/info.h>
13 #ifdef CONFIG_SND_DEBUG
14 static void snd_gf1_mem_info_read(struct snd_info_entry *entry,
15 struct snd_info_buffer *buffer);
18 void snd_gf1_mem_lock(struct snd_gf1_mem * alloc, int xup)
21 mutex_lock(&alloc->memory_mutex);
23 mutex_unlock(&alloc->memory_mutex);
27 static struct snd_gf1_mem_block *
28 snd_gf1_mem_xalloc(struct snd_gf1_mem *alloc, struct snd_gf1_mem_block *block,
31 struct snd_gf1_mem_block *pblock, *nblock;
33 nblock = kmalloc(sizeof(struct snd_gf1_mem_block), GFP_KERNEL);
37 nblock->name = kstrdup(name, GFP_KERNEL);
43 pblock = alloc->first;
45 if (pblock->ptr > nblock->ptr) {
46 nblock->prev = pblock->prev;
47 nblock->next = pblock;
48 pblock->prev = nblock;
49 if (pblock == alloc->first)
50 alloc->first = nblock;
52 nblock->prev->next = nblock;
53 mutex_unlock(&alloc->memory_mutex);
56 pblock = pblock->next;
59 if (alloc->last == NULL) {
61 alloc->first = alloc->last = nblock;
63 nblock->prev = alloc->last;
64 alloc->last->next = nblock;
70 int snd_gf1_mem_xfree(struct snd_gf1_mem * alloc, struct snd_gf1_mem_block * block)
72 if (block->share) { /* ok.. shared block */
74 mutex_unlock(&alloc->memory_mutex);
77 if (alloc->first == block) {
78 alloc->first = block->next;
80 block->next->prev = NULL;
82 block->prev->next = block->next;
84 block->next->prev = block->prev;
86 if (alloc->last == block) {
87 alloc->last = block->prev;
89 block->prev->next = NULL;
91 block->next->prev = block->prev;
93 block->prev->next = block->next;
100 static struct snd_gf1_mem_block *snd_gf1_mem_look(struct snd_gf1_mem * alloc,
101 unsigned int address)
103 struct snd_gf1_mem_block *block;
105 for (block = alloc->first; block; block = block->next) {
106 if (block->ptr == address) {
113 static struct snd_gf1_mem_block *snd_gf1_mem_share(struct snd_gf1_mem * alloc,
114 unsigned int *share_id)
116 struct snd_gf1_mem_block *block;
118 if (!share_id[0] && !share_id[1] &&
119 !share_id[2] && !share_id[3])
121 for (block = alloc->first; block; block = block->next)
122 if (!memcmp(share_id, block->share_id,
123 sizeof(block->share_id)))
128 static int snd_gf1_mem_find(struct snd_gf1_mem * alloc,
129 struct snd_gf1_mem_block * block,
130 unsigned int size, int w_16, int align)
132 struct snd_gf1_bank_info *info = w_16 ? alloc->banks_16 : alloc->banks_8;
133 unsigned int idx, boundary;
135 struct snd_gf1_mem_block *pblock;
136 unsigned int ptr1, ptr2;
138 if (w_16 && align < 2)
140 block->flags = w_16 ? SNDRV_GF1_MEM_BLOCK_16BIT : 0;
141 block->owner = SNDRV_GF1_MEM_OWNER_DRIVER;
143 block->share_id[0] = block->share_id[1] =
144 block->share_id[2] = block->share_id[3] = 0;
146 block->prev = block->next = NULL;
147 for (pblock = alloc->first, idx = 0; pblock; pblock = pblock->next) {
148 while (pblock->ptr >= (boundary = info[idx].address + info[idx].size))
150 while (pblock->ptr + pblock->size >= (boundary = info[idx].address + info[idx].size))
154 if (pblock->ptr + pblock->size == pblock->next->ptr)
156 if (pblock->next->ptr < boundary)
157 ptr2 = pblock->next->ptr;
159 ptr1 = ALIGN(pblock->ptr + pblock->size, align);
163 if ((int)size <= size1) {
170 if (size <= info[idx].size) {
171 /* I assume that bank address is already aligned.. */
172 block->ptr = info[idx].address;
180 struct snd_gf1_mem_block *snd_gf1_mem_alloc(struct snd_gf1_mem * alloc, int owner,
181 char *name, int size, int w_16, int align,
182 unsigned int *share_id)
184 struct snd_gf1_mem_block block, *nblock;
186 snd_gf1_mem_lock(alloc, 0);
187 if (share_id != NULL) {
188 nblock = snd_gf1_mem_share(alloc, share_id);
189 if (nblock != NULL) {
190 if (size != (int)nblock->size) {
191 /* TODO: remove in the future */
192 snd_printk(KERN_ERR "snd_gf1_mem_alloc - share: sizes differ\n");
196 snd_gf1_mem_lock(alloc, 1);
201 if (snd_gf1_mem_find(alloc, &block, size, w_16, align) < 0) {
202 snd_gf1_mem_lock(alloc, 1);
205 if (share_id != NULL)
206 memcpy(&block.share_id, share_id, sizeof(block.share_id));
208 nblock = snd_gf1_mem_xalloc(alloc, &block, name);
209 snd_gf1_mem_lock(alloc, 1);
213 int snd_gf1_mem_free(struct snd_gf1_mem * alloc, unsigned int address)
216 struct snd_gf1_mem_block *block;
218 snd_gf1_mem_lock(alloc, 0);
219 block = snd_gf1_mem_look(alloc, address);
221 result = snd_gf1_mem_xfree(alloc, block);
222 snd_gf1_mem_lock(alloc, 1);
225 snd_gf1_mem_lock(alloc, 1);
229 int snd_gf1_mem_init(struct snd_gus_card * gus)
231 struct snd_gf1_mem *alloc;
232 struct snd_gf1_mem_block block;
234 alloc = &gus->gf1.mem_alloc;
235 mutex_init(&alloc->memory_mutex);
236 alloc->first = alloc->last = NULL;
237 if (!gus->gf1.memory)
240 memset(&block, 0, sizeof(block));
241 block.owner = SNDRV_GF1_MEM_OWNER_DRIVER;
242 if (gus->gf1.enh_mode) {
245 if (!snd_gf1_mem_xalloc(alloc, &block, "InterWave LFOs"))
248 block.ptr = gus->gf1.default_voice_address;
250 if (!snd_gf1_mem_xalloc(alloc, &block, "Voice default (NULL's)"))
252 #ifdef CONFIG_SND_DEBUG
253 snd_card_ro_proc_new(gus->card, "gusmem", gus, snd_gf1_mem_info_read);
258 int snd_gf1_mem_done(struct snd_gus_card * gus)
260 struct snd_gf1_mem *alloc;
261 struct snd_gf1_mem_block *block, *nblock;
263 alloc = &gus->gf1.mem_alloc;
264 block = alloc->first;
266 nblock = block->next;
267 snd_gf1_mem_xfree(alloc, block);
273 #ifdef CONFIG_SND_DEBUG
274 static void snd_gf1_mem_info_read(struct snd_info_entry *entry,
275 struct snd_info_buffer *buffer)
277 struct snd_gus_card *gus;
278 struct snd_gf1_mem *alloc;
279 struct snd_gf1_mem_block *block;
280 unsigned int total, used;
283 gus = entry->private_data;
284 alloc = &gus->gf1.mem_alloc;
285 mutex_lock(&alloc->memory_mutex);
286 snd_iprintf(buffer, "8-bit banks : \n ");
287 for (i = 0; i < 4; i++)
288 snd_iprintf(buffer, "0x%06x (%04ik)%s", alloc->banks_8[i].address, alloc->banks_8[i].size >> 10, i + 1 < 4 ? "," : "");
289 snd_iprintf(buffer, "\n"
290 "16-bit banks : \n ");
291 for (i = total = 0; i < 4; i++) {
292 snd_iprintf(buffer, "0x%06x (%04ik)%s", alloc->banks_16[i].address, alloc->banks_16[i].size >> 10, i + 1 < 4 ? "," : "");
293 total += alloc->banks_16[i].size;
295 snd_iprintf(buffer, "\n");
297 for (block = alloc->first, i = 0; block; block = block->next, i++) {
299 snd_iprintf(buffer, "Block %i onboard 0x%x size %i (0x%x):\n", i, block->ptr, block->size, block->size);
301 block->share_id[0] || block->share_id[1] ||
302 block->share_id[2] || block->share_id[3])
303 snd_iprintf(buffer, " Share : %i [id0 0x%x] [id1 0x%x] [id2 0x%x] [id3 0x%x]\n",
305 block->share_id[0], block->share_id[1],
306 block->share_id[2], block->share_id[3]);
307 snd_iprintf(buffer, " Flags :%s\n",
308 block->flags & SNDRV_GF1_MEM_BLOCK_16BIT ? " 16-bit" : "");
309 snd_iprintf(buffer, " Owner : ");
310 switch (block->owner) {
311 case SNDRV_GF1_MEM_OWNER_DRIVER:
312 snd_iprintf(buffer, "driver - %s\n", block->name);
314 case SNDRV_GF1_MEM_OWNER_WAVE_SIMPLE:
315 snd_iprintf(buffer, "SIMPLE wave\n");
317 case SNDRV_GF1_MEM_OWNER_WAVE_GF1:
318 snd_iprintf(buffer, "GF1 wave\n");
320 case SNDRV_GF1_MEM_OWNER_WAVE_IWFFFF:
321 snd_iprintf(buffer, "IWFFFF wave\n");
324 snd_iprintf(buffer, "unknown\n");
327 snd_iprintf(buffer, " Total: memory = %i, used = %i, free = %i\n",
328 total, used, total - used);
329 mutex_unlock(&alloc->memory_mutex);
331 ultra_iprintf(buffer, " Verify: free = %i, max 8-bit block = %i, max 16-bit block = %i\n",
332 ultra_memory_free_size(card, &card->gf1.mem_alloc),
333 ultra_memory_free_block(card, &card->gf1.mem_alloc, 0),
334 ultra_memory_free_block(card, &card->gf1.mem_alloc, 1));