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