radeon_mem.c 7.5 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3
/* radeon_mem.c -- Simple GART/fb memory manager for radeon -*- linux-c -*-
 *
 * Copyright (C) The Weather Channel, Inc.  2002.  All Rights Reserved.
D
Dave Airlie 已提交
4
 *
L
Linus Torvalds 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
 * The Weather Channel (TM) funded Tungsten Graphics to develop the
 * initial release of the Radeon 8500 driver under the XFree86 license.
 * This notice must be preserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *
 * Authors:
 *    Keith Whitwell <keith@tungstengraphics.com>
 */

#include "drmP.h"
#include "drm.h"
#include "radeon_drm.h"
#include "radeon_drv.h"

/* Very simple allocator for GART memory, working on a static range
D
Dave Airlie 已提交
38
 * already mapped into each client's address space.
L
Linus Torvalds 已提交
39 40 41
 */

static struct mem_block *split_block(struct mem_block *p, int start, int size,
D
Dave Airlie 已提交
42
				     DRMFILE filp)
L
Linus Torvalds 已提交
43 44 45
{
	/* Maybe cut off the start of an existing block */
	if (start > p->start) {
D
Dave Airlie 已提交
46 47 48
		struct mem_block *newblock =
		    drm_alloc(sizeof(*newblock), DRM_MEM_BUFS);
		if (!newblock)
L
Linus Torvalds 已提交
49 50 51 52 53 54 55 56 57 58 59
			goto out;
		newblock->start = start;
		newblock->size = p->size - (start - p->start);
		newblock->filp = NULL;
		newblock->next = p->next;
		newblock->prev = p;
		p->next->prev = newblock;
		p->next = newblock;
		p->size -= newblock->size;
		p = newblock;
	}
D
Dave Airlie 已提交
60

L
Linus Torvalds 已提交
61 62
	/* Maybe cut off the end of an existing block */
	if (size < p->size) {
D
Dave Airlie 已提交
63 64
		struct mem_block *newblock =
		    drm_alloc(sizeof(*newblock), DRM_MEM_BUFS);
L
Linus Torvalds 已提交
65 66 67 68 69 70 71 72 73 74 75 76
		if (!newblock)
			goto out;
		newblock->start = start + size;
		newblock->size = p->size - size;
		newblock->filp = NULL;
		newblock->next = p->next;
		newblock->prev = p;
		p->next->prev = newblock;
		p->next = newblock;
		p->size = size;
	}

D
Dave Airlie 已提交
77
      out:
L
Linus Torvalds 已提交
78 79 80 81 82
	/* Our block is in the middle */
	p->filp = filp;
	return p;
}

D
Dave Airlie 已提交
83 84
static struct mem_block *alloc_block(struct mem_block *heap, int size,
				     int align2, DRMFILE filp)
L
Linus Torvalds 已提交
85 86
{
	struct mem_block *p;
D
Dave Airlie 已提交
87
	int mask = (1 << align2) - 1;
L
Linus Torvalds 已提交
88 89 90 91

	list_for_each(p, heap) {
		int start = (p->start + mask) & ~mask;
		if (p->filp == 0 && start + size <= p->start + p->size)
D
Dave Airlie 已提交
92
			return split_block(p, start, size, filp);
L
Linus Torvalds 已提交
93 94 95 96 97
	}

	return NULL;
}

D
Dave Airlie 已提交
98
static struct mem_block *find_block(struct mem_block *heap, int start)
L
Linus Torvalds 已提交
99 100 101 102
{
	struct mem_block *p;

	list_for_each(p, heap)
D
Dave Airlie 已提交
103 104
	    if (p->start == start)
		return p;
L
Linus Torvalds 已提交
105 106 107 108

	return NULL;
}

D
Dave Airlie 已提交
109
static void free_block(struct mem_block *p)
L
Linus Torvalds 已提交
110 111 112 113 114 115 116 117 118 119 120
{
	p->filp = NULL;

	/* Assumes a single contiguous range.  Needs a special filp in
	 * 'heap' to stop it being subsumed.
	 */
	if (p->next->filp == 0) {
		struct mem_block *q = p->next;
		p->size += q->size;
		p->next = q->next;
		p->next->prev = p;
D
Dave Airlie 已提交
121
		drm_free(q, sizeof(*q), DRM_MEM_BUFS);
L
Linus Torvalds 已提交
122 123 124 125 126 127 128
	}

	if (p->prev->filp == 0) {
		struct mem_block *q = p->prev;
		q->size += p->size;
		q->next = p->next;
		q->next->prev = q;
D
Dave Airlie 已提交
129
		drm_free(p, sizeof(*q), DRM_MEM_BUFS);
L
Linus Torvalds 已提交
130 131 132 133 134 135 136
	}
}

/* Initialize.  How to check for an uninitialized heap?
 */
static int init_heap(struct mem_block **heap, int start, int size)
{
D
Dave Airlie 已提交
137
	struct mem_block *blocks = drm_alloc(sizeof(*blocks), DRM_MEM_BUFS);
L
Linus Torvalds 已提交
138

D
Dave Airlie 已提交
139
	if (!blocks)
L
Linus Torvalds 已提交
140
		return DRM_ERR(ENOMEM);
D
Dave Airlie 已提交
141 142

	*heap = drm_alloc(sizeof(**heap), DRM_MEM_BUFS);
L
Linus Torvalds 已提交
143
	if (!*heap) {
D
Dave Airlie 已提交
144
		drm_free(blocks, sizeof(*blocks), DRM_MEM_BUFS);
L
Linus Torvalds 已提交
145 146 147 148 149 150 151 152
		return DRM_ERR(ENOMEM);
	}

	blocks->start = start;
	blocks->size = size;
	blocks->filp = NULL;
	blocks->next = blocks->prev = *heap;

D
Dave Airlie 已提交
153 154
	memset(*heap, 0, sizeof(**heap));
	(*heap)->filp = (DRMFILE) - 1;
L
Linus Torvalds 已提交
155 156 157 158 159 160
	(*heap)->next = (*heap)->prev = blocks;
	return 0;
}

/* Free all blocks associated with the releasing file.
 */
D
Dave Airlie 已提交
161
void radeon_mem_release(DRMFILE filp, struct mem_block *heap)
L
Linus Torvalds 已提交
162 163 164 165 166 167 168
{
	struct mem_block *p;

	if (!heap || !heap->next)
		return;

	list_for_each(p, heap) {
D
Dave Airlie 已提交
169
		if (p->filp == filp)
L
Linus Torvalds 已提交
170 171 172 173 174 175 176 177 178 179 180 181
			p->filp = NULL;
	}

	/* Assumes a single contiguous range.  Needs a special filp in
	 * 'heap' to stop it being subsumed.
	 */
	list_for_each(p, heap) {
		while (p->filp == 0 && p->next->filp == 0) {
			struct mem_block *q = p->next;
			p->size += q->size;
			p->next = q->next;
			p->next->prev = p;
D
Dave Airlie 已提交
182
			drm_free(q, sizeof(*q), DRM_MEM_DRIVER);
L
Linus Torvalds 已提交
183 184 185 186 187 188
		}
	}
}

/* Shutdown.
 */
D
Dave Airlie 已提交
189
void radeon_mem_takedown(struct mem_block **heap)
L
Linus Torvalds 已提交
190 191
{
	struct mem_block *p;
D
Dave Airlie 已提交
192

L
Linus Torvalds 已提交
193 194 195
	if (!*heap)
		return;

D
Dave Airlie 已提交
196
	for (p = (*heap)->next; p != *heap;) {
L
Linus Torvalds 已提交
197 198
		struct mem_block *q = p;
		p = p->next;
D
Dave Airlie 已提交
199
		drm_free(q, sizeof(*q), DRM_MEM_DRIVER);
L
Linus Torvalds 已提交
200 201
	}

D
Dave Airlie 已提交
202
	drm_free(*heap, sizeof(**heap), DRM_MEM_DRIVER);
L
Linus Torvalds 已提交
203 204 205 206 207
	*heap = NULL;
}

/* IOCTL HANDLERS */

D
Dave Airlie 已提交
208
static struct mem_block **get_heap(drm_radeon_private_t * dev_priv, int region)
L
Linus Torvalds 已提交
209
{
D
Dave Airlie 已提交
210
	switch (region) {
L
Linus Torvalds 已提交
211
	case RADEON_MEM_REGION_GART:
D
Dave Airlie 已提交
212
		return &dev_priv->gart_heap;
L
Linus Torvalds 已提交
213 214 215 216 217 218 219
	case RADEON_MEM_REGION_FB:
		return &dev_priv->fb_heap;
	default:
		return NULL;
	}
}

D
Dave Airlie 已提交
220
int radeon_mem_alloc(DRM_IOCTL_ARGS)
L
Linus Torvalds 已提交
221 222 223 224 225 226
{
	DRM_DEVICE;
	drm_radeon_private_t *dev_priv = dev->dev_private;
	drm_radeon_mem_alloc_t alloc;
	struct mem_block *block, **heap;

D
Dave Airlie 已提交
227 228
	if (!dev_priv) {
		DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
L
Linus Torvalds 已提交
229 230 231
		return DRM_ERR(EINVAL);
	}

D
Dave Airlie 已提交
232 233
	DRM_COPY_FROM_USER_IOCTL(alloc, (drm_radeon_mem_alloc_t __user *) data,
				 sizeof(alloc));
L
Linus Torvalds 已提交
234

D
Dave Airlie 已提交
235
	heap = get_heap(dev_priv, alloc.region);
L
Linus Torvalds 已提交
236 237
	if (!heap || !*heap)
		return DRM_ERR(EFAULT);
D
Dave Airlie 已提交
238

L
Linus Torvalds 已提交
239 240 241 242 243 244
	/* Make things easier on ourselves: all allocations at least
	 * 4k aligned.
	 */
	if (alloc.alignment < 12)
		alloc.alignment = 12;

D
Dave Airlie 已提交
245
	block = alloc_block(*heap, alloc.size, alloc.alignment, filp);
L
Linus Torvalds 已提交
246

D
Dave Airlie 已提交
247
	if (!block)
L
Linus Torvalds 已提交
248 249
		return DRM_ERR(ENOMEM);

D
Dave Airlie 已提交
250 251
	if (DRM_COPY_TO_USER(alloc.region_offset, &block->start, sizeof(int))) {
		DRM_ERROR("copy_to_user\n");
L
Linus Torvalds 已提交
252 253
		return DRM_ERR(EFAULT);
	}
D
Dave Airlie 已提交
254

L
Linus Torvalds 已提交
255 256 257
	return 0;
}

D
Dave Airlie 已提交
258
int radeon_mem_free(DRM_IOCTL_ARGS)
L
Linus Torvalds 已提交
259 260 261 262 263 264
{
	DRM_DEVICE;
	drm_radeon_private_t *dev_priv = dev->dev_private;
	drm_radeon_mem_free_t memfree;
	struct mem_block *block, **heap;

D
Dave Airlie 已提交
265 266
	if (!dev_priv) {
		DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
L
Linus Torvalds 已提交
267 268 269
		return DRM_ERR(EINVAL);
	}

D
Dave Airlie 已提交
270 271
	DRM_COPY_FROM_USER_IOCTL(memfree, (drm_radeon_mem_free_t __user *) data,
				 sizeof(memfree));
L
Linus Torvalds 已提交
272

D
Dave Airlie 已提交
273
	heap = get_heap(dev_priv, memfree.region);
L
Linus Torvalds 已提交
274 275
	if (!heap || !*heap)
		return DRM_ERR(EFAULT);
D
Dave Airlie 已提交
276 277

	block = find_block(*heap, memfree.region_offset);
L
Linus Torvalds 已提交
278 279 280 281 282 283
	if (!block)
		return DRM_ERR(EFAULT);

	if (block->filp != filp)
		return DRM_ERR(EPERM);

D
Dave Airlie 已提交
284
	free_block(block);
L
Linus Torvalds 已提交
285 286 287
	return 0;
}

D
Dave Airlie 已提交
288
int radeon_mem_init_heap(DRM_IOCTL_ARGS)
L
Linus Torvalds 已提交
289 290 291 292 293 294
{
	DRM_DEVICE;
	drm_radeon_private_t *dev_priv = dev->dev_private;
	drm_radeon_mem_init_heap_t initheap;
	struct mem_block **heap;

D
Dave Airlie 已提交
295 296
	if (!dev_priv) {
		DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
L
Linus Torvalds 已提交
297 298 299
		return DRM_ERR(EINVAL);
	}

D
Dave Airlie 已提交
300 301 302
	DRM_COPY_FROM_USER_IOCTL(initheap,
				 (drm_radeon_mem_init_heap_t __user *) data,
				 sizeof(initheap));
L
Linus Torvalds 已提交
303

D
Dave Airlie 已提交
304 305
	heap = get_heap(dev_priv, initheap.region);
	if (!heap)
L
Linus Torvalds 已提交
306
		return DRM_ERR(EFAULT);
D
Dave Airlie 已提交
307

L
Linus Torvalds 已提交
308 309 310 311 312
	if (*heap) {
		DRM_ERROR("heap already initialized?");
		return DRM_ERR(EFAULT);
	}

D
Dave Airlie 已提交
313 314
	return init_heap(heap, initheap.start, initheap.size);
}