Lines Matching +full:page +full:- +full:offset
1 // SPDX-License-Identifier: GPL-2.0-or-later
6 * EMU10K1 memory page allocation (PTB area)
18 /* page arguments of these two macros are Emu page (4096 bytes), not like
21 #define __set_ptb_entry(emu,page,addr) \ argument
22 (((__le32 *)(emu)->ptb_pages.area)[page] = \
23 cpu_to_le32(((addr) << (emu->address_mode)) | (page)))
24 #define __get_ptb_entry(emu, page) \ argument
25 (le32_to_cpu(((__le32 *)(emu)->ptb_pages.area)[page]))
30 /* get aligned page from offset address */
31 #define get_aligned_page(offset) ((offset) >> PAGE_SHIFT) argument
32 /* get offset address from aligned page */
33 #define aligned_page_offset(page) ((page) << PAGE_SHIFT) argument
36 /* fill PTB entrie(s) corresponding to page with addr */
37 #define set_ptb_entry(emu,page,addr) __set_ptb_entry(emu,page,addr) argument
38 /* fill PTB entrie(s) corresponding to page with silence pointer */
39 #define set_silent_ptb(emu,page) __set_ptb_entry(emu,page,emu->silent_page.addr) argument
41 /* fill PTB entries -- we need to fill UNIT_PAGES entries */
42 static inline void set_ptb_entry(struct snd_emu10k1 *emu, int page, dma_addr_t addr) in set_ptb_entry() argument
45 page *= UNIT_PAGES; in set_ptb_entry()
46 for (i = 0; i < UNIT_PAGES; i++, page++) { in set_ptb_entry()
47 __set_ptb_entry(emu, page, addr); in set_ptb_entry()
48 dev_dbg(emu->card->dev, "mapped page %d to entry %.8x\n", page, in set_ptb_entry()
49 (unsigned int)__get_ptb_entry(emu, page)); in set_ptb_entry()
53 static inline void set_silent_ptb(struct snd_emu10k1 *emu, int page) in set_silent_ptb() argument
56 page *= UNIT_PAGES; in set_silent_ptb()
57 for (i = 0; i < UNIT_PAGES; i++, page++) { in set_silent_ptb()
59 __set_ptb_entry(emu, page, emu->silent_page.addr); in set_silent_ptb()
60 dev_dbg(emu->card->dev, "mapped silent page %d to entry %.8x\n", in set_silent_ptb()
61 page, (unsigned int)__get_ptb_entry(emu, page)); in set_silent_ptb()
78 blk->mapped_page = -1; in emu10k1_memblk_init()
79 INIT_LIST_HEAD(&blk->mapped_link); in emu10k1_memblk_init()
80 INIT_LIST_HEAD(&blk->mapped_order_link); in emu10k1_memblk_init()
81 blk->map_locked = 0; in emu10k1_memblk_init()
83 blk->first_page = get_aligned_page(blk->mem.offset); in emu10k1_memblk_init()
84 blk->last_page = get_aligned_page(blk->mem.offset + blk->mem.size - 1); in emu10k1_memblk_init()
85 blk->pages = blk->last_page - blk->first_page + 1; in emu10k1_memblk_init()
91 * if an empty region is found, return the page and store the next mapped block
97 int page = 1, found_page = -ENOMEM; in search_empty_map_area() local
100 struct list_head *candidate = &emu->mapped_link_head; in search_empty_map_area()
103 list_for_each (pos, &emu->mapped_link_head) { in search_empty_map_area()
105 if (blk->mapped_page < 0) in search_empty_map_area()
107 size = blk->mapped_page - page; in search_empty_map_area()
110 return page; in search_empty_map_area()
116 found_page = page; in search_empty_map_area()
118 page = blk->mapped_page + blk->pages; in search_empty_map_area()
120 size = (emu->address_mode ? MAX_ALIGN_PAGES1 : MAX_ALIGN_PAGES0) - page; in search_empty_map_area()
123 return page; in search_empty_map_area()
136 int page, pg; in map_memblk() local
139 page = search_empty_map_area(emu, blk->pages, &next); in map_memblk()
140 if (page < 0) /* not found */ in map_memblk()
141 return page; in map_memblk()
142 if (page == 0) { in map_memblk()
143 dev_err(emu->card->dev, "trying to map zero (reserved) page\n"); in map_memblk()
144 return -EINVAL; in map_memblk()
147 list_add_tail(&blk->mapped_link, next); in map_memblk()
149 list_add_tail(&blk->mapped_order_link, &emu->mapped_order_link_head); in map_memblk()
150 blk->mapped_page = page; in map_memblk()
152 for (pg = blk->first_page; pg <= blk->last_page; pg++) { in map_memblk()
153 set_ptb_entry(emu, page, emu->page_addr_table[pg]); in map_memblk()
154 page++; in map_memblk()
172 p = blk->mapped_link.prev; in unmap_memblk()
173 if (p != &emu->mapped_link_head) { in unmap_memblk()
175 start_page = q->mapped_page + q->pages; in unmap_memblk()
179 p = blk->mapped_link.next; in unmap_memblk()
180 if (p != &emu->mapped_link_head) { in unmap_memblk()
182 end_page = q->mapped_page; in unmap_memblk()
184 end_page = (emu->address_mode ? MAX_ALIGN_PAGES1 : MAX_ALIGN_PAGES0); in unmap_memblk()
188 list_del(&blk->mapped_link); in unmap_memblk()
189 list_del(&blk->mapped_order_link); in unmap_memblk()
191 mpage = blk->mapped_page; in unmap_memblk()
192 for (pg = blk->first_page; pg <= blk->last_page; pg++) { in unmap_memblk()
196 blk->mapped_page = -1; in unmap_memblk()
197 return end_page - start_page; /* return the new empty size */ in unmap_memblk()
203 * unlike synth_alloc the memory block is aligned to the page start
210 int page, psize; in search_empty() local
212 psize = get_aligned_page(size + PAGE_SIZE -1); in search_empty()
213 page = 0; in search_empty()
214 list_for_each(p, &emu->memhdr->block) { in search_empty()
216 if (page + psize <= blk->first_page) in search_empty()
218 page = blk->last_page + 1; in search_empty()
220 if (page + psize > emu->max_cache_pages) in search_empty()
225 …blk = (struct snd_emu10k1_memblk *)__snd_util_memblk_new(emu->memhdr, psize << PAGE_SHIFT, p->prev… in search_empty()
228 blk->mem.offset = aligned_page_offset(page); /* set aligned offset */ in search_empty()
239 if (addr & ~emu->dma_mask) { in is_valid_page()
240 dev_err_ratelimited(emu->card->dev, in is_valid_page()
242 emu->dma_mask, (unsigned long)addr); in is_valid_page()
245 if (addr & (EMUPAGESIZE-1)) { in is_valid_page()
246 dev_err_ratelimited(emu->card->dev, "page is not aligned\n"); in is_valid_page()
266 spin_lock_irqsave(&emu->memblk_lock, flags); in snd_emu10k1_memblk_map()
267 if (blk->mapped_page >= 0) { in snd_emu10k1_memblk_map()
269 list_move_tail(&blk->mapped_order_link, in snd_emu10k1_memblk_map()
270 &emu->mapped_order_link_head); in snd_emu10k1_memblk_map()
271 spin_unlock_irqrestore(&emu->memblk_lock, flags); in snd_emu10k1_memblk_map()
276 /* no enough page - try to unmap some blocks */ in snd_emu10k1_memblk_map()
278 p = emu->mapped_order_link_head.next; in snd_emu10k1_memblk_map()
279 for (; p != &emu->mapped_order_link_head; p = nextp) { in snd_emu10k1_memblk_map()
280 nextp = p->next; in snd_emu10k1_memblk_map()
282 if (deleted->map_locked) in snd_emu10k1_memblk_map()
285 if (size >= blk->pages) { in snd_emu10k1_memblk_map()
292 spin_unlock_irqrestore(&emu->memblk_lock, flags); in snd_emu10k1_memblk_map()
299 * page allocation for DMA
304 struct snd_pcm_runtime *runtime = substream->runtime; in snd_emu10k1_alloc_pages()
307 int page, err, idx; in snd_emu10k1_alloc_pages() local
311 if (snd_BUG_ON(runtime->dma_bytes <= 0 || in snd_emu10k1_alloc_pages()
312 runtime->dma_bytes >= (emu->address_mode ? MAXPAGES1 : MAXPAGES0) * EMUPAGESIZE)) in snd_emu10k1_alloc_pages()
314 hdr = emu->memhdr; in snd_emu10k1_alloc_pages()
318 mutex_lock(&hdr->block_mutex); in snd_emu10k1_alloc_pages()
319 blk = search_empty(emu, runtime->dma_bytes); in snd_emu10k1_alloc_pages()
321 mutex_unlock(&hdr->block_mutex); in snd_emu10k1_alloc_pages()
328 for (page = blk->first_page; page <= blk->last_page; page++, idx++) { in snd_emu10k1_alloc_pages()
331 if (ofs >= runtime->dma_bytes) in snd_emu10k1_alloc_pages()
332 addr = emu->silent_page.addr; in snd_emu10k1_alloc_pages()
336 dev_err_ratelimited(emu->card->dev, in snd_emu10k1_alloc_pages()
337 "emu: failure page = %d\n", idx); in snd_emu10k1_alloc_pages()
338 mutex_unlock(&hdr->block_mutex); in snd_emu10k1_alloc_pages()
341 emu->page_addr_table[page] = addr; in snd_emu10k1_alloc_pages()
342 emu->page_ptr_table[page] = NULL; in snd_emu10k1_alloc_pages()
346 blk->map_locked = 1; /* do not unmap this block! */ in snd_emu10k1_alloc_pages()
350 mutex_unlock(&hdr->block_mutex); in snd_emu10k1_alloc_pages()
353 mutex_unlock(&hdr->block_mutex); in snd_emu10k1_alloc_pages()
359 * release DMA buffer from page table
364 return -EINVAL; in snd_emu10k1_free_pages()
380 if (emu->iommu_workaround) { in snd_emu10k1_alloc_pages_maybe_wider()
393 &emu->pci->dev, size, dmab); in snd_emu10k1_alloc_pages_maybe_wider()
398 * Unlike the DMA allocation above, non-contiguous pages are assined.
408 struct snd_util_memhdr *hdr = hw->memhdr; in snd_emu10k1_synth_alloc()
410 mutex_lock(&hdr->block_mutex); in snd_emu10k1_synth_alloc()
413 mutex_unlock(&hdr->block_mutex); in snd_emu10k1_synth_alloc()
418 mutex_unlock(&hdr->block_mutex); in snd_emu10k1_synth_alloc()
422 mutex_unlock(&hdr->block_mutex); in snd_emu10k1_synth_alloc()
434 struct snd_util_memhdr *hdr = emu->memhdr; in snd_emu10k1_synth_free()
438 mutex_lock(&hdr->block_mutex); in snd_emu10k1_synth_free()
439 spin_lock_irqsave(&emu->memblk_lock, flags); in snd_emu10k1_synth_free()
440 if (blk->mapped_page >= 0) in snd_emu10k1_synth_free()
442 spin_unlock_irqrestore(&emu->memblk_lock, flags); in snd_emu10k1_synth_free()
445 mutex_unlock(&hdr->block_mutex); in snd_emu10k1_synth_free()
459 first_page = blk->first_page; in get_single_page_range()
460 p = blk->mem.list.prev; in get_single_page_range()
461 if (p != &hdr->block) { in get_single_page_range()
463 if (q->last_page == first_page) in get_single_page_range()
464 first_page++; /* first page was already allocated */ in get_single_page_range()
466 last_page = blk->last_page; in get_single_page_range()
467 p = blk->mem.list.next; in get_single_page_range()
468 if (p != &hdr->block) { in get_single_page_range()
470 if (q->first_page == last_page) in get_single_page_range()
471 last_page--; /* last page was already allocated */ in get_single_page_range()
482 int page; in __synth_free_pages() local
485 dmab.dev.dev = &emu->pci->dev; in __synth_free_pages()
487 for (page = first_page; page <= last_page; page++) { in __synth_free_pages()
488 if (emu->page_ptr_table[page] == NULL) in __synth_free_pages()
490 dmab.area = emu->page_ptr_table[page]; in __synth_free_pages()
491 dmab.addr = emu->page_addr_table[page]; in __synth_free_pages()
498 if (emu->iommu_workaround) in __synth_free_pages()
502 emu->page_addr_table[page] = 0; in __synth_free_pages()
503 emu->page_ptr_table[page] = NULL; in __synth_free_pages()
512 int page, first_page, last_page; in synth_alloc_pages() local
516 get_single_page_range(emu->memhdr, blk, &first_page, &last_page); in synth_alloc_pages()
518 for (page = first_page; page <= last_page; page++) { in synth_alloc_pages()
526 emu->page_addr_table[page] = dmab.addr; in synth_alloc_pages()
527 emu->page_ptr_table[page] = dmab.area; in synth_alloc_pages()
533 last_page = page - 1; in synth_alloc_pages()
536 return -ENOMEM; in synth_alloc_pages()
546 get_single_page_range(emu->memhdr, blk, &first_page, &last_page); in synth_free_pages()
551 /* calculate buffer pointer from offset address */
552 static inline void *offset_ptr(struct snd_emu10k1 *emu, int page, int offset) in offset_ptr() argument
555 if (snd_BUG_ON(page < 0 || page >= emu->max_cache_pages)) in offset_ptr()
557 ptr = emu->page_ptr_table[page]; in offset_ptr()
559 dev_err(emu->card->dev, in offset_ptr()
560 "access to NULL ptr: page = %d\n", page); in offset_ptr()
563 ptr += offset & (PAGE_SIZE - 1); in offset_ptr()
568 * memset(blk + offset, value, size)
571 int offset, int size, u8 value) in snd_emu10k1_synth_memset() argument
573 int page, nextofs, end_offset, temp, temp1; in snd_emu10k1_synth_memset() local
577 if (snd_BUG_ON(offset + size > p->mem.size)) in snd_emu10k1_synth_memset()
578 return -EFAULT; in snd_emu10k1_synth_memset()
580 offset += blk->offset & (PAGE_SIZE - 1); in snd_emu10k1_synth_memset()
581 end_offset = offset + size; in snd_emu10k1_synth_memset()
582 page = get_aligned_page(offset); in snd_emu10k1_synth_memset()
584 nextofs = aligned_page_offset(page + 1); in snd_emu10k1_synth_memset()
585 temp = nextofs - offset; in snd_emu10k1_synth_memset()
586 temp1 = end_offset - offset; in snd_emu10k1_synth_memset()
589 ptr = offset_ptr(emu, page + p->first_page, offset); in snd_emu10k1_synth_memset()
592 offset = nextofs; in snd_emu10k1_synth_memset()
593 page++; in snd_emu10k1_synth_memset()
594 } while (offset < end_offset); in snd_emu10k1_synth_memset()
606 size--; in xor_range()
611 size -= 2; in xor_range()
616 size -= 4; in xor_range()
621 size -= 2; in xor_range()
628 * copy_from_user(blk + offset, data, size) ^ xor
631 int offset, const char __user *data, int size, u32 xor) in snd_emu10k1_synth_copy_from_user() argument
633 int page, nextofs, end_offset, temp, temp1; in snd_emu10k1_synth_copy_from_user() local
637 if (snd_BUG_ON(offset + size > p->mem.size)) in snd_emu10k1_synth_copy_from_user()
638 return -EFAULT; in snd_emu10k1_synth_copy_from_user()
640 offset += blk->offset & (PAGE_SIZE - 1); in snd_emu10k1_synth_copy_from_user()
641 end_offset = offset + size; in snd_emu10k1_synth_copy_from_user()
642 page = get_aligned_page(offset); in snd_emu10k1_synth_copy_from_user()
644 nextofs = aligned_page_offset(page + 1); in snd_emu10k1_synth_copy_from_user()
645 temp = nextofs - offset; in snd_emu10k1_synth_copy_from_user()
646 temp1 = end_offset - offset; in snd_emu10k1_synth_copy_from_user()
649 ptr = offset_ptr(emu, page + p->first_page, offset); in snd_emu10k1_synth_copy_from_user()
652 return -EFAULT; in snd_emu10k1_synth_copy_from_user()
656 offset = nextofs; in snd_emu10k1_synth_copy_from_user()
658 page++; in snd_emu10k1_synth_copy_from_user()
659 } while (offset < end_offset); in snd_emu10k1_synth_copy_from_user()