2 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
3 * Copyright (c) by Takashi Iwai <tiwai@suse.de>
5 * EMU10K1 memory page allocation (PTB area)
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <sound/driver.h>
25 #include <linux/pci.h>
26 #include <linux/time.h>
27 #include <linux/mutex.h>
29 #include <sound/core.h>
30 #include <sound/emu10k1.h>
32 /* page arguments of these two macros are Emu page (4096 bytes), not like
33 * aligned pages in others
35 #define __set_ptb_entry(emu,page,addr) \
36 (((u32 *)(emu)->ptb_pages.area)[page] = cpu_to_le32(((addr) << 1) | (page)))
38 #define UNIT_PAGES (PAGE_SIZE / EMUPAGESIZE)
39 #define MAX_ALIGN_PAGES (MAXPAGES / UNIT_PAGES)
40 /* get aligned page from offset address */
41 #define get_aligned_page(offset) ((offset) >> PAGE_SHIFT)
42 /* get offset address from aligned page */
43 #define aligned_page_offset(page) ((page) << PAGE_SHIFT)
46 /* page size == EMUPAGESIZE */
47 /* fill PTB entrie(s) corresponding to page with addr */
48 #define set_ptb_entry(emu,page,addr) __set_ptb_entry(emu,page,addr)
49 /* fill PTB entrie(s) corresponding to page with silence pointer */
50 #define set_silent_ptb(emu,page) __set_ptb_entry(emu,page,emu->silent_page.addr)
52 /* fill PTB entries -- we need to fill UNIT_PAGES entries */
53 static inline void set_ptb_entry(struct snd_emu10k1
*emu
, int page
, dma_addr_t addr
)
57 for (i
= 0; i
< UNIT_PAGES
; i
++, page
++) {
58 __set_ptb_entry(emu
, page
, addr
);
62 static inline void set_silent_ptb(struct snd_emu10k1
*emu
, int page
)
66 for (i
= 0; i
< UNIT_PAGES
; i
++, page
++)
67 /* do not increment ptr */
68 __set_ptb_entry(emu
, page
, emu
->silent_page
.addr
);
70 #endif /* PAGE_SIZE */
75 static int synth_alloc_pages(struct snd_emu10k1
*hw
, struct snd_emu10k1_memblk
*blk
);
76 static int synth_free_pages(struct snd_emu10k1
*hw
, struct snd_emu10k1_memblk
*blk
);
78 #define get_emu10k1_memblk(l,member) list_entry(l, struct snd_emu10k1_memblk, member)
81 /* initialize emu10k1 part */
82 static void emu10k1_memblk_init(struct snd_emu10k1_memblk
*blk
)
84 blk
->mapped_page
= -1;
85 INIT_LIST_HEAD(&blk
->mapped_link
);
86 INIT_LIST_HEAD(&blk
->mapped_order_link
);
89 blk
->first_page
= get_aligned_page(blk
->mem
.offset
);
90 blk
->last_page
= get_aligned_page(blk
->mem
.offset
+ blk
->mem
.size
- 1);
91 blk
->pages
= blk
->last_page
- blk
->first_page
+ 1;
95 * search empty region on PTB with the given size
97 * if an empty region is found, return the page and store the next mapped block
99 * if not found, return a negative error code.
101 static int search_empty_map_area(struct snd_emu10k1
*emu
, int npages
, struct list_head
**nextp
)
103 int page
= 0, found_page
= -ENOMEM
;
104 int max_size
= npages
;
106 struct list_head
*candidate
= &emu
->mapped_link_head
;
107 struct list_head
*pos
;
109 list_for_each (pos
, &emu
->mapped_link_head
) {
110 struct snd_emu10k1_memblk
*blk
= get_emu10k1_memblk(pos
, mapped_link
);
111 snd_assert(blk
->mapped_page
>= 0, continue);
112 size
= blk
->mapped_page
- page
;
113 if (size
== npages
) {
117 else if (size
> max_size
) {
118 /* we look for the maximum empty hole */
123 page
= blk
->mapped_page
+ blk
->pages
;
125 size
= MAX_ALIGN_PAGES
- page
;
126 if (size
>= max_size
) {
135 * map a memory block onto emu10k1's PTB
137 * call with memblk_lock held
139 static int map_memblk(struct snd_emu10k1
*emu
, struct snd_emu10k1_memblk
*blk
)
142 struct list_head
*next
;
144 page
= search_empty_map_area(emu
, blk
->pages
, &next
);
145 if (page
< 0) /* not found */
147 /* insert this block in the proper position of mapped list */
148 list_add_tail(&blk
->mapped_link
, next
);
149 /* append this as a newest block in order list */
150 list_add_tail(&blk
->mapped_order_link
, &emu
->mapped_order_link_head
);
151 blk
->mapped_page
= page
;
153 for (pg
= blk
->first_page
; pg
<= blk
->last_page
; pg
++) {
154 set_ptb_entry(emu
, page
, emu
->page_addr_table
[pg
]);
162 * return the size of resultant empty pages
164 * call with memblk_lock held
166 static int unmap_memblk(struct snd_emu10k1
*emu
, struct snd_emu10k1_memblk
*blk
)
168 int start_page
, end_page
, mpage
, pg
;
170 struct snd_emu10k1_memblk
*q
;
172 /* calculate the expected size of empty region */
173 if ((p
= blk
->mapped_link
.prev
) != &emu
->mapped_link_head
) {
174 q
= get_emu10k1_memblk(p
, mapped_link
);
175 start_page
= q
->mapped_page
+ q
->pages
;
178 if ((p
= blk
->mapped_link
.next
) != &emu
->mapped_link_head
) {
179 q
= get_emu10k1_memblk(p
, mapped_link
);
180 end_page
= q
->mapped_page
;
182 end_page
= MAX_ALIGN_PAGES
;
185 list_del(&blk
->mapped_link
);
186 list_del(&blk
->mapped_order_link
);
188 mpage
= blk
->mapped_page
;
189 for (pg
= blk
->first_page
; pg
<= blk
->last_page
; pg
++) {
190 set_silent_ptb(emu
, mpage
);
193 blk
->mapped_page
= -1;
194 return end_page
- start_page
; /* return the new empty size */
198 * search empty pages with the given size, and create a memory block
200 * unlike synth_alloc the memory block is aligned to the page start
202 static struct snd_emu10k1_memblk
*
203 search_empty(struct snd_emu10k1
*emu
, int size
)
206 struct snd_emu10k1_memblk
*blk
;
209 psize
= get_aligned_page(size
+ PAGE_SIZE
-1);
211 list_for_each(p
, &emu
->memhdr
->block
) {
212 blk
= get_emu10k1_memblk(p
, mem
.list
);
213 if (page
+ psize
<= blk
->first_page
)
215 page
= blk
->last_page
+ 1;
217 if (page
+ psize
> emu
->max_cache_pages
)
221 /* create a new memory block */
222 blk
= (struct snd_emu10k1_memblk
*)__snd_util_memblk_new(emu
->memhdr
, psize
<< PAGE_SHIFT
, p
->prev
);
225 blk
->mem
.offset
= aligned_page_offset(page
); /* set aligned offset */
226 emu10k1_memblk_init(blk
);
232 * check if the given pointer is valid for pages
234 static int is_valid_page(struct snd_emu10k1
*emu
, dma_addr_t addr
)
236 if (addr
& ~emu
->dma_mask
) {
237 snd_printk(KERN_ERR
"max memory size is 0x%lx (addr = 0x%lx)!!\n", emu
->dma_mask
, (unsigned long)addr
);
240 if (addr
& (EMUPAGESIZE
-1)) {
241 snd_printk(KERN_ERR
"page is not aligned\n");
248 * map the given memory block on PTB.
249 * if the block is already mapped, update the link order.
250 * if no empty pages are found, tries to release unsed memory blocks
251 * and retry the mapping.
253 int snd_emu10k1_memblk_map(struct snd_emu10k1
*emu
, struct snd_emu10k1_memblk
*blk
)
257 struct list_head
*p
, *nextp
;
258 struct snd_emu10k1_memblk
*deleted
;
261 spin_lock_irqsave(&emu
->memblk_lock
, flags
);
262 if (blk
->mapped_page
>= 0) {
263 /* update order link */
264 list_del(&blk
->mapped_order_link
);
265 list_add_tail(&blk
->mapped_order_link
, &emu
->mapped_order_link_head
);
266 spin_unlock_irqrestore(&emu
->memblk_lock
, flags
);
269 if ((err
= map_memblk(emu
, blk
)) < 0) {
270 /* no enough page - try to unmap some blocks */
271 /* starting from the oldest block */
272 p
= emu
->mapped_order_link_head
.next
;
273 for (; p
!= &emu
->mapped_order_link_head
; p
= nextp
) {
275 deleted
= get_emu10k1_memblk(p
, mapped_order_link
);
276 if (deleted
->map_locked
)
278 size
= unmap_memblk(emu
, deleted
);
279 if (size
>= blk
->pages
) {
280 /* ok the empty region is enough large */
281 err
= map_memblk(emu
, blk
);
286 spin_unlock_irqrestore(&emu
->memblk_lock
, flags
);
291 * page allocation for DMA
293 struct snd_util_memblk
*
294 snd_emu10k1_alloc_pages(struct snd_emu10k1
*emu
, struct snd_pcm_substream
*substream
)
296 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
297 struct snd_sg_buf
*sgbuf
= snd_pcm_substream_sgbuf(substream
);
298 struct snd_util_memhdr
*hdr
;
299 struct snd_emu10k1_memblk
*blk
;
302 snd_assert(emu
, return NULL
);
303 snd_assert(runtime
->dma_bytes
> 0 && runtime
->dma_bytes
< MAXPAGES
* EMUPAGESIZE
, return NULL
);
305 snd_assert(hdr
, return NULL
);
307 mutex_lock(&hdr
->block_mutex
);
308 blk
= search_empty(emu
, runtime
->dma_bytes
);
310 mutex_unlock(&hdr
->block_mutex
);
313 /* fill buffer addresses but pointers are not stored so that
314 * snd_free_pci_page() is not called in in synth_free()
317 for (page
= blk
->first_page
; page
<= blk
->last_page
; page
++, idx
++) {
319 #ifdef CONFIG_SND_DEBUG
320 if (idx
>= sgbuf
->pages
) {
321 printk(KERN_ERR
"emu: pages overflow! (%d-%d) for %d\n",
322 blk
->first_page
, blk
->last_page
, sgbuf
->pages
);
323 mutex_unlock(&hdr
->block_mutex
);
327 addr
= sgbuf
->table
[idx
].addr
;
328 if (! is_valid_page(emu
, addr
)) {
329 printk(KERN_ERR
"emu: failure page = %d\n", idx
);
330 mutex_unlock(&hdr
->block_mutex
);
333 emu
->page_addr_table
[page
] = addr
;
334 emu
->page_ptr_table
[page
] = NULL
;
337 /* set PTB entries */
338 blk
->map_locked
= 1; /* do not unmap this block! */
339 err
= snd_emu10k1_memblk_map(emu
, blk
);
341 __snd_util_mem_free(hdr
, (struct snd_util_memblk
*)blk
);
342 mutex_unlock(&hdr
->block_mutex
);
345 mutex_unlock(&hdr
->block_mutex
);
346 return (struct snd_util_memblk
*)blk
;
351 * release DMA buffer from page table
353 int snd_emu10k1_free_pages(struct snd_emu10k1
*emu
, struct snd_util_memblk
*blk
)
355 snd_assert(emu
&& blk
, return -EINVAL
);
356 return snd_emu10k1_synth_free(emu
, blk
);
361 * memory allocation using multiple pages (for synth)
362 * Unlike the DMA allocation above, non-contiguous pages are assined.
366 * allocate a synth sample area
368 struct snd_util_memblk
*
369 snd_emu10k1_synth_alloc(struct snd_emu10k1
*hw
, unsigned int size
)
371 struct snd_emu10k1_memblk
*blk
;
372 struct snd_util_memhdr
*hdr
= hw
->memhdr
;
374 mutex_lock(&hdr
->block_mutex
);
375 blk
= (struct snd_emu10k1_memblk
*)__snd_util_mem_alloc(hdr
, size
);
377 mutex_unlock(&hdr
->block_mutex
);
380 if (synth_alloc_pages(hw
, blk
)) {
381 __snd_util_mem_free(hdr
, (struct snd_util_memblk
*)blk
);
382 mutex_unlock(&hdr
->block_mutex
);
385 snd_emu10k1_memblk_map(hw
, blk
);
386 mutex_unlock(&hdr
->block_mutex
);
387 return (struct snd_util_memblk
*)blk
;
392 * free a synth sample area
395 snd_emu10k1_synth_free(struct snd_emu10k1
*emu
, struct snd_util_memblk
*memblk
)
397 struct snd_util_memhdr
*hdr
= emu
->memhdr
;
398 struct snd_emu10k1_memblk
*blk
= (struct snd_emu10k1_memblk
*)memblk
;
401 mutex_lock(&hdr
->block_mutex
);
402 spin_lock_irqsave(&emu
->memblk_lock
, flags
);
403 if (blk
->mapped_page
>= 0)
404 unmap_memblk(emu
, blk
);
405 spin_unlock_irqrestore(&emu
->memblk_lock
, flags
);
406 synth_free_pages(emu
, blk
);
407 __snd_util_mem_free(hdr
, memblk
);
408 mutex_unlock(&hdr
->block_mutex
);
413 /* check new allocation range */
414 static void get_single_page_range(struct snd_util_memhdr
*hdr
,
415 struct snd_emu10k1_memblk
*blk
,
416 int *first_page_ret
, int *last_page_ret
)
419 struct snd_emu10k1_memblk
*q
;
420 int first_page
, last_page
;
421 first_page
= blk
->first_page
;
422 if ((p
= blk
->mem
.list
.prev
) != &hdr
->block
) {
423 q
= get_emu10k1_memblk(p
, mem
.list
);
424 if (q
->last_page
== first_page
)
425 first_page
++; /* first page was already allocated */
427 last_page
= blk
->last_page
;
428 if ((p
= blk
->mem
.list
.next
) != &hdr
->block
) {
429 q
= get_emu10k1_memblk(p
, mem
.list
);
430 if (q
->first_page
== last_page
)
431 last_page
--; /* last page was already allocated */
433 *first_page_ret
= first_page
;
434 *last_page_ret
= last_page
;
438 * allocate kernel pages
440 static int synth_alloc_pages(struct snd_emu10k1
*emu
, struct snd_emu10k1_memblk
*blk
)
442 int page
, first_page
, last_page
;
443 struct snd_dma_buffer dmab
;
445 emu10k1_memblk_init(blk
);
446 get_single_page_range(emu
->memhdr
, blk
, &first_page
, &last_page
);
447 /* allocate kernel pages */
448 for (page
= first_page
; page
<= last_page
; page
++) {
449 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV
, snd_dma_pci_data(emu
->pci
),
450 PAGE_SIZE
, &dmab
) < 0)
452 if (! is_valid_page(emu
, dmab
.addr
)) {
453 snd_dma_free_pages(&dmab
);
456 emu
->page_addr_table
[page
] = dmab
.addr
;
457 emu
->page_ptr_table
[page
] = dmab
.area
;
462 /* release allocated pages */
463 last_page
= page
- 1;
464 for (page
= first_page
; page
<= last_page
; page
++) {
465 dmab
.area
= emu
->page_ptr_table
[page
];
466 dmab
.addr
= emu
->page_addr_table
[page
];
467 dmab
.bytes
= PAGE_SIZE
;
468 snd_dma_free_pages(&dmab
);
469 emu
->page_addr_table
[page
] = 0;
470 emu
->page_ptr_table
[page
] = NULL
;
479 static int synth_free_pages(struct snd_emu10k1
*emu
, struct snd_emu10k1_memblk
*blk
)
481 int page
, first_page
, last_page
;
482 struct snd_dma_buffer dmab
;
484 get_single_page_range(emu
->memhdr
, blk
, &first_page
, &last_page
);
485 dmab
.dev
.type
= SNDRV_DMA_TYPE_DEV
;
486 dmab
.dev
.dev
= snd_dma_pci_data(emu
->pci
);
487 for (page
= first_page
; page
<= last_page
; page
++) {
488 if (emu
->page_ptr_table
[page
] == NULL
)
490 dmab
.area
= emu
->page_ptr_table
[page
];
491 dmab
.addr
= emu
->page_addr_table
[page
];
492 dmab
.bytes
= PAGE_SIZE
;
493 snd_dma_free_pages(&dmab
);
494 emu
->page_addr_table
[page
] = 0;
495 emu
->page_ptr_table
[page
] = NULL
;
501 /* calculate buffer pointer from offset address */
502 static inline void *offset_ptr(struct snd_emu10k1
*emu
, int page
, int offset
)
505 snd_assert(page
>= 0 && page
< emu
->max_cache_pages
, return NULL
);
506 ptr
= emu
->page_ptr_table
[page
];
508 printk(KERN_ERR
"emu10k1: access to NULL ptr: page = %d\n", page
);
511 ptr
+= offset
& (PAGE_SIZE
- 1);
516 * bzero(blk + offset, size)
518 int snd_emu10k1_synth_bzero(struct snd_emu10k1
*emu
, struct snd_util_memblk
*blk
,
519 int offset
, int size
)
521 int page
, nextofs
, end_offset
, temp
, temp1
;
523 struct snd_emu10k1_memblk
*p
= (struct snd_emu10k1_memblk
*)blk
;
525 offset
+= blk
->offset
& (PAGE_SIZE
- 1);
526 end_offset
= offset
+ size
;
527 page
= get_aligned_page(offset
);
529 nextofs
= aligned_page_offset(page
+ 1);
530 temp
= nextofs
- offset
;
531 temp1
= end_offset
- offset
;
534 ptr
= offset_ptr(emu
, page
+ p
->first_page
, offset
);
536 memset(ptr
, 0, temp
);
539 } while (offset
< end_offset
);
544 * copy_from_user(blk + offset, data, size)
546 int snd_emu10k1_synth_copy_from_user(struct snd_emu10k1
*emu
, struct snd_util_memblk
*blk
,
547 int offset
, const char __user
*data
, int size
)
549 int page
, nextofs
, end_offset
, temp
, temp1
;
551 struct snd_emu10k1_memblk
*p
= (struct snd_emu10k1_memblk
*)blk
;
553 offset
+= blk
->offset
& (PAGE_SIZE
- 1);
554 end_offset
= offset
+ size
;
555 page
= get_aligned_page(offset
);
557 nextofs
= aligned_page_offset(page
+ 1);
558 temp
= nextofs
- offset
;
559 temp1
= end_offset
- offset
;
562 ptr
= offset_ptr(emu
, page
+ p
->first_page
, offset
);
563 if (ptr
&& copy_from_user(ptr
, data
, temp
))
568 } while (offset
< end_offset
);