blob: 74a03623d047ae2b51b8e17c5dc3daec77a05ad4 [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>
Wai Yew CHAY8cc72362009-05-14 08:05:58 +020021#include <linux/io.h>
22#include <asm/pgtable.h>
23
Takashi Iwaicd391e22009-06-02 15:04:29 +020024#define CT_PTES_PER_PAGE (CT_PAGE_SIZE / sizeof(void *))
25#define CT_ADDRS_PER_PAGE (CT_PTES_PER_PAGE * CT_PAGE_SIZE)
Wai Yew CHAY8cc72362009-05-14 08:05:58 +020026
27/* *
28 * Find or create vm block based on requested @size.
29 * @size must be page aligned.
30 * */
31static struct ct_vm_block *
32get_vm_block(struct ct_vm *vm, unsigned int size)
33{
34 struct ct_vm_block *block = NULL, *entry = NULL;
35 struct list_head *pos = NULL;
36
Takashi Iwai8a4259b2009-06-02 08:40:51 +020037 mutex_lock(&vm->lock);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +020038 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 */
42 }
43 if (pos == &vm->unused)
Takashi Iwai8a4259b2009-06-02 08:40:51 +020044 goto out;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +020045
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);
50 vm->size -= size;
Takashi Iwai8a4259b2009-06-02 08:40:51 +020051 block = entry;
52 goto out;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +020053 }
54
55 block = kzalloc(sizeof(*block), GFP_KERNEL);
56 if (NULL == block)
Takashi Iwai8a4259b2009-06-02 08:40:51 +020057 goto out;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +020058
59 block->addr = entry->addr;
60 block->size = size;
61 list_add(&block->list, &vm->used);
62 entry->addr += size;
63 entry->size -= size;
64 vm->size -= size;
65
Takashi Iwai8a4259b2009-06-02 08:40:51 +020066 out:
67 mutex_unlock(&vm->lock);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +020068 return block;
69}
70
71static void put_vm_block(struct ct_vm *vm, struct ct_vm_block *block)
72{
73 struct ct_vm_block *entry = NULL, *pre_ent = NULL;
74 struct list_head *pos = NULL, *pre = NULL;
75
Takashi Iwai8a4259b2009-06-02 08:40:51 +020076 mutex_lock(&vm->lock);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +020077 list_del(&block->list);
78 vm->size += block->size;
79
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 */
84 }
85 if (pos == &vm->unused) {
86 list_add_tail(&block->list, &vm->unused);
87 entry = block;
88 } else {
89 if ((block->addr + block->size) == entry->addr) {
90 entry->addr = block->addr;
91 entry->size += block->size;
92 kfree(block);
93 } else {
94 __list_add(&block->list, pos->prev, pos);
95 entry = block;
96 }
97 }
98
99 pos = &entry->list;
100 pre = pos->prev;
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)
105 break;
106
107 pre_ent->size += entry->size;
108 list_del(pos);
109 kfree(entry);
110 pos = pre;
111 pre = pos->prev;
112 }
Takashi Iwai8a4259b2009-06-02 08:40:51 +0200113 mutex_unlock(&vm->lock);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200114}
115
116/* Map host addr (kmalloced/vmalloced) to device logical addr. */
117static struct ct_vm_block *
118ct_vm_map(struct ct_vm *vm, void *host_addr, int size)
119{
120 struct ct_vm_block *block = NULL;
121 unsigned long pte_start;
122 unsigned long i;
123 unsigned long pages;
124 unsigned long start_phys;
125 unsigned long *ptp;
126
127 /* do mapping */
128 if ((unsigned long)host_addr >= VMALLOC_START) {
Takashi Iwaib3e0afe2009-05-14 15:19:30 +0200129 printk(KERN_ERR "ctxfi: "
130 "Fail! Not support vmalloced addr now!\n");
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200131 return NULL;
132 }
133
134 if (size > vm->size) {
Takashi Iwaib3e0afe2009-05-14 15:19:30 +0200135 printk(KERN_ERR "ctxfi: Fail! No sufficient device virtural "
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200136 "memory space available!\n");
137 return NULL;
138 }
139
Takashi Iwaicd391e22009-06-02 15:04:29 +0200140 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;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200143
144 ptp = vm->ptp[0];
145
Takashi Iwaicd391e22009-06-02 15:04:29 +0200146 block = get_vm_block(vm, (pages << CT_PAGE_SHIFT));
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200147 if (block == NULL) {
Takashi Iwaib3e0afe2009-05-14 15:19:30 +0200148 printk(KERN_ERR "ctxfi: No virtual memory block that is big "
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200149 "enough to allocate!\n");
150 return NULL;
151 }
152
Takashi Iwaicd391e22009-06-02 15:04:29 +0200153 pte_start = (block->addr >> CT_PAGE_SHIFT);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200154 for (i = 0; i < pages; i++)
Takashi Iwaicd391e22009-06-02 15:04:29 +0200155 ptp[pte_start+i] = start_phys + (i << CT_PAGE_SHIFT);
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200156
Takashi Iwaicd391e22009-06-02 15:04:29 +0200157 block->addr += (virt_to_phys(host_addr) & (~CT_PAGE_MASK));
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200158 block->size = size;
159
160 return block;
161}
162
163static void ct_vm_unmap(struct ct_vm *vm, struct ct_vm_block *block)
164{
165 /* do unmapping */
Takashi Iwaicd391e22009-06-02 15:04:29 +0200166 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;
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200169 put_vm_block(vm, block);
170}
171
172/* *
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.
176 * */
177static void *
178ct_get_ptp_virt(struct ct_vm *vm, int index)
179{
180 void *addr;
181
182 addr = (index >= CT_PTP_NUM) ? NULL : vm->ptp[index];
183
184 return addr;
185}
186
187int ct_vm_create(struct ct_vm **rvm)
188{
189 struct ct_vm *vm;
190 struct ct_vm_block *block;
191 int i;
192
193 *rvm = NULL;
194
195 vm = kzalloc(sizeof(*vm), GFP_KERNEL);
196 if (NULL == vm)
197 return -ENOMEM;
198
Takashi Iwai8a4259b2009-06-02 08:40:51 +0200199 mutex_init(&vm->lock);
200
Wai Yew CHAY8cc72362009-05-14 08:05:58 +0200201 /* 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])
205 break;
206 }
207 if (!i) {
208 /* no page table pages are allocated */
209 kfree(vm);
210 return -ENOMEM;
211 }
212 vm->size = CT_ADDRS_PER_PAGE * i;
213 /* Initialise remaining ptps */
214 for (; i < CT_PTP_NUM; i++)
215 vm->ptp[i] = NULL;
216
217 vm->map = ct_vm_map;
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);
223 if (NULL != block) {
224 block->addr = 0;
225 block->size = vm->size;
226 list_add(&block->list, &vm->unused);
227 }
228
229 *rvm = vm;
230 return 0;
231}
232
233/* The caller must ensure no mapping pages are being used
234 * by hardware before calling this function */
235void ct_vm_destroy(struct ct_vm *vm)
236{
237 int i;
238 struct list_head *pos = NULL;
239 struct ct_vm_block *entry = NULL;
240
241 /* free used and unused list nodes */
242 while (!list_empty(&vm->used)) {
243 pos = vm->used.next;
244 list_del(pos);
245 entry = list_entry(pos, struct ct_vm_block, list);
246 kfree(entry);
247 }
248 while (!list_empty(&vm->unused)) {
249 pos = vm->unused.next;
250 list_del(pos);
251 entry = list_entry(pos, struct ct_vm_block, list);
252 kfree(entry);
253 }
254
255 /* free allocated page table pages */
256 for (i = 0; i < CT_PTP_NUM; i++)
257 kfree(vm->ptp[i]);
258
259 vm->size = 0;
260
261 kfree(vm);
262}