gus_mem.c 9.7 KB

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