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 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);
54 block = kzalloc(sizeof(*block), GFP_KERNEL);
58 block->addr = entry->addr;
60 list_add(&block->list, &vm->used);
68 static void put_vm_block(struct ct_vm *vm, struct ct_vm_block *block)
70 struct ct_vm_block *entry = NULL, *pre_ent = NULL;
71 struct list_head *pos = NULL, *pre = NULL;
73 list_del(&block->list);
74 vm->size += block->size;
76 list_for_each(pos, &vm->unused) {
77 entry = list_entry(pos, struct ct_vm_block, list);
78 if (entry->addr >= (block->addr + block->size))
79 break; /* found a position */
81 if (pos == &vm->unused) {
82 list_add_tail(&block->list, &vm->unused);
85 if ((block->addr + block->size) == entry->addr) {
86 entry->addr = block->addr;
87 entry->size += block->size;
90 __list_add(&block->list, pos->prev, pos);
97 while (pre != &vm->unused) {
98 entry = list_entry(pos, struct ct_vm_block, list);
99 pre_ent = list_entry(pre, struct ct_vm_block, list);
100 if ((pre_ent->addr + pre_ent->size) > entry->addr)
103 pre_ent->size += entry->size;
111 /* Map host addr (kmalloced/vmalloced) to device logical addr. */
112 static struct ct_vm_block *
113 ct_vm_map(struct ct_vm *vm, void *host_addr, int size)
115 struct ct_vm_block *block = NULL;
116 unsigned long pte_start;
119 unsigned long start_phys;
123 if ((unsigned long)host_addr >= VMALLOC_START) {
124 printk(KERN_ERR "ctxfi: "
125 "Fail! Not support vmalloced addr now!\n");
129 if (size > vm->size) {
130 printk(KERN_ERR "ctxfi: Fail! No sufficient device virtural "
131 "memory space available!\n");
135 start_phys = (virt_to_phys(host_addr) & PAGE_MASK);
136 pages = (PAGE_ALIGN(virt_to_phys(host_addr) + size)
137 - start_phys) >> PAGE_SHIFT;
141 block = get_vm_block(vm, (pages << PAGE_SHIFT));
143 printk(KERN_ERR "ctxfi: No virtual memory block that is big "
144 "enough to allocate!\n");
148 pte_start = (block->addr >> PAGE_SHIFT);
149 for (i = 0; i < pages; i++)
150 ptp[pte_start+i] = start_phys + (i << PAGE_SHIFT);
152 block->addr += (virt_to_phys(host_addr) & (~PAGE_MASK));
158 static void ct_vm_unmap(struct ct_vm *vm, struct ct_vm_block *block)
161 block->size = ((block->addr + block->size + PAGE_SIZE - 1)
162 & PAGE_MASK) - (block->addr & PAGE_MASK);
163 block->addr &= PAGE_MASK;
164 put_vm_block(vm, block);
168 * return the host (kmalloced) addr of the @index-th device
169 * page talbe page on success, or NULL on failure.
170 * The first returned NULL indicates the termination.
173 ct_get_ptp_virt(struct ct_vm *vm, int index)
177 addr = (index >= CT_PTP_NUM) ? NULL : vm->ptp[index];
182 int ct_vm_create(struct ct_vm **rvm)
185 struct ct_vm_block *block;
190 vm = kzalloc(sizeof(*vm), GFP_KERNEL);
194 /* Allocate page table pages */
195 for (i = 0; i < CT_PTP_NUM; i++) {
196 vm->ptp[i] = kmalloc(PAGE_SIZE, GFP_KERNEL);
197 if (NULL == vm->ptp[i])
201 /* no page table pages are allocated */
205 vm->size = CT_ADDRS_PER_PAGE * i;
206 /* Initialise remaining ptps */
207 for (; i < CT_PTP_NUM; i++)
211 vm->unmap = ct_vm_unmap;
212 vm->get_ptp_virt = ct_get_ptp_virt;
213 INIT_LIST_HEAD(&vm->unused);
214 INIT_LIST_HEAD(&vm->used);
215 block = kzalloc(sizeof(*block), GFP_KERNEL);
218 block->size = vm->size;
219 list_add(&block->list, &vm->unused);
226 /* The caller must ensure no mapping pages are being used
227 * by hardware before calling this function */
228 void ct_vm_destroy(struct ct_vm *vm)
231 struct list_head *pos = NULL;
232 struct ct_vm_block *entry = NULL;
234 /* free used and unused list nodes */
235 while (!list_empty(&vm->used)) {
238 entry = list_entry(pos, struct ct_vm_block, list);
241 while (!list_empty(&vm->unused)) {
242 pos = vm->unused.next;
244 entry = list_entry(pos, struct ct_vm_block, list);
248 /* free allocated page table pages */
249 for (i = 0; i < CT_PTP_NUM; i++)