1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
4 *  GUS's memory allocation routines / bottom layer
5 */
6
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>
12
13#ifdef CONFIG_SND_DEBUG
14static void snd_gf1_mem_info_read(struct snd_info_entry *entry,
15				  struct snd_info_buffer *buffer);
16#endif
17
18void snd_gf1_mem_lock(struct snd_gf1_mem * alloc, int xup)
19{
20	if (!xup) {
21		mutex_lock(&alloc->memory_mutex);
22	} else {
23		mutex_unlock(&alloc->memory_mutex);
24	}
25}
26
27static struct snd_gf1_mem_block *
28snd_gf1_mem_xalloc(struct snd_gf1_mem *alloc, struct snd_gf1_mem_block *block,
29		   const char *name)
30{
31	struct snd_gf1_mem_block *pblock, *nblock;
32
33	nblock = kmalloc(sizeof(struct snd_gf1_mem_block), GFP_KERNEL);
34	if (nblock == NULL)
35		return NULL;
36	*nblock = *block;
37	nblock->name = kstrdup(name, GFP_KERNEL);
38	if (!nblock->name) {
39		kfree(nblock);
40		return NULL;
41	}
42
43	pblock = alloc->first;
44	while (pblock) {
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;
51			else
52				nblock->prev->next = nblock;
53			mutex_unlock(&alloc->memory_mutex);
54			return nblock;
55		}
56		pblock = pblock->next;
57	}
58	nblock->next = NULL;
59	if (alloc->last == NULL) {
60		nblock->prev = NULL;
61		alloc->first = alloc->last = nblock;
62	} else {
63		nblock->prev = alloc->last;
64		alloc->last->next = nblock;
65		alloc->last = nblock;
66	}
67	return nblock;
68}
69
70int snd_gf1_mem_xfree(struct snd_gf1_mem * alloc, struct snd_gf1_mem_block * block)
71{
72	if (block->share) {	/* ok.. shared block */
73		block->share--;
74		mutex_unlock(&alloc->memory_mutex);
75		return 0;
76	}
77	if (alloc->first == block) {
78		alloc->first = block->next;
79		if (block->next)
80			block->next->prev = NULL;
81	} else {
82		block->prev->next = block->next;
83		if (block->next)
84			block->next->prev = block->prev;
85	}
86	if (alloc->last == block) {
87		alloc->last = block->prev;
88		if (block->prev)
89			block->prev->next = NULL;
90	} else {
91		block->next->prev = block->prev;
92		if (block->prev)
93			block->prev->next = block->next;
94	}
95	kfree(block->name);
96	kfree(block);
97	return 0;
98}
99
100static struct snd_gf1_mem_block *snd_gf1_mem_look(struct snd_gf1_mem * alloc,
101					     unsigned int address)
102{
103	struct snd_gf1_mem_block *block;
104
105	for (block = alloc->first; block; block = block->next) {
106		if (block->ptr == address) {
107			return block;
108		}
109	}
110	return NULL;
111}
112
113static struct snd_gf1_mem_block *snd_gf1_mem_share(struct snd_gf1_mem * alloc,
114					      unsigned int *share_id)
115{
116	struct snd_gf1_mem_block *block;
117
118	if (!share_id[0] && !share_id[1] &&
119	    !share_id[2] && !share_id[3])
120		return NULL;
121	for (block = alloc->first; block; block = block->next)
122		if (!memcmp(share_id, block->share_id,
123				sizeof(block->share_id)))
124			return block;
125	return NULL;
126}
127
128static 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)
131{
132	struct snd_gf1_bank_info *info = w_16 ? alloc->banks_16 : alloc->banks_8;
133	unsigned int idx, boundary;
134	int size1;
135	struct snd_gf1_mem_block *pblock;
136	unsigned int ptr1, ptr2;
137
138	if (w_16 && align < 2)
139		align = 2;
140	block->flags = w_16 ? SNDRV_GF1_MEM_BLOCK_16BIT : 0;
141	block->owner = SNDRV_GF1_MEM_OWNER_DRIVER;
142	block->share = 0;
143	block->share_id[0] = block->share_id[1] =
144	block->share_id[2] = block->share_id[3] = 0;
145	block->name = NULL;
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))
149			idx++;
150		while (pblock->ptr + pblock->size >= (boundary = info[idx].address + info[idx].size))
151			idx++;
152		ptr2 = boundary;
153		if (pblock->next) {
154			if (pblock->ptr + pblock->size == pblock->next->ptr)
155				continue;
156			if (pblock->next->ptr < boundary)
157				ptr2 = pblock->next->ptr;
158		}
159		ptr1 = ALIGN(pblock->ptr + pblock->size, align);
160		if (ptr1 >= ptr2)
161			continue;
162		size1 = ptr2 - ptr1;
163		if ((int)size <= size1) {
164			block->ptr = ptr1;
165			block->size = size;
166			return 0;
167		}
168	}
169	while (++idx < 4) {
170		if (size <= info[idx].size) {
171			/* I assume that bank address is already aligned.. */
172			block->ptr = info[idx].address;
173			block->size = size;
174			return 0;
175		}
176	}
177	return -ENOMEM;
178}
179
180struct 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)
183{
184	struct snd_gf1_mem_block block, *nblock;
185
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");
193				goto __std;
194			}
195			nblock->share++;
196			snd_gf1_mem_lock(alloc, 1);
197			return NULL;
198		}
199	}
200      __std:
201	if (snd_gf1_mem_find(alloc, &block, size, w_16, align) < 0) {
202		snd_gf1_mem_lock(alloc, 1);
203		return NULL;
204	}
205	if (share_id != NULL)
206		memcpy(&block.share_id, share_id, sizeof(block.share_id));
207	block.owner = owner;
208	nblock = snd_gf1_mem_xalloc(alloc, &block, name);
209	snd_gf1_mem_lock(alloc, 1);
210	return nblock;
211}
212
213int snd_gf1_mem_free(struct snd_gf1_mem * alloc, unsigned int address)
214{
215	int result;
216	struct snd_gf1_mem_block *block;
217
218	snd_gf1_mem_lock(alloc, 0);
219	block = snd_gf1_mem_look(alloc, address);
220	if (block) {
221		result = snd_gf1_mem_xfree(alloc, block);
222		snd_gf1_mem_lock(alloc, 1);
223		return result;
224	}
225	snd_gf1_mem_lock(alloc, 1);
226	return -EINVAL;
227}
228
229int snd_gf1_mem_init(struct snd_gus_card * gus)
230{
231	struct snd_gf1_mem *alloc;
232	struct snd_gf1_mem_block block;
233
234	alloc = &gus->gf1.mem_alloc;
235	mutex_init(&alloc->memory_mutex);
236	alloc->first = alloc->last = NULL;
237	if (!gus->gf1.memory)
238		return 0;
239
240	memset(&block, 0, sizeof(block));
241	block.owner = SNDRV_GF1_MEM_OWNER_DRIVER;
242	if (gus->gf1.enh_mode) {
243		block.ptr = 0;
244		block.size = 1024;
245		if (!snd_gf1_mem_xalloc(alloc, &block, "InterWave LFOs"))
246			return -ENOMEM;
247	}
248	block.ptr = gus->gf1.default_voice_address;
249	block.size = 4;
250	if (!snd_gf1_mem_xalloc(alloc, &block, "Voice default (NULL's)"))
251		return -ENOMEM;
252#ifdef CONFIG_SND_DEBUG
253	snd_card_ro_proc_new(gus->card, "gusmem", gus, snd_gf1_mem_info_read);
254#endif
255	return 0;
256}
257
258int snd_gf1_mem_done(struct snd_gus_card * gus)
259{
260	struct snd_gf1_mem *alloc;
261	struct snd_gf1_mem_block *block, *nblock;
262
263	alloc = &gus->gf1.mem_alloc;
264	block = alloc->first;
265	while (block) {
266		nblock = block->next;
267		snd_gf1_mem_xfree(alloc, block);
268		block = nblock;
269	}
270	return 0;
271}
272
273#ifdef CONFIG_SND_DEBUG
274static void snd_gf1_mem_info_read(struct snd_info_entry *entry,
275				  struct snd_info_buffer *buffer)
276{
277	struct snd_gus_card *gus;
278	struct snd_gf1_mem *alloc;
279	struct snd_gf1_mem_block *block;
280	unsigned int total, used;
281	int i;
282
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;
294	}
295	snd_iprintf(buffer, "\n");
296	used = 0;
297	for (block = alloc->first, i = 0; block; block = block->next, i++) {
298		used += block->size;
299		snd_iprintf(buffer, "Block %i onboard 0x%x size %i (0x%x):\n", i, block->ptr, block->size, block->size);
300		if (block->share ||
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",
304				block->share,
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);
313			break;
314		case SNDRV_GF1_MEM_OWNER_WAVE_SIMPLE:
315			snd_iprintf(buffer, "SIMPLE wave\n");
316			break;
317		case SNDRV_GF1_MEM_OWNER_WAVE_GF1:
318			snd_iprintf(buffer, "GF1 wave\n");
319			break;
320		case SNDRV_GF1_MEM_OWNER_WAVE_IWFFFF:
321			snd_iprintf(buffer, "IWFFFF wave\n");
322			break;
323		default:
324			snd_iprintf(buffer, "unknown\n");
325		}
326	}
327	snd_iprintf(buffer, "  Total: memory = %i, used = %i, free = %i\n",
328		    total, used, total - used);
329	mutex_unlock(&alloc->memory_mutex);
330#if 0
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));
335#endif
336}
337#endif
338