blob: 988548efea92acdcace34b6d91c25707650e8da9 [file] [log] [blame]
Dave Airlie94bb5982006-12-19 17:49:08 +11001/* radeon_mem.c -- Simple GART/fb memory manager for radeon -*- linux-c -*- */
2/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Copyright (C) The Weather Channel, Inc. 2002. All Rights Reserved.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10004 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * 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 "drmP.h"
33#include "drm.h"
34#include "radeon_drm.h"
35#include "radeon_drv.h"
36
37/* Very simple allocator for GART memory, working on a static range
Dave Airlieb5e89ed2005-09-25 14:28:13 +100038 * already mapped into each client's address space.
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 */
40
41static struct mem_block *split_block(struct mem_block *p, int start, int size,
Eric Anholt6c340ea2007-08-25 20:23:09 +100042 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
44 /* Maybe cut off the start of an existing block */
45 if (start > p->start) {
Eric Anholt9a298b22009-03-24 12:23:04 -070046 struct mem_block *newblock = kmalloc(sizeof(*newblock),
47 GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +100048 if (!newblock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 goto out;
50 newblock->start = start;
51 newblock->size = p->size - (start - p->start);
Eric Anholt6c340ea2007-08-25 20:23:09 +100052 newblock->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 newblock->next = p->next;
54 newblock->prev = p;
55 p->next->prev = newblock;
56 p->next = newblock;
57 p->size -= newblock->size;
58 p = newblock;
59 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +100060
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 /* Maybe cut off the end of an existing block */
62 if (size < p->size) {
Eric Anholt9a298b22009-03-24 12:23:04 -070063 struct mem_block *newblock = kmalloc(sizeof(*newblock),
64 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 if (!newblock)
66 goto out;
67 newblock->start = start + size;
68 newblock->size = p->size - size;
Eric Anholt6c340ea2007-08-25 20:23:09 +100069 newblock->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 newblock->next = p->next;
71 newblock->prev = p;
72 p->next->prev = newblock;
73 p->next = newblock;
74 p->size = size;
75 }
76
Dave Airlieb5e89ed2005-09-25 14:28:13 +100077 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 /* Our block is in the middle */
Eric Anholt6c340ea2007-08-25 20:23:09 +100079 p->file_priv = file_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 return p;
81}
82
Dave Airlieb5e89ed2005-09-25 14:28:13 +100083static struct mem_block *alloc_block(struct mem_block *heap, int size,
Eric Anholt6c340ea2007-08-25 20:23:09 +100084 int align2, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
86 struct mem_block *p;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100087 int mask = (1 << align2) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89 list_for_each(p, heap) {
90 int start = (p->start + mask) & ~mask;
Harvey Harrison2b462782008-03-28 14:23:06 -070091 if (p->file_priv == NULL && start + size <= p->start + p->size)
Eric Anholt6c340ea2007-08-25 20:23:09 +100092 return split_block(p, start, size, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 }
94
95 return NULL;
96}
97
Dave Airlieb5e89ed2005-09-25 14:28:13 +100098static struct mem_block *find_block(struct mem_block *heap, int start)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
100 struct mem_block *p;
101
102 list_for_each(p, heap)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000103 if (p->start == start)
104 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106 return NULL;
107}
108
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000109static void free_block(struct mem_block *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
Eric Anholt6c340ea2007-08-25 20:23:09 +1000111 p->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Eric Anholt6c340ea2007-08-25 20:23:09 +1000113 /* Assumes a single contiguous range. Needs a special file_priv in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 * 'heap' to stop it being subsumed.
115 */
Harvey Harrison2b462782008-03-28 14:23:06 -0700116 if (p->next->file_priv == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 struct mem_block *q = p->next;
118 p->size += q->size;
119 p->next = q->next;
120 p->next->prev = p;
Eric Anholt9a298b22009-03-24 12:23:04 -0700121 kfree(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 }
123
Harvey Harrison2b462782008-03-28 14:23:06 -0700124 if (p->prev->file_priv == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 struct mem_block *q = p->prev;
126 q->size += p->size;
127 q->next = p->next;
128 q->next->prev = q;
Eric Anholt9a298b22009-03-24 12:23:04 -0700129 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 }
131}
132
133/* Initialize. How to check for an uninitialized heap?
134 */
135static int init_heap(struct mem_block **heap, int start, int size)
136{
Eric Anholt9a298b22009-03-24 12:23:04 -0700137 struct mem_block *blocks = kmalloc(sizeof(*blocks), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000139 if (!blocks)
Eric Anholt20caafa2007-08-25 19:22:43 +1000140 return -ENOMEM;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000141
Rakib Mullickf35119d2011-07-25 17:12:56 -0700142 *heap = kzalloc(sizeof(**heap), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 if (!*heap) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700144 kfree(blocks);
Eric Anholt20caafa2007-08-25 19:22:43 +1000145 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 }
147
148 blocks->start = start;
149 blocks->size = size;
Eric Anholt6c340ea2007-08-25 20:23:09 +1000150 blocks->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 blocks->next = blocks->prev = *heap;
152
Eric Anholt6c340ea2007-08-25 20:23:09 +1000153 (*heap)->file_priv = (struct drm_file *) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 (*heap)->next = (*heap)->prev = blocks;
155 return 0;
156}
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158/* Free all blocks associated with the releasing file.
159 */
Eric Anholt6c340ea2007-08-25 20:23:09 +1000160void radeon_mem_release(struct drm_file *file_priv, struct mem_block *heap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
162 struct mem_block *p;
163
164 if (!heap || !heap->next)
165 return;
166
167 list_for_each(p, heap) {
Eric Anholt6c340ea2007-08-25 20:23:09 +1000168 if (p->file_priv == file_priv)
169 p->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 }
171
Eric Anholt6c340ea2007-08-25 20:23:09 +1000172 /* Assumes a single contiguous range. Needs a special file_priv in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 * 'heap' to stop it being subsumed.
174 */
175 list_for_each(p, heap) {
Harvey Harrison2b462782008-03-28 14:23:06 -0700176 while (p->file_priv == NULL && p->next->file_priv == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 struct mem_block *q = p->next;
178 p->size += q->size;
179 p->next = q->next;
180 p->next->prev = p;
Eric Anholt9a298b22009-03-24 12:23:04 -0700181 kfree(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 }
183 }
184}
185
186/* Shutdown.
187 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000188void radeon_mem_takedown(struct mem_block **heap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
190 struct mem_block *p;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 if (!*heap)
193 return;
194
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000195 for (p = (*heap)->next; p != *heap;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 struct mem_block *q = p;
197 p = p->next;
Eric Anholt9a298b22009-03-24 12:23:04 -0700198 kfree(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 }
200
Eric Anholt9a298b22009-03-24 12:23:04 -0700201 kfree(*heap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 *heap = NULL;
203}
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205/* IOCTL HANDLERS */
206
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000207static struct mem_block **get_heap(drm_radeon_private_t * dev_priv, int region)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000209 switch (region) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 case RADEON_MEM_REGION_GART:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000211 return &dev_priv->gart_heap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 case RADEON_MEM_REGION_FB:
213 return &dev_priv->fb_heap;
214 default:
215 return NULL;
216 }
217}
218
Eric Anholtc153f452007-09-03 12:06:45 +1000219int radeon_mem_alloc(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 drm_radeon_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000222 drm_radeon_mem_alloc_t *alloc = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 struct mem_block *block, **heap;
224
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000225 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000226 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000227 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 }
229
Eric Anholtc153f452007-09-03 12:06:45 +1000230 heap = get_heap(dev_priv, alloc->region);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 if (!heap || !*heap)
Eric Anholt20caafa2007-08-25 19:22:43 +1000232 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 /* Make things easier on ourselves: all allocations at least
235 * 4k aligned.
236 */
Eric Anholtc153f452007-09-03 12:06:45 +1000237 if (alloc->alignment < 12)
238 alloc->alignment = 12;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Eric Anholtc153f452007-09-03 12:06:45 +1000240 block = alloc_block(*heap, alloc->size, alloc->alignment, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000242 if (!block)
Eric Anholt20caafa2007-08-25 19:22:43 +1000243 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Eric Anholtc153f452007-09-03 12:06:45 +1000245 if (DRM_COPY_TO_USER(alloc->region_offset, &block->start,
246 sizeof(int))) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000247 DRM_ERROR("copy_to_user\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000248 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 return 0;
252}
253
Eric Anholtc153f452007-09-03 12:06:45 +1000254int radeon_mem_free(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 drm_radeon_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000257 drm_radeon_mem_free_t *memfree = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 struct mem_block *block, **heap;
259
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000260 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000261 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000262 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 }
264
Eric Anholtc153f452007-09-03 12:06:45 +1000265 heap = get_heap(dev_priv, memfree->region);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 if (!heap || !*heap)
Eric Anholt20caafa2007-08-25 19:22:43 +1000267 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000268
Eric Anholtc153f452007-09-03 12:06:45 +1000269 block = find_block(*heap, memfree->region_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 if (!block)
Eric Anholt20caafa2007-08-25 19:22:43 +1000271 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Eric Anholt6c340ea2007-08-25 20:23:09 +1000273 if (block->file_priv != file_priv)
Eric Anholt20caafa2007-08-25 19:22:43 +1000274 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000276 free_block(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 return 0;
278}
279
Eric Anholtc153f452007-09-03 12:06:45 +1000280int radeon_mem_init_heap(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 drm_radeon_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000283 drm_radeon_mem_init_heap_t *initheap = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 struct mem_block **heap;
285
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000286 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000287 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000288 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 }
290
Eric Anholtc153f452007-09-03 12:06:45 +1000291 heap = get_heap(dev_priv, initheap->region);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000292 if (!heap)
Eric Anholt20caafa2007-08-25 19:22:43 +1000293 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 if (*heap) {
296 DRM_ERROR("heap already initialized?");
Eric Anholt20caafa2007-08-25 19:22:43 +1000297 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000299
Eric Anholtc153f452007-09-03 12:06:45 +1000300 return init_heap(heap, initheap->start, initheap->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301}