blob: 363b67e3a9e7e814260ecd7163e876a394db418c [file] [log] [blame]
Wai Yew CHAY8cc72362009-05-14 08:05:58 +02001/**
2 * Copyright (C) 2008, Creative Technology Ltd. All Rights Reserved.
3 *
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.
7 *
8 * @File ctvmem.c
9 *
10 * @Brief
11 * This file contains the implementation of virtual memory management object
12 * for card device.
13 *
14 * @Author Liu Chun
15 * @Date Apr 1 2008
16 */
17
18#include "ctvmem.h"
19#include <linux/slab.h>
20#include <linux/mm.h>
21#include <asm/page.h> /* for PAGE_SIZE macro definition */
22#include <linux/io.h>
23#include <asm/pgtable.h>
24
25#define CT_PTES_PER_PAGE (PAGE_SIZE / sizeof(void *))
26#define CT_ADDRS_PER_PAGE (CT_PTES_PER_PAGE * PAGE_SIZE)
27
28/* *
29 * Find or create vm block based on requested @size.
30 * @size must be page aligned.
31 * */
32static struct ct_vm_block *
33get_vm_block(struct ct_vm *vm, unsigned int size)
34{
35 struct ct_vm_block *block = NULL, *entry = NULL;
36 struct list_head *pos = NULL;
37
Takashi Iwai8a4259b2009-06-02 08:40:51 +020038 mutex_lock(&vm->lock);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +020039 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 */
43 }
44 if (pos == &vm->unused)
Takashi Iwai8a4259b2009-06-02 08:40:51 +020045 goto out;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +020046
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);
51 vm->size -= size;
Takashi Iwai8a4259b2009-06-02 08:40:51 +020052 block = entry;
53 goto out;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +020054 }
55
56 block = kzalloc(sizeof(*block), GFP_KERNEL);
57 if (NULL == block)
Takashi Iwai8a4259b2009-06-02 08:40:51 +020058 goto out;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +020059
60 block->addr = entry->addr;
61 block->size = size;
62 list_add(&block->list, &vm->used);
63 entry->addr += size;
64 entry->size -= size;
65 vm->size -= size;
66
Takashi Iwai8a4259b2009-06-02 08:40:51 +020067 out:
68 mutex_unlock(&vm->lock);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +020069 return block;
70}
71
72static void put_vm_block(struct ct_vm *vm, struct ct_vm_block *block)
73{
74 struct ct_vm_block *entry = NULL, *pre_ent = NULL;
75 struct list_head *pos = NULL, *pre = NULL;
76
Takashi Iwai8a4259b2009-06-02 08:40:51 +020077 mutex_lock(&vm->lock);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +020078 list_del(&block->list);
79 vm->size += block->size;
80
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 */
85 }
86 if (pos == &vm->unused) {
87 list_add_tail(&block->list, &vm->unused);
88 entry = block;
89 } else {
90 if ((block->addr + block->size) == entry->addr) {
91 entry->addr = block->addr;
92 entry->size += block->size;
93 kfree(block);
94 } else {
95 __list_add(&block->list, pos->prev, pos);
96 entry = block;
97 }
98 }
99
100 pos = &entry->list;
101 pre = pos->prev;
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)
106 break;
107
108 pre_ent->size += entry->size;
109 list_del(pos);
110 kfree(entry);
111 pos = pre;
112 pre = pos->prev;
113 }
Takashi Iwai8a4259b2009-06-02 08:40:51 +0200114 mutex_unlock(&vm->lock);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200115}
116
117/* Map host addr (kmalloced/vmalloced) to device logical addr. */
118static struct ct_vm_block *
119ct_vm_map(struct ct_vm *vm, void *host_addr, int size)
120{
121 struct ct_vm_block *block = NULL;
122 unsigned long pte_start;
123 unsigned long i;
124 unsigned long pages;
125 unsigned long start_phys;
126 unsigned long *ptp;
127
128 /* do mapping */
129 if ((unsigned long)host_addr >= VMALLOC_START) {
Takashi Iwaib3e0afe2009-05-14 15:19:30 +0200130 printk(KERN_ERR "ctxfi: "
131 "Fail! Not support vmalloced addr now!\n");
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200132 return NULL;
133 }
134
135 if (size > vm->size) {
Takashi Iwaib3e0afe2009-05-14 15:19:30 +0200136 printk(KERN_ERR "ctxfi: Fail! No sufficient device virtural "
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200137 "memory space available!\n");
138 return NULL;
139 }
140
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;
144
145 ptp = vm->ptp[0];
146
147 block = get_vm_block(vm, (pages << PAGE_SHIFT));
148 if (block == NULL) {
Takashi Iwaib3e0afe2009-05-14 15:19:30 +0200149 printk(KERN_ERR "ctxfi: No virtual memory block that is big "
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200150 "enough to allocate!\n");
151 return NULL;
152 }
153
154 pte_start = (block->addr >> PAGE_SHIFT);
155 for (i = 0; i < pages; i++)
156 ptp[pte_start+i] = start_phys + (i << PAGE_SHIFT);
157
158 block->addr += (virt_to_phys(host_addr) & (~PAGE_MASK));
159 block->size = size;
160
161 return block;
162}
163
164static void ct_vm_unmap(struct ct_vm *vm, struct ct_vm_block *block)
165{
166 /* do unmapping */
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);
171}
172
173/* *
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.
177 * */
178static void *
179ct_get_ptp_virt(struct ct_vm *vm, int index)
180{
181 void *addr;
182
183 addr = (index >= CT_PTP_NUM) ? NULL : vm->ptp[index];
184
185 return addr;
186}
187
188int ct_vm_create(struct ct_vm **rvm)
189{
190 struct ct_vm *vm;
191 struct ct_vm_block *block;
192 int i;
193
194 *rvm = NULL;
195
196 vm = kzalloc(sizeof(*vm), GFP_KERNEL);
197 if (NULL == vm)
198 return -ENOMEM;
199
Takashi Iwai8a4259b2009-06-02 08:40:51 +0200200 mutex_init(&vm->lock);
201
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200202 /* 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])
206 break;
207 }
208 if (!i) {
209 /* no page table pages are allocated */
210 kfree(vm);
211 return -ENOMEM;
212 }
213 vm->size = CT_ADDRS_PER_PAGE * i;
214 /* Initialise remaining ptps */
215 for (; i < CT_PTP_NUM; i++)
216 vm->ptp[i] = NULL;
217
218 vm->map = ct_vm_map;
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);
224 if (NULL != block) {
225 block->addr = 0;
226 block->size = vm->size;
227 list_add(&block->list, &vm->unused);
228 }
229
230 *rvm = vm;
231 return 0;
232}
233
234/* The caller must ensure no mapping pages are being used
235 * by hardware before calling this function */
236void ct_vm_destroy(struct ct_vm *vm)
237{
238 int i;
239 struct list_head *pos = NULL;
240 struct ct_vm_block *entry = NULL;
241
242 /* free used and unused list nodes */
243 while (!list_empty(&vm->used)) {
244 pos = vm->used.next;
245 list_del(pos);
246 entry = list_entry(pos, struct ct_vm_block, list);
247 kfree(entry);
248 }
249 while (!list_empty(&vm->unused)) {
250 pos = vm->unused.next;
251 list_del(pos);
252 entry = list_entry(pos, struct ct_vm_block, list);
253 kfree(entry);
254 }
255
256 /* free allocated page table pages */
257 for (i = 0; i < CT_PTP_NUM; i++)
258 kfree(vm->ptp[i]);
259
260 vm->size = 0;
261
262 kfree(vm);
263}