OSDN Git Service

remove all TRUE instances as well
[android-x86/external-libdrm.git] / linux-core / 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 #if defined(__linux__)
39 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
40 #include <video/sisfb.h>
41 #else
42 #include <linux/sisfb.h>
43 #endif
44 #endif
45
46 #define VIDEO_TYPE 0
47 #define AGP_TYPE 1
48
49 #define SIS_MM_ALIGN_SHIFT 4
50 #define SIS_MM_ALIGN_MASK ( (1 << SIS_MM_ALIGN_SHIFT) - 1)
51
52 #if defined(__linux__) && defined(CONFIG_FB_SIS)
53 /* fb management via fb device */
54
55 #define SIS_MM_ALIGN_SHIFT 0
56 #define SIS_MM_ALIGN_MASK 0
57
58 static void *sis_sman_mm_allocate(void *private, unsigned long size,
59                                   unsigned alignment)
60 {
61         struct sis_memreq req;
62
63         req.size = size;
64         sis_malloc(&req);
65         if (req.size == 0)
66                 return NULL;
67         else
68                 return (void *)~req.offset;
69 }
70
71 static void sis_sman_mm_free(void *private, void *ref)
72 {
73         sis_free(~((unsigned long)ref));
74 }
75
76 static void sis_sman_mm_destroy(void *private)
77 {
78         ;
79 }
80
81 unsigned long sis_sman_mm_offset(void *private, void *ref)
82 {
83         return ~((unsigned long)ref);
84 }
85
86 #endif
87
88 static int sis_fb_init(DRM_IOCTL_ARGS)
89 {
90         DRM_DEVICE;
91         drm_sis_private_t *dev_priv = dev->dev_private;
92         drm_sis_fb_t fb;
93         int ret;
94
95         DRM_COPY_FROM_USER_IOCTL(fb, (drm_sis_fb_t __user *) data, sizeof(fb));
96
97         mutex_lock(&dev->struct_mutex);
98 #if defined(__linux__) && defined(CONFIG_FB_SIS)
99         {
100                 drm_sman_mm_t sman_mm;
101                 sman_mm.private = (void *)0xFFFFFFFF;
102                 sman_mm.allocate = sis_sman_mm_allocate;
103                 sman_mm.free = sis_sman_mm_free;
104                 sman_mm.destroy = sis_sman_mm_destroy;
105                 sman_mm.offset = sis_sman_mm_offset;
106                 ret =
107                     drm_sman_set_manager(&dev_priv->sman, VIDEO_TYPE, &sman_mm);
108         }
109 #else
110         ret = drm_sman_set_range(&dev_priv->sman, VIDEO_TYPE, 0,
111                                  fb.size >> SIS_MM_ALIGN_SHIFT);
112 #endif
113
114         if (ret) {
115                 DRM_ERROR("VRAM memory manager initialisation error\n");
116                 mutex_unlock(&dev->struct_mutex);
117                 return ret;
118         }
119
120         dev_priv->vram_initialized = 1;
121         dev_priv->vram_offset = fb.offset;
122
123         mutex_unlock(&dev->struct_mutex);
124         DRM_DEBUG("offset = %u, size = %u", fb.offset, fb.size);
125
126         return 0;
127 }
128
129 static int sis_drm_alloc(drm_device_t * dev, drm_file_t * priv,
130                          unsigned long data, int pool)
131 {
132         drm_sis_private_t *dev_priv = dev->dev_private;
133         drm_sis_mem_t __user *argp = (drm_sis_mem_t __user *) data;
134         drm_sis_mem_t mem;
135         int retval = 0;
136         drm_memblock_item_t *item;
137
138         DRM_COPY_FROM_USER_IOCTL(mem, argp, sizeof(mem));
139
140         mutex_lock(&dev->struct_mutex);
141
142         if (0 == ((pool == 0) ? dev_priv->vram_initialized :
143                       dev_priv->agp_initialized)) {
144                 DRM_ERROR
145                     ("Attempt to allocate from uninitialized memory manager.\n");
146                 return DRM_ERR(EINVAL);
147         }
148
149         mem.size = (mem.size + SIS_MM_ALIGN_MASK) >> SIS_MM_ALIGN_SHIFT;
150         item = drm_sman_alloc(&dev_priv->sman, pool, mem.size, 0,
151                               (unsigned long)priv);
152
153         mutex_unlock(&dev->struct_mutex);
154         if (item) {
155                 mem.offset = ((pool == 0) ?
156                               dev_priv->vram_offset : dev_priv->agp_offset) +
157                     (item->mm->
158                      offset(item->mm, item->mm_info) << SIS_MM_ALIGN_SHIFT);
159                 mem.free = item->user_hash.key;
160                 mem.size = mem.size << SIS_MM_ALIGN_SHIFT;
161         } else {
162                 mem.offset = 0;
163                 mem.size = 0;
164                 mem.free = 0;
165                 retval = DRM_ERR(ENOMEM);
166         }
167
168         DRM_COPY_TO_USER_IOCTL(argp, mem, sizeof(mem));
169
170         DRM_DEBUG("alloc %d, size = %d, offset = %d\n", pool, mem.size,
171                   mem.offset);
172
173         return retval;
174 }
175
176 static int sis_drm_free(DRM_IOCTL_ARGS)
177 {
178         DRM_DEVICE;
179         drm_sis_private_t *dev_priv = dev->dev_private;
180         drm_sis_mem_t mem;
181         int ret;
182
183         DRM_COPY_FROM_USER_IOCTL(mem, (drm_sis_mem_t __user *) data,
184                                  sizeof(mem));
185
186         mutex_lock(&dev->struct_mutex);
187         ret = drm_sman_free_key(&dev_priv->sman, mem.free);
188         mutex_unlock(&dev->struct_mutex);
189         DRM_DEBUG("free = 0x%lx\n", mem.free);
190
191         return ret;
192 }
193
194 static int sis_fb_alloc(DRM_IOCTL_ARGS)
195 {
196         DRM_DEVICE;
197         return sis_drm_alloc(dev, priv, data, VIDEO_TYPE);
198 }
199
200 static int sis_ioctl_agp_init(DRM_IOCTL_ARGS)
201 {
202         DRM_DEVICE;
203         drm_sis_private_t *dev_priv = dev->dev_private;
204         drm_sis_agp_t agp;
205         int ret;
206         dev_priv = dev->dev_private;
207
208         DRM_COPY_FROM_USER_IOCTL(agp, (drm_sis_agp_t __user *) data,
209                                  sizeof(agp));
210         mutex_lock(&dev->struct_mutex);
211         ret = drm_sman_set_range(&dev_priv->sman, AGP_TYPE, 0,
212                                  agp.size >> SIS_MM_ALIGN_SHIFT);
213
214         if (ret) {
215                 DRM_ERROR("AGP memory manager initialisation error\n");
216                 mutex_unlock(&dev->struct_mutex);
217                 return ret;
218         }
219
220         dev_priv->agp_initialized = 1;
221         dev_priv->agp_offset = agp.offset;
222         mutex_unlock(&dev->struct_mutex);
223
224         DRM_DEBUG("offset = %u, size = %u", agp.offset, agp.size);
225         return 0;
226 }
227
228 static int sis_ioctl_agp_alloc(DRM_IOCTL_ARGS)
229 {
230         DRM_DEVICE;
231
232         return sis_drm_alloc(dev, priv, data, AGP_TYPE);
233 }
234
235 static drm_local_map_t *sis_reg_init(drm_device_t *dev)
236 {
237         drm_map_list_t *entry;
238         drm_local_map_t *map;
239
240         list_for_each_entry(entry, &dev->maplist->head, head) {
241                 map = entry->map;
242                 if (!map)
243                         continue;
244                 if (map->type == _DRM_REGISTERS) {
245                         return map;
246                 }
247         }
248         return NULL;
249 }
250
251 int
252 sis_idle(drm_device_t *dev)
253 {
254         drm_sis_private_t *dev_priv = dev->dev_private;
255         uint32_t idle_reg;
256         unsigned long end;
257         int i;
258
259         if (dev_priv->idle_fault)
260                 return 0;
261
262         if (dev_priv->mmio == NULL) {
263                 dev_priv->mmio = sis_reg_init(dev);
264                 if (dev_priv->mmio == NULL) {
265                         DRM_ERROR("Could not find register map.\n");
266                         return 0;
267                 }
268         }
269         
270         /*
271          * Implement a device switch here if needed
272          */
273
274         if (dev_priv->chipset != SIS_CHIP_315)
275                 return 0;
276
277         /*
278          * Timeout after 3 seconds. We cannot use DRM_WAIT_ON here
279          * because its polling frequency is too low.
280          */
281
282         end = jiffies + (DRM_HZ * 3);
283
284         for (i=0; i<4; ++i) {
285                 do {
286                         idle_reg = SIS_READ(0x85cc);
287                 } while ( !time_after_eq(jiffies, end) &&
288                           ((idle_reg & 0x80000000) != 0x80000000));
289         }
290
291         if (time_after_eq(jiffies, end)) {
292                 DRM_ERROR("Graphics engine idle timeout. "
293                           "Disabling idle check\n");
294                 dev_priv->idle_fault = 1;
295         }
296
297         /*
298          * The caller never sees an error code. It gets trapped
299          * in libdrm.
300          */
301
302         return 0;
303 }
304
305
306 void sis_lastclose(struct drm_device *dev)
307 {
308         drm_sis_private_t *dev_priv = dev->dev_private;
309
310         if (!dev_priv)
311                 return;
312
313         mutex_lock(&dev->struct_mutex);
314         drm_sman_cleanup(&dev_priv->sman);
315         dev_priv->vram_initialized = 0;
316         dev_priv->agp_initialized = 0;
317         dev_priv->mmio = NULL;
318         mutex_unlock(&dev->struct_mutex);
319 }
320
321 void sis_reclaim_buffers_locked(drm_device_t * dev, struct file *filp)
322 {
323         drm_sis_private_t *dev_priv = dev->dev_private;
324         drm_file_t *priv = filp->private_data;
325
326         mutex_lock(&dev->struct_mutex);
327         if (drm_sman_owner_clean(&dev_priv->sman, (unsigned long)priv)) {
328                 mutex_unlock(&dev->struct_mutex);
329                 return;
330         }
331
332         if (dev->driver->dma_quiescent) {
333                 dev->driver->dma_quiescent(dev);
334         }
335
336         drm_sman_owner_cleanup(&dev_priv->sman, (unsigned long)priv);
337         mutex_unlock(&dev->struct_mutex);
338         return;
339 }
340
341 drm_ioctl_desc_t sis_ioctls[] = {
342         [DRM_IOCTL_NR(DRM_SIS_FB_ALLOC)] = {sis_fb_alloc, DRM_AUTH},
343         [DRM_IOCTL_NR(DRM_SIS_FB_FREE)] = {sis_drm_free, DRM_AUTH},
344         [DRM_IOCTL_NR(DRM_SIS_AGP_INIT)] =
345             {sis_ioctl_agp_init, DRM_AUTH | DRM_MASTER | DRM_ROOT_ONLY},
346         [DRM_IOCTL_NR(DRM_SIS_AGP_ALLOC)] = {sis_ioctl_agp_alloc, DRM_AUTH},
347         [DRM_IOCTL_NR(DRM_SIS_AGP_FREE)] = {sis_drm_free, DRM_AUTH},
348         [DRM_IOCTL_NR(DRM_SIS_FB_INIT)] =
349             {sis_fb_init, DRM_AUTH | DRM_MASTER | DRM_ROOT_ONLY}
350 };
351
352 int sis_max_ioctl = DRM_ARRAY_SIZE(sis_ioctls);