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>
33 #include <drm/radeon_drm.h>
34 #include "radeon_drv.h"
36 /* Very simple allocator for GART memory, working on a static range
37 * already mapped into each client's address space.
40 static struct mem_block
*split_block(struct mem_block
*p
, int start
, int size
,
41 struct drm_file
*file_priv
)
43 /* Maybe cut off the start of an existing block */
44 if (start
> p
->start
) {
45 struct mem_block
*newblock
= kmalloc(sizeof(*newblock
),
49 newblock
->start
= start
;
50 newblock
->size
= p
->size
- (start
- p
->start
);
51 newblock
->file_priv
= NULL
;
52 newblock
->next
= p
->next
;
54 p
->next
->prev
= newblock
;
56 p
->size
-= newblock
->size
;
60 /* Maybe cut off the end of an existing block */
62 struct mem_block
*newblock
= kmalloc(sizeof(*newblock
),
66 newblock
->start
= start
+ size
;
67 newblock
->size
= p
->size
- size
;
68 newblock
->file_priv
= NULL
;
69 newblock
->next
= p
->next
;
71 p
->next
->prev
= newblock
;
77 /* Our block is in the middle */
78 p
->file_priv
= file_priv
;
82 static struct mem_block
*alloc_block(struct mem_block
*heap
, int size
,
83 int align2
, struct drm_file
*file_priv
)
86 int mask
= (1 << align2
) - 1;
88 list_for_each(p
, heap
) {
89 int start
= (p
->start
+ mask
) & ~mask
;
90 if (p
->file_priv
== NULL
&& start
+ size
<= p
->start
+ p
->size
)
91 return split_block(p
, start
, size
, file_priv
);
97 static struct mem_block
*find_block(struct mem_block
*heap
, int start
)
101 list_for_each(p
, heap
)
102 if (p
->start
== start
)
108 static void free_block(struct mem_block
*p
)
112 /* Assumes a single contiguous range. Needs a special file_priv in
113 * 'heap' to stop it being subsumed.
115 if (p
->next
->file_priv
== NULL
) {
116 struct mem_block
*q
= p
->next
;
123 if (p
->prev
->file_priv
== NULL
) {
124 struct mem_block
*q
= p
->prev
;
132 /* Initialize. How to check for an uninitialized heap?
134 static int init_heap(struct mem_block
**heap
, int start
, int size
)
136 struct mem_block
*blocks
= kmalloc(sizeof(*blocks
), GFP_KERNEL
);
141 *heap
= kzalloc(sizeof(**heap
), GFP_KERNEL
);
147 blocks
->start
= start
;
149 blocks
->file_priv
= NULL
;
150 blocks
->next
= blocks
->prev
= *heap
;
152 (*heap
)->file_priv
= (struct drm_file
*) - 1;
153 (*heap
)->next
= (*heap
)->prev
= blocks
;
157 /* Free all blocks associated with the releasing file.
159 void radeon_mem_release(struct drm_file
*file_priv
, struct mem_block
*heap
)
163 if (!heap
|| !heap
->next
)
166 list_for_each(p
, heap
) {
167 if (p
->file_priv
== file_priv
)
171 /* Assumes a single contiguous range. Needs a special file_priv in
172 * 'heap' to stop it being subsumed.
174 list_for_each(p
, heap
) {
175 while (p
->file_priv
== NULL
&& p
->next
->file_priv
== NULL
) {
176 struct mem_block
*q
= p
->next
;
187 void radeon_mem_takedown(struct mem_block
**heap
)
194 for (p
= (*heap
)->next
; p
!= *heap
;) {
195 struct mem_block
*q
= p
;
206 static struct mem_block
**get_heap(drm_radeon_private_t
* dev_priv
, int region
)
209 case RADEON_MEM_REGION_GART
:
210 return &dev_priv
->gart_heap
;
211 case RADEON_MEM_REGION_FB
:
212 return &dev_priv
->fb_heap
;
218 int radeon_mem_alloc(struct drm_device
*dev
, void *data
, struct drm_file
*file_priv
)
220 drm_radeon_private_t
*dev_priv
= dev
->dev_private
;
221 drm_radeon_mem_alloc_t
*alloc
= data
;
222 struct mem_block
*block
, **heap
;
225 DRM_ERROR("called with no initialization\n");
229 heap
= get_heap(dev_priv
, alloc
->region
);
233 /* Make things easier on ourselves: all allocations at least
236 if (alloc
->alignment
< 12)
237 alloc
->alignment
= 12;
239 block
= alloc_block(*heap
, alloc
->size
, alloc
->alignment
, file_priv
);
244 if (DRM_COPY_TO_USER(alloc
->region_offset
, &block
->start
,
246 DRM_ERROR("copy_to_user\n");
253 int radeon_mem_free(struct drm_device
*dev
, void *data
, struct drm_file
*file_priv
)
255 drm_radeon_private_t
*dev_priv
= dev
->dev_private
;
256 drm_radeon_mem_free_t
*memfree
= data
;
257 struct mem_block
*block
, **heap
;
260 DRM_ERROR("called with no initialization\n");
264 heap
= get_heap(dev_priv
, memfree
->region
);
268 block
= find_block(*heap
, memfree
->region_offset
);
272 if (block
->file_priv
!= file_priv
)
279 int radeon_mem_init_heap(struct drm_device
*dev
, void *data
, struct drm_file
*file_priv
)
281 drm_radeon_private_t
*dev_priv
= dev
->dev_private
;
282 drm_radeon_mem_init_heap_t
*initheap
= data
;
283 struct mem_block
**heap
;
286 DRM_ERROR("called with no initialization\n");
290 heap
= get_heap(dev_priv
, initheap
->region
);
295 DRM_ERROR("heap already initialized?");
299 return init_heap(heap
, initheap
->start
, initheap
->size
);