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