blob: ed95155c4b1d64136f99fada3e577d0d967fdffa [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
Eric Anholt9a298b22009-03-24 12:23:04 -0700142 *heap = kmalloc(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
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000153 memset(*heap, 0, sizeof(**heap));
Eric Anholt6c340ea2007-08-25 20:23:09 +1000154 (*heap)->file_priv = (struct drm_file *) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 (*heap)->next = (*heap)->prev = blocks;
156 return 0;
157}
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159/* Free all blocks associated with the releasing file.
160 */
Eric Anholt6c340ea2007-08-25 20:23:09 +1000161void radeon_mem_release(struct drm_file *file_priv, struct mem_block *heap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
163 struct mem_block *p;
164
165 if (!heap || !heap->next)
166 return;
167
168 list_for_each(p, heap) {
Eric Anholt6c340ea2007-08-25 20:23:09 +1000169 if (p->file_priv == file_priv)
170 p->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 }
172
Eric Anholt6c340ea2007-08-25 20:23:09 +1000173 /* Assumes a single contiguous range. Needs a special file_priv in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 * 'heap' to stop it being subsumed.
175 */
176 list_for_each(p, heap) {
Harvey Harrison2b462782008-03-28 14:23:06 -0700177 while (p->file_priv == NULL && p->next->file_priv == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 struct mem_block *q = p->next;
179 p->size += q->size;
180 p->next = q->next;
181 p->next->prev = p;
Eric Anholt9a298b22009-03-24 12:23:04 -0700182 kfree(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 }
184 }
185}
186
187/* Shutdown.
188 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000189void radeon_mem_takedown(struct mem_block **heap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
191 struct mem_block *p;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 if (!*heap)
194 return;
195
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000196 for (p = (*heap)->next; p != *heap;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 struct mem_block *q = p;
198 p = p->next;
Eric Anholt9a298b22009-03-24 12:23:04 -0700199 kfree(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 }
201
Eric Anholt9a298b22009-03-24 12:23:04 -0700202 kfree(*heap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 *heap = NULL;
204}
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206/* IOCTL HANDLERS */
207
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000208static struct mem_block **get_heap(drm_radeon_private_t * dev_priv, int region)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000210 switch (region) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 case RADEON_MEM_REGION_GART:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000212 return &dev_priv->gart_heap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 case RADEON_MEM_REGION_FB:
214 return &dev_priv->fb_heap;
215 default:
216 return NULL;
217 }
218}
219
Eric Anholtc153f452007-09-03 12:06:45 +1000220int radeon_mem_alloc(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 drm_radeon_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000223 drm_radeon_mem_alloc_t *alloc = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 struct mem_block *block, **heap;
225
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000226 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000227 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000228 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 }
230
Eric Anholtc153f452007-09-03 12:06:45 +1000231 heap = get_heap(dev_priv, alloc->region);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 if (!heap || !*heap)
Eric Anholt20caafa2007-08-25 19:22:43 +1000233 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 /* Make things easier on ourselves: all allocations at least
236 * 4k aligned.
237 */
Eric Anholtc153f452007-09-03 12:06:45 +1000238 if (alloc->alignment < 12)
239 alloc->alignment = 12;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Eric Anholtc153f452007-09-03 12:06:45 +1000241 block = alloc_block(*heap, alloc->size, alloc->alignment, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000243 if (!block)
Eric Anholt20caafa2007-08-25 19:22:43 +1000244 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Eric Anholtc153f452007-09-03 12:06:45 +1000246 if (DRM_COPY_TO_USER(alloc->region_offset, &block->start,
247 sizeof(int))) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000248 DRM_ERROR("copy_to_user\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000249 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 return 0;
253}
254
Eric Anholtc153f452007-09-03 12:06:45 +1000255int radeon_mem_free(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 drm_radeon_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000258 drm_radeon_mem_free_t *memfree = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 struct mem_block *block, **heap;
260
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000261 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000262 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000263 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 }
265
Eric Anholtc153f452007-09-03 12:06:45 +1000266 heap = get_heap(dev_priv, memfree->region);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 if (!heap || !*heap)
Eric Anholt20caafa2007-08-25 19:22:43 +1000268 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000269
Eric Anholtc153f452007-09-03 12:06:45 +1000270 block = find_block(*heap, memfree->region_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 if (!block)
Eric Anholt20caafa2007-08-25 19:22:43 +1000272 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Eric Anholt6c340ea2007-08-25 20:23:09 +1000274 if (block->file_priv != file_priv)
Eric Anholt20caafa2007-08-25 19:22:43 +1000275 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000277 free_block(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 return 0;
279}
280
Eric Anholtc153f452007-09-03 12:06:45 +1000281int radeon_mem_init_heap(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 drm_radeon_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000284 drm_radeon_mem_init_heap_t *initheap = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 struct mem_block **heap;
286
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000287 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000288 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000289 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 }
291
Eric Anholtc153f452007-09-03 12:06:45 +1000292 heap = get_heap(dev_priv, initheap->region);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000293 if (!heap)
Eric Anholt20caafa2007-08-25 19:22:43 +1000294 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 if (*heap) {
297 DRM_ERROR("heap already initialized?");
Eric Anholt20caafa2007-08-25 19:22:43 +1000298 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000300
Eric Anholtc153f452007-09-03 12:06:45 +1000301 return init_heap(heap, initheap->start, initheap->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302}