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>
21 #include <asm/page.h> /* for PAGE_SIZE macro definition */
23 #include <asm/pgtable.h>
25 #define CT_PTES_PER_PAGE (PAGE_SIZE / sizeof(void *))
26 #define CT_ADDRS_PER_PAGE (CT_PTES_PER_PAGE * PAGE_SIZE)
29 * Find or create vm block based on requested @size.
30 * @size must be page aligned.
32 static struct ct_vm_block *
33 get_vm_block(struct ct_vm *vm, unsigned int size)
35 struct ct_vm_block *block = NULL, *entry = NULL;
36 struct list_head *pos = NULL;
38 mutex_lock(&vm->lock);
39 list_for_each(pos, &vm->unused) {
40 entry = list_entry(pos, struct ct_vm_block, list);
41 if (entry->size >= size)
42 break; /* found a block that is big enough */
44 if (pos == &vm->unused)
47 if (entry->size == size) {
48 /* Move the vm node from unused list to used list directly */
49 list_del(&entry->list);
50 list_add(&entry->list, &vm->used);
56 block = kzalloc(sizeof(*block), GFP_KERNEL);
60 block->addr = entry->addr;
62 list_add(&block->list, &vm->used);
68 mutex_unlock(&vm->lock);
72 static void put_vm_block(struct ct_vm *vm, struct ct_vm_block *block)
74 struct ct_vm_block *entry = NULL, *pre_ent = NULL;
75 struct list_head *pos = NULL, *pre = NULL;
77 mutex_lock(&vm->lock);
78 list_del(&block->list);
79 vm->size += block->size;
81 list_for_each(pos, &vm->unused) {
82 entry = list_entry(pos, struct ct_vm_block, list);
83 if (entry->addr >= (block->addr + block->size))
84 break; /* found a position */
86 if (pos == &vm->unused) {
87 list_add_tail(&block->list, &vm->unused);
90 if ((block->addr + block->size) == entry->addr) {
91 entry->addr = block->addr;
92 entry->size += block->size;
95 __list_add(&block->list, pos->prev, pos);
102 while (pre != &vm->unused) {
103 entry = list_entry(pos, struct ct_vm_block, list);
104 pre_ent = list_entry(pre, struct ct_vm_block, list);
105 if ((pre_ent->addr + pre_ent->size) > entry->addr)
108 pre_ent->size += entry->size;
114 mutex_unlock(&vm->lock);
117 /* Map host addr (kmalloced/vmalloced) to device logical addr. */
118 static struct ct_vm_block *
119 ct_vm_map(struct ct_vm *vm, void *host_addr, int size)
121 struct ct_vm_block *block = NULL;
122 unsigned long pte_start;
125 unsigned long start_phys;
129 if ((unsigned long)host_addr >= VMALLOC_START) {
130 printk(KERN_ERR "ctxfi: "
131 "Fail! Not support vmalloced addr now!\n");
135 if (size > vm->size) {
136 printk(KERN_ERR "ctxfi: Fail! No sufficient device virtural "
137 "memory space available!\n");
141 start_phys = (virt_to_phys(host_addr) & PAGE_MASK);
142 pages = (PAGE_ALIGN(virt_to_phys(host_addr) + size)
143 - start_phys) >> PAGE_SHIFT;
147 block = get_vm_block(vm, (pages << PAGE_SHIFT));
149 printk(KERN_ERR "ctxfi: No virtual memory block that is big "
150 "enough to allocate!\n");
154 pte_start = (block->addr >> PAGE_SHIFT);
155 for (i = 0; i < pages; i++)
156 ptp[pte_start+i] = start_phys + (i << PAGE_SHIFT);
158 block->addr += (virt_to_phys(host_addr) & (~PAGE_MASK));
164 static void ct_vm_unmap(struct ct_vm *vm, struct ct_vm_block *block)
167 block->size = ((block->addr + block->size + PAGE_SIZE - 1)
168 & PAGE_MASK) - (block->addr & PAGE_MASK);
169 block->addr &= PAGE_MASK;
170 put_vm_block(vm, block);
174 * return the host (kmalloced) addr of the @index-th device
175 * page talbe page on success, or NULL on failure.
176 * The first returned NULL indicates the termination.
179 ct_get_ptp_virt(struct ct_vm *vm, int index)
183 addr = (index >= CT_PTP_NUM) ? NULL : vm->ptp[index];
188 int ct_vm_create(struct ct_vm **rvm)
191 struct ct_vm_block *block;
196 vm = kzalloc(sizeof(*vm), GFP_KERNEL);
200 mutex_init(&vm->lock);
202 /* Allocate page table pages */
203 for (i = 0; i < CT_PTP_NUM; i++) {
204 vm->ptp[i] = kmalloc(PAGE_SIZE, GFP_KERNEL);
205 if (NULL == vm->ptp[i])
209 /* no page table pages are allocated */
213 vm->size = CT_ADDRS_PER_PAGE * i;
214 /* Initialise remaining ptps */
215 for (; i < CT_PTP_NUM; i++)
219 vm->unmap = ct_vm_unmap;
220 vm->get_ptp_virt = ct_get_ptp_virt;
221 INIT_LIST_HEAD(&vm->unused);
222 INIT_LIST_HEAD(&vm->used);
223 block = kzalloc(sizeof(*block), GFP_KERNEL);
226 block->size = vm->size;
227 list_add(&block->list, &vm->unused);
234 /* The caller must ensure no mapping pages are being used
235 * by hardware before calling this function */
236 void ct_vm_destroy(struct ct_vm *vm)
239 struct list_head *pos = NULL;
240 struct ct_vm_block *entry = NULL;
242 /* free used and unused list nodes */
243 while (!list_empty(&vm->used)) {
246 entry = list_entry(pos, struct ct_vm_block, list);
249 while (!list_empty(&vm->unused)) {
250 pos = vm->unused.next;
252 entry = list_entry(pos, struct ct_vm_block, list);
256 /* free allocated page table pages */
257 for (i = 0; i < CT_PTP_NUM; i++)