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 <asm/pgtable.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 = NULL;
35 struct list_head *pos = NULL;
37 mutex_lock(&vm->lock);
38 list_for_each(pos, &vm->unused) {
39 entry = list_entry(pos, struct ct_vm_block, list);
40 if (entry->size >= size)
41 break; /* found a block that is big enough */
43 if (pos == &vm->unused)
46 if (entry->size == size) {
47 /* Move the vm node from unused list to used list directly */
48 list_del(&entry->list);
49 list_add(&entry->list, &vm->used);
55 block = kzalloc(sizeof(*block), GFP_KERNEL);
59 block->addr = entry->addr;
61 list_add(&block->list, &vm->used);
67 mutex_unlock(&vm->lock);
71 static void put_vm_block(struct ct_vm *vm, struct ct_vm_block *block)
73 struct ct_vm_block *entry = NULL, *pre_ent = NULL;
74 struct list_head *pos = NULL, *pre = NULL;
76 mutex_lock(&vm->lock);
77 list_del(&block->list);
78 vm->size += block->size;
80 list_for_each(pos, &vm->unused) {
81 entry = list_entry(pos, struct ct_vm_block, list);
82 if (entry->addr >= (block->addr + block->size))
83 break; /* found a position */
85 if (pos == &vm->unused) {
86 list_add_tail(&block->list, &vm->unused);
89 if ((block->addr + block->size) == entry->addr) {
90 entry->addr = block->addr;
91 entry->size += block->size;
94 __list_add(&block->list, pos->prev, pos);
101 while (pre != &vm->unused) {
102 entry = list_entry(pos, struct ct_vm_block, list);
103 pre_ent = list_entry(pre, struct ct_vm_block, list);
104 if ((pre_ent->addr + pre_ent->size) > entry->addr)
107 pre_ent->size += entry->size;
113 mutex_unlock(&vm->lock);
116 /* Map host addr (kmalloced/vmalloced) to device logical addr. */
117 static struct ct_vm_block *
118 ct_vm_map(struct ct_vm *vm, void *host_addr, int size)
120 struct ct_vm_block *block = NULL;
121 unsigned long pte_start;
124 unsigned long start_phys;
128 if ((unsigned long)host_addr >= VMALLOC_START) {
129 printk(KERN_ERR "ctxfi: "
130 "Fail! Not support vmalloced addr now!\n");
134 if (size > vm->size) {
135 printk(KERN_ERR "ctxfi: Fail! No sufficient device virtural "
136 "memory space available!\n");
140 start_phys = (virt_to_phys(host_addr) & CT_PAGE_MASK);
141 pages = (CT_PAGE_ALIGN(virt_to_phys(host_addr) + size)
142 - start_phys) >> CT_PAGE_SHIFT;
146 block = get_vm_block(vm, (pages << CT_PAGE_SHIFT));
148 printk(KERN_ERR "ctxfi: No virtual memory block that is big "
149 "enough to allocate!\n");
153 pte_start = (block->addr >> CT_PAGE_SHIFT);
154 for (i = 0; i < pages; i++)
155 ptp[pte_start+i] = start_phys + (i << CT_PAGE_SHIFT);
157 block->addr += (virt_to_phys(host_addr) & (~CT_PAGE_MASK));
163 static void ct_vm_unmap(struct ct_vm *vm, struct ct_vm_block *block)
166 block->size = ((block->addr + block->size + CT_PAGE_SIZE - 1)
167 & CT_PAGE_MASK) - (block->addr & CT_PAGE_MASK);
168 block->addr &= CT_PAGE_MASK;
169 put_vm_block(vm, block);
173 * return the host (kmalloced) addr of the @index-th device
174 * page talbe page on success, or NULL on failure.
175 * The first returned NULL indicates the termination.
178 ct_get_ptp_virt(struct ct_vm *vm, int index)
182 addr = (index >= CT_PTP_NUM) ? NULL : vm->ptp[index];
187 int ct_vm_create(struct ct_vm **rvm)
190 struct ct_vm_block *block;
195 vm = kzalloc(sizeof(*vm), GFP_KERNEL);
199 mutex_init(&vm->lock);
201 /* Allocate page table pages */
202 for (i = 0; i < CT_PTP_NUM; i++) {
203 vm->ptp[i] = kmalloc(PAGE_SIZE, GFP_KERNEL);
204 if (NULL == vm->ptp[i])
208 /* no page table pages are allocated */
212 vm->size = CT_ADDRS_PER_PAGE * i;
213 /* Initialise remaining ptps */
214 for (; i < CT_PTP_NUM; i++)
218 vm->unmap = ct_vm_unmap;
219 vm->get_ptp_virt = ct_get_ptp_virt;
220 INIT_LIST_HEAD(&vm->unused);
221 INIT_LIST_HEAD(&vm->used);
222 block = kzalloc(sizeof(*block), GFP_KERNEL);
225 block->size = vm->size;
226 list_add(&block->list, &vm->unused);
233 /* The caller must ensure no mapping pages are being used
234 * by hardware before calling this function */
235 void ct_vm_destroy(struct ct_vm *vm)
238 struct list_head *pos = NULL;
239 struct ct_vm_block *entry = NULL;
241 /* free used and unused list nodes */
242 while (!list_empty(&vm->used)) {
245 entry = list_entry(pos, struct ct_vm_block, list);
248 while (!list_empty(&vm->unused)) {
249 pos = vm->unused.next;
251 entry = list_entry(pos, struct ct_vm_block, list);
255 /* free allocated page table pages */
256 for (i = 0; i < CT_PTP_NUM; i++)