gus_mem.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (c) by Jaroslav Kysela <[email protected]>
  4. * GUS's memory allocation routines / bottom layer
  5. */
  6. #include <linux/slab.h>
  7. #include <linux/string.h>
  8. #include <sound/core.h>
  9. #include <sound/gus.h>
  10. #include <sound/info.h>
  11. #ifdef CONFIG_SND_DEBUG
  12. static void snd_gf1_mem_info_read(struct snd_info_entry *entry,
  13. struct snd_info_buffer *buffer);
  14. #endif
  15. void snd_gf1_mem_lock(struct snd_gf1_mem * alloc, int xup)
  16. {
  17. if (!xup) {
  18. mutex_lock(&alloc->memory_mutex);
  19. } else {
  20. mutex_unlock(&alloc->memory_mutex);
  21. }
  22. }
  23. static struct snd_gf1_mem_block *
  24. snd_gf1_mem_xalloc(struct snd_gf1_mem *alloc, struct snd_gf1_mem_block *block,
  25. const char *name)
  26. {
  27. struct snd_gf1_mem_block *pblock, *nblock;
  28. nblock = kmalloc(sizeof(struct snd_gf1_mem_block), GFP_KERNEL);
  29. if (nblock == NULL)
  30. return NULL;
  31. *nblock = *block;
  32. nblock->name = kstrdup(name, GFP_KERNEL);
  33. if (!nblock->name) {
  34. kfree(nblock);
  35. return NULL;
  36. }
  37. pblock = alloc->first;
  38. while (pblock) {
  39. if (pblock->ptr > nblock->ptr) {
  40. nblock->prev = pblock->prev;
  41. nblock->next = pblock;
  42. pblock->prev = nblock;
  43. if (pblock == alloc->first)
  44. alloc->first = nblock;
  45. else
  46. nblock->prev->next = nblock;
  47. mutex_unlock(&alloc->memory_mutex);
  48. return nblock;
  49. }
  50. pblock = pblock->next;
  51. }
  52. nblock->next = NULL;
  53. if (alloc->last == NULL) {
  54. nblock->prev = NULL;
  55. alloc->first = alloc->last = nblock;
  56. } else {
  57. nblock->prev = alloc->last;
  58. alloc->last->next = nblock;
  59. alloc->last = nblock;
  60. }
  61. return nblock;
  62. }
  63. int snd_gf1_mem_xfree(struct snd_gf1_mem * alloc, struct snd_gf1_mem_block * block)
  64. {
  65. if (block->share) { /* ok.. shared block */
  66. block->share--;
  67. mutex_unlock(&alloc->memory_mutex);
  68. return 0;
  69. }
  70. if (alloc->first == block) {
  71. alloc->first = block->next;
  72. if (block->next)
  73. block->next->prev = NULL;
  74. } else {
  75. block->prev->next = block->next;
  76. if (block->next)
  77. block->next->prev = block->prev;
  78. }
  79. if (alloc->last == block) {
  80. alloc->last = block->prev;
  81. if (block->prev)
  82. block->prev->next = NULL;
  83. } else {
  84. block->next->prev = block->prev;
  85. if (block->prev)
  86. block->prev->next = block->next;
  87. }
  88. kfree(block->name);
  89. kfree(block);
  90. return 0;
  91. }
  92. static struct snd_gf1_mem_block *snd_gf1_mem_look(struct snd_gf1_mem * alloc,
  93. unsigned int address)
  94. {
  95. struct snd_gf1_mem_block *block;
  96. for (block = alloc->first; block; block = block->next) {
  97. if (block->ptr == address) {
  98. return block;
  99. }
  100. }
  101. return NULL;
  102. }
  103. static struct snd_gf1_mem_block *snd_gf1_mem_share(struct snd_gf1_mem * alloc,
  104. unsigned int *share_id)
  105. {
  106. struct snd_gf1_mem_block *block;
  107. if (!share_id[0] && !share_id[1] &&
  108. !share_id[2] && !share_id[3])
  109. return NULL;
  110. for (block = alloc->first; block; block = block->next)
  111. if (!memcmp(share_id, block->share_id,
  112. sizeof(block->share_id)))
  113. return block;
  114. return NULL;
  115. }
  116. static int snd_gf1_mem_find(struct snd_gf1_mem * alloc,
  117. struct snd_gf1_mem_block * block,
  118. unsigned int size, int w_16, int align)
  119. {
  120. struct snd_gf1_bank_info *info = w_16 ? alloc->banks_16 : alloc->banks_8;
  121. unsigned int idx, boundary;
  122. int size1;
  123. struct snd_gf1_mem_block *pblock;
  124. unsigned int ptr1, ptr2;
  125. if (w_16 && align < 2)
  126. align = 2;
  127. block->flags = w_16 ? SNDRV_GF1_MEM_BLOCK_16BIT : 0;
  128. block->owner = SNDRV_GF1_MEM_OWNER_DRIVER;
  129. block->share = 0;
  130. block->share_id[0] = block->share_id[1] =
  131. block->share_id[2] = block->share_id[3] = 0;
  132. block->name = NULL;
  133. block->prev = block->next = NULL;
  134. for (pblock = alloc->first, idx = 0; pblock; pblock = pblock->next) {
  135. while (pblock->ptr >= (boundary = info[idx].address + info[idx].size))
  136. idx++;
  137. while (pblock->ptr + pblock->size >= (boundary = info[idx].address + info[idx].size))
  138. idx++;
  139. ptr2 = boundary;
  140. if (pblock->next) {
  141. if (pblock->ptr + pblock->size == pblock->next->ptr)
  142. continue;
  143. if (pblock->next->ptr < boundary)
  144. ptr2 = pblock->next->ptr;
  145. }
  146. ptr1 = ALIGN(pblock->ptr + pblock->size, align);
  147. if (ptr1 >= ptr2)
  148. continue;
  149. size1 = ptr2 - ptr1;
  150. if ((int)size <= size1) {
  151. block->ptr = ptr1;
  152. block->size = size;
  153. return 0;
  154. }
  155. }
  156. while (++idx < 4) {
  157. if (size <= info[idx].size) {
  158. /* I assume that bank address is already aligned.. */
  159. block->ptr = info[idx].address;
  160. block->size = size;
  161. return 0;
  162. }
  163. }
  164. return -ENOMEM;
  165. }
  166. struct snd_gf1_mem_block *snd_gf1_mem_alloc(struct snd_gf1_mem * alloc, int owner,
  167. char *name, int size, int w_16, int align,
  168. unsigned int *share_id)
  169. {
  170. struct snd_gf1_mem_block block, *nblock;
  171. snd_gf1_mem_lock(alloc, 0);
  172. if (share_id != NULL) {
  173. nblock = snd_gf1_mem_share(alloc, share_id);
  174. if (nblock != NULL) {
  175. if (size != (int)nblock->size) {
  176. /* TODO: remove in the future */
  177. snd_printk(KERN_ERR "snd_gf1_mem_alloc - share: sizes differ\n");
  178. goto __std;
  179. }
  180. nblock->share++;
  181. snd_gf1_mem_lock(alloc, 1);
  182. return NULL;
  183. }
  184. }
  185. __std:
  186. if (snd_gf1_mem_find(alloc, &block, size, w_16, align) < 0) {
  187. snd_gf1_mem_lock(alloc, 1);
  188. return NULL;
  189. }
  190. if (share_id != NULL)
  191. memcpy(&block.share_id, share_id, sizeof(block.share_id));
  192. block.owner = owner;
  193. nblock = snd_gf1_mem_xalloc(alloc, &block, name);
  194. snd_gf1_mem_lock(alloc, 1);
  195. return nblock;
  196. }
  197. int snd_gf1_mem_free(struct snd_gf1_mem * alloc, unsigned int address)
  198. {
  199. int result;
  200. struct snd_gf1_mem_block *block;
  201. snd_gf1_mem_lock(alloc, 0);
  202. block = snd_gf1_mem_look(alloc, address);
  203. if (block) {
  204. result = snd_gf1_mem_xfree(alloc, block);
  205. snd_gf1_mem_lock(alloc, 1);
  206. return result;
  207. }
  208. snd_gf1_mem_lock(alloc, 1);
  209. return -EINVAL;
  210. }
  211. int snd_gf1_mem_init(struct snd_gus_card * gus)
  212. {
  213. struct snd_gf1_mem *alloc;
  214. struct snd_gf1_mem_block block;
  215. alloc = &gus->gf1.mem_alloc;
  216. mutex_init(&alloc->memory_mutex);
  217. alloc->first = alloc->last = NULL;
  218. if (!gus->gf1.memory)
  219. return 0;
  220. memset(&block, 0, sizeof(block));
  221. block.owner = SNDRV_GF1_MEM_OWNER_DRIVER;
  222. if (gus->gf1.enh_mode) {
  223. block.ptr = 0;
  224. block.size = 1024;
  225. if (!snd_gf1_mem_xalloc(alloc, &block, "InterWave LFOs"))
  226. return -ENOMEM;
  227. }
  228. block.ptr = gus->gf1.default_voice_address;
  229. block.size = 4;
  230. if (!snd_gf1_mem_xalloc(alloc, &block, "Voice default (NULL's)"))
  231. return -ENOMEM;
  232. #ifdef CONFIG_SND_DEBUG
  233. snd_card_ro_proc_new(gus->card, "gusmem", gus, snd_gf1_mem_info_read);
  234. #endif
  235. return 0;
  236. }
  237. int snd_gf1_mem_done(struct snd_gus_card * gus)
  238. {
  239. struct snd_gf1_mem *alloc;
  240. struct snd_gf1_mem_block *block, *nblock;
  241. alloc = &gus->gf1.mem_alloc;
  242. block = alloc->first;
  243. while (block) {
  244. nblock = block->next;
  245. snd_gf1_mem_xfree(alloc, block);
  246. block = nblock;
  247. }
  248. return 0;
  249. }
  250. #ifdef CONFIG_SND_DEBUG
  251. static void snd_gf1_mem_info_read(struct snd_info_entry *entry,
  252. struct snd_info_buffer *buffer)
  253. {
  254. struct snd_gus_card *gus;
  255. struct snd_gf1_mem *alloc;
  256. struct snd_gf1_mem_block *block;
  257. unsigned int total, used;
  258. int i;
  259. gus = entry->private_data;
  260. alloc = &gus->gf1.mem_alloc;
  261. mutex_lock(&alloc->memory_mutex);
  262. snd_iprintf(buffer, "8-bit banks : \n ");
  263. for (i = 0; i < 4; i++)
  264. snd_iprintf(buffer, "0x%06x (%04ik)%s", alloc->banks_8[i].address, alloc->banks_8[i].size >> 10, i + 1 < 4 ? "," : "");
  265. snd_iprintf(buffer, "\n"
  266. "16-bit banks : \n ");
  267. for (i = total = 0; i < 4; i++) {
  268. snd_iprintf(buffer, "0x%06x (%04ik)%s", alloc->banks_16[i].address, alloc->banks_16[i].size >> 10, i + 1 < 4 ? "," : "");
  269. total += alloc->banks_16[i].size;
  270. }
  271. snd_iprintf(buffer, "\n");
  272. used = 0;
  273. for (block = alloc->first, i = 0; block; block = block->next, i++) {
  274. used += block->size;
  275. snd_iprintf(buffer, "Block %i onboard 0x%x size %i (0x%x):\n", i, block->ptr, block->size, block->size);
  276. if (block->share ||
  277. block->share_id[0] || block->share_id[1] ||
  278. block->share_id[2] || block->share_id[3])
  279. snd_iprintf(buffer, " Share : %i [id0 0x%x] [id1 0x%x] [id2 0x%x] [id3 0x%x]\n",
  280. block->share,
  281. block->share_id[0], block->share_id[1],
  282. block->share_id[2], block->share_id[3]);
  283. snd_iprintf(buffer, " Flags :%s\n",
  284. block->flags & SNDRV_GF1_MEM_BLOCK_16BIT ? " 16-bit" : "");
  285. snd_iprintf(buffer, " Owner : ");
  286. switch (block->owner) {
  287. case SNDRV_GF1_MEM_OWNER_DRIVER:
  288. snd_iprintf(buffer, "driver - %s\n", block->name);
  289. break;
  290. case SNDRV_GF1_MEM_OWNER_WAVE_SIMPLE:
  291. snd_iprintf(buffer, "SIMPLE wave\n");
  292. break;
  293. case SNDRV_GF1_MEM_OWNER_WAVE_GF1:
  294. snd_iprintf(buffer, "GF1 wave\n");
  295. break;
  296. case SNDRV_GF1_MEM_OWNER_WAVE_IWFFFF:
  297. snd_iprintf(buffer, "IWFFFF wave\n");
  298. break;
  299. default:
  300. snd_iprintf(buffer, "unknown\n");
  301. }
  302. }
  303. snd_iprintf(buffer, " Total: memory = %i, used = %i, free = %i\n",
  304. total, used, total - used);
  305. mutex_unlock(&alloc->memory_mutex);
  306. #if 0
  307. ultra_iprintf(buffer, " Verify: free = %i, max 8-bit block = %i, max 16-bit block = %i\n",
  308. ultra_memory_free_size(card, &card->gf1.mem_alloc),
  309. ultra_memory_free_block(card, &card->gf1.mem_alloc, 0),
  310. ultra_memory_free_block(card, &card->gf1.mem_alloc, 1));
  311. #endif
  312. }
  313. #endif