blob: 64f59a03eb5ae0b60ae2e78eb1cc82db3933ce25 [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "radeon_drm.h"
34#include "radeon_drv.h"
35
36/* Very simple allocator for GART memory, working on a static range
Dave Airlieb5e89ed2005-09-25 14:28:13 +100037 * already mapped into each client's address space.
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 */
39
40static struct mem_block *split_block(struct mem_block *p, int start, int size,
Eric Anholt6c340ea2007-08-25 20:23:09 +100041 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042{
43 /* Maybe cut off the start of an existing block */
44 if (start > p->start) {
Eric Anholt9a298b22009-03-24 12:23:04 -070045 struct mem_block *newblock = kmalloc(sizeof(*newblock),
46 GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +100047 if (!newblock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 goto out;
49 newblock->start = start;
50 newblock->size = p->size - (start - p->start);
Eric Anholt6c340ea2007-08-25 20:23:09 +100051 newblock->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 newblock->next = p->next;
53 newblock->prev = p;
54 p->next->prev = newblock;
55 p->next = newblock;
56 p->size -= newblock->size;
57 p = newblock;
58 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +100059
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 /* Maybe cut off the end of an existing block */
61 if (size < p->size) {
Eric Anholt9a298b22009-03-24 12:23:04 -070062 struct mem_block *newblock = kmalloc(sizeof(*newblock),
63 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 if (!newblock)
65 goto out;
66 newblock->start = start + size;
67 newblock->size = p->size - size;
Eric Anholt6c340ea2007-08-25 20:23:09 +100068 newblock->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 newblock->next = p->next;
70 newblock->prev = p;
71 p->next->prev = newblock;
72 p->next = newblock;
73 p->size = size;
74 }
75
Dave Airlieb5e89ed2005-09-25 14:28:13 +100076 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 /* Our block is in the middle */
Eric Anholt6c340ea2007-08-25 20:23:09 +100078 p->file_priv = file_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 return p;
80}
81
Dave Airlieb5e89ed2005-09-25 14:28:13 +100082static struct mem_block *alloc_block(struct mem_block *heap, int size,
Eric Anholt6c340ea2007-08-25 20:23:09 +100083 int align2, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084{
85 struct mem_block *p;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100086 int mask = (1 << align2) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88 list_for_each(p, heap) {
89 int start = (p->start + mask) & ~mask;
Harvey Harrison2b462782008-03-28 14:23:06 -070090 if (p->file_priv == NULL && start + size <= p->start + p->size)
Eric Anholt6c340ea2007-08-25 20:23:09 +100091 return split_block(p, start, size, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 }
93
94 return NULL;
95}
96
Dave Airlieb5e89ed2005-09-25 14:28:13 +100097static struct mem_block *find_block(struct mem_block *heap, int start)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
99 struct mem_block *p;
100
101 list_for_each(p, heap)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000102 if (p->start == start)
103 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105 return NULL;
106}
107
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000108static void free_block(struct mem_block *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
Eric Anholt6c340ea2007-08-25 20:23:09 +1000110 p->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Eric Anholt6c340ea2007-08-25 20:23:09 +1000112 /* Assumes a single contiguous range. Needs a special file_priv in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 * 'heap' to stop it being subsumed.
114 */
Harvey Harrison2b462782008-03-28 14:23:06 -0700115 if (p->next->file_priv == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 struct mem_block *q = p->next;
117 p->size += q->size;
118 p->next = q->next;
119 p->next->prev = p;
Eric Anholt9a298b22009-03-24 12:23:04 -0700120 kfree(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 }
122
Harvey Harrison2b462782008-03-28 14:23:06 -0700123 if (p->prev->file_priv == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 struct mem_block *q = p->prev;
125 q->size += p->size;
126 q->next = p->next;
127 q->next->prev = q;
Eric Anholt9a298b22009-03-24 12:23:04 -0700128 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 }
130}
131
132/* Initialize. How to check for an uninitialized heap?
133 */
134static int init_heap(struct mem_block **heap, int start, int size)
135{
Eric Anholt9a298b22009-03-24 12:23:04 -0700136 struct mem_block *blocks = kmalloc(sizeof(*blocks), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000138 if (!blocks)
Eric Anholt20caafa2007-08-25 19:22:43 +1000139 return -ENOMEM;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000140
Rakib Mullickf35119d2011-07-25 17:12:56 -0700141 *heap = kzalloc(sizeof(**heap), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 if (!*heap) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700143 kfree(blocks);
Eric Anholt20caafa2007-08-25 19:22:43 +1000144 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 }
146
147 blocks->start = start;
148 blocks->size = size;
Eric Anholt6c340ea2007-08-25 20:23:09 +1000149 blocks->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 blocks->next = blocks->prev = *heap;
151
Eric Anholt6c340ea2007-08-25 20:23:09 +1000152 (*heap)->file_priv = (struct drm_file *) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 (*heap)->next = (*heap)->prev = blocks;
154 return 0;
155}
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157/* Free all blocks associated with the releasing file.
158 */
Eric Anholt6c340ea2007-08-25 20:23:09 +1000159void radeon_mem_release(struct drm_file *file_priv, struct mem_block *heap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
161 struct mem_block *p;
162
163 if (!heap || !heap->next)
164 return;
165
166 list_for_each(p, heap) {
Eric Anholt6c340ea2007-08-25 20:23:09 +1000167 if (p->file_priv == file_priv)
168 p->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 }
170
Eric Anholt6c340ea2007-08-25 20:23:09 +1000171 /* Assumes a single contiguous range. Needs a special file_priv in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 * 'heap' to stop it being subsumed.
173 */
174 list_for_each(p, heap) {
Harvey Harrison2b462782008-03-28 14:23:06 -0700175 while (p->file_priv == NULL && p->next->file_priv == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 struct mem_block *q = p->next;
177 p->size += q->size;
178 p->next = q->next;
179 p->next->prev = p;
Eric Anholt9a298b22009-03-24 12:23:04 -0700180 kfree(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 }
182 }
183}
184
185/* Shutdown.
186 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000187void radeon_mem_takedown(struct mem_block **heap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
189 struct mem_block *p;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 if (!*heap)
192 return;
193
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000194 for (p = (*heap)->next; p != *heap;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 struct mem_block *q = p;
196 p = p->next;
Eric Anholt9a298b22009-03-24 12:23:04 -0700197 kfree(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 }
199
Eric Anholt9a298b22009-03-24 12:23:04 -0700200 kfree(*heap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 *heap = NULL;
202}
203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204/* IOCTL HANDLERS */
205
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000206static struct mem_block **get_heap(drm_radeon_private_t * dev_priv, int region)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000208 switch (region) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 case RADEON_MEM_REGION_GART:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000210 return &dev_priv->gart_heap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 case RADEON_MEM_REGION_FB:
212 return &dev_priv->fb_heap;
213 default:
214 return NULL;
215 }
216}
217
Eric Anholtc153f452007-09-03 12:06:45 +1000218int radeon_mem_alloc(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 drm_radeon_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000221 drm_radeon_mem_alloc_t *alloc = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 struct mem_block *block, **heap;
223
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000224 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000225 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000226 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 }
228
Eric Anholtc153f452007-09-03 12:06:45 +1000229 heap = get_heap(dev_priv, alloc->region);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 if (!heap || !*heap)
Eric Anholt20caafa2007-08-25 19:22:43 +1000231 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 /* Make things easier on ourselves: all allocations at least
234 * 4k aligned.
235 */
Eric Anholtc153f452007-09-03 12:06:45 +1000236 if (alloc->alignment < 12)
237 alloc->alignment = 12;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Eric Anholtc153f452007-09-03 12:06:45 +1000239 block = alloc_block(*heap, alloc->size, alloc->alignment, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000241 if (!block)
Eric Anholt20caafa2007-08-25 19:22:43 +1000242 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Eric Anholtc153f452007-09-03 12:06:45 +1000244 if (DRM_COPY_TO_USER(alloc->region_offset, &block->start,
245 sizeof(int))) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000246 DRM_ERROR("copy_to_user\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000247 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 return 0;
251}
252
Eric Anholtc153f452007-09-03 12:06:45 +1000253int radeon_mem_free(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 drm_radeon_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000256 drm_radeon_mem_free_t *memfree = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 struct mem_block *block, **heap;
258
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000259 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000260 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000261 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 }
263
Eric Anholtc153f452007-09-03 12:06:45 +1000264 heap = get_heap(dev_priv, memfree->region);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 if (!heap || !*heap)
Eric Anholt20caafa2007-08-25 19:22:43 +1000266 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000267
Eric Anholtc153f452007-09-03 12:06:45 +1000268 block = find_block(*heap, memfree->region_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 if (!block)
Eric Anholt20caafa2007-08-25 19:22:43 +1000270 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Eric Anholt6c340ea2007-08-25 20:23:09 +1000272 if (block->file_priv != file_priv)
Eric Anholt20caafa2007-08-25 19:22:43 +1000273 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000275 free_block(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 return 0;
277}
278
Eric Anholtc153f452007-09-03 12:06:45 +1000279int radeon_mem_init_heap(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 drm_radeon_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000282 drm_radeon_mem_init_heap_t *initheap = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 struct mem_block **heap;
284
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000285 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000286 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000287 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 }
289
Eric Anholtc153f452007-09-03 12:06:45 +1000290 heap = get_heap(dev_priv, initheap->region);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000291 if (!heap)
Eric Anholt20caafa2007-08-25 19:22:43 +1000292 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 if (*heap) {
295 DRM_ERROR("heap already initialized?");
Eric Anholt20caafa2007-08-25 19:22:43 +1000296 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000298
Eric Anholtc153f452007-09-03 12:06:45 +1000299 return init_heap(heap, initheap->start, initheap->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300}