1 /* i915_mem.c -- Simple agp/fb memory manager for i915 -*- linux-c -*-
4 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include "dev/drm/drmP.h"
33 #include "dev/drm/drm.h"
34 #include "dev/drm/i915_drm.h"
35 #include "dev/drm/i915_drv.h"
37 /* This memory manager is integrated into the global/local lru
38 * mechanisms used by the clients. Specifically, it operates by
39 * setting the 'in_use' fields of the global LRU to indicate whether
40 * this region is privately allocated to a client.
42 * This does require the client to actually respect that field.
44 * Currently no effort is made to allocate 'private' memory in any
45 * clever way - the LRU information isn't used to determine which
46 * block to allocate, and the ring is drained prior to allocations --
47 * in other words allocation is expensive.
49 static void mark_block(drm_device_t * dev, struct mem_block *p, int in_use)
51 drm_i915_private_t *dev_priv = dev->dev_private;
52 drm_i915_sarea_t *sarea_priv = dev_priv->sarea_priv;
53 drm_tex_region_t *list;
60 shift = dev_priv->tex_lru_log_granularity;
61 nr = I915_NR_TEX_REGIONS;
63 start = p->start >> shift;
64 end = (p->start + p->size - 1) >> shift;
66 age = ++sarea_priv->texAge;
67 list = sarea_priv->texList;
69 /* Mark the regions with the new flag and update their age. Move
70 * them to head of list to preserve LRU semantics.
72 for (i = start; i <= end; i++) {
73 list[i].in_use = in_use;
76 /* remove_from_list(i)
78 list[(unsigned)list[i].next].prev = list[i].prev;
79 list[(unsigned)list[i].prev].next = list[i].next;
81 /* insert_at_head(list, i)
84 list[i].next = list[nr].next;
85 list[(unsigned)list[nr].next].prev = i;
90 /* Very simple allocator for agp memory, working on a static range
91 * already mapped into each client's address space.
94 static struct mem_block *split_block(struct mem_block *p, int start, int size,
97 /* Maybe cut off the start of an existing block */
98 if (start > p->start) {
99 struct mem_block *newblock =
100 drm_alloc(sizeof(*newblock), DRM_MEM_BUFLISTS);
103 newblock->start = start;
104 newblock->size = p->size - (start - p->start);
105 newblock->filp = NULL;
106 newblock->next = p->next;
108 p->next->prev = newblock;
110 p->size -= newblock->size;
114 /* Maybe cut off the end of an existing block */
115 if (size < p->size) {
116 struct mem_block *newblock =
117 drm_alloc(sizeof(*newblock), DRM_MEM_BUFLISTS);
120 newblock->start = start + size;
121 newblock->size = p->size - size;
122 newblock->filp = NULL;
123 newblock->next = p->next;
125 p->next->prev = newblock;
131 /* Our block is in the middle */
136 static struct mem_block *alloc_block(struct mem_block *heap, int size,
137 int align2, DRMFILE filp)
140 int mask = (1 << align2) - 1;
142 for (p = heap->next; p != heap; p = p->next) {
143 int start = (p->start + mask) & ~mask;
144 if (p->filp == NULL && start + size <= p->start + p->size)
145 return split_block(p, start, size, filp);
151 static struct mem_block *find_block(struct mem_block *heap, int start)
155 for (p = heap->next; p != heap; p = p->next)
156 if (p->start == start)
162 static void free_block(struct mem_block *p)
166 /* Assumes a single contiguous range. Needs a special filp in
167 * 'heap' to stop it being subsumed.
169 if (p->next->filp == NULL) {
170 struct mem_block *q = p->next;
174 drm_free(q, sizeof(*q), DRM_MEM_BUFLISTS);
177 if (p->prev->filp == NULL) {
178 struct mem_block *q = p->prev;
182 drm_free(p, sizeof(*q), DRM_MEM_BUFLISTS);
186 /* Initialize. How to check for an uninitialized heap?
188 static int init_heap(struct mem_block **heap, int start, int size)
190 struct mem_block *blocks = drm_alloc(sizeof(*blocks), DRM_MEM_BUFLISTS);
195 *heap = drm_alloc(sizeof(**heap), DRM_MEM_BUFLISTS);
197 drm_free(blocks, sizeof(*blocks), DRM_MEM_BUFLISTS);
201 blocks->start = start;
204 blocks->next = blocks->prev = *heap;
206 memset(*heap, 0, sizeof(**heap));
207 (*heap)->filp = (DRMFILE) - 1;
208 (*heap)->next = (*heap)->prev = blocks;
212 /* Free all blocks associated with the releasing file.
214 void i915_mem_release(drm_device_t * dev, DRMFILE filp, struct mem_block *heap)
218 if (!heap || !heap->next)
221 for (p = heap->next; p != heap; p = p->next) {
222 if (p->filp == filp) {
224 mark_block(dev, p, 0);
228 /* Assumes a single contiguous range. Needs a special filp in
229 * 'heap' to stop it being subsumed.
231 for (p = heap->next; p != heap; p = p->next) {
232 while (p->filp == NULL && p->next->filp == NULL) {
233 struct mem_block *q = p->next;
237 drm_free(q, sizeof(*q), DRM_MEM_BUFLISTS);
244 void i915_mem_takedown(struct mem_block **heap)
251 for (p = (*heap)->next; p != *heap;) {
252 struct mem_block *q = p;
254 drm_free(q, sizeof(*q), DRM_MEM_BUFLISTS);
257 drm_free(*heap, sizeof(**heap), DRM_MEM_BUFLISTS);
261 static struct mem_block **get_heap(drm_i915_private_t * dev_priv, int region)
264 case I915_MEM_REGION_AGP:
265 return &dev_priv->agp_heap;
273 int i915_mem_alloc(DRM_IOCTL_ARGS)
276 drm_i915_private_t *dev_priv = dev->dev_private;
277 drm_i915_mem_alloc_t alloc;
278 struct mem_block *block, **heap;
281 DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
282 return DRM_ERR(EINVAL);
285 DRM_COPY_FROM_USER_IOCTL(alloc, (drm_i915_mem_alloc_t __user *) data,
288 heap = get_heap(dev_priv, alloc.region);
290 return DRM_ERR(EFAULT);
292 /* Make things easier on ourselves: all allocations at least
295 if (alloc.alignment < 12)
296 alloc.alignment = 12;
298 block = alloc_block(*heap, alloc.size, alloc.alignment, filp);
301 return DRM_ERR(ENOMEM);
303 mark_block(dev, block, 1);
305 if (DRM_COPY_TO_USER(alloc.region_offset, &block->start, sizeof(int))) {
306 DRM_ERROR("copy_to_user\n");
307 return DRM_ERR(EFAULT);
313 int i915_mem_free(DRM_IOCTL_ARGS)
316 drm_i915_private_t *dev_priv = dev->dev_private;
317 drm_i915_mem_free_t memfree;
318 struct mem_block *block, **heap;
321 DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
322 return DRM_ERR(EINVAL);
325 DRM_COPY_FROM_USER_IOCTL(memfree, (drm_i915_mem_free_t __user *) data,
328 heap = get_heap(dev_priv, memfree.region);
330 return DRM_ERR(EFAULT);
332 block = find_block(*heap, memfree.region_offset);
334 return DRM_ERR(EFAULT);
336 if (block->filp != filp)
337 return DRM_ERR(EPERM);
339 mark_block(dev, block, 0);
344 int i915_mem_init_heap(DRM_IOCTL_ARGS)
347 drm_i915_private_t *dev_priv = dev->dev_private;
348 drm_i915_mem_init_heap_t initheap;
349 struct mem_block **heap;
352 DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
353 return DRM_ERR(EINVAL);
356 DRM_COPY_FROM_USER_IOCTL(initheap,
357 (drm_i915_mem_init_heap_t __user *) data,
360 heap = get_heap(dev_priv, initheap.region);
362 return DRM_ERR(EFAULT);
365 DRM_ERROR("heap already initialized?");
366 return DRM_ERR(EFAULT);
369 return init_heap(heap, initheap.start, initheap.size);