trident_memory.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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(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. struct snd_sg_buf *sgbuf = snd_pcm_substream_sgbuf(substream);
  181. snd_assert(runtime->dma_bytes > 0 && runtime->dma_bytes <= SNDRV_TRIDENT_MAX_PAGES * SNDRV_TRIDENT_PAGE_SIZE, return NULL);
  182. hdr = trident->tlb.memhdr;
  183. snd_assert(hdr != NULL, return NULL);
  184. down(&hdr->block_mutex);
  185. blk = search_empty(hdr, runtime->dma_bytes);
  186. if (blk == NULL) {
  187. up(&hdr->block_mutex);
  188. return NULL;
  189. }
  190. if (lastpg(blk) - firstpg(blk) >= sgbuf->pages) {
  191. snd_printk(KERN_ERR "page calculation doesn't match: allocated pages = %d, trident = %d/%d\n", sgbuf->pages, firstpg(blk), lastpg(blk));
  192. __snd_util_mem_free(hdr, blk);
  193. up(&hdr->block_mutex);
  194. return NULL;
  195. }
  196. /* set TLB entries */
  197. idx = 0;
  198. for (page = firstpg(blk); page <= lastpg(blk); page++, idx++) {
  199. dma_addr_t addr = sgbuf->table[idx].addr;
  200. unsigned long ptr = (unsigned long)sgbuf->table[idx].buf;
  201. if (! is_valid_page(addr)) {
  202. __snd_util_mem_free(hdr, blk);
  203. up(&hdr->block_mutex);
  204. return NULL;
  205. }
  206. set_tlb_bus(trident, page, ptr, addr);
  207. }
  208. up(&hdr->block_mutex);
  209. return blk;
  210. }
  211. /*
  212. * page allocation for DMA (contiguous version)
  213. */
  214. static struct snd_util_memblk *
  215. snd_trident_alloc_cont_pages(struct snd_trident *trident,
  216. struct snd_pcm_substream *substream)
  217. {
  218. struct snd_util_memhdr *hdr;
  219. struct snd_util_memblk *blk;
  220. int page;
  221. struct snd_pcm_runtime *runtime = substream->runtime;
  222. dma_addr_t addr;
  223. unsigned long ptr;
  224. snd_assert(runtime->dma_bytes> 0 && runtime->dma_bytes <= SNDRV_TRIDENT_MAX_PAGES * SNDRV_TRIDENT_PAGE_SIZE, return NULL);
  225. hdr = trident->tlb.memhdr;
  226. snd_assert(hdr != NULL, return NULL);
  227. down(&hdr->block_mutex);
  228. blk = search_empty(hdr, runtime->dma_bytes);
  229. if (blk == NULL) {
  230. up(&hdr->block_mutex);
  231. return NULL;
  232. }
  233. /* set TLB entries */
  234. addr = runtime->dma_addr;
  235. ptr = (unsigned long)runtime->dma_area;
  236. for (page = firstpg(blk); page <= lastpg(blk); page++,
  237. ptr += SNDRV_TRIDENT_PAGE_SIZE, addr += SNDRV_TRIDENT_PAGE_SIZE) {
  238. if (! is_valid_page(addr)) {
  239. __snd_util_mem_free(hdr, blk);
  240. up(&hdr->block_mutex);
  241. return NULL;
  242. }
  243. set_tlb_bus(trident, page, ptr, addr);
  244. }
  245. up(&hdr->block_mutex);
  246. return blk;
  247. }
  248. /*
  249. * page allocation for DMA
  250. */
  251. struct snd_util_memblk *
  252. snd_trident_alloc_pages(struct snd_trident *trident,
  253. struct snd_pcm_substream *substream)
  254. {
  255. snd_assert(trident != NULL, return NULL);
  256. snd_assert(substream != NULL, return NULL);
  257. if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV_SG)
  258. return snd_trident_alloc_sg_pages(trident, substream);
  259. else
  260. return snd_trident_alloc_cont_pages(trident, substream);
  261. }
  262. /*
  263. * release DMA buffer from page table
  264. */
  265. int snd_trident_free_pages(struct snd_trident *trident,
  266. struct snd_util_memblk *blk)
  267. {
  268. struct snd_util_memhdr *hdr;
  269. int page;
  270. snd_assert(trident != NULL, return -EINVAL);
  271. snd_assert(blk != NULL, return -EINVAL);
  272. hdr = trident->tlb.memhdr;
  273. down(&hdr->block_mutex);
  274. /* reset TLB entries */
  275. for (page = firstpg(blk); page <= lastpg(blk); page++)
  276. set_silent_tlb(trident, page);
  277. /* free memory block */
  278. __snd_util_mem_free(hdr, blk);
  279. up(&hdr->block_mutex);
  280. return 0;
  281. }
  282. /*----------------------------------------------------------------
  283. * memory allocation using multiple pages (for synth)
  284. *----------------------------------------------------------------
  285. * Unlike the DMA allocation above, non-contiguous pages are
  286. * assigned to TLB.
  287. *----------------------------------------------------------------*/
  288. /*
  289. */
  290. static int synth_alloc_pages(struct snd_trident *hw, struct snd_util_memblk *blk);
  291. static int synth_free_pages(struct snd_trident *hw, struct snd_util_memblk *blk);
  292. /*
  293. * allocate a synth sample area
  294. */
  295. struct snd_util_memblk *
  296. snd_trident_synth_alloc(struct snd_trident *hw, unsigned int size)
  297. {
  298. struct snd_util_memblk *blk;
  299. struct snd_util_memhdr *hdr = hw->tlb.memhdr;
  300. down(&hdr->block_mutex);
  301. blk = __snd_util_mem_alloc(hdr, size);
  302. if (blk == NULL) {
  303. up(&hdr->block_mutex);
  304. return NULL;
  305. }
  306. if (synth_alloc_pages(hw, blk)) {
  307. __snd_util_mem_free(hdr, blk);
  308. up(&hdr->block_mutex);
  309. return NULL;
  310. }
  311. up(&hdr->block_mutex);
  312. return blk;
  313. }
  314. /*
  315. * free a synth sample area
  316. */
  317. int
  318. snd_trident_synth_free(struct snd_trident *hw, struct snd_util_memblk *blk)
  319. {
  320. struct snd_util_memhdr *hdr = hw->tlb.memhdr;
  321. down(&hdr->block_mutex);
  322. synth_free_pages(hw, blk);
  323. __snd_util_mem_free(hdr, blk);
  324. up(&hdr->block_mutex);
  325. return 0;
  326. }
  327. /*
  328. * reset TLB entry and free kernel page
  329. */
  330. static void clear_tlb(struct snd_trident *trident, int page)
  331. {
  332. void *ptr = page_to_ptr(trident, page);
  333. dma_addr_t addr = page_to_addr(trident, page);
  334. set_silent_tlb(trident, page);
  335. if (ptr) {
  336. struct snd_dma_buffer dmab;
  337. dmab.dev.type = SNDRV_DMA_TYPE_DEV;
  338. dmab.dev.dev = snd_dma_pci_data(trident->pci);
  339. dmab.area = ptr;
  340. dmab.addr = addr;
  341. dmab.bytes = ALIGN_PAGE_SIZE;
  342. snd_dma_free_pages(&dmab);
  343. }
  344. }
  345. /* check new allocation range */
  346. static void get_single_page_range(struct snd_util_memhdr *hdr,
  347. struct snd_util_memblk *blk,
  348. int *first_page_ret, int *last_page_ret)
  349. {
  350. struct list_head *p;
  351. struct snd_util_memblk *q;
  352. int first_page, last_page;
  353. first_page = firstpg(blk);
  354. if ((p = blk->list.prev) != &hdr->block) {
  355. q = list_entry(p, struct snd_util_memblk, list);
  356. if (lastpg(q) == first_page)
  357. first_page++; /* first page was already allocated */
  358. }
  359. last_page = lastpg(blk);
  360. if ((p = blk->list.next) != &hdr->block) {
  361. q = list_entry(p, struct snd_util_memblk, list);
  362. if (firstpg(q) == last_page)
  363. last_page--; /* last page was already allocated */
  364. }
  365. *first_page_ret = first_page;
  366. *last_page_ret = last_page;
  367. }
  368. /*
  369. * allocate kernel pages and assign them to TLB
  370. */
  371. static int synth_alloc_pages(struct snd_trident *hw, struct snd_util_memblk *blk)
  372. {
  373. int page, first_page, last_page;
  374. struct snd_dma_buffer dmab;
  375. firstpg(blk) = get_aligned_page(blk->offset);
  376. lastpg(blk) = get_aligned_page(blk->offset + blk->size - 1);
  377. get_single_page_range(hw->tlb.memhdr, blk, &first_page, &last_page);
  378. /* allocate a kernel page for each Trident page -
  379. * fortunately Trident page size and kernel PAGE_SIZE is identical!
  380. */
  381. for (page = first_page; page <= last_page; page++) {
  382. if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(hw->pci),
  383. ALIGN_PAGE_SIZE, &dmab) < 0)
  384. goto __fail;
  385. if (! is_valid_page(dmab.addr)) {
  386. snd_dma_free_pages(&dmab);
  387. goto __fail;
  388. }
  389. set_tlb_bus(hw, page, (unsigned long)dmab.area, dmab.addr);
  390. }
  391. return 0;
  392. __fail:
  393. /* release allocated pages */
  394. last_page = page - 1;
  395. for (page = first_page; page <= last_page; page++)
  396. clear_tlb(hw, page);
  397. return -ENOMEM;
  398. }
  399. /*
  400. * free pages
  401. */
  402. static int synth_free_pages(struct snd_trident *trident, struct snd_util_memblk *blk)
  403. {
  404. int page, first_page, last_page;
  405. get_single_page_range(trident->tlb.memhdr, blk, &first_page, &last_page);
  406. for (page = first_page; page <= last_page; page++)
  407. clear_tlb(trident, page);
  408. return 0;
  409. }
  410. /*
  411. * copy_from_user(blk + offset, data, size)
  412. */
  413. int snd_trident_synth_copy_from_user(struct snd_trident *trident,
  414. struct snd_util_memblk *blk,
  415. int offset, const char __user *data, int size)
  416. {
  417. int page, nextofs, end_offset, temp, temp1;
  418. offset += blk->offset;
  419. end_offset = offset + size;
  420. page = get_aligned_page(offset) + 1;
  421. do {
  422. nextofs = aligned_page_offset(page);
  423. temp = nextofs - offset;
  424. temp1 = end_offset - offset;
  425. if (temp1 < temp)
  426. temp = temp1;
  427. if (copy_from_user(offset_ptr(trident, offset), data, temp))
  428. return -EFAULT;
  429. offset = nextofs;
  430. data += temp;
  431. page++;
  432. } while (offset < end_offset);
  433. return 0;
  434. }