⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 memory.c

📁 linux2.6.16版本
💻 C
📖 第 1 页 / 共 2 页
字号:
/* *  Copyright (c) by Jaroslav Kysela <perex@suse.cz> *  Copyright (c) by Takashi Iwai <tiwai@suse.de> * *  EMU10K1 memory page allocation (PTB area) * * *   This program is free software; you can redistribute it and/or modify *   it under the terms of the GNU General Public License as published by *   the Free Software Foundation; either version 2 of the License, or *   (at your option) any later version. * *   This program is distributed in the hope that it will be useful, *   but WITHOUT ANY WARRANTY; without even the implied warranty of *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the *   GNU General Public License for more details. * *   You should have received a copy of the GNU General Public License *   along with this program; if not, write to the Free Software *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA * */#include <sound/driver.h>#include <linux/pci.h>#include <linux/time.h>#include <sound/core.h>#include <sound/emu10k1.h>/* page arguments of these two macros are Emu page (4096 bytes), not like * aligned pages in others */#define __set_ptb_entry(emu,page,addr) \	(((u32 *)(emu)->ptb_pages.area)[page] = cpu_to_le32(((addr) << 1) | (page)))#define UNIT_PAGES		(PAGE_SIZE / EMUPAGESIZE)#define MAX_ALIGN_PAGES		(MAXPAGES / UNIT_PAGES)/* get aligned page from offset address */#define get_aligned_page(offset)	((offset) >> PAGE_SHIFT)/* get offset address from aligned page */#define aligned_page_offset(page)	((page) << PAGE_SHIFT)#if PAGE_SIZE == 4096/* page size == EMUPAGESIZE *//* fill PTB entrie(s) corresponding to page with addr */#define set_ptb_entry(emu,page,addr)	__set_ptb_entry(emu,page,addr)/* fill PTB entrie(s) corresponding to page with silence pointer */#define set_silent_ptb(emu,page)	__set_ptb_entry(emu,page,emu->silent_page.addr)#else/* fill PTB entries -- we need to fill UNIT_PAGES entries */static inline void set_ptb_entry(struct snd_emu10k1 *emu, int page, dma_addr_t addr){	int i;	page *= UNIT_PAGES;	for (i = 0; i < UNIT_PAGES; i++, page++) {		__set_ptb_entry(emu, page, addr);		addr += EMUPAGESIZE;	}}static inline void set_silent_ptb(struct snd_emu10k1 *emu, int page){	int i;	page *= UNIT_PAGES;	for (i = 0; i < UNIT_PAGES; i++, page++)		/* do not increment ptr */		__set_ptb_entry(emu, page, emu->silent_page.addr);}#endif /* PAGE_SIZE *//* */static int synth_alloc_pages(struct snd_emu10k1 *hw, struct snd_emu10k1_memblk *blk);static int synth_free_pages(struct snd_emu10k1 *hw, struct snd_emu10k1_memblk *blk);#define get_emu10k1_memblk(l,member)	list_entry(l, struct snd_emu10k1_memblk, member)/* initialize emu10k1 part */static void emu10k1_memblk_init(struct snd_emu10k1_memblk *blk){	blk->mapped_page = -1;	INIT_LIST_HEAD(&blk->mapped_link);	INIT_LIST_HEAD(&blk->mapped_order_link);	blk->map_locked = 0;	blk->first_page = get_aligned_page(blk->mem.offset);	blk->last_page = get_aligned_page(blk->mem.offset + blk->mem.size - 1);	blk->pages = blk->last_page - blk->first_page + 1;}/* * search empty region on PTB with the given size * * if an empty region is found, return the page and store the next mapped block * in nextp * if not found, return a negative error code. */static int search_empty_map_area(struct snd_emu10k1 *emu, int npages, struct list_head **nextp){	int page = 0, found_page = -ENOMEM;	int max_size = npages;	int size;	struct list_head *candidate = &emu->mapped_link_head;	struct list_head *pos;	list_for_each (pos, &emu->mapped_link_head) {		struct snd_emu10k1_memblk *blk = get_emu10k1_memblk(pos, mapped_link);		snd_assert(blk->mapped_page >= 0, continue);		size = blk->mapped_page - page;		if (size == npages) {			*nextp = pos;			return page;		}		else if (size > max_size) {			/* we look for the maximum empty hole */			max_size = size;			candidate = pos;			found_page = page;		}		page = blk->mapped_page + blk->pages;	}	size = MAX_ALIGN_PAGES - page;	if (size >= max_size) {		*nextp = pos;		return page;	}	*nextp = candidate;	return found_page;}/* * map a memory block onto emu10k1's PTB * * call with memblk_lock held */static int map_memblk(struct snd_emu10k1 *emu, struct snd_emu10k1_memblk *blk){	int page, pg;	struct list_head *next;	page = search_empty_map_area(emu, blk->pages, &next);	if (page < 0) /* not found */		return page;	/* insert this block in the proper position of mapped list */	list_add_tail(&blk->mapped_link, next);	/* append this as a newest block in order list */	list_add_tail(&blk->mapped_order_link, &emu->mapped_order_link_head);	blk->mapped_page = page;	/* fill PTB */	for (pg = blk->first_page; pg <= blk->last_page; pg++) {		set_ptb_entry(emu, page, emu->page_addr_table[pg]);		page++;	}	return 0;}/* * unmap the block * return the size of resultant empty pages * * call with memblk_lock held */static int unmap_memblk(struct snd_emu10k1 *emu, struct snd_emu10k1_memblk *blk){	int start_page, end_page, mpage, pg;	struct list_head *p;	struct snd_emu10k1_memblk *q;	/* calculate the expected size of empty region */	if ((p = blk->mapped_link.prev) != &emu->mapped_link_head) {		q = get_emu10k1_memblk(p, mapped_link);		start_page = q->mapped_page + q->pages;	} else		start_page = 0;	if ((p = blk->mapped_link.next) != &emu->mapped_link_head) {		q = get_emu10k1_memblk(p, mapped_link);		end_page = q->mapped_page;	} else		end_page = MAX_ALIGN_PAGES;	/* remove links */	list_del(&blk->mapped_link);	list_del(&blk->mapped_order_link);	/* clear PTB */	mpage = blk->mapped_page;	for (pg = blk->first_page; pg <= blk->last_page; pg++) {		set_silent_ptb(emu, mpage);		mpage++;	}	blk->mapped_page = -1;	return end_page - start_page; /* return the new empty size */}/* * search empty pages with the given size, and create a memory block * * unlike synth_alloc the memory block is aligned to the page start */static struct snd_emu10k1_memblk *search_empty(struct snd_emu10k1 *emu, int size){	struct list_head *p;	struct snd_emu10k1_memblk *blk;	int page, psize;	psize = get_aligned_page(size + PAGE_SIZE -1);	page = 0;	list_for_each(p, &emu->memhdr->block) {		blk = get_emu10k1_memblk(p, mem.list);		if (page + psize <= blk->first_page)			goto __found_pages;		page = blk->last_page + 1;	}	if (page + psize > emu->max_cache_pages)		return NULL;__found_pages:	/* create a new memory block */	blk = (struct snd_emu10k1_memblk *)__snd_util_memblk_new(emu->memhdr, psize << PAGE_SHIFT, p->prev);	if (blk == NULL)		return NULL;	blk->mem.offset = aligned_page_offset(page); /* set aligned offset */	emu10k1_memblk_init(blk);	return blk;}/* * check if the given pointer is valid for pages */static int is_valid_page(struct snd_emu10k1 *emu, dma_addr_t addr){	if (addr & ~emu->dma_mask) {		snd_printk(KERN_ERR "max memory size is 0x%lx (addr = 0x%lx)!!\n", emu->dma_mask, (unsigned long)addr);		return 0;	}	if (addr & (EMUPAGESIZE-1)) {		snd_printk(KERN_ERR "page is not aligned\n");		return 0;	}	return 1;}/* * map the given memory block on PTB. * if the block is already mapped, update the link order. * if no empty pages are found, tries to release unsed memory blocks * and retry the mapping. */int snd_emu10k1_memblk_map(struct snd_emu10k1 *emu, struct snd_emu10k1_memblk *blk){	int err;	int size;	struct list_head *p, *nextp;	struct snd_emu10k1_memblk *deleted;	unsigned long flags;	spin_lock_irqsave(&emu->memblk_lock, flags);	if (blk->mapped_page >= 0) {		/* update order link */		list_del(&blk->mapped_order_link);		list_add_tail(&blk->mapped_order_link, &emu->mapped_order_link_head);		spin_unlock_irqrestore(&emu->memblk_lock, flags);		return 0;	}	if ((err = map_memblk(emu, blk)) < 0) {		/* no enough page - try to unmap some blocks */		/* starting from the oldest block */		p = emu->mapped_order_link_head.next;		for (; p != &emu->mapped_order_link_head; p = nextp) {			nextp = p->next;			deleted = get_emu10k1_memblk(p, mapped_order_link);			if (deleted->map_locked)				continue;			size = unmap_memblk(emu, deleted);			if (size >= blk->pages) {				/* ok the empty region is enough large */				err = map_memblk(emu, blk);				break;			}		}	}	spin_unlock_irqrestore(&emu->memblk_lock, flags);	return err;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -