summaryrefslogtreecommitdiff
path: root/sys/dev/pci/drm/drm_heap.c
blob: ffdf723a5c8fa8bfd153a8e95a49f1c5cfc75cd5 (plain)
1
2
3
4
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/* $OpenBSD: drm_heap.c,v 1.5 2011/06/02 18:22:00 weerd Exp $ */
/*
 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
 * Copyright (C) The Weather Channel, Inc.  2002.  All Rights Reserved.
 *
 * 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"

struct drm_mem *drm_split_block(struct drm_heap *, struct drm_mem *, int,
		    int, struct drm_file *);
struct drm_mem *drm_find_block(struct drm_heap *, int);
void		drm_free_block(struct drm_heap *, struct drm_mem *);
/*
 * Very simple allocator for GART memory, working on a static range
 * already mapped into each client's address space.
 */
struct drm_mem *
drm_split_block(struct drm_heap *heap, struct drm_mem *p, int start,
    int size, struct drm_file *file_priv)
{
	/* Maybe cut off the start of an existing block */
	if (start > p->start) {
		struct drm_mem *newblock = drm_alloc(sizeof(*newblock));
		if (newblock == NULL)
			goto out;
		newblock->start = start;
		newblock->size = p->size - (start - p->start);
		newblock->file_priv = NULL;
		TAILQ_INSERT_AFTER(heap, p, newblock, link);
		p->size -= newblock->size;
		p = newblock;
	}

	/* Maybe cut off the end of an existing block */
	if (size < p->size) {
		struct drm_mem *newblock = drm_alloc(sizeof(*newblock));
		if (newblock == NULL)
			goto out;
		newblock->start = start + size;
		newblock->size = p->size - size;
		newblock->file_priv = NULL;
		TAILQ_INSERT_AFTER(heap, p, newblock, link);
		p->size = size;
	}

      out:
	/* Our block is in the middle */
	p->file_priv = file_priv;
	return p;
}

struct drm_mem *
drm_alloc_block(struct drm_heap *heap, int size, int align2,
    struct drm_file *file_priv)
{
	struct drm_mem *p;
	int mask = (1 << align2) - 1;

	TAILQ_FOREACH(p, heap, link) {
		int start = (p->start + mask) & ~mask;
		if (p->file_priv == NULL && start + size <= p->start + p->size)
			return (drm_split_block(heap, p, start,
			    size, file_priv));
	}

	return NULL;
}

struct drm_mem *
drm_find_block(struct drm_heap *heap, int start)
{
	struct drm_mem *p;

	TAILQ_FOREACH(p, heap, link)
		if (p->start == start)
			return p;

	return NULL;
}

void
drm_free_block(struct drm_heap *heap, struct drm_mem *p)
{
	struct drm_mem	*q;

	p->file_priv = NULL;

	if ((q = TAILQ_NEXT(p, link)) != TAILQ_END(heap) &&
	    q->file_priv == NULL) {
		p->size += q->size;
		TAILQ_REMOVE(heap, q, link);
		drm_free(q);
	}

	if ((q = TAILQ_PREV(p, drm_heap, link)) != TAILQ_END(heap) &&
	    q->file_priv == NULL) {
		q->size += p->size;
		TAILQ_REMOVE(heap, p, link);
		drm_free(p);
	}
}

/*
 * Initialize.
 */
int
drm_init_heap(struct drm_heap *heap, int start, int size)
{
	struct drm_mem *blocks;

	if (!TAILQ_EMPTY(heap))
		return (EBUSY);

	if ((blocks  = drm_alloc(sizeof(*blocks))) == NULL)
		return (ENOMEM);

	blocks->start = start;
	blocks->size = size;
	blocks->file_priv = NULL;
	TAILQ_INSERT_HEAD(heap, blocks, link);

	return (0);
}

/*
 * Free block at offset ``offset'' owned by file_priv.
 */
int
drm_mem_free(struct drm_heap *heap, int offset, struct drm_file *file_priv)
{
	struct drm_mem	*p;

	if ((p = drm_find_block(heap, offset)) == NULL)
		return (EFAULT);
	if (p->file_priv != file_priv)
		return (EPERM);

	drm_free_block(heap, p);
	return (0);
}

/*
 * Free all blocks associated with the releasing file.
 */
void
drm_mem_release(struct drm_heap *heap, struct drm_file *file_priv)
{
	struct drm_mem	*p, *q;

	if (heap == NULL || TAILQ_EMPTY(heap))
		return;

	TAILQ_FOREACH(p, heap, link) {
		if (p->file_priv == file_priv)
			p->file_priv = NULL;
	}

	/* Coalesce the entries.  ugh... */
	for (p = TAILQ_FIRST(heap); p != TAILQ_END(heap); p = q) {
		while (p->file_priv == NULL &&
		    (q = TAILQ_NEXT(p, link)) != TAILQ_END(heap) &&
		    q->file_priv == NULL) {
			p->size += q->size;
			TAILQ_REMOVE(heap, q, link);
			drm_free(q);
		}
		q = TAILQ_NEXT(p, link);
	}
}

/*
 * Shutdown the heap.
 */
void
drm_mem_takedown(struct drm_heap *heap)
{
	struct drm_mem	*p;

	if (heap == NULL)
		return;

	while ((p = TAILQ_FIRST(heap)) != NULL) {
		TAILQ_REMOVE(heap, p, link);
		drm_free(p);
	}
}