18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright (c) by Jaroslav Kysela <perex@perex.cz> 48c2ecf20Sopenharmony_ci * Copyright (c) by Takashi Iwai <tiwai@suse.de> 58c2ecf20Sopenharmony_ci * Copyright (c) by Scott McNab <sdm@fractalgraphics.com.au> 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * Trident 4DWave-NX memory page allocation (TLB area) 88c2ecf20Sopenharmony_ci * Trident chip can handle only 16MByte of the memory at the same time. 98c2ecf20Sopenharmony_ci */ 108c2ecf20Sopenharmony_ci 118c2ecf20Sopenharmony_ci#include <linux/io.h> 128c2ecf20Sopenharmony_ci#include <linux/pci.h> 138c2ecf20Sopenharmony_ci#include <linux/time.h> 148c2ecf20Sopenharmony_ci#include <linux/mutex.h> 158c2ecf20Sopenharmony_ci 168c2ecf20Sopenharmony_ci#include <sound/core.h> 178c2ecf20Sopenharmony_ci#include "trident.h" 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_ci/* page arguments of these two macros are Trident page (4096 bytes), not like 208c2ecf20Sopenharmony_ci * aligned pages in others 218c2ecf20Sopenharmony_ci */ 228c2ecf20Sopenharmony_ci#define __set_tlb_bus(trident,page,ptr,addr) \ 238c2ecf20Sopenharmony_ci do { (trident)->tlb.entries[page] = cpu_to_le32((addr) & ~(SNDRV_TRIDENT_PAGE_SIZE-1)); \ 248c2ecf20Sopenharmony_ci (trident)->tlb.shadow_entries[page] = (ptr); } while (0) 258c2ecf20Sopenharmony_ci#define __tlb_to_ptr(trident,page) \ 268c2ecf20Sopenharmony_ci (void*)((trident)->tlb.shadow_entries[page]) 278c2ecf20Sopenharmony_ci#define __tlb_to_addr(trident,page) \ 288c2ecf20Sopenharmony_ci (dma_addr_t)le32_to_cpu((trident->tlb.entries[page]) & ~(SNDRV_TRIDENT_PAGE_SIZE - 1)) 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_ci#if PAGE_SIZE == 4096 318c2ecf20Sopenharmony_ci/* page size == SNDRV_TRIDENT_PAGE_SIZE */ 328c2ecf20Sopenharmony_ci#define ALIGN_PAGE_SIZE PAGE_SIZE /* minimum page size for allocation */ 338c2ecf20Sopenharmony_ci#define MAX_ALIGN_PAGES SNDRV_TRIDENT_MAX_PAGES /* maxmium aligned pages */ 348c2ecf20Sopenharmony_ci/* fill TLB entrie(s) corresponding to page with ptr */ 358c2ecf20Sopenharmony_ci#define set_tlb_bus(trident,page,ptr,addr) __set_tlb_bus(trident,page,ptr,addr) 368c2ecf20Sopenharmony_ci/* fill TLB entrie(s) corresponding to page with silence pointer */ 378c2ecf20Sopenharmony_ci#define set_silent_tlb(trident,page) __set_tlb_bus(trident, page, (unsigned long)trident->tlb.silent_page.area, trident->tlb.silent_page.addr) 388c2ecf20Sopenharmony_ci/* get aligned page from offset address */ 398c2ecf20Sopenharmony_ci#define get_aligned_page(offset) ((offset) >> 12) 408c2ecf20Sopenharmony_ci/* get offset address from aligned page */ 418c2ecf20Sopenharmony_ci#define aligned_page_offset(page) ((page) << 12) 428c2ecf20Sopenharmony_ci/* get buffer address from aligned page */ 438c2ecf20Sopenharmony_ci#define page_to_ptr(trident,page) __tlb_to_ptr(trident, page) 448c2ecf20Sopenharmony_ci/* get PCI physical address from aligned page */ 458c2ecf20Sopenharmony_ci#define page_to_addr(trident,page) __tlb_to_addr(trident, page) 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ci#elif PAGE_SIZE == 8192 488c2ecf20Sopenharmony_ci/* page size == SNDRV_TRIDENT_PAGE_SIZE x 2*/ 498c2ecf20Sopenharmony_ci#define ALIGN_PAGE_SIZE PAGE_SIZE 508c2ecf20Sopenharmony_ci#define MAX_ALIGN_PAGES (SNDRV_TRIDENT_MAX_PAGES / 2) 518c2ecf20Sopenharmony_ci#define get_aligned_page(offset) ((offset) >> 13) 528c2ecf20Sopenharmony_ci#define aligned_page_offset(page) ((page) << 13) 538c2ecf20Sopenharmony_ci#define page_to_ptr(trident,page) __tlb_to_ptr(trident, (page) << 1) 548c2ecf20Sopenharmony_ci#define page_to_addr(trident,page) __tlb_to_addr(trident, (page) << 1) 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_ci/* fill TLB entries -- we need to fill two entries */ 578c2ecf20Sopenharmony_cistatic inline void set_tlb_bus(struct snd_trident *trident, int page, 588c2ecf20Sopenharmony_ci unsigned long ptr, dma_addr_t addr) 598c2ecf20Sopenharmony_ci{ 608c2ecf20Sopenharmony_ci page <<= 1; 618c2ecf20Sopenharmony_ci __set_tlb_bus(trident, page, ptr, addr); 628c2ecf20Sopenharmony_ci __set_tlb_bus(trident, page+1, ptr + SNDRV_TRIDENT_PAGE_SIZE, addr + SNDRV_TRIDENT_PAGE_SIZE); 638c2ecf20Sopenharmony_ci} 648c2ecf20Sopenharmony_cistatic inline void set_silent_tlb(struct snd_trident *trident, int page) 658c2ecf20Sopenharmony_ci{ 668c2ecf20Sopenharmony_ci page <<= 1; 678c2ecf20Sopenharmony_ci __set_tlb_bus(trident, page, (unsigned long)trident->tlb.silent_page.area, trident->tlb.silent_page.addr); 688c2ecf20Sopenharmony_ci __set_tlb_bus(trident, page+1, (unsigned long)trident->tlb.silent_page.area, trident->tlb.silent_page.addr); 698c2ecf20Sopenharmony_ci} 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_ci#else 728c2ecf20Sopenharmony_ci/* arbitrary size */ 738c2ecf20Sopenharmony_ci#define UNIT_PAGES (PAGE_SIZE / SNDRV_TRIDENT_PAGE_SIZE) 748c2ecf20Sopenharmony_ci#define ALIGN_PAGE_SIZE (SNDRV_TRIDENT_PAGE_SIZE * UNIT_PAGES) 758c2ecf20Sopenharmony_ci#define MAX_ALIGN_PAGES (SNDRV_TRIDENT_MAX_PAGES / UNIT_PAGES) 768c2ecf20Sopenharmony_ci/* Note: if alignment doesn't match to the maximum size, the last few blocks 778c2ecf20Sopenharmony_ci * become unusable. To use such blocks, you'll need to check the validity 788c2ecf20Sopenharmony_ci * of accessing page in set_tlb_bus and set_silent_tlb. search_empty() 798c2ecf20Sopenharmony_ci * should also check it, too. 808c2ecf20Sopenharmony_ci */ 818c2ecf20Sopenharmony_ci#define get_aligned_page(offset) ((offset) / ALIGN_PAGE_SIZE) 828c2ecf20Sopenharmony_ci#define aligned_page_offset(page) ((page) * ALIGN_PAGE_SIZE) 838c2ecf20Sopenharmony_ci#define page_to_ptr(trident,page) __tlb_to_ptr(trident, (page) * UNIT_PAGES) 848c2ecf20Sopenharmony_ci#define page_to_addr(trident,page) __tlb_to_addr(trident, (page) * UNIT_PAGES) 858c2ecf20Sopenharmony_ci 868c2ecf20Sopenharmony_ci/* fill TLB entries -- UNIT_PAGES entries must be filled */ 878c2ecf20Sopenharmony_cistatic inline void set_tlb_bus(struct snd_trident *trident, int page, 888c2ecf20Sopenharmony_ci unsigned long ptr, dma_addr_t addr) 898c2ecf20Sopenharmony_ci{ 908c2ecf20Sopenharmony_ci int i; 918c2ecf20Sopenharmony_ci page *= UNIT_PAGES; 928c2ecf20Sopenharmony_ci for (i = 0; i < UNIT_PAGES; i++, page++) { 938c2ecf20Sopenharmony_ci __set_tlb_bus(trident, page, ptr, addr); 948c2ecf20Sopenharmony_ci ptr += SNDRV_TRIDENT_PAGE_SIZE; 958c2ecf20Sopenharmony_ci addr += SNDRV_TRIDENT_PAGE_SIZE; 968c2ecf20Sopenharmony_ci } 978c2ecf20Sopenharmony_ci} 988c2ecf20Sopenharmony_cistatic inline void set_silent_tlb(struct snd_trident *trident, int page) 998c2ecf20Sopenharmony_ci{ 1008c2ecf20Sopenharmony_ci int i; 1018c2ecf20Sopenharmony_ci page *= UNIT_PAGES; 1028c2ecf20Sopenharmony_ci for (i = 0; i < UNIT_PAGES; i++, page++) 1038c2ecf20Sopenharmony_ci __set_tlb_bus(trident, page, (unsigned long)trident->tlb.silent_page.area, trident->tlb.silent_page.addr); 1048c2ecf20Sopenharmony_ci} 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_ci#endif /* PAGE_SIZE */ 1078c2ecf20Sopenharmony_ci 1088c2ecf20Sopenharmony_ci/* calculate buffer pointer from offset address */ 1098c2ecf20Sopenharmony_cistatic inline void *offset_ptr(struct snd_trident *trident, int offset) 1108c2ecf20Sopenharmony_ci{ 1118c2ecf20Sopenharmony_ci char *ptr; 1128c2ecf20Sopenharmony_ci ptr = page_to_ptr(trident, get_aligned_page(offset)); 1138c2ecf20Sopenharmony_ci ptr += offset % ALIGN_PAGE_SIZE; 1148c2ecf20Sopenharmony_ci return (void*)ptr; 1158c2ecf20Sopenharmony_ci} 1168c2ecf20Sopenharmony_ci 1178c2ecf20Sopenharmony_ci/* first and last (aligned) pages of memory block */ 1188c2ecf20Sopenharmony_ci#define firstpg(blk) (((struct snd_trident_memblk_arg *)snd_util_memblk_argptr(blk))->first_page) 1198c2ecf20Sopenharmony_ci#define lastpg(blk) (((struct snd_trident_memblk_arg *)snd_util_memblk_argptr(blk))->last_page) 1208c2ecf20Sopenharmony_ci 1218c2ecf20Sopenharmony_ci/* 1228c2ecf20Sopenharmony_ci * search empty pages which may contain given size 1238c2ecf20Sopenharmony_ci */ 1248c2ecf20Sopenharmony_cistatic struct snd_util_memblk * 1258c2ecf20Sopenharmony_cisearch_empty(struct snd_util_memhdr *hdr, int size) 1268c2ecf20Sopenharmony_ci{ 1278c2ecf20Sopenharmony_ci struct snd_util_memblk *blk; 1288c2ecf20Sopenharmony_ci int page, psize; 1298c2ecf20Sopenharmony_ci struct list_head *p; 1308c2ecf20Sopenharmony_ci 1318c2ecf20Sopenharmony_ci psize = get_aligned_page(size + ALIGN_PAGE_SIZE -1); 1328c2ecf20Sopenharmony_ci page = 0; 1338c2ecf20Sopenharmony_ci list_for_each(p, &hdr->block) { 1348c2ecf20Sopenharmony_ci blk = list_entry(p, struct snd_util_memblk, list); 1358c2ecf20Sopenharmony_ci if (page + psize <= firstpg(blk)) 1368c2ecf20Sopenharmony_ci goto __found_pages; 1378c2ecf20Sopenharmony_ci page = lastpg(blk) + 1; 1388c2ecf20Sopenharmony_ci } 1398c2ecf20Sopenharmony_ci if (page + psize > MAX_ALIGN_PAGES) 1408c2ecf20Sopenharmony_ci return NULL; 1418c2ecf20Sopenharmony_ci 1428c2ecf20Sopenharmony_ci__found_pages: 1438c2ecf20Sopenharmony_ci /* create a new memory block */ 1448c2ecf20Sopenharmony_ci blk = __snd_util_memblk_new(hdr, psize * ALIGN_PAGE_SIZE, p->prev); 1458c2ecf20Sopenharmony_ci if (blk == NULL) 1468c2ecf20Sopenharmony_ci return NULL; 1478c2ecf20Sopenharmony_ci blk->offset = aligned_page_offset(page); /* set aligned offset */ 1488c2ecf20Sopenharmony_ci firstpg(blk) = page; 1498c2ecf20Sopenharmony_ci lastpg(blk) = page + psize - 1; 1508c2ecf20Sopenharmony_ci return blk; 1518c2ecf20Sopenharmony_ci} 1528c2ecf20Sopenharmony_ci 1538c2ecf20Sopenharmony_ci 1548c2ecf20Sopenharmony_ci/* 1558c2ecf20Sopenharmony_ci * check if the given pointer is valid for pages 1568c2ecf20Sopenharmony_ci */ 1578c2ecf20Sopenharmony_cistatic int is_valid_page(unsigned long ptr) 1588c2ecf20Sopenharmony_ci{ 1598c2ecf20Sopenharmony_ci if (ptr & ~0x3fffffffUL) { 1608c2ecf20Sopenharmony_ci snd_printk(KERN_ERR "max memory size is 1GB!!\n"); 1618c2ecf20Sopenharmony_ci return 0; 1628c2ecf20Sopenharmony_ci } 1638c2ecf20Sopenharmony_ci if (ptr & (SNDRV_TRIDENT_PAGE_SIZE-1)) { 1648c2ecf20Sopenharmony_ci snd_printk(KERN_ERR "page is not aligned\n"); 1658c2ecf20Sopenharmony_ci return 0; 1668c2ecf20Sopenharmony_ci } 1678c2ecf20Sopenharmony_ci return 1; 1688c2ecf20Sopenharmony_ci} 1698c2ecf20Sopenharmony_ci 1708c2ecf20Sopenharmony_ci/* 1718c2ecf20Sopenharmony_ci * page allocation for DMA (Scatter-Gather version) 1728c2ecf20Sopenharmony_ci */ 1738c2ecf20Sopenharmony_cistatic struct snd_util_memblk * 1748c2ecf20Sopenharmony_cisnd_trident_alloc_sg_pages(struct snd_trident *trident, 1758c2ecf20Sopenharmony_ci struct snd_pcm_substream *substream) 1768c2ecf20Sopenharmony_ci{ 1778c2ecf20Sopenharmony_ci struct snd_util_memhdr *hdr; 1788c2ecf20Sopenharmony_ci struct snd_util_memblk *blk; 1798c2ecf20Sopenharmony_ci struct snd_pcm_runtime *runtime = substream->runtime; 1808c2ecf20Sopenharmony_ci int idx, page; 1818c2ecf20Sopenharmony_ci 1828c2ecf20Sopenharmony_ci if (snd_BUG_ON(runtime->dma_bytes <= 0 || 1838c2ecf20Sopenharmony_ci runtime->dma_bytes > SNDRV_TRIDENT_MAX_PAGES * 1848c2ecf20Sopenharmony_ci SNDRV_TRIDENT_PAGE_SIZE)) 1858c2ecf20Sopenharmony_ci return NULL; 1868c2ecf20Sopenharmony_ci hdr = trident->tlb.memhdr; 1878c2ecf20Sopenharmony_ci if (snd_BUG_ON(!hdr)) 1888c2ecf20Sopenharmony_ci return NULL; 1898c2ecf20Sopenharmony_ci 1908c2ecf20Sopenharmony_ci 1918c2ecf20Sopenharmony_ci 1928c2ecf20Sopenharmony_ci mutex_lock(&hdr->block_mutex); 1938c2ecf20Sopenharmony_ci blk = search_empty(hdr, runtime->dma_bytes); 1948c2ecf20Sopenharmony_ci if (blk == NULL) { 1958c2ecf20Sopenharmony_ci mutex_unlock(&hdr->block_mutex); 1968c2ecf20Sopenharmony_ci return NULL; 1978c2ecf20Sopenharmony_ci } 1988c2ecf20Sopenharmony_ci 1998c2ecf20Sopenharmony_ci /* set TLB entries */ 2008c2ecf20Sopenharmony_ci idx = 0; 2018c2ecf20Sopenharmony_ci for (page = firstpg(blk); page <= lastpg(blk); page++, idx++) { 2028c2ecf20Sopenharmony_ci unsigned long ofs = idx << PAGE_SHIFT; 2038c2ecf20Sopenharmony_ci dma_addr_t addr = snd_pcm_sgbuf_get_addr(substream, ofs); 2048c2ecf20Sopenharmony_ci unsigned long ptr = (unsigned long) 2058c2ecf20Sopenharmony_ci snd_pcm_sgbuf_get_ptr(substream, ofs); 2068c2ecf20Sopenharmony_ci if (! is_valid_page(addr)) { 2078c2ecf20Sopenharmony_ci __snd_util_mem_free(hdr, blk); 2088c2ecf20Sopenharmony_ci mutex_unlock(&hdr->block_mutex); 2098c2ecf20Sopenharmony_ci return NULL; 2108c2ecf20Sopenharmony_ci } 2118c2ecf20Sopenharmony_ci set_tlb_bus(trident, page, ptr, addr); 2128c2ecf20Sopenharmony_ci } 2138c2ecf20Sopenharmony_ci mutex_unlock(&hdr->block_mutex); 2148c2ecf20Sopenharmony_ci return blk; 2158c2ecf20Sopenharmony_ci} 2168c2ecf20Sopenharmony_ci 2178c2ecf20Sopenharmony_ci/* 2188c2ecf20Sopenharmony_ci * page allocation for DMA (contiguous version) 2198c2ecf20Sopenharmony_ci */ 2208c2ecf20Sopenharmony_cistatic struct snd_util_memblk * 2218c2ecf20Sopenharmony_cisnd_trident_alloc_cont_pages(struct snd_trident *trident, 2228c2ecf20Sopenharmony_ci struct snd_pcm_substream *substream) 2238c2ecf20Sopenharmony_ci{ 2248c2ecf20Sopenharmony_ci struct snd_util_memhdr *hdr; 2258c2ecf20Sopenharmony_ci struct snd_util_memblk *blk; 2268c2ecf20Sopenharmony_ci int page; 2278c2ecf20Sopenharmony_ci struct snd_pcm_runtime *runtime = substream->runtime; 2288c2ecf20Sopenharmony_ci dma_addr_t addr; 2298c2ecf20Sopenharmony_ci unsigned long ptr; 2308c2ecf20Sopenharmony_ci 2318c2ecf20Sopenharmony_ci if (snd_BUG_ON(runtime->dma_bytes <= 0 || 2328c2ecf20Sopenharmony_ci runtime->dma_bytes > SNDRV_TRIDENT_MAX_PAGES * 2338c2ecf20Sopenharmony_ci SNDRV_TRIDENT_PAGE_SIZE)) 2348c2ecf20Sopenharmony_ci return NULL; 2358c2ecf20Sopenharmony_ci hdr = trident->tlb.memhdr; 2368c2ecf20Sopenharmony_ci if (snd_BUG_ON(!hdr)) 2378c2ecf20Sopenharmony_ci return NULL; 2388c2ecf20Sopenharmony_ci 2398c2ecf20Sopenharmony_ci mutex_lock(&hdr->block_mutex); 2408c2ecf20Sopenharmony_ci blk = search_empty(hdr, runtime->dma_bytes); 2418c2ecf20Sopenharmony_ci if (blk == NULL) { 2428c2ecf20Sopenharmony_ci mutex_unlock(&hdr->block_mutex); 2438c2ecf20Sopenharmony_ci return NULL; 2448c2ecf20Sopenharmony_ci } 2458c2ecf20Sopenharmony_ci 2468c2ecf20Sopenharmony_ci /* set TLB entries */ 2478c2ecf20Sopenharmony_ci addr = runtime->dma_addr; 2488c2ecf20Sopenharmony_ci ptr = (unsigned long)runtime->dma_area; 2498c2ecf20Sopenharmony_ci for (page = firstpg(blk); page <= lastpg(blk); page++, 2508c2ecf20Sopenharmony_ci ptr += SNDRV_TRIDENT_PAGE_SIZE, addr += SNDRV_TRIDENT_PAGE_SIZE) { 2518c2ecf20Sopenharmony_ci if (! is_valid_page(addr)) { 2528c2ecf20Sopenharmony_ci __snd_util_mem_free(hdr, blk); 2538c2ecf20Sopenharmony_ci mutex_unlock(&hdr->block_mutex); 2548c2ecf20Sopenharmony_ci return NULL; 2558c2ecf20Sopenharmony_ci } 2568c2ecf20Sopenharmony_ci set_tlb_bus(trident, page, ptr, addr); 2578c2ecf20Sopenharmony_ci } 2588c2ecf20Sopenharmony_ci mutex_unlock(&hdr->block_mutex); 2598c2ecf20Sopenharmony_ci return blk; 2608c2ecf20Sopenharmony_ci} 2618c2ecf20Sopenharmony_ci 2628c2ecf20Sopenharmony_ci/* 2638c2ecf20Sopenharmony_ci * page allocation for DMA 2648c2ecf20Sopenharmony_ci */ 2658c2ecf20Sopenharmony_cistruct snd_util_memblk * 2668c2ecf20Sopenharmony_cisnd_trident_alloc_pages(struct snd_trident *trident, 2678c2ecf20Sopenharmony_ci struct snd_pcm_substream *substream) 2688c2ecf20Sopenharmony_ci{ 2698c2ecf20Sopenharmony_ci if (snd_BUG_ON(!trident || !substream)) 2708c2ecf20Sopenharmony_ci return NULL; 2718c2ecf20Sopenharmony_ci if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV_SG) 2728c2ecf20Sopenharmony_ci return snd_trident_alloc_sg_pages(trident, substream); 2738c2ecf20Sopenharmony_ci else 2748c2ecf20Sopenharmony_ci return snd_trident_alloc_cont_pages(trident, substream); 2758c2ecf20Sopenharmony_ci} 2768c2ecf20Sopenharmony_ci 2778c2ecf20Sopenharmony_ci 2788c2ecf20Sopenharmony_ci/* 2798c2ecf20Sopenharmony_ci * release DMA buffer from page table 2808c2ecf20Sopenharmony_ci */ 2818c2ecf20Sopenharmony_ciint snd_trident_free_pages(struct snd_trident *trident, 2828c2ecf20Sopenharmony_ci struct snd_util_memblk *blk) 2838c2ecf20Sopenharmony_ci{ 2848c2ecf20Sopenharmony_ci struct snd_util_memhdr *hdr; 2858c2ecf20Sopenharmony_ci int page; 2868c2ecf20Sopenharmony_ci 2878c2ecf20Sopenharmony_ci if (snd_BUG_ON(!trident || !blk)) 2888c2ecf20Sopenharmony_ci return -EINVAL; 2898c2ecf20Sopenharmony_ci 2908c2ecf20Sopenharmony_ci hdr = trident->tlb.memhdr; 2918c2ecf20Sopenharmony_ci mutex_lock(&hdr->block_mutex); 2928c2ecf20Sopenharmony_ci /* reset TLB entries */ 2938c2ecf20Sopenharmony_ci for (page = firstpg(blk); page <= lastpg(blk); page++) 2948c2ecf20Sopenharmony_ci set_silent_tlb(trident, page); 2958c2ecf20Sopenharmony_ci /* free memory block */ 2968c2ecf20Sopenharmony_ci __snd_util_mem_free(hdr, blk); 2978c2ecf20Sopenharmony_ci mutex_unlock(&hdr->block_mutex); 2988c2ecf20Sopenharmony_ci return 0; 2998c2ecf20Sopenharmony_ci} 300