blob: 809fd79ecad678ee75836049459fe6eb7ece9c99 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02002 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Takashi Iwai <tiwai@suse.de>
4 *
5 * Generic memory allocators
6 *
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 */
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/module.h>
25#include <linux/proc_fs.h>
26#include <linux/init.h>
27#include <linux/pci.h>
28#include <linux/slab.h>
29#include <linux/mm.h>
Takashi Iwaiccec6e22007-09-17 21:55:10 +020030#include <linux/seq_file.h>
Takashi Iwaib6a96912005-05-30 18:27:03 +020031#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/dma-mapping.h>
Nicolin Chen05503212013-10-23 11:47:43 +080033#include <linux/genalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/moduleparam.h>
Ingo Molnar1a60d4c2006-01-16 16:29:08 +010035#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <sound/memalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38
Jaroslav Kyselac1017a42007-10-15 09:50:19 +020039MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>, Jaroslav Kysela <perex@perex.cz>");
Linus Torvalds1da177e2005-04-16 15:20:36 -070040MODULE_DESCRIPTION("Memory allocator for ALSA system.");
41MODULE_LICENSE("GPL");
42
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044/*
45 */
46
Ingo Molnar1a60d4c2006-01-16 16:29:08 +010047static DEFINE_MUTEX(list_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048static LIST_HEAD(mem_list_head);
49
50/* buffer preservation list */
51struct snd_mem_list {
52 struct snd_dma_buffer buffer;
53 unsigned int id;
54 struct list_head list;
55};
56
57/* id for pre-allocated buffers */
58#define SNDRV_DMA_DEVICE_UNUSED (unsigned int)-1
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 *
62 * Generic memory allocators
63 *
64 */
65
66static long snd_allocated_pages; /* holding the number of allocated pages */
67
68static inline void inc_snd_pages(int order)
69{
70 snd_allocated_pages += 1 << order;
71}
72
73static inline void dec_snd_pages(int order)
74{
75 snd_allocated_pages -= 1 << order;
76}
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078/**
79 * snd_malloc_pages - allocate pages with the given size
80 * @size: the size to allocate in bytes
81 * @gfp_flags: the allocation conditions, GFP_XXX
82 *
83 * Allocates the physically contiguous pages with the given size.
84 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +010085 * Return: The pointer of the buffer, or %NULL if no enough memory.
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 */
Al Viro1ef64e62005-10-21 03:22:18 -040087void *snd_malloc_pages(size_t size, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
89 int pg;
90 void *res;
91
Takashi Iwai7eaa9432008-08-08 17:09:09 +020092 if (WARN_ON(!size))
93 return NULL;
94 if (WARN_ON(!gfp_flags))
95 return NULL;
Hugh Dickinsf3d48f02005-11-21 21:32:22 -080096 gfp_flags |= __GFP_COMP; /* compound page lets parts be mapped */
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 pg = get_order(size);
Takashi Iwai2ba8c152006-01-31 14:44:28 +010098 if ((res = (void *) __get_free_pages(gfp_flags, pg)) != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 inc_snd_pages(pg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 return res;
101}
102
103/**
104 * snd_free_pages - release the pages
105 * @ptr: the buffer pointer to release
106 * @size: the allocated buffer size
107 *
108 * Releases the buffer allocated via snd_malloc_pages().
109 */
110void snd_free_pages(void *ptr, size_t size)
111{
112 int pg;
113
114 if (ptr == NULL)
115 return;
116 pg = get_order(size);
117 dec_snd_pages(pg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 free_pages((unsigned long) ptr, pg);
119}
120
121/*
122 *
123 * Bus-specific memory allocators
124 *
125 */
126
Takashi Iwai8f115512007-07-26 18:59:36 +0200127#ifdef CONFIG_HAS_DMA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128/* allocate the coherent DMA pages */
129static void *snd_malloc_dev_pages(struct device *dev, size_t size, dma_addr_t *dma)
130{
131 int pg;
132 void *res;
Al Viro1ef64e62005-10-21 03:22:18 -0400133 gfp_t gfp_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200135 if (WARN_ON(!dma))
136 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 pg = get_order(size);
138 gfp_flags = GFP_KERNEL
Hugh Dickinsf3d48f02005-11-21 21:32:22 -0800139 | __GFP_COMP /* compound page lets parts be mapped */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 | __GFP_NORETRY /* don't trigger OOM-killer */
141 | __GFP_NOWARN; /* no stack trace print - this call is non-critical */
142 res = dma_alloc_coherent(dev, PAGE_SIZE << pg, dma, gfp_flags);
Takashi Iwai2ba8c152006-01-31 14:44:28 +0100143 if (res != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 inc_snd_pages(pg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 return res;
147}
148
149/* free the coherent DMA pages */
150static void snd_free_dev_pages(struct device *dev, size_t size, void *ptr,
151 dma_addr_t dma)
152{
153 int pg;
154
155 if (ptr == NULL)
156 return;
157 pg = get_order(size);
158 dec_snd_pages(pg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 dma_free_coherent(dev, PAGE_SIZE << pg, ptr, dma);
160}
Nicolin Chen05503212013-10-23 11:47:43 +0800161
Takashi Iwai63437312013-10-28 16:08:27 +0100162#ifdef CONFIG_GENERIC_ALLOCATOR
Nicolin Chen05503212013-10-23 11:47:43 +0800163/**
164 * snd_malloc_dev_iram - allocate memory from on-chip internal ram
165 * @dmab: buffer allocation record to store the allocated data
166 * @size: number of bytes to allocate from the iram
167 *
168 * This function requires iram phandle provided via of_node
169 */
170void snd_malloc_dev_iram(struct snd_dma_buffer *dmab, size_t size)
171{
172 struct device *dev = dmab->dev.dev;
173 struct gen_pool *pool = NULL;
174
175 if (dev->of_node)
176 pool = of_get_named_gen_pool(dev->of_node, "iram", 0);
177
178 if (!pool)
179 return;
180
181 /* Assign the pool into private_data field */
182 dmab->private_data = pool;
183
184 dmab->area = (void *)gen_pool_alloc(pool, size);
185 if (!dmab->area)
186 return;
187
188 dmab->addr = gen_pool_virt_to_phys(pool, (unsigned long)dmab->area);
189}
190
191/**
192 * snd_free_dev_iram - free allocated specific memory from on-chip internal ram
193 * @dmab: buffer allocation record to store the allocated data
194 */
195void snd_free_dev_iram(struct snd_dma_buffer *dmab)
196{
197 struct gen_pool *pool = dmab->private_data;
198
199 if (pool && dmab->area)
200 gen_pool_free(pool, (unsigned long)dmab->area, dmab->bytes);
201}
Takashi Iwai63437312013-10-28 16:08:27 +0100202#endif /* CONFIG_GENERIC_ALLOCATOR */
Takashi Iwai8f115512007-07-26 18:59:36 +0200203#endif /* CONFIG_HAS_DMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205/*
206 *
207 * ALSA generic memory management
208 *
209 */
210
211
212/**
213 * snd_dma_alloc_pages - allocate the buffer area according to the given type
214 * @type: the DMA buffer type
215 * @device: the device pointer
216 * @size: the buffer size to allocate
217 * @dmab: buffer allocation record to store the allocated data
218 *
219 * Calls the memory-allocator function for the corresponding
220 * buffer type.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100221 *
222 * Return: Zero if the buffer with the given size is allocated successfully,
223 * otherwise a negative value on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 */
225int snd_dma_alloc_pages(int type, struct device *device, size_t size,
226 struct snd_dma_buffer *dmab)
227{
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200228 if (WARN_ON(!size))
229 return -ENXIO;
230 if (WARN_ON(!dmab))
231 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 dmab->dev.type = type;
234 dmab->dev.dev = device;
235 dmab->bytes = 0;
236 switch (type) {
237 case SNDRV_DMA_TYPE_CONTINUOUS:
Clemens Ladischfea952e2011-02-14 11:00:47 +0100238 dmab->area = snd_malloc_pages(size,
239 (__force gfp_t)(unsigned long)device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 dmab->addr = 0;
241 break;
Takashi Iwai8f115512007-07-26 18:59:36 +0200242#ifdef CONFIG_HAS_DMA
Takashi Iwaia5606f82013-10-24 14:25:32 +0200243#ifdef CONFIG_GENERIC_ALLOCATOR
Nicolin Chen05503212013-10-23 11:47:43 +0800244 case SNDRV_DMA_TYPE_DEV_IRAM:
245 snd_malloc_dev_iram(dmab, size);
246 if (dmab->area)
247 break;
248 /* Internal memory might have limited size and no enough space,
249 * so if we fail to malloc, try to fetch memory traditionally.
250 */
251 dmab->dev.type = SNDRV_DMA_TYPE_DEV;
Takashi Iwaia5606f82013-10-24 14:25:32 +0200252#endif /* CONFIG_GENERIC_ALLOCATOR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 case SNDRV_DMA_TYPE_DEV:
254 dmab->area = snd_malloc_dev_pages(device, size, &dmab->addr);
255 break;
Takashi Iwaicc6a8ac2008-06-17 16:39:06 +0200256#endif
257#ifdef CONFIG_SND_DMA_SGBUF
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 case SNDRV_DMA_TYPE_DEV_SG:
259 snd_malloc_sgbuf_pages(device, size, dmab, NULL);
260 break;
Takashi Iwai8f115512007-07-26 18:59:36 +0200261#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 default:
263 printk(KERN_ERR "snd-malloc: invalid device type %d\n", type);
264 dmab->area = NULL;
265 dmab->addr = 0;
266 return -ENXIO;
267 }
268 if (! dmab->area)
269 return -ENOMEM;
270 dmab->bytes = size;
271 return 0;
272}
273
274/**
275 * snd_dma_alloc_pages_fallback - allocate the buffer area according to the given type with fallback
276 * @type: the DMA buffer type
277 * @device: the device pointer
278 * @size: the buffer size to allocate
279 * @dmab: buffer allocation record to store the allocated data
280 *
281 * Calls the memory-allocator function for the corresponding
282 * buffer type. When no space is left, this function reduces the size and
283 * tries to allocate again. The size actually allocated is stored in
284 * res_size argument.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100285 *
286 * Return: Zero if the buffer with the given size is allocated successfully,
287 * otherwise a negative value on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 */
289int snd_dma_alloc_pages_fallback(int type, struct device *device, size_t size,
290 struct snd_dma_buffer *dmab)
291{
292 int err;
293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 while ((err = snd_dma_alloc_pages(type, device, size, dmab)) < 0) {
Takashi Iwai4e184f82008-07-30 15:13:33 +0200295 size_t aligned_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 if (err != -ENOMEM)
297 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 if (size <= PAGE_SIZE)
299 return -ENOMEM;
Takashi Iwai4e184f82008-07-30 15:13:33 +0200300 aligned_size = PAGE_SIZE << get_order(size);
301 if (size != aligned_size)
302 size = aligned_size;
303 else
304 size >>= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 }
306 if (! dmab->area)
307 return -ENOMEM;
308 return 0;
309}
310
311
312/**
313 * snd_dma_free_pages - release the allocated buffer
314 * @dmab: the buffer allocation record to release
315 *
316 * Releases the allocated buffer via snd_dma_alloc_pages().
317 */
318void snd_dma_free_pages(struct snd_dma_buffer *dmab)
319{
320 switch (dmab->dev.type) {
321 case SNDRV_DMA_TYPE_CONTINUOUS:
322 snd_free_pages(dmab->area, dmab->bytes);
323 break;
Takashi Iwai8f115512007-07-26 18:59:36 +0200324#ifdef CONFIG_HAS_DMA
Takashi Iwaia5606f82013-10-24 14:25:32 +0200325#ifdef CONFIG_GENERIC_ALLOCATOR
Nicolin Chen05503212013-10-23 11:47:43 +0800326 case SNDRV_DMA_TYPE_DEV_IRAM:
327 snd_free_dev_iram(dmab);
328 break;
Takashi Iwaia5606f82013-10-24 14:25:32 +0200329#endif /* CONFIG_GENERIC_ALLOCATOR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 case SNDRV_DMA_TYPE_DEV:
331 snd_free_dev_pages(dmab->dev.dev, dmab->bytes, dmab->area, dmab->addr);
332 break;
Takashi Iwaicc6a8ac2008-06-17 16:39:06 +0200333#endif
334#ifdef CONFIG_SND_DMA_SGBUF
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 case SNDRV_DMA_TYPE_DEV_SG:
336 snd_free_sgbuf_pages(dmab);
337 break;
Takashi Iwai8f115512007-07-26 18:59:36 +0200338#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 default:
340 printk(KERN_ERR "snd-malloc: invalid device type %d\n", dmab->dev.type);
341 }
342}
343
344
345/**
346 * snd_dma_get_reserved - get the reserved buffer for the given device
347 * @dmab: the buffer allocation record to store
348 * @id: the buffer id
349 *
350 * Looks for the reserved-buffer list and re-uses if the same buffer
351 * is found in the list. When the buffer is found, it's removed from the free list.
352 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100353 * Return: The size of buffer if the buffer is found, or zero if not found.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 */
355size_t snd_dma_get_reserved_buf(struct snd_dma_buffer *dmab, unsigned int id)
356{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 struct snd_mem_list *mem;
358
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200359 if (WARN_ON(!dmab))
360 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100362 mutex_lock(&list_mutex);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200363 list_for_each_entry(mem, &mem_list_head, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 if (mem->id == id &&
Takashi Iwaib6a96912005-05-30 18:27:03 +0200365 (mem->buffer.dev.dev == NULL || dmab->dev.dev == NULL ||
366 ! memcmp(&mem->buffer.dev, &dmab->dev, sizeof(dmab->dev)))) {
367 struct device *dev = dmab->dev.dev;
Johannes Berg9244b2c2006-10-05 16:02:22 +0200368 list_del(&mem->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 *dmab = mem->buffer;
Takashi Iwaib6a96912005-05-30 18:27:03 +0200370 if (dmab->dev.dev == NULL)
371 dmab->dev.dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 kfree(mem);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100373 mutex_unlock(&list_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 return dmab->bytes;
375 }
376 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100377 mutex_unlock(&list_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 return 0;
379}
380
381/**
382 * snd_dma_reserve_buf - reserve the buffer
383 * @dmab: the buffer to reserve
384 * @id: the buffer id
385 *
386 * Reserves the given buffer as a reserved buffer.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100387 *
388 * Return: Zero if successful, or a negative code on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 */
390int snd_dma_reserve_buf(struct snd_dma_buffer *dmab, unsigned int id)
391{
392 struct snd_mem_list *mem;
393
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200394 if (WARN_ON(!dmab))
395 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 mem = kmalloc(sizeof(*mem), GFP_KERNEL);
397 if (! mem)
398 return -ENOMEM;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100399 mutex_lock(&list_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 mem->buffer = *dmab;
401 mem->id = id;
402 list_add_tail(&mem->list, &mem_list_head);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100403 mutex_unlock(&list_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 return 0;
405}
406
407/*
408 * purge all reserved buffers
409 */
410static void free_all_reserved_pages(void)
411{
412 struct list_head *p;
413 struct snd_mem_list *mem;
414
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100415 mutex_lock(&list_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 while (! list_empty(&mem_list_head)) {
417 p = mem_list_head.next;
418 mem = list_entry(p, struct snd_mem_list, list);
419 list_del(p);
420 snd_dma_free_pages(&mem->buffer);
421 kfree(mem);
422 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100423 mutex_unlock(&list_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424}
425
426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427#ifdef CONFIG_PROC_FS
428/*
429 * proc file interface
430 */
Takashi Iwaib6a96912005-05-30 18:27:03 +0200431#define SND_MEM_PROC_FILE "driver/snd-page-alloc"
Clemens Ladischa53fc182005-08-11 15:59:17 +0200432static struct proc_dir_entry *snd_mem_proc;
Takashi Iwaib6a96912005-05-30 18:27:03 +0200433
Takashi Iwaiccec6e22007-09-17 21:55:10 +0200434static int snd_mem_proc_read(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 long pages = snd_allocated_pages >> (PAGE_SHIFT-12);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 struct snd_mem_list *mem;
438 int devno;
David S. Miller759ee812008-08-27 00:33:26 -0700439 static char *types[] = { "UNKNOWN", "CONT", "DEV", "DEV-SG" };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100441 mutex_lock(&list_mutex);
Takashi Iwaiccec6e22007-09-17 21:55:10 +0200442 seq_printf(seq, "pages : %li bytes (%li pages per %likB)\n",
443 pages * PAGE_SIZE, pages, PAGE_SIZE / 1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 devno = 0;
Johannes Berg9244b2c2006-10-05 16:02:22 +0200445 list_for_each_entry(mem, &mem_list_head, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 devno++;
Takashi Iwaiccec6e22007-09-17 21:55:10 +0200447 seq_printf(seq, "buffer %d : ID %08x : type %s\n",
448 devno, mem->id, types[mem->buffer.dev.type]);
449 seq_printf(seq, " addr = 0x%lx, size = %d bytes\n",
450 (unsigned long)mem->buffer.addr,
451 (int)mem->buffer.bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100453 mutex_unlock(&list_mutex);
Takashi Iwaiccec6e22007-09-17 21:55:10 +0200454 return 0;
455}
456
457static int snd_mem_proc_open(struct inode *inode, struct file *file)
458{
459 return single_open(file, snd_mem_proc_read, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460}
Takashi Iwaib6a96912005-05-30 18:27:03 +0200461
462/* FIXME: for pci only - other bus? */
463#ifdef CONFIG_PCI
464#define gettoken(bufp) strsep(bufp, " \t\n")
465
Takashi Iwaiccec6e22007-09-17 21:55:10 +0200466static ssize_t snd_mem_proc_write(struct file *file, const char __user * buffer,
467 size_t count, loff_t * ppos)
Takashi Iwaib6a96912005-05-30 18:27:03 +0200468{
469 char buf[128];
470 char *token, *p;
471
Takashi Iwaiccec6e22007-09-17 21:55:10 +0200472 if (count > sizeof(buf) - 1)
473 return -EINVAL;
Takashi Iwaib6a96912005-05-30 18:27:03 +0200474 if (copy_from_user(buf, buffer, count))
475 return -EFAULT;
Takashi Iwaiccec6e22007-09-17 21:55:10 +0200476 buf[count] = '\0';
Takashi Iwaib6a96912005-05-30 18:27:03 +0200477
478 p = buf;
479 token = gettoken(&p);
480 if (! token || *token == '#')
Takashi Iwaiccec6e22007-09-17 21:55:10 +0200481 return count;
Takashi Iwaib6a96912005-05-30 18:27:03 +0200482 if (strcmp(token, "add") == 0) {
483 char *endp;
484 int vendor, device, size, buffers;
485 long mask;
486 int i, alloced;
487 struct pci_dev *pci;
488
489 if ((token = gettoken(&p)) == NULL ||
490 (vendor = simple_strtol(token, NULL, 0)) <= 0 ||
491 (token = gettoken(&p)) == NULL ||
492 (device = simple_strtol(token, NULL, 0)) <= 0 ||
493 (token = gettoken(&p)) == NULL ||
494 (mask = simple_strtol(token, NULL, 0)) < 0 ||
495 (token = gettoken(&p)) == NULL ||
496 (size = memparse(token, &endp)) < 64*1024 ||
497 size > 16*1024*1024 /* too big */ ||
498 (token = gettoken(&p)) == NULL ||
499 (buffers = simple_strtol(token, NULL, 0)) <= 0 ||
500 buffers > 4) {
501 printk(KERN_ERR "snd-page-alloc: invalid proc write format\n");
Takashi Iwaiccec6e22007-09-17 21:55:10 +0200502 return count;
Takashi Iwaib6a96912005-05-30 18:27:03 +0200503 }
504 vendor &= 0xffff;
505 device &= 0xffff;
506
507 alloced = 0;
508 pci = NULL;
Jiri Slaby0dd119f2005-09-07 14:28:33 +0200509 while ((pci = pci_get_device(vendor, device, pci)) != NULL) {
Takashi Iwaib6a96912005-05-30 18:27:03 +0200510 if (mask > 0 && mask < 0xffffffff) {
511 if (pci_set_dma_mask(pci, mask) < 0 ||
512 pci_set_consistent_dma_mask(pci, mask) < 0) {
513 printk(KERN_ERR "snd-page-alloc: cannot set DMA mask %lx for pci %04x:%04x\n", mask, vendor, device);
Julia Lawalldf1deb62007-11-28 11:58:56 +0100514 pci_dev_put(pci);
Takashi Iwaiccec6e22007-09-17 21:55:10 +0200515 return count;
Takashi Iwaib6a96912005-05-30 18:27:03 +0200516 }
517 }
518 for (i = 0; i < buffers; i++) {
519 struct snd_dma_buffer dmab;
520 memset(&dmab, 0, sizeof(dmab));
521 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
522 size, &dmab) < 0) {
523 printk(KERN_ERR "snd-page-alloc: cannot allocate buffer pages (size = %d)\n", size);
Jiri Slaby0dd119f2005-09-07 14:28:33 +0200524 pci_dev_put(pci);
Takashi Iwaiccec6e22007-09-17 21:55:10 +0200525 return count;
Takashi Iwaib6a96912005-05-30 18:27:03 +0200526 }
527 snd_dma_reserve_buf(&dmab, snd_dma_pci_buf_id(pci));
528 }
529 alloced++;
530 }
531 if (! alloced) {
532 for (i = 0; i < buffers; i++) {
533 struct snd_dma_buffer dmab;
534 memset(&dmab, 0, sizeof(dmab));
535 /* FIXME: We can allocate only in ZONE_DMA
536 * without a device pointer!
537 */
538 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, NULL,
539 size, &dmab) < 0) {
540 printk(KERN_ERR "snd-page-alloc: cannot allocate buffer pages (size = %d)\n", size);
541 break;
542 }
543 snd_dma_reserve_buf(&dmab, (unsigned int)((vendor << 16) | device));
544 }
545 }
546 } else if (strcmp(token, "erase") == 0)
547 /* FIXME: need for releasing each buffer chunk? */
548 free_all_reserved_pages();
549 else
550 printk(KERN_ERR "snd-page-alloc: invalid proc cmd\n");
Takashi Iwaiccec6e22007-09-17 21:55:10 +0200551 return count;
Takashi Iwaib6a96912005-05-30 18:27:03 +0200552}
553#endif /* CONFIG_PCI */
Takashi Iwaiccec6e22007-09-17 21:55:10 +0200554
555static const struct file_operations snd_mem_proc_fops = {
556 .owner = THIS_MODULE,
557 .open = snd_mem_proc_open,
558 .read = seq_read,
559#ifdef CONFIG_PCI
560 .write = snd_mem_proc_write,
561#endif
562 .llseek = seq_lseek,
563 .release = single_release,
564};
565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566#endif /* CONFIG_PROC_FS */
567
568/*
569 * module entry
570 */
571
572static int __init snd_mem_init(void)
573{
574#ifdef CONFIG_PROC_FS
Denis V. Lunev7bf4e6d2008-04-29 01:02:13 -0700575 snd_mem_proc = proc_create(SND_MEM_PROC_FILE, 0644, NULL,
576 &snd_mem_proc_fops);
Takashi Iwaib6a96912005-05-30 18:27:03 +0200577#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 return 0;
579}
580
581static void __exit snd_mem_exit(void)
582{
Takashi Iwaie0be4d32005-08-23 11:11:03 +0200583 remove_proc_entry(SND_MEM_PROC_FILE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 free_all_reserved_pages();
585 if (snd_allocated_pages > 0)
586 printk(KERN_ERR "snd-malloc: Memory leak? pages not freed = %li\n", snd_allocated_pages);
587}
588
589
590module_init(snd_mem_init)
591module_exit(snd_mem_exit)
592
593
594/*
595 * exports
596 */
597EXPORT_SYMBOL(snd_dma_alloc_pages);
598EXPORT_SYMBOL(snd_dma_alloc_pages_fallback);
599EXPORT_SYMBOL(snd_dma_free_pages);
600
601EXPORT_SYMBOL(snd_dma_get_reserved_buf);
602EXPORT_SYMBOL(snd_dma_reserve_buf);
603
604EXPORT_SYMBOL(snd_malloc_pages);
605EXPORT_SYMBOL(snd_free_pages);