Thomas Gleixner | 1a59d1b8 | 2019-05-27 08:55:05 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Jaroslav Kysela | c1017a4 | 2007-10-15 09:50:19 +0200 | [diff] [blame] | 3 | * Copyright (c) by Jaroslav Kysela <perex@perex.cz> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * Takashi Iwai <tiwai@suse.de> |
| 5 | * |
| 6 | * Generic memory allocators |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #ifndef __SOUND_MEMALLOC_H |
| 10 | #define __SOUND_MEMALLOC_H |
| 11 | |
Takashi Iwai | 83fbcae | 2021-06-10 13:09:35 +0200 | [diff] [blame] | 12 | #include <asm/page.h> |
| 13 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | struct device; |
Takashi Iwai | a202bd1 | 2021-06-09 18:25:50 +0200 | [diff] [blame] | 15 | struct vm_area_struct; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | |
| 17 | /* |
| 18 | * buffer device info |
| 19 | */ |
| 20 | struct snd_dma_device { |
| 21 | int type; /* SNDRV_DMA_TYPE_XXX */ |
| 22 | struct device *dev; /* generic device */ |
| 23 | }; |
| 24 | |
Antonio Ospite | 019d80d | 2013-01-29 12:56:26 +0100 | [diff] [blame] | 25 | #define snd_dma_continuous_data(x) ((struct device *)(__force unsigned long)(x)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
| 27 | |
| 28 | /* |
| 29 | * buffer types |
| 30 | */ |
| 31 | #define SNDRV_DMA_TYPE_UNKNOWN 0 /* not defined */ |
| 32 | #define SNDRV_DMA_TYPE_CONTINUOUS 1 /* continuous no-DMA memory */ |
| 33 | #define SNDRV_DMA_TYPE_DEV 2 /* generic device continuous */ |
Takashi Iwai | 58a95df | 2021-08-02 09:28:02 +0200 | [diff] [blame] | 34 | #define SNDRV_DMA_TYPE_DEV_WC 5 /* continuous write-combined */ |
Takashi Iwai | cc6a8ac | 2008-06-17 16:39:06 +0200 | [diff] [blame] | 35 | #ifdef CONFIG_SND_DMA_SGBUF |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #define SNDRV_DMA_TYPE_DEV_SG 3 /* generic device SG-buffer */ |
Takashi Iwai | 58a95df | 2021-08-02 09:28:02 +0200 | [diff] [blame] | 37 | #define SNDRV_DMA_TYPE_DEV_WC_SG 6 /* SG write-combined */ |
Takashi Iwai | cc6a8ac | 2008-06-17 16:39:06 +0200 | [diff] [blame] | 38 | #else |
| 39 | #define SNDRV_DMA_TYPE_DEV_SG SNDRV_DMA_TYPE_DEV /* no SG-buf support */ |
Takashi Iwai | 58a95df | 2021-08-02 09:28:02 +0200 | [diff] [blame] | 40 | #define SNDRV_DMA_TYPE_DEV_WC_SG SNDRV_DMA_TYPE_DEV_WC |
Takashi Iwai | cc6a8ac | 2008-06-17 16:39:06 +0200 | [diff] [blame] | 41 | #endif |
Takashi Iwai | a5606f8 | 2013-10-24 14:25:32 +0200 | [diff] [blame] | 42 | #ifdef CONFIG_GENERIC_ALLOCATOR |
Nicolin Chen | 0550321 | 2013-10-23 11:47:43 +0800 | [diff] [blame] | 43 | #define SNDRV_DMA_TYPE_DEV_IRAM 4 /* generic device iram-buffer */ |
Takashi Iwai | a5606f8 | 2013-10-24 14:25:32 +0200 | [diff] [blame] | 44 | #else |
| 45 | #define SNDRV_DMA_TYPE_DEV_IRAM SNDRV_DMA_TYPE_DEV |
| 46 | #endif |
Takashi Iwai | 1fe7f39 | 2019-11-05 09:01:36 +0100 | [diff] [blame] | 47 | #define SNDRV_DMA_TYPE_VMALLOC 7 /* vmalloc'ed buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
| 49 | /* |
| 50 | * info for buffer allocation |
| 51 | */ |
| 52 | struct snd_dma_buffer { |
| 53 | struct snd_dma_device dev; /* device type */ |
| 54 | unsigned char *area; /* virtual pointer */ |
| 55 | dma_addr_t addr; /* physical address */ |
| 56 | size_t bytes; /* buffer size in bytes */ |
| 57 | void *private_data; /* private for allocator; don't touch */ |
| 58 | }; |
| 59 | |
Pierre-Louis Bossart | 4cae99d | 2018-07-25 15:15:56 -0500 | [diff] [blame] | 60 | /* |
| 61 | * return the pages matching with the given byte size |
| 62 | */ |
| 63 | static inline unsigned int snd_sgbuf_aligned_pages(size_t size) |
| 64 | { |
| 65 | return (size + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 66 | } |
| 67 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | /* allocate/release a buffer */ |
| 69 | int snd_dma_alloc_pages(int type, struct device *dev, size_t size, |
| 70 | struct snd_dma_buffer *dmab); |
| 71 | int snd_dma_alloc_pages_fallback(int type, struct device *dev, size_t size, |
| 72 | struct snd_dma_buffer *dmab); |
| 73 | void snd_dma_free_pages(struct snd_dma_buffer *dmab); |
Takashi Iwai | a202bd1 | 2021-06-09 18:25:50 +0200 | [diff] [blame] | 74 | int snd_dma_buffer_mmap(struct snd_dma_buffer *dmab, |
| 75 | struct vm_area_struct *area); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | |
Takashi Iwai | 37af81c | 2021-06-09 18:25:49 +0200 | [diff] [blame] | 77 | dma_addr_t snd_sgbuf_get_addr(struct snd_dma_buffer *dmab, size_t offset); |
| 78 | struct page *snd_sgbuf_get_page(struct snd_dma_buffer *dmab, size_t offset); |
| 79 | unsigned int snd_sgbuf_get_chunk_size(struct snd_dma_buffer *dmab, |
| 80 | unsigned int ofs, unsigned int size); |
| 81 | |
Takashi Iwai | 427ae26 | 2021-07-15 09:58:23 +0200 | [diff] [blame] | 82 | /* device-managed memory allocator */ |
| 83 | struct snd_dma_buffer *snd_devm_alloc_pages(struct device *dev, int type, |
| 84 | size_t size); |
| 85 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | #endif /* __SOUND_MEMALLOC_H */ |
| 87 | |