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 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #if defined(__linux__) && defined(CONFIG_FB_SIS)
35 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
36 #include <video/sisfb.h>
38 #include <linux/sisfb.h>
41 #include "dev/drm/drmP.h"
42 #include "dev/drm/sis_drm.h"
43 #include "dev/drm/sis_drv.h"
44 #include "dev/drm/sis_ds.h"
46 #define MAX_CONTEXT 100
53 set_t *sets[2]; /* 0 for video, 1 for AGP */
56 static sis_context_t global_ppriv[MAX_CONTEXT];
58 static int add_alloc_set(int context, int type, unsigned int val)
62 for (i = 0; i < MAX_CONTEXT; i++) {
63 if (global_ppriv[i].used && global_ppriv[i].context == context) {
64 retval = setAdd(global_ppriv[i].sets[type], val);
71 static int del_alloc_set(int context, int type, unsigned int val)
75 for (i = 0; i < MAX_CONTEXT; i++) {
76 if (global_ppriv[i].used && global_ppriv[i].context == context) {
77 retval = setDel(global_ppriv[i].sets[type], val);
84 /* fb management via fb device */
85 #if defined(__linux__) && defined(CONFIG_FB_SIS)
87 static int sis_fb_init(struct drm_device *dev, void *data, struct drm_file *file_priv)
92 static int sis_fb_alloc(struct drm_device *dev, void *data, struct drm_file *file_priv)
94 drm_sis_mem_t *fb = data;
95 struct sis_memreq req;
102 fb->offset = req.offset;
103 fb->free = req.offset;
104 if (!add_alloc_set(fb->context, VIDEO_TYPE, fb->free)) {
105 DRM_DEBUG("adding to allocation set fails\n");
106 sis_free(req.offset);
115 DRM_DEBUG("alloc fb, size = %d, offset = %ld\n", fb->size, req.offset);
120 static int sis_fb_free(struct drm_device *dev, void *data, struct drm_file *file_priv)
128 if (!del_alloc_set(fb->context, VIDEO_TYPE, fb->free))
132 DRM_DEBUG("free fb, offset = 0x%lx\n", fb->free);
139 /* Called by the X Server to initialize the FB heap. Allocations will fail
140 * unless this is called. Offset is the beginning of the heap from the
141 * framebuffer offset (MaxXFBMem in XFree86).
143 * Memory layout according to Thomas Winischofer:
144 * |------------------|DDDDDDDDDDDDDDDDDDDDDDDDDDDDD|HHHH|CCCCCCCCCCC|
146 * X driver/sisfb HW- Command-
147 * framebuffer memory DRI heap Cursor queue
149 static int sis_fb_init(struct drm_device *dev, void *data, struct drm_file *file_priv)
151 drm_sis_private_t *dev_priv = dev->dev_private;
152 drm_sis_fb_t *fb = data;
154 if (dev_priv == NULL) {
155 dev->dev_private = drm_calloc(1, sizeof(drm_sis_private_t),
157 dev_priv = dev->dev_private;
158 if (dev_priv == NULL)
162 if (dev_priv->FBHeap != NULL)
165 dev_priv->FBHeap = mmInit(fb->offset, fb->size);
167 DRM_DEBUG("offset = %u, size = %u", fb->offset, fb->size);
172 static int sis_fb_alloc(struct drm_device *dev, void *data, struct drm_file *file_priv)
174 drm_sis_private_t *dev_priv = dev->dev_private;
175 drm_sis_mem_t *fb = data;
179 if (dev_priv == NULL || dev_priv->FBHeap == NULL)
182 block = mmAllocMem(dev_priv->FBHeap, fb->size, 0, 0);
185 fb->offset = block->ofs;
186 fb->free = (unsigned long)block;
187 if (!add_alloc_set(fb->context, VIDEO_TYPE, fb->free)) {
188 DRM_DEBUG("adding to allocation set fails\n");
189 mmFreeMem((PMemBlock) fb->free);
198 DRM_DEBUG("alloc fb, size = %d, offset = %d\n", fb->size, fb->offset);
203 static int sis_fb_free(struct drm_device *dev, void *data, struct drm_file *file_priv)
205 drm_sis_private_t *dev_priv = dev->dev_private;
206 drm_sis_mem_t *fb = data;
208 if (dev_priv == NULL || dev_priv->FBHeap == NULL)
211 if (!mmBlockInHeap(dev_priv->FBHeap, (PMemBlock) fb->free))
214 if (!del_alloc_set(fb->context, VIDEO_TYPE, fb->free))
216 mmFreeMem((PMemBlock) fb->free);
218 DRM_DEBUG("free fb, free = 0x%lx\n", fb->free);
225 /* agp memory management */
227 static int sis_ioctl_agp_init(struct drm_device *dev, void *data, struct drm_file *file_priv)
229 drm_sis_private_t *dev_priv = dev->dev_private;
230 drm_sis_agp_t *agp = data;
232 if (dev_priv == NULL) {
233 dev->dev_private = drm_calloc(1, sizeof(drm_sis_private_t),
235 dev_priv = dev->dev_private;
236 if (dev_priv == NULL)
240 if (dev_priv->AGPHeap != NULL)
243 dev_priv->AGPHeap = mmInit(agp->offset, agp->size);
245 DRM_DEBUG("offset = %u, size = %u", agp->offset, agp->size);
250 static int sis_ioctl_agp_alloc(struct drm_device *dev, void *data, struct drm_file *file_priv)
252 drm_sis_private_t *dev_priv = dev->dev_private;
253 drm_sis_mem_t *agp = data;
257 if (dev_priv == NULL || dev_priv->AGPHeap == NULL)
260 block = mmAllocMem(dev_priv->AGPHeap, agp->size, 0, 0);
263 agp->offset = block->ofs;
264 agp->free = (unsigned long)block;
265 if (!add_alloc_set(agp->context, AGP_TYPE, agp->free)) {
266 DRM_DEBUG("adding to allocation set fails\n");
267 mmFreeMem((PMemBlock) agp->free);
276 DRM_DEBUG("alloc agp, size = %d, offset = %d\n", agp->size,
282 static int sis_ioctl_agp_free(struct drm_device *dev, void *data, struct drm_file *file_priv)
284 drm_sis_private_t *dev_priv = dev->dev_private;
285 drm_sis_mem_t *agp = data;
287 if (dev_priv == NULL || dev_priv->AGPHeap == NULL)
290 if (!mmBlockInHeap(dev_priv->AGPHeap, (PMemBlock) agp->free))
293 mmFreeMem((PMemBlock) agp->free);
294 if (!del_alloc_set(agp->context, AGP_TYPE, agp->free))
297 DRM_DEBUG("free agp, free = 0x%lx\n", agp->free);
302 int sis_init_context(struct drm_device *dev, int context)
306 for (i = 0; i < MAX_CONTEXT; i++) {
307 if (global_ppriv[i].used &&
308 (global_ppriv[i].context == context))
312 if (i >= MAX_CONTEXT) {
313 for (i = 0; i < MAX_CONTEXT; i++) {
314 if (!global_ppriv[i].used) {
315 global_ppriv[i].context = context;
316 global_ppriv[i].used = 1;
317 global_ppriv[i].sets[0] = setInit();
318 global_ppriv[i].sets[1] = setInit();
319 DRM_DEBUG("init allocation set, socket=%d, "
320 "context = %d\n", i, context);
324 if ((i >= MAX_CONTEXT) || (global_ppriv[i].sets[0] == NULL) ||
325 (global_ppriv[i].sets[1] == NULL)) {
333 int sis_final_context(struct drm_device *dev, int context)
337 for (i = 0; i < MAX_CONTEXT; i++) {
338 if (global_ppriv[i].used &&
339 (global_ppriv[i].context == context))
343 if (i < MAX_CONTEXT) {
348 DRM_DEBUG("find socket %d, context = %d\n", i, context);
351 set = global_ppriv[i].sets[0];
352 retval = setFirst(set, &item);
354 DRM_DEBUG("free video memory 0x%lx\n", item);
355 #if defined(__linux__) && defined(CONFIG_FB_SIS)
358 mmFreeMem((PMemBlock) item);
360 retval = setNext(set, &item);
365 set = global_ppriv[i].sets[1];
366 retval = setFirst(set, &item);
368 DRM_DEBUG("free agp memory 0x%lx\n", item);
369 mmFreeMem((PMemBlock) item);
370 retval = setNext(set, &item);
374 global_ppriv[i].used = 0;
380 drm_ioctl_desc_t sis_ioctls[] = {
381 DRM_IOCTL_DEF(DRM_SIS_FB_ALLOC, sis_fb_alloc, DRM_AUTH),
382 DRM_IOCTL_DEF(DRM_SIS_FB_FREE, sis_fb_free, DRM_AUTH),
383 DRM_IOCTL_DEF(DRM_SIS_AGP_INIT, sis_ioctl_agp_init, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
384 DRM_IOCTL_DEF(DRM_SIS_AGP_ALLOC, sis_ioctl_agp_alloc, DRM_AUTH),
385 DRM_IOCTL_DEF(DRM_SIS_AGP_FREE, sis_ioctl_agp_free, DRM_AUTH),
386 DRM_IOCTL_DEF(DRM_SIS_FB_INIT, sis_fb_init, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY)
389 int sis_max_ioctl = DRM_ARRAY_SIZE(sis_ioctls);