--- a/alsa-kernel/core/memalloc.c +++ b/alsa-kernel/core/memalloc.c @@ -1,3 +1,4 @@ +#include "memalloc.inc" /* * Copyright (c) by Jaroslav Kysela * Takashi Iwai @@ -27,6 +28,7 @@ #include #include #include +#include #include #include #include @@ -82,7 +84,50 @@ struct snd_mem_list { * Hacks */ -#if defined(__i386__) +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0) +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 5) +static void *snd_dma_alloc_coherent1(struct device *dev, size_t size, + dma_addr_t *dma_handle, int flags) +{ + if (dev) + return dma_alloc_coherent(dev, size, dma_handle, flags); + else /* FIXME: dma_alloc_coherent does't always accept dev=NULL */ + return pci_alloc_consistent(NULL, size, dma_handle); +} + +static void snd_dma_free_coherent1(struct device *dev, size_t size, void *dma_addr, + dma_addr_t dma_handle) +{ + if (dev) + return dma_free_coherent(dev, size, dma_addr, dma_handle); + else + return pci_free_consistent(NULL, size, dma_addr, dma_handle); +} + +#undef dma_alloc_coherent +#define dma_alloc_coherent snd_dma_alloc_coherent1 +#undef dma_free_coherent +#define dma_free_coherent snd_dma_free_coherent1 +#endif /* < 2.6.5 */ + +#else + +/* for 2.2/2.4 kernels */ +#if defined(CONFIG_PCI) || defined(CONFIG_ISA) || defined(__i386__) +#define dma_alloc_coherent(dev,size,addr,flags) pci_alloc_consistent((struct pci_dev *)(dev),size,addr) +#define dma_free_coherent(dev,size,ptr,addr) pci_free_consistent((struct pci_dev *)(dev),size,ptr,addr) +#elif CONFIG_SBUS +#define dma_alloc_coherent(dev,size,addr,flags) sbus_alloc_consistent((struct sbus_dev *)(dev),size,addr) +#define dma_free_coherent(dev,size,ptr,addr) sbus_free_consistent((struct sbus_dev *)(dev),size,ptr,addr) +#else +#error "Need a bus for dma_alloc_coherent()" +#endif + +#endif /* >= 2.6.0 */ + +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 0) +#if defined(__i386__) || defined(__ppc__) || defined(__x86_64__) + /* * A hack to allocate large buffers via dma_alloc_coherent() * @@ -99,6 +144,30 @@ struct snd_mem_list { * so dma_mask doesn't have to be messed with. */ +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0) +#define VALID_DMA_MASK(dev) (dev)->dma_mask +#define GET_DMA_MASK(dev) *(dev)->dma_mask +#define SET_DMA_MASK(dev,val) (*(dev)->dma_mask = (val)) +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 5) +#define GET_COHERENT_DMA_MASK(dev) (dev)->coherent_dma_mask +#define SET_COHERENT_DMA_MASK(dev,val) ((dev)->coherent_dma_mask = (val)) +#else +#define GET_COHERENT_DMA_MASK(dev) 0 /* dummy */ +#define SET_COHERENT_DMA_MASK(dev,val) +#endif +#else /* 2.4.x */ +#define VALID_DMA_MASK(dev) 1 +#define GET_DMA_MASK(dev) ((struct pci_dev *)(dev))->dma_mask +#define SET_DMA_MASK(dev,val) (((struct pci_dev *)(dev))->dma_mask = (val)) +#ifdef CONFIG_HAVE_PCI_CONSISTENT_DMA_MASK +#define GET_COHERENT_DMA_MASK(dev) ((struct pci_dev *)(dev))->consistent_dma_mask +#define SET_COHERENT_DMA_MASK(dev,val) (((struct pci_dev *)(dev))->consistent_dma_mask = (val)) +#else +#define GET_COHERENT_DMA_MASK(dev) 0 /* dummy */ +#define SET_COHERENT_DMA_MASK(dev,val) +#endif +#endif + static void *snd_dma_hack_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle, gfp_t flags) @@ -106,15 +175,15 @@ static void *snd_dma_hack_alloc_coherent void *ret; u64 dma_mask, coherent_dma_mask; - if (dev == NULL || !dev->dma_mask) + if (dev == NULL || !VALID_DMA_MASK(dev)) return dma_alloc_coherent(dev, size, dma_handle, flags); - dma_mask = *dev->dma_mask; - coherent_dma_mask = dev->coherent_dma_mask; - *dev->dma_mask = 0xffffffff; /* do without masking */ - dev->coherent_dma_mask = 0xffffffff; /* do without masking */ + dma_mask = GET_DMA_MASK(dev); + coherent_dma_mask = GET_COHERENT_DMA_MASK(dev); + SET_DMA_MASK(dev, 0xffffffff); /* do without masking */ + SET_COHERENT_DMA_MASK(dev, 0xffffffff); /* do without masking */ ret = dma_alloc_coherent(dev, size, dma_handle, flags); - *dev->dma_mask = dma_mask; /* restore */ - dev->coherent_dma_mask = coherent_dma_mask; /* restore */ + SET_DMA_MASK(dev, dma_mask); /* restore */ + SET_COHERENT_DMA_MASK(dev, coherent_dma_mask) /* restore */; if (ret) { /* obtained address is out of range? */ if (((unsigned long)*dma_handle + size - 1) & ~dma_mask) { @@ -139,6 +208,8 @@ static void *snd_dma_hack_alloc_coherent #define dma_alloc_coherent snd_dma_hack_alloc_coherent #endif /* arch */ +#endif /* >= 2.4.0 */ + /* * @@ -158,6 +229,24 @@ static inline void dec_snd_pages(int ord snd_allocated_pages -= 1 << order; } +static void mark_pages(struct page *page, int order) +{ +#if ! defined(__arm__) && LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 16) + struct page *last_page = page + (1 << order); + while (page < last_page) + SetPageReserved(page++); +#endif +} + +static void unmark_pages(struct page *page, int order) +{ +#if ! defined(__arm__) && LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 16) + struct page *last_page = page + (1 << order); + while (page < last_page) + ClearPageReserved(page++); +#endif +} + /** * snd_malloc_pages - allocate pages with the given size * @size: the size to allocate in bytes @@ -176,8 +265,10 @@ void *snd_malloc_pages(size_t size, gfp_ snd_assert(gfp_flags != 0, return NULL); gfp_flags |= __GFP_COMP; /* compound page lets parts be mapped */ pg = get_order(size); - if ((res = (void *) __get_free_pages(gfp_flags, pg)) != NULL) + if ((res = (void *) __get_free_pages(gfp_flags, pg)) != NULL) { + mark_pages(virt_to_page(res), pg); inc_snd_pages(pg); + } return res; } @@ -196,6 +287,7 @@ void snd_free_pages(void *ptr, size_t si return; pg = get_order(size); dec_snd_pages(pg); + unmark_pages(virt_to_page(ptr), pg); free_pages((unsigned long) ptr, pg); } @@ -220,8 +312,10 @@ static void *snd_malloc_dev_pages(struct | __GFP_NORETRY /* don't trigger OOM-killer */ | __GFP_NOWARN; /* no stack trace print - this call is non-critical */ res = dma_alloc_coherent(dev, PAGE_SIZE << pg, dma, gfp_flags); - if (res != NULL) + if (res != NULL) { + mark_pages(virt_to_page(res), pg); inc_snd_pages(pg); + } return res; } @@ -236,6 +330,7 @@ static void snd_free_dev_pages(struct de return; pg = get_order(size); dec_snd_pages(pg); + unmark_pages(virt_to_page(ptr), pg); dma_free_coherent(dev, PAGE_SIZE << pg, ptr, dma); } @@ -481,53 +576,55 @@ static void free_all_reserved_pages(void #define SND_MEM_PROC_FILE "driver/snd-page-alloc" static struct proc_dir_entry *snd_mem_proc; -static int snd_mem_proc_read(char *page, char **start, off_t off, - int count, int *eof, void *data) +static int snd_mem_proc_read(struct seq_file *seq, void *offset) { - int len = 0; long pages = snd_allocated_pages >> (PAGE_SHIFT-12); struct snd_mem_list *mem; int devno; static char *types[] = { "UNKNOWN", "CONT", "DEV", "DEV-SG", "SBUS" }; mutex_lock(&list_mutex); - len += snprintf(page + len, count - len, - "pages : %li bytes (%li pages per %likB)\n", - pages * PAGE_SIZE, pages, PAGE_SIZE / 1024); + seq_printf(seq, "pages : %li bytes (%li pages per %likB)\n", + pages * PAGE_SIZE, pages, PAGE_SIZE / 1024); devno = 0; list_for_each_entry(mem, &mem_list_head, list) { devno++; - len += snprintf(page + len, count - len, - "buffer %d : ID %08x : type %s\n", - devno, mem->id, types[mem->buffer.dev.type]); - len += snprintf(page + len, count - len, - " addr = 0x%lx, size = %d bytes\n", - (unsigned long)mem->buffer.addr, (int)mem->buffer.bytes); + seq_printf(seq, "buffer %d : ID %08x : type %s\n", + devno, mem->id, types[mem->buffer.dev.type]); + seq_printf(seq, " addr = 0x%lx, size = %d bytes\n", + (unsigned long)mem->buffer.addr, + (int)mem->buffer.bytes); } mutex_unlock(&list_mutex); - return len; + return 0; +} + +static int snd_mem_proc_open(struct inode *inode, struct file *file) +{ + return single_open(file, snd_mem_proc_read, NULL); } /* FIXME: for pci only - other bus? */ #ifdef CONFIG_PCI +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 0) #define gettoken(bufp) strsep(bufp, " \t\n") -static int snd_mem_proc_write(struct file *file, const char __user *buffer, - unsigned long count, void *data) +static ssize_t snd_mem_proc_write(struct file *file, const char __user * buffer, + size_t count, loff_t * ppos) { char buf[128]; char *token, *p; - if (count > ARRAY_SIZE(buf) - 1) - count = ARRAY_SIZE(buf) - 1; + if (count > sizeof(buf) - 1) + return -EINVAL; if (copy_from_user(buf, buffer, count)) return -EFAULT; - buf[ARRAY_SIZE(buf) - 1] = '\0'; + buf[count] = '\0'; p = buf; token = gettoken(&p); if (! token || *token == '#') - return (int)count; + return count; if (strcmp(token, "add") == 0) { char *endp; int vendor, device, size, buffers; @@ -548,7 +645,7 @@ static int snd_mem_proc_write(struct fil (buffers = simple_strtol(token, NULL, 0)) <= 0 || buffers > 4) { printk(KERN_ERR "snd-page-alloc: invalid proc write format\n"); - return (int)count; + return count; } vendor &= 0xffff; device &= 0xffff; @@ -560,7 +657,7 @@ static int snd_mem_proc_write(struct fil if (pci_set_dma_mask(pci, mask) < 0 || pci_set_consistent_dma_mask(pci, mask) < 0) { printk(KERN_ERR "snd-page-alloc: cannot set DMA mask %lx for pci %04x:%04x\n", mask, vendor, device); - return (int)count; + return count; } } for (i = 0; i < buffers; i++) { @@ -570,7 +667,7 @@ static int snd_mem_proc_write(struct fil size, &dmab) < 0) { printk(KERN_ERR "snd-page-alloc: cannot allocate buffer pages (size = %d)\n", size); pci_dev_put(pci); - return (int)count; + return count; } snd_dma_reserve_buf(&dmab, snd_dma_pci_buf_id(pci)); } @@ -596,9 +693,22 @@ static int snd_mem_proc_write(struct fil free_all_reserved_pages(); else printk(KERN_ERR "snd-page-alloc: invalid proc cmd\n"); - return (int)count; + return count; } +#endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 0) */ #endif /* CONFIG_PCI */ + +static const struct file_operations snd_mem_proc_fops = { + .owner = THIS_MODULE, + .open = snd_mem_proc_open, + .read = seq_read, +#ifdef CONFIG_PCI + .write = snd_mem_proc_write, +#endif + .llseek = seq_lseek, + .release = single_release, +}; + #endif /* CONFIG_PROC_FS */ /* @@ -609,12 +719,8 @@ static int __init snd_mem_init(void) { #ifdef CONFIG_PROC_FS snd_mem_proc = create_proc_entry(SND_MEM_PROC_FILE, 0644, NULL); - if (snd_mem_proc) { - snd_mem_proc->read_proc = snd_mem_proc_read; -#ifdef CONFIG_PCI - snd_mem_proc->write_proc = snd_mem_proc_write; -#endif - } + if (snd_mem_proc) + snd_mem_proc->proc_fops = &snd_mem_proc_fops; #endif return 0; } @@ -644,3 +750,5 @@ EXPORT_SYMBOL(snd_dma_reserve_buf); EXPORT_SYMBOL(snd_malloc_pages); EXPORT_SYMBOL(snd_free_pages); + +#include "memalloc.inc1"