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>
31 * $DragonFly: src/sys/dev/drm/radeon_mem.c,v 1.1 2008/04/05 18:12:29 hasso Exp $
36 #include "radeon_drm.h"
37 #include "radeon_drv.h"
39 /* Very simple allocator for GART memory, working on a static range
40 * already mapped into each client's address space.
43 static struct mem_block
*split_block(struct mem_block
*p
, int start
, int size
,
44 struct drm_file
*file_priv
)
46 /* Maybe cut off the start of an existing block */
47 if (start
> p
->start
) {
48 struct mem_block
*newblock
=
49 drm_alloc(sizeof(*newblock
), DRM_MEM_BUFS
);
52 newblock
->start
= start
;
53 newblock
->size
= p
->size
- (start
- p
->start
);
54 newblock
->file_priv
= NULL
;
55 newblock
->next
= p
->next
;
57 p
->next
->prev
= newblock
;
59 p
->size
-= newblock
->size
;
63 /* Maybe cut off the end of an existing block */
65 struct mem_block
*newblock
=
66 drm_alloc(sizeof(*newblock
), DRM_MEM_BUFS
);
69 newblock
->start
= start
+ size
;
70 newblock
->size
= p
->size
- size
;
71 newblock
->file_priv
= NULL
;
72 newblock
->next
= p
->next
;
74 p
->next
->prev
= newblock
;
80 /* Our block is in the middle */
81 p
->file_priv
= file_priv
;
85 static struct mem_block
*alloc_block(struct mem_block
*heap
, int size
,
86 int align2
, struct drm_file
*file_priv
)
89 int mask
= (1 << align2
) - 1;
91 list_for_each(p
, heap
) {
92 int start
= (p
->start
+ mask
) & ~mask
;
93 if (p
->file_priv
== 0 && start
+ size
<= p
->start
+ p
->size
)
94 return split_block(p
, start
, size
, file_priv
);
100 static struct mem_block
*find_block(struct mem_block
*heap
, int start
)
104 list_for_each(p
, heap
)
105 if (p
->start
== start
)
111 static void free_block(struct mem_block
*p
)
115 /* Assumes a single contiguous range. Needs a special file_priv in
116 * 'heap' to stop it being subsumed.
118 if (p
->next
->file_priv
== 0) {
119 struct mem_block
*q
= p
->next
;
123 drm_free(q
, sizeof(*q
), DRM_MEM_BUFS
);
126 if (p
->prev
->file_priv
== 0) {
127 struct mem_block
*q
= p
->prev
;
131 drm_free(p
, sizeof(*q
), DRM_MEM_BUFS
);
135 /* Initialize. How to check for an uninitialized heap?
137 static int init_heap(struct mem_block
**heap
, int start
, int size
)
139 struct mem_block
*blocks
= drm_alloc(sizeof(*blocks
), DRM_MEM_BUFS
);
144 *heap
= drm_alloc(sizeof(**heap
), DRM_MEM_BUFS
);
146 drm_free(blocks
, sizeof(*blocks
), DRM_MEM_BUFS
);
150 blocks
->start
= start
;
152 blocks
->file_priv
= NULL
;
153 blocks
->next
= blocks
->prev
= *heap
;
155 memset(*heap
, 0, sizeof(**heap
));
156 (*heap
)->file_priv
= (struct drm_file
*) - 1;
157 (*heap
)->next
= (*heap
)->prev
= blocks
;
161 /* Free all blocks associated with the releasing file.
163 void radeon_mem_release(struct drm_file
*file_priv
, struct mem_block
*heap
)
167 if (!heap
|| !heap
->next
)
170 list_for_each(p
, heap
) {
171 if (p
->file_priv
== file_priv
)
175 /* Assumes a single contiguous range. Needs a special file_priv in
176 * 'heap' to stop it being subsumed.
178 list_for_each(p
, heap
) {
179 while (p
->file_priv
== 0 && p
->next
->file_priv
== 0) {
180 struct mem_block
*q
= p
->next
;
184 drm_free(q
, sizeof(*q
), DRM_MEM_DRIVER
);
191 void radeon_mem_takedown(struct mem_block
**heap
)
198 for (p
= (*heap
)->next
; p
!= *heap
;) {
199 struct mem_block
*q
= p
;
201 drm_free(q
, sizeof(*q
), DRM_MEM_DRIVER
);
204 drm_free(*heap
, sizeof(**heap
), DRM_MEM_DRIVER
);
210 static struct mem_block
**get_heap(drm_radeon_private_t
* dev_priv
, int region
)
213 case RADEON_MEM_REGION_GART
:
214 return &dev_priv
->gart_heap
;
215 case RADEON_MEM_REGION_FB
:
216 return &dev_priv
->fb_heap
;
222 int radeon_mem_alloc(struct drm_device
*dev
, void *data
, struct drm_file
*file_priv
)
224 drm_radeon_private_t
*dev_priv
= dev
->dev_private
;
225 drm_radeon_mem_alloc_t
*alloc
= data
;
226 struct mem_block
*block
, **heap
;
229 DRM_ERROR("called with no initialization\n");
233 heap
= get_heap(dev_priv
, alloc
->region
);
237 /* Make things easier on ourselves: all allocations at least
240 if (alloc
->alignment
< 12)
241 alloc
->alignment
= 12;
243 block
= alloc_block(*heap
, alloc
->size
, alloc
->alignment
, file_priv
);
248 if (DRM_COPY_TO_USER(alloc
->region_offset
, &block
->start
,
250 DRM_ERROR("copy_to_user\n");
257 int radeon_mem_free(struct drm_device
*dev
, void *data
, struct drm_file
*file_priv
)
259 drm_radeon_private_t
*dev_priv
= dev
->dev_private
;
260 drm_radeon_mem_free_t
*memfree
= data
;
261 struct mem_block
*block
, **heap
;
264 DRM_ERROR("called with no initialization\n");
268 heap
= get_heap(dev_priv
, memfree
->region
);
272 block
= find_block(*heap
, memfree
->region_offset
);
276 if (block
->file_priv
!= file_priv
)
283 int radeon_mem_init_heap(struct drm_device
*dev
, void *data
, struct drm_file
*file_priv
)
285 drm_radeon_private_t
*dev_priv
= dev
->dev_private
;
286 drm_radeon_mem_init_heap_t
*initheap
= data
;
287 struct mem_block
**heap
;
290 DRM_ERROR("called with no initialization\n");
294 heap
= get_heap(dev_priv
, initheap
->region
);
299 DRM_ERROR("heap already initialized?");
303 return init_heap(heap
, initheap
->start
, initheap
->size
);