Merge branch 'master' of ssh+git://git.freedesktop.org/git/mesa/drm into xgi-0-0-2
[platform/upstream/libdrm.git] / linux-core / xgi_fb.c
1 /****************************************************************************
2  * Copyright (C) 2003-2006 by XGI Technology, Taiwan.
3  *
4  * All Rights Reserved.
5  *
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:
13  *
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.
17  *
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  ***************************************************************************/
26
27 #include "xgi_drv.h"
28
29 #define XGI_FB_HEAP_START 0x1000000
30
31 int xgi_alloc(struct xgi_info * info, struct xgi_mem_alloc * alloc,
32               struct drm_file * filp)
33 {
34         struct drm_memblock_item *block;
35         const char *const mem_name = (alloc->location == XGI_MEMLOC_LOCAL) 
36                 ? "on-card" : "GART";
37
38
39         if ((alloc->location != XGI_MEMLOC_LOCAL)
40             && (alloc->location != XGI_MEMLOC_NON_LOCAL)) {
41                 DRM_ERROR("Invalid memory pool (0x%08x) specified.\n",
42                           alloc->location);
43                 return -EINVAL;
44         }
45
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);
50                 return -EINVAL;
51         }
52
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);
57
58         if (block == NULL) {
59                 alloc->size = 0;
60                 DRM_ERROR("%s memory allocation failed\n", mem_name);
61                 return -ENOMEM;
62         } else {
63                 alloc->offset = (*block->mm->offset)(block->mm,
64                                                      block->mm_info);
65                 alloc->hw_addr = alloc->offset;
66                 alloc->index = block->user_hash.key;
67
68                 if (alloc->location == XGI_MEMLOC_NON_LOCAL) {
69                         alloc->hw_addr += info->pcie.base;
70                 }
71
72                 DRM_DEBUG("%s memory allocation succeeded: 0x%x\n",
73                           mem_name, alloc->offset);
74         }
75
76         return 0;
77 }
78
79
80 int xgi_alloc_ioctl(struct drm_device * dev, void * data,
81                     struct drm_file * filp)
82 {
83         struct xgi_info *info = dev->dev_private;
84
85         return xgi_alloc(info, (struct xgi_mem_alloc *) data, filp);
86 }
87
88
89 int xgi_free(struct xgi_info * info, unsigned long index,
90              struct drm_file * filp)
91 {
92         int err;
93
94         mutex_lock(&info->dev->struct_mutex);
95         err = drm_sman_free_key(&info->sman, index);
96         mutex_unlock(&info->dev->struct_mutex);
97
98         return err;
99 }
100
101
102 int xgi_free_ioctl(struct drm_device * dev, void * data,
103                    struct drm_file * filp)
104 {
105         struct xgi_info *info = dev->dev_private;
106
107         return xgi_free(info, *(unsigned long *) data, filp);
108 }
109
110
111 int xgi_fb_heap_init(struct xgi_info * info)
112 {
113         int err;
114         
115         mutex_lock(&info->dev->struct_mutex);
116         err = drm_sman_set_range(&info->sman, XGI_MEMLOC_LOCAL,
117                                  XGI_FB_HEAP_START,
118                                  info->fb.size - XGI_FB_HEAP_START);
119         mutex_unlock(&info->dev->struct_mutex);
120
121         info->fb_heap_initialized = (err == 0);
122         return err;
123 }