Sync DRM code with FreeBSD trunk rev 190433.
[dragonfly.git] / sys / dev / drm / sis_mm.c
blobd8a0e0bcae8ca6e973d95803b12cebfec3deab53
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>
31 #if defined(__linux__) && defined(CONFIG_FB_SIS)
32 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
33 #include <video/sisfb.h>
34 #else
35 #include <linux/sisfb.h>
36 #endif
37 #endif
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
44 #define VIDEO_TYPE 0
45 #define AGP_TYPE 1
47 typedef struct {
48 int used;
49 int context;
50 set_t *sets[2]; /* 0 for video, 1 for AGP */
51 } sis_context_t;
53 static sis_context_t global_ppriv[MAX_CONTEXT];
55 static int add_alloc_set(int context, int type, unsigned int val)
57 int i, retval = 0;
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);
62 break;
65 return retval;
68 static int del_alloc_set(int context, int type, unsigned int val)
70 int i, retval = 0;
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);
75 break;
78 return retval;
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)
86 return 0;
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;
93 int retval = 0;
95 req.size = fb->size;
96 sis_malloc(&req);
97 if (req.offset) {
98 /* TODO */
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);
104 retval = -EINVAL;
106 } else {
107 fb->offset = 0;
108 fb->size = 0;
109 fb->free = 0;
112 DRM_DEBUG("alloc fb, size = %d, offset = %ld\n", fb->size, req.offset);
114 return retval;
117 static int sis_fb_free(struct drm_device *dev, void *data, struct drm_file *file_priv)
119 drm_sis_mem_t fb;
120 int retval = 0;
122 if (!fb->free)
123 return -EINVAL;
125 if (!del_alloc_set(fb->context, VIDEO_TYPE, fb->free))
126 retval = -EINVAL;
127 sis_free(fb->free);
129 DRM_DEBUG("free fb, offset = 0x%lx\n", fb->free);
131 return retval;
134 #else
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),
153 DRM_MEM_DRIVER);
154 dev_priv = dev->dev_private;
155 if (dev_priv == NULL)
156 return ENOMEM;
159 if (dev_priv->FBHeap != NULL)
160 return -EINVAL;
162 dev_priv->FBHeap = mmInit(fb->offset, fb->size);
164 DRM_DEBUG("offset = %u, size = %u", fb->offset, fb->size);
166 return 0;
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;
173 PMemBlock block;
174 int retval = 0;
176 if (dev_priv == NULL || dev_priv->FBHeap == NULL)
177 return -EINVAL;
179 block = mmAllocMem(dev_priv->FBHeap, fb->size, 0, 0);
180 if (block) {
181 /* TODO */
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);
187 retval = -EINVAL;
189 } else {
190 fb->offset = 0;
191 fb->size = 0;
192 fb->free = 0;
195 DRM_DEBUG("alloc fb, size = %d, offset = %d\n", fb->size, fb->offset);
197 return retval;
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)
206 return -EINVAL;
208 if (!mmBlockInHeap(dev_priv->FBHeap, (PMemBlock) fb->free))
209 return -EINVAL;
211 if (!del_alloc_set(fb->context, VIDEO_TYPE, fb->free))
212 return -EINVAL;
213 mmFreeMem((PMemBlock) fb->free);
215 DRM_DEBUG("free fb, free = 0x%lx\n", fb->free);
217 return 0;
220 #endif
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),
231 DRM_MEM_DRIVER);
232 dev_priv = dev->dev_private;
233 if (dev_priv == NULL)
234 return ENOMEM;
237 if (dev_priv->AGPHeap != NULL)
238 return -EINVAL;
240 dev_priv->AGPHeap = mmInit(agp->offset, agp->size);
242 DRM_DEBUG("offset = %u, size = %u", agp->offset, agp->size);
244 return 0;
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;
251 PMemBlock block;
252 int retval = 0;
254 if (dev_priv == NULL || dev_priv->AGPHeap == NULL)
255 return -EINVAL;
257 block = mmAllocMem(dev_priv->AGPHeap, agp->size, 0, 0);
258 if (block) {
259 /* TODO */
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);
265 retval = -1;
267 } else {
268 agp->offset = 0;
269 agp->size = 0;
270 agp->free = 0;
273 DRM_DEBUG("alloc agp, size = %d, offset = %d\n", agp->size,
274 agp->offset);
276 return retval;
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)
285 return -EINVAL;
287 if (!mmBlockInHeap(dev_priv->AGPHeap, (PMemBlock) agp->free))
288 return -EINVAL;
290 mmFreeMem((PMemBlock) agp->free);
291 if (!del_alloc_set(agp->context, AGP_TYPE, agp->free))
292 return -EINVAL;
294 DRM_DEBUG("free agp, free = 0x%lx\n", agp->free);
296 return 0;
299 int sis_init_context(struct drm_device *dev, int context)
301 int i;
303 for (i = 0; i < MAX_CONTEXT; i++) {
304 if (global_ppriv[i].used &&
305 (global_ppriv[i].context == context))
306 break;
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);
318 break;
321 if ((i >= MAX_CONTEXT) || (global_ppriv[i].sets[0] == NULL) ||
322 (global_ppriv[i].sets[1] == NULL)) {
323 return 0;
327 return 1;
330 int sis_final_context(struct drm_device *dev, int context)
332 int i;
334 for (i = 0; i < MAX_CONTEXT; i++) {
335 if (global_ppriv[i].used &&
336 (global_ppriv[i].context == context))
337 break;
340 if (i < MAX_CONTEXT) {
341 set_t *set;
342 ITEM_TYPE item;
343 int retval;
345 DRM_DEBUG("find socket %d, context = %d\n", i, context);
347 /* Video Memory */
348 set = global_ppriv[i].sets[0];
349 retval = setFirst(set, &item);
350 while (retval) {
351 DRM_DEBUG("free video memory 0x%lx\n", item);
352 #if defined(__linux__) && defined(CONFIG_FB_SIS)
353 sis_free(item);
354 #else
355 mmFreeMem((PMemBlock) item);
356 #endif
357 retval = setNext(set, &item);
359 setDestroy(set);
361 /* AGP Memory */
362 set = global_ppriv[i].sets[1];
363 retval = setFirst(set, &item);
364 while (retval) {
365 DRM_DEBUG("free agp memory 0x%lx\n", item);
366 mmFreeMem((PMemBlock) item);
367 retval = setNext(set, &item);
369 setDestroy(set);
371 global_ppriv[i].used = 0;
374 return 1;
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);