1 /****************************************************************************
2 * Copyright (C) 2003-2006 by XGI Technology, Taiwan.
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation on the rights to use, copy, modify, merge,
10 * publish, distribute, sublicense, and/or sell copies of the Software,
11 * and to permit persons to whom the Software is furnished to do so,
12 * subject to the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial
16 * portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 * XGI AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
25 ***************************************************************************/
29 #define XGI_FB_HEAP_START 0x1000000
31 int xgi_alloc(struct xgi_info * info, struct xgi_mem_alloc * alloc,
32 struct drm_file * filp)
34 struct drm_memblock_item *block;
35 const char *const mem_name = (alloc->location == XGI_MEMLOC_LOCAL)
39 if ((alloc->location != XGI_MEMLOC_LOCAL)
40 && (alloc->location != XGI_MEMLOC_NON_LOCAL)) {
41 DRM_ERROR("Invalid memory pool (0x%08x) specified.\n",
46 if ((alloc->location == XGI_MEMLOC_LOCAL)
47 ? !info->fb_heap_initialized : !info->pcie_heap_initialized) {
48 DRM_ERROR("Attempt to allocate from uninitialized memory "
49 "pool (0x%08x).\n", alloc->location);
53 mutex_lock(&info->dev->struct_mutex);
54 block = drm_sman_alloc(&info->sman, alloc->location, alloc->size,
55 0, (unsigned long) filp);
56 mutex_unlock(&info->dev->struct_mutex);
60 DRM_ERROR("%s memory allocation failed\n", mem_name);
63 alloc->offset = (*block->mm->offset)(block->mm,
65 alloc->hw_addr = alloc->offset;
66 alloc->index = block->user_hash.key;
68 if (block->user_hash.key != (unsigned long) alloc->index) {
69 DRM_ERROR("%s truncated handle %lx for pool %d "
71 __func__, block->user_hash.key,
72 alloc->location, alloc->offset);
75 if (alloc->location == XGI_MEMLOC_NON_LOCAL) {
76 alloc->hw_addr += info->pcie.base;
79 DRM_DEBUG("%s memory allocation succeeded: 0x%x\n",
80 mem_name, alloc->offset);
87 int xgi_alloc_ioctl(struct drm_device * dev, void * data,
88 struct drm_file * filp)
90 struct xgi_info *info = dev->dev_private;
92 return xgi_alloc(info, (struct xgi_mem_alloc *) data, filp);
96 int xgi_free(struct xgi_info * info, unsigned long index,
97 struct drm_file * filp)
101 mutex_lock(&info->dev->struct_mutex);
102 err = drm_sman_free_key(&info->sman, index);
103 mutex_unlock(&info->dev->struct_mutex);
109 int xgi_free_ioctl(struct drm_device * dev, void * data,
110 struct drm_file * filp)
112 struct xgi_info *info = dev->dev_private;
114 return xgi_free(info, *(unsigned long *) data, filp);
118 int xgi_fb_heap_init(struct xgi_info * info)
122 mutex_lock(&info->dev->struct_mutex);
123 err = drm_sman_set_range(&info->sman, XGI_MEMLOC_LOCAL,
125 info->fb.size - XGI_FB_HEAP_START);
126 mutex_unlock(&info->dev->struct_mutex);
128 info->fb_heap_initialized = (err == 0);