drm/radeon: Import the Radeon KMS driver from FreeBSD
[dragonfly.git] / sys / dev / drm / radeon / radeon_mem.c
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  * $FreeBSD: head/sys/dev/drm2/radeon/radeon_mem.c 254885 2013-08-25 19:37:15Z dumbbell $
32  */
33
34 #include <drm/drmP.h>
35 #include <uapi_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
42 static 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                                                      DRM_MEM_DRIVER, M_WAITOK);
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                                                      DRM_MEM_DRIVER, M_WAITOK);
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
84 static 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
99 static 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
110 static 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                 drm_free(q, DRM_MEM_DRIVER);
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                 drm_free(p, DRM_MEM_DRIVER);
131         }
132 }
133
134 /* Initialize.  How to check for an uninitialized heap?
135  */
136 static int init_heap(struct mem_block **heap, int start, int size)
137 {
138         struct mem_block *blocks = kmalloc(sizeof(*blocks), DRM_MEM_DRIVER,
139                                            M_WAITOK);
140
141         if (!blocks)
142                 return -ENOMEM;
143
144         *heap = kmalloc(sizeof(**heap), DRM_MEM_DRIVER, M_ZERO | M_WAITOK);
145         if (!*heap) {
146                 drm_free(blocks, DRM_MEM_DRIVER);
147                 return -ENOMEM;
148         }
149
150         blocks->start = start;
151         blocks->size = size;
152         blocks->file_priv = NULL;
153         blocks->next = blocks->prev = *heap;
154
155         (*heap)->file_priv = (struct drm_file *) - 1;
156         (*heap)->next = (*heap)->prev = blocks;
157         return 0;
158 }
159
160 /* Free all blocks associated with the releasing file.
161  */
162 void radeon_mem_release(struct drm_file *file_priv, struct mem_block *heap)
163 {
164         struct mem_block *p;
165
166         if (!heap || !heap->next)
167                 return;
168
169         list_for_each(p, heap) {
170                 if (p->file_priv == file_priv)
171                         p->file_priv = NULL;
172         }
173
174         /* Assumes a single contiguous range.  Needs a special file_priv in
175          * 'heap' to stop it being subsumed.
176          */
177         list_for_each(p, heap) {
178                 while (p->file_priv == NULL && p->next->file_priv == NULL) {
179                         struct mem_block *q = p->next;
180                         p->size += q->size;
181                         p->next = q->next;
182                         p->next->prev = p;
183                         drm_free(q, DRM_MEM_DRIVER);
184                 }
185         }
186 }
187
188 /* Shutdown.
189  */
190 void radeon_mem_takedown(struct mem_block **heap)
191 {
192         struct mem_block *p;
193
194         if (!*heap)
195                 return;
196
197         for (p = (*heap)->next; p != *heap;) {
198                 struct mem_block *q = p;
199                 p = p->next;
200                 drm_free(q, DRM_MEM_DRIVER);
201         }
202
203         drm_free(*heap, DRM_MEM_DRIVER);
204         *heap = NULL;
205 }
206
207 /* IOCTL HANDLERS */
208
209 static struct mem_block **get_heap(drm_radeon_private_t * dev_priv, int region)
210 {
211         switch (region) {
212         case RADEON_MEM_REGION_GART:
213                 return &dev_priv->gart_heap;
214         case RADEON_MEM_REGION_FB:
215                 return &dev_priv->fb_heap;
216         default:
217                 return NULL;
218         }
219 }
220
221 int radeon_mem_alloc(struct drm_device *dev, void *data, struct drm_file *file_priv)
222 {
223         drm_radeon_private_t *dev_priv = dev->dev_private;
224         drm_radeon_mem_alloc_t *alloc = data;
225         struct mem_block *block, **heap;
226
227         if (!dev_priv) {
228                 DRM_ERROR("called with no initialization\n");
229                 return -EINVAL;
230         }
231
232         heap = get_heap(dev_priv, alloc->region);
233         if (!heap || !*heap)
234                 return -EFAULT;
235
236         /* Make things easier on ourselves: all allocations at least
237          * 4k aligned.
238          */
239         if (alloc->alignment < 12)
240                 alloc->alignment = 12;
241
242         block = alloc_block(*heap, alloc->size, alloc->alignment, file_priv);
243
244         if (!block)
245                 return -ENOMEM;
246
247         if (DRM_COPY_TO_USER(alloc->region_offset, &block->start,
248                              sizeof(int))) {
249                 DRM_ERROR("copy_to_user\n");
250                 return -EFAULT;
251         }
252
253         return 0;
254 }
255
256 int radeon_mem_free(struct drm_device *dev, void *data, struct drm_file *file_priv)
257 {
258         drm_radeon_private_t *dev_priv = dev->dev_private;
259         drm_radeon_mem_free_t *memfree = data;
260         struct mem_block *block, **heap;
261
262         if (!dev_priv) {
263                 DRM_ERROR("called with no initialization\n");
264                 return -EINVAL;
265         }
266
267         heap = get_heap(dev_priv, memfree->region);
268         if (!heap || !*heap)
269                 return -EFAULT;
270
271         block = find_block(*heap, memfree->region_offset);
272         if (!block)
273                 return -EFAULT;
274
275         if (block->file_priv != file_priv)
276                 return -EPERM;
277
278         free_block(block);
279         return 0;
280 }
281
282 int radeon_mem_init_heap(struct drm_device *dev, void *data, struct drm_file *file_priv)
283 {
284         drm_radeon_private_t *dev_priv = dev->dev_private;
285         drm_radeon_mem_init_heap_t *initheap = data;
286         struct mem_block **heap;
287
288         if (!dev_priv) {
289                 DRM_ERROR("called with no initialization\n");
290                 return -EINVAL;
291         }
292
293         heap = get_heap(dev_priv, initheap->region);
294         if (!heap)
295                 return -EFAULT;
296
297         if (*heap) {
298                 DRM_ERROR("heap already initialized?");
299                 return -EFAULT;
300         }
301
302         return init_heap(heap, initheap->start, initheap->size);
303 }