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>
6 * Trident 4DWave-NX memory page allocation (TLB area)
7 * Trident chip can handle only 16MByte of the memory at the same time.
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.
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.
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
26 #include <sound/driver.h>
28 #include <linux/pci.h>
29 #include <linux/time.h>
30 #include <sound/core.h>
31 #include <sound/trident.h>
33 /* page arguments of these two macros are Trident page (4096 bytes), not like
34 * aligned pages in others
36 #define __set_tlb_bus(trident,page,ptr,addr) \
37 do { (trident)->tlb.entries[page] = cpu_to_le32((addr) & ~(SNDRV_TRIDENT_PAGE_SIZE-1)); \
38 (trident)->tlb.shadow_entries[page] = (ptr); } while (0)
39 #define __tlb_to_ptr(trident,page) \
40 (void*)((trident)->tlb.shadow_entries[page])
41 #define __tlb_to_addr(trident,page) \
42 (dma_addr_t)le32_to_cpu((trident->tlb.entries[page]) & ~(SNDRV_TRIDENT_PAGE_SIZE - 1))
45 /* page size == SNDRV_TRIDENT_PAGE_SIZE */
46 #define ALIGN_PAGE_SIZE PAGE_SIZE /* minimum page size for allocation */
47 #define MAX_ALIGN_PAGES SNDRV_TRIDENT_MAX_PAGES /* maxmium aligned pages */
48 /* fill TLB entrie(s) corresponding to page with ptr */
49 #define set_tlb_bus(trident,page,ptr,addr) __set_tlb_bus(trident,page,ptr,addr)
50 /* fill TLB entrie(s) corresponding to page with silence pointer */
51 #define set_silent_tlb(trident,page) __set_tlb_bus(trident, page, (unsigned long)trident->tlb.silent_page.area, trident->tlb.silent_page.addr)
52 /* get aligned page from offset address */
53 #define get_aligned_page(offset) ((offset) >> 12)
54 /* get offset address from aligned page */
55 #define aligned_page_offset(page) ((page) << 12)
56 /* get buffer address from aligned page */
57 #define page_to_ptr(trident,page) __tlb_to_ptr(trident, page)
58 /* get PCI physical address from aligned page */
59 #define page_to_addr(trident,page) __tlb_to_addr(trident, page)
61 #elif PAGE_SIZE == 8192
62 /* page size == SNDRV_TRIDENT_PAGE_SIZE x 2*/
63 #define ALIGN_PAGE_SIZE PAGE_SIZE
64 #define MAX_ALIGN_PAGES (SNDRV_TRIDENT_MAX_PAGES / 2)
65 #define get_aligned_page(offset) ((offset) >> 13)
66 #define aligned_page_offset(page) ((page) << 13)
67 #define page_to_ptr(trident,page) __tlb_to_ptr(trident, (page) << 1)
68 #define page_to_addr(trident,page) __tlb_to_addr(trident, (page) << 1)
70 /* fill TLB entries -- we need to fill two entries */
71 static inline void set_tlb_bus(trident_t
*trident
, int page
, unsigned long ptr
, dma_addr_t addr
)
74 __set_tlb_bus(trident
, page
, ptr
, addr
);
75 __set_tlb_bus(trident
, page
+1, ptr
+ SNDRV_TRIDENT_PAGE_SIZE
, addr
+ SNDRV_TRIDENT_PAGE_SIZE
);
77 static inline void set_silent_tlb(trident_t
*trident
, int page
)
80 __set_tlb_bus(trident
, page
, (unsigned long)trident
->tlb
.silent_page
.area
, trident
->tlb
.silent_page
.addr
);
81 __set_tlb_bus(trident
, page
+1, (unsigned long)trident
->tlb
.silent_page
.area
, trident
->tlb
.silent_page
.addr
);
86 #define UNIT_PAGES (PAGE_SIZE / SNDRV_TRIDENT_PAGE_SIZE)
87 #define ALIGN_PAGE_SIZE (SNDRV_TRIDENT_PAGE_SIZE * UNIT_PAGES)
88 #define MAX_ALIGN_PAGES (SNDRV_TRIDENT_MAX_PAGES / UNIT_PAGES)
89 /* Note: if alignment doesn't match to the maximum size, the last few blocks
90 * become unusable. To use such blocks, you'll need to check the validity
91 * of accessing page in set_tlb_bus and set_silent_tlb. search_empty()
92 * should also check it, too.
94 #define get_aligned_page(offset) ((offset) / ALIGN_PAGE_SIZE)
95 #define aligned_page_offset(page) ((page) * ALIGN_PAGE_SIZE)
96 #define page_to_ptr(trident,page) __tlb_to_ptr(trident, (page) * UNIT_PAGES)
97 #define page_to_addr(trident,page) __tlb_to_addr(trident, (page) * UNIT_PAGES)
99 /* fill TLB entries -- UNIT_PAGES entries must be filled */
100 static inline void set_tlb_bus(trident_t
*trident
, int page
, unsigned long ptr
, dma_addr_t addr
)
104 for (i
= 0; i
< UNIT_PAGES
; i
++, page
++) {
105 __set_tlb_bus(trident
, page
, ptr
, addr
);
106 ptr
+= SNDRV_TRIDENT_PAGE_SIZE
;
107 addr
+= SNDRV_TRIDENT_PAGE_SIZE
;
110 static inline void set_silent_tlb(trident_t
*trident
, int page
)
114 for (i
= 0; i
< UNIT_PAGES
; i
++, page
++)
115 __set_tlb_bus(trident
, page
, (unsigned long)trident
->tlb
.silent_page
.area
, trident
->tlb
.silent_page
.addr
);
118 #endif /* PAGE_SIZE */
120 /* calculate buffer pointer from offset address */
121 inline static void *offset_ptr(trident_t
*trident
, int offset
)
124 ptr
= page_to_ptr(trident
, get_aligned_page(offset
));
125 ptr
+= offset
% ALIGN_PAGE_SIZE
;
129 /* first and last (aligned) pages of memory block */
130 #define firstpg(blk) (((snd_trident_memblk_arg_t*)snd_util_memblk_argptr(blk))->first_page)
131 #define lastpg(blk) (((snd_trident_memblk_arg_t*)snd_util_memblk_argptr(blk))->last_page)
134 * search empty pages which may contain given size
136 static snd_util_memblk_t
*
137 search_empty(snd_util_memhdr_t
*hdr
, int size
)
139 snd_util_memblk_t
*blk
, *prev
;
143 psize
= get_aligned_page(size
+ ALIGN_PAGE_SIZE
-1);
146 list_for_each(p
, &hdr
->block
) {
147 blk
= list_entry(p
, snd_util_memblk_t
, list
);
148 if (page
+ psize
<= firstpg(blk
))
150 page
= lastpg(blk
) + 1;
152 if (page
+ psize
> MAX_ALIGN_PAGES
)
156 /* create a new memory block */
157 blk
= __snd_util_memblk_new(hdr
, psize
* ALIGN_PAGE_SIZE
, p
->prev
);
160 blk
->offset
= aligned_page_offset(page
); /* set aligned offset */
162 lastpg(blk
) = page
+ psize
- 1;
168 * check if the given pointer is valid for pages
170 static int is_valid_page(unsigned long ptr
)
172 if (ptr
& ~0x3fffffffUL
) {
173 snd_printk("max memory size is 1GB!!\n");
176 if (ptr
& (SNDRV_TRIDENT_PAGE_SIZE
-1)) {
177 snd_printk("page is not aligned\n");
184 * page allocation for DMA (Scatter-Gather version)
186 static snd_util_memblk_t
*
187 snd_trident_alloc_sg_pages(trident_t
*trident
, snd_pcm_substream_t
*substream
)
189 snd_util_memhdr_t
*hdr
;
190 snd_util_memblk_t
*blk
;
191 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
193 struct snd_sg_buf
*sgbuf
= snd_pcm_substream_sgbuf(substream
);
195 snd_assert(runtime
->dma_bytes
> 0 && runtime
->dma_bytes
<= SNDRV_TRIDENT_MAX_PAGES
* SNDRV_TRIDENT_PAGE_SIZE
, return NULL
);
196 hdr
= trident
->tlb
.memhdr
;
197 snd_assert(hdr
!= NULL
, return NULL
);
201 down(&hdr
->block_mutex
);
202 blk
= search_empty(hdr
, runtime
->dma_bytes
);
204 up(&hdr
->block_mutex
);
207 if (lastpg(blk
) - firstpg(blk
) >= sgbuf
->pages
) {
208 snd_printk(KERN_ERR
"page calculation doesn't match: allocated pages = %d, trident = %d/%d\n", sgbuf
->pages
, firstpg(blk
), lastpg(blk
));
209 __snd_util_mem_free(hdr
, blk
);
210 up(&hdr
->block_mutex
);
214 /* set TLB entries */
216 for (page
= firstpg(blk
); page
<= lastpg(blk
); page
++, idx
++) {
217 dma_addr_t addr
= sgbuf
->table
[idx
].addr
;
218 unsigned long ptr
= (unsigned long)sgbuf
->table
[idx
].buf
;
219 if (! is_valid_page(addr
)) {
220 __snd_util_mem_free(hdr
, blk
);
221 up(&hdr
->block_mutex
);
224 set_tlb_bus(trident
, page
, ptr
, addr
);
226 up(&hdr
->block_mutex
);
231 * page allocation for DMA (contiguous version)
233 static snd_util_memblk_t
*
234 snd_trident_alloc_cont_pages(trident_t
*trident
, snd_pcm_substream_t
*substream
)
236 snd_util_memhdr_t
*hdr
;
237 snd_util_memblk_t
*blk
;
239 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
243 snd_assert(runtime
->dma_bytes
> 0 && runtime
->dma_bytes
<= SNDRV_TRIDENT_MAX_PAGES
* SNDRV_TRIDENT_PAGE_SIZE
, return NULL
);
244 hdr
= trident
->tlb
.memhdr
;
245 snd_assert(hdr
!= NULL
, return NULL
);
247 down(&hdr
->block_mutex
);
248 blk
= search_empty(hdr
, runtime
->dma_bytes
);
250 up(&hdr
->block_mutex
);
254 /* set TLB entries */
255 addr
= runtime
->dma_addr
;
256 ptr
= (unsigned long)runtime
->dma_area
;
257 for (page
= firstpg(blk
); page
<= lastpg(blk
); page
++,
258 ptr
+= SNDRV_TRIDENT_PAGE_SIZE
, addr
+= SNDRV_TRIDENT_PAGE_SIZE
) {
259 if (! is_valid_page(addr
)) {
260 __snd_util_mem_free(hdr
, blk
);
261 up(&hdr
->block_mutex
);
264 set_tlb_bus(trident
, page
, ptr
, addr
);
266 up(&hdr
->block_mutex
);
271 * page allocation for DMA
274 snd_trident_alloc_pages(trident_t
*trident
, snd_pcm_substream_t
*substream
)
276 snd_assert(trident
!= NULL
, return NULL
);
277 snd_assert(substream
!= NULL
, return NULL
);
278 if (substream
->dma_buffer
.dev
.type
== SNDRV_DMA_TYPE_DEV_SG
)
279 return snd_trident_alloc_sg_pages(trident
, substream
);
281 return snd_trident_alloc_cont_pages(trident
, substream
);
286 * release DMA buffer from page table
288 int snd_trident_free_pages(trident_t
*trident
, snd_util_memblk_t
*blk
)
290 snd_util_memhdr_t
*hdr
;
293 snd_assert(trident
!= NULL
, return -EINVAL
);
294 snd_assert(blk
!= NULL
, return -EINVAL
);
296 hdr
= trident
->tlb
.memhdr
;
297 down(&hdr
->block_mutex
);
298 /* reset TLB entries */
299 for (page
= firstpg(blk
); page
<= lastpg(blk
); page
++)
300 set_silent_tlb(trident
, page
);
301 /* free memory block */
302 __snd_util_mem_free(hdr
, blk
);
303 up(&hdr
->block_mutex
);
308 /*----------------------------------------------------------------
309 * memory allocation using multiple pages (for synth)
310 *----------------------------------------------------------------
311 * Unlike the DMA allocation above, non-contiguous pages are
313 *----------------------------------------------------------------*/
317 static int synth_alloc_pages(trident_t
*hw
, snd_util_memblk_t
*blk
);
318 static int synth_free_pages(trident_t
*hw
, snd_util_memblk_t
*blk
);
321 * allocate a synth sample area
324 snd_trident_synth_alloc(trident_t
*hw
, unsigned int size
)
326 snd_util_memblk_t
*blk
;
327 snd_util_memhdr_t
*hdr
= hw
->tlb
.memhdr
;
329 down(&hdr
->block_mutex
);
330 blk
= __snd_util_mem_alloc(hdr
, size
);
332 up(&hdr
->block_mutex
);
335 if (synth_alloc_pages(hw
, blk
)) {
336 __snd_util_mem_free(hdr
, blk
);
337 up(&hdr
->block_mutex
);
340 up(&hdr
->block_mutex
);
346 * free a synth sample area
349 snd_trident_synth_free(trident_t
*hw
, snd_util_memblk_t
*blk
)
351 snd_util_memhdr_t
*hdr
= hw
->tlb
.memhdr
;
353 down(&hdr
->block_mutex
);
354 synth_free_pages(hw
, blk
);
355 __snd_util_mem_free(hdr
, blk
);
356 up(&hdr
->block_mutex
);
362 * reset TLB entry and free kernel page
364 static void clear_tlb(trident_t
*trident
, int page
)
366 void *ptr
= page_to_ptr(trident
, page
);
367 dma_addr_t addr
= page_to_addr(trident
, page
);
368 set_silent_tlb(trident
, page
);
370 struct snd_dma_buffer dmab
;
371 dmab
.dev
.type
= SNDRV_DMA_TYPE_DEV
;
372 dmab
.dev
.dev
= snd_dma_pci_data(trident
->pci
);
375 dmab
.bytes
= ALIGN_PAGE_SIZE
;
376 snd_dma_free_pages(&dmab
);
380 /* check new allocation range */
381 static void get_single_page_range(snd_util_memhdr_t
*hdr
, snd_util_memblk_t
*blk
, int *first_page_ret
, int *last_page_ret
)
384 snd_util_memblk_t
*q
;
385 int first_page
, last_page
;
386 first_page
= firstpg(blk
);
387 if ((p
= blk
->list
.prev
) != &hdr
->block
) {
388 q
= list_entry(p
, snd_util_memblk_t
, list
);
389 if (lastpg(q
) == first_page
)
390 first_page
++; /* first page was already allocated */
392 last_page
= lastpg(blk
);
393 if ((p
= blk
->list
.next
) != &hdr
->block
) {
394 q
= list_entry(p
, snd_util_memblk_t
, list
);
395 if (firstpg(q
) == last_page
)
396 last_page
--; /* last page was already allocated */
398 *first_page_ret
= first_page
;
399 *last_page_ret
= last_page
;
403 * allocate kernel pages and assign them to TLB
405 static int synth_alloc_pages(trident_t
*hw
, snd_util_memblk_t
*blk
)
407 int page
, first_page
, last_page
;
408 struct snd_dma_buffer dmab
;
410 firstpg(blk
) = get_aligned_page(blk
->offset
);
411 lastpg(blk
) = get_aligned_page(blk
->offset
+ blk
->size
- 1);
412 get_single_page_range(hw
->tlb
.memhdr
, blk
, &first_page
, &last_page
);
414 /* allocate a kernel page for each Trident page -
415 * fortunately Trident page size and kernel PAGE_SIZE is identical!
417 for (page
= first_page
; page
<= last_page
; page
++) {
418 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV
, snd_dma_pci_data(hw
->pci
),
419 ALIGN_PAGE_SIZE
, &dmab
) < 0)
421 if (! is_valid_page(dmab
.addr
)) {
422 snd_dma_free_pages(&dmab
);
425 set_tlb_bus(hw
, page
, (unsigned long)dmab
.area
, dmab
.addr
);
430 /* release allocated pages */
431 last_page
= page
- 1;
432 for (page
= first_page
; page
<= last_page
; page
++)
441 static int synth_free_pages(trident_t
*trident
, snd_util_memblk_t
*blk
)
443 int page
, first_page
, last_page
;
445 get_single_page_range(trident
->tlb
.memhdr
, blk
, &first_page
, &last_page
);
446 for (page
= first_page
; page
<= last_page
; page
++)
447 clear_tlb(trident
, page
);
453 * copy_from_user(blk + offset, data, size)
455 int snd_trident_synth_copy_from_user(trident_t
*trident
, snd_util_memblk_t
*blk
, int offset
, const char __user
*data
, int size
)
457 int page
, nextofs
, end_offset
, temp
, temp1
;
459 offset
+= blk
->offset
;
460 end_offset
= offset
+ size
;
461 page
= get_aligned_page(offset
) + 1;
463 nextofs
= aligned_page_offset(page
);
464 temp
= nextofs
- offset
;
465 temp1
= end_offset
- offset
;
468 if (copy_from_user(offset_ptr(trident
, offset
), data
, temp
))
473 } while (offset
< end_offset
);