ALSA: PCM: Fix possible memory leaks in the error path
[linux-2.6-block.git] / sound / pci / emu10k1 / memory.c
CommitLineData
1da177e4 1/*
c1017a4c 2 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
1da177e4
LT
3 * Copyright (c) by Takashi Iwai <tiwai@suse.de>
4 *
5 * EMU10K1 memory page allocation (PTB area)
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
1da177e4 24#include <linux/pci.h>
5a0e3ad6 25#include <linux/gfp.h>
1da177e4 26#include <linux/time.h>
62932df8 27#include <linux/mutex.h>
d81a6d71 28#include <linux/export.h>
62932df8 29
1da177e4
LT
30#include <sound/core.h>
31#include <sound/emu10k1.h>
32
33/* page arguments of these two macros are Emu page (4096 bytes), not like
34 * aligned pages in others
35 */
36#define __set_ptb_entry(emu,page,addr) \
37 (((u32 *)(emu)->ptb_pages.area)[page] = cpu_to_le32(((addr) << 1) | (page)))
38
39#define UNIT_PAGES (PAGE_SIZE / EMUPAGESIZE)
40#define MAX_ALIGN_PAGES (MAXPAGES / UNIT_PAGES)
41/* get aligned page from offset address */
42#define get_aligned_page(offset) ((offset) >> PAGE_SHIFT)
43/* get offset address from aligned page */
44#define aligned_page_offset(page) ((page) << PAGE_SHIFT)
45
46#if PAGE_SIZE == 4096
47/* page size == EMUPAGESIZE */
48/* fill PTB entrie(s) corresponding to page with addr */
49#define set_ptb_entry(emu,page,addr) __set_ptb_entry(emu,page,addr)
50/* fill PTB entrie(s) corresponding to page with silence pointer */
51#define set_silent_ptb(emu,page) __set_ptb_entry(emu,page,emu->silent_page.addr)
52#else
53/* fill PTB entries -- we need to fill UNIT_PAGES entries */
eb4698f3 54static inline void set_ptb_entry(struct snd_emu10k1 *emu, int page, dma_addr_t addr)
1da177e4
LT
55{
56 int i;
57 page *= UNIT_PAGES;
58 for (i = 0; i < UNIT_PAGES; i++, page++) {
59 __set_ptb_entry(emu, page, addr);
60 addr += EMUPAGESIZE;
61 }
62}
eb4698f3 63static inline void set_silent_ptb(struct snd_emu10k1 *emu, int page)
1da177e4
LT
64{
65 int i;
66 page *= UNIT_PAGES;
67 for (i = 0; i < UNIT_PAGES; i++, page++)
68 /* do not increment ptr */
69 __set_ptb_entry(emu, page, emu->silent_page.addr);
70}
71#endif /* PAGE_SIZE */
72
73
74/*
75 */
eb4698f3
TI
76static int synth_alloc_pages(struct snd_emu10k1 *hw, struct snd_emu10k1_memblk *blk);
77static int synth_free_pages(struct snd_emu10k1 *hw, struct snd_emu10k1_memblk *blk);
1da177e4 78
eb4698f3 79#define get_emu10k1_memblk(l,member) list_entry(l, struct snd_emu10k1_memblk, member)
1da177e4
LT
80
81
82/* initialize emu10k1 part */
eb4698f3 83static void emu10k1_memblk_init(struct snd_emu10k1_memblk *blk)
1da177e4
LT
84{
85 blk->mapped_page = -1;
86 INIT_LIST_HEAD(&blk->mapped_link);
87 INIT_LIST_HEAD(&blk->mapped_order_link);
88 blk->map_locked = 0;
89
90 blk->first_page = get_aligned_page(blk->mem.offset);
91 blk->last_page = get_aligned_page(blk->mem.offset + blk->mem.size - 1);
92 blk->pages = blk->last_page - blk->first_page + 1;
93}
94
95/*
96 * search empty region on PTB with the given size
97 *
98 * if an empty region is found, return the page and store the next mapped block
99 * in nextp
100 * if not found, return a negative error code.
101 */
eb4698f3 102static int search_empty_map_area(struct snd_emu10k1 *emu, int npages, struct list_head **nextp)
1da177e4
LT
103{
104 int page = 0, found_page = -ENOMEM;
105 int max_size = npages;
106 int size;
107 struct list_head *candidate = &emu->mapped_link_head;
108 struct list_head *pos;
109
110 list_for_each (pos, &emu->mapped_link_head) {
eb4698f3 111 struct snd_emu10k1_memblk *blk = get_emu10k1_memblk(pos, mapped_link);
da3cec35
TI
112 if (blk->mapped_page < 0)
113 continue;
1da177e4
LT
114 size = blk->mapped_page - page;
115 if (size == npages) {
116 *nextp = pos;
117 return page;
118 }
119 else if (size > max_size) {
120 /* we look for the maximum empty hole */
121 max_size = size;
122 candidate = pos;
123 found_page = page;
124 }
125 page = blk->mapped_page + blk->pages;
126 }
127 size = MAX_ALIGN_PAGES - page;
128 if (size >= max_size) {
129 *nextp = pos;
130 return page;
131 }
132 *nextp = candidate;
133 return found_page;
134}
135
136/*
137 * map a memory block onto emu10k1's PTB
138 *
139 * call with memblk_lock held
140 */
eb4698f3 141static int map_memblk(struct snd_emu10k1 *emu, struct snd_emu10k1_memblk *blk)
1da177e4
LT
142{
143 int page, pg;
144 struct list_head *next;
145
146 page = search_empty_map_area(emu, blk->pages, &next);
147 if (page < 0) /* not found */
148 return page;
149 /* insert this block in the proper position of mapped list */
150 list_add_tail(&blk->mapped_link, next);
151 /* append this as a newest block in order list */
152 list_add_tail(&blk->mapped_order_link, &emu->mapped_order_link_head);
153 blk->mapped_page = page;
154 /* fill PTB */
155 for (pg = blk->first_page; pg <= blk->last_page; pg++) {
156 set_ptb_entry(emu, page, emu->page_addr_table[pg]);
157 page++;
158 }
159 return 0;
160}
161
162/*
163 * unmap the block
164 * return the size of resultant empty pages
165 *
166 * call with memblk_lock held
167 */
eb4698f3 168static int unmap_memblk(struct snd_emu10k1 *emu, struct snd_emu10k1_memblk *blk)
1da177e4
LT
169{
170 int start_page, end_page, mpage, pg;
171 struct list_head *p;
eb4698f3 172 struct snd_emu10k1_memblk *q;
1da177e4
LT
173
174 /* calculate the expected size of empty region */
175 if ((p = blk->mapped_link.prev) != &emu->mapped_link_head) {
176 q = get_emu10k1_memblk(p, mapped_link);
177 start_page = q->mapped_page + q->pages;
178 } else
179 start_page = 0;
180 if ((p = blk->mapped_link.next) != &emu->mapped_link_head) {
181 q = get_emu10k1_memblk(p, mapped_link);
182 end_page = q->mapped_page;
183 } else
184 end_page = MAX_ALIGN_PAGES;
185
186 /* remove links */
187 list_del(&blk->mapped_link);
188 list_del(&blk->mapped_order_link);
189 /* clear PTB */
190 mpage = blk->mapped_page;
191 for (pg = blk->first_page; pg <= blk->last_page; pg++) {
192 set_silent_ptb(emu, mpage);
193 mpage++;
194 }
195 blk->mapped_page = -1;
196 return end_page - start_page; /* return the new empty size */
197}
198
199/*
200 * search empty pages with the given size, and create a memory block
201 *
202 * unlike synth_alloc the memory block is aligned to the page start
203 */
eb4698f3
TI
204static struct snd_emu10k1_memblk *
205search_empty(struct snd_emu10k1 *emu, int size)
1da177e4
LT
206{
207 struct list_head *p;
eb4698f3 208 struct snd_emu10k1_memblk *blk;
1da177e4
LT
209 int page, psize;
210
211 psize = get_aligned_page(size + PAGE_SIZE -1);
212 page = 0;
213 list_for_each(p, &emu->memhdr->block) {
214 blk = get_emu10k1_memblk(p, mem.list);
215 if (page + psize <= blk->first_page)
216 goto __found_pages;
217 page = blk->last_page + 1;
218 }
219 if (page + psize > emu->max_cache_pages)
220 return NULL;
221
222__found_pages:
223 /* create a new memory block */
eb4698f3 224 blk = (struct snd_emu10k1_memblk *)__snd_util_memblk_new(emu->memhdr, psize << PAGE_SHIFT, p->prev);
1da177e4
LT
225 if (blk == NULL)
226 return NULL;
227 blk->mem.offset = aligned_page_offset(page); /* set aligned offset */
228 emu10k1_memblk_init(blk);
229 return blk;
230}
231
232
233/*
234 * check if the given pointer is valid for pages
235 */
eb4698f3 236static int is_valid_page(struct snd_emu10k1 *emu, dma_addr_t addr)
1da177e4
LT
237{
238 if (addr & ~emu->dma_mask) {
99b359ba 239 snd_printk(KERN_ERR "max memory size is 0x%lx (addr = 0x%lx)!!\n", emu->dma_mask, (unsigned long)addr);
1da177e4
LT
240 return 0;
241 }
242 if (addr & (EMUPAGESIZE-1)) {
99b359ba 243 snd_printk(KERN_ERR "page is not aligned\n");
1da177e4
LT
244 return 0;
245 }
246 return 1;
247}
248
249/*
250 * map the given memory block on PTB.
251 * if the block is already mapped, update the link order.
25985edc 252 * if no empty pages are found, tries to release unused memory blocks
1da177e4
LT
253 * and retry the mapping.
254 */
eb4698f3 255int snd_emu10k1_memblk_map(struct snd_emu10k1 *emu, struct snd_emu10k1_memblk *blk)
1da177e4
LT
256{
257 int err;
258 int size;
259 struct list_head *p, *nextp;
eb4698f3 260 struct snd_emu10k1_memblk *deleted;
1da177e4
LT
261 unsigned long flags;
262
263 spin_lock_irqsave(&emu->memblk_lock, flags);
264 if (blk->mapped_page >= 0) {
265 /* update order link */
266 list_del(&blk->mapped_order_link);
267 list_add_tail(&blk->mapped_order_link, &emu->mapped_order_link_head);
268 spin_unlock_irqrestore(&emu->memblk_lock, flags);
269 return 0;
270 }
271 if ((err = map_memblk(emu, blk)) < 0) {
272 /* no enough page - try to unmap some blocks */
273 /* starting from the oldest block */
274 p = emu->mapped_order_link_head.next;
275 for (; p != &emu->mapped_order_link_head; p = nextp) {
276 nextp = p->next;
277 deleted = get_emu10k1_memblk(p, mapped_order_link);
278 if (deleted->map_locked)
279 continue;
280 size = unmap_memblk(emu, deleted);
281 if (size >= blk->pages) {
282 /* ok the empty region is enough large */
283 err = map_memblk(emu, blk);
284 break;
285 }
286 }
287 }
288 spin_unlock_irqrestore(&emu->memblk_lock, flags);
289 return err;
290}
291
2dd31dee
TI
292EXPORT_SYMBOL(snd_emu10k1_memblk_map);
293
1da177e4
LT
294/*
295 * page allocation for DMA
296 */
eb4698f3
TI
297struct snd_util_memblk *
298snd_emu10k1_alloc_pages(struct snd_emu10k1 *emu, struct snd_pcm_substream *substream)
1da177e4 299{
eb4698f3 300 struct snd_pcm_runtime *runtime = substream->runtime;
eb4698f3
TI
301 struct snd_util_memhdr *hdr;
302 struct snd_emu10k1_memblk *blk;
1da177e4
LT
303 int page, err, idx;
304
da3cec35
TI
305 if (snd_BUG_ON(!emu))
306 return NULL;
307 if (snd_BUG_ON(runtime->dma_bytes <= 0 ||
308 runtime->dma_bytes >= MAXPAGES * EMUPAGESIZE))
309 return NULL;
1da177e4 310 hdr = emu->memhdr;
da3cec35
TI
311 if (snd_BUG_ON(!hdr))
312 return NULL;
1da177e4 313
56385a12
JK
314 idx = runtime->period_size >= runtime->buffer_size ?
315 (emu->delay_pcm_irq * 2) : 0;
62932df8 316 mutex_lock(&hdr->block_mutex);
56385a12 317 blk = search_empty(emu, runtime->dma_bytes + idx);
1da177e4 318 if (blk == NULL) {
62932df8 319 mutex_unlock(&hdr->block_mutex);
1da177e4
LT
320 return NULL;
321 }
322 /* fill buffer addresses but pointers are not stored so that
323 * snd_free_pci_page() is not called in in synth_free()
324 */
325 idx = 0;
326 for (page = blk->first_page; page <= blk->last_page; page++, idx++) {
77a23f26 327 unsigned long ofs = idx << PAGE_SHIFT;
1da177e4 328 dma_addr_t addr;
77a23f26 329 addr = snd_pcm_sgbuf_get_addr(substream, ofs);
1da177e4
LT
330 if (! is_valid_page(emu, addr)) {
331 printk(KERN_ERR "emu: failure page = %d\n", idx);
62932df8 332 mutex_unlock(&hdr->block_mutex);
1da177e4
LT
333 return NULL;
334 }
335 emu->page_addr_table[page] = addr;
336 emu->page_ptr_table[page] = NULL;
337 }
338
339 /* set PTB entries */
340 blk->map_locked = 1; /* do not unmap this block! */
341 err = snd_emu10k1_memblk_map(emu, blk);
342 if (err < 0) {
eb4698f3 343 __snd_util_mem_free(hdr, (struct snd_util_memblk *)blk);
62932df8 344 mutex_unlock(&hdr->block_mutex);
1da177e4
LT
345 return NULL;
346 }
62932df8 347 mutex_unlock(&hdr->block_mutex);
eb4698f3 348 return (struct snd_util_memblk *)blk;
1da177e4
LT
349}
350
351
352/*
353 * release DMA buffer from page table
354 */
eb4698f3 355int snd_emu10k1_free_pages(struct snd_emu10k1 *emu, struct snd_util_memblk *blk)
1da177e4 356{
da3cec35
TI
357 if (snd_BUG_ON(!emu || !blk))
358 return -EINVAL;
1da177e4
LT
359 return snd_emu10k1_synth_free(emu, blk);
360}
361
362
363/*
364 * memory allocation using multiple pages (for synth)
365 * Unlike the DMA allocation above, non-contiguous pages are assined.
366 */
367
368/*
369 * allocate a synth sample area
370 */
eb4698f3
TI
371struct snd_util_memblk *
372snd_emu10k1_synth_alloc(struct snd_emu10k1 *hw, unsigned int size)
1da177e4 373{
eb4698f3
TI
374 struct snd_emu10k1_memblk *blk;
375 struct snd_util_memhdr *hdr = hw->memhdr;
1da177e4 376
62932df8 377 mutex_lock(&hdr->block_mutex);
eb4698f3 378 blk = (struct snd_emu10k1_memblk *)__snd_util_mem_alloc(hdr, size);
1da177e4 379 if (blk == NULL) {
62932df8 380 mutex_unlock(&hdr->block_mutex);
1da177e4
LT
381 return NULL;
382 }
383 if (synth_alloc_pages(hw, blk)) {
eb4698f3 384 __snd_util_mem_free(hdr, (struct snd_util_memblk *)blk);
62932df8 385 mutex_unlock(&hdr->block_mutex);
1da177e4
LT
386 return NULL;
387 }
388 snd_emu10k1_memblk_map(hw, blk);
62932df8 389 mutex_unlock(&hdr->block_mutex);
eb4698f3 390 return (struct snd_util_memblk *)blk;
1da177e4
LT
391}
392
2dd31dee 393EXPORT_SYMBOL(snd_emu10k1_synth_alloc);
1da177e4
LT
394
395/*
396 * free a synth sample area
397 */
398int
eb4698f3 399snd_emu10k1_synth_free(struct snd_emu10k1 *emu, struct snd_util_memblk *memblk)
1da177e4 400{
eb4698f3
TI
401 struct snd_util_memhdr *hdr = emu->memhdr;
402 struct snd_emu10k1_memblk *blk = (struct snd_emu10k1_memblk *)memblk;
1da177e4
LT
403 unsigned long flags;
404
62932df8 405 mutex_lock(&hdr->block_mutex);
1da177e4
LT
406 spin_lock_irqsave(&emu->memblk_lock, flags);
407 if (blk->mapped_page >= 0)
408 unmap_memblk(emu, blk);
409 spin_unlock_irqrestore(&emu->memblk_lock, flags);
410 synth_free_pages(emu, blk);
411 __snd_util_mem_free(hdr, memblk);
62932df8 412 mutex_unlock(&hdr->block_mutex);
1da177e4
LT
413 return 0;
414}
415
2dd31dee 416EXPORT_SYMBOL(snd_emu10k1_synth_free);
1da177e4
LT
417
418/* check new allocation range */
eb4698f3
TI
419static void get_single_page_range(struct snd_util_memhdr *hdr,
420 struct snd_emu10k1_memblk *blk,
421 int *first_page_ret, int *last_page_ret)
1da177e4
LT
422{
423 struct list_head *p;
eb4698f3 424 struct snd_emu10k1_memblk *q;
1da177e4
LT
425 int first_page, last_page;
426 first_page = blk->first_page;
427 if ((p = blk->mem.list.prev) != &hdr->block) {
428 q = get_emu10k1_memblk(p, mem.list);
429 if (q->last_page == first_page)
430 first_page++; /* first page was already allocated */
431 }
432 last_page = blk->last_page;
433 if ((p = blk->mem.list.next) != &hdr->block) {
434 q = get_emu10k1_memblk(p, mem.list);
435 if (q->first_page == last_page)
436 last_page--; /* last page was already allocated */
437 }
438 *first_page_ret = first_page;
439 *last_page_ret = last_page;
440}
441
a5003fc0
TI
442/* release allocated pages */
443static void __synth_free_pages(struct snd_emu10k1 *emu, int first_page,
444 int last_page)
445{
446 int page;
447
448 for (page = first_page; page <= last_page; page++) {
449 free_page((unsigned long)emu->page_ptr_table[page]);
450 emu->page_addr_table[page] = 0;
451 emu->page_ptr_table[page] = NULL;
452 }
453}
454
1da177e4
LT
455/*
456 * allocate kernel pages
457 */
eb4698f3 458static int synth_alloc_pages(struct snd_emu10k1 *emu, struct snd_emu10k1_memblk *blk)
1da177e4
LT
459{
460 int page, first_page, last_page;
1da177e4
LT
461
462 emu10k1_memblk_init(blk);
463 get_single_page_range(emu->memhdr, blk, &first_page, &last_page);
464 /* allocate kernel pages */
465 for (page = first_page; page <= last_page; page++) {
a5003fc0
TI
466 /* first try to allocate from <4GB zone */
467 struct page *p = alloc_page(GFP_KERNEL | GFP_DMA32 |
468 __GFP_NOWARN);
9f515b68 469 if (!p || (page_to_pfn(p) & ~(emu->dma_mask >> PAGE_SHIFT))) {
28437305
TI
470 if (p)
471 __free_page(p);
a5003fc0 472 /* try to allocate from <16MB zone */
28437305 473 p = alloc_page(GFP_ATOMIC | GFP_DMA |
a5003fc0
TI
474 __GFP_NORETRY | /* no OOM-killer */
475 __GFP_NOWARN);
9f515b68 476 }
a5003fc0
TI
477 if (!p) {
478 __synth_free_pages(emu, first_page, page - 1);
479 return -ENOMEM;
1da177e4 480 }
a5003fc0
TI
481 emu->page_addr_table[page] = page_to_phys(p);
482 emu->page_ptr_table[page] = page_address(p);
1da177e4
LT
483 }
484 return 0;
1da177e4
LT
485}
486
487/*
488 * free pages
489 */
eb4698f3 490static int synth_free_pages(struct snd_emu10k1 *emu, struct snd_emu10k1_memblk *blk)
1da177e4 491{
a5003fc0 492 int first_page, last_page;
1da177e4
LT
493
494 get_single_page_range(emu->memhdr, blk, &first_page, &last_page);
a5003fc0 495 __synth_free_pages(emu, first_page, last_page);
1da177e4
LT
496 return 0;
497}
498
499/* calculate buffer pointer from offset address */
eb4698f3 500static inline void *offset_ptr(struct snd_emu10k1 *emu, int page, int offset)
1da177e4
LT
501{
502 char *ptr;
da3cec35
TI
503 if (snd_BUG_ON(page < 0 || page >= emu->max_cache_pages))
504 return NULL;
1da177e4
LT
505 ptr = emu->page_ptr_table[page];
506 if (! ptr) {
99b359ba 507 printk(KERN_ERR "emu10k1: access to NULL ptr: page = %d\n", page);
1da177e4
LT
508 return NULL;
509 }
510 ptr += offset & (PAGE_SIZE - 1);
511 return (void*)ptr;
512}
513
514/*
515 * bzero(blk + offset, size)
516 */
eb4698f3
TI
517int snd_emu10k1_synth_bzero(struct snd_emu10k1 *emu, struct snd_util_memblk *blk,
518 int offset, int size)
1da177e4
LT
519{
520 int page, nextofs, end_offset, temp, temp1;
521 void *ptr;
eb4698f3 522 struct snd_emu10k1_memblk *p = (struct snd_emu10k1_memblk *)blk;
1da177e4
LT
523
524 offset += blk->offset & (PAGE_SIZE - 1);
525 end_offset = offset + size;
526 page = get_aligned_page(offset);
527 do {
528 nextofs = aligned_page_offset(page + 1);
529 temp = nextofs - offset;
530 temp1 = end_offset - offset;
531 if (temp1 < temp)
532 temp = temp1;
533 ptr = offset_ptr(emu, page + p->first_page, offset);
534 if (ptr)
535 memset(ptr, 0, temp);
536 offset = nextofs;
537 page++;
538 } while (offset < end_offset);
539 return 0;
540}
541
2dd31dee
TI
542EXPORT_SYMBOL(snd_emu10k1_synth_bzero);
543
1da177e4
LT
544/*
545 * copy_from_user(blk + offset, data, size)
546 */
eb4698f3
TI
547int snd_emu10k1_synth_copy_from_user(struct snd_emu10k1 *emu, struct snd_util_memblk *blk,
548 int offset, const char __user *data, int size)
1da177e4
LT
549{
550 int page, nextofs, end_offset, temp, temp1;
551 void *ptr;
eb4698f3 552 struct snd_emu10k1_memblk *p = (struct snd_emu10k1_memblk *)blk;
1da177e4
LT
553
554 offset += blk->offset & (PAGE_SIZE - 1);
555 end_offset = offset + size;
556 page = get_aligned_page(offset);
557 do {
558 nextofs = aligned_page_offset(page + 1);
559 temp = nextofs - offset;
560 temp1 = end_offset - offset;
561 if (temp1 < temp)
562 temp = temp1;
563 ptr = offset_ptr(emu, page + p->first_page, offset);
564 if (ptr && copy_from_user(ptr, data, temp))
565 return -EFAULT;
566 offset = nextofs;
567 data += temp;
568 page++;
569 } while (offset < end_offset);
570 return 0;
571}
2dd31dee
TI
572
573EXPORT_SYMBOL(snd_emu10k1_synth_copy_from_user);