1 // SPDX-License-Identifier: GPL-2.0-or-later
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>
7 * Trident 4DWave-NX memory page allocation (TLB area)
8 * Trident chip can handle only 16MByte of the memory at the same time.
12 #include <linux/pci.h>
13 #include <linux/time.h>
14 #include <linux/mutex.h>
16 #include <sound/core.h>
19 /* page arguments of these two macros are Trident page (4096 bytes), not like
20 * aligned pages in others
22 #define __set_tlb_bus(trident,page,addr) \
23 (trident)->tlb.entries[page] = cpu_to_le32((addr) & ~(SNDRV_TRIDENT_PAGE_SIZE-1))
24 #define __tlb_to_addr(trident,page) \
25 (dma_addr_t)le32_to_cpu((trident->tlb.entries[page]) & ~(SNDRV_TRIDENT_PAGE_SIZE - 1))
28 /* page size == SNDRV_TRIDENT_PAGE_SIZE */
29 #define ALIGN_PAGE_SIZE PAGE_SIZE /* minimum page size for allocation */
30 #define MAX_ALIGN_PAGES SNDRV_TRIDENT_MAX_PAGES /* maxmium aligned pages */
31 /* fill TLB entrie(s) corresponding to page with ptr */
32 #define set_tlb_bus(trident,page,addr) __set_tlb_bus(trident,page,addr)
33 /* fill TLB entrie(s) corresponding to page with silence pointer */
34 #define set_silent_tlb(trident,page) __set_tlb_bus(trident, page, trident->tlb.silent_page->addr)
35 /* get aligned page from offset address */
36 #define get_aligned_page(offset) ((offset) >> 12)
37 /* get offset address from aligned page */
38 #define aligned_page_offset(page) ((page) << 12)
39 /* get PCI physical address from aligned page */
40 #define page_to_addr(trident,page) __tlb_to_addr(trident, page)
42 #elif PAGE_SIZE == 8192
43 /* page size == SNDRV_TRIDENT_PAGE_SIZE x 2*/
44 #define ALIGN_PAGE_SIZE PAGE_SIZE
45 #define MAX_ALIGN_PAGES (SNDRV_TRIDENT_MAX_PAGES / 2)
46 #define get_aligned_page(offset) ((offset) >> 13)
47 #define aligned_page_offset(page) ((page) << 13)
48 #define page_to_addr(trident,page) __tlb_to_addr(trident, (page) << 1)
50 /* fill TLB entries -- we need to fill two entries */
51 static inline void set_tlb_bus(struct snd_trident *trident, int page,
55 __set_tlb_bus(trident, page, addr);
56 __set_tlb_bus(trident, page+1, addr + SNDRV_TRIDENT_PAGE_SIZE);
58 static inline void set_silent_tlb(struct snd_trident *trident, int page)
61 __set_tlb_bus(trident, page, trident->tlb.silent_page->addr);
62 __set_tlb_bus(trident, page+1, trident->tlb.silent_page->addr);
67 #define UNIT_PAGES (PAGE_SIZE / SNDRV_TRIDENT_PAGE_SIZE)
68 #define ALIGN_PAGE_SIZE (SNDRV_TRIDENT_PAGE_SIZE * UNIT_PAGES)
69 #define MAX_ALIGN_PAGES (SNDRV_TRIDENT_MAX_PAGES / UNIT_PAGES)
70 /* Note: if alignment doesn't match to the maximum size, the last few blocks
71 * become unusable. To use such blocks, you'll need to check the validity
72 * of accessing page in set_tlb_bus and set_silent_tlb. search_empty()
73 * should also check it, too.
75 #define get_aligned_page(offset) ((offset) / ALIGN_PAGE_SIZE)
76 #define aligned_page_offset(page) ((page) * ALIGN_PAGE_SIZE)
77 #define page_to_addr(trident,page) __tlb_to_addr(trident, (page) * UNIT_PAGES)
79 /* fill TLB entries -- UNIT_PAGES entries must be filled */
80 static inline void set_tlb_bus(struct snd_trident *trident, int page,
85 for (i = 0; i < UNIT_PAGES; i++, page++) {
86 __set_tlb_bus(trident, page, addr);
87 addr += SNDRV_TRIDENT_PAGE_SIZE;
90 static inline void set_silent_tlb(struct snd_trident *trident, int page)
94 for (i = 0; i < UNIT_PAGES; i++, page++)
95 __set_tlb_bus(trident, page, trident->tlb.silent_page->addr);
98 #endif /* PAGE_SIZE */
100 /* first and last (aligned) pages of memory block */
101 #define firstpg(blk) (((struct snd_trident_memblk_arg *)snd_util_memblk_argptr(blk))->first_page)
102 #define lastpg(blk) (((struct snd_trident_memblk_arg *)snd_util_memblk_argptr(blk))->last_page)
105 * search empty pages which may contain given size
107 static struct snd_util_memblk *
108 search_empty(struct snd_util_memhdr *hdr, int size)
110 struct snd_util_memblk *blk;
114 psize = get_aligned_page(size + ALIGN_PAGE_SIZE -1);
116 list_for_each(p, &hdr->block) {
117 blk = list_entry(p, struct snd_util_memblk, list);
118 if (page + psize <= firstpg(blk))
120 page = lastpg(blk) + 1;
122 if (page + psize > MAX_ALIGN_PAGES)
126 /* create a new memory block */
127 blk = __snd_util_memblk_new(hdr, psize * ALIGN_PAGE_SIZE, p->prev);
130 blk->offset = aligned_page_offset(page); /* set aligned offset */
132 lastpg(blk) = page + psize - 1;
138 * check if the given pointer is valid for pages
140 static int is_valid_page(unsigned long ptr)
142 if (ptr & ~0x3fffffffUL) {
143 snd_printk(KERN_ERR "max memory size is 1GB!!\n");
146 if (ptr & (SNDRV_TRIDENT_PAGE_SIZE-1)) {
147 snd_printk(KERN_ERR "page is not aligned\n");
154 * page allocation for DMA (Scatter-Gather version)
156 static struct snd_util_memblk *
157 snd_trident_alloc_sg_pages(struct snd_trident *trident,
158 struct snd_pcm_substream *substream)
160 struct snd_util_memhdr *hdr;
161 struct snd_util_memblk *blk;
162 struct snd_pcm_runtime *runtime = substream->runtime;
165 if (snd_BUG_ON(runtime->dma_bytes <= 0 ||
166 runtime->dma_bytes > SNDRV_TRIDENT_MAX_PAGES *
167 SNDRV_TRIDENT_PAGE_SIZE))
169 hdr = trident->tlb.memhdr;
170 if (snd_BUG_ON(!hdr))
175 mutex_lock(&hdr->block_mutex);
176 blk = search_empty(hdr, runtime->dma_bytes);
178 mutex_unlock(&hdr->block_mutex);
182 /* set TLB entries */
184 for (page = firstpg(blk); page <= lastpg(blk); page++, idx++) {
185 unsigned long ofs = idx << PAGE_SHIFT;
186 dma_addr_t addr = snd_pcm_sgbuf_get_addr(substream, ofs);
187 if (! is_valid_page(addr)) {
188 __snd_util_mem_free(hdr, blk);
189 mutex_unlock(&hdr->block_mutex);
192 set_tlb_bus(trident, page, addr);
194 mutex_unlock(&hdr->block_mutex);
199 * page allocation for DMA (contiguous version)
201 static struct snd_util_memblk *
202 snd_trident_alloc_cont_pages(struct snd_trident *trident,
203 struct snd_pcm_substream *substream)
205 struct snd_util_memhdr *hdr;
206 struct snd_util_memblk *blk;
208 struct snd_pcm_runtime *runtime = substream->runtime;
211 if (snd_BUG_ON(runtime->dma_bytes <= 0 ||
212 runtime->dma_bytes > SNDRV_TRIDENT_MAX_PAGES *
213 SNDRV_TRIDENT_PAGE_SIZE))
215 hdr = trident->tlb.memhdr;
216 if (snd_BUG_ON(!hdr))
219 mutex_lock(&hdr->block_mutex);
220 blk = search_empty(hdr, runtime->dma_bytes);
222 mutex_unlock(&hdr->block_mutex);
226 /* set TLB entries */
227 addr = runtime->dma_addr;
228 for (page = firstpg(blk); page <= lastpg(blk); page++,
229 addr += SNDRV_TRIDENT_PAGE_SIZE) {
230 if (! is_valid_page(addr)) {
231 __snd_util_mem_free(hdr, blk);
232 mutex_unlock(&hdr->block_mutex);
235 set_tlb_bus(trident, page, addr);
237 mutex_unlock(&hdr->block_mutex);
242 * page allocation for DMA
244 struct snd_util_memblk *
245 snd_trident_alloc_pages(struct snd_trident *trident,
246 struct snd_pcm_substream *substream)
248 if (snd_BUG_ON(!trident || !substream))
250 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV_SG)
251 return snd_trident_alloc_sg_pages(trident, substream);
253 return snd_trident_alloc_cont_pages(trident, substream);
258 * release DMA buffer from page table
260 int snd_trident_free_pages(struct snd_trident *trident,
261 struct snd_util_memblk *blk)
263 struct snd_util_memhdr *hdr;
266 if (snd_BUG_ON(!trident || !blk))
269 hdr = trident->tlb.memhdr;
270 mutex_lock(&hdr->block_mutex);
271 /* reset TLB entries */
272 for (page = firstpg(blk); page <= lastpg(blk); page++)
273 set_silent_tlb(trident, page);
274 /* free memory block */
275 __snd_util_mem_free(hdr, blk);
276 mutex_unlock(&hdr->block_mutex);