trident_memory.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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 <linux/io.h>
  26. #include <linux/pci.h>
  27. #include <linux/time.h>
  28. #include <linux/mutex.h>
  29. #include <sound/core.h>
  30. #include "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;
  131. int page, psize;
  132. struct list_head *p;
  133. psize = get_aligned_page(size + ALIGN_PAGE_SIZE -1);
  134. page = 0;
  135. list_for_each(p, &hdr->block) {
  136. blk = list_entry(p, struct snd_util_memblk, list);
  137. if (page + psize <= firstpg(blk))
  138. goto __found_pages;
  139. page = lastpg(blk) + 1;
  140. }
  141. if (page + psize > MAX_ALIGN_PAGES)
  142. return NULL;
  143. __found_pages:
  144. /* create a new memory block */
  145. blk = __snd_util_memblk_new(hdr, psize * ALIGN_PAGE_SIZE, p->prev);
  146. if (blk == NULL)
  147. return NULL;
  148. blk->offset = aligned_page_offset(page); /* set aligned offset */
  149. firstpg(blk) = page;
  150. lastpg(blk) = page + psize - 1;
  151. return blk;
  152. }
  153. /*
  154. * check if the given pointer is valid for pages
  155. */
  156. static int is_valid_page(unsigned long ptr)
  157. {
  158. if (ptr & ~0x3fffffffUL) {
  159. snd_printk(KERN_ERR "max memory size is 1GB!!\n");
  160. return 0;
  161. }
  162. if (ptr & (SNDRV_TRIDENT_PAGE_SIZE-1)) {
  163. snd_printk(KERN_ERR "page is not aligned\n");
  164. return 0;
  165. }
  166. return 1;
  167. }
  168. /*
  169. * page allocation for DMA (Scatter-Gather version)
  170. */
  171. static struct snd_util_memblk *
  172. snd_trident_alloc_sg_pages(struct snd_trident *trident,
  173. struct snd_pcm_substream *substream)
  174. {
  175. struct snd_util_memhdr *hdr;
  176. struct snd_util_memblk *blk;
  177. struct snd_pcm_runtime *runtime = substream->runtime;
  178. int idx, page;
  179. if (snd_BUG_ON(runtime->dma_bytes <= 0 ||
  180. runtime->dma_bytes > SNDRV_TRIDENT_MAX_PAGES *
  181. SNDRV_TRIDENT_PAGE_SIZE))
  182. return NULL;
  183. hdr = trident->tlb.memhdr;
  184. if (snd_BUG_ON(!hdr))
  185. return NULL;
  186. mutex_lock(&hdr->block_mutex);
  187. blk = search_empty(hdr, runtime->dma_bytes);
  188. if (blk == NULL) {
  189. mutex_unlock(&hdr->block_mutex);
  190. return NULL;
  191. }
  192. /* set TLB entries */
  193. idx = 0;
  194. for (page = firstpg(blk); page <= lastpg(blk); page++, idx++) {
  195. unsigned long ofs = idx << PAGE_SHIFT;
  196. dma_addr_t addr = snd_pcm_sgbuf_get_addr(substream, ofs);
  197. unsigned long ptr = (unsigned long)
  198. snd_pcm_sgbuf_get_ptr(substream, ofs);
  199. if (! is_valid_page(addr)) {
  200. __snd_util_mem_free(hdr, blk);
  201. mutex_unlock(&hdr->block_mutex);
  202. return NULL;
  203. }
  204. set_tlb_bus(trident, page, ptr, addr);
  205. }
  206. mutex_unlock(&hdr->block_mutex);
  207. return blk;
  208. }
  209. /*
  210. * page allocation for DMA (contiguous version)
  211. */
  212. static struct snd_util_memblk *
  213. snd_trident_alloc_cont_pages(struct snd_trident *trident,
  214. struct snd_pcm_substream *substream)
  215. {
  216. struct snd_util_memhdr *hdr;
  217. struct snd_util_memblk *blk;
  218. int page;
  219. struct snd_pcm_runtime *runtime = substream->runtime;
  220. dma_addr_t addr;
  221. unsigned long ptr;
  222. if (snd_BUG_ON(runtime->dma_bytes <= 0 ||
  223. runtime->dma_bytes > SNDRV_TRIDENT_MAX_PAGES *
  224. SNDRV_TRIDENT_PAGE_SIZE))
  225. return NULL;
  226. hdr = trident->tlb.memhdr;
  227. if (snd_BUG_ON(!hdr))
  228. return NULL;
  229. mutex_lock(&hdr->block_mutex);
  230. blk = search_empty(hdr, runtime->dma_bytes);
  231. if (blk == NULL) {
  232. mutex_unlock(&hdr->block_mutex);
  233. return NULL;
  234. }
  235. /* set TLB entries */
  236. addr = runtime->dma_addr;
  237. ptr = (unsigned long)runtime->dma_area;
  238. for (page = firstpg(blk); page <= lastpg(blk); page++,
  239. ptr += SNDRV_TRIDENT_PAGE_SIZE, addr += SNDRV_TRIDENT_PAGE_SIZE) {
  240. if (! is_valid_page(addr)) {
  241. __snd_util_mem_free(hdr, blk);
  242. mutex_unlock(&hdr->block_mutex);
  243. return NULL;
  244. }
  245. set_tlb_bus(trident, page, ptr, addr);
  246. }
  247. mutex_unlock(&hdr->block_mutex);
  248. return blk;
  249. }
  250. /*
  251. * page allocation for DMA
  252. */
  253. struct snd_util_memblk *
  254. snd_trident_alloc_pages(struct snd_trident *trident,
  255. struct snd_pcm_substream *substream)
  256. {
  257. if (snd_BUG_ON(!trident || !substream))
  258. return NULL;
  259. if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV_SG)
  260. return snd_trident_alloc_sg_pages(trident, substream);
  261. else
  262. return snd_trident_alloc_cont_pages(trident, substream);
  263. }
  264. /*
  265. * release DMA buffer from page table
  266. */
  267. int snd_trident_free_pages(struct snd_trident *trident,
  268. struct snd_util_memblk *blk)
  269. {
  270. struct snd_util_memhdr *hdr;
  271. int page;
  272. if (snd_BUG_ON(!trident || !blk))
  273. return -EINVAL;
  274. hdr = trident->tlb.memhdr;
  275. mutex_lock(&hdr->block_mutex);
  276. /* reset TLB entries */
  277. for (page = firstpg(blk); page <= lastpg(blk); page++)
  278. set_silent_tlb(trident, page);
  279. /* free memory block */
  280. __snd_util_mem_free(hdr, blk);
  281. mutex_unlock(&hdr->block_mutex);
  282. return 0;
  283. }