11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * Copyright (c) by Jaroslav Kysela <perex@suse.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 271da177e4SLinus Torvalds struct device; 281da177e4SLinus Torvalds 291da177e4SLinus Torvalds /* 301da177e4SLinus Torvalds * buffer device info 311da177e4SLinus Torvalds */ 321da177e4SLinus Torvalds struct snd_dma_device { 331da177e4SLinus Torvalds int type; /* SNDRV_DMA_TYPE_XXX */ 341da177e4SLinus Torvalds struct device *dev; /* generic device */ 351da177e4SLinus Torvalds }; 361da177e4SLinus Torvalds 371da177e4SLinus Torvalds #ifndef snd_dma_pci_data 381da177e4SLinus Torvalds #define snd_dma_pci_data(pci) (&(pci)->dev) 391da177e4SLinus Torvalds #define snd_dma_isa_data() NULL 401da177e4SLinus Torvalds #define snd_dma_sbus_data(sbus) ((struct device *)(sbus)) 411da177e4SLinus Torvalds #define snd_dma_continuous_data(x) ((struct device *)(unsigned long)(x)) 421da177e4SLinus Torvalds #endif 431da177e4SLinus Torvalds 441da177e4SLinus Torvalds 451da177e4SLinus Torvalds /* 461da177e4SLinus Torvalds * buffer types 471da177e4SLinus Torvalds */ 481da177e4SLinus Torvalds #define SNDRV_DMA_TYPE_UNKNOWN 0 /* not defined */ 491da177e4SLinus Torvalds #define SNDRV_DMA_TYPE_CONTINUOUS 1 /* continuous no-DMA memory */ 501da177e4SLinus Torvalds #define SNDRV_DMA_TYPE_DEV 2 /* generic device continuous */ 511da177e4SLinus Torvalds #define SNDRV_DMA_TYPE_DEV_SG 3 /* generic device SG-buffer */ 521da177e4SLinus Torvalds #define SNDRV_DMA_TYPE_SBUS 4 /* SBUS continuous */ 531da177e4SLinus Torvalds 541da177e4SLinus Torvalds /* 551da177e4SLinus Torvalds * info for buffer allocation 561da177e4SLinus Torvalds */ 571da177e4SLinus Torvalds struct snd_dma_buffer { 581da177e4SLinus Torvalds struct snd_dma_device dev; /* device type */ 591da177e4SLinus Torvalds unsigned char *area; /* virtual pointer */ 601da177e4SLinus Torvalds dma_addr_t addr; /* physical address */ 611da177e4SLinus Torvalds size_t bytes; /* buffer size in bytes */ 621da177e4SLinus Torvalds void *private_data; /* private for allocator; don't touch */ 631da177e4SLinus Torvalds }; 641da177e4SLinus Torvalds 651da177e4SLinus Torvalds /* 661da177e4SLinus Torvalds * Scatter-Gather generic device pages 671da177e4SLinus Torvalds */ 681da177e4SLinus Torvalds struct snd_sg_page { 691da177e4SLinus Torvalds void *buf; 701da177e4SLinus Torvalds dma_addr_t addr; 711da177e4SLinus Torvalds }; 721da177e4SLinus Torvalds 731da177e4SLinus Torvalds struct snd_sg_buf { 741da177e4SLinus Torvalds int size; /* allocated byte size */ 751da177e4SLinus Torvalds int pages; /* allocated pages */ 761da177e4SLinus Torvalds int tblsize; /* allocated table size */ 771da177e4SLinus Torvalds struct snd_sg_page *table; /* address table */ 781da177e4SLinus Torvalds struct page **page_table; /* page table (for vmap/vunmap) */ 791da177e4SLinus Torvalds struct device *dev; 801da177e4SLinus Torvalds }; 811da177e4SLinus Torvalds 821da177e4SLinus Torvalds /* 831da177e4SLinus Torvalds * return the pages matching with the given byte size 841da177e4SLinus Torvalds */ 851da177e4SLinus Torvalds static inline unsigned int snd_sgbuf_aligned_pages(size_t size) 861da177e4SLinus Torvalds { 871da177e4SLinus Torvalds return (size + PAGE_SIZE - 1) >> PAGE_SHIFT; 881da177e4SLinus Torvalds } 891da177e4SLinus Torvalds 901da177e4SLinus Torvalds /* 911da177e4SLinus Torvalds * return the physical address at the corresponding offset 921da177e4SLinus Torvalds */ 931da177e4SLinus Torvalds static inline dma_addr_t snd_sgbuf_get_addr(struct snd_sg_buf *sgbuf, size_t offset) 941da177e4SLinus Torvalds { 951da177e4SLinus Torvalds return sgbuf->table[offset >> PAGE_SHIFT].addr + offset % PAGE_SIZE; 961da177e4SLinus Torvalds } 971da177e4SLinus Torvalds 981da177e4SLinus Torvalds 991da177e4SLinus Torvalds /* allocate/release a buffer */ 1001da177e4SLinus Torvalds int snd_dma_alloc_pages(int type, struct device *dev, size_t size, 1011da177e4SLinus Torvalds struct snd_dma_buffer *dmab); 1021da177e4SLinus Torvalds int snd_dma_alloc_pages_fallback(int type, struct device *dev, size_t size, 1031da177e4SLinus Torvalds struct snd_dma_buffer *dmab); 1041da177e4SLinus Torvalds void snd_dma_free_pages(struct snd_dma_buffer *dmab); 1051da177e4SLinus Torvalds 1061da177e4SLinus Torvalds /* buffer-preservation managements */ 1071da177e4SLinus Torvalds 1081da177e4SLinus Torvalds #define snd_dma_pci_buf_id(pci) (((unsigned int)(pci)->vendor << 16) | (pci)->device) 1091da177e4SLinus Torvalds 1101da177e4SLinus Torvalds size_t snd_dma_get_reserved_buf(struct snd_dma_buffer *dmab, unsigned int id); 1111da177e4SLinus Torvalds int snd_dma_reserve_buf(struct snd_dma_buffer *dmab, unsigned int id); 1121da177e4SLinus Torvalds 1131da177e4SLinus Torvalds /* basic memory allocation functions */ 1141ef64e67SAl Viro void *snd_malloc_pages(size_t size, gfp_t gfp_flags); 1151da177e4SLinus Torvalds void snd_free_pages(void *ptr, size_t size); 1161da177e4SLinus Torvalds 1171da177e4SLinus Torvalds #endif /* __SOUND_MEMALLOC_H */ 1181da177e4SLinus Torvalds 119