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(struct snd_trident
*trident
, int page
,
72 unsigned long ptr
, dma_addr_t addr
)
75 __set_tlb_bus(trident
, page
, ptr
, addr
);
76 __set_tlb_bus(trident
, page
+1, ptr
+ SNDRV_TRIDENT_PAGE_SIZE
, addr
+ SNDRV_TRIDENT_PAGE_SIZE
);
78 static inline void set_silent_tlb(struct snd_trident
*trident
, int page
)
81 __set_tlb_bus(trident
, page
, (unsigned long)trident
->tlb
.silent_page
.area
, trident
->tlb
.silent_page
.addr
);
82 __set_tlb_bus(trident
, page
+1, (unsigned long)trident
->tlb
.silent_page
.area
, trident
->tlb
.silent_page
.addr
);
87 #define UNIT_PAGES (PAGE_SIZE / SNDRV_TRIDENT_PAGE_SIZE)
88 #define ALIGN_PAGE_SIZE (SNDRV_TRIDENT_PAGE_SIZE * UNIT_PAGES)
89 #define MAX_ALIGN_PAGES (SNDRV_TRIDENT_MAX_PAGES / UNIT_PAGES)
90 /* Note: if alignment doesn't match to the maximum size, the last few blocks
91 * become unusable. To use such blocks, you'll need to check the validity
92 * of accessing page in set_tlb_bus and set_silent_tlb. search_empty()
93 * should also check it, too.
95 #define get_aligned_page(offset) ((offset) / ALIGN_PAGE_SIZE)
96 #define aligned_page_offset(page) ((page) * ALIGN_PAGE_SIZE)
97 #define page_to_ptr(trident,page) __tlb_to_ptr(trident, (page) * UNIT_PAGES)
98 #define page_to_addr(trident,page) __tlb_to_addr(trident, (page) * UNIT_PAGES)
100 /* fill TLB entries -- UNIT_PAGES entries must be filled */
101 static inline void set_tlb_bus(struct snd_trident
*trident
, int page
,
102 unsigned long ptr
, dma_addr_t addr
)
106 for (i
= 0; i
< UNIT_PAGES
; i
++, page
++) {
107 __set_tlb_bus(trident
, page
, ptr
, addr
);
108 ptr
+= SNDRV_TRIDENT_PAGE_SIZE
;
109 addr
+= SNDRV_TRIDENT_PAGE_SIZE
;
112 static inline void set_silent_tlb(struct snd_trident
*trident
, int page
)
116 for (i
= 0; i
< UNIT_PAGES
; i
++, page
++)
117 __set_tlb_bus(trident
, page
, (unsigned long)trident
->tlb
.silent_page
.area
, trident
->tlb
.silent_page
.addr
);
120 #endif /* PAGE_SIZE */
122 /* calculate buffer pointer from offset address */
123 static inline void *offset_ptr(struct snd_trident
*trident
, int offset
)
126 ptr
= page_to_ptr(trident
, get_aligned_page(offset
));
127 ptr
+= offset
% ALIGN_PAGE_SIZE
;
131 /* first and last (aligned) pages of memory block */
132 #define firstpg(blk) (((struct snd_trident_memblk_arg *)snd_util_memblk_argptr(blk))->first_page)
133 #define lastpg(blk) (((struct snd_trident_memblk_arg *)snd_util_memblk_argptr(blk))->last_page)
136 * search empty pages which may contain given size
138 static struct snd_util_memblk
*
139 search_empty(struct snd_util_memhdr
*hdr
, int size
)
141 struct snd_util_memblk
*blk
, *prev
;
145 psize
= get_aligned_page(size
+ ALIGN_PAGE_SIZE
-1);
148 list_for_each(p
, &hdr
->block
) {
149 blk
= list_entry(p
, struct snd_util_memblk
, list
);
150 if (page
+ psize
<= firstpg(blk
))
152 page
= lastpg(blk
) + 1;
154 if (page
+ psize
> MAX_ALIGN_PAGES
)
158 /* create a new memory block */
159 blk
= __snd_util_memblk_new(hdr
, psize
* ALIGN_PAGE_SIZE
, p
->prev
);
162 blk
->offset
= aligned_page_offset(page
); /* set aligned offset */
164 lastpg(blk
) = page
+ psize
- 1;
170 * check if the given pointer is valid for pages
172 static int is_valid_page(unsigned long ptr
)
174 if (ptr
& ~0x3fffffffUL
) {
175 snd_printk(KERN_ERR
"max memory size is 1GB!!\n");
178 if (ptr
& (SNDRV_TRIDENT_PAGE_SIZE
-1)) {
179 snd_printk(KERN_ERR
"page is not aligned\n");
186 * page allocation for DMA (Scatter-Gather version)
188 static struct snd_util_memblk
*
189 snd_trident_alloc_sg_pages(struct snd_trident
*trident
,
190 struct snd_pcm_substream
*substream
)
192 struct snd_util_memhdr
*hdr
;
193 struct snd_util_memblk
*blk
;
194 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
196 struct snd_sg_buf
*sgbuf
= snd_pcm_substream_sgbuf(substream
);
198 snd_assert(runtime
->dma_bytes
> 0 && runtime
->dma_bytes
<= SNDRV_TRIDENT_MAX_PAGES
* SNDRV_TRIDENT_PAGE_SIZE
, return NULL
);
199 hdr
= trident
->tlb
.memhdr
;
200 snd_assert(hdr
!= NULL
, return NULL
);
204 down(&hdr
->block_mutex
);
205 blk
= search_empty(hdr
, runtime
->dma_bytes
);
207 up(&hdr
->block_mutex
);
210 if (lastpg(blk
) - firstpg(blk
) >= sgbuf
->pages
) {
211 snd_printk(KERN_ERR
"page calculation doesn't match: allocated pages = %d, trident = %d/%d\n", sgbuf
->pages
, firstpg(blk
), lastpg(blk
));
212 __snd_util_mem_free(hdr
, blk
);
213 up(&hdr
->block_mutex
);
217 /* set TLB entries */
219 for (page
= firstpg(blk
); page
<= lastpg(blk
); page
++, idx
++) {
220 dma_addr_t addr
= sgbuf
->table
[idx
].addr
;
221 unsigned long ptr
= (unsigned long)sgbuf
->table
[idx
].buf
;
222 if (! is_valid_page(addr
)) {
223 __snd_util_mem_free(hdr
, blk
);
224 up(&hdr
->block_mutex
);
227 set_tlb_bus(trident
, page
, ptr
, addr
);
229 up(&hdr
->block_mutex
);
234 * page allocation for DMA (contiguous version)
236 static struct snd_util_memblk
*
237 snd_trident_alloc_cont_pages(struct snd_trident
*trident
,
238 struct snd_pcm_substream
*substream
)
240 struct snd_util_memhdr
*hdr
;
241 struct snd_util_memblk
*blk
;
243 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
247 snd_assert(runtime
->dma_bytes
> 0 && runtime
->dma_bytes
<= SNDRV_TRIDENT_MAX_PAGES
* SNDRV_TRIDENT_PAGE_SIZE
, return NULL
);
248 hdr
= trident
->tlb
.memhdr
;
249 snd_assert(hdr
!= NULL
, return NULL
);
251 down(&hdr
->block_mutex
);
252 blk
= search_empty(hdr
, runtime
->dma_bytes
);
254 up(&hdr
->block_mutex
);
258 /* set TLB entries */
259 addr
= runtime
->dma_addr
;
260 ptr
= (unsigned long)runtime
->dma_area
;
261 for (page
= firstpg(blk
); page
<= lastpg(blk
); page
++,
262 ptr
+= SNDRV_TRIDENT_PAGE_SIZE
, addr
+= SNDRV_TRIDENT_PAGE_SIZE
) {
263 if (! is_valid_page(addr
)) {
264 __snd_util_mem_free(hdr
, blk
);
265 up(&hdr
->block_mutex
);
268 set_tlb_bus(trident
, page
, ptr
, addr
);
270 up(&hdr
->block_mutex
);
275 * page allocation for DMA
277 struct snd_util_memblk
*
278 snd_trident_alloc_pages(struct snd_trident
*trident
,
279 struct snd_pcm_substream
*substream
)
281 snd_assert(trident
!= NULL
, return NULL
);
282 snd_assert(substream
!= NULL
, return NULL
);
283 if (substream
->dma_buffer
.dev
.type
== SNDRV_DMA_TYPE_DEV_SG
)
284 return snd_trident_alloc_sg_pages(trident
, substream
);
286 return snd_trident_alloc_cont_pages(trident
, substream
);
291 * release DMA buffer from page table
293 int snd_trident_free_pages(struct snd_trident
*trident
,
294 struct snd_util_memblk
*blk
)
296 struct snd_util_memhdr
*hdr
;
299 snd_assert(trident
!= NULL
, return -EINVAL
);
300 snd_assert(blk
!= NULL
, return -EINVAL
);
302 hdr
= trident
->tlb
.memhdr
;
303 down(&hdr
->block_mutex
);
304 /* reset TLB entries */
305 for (page
= firstpg(blk
); page
<= lastpg(blk
); page
++)
306 set_silent_tlb(trident
, page
);
307 /* free memory block */
308 __snd_util_mem_free(hdr
, blk
);
309 up(&hdr
->block_mutex
);
314 /*----------------------------------------------------------------
315 * memory allocation using multiple pages (for synth)
316 *----------------------------------------------------------------
317 * Unlike the DMA allocation above, non-contiguous pages are
319 *----------------------------------------------------------------*/
323 static int synth_alloc_pages(struct snd_trident
*hw
, struct snd_util_memblk
*blk
);
324 static int synth_free_pages(struct snd_trident
*hw
, struct snd_util_memblk
*blk
);
327 * allocate a synth sample area
329 struct snd_util_memblk
*
330 snd_trident_synth_alloc(struct snd_trident
*hw
, unsigned int size
)
332 struct snd_util_memblk
*blk
;
333 struct snd_util_memhdr
*hdr
= hw
->tlb
.memhdr
;
335 down(&hdr
->block_mutex
);
336 blk
= __snd_util_mem_alloc(hdr
, size
);
338 up(&hdr
->block_mutex
);
341 if (synth_alloc_pages(hw
, blk
)) {
342 __snd_util_mem_free(hdr
, blk
);
343 up(&hdr
->block_mutex
);
346 up(&hdr
->block_mutex
);
352 * free a synth sample area
355 snd_trident_synth_free(struct snd_trident
*hw
, struct snd_util_memblk
*blk
)
357 struct snd_util_memhdr
*hdr
= hw
->tlb
.memhdr
;
359 down(&hdr
->block_mutex
);
360 synth_free_pages(hw
, blk
);
361 __snd_util_mem_free(hdr
, blk
);
362 up(&hdr
->block_mutex
);
368 * reset TLB entry and free kernel page
370 static void clear_tlb(struct snd_trident
*trident
, int page
)
372 void *ptr
= page_to_ptr(trident
, page
);
373 dma_addr_t addr
= page_to_addr(trident
, page
);
374 set_silent_tlb(trident
, page
);
376 struct snd_dma_buffer dmab
;
377 dmab
.dev
.type
= SNDRV_DMA_TYPE_DEV
;
378 dmab
.dev
.dev
= snd_dma_pci_data(trident
->pci
);
381 dmab
.bytes
= ALIGN_PAGE_SIZE
;
382 snd_dma_free_pages(&dmab
);
386 /* check new allocation range */
387 static void get_single_page_range(struct snd_util_memhdr
*hdr
,
388 struct snd_util_memblk
*blk
,
389 int *first_page_ret
, int *last_page_ret
)
392 struct snd_util_memblk
*q
;
393 int first_page
, last_page
;
394 first_page
= firstpg(blk
);
395 if ((p
= blk
->list
.prev
) != &hdr
->block
) {
396 q
= list_entry(p
, struct snd_util_memblk
, list
);
397 if (lastpg(q
) == first_page
)
398 first_page
++; /* first page was already allocated */
400 last_page
= lastpg(blk
);
401 if ((p
= blk
->list
.next
) != &hdr
->block
) {
402 q
= list_entry(p
, struct snd_util_memblk
, list
);
403 if (firstpg(q
) == last_page
)
404 last_page
--; /* last page was already allocated */
406 *first_page_ret
= first_page
;
407 *last_page_ret
= last_page
;
411 * allocate kernel pages and assign them to TLB
413 static int synth_alloc_pages(struct snd_trident
*hw
, struct snd_util_memblk
*blk
)
415 int page
, first_page
, last_page
;
416 struct snd_dma_buffer dmab
;
418 firstpg(blk
) = get_aligned_page(blk
->offset
);
419 lastpg(blk
) = get_aligned_page(blk
->offset
+ blk
->size
- 1);
420 get_single_page_range(hw
->tlb
.memhdr
, blk
, &first_page
, &last_page
);
422 /* allocate a kernel page for each Trident page -
423 * fortunately Trident page size and kernel PAGE_SIZE is identical!
425 for (page
= first_page
; page
<= last_page
; page
++) {
426 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV
, snd_dma_pci_data(hw
->pci
),
427 ALIGN_PAGE_SIZE
, &dmab
) < 0)
429 if (! is_valid_page(dmab
.addr
)) {
430 snd_dma_free_pages(&dmab
);
433 set_tlb_bus(hw
, page
, (unsigned long)dmab
.area
, dmab
.addr
);
438 /* release allocated pages */
439 last_page
= page
- 1;
440 for (page
= first_page
; page
<= last_page
; page
++)
449 static int synth_free_pages(struct snd_trident
*trident
, struct snd_util_memblk
*blk
)
451 int page
, first_page
, last_page
;
453 get_single_page_range(trident
->tlb
.memhdr
, blk
, &first_page
, &last_page
);
454 for (page
= first_page
; page
<= last_page
; page
++)
455 clear_tlb(trident
, page
);
461 * copy_from_user(blk + offset, data, size)
463 int snd_trident_synth_copy_from_user(struct snd_trident
*trident
,
464 struct snd_util_memblk
*blk
,
465 int offset
, const char __user
*data
, int size
)
467 int page
, nextofs
, end_offset
, temp
, temp1
;
469 offset
+= blk
->offset
;
470 end_offset
= offset
+ size
;
471 page
= get_aligned_page(offset
) + 1;
473 nextofs
= aligned_page_offset(page
);
474 temp
= nextofs
- offset
;
475 temp1
= end_offset
- offset
;
478 if (copy_from_user(offset_ptr(trident
, offset
), data
, temp
))
483 } while (offset
< end_offset
);