1 /* i915_mem.c -- Simple agp/fb memory manager for i915 -*- linux-c -*-
5 /**************************************************************************
7 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
10 **************************************************************************/
17 /* This memory manager is integrated into the global/local lru
18 * mechanisms used by the clients. Specifically, it operates by
19 * setting the 'in_use' fields of the global LRU to indicate whether
20 * this region is privately allocated to a client.
22 * This does require the client to actually respect that field.
24 * Currently no effort is made to allocate 'private' memory in any
25 * clever way - the LRU information isn't used to determine which
26 * block to allocate, and the ring is drained prior to allocations --
27 * in other words allocation is expensive.
29 static void mark_block(drm_device_t * dev, struct mem_block *p, int in_use)
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;
70 /* Very simple allocator for agp memory, working on a static range
71 * already mapped into each client's address space.
74 static struct mem_block *split_block(struct mem_block *p, int start, int size,
77 /* Maybe cut off the start of an existing block */
78 if (start > p->start) {
79 struct mem_block *newblock =
80 drm_alloc(sizeof(*newblock), DRM_MEM_BUFLISTS);
83 newblock->start = start;
84 newblock->size = p->size - (start - p->start);
85 newblock->filp = NULL;
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 =
97 drm_alloc(sizeof(*newblock), DRM_MEM_BUFLISTS);
100 newblock->start = start + size;
101 newblock->size = p->size - size;
102 newblock->filp = NULL;
103 newblock->next = p->next;
105 p->next->prev = newblock;
111 /* Our block is in the middle */
116 static struct mem_block *alloc_block(struct mem_block *heap, int size,
117 int align2, DRMFILE filp)
120 int mask = (1 << align2) - 1;
122 for (p = heap->next; p != heap; p = p->next) {
123 int start = (p->start + mask) & ~mask;
124 if (p->filp == NULL && start + size <= p->start + p->size)
125 return split_block(p, start, size, filp);
131 static struct mem_block *find_block(struct mem_block *heap, int start)
135 for (p = heap->next; p != heap; p = p->next)
136 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 == NULL) {
150 struct mem_block *q = p->next;
154 drm_free(q, sizeof(*q), DRM_MEM_BUFLISTS);
157 if (p->prev->filp == NULL) {
158 struct mem_block *q = p->prev;
162 drm_free(p, sizeof(*q), DRM_MEM_BUFLISTS);
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_alloc(sizeof(*blocks), DRM_MEM_BUFLISTS);
175 *heap = drm_alloc(sizeof(**heap), DRM_MEM_BUFLISTS);
177 drm_free(blocks, sizeof(*blocks), DRM_MEM_BUFLISTS);
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;
192 /* Free all blocks associated with the releasing file.
194 void i915_mem_release(drm_device_t * dev, DRMFILE filp, struct mem_block *heap)
198 if (!heap || !heap->next)
201 for (p = heap->next; p != heap; p = p->next) {
202 if (p->filp == filp) {
204 mark_block(dev, p, 0);
208 /* Assumes a single contiguous range. Needs a special filp in
209 * 'heap' to stop it being subsumed.
211 for (p = heap->next; p != heap; p = p->next) {
212 while (p->filp == NULL && p->next->filp == NULL) {
213 struct mem_block *q = p->next;
217 drm_free(q, sizeof(*q), DRM_MEM_BUFLISTS);
224 void i915_mem_takedown(struct mem_block **heap)
231 for (p = (*heap)->next; p != *heap;) {
232 struct mem_block *q = p;
234 drm_free(q, sizeof(*q), DRM_MEM_BUFLISTS);
237 drm_free(*heap, sizeof(**heap), DRM_MEM_BUFLISTS);
241 static struct mem_block **get_heap(drm_i915_private_t * dev_priv, int region)
244 case I915_MEM_REGION_AGP:
245 return &dev_priv->agp_heap;
253 int i915_mem_alloc(DRM_IOCTL_ARGS)
256 drm_i915_private_t *dev_priv = dev->dev_private;
257 drm_i915_mem_alloc_t alloc;
258 struct mem_block *block, **heap;
261 DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
262 return DRM_ERR(EINVAL);
265 DRM_COPY_FROM_USER_IOCTL(alloc, (drm_i915_mem_alloc_t __user *) data,
268 heap = get_heap(dev_priv, alloc.region);
270 return DRM_ERR(EFAULT);
272 /* Make things easier on ourselves: all allocations at least
275 if (alloc.alignment < 12)
276 alloc.alignment = 12;
278 block = alloc_block(*heap, alloc.size, alloc.alignment, filp);
281 return DRM_ERR(ENOMEM);
283 mark_block(dev, block, 1);
285 if (DRM_COPY_TO_USER(alloc.region_offset, &block->start, sizeof(int))) {
286 DRM_ERROR("copy_to_user\n");
287 return DRM_ERR(EFAULT);
293 int i915_mem_free(DRM_IOCTL_ARGS)
296 drm_i915_private_t *dev_priv = dev->dev_private;
297 drm_i915_mem_free_t memfree;
298 struct mem_block *block, **heap;
301 DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
302 return DRM_ERR(EINVAL);
305 DRM_COPY_FROM_USER_IOCTL(memfree, (drm_i915_mem_free_t __user *) data,
308 heap = get_heap(dev_priv, memfree.region);
310 return DRM_ERR(EFAULT);
312 block = find_block(*heap, memfree.region_offset);
314 return DRM_ERR(EFAULT);
316 if (block->filp != filp)
317 return DRM_ERR(EPERM);
319 mark_block(dev, block, 0);
324 int i915_mem_init_heap(DRM_IOCTL_ARGS)
327 drm_i915_private_t *dev_priv = dev->dev_private;
328 drm_i915_mem_init_heap_t initheap;
329 struct mem_block **heap;
332 DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
333 return DRM_ERR(EINVAL);
336 DRM_COPY_FROM_USER_IOCTL(initheap,
337 (drm_i915_mem_init_heap_t __user *) data,
340 heap = get_heap(dev_priv, initheap.region);
342 return DRM_ERR(EFAULT);
345 DRM_ERROR("heap already initialized?");
346 return DRM_ERR(EFAULT);
349 return init_heap(heap, initheap.start, initheap.size);