trident_memory.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. /*
  2. * Copyright (c) by Jaroslav Kysela <perex@suse.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 <sound/driver.h>
  26. #include <asm/io.h>
  27. #include <linux/pci.h>
  28. #include <linux/time.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(trident_t *trident, int page, unsigned long ptr, dma_addr_t addr)
  67. {
  68. page <<= 1;
  69. __set_tlb_bus(trident, page, ptr, addr);
  70. __set_tlb_bus(trident, page+1, ptr + SNDRV_TRIDENT_PAGE_SIZE, addr + SNDRV_TRIDENT_PAGE_SIZE);
  71. }
  72. static inline void set_silent_tlb(trident_t *trident, int page)
  73. {
  74. page <<= 1;
  75. __set_tlb_bus(trident, page, (unsigned long)trident->tlb.silent_page.area, trident->tlb.silent_page.addr);
  76. __set_tlb_bus(trident, page+1, (unsigned long)trident->tlb.silent_page.area, trident->tlb.silent_page.addr);
  77. }
  78. #else
  79. /* arbitrary size */
  80. #define UNIT_PAGES (PAGE_SIZE / SNDRV_TRIDENT_PAGE_SIZE)
  81. #define ALIGN_PAGE_SIZE (SNDRV_TRIDENT_PAGE_SIZE * UNIT_PAGES)
  82. #define MAX_ALIGN_PAGES (SNDRV_TRIDENT_MAX_PAGES / UNIT_PAGES)
  83. /* Note: if alignment doesn't match to the maximum size, the last few blocks
  84. * become unusable. To use such blocks, you'll need to check the validity
  85. * of accessing page in set_tlb_bus and set_silent_tlb. search_empty()
  86. * should also check it, too.
  87. */
  88. #define get_aligned_page(offset) ((offset) / ALIGN_PAGE_SIZE)
  89. #define aligned_page_offset(page) ((page) * ALIGN_PAGE_SIZE)
  90. #define page_to_ptr(trident,page) __tlb_to_ptr(trident, (page) * UNIT_PAGES)
  91. #define page_to_addr(trident,page) __tlb_to_addr(trident, (page) * UNIT_PAGES)
  92. /* fill TLB entries -- UNIT_PAGES entries must be filled */
  93. static inline void set_tlb_bus(trident_t *trident, int page, unsigned long ptr, dma_addr_t addr)
  94. {
  95. int i;
  96. page *= UNIT_PAGES;
  97. for (i = 0; i < UNIT_PAGES; i++, page++) {
  98. __set_tlb_bus(trident, page, ptr, addr);
  99. ptr += SNDRV_TRIDENT_PAGE_SIZE;
  100. addr += SNDRV_TRIDENT_PAGE_SIZE;
  101. }
  102. }
  103. static inline void set_silent_tlb(trident_t *trident, int page)
  104. {
  105. int i;
  106. page *= UNIT_PAGES;
  107. for (i = 0; i < UNIT_PAGES; i++, page++)
  108. __set_tlb_bus(trident, page, (unsigned long)trident->tlb.silent_page.area, trident->tlb.silent_page.addr);
  109. }
  110. #endif /* PAGE_SIZE */
  111. /* calculate buffer pointer from offset address */
  112. inline static void *offset_ptr(trident_t *trident, int offset)
  113. {
  114. char *ptr;
  115. ptr = page_to_ptr(trident, get_aligned_page(offset));
  116. ptr += offset % ALIGN_PAGE_SIZE;
  117. return (void*)ptr;
  118. }
  119. /* first and last (aligned) pages of memory block */
  120. #define firstpg(blk) (((snd_trident_memblk_arg_t*)snd_util_memblk_argptr(blk))->first_page)
  121. #define lastpg(blk) (((snd_trident_memblk_arg_t*)snd_util_memblk_argptr(blk))->last_page)
  122. /*
  123. * search empty pages which may contain given size
  124. */
  125. static snd_util_memblk_t *
  126. search_empty(snd_util_memhdr_t *hdr, int size)
  127. {
  128. snd_util_memblk_t *blk, *prev;
  129. int page, psize;
  130. struct list_head *p;
  131. psize = get_aligned_page(size + ALIGN_PAGE_SIZE -1);
  132. prev = NULL;
  133. page = 0;
  134. list_for_each(p, &hdr->block) {
  135. blk = list_entry(p, snd_util_memblk_t, list);
  136. if (page + psize <= firstpg(blk))
  137. goto __found_pages;
  138. page = lastpg(blk) + 1;
  139. }
  140. if (page + psize > MAX_ALIGN_PAGES)
  141. return NULL;
  142. __found_pages:
  143. /* create a new memory block */
  144. blk = __snd_util_memblk_new(hdr, psize * ALIGN_PAGE_SIZE, p->prev);
  145. if (blk == NULL)
  146. return NULL;
  147. blk->offset = aligned_page_offset(page); /* set aligned offset */
  148. firstpg(blk) = page;
  149. lastpg(blk) = page + psize - 1;
  150. return blk;
  151. }
  152. /*
  153. * check if the given pointer is valid for pages
  154. */
  155. static int is_valid_page(unsigned long ptr)
  156. {
  157. if (ptr & ~0x3fffffffUL) {
  158. snd_printk("max memory size is 1GB!!\n");
  159. return 0;
  160. }
  161. if (ptr & (SNDRV_TRIDENT_PAGE_SIZE-1)) {
  162. snd_printk("page is not aligned\n");
  163. return 0;
  164. }
  165. return 1;
  166. }
  167. /*
  168. * page allocation for DMA (Scatter-Gather version)
  169. */
  170. static snd_util_memblk_t *
  171. snd_trident_alloc_sg_pages(trident_t *trident, snd_pcm_substream_t *substream)
  172. {
  173. snd_util_memhdr_t *hdr;
  174. snd_util_memblk_t *blk;
  175. snd_pcm_runtime_t *runtime = substream->runtime;
  176. int idx, page;
  177. struct snd_sg_buf *sgbuf = snd_pcm_substream_sgbuf(substream);
  178. snd_assert(runtime->dma_bytes > 0 && runtime->dma_bytes <= SNDRV_TRIDENT_MAX_PAGES * SNDRV_TRIDENT_PAGE_SIZE, return NULL);
  179. hdr = trident->tlb.memhdr;
  180. snd_assert(hdr != NULL, return NULL);
  181. down(&hdr->block_mutex);
  182. blk = search_empty(hdr, runtime->dma_bytes);
  183. if (blk == NULL) {
  184. up(&hdr->block_mutex);
  185. return NULL;
  186. }
  187. if (lastpg(blk) - firstpg(blk) >= sgbuf->pages) {
  188. snd_printk(KERN_ERR "page calculation doesn't match: allocated pages = %d, trident = %d/%d\n", sgbuf->pages, firstpg(blk), lastpg(blk));
  189. __snd_util_mem_free(hdr, blk);
  190. up(&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. dma_addr_t addr = sgbuf->table[idx].addr;
  197. unsigned long ptr = (unsigned long)sgbuf->table[idx].buf;
  198. if (! is_valid_page(addr)) {
  199. __snd_util_mem_free(hdr, blk);
  200. up(&hdr->block_mutex);
  201. return NULL;
  202. }
  203. set_tlb_bus(trident, page, ptr, addr);
  204. }
  205. up(&hdr->block_mutex);
  206. return blk;
  207. }
  208. /*
  209. * page allocation for DMA (contiguous version)
  210. */
  211. static snd_util_memblk_t *
  212. snd_trident_alloc_cont_pages(trident_t *trident, snd_pcm_substream_t *substream)
  213. {
  214. snd_util_memhdr_t *hdr;
  215. snd_util_memblk_t *blk;
  216. int page;
  217. snd_pcm_runtime_t *runtime = substream->runtime;
  218. dma_addr_t addr;
  219. unsigned long ptr;
  220. snd_assert(runtime->dma_bytes> 0 && runtime->dma_bytes <= SNDRV_TRIDENT_MAX_PAGES * SNDRV_TRIDENT_PAGE_SIZE, return NULL);
  221. hdr = trident->tlb.memhdr;
  222. snd_assert(hdr != NULL, return NULL);
  223. down(&hdr->block_mutex);
  224. blk = search_empty(hdr, runtime->dma_bytes);
  225. if (blk == NULL) {
  226. up(&hdr->block_mutex);
  227. return NULL;
  228. }
  229. /* set TLB entries */
  230. addr = runtime->dma_addr;
  231. ptr = (unsigned long)runtime->dma_area;
  232. for (page = firstpg(blk); page <= lastpg(blk); page++,
  233. ptr += SNDRV_TRIDENT_PAGE_SIZE, addr += SNDRV_TRIDENT_PAGE_SIZE) {
  234. if (! is_valid_page(addr)) {
  235. __snd_util_mem_free(hdr, blk);
  236. up(&hdr->block_mutex);
  237. return NULL;
  238. }
  239. set_tlb_bus(trident, page, ptr, addr);
  240. }
  241. up(&hdr->block_mutex);
  242. return blk;
  243. }
  244. /*
  245. * page allocation for DMA
  246. */
  247. snd_util_memblk_t *
  248. snd_trident_alloc_pages(trident_t *trident, snd_pcm_substream_t *substream)
  249. {
  250. snd_assert(trident != NULL, return NULL);
  251. snd_assert(substream != NULL, return NULL);
  252. if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV_SG)
  253. return snd_trident_alloc_sg_pages(trident, substream);
  254. else
  255. return snd_trident_alloc_cont_pages(trident, substream);
  256. }
  257. /*
  258. * release DMA buffer from page table
  259. */
  260. int snd_trident_free_pages(trident_t *trident, snd_util_memblk_t *blk)
  261. {
  262. snd_util_memhdr_t *hdr;
  263. int page;
  264. snd_assert(trident != NULL, return -EINVAL);
  265. snd_assert(blk != NULL, return -EINVAL);
  266. hdr = trident->tlb.memhdr;
  267. down(&hdr->block_mutex);
  268. /* reset TLB entries */
  269. for (page = firstpg(blk); page <= lastpg(blk); page++)
  270. set_silent_tlb(trident, page);
  271. /* free memory block */
  272. __snd_util_mem_free(hdr, blk);
  273. up(&hdr->block_mutex);
  274. return 0;
  275. }
  276. /*----------------------------------------------------------------
  277. * memory allocation using multiple pages (for synth)
  278. *----------------------------------------------------------------
  279. * Unlike the DMA allocation above, non-contiguous pages are
  280. * assigned to TLB.
  281. *----------------------------------------------------------------*/
  282. /*
  283. */
  284. static int synth_alloc_pages(trident_t *hw, snd_util_memblk_t *blk);
  285. static int synth_free_pages(trident_t *hw, snd_util_memblk_t *blk);
  286. /*
  287. * allocate a synth sample area
  288. */
  289. snd_util_memblk_t *
  290. snd_trident_synth_alloc(trident_t *hw, unsigned int size)
  291. {
  292. snd_util_memblk_t *blk;
  293. snd_util_memhdr_t *hdr = hw->tlb.memhdr;
  294. down(&hdr->block_mutex);
  295. blk = __snd_util_mem_alloc(hdr, size);
  296. if (blk == NULL) {
  297. up(&hdr->block_mutex);
  298. return NULL;
  299. }
  300. if (synth_alloc_pages(hw, blk)) {
  301. __snd_util_mem_free(hdr, blk);
  302. up(&hdr->block_mutex);
  303. return NULL;
  304. }
  305. up(&hdr->block_mutex);
  306. return blk;
  307. }
  308. /*
  309. * free a synth sample area
  310. */
  311. int
  312. snd_trident_synth_free(trident_t *hw, snd_util_memblk_t *blk)
  313. {
  314. snd_util_memhdr_t *hdr = hw->tlb.memhdr;
  315. down(&hdr->block_mutex);
  316. synth_free_pages(hw, blk);
  317. __snd_util_mem_free(hdr, blk);
  318. up(&hdr->block_mutex);
  319. return 0;
  320. }
  321. /*
  322. * reset TLB entry and free kernel page
  323. */
  324. static void clear_tlb(trident_t *trident, int page)
  325. {
  326. void *ptr = page_to_ptr(trident, page);
  327. dma_addr_t addr = page_to_addr(trident, page);
  328. set_silent_tlb(trident, page);
  329. if (ptr) {
  330. struct snd_dma_buffer dmab;
  331. dmab.dev.type = SNDRV_DMA_TYPE_DEV;
  332. dmab.dev.dev = snd_dma_pci_data(trident->pci);
  333. dmab.area = ptr;
  334. dmab.addr = addr;
  335. dmab.bytes = ALIGN_PAGE_SIZE;
  336. snd_dma_free_pages(&dmab);
  337. }
  338. }
  339. /* check new allocation range */
  340. static void get_single_page_range(snd_util_memhdr_t *hdr, snd_util_memblk_t *blk, int *first_page_ret, int *last_page_ret)
  341. {
  342. struct list_head *p;
  343. snd_util_memblk_t *q;
  344. int first_page, last_page;
  345. first_page = firstpg(blk);
  346. if ((p = blk->list.prev) != &hdr->block) {
  347. q = list_entry(p, snd_util_memblk_t, list);
  348. if (lastpg(q) == first_page)
  349. first_page++; /* first page was already allocated */
  350. }
  351. last_page = lastpg(blk);
  352. if ((p = blk->list.next) != &hdr->block) {
  353. q = list_entry(p, snd_util_memblk_t, list);
  354. if (firstpg(q) == last_page)
  355. last_page--; /* last page was already allocated */
  356. }
  357. *first_page_ret = first_page;
  358. *last_page_ret = last_page;
  359. }
  360. /*
  361. * allocate kernel pages and assign them to TLB
  362. */
  363. static int synth_alloc_pages(trident_t *hw, snd_util_memblk_t *blk)
  364. {
  365. int page, first_page, last_page;
  366. struct snd_dma_buffer dmab;
  367. firstpg(blk) = get_aligned_page(blk->offset);
  368. lastpg(blk) = get_aligned_page(blk->offset + blk->size - 1);
  369. get_single_page_range(hw->tlb.memhdr, blk, &first_page, &last_page);
  370. /* allocate a kernel page for each Trident page -
  371. * fortunately Trident page size and kernel PAGE_SIZE is identical!
  372. */
  373. for (page = first_page; page <= last_page; page++) {
  374. if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(hw->pci),
  375. ALIGN_PAGE_SIZE, &dmab) < 0)
  376. goto __fail;
  377. if (! is_valid_page(dmab.addr)) {
  378. snd_dma_free_pages(&dmab);
  379. goto __fail;
  380. }
  381. set_tlb_bus(hw, page, (unsigned long)dmab.area, dmab.addr);
  382. }
  383. return 0;
  384. __fail:
  385. /* release allocated pages */
  386. last_page = page - 1;
  387. for (page = first_page; page <= last_page; page++)
  388. clear_tlb(hw, page);
  389. return -ENOMEM;
  390. }
  391. /*
  392. * free pages
  393. */
  394. static int synth_free_pages(trident_t *trident, snd_util_memblk_t *blk)
  395. {
  396. int page, first_page, last_page;
  397. get_single_page_range(trident->tlb.memhdr, blk, &first_page, &last_page);
  398. for (page = first_page; page <= last_page; page++)
  399. clear_tlb(trident, page);
  400. return 0;
  401. }
  402. /*
  403. * copy_from_user(blk + offset, data, size)
  404. */
  405. int snd_trident_synth_copy_from_user(trident_t *trident, snd_util_memblk_t *blk, int offset, const char __user *data, int size)
  406. {
  407. int page, nextofs, end_offset, temp, temp1;
  408. offset += blk->offset;
  409. end_offset = offset + size;
  410. page = get_aligned_page(offset) + 1;
  411. do {
  412. nextofs = aligned_page_offset(page);
  413. temp = nextofs - offset;
  414. temp1 = end_offset - offset;
  415. if (temp1 < temp)
  416. temp = temp1;
  417. if (copy_from_user(offset_ptr(trident, offset), data, temp))
  418. return -EFAULT;
  419. offset = nextofs;
  420. data += temp;
  421. page++;
  422. } while (offset < end_offset);
  423. return 0;
  424. }