trident_memory.c 15 KB

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