Rev 817 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
808 | serge | 1 | /* radeon_mem.c -- Simple GART/fb memory manager for radeon -*- linux-c -*- */ |
2 | /* |
||
3 | * Copyright (C) The Weather Channel, Inc. 2002. All Rights Reserved. |
||
4 | * |
||
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. |
||
8 | * |
||
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: |
||
15 | * |
||
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 |
||
18 | * Software. |
||
19 | * |
||
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. |
||
27 | * |
||
28 | * Authors: |
||
29 | * Keith Whitwell |
||
30 | */ |
||
31 | |||
32 | #define USED_BLOCK 1 |
||
33 | |||
34 | #define list_for_each(entry, head) \ |
||
35 | for (entry = (head)->next; entry != head; entry = (entry)->next) |
||
36 | |||
37 | |||
38 | /* Very simple allocator for GART memory, working on a static range |
||
39 | * already mapped into each client's address space. |
||
40 | */ |
||
41 | |||
42 | struct mem_block { |
||
43 | struct mem_block *next; |
||
44 | struct mem_block *prev; |
||
829 | serge | 45 | u32_t start; |
46 | size_t size; |
||
808 | serge | 47 | }; |
48 | |||
49 | /* Initialize. How to check for an uninitialized heap? |
||
50 | */ |
||
51 | static int init_heap(struct mem_block **heap, int start, int size) |
||
52 | { |
||
829 | serge | 53 | struct mem_block *blocks = malloc(sizeof(*blocks)); |
808 | serge | 54 | |
55 | if (!blocks) |
||
56 | return -1; //-ENOMEM; |
||
57 | |||
829 | serge | 58 | *heap = malloc(sizeof(**heap)); |
808 | serge | 59 | if (!*heap) |
60 | { |
||
829 | serge | 61 | free(blocks); |
808 | serge | 62 | return -1; //-ENOMEM; |
63 | } |
||
64 | |||
65 | blocks->start = start; |
||
66 | blocks->size = size; |
||
67 | blocks->next = blocks->prev = *heap; |
||
68 | |||
69 | __clear(*heap,sizeof(**heap)); |
||
70 | (*heap)->next = (*heap)->prev = blocks; |
||
829 | serge | 71 | (*heap)->start |= USED_BLOCK; |
808 | serge | 72 | return 0; |
73 | } |
||
74 | |||
75 | static struct mem_block **get_heap(RHDPtr rhdPtr, int region) |
||
76 | { |
||
77 | switch (region) |
||
78 | { |
||
79 | case RHD_MEM_GART: |
||
80 | return &rhdPtr->gart_heap; |
||
81 | case RHD_MEM_FB: |
||
82 | return &rhdPtr->fb_heap; |
||
83 | default: |
||
84 | return NULL; |
||
85 | } |
||
86 | } |
||
87 | |||
88 | static struct mem_block *split_block(struct mem_block *p, int size) |
||
89 | { |
||
90 | |||
91 | /* Maybe cut off the end of an existing block */ |
||
92 | if (size < p->size) |
||
93 | { |
||
829 | serge | 94 | struct mem_block *newblock = malloc(sizeof(*newblock)); |
808 | serge | 95 | if (!newblock) |
96 | goto out; |
||
97 | newblock->start = p->start + size; |
||
98 | newblock->size = p->size - size; |
||
99 | newblock->next = p->next; |
||
100 | newblock->prev = p; |
||
101 | p->next->prev = newblock; |
||
102 | p->next = newblock; |
||
103 | p->size = size; |
||
829 | serge | 104 | p->start|=USED_BLOCK; |
808 | serge | 105 | } |
106 | |||
107 | out: |
||
108 | return p; |
||
109 | } |
||
110 | |||
111 | static struct mem_block *alloc_block(struct mem_block *heap, int size) |
||
112 | { |
||
113 | struct mem_block *p; |
||
114 | |||
115 | list_for_each(p, heap) |
||
116 | { |
||
829 | serge | 117 | |
808 | serge | 118 | if ( !(p->start & USED_BLOCK) && size <= p->size) |
119 | return split_block(p, size); |
||
120 | } |
||
121 | |||
122 | return NULL; |
||
123 | } |
||
124 | |||
125 | |||
126 | static struct mem_block *find_block(struct mem_block *heap, int start) |
||
127 | { |
||
128 | struct mem_block *p; |
||
129 | |||
130 | list_for_each(p, heap) |
||
131 | if ((p->start & ~USED_BLOCK) == start) |
||
132 | return p; |
||
133 | return NULL; |
||
134 | } |
||
135 | |||
136 | static void free_block(struct mem_block *p) |
||
137 | { |
||
138 | |||
139 | /* Assumes a single contiguous range. Needs a special file_priv in |
||
140 | * 'heap' to stop it being subsumed. |
||
141 | */ |
||
142 | |||
143 | p->start &= ~USED_BLOCK; |
||
144 | |||
145 | if ( !(p->next->start & USED_BLOCK)) |
||
146 | { |
||
147 | struct mem_block *q = p->next; |
||
148 | p->size += q->size; |
||
149 | p->next = q->next; |
||
150 | p->next->prev = p; |
||
829 | serge | 151 | free(q); |
808 | serge | 152 | } |
153 | |||
154 | if ( !(p->prev->start & USED_BLOCK)) |
||
155 | { |
||
156 | struct mem_block *q = p->prev; |
||
157 | q->size += p->size; |
||
158 | q->next = p->next; |
||
159 | q->next->prev = q; |
||
829 | serge | 160 | free(p); |
808 | serge | 161 | } |
162 | } |
||
163 | |||
164 | int rhdInitHeap(RHDPtr rhdPtr) |
||
165 | { |
||
817 | serge | 166 | int base = rhdPtr->FbFreeStart; |
808 | serge | 167 | |
168 | return init_heap(&rhdPtr->fb_heap, base, rhdPtr->FbFreeSize); |
||
169 | }; |
||
170 | |||
171 | void *rhd_mem_alloc(RHDPtr rhdPtr,int region, int size) |
||
172 | { |
||
173 | struct mem_block *block, **heap; |
||
174 | |||
175 | heap = get_heap(rhdPtr, region); |
||
829 | serge | 176 | if (!heap || !*heap) |
808 | serge | 177 | return NULL; |
178 | |||
179 | /* Make things easier on ourselves: all allocations at least |
||
180 | * 4k aligned. |
||
181 | */ |
||
182 | |||
183 | size = (size+4095) & ~4095; |
||
184 | |||
185 | block = alloc_block(*heap, size); |
||
186 | |||
187 | if (!block) |
||
188 | return NULL; |
||
189 | |||
190 | return (void*)(block->start & ~USED_BLOCK); |
||
191 | } |
||
192 | |||
193 | int rhd_mem_free(RHDPtr rhdPtr, int region, void *offset) |
||
194 | { |
||
195 | struct mem_block *block, **heap; |
||
196 | |||
197 | heap = get_heap(rhdPtr, region); |
||
198 | if (!heap || !*heap) |
||
199 | return -1; |
||
200 | |||
201 | block = find_block(*heap, (int)offset); |
||
202 | if (!block) |
||
203 | return -1; |
||
204 | |||
829 | serge | 205 | if ( !(block->start & USED_BLOCK)) |
808 | serge | 206 | return -1; |
207 | |||
208 | free_block(block); |
||
209 | return 0; |
||
210 | } |
||
211 | |||
829 | serge | 212 | void dump_mem() |
213 | { |
||
214 | struct mem_block *p; |
||
215 | struct mem_block **heap; |
||
808 | serge | 216 | |
829 | serge | 217 | heap = &rhd.fb_heap; |
218 | |||
219 | list_for_each(p, *heap) |
||
220 | { |
||
221 | dbgprintf("block: %x next: %x prev: %x start: %x size:%x\n", |
||
222 | p,p->next,p->prev,p->start,p->size); |
||
223 | } |
||
224 | }=>> |
||
225 |