1 /* radeon_mem.c -- Simple GART/fb memory manager for radeon -*- linux-c -*-
3 * Copyright (C) The Weather Channel, Inc. 2002. All Rights Reserved.
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.
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:
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
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.
29 * Keith Whitwell <keith@tungstengraphics.com>
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include "dev/drm/radeon.h"
37 #include "dev/drm/drmP.h"
38 #include "dev/drm/drm.h"
39 #include "dev/drm/radeon_drm.h"
40 #include "dev/drm/radeon_drv.h"
42 /* Very simple allocator for GART memory, working on a static range
43 * already mapped into each client's address space.
46 static struct mem_block *split_block(struct mem_block *p, int start, int size,
49 /* Maybe cut off the start of an existing block */
50 if (start > p->start) {
51 struct mem_block *newblock = DRM_MALLOC(sizeof(*newblock));
54 newblock->start = start;
55 newblock->size = p->size - (start - p->start);
57 newblock->next = p->next;
59 p->next->prev = newblock;
61 p->size -= newblock->size;
65 /* Maybe cut off the end of an existing block */
67 struct mem_block *newblock = DRM_MALLOC(sizeof(*newblock));
70 newblock->start = start + size;
71 newblock->size = p->size - size;
73 newblock->next = p->next;
75 p->next->prev = newblock;
81 /* Our block is in the middle */
86 static struct mem_block *alloc_block( struct mem_block *heap, int size,
87 int align2, DRMFILE filp )
90 int mask = (1 << align2)-1;
92 for (p = heap->next ; p != heap ; p = p->next) {
93 int start = (p->start + mask) & ~mask;
94 if (p->filp == 0 && start + size <= p->start + p->size)
95 return split_block( p, start, size, filp );
101 static struct mem_block *find_block( struct mem_block *heap, int start )
105 for (p = heap->next ; p != heap ; p = p->next)
106 if (p->start == start)
113 static void free_block( struct mem_block *p )
117 /* Assumes a single contiguous range. Needs a special filp in
118 * 'heap' to stop it being subsumed.
120 if (p->next->filp == 0) {
121 struct mem_block *q = p->next;
125 DRM_FREE(q, sizeof(*q));
128 if (p->prev->filp == 0) {
129 struct mem_block *q = p->prev;
133 DRM_FREE(p, sizeof(*q));
137 /* Initialize. How to check for an uninitialized heap?
139 static int init_heap(struct mem_block **heap, int start, int size)
141 struct mem_block *blocks = DRM_MALLOC(sizeof(*blocks));
144 return DRM_ERR(ENOMEM);
146 *heap = DRM_MALLOC(sizeof(**heap));
148 DRM_FREE( blocks, sizeof(*blocks) );
149 return DRM_ERR(ENOMEM);
152 blocks->start = start;
155 blocks->next = blocks->prev = *heap;
157 memset( *heap, 0, sizeof(**heap) );
158 (*heap)->filp = (DRMFILE) -1;
159 (*heap)->next = (*heap)->prev = blocks;
164 /* Free all blocks associated with the releasing file.
166 void radeon_mem_release( DRMFILE filp, struct mem_block *heap )
170 if (!heap || !heap->next)
173 for (p = heap->next ; p != heap ; p = p->next) {
178 /* Assumes a single contiguous range. Needs a special filp in
179 * 'heap' to stop it being subsumed.
181 for (p = heap->next ; p != heap ; p = p->next) {
182 while (p->filp == 0 && p->next->filp == 0) {
183 struct mem_block *q = p->next;
187 DRM_FREE(q, sizeof(*q));
194 void radeon_mem_takedown( struct mem_block **heap )
201 for (p = (*heap)->next ; p != *heap ; ) {
202 struct mem_block *q = p;
204 DRM_FREE(q, sizeof(*q));
207 DRM_FREE( *heap, sizeof(**heap) );
215 static struct mem_block **get_heap( drm_radeon_private_t *dev_priv,
219 case RADEON_MEM_REGION_GART:
220 return &dev_priv->gart_heap;
221 case RADEON_MEM_REGION_FB:
222 return &dev_priv->fb_heap;
228 int radeon_mem_alloc( DRM_IOCTL_ARGS )
231 drm_radeon_private_t *dev_priv = dev->dev_private;
232 drm_radeon_mem_alloc_t alloc;
233 struct mem_block *block, **heap;
236 DRM_ERROR( "%s called with no initialization\n", __FUNCTION__ );
237 return DRM_ERR(EINVAL);
240 DRM_COPY_FROM_USER_IOCTL( alloc, (drm_radeon_mem_alloc_t *)data,
243 heap = get_heap( dev_priv, alloc.region );
245 return DRM_ERR(EFAULT);
247 /* Make things easier on ourselves: all allocations at least
250 if (alloc.alignment < 12)
251 alloc.alignment = 12;
253 block = alloc_block( *heap, alloc.size, alloc.alignment,
257 return DRM_ERR(ENOMEM);
259 if ( DRM_COPY_TO_USER( alloc.region_offset, &block->start,
261 DRM_ERROR( "copy_to_user\n" );
262 return DRM_ERR(EFAULT);
270 int radeon_mem_free( DRM_IOCTL_ARGS )
273 drm_radeon_private_t *dev_priv = dev->dev_private;
274 drm_radeon_mem_free_t memfree;
275 struct mem_block *block, **heap;
278 DRM_ERROR( "%s called with no initialization\n", __FUNCTION__ );
279 return DRM_ERR(EINVAL);
282 DRM_COPY_FROM_USER_IOCTL( memfree, (drm_radeon_mem_free_t *)data,
285 heap = get_heap( dev_priv, memfree.region );
287 return DRM_ERR(EFAULT);
289 block = find_block( *heap, memfree.region_offset );
291 return DRM_ERR(EFAULT);
293 if (block->filp != filp)
294 return DRM_ERR(EPERM);
300 int radeon_mem_init_heap( DRM_IOCTL_ARGS )
303 drm_radeon_private_t *dev_priv = dev->dev_private;
304 drm_radeon_mem_init_heap_t initheap;
305 struct mem_block **heap;
308 DRM_ERROR( "%s called with no initialization\n", __FUNCTION__ );
309 return DRM_ERR(EINVAL);
312 DRM_COPY_FROM_USER_IOCTL( initheap, (drm_radeon_mem_init_heap_t *)data,
315 heap = get_heap( dev_priv, initheap.region );
317 return DRM_ERR(EFAULT);
320 DRM_ERROR("heap already initialized?");
321 return DRM_ERR(EFAULT);
324 return init_heap( heap, initheap.start, initheap.size );