2007-06-26 14:10:30 -06:00
|
|
|
/****************************************************************************
|
2007-07-24 14:36:02 -06:00
|
|
|
* Copyright (C) 2003-2006 by XGI Technology, Taiwan.
|
|
|
|
*
|
|
|
|
* All Rights Reserved.
|
|
|
|
*
|
2007-06-26 14:10:30 -06:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
2007-07-24 14:36:02 -06:00
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation on the rights to use, copy, modify, merge,
|
|
|
|
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
|
|
* and to permit persons to whom the Software is furnished to do so,
|
|
|
|
* subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice (including the
|
|
|
|
* next paragraph) shall be included in all copies or substantial
|
|
|
|
* portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* XGI AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
2007-06-26 14:10:30 -06:00
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#include "xgi_drv.h"
|
|
|
|
|
|
|
|
#define XGI_FB_HEAP_START 0x1000000
|
|
|
|
|
2007-07-19 11:29:18 -06:00
|
|
|
struct kmem_cache *xgi_mem_block_cache = NULL;
|
2007-06-26 14:10:30 -06:00
|
|
|
|
2007-06-29 16:27:38 -06:00
|
|
|
static struct xgi_mem_block *xgi_mem_new_node(void);
|
2007-06-26 14:10:30 -06:00
|
|
|
|
|
|
|
|
2007-07-19 11:29:18 -06:00
|
|
|
int xgi_mem_heap_init(struct xgi_mem_heap *heap, unsigned int start,
|
|
|
|
unsigned int end)
|
2007-06-26 14:10:30 -06:00
|
|
|
{
|
2007-06-29 16:27:38 -06:00
|
|
|
struct xgi_mem_block *block;
|
2007-06-26 14:10:30 -06:00
|
|
|
|
2007-07-19 11:29:18 -06:00
|
|
|
INIT_LIST_HEAD(&heap->free_list);
|
|
|
|
INIT_LIST_HEAD(&heap->used_list);
|
|
|
|
INIT_LIST_HEAD(&heap->sort_list);
|
|
|
|
heap->initialized = TRUE;
|
2007-06-26 14:10:30 -06:00
|
|
|
|
2007-07-19 11:29:18 -06:00
|
|
|
block = kmem_cache_alloc(xgi_mem_block_cache, GFP_KERNEL);
|
2007-06-26 14:10:30 -06:00
|
|
|
if (!block) {
|
2007-07-26 18:01:16 -06:00
|
|
|
return -ENOMEM;
|
2007-06-26 14:10:30 -06:00
|
|
|
}
|
|
|
|
|
2007-07-19 11:29:18 -06:00
|
|
|
block->offset = start;
|
|
|
|
block->size = end - start;
|
2007-06-26 14:10:30 -06:00
|
|
|
|
2007-07-19 11:29:18 -06:00
|
|
|
list_add(&block->list, &heap->free_list);
|
2007-06-26 14:10:30 -06:00
|
|
|
|
2007-07-19 11:29:18 -06:00
|
|
|
heap->max_freesize = end - start;
|
2007-06-26 14:10:30 -06:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-07-19 11:29:18 -06:00
|
|
|
|
|
|
|
void xgi_mem_heap_cleanup(struct xgi_mem_heap * heap)
|
2007-06-26 14:10:30 -06:00
|
|
|
{
|
2007-06-29 22:35:27 -06:00
|
|
|
struct list_head *free_list;
|
2007-06-29 16:27:38 -06:00
|
|
|
struct xgi_mem_block *block;
|
2007-06-29 22:35:27 -06:00
|
|
|
struct xgi_mem_block *next;
|
2007-06-26 14:10:30 -06:00
|
|
|
int i;
|
|
|
|
|
2007-07-19 11:29:18 -06:00
|
|
|
free_list = &heap->free_list;
|
|
|
|
for (i = 0; i < 3; i++, free_list++) {
|
|
|
|
list_for_each_entry_safe(block, next, free_list, list) {
|
|
|
|
DRM_INFO
|
|
|
|
("No. %d block->offset: 0x%lx block->size: 0x%lx \n",
|
|
|
|
i, block->offset, block->size);
|
|
|
|
kmem_cache_free(xgi_mem_block_cache, block);
|
|
|
|
block = NULL;
|
2007-06-26 14:10:30 -06:00
|
|
|
}
|
|
|
|
}
|
2007-07-19 11:29:18 -06:00
|
|
|
|
|
|
|
heap->initialized = 0;
|
2007-06-26 14:10:30 -06:00
|
|
|
}
|
|
|
|
|
2007-07-19 11:29:18 -06:00
|
|
|
|
|
|
|
struct xgi_mem_block *xgi_mem_new_node(void)
|
2007-06-26 14:10:30 -06:00
|
|
|
{
|
2007-07-19 11:29:18 -06:00
|
|
|
struct xgi_mem_block *block =
|
|
|
|
kmem_cache_alloc(xgi_mem_block_cache, GFP_KERNEL);
|
2007-06-26 14:10:30 -06:00
|
|
|
|
|
|
|
if (!block) {
|
2007-07-19 11:29:18 -06:00
|
|
|
DRM_ERROR("kmem_cache_alloc failed\n");
|
2007-06-26 14:10:30 -06:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-07-19 11:29:18 -06:00
|
|
|
block->offset = 0;
|
|
|
|
block->size = 0;
|
2007-07-27 16:45:59 -06:00
|
|
|
block->filp = (struct drm_file *) -1;
|
2007-06-26 14:10:30 -06:00
|
|
|
|
2007-07-19 11:29:18 -06:00
|
|
|
return block;
|
2007-06-26 14:10:30 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-07-19 11:29:18 -06:00
|
|
|
struct xgi_mem_block *xgi_mem_alloc(struct xgi_mem_heap * heap,
|
2007-08-06 16:31:34 -06:00
|
|
|
unsigned long originalSize)
|
2007-06-26 14:10:30 -06:00
|
|
|
{
|
2007-06-29 16:27:38 -06:00
|
|
|
struct xgi_mem_block *block, *free_block, *used_block;
|
2007-06-26 14:10:30 -06:00
|
|
|
unsigned long size = (originalSize + PAGE_SIZE - 1) & PAGE_MASK;
|
|
|
|
|
2007-07-19 11:29:18 -06:00
|
|
|
|
|
|
|
DRM_INFO("Original 0x%lx bytes requested, really 0x%lx allocated\n",
|
2007-06-26 14:10:30 -06:00
|
|
|
originalSize, size);
|
|
|
|
|
|
|
|
if (size == 0) {
|
2007-07-19 11:29:18 -06:00
|
|
|
DRM_ERROR("size == 0\n");
|
2007-06-26 14:10:30 -06:00
|
|
|
return (NULL);
|
|
|
|
}
|
2007-07-19 11:29:18 -06:00
|
|
|
DRM_INFO("max_freesize: 0x%lx \n", heap->max_freesize);
|
|
|
|
if (size > heap->max_freesize) {
|
|
|
|
DRM_ERROR
|
2007-06-26 14:10:30 -06:00
|
|
|
("size: 0x%lx is bigger than frame buffer total free size: 0x%lx !\n",
|
2007-07-19 11:29:18 -06:00
|
|
|
size, heap->max_freesize);
|
2007-06-26 14:10:30 -06:00
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2007-07-19 11:29:18 -06:00
|
|
|
list_for_each_entry(block, &heap->free_list, list) {
|
|
|
|
DRM_INFO("block: 0x%px \n", block);
|
2007-06-26 14:10:30 -06:00
|
|
|
if (size <= block->size) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-19 11:29:18 -06:00
|
|
|
if (&block->list == &heap->free_list) {
|
|
|
|
DRM_ERROR
|
2007-06-26 14:10:30 -06:00
|
|
|
("Can't allocate %ldk size from frame buffer memory !\n",
|
|
|
|
size / 1024);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
free_block = block;
|
2007-07-19 11:29:18 -06:00
|
|
|
DRM_INFO("alloc size: 0x%lx from offset: 0x%lx size: 0x%lx \n",
|
2007-06-26 14:10:30 -06:00
|
|
|
size, free_block->offset, free_block->size);
|
|
|
|
|
|
|
|
if (size == free_block->size) {
|
|
|
|
used_block = free_block;
|
2007-07-19 11:29:18 -06:00
|
|
|
DRM_INFO("size == free_block->size: free_block = 0x%p\n",
|
2007-06-26 14:10:30 -06:00
|
|
|
free_block);
|
|
|
|
list_del(&free_block->list);
|
|
|
|
} else {
|
|
|
|
used_block = xgi_mem_new_node();
|
|
|
|
|
|
|
|
if (used_block == NULL)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
if (used_block == free_block) {
|
2007-07-19 11:29:18 -06:00
|
|
|
DRM_ERROR("used_block == free_block = 0x%p\n",
|
2007-06-26 14:10:30 -06:00
|
|
|
used_block);
|
|
|
|
}
|
|
|
|
|
|
|
|
used_block->offset = free_block->offset;
|
|
|
|
used_block->size = size;
|
|
|
|
|
|
|
|
free_block->offset += size;
|
|
|
|
free_block->size -= size;
|
|
|
|
}
|
|
|
|
|
2007-07-19 11:29:18 -06:00
|
|
|
heap->max_freesize -= size;
|
2007-06-26 14:10:30 -06:00
|
|
|
|
2007-07-19 11:29:18 -06:00
|
|
|
list_add(&used_block->list, &heap->used_list);
|
2007-06-26 14:10:30 -06:00
|
|
|
|
|
|
|
return (used_block);
|
|
|
|
}
|
|
|
|
|
2007-07-19 11:29:18 -06:00
|
|
|
int xgi_mem_free(struct xgi_mem_heap * heap, unsigned long offset,
|
2007-07-27 16:45:59 -06:00
|
|
|
struct drm_file * filp)
|
2007-06-26 14:10:30 -06:00
|
|
|
{
|
2007-06-29 22:35:27 -06:00
|
|
|
struct xgi_mem_block *used_block = NULL, *block;
|
2007-06-29 16:27:38 -06:00
|
|
|
struct xgi_mem_block *prev, *next;
|
2007-06-26 14:10:30 -06:00
|
|
|
|
|
|
|
unsigned long upper;
|
|
|
|
unsigned long lower;
|
|
|
|
|
2007-07-19 11:29:18 -06:00
|
|
|
list_for_each_entry(block, &heap->used_list, list) {
|
2007-06-26 14:10:30 -06:00
|
|
|
if (block->offset == offset) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-19 11:29:18 -06:00
|
|
|
if (&block->list == &heap->used_list) {
|
|
|
|
DRM_ERROR("can't find block: 0x%lx to free!\n", offset);
|
2007-07-26 18:01:16 -06:00
|
|
|
return -ENOENT;
|
2007-07-19 11:29:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if (block->filp != filp) {
|
2007-07-26 18:01:16 -06:00
|
|
|
return -EPERM;
|
2007-06-26 14:10:30 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
used_block = block;
|
2007-07-19 11:29:18 -06:00
|
|
|
DRM_INFO("used_block: 0x%p, offset = 0x%lx, size = 0x%lx\n",
|
2007-06-26 14:10:30 -06:00
|
|
|
used_block, used_block->offset, used_block->size);
|
|
|
|
|
2007-07-19 11:29:18 -06:00
|
|
|
heap->max_freesize += used_block->size;
|
2007-06-26 14:10:30 -06:00
|
|
|
|
|
|
|
prev = next = NULL;
|
|
|
|
upper = used_block->offset + used_block->size;
|
|
|
|
lower = used_block->offset;
|
|
|
|
|
2007-07-19 11:29:18 -06:00
|
|
|
list_for_each_entry(block, &heap->free_list, list) {
|
2007-06-26 14:10:30 -06:00
|
|
|
if (block->offset == upper) {
|
|
|
|
next = block;
|
|
|
|
} else if ((block->offset + block->size) == lower) {
|
|
|
|
prev = block;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-19 11:29:18 -06:00
|
|
|
DRM_INFO("next = 0x%p, prev = 0x%p\n", next, prev);
|
2007-06-26 14:10:30 -06:00
|
|
|
list_del(&used_block->list);
|
|
|
|
|
|
|
|
if (prev && next) {
|
|
|
|
prev->size += (used_block->size + next->size);
|
|
|
|
list_del(&next->list);
|
2007-07-19 11:29:18 -06:00
|
|
|
DRM_INFO("free node 0x%p\n", next);
|
|
|
|
kmem_cache_free(xgi_mem_block_cache, next);
|
|
|
|
kmem_cache_free(xgi_mem_block_cache, used_block);
|
2007-06-26 14:10:30 -06:00
|
|
|
}
|
2007-07-19 11:29:18 -06:00
|
|
|
else if (prev) {
|
2007-06-26 14:10:30 -06:00
|
|
|
prev->size += used_block->size;
|
2007-07-19 11:29:18 -06:00
|
|
|
DRM_INFO("free node 0x%p\n", used_block);
|
|
|
|
kmem_cache_free(xgi_mem_block_cache, used_block);
|
2007-06-26 14:10:30 -06:00
|
|
|
}
|
2007-07-19 11:29:18 -06:00
|
|
|
else if (next) {
|
2007-06-26 14:10:30 -06:00
|
|
|
next->size += used_block->size;
|
|
|
|
next->offset = used_block->offset;
|
2007-07-19 11:29:18 -06:00
|
|
|
DRM_INFO("free node 0x%p\n", used_block);
|
|
|
|
kmem_cache_free(xgi_mem_block_cache, used_block);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
list_add(&used_block->list, &heap->free_list);
|
|
|
|
DRM_INFO("Recycled free node %p, offset = 0x%lx, size = 0x%lx\n",
|
|
|
|
used_block, used_block->offset, used_block->size);
|
2007-06-26 14:10:30 -06:00
|
|
|
}
|
|
|
|
|
2007-07-19 11:29:18 -06:00
|
|
|
return 0;
|
|
|
|
}
|
2007-06-26 14:10:30 -06:00
|
|
|
|
2007-07-19 11:29:18 -06:00
|
|
|
|
|
|
|
int xgi_fb_alloc(struct xgi_info * info, struct xgi_mem_alloc * alloc,
|
2007-07-27 16:45:59 -06:00
|
|
|
struct drm_file * filp)
|
2007-07-19 11:29:18 -06:00
|
|
|
{
|
|
|
|
struct xgi_mem_block *block;
|
|
|
|
|
2007-08-06 16:37:56 -06:00
|
|
|
down(&info->fb_sem);
|
|
|
|
block = xgi_mem_alloc(&info->fb_heap, alloc->size);
|
|
|
|
up(&info->fb_sem);
|
|
|
|
|
|
|
|
if (block == NULL) {
|
|
|
|
alloc->size = 0;
|
|
|
|
DRM_ERROR("Video RAM allocation failed\n");
|
|
|
|
return -ENOMEM;
|
2007-07-19 11:29:18 -06:00
|
|
|
} else {
|
2007-08-06 16:37:56 -06:00
|
|
|
DRM_INFO("Video RAM allocation succeeded: 0x%p\n",
|
|
|
|
(char *)block->offset);
|
|
|
|
alloc->location = XGI_MEMLOC_LOCAL;
|
|
|
|
alloc->size = block->size;
|
|
|
|
alloc->offset = block->offset;
|
|
|
|
alloc->hw_addr = block->offset;
|
|
|
|
|
|
|
|
block->filp = filp;
|
2007-07-19 11:29:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-07-27 16:45:59 -06:00
|
|
|
int xgi_fb_alloc_ioctl(struct drm_device * dev, void * data,
|
|
|
|
struct drm_file * filp)
|
2007-07-19 11:29:18 -06:00
|
|
|
{
|
2007-07-27 16:45:59 -06:00
|
|
|
struct xgi_mem_alloc *alloc =
|
|
|
|
(struct xgi_mem_alloc *) data;
|
2007-07-19 11:29:18 -06:00
|
|
|
struct xgi_info *info = dev->dev_private;
|
|
|
|
|
2007-07-27 16:45:59 -06:00
|
|
|
return xgi_fb_alloc(info, alloc, filp);
|
2007-07-19 11:29:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-07-27 16:45:59 -06:00
|
|
|
int xgi_fb_free(struct xgi_info * info, unsigned long offset,
|
|
|
|
struct drm_file * filp)
|
2007-07-19 11:29:18 -06:00
|
|
|
{
|
|
|
|
int err = 0;
|
|
|
|
|
2007-08-06 16:37:56 -06:00
|
|
|
down(&info->fb_sem);
|
|
|
|
err = xgi_mem_free(&info->fb_heap, offset, filp);
|
|
|
|
up(&info->fb_sem);
|
2007-07-19 11:29:18 -06:00
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-07-27 16:45:59 -06:00
|
|
|
int xgi_fb_free_ioctl(struct drm_device * dev, void * data,
|
|
|
|
struct drm_file * filp)
|
2007-07-19 11:29:18 -06:00
|
|
|
{
|
|
|
|
struct xgi_info *info = dev->dev_private;
|
|
|
|
|
2007-07-27 16:45:59 -06:00
|
|
|
return xgi_fb_free(info, *(u32 *) data, filp);
|
2007-07-19 11:29:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int xgi_fb_heap_init(struct xgi_info * info)
|
|
|
|
{
|
|
|
|
return xgi_mem_heap_init(&info->fb_heap, XGI_FB_HEAP_START,
|
|
|
|
info->fb.size);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Free all blocks associated with a particular file handle.
|
|
|
|
*/
|
2007-07-27 16:45:59 -06:00
|
|
|
void xgi_fb_free_all(struct xgi_info * info, struct drm_file * filp)
|
2007-07-19 11:29:18 -06:00
|
|
|
{
|
|
|
|
if (!info->fb_heap.initialized) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
down(&info->fb_sem);
|
|
|
|
|
|
|
|
do {
|
|
|
|
struct xgi_mem_block *block;
|
|
|
|
|
|
|
|
list_for_each_entry(block, &info->fb_heap.used_list, list) {
|
|
|
|
if (block->filp == filp) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (&block->list == &info->fb_heap.used_list) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:38:56 -06:00
|
|
|
(void) xgi_mem_free(&info->fb_heap, block->offset, filp);
|
2007-07-19 11:29:18 -06:00
|
|
|
} while(1);
|
|
|
|
|
|
|
|
up(&info->fb_sem);
|
2007-06-26 14:10:30 -06:00
|
|
|
}
|