Commit | Line | Data |
---|---|---|
94bb598e DA |
1 | /* radeon_mem.c -- Simple GART/fb memory manager for radeon -*- linux-c -*- */ |
2 | /* | |
1da177e4 | 3 | * Copyright (C) The Weather Channel, Inc. 2002. All Rights Reserved. |
b5e89ed5 | 4 | * |
1da177e4 LT |
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 <keith@tungstengraphics.com> | |
30 | */ | |
31 | ||
32 | #include "drmP.h" | |
33 | #include "drm.h" | |
34 | #include "radeon_drm.h" | |
35 | #include "radeon_drv.h" | |
36 | ||
37 | /* Very simple allocator for GART memory, working on a static range | |
b5e89ed5 | 38 | * already mapped into each client's address space. |
1da177e4 LT |
39 | */ |
40 | ||
41 | static struct mem_block *split_block(struct mem_block *p, int start, int size, | |
6c340eac | 42 | struct drm_file *file_priv) |
1da177e4 LT |
43 | { |
44 | /* Maybe cut off the start of an existing block */ | |
45 | if (start > p->start) { | |
9a298b2a EA |
46 | struct mem_block *newblock = kmalloc(sizeof(*newblock), |
47 | GFP_KERNEL); | |
b5e89ed5 | 48 | if (!newblock) |
1da177e4 LT |
49 | goto out; |
50 | newblock->start = start; | |
51 | newblock->size = p->size - (start - p->start); | |
6c340eac | 52 | newblock->file_priv = NULL; |
1da177e4 LT |
53 | newblock->next = p->next; |
54 | newblock->prev = p; | |
55 | p->next->prev = newblock; | |
56 | p->next = newblock; | |
57 | p->size -= newblock->size; | |
58 | p = newblock; | |
59 | } | |
b5e89ed5 | 60 | |
1da177e4 LT |
61 | /* Maybe cut off the end of an existing block */ |
62 | if (size < p->size) { | |
9a298b2a EA |
63 | struct mem_block *newblock = kmalloc(sizeof(*newblock), |
64 | GFP_KERNEL); | |
1da177e4 LT |
65 | if (!newblock) |
66 | goto out; | |
67 | newblock->start = start + size; | |
68 | newblock->size = p->size - size; | |
6c340eac | 69 | newblock->file_priv = NULL; |
1da177e4 LT |
70 | newblock->next = p->next; |
71 | newblock->prev = p; | |
72 | p->next->prev = newblock; | |
73 | p->next = newblock; | |
74 | p->size = size; | |
75 | } | |
76 | ||
b5e89ed5 | 77 | out: |
1da177e4 | 78 | /* Our block is in the middle */ |
6c340eac | 79 | p->file_priv = file_priv; |
1da177e4 LT |
80 | return p; |
81 | } | |
82 | ||
b5e89ed5 | 83 | static struct mem_block *alloc_block(struct mem_block *heap, int size, |
6c340eac | 84 | int align2, struct drm_file *file_priv) |
1da177e4 LT |
85 | { |
86 | struct mem_block *p; | |
b5e89ed5 | 87 | int mask = (1 << align2) - 1; |
1da177e4 LT |
88 | |
89 | list_for_each(p, heap) { | |
90 | int start = (p->start + mask) & ~mask; | |
2b46278b | 91 | if (p->file_priv == NULL && start + size <= p->start + p->size) |
6c340eac | 92 | return split_block(p, start, size, file_priv); |
1da177e4 LT |
93 | } |
94 | ||
95 | return NULL; | |
96 | } | |
97 | ||
b5e89ed5 | 98 | static struct mem_block *find_block(struct mem_block *heap, int start) |
1da177e4 LT |
99 | { |
100 | struct mem_block *p; | |
101 | ||
102 | list_for_each(p, heap) | |
b5e89ed5 DA |
103 | if (p->start == start) |
104 | return p; | |
1da177e4 LT |
105 | |
106 | return NULL; | |
107 | } | |
108 | ||
b5e89ed5 | 109 | static void free_block(struct mem_block *p) |
1da177e4 | 110 | { |
6c340eac | 111 | p->file_priv = NULL; |
1da177e4 | 112 | |
6c340eac | 113 | /* Assumes a single contiguous range. Needs a special file_priv in |
1da177e4 LT |
114 | * 'heap' to stop it being subsumed. |
115 | */ | |
2b46278b | 116 | if (p->next->file_priv == NULL) { |
1da177e4 LT |
117 | struct mem_block *q = p->next; |
118 | p->size += q->size; | |
119 | p->next = q->next; | |
120 | p->next->prev = p; | |
9a298b2a | 121 | kfree(q); |
1da177e4 LT |
122 | } |
123 | ||
2b46278b | 124 | if (p->prev->file_priv == NULL) { |
1da177e4 LT |
125 | struct mem_block *q = p->prev; |
126 | q->size += p->size; | |
127 | q->next = p->next; | |
128 | q->next->prev = q; | |
9a298b2a | 129 | kfree(p); |
1da177e4 LT |
130 | } |
131 | } | |
132 | ||
133 | /* Initialize. How to check for an uninitialized heap? | |
134 | */ | |
135 | static int init_heap(struct mem_block **heap, int start, int size) | |
136 | { | |
9a298b2a | 137 | struct mem_block *blocks = kmalloc(sizeof(*blocks), GFP_KERNEL); |
1da177e4 | 138 | |
b5e89ed5 | 139 | if (!blocks) |
20caafa6 | 140 | return -ENOMEM; |
b5e89ed5 | 141 | |
f35119d6 | 142 | *heap = kzalloc(sizeof(**heap), GFP_KERNEL); |
1da177e4 | 143 | if (!*heap) { |
9a298b2a | 144 | kfree(blocks); |
20caafa6 | 145 | return -ENOMEM; |
1da177e4 LT |
146 | } |
147 | ||
148 | blocks->start = start; | |
149 | blocks->size = size; | |
6c340eac | 150 | blocks->file_priv = NULL; |
1da177e4 LT |
151 | blocks->next = blocks->prev = *heap; |
152 | ||
6c340eac | 153 | (*heap)->file_priv = (struct drm_file *) - 1; |
1da177e4 LT |
154 | (*heap)->next = (*heap)->prev = blocks; |
155 | return 0; | |
156 | } | |
157 | ||
1da177e4 LT |
158 | /* Free all blocks associated with the releasing file. |
159 | */ | |
6c340eac | 160 | void radeon_mem_release(struct drm_file *file_priv, struct mem_block *heap) |
1da177e4 LT |
161 | { |
162 | struct mem_block *p; | |
163 | ||
164 | if (!heap || !heap->next) | |
165 | return; | |
166 | ||
167 | list_for_each(p, heap) { | |
6c340eac EA |
168 | if (p->file_priv == file_priv) |
169 | p->file_priv = NULL; | |
1da177e4 LT |
170 | } |
171 | ||
6c340eac | 172 | /* Assumes a single contiguous range. Needs a special file_priv in |
1da177e4 LT |
173 | * 'heap' to stop it being subsumed. |
174 | */ | |
175 | list_for_each(p, heap) { | |
2b46278b | 176 | while (p->file_priv == NULL && p->next->file_priv == NULL) { |
1da177e4 LT |
177 | struct mem_block *q = p->next; |
178 | p->size += q->size; | |
179 | p->next = q->next; | |
180 | p->next->prev = p; | |
9a298b2a | 181 | kfree(q); |
1da177e4 LT |
182 | } |
183 | } | |
184 | } | |
185 | ||
186 | /* Shutdown. | |
187 | */ | |
b5e89ed5 | 188 | void radeon_mem_takedown(struct mem_block **heap) |
1da177e4 LT |
189 | { |
190 | struct mem_block *p; | |
b5e89ed5 | 191 | |
1da177e4 LT |
192 | if (!*heap) |
193 | return; | |
194 | ||
b5e89ed5 | 195 | for (p = (*heap)->next; p != *heap;) { |
1da177e4 LT |
196 | struct mem_block *q = p; |
197 | p = p->next; | |
9a298b2a | 198 | kfree(q); |
1da177e4 LT |
199 | } |
200 | ||
9a298b2a | 201 | kfree(*heap); |
1da177e4 LT |
202 | *heap = NULL; |
203 | } | |
204 | ||
1da177e4 LT |
205 | /* IOCTL HANDLERS */ |
206 | ||
b5e89ed5 | 207 | static struct mem_block **get_heap(drm_radeon_private_t * dev_priv, int region) |
1da177e4 | 208 | { |
b5e89ed5 | 209 | switch (region) { |
1da177e4 | 210 | case RADEON_MEM_REGION_GART: |
b5e89ed5 | 211 | return &dev_priv->gart_heap; |
1da177e4 LT |
212 | case RADEON_MEM_REGION_FB: |
213 | return &dev_priv->fb_heap; | |
214 | default: | |
215 | return NULL; | |
216 | } | |
217 | } | |
218 | ||
c153f45f | 219 | int radeon_mem_alloc(struct drm_device *dev, void *data, struct drm_file *file_priv) |
1da177e4 | 220 | { |
1da177e4 | 221 | drm_radeon_private_t *dev_priv = dev->dev_private; |
c153f45f | 222 | drm_radeon_mem_alloc_t *alloc = data; |
1da177e4 LT |
223 | struct mem_block *block, **heap; |
224 | ||
b5e89ed5 | 225 | if (!dev_priv) { |
3e684eae | 226 | DRM_ERROR("called with no initialization\n"); |
20caafa6 | 227 | return -EINVAL; |
1da177e4 LT |
228 | } |
229 | ||
c153f45f | 230 | heap = get_heap(dev_priv, alloc->region); |
1da177e4 | 231 | if (!heap || !*heap) |
20caafa6 | 232 | return -EFAULT; |
b5e89ed5 | 233 | |
1da177e4 LT |
234 | /* Make things easier on ourselves: all allocations at least |
235 | * 4k aligned. | |
236 | */ | |
c153f45f EA |
237 | if (alloc->alignment < 12) |
238 | alloc->alignment = 12; | |
1da177e4 | 239 | |
c153f45f | 240 | block = alloc_block(*heap, alloc->size, alloc->alignment, file_priv); |
1da177e4 | 241 | |
b5e89ed5 | 242 | if (!block) |
20caafa6 | 243 | return -ENOMEM; |
1da177e4 | 244 | |
c153f45f EA |
245 | if (DRM_COPY_TO_USER(alloc->region_offset, &block->start, |
246 | sizeof(int))) { | |
b5e89ed5 | 247 | DRM_ERROR("copy_to_user\n"); |
20caafa6 | 248 | return -EFAULT; |
1da177e4 | 249 | } |
b5e89ed5 | 250 | |
1da177e4 LT |
251 | return 0; |
252 | } | |
253 | ||
c153f45f | 254 | int radeon_mem_free(struct drm_device *dev, void *data, struct drm_file *file_priv) |
1da177e4 | 255 | { |
1da177e4 | 256 | drm_radeon_private_t *dev_priv = dev->dev_private; |
c153f45f | 257 | drm_radeon_mem_free_t *memfree = data; |
1da177e4 LT |
258 | struct mem_block *block, **heap; |
259 | ||
b5e89ed5 | 260 | if (!dev_priv) { |
3e684eae | 261 | DRM_ERROR("called with no initialization\n"); |
20caafa6 | 262 | return -EINVAL; |
1da177e4 LT |
263 | } |
264 | ||
c153f45f | 265 | heap = get_heap(dev_priv, memfree->region); |
1da177e4 | 266 | if (!heap || !*heap) |
20caafa6 | 267 | return -EFAULT; |
b5e89ed5 | 268 | |
c153f45f | 269 | block = find_block(*heap, memfree->region_offset); |
1da177e4 | 270 | if (!block) |
20caafa6 | 271 | return -EFAULT; |
1da177e4 | 272 | |
6c340eac | 273 | if (block->file_priv != file_priv) |
20caafa6 | 274 | return -EPERM; |
1da177e4 | 275 | |
b5e89ed5 | 276 | free_block(block); |
1da177e4 LT |
277 | return 0; |
278 | } | |
279 | ||
c153f45f | 280 | int radeon_mem_init_heap(struct drm_device *dev, void *data, struct drm_file *file_priv) |
1da177e4 | 281 | { |
1da177e4 | 282 | drm_radeon_private_t *dev_priv = dev->dev_private; |
c153f45f | 283 | drm_radeon_mem_init_heap_t *initheap = data; |
1da177e4 LT |
284 | struct mem_block **heap; |
285 | ||
b5e89ed5 | 286 | if (!dev_priv) { |
3e684eae | 287 | DRM_ERROR("called with no initialization\n"); |
20caafa6 | 288 | return -EINVAL; |
1da177e4 LT |
289 | } |
290 | ||
c153f45f | 291 | heap = get_heap(dev_priv, initheap->region); |
b5e89ed5 | 292 | if (!heap) |
20caafa6 | 293 | return -EFAULT; |
b5e89ed5 | 294 | |
1da177e4 LT |
295 | if (*heap) { |
296 | DRM_ERROR("heap already initialized?"); | |
20caafa6 | 297 | return -EFAULT; |
1da177e4 | 298 | } |
1da177e4 | 299 | |
c153f45f | 300 | return init_heap(heap, initheap->start, initheap->size); |
b5e89ed5 | 301 | } |