1 /**************************************************************************
3 * Copyright 2006 Tungsten Graphics, Inc., Cedar Park, Texas.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
28 /* Originally a fake version of the buffer manager so that we can
29 * prototype the changes in a driver fairly quickly, has been fleshed
30 * out to a fully functional interim solution.
32 * Basically wraps the old style memory management in the new
33 * programming interface, but is more expressive and avoids many of
34 * the bugs in the old texture manager.
38 #include "intel_context.h"
39 #include "intel_ioctl.h"
40 #include "intel_batchbuffer.h"
42 #include "simple_list.h"
50 #define BM_NO_BACKING_STORE 0x2000
51 #define BM_NO_FENCE_SUBDATA 0x4000
54 static int check_fenced( struct intel_context
*intel
);
56 static int nr_attach
= 0;
58 /* Wrapper around mm.c's mem_block, which understands that you must
59 * wait for fences to expire before memory can be freed. This is
60 * specific to our use of memcpy for uploads - an upload that was
61 * processed through the command queue wouldn't need to care about
65 struct block
*next
, *prev
;
66 struct pool
*pool
; /* BM_MEM_AGP */
67 struct mem_block
*mem
; /* BM_MEM_AGP */
69 unsigned referenced
:1;
70 unsigned on_hardware
:1;
74 unsigned fence
; /* BM_MEM_AGP, Split to read_fence, write_fence */
82 unsigned id
; /* debug only */
89 unsigned alignment
:13;
94 void (*invalidate_cb
)( struct intel_context
*, void * );
101 struct buffer
*static_buffer
;
103 struct mem_block
*heap
;
105 struct block lru
; /* only allocated, non-fence-pending blocks here */
109 _glthread_Mutex mutex
; /**< for thread safety */
110 struct pool pool
[BM_POOL_MAX
];
113 unsigned buf_nr
; /* for generating ids */
115 struct block referenced
; /* after bmBufferOffset */
116 struct block on_hardware
; /* after bmValidateBuffers */
117 struct block fenced
; /* after bmFenceBuffers (mi_flush, emit irq, write dword) */
118 /* then to pool->lru or free() */
121 unsigned free_on_hardware
;
124 unsigned need_fence
:1;
127 #define MAXFENCE 0x7fffffff
129 static GLboolean
FENCE_LTE( unsigned a
, unsigned b
)
134 if (a
< b
&& b
- a
< (1<<24))
137 if (a
> b
&& MAXFENCE
- a
+ b
< (1<<24))
143 int bmTestFence( struct intel_context
*intel
, unsigned fence
)
145 /* Slight problem with wrap-around:
147 return fence
== 0 || FENCE_LTE(fence
, intel
->sarea
->last_dispatch
);
151 int dolock = nr_attach > 1; \
152 if (dolock) _glthread_LOCK_MUTEX(bm->mutex)
155 if (dolock) _glthread_UNLOCK_MUTEX(bm->mutex)
159 static GLboolean
alloc_from_pool( struct intel_context
*intel
,
163 struct bufmgr
*bm
= intel
->bm
;
164 struct pool
*pool
= &bm
->pool
[pool_nr
];
165 struct block
*block
= (struct block
*)calloc(sizeof *block
, 1);
166 GLuint sz
, align
= (1<<buf
->alignment
);
171 sz
= (buf
->size
+ align
-1) & ~(align
-1);
173 block
->mem
= mmAllocMem(pool
->heap
,
181 make_empty_list(block
);
183 /* Insert at head or at tail???
185 insert_at_tail(&pool
->lru
, block
);
188 block
->virtual = pool
->virtual + block
->mem
->ofs
;
203 /* Release the card storage associated with buf:
205 static void free_block( struct intel_context
*intel
, struct block
*block
)
207 DBG("free block %p\n", block
);
214 if (block
->referenced
) {
215 _mesa_printf("tried to free block on referenced list\n");
218 else if (block
->on_hardware
) {
220 intel
->bm
->free_on_hardware
+= block
->mem
->size
;
222 else if (block
->fenced
) {
226 DBG(" - free immediately\n");
227 remove_from_list(block
);
229 mmFreeMem(block
->mem
);
235 static void alloc_backing_store( struct intel_context
*intel
, struct buffer
*buf
)
237 assert(!buf
->backing_store
);
238 assert(!(buf
->flags
& (BM_NO_EVICT
|BM_NO_BACKING_STORE
)));
240 buf
->backing_store
= ALIGN_MALLOC(buf
->size
, 64);
243 static void free_backing_store( struct intel_context
*intel
, struct buffer
*buf
)
245 assert(!(buf
->flags
& (BM_NO_EVICT
|BM_NO_BACKING_STORE
)));
247 if (buf
->backing_store
) {
248 ALIGN_FREE(buf
->backing_store
);
249 buf
->backing_store
= NULL
;
258 static void set_dirty( struct intel_context
*intel
,
261 if (buf
->flags
& BM_NO_BACKING_STORE
)
262 buf
->invalidate_cb(intel
, buf
->invalidate_ptr
);
264 assert(!(buf
->flags
& BM_NO_EVICT
));
266 DBG("set_dirty - buf %d\n", buf
->id
);
271 static int evict_lru( struct intel_context
*intel
, GLuint max_fence
)
273 struct bufmgr
*bm
= intel
->bm
;
274 struct block
*block
, *tmp
;
277 DBG("%s\n", __FUNCTION__
);
279 for (i
= 0; i
< bm
->nr_pools
; i
++) {
280 if (!(bm
->pool
[i
].flags
& BM_NO_EVICT
)) {
281 foreach_s(block
, tmp
, &bm
->pool
[i
].lru
) {
284 (block
->buf
->flags
& BM_NO_FENCE_SUBDATA
))
287 if (block
->fence
&& max_fence
&&
288 !FENCE_LTE(block
->fence
, max_fence
))
291 set_dirty(intel
, block
->buf
);
292 block
->buf
->block
= NULL
;
294 free_block(intel
, block
);
305 #define foreach_s_rev(ptr, t, list) \
306 for(ptr=(list)->prev,t=(ptr)->prev; list != ptr; ptr=t, t=(t)->prev)
308 static int evict_mru( struct intel_context
*intel
)
310 struct bufmgr
*bm
= intel
->bm
;
311 struct block
*block
, *tmp
;
314 DBG("%s\n", __FUNCTION__
);
316 for (i
= 0; i
< bm
->nr_pools
; i
++) {
317 if (!(bm
->pool
[i
].flags
& BM_NO_EVICT
)) {
318 foreach_s_rev(block
, tmp
, &bm
->pool
[i
].lru
) {
321 (block
->buf
->flags
& BM_NO_FENCE_SUBDATA
))
324 set_dirty(intel
, block
->buf
);
325 block
->buf
->block
= NULL
;
327 free_block(intel
, block
);
339 static int check_fenced( struct intel_context
*intel
)
341 struct bufmgr
*bm
= intel
->bm
;
342 struct block
*block
, *tmp
;
345 foreach_s(block
, tmp
, &bm
->fenced
) {
346 assert(block
->fenced
);
348 if (bmTestFence(intel
, block
->fence
)) {
353 DBG("delayed free: offset %x sz %x\n", block
->mem
->ofs
, block
->mem
->size
);
354 remove_from_list(block
);
355 mmFreeMem(block
->mem
);
359 DBG("return to lru: offset %x sz %x\n", block
->mem
->ofs
, block
->mem
->size
);
360 move_to_tail(&block
->pool
->lru
, block
);
366 /* Blocks are ordered by fence, so if one fails, all from
367 * here will fail also:
373 /* Also check the referenced list:
375 foreach_s(block
, tmp
, &bm
->referenced
) {
377 bmTestFence(intel
, block
->fence
)) {
383 DBG("%s: %d\n", __FUNCTION__
, ret
);
389 static void fence_blocks( struct intel_context
*intel
,
392 struct bufmgr
*bm
= intel
->bm
;
393 struct block
*block
, *tmp
;
395 foreach_s (block
, tmp
, &bm
->on_hardware
) {
396 DBG("Fence block %p (sz 0x%x buf %p) with fence %d\n", block
,
397 block
->mem
->size
, block
->buf
, fence
);
398 block
->fence
= fence
;
400 block
->on_hardware
= 0;
403 /* Move to tail of pending list here
405 move_to_tail(&bm
->fenced
, block
);
408 /* Also check the referenced list:
410 foreach_s (block
, tmp
, &bm
->referenced
) {
411 if (block
->on_hardware
) {
412 DBG("Fence block %p (sz 0x%x buf %p) with fence %d\n", block
,
413 block
->mem
->size
, block
->buf
, fence
);
415 block
->fence
= fence
;
416 block
->on_hardware
= 0;
422 bm
->last_fence
= fence
;
423 assert(is_empty_list(&bm
->on_hardware
));
429 static GLboolean
alloc_block( struct intel_context
*intel
,
432 struct bufmgr
*bm
= intel
->bm
;
435 DBG("%s 0x%x bytes (%s)\n", __FUNCTION__
, buf
->size
, buf
->name
);
437 for (i
= 0; i
< bm
->nr_pools
; i
++) {
438 if (!(bm
->pool
[i
].flags
& BM_NO_ALLOC
) &&
439 alloc_from_pool(intel
, i
, buf
)) {
441 DBG("%s --> 0x%x (sz %x)\n", __FUNCTION__
,
442 buf
->block
->mem
->ofs
, buf
->block
->mem
->size
);
448 DBG("%s --> fail\n", __FUNCTION__
);
453 static GLboolean
evict_and_alloc_block( struct intel_context
*intel
,
456 struct bufmgr
*bm
= intel
->bm
;
458 assert(buf
->block
== NULL
);
460 /* Put a cap on the amount of free memory we'll allow to accumulate
461 * before emitting a fence.
463 if (bm
->free_on_hardware
> 1 * 1024 * 1024) {
464 DBG("fence for free space: %x\n", bm
->free_on_hardware
);
468 /* Search for already free memory:
470 if (alloc_block(intel
, buf
))
473 /* Look for memory that may have become free:
475 if (check_fenced(intel
) &&
476 alloc_block(intel
, buf
))
479 /* Look for memory blocks not used for >1 frame:
481 while (evict_lru(intel
, intel
->second_last_swap_fence
))
482 if (alloc_block(intel
, buf
))
485 /* If we're not thrashing, allow lru eviction to dig deeper into
486 * recently used textures. We'll probably be thrashing soon:
488 if (!intel
->thrashing
) {
489 while (evict_lru(intel
, 0))
490 if (alloc_block(intel
, buf
))
494 /* Keep thrashing counter alive?
496 if (intel
->thrashing
)
497 intel
->thrashing
= 20;
499 /* Wait on any already pending fences - here we are waiting for any
500 * freed memory that has been submitted to hardware and fenced to
503 while (!is_empty_list(&bm
->fenced
)) {
504 GLuint fence
= bm
->fenced
.next
->fence
;
505 bmFinishFence(intel
, fence
);
507 if (alloc_block(intel
, buf
))
514 if (!is_empty_list(&bm
->on_hardware
)) {
517 if (!is_empty_list(&bm
->fenced
)) {
518 GLuint fence
= bm
->fenced
.next
->fence
;
519 bmFinishFence(intel
, fence
);
522 if (!intel
->thrashing
) {
525 intel
->thrashing
= 20;
527 if (alloc_block(intel
, buf
))
531 while (evict_mru(intel
))
532 if (alloc_block(intel
, buf
))
547 /***********************************************************************
552 /* The initialization functions are skewed in the fake implementation.
553 * This call would be to attach to an existing manager, rather than to
554 * create a local one.
556 struct bufmgr
*bm_fake_intel_Attach( struct intel_context
*intel
)
558 _glthread_DECLARE_STATIC_MUTEX(initMutex
);
559 static struct bufmgr bm
;
561 /* This function needs a mutex of its own...
563 _glthread_LOCK_MUTEX(initMutex
);
565 if (nr_attach
== 0) {
566 _glthread_INIT_MUTEX(bm
.mutex
);
568 make_empty_list(&bm
.referenced
);
569 make_empty_list(&bm
.fenced
);
570 make_empty_list(&bm
.on_hardware
);
575 _glthread_UNLOCK_MUTEX(initMutex
);
582 /* The virtual pointer would go away in a true implementation.
584 int bmInitPool( struct intel_context
*intel
,
585 unsigned long low_offset
,
590 struct bufmgr
*bm
= intel
->bm
;
597 for (i
= 0; i
< bm
->nr_pools
; i
++) {
598 if (bm
->pool
[i
].low_offset
== low_offset
&&
599 bm
->pool
[i
].size
== size
) {
606 if (bm
->nr_pools
>= BM_POOL_MAX
)
611 DBG("bmInitPool %d low_offset %x sz %x\n",
612 i
, low_offset
, size
);
614 bm
->pool
[i
].low_offset
= low_offset
;
615 bm
->pool
[i
].size
= size
;
616 bm
->pool
[i
].heap
= mmInit( low_offset
, size
);
617 bm
->pool
[i
].virtual = low_virtual
- low_offset
;
618 bm
->pool
[i
].flags
= flags
;
620 make_empty_list(&bm
->pool
[i
].lru
);
630 static struct buffer
*do_GenBuffer(struct intel_context
*intel
, const char *name
, int align
)
632 struct bufmgr
*bm
= intel
->bm
;
633 struct buffer
*buf
= calloc(sizeof(*buf
), 1);
635 buf
->id
= ++bm
->buf_nr
;
637 buf
->alignment
= align
? align
: 6;
638 buf
->flags
= BM_MEM_AGP
|BM_MEM_VRAM
|BM_MEM_LOCAL
;
645 void bmGenBuffers(struct intel_context
*intel
,
646 const char *name
, unsigned n
,
647 struct buffer
**buffers
,
650 struct bufmgr
*bm
= intel
->bm
;
655 for (i
= 0; i
< n
; i
++)
656 buffers
[i
] = do_GenBuffer(intel
, name
, align
);
662 void bmDeleteBuffers(struct intel_context
*intel
, unsigned n
, struct buffer
**buffers
)
664 struct bufmgr
*bm
= intel
->bm
;
670 for (i
= 0; i
< n
; i
++) {
671 struct buffer
*buf
= buffers
[i
];
673 if (buf
&& buf
->block
)
674 free_block(intel
, buf
->block
);
686 /* Hook to inform faked buffer manager about fixed-position
687 * front,depth,back buffers. These may move to a fully memory-managed
688 * scheme, or they may continue to be managed as is. It will probably
689 * be useful to pass a fixed offset here one day.
691 struct buffer
*bmGenBufferStatic(struct intel_context
*intel
,
694 struct bufmgr
*bm
= intel
->bm
;
698 assert(bm
->pool
[pool
].flags
& BM_NO_EVICT
);
699 assert(bm
->pool
[pool
].flags
& BM_NO_MOVE
);
701 if (bm
->pool
[pool
].static_buffer
)
702 buf
= bm
->pool
[pool
].static_buffer
;
704 buf
= do_GenBuffer(intel
, "static", 12);
706 bm
->pool
[pool
].static_buffer
= buf
;
709 buf
->size
= bm
->pool
[pool
].size
;
710 buf
->flags
= bm
->pool
[pool
].flags
;
713 if (!alloc_from_pool(intel
, pool
, buf
))
722 static void wait_quiescent(struct intel_context
*intel
,
725 if (block
->on_hardware
) {
726 assert(intel
->bm
->need_fence
);
728 assert(!block
->on_hardware
);
733 bmFinishFence(intel
, block
->fence
);
736 assert(!block
->on_hardware
);
737 assert(!block
->fenced
);
742 /* If buffer size changes, free and reallocate. Otherwise update in
745 void bmBufferData(struct intel_context
*intel
,
751 struct bufmgr
*bm
= intel
->bm
;
755 DBG("bmBufferData %d sz 0x%x data: %p\n", buf
->id
, size
, data
);
757 assert(!buf
->mapped
);
760 struct block
*block
= buf
->block
;
762 /* Optimistic check to see if we can reuse the block -- not
763 * required for correctness:
768 if (block
->on_hardware
||
770 (buf
->size
&& buf
->size
!= size
) ||
773 assert(!block
->referenced
);
775 free_block(intel
, block
);
783 assert (buf
->block
->mem
->size
== size
);
786 if (buf
->flags
& (BM_NO_BACKING_STORE
|BM_NO_EVICT
)) {
788 if (!buf
->block
&& !evict_and_alloc_block(intel
, buf
))
791 wait_quiescent(intel
, buf
->block
);
793 DBG("bmBufferData %d offset 0x%x sz 0x%x\n",
794 buf
->id
, buf
->block
->mem
->ofs
, size
);
796 assert(buf
->block
->virtual == buf
->block
->pool
->virtual + buf
->block
->mem
->ofs
);
798 do_memcpy(buf
->block
->virtual, data
, size
);
803 DBG("%s - set buf %d dirty\n", __FUNCTION__
, buf
->id
);
804 set_dirty(intel
, buf
);
805 free_backing_store(intel
, buf
);
808 alloc_backing_store(intel
, buf
);
809 do_memcpy(buf
->backing_store
, data
, size
);
817 /* Update the buffer in place, in whatever space it is currently resident:
819 void bmBufferSubData(struct intel_context
*intel
,
825 struct bufmgr
*bm
= intel
->bm
;
832 DBG("bmBufferSubdata %d offset 0x%x sz 0x%x\n", buf
->id
, offset
, size
);
834 assert(offset
+size
<= buf
->size
);
836 if (buf
->flags
& (BM_NO_EVICT
|BM_NO_BACKING_STORE
)) {
837 if (!buf
->block
&& !evict_and_alloc_block(intel
, buf
))
840 if (!(buf
->flags
& BM_NO_FENCE_SUBDATA
))
841 wait_quiescent(intel
, buf
->block
);
845 do_memcpy(buf
->block
->virtual + offset
, data
, size
);
848 DBG("%s - set buf %d dirty\n", __FUNCTION__
, buf
->id
);
849 set_dirty(intel
, buf
);
851 if (buf
->backing_store
== NULL
)
852 alloc_backing_store(intel
, buf
);
854 do_memcpy(buf
->backing_store
+ offset
, data
, size
);
862 void bmBufferDataAUB(struct intel_context
*intel
,
868 unsigned aubsubtype
)
870 bmBufferData(intel
, buf
, size
, data
, flags
);
873 /* This only works because in this version of the buffer manager we
874 * allocate all buffers statically in agp space and so can emit the
875 * uploads to the aub file with the correct offsets as they happen.
877 if (data
&& intel
->aub_file
) {
879 if (buf
->block
&& !buf
->dirty
) {
880 intel
->vtbl
.aub_gtt_data(intel
,
881 buf
->block
->mem
->ofs
,
892 void bmBufferSubDataAUB(struct intel_context
*intel
,
898 unsigned aubsubtype
)
900 bmBufferSubData(intel
, buf
, offset
, size
, data
);
903 /* This only works because in this version of the buffer manager we
904 * allocate all buffers statically in agp space and so can emit the
905 * uploads to the aub file with the correct offsets as they happen.
907 if (intel
->aub_file
) {
908 if (buf
->block
&& !buf
->dirty
)
909 intel
->vtbl
.aub_gtt_data(intel
,
910 buf
->block
->mem
->ofs
+ offset
,
911 ((const char *)buf
->block
->virtual) + offset
,
918 void bmUnmapBufferAUB( struct intel_context
*intel
,
921 unsigned aubsubtype
)
923 bmUnmapBuffer(intel
, buf
);
925 if (intel
->aub_file
) {
926 /* Hack - exclude the framebuffer mappings. If you removed
927 * this, you'd get very big aubfiles, but you *would* be able to
928 * see fallback rendering.
930 if (buf
->block
&& !buf
->dirty
&& buf
->block
->pool
== &intel
->bm
->pool
[0]) {
936 unsigned bmBufferOffset(struct intel_context
*intel
,
939 struct bufmgr
*bm
= intel
->bm
;
944 assert(intel
->locked
);
947 !evict_and_alloc_block(intel
, buf
)) {
953 assert(buf
->block
->buf
== buf
);
955 DBG("Add buf %d (block %p, dirty %d) to referenced list\n", buf
->id
, buf
->block
,
958 move_to_tail(&bm
->referenced
, buf
->block
);
959 buf
->block
->referenced
= 1;
961 retval
= buf
->block
->mem
->ofs
;
971 /* Extract data from the buffer:
973 void bmBufferGetSubData(struct intel_context
*intel
,
979 struct bufmgr
*bm
= intel
->bm
;
983 DBG("bmBufferSubdata %d offset 0x%x sz 0x%x\n", buf
->id
, offset
, size
);
985 if (buf
->flags
& (BM_NO_EVICT
|BM_NO_BACKING_STORE
)) {
986 if (buf
->block
&& size
) {
987 wait_quiescent(intel
, buf
->block
);
988 do_memcpy(data
, buf
->block
->virtual + offset
, size
);
992 if (buf
->backing_store
&& size
) {
993 do_memcpy(data
, buf
->backing_store
+ offset
, size
);
1001 /* Return a pointer to whatever space the buffer is currently resident in:
1003 void *bmMapBuffer( struct intel_context
*intel
,
1007 struct bufmgr
*bm
= intel
->bm
;
1012 DBG("bmMapBuffer %d\n", buf
->id
);
1015 _mesa_printf("%s: already mapped\n", __FUNCTION__
);
1018 else if (buf
->flags
& (BM_NO_BACKING_STORE
|BM_NO_EVICT
)) {
1019 if (!buf
->block
&& !evict_and_alloc_block(intel
, buf
)) {
1020 _mesa_printf("%s: alloc failed\n", __FUNCTION__
);
1027 if (!(buf
->flags
& BM_NO_FENCE_SUBDATA
))
1028 wait_quiescent(intel
, buf
->block
);
1031 retval
= buf
->block
->virtual;
1035 DBG("%s - set buf %d dirty\n", __FUNCTION__
, buf
->id
);
1036 set_dirty(intel
, buf
);
1038 if (buf
->backing_store
== 0)
1039 alloc_backing_store(intel
, buf
);
1042 retval
= buf
->backing_store
;
1049 void bmUnmapBuffer( struct intel_context
*intel
, struct buffer
*buf
)
1051 struct bufmgr
*bm
= intel
->bm
;
1055 DBG("bmUnmapBuffer %d\n", buf
->id
);
1064 /* This is the big hack that turns on BM_NO_BACKING_STORE. Basically
1065 * says that an external party will maintain the backing store, eg
1066 * Mesa's local copy of texture data.
1068 void bmBufferSetInvalidateCB(struct intel_context
*intel
,
1070 void (*invalidate_cb
)( struct intel_context
*, void *ptr
),
1072 GLboolean dont_fence_subdata
)
1074 struct bufmgr
*bm
= intel
->bm
;
1078 if (buf
->backing_store
)
1079 free_backing_store(intel
, buf
);
1081 buf
->flags
|= BM_NO_BACKING_STORE
;
1083 if (dont_fence_subdata
)
1084 buf
->flags
|= BM_NO_FENCE_SUBDATA
;
1086 DBG("bmBufferSetInvalidateCB set buf %d dirty\n", buf
->id
);
1088 buf
->invalidate_cb
= invalidate_cb
;
1089 buf
->invalidate_ptr
= ptr
;
1091 /* Note that it is invalid right from the start. Also note
1092 * invalidate_cb is called with the bufmgr locked, so cannot
1093 * itself make bufmgr calls.
1095 invalidate_cb( intel
, ptr
);
1106 /* This is only protected against thread interactions by the DRI lock
1107 * and the policy of ensuring that all dma is flushed prior to
1108 * releasing that lock. Otherwise you might have two threads building
1109 * up a list of buffers to validate at once.
1111 int bmValidateBuffers( struct intel_context
*intel
)
1113 struct bufmgr
*bm
= intel
->bm
;
1118 DBG("%s fail %d\n", __FUNCTION__
, bm
->fail
);
1121 struct block
*block
, *tmp
;
1123 foreach_s(block
, tmp
, &bm
->referenced
) {
1124 struct buffer
*buf
= block
->buf
;
1126 DBG("Validate buf %d / block %p / dirty %d\n", buf
->id
, block
, buf
->dirty
);
1128 /* Upload the buffer contents if necessary:
1131 DBG("Upload dirty buf %d (%s) sz %d offset 0x%x\n", buf
->id
,
1132 buf
->name
, buf
->size
, block
->mem
->ofs
);
1134 assert(!(buf
->flags
& (BM_NO_BACKING_STORE
|BM_NO_EVICT
)));
1136 wait_quiescent(intel
, buf
->block
);
1138 do_memcpy(buf
->block
->virtual,
1142 if (intel
->aub_file
) {
1143 intel
->vtbl
.aub_gtt_data(intel
,
1144 buf
->block
->mem
->ofs
,
1154 else if (buf
->aub_dirty
) {
1155 intel
->vtbl
.aub_gtt_data(intel
,
1156 buf
->block
->mem
->ofs
,
1157 buf
->block
->virtual,
1164 block
->referenced
= 0;
1165 block
->on_hardware
= 1;
1166 move_to_tail(&bm
->on_hardware
, block
);
1174 assert(is_empty_list(&bm
->referenced
));
1184 void bmReleaseBuffers( struct intel_context
*intel
)
1186 struct bufmgr
*bm
= intel
->bm
;
1190 struct block
*block
, *tmp
;
1192 foreach_s (block
, tmp
, &bm
->referenced
) {
1194 DBG("remove block %p from referenced list\n", block
);
1196 if (block
->on_hardware
) {
1197 /* Return to the on-hardware list.
1199 move_to_tail(&bm
->on_hardware
, block
);
1201 else if (block
->fenced
) {
1204 /* Hmm - have to scan the fenced list to insert the
1205 * buffers in order. This is O(nm), but rare and the
1208 foreach (s
, &bm
->fenced
) {
1209 if (FENCE_LTE(block
->fence
, s
->fence
))
1213 move_to_tail(s
, block
);
1216 /* Return to the lru list:
1218 move_to_tail(&block
->pool
->lru
, block
);
1221 block
->referenced
= 0;
1230 /* This functionality is used by the buffer manager, not really sure
1231 * if we need to be exposing it in this way, probably libdrm will
1232 * offer equivalent calls.
1234 * For now they can stay, but will likely change/move before final:
1236 unsigned bmSetFence( struct intel_context
*intel
)
1238 assert(intel
->locked
);
1240 /* Emit MI_FLUSH here:
1242 if (intel
->bm
->need_fence
) {
1244 /* Emit a flush without using a batchbuffer. Can't rely on the
1245 * batchbuffer at this level really. Would really prefer that
1246 * the IRQ ioctly emitted the flush at the same time.
1249 dword
[0] = intel
->vtbl
.flush_cmd();
1251 intel_cmd_ioctl(intel
, (char *)&dword
, sizeof(dword
), GL_TRUE
);
1253 intel
->bm
->last_fence
= intelEmitIrqLocked( intel
);
1255 fence_blocks(intel
, intel
->bm
->last_fence
);
1257 intel
->vtbl
.note_fence(intel
, intel
->bm
->last_fence
);
1258 intel
->bm
->need_fence
= 0;
1260 if (intel
->thrashing
) {
1262 if (!intel
->thrashing
)
1263 DBG("not thrashing\n");
1266 intel
->bm
->free_on_hardware
= 0;
1269 return intel
->bm
->last_fence
;
1272 unsigned bmLockAndFence( struct intel_context
*intel
)
1274 if (intel
->bm
->need_fence
) {
1275 LOCK_HARDWARE(intel
);
1277 UNLOCK_HARDWARE(intel
);
1280 return intel
->bm
->last_fence
;
1284 void bmFinishFence( struct intel_context
*intel
, unsigned fence
)
1286 if (!bmTestFence(intel
, fence
)) {
1287 DBG("...wait on fence %d\n", fence
);
1288 intelWaitIrq( intel
, fence
);
1290 assert(bmTestFence(intel
, fence
));
1291 check_fenced(intel
);
1297 /* Specifically ignore texture memory sharing.
1298 * -- just evict everything
1299 * -- and wait for idle
1301 void bm_fake_NotifyContendedLockTake( struct intel_context
*intel
)
1303 struct bufmgr
*bm
= intel
->bm
;
1307 struct block
*block
, *tmp
;
1310 assert(is_empty_list(&bm
->referenced
));
1313 bmFinishFence(intel
, bmSetFence(intel
));
1315 for (i
= 0; i
< bm
->nr_pools
; i
++) {
1316 if (!(bm
->pool
[i
].flags
& BM_NO_EVICT
)) {
1317 foreach_s(block
, tmp
, &bm
->pool
[i
].lru
) {
1318 assert(bmTestFence(intel
, block
->fence
));
1319 set_dirty(intel
, block
->buf
);