drm: use kernel macros
[linux-2.6-block.git] / drivers / char / drm / i915_mem.c
CommitLineData
1da177e4
LT
1/* i915_mem.c -- Simple agp/fb memory manager for i915 -*- linux-c -*-
2 */
3/**************************************************************************
bc54fd1a 4 *
1da177e4
LT
5 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
6 * All Rights Reserved.
bc54fd1a
DA
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the
10 * "Software"), to deal in the Software without restriction, including
11 * without limitation the rights to use, copy, modify, merge, publish,
12 * distribute, sub license, and/or sell copies of the Software, and to
13 * permit persons to whom the Software is furnished to do so, subject to
14 * the following conditions:
15 *
16 * The above copyright notice and this permission notice (including the
17 * next paragraph) shall be included in all copies or substantial portions
18 * of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
23 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
24 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 *
1da177e4
LT
28 **************************************************************************/
29
30#include "drmP.h"
31#include "drm.h"
32#include "i915_drm.h"
33#include "i915_drv.h"
34
35/* This memory manager is integrated into the global/local lru
36 * mechanisms used by the clients. Specifically, it operates by
37 * setting the 'in_use' fields of the global LRU to indicate whether
38 * this region is privately allocated to a client.
39 *
40 * This does require the client to actually respect that field.
41 *
42 * Currently no effort is made to allocate 'private' memory in any
43 * clever way - the LRU information isn't used to determine which
44 * block to allocate, and the ring is drained prior to allocations --
45 * in other words allocation is expensive.
46 */
47static void mark_block(drm_device_t * dev, struct mem_block *p, int in_use)
48{
49 drm_i915_private_t *dev_priv = dev->dev_private;
50 drm_i915_sarea_t *sarea_priv = dev_priv->sarea_priv;
51 drm_tex_region_t *list;
52 unsigned shift, nr;
53 unsigned start;
54 unsigned end;
55 unsigned i;
56 int age;
57
58 shift = dev_priv->tex_lru_log_granularity;
59 nr = I915_NR_TEX_REGIONS;
60
61 start = p->start >> shift;
62 end = (p->start + p->size - 1) >> shift;
63
64 age = ++sarea_priv->texAge;
65 list = sarea_priv->texList;
66
67 /* Mark the regions with the new flag and update their age. Move
68 * them to head of list to preserve LRU semantics.
69 */
70 for (i = start; i <= end; i++) {
71 list[i].in_use = in_use;
72 list[i].age = age;
73
74 /* remove_from_list(i)
75 */
76 list[(unsigned)list[i].next].prev = list[i].prev;
77 list[(unsigned)list[i].prev].next = list[i].next;
78
79 /* insert_at_head(list, i)
80 */
81 list[i].prev = nr;
82 list[i].next = list[nr].next;
83 list[(unsigned)list[nr].next].prev = i;
84 list[nr].next = i;
85 }
86}
87
88/* Very simple allocator for agp memory, working on a static range
89 * already mapped into each client's address space.
90 */
91
92static struct mem_block *split_block(struct mem_block *p, int start, int size,
93 DRMFILE filp)
94{
95 /* Maybe cut off the start of an existing block */
96 if (start > p->start) {
97 struct mem_block *newblock = drm_alloc(sizeof(*newblock), DRM_MEM_BUFLISTS);
98 if (!newblock)
99 goto out;
100 newblock->start = start;
101 newblock->size = p->size - (start - p->start);
102 newblock->filp = NULL;
103 newblock->next = p->next;
104 newblock->prev = p;
105 p->next->prev = newblock;
106 p->next = newblock;
107 p->size -= newblock->size;
108 p = newblock;
109 }
110
111 /* Maybe cut off the end of an existing block */
112 if (size < p->size) {
113 struct mem_block *newblock = drm_alloc(sizeof(*newblock), DRM_MEM_BUFLISTS);
114 if (!newblock)
115 goto out;
116 newblock->start = start + size;
117 newblock->size = p->size - size;
118 newblock->filp = NULL;
119 newblock->next = p->next;
120 newblock->prev = p;
121 p->next->prev = newblock;
122 p->next = newblock;
123 p->size = size;
124 }
125
126 out:
127 /* Our block is in the middle */
128 p->filp = filp;
129 return p;
130}
131
132static struct mem_block *alloc_block(struct mem_block *heap, int size,
133 int align2, DRMFILE filp)
134{
135 struct mem_block *p;
136 int mask = (1 << align2) - 1;
137
138 for (p = heap->next; p != heap; p = p->next) {
139 int start = (p->start + mask) & ~mask;
140 if (p->filp == NULL && start + size <= p->start + p->size)
141 return split_block(p, start, size, filp);
142 }
143
144 return NULL;
145}
146
147static struct mem_block *find_block(struct mem_block *heap, int start)
148{
149 struct mem_block *p;
150
151 for (p = heap->next; p != heap; p = p->next)
152 if (p->start == start)
153 return p;
154
155 return NULL;
156}
157
158static void free_block(struct mem_block *p)
159{
160 p->filp = NULL;
161
162 /* Assumes a single contiguous range. Needs a special filp in
163 * 'heap' to stop it being subsumed.
164 */
165 if (p->next->filp == NULL) {
166 struct mem_block *q = p->next;
167 p->size += q->size;
168 p->next = q->next;
169 p->next->prev = p;
170 drm_free(q, sizeof(*q), DRM_MEM_BUFLISTS);
171 }
172
173 if (p->prev->filp == NULL) {
174 struct mem_block *q = p->prev;
175 q->size += p->size;
176 q->next = p->next;
177 q->next->prev = q;
178 drm_free(p, sizeof(*q), DRM_MEM_BUFLISTS);
179 }
180}
181
182/* Initialize. How to check for an uninitialized heap?
183 */
184static int init_heap(struct mem_block **heap, int start, int size)
185{
186 struct mem_block *blocks = drm_alloc(sizeof(*blocks), DRM_MEM_BUFLISTS);
187
188 if (!blocks)
189 return -ENOMEM;
190
191 *heap = drm_alloc(sizeof(**heap), DRM_MEM_BUFLISTS);
192 if (!*heap) {
193 drm_free(blocks, sizeof(*blocks), DRM_MEM_BUFLISTS);
194 return -ENOMEM;
195 }
196
197 blocks->start = start;
198 blocks->size = size;
199 blocks->filp = NULL;
200 blocks->next = blocks->prev = *heap;
201
202 memset(*heap, 0, sizeof(**heap));
203 (*heap)->filp = (DRMFILE) - 1;
204 (*heap)->next = (*heap)->prev = blocks;
205 return 0;
206}
207
208/* Free all blocks associated with the releasing file.
209 */
210void i915_mem_release(drm_device_t * dev, DRMFILE filp, struct mem_block *heap)
211{
212 struct mem_block *p;
213
214 if (!heap || !heap->next)
215 return;
216
217 for (p = heap->next; p != heap; p = p->next) {
218 if (p->filp == filp) {
219 p->filp = NULL;
220 mark_block(dev, p, 0);
221 }
222 }
223
224 /* Assumes a single contiguous range. Needs a special filp in
225 * 'heap' to stop it being subsumed.
226 */
227 for (p = heap->next; p != heap; p = p->next) {
228 while (p->filp == NULL && p->next->filp == NULL) {
229 struct mem_block *q = p->next;
230 p->size += q->size;
231 p->next = q->next;
232 p->next->prev = p;
233 drm_free(q, sizeof(*q), DRM_MEM_BUFLISTS);
234 }
235 }
236}
237
238/* Shutdown.
239 */
240void i915_mem_takedown(struct mem_block **heap)
241{
242 struct mem_block *p;
243
244 if (!*heap)
245 return;
246
247 for (p = (*heap)->next; p != *heap;) {
248 struct mem_block *q = p;
249 p = p->next;
250 drm_free(q, sizeof(*q), DRM_MEM_BUFLISTS);
251 }
252
253 drm_free(*heap, sizeof(**heap), DRM_MEM_BUFLISTS);
254 *heap = NULL;
255}
256
257static struct mem_block **get_heap(drm_i915_private_t * dev_priv, int region)
258{
259 switch (region) {
260 case I915_MEM_REGION_AGP:
261 return &dev_priv->agp_heap;
262 default:
263 return NULL;
264 }
265}
266
267/* IOCTL HANDLERS */
268
269int i915_mem_alloc(DRM_IOCTL_ARGS)
270{
271 DRM_DEVICE;
272 drm_i915_private_t *dev_priv = dev->dev_private;
273 drm_i915_mem_alloc_t alloc;
274 struct mem_block *block, **heap;
275
276 if (!dev_priv) {
277 DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
278 return DRM_ERR(EINVAL);
279 }
280
281 DRM_COPY_FROM_USER_IOCTL(alloc, (drm_i915_mem_alloc_t __user *) data,
282 sizeof(alloc));
283
284 heap = get_heap(dev_priv, alloc.region);
285 if (!heap || !*heap)
286 return DRM_ERR(EFAULT);
287
288 /* Make things easier on ourselves: all allocations at least
289 * 4k aligned.
290 */
291 if (alloc.alignment < 12)
292 alloc.alignment = 12;
293
294 block = alloc_block(*heap, alloc.size, alloc.alignment, filp);
295
296 if (!block)
297 return DRM_ERR(ENOMEM);
298
299 mark_block(dev, block, 1);
300
301 if (DRM_COPY_TO_USER(alloc.region_offset, &block->start, sizeof(int))) {
302 DRM_ERROR("copy_to_user\n");
303 return DRM_ERR(EFAULT);
304 }
305
306 return 0;
307}
308
309int i915_mem_free(DRM_IOCTL_ARGS)
310{
311 DRM_DEVICE;
312 drm_i915_private_t *dev_priv = dev->dev_private;
313 drm_i915_mem_free_t memfree;
314 struct mem_block *block, **heap;
315
316 if (!dev_priv) {
317 DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
318 return DRM_ERR(EINVAL);
319 }
320
321 DRM_COPY_FROM_USER_IOCTL(memfree, (drm_i915_mem_free_t __user *) data,
322 sizeof(memfree));
323
324 heap = get_heap(dev_priv, memfree.region);
325 if (!heap || !*heap)
326 return DRM_ERR(EFAULT);
327
328 block = find_block(*heap, memfree.region_offset);
329 if (!block)
330 return DRM_ERR(EFAULT);
331
332 if (block->filp != filp)
333 return DRM_ERR(EPERM);
334
335 mark_block(dev, block, 0);
336 free_block(block);
337 return 0;
338}
339
340int i915_mem_init_heap(DRM_IOCTL_ARGS)
341{
342 DRM_DEVICE;
343 drm_i915_private_t *dev_priv = dev->dev_private;
344 drm_i915_mem_init_heap_t initheap;
345 struct mem_block **heap;
346
347 if (!dev_priv) {
348 DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
349 return DRM_ERR(EINVAL);
350 }
351
352 DRM_COPY_FROM_USER_IOCTL(initheap,
353 (drm_i915_mem_init_heap_t __user *) data,
354 sizeof(initheap));
355
356 heap = get_heap(dev_priv, initheap.region);
357 if (!heap)
358 return DRM_ERR(EFAULT);
359
360 if (*heap) {
361 DRM_ERROR("heap already initialized?");
362 return DRM_ERR(EFAULT);
363 }
364
365 return init_heap(heap, initheap.start, initheap.size);
366}