/*1* Copyright (c) by Jaroslav Kysela <[email protected]>2* Takashi Iwai <[email protected]>3*4* Generic memory allocators5*6*7* This program is free software; you can redistribute it and/or modify8* it under the terms of the GNU General Public License as published by9* the Free Software Foundation; either version 2 of the License, or10* (at your option) any later version.11*12* This program is distributed in the hope that it will be useful,13* but WITHOUT ANY WARRANTY; without even the implied warranty of14* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the15* GNU General Public License for more details.16*17* You should have received a copy of the GNU General Public License18* along with this program; if not, write to the Free Software19* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA20*21*/2223#ifndef __SOUND_MEMALLOC_H24#define __SOUND_MEMALLOC_H2526struct device;2728/*29* buffer device info30*/31struct snd_dma_device {32int type; /* SNDRV_DMA_TYPE_XXX */33struct device *dev; /* generic device */34};3536#ifndef snd_dma_pci_data37#define snd_dma_pci_data(pci) (&(pci)->dev)38#define snd_dma_isa_data() NULL39#define snd_dma_continuous_data(x) ((struct device *)(unsigned long)(x))40#endif414243/*44* buffer types45*/46#define SNDRV_DMA_TYPE_UNKNOWN 0 /* not defined */47#define SNDRV_DMA_TYPE_CONTINUOUS 1 /* continuous no-DMA memory */48#define SNDRV_DMA_TYPE_DEV 2 /* generic device continuous */49#ifdef CONFIG_SND_DMA_SGBUF50#define SNDRV_DMA_TYPE_DEV_SG 3 /* generic device SG-buffer */51#else52#define SNDRV_DMA_TYPE_DEV_SG SNDRV_DMA_TYPE_DEV /* no SG-buf support */53#endif5455/*56* info for buffer allocation57*/58struct snd_dma_buffer {59struct snd_dma_device dev; /* device type */60unsigned char *area; /* virtual pointer */61dma_addr_t addr; /* physical address */62size_t bytes; /* buffer size in bytes */63void *private_data; /* private for allocator; don't touch */64};6566#ifdef CONFIG_SND_DMA_SGBUF67/*68* Scatter-Gather generic device pages69*/70void *snd_malloc_sgbuf_pages(struct device *device,71size_t size, struct snd_dma_buffer *dmab,72size_t *res_size);73int snd_free_sgbuf_pages(struct snd_dma_buffer *dmab);7475struct snd_sg_page {76void *buf;77dma_addr_t addr;78};7980struct snd_sg_buf {81int size; /* allocated byte size */82int pages; /* allocated pages */83int tblsize; /* allocated table size */84struct snd_sg_page *table; /* address table */85struct page **page_table; /* page table (for vmap/vunmap) */86struct device *dev;87};8889/*90* return the pages matching with the given byte size91*/92static inline unsigned int snd_sgbuf_aligned_pages(size_t size)93{94return (size + PAGE_SIZE - 1) >> PAGE_SHIFT;95}9697/*98* return the physical address at the corresponding offset99*/100static inline dma_addr_t snd_sgbuf_get_addr(struct snd_sg_buf *sgbuf, size_t offset)101{102dma_addr_t addr = sgbuf->table[offset >> PAGE_SHIFT].addr;103addr &= PAGE_MASK;104return addr + offset % PAGE_SIZE;105}106107/*108* return the virtual address at the corresponding offset109*/110static inline void *snd_sgbuf_get_ptr(struct snd_sg_buf *sgbuf, size_t offset)111{112return sgbuf->table[offset >> PAGE_SHIFT].buf + offset % PAGE_SIZE;113}114#endif /* CONFIG_SND_DMA_SGBUF */115116/* allocate/release a buffer */117int snd_dma_alloc_pages(int type, struct device *dev, size_t size,118struct snd_dma_buffer *dmab);119int snd_dma_alloc_pages_fallback(int type, struct device *dev, size_t size,120struct snd_dma_buffer *dmab);121void snd_dma_free_pages(struct snd_dma_buffer *dmab);122123/* buffer-preservation managements */124125#define snd_dma_pci_buf_id(pci) (((unsigned int)(pci)->vendor << 16) | (pci)->device)126127size_t snd_dma_get_reserved_buf(struct snd_dma_buffer *dmab, unsigned int id);128int snd_dma_reserve_buf(struct snd_dma_buffer *dmab, unsigned int id);129130/* basic memory allocation functions */131void *snd_malloc_pages(size_t size, gfp_t gfp_flags);132void snd_free_pages(void *ptr, size_t size);133134#endif /* __SOUND_MEMALLOC_H */135136137138