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);
169 block
->mem
= mmAllocMem(pool
->heap
, buf
->size
, buf
->alignment
, 0);
175 make_empty_list(block
);
177 /* Insert at head or at tail???
179 insert_at_tail(&pool
->lru
, block
);
182 block
->virtual = pool
->virtual + block
->mem
->ofs
;
197 /* Release the card storage associated with buf:
199 static void free_block( struct intel_context
*intel
, struct block
*block
)
201 DBG("free block %p\n", block
);
208 if (block
->referenced
) {
209 _mesa_printf("tried to free block on referenced list\n");
212 else if (block
->on_hardware
) {
214 intel
->bm
->free_on_hardware
+= block
->mem
->size
;
216 else if (block
->fenced
) {
220 DBG(" - free immediately\n");
221 remove_from_list(block
);
223 mmFreeMem(block
->mem
);
229 static void alloc_backing_store( struct intel_context
*intel
, struct buffer
*buf
)
231 assert(!buf
->backing_store
);
232 assert(!(buf
->flags
& (BM_NO_EVICT
|BM_NO_BACKING_STORE
)));
234 buf
->backing_store
= ALIGN_MALLOC(buf
->size
, 64);
237 static void free_backing_store( struct intel_context
*intel
, struct buffer
*buf
)
239 assert(!(buf
->flags
& (BM_NO_EVICT
|BM_NO_BACKING_STORE
)));
241 if (buf
->backing_store
) {
242 ALIGN_FREE(buf
->backing_store
);
243 buf
->backing_store
= NULL
;
252 static void set_dirty( struct intel_context
*intel
,
255 if (buf
->flags
& BM_NO_BACKING_STORE
)
256 buf
->invalidate_cb(intel
, buf
->invalidate_ptr
);
258 assert(!(buf
->flags
& BM_NO_EVICT
));
260 DBG("set_dirty - buf %d\n", buf
->id
);
265 static int evict_lru( struct intel_context
*intel
, GLuint max_fence
)
267 struct bufmgr
*bm
= intel
->bm
;
268 struct block
*block
, *tmp
;
271 DBG("%s\n", __FUNCTION__
);
273 for (i
= 0; i
< bm
->nr_pools
; i
++) {
274 if (!(bm
->pool
[i
].flags
& BM_NO_EVICT
)) {
275 foreach_s(block
, tmp
, &bm
->pool
[i
].lru
) {
278 (block
->buf
->flags
& BM_NO_FENCE_SUBDATA
))
281 if (block
->fence
&& max_fence
&&
282 !FENCE_LTE(block
->fence
, max_fence
))
285 set_dirty(intel
, block
->buf
);
286 block
->buf
->block
= NULL
;
288 free_block(intel
, block
);
299 #define foreach_s_rev(ptr, t, list) \
300 for(ptr=(list)->prev,t=(ptr)->prev; list != ptr; ptr=t, t=(t)->prev)
302 static int evict_mru( struct intel_context
*intel
)
304 struct bufmgr
*bm
= intel
->bm
;
305 struct block
*block
, *tmp
;
308 DBG("%s\n", __FUNCTION__
);
310 for (i
= 0; i
< bm
->nr_pools
; i
++) {
311 if (!(bm
->pool
[i
].flags
& BM_NO_EVICT
)) {
312 foreach_s_rev(block
, tmp
, &bm
->pool
[i
].lru
) {
315 (block
->buf
->flags
& BM_NO_FENCE_SUBDATA
))
318 set_dirty(intel
, block
->buf
);
319 block
->buf
->block
= NULL
;
321 free_block(intel
, block
);
333 static int check_fenced( struct intel_context
*intel
)
335 struct bufmgr
*bm
= intel
->bm
;
336 struct block
*block
, *tmp
;
339 foreach_s(block
, tmp
, &bm
->fenced
) {
340 assert(block
->fenced
);
342 if (bmTestFence(intel
, block
->fence
)) {
347 DBG("delayed free: offset %x sz %x\n", block
->mem
->ofs
, block
->mem
->size
);
348 remove_from_list(block
);
349 mmFreeMem(block
->mem
);
353 DBG("return to lru: offset %x sz %x\n", block
->mem
->ofs
, block
->mem
->size
);
354 move_to_tail(&block
->pool
->lru
, block
);
360 /* Blocks are ordered by fence, so if one fails, all from
361 * here will fail also:
367 /* Also check the referenced list:
369 foreach_s(block
, tmp
, &bm
->referenced
) {
371 bmTestFence(intel
, block
->fence
)) {
377 DBG("%s: %d\n", __FUNCTION__
, ret
);
383 static void fence_blocks( struct intel_context
*intel
,
386 struct bufmgr
*bm
= intel
->bm
;
387 struct block
*block
, *tmp
;
389 foreach_s (block
, tmp
, &bm
->on_hardware
) {
390 DBG("Fence block %p (sz 0x%x buf %p) with fence %d\n", block
,
391 block
->mem
->size
, block
->buf
, fence
);
392 block
->fence
= fence
;
394 block
->on_hardware
= 0;
397 /* Move to tail of pending list here
399 move_to_tail(&bm
->fenced
, block
);
402 /* Also check the referenced list:
404 foreach_s (block
, tmp
, &bm
->referenced
) {
405 if (block
->on_hardware
) {
406 DBG("Fence block %p (sz 0x%x buf %p) with fence %d\n", block
,
407 block
->mem
->size
, block
->buf
, fence
);
409 block
->fence
= fence
;
410 block
->on_hardware
= 0;
416 bm
->last_fence
= fence
;
417 assert(is_empty_list(&bm
->on_hardware
));
423 static GLboolean
alloc_block( struct intel_context
*intel
,
426 struct bufmgr
*bm
= intel
->bm
;
429 DBG("%s 0x%x bytes (%s)\n", __FUNCTION__
, buf
->size
, buf
->name
);
431 for (i
= 0; i
< bm
->nr_pools
; i
++) {
432 if (!(bm
->pool
[i
].flags
& BM_NO_ALLOC
) &&
433 alloc_from_pool(intel
, i
, buf
)) {
435 DBG("%s --> 0x%x (sz %x)\n", __FUNCTION__
,
436 buf
->block
->mem
->ofs
, buf
->block
->mem
->size
);
442 DBG("%s --> fail\n", __FUNCTION__
);
447 static GLboolean
evict_and_alloc_block( struct intel_context
*intel
,
450 struct bufmgr
*bm
= intel
->bm
;
452 assert(buf
->block
== NULL
);
454 /* Put a cap on the amount of free memory we'll allow to accumulate
455 * before emitting a fence.
457 if (bm
->free_on_hardware
> 1 * 1024 * 1024) {
458 DBG("fence for free space: %x\n", bm
->free_on_hardware
);
462 /* Search for already free memory:
464 if (alloc_block(intel
, buf
))
467 /* Look for memory that may have become free:
469 if (check_fenced(intel
) &&
470 alloc_block(intel
, buf
))
473 /* Look for memory blocks not used for >1 frame:
475 while (evict_lru(intel
, intel
->second_last_swap_fence
))
476 if (alloc_block(intel
, buf
))
479 /* If we're not thrashing, allow lru eviction to dig deeper into
480 * recently used textures. We'll probably be thrashing soon:
482 if (!intel
->thrashing
) {
483 while (evict_lru(intel
, 0))
484 if (alloc_block(intel
, buf
))
488 /* Keep thrashing counter alive?
490 if (intel
->thrashing
)
491 intel
->thrashing
= 20;
493 /* Wait on any already pending fences - here we are waiting for any
494 * freed memory that has been submitted to hardware and fenced to
497 while (!is_empty_list(&bm
->fenced
)) {
498 GLuint fence
= bm
->fenced
.next
->fence
;
499 bmFinishFence(intel
, fence
);
501 if (alloc_block(intel
, buf
))
508 if (!is_empty_list(&bm
->on_hardware
)) {
511 if (!is_empty_list(&bm
->fenced
)) {
512 GLuint fence
= bm
->fenced
.next
->fence
;
513 bmFinishFence(intel
, fence
);
516 if (!intel
->thrashing
) {
519 intel
->thrashing
= 20;
521 if (alloc_block(intel
, buf
))
525 while (evict_mru(intel
))
526 if (alloc_block(intel
, buf
))
541 /***********************************************************************
546 /* The initialization functions are skewed in the fake implementation.
547 * This call would be to attach to an existing manager, rather than to
548 * create a local one.
550 struct bufmgr
*bm_fake_intel_Attach( struct intel_context
*intel
)
552 _glthread_DECLARE_STATIC_MUTEX(initMutex
);
553 static struct bufmgr bm
;
555 /* This function needs a mutex of its own...
557 _glthread_LOCK_MUTEX(initMutex
);
559 if (nr_attach
== 0) {
560 _glthread_INIT_MUTEX(bm
.mutex
);
562 make_empty_list(&bm
.referenced
);
563 make_empty_list(&bm
.fenced
);
564 make_empty_list(&bm
.on_hardware
);
569 _glthread_UNLOCK_MUTEX(initMutex
);
576 /* The virtual pointer would go away in a true implementation.
578 int bmInitPool( struct intel_context
*intel
,
579 unsigned long low_offset
,
584 struct bufmgr
*bm
= intel
->bm
;
591 for (i
= 0; i
< bm
->nr_pools
; i
++) {
592 if (bm
->pool
[i
].low_offset
== low_offset
&&
593 bm
->pool
[i
].size
== size
) {
600 if (bm
->nr_pools
>= BM_POOL_MAX
)
605 DBG("bmInitPool %d low_offset %x sz %x\n",
606 i
, low_offset
, size
);
608 bm
->pool
[i
].low_offset
= low_offset
;
609 bm
->pool
[i
].size
= size
;
610 bm
->pool
[i
].heap
= mmInit( low_offset
, size
);
611 bm
->pool
[i
].virtual = low_virtual
- low_offset
;
612 bm
->pool
[i
].flags
= flags
;
614 make_empty_list(&bm
->pool
[i
].lru
);
624 static struct buffer
*do_GenBuffer(struct intel_context
*intel
, const char *name
)
626 struct bufmgr
*bm
= intel
->bm
;
627 struct buffer
*buf
= calloc(sizeof(*buf
), 1);
629 buf
->id
= ++bm
->buf_nr
;
631 buf
->alignment
= 12; /* page-alignment to fit in with AGP swapping */
632 buf
->flags
= BM_MEM_AGP
|BM_MEM_VRAM
|BM_MEM_LOCAL
;
639 void bmGenBuffers(struct intel_context
*intel
,
640 const char *name
, unsigned n
,
641 struct buffer
**buffers
)
643 struct bufmgr
*bm
= intel
->bm
;
648 for (i
= 0; i
< n
; i
++)
649 buffers
[i
] = do_GenBuffer(intel
, name
);
655 void bmDeleteBuffers(struct intel_context
*intel
, unsigned n
, struct buffer
**buffers
)
657 struct bufmgr
*bm
= intel
->bm
;
663 for (i
= 0; i
< n
; i
++) {
664 struct buffer
*buf
= buffers
[i
];
666 if (buf
&& buf
->block
)
667 free_block(intel
, buf
->block
);
679 /* Hook to inform faked buffer manager about fixed-position
680 * front,depth,back buffers. These may move to a fully memory-managed
681 * scheme, or they may continue to be managed as is. It will probably
682 * be useful to pass a fixed offset here one day.
684 struct buffer
*bmGenBufferStatic(struct intel_context
*intel
,
687 struct bufmgr
*bm
= intel
->bm
;
691 assert(bm
->pool
[pool
].flags
& BM_NO_EVICT
);
692 assert(bm
->pool
[pool
].flags
& BM_NO_MOVE
);
694 if (bm
->pool
[pool
].static_buffer
)
695 buf
= bm
->pool
[pool
].static_buffer
;
697 buf
= do_GenBuffer(intel
, "static");
699 bm
->pool
[pool
].static_buffer
= buf
;
702 buf
->size
= bm
->pool
[pool
].size
;
703 buf
->flags
= bm
->pool
[pool
].flags
;
706 if (!alloc_from_pool(intel
, pool
, buf
))
715 static void wait_quiescent(struct intel_context
*intel
,
718 if (block
->on_hardware
) {
719 assert(intel
->bm
->need_fence
);
721 assert(!block
->on_hardware
);
726 bmFinishFence(intel
, block
->fence
);
729 assert(!block
->on_hardware
);
730 assert(!block
->fenced
);
735 /* If buffer size changes, free and reallocate. Otherwise update in
738 void bmBufferData(struct intel_context
*intel
,
744 struct bufmgr
*bm
= intel
->bm
;
748 DBG("bmBufferData %d sz 0x%x data: %p\n", buf
->id
, size
, data
);
750 assert(!buf
->mapped
);
753 struct block
*block
= buf
->block
;
755 /* Optimistic check to see if we can reuse the block -- not
756 * required for correctness:
761 if (block
->on_hardware
||
763 (buf
->size
&& buf
->size
!= size
) ||
766 assert(!block
->referenced
);
768 free_block(intel
, block
);
776 assert (buf
->block
->mem
->size
== size
);
779 if (buf
->flags
& (BM_NO_BACKING_STORE
|BM_NO_EVICT
)) {
781 if (!buf
->block
&& !evict_and_alloc_block(intel
, buf
))
784 wait_quiescent(intel
, buf
->block
);
786 DBG("bmBufferData %d offset 0x%x sz 0x%x\n",
787 buf
->id
, buf
->block
->mem
->ofs
, size
);
789 assert(buf
->block
->virtual == buf
->block
->pool
->virtual + buf
->block
->mem
->ofs
);
791 do_memcpy(buf
->block
->virtual, data
, size
);
796 DBG("%s - set buf %d dirty\n", __FUNCTION__
, buf
->id
);
797 set_dirty(intel
, buf
);
798 free_backing_store(intel
, buf
);
801 alloc_backing_store(intel
, buf
);
802 do_memcpy(buf
->backing_store
, data
, size
);
810 /* Update the buffer in place, in whatever space it is currently resident:
812 void bmBufferSubData(struct intel_context
*intel
,
818 struct bufmgr
*bm
= intel
->bm
;
825 DBG("bmBufferSubdata %d offset 0x%x sz 0x%x\n", buf
->id
, offset
, size
);
827 assert(offset
+size
<= buf
->size
);
829 if (buf
->flags
& (BM_NO_EVICT
|BM_NO_BACKING_STORE
)) {
830 if (!buf
->block
&& !evict_and_alloc_block(intel
, buf
))
833 if (!(buf
->flags
& BM_NO_FENCE_SUBDATA
))
834 wait_quiescent(intel
, buf
->block
);
838 do_memcpy(buf
->block
->virtual + offset
, data
, size
);
841 DBG("%s - set buf %d dirty\n", __FUNCTION__
, buf
->id
);
842 set_dirty(intel
, buf
);
844 if (buf
->backing_store
== NULL
)
845 alloc_backing_store(intel
, buf
);
847 do_memcpy(buf
->backing_store
+ offset
, data
, size
);
855 void bmBufferDataAUB(struct intel_context
*intel
,
861 unsigned aubsubtype
)
863 bmBufferData(intel
, buf
, size
, data
, flags
);
866 /* This only works because in this version of the buffer manager we
867 * allocate all buffers statically in agp space and so can emit the
868 * uploads to the aub file with the correct offsets as they happen.
870 if (data
&& intel
->aub_file
) {
872 if (buf
->block
&& !buf
->dirty
) {
873 intel
->vtbl
.aub_gtt_data(intel
,
874 buf
->block
->mem
->ofs
,
885 void bmBufferSubDataAUB(struct intel_context
*intel
,
891 unsigned aubsubtype
)
893 bmBufferSubData(intel
, buf
, offset
, size
, data
);
896 /* This only works because in this version of the buffer manager we
897 * allocate all buffers statically in agp space and so can emit the
898 * uploads to the aub file with the correct offsets as they happen.
900 if (intel
->aub_file
) {
901 if (buf
->block
&& !buf
->dirty
)
902 intel
->vtbl
.aub_gtt_data(intel
,
903 buf
->block
->mem
->ofs
+ offset
,
904 ((const char *)buf
->block
->virtual) + offset
,
911 void bmUnmapBufferAUB( struct intel_context
*intel
,
914 unsigned aubsubtype
)
916 bmUnmapBuffer(intel
, buf
);
918 if (intel
->aub_file
) {
919 /* Hack - exclude the framebuffer mappings. If you removed
920 * this, you'd get very big aubfiles, but you *would* be able to
921 * see fallback rendering.
923 if (buf
->block
&& !buf
->dirty
&& buf
->block
->pool
== &intel
->bm
->pool
[0]) {
929 unsigned bmBufferOffset(struct intel_context
*intel
,
932 struct bufmgr
*bm
= intel
->bm
;
937 assert(intel
->locked
);
940 !evict_and_alloc_block(intel
, buf
)) {
946 assert(buf
->block
->buf
== buf
);
948 DBG("Add buf %d (block %p, dirty %d) to referenced list\n", buf
->id
, buf
->block
,
951 move_to_tail(&bm
->referenced
, buf
->block
);
952 buf
->block
->referenced
= 1;
954 retval
= buf
->block
->mem
->ofs
;
964 /* Extract data from the buffer:
966 void bmBufferGetSubData(struct intel_context
*intel
,
972 struct bufmgr
*bm
= intel
->bm
;
976 DBG("bmBufferSubdata %d offset 0x%x sz 0x%x\n", buf
->id
, offset
, size
);
978 if (buf
->flags
& (BM_NO_EVICT
|BM_NO_BACKING_STORE
)) {
979 if (buf
->block
&& size
) {
980 wait_quiescent(intel
, buf
->block
);
981 do_memcpy(data
, buf
->block
->virtual + offset
, size
);
985 if (buf
->backing_store
&& size
) {
986 do_memcpy(data
, buf
->backing_store
+ offset
, size
);
994 /* Return a pointer to whatever space the buffer is currently resident in:
996 void *bmMapBuffer( struct intel_context
*intel
,
1000 struct bufmgr
*bm
= intel
->bm
;
1005 DBG("bmMapBuffer %d\n", buf
->id
);
1008 _mesa_printf("%s: already mapped\n", __FUNCTION__
);
1011 else if (buf
->flags
& (BM_NO_BACKING_STORE
|BM_NO_EVICT
)) {
1012 if (!buf
->block
&& !evict_and_alloc_block(intel
, buf
)) {
1013 _mesa_printf("%s: alloc failed\n", __FUNCTION__
);
1020 if (!(buf
->flags
& BM_NO_FENCE_SUBDATA
))
1021 wait_quiescent(intel
, buf
->block
);
1024 retval
= buf
->block
->virtual;
1028 DBG("%s - set buf %d dirty\n", __FUNCTION__
, buf
->id
);
1029 set_dirty(intel
, buf
);
1031 if (buf
->backing_store
== 0)
1032 alloc_backing_store(intel
, buf
);
1035 retval
= buf
->backing_store
;
1042 void bmUnmapBuffer( struct intel_context
*intel
, struct buffer
*buf
)
1044 struct bufmgr
*bm
= intel
->bm
;
1048 DBG("bmUnmapBuffer %d\n", buf
->id
);
1057 /* This is the big hack that turns on BM_NO_BACKING_STORE. Basically
1058 * says that an external party will maintain the backing store, eg
1059 * Mesa's local copy of texture data.
1061 void bmBufferSetInvalidateCB(struct intel_context
*intel
,
1063 void (*invalidate_cb
)( struct intel_context
*, void *ptr
),
1065 GLboolean dont_fence_subdata
)
1067 struct bufmgr
*bm
= intel
->bm
;
1071 if (buf
->backing_store
)
1072 free_backing_store(intel
, buf
);
1074 buf
->flags
|= BM_NO_BACKING_STORE
;
1076 if (dont_fence_subdata
)
1077 buf
->flags
|= BM_NO_FENCE_SUBDATA
;
1079 DBG("bmBufferSetInvalidateCB set buf %d dirty\n", buf
->id
);
1081 buf
->invalidate_cb
= invalidate_cb
;
1082 buf
->invalidate_ptr
= ptr
;
1084 /* Note that it is invalid right from the start. Also note
1085 * invalidate_cb is called with the bufmgr locked, so cannot
1086 * itself make bufmgr calls.
1088 invalidate_cb( intel
, ptr
);
1099 /* This is only protected against thread interactions by the DRI lock
1100 * and the policy of ensuring that all dma is flushed prior to
1101 * releasing that lock. Otherwise you might have two threads building
1102 * up a list of buffers to validate at once.
1104 int bmValidateBuffers( struct intel_context
*intel
)
1106 struct bufmgr
*bm
= intel
->bm
;
1111 DBG("%s fail %d\n", __FUNCTION__
, bm
->fail
);
1114 struct block
*block
, *tmp
;
1116 foreach_s(block
, tmp
, &bm
->referenced
) {
1117 struct buffer
*buf
= block
->buf
;
1119 DBG("Validate buf %d / block %p / dirty %d\n", buf
->id
, block
, buf
->dirty
);
1121 /* Upload the buffer contents if necessary:
1124 DBG("Upload dirty buf %d (%s) sz %d offset 0x%x\n", buf
->id
,
1125 buf
->name
, buf
->size
, block
->mem
->ofs
);
1127 assert(!(buf
->flags
& (BM_NO_BACKING_STORE
|BM_NO_EVICT
)));
1129 wait_quiescent(intel
, buf
->block
);
1131 do_memcpy(buf
->block
->virtual,
1135 if (intel
->aub_file
) {
1136 intel
->vtbl
.aub_gtt_data(intel
,
1137 buf
->block
->mem
->ofs
,
1147 else if (buf
->aub_dirty
) {
1148 intel
->vtbl
.aub_gtt_data(intel
,
1149 buf
->block
->mem
->ofs
,
1150 buf
->block
->virtual,
1157 block
->referenced
= 0;
1158 block
->on_hardware
= 1;
1159 move_to_tail(&bm
->on_hardware
, block
);
1167 assert(is_empty_list(&bm
->referenced
));
1177 void bmReleaseBuffers( struct intel_context
*intel
)
1179 struct bufmgr
*bm
= intel
->bm
;
1183 struct block
*block
, *tmp
;
1185 foreach_s (block
, tmp
, &bm
->referenced
) {
1187 DBG("remove block %p from referenced list\n", block
);
1189 if (block
->on_hardware
) {
1190 /* Return to the on-hardware list.
1192 move_to_tail(&bm
->on_hardware
, block
);
1194 else if (block
->fenced
) {
1197 /* Hmm - have to scan the fenced list to insert the
1198 * buffers in order. This is O(nm), but rare and the
1201 foreach (s
, &bm
->fenced
) {
1202 if (FENCE_LTE(block
->fence
, s
->fence
))
1206 move_to_tail(s
, block
);
1209 /* Return to the lru list:
1211 move_to_tail(&block
->pool
->lru
, block
);
1214 block
->referenced
= 0;
1223 /* This functionality is used by the buffer manager, not really sure
1224 * if we need to be exposing it in this way, probably libdrm will
1225 * offer equivalent calls.
1227 * For now they can stay, but will likely change/move before final:
1229 unsigned bmSetFence( struct intel_context
*intel
)
1231 assert(intel
->locked
);
1233 /* Emit MI_FLUSH here:
1235 if (intel
->bm
->need_fence
) {
1237 /* Emit a flush without using a batchbuffer. Can't rely on the
1238 * batchbuffer at this level really. Would really prefer that
1239 * the IRQ ioctly emitted the flush at the same time.
1242 dword
[0] = intel
->vtbl
.flush_cmd();
1244 intel_cmd_ioctl(intel
, (char *)&dword
, sizeof(dword
), GL_TRUE
);
1246 intel
->bm
->last_fence
= intelEmitIrqLocked( intel
);
1248 fence_blocks(intel
, intel
->bm
->last_fence
);
1250 intel
->vtbl
.note_fence(intel
, intel
->bm
->last_fence
);
1251 intel
->bm
->need_fence
= 0;
1253 if (intel
->thrashing
) {
1255 if (!intel
->thrashing
)
1256 DBG("not thrashing\n");
1259 intel
->bm
->free_on_hardware
= 0;
1262 return intel
->bm
->last_fence
;
1265 unsigned bmLockAndFence( struct intel_context
*intel
)
1267 if (intel
->bm
->need_fence
) {
1268 LOCK_HARDWARE(intel
);
1270 UNLOCK_HARDWARE(intel
);
1273 return intel
->bm
->last_fence
;
1277 void bmFinishFence( struct intel_context
*intel
, unsigned fence
)
1279 if (!bmTestFence(intel
, fence
)) {
1280 DBG("...wait on fence %d\n", fence
);
1281 intelWaitIrq( intel
, fence
);
1283 assert(bmTestFence(intel
, fence
));
1284 check_fenced(intel
);
1290 /* Specifically ignore texture memory sharing.
1291 * -- just evict everything
1292 * -- and wait for idle
1294 void bm_fake_NotifyContendedLockTake( struct intel_context
*intel
)
1296 struct bufmgr
*bm
= intel
->bm
;
1300 struct block
*block
, *tmp
;
1303 assert(is_empty_list(&bm
->referenced
));
1306 bmFinishFence(intel
, bmSetFence(intel
));
1308 for (i
= 0; i
< bm
->nr_pools
; i
++) {
1309 if (!(bm
->pool
[i
].flags
& BM_NO_EVICT
)) {
1310 foreach_s(block
, tmp
, &bm
->pool
[i
].lru
) {
1311 assert(bmTestFence(intel
, block
->fence
));
1312 set_dirty(intel
, block
->buf
);