[PATCH] clean up inline static vs static inline
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / sound / pci / trident / trident_memory.c
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 */
71 static inline void set_tlb_bus(trident_t *trident, int page, unsigned long ptr, dma_addr_t addr)
72 {
73 page <<= 1;
74 __set_tlb_bus(trident, page, ptr, addr);
75 __set_tlb_bus(trident, page+1, ptr + SNDRV_TRIDENT_PAGE_SIZE, addr + SNDRV_TRIDENT_PAGE_SIZE);
76 }
77 static inline void set_silent_tlb(trident_t *trident, int page)
78 {
79 page <<= 1;
80 __set_tlb_bus(trident, page, (unsigned long)trident->tlb.silent_page.area, trident->tlb.silent_page.addr);
81 __set_tlb_bus(trident, page+1, (unsigned long)trident->tlb.silent_page.area, trident->tlb.silent_page.addr);
82 }
83
84 #else
85 /* arbitrary size */
86 #define UNIT_PAGES (PAGE_SIZE / SNDRV_TRIDENT_PAGE_SIZE)
87 #define ALIGN_PAGE_SIZE (SNDRV_TRIDENT_PAGE_SIZE * UNIT_PAGES)
88 #define MAX_ALIGN_PAGES (SNDRV_TRIDENT_MAX_PAGES / UNIT_PAGES)
89 /* Note: if alignment doesn't match to the maximum size, the last few blocks
90 * become unusable. To use such blocks, you'll need to check the validity
91 * of accessing page in set_tlb_bus and set_silent_tlb. search_empty()
92 * should also check it, too.
93 */
94 #define get_aligned_page(offset) ((offset) / ALIGN_PAGE_SIZE)
95 #define aligned_page_offset(page) ((page) * ALIGN_PAGE_SIZE)
96 #define page_to_ptr(trident,page) __tlb_to_ptr(trident, (page) * UNIT_PAGES)
97 #define page_to_addr(trident,page) __tlb_to_addr(trident, (page) * UNIT_PAGES)
98
99 /* fill TLB entries -- UNIT_PAGES entries must be filled */
100 static inline void set_tlb_bus(trident_t *trident, int page, unsigned long ptr, dma_addr_t addr)
101 {
102 int i;
103 page *= UNIT_PAGES;
104 for (i = 0; i < UNIT_PAGES; i++, page++) {
105 __set_tlb_bus(trident, page, ptr, addr);
106 ptr += SNDRV_TRIDENT_PAGE_SIZE;
107 addr += SNDRV_TRIDENT_PAGE_SIZE;
108 }
109 }
110 static inline void set_silent_tlb(trident_t *trident, int page)
111 {
112 int i;
113 page *= UNIT_PAGES;
114 for (i = 0; i < UNIT_PAGES; i++, page++)
115 __set_tlb_bus(trident, page, (unsigned long)trident->tlb.silent_page.area, trident->tlb.silent_page.addr);
116 }
117
118 #endif /* PAGE_SIZE */
119
120 /* calculate buffer pointer from offset address */
121 static inline void *offset_ptr(trident_t *trident, int offset)
122 {
123 char *ptr;
124 ptr = page_to_ptr(trident, get_aligned_page(offset));
125 ptr += offset % ALIGN_PAGE_SIZE;
126 return (void*)ptr;
127 }
128
129 /* first and last (aligned) pages of memory block */
130 #define firstpg(blk) (((snd_trident_memblk_arg_t*)snd_util_memblk_argptr(blk))->first_page)
131 #define lastpg(blk) (((snd_trident_memblk_arg_t*)snd_util_memblk_argptr(blk))->last_page)
132
133 /*
134 * search empty pages which may contain given size
135 */
136 static snd_util_memblk_t *
137 search_empty(snd_util_memhdr_t *hdr, int size)
138 {
139 snd_util_memblk_t *blk, *prev;
140 int page, psize;
141 struct list_head *p;
142
143 psize = get_aligned_page(size + ALIGN_PAGE_SIZE -1);
144 prev = NULL;
145 page = 0;
146 list_for_each(p, &hdr->block) {
147 blk = list_entry(p, snd_util_memblk_t, list);
148 if (page + psize <= firstpg(blk))
149 goto __found_pages;
150 page = lastpg(blk) + 1;
151 }
152 if (page + psize > MAX_ALIGN_PAGES)
153 return NULL;
154
155 __found_pages:
156 /* create a new memory block */
157 blk = __snd_util_memblk_new(hdr, psize * ALIGN_PAGE_SIZE, p->prev);
158 if (blk == NULL)
159 return NULL;
160 blk->offset = aligned_page_offset(page); /* set aligned offset */
161 firstpg(blk) = page;
162 lastpg(blk) = page + psize - 1;
163 return blk;
164 }
165
166
167 /*
168 * check if the given pointer is valid for pages
169 */
170 static int is_valid_page(unsigned long ptr)
171 {
172 if (ptr & ~0x3fffffffUL) {
173 snd_printk("max memory size is 1GB!!\n");
174 return 0;
175 }
176 if (ptr & (SNDRV_TRIDENT_PAGE_SIZE-1)) {
177 snd_printk("page is not aligned\n");
178 return 0;
179 }
180 return 1;
181 }
182
183 /*
184 * page allocation for DMA (Scatter-Gather version)
185 */
186 static snd_util_memblk_t *
187 snd_trident_alloc_sg_pages(trident_t *trident, snd_pcm_substream_t *substream)
188 {
189 snd_util_memhdr_t *hdr;
190 snd_util_memblk_t *blk;
191 snd_pcm_runtime_t *runtime = substream->runtime;
192 int idx, page;
193 struct snd_sg_buf *sgbuf = snd_pcm_substream_sgbuf(substream);
194
195 snd_assert(runtime->dma_bytes > 0 && runtime->dma_bytes <= SNDRV_TRIDENT_MAX_PAGES * SNDRV_TRIDENT_PAGE_SIZE, return NULL);
196 hdr = trident->tlb.memhdr;
197 snd_assert(hdr != NULL, return NULL);
198
199
200
201 down(&hdr->block_mutex);
202 blk = search_empty(hdr, runtime->dma_bytes);
203 if (blk == NULL) {
204 up(&hdr->block_mutex);
205 return NULL;
206 }
207 if (lastpg(blk) - firstpg(blk) >= sgbuf->pages) {
208 snd_printk(KERN_ERR "page calculation doesn't match: allocated pages = %d, trident = %d/%d\n", sgbuf->pages, firstpg(blk), lastpg(blk));
209 __snd_util_mem_free(hdr, blk);
210 up(&hdr->block_mutex);
211 return NULL;
212 }
213
214 /* set TLB entries */
215 idx = 0;
216 for (page = firstpg(blk); page <= lastpg(blk); page++, idx++) {
217 dma_addr_t addr = sgbuf->table[idx].addr;
218 unsigned long ptr = (unsigned long)sgbuf->table[idx].buf;
219 if (! is_valid_page(addr)) {
220 __snd_util_mem_free(hdr, blk);
221 up(&hdr->block_mutex);
222 return NULL;
223 }
224 set_tlb_bus(trident, page, ptr, addr);
225 }
226 up(&hdr->block_mutex);
227 return blk;
228 }
229
230 /*
231 * page allocation for DMA (contiguous version)
232 */
233 static snd_util_memblk_t *
234 snd_trident_alloc_cont_pages(trident_t *trident, snd_pcm_substream_t *substream)
235 {
236 snd_util_memhdr_t *hdr;
237 snd_util_memblk_t *blk;
238 int page;
239 snd_pcm_runtime_t *runtime = substream->runtime;
240 dma_addr_t addr;
241 unsigned long ptr;
242
243 snd_assert(runtime->dma_bytes> 0 && runtime->dma_bytes <= SNDRV_TRIDENT_MAX_PAGES * SNDRV_TRIDENT_PAGE_SIZE, return NULL);
244 hdr = trident->tlb.memhdr;
245 snd_assert(hdr != NULL, return NULL);
246
247 down(&hdr->block_mutex);
248 blk = search_empty(hdr, runtime->dma_bytes);
249 if (blk == NULL) {
250 up(&hdr->block_mutex);
251 return NULL;
252 }
253
254 /* set TLB entries */
255 addr = runtime->dma_addr;
256 ptr = (unsigned long)runtime->dma_area;
257 for (page = firstpg(blk); page <= lastpg(blk); page++,
258 ptr += SNDRV_TRIDENT_PAGE_SIZE, addr += SNDRV_TRIDENT_PAGE_SIZE) {
259 if (! is_valid_page(addr)) {
260 __snd_util_mem_free(hdr, blk);
261 up(&hdr->block_mutex);
262 return NULL;
263 }
264 set_tlb_bus(trident, page, ptr, addr);
265 }
266 up(&hdr->block_mutex);
267 return blk;
268 }
269
270 /*
271 * page allocation for DMA
272 */
273 snd_util_memblk_t *
274 snd_trident_alloc_pages(trident_t *trident, snd_pcm_substream_t *substream)
275 {
276 snd_assert(trident != NULL, return NULL);
277 snd_assert(substream != NULL, return NULL);
278 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV_SG)
279 return snd_trident_alloc_sg_pages(trident, substream);
280 else
281 return snd_trident_alloc_cont_pages(trident, substream);
282 }
283
284
285 /*
286 * release DMA buffer from page table
287 */
288 int snd_trident_free_pages(trident_t *trident, snd_util_memblk_t *blk)
289 {
290 snd_util_memhdr_t *hdr;
291 int page;
292
293 snd_assert(trident != NULL, return -EINVAL);
294 snd_assert(blk != NULL, return -EINVAL);
295
296 hdr = trident->tlb.memhdr;
297 down(&hdr->block_mutex);
298 /* reset TLB entries */
299 for (page = firstpg(blk); page <= lastpg(blk); page++)
300 set_silent_tlb(trident, page);
301 /* free memory block */
302 __snd_util_mem_free(hdr, blk);
303 up(&hdr->block_mutex);
304 return 0;
305 }
306
307
308 /*----------------------------------------------------------------
309 * memory allocation using multiple pages (for synth)
310 *----------------------------------------------------------------
311 * Unlike the DMA allocation above, non-contiguous pages are
312 * assigned to TLB.
313 *----------------------------------------------------------------*/
314
315 /*
316 */
317 static int synth_alloc_pages(trident_t *hw, snd_util_memblk_t *blk);
318 static int synth_free_pages(trident_t *hw, snd_util_memblk_t *blk);
319
320 /*
321 * allocate a synth sample area
322 */
323 snd_util_memblk_t *
324 snd_trident_synth_alloc(trident_t *hw, unsigned int size)
325 {
326 snd_util_memblk_t *blk;
327 snd_util_memhdr_t *hdr = hw->tlb.memhdr;
328
329 down(&hdr->block_mutex);
330 blk = __snd_util_mem_alloc(hdr, size);
331 if (blk == NULL) {
332 up(&hdr->block_mutex);
333 return NULL;
334 }
335 if (synth_alloc_pages(hw, blk)) {
336 __snd_util_mem_free(hdr, blk);
337 up(&hdr->block_mutex);
338 return NULL;
339 }
340 up(&hdr->block_mutex);
341 return blk;
342 }
343
344
345 /*
346 * free a synth sample area
347 */
348 int
349 snd_trident_synth_free(trident_t *hw, snd_util_memblk_t *blk)
350 {
351 snd_util_memhdr_t *hdr = hw->tlb.memhdr;
352
353 down(&hdr->block_mutex);
354 synth_free_pages(hw, blk);
355 __snd_util_mem_free(hdr, blk);
356 up(&hdr->block_mutex);
357 return 0;
358 }
359
360
361 /*
362 * reset TLB entry and free kernel page
363 */
364 static void clear_tlb(trident_t *trident, int page)
365 {
366 void *ptr = page_to_ptr(trident, page);
367 dma_addr_t addr = page_to_addr(trident, page);
368 set_silent_tlb(trident, page);
369 if (ptr) {
370 struct snd_dma_buffer dmab;
371 dmab.dev.type = SNDRV_DMA_TYPE_DEV;
372 dmab.dev.dev = snd_dma_pci_data(trident->pci);
373 dmab.area = ptr;
374 dmab.addr = addr;
375 dmab.bytes = ALIGN_PAGE_SIZE;
376 snd_dma_free_pages(&dmab);
377 }
378 }
379
380 /* check new allocation range */
381 static void get_single_page_range(snd_util_memhdr_t *hdr, snd_util_memblk_t *blk, int *first_page_ret, int *last_page_ret)
382 {
383 struct list_head *p;
384 snd_util_memblk_t *q;
385 int first_page, last_page;
386 first_page = firstpg(blk);
387 if ((p = blk->list.prev) != &hdr->block) {
388 q = list_entry(p, snd_util_memblk_t, list);
389 if (lastpg(q) == first_page)
390 first_page++; /* first page was already allocated */
391 }
392 last_page = lastpg(blk);
393 if ((p = blk->list.next) != &hdr->block) {
394 q = list_entry(p, snd_util_memblk_t, list);
395 if (firstpg(q) == last_page)
396 last_page--; /* last page was already allocated */
397 }
398 *first_page_ret = first_page;
399 *last_page_ret = last_page;
400 }
401
402 /*
403 * allocate kernel pages and assign them to TLB
404 */
405 static int synth_alloc_pages(trident_t *hw, snd_util_memblk_t *blk)
406 {
407 int page, first_page, last_page;
408 struct snd_dma_buffer dmab;
409
410 firstpg(blk) = get_aligned_page(blk->offset);
411 lastpg(blk) = get_aligned_page(blk->offset + blk->size - 1);
412 get_single_page_range(hw->tlb.memhdr, blk, &first_page, &last_page);
413
414 /* allocate a kernel page for each Trident page -
415 * fortunately Trident page size and kernel PAGE_SIZE is identical!
416 */
417 for (page = first_page; page <= last_page; page++) {
418 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(hw->pci),
419 ALIGN_PAGE_SIZE, &dmab) < 0)
420 goto __fail;
421 if (! is_valid_page(dmab.addr)) {
422 snd_dma_free_pages(&dmab);
423 goto __fail;
424 }
425 set_tlb_bus(hw, page, (unsigned long)dmab.area, dmab.addr);
426 }
427 return 0;
428
429 __fail:
430 /* release allocated pages */
431 last_page = page - 1;
432 for (page = first_page; page <= last_page; page++)
433 clear_tlb(hw, page);
434
435 return -ENOMEM;
436 }
437
438 /*
439 * free pages
440 */
441 static int synth_free_pages(trident_t *trident, snd_util_memblk_t *blk)
442 {
443 int page, first_page, last_page;
444
445 get_single_page_range(trident->tlb.memhdr, blk, &first_page, &last_page);
446 for (page = first_page; page <= last_page; page++)
447 clear_tlb(trident, page);
448
449 return 0;
450 }
451
452 /*
453 * copy_from_user(blk + offset, data, size)
454 */
455 int snd_trident_synth_copy_from_user(trident_t *trident, snd_util_memblk_t *blk, int offset, const char __user *data, int size)
456 {
457 int page, nextofs, end_offset, temp, temp1;
458
459 offset += blk->offset;
460 end_offset = offset + size;
461 page = get_aligned_page(offset) + 1;
462 do {
463 nextofs = aligned_page_offset(page);
464 temp = nextofs - offset;
465 temp1 = end_offset - offset;
466 if (temp1 < temp)
467 temp = temp1;
468 if (copy_from_user(offset_ptr(trident, offset), data, temp))
469 return -EFAULT;
470 offset = nextofs;
471 data += temp;
472 page++;
473 } while (offset < end_offset);
474 return 0;
475 }
476