1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
|
--- a/alsa-kernel/core/memalloc.c
+++ b/alsa-kernel/core/memalloc.c
@@ -1,3 +1,4 @@
+#include "memalloc.inc"
/*
* Copyright (c) by Jaroslav Kysela <perex@suse.cz>
* Takashi Iwai <tiwai@suse.de>
@@ -27,6 +28,7 @@
#include <linux/pci.h>
#include <linux/slab.h>
#include <linux/mm.h>
+#include <linux/seq_file.h>
#include <asm/uaccess.h>
#include <linux/dma-mapping.h>
#include <linux/moduleparam.h>
@@ -82,7 +84,50 @@ struct snd_mem_list {
* Hacks
*/
-#if defined(__i386__)
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 5)
+static void *snd_dma_alloc_coherent1(struct device *dev, size_t size,
+ dma_addr_t *dma_handle, int flags)
+{
+ if (dev)
+ return dma_alloc_coherent(dev, size, dma_handle, flags);
+ else /* FIXME: dma_alloc_coherent does't always accept dev=NULL */
+ return pci_alloc_consistent(NULL, size, dma_handle);
+}
+
+static void snd_dma_free_coherent1(struct device *dev, size_t size, void *dma_addr,
+ dma_addr_t dma_handle)
+{
+ if (dev)
+ return dma_free_coherent(dev, size, dma_addr, dma_handle);
+ else
+ return pci_free_consistent(NULL, size, dma_addr, dma_handle);
+}
+
+#undef dma_alloc_coherent
+#define dma_alloc_coherent snd_dma_alloc_coherent1
+#undef dma_free_coherent
+#define dma_free_coherent snd_dma_free_coherent1
+#endif /* < 2.6.5 */
+
+#else
+
+/* for 2.2/2.4 kernels */
+#if defined(CONFIG_PCI) || defined(CONFIG_ISA) || defined(__i386__)
+#define dma_alloc_coherent(dev,size,addr,flags) pci_alloc_consistent((struct pci_dev *)(dev),size,addr)
+#define dma_free_coherent(dev,size,ptr,addr) pci_free_consistent((struct pci_dev *)(dev),size,ptr,addr)
+#elif CONFIG_SBUS
+#define dma_alloc_coherent(dev,size,addr,flags) sbus_alloc_consistent((struct sbus_dev *)(dev),size,addr)
+#define dma_free_coherent(dev,size,ptr,addr) sbus_free_consistent((struct sbus_dev *)(dev),size,ptr,addr)
+#else
+#error "Need a bus for dma_alloc_coherent()"
+#endif
+
+#endif /* >= 2.6.0 */
+
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 0)
+#if defined(__i386__) || defined(__ppc__) || defined(__x86_64__)
+
/*
* A hack to allocate large buffers via dma_alloc_coherent()
*
@@ -99,6 +144,30 @@ struct snd_mem_list {
* so dma_mask doesn't have to be messed with.
*/
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
+#define VALID_DMA_MASK(dev) (dev)->dma_mask
+#define GET_DMA_MASK(dev) *(dev)->dma_mask
+#define SET_DMA_MASK(dev,val) (*(dev)->dma_mask = (val))
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 5)
+#define GET_COHERENT_DMA_MASK(dev) (dev)->coherent_dma_mask
+#define SET_COHERENT_DMA_MASK(dev,val) ((dev)->coherent_dma_mask = (val))
+#else
+#define GET_COHERENT_DMA_MASK(dev) 0 /* dummy */
+#define SET_COHERENT_DMA_MASK(dev,val)
+#endif
+#else /* 2.4.x */
+#define VALID_DMA_MASK(dev) 1
+#define GET_DMA_MASK(dev) ((struct pci_dev *)(dev))->dma_mask
+#define SET_DMA_MASK(dev,val) (((struct pci_dev *)(dev))->dma_mask = (val))
+#ifdef CONFIG_HAVE_PCI_CONSISTENT_DMA_MASK
+#define GET_COHERENT_DMA_MASK(dev) ((struct pci_dev *)(dev))->consistent_dma_mask
+#define SET_COHERENT_DMA_MASK(dev,val) (((struct pci_dev *)(dev))->consistent_dma_mask = (val))
+#else
+#define GET_COHERENT_DMA_MASK(dev) 0 /* dummy */
+#define SET_COHERENT_DMA_MASK(dev,val)
+#endif
+#endif
+
static void *snd_dma_hack_alloc_coherent(struct device *dev, size_t size,
dma_addr_t *dma_handle,
gfp_t flags)
@@ -106,15 +175,15 @@ static void *snd_dma_hack_alloc_coherent
void *ret;
u64 dma_mask, coherent_dma_mask;
- if (dev == NULL || !dev->dma_mask)
+ if (dev == NULL || !VALID_DMA_MASK(dev))
return dma_alloc_coherent(dev, size, dma_handle, flags);
- dma_mask = *dev->dma_mask;
- coherent_dma_mask = dev->coherent_dma_mask;
- *dev->dma_mask = 0xffffffff; /* do without masking */
- dev->coherent_dma_mask = 0xffffffff; /* do without masking */
+ dma_mask = GET_DMA_MASK(dev);
+ coherent_dma_mask = GET_COHERENT_DMA_MASK(dev);
+ SET_DMA_MASK(dev, 0xffffffff); /* do without masking */
+ SET_COHERENT_DMA_MASK(dev, 0xffffffff); /* do without masking */
ret = dma_alloc_coherent(dev, size, dma_handle, flags);
- *dev->dma_mask = dma_mask; /* restore */
- dev->coherent_dma_mask = coherent_dma_mask; /* restore */
+ SET_DMA_MASK(dev, dma_mask); /* restore */
+ SET_COHERENT_DMA_MASK(dev, coherent_dma_mask) /* restore */;
if (ret) {
/* obtained address is out of range? */
if (((unsigned long)*dma_handle + size - 1) & ~dma_mask) {
@@ -139,6 +208,8 @@ static void *snd_dma_hack_alloc_coherent
#define dma_alloc_coherent snd_dma_hack_alloc_coherent
#endif /* arch */
+#endif /* >= 2.4.0 */
+
/*
*
@@ -158,6 +229,24 @@ static inline void dec_snd_pages(int ord
snd_allocated_pages -= 1 << order;
}
+static void mark_pages(struct page *page, int order)
+{
+#if ! defined(__arm__) && LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 16)
+ struct page *last_page = page + (1 << order);
+ while (page < last_page)
+ SetPageReserved(page++);
+#endif
+}
+
+static void unmark_pages(struct page *page, int order)
+{
+#if ! defined(__arm__) && LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 16)
+ struct page *last_page = page + (1 << order);
+ while (page < last_page)
+ ClearPageReserved(page++);
+#endif
+}
+
/**
* snd_malloc_pages - allocate pages with the given size
* @size: the size to allocate in bytes
@@ -176,8 +265,10 @@ void *snd_malloc_pages(size_t size, gfp_
snd_assert(gfp_flags != 0, return NULL);
gfp_flags |= __GFP_COMP; /* compound page lets parts be mapped */
pg = get_order(size);
- if ((res = (void *) __get_free_pages(gfp_flags, pg)) != NULL)
+ if ((res = (void *) __get_free_pages(gfp_flags, pg)) != NULL) {
+ mark_pages(virt_to_page(res), pg);
inc_snd_pages(pg);
+ }
return res;
}
@@ -196,6 +287,7 @@ void snd_free_pages(void *ptr, size_t si
return;
pg = get_order(size);
dec_snd_pages(pg);
+ unmark_pages(virt_to_page(ptr), pg);
free_pages((unsigned long) ptr, pg);
}
@@ -220,8 +312,10 @@ static void *snd_malloc_dev_pages(struct
| __GFP_NORETRY /* don't trigger OOM-killer */
| __GFP_NOWARN; /* no stack trace print - this call is non-critical */
res = dma_alloc_coherent(dev, PAGE_SIZE << pg, dma, gfp_flags);
- if (res != NULL)
+ if (res != NULL) {
+ mark_pages(virt_to_page(res), pg);
inc_snd_pages(pg);
+ }
return res;
}
@@ -236,6 +330,7 @@ static void snd_free_dev_pages(struct de
return;
pg = get_order(size);
dec_snd_pages(pg);
+ unmark_pages(virt_to_page(ptr), pg);
dma_free_coherent(dev, PAGE_SIZE << pg, ptr, dma);
}
@@ -481,53 +576,55 @@ static void free_all_reserved_pages(void
#define SND_MEM_PROC_FILE "driver/snd-page-alloc"
static struct proc_dir_entry *snd_mem_proc;
-static int snd_mem_proc_read(char *page, char **start, off_t off,
- int count, int *eof, void *data)
+static int snd_mem_proc_read(struct seq_file *seq, void *offset)
{
- int len = 0;
long pages = snd_allocated_pages >> (PAGE_SHIFT-12);
struct snd_mem_list *mem;
int devno;
static char *types[] = { "UNKNOWN", "CONT", "DEV", "DEV-SG", "SBUS" };
mutex_lock(&list_mutex);
- len += snprintf(page + len, count - len,
- "pages : %li bytes (%li pages per %likB)\n",
- pages * PAGE_SIZE, pages, PAGE_SIZE / 1024);
+ seq_printf(seq, "pages : %li bytes (%li pages per %likB)\n",
+ pages * PAGE_SIZE, pages, PAGE_SIZE / 1024);
devno = 0;
list_for_each_entry(mem, &mem_list_head, list) {
devno++;
- len += snprintf(page + len, count - len,
- "buffer %d : ID %08x : type %s\n",
- devno, mem->id, types[mem->buffer.dev.type]);
- len += snprintf(page + len, count - len,
- " addr = 0x%lx, size = %d bytes\n",
- (unsigned long)mem->buffer.addr, (int)mem->buffer.bytes);
+ seq_printf(seq, "buffer %d : ID %08x : type %s\n",
+ devno, mem->id, types[mem->buffer.dev.type]);
+ seq_printf(seq, " addr = 0x%lx, size = %d bytes\n",
+ (unsigned long)mem->buffer.addr,
+ (int)mem->buffer.bytes);
}
mutex_unlock(&list_mutex);
- return len;
+ return 0;
+}
+
+static int snd_mem_proc_open(struct inode *inode, struct file *file)
+{
+ return single_open(file, snd_mem_proc_read, NULL);
}
/* FIXME: for pci only - other bus? */
#ifdef CONFIG_PCI
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 0)
#define gettoken(bufp) strsep(bufp, " \t\n")
-static int snd_mem_proc_write(struct file *file, const char __user *buffer,
- unsigned long count, void *data)
+static ssize_t snd_mem_proc_write(struct file *file, const char __user * buffer,
+ size_t count, loff_t * ppos)
{
char buf[128];
char *token, *p;
- if (count > ARRAY_SIZE(buf) - 1)
- count = ARRAY_SIZE(buf) - 1;
+ if (count > sizeof(buf) - 1)
+ return -EINVAL;
if (copy_from_user(buf, buffer, count))
return -EFAULT;
- buf[ARRAY_SIZE(buf) - 1] = '\0';
+ buf[count] = '\0';
p = buf;
token = gettoken(&p);
if (! token || *token == '#')
- return (int)count;
+ return count;
if (strcmp(token, "add") == 0) {
char *endp;
int vendor, device, size, buffers;
@@ -548,7 +645,7 @@ static int snd_mem_proc_write(struct fil
(buffers = simple_strtol(token, NULL, 0)) <= 0 ||
buffers > 4) {
printk(KERN_ERR "snd-page-alloc: invalid proc write format\n");
- return (int)count;
+ return count;
}
vendor &= 0xffff;
device &= 0xffff;
@@ -560,7 +657,7 @@ static int snd_mem_proc_write(struct fil
if (pci_set_dma_mask(pci, mask) < 0 ||
pci_set_consistent_dma_mask(pci, mask) < 0) {
printk(KERN_ERR "snd-page-alloc: cannot set DMA mask %lx for pci %04x:%04x\n", mask, vendor, device);
- return (int)count;
+ return count;
}
}
for (i = 0; i < buffers; i++) {
@@ -570,7 +667,7 @@ static int snd_mem_proc_write(struct fil
size, &dmab) < 0) {
printk(KERN_ERR "snd-page-alloc: cannot allocate buffer pages (size = %d)\n", size);
pci_dev_put(pci);
- return (int)count;
+ return count;
}
snd_dma_reserve_buf(&dmab, snd_dma_pci_buf_id(pci));
}
@@ -596,9 +693,22 @@ static int snd_mem_proc_write(struct fil
free_all_reserved_pages();
else
printk(KERN_ERR "snd-page-alloc: invalid proc cmd\n");
- return (int)count;
+ return count;
}
+#endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 0) */
#endif /* CONFIG_PCI */
+
+static const struct file_operations snd_mem_proc_fops = {
+ .owner = THIS_MODULE,
+ .open = snd_mem_proc_open,
+ .read = seq_read,
+#ifdef CONFIG_PCI
+ .write = snd_mem_proc_write,
+#endif
+ .llseek = seq_lseek,
+ .release = single_release,
+};
+
#endif /* CONFIG_PROC_FS */
/*
@@ -609,12 +719,8 @@ static int __init snd_mem_init(void)
{
#ifdef CONFIG_PROC_FS
snd_mem_proc = create_proc_entry(SND_MEM_PROC_FILE, 0644, NULL);
- if (snd_mem_proc) {
- snd_mem_proc->read_proc = snd_mem_proc_read;
-#ifdef CONFIG_PCI
- snd_mem_proc->write_proc = snd_mem_proc_write;
-#endif
- }
+ if (snd_mem_proc)
+ snd_mem_proc->proc_fops = &snd_mem_proc_fops;
#endif
return 0;
}
@@ -644,3 +750,5 @@ EXPORT_SYMBOL(snd_dma_reserve_buf);
EXPORT_SYMBOL(snd_malloc_pages);
EXPORT_SYMBOL(snd_free_pages);
+
+#include "memalloc.inc1"
|