Eliminate some dead initialization.
[dragonfly.git] / sys / dev / drm / sis_mm.c
blobb24d47c0abe578ba1aab43d682507c934daaeccf
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.
5 * All rights reserved.
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
16 * Software.
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.
26 * Authors:
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>
35 #else
36 #include <linux/sisfb.h>
37 #endif
38 #endif
39 #include "drmP.h"
40 #include "sis_drm.h"
41 #include "sis_drv.h"
42 #include "sis_ds.h"
44 #define MAX_CONTEXT 100
45 #define VIDEO_TYPE 0
46 #define AGP_TYPE 1
48 typedef struct {
49 int used;
50 int context;
51 set_t *sets[2]; /* 0 for video, 1 for AGP */
52 } sis_context_t;
54 static sis_context_t global_ppriv[MAX_CONTEXT];
56 static int add_alloc_set(int context, int type, unsigned int val)
58 int i, retval = 0;
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);
63 break;
66 return retval;
69 static int del_alloc_set(int context, int type, unsigned int val)
71 int i, retval = 0;
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);
76 break;
79 return retval;
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)
87 return 0;
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;
94 int retval = 0;
96 req.size = fb->size;
97 sis_malloc(&req);
98 if (req.offset) {
99 /* TODO */
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);
105 retval = -EINVAL;
107 } else {
108 fb->offset = 0;
109 fb->size = 0;
110 fb->free = 0;
113 DRM_DEBUG("alloc fb, size = %d, offset = %ld\n", fb->size, req.offset);
115 return retval;
118 static int sis_fb_free(struct drm_device *dev, void *data, struct drm_file *file_priv)
120 drm_sis_mem_t fb;
121 int retval = 0;
123 if (!fb->free)
124 return -EINVAL;
126 if (!del_alloc_set(fb->context, VIDEO_TYPE, fb->free))
127 retval = -EINVAL;
128 sis_free(fb->free);
130 DRM_DEBUG("free fb, offset = 0x%lx\n", fb->free);
132 return retval;
135 #else
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),
154 DRM_MEM_DRIVER);
155 dev_priv = dev->dev_private;
156 if (dev_priv == NULL)
157 return ENOMEM;
160 if (dev_priv->FBHeap != NULL)
161 return -EINVAL;
163 dev_priv->FBHeap = mmInit(fb->offset, fb->size);
165 DRM_DEBUG("offset = %u, size = %u", fb->offset, fb->size);
167 return 0;
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;
174 PMemBlock block;
175 int retval = 0;
177 if (dev_priv == NULL || dev_priv->FBHeap == NULL)
178 return -EINVAL;
180 block = mmAllocMem(dev_priv->FBHeap, fb->size, 0, 0);
181 if (block) {
182 /* TODO */
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);
188 retval = -EINVAL;
190 } else {
191 fb->offset = 0;
192 fb->size = 0;
193 fb->free = 0;
196 DRM_DEBUG("alloc fb, size = %d, offset = %d\n", fb->size, fb->offset);
198 return retval;
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)
207 return -EINVAL;
209 if (!mmBlockInHeap(dev_priv->FBHeap, (PMemBlock) fb->free))
210 return -EINVAL;
212 if (!del_alloc_set(fb->context, VIDEO_TYPE, fb->free))
213 return -EINVAL;
214 mmFreeMem((PMemBlock) fb->free);
216 DRM_DEBUG("free fb, free = 0x%lx\n", fb->free);
218 return 0;
221 #endif
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),
232 DRM_MEM_DRIVER);
233 dev_priv = dev->dev_private;
234 if (dev_priv == NULL)
235 return ENOMEM;
238 if (dev_priv->AGPHeap != NULL)
239 return -EINVAL;
241 dev_priv->AGPHeap = mmInit(agp->offset, agp->size);
243 DRM_DEBUG("offset = %u, size = %u", agp->offset, agp->size);
245 return 0;
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;
252 PMemBlock block;
253 int retval = 0;
255 if (dev_priv == NULL || dev_priv->AGPHeap == NULL)
256 return -EINVAL;
258 block = mmAllocMem(dev_priv->AGPHeap, agp->size, 0, 0);
259 if (block) {
260 /* TODO */
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);
266 retval = -1;
268 } else {
269 agp->offset = 0;
270 agp->size = 0;
271 agp->free = 0;
274 DRM_DEBUG("alloc agp, size = %d, offset = %d\n", agp->size,
275 agp->offset);
277 return retval;
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)
286 return -EINVAL;
288 if (!mmBlockInHeap(dev_priv->AGPHeap, (PMemBlock) agp->free))
289 return -EINVAL;
291 mmFreeMem((PMemBlock) agp->free);
292 if (!del_alloc_set(agp->context, AGP_TYPE, agp->free))
293 return -EINVAL;
295 DRM_DEBUG("free agp, free = 0x%lx\n", agp->free);
297 return 0;
300 int sis_init_context(struct drm_device *dev, int context)
302 int i;
304 for (i = 0; i < MAX_CONTEXT; i++) {
305 if (global_ppriv[i].used &&
306 (global_ppriv[i].context == context))
307 break;
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);
319 break;
322 if ((i >= MAX_CONTEXT) || (global_ppriv[i].sets[0] == NULL) ||
323 (global_ppriv[i].sets[1] == NULL)) {
324 return 0;
328 return 1;
331 int sis_final_context(struct drm_device *dev, int context)
333 int i;
335 for (i = 0; i < MAX_CONTEXT; i++) {
336 if (global_ppriv[i].used &&
337 (global_ppriv[i].context == context))
338 break;
341 if (i < MAX_CONTEXT) {
342 set_t *set;
343 ITEM_TYPE item;
344 int retval;
346 DRM_DEBUG("find socket %d, context = %d\n", i, context);
348 /* Video Memory */
349 set = global_ppriv[i].sets[0];
350 retval = setFirst(set, &item);
351 while (retval) {
352 DRM_DEBUG("free video memory 0x%lx\n", item);
353 #if defined(__linux__) && defined(CONFIG_FB_SIS)
354 sis_free(item);
355 #else
356 mmFreeMem((PMemBlock) item);
357 #endif
358 retval = setNext(set, &item);
360 setDestroy(set);
362 /* AGP Memory */
363 set = global_ppriv[i].sets[1];
364 retval = setFirst(set, &item);
365 while (retval) {
366 DRM_DEBUG("free agp memory 0x%lx\n", item);
367 mmFreeMem((PMemBlock) item);
368 retval = setNext(set, &item);
370 setDestroy(set);
372 global_ppriv[i].used = 0;
375 return 1;
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);