merged r200-0-2-branch to trunk
[profile/ivi/libdrm.git] / shared-core / 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
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 agp 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                                      int pid )
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->pid = 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->pid = 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->pid = pid;
79         return p;
80 }
81
82 static struct mem_block *alloc_block( struct mem_block *heap, int size, 
83                                       int align2, int pid )
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->pid == 0 && start + size <= p->start + p->size)
91                         return split_block( p, start, size, pid );
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->pid = 0;
112
113         /* Assumes a single contiguous range.  Needs a special pid in
114          * 'heap' to stop it being subsumed.
115          */
116         if (p->next->pid == 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(p);
122         }
123
124         if (p->prev->pid == 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);
130         }
131 }
132
133 static void print_heap( struct mem_block *heap )
134 {
135         struct mem_block *p;
136
137         for (p = heap->next ; p != heap ; p = p->next) 
138                 DRM_DEBUG("0x%x..0x%x (0x%x) -- owner %d\n",
139                           p->start, p->start + p->size,
140                           p->size, p->pid);
141 }
142
143 /* Initialize.  How to check for an uninitialized heap?
144  */
145 static int init_heap(struct mem_block **heap, int start, int size)
146 {
147         struct mem_block *blocks = DRM_MALLOC(sizeof(*blocks));
148
149         if (!blocks) 
150                 return -ENOMEM;
151         
152         *heap = DRM_MALLOC(sizeof(**heap));
153         if (!*heap) {
154                 DRM_FREE( blocks );
155                 return -ENOMEM;
156         }
157
158         blocks->start = start;
159         blocks->size = size;
160         blocks->pid = 0;
161         blocks->next = blocks->prev = *heap;
162
163         memset( *heap, 0, sizeof(**heap) );
164         (*heap)->pid = -1;
165         (*heap)->next = (*heap)->prev = blocks;
166         return 0;
167 }
168
169
170 /* Free all blocks associated with the releasing pid.
171  */
172 void radeon_mem_release( struct mem_block *heap )
173 {
174         int pid = DRM_CURRENTPID;
175         struct mem_block *p;
176
177         if (!heap || !heap->next)
178                 return;
179
180         for (p = heap->next ; p != heap ; p = p->next) {
181                 if (p->pid == pid) 
182                         p->pid = 0;
183         }
184
185         /* Assumes a single contiguous range.  Needs a special pid in
186          * 'heap' to stop it being subsumed.
187          */
188         for (p = heap->next ; p != heap ; p = p->next) {
189                 while (p->pid == 0 && p->next->pid == 0) {
190                         struct mem_block *q = p->next;
191                         p->size += q->size;
192                         p->next = q->next;
193                         p->next->prev = p;
194                         DRM_FREE(q);
195                 }
196         }
197 }
198
199 /* Shutdown.
200  */
201 void radeon_mem_takedown( struct mem_block **heap )
202 {
203         struct mem_block *p;
204         
205         if (!*heap)
206                 return;
207
208         for (p = (*heap)->next ; p != *heap ; ) {
209                 struct mem_block *q = p;
210                 p = p->next;
211                 DRM_FREE(q);
212         }
213
214         DRM_FREE( *heap );
215         *heap = 0;
216 }
217
218
219
220 /* IOCTL HANDLERS */
221
222 static struct mem_block **get_heap( drm_radeon_private_t *dev_priv,
223                                    int region )
224 {
225         switch( region ) {
226         case RADEON_MEM_REGION_AGP:
227                 return &dev_priv->agp_heap; 
228         case RADEON_MEM_REGION_FB:
229                 return &dev_priv->fb_heap;
230         default:
231                 return 0;
232         }
233 }
234
235 int radeon_mem_alloc( DRM_IOCTL_ARGS )
236 {
237         DRM_DEVICE;
238         drm_radeon_private_t *dev_priv = dev->dev_private;
239         drm_radeon_mem_alloc_t alloc;
240         struct mem_block *block, **heap;
241
242         if ( !dev_priv ) {
243                 DRM_ERROR( "%s called with no initialization\n", __FUNCTION__ );
244                 return DRM_ERR(EINVAL);
245         }
246
247         DRM_COPY_FROM_USER_IOCTL( alloc, (drm_radeon_mem_alloc_t *)data,
248                                   sizeof(alloc) );
249
250         heap = get_heap( dev_priv, alloc.region );
251         if (!heap || !*heap)
252                 return DRM_ERR(EFAULT);
253         
254         /* Make things easier on ourselves: all allocations at least
255          * 4k aligned.
256          */
257         if (alloc.alignment < 12)
258                 alloc.alignment = 12;
259
260         block = alloc_block( *heap, alloc.size, alloc.alignment,
261                              DRM_CURRENTPID );
262
263         if (!block) 
264                 return DRM_ERR(ENOMEM);
265
266         if ( DRM_COPY_TO_USER( alloc.region_offset, &block->start, 
267                                sizeof(int) ) ) {
268                 DRM_ERROR( "copy_to_user\n" );
269                 return DRM_ERR(EFAULT);
270         }
271         
272         return 0;
273 }
274
275
276
277 int radeon_mem_free( DRM_IOCTL_ARGS )
278 {
279         DRM_DEVICE;
280         drm_radeon_private_t *dev_priv = dev->dev_private;
281         drm_radeon_mem_free_t memfree;
282         struct mem_block *block, **heap;
283
284         if ( !dev_priv ) {
285                 DRM_ERROR( "%s called with no initialization\n", __FUNCTION__ );
286                 return DRM_ERR(EINVAL);
287         }
288
289         DRM_COPY_FROM_USER_IOCTL( memfree, (drm_radeon_mem_free_t *)data,
290                                   sizeof(memfree) );
291
292         heap = get_heap( dev_priv, memfree.region );
293         if (!heap || !*heap)
294                 return DRM_ERR(EFAULT);
295         
296         block = find_block( *heap, memfree.region_offset );
297         if (!block)
298                 return DRM_ERR(EFAULT);
299
300         if (block->pid != DRM_CURRENTPID)
301                 return DRM_ERR(EPERM);
302
303         free_block( block );    
304         return 0;
305 }
306
307 int radeon_mem_init_heap( DRM_IOCTL_ARGS )
308 {
309         DRM_DEVICE;
310         drm_radeon_private_t *dev_priv = dev->dev_private;
311         drm_radeon_mem_init_heap_t initheap;
312         struct mem_block **heap;
313
314         if ( !dev_priv ) {
315                 DRM_ERROR( "%s called with no initialization\n", __FUNCTION__ );
316                 return DRM_ERR(EINVAL);
317         }
318
319         DRM_COPY_FROM_USER_IOCTL( initheap, (drm_radeon_mem_init_heap_t *)data,
320                                   sizeof(initheap) );
321
322         heap = get_heap( dev_priv, initheap.region );
323         if (!heap) 
324                 return DRM_ERR(EFAULT);
325         
326         if (*heap) {
327                 DRM_ERROR("heap already initialized?");
328                 return DRM_ERR(EFAULT);
329         }
330                 
331         return init_heap( heap, initheap.start, initheap.size );
332 }
333
334