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