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