3 /* i915_mem.c -- Simple agp/fb memory manager for i915 -*- linux-c -*-
6 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the
11 * "Software"), to deal in the Software without restriction, including
12 * without limitation the rights to use, copy, modify, merge, publish,
13 * distribute, sub license, and/or sell copies of the Software, and to
14 * permit persons to whom the Software is furnished to do so, subject to
15 * the following conditions:
17 * The above copyright notice and this permission notice (including the
18 * next paragraph) shall be included in all copies or substantial portions
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
24 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
25 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
26 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
27 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
33 * Use is subject to license terms.
36 #pragma ident "%Z%%M% %I% %E% SMI"
38 /* This memory manager is integrated into the global/local lru
39 * mechanisms used by the clients. Specifically, it operates by
40 * setting the 'in_use' fields of the global LRU to indicate whether
41 * this region is privately allocated to a client.
43 * This does require the client to actually respect that field.
45 * Currently no effort is made to allocate 'private' memory in any
46 * clever way - the LRU information isn't used to determine which
47 * block to allocate, and the ring is drained prior to allocations --
48 * in other words allocation is expensive.
56 void mark_block(drm_device_t
* dev
, struct mem_block
*p
, int in_use
)
58 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
59 drm_i915_sarea_t
*sarea_priv
= dev_priv
->sarea_priv
;
60 drm_tex_region_t
*list
;
67 shift
= dev_priv
->tex_lru_log_granularity
;
68 nr
= I915_NR_TEX_REGIONS
;
70 start
= p
->start
>> shift
;
71 end
= (p
->start
+ p
->size
- 1) >> shift
;
73 age
= ++sarea_priv
->texAge
;
74 list
= sarea_priv
->texList
;
76 /* Mark the regions with the new flag and update their age. Move
77 * them to head of list to preserve LRU semantics.
79 for (i
= start
; i
<= end
; i
++) {
80 list
[i
].in_use
= (unsigned char)in_use
;
83 /* remove_from_list(i)
85 list
[(unsigned)list
[i
].next
].prev
= list
[i
].prev
;
86 list
[(unsigned)list
[i
].prev
].next
= list
[i
].next
;
88 /* insert_at_head(list, i)
90 list
[i
].prev
= (unsigned char)nr
;
91 list
[i
].next
= list
[nr
].next
;
92 list
[(unsigned)list
[nr
].next
].prev
= (unsigned char)i
;
93 list
[nr
].next
= (unsigned char)i
;
97 /* Very simple allocator for agp memory, working on a static range
98 * already mapped into each client's address space.
101 static struct mem_block
*split_block(struct mem_block
*p
, int start
, int size
, drm_file_t
*fpriv
)
103 /* Maybe cut off the start of an existing block */
104 if (start
> p
->start
) {
105 struct mem_block
*newblock
=
106 drm_alloc(sizeof(*newblock
), DRM_MEM_BUFLISTS
);
109 newblock
->start
= start
;
110 newblock
->size
= p
->size
- (start
- p
->start
);
111 newblock
->filp
= NULL
;
112 newblock
->next
= p
->next
;
114 p
->next
->prev
= newblock
;
116 p
->size
-= newblock
->size
;
120 /* Maybe cut off the end of an existing block */
121 if (size
< p
->size
) {
122 struct mem_block
*newblock
=
123 drm_alloc(sizeof(*newblock
), DRM_MEM_BUFLISTS
);
126 newblock
->start
= start
+ size
;
127 newblock
->size
= p
->size
- size
;
128 newblock
->filp
= NULL
;
129 newblock
->next
= p
->next
;
131 p
->next
->prev
= newblock
;
137 /* Our block is in the middle */
142 static struct mem_block
*alloc_block(struct mem_block
*heap
, int size
,
143 int align2
, drm_file_t
*fpriv
)
146 int mask
= (1 << align2
) - 1;
148 for (p
= heap
->next
; p
!= heap
; p
= p
->next
) {
149 int start
= (p
->start
+ mask
) & ~mask
;
150 if (p
->filp
== NULL
&& start
+ size
<= p
->start
+ p
->size
)
151 return split_block(p
, start
, size
, fpriv
);
157 static struct mem_block
*find_block(struct mem_block
*heap
, int start
)
161 for (p
= heap
->next
; p
!= heap
; p
= p
->next
)
162 if (p
->start
== start
)
168 struct mem_block
*find_block_by_proc(struct mem_block
*heap
, drm_file_t
*fpriv
)
172 for (p
= heap
->next
; p
!= heap
; p
= p
->next
)
173 if (p
->filp
== fpriv
)
179 void free_block(struct mem_block
*p
)
183 /* Assumes a single contiguous range. Needs a special filp in
184 * 'heap' to stop it being subsumed.
186 if (p
->next
->filp
== NULL
) {
187 struct mem_block
*q
= p
->next
;
191 drm_free(q
, sizeof(*q
), DRM_MEM_BUFLISTS
);
194 if (p
->prev
->filp
== NULL
) {
195 struct mem_block
*q
= p
->prev
;
199 drm_free(p
, sizeof(*q
), DRM_MEM_BUFLISTS
);
203 /* Initialize. How to check for an uninitialized heap?
205 static int init_heap(struct mem_block
**heap
, int start
, int size
)
207 struct mem_block
*blocks
= drm_alloc(sizeof(*blocks
), DRM_MEM_BUFLISTS
);
212 *heap
= drm_alloc(sizeof(**heap
), DRM_MEM_BUFLISTS
);
214 drm_free(blocks
, sizeof(*blocks
), DRM_MEM_BUFLISTS
);
218 blocks
->start
= start
;
221 blocks
->next
= blocks
->prev
= *heap
;
223 (void) memset(*heap
, 0, sizeof(**heap
));
224 (*heap
)->filp
= (drm_file_t
*) - 1;
225 (*heap
)->next
= (*heap
)->prev
= blocks
;
229 /* Free all blocks associated with the releasing file.
231 void i915_mem_release(drm_device_t
* dev
, drm_file_t
*fpriv
, struct mem_block
*heap
)
235 if (!heap
|| !heap
->next
)
238 for (p
= heap
->next
; p
!= heap
; p
= p
->next
) {
239 if (p
->filp
== fpriv
) {
241 mark_block(dev
, p
, 0);
245 /* Assumes a single contiguous range. Needs a special filp in
246 * 'heap' to stop it being subsumed.
248 for (p
= heap
->next
; p
!= heap
; p
= p
->next
) {
249 while (p
->filp
== NULL
&& p
->next
->filp
== NULL
) {
250 struct mem_block
*q
= p
->next
;
254 drm_free(q
, sizeof(*q
), DRM_MEM_BUFLISTS
);
261 void i915_mem_takedown(struct mem_block
**heap
)
268 for (p
= (*heap
)->next
; p
!= *heap
;) {
269 struct mem_block
*q
= p
;
271 drm_free(q
, sizeof(*q
), DRM_MEM_BUFLISTS
);
274 drm_free(*heap
, sizeof(**heap
), DRM_MEM_BUFLISTS
);
278 struct mem_block
**get_heap(drm_i915_private_t
* dev_priv
, int region
)
281 case I915_MEM_REGION_AGP
:
282 return (&dev_priv
->agp_heap
);
291 int i915_mem_alloc(DRM_IOCTL_ARGS
)
294 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
295 drm_i915_mem_alloc_t alloc
;
296 struct mem_block
*block
, **heap
;
299 DRM_ERROR("%s called with no initialization\n", __FUNCTION__
);
303 if (ddi_model_convert_from(mode
& FMODELS
) == DDI_MODEL_ILP32
) {
304 drm_i915_mem_alloc32_t alloc32
;
306 DRM_COPYFROM_WITH_RETURN(&alloc32
, (void *)data
, sizeof (alloc32
));
307 alloc
.region
= alloc32
.region
;
308 alloc
.alignment
= alloc32
.alignment
;
309 alloc
.size
= alloc32
.size
;
310 alloc
.region_offset
= (int *)(uintptr_t)alloc32
.region_offset
;
312 DRM_COPYFROM_WITH_RETURN(&alloc
, (void *) data
, sizeof(alloc
));
314 heap
= get_heap(dev_priv
, alloc
.region
);
318 /* Make things easier on ourselves: all allocations at least
321 if (alloc
.alignment
< 12)
322 alloc
.alignment
= 12;
324 block
= alloc_block(*heap
, alloc
.size
, alloc
.alignment
, fpriv
);
329 mark_block(dev
, block
, 1);
331 if (DRM_COPY_TO_USER(alloc
.region_offset
, &block
->start
, sizeof(int))) {
332 DRM_ERROR("copy_to_user\n");
340 int i915_mem_free(DRM_IOCTL_ARGS
)
343 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
344 drm_i915_mem_free_t memfree
;
345 struct mem_block
*block
, **heap
;
348 DRM_ERROR("%s called with no initialization\n", __FUNCTION__
);
352 DRM_COPYFROM_WITH_RETURN(&memfree
, (void *)data
, sizeof(memfree
));
354 heap
= get_heap(dev_priv
, memfree
.region
);
358 block
= find_block(*heap
, memfree
.region_offset
);
362 if (block
->filp
!= fpriv
)
365 mark_block(dev
, block
, 0);
371 int i915_mem_init_heap(DRM_IOCTL_ARGS
)
374 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
375 drm_i915_mem_init_heap_t initheap
;
376 struct mem_block
**heap
;
379 DRM_ERROR("%s called with no initialization\n", __FUNCTION__
);
383 DRM_COPYFROM_WITH_RETURN(&initheap
, (void *)data
, sizeof(initheap
));
385 heap
= get_heap(dev_priv
, initheap
.region
);
390 DRM_ERROR("heap already initialized?");
394 return init_heap(heap
, initheap
.start
, initheap
.size
);
398 int i915_mem_destroy_heap(DRM_IOCTL_ARGS
)
401 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
402 drm_i915_mem_destroy_heap_t destroyheap
;
403 struct mem_block
**heap
;
406 DRM_ERROR("%s called with no initialization\n", __FUNCTION__
);
410 DRM_COPYFROM_WITH_RETURN(&destroyheap
, (void *)data
, sizeof(destroyheap
));
412 heap
= get_heap(dev_priv
, destroyheap
.region
);
414 DRM_ERROR("get_heap failed");
419 DRM_ERROR("heap not initialized?");
423 i915_mem_takedown(heap
);