11da177e4SLinus Torvalds /* 2c1017a4cSJaroslav Kysela * Copyright (c) by Jaroslav Kysela <perex@perex.cz> 31da177e4SLinus Torvalds * Takashi Iwai <tiwai@suse.de> 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Generic memory allocators 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or modify 91da177e4SLinus Torvalds * it under the terms of the GNU General Public License as published by 101da177e4SLinus Torvalds * the Free Software Foundation; either version 2 of the License, or 111da177e4SLinus Torvalds * (at your option) any later version. 121da177e4SLinus Torvalds * 131da177e4SLinus Torvalds * This program is distributed in the hope that it will be useful, 141da177e4SLinus Torvalds * but WITHOUT ANY WARRANTY; without even the implied warranty of 151da177e4SLinus Torvalds * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 161da177e4SLinus Torvalds * GNU General Public License for more details. 171da177e4SLinus Torvalds * 181da177e4SLinus Torvalds * You should have received a copy of the GNU General Public License 191da177e4SLinus Torvalds * along with this program; if not, write to the Free Software 201da177e4SLinus Torvalds * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 211da177e4SLinus Torvalds * 221da177e4SLinus Torvalds */ 231da177e4SLinus Torvalds 241da177e4SLinus Torvalds #ifndef __SOUND_MEMALLOC_H 251da177e4SLinus Torvalds #define __SOUND_MEMALLOC_H 261da177e4SLinus Torvalds 27345a9ca3STakashi Iwai #include <asm/page.h> 28345a9ca3STakashi Iwai 291da177e4SLinus Torvalds struct device; 301da177e4SLinus Torvalds 311da177e4SLinus Torvalds /* 321da177e4SLinus Torvalds * buffer device info 331da177e4SLinus Torvalds */ 341da177e4SLinus Torvalds struct snd_dma_device { 351da177e4SLinus Torvalds int type; /* SNDRV_DMA_TYPE_XXX */ 361da177e4SLinus Torvalds struct device *dev; /* generic device */ 371da177e4SLinus Torvalds }; 381da177e4SLinus Torvalds 391da177e4SLinus Torvalds #define snd_dma_pci_data(pci) (&(pci)->dev) 401da177e4SLinus Torvalds #define snd_dma_isa_data() NULL 41019d80dbSAntonio Ospite #define snd_dma_continuous_data(x) ((struct device *)(__force unsigned long)(x)) 421da177e4SLinus Torvalds 431da177e4SLinus Torvalds 441da177e4SLinus Torvalds /* 451da177e4SLinus Torvalds * buffer types 461da177e4SLinus Torvalds */ 471da177e4SLinus Torvalds #define SNDRV_DMA_TYPE_UNKNOWN 0 /* not defined */ 481da177e4SLinus Torvalds #define SNDRV_DMA_TYPE_CONTINUOUS 1 /* continuous no-DMA memory */ 491da177e4SLinus Torvalds #define SNDRV_DMA_TYPE_DEV 2 /* generic device continuous */ 5042e748a0STakashi Iwai #define SNDRV_DMA_TYPE_DEV_UC 5 /* continuous non-cahced */ 51cc6a8acdSTakashi Iwai #ifdef CONFIG_SND_DMA_SGBUF 521da177e4SLinus Torvalds #define SNDRV_DMA_TYPE_DEV_SG 3 /* generic device SG-buffer */ 5342e748a0STakashi Iwai #define SNDRV_DMA_TYPE_DEV_UC_SG 6 /* SG non-cached */ 54cc6a8acdSTakashi Iwai #else 55cc6a8acdSTakashi Iwai #define SNDRV_DMA_TYPE_DEV_SG SNDRV_DMA_TYPE_DEV /* no SG-buf support */ 5642e748a0STakashi Iwai #define SNDRV_DMA_TYPE_DEV_UC_SG SNDRV_DMA_TYPE_DEV_UC 57cc6a8acdSTakashi Iwai #endif 58a5606f85STakashi Iwai #ifdef CONFIG_GENERIC_ALLOCATOR 5905503214SNicolin Chen #define SNDRV_DMA_TYPE_DEV_IRAM 4 /* generic device iram-buffer */ 60a5606f85STakashi Iwai #else 61a5606f85STakashi Iwai #define SNDRV_DMA_TYPE_DEV_IRAM SNDRV_DMA_TYPE_DEV 62a5606f85STakashi Iwai #endif 631da177e4SLinus Torvalds 641da177e4SLinus Torvalds /* 651da177e4SLinus Torvalds * info for buffer allocation 661da177e4SLinus Torvalds */ 671da177e4SLinus Torvalds struct snd_dma_buffer { 681da177e4SLinus Torvalds struct snd_dma_device dev; /* device type */ 691da177e4SLinus Torvalds unsigned char *area; /* virtual pointer */ 701da177e4SLinus Torvalds dma_addr_t addr; /* physical address */ 711da177e4SLinus Torvalds size_t bytes; /* buffer size in bytes */ 721da177e4SLinus Torvalds void *private_data; /* private for allocator; don't touch */ 731da177e4SLinus Torvalds }; 741da177e4SLinus Torvalds 754cae99d9SPierre-Louis Bossart /* 764cae99d9SPierre-Louis Bossart * return the pages matching with the given byte size 774cae99d9SPierre-Louis Bossart */ 784cae99d9SPierre-Louis Bossart static inline unsigned int snd_sgbuf_aligned_pages(size_t size) 794cae99d9SPierre-Louis Bossart { 804cae99d9SPierre-Louis Bossart return (size + PAGE_SIZE - 1) >> PAGE_SHIFT; 814cae99d9SPierre-Louis Bossart } 824cae99d9SPierre-Louis Bossart 83cc6a8acdSTakashi Iwai #ifdef CONFIG_SND_DMA_SGBUF 841da177e4SLinus Torvalds /* 851da177e4SLinus Torvalds * Scatter-Gather generic device pages 861da177e4SLinus Torvalds */ 8777a23f26STakashi Iwai void *snd_malloc_sgbuf_pages(struct device *device, 8877a23f26STakashi Iwai size_t size, struct snd_dma_buffer *dmab, 8977a23f26STakashi Iwai size_t *res_size); 9077a23f26STakashi Iwai int snd_free_sgbuf_pages(struct snd_dma_buffer *dmab); 9177a23f26STakashi Iwai 921da177e4SLinus Torvalds struct snd_sg_page { 931da177e4SLinus Torvalds void *buf; 941da177e4SLinus Torvalds dma_addr_t addr; 951da177e4SLinus Torvalds }; 961da177e4SLinus Torvalds 971da177e4SLinus Torvalds struct snd_sg_buf { 981da177e4SLinus Torvalds int size; /* allocated byte size */ 991da177e4SLinus Torvalds int pages; /* allocated pages */ 1001da177e4SLinus Torvalds int tblsize; /* allocated table size */ 1011da177e4SLinus Torvalds struct snd_sg_page *table; /* address table */ 1021da177e4SLinus Torvalds struct page **page_table; /* page table (for vmap/vunmap) */ 1031da177e4SLinus Torvalds struct device *dev; 1041da177e4SLinus Torvalds }; 1051da177e4SLinus Torvalds 1061da177e4SLinus Torvalds /* 1071da177e4SLinus Torvalds * return the physical address at the corresponding offset 1081da177e4SLinus Torvalds */ 1099d069dc0STakashi Iwai static inline dma_addr_t snd_sgbuf_get_addr(struct snd_dma_buffer *dmab, 1109d069dc0STakashi Iwai size_t offset) 1111da177e4SLinus Torvalds { 1129d069dc0STakashi Iwai struct snd_sg_buf *sgbuf = dmab->private_data; 11351e9f2e6STakashi Iwai dma_addr_t addr = sgbuf->table[offset >> PAGE_SHIFT].addr; 114932e9decSStefano Panella addr &= ~((dma_addr_t)PAGE_SIZE - 1); 11551e9f2e6STakashi Iwai return addr + offset % PAGE_SIZE; 1161da177e4SLinus Torvalds } 1171da177e4SLinus Torvalds 11877a23f26STakashi Iwai /* 11977a23f26STakashi Iwai * return the virtual address at the corresponding offset 12077a23f26STakashi Iwai */ 1219d069dc0STakashi Iwai static inline void *snd_sgbuf_get_ptr(struct snd_dma_buffer *dmab, 1229d069dc0STakashi Iwai size_t offset) 12377a23f26STakashi Iwai { 1249d069dc0STakashi Iwai struct snd_sg_buf *sgbuf = dmab->private_data; 12577a23f26STakashi Iwai return sgbuf->table[offset >> PAGE_SHIFT].buf + offset % PAGE_SIZE; 12677a23f26STakashi Iwai } 1279d069dc0STakashi Iwai 1289d069dc0STakashi Iwai unsigned int snd_sgbuf_get_chunk_size(struct snd_dma_buffer *dmab, 1299d069dc0STakashi Iwai unsigned int ofs, unsigned int size); 1309d069dc0STakashi Iwai #else 1319d069dc0STakashi Iwai /* non-SG versions */ 1329d069dc0STakashi Iwai static inline dma_addr_t snd_sgbuf_get_addr(struct snd_dma_buffer *dmab, 1339d069dc0STakashi Iwai size_t offset) 1349d069dc0STakashi Iwai { 1359d069dc0STakashi Iwai return dmab->addr + offset; 1369d069dc0STakashi Iwai } 1379d069dc0STakashi Iwai 1389d069dc0STakashi Iwai static inline void *snd_sgbuf_get_ptr(struct snd_dma_buffer *dmab, 1399d069dc0STakashi Iwai size_t offset) 1409d069dc0STakashi Iwai { 1419d069dc0STakashi Iwai return dmab->area + offset; 1429d069dc0STakashi Iwai } 1439d069dc0STakashi Iwai 1449d069dc0STakashi Iwai #define snd_sgbuf_get_chunk_size(dmab, ofs, size) (size) 1459d069dc0STakashi Iwai 146cc6a8acdSTakashi Iwai #endif /* CONFIG_SND_DMA_SGBUF */ 1471da177e4SLinus Torvalds 1481da177e4SLinus Torvalds /* allocate/release a buffer */ 1491da177e4SLinus Torvalds int snd_dma_alloc_pages(int type, struct device *dev, size_t size, 1501da177e4SLinus Torvalds struct snd_dma_buffer *dmab); 1511da177e4SLinus Torvalds int snd_dma_alloc_pages_fallback(int type, struct device *dev, size_t size, 1521da177e4SLinus Torvalds struct snd_dma_buffer *dmab); 1531da177e4SLinus Torvalds void snd_dma_free_pages(struct snd_dma_buffer *dmab); 1541da177e4SLinus Torvalds 1551da177e4SLinus Torvalds /* basic memory allocation functions */ 1561ef64e67SAl Viro void *snd_malloc_pages(size_t size, gfp_t gfp_flags); 1571da177e4SLinus Torvalds void snd_free_pages(void *ptr, size_t size); 1581da177e4SLinus Torvalds 1591da177e4SLinus Torvalds #endif /* __SOUND_MEMALLOC_H */ 1601da177e4SLinus Torvalds 161