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, int in_use)
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;
39 shift = dev_priv->tex_lru_log_granularity;
40 nr = I915_NR_TEX_REGIONS;
42 start = p->start >> shift;
43 end = (p->start + p->size - 1) >> shift;
45 age = ++sarea_priv->texAge;
46 list = sarea_priv->texList;
48 /* Mark the regions with the new flag and update their age. Move
49 * them to head of list to preserve LRU semantics.
51 for (i = start; i <= end; i++) {
52 list[i].in_use = in_use;
55 /* remove_from_list(i)
57 list[(unsigned)list[i].next].prev = list[i].prev;
58 list[(unsigned)list[i].prev].next = list[i].next;
60 /* insert_at_head(list, i)
63 list[i].next = list[nr].next;
64 list[(unsigned)list[nr].next].prev = i;
69 /* Very simple allocator for agp memory, working on a static range
70 * already mapped into each client's address space.
73 static struct mem_block *split_block(struct mem_block *p, int start, int size,
76 /* Maybe cut off the start of an existing block */
77 if (start > p->start) {
78 struct mem_block *newblock = DRM_MALLOC(sizeof(*newblock));
81 newblock->start = start;
82 newblock->size = p->size - (start - p->start);
83 newblock->filp = NULL;
84 newblock->next = p->next;
86 p->next->prev = newblock;
88 p->size -= newblock->size;
92 /* Maybe cut off the end of an existing block */
94 struct mem_block *newblock = DRM_MALLOC(sizeof(*newblock));
97 newblock->start = start + size;
98 newblock->size = p->size - size;
99 newblock->filp = NULL;
100 newblock->next = p->next;
102 p->next->prev = newblock;
108 /* Our block is in the middle */
113 static struct mem_block *alloc_block(struct mem_block *heap, int size,
114 int align2, DRMFILE filp)
117 int mask = (1 << align2) - 1;
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);
128 static struct mem_block *find_block(struct mem_block *heap, int start)
132 for (p = heap->next; p != heap; p = p->next)
133 if (p->start == start)
139 static void free_block(struct mem_block *p)
143 /* Assumes a single contiguous range. Needs a special filp in
144 * 'heap' to stop it being subsumed.
146 if (p->next->filp == NULL) {
147 struct mem_block *q = p->next;
151 DRM_FREE(q, sizeof(*q));
154 if (p->prev->filp == NULL) {
155 struct mem_block *q = p->prev;
159 DRM_FREE(p, sizeof(*q));
163 /* Initialize. How to check for an uninitialized heap?
165 static int init_heap(struct mem_block **heap, int start, int size)
167 struct mem_block *blocks = DRM_MALLOC(sizeof(*blocks));
172 *heap = DRM_MALLOC(sizeof(**heap));
174 DRM_FREE(blocks, sizeof(*blocks));
178 blocks->start = start;
181 blocks->next = blocks->prev = *heap;
183 memset(*heap, 0, sizeof(**heap));
184 (*heap)->filp = (DRMFILE) - 1;
185 (*heap)->next = (*heap)->prev = blocks;
189 /* Free all blocks associated with the releasing file.
191 void i915_mem_release(drm_device_t * dev, DRMFILE filp, struct mem_block *heap)
195 if (!heap || !heap->next)
198 for (p = heap->next; p != heap; p = p->next) {
199 if (p->filp == filp) {
201 mark_block(dev, p, 0);
205 /* Assumes a single contiguous range. Needs a special filp in
206 * 'heap' to stop it being subsumed.
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;
214 DRM_FREE(q, sizeof(*q));
221 void i915_mem_takedown(struct mem_block **heap)
228 for (p = (*heap)->next; p != *heap;) {
229 struct mem_block *q = p;
231 DRM_FREE(q, sizeof(*q));
234 DRM_FREE(*heap, sizeof(**heap));
238 static struct mem_block **get_heap(drm_i915_private_t * dev_priv, int region)
241 case I915_MEM_REGION_AGP:
242 return &dev_priv->agp_heap;
250 int i915_mem_alloc(DRM_IOCTL_ARGS)
253 drm_i915_private_t *dev_priv = dev->dev_private;
254 drm_i915_mem_alloc_t alloc;
255 struct mem_block *block, **heap;
258 DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
259 return DRM_ERR(EINVAL);
262 DRM_COPY_FROM_USER_IOCTL(alloc, (drm_i915_mem_alloc_t __user *) data,
265 heap = get_heap(dev_priv, alloc.region);
267 return DRM_ERR(EFAULT);
269 /* Make things easier on ourselves: all allocations at least
272 if (alloc.alignment < 12)
273 alloc.alignment = 12;
275 block = alloc_block(*heap, alloc.size, alloc.alignment, filp);
278 return DRM_ERR(ENOMEM);
280 mark_block(dev, block, 1);
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);
290 int i915_mem_free(DRM_IOCTL_ARGS)
293 drm_i915_private_t *dev_priv = dev->dev_private;
294 drm_i915_mem_free_t memfree;
295 struct mem_block *block, **heap;
298 DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
299 return DRM_ERR(EINVAL);
302 DRM_COPY_FROM_USER_IOCTL(memfree, (drm_i915_mem_free_t __user *) data,
305 heap = get_heap(dev_priv, memfree.region);
307 return DRM_ERR(EFAULT);
309 block = find_block(*heap, memfree.region_offset);
311 return DRM_ERR(EFAULT);
313 if (block->filp != filp)
314 return DRM_ERR(EPERM);
316 mark_block(dev, block, 0);
321 int i915_mem_init_heap(DRM_IOCTL_ARGS)
324 drm_i915_private_t *dev_priv = dev->dev_private;
325 drm_i915_mem_init_heap_t initheap;
326 struct mem_block **heap;
329 DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
330 return DRM_ERR(EINVAL);
333 DRM_COPY_FROM_USER_IOCTL(initheap,
334 (drm_i915_mem_init_heap_t __user *) data,
337 heap = get_heap(dev_priv, initheap.region);
339 return DRM_ERR(EFAULT);
342 DRM_ERROR("heap already initialized?");
343 return DRM_ERR(EFAULT);
346 return init_heap(heap, initheap.start, initheap.size);