2 * Copyright (C) 2008, Creative Technology Ltd. All Rights Reserved.
4 * This source file is released under GPL v2 license (no other versions).
5 * See the COPYING file included in the main directory of this source
6 * distribution for the license terms and conditions.
11 * This file contains the implementation of virtual memory management object
19 #include <linux/slab.h>
22 #include <sound/pcm.h>
24 #define CT_PTES_PER_PAGE (CT_PAGE_SIZE / sizeof(void *))
25 #define CT_ADDRS_PER_PAGE (CT_PTES_PER_PAGE * CT_PAGE_SIZE)
28 * Find or create vm block based on requested @size.
29 * @size must be page aligned.
31 static struct ct_vm_block *
32 get_vm_block(struct ct_vm *vm, unsigned int size)
34 struct ct_vm_block *block = NULL, *entry;
35 struct list_head *pos;
37 size = CT_PAGE_ALIGN(size);
38 if (size > vm->size) {
39 pr_err("ctxfi: Fail! No sufficient device virtual memory space available!\n");
43 mutex_lock(&vm->lock);
44 list_for_each(pos, &vm->unused) {
45 entry = list_entry(pos, struct ct_vm_block, list);
46 if (entry->size >= size)
47 break; /* found a block that is big enough */
49 if (pos == &vm->unused)
52 if (entry->size == size) {
53 /* Move the vm node from unused list to used list directly */
54 list_move(&entry->list, &vm->used);
60 block = kzalloc(sizeof(*block), GFP_KERNEL);
64 block->addr = entry->addr;
66 list_add(&block->list, &vm->used);
72 mutex_unlock(&vm->lock);
76 static void put_vm_block(struct ct_vm *vm, struct ct_vm_block *block)
78 struct ct_vm_block *entry, *pre_ent;
79 struct list_head *pos, *pre;
81 block->size = CT_PAGE_ALIGN(block->size);
83 mutex_lock(&vm->lock);
84 list_del(&block->list);
85 vm->size += block->size;
87 list_for_each(pos, &vm->unused) {
88 entry = list_entry(pos, struct ct_vm_block, list);
89 if (entry->addr >= (block->addr + block->size))
90 break; /* found a position */
92 if (pos == &vm->unused) {
93 list_add_tail(&block->list, &vm->unused);
96 if ((block->addr + block->size) == entry->addr) {
97 entry->addr = block->addr;
98 entry->size += block->size;
101 __list_add(&block->list, pos->prev, pos);
108 while (pre != &vm->unused) {
109 entry = list_entry(pos, struct ct_vm_block, list);
110 pre_ent = list_entry(pre, struct ct_vm_block, list);
111 if ((pre_ent->addr + pre_ent->size) > entry->addr)
114 pre_ent->size += entry->size;
120 mutex_unlock(&vm->lock);
123 /* Map host addr (kmalloced/vmalloced) to device logical addr. */
124 static struct ct_vm_block *
125 ct_vm_map(struct ct_vm *vm, struct snd_pcm_substream *substream, int size)
127 struct ct_vm_block *block;
128 unsigned int pte_start;
132 block = get_vm_block(vm, size);
134 pr_err("ctxfi: No virtual memory block that is big enough to allocate!\n");
138 ptp = (unsigned long *)vm->ptp[0].area;
139 pte_start = (block->addr >> CT_PAGE_SHIFT);
140 pages = block->size >> CT_PAGE_SHIFT;
141 for (i = 0; i < pages; i++) {
143 addr = snd_pcm_sgbuf_get_addr(substream, i << CT_PAGE_SHIFT);
144 ptp[pte_start + i] = addr;
151 static void ct_vm_unmap(struct ct_vm *vm, struct ct_vm_block *block)
154 put_vm_block(vm, block);
158 * return the host physical addr of the @index-th device
159 * page table page on success, or ~0UL on failure.
160 * The first returned ~0UL indicates the termination.
163 ct_get_ptp_phys(struct ct_vm *vm, int index)
167 addr = (index >= CT_PTP_NUM) ? ~0UL : vm->ptp[index].addr;
172 int ct_vm_create(struct ct_vm **rvm, struct pci_dev *pci)
175 struct ct_vm_block *block;
180 vm = kzalloc(sizeof(*vm), GFP_KERNEL);
184 mutex_init(&vm->lock);
186 /* Allocate page table pages */
187 for (i = 0; i < CT_PTP_NUM; i++) {
188 err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV,
189 snd_dma_pci_data(pci),
190 PAGE_SIZE, &vm->ptp[i]);
195 /* no page table pages are allocated */
199 vm->size = CT_ADDRS_PER_PAGE * i;
201 vm->unmap = ct_vm_unmap;
202 vm->get_ptp_phys = ct_get_ptp_phys;
203 INIT_LIST_HEAD(&vm->unused);
204 INIT_LIST_HEAD(&vm->used);
205 block = kzalloc(sizeof(*block), GFP_KERNEL);
208 block->size = vm->size;
209 list_add(&block->list, &vm->unused);
216 /* The caller must ensure no mapping pages are being used
217 * by hardware before calling this function */
218 void ct_vm_destroy(struct ct_vm *vm)
221 struct list_head *pos;
222 struct ct_vm_block *entry;
224 /* free used and unused list nodes */
225 while (!list_empty(&vm->used)) {
228 entry = list_entry(pos, struct ct_vm_block, list);
231 while (!list_empty(&vm->unused)) {
232 pos = vm->unused.next;
234 entry = list_entry(pos, struct ct_vm_block, list);
238 /* free allocated page table pages */
239 for (i = 0; i < CT_PTP_NUM; i++)
240 snd_dma_free_pages(&vm->ptp[i]);