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() */
122 unsigned free_on_hardware
;
125 unsigned need_fence
:1;
128 #define MAXFENCE 0x7fffffff
130 static GLboolean
FENCE_LTE( unsigned a
, unsigned b
)
135 if (a
< b
&& b
- a
< (1<<24))
138 if (a
> b
&& MAXFENCE
- a
+ b
< (1<<24))
144 int bmTestFence( struct intel_context
*intel
, unsigned fence
)
146 /* Slight problem with wrap-around:
148 return fence
== 0 || FENCE_LTE(fence
, intel
->sarea
->last_dispatch
);
152 int dolock = nr_attach > 1; \
153 if (dolock) _glthread_LOCK_MUTEX(bm->mutex)
156 if (dolock) _glthread_UNLOCK_MUTEX(bm->mutex)
160 static GLboolean
alloc_from_pool( struct intel_context
*intel
,
164 struct bufmgr
*bm
= intel
->bm
;
165 struct pool
*pool
= &bm
->pool
[pool_nr
];
166 struct block
*block
= (struct block
*)calloc(sizeof *block
, 1);
167 GLuint sz
, align
= (1<<buf
->alignment
);
172 sz
= (buf
->size
+ align
-1) & ~(align
-1);
174 block
->mem
= mmAllocMem(pool
->heap
,
182 make_empty_list(block
);
184 /* Insert at head or at tail???
186 insert_at_tail(&pool
->lru
, block
);
189 block
->virtual = pool
->virtual + block
->mem
->ofs
;
204 /* Release the card storage associated with buf:
206 static void free_block( struct intel_context
*intel
, struct block
*block
)
208 DBG("free block %p\n", block
);
215 if (block
->referenced
) {
216 _mesa_printf("tried to free block on referenced list\n");
219 else if (block
->on_hardware
) {
221 intel
->bm
->free_on_hardware
+= block
->mem
->size
;
223 else if (block
->fenced
) {
227 DBG(" - free immediately\n");
228 remove_from_list(block
);
230 mmFreeMem(block
->mem
);
236 static void alloc_backing_store( struct intel_context
*intel
, struct buffer
*buf
)
238 assert(!buf
->backing_store
);
239 assert(!(buf
->flags
& (BM_NO_EVICT
|BM_NO_BACKING_STORE
)));
241 buf
->backing_store
= ALIGN_MALLOC(buf
->size
, 64);
244 static void free_backing_store( struct intel_context
*intel
, struct buffer
*buf
)
246 assert(!(buf
->flags
& (BM_NO_EVICT
|BM_NO_BACKING_STORE
)));
248 if (buf
->backing_store
) {
249 ALIGN_FREE(buf
->backing_store
);
250 buf
->backing_store
= NULL
;
259 static void set_dirty( struct intel_context
*intel
,
262 if (buf
->flags
& BM_NO_BACKING_STORE
)
263 buf
->invalidate_cb(intel
, buf
->invalidate_ptr
);
265 assert(!(buf
->flags
& BM_NO_EVICT
));
267 DBG("set_dirty - buf %d\n", buf
->id
);
272 static int evict_lru( struct intel_context
*intel
, GLuint max_fence
, GLuint
*pool
)
274 struct bufmgr
*bm
= intel
->bm
;
275 struct block
*block
, *tmp
;
278 DBG("%s\n", __FUNCTION__
);
280 for (i
= 0; i
< bm
->nr_pools
; i
++) {
281 if (!(bm
->pool
[i
].flags
& BM_NO_EVICT
)) {
282 foreach_s(block
, tmp
, &bm
->pool
[i
].lru
) {
285 (block
->buf
->flags
& BM_NO_FENCE_SUBDATA
))
288 if (block
->fence
&& max_fence
&&
289 !FENCE_LTE(block
->fence
, max_fence
))
292 set_dirty(intel
, block
->buf
);
293 block
->buf
->block
= NULL
;
295 free_block(intel
, block
);
307 #define foreach_s_rev(ptr, t, list) \
308 for(ptr=(list)->prev,t=(ptr)->prev; list != ptr; ptr=t, t=(t)->prev)
310 static int evict_mru( struct intel_context
*intel
, GLuint
*pool
)
312 struct bufmgr
*bm
= intel
->bm
;
313 struct block
*block
, *tmp
;
316 DBG("%s\n", __FUNCTION__
);
318 for (i
= 0; i
< bm
->nr_pools
; i
++) {
319 if (!(bm
->pool
[i
].flags
& BM_NO_EVICT
)) {
320 foreach_s_rev(block
, tmp
, &bm
->pool
[i
].lru
) {
323 (block
->buf
->flags
& BM_NO_FENCE_SUBDATA
))
326 set_dirty(intel
, block
->buf
);
327 block
->buf
->block
= NULL
;
329 free_block(intel
, block
);
342 static int check_fenced( struct intel_context
*intel
)
344 struct bufmgr
*bm
= intel
->bm
;
345 struct block
*block
, *tmp
;
348 foreach_s(block
, tmp
, &bm
->fenced
) {
349 assert(block
->fenced
);
351 if (bmTestFence(intel
, block
->fence
)) {
356 DBG("delayed free: offset %x sz %x\n", block
->mem
->ofs
, block
->mem
->size
);
357 remove_from_list(block
);
358 mmFreeMem(block
->mem
);
362 DBG("return to lru: offset %x sz %x\n", block
->mem
->ofs
, block
->mem
->size
);
363 move_to_tail(&block
->pool
->lru
, block
);
369 /* Blocks are ordered by fence, so if one fails, all from
370 * here will fail also:
376 /* Also check the referenced list:
378 foreach_s(block
, tmp
, &bm
->referenced
) {
380 bmTestFence(intel
, block
->fence
)) {
386 DBG("%s: %d\n", __FUNCTION__
, ret
);
392 static void fence_blocks( struct intel_context
*intel
,
395 struct bufmgr
*bm
= intel
->bm
;
396 struct block
*block
, *tmp
;
398 foreach_s (block
, tmp
, &bm
->on_hardware
) {
399 DBG("Fence block %p (sz 0x%x buf %p) with fence %d\n", block
,
400 block
->mem
->size
, block
->buf
, fence
);
401 block
->fence
= fence
;
403 block
->on_hardware
= 0;
406 /* Move to tail of pending list here
408 move_to_tail(&bm
->fenced
, block
);
411 /* Also check the referenced list:
413 foreach_s (block
, tmp
, &bm
->referenced
) {
414 if (block
->on_hardware
) {
415 DBG("Fence block %p (sz 0x%x buf %p) with fence %d\n", block
,
416 block
->mem
->size
, block
->buf
, fence
);
418 block
->fence
= fence
;
419 block
->on_hardware
= 0;
425 bm
->last_fence
= fence
;
426 assert(is_empty_list(&bm
->on_hardware
));
432 static GLboolean
alloc_block( struct intel_context
*intel
,
435 struct bufmgr
*bm
= intel
->bm
;
438 assert(intel
->locked
);
440 DBG("%s 0x%x bytes (%s)\n", __FUNCTION__
, buf
->size
, buf
->name
);
442 for (i
= 0; i
< bm
->nr_pools
; i
++) {
443 if (!(bm
->pool
[i
].flags
& BM_NO_ALLOC
) &&
444 alloc_from_pool(intel
, i
, buf
)) {
446 DBG("%s --> 0x%x (sz %x)\n", __FUNCTION__
,
447 buf
->block
->mem
->ofs
, buf
->block
->mem
->size
);
453 DBG("%s --> fail\n", __FUNCTION__
);
458 static GLboolean
evict_and_alloc_block( struct intel_context
*intel
,
462 struct bufmgr
*bm
= intel
->bm
;
464 assert(buf
->block
== NULL
);
466 /* Put a cap on the amount of free memory we'll allow to accumulate
467 * before emitting a fence.
469 if (bm
->free_on_hardware
> 1 * 1024 * 1024) {
470 DBG("fence for free space: %x\n", bm
->free_on_hardware
);
474 /* Search for already free memory:
476 if (alloc_block(intel
, buf
))
479 /* Look for memory that may have become free:
481 if (check_fenced(intel
) &&
482 alloc_block(intel
, buf
))
485 /* Look for memory blocks not used for >1 frame:
487 while (evict_lru(intel
, intel
->second_last_swap_fence
, &pool
))
488 if (alloc_from_pool(intel
, pool
, buf
))
491 /* If we're not thrashing, allow lru eviction to dig deeper into
492 * recently used textures. We'll probably be thrashing soon:
494 if (!intel
->thrashing
) {
495 while (evict_lru(intel
, 0, &pool
))
496 if (alloc_from_pool(intel
, pool
, buf
))
500 /* Keep thrashing counter alive?
502 if (intel
->thrashing
)
503 intel
->thrashing
= 20;
505 /* Wait on any already pending fences - here we are waiting for any
506 * freed memory that has been submitted to hardware and fenced to
509 while (!is_empty_list(&bm
->fenced
)) {
510 GLuint fence
= bm
->fenced
.next
->fence
;
511 bmFinishFence(intel
, fence
);
513 if (alloc_block(intel
, buf
))
520 if (!is_empty_list(&bm
->on_hardware
)) {
523 while (!is_empty_list(&bm
->fenced
)) {
524 GLuint fence
= bm
->fenced
.next
->fence
;
525 bmFinishFence(intel
, fence
);
528 if (!intel
->thrashing
) {
531 intel
->thrashing
= 20;
533 if (alloc_block(intel
, buf
))
537 while (evict_mru(intel
, &pool
))
538 if (alloc_from_pool(intel
, pool
, buf
))
541 DBG("%s 0x%x bytes failed\n", __FUNCTION__
, buf
->size
);
543 assert(is_empty_list(&bm
->on_hardware
));
544 assert(is_empty_list(&bm
->fenced
));
558 /***********************************************************************
563 /* The initialization functions are skewed in the fake implementation.
564 * This call would be to attach to an existing manager, rather than to
565 * create a local one.
567 struct bufmgr
*bm_fake_intel_Attach( struct intel_context
*intel
)
569 _glthread_DECLARE_STATIC_MUTEX(initMutex
);
570 static struct bufmgr bm
;
572 /* This function needs a mutex of its own...
574 _glthread_LOCK_MUTEX(initMutex
);
576 if (nr_attach
== 0) {
577 _glthread_INIT_MUTEX(bm
.mutex
);
579 make_empty_list(&bm
.referenced
);
580 make_empty_list(&bm
.fenced
);
581 make_empty_list(&bm
.on_hardware
);
583 /* The context id of any of the share group. This won't be used
584 * in communication with the kernel, so it doesn't matter if
585 * this context is eventually deleted.
587 bm
.ctxId
= intel
->hHWContext
;
592 _glthread_UNLOCK_MUTEX(initMutex
);
599 /* The virtual pointer would go away in a true implementation.
601 int bmInitPool( struct intel_context
*intel
,
602 unsigned long low_offset
,
607 struct bufmgr
*bm
= intel
->bm
;
614 for (i
= 0; i
< bm
->nr_pools
; i
++) {
615 if (bm
->pool
[i
].low_offset
== low_offset
&&
616 bm
->pool
[i
].size
== size
) {
623 if (bm
->nr_pools
>= BM_POOL_MAX
)
628 DBG("bmInitPool %d low_offset %x sz %x\n",
629 i
, low_offset
, size
);
631 bm
->pool
[i
].low_offset
= low_offset
;
632 bm
->pool
[i
].size
= size
;
633 bm
->pool
[i
].heap
= mmInit( low_offset
, size
);
634 bm
->pool
[i
].virtual = low_virtual
- low_offset
;
635 bm
->pool
[i
].flags
= flags
;
637 make_empty_list(&bm
->pool
[i
].lru
);
647 static struct buffer
*do_GenBuffer(struct intel_context
*intel
, const char *name
, int align
)
649 struct bufmgr
*bm
= intel
->bm
;
650 struct buffer
*buf
= calloc(sizeof(*buf
), 1);
652 buf
->id
= ++bm
->buf_nr
;
654 buf
->alignment
= align
;
655 buf
->flags
= BM_MEM_AGP
|BM_MEM_VRAM
|BM_MEM_LOCAL
;
661 void *bmFindVirtual( struct intel_context
*intel
,
665 struct bufmgr
*bm
= intel
->bm
;
668 for (i
= 0; i
< bm
->nr_pools
; i
++)
669 if (offset
>= bm
->pool
[i
].low_offset
&&
670 offset
+ sz
<= bm
->pool
[i
].low_offset
+ bm
->pool
[i
].size
)
671 return bm
->pool
[i
].virtual + offset
;
677 void bmGenBuffers(struct intel_context
*intel
,
678 const char *name
, unsigned n
,
679 struct buffer
**buffers
,
682 struct bufmgr
*bm
= intel
->bm
;
687 for (i
= 0; i
< n
; i
++)
688 buffers
[i
] = do_GenBuffer(intel
, name
, align
);
694 void bmDeleteBuffers(struct intel_context
*intel
, unsigned n
, struct buffer
**buffers
)
696 struct bufmgr
*bm
= intel
->bm
;
702 for (i
= 0; i
< n
; i
++) {
703 struct buffer
*buf
= buffers
[i
];
705 if (buf
&& buf
->block
)
706 free_block(intel
, buf
->block
);
718 /* Hook to inform faked buffer manager about fixed-position
719 * front,depth,back buffers. These may move to a fully memory-managed
720 * scheme, or they may continue to be managed as is. It will probably
721 * be useful to pass a fixed offset here one day.
723 struct buffer
*bmGenBufferStatic(struct intel_context
*intel
,
726 struct bufmgr
*bm
= intel
->bm
;
730 assert(bm
->pool
[pool
].flags
& BM_NO_EVICT
);
731 assert(bm
->pool
[pool
].flags
& BM_NO_MOVE
);
733 if (bm
->pool
[pool
].static_buffer
)
734 buf
= bm
->pool
[pool
].static_buffer
;
736 buf
= do_GenBuffer(intel
, "static", 12);
738 bm
->pool
[pool
].static_buffer
= buf
;
741 buf
->size
= bm
->pool
[pool
].size
;
742 buf
->flags
= bm
->pool
[pool
].flags
;
745 if (!alloc_from_pool(intel
, pool
, buf
))
754 static void wait_quiescent(struct intel_context
*intel
,
757 if (block
->on_hardware
) {
758 assert(intel
->bm
->need_fence
);
760 assert(!block
->on_hardware
);
765 bmFinishFence(intel
, block
->fence
);
768 assert(!block
->on_hardware
);
769 assert(!block
->fenced
);
774 /* If buffer size changes, free and reallocate. Otherwise update in
777 int bmBufferData(struct intel_context
*intel
,
783 struct bufmgr
*bm
= intel
->bm
;
788 DBG("bmBufferData %d sz 0x%x data: %p\n", buf
->id
, size
, data
);
790 assert(!buf
->mapped
);
793 struct block
*block
= buf
->block
;
795 /* Optimistic check to see if we can reuse the block -- not
796 * required for correctness:
801 if (block
->on_hardware
||
803 (buf
->size
&& buf
->size
!= size
) ||
806 assert(!block
->referenced
);
808 free_block(intel
, block
);
816 assert (buf
->block
->mem
->size
>= size
);
819 if (buf
->flags
& (BM_NO_BACKING_STORE
|BM_NO_EVICT
)) {
821 assert(intel
->locked
|| data
== NULL
);
824 if (!buf
->block
&& !evict_and_alloc_block(intel
, buf
)) {
830 wait_quiescent(intel
, buf
->block
);
832 DBG("bmBufferData %d offset 0x%x sz 0x%x\n",
833 buf
->id
, buf
->block
->mem
->ofs
, size
);
835 assert(buf
->block
->virtual == buf
->block
->pool
->virtual + buf
->block
->mem
->ofs
);
837 do_memcpy(buf
->block
->virtual, data
, size
);
842 DBG("%s - set buf %d dirty\n", __FUNCTION__
, buf
->id
);
843 set_dirty(intel
, buf
);
844 free_backing_store(intel
, buf
);
847 alloc_backing_store(intel
, buf
);
848 do_memcpy(buf
->backing_store
, data
, size
);
858 /* Update the buffer in place, in whatever space it is currently resident:
860 int bmBufferSubData(struct intel_context
*intel
,
866 struct bufmgr
*bm
= intel
->bm
;
874 DBG("bmBufferSubdata %d offset 0x%x sz 0x%x\n", buf
->id
, offset
, size
);
876 assert(offset
+size
<= buf
->size
);
878 if (buf
->flags
& (BM_NO_EVICT
|BM_NO_BACKING_STORE
)) {
880 assert(intel
->locked
);
882 if (!buf
->block
&& !evict_and_alloc_block(intel
, buf
)) {
888 if (!(buf
->flags
& BM_NO_FENCE_SUBDATA
))
889 wait_quiescent(intel
, buf
->block
);
893 do_memcpy(buf
->block
->virtual + offset
, data
, size
);
896 DBG("%s - set buf %d dirty\n", __FUNCTION__
, buf
->id
);
897 set_dirty(intel
, buf
);
899 if (buf
->backing_store
== NULL
)
900 alloc_backing_store(intel
, buf
);
902 do_memcpy(buf
->backing_store
+ offset
, data
, size
);
912 int bmBufferDataAUB(struct intel_context
*intel
,
918 unsigned aubsubtype
)
920 int retval
= bmBufferData(intel
, buf
, size
, data
, flags
);
923 /* This only works because in this version of the buffer manager we
924 * allocate all buffers statically in agp space and so can emit the
925 * uploads to the aub file with the correct offsets as they happen.
927 if (retval
== 0 && data
&& intel
->aub_file
) {
929 if (buf
->block
&& !buf
->dirty
) {
930 intel
->vtbl
.aub_gtt_data(intel
,
931 buf
->block
->mem
->ofs
,
944 int bmBufferSubDataAUB(struct intel_context
*intel
,
950 unsigned aubsubtype
)
952 int retval
= bmBufferSubData(intel
, buf
, offset
, size
, data
);
955 /* This only works because in this version of the buffer manager we
956 * allocate all buffers statically in agp space and so can emit the
957 * uploads to the aub file with the correct offsets as they happen.
959 if (intel
->aub_file
) {
960 if (retval
== 0 && buf
->block
&& !buf
->dirty
)
961 intel
->vtbl
.aub_gtt_data(intel
,
962 buf
->block
->mem
->ofs
+ offset
,
963 ((const char *)buf
->block
->virtual) + offset
,
972 void bmUnmapBufferAUB( struct intel_context
*intel
,
975 unsigned aubsubtype
)
977 bmUnmapBuffer(intel
, buf
);
979 if (intel
->aub_file
) {
980 /* Hack - exclude the framebuffer mappings. If you removed
981 * this, you'd get very big aubfiles, but you *would* be able to
982 * see fallback rendering.
984 if (buf
->block
&& !buf
->dirty
&& buf
->block
->pool
== &intel
->bm
->pool
[0]) {
990 unsigned bmBufferOffset(struct intel_context
*intel
,
993 struct bufmgr
*bm
= intel
->bm
;
998 assert(intel
->locked
);
1001 !evict_and_alloc_block(intel
, buf
)) {
1007 assert(buf
->block
->buf
== buf
);
1009 DBG("Add buf %d (block %p, dirty %d) to referenced list\n", buf
->id
, buf
->block
,
1012 move_to_tail(&bm
->referenced
, buf
->block
);
1013 buf
->block
->referenced
= 1;
1015 retval
= buf
->block
->mem
->ofs
;
1025 /* Extract data from the buffer:
1027 void bmBufferGetSubData(struct intel_context
*intel
,
1033 struct bufmgr
*bm
= intel
->bm
;
1037 DBG("bmBufferSubdata %d offset 0x%x sz 0x%x\n", buf
->id
, offset
, size
);
1039 if (buf
->flags
& (BM_NO_EVICT
|BM_NO_BACKING_STORE
)) {
1040 if (buf
->block
&& size
) {
1041 wait_quiescent(intel
, buf
->block
);
1042 do_memcpy(data
, buf
->block
->virtual + offset
, size
);
1046 if (buf
->backing_store
&& size
) {
1047 do_memcpy(data
, buf
->backing_store
+ offset
, size
);
1055 /* Return a pointer to whatever space the buffer is currently resident in:
1057 void *bmMapBuffer( struct intel_context
*intel
,
1061 struct bufmgr
*bm
= intel
->bm
;
1062 void *retval
= NULL
;
1066 DBG("bmMapBuffer %d\n", buf
->id
);
1069 _mesa_printf("%s: already mapped\n", __FUNCTION__
);
1072 else if (buf
->flags
& (BM_NO_BACKING_STORE
|BM_NO_EVICT
)) {
1074 assert(intel
->locked
);
1076 if (!buf
->block
&& !evict_and_alloc_block(intel
, buf
)) {
1077 DBG("%s: alloc failed\n", __FUNCTION__
);
1085 if (!(buf
->flags
& BM_NO_FENCE_SUBDATA
))
1086 wait_quiescent(intel
, buf
->block
);
1089 retval
= buf
->block
->virtual;
1093 DBG("%s - set buf %d dirty\n", __FUNCTION__
, buf
->id
);
1094 set_dirty(intel
, buf
);
1096 if (buf
->backing_store
== 0)
1097 alloc_backing_store(intel
, buf
);
1100 retval
= buf
->backing_store
;
1107 void bmUnmapBuffer( struct intel_context
*intel
, struct buffer
*buf
)
1109 struct bufmgr
*bm
= intel
->bm
;
1113 DBG("bmUnmapBuffer %d\n", buf
->id
);
1122 /* This is the big hack that turns on BM_NO_BACKING_STORE. Basically
1123 * says that an external party will maintain the backing store, eg
1124 * Mesa's local copy of texture data.
1126 void bmBufferSetInvalidateCB(struct intel_context
*intel
,
1128 void (*invalidate_cb
)( struct intel_context
*, void *ptr
),
1130 GLboolean dont_fence_subdata
)
1132 struct bufmgr
*bm
= intel
->bm
;
1136 if (buf
->backing_store
)
1137 free_backing_store(intel
, buf
);
1139 buf
->flags
|= BM_NO_BACKING_STORE
;
1141 if (dont_fence_subdata
)
1142 buf
->flags
|= BM_NO_FENCE_SUBDATA
;
1144 DBG("bmBufferSetInvalidateCB set buf %d dirty\n", buf
->id
);
1146 buf
->invalidate_cb
= invalidate_cb
;
1147 buf
->invalidate_ptr
= ptr
;
1149 /* Note that it is invalid right from the start. Also note
1150 * invalidate_cb is called with the bufmgr locked, so cannot
1151 * itself make bufmgr calls.
1153 invalidate_cb( intel
, ptr
);
1164 /* This is only protected against thread interactions by the DRI lock
1165 * and the policy of ensuring that all dma is flushed prior to
1166 * releasing that lock. Otherwise you might have two threads building
1167 * up a list of buffers to validate at once.
1169 int bmValidateBuffers( struct intel_context
*intel
)
1171 struct bufmgr
*bm
= intel
->bm
;
1176 DBG("%s fail %d\n", __FUNCTION__
, bm
->fail
);
1177 assert(intel
->locked
);
1180 struct block
*block
, *tmp
;
1182 foreach_s(block
, tmp
, &bm
->referenced
) {
1183 struct buffer
*buf
= block
->buf
;
1185 DBG("Validate buf %d / block %p / dirty %d\n", buf
->id
, block
, buf
->dirty
);
1187 /* Upload the buffer contents if necessary:
1190 DBG("Upload dirty buf %d (%s) sz %d offset 0x%x\n", buf
->id
,
1191 buf
->name
, buf
->size
, block
->mem
->ofs
);
1193 assert(!(buf
->flags
& (BM_NO_BACKING_STORE
|BM_NO_EVICT
)));
1195 wait_quiescent(intel
, buf
->block
);
1197 do_memcpy(buf
->block
->virtual,
1201 if (intel
->aub_file
) {
1202 intel
->vtbl
.aub_gtt_data(intel
,
1203 buf
->block
->mem
->ofs
,
1213 else if (buf
->aub_dirty
) {
1214 intel
->vtbl
.aub_gtt_data(intel
,
1215 buf
->block
->mem
->ofs
,
1216 buf
->block
->virtual,
1223 block
->referenced
= 0;
1224 block
->on_hardware
= 1;
1225 move_to_tail(&bm
->on_hardware
, block
);
1231 retval
= bm
->fail
? -1 : 0;
1237 DBG("%s failed\n", __FUNCTION__
);
1245 void bmReleaseBuffers( struct intel_context
*intel
)
1247 struct bufmgr
*bm
= intel
->bm
;
1251 struct block
*block
, *tmp
;
1253 foreach_s (block
, tmp
, &bm
->referenced
) {
1255 DBG("remove block %p from referenced list\n", block
);
1257 if (block
->on_hardware
) {
1258 /* Return to the on-hardware list.
1260 move_to_tail(&bm
->on_hardware
, block
);
1262 else if (block
->fenced
) {
1265 /* Hmm - have to scan the fenced list to insert the
1266 * buffers in order. This is O(nm), but rare and the
1269 foreach (s
, &bm
->fenced
) {
1270 if (FENCE_LTE(block
->fence
, s
->fence
))
1274 move_to_tail(s
, block
);
1277 /* Return to the lru list:
1279 move_to_tail(&block
->pool
->lru
, block
);
1282 block
->referenced
= 0;
1289 /* This functionality is used by the buffer manager, not really sure
1290 * if we need to be exposing it in this way, probably libdrm will
1291 * offer equivalent calls.
1293 * For now they can stay, but will likely change/move before final:
1295 unsigned bmSetFence( struct intel_context
*intel
)
1297 assert(intel
->locked
);
1299 /* Emit MI_FLUSH here:
1301 if (intel
->bm
->need_fence
) {
1303 /* Emit a flush without using a batchbuffer. Can't rely on the
1304 * batchbuffer at this level really. Would really prefer that
1305 * the IRQ ioctly emitted the flush at the same time.
1308 dword
[0] = intel
->vtbl
.flush_cmd();
1310 intel_cmd_ioctl(intel
, (char *)&dword
, sizeof(dword
), GL_TRUE
);
1312 intel
->bm
->last_fence
= intelEmitIrqLocked( intel
);
1314 fence_blocks(intel
, intel
->bm
->last_fence
);
1316 intel
->vtbl
.note_fence(intel
, intel
->bm
->last_fence
);
1317 intel
->bm
->need_fence
= 0;
1319 if (intel
->thrashing
) {
1321 if (!intel
->thrashing
)
1322 DBG("not thrashing\n");
1325 intel
->bm
->free_on_hardware
= 0;
1328 return intel
->bm
->last_fence
;
1331 unsigned bmLockAndFence( struct intel_context
*intel
)
1333 if (intel
->bm
->need_fence
) {
1334 LOCK_HARDWARE(intel
);
1336 UNLOCK_HARDWARE(intel
);
1339 return intel
->bm
->last_fence
;
1343 void bmFinishFence( struct intel_context
*intel
, unsigned fence
)
1345 if (!bmTestFence(intel
, fence
)) {
1346 DBG("...wait on fence %d\n", fence
);
1347 intelWaitIrq( intel
, fence
);
1349 assert(bmTestFence(intel
, fence
));
1350 check_fenced(intel
);
1356 /* Specifically ignore texture memory sharing.
1357 * -- just evict everything
1358 * -- and wait for idle
1360 void bm_fake_NotifyContendedLockTake( struct intel_context
*intel
)
1362 struct bufmgr
*bm
= intel
->bm
;
1366 struct block
*block
, *tmp
;
1369 assert(is_empty_list(&bm
->referenced
));
1373 bmFinishFence(intel
, bmSetFence(intel
));
1375 assert(is_empty_list(&bm
->fenced
));
1376 assert(is_empty_list(&bm
->on_hardware
));
1378 for (i
= 0; i
< bm
->nr_pools
; i
++) {
1379 if (!(bm
->pool
[i
].flags
& BM_NO_EVICT
)) {
1380 foreach_s(block
, tmp
, &bm
->pool
[i
].lru
) {
1381 assert(bmTestFence(intel
, block
->fence
));
1382 set_dirty(intel
, block
->buf
);
1392 void bmEvictAll( struct intel_context
*intel
)
1394 struct bufmgr
*bm
= intel
->bm
;
1398 struct block
*block
, *tmp
;
1401 DBG("%s\n", __FUNCTION__
);
1403 assert(is_empty_list(&bm
->referenced
));
1407 bmFinishFence(intel
, bmSetFence(intel
));
1409 assert(is_empty_list(&bm
->fenced
));
1410 assert(is_empty_list(&bm
->on_hardware
));
1412 for (i
= 0; i
< bm
->nr_pools
; i
++) {
1413 if (!(bm
->pool
[i
].flags
& BM_NO_EVICT
)) {
1414 foreach_s(block
, tmp
, &bm
->pool
[i
].lru
) {
1415 assert(bmTestFence(intel
, block
->fence
));
1416 set_dirty(intel
, block
->buf
);
1417 block
->buf
->block
= NULL
;
1419 free_block(intel
, block
);
1428 GLboolean
bmError( struct intel_context
*intel
)
1430 struct bufmgr
*bm
= intel
->bm
;
1443 GLuint
bmCtxId( struct intel_context
*intel
)
1445 return intel
->bm
->ctxId
;