kernel tree reorganization stage 1: Major cvs repository work (not logged as
[dragonfly.git] / sys / dev / drm / radeon / radeon_mem.c
1 /* radeon_mem.c -- Simple agp/fb memory manager for radeon -*- linux-c -*-
2  *
3  * Copyright (C) The Weather Channel, Inc.  2002.  All Rights Reserved.
4  * 
5  * The Weather Channel (TM) funded Tungsten Graphics to develop the
6  * initial release of the Radeon 8500 driver under the XFree86 license.
7  * This notice must be preserved.
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a
10  * copy of this software and associated documentation files (the "Software"),
11  * to deal in the Software without restriction, including without limitation
12  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13  * and/or sell copies of the Software, and to permit persons to whom the
14  * Software is furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice (including the next
17  * paragraph) shall be included in all copies or substantial portions of the
18  * Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
23  * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
24  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
25  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26  * DEALINGS IN THE SOFTWARE.
27  *
28  * Authors:
29  *    Keith Whitwell <keith@tungstengraphics.com>
30  *
31  * $FreeBSD: src/sys/dev/drm/radeon_mem.c,v 1.2.2.1 2003/04/26 07:05:29 anholt Exp $
32  * $DragonFly: src/sys/dev/drm/radeon/Attic/radeon_mem.c,v 1.3 2003/08/07 21:16:55 dillon Exp $
33  */
34
35 #include "radeon.h"
36 #include "dev/drm/drmP.h"
37 #include "dev/drm/drm.h"
38 #include "radeon_drm.h"
39 #include "radeon_drv.h"
40
41 /* Very simple allocator for agp memory, working on a static range
42  * already mapped into each client's address space.  
43  */
44
45 static struct mem_block *split_block(struct mem_block *p, int start, int size,
46                                      DRMFILE filp )
47 {
48         /* Maybe cut off the start of an existing block */
49         if (start > p->start) {
50                 struct mem_block *newblock = DRM_MALLOC(sizeof(*newblock));
51                 if (!newblock) 
52                         goto out;
53                 newblock->start = start;
54                 newblock->size = p->size - (start - p->start);
55                 newblock->filp = 0;
56                 newblock->next = p->next;
57                 newblock->prev = p;
58                 p->next->prev = newblock;
59                 p->next = newblock;
60                 p->size -= newblock->size;
61                 p = newblock;
62         }
63    
64         /* Maybe cut off the end of an existing block */
65         if (size < p->size) {
66                 struct mem_block *newblock = DRM_MALLOC(sizeof(*newblock));
67                 if (!newblock)
68                         goto out;
69                 newblock->start = start + size;
70                 newblock->size = p->size - size;
71                 newblock->filp = 0;
72                 newblock->next = p->next;
73                 newblock->prev = p;
74                 p->next->prev = newblock;
75                 p->next = newblock;
76                 p->size = size;
77         }
78
79  out:
80         /* Our block is in the middle */
81         p->filp = filp;
82         return p;
83 }
84
85 static struct mem_block *alloc_block( struct mem_block *heap, int size, 
86                                       int align2, DRMFILE filp )
87 {
88         struct mem_block *p;
89         int mask = (1 << align2)-1;
90
91         for (p = heap->next ; p != heap ; p = p->next) {
92                 int start = (p->start + mask) & ~mask;
93                 if (p->filp == 0 && start + size <= p->start + p->size)
94                         return split_block( p, start, size, filp );
95         }
96
97         return NULL;
98 }
99
100 static struct mem_block *find_block( struct mem_block *heap, int start )
101 {
102         struct mem_block *p;
103
104         for (p = heap->next ; p != heap ; p = p->next) 
105                 if (p->start == start)
106                         return p;
107
108         return NULL;
109 }
110
111
112 static void free_block( struct mem_block *p )
113 {
114         p->filp = 0;
115
116         /* Assumes a single contiguous range.  Needs a special filp in
117          * 'heap' to stop it being subsumed.
118          */
119         if (p->next->filp == 0) {
120                 struct mem_block *q = p->next;
121                 p->size += q->size;
122                 p->next = q->next;
123                 p->next->prev = p;
124                 DRM_FREE(q, sizeof(*q));
125         }
126
127         if (p->prev->filp == 0) {
128                 struct mem_block *q = p->prev;
129                 q->size += p->size;
130                 q->next = p->next;
131                 q->next->prev = q;
132                 DRM_FREE(p, sizeof(*q));
133         }
134 }
135
136 /* Initialize.  How to check for an uninitialized heap?
137  */
138 static int init_heap(struct mem_block **heap, int start, int size)
139 {
140         struct mem_block *blocks = DRM_MALLOC(sizeof(*blocks));
141
142         if (!blocks) 
143                 return -ENOMEM;
144         
145         *heap = DRM_MALLOC(sizeof(**heap));
146         if (!*heap) {
147                 DRM_FREE( blocks, sizeof(*blocks) );
148                 return -ENOMEM;
149         }
150
151         blocks->start = start;
152         blocks->size = size;
153         blocks->filp = 0;
154         blocks->next = blocks->prev = *heap;
155
156         memset( *heap, 0, sizeof(**heap) );
157         (*heap)->filp = (DRMFILE) -1;
158         (*heap)->next = (*heap)->prev = blocks;
159         return 0;
160 }
161
162
163 /* Free all blocks associated with the releasing file.
164  */
165 void radeon_mem_release( DRMFILE filp, struct mem_block *heap )
166 {
167         struct mem_block *p;
168
169         if (!heap || !heap->next)
170                 return;
171
172         for (p = heap->next ; p != heap ; p = p->next) {
173                 if (p->filp == filp) 
174                         p->filp = 0;
175         }
176
177         /* Assumes a single contiguous range.  Needs a special filp in
178          * 'heap' to stop it being subsumed.
179          */
180         for (p = heap->next ; p != heap ; p = p->next) {
181                 while (p->filp == 0 && p->next->filp == 0) {
182                         struct mem_block *q = p->next;
183                         p->size += q->size;
184                         p->next = q->next;
185                         p->next->prev = p;
186                         DRM_FREE(q, sizeof(*q));
187                 }
188         }
189 }
190
191 /* Shutdown.
192  */
193 void radeon_mem_takedown( struct mem_block **heap )
194 {
195         struct mem_block *p;
196         
197         if (!*heap)
198                 return;
199
200         for (p = (*heap)->next ; p != *heap ; ) {
201                 struct mem_block *q = p;
202                 p = p->next;
203                 DRM_FREE(q, sizeof(*q));
204         }
205
206         DRM_FREE( *heap, sizeof(**heap) );
207         *heap = 0;
208 }
209
210
211
212 /* IOCTL HANDLERS */
213
214 static struct mem_block **get_heap( drm_radeon_private_t *dev_priv,
215                                    int region )
216 {
217         switch( region ) {
218         case RADEON_MEM_REGION_AGP:
219                 return &dev_priv->agp_heap; 
220         case RADEON_MEM_REGION_FB:
221                 return &dev_priv->fb_heap;
222         default:
223                 return 0;
224         }
225 }
226
227 int radeon_mem_alloc( DRM_IOCTL_ARGS )
228 {
229         DRM_DEVICE;
230         drm_radeon_private_t *dev_priv = dev->dev_private;
231         drm_radeon_mem_alloc_t alloc;
232         struct mem_block *block, **heap;
233
234         if ( !dev_priv ) {
235                 DRM_ERROR( "%s called with no initialization\n", __FUNCTION__ );
236                 return DRM_ERR(EINVAL);
237         }
238
239         DRM_COPY_FROM_USER_IOCTL( alloc, (drm_radeon_mem_alloc_t *)data,
240                                   sizeof(alloc) );
241
242         heap = get_heap( dev_priv, alloc.region );
243         if (!heap || !*heap)
244                 return DRM_ERR(EFAULT);
245         
246         /* Make things easier on ourselves: all allocations at least
247          * 4k aligned.
248          */
249         if (alloc.alignment < 12)
250                 alloc.alignment = 12;
251
252         block = alloc_block( *heap, alloc.size, alloc.alignment,
253                              filp );
254
255         if (!block) 
256                 return DRM_ERR(ENOMEM);
257
258         if ( DRM_COPY_TO_USER( alloc.region_offset, &block->start, 
259                                sizeof(int) ) ) {
260                 DRM_ERROR( "copy_to_user\n" );
261                 return DRM_ERR(EFAULT);
262         }
263         
264         return 0;
265 }
266
267
268
269 int radeon_mem_free( DRM_IOCTL_ARGS )
270 {
271         DRM_DEVICE;
272         drm_radeon_private_t *dev_priv = dev->dev_private;
273         drm_radeon_mem_free_t memfree;
274         struct mem_block *block, **heap;
275
276         if ( !dev_priv ) {
277                 DRM_ERROR( "%s called with no initialization\n", __FUNCTION__ );
278                 return DRM_ERR(EINVAL);
279         }
280
281         DRM_COPY_FROM_USER_IOCTL( memfree, (drm_radeon_mem_free_t *)data,
282                                   sizeof(memfree) );
283
284         heap = get_heap( dev_priv, memfree.region );
285         if (!heap || !*heap)
286                 return DRM_ERR(EFAULT);
287         
288         block = find_block( *heap, memfree.region_offset );
289         if (!block)
290                 return DRM_ERR(EFAULT);
291
292         if (block->filp != filp)
293                 return DRM_ERR(EPERM);
294
295         free_block( block );    
296         return 0;
297 }
298
299 int radeon_mem_init_heap( DRM_IOCTL_ARGS )
300 {
301         DRM_DEVICE;
302         drm_radeon_private_t *dev_priv = dev->dev_private;
303         drm_radeon_mem_init_heap_t initheap;
304         struct mem_block **heap;
305
306         if ( !dev_priv ) {
307                 DRM_ERROR( "%s called with no initialization\n", __FUNCTION__ );
308                 return DRM_ERR(EINVAL);
309         }
310
311         DRM_COPY_FROM_USER_IOCTL( initheap, (drm_radeon_mem_init_heap_t *)data,
312                                   sizeof(initheap) );
313
314         heap = get_heap( dev_priv, initheap.region );
315         if (!heap) 
316                 return DRM_ERR(EFAULT);
317         
318         if (*heap) {
319                 DRM_ERROR("heap already initialized?");
320                 return DRM_ERR(EFAULT);
321         }
322                 
323         return init_heap( heap, initheap.start, initheap.size );
324 }
325
326