gus_mem.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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 <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(snd_info_entry_t *entry,
  28. snd_info_buffer_t * buffer);
  29. #endif
  30. void snd_gf1_mem_lock(snd_gf1_mem_t * alloc, int xup)
  31. {
  32. if (!xup) {
  33. down(&alloc->memory_mutex);
  34. } else {
  35. up(&alloc->memory_mutex);
  36. }
  37. }
  38. static snd_gf1_mem_block_t *snd_gf1_mem_xalloc(snd_gf1_mem_t * alloc,
  39. snd_gf1_mem_block_t * block)
  40. {
  41. snd_gf1_mem_block_t *pblock, *nblock;
  42. nblock = (snd_gf1_mem_block_t *) kmalloc(sizeof(snd_gf1_mem_block_t), 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. up(&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(snd_gf1_mem_t * alloc, snd_gf1_mem_block_t * block)
  73. {
  74. if (block->share) { /* ok.. shared block */
  75. block->share--;
  76. up(&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 snd_gf1_mem_block_t *snd_gf1_mem_look(snd_gf1_mem_t * alloc,
  102. unsigned int address)
  103. {
  104. snd_gf1_mem_block_t *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 snd_gf1_mem_block_t *snd_gf1_mem_share(snd_gf1_mem_t * alloc,
  113. unsigned int *share_id)
  114. {
  115. snd_gf1_mem_block_t *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(snd_gf1_mem_t * alloc,
  125. snd_gf1_mem_block_t * block,
  126. unsigned int size, int w_16, int align)
  127. {
  128. snd_gf1_bank_info_t *info = w_16 ? alloc->banks_16 : alloc->banks_8;
  129. unsigned int idx, boundary;
  130. int size1;
  131. snd_gf1_mem_block_t *pblock;
  132. unsigned int ptr1, ptr2;
  133. align--;
  134. if (w_16 && align < 1)
  135. align = 1;
  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 = (pblock->ptr + pblock->size + align) & ~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. snd_gf1_mem_block_t *snd_gf1_mem_alloc(snd_gf1_mem_t * alloc, int owner,
  176. char *name, int size, int w_16, int align,
  177. unsigned int *share_id)
  178. {
  179. snd_gf1_mem_block_t 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("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 = snd_kmalloc_strdup(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(snd_gf1_mem_t * alloc, unsigned int address)
  208. {
  209. int result;
  210. snd_gf1_mem_block_t *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(snd_gus_card_t * gus)
  221. {
  222. snd_gf1_mem_t *alloc;
  223. snd_gf1_mem_block_t block;
  224. #ifdef CONFIG_SND_DEBUG
  225. snd_info_entry_t *entry;
  226. #endif
  227. alloc = &gus->gf1.mem_alloc;
  228. init_MUTEX(&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 = snd_kmalloc_strdup("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 = snd_kmalloc_strdup("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, 1024, snd_gf1_mem_info_read);
  249. entry->c.text.read_size = 256 * 1024;
  250. }
  251. #endif
  252. return 0;
  253. }
  254. int snd_gf1_mem_done(snd_gus_card_t * gus)
  255. {
  256. snd_gf1_mem_t *alloc;
  257. snd_gf1_mem_block_t *block, *nblock;
  258. alloc = &gus->gf1.mem_alloc;
  259. block = alloc->first;
  260. while (block) {
  261. nblock = block->next;
  262. snd_gf1_mem_xfree(alloc, block);
  263. block = nblock;
  264. }
  265. return 0;
  266. }
  267. #ifdef CONFIG_SND_DEBUG
  268. static void snd_gf1_mem_info_read(snd_info_entry_t *entry,
  269. snd_info_buffer_t * buffer)
  270. {
  271. snd_gus_card_t *gus;
  272. snd_gf1_mem_t *alloc;
  273. snd_gf1_mem_block_t *block;
  274. unsigned int total, used;
  275. int i;
  276. gus = entry->private_data;
  277. alloc = &gus->gf1.mem_alloc;
  278. down(&alloc->memory_mutex);
  279. snd_iprintf(buffer, "8-bit banks : \n ");
  280. for (i = 0; i < 4; i++)
  281. snd_iprintf(buffer, "0x%06x (%04ik)%s", alloc->banks_8[i].address, alloc->banks_8[i].size >> 10, i + 1 < 4 ? "," : "");
  282. snd_iprintf(buffer, "\n"
  283. "16-bit banks : \n ");
  284. for (i = total = 0; i < 4; i++) {
  285. snd_iprintf(buffer, "0x%06x (%04ik)%s", alloc->banks_16[i].address, alloc->banks_16[i].size >> 10, i + 1 < 4 ? "," : "");
  286. total += alloc->banks_16[i].size;
  287. }
  288. snd_iprintf(buffer, "\n");
  289. used = 0;
  290. for (block = alloc->first, i = 0; block; block = block->next, i++) {
  291. used += block->size;
  292. 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);
  293. if (block->share ||
  294. block->share_id[0] || block->share_id[1] ||
  295. block->share_id[2] || block->share_id[3])
  296. snd_iprintf(buffer, " Share : %i [id0 0x%x] [id1 0x%x] [id2 0x%x] [id3 0x%x]\n",
  297. block->share,
  298. block->share_id[0], block->share_id[1],
  299. block->share_id[2], block->share_id[3]);
  300. snd_iprintf(buffer, " Flags :%s\n",
  301. block->flags & SNDRV_GF1_MEM_BLOCK_16BIT ? " 16-bit" : "");
  302. snd_iprintf(buffer, " Owner : ");
  303. switch (block->owner) {
  304. case SNDRV_GF1_MEM_OWNER_DRIVER:
  305. snd_iprintf(buffer, "driver - %s\n", block->name);
  306. break;
  307. case SNDRV_GF1_MEM_OWNER_WAVE_SIMPLE:
  308. snd_iprintf(buffer, "SIMPLE wave\n");
  309. break;
  310. case SNDRV_GF1_MEM_OWNER_WAVE_GF1:
  311. snd_iprintf(buffer, "GF1 wave\n");
  312. break;
  313. case SNDRV_GF1_MEM_OWNER_WAVE_IWFFFF:
  314. snd_iprintf(buffer, "IWFFFF wave\n");
  315. break;
  316. default:
  317. snd_iprintf(buffer, "unknown\n");
  318. }
  319. }
  320. snd_iprintf(buffer, " Total: memory = %i, used = %i, free = %i\n",
  321. total, used, total - used);
  322. up(&alloc->memory_mutex);
  323. #if 0
  324. ultra_iprintf(buffer, " Verify: free = %i, max 8-bit block = %i, max 16-bit block = %i\n",
  325. ultra_memory_free_size(card, &card->gf1.mem_alloc),
  326. ultra_memory_free_block(card, &card->gf1.mem_alloc, 0),
  327. ultra_memory_free_block(card, &card->gf1.mem_alloc, 1));
  328. #endif
  329. }
  330. #endif