1 /* i915_mem.c -- Simple agp/fb memory manager for i915 -*- linux-c -*-
3 /**************************************************************************
5 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
8 **************************************************************************/
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.
21 * This does require the client to actually respect that field.
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.
28 static void mark_block( drm_device_t *dev, struct mem_block *p,
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;
40 shift = dev_priv->tex_lru_log_granularity;
41 nr = I915_NR_TEX_REGIONS;
43 start = p->start >> shift;
44 end = (p->start + p->size - 1) >> shift;
46 age = ++sarea_priv->texAge;
47 list = sarea_priv->texList;
49 /* Mark the regions with the new flag and update their age. Move
50 * them to head of list to preserve LRU semantics.
52 for (i = start ; i <= end ; i++) {
53 list[i].in_use = in_use;
56 /* remove_from_list(i)
58 list[(unsigned)list[i].next].prev = list[i].prev;
59 list[(unsigned)list[i].prev].next = list[i].next;
61 /* insert_at_head(list, i)
64 list[i].next = list[nr].next;
65 list[(unsigned)list[nr].next].prev = i;
71 /* Very simple allocator for agp memory, working on a static range
72 * already mapped into each client's address space.
75 static struct mem_block *split_block(struct mem_block *p, int start, int size,
78 /* Maybe cut off the start of an existing block */
79 if (start > p->start) {
80 struct mem_block *newblock = DRM_MALLOC(sizeof(*newblock));
83 newblock->start = start;
84 newblock->size = p->size - (start - p->start);
86 newblock->next = p->next;
88 p->next->prev = newblock;
90 p->size -= newblock->size;
94 /* Maybe cut off the end of an existing block */
96 struct mem_block *newblock = DRM_MALLOC(sizeof(*newblock));
99 newblock->start = start + size;
100 newblock->size = p->size - size;
102 newblock->next = p->next;
104 p->next->prev = newblock;
110 /* Our block is in the middle */
115 static struct mem_block *alloc_block( struct mem_block *heap, int size,
116 int align2, DRMFILE filp )
119 int mask = (1 << align2)-1;
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 );
130 static struct mem_block *find_block( struct mem_block *heap, int start )
134 for (p = heap->next ; p != heap ; p = p->next)
135 if (p->start == start)
142 static void free_block( struct mem_block *p )
146 /* Assumes a single contiguous range. Needs a special filp in
147 * 'heap' to stop it being subsumed.
149 if (p->next->filp == 0) {
150 struct mem_block *q = p->next;
154 DRM_FREE(q, sizeof(*q));
157 if (p->prev->filp == 0) {
158 struct mem_block *q = p->prev;
162 DRM_FREE(p, sizeof(*q));
166 /* Initialize. How to check for an uninitialized heap?
168 static int init_heap(struct mem_block **heap, int start, int size)
170 struct mem_block *blocks = DRM_MALLOC(sizeof(*blocks));
175 *heap = DRM_MALLOC(sizeof(**heap));
177 DRM_FREE( blocks, sizeof(*blocks) );
181 blocks->start = start;
184 blocks->next = blocks->prev = *heap;
186 memset( *heap, 0, sizeof(**heap) );
187 (*heap)->filp = (DRMFILE) -1;
188 (*heap)->next = (*heap)->prev = blocks;
193 /* Free all blocks associated with the releasing file.
195 void i915_mem_release( drm_device_t *dev,
196 DRMFILE filp, struct mem_block *heap )
200 if (!heap || !heap->next)
203 for (p = heap->next ; p != heap ; p = p->next) {
204 if (p->filp == filp) {
206 mark_block( dev, p, 0 );
210 /* Assumes a single contiguous range. Needs a special filp in
211 * 'heap' to stop it being subsumed.
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;
219 DRM_FREE(q, sizeof(*q));
226 void i915_mem_takedown( struct mem_block **heap )
233 for (p = (*heap)->next ; p != *heap ; ) {
234 struct mem_block *q = p;
236 DRM_FREE(q, sizeof(*q));
239 DRM_FREE( *heap, sizeof(**heap) );
245 static struct mem_block **get_heap( drm_i915_private_t *dev_priv,
249 case I915_MEM_REGION_AGP:
250 return &dev_priv->agp_heap;
259 int i915_mem_alloc( DRM_IOCTL_ARGS )
262 drm_i915_private_t *dev_priv = dev->dev_private;
263 drm_i915_mem_alloc_t alloc;
264 struct mem_block *block, **heap;
267 DRM_ERROR( "%s called with no initialization\n", __FUNCTION__ );
268 return DRM_ERR(EINVAL);
271 DRM_COPY_FROM_USER_IOCTL( alloc, (drm_i915_mem_alloc_t *)data,
274 heap = get_heap( dev_priv, alloc.region );
276 return DRM_ERR(EFAULT);
278 /* Make things easier on ourselves: all allocations at least
281 if (alloc.alignment < 12)
282 alloc.alignment = 12;
284 block = alloc_block( *heap, alloc.size, alloc.alignment,
288 return DRM_ERR(ENOMEM);
290 mark_block( dev, block, 1 );
292 if ( DRM_COPY_TO_USER( alloc.region_offset, &block->start,
294 DRM_ERROR( "copy_to_user\n" );
295 return DRM_ERR(EFAULT);
303 int i915_mem_free( DRM_IOCTL_ARGS )
306 drm_i915_private_t *dev_priv = dev->dev_private;
307 drm_i915_mem_free_t memfree;
308 struct mem_block *block, **heap;
311 DRM_ERROR( "%s called with no initialization\n", __FUNCTION__ );
312 return DRM_ERR(EINVAL);
315 DRM_COPY_FROM_USER_IOCTL( memfree, (drm_i915_mem_free_t *)data,
318 heap = get_heap( dev_priv, memfree.region );
320 return DRM_ERR(EFAULT);
322 block = find_block( *heap, memfree.region_offset );
324 return DRM_ERR(EFAULT);
326 if (block->filp != filp)
327 return DRM_ERR(EPERM);
329 mark_block( dev, block, 0 );
334 int i915_mem_init_heap( DRM_IOCTL_ARGS )
337 drm_i915_private_t *dev_priv = dev->dev_private;
338 drm_i915_mem_init_heap_t initheap;
339 struct mem_block **heap;
342 DRM_ERROR( "%s called with no initialization\n", __FUNCTION__ );
343 return DRM_ERR(EINVAL);
346 DRM_COPY_FROM_USER_IOCTL( initheap, (drm_i915_mem_init_heap_t *)data,
349 heap = get_heap( dev_priv, initheap.region );
351 return DRM_ERR(EFAULT);
354 DRM_ERROR("heap already initialized?");
355 return DRM_ERR(EFAULT);
358 return init_heap( heap, initheap.start, initheap.size );