OSDN Git Service

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