[ALSA] semaphore -> mutex (ISA part)
[deliverable/linux.git] / sound / pci / trident / trident_memory.c
CommitLineData
1da177e4
LT
1/*
2 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
3 * Copyright (c) by Takashi Iwai <tiwai@suse.de>
4 * Copyright (c) by Scott McNab <sdm@fractalgraphics.com.au>
5 *
6 * Trident 4DWave-NX memory page allocation (TLB area)
7 * Trident chip can handle only 16MByte of the memory at the same time.
8 *
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
24 */
25
26#include <sound/driver.h>
27#include <asm/io.h>
28#include <linux/pci.h>
29#include <linux/time.h>
30#include <sound/core.h>
31#include <sound/trident.h>
32
33/* page arguments of these two macros are Trident page (4096 bytes), not like
34 * aligned pages in others
35 */
36#define __set_tlb_bus(trident,page,ptr,addr) \
37 do { (trident)->tlb.entries[page] = cpu_to_le32((addr) & ~(SNDRV_TRIDENT_PAGE_SIZE-1)); \
38 (trident)->tlb.shadow_entries[page] = (ptr); } while (0)
39#define __tlb_to_ptr(trident,page) \
40 (void*)((trident)->tlb.shadow_entries[page])
41#define __tlb_to_addr(trident,page) \
42 (dma_addr_t)le32_to_cpu((trident->tlb.entries[page]) & ~(SNDRV_TRIDENT_PAGE_SIZE - 1))
43
44#if PAGE_SIZE == 4096
45/* page size == SNDRV_TRIDENT_PAGE_SIZE */
46#define ALIGN_PAGE_SIZE PAGE_SIZE /* minimum page size for allocation */
47#define MAX_ALIGN_PAGES SNDRV_TRIDENT_MAX_PAGES /* maxmium aligned pages */
48/* fill TLB entrie(s) corresponding to page with ptr */
49#define set_tlb_bus(trident,page,ptr,addr) __set_tlb_bus(trident,page,ptr,addr)
50/* fill TLB entrie(s) corresponding to page with silence pointer */
51#define set_silent_tlb(trident,page) __set_tlb_bus(trident, page, (unsigned long)trident->tlb.silent_page.area, trident->tlb.silent_page.addr)
52/* get aligned page from offset address */
53#define get_aligned_page(offset) ((offset) >> 12)
54/* get offset address from aligned page */
55#define aligned_page_offset(page) ((page) << 12)
56/* get buffer address from aligned page */
57#define page_to_ptr(trident,page) __tlb_to_ptr(trident, page)
58/* get PCI physical address from aligned page */
59#define page_to_addr(trident,page) __tlb_to_addr(trident, page)
60
61#elif PAGE_SIZE == 8192
62/* page size == SNDRV_TRIDENT_PAGE_SIZE x 2*/
63#define ALIGN_PAGE_SIZE PAGE_SIZE
64#define MAX_ALIGN_PAGES (SNDRV_TRIDENT_MAX_PAGES / 2)
65#define get_aligned_page(offset) ((offset) >> 13)
66#define aligned_page_offset(page) ((page) << 13)
67#define page_to_ptr(trident,page) __tlb_to_ptr(trident, (page) << 1)
68#define page_to_addr(trident,page) __tlb_to_addr(trident, (page) << 1)
69
70/* fill TLB entries -- we need to fill two entries */
bee1a5be
TI
71static inline void set_tlb_bus(struct snd_trident *trident, int page,
72 unsigned long ptr, dma_addr_t addr)
1da177e4
LT
73{
74 page <<= 1;
75 __set_tlb_bus(trident, page, ptr, addr);
76 __set_tlb_bus(trident, page+1, ptr + SNDRV_TRIDENT_PAGE_SIZE, addr + SNDRV_TRIDENT_PAGE_SIZE);
77}
bee1a5be 78static inline void set_silent_tlb(struct snd_trident *trident, int page)
1da177e4
LT
79{
80 page <<= 1;
81 __set_tlb_bus(trident, page, (unsigned long)trident->tlb.silent_page.area, trident->tlb.silent_page.addr);
82 __set_tlb_bus(trident, page+1, (unsigned long)trident->tlb.silent_page.area, trident->tlb.silent_page.addr);
83}
84
85#else
86/* arbitrary size */
87#define UNIT_PAGES (PAGE_SIZE / SNDRV_TRIDENT_PAGE_SIZE)
88#define ALIGN_PAGE_SIZE (SNDRV_TRIDENT_PAGE_SIZE * UNIT_PAGES)
89#define MAX_ALIGN_PAGES (SNDRV_TRIDENT_MAX_PAGES / UNIT_PAGES)
90/* Note: if alignment doesn't match to the maximum size, the last few blocks
91 * become unusable. To use such blocks, you'll need to check the validity
92 * of accessing page in set_tlb_bus and set_silent_tlb. search_empty()
93 * should also check it, too.
94 */
95#define get_aligned_page(offset) ((offset) / ALIGN_PAGE_SIZE)
96#define aligned_page_offset(page) ((page) * ALIGN_PAGE_SIZE)
97#define page_to_ptr(trident,page) __tlb_to_ptr(trident, (page) * UNIT_PAGES)
98#define page_to_addr(trident,page) __tlb_to_addr(trident, (page) * UNIT_PAGES)
99
100/* fill TLB entries -- UNIT_PAGES entries must be filled */
bee1a5be
TI
101static inline void set_tlb_bus(struct snd_trident *trident, int page,
102 unsigned long ptr, dma_addr_t addr)
1da177e4
LT
103{
104 int i;
105 page *= UNIT_PAGES;
106 for (i = 0; i < UNIT_PAGES; i++, page++) {
107 __set_tlb_bus(trident, page, ptr, addr);
108 ptr += SNDRV_TRIDENT_PAGE_SIZE;
109 addr += SNDRV_TRIDENT_PAGE_SIZE;
110 }
111}
bee1a5be 112static inline void set_silent_tlb(struct snd_trident *trident, int page)
1da177e4
LT
113{
114 int i;
115 page *= UNIT_PAGES;
116 for (i = 0; i < UNIT_PAGES; i++, page++)
117 __set_tlb_bus(trident, page, (unsigned long)trident->tlb.silent_page.area, trident->tlb.silent_page.addr);
118}
119
120#endif /* PAGE_SIZE */
121
122/* calculate buffer pointer from offset address */
bee1a5be 123static inline void *offset_ptr(struct snd_trident *trident, int offset)
1da177e4
LT
124{
125 char *ptr;
126 ptr = page_to_ptr(trident, get_aligned_page(offset));
127 ptr += offset % ALIGN_PAGE_SIZE;
128 return (void*)ptr;
129}
130
131/* first and last (aligned) pages of memory block */
bee1a5be
TI
132#define firstpg(blk) (((struct snd_trident_memblk_arg *)snd_util_memblk_argptr(blk))->first_page)
133#define lastpg(blk) (((struct snd_trident_memblk_arg *)snd_util_memblk_argptr(blk))->last_page)
1da177e4
LT
134
135/*
136 * search empty pages which may contain given size
137 */
bee1a5be
TI
138static struct snd_util_memblk *
139search_empty(struct snd_util_memhdr *hdr, int size)
1da177e4 140{
bee1a5be 141 struct snd_util_memblk *blk, *prev;
1da177e4
LT
142 int page, psize;
143 struct list_head *p;
144
145 psize = get_aligned_page(size + ALIGN_PAGE_SIZE -1);
146 prev = NULL;
147 page = 0;
148 list_for_each(p, &hdr->block) {
bee1a5be 149 blk = list_entry(p, struct snd_util_memblk, list);
1da177e4
LT
150 if (page + psize <= firstpg(blk))
151 goto __found_pages;
152 page = lastpg(blk) + 1;
153 }
154 if (page + psize > MAX_ALIGN_PAGES)
155 return NULL;
156
157__found_pages:
158 /* create a new memory block */
159 blk = __snd_util_memblk_new(hdr, psize * ALIGN_PAGE_SIZE, p->prev);
160 if (blk == NULL)
161 return NULL;
162 blk->offset = aligned_page_offset(page); /* set aligned offset */
163 firstpg(blk) = page;
164 lastpg(blk) = page + psize - 1;
165 return blk;
166}
167
168
169/*
170 * check if the given pointer is valid for pages
171 */
172static int is_valid_page(unsigned long ptr)
173{
174 if (ptr & ~0x3fffffffUL) {
99b359ba 175 snd_printk(KERN_ERR "max memory size is 1GB!!\n");
1da177e4
LT
176 return 0;
177 }
178 if (ptr & (SNDRV_TRIDENT_PAGE_SIZE-1)) {
99b359ba 179 snd_printk(KERN_ERR "page is not aligned\n");
1da177e4
LT
180 return 0;
181 }
182 return 1;
183}
184
185/*
186 * page allocation for DMA (Scatter-Gather version)
187 */
bee1a5be
TI
188static struct snd_util_memblk *
189snd_trident_alloc_sg_pages(struct snd_trident *trident,
190 struct snd_pcm_substream *substream)
1da177e4 191{
bee1a5be
TI
192 struct snd_util_memhdr *hdr;
193 struct snd_util_memblk *blk;
194 struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4
LT
195 int idx, page;
196 struct snd_sg_buf *sgbuf = snd_pcm_substream_sgbuf(substream);
197
198 snd_assert(runtime->dma_bytes > 0 && runtime->dma_bytes <= SNDRV_TRIDENT_MAX_PAGES * SNDRV_TRIDENT_PAGE_SIZE, return NULL);
199 hdr = trident->tlb.memhdr;
200 snd_assert(hdr != NULL, return NULL);
201
202
203
204 down(&hdr->block_mutex);
205 blk = search_empty(hdr, runtime->dma_bytes);
206 if (blk == NULL) {
207 up(&hdr->block_mutex);
208 return NULL;
209 }
210 if (lastpg(blk) - firstpg(blk) >= sgbuf->pages) {
211 snd_printk(KERN_ERR "page calculation doesn't match: allocated pages = %d, trident = %d/%d\n", sgbuf->pages, firstpg(blk), lastpg(blk));
212 __snd_util_mem_free(hdr, blk);
213 up(&hdr->block_mutex);
214 return NULL;
215 }
216
217 /* set TLB entries */
218 idx = 0;
219 for (page = firstpg(blk); page <= lastpg(blk); page++, idx++) {
220 dma_addr_t addr = sgbuf->table[idx].addr;
221 unsigned long ptr = (unsigned long)sgbuf->table[idx].buf;
222 if (! is_valid_page(addr)) {
223 __snd_util_mem_free(hdr, blk);
224 up(&hdr->block_mutex);
225 return NULL;
226 }
227 set_tlb_bus(trident, page, ptr, addr);
228 }
229 up(&hdr->block_mutex);
230 return blk;
231}
232
233/*
234 * page allocation for DMA (contiguous version)
235 */
bee1a5be
TI
236static struct snd_util_memblk *
237snd_trident_alloc_cont_pages(struct snd_trident *trident,
238 struct snd_pcm_substream *substream)
1da177e4 239{
bee1a5be
TI
240 struct snd_util_memhdr *hdr;
241 struct snd_util_memblk *blk;
1da177e4 242 int page;
bee1a5be 243 struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4
LT
244 dma_addr_t addr;
245 unsigned long ptr;
246
247 snd_assert(runtime->dma_bytes> 0 && runtime->dma_bytes <= SNDRV_TRIDENT_MAX_PAGES * SNDRV_TRIDENT_PAGE_SIZE, return NULL);
248 hdr = trident->tlb.memhdr;
249 snd_assert(hdr != NULL, return NULL);
250
251 down(&hdr->block_mutex);
252 blk = search_empty(hdr, runtime->dma_bytes);
253 if (blk == NULL) {
254 up(&hdr->block_mutex);
255 return NULL;
256 }
257
258 /* set TLB entries */
259 addr = runtime->dma_addr;
260 ptr = (unsigned long)runtime->dma_area;
261 for (page = firstpg(blk); page <= lastpg(blk); page++,
262 ptr += SNDRV_TRIDENT_PAGE_SIZE, addr += SNDRV_TRIDENT_PAGE_SIZE) {
263 if (! is_valid_page(addr)) {
264 __snd_util_mem_free(hdr, blk);
265 up(&hdr->block_mutex);
266 return NULL;
267 }
268 set_tlb_bus(trident, page, ptr, addr);
269 }
270 up(&hdr->block_mutex);
271 return blk;
272}
273
274/*
275 * page allocation for DMA
276 */
bee1a5be
TI
277struct snd_util_memblk *
278snd_trident_alloc_pages(struct snd_trident *trident,
279 struct snd_pcm_substream *substream)
1da177e4
LT
280{
281 snd_assert(trident != NULL, return NULL);
282 snd_assert(substream != NULL, return NULL);
283 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV_SG)
284 return snd_trident_alloc_sg_pages(trident, substream);
285 else
286 return snd_trident_alloc_cont_pages(trident, substream);
287}
288
289
290/*
291 * release DMA buffer from page table
292 */
bee1a5be
TI
293int snd_trident_free_pages(struct snd_trident *trident,
294 struct snd_util_memblk *blk)
1da177e4 295{
bee1a5be 296 struct snd_util_memhdr *hdr;
1da177e4
LT
297 int page;
298
299 snd_assert(trident != NULL, return -EINVAL);
300 snd_assert(blk != NULL, return -EINVAL);
301
302 hdr = trident->tlb.memhdr;
303 down(&hdr->block_mutex);
304 /* reset TLB entries */
305 for (page = firstpg(blk); page <= lastpg(blk); page++)
306 set_silent_tlb(trident, page);
307 /* free memory block */
308 __snd_util_mem_free(hdr, blk);
309 up(&hdr->block_mutex);
310 return 0;
311}
312
313
314/*----------------------------------------------------------------
315 * memory allocation using multiple pages (for synth)
316 *----------------------------------------------------------------
317 * Unlike the DMA allocation above, non-contiguous pages are
318 * assigned to TLB.
319 *----------------------------------------------------------------*/
320
321/*
322 */
bee1a5be
TI
323static int synth_alloc_pages(struct snd_trident *hw, struct snd_util_memblk *blk);
324static int synth_free_pages(struct snd_trident *hw, struct snd_util_memblk *blk);
1da177e4
LT
325
326/*
327 * allocate a synth sample area
328 */
bee1a5be
TI
329struct snd_util_memblk *
330snd_trident_synth_alloc(struct snd_trident *hw, unsigned int size)
1da177e4 331{
bee1a5be
TI
332 struct snd_util_memblk *blk;
333 struct snd_util_memhdr *hdr = hw->tlb.memhdr;
1da177e4
LT
334
335 down(&hdr->block_mutex);
336 blk = __snd_util_mem_alloc(hdr, size);
337 if (blk == NULL) {
338 up(&hdr->block_mutex);
339 return NULL;
340 }
341 if (synth_alloc_pages(hw, blk)) {
342 __snd_util_mem_free(hdr, blk);
343 up(&hdr->block_mutex);
344 return NULL;
345 }
346 up(&hdr->block_mutex);
347 return blk;
348}
349
350
351/*
352 * free a synth sample area
353 */
354int
bee1a5be 355snd_trident_synth_free(struct snd_trident *hw, struct snd_util_memblk *blk)
1da177e4 356{
bee1a5be 357 struct snd_util_memhdr *hdr = hw->tlb.memhdr;
1da177e4
LT
358
359 down(&hdr->block_mutex);
360 synth_free_pages(hw, blk);
361 __snd_util_mem_free(hdr, blk);
362 up(&hdr->block_mutex);
363 return 0;
364}
365
366
367/*
368 * reset TLB entry and free kernel page
369 */
bee1a5be 370static void clear_tlb(struct snd_trident *trident, int page)
1da177e4
LT
371{
372 void *ptr = page_to_ptr(trident, page);
373 dma_addr_t addr = page_to_addr(trident, page);
374 set_silent_tlb(trident, page);
375 if (ptr) {
376 struct snd_dma_buffer dmab;
377 dmab.dev.type = SNDRV_DMA_TYPE_DEV;
378 dmab.dev.dev = snd_dma_pci_data(trident->pci);
379 dmab.area = ptr;
380 dmab.addr = addr;
381 dmab.bytes = ALIGN_PAGE_SIZE;
382 snd_dma_free_pages(&dmab);
383 }
384}
385
386/* check new allocation range */
bee1a5be
TI
387static void get_single_page_range(struct snd_util_memhdr *hdr,
388 struct snd_util_memblk *blk,
389 int *first_page_ret, int *last_page_ret)
1da177e4
LT
390{
391 struct list_head *p;
bee1a5be 392 struct snd_util_memblk *q;
1da177e4
LT
393 int first_page, last_page;
394 first_page = firstpg(blk);
395 if ((p = blk->list.prev) != &hdr->block) {
bee1a5be 396 q = list_entry(p, struct snd_util_memblk, list);
1da177e4
LT
397 if (lastpg(q) == first_page)
398 first_page++; /* first page was already allocated */
399 }
400 last_page = lastpg(blk);
401 if ((p = blk->list.next) != &hdr->block) {
bee1a5be 402 q = list_entry(p, struct snd_util_memblk, list);
1da177e4
LT
403 if (firstpg(q) == last_page)
404 last_page--; /* last page was already allocated */
405 }
406 *first_page_ret = first_page;
407 *last_page_ret = last_page;
408}
409
410/*
411 * allocate kernel pages and assign them to TLB
412 */
bee1a5be 413static int synth_alloc_pages(struct snd_trident *hw, struct snd_util_memblk *blk)
1da177e4
LT
414{
415 int page, first_page, last_page;
416 struct snd_dma_buffer dmab;
417
418 firstpg(blk) = get_aligned_page(blk->offset);
419 lastpg(blk) = get_aligned_page(blk->offset + blk->size - 1);
420 get_single_page_range(hw->tlb.memhdr, blk, &first_page, &last_page);
421
422 /* allocate a kernel page for each Trident page -
423 * fortunately Trident page size and kernel PAGE_SIZE is identical!
424 */
425 for (page = first_page; page <= last_page; page++) {
426 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(hw->pci),
427 ALIGN_PAGE_SIZE, &dmab) < 0)
428 goto __fail;
429 if (! is_valid_page(dmab.addr)) {
430 snd_dma_free_pages(&dmab);
431 goto __fail;
432 }
433 set_tlb_bus(hw, page, (unsigned long)dmab.area, dmab.addr);
434 }
435 return 0;
436
437__fail:
438 /* release allocated pages */
439 last_page = page - 1;
440 for (page = first_page; page <= last_page; page++)
441 clear_tlb(hw, page);
442
443 return -ENOMEM;
444}
445
446/*
447 * free pages
448 */
bee1a5be 449static int synth_free_pages(struct snd_trident *trident, struct snd_util_memblk *blk)
1da177e4
LT
450{
451 int page, first_page, last_page;
452
453 get_single_page_range(trident->tlb.memhdr, blk, &first_page, &last_page);
454 for (page = first_page; page <= last_page; page++)
455 clear_tlb(trident, page);
456
457 return 0;
458}
459
460/*
461 * copy_from_user(blk + offset, data, size)
462 */
bee1a5be
TI
463int snd_trident_synth_copy_from_user(struct snd_trident *trident,
464 struct snd_util_memblk *blk,
465 int offset, const char __user *data, int size)
1da177e4
LT
466{
467 int page, nextofs, end_offset, temp, temp1;
468
469 offset += blk->offset;
470 end_offset = offset + size;
471 page = get_aligned_page(offset) + 1;
472 do {
473 nextofs = aligned_page_offset(page);
474 temp = nextofs - offset;
475 temp1 = end_offset - offset;
476 if (temp1 < temp)
477 temp = temp1;
478 if (copy_from_user(offset_ptr(trident, offset), data, temp))
479 return -EFAULT;
480 offset = nextofs;
481 data += temp;
482 page++;
483 } while (offset < end_offset);
484 return 0;
485}
486
This page took 0.118499 seconds and 5 git commands to generate.