1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
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 *snd_gf1_mem_xalloc(struct snd_gf1_mem * alloc,
28 struct snd_gf1_mem_block * block)
30 struct snd_gf1_mem_block *pblock, *nblock;
32 nblock = kmalloc(sizeof(struct snd_gf1_mem_block), GFP_KERNEL);
36 pblock = alloc->first;
38 if (pblock->ptr > nblock->ptr) {
39 nblock->prev = pblock->prev;
40 nblock->next = pblock;
41 pblock->prev = nblock;
42 if (pblock == alloc->first)
43 alloc->first = nblock;
45 nblock->prev->next = nblock;
46 mutex_unlock(&alloc->memory_mutex);
49 pblock = pblock->next;
52 if (alloc->last == NULL) {
54 alloc->first = alloc->last = nblock;
56 nblock->prev = alloc->last;
57 alloc->last->next = nblock;
63 int snd_gf1_mem_xfree(struct snd_gf1_mem * alloc, struct snd_gf1_mem_block * block)
65 if (block->share) { /* ok.. shared block */
67 mutex_unlock(&alloc->memory_mutex);
70 if (alloc->first == block) {
71 alloc->first = block->next;
73 block->next->prev = NULL;
75 block->prev->next = block->next;
77 block->next->prev = block->prev;
79 if (alloc->last == block) {
80 alloc->last = block->prev;
82 block->prev->next = NULL;
84 block->next->prev = block->prev;
86 block->prev->next = block->next;
93 static struct snd_gf1_mem_block *snd_gf1_mem_look(struct snd_gf1_mem * alloc,
96 struct snd_gf1_mem_block *block;
98 for (block = alloc->first; block; block = block->next) {
99 if (block->ptr == address) {
106 static struct snd_gf1_mem_block *snd_gf1_mem_share(struct snd_gf1_mem * alloc,
107 unsigned int *share_id)
109 struct snd_gf1_mem_block *block;
111 if (!share_id[0] && !share_id[1] &&
112 !share_id[2] && !share_id[3])
114 for (block = alloc->first; block; block = block->next)
115 if (!memcmp(share_id, block->share_id,
116 sizeof(block->share_id)))
121 static int snd_gf1_mem_find(struct snd_gf1_mem * alloc,
122 struct snd_gf1_mem_block * block,
123 unsigned int size, int w_16, int align)
125 struct snd_gf1_bank_info *info = w_16 ? alloc->banks_16 : alloc->banks_8;
126 unsigned int idx, boundary;
128 struct snd_gf1_mem_block *pblock;
129 unsigned int ptr1, ptr2;
131 if (w_16 && align < 2)
133 block->flags = w_16 ? SNDRV_GF1_MEM_BLOCK_16BIT : 0;
134 block->owner = SNDRV_GF1_MEM_OWNER_DRIVER;
136 block->share_id[0] = block->share_id[1] =
137 block->share_id[2] = block->share_id[3] = 0;
139 block->prev = block->next = NULL;
140 for (pblock = alloc->first, idx = 0; pblock; pblock = pblock->next) {
141 while (pblock->ptr >= (boundary = info[idx].address + info[idx].size))
143 while (pblock->ptr + pblock->size >= (boundary = info[idx].address + info[idx].size))
147 if (pblock->ptr + pblock->size == pblock->next->ptr)
149 if (pblock->next->ptr < boundary)
150 ptr2 = pblock->next->ptr;
152 ptr1 = ALIGN(pblock->ptr + pblock->size, align);
156 if ((int)size <= size1) {
163 if (size <= info[idx].size) {
164 /* I assume that bank address is already aligned.. */
165 block->ptr = info[idx].address;
173 struct snd_gf1_mem_block *snd_gf1_mem_alloc(struct snd_gf1_mem * alloc, int owner,
174 char *name, int size, int w_16, int align,
175 unsigned int *share_id)
177 struct snd_gf1_mem_block block, *nblock;
179 snd_gf1_mem_lock(alloc, 0);
180 if (share_id != NULL) {
181 nblock = snd_gf1_mem_share(alloc, share_id);
182 if (nblock != NULL) {
183 if (size != (int)nblock->size) {
184 /* TODO: remove in the future */
185 snd_printk(KERN_ERR "snd_gf1_mem_alloc - share: sizes differ\n");
189 snd_gf1_mem_lock(alloc, 1);
194 if (snd_gf1_mem_find(alloc, &block, size, w_16, align) < 0) {
195 snd_gf1_mem_lock(alloc, 1);
198 if (share_id != NULL)
199 memcpy(&block.share_id, share_id, sizeof(block.share_id));
201 block.name = kstrdup(name, GFP_KERNEL);
202 nblock = snd_gf1_mem_xalloc(alloc, &block);
203 snd_gf1_mem_lock(alloc, 1);
207 int snd_gf1_mem_free(struct snd_gf1_mem * alloc, unsigned int address)
210 struct snd_gf1_mem_block *block;
212 snd_gf1_mem_lock(alloc, 0);
213 block = snd_gf1_mem_look(alloc, address);
215 result = snd_gf1_mem_xfree(alloc, block);
216 snd_gf1_mem_lock(alloc, 1);
219 snd_gf1_mem_lock(alloc, 1);
223 int snd_gf1_mem_init(struct snd_gus_card * gus)
225 struct snd_gf1_mem *alloc;
226 struct snd_gf1_mem_block block;
228 alloc = &gus->gf1.mem_alloc;
229 mutex_init(&alloc->memory_mutex);
230 alloc->first = alloc->last = NULL;
231 if (!gus->gf1.memory)
234 memset(&block, 0, sizeof(block));
235 block.owner = SNDRV_GF1_MEM_OWNER_DRIVER;
236 if (gus->gf1.enh_mode) {
239 block.name = kstrdup("InterWave LFOs", GFP_KERNEL);
240 if (snd_gf1_mem_xalloc(alloc, &block) == NULL)
243 block.ptr = gus->gf1.default_voice_address;
245 block.name = kstrdup("Voice default (NULL's)", GFP_KERNEL);
246 if (snd_gf1_mem_xalloc(alloc, &block) == NULL)
248 #ifdef CONFIG_SND_DEBUG
249 snd_card_ro_proc_new(gus->card, "gusmem", gus, snd_gf1_mem_info_read);
254 int snd_gf1_mem_done(struct snd_gus_card * gus)
256 struct snd_gf1_mem *alloc;
257 struct snd_gf1_mem_block *block, *nblock;
259 alloc = &gus->gf1.mem_alloc;
260 block = alloc->first;
262 nblock = block->next;
263 snd_gf1_mem_xfree(alloc, block);
269 #ifdef CONFIG_SND_DEBUG
270 static void snd_gf1_mem_info_read(struct snd_info_entry *entry,
271 struct snd_info_buffer *buffer)
273 struct snd_gus_card *gus;
274 struct snd_gf1_mem *alloc;
275 struct snd_gf1_mem_block *block;
276 unsigned int total, used;
279 gus = entry->private_data;
280 alloc = &gus->gf1.mem_alloc;
281 mutex_lock(&alloc->memory_mutex);
282 snd_iprintf(buffer, "8-bit banks : \n ");
283 for (i = 0; i < 4; i++)
284 snd_iprintf(buffer, "0x%06x (%04ik)%s", alloc->banks_8[i].address, alloc->banks_8[i].size >> 10, i + 1 < 4 ? "," : "");
285 snd_iprintf(buffer, "\n"
286 "16-bit banks : \n ");
287 for (i = total = 0; i < 4; i++) {
288 snd_iprintf(buffer, "0x%06x (%04ik)%s", alloc->banks_16[i].address, alloc->banks_16[i].size >> 10, i + 1 < 4 ? "," : "");
289 total += alloc->banks_16[i].size;
291 snd_iprintf(buffer, "\n");
293 for (block = alloc->first, i = 0; block; block = block->next, i++) {
295 snd_iprintf(buffer, "Block %i onboard 0x%x size %i (0x%x):\n", i, block->ptr, block->size, block->size);
297 block->share_id[0] || block->share_id[1] ||
298 block->share_id[2] || block->share_id[3])
299 snd_iprintf(buffer, " Share : %i [id0 0x%x] [id1 0x%x] [id2 0x%x] [id3 0x%x]\n",
301 block->share_id[0], block->share_id[1],
302 block->share_id[2], block->share_id[3]);
303 snd_iprintf(buffer, " Flags :%s\n",
304 block->flags & SNDRV_GF1_MEM_BLOCK_16BIT ? " 16-bit" : "");
305 snd_iprintf(buffer, " Owner : ");
306 switch (block->owner) {
307 case SNDRV_GF1_MEM_OWNER_DRIVER:
308 snd_iprintf(buffer, "driver - %s\n", block->name);
310 case SNDRV_GF1_MEM_OWNER_WAVE_SIMPLE:
311 snd_iprintf(buffer, "SIMPLE wave\n");
313 case SNDRV_GF1_MEM_OWNER_WAVE_GF1:
314 snd_iprintf(buffer, "GF1 wave\n");
316 case SNDRV_GF1_MEM_OWNER_WAVE_IWFFFF:
317 snd_iprintf(buffer, "IWFFFF wave\n");
320 snd_iprintf(buffer, "unknown\n");
323 snd_iprintf(buffer, " Total: memory = %i, used = %i, free = %i\n",
324 total, used, total - used);
325 mutex_unlock(&alloc->memory_mutex);
327 ultra_iprintf(buffer, " Verify: free = %i, max 8-bit block = %i, max 16-bit block = %i\n",
328 ultra_memory_free_size(card, &card->gf1.mem_alloc),
329 ultra_memory_free_block(card, &card->gf1.mem_alloc, 0),
330 ultra_memory_free_block(card, &card->gf1.mem_alloc, 1));