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.
34 /* This memory manager is integrated into the global/local lru
35 * mechanisms used by the clients. Specifically, it operates by
36 * setting the 'in_use' fields of the global LRU to indicate whether
37 * this region is privately allocated to a client.
39 * This does require the client to actually respect that field.
41 * Currently no effort is made to allocate 'private' memory in any
42 * clever way - the LRU information isn't used to determine which
43 * block to allocate, and the ring is drained prior to allocations --
44 * in other words allocation is expensive.
46 static void mark_block(struct drm_device * dev, struct mem_block *p, int in_use)
48 drm_i915_private_t *dev_priv = dev->dev_private;
49 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
50 drm_i915_sarea_t *sarea_priv = master_priv->sarea_priv;
51 struct drm_tex_region *list;
58 shift = dev_priv->tex_lru_log_granularity;
59 nr = I915_NR_TEX_REGIONS;
61 start = p->start >> shift;
62 end = (p->start + p->size - 1) >> shift;
64 age = ++sarea_priv->texAge;
65 list = sarea_priv->texList;
67 /* Mark the regions with the new flag and update their age. Move
68 * them to head of list to preserve LRU semantics.
70 for (i = start; i <= end; i++) {
71 list[i].in_use = in_use;
74 /* remove_from_list(i)
76 list[(unsigned)list[i].next].prev = list[i].prev;
77 list[(unsigned)list[i].prev].next = list[i].next;
79 /* insert_at_head(list, i)
82 list[i].next = list[nr].next;
83 list[(unsigned)list[nr].next].prev = i;
88 /* Very simple allocator for agp memory, working on a static range
89 * already mapped into each client's address space.
92 static struct mem_block *split_block(struct mem_block *p, int start, int size,
93 struct drm_file *file_priv)
95 /* Maybe cut off the start of an existing block */
96 if (start > p->start) {
97 struct mem_block *newblock = kmalloc(sizeof(*newblock),
101 newblock->start = start;
102 newblock->size = p->size - (start - p->start);
103 newblock->file_priv = NULL;
104 newblock->next = p->next;
106 p->next->prev = newblock;
108 p->size -= newblock->size;
112 /* Maybe cut off the end of an existing block */
113 if (size < p->size) {
114 struct mem_block *newblock = kmalloc(sizeof(*newblock),
118 newblock->start = start + size;
119 newblock->size = p->size - size;
120 newblock->file_priv = NULL;
121 newblock->next = p->next;
123 p->next->prev = newblock;
129 /* Our block is in the middle */
130 p->file_priv = file_priv;
134 static struct mem_block *alloc_block(struct mem_block *heap, int size,
135 int align2, struct drm_file *file_priv)
138 int mask = (1 << align2) - 1;
140 for (p = heap->next; p != heap; p = p->next) {
141 int start = (p->start + mask) & ~mask;
142 if (p->file_priv == NULL && start + size <= p->start + p->size)
143 return split_block(p, start, size, file_priv);
149 static struct mem_block *find_block(struct mem_block *heap, int start)
153 for (p = heap->next; p != heap; p = p->next)
154 if (p->start == start)
160 static void free_block(struct mem_block *p)
164 /* Assumes a single contiguous range. Needs a special file_priv in
165 * 'heap' to stop it being subsumed.
167 if (p->next->file_priv == NULL) {
168 struct mem_block *q = p->next;
175 if (p->prev->file_priv == NULL) {
176 struct mem_block *q = p->prev;
184 /* Initialize. How to check for an uninitialized heap?
186 static int init_heap(struct mem_block **heap, int start, int size)
188 struct mem_block *blocks = kmalloc(sizeof(*blocks), GFP_KERNEL);
193 *heap = kmalloc(sizeof(**heap), GFP_KERNEL);
199 blocks->start = start;
201 blocks->file_priv = NULL;
202 blocks->next = blocks->prev = *heap;
204 memset(*heap, 0, sizeof(**heap));
205 (*heap)->file_priv = (struct drm_file *) - 1;
206 (*heap)->next = (*heap)->prev = blocks;
210 /* Free all blocks associated with the releasing file.
212 void i915_mem_release(struct drm_device * dev, struct drm_file *file_priv,
213 struct mem_block *heap)
217 if (!heap || !heap->next)
220 for (p = heap->next; p != heap; p = p->next) {
221 if (p->file_priv == file_priv) {
223 mark_block(dev, p, 0);
227 /* Assumes a single contiguous range. Needs a special file_priv in
228 * 'heap' to stop it being subsumed.
230 for (p = heap->next; p != heap; p = p->next) {
231 while (p->file_priv == NULL && p->next->file_priv == NULL) {
232 struct mem_block *q = p->next;
243 void i915_mem_takedown(struct mem_block **heap)
250 for (p = (*heap)->next; p != *heap;) {
251 struct mem_block *q = p;
260 static struct mem_block **get_heap(drm_i915_private_t * dev_priv, int region)
263 case I915_MEM_REGION_AGP:
264 return &dev_priv->agp_heap;
272 int i915_mem_alloc(struct drm_device *dev, void *data,
273 struct drm_file *file_priv)
275 drm_i915_private_t *dev_priv = dev->dev_private;
276 drm_i915_mem_alloc_t *alloc = data;
277 struct mem_block *block, **heap;
280 DRM_ERROR("called with no initialization\n");
284 heap = get_heap(dev_priv, alloc->region);
288 /* Make things easier on ourselves: all allocations at least
291 if (alloc->alignment < 12)
292 alloc->alignment = 12;
294 block = alloc_block(*heap, alloc->size, alloc->alignment, file_priv);
299 mark_block(dev, block, 1);
301 if (DRM_COPY_TO_USER(alloc->region_offset, &block->start,
303 DRM_ERROR("copy_to_user\n");
310 int i915_mem_free(struct drm_device *dev, void *data,
311 struct drm_file *file_priv)
313 drm_i915_private_t *dev_priv = dev->dev_private;
314 drm_i915_mem_free_t *memfree = data;
315 struct mem_block *block, **heap;
318 DRM_ERROR("called with no initialization\n");
322 heap = get_heap(dev_priv, memfree->region);
326 block = find_block(*heap, memfree->region_offset);
330 if (block->file_priv != file_priv)
333 mark_block(dev, block, 0);
338 int i915_mem_init_heap(struct drm_device *dev, void *data,
339 struct drm_file *file_priv)
341 drm_i915_private_t *dev_priv = dev->dev_private;
342 drm_i915_mem_init_heap_t *initheap = data;
343 struct mem_block **heap;
346 DRM_ERROR("called with no initialization\n");
350 heap = get_heap(dev_priv, initheap->region);
355 DRM_ERROR("heap already initialized?");
359 return init_heap(heap, initheap->start, initheap->size);
362 int i915_mem_destroy_heap( struct drm_device *dev, void *data,
363 struct drm_file *file_priv )
365 drm_i915_private_t *dev_priv = dev->dev_private;
366 drm_i915_mem_destroy_heap_t *destroyheap = data;
367 struct mem_block **heap;
370 DRM_ERROR( "called with no initialization\n" );
374 heap = get_heap( dev_priv, destroyheap->region );
376 DRM_ERROR("get_heap failed");
381 DRM_ERROR("heap not initialized?");
385 i915_mem_takedown( heap );