1 /**************************************************************************
3 * Copyright 2006 Tungsten Graphics, Inc., Bismarck, ND., USA.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
27 **************************************************************************/
31 * Thomas Hellström <thomas-at-tungstengraphics-dot-com>
38 #include <video/sisfb.h>
45 struct drm_mm_node mm_node
;
46 struct sis_memreq req
;
47 struct list_head owner_list
;
50 #if defined(CONFIG_FB_SIS) || defined(CONFIG_FB_SIS_MODULE)
51 /* fb management via fb device */
53 #define SIS_MM_ALIGN_SHIFT 0
54 #define SIS_MM_ALIGN_MASK 0
56 #else /* CONFIG_FB_SIS[_MODULE] */
58 #define SIS_MM_ALIGN_SHIFT 4
59 #define SIS_MM_ALIGN_MASK ((1 << SIS_MM_ALIGN_SHIFT) - 1)
61 #endif /* CONFIG_FB_SIS[_MODULE] */
63 static int sis_fb_init(struct drm_device
*dev
, void *data
, struct drm_file
*file_priv
)
65 drm_sis_private_t
*dev_priv
= dev
->dev_private
;
66 drm_sis_fb_t
*fb
= data
;
68 mutex_lock(&dev
->struct_mutex
);
69 /* Unconditionally init the drm_mm, even though we don't use it when the
70 * fb sis driver is available - make cleanup easier. */
71 drm_mm_init(&dev_priv
->vram_mm
, 0, fb
->size
>> SIS_MM_ALIGN_SHIFT
);
73 dev_priv
->vram_initialized
= 1;
74 dev_priv
->vram_offset
= fb
->offset
;
76 mutex_unlock(&dev
->struct_mutex
);
77 DRM_DEBUG("offset = %u, size = %u\n", fb
->offset
, fb
->size
);
82 static int sis_drm_alloc(struct drm_device
*dev
, struct drm_file
*file
,
85 drm_sis_private_t
*dev_priv
= dev
->dev_private
;
86 drm_sis_mem_t
*mem
= data
;
87 int retval
= 0, user_key
;
88 struct sis_memblock
*item
;
89 struct sis_file_private
*file_priv
= file
->driver_priv
;
92 mutex_lock(&dev
->struct_mutex
);
94 if (0 == ((pool
== 0) ? dev_priv
->vram_initialized
:
95 dev_priv
->agp_initialized
)) {
97 ("Attempt to allocate from uninitialized memory manager.\n");
98 mutex_unlock(&dev
->struct_mutex
);
102 item
= kzalloc(sizeof(*item
), GFP_KERNEL
);
108 mem
->size
= (mem
->size
+ SIS_MM_ALIGN_MASK
) >> SIS_MM_ALIGN_SHIFT
;
109 if (pool
== AGP_TYPE
) {
110 retval
= drm_mm_insert_node(&dev_priv
->agp_mm
,
113 offset
= item
->mm_node
.start
;
115 #if defined(CONFIG_FB_SIS) || defined(CONFIG_FB_SIS_MODULE)
116 item
->req
.size
= mem
->size
;
117 sis_malloc(&item
->req
);
118 if (item
->req
.size
== 0)
120 offset
= item
->req
.offset
;
122 retval
= drm_mm_insert_node(&dev_priv
->vram_mm
,
125 offset
= item
->mm_node
.start
;
132 if (idr_pre_get(&dev_priv
->object_idr
, GFP_KERNEL
) == 0) {
137 retval
= idr_get_new_above(&dev_priv
->object_idr
, item
, 1, &user_key
);
138 if (retval
== -EAGAIN
)
143 list_add(&item
->owner_list
, &file_priv
->obj_list
);
144 mutex_unlock(&dev
->struct_mutex
);
146 mem
->offset
= ((pool
== 0) ?
147 dev_priv
->vram_offset
: dev_priv
->agp_offset
) +
148 (offset
<< SIS_MM_ALIGN_SHIFT
);
149 mem
->free
= user_key
;
150 mem
->size
= mem
->size
<< SIS_MM_ALIGN_SHIFT
;
155 drm_mm_remove_node(&item
->mm_node
);
158 mutex_unlock(&dev
->struct_mutex
);
164 DRM_DEBUG("alloc %d, size = %d, offset = %d\n", pool
, mem
->size
,
170 static int sis_drm_free(struct drm_device
*dev
, void *data
, struct drm_file
*file_priv
)
172 drm_sis_private_t
*dev_priv
= dev
->dev_private
;
173 drm_sis_mem_t
*mem
= data
;
174 struct sis_memblock
*obj
;
176 mutex_lock(&dev
->struct_mutex
);
177 obj
= idr_find(&dev_priv
->object_idr
, mem
->free
);
179 mutex_unlock(&dev
->struct_mutex
);
183 idr_remove(&dev_priv
->object_idr
, mem
->free
);
184 list_del(&obj
->owner_list
);
185 if (drm_mm_node_allocated(&obj
->mm_node
))
186 drm_mm_remove_node(&obj
->mm_node
);
187 #if defined(CONFIG_FB_SIS) || defined(CONFIG_FB_SIS_MODULE)
189 sis_free(obj
->req
.offset
);
192 mutex_unlock(&dev
->struct_mutex
);
193 DRM_DEBUG("free = 0x%lx\n", mem
->free
);
198 static int sis_fb_alloc(struct drm_device
*dev
, void *data
,
199 struct drm_file
*file_priv
)
201 return sis_drm_alloc(dev
, file_priv
, data
, VIDEO_TYPE
);
204 static int sis_ioctl_agp_init(struct drm_device
*dev
, void *data
,
205 struct drm_file
*file_priv
)
207 drm_sis_private_t
*dev_priv
= dev
->dev_private
;
208 drm_sis_agp_t
*agp
= data
;
209 dev_priv
= dev
->dev_private
;
211 mutex_lock(&dev
->struct_mutex
);
212 drm_mm_init(&dev_priv
->agp_mm
, 0, agp
->size
>> SIS_MM_ALIGN_SHIFT
);
214 dev_priv
->agp_initialized
= 1;
215 dev_priv
->agp_offset
= agp
->offset
;
216 mutex_unlock(&dev
->struct_mutex
);
218 DRM_DEBUG("offset = %u, size = %u\n", agp
->offset
, agp
->size
);
222 static int sis_ioctl_agp_alloc(struct drm_device
*dev
, void *data
,
223 struct drm_file
*file_priv
)
226 return sis_drm_alloc(dev
, file_priv
, data
, AGP_TYPE
);
229 static drm_local_map_t
*sis_reg_init(struct drm_device
*dev
)
231 struct drm_map_list
*entry
;
232 drm_local_map_t
*map
;
234 list_for_each_entry(entry
, &dev
->maplist
, head
) {
238 if (map
->type
== _DRM_REGISTERS
)
244 int sis_idle(struct drm_device
*dev
)
246 drm_sis_private_t
*dev_priv
= dev
->dev_private
;
251 if (dev_priv
->idle_fault
)
254 if (dev_priv
->mmio
== NULL
) {
255 dev_priv
->mmio
= sis_reg_init(dev
);
256 if (dev_priv
->mmio
== NULL
) {
257 DRM_ERROR("Could not find register map.\n");
263 * Implement a device switch here if needed
266 if (dev_priv
->chipset
!= SIS_CHIP_315
)
270 * Timeout after 3 seconds. We cannot use DRM_WAIT_ON here
271 * because its polling frequency is too low.
274 end
= jiffies
+ (DRM_HZ
* 3);
276 for (i
= 0; i
< 4; ++i
) {
278 idle_reg
= SIS_READ(0x85cc);
279 } while (!time_after_eq(jiffies
, end
) &&
280 ((idle_reg
& 0x80000000) != 0x80000000));
283 if (time_after_eq(jiffies
, end
)) {
284 DRM_ERROR("Graphics engine idle timeout. "
285 "Disabling idle check\n");
286 dev_priv
->idle_fault
= 1;
290 * The caller never sees an error code. It gets trapped
298 void sis_lastclose(struct drm_device
*dev
)
300 drm_sis_private_t
*dev_priv
= dev
->dev_private
;
305 mutex_lock(&dev
->struct_mutex
);
306 if (dev_priv
->vram_initialized
) {
307 drm_mm_takedown(&dev_priv
->vram_mm
);
308 dev_priv
->vram_initialized
= 0;
310 if (dev_priv
->agp_initialized
) {
311 drm_mm_takedown(&dev_priv
->agp_mm
);
312 dev_priv
->agp_initialized
= 0;
314 dev_priv
->mmio
= NULL
;
315 mutex_unlock(&dev
->struct_mutex
);
318 void sis_reclaim_buffers_locked(struct drm_device
*dev
,
319 struct drm_file
*file
)
321 struct sis_file_private
*file_priv
= file
->driver_priv
;
322 struct sis_memblock
*entry
, *next
;
324 mutex_lock(&dev
->struct_mutex
);
325 if (list_empty(&file_priv
->obj_list
)) {
326 mutex_unlock(&dev
->struct_mutex
);
330 if (dev
->driver
->dma_quiescent
)
331 dev
->driver
->dma_quiescent(dev
);
334 list_for_each_entry_safe(entry
, next
, &file_priv
->obj_list
,
336 list_del(&entry
->owner_list
);
337 if (drm_mm_node_allocated(&entry
->mm_node
))
338 drm_mm_remove_node(&entry
->mm_node
);
339 #if defined(CONFIG_FB_SIS) || defined(CONFIG_FB_SIS_MODULE)
341 sis_free(entry
->req
.offset
);
345 mutex_unlock(&dev
->struct_mutex
);
349 struct drm_ioctl_desc sis_ioctls
[] = {
350 DRM_IOCTL_DEF_DRV(SIS_FB_ALLOC
, sis_fb_alloc
, DRM_AUTH
),
351 DRM_IOCTL_DEF_DRV(SIS_FB_FREE
, sis_drm_free
, DRM_AUTH
),
352 DRM_IOCTL_DEF_DRV(SIS_AGP_INIT
, sis_ioctl_agp_init
, DRM_AUTH
| DRM_MASTER
| DRM_ROOT_ONLY
),
353 DRM_IOCTL_DEF_DRV(SIS_AGP_ALLOC
, sis_ioctl_agp_alloc
, DRM_AUTH
),
354 DRM_IOCTL_DEF_DRV(SIS_AGP_FREE
, sis_drm_free
, DRM_AUTH
),
355 DRM_IOCTL_DEF_DRV(SIS_FB_INIT
, sis_fb_init
, DRM_AUTH
| DRM_MASTER
| DRM_ROOT_ONLY
),
358 int sis_max_ioctl
= DRM_ARRAY_SIZE(sis_ioctls
);