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 <sound/core.h>
28 #include <sound/emu10k1.h>
30 /* page arguments of these two macros are Emu page (4096 bytes), not like
31 * aligned pages in others
33 #define __set_ptb_entry(emu,page,addr) \
34 (((u32 *)(emu)->ptb_pages.area)[page] = cpu_to_le32(((addr) << 1) | (page)))
36 #define UNIT_PAGES (PAGE_SIZE / EMUPAGESIZE)
37 #define MAX_ALIGN_PAGES (MAXPAGES / UNIT_PAGES)
38 /* get aligned page from offset address */
39 #define get_aligned_page(offset) ((offset) >> PAGE_SHIFT)
40 /* get offset address from aligned page */
41 #define aligned_page_offset(page) ((page) << PAGE_SHIFT)
44 /* page size == EMUPAGESIZE */
45 /* fill PTB entrie(s) corresponding to page with addr */
46 #define set_ptb_entry(emu,page,addr) __set_ptb_entry(emu,page,addr)
47 /* fill PTB entrie(s) corresponding to page with silence pointer */
48 #define set_silent_ptb(emu,page) __set_ptb_entry(emu,page,emu->silent_page.addr)
50 /* fill PTB entries -- we need to fill UNIT_PAGES entries */
51 static inline void set_ptb_entry(emu10k1_t *emu, int page, dma_addr_t addr)
55 for (i = 0; i < UNIT_PAGES; i++, page++) {
56 __set_ptb_entry(emu, page, addr);
60 static inline void set_silent_ptb(emu10k1_t *emu, int page)
64 for (i = 0; i < UNIT_PAGES; i++, page++)
65 /* do not increment ptr */
66 __set_ptb_entry(emu, page, emu->silent_page.addr);
68 #endif /* PAGE_SIZE */
73 static int synth_alloc_pages(emu10k1_t *hw, emu10k1_memblk_t *blk);
74 static int synth_free_pages(emu10k1_t *hw, emu10k1_memblk_t *blk);
76 #define get_emu10k1_memblk(l,member) list_entry(l, emu10k1_memblk_t, member)
79 /* initialize emu10k1 part */
80 static void emu10k1_memblk_init(emu10k1_memblk_t *blk)
82 blk->mapped_page = -1;
83 INIT_LIST_HEAD(&blk->mapped_link);
84 INIT_LIST_HEAD(&blk->mapped_order_link);
87 blk->first_page = get_aligned_page(blk->mem.offset);
88 blk->last_page = get_aligned_page(blk->mem.offset + blk->mem.size - 1);
89 blk->pages = blk->last_page - blk->first_page + 1;
93 * search empty region on PTB with the given size
95 * if an empty region is found, return the page and store the next mapped block
97 * if not found, return a negative error code.
99 static int search_empty_map_area(emu10k1_t *emu, int npages, struct list_head **nextp)
101 int page = 0, found_page = -ENOMEM;
102 int max_size = npages;
104 struct list_head *candidate = &emu->mapped_link_head;
105 struct list_head *pos;
107 list_for_each (pos, &emu->mapped_link_head) {
108 emu10k1_memblk_t *blk = get_emu10k1_memblk(pos, mapped_link);
109 snd_assert(blk->mapped_page >= 0, continue);
110 size = blk->mapped_page - page;
111 if (size == npages) {
115 else if (size > max_size) {
116 /* we look for the maximum empty hole */
121 page = blk->mapped_page + blk->pages;
123 size = MAX_ALIGN_PAGES - page;
124 if (size >= max_size) {
133 * map a memory block onto emu10k1's PTB
135 * call with memblk_lock held
137 static int map_memblk(emu10k1_t *emu, emu10k1_memblk_t *blk)
140 struct list_head *next;
142 page = search_empty_map_area(emu, blk->pages, &next);
143 if (page < 0) /* not found */
145 /* insert this block in the proper position of mapped list */
146 list_add_tail(&blk->mapped_link, next);
147 /* append this as a newest block in order list */
148 list_add_tail(&blk->mapped_order_link, &emu->mapped_order_link_head);
149 blk->mapped_page = page;
151 for (pg = blk->first_page; pg <= blk->last_page; pg++) {
152 set_ptb_entry(emu, page, emu->page_addr_table[pg]);
160 * return the size of resultant empty pages
162 * call with memblk_lock held
164 static int unmap_memblk(emu10k1_t *emu, emu10k1_memblk_t *blk)
166 int start_page, end_page, mpage, pg;
170 /* calculate the expected size of empty region */
171 if ((p = blk->mapped_link.prev) != &emu->mapped_link_head) {
172 q = get_emu10k1_memblk(p, mapped_link);
173 start_page = q->mapped_page + q->pages;
176 if ((p = blk->mapped_link.next) != &emu->mapped_link_head) {
177 q = get_emu10k1_memblk(p, mapped_link);
178 end_page = q->mapped_page;
180 end_page = MAX_ALIGN_PAGES;
183 list_del(&blk->mapped_link);
184 list_del(&blk->mapped_order_link);
186 mpage = blk->mapped_page;
187 for (pg = blk->first_page; pg <= blk->last_page; pg++) {
188 set_silent_ptb(emu, mpage);
191 blk->mapped_page = -1;
192 return end_page - start_page; /* return the new empty size */
196 * search empty pages with the given size, and create a memory block
198 * unlike synth_alloc the memory block is aligned to the page start
200 static emu10k1_memblk_t *
201 search_empty(emu10k1_t *emu, int size)
204 emu10k1_memblk_t *blk;
207 psize = get_aligned_page(size + PAGE_SIZE -1);
209 list_for_each(p, &emu->memhdr->block) {
210 blk = get_emu10k1_memblk(p, mem.list);
211 if (page + psize <= blk->first_page)
213 page = blk->last_page + 1;
215 if (page + psize > emu->max_cache_pages)
219 /* create a new memory block */
220 blk = (emu10k1_memblk_t *)__snd_util_memblk_new(emu->memhdr, psize << PAGE_SHIFT, p->prev);
223 blk->mem.offset = aligned_page_offset(page); /* set aligned offset */
224 emu10k1_memblk_init(blk);
230 * check if the given pointer is valid for pages
232 static int is_valid_page(emu10k1_t *emu, dma_addr_t addr)
234 if (addr & ~emu->dma_mask) {
235 snd_printk(KERN_ERR "max memory size is 0x%lx (addr = 0x%lx)!!\n", emu->dma_mask, (unsigned long)addr);
238 if (addr & (EMUPAGESIZE-1)) {
239 snd_printk(KERN_ERR "page is not aligned\n");
246 * map the given memory block on PTB.
247 * if the block is already mapped, update the link order.
248 * if no empty pages are found, tries to release unsed memory blocks
249 * and retry the mapping.
251 int snd_emu10k1_memblk_map(emu10k1_t *emu, emu10k1_memblk_t *blk)
255 struct list_head *p, *nextp;
256 emu10k1_memblk_t *deleted;
259 spin_lock_irqsave(&emu->memblk_lock, flags);
260 if (blk->mapped_page >= 0) {
261 /* update order link */
262 list_del(&blk->mapped_order_link);
263 list_add_tail(&blk->mapped_order_link, &emu->mapped_order_link_head);
264 spin_unlock_irqrestore(&emu->memblk_lock, flags);
267 if ((err = map_memblk(emu, blk)) < 0) {
268 /* no enough page - try to unmap some blocks */
269 /* starting from the oldest block */
270 p = emu->mapped_order_link_head.next;
271 for (; p != &emu->mapped_order_link_head; p = nextp) {
273 deleted = get_emu10k1_memblk(p, mapped_order_link);
274 if (deleted->map_locked)
276 size = unmap_memblk(emu, deleted);
277 if (size >= blk->pages) {
278 /* ok the empty region is enough large */
279 err = map_memblk(emu, blk);
284 spin_unlock_irqrestore(&emu->memblk_lock, flags);
289 * page allocation for DMA
292 snd_emu10k1_alloc_pages(emu10k1_t *emu, snd_pcm_substream_t *substream)
294 snd_pcm_runtime_t *runtime = substream->runtime;
295 struct snd_sg_buf *sgbuf = snd_pcm_substream_sgbuf(substream);
296 snd_util_memhdr_t *hdr;
297 emu10k1_memblk_t *blk;
300 snd_assert(emu, return NULL);
301 snd_assert(runtime->dma_bytes > 0 && runtime->dma_bytes < MAXPAGES * EMUPAGESIZE, return NULL);
303 snd_assert(hdr, return NULL);
305 down(&hdr->block_mutex);
306 blk = search_empty(emu, runtime->dma_bytes);
308 up(&hdr->block_mutex);
311 /* fill buffer addresses but pointers are not stored so that
312 * snd_free_pci_page() is not called in in synth_free()
315 for (page = blk->first_page; page <= blk->last_page; page++, idx++) {
317 #ifdef CONFIG_SND_DEBUG
318 if (idx >= sgbuf->pages) {
319 printk(KERN_ERR "emu: pages overflow! (%d-%d) for %d\n",
320 blk->first_page, blk->last_page, sgbuf->pages);
321 up(&hdr->block_mutex);
325 addr = sgbuf->table[idx].addr;
326 if (! is_valid_page(emu, addr)) {
327 printk(KERN_ERR "emu: failure page = %d\n", idx);
328 up(&hdr->block_mutex);
331 emu->page_addr_table[page] = addr;
332 emu->page_ptr_table[page] = NULL;
335 /* set PTB entries */
336 blk->map_locked = 1; /* do not unmap this block! */
337 err = snd_emu10k1_memblk_map(emu, blk);
339 __snd_util_mem_free(hdr, (snd_util_memblk_t *)blk);
340 up(&hdr->block_mutex);
343 up(&hdr->block_mutex);
344 return (snd_util_memblk_t *)blk;
349 * release DMA buffer from page table
351 int snd_emu10k1_free_pages(emu10k1_t *emu, snd_util_memblk_t *blk)
353 snd_assert(emu && blk, return -EINVAL);
354 return snd_emu10k1_synth_free(emu, blk);
359 * memory allocation using multiple pages (for synth)
360 * Unlike the DMA allocation above, non-contiguous pages are assined.
364 * allocate a synth sample area
367 snd_emu10k1_synth_alloc(emu10k1_t *hw, unsigned int size)
369 emu10k1_memblk_t *blk;
370 snd_util_memhdr_t *hdr = hw->memhdr;
372 down(&hdr->block_mutex);
373 blk = (emu10k1_memblk_t *)__snd_util_mem_alloc(hdr, size);
375 up(&hdr->block_mutex);
378 if (synth_alloc_pages(hw, blk)) {
379 __snd_util_mem_free(hdr, (snd_util_memblk_t *)blk);
380 up(&hdr->block_mutex);
383 snd_emu10k1_memblk_map(hw, blk);
384 up(&hdr->block_mutex);
385 return (snd_util_memblk_t *)blk;
390 * free a synth sample area
393 snd_emu10k1_synth_free(emu10k1_t *emu, snd_util_memblk_t *memblk)
395 snd_util_memhdr_t *hdr = emu->memhdr;
396 emu10k1_memblk_t *blk = (emu10k1_memblk_t *)memblk;
399 down(&hdr->block_mutex);
400 spin_lock_irqsave(&emu->memblk_lock, flags);
401 if (blk->mapped_page >= 0)
402 unmap_memblk(emu, blk);
403 spin_unlock_irqrestore(&emu->memblk_lock, flags);
404 synth_free_pages(emu, blk);
405 __snd_util_mem_free(hdr, memblk);
406 up(&hdr->block_mutex);
411 /* check new allocation range */
412 static void get_single_page_range(snd_util_memhdr_t *hdr, emu10k1_memblk_t *blk, int *first_page_ret, int *last_page_ret)
416 int first_page, last_page;
417 first_page = blk->first_page;
418 if ((p = blk->mem.list.prev) != &hdr->block) {
419 q = get_emu10k1_memblk(p, mem.list);
420 if (q->last_page == first_page)
421 first_page++; /* first page was already allocated */
423 last_page = blk->last_page;
424 if ((p = blk->mem.list.next) != &hdr->block) {
425 q = get_emu10k1_memblk(p, mem.list);
426 if (q->first_page == last_page)
427 last_page--; /* last page was already allocated */
429 *first_page_ret = first_page;
430 *last_page_ret = last_page;
434 * allocate kernel pages
436 static int synth_alloc_pages(emu10k1_t *emu, emu10k1_memblk_t *blk)
438 int page, first_page, last_page;
439 struct snd_dma_buffer dmab;
441 emu10k1_memblk_init(blk);
442 get_single_page_range(emu->memhdr, blk, &first_page, &last_page);
443 /* allocate kernel pages */
444 for (page = first_page; page <= last_page; page++) {
445 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(emu->pci),
446 PAGE_SIZE, &dmab) < 0)
448 if (! is_valid_page(emu, dmab.addr)) {
449 snd_dma_free_pages(&dmab);
452 emu->page_addr_table[page] = dmab.addr;
453 emu->page_ptr_table[page] = dmab.area;
458 /* release allocated pages */
459 last_page = page - 1;
460 for (page = first_page; page <= last_page; page++) {
461 dmab.area = emu->page_ptr_table[page];
462 dmab.addr = emu->page_addr_table[page];
463 dmab.bytes = PAGE_SIZE;
464 snd_dma_free_pages(&dmab);
465 emu->page_addr_table[page] = 0;
466 emu->page_ptr_table[page] = NULL;
475 static int synth_free_pages(emu10k1_t *emu, emu10k1_memblk_t *blk)
477 int page, first_page, last_page;
478 struct snd_dma_buffer dmab;
480 get_single_page_range(emu->memhdr, blk, &first_page, &last_page);
481 dmab.dev.type = SNDRV_DMA_TYPE_DEV;
482 dmab.dev.dev = snd_dma_pci_data(emu->pci);
483 for (page = first_page; page <= last_page; page++) {
484 if (emu->page_ptr_table[page] == NULL)
486 dmab.area = emu->page_ptr_table[page];
487 dmab.addr = emu->page_addr_table[page];
488 dmab.bytes = PAGE_SIZE;
489 snd_dma_free_pages(&dmab);
490 emu->page_addr_table[page] = 0;
491 emu->page_ptr_table[page] = NULL;
497 /* calculate buffer pointer from offset address */
498 static inline void *offset_ptr(emu10k1_t *emu, int page, int offset)
501 snd_assert(page >= 0 && page < emu->max_cache_pages, return NULL);
502 ptr = emu->page_ptr_table[page];
504 printk(KERN_ERR "emu10k1: access to NULL ptr: page = %d\n", page);
507 ptr += offset & (PAGE_SIZE - 1);
512 * bzero(blk + offset, size)
514 int snd_emu10k1_synth_bzero(emu10k1_t *emu, snd_util_memblk_t *blk, int offset, int size)
516 int page, nextofs, end_offset, temp, temp1;
518 emu10k1_memblk_t *p = (emu10k1_memblk_t *)blk;
520 offset += blk->offset & (PAGE_SIZE - 1);
521 end_offset = offset + size;
522 page = get_aligned_page(offset);
524 nextofs = aligned_page_offset(page + 1);
525 temp = nextofs - offset;
526 temp1 = end_offset - offset;
529 ptr = offset_ptr(emu, page + p->first_page, offset);
531 memset(ptr, 0, temp);
534 } while (offset < end_offset);
539 * copy_from_user(blk + offset, data, size)
541 int snd_emu10k1_synth_copy_from_user(emu10k1_t *emu, snd_util_memblk_t *blk, int offset, const char __user *data, int size)
543 int page, nextofs, end_offset, temp, temp1;
545 emu10k1_memblk_t *p = (emu10k1_memblk_t *)blk;
547 offset += blk->offset & (PAGE_SIZE - 1);
548 end_offset = offset + size;
549 page = get_aligned_page(offset);
551 nextofs = aligned_page_offset(page + 1);
552 temp = nextofs - offset;
553 temp1 = end_offset - offset;
556 ptr = offset_ptr(emu, page + p->first_page, offset);
557 if (ptr && copy_from_user(ptr, data, temp))
562 } while (offset < end_offset);