2 /****************************************************************************
3 * Copyright (C) 2003-2006 by XGI Technology, Taiwan.
5 * All Rights Reserved. *
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation on the rights to use, copy, modify, merge,
11 * publish, distribute, sublicense, and/or sell copies of the Software,
12 * and to permit persons to whom the Software is furnished to do so,
13 * subject to the following conditions:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial
17 * portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NON-INFRINGEMENT. IN NO EVENT SHALL XGI AND/OR
23 * ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
24 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 * DEALINGS IN THE SOFTWARE.
27 ***************************************************************************/
29 #include "xgi_types.h"
30 #include "xgi_linux.h"
34 #define XGI_FB_HEAP_START 0x1000000
36 static struct xgi_mem_heap *xgi_fb_heap;
37 static struct kmem_cache *xgi_fb_cache_block = NULL;
38 extern struct list_head xgi_mempid_list;
40 static struct xgi_mem_block *xgi_mem_new_node(void);
41 static struct xgi_mem_block *xgi_mem_alloc(struct xgi_info * info, unsigned long size);
42 static struct xgi_mem_block *xgi_mem_free(struct xgi_info * info, unsigned long offset);
44 void xgi_fb_alloc(struct xgi_info * info,
45 struct xgi_mem_req * req, struct xgi_mem_alloc * alloc)
47 struct xgi_mem_block *block;
48 struct xgi_mem_pid *mempid_block;
51 alloc->location = LOCAL;
52 alloc->bus_addr = info->fb.base;
55 ("Video RAM allocation on front buffer successfully! \n");
57 xgi_down(info->fb_sem);
58 block = xgi_mem_alloc(info, req->size);
62 alloc->location = LOCAL;
66 XGI_ERROR("Video RAM allocation failed\n");
68 XGI_INFO("Video RAM allocation succeeded: 0x%p\n",
69 (char *)block->offset);
70 alloc->location = LOCAL;
71 alloc->size = block->size;
72 alloc->bus_addr = info->fb.base + block->offset;
73 alloc->hw_addr = block->offset;
77 kmalloc(sizeof(struct xgi_mem_pid), GFP_KERNEL);
78 mempid_block->location = LOCAL;
79 mempid_block->bus_addr = alloc->bus_addr;
80 mempid_block->pid = alloc->pid;
83 XGI_ERROR("mempid_block alloc failed\n");
86 ("Memory ProcessID add one fb block pid:%ld successfully! \n",
88 list_add(&mempid_block->list, &xgi_mempid_list);
93 void xgi_fb_free(struct xgi_info * info, unsigned long bus_addr)
95 struct xgi_mem_block *block;
96 unsigned long offset = bus_addr - info->fb.base;
97 struct xgi_mem_pid *mempid_block;
98 struct xgi_mem_pid *mempid_freeblock = NULL;
99 struct list_head *mempid_list;
102 XGI_INFO("free onscreen frame buffer successfully !\n");
104 xgi_down(info->fb_sem);
105 block = xgi_mem_free(info, offset);
106 xgi_up(info->fb_sem);
109 XGI_ERROR("xgi_mem_free() failed at base 0x%lx\n",
114 mempid_list = xgi_mempid_list.next;
115 while (mempid_list != &xgi_mempid_list) {
117 list_entry(mempid_list, struct xgi_mem_pid, list);
118 if (mempid_block->location == LOCAL
119 && mempid_block->bus_addr == bus_addr) {
120 mempid_freeblock = mempid_block;
123 mempid_list = mempid_list->next;
125 if (mempid_freeblock) {
126 list_del(&mempid_freeblock->list);
128 ("Memory ProcessID delete one fb block pid:%ld successfully! \n",
129 mempid_freeblock->pid);
130 kfree(mempid_freeblock);
135 int xgi_fb_heap_init(struct xgi_info * info)
137 struct xgi_mem_block *block;
139 xgi_fb_heap = kmalloc(sizeof(struct xgi_mem_heap), GFP_KERNEL);
141 XGI_ERROR("xgi_fb_heap alloc failed\n");
145 INIT_LIST_HEAD(&xgi_fb_heap->free_list);
146 INIT_LIST_HEAD(&xgi_fb_heap->used_list);
147 INIT_LIST_HEAD(&xgi_fb_heap->sort_list);
150 kmem_cache_create("xgi_fb_block", sizeof(struct xgi_mem_block), 0,
151 SLAB_HWCACHE_ALIGN, NULL, NULL);
153 if (NULL == xgi_fb_cache_block) {
154 XGI_ERROR("Fail to creat xgi_fb_block\n");
159 (struct xgi_mem_block *) kmem_cache_alloc(xgi_fb_cache_block,
162 XGI_ERROR("kmem_cache_alloc failed\n");
166 block->offset = XGI_FB_HEAP_START;
167 block->size = info->fb.size - XGI_FB_HEAP_START;
169 list_add(&block->list, &xgi_fb_heap->free_list);
171 xgi_fb_heap->max_freesize = info->fb.size - XGI_FB_HEAP_START;
173 XGI_INFO("fb start offset: 0x%lx, memory size : 0x%lx\n", block->offset,
175 XGI_INFO("xgi_fb_heap->max_freesize: 0x%lx \n",
176 xgi_fb_heap->max_freesize);
181 if (xgi_fb_cache_block) {
182 kmem_cache_destroy(xgi_fb_cache_block);
183 xgi_fb_cache_block = NULL;
193 void xgi_fb_heap_cleanup(struct xgi_info * info)
195 struct list_head *free_list, *temp;
196 struct xgi_mem_block *block;
200 free_list = &xgi_fb_heap->free_list;
201 for (i = 0; i < 3; i++, free_list++) {
202 temp = free_list->next;
203 while (temp != free_list) {
205 list_entry(temp, struct xgi_mem_block,
210 ("No. %d block->offset: 0x%lx block->size: 0x%lx \n",
211 i, block->offset, block->size);
212 //XGI_INFO("No. %d free block: 0x%p \n", i, block);
213 kmem_cache_free(xgi_fb_cache_block, block);
217 XGI_INFO("xgi_fb_heap: 0x%p \n", xgi_fb_heap);
222 if (xgi_fb_cache_block) {
223 kmem_cache_destroy(xgi_fb_cache_block);
224 xgi_fb_cache_block = NULL;
228 static struct xgi_mem_block *xgi_mem_new_node(void)
230 struct xgi_mem_block *block;
233 (struct xgi_mem_block *) kmem_cache_alloc(xgi_fb_cache_block,
236 XGI_ERROR("kmem_cache_alloc failed\n");
244 static void xgi_mem_insert_node_after(struct xgi_mem_list * list,
245 struct xgi_mem_block * current,
246 struct xgi_mem_block * block);
247 static void xgi_mem_insert_node_before(struct xgi_mem_list * list,
248 struct xgi_mem_block * current,
249 struct xgi_mem_block * block);
250 static void xgi_mem_insert_node_head(struct xgi_mem_list * list,
251 struct xgi_mem_block * block);
252 static void xgi_mem_insert_node_tail(struct xgi_mem_list * list,
253 struct xgi_mem_block * block);
254 static void xgi_mem_delete_node(struct xgi_mem_list * list, struct xgi_mem_block * block);
256 * insert node:block after node:current
258 static void xgi_mem_insert_node_after(struct xgi_mem_list * list,
259 struct xgi_mem_block * current,
260 struct xgi_mem_block * block)
262 block->prev = current;
263 block->next = current->next;
264 current->next = block;
266 if (current == list->tail) {
269 block->next->prev = block;
274 * insert node:block before node:current
276 static void xgi_mem_insert_node_before(struct xgi_mem_list * list,
277 struct xgi_mem_block * current,
278 struct xgi_mem_block * block)
280 block->prev = current->prev;
281 block->next = current;
282 current->prev = block;
283 if (current == list->head) {
286 block->prev->next = block;
289 void xgi_mem_insert_node_head(struct xgi_mem_list * list, struct xgi_mem_block * block)
291 block->next = list->head;
294 if (NULL == list->head) {
297 list->head->prev = block;
302 static void xgi_mem_insert_node_tail(struct xgi_mem_list * list,
303 struct xgi_mem_block * block)
306 block->prev = list->tail;
307 if (NULL == list->tail) {
310 list->tail->next = block;
315 static void xgi_mem_delete_node(struct xgi_mem_list * list, struct xgi_mem_block * block)
317 if (block == list->head) {
318 list->head = block->next;
320 if (block == list->tail) {
321 list->tail = block->prev;
325 block->prev->next = block->next;
328 block->next->prev = block->prev;
331 block->next = block->prev = NULL;
334 static struct xgi_mem_block *xgi_mem_alloc(struct xgi_info * info,
335 unsigned long originalSize)
337 struct list_head *free_list;
338 struct xgi_mem_block *block, *free_block, *used_block;
340 unsigned long size = (originalSize + PAGE_SIZE - 1) & PAGE_MASK;
342 XGI_INFO("Original 0x%lx bytes requested, really 0x%lx allocated\n",
346 XGI_ERROR("size == 0\n");
349 XGI_INFO("max_freesize: 0x%lx \n", xgi_fb_heap->max_freesize);
350 if (size > xgi_fb_heap->max_freesize) {
352 ("size: 0x%lx is bigger than frame buffer total free size: 0x%lx !\n",
353 size, xgi_fb_heap->max_freesize);
357 free_list = xgi_fb_heap->free_list.next;
359 while (free_list != &xgi_fb_heap->free_list) {
360 XGI_INFO("free_list: 0x%px \n", free_list);
361 block = list_entry(free_list, struct xgi_mem_block, list);
362 if (size <= block->size) {
365 free_list = free_list->next;
368 if (free_list == &xgi_fb_heap->free_list) {
370 ("Can't allocate %ldk size from frame buffer memory !\n",
376 XGI_INFO("alloc size: 0x%lx from offset: 0x%lx size: 0x%lx \n",
377 size, free_block->offset, free_block->size);
379 if (size == free_block->size) {
380 used_block = free_block;
381 XGI_INFO("size == free_block->size: free_block = 0x%p\n",
383 list_del(&free_block->list);
385 used_block = xgi_mem_new_node();
387 if (used_block == NULL)
390 if (used_block == free_block) {
391 XGI_ERROR("used_block == free_block = 0x%p\n",
395 used_block->offset = free_block->offset;
396 used_block->size = size;
398 free_block->offset += size;
399 free_block->size -= size;
402 xgi_fb_heap->max_freesize -= size;
404 list_add(&used_block->list, &xgi_fb_heap->used_list);
409 static struct xgi_mem_block *xgi_mem_free(struct xgi_info * info, unsigned long offset)
411 struct list_head *free_list, *used_list;
412 struct xgi_mem_block *used_block = NULL, *block = NULL;
413 struct xgi_mem_block *prev, *next;
418 used_list = xgi_fb_heap->used_list.next;
419 while (used_list != &xgi_fb_heap->used_list) {
420 block = list_entry(used_list, struct xgi_mem_block, list);
421 if (block->offset == offset) {
424 used_list = used_list->next;
427 if (used_list == &xgi_fb_heap->used_list) {
428 XGI_ERROR("can't find block: 0x%lx to free!\n", offset);
433 XGI_INFO("used_block: 0x%p, offset = 0x%lx, size = 0x%lx\n",
434 used_block, used_block->offset, used_block->size);
436 xgi_fb_heap->max_freesize += used_block->size;
439 upper = used_block->offset + used_block->size;
440 lower = used_block->offset;
442 free_list = xgi_fb_heap->free_list.next;
443 while (free_list != &xgi_fb_heap->free_list) {
444 block = list_entry(free_list, struct xgi_mem_block, list);
446 if (block->offset == upper) {
448 } else if ((block->offset + block->size) == lower) {
451 free_list = free_list->next;
454 XGI_INFO("next = 0x%p, prev = 0x%p\n", next, prev);
455 list_del(&used_block->list);
458 prev->size += (used_block->size + next->size);
459 list_del(&next->list);
460 XGI_INFO("free node 0x%p\n", next);
461 kmem_cache_free(xgi_fb_cache_block, next);
462 kmem_cache_free(xgi_fb_cache_block, used_block);
470 prev->size += used_block->size;
471 XGI_INFO("free node 0x%p\n", used_block);
472 kmem_cache_free(xgi_fb_cache_block, used_block);
478 next->size += used_block->size;
479 next->offset = used_block->offset;
480 XGI_INFO("free node 0x%p\n", used_block);
481 kmem_cache_free(xgi_fb_cache_block, used_block);
486 list_add(&used_block->list, &xgi_fb_heap->free_list);
487 XGI_INFO("Recycled free node %p, offset = 0x%lx, size = 0x%lx\n",
488 used_block, used_block->offset, used_block->size);