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