trident_memory.c 8.0 KB

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