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>
34 #include "radeon_drm.h"
35 #include "radeon_drv.h"
37 /* Very simple allocator for GART memory, working on a static range
38 * already mapped into each client's address space.
41 static struct mem_block
*split_block(struct mem_block
*p
, int start
, int size
,
42 struct drm_file
*file_priv
)
44 /* Maybe cut off the start of an existing block */
45 if (start
> p
->start
) {
46 struct mem_block
*newblock
= kmalloc(sizeof(*newblock
),
50 newblock
->start
= start
;
51 newblock
->size
= p
->size
- (start
- p
->start
);
52 newblock
->file_priv
= NULL
;
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
= kmalloc(sizeof(*newblock
),
67 newblock
->start
= start
+ size
;
68 newblock
->size
= p
->size
- size
;
69 newblock
->file_priv
= NULL
;
70 newblock
->next
= p
->next
;
72 p
->next
->prev
= newblock
;
78 /* Our block is in the middle */
79 p
->file_priv
= file_priv
;
83 static struct mem_block
*alloc_block(struct mem_block
*heap
, int size
,
84 int align2
, struct drm_file
*file_priv
)
87 int mask
= (1 << align2
) - 1;
89 list_for_each(p
, heap
) {
90 int start
= (p
->start
+ mask
) & ~mask
;
91 if (p
->file_priv
== NULL
&& start
+ size
<= p
->start
+ p
->size
)
92 return split_block(p
, start
, size
, file_priv
);
98 static struct mem_block
*find_block(struct mem_block
*heap
, int start
)
102 list_for_each(p
, heap
)
103 if (p
->start
== start
)
109 static void free_block(struct mem_block
*p
)
113 /* Assumes a single contiguous range. Needs a special file_priv in
114 * 'heap' to stop it being subsumed.
116 if (p
->next
->file_priv
== NULL
) {
117 struct mem_block
*q
= p
->next
;
124 if (p
->prev
->file_priv
== NULL
) {
125 struct mem_block
*q
= p
->prev
;
133 /* Initialize. How to check for an uninitialized heap?
135 static int init_heap(struct mem_block
**heap
, int start
, int size
)
137 struct mem_block
*blocks
= kmalloc(sizeof(*blocks
), GFP_KERNEL
);
142 *heap
= kzalloc(sizeof(**heap
), GFP_KERNEL
);
148 blocks
->start
= start
;
150 blocks
->file_priv
= NULL
;
151 blocks
->next
= blocks
->prev
= *heap
;
153 (*heap
)->file_priv
= (struct drm_file
*) - 1;
154 (*heap
)->next
= (*heap
)->prev
= blocks
;
158 /* Free all blocks associated with the releasing file.
160 void radeon_mem_release(struct drm_file
*file_priv
, struct mem_block
*heap
)
164 if (!heap
|| !heap
->next
)
167 list_for_each(p
, heap
) {
168 if (p
->file_priv
== file_priv
)
172 /* Assumes a single contiguous range. Needs a special file_priv in
173 * 'heap' to stop it being subsumed.
175 list_for_each(p
, heap
) {
176 while (p
->file_priv
== NULL
&& p
->next
->file_priv
== NULL
) {
177 struct mem_block
*q
= p
->next
;
188 void radeon_mem_takedown(struct mem_block
**heap
)
195 for (p
= (*heap
)->next
; p
!= *heap
;) {
196 struct mem_block
*q
= p
;
207 static struct mem_block
**get_heap(drm_radeon_private_t
* dev_priv
, int region
)
210 case RADEON_MEM_REGION_GART
:
211 return &dev_priv
->gart_heap
;
212 case RADEON_MEM_REGION_FB
:
213 return &dev_priv
->fb_heap
;
219 int radeon_mem_alloc(struct drm_device
*dev
, void *data
, struct drm_file
*file_priv
)
221 drm_radeon_private_t
*dev_priv
= dev
->dev_private
;
222 drm_radeon_mem_alloc_t
*alloc
= data
;
223 struct mem_block
*block
, **heap
;
226 DRM_ERROR("called with no initialization\n");
230 heap
= get_heap(dev_priv
, alloc
->region
);
234 /* Make things easier on ourselves: all allocations at least
237 if (alloc
->alignment
< 12)
238 alloc
->alignment
= 12;
240 block
= alloc_block(*heap
, alloc
->size
, alloc
->alignment
, file_priv
);
245 if (DRM_COPY_TO_USER(alloc
->region_offset
, &block
->start
,
247 DRM_ERROR("copy_to_user\n");
254 int radeon_mem_free(struct drm_device
*dev
, void *data
, struct drm_file
*file_priv
)
256 drm_radeon_private_t
*dev_priv
= dev
->dev_private
;
257 drm_radeon_mem_free_t
*memfree
= data
;
258 struct mem_block
*block
, **heap
;
261 DRM_ERROR("called with no initialization\n");
265 heap
= get_heap(dev_priv
, memfree
->region
);
269 block
= find_block(*heap
, memfree
->region_offset
);
273 if (block
->file_priv
!= file_priv
)
280 int radeon_mem_init_heap(struct drm_device
*dev
, void *data
, struct drm_file
*file_priv
)
282 drm_radeon_private_t
*dev_priv
= dev
->dev_private
;
283 drm_radeon_mem_init_heap_t
*initheap
= data
;
284 struct mem_block
**heap
;
287 DRM_ERROR("called with no initialization\n");
291 heap
= get_heap(dev_priv
, initheap
->region
);
296 DRM_ERROR("heap already initialized?");
300 return init_heap(heap
, initheap
->start
, initheap
->size
);