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>
44 #if defined(CONFIG_FB_SIS)
45 /* fb management via fb device */
47 #define SIS_MM_ALIGN_SHIFT 0
48 #define SIS_MM_ALIGN_MASK 0
50 static void *sis_sman_mm_allocate(void *private, unsigned long size
,
53 struct sis_memreq req
;
60 return (void *)~req
.offset
;
63 static void sis_sman_mm_free(void *private, void *ref
)
65 sis_free(~((unsigned long)ref
));
68 static void sis_sman_mm_destroy(void *private)
73 static unsigned long sis_sman_mm_offset(void *private, void *ref
)
75 return ~((unsigned long)ref
);
78 #else /* CONFIG_FB_SIS */
80 #define SIS_MM_ALIGN_SHIFT 4
81 #define SIS_MM_ALIGN_MASK ( (1 << SIS_MM_ALIGN_SHIFT) - 1)
83 #endif /* CONFIG_FB_SIS */
85 static int sis_fb_init(struct drm_device
*dev
, void *data
, struct drm_file
*file_priv
)
87 drm_sis_private_t
*dev_priv
= dev
->dev_private
;
88 drm_sis_fb_t
*fb
= data
;
91 mutex_lock(&dev
->struct_mutex
);
92 #if defined(CONFIG_FB_SIS)
94 struct drm_sman_mm 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
;
101 drm_sman_set_manager(&dev_priv
->sman
, VIDEO_TYPE
, &sman_mm
);
104 ret
= drm_sman_set_range(&dev_priv
->sman
, VIDEO_TYPE
, 0,
105 fb
->size
>> SIS_MM_ALIGN_SHIFT
);
109 DRM_ERROR("VRAM memory manager initialisation error\n");
110 mutex_unlock(&dev
->struct_mutex
);
114 dev_priv
->vram_initialized
= 1;
115 dev_priv
->vram_offset
= fb
->offset
;
117 mutex_unlock(&dev
->struct_mutex
);
118 DRM_DEBUG("offset = %u, size = %u", fb
->offset
, fb
->size
);
123 static int sis_drm_alloc(struct drm_device
*dev
, struct drm_file
*file_priv
,
124 void *data
, int pool
)
126 drm_sis_private_t
*dev_priv
= dev
->dev_private
;
127 drm_sis_mem_t
*mem
= data
;
129 struct drm_memblock_item
*item
;
131 mutex_lock(&dev
->struct_mutex
);
133 if (0 == ((pool
== 0) ? dev_priv
->vram_initialized
:
134 dev_priv
->agp_initialized
)) {
136 ("Attempt to allocate from uninitialized memory manager.\n");
140 mem
->size
= (mem
->size
+ SIS_MM_ALIGN_MASK
) >> SIS_MM_ALIGN_SHIFT
;
141 item
= drm_sman_alloc(&dev_priv
->sman
, pool
, mem
->size
, 0,
142 (unsigned long)file_priv
);
144 mutex_unlock(&dev
->struct_mutex
);
146 mem
->offset
= ((pool
== 0) ?
147 dev_priv
->vram_offset
: dev_priv
->agp_offset
) +
149 offset(item
->mm
, item
->mm_info
) << SIS_MM_ALIGN_SHIFT
);
150 mem
->free
= item
->user_hash
.key
;
151 mem
->size
= mem
->size
<< SIS_MM_ALIGN_SHIFT
;
159 DRM_DEBUG("alloc %d, size = %d, offset = %d\n", pool
, mem
->size
,
165 static int sis_drm_free(struct drm_device
*dev
, void *data
, struct drm_file
*file_priv
)
167 drm_sis_private_t
*dev_priv
= dev
->dev_private
;
168 drm_sis_mem_t
*mem
= data
;
171 mutex_lock(&dev
->struct_mutex
);
172 ret
= drm_sman_free_key(&dev_priv
->sman
, mem
->free
);
173 mutex_unlock(&dev
->struct_mutex
);
174 DRM_DEBUG("free = 0x%lx\n", mem
->free
);
179 static int sis_fb_alloc(struct drm_device
*dev
, void *data
,
180 struct drm_file
*file_priv
)
182 return sis_drm_alloc(dev
, file_priv
, data
, VIDEO_TYPE
);
185 static int sis_ioctl_agp_init(struct drm_device
*dev
, void *data
,
186 struct drm_file
*file_priv
)
188 drm_sis_private_t
*dev_priv
= dev
->dev_private
;
189 drm_sis_agp_t
*agp
= data
;
191 dev_priv
= dev
->dev_private
;
193 mutex_lock(&dev
->struct_mutex
);
194 ret
= drm_sman_set_range(&dev_priv
->sman
, AGP_TYPE
, 0,
195 agp
->size
>> SIS_MM_ALIGN_SHIFT
);
198 DRM_ERROR("AGP memory manager initialisation error\n");
199 mutex_unlock(&dev
->struct_mutex
);
203 dev_priv
->agp_initialized
= 1;
204 dev_priv
->agp_offset
= agp
->offset
;
205 mutex_unlock(&dev
->struct_mutex
);
207 DRM_DEBUG("offset = %u, size = %u", agp
->offset
, agp
->size
);
211 static int sis_ioctl_agp_alloc(struct drm_device
*dev
, void *data
,
212 struct drm_file
*file_priv
)
215 return sis_drm_alloc(dev
, file_priv
, data
, AGP_TYPE
);
218 static drm_local_map_t
*sis_reg_init(struct drm_device
*dev
)
220 struct drm_map_list
*entry
;
221 drm_local_map_t
*map
;
223 list_for_each_entry(entry
, &dev
->maplist
, head
) {
227 if (map
->type
== _DRM_REGISTERS
) {
234 int sis_idle(struct drm_device
*dev
)
236 drm_sis_private_t
*dev_priv
= dev
->dev_private
;
241 if (dev_priv
->idle_fault
)
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");
253 * Implement a device switch here if needed
256 if (dev_priv
->chipset
!= SIS_CHIP_315
)
260 * Timeout after 3 seconds. We cannot use DRM_WAIT_ON here
261 * because its polling frequency is too low.
264 end
= jiffies
+ (DRM_HZ
* 3);
266 for (i
=0; i
<4; ++i
) {
268 idle_reg
= SIS_READ(0x85cc);
269 } while ( !time_after_eq(jiffies
, end
) &&
270 ((idle_reg
& 0x80000000) != 0x80000000));
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;
280 * The caller never sees an error code. It gets trapped
288 void sis_lastclose(struct drm_device
*dev
)
290 drm_sis_private_t
*dev_priv
= dev
->dev_private
;
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
);
303 void sis_reclaim_buffers_locked(struct drm_device
* dev
,
304 struct drm_file
*file_priv
)
306 drm_sis_private_t
*dev_priv
= dev
->dev_private
;
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
);
314 if (dev
->driver
->dma_quiescent
) {
315 dev
->driver
->dma_quiescent(dev
);
318 drm_sman_owner_cleanup(&dev_priv
->sman
, (unsigned long)file_priv
);
319 mutex_unlock(&dev
->struct_mutex
);
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
),
332 int sis_max_ioctl
= DRM_ARRAY_SIZE(sis_ioctls
);