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