gus_mem.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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,
  121. sizeof(block->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. if (w_16 && align < 2)
  135. align = 2;
  136. block->flags = w_16 ? SNDRV_GF1_MEM_BLOCK_16BIT : 0;
  137. block->owner = SNDRV_GF1_MEM_OWNER_DRIVER;
  138. block->share = 0;
  139. block->share_id[0] = block->share_id[1] =
  140. block->share_id[2] = block->share_id[3] = 0;
  141. block->name = NULL;
  142. block->prev = block->next = NULL;
  143. for (pblock = alloc->first, idx = 0; pblock; pblock = pblock->next) {
  144. while (pblock->ptr >= (boundary = info[idx].address + info[idx].size))
  145. idx++;
  146. while (pblock->ptr + pblock->size >= (boundary = info[idx].address + info[idx].size))
  147. idx++;
  148. ptr2 = boundary;
  149. if (pblock->next) {
  150. if (pblock->ptr + pblock->size == pblock->next->ptr)
  151. continue;
  152. if (pblock->next->ptr < boundary)
  153. ptr2 = pblock->next->ptr;
  154. }
  155. ptr1 = ALIGN(pblock->ptr + pblock->size, align);
  156. if (ptr1 >= ptr2)
  157. continue;
  158. size1 = ptr2 - ptr1;
  159. if ((int)size <= size1) {
  160. block->ptr = ptr1;
  161. block->size = size;
  162. return 0;
  163. }
  164. }
  165. while (++idx < 4) {
  166. if (size <= info[idx].size) {
  167. /* I assume that bank address is already aligned.. */
  168. block->ptr = info[idx].address;
  169. block->size = size;
  170. return 0;
  171. }
  172. }
  173. return -ENOMEM;
  174. }
  175. struct snd_gf1_mem_block *snd_gf1_mem_alloc(struct snd_gf1_mem * alloc, int owner,
  176. char *name, int size, int w_16, int align,
  177. unsigned int *share_id)
  178. {
  179. struct snd_gf1_mem_block block, *nblock;
  180. snd_gf1_mem_lock(alloc, 0);
  181. if (share_id != NULL) {
  182. nblock = snd_gf1_mem_share(alloc, share_id);
  183. if (nblock != NULL) {
  184. if (size != (int)nblock->size) {
  185. /* TODO: remove in the future */
  186. snd_printk(KERN_ERR "snd_gf1_mem_alloc - share: sizes differ\n");
  187. goto __std;
  188. }
  189. nblock->share++;
  190. snd_gf1_mem_lock(alloc, 1);
  191. return NULL;
  192. }
  193. }
  194. __std:
  195. if (snd_gf1_mem_find(alloc, &block, size, w_16, align) < 0) {
  196. snd_gf1_mem_lock(alloc, 1);
  197. return NULL;
  198. }
  199. if (share_id != NULL)
  200. memcpy(&block.share_id, share_id, sizeof(block.share_id));
  201. block.owner = owner;
  202. block.name = kstrdup(name, GFP_KERNEL);
  203. nblock = snd_gf1_mem_xalloc(alloc, &block);
  204. snd_gf1_mem_lock(alloc, 1);
  205. return nblock;
  206. }
  207. int snd_gf1_mem_free(struct snd_gf1_mem * alloc, unsigned int address)
  208. {
  209. int result;
  210. struct snd_gf1_mem_block *block;
  211. snd_gf1_mem_lock(alloc, 0);
  212. if ((block = snd_gf1_mem_look(alloc, address)) != NULL) {
  213. result = snd_gf1_mem_xfree(alloc, block);
  214. snd_gf1_mem_lock(alloc, 1);
  215. return result;
  216. }
  217. snd_gf1_mem_lock(alloc, 1);
  218. return -EINVAL;
  219. }
  220. int snd_gf1_mem_init(struct snd_gus_card * gus)
  221. {
  222. struct snd_gf1_mem *alloc;
  223. struct snd_gf1_mem_block block;
  224. #ifdef CONFIG_SND_DEBUG
  225. struct snd_info_entry *entry;
  226. #endif
  227. alloc = &gus->gf1.mem_alloc;
  228. mutex_init(&alloc->memory_mutex);
  229. alloc->first = alloc->last = NULL;
  230. if (!gus->gf1.memory)
  231. return 0;
  232. memset(&block, 0, sizeof(block));
  233. block.owner = SNDRV_GF1_MEM_OWNER_DRIVER;
  234. if (gus->gf1.enh_mode) {
  235. block.ptr = 0;
  236. block.size = 1024;
  237. block.name = kstrdup("InterWave LFOs", GFP_KERNEL);
  238. if (snd_gf1_mem_xalloc(alloc, &block) == NULL)
  239. return -ENOMEM;
  240. }
  241. block.ptr = gus->gf1.default_voice_address;
  242. block.size = 4;
  243. block.name = kstrdup("Voice default (NULL's)", GFP_KERNEL);
  244. if (snd_gf1_mem_xalloc(alloc, &block) == NULL)
  245. return -ENOMEM;
  246. #ifdef CONFIG_SND_DEBUG
  247. if (! snd_card_proc_new(gus->card, "gusmem", &entry))
  248. snd_info_set_text_ops(entry, gus, snd_gf1_mem_info_read);
  249. #endif
  250. return 0;
  251. }
  252. int snd_gf1_mem_done(struct snd_gus_card * gus)
  253. {
  254. struct snd_gf1_mem *alloc;
  255. struct snd_gf1_mem_block *block, *nblock;
  256. alloc = &gus->gf1.mem_alloc;
  257. block = alloc->first;
  258. while (block) {
  259. nblock = block->next;
  260. snd_gf1_mem_xfree(alloc, block);
  261. block = nblock;
  262. }
  263. return 0;
  264. }
  265. #ifdef CONFIG_SND_DEBUG
  266. static void snd_gf1_mem_info_read(struct snd_info_entry *entry,
  267. struct snd_info_buffer *buffer)
  268. {
  269. struct snd_gus_card *gus;
  270. struct snd_gf1_mem *alloc;
  271. struct snd_gf1_mem_block *block;
  272. unsigned int total, used;
  273. int i;
  274. gus = entry->private_data;
  275. alloc = &gus->gf1.mem_alloc;
  276. mutex_lock(&alloc->memory_mutex);
  277. snd_iprintf(buffer, "8-bit banks : \n ");
  278. for (i = 0; i < 4; i++)
  279. snd_iprintf(buffer, "0x%06x (%04ik)%s", alloc->banks_8[i].address, alloc->banks_8[i].size >> 10, i + 1 < 4 ? "," : "");
  280. snd_iprintf(buffer, "\n"
  281. "16-bit banks : \n ");
  282. for (i = total = 0; i < 4; i++) {
  283. snd_iprintf(buffer, "0x%06x (%04ik)%s", alloc->banks_16[i].address, alloc->banks_16[i].size >> 10, i + 1 < 4 ? "," : "");
  284. total += alloc->banks_16[i].size;
  285. }
  286. snd_iprintf(buffer, "\n");
  287. used = 0;
  288. for (block = alloc->first, i = 0; block; block = block->next, i++) {
  289. used += block->size;
  290. 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);
  291. if (block->share ||
  292. block->share_id[0] || block->share_id[1] ||
  293. block->share_id[2] || block->share_id[3])
  294. snd_iprintf(buffer, " Share : %i [id0 0x%x] [id1 0x%x] [id2 0x%x] [id3 0x%x]\n",
  295. block->share,
  296. block->share_id[0], block->share_id[1],
  297. block->share_id[2], block->share_id[3]);
  298. snd_iprintf(buffer, " Flags :%s\n",
  299. block->flags & SNDRV_GF1_MEM_BLOCK_16BIT ? " 16-bit" : "");
  300. snd_iprintf(buffer, " Owner : ");
  301. switch (block->owner) {
  302. case SNDRV_GF1_MEM_OWNER_DRIVER:
  303. snd_iprintf(buffer, "driver - %s\n", block->name);
  304. break;
  305. case SNDRV_GF1_MEM_OWNER_WAVE_SIMPLE:
  306. snd_iprintf(buffer, "SIMPLE wave\n");
  307. break;
  308. case SNDRV_GF1_MEM_OWNER_WAVE_GF1:
  309. snd_iprintf(buffer, "GF1 wave\n");
  310. break;
  311. case SNDRV_GF1_MEM_OWNER_WAVE_IWFFFF:
  312. snd_iprintf(buffer, "IWFFFF wave\n");
  313. break;
  314. default:
  315. snd_iprintf(buffer, "unknown\n");
  316. }
  317. }
  318. snd_iprintf(buffer, " Total: memory = %i, used = %i, free = %i\n",
  319. total, used, total - used);
  320. mutex_unlock(&alloc->memory_mutex);
  321. #if 0
  322. ultra_iprintf(buffer, " Verify: free = %i, max 8-bit block = %i, max 16-bit block = %i\n",
  323. ultra_memory_free_size(card, &card->gf1.mem_alloc),
  324. ultra_memory_free_block(card, &card->gf1.mem_alloc, 0),
  325. ultra_memory_free_block(card, &card->gf1.mem_alloc, 1));
  326. #endif
  327. }
  328. #endif