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>
31 #if defined(__linux__) && defined(CONFIG_FB_SIS)
32 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
33 #include <video/sisfb.h>
35 #include <linux/sisfb.h>
38 #include "dev/drm/drmP.h"
39 #include "dev/drm/sis_drm.h"
40 #include "dev/drm/sis_drv.h"
41 #include "dev/drm/sis_ds.h"
43 #define MAX_CONTEXT 100
50 set_t
*sets
[2]; /* 0 for video, 1 for AGP */
53 static sis_context_t global_ppriv
[MAX_CONTEXT
];
55 static int add_alloc_set(int context
, int type
, unsigned int val
)
59 for (i
= 0; i
< MAX_CONTEXT
; i
++) {
60 if (global_ppriv
[i
].used
&& global_ppriv
[i
].context
== context
) {
61 retval
= setAdd(global_ppriv
[i
].sets
[type
], val
);
68 static int del_alloc_set(int context
, int type
, unsigned int val
)
72 for (i
= 0; i
< MAX_CONTEXT
; i
++) {
73 if (global_ppriv
[i
].used
&& global_ppriv
[i
].context
== context
) {
74 retval
= setDel(global_ppriv
[i
].sets
[type
], val
);
81 /* fb management via fb device */
82 #if defined(__linux__) && defined(CONFIG_FB_SIS)
84 static int sis_fb_init(struct drm_device
*dev
, void *data
, struct drm_file
*file_priv
)
89 static int sis_fb_alloc(struct drm_device
*dev
, void *data
, struct drm_file
*file_priv
)
91 drm_sis_mem_t
*fb
= data
;
92 struct sis_memreq req
;
99 fb
->offset
= req
.offset
;
100 fb
->free
= req
.offset
;
101 if (!add_alloc_set(fb
->context
, VIDEO_TYPE
, fb
->free
)) {
102 DRM_DEBUG("adding to allocation set fails\n");
103 sis_free(req
.offset
);
112 DRM_DEBUG("alloc fb, size = %d, offset = %ld\n", fb
->size
, req
.offset
);
117 static int sis_fb_free(struct drm_device
*dev
, void *data
, struct drm_file
*file_priv
)
125 if (!del_alloc_set(fb
->context
, VIDEO_TYPE
, fb
->free
))
129 DRM_DEBUG("free fb, offset = 0x%lx\n", fb
->free
);
136 /* Called by the X Server to initialize the FB heap. Allocations will fail
137 * unless this is called. Offset is the beginning of the heap from the
138 * framebuffer offset (MaxXFBMem in XFree86).
140 * Memory layout according to Thomas Winischofer:
141 * |------------------|DDDDDDDDDDDDDDDDDDDDDDDDDDDDD|HHHH|CCCCCCCCCCC|
143 * X driver/sisfb HW- Command-
144 * framebuffer memory DRI heap Cursor queue
146 static int sis_fb_init(struct drm_device
*dev
, void *data
, struct drm_file
*file_priv
)
148 drm_sis_private_t
*dev_priv
= dev
->dev_private
;
149 drm_sis_fb_t
*fb
= data
;
151 if (dev_priv
== NULL
) {
152 dev
->dev_private
= drm_calloc(1, sizeof(drm_sis_private_t
),
154 dev_priv
= dev
->dev_private
;
155 if (dev_priv
== NULL
)
159 if (dev_priv
->FBHeap
!= NULL
)
162 dev_priv
->FBHeap
= mmInit(fb
->offset
, fb
->size
);
164 DRM_DEBUG("offset = %u, size = %u", fb
->offset
, fb
->size
);
169 static int sis_fb_alloc(struct drm_device
*dev
, void *data
, struct drm_file
*file_priv
)
171 drm_sis_private_t
*dev_priv
= dev
->dev_private
;
172 drm_sis_mem_t
*fb
= data
;
176 if (dev_priv
== NULL
|| dev_priv
->FBHeap
== NULL
)
179 block
= mmAllocMem(dev_priv
->FBHeap
, fb
->size
, 0, 0);
182 fb
->offset
= block
->ofs
;
183 fb
->free
= (unsigned long)block
;
184 if (!add_alloc_set(fb
->context
, VIDEO_TYPE
, fb
->free
)) {
185 DRM_DEBUG("adding to allocation set fails\n");
186 mmFreeMem((PMemBlock
) fb
->free
);
195 DRM_DEBUG("alloc fb, size = %d, offset = %d\n", fb
->size
, fb
->offset
);
200 static int sis_fb_free(struct drm_device
*dev
, void *data
, struct drm_file
*file_priv
)
202 drm_sis_private_t
*dev_priv
= dev
->dev_private
;
203 drm_sis_mem_t
*fb
= data
;
205 if (dev_priv
== NULL
|| dev_priv
->FBHeap
== NULL
)
208 if (!mmBlockInHeap(dev_priv
->FBHeap
, (PMemBlock
) fb
->free
))
211 if (!del_alloc_set(fb
->context
, VIDEO_TYPE
, fb
->free
))
213 mmFreeMem((PMemBlock
) fb
->free
);
215 DRM_DEBUG("free fb, free = 0x%lx\n", fb
->free
);
222 /* agp memory management */
224 static int sis_ioctl_agp_init(struct drm_device
*dev
, void *data
, struct drm_file
*file_priv
)
226 drm_sis_private_t
*dev_priv
= dev
->dev_private
;
227 drm_sis_agp_t
*agp
= data
;
229 if (dev_priv
== NULL
) {
230 dev
->dev_private
= drm_calloc(1, sizeof(drm_sis_private_t
),
232 dev_priv
= dev
->dev_private
;
233 if (dev_priv
== NULL
)
237 if (dev_priv
->AGPHeap
!= NULL
)
240 dev_priv
->AGPHeap
= mmInit(agp
->offset
, agp
->size
);
242 DRM_DEBUG("offset = %u, size = %u", agp
->offset
, agp
->size
);
247 static int sis_ioctl_agp_alloc(struct drm_device
*dev
, void *data
, struct drm_file
*file_priv
)
249 drm_sis_private_t
*dev_priv
= dev
->dev_private
;
250 drm_sis_mem_t
*agp
= data
;
254 if (dev_priv
== NULL
|| dev_priv
->AGPHeap
== NULL
)
257 block
= mmAllocMem(dev_priv
->AGPHeap
, agp
->size
, 0, 0);
260 agp
->offset
= block
->ofs
;
261 agp
->free
= (unsigned long)block
;
262 if (!add_alloc_set(agp
->context
, AGP_TYPE
, agp
->free
)) {
263 DRM_DEBUG("adding to allocation set fails\n");
264 mmFreeMem((PMemBlock
) agp
->free
);
273 DRM_DEBUG("alloc agp, size = %d, offset = %d\n", agp
->size
,
279 static int sis_ioctl_agp_free(struct drm_device
*dev
, void *data
, struct drm_file
*file_priv
)
281 drm_sis_private_t
*dev_priv
= dev
->dev_private
;
282 drm_sis_mem_t
*agp
= data
;
284 if (dev_priv
== NULL
|| dev_priv
->AGPHeap
== NULL
)
287 if (!mmBlockInHeap(dev_priv
->AGPHeap
, (PMemBlock
) agp
->free
))
290 mmFreeMem((PMemBlock
) agp
->free
);
291 if (!del_alloc_set(agp
->context
, AGP_TYPE
, agp
->free
))
294 DRM_DEBUG("free agp, free = 0x%lx\n", agp
->free
);
299 int sis_init_context(struct drm_device
*dev
, int context
)
303 for (i
= 0; i
< MAX_CONTEXT
; i
++) {
304 if (global_ppriv
[i
].used
&&
305 (global_ppriv
[i
].context
== context
))
309 if (i
>= MAX_CONTEXT
) {
310 for (i
= 0; i
< MAX_CONTEXT
; i
++) {
311 if (!global_ppriv
[i
].used
) {
312 global_ppriv
[i
].context
= context
;
313 global_ppriv
[i
].used
= 1;
314 global_ppriv
[i
].sets
[0] = setInit();
315 global_ppriv
[i
].sets
[1] = setInit();
316 DRM_DEBUG("init allocation set, socket=%d, "
317 "context = %d\n", i
, context
);
321 if ((i
>= MAX_CONTEXT
) || (global_ppriv
[i
].sets
[0] == NULL
) ||
322 (global_ppriv
[i
].sets
[1] == NULL
)) {
330 int sis_final_context(struct drm_device
*dev
, int context
)
334 for (i
= 0; i
< MAX_CONTEXT
; i
++) {
335 if (global_ppriv
[i
].used
&&
336 (global_ppriv
[i
].context
== context
))
340 if (i
< MAX_CONTEXT
) {
345 DRM_DEBUG("find socket %d, context = %d\n", i
, context
);
348 set
= global_ppriv
[i
].sets
[0];
349 retval
= setFirst(set
, &item
);
351 DRM_DEBUG("free video memory 0x%lx\n", item
);
352 #if defined(__linux__) && defined(CONFIG_FB_SIS)
355 mmFreeMem((PMemBlock
) item
);
357 retval
= setNext(set
, &item
);
362 set
= global_ppriv
[i
].sets
[1];
363 retval
= setFirst(set
, &item
);
365 DRM_DEBUG("free agp memory 0x%lx\n", item
);
366 mmFreeMem((PMemBlock
) item
);
367 retval
= setNext(set
, &item
);
371 global_ppriv
[i
].used
= 0;
377 drm_ioctl_desc_t sis_ioctls
[] = {
378 DRM_IOCTL_DEF(DRM_SIS_FB_ALLOC
, sis_fb_alloc
, DRM_AUTH
),
379 DRM_IOCTL_DEF(DRM_SIS_FB_FREE
, sis_fb_free
, DRM_AUTH
),
380 DRM_IOCTL_DEF(DRM_SIS_AGP_INIT
, sis_ioctl_agp_init
, DRM_AUTH
|DRM_MASTER
|DRM_ROOT_ONLY
),
381 DRM_IOCTL_DEF(DRM_SIS_AGP_ALLOC
, sis_ioctl_agp_alloc
, DRM_AUTH
),
382 DRM_IOCTL_DEF(DRM_SIS_AGP_FREE
, sis_ioctl_agp_free
, DRM_AUTH
),
383 DRM_IOCTL_DEF(DRM_SIS_FB_INIT
, sis_fb_init
, DRM_AUTH
|DRM_MASTER
|DRM_ROOT_ONLY
)
386 int sis_max_ioctl
= DRM_ARRAY_SIZE(sis_ioctls
);