patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / char / drm / radeon_mem.c
1 /* radeon_mem.c -- Simple GART/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
32 #include "radeon.h"
33 #include "drmP.h"
34 #include "drm.h"
35 #include "radeon_drm.h"
36 #include "radeon_drv.h"
37
38 /* Very simple allocator for GART memory, working on a static range
39  * already mapped into each client's address space.  
40  */
41
42 static struct mem_block *split_block(struct mem_block *p, int start, int size,
43                                      DRMFILE filp )
44 {
45         /* Maybe cut off the start of an existing block */
46         if (start > p->start) {
47                 struct mem_block *newblock = DRM_MALLOC(sizeof(*newblock));
48                 if (!newblock) 
49                         goto out;
50                 newblock->start = start;
51                 newblock->size = p->size - (start - p->start);
52                 newblock->filp = 0;
53                 newblock->next = p->next;
54                 newblock->prev = p;
55                 p->next->prev = newblock;
56                 p->next = newblock;
57                 p->size -= newblock->size;
58                 p = newblock;
59         }
60    
61         /* Maybe cut off the end of an existing block */
62         if (size < p->size) {
63                 struct mem_block *newblock = DRM_MALLOC(sizeof(*newblock));
64                 if (!newblock)
65                         goto out;
66                 newblock->start = start + size;
67                 newblock->size = p->size - size;
68                 newblock->filp = 0;
69                 newblock->next = p->next;
70                 newblock->prev = p;
71                 p->next->prev = newblock;
72                 p->next = newblock;
73                 p->size = size;
74         }
75
76  out:
77         /* Our block is in the middle */
78         p->filp = filp;
79         return p;
80 }
81
82 static struct mem_block *alloc_block( struct mem_block *heap, int size, 
83                                       int align2, DRMFILE filp )
84 {
85         struct mem_block *p;
86         int mask = (1 << align2)-1;
87
88         for (p = heap->next ; p != heap ; p = p->next) {
89                 int start = (p->start + mask) & ~mask;
90                 if (p->filp == 0 && start + size <= p->start + p->size)
91                         return split_block( p, start, size, filp );
92         }
93
94         return NULL;
95 }
96
97 static struct mem_block *find_block( struct mem_block *heap, int start )
98 {
99         struct mem_block *p;
100
101         for (p = heap->next ; p != heap ; p = p->next) 
102                 if (p->start == start)
103                         return p;
104
105         return NULL;
106 }
107
108
109 static void free_block( struct mem_block *p )
110 {
111         p->filp = 0;
112
113         /* Assumes a single contiguous range.  Needs a special filp in
114          * 'heap' to stop it being subsumed.
115          */
116         if (p->next->filp == 0) {
117                 struct mem_block *q = p->next;
118                 p->size += q->size;
119                 p->next = q->next;
120                 p->next->prev = p;
121                 DRM_FREE(q, sizeof(*q));
122         }
123
124         if (p->prev->filp == 0) {
125                 struct mem_block *q = p->prev;
126                 q->size += p->size;
127                 q->next = p->next;
128                 q->next->prev = q;
129                 DRM_FREE(p, sizeof(*q));
130         }
131 }
132
133 /* Initialize.  How to check for an uninitialized heap?
134  */
135 static int init_heap(struct mem_block **heap, int start, int size)
136 {
137         struct mem_block *blocks = DRM_MALLOC(sizeof(*blocks));
138
139         if (!blocks) 
140                 return DRM_ERR(ENOMEM);
141         
142         *heap = DRM_MALLOC(sizeof(**heap));
143         if (!*heap) {
144                 DRM_FREE( blocks, sizeof(*blocks) );
145                 return DRM_ERR(ENOMEM);
146         }
147
148         blocks->start = start;
149         blocks->size = size;
150         blocks->filp = 0;
151         blocks->next = blocks->prev = *heap;
152
153         memset( *heap, 0, sizeof(**heap) );
154         (*heap)->filp = (DRMFILE) -1;
155         (*heap)->next = (*heap)->prev = blocks;
156         return 0;
157 }
158
159
160 /* Free all blocks associated with the releasing file.
161  */
162 void radeon_mem_release( DRMFILE filp, struct mem_block *heap )
163 {
164         struct mem_block *p;
165
166         if (!heap || !heap->next)
167                 return;
168
169         for (p = heap->next ; p != heap ; p = p->next) {
170                 if (p->filp == filp) 
171                         p->filp = 0;
172         }
173
174         /* Assumes a single contiguous range.  Needs a special filp in
175          * 'heap' to stop it being subsumed.
176          */
177         for (p = heap->next ; p != heap ; p = p->next) {
178                 while (p->filp == 0 && p->next->filp == 0) {
179                         struct mem_block *q = p->next;
180                         p->size += q->size;
181                         p->next = q->next;
182                         p->next->prev = p;
183                         DRM_FREE(q, sizeof(*q));
184                 }
185         }
186 }
187
188 /* Shutdown.
189  */
190 void radeon_mem_takedown( struct mem_block **heap )
191 {
192         struct mem_block *p;
193         
194         if (!*heap)
195                 return;
196
197         for (p = (*heap)->next ; p != *heap ; ) {
198                 struct mem_block *q = p;
199                 p = p->next;
200                 DRM_FREE(q, sizeof(*q));
201         }
202
203         DRM_FREE( *heap, sizeof(**heap) );
204         *heap = 0;
205 }
206
207
208
209 /* IOCTL HANDLERS */
210
211 static struct mem_block **get_heap( drm_radeon_private_t *dev_priv,
212                                    int region )
213 {
214         switch( region ) {
215         case RADEON_MEM_REGION_GART:
216                 return &dev_priv->gart_heap; 
217         case RADEON_MEM_REGION_FB:
218                 return &dev_priv->fb_heap;
219         default:
220                 return 0;
221         }
222 }
223
224 int radeon_mem_alloc( DRM_IOCTL_ARGS )
225 {
226         DRM_DEVICE;
227         drm_radeon_private_t *dev_priv = dev->dev_private;
228         drm_radeon_mem_alloc_t alloc;
229         struct mem_block *block, **heap;
230
231         if ( !dev_priv ) {
232                 DRM_ERROR( "%s called with no initialization\n", __FUNCTION__ );
233                 return DRM_ERR(EINVAL);
234         }
235
236         DRM_COPY_FROM_USER_IOCTL( alloc, (drm_radeon_mem_alloc_t *)data,
237                                   sizeof(alloc) );
238
239         heap = get_heap( dev_priv, alloc.region );
240         if (!heap || !*heap)
241                 return DRM_ERR(EFAULT);
242         
243         /* Make things easier on ourselves: all allocations at least
244          * 4k aligned.
245          */
246         if (alloc.alignment < 12)
247                 alloc.alignment = 12;
248
249         block = alloc_block( *heap, alloc.size, alloc.alignment,
250                              filp );
251
252         if (!block) 
253                 return DRM_ERR(ENOMEM);
254
255         if ( DRM_COPY_TO_USER( alloc.region_offset, &block->start, 
256                                sizeof(int) ) ) {
257                 DRM_ERROR( "copy_to_user\n" );
258                 return DRM_ERR(EFAULT);
259         }
260         
261         return 0;
262 }
263
264
265
266 int radeon_mem_free( DRM_IOCTL_ARGS )
267 {
268         DRM_DEVICE;
269         drm_radeon_private_t *dev_priv = dev->dev_private;
270         drm_radeon_mem_free_t memfree;
271         struct mem_block *block, **heap;
272
273         if ( !dev_priv ) {
274                 DRM_ERROR( "%s called with no initialization\n", __FUNCTION__ );
275                 return DRM_ERR(EINVAL);
276         }
277
278         DRM_COPY_FROM_USER_IOCTL( memfree, (drm_radeon_mem_free_t *)data,
279                                   sizeof(memfree) );
280
281         heap = get_heap( dev_priv, memfree.region );
282         if (!heap || !*heap)
283                 return DRM_ERR(EFAULT);
284         
285         block = find_block( *heap, memfree.region_offset );
286         if (!block)
287                 return DRM_ERR(EFAULT);
288
289         if (block->filp != filp)
290                 return DRM_ERR(EPERM);
291
292         free_block( block );    
293         return 0;
294 }
295
296 int radeon_mem_init_heap( DRM_IOCTL_ARGS )
297 {
298         DRM_DEVICE;
299         drm_radeon_private_t *dev_priv = dev->dev_private;
300         drm_radeon_mem_init_heap_t initheap;
301         struct mem_block **heap;
302
303         if ( !dev_priv ) {
304                 DRM_ERROR( "%s called with no initialization\n", __FUNCTION__ );
305                 return DRM_ERR(EINVAL);
306         }
307
308         DRM_COPY_FROM_USER_IOCTL( initheap, (drm_radeon_mem_init_heap_t *)data,
309                                   sizeof(initheap) );
310
311         heap = get_heap( dev_priv, initheap.region );
312         if (!heap) 
313                 return DRM_ERR(EFAULT);
314         
315         if (*heap) {
316                 DRM_ERROR("heap already initialized?");
317                 return DRM_ERR(EFAULT);
318         }
319                 
320         return init_heap( heap, initheap.start, initheap.size );
321 }
322
323