drm: add drm simple memory manager support for SiS and VIA drivers
[linux-2.6] / drivers / char / drm / sis_mm.c
1 /**************************************************************************
2  *
3  * Copyright 2006 Tungsten Graphics, Inc., Bismarck, ND., USA.
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * 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 portions
16  * of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24  * USE OR OTHER DEALINGS IN THE SOFTWARE.
25  *
26  *
27  **************************************************************************/
28
29 /*
30  * Authors:
31  *    Thomas Hellström <thomas-at-tungstengraphics-dot-com>
32  */
33
34 #include "drmP.h"
35 #include "sis_drm.h"
36 #include "sis_drv.h"
37
38 #include <video/sisfb.h>
39
40 #define VIDEO_TYPE 0
41 #define AGP_TYPE 1
42
43 #define SIS_MM_ALIGN_SHIFT 4
44 #define SIS_MM_ALIGN_MASK ( (1 << SIS_MM_ALIGN_SHIFT) - 1)
45
46 #if defined(CONFIG_FB_SIS)
47 /* fb management via fb device */
48
49 #define SIS_MM_ALIGN_SHIFT 0
50 #define SIS_MM_ALIGN_MASK 0
51
52 static void *sis_sman_mm_allocate(void *private, unsigned long size,
53                                   unsigned alignment)
54 {
55         struct sis_memreq req;
56
57         req.size = size;
58         sis_malloc(&req);
59         if (req.size == 0)
60                 return NULL;
61         else
62                 return (void *)~req.offset;
63 }
64
65 static void sis_sman_mm_free(void *private, void *ref)
66 {
67         sis_free(~((unsigned long)ref));
68 }
69
70 static void sis_sman_mm_destroy(void *private)
71 {
72         ;
73 }
74
75 unsigned long sis_sman_mm_offset(void *private, void *ref)
76 {
77         return ~((unsigned long)ref);
78 }
79
80 #endif
81
82 static int sis_fb_init(DRM_IOCTL_ARGS)
83 {
84         DRM_DEVICE;
85         drm_sis_private_t *dev_priv = dev->dev_private;
86         drm_sis_fb_t fb;
87         int ret;
88
89         DRM_COPY_FROM_USER_IOCTL(fb, (drm_sis_fb_t __user *) data, sizeof(fb));
90
91         mutex_lock(&dev->struct_mutex);
92 #if defined(CONFIG_FB_SIS)
93         {
94                 drm_sman_mm_t sman_mm;
95                 sman_mm.private = (void *)0xFFFFFFFF;
96                 sman_mm.allocate = sis_sman_mm_allocate;
97                 sman_mm.free = sis_sman_mm_free;
98                 sman_mm.destroy = sis_sman_mm_destroy;
99                 sman_mm.offset = sis_sman_mm_offset;
100                 ret =
101                     drm_sman_set_manager(&dev_priv->sman, VIDEO_TYPE, &sman_mm);
102         }
103 #else
104         ret = drm_sman_set_range(&dev_priv->sman, VIDEO_TYPE, 0,
105                                  fb.size >> SIS_MM_ALIGN_SHIFT);
106 #endif
107
108         if (ret) {
109                 DRM_ERROR("VRAM memory manager initialisation error\n");
110                 mutex_unlock(&dev->struct_mutex);
111                 return ret;
112         }
113
114         dev_priv->vram_initialized = TRUE;
115         dev_priv->vram_offset = fb.offset;
116
117         mutex_unlock(&dev->struct_mutex);
118         DRM_DEBUG("offset = %u, size = %u", fb.offset, fb.size);
119
120         return 0;
121 }
122
123 static int sis_drm_alloc(drm_device_t * dev, drm_file_t * priv,
124                          unsigned long data, int pool)
125 {
126         drm_sis_private_t *dev_priv = dev->dev_private;
127         drm_sis_mem_t __user *argp = (drm_sis_mem_t __user *) data;
128         drm_sis_mem_t mem;
129         int retval = 0;
130         drm_memblock_item_t *item;
131
132         DRM_COPY_FROM_USER_IOCTL(mem, argp, sizeof(mem));
133
134         mutex_lock(&dev->struct_mutex);
135
136         if (FALSE == ((pool == 0) ? dev_priv->vram_initialized :
137                       dev_priv->agp_initialized)) {
138                 DRM_ERROR
139                     ("Attempt to allocate from uninitialized memory manager.\n");
140                 return DRM_ERR(EINVAL);
141         }
142
143         mem.size = (mem.size + SIS_MM_ALIGN_MASK) >> SIS_MM_ALIGN_SHIFT;
144         item = drm_sman_alloc(&dev_priv->sman, pool, mem.size, 0,
145                               (unsigned long)priv);
146
147         mutex_unlock(&dev->struct_mutex);
148         if (item) {
149                 mem.offset = ((pool == 0) ?
150                               dev_priv->vram_offset : dev_priv->agp_offset) +
151                     (item->mm->
152                      offset(item->mm, item->mm_info) << SIS_MM_ALIGN_SHIFT);
153                 mem.free = item->user_hash.key;
154                 mem.size = mem.size << SIS_MM_ALIGN_SHIFT;
155         } else {
156                 mem.offset = 0;
157                 mem.size = 0;
158                 mem.free = 0;
159                 retval = DRM_ERR(ENOMEM);
160         }
161
162         DRM_COPY_TO_USER_IOCTL(argp, mem, sizeof(mem));
163
164         DRM_DEBUG("alloc %d, size = %d, offset = %d\n", pool, mem.size,
165                   mem.offset);
166
167         return retval;
168 }
169
170 static int sis_drm_free(DRM_IOCTL_ARGS)
171 {
172         DRM_DEVICE;
173         drm_sis_private_t *dev_priv = dev->dev_private;
174         drm_sis_mem_t mem;
175         int ret;
176
177         DRM_COPY_FROM_USER_IOCTL(mem, (drm_sis_mem_t __user *) data,
178                                  sizeof(mem));
179
180         mutex_lock(&dev->struct_mutex);
181         ret = drm_sman_free_key(&dev_priv->sman, mem.free);
182         mutex_unlock(&dev->struct_mutex);
183         DRM_DEBUG("free = 0x%lx\n", mem.free);
184
185         return ret;
186 }
187
188 static int sis_fb_alloc(DRM_IOCTL_ARGS)
189 {
190         DRM_DEVICE;
191         return sis_drm_alloc(dev, priv, data, VIDEO_TYPE);
192 }
193
194 static int sis_ioctl_agp_init(DRM_IOCTL_ARGS)
195 {
196         DRM_DEVICE;
197         drm_sis_private_t *dev_priv = dev->dev_private;
198         drm_sis_agp_t agp;
199         int ret;
200         dev_priv = dev->dev_private;
201
202         DRM_COPY_FROM_USER_IOCTL(agp, (drm_sis_agp_t __user *) data,
203                                  sizeof(agp));
204         mutex_lock(&dev->struct_mutex);
205         ret = drm_sman_set_range(&dev_priv->sman, AGP_TYPE, 0,
206                                  agp.size >> SIS_MM_ALIGN_SHIFT);
207
208         if (ret) {
209                 DRM_ERROR("AGP memory manager initialisation error\n");
210                 mutex_unlock(&dev->struct_mutex);
211                 return ret;
212         }
213
214         dev_priv->agp_initialized = TRUE;
215         dev_priv->agp_offset = agp.offset;
216         mutex_unlock(&dev->struct_mutex);
217
218         DRM_DEBUG("offset = %u, size = %u", agp.offset, agp.size);
219         return 0;
220 }
221
222 static int sis_ioctl_agp_alloc(DRM_IOCTL_ARGS)
223 {
224         DRM_DEVICE;
225
226         return sis_drm_alloc(dev, priv, data, AGP_TYPE);
227 }
228
229 void sis_lastclose(struct drm_device *dev)
230 {
231         drm_sis_private_t *dev_priv = dev->dev_private;
232
233         if (!dev_priv)
234                 return;
235
236         mutex_lock(&dev->struct_mutex);
237         drm_sman_cleanup(&dev_priv->sman);
238         dev_priv->vram_initialized = FALSE;
239         dev_priv->agp_initialized = FALSE;
240         mutex_unlock(&dev->struct_mutex);
241 }
242
243 void sis_reclaim_buffers_locked(drm_device_t * dev, struct file *filp)
244 {
245         drm_sis_private_t *dev_priv = dev->dev_private;
246         drm_file_t *priv = filp->private_data;
247
248         mutex_lock(&dev->struct_mutex);
249         if (drm_sman_owner_clean(&dev_priv->sman, (unsigned long)priv)) {
250                 mutex_unlock(&dev->struct_mutex);
251                 return;
252         }
253
254         if (dev->driver->dma_quiescent) {
255                 dev->driver->dma_quiescent(dev);
256         }
257
258         drm_sman_owner_cleanup(&dev_priv->sman, (unsigned long)priv);
259         mutex_unlock(&dev->struct_mutex);
260         return;
261 }
262
263 drm_ioctl_desc_t sis_ioctls[] = {
264         [DRM_IOCTL_NR(DRM_SIS_FB_ALLOC)] = {sis_fb_alloc, DRM_AUTH},
265         [DRM_IOCTL_NR(DRM_SIS_FB_FREE)] = {sis_drm_free, DRM_AUTH},
266         [DRM_IOCTL_NR(DRM_SIS_AGP_INIT)] =
267             {sis_ioctl_agp_init, DRM_AUTH | DRM_MASTER | DRM_ROOT_ONLY},
268         [DRM_IOCTL_NR(DRM_SIS_AGP_ALLOC)] = {sis_ioctl_agp_alloc, DRM_AUTH},
269         [DRM_IOCTL_NR(DRM_SIS_AGP_FREE)] = {sis_drm_free, DRM_AUTH},
270         [DRM_IOCTL_NR(DRM_SIS_FB_INIT)] =
271             {sis_fb_init, DRM_AUTH | DRM_MASTER | DRM_ROOT_ONLY}
272 };
273
274 int sis_max_ioctl = DRM_ARRAY_SIZE(sis_ioctls);