gus_mem.c 9.7 KB

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