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,ptr,addr) \
23 do { (trident)->tlb.entries[page] = cpu_to_le32((addr) & ~(SNDRV_TRIDENT_PAGE_SIZE-1)); \
24 (trident)->tlb.shadow_entries[page] = (ptr); } while (0)
25 #define __tlb_to_ptr(trident,page) \
26 (void*)((trident)->tlb.shadow_entries[page])
27 #define __tlb_to_addr(trident,page) \
28 (dma_addr_t)le32_to_cpu((trident->tlb.entries[page]) & ~(SNDRV_TRIDENT_PAGE_SIZE - 1))
31 /* page size == SNDRV_TRIDENT_PAGE_SIZE */
32 #define ALIGN_PAGE_SIZE PAGE_SIZE /* minimum page size for allocation */
33 #define MAX_ALIGN_PAGES SNDRV_TRIDENT_MAX_PAGES /* maxmium aligned pages */
34 /* fill TLB entrie(s) corresponding to page with ptr */
35 #define set_tlb_bus(trident,page,ptr,addr) __set_tlb_bus(trident,page,ptr,addr)
36 /* fill TLB entrie(s) corresponding to page with silence pointer */
37 #define set_silent_tlb(trident,page) __set_tlb_bus(trident, page, (unsigned long)trident->tlb.silent_page.area, trident->tlb.silent_page.addr)
38 /* get aligned page from offset address */
39 #define get_aligned_page(offset) ((offset) >> 12)
40 /* get offset address from aligned page */
41 #define aligned_page_offset(page) ((page) << 12)
42 /* get buffer address from aligned page */
43 #define page_to_ptr(trident,page) __tlb_to_ptr(trident, page)
44 /* get PCI physical address from aligned page */
45 #define page_to_addr(trident,page) __tlb_to_addr(trident, page)
47 #elif PAGE_SIZE == 8192
48 /* page size == SNDRV_TRIDENT_PAGE_SIZE x 2*/
49 #define ALIGN_PAGE_SIZE PAGE_SIZE
50 #define MAX_ALIGN_PAGES (SNDRV_TRIDENT_MAX_PAGES / 2)
51 #define get_aligned_page(offset) ((offset) >> 13)
52 #define aligned_page_offset(page) ((page) << 13)
53 #define page_to_ptr(trident,page) __tlb_to_ptr(trident, (page) << 1)
54 #define page_to_addr(trident,page) __tlb_to_addr(trident, (page) << 1)
56 /* fill TLB entries -- we need to fill two entries */
57 static inline void set_tlb_bus(struct snd_trident
*trident
, int page
,
58 unsigned long ptr
, dma_addr_t addr
)
61 __set_tlb_bus(trident
, page
, ptr
, addr
);
62 __set_tlb_bus(trident
, page
+1, ptr
+ SNDRV_TRIDENT_PAGE_SIZE
, addr
+ SNDRV_TRIDENT_PAGE_SIZE
);
64 static inline void set_silent_tlb(struct snd_trident
*trident
, int page
)
67 __set_tlb_bus(trident
, page
, (unsigned long)trident
->tlb
.silent_page
.area
, trident
->tlb
.silent_page
.addr
);
68 __set_tlb_bus(trident
, page
+1, (unsigned long)trident
->tlb
.silent_page
.area
, trident
->tlb
.silent_page
.addr
);
73 #define UNIT_PAGES (PAGE_SIZE / SNDRV_TRIDENT_PAGE_SIZE)
74 #define ALIGN_PAGE_SIZE (SNDRV_TRIDENT_PAGE_SIZE * UNIT_PAGES)
75 #define MAX_ALIGN_PAGES (SNDRV_TRIDENT_MAX_PAGES / UNIT_PAGES)
76 /* Note: if alignment doesn't match to the maximum size, the last few blocks
77 * become unusable. To use such blocks, you'll need to check the validity
78 * of accessing page in set_tlb_bus and set_silent_tlb. search_empty()
79 * should also check it, too.
81 #define get_aligned_page(offset) ((offset) / ALIGN_PAGE_SIZE)
82 #define aligned_page_offset(page) ((page) * ALIGN_PAGE_SIZE)
83 #define page_to_ptr(trident,page) __tlb_to_ptr(trident, (page) * UNIT_PAGES)
84 #define page_to_addr(trident,page) __tlb_to_addr(trident, (page) * UNIT_PAGES)
86 /* fill TLB entries -- UNIT_PAGES entries must be filled */
87 static inline void set_tlb_bus(struct snd_trident
*trident
, int page
,
88 unsigned long ptr
, dma_addr_t addr
)
92 for (i
= 0; i
< UNIT_PAGES
; i
++, page
++) {
93 __set_tlb_bus(trident
, page
, ptr
, addr
);
94 ptr
+= SNDRV_TRIDENT_PAGE_SIZE
;
95 addr
+= SNDRV_TRIDENT_PAGE_SIZE
;
98 static inline void set_silent_tlb(struct snd_trident
*trident
, int page
)
102 for (i
= 0; i
< UNIT_PAGES
; i
++, page
++)
103 __set_tlb_bus(trident
, page
, (unsigned long)trident
->tlb
.silent_page
.area
, trident
->tlb
.silent_page
.addr
);
106 #endif /* PAGE_SIZE */
108 /* calculate buffer pointer from offset address */
109 static inline void *offset_ptr(struct snd_trident
*trident
, int offset
)
112 ptr
= page_to_ptr(trident
, get_aligned_page(offset
));
113 ptr
+= offset
% ALIGN_PAGE_SIZE
;
117 /* first and last (aligned) pages of memory block */
118 #define firstpg(blk) (((struct snd_trident_memblk_arg *)snd_util_memblk_argptr(blk))->first_page)
119 #define lastpg(blk) (((struct snd_trident_memblk_arg *)snd_util_memblk_argptr(blk))->last_page)
122 * search empty pages which may contain given size
124 static struct snd_util_memblk
*
125 search_empty(struct snd_util_memhdr
*hdr
, int size
)
127 struct snd_util_memblk
*blk
;
131 psize
= get_aligned_page(size
+ ALIGN_PAGE_SIZE
-1);
133 list_for_each(p
, &hdr
->block
) {
134 blk
= list_entry(p
, struct snd_util_memblk
, list
);
135 if (page
+ psize
<= firstpg(blk
))
137 page
= lastpg(blk
) + 1;
139 if (page
+ psize
> MAX_ALIGN_PAGES
)
143 /* create a new memory block */
144 blk
= __snd_util_memblk_new(hdr
, psize
* ALIGN_PAGE_SIZE
, p
->prev
);
147 blk
->offset
= aligned_page_offset(page
); /* set aligned offset */
149 lastpg(blk
) = page
+ psize
- 1;
155 * check if the given pointer is valid for pages
157 static int is_valid_page(unsigned long ptr
)
159 if (ptr
& ~0x3fffffffUL
) {
160 snd_printk(KERN_ERR
"max memory size is 1GB!!\n");
163 if (ptr
& (SNDRV_TRIDENT_PAGE_SIZE
-1)) {
164 snd_printk(KERN_ERR
"page is not aligned\n");
171 * page allocation for DMA (Scatter-Gather version)
173 static struct snd_util_memblk
*
174 snd_trident_alloc_sg_pages(struct snd_trident
*trident
,
175 struct snd_pcm_substream
*substream
)
177 struct snd_util_memhdr
*hdr
;
178 struct snd_util_memblk
*blk
;
179 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
182 if (snd_BUG_ON(runtime
->dma_bytes
<= 0 ||
183 runtime
->dma_bytes
> SNDRV_TRIDENT_MAX_PAGES
*
184 SNDRV_TRIDENT_PAGE_SIZE
))
186 hdr
= trident
->tlb
.memhdr
;
187 if (snd_BUG_ON(!hdr
))
192 mutex_lock(&hdr
->block_mutex
);
193 blk
= search_empty(hdr
, runtime
->dma_bytes
);
195 mutex_unlock(&hdr
->block_mutex
);
199 /* set TLB entries */
201 for (page
= firstpg(blk
); page
<= lastpg(blk
); page
++, idx
++) {
202 unsigned long ofs
= idx
<< PAGE_SHIFT
;
203 dma_addr_t addr
= snd_pcm_sgbuf_get_addr(substream
, ofs
);
204 unsigned long ptr
= (unsigned long)
205 snd_pcm_sgbuf_get_ptr(substream
, ofs
);
206 if (! is_valid_page(addr
)) {
207 __snd_util_mem_free(hdr
, blk
);
208 mutex_unlock(&hdr
->block_mutex
);
211 set_tlb_bus(trident
, page
, ptr
, addr
);
213 mutex_unlock(&hdr
->block_mutex
);
218 * page allocation for DMA (contiguous version)
220 static struct snd_util_memblk
*
221 snd_trident_alloc_cont_pages(struct snd_trident
*trident
,
222 struct snd_pcm_substream
*substream
)
224 struct snd_util_memhdr
*hdr
;
225 struct snd_util_memblk
*blk
;
227 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
231 if (snd_BUG_ON(runtime
->dma_bytes
<= 0 ||
232 runtime
->dma_bytes
> SNDRV_TRIDENT_MAX_PAGES
*
233 SNDRV_TRIDENT_PAGE_SIZE
))
235 hdr
= trident
->tlb
.memhdr
;
236 if (snd_BUG_ON(!hdr
))
239 mutex_lock(&hdr
->block_mutex
);
240 blk
= search_empty(hdr
, runtime
->dma_bytes
);
242 mutex_unlock(&hdr
->block_mutex
);
246 /* set TLB entries */
247 addr
= runtime
->dma_addr
;
248 ptr
= (unsigned long)runtime
->dma_area
;
249 for (page
= firstpg(blk
); page
<= lastpg(blk
); page
++,
250 ptr
+= SNDRV_TRIDENT_PAGE_SIZE
, addr
+= SNDRV_TRIDENT_PAGE_SIZE
) {
251 if (! is_valid_page(addr
)) {
252 __snd_util_mem_free(hdr
, blk
);
253 mutex_unlock(&hdr
->block_mutex
);
256 set_tlb_bus(trident
, page
, ptr
, addr
);
258 mutex_unlock(&hdr
->block_mutex
);
263 * page allocation for DMA
265 struct snd_util_memblk
*
266 snd_trident_alloc_pages(struct snd_trident
*trident
,
267 struct snd_pcm_substream
*substream
)
269 if (snd_BUG_ON(!trident
|| !substream
))
271 if (substream
->dma_buffer
.dev
.type
== SNDRV_DMA_TYPE_DEV_SG
)
272 return snd_trident_alloc_sg_pages(trident
, substream
);
274 return snd_trident_alloc_cont_pages(trident
, substream
);
279 * release DMA buffer from page table
281 int snd_trident_free_pages(struct snd_trident
*trident
,
282 struct snd_util_memblk
*blk
)
284 struct snd_util_memhdr
*hdr
;
287 if (snd_BUG_ON(!trident
|| !blk
))
290 hdr
= trident
->tlb
.memhdr
;
291 mutex_lock(&hdr
->block_mutex
);
292 /* reset TLB entries */
293 for (page
= firstpg(blk
); page
<= lastpg(blk
); page
++)
294 set_silent_tlb(trident
, page
);
295 /* free memory block */
296 __snd_util_mem_free(hdr
, blk
);
297 mutex_unlock(&hdr
->block_mutex
);