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>
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
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"
40 /* Very simple allocator for GART memory, working on a static range
41 * already mapped into each client's address space.
44 static struct mem_block *split_block(struct mem_block *p, int start, int size,
47 /* Maybe cut off the start of an existing block */
48 if (start > p->start) {
49 struct mem_block *newblock =
50 drm_alloc(sizeof(*newblock), DRM_MEM_BUFS);
53 newblock->start = start;
54 newblock->size = p->size - (start - p->start);
55 newblock->filp = NULL;
56 newblock->next = p->next;
58 p->next->prev = newblock;
60 p->size -= newblock->size;
64 /* Maybe cut off the end of an existing block */
66 struct mem_block *newblock =
67 drm_alloc(sizeof(*newblock), DRM_MEM_BUFS);
70 newblock->start = start + size;
71 newblock->size = p->size - size;
72 newblock->filp = NULL;
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 list_for_each(p, heap) {
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 list_for_each(p, heap)
106 if (p->start == start)
112 static void free_block(struct mem_block *p)
116 /* Assumes a single contiguous range. Needs a special filp in
117 * 'heap' to stop it being subsumed.
119 if (p->next->filp == 0) {
120 struct mem_block *q = p->next;
124 drm_free(q, sizeof(*q), DRM_MEM_BUFS);
127 if (p->prev->filp == 0) {
128 struct mem_block *q = p->prev;
132 drm_free(p, sizeof(*q), DRM_MEM_BUFS);
136 /* Initialize. How to check for an uninitialized heap?
138 static int init_heap(struct mem_block **heap, int start, int size)
140 struct mem_block *blocks = drm_alloc(sizeof(*blocks), DRM_MEM_BUFS);
143 return DRM_ERR(ENOMEM);
145 *heap = drm_alloc(sizeof(**heap), DRM_MEM_BUFS);
147 drm_free(blocks, sizeof(*blocks), DRM_MEM_BUFS);
148 return DRM_ERR(ENOMEM);
151 blocks->start = start;
154 blocks->next = blocks->prev = *heap;
156 memset(*heap, 0, sizeof(**heap));
157 (*heap)->filp = (DRMFILE) - 1;
158 (*heap)->next = (*heap)->prev = blocks;
162 /* Free all blocks associated with the releasing file.
164 void radeon_mem_release(DRMFILE filp, struct mem_block *heap)
168 if (!heap || !heap->next)
171 list_for_each(p, heap) {
176 /* Assumes a single contiguous range. Needs a special filp in
177 * 'heap' to stop it being subsumed.
179 list_for_each(p, heap) {
180 while (p->filp == 0 && p->next->filp == 0) {
181 struct mem_block *q = p->next;
185 drm_free(q, sizeof(*q), DRM_MEM_DRIVER);
192 void radeon_mem_takedown(struct mem_block **heap)
199 for (p = (*heap)->next; p != *heap;) {
200 struct mem_block *q = p;
202 drm_free(q, sizeof(*q), DRM_MEM_DRIVER);
205 drm_free(*heap, sizeof(**heap), DRM_MEM_DRIVER);
211 static struct mem_block **get_heap(drm_radeon_private_t * dev_priv, int region)
214 case RADEON_MEM_REGION_GART:
215 return &dev_priv->gart_heap;
216 case RADEON_MEM_REGION_FB:
217 return &dev_priv->fb_heap;
223 int radeon_mem_alloc(DRM_IOCTL_ARGS)
226 drm_radeon_private_t *dev_priv = dev->dev_private;
227 drm_radeon_mem_alloc_t alloc;
228 struct mem_block *block, **heap;
231 DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
232 return DRM_ERR(EINVAL);
235 DRM_COPY_FROM_USER_IOCTL(alloc, (drm_radeon_mem_alloc_t __user *) data,
238 heap = get_heap(dev_priv, alloc.region);
240 return DRM_ERR(EFAULT);
242 /* Make things easier on ourselves: all allocations at least
245 if (alloc.alignment < 12)
246 alloc.alignment = 12;
248 block = alloc_block(*heap, alloc.size, alloc.alignment, filp);
251 return DRM_ERR(ENOMEM);
253 if (DRM_COPY_TO_USER(alloc.region_offset, &block->start, sizeof(int))) {
254 DRM_ERROR("copy_to_user\n");
255 return DRM_ERR(EFAULT);
261 int radeon_mem_free(DRM_IOCTL_ARGS)
264 drm_radeon_private_t *dev_priv = dev->dev_private;
265 drm_radeon_mem_free_t memfree;
266 struct mem_block *block, **heap;
269 DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
270 return DRM_ERR(EINVAL);
273 DRM_COPY_FROM_USER_IOCTL(memfree, (drm_radeon_mem_free_t __user *) data,
276 heap = get_heap(dev_priv, memfree.region);
278 return DRM_ERR(EFAULT);
280 block = find_block(*heap, memfree.region_offset);
282 return DRM_ERR(EFAULT);
284 if (block->filp != filp)
285 return DRM_ERR(EPERM);
291 int radeon_mem_init_heap(DRM_IOCTL_ARGS)
294 drm_radeon_private_t *dev_priv = dev->dev_private;
295 drm_radeon_mem_init_heap_t initheap;
296 struct mem_block **heap;
299 DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
300 return DRM_ERR(EINVAL);
303 DRM_COPY_FROM_USER_IOCTL(initheap,
304 (drm_radeon_mem_init_heap_t __user *) data,
307 heap = get_heap(dev_priv, initheap.region);
309 return DRM_ERR(EFAULT);
312 DRM_ERROR("heap already initialized?");
313 return DRM_ERR(EFAULT);
316 return init_heap(heap, initheap.start, initheap.size);