1 /* sis_mm.c -- Private header for Direct Rendering Manager -*- linux-c -*-
2 * Created: Mon Jan 4 10:05:05 1999 by sclin@sis.com.tw
4 * Copyright 2000 Silicon Integrated Systems Corp, Inc., HsinChu, Taiwan.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice (including the next
15 * paragraph) shall be included in all copies or substantial portions of the
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 NONINFRINGEMENT. IN NO EVENT SHALL
21 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
22 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
27 * Sung-Ching Lin <sclin@sis.com.tw>
29 * $DragonFly: src/sys/dev/drm/sis_mm.c,v 1.1 2008/04/05 18:12:29 hasso Exp $
32 #if defined(__linux__) && defined(CONFIG_FB_SIS)
33 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
34 #include <video/sisfb.h>
36 #include <linux/sisfb.h>
44 #define MAX_CONTEXT 100
51 set_t
*sets
[2]; /* 0 for video, 1 for AGP */
54 static sis_context_t global_ppriv
[MAX_CONTEXT
];
56 static int add_alloc_set(int context
, int type
, unsigned int val
)
60 for (i
= 0; i
< MAX_CONTEXT
; i
++) {
61 if (global_ppriv
[i
].used
&& global_ppriv
[i
].context
== context
) {
62 retval
= setAdd(global_ppriv
[i
].sets
[type
], val
);
69 static int del_alloc_set(int context
, int type
, unsigned int val
)
73 for (i
= 0; i
< MAX_CONTEXT
; i
++) {
74 if (global_ppriv
[i
].used
&& global_ppriv
[i
].context
== context
) {
75 retval
= setDel(global_ppriv
[i
].sets
[type
], val
);
82 /* fb management via fb device */
83 #if defined(__linux__) && defined(CONFIG_FB_SIS)
85 static int sis_fb_init(struct drm_device
*dev
, void *data
, struct drm_file
*file_priv
)
90 static int sis_fb_alloc(struct drm_device
*dev
, void *data
, struct drm_file
*file_priv
)
92 drm_sis_mem_t
*fb
= data
;
93 struct sis_memreq req
;
100 fb
->offset
= req
.offset
;
101 fb
->free
= req
.offset
;
102 if (!add_alloc_set(fb
->context
, VIDEO_TYPE
, fb
->free
)) {
103 DRM_DEBUG("adding to allocation set fails\n");
104 sis_free(req
.offset
);
113 DRM_DEBUG("alloc fb, size = %d, offset = %ld\n", fb
->size
, req
.offset
);
118 static int sis_fb_free(struct drm_device
*dev
, void *data
, struct drm_file
*file_priv
)
126 if (!del_alloc_set(fb
->context
, VIDEO_TYPE
, fb
->free
))
130 DRM_DEBUG("free fb, offset = 0x%lx\n", fb
->free
);
137 /* Called by the X Server to initialize the FB heap. Allocations will fail
138 * unless this is called. Offset is the beginning of the heap from the
139 * framebuffer offset (MaxXFBMem in XFree86).
141 * Memory layout according to Thomas Winischofer:
142 * |------------------|DDDDDDDDDDDDDDDDDDDDDDDDDDDDD|HHHH|CCCCCCCCCCC|
144 * X driver/sisfb HW- Command-
145 * framebuffer memory DRI heap Cursor queue
147 static int sis_fb_init(struct drm_device
*dev
, void *data
, struct drm_file
*file_priv
)
149 drm_sis_private_t
*dev_priv
= dev
->dev_private
;
150 drm_sis_fb_t
*fb
= data
;
152 if (dev_priv
== NULL
) {
153 dev
->dev_private
= drm_calloc(1, sizeof(drm_sis_private_t
),
155 dev_priv
= dev
->dev_private
;
156 if (dev_priv
== NULL
)
160 if (dev_priv
->FBHeap
!= NULL
)
163 dev_priv
->FBHeap
= mmInit(fb
->offset
, fb
->size
);
165 DRM_DEBUG("offset = %u, size = %u", fb
->offset
, fb
->size
);
170 static int sis_fb_alloc(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
*fb
= data
;
177 if (dev_priv
== NULL
|| dev_priv
->FBHeap
== NULL
)
180 block
= mmAllocMem(dev_priv
->FBHeap
, fb
->size
, 0, 0);
183 fb
->offset
= block
->ofs
;
184 fb
->free
= (unsigned long)block
;
185 if (!add_alloc_set(fb
->context
, VIDEO_TYPE
, fb
->free
)) {
186 DRM_DEBUG("adding to allocation set fails\n");
187 mmFreeMem((PMemBlock
) fb
->free
);
196 DRM_DEBUG("alloc fb, size = %d, offset = %d\n", fb
->size
, fb
->offset
);
201 static int sis_fb_free(struct drm_device
*dev
, void *data
, struct drm_file
*file_priv
)
203 drm_sis_private_t
*dev_priv
= dev
->dev_private
;
204 drm_sis_mem_t
*fb
= data
;
206 if (dev_priv
== NULL
|| dev_priv
->FBHeap
== NULL
)
209 if (!mmBlockInHeap(dev_priv
->FBHeap
, (PMemBlock
) fb
->free
))
212 if (!del_alloc_set(fb
->context
, VIDEO_TYPE
, fb
->free
))
214 mmFreeMem((PMemBlock
) fb
->free
);
216 DRM_DEBUG("free fb, free = 0x%lx\n", fb
->free
);
223 /* agp memory management */
225 static int sis_ioctl_agp_init(struct drm_device
*dev
, void *data
, struct drm_file
*file_priv
)
227 drm_sis_private_t
*dev_priv
= dev
->dev_private
;
228 drm_sis_agp_t
*agp
= data
;
230 if (dev_priv
== NULL
) {
231 dev
->dev_private
= drm_calloc(1, sizeof(drm_sis_private_t
),
233 dev_priv
= dev
->dev_private
;
234 if (dev_priv
== NULL
)
238 if (dev_priv
->AGPHeap
!= NULL
)
241 dev_priv
->AGPHeap
= mmInit(agp
->offset
, agp
->size
);
243 DRM_DEBUG("offset = %u, size = %u", agp
->offset
, agp
->size
);
248 static int sis_ioctl_agp_alloc(struct drm_device
*dev
, void *data
, struct drm_file
*file_priv
)
250 drm_sis_private_t
*dev_priv
= dev
->dev_private
;
251 drm_sis_mem_t
*agp
= data
;
255 if (dev_priv
== NULL
|| dev_priv
->AGPHeap
== NULL
)
258 block
= mmAllocMem(dev_priv
->AGPHeap
, agp
->size
, 0, 0);
261 agp
->offset
= block
->ofs
;
262 agp
->free
= (unsigned long)block
;
263 if (!add_alloc_set(agp
->context
, AGP_TYPE
, agp
->free
)) {
264 DRM_DEBUG("adding to allocation set fails\n");
265 mmFreeMem((PMemBlock
) agp
->free
);
274 DRM_DEBUG("alloc agp, size = %d, offset = %d\n", agp
->size
,
280 static int sis_ioctl_agp_free(struct drm_device
*dev
, void *data
, struct drm_file
*file_priv
)
282 drm_sis_private_t
*dev_priv
= dev
->dev_private
;
283 drm_sis_mem_t
*agp
= data
;
285 if (dev_priv
== NULL
|| dev_priv
->AGPHeap
== NULL
)
288 if (!mmBlockInHeap(dev_priv
->AGPHeap
, (PMemBlock
) agp
->free
))
291 mmFreeMem((PMemBlock
) agp
->free
);
292 if (!del_alloc_set(agp
->context
, AGP_TYPE
, agp
->free
))
295 DRM_DEBUG("free agp, free = 0x%lx\n", agp
->free
);
300 int sis_init_context(struct drm_device
*dev
, int context
)
304 for (i
= 0; i
< MAX_CONTEXT
; i
++) {
305 if (global_ppriv
[i
].used
&&
306 (global_ppriv
[i
].context
== context
))
310 if (i
>= MAX_CONTEXT
) {
311 for (i
= 0; i
< MAX_CONTEXT
; i
++) {
312 if (!global_ppriv
[i
].used
) {
313 global_ppriv
[i
].context
= context
;
314 global_ppriv
[i
].used
= 1;
315 global_ppriv
[i
].sets
[0] = setInit();
316 global_ppriv
[i
].sets
[1] = setInit();
317 DRM_DEBUG("init allocation set, socket=%d, "
318 "context = %d\n", i
, context
);
322 if ((i
>= MAX_CONTEXT
) || (global_ppriv
[i
].sets
[0] == NULL
) ||
323 (global_ppriv
[i
].sets
[1] == NULL
)) {
331 int sis_final_context(struct drm_device
*dev
, int context
)
335 for (i
= 0; i
< MAX_CONTEXT
; i
++) {
336 if (global_ppriv
[i
].used
&&
337 (global_ppriv
[i
].context
== context
))
341 if (i
< MAX_CONTEXT
) {
346 DRM_DEBUG("find socket %d, context = %d\n", i
, context
);
349 set
= global_ppriv
[i
].sets
[0];
350 retval
= setFirst(set
, &item
);
352 DRM_DEBUG("free video memory 0x%lx\n", item
);
353 #if defined(__linux__) && defined(CONFIG_FB_SIS)
356 mmFreeMem((PMemBlock
) item
);
358 retval
= setNext(set
, &item
);
363 set
= global_ppriv
[i
].sets
[1];
364 retval
= setFirst(set
, &item
);
366 DRM_DEBUG("free agp memory 0x%lx\n", item
);
367 mmFreeMem((PMemBlock
) item
);
368 retval
= setNext(set
, &item
);
372 global_ppriv
[i
].used
= 0;
378 drm_ioctl_desc_t sis_ioctls
[] = {
379 DRM_IOCTL_DEF(DRM_SIS_FB_ALLOC
, sis_fb_alloc
, DRM_AUTH
),
380 DRM_IOCTL_DEF(DRM_SIS_FB_FREE
, sis_fb_free
, DRM_AUTH
),
381 DRM_IOCTL_DEF(DRM_SIS_AGP_INIT
, sis_ioctl_agp_init
, DRM_AUTH
|DRM_MASTER
|DRM_ROOT_ONLY
),
382 DRM_IOCTL_DEF(DRM_SIS_AGP_ALLOC
, sis_ioctl_agp_alloc
, DRM_AUTH
),
383 DRM_IOCTL_DEF(DRM_SIS_AGP_FREE
, sis_ioctl_agp_free
, DRM_AUTH
),
384 DRM_IOCTL_DEF(DRM_SIS_FB_INIT
, sis_fb_init
, DRM_AUTH
|DRM_MASTER
|DRM_ROOT_ONLY
)
387 int sis_max_ioctl
= DRM_ARRAY_SIZE(sis_ioctls
);