trident_memory.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
  3. * Copyright (c) by Takashi Iwai <tiwai@suse.de>
  4. * Copyright (c) by Scott McNab <sdm@fractalgraphics.com.au>
  5. *
  6. * Trident 4DWave-NX memory page allocation (TLB area)
  7. * Trident chip can handle only 16MByte of the memory at the same time.
  8. *
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. *
  24. */
  25. #include <asm/io.h>
  26. #include <linux/pci.h>
  27. #include <linux/time.h>
  28. #include <linux/mutex.h>
  29. #include <sound/core.h>
  30. #include <sound/trident.h>
  31. /* page arguments of these two macros are Trident page (4096 bytes), not like
  32. * aligned pages in others
  33. */
  34. #define __set_tlb_bus(trident,page,ptr,addr) \
  35. do { (trident)->tlb.entries[page] = cpu_to_le32((addr) & ~(SNDRV_TRIDENT_PAGE_SIZE-1)); \
  36. (trident)->tlb.shadow_entries[page] = (ptr); } while (0)
  37. #define __tlb_to_ptr(trident,page) \
  38. (void*)((trident)->tlb.shadow_entries[page])
  39. #define __tlb_to_addr(trident,page) \
  40. (dma_addr_t)le32_to_cpu((trident->tlb.entries[page]) & ~(SNDRV_TRIDENT_PAGE_SIZE - 1))
  41. #if PAGE_SIZE == 4096
  42. /* page size == SNDRV_TRIDENT_PAGE_SIZE */
  43. #define ALIGN_PAGE_SIZE PAGE_SIZE /* minimum page size for allocation */
  44. #define MAX_ALIGN_PAGES SNDRV_TRIDENT_MAX_PAGES /* maxmium aligned pages */
  45. /* fill TLB entrie(s) corresponding to page with ptr */
  46. #define set_tlb_bus(trident,page,ptr,addr) __set_tlb_bus(trident,page,ptr,addr)
  47. /* fill TLB entrie(s) corresponding to page with silence pointer */
  48. #define set_silent_tlb(trident,page) __set_tlb_bus(trident, page, (unsigned long)trident->tlb.silent_page.area, trident->tlb.silent_page.addr)
  49. /* get aligned page from offset address */
  50. #define get_aligned_page(offset) ((offset) >> 12)
  51. /* get offset address from aligned page */
  52. #define aligned_page_offset(page) ((page) << 12)
  53. /* get buffer address from aligned page */
  54. #define page_to_ptr(trident,page) __tlb_to_ptr(trident, page)
  55. /* get PCI physical address from aligned page */
  56. #define page_to_addr(trident,page) __tlb_to_addr(trident, page)
  57. #elif PAGE_SIZE == 8192
  58. /* page size == SNDRV_TRIDENT_PAGE_SIZE x 2*/
  59. #define ALIGN_PAGE_SIZE PAGE_SIZE
  60. #define MAX_ALIGN_PAGES (SNDRV_TRIDENT_MAX_PAGES / 2)
  61. #define get_aligned_page(offset) ((offset) >> 13)
  62. #define aligned_page_offset(page) ((page) << 13)
  63. #define page_to_ptr(trident,page) __tlb_to_ptr(trident, (page) << 1)
  64. #define page_to_addr(trident,page) __tlb_to_addr(trident, (page) << 1)
  65. /* fill TLB entries -- we need to fill two entries */
  66. static inline void set_tlb_bus(struct snd_trident *trident, int page,
  67. unsigned long ptr, dma_addr_t addr)
  68. {
  69. page <<= 1;
  70. __set_tlb_bus(trident, page, ptr, addr);
  71. __set_tlb_bus(trident, page+1, ptr + SNDRV_TRIDENT_PAGE_SIZE, addr + SNDRV_TRIDENT_PAGE_SIZE);
  72. }
  73. static inline void set_silent_tlb(struct snd_trident *trident, int page)
  74. {
  75. page <<= 1;
  76. __set_tlb_bus(trident, page, (unsigned long)trident->tlb.silent_page.area, trident->tlb.silent_page.addr);
  77. __set_tlb_bus(trident, page+1, (unsigned long)trident->tlb.silent_page.area, trident->tlb.silent_page.addr);
  78. }
  79. #else
  80. /* arbitrary size */
  81. #define UNIT_PAGES (PAGE_SIZE / SNDRV_TRIDENT_PAGE_SIZE)
  82. #define ALIGN_PAGE_SIZE (SNDRV_TRIDENT_PAGE_SIZE * UNIT_PAGES)
  83. #define MAX_ALIGN_PAGES (SNDRV_TRIDENT_MAX_PAGES / UNIT_PAGES)
  84. /* Note: if alignment doesn't match to the maximum size, the last few blocks
  85. * become unusable. To use such blocks, you'll need to check the validity
  86. * of accessing page in set_tlb_bus and set_silent_tlb. search_empty()
  87. * should also check it, too.
  88. */
  89. #define get_aligned_page(offset) ((offset) / ALIGN_PAGE_SIZE)
  90. #define aligned_page_offset(page) ((page) * ALIGN_PAGE_SIZE)
  91. #define page_to_ptr(trident,page) __tlb_to_ptr(trident, (page) * UNIT_PAGES)
  92. #define page_to_addr(trident,page) __tlb_to_addr(trident, (page) * UNIT_PAGES)
  93. /* fill TLB entries -- UNIT_PAGES entries must be filled */
  94. static inline void set_tlb_bus(struct snd_trident *trident, int page,
  95. unsigned long ptr, dma_addr_t addr)
  96. {
  97. int i;
  98. page *= UNIT_PAGES;
  99. for (i = 0; i < UNIT_PAGES; i++, page++) {
  100. __set_tlb_bus(trident, page, ptr, addr);
  101. ptr += SNDRV_TRIDENT_PAGE_SIZE;
  102. addr += SNDRV_TRIDENT_PAGE_SIZE;
  103. }
  104. }
  105. static inline void set_silent_tlb(struct snd_trident *trident, int page)
  106. {
  107. int i;
  108. page *= UNIT_PAGES;
  109. for (i = 0; i < UNIT_PAGES; i++, page++)
  110. __set_tlb_bus(trident, page, (unsigned long)trident->tlb.silent_page.area, trident->tlb.silent_page.addr);
  111. }
  112. #endif /* PAGE_SIZE */
  113. /* calculate buffer pointer from offset address */
  114. static inline void *offset_ptr(struct snd_trident *trident, int offset)
  115. {
  116. char *ptr;
  117. ptr = page_to_ptr(trident, get_aligned_page(offset));
  118. ptr += offset % ALIGN_PAGE_SIZE;
  119. return (void*)ptr;
  120. }
  121. /* first and last (aligned) pages of memory block */
  122. #define firstpg(blk) (((struct snd_trident_memblk_arg *)snd_util_memblk_argptr(blk))->first_page)
  123. #define lastpg(blk) (((struct snd_trident_memblk_arg *)snd_util_memblk_argptr(blk))->last_page)
  124. /*
  125. * search empty pages which may contain given size
  126. */
  127. static struct snd_util_memblk *
  128. search_empty(struct snd_util_memhdr *hdr, int size)
  129. {
  130. struct snd_util_memblk *blk, *prev;
  131. int page, psize;
  132. struct list_head *p;
  133. psize = get_aligned_page(size + ALIGN_PAGE_SIZE -1);
  134. prev = NULL;
  135. page = 0;
  136. list_for_each(p, &hdr->block) {
  137. blk = list_entry(p, struct snd_util_memblk, list);
  138. if (page + psize <= firstpg(blk))
  139. goto __found_pages;
  140. page = lastpg(blk) + 1;
  141. }
  142. if (page + psize > MAX_ALIGN_PAGES)
  143. return NULL;
  144. __found_pages:
  145. /* create a new memory block */
  146. blk = __snd_util_memblk_new(hdr, psize * ALIGN_PAGE_SIZE, p->prev);
  147. if (blk == NULL)
  148. return NULL;
  149. blk->offset = aligned_page_offset(page); /* set aligned offset */
  150. firstpg(blk) = page;
  151. lastpg(blk) = page + psize - 1;
  152. return blk;
  153. }
  154. /*
  155. * check if the given pointer is valid for pages
  156. */
  157. static int is_valid_page(unsigned long ptr)
  158. {
  159. if (ptr & ~0x3fffffffUL) {
  160. snd_printk(KERN_ERR "max memory size is 1GB!!\n");
  161. return 0;
  162. }
  163. if (ptr & (SNDRV_TRIDENT_PAGE_SIZE-1)) {
  164. snd_printk(KERN_ERR "page is not aligned\n");
  165. return 0;
  166. }
  167. return 1;
  168. }
  169. /*
  170. * page allocation for DMA (Scatter-Gather version)
  171. */
  172. static struct snd_util_memblk *
  173. snd_trident_alloc_sg_pages(struct snd_trident *trident,
  174. struct snd_pcm_substream *substream)
  175. {
  176. struct snd_util_memhdr *hdr;
  177. struct snd_util_memblk *blk;
  178. struct snd_pcm_runtime *runtime = substream->runtime;
  179. int idx, page;
  180. if (snd_BUG_ON(runtime->dma_bytes <= 0 ||
  181. runtime->dma_bytes > SNDRV_TRIDENT_MAX_PAGES *
  182. SNDRV_TRIDENT_PAGE_SIZE))
  183. return NULL;
  184. hdr = trident->tlb.memhdr;
  185. if (snd_BUG_ON(!hdr))
  186. return NULL;
  187. mutex_lock(&hdr->block_mutex);
  188. blk = search_empty(hdr, runtime->dma_bytes);
  189. if (blk == NULL) {
  190. mutex_unlock(&hdr->block_mutex);
  191. return NULL;
  192. }
  193. /* set TLB entries */
  194. idx = 0;
  195. for (page = firstpg(blk); page <= lastpg(blk); page++, idx++) {
  196. unsigned long ofs = idx << PAGE_SHIFT;
  197. dma_addr_t addr = snd_pcm_sgbuf_get_addr(substream, ofs);
  198. unsigned long ptr = (unsigned long)
  199. snd_pcm_sgbuf_get_ptr(substream, ofs);
  200. if (! is_valid_page(addr)) {
  201. __snd_util_mem_free(hdr, blk);
  202. mutex_unlock(&hdr->block_mutex);
  203. return NULL;
  204. }
  205. set_tlb_bus(trident, page, ptr, addr);
  206. }
  207. mutex_unlock(&hdr->block_mutex);
  208. return blk;
  209. }
  210. /*
  211. * page allocation for DMA (contiguous version)
  212. */
  213. static struct snd_util_memblk *
  214. snd_trident_alloc_cont_pages(struct snd_trident *trident,
  215. struct snd_pcm_substream *substream)
  216. {
  217. struct snd_util_memhdr *hdr;
  218. struct snd_util_memblk *blk;
  219. int page;
  220. struct snd_pcm_runtime *runtime = substream->runtime;
  221. dma_addr_t addr;
  222. unsigned long ptr;
  223. if (snd_BUG_ON(runtime->dma_bytes <= 0 ||
  224. runtime->dma_bytes > SNDRV_TRIDENT_MAX_PAGES *
  225. SNDRV_TRIDENT_PAGE_SIZE))
  226. return NULL;
  227. hdr = trident->tlb.memhdr;
  228. if (snd_BUG_ON(!hdr))
  229. return NULL;
  230. mutex_lock(&hdr->block_mutex);
  231. blk = search_empty(hdr, runtime->dma_bytes);
  232. if (blk == NULL) {
  233. mutex_unlock(&hdr->block_mutex);
  234. return NULL;
  235. }
  236. /* set TLB entries */
  237. addr = runtime->dma_addr;
  238. ptr = (unsigned long)runtime->dma_area;
  239. for (page = firstpg(blk); page <= lastpg(blk); page++,
  240. ptr += SNDRV_TRIDENT_PAGE_SIZE, addr += SNDRV_TRIDENT_PAGE_SIZE) {
  241. if (! is_valid_page(addr)) {
  242. __snd_util_mem_free(hdr, blk);
  243. mutex_unlock(&hdr->block_mutex);
  244. return NULL;
  245. }
  246. set_tlb_bus(trident, page, ptr, addr);
  247. }
  248. mutex_unlock(&hdr->block_mutex);
  249. return blk;
  250. }
  251. /*
  252. * page allocation for DMA
  253. */
  254. struct snd_util_memblk *
  255. snd_trident_alloc_pages(struct snd_trident *trident,
  256. struct snd_pcm_substream *substream)
  257. {
  258. if (snd_BUG_ON(!trident || !substream))
  259. return NULL;
  260. if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV_SG)
  261. return snd_trident_alloc_sg_pages(trident, substream);
  262. else
  263. return snd_trident_alloc_cont_pages(trident, substream);
  264. }
  265. /*
  266. * release DMA buffer from page table
  267. */
  268. int snd_trident_free_pages(struct snd_trident *trident,
  269. struct snd_util_memblk *blk)
  270. {
  271. struct snd_util_memhdr *hdr;
  272. int page;
  273. if (snd_BUG_ON(!trident || !blk))
  274. return -EINVAL;
  275. hdr = trident->tlb.memhdr;
  276. mutex_lock(&hdr->block_mutex);
  277. /* reset TLB entries */
  278. for (page = firstpg(blk); page <= lastpg(blk); page++)
  279. set_silent_tlb(trident, page);
  280. /* free memory block */
  281. __snd_util_mem_free(hdr, blk);
  282. mutex_unlock(&hdr->block_mutex);
  283. return 0;
  284. }