trident_memory.c 14.6 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
2
 *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
L
Linus Torvalds 已提交
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
 *  Copyright (c) by Takashi Iwai <tiwai@suse.de>
 *  Copyright (c) by Scott McNab <sdm@fractalgraphics.com.au>
 *
 *  Trident 4DWave-NX memory page allocation (TLB area)
 *  Trident chip can handle only 16MByte of the memory at the same time.
 *
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 *
 */

#include <sound/driver.h>
#include <asm/io.h>
#include <linux/pci.h>
#include <linux/time.h>
30 31
#include <linux/mutex.h>

L
Linus Torvalds 已提交
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
#include <sound/core.h>
#include <sound/trident.h>

/* page arguments of these two macros are Trident page (4096 bytes), not like
 * aligned pages in others
 */
#define __set_tlb_bus(trident,page,ptr,addr) \
	do { (trident)->tlb.entries[page] = cpu_to_le32((addr) & ~(SNDRV_TRIDENT_PAGE_SIZE-1)); \
	     (trident)->tlb.shadow_entries[page] = (ptr); } while (0)
#define __tlb_to_ptr(trident,page) \
	(void*)((trident)->tlb.shadow_entries[page])
#define __tlb_to_addr(trident,page) \
	(dma_addr_t)le32_to_cpu((trident->tlb.entries[page]) & ~(SNDRV_TRIDENT_PAGE_SIZE - 1))

#if PAGE_SIZE == 4096
/* page size == SNDRV_TRIDENT_PAGE_SIZE */
#define ALIGN_PAGE_SIZE		PAGE_SIZE	/* minimum page size for allocation */
#define MAX_ALIGN_PAGES		SNDRV_TRIDENT_MAX_PAGES	/* maxmium aligned pages */
/* fill TLB entrie(s) corresponding to page with ptr */
#define set_tlb_bus(trident,page,ptr,addr) __set_tlb_bus(trident,page,ptr,addr)
/* fill TLB entrie(s) corresponding to page with silence pointer */
#define set_silent_tlb(trident,page)	__set_tlb_bus(trident, page, (unsigned long)trident->tlb.silent_page.area, trident->tlb.silent_page.addr)
/* get aligned page from offset address */
#define get_aligned_page(offset)	((offset) >> 12)
/* get offset address from aligned page */
#define aligned_page_offset(page)	((page) << 12)
/* get buffer address from aligned page */
#define page_to_ptr(trident,page)	__tlb_to_ptr(trident, page)
/* get PCI physical address from aligned page */
#define page_to_addr(trident,page)	__tlb_to_addr(trident, page)

#elif PAGE_SIZE == 8192
/* page size == SNDRV_TRIDENT_PAGE_SIZE x 2*/
#define ALIGN_PAGE_SIZE		PAGE_SIZE
#define MAX_ALIGN_PAGES		(SNDRV_TRIDENT_MAX_PAGES / 2)
#define get_aligned_page(offset)	((offset) >> 13)
#define aligned_page_offset(page)	((page) << 13)
#define page_to_ptr(trident,page)	__tlb_to_ptr(trident, (page) << 1)
#define page_to_addr(trident,page)	__tlb_to_addr(trident, (page) << 1)

/* fill TLB entries -- we need to fill two entries */
73 74
static inline void set_tlb_bus(struct snd_trident *trident, int page,
			       unsigned long ptr, dma_addr_t addr)
L
Linus Torvalds 已提交
75 76 77 78 79
{
	page <<= 1;
	__set_tlb_bus(trident, page, ptr, addr);
	__set_tlb_bus(trident, page+1, ptr + SNDRV_TRIDENT_PAGE_SIZE, addr + SNDRV_TRIDENT_PAGE_SIZE);
}
80
static inline void set_silent_tlb(struct snd_trident *trident, int page)
L
Linus Torvalds 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
{
	page <<= 1;
	__set_tlb_bus(trident, page, (unsigned long)trident->tlb.silent_page.area, trident->tlb.silent_page.addr);
	__set_tlb_bus(trident, page+1, (unsigned long)trident->tlb.silent_page.area, trident->tlb.silent_page.addr);
}

#else
/* arbitrary size */
#define UNIT_PAGES		(PAGE_SIZE / SNDRV_TRIDENT_PAGE_SIZE)
#define ALIGN_PAGE_SIZE		(SNDRV_TRIDENT_PAGE_SIZE * UNIT_PAGES)
#define MAX_ALIGN_PAGES		(SNDRV_TRIDENT_MAX_PAGES / UNIT_PAGES)
/* Note: if alignment doesn't match to the maximum size, the last few blocks
 * become unusable.  To use such blocks, you'll need to check the validity
 * of accessing page in set_tlb_bus and set_silent_tlb.  search_empty()
 * should also check it, too.
 */
#define get_aligned_page(offset)	((offset) / ALIGN_PAGE_SIZE)
#define aligned_page_offset(page)	((page) * ALIGN_PAGE_SIZE)
#define page_to_ptr(trident,page)	__tlb_to_ptr(trident, (page) * UNIT_PAGES)
#define page_to_addr(trident,page)	__tlb_to_addr(trident, (page) * UNIT_PAGES)

/* fill TLB entries -- UNIT_PAGES entries must be filled */
103 104
static inline void set_tlb_bus(struct snd_trident *trident, int page,
			       unsigned long ptr, dma_addr_t addr)
L
Linus Torvalds 已提交
105 106 107 108 109 110 111 112 113
{
	int i;
	page *= UNIT_PAGES;
	for (i = 0; i < UNIT_PAGES; i++, page++) {
		__set_tlb_bus(trident, page, ptr, addr);
		ptr += SNDRV_TRIDENT_PAGE_SIZE;
		addr += SNDRV_TRIDENT_PAGE_SIZE;
	}
}
114
static inline void set_silent_tlb(struct snd_trident *trident, int page)
L
Linus Torvalds 已提交
115 116 117 118 119 120 121 122 123 124
{
	int i;
	page *= UNIT_PAGES;
	for (i = 0; i < UNIT_PAGES; i++, page++)
		__set_tlb_bus(trident, page, (unsigned long)trident->tlb.silent_page.area, trident->tlb.silent_page.addr);
}

#endif /* PAGE_SIZE */

/* calculate buffer pointer from offset address */
125
static inline void *offset_ptr(struct snd_trident *trident, int offset)
L
Linus Torvalds 已提交
126 127 128 129 130 131 132 133
{
	char *ptr;
	ptr = page_to_ptr(trident, get_aligned_page(offset));
	ptr += offset % ALIGN_PAGE_SIZE;
	return (void*)ptr;
}

/* first and last (aligned) pages of memory block */
134 135
#define firstpg(blk)	(((struct snd_trident_memblk_arg *)snd_util_memblk_argptr(blk))->first_page)
#define lastpg(blk)	(((struct snd_trident_memblk_arg *)snd_util_memblk_argptr(blk))->last_page)
L
Linus Torvalds 已提交
136 137 138 139

/*
 * search empty pages which may contain given size
 */
140 141
static struct snd_util_memblk *
search_empty(struct snd_util_memhdr *hdr, int size)
L
Linus Torvalds 已提交
142
{
143
	struct snd_util_memblk *blk, *prev;
L
Linus Torvalds 已提交
144 145 146 147 148 149 150
	int page, psize;
	struct list_head *p;

	psize = get_aligned_page(size + ALIGN_PAGE_SIZE -1);
	prev = NULL;
	page = 0;
	list_for_each(p, &hdr->block) {
151
		blk = list_entry(p, struct snd_util_memblk, list);
L
Linus Torvalds 已提交
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
		if (page + psize <= firstpg(blk))
			goto __found_pages;
		page = lastpg(blk) + 1;
	}
	if (page + psize > MAX_ALIGN_PAGES)
		return NULL;

__found_pages:
	/* create a new memory block */
	blk = __snd_util_memblk_new(hdr, psize * ALIGN_PAGE_SIZE, p->prev);
	if (blk == NULL)
		return NULL;
	blk->offset = aligned_page_offset(page); /* set aligned offset */
	firstpg(blk) = page;
	lastpg(blk) = page + psize - 1;
	return blk;
}


/*
 * check if the given pointer is valid for pages
 */
static int is_valid_page(unsigned long ptr)
{
	if (ptr & ~0x3fffffffUL) {
177
		snd_printk(KERN_ERR "max memory size is 1GB!!\n");
L
Linus Torvalds 已提交
178 179 180
		return 0;
	}
	if (ptr & (SNDRV_TRIDENT_PAGE_SIZE-1)) {
181
		snd_printk(KERN_ERR "page is not aligned\n");
L
Linus Torvalds 已提交
182 183 184 185 186 187 188 189
		return 0;
	}
	return 1;
}

/*
 * page allocation for DMA (Scatter-Gather version)
 */
190 191 192
static struct snd_util_memblk *
snd_trident_alloc_sg_pages(struct snd_trident *trident,
			   struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
193
{
194 195 196
	struct snd_util_memhdr *hdr;
	struct snd_util_memblk *blk;
	struct snd_pcm_runtime *runtime = substream->runtime;
L
Linus Torvalds 已提交
197 198 199 200 201 202 203 204 205
	int idx, page;
	struct snd_sg_buf *sgbuf = snd_pcm_substream_sgbuf(substream);

	snd_assert(runtime->dma_bytes > 0 && runtime->dma_bytes <= SNDRV_TRIDENT_MAX_PAGES * SNDRV_TRIDENT_PAGE_SIZE, return NULL);
	hdr = trident->tlb.memhdr;
	snd_assert(hdr != NULL, return NULL);

	

206
	mutex_lock(&hdr->block_mutex);
L
Linus Torvalds 已提交
207 208
	blk = search_empty(hdr, runtime->dma_bytes);
	if (blk == NULL) {
209
		mutex_unlock(&hdr->block_mutex);
L
Linus Torvalds 已提交
210 211 212 213 214
		return NULL;
	}
	if (lastpg(blk) - firstpg(blk) >= sgbuf->pages) {
		snd_printk(KERN_ERR "page calculation doesn't match: allocated pages = %d, trident = %d/%d\n", sgbuf->pages, firstpg(blk), lastpg(blk));
		__snd_util_mem_free(hdr, blk);
215
		mutex_unlock(&hdr->block_mutex);
L
Linus Torvalds 已提交
216 217 218 219 220 221 222 223 224 225
		return NULL;
	}
			   
	/* set TLB entries */
	idx = 0;
	for (page = firstpg(blk); page <= lastpg(blk); page++, idx++) {
		dma_addr_t addr = sgbuf->table[idx].addr;
		unsigned long ptr = (unsigned long)sgbuf->table[idx].buf;
		if (! is_valid_page(addr)) {
			__snd_util_mem_free(hdr, blk);
226
			mutex_unlock(&hdr->block_mutex);
L
Linus Torvalds 已提交
227 228 229 230
			return NULL;
		}
		set_tlb_bus(trident, page, ptr, addr);
	}
231
	mutex_unlock(&hdr->block_mutex);
L
Linus Torvalds 已提交
232 233 234 235 236 237
	return blk;
}

/*
 * page allocation for DMA (contiguous version)
 */
238 239 240
static struct snd_util_memblk *
snd_trident_alloc_cont_pages(struct snd_trident *trident,
			     struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
241
{
242 243
	struct snd_util_memhdr *hdr;
	struct snd_util_memblk *blk;
L
Linus Torvalds 已提交
244
	int page;
245
	struct snd_pcm_runtime *runtime = substream->runtime;
L
Linus Torvalds 已提交
246 247 248 249 250 251 252
	dma_addr_t addr;
	unsigned long ptr;

	snd_assert(runtime->dma_bytes> 0 && runtime->dma_bytes <= SNDRV_TRIDENT_MAX_PAGES * SNDRV_TRIDENT_PAGE_SIZE, return NULL);
	hdr = trident->tlb.memhdr;
	snd_assert(hdr != NULL, return NULL);

253
	mutex_lock(&hdr->block_mutex);
L
Linus Torvalds 已提交
254 255
	blk = search_empty(hdr, runtime->dma_bytes);
	if (blk == NULL) {
256
		mutex_unlock(&hdr->block_mutex);
L
Linus Torvalds 已提交
257 258 259 260 261 262 263 264 265 266
		return NULL;
	}
			   
	/* set TLB entries */
	addr = runtime->dma_addr;
	ptr = (unsigned long)runtime->dma_area;
	for (page = firstpg(blk); page <= lastpg(blk); page++,
	     ptr += SNDRV_TRIDENT_PAGE_SIZE, addr += SNDRV_TRIDENT_PAGE_SIZE) {
		if (! is_valid_page(addr)) {
			__snd_util_mem_free(hdr, blk);
267
			mutex_unlock(&hdr->block_mutex);
L
Linus Torvalds 已提交
268 269 270 271
			return NULL;
		}
		set_tlb_bus(trident, page, ptr, addr);
	}
272
	mutex_unlock(&hdr->block_mutex);
L
Linus Torvalds 已提交
273 274 275 276 277 278
	return blk;
}

/*
 * page allocation for DMA
 */
279 280 281
struct snd_util_memblk *
snd_trident_alloc_pages(struct snd_trident *trident,
			struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
282 283 284 285 286 287 288 289 290 291 292 293 294
{
	snd_assert(trident != NULL, return NULL);
	snd_assert(substream != NULL, return NULL);
	if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV_SG)
		return snd_trident_alloc_sg_pages(trident, substream);
	else
		return snd_trident_alloc_cont_pages(trident, substream);
}


/*
 * release DMA buffer from page table
 */
295 296
int snd_trident_free_pages(struct snd_trident *trident,
			   struct snd_util_memblk *blk)
L
Linus Torvalds 已提交
297
{
298
	struct snd_util_memhdr *hdr;
L
Linus Torvalds 已提交
299 300 301 302 303 304
	int page;

	snd_assert(trident != NULL, return -EINVAL);
	snd_assert(blk != NULL, return -EINVAL);

	hdr = trident->tlb.memhdr;
305
	mutex_lock(&hdr->block_mutex);
L
Linus Torvalds 已提交
306 307 308 309 310
	/* reset TLB entries */
	for (page = firstpg(blk); page <= lastpg(blk); page++)
		set_silent_tlb(trident, page);
	/* free memory block */
	__snd_util_mem_free(hdr, blk);
311
	mutex_unlock(&hdr->block_mutex);
L
Linus Torvalds 已提交
312 313 314 315 316 317 318 319 320 321 322 323 324
	return 0;
}


/*----------------------------------------------------------------
 * memory allocation using multiple pages (for synth)
 *----------------------------------------------------------------
 * Unlike the DMA allocation above, non-contiguous pages are
 * assigned to TLB.
 *----------------------------------------------------------------*/

/*
 */
325 326
static int synth_alloc_pages(struct snd_trident *hw, struct snd_util_memblk *blk);
static int synth_free_pages(struct snd_trident *hw, struct snd_util_memblk *blk);
L
Linus Torvalds 已提交
327 328 329 330

/*
 * allocate a synth sample area
 */
331 332
struct snd_util_memblk *
snd_trident_synth_alloc(struct snd_trident *hw, unsigned int size)
L
Linus Torvalds 已提交
333
{
334 335
	struct snd_util_memblk *blk;
	struct snd_util_memhdr *hdr = hw->tlb.memhdr; 
L
Linus Torvalds 已提交
336

337
	mutex_lock(&hdr->block_mutex);
L
Linus Torvalds 已提交
338 339
	blk = __snd_util_mem_alloc(hdr, size);
	if (blk == NULL) {
340
		mutex_unlock(&hdr->block_mutex);
L
Linus Torvalds 已提交
341 342 343 344
		return NULL;
	}
	if (synth_alloc_pages(hw, blk)) {
		__snd_util_mem_free(hdr, blk);
345
		mutex_unlock(&hdr->block_mutex);
L
Linus Torvalds 已提交
346 347
		return NULL;
	}
348
	mutex_unlock(&hdr->block_mutex);
L
Linus Torvalds 已提交
349 350 351
	return blk;
}

352
EXPORT_SYMBOL(snd_trident_synth_alloc);
L
Linus Torvalds 已提交
353 354 355 356 357

/*
 * free a synth sample area
 */
int
358
snd_trident_synth_free(struct snd_trident *hw, struct snd_util_memblk *blk)
L
Linus Torvalds 已提交
359
{
360
	struct snd_util_memhdr *hdr = hw->tlb.memhdr; 
L
Linus Torvalds 已提交
361

362
	mutex_lock(&hdr->block_mutex);
L
Linus Torvalds 已提交
363 364
	synth_free_pages(hw, blk);
	 __snd_util_mem_free(hdr, blk);
365
	mutex_unlock(&hdr->block_mutex);
L
Linus Torvalds 已提交
366 367 368
	return 0;
}

369
EXPORT_SYMBOL(snd_trident_synth_free);
L
Linus Torvalds 已提交
370 371 372 373

/*
 * reset TLB entry and free kernel page
 */
374
static void clear_tlb(struct snd_trident *trident, int page)
L
Linus Torvalds 已提交
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
{
	void *ptr = page_to_ptr(trident, page);
	dma_addr_t addr = page_to_addr(trident, page);
	set_silent_tlb(trident, page);
	if (ptr) {
		struct snd_dma_buffer dmab;
		dmab.dev.type = SNDRV_DMA_TYPE_DEV;
		dmab.dev.dev = snd_dma_pci_data(trident->pci);
		dmab.area = ptr;
		dmab.addr = addr;
		dmab.bytes = ALIGN_PAGE_SIZE;
		snd_dma_free_pages(&dmab);
	}
}

/* check new allocation range */
391 392 393
static void get_single_page_range(struct snd_util_memhdr *hdr,
				  struct snd_util_memblk *blk,
				  int *first_page_ret, int *last_page_ret)
L
Linus Torvalds 已提交
394 395
{
	struct list_head *p;
396
	struct snd_util_memblk *q;
L
Linus Torvalds 已提交
397 398 399
	int first_page, last_page;
	first_page = firstpg(blk);
	if ((p = blk->list.prev) != &hdr->block) {
400
		q = list_entry(p, struct snd_util_memblk, list);
L
Linus Torvalds 已提交
401 402 403 404 405
		if (lastpg(q) == first_page)
			first_page++;  /* first page was already allocated */
	}
	last_page = lastpg(blk);
	if ((p = blk->list.next) != &hdr->block) {
406
		q = list_entry(p, struct snd_util_memblk, list);
L
Linus Torvalds 已提交
407 408 409 410 411 412 413 414 415 416
		if (firstpg(q) == last_page)
			last_page--; /* last page was already allocated */
	}
	*first_page_ret = first_page;
	*last_page_ret = last_page;
}

/*
 * allocate kernel pages and assign them to TLB
 */
417
static int synth_alloc_pages(struct snd_trident *hw, struct snd_util_memblk *blk)
L
Linus Torvalds 已提交
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
{
	int page, first_page, last_page;
	struct snd_dma_buffer dmab;

	firstpg(blk) = get_aligned_page(blk->offset);
	lastpg(blk) = get_aligned_page(blk->offset + blk->size - 1);
	get_single_page_range(hw->tlb.memhdr, blk, &first_page, &last_page);

	/* allocate a kernel page for each Trident page -
	 * fortunately Trident page size and kernel PAGE_SIZE is identical!
	 */
	for (page = first_page; page <= last_page; page++) {
		if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(hw->pci),
					ALIGN_PAGE_SIZE, &dmab) < 0)
			goto __fail;
		if (! is_valid_page(dmab.addr)) {
			snd_dma_free_pages(&dmab);
			goto __fail;
		}
		set_tlb_bus(hw, page, (unsigned long)dmab.area, dmab.addr);
	}
	return 0;

__fail:
	/* release allocated pages */
	last_page = page - 1;
	for (page = first_page; page <= last_page; page++)
		clear_tlb(hw, page);

	return -ENOMEM;
}

/*
 * free pages
 */
453
static int synth_free_pages(struct snd_trident *trident, struct snd_util_memblk *blk)
L
Linus Torvalds 已提交
454 455 456 457 458 459 460 461 462 463 464 465 466
{
	int page, first_page, last_page;

	get_single_page_range(trident->tlb.memhdr, blk, &first_page, &last_page);
	for (page = first_page; page <= last_page; page++)
		clear_tlb(trident, page);

	return 0;
}

/*
 * copy_from_user(blk + offset, data, size)
 */
467 468 469
int snd_trident_synth_copy_from_user(struct snd_trident *trident,
				     struct snd_util_memblk *blk,
				     int offset, const char __user *data, int size)
L
Linus Torvalds 已提交
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
{
	int page, nextofs, end_offset, temp, temp1;

	offset += blk->offset;
	end_offset = offset + size;
	page = get_aligned_page(offset) + 1;
	do {
		nextofs = aligned_page_offset(page);
		temp = nextofs - offset;
		temp1 = end_offset - offset;
		if (temp1 < temp)
			temp = temp1;
		if (copy_from_user(offset_ptr(trident, offset), data, temp))
			return -EFAULT;
		offset = nextofs;
		data += temp;
		page++;
	} while (offset < end_offset);
	return 0;
}

491
EXPORT_SYMBOL(snd_trident_synth_copy_from_user);