3 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
4 * Use is subject to license terms.
6 /* radeon_mem.c -- Simple GART/fb memory manager for radeon -*- linux-c -*- */
8 * Copyright (C) The Weather Channel, Inc. 2002. All Rights Reserved.
10 * The Weather Channel (TM) funded Tungsten Graphics to develop the
11 * initial release of the Radeon 8500 driver under the XFree86 license.
12 * This notice must be preserved.
14 * Permission is hereby granted, free of charge, to any person obtaining a
15 * copy of this software and associated documentation files (the "Software"),
16 * to deal in the Software without restriction, including without limitation
17 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
18 * and/or sell copies of the Software, and to permit persons to whom the
19 * Software is furnished to do so, subject to the following conditions:
21 * The above copyright notice and this permission notice (including the next
22 * paragraph) shall be included in all copies or substantial portions of the
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
28 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
29 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
30 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
31 * DEALINGS IN THE SOFTWARE.
34 * Keith Whitwell <keith@tungstengraphics.com>
37 #pragma ident "%Z%%M% %I% %E% SMI"
41 #include "radeon_drm.h"
42 #include "radeon_drv.h"
43 #include "radeon_io32.h"
46 * Very simple allocator for GART memory, working on a static range
47 * already mapped into each client's address space.
50 static struct mem_block
*
51 split_block(struct mem_block
*p
, int start
, int size
, drm_file_t
*filp
)
53 /* Maybe cut off the start of an existing block */
54 if (start
> p
->start
) {
55 struct mem_block
*newblock
=
56 drm_alloc(sizeof (*newblock
), DRM_MEM_BUFS
);
59 newblock
->start
= start
;
60 newblock
->size
= p
->size
- (start
- p
->start
);
61 newblock
->filp
= NULL
;
62 newblock
->next
= p
->next
;
64 p
->next
->prev
= newblock
;
66 p
->size
-= newblock
->size
;
70 /* Maybe cut off the end of an existing block */
72 struct mem_block
*newblock
=
73 drm_alloc(sizeof (*newblock
), DRM_MEM_BUFS
);
76 newblock
->start
= start
+ size
;
77 newblock
->size
= p
->size
- size
;
78 newblock
->filp
= NULL
;
79 newblock
->next
= p
->next
;
81 p
->next
->prev
= newblock
;
87 /* Our block is in the middle */
92 static struct mem_block
*
93 alloc_block(struct mem_block
*heap
, int size
, int align2
, drm_file_t
*filp
)
96 int mask
= (1 << align2
) - 1;
98 for (p
= heap
->next
; p
!= heap
; p
= p
->next
) {
99 int start
= (p
->start
+ mask
) & ~mask
;
100 if (p
->filp
== 0 && start
+ size
<= p
->start
+ p
->size
)
101 return (split_block(p
, start
, size
, filp
));
107 static struct mem_block
*
108 find_block(struct mem_block
*heap
, int start
)
112 for (p
= heap
->next
; p
!= heap
; p
= p
->next
)
113 if (p
->start
== start
)
120 free_block(struct mem_block
*p
)
125 * Assumes a single contiguous range. Needs a special filp in
126 * 'heap' to stop it being subsumed.
128 if (p
->next
->filp
== 0) {
129 struct mem_block
*q
= p
->next
;
133 drm_free(q
, sizeof (*q
), DRM_MEM_BUFS
);
136 if (p
->prev
->filp
== 0) {
137 struct mem_block
*q
= p
->prev
;
141 drm_free(p
, sizeof (*q
), DRM_MEM_BUFS
);
146 * Initialize. How to check for an uninitialized heap?
149 init_heap(struct mem_block
**heap
, int start
, int size
)
151 struct mem_block
*blocks
= drm_alloc(sizeof (*blocks
), DRM_MEM_BUFS
);
156 *heap
= drm_alloc(sizeof (**heap
), DRM_MEM_BUFS
);
158 drm_free(blocks
, sizeof (*blocks
), DRM_MEM_BUFS
);
162 blocks
->start
= start
;
165 blocks
->next
= blocks
->prev
= *heap
;
167 (void) memset(*heap
, 0, sizeof (**heap
));
168 (*heap
)->filp
= (drm_file_t
*)-1;
169 (*heap
)->next
= (*heap
)->prev
= blocks
;
174 * Free all blocks associated with the releasing file.
177 radeon_mem_release(drm_file_t
*filp
, struct mem_block
*heap
)
181 if (!heap
|| !heap
->next
)
184 for (p
= heap
->next
; p
!= heap
; p
= p
->next
) {
190 * Assumes a single contiguous range. Needs a special filp in
191 * 'heap' to stop it being subsumed.
193 for (p
= heap
->next
; p
!= heap
; p
= p
->next
) {
194 while (p
->filp
== 0 && p
->next
->filp
== 0) {
195 struct mem_block
*q
= p
->next
;
199 drm_free(q
, sizeof (*q
), DRM_MEM_DRIVER
);
208 radeon_mem_takedown(struct mem_block
**heap
)
215 for (p
= (*heap
)->next
; p
!= *heap
; ) {
216 struct mem_block
*q
= p
;
218 drm_free(q
, sizeof (*q
), DRM_MEM_DRIVER
);
221 drm_free(*heap
, sizeof (**heap
), DRM_MEM_DRIVER
);
227 static struct mem_block
**
228 get_heap(drm_radeon_private_t
*dev_priv
, int region
)
231 case RADEON_MEM_REGION_GART
:
232 return (&dev_priv
->gart_heap
);
233 case RADEON_MEM_REGION_FB
:
234 return (&dev_priv
->fb_heap
);
242 radeon_mem_alloc(DRM_IOCTL_ARGS
)
245 drm_radeon_private_t
*dev_priv
= dev
->dev_private
;
246 drm_radeon_mem_alloc_t alloc
;
247 struct mem_block
*block
, **heap
;
250 DRM_ERROR("%s called with no initialization\n", __FUNCTION__
);
254 #ifdef _MULTI_DATAMODEL
255 if (ddi_model_convert_from(mode
& FMODELS
) == DDI_MODEL_ILP32
) {
256 drm_radeon_mem_alloc_32_t alloc32
;
258 DRM_COPYFROM_WITH_RETURN(&alloc32
, (void *) data
,
260 alloc
.region
= alloc32
.region
;
261 alloc
.alignment
= alloc32
.alignment
;
262 alloc
.size
= alloc32
.size
;
263 alloc
.region_offset
= (void *)(uintptr_t)alloc32
.region_offset
;
266 DRM_COPYFROM_WITH_RETURN(&alloc
, (void *) data
, sizeof (alloc
));
267 #ifdef _MULTI_DATAMODEL
271 heap
= get_heap(dev_priv
, alloc
.region
);
276 * Make things easier on ourselves: all allocations at least
279 if (alloc
.alignment
< 12)
280 alloc
.alignment
= 12;
282 block
= alloc_block(*heap
, alloc
.size
, alloc
.alignment
, fpriv
);
287 if (DRM_COPY_TO_USER(alloc
.region_offset
, &block
->start
,
289 DRM_ERROR("copy_to_user\n");
298 radeon_mem_free(DRM_IOCTL_ARGS
)
301 drm_radeon_private_t
*dev_priv
= dev
->dev_private
;
302 drm_radeon_mem_free_t memfree
;
303 struct mem_block
*block
, **heap
;
306 DRM_ERROR("%s called with no initialization\n", __FUNCTION__
);
310 DRM_COPYFROM_WITH_RETURN(&memfree
, (void *) data
, sizeof (memfree
));
312 heap
= get_heap(dev_priv
, memfree
.region
);
316 block
= find_block(*heap
, memfree
.region_offset
);
320 if (block
->filp
!= fpriv
)
329 radeon_mem_init_heap(DRM_IOCTL_ARGS
)
332 drm_radeon_private_t
*dev_priv
= dev
->dev_private
;
333 drm_radeon_mem_init_heap_t initheap
;
334 struct mem_block
**heap
;
337 DRM_ERROR("%s called with no initialization\n", __FUNCTION__
);
341 DRM_COPYFROM_WITH_RETURN(&initheap
, (void *) data
, sizeof (initheap
));
343 heap
= get_heap(dev_priv
, initheap
.region
);
348 DRM_ERROR("heap already initialized?");
352 return (init_heap(heap
, initheap
.start
, initheap
.size
));