2 /****************************************************************************
\r
3 * Copyright (C) 2003-2006 by XGI Technology, Taiwan.
\r
5 * All Rights Reserved. *
\r
7 * Permission is hereby granted, free of charge, to any person obtaining
\r
8 * a copy of this software and associated documentation files (the
\r
9 * "Software"), to deal in the Software without restriction, including
\r
10 * without limitation on the rights to use, copy, modify, merge,
\r
11 * publish, distribute, sublicense, and/or sell copies of the Software,
\r
12 * and to permit persons to whom the Software is furnished to do so,
\r
13 * subject to the following conditions:
\r
15 * The above copyright notice and this permission notice (including the
\r
16 * next paragraph) shall be included in all copies or substantial
\r
17 * portions of the Software.
\r
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
\r
20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
\r
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
\r
22 * NON-INFRINGEMENT. IN NO EVENT SHALL XGI AND/OR
\r
23 * ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
\r
24 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
\r
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
\r
26 * DEALINGS IN THE SOFTWARE.
\r
27 ***************************************************************************/
\r
29 #include "xgi_types.h"
\r
30 #include "xgi_linux.h"
\r
31 #include "xgi_drv.h"
\r
34 #define XGI_FB_HEAP_START 0x1000000
\r
36 static xgi_mem_heap_t *xgi_fb_heap;
\r
37 static kmem_cache_t *xgi_fb_cache_block = NULL;
\r
38 extern struct list_head xgi_mempid_list;
\r
40 static xgi_mem_block_t *xgi_mem_new_node(void);
\r
41 static xgi_mem_block_t *xgi_mem_alloc(xgi_info_t *info, unsigned long size);
\r
42 static xgi_mem_block_t *xgi_mem_free(xgi_info_t *info, unsigned long offset);
\r
44 void xgi_fb_alloc(xgi_info_t *info,
\r
46 xgi_mem_alloc_t *alloc)
\r
48 xgi_mem_block_t *block;
\r
49 xgi_mem_pid_t *mempid_block;
\r
53 alloc->location = LOCAL;
\r
54 alloc->bus_addr = info->fb.base;
\r
56 XGI_INFO("Video RAM allocation on front buffer successfully! \n");
\r
60 xgi_down(info->fb_sem);
\r
61 block = xgi_mem_alloc(info, req->size);
\r
62 xgi_up(info->fb_sem);
\r
66 alloc->location = LOCAL;
\r
68 alloc->bus_addr = 0;
\r
70 XGI_ERROR("Video RAM allocation failed\n");
\r
74 XGI_INFO("Video RAM allocation succeeded: 0x%p\n",
\r
75 (char *) block->offset);
\r
76 alloc->location = LOCAL;
\r
77 alloc->size = block->size;
\r
78 alloc->bus_addr = info->fb.base + block->offset;
\r
79 alloc->hw_addr = block->offset;
\r
82 mempid_block = kmalloc(sizeof(xgi_mem_pid_t), GFP_KERNEL);
\r
83 mempid_block->location = LOCAL;
\r
84 mempid_block->bus_addr = alloc->bus_addr;
\r
85 mempid_block->pid = alloc->pid;
\r
88 XGI_ERROR("mempid_block alloc failed\n");
\r
90 XGI_INFO("Memory ProcessID add one fb block pid:%ld successfully! \n", mempid_block->pid);
\r
91 list_add(&mempid_block->list, &xgi_mempid_list);
\r
96 void xgi_fb_free(xgi_info_t *info, unsigned long bus_addr)
\r
98 xgi_mem_block_t *block;
\r
99 unsigned long offset = bus_addr - info->fb.base;
\r
100 xgi_mem_pid_t *mempid_block;
\r
101 xgi_mem_pid_t *mempid_freeblock = NULL;
\r
102 struct list_head *mempid_list;
\r
106 XGI_INFO("free onscreen frame buffer successfully !\n");
\r
110 xgi_down(info->fb_sem);
\r
111 block = xgi_mem_free(info, offset);
\r
112 xgi_up(info->fb_sem);
\r
116 XGI_ERROR("xgi_mem_free() failed at base 0x%lx\n", offset);
\r
119 /* manage mempid */
\r
120 mempid_list = xgi_mempid_list.next;
\r
121 while (mempid_list != &xgi_mempid_list)
\r
123 mempid_block = list_entry(mempid_list, struct xgi_mem_pid_s, list);
\r
124 if (mempid_block->location == LOCAL && mempid_block->bus_addr == bus_addr)
\r
126 mempid_freeblock = mempid_block;
\r
129 mempid_list = mempid_list->next;
\r
131 if (mempid_freeblock)
\r
133 list_del(&mempid_freeblock->list);
\r
134 XGI_INFO("Memory ProcessID delete one fb block pid:%ld successfully! \n", mempid_freeblock->pid);
\r
135 kfree(mempid_freeblock);
\r
140 int xgi_fb_heap_init(xgi_info_t *info)
\r
142 xgi_mem_block_t *block;
\r
144 xgi_fb_heap = kmalloc(sizeof(xgi_mem_heap_t), GFP_KERNEL);
\r
147 XGI_ERROR("xgi_fb_heap alloc failed\n");
\r
151 INIT_LIST_HEAD(&xgi_fb_heap->free_list);
\r
152 INIT_LIST_HEAD(&xgi_fb_heap->used_list);
\r
153 INIT_LIST_HEAD(&xgi_fb_heap->sort_list);
\r
155 xgi_fb_cache_block = kmem_cache_create("xgi_fb_block", sizeof(xgi_mem_block_t),
\r
156 0, SLAB_HWCACHE_ALIGN, NULL, NULL);
\r
158 if (NULL == xgi_fb_cache_block)
\r
160 XGI_ERROR("Fail to creat xgi_fb_block\n");
\r
164 block = (xgi_mem_block_t *)kmem_cache_alloc(xgi_fb_cache_block, GFP_KERNEL);
\r
167 XGI_ERROR("kmem_cache_alloc failed\n");
\r
171 block->offset = XGI_FB_HEAP_START;
\r
172 block->size = info->fb.size - XGI_FB_HEAP_START;
\r
174 list_add(&block->list, &xgi_fb_heap->free_list);
\r
176 xgi_fb_heap->max_freesize = info->fb.size - XGI_FB_HEAP_START;
\r
178 XGI_INFO("fb start offset: 0x%lx, memory size : 0x%lx\n", block->offset, block->size);
\r
179 XGI_INFO("xgi_fb_heap->max_freesize: 0x%lx \n", xgi_fb_heap->max_freesize);
\r
184 if (xgi_fb_cache_block)
\r
186 kmem_cache_destroy(xgi_fb_cache_block);
\r
187 xgi_fb_cache_block = NULL;
\r
192 kfree(xgi_fb_heap);
\r
193 xgi_fb_heap = NULL;
\r
198 void xgi_fb_heap_cleanup(xgi_info_t *info)
\r
200 struct list_head *free_list, *temp;
\r
201 xgi_mem_block_t *block;
\r
206 free_list = &xgi_fb_heap->free_list;
\r
207 for (i = 0; i < 3; i++, free_list++)
\r
209 temp = free_list->next;
\r
210 while (temp != free_list)
\r
212 block = list_entry(temp, struct xgi_mem_block_s, list);
\r
215 XGI_INFO("No. %d block->offset: 0x%lx block->size: 0x%lx \n",
\r
216 i, block->offset, block->size);
\r
217 //XGI_INFO("No. %d free block: 0x%p \n", i, block);
\r
218 kmem_cache_free(xgi_fb_cache_block, block);
\r
222 XGI_INFO("xgi_fb_heap: 0x%p \n", xgi_fb_heap);
\r
223 kfree(xgi_fb_heap);
\r
224 xgi_fb_heap = NULL;
\r
227 if (xgi_fb_cache_block)
\r
229 kmem_cache_destroy(xgi_fb_cache_block);
\r
230 xgi_fb_cache_block = NULL;
\r
234 static xgi_mem_block_t * xgi_mem_new_node(void)
\r
236 xgi_mem_block_t *block;
\r
238 block = (xgi_mem_block_t *)kmem_cache_alloc(xgi_fb_cache_block, GFP_KERNEL);
\r
241 XGI_ERROR("kmem_cache_alloc failed\n");
\r
249 static void xgi_mem_insert_node_after(xgi_mem_list_t *list,
\r
250 xgi_mem_block_t *current,
\r
251 xgi_mem_block_t *block);
\r
252 static void xgi_mem_insert_node_before(xgi_mem_list_t *list,
\r
253 xgi_mem_block_t *current,
\r
254 xgi_mem_block_t *block);
\r
255 static void xgi_mem_insert_node_head(xgi_mem_list_t *list,
\r
256 xgi_mem_block_t *block);
\r
257 static void xgi_mem_insert_node_tail(xgi_mem_list_t *list,
\r
258 xgi_mem_block_t *block);
\r
259 static void xgi_mem_delete_node(xgi_mem_list_t *list,
\r
260 xgi_mem_block_t *block);
\r
262 * insert node:block after node:current
\r
264 static void xgi_mem_insert_node_after(xgi_mem_list_t *list,
\r
265 xgi_mem_block_t *current,
\r
266 xgi_mem_block_t *block)
\r
268 block->prev = current;
\r
269 block->next = current->next;
\r
270 current->next = block;
\r
272 if (current == list->tail)
\r
274 list->tail = block;
\r
278 block->next->prev = block;
\r
283 * insert node:block before node:current
\r
285 static void xgi_mem_insert_node_before(xgi_mem_list_t *list,
\r
286 xgi_mem_block_t *current,
\r
287 xgi_mem_block_t *block)
\r
289 block->prev = current->prev;
\r
290 block->next = current;
\r
291 current->prev = block;
\r
292 if (current == list->head)
\r
294 list->head = block;
\r
298 block->prev->next = block;
\r
301 void xgi_mem_insert_node_head(xgi_mem_list_t *list,
\r
302 xgi_mem_block_t *block)
\r
304 block->next = list->head;
\r
305 block->prev = NULL;
\r
307 if (NULL == list->head)
\r
309 list->tail = block;
\r
313 list->head->prev = block;
\r
315 list->head = block;
\r
318 static void xgi_mem_insert_node_tail(xgi_mem_list_t *list,
\r
319 xgi_mem_block_t *block)
\r
322 block->next = NULL;
\r
323 block->prev = list->tail;
\r
324 if (NULL == list->tail)
\r
326 list->head = block;
\r
330 list->tail->next = block;
\r
332 list->tail = block;
\r
335 static void xgi_mem_delete_node(xgi_mem_list_t *list,
\r
336 xgi_mem_block_t *block)
\r
338 if (block == list->head)
\r
340 list->head = block->next;
\r
342 if (block == list->tail)
\r
344 list->tail = block->prev;
\r
349 block->prev->next = block->next;
\r
353 block->next->prev = block->prev;
\r
356 block->next = block->prev = NULL;
\r
359 static xgi_mem_block_t *xgi_mem_alloc(xgi_info_t *info, unsigned long originalSize)
\r
361 struct list_head *free_list;
\r
362 xgi_mem_block_t *block, *free_block, *used_block;
\r
364 unsigned long size = (originalSize + PAGE_SIZE - 1) & PAGE_MASK;
\r
366 XGI_INFO("Original 0x%lx bytes requested, really 0x%lx allocated\n", originalSize, size);
\r
370 XGI_ERROR("size == 0\n");
\r
373 XGI_INFO("max_freesize: 0x%lx \n", xgi_fb_heap->max_freesize);
\r
374 if (size > xgi_fb_heap->max_freesize)
\r
376 XGI_ERROR("size: 0x%lx is bigger than frame buffer total free size: 0x%lx !\n",
\r
377 size, xgi_fb_heap->max_freesize);
\r
381 free_list = xgi_fb_heap->free_list.next;
\r
383 while (free_list != &xgi_fb_heap->free_list)
\r
385 XGI_INFO("free_list: 0x%px \n", free_list);
\r
386 block = list_entry(free_list, struct xgi_mem_block_s, list);
\r
387 if (size <= block->size)
\r
391 free_list = free_list->next;
\r
394 if (free_list == &xgi_fb_heap->free_list)
\r
396 XGI_ERROR("Can't allocate %ldk size from frame buffer memory !\n", size/1024);
\r
400 free_block = block;
\r
401 XGI_INFO("alloc size: 0x%lx from offset: 0x%lx size: 0x%lx \n",
\r
402 size, free_block->offset, free_block->size);
\r
404 if (size == free_block->size)
\r
406 used_block = free_block;
\r
407 XGI_INFO("size == free_block->size: free_block = 0x%p\n", free_block);
\r
408 list_del(&free_block->list);
\r
412 used_block = xgi_mem_new_node();
\r
414 if (used_block == NULL) return (NULL);
\r
416 if (used_block == free_block)
\r
418 XGI_ERROR("used_block == free_block = 0x%p\n", used_block);
\r
421 used_block->offset = free_block->offset;
\r
422 used_block->size = size;
\r
424 free_block->offset += size;
\r
425 free_block->size -= size;
\r
428 xgi_fb_heap->max_freesize -= size;
\r
430 list_add(&used_block->list, &xgi_fb_heap->used_list);
\r
432 return (used_block);
\r
435 static xgi_mem_block_t *xgi_mem_free(xgi_info_t *info, unsigned long offset)
\r
437 struct list_head *free_list, *used_list;
\r
438 xgi_mem_block_t *used_block = NULL, *block = NULL;
\r
439 xgi_mem_block_t *prev, *next;
\r
441 unsigned long upper;
\r
442 unsigned long lower;
\r
444 used_list = xgi_fb_heap->used_list.next;
\r
445 while (used_list != &xgi_fb_heap->used_list)
\r
447 block = list_entry(used_list, struct xgi_mem_block_s, list);
\r
448 if (block->offset == offset)
\r
452 used_list = used_list->next;
\r
455 if (used_list == &xgi_fb_heap->used_list)
\r
457 XGI_ERROR("can't find block: 0x%lx to free!\n", offset);
\r
461 used_block = block;
\r
462 XGI_INFO("used_block: 0x%p, offset = 0x%lx, size = 0x%lx\n",
\r
463 used_block, used_block->offset, used_block->size);
\r
465 xgi_fb_heap->max_freesize += used_block->size;
\r
467 prev = next = NULL;
\r
468 upper = used_block->offset + used_block->size;
\r
469 lower = used_block->offset;
\r
471 free_list = xgi_fb_heap->free_list.next;
\r
472 while (free_list != &xgi_fb_heap->free_list)
\r
474 block = list_entry(free_list, struct xgi_mem_block_s, list);
\r
476 if (block->offset == upper)
\r
480 else if ((block->offset + block->size) == lower)
\r
484 free_list = free_list->next;
\r
487 XGI_INFO("next = 0x%p, prev = 0x%p\n", next, prev);
\r
488 list_del(&used_block->list);
\r
492 prev->size += (used_block->size + next->size);
\r
493 list_del(&next->list);
\r
494 XGI_INFO("free node 0x%p\n", next);
\r
495 kmem_cache_free(xgi_fb_cache_block, next);
\r
496 kmem_cache_free(xgi_fb_cache_block, used_block);
\r
505 prev->size += used_block->size;
\r
506 XGI_INFO("free node 0x%p\n", used_block);
\r
507 kmem_cache_free(xgi_fb_cache_block, used_block);
\r
514 next->size += used_block->size;
\r
515 next->offset = used_block->offset;
\r
516 XGI_INFO("free node 0x%p\n", used_block);
\r
517 kmem_cache_free(xgi_fb_cache_block, used_block);
\r
522 list_add(&used_block->list, &xgi_fb_heap->free_list);
\r
523 XGI_INFO("Recycled free node %p, offset = 0x%lx, size = 0x%lx\n",
\r
524 used_block, used_block->offset, used_block->size);
\r
526 return (used_block);
\r