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 * ------------------------ This file is DEPRECATED! -------------------------
35 #include <drm/radeon_drm.h>
36 #include "radeon_drv.h"
38 /* Very simple allocator for GART memory, working on a static range
39 * already mapped into each client's address space.
42 static struct mem_block
*split_block(struct mem_block
*p
, int start
, int size
,
43 struct drm_file
*file_priv
)
45 /* Maybe cut off the start of an existing block */
46 if (start
> p
->start
) {
47 struct mem_block
*newblock
= kmalloc(sizeof(*newblock
),
51 newblock
->start
= start
;
52 newblock
->size
= p
->size
- (start
- p
->start
);
53 newblock
->file_priv
= NULL
;
54 newblock
->next
= p
->next
;
56 p
->next
->prev
= newblock
;
58 p
->size
-= newblock
->size
;
62 /* Maybe cut off the end of an existing block */
64 struct mem_block
*newblock
= kmalloc(sizeof(*newblock
),
68 newblock
->start
= start
+ size
;
69 newblock
->size
= p
->size
- size
;
70 newblock
->file_priv
= NULL
;
71 newblock
->next
= p
->next
;
73 p
->next
->prev
= newblock
;
79 /* Our block is in the middle */
80 p
->file_priv
= file_priv
;
84 static struct mem_block
*alloc_block(struct mem_block
*heap
, int size
,
85 int align2
, struct drm_file
*file_priv
)
88 int mask
= (1 << align2
) - 1;
90 list_for_each(p
, heap
) {
91 int start
= (p
->start
+ mask
) & ~mask
;
92 if (p
->file_priv
== NULL
&& start
+ size
<= p
->start
+ p
->size
)
93 return split_block(p
, start
, size
, file_priv
);
99 static struct mem_block
*find_block(struct mem_block
*heap
, int start
)
103 list_for_each(p
, heap
)
104 if (p
->start
== start
)
110 static void free_block(struct mem_block
*p
)
114 /* Assumes a single contiguous range. Needs a special file_priv in
115 * 'heap' to stop it being subsumed.
117 if (p
->next
->file_priv
== NULL
) {
118 struct mem_block
*q
= p
->next
;
125 if (p
->prev
->file_priv
== NULL
) {
126 struct mem_block
*q
= p
->prev
;
134 /* Initialize. How to check for an uninitialized heap?
136 static int init_heap(struct mem_block
**heap
, int start
, int size
)
138 struct mem_block
*blocks
= kmalloc(sizeof(*blocks
), GFP_KERNEL
);
143 *heap
= kzalloc(sizeof(**heap
), GFP_KERNEL
);
149 blocks
->start
= start
;
151 blocks
->file_priv
= NULL
;
152 blocks
->next
= blocks
->prev
= *heap
;
154 (*heap
)->file_priv
= (struct drm_file
*) - 1;
155 (*heap
)->next
= (*heap
)->prev
= blocks
;
159 /* Free all blocks associated with the releasing file.
161 void radeon_mem_release(struct drm_file
*file_priv
, struct mem_block
*heap
)
165 if (!heap
|| !heap
->next
)
168 list_for_each(p
, heap
) {
169 if (p
->file_priv
== file_priv
)
173 /* Assumes a single contiguous range. Needs a special file_priv in
174 * 'heap' to stop it being subsumed.
176 list_for_each(p
, heap
) {
177 while (p
->file_priv
== NULL
&& p
->next
->file_priv
== NULL
) {
178 struct mem_block
*q
= p
->next
;
189 void radeon_mem_takedown(struct mem_block
**heap
)
196 for (p
= (*heap
)->next
; p
!= *heap
;) {
197 struct mem_block
*q
= p
;
208 static struct mem_block
**get_heap(drm_radeon_private_t
* dev_priv
, int region
)
211 case RADEON_MEM_REGION_GART
:
212 return &dev_priv
->gart_heap
;
213 case RADEON_MEM_REGION_FB
:
214 return &dev_priv
->fb_heap
;
220 int radeon_mem_alloc(struct drm_device
*dev
, void *data
, struct drm_file
*file_priv
)
222 drm_radeon_private_t
*dev_priv
= dev
->dev_private
;
223 drm_radeon_mem_alloc_t
*alloc
= data
;
224 struct mem_block
*block
, **heap
;
227 DRM_ERROR("called with no initialization\n");
231 heap
= get_heap(dev_priv
, alloc
->region
);
235 /* Make things easier on ourselves: all allocations at least
238 if (alloc
->alignment
< 12)
239 alloc
->alignment
= 12;
241 block
= alloc_block(*heap
, alloc
->size
, alloc
->alignment
, file_priv
);
246 if (copy_to_user(alloc
->region_offset
, &block
->start
,
248 DRM_ERROR("copy_to_user\n");
255 int radeon_mem_free(struct drm_device
*dev
, void *data
, struct drm_file
*file_priv
)
257 drm_radeon_private_t
*dev_priv
= dev
->dev_private
;
258 drm_radeon_mem_free_t
*memfree
= data
;
259 struct mem_block
*block
, **heap
;
262 DRM_ERROR("called with no initialization\n");
266 heap
= get_heap(dev_priv
, memfree
->region
);
270 block
= find_block(*heap
, memfree
->region_offset
);
274 if (block
->file_priv
!= file_priv
)
281 int radeon_mem_init_heap(struct drm_device
*dev
, void *data
, struct drm_file
*file_priv
)
283 drm_radeon_private_t
*dev_priv
= dev
->dev_private
;
284 drm_radeon_mem_init_heap_t
*initheap
= data
;
285 struct mem_block
**heap
;
288 DRM_ERROR("called with no initialization\n");
292 heap
= get_heap(dev_priv
, initheap
->region
);
297 DRM_ERROR("heap already initialized?");
301 return init_heap(heap
, initheap
->start
, initheap
->size
);