| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * Digital Audio (PCM) abstract layer |
|---|
| 3 | 4 | * Copyright (c) by Jaroslav Kysela <perex@perex.cz> |
|---|
| 4 | | - * |
|---|
| 5 | | - * |
|---|
| 6 | | - * This program is free software; you can redistribute it and/or modify |
|---|
| 7 | | - * it under the terms of the GNU General Public License as published by |
|---|
| 8 | | - * the Free Software Foundation; either version 2 of the License, or |
|---|
| 9 | | - * (at your option) any later version. |
|---|
| 10 | | - * |
|---|
| 11 | | - * This program is distributed in the hope that it will be useful, |
|---|
| 12 | | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 13 | | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 14 | | - * GNU General Public License for more details. |
|---|
| 15 | | - * |
|---|
| 16 | | - * You should have received a copy of the GNU General Public License |
|---|
| 17 | | - * along with this program; if not, write to the Free Software |
|---|
| 18 | | - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 19 | | - * |
|---|
| 20 | 5 | */ |
|---|
| 21 | 6 | |
|---|
| 22 | 7 | #include <linux/io.h> |
|---|
| .. | .. |
|---|
| 30 | 15 | #include <sound/pcm.h> |
|---|
| 31 | 16 | #include <sound/info.h> |
|---|
| 32 | 17 | #include <sound/initval.h> |
|---|
| 18 | +#include "pcm_local.h" |
|---|
| 33 | 19 | |
|---|
| 34 | 20 | static int preallocate_dma = 1; |
|---|
| 35 | 21 | module_param(preallocate_dma, int, 0444); |
|---|
| .. | .. |
|---|
| 41 | 27 | |
|---|
| 42 | 28 | static const size_t snd_minimum_buffer = 16384; |
|---|
| 43 | 29 | |
|---|
| 30 | +static unsigned long max_alloc_per_card = 32UL * 1024UL * 1024UL; |
|---|
| 31 | +module_param(max_alloc_per_card, ulong, 0644); |
|---|
| 32 | +MODULE_PARM_DESC(max_alloc_per_card, "Max total allocation bytes per card."); |
|---|
| 33 | + |
|---|
| 34 | +static void __update_allocated_size(struct snd_card *card, ssize_t bytes) |
|---|
| 35 | +{ |
|---|
| 36 | + card->total_pcm_alloc_bytes += bytes; |
|---|
| 37 | +} |
|---|
| 38 | + |
|---|
| 39 | +static void update_allocated_size(struct snd_card *card, ssize_t bytes) |
|---|
| 40 | +{ |
|---|
| 41 | + mutex_lock(&card->memory_mutex); |
|---|
| 42 | + __update_allocated_size(card, bytes); |
|---|
| 43 | + mutex_unlock(&card->memory_mutex); |
|---|
| 44 | +} |
|---|
| 45 | + |
|---|
| 46 | +static void decrease_allocated_size(struct snd_card *card, size_t bytes) |
|---|
| 47 | +{ |
|---|
| 48 | + mutex_lock(&card->memory_mutex); |
|---|
| 49 | + WARN_ON(card->total_pcm_alloc_bytes < bytes); |
|---|
| 50 | + __update_allocated_size(card, -(ssize_t)bytes); |
|---|
| 51 | + mutex_unlock(&card->memory_mutex); |
|---|
| 52 | +} |
|---|
| 53 | + |
|---|
| 54 | +static int do_alloc_pages(struct snd_card *card, int type, struct device *dev, |
|---|
| 55 | + size_t size, struct snd_dma_buffer *dmab) |
|---|
| 56 | +{ |
|---|
| 57 | + int err; |
|---|
| 58 | + |
|---|
| 59 | + /* check and reserve the requested size */ |
|---|
| 60 | + mutex_lock(&card->memory_mutex); |
|---|
| 61 | + if (max_alloc_per_card && |
|---|
| 62 | + card->total_pcm_alloc_bytes + size > max_alloc_per_card) { |
|---|
| 63 | + mutex_unlock(&card->memory_mutex); |
|---|
| 64 | + return -ENOMEM; |
|---|
| 65 | + } |
|---|
| 66 | + __update_allocated_size(card, size); |
|---|
| 67 | + mutex_unlock(&card->memory_mutex); |
|---|
| 68 | + |
|---|
| 69 | + err = snd_dma_alloc_pages(type, dev, size, dmab); |
|---|
| 70 | + if (!err) { |
|---|
| 71 | + /* the actual allocation size might be bigger than requested, |
|---|
| 72 | + * and we need to correct the account |
|---|
| 73 | + */ |
|---|
| 74 | + if (dmab->bytes != size) |
|---|
| 75 | + update_allocated_size(card, dmab->bytes - size); |
|---|
| 76 | + } else { |
|---|
| 77 | + /* take back on allocation failure */ |
|---|
| 78 | + decrease_allocated_size(card, size); |
|---|
| 79 | + } |
|---|
| 80 | + return err; |
|---|
| 81 | +} |
|---|
| 82 | + |
|---|
| 83 | +static void do_free_pages(struct snd_card *card, struct snd_dma_buffer *dmab) |
|---|
| 84 | +{ |
|---|
| 85 | + if (!dmab->area) |
|---|
| 86 | + return; |
|---|
| 87 | + decrease_allocated_size(card, dmab->bytes); |
|---|
| 88 | + snd_dma_free_pages(dmab); |
|---|
| 89 | + dmab->area = NULL; |
|---|
| 90 | +} |
|---|
| 44 | 91 | |
|---|
| 45 | 92 | /* |
|---|
| 46 | 93 | * try to allocate as the large pages as possible. |
|---|
| .. | .. |
|---|
| 51 | 98 | static int preallocate_pcm_pages(struct snd_pcm_substream *substream, size_t size) |
|---|
| 52 | 99 | { |
|---|
| 53 | 100 | struct snd_dma_buffer *dmab = &substream->dma_buffer; |
|---|
| 101 | + struct snd_card *card = substream->pcm->card; |
|---|
| 54 | 102 | size_t orig_size = size; |
|---|
| 55 | 103 | int err; |
|---|
| 56 | 104 | |
|---|
| 57 | 105 | do { |
|---|
| 58 | | - if ((err = snd_dma_alloc_pages(dmab->dev.type, dmab->dev.dev, |
|---|
| 59 | | - size, dmab)) < 0) { |
|---|
| 60 | | - if (err != -ENOMEM) |
|---|
| 61 | | - return err; /* fatal error */ |
|---|
| 62 | | - } else |
|---|
| 63 | | - return 0; |
|---|
| 106 | + err = do_alloc_pages(card, dmab->dev.type, dmab->dev.dev, |
|---|
| 107 | + size, dmab); |
|---|
| 108 | + if (err != -ENOMEM) |
|---|
| 109 | + return err; |
|---|
| 64 | 110 | size >>= 1; |
|---|
| 65 | 111 | } while (size >= snd_minimum_buffer); |
|---|
| 66 | 112 | dmab->bytes = 0; /* tell error */ |
|---|
| .. | .. |
|---|
| 76 | 122 | */ |
|---|
| 77 | 123 | static void snd_pcm_lib_preallocate_dma_free(struct snd_pcm_substream *substream) |
|---|
| 78 | 124 | { |
|---|
| 79 | | - if (substream->dma_buffer.area == NULL) |
|---|
| 80 | | - return; |
|---|
| 81 | | - snd_dma_free_pages(&substream->dma_buffer); |
|---|
| 82 | | - substream->dma_buffer.area = NULL; |
|---|
| 125 | + do_free_pages(substream->pcm->card, &substream->dma_buffer); |
|---|
| 83 | 126 | } |
|---|
| 84 | 127 | |
|---|
| 85 | 128 | /** |
|---|
| .. | .. |
|---|
| 87 | 130 | * @substream: the pcm substream instance |
|---|
| 88 | 131 | * |
|---|
| 89 | 132 | * Releases the pre-allocated buffer of the given substream. |
|---|
| 90 | | - * |
|---|
| 91 | | - * Return: Zero if successful, or a negative error code on failure. |
|---|
| 92 | 133 | */ |
|---|
| 93 | | -int snd_pcm_lib_preallocate_free(struct snd_pcm_substream *substream) |
|---|
| 134 | +void snd_pcm_lib_preallocate_free(struct snd_pcm_substream *substream) |
|---|
| 94 | 135 | { |
|---|
| 95 | 136 | snd_pcm_lib_preallocate_dma_free(substream); |
|---|
| 96 | | -#ifdef CONFIG_SND_VERBOSE_PROCFS |
|---|
| 97 | | - snd_info_free_entry(substream->proc_prealloc_max_entry); |
|---|
| 98 | | - substream->proc_prealloc_max_entry = NULL; |
|---|
| 99 | | - snd_info_free_entry(substream->proc_prealloc_entry); |
|---|
| 100 | | - substream->proc_prealloc_entry = NULL; |
|---|
| 101 | | -#endif |
|---|
| 102 | | - return 0; |
|---|
| 103 | 137 | } |
|---|
| 104 | 138 | |
|---|
| 105 | 139 | /** |
|---|
| .. | .. |
|---|
| 107 | 141 | * @pcm: the pcm instance |
|---|
| 108 | 142 | * |
|---|
| 109 | 143 | * Releases all the pre-allocated buffers on the given pcm. |
|---|
| 110 | | - * |
|---|
| 111 | | - * Return: Zero if successful, or a negative error code on failure. |
|---|
| 112 | 144 | */ |
|---|
| 113 | | -int snd_pcm_lib_preallocate_free_for_all(struct snd_pcm *pcm) |
|---|
| 145 | +void snd_pcm_lib_preallocate_free_for_all(struct snd_pcm *pcm) |
|---|
| 114 | 146 | { |
|---|
| 115 | 147 | struct snd_pcm_substream *substream; |
|---|
| 116 | 148 | int stream; |
|---|
| .. | .. |
|---|
| 118 | 150 | for (stream = 0; stream < 2; stream++) |
|---|
| 119 | 151 | for (substream = pcm->streams[stream].substream; substream; substream = substream->next) |
|---|
| 120 | 152 | snd_pcm_lib_preallocate_free(substream); |
|---|
| 121 | | - return 0; |
|---|
| 122 | 153 | } |
|---|
| 123 | 154 | EXPORT_SYMBOL(snd_pcm_lib_preallocate_free_for_all); |
|---|
| 124 | 155 | |
|---|
| .. | .. |
|---|
| 156 | 187 | struct snd_info_buffer *buffer) |
|---|
| 157 | 188 | { |
|---|
| 158 | 189 | struct snd_pcm_substream *substream = entry->private_data; |
|---|
| 190 | + struct snd_card *card = substream->pcm->card; |
|---|
| 159 | 191 | char line[64], str[64]; |
|---|
| 160 | 192 | size_t size; |
|---|
| 161 | 193 | struct snd_dma_buffer new_dmab; |
|---|
| 162 | 194 | |
|---|
| 195 | + mutex_lock(&substream->pcm->open_mutex); |
|---|
| 163 | 196 | if (substream->runtime) { |
|---|
| 164 | 197 | buffer->error = -EBUSY; |
|---|
| 165 | | - return; |
|---|
| 198 | + goto unlock; |
|---|
| 166 | 199 | } |
|---|
| 167 | 200 | if (!snd_info_get_line(buffer, line, sizeof(line))) { |
|---|
| 168 | 201 | snd_info_get_str(str, line, sizeof(str)); |
|---|
| 169 | 202 | size = simple_strtoul(str, NULL, 10) * 1024; |
|---|
| 170 | 203 | if ((size != 0 && size < 8192) || size > substream->dma_max) { |
|---|
| 171 | 204 | buffer->error = -EINVAL; |
|---|
| 172 | | - return; |
|---|
| 205 | + goto unlock; |
|---|
| 173 | 206 | } |
|---|
| 174 | 207 | if (substream->dma_buffer.bytes == size) |
|---|
| 175 | | - return; |
|---|
| 208 | + goto unlock; |
|---|
| 176 | 209 | memset(&new_dmab, 0, sizeof(new_dmab)); |
|---|
| 177 | 210 | new_dmab.dev = substream->dma_buffer.dev; |
|---|
| 178 | 211 | if (size > 0) { |
|---|
| 179 | | - if (snd_dma_alloc_pages(substream->dma_buffer.dev.type, |
|---|
| 180 | | - substream->dma_buffer.dev.dev, |
|---|
| 181 | | - size, &new_dmab) < 0) { |
|---|
| 212 | + if (do_alloc_pages(card, |
|---|
| 213 | + substream->dma_buffer.dev.type, |
|---|
| 214 | + substream->dma_buffer.dev.dev, |
|---|
| 215 | + size, &new_dmab) < 0) { |
|---|
| 182 | 216 | buffer->error = -ENOMEM; |
|---|
| 183 | | - return; |
|---|
| 217 | + goto unlock; |
|---|
| 184 | 218 | } |
|---|
| 185 | 219 | substream->buffer_bytes_max = size; |
|---|
| 186 | 220 | } else { |
|---|
| 187 | 221 | substream->buffer_bytes_max = UINT_MAX; |
|---|
| 188 | 222 | } |
|---|
| 189 | 223 | if (substream->dma_buffer.area) |
|---|
| 190 | | - snd_dma_free_pages(&substream->dma_buffer); |
|---|
| 224 | + do_free_pages(card, &substream->dma_buffer); |
|---|
| 191 | 225 | substream->dma_buffer = new_dmab; |
|---|
| 192 | 226 | } else { |
|---|
| 193 | 227 | buffer->error = -EINVAL; |
|---|
| 194 | 228 | } |
|---|
| 229 | + unlock: |
|---|
| 230 | + mutex_unlock(&substream->pcm->open_mutex); |
|---|
| 195 | 231 | } |
|---|
| 196 | 232 | |
|---|
| 197 | 233 | static inline void preallocate_info_init(struct snd_pcm_substream *substream) |
|---|
| 198 | 234 | { |
|---|
| 199 | 235 | struct snd_info_entry *entry; |
|---|
| 200 | 236 | |
|---|
| 201 | | - if ((entry = snd_info_create_card_entry(substream->pcm->card, "prealloc", substream->proc_root)) != NULL) { |
|---|
| 202 | | - entry->c.text.read = snd_pcm_lib_preallocate_proc_read; |
|---|
| 237 | + entry = snd_info_create_card_entry(substream->pcm->card, "prealloc", |
|---|
| 238 | + substream->proc_root); |
|---|
| 239 | + if (entry) { |
|---|
| 240 | + snd_info_set_text_ops(entry, substream, |
|---|
| 241 | + snd_pcm_lib_preallocate_proc_read); |
|---|
| 203 | 242 | entry->c.text.write = snd_pcm_lib_preallocate_proc_write; |
|---|
| 204 | 243 | entry->mode |= 0200; |
|---|
| 205 | | - entry->private_data = substream; |
|---|
| 206 | | - if (snd_info_register(entry) < 0) { |
|---|
| 207 | | - snd_info_free_entry(entry); |
|---|
| 208 | | - entry = NULL; |
|---|
| 209 | | - } |
|---|
| 210 | 244 | } |
|---|
| 211 | | - substream->proc_prealloc_entry = entry; |
|---|
| 212 | | - if ((entry = snd_info_create_card_entry(substream->pcm->card, "prealloc_max", substream->proc_root)) != NULL) { |
|---|
| 213 | | - entry->c.text.read = snd_pcm_lib_preallocate_max_proc_read; |
|---|
| 214 | | - entry->private_data = substream; |
|---|
| 215 | | - if (snd_info_register(entry) < 0) { |
|---|
| 216 | | - snd_info_free_entry(entry); |
|---|
| 217 | | - entry = NULL; |
|---|
| 218 | | - } |
|---|
| 219 | | - } |
|---|
| 220 | | - substream->proc_prealloc_max_entry = entry; |
|---|
| 245 | + entry = snd_info_create_card_entry(substream->pcm->card, "prealloc_max", |
|---|
| 246 | + substream->proc_root); |
|---|
| 247 | + if (entry) |
|---|
| 248 | + snd_info_set_text_ops(entry, substream, |
|---|
| 249 | + snd_pcm_lib_preallocate_max_proc_read); |
|---|
| 221 | 250 | } |
|---|
| 222 | 251 | |
|---|
| 223 | 252 | #else /* !CONFIG_SND_VERBOSE_PROCFS */ |
|---|
| .. | .. |
|---|
| 227 | 256 | /* |
|---|
| 228 | 257 | * pre-allocate the buffer and create a proc file for the substream |
|---|
| 229 | 258 | */ |
|---|
| 230 | | -static int snd_pcm_lib_preallocate_pages1(struct snd_pcm_substream *substream, |
|---|
| 231 | | - size_t size, size_t max) |
|---|
| 259 | +static void preallocate_pages(struct snd_pcm_substream *substream, |
|---|
| 260 | + int type, struct device *data, |
|---|
| 261 | + size_t size, size_t max, bool managed) |
|---|
| 232 | 262 | { |
|---|
| 263 | + if (snd_BUG_ON(substream->dma_buffer.dev.type)) |
|---|
| 264 | + return; |
|---|
| 265 | + |
|---|
| 266 | + substream->dma_buffer.dev.type = type; |
|---|
| 267 | + substream->dma_buffer.dev.dev = data; |
|---|
| 233 | 268 | |
|---|
| 234 | 269 | if (size > 0 && preallocate_dma && substream->number < maximum_substreams) |
|---|
| 235 | 270 | preallocate_pcm_pages(substream, size); |
|---|
| .. | .. |
|---|
| 237 | 272 | if (substream->dma_buffer.bytes > 0) |
|---|
| 238 | 273 | substream->buffer_bytes_max = substream->dma_buffer.bytes; |
|---|
| 239 | 274 | substream->dma_max = max; |
|---|
| 240 | | - preallocate_info_init(substream); |
|---|
| 241 | | - return 0; |
|---|
| 275 | + if (max > 0) |
|---|
| 276 | + preallocate_info_init(substream); |
|---|
| 277 | + if (managed) |
|---|
| 278 | + substream->managed_buffer_alloc = 1; |
|---|
| 242 | 279 | } |
|---|
| 243 | 280 | |
|---|
| 281 | +static void preallocate_pages_for_all(struct snd_pcm *pcm, int type, |
|---|
| 282 | + void *data, size_t size, size_t max, |
|---|
| 283 | + bool managed) |
|---|
| 284 | +{ |
|---|
| 285 | + struct snd_pcm_substream *substream; |
|---|
| 286 | + int stream; |
|---|
| 287 | + |
|---|
| 288 | + for (stream = 0; stream < 2; stream++) |
|---|
| 289 | + for (substream = pcm->streams[stream].substream; substream; |
|---|
| 290 | + substream = substream->next) |
|---|
| 291 | + preallocate_pages(substream, type, data, size, max, |
|---|
| 292 | + managed); |
|---|
| 293 | +} |
|---|
| 244 | 294 | |
|---|
| 245 | 295 | /** |
|---|
| 246 | 296 | * snd_pcm_lib_preallocate_pages - pre-allocation for the given DMA type |
|---|
| .. | .. |
|---|
| 251 | 301 | * @max: the max. allowed pre-allocation size |
|---|
| 252 | 302 | * |
|---|
| 253 | 303 | * Do pre-allocation for the given DMA buffer type. |
|---|
| 254 | | - * |
|---|
| 255 | | - * Return: Zero if successful, or a negative error code on failure. |
|---|
| 256 | 304 | */ |
|---|
| 257 | | -int snd_pcm_lib_preallocate_pages(struct snd_pcm_substream *substream, |
|---|
| 305 | +void snd_pcm_lib_preallocate_pages(struct snd_pcm_substream *substream, |
|---|
| 258 | 306 | int type, struct device *data, |
|---|
| 259 | 307 | size_t size, size_t max) |
|---|
| 260 | 308 | { |
|---|
| 261 | | - substream->dma_buffer.dev.type = type; |
|---|
| 262 | | - substream->dma_buffer.dev.dev = data; |
|---|
| 263 | | - return snd_pcm_lib_preallocate_pages1(substream, size, max); |
|---|
| 309 | + preallocate_pages(substream, type, data, size, max, false); |
|---|
| 264 | 310 | } |
|---|
| 265 | 311 | EXPORT_SYMBOL(snd_pcm_lib_preallocate_pages); |
|---|
| 266 | 312 | |
|---|
| .. | .. |
|---|
| 274 | 320 | * |
|---|
| 275 | 321 | * Do pre-allocation to all substreams of the given pcm for the |
|---|
| 276 | 322 | * specified DMA type. |
|---|
| 277 | | - * |
|---|
| 278 | | - * Return: Zero if successful, or a negative error code on failure. |
|---|
| 279 | 323 | */ |
|---|
| 280 | | -int snd_pcm_lib_preallocate_pages_for_all(struct snd_pcm *pcm, |
|---|
| 324 | +void snd_pcm_lib_preallocate_pages_for_all(struct snd_pcm *pcm, |
|---|
| 281 | 325 | int type, void *data, |
|---|
| 282 | 326 | size_t size, size_t max) |
|---|
| 283 | 327 | { |
|---|
| 284 | | - struct snd_pcm_substream *substream; |
|---|
| 285 | | - int stream, err; |
|---|
| 286 | | - |
|---|
| 287 | | - for (stream = 0; stream < 2; stream++) |
|---|
| 288 | | - for (substream = pcm->streams[stream].substream; substream; substream = substream->next) |
|---|
| 289 | | - if ((err = snd_pcm_lib_preallocate_pages(substream, type, data, size, max)) < 0) |
|---|
| 290 | | - return err; |
|---|
| 291 | | - return 0; |
|---|
| 328 | + preallocate_pages_for_all(pcm, type, data, size, max, false); |
|---|
| 292 | 329 | } |
|---|
| 293 | 330 | EXPORT_SYMBOL(snd_pcm_lib_preallocate_pages_for_all); |
|---|
| 294 | 331 | |
|---|
| 295 | | -#ifdef CONFIG_SND_DMA_SGBUF |
|---|
| 296 | 332 | /** |
|---|
| 333 | + * snd_pcm_set_managed_buffer - set up buffer management for a substream |
|---|
| 334 | + * @substream: the pcm substream instance |
|---|
| 335 | + * @type: DMA type (SNDRV_DMA_TYPE_*) |
|---|
| 336 | + * @data: DMA type dependent data |
|---|
| 337 | + * @size: the requested pre-allocation size in bytes |
|---|
| 338 | + * @max: the max. allowed pre-allocation size |
|---|
| 339 | + * |
|---|
| 340 | + * Do pre-allocation for the given DMA buffer type, and set the managed |
|---|
| 341 | + * buffer allocation mode to the given substream. |
|---|
| 342 | + * In this mode, PCM core will allocate a buffer automatically before PCM |
|---|
| 343 | + * hw_params ops call, and release the buffer after PCM hw_free ops call |
|---|
| 344 | + * as well, so that the driver doesn't need to invoke the allocation and |
|---|
| 345 | + * the release explicitly in its callback. |
|---|
| 346 | + * When a buffer is actually allocated before the PCM hw_params call, it |
|---|
| 347 | + * turns on the runtime buffer_changed flag for drivers changing their h/w |
|---|
| 348 | + * parameters accordingly. |
|---|
| 349 | + */ |
|---|
| 350 | +void snd_pcm_set_managed_buffer(struct snd_pcm_substream *substream, int type, |
|---|
| 351 | + struct device *data, size_t size, size_t max) |
|---|
| 352 | +{ |
|---|
| 353 | + preallocate_pages(substream, type, data, size, max, true); |
|---|
| 354 | +} |
|---|
| 355 | +EXPORT_SYMBOL(snd_pcm_set_managed_buffer); |
|---|
| 356 | + |
|---|
| 357 | +/** |
|---|
| 358 | + * snd_pcm_set_managed_buffer_all - set up buffer management for all substreams |
|---|
| 359 | + * for all substreams |
|---|
| 360 | + * @pcm: the pcm instance |
|---|
| 361 | + * @type: DMA type (SNDRV_DMA_TYPE_*) |
|---|
| 362 | + * @data: DMA type dependent data |
|---|
| 363 | + * @size: the requested pre-allocation size in bytes |
|---|
| 364 | + * @max: the max. allowed pre-allocation size |
|---|
| 365 | + * |
|---|
| 366 | + * Do pre-allocation to all substreams of the given pcm for the specified DMA |
|---|
| 367 | + * type and size, and set the managed_buffer_alloc flag to each substream. |
|---|
| 368 | + */ |
|---|
| 369 | +void snd_pcm_set_managed_buffer_all(struct snd_pcm *pcm, int type, |
|---|
| 370 | + struct device *data, |
|---|
| 371 | + size_t size, size_t max) |
|---|
| 372 | +{ |
|---|
| 373 | + preallocate_pages_for_all(pcm, type, data, size, max, true); |
|---|
| 374 | +} |
|---|
| 375 | +EXPORT_SYMBOL(snd_pcm_set_managed_buffer_all); |
|---|
| 376 | + |
|---|
| 377 | +#ifdef CONFIG_SND_DMA_SGBUF |
|---|
| 378 | +/* |
|---|
| 297 | 379 | * snd_pcm_sgbuf_ops_page - get the page struct at the given offset |
|---|
| 298 | 380 | * @substream: the pcm substream instance |
|---|
| 299 | 381 | * @offset: the buffer offset |
|---|
| .. | .. |
|---|
| 311 | 393 | return NULL; |
|---|
| 312 | 394 | return sgbuf->page_table[idx]; |
|---|
| 313 | 395 | } |
|---|
| 314 | | -EXPORT_SYMBOL(snd_pcm_sgbuf_ops_page); |
|---|
| 315 | 396 | #endif /* CONFIG_SND_DMA_SGBUF */ |
|---|
| 316 | 397 | |
|---|
| 317 | 398 | /** |
|---|
| .. | .. |
|---|
| 327 | 408 | */ |
|---|
| 328 | 409 | int snd_pcm_lib_malloc_pages(struct snd_pcm_substream *substream, size_t size) |
|---|
| 329 | 410 | { |
|---|
| 411 | + struct snd_card *card; |
|---|
| 330 | 412 | struct snd_pcm_runtime *runtime; |
|---|
| 331 | 413 | struct snd_dma_buffer *dmab = NULL; |
|---|
| 332 | 414 | |
|---|
| .. | .. |
|---|
| 336 | 418 | SNDRV_DMA_TYPE_UNKNOWN)) |
|---|
| 337 | 419 | return -EINVAL; |
|---|
| 338 | 420 | runtime = substream->runtime; |
|---|
| 421 | + card = substream->pcm->card; |
|---|
| 339 | 422 | |
|---|
| 340 | 423 | if (runtime->dma_buffer_p) { |
|---|
| 341 | 424 | /* perphaps, we might free the large DMA memory region |
|---|
| .. | .. |
|---|
| 355 | 438 | if (! dmab) |
|---|
| 356 | 439 | return -ENOMEM; |
|---|
| 357 | 440 | dmab->dev = substream->dma_buffer.dev; |
|---|
| 358 | | - if (snd_dma_alloc_pages(substream->dma_buffer.dev.type, |
|---|
| 359 | | - substream->dma_buffer.dev.dev, |
|---|
| 360 | | - size, dmab) < 0) { |
|---|
| 441 | + if (do_alloc_pages(card, |
|---|
| 442 | + substream->dma_buffer.dev.type, |
|---|
| 443 | + substream->dma_buffer.dev.dev, |
|---|
| 444 | + size, dmab) < 0) { |
|---|
| 361 | 445 | kfree(dmab); |
|---|
| 362 | 446 | return -ENOMEM; |
|---|
| 363 | 447 | } |
|---|
| .. | .. |
|---|
| 386 | 470 | if (runtime->dma_area == NULL) |
|---|
| 387 | 471 | return 0; |
|---|
| 388 | 472 | if (runtime->dma_buffer_p != &substream->dma_buffer) { |
|---|
| 473 | + struct snd_card *card = substream->pcm->card; |
|---|
| 474 | + |
|---|
| 389 | 475 | /* it's a newly allocated buffer. release it now. */ |
|---|
| 390 | | - snd_dma_free_pages(runtime->dma_buffer_p); |
|---|
| 476 | + do_free_pages(card, runtime->dma_buffer_p); |
|---|
| 391 | 477 | kfree(runtime->dma_buffer_p); |
|---|
| 392 | 478 | } |
|---|
| 393 | 479 | snd_pcm_set_runtime_buffer(substream, NULL); |
|---|
| .. | .. |
|---|
| 408 | 494 | return 0; /* already large enough */ |
|---|
| 409 | 495 | vfree(runtime->dma_area); |
|---|
| 410 | 496 | } |
|---|
| 411 | | - runtime->dma_area = __vmalloc(size, gfp_flags, PAGE_KERNEL); |
|---|
| 497 | + runtime->dma_area = __vmalloc(size, gfp_flags); |
|---|
| 412 | 498 | if (!runtime->dma_area) |
|---|
| 413 | 499 | return -ENOMEM; |
|---|
| 414 | 500 | runtime->dma_bytes = size; |
|---|