1 /* radeon_mem.c -- Simple agp/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>
35 #include "radeon_drm.h"
36 #include "radeon_drv.h"
38 /* Very simple allocator for agp memory, working on a static range
39 * already mapped into each client's address space.
42 static struct mem_block *split_block(struct mem_block *p, int start, int size,
45 /* Maybe cut off the start of an existing block */
46 if (start > p->start) {
47 struct mem_block *newblock = DRM_MALLOC(sizeof(*newblock));
50 newblock->start = start;
51 newblock->size = p->size - (start - p->start);
53 newblock->next = p->next;
55 p->next->prev = newblock;
57 p->size -= newblock->size;
61 /* Maybe cut off the end of an existing block */
63 struct mem_block *newblock = DRM_MALLOC(sizeof(*newblock));
66 newblock->start = start + size;
67 newblock->size = p->size - size;
69 newblock->next = p->next;
71 p->next->prev = newblock;
77 /* Our block is in the middle */
82 static struct mem_block *alloc_block( struct mem_block *heap, int size,
86 int mask = (1 << align2)-1;
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 );
97 static struct mem_block *find_block( struct mem_block *heap, int start )
101 for (p = heap->next ; p != heap ; p = p->next)
102 if (p->start == start)
109 static void free_block( struct mem_block *p )
113 /* Assumes a single contiguous range. Needs a special pid in
114 * 'heap' to stop it being subsumed.
116 if (p->next->pid == 0) {
117 struct mem_block *q = p->next;
124 if (p->prev->pid == 0) {
125 struct mem_block *q = p->prev;
133 static void print_heap( struct mem_block *heap )
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,
143 /* Initialize. How to check for an uninitialized heap?
145 static int init_heap(struct mem_block **heap, int start, int size)
147 struct mem_block *blocks = DRM_MALLOC(sizeof(*blocks));
152 *heap = DRM_MALLOC(sizeof(**heap));
158 blocks->start = start;
161 blocks->next = blocks->prev = *heap;
163 memset( *heap, 0, sizeof(**heap) );
165 (*heap)->next = (*heap)->prev = blocks;
170 /* Free all blocks associated with the releasing pid.
172 void radeon_mem_release( struct mem_block *heap )
174 int pid = DRM_CURRENTPID;
177 if (!heap || !heap->next)
180 for (p = heap->next ; p != heap ; p = p->next) {
185 /* Assumes a single contiguous range. Needs a special pid in
186 * 'heap' to stop it being subsumed.
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;
201 void radeon_mem_takedown( struct mem_block **heap )
208 for (p = (*heap)->next ; p != *heap ; ) {
209 struct mem_block *q = p;
222 static struct mem_block **get_heap( drm_radeon_private_t *dev_priv,
226 case RADEON_MEM_REGION_AGP:
227 return &dev_priv->agp_heap;
228 case RADEON_MEM_REGION_FB:
229 return &dev_priv->fb_heap;
235 int radeon_mem_alloc( DRM_IOCTL_ARGS )
238 drm_radeon_private_t *dev_priv = dev->dev_private;
239 drm_radeon_mem_alloc_t alloc;
240 struct mem_block *block, **heap;
243 DRM_ERROR( "%s called with no initialization\n", __FUNCTION__ );
244 return DRM_ERR(EINVAL);
247 DRM_COPY_FROM_USER_IOCTL( alloc, (drm_radeon_mem_alloc_t *)data,
250 heap = get_heap( dev_priv, alloc.region );
252 return DRM_ERR(EFAULT);
254 /* Make things easier on ourselves: all allocations at least
257 if (alloc.alignment < 12)
258 alloc.alignment = 12;
260 block = alloc_block( *heap, alloc.size, alloc.alignment,
264 return DRM_ERR(ENOMEM);
266 if ( DRM_COPY_TO_USER( alloc.region_offset, &block->start,
268 DRM_ERROR( "copy_to_user\n" );
269 return DRM_ERR(EFAULT);
277 int radeon_mem_free( DRM_IOCTL_ARGS )
280 drm_radeon_private_t *dev_priv = dev->dev_private;
281 drm_radeon_mem_free_t memfree;
282 struct mem_block *block, **heap;
285 DRM_ERROR( "%s called with no initialization\n", __FUNCTION__ );
286 return DRM_ERR(EINVAL);
289 DRM_COPY_FROM_USER_IOCTL( memfree, (drm_radeon_mem_free_t *)data,
292 heap = get_heap( dev_priv, memfree.region );
294 return DRM_ERR(EFAULT);
296 block = find_block( *heap, memfree.region_offset );
298 return DRM_ERR(EFAULT);
300 if (block->pid != DRM_CURRENTPID)
301 return DRM_ERR(EPERM);
307 int radeon_mem_init_heap( DRM_IOCTL_ARGS )
310 drm_radeon_private_t *dev_priv = dev->dev_private;
311 drm_radeon_mem_init_heap_t initheap;
312 struct mem_block **heap;
315 DRM_ERROR( "%s called with no initialization\n", __FUNCTION__ );
316 return DRM_ERR(EINVAL);
319 DRM_COPY_FROM_USER_IOCTL( initheap, (drm_radeon_mem_init_heap_t *)data,
322 heap = get_heap( dev_priv, initheap.region );
324 return DRM_ERR(EFAULT);
327 DRM_ERROR("heap already initialized?");
328 return DRM_ERR(EFAULT);
331 return init_heap( heap, initheap.start, initheap.size );