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