blob: 146d253f1131a4780e889893b3e566336548b3f7 [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>
Christian König14adc892013-01-21 13:58:46 +010030 *
31 * ------------------------ This file is DEPRECATED! -------------------------
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 */
33
David Howells760285e2012-10-02 18:01:07 +010034#include <drm/drmP.h>
35#include <drm/radeon_drm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "radeon_drv.h"
37
38/* Very simple allocator for GART memory, working on a static range
Dave Airlieb5e89ed2005-09-25 14:28:13 +100039 * already mapped into each client's address space.
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 */
41
42static struct mem_block *split_block(struct mem_block *p, int start, int size,
Eric Anholt6c340ea2007-08-25 20:23:09 +100043 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
45 /* Maybe cut off the start of an existing block */
46 if (start > p->start) {
Eric Anholt9a298b22009-03-24 12:23:04 -070047 struct mem_block *newblock = kmalloc(sizeof(*newblock),
48 GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +100049 if (!newblock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 goto out;
51 newblock->start = start;
52 newblock->size = p->size - (start - p->start);
Eric Anholt6c340ea2007-08-25 20:23:09 +100053 newblock->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 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 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +100061
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 /* Maybe cut off the end of an existing block */
63 if (size < p->size) {
Eric Anholt9a298b22009-03-24 12:23:04 -070064 struct mem_block *newblock = kmalloc(sizeof(*newblock),
65 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 if (!newblock)
67 goto out;
68 newblock->start = start + size;
69 newblock->size = p->size - size;
Eric Anholt6c340ea2007-08-25 20:23:09 +100070 newblock->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 newblock->next = p->next;
72 newblock->prev = p;
73 p->next->prev = newblock;
74 p->next = newblock;
75 p->size = size;
76 }
77
Dave Airlieb5e89ed2005-09-25 14:28:13 +100078 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 /* Our block is in the middle */
Eric Anholt6c340ea2007-08-25 20:23:09 +100080 p->file_priv = file_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 return p;
82}
83
Dave Airlieb5e89ed2005-09-25 14:28:13 +100084static struct mem_block *alloc_block(struct mem_block *heap, int size,
Eric Anholt6c340ea2007-08-25 20:23:09 +100085 int align2, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
87 struct mem_block *p;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100088 int mask = (1 << align2) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90 list_for_each(p, heap) {
91 int start = (p->start + mask) & ~mask;
Harvey Harrison2b462782008-03-28 14:23:06 -070092 if (p->file_priv == NULL && start + size <= p->start + p->size)
Eric Anholt6c340ea2007-08-25 20:23:09 +100093 return split_block(p, start, size, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 }
95
96 return NULL;
97}
98
Dave Airlieb5e89ed2005-09-25 14:28:13 +100099static struct mem_block *find_block(struct mem_block *heap, int start)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
101 struct mem_block *p;
102
103 list_for_each(p, heap)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000104 if (p->start == start)
105 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107 return NULL;
108}
109
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000110static void free_block(struct mem_block *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
Eric Anholt6c340ea2007-08-25 20:23:09 +1000112 p->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Eric Anholt6c340ea2007-08-25 20:23:09 +1000114 /* Assumes a single contiguous range. Needs a special file_priv in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 * 'heap' to stop it being subsumed.
116 */
Harvey Harrison2b462782008-03-28 14:23:06 -0700117 if (p->next->file_priv == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 struct mem_block *q = p->next;
119 p->size += q->size;
120 p->next = q->next;
121 p->next->prev = p;
Eric Anholt9a298b22009-03-24 12:23:04 -0700122 kfree(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 }
124
Harvey Harrison2b462782008-03-28 14:23:06 -0700125 if (p->prev->file_priv == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 struct mem_block *q = p->prev;
127 q->size += p->size;
128 q->next = p->next;
129 q->next->prev = q;
Eric Anholt9a298b22009-03-24 12:23:04 -0700130 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 }
132}
133
134/* Initialize. How to check for an uninitialized heap?
135 */
136static int init_heap(struct mem_block **heap, int start, int size)
137{
Eric Anholt9a298b22009-03-24 12:23:04 -0700138 struct mem_block *blocks = kmalloc(sizeof(*blocks), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000140 if (!blocks)
Eric Anholt20caafa2007-08-25 19:22:43 +1000141 return -ENOMEM;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000142
Rakib Mullickf35119d2011-07-25 17:12:56 -0700143 *heap = kzalloc(sizeof(**heap), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 if (!*heap) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700145 kfree(blocks);
Eric Anholt20caafa2007-08-25 19:22:43 +1000146 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 }
148
149 blocks->start = start;
150 blocks->size = size;
Eric Anholt6c340ea2007-08-25 20:23:09 +1000151 blocks->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 blocks->next = blocks->prev = *heap;
153
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
Daniel Vetter1d6ac182013-12-11 11:34:44 +0100246 if (copy_to_user(alloc->region_offset, &block->start,
Eric Anholtc153f452007-09-03 12:06:45 +1000247 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}